1 /*
2  * Connection tracking protocol helper module for SCTP.
3  *
4  * Copyright (c) 2004 Kiran Kumar Immidi <immidi_kiran@yahoo.com>
5  * Copyright (c) 2004-2012 Patrick McHardy <kaber@trash.net>
6  *
7  * SCTP is defined in RFC 2960. References to various sections in this code
8  * are to this RFC.
9  *
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.
13  */
14 
15 #include <linux/types.h>
16 #include <linux/timer.h>
17 #include <linux/netfilter.h>
18 #include <linux/in.h>
19 #include <linux/ip.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>
26 
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>
32 
33 /* FIXME: Examine ipfilter's timeouts and conntrack transitions more
34    closely.  They're more complex. --RR
35 
36    And so for me for SCTP :D -Kiran */
37 
38 static const char *const sctp_conntrack_names[] = {
39 	"NONE",
40 	"CLOSED",
41 	"COOKIE_WAIT",
42 	"COOKIE_ECHOED",
43 	"ESTABLISHED",
44 	"SHUTDOWN_SENT",
45 	"SHUTDOWN_RECD",
46 	"SHUTDOWN_ACK_SENT",
47 	"HEARTBEAT_SENT",
48 	"HEARTBEAT_ACKED",
49 };
50 
51 #define SECS  * HZ
52 #define MINS  * 60 SECS
53 #define HOURS * 60 MINS
54 #define DAYS  * 24 HOURS
55 
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,
66 };
67 
68 #define sNO SCTP_CONNTRACK_NONE
69 #define	sCL SCTP_CONNTRACK_CLOSED
70 #define	sCW SCTP_CONNTRACK_COOKIE_WAIT
71 #define	sCE SCTP_CONNTRACK_COOKIE_ECHOED
72 #define	sES SCTP_CONNTRACK_ESTABLISHED
73 #define	sSS SCTP_CONNTRACK_SHUTDOWN_SENT
74 #define	sSR SCTP_CONNTRACK_SHUTDOWN_RECD
75 #define	sSA SCTP_CONNTRACK_SHUTDOWN_ACK_SENT
76 #define	sHS SCTP_CONNTRACK_HEARTBEAT_SENT
77 #define	sHA SCTP_CONNTRACK_HEARTBEAT_ACKED
78 #define	sIV SCTP_CONNTRACK_MAX
79 
80 /*
81 	These are the descriptions of the states:
82 
83 NOTE: These state names are tantalizingly similar to the states of an
84 SCTP endpoint. But the interpretation of the states is a little different,
85 considering that these are the states of the connection and not of an end
86 point. Please note the subtleties. -Kiran
87 
88 NONE              - Nothing so far.
89 COOKIE WAIT       - We have seen an INIT chunk in the original direction, or also
90 		    an INIT_ACK chunk in the reply direction.
91 COOKIE ECHOED     - We have seen a COOKIE_ECHO chunk in the original direction.
92 ESTABLISHED       - We have seen a COOKIE_ACK in the reply direction.
93 SHUTDOWN_SENT     - We have seen a SHUTDOWN chunk in the original direction.
94 SHUTDOWN_RECD     - We have seen a SHUTDOWN chunk in the reply directoin.
95 SHUTDOWN_ACK_SENT - We have seen a SHUTDOWN_ACK chunk in the direction opposite
96 		    to that of the SHUTDOWN chunk.
97 CLOSED            - We have seen a SHUTDOWN_COMPLETE chunk in the direction of
98 		    the SHUTDOWN chunk. Connection is closed.
99 HEARTBEAT_SENT    - We have seen a HEARTBEAT in a new flow.
100 HEARTBEAT_ACKED   - We have seen a HEARTBEAT-ACK in the direction opposite to
101 		    that of the HEARTBEAT chunk. Secondary connection is
102 		    established.
103 */
104 
105 /* TODO
106  - I have assumed that the first INIT is in the original direction.
107  This messes things when an INIT comes in the reply direction in CLOSED
108  state.
109  - Check the error type in the reply dir before transitioning from
110 cookie echoed to closed.
111  - Sec 5.2.4 of RFC 2960
112  - Full Multi Homing support.
113 */
114 
115 /* SCTP conntrack state transitions */
116 static const u8 sctp_conntracks[2][11][SCTP_CONNTRACK_MAX] = {
117 	{
118 /*	ORIGINAL	*/
119 /*                  sNO, sCL, sCW, sCE, sES, sSS, sSR, sSA, sHS, sHA */
120 /* init         */ {sCW, sCW, sCW, sCE, sES, sSS, sSR, sSA, sCW, sHA},
121 /* init_ack     */ {sCL, sCL, sCW, sCE, sES, sSS, sSR, sSA, sCL, sHA},
122 /* abort        */ {sCL, sCL, sCL, sCL, sCL, sCL, sCL, sCL, sCL, sCL},
123 /* shutdown     */ {sCL, sCL, sCW, sCE, sSS, sSS, sSR, sSA, sCL, sSS},
124 /* shutdown_ack */ {sSA, sCL, sCW, sCE, sES, sSA, sSA, sSA, sSA, sHA},
125 /* error        */ {sCL, sCL, sCW, sCE, sES, sSS, sSR, sSA, sCL, sHA},/* Can't have Stale cookie*/
126 /* cookie_echo  */ {sCL, sCL, sCE, sCE, sES, sSS, sSR, sSA, sCL, sHA},/* 5.2.4 - Big TODO */
127 /* cookie_ack   */ {sCL, sCL, sCW, sCE, sES, sSS, sSR, sSA, sCL, sHA},/* Can't come in orig dir */
128 /* shutdown_comp*/ {sCL, sCL, sCW, sCE, sES, sSS, sSR, sCL, sCL, sHA},
129 /* heartbeat    */ {sHS, sCL, sCW, sCE, sES, sSS, sSR, sSA, sHS, sHA},
130 /* heartbeat_ack*/ {sCL, sCL, sCW, sCE, sES, sSS, sSR, sSA, sHS, sHA}
131 	},
132 	{
133 /*	REPLY	*/
134 /*                  sNO, sCL, sCW, sCE, sES, sSS, sSR, sSA, sHS, sHA */
135 /* init         */ {sIV, sCL, sCW, sCE, sES, sSS, sSR, sSA, sIV, sHA},/* INIT in sCL Big TODO */
136 /* init_ack     */ {sIV, sCL, sCW, sCE, sES, sSS, sSR, sSA, sIV, sHA},
137 /* abort        */ {sIV, sCL, sCL, sCL, sCL, sCL, sCL, sCL, sIV, sCL},
138 /* shutdown     */ {sIV, sCL, sCW, sCE, sSR, sSS, sSR, sSA, sIV, sSR},
139 /* shutdown_ack */ {sIV, sCL, sCW, sCE, sES, sSA, sSA, sSA, sIV, sHA},
140 /* error        */ {sIV, sCL, sCW, sCL, sES, sSS, sSR, sSA, sIV, sHA},
141 /* cookie_echo  */ {sIV, sCL, sCW, sCE, sES, sSS, sSR, sSA, sIV, sHA},/* Can't come in reply dir */
142 /* cookie_ack   */ {sIV, sCL, sCW, sES, sES, sSS, sSR, sSA, sIV, sHA},
143 /* shutdown_comp*/ {sIV, sCL, sCW, sCE, sES, sSS, sSR, sCL, sIV, sHA},
144 /* heartbeat    */ {sIV, sCL, sCW, sCE, sES, sSS, sSR, sSA, sHS, sHA},
145 /* heartbeat_ack*/ {sIV, sCL, sCW, sCE, sES, sSS, sSR, sSA, sHA, sHA}
146 	}
147 };
148 
149 static inline struct nf_sctp_net *sctp_pernet(struct net *net)
150 {
151 	return &net->ct.nf_ct_proto.sctp;
152 }
153 
154 #ifdef CONFIG_NF_CONNTRACK_PROCFS
155 /* Print out the private part of the conntrack. */
156 static void sctp_print_conntrack(struct seq_file *s, struct nf_conn *ct)
157 {
158 	seq_printf(s, "%s ", sctp_conntrack_names[ct->proto.sctp.state]);
159 }
160 #endif
161 
162 #define for_each_sctp_chunk(skb, sch, _sch, offset, dataoff, count)	\
163 for ((offset) = (dataoff) + sizeof(struct sctphdr), (count) = 0;	\
164 	(offset) < (skb)->len &&					\
165 	((sch) = skb_header_pointer((skb), (offset), sizeof(_sch), &(_sch)));	\
166 	(offset) += (ntohs((sch)->length) + 3) & ~3, (count)++)
167 
168 /* Some validity checks to make sure the chunks are fine */
169 static int do_basic_checks(struct nf_conn *ct,
170 			   const struct sk_buff *skb,
171 			   unsigned int dataoff,
172 			   unsigned long *map)
173 {
174 	u_int32_t offset, count;
175 	struct sctp_chunkhdr _sch, *sch;
176 	int flag;
177 
178 	flag = 0;
179 
180 	for_each_sctp_chunk (skb, sch, _sch, offset, dataoff, count) {
181 		pr_debug("Chunk Num: %d  Type: %d\n", count, sch->type);
182 
183 		if (sch->type == SCTP_CID_INIT ||
184 		    sch->type == SCTP_CID_INIT_ACK ||
185 		    sch->type == SCTP_CID_SHUTDOWN_COMPLETE)
186 			flag = 1;
187 
188 		/*
189 		 * Cookie Ack/Echo chunks not the first OR
190 		 * Init / Init Ack / Shutdown compl chunks not the only chunks
191 		 * OR zero-length.
192 		 */
193 		if (((sch->type == SCTP_CID_COOKIE_ACK ||
194 		      sch->type == SCTP_CID_COOKIE_ECHO ||
195 		      flag) &&
196 		     count != 0) || !sch->length) {
197 			pr_debug("Basic checks failed\n");
198 			return 1;
199 		}
200 
201 		if (map)
202 			set_bit(sch->type, map);
203 	}
204 
205 	pr_debug("Basic checks passed\n");
206 	return count == 0;
207 }
208 
209 static int sctp_new_state(enum ip_conntrack_dir dir,
210 			  enum sctp_conntrack cur_state,
211 			  int chunk_type)
212 {
213 	int i;
214 
215 	pr_debug("Chunk type: %d\n", chunk_type);
216 
217 	switch (chunk_type) {
218 	case SCTP_CID_INIT:
219 		pr_debug("SCTP_CID_INIT\n");
220 		i = 0;
221 		break;
222 	case SCTP_CID_INIT_ACK:
223 		pr_debug("SCTP_CID_INIT_ACK\n");
224 		i = 1;
225 		break;
226 	case SCTP_CID_ABORT:
227 		pr_debug("SCTP_CID_ABORT\n");
228 		i = 2;
229 		break;
230 	case SCTP_CID_SHUTDOWN:
231 		pr_debug("SCTP_CID_SHUTDOWN\n");
232 		i = 3;
233 		break;
234 	case SCTP_CID_SHUTDOWN_ACK:
235 		pr_debug("SCTP_CID_SHUTDOWN_ACK\n");
236 		i = 4;
237 		break;
238 	case SCTP_CID_ERROR:
239 		pr_debug("SCTP_CID_ERROR\n");
240 		i = 5;
241 		break;
242 	case SCTP_CID_COOKIE_ECHO:
243 		pr_debug("SCTP_CID_COOKIE_ECHO\n");
244 		i = 6;
245 		break;
246 	case SCTP_CID_COOKIE_ACK:
247 		pr_debug("SCTP_CID_COOKIE_ACK\n");
248 		i = 7;
249 		break;
250 	case SCTP_CID_SHUTDOWN_COMPLETE:
251 		pr_debug("SCTP_CID_SHUTDOWN_COMPLETE\n");
252 		i = 8;
253 		break;
254 	case SCTP_CID_HEARTBEAT:
255 		pr_debug("SCTP_CID_HEARTBEAT");
256 		i = 9;
257 		break;
258 	case SCTP_CID_HEARTBEAT_ACK:
259 		pr_debug("SCTP_CID_HEARTBEAT_ACK");
260 		i = 10;
261 		break;
262 	default:
263 		/* Other chunks like DATA or SACK do not change the state */
264 		pr_debug("Unknown chunk type, Will stay in %s\n",
265 			 sctp_conntrack_names[cur_state]);
266 		return cur_state;
267 	}
268 
269 	pr_debug("dir: %d   cur_state: %s  chunk_type: %d  new_state: %s\n",
270 		 dir, sctp_conntrack_names[cur_state], chunk_type,
271 		 sctp_conntrack_names[sctp_conntracks[dir][i][cur_state]]);
272 
273 	return sctp_conntracks[dir][i][cur_state];
274 }
275 
276 /* Don't need lock here: this conntrack not in circulation yet */
277 static noinline bool
278 sctp_new(struct nf_conn *ct, const struct sk_buff *skb,
279 	 const struct sctphdr *sh, unsigned int dataoff)
280 {
281 	enum sctp_conntrack new_state;
282 	const struct sctp_chunkhdr *sch;
283 	struct sctp_chunkhdr _sch;
284 	u32 offset, count;
285 
286 	memset(&ct->proto.sctp, 0, sizeof(ct->proto.sctp));
287 	new_state = SCTP_CONNTRACK_MAX;
288 	for_each_sctp_chunk(skb, sch, _sch, offset, dataoff, count) {
289 		new_state = sctp_new_state(IP_CT_DIR_ORIGINAL,
290 					   SCTP_CONNTRACK_NONE, sch->type);
291 
292 		/* Invalid: delete conntrack */
293 		if (new_state == SCTP_CONNTRACK_NONE ||
294 		    new_state == SCTP_CONNTRACK_MAX) {
295 			pr_debug("nf_conntrack_sctp: invalid new deleting.\n");
296 			return false;
297 		}
298 
299 		/* Copy the vtag into the state info */
300 		if (sch->type == SCTP_CID_INIT) {
301 			struct sctp_inithdr _inithdr, *ih;
302 			/* Sec 8.5.1 (A) */
303 			if (sh->vtag)
304 				return false;
305 
306 			ih = skb_header_pointer(skb, offset + sizeof(_sch),
307 						sizeof(_inithdr), &_inithdr);
308 			if (!ih)
309 				return false;
310 
311 			pr_debug("Setting vtag %x for new conn\n",
312 				 ih->init_tag);
313 
314 			ct->proto.sctp.vtag[IP_CT_DIR_REPLY] = ih->init_tag;
315 		} else if (sch->type == SCTP_CID_HEARTBEAT) {
316 			pr_debug("Setting vtag %x for secondary conntrack\n",
317 				 sh->vtag);
318 			ct->proto.sctp.vtag[IP_CT_DIR_ORIGINAL] = sh->vtag;
319 		} else {
320 		/* If it is a shutdown ack OOTB packet, we expect a return
321 		   shutdown complete, otherwise an ABORT Sec 8.4 (5) and (8) */
322 			pr_debug("Setting vtag %x for new conn OOTB\n",
323 				 sh->vtag);
324 			ct->proto.sctp.vtag[IP_CT_DIR_REPLY] = sh->vtag;
325 		}
326 
327 		ct->proto.sctp.state = new_state;
328 	}
329 
330 	return true;
331 }
332 
333 static bool sctp_error(struct sk_buff *skb,
334 		       unsigned int dataoff,
335 		       const struct nf_hook_state *state)
336 {
337 	const struct sctphdr *sh;
338 	const char *logmsg;
339 
340 	if (skb->len < dataoff + sizeof(struct sctphdr)) {
341 		logmsg = "nf_ct_sctp: short packet ";
342 		goto out_invalid;
343 	}
344 	if (state->hook == NF_INET_PRE_ROUTING &&
345 	    state->net->ct.sysctl_checksum &&
346 	    skb->ip_summed == CHECKSUM_NONE) {
347 		if (!skb_make_writable(skb, dataoff + sizeof(struct sctphdr))) {
348 			logmsg = "nf_ct_sctp: failed to read header ";
349 			goto out_invalid;
350 		}
351 		sh = (const struct sctphdr *)(skb->data + dataoff);
352 		if (sh->checksum != sctp_compute_cksum(skb, dataoff)) {
353 			logmsg = "nf_ct_sctp: bad CRC ";
354 			goto out_invalid;
355 		}
356 		skb->ip_summed = CHECKSUM_UNNECESSARY;
357 	}
358 	return false;
359 out_invalid:
360 	nf_l4proto_log_invalid(skb, state->net, state->pf, IPPROTO_SCTP, "%s", logmsg);
361 	return true;
362 }
363 
364 /* Returns verdict for packet, or -NF_ACCEPT for invalid. */
365 static int sctp_packet(struct nf_conn *ct,
366 		       struct sk_buff *skb,
367 		       unsigned int dataoff,
368 		       enum ip_conntrack_info ctinfo,
369 		       const struct nf_hook_state *state)
370 {
371 	enum sctp_conntrack new_state, old_state;
372 	enum ip_conntrack_dir dir = CTINFO2DIR(ctinfo);
373 	const struct sctphdr *sh;
374 	struct sctphdr _sctph;
375 	const struct sctp_chunkhdr *sch;
376 	struct sctp_chunkhdr _sch;
377 	u_int32_t offset, count;
378 	unsigned int *timeouts;
379 	unsigned long map[256 / sizeof(unsigned long)] = { 0 };
380 
381 	if (sctp_error(skb, dataoff, state))
382 		return -NF_ACCEPT;
383 
384 	sh = skb_header_pointer(skb, dataoff, sizeof(_sctph), &_sctph);
385 	if (sh == NULL)
386 		goto out;
387 
388 	if (do_basic_checks(ct, skb, dataoff, map) != 0)
389 		goto out;
390 
391 	if (!nf_ct_is_confirmed(ct)) {
392 		/* If an OOTB packet has any of these chunks discard (Sec 8.4) */
393 		if (test_bit(SCTP_CID_ABORT, map) ||
394 		    test_bit(SCTP_CID_SHUTDOWN_COMPLETE, map) ||
395 		    test_bit(SCTP_CID_COOKIE_ACK, map))
396 			return -NF_ACCEPT;
397 
398 		if (!sctp_new(ct, skb, sh, dataoff))
399 			return -NF_ACCEPT;
400 	}
401 
402 	/* Check the verification tag (Sec 8.5) */
403 	if (!test_bit(SCTP_CID_INIT, map) &&
404 	    !test_bit(SCTP_CID_SHUTDOWN_COMPLETE, map) &&
405 	    !test_bit(SCTP_CID_COOKIE_ECHO, map) &&
406 	    !test_bit(SCTP_CID_ABORT, map) &&
407 	    !test_bit(SCTP_CID_SHUTDOWN_ACK, map) &&
408 	    !test_bit(SCTP_CID_HEARTBEAT, map) &&
409 	    !test_bit(SCTP_CID_HEARTBEAT_ACK, map) &&
410 	    sh->vtag != ct->proto.sctp.vtag[dir]) {
411 		pr_debug("Verification tag check failed\n");
412 		goto out;
413 	}
414 
415 	old_state = new_state = SCTP_CONNTRACK_NONE;
416 	spin_lock_bh(&ct->lock);
417 	for_each_sctp_chunk (skb, sch, _sch, offset, dataoff, count) {
418 		/* Special cases of Verification tag check (Sec 8.5.1) */
419 		if (sch->type == SCTP_CID_INIT) {
420 			/* Sec 8.5.1 (A) */
421 			if (sh->vtag != 0)
422 				goto out_unlock;
423 		} else if (sch->type == SCTP_CID_ABORT) {
424 			/* Sec 8.5.1 (B) */
425 			if (sh->vtag != ct->proto.sctp.vtag[dir] &&
426 			    sh->vtag != ct->proto.sctp.vtag[!dir])
427 				goto out_unlock;
428 		} else if (sch->type == SCTP_CID_SHUTDOWN_COMPLETE) {
429 			/* Sec 8.5.1 (C) */
430 			if (sh->vtag != ct->proto.sctp.vtag[dir] &&
431 			    sh->vtag != ct->proto.sctp.vtag[!dir] &&
432 			    sch->flags & SCTP_CHUNK_FLAG_T)
433 				goto out_unlock;
434 		} else if (sch->type == SCTP_CID_COOKIE_ECHO) {
435 			/* Sec 8.5.1 (D) */
436 			if (sh->vtag != ct->proto.sctp.vtag[dir])
437 				goto out_unlock;
438 		} else if (sch->type == SCTP_CID_HEARTBEAT ||
439 			   sch->type == SCTP_CID_HEARTBEAT_ACK) {
440 			if (ct->proto.sctp.vtag[dir] == 0) {
441 				pr_debug("Setting vtag %x for dir %d\n",
442 					 sh->vtag, dir);
443 				ct->proto.sctp.vtag[dir] = sh->vtag;
444 			} else if (sh->vtag != ct->proto.sctp.vtag[dir]) {
445 				pr_debug("Verification tag check failed\n");
446 				goto out_unlock;
447 			}
448 		}
449 
450 		old_state = ct->proto.sctp.state;
451 		new_state = sctp_new_state(dir, old_state, sch->type);
452 
453 		/* Invalid */
454 		if (new_state == SCTP_CONNTRACK_MAX) {
455 			pr_debug("nf_conntrack_sctp: Invalid dir=%i ctype=%u "
456 				 "conntrack=%u\n",
457 				 dir, sch->type, old_state);
458 			goto out_unlock;
459 		}
460 
461 		/* If it is an INIT or an INIT ACK note down the vtag */
462 		if (sch->type == SCTP_CID_INIT ||
463 		    sch->type == SCTP_CID_INIT_ACK) {
464 			struct sctp_inithdr _inithdr, *ih;
465 
466 			ih = skb_header_pointer(skb, offset + sizeof(_sch),
467 						sizeof(_inithdr), &_inithdr);
468 			if (ih == NULL)
469 				goto out_unlock;
470 			pr_debug("Setting vtag %x for dir %d\n",
471 				 ih->init_tag, !dir);
472 			ct->proto.sctp.vtag[!dir] = ih->init_tag;
473 		}
474 
475 		ct->proto.sctp.state = new_state;
476 		if (old_state != new_state)
477 			nf_conntrack_event_cache(IPCT_PROTOINFO, ct);
478 	}
479 	spin_unlock_bh(&ct->lock);
480 
481 	timeouts = nf_ct_timeout_lookup(ct);
482 	if (!timeouts)
483 		timeouts = sctp_pernet(nf_ct_net(ct))->timeouts;
484 
485 	nf_ct_refresh_acct(ct, ctinfo, skb, timeouts[new_state]);
486 
487 	if (old_state == SCTP_CONNTRACK_COOKIE_ECHOED &&
488 	    dir == IP_CT_DIR_REPLY &&
489 	    new_state == SCTP_CONNTRACK_ESTABLISHED) {
490 		pr_debug("Setting assured bit\n");
491 		set_bit(IPS_ASSURED_BIT, &ct->status);
492 		nf_conntrack_event_cache(IPCT_ASSURED, ct);
493 	}
494 
495 	return NF_ACCEPT;
496 
497 out_unlock:
498 	spin_unlock_bh(&ct->lock);
499 out:
500 	return -NF_ACCEPT;
501 }
502 
503 static bool sctp_can_early_drop(const struct nf_conn *ct)
504 {
505 	switch (ct->proto.sctp.state) {
506 	case SCTP_CONNTRACK_SHUTDOWN_SENT:
507 	case SCTP_CONNTRACK_SHUTDOWN_RECD:
508 	case SCTP_CONNTRACK_SHUTDOWN_ACK_SENT:
509 		return true;
510 	default:
511 		break;
512 	}
513 
514 	return false;
515 }
516 
517 #if IS_ENABLED(CONFIG_NF_CT_NETLINK)
518 
519 #include <linux/netfilter/nfnetlink.h>
520 #include <linux/netfilter/nfnetlink_conntrack.h>
521 
522 static int sctp_to_nlattr(struct sk_buff *skb, struct nlattr *nla,
523 			  struct nf_conn *ct)
524 {
525 	struct nlattr *nest_parms;
526 
527 	spin_lock_bh(&ct->lock);
528 	nest_parms = nla_nest_start(skb, CTA_PROTOINFO_SCTP | NLA_F_NESTED);
529 	if (!nest_parms)
530 		goto nla_put_failure;
531 
532 	if (nla_put_u8(skb, CTA_PROTOINFO_SCTP_STATE, ct->proto.sctp.state) ||
533 	    nla_put_be32(skb, CTA_PROTOINFO_SCTP_VTAG_ORIGINAL,
534 			 ct->proto.sctp.vtag[IP_CT_DIR_ORIGINAL]) ||
535 	    nla_put_be32(skb, CTA_PROTOINFO_SCTP_VTAG_REPLY,
536 			 ct->proto.sctp.vtag[IP_CT_DIR_REPLY]))
537 		goto nla_put_failure;
538 
539 	spin_unlock_bh(&ct->lock);
540 
541 	nla_nest_end(skb, nest_parms);
542 
543 	return 0;
544 
545 nla_put_failure:
546 	spin_unlock_bh(&ct->lock);
547 	return -1;
548 }
549 
550 static const struct nla_policy sctp_nla_policy[CTA_PROTOINFO_SCTP_MAX+1] = {
551 	[CTA_PROTOINFO_SCTP_STATE]	    = { .type = NLA_U8 },
552 	[CTA_PROTOINFO_SCTP_VTAG_ORIGINAL]  = { .type = NLA_U32 },
553 	[CTA_PROTOINFO_SCTP_VTAG_REPLY]     = { .type = NLA_U32 },
554 };
555 
556 #define SCTP_NLATTR_SIZE ( \
557 		NLA_ALIGN(NLA_HDRLEN + 1) + \
558 		NLA_ALIGN(NLA_HDRLEN + 4) + \
559 		NLA_ALIGN(NLA_HDRLEN + 4))
560 
561 static int nlattr_to_sctp(struct nlattr *cda[], struct nf_conn *ct)
562 {
563 	struct nlattr *attr = cda[CTA_PROTOINFO_SCTP];
564 	struct nlattr *tb[CTA_PROTOINFO_SCTP_MAX+1];
565 	int err;
566 
567 	/* updates may not contain the internal protocol info, skip parsing */
568 	if (!attr)
569 		return 0;
570 
571 	err = nla_parse_nested(tb, CTA_PROTOINFO_SCTP_MAX, attr,
572 			       sctp_nla_policy, NULL);
573 	if (err < 0)
574 		return err;
575 
576 	if (!tb[CTA_PROTOINFO_SCTP_STATE] ||
577 	    !tb[CTA_PROTOINFO_SCTP_VTAG_ORIGINAL] ||
578 	    !tb[CTA_PROTOINFO_SCTP_VTAG_REPLY])
579 		return -EINVAL;
580 
581 	spin_lock_bh(&ct->lock);
582 	ct->proto.sctp.state = nla_get_u8(tb[CTA_PROTOINFO_SCTP_STATE]);
583 	ct->proto.sctp.vtag[IP_CT_DIR_ORIGINAL] =
584 		nla_get_be32(tb[CTA_PROTOINFO_SCTP_VTAG_ORIGINAL]);
585 	ct->proto.sctp.vtag[IP_CT_DIR_REPLY] =
586 		nla_get_be32(tb[CTA_PROTOINFO_SCTP_VTAG_REPLY]);
587 	spin_unlock_bh(&ct->lock);
588 
589 	return 0;
590 }
591 #endif
592 
593 #ifdef CONFIG_NF_CONNTRACK_TIMEOUT
594 
595 #include <linux/netfilter/nfnetlink.h>
596 #include <linux/netfilter/nfnetlink_cttimeout.h>
597 
598 static int sctp_timeout_nlattr_to_obj(struct nlattr *tb[],
599 				      struct net *net, void *data)
600 {
601 	unsigned int *timeouts = data;
602 	struct nf_sctp_net *sn = sctp_pernet(net);
603 	int i;
604 
605 	/* set default SCTP timeouts. */
606 	for (i=0; i<SCTP_CONNTRACK_MAX; i++)
607 		timeouts[i] = sn->timeouts[i];
608 
609 	/* there's a 1:1 mapping between attributes and protocol states. */
610 	for (i=CTA_TIMEOUT_SCTP_UNSPEC+1; i<CTA_TIMEOUT_SCTP_MAX+1; i++) {
611 		if (tb[i]) {
612 			timeouts[i] = ntohl(nla_get_be32(tb[i])) * HZ;
613 		}
614 	}
615 
616 	timeouts[CTA_TIMEOUT_SCTP_UNSPEC] = timeouts[CTA_TIMEOUT_SCTP_CLOSED];
617 	return 0;
618 }
619 
620 static int
621 sctp_timeout_obj_to_nlattr(struct sk_buff *skb, const void *data)
622 {
623         const unsigned int *timeouts = data;
624 	int i;
625 
626 	for (i=CTA_TIMEOUT_SCTP_UNSPEC+1; i<CTA_TIMEOUT_SCTP_MAX+1; i++) {
627 	        if (nla_put_be32(skb, i, htonl(timeouts[i] / HZ)))
628 			goto nla_put_failure;
629 	}
630         return 0;
631 
632 nla_put_failure:
633         return -ENOSPC;
634 }
635 
636 static const struct nla_policy
637 sctp_timeout_nla_policy[CTA_TIMEOUT_SCTP_MAX+1] = {
638 	[CTA_TIMEOUT_SCTP_CLOSED]		= { .type = NLA_U32 },
639 	[CTA_TIMEOUT_SCTP_COOKIE_WAIT]		= { .type = NLA_U32 },
640 	[CTA_TIMEOUT_SCTP_COOKIE_ECHOED]	= { .type = NLA_U32 },
641 	[CTA_TIMEOUT_SCTP_ESTABLISHED]		= { .type = NLA_U32 },
642 	[CTA_TIMEOUT_SCTP_SHUTDOWN_SENT]	= { .type = NLA_U32 },
643 	[CTA_TIMEOUT_SCTP_SHUTDOWN_RECD]	= { .type = NLA_U32 },
644 	[CTA_TIMEOUT_SCTP_SHUTDOWN_ACK_SENT]	= { .type = NLA_U32 },
645 	[CTA_TIMEOUT_SCTP_HEARTBEAT_SENT]	= { .type = NLA_U32 },
646 	[CTA_TIMEOUT_SCTP_HEARTBEAT_ACKED]	= { .type = NLA_U32 },
647 };
648 #endif /* CONFIG_NF_CONNTRACK_TIMEOUT */
649 
650 
651 #ifdef CONFIG_SYSCTL
652 static struct ctl_table sctp_sysctl_table[] = {
653 	{
654 		.procname	= "nf_conntrack_sctp_timeout_closed",
655 		.maxlen		= sizeof(unsigned int),
656 		.mode		= 0644,
657 		.proc_handler	= proc_dointvec_jiffies,
658 	},
659 	{
660 		.procname	= "nf_conntrack_sctp_timeout_cookie_wait",
661 		.maxlen		= sizeof(unsigned int),
662 		.mode		= 0644,
663 		.proc_handler	= proc_dointvec_jiffies,
664 	},
665 	{
666 		.procname	= "nf_conntrack_sctp_timeout_cookie_echoed",
667 		.maxlen		= sizeof(unsigned int),
668 		.mode		= 0644,
669 		.proc_handler	= proc_dointvec_jiffies,
670 	},
671 	{
672 		.procname	= "nf_conntrack_sctp_timeout_established",
673 		.maxlen		= sizeof(unsigned int),
674 		.mode		= 0644,
675 		.proc_handler	= proc_dointvec_jiffies,
676 	},
677 	{
678 		.procname	= "nf_conntrack_sctp_timeout_shutdown_sent",
679 		.maxlen		= sizeof(unsigned int),
680 		.mode		= 0644,
681 		.proc_handler	= proc_dointvec_jiffies,
682 	},
683 	{
684 		.procname	= "nf_conntrack_sctp_timeout_shutdown_recd",
685 		.maxlen		= sizeof(unsigned int),
686 		.mode		= 0644,
687 		.proc_handler	= proc_dointvec_jiffies,
688 	},
689 	{
690 		.procname	= "nf_conntrack_sctp_timeout_shutdown_ack_sent",
691 		.maxlen		= sizeof(unsigned int),
692 		.mode		= 0644,
693 		.proc_handler	= proc_dointvec_jiffies,
694 	},
695 	{
696 		.procname	= "nf_conntrack_sctp_timeout_heartbeat_sent",
697 		.maxlen		= sizeof(unsigned int),
698 		.mode		= 0644,
699 		.proc_handler	= proc_dointvec_jiffies,
700 	},
701 	{
702 		.procname	= "nf_conntrack_sctp_timeout_heartbeat_acked",
703 		.maxlen		= sizeof(unsigned int),
704 		.mode		= 0644,
705 		.proc_handler	= proc_dointvec_jiffies,
706 	},
707 	{ }
708 };
709 #endif
710 
711 static int sctp_kmemdup_sysctl_table(struct nf_proto_net *pn,
712 				     struct nf_sctp_net *sn)
713 {
714 #ifdef CONFIG_SYSCTL
715 	if (pn->ctl_table)
716 		return 0;
717 
718 	pn->ctl_table = kmemdup(sctp_sysctl_table,
719 				sizeof(sctp_sysctl_table),
720 				GFP_KERNEL);
721 	if (!pn->ctl_table)
722 		return -ENOMEM;
723 
724 	pn->ctl_table[0].data = &sn->timeouts[SCTP_CONNTRACK_CLOSED];
725 	pn->ctl_table[1].data = &sn->timeouts[SCTP_CONNTRACK_COOKIE_WAIT];
726 	pn->ctl_table[2].data = &sn->timeouts[SCTP_CONNTRACK_COOKIE_ECHOED];
727 	pn->ctl_table[3].data = &sn->timeouts[SCTP_CONNTRACK_ESTABLISHED];
728 	pn->ctl_table[4].data = &sn->timeouts[SCTP_CONNTRACK_SHUTDOWN_SENT];
729 	pn->ctl_table[5].data = &sn->timeouts[SCTP_CONNTRACK_SHUTDOWN_RECD];
730 	pn->ctl_table[6].data = &sn->timeouts[SCTP_CONNTRACK_SHUTDOWN_ACK_SENT];
731 	pn->ctl_table[7].data = &sn->timeouts[SCTP_CONNTRACK_HEARTBEAT_SENT];
732 	pn->ctl_table[8].data = &sn->timeouts[SCTP_CONNTRACK_HEARTBEAT_ACKED];
733 #endif
734 	return 0;
735 }
736 
737 static int sctp_init_net(struct net *net, u_int16_t proto)
738 {
739 	struct nf_sctp_net *sn = sctp_pernet(net);
740 	struct nf_proto_net *pn = &sn->pn;
741 
742 	if (!pn->users) {
743 		int i;
744 
745 		for (i = 0; i < SCTP_CONNTRACK_MAX; i++)
746 			sn->timeouts[i] = sctp_timeouts[i];
747 
748 		/* timeouts[0] is unused, init it so ->timeouts[0] contains
749 		 * 'new' timeout, like udp or icmp.
750 		 */
751 		sn->timeouts[0] = sctp_timeouts[SCTP_CONNTRACK_CLOSED];
752 	}
753 
754 	return sctp_kmemdup_sysctl_table(pn, sn);
755 }
756 
757 static struct nf_proto_net *sctp_get_net_proto(struct net *net)
758 {
759 	return &net->ct.nf_ct_proto.sctp.pn;
760 }
761 
762 const struct nf_conntrack_l4proto nf_conntrack_l4proto_sctp4 = {
763 	.l3proto		= PF_INET,
764 	.l4proto 		= IPPROTO_SCTP,
765 #ifdef CONFIG_NF_CONNTRACK_PROCFS
766 	.print_conntrack	= sctp_print_conntrack,
767 #endif
768 	.packet 		= sctp_packet,
769 	.can_early_drop		= sctp_can_early_drop,
770 	.me 			= THIS_MODULE,
771 #if IS_ENABLED(CONFIG_NF_CT_NETLINK)
772 	.nlattr_size		= SCTP_NLATTR_SIZE,
773 	.to_nlattr		= sctp_to_nlattr,
774 	.from_nlattr		= nlattr_to_sctp,
775 	.tuple_to_nlattr	= nf_ct_port_tuple_to_nlattr,
776 	.nlattr_tuple_size	= nf_ct_port_nlattr_tuple_size,
777 	.nlattr_to_tuple	= nf_ct_port_nlattr_to_tuple,
778 	.nla_policy		= nf_ct_port_nla_policy,
779 #endif
780 #ifdef CONFIG_NF_CONNTRACK_TIMEOUT
781 	.ctnl_timeout		= {
782 		.nlattr_to_obj	= sctp_timeout_nlattr_to_obj,
783 		.obj_to_nlattr	= sctp_timeout_obj_to_nlattr,
784 		.nlattr_max	= CTA_TIMEOUT_SCTP_MAX,
785 		.obj_size	= sizeof(unsigned int) * SCTP_CONNTRACK_MAX,
786 		.nla_policy	= sctp_timeout_nla_policy,
787 	},
788 #endif /* CONFIG_NF_CONNTRACK_TIMEOUT */
789 	.init_net		= sctp_init_net,
790 	.get_net_proto		= sctp_get_net_proto,
791 };
792 EXPORT_SYMBOL_GPL(nf_conntrack_l4proto_sctp4);
793 
794 const struct nf_conntrack_l4proto nf_conntrack_l4proto_sctp6 = {
795 	.l3proto		= PF_INET6,
796 	.l4proto 		= IPPROTO_SCTP,
797 #ifdef CONFIG_NF_CONNTRACK_PROCFS
798 	.print_conntrack	= sctp_print_conntrack,
799 #endif
800 	.packet 		= sctp_packet,
801 	.can_early_drop		= sctp_can_early_drop,
802 	.me 			= THIS_MODULE,
803 #if IS_ENABLED(CONFIG_NF_CT_NETLINK)
804 	.nlattr_size		= SCTP_NLATTR_SIZE,
805 	.to_nlattr		= sctp_to_nlattr,
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,
811 #endif
812 #ifdef CONFIG_NF_CONNTRACK_TIMEOUT
813 	.ctnl_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,
819 	},
820 #endif /* CONFIG_NF_CONNTRACK_TIMEOUT */
821 	.init_net		= sctp_init_net,
822 	.get_net_proto		= sctp_get_net_proto,
823 };
824 EXPORT_SYMBOL_GPL(nf_conntrack_l4proto_sctp6);
825