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