]> git.proxmox.com Git - mirror_ubuntu-hirsute-kernel.git/blame - net/netfilter/nf_conntrack_proto_dccp.c
netfilter: conntrack: pass nf_hook_state to packet and error handlers
[mirror_ubuntu-hirsute-kernel.git] / net / netfilter / nf_conntrack_proto_dccp.c
CommitLineData
2bc78049
PM
1/*
2 * DCCP connection tracking protocol helper
3 *
4 * Copyright (c) 2005, 2006, 2008 Patrick McHardy <kaber@trash.net>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
9 *
10 */
11#include <linux/kernel.h>
2bc78049
PM
12#include <linux/init.h>
13#include <linux/sysctl.h>
14#include <linux/spinlock.h>
15#include <linux/skbuff.h>
16#include <linux/dccp.h>
5a0e3ad6 17#include <linux/slab.h>
2bc78049 18
1546000f
CG
19#include <net/net_namespace.h>
20#include <net/netns/generic.h>
21
2bc78049
PM
22#include <linux/netfilter/nfnetlink_conntrack.h>
23#include <net/netfilter/nf_conntrack.h>
24#include <net/netfilter/nf_conntrack_l4proto.h>
b38b1f61 25#include <net/netfilter/nf_conntrack_ecache.h>
c779e849 26#include <net/netfilter/nf_conntrack_timeout.h>
2bc78049
PM
27#include <net/netfilter/nf_log.h>
28
2bc78049
PM
29/* Timeouts are based on values from RFC4340:
30 *
31 * - REQUEST:
32 *
33 * 8.1.2. Client Request
34 *
35 * A client MAY give up on its DCCP-Requests after some time
36 * (3 minutes, for example).
37 *
38 * - RESPOND:
39 *
40 * 8.1.3. Server Response
41 *
42 * It MAY also leave the RESPOND state for CLOSED after a timeout of
43 * not less than 4MSL (8 minutes);
44 *
45 * - PARTOPEN:
46 *
47 * 8.1.5. Handshake Completion
48 *
49 * If the client remains in PARTOPEN for more than 4MSL (8 minutes),
50 * it SHOULD reset the connection with Reset Code 2, "Aborted".
51 *
52 * - OPEN:
53 *
54 * The DCCP timestamp overflows after 11.9 hours. If the connection
55 * stays idle this long the sequence number won't be recognized
56 * as valid anymore.
57 *
58 * - CLOSEREQ/CLOSING:
59 *
60 * 8.3. Termination
61 *
62 * The retransmission timer should initially be set to go off in two
63 * round-trip times and should back off to not less than once every
64 * 64 seconds ...
65 *
66 * - TIMEWAIT:
67 *
68 * 4.3. States
69 *
70 * A server or client socket remains in this state for 2MSL (4 minutes)
71 * after the connection has been town down, ...
72 */
73
74#define DCCP_MSL (2 * 60 * HZ)
75
2bc78049
PM
76static const char * const dccp_state_names[] = {
77 [CT_DCCP_NONE] = "NONE",
78 [CT_DCCP_REQUEST] = "REQUEST",
79 [CT_DCCP_RESPOND] = "RESPOND",
80 [CT_DCCP_PARTOPEN] = "PARTOPEN",
81 [CT_DCCP_OPEN] = "OPEN",
82 [CT_DCCP_CLOSEREQ] = "CLOSEREQ",
83 [CT_DCCP_CLOSING] = "CLOSING",
84 [CT_DCCP_TIMEWAIT] = "TIMEWAIT",
85 [CT_DCCP_IGNORE] = "IGNORE",
86 [CT_DCCP_INVALID] = "INVALID",
87};
88
89#define sNO CT_DCCP_NONE
90#define sRQ CT_DCCP_REQUEST
91#define sRS CT_DCCP_RESPOND
92#define sPO CT_DCCP_PARTOPEN
93#define sOP CT_DCCP_OPEN
94#define sCR CT_DCCP_CLOSEREQ
95#define sCG CT_DCCP_CLOSING
96#define sTW CT_DCCP_TIMEWAIT
97#define sIG CT_DCCP_IGNORE
98#define sIV CT_DCCP_INVALID
99
100/*
25985edc 101 * DCCP state transition table
2bc78049
PM
102 *
103 * The assumption is the same as for TCP tracking:
104 *
105 * We are the man in the middle. All the packets go through us but might
106 * get lost in transit to the destination. It is assumed that the destination
107 * can't receive segments we haven't seen.
108 *
109 * The following states exist:
110 *
111 * NONE: Initial state, expecting Request
112 * REQUEST: Request seen, waiting for Response from server
113 * RESPOND: Response from server seen, waiting for Ack from client
114 * PARTOPEN: Ack after Response seen, waiting for packet other than Response,
115 * Reset or Sync from server
116 * OPEN: Packet other than Response, Reset or Sync seen
117 * CLOSEREQ: CloseReq from server seen, expecting Close from client
118 * CLOSING: Close seen, expecting Reset
119 * TIMEWAIT: Reset seen
120 * IGNORE: Not determinable whether packet is valid
121 *
122 * Some states exist only on one side of the connection: REQUEST, RESPOND,
123 * PARTOPEN, CLOSEREQ. For the other side these states are equivalent to
124 * the one it was in before.
125 *
126 * Packets are marked as ignored (sIG) if we don't know if they're valid
127 * (for example a reincarnation of a connection we didn't notice is dead
128 * already) and the server may send back a connection closing Reset or a
129 * Response. They're also used for Sync/SyncAck packets, which we don't
130 * care about.
131 */
132static const u_int8_t
133dccp_state_table[CT_DCCP_ROLE_MAX + 1][DCCP_PKT_SYNCACK + 1][CT_DCCP_MAX + 1] = {
134 [CT_DCCP_ROLE_CLIENT] = {
135 [DCCP_PKT_REQUEST] = {
136 /*
137 * sNO -> sRQ Regular Request
138 * sRQ -> sRQ Retransmitted Request or reincarnation
139 * sRS -> sRS Retransmitted Request (apparently Response
140 * got lost after we saw it) or reincarnation
141 * sPO -> sIG Ignore, conntrack might be out of sync
142 * sOP -> sIG Ignore, conntrack might be out of sync
143 * sCR -> sIG Ignore, conntrack might be out of sync
144 * sCG -> sIG Ignore, conntrack might be out of sync
145 * sTW -> sRQ Reincarnation
146 *
147 * sNO, sRQ, sRS, sPO. sOP, sCR, sCG, sTW, */
148 sRQ, sRQ, sRS, sIG, sIG, sIG, sIG, sRQ,
149 },
150 [DCCP_PKT_RESPONSE] = {
151 /*
152 * sNO -> sIV Invalid
153 * sRQ -> sIG Ignore, might be response to ignored Request
154 * sRS -> sIG Ignore, might be response to ignored Request
155 * sPO -> sIG Ignore, might be response to ignored Request
156 * sOP -> sIG Ignore, might be response to ignored Request
157 * sCR -> sIG Ignore, might be response to ignored Request
158 * sCG -> sIG Ignore, might be response to ignored Request
159 * sTW -> sIV Invalid, reincarnation in reverse direction
160 * goes through sRQ
161 *
162 * sNO, sRQ, sRS, sPO, sOP, sCR, sCG, sTW */
163 sIV, sIG, sIG, sIG, sIG, sIG, sIG, sIV,
164 },
165 [DCCP_PKT_ACK] = {
166 /*
167 * sNO -> sIV No connection
168 * sRQ -> sIV No connection
169 * sRS -> sPO Ack for Response, move to PARTOPEN (8.1.5.)
170 * sPO -> sPO Retransmitted Ack for Response, remain in PARTOPEN
171 * sOP -> sOP Regular ACK, remain in OPEN
172 * sCR -> sCR Ack in CLOSEREQ MAY be processed (8.3.)
173 * sCG -> sCG Ack in CLOSING MAY be processed (8.3.)
174 * sTW -> sIV
175 *
176 * sNO, sRQ, sRS, sPO, sOP, sCR, sCG, sTW */
177 sIV, sIV, sPO, sPO, sOP, sCR, sCG, sIV
178 },
179 [DCCP_PKT_DATA] = {
180 /*
181 * sNO -> sIV No connection
182 * sRQ -> sIV No connection
183 * sRS -> sIV No connection
184 * sPO -> sIV MUST use DataAck in PARTOPEN state (8.1.5.)
185 * sOP -> sOP Regular Data packet
186 * sCR -> sCR Data in CLOSEREQ MAY be processed (8.3.)
187 * sCG -> sCG Data in CLOSING MAY be processed (8.3.)
188 * sTW -> sIV
189 *
190 * sNO, sRQ, sRS, sPO, sOP, sCR, sCG, sTW */
191 sIV, sIV, sIV, sIV, sOP, sCR, sCG, sIV,
192 },
193 [DCCP_PKT_DATAACK] = {
194 /*
195 * sNO -> sIV No connection
196 * sRQ -> sIV No connection
197 * sRS -> sPO Ack for Response, move to PARTOPEN (8.1.5.)
198 * sPO -> sPO Remain in PARTOPEN state
199 * sOP -> sOP Regular DataAck packet in OPEN state
200 * sCR -> sCR DataAck in CLOSEREQ MAY be processed (8.3.)
201 * sCG -> sCG DataAck in CLOSING MAY be processed (8.3.)
202 * sTW -> sIV
203 *
204 * sNO, sRQ, sRS, sPO, sOP, sCR, sCG, sTW */
205 sIV, sIV, sPO, sPO, sOP, sCR, sCG, sIV
206 },
207 [DCCP_PKT_CLOSEREQ] = {
208 /*
209 * CLOSEREQ may only be sent by the server.
210 *
211 * sNO, sRQ, sRS, sPO, sOP, sCR, sCG, sTW */
212 sIV, sIV, sIV, sIV, sIV, sIV, sIV, sIV
213 },
214 [DCCP_PKT_CLOSE] = {
215 /*
216 * sNO -> sIV No connection
217 * sRQ -> sIV No connection
218 * sRS -> sIV No connection
219 * sPO -> sCG Client-initiated close
220 * sOP -> sCG Client-initiated close
221 * sCR -> sCG Close in response to CloseReq (8.3.)
222 * sCG -> sCG Retransmit
223 * sTW -> sIV Late retransmit, already in TIME_WAIT
224 *
225 * sNO, sRQ, sRS, sPO, sOP, sCR, sCG, sTW */
226 sIV, sIV, sIV, sCG, sCG, sCG, sIV, sIV
227 },
228 [DCCP_PKT_RESET] = {
229 /*
230 * sNO -> sIV No connection
231 * sRQ -> sTW Sync received or timeout, SHOULD send Reset (8.1.1.)
232 * sRS -> sTW Response received without Request
233 * sPO -> sTW Timeout, SHOULD send Reset (8.1.5.)
234 * sOP -> sTW Connection reset
235 * sCR -> sTW Connection reset
236 * sCG -> sTW Connection reset
237 * sTW -> sIG Ignore (don't refresh timer)
238 *
239 * sNO, sRQ, sRS, sPO, sOP, sCR, sCG, sTW */
240 sIV, sTW, sTW, sTW, sTW, sTW, sTW, sIG
241 },
242 [DCCP_PKT_SYNC] = {
243 /*
244 * We currently ignore Sync packets
245 *
246 * sNO, sRQ, sRS, sPO, sOP, sCR, sCG, sTW */
6613b617 247 sIV, sIG, sIG, sIG, sIG, sIG, sIG, sIG,
2bc78049
PM
248 },
249 [DCCP_PKT_SYNCACK] = {
250 /*
251 * We currently ignore SyncAck packets
252 *
253 * sNO, sRQ, sRS, sPO, sOP, sCR, sCG, sTW */
6613b617 254 sIV, sIG, sIG, sIG, sIG, sIG, sIG, sIG,
2bc78049
PM
255 },
256 },
257 [CT_DCCP_ROLE_SERVER] = {
258 [DCCP_PKT_REQUEST] = {
259 /*
260 * sNO -> sIV Invalid
261 * sRQ -> sIG Ignore, conntrack might be out of sync
262 * sRS -> sIG Ignore, conntrack might be out of sync
263 * sPO -> sIG Ignore, conntrack might be out of sync
264 * sOP -> sIG Ignore, conntrack might be out of sync
265 * sCR -> sIG Ignore, conntrack might be out of sync
266 * sCG -> sIG Ignore, conntrack might be out of sync
267 * sTW -> sRQ Reincarnation, must reverse roles
268 *
269 * sNO, sRQ, sRS, sPO, sOP, sCR, sCG, sTW */
270 sIV, sIG, sIG, sIG, sIG, sIG, sIG, sRQ
271 },
272 [DCCP_PKT_RESPONSE] = {
273 /*
274 * sNO -> sIV Response without Request
275 * sRQ -> sRS Response to clients Request
276 * sRS -> sRS Retransmitted Response (8.1.3. SHOULD NOT)
277 * sPO -> sIG Response to an ignored Request or late retransmit
278 * sOP -> sIG Ignore, might be response to ignored Request
279 * sCR -> sIG Ignore, might be response to ignored Request
280 * sCG -> sIG Ignore, might be response to ignored Request
281 * sTW -> sIV Invalid, Request from client in sTW moves to sRQ
282 *
283 * sNO, sRQ, sRS, sPO, sOP, sCR, sCG, sTW */
284 sIV, sRS, sRS, sIG, sIG, sIG, sIG, sIV
285 },
286 [DCCP_PKT_ACK] = {
287 /*
288 * sNO -> sIV No connection
289 * sRQ -> sIV No connection
290 * sRS -> sIV No connection
291 * sPO -> sOP Enter OPEN state (8.1.5.)
292 * sOP -> sOP Regular Ack in OPEN state
293 * sCR -> sIV Waiting for Close from client
294 * sCG -> sCG Ack in CLOSING MAY be processed (8.3.)
295 * sTW -> sIV
296 *
297 * sNO, sRQ, sRS, sPO, sOP, sCR, sCG, sTW */
298 sIV, sIV, sIV, sOP, sOP, sIV, sCG, sIV
299 },
300 [DCCP_PKT_DATA] = {
301 /*
302 * sNO -> sIV No connection
303 * sRQ -> sIV No connection
304 * sRS -> sIV No connection
305 * sPO -> sOP Enter OPEN state (8.1.5.)
306 * sOP -> sOP Regular Data packet in OPEN state
307 * sCR -> sIV Waiting for Close from client
308 * sCG -> sCG Data in CLOSING MAY be processed (8.3.)
309 * sTW -> sIV
310 *
311 * sNO, sRQ, sRS, sPO, sOP, sCR, sCG, sTW */
312 sIV, sIV, sIV, sOP, sOP, sIV, sCG, sIV
313 },
314 [DCCP_PKT_DATAACK] = {
315 /*
316 * sNO -> sIV No connection
317 * sRQ -> sIV No connection
318 * sRS -> sIV No connection
319 * sPO -> sOP Enter OPEN state (8.1.5.)
320 * sOP -> sOP Regular DataAck in OPEN state
321 * sCR -> sIV Waiting for Close from client
322 * sCG -> sCG Data in CLOSING MAY be processed (8.3.)
323 * sTW -> sIV
324 *
325 * sNO, sRQ, sRS, sPO, sOP, sCR, sCG, sTW */
326 sIV, sIV, sIV, sOP, sOP, sIV, sCG, sIV
327 },
328 [DCCP_PKT_CLOSEREQ] = {
329 /*
330 * sNO -> sIV No connection
331 * sRQ -> sIV No connection
332 * sRS -> sIV No connection
333 * sPO -> sOP -> sCR Move directly to CLOSEREQ (8.1.5.)
334 * sOP -> sCR CloseReq in OPEN state
335 * sCR -> sCR Retransmit
336 * sCG -> sCR Simultaneous close, client sends another Close
337 * sTW -> sIV Already closed
338 *
339 * sNO, sRQ, sRS, sPO, sOP, sCR, sCG, sTW */
340 sIV, sIV, sIV, sCR, sCR, sCR, sCR, sIV
341 },
342 [DCCP_PKT_CLOSE] = {
343 /*
344 * sNO -> sIV No connection
345 * sRQ -> sIV No connection
346 * sRS -> sIV No connection
347 * sPO -> sOP -> sCG Move direcly to CLOSING
348 * sOP -> sCG Move to CLOSING
349 * sCR -> sIV Close after CloseReq is invalid
350 * sCG -> sCG Retransmit
351 * sTW -> sIV Already closed
352 *
353 * sNO, sRQ, sRS, sPO, sOP, sCR, sCG, sTW */
354 sIV, sIV, sIV, sCG, sCG, sIV, sCG, sIV
355 },
356 [DCCP_PKT_RESET] = {
357 /*
358 * sNO -> sIV No connection
359 * sRQ -> sTW Reset in response to Request
360 * sRS -> sTW Timeout, SHOULD send Reset (8.1.3.)
361 * sPO -> sTW Timeout, SHOULD send Reset (8.1.3.)
362 * sOP -> sTW
363 * sCR -> sTW
364 * sCG -> sTW
365 * sTW -> sIG Ignore (don't refresh timer)
366 *
367 * sNO, sRQ, sRS, sPO, sOP, sCR, sCG, sTW, sTW */
368 sIV, sTW, sTW, sTW, sTW, sTW, sTW, sTW, sIG
369 },
370 [DCCP_PKT_SYNC] = {
371 /*
372 * We currently ignore Sync packets
373 *
374 * sNO, sRQ, sRS, sPO, sOP, sCR, sCG, sTW */
6613b617 375 sIV, sIG, sIG, sIG, sIG, sIG, sIG, sIG,
2bc78049
PM
376 },
377 [DCCP_PKT_SYNCACK] = {
378 /*
379 * We currently ignore SyncAck packets
380 *
381 * sNO, sRQ, sRS, sPO, sOP, sCR, sCG, sTW */
6613b617 382 sIV, sIG, sIG, sIG, sIG, sIG, sIG, sIG,
2bc78049
PM
383 },
384 },
385};
386
c51d3901 387static inline struct nf_dccp_net *dccp_pernet(struct net *net)
1546000f 388{
c51d3901 389 return &net->ct.nf_ct_proto.dccp;
1546000f
CG
390}
391
09f263cd 392static bool dccp_new(struct nf_conn *ct, const struct sk_buff *skb,
c779e849 393 unsigned int dataoff)
2bc78049 394{
c2a2c7e0 395 struct net *net = nf_ct_net(ct);
c51d3901 396 struct nf_dccp_net *dn;
2bc78049
PM
397 struct dccp_hdr _dh, *dh;
398 const char *msg;
399 u_int8_t state;
400
b22f5126 401 dh = skb_header_pointer(skb, dataoff, sizeof(_dh), &_dh);
2bc78049
PM
402 BUG_ON(dh == NULL);
403
404 state = dccp_state_table[CT_DCCP_ROLE_CLIENT][dh->dccph_type][CT_DCCP_NONE];
405 switch (state) {
406 default:
1546000f
CG
407 dn = dccp_pernet(net);
408 if (dn->dccp_loose == 0) {
3d0b527b 409 msg = "not picking up existing connection ";
2bc78049
PM
410 goto out_invalid;
411 }
412 case CT_DCCP_REQUEST:
413 break;
414 case CT_DCCP_INVALID:
3d0b527b 415 msg = "invalid state transition ";
2bc78049
PM
416 goto out_invalid;
417 }
418
419 ct->proto.dccp.role[IP_CT_DIR_ORIGINAL] = CT_DCCP_ROLE_CLIENT;
420 ct->proto.dccp.role[IP_CT_DIR_REPLY] = CT_DCCP_ROLE_SERVER;
421 ct->proto.dccp.state = CT_DCCP_NONE;
e5fc9e7a
CG
422 ct->proto.dccp.last_pkt = DCCP_PKT_REQUEST;
423 ct->proto.dccp.last_dir = IP_CT_DIR_ORIGINAL;
424 ct->proto.dccp.handshake_seq = 0;
09f263cd 425 return true;
2bc78049
PM
426
427out_invalid:
3d0b527b 428 nf_ct_l4proto_log_invalid(skb, ct, "%s", msg);
09f263cd 429 return false;
2bc78049
PM
430}
431
432static u64 dccp_ack_seq(const struct dccp_hdr *dh)
433{
434 const struct dccp_hdr_ack_bits *dhack;
435
436 dhack = (void *)dh + __dccp_basic_hdr_len(dh);
437 return ((u64)ntohs(dhack->dccph_ack_nr_high) << 32) +
438 ntohl(dhack->dccph_ack_nr_low);
439}
440
441static int dccp_packet(struct nf_conn *ct, const struct sk_buff *skb,
93e66024
FW
442 unsigned int dataoff, enum ip_conntrack_info ctinfo,
443 const struct nf_hook_state *state)
2bc78049
PM
444{
445 enum ip_conntrack_dir dir = CTINFO2DIR(ctinfo);
446 struct dccp_hdr _dh, *dh;
447 u_int8_t type, old_state, new_state;
448 enum ct_dccp_roles role;
c779e849 449 unsigned int *timeouts;
2bc78049 450
b22f5126 451 dh = skb_header_pointer(skb, dataoff, sizeof(_dh), &_dh);
2bc78049
PM
452 BUG_ON(dh == NULL);
453 type = dh->dccph_type;
454
455 if (type == DCCP_PKT_RESET &&
456 !test_bit(IPS_SEEN_REPLY_BIT, &ct->status)) {
457 /* Tear down connection immediately if only reply is a RESET */
718d4ad9 458 nf_ct_kill_acct(ct, ctinfo, skb);
2bc78049
PM
459 return NF_ACCEPT;
460 }
461
440f0d58 462 spin_lock_bh(&ct->lock);
2bc78049
PM
463
464 role = ct->proto.dccp.role[dir];
465 old_state = ct->proto.dccp.state;
466 new_state = dccp_state_table[role][type][old_state];
467
468 switch (new_state) {
469 case CT_DCCP_REQUEST:
470 if (old_state == CT_DCCP_TIMEWAIT &&
471 role == CT_DCCP_ROLE_SERVER) {
472 /* Reincarnation in the reverse direction: reopen and
473 * reverse client/server roles. */
474 ct->proto.dccp.role[dir] = CT_DCCP_ROLE_CLIENT;
475 ct->proto.dccp.role[!dir] = CT_DCCP_ROLE_SERVER;
476 }
477 break;
478 case CT_DCCP_RESPOND:
479 if (old_state == CT_DCCP_REQUEST)
480 ct->proto.dccp.handshake_seq = dccp_hdr_seq(dh);
481 break;
482 case CT_DCCP_PARTOPEN:
483 if (old_state == CT_DCCP_RESPOND &&
484 type == DCCP_PKT_ACK &&
485 dccp_ack_seq(dh) == ct->proto.dccp.handshake_seq)
486 set_bit(IPS_ASSURED_BIT, &ct->status);
487 break;
488 case CT_DCCP_IGNORE:
489 /*
490 * Connection tracking might be out of sync, so we ignore
491 * packets that might establish a new connection and resync
492 * if the server responds with a valid Response.
493 */
494 if (ct->proto.dccp.last_dir == !dir &&
495 ct->proto.dccp.last_pkt == DCCP_PKT_REQUEST &&
496 type == DCCP_PKT_RESPONSE) {
497 ct->proto.dccp.role[!dir] = CT_DCCP_ROLE_CLIENT;
498 ct->proto.dccp.role[dir] = CT_DCCP_ROLE_SERVER;
499 ct->proto.dccp.handshake_seq = dccp_hdr_seq(dh);
500 new_state = CT_DCCP_RESPOND;
501 break;
502 }
503 ct->proto.dccp.last_dir = dir;
504 ct->proto.dccp.last_pkt = type;
505
440f0d58 506 spin_unlock_bh(&ct->lock);
3d0b527b 507 nf_ct_l4proto_log_invalid(skb, ct, "%s", "invalid packet");
2bc78049
PM
508 return NF_ACCEPT;
509 case CT_DCCP_INVALID:
440f0d58 510 spin_unlock_bh(&ct->lock);
3d0b527b 511 nf_ct_l4proto_log_invalid(skb, ct, "%s", "invalid state transition");
2bc78049
PM
512 return -NF_ACCEPT;
513 }
514
515 ct->proto.dccp.last_dir = dir;
516 ct->proto.dccp.last_pkt = type;
517 ct->proto.dccp.state = new_state;
440f0d58 518 spin_unlock_bh(&ct->lock);
1546000f 519
b38b1f61
PNA
520 if (new_state != old_state)
521 nf_conntrack_event_cache(IPCT_PROTOINFO, ct);
522
c779e849
FW
523 timeouts = nf_ct_timeout_lookup(ct);
524 if (!timeouts)
525 timeouts = dccp_pernet(nf_ct_net(ct))->dccp_timeout;
2c8503f5 526 nf_ct_refresh_acct(ct, ctinfo, skb, timeouts[new_state]);
2bc78049
PM
527
528 return NF_ACCEPT;
529}
530
93e66024 531static int dccp_error(struct nf_conn *tmpl,
8fea97ec 532 struct sk_buff *skb, unsigned int dataoff,
93e66024 533 const struct nf_hook_state *state)
2bc78049
PM
534{
535 struct dccp_hdr _dh, *dh;
536 unsigned int dccp_len = skb->len - dataoff;
537 unsigned int cscov;
538 const char *msg;
539
b22f5126 540 dh = skb_header_pointer(skb, dataoff, sizeof(_dh), &_dh);
2bc78049
PM
541 if (dh == NULL) {
542 msg = "nf_ct_dccp: short packet ";
543 goto out_invalid;
544 }
545
546 if (dh->dccph_doff * 4 < sizeof(struct dccp_hdr) ||
547 dh->dccph_doff * 4 > dccp_len) {
548 msg = "nf_ct_dccp: truncated/malformed packet ";
549 goto out_invalid;
550 }
551
552 cscov = dccp_len;
553 if (dh->dccph_cscov) {
554 cscov = (dh->dccph_cscov - 1) * 4;
555 if (cscov > dccp_len) {
556 msg = "nf_ct_dccp: bad checksum coverage ";
557 goto out_invalid;
558 }
559 }
560
93e66024
FW
561 if (state->hook == NF_INET_PRE_ROUTING &&
562 state->net->ct.sysctl_checksum &&
563 nf_checksum_partial(skb, state->hook, dataoff, cscov,
564 IPPROTO_DCCP, state->pf)) {
2bc78049
PM
565 msg = "nf_ct_dccp: bad checksum ";
566 goto out_invalid;
567 }
568
569 if (dh->dccph_type >= DCCP_PKT_INVALID) {
570 msg = "nf_ct_dccp: reserved packet type ";
571 goto out_invalid;
572 }
573
574 return NF_ACCEPT;
575
576out_invalid:
93e66024
FW
577 nf_l4proto_log_invalid(skb, state->net, state->pf,
578 IPPROTO_DCCP, "%s", msg);
2bc78049
PM
579 return -NF_ACCEPT;
580}
581
c6dd940b
FW
582static bool dccp_can_early_drop(const struct nf_conn *ct)
583{
584 switch (ct->proto.dccp.state) {
585 case CT_DCCP_CLOSEREQ:
586 case CT_DCCP_CLOSING:
587 case CT_DCCP_TIMEWAIT:
588 return true;
589 default:
590 break;
591 }
592
593 return false;
594}
595
ea48cc83 596#ifdef CONFIG_NF_CONNTRACK_PROCFS
37246a58 597static void dccp_print_conntrack(struct seq_file *s, struct nf_conn *ct)
2bc78049 598{
37246a58 599 seq_printf(s, "%s ", dccp_state_names[ct->proto.dccp.state]);
2bc78049 600}
ea48cc83 601#endif
2bc78049 602
c0cd1156 603#if IS_ENABLED(CONFIG_NF_CT_NETLINK)
2bc78049 604static int dccp_to_nlattr(struct sk_buff *skb, struct nlattr *nla,
440f0d58 605 struct nf_conn *ct)
2bc78049
PM
606{
607 struct nlattr *nest_parms;
608
440f0d58 609 spin_lock_bh(&ct->lock);
2bc78049
PM
610 nest_parms = nla_nest_start(skb, CTA_PROTOINFO_DCCP | NLA_F_NESTED);
611 if (!nest_parms)
612 goto nla_put_failure;
516ee48f
DM
613 if (nla_put_u8(skb, CTA_PROTOINFO_DCCP_STATE, ct->proto.dccp.state) ||
614 nla_put_u8(skb, CTA_PROTOINFO_DCCP_ROLE,
615 ct->proto.dccp.role[IP_CT_DIR_ORIGINAL]) ||
616 nla_put_be64(skb, CTA_PROTOINFO_DCCP_HANDSHAKE_SEQ,
b46f6ded
ND
617 cpu_to_be64(ct->proto.dccp.handshake_seq),
618 CTA_PROTOINFO_DCCP_PAD))
516ee48f 619 goto nla_put_failure;
2bc78049 620 nla_nest_end(skb, nest_parms);
440f0d58 621 spin_unlock_bh(&ct->lock);
2bc78049
PM
622 return 0;
623
624nla_put_failure:
440f0d58 625 spin_unlock_bh(&ct->lock);
2bc78049
PM
626 return -1;
627}
628
629static const struct nla_policy dccp_nla_policy[CTA_PROTOINFO_DCCP_MAX + 1] = {
630 [CTA_PROTOINFO_DCCP_STATE] = { .type = NLA_U8 },
71951b64 631 [CTA_PROTOINFO_DCCP_ROLE] = { .type = NLA_U8 },
a17c8598 632 [CTA_PROTOINFO_DCCP_HANDSHAKE_SEQ] = { .type = NLA_U64 },
b46f6ded 633 [CTA_PROTOINFO_DCCP_PAD] = { .type = NLA_UNSPEC },
2bc78049
PM
634};
635
39215846
FW
636#define DCCP_NLATTR_SIZE ( \
637 NLA_ALIGN(NLA_HDRLEN + 1) + \
638 NLA_ALIGN(NLA_HDRLEN + 1) + \
639 NLA_ALIGN(NLA_HDRLEN + sizeof(u64)) + \
640 NLA_ALIGN(NLA_HDRLEN + 0))
641
2bc78049
PM
642static int nlattr_to_dccp(struct nlattr *cda[], struct nf_conn *ct)
643{
644 struct nlattr *attr = cda[CTA_PROTOINFO_DCCP];
645 struct nlattr *tb[CTA_PROTOINFO_DCCP_MAX + 1];
646 int err;
647
648 if (!attr)
649 return 0;
650
651 err = nla_parse_nested(tb, CTA_PROTOINFO_DCCP_MAX, attr,
fceb6435 652 dccp_nla_policy, NULL);
2bc78049
PM
653 if (err < 0)
654 return err;
655
656 if (!tb[CTA_PROTOINFO_DCCP_STATE] ||
71951b64
PNA
657 !tb[CTA_PROTOINFO_DCCP_ROLE] ||
658 nla_get_u8(tb[CTA_PROTOINFO_DCCP_ROLE]) > CT_DCCP_ROLE_MAX ||
659 nla_get_u8(tb[CTA_PROTOINFO_DCCP_STATE]) >= CT_DCCP_IGNORE) {
2bc78049 660 return -EINVAL;
71951b64 661 }
2bc78049 662
440f0d58 663 spin_lock_bh(&ct->lock);
2bc78049 664 ct->proto.dccp.state = nla_get_u8(tb[CTA_PROTOINFO_DCCP_STATE]);
71951b64
PNA
665 if (nla_get_u8(tb[CTA_PROTOINFO_DCCP_ROLE]) == CT_DCCP_ROLE_CLIENT) {
666 ct->proto.dccp.role[IP_CT_DIR_ORIGINAL] = CT_DCCP_ROLE_CLIENT;
667 ct->proto.dccp.role[IP_CT_DIR_REPLY] = CT_DCCP_ROLE_SERVER;
668 } else {
669 ct->proto.dccp.role[IP_CT_DIR_ORIGINAL] = CT_DCCP_ROLE_SERVER;
670 ct->proto.dccp.role[IP_CT_DIR_REPLY] = CT_DCCP_ROLE_CLIENT;
671 }
a17c8598
PNA
672 if (tb[CTA_PROTOINFO_DCCP_HANDSHAKE_SEQ]) {
673 ct->proto.dccp.handshake_seq =
674 be64_to_cpu(nla_get_be64(tb[CTA_PROTOINFO_DCCP_HANDSHAKE_SEQ]));
675 }
440f0d58 676 spin_unlock_bh(&ct->lock);
2bc78049
PM
677 return 0;
678}
679#endif
680
a874752a 681#ifdef CONFIG_NF_CONNTRACK_TIMEOUT
50978462
PNA
682
683#include <linux/netfilter/nfnetlink.h>
684#include <linux/netfilter/nfnetlink_cttimeout.h>
685
8264deb8
G
686static int dccp_timeout_nlattr_to_obj(struct nlattr *tb[],
687 struct net *net, void *data)
50978462 688{
c51d3901 689 struct nf_dccp_net *dn = dccp_pernet(net);
50978462
PNA
690 unsigned int *timeouts = data;
691 int i;
692
693 /* set default DCCP timeouts. */
694 for (i=0; i<CT_DCCP_MAX; i++)
695 timeouts[i] = dn->dccp_timeout[i];
696
697 /* there's a 1:1 mapping between attributes and protocol states. */
698 for (i=CTA_TIMEOUT_DCCP_UNSPEC+1; i<CTA_TIMEOUT_DCCP_MAX+1; i++) {
699 if (tb[i]) {
700 timeouts[i] = ntohl(nla_get_be32(tb[i])) * HZ;
701 }
702 }
ef39078d
FW
703
704 timeouts[CTA_TIMEOUT_DCCP_UNSPEC] = timeouts[CTA_TIMEOUT_DCCP_REQUEST];
50978462
PNA
705 return 0;
706}
707
708static int
709dccp_timeout_obj_to_nlattr(struct sk_buff *skb, const void *data)
710{
711 const unsigned int *timeouts = data;
712 int i;
713
516ee48f
DM
714 for (i=CTA_TIMEOUT_DCCP_UNSPEC+1; i<CTA_TIMEOUT_DCCP_MAX+1; i++) {
715 if (nla_put_be32(skb, i, htonl(timeouts[i] / HZ)))
716 goto nla_put_failure;
717 }
50978462
PNA
718 return 0;
719
720nla_put_failure:
721 return -ENOSPC;
722}
723
724static const struct nla_policy
725dccp_timeout_nla_policy[CTA_TIMEOUT_DCCP_MAX+1] = {
726 [CTA_TIMEOUT_DCCP_REQUEST] = { .type = NLA_U32 },
727 [CTA_TIMEOUT_DCCP_RESPOND] = { .type = NLA_U32 },
728 [CTA_TIMEOUT_DCCP_PARTOPEN] = { .type = NLA_U32 },
729 [CTA_TIMEOUT_DCCP_OPEN] = { .type = NLA_U32 },
730 [CTA_TIMEOUT_DCCP_CLOSEREQ] = { .type = NLA_U32 },
731 [CTA_TIMEOUT_DCCP_CLOSING] = { .type = NLA_U32 },
732 [CTA_TIMEOUT_DCCP_TIMEWAIT] = { .type = NLA_U32 },
733};
a874752a 734#endif /* CONFIG_NF_CONNTRACK_TIMEOUT */
50978462 735
2bc78049 736#ifdef CONFIG_SYSCTL
1546000f
CG
737/* template, data assigned later */
738static struct ctl_table dccp_sysctl_table[] = {
2bc78049 739 {
2bc78049 740 .procname = "nf_conntrack_dccp_timeout_request",
2bc78049
PM
741 .maxlen = sizeof(unsigned int),
742 .mode = 0644,
743 .proc_handler = proc_dointvec_jiffies,
744 },
745 {
2bc78049 746 .procname = "nf_conntrack_dccp_timeout_respond",
2bc78049
PM
747 .maxlen = sizeof(unsigned int),
748 .mode = 0644,
749 .proc_handler = proc_dointvec_jiffies,
750 },
751 {
2bc78049 752 .procname = "nf_conntrack_dccp_timeout_partopen",
2bc78049
PM
753 .maxlen = sizeof(unsigned int),
754 .mode = 0644,
755 .proc_handler = proc_dointvec_jiffies,
756 },
757 {
2bc78049 758 .procname = "nf_conntrack_dccp_timeout_open",
2bc78049
PM
759 .maxlen = sizeof(unsigned int),
760 .mode = 0644,
761 .proc_handler = proc_dointvec_jiffies,
762 },
763 {
2bc78049 764 .procname = "nf_conntrack_dccp_timeout_closereq",
2bc78049
PM
765 .maxlen = sizeof(unsigned int),
766 .mode = 0644,
767 .proc_handler = proc_dointvec_jiffies,
768 },
769 {
2bc78049 770 .procname = "nf_conntrack_dccp_timeout_closing",
2bc78049
PM
771 .maxlen = sizeof(unsigned int),
772 .mode = 0644,
773 .proc_handler = proc_dointvec_jiffies,
774 },
775 {
2bc78049 776 .procname = "nf_conntrack_dccp_timeout_timewait",
2bc78049
PM
777 .maxlen = sizeof(unsigned int),
778 .mode = 0644,
779 .proc_handler = proc_dointvec_jiffies,
780 },
781 {
2bc78049 782 .procname = "nf_conntrack_dccp_loose",
1546000f 783 .maxlen = sizeof(int),
2bc78049
PM
784 .mode = 0644,
785 .proc_handler = proc_dointvec,
786 },
f8572d8f 787 { }
2bc78049
PM
788};
789#endif /* CONFIG_SYSCTL */
790
464dc801 791static int dccp_kmemdup_sysctl_table(struct net *net, struct nf_proto_net *pn,
c51d3901 792 struct nf_dccp_net *dn)
54b8873f
G
793{
794#ifdef CONFIG_SYSCTL
795 if (pn->ctl_table)
796 return 0;
797
798 pn->ctl_table = kmemdup(dccp_sysctl_table,
799 sizeof(dccp_sysctl_table),
800 GFP_KERNEL);
801 if (!pn->ctl_table)
802 return -ENOMEM;
803
804 pn->ctl_table[0].data = &dn->dccp_timeout[CT_DCCP_REQUEST];
805 pn->ctl_table[1].data = &dn->dccp_timeout[CT_DCCP_RESPOND];
806 pn->ctl_table[2].data = &dn->dccp_timeout[CT_DCCP_PARTOPEN];
807 pn->ctl_table[3].data = &dn->dccp_timeout[CT_DCCP_OPEN];
808 pn->ctl_table[4].data = &dn->dccp_timeout[CT_DCCP_CLOSEREQ];
809 pn->ctl_table[5].data = &dn->dccp_timeout[CT_DCCP_CLOSING];
810 pn->ctl_table[6].data = &dn->dccp_timeout[CT_DCCP_TIMEWAIT];
811 pn->ctl_table[7].data = &dn->dccp_loose;
464dc801
EB
812
813 /* Don't export sysctls to unprivileged users */
814 if (net->user_ns != &init_user_ns)
815 pn->ctl_table[0].procname = NULL;
54b8873f
G
816#endif
817 return 0;
818}
819
f1caad27 820static int dccp_init_net(struct net *net, u_int16_t proto)
84c39451 821{
c51d3901 822 struct nf_dccp_net *dn = dccp_pernet(net);
54b8873f 823 struct nf_proto_net *pn = &dn->pn;
84c39451 824
54b8873f 825 if (!pn->users) {
84c39451
G
826 /* default values */
827 dn->dccp_loose = 1;
828 dn->dccp_timeout[CT_DCCP_REQUEST] = 2 * DCCP_MSL;
829 dn->dccp_timeout[CT_DCCP_RESPOND] = 4 * DCCP_MSL;
830 dn->dccp_timeout[CT_DCCP_PARTOPEN] = 4 * DCCP_MSL;
831 dn->dccp_timeout[CT_DCCP_OPEN] = 12 * 3600 * HZ;
832 dn->dccp_timeout[CT_DCCP_CLOSEREQ] = 64 * HZ;
833 dn->dccp_timeout[CT_DCCP_CLOSING] = 64 * HZ;
834 dn->dccp_timeout[CT_DCCP_TIMEWAIT] = 2 * DCCP_MSL;
ef39078d
FW
835
836 /* timeouts[0] is unused, make it same as SYN_SENT so
837 * ->timeouts[0] contains 'new' timeout, like udp or icmp.
838 */
839 dn->dccp_timeout[CT_DCCP_NONE] = dn->dccp_timeout[CT_DCCP_REQUEST];
84c39451 840 }
54b8873f 841
464dc801 842 return dccp_kmemdup_sysctl_table(net, pn, dn);
84c39451
G
843}
844
deaa0a97
LZ
845static struct nf_proto_net *dccp_get_net_proto(struct net *net)
846{
847 return &net->ct.nf_ct_proto.dccp.pn;
848}
849
9dae47ab 850const struct nf_conntrack_l4proto nf_conntrack_l4proto_dccp4 = {
2bc78049
PM
851 .l3proto = AF_INET,
852 .l4proto = IPPROTO_DCCP,
2bc78049
PM
853 .new = dccp_new,
854 .packet = dccp_packet,
855 .error = dccp_error,
c6dd940b 856 .can_early_drop = dccp_can_early_drop,
ea48cc83 857#ifdef CONFIG_NF_CONNTRACK_PROCFS
2bc78049 858 .print_conntrack = dccp_print_conntrack,
ea48cc83 859#endif
c0cd1156 860#if IS_ENABLED(CONFIG_NF_CT_NETLINK)
39215846 861 .nlattr_size = DCCP_NLATTR_SIZE,
2bc78049
PM
862 .to_nlattr = dccp_to_nlattr,
863 .from_nlattr = nlattr_to_dccp,
864 .tuple_to_nlattr = nf_ct_port_tuple_to_nlattr,
a400c30e 865 .nlattr_tuple_size = nf_ct_port_nlattr_tuple_size,
2bc78049
PM
866 .nlattr_to_tuple = nf_ct_port_nlattr_to_tuple,
867 .nla_policy = nf_ct_port_nla_policy,
868#endif
a874752a 869#ifdef CONFIG_NF_CONNTRACK_TIMEOUT
50978462
PNA
870 .ctnl_timeout = {
871 .nlattr_to_obj = dccp_timeout_nlattr_to_obj,
872 .obj_to_nlattr = dccp_timeout_obj_to_nlattr,
873 .nlattr_max = CTA_TIMEOUT_DCCP_MAX,
874 .obj_size = sizeof(unsigned int) * CT_DCCP_MAX,
875 .nla_policy = dccp_timeout_nla_policy,
876 },
a874752a 877#endif /* CONFIG_NF_CONNTRACK_TIMEOUT */
84c39451 878 .init_net = dccp_init_net,
deaa0a97 879 .get_net_proto = dccp_get_net_proto,
2bc78049 880};
c51d3901 881EXPORT_SYMBOL_GPL(nf_conntrack_l4proto_dccp4);
2bc78049 882
9dae47ab 883const struct nf_conntrack_l4proto nf_conntrack_l4proto_dccp6 = {
2bc78049
PM
884 .l3proto = AF_INET6,
885 .l4proto = IPPROTO_DCCP,
2bc78049
PM
886 .new = dccp_new,
887 .packet = dccp_packet,
888 .error = dccp_error,
c6dd940b 889 .can_early_drop = dccp_can_early_drop,
ea48cc83 890#ifdef CONFIG_NF_CONNTRACK_PROCFS
2bc78049 891 .print_conntrack = dccp_print_conntrack,
ea48cc83 892#endif
c0cd1156 893#if IS_ENABLED(CONFIG_NF_CT_NETLINK)
39215846 894 .nlattr_size = DCCP_NLATTR_SIZE,
2bc78049
PM
895 .to_nlattr = dccp_to_nlattr,
896 .from_nlattr = nlattr_to_dccp,
897 .tuple_to_nlattr = nf_ct_port_tuple_to_nlattr,
a400c30e 898 .nlattr_tuple_size = nf_ct_port_nlattr_tuple_size,
2bc78049
PM
899 .nlattr_to_tuple = nf_ct_port_nlattr_to_tuple,
900 .nla_policy = nf_ct_port_nla_policy,
901#endif
a874752a 902#ifdef CONFIG_NF_CONNTRACK_TIMEOUT
50978462
PNA
903 .ctnl_timeout = {
904 .nlattr_to_obj = dccp_timeout_nlattr_to_obj,
905 .obj_to_nlattr = dccp_timeout_obj_to_nlattr,
906 .nlattr_max = CTA_TIMEOUT_DCCP_MAX,
907 .obj_size = sizeof(unsigned int) * CT_DCCP_MAX,
908 .nla_policy = dccp_timeout_nla_policy,
909 },
a874752a 910#endif /* CONFIG_NF_CONNTRACK_TIMEOUT */
84c39451 911 .init_net = dccp_init_net,
deaa0a97 912 .get_net_proto = dccp_get_net_proto,
1546000f 913};
c51d3901 914EXPORT_SYMBOL_GPL(nf_conntrack_l4proto_dccp6);