2 * DCCP connection tracking protocol helper
4 * Copyright (c) 2005, 2006, 2008 Patrick McHardy <kaber@trash.net>
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.
11 #include <linux/kernel.h>
12 #include <linux/module.h>
13 #include <linux/init.h>
14 #include <linux/sysctl.h>
15 #include <linux/spinlock.h>
16 #include <linux/skbuff.h>
17 #include <linux/dccp.h>
18 #include <linux/slab.h>
20 #include <net/net_namespace.h>
21 #include <net/netns/generic.h>
23 #include <linux/netfilter/nfnetlink_conntrack.h>
24 #include <net/netfilter/nf_conntrack.h>
25 #include <net/netfilter/nf_conntrack_l4proto.h>
26 #include <net/netfilter/nf_conntrack_ecache.h>
27 #include <net/netfilter/nf_log.h>
29 /* Timeouts are based on values from RFC4340:
33 * 8.1.2. Client Request
35 * A client MAY give up on its DCCP-Requests after some time
36 * (3 minutes, for example).
40 * 8.1.3. Server Response
42 * It MAY also leave the RESPOND state for CLOSED after a timeout of
43 * not less than 4MSL (8 minutes);
47 * 8.1.5. Handshake Completion
49 * If the client remains in PARTOPEN for more than 4MSL (8 minutes),
50 * it SHOULD reset the connection with Reset Code 2, "Aborted".
54 * The DCCP timestamp overflows after 11.9 hours. If the connection
55 * stays idle this long the sequence number won't be recognized
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
70 * A server or client socket remains in this state for 2MSL (4 minutes)
71 * after the connection has been town down, ...
74 #define DCCP_MSL (2 * 60 * HZ)
76 static 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",
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
101 * DCCP state transition table
103 * The assumption is the same as for TCP tracking:
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.
109 * The following states exist:
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
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.
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
132 static const u_int8_t
133 dccp_state_table
[CT_DCCP_ROLE_MAX
+ 1][DCCP_PKT_SYNCACK
+ 1][CT_DCCP_MAX
+ 1] = {
134 [CT_DCCP_ROLE_CLIENT
] = {
135 [DCCP_PKT_REQUEST
] = {
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
147 * sNO, sRQ, sRS, sPO. sOP, sCR, sCG, sTW, */
148 sRQ
, sRQ
, sRS
, sIG
, sIG
, sIG
, sIG
, sRQ
,
150 [DCCP_PKT_RESPONSE
] = {
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
162 * sNO, sRQ, sRS, sPO, sOP, sCR, sCG, sTW */
163 sIV
, sIG
, sIG
, sIG
, sIG
, sIG
, sIG
, sIV
,
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.)
176 * sNO, sRQ, sRS, sPO, sOP, sCR, sCG, sTW */
177 sIV
, sIV
, sPO
, sPO
, sOP
, sCR
, sCG
, sIV
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.)
190 * sNO, sRQ, sRS, sPO, sOP, sCR, sCG, sTW */
191 sIV
, sIV
, sIV
, sIV
, sOP
, sCR
, sCG
, sIV
,
193 [DCCP_PKT_DATAACK
] = {
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.)
204 * sNO, sRQ, sRS, sPO, sOP, sCR, sCG, sTW */
205 sIV
, sIV
, sPO
, sPO
, sOP
, sCR
, sCG
, sIV
207 [DCCP_PKT_CLOSEREQ
] = {
209 * CLOSEREQ may only be sent by the server.
211 * sNO, sRQ, sRS, sPO, sOP, sCR, sCG, sTW */
212 sIV
, sIV
, sIV
, sIV
, sIV
, sIV
, sIV
, sIV
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
225 * sNO, sRQ, sRS, sPO, sOP, sCR, sCG, sTW */
226 sIV
, sIV
, sIV
, sCG
, sCG
, sCG
, sIV
, sIV
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)
239 * sNO, sRQ, sRS, sPO, sOP, sCR, sCG, sTW */
240 sIV
, sTW
, sTW
, sTW
, sTW
, sTW
, sTW
, sIG
244 * We currently ignore Sync packets
246 * sNO, sRQ, sRS, sPO, sOP, sCR, sCG, sTW */
247 sIG
, sIG
, sIG
, sIG
, sIG
, sIG
, sIG
, sIG
,
249 [DCCP_PKT_SYNCACK
] = {
251 * We currently ignore SyncAck packets
253 * sNO, sRQ, sRS, sPO, sOP, sCR, sCG, sTW */
254 sIG
, sIG
, sIG
, sIG
, sIG
, sIG
, sIG
, sIG
,
257 [CT_DCCP_ROLE_SERVER
] = {
258 [DCCP_PKT_REQUEST
] = {
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
269 * sNO, sRQ, sRS, sPO, sOP, sCR, sCG, sTW */
270 sIV
, sIG
, sIG
, sIG
, sIG
, sIG
, sIG
, sRQ
272 [DCCP_PKT_RESPONSE
] = {
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
283 * sNO, sRQ, sRS, sPO, sOP, sCR, sCG, sTW */
284 sIV
, sRS
, sRS
, sIG
, sIG
, sIG
, sIG
, sIV
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.)
297 * sNO, sRQ, sRS, sPO, sOP, sCR, sCG, sTW */
298 sIV
, sIV
, sIV
, sOP
, sOP
, sIV
, sCG
, sIV
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.)
311 * sNO, sRQ, sRS, sPO, sOP, sCR, sCG, sTW */
312 sIV
, sIV
, sIV
, sOP
, sOP
, sIV
, sCG
, sIV
314 [DCCP_PKT_DATAACK
] = {
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.)
325 * sNO, sRQ, sRS, sPO, sOP, sCR, sCG, sTW */
326 sIV
, sIV
, sIV
, sOP
, sOP
, sIV
, sCG
, sIV
328 [DCCP_PKT_CLOSEREQ
] = {
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
339 * sNO, sRQ, sRS, sPO, sOP, sCR, sCG, sTW */
340 sIV
, sIV
, sIV
, sCR
, sCR
, sCR
, sCR
, sIV
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
353 * sNO, sRQ, sRS, sPO, sOP, sCR, sCG, sTW */
354 sIV
, sIV
, sIV
, sCG
, sCG
, sIV
, sCG
, sIV
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.)
365 * sTW -> sIG Ignore (don't refresh timer)
367 * sNO, sRQ, sRS, sPO, sOP, sCR, sCG, sTW, sTW */
368 sIV
, sTW
, sTW
, sTW
, sTW
, sTW
, sTW
, sTW
, sIG
372 * We currently ignore Sync packets
374 * sNO, sRQ, sRS, sPO, sOP, sCR, sCG, sTW */
375 sIG
, sIG
, sIG
, sIG
, sIG
, sIG
, sIG
, sIG
,
377 [DCCP_PKT_SYNCACK
] = {
379 * We currently ignore SyncAck packets
381 * sNO, sRQ, sRS, sPO, sOP, sCR, sCG, sTW */
382 sIG
, sIG
, sIG
, sIG
, sIG
, sIG
, sIG
, sIG
,
387 /* this module per-net specifics */
388 static int dccp_net_id __read_mostly
;
391 unsigned int dccp_timeout
[CT_DCCP_MAX
+ 1];
393 struct ctl_table_header
*sysctl_header
;
394 struct ctl_table
*sysctl_table
;
398 static inline struct dccp_net
*dccp_pernet(struct net
*net
)
400 return net_generic(net
, dccp_net_id
);
403 static bool dccp_pkt_to_tuple(const struct sk_buff
*skb
, unsigned int dataoff
,
404 struct nf_conntrack_tuple
*tuple
)
406 struct dccp_hdr _hdr
, *dh
;
408 dh
= skb_header_pointer(skb
, dataoff
, sizeof(_hdr
), &_hdr
);
412 tuple
->src
.u
.dccp
.port
= dh
->dccph_sport
;
413 tuple
->dst
.u
.dccp
.port
= dh
->dccph_dport
;
417 static bool dccp_invert_tuple(struct nf_conntrack_tuple
*inv
,
418 const struct nf_conntrack_tuple
*tuple
)
420 inv
->src
.u
.dccp
.port
= tuple
->dst
.u
.dccp
.port
;
421 inv
->dst
.u
.dccp
.port
= tuple
->src
.u
.dccp
.port
;
425 static bool dccp_new(struct nf_conn
*ct
, const struct sk_buff
*skb
,
426 unsigned int dataoff
, unsigned int *timeouts
)
428 struct net
*net
= nf_ct_net(ct
);
430 struct dccp_hdr _dh
, *dh
;
434 dh
= skb_header_pointer(skb
, dataoff
, sizeof(_dh
), &dh
);
437 state
= dccp_state_table
[CT_DCCP_ROLE_CLIENT
][dh
->dccph_type
][CT_DCCP_NONE
];
440 dn
= dccp_pernet(net
);
441 if (dn
->dccp_loose
== 0) {
442 msg
= "nf_ct_dccp: not picking up existing connection ";
445 case CT_DCCP_REQUEST
:
447 case CT_DCCP_INVALID
:
448 msg
= "nf_ct_dccp: invalid state transition ";
452 ct
->proto
.dccp
.role
[IP_CT_DIR_ORIGINAL
] = CT_DCCP_ROLE_CLIENT
;
453 ct
->proto
.dccp
.role
[IP_CT_DIR_REPLY
] = CT_DCCP_ROLE_SERVER
;
454 ct
->proto
.dccp
.state
= CT_DCCP_NONE
;
455 ct
->proto
.dccp
.last_pkt
= DCCP_PKT_REQUEST
;
456 ct
->proto
.dccp
.last_dir
= IP_CT_DIR_ORIGINAL
;
457 ct
->proto
.dccp
.handshake_seq
= 0;
461 if (LOG_INVALID(net
, IPPROTO_DCCP
))
462 nf_log_packet(nf_ct_l3num(ct
), 0, skb
, NULL
, NULL
, NULL
, msg
);
466 static u64
dccp_ack_seq(const struct dccp_hdr
*dh
)
468 const struct dccp_hdr_ack_bits
*dhack
;
470 dhack
= (void *)dh
+ __dccp_basic_hdr_len(dh
);
471 return ((u64
)ntohs(dhack
->dccph_ack_nr_high
) << 32) +
472 ntohl(dhack
->dccph_ack_nr_low
);
475 static unsigned int *dccp_get_timeouts(struct net
*net
)
477 return dccp_pernet(net
)->dccp_timeout
;
480 static int dccp_packet(struct nf_conn
*ct
, const struct sk_buff
*skb
,
481 unsigned int dataoff
, enum ip_conntrack_info ctinfo
,
482 u_int8_t pf
, unsigned int hooknum
,
483 unsigned int *timeouts
)
485 struct net
*net
= nf_ct_net(ct
);
486 enum ip_conntrack_dir dir
= CTINFO2DIR(ctinfo
);
487 struct dccp_hdr _dh
, *dh
;
488 u_int8_t type
, old_state
, new_state
;
489 enum ct_dccp_roles role
;
491 dh
= skb_header_pointer(skb
, dataoff
, sizeof(_dh
), &dh
);
493 type
= dh
->dccph_type
;
495 if (type
== DCCP_PKT_RESET
&&
496 !test_bit(IPS_SEEN_REPLY_BIT
, &ct
->status
)) {
497 /* Tear down connection immediately if only reply is a RESET */
498 nf_ct_kill_acct(ct
, ctinfo
, skb
);
502 spin_lock_bh(&ct
->lock
);
504 role
= ct
->proto
.dccp
.role
[dir
];
505 old_state
= ct
->proto
.dccp
.state
;
506 new_state
= dccp_state_table
[role
][type
][old_state
];
509 case CT_DCCP_REQUEST
:
510 if (old_state
== CT_DCCP_TIMEWAIT
&&
511 role
== CT_DCCP_ROLE_SERVER
) {
512 /* Reincarnation in the reverse direction: reopen and
513 * reverse client/server roles. */
514 ct
->proto
.dccp
.role
[dir
] = CT_DCCP_ROLE_CLIENT
;
515 ct
->proto
.dccp
.role
[!dir
] = CT_DCCP_ROLE_SERVER
;
518 case CT_DCCP_RESPOND
:
519 if (old_state
== CT_DCCP_REQUEST
)
520 ct
->proto
.dccp
.handshake_seq
= dccp_hdr_seq(dh
);
522 case CT_DCCP_PARTOPEN
:
523 if (old_state
== CT_DCCP_RESPOND
&&
524 type
== DCCP_PKT_ACK
&&
525 dccp_ack_seq(dh
) == ct
->proto
.dccp
.handshake_seq
)
526 set_bit(IPS_ASSURED_BIT
, &ct
->status
);
530 * Connection tracking might be out of sync, so we ignore
531 * packets that might establish a new connection and resync
532 * if the server responds with a valid Response.
534 if (ct
->proto
.dccp
.last_dir
== !dir
&&
535 ct
->proto
.dccp
.last_pkt
== DCCP_PKT_REQUEST
&&
536 type
== DCCP_PKT_RESPONSE
) {
537 ct
->proto
.dccp
.role
[!dir
] = CT_DCCP_ROLE_CLIENT
;
538 ct
->proto
.dccp
.role
[dir
] = CT_DCCP_ROLE_SERVER
;
539 ct
->proto
.dccp
.handshake_seq
= dccp_hdr_seq(dh
);
540 new_state
= CT_DCCP_RESPOND
;
543 ct
->proto
.dccp
.last_dir
= dir
;
544 ct
->proto
.dccp
.last_pkt
= type
;
546 spin_unlock_bh(&ct
->lock
);
547 if (LOG_INVALID(net
, IPPROTO_DCCP
))
548 nf_log_packet(pf
, 0, skb
, NULL
, NULL
, NULL
,
549 "nf_ct_dccp: invalid packet ignored ");
551 case CT_DCCP_INVALID
:
552 spin_unlock_bh(&ct
->lock
);
553 if (LOG_INVALID(net
, IPPROTO_DCCP
))
554 nf_log_packet(pf
, 0, skb
, NULL
, NULL
, NULL
,
555 "nf_ct_dccp: invalid state transition ");
559 ct
->proto
.dccp
.last_dir
= dir
;
560 ct
->proto
.dccp
.last_pkt
= type
;
561 ct
->proto
.dccp
.state
= new_state
;
562 spin_unlock_bh(&ct
->lock
);
564 if (new_state
!= old_state
)
565 nf_conntrack_event_cache(IPCT_PROTOINFO
, ct
);
567 nf_ct_refresh_acct(ct
, ctinfo
, skb
, timeouts
[new_state
]);
572 static int dccp_error(struct net
*net
, struct nf_conn
*tmpl
,
573 struct sk_buff
*skb
, unsigned int dataoff
,
574 enum ip_conntrack_info
*ctinfo
,
575 u_int8_t pf
, unsigned int hooknum
)
577 struct dccp_hdr _dh
, *dh
;
578 unsigned int dccp_len
= skb
->len
- dataoff
;
582 dh
= skb_header_pointer(skb
, dataoff
, sizeof(_dh
), &dh
);
584 msg
= "nf_ct_dccp: short packet ";
588 if (dh
->dccph_doff
* 4 < sizeof(struct dccp_hdr
) ||
589 dh
->dccph_doff
* 4 > dccp_len
) {
590 msg
= "nf_ct_dccp: truncated/malformed packet ";
595 if (dh
->dccph_cscov
) {
596 cscov
= (dh
->dccph_cscov
- 1) * 4;
597 if (cscov
> dccp_len
) {
598 msg
= "nf_ct_dccp: bad checksum coverage ";
603 if (net
->ct
.sysctl_checksum
&& hooknum
== NF_INET_PRE_ROUTING
&&
604 nf_checksum_partial(skb
, hooknum
, dataoff
, cscov
, IPPROTO_DCCP
,
606 msg
= "nf_ct_dccp: bad checksum ";
610 if (dh
->dccph_type
>= DCCP_PKT_INVALID
) {
611 msg
= "nf_ct_dccp: reserved packet type ";
618 if (LOG_INVALID(net
, IPPROTO_DCCP
))
619 nf_log_packet(pf
, 0, skb
, NULL
, NULL
, NULL
, msg
);
623 static int dccp_print_tuple(struct seq_file
*s
,
624 const struct nf_conntrack_tuple
*tuple
)
626 return seq_printf(s
, "sport=%hu dport=%hu ",
627 ntohs(tuple
->src
.u
.dccp
.port
),
628 ntohs(tuple
->dst
.u
.dccp
.port
));
631 static int dccp_print_conntrack(struct seq_file
*s
, struct nf_conn
*ct
)
633 return seq_printf(s
, "%s ", dccp_state_names
[ct
->proto
.dccp
.state
]);
636 #if IS_ENABLED(CONFIG_NF_CT_NETLINK)
637 static int dccp_to_nlattr(struct sk_buff
*skb
, struct nlattr
*nla
,
640 struct nlattr
*nest_parms
;
642 spin_lock_bh(&ct
->lock
);
643 nest_parms
= nla_nest_start(skb
, CTA_PROTOINFO_DCCP
| NLA_F_NESTED
);
645 goto nla_put_failure
;
646 if (nla_put_u8(skb
, CTA_PROTOINFO_DCCP_STATE
, ct
->proto
.dccp
.state
) ||
647 nla_put_u8(skb
, CTA_PROTOINFO_DCCP_ROLE
,
648 ct
->proto
.dccp
.role
[IP_CT_DIR_ORIGINAL
]) ||
649 nla_put_be64(skb
, CTA_PROTOINFO_DCCP_HANDSHAKE_SEQ
,
650 cpu_to_be64(ct
->proto
.dccp
.handshake_seq
)))
651 goto nla_put_failure
;
652 nla_nest_end(skb
, nest_parms
);
653 spin_unlock_bh(&ct
->lock
);
657 spin_unlock_bh(&ct
->lock
);
661 static const struct nla_policy dccp_nla_policy
[CTA_PROTOINFO_DCCP_MAX
+ 1] = {
662 [CTA_PROTOINFO_DCCP_STATE
] = { .type
= NLA_U8
},
663 [CTA_PROTOINFO_DCCP_ROLE
] = { .type
= NLA_U8
},
664 [CTA_PROTOINFO_DCCP_HANDSHAKE_SEQ
] = { .type
= NLA_U64
},
667 static int nlattr_to_dccp(struct nlattr
*cda
[], struct nf_conn
*ct
)
669 struct nlattr
*attr
= cda
[CTA_PROTOINFO_DCCP
];
670 struct nlattr
*tb
[CTA_PROTOINFO_DCCP_MAX
+ 1];
676 err
= nla_parse_nested(tb
, CTA_PROTOINFO_DCCP_MAX
, attr
,
681 if (!tb
[CTA_PROTOINFO_DCCP_STATE
] ||
682 !tb
[CTA_PROTOINFO_DCCP_ROLE
] ||
683 nla_get_u8(tb
[CTA_PROTOINFO_DCCP_ROLE
]) > CT_DCCP_ROLE_MAX
||
684 nla_get_u8(tb
[CTA_PROTOINFO_DCCP_STATE
]) >= CT_DCCP_IGNORE
) {
688 spin_lock_bh(&ct
->lock
);
689 ct
->proto
.dccp
.state
= nla_get_u8(tb
[CTA_PROTOINFO_DCCP_STATE
]);
690 if (nla_get_u8(tb
[CTA_PROTOINFO_DCCP_ROLE
]) == CT_DCCP_ROLE_CLIENT
) {
691 ct
->proto
.dccp
.role
[IP_CT_DIR_ORIGINAL
] = CT_DCCP_ROLE_CLIENT
;
692 ct
->proto
.dccp
.role
[IP_CT_DIR_REPLY
] = CT_DCCP_ROLE_SERVER
;
694 ct
->proto
.dccp
.role
[IP_CT_DIR_ORIGINAL
] = CT_DCCP_ROLE_SERVER
;
695 ct
->proto
.dccp
.role
[IP_CT_DIR_REPLY
] = CT_DCCP_ROLE_CLIENT
;
697 if (tb
[CTA_PROTOINFO_DCCP_HANDSHAKE_SEQ
]) {
698 ct
->proto
.dccp
.handshake_seq
=
699 be64_to_cpu(nla_get_be64(tb
[CTA_PROTOINFO_DCCP_HANDSHAKE_SEQ
]));
701 spin_unlock_bh(&ct
->lock
);
705 static int dccp_nlattr_size(void)
707 return nla_total_size(0) /* CTA_PROTOINFO_DCCP */
708 + nla_policy_len(dccp_nla_policy
, CTA_PROTOINFO_DCCP_MAX
+ 1);
713 #if IS_ENABLED(CONFIG_NF_CT_NETLINK_TIMEOUT)
715 #include <linux/netfilter/nfnetlink.h>
716 #include <linux/netfilter/nfnetlink_cttimeout.h>
718 static int dccp_timeout_nlattr_to_obj(struct nlattr
*tb
[], void *data
)
720 struct dccp_net
*dn
= dccp_pernet(&init_net
);
721 unsigned int *timeouts
= data
;
724 /* set default DCCP timeouts. */
725 for (i
=0; i
<CT_DCCP_MAX
; i
++)
726 timeouts
[i
] = dn
->dccp_timeout
[i
];
728 /* there's a 1:1 mapping between attributes and protocol states. */
729 for (i
=CTA_TIMEOUT_DCCP_UNSPEC
+1; i
<CTA_TIMEOUT_DCCP_MAX
+1; i
++) {
731 timeouts
[i
] = ntohl(nla_get_be32(tb
[i
])) * HZ
;
738 dccp_timeout_obj_to_nlattr(struct sk_buff
*skb
, const void *data
)
740 const unsigned int *timeouts
= data
;
743 for (i
=CTA_TIMEOUT_DCCP_UNSPEC
+1; i
<CTA_TIMEOUT_DCCP_MAX
+1; i
++) {
744 if (nla_put_be32(skb
, i
, htonl(timeouts
[i
] / HZ
)))
745 goto nla_put_failure
;
753 static const struct nla_policy
754 dccp_timeout_nla_policy
[CTA_TIMEOUT_DCCP_MAX
+1] = {
755 [CTA_TIMEOUT_DCCP_REQUEST
] = { .type
= NLA_U32
},
756 [CTA_TIMEOUT_DCCP_RESPOND
] = { .type
= NLA_U32
},
757 [CTA_TIMEOUT_DCCP_PARTOPEN
] = { .type
= NLA_U32
},
758 [CTA_TIMEOUT_DCCP_OPEN
] = { .type
= NLA_U32
},
759 [CTA_TIMEOUT_DCCP_CLOSEREQ
] = { .type
= NLA_U32
},
760 [CTA_TIMEOUT_DCCP_CLOSING
] = { .type
= NLA_U32
},
761 [CTA_TIMEOUT_DCCP_TIMEWAIT
] = { .type
= NLA_U32
},
763 #endif /* CONFIG_NF_CT_NETLINK_TIMEOUT */
766 /* template, data assigned later */
767 static struct ctl_table dccp_sysctl_table
[] = {
769 .procname
= "nf_conntrack_dccp_timeout_request",
770 .maxlen
= sizeof(unsigned int),
772 .proc_handler
= proc_dointvec_jiffies
,
775 .procname
= "nf_conntrack_dccp_timeout_respond",
776 .maxlen
= sizeof(unsigned int),
778 .proc_handler
= proc_dointvec_jiffies
,
781 .procname
= "nf_conntrack_dccp_timeout_partopen",
782 .maxlen
= sizeof(unsigned int),
784 .proc_handler
= proc_dointvec_jiffies
,
787 .procname
= "nf_conntrack_dccp_timeout_open",
788 .maxlen
= sizeof(unsigned int),
790 .proc_handler
= proc_dointvec_jiffies
,
793 .procname
= "nf_conntrack_dccp_timeout_closereq",
794 .maxlen
= sizeof(unsigned int),
796 .proc_handler
= proc_dointvec_jiffies
,
799 .procname
= "nf_conntrack_dccp_timeout_closing",
800 .maxlen
= sizeof(unsigned int),
802 .proc_handler
= proc_dointvec_jiffies
,
805 .procname
= "nf_conntrack_dccp_timeout_timewait",
806 .maxlen
= sizeof(unsigned int),
808 .proc_handler
= proc_dointvec_jiffies
,
811 .procname
= "nf_conntrack_dccp_loose",
812 .maxlen
= sizeof(int),
814 .proc_handler
= proc_dointvec
,
818 #endif /* CONFIG_SYSCTL */
820 static struct nf_conntrack_l4proto dccp_proto4 __read_mostly
= {
822 .l4proto
= IPPROTO_DCCP
,
824 .pkt_to_tuple
= dccp_pkt_to_tuple
,
825 .invert_tuple
= dccp_invert_tuple
,
827 .packet
= dccp_packet
,
828 .get_timeouts
= dccp_get_timeouts
,
830 .print_tuple
= dccp_print_tuple
,
831 .print_conntrack
= dccp_print_conntrack
,
832 #if IS_ENABLED(CONFIG_NF_CT_NETLINK)
833 .to_nlattr
= dccp_to_nlattr
,
834 .nlattr_size
= dccp_nlattr_size
,
835 .from_nlattr
= nlattr_to_dccp
,
836 .tuple_to_nlattr
= nf_ct_port_tuple_to_nlattr
,
837 .nlattr_tuple_size
= nf_ct_port_nlattr_tuple_size
,
838 .nlattr_to_tuple
= nf_ct_port_nlattr_to_tuple
,
839 .nla_policy
= nf_ct_port_nla_policy
,
841 #if IS_ENABLED(CONFIG_NF_CT_NETLINK_TIMEOUT)
843 .nlattr_to_obj
= dccp_timeout_nlattr_to_obj
,
844 .obj_to_nlattr
= dccp_timeout_obj_to_nlattr
,
845 .nlattr_max
= CTA_TIMEOUT_DCCP_MAX
,
846 .obj_size
= sizeof(unsigned int) * CT_DCCP_MAX
,
847 .nla_policy
= dccp_timeout_nla_policy
,
849 #endif /* CONFIG_NF_CT_NETLINK_TIMEOUT */
852 static struct nf_conntrack_l4proto dccp_proto6 __read_mostly
= {
854 .l4proto
= IPPROTO_DCCP
,
856 .pkt_to_tuple
= dccp_pkt_to_tuple
,
857 .invert_tuple
= dccp_invert_tuple
,
859 .packet
= dccp_packet
,
860 .get_timeouts
= dccp_get_timeouts
,
862 .print_tuple
= dccp_print_tuple
,
863 .print_conntrack
= dccp_print_conntrack
,
864 #if IS_ENABLED(CONFIG_NF_CT_NETLINK)
865 .to_nlattr
= dccp_to_nlattr
,
866 .nlattr_size
= dccp_nlattr_size
,
867 .from_nlattr
= nlattr_to_dccp
,
868 .tuple_to_nlattr
= nf_ct_port_tuple_to_nlattr
,
869 .nlattr_tuple_size
= nf_ct_port_nlattr_tuple_size
,
870 .nlattr_to_tuple
= nf_ct_port_nlattr_to_tuple
,
871 .nla_policy
= nf_ct_port_nla_policy
,
873 #if IS_ENABLED(CONFIG_NF_CT_NETLINK_TIMEOUT)
875 .nlattr_to_obj
= dccp_timeout_nlattr_to_obj
,
876 .obj_to_nlattr
= dccp_timeout_obj_to_nlattr
,
877 .nlattr_max
= CTA_TIMEOUT_DCCP_MAX
,
878 .obj_size
= sizeof(unsigned int) * CT_DCCP_MAX
,
879 .nla_policy
= dccp_timeout_nla_policy
,
881 #endif /* CONFIG_NF_CT_NETLINK_TIMEOUT */
884 static __net_init
int dccp_net_init(struct net
*net
)
886 struct dccp_net
*dn
= dccp_pernet(net
);
890 dn
->dccp_timeout
[CT_DCCP_REQUEST
] = 2 * DCCP_MSL
;
891 dn
->dccp_timeout
[CT_DCCP_RESPOND
] = 4 * DCCP_MSL
;
892 dn
->dccp_timeout
[CT_DCCP_PARTOPEN
] = 4 * DCCP_MSL
;
893 dn
->dccp_timeout
[CT_DCCP_OPEN
] = 12 * 3600 * HZ
;
894 dn
->dccp_timeout
[CT_DCCP_CLOSEREQ
] = 64 * HZ
;
895 dn
->dccp_timeout
[CT_DCCP_CLOSING
] = 64 * HZ
;
896 dn
->dccp_timeout
[CT_DCCP_TIMEWAIT
] = 2 * DCCP_MSL
;
899 dn
->sysctl_table
= kmemdup(dccp_sysctl_table
,
900 sizeof(dccp_sysctl_table
), GFP_KERNEL
);
901 if (!dn
->sysctl_table
)
904 dn
->sysctl_table
[0].data
= &dn
->dccp_timeout
[CT_DCCP_REQUEST
];
905 dn
->sysctl_table
[1].data
= &dn
->dccp_timeout
[CT_DCCP_RESPOND
];
906 dn
->sysctl_table
[2].data
= &dn
->dccp_timeout
[CT_DCCP_PARTOPEN
];
907 dn
->sysctl_table
[3].data
= &dn
->dccp_timeout
[CT_DCCP_OPEN
];
908 dn
->sysctl_table
[4].data
= &dn
->dccp_timeout
[CT_DCCP_CLOSEREQ
];
909 dn
->sysctl_table
[5].data
= &dn
->dccp_timeout
[CT_DCCP_CLOSING
];
910 dn
->sysctl_table
[6].data
= &dn
->dccp_timeout
[CT_DCCP_TIMEWAIT
];
911 dn
->sysctl_table
[7].data
= &dn
->dccp_loose
;
913 dn
->sysctl_header
= register_net_sysctl_table(net
,
914 nf_net_netfilter_sysctl_path
, dn
->sysctl_table
);
915 if (!dn
->sysctl_header
) {
916 kfree(dn
->sysctl_table
);
924 static __net_exit
void dccp_net_exit(struct net
*net
)
926 struct dccp_net
*dn
= dccp_pernet(net
);
928 unregister_net_sysctl_table(dn
->sysctl_header
);
929 kfree(dn
->sysctl_table
);
933 static struct pernet_operations dccp_net_ops
= {
934 .init
= dccp_net_init
,
935 .exit
= dccp_net_exit
,
937 .size
= sizeof(struct dccp_net
),
940 static int __init
nf_conntrack_proto_dccp_init(void)
944 err
= register_pernet_subsys(&dccp_net_ops
);
948 err
= nf_conntrack_l4proto_register(&dccp_proto4
);
952 err
= nf_conntrack_l4proto_register(&dccp_proto6
);
958 nf_conntrack_l4proto_unregister(&dccp_proto4
);
960 unregister_pernet_subsys(&dccp_net_ops
);
965 static void __exit
nf_conntrack_proto_dccp_fini(void)
967 unregister_pernet_subsys(&dccp_net_ops
);
968 nf_conntrack_l4proto_unregister(&dccp_proto6
);
969 nf_conntrack_l4proto_unregister(&dccp_proto4
);
972 module_init(nf_conntrack_proto_dccp_init
);
973 module_exit(nf_conntrack_proto_dccp_fini
);
975 MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
976 MODULE_DESCRIPTION("DCCP connection tracking protocol helper");
977 MODULE_LICENSE("GPL");