1 // SPDX-License-Identifier: GPL-2.0-only
3 * DCCP connection tracking protocol helper
5 * Copyright (c) 2005, 2006, 2008 Patrick McHardy <kaber@trash.net>
7 #include <linux/kernel.h>
8 #include <linux/init.h>
9 #include <linux/sysctl.h>
10 #include <linux/spinlock.h>
11 #include <linux/skbuff.h>
12 #include <linux/dccp.h>
13 #include <linux/slab.h>
15 #include <net/net_namespace.h>
16 #include <net/netns/generic.h>
18 #include <linux/netfilter/nfnetlink_conntrack.h>
19 #include <net/netfilter/nf_conntrack.h>
20 #include <net/netfilter/nf_conntrack_l4proto.h>
21 #include <net/netfilter/nf_conntrack_ecache.h>
22 #include <net/netfilter/nf_conntrack_timeout.h>
23 #include <net/netfilter/nf_log.h>
25 /* Timeouts are based on values from RFC4340:
29 * 8.1.2. Client Request
31 * A client MAY give up on its DCCP-Requests after some time
32 * (3 minutes, for example).
36 * 8.1.3. Server Response
38 * It MAY also leave the RESPOND state for CLOSED after a timeout of
39 * not less than 4MSL (8 minutes);
43 * 8.1.5. Handshake Completion
45 * If the client remains in PARTOPEN for more than 4MSL (8 minutes),
46 * it SHOULD reset the connection with Reset Code 2, "Aborted".
50 * The DCCP timestamp overflows after 11.9 hours. If the connection
51 * stays idle this long the sequence number won't be recognized
58 * The retransmission timer should initially be set to go off in two
59 * round-trip times and should back off to not less than once every
66 * A server or client socket remains in this state for 2MSL (4 minutes)
67 * after the connection has been town down, ...
70 #define DCCP_MSL (2 * 60 * HZ)
72 static const char * const dccp_state_names[] = {
73 [CT_DCCP_NONE] = "NONE",
74 [CT_DCCP_REQUEST] = "REQUEST",
75 [CT_DCCP_RESPOND] = "RESPOND",
76 [CT_DCCP_PARTOPEN] = "PARTOPEN",
77 [CT_DCCP_OPEN] = "OPEN",
78 [CT_DCCP_CLOSEREQ] = "CLOSEREQ",
79 [CT_DCCP_CLOSING] = "CLOSING",
80 [CT_DCCP_TIMEWAIT] = "TIMEWAIT",
81 [CT_DCCP_IGNORE] = "IGNORE",
82 [CT_DCCP_INVALID] = "INVALID",
85 #define sNO CT_DCCP_NONE
86 #define sRQ CT_DCCP_REQUEST
87 #define sRS CT_DCCP_RESPOND
88 #define sPO CT_DCCP_PARTOPEN
89 #define sOP CT_DCCP_OPEN
90 #define sCR CT_DCCP_CLOSEREQ
91 #define sCG CT_DCCP_CLOSING
92 #define sTW CT_DCCP_TIMEWAIT
93 #define sIG CT_DCCP_IGNORE
94 #define sIV CT_DCCP_INVALID
97 * DCCP state transition table
99 * The assumption is the same as for TCP tracking:
101 * We are the man in the middle. All the packets go through us but might
102 * get lost in transit to the destination. It is assumed that the destination
103 * can't receive segments we haven't seen.
105 * The following states exist:
107 * NONE: Initial state, expecting Request
108 * REQUEST: Request seen, waiting for Response from server
109 * RESPOND: Response from server seen, waiting for Ack from client
110 * PARTOPEN: Ack after Response seen, waiting for packet other than Response,
111 * Reset or Sync from server
112 * OPEN: Packet other than Response, Reset or Sync seen
113 * CLOSEREQ: CloseReq from server seen, expecting Close from client
114 * CLOSING: Close seen, expecting Reset
115 * TIMEWAIT: Reset seen
116 * IGNORE: Not determinable whether packet is valid
118 * Some states exist only on one side of the connection: REQUEST, RESPOND,
119 * PARTOPEN, CLOSEREQ. For the other side these states are equivalent to
120 * the one it was in before.
122 * Packets are marked as ignored (sIG) if we don't know if they're valid
123 * (for example a reincarnation of a connection we didn't notice is dead
124 * already) and the server may send back a connection closing Reset or a
125 * Response. They're also used for Sync/SyncAck packets, which we don't
128 static const u_int8_t
129 dccp_state_table[CT_DCCP_ROLE_MAX + 1][DCCP_PKT_SYNCACK + 1][CT_DCCP_MAX + 1] = {
130 [CT_DCCP_ROLE_CLIENT] = {
131 [DCCP_PKT_REQUEST] = {
133 * sNO -> sRQ Regular Request
134 * sRQ -> sRQ Retransmitted Request or reincarnation
135 * sRS -> sRS Retransmitted Request (apparently Response
136 * got lost after we saw it) or reincarnation
137 * sPO -> sIG Ignore, conntrack might be out of sync
138 * sOP -> sIG Ignore, conntrack might be out of sync
139 * sCR -> sIG Ignore, conntrack might be out of sync
140 * sCG -> sIG Ignore, conntrack might be out of sync
141 * sTW -> sRQ Reincarnation
143 * sNO, sRQ, sRS, sPO. sOP, sCR, sCG, sTW, */
144 sRQ, sRQ, sRS, sIG, sIG, sIG, sIG, sRQ,
146 [DCCP_PKT_RESPONSE] = {
149 * sRQ -> sIG Ignore, might be response to ignored Request
150 * sRS -> sIG Ignore, might be response to ignored Request
151 * sPO -> sIG Ignore, might be response to ignored Request
152 * sOP -> sIG Ignore, might be response to ignored Request
153 * sCR -> sIG Ignore, might be response to ignored Request
154 * sCG -> sIG Ignore, might be response to ignored Request
155 * sTW -> sIV Invalid, reincarnation in reverse direction
158 * sNO, sRQ, sRS, sPO, sOP, sCR, sCG, sTW */
159 sIV, sIG, sIG, sIG, sIG, sIG, sIG, sIV,
163 * sNO -> sIV No connection
164 * sRQ -> sIV No connection
165 * sRS -> sPO Ack for Response, move to PARTOPEN (8.1.5.)
166 * sPO -> sPO Retransmitted Ack for Response, remain in PARTOPEN
167 * sOP -> sOP Regular ACK, remain in OPEN
168 * sCR -> sCR Ack in CLOSEREQ MAY be processed (8.3.)
169 * sCG -> sCG Ack in CLOSING MAY be processed (8.3.)
172 * sNO, sRQ, sRS, sPO, sOP, sCR, sCG, sTW */
173 sIV, sIV, sPO, sPO, sOP, sCR, sCG, sIV
177 * sNO -> sIV No connection
178 * sRQ -> sIV No connection
179 * sRS -> sIV No connection
180 * sPO -> sIV MUST use DataAck in PARTOPEN state (8.1.5.)
181 * sOP -> sOP Regular Data packet
182 * sCR -> sCR Data in CLOSEREQ MAY be processed (8.3.)
183 * sCG -> sCG Data in CLOSING MAY be processed (8.3.)
186 * sNO, sRQ, sRS, sPO, sOP, sCR, sCG, sTW */
187 sIV, sIV, sIV, sIV, sOP, sCR, sCG, sIV,
189 [DCCP_PKT_DATAACK] = {
191 * sNO -> sIV No connection
192 * sRQ -> sIV No connection
193 * sRS -> sPO Ack for Response, move to PARTOPEN (8.1.5.)
194 * sPO -> sPO Remain in PARTOPEN state
195 * sOP -> sOP Regular DataAck packet in OPEN state
196 * sCR -> sCR DataAck in CLOSEREQ MAY be processed (8.3.)
197 * sCG -> sCG DataAck in CLOSING MAY be processed (8.3.)
200 * sNO, sRQ, sRS, sPO, sOP, sCR, sCG, sTW */
201 sIV, sIV, sPO, sPO, sOP, sCR, sCG, sIV
203 [DCCP_PKT_CLOSEREQ] = {
205 * CLOSEREQ may only be sent by the server.
207 * sNO, sRQ, sRS, sPO, sOP, sCR, sCG, sTW */
208 sIV, sIV, sIV, sIV, sIV, sIV, sIV, sIV
212 * sNO -> sIV No connection
213 * sRQ -> sIV No connection
214 * sRS -> sIV No connection
215 * sPO -> sCG Client-initiated close
216 * sOP -> sCG Client-initiated close
217 * sCR -> sCG Close in response to CloseReq (8.3.)
218 * sCG -> sCG Retransmit
219 * sTW -> sIV Late retransmit, already in TIME_WAIT
221 * sNO, sRQ, sRS, sPO, sOP, sCR, sCG, sTW */
222 sIV, sIV, sIV, sCG, sCG, sCG, sIV, sIV
226 * sNO -> sIV No connection
227 * sRQ -> sTW Sync received or timeout, SHOULD send Reset (8.1.1.)
228 * sRS -> sTW Response received without Request
229 * sPO -> sTW Timeout, SHOULD send Reset (8.1.5.)
230 * sOP -> sTW Connection reset
231 * sCR -> sTW Connection reset
232 * sCG -> sTW Connection reset
233 * sTW -> sIG Ignore (don't refresh timer)
235 * sNO, sRQ, sRS, sPO, sOP, sCR, sCG, sTW */
236 sIV, sTW, sTW, sTW, sTW, sTW, sTW, sIG
240 * We currently ignore Sync packets
242 * sNO, sRQ, sRS, sPO, sOP, sCR, sCG, sTW */
243 sIV, sIG, sIG, sIG, sIG, sIG, sIG, sIG,
245 [DCCP_PKT_SYNCACK] = {
247 * We currently ignore SyncAck packets
249 * sNO, sRQ, sRS, sPO, sOP, sCR, sCG, sTW */
250 sIV, sIG, sIG, sIG, sIG, sIG, sIG, sIG,
253 [CT_DCCP_ROLE_SERVER] = {
254 [DCCP_PKT_REQUEST] = {
257 * sRQ -> sIG Ignore, conntrack might be out of sync
258 * sRS -> sIG Ignore, conntrack might be out of sync
259 * sPO -> sIG Ignore, conntrack might be out of sync
260 * sOP -> sIG Ignore, conntrack might be out of sync
261 * sCR -> sIG Ignore, conntrack might be out of sync
262 * sCG -> sIG Ignore, conntrack might be out of sync
263 * sTW -> sRQ Reincarnation, must reverse roles
265 * sNO, sRQ, sRS, sPO, sOP, sCR, sCG, sTW */
266 sIV, sIG, sIG, sIG, sIG, sIG, sIG, sRQ
268 [DCCP_PKT_RESPONSE] = {
270 * sNO -> sIV Response without Request
271 * sRQ -> sRS Response to clients Request
272 * sRS -> sRS Retransmitted Response (8.1.3. SHOULD NOT)
273 * sPO -> sIG Response to an ignored Request or late retransmit
274 * sOP -> sIG Ignore, might be response to ignored Request
275 * sCR -> sIG Ignore, might be response to ignored Request
276 * sCG -> sIG Ignore, might be response to ignored Request
277 * sTW -> sIV Invalid, Request from client in sTW moves to sRQ
279 * sNO, sRQ, sRS, sPO, sOP, sCR, sCG, sTW */
280 sIV, sRS, sRS, sIG, sIG, sIG, sIG, sIV
284 * sNO -> sIV No connection
285 * sRQ -> sIV No connection
286 * sRS -> sIV No connection
287 * sPO -> sOP Enter OPEN state (8.1.5.)
288 * sOP -> sOP Regular Ack in OPEN state
289 * sCR -> sIV Waiting for Close from client
290 * sCG -> sCG Ack in CLOSING MAY be processed (8.3.)
293 * sNO, sRQ, sRS, sPO, sOP, sCR, sCG, sTW */
294 sIV, sIV, sIV, sOP, sOP, sIV, sCG, sIV
298 * sNO -> sIV No connection
299 * sRQ -> sIV No connection
300 * sRS -> sIV No connection
301 * sPO -> sOP Enter OPEN state (8.1.5.)
302 * sOP -> sOP Regular Data packet in OPEN state
303 * sCR -> sIV Waiting for Close from client
304 * sCG -> sCG Data in CLOSING MAY be processed (8.3.)
307 * sNO, sRQ, sRS, sPO, sOP, sCR, sCG, sTW */
308 sIV, sIV, sIV, sOP, sOP, sIV, sCG, sIV
310 [DCCP_PKT_DATAACK] = {
312 * sNO -> sIV No connection
313 * sRQ -> sIV No connection
314 * sRS -> sIV No connection
315 * sPO -> sOP Enter OPEN state (8.1.5.)
316 * sOP -> sOP Regular DataAck in OPEN state
317 * sCR -> sIV Waiting for Close from client
318 * sCG -> sCG Data in CLOSING MAY be processed (8.3.)
321 * sNO, sRQ, sRS, sPO, sOP, sCR, sCG, sTW */
322 sIV, sIV, sIV, sOP, sOP, sIV, sCG, sIV
324 [DCCP_PKT_CLOSEREQ] = {
326 * sNO -> sIV No connection
327 * sRQ -> sIV No connection
328 * sRS -> sIV No connection
329 * sPO -> sOP -> sCR Move directly to CLOSEREQ (8.1.5.)
330 * sOP -> sCR CloseReq in OPEN state
331 * sCR -> sCR Retransmit
332 * sCG -> sCR Simultaneous close, client sends another Close
333 * sTW -> sIV Already closed
335 * sNO, sRQ, sRS, sPO, sOP, sCR, sCG, sTW */
336 sIV, sIV, sIV, sCR, sCR, sCR, sCR, sIV
340 * sNO -> sIV No connection
341 * sRQ -> sIV No connection
342 * sRS -> sIV No connection
343 * sPO -> sOP -> sCG Move direcly to CLOSING
344 * sOP -> sCG Move to CLOSING
345 * sCR -> sIV Close after CloseReq is invalid
346 * sCG -> sCG Retransmit
347 * sTW -> sIV Already closed
349 * sNO, sRQ, sRS, sPO, sOP, sCR, sCG, sTW */
350 sIV, sIV, sIV, sCG, sCG, sIV, sCG, sIV
354 * sNO -> sIV No connection
355 * sRQ -> sTW Reset in response to Request
356 * sRS -> sTW Timeout, SHOULD send Reset (8.1.3.)
357 * sPO -> sTW Timeout, SHOULD send Reset (8.1.3.)
361 * sTW -> sIG Ignore (don't refresh timer)
363 * sNO, sRQ, sRS, sPO, sOP, sCR, sCG, sTW, sTW */
364 sIV, sTW, sTW, sTW, sTW, sTW, sTW, sTW, sIG
368 * We currently ignore Sync packets
370 * sNO, sRQ, sRS, sPO, sOP, sCR, sCG, sTW */
371 sIV, sIG, sIG, sIG, sIG, sIG, sIG, sIG,
373 [DCCP_PKT_SYNCACK] = {
375 * We currently ignore SyncAck packets
377 * sNO, sRQ, sRS, sPO, sOP, sCR, sCG, sTW */
378 sIV, sIG, sIG, sIG, sIG, sIG, sIG, sIG,
384 dccp_new(struct nf_conn *ct, const struct sk_buff *skb,
385 const struct dccp_hdr *dh)
387 struct net *net = nf_ct_net(ct);
388 struct nf_dccp_net *dn;
392 state = dccp_state_table[CT_DCCP_ROLE_CLIENT][dh->dccph_type][CT_DCCP_NONE];
395 dn = nf_dccp_pernet(net);
396 if (dn->dccp_loose == 0) {
397 msg = "not picking up existing connection ";
401 case CT_DCCP_REQUEST:
403 case CT_DCCP_INVALID:
404 msg = "invalid state transition ";
408 ct->proto.dccp.role[IP_CT_DIR_ORIGINAL] = CT_DCCP_ROLE_CLIENT;
409 ct->proto.dccp.role[IP_CT_DIR_REPLY] = CT_DCCP_ROLE_SERVER;
410 ct->proto.dccp.state = CT_DCCP_NONE;
411 ct->proto.dccp.last_pkt = DCCP_PKT_REQUEST;
412 ct->proto.dccp.last_dir = IP_CT_DIR_ORIGINAL;
413 ct->proto.dccp.handshake_seq = 0;
417 nf_ct_l4proto_log_invalid(skb, ct, "%s", msg);
421 static u64 dccp_ack_seq(const struct dccp_hdr *dh)
423 const struct dccp_hdr_ack_bits *dhack;
425 dhack = (void *)dh + __dccp_basic_hdr_len(dh);
426 return ((u64)ntohs(dhack->dccph_ack_nr_high) << 32) +
427 ntohl(dhack->dccph_ack_nr_low);
430 static bool dccp_error(const struct dccp_hdr *dh,
431 struct sk_buff *skb, unsigned int dataoff,
432 const struct nf_hook_state *state)
434 static const unsigned long require_seq48 = 1 << DCCP_PKT_REQUEST |
435 1 << DCCP_PKT_RESPONSE |
436 1 << DCCP_PKT_CLOSEREQ |
437 1 << DCCP_PKT_CLOSE |
438 1 << DCCP_PKT_RESET |
440 1 << DCCP_PKT_SYNCACK;
441 unsigned int dccp_len = skb->len - dataoff;
446 BUILD_BUG_ON(DCCP_PKT_INVALID >= BITS_PER_LONG);
448 if (dh->dccph_doff * 4 < sizeof(struct dccp_hdr) ||
449 dh->dccph_doff * 4 > dccp_len) {
450 msg = "nf_ct_dccp: truncated/malformed packet ";
455 if (dh->dccph_cscov) {
456 cscov = (dh->dccph_cscov - 1) * 4;
457 if (cscov > dccp_len) {
458 msg = "nf_ct_dccp: bad checksum coverage ";
463 if (state->hook == NF_INET_PRE_ROUTING &&
464 state->net->ct.sysctl_checksum &&
465 nf_checksum_partial(skb, state->hook, dataoff, cscov,
466 IPPROTO_DCCP, state->pf)) {
467 msg = "nf_ct_dccp: bad checksum ";
471 type = dh->dccph_type;
472 if (type >= DCCP_PKT_INVALID) {
473 msg = "nf_ct_dccp: reserved packet type ";
477 if (test_bit(type, &require_seq48) && !dh->dccph_x) {
478 msg = "nf_ct_dccp: type lacks 48bit sequence numbers";
484 nf_l4proto_log_invalid(skb, state->net, state->pf,
485 IPPROTO_DCCP, "%s", msg);
489 struct nf_conntrack_dccp_buf {
490 struct dccp_hdr dh; /* generic header part */
491 struct dccp_hdr_ext ext; /* optional depending dh->dccph_x */
492 union { /* depends on header type */
493 struct dccp_hdr_ack_bits ack;
494 struct dccp_hdr_request req;
495 struct dccp_hdr_response response;
496 struct dccp_hdr_reset rst;
500 static struct dccp_hdr *
501 dccp_header_pointer(const struct sk_buff *skb, int offset, const struct dccp_hdr *dh,
502 struct nf_conntrack_dccp_buf *buf)
504 unsigned int hdrlen = __dccp_hdr_len(dh);
506 if (hdrlen > sizeof(*buf))
509 return skb_header_pointer(skb, offset, hdrlen, buf);
512 int nf_conntrack_dccp_packet(struct nf_conn *ct, struct sk_buff *skb,
513 unsigned int dataoff,
514 enum ip_conntrack_info ctinfo,
515 const struct nf_hook_state *state)
517 enum ip_conntrack_dir dir = CTINFO2DIR(ctinfo);
518 struct nf_conntrack_dccp_buf _dh;
519 u_int8_t type, old_state, new_state;
520 enum ct_dccp_roles role;
521 unsigned int *timeouts;
524 dh = skb_header_pointer(skb, dataoff, sizeof(*dh), &_dh.dh);
528 if (dccp_error(dh, skb, dataoff, state))
531 /* pull again, including possible 48 bit sequences and subtype header */
532 dh = dccp_header_pointer(skb, dataoff, dh, &_dh);
536 type = dh->dccph_type;
537 if (!nf_ct_is_confirmed(ct) && !dccp_new(ct, skb, dh))
540 if (type == DCCP_PKT_RESET &&
541 !test_bit(IPS_SEEN_REPLY_BIT, &ct->status)) {
542 /* Tear down connection immediately if only reply is a RESET */
543 nf_ct_kill_acct(ct, ctinfo, skb);
547 spin_lock_bh(&ct->lock);
549 role = ct->proto.dccp.role[dir];
550 old_state = ct->proto.dccp.state;
551 new_state = dccp_state_table[role][type][old_state];
554 case CT_DCCP_REQUEST:
555 if (old_state == CT_DCCP_TIMEWAIT &&
556 role == CT_DCCP_ROLE_SERVER) {
557 /* Reincarnation in the reverse direction: reopen and
558 * reverse client/server roles. */
559 ct->proto.dccp.role[dir] = CT_DCCP_ROLE_CLIENT;
560 ct->proto.dccp.role[!dir] = CT_DCCP_ROLE_SERVER;
563 case CT_DCCP_RESPOND:
564 if (old_state == CT_DCCP_REQUEST)
565 ct->proto.dccp.handshake_seq = dccp_hdr_seq(dh);
567 case CT_DCCP_PARTOPEN:
568 if (old_state == CT_DCCP_RESPOND &&
569 type == DCCP_PKT_ACK &&
570 dccp_ack_seq(dh) == ct->proto.dccp.handshake_seq)
571 set_bit(IPS_ASSURED_BIT, &ct->status);
575 * Connection tracking might be out of sync, so we ignore
576 * packets that might establish a new connection and resync
577 * if the server responds with a valid Response.
579 if (ct->proto.dccp.last_dir == !dir &&
580 ct->proto.dccp.last_pkt == DCCP_PKT_REQUEST &&
581 type == DCCP_PKT_RESPONSE) {
582 ct->proto.dccp.role[!dir] = CT_DCCP_ROLE_CLIENT;
583 ct->proto.dccp.role[dir] = CT_DCCP_ROLE_SERVER;
584 ct->proto.dccp.handshake_seq = dccp_hdr_seq(dh);
585 new_state = CT_DCCP_RESPOND;
588 ct->proto.dccp.last_dir = dir;
589 ct->proto.dccp.last_pkt = type;
591 spin_unlock_bh(&ct->lock);
592 nf_ct_l4proto_log_invalid(skb, ct, "%s", "invalid packet");
594 case CT_DCCP_INVALID:
595 spin_unlock_bh(&ct->lock);
596 nf_ct_l4proto_log_invalid(skb, ct, "%s", "invalid state transition");
600 ct->proto.dccp.last_dir = dir;
601 ct->proto.dccp.last_pkt = type;
602 ct->proto.dccp.state = new_state;
603 spin_unlock_bh(&ct->lock);
605 if (new_state != old_state)
606 nf_conntrack_event_cache(IPCT_PROTOINFO, ct);
608 timeouts = nf_ct_timeout_lookup(ct);
610 timeouts = nf_dccp_pernet(nf_ct_net(ct))->dccp_timeout;
611 nf_ct_refresh_acct(ct, ctinfo, skb, timeouts[new_state]);
616 static bool dccp_can_early_drop(const struct nf_conn *ct)
618 switch (ct->proto.dccp.state) {
619 case CT_DCCP_CLOSEREQ:
620 case CT_DCCP_CLOSING:
621 case CT_DCCP_TIMEWAIT:
630 #ifdef CONFIG_NF_CONNTRACK_PROCFS
631 static void dccp_print_conntrack(struct seq_file *s, struct nf_conn *ct)
633 seq_printf(s, "%s ", dccp_state_names[ct->proto.dccp.state]);
637 #if IS_ENABLED(CONFIG_NF_CT_NETLINK)
638 static int dccp_to_nlattr(struct sk_buff *skb, struct nlattr *nla,
641 struct nlattr *nest_parms;
643 spin_lock_bh(&ct->lock);
644 nest_parms = nla_nest_start(skb, CTA_PROTOINFO_DCCP);
646 goto nla_put_failure;
647 if (nla_put_u8(skb, CTA_PROTOINFO_DCCP_STATE, ct->proto.dccp.state) ||
648 nla_put_u8(skb, CTA_PROTOINFO_DCCP_ROLE,
649 ct->proto.dccp.role[IP_CT_DIR_ORIGINAL]) ||
650 nla_put_be64(skb, CTA_PROTOINFO_DCCP_HANDSHAKE_SEQ,
651 cpu_to_be64(ct->proto.dccp.handshake_seq),
652 CTA_PROTOINFO_DCCP_PAD))
653 goto nla_put_failure;
654 nla_nest_end(skb, nest_parms);
655 spin_unlock_bh(&ct->lock);
659 spin_unlock_bh(&ct->lock);
663 static const struct nla_policy dccp_nla_policy[CTA_PROTOINFO_DCCP_MAX + 1] = {
664 [CTA_PROTOINFO_DCCP_STATE] = { .type = NLA_U8 },
665 [CTA_PROTOINFO_DCCP_ROLE] = { .type = NLA_U8 },
666 [CTA_PROTOINFO_DCCP_HANDSHAKE_SEQ] = { .type = NLA_U64 },
667 [CTA_PROTOINFO_DCCP_PAD] = { .type = NLA_UNSPEC },
670 #define DCCP_NLATTR_SIZE ( \
671 NLA_ALIGN(NLA_HDRLEN + 1) + \
672 NLA_ALIGN(NLA_HDRLEN + 1) + \
673 NLA_ALIGN(NLA_HDRLEN + sizeof(u64)) + \
674 NLA_ALIGN(NLA_HDRLEN + 0))
676 static int nlattr_to_dccp(struct nlattr *cda[], struct nf_conn *ct)
678 struct nlattr *attr = cda[CTA_PROTOINFO_DCCP];
679 struct nlattr *tb[CTA_PROTOINFO_DCCP_MAX + 1];
685 err = nla_parse_nested_deprecated(tb, CTA_PROTOINFO_DCCP_MAX, attr,
686 dccp_nla_policy, NULL);
690 if (!tb[CTA_PROTOINFO_DCCP_STATE] ||
691 !tb[CTA_PROTOINFO_DCCP_ROLE] ||
692 nla_get_u8(tb[CTA_PROTOINFO_DCCP_ROLE]) > CT_DCCP_ROLE_MAX ||
693 nla_get_u8(tb[CTA_PROTOINFO_DCCP_STATE]) >= CT_DCCP_IGNORE) {
697 spin_lock_bh(&ct->lock);
698 ct->proto.dccp.state = nla_get_u8(tb[CTA_PROTOINFO_DCCP_STATE]);
699 if (nla_get_u8(tb[CTA_PROTOINFO_DCCP_ROLE]) == CT_DCCP_ROLE_CLIENT) {
700 ct->proto.dccp.role[IP_CT_DIR_ORIGINAL] = CT_DCCP_ROLE_CLIENT;
701 ct->proto.dccp.role[IP_CT_DIR_REPLY] = CT_DCCP_ROLE_SERVER;
703 ct->proto.dccp.role[IP_CT_DIR_ORIGINAL] = CT_DCCP_ROLE_SERVER;
704 ct->proto.dccp.role[IP_CT_DIR_REPLY] = CT_DCCP_ROLE_CLIENT;
706 if (tb[CTA_PROTOINFO_DCCP_HANDSHAKE_SEQ]) {
707 ct->proto.dccp.handshake_seq =
708 be64_to_cpu(nla_get_be64(tb[CTA_PROTOINFO_DCCP_HANDSHAKE_SEQ]));
710 spin_unlock_bh(&ct->lock);
715 #ifdef CONFIG_NF_CONNTRACK_TIMEOUT
717 #include <linux/netfilter/nfnetlink.h>
718 #include <linux/netfilter/nfnetlink_cttimeout.h>
720 static int dccp_timeout_nlattr_to_obj(struct nlattr *tb[],
721 struct net *net, void *data)
723 struct nf_dccp_net *dn = nf_dccp_pernet(net);
724 unsigned int *timeouts = data;
728 timeouts = dn->dccp_timeout;
730 /* set default DCCP timeouts. */
731 for (i=0; i<CT_DCCP_MAX; i++)
732 timeouts[i] = dn->dccp_timeout[i];
734 /* there's a 1:1 mapping between attributes and protocol states. */
735 for (i=CTA_TIMEOUT_DCCP_UNSPEC+1; i<CTA_TIMEOUT_DCCP_MAX+1; i++) {
737 timeouts[i] = ntohl(nla_get_be32(tb[i])) * HZ;
741 timeouts[CTA_TIMEOUT_DCCP_UNSPEC] = timeouts[CTA_TIMEOUT_DCCP_REQUEST];
746 dccp_timeout_obj_to_nlattr(struct sk_buff *skb, const void *data)
748 const unsigned int *timeouts = data;
751 for (i=CTA_TIMEOUT_DCCP_UNSPEC+1; i<CTA_TIMEOUT_DCCP_MAX+1; i++) {
752 if (nla_put_be32(skb, i, htonl(timeouts[i] / HZ)))
753 goto nla_put_failure;
761 static const struct nla_policy
762 dccp_timeout_nla_policy[CTA_TIMEOUT_DCCP_MAX+1] = {
763 [CTA_TIMEOUT_DCCP_REQUEST] = { .type = NLA_U32 },
764 [CTA_TIMEOUT_DCCP_RESPOND] = { .type = NLA_U32 },
765 [CTA_TIMEOUT_DCCP_PARTOPEN] = { .type = NLA_U32 },
766 [CTA_TIMEOUT_DCCP_OPEN] = { .type = NLA_U32 },
767 [CTA_TIMEOUT_DCCP_CLOSEREQ] = { .type = NLA_U32 },
768 [CTA_TIMEOUT_DCCP_CLOSING] = { .type = NLA_U32 },
769 [CTA_TIMEOUT_DCCP_TIMEWAIT] = { .type = NLA_U32 },
771 #endif /* CONFIG_NF_CONNTRACK_TIMEOUT */
773 void nf_conntrack_dccp_init_net(struct net *net)
775 struct nf_dccp_net *dn = nf_dccp_pernet(net);
779 dn->dccp_timeout[CT_DCCP_REQUEST] = 2 * DCCP_MSL;
780 dn->dccp_timeout[CT_DCCP_RESPOND] = 4 * DCCP_MSL;
781 dn->dccp_timeout[CT_DCCP_PARTOPEN] = 4 * DCCP_MSL;
782 dn->dccp_timeout[CT_DCCP_OPEN] = 12 * 3600 * HZ;
783 dn->dccp_timeout[CT_DCCP_CLOSEREQ] = 64 * HZ;
784 dn->dccp_timeout[CT_DCCP_CLOSING] = 64 * HZ;
785 dn->dccp_timeout[CT_DCCP_TIMEWAIT] = 2 * DCCP_MSL;
787 /* timeouts[0] is unused, make it same as SYN_SENT so
788 * ->timeouts[0] contains 'new' timeout, like udp or icmp.
790 dn->dccp_timeout[CT_DCCP_NONE] = dn->dccp_timeout[CT_DCCP_REQUEST];
793 const struct nf_conntrack_l4proto nf_conntrack_l4proto_dccp = {
794 .l4proto = IPPROTO_DCCP,
795 .can_early_drop = dccp_can_early_drop,
796 #ifdef CONFIG_NF_CONNTRACK_PROCFS
797 .print_conntrack = dccp_print_conntrack,
799 #if IS_ENABLED(CONFIG_NF_CT_NETLINK)
800 .nlattr_size = DCCP_NLATTR_SIZE,
801 .to_nlattr = dccp_to_nlattr,
802 .from_nlattr = nlattr_to_dccp,
803 .tuple_to_nlattr = nf_ct_port_tuple_to_nlattr,
804 .nlattr_tuple_size = nf_ct_port_nlattr_tuple_size,
805 .nlattr_to_tuple = nf_ct_port_nlattr_to_tuple,
806 .nla_policy = nf_ct_port_nla_policy,
808 #ifdef CONFIG_NF_CONNTRACK_TIMEOUT
810 .nlattr_to_obj = dccp_timeout_nlattr_to_obj,
811 .obj_to_nlattr = dccp_timeout_obj_to_nlattr,
812 .nlattr_max = CTA_TIMEOUT_DCCP_MAX,
813 .obj_size = sizeof(unsigned int) * CT_DCCP_MAX,
814 .nla_policy = dccp_timeout_nla_policy,
816 #endif /* CONFIG_NF_CONNTRACK_TIMEOUT */