netfilter: nf_conntrack: use per-conntrack locks for protocol data
[linux-2.6.git] / net / netfilter / nf_conntrack_proto_dccp.c
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>
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
19 #include <net/net_namespace.h>
20 #include <net/netns/generic.h>
21
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>
26
27 /* Timeouts are based on values from RFC4340:
28  *
29  * - REQUEST:
30  *
31  *   8.1.2. Client Request
32  *
33  *   A client MAY give up on its DCCP-Requests after some time
34  *   (3 minutes, for example).
35  *
36  * - RESPOND:
37  *
38  *   8.1.3. Server Response
39  *
40  *   It MAY also leave the RESPOND state for CLOSED after a timeout of
41  *   not less than 4MSL (8 minutes);
42  *
43  * - PARTOPEN:
44  *
45  *   8.1.5. Handshake Completion
46  *
47  *   If the client remains in PARTOPEN for more than 4MSL (8 minutes),
48  *   it SHOULD reset the connection with Reset Code 2, "Aborted".
49  *
50  * - OPEN:
51  *
52  *   The DCCP timestamp overflows after 11.9 hours. If the connection
53  *   stays idle this long the sequence number won't be recognized
54  *   as valid anymore.
55  *
56  * - CLOSEREQ/CLOSING:
57  *
58  *   8.3. Termination
59  *
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
62  *   64 seconds ...
63  *
64  * - TIMEWAIT:
65  *
66  *   4.3. States
67  *
68  *   A server or client socket remains in this state for 2MSL (4 minutes)
69  *   after the connection has been town down, ...
70  */
71
72 #define DCCP_MSL (2 * 60 * HZ)
73
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",
85 };
86
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
97
98 /*
99  * DCCP state transistion table
100  *
101  * The assumption is the same as for TCP tracking:
102  *
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.
106  *
107  * The following states exist:
108  *
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
119  *
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.
123  *
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
128  * care about.
129  */
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] = {
134                 /*
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
144                  *
145                  *      sNO, sRQ, sRS, sPO. sOP, sCR, sCG, sTW, */
146                         sRQ, sRQ, sRS, sIG, sIG, sIG, sIG, sRQ,
147                 },
148                 [DCCP_PKT_RESPONSE] = {
149                 /*
150                  * sNO -> sIV           Invalid
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
158                  *                      goes through sRQ
159                  *
160                  *      sNO, sRQ, sRS, sPO, sOP, sCR, sCG, sTW */
161                         sIV, sIG, sIG, sIG, sIG, sIG, sIG, sIV,
162                 },
163                 [DCCP_PKT_ACK] = {
164                 /*
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.)
172                  * sTW -> sIV
173                  *
174                  *      sNO, sRQ, sRS, sPO, sOP, sCR, sCG, sTW */
175                         sIV, sIV, sPO, sPO, sOP, sCR, sCG, sIV
176                 },
177                 [DCCP_PKT_DATA] = {
178                 /*
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.)
186                  * sTW -> sIV
187                  *
188                  *      sNO, sRQ, sRS, sPO, sOP, sCR, sCG, sTW */
189                         sIV, sIV, sIV, sIV, sOP, sCR, sCG, sIV,
190                 },
191                 [DCCP_PKT_DATAACK] = {
192                 /*
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.)
200                  * sTW -> sIV
201                  *
202                  *      sNO, sRQ, sRS, sPO, sOP, sCR, sCG, sTW */
203                         sIV, sIV, sPO, sPO, sOP, sCR, sCG, sIV
204                 },
205                 [DCCP_PKT_CLOSEREQ] = {
206                 /*
207                  * CLOSEREQ may only be sent by the server.
208                  *
209                  *      sNO, sRQ, sRS, sPO, sOP, sCR, sCG, sTW */
210                         sIV, sIV, sIV, sIV, sIV, sIV, sIV, sIV
211                 },
212                 [DCCP_PKT_CLOSE] = {
213                 /*
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
222                  *
223                  *      sNO, sRQ, sRS, sPO, sOP, sCR, sCG, sTW */
224                         sIV, sIV, sIV, sCG, sCG, sCG, sIV, sIV
225                 },
226                 [DCCP_PKT_RESET] = {
227                 /*
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)
236                  *
237                  *      sNO, sRQ, sRS, sPO, sOP, sCR, sCG, sTW */
238                         sIV, sTW, sTW, sTW, sTW, sTW, sTW, sIG
239                 },
240                 [DCCP_PKT_SYNC] = {
241                 /*
242                  * We currently ignore Sync packets
243                  *
244                  *      sNO, sRQ, sRS, sPO, sOP, sCR, sCG, sTW */
245                         sIG, sIG, sIG, sIG, sIG, sIG, sIG, sIG,
246                 },
247                 [DCCP_PKT_SYNCACK] = {
248                 /*
249                  * We currently ignore SyncAck packets
250                  *
251                  *      sNO, sRQ, sRS, sPO, sOP, sCR, sCG, sTW */
252                         sIG, sIG, sIG, sIG, sIG, sIG, sIG, sIG,
253                 },
254         },
255         [CT_DCCP_ROLE_SERVER] = {
256                 [DCCP_PKT_REQUEST] = {
257                 /*
258                  * sNO -> sIV           Invalid
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
266                  *
267                  *      sNO, sRQ, sRS, sPO, sOP, sCR, sCG, sTW */
268                         sIV, sIG, sIG, sIG, sIG, sIG, sIG, sRQ
269                 },
270                 [DCCP_PKT_RESPONSE] = {
271                 /*
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
280                  *
281                  *      sNO, sRQ, sRS, sPO, sOP, sCR, sCG, sTW */
282                         sIV, sRS, sRS, sIG, sIG, sIG, sIG, sIV
283                 },
284                 [DCCP_PKT_ACK] = {
285                 /*
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.)
293                  * sTW -> sIV
294                  *
295                  *      sNO, sRQ, sRS, sPO, sOP, sCR, sCG, sTW */
296                         sIV, sIV, sIV, sOP, sOP, sIV, sCG, sIV
297                 },
298                 [DCCP_PKT_DATA] = {
299                 /*
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.)
307                  * sTW -> sIV
308                  *
309                  *      sNO, sRQ, sRS, sPO, sOP, sCR, sCG, sTW */
310                         sIV, sIV, sIV, sOP, sOP, sIV, sCG, sIV
311                 },
312                 [DCCP_PKT_DATAACK] = {
313                 /*
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.)
321                  * sTW -> sIV
322                  *
323                  *      sNO, sRQ, sRS, sPO, sOP, sCR, sCG, sTW */
324                         sIV, sIV, sIV, sOP, sOP, sIV, sCG, sIV
325                 },
326                 [DCCP_PKT_CLOSEREQ] = {
327                 /*
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
336                  *
337                  *      sNO, sRQ, sRS, sPO, sOP, sCR, sCG, sTW */
338                         sIV, sIV, sIV, sCR, sCR, sCR, sCR, sIV
339                 },
340                 [DCCP_PKT_CLOSE] = {
341                 /*
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
350                  *
351                  *      sNO, sRQ, sRS, sPO, sOP, sCR, sCG, sTW */
352                         sIV, sIV, sIV, sCG, sCG, sIV, sCG, sIV
353                 },
354                 [DCCP_PKT_RESET] = {
355                 /*
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.)
360                  * sOP -> sTW
361                  * sCR -> sTW
362                  * sCG -> sTW
363                  * sTW -> sIG           Ignore (don't refresh timer)
364                  *
365                  *      sNO, sRQ, sRS, sPO, sOP, sCR, sCG, sTW, sTW */
366                         sIV, sTW, sTW, sTW, sTW, sTW, sTW, sTW, sIG
367                 },
368                 [DCCP_PKT_SYNC] = {
369                 /*
370                  * We currently ignore Sync packets
371                  *
372                  *      sNO, sRQ, sRS, sPO, sOP, sCR, sCG, sTW */
373                         sIG, sIG, sIG, sIG, sIG, sIG, sIG, sIG,
374                 },
375                 [DCCP_PKT_SYNCACK] = {
376                 /*
377                  * We currently ignore SyncAck packets
378                  *
379                  *      sNO, sRQ, sRS, sPO, sOP, sCR, sCG, sTW */
380                         sIG, sIG, sIG, sIG, sIG, sIG, sIG, sIG,
381                 },
382         },
383 };
384
385 /* this module per-net specifics */
386 static int dccp_net_id;
387 struct dccp_net {
388         int dccp_loose;
389         unsigned int dccp_timeout[CT_DCCP_MAX + 1];
390 #ifdef CONFIG_SYSCTL
391         struct ctl_table_header *sysctl_header;
392         struct ctl_table *sysctl_table;
393 #endif
394 };
395
396 static inline struct dccp_net *dccp_pernet(struct net *net)
397 {
398         return net_generic(net, dccp_net_id);
399 }
400
401 static bool dccp_pkt_to_tuple(const struct sk_buff *skb, unsigned int dataoff,
402                               struct nf_conntrack_tuple *tuple)
403 {
404         struct dccp_hdr _hdr, *dh;
405
406         dh = skb_header_pointer(skb, dataoff, sizeof(_hdr), &_hdr);
407         if (dh == NULL)
408                 return false;
409
410         tuple->src.u.dccp.port = dh->dccph_sport;
411         tuple->dst.u.dccp.port = dh->dccph_dport;
412         return true;
413 }
414
415 static bool dccp_invert_tuple(struct nf_conntrack_tuple *inv,
416                               const struct nf_conntrack_tuple *tuple)
417 {
418         inv->src.u.dccp.port = tuple->dst.u.dccp.port;
419         inv->dst.u.dccp.port = tuple->src.u.dccp.port;
420         return true;
421 }
422
423 static bool dccp_new(struct nf_conn *ct, const struct sk_buff *skb,
424                      unsigned int dataoff)
425 {
426         struct net *net = nf_ct_net(ct);
427         struct dccp_net *dn;
428         struct dccp_hdr _dh, *dh;
429         const char *msg;
430         u_int8_t state;
431
432         dh = skb_header_pointer(skb, dataoff, sizeof(_dh), &dh);
433         BUG_ON(dh == NULL);
434
435         state = dccp_state_table[CT_DCCP_ROLE_CLIENT][dh->dccph_type][CT_DCCP_NONE];
436         switch (state) {
437         default:
438                 dn = dccp_pernet(net);
439                 if (dn->dccp_loose == 0) {
440                         msg = "nf_ct_dccp: not picking up existing connection ";
441                         goto out_invalid;
442                 }
443         case CT_DCCP_REQUEST:
444                 break;
445         case CT_DCCP_INVALID:
446                 msg = "nf_ct_dccp: invalid state transition ";
447                 goto out_invalid;
448         }
449
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;
453         return true;
454
455 out_invalid:
456         if (LOG_INVALID(net, IPPROTO_DCCP))
457                 nf_log_packet(nf_ct_l3num(ct), 0, skb, NULL, NULL, NULL, msg);
458         return false;
459 }
460
461 static u64 dccp_ack_seq(const struct dccp_hdr *dh)
462 {
463         const struct dccp_hdr_ack_bits *dhack;
464
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);
468 }
469
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)
473 {
474         struct net *net = nf_ct_net(ct);
475         struct dccp_net *dn;
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;
480
481         dh = skb_header_pointer(skb, dataoff, sizeof(_dh), &dh);
482         BUG_ON(dh == NULL);
483         type = dh->dccph_type;
484
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);
489                 return NF_ACCEPT;
490         }
491
492         spin_lock_bh(&ct->lock);
493
494         role = ct->proto.dccp.role[dir];
495         old_state = ct->proto.dccp.state;
496         new_state = dccp_state_table[role][type][old_state];
497
498         switch (new_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;
506                 }
507                 break;
508         case CT_DCCP_RESPOND:
509                 if (old_state == CT_DCCP_REQUEST)
510                         ct->proto.dccp.handshake_seq = dccp_hdr_seq(dh);
511                 break;
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);
517                 break;
518         case CT_DCCP_IGNORE:
519                 /*
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.
523                  */
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;
531                         break;
532                 }
533                 ct->proto.dccp.last_dir = dir;
534                 ct->proto.dccp.last_pkt = type;
535
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 ");
540                 return NF_ACCEPT;
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 ");
546                 return -NF_ACCEPT;
547         }
548
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);
553
554         dn = dccp_pernet(net);
555         nf_ct_refresh_acct(ct, ctinfo, skb, dn->dccp_timeout[new_state]);
556
557         return NF_ACCEPT;
558 }
559
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)
563 {
564         struct dccp_hdr _dh, *dh;
565         unsigned int dccp_len = skb->len - dataoff;
566         unsigned int cscov;
567         const char *msg;
568
569         dh = skb_header_pointer(skb, dataoff, sizeof(_dh), &dh);
570         if (dh == NULL) {
571                 msg = "nf_ct_dccp: short packet ";
572                 goto out_invalid;
573         }
574
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 ";
578                 goto out_invalid;
579         }
580
581         cscov = dccp_len;
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 ";
586                         goto out_invalid;
587                 }
588         }
589
590         if (net->ct.sysctl_checksum && hooknum == NF_INET_PRE_ROUTING &&
591             nf_checksum_partial(skb, hooknum, dataoff, cscov, IPPROTO_DCCP,
592                                 pf)) {
593                 msg = "nf_ct_dccp: bad checksum ";
594                 goto out_invalid;
595         }
596
597         if (dh->dccph_type >= DCCP_PKT_INVALID) {
598                 msg = "nf_ct_dccp: reserved packet type ";
599                 goto out_invalid;
600         }
601
602         return NF_ACCEPT;
603
604 out_invalid:
605         if (LOG_INVALID(net, IPPROTO_DCCP))
606                 nf_log_packet(pf, 0, skb, NULL, NULL, NULL, msg);
607         return -NF_ACCEPT;
608 }
609
610 static int dccp_print_tuple(struct seq_file *s,
611                             const struct nf_conntrack_tuple *tuple)
612 {
613         return seq_printf(s, "sport=%hu dport=%hu ",
614                           ntohs(tuple->src.u.dccp.port),
615                           ntohs(tuple->dst.u.dccp.port));
616 }
617
618 static int dccp_print_conntrack(struct seq_file *s, struct nf_conn *ct)
619 {
620         return seq_printf(s, "%s ", dccp_state_names[ct->proto.dccp.state]);
621 }
622
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,
625                           struct nf_conn *ct)
626 {
627         struct nlattr *nest_parms;
628
629         spin_lock_bh(&ct->lock);
630         nest_parms = nla_nest_start(skb, CTA_PROTOINFO_DCCP | NLA_F_NESTED);
631         if (!nest_parms)
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);
640         return 0;
641
642 nla_put_failure:
643         spin_unlock_bh(&ct->lock);
644         return -1;
645 }
646
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 },
651 };
652
653 static int nlattr_to_dccp(struct nlattr *cda[], struct nf_conn *ct)
654 {
655         struct nlattr *attr = cda[CTA_PROTOINFO_DCCP];
656         struct nlattr *tb[CTA_PROTOINFO_DCCP_MAX + 1];
657         int err;
658
659         if (!attr)
660                 return 0;
661
662         err = nla_parse_nested(tb, CTA_PROTOINFO_DCCP_MAX, attr,
663                                dccp_nla_policy);
664         if (err < 0)
665                 return err;
666
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) {
671                 return -EINVAL;
672         }
673
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;
679         } else {
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;
682         }
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]));
686         }
687         spin_unlock_bh(&ct->lock);
688         return 0;
689 }
690
691 static int dccp_nlattr_size(void)
692 {
693         return nla_total_size(0)        /* CTA_PROTOINFO_DCCP */
694                 + nla_policy_len(dccp_nla_policy, CTA_PROTOINFO_DCCP_MAX + 1);
695 }
696 #endif
697
698 #ifdef CONFIG_SYSCTL
699 /* template, data assigned later */
700 static struct ctl_table dccp_sysctl_table[] = {
701         {
702                 .ctl_name       = CTL_UNNUMBERED,
703                 .procname       = "nf_conntrack_dccp_timeout_request",
704                 .maxlen         = sizeof(unsigned int),
705                 .mode           = 0644,
706                 .proc_handler   = proc_dointvec_jiffies,
707         },
708         {
709                 .ctl_name       = CTL_UNNUMBERED,
710                 .procname       = "nf_conntrack_dccp_timeout_respond",
711                 .maxlen         = sizeof(unsigned int),
712                 .mode           = 0644,
713                 .proc_handler   = proc_dointvec_jiffies,
714         },
715         {
716                 .ctl_name       = CTL_UNNUMBERED,
717                 .procname       = "nf_conntrack_dccp_timeout_partopen",
718                 .maxlen         = sizeof(unsigned int),
719                 .mode           = 0644,
720                 .proc_handler   = proc_dointvec_jiffies,
721         },
722         {
723                 .ctl_name       = CTL_UNNUMBERED,
724                 .procname       = "nf_conntrack_dccp_timeout_open",
725                 .maxlen         = sizeof(unsigned int),
726                 .mode           = 0644,
727                 .proc_handler   = proc_dointvec_jiffies,
728         },
729         {
730                 .ctl_name       = CTL_UNNUMBERED,
731                 .procname       = "nf_conntrack_dccp_timeout_closereq",
732                 .maxlen         = sizeof(unsigned int),
733                 .mode           = 0644,
734                 .proc_handler   = proc_dointvec_jiffies,
735         },
736         {
737                 .ctl_name       = CTL_UNNUMBERED,
738                 .procname       = "nf_conntrack_dccp_timeout_closing",
739                 .maxlen         = sizeof(unsigned int),
740                 .mode           = 0644,
741                 .proc_handler   = proc_dointvec_jiffies,
742         },
743         {
744                 .ctl_name       = CTL_UNNUMBERED,
745                 .procname       = "nf_conntrack_dccp_timeout_timewait",
746                 .maxlen         = sizeof(unsigned int),
747                 .mode           = 0644,
748                 .proc_handler   = proc_dointvec_jiffies,
749         },
750         {
751                 .ctl_name       = CTL_UNNUMBERED,
752                 .procname       = "nf_conntrack_dccp_loose",
753                 .maxlen         = sizeof(int),
754                 .mode           = 0644,
755                 .proc_handler   = proc_dointvec,
756         },
757         {
758                 .ctl_name       = 0,
759         }
760 };
761 #endif /* CONFIG_SYSCTL */
762
763 static struct nf_conntrack_l4proto dccp_proto4 __read_mostly = {
764         .l3proto                = AF_INET,
765         .l4proto                = IPPROTO_DCCP,
766         .name                   = "dccp",
767         .pkt_to_tuple           = dccp_pkt_to_tuple,
768         .invert_tuple           = dccp_invert_tuple,
769         .new                    = dccp_new,
770         .packet                 = dccp_packet,
771         .error                  = dccp_error,
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,
782 #endif
783 };
784
785 static struct nf_conntrack_l4proto dccp_proto6 __read_mostly = {
786         .l3proto                = AF_INET6,
787         .l4proto                = IPPROTO_DCCP,
788         .name                   = "dccp",
789         .pkt_to_tuple           = dccp_pkt_to_tuple,
790         .invert_tuple           = dccp_invert_tuple,
791         .new                    = dccp_new,
792         .packet                 = dccp_packet,
793         .error                  = dccp_error,
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,
804 #endif
805 };
806
807 static __net_init int dccp_net_init(struct net *net)
808 {
809         struct dccp_net *dn;
810         int err;
811
812         dn = kmalloc(sizeof(*dn), GFP_KERNEL);
813         if (!dn)
814                 return -ENOMEM;
815
816         /* default values */
817         dn->dccp_loose = 1;
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;
825
826         err = net_assign_generic(net, dccp_net_id, dn);
827         if (err)
828                 goto out;
829
830 #ifdef CONFIG_SYSCTL
831         err = -ENOMEM;
832         dn->sysctl_table = kmemdup(dccp_sysctl_table,
833                         sizeof(dccp_sysctl_table), GFP_KERNEL);
834         if (!dn->sysctl_table)
835                 goto out;
836
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;
845
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);
850                 goto out;
851         }
852 #endif
853
854         return 0;
855
856 out:
857         kfree(dn);
858         return err;
859 }
860
861 static __net_exit void dccp_net_exit(struct net *net)
862 {
863         struct dccp_net *dn = dccp_pernet(net);
864 #ifdef CONFIG_SYSCTL
865         unregister_net_sysctl_table(dn->sysctl_header);
866         kfree(dn->sysctl_table);
867 #endif
868         kfree(dn);
869
870         net_assign_generic(net, dccp_net_id, NULL);
871 }
872
873 static struct pernet_operations dccp_net_ops = {
874         .init = dccp_net_init,
875         .exit = dccp_net_exit,
876 };
877
878 static int __init nf_conntrack_proto_dccp_init(void)
879 {
880         int err;
881
882         err = register_pernet_gen_subsys(&dccp_net_id, &dccp_net_ops);
883         if (err < 0)
884                 goto err1;
885
886         err = nf_conntrack_l4proto_register(&dccp_proto4);
887         if (err < 0)
888                 goto err2;
889
890         err = nf_conntrack_l4proto_register(&dccp_proto6);
891         if (err < 0)
892                 goto err3;
893         return 0;
894
895 err3:
896         nf_conntrack_l4proto_unregister(&dccp_proto4);
897 err2:
898         unregister_pernet_gen_subsys(dccp_net_id, &dccp_net_ops);
899 err1:
900         return err;
901 }
902
903 static void __exit nf_conntrack_proto_dccp_fini(void)
904 {
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);
908 }
909
910 module_init(nf_conntrack_proto_dccp_init);
911 module_exit(nf_conntrack_proto_dccp_fini);
912
913 MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
914 MODULE_DESCRIPTION("DCCP connection tracking protocol helper");
915 MODULE_LICENSE("GPL");