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>
32 /* FIXME: Examine ipfilter's timeouts and conntrack transitions more
33 closely. They're more complex. --RR
35 And so for me for SCTP :D -Kiran */
37 static const char *const sctp_conntrack_names[] = {
51 #define MINS * 60 SECS
52 #define HOURS * 60 MINS
53 #define DAYS * 24 HOURS
55 static unsigned int sctp_timeouts[SCTP_CONNTRACK_MAX] __read_mostly = {
56 [SCTP_CONNTRACK_CLOSED] = 10 SECS,
57 [SCTP_CONNTRACK_COOKIE_WAIT] = 3 SECS,
58 [SCTP_CONNTRACK_COOKIE_ECHOED] = 3 SECS,
59 [SCTP_CONNTRACK_ESTABLISHED] = 5 DAYS,
60 [SCTP_CONNTRACK_SHUTDOWN_SENT] = 300 SECS / 1000,
61 [SCTP_CONNTRACK_SHUTDOWN_RECD] = 300 SECS / 1000,
62 [SCTP_CONNTRACK_SHUTDOWN_ACK_SENT] = 3 SECS,
63 [SCTP_CONNTRACK_HEARTBEAT_SENT] = 30 SECS,
64 [SCTP_CONNTRACK_HEARTBEAT_ACKED] = 210 SECS,
67 #define sNO SCTP_CONNTRACK_NONE
68 #define sCL SCTP_CONNTRACK_CLOSED
69 #define sCW SCTP_CONNTRACK_COOKIE_WAIT
70 #define sCE SCTP_CONNTRACK_COOKIE_ECHOED
71 #define sES SCTP_CONNTRACK_ESTABLISHED
72 #define sSS SCTP_CONNTRACK_SHUTDOWN_SENT
73 #define sSR SCTP_CONNTRACK_SHUTDOWN_RECD
74 #define sSA SCTP_CONNTRACK_SHUTDOWN_ACK_SENT
75 #define sHS SCTP_CONNTRACK_HEARTBEAT_SENT
76 #define sHA SCTP_CONNTRACK_HEARTBEAT_ACKED
77 #define sIV SCTP_CONNTRACK_MAX
80 These are the descriptions of the states:
82 NOTE: These state names are tantalizingly similar to the states of an
83 SCTP endpoint. But the interpretation of the states is a little different,
84 considering that these are the states of the connection and not of an end
85 point. Please note the subtleties. -Kiran
87 NONE - Nothing so far.
88 COOKIE WAIT - We have seen an INIT chunk in the original direction, or also
89 an INIT_ACK chunk in the reply direction.
90 COOKIE ECHOED - We have seen a COOKIE_ECHO chunk in the original direction.
91 ESTABLISHED - We have seen a COOKIE_ACK in the reply direction.
92 SHUTDOWN_SENT - We have seen a SHUTDOWN chunk in the original direction.
93 SHUTDOWN_RECD - We have seen a SHUTDOWN chunk in the reply directoin.
94 SHUTDOWN_ACK_SENT - We have seen a SHUTDOWN_ACK chunk in the direction opposite
95 to that of the SHUTDOWN chunk.
96 CLOSED - We have seen a SHUTDOWN_COMPLETE chunk in the direction of
97 the SHUTDOWN chunk. Connection is closed.
98 HEARTBEAT_SENT - We have seen a HEARTBEAT in a new flow.
99 HEARTBEAT_ACKED - We have seen a HEARTBEAT-ACK in the direction opposite to
100 that of the HEARTBEAT chunk. Secondary connection is
105 - I have assumed that the first INIT is in the original direction.
106 This messes things when an INIT comes in the reply direction in CLOSED
108 - Check the error type in the reply dir before transitioning from
109 cookie echoed to closed.
110 - Sec 5.2.4 of RFC 2960
111 - Full Multi Homing support.
114 /* SCTP conntrack state transitions */
115 static const u8 sctp_conntracks[2][11][SCTP_CONNTRACK_MAX] = {
118 /* sNO, sCL, sCW, sCE, sES, sSS, sSR, sSA, sHS, sHA */
119 /* init */ {sCW, sCW, sCW, sCE, sES, sSS, sSR, sSA, sCW, sHA},
120 /* init_ack */ {sCL, sCL, sCW, sCE, sES, sSS, sSR, sSA, sCL, sHA},
121 /* abort */ {sCL, sCL, sCL, sCL, sCL, sCL, sCL, sCL, sCL, sCL},
122 /* shutdown */ {sCL, sCL, sCW, sCE, sSS, sSS, sSR, sSA, sCL, sSS},
123 /* shutdown_ack */ {sSA, sCL, sCW, sCE, sES, sSA, sSA, sSA, sSA, sHA},
124 /* error */ {sCL, sCL, sCW, sCE, sES, sSS, sSR, sSA, sCL, sHA},/* Can't have Stale cookie*/
125 /* cookie_echo */ {sCL, sCL, sCE, sCE, sES, sSS, sSR, sSA, sCL, sHA},/* 5.2.4 - Big TODO */
126 /* cookie_ack */ {sCL, sCL, sCW, sCE, sES, sSS, sSR, sSA, sCL, sHA},/* Can't come in orig dir */
127 /* shutdown_comp*/ {sCL, sCL, sCW, sCE, sES, sSS, sSR, sCL, sCL, sHA},
128 /* heartbeat */ {sHS, sCL, sCW, sCE, sES, sSS, sSR, sSA, sHS, sHA},
129 /* heartbeat_ack*/ {sCL, sCL, sCW, sCE, sES, sSS, sSR, sSA, sHS, sHA}
133 /* sNO, sCL, sCW, sCE, sES, sSS, sSR, sSA, sHS, sHA */
134 /* init */ {sIV, sCL, sCW, sCE, sES, sSS, sSR, sSA, sIV, sHA},/* INIT in sCL Big TODO */
135 /* init_ack */ {sIV, sCL, sCW, sCE, sES, sSS, sSR, sSA, sIV, sHA},
136 /* abort */ {sIV, sCL, sCL, sCL, sCL, sCL, sCL, sCL, sIV, sCL},
137 /* shutdown */ {sIV, sCL, sCW, sCE, sSR, sSS, sSR, sSA, sIV, sSR},
138 /* shutdown_ack */ {sIV, sCL, sCW, sCE, sES, sSA, sSA, sSA, sIV, sHA},
139 /* error */ {sIV, sCL, sCW, sCL, sES, sSS, sSR, sSA, sIV, sHA},
140 /* cookie_echo */ {sIV, sCL, sCW, sCE, sES, sSS, sSR, sSA, sIV, sHA},/* Can't come in reply dir */
141 /* cookie_ack */ {sIV, sCL, sCW, sES, sES, sSS, sSR, sSA, sIV, sHA},
142 /* shutdown_comp*/ {sIV, sCL, sCW, sCE, sES, sSS, sSR, sCL, sIV, sHA},
143 /* heartbeat */ {sIV, sCL, sCW, sCE, sES, sSS, sSR, sSA, sHS, sHA},
144 /* heartbeat_ack*/ {sIV, sCL, sCW, sCE, sES, sSS, sSR, sSA, sHA, sHA}
148 static inline struct nf_sctp_net *sctp_pernet(struct net *net)
150 return &net->ct.nf_ct_proto.sctp;
153 static bool sctp_pkt_to_tuple(const struct sk_buff *skb, unsigned int dataoff,
154 struct net *net, struct nf_conntrack_tuple *tuple)
156 const struct sctphdr *hp;
159 /* Actually only need first 4 bytes to get ports. */
160 hp = skb_header_pointer(skb, dataoff, 4, &_hdr);
164 tuple->src.u.sctp.port = hp->source;
165 tuple->dst.u.sctp.port = hp->dest;
169 static bool sctp_invert_tuple(struct nf_conntrack_tuple *tuple,
170 const struct nf_conntrack_tuple *orig)
172 tuple->src.u.sctp.port = orig->dst.u.sctp.port;
173 tuple->dst.u.sctp.port = orig->src.u.sctp.port;
177 /* Print out the per-protocol part of the tuple. */
178 static void sctp_print_tuple(struct seq_file *s,
179 const struct nf_conntrack_tuple *tuple)
181 seq_printf(s, "sport=%hu dport=%hu ",
182 ntohs(tuple->src.u.sctp.port),
183 ntohs(tuple->dst.u.sctp.port));
186 /* Print out the private part of the conntrack. */
187 static void sctp_print_conntrack(struct seq_file *s, struct nf_conn *ct)
189 seq_printf(s, "%s ", sctp_conntrack_names[ct->proto.sctp.state]);
192 #define for_each_sctp_chunk(skb, sch, _sch, offset, dataoff, count) \
193 for ((offset) = (dataoff) + sizeof(sctp_sctphdr_t), (count) = 0; \
194 (offset) < (skb)->len && \
195 ((sch) = skb_header_pointer((skb), (offset), sizeof(_sch), &(_sch))); \
196 (offset) += (ntohs((sch)->length) + 3) & ~3, (count)++)
198 /* Some validity checks to make sure the chunks are fine */
199 static int do_basic_checks(struct nf_conn *ct,
200 const struct sk_buff *skb,
201 unsigned int dataoff,
204 u_int32_t offset, count;
205 sctp_chunkhdr_t _sch, *sch;
210 for_each_sctp_chunk (skb, sch, _sch, offset, dataoff, count) {
211 pr_debug("Chunk Num: %d Type: %d\n", count, sch->type);
213 if (sch->type == SCTP_CID_INIT ||
214 sch->type == SCTP_CID_INIT_ACK ||
215 sch->type == SCTP_CID_SHUTDOWN_COMPLETE)
219 * Cookie Ack/Echo chunks not the first OR
220 * Init / Init Ack / Shutdown compl chunks not the only chunks
223 if (((sch->type == SCTP_CID_COOKIE_ACK ||
224 sch->type == SCTP_CID_COOKIE_ECHO ||
226 count != 0) || !sch->length) {
227 pr_debug("Basic checks failed\n");
232 set_bit(sch->type, map);
235 pr_debug("Basic checks passed\n");
239 static int sctp_new_state(enum ip_conntrack_dir dir,
240 enum sctp_conntrack cur_state,
245 pr_debug("Chunk type: %d\n", chunk_type);
247 switch (chunk_type) {
249 pr_debug("SCTP_CID_INIT\n");
252 case SCTP_CID_INIT_ACK:
253 pr_debug("SCTP_CID_INIT_ACK\n");
257 pr_debug("SCTP_CID_ABORT\n");
260 case SCTP_CID_SHUTDOWN:
261 pr_debug("SCTP_CID_SHUTDOWN\n");
264 case SCTP_CID_SHUTDOWN_ACK:
265 pr_debug("SCTP_CID_SHUTDOWN_ACK\n");
269 pr_debug("SCTP_CID_ERROR\n");
272 case SCTP_CID_COOKIE_ECHO:
273 pr_debug("SCTP_CID_COOKIE_ECHO\n");
276 case SCTP_CID_COOKIE_ACK:
277 pr_debug("SCTP_CID_COOKIE_ACK\n");
280 case SCTP_CID_SHUTDOWN_COMPLETE:
281 pr_debug("SCTP_CID_SHUTDOWN_COMPLETE\n");
284 case SCTP_CID_HEARTBEAT:
285 pr_debug("SCTP_CID_HEARTBEAT");
288 case SCTP_CID_HEARTBEAT_ACK:
289 pr_debug("SCTP_CID_HEARTBEAT_ACK");
293 /* Other chunks like DATA or SACK do not change the state */
294 pr_debug("Unknown chunk type, Will stay in %s\n",
295 sctp_conntrack_names[cur_state]);
299 pr_debug("dir: %d cur_state: %s chunk_type: %d new_state: %s\n",
300 dir, sctp_conntrack_names[cur_state], chunk_type,
301 sctp_conntrack_names[sctp_conntracks[dir][i][cur_state]]);
303 return sctp_conntracks[dir][i][cur_state];
306 static unsigned int *sctp_get_timeouts(struct net *net)
308 return sctp_pernet(net)->timeouts;
311 /* Returns verdict for packet, or -NF_ACCEPT for invalid. */
312 static int sctp_packet(struct nf_conn *ct,
313 const struct sk_buff *skb,
314 unsigned int dataoff,
315 enum ip_conntrack_info ctinfo,
317 unsigned int hooknum,
318 unsigned int *timeouts)
320 enum sctp_conntrack new_state, old_state;
321 enum ip_conntrack_dir dir = CTINFO2DIR(ctinfo);
322 const struct sctphdr *sh;
323 struct sctphdr _sctph;
324 const struct sctp_chunkhdr *sch;
325 struct sctp_chunkhdr _sch;
326 u_int32_t offset, count;
327 unsigned long map[256 / sizeof(unsigned long)] = { 0 };
329 sh = skb_header_pointer(skb, dataoff, sizeof(_sctph), &_sctph);
333 if (do_basic_checks(ct, skb, dataoff, map) != 0)
336 /* Check the verification tag (Sec 8.5) */
337 if (!test_bit(SCTP_CID_INIT, map) &&
338 !test_bit(SCTP_CID_SHUTDOWN_COMPLETE, map) &&
339 !test_bit(SCTP_CID_COOKIE_ECHO, map) &&
340 !test_bit(SCTP_CID_ABORT, map) &&
341 !test_bit(SCTP_CID_SHUTDOWN_ACK, map) &&
342 !test_bit(SCTP_CID_HEARTBEAT, map) &&
343 !test_bit(SCTP_CID_HEARTBEAT_ACK, map) &&
344 sh->vtag != ct->proto.sctp.vtag[dir]) {
345 pr_debug("Verification tag check failed\n");
349 old_state = new_state = SCTP_CONNTRACK_NONE;
350 spin_lock_bh(&ct->lock);
351 for_each_sctp_chunk (skb, sch, _sch, offset, dataoff, count) {
352 /* Special cases of Verification tag check (Sec 8.5.1) */
353 if (sch->type == SCTP_CID_INIT) {
357 } else if (sch->type == SCTP_CID_ABORT) {
359 if (sh->vtag != ct->proto.sctp.vtag[dir] &&
360 sh->vtag != ct->proto.sctp.vtag[!dir])
362 } else if (sch->type == SCTP_CID_SHUTDOWN_COMPLETE) {
364 if (sh->vtag != ct->proto.sctp.vtag[dir] &&
365 sh->vtag != ct->proto.sctp.vtag[!dir] &&
366 sch->flags & SCTP_CHUNK_FLAG_T)
368 } else if (sch->type == SCTP_CID_COOKIE_ECHO) {
370 if (sh->vtag != ct->proto.sctp.vtag[dir])
372 } else if (sch->type == SCTP_CID_HEARTBEAT ||
373 sch->type == SCTP_CID_HEARTBEAT_ACK) {
374 if (ct->proto.sctp.vtag[dir] == 0) {
375 pr_debug("Setting vtag %x for dir %d\n",
377 ct->proto.sctp.vtag[dir] = sh->vtag;
378 } else if (sh->vtag != ct->proto.sctp.vtag[dir]) {
379 pr_debug("Verification tag check failed\n");
384 old_state = ct->proto.sctp.state;
385 new_state = sctp_new_state(dir, old_state, sch->type);
388 if (new_state == SCTP_CONNTRACK_MAX) {
389 pr_debug("nf_conntrack_sctp: Invalid dir=%i ctype=%u "
391 dir, sch->type, old_state);
395 /* If it is an INIT or an INIT ACK note down the vtag */
396 if (sch->type == SCTP_CID_INIT ||
397 sch->type == SCTP_CID_INIT_ACK) {
398 sctp_inithdr_t _inithdr, *ih;
400 ih = skb_header_pointer(skb, offset + sizeof(sctp_chunkhdr_t),
401 sizeof(_inithdr), &_inithdr);
404 pr_debug("Setting vtag %x for dir %d\n",
406 ct->proto.sctp.vtag[!dir] = ih->init_tag;
409 ct->proto.sctp.state = new_state;
410 if (old_state != new_state)
411 nf_conntrack_event_cache(IPCT_PROTOINFO, ct);
413 spin_unlock_bh(&ct->lock);
415 nf_ct_refresh_acct(ct, ctinfo, skb, timeouts[new_state]);
417 if (old_state == SCTP_CONNTRACK_COOKIE_ECHOED &&
418 dir == IP_CT_DIR_REPLY &&
419 new_state == SCTP_CONNTRACK_ESTABLISHED) {
420 pr_debug("Setting assured bit\n");
421 set_bit(IPS_ASSURED_BIT, &ct->status);
422 nf_conntrack_event_cache(IPCT_ASSURED, ct);
428 spin_unlock_bh(&ct->lock);
433 /* Called when a new connection for this protocol found. */
434 static bool sctp_new(struct nf_conn *ct, const struct sk_buff *skb,
435 unsigned int dataoff, unsigned int *timeouts)
437 enum sctp_conntrack new_state;
438 const struct sctphdr *sh;
439 struct sctphdr _sctph;
440 const struct sctp_chunkhdr *sch;
441 struct sctp_chunkhdr _sch;
442 u_int32_t offset, count;
443 unsigned long map[256 / sizeof(unsigned long)] = { 0 };
445 sh = skb_header_pointer(skb, dataoff, sizeof(_sctph), &_sctph);
449 if (do_basic_checks(ct, skb, dataoff, map) != 0)
452 /* If an OOTB packet has any of these chunks discard (Sec 8.4) */
453 if (test_bit(SCTP_CID_ABORT, map) ||
454 test_bit(SCTP_CID_SHUTDOWN_COMPLETE, map) ||
455 test_bit(SCTP_CID_COOKIE_ACK, map))
458 memset(&ct->proto.sctp, 0, sizeof(ct->proto.sctp));
459 new_state = SCTP_CONNTRACK_MAX;
460 for_each_sctp_chunk (skb, sch, _sch, offset, dataoff, count) {
461 /* Don't need lock here: this conntrack not in circulation yet */
462 new_state = sctp_new_state(IP_CT_DIR_ORIGINAL,
463 SCTP_CONNTRACK_NONE, sch->type);
465 /* Invalid: delete conntrack */
466 if (new_state == SCTP_CONNTRACK_NONE ||
467 new_state == SCTP_CONNTRACK_MAX) {
468 pr_debug("nf_conntrack_sctp: invalid new deleting.\n");
472 /* Copy the vtag into the state info */
473 if (sch->type == SCTP_CID_INIT) {
475 sctp_inithdr_t _inithdr, *ih;
477 ih = skb_header_pointer(skb, offset + sizeof(sctp_chunkhdr_t),
478 sizeof(_inithdr), &_inithdr);
482 pr_debug("Setting vtag %x for new conn\n",
485 ct->proto.sctp.vtag[IP_CT_DIR_REPLY] =
491 } else if (sch->type == SCTP_CID_HEARTBEAT) {
492 pr_debug("Setting vtag %x for secondary conntrack\n",
494 ct->proto.sctp.vtag[IP_CT_DIR_ORIGINAL] = sh->vtag;
496 /* If it is a shutdown ack OOTB packet, we expect a return
497 shutdown complete, otherwise an ABORT Sec 8.4 (5) and (8) */
499 pr_debug("Setting vtag %x for new conn OOTB\n",
501 ct->proto.sctp.vtag[IP_CT_DIR_REPLY] = sh->vtag;
504 ct->proto.sctp.state = new_state;
510 static int sctp_error(struct net *net, struct nf_conn *tpl, struct sk_buff *skb,
511 unsigned int dataoff,
512 u8 pf, unsigned int hooknum)
514 const struct sctphdr *sh;
517 if (skb->len < dataoff + sizeof(struct sctphdr)) {
518 logmsg = "nf_ct_sctp: short packet ";
521 if (net->ct.sysctl_checksum && hooknum == NF_INET_PRE_ROUTING &&
522 skb->ip_summed == CHECKSUM_NONE) {
523 if (!skb_make_writable(skb, dataoff + sizeof(struct sctphdr))) {
524 logmsg = "nf_ct_sctp: failed to read header ";
527 sh = (const struct sctphdr *)(skb->data + dataoff);
528 if (sh->checksum != sctp_compute_cksum(skb, dataoff)) {
529 logmsg = "nf_ct_sctp: bad CRC ";
532 skb->ip_summed = CHECKSUM_UNNECESSARY;
536 if (LOG_INVALID(net, IPPROTO_SCTP))
537 nf_log_packet(net, pf, 0, skb, NULL, NULL, NULL, "%s", logmsg);
541 static bool sctp_can_early_drop(const struct nf_conn *ct)
543 switch (ct->proto.sctp.state) {
544 case SCTP_CONNTRACK_SHUTDOWN_SENT:
545 case SCTP_CONNTRACK_SHUTDOWN_RECD:
546 case SCTP_CONNTRACK_SHUTDOWN_ACK_SENT:
555 #if IS_ENABLED(CONFIG_NF_CT_NETLINK)
557 #include <linux/netfilter/nfnetlink.h>
558 #include <linux/netfilter/nfnetlink_conntrack.h>
560 static int sctp_to_nlattr(struct sk_buff *skb, struct nlattr *nla,
563 struct nlattr *nest_parms;
565 spin_lock_bh(&ct->lock);
566 nest_parms = nla_nest_start(skb, CTA_PROTOINFO_SCTP | NLA_F_NESTED);
568 goto nla_put_failure;
570 if (nla_put_u8(skb, CTA_PROTOINFO_SCTP_STATE, ct->proto.sctp.state) ||
571 nla_put_be32(skb, CTA_PROTOINFO_SCTP_VTAG_ORIGINAL,
572 ct->proto.sctp.vtag[IP_CT_DIR_ORIGINAL]) ||
573 nla_put_be32(skb, CTA_PROTOINFO_SCTP_VTAG_REPLY,
574 ct->proto.sctp.vtag[IP_CT_DIR_REPLY]))
575 goto nla_put_failure;
577 spin_unlock_bh(&ct->lock);
579 nla_nest_end(skb, nest_parms);
584 spin_unlock_bh(&ct->lock);
588 static const struct nla_policy sctp_nla_policy[CTA_PROTOINFO_SCTP_MAX+1] = {
589 [CTA_PROTOINFO_SCTP_STATE] = { .type = NLA_U8 },
590 [CTA_PROTOINFO_SCTP_VTAG_ORIGINAL] = { .type = NLA_U32 },
591 [CTA_PROTOINFO_SCTP_VTAG_REPLY] = { .type = NLA_U32 },
594 static int nlattr_to_sctp(struct nlattr *cda[], struct nf_conn *ct)
596 struct nlattr *attr = cda[CTA_PROTOINFO_SCTP];
597 struct nlattr *tb[CTA_PROTOINFO_SCTP_MAX+1];
600 /* updates may not contain the internal protocol info, skip parsing */
604 err = nla_parse_nested(tb, CTA_PROTOINFO_SCTP_MAX, attr,
605 sctp_nla_policy, NULL);
609 if (!tb[CTA_PROTOINFO_SCTP_STATE] ||
610 !tb[CTA_PROTOINFO_SCTP_VTAG_ORIGINAL] ||
611 !tb[CTA_PROTOINFO_SCTP_VTAG_REPLY])
614 spin_lock_bh(&ct->lock);
615 ct->proto.sctp.state = nla_get_u8(tb[CTA_PROTOINFO_SCTP_STATE]);
616 ct->proto.sctp.vtag[IP_CT_DIR_ORIGINAL] =
617 nla_get_be32(tb[CTA_PROTOINFO_SCTP_VTAG_ORIGINAL]);
618 ct->proto.sctp.vtag[IP_CT_DIR_REPLY] =
619 nla_get_be32(tb[CTA_PROTOINFO_SCTP_VTAG_REPLY]);
620 spin_unlock_bh(&ct->lock);
625 static int sctp_nlattr_size(void)
627 return nla_total_size(0) /* CTA_PROTOINFO_SCTP */
628 + nla_policy_len(sctp_nla_policy, CTA_PROTOINFO_SCTP_MAX + 1);
632 #if IS_ENABLED(CONFIG_NF_CT_NETLINK_TIMEOUT)
634 #include <linux/netfilter/nfnetlink.h>
635 #include <linux/netfilter/nfnetlink_cttimeout.h>
637 static int sctp_timeout_nlattr_to_obj(struct nlattr *tb[],
638 struct net *net, void *data)
640 unsigned int *timeouts = data;
641 struct nf_sctp_net *sn = sctp_pernet(net);
644 /* set default SCTP timeouts. */
645 for (i=0; i<SCTP_CONNTRACK_MAX; i++)
646 timeouts[i] = sn->timeouts[i];
648 /* there's a 1:1 mapping between attributes and protocol states. */
649 for (i=CTA_TIMEOUT_SCTP_UNSPEC+1; i<CTA_TIMEOUT_SCTP_MAX+1; i++) {
651 timeouts[i] = ntohl(nla_get_be32(tb[i])) * HZ;
658 sctp_timeout_obj_to_nlattr(struct sk_buff *skb, const void *data)
660 const unsigned int *timeouts = data;
663 for (i=CTA_TIMEOUT_SCTP_UNSPEC+1; i<CTA_TIMEOUT_SCTP_MAX+1; i++) {
664 if (nla_put_be32(skb, i, htonl(timeouts[i] / HZ)))
665 goto nla_put_failure;
673 static const struct nla_policy
674 sctp_timeout_nla_policy[CTA_TIMEOUT_SCTP_MAX+1] = {
675 [CTA_TIMEOUT_SCTP_CLOSED] = { .type = NLA_U32 },
676 [CTA_TIMEOUT_SCTP_COOKIE_WAIT] = { .type = NLA_U32 },
677 [CTA_TIMEOUT_SCTP_COOKIE_ECHOED] = { .type = NLA_U32 },
678 [CTA_TIMEOUT_SCTP_ESTABLISHED] = { .type = NLA_U32 },
679 [CTA_TIMEOUT_SCTP_SHUTDOWN_SENT] = { .type = NLA_U32 },
680 [CTA_TIMEOUT_SCTP_SHUTDOWN_RECD] = { .type = NLA_U32 },
681 [CTA_TIMEOUT_SCTP_SHUTDOWN_ACK_SENT] = { .type = NLA_U32 },
682 [CTA_TIMEOUT_SCTP_HEARTBEAT_SENT] = { .type = NLA_U32 },
683 [CTA_TIMEOUT_SCTP_HEARTBEAT_ACKED] = { .type = NLA_U32 },
685 #endif /* CONFIG_NF_CT_NETLINK_TIMEOUT */
689 static struct ctl_table sctp_sysctl_table[] = {
691 .procname = "nf_conntrack_sctp_timeout_closed",
692 .maxlen = sizeof(unsigned int),
694 .proc_handler = proc_dointvec_jiffies,
697 .procname = "nf_conntrack_sctp_timeout_cookie_wait",
698 .maxlen = sizeof(unsigned int),
700 .proc_handler = proc_dointvec_jiffies,
703 .procname = "nf_conntrack_sctp_timeout_cookie_echoed",
704 .maxlen = sizeof(unsigned int),
706 .proc_handler = proc_dointvec_jiffies,
709 .procname = "nf_conntrack_sctp_timeout_established",
710 .maxlen = sizeof(unsigned int),
712 .proc_handler = proc_dointvec_jiffies,
715 .procname = "nf_conntrack_sctp_timeout_shutdown_sent",
716 .maxlen = sizeof(unsigned int),
718 .proc_handler = proc_dointvec_jiffies,
721 .procname = "nf_conntrack_sctp_timeout_shutdown_recd",
722 .maxlen = sizeof(unsigned int),
724 .proc_handler = proc_dointvec_jiffies,
727 .procname = "nf_conntrack_sctp_timeout_shutdown_ack_sent",
728 .maxlen = sizeof(unsigned int),
730 .proc_handler = proc_dointvec_jiffies,
733 .procname = "nf_conntrack_sctp_timeout_heartbeat_sent",
734 .maxlen = sizeof(unsigned int),
736 .proc_handler = proc_dointvec_jiffies,
739 .procname = "nf_conntrack_sctp_timeout_heartbeat_acked",
740 .maxlen = sizeof(unsigned int),
742 .proc_handler = proc_dointvec_jiffies,
748 static int sctp_kmemdup_sysctl_table(struct nf_proto_net *pn,
749 struct nf_sctp_net *sn)
755 pn->ctl_table = kmemdup(sctp_sysctl_table,
756 sizeof(sctp_sysctl_table),
761 pn->ctl_table[0].data = &sn->timeouts[SCTP_CONNTRACK_CLOSED];
762 pn->ctl_table[1].data = &sn->timeouts[SCTP_CONNTRACK_COOKIE_WAIT];
763 pn->ctl_table[2].data = &sn->timeouts[SCTP_CONNTRACK_COOKIE_ECHOED];
764 pn->ctl_table[3].data = &sn->timeouts[SCTP_CONNTRACK_ESTABLISHED];
765 pn->ctl_table[4].data = &sn->timeouts[SCTP_CONNTRACK_SHUTDOWN_SENT];
766 pn->ctl_table[5].data = &sn->timeouts[SCTP_CONNTRACK_SHUTDOWN_RECD];
767 pn->ctl_table[6].data = &sn->timeouts[SCTP_CONNTRACK_SHUTDOWN_ACK_SENT];
768 pn->ctl_table[7].data = &sn->timeouts[SCTP_CONNTRACK_HEARTBEAT_SENT];
769 pn->ctl_table[8].data = &sn->timeouts[SCTP_CONNTRACK_HEARTBEAT_ACKED];
774 static int sctp_init_net(struct net *net, u_int16_t proto)
776 struct nf_sctp_net *sn = sctp_pernet(net);
777 struct nf_proto_net *pn = &sn->pn;
782 for (i = 0; i < SCTP_CONNTRACK_MAX; i++)
783 sn->timeouts[i] = sctp_timeouts[i];
786 return sctp_kmemdup_sysctl_table(pn, sn);
789 struct nf_conntrack_l4proto nf_conntrack_l4proto_sctp4 __read_mostly = {
791 .l4proto = IPPROTO_SCTP,
793 .pkt_to_tuple = sctp_pkt_to_tuple,
794 .invert_tuple = sctp_invert_tuple,
795 .print_tuple = sctp_print_tuple,
796 .print_conntrack = sctp_print_conntrack,
797 .packet = sctp_packet,
798 .get_timeouts = sctp_get_timeouts,
801 .can_early_drop = sctp_can_early_drop,
803 #if IS_ENABLED(CONFIG_NF_CT_NETLINK)
804 .to_nlattr = sctp_to_nlattr,
805 .nlattr_size = sctp_nlattr_size,
806 .from_nlattr = nlattr_to_sctp,
807 .tuple_to_nlattr = nf_ct_port_tuple_to_nlattr,
808 .nlattr_tuple_size = nf_ct_port_nlattr_tuple_size,
809 .nlattr_to_tuple = nf_ct_port_nlattr_to_tuple,
810 .nla_policy = nf_ct_port_nla_policy,
812 #if IS_ENABLED(CONFIG_NF_CT_NETLINK_TIMEOUT)
814 .nlattr_to_obj = sctp_timeout_nlattr_to_obj,
815 .obj_to_nlattr = sctp_timeout_obj_to_nlattr,
816 .nlattr_max = CTA_TIMEOUT_SCTP_MAX,
817 .obj_size = sizeof(unsigned int) * SCTP_CONNTRACK_MAX,
818 .nla_policy = sctp_timeout_nla_policy,
820 #endif /* CONFIG_NF_CT_NETLINK_TIMEOUT */
821 .init_net = sctp_init_net,
823 EXPORT_SYMBOL_GPL(nf_conntrack_l4proto_sctp4);
825 struct nf_conntrack_l4proto nf_conntrack_l4proto_sctp6 __read_mostly = {
827 .l4proto = IPPROTO_SCTP,
829 .pkt_to_tuple = sctp_pkt_to_tuple,
830 .invert_tuple = sctp_invert_tuple,
831 .print_tuple = sctp_print_tuple,
832 .print_conntrack = sctp_print_conntrack,
833 .packet = sctp_packet,
834 .get_timeouts = sctp_get_timeouts,
837 .can_early_drop = sctp_can_early_drop,
839 #if IS_ENABLED(CONFIG_NF_CT_NETLINK)
840 .to_nlattr = sctp_to_nlattr,
841 .nlattr_size = sctp_nlattr_size,
842 .from_nlattr = nlattr_to_sctp,
843 .tuple_to_nlattr = nf_ct_port_tuple_to_nlattr,
844 .nlattr_tuple_size = nf_ct_port_nlattr_tuple_size,
845 .nlattr_to_tuple = nf_ct_port_nlattr_to_tuple,
846 .nla_policy = nf_ct_port_nla_policy,
847 #if IS_ENABLED(CONFIG_NF_CT_NETLINK_TIMEOUT)
849 .nlattr_to_obj = sctp_timeout_nlattr_to_obj,
850 .obj_to_nlattr = sctp_timeout_obj_to_nlattr,
851 .nlattr_max = CTA_TIMEOUT_SCTP_MAX,
852 .obj_size = sizeof(unsigned int) * SCTP_CONNTRACK_MAX,
853 .nla_policy = sctp_timeout_nla_policy,
855 #endif /* CONFIG_NF_CT_NETLINK_TIMEOUT */
857 .init_net = sctp_init_net,
859 EXPORT_SYMBOL_GPL(nf_conntrack_l4proto_sctp6);