2 * Connection tracking protocol helper module for SCTP.
4 * Copyright (c) 2004 Kiran Kumar Immidi <immidi_kiran@yahoo.com>
5 * Copyright (c) 2004-2012 Patrick McHardy <kaber@trash.net>
7 * SCTP is defined in RFC 2960. References to various sections in this code
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.
15 #include <linux/types.h>
16 #include <linux/timer.h>
17 #include <linux/netfilter.h>
20 #include <linux/sctp.h>
21 #include <linux/string.h>
22 #include <linux/seq_file.h>
23 #include <linux/spinlock.h>
24 #include <linux/interrupt.h>
25 #include <net/sctp/checksum.h>
27 #include <net/netfilter/nf_log.h>
28 #include <net/netfilter/nf_conntrack.h>
29 #include <net/netfilter/nf_conntrack_l4proto.h>
30 #include <net/netfilter/nf_conntrack_ecache.h>
31 #include <net/netfilter/nf_conntrack_timeout.h>
33 /* FIXME: Examine ipfilter's timeouts and conntrack transitions more
34 closely. They're more complex. --RR
36 And so for me for SCTP :D -Kiran */
38 static const char *const sctp_conntrack_names[] = {
52 #define MINS * 60 SECS
53 #define HOURS * 60 MINS
54 #define DAYS * 24 HOURS
56 static const unsigned int sctp_timeouts[SCTP_CONNTRACK_MAX] = {
57 [SCTP_CONNTRACK_CLOSED] = 10 SECS,
58 [SCTP_CONNTRACK_COOKIE_WAIT] = 3 SECS,
59 [SCTP_CONNTRACK_COOKIE_ECHOED] = 3 SECS,
60 [SCTP_CONNTRACK_ESTABLISHED] = 5 DAYS,
61 [SCTP_CONNTRACK_SHUTDOWN_SENT] = 300 SECS / 1000,
62 [SCTP_CONNTRACK_SHUTDOWN_RECD] = 300 SECS / 1000,
63 [SCTP_CONNTRACK_SHUTDOWN_ACK_SENT] = 3 SECS,
64 [SCTP_CONNTRACK_HEARTBEAT_SENT] = 30 SECS,
65 [SCTP_CONNTRACK_HEARTBEAT_ACKED] = 210 SECS,
68 #define SCTP_FLAG_HEARTBEAT_VTAG_FAILED 1
70 #define sNO SCTP_CONNTRACK_NONE
71 #define sCL SCTP_CONNTRACK_CLOSED
72 #define sCW SCTP_CONNTRACK_COOKIE_WAIT
73 #define sCE SCTP_CONNTRACK_COOKIE_ECHOED
74 #define sES SCTP_CONNTRACK_ESTABLISHED
75 #define sSS SCTP_CONNTRACK_SHUTDOWN_SENT
76 #define sSR SCTP_CONNTRACK_SHUTDOWN_RECD
77 #define sSA SCTP_CONNTRACK_SHUTDOWN_ACK_SENT
78 #define sHS SCTP_CONNTRACK_HEARTBEAT_SENT
79 #define sHA SCTP_CONNTRACK_HEARTBEAT_ACKED
80 #define sIV SCTP_CONNTRACK_MAX
83 These are the descriptions of the states:
85 NOTE: These state names are tantalizingly similar to the states of an
86 SCTP endpoint. But the interpretation of the states is a little different,
87 considering that these are the states of the connection and not of an end
88 point. Please note the subtleties. -Kiran
90 NONE - Nothing so far.
91 COOKIE WAIT - We have seen an INIT chunk in the original direction, or also
92 an INIT_ACK chunk in the reply direction.
93 COOKIE ECHOED - We have seen a COOKIE_ECHO chunk in the original direction.
94 ESTABLISHED - We have seen a COOKIE_ACK in the reply direction.
95 SHUTDOWN_SENT - We have seen a SHUTDOWN chunk in the original direction.
96 SHUTDOWN_RECD - We have seen a SHUTDOWN chunk in the reply directoin.
97 SHUTDOWN_ACK_SENT - We have seen a SHUTDOWN_ACK chunk in the direction opposite
98 to that of the SHUTDOWN chunk.
99 CLOSED - We have seen a SHUTDOWN_COMPLETE chunk in the direction of
100 the SHUTDOWN chunk. Connection is closed.
101 HEARTBEAT_SENT - We have seen a HEARTBEAT in a new flow.
102 HEARTBEAT_ACKED - We have seen a HEARTBEAT-ACK in the direction opposite to
103 that of the HEARTBEAT chunk. Secondary connection is
108 - I have assumed that the first INIT is in the original direction.
109 This messes things when an INIT comes in the reply direction in CLOSED
111 - Check the error type in the reply dir before transitioning from
112 cookie echoed to closed.
113 - Sec 5.2.4 of RFC 2960
114 - Full Multi Homing support.
117 /* SCTP conntrack state transitions */
118 static const u8 sctp_conntracks[2][11][SCTP_CONNTRACK_MAX] = {
121 /* sNO, sCL, sCW, sCE, sES, sSS, sSR, sSA, sHS, sHA */
122 /* init */ {sCW, sCW, sCW, sCE, sES, sSS, sSR, sSA, sCW, sHA},
123 /* init_ack */ {sCL, sCL, sCW, sCE, sES, sSS, sSR, sSA, sCL, sHA},
124 /* abort */ {sCL, sCL, sCL, sCL, sCL, sCL, sCL, sCL, sCL, sCL},
125 /* shutdown */ {sCL, sCL, sCW, sCE, sSS, sSS, sSR, sSA, sCL, sSS},
126 /* shutdown_ack */ {sSA, sCL, sCW, sCE, sES, sSA, sSA, sSA, sSA, sHA},
127 /* error */ {sCL, sCL, sCW, sCE, sES, sSS, sSR, sSA, sCL, sHA},/* Can't have Stale cookie*/
128 /* cookie_echo */ {sCL, sCL, sCE, sCE, sES, sSS, sSR, sSA, sCL, sHA},/* 5.2.4 - Big TODO */
129 /* cookie_ack */ {sCL, sCL, sCW, sCE, sES, sSS, sSR, sSA, sCL, sHA},/* Can't come in orig dir */
130 /* shutdown_comp*/ {sCL, sCL, sCW, sCE, sES, sSS, sSR, sCL, sCL, sHA},
131 /* heartbeat */ {sHS, sCL, sCW, sCE, sES, sSS, sSR, sSA, sHS, sHA},
132 /* heartbeat_ack*/ {sCL, sCL, sCW, sCE, sES, sSS, sSR, sSA, sHS, sHA}
136 /* sNO, sCL, sCW, sCE, sES, sSS, sSR, sSA, sHS, sHA */
137 /* init */ {sIV, sCL, sCW, sCE, sES, sSS, sSR, sSA, sIV, sHA},/* INIT in sCL Big TODO */
138 /* init_ack */ {sIV, sCL, sCW, sCE, sES, sSS, sSR, sSA, sIV, sHA},
139 /* abort */ {sIV, sCL, sCL, sCL, sCL, sCL, sCL, sCL, sIV, sCL},
140 /* shutdown */ {sIV, sCL, sCW, sCE, sSR, sSS, sSR, sSA, sIV, sSR},
141 /* shutdown_ack */ {sIV, sCL, sCW, sCE, sES, sSA, sSA, sSA, sIV, sHA},
142 /* error */ {sIV, sCL, sCW, sCL, sES, sSS, sSR, sSA, sIV, sHA},
143 /* cookie_echo */ {sIV, sCL, sCW, sCE, sES, sSS, sSR, sSA, sIV, sHA},/* Can't come in reply dir */
144 /* cookie_ack */ {sIV, sCL, sCW, sES, sES, sSS, sSR, sSA, sIV, sHA},
145 /* shutdown_comp*/ {sIV, sCL, sCW, sCE, sES, sSS, sSR, sCL, sIV, sHA},
146 /* heartbeat */ {sIV, sCL, sCW, sCE, sES, sSS, sSR, sSA, sHS, sHA},
147 /* heartbeat_ack*/ {sIV, sCL, sCW, sCE, sES, sSS, sSR, sSA, sHA, sHA}
151 static inline struct nf_sctp_net *sctp_pernet(struct net *net)
153 return &net->ct.nf_ct_proto.sctp;
156 #ifdef CONFIG_NF_CONNTRACK_PROCFS
157 /* Print out the private part of the conntrack. */
158 static void sctp_print_conntrack(struct seq_file *s, struct nf_conn *ct)
160 seq_printf(s, "%s ", sctp_conntrack_names[ct->proto.sctp.state]);
164 #define for_each_sctp_chunk(skb, sch, _sch, offset, dataoff, count) \
165 for ((offset) = (dataoff) + sizeof(struct sctphdr), (count) = 0; \
166 (offset) < (skb)->len && \
167 ((sch) = skb_header_pointer((skb), (offset), sizeof(_sch), &(_sch))); \
168 (offset) += (ntohs((sch)->length) + 3) & ~3, (count)++)
170 /* Some validity checks to make sure the chunks are fine */
171 static int do_basic_checks(struct nf_conn *ct,
172 const struct sk_buff *skb,
173 unsigned int dataoff,
176 u_int32_t offset, count;
177 struct sctp_chunkhdr _sch, *sch;
182 for_each_sctp_chunk (skb, sch, _sch, offset, dataoff, count) {
183 pr_debug("Chunk Num: %d Type: %d\n", count, sch->type);
185 if (sch->type == SCTP_CID_INIT ||
186 sch->type == SCTP_CID_INIT_ACK ||
187 sch->type == SCTP_CID_SHUTDOWN_COMPLETE)
191 * Cookie Ack/Echo chunks not the first OR
192 * Init / Init Ack / Shutdown compl chunks not the only chunks
195 if (((sch->type == SCTP_CID_COOKIE_ACK ||
196 sch->type == SCTP_CID_COOKIE_ECHO ||
198 count != 0) || !sch->length) {
199 pr_debug("Basic checks failed\n");
204 set_bit(sch->type, map);
207 pr_debug("Basic checks passed\n");
211 static int sctp_new_state(enum ip_conntrack_dir dir,
212 enum sctp_conntrack cur_state,
217 pr_debug("Chunk type: %d\n", chunk_type);
219 switch (chunk_type) {
221 pr_debug("SCTP_CID_INIT\n");
224 case SCTP_CID_INIT_ACK:
225 pr_debug("SCTP_CID_INIT_ACK\n");
229 pr_debug("SCTP_CID_ABORT\n");
232 case SCTP_CID_SHUTDOWN:
233 pr_debug("SCTP_CID_SHUTDOWN\n");
236 case SCTP_CID_SHUTDOWN_ACK:
237 pr_debug("SCTP_CID_SHUTDOWN_ACK\n");
241 pr_debug("SCTP_CID_ERROR\n");
244 case SCTP_CID_COOKIE_ECHO:
245 pr_debug("SCTP_CID_COOKIE_ECHO\n");
248 case SCTP_CID_COOKIE_ACK:
249 pr_debug("SCTP_CID_COOKIE_ACK\n");
252 case SCTP_CID_SHUTDOWN_COMPLETE:
253 pr_debug("SCTP_CID_SHUTDOWN_COMPLETE\n");
256 case SCTP_CID_HEARTBEAT:
257 pr_debug("SCTP_CID_HEARTBEAT");
260 case SCTP_CID_HEARTBEAT_ACK:
261 pr_debug("SCTP_CID_HEARTBEAT_ACK");
265 /* Other chunks like DATA or SACK do not change the state */
266 pr_debug("Unknown chunk type, Will stay in %s\n",
267 sctp_conntrack_names[cur_state]);
271 pr_debug("dir: %d cur_state: %s chunk_type: %d new_state: %s\n",
272 dir, sctp_conntrack_names[cur_state], chunk_type,
273 sctp_conntrack_names[sctp_conntracks[dir][i][cur_state]]);
275 return sctp_conntracks[dir][i][cur_state];
278 /* Returns verdict for packet, or -NF_ACCEPT for invalid. */
279 static int sctp_packet(struct nf_conn *ct,
280 const struct sk_buff *skb,
281 unsigned int dataoff,
282 enum ip_conntrack_info ctinfo)
284 enum sctp_conntrack new_state, old_state;
285 enum ip_conntrack_dir dir = CTINFO2DIR(ctinfo);
286 const struct sctphdr *sh;
287 struct sctphdr _sctph;
288 const struct sctp_chunkhdr *sch;
289 struct sctp_chunkhdr _sch;
290 u_int32_t offset, count;
291 unsigned int *timeouts;
292 unsigned long map[256 / sizeof(unsigned long)] = { 0 };
295 sh = skb_header_pointer(skb, dataoff, sizeof(_sctph), &_sctph);
299 if (do_basic_checks(ct, skb, dataoff, map) != 0)
302 /* Check the verification tag (Sec 8.5) */
303 if (!test_bit(SCTP_CID_INIT, map) &&
304 !test_bit(SCTP_CID_SHUTDOWN_COMPLETE, map) &&
305 !test_bit(SCTP_CID_COOKIE_ECHO, map) &&
306 !test_bit(SCTP_CID_ABORT, map) &&
307 !test_bit(SCTP_CID_SHUTDOWN_ACK, map) &&
308 !test_bit(SCTP_CID_HEARTBEAT, map) &&
309 !test_bit(SCTP_CID_HEARTBEAT_ACK, map) &&
310 sh->vtag != ct->proto.sctp.vtag[dir]) {
311 pr_debug("Verification tag check failed\n");
315 old_state = new_state = SCTP_CONNTRACK_NONE;
316 spin_lock_bh(&ct->lock);
317 for_each_sctp_chunk (skb, sch, _sch, offset, dataoff, count) {
318 /* Special cases of Verification tag check (Sec 8.5.1) */
319 if (sch->type == SCTP_CID_INIT) {
323 } else if (sch->type == SCTP_CID_ABORT) {
325 if (sh->vtag != ct->proto.sctp.vtag[dir] &&
326 sh->vtag != ct->proto.sctp.vtag[!dir])
328 } else if (sch->type == SCTP_CID_SHUTDOWN_COMPLETE) {
330 if (sh->vtag != ct->proto.sctp.vtag[dir] &&
331 sh->vtag != ct->proto.sctp.vtag[!dir] &&
332 sch->flags & SCTP_CHUNK_FLAG_T)
334 } else if (sch->type == SCTP_CID_COOKIE_ECHO) {
336 if (sh->vtag != ct->proto.sctp.vtag[dir])
338 } else if (sch->type == SCTP_CID_HEARTBEAT) {
339 if (ct->proto.sctp.vtag[dir] == 0) {
340 pr_debug("Setting %d vtag %x for dir %d\n", sch->type, sh->vtag, dir);
341 ct->proto.sctp.vtag[dir] = sh->vtag;
342 } else if (sh->vtag != ct->proto.sctp.vtag[dir]) {
343 if (test_bit(SCTP_CID_DATA, map) || ignore)
346 ct->proto.sctp.flags |= SCTP_FLAG_HEARTBEAT_VTAG_FAILED;
347 ct->proto.sctp.last_dir = dir;
350 } else if (ct->proto.sctp.flags & SCTP_FLAG_HEARTBEAT_VTAG_FAILED) {
351 ct->proto.sctp.flags &= ~SCTP_FLAG_HEARTBEAT_VTAG_FAILED;
353 } else if (sch->type == SCTP_CID_HEARTBEAT_ACK) {
354 if (ct->proto.sctp.vtag[dir] == 0) {
355 pr_debug("Setting vtag %x for dir %d\n",
357 ct->proto.sctp.vtag[dir] = sh->vtag;
358 } else if (sh->vtag != ct->proto.sctp.vtag[dir]) {
359 if (test_bit(SCTP_CID_DATA, map) || ignore)
362 if ((ct->proto.sctp.flags & SCTP_FLAG_HEARTBEAT_VTAG_FAILED) == 0 ||
363 ct->proto.sctp.last_dir == dir)
366 ct->proto.sctp.flags &= ~SCTP_FLAG_HEARTBEAT_VTAG_FAILED;
367 ct->proto.sctp.vtag[dir] = sh->vtag;
368 ct->proto.sctp.vtag[!dir] = 0;
369 } else if (ct->proto.sctp.flags & SCTP_FLAG_HEARTBEAT_VTAG_FAILED) {
370 ct->proto.sctp.flags &= ~SCTP_FLAG_HEARTBEAT_VTAG_FAILED;
374 old_state = ct->proto.sctp.state;
375 new_state = sctp_new_state(dir, old_state, sch->type);
378 if (new_state == SCTP_CONNTRACK_MAX) {
379 pr_debug("nf_conntrack_sctp: Invalid dir=%i ctype=%u "
381 dir, sch->type, old_state);
385 /* If it is an INIT or an INIT ACK note down the vtag */
386 if (sch->type == SCTP_CID_INIT ||
387 sch->type == SCTP_CID_INIT_ACK) {
388 struct sctp_inithdr _inithdr, *ih;
390 ih = skb_header_pointer(skb, offset + sizeof(_sch),
391 sizeof(_inithdr), &_inithdr);
394 pr_debug("Setting vtag %x for dir %d\n",
396 ct->proto.sctp.vtag[!dir] = ih->init_tag;
398 /* don't renew timeout on init retransmit so
399 * port reuse by client or NAT middlebox cannot
400 * keep entry alive indefinitely (incl. nat info).
402 if (new_state == SCTP_CONNTRACK_CLOSED &&
403 old_state == SCTP_CONNTRACK_CLOSED &&
404 nf_ct_is_confirmed(ct))
408 ct->proto.sctp.state = new_state;
409 if (old_state != new_state)
410 nf_conntrack_event_cache(IPCT_PROTOINFO, ct);
412 spin_unlock_bh(&ct->lock);
414 /* allow but do not refresh timeout */
418 timeouts = nf_ct_timeout_lookup(ct);
420 timeouts = sctp_pernet(nf_ct_net(ct))->timeouts;
422 nf_ct_refresh_acct(ct, ctinfo, skb, timeouts[new_state]);
424 if (old_state == SCTP_CONNTRACK_COOKIE_ECHOED &&
425 dir == IP_CT_DIR_REPLY &&
426 new_state == SCTP_CONNTRACK_ESTABLISHED) {
427 pr_debug("Setting assured bit\n");
428 set_bit(IPS_ASSURED_BIT, &ct->status);
429 nf_conntrack_event_cache(IPCT_ASSURED, ct);
435 spin_unlock_bh(&ct->lock);
440 /* Called when a new connection for this protocol found. */
441 static bool sctp_new(struct nf_conn *ct, const struct sk_buff *skb,
442 unsigned int dataoff)
444 enum sctp_conntrack new_state;
445 const struct sctphdr *sh;
446 struct sctphdr _sctph;
447 const struct sctp_chunkhdr *sch;
448 struct sctp_chunkhdr _sch;
449 u_int32_t offset, count;
450 unsigned long map[256 / sizeof(unsigned long)] = { 0 };
452 sh = skb_header_pointer(skb, dataoff, sizeof(_sctph), &_sctph);
456 if (do_basic_checks(ct, skb, dataoff, map) != 0)
459 /* If an OOTB packet has any of these chunks discard (Sec 8.4) */
460 if (test_bit(SCTP_CID_ABORT, map) ||
461 test_bit(SCTP_CID_SHUTDOWN_COMPLETE, map) ||
462 test_bit(SCTP_CID_COOKIE_ACK, map))
465 memset(&ct->proto.sctp, 0, sizeof(ct->proto.sctp));
466 new_state = SCTP_CONNTRACK_MAX;
467 for_each_sctp_chunk (skb, sch, _sch, offset, dataoff, count) {
468 /* Don't need lock here: this conntrack not in circulation yet */
469 new_state = sctp_new_state(IP_CT_DIR_ORIGINAL,
470 SCTP_CONNTRACK_NONE, sch->type);
472 /* Invalid: delete conntrack */
473 if (new_state == SCTP_CONNTRACK_NONE ||
474 new_state == SCTP_CONNTRACK_MAX) {
475 pr_debug("nf_conntrack_sctp: invalid new deleting.\n");
479 /* Copy the vtag into the state info */
480 if (sch->type == SCTP_CID_INIT) {
481 struct sctp_inithdr _inithdr, *ih;
486 ih = skb_header_pointer(skb, offset + sizeof(_sch),
487 sizeof(_inithdr), &_inithdr);
491 pr_debug("Setting vtag %x for new conn\n",
494 ct->proto.sctp.vtag[IP_CT_DIR_REPLY] = ih->init_tag;
495 } else if (sch->type == SCTP_CID_HEARTBEAT) {
496 pr_debug("Setting vtag %x for secondary conntrack\n",
498 ct->proto.sctp.vtag[IP_CT_DIR_ORIGINAL] = sh->vtag;
500 /* If it is a shutdown ack OOTB packet, we expect a return
501 shutdown complete, otherwise an ABORT Sec 8.4 (5) and (8) */
503 pr_debug("Setting vtag %x for new conn OOTB\n",
505 ct->proto.sctp.vtag[IP_CT_DIR_REPLY] = sh->vtag;
508 ct->proto.sctp.state = new_state;
514 static int sctp_error(struct net *net, struct nf_conn *tpl, struct sk_buff *skb,
515 unsigned int dataoff,
516 u8 pf, unsigned int hooknum)
518 const struct sctphdr *sh;
521 if (skb->len < dataoff + sizeof(struct sctphdr)) {
522 logmsg = "nf_ct_sctp: short packet ";
525 if (net->ct.sysctl_checksum && hooknum == NF_INET_PRE_ROUTING &&
526 skb->ip_summed == CHECKSUM_NONE) {
527 if (!skb_make_writable(skb, dataoff + sizeof(struct sctphdr))) {
528 logmsg = "nf_ct_sctp: failed to read header ";
531 sh = (const struct sctphdr *)(skb->data + dataoff);
532 if (sh->checksum != sctp_compute_cksum(skb, dataoff)) {
533 logmsg = "nf_ct_sctp: bad CRC ";
536 skb->ip_summed = CHECKSUM_UNNECESSARY;
540 nf_l4proto_log_invalid(skb, net, pf, IPPROTO_SCTP, "%s", logmsg);
544 static bool sctp_can_early_drop(const struct nf_conn *ct)
546 switch (ct->proto.sctp.state) {
547 case SCTP_CONNTRACK_SHUTDOWN_SENT:
548 case SCTP_CONNTRACK_SHUTDOWN_RECD:
549 case SCTP_CONNTRACK_SHUTDOWN_ACK_SENT:
558 #if IS_ENABLED(CONFIG_NF_CT_NETLINK)
560 #include <linux/netfilter/nfnetlink.h>
561 #include <linux/netfilter/nfnetlink_conntrack.h>
563 static int sctp_to_nlattr(struct sk_buff *skb, struct nlattr *nla,
566 struct nlattr *nest_parms;
568 spin_lock_bh(&ct->lock);
569 nest_parms = nla_nest_start(skb, CTA_PROTOINFO_SCTP | NLA_F_NESTED);
571 goto nla_put_failure;
573 if (nla_put_u8(skb, CTA_PROTOINFO_SCTP_STATE, ct->proto.sctp.state) ||
574 nla_put_be32(skb, CTA_PROTOINFO_SCTP_VTAG_ORIGINAL,
575 ct->proto.sctp.vtag[IP_CT_DIR_ORIGINAL]) ||
576 nla_put_be32(skb, CTA_PROTOINFO_SCTP_VTAG_REPLY,
577 ct->proto.sctp.vtag[IP_CT_DIR_REPLY]))
578 goto nla_put_failure;
580 spin_unlock_bh(&ct->lock);
582 nla_nest_end(skb, nest_parms);
587 spin_unlock_bh(&ct->lock);
591 static const struct nla_policy sctp_nla_policy[CTA_PROTOINFO_SCTP_MAX+1] = {
592 [CTA_PROTOINFO_SCTP_STATE] = { .type = NLA_U8 },
593 [CTA_PROTOINFO_SCTP_VTAG_ORIGINAL] = { .type = NLA_U32 },
594 [CTA_PROTOINFO_SCTP_VTAG_REPLY] = { .type = NLA_U32 },
597 #define SCTP_NLATTR_SIZE ( \
598 NLA_ALIGN(NLA_HDRLEN + 1) + \
599 NLA_ALIGN(NLA_HDRLEN + 4) + \
600 NLA_ALIGN(NLA_HDRLEN + 4))
602 static int nlattr_to_sctp(struct nlattr *cda[], struct nf_conn *ct)
604 struct nlattr *attr = cda[CTA_PROTOINFO_SCTP];
605 struct nlattr *tb[CTA_PROTOINFO_SCTP_MAX+1];
608 /* updates may not contain the internal protocol info, skip parsing */
612 err = nla_parse_nested(tb, CTA_PROTOINFO_SCTP_MAX, attr,
613 sctp_nla_policy, NULL);
617 if (!tb[CTA_PROTOINFO_SCTP_STATE] ||
618 !tb[CTA_PROTOINFO_SCTP_VTAG_ORIGINAL] ||
619 !tb[CTA_PROTOINFO_SCTP_VTAG_REPLY])
622 spin_lock_bh(&ct->lock);
623 ct->proto.sctp.state = nla_get_u8(tb[CTA_PROTOINFO_SCTP_STATE]);
624 ct->proto.sctp.vtag[IP_CT_DIR_ORIGINAL] =
625 nla_get_be32(tb[CTA_PROTOINFO_SCTP_VTAG_ORIGINAL]);
626 ct->proto.sctp.vtag[IP_CT_DIR_REPLY] =
627 nla_get_be32(tb[CTA_PROTOINFO_SCTP_VTAG_REPLY]);
628 spin_unlock_bh(&ct->lock);
634 #ifdef CONFIG_NF_CONNTRACK_TIMEOUT
636 #include <linux/netfilter/nfnetlink.h>
637 #include <linux/netfilter/nfnetlink_cttimeout.h>
639 static int sctp_timeout_nlattr_to_obj(struct nlattr *tb[],
640 struct net *net, void *data)
642 unsigned int *timeouts = data;
643 struct nf_sctp_net *sn = sctp_pernet(net);
647 timeouts = sn->timeouts;
649 /* set default SCTP timeouts. */
650 for (i=0; i<SCTP_CONNTRACK_MAX; i++)
651 timeouts[i] = sn->timeouts[i];
653 /* there's a 1:1 mapping between attributes and protocol states. */
654 for (i=CTA_TIMEOUT_SCTP_UNSPEC+1; i<CTA_TIMEOUT_SCTP_MAX+1; i++) {
656 timeouts[i] = ntohl(nla_get_be32(tb[i])) * HZ;
660 timeouts[CTA_TIMEOUT_SCTP_UNSPEC] = timeouts[CTA_TIMEOUT_SCTP_CLOSED];
665 sctp_timeout_obj_to_nlattr(struct sk_buff *skb, const void *data)
667 const unsigned int *timeouts = data;
670 for (i=CTA_TIMEOUT_SCTP_UNSPEC+1; i<CTA_TIMEOUT_SCTP_MAX+1; i++) {
671 if (nla_put_be32(skb, i, htonl(timeouts[i] / HZ)))
672 goto nla_put_failure;
680 static const struct nla_policy
681 sctp_timeout_nla_policy[CTA_TIMEOUT_SCTP_MAX+1] = {
682 [CTA_TIMEOUT_SCTP_CLOSED] = { .type = NLA_U32 },
683 [CTA_TIMEOUT_SCTP_COOKIE_WAIT] = { .type = NLA_U32 },
684 [CTA_TIMEOUT_SCTP_COOKIE_ECHOED] = { .type = NLA_U32 },
685 [CTA_TIMEOUT_SCTP_ESTABLISHED] = { .type = NLA_U32 },
686 [CTA_TIMEOUT_SCTP_SHUTDOWN_SENT] = { .type = NLA_U32 },
687 [CTA_TIMEOUT_SCTP_SHUTDOWN_RECD] = { .type = NLA_U32 },
688 [CTA_TIMEOUT_SCTP_SHUTDOWN_ACK_SENT] = { .type = NLA_U32 },
689 [CTA_TIMEOUT_SCTP_HEARTBEAT_SENT] = { .type = NLA_U32 },
690 [CTA_TIMEOUT_SCTP_HEARTBEAT_ACKED] = { .type = NLA_U32 },
692 #endif /* CONFIG_NF_CONNTRACK_TIMEOUT */
696 static struct ctl_table sctp_sysctl_table[] = {
698 .procname = "nf_conntrack_sctp_timeout_closed",
699 .maxlen = sizeof(unsigned int),
701 .proc_handler = proc_dointvec_jiffies,
704 .procname = "nf_conntrack_sctp_timeout_cookie_wait",
705 .maxlen = sizeof(unsigned int),
707 .proc_handler = proc_dointvec_jiffies,
710 .procname = "nf_conntrack_sctp_timeout_cookie_echoed",
711 .maxlen = sizeof(unsigned int),
713 .proc_handler = proc_dointvec_jiffies,
716 .procname = "nf_conntrack_sctp_timeout_established",
717 .maxlen = sizeof(unsigned int),
719 .proc_handler = proc_dointvec_jiffies,
722 .procname = "nf_conntrack_sctp_timeout_shutdown_sent",
723 .maxlen = sizeof(unsigned int),
725 .proc_handler = proc_dointvec_jiffies,
728 .procname = "nf_conntrack_sctp_timeout_shutdown_recd",
729 .maxlen = sizeof(unsigned int),
731 .proc_handler = proc_dointvec_jiffies,
734 .procname = "nf_conntrack_sctp_timeout_shutdown_ack_sent",
735 .maxlen = sizeof(unsigned int),
737 .proc_handler = proc_dointvec_jiffies,
740 .procname = "nf_conntrack_sctp_timeout_heartbeat_sent",
741 .maxlen = sizeof(unsigned int),
743 .proc_handler = proc_dointvec_jiffies,
746 .procname = "nf_conntrack_sctp_timeout_heartbeat_acked",
747 .maxlen = sizeof(unsigned int),
749 .proc_handler = proc_dointvec_jiffies,
755 static int sctp_kmemdup_sysctl_table(struct nf_proto_net *pn,
756 struct nf_sctp_net *sn)
762 pn->ctl_table = kmemdup(sctp_sysctl_table,
763 sizeof(sctp_sysctl_table),
768 pn->ctl_table[0].data = &sn->timeouts[SCTP_CONNTRACK_CLOSED];
769 pn->ctl_table[1].data = &sn->timeouts[SCTP_CONNTRACK_COOKIE_WAIT];
770 pn->ctl_table[2].data = &sn->timeouts[SCTP_CONNTRACK_COOKIE_ECHOED];
771 pn->ctl_table[3].data = &sn->timeouts[SCTP_CONNTRACK_ESTABLISHED];
772 pn->ctl_table[4].data = &sn->timeouts[SCTP_CONNTRACK_SHUTDOWN_SENT];
773 pn->ctl_table[5].data = &sn->timeouts[SCTP_CONNTRACK_SHUTDOWN_RECD];
774 pn->ctl_table[6].data = &sn->timeouts[SCTP_CONNTRACK_SHUTDOWN_ACK_SENT];
775 pn->ctl_table[7].data = &sn->timeouts[SCTP_CONNTRACK_HEARTBEAT_SENT];
776 pn->ctl_table[8].data = &sn->timeouts[SCTP_CONNTRACK_HEARTBEAT_ACKED];
781 static int sctp_init_net(struct net *net, u_int16_t proto)
783 struct nf_sctp_net *sn = sctp_pernet(net);
784 struct nf_proto_net *pn = &sn->pn;
789 for (i = 0; i < SCTP_CONNTRACK_MAX; i++)
790 sn->timeouts[i] = sctp_timeouts[i];
792 /* timeouts[0] is unused, init it so ->timeouts[0] contains
793 * 'new' timeout, like udp or icmp.
795 sn->timeouts[0] = sctp_timeouts[SCTP_CONNTRACK_CLOSED];
798 return sctp_kmemdup_sysctl_table(pn, sn);
801 static struct nf_proto_net *sctp_get_net_proto(struct net *net)
803 return &net->ct.nf_ct_proto.sctp.pn;
806 const struct nf_conntrack_l4proto nf_conntrack_l4proto_sctp4 = {
808 .l4proto = IPPROTO_SCTP,
809 #ifdef CONFIG_NF_CONNTRACK_PROCFS
810 .print_conntrack = sctp_print_conntrack,
812 .packet = sctp_packet,
815 .can_early_drop = sctp_can_early_drop,
817 #if IS_ENABLED(CONFIG_NF_CT_NETLINK)
818 .nlattr_size = SCTP_NLATTR_SIZE,
819 .to_nlattr = sctp_to_nlattr,
820 .from_nlattr = nlattr_to_sctp,
821 .tuple_to_nlattr = nf_ct_port_tuple_to_nlattr,
822 .nlattr_tuple_size = nf_ct_port_nlattr_tuple_size,
823 .nlattr_to_tuple = nf_ct_port_nlattr_to_tuple,
824 .nla_policy = nf_ct_port_nla_policy,
826 #ifdef CONFIG_NF_CONNTRACK_TIMEOUT
828 .nlattr_to_obj = sctp_timeout_nlattr_to_obj,
829 .obj_to_nlattr = sctp_timeout_obj_to_nlattr,
830 .nlattr_max = CTA_TIMEOUT_SCTP_MAX,
831 .obj_size = sizeof(unsigned int) * SCTP_CONNTRACK_MAX,
832 .nla_policy = sctp_timeout_nla_policy,
834 #endif /* CONFIG_NF_CONNTRACK_TIMEOUT */
835 .init_net = sctp_init_net,
836 .get_net_proto = sctp_get_net_proto,
838 EXPORT_SYMBOL_GPL(nf_conntrack_l4proto_sctp4);
840 const struct nf_conntrack_l4proto nf_conntrack_l4proto_sctp6 = {
842 .l4proto = IPPROTO_SCTP,
843 #ifdef CONFIG_NF_CONNTRACK_PROCFS
844 .print_conntrack = sctp_print_conntrack,
846 .packet = sctp_packet,
849 .can_early_drop = sctp_can_early_drop,
851 #if IS_ENABLED(CONFIG_NF_CT_NETLINK)
852 .nlattr_size = SCTP_NLATTR_SIZE,
853 .to_nlattr = sctp_to_nlattr,
854 .from_nlattr = nlattr_to_sctp,
855 .tuple_to_nlattr = nf_ct_port_tuple_to_nlattr,
856 .nlattr_tuple_size = nf_ct_port_nlattr_tuple_size,
857 .nlattr_to_tuple = nf_ct_port_nlattr_to_tuple,
858 .nla_policy = nf_ct_port_nla_policy,
860 #ifdef CONFIG_NF_CONNTRACK_TIMEOUT
862 .nlattr_to_obj = sctp_timeout_nlattr_to_obj,
863 .obj_to_nlattr = sctp_timeout_obj_to_nlattr,
864 .nlattr_max = CTA_TIMEOUT_SCTP_MAX,
865 .obj_size = sizeof(unsigned int) * SCTP_CONNTRACK_MAX,
866 .nla_policy = sctp_timeout_nla_policy,
868 #endif /* CONFIG_NF_CONNTRACK_TIMEOUT */
869 .init_net = sctp_init_net,
870 .get_net_proto = sctp_get_net_proto,
872 EXPORT_SYMBOL_GPL(nf_conntrack_l4proto_sctp6);