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 | * |
Patrick McHardy | f229f6c | 2013-04-06 15:24:29 +0200 | [diff] [blame] | 4 | * Copyright (c) 2004 Kiran Kumar Immidi <immidi_kiran@yahoo.com> |
| 5 | * Copyright (c) 2004-2012 Patrick McHardy <kaber@trash.net> |
| 6 | * |
YOSHIFUJI Hideaki | 601e68e | 2007-02-12 11:15:49 -0800 | [diff] [blame] | 7 | * 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] | 8 | * are to this RFC. |
YOSHIFUJI Hideaki | 601e68e | 2007-02-12 11:15:49 -0800 | [diff] [blame] | 9 | * |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 10 | * This program is free software; you can redistribute it and/or modify |
| 11 | * it under the terms of the GNU General Public License version 2 as |
| 12 | * published by the Free Software Foundation. |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 13 | */ |
| 14 | |
| 15 | #include <linux/types.h> |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 16 | #include <linux/timer.h> |
| 17 | #include <linux/netfilter.h> |
| 18 | #include <linux/module.h> |
| 19 | #include <linux/in.h> |
| 20 | #include <linux/ip.h> |
| 21 | #include <linux/sctp.h> |
| 22 | #include <linux/string.h> |
| 23 | #include <linux/seq_file.h> |
Yasuyuki Kozakai | 40a839f | 2006-06-27 03:00:35 -0700 | [diff] [blame] | 24 | #include <linux/spinlock.h> |
| 25 | #include <linux/interrupt.h> |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 26 | |
| 27 | #include <net/netfilter/nf_conntrack.h> |
Martin Josefsson | 605dcad | 2006-11-29 02:35:06 +0100 | [diff] [blame] | 28 | #include <net/netfilter/nf_conntrack_l4proto.h> |
Martin Josefsson | f618012 | 2006-11-29 02:35:01 +0100 | [diff] [blame] | 29 | #include <net/netfilter/nf_conntrack_ecache.h> |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 30 | |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 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", |
Michal Kubeček | d7ee351 | 2015-07-17 16:17:56 +0200 | [diff] [blame] | 45 | "HEARTBEAT_SENT", |
| 46 | "HEARTBEAT_ACKED", |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 47 | }; |
| 48 | |
| 49 | #define SECS * HZ |
| 50 | #define MINS * 60 SECS |
| 51 | #define HOURS * 60 MINS |
| 52 | #define DAYS * 24 HOURS |
| 53 | |
Patrick McHardy | 86c0bf4 | 2008-01-14 23:48:17 -0800 | [diff] [blame] | 54 | static unsigned int sctp_timeouts[SCTP_CONNTRACK_MAX] __read_mostly = { |
| 55 | [SCTP_CONNTRACK_CLOSED] = 10 SECS, |
| 56 | [SCTP_CONNTRACK_COOKIE_WAIT] = 3 SECS, |
| 57 | [SCTP_CONNTRACK_COOKIE_ECHOED] = 3 SECS, |
| 58 | [SCTP_CONNTRACK_ESTABLISHED] = 5 DAYS, |
| 59 | [SCTP_CONNTRACK_SHUTDOWN_SENT] = 300 SECS / 1000, |
| 60 | [SCTP_CONNTRACK_SHUTDOWN_RECD] = 300 SECS / 1000, |
| 61 | [SCTP_CONNTRACK_SHUTDOWN_ACK_SENT] = 3 SECS, |
Michal Kubeček | d7ee351 | 2015-07-17 16:17:56 +0200 | [diff] [blame] | 62 | [SCTP_CONNTRACK_HEARTBEAT_SENT] = 30 SECS, |
| 63 | [SCTP_CONNTRACK_HEARTBEAT_ACKED] = 210 SECS, |
Patrick McHardy | 86c0bf4 | 2008-01-14 23:48:17 -0800 | [diff] [blame] | 64 | }; |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 65 | |
| 66 | #define sNO SCTP_CONNTRACK_NONE |
| 67 | #define sCL SCTP_CONNTRACK_CLOSED |
| 68 | #define sCW SCTP_CONNTRACK_COOKIE_WAIT |
| 69 | #define sCE SCTP_CONNTRACK_COOKIE_ECHOED |
| 70 | #define sES SCTP_CONNTRACK_ESTABLISHED |
| 71 | #define sSS SCTP_CONNTRACK_SHUTDOWN_SENT |
| 72 | #define sSR SCTP_CONNTRACK_SHUTDOWN_RECD |
| 73 | #define sSA SCTP_CONNTRACK_SHUTDOWN_ACK_SENT |
Michal Kubeček | d7ee351 | 2015-07-17 16:17:56 +0200 | [diff] [blame] | 74 | #define sHS SCTP_CONNTRACK_HEARTBEAT_SENT |
| 75 | #define sHA SCTP_CONNTRACK_HEARTBEAT_ACKED |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 76 | #define sIV SCTP_CONNTRACK_MAX |
| 77 | |
YOSHIFUJI Hideaki | 601e68e | 2007-02-12 11:15:49 -0800 | [diff] [blame] | 78 | /* |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 79 | These are the descriptions of the states: |
| 80 | |
YOSHIFUJI Hideaki | 601e68e | 2007-02-12 11:15:49 -0800 | [diff] [blame] | 81 | NOTE: These state names are tantalizingly similar to the states of an |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 82 | SCTP endpoint. But the interpretation of the states is a little different, |
YOSHIFUJI Hideaki | 601e68e | 2007-02-12 11:15:49 -0800 | [diff] [blame] | 83 | 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] | 84 | point. Please note the subtleties. -Kiran |
| 85 | |
| 86 | NONE - Nothing so far. |
YOSHIFUJI Hideaki | 601e68e | 2007-02-12 11:15:49 -0800 | [diff] [blame] | 87 | COOKIE WAIT - We have seen an INIT chunk in the original direction, or also |
| 88 | an INIT_ACK chunk in the reply direction. |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 89 | COOKIE ECHOED - We have seen a COOKIE_ECHO chunk in the original direction. |
| 90 | ESTABLISHED - We have seen a COOKIE_ACK in the reply direction. |
| 91 | SHUTDOWN_SENT - We have seen a SHUTDOWN chunk in the original direction. |
| 92 | SHUTDOWN_RECD - We have seen a SHUTDOWN chunk in the reply directoin. |
| 93 | 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] | 94 | to that of the SHUTDOWN chunk. |
| 95 | CLOSED - We have seen a SHUTDOWN_COMPLETE chunk in the direction of |
| 96 | the SHUTDOWN chunk. Connection is closed. |
Michal Kubeček | d7ee351 | 2015-07-17 16:17:56 +0200 | [diff] [blame] | 97 | HEARTBEAT_SENT - We have seen a HEARTBEAT in a new flow. |
| 98 | HEARTBEAT_ACKED - We have seen a HEARTBEAT-ACK in the direction opposite to |
| 99 | that of the HEARTBEAT chunk. Secondary connection is |
| 100 | established. |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 101 | */ |
| 102 | |
| 103 | /* TODO |
YOSHIFUJI Hideaki | 601e68e | 2007-02-12 11:15:49 -0800 | [diff] [blame] | 104 | - I have assumed that the first INIT is in the original direction. |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 105 | This messes things when an INIT comes in the reply direction in CLOSED |
| 106 | state. |
YOSHIFUJI Hideaki | 601e68e | 2007-02-12 11:15:49 -0800 | [diff] [blame] | 107 | - Check the error type in the reply dir before transitioning from |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 108 | cookie echoed to closed. |
| 109 | - Sec 5.2.4 of RFC 2960 |
Michal Kubeček | d7ee351 | 2015-07-17 16:17:56 +0200 | [diff] [blame] | 110 | - Full Multi Homing support. |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 111 | */ |
| 112 | |
| 113 | /* SCTP conntrack state transitions */ |
Michal Kubeček | d7ee351 | 2015-07-17 16:17:56 +0200 | [diff] [blame] | 114 | static const u8 sctp_conntracks[2][11][SCTP_CONNTRACK_MAX] = { |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 115 | { |
| 116 | /* ORIGINAL */ |
Michal Kubeček | d7ee351 | 2015-07-17 16:17:56 +0200 | [diff] [blame] | 117 | /* sNO, sCL, sCW, sCE, sES, sSS, sSR, sSA, sHS, sHA */ |
| 118 | /* init */ {sCW, sCW, sCW, sCE, sES, sSS, sSR, sSA, sCW, sHA}, |
| 119 | /* init_ack */ {sCL, sCL, sCW, sCE, sES, sSS, sSR, sSA, sCL, sHA}, |
| 120 | /* abort */ {sCL, sCL, sCL, sCL, sCL, sCL, sCL, sCL, sCL, sCL}, |
| 121 | /* shutdown */ {sCL, sCL, sCW, sCE, sSS, sSS, sSR, sSA, sCL, sSS}, |
| 122 | /* shutdown_ack */ {sSA, sCL, sCW, sCE, sES, sSA, sSA, sSA, sSA, sHA}, |
| 123 | /* error */ {sCL, sCL, sCW, sCE, sES, sSS, sSR, sSA, sCL, sHA},/* Can't have Stale cookie*/ |
| 124 | /* cookie_echo */ {sCL, sCL, sCE, sCE, sES, sSS, sSR, sSA, sCL, sHA},/* 5.2.4 - Big TODO */ |
| 125 | /* cookie_ack */ {sCL, sCL, sCW, sCE, sES, sSS, sSR, sSA, sCL, sHA},/* Can't come in orig dir */ |
| 126 | /* shutdown_comp*/ {sCL, sCL, sCW, sCE, sES, sSS, sSR, sCL, sCL, sHA}, |
| 127 | /* heartbeat */ {sHS, sCL, sCW, sCE, sES, sSS, sSR, sSA, sHS, sHA}, |
| 128 | /* heartbeat_ack*/ {sCL, sCL, sCW, sCE, sES, sSS, sSR, sSA, sHS, sHA} |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 129 | }, |
| 130 | { |
| 131 | /* REPLY */ |
Michal Kubeček | d7ee351 | 2015-07-17 16:17:56 +0200 | [diff] [blame] | 132 | /* sNO, sCL, sCW, sCE, sES, sSS, sSR, sSA, sHS, sHA */ |
| 133 | /* init */ {sIV, sCL, sCW, sCE, sES, sSS, sSR, sSA, sIV, sHA},/* INIT in sCL Big TODO */ |
| 134 | /* init_ack */ {sIV, sCL, sCW, sCE, sES, sSS, sSR, sSA, sIV, sHA}, |
| 135 | /* abort */ {sIV, sCL, sCL, sCL, sCL, sCL, sCL, sCL, sIV, sCL}, |
| 136 | /* shutdown */ {sIV, sCL, sCW, sCE, sSR, sSS, sSR, sSA, sIV, sSR}, |
| 137 | /* shutdown_ack */ {sIV, sCL, sCW, sCE, sES, sSA, sSA, sSA, sIV, sHA}, |
| 138 | /* error */ {sIV, sCL, sCW, sCL, sES, sSS, sSR, sSA, sIV, sHA}, |
| 139 | /* cookie_echo */ {sIV, sCL, sCW, sCE, sES, sSS, sSR, sSA, sIV, sHA},/* Can't come in reply dir */ |
| 140 | /* cookie_ack */ {sIV, sCL, sCW, sES, sES, sSS, sSR, sSA, sIV, sHA}, |
| 141 | /* shutdown_comp*/ {sIV, sCL, sCW, sCE, sES, sSS, sSR, sCL, sIV, sHA}, |
| 142 | /* heartbeat */ {sIV, sCL, sCW, sCE, sES, sSS, sSR, sSA, sHS, sHA}, |
| 143 | /* heartbeat_ack*/ {sIV, sCL, sCW, sCE, sES, sSS, sSR, sSA, sHA, sHA} |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 144 | } |
| 145 | }; |
| 146 | |
Gao feng | 49d485a3 | 2012-05-28 21:04:18 +0000 | [diff] [blame] | 147 | static int sctp_net_id __read_mostly; |
| 148 | struct sctp_net { |
| 149 | struct nf_proto_net pn; |
| 150 | unsigned int timeouts[SCTP_CONNTRACK_MAX]; |
| 151 | }; |
| 152 | |
| 153 | static inline struct sctp_net *sctp_pernet(struct net *net) |
| 154 | { |
| 155 | return net_generic(net, sctp_net_id); |
| 156 | } |
| 157 | |
Jan Engelhardt | 09f263c | 2008-04-14 11:15:53 +0200 | [diff] [blame] | 158 | static bool sctp_pkt_to_tuple(const struct sk_buff *skb, unsigned int dataoff, |
Eric W. Biederman | a31f1ad | 2015-09-18 14:33:04 -0500 | [diff] [blame] | 159 | struct net *net, struct nf_conntrack_tuple *tuple) |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 160 | { |
Jan Engelhardt | 12c33aa | 2008-04-14 11:15:54 +0200 | [diff] [blame] | 161 | const struct sctphdr *hp; |
| 162 | struct sctphdr _hdr; |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 163 | |
Gao Feng | e5e693a | 2016-07-23 19:21:47 +0800 | [diff] [blame] | 164 | /* Actually only need first 4 bytes to get ports. */ |
| 165 | hp = skb_header_pointer(skb, dataoff, 4, &_hdr); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 166 | if (hp == NULL) |
Jan Engelhardt | 09f263c | 2008-04-14 11:15:53 +0200 | [diff] [blame] | 167 | return false; |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 168 | |
| 169 | tuple->src.u.sctp.port = hp->source; |
| 170 | tuple->dst.u.sctp.port = hp->dest; |
Jan Engelhardt | 09f263c | 2008-04-14 11:15:53 +0200 | [diff] [blame] | 171 | return true; |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 172 | } |
| 173 | |
Jan Engelhardt | 09f263c | 2008-04-14 11:15:53 +0200 | [diff] [blame] | 174 | static bool sctp_invert_tuple(struct nf_conntrack_tuple *tuple, |
| 175 | const struct nf_conntrack_tuple *orig) |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 176 | { |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 177 | tuple->src.u.sctp.port = orig->dst.u.sctp.port; |
| 178 | tuple->dst.u.sctp.port = orig->src.u.sctp.port; |
Jan Engelhardt | 09f263c | 2008-04-14 11:15:53 +0200 | [diff] [blame] | 179 | return true; |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | /* Print out the per-protocol part of the tuple. */ |
Joe Perches | 824f1fb | 2014-09-29 16:08:22 -0700 | [diff] [blame] | 183 | static void sctp_print_tuple(struct seq_file *s, |
| 184 | const struct nf_conntrack_tuple *tuple) |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 185 | { |
Joe Perches | 824f1fb | 2014-09-29 16:08:22 -0700 | [diff] [blame] | 186 | seq_printf(s, "sport=%hu dport=%hu ", |
| 187 | ntohs(tuple->src.u.sctp.port), |
| 188 | ntohs(tuple->dst.u.sctp.port)); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | /* Print out the private part of the conntrack. */ |
Steven Rostedt (Red Hat) | 37246a5 | 2014-10-27 16:02:47 -0400 | [diff] [blame] | 192 | static void sctp_print_conntrack(struct seq_file *s, struct nf_conn *ct) |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 193 | { |
Florian Westphal | a163f2cb | 2016-04-11 21:14:29 +0200 | [diff] [blame] | 194 | seq_printf(s, "%s ", sctp_conntrack_names[ct->proto.sctp.state]); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 195 | } |
| 196 | |
| 197 | #define for_each_sctp_chunk(skb, sch, _sch, offset, dataoff, count) \ |
Jan Engelhardt | e79ec50 | 2007-12-17 22:44:06 -0800 | [diff] [blame] | 198 | for ((offset) = (dataoff) + sizeof(sctp_sctphdr_t), (count) = 0; \ |
| 199 | (offset) < (skb)->len && \ |
| 200 | ((sch) = skb_header_pointer((skb), (offset), sizeof(_sch), &(_sch))); \ |
| 201 | (offset) += (ntohs((sch)->length) + 3) & ~3, (count)++) |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 202 | |
| 203 | /* Some validity checks to make sure the chunks are fine */ |
Patrick McHardy | 112f35c | 2008-01-14 23:46:20 -0800 | [diff] [blame] | 204 | static int do_basic_checks(struct nf_conn *ct, |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 205 | const struct sk_buff *skb, |
| 206 | unsigned int dataoff, |
Patrick McHardy | 35c6d3c | 2008-01-14 23:46:05 -0800 | [diff] [blame] | 207 | unsigned long *map) |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 208 | { |
| 209 | u_int32_t offset, count; |
| 210 | sctp_chunkhdr_t _sch, *sch; |
| 211 | int flag; |
| 212 | |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 213 | flag = 0; |
| 214 | |
| 215 | for_each_sctp_chunk (skb, sch, _sch, offset, dataoff, count) { |
Patrick McHardy | 0d53778 | 2007-07-07 22:39:38 -0700 | [diff] [blame] | 216 | pr_debug("Chunk Num: %d Type: %d\n", count, sch->type); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 217 | |
Patrick McHardy | 5447d47 | 2008-01-14 23:45:48 -0800 | [diff] [blame] | 218 | if (sch->type == SCTP_CID_INIT || |
| 219 | sch->type == SCTP_CID_INIT_ACK || |
| 220 | sch->type == SCTP_CID_SHUTDOWN_COMPLETE) |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 221 | flag = 1; |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 222 | |
Patrick McHardy | e17df68 | 2006-05-02 23:23:07 +0200 | [diff] [blame] | 223 | /* |
| 224 | * Cookie Ack/Echo chunks not the first OR |
| 225 | * Init / Init Ack / Shutdown compl chunks not the only chunks |
| 226 | * OR zero-length. |
| 227 | */ |
Patrick McHardy | 5447d47 | 2008-01-14 23:45:48 -0800 | [diff] [blame] | 228 | if (((sch->type == SCTP_CID_COOKIE_ACK || |
| 229 | sch->type == SCTP_CID_COOKIE_ECHO || |
| 230 | flag) && |
| 231 | count != 0) || !sch->length) { |
Patrick McHardy | 0d53778 | 2007-07-07 22:39:38 -0700 | [diff] [blame] | 232 | pr_debug("Basic checks failed\n"); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 233 | return 1; |
| 234 | } |
| 235 | |
Patrick McHardy | 5447d47 | 2008-01-14 23:45:48 -0800 | [diff] [blame] | 236 | if (map) |
Patrick McHardy | 35c6d3c | 2008-01-14 23:46:05 -0800 | [diff] [blame] | 237 | set_bit(sch->type, map); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 238 | } |
| 239 | |
Patrick McHardy | 0d53778 | 2007-07-07 22:39:38 -0700 | [diff] [blame] | 240 | pr_debug("Basic checks passed\n"); |
Patrick McHardy | dd7271f | 2006-06-29 21:40:23 -0700 | [diff] [blame] | 241 | return count == 0; |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 242 | } |
| 243 | |
Patrick McHardy | efe9f68 | 2008-01-14 23:47:09 -0800 | [diff] [blame] | 244 | static int sctp_new_state(enum ip_conntrack_dir dir, |
| 245 | enum sctp_conntrack cur_state, |
| 246 | int chunk_type) |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 247 | { |
| 248 | int i; |
| 249 | |
Patrick McHardy | 0d53778 | 2007-07-07 22:39:38 -0700 | [diff] [blame] | 250 | pr_debug("Chunk type: %d\n", chunk_type); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 251 | |
| 252 | switch (chunk_type) { |
Patrick McHardy | 5447d47 | 2008-01-14 23:45:48 -0800 | [diff] [blame] | 253 | case SCTP_CID_INIT: |
| 254 | pr_debug("SCTP_CID_INIT\n"); |
| 255 | i = 0; |
| 256 | break; |
| 257 | case SCTP_CID_INIT_ACK: |
| 258 | pr_debug("SCTP_CID_INIT_ACK\n"); |
| 259 | i = 1; |
| 260 | break; |
| 261 | case SCTP_CID_ABORT: |
| 262 | pr_debug("SCTP_CID_ABORT\n"); |
| 263 | i = 2; |
| 264 | break; |
| 265 | case SCTP_CID_SHUTDOWN: |
| 266 | pr_debug("SCTP_CID_SHUTDOWN\n"); |
| 267 | i = 3; |
| 268 | break; |
| 269 | case SCTP_CID_SHUTDOWN_ACK: |
| 270 | pr_debug("SCTP_CID_SHUTDOWN_ACK\n"); |
| 271 | i = 4; |
| 272 | break; |
| 273 | case SCTP_CID_ERROR: |
| 274 | pr_debug("SCTP_CID_ERROR\n"); |
| 275 | i = 5; |
| 276 | break; |
| 277 | case SCTP_CID_COOKIE_ECHO: |
| 278 | pr_debug("SCTP_CID_COOKIE_ECHO\n"); |
| 279 | i = 6; |
| 280 | break; |
| 281 | case SCTP_CID_COOKIE_ACK: |
| 282 | pr_debug("SCTP_CID_COOKIE_ACK\n"); |
| 283 | i = 7; |
| 284 | break; |
| 285 | case SCTP_CID_SHUTDOWN_COMPLETE: |
| 286 | pr_debug("SCTP_CID_SHUTDOWN_COMPLETE\n"); |
| 287 | i = 8; |
| 288 | break; |
Michal Kubeček | d7ee351 | 2015-07-17 16:17:56 +0200 | [diff] [blame] | 289 | case SCTP_CID_HEARTBEAT: |
| 290 | pr_debug("SCTP_CID_HEARTBEAT"); |
| 291 | i = 9; |
| 292 | break; |
| 293 | case SCTP_CID_HEARTBEAT_ACK: |
| 294 | pr_debug("SCTP_CID_HEARTBEAT_ACK"); |
| 295 | i = 10; |
| 296 | break; |
Patrick McHardy | 5447d47 | 2008-01-14 23:45:48 -0800 | [diff] [blame] | 297 | default: |
Michal Kubeček | d7ee351 | 2015-07-17 16:17:56 +0200 | [diff] [blame] | 298 | /* Other chunks like DATA or SACK do not change the state */ |
Patrick McHardy | 5447d47 | 2008-01-14 23:45:48 -0800 | [diff] [blame] | 299 | pr_debug("Unknown chunk type, Will stay in %s\n", |
| 300 | sctp_conntrack_names[cur_state]); |
| 301 | return cur_state; |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 302 | } |
| 303 | |
Patrick McHardy | 0d53778 | 2007-07-07 22:39:38 -0700 | [diff] [blame] | 304 | pr_debug("dir: %d cur_state: %s chunk_type: %d new_state: %s\n", |
| 305 | dir, sctp_conntrack_names[cur_state], chunk_type, |
| 306 | sctp_conntrack_names[sctp_conntracks[dir][i][cur_state]]); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 307 | |
| 308 | return sctp_conntracks[dir][i][cur_state]; |
| 309 | } |
| 310 | |
Pablo Neira Ayuso | 2c8503f | 2012-02-28 18:23:31 +0100 | [diff] [blame] | 311 | static unsigned int *sctp_get_timeouts(struct net *net) |
| 312 | { |
Gao feng | 49d485a3 | 2012-05-28 21:04:18 +0000 | [diff] [blame] | 313 | return sctp_pernet(net)->timeouts; |
Pablo Neira Ayuso | 2c8503f | 2012-02-28 18:23:31 +0100 | [diff] [blame] | 314 | } |
| 315 | |
Patrick McHardy | b37e933 | 2008-01-14 23:46:52 -0800 | [diff] [blame] | 316 | /* Returns verdict for packet, or -NF_ACCEPT for invalid. */ |
Patrick McHardy | 112f35c | 2008-01-14 23:46:20 -0800 | [diff] [blame] | 317 | static int sctp_packet(struct nf_conn *ct, |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 318 | const struct sk_buff *skb, |
| 319 | unsigned int dataoff, |
| 320 | enum ip_conntrack_info ctinfo, |
Jan Engelhardt | 76108ce | 2008-10-08 11:35:00 +0200 | [diff] [blame] | 321 | u_int8_t pf, |
Pablo Neira Ayuso | 2c8503f | 2012-02-28 18:23:31 +0100 | [diff] [blame] | 322 | unsigned int hooknum, |
| 323 | unsigned int *timeouts) |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 324 | { |
Patrick McHardy | efe9f68 | 2008-01-14 23:47:09 -0800 | [diff] [blame] | 325 | enum sctp_conntrack new_state, old_state; |
Patrick McHardy | 8528819 | 2008-01-14 23:46:37 -0800 | [diff] [blame] | 326 | enum ip_conntrack_dir dir = CTINFO2DIR(ctinfo); |
Jan Engelhardt | 12c33aa | 2008-04-14 11:15:54 +0200 | [diff] [blame] | 327 | const struct sctphdr *sh; |
| 328 | struct sctphdr _sctph; |
| 329 | const struct sctp_chunkhdr *sch; |
| 330 | struct sctp_chunkhdr _sch; |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 331 | u_int32_t offset, count; |
Patrick McHardy | 35c6d3c | 2008-01-14 23:46:05 -0800 | [diff] [blame] | 332 | unsigned long map[256 / sizeof(unsigned long)] = { 0 }; |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 333 | |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 334 | sh = skb_header_pointer(skb, dataoff, sizeof(_sctph), &_sctph); |
| 335 | if (sh == NULL) |
Patrick McHardy | b37e933 | 2008-01-14 23:46:52 -0800 | [diff] [blame] | 336 | goto out; |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 337 | |
Patrick McHardy | 112f35c | 2008-01-14 23:46:20 -0800 | [diff] [blame] | 338 | if (do_basic_checks(ct, skb, dataoff, map) != 0) |
Patrick McHardy | b37e933 | 2008-01-14 23:46:52 -0800 | [diff] [blame] | 339 | goto out; |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 340 | |
| 341 | /* Check the verification tag (Sec 8.5) */ |
Patrick McHardy | 35c6d3c | 2008-01-14 23:46:05 -0800 | [diff] [blame] | 342 | if (!test_bit(SCTP_CID_INIT, map) && |
| 343 | !test_bit(SCTP_CID_SHUTDOWN_COMPLETE, map) && |
| 344 | !test_bit(SCTP_CID_COOKIE_ECHO, map) && |
| 345 | !test_bit(SCTP_CID_ABORT, map) && |
| 346 | !test_bit(SCTP_CID_SHUTDOWN_ACK, map) && |
Michal Kubeček | d7ee351 | 2015-07-17 16:17:56 +0200 | [diff] [blame] | 347 | !test_bit(SCTP_CID_HEARTBEAT, map) && |
| 348 | !test_bit(SCTP_CID_HEARTBEAT_ACK, map) && |
Patrick McHardy | 8528819 | 2008-01-14 23:46:37 -0800 | [diff] [blame] | 349 | sh->vtag != ct->proto.sctp.vtag[dir]) { |
Patrick McHardy | 0d53778 | 2007-07-07 22:39:38 -0700 | [diff] [blame] | 350 | pr_debug("Verification tag check failed\n"); |
Patrick McHardy | b37e933 | 2008-01-14 23:46:52 -0800 | [diff] [blame] | 351 | goto out; |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 352 | } |
| 353 | |
Patrick McHardy | 328bd89 | 2008-11-24 13:44:55 +0100 | [diff] [blame] | 354 | old_state = new_state = SCTP_CONNTRACK_NONE; |
Patrick McHardy | 440f0d5 | 2009-06-10 14:32:47 +0200 | [diff] [blame] | 355 | spin_lock_bh(&ct->lock); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 356 | for_each_sctp_chunk (skb, sch, _sch, offset, dataoff, count) { |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 357 | /* Special cases of Verification tag check (Sec 8.5.1) */ |
| 358 | if (sch->type == SCTP_CID_INIT) { |
| 359 | /* Sec 8.5.1 (A) */ |
Patrick McHardy | b37e933 | 2008-01-14 23:46:52 -0800 | [diff] [blame] | 360 | if (sh->vtag != 0) |
| 361 | goto out_unlock; |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 362 | } else if (sch->type == SCTP_CID_ABORT) { |
| 363 | /* Sec 8.5.1 (B) */ |
Patrick McHardy | 8528819 | 2008-01-14 23:46:37 -0800 | [diff] [blame] | 364 | if (sh->vtag != ct->proto.sctp.vtag[dir] && |
Patrick McHardy | b37e933 | 2008-01-14 23:46:52 -0800 | [diff] [blame] | 365 | sh->vtag != ct->proto.sctp.vtag[!dir]) |
| 366 | goto out_unlock; |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 367 | } else if (sch->type == SCTP_CID_SHUTDOWN_COMPLETE) { |
| 368 | /* Sec 8.5.1 (C) */ |
Patrick McHardy | 8528819 | 2008-01-14 23:46:37 -0800 | [diff] [blame] | 369 | if (sh->vtag != ct->proto.sctp.vtag[dir] && |
| 370 | sh->vtag != ct->proto.sctp.vtag[!dir] && |
Patrick McHardy | 9b1c2cf | 2008-01-14 23:48:02 -0800 | [diff] [blame] | 371 | sch->flags & SCTP_CHUNK_FLAG_T) |
Patrick McHardy | b37e933 | 2008-01-14 23:46:52 -0800 | [diff] [blame] | 372 | goto out_unlock; |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 373 | } else if (sch->type == SCTP_CID_COOKIE_ECHO) { |
| 374 | /* Sec 8.5.1 (D) */ |
Patrick McHardy | b37e933 | 2008-01-14 23:46:52 -0800 | [diff] [blame] | 375 | if (sh->vtag != ct->proto.sctp.vtag[dir]) |
| 376 | goto out_unlock; |
Michal Kubeček | d7ee351 | 2015-07-17 16:17:56 +0200 | [diff] [blame] | 377 | } else if (sch->type == SCTP_CID_HEARTBEAT || |
| 378 | sch->type == SCTP_CID_HEARTBEAT_ACK) { |
| 379 | if (ct->proto.sctp.vtag[dir] == 0) { |
| 380 | pr_debug("Setting vtag %x for dir %d\n", |
| 381 | sh->vtag, dir); |
| 382 | ct->proto.sctp.vtag[dir] = sh->vtag; |
| 383 | } else if (sh->vtag != ct->proto.sctp.vtag[dir]) { |
| 384 | pr_debug("Verification tag check failed\n"); |
| 385 | goto out_unlock; |
| 386 | } |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 387 | } |
| 388 | |
Patrick McHardy | efe9f68 | 2008-01-14 23:47:09 -0800 | [diff] [blame] | 389 | old_state = ct->proto.sctp.state; |
| 390 | new_state = sctp_new_state(dir, old_state, sch->type); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 391 | |
| 392 | /* Invalid */ |
Patrick McHardy | efe9f68 | 2008-01-14 23:47:09 -0800 | [diff] [blame] | 393 | if (new_state == SCTP_CONNTRACK_MAX) { |
Patrick McHardy | 0d53778 | 2007-07-07 22:39:38 -0700 | [diff] [blame] | 394 | pr_debug("nf_conntrack_sctp: Invalid dir=%i ctype=%u " |
| 395 | "conntrack=%u\n", |
Patrick McHardy | efe9f68 | 2008-01-14 23:47:09 -0800 | [diff] [blame] | 396 | dir, sch->type, old_state); |
Patrick McHardy | b37e933 | 2008-01-14 23:46:52 -0800 | [diff] [blame] | 397 | goto out_unlock; |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 398 | } |
| 399 | |
| 400 | /* 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] | 401 | if (sch->type == SCTP_CID_INIT || |
| 402 | sch->type == SCTP_CID_INIT_ACK) { |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 403 | sctp_inithdr_t _inithdr, *ih; |
| 404 | |
| 405 | ih = skb_header_pointer(skb, offset + sizeof(sctp_chunkhdr_t), |
YOSHIFUJI Hideaki | 601e68e | 2007-02-12 11:15:49 -0800 | [diff] [blame] | 406 | sizeof(_inithdr), &_inithdr); |
Patrick McHardy | b37e933 | 2008-01-14 23:46:52 -0800 | [diff] [blame] | 407 | if (ih == NULL) |
| 408 | goto out_unlock; |
Patrick McHardy | 0d53778 | 2007-07-07 22:39:38 -0700 | [diff] [blame] | 409 | pr_debug("Setting vtag %x for dir %d\n", |
Patrick McHardy | 8528819 | 2008-01-14 23:46:37 -0800 | [diff] [blame] | 410 | ih->init_tag, !dir); |
| 411 | ct->proto.sctp.vtag[!dir] = ih->init_tag; |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 412 | } |
| 413 | |
Patrick McHardy | efe9f68 | 2008-01-14 23:47:09 -0800 | [diff] [blame] | 414 | ct->proto.sctp.state = new_state; |
| 415 | if (old_state != new_state) |
Alexey Dobriyan | a71996f | 2008-10-08 11:35:07 +0200 | [diff] [blame] | 416 | nf_conntrack_event_cache(IPCT_PROTOINFO, ct); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 417 | } |
Patrick McHardy | 440f0d5 | 2009-06-10 14:32:47 +0200 | [diff] [blame] | 418 | spin_unlock_bh(&ct->lock); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 419 | |
Pablo Neira Ayuso | 2c8503f | 2012-02-28 18:23:31 +0100 | [diff] [blame] | 420 | nf_ct_refresh_acct(ct, ctinfo, skb, timeouts[new_state]); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 421 | |
Patrick McHardy | efe9f68 | 2008-01-14 23:47:09 -0800 | [diff] [blame] | 422 | if (old_state == SCTP_CONNTRACK_COOKIE_ECHOED && |
Patrick McHardy | 8528819 | 2008-01-14 23:46:37 -0800 | [diff] [blame] | 423 | dir == IP_CT_DIR_REPLY && |
Patrick McHardy | efe9f68 | 2008-01-14 23:47:09 -0800 | [diff] [blame] | 424 | new_state == SCTP_CONNTRACK_ESTABLISHED) { |
Patrick McHardy | 0d53778 | 2007-07-07 22:39:38 -0700 | [diff] [blame] | 425 | pr_debug("Setting assured bit\n"); |
Patrick McHardy | 112f35c | 2008-01-14 23:46:20 -0800 | [diff] [blame] | 426 | set_bit(IPS_ASSURED_BIT, &ct->status); |
Patrick McHardy | 858b3133 | 2010-02-03 13:48:53 +0100 | [diff] [blame] | 427 | nf_conntrack_event_cache(IPCT_ASSURED, ct); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 428 | } |
| 429 | |
| 430 | return NF_ACCEPT; |
Patrick McHardy | b37e933 | 2008-01-14 23:46:52 -0800 | [diff] [blame] | 431 | |
| 432 | out_unlock: |
Patrick McHardy | 440f0d5 | 2009-06-10 14:32:47 +0200 | [diff] [blame] | 433 | spin_unlock_bh(&ct->lock); |
Patrick McHardy | b37e933 | 2008-01-14 23:46:52 -0800 | [diff] [blame] | 434 | out: |
| 435 | return -NF_ACCEPT; |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 436 | } |
| 437 | |
| 438 | /* Called when a new connection for this protocol found. */ |
Jan Engelhardt | 09f263c | 2008-04-14 11:15:53 +0200 | [diff] [blame] | 439 | static bool sctp_new(struct nf_conn *ct, const struct sk_buff *skb, |
Pablo Neira Ayuso | 2c8503f | 2012-02-28 18:23:31 +0100 | [diff] [blame] | 440 | unsigned int dataoff, unsigned int *timeouts) |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 441 | { |
Patrick McHardy | efe9f68 | 2008-01-14 23:47:09 -0800 | [diff] [blame] | 442 | enum sctp_conntrack new_state; |
Jan Engelhardt | 12c33aa | 2008-04-14 11:15:54 +0200 | [diff] [blame] | 443 | const struct sctphdr *sh; |
| 444 | struct sctphdr _sctph; |
| 445 | const struct sctp_chunkhdr *sch; |
| 446 | struct sctp_chunkhdr _sch; |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 447 | u_int32_t offset, count; |
Patrick McHardy | 35c6d3c | 2008-01-14 23:46:05 -0800 | [diff] [blame] | 448 | unsigned long map[256 / sizeof(unsigned long)] = { 0 }; |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 449 | |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 450 | sh = skb_header_pointer(skb, dataoff, sizeof(_sctph), &_sctph); |
| 451 | if (sh == NULL) |
Jan Engelhardt | 09f263c | 2008-04-14 11:15:53 +0200 | [diff] [blame] | 452 | return false; |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 453 | |
Patrick McHardy | 112f35c | 2008-01-14 23:46:20 -0800 | [diff] [blame] | 454 | if (do_basic_checks(ct, skb, dataoff, map) != 0) |
Jan Engelhardt | 09f263c | 2008-04-14 11:15:53 +0200 | [diff] [blame] | 455 | return false; |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 456 | |
| 457 | /* 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] | 458 | if (test_bit(SCTP_CID_ABORT, map) || |
| 459 | test_bit(SCTP_CID_SHUTDOWN_COMPLETE, map) || |
| 460 | test_bit(SCTP_CID_COOKIE_ACK, map)) |
Jan Engelhardt | 09f263c | 2008-04-14 11:15:53 +0200 | [diff] [blame] | 461 | return false; |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 462 | |
Changli Gao | e5fc9e7 | 2010-11-12 17:33:17 +0100 | [diff] [blame] | 463 | memset(&ct->proto.sctp, 0, sizeof(ct->proto.sctp)); |
Patrick McHardy | efe9f68 | 2008-01-14 23:47:09 -0800 | [diff] [blame] | 464 | new_state = SCTP_CONNTRACK_MAX; |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 465 | for_each_sctp_chunk (skb, sch, _sch, offset, dataoff, count) { |
| 466 | /* Don't need lock here: this conntrack not in circulation yet */ |
Patrick McHardy | efe9f68 | 2008-01-14 23:47:09 -0800 | [diff] [blame] | 467 | new_state = sctp_new_state(IP_CT_DIR_ORIGINAL, |
| 468 | SCTP_CONNTRACK_NONE, sch->type); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 469 | |
| 470 | /* Invalid: delete conntrack */ |
Patrick McHardy | efe9f68 | 2008-01-14 23:47:09 -0800 | [diff] [blame] | 471 | if (new_state == SCTP_CONNTRACK_NONE || |
| 472 | new_state == SCTP_CONNTRACK_MAX) { |
Patrick McHardy | 0d53778 | 2007-07-07 22:39:38 -0700 | [diff] [blame] | 473 | pr_debug("nf_conntrack_sctp: invalid new deleting.\n"); |
Jan Engelhardt | 09f263c | 2008-04-14 11:15:53 +0200 | [diff] [blame] | 474 | return false; |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 475 | } |
| 476 | |
| 477 | /* Copy the vtag into the state info */ |
| 478 | if (sch->type == SCTP_CID_INIT) { |
| 479 | if (sh->vtag == 0) { |
| 480 | sctp_inithdr_t _inithdr, *ih; |
| 481 | |
| 482 | ih = skb_header_pointer(skb, offset + sizeof(sctp_chunkhdr_t), |
YOSHIFUJI Hideaki | 601e68e | 2007-02-12 11:15:49 -0800 | [diff] [blame] | 483 | sizeof(_inithdr), &_inithdr); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 484 | if (ih == NULL) |
Jan Engelhardt | 09f263c | 2008-04-14 11:15:53 +0200 | [diff] [blame] | 485 | return false; |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 486 | |
Patrick McHardy | 0d53778 | 2007-07-07 22:39:38 -0700 | [diff] [blame] | 487 | pr_debug("Setting vtag %x for new conn\n", |
| 488 | ih->init_tag); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 489 | |
Patrick McHardy | 112f35c | 2008-01-14 23:46:20 -0800 | [diff] [blame] | 490 | ct->proto.sctp.vtag[IP_CT_DIR_REPLY] = |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 491 | ih->init_tag; |
| 492 | } else { |
| 493 | /* Sec 8.5.1 (A) */ |
Jan Engelhardt | 09f263c | 2008-04-14 11:15:53 +0200 | [diff] [blame] | 494 | return false; |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 495 | } |
Michal Kubeček | d7ee351 | 2015-07-17 16:17:56 +0200 | [diff] [blame] | 496 | } else if (sch->type == SCTP_CID_HEARTBEAT) { |
| 497 | pr_debug("Setting vtag %x for secondary conntrack\n", |
| 498 | sh->vtag); |
| 499 | ct->proto.sctp.vtag[IP_CT_DIR_ORIGINAL] = sh->vtag; |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 500 | } |
| 501 | /* If it is a shutdown ack OOTB packet, we expect a return |
| 502 | shutdown complete, otherwise an ABORT Sec 8.4 (5) and (8) */ |
| 503 | else { |
Patrick McHardy | 0d53778 | 2007-07-07 22:39:38 -0700 | [diff] [blame] | 504 | pr_debug("Setting vtag %x for new conn OOTB\n", |
| 505 | sh->vtag); |
Patrick McHardy | 112f35c | 2008-01-14 23:46:20 -0800 | [diff] [blame] | 506 | ct->proto.sctp.vtag[IP_CT_DIR_REPLY] = sh->vtag; |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 507 | } |
| 508 | |
Patrick McHardy | efe9f68 | 2008-01-14 23:47:09 -0800 | [diff] [blame] | 509 | ct->proto.sctp.state = new_state; |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 510 | } |
| 511 | |
Jan Engelhardt | 09f263c | 2008-04-14 11:15:53 +0200 | [diff] [blame] | 512 | return true; |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 513 | } |
| 514 | |
Igor Maravić | c0cd115 | 2011-12-12 02:58:24 +0000 | [diff] [blame] | 515 | #if IS_ENABLED(CONFIG_NF_CT_NETLINK) |
Pablo Neira Ayuso | a258860 | 2008-06-09 15:56:39 -0700 | [diff] [blame] | 516 | |
| 517 | #include <linux/netfilter/nfnetlink.h> |
| 518 | #include <linux/netfilter/nfnetlink_conntrack.h> |
| 519 | |
| 520 | static int sctp_to_nlattr(struct sk_buff *skb, struct nlattr *nla, |
Patrick McHardy | 440f0d5 | 2009-06-10 14:32:47 +0200 | [diff] [blame] | 521 | struct nf_conn *ct) |
Pablo Neira Ayuso | a258860 | 2008-06-09 15:56:39 -0700 | [diff] [blame] | 522 | { |
| 523 | struct nlattr *nest_parms; |
| 524 | |
Patrick McHardy | 440f0d5 | 2009-06-10 14:32:47 +0200 | [diff] [blame] | 525 | spin_lock_bh(&ct->lock); |
Pablo Neira Ayuso | a258860 | 2008-06-09 15:56:39 -0700 | [diff] [blame] | 526 | nest_parms = nla_nest_start(skb, CTA_PROTOINFO_SCTP | NLA_F_NESTED); |
| 527 | if (!nest_parms) |
| 528 | goto nla_put_failure; |
| 529 | |
David S. Miller | 5e8d1eb | 2012-04-01 18:51:07 -0400 | [diff] [blame] | 530 | if (nla_put_u8(skb, CTA_PROTOINFO_SCTP_STATE, ct->proto.sctp.state) || |
| 531 | nla_put_be32(skb, CTA_PROTOINFO_SCTP_VTAG_ORIGINAL, |
| 532 | ct->proto.sctp.vtag[IP_CT_DIR_ORIGINAL]) || |
| 533 | nla_put_be32(skb, CTA_PROTOINFO_SCTP_VTAG_REPLY, |
| 534 | ct->proto.sctp.vtag[IP_CT_DIR_REPLY])) |
| 535 | goto nla_put_failure; |
Pablo Neira Ayuso | a258860 | 2008-06-09 15:56:39 -0700 | [diff] [blame] | 536 | |
Patrick McHardy | 440f0d5 | 2009-06-10 14:32:47 +0200 | [diff] [blame] | 537 | spin_unlock_bh(&ct->lock); |
Pablo Neira Ayuso | a258860 | 2008-06-09 15:56:39 -0700 | [diff] [blame] | 538 | |
| 539 | nla_nest_end(skb, nest_parms); |
| 540 | |
| 541 | return 0; |
| 542 | |
| 543 | nla_put_failure: |
Patrick McHardy | 440f0d5 | 2009-06-10 14:32:47 +0200 | [diff] [blame] | 544 | spin_unlock_bh(&ct->lock); |
Pablo Neira Ayuso | a258860 | 2008-06-09 15:56:39 -0700 | [diff] [blame] | 545 | return -1; |
| 546 | } |
| 547 | |
| 548 | static const struct nla_policy sctp_nla_policy[CTA_PROTOINFO_SCTP_MAX+1] = { |
| 549 | [CTA_PROTOINFO_SCTP_STATE] = { .type = NLA_U8 }, |
| 550 | [CTA_PROTOINFO_SCTP_VTAG_ORIGINAL] = { .type = NLA_U32 }, |
| 551 | [CTA_PROTOINFO_SCTP_VTAG_REPLY] = { .type = NLA_U32 }, |
| 552 | }; |
| 553 | |
| 554 | static int nlattr_to_sctp(struct nlattr *cda[], struct nf_conn *ct) |
| 555 | { |
| 556 | struct nlattr *attr = cda[CTA_PROTOINFO_SCTP]; |
| 557 | struct nlattr *tb[CTA_PROTOINFO_SCTP_MAX+1]; |
| 558 | int err; |
| 559 | |
| 560 | /* updates may not contain the internal protocol info, skip parsing */ |
| 561 | if (!attr) |
| 562 | return 0; |
| 563 | |
| 564 | err = nla_parse_nested(tb, |
| 565 | CTA_PROTOINFO_SCTP_MAX, |
| 566 | attr, |
| 567 | sctp_nla_policy); |
| 568 | if (err < 0) |
| 569 | return err; |
| 570 | |
| 571 | if (!tb[CTA_PROTOINFO_SCTP_STATE] || |
| 572 | !tb[CTA_PROTOINFO_SCTP_VTAG_ORIGINAL] || |
| 573 | !tb[CTA_PROTOINFO_SCTP_VTAG_REPLY]) |
| 574 | return -EINVAL; |
| 575 | |
Patrick McHardy | 440f0d5 | 2009-06-10 14:32:47 +0200 | [diff] [blame] | 576 | spin_lock_bh(&ct->lock); |
Pablo Neira Ayuso | a258860 | 2008-06-09 15:56:39 -0700 | [diff] [blame] | 577 | ct->proto.sctp.state = nla_get_u8(tb[CTA_PROTOINFO_SCTP_STATE]); |
| 578 | ct->proto.sctp.vtag[IP_CT_DIR_ORIGINAL] = |
Patrick McHardy | 5547cd0 | 2008-07-21 10:03:49 -0700 | [diff] [blame] | 579 | nla_get_be32(tb[CTA_PROTOINFO_SCTP_VTAG_ORIGINAL]); |
Pablo Neira Ayuso | a258860 | 2008-06-09 15:56:39 -0700 | [diff] [blame] | 580 | ct->proto.sctp.vtag[IP_CT_DIR_REPLY] = |
Patrick McHardy | 5547cd0 | 2008-07-21 10:03:49 -0700 | [diff] [blame] | 581 | nla_get_be32(tb[CTA_PROTOINFO_SCTP_VTAG_REPLY]); |
Patrick McHardy | 440f0d5 | 2009-06-10 14:32:47 +0200 | [diff] [blame] | 582 | spin_unlock_bh(&ct->lock); |
Pablo Neira Ayuso | a258860 | 2008-06-09 15:56:39 -0700 | [diff] [blame] | 583 | |
| 584 | return 0; |
| 585 | } |
Holger Eitzenberger | a400c30 | 2009-03-25 21:53:39 +0100 | [diff] [blame] | 586 | |
| 587 | static int sctp_nlattr_size(void) |
| 588 | { |
| 589 | return nla_total_size(0) /* CTA_PROTOINFO_SCTP */ |
| 590 | + nla_policy_len(sctp_nla_policy, CTA_PROTOINFO_SCTP_MAX + 1); |
| 591 | } |
Pablo Neira Ayuso | a258860 | 2008-06-09 15:56:39 -0700 | [diff] [blame] | 592 | #endif |
| 593 | |
Pablo Neira Ayuso | 5097846 | 2012-02-28 19:13:48 +0100 | [diff] [blame] | 594 | #if IS_ENABLED(CONFIG_NF_CT_NETLINK_TIMEOUT) |
| 595 | |
| 596 | #include <linux/netfilter/nfnetlink.h> |
| 597 | #include <linux/netfilter/nfnetlink_cttimeout.h> |
| 598 | |
Gao feng | 8264deb | 2012-05-28 21:04:23 +0000 | [diff] [blame] | 599 | static int sctp_timeout_nlattr_to_obj(struct nlattr *tb[], |
| 600 | struct net *net, void *data) |
Pablo Neira Ayuso | 5097846 | 2012-02-28 19:13:48 +0100 | [diff] [blame] | 601 | { |
| 602 | unsigned int *timeouts = data; |
Gao feng | 8264deb | 2012-05-28 21:04:23 +0000 | [diff] [blame] | 603 | struct sctp_net *sn = sctp_pernet(net); |
Pablo Neira Ayuso | 5097846 | 2012-02-28 19:13:48 +0100 | [diff] [blame] | 604 | int i; |
| 605 | |
| 606 | /* set default SCTP timeouts. */ |
| 607 | for (i=0; i<SCTP_CONNTRACK_MAX; i++) |
Gao feng | 8264deb | 2012-05-28 21:04:23 +0000 | [diff] [blame] | 608 | timeouts[i] = sn->timeouts[i]; |
Pablo Neira Ayuso | 5097846 | 2012-02-28 19:13:48 +0100 | [diff] [blame] | 609 | |
| 610 | /* there's a 1:1 mapping between attributes and protocol states. */ |
| 611 | for (i=CTA_TIMEOUT_SCTP_UNSPEC+1; i<CTA_TIMEOUT_SCTP_MAX+1; i++) { |
| 612 | if (tb[i]) { |
| 613 | timeouts[i] = ntohl(nla_get_be32(tb[i])) * HZ; |
| 614 | } |
| 615 | } |
| 616 | return 0; |
| 617 | } |
| 618 | |
| 619 | static int |
| 620 | sctp_timeout_obj_to_nlattr(struct sk_buff *skb, const void *data) |
| 621 | { |
| 622 | const unsigned int *timeouts = data; |
| 623 | int i; |
| 624 | |
David S. Miller | 5e8d1eb | 2012-04-01 18:51:07 -0400 | [diff] [blame] | 625 | for (i=CTA_TIMEOUT_SCTP_UNSPEC+1; i<CTA_TIMEOUT_SCTP_MAX+1; i++) { |
| 626 | if (nla_put_be32(skb, i, htonl(timeouts[i] / HZ))) |
| 627 | goto nla_put_failure; |
| 628 | } |
Pablo Neira Ayuso | 5097846 | 2012-02-28 19:13:48 +0100 | [diff] [blame] | 629 | return 0; |
| 630 | |
| 631 | nla_put_failure: |
| 632 | return -ENOSPC; |
| 633 | } |
| 634 | |
| 635 | static const struct nla_policy |
| 636 | sctp_timeout_nla_policy[CTA_TIMEOUT_SCTP_MAX+1] = { |
| 637 | [CTA_TIMEOUT_SCTP_CLOSED] = { .type = NLA_U32 }, |
| 638 | [CTA_TIMEOUT_SCTP_COOKIE_WAIT] = { .type = NLA_U32 }, |
| 639 | [CTA_TIMEOUT_SCTP_COOKIE_ECHOED] = { .type = NLA_U32 }, |
| 640 | [CTA_TIMEOUT_SCTP_ESTABLISHED] = { .type = NLA_U32 }, |
| 641 | [CTA_TIMEOUT_SCTP_SHUTDOWN_SENT] = { .type = NLA_U32 }, |
| 642 | [CTA_TIMEOUT_SCTP_SHUTDOWN_RECD] = { .type = NLA_U32 }, |
| 643 | [CTA_TIMEOUT_SCTP_SHUTDOWN_ACK_SENT] = { .type = NLA_U32 }, |
Michal Kubeček | d7ee351 | 2015-07-17 16:17:56 +0200 | [diff] [blame] | 644 | [CTA_TIMEOUT_SCTP_HEARTBEAT_SENT] = { .type = NLA_U32 }, |
| 645 | [CTA_TIMEOUT_SCTP_HEARTBEAT_ACKED] = { .type = NLA_U32 }, |
Pablo Neira Ayuso | 5097846 | 2012-02-28 19:13:48 +0100 | [diff] [blame] | 646 | }; |
| 647 | #endif /* CONFIG_NF_CT_NETLINK_TIMEOUT */ |
| 648 | |
| 649 | |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 650 | #ifdef CONFIG_SYSCTL |
Patrick McHardy | 933a41e | 2006-11-29 02:35:18 +0100 | [diff] [blame] | 651 | static struct ctl_table sctp_sysctl_table[] = { |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 652 | { |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 653 | .procname = "nf_conntrack_sctp_timeout_closed", |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 654 | .maxlen = sizeof(unsigned int), |
| 655 | .mode = 0644, |
Alexey Dobriyan | 6d9f239 | 2008-11-03 18:21:05 -0800 | [diff] [blame] | 656 | .proc_handler = proc_dointvec_jiffies, |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 657 | }, |
| 658 | { |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 659 | .procname = "nf_conntrack_sctp_timeout_cookie_wait", |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 660 | .maxlen = sizeof(unsigned int), |
| 661 | .mode = 0644, |
Alexey Dobriyan | 6d9f239 | 2008-11-03 18:21:05 -0800 | [diff] [blame] | 662 | .proc_handler = proc_dointvec_jiffies, |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 663 | }, |
| 664 | { |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 665 | .procname = "nf_conntrack_sctp_timeout_cookie_echoed", |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 666 | .maxlen = sizeof(unsigned int), |
| 667 | .mode = 0644, |
Alexey Dobriyan | 6d9f239 | 2008-11-03 18:21:05 -0800 | [diff] [blame] | 668 | .proc_handler = proc_dointvec_jiffies, |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 669 | }, |
| 670 | { |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 671 | .procname = "nf_conntrack_sctp_timeout_established", |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 672 | .maxlen = sizeof(unsigned int), |
| 673 | .mode = 0644, |
Alexey Dobriyan | 6d9f239 | 2008-11-03 18:21:05 -0800 | [diff] [blame] | 674 | .proc_handler = proc_dointvec_jiffies, |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 675 | }, |
| 676 | { |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 677 | .procname = "nf_conntrack_sctp_timeout_shutdown_sent", |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 678 | .maxlen = sizeof(unsigned int), |
| 679 | .mode = 0644, |
Alexey Dobriyan | 6d9f239 | 2008-11-03 18:21:05 -0800 | [diff] [blame] | 680 | .proc_handler = proc_dointvec_jiffies, |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 681 | }, |
| 682 | { |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 683 | .procname = "nf_conntrack_sctp_timeout_shutdown_recd", |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 684 | .maxlen = sizeof(unsigned int), |
| 685 | .mode = 0644, |
Alexey Dobriyan | 6d9f239 | 2008-11-03 18:21:05 -0800 | [diff] [blame] | 686 | .proc_handler = proc_dointvec_jiffies, |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 687 | }, |
| 688 | { |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 689 | .procname = "nf_conntrack_sctp_timeout_shutdown_ack_sent", |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 690 | .maxlen = sizeof(unsigned int), |
| 691 | .mode = 0644, |
Alexey Dobriyan | 6d9f239 | 2008-11-03 18:21:05 -0800 | [diff] [blame] | 692 | .proc_handler = proc_dointvec_jiffies, |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 693 | }, |
Michal Kubeček | d7ee351 | 2015-07-17 16:17:56 +0200 | [diff] [blame] | 694 | { |
| 695 | .procname = "nf_conntrack_sctp_timeout_heartbeat_sent", |
| 696 | .maxlen = sizeof(unsigned int), |
| 697 | .mode = 0644, |
| 698 | .proc_handler = proc_dointvec_jiffies, |
| 699 | }, |
| 700 | { |
| 701 | .procname = "nf_conntrack_sctp_timeout_heartbeat_acked", |
| 702 | .maxlen = sizeof(unsigned int), |
| 703 | .mode = 0644, |
| 704 | .proc_handler = proc_dointvec_jiffies, |
| 705 | }, |
Eric W. Biederman | f8572d8 | 2009-11-05 13:32:03 -0800 | [diff] [blame] | 706 | { } |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 707 | }; |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 708 | #endif |
| 709 | |
Gao feng | f42c418 | 2012-06-21 04:36:46 +0000 | [diff] [blame] | 710 | static int sctp_kmemdup_sysctl_table(struct nf_proto_net *pn, |
| 711 | struct sctp_net *sn) |
Gao feng | 49d485a3 | 2012-05-28 21:04:18 +0000 | [diff] [blame] | 712 | { |
| 713 | #ifdef CONFIG_SYSCTL |
Gao feng | 49d485a3 | 2012-05-28 21:04:18 +0000 | [diff] [blame] | 714 | if (pn->ctl_table) |
| 715 | return 0; |
| 716 | |
| 717 | pn->ctl_table = kmemdup(sctp_sysctl_table, |
| 718 | sizeof(sctp_sysctl_table), |
| 719 | GFP_KERNEL); |
| 720 | if (!pn->ctl_table) |
| 721 | return -ENOMEM; |
| 722 | |
| 723 | pn->ctl_table[0].data = &sn->timeouts[SCTP_CONNTRACK_CLOSED]; |
| 724 | pn->ctl_table[1].data = &sn->timeouts[SCTP_CONNTRACK_COOKIE_WAIT]; |
| 725 | pn->ctl_table[2].data = &sn->timeouts[SCTP_CONNTRACK_COOKIE_ECHOED]; |
| 726 | pn->ctl_table[3].data = &sn->timeouts[SCTP_CONNTRACK_ESTABLISHED]; |
| 727 | pn->ctl_table[4].data = &sn->timeouts[SCTP_CONNTRACK_SHUTDOWN_SENT]; |
| 728 | pn->ctl_table[5].data = &sn->timeouts[SCTP_CONNTRACK_SHUTDOWN_RECD]; |
| 729 | pn->ctl_table[6].data = &sn->timeouts[SCTP_CONNTRACK_SHUTDOWN_ACK_SENT]; |
Michal Kubeček | d7ee351 | 2015-07-17 16:17:56 +0200 | [diff] [blame] | 730 | pn->ctl_table[7].data = &sn->timeouts[SCTP_CONNTRACK_HEARTBEAT_SENT]; |
| 731 | pn->ctl_table[8].data = &sn->timeouts[SCTP_CONNTRACK_HEARTBEAT_ACKED]; |
Gao feng | 49d485a3 | 2012-05-28 21:04:18 +0000 | [diff] [blame] | 732 | #endif |
| 733 | return 0; |
| 734 | } |
| 735 | |
Gao feng | f42c418 | 2012-06-21 04:36:46 +0000 | [diff] [blame] | 736 | static int sctp_init_net(struct net *net, u_int16_t proto) |
Gao feng | 49d485a3 | 2012-05-28 21:04:18 +0000 | [diff] [blame] | 737 | { |
Gao feng | 49d485a3 | 2012-05-28 21:04:18 +0000 | [diff] [blame] | 738 | struct sctp_net *sn = sctp_pernet(net); |
Gao feng | f42c418 | 2012-06-21 04:36:46 +0000 | [diff] [blame] | 739 | struct nf_proto_net *pn = &sn->pn; |
Gao feng | 49d485a3 | 2012-05-28 21:04:18 +0000 | [diff] [blame] | 740 | |
Gao feng | f42c418 | 2012-06-21 04:36:46 +0000 | [diff] [blame] | 741 | if (!pn->users) { |
| 742 | int i; |
Gao feng | 49d485a3 | 2012-05-28 21:04:18 +0000 | [diff] [blame] | 743 | |
Gao feng | f42c418 | 2012-06-21 04:36:46 +0000 | [diff] [blame] | 744 | for (i = 0; i < SCTP_CONNTRACK_MAX; i++) |
| 745 | sn->timeouts[i] = sctp_timeouts[i]; |
Gao feng | 49d485a3 | 2012-05-28 21:04:18 +0000 | [diff] [blame] | 746 | } |
Gao feng | f42c418 | 2012-06-21 04:36:46 +0000 | [diff] [blame] | 747 | |
Pablo Neira Ayuso | adf0516 | 2016-08-12 13:47:06 +0200 | [diff] [blame] | 748 | return sctp_kmemdup_sysctl_table(pn, sn); |
Gao feng | 49d485a3 | 2012-05-28 21:04:18 +0000 | [diff] [blame] | 749 | } |
| 750 | |
Patrick McHardy | 61075af | 2007-07-14 20:48:19 -0700 | [diff] [blame] | 751 | static struct nf_conntrack_l4proto nf_conntrack_l4proto_sctp4 __read_mostly = { |
Patrick McHardy | 933a41e | 2006-11-29 02:35:18 +0100 | [diff] [blame] | 752 | .l3proto = PF_INET, |
| 753 | .l4proto = IPPROTO_SCTP, |
| 754 | .name = "sctp", |
| 755 | .pkt_to_tuple = sctp_pkt_to_tuple, |
| 756 | .invert_tuple = sctp_invert_tuple, |
| 757 | .print_tuple = sctp_print_tuple, |
| 758 | .print_conntrack = sctp_print_conntrack, |
| 759 | .packet = sctp_packet, |
Pablo Neira Ayuso | 2c8503f | 2012-02-28 18:23:31 +0100 | [diff] [blame] | 760 | .get_timeouts = sctp_get_timeouts, |
Patrick McHardy | 933a41e | 2006-11-29 02:35:18 +0100 | [diff] [blame] | 761 | .new = sctp_new, |
| 762 | .me = THIS_MODULE, |
Igor Maravić | c0cd115 | 2011-12-12 02:58:24 +0000 | [diff] [blame] | 763 | #if IS_ENABLED(CONFIG_NF_CT_NETLINK) |
Pablo Neira Ayuso | a258860 | 2008-06-09 15:56:39 -0700 | [diff] [blame] | 764 | .to_nlattr = sctp_to_nlattr, |
Holger Eitzenberger | a400c30 | 2009-03-25 21:53:39 +0100 | [diff] [blame] | 765 | .nlattr_size = sctp_nlattr_size, |
Pablo Neira Ayuso | a258860 | 2008-06-09 15:56:39 -0700 | [diff] [blame] | 766 | .from_nlattr = nlattr_to_sctp, |
Pablo Neira Ayuso | c7212e9 | 2007-12-17 22:29:02 -0800 | [diff] [blame] | 767 | .tuple_to_nlattr = nf_ct_port_tuple_to_nlattr, |
Holger Eitzenberger | a400c30 | 2009-03-25 21:53:39 +0100 | [diff] [blame] | 768 | .nlattr_tuple_size = nf_ct_port_nlattr_tuple_size, |
Pablo Neira Ayuso | c7212e9 | 2007-12-17 22:29:02 -0800 | [diff] [blame] | 769 | .nlattr_to_tuple = nf_ct_port_nlattr_to_tuple, |
| 770 | .nla_policy = nf_ct_port_nla_policy, |
| 771 | #endif |
Pablo Neira Ayuso | 5097846 | 2012-02-28 19:13:48 +0100 | [diff] [blame] | 772 | #if IS_ENABLED(CONFIG_NF_CT_NETLINK_TIMEOUT) |
| 773 | .ctnl_timeout = { |
| 774 | .nlattr_to_obj = sctp_timeout_nlattr_to_obj, |
| 775 | .obj_to_nlattr = sctp_timeout_obj_to_nlattr, |
| 776 | .nlattr_max = CTA_TIMEOUT_SCTP_MAX, |
| 777 | .obj_size = sizeof(unsigned int) * SCTP_CONNTRACK_MAX, |
| 778 | .nla_policy = sctp_timeout_nla_policy, |
| 779 | }, |
| 780 | #endif /* CONFIG_NF_CT_NETLINK_TIMEOUT */ |
Gao feng | 49d485a3 | 2012-05-28 21:04:18 +0000 | [diff] [blame] | 781 | .net_id = &sctp_net_id, |
Gao feng | f42c418 | 2012-06-21 04:36:46 +0000 | [diff] [blame] | 782 | .init_net = sctp_init_net, |
Patrick McHardy | 933a41e | 2006-11-29 02:35:18 +0100 | [diff] [blame] | 783 | }; |
| 784 | |
Patrick McHardy | 61075af | 2007-07-14 20:48:19 -0700 | [diff] [blame] | 785 | static struct nf_conntrack_l4proto nf_conntrack_l4proto_sctp6 __read_mostly = { |
Patrick McHardy | 933a41e | 2006-11-29 02:35:18 +0100 | [diff] [blame] | 786 | .l3proto = PF_INET6, |
| 787 | .l4proto = IPPROTO_SCTP, |
| 788 | .name = "sctp", |
| 789 | .pkt_to_tuple = sctp_pkt_to_tuple, |
| 790 | .invert_tuple = sctp_invert_tuple, |
| 791 | .print_tuple = sctp_print_tuple, |
| 792 | .print_conntrack = sctp_print_conntrack, |
| 793 | .packet = sctp_packet, |
Pablo Neira Ayuso | 2c8503f | 2012-02-28 18:23:31 +0100 | [diff] [blame] | 794 | .get_timeouts = sctp_get_timeouts, |
Patrick McHardy | 933a41e | 2006-11-29 02:35:18 +0100 | [diff] [blame] | 795 | .new = sctp_new, |
| 796 | .me = THIS_MODULE, |
Igor Maravić | c0cd115 | 2011-12-12 02:58:24 +0000 | [diff] [blame] | 797 | #if IS_ENABLED(CONFIG_NF_CT_NETLINK) |
Pablo Neira Ayuso | a258860 | 2008-06-09 15:56:39 -0700 | [diff] [blame] | 798 | .to_nlattr = sctp_to_nlattr, |
Holger Eitzenberger | a400c30 | 2009-03-25 21:53:39 +0100 | [diff] [blame] | 799 | .nlattr_size = sctp_nlattr_size, |
Pablo Neira Ayuso | a258860 | 2008-06-09 15:56:39 -0700 | [diff] [blame] | 800 | .from_nlattr = nlattr_to_sctp, |
Pablo Neira Ayuso | c7212e9 | 2007-12-17 22:29:02 -0800 | [diff] [blame] | 801 | .tuple_to_nlattr = nf_ct_port_tuple_to_nlattr, |
Holger Eitzenberger | a400c30 | 2009-03-25 21:53:39 +0100 | [diff] [blame] | 802 | .nlattr_tuple_size = nf_ct_port_nlattr_tuple_size, |
Pablo Neira Ayuso | c7212e9 | 2007-12-17 22:29:02 -0800 | [diff] [blame] | 803 | .nlattr_to_tuple = nf_ct_port_nlattr_to_tuple, |
| 804 | .nla_policy = nf_ct_port_nla_policy, |
Pablo Neira Ayuso | 5097846 | 2012-02-28 19:13:48 +0100 | [diff] [blame] | 805 | #if IS_ENABLED(CONFIG_NF_CT_NETLINK_TIMEOUT) |
| 806 | .ctnl_timeout = { |
| 807 | .nlattr_to_obj = sctp_timeout_nlattr_to_obj, |
| 808 | .obj_to_nlattr = sctp_timeout_obj_to_nlattr, |
| 809 | .nlattr_max = CTA_TIMEOUT_SCTP_MAX, |
| 810 | .obj_size = sizeof(unsigned int) * SCTP_CONNTRACK_MAX, |
| 811 | .nla_policy = sctp_timeout_nla_policy, |
| 812 | }, |
| 813 | #endif /* CONFIG_NF_CT_NETLINK_TIMEOUT */ |
Pablo Neira Ayuso | c7212e9 | 2007-12-17 22:29:02 -0800 | [diff] [blame] | 814 | #endif |
Gao feng | 49d485a3 | 2012-05-28 21:04:18 +0000 | [diff] [blame] | 815 | .net_id = &sctp_net_id, |
Gao feng | f42c418 | 2012-06-21 04:36:46 +0000 | [diff] [blame] | 816 | .init_net = sctp_init_net, |
Gao feng | 49d485a3 | 2012-05-28 21:04:18 +0000 | [diff] [blame] | 817 | }; |
| 818 | |
| 819 | static int sctp_net_init(struct net *net) |
| 820 | { |
| 821 | int ret = 0; |
| 822 | |
Gao feng | c296bb4 | 2013-01-23 12:51:10 +0100 | [diff] [blame] | 823 | ret = nf_ct_l4proto_pernet_register(net, &nf_conntrack_l4proto_sctp4); |
Gao feng | 49d485a3 | 2012-05-28 21:04:18 +0000 | [diff] [blame] | 824 | if (ret < 0) { |
Gao feng | c296bb4 | 2013-01-23 12:51:10 +0100 | [diff] [blame] | 825 | pr_err("nf_conntrack_sctp4: pernet registration failed.\n"); |
Gao feng | 49d485a3 | 2012-05-28 21:04:18 +0000 | [diff] [blame] | 826 | goto out; |
| 827 | } |
Gao feng | c296bb4 | 2013-01-23 12:51:10 +0100 | [diff] [blame] | 828 | ret = nf_ct_l4proto_pernet_register(net, &nf_conntrack_l4proto_sctp6); |
Gao feng | 49d485a3 | 2012-05-28 21:04:18 +0000 | [diff] [blame] | 829 | if (ret < 0) { |
Gao feng | c296bb4 | 2013-01-23 12:51:10 +0100 | [diff] [blame] | 830 | pr_err("nf_conntrack_sctp6: pernet registration failed.\n"); |
Gao feng | 49d485a3 | 2012-05-28 21:04:18 +0000 | [diff] [blame] | 831 | goto cleanup_sctp4; |
| 832 | } |
| 833 | return 0; |
| 834 | |
| 835 | cleanup_sctp4: |
Gao feng | c296bb4 | 2013-01-23 12:51:10 +0100 | [diff] [blame] | 836 | nf_ct_l4proto_pernet_unregister(net, &nf_conntrack_l4proto_sctp4); |
Gao feng | 49d485a3 | 2012-05-28 21:04:18 +0000 | [diff] [blame] | 837 | out: |
| 838 | return ret; |
| 839 | } |
| 840 | |
| 841 | static void sctp_net_exit(struct net *net) |
| 842 | { |
Gao feng | c296bb4 | 2013-01-23 12:51:10 +0100 | [diff] [blame] | 843 | nf_ct_l4proto_pernet_unregister(net, &nf_conntrack_l4proto_sctp6); |
| 844 | nf_ct_l4proto_pernet_unregister(net, &nf_conntrack_l4proto_sctp4); |
Gao feng | 49d485a3 | 2012-05-28 21:04:18 +0000 | [diff] [blame] | 845 | } |
| 846 | |
| 847 | static struct pernet_operations sctp_net_ops = { |
| 848 | .init = sctp_net_init, |
| 849 | .exit = sctp_net_exit, |
| 850 | .id = &sctp_net_id, |
| 851 | .size = sizeof(struct sctp_net), |
Patrick McHardy | 933a41e | 2006-11-29 02:35:18 +0100 | [diff] [blame] | 852 | }; |
| 853 | |
Stephen Hemminger | 2f0d2f1 | 2008-01-31 04:08:10 -0800 | [diff] [blame] | 854 | static int __init nf_conntrack_proto_sctp_init(void) |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 855 | { |
Gao feng | c296bb4 | 2013-01-23 12:51:10 +0100 | [diff] [blame] | 856 | int ret; |
| 857 | |
Gao feng | 0d98da5 | 2013-03-07 17:20:46 +0000 | [diff] [blame] | 858 | ret = register_pernet_subsys(&sctp_net_ops); |
| 859 | if (ret < 0) |
| 860 | goto out_pernet; |
| 861 | |
Gao feng | c296bb4 | 2013-01-23 12:51:10 +0100 | [diff] [blame] | 862 | ret = nf_ct_l4proto_register(&nf_conntrack_l4proto_sctp4); |
| 863 | if (ret < 0) |
| 864 | goto out_sctp4; |
| 865 | |
| 866 | ret = nf_ct_l4proto_register(&nf_conntrack_l4proto_sctp6); |
| 867 | if (ret < 0) |
| 868 | goto out_sctp6; |
| 869 | |
Gao feng | c296bb4 | 2013-01-23 12:51:10 +0100 | [diff] [blame] | 870 | return 0; |
Gao feng | c296bb4 | 2013-01-23 12:51:10 +0100 | [diff] [blame] | 871 | out_sctp6: |
| 872 | nf_ct_l4proto_unregister(&nf_conntrack_l4proto_sctp4); |
| 873 | out_sctp4: |
Gao feng | 0d98da5 | 2013-03-07 17:20:46 +0000 | [diff] [blame] | 874 | unregister_pernet_subsys(&sctp_net_ops); |
| 875 | out_pernet: |
Gao feng | c296bb4 | 2013-01-23 12:51:10 +0100 | [diff] [blame] | 876 | return ret; |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 877 | } |
| 878 | |
Stephen Hemminger | 2f0d2f1 | 2008-01-31 04:08:10 -0800 | [diff] [blame] | 879 | static void __exit nf_conntrack_proto_sctp_fini(void) |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 880 | { |
Gao feng | c296bb4 | 2013-01-23 12:51:10 +0100 | [diff] [blame] | 881 | nf_ct_l4proto_unregister(&nf_conntrack_l4proto_sctp6); |
| 882 | nf_ct_l4proto_unregister(&nf_conntrack_l4proto_sctp4); |
Gao feng | 49d485a3 | 2012-05-28 21:04:18 +0000 | [diff] [blame] | 883 | unregister_pernet_subsys(&sctp_net_ops); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 884 | } |
| 885 | |
Andrew Morton | 65b4b4e | 2006-03-28 16:37:06 -0800 | [diff] [blame] | 886 | module_init(nf_conntrack_proto_sctp_init); |
| 887 | module_exit(nf_conntrack_proto_sctp_fini); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 888 | |
| 889 | MODULE_LICENSE("GPL"); |
| 890 | MODULE_AUTHOR("Kiran Kumar Immidi"); |
| 891 | MODULE_DESCRIPTION("Netfilter connection tracking protocol helper for SCTP"); |
Patrick McHardy | d2483dd | 2006-12-02 22:06:05 -0800 | [diff] [blame] | 892 | MODULE_ALIAS("ip_conntrack_proto_sctp"); |