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>
19 #include <net/net_namespace.h>
20 #include <net/netns/generic.h>
22 #include <linux/netfilter/nfnetlink_conntrack.h>
23 #include <net/netfilter/nf_conntrack.h>
24 #include <net/netfilter/nf_conntrack_l4proto.h>
25 #include <net/netfilter/nf_log.h>
27 /* Timeouts are based on values from RFC4340:
31 * 8.1.2. Client Request
33 * A client MAY give up on its DCCP-Requests after some time
34 * (3 minutes, for example).
38 * 8.1.3. Server Response
40 * It MAY also leave the RESPOND state for CLOSED after a timeout of
41 * not less than 4MSL (8 minutes);
45 * 8.1.5. Handshake Completion
47 * If the client remains in PARTOPEN for more than 4MSL (8 minutes),
48 * it SHOULD reset the connection with Reset Code 2, "Aborted".
52 * The DCCP timestamp overflows after 11.9 hours. If the connection
53 * stays idle this long the sequence number won't be recognized
60 * The retransmission timer should initially be set to go off in two
61 * round-trip times and should back off to not less than once every
68 * A server or client socket remains in this state for 2MSL (4 minutes)
69 * after the connection has been town down, ...
72 #define DCCP_MSL (2 * 60 * HZ)
74 static const char * const dccp_state_names[] = {
75 [CT_DCCP_NONE] = "NONE",
76 [CT_DCCP_REQUEST] = "REQUEST",
77 [CT_DCCP_RESPOND] = "RESPOND",
78 [CT_DCCP_PARTOPEN] = "PARTOPEN",
79 [CT_DCCP_OPEN] = "OPEN",
80 [CT_DCCP_CLOSEREQ] = "CLOSEREQ",
81 [CT_DCCP_CLOSING] = "CLOSING",
82 [CT_DCCP_TIMEWAIT] = "TIMEWAIT",
83 [CT_DCCP_IGNORE] = "IGNORE",
84 [CT_DCCP_INVALID] = "INVALID",
87 #define sNO CT_DCCP_NONE
88 #define sRQ CT_DCCP_REQUEST
89 #define sRS CT_DCCP_RESPOND
90 #define sPO CT_DCCP_PARTOPEN
91 #define sOP CT_DCCP_OPEN
92 #define sCR CT_DCCP_CLOSEREQ
93 #define sCG CT_DCCP_CLOSING
94 #define sTW CT_DCCP_TIMEWAIT
95 #define sIG CT_DCCP_IGNORE
96 #define sIV CT_DCCP_INVALID
99 * DCCP state transistion table
101 * The assumption is the same as for TCP tracking:
103 * We are the man in the middle. All the packets go through us but might
104 * get lost in transit to the destination. It is assumed that the destination
105 * can't receive segments we haven't seen.
107 * The following states exist:
109 * NONE: Initial state, expecting Request
110 * REQUEST: Request seen, waiting for Response from server
111 * RESPOND: Response from server seen, waiting for Ack from client
112 * PARTOPEN: Ack after Response seen, waiting for packet other than Response,
113 * Reset or Sync from server
114 * OPEN: Packet other than Response, Reset or Sync seen
115 * CLOSEREQ: CloseReq from server seen, expecting Close from client
116 * CLOSING: Close seen, expecting Reset
117 * TIMEWAIT: Reset seen
118 * IGNORE: Not determinable whether packet is valid
120 * Some states exist only on one side of the connection: REQUEST, RESPOND,
121 * PARTOPEN, CLOSEREQ. For the other side these states are equivalent to
122 * the one it was in before.
124 * Packets are marked as ignored (sIG) if we don't know if they're valid
125 * (for example a reincarnation of a connection we didn't notice is dead
126 * already) and the server may send back a connection closing Reset or a
127 * Response. They're also used for Sync/SyncAck packets, which we don't
130 static const u_int8_t
131 dccp_state_table[CT_DCCP_ROLE_MAX + 1][DCCP_PKT_SYNCACK + 1][CT_DCCP_MAX + 1] = {
132 [CT_DCCP_ROLE_CLIENT] = {
133 [DCCP_PKT_REQUEST] = {
135 * sNO -> sRQ Regular Request
136 * sRQ -> sRQ Retransmitted Request or reincarnation
137 * sRS -> sRS Retransmitted Request (apparently Response
138 * got lost after we saw it) or reincarnation
139 * sPO -> sIG Ignore, conntrack might be out of sync
140 * sOP -> sIG Ignore, conntrack might be out of sync
141 * sCR -> sIG Ignore, conntrack might be out of sync
142 * sCG -> sIG Ignore, conntrack might be out of sync
143 * sTW -> sRQ Reincarnation
145 * sNO, sRQ, sRS, sPO. sOP, sCR, sCG, sTW, */
146 sRQ, sRQ, sRS, sIG, sIG, sIG, sIG, sRQ,
148 [DCCP_PKT_RESPONSE] = {
151 * sRQ -> sIG Ignore, might be response to ignored Request
152 * sRS -> sIG Ignore, might be response to ignored Request
153 * sPO -> sIG Ignore, might be response to ignored Request
154 * sOP -> sIG Ignore, might be response to ignored Request
155 * sCR -> sIG Ignore, might be response to ignored Request
156 * sCG -> sIG Ignore, might be response to ignored Request
157 * sTW -> sIV Invalid, reincarnation in reverse direction
160 * sNO, sRQ, sRS, sPO, sOP, sCR, sCG, sTW */
161 sIV, sIG, sIG, sIG, sIG, sIG, sIG, sIV,
165 * sNO -> sIV No connection
166 * sRQ -> sIV No connection
167 * sRS -> sPO Ack for Response, move to PARTOPEN (8.1.5.)
168 * sPO -> sPO Retransmitted Ack for Response, remain in PARTOPEN
169 * sOP -> sOP Regular ACK, remain in OPEN
170 * sCR -> sCR Ack in CLOSEREQ MAY be processed (8.3.)
171 * sCG -> sCG Ack in CLOSING MAY be processed (8.3.)
174 * sNO, sRQ, sRS, sPO, sOP, sCR, sCG, sTW */
175 sIV, sIV, sPO, sPO, sOP, sCR, sCG, sIV
179 * sNO -> sIV No connection
180 * sRQ -> sIV No connection
181 * sRS -> sIV No connection
182 * sPO -> sIV MUST use DataAck in PARTOPEN state (8.1.5.)
183 * sOP -> sOP Regular Data packet
184 * sCR -> sCR Data in CLOSEREQ MAY be processed (8.3.)
185 * sCG -> sCG Data in CLOSING MAY be processed (8.3.)
188 * sNO, sRQ, sRS, sPO, sOP, sCR, sCG, sTW */
189 sIV, sIV, sIV, sIV, sOP, sCR, sCG, sIV,
191 [DCCP_PKT_DATAACK] = {
193 * sNO -> sIV No connection
194 * sRQ -> sIV No connection
195 * sRS -> sPO Ack for Response, move to PARTOPEN (8.1.5.)
196 * sPO -> sPO Remain in PARTOPEN state
197 * sOP -> sOP Regular DataAck packet in OPEN state
198 * sCR -> sCR DataAck in CLOSEREQ MAY be processed (8.3.)
199 * sCG -> sCG DataAck in CLOSING MAY be processed (8.3.)
202 * sNO, sRQ, sRS, sPO, sOP, sCR, sCG, sTW */
203 sIV, sIV, sPO, sPO, sOP, sCR, sCG, sIV
205 [DCCP_PKT_CLOSEREQ] = {
207 * CLOSEREQ may only be sent by the server.
209 * sNO, sRQ, sRS, sPO, sOP, sCR, sCG, sTW */
210 sIV, sIV, sIV, sIV, sIV, sIV, sIV, sIV
214 * sNO -> sIV No connection
215 * sRQ -> sIV No connection
216 * sRS -> sIV No connection
217 * sPO -> sCG Client-initiated close
218 * sOP -> sCG Client-initiated close
219 * sCR -> sCG Close in response to CloseReq (8.3.)
220 * sCG -> sCG Retransmit
221 * sTW -> sIV Late retransmit, already in TIME_WAIT
223 * sNO, sRQ, sRS, sPO, sOP, sCR, sCG, sTW */
224 sIV, sIV, sIV, sCG, sCG, sCG, sIV, sIV
228 * sNO -> sIV No connection
229 * sRQ -> sTW Sync received or timeout, SHOULD send Reset (8.1.1.)
230 * sRS -> sTW Response received without Request
231 * sPO -> sTW Timeout, SHOULD send Reset (8.1.5.)
232 * sOP -> sTW Connection reset
233 * sCR -> sTW Connection reset
234 * sCG -> sTW Connection reset
235 * sTW -> sIG Ignore (don't refresh timer)
237 * sNO, sRQ, sRS, sPO, sOP, sCR, sCG, sTW */
238 sIV, sTW, sTW, sTW, sTW, sTW, sTW, sIG
242 * We currently ignore Sync packets
244 * sNO, sRQ, sRS, sPO, sOP, sCR, sCG, sTW */
245 sIG, sIG, sIG, sIG, sIG, sIG, sIG, sIG,
247 [DCCP_PKT_SYNCACK] = {
249 * We currently ignore SyncAck packets
251 * sNO, sRQ, sRS, sPO, sOP, sCR, sCG, sTW */
252 sIG, sIG, sIG, sIG, sIG, sIG, sIG, sIG,
255 [CT_DCCP_ROLE_SERVER] = {
256 [DCCP_PKT_REQUEST] = {
259 * sRQ -> sIG Ignore, conntrack might be out of sync
260 * sRS -> sIG Ignore, conntrack might be out of sync
261 * sPO -> sIG Ignore, conntrack might be out of sync
262 * sOP -> sIG Ignore, conntrack might be out of sync
263 * sCR -> sIG Ignore, conntrack might be out of sync
264 * sCG -> sIG Ignore, conntrack might be out of sync
265 * sTW -> sRQ Reincarnation, must reverse roles
267 * sNO, sRQ, sRS, sPO, sOP, sCR, sCG, sTW */
268 sIV, sIG, sIG, sIG, sIG, sIG, sIG, sRQ
270 [DCCP_PKT_RESPONSE] = {
272 * sNO -> sIV Response without Request
273 * sRQ -> sRS Response to clients Request
274 * sRS -> sRS Retransmitted Response (8.1.3. SHOULD NOT)
275 * sPO -> sIG Response to an ignored Request or late retransmit
276 * sOP -> sIG Ignore, might be response to ignored Request
277 * sCR -> sIG Ignore, might be response to ignored Request
278 * sCG -> sIG Ignore, might be response to ignored Request
279 * sTW -> sIV Invalid, Request from client in sTW moves to sRQ
281 * sNO, sRQ, sRS, sPO, sOP, sCR, sCG, sTW */
282 sIV, sRS, sRS, sIG, sIG, sIG, sIG, sIV
286 * sNO -> sIV No connection
287 * sRQ -> sIV No connection
288 * sRS -> sIV No connection
289 * sPO -> sOP Enter OPEN state (8.1.5.)
290 * sOP -> sOP Regular Ack in OPEN state
291 * sCR -> sIV Waiting for Close from client
292 * sCG -> sCG Ack in CLOSING MAY be processed (8.3.)
295 * sNO, sRQ, sRS, sPO, sOP, sCR, sCG, sTW */
296 sIV, sIV, sIV, sOP, sOP, sIV, sCG, sIV
300 * sNO -> sIV No connection
301 * sRQ -> sIV No connection
302 * sRS -> sIV No connection
303 * sPO -> sOP Enter OPEN state (8.1.5.)
304 * sOP -> sOP Regular Data packet in OPEN state
305 * sCR -> sIV Waiting for Close from client
306 * sCG -> sCG Data in CLOSING MAY be processed (8.3.)
309 * sNO, sRQ, sRS, sPO, sOP, sCR, sCG, sTW */
310 sIV, sIV, sIV, sOP, sOP, sIV, sCG, sIV
312 [DCCP_PKT_DATAACK] = {
314 * sNO -> sIV No connection
315 * sRQ -> sIV No connection
316 * sRS -> sIV No connection
317 * sPO -> sOP Enter OPEN state (8.1.5.)
318 * sOP -> sOP Regular DataAck in OPEN state
319 * sCR -> sIV Waiting for Close from client
320 * sCG -> sCG Data in CLOSING MAY be processed (8.3.)
323 * sNO, sRQ, sRS, sPO, sOP, sCR, sCG, sTW */
324 sIV, sIV, sIV, sOP, sOP, sIV, sCG, sIV
326 [DCCP_PKT_CLOSEREQ] = {
328 * sNO -> sIV No connection
329 * sRQ -> sIV No connection
330 * sRS -> sIV No connection
331 * sPO -> sOP -> sCR Move directly to CLOSEREQ (8.1.5.)
332 * sOP -> sCR CloseReq in OPEN state
333 * sCR -> sCR Retransmit
334 * sCG -> sCR Simultaneous close, client sends another Close
335 * sTW -> sIV Already closed
337 * sNO, sRQ, sRS, sPO, sOP, sCR, sCG, sTW */
338 sIV, sIV, sIV, sCR, sCR, sCR, sCR, sIV
342 * sNO -> sIV No connection
343 * sRQ -> sIV No connection
344 * sRS -> sIV No connection
345 * sPO -> sOP -> sCG Move direcly to CLOSING
346 * sOP -> sCG Move to CLOSING
347 * sCR -> sIV Close after CloseReq is invalid
348 * sCG -> sCG Retransmit
349 * sTW -> sIV Already closed
351 * sNO, sRQ, sRS, sPO, sOP, sCR, sCG, sTW */
352 sIV, sIV, sIV, sCG, sCG, sIV, sCG, sIV
356 * sNO -> sIV No connection
357 * sRQ -> sTW Reset in response to Request
358 * sRS -> sTW Timeout, SHOULD send Reset (8.1.3.)
359 * sPO -> sTW Timeout, SHOULD send Reset (8.1.3.)
363 * sTW -> sIG Ignore (don't refresh timer)
365 * sNO, sRQ, sRS, sPO, sOP, sCR, sCG, sTW, sTW */
366 sIV, sTW, sTW, sTW, sTW, sTW, sTW, sTW, sIG
370 * We currently ignore Sync packets
372 * sNO, sRQ, sRS, sPO, sOP, sCR, sCG, sTW */
373 sIG, sIG, sIG, sIG, sIG, sIG, sIG, sIG,
375 [DCCP_PKT_SYNCACK] = {
377 * We currently ignore SyncAck packets
379 * sNO, sRQ, sRS, sPO, sOP, sCR, sCG, sTW */
380 sIG, sIG, sIG, sIG, sIG, sIG, sIG, sIG,
385 /* this module per-net specifics */
386 static int dccp_net_id;
389 unsigned int dccp_timeout[CT_DCCP_MAX + 1];
391 struct ctl_table_header *sysctl_header;
392 struct ctl_table *sysctl_table;
396 static inline struct dccp_net *dccp_pernet(struct net *net)
398 return net_generic(net, dccp_net_id);
401 static bool dccp_pkt_to_tuple(const struct sk_buff *skb, unsigned int dataoff,
402 struct nf_conntrack_tuple *tuple)
404 struct dccp_hdr _hdr, *dh;
406 dh = skb_header_pointer(skb, dataoff, sizeof(_hdr), &_hdr);
410 tuple->src.u.dccp.port = dh->dccph_sport;
411 tuple->dst.u.dccp.port = dh->dccph_dport;
415 static bool dccp_invert_tuple(struct nf_conntrack_tuple *inv,
416 const struct nf_conntrack_tuple *tuple)
418 inv->src.u.dccp.port = tuple->dst.u.dccp.port;
419 inv->dst.u.dccp.port = tuple->src.u.dccp.port;
423 static bool dccp_new(struct nf_conn *ct, const struct sk_buff *skb,
424 unsigned int dataoff)
426 struct net *net = nf_ct_net(ct);
428 struct dccp_hdr _dh, *dh;
432 dh = skb_header_pointer(skb, dataoff, sizeof(_dh), &dh);
435 state = dccp_state_table[CT_DCCP_ROLE_CLIENT][dh->dccph_type][CT_DCCP_NONE];
438 dn = dccp_pernet(net);
439 if (dn->dccp_loose == 0) {
440 msg = "nf_ct_dccp: not picking up existing connection ";
443 case CT_DCCP_REQUEST:
445 case CT_DCCP_INVALID:
446 msg = "nf_ct_dccp: invalid state transition ";
450 ct->proto.dccp.role[IP_CT_DIR_ORIGINAL] = CT_DCCP_ROLE_CLIENT;
451 ct->proto.dccp.role[IP_CT_DIR_REPLY] = CT_DCCP_ROLE_SERVER;
452 ct->proto.dccp.state = CT_DCCP_NONE;
456 if (LOG_INVALID(net, IPPROTO_DCCP))
457 nf_log_packet(nf_ct_l3num(ct), 0, skb, NULL, NULL, NULL, msg);
461 static u64 dccp_ack_seq(const struct dccp_hdr *dh)
463 const struct dccp_hdr_ack_bits *dhack;
465 dhack = (void *)dh + __dccp_basic_hdr_len(dh);
466 return ((u64)ntohs(dhack->dccph_ack_nr_high) << 32) +
467 ntohl(dhack->dccph_ack_nr_low);
470 static int dccp_packet(struct nf_conn *ct, const struct sk_buff *skb,
471 unsigned int dataoff, enum ip_conntrack_info ctinfo,
472 u_int8_t pf, unsigned int hooknum)
474 struct net *net = nf_ct_net(ct);
476 enum ip_conntrack_dir dir = CTINFO2DIR(ctinfo);
477 struct dccp_hdr _dh, *dh;
478 u_int8_t type, old_state, new_state;
479 enum ct_dccp_roles role;
481 dh = skb_header_pointer(skb, dataoff, sizeof(_dh), &dh);
483 type = dh->dccph_type;
485 if (type == DCCP_PKT_RESET &&
486 !test_bit(IPS_SEEN_REPLY_BIT, &ct->status)) {
487 /* Tear down connection immediately if only reply is a RESET */
488 nf_ct_kill_acct(ct, ctinfo, skb);
492 spin_lock_bh(&ct->lock);
494 role = ct->proto.dccp.role[dir];
495 old_state = ct->proto.dccp.state;
496 new_state = dccp_state_table[role][type][old_state];
499 case CT_DCCP_REQUEST:
500 if (old_state == CT_DCCP_TIMEWAIT &&
501 role == CT_DCCP_ROLE_SERVER) {
502 /* Reincarnation in the reverse direction: reopen and
503 * reverse client/server roles. */
504 ct->proto.dccp.role[dir] = CT_DCCP_ROLE_CLIENT;
505 ct->proto.dccp.role[!dir] = CT_DCCP_ROLE_SERVER;
508 case CT_DCCP_RESPOND:
509 if (old_state == CT_DCCP_REQUEST)
510 ct->proto.dccp.handshake_seq = dccp_hdr_seq(dh);
512 case CT_DCCP_PARTOPEN:
513 if (old_state == CT_DCCP_RESPOND &&
514 type == DCCP_PKT_ACK &&
515 dccp_ack_seq(dh) == ct->proto.dccp.handshake_seq)
516 set_bit(IPS_ASSURED_BIT, &ct->status);
520 * Connection tracking might be out of sync, so we ignore
521 * packets that might establish a new connection and resync
522 * if the server responds with a valid Response.
524 if (ct->proto.dccp.last_dir == !dir &&
525 ct->proto.dccp.last_pkt == DCCP_PKT_REQUEST &&
526 type == DCCP_PKT_RESPONSE) {
527 ct->proto.dccp.role[!dir] = CT_DCCP_ROLE_CLIENT;
528 ct->proto.dccp.role[dir] = CT_DCCP_ROLE_SERVER;
529 ct->proto.dccp.handshake_seq = dccp_hdr_seq(dh);
530 new_state = CT_DCCP_RESPOND;
533 ct->proto.dccp.last_dir = dir;
534 ct->proto.dccp.last_pkt = type;
536 spin_unlock_bh(&ct->lock);
537 if (LOG_INVALID(net, IPPROTO_DCCP))
538 nf_log_packet(pf, 0, skb, NULL, NULL, NULL,
539 "nf_ct_dccp: invalid packet ignored ");
541 case CT_DCCP_INVALID:
542 spin_unlock_bh(&ct->lock);
543 if (LOG_INVALID(net, IPPROTO_DCCP))
544 nf_log_packet(pf, 0, skb, NULL, NULL, NULL,
545 "nf_ct_dccp: invalid state transition ");
549 ct->proto.dccp.last_dir = dir;
550 ct->proto.dccp.last_pkt = type;
551 ct->proto.dccp.state = new_state;
552 spin_unlock_bh(&ct->lock);
554 dn = dccp_pernet(net);
555 nf_ct_refresh_acct(ct, ctinfo, skb, dn->dccp_timeout[new_state]);
560 static int dccp_error(struct net *net, struct sk_buff *skb,
561 unsigned int dataoff, enum ip_conntrack_info *ctinfo,
562 u_int8_t pf, unsigned int hooknum)
564 struct dccp_hdr _dh, *dh;
565 unsigned int dccp_len = skb->len - dataoff;
569 dh = skb_header_pointer(skb, dataoff, sizeof(_dh), &dh);
571 msg = "nf_ct_dccp: short packet ";
575 if (dh->dccph_doff * 4 < sizeof(struct dccp_hdr) ||
576 dh->dccph_doff * 4 > dccp_len) {
577 msg = "nf_ct_dccp: truncated/malformed packet ";
582 if (dh->dccph_cscov) {
583 cscov = (dh->dccph_cscov - 1) * 4;
584 if (cscov > dccp_len) {
585 msg = "nf_ct_dccp: bad checksum coverage ";
590 if (net->ct.sysctl_checksum && hooknum == NF_INET_PRE_ROUTING &&
591 nf_checksum_partial(skb, hooknum, dataoff, cscov, IPPROTO_DCCP,
593 msg = "nf_ct_dccp: bad checksum ";
597 if (dh->dccph_type >= DCCP_PKT_INVALID) {
598 msg = "nf_ct_dccp: reserved packet type ";
605 if (LOG_INVALID(net, IPPROTO_DCCP))
606 nf_log_packet(pf, 0, skb, NULL, NULL, NULL, msg);
610 static int dccp_print_tuple(struct seq_file *s,
611 const struct nf_conntrack_tuple *tuple)
613 return seq_printf(s, "sport=%hu dport=%hu ",
614 ntohs(tuple->src.u.dccp.port),
615 ntohs(tuple->dst.u.dccp.port));
618 static int dccp_print_conntrack(struct seq_file *s, struct nf_conn *ct)
620 return seq_printf(s, "%s ", dccp_state_names[ct->proto.dccp.state]);
623 #if defined(CONFIG_NF_CT_NETLINK) || defined(CONFIG_NF_CT_NETLINK_MODULE)
624 static int dccp_to_nlattr(struct sk_buff *skb, struct nlattr *nla,
627 struct nlattr *nest_parms;
629 spin_lock_bh(&ct->lock);
630 nest_parms = nla_nest_start(skb, CTA_PROTOINFO_DCCP | NLA_F_NESTED);
632 goto nla_put_failure;
633 NLA_PUT_U8(skb, CTA_PROTOINFO_DCCP_STATE, ct->proto.dccp.state);
634 NLA_PUT_U8(skb, CTA_PROTOINFO_DCCP_ROLE,
635 ct->proto.dccp.role[IP_CT_DIR_ORIGINAL]);
636 NLA_PUT_BE64(skb, CTA_PROTOINFO_DCCP_HANDSHAKE_SEQ,
637 cpu_to_be64(ct->proto.dccp.handshake_seq));
638 nla_nest_end(skb, nest_parms);
639 spin_unlock_bh(&ct->lock);
643 spin_unlock_bh(&ct->lock);
647 static const struct nla_policy dccp_nla_policy[CTA_PROTOINFO_DCCP_MAX + 1] = {
648 [CTA_PROTOINFO_DCCP_STATE] = { .type = NLA_U8 },
649 [CTA_PROTOINFO_DCCP_ROLE] = { .type = NLA_U8 },
650 [CTA_PROTOINFO_DCCP_HANDSHAKE_SEQ] = { .type = NLA_U64 },
653 static int nlattr_to_dccp(struct nlattr *cda[], struct nf_conn *ct)
655 struct nlattr *attr = cda[CTA_PROTOINFO_DCCP];
656 struct nlattr *tb[CTA_PROTOINFO_DCCP_MAX + 1];
662 err = nla_parse_nested(tb, CTA_PROTOINFO_DCCP_MAX, attr,
667 if (!tb[CTA_PROTOINFO_DCCP_STATE] ||
668 !tb[CTA_PROTOINFO_DCCP_ROLE] ||
669 nla_get_u8(tb[CTA_PROTOINFO_DCCP_ROLE]) > CT_DCCP_ROLE_MAX ||
670 nla_get_u8(tb[CTA_PROTOINFO_DCCP_STATE]) >= CT_DCCP_IGNORE) {
674 spin_lock_bh(&ct->lock);
675 ct->proto.dccp.state = nla_get_u8(tb[CTA_PROTOINFO_DCCP_STATE]);
676 if (nla_get_u8(tb[CTA_PROTOINFO_DCCP_ROLE]) == CT_DCCP_ROLE_CLIENT) {
677 ct->proto.dccp.role[IP_CT_DIR_ORIGINAL] = CT_DCCP_ROLE_CLIENT;
678 ct->proto.dccp.role[IP_CT_DIR_REPLY] = CT_DCCP_ROLE_SERVER;
680 ct->proto.dccp.role[IP_CT_DIR_ORIGINAL] = CT_DCCP_ROLE_SERVER;
681 ct->proto.dccp.role[IP_CT_DIR_REPLY] = CT_DCCP_ROLE_CLIENT;
683 if (tb[CTA_PROTOINFO_DCCP_HANDSHAKE_SEQ]) {
684 ct->proto.dccp.handshake_seq =
685 be64_to_cpu(nla_get_be64(tb[CTA_PROTOINFO_DCCP_HANDSHAKE_SEQ]));
687 spin_unlock_bh(&ct->lock);
691 static int dccp_nlattr_size(void)
693 return nla_total_size(0) /* CTA_PROTOINFO_DCCP */
694 + nla_policy_len(dccp_nla_policy, CTA_PROTOINFO_DCCP_MAX + 1);
699 /* template, data assigned later */
700 static struct ctl_table dccp_sysctl_table[] = {
702 .ctl_name = CTL_UNNUMBERED,
703 .procname = "nf_conntrack_dccp_timeout_request",
704 .maxlen = sizeof(unsigned int),
706 .proc_handler = proc_dointvec_jiffies,
709 .ctl_name = CTL_UNNUMBERED,
710 .procname = "nf_conntrack_dccp_timeout_respond",
711 .maxlen = sizeof(unsigned int),
713 .proc_handler = proc_dointvec_jiffies,
716 .ctl_name = CTL_UNNUMBERED,
717 .procname = "nf_conntrack_dccp_timeout_partopen",
718 .maxlen = sizeof(unsigned int),
720 .proc_handler = proc_dointvec_jiffies,
723 .ctl_name = CTL_UNNUMBERED,
724 .procname = "nf_conntrack_dccp_timeout_open",
725 .maxlen = sizeof(unsigned int),
727 .proc_handler = proc_dointvec_jiffies,
730 .ctl_name = CTL_UNNUMBERED,
731 .procname = "nf_conntrack_dccp_timeout_closereq",
732 .maxlen = sizeof(unsigned int),
734 .proc_handler = proc_dointvec_jiffies,
737 .ctl_name = CTL_UNNUMBERED,
738 .procname = "nf_conntrack_dccp_timeout_closing",
739 .maxlen = sizeof(unsigned int),
741 .proc_handler = proc_dointvec_jiffies,
744 .ctl_name = CTL_UNNUMBERED,
745 .procname = "nf_conntrack_dccp_timeout_timewait",
746 .maxlen = sizeof(unsigned int),
748 .proc_handler = proc_dointvec_jiffies,
751 .ctl_name = CTL_UNNUMBERED,
752 .procname = "nf_conntrack_dccp_loose",
753 .maxlen = sizeof(int),
755 .proc_handler = proc_dointvec,
761 #endif /* CONFIG_SYSCTL */
763 static struct nf_conntrack_l4proto dccp_proto4 __read_mostly = {
765 .l4proto = IPPROTO_DCCP,
767 .pkt_to_tuple = dccp_pkt_to_tuple,
768 .invert_tuple = dccp_invert_tuple,
770 .packet = dccp_packet,
772 .print_tuple = dccp_print_tuple,
773 .print_conntrack = dccp_print_conntrack,
774 #if defined(CONFIG_NF_CT_NETLINK) || defined(CONFIG_NF_CT_NETLINK_MODULE)
775 .to_nlattr = dccp_to_nlattr,
776 .nlattr_size = dccp_nlattr_size,
777 .from_nlattr = nlattr_to_dccp,
778 .tuple_to_nlattr = nf_ct_port_tuple_to_nlattr,
779 .nlattr_tuple_size = nf_ct_port_nlattr_tuple_size,
780 .nlattr_to_tuple = nf_ct_port_nlattr_to_tuple,
781 .nla_policy = nf_ct_port_nla_policy,
785 static struct nf_conntrack_l4proto dccp_proto6 __read_mostly = {
787 .l4proto = IPPROTO_DCCP,
789 .pkt_to_tuple = dccp_pkt_to_tuple,
790 .invert_tuple = dccp_invert_tuple,
792 .packet = dccp_packet,
794 .print_tuple = dccp_print_tuple,
795 .print_conntrack = dccp_print_conntrack,
796 #if defined(CONFIG_NF_CT_NETLINK) || defined(CONFIG_NF_CT_NETLINK_MODULE)
797 .to_nlattr = dccp_to_nlattr,
798 .nlattr_size = dccp_nlattr_size,
799 .from_nlattr = nlattr_to_dccp,
800 .tuple_to_nlattr = nf_ct_port_tuple_to_nlattr,
801 .nlattr_tuple_size = nf_ct_port_nlattr_tuple_size,
802 .nlattr_to_tuple = nf_ct_port_nlattr_to_tuple,
803 .nla_policy = nf_ct_port_nla_policy,
807 static __net_init int dccp_net_init(struct net *net)
812 dn = kmalloc(sizeof(*dn), GFP_KERNEL);
818 dn->dccp_timeout[CT_DCCP_REQUEST] = 2 * DCCP_MSL;
819 dn->dccp_timeout[CT_DCCP_RESPOND] = 4 * DCCP_MSL;
820 dn->dccp_timeout[CT_DCCP_PARTOPEN] = 4 * DCCP_MSL;
821 dn->dccp_timeout[CT_DCCP_OPEN] = 12 * 3600 * HZ;
822 dn->dccp_timeout[CT_DCCP_CLOSEREQ] = 64 * HZ;
823 dn->dccp_timeout[CT_DCCP_CLOSING] = 64 * HZ;
824 dn->dccp_timeout[CT_DCCP_TIMEWAIT] = 2 * DCCP_MSL;
826 err = net_assign_generic(net, dccp_net_id, dn);
832 dn->sysctl_table = kmemdup(dccp_sysctl_table,
833 sizeof(dccp_sysctl_table), GFP_KERNEL);
834 if (!dn->sysctl_table)
837 dn->sysctl_table[0].data = &dn->dccp_timeout[CT_DCCP_REQUEST];
838 dn->sysctl_table[1].data = &dn->dccp_timeout[CT_DCCP_RESPOND];
839 dn->sysctl_table[2].data = &dn->dccp_timeout[CT_DCCP_PARTOPEN];
840 dn->sysctl_table[3].data = &dn->dccp_timeout[CT_DCCP_OPEN];
841 dn->sysctl_table[4].data = &dn->dccp_timeout[CT_DCCP_CLOSEREQ];
842 dn->sysctl_table[5].data = &dn->dccp_timeout[CT_DCCP_CLOSING];
843 dn->sysctl_table[6].data = &dn->dccp_timeout[CT_DCCP_TIMEWAIT];
844 dn->sysctl_table[7].data = &dn->dccp_loose;
846 dn->sysctl_header = register_net_sysctl_table(net,
847 nf_net_netfilter_sysctl_path, dn->sysctl_table);
848 if (!dn->sysctl_header) {
849 kfree(dn->sysctl_table);
861 static __net_exit void dccp_net_exit(struct net *net)
863 struct dccp_net *dn = dccp_pernet(net);
865 unregister_net_sysctl_table(dn->sysctl_header);
866 kfree(dn->sysctl_table);
870 net_assign_generic(net, dccp_net_id, NULL);
873 static struct pernet_operations dccp_net_ops = {
874 .init = dccp_net_init,
875 .exit = dccp_net_exit,
878 static int __init nf_conntrack_proto_dccp_init(void)
882 err = register_pernet_gen_subsys(&dccp_net_id, &dccp_net_ops);
886 err = nf_conntrack_l4proto_register(&dccp_proto4);
890 err = nf_conntrack_l4proto_register(&dccp_proto6);
896 nf_conntrack_l4proto_unregister(&dccp_proto4);
898 unregister_pernet_gen_subsys(dccp_net_id, &dccp_net_ops);
903 static void __exit nf_conntrack_proto_dccp_fini(void)
905 unregister_pernet_gen_subsys(dccp_net_id, &dccp_net_ops);
906 nf_conntrack_l4proto_unregister(&dccp_proto6);
907 nf_conntrack_l4proto_unregister(&dccp_proto4);
910 module_init(nf_conntrack_proto_dccp_init);
911 module_exit(nf_conntrack_proto_dccp_fini);
913 MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
914 MODULE_DESCRIPTION("DCCP connection tracking protocol helper");
915 MODULE_LICENSE("GPL");