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