1 // SPDX-License-Identifier: GPL-2.0-only
3 * Connection tracking protocol helper module for SCTP.
5 * Copyright (c) 2004 Kiran Kumar Immidi <immidi_kiran@yahoo.com>
6 * Copyright (c) 2004-2012 Patrick McHardy <kaber@trash.net>
8 * SCTP is defined in RFC 2960. References to various sections in this code
12 #include <linux/types.h>
13 #include <linux/timer.h>
14 #include <linux/netfilter.h>
17 #include <linux/sctp.h>
18 #include <linux/string.h>
19 #include <linux/seq_file.h>
20 #include <linux/spinlock.h>
21 #include <linux/interrupt.h>
22 #include <net/sctp/checksum.h>
24 #include <net/netfilter/nf_log.h>
25 #include <net/netfilter/nf_conntrack.h>
26 #include <net/netfilter/nf_conntrack_l4proto.h>
27 #include <net/netfilter/nf_conntrack_ecache.h>
28 #include <net/netfilter/nf_conntrack_timeout.h>
30 static const char *const sctp_conntrack_names[] = {
31 [SCTP_CONNTRACK_NONE] = "NONE",
32 [SCTP_CONNTRACK_CLOSED] = "CLOSED",
33 [SCTP_CONNTRACK_COOKIE_WAIT] = "COOKIE_WAIT",
34 [SCTP_CONNTRACK_COOKIE_ECHOED] = "COOKIE_ECHOED",
35 [SCTP_CONNTRACK_ESTABLISHED] = "ESTABLISHED",
36 [SCTP_CONNTRACK_SHUTDOWN_SENT] = "SHUTDOWN_SENT",
37 [SCTP_CONNTRACK_SHUTDOWN_RECD] = "SHUTDOWN_RECD",
38 [SCTP_CONNTRACK_SHUTDOWN_ACK_SENT] = "SHUTDOWN_ACK_SENT",
39 [SCTP_CONNTRACK_HEARTBEAT_SENT] = "HEARTBEAT_SENT",
43 #define MINS * 60 SECS
44 #define HOURS * 60 MINS
45 #define DAYS * 24 HOURS
47 static const unsigned int sctp_timeouts[SCTP_CONNTRACK_MAX] = {
48 [SCTP_CONNTRACK_CLOSED] = 10 SECS,
49 [SCTP_CONNTRACK_COOKIE_WAIT] = 3 SECS,
50 [SCTP_CONNTRACK_COOKIE_ECHOED] = 3 SECS,
51 [SCTP_CONNTRACK_ESTABLISHED] = 210 SECS,
52 [SCTP_CONNTRACK_SHUTDOWN_SENT] = 3 SECS,
53 [SCTP_CONNTRACK_SHUTDOWN_RECD] = 3 SECS,
54 [SCTP_CONNTRACK_SHUTDOWN_ACK_SENT] = 3 SECS,
55 [SCTP_CONNTRACK_HEARTBEAT_SENT] = 30 SECS,
58 #define SCTP_FLAG_HEARTBEAT_VTAG_FAILED 1
60 #define sNO SCTP_CONNTRACK_NONE
61 #define sCL SCTP_CONNTRACK_CLOSED
62 #define sCW SCTP_CONNTRACK_COOKIE_WAIT
63 #define sCE SCTP_CONNTRACK_COOKIE_ECHOED
64 #define sES SCTP_CONNTRACK_ESTABLISHED
65 #define sSS SCTP_CONNTRACK_SHUTDOWN_SENT
66 #define sSR SCTP_CONNTRACK_SHUTDOWN_RECD
67 #define sSA SCTP_CONNTRACK_SHUTDOWN_ACK_SENT
68 #define sHS SCTP_CONNTRACK_HEARTBEAT_SENT
69 #define sIV SCTP_CONNTRACK_MAX
72 These are the descriptions of the states:
74 NOTE: These state names are tantalizingly similar to the states of an
75 SCTP endpoint. But the interpretation of the states is a little different,
76 considering that these are the states of the connection and not of an end
77 point. Please note the subtleties. -Kiran
79 NONE - Nothing so far.
80 COOKIE WAIT - We have seen an INIT chunk in the original direction, or also
81 an INIT_ACK chunk in the reply direction.
82 COOKIE ECHOED - We have seen a COOKIE_ECHO chunk in the original direction.
83 ESTABLISHED - We have seen a COOKIE_ACK in the reply direction.
84 SHUTDOWN_SENT - We have seen a SHUTDOWN chunk in the original direction.
85 SHUTDOWN_RECD - We have seen a SHUTDOWN chunk in the reply directoin.
86 SHUTDOWN_ACK_SENT - We have seen a SHUTDOWN_ACK chunk in the direction opposite
87 to that of the SHUTDOWN chunk.
88 CLOSED - We have seen a SHUTDOWN_COMPLETE chunk in the direction of
89 the SHUTDOWN chunk. Connection is closed.
90 HEARTBEAT_SENT - We have seen a HEARTBEAT in a new flow.
94 - I have assumed that the first INIT is in the original direction.
95 This messes things when an INIT comes in the reply direction in CLOSED
97 - Check the error type in the reply dir before transitioning from
98 cookie echoed to closed.
99 - Sec 5.2.4 of RFC 2960
100 - Full Multi Homing support.
103 /* SCTP conntrack state transitions */
104 static const u8 sctp_conntracks[2][11][SCTP_CONNTRACK_MAX] = {
107 /* sNO, sCL, sCW, sCE, sES, sSS, sSR, sSA, sHS */
108 /* init */ {sCL, sCL, sCW, sCE, sES, sCL, sCL, sSA, sCW},
109 /* init_ack */ {sCL, sCL, sCW, sCE, sES, sSS, sSR, sSA, sCL},
110 /* abort */ {sCL, sCL, sCL, sCL, sCL, sCL, sCL, sCL, sCL},
111 /* shutdown */ {sCL, sCL, sCW, sCE, sSS, sSS, sSR, sSA, sCL},
112 /* shutdown_ack */ {sSA, sCL, sCW, sCE, sES, sSA, sSA, sSA, sSA},
113 /* error */ {sCL, sCL, sCW, sCE, sES, sSS, sSR, sSA, sCL},/* Can't have Stale cookie*/
114 /* cookie_echo */ {sCL, sCL, sCE, sCE, sES, sSS, sSR, sSA, sCL},/* 5.2.4 - Big TODO */
115 /* cookie_ack */ {sCL, sCL, sCW, sES, sES, sSS, sSR, sSA, sCL},/* Can't come in orig dir */
116 /* shutdown_comp*/ {sCL, sCL, sCW, sCE, sES, sSS, sSR, sCL, sCL},
117 /* heartbeat */ {sHS, sCL, sCW, sCE, sES, sSS, sSR, sSA, sHS},
118 /* heartbeat_ack*/ {sCL, sCL, sCW, sCE, sES, sSS, sSR, sSA, sHS},
122 /* sNO, sCL, sCW, sCE, sES, sSS, sSR, sSA, sHS */
123 /* init */ {sIV, sCL, sCW, sCE, sES, sSS, sSR, sSA, sIV},/* INIT in sCL Big TODO */
124 /* init_ack */ {sIV, sCW, sCW, sCE, sES, sSS, sSR, sSA, sIV},
125 /* abort */ {sIV, sCL, sCL, sCL, sCL, sCL, sCL, sCL, sIV},
126 /* shutdown */ {sIV, sCL, sCW, sCE, sSR, sSS, sSR, sSA, sIV},
127 /* shutdown_ack */ {sIV, sCL, sCW, sCE, sES, sSA, sSA, sSA, sIV},
128 /* error */ {sIV, sCL, sCW, sCL, sES, sSS, sSR, sSA, sIV},
129 /* cookie_echo */ {sIV, sCL, sCE, sCE, sES, sSS, sSR, sSA, sIV},/* Can't come in reply dir */
130 /* cookie_ack */ {sIV, sCL, sCW, sES, sES, sSS, sSR, sSA, sIV},
131 /* shutdown_comp*/ {sIV, sCL, sCW, sCE, sES, sSS, sSR, sCL, sIV},
132 /* heartbeat */ {sIV, sCL, sCW, sCE, sES, sSS, sSR, sSA, sHS},
133 /* heartbeat_ack*/ {sIV, sCL, sCW, sCE, sES, sSS, sSR, sSA, sES},
137 #ifdef CONFIG_NF_CONNTRACK_PROCFS
138 /* Print out the private part of the conntrack. */
139 static void sctp_print_conntrack(struct seq_file *s, struct nf_conn *ct)
141 seq_printf(s, "%s ", sctp_conntrack_names[ct->proto.sctp.state]);
145 #define for_each_sctp_chunk(skb, sch, _sch, offset, dataoff, count) \
146 for ((offset) = (dataoff) + sizeof(struct sctphdr), (count) = 0; \
147 (offset) < (skb)->len && \
148 ((sch) = skb_header_pointer((skb), (offset), sizeof(_sch), &(_sch))); \
149 (offset) += (ntohs((sch)->length) + 3) & ~3, (count)++)
151 /* Some validity checks to make sure the chunks are fine */
152 static int do_basic_checks(struct nf_conn *ct,
153 const struct sk_buff *skb,
154 unsigned int dataoff,
157 u_int32_t offset, count;
158 struct sctp_chunkhdr _sch, *sch;
163 for_each_sctp_chunk (skb, sch, _sch, offset, dataoff, count) {
164 pr_debug("Chunk Num: %d Type: %d\n", count, sch->type);
166 if (sch->type == SCTP_CID_INIT ||
167 sch->type == SCTP_CID_INIT_ACK ||
168 sch->type == SCTP_CID_SHUTDOWN_COMPLETE)
172 * Cookie Ack/Echo chunks not the first OR
173 * Init / Init Ack / Shutdown compl chunks not the only chunks
176 if (((sch->type == SCTP_CID_COOKIE_ACK ||
177 sch->type == SCTP_CID_COOKIE_ECHO ||
179 count != 0) || !sch->length) {
180 pr_debug("Basic checks failed\n");
185 set_bit(sch->type, map);
188 pr_debug("Basic checks passed\n");
192 static int sctp_new_state(enum ip_conntrack_dir dir,
193 enum sctp_conntrack cur_state,
198 pr_debug("Chunk type: %d\n", chunk_type);
200 switch (chunk_type) {
202 pr_debug("SCTP_CID_INIT\n");
205 case SCTP_CID_INIT_ACK:
206 pr_debug("SCTP_CID_INIT_ACK\n");
210 pr_debug("SCTP_CID_ABORT\n");
213 case SCTP_CID_SHUTDOWN:
214 pr_debug("SCTP_CID_SHUTDOWN\n");
217 case SCTP_CID_SHUTDOWN_ACK:
218 pr_debug("SCTP_CID_SHUTDOWN_ACK\n");
222 pr_debug("SCTP_CID_ERROR\n");
225 case SCTP_CID_COOKIE_ECHO:
226 pr_debug("SCTP_CID_COOKIE_ECHO\n");
229 case SCTP_CID_COOKIE_ACK:
230 pr_debug("SCTP_CID_COOKIE_ACK\n");
233 case SCTP_CID_SHUTDOWN_COMPLETE:
234 pr_debug("SCTP_CID_SHUTDOWN_COMPLETE\n");
237 case SCTP_CID_HEARTBEAT:
238 pr_debug("SCTP_CID_HEARTBEAT");
241 case SCTP_CID_HEARTBEAT_ACK:
242 pr_debug("SCTP_CID_HEARTBEAT_ACK");
246 /* Other chunks like DATA or SACK do not change the state */
247 pr_debug("Unknown chunk type, Will stay in %s\n",
248 sctp_conntrack_names[cur_state]);
252 pr_debug("dir: %d cur_state: %s chunk_type: %d new_state: %s\n",
253 dir, sctp_conntrack_names[cur_state], chunk_type,
254 sctp_conntrack_names[sctp_conntracks[dir][i][cur_state]]);
256 return sctp_conntracks[dir][i][cur_state];
259 /* Don't need lock here: this conntrack not in circulation yet */
261 sctp_new(struct nf_conn *ct, const struct sk_buff *skb,
262 const struct sctphdr *sh, unsigned int dataoff)
264 enum sctp_conntrack new_state;
265 const struct sctp_chunkhdr *sch;
266 struct sctp_chunkhdr _sch;
269 memset(&ct->proto.sctp, 0, sizeof(ct->proto.sctp));
270 new_state = SCTP_CONNTRACK_MAX;
271 for_each_sctp_chunk(skb, sch, _sch, offset, dataoff, count) {
272 new_state = sctp_new_state(IP_CT_DIR_ORIGINAL,
273 SCTP_CONNTRACK_NONE, sch->type);
275 /* Invalid: delete conntrack */
276 if (new_state == SCTP_CONNTRACK_NONE ||
277 new_state == SCTP_CONNTRACK_MAX) {
278 pr_debug("nf_conntrack_sctp: invalid new deleting.\n");
282 /* Copy the vtag into the state info */
283 if (sch->type == SCTP_CID_INIT) {
284 struct sctp_inithdr _inithdr, *ih;
289 ih = skb_header_pointer(skb, offset + sizeof(_sch),
290 sizeof(_inithdr), &_inithdr);
294 pr_debug("Setting vtag %x for new conn\n",
297 ct->proto.sctp.vtag[IP_CT_DIR_REPLY] = ih->init_tag;
298 } else if (sch->type == SCTP_CID_HEARTBEAT) {
299 pr_debug("Setting vtag %x for secondary conntrack\n",
301 ct->proto.sctp.vtag[IP_CT_DIR_ORIGINAL] = sh->vtag;
302 } else if (sch->type == SCTP_CID_SHUTDOWN_ACK) {
303 /* If it is a shutdown ack OOTB packet, we expect a return
304 shutdown complete, otherwise an ABORT Sec 8.4 (5) and (8) */
305 pr_debug("Setting vtag %x for new conn OOTB\n",
307 ct->proto.sctp.vtag[IP_CT_DIR_REPLY] = sh->vtag;
310 ct->proto.sctp.state = SCTP_CONNTRACK_NONE;
316 static bool sctp_error(struct sk_buff *skb,
317 unsigned int dataoff,
318 const struct nf_hook_state *state)
320 const struct sctphdr *sh;
323 if (skb->len < dataoff + sizeof(struct sctphdr)) {
324 logmsg = "nf_ct_sctp: short packet ";
327 if (state->hook == NF_INET_PRE_ROUTING &&
328 state->net->ct.sysctl_checksum &&
329 skb->ip_summed == CHECKSUM_NONE) {
330 if (skb_ensure_writable(skb, dataoff + sizeof(*sh))) {
331 logmsg = "nf_ct_sctp: failed to read header ";
334 sh = (const struct sctphdr *)(skb->data + dataoff);
335 if (sh->checksum != sctp_compute_cksum(skb, dataoff)) {
336 logmsg = "nf_ct_sctp: bad CRC ";
339 skb->ip_summed = CHECKSUM_UNNECESSARY;
343 nf_l4proto_log_invalid(skb, state, IPPROTO_SCTP, "%s", logmsg);
347 /* Returns verdict for packet, or -NF_ACCEPT for invalid. */
348 int nf_conntrack_sctp_packet(struct nf_conn *ct,
350 unsigned int dataoff,
351 enum ip_conntrack_info ctinfo,
352 const struct nf_hook_state *state)
354 enum sctp_conntrack new_state, old_state;
355 enum ip_conntrack_dir dir = CTINFO2DIR(ctinfo);
356 const struct sctphdr *sh;
357 struct sctphdr _sctph;
358 const struct sctp_chunkhdr *sch;
359 struct sctp_chunkhdr _sch;
360 u_int32_t offset, count;
361 unsigned int *timeouts;
362 unsigned long map[256 / sizeof(unsigned long)] = { 0 };
365 if (sctp_error(skb, dataoff, state))
368 sh = skb_header_pointer(skb, dataoff, sizeof(_sctph), &_sctph);
372 if (do_basic_checks(ct, skb, dataoff, map) != 0)
375 if (!nf_ct_is_confirmed(ct)) {
376 /* If an OOTB packet has any of these chunks discard (Sec 8.4) */
377 if (test_bit(SCTP_CID_ABORT, map) ||
378 test_bit(SCTP_CID_SHUTDOWN_COMPLETE, map) ||
379 test_bit(SCTP_CID_COOKIE_ACK, map))
382 if (!sctp_new(ct, skb, sh, dataoff))
386 /* Check the verification tag (Sec 8.5) */
387 if (!test_bit(SCTP_CID_INIT, map) &&
388 !test_bit(SCTP_CID_SHUTDOWN_COMPLETE, map) &&
389 !test_bit(SCTP_CID_COOKIE_ECHO, map) &&
390 !test_bit(SCTP_CID_ABORT, map) &&
391 !test_bit(SCTP_CID_SHUTDOWN_ACK, map) &&
392 !test_bit(SCTP_CID_HEARTBEAT, map) &&
393 !test_bit(SCTP_CID_HEARTBEAT_ACK, map) &&
394 sh->vtag != ct->proto.sctp.vtag[dir]) {
395 pr_debug("Verification tag check failed\n");
399 old_state = new_state = SCTP_CONNTRACK_NONE;
400 spin_lock_bh(&ct->lock);
401 for_each_sctp_chunk (skb, sch, _sch, offset, dataoff, count) {
402 /* Special cases of Verification tag check (Sec 8.5.1) */
403 if (sch->type == SCTP_CID_INIT) {
404 /* (A) vtag MUST be zero */
407 } else if (sch->type == SCTP_CID_ABORT) {
408 /* (B) vtag MUST match own vtag if T flag is unset OR
409 * MUST match peer's vtag if T flag is set
411 if ((!(sch->flags & SCTP_CHUNK_FLAG_T) &&
412 sh->vtag != ct->proto.sctp.vtag[dir]) ||
413 ((sch->flags & SCTP_CHUNK_FLAG_T) &&
414 sh->vtag != ct->proto.sctp.vtag[!dir]))
416 } else if (sch->type == SCTP_CID_SHUTDOWN_COMPLETE) {
417 /* (C) vtag MUST match own vtag if T flag is unset OR
418 * MUST match peer's vtag if T flag is set
420 if ((!(sch->flags & SCTP_CHUNK_FLAG_T) &&
421 sh->vtag != ct->proto.sctp.vtag[dir]) ||
422 ((sch->flags & SCTP_CHUNK_FLAG_T) &&
423 sh->vtag != ct->proto.sctp.vtag[!dir]))
425 } else if (sch->type == SCTP_CID_COOKIE_ECHO) {
426 /* (D) vtag must be same as init_vtag as found in INIT_ACK */
427 if (sh->vtag != ct->proto.sctp.vtag[dir])
429 } else if (sch->type == SCTP_CID_COOKIE_ACK) {
430 ct->proto.sctp.init[dir] = 0;
431 ct->proto.sctp.init[!dir] = 0;
432 } else if (sch->type == SCTP_CID_HEARTBEAT) {
433 if (ct->proto.sctp.vtag[dir] == 0) {
434 pr_debug("Setting %d vtag %x for dir %d\n", sch->type, sh->vtag, dir);
435 ct->proto.sctp.vtag[dir] = sh->vtag;
436 } else if (sh->vtag != ct->proto.sctp.vtag[dir]) {
437 if (test_bit(SCTP_CID_DATA, map) || ignore)
440 ct->proto.sctp.flags |= SCTP_FLAG_HEARTBEAT_VTAG_FAILED;
441 ct->proto.sctp.last_dir = dir;
444 } else if (ct->proto.sctp.flags & SCTP_FLAG_HEARTBEAT_VTAG_FAILED) {
445 ct->proto.sctp.flags &= ~SCTP_FLAG_HEARTBEAT_VTAG_FAILED;
447 } else if (sch->type == SCTP_CID_HEARTBEAT_ACK) {
448 if (ct->proto.sctp.vtag[dir] == 0) {
449 pr_debug("Setting vtag %x for dir %d\n",
451 ct->proto.sctp.vtag[dir] = sh->vtag;
452 } else if (sh->vtag != ct->proto.sctp.vtag[dir]) {
453 if (test_bit(SCTP_CID_DATA, map) || ignore)
456 if ((ct->proto.sctp.flags & SCTP_FLAG_HEARTBEAT_VTAG_FAILED) == 0 ||
457 ct->proto.sctp.last_dir == dir)
460 ct->proto.sctp.flags &= ~SCTP_FLAG_HEARTBEAT_VTAG_FAILED;
461 ct->proto.sctp.vtag[dir] = sh->vtag;
462 ct->proto.sctp.vtag[!dir] = 0;
463 } else if (ct->proto.sctp.flags & SCTP_FLAG_HEARTBEAT_VTAG_FAILED) {
464 ct->proto.sctp.flags &= ~SCTP_FLAG_HEARTBEAT_VTAG_FAILED;
468 old_state = ct->proto.sctp.state;
469 new_state = sctp_new_state(dir, old_state, sch->type);
472 if (new_state == SCTP_CONNTRACK_MAX) {
473 pr_debug("nf_conntrack_sctp: Invalid dir=%i ctype=%u "
475 dir, sch->type, old_state);
479 /* If it is an INIT or an INIT ACK note down the vtag */
480 if (sch->type == SCTP_CID_INIT) {
481 struct sctp_inithdr _ih, *ih;
483 ih = skb_header_pointer(skb, offset + sizeof(_sch), sizeof(*ih), &_ih);
487 if (ct->proto.sctp.init[dir] && ct->proto.sctp.init[!dir])
488 ct->proto.sctp.init[!dir] = 0;
489 ct->proto.sctp.init[dir] = 1;
491 pr_debug("Setting vtag %x for dir %d\n", ih->init_tag, !dir);
492 ct->proto.sctp.vtag[!dir] = ih->init_tag;
494 /* don't renew timeout on init retransmit so
495 * port reuse by client or NAT middlebox cannot
496 * keep entry alive indefinitely (incl. nat info).
498 if (new_state == SCTP_CONNTRACK_CLOSED &&
499 old_state == SCTP_CONNTRACK_CLOSED &&
500 nf_ct_is_confirmed(ct))
502 } else if (sch->type == SCTP_CID_INIT_ACK) {
503 struct sctp_inithdr _ih, *ih;
506 ih = skb_header_pointer(skb, offset + sizeof(_sch), sizeof(*ih), &_ih);
510 vtag = ct->proto.sctp.vtag[!dir];
511 if (!ct->proto.sctp.init[!dir] && vtag && vtag != ih->init_tag)
514 if (ct->proto.sctp.init[dir] && ct->proto.sctp.init[!dir] &&
515 vtag != ih->init_tag)
518 pr_debug("Setting vtag %x for dir %d\n", ih->init_tag, !dir);
519 ct->proto.sctp.vtag[!dir] = ih->init_tag;
522 ct->proto.sctp.state = new_state;
523 if (old_state != new_state) {
524 nf_conntrack_event_cache(IPCT_PROTOINFO, ct);
525 if (new_state == SCTP_CONNTRACK_ESTABLISHED &&
526 !test_and_set_bit(IPS_ASSURED_BIT, &ct->status))
527 nf_conntrack_event_cache(IPCT_ASSURED, ct);
530 spin_unlock_bh(&ct->lock);
532 /* allow but do not refresh timeout */
536 timeouts = nf_ct_timeout_lookup(ct);
538 timeouts = nf_sctp_pernet(nf_ct_net(ct))->timeouts;
540 nf_ct_refresh_acct(ct, ctinfo, skb, timeouts[new_state]);
545 spin_unlock_bh(&ct->lock);
550 static bool sctp_can_early_drop(const struct nf_conn *ct)
552 switch (ct->proto.sctp.state) {
553 case SCTP_CONNTRACK_SHUTDOWN_SENT:
554 case SCTP_CONNTRACK_SHUTDOWN_RECD:
555 case SCTP_CONNTRACK_SHUTDOWN_ACK_SENT:
564 #if IS_ENABLED(CONFIG_NF_CT_NETLINK)
566 #include <linux/netfilter/nfnetlink.h>
567 #include <linux/netfilter/nfnetlink_conntrack.h>
569 static int sctp_to_nlattr(struct sk_buff *skb, struct nlattr *nla,
570 struct nf_conn *ct, bool destroy)
572 struct nlattr *nest_parms;
574 spin_lock_bh(&ct->lock);
575 nest_parms = nla_nest_start(skb, CTA_PROTOINFO_SCTP);
577 goto nla_put_failure;
579 if (nla_put_u8(skb, CTA_PROTOINFO_SCTP_STATE, ct->proto.sctp.state))
580 goto nla_put_failure;
585 if (nla_put_be32(skb, CTA_PROTOINFO_SCTP_VTAG_ORIGINAL,
586 ct->proto.sctp.vtag[IP_CT_DIR_ORIGINAL]) ||
587 nla_put_be32(skb, CTA_PROTOINFO_SCTP_VTAG_REPLY,
588 ct->proto.sctp.vtag[IP_CT_DIR_REPLY]))
589 goto nla_put_failure;
592 spin_unlock_bh(&ct->lock);
593 nla_nest_end(skb, nest_parms);
598 spin_unlock_bh(&ct->lock);
602 static const struct nla_policy sctp_nla_policy[CTA_PROTOINFO_SCTP_MAX+1] = {
603 [CTA_PROTOINFO_SCTP_STATE] = { .type = NLA_U8 },
604 [CTA_PROTOINFO_SCTP_VTAG_ORIGINAL] = { .type = NLA_U32 },
605 [CTA_PROTOINFO_SCTP_VTAG_REPLY] = { .type = NLA_U32 },
608 #define SCTP_NLATTR_SIZE ( \
609 NLA_ALIGN(NLA_HDRLEN + 1) + \
610 NLA_ALIGN(NLA_HDRLEN + 4) + \
611 NLA_ALIGN(NLA_HDRLEN + 4))
613 static int nlattr_to_sctp(struct nlattr *cda[], struct nf_conn *ct)
615 struct nlattr *attr = cda[CTA_PROTOINFO_SCTP];
616 struct nlattr *tb[CTA_PROTOINFO_SCTP_MAX+1];
619 /* updates may not contain the internal protocol info, skip parsing */
623 err = nla_parse_nested_deprecated(tb, CTA_PROTOINFO_SCTP_MAX, attr,
624 sctp_nla_policy, NULL);
628 if (!tb[CTA_PROTOINFO_SCTP_STATE] ||
629 !tb[CTA_PROTOINFO_SCTP_VTAG_ORIGINAL] ||
630 !tb[CTA_PROTOINFO_SCTP_VTAG_REPLY])
633 spin_lock_bh(&ct->lock);
634 ct->proto.sctp.state = nla_get_u8(tb[CTA_PROTOINFO_SCTP_STATE]);
635 ct->proto.sctp.vtag[IP_CT_DIR_ORIGINAL] =
636 nla_get_be32(tb[CTA_PROTOINFO_SCTP_VTAG_ORIGINAL]);
637 ct->proto.sctp.vtag[IP_CT_DIR_REPLY] =
638 nla_get_be32(tb[CTA_PROTOINFO_SCTP_VTAG_REPLY]);
639 spin_unlock_bh(&ct->lock);
645 #ifdef CONFIG_NF_CONNTRACK_TIMEOUT
647 #include <linux/netfilter/nfnetlink.h>
648 #include <linux/netfilter/nfnetlink_cttimeout.h>
650 static int sctp_timeout_nlattr_to_obj(struct nlattr *tb[],
651 struct net *net, void *data)
653 unsigned int *timeouts = data;
654 struct nf_sctp_net *sn = nf_sctp_pernet(net);
658 timeouts = sn->timeouts;
660 /* set default SCTP timeouts. */
661 for (i=0; i<SCTP_CONNTRACK_MAX; i++)
662 timeouts[i] = sn->timeouts[i];
664 /* there's a 1:1 mapping between attributes and protocol states. */
665 for (i=CTA_TIMEOUT_SCTP_UNSPEC+1; i<CTA_TIMEOUT_SCTP_MAX+1; i++) {
667 timeouts[i] = ntohl(nla_get_be32(tb[i])) * HZ;
671 timeouts[CTA_TIMEOUT_SCTP_UNSPEC] = timeouts[CTA_TIMEOUT_SCTP_CLOSED];
676 sctp_timeout_obj_to_nlattr(struct sk_buff *skb, const void *data)
678 const unsigned int *timeouts = data;
681 for (i=CTA_TIMEOUT_SCTP_UNSPEC+1; i<CTA_TIMEOUT_SCTP_MAX+1; i++) {
682 if (nla_put_be32(skb, i, htonl(timeouts[i] / HZ)))
683 goto nla_put_failure;
691 static const struct nla_policy
692 sctp_timeout_nla_policy[CTA_TIMEOUT_SCTP_MAX+1] = {
693 [CTA_TIMEOUT_SCTP_CLOSED] = { .type = NLA_U32 },
694 [CTA_TIMEOUT_SCTP_COOKIE_WAIT] = { .type = NLA_U32 },
695 [CTA_TIMEOUT_SCTP_COOKIE_ECHOED] = { .type = NLA_U32 },
696 [CTA_TIMEOUT_SCTP_ESTABLISHED] = { .type = NLA_U32 },
697 [CTA_TIMEOUT_SCTP_SHUTDOWN_SENT] = { .type = NLA_U32 },
698 [CTA_TIMEOUT_SCTP_SHUTDOWN_RECD] = { .type = NLA_U32 },
699 [CTA_TIMEOUT_SCTP_SHUTDOWN_ACK_SENT] = { .type = NLA_U32 },
700 [CTA_TIMEOUT_SCTP_HEARTBEAT_SENT] = { .type = NLA_U32 },
701 [CTA_TIMEOUT_SCTP_HEARTBEAT_ACKED] = { .type = NLA_U32 },
703 #endif /* CONFIG_NF_CONNTRACK_TIMEOUT */
705 void nf_conntrack_sctp_init_net(struct net *net)
707 struct nf_sctp_net *sn = nf_sctp_pernet(net);
710 for (i = 0; i < SCTP_CONNTRACK_MAX; i++)
711 sn->timeouts[i] = sctp_timeouts[i];
713 /* timeouts[0] is unused, init it so ->timeouts[0] contains
714 * 'new' timeout, like udp or icmp.
716 sn->timeouts[0] = sctp_timeouts[SCTP_CONNTRACK_CLOSED];
719 const struct nf_conntrack_l4proto nf_conntrack_l4proto_sctp = {
720 .l4proto = IPPROTO_SCTP,
721 #ifdef CONFIG_NF_CONNTRACK_PROCFS
722 .print_conntrack = sctp_print_conntrack,
724 .can_early_drop = sctp_can_early_drop,
725 #if IS_ENABLED(CONFIG_NF_CT_NETLINK)
726 .nlattr_size = SCTP_NLATTR_SIZE,
727 .to_nlattr = sctp_to_nlattr,
728 .from_nlattr = nlattr_to_sctp,
729 .tuple_to_nlattr = nf_ct_port_tuple_to_nlattr,
730 .nlattr_tuple_size = nf_ct_port_nlattr_tuple_size,
731 .nlattr_to_tuple = nf_ct_port_nlattr_to_tuple,
732 .nla_policy = nf_ct_port_nla_policy,
734 #ifdef CONFIG_NF_CONNTRACK_TIMEOUT
736 .nlattr_to_obj = sctp_timeout_nlattr_to_obj,
737 .obj_to_nlattr = sctp_timeout_obj_to_nlattr,
738 .nlattr_max = CTA_TIMEOUT_SCTP_MAX,
739 .obj_size = sizeof(unsigned int) * SCTP_CONNTRACK_MAX,
740 .nla_policy = sctp_timeout_nla_policy,
742 #endif /* CONFIG_NF_CONNTRACK_TIMEOUT */