[NETFILTER]: nf_conntrack: use bool type in struct nf_conntrack_l3proto
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / net / netfilter / nf_conntrack_proto_dccp.c
CommitLineData
2bc78049
PM
1/*
2 * DCCP connection tracking protocol helper
3 *
4 * Copyright (c) 2005, 2006, 2008 Patrick McHardy <kaber@trash.net>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
9 *
10 */
11#include <linux/kernel.h>
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 <linux/netfilter/nfnetlink_conntrack.h>
20#include <net/netfilter/nf_conntrack.h>
21#include <net/netfilter/nf_conntrack_l4proto.h>
22#include <net/netfilter/nf_log.h>
23
24static DEFINE_RWLOCK(dccp_lock);
25
26static int nf_ct_dccp_loose __read_mostly = 1;
27
28/* Timeouts are based on values from RFC4340:
29 *
30 * - REQUEST:
31 *
32 * 8.1.2. Client Request
33 *
34 * A client MAY give up on its DCCP-Requests after some time
35 * (3 minutes, for example).
36 *
37 * - RESPOND:
38 *
39 * 8.1.3. Server Response
40 *
41 * It MAY also leave the RESPOND state for CLOSED after a timeout of
42 * not less than 4MSL (8 minutes);
43 *
44 * - PARTOPEN:
45 *
46 * 8.1.5. Handshake Completion
47 *
48 * If the client remains in PARTOPEN for more than 4MSL (8 minutes),
49 * it SHOULD reset the connection with Reset Code 2, "Aborted".
50 *
51 * - OPEN:
52 *
53 * The DCCP timestamp overflows after 11.9 hours. If the connection
54 * stays idle this long the sequence number won't be recognized
55 * as valid anymore.
56 *
57 * - CLOSEREQ/CLOSING:
58 *
59 * 8.3. Termination
60 *
61 * The retransmission timer should initially be set to go off in two
62 * round-trip times and should back off to not less than once every
63 * 64 seconds ...
64 *
65 * - TIMEWAIT:
66 *
67 * 4.3. States
68 *
69 * A server or client socket remains in this state for 2MSL (4 minutes)
70 * after the connection has been town down, ...
71 */
72
73#define DCCP_MSL (2 * 60 * HZ)
74
75static unsigned int dccp_timeout[CT_DCCP_MAX + 1] __read_mostly = {
76 [CT_DCCP_REQUEST] = 2 * DCCP_MSL,
77 [CT_DCCP_RESPOND] = 4 * DCCP_MSL,
78 [CT_DCCP_PARTOPEN] = 4 * DCCP_MSL,
79 [CT_DCCP_OPEN] = 12 * 3600 * HZ,
80 [CT_DCCP_CLOSEREQ] = 64 * HZ,
81 [CT_DCCP_CLOSING] = 64 * HZ,
82 [CT_DCCP_TIMEWAIT] = 2 * DCCP_MSL,
83};
84
85static const char * const dccp_state_names[] = {
86 [CT_DCCP_NONE] = "NONE",
87 [CT_DCCP_REQUEST] = "REQUEST",
88 [CT_DCCP_RESPOND] = "RESPOND",
89 [CT_DCCP_PARTOPEN] = "PARTOPEN",
90 [CT_DCCP_OPEN] = "OPEN",
91 [CT_DCCP_CLOSEREQ] = "CLOSEREQ",
92 [CT_DCCP_CLOSING] = "CLOSING",
93 [CT_DCCP_TIMEWAIT] = "TIMEWAIT",
94 [CT_DCCP_IGNORE] = "IGNORE",
95 [CT_DCCP_INVALID] = "INVALID",
96};
97
98#define sNO CT_DCCP_NONE
99#define sRQ CT_DCCP_REQUEST
100#define sRS CT_DCCP_RESPOND
101#define sPO CT_DCCP_PARTOPEN
102#define sOP CT_DCCP_OPEN
103#define sCR CT_DCCP_CLOSEREQ
104#define sCG CT_DCCP_CLOSING
105#define sTW CT_DCCP_TIMEWAIT
106#define sIG CT_DCCP_IGNORE
107#define sIV CT_DCCP_INVALID
108
109/*
110 * DCCP state transistion table
111 *
112 * The assumption is the same as for TCP tracking:
113 *
114 * We are the man in the middle. All the packets go through us but might
115 * get lost in transit to the destination. It is assumed that the destination
116 * can't receive segments we haven't seen.
117 *
118 * The following states exist:
119 *
120 * NONE: Initial state, expecting Request
121 * REQUEST: Request seen, waiting for Response from server
122 * RESPOND: Response from server seen, waiting for Ack from client
123 * PARTOPEN: Ack after Response seen, waiting for packet other than Response,
124 * Reset or Sync from server
125 * OPEN: Packet other than Response, Reset or Sync seen
126 * CLOSEREQ: CloseReq from server seen, expecting Close from client
127 * CLOSING: Close seen, expecting Reset
128 * TIMEWAIT: Reset seen
129 * IGNORE: Not determinable whether packet is valid
130 *
131 * Some states exist only on one side of the connection: REQUEST, RESPOND,
132 * PARTOPEN, CLOSEREQ. For the other side these states are equivalent to
133 * the one it was in before.
134 *
135 * Packets are marked as ignored (sIG) if we don't know if they're valid
136 * (for example a reincarnation of a connection we didn't notice is dead
137 * already) and the server may send back a connection closing Reset or a
138 * Response. They're also used for Sync/SyncAck packets, which we don't
139 * care about.
140 */
141static const u_int8_t
142dccp_state_table[CT_DCCP_ROLE_MAX + 1][DCCP_PKT_SYNCACK + 1][CT_DCCP_MAX + 1] = {
143 [CT_DCCP_ROLE_CLIENT] = {
144 [DCCP_PKT_REQUEST] = {
145 /*
146 * sNO -> sRQ Regular Request
147 * sRQ -> sRQ Retransmitted Request or reincarnation
148 * sRS -> sRS Retransmitted Request (apparently Response
149 * got lost after we saw it) or reincarnation
150 * sPO -> sIG Ignore, conntrack might be out of sync
151 * sOP -> sIG Ignore, conntrack might be out of sync
152 * sCR -> sIG Ignore, conntrack might be out of sync
153 * sCG -> sIG Ignore, conntrack might be out of sync
154 * sTW -> sRQ Reincarnation
155 *
156 * sNO, sRQ, sRS, sPO. sOP, sCR, sCG, sTW, */
157 sRQ, sRQ, sRS, sIG, sIG, sIG, sIG, sRQ,
158 },
159 [DCCP_PKT_RESPONSE] = {
160 /*
161 * sNO -> sIV Invalid
162 * sRQ -> sIG Ignore, might be response to ignored Request
163 * sRS -> sIG Ignore, might be response to ignored Request
164 * sPO -> sIG Ignore, might be response to ignored Request
165 * sOP -> sIG Ignore, might be response to ignored Request
166 * sCR -> sIG Ignore, might be response to ignored Request
167 * sCG -> sIG Ignore, might be response to ignored Request
168 * sTW -> sIV Invalid, reincarnation in reverse direction
169 * goes through sRQ
170 *
171 * sNO, sRQ, sRS, sPO, sOP, sCR, sCG, sTW */
172 sIV, sIG, sIG, sIG, sIG, sIG, sIG, sIV,
173 },
174 [DCCP_PKT_ACK] = {
175 /*
176 * sNO -> sIV No connection
177 * sRQ -> sIV No connection
178 * sRS -> sPO Ack for Response, move to PARTOPEN (8.1.5.)
179 * sPO -> sPO Retransmitted Ack for Response, remain in PARTOPEN
180 * sOP -> sOP Regular ACK, remain in OPEN
181 * sCR -> sCR Ack in CLOSEREQ MAY be processed (8.3.)
182 * sCG -> sCG Ack in CLOSING MAY be processed (8.3.)
183 * sTW -> sIV
184 *
185 * sNO, sRQ, sRS, sPO, sOP, sCR, sCG, sTW */
186 sIV, sIV, sPO, sPO, sOP, sCR, sCG, sIV
187 },
188 [DCCP_PKT_DATA] = {
189 /*
190 * sNO -> sIV No connection
191 * sRQ -> sIV No connection
192 * sRS -> sIV No connection
193 * sPO -> sIV MUST use DataAck in PARTOPEN state (8.1.5.)
194 * sOP -> sOP Regular Data packet
195 * sCR -> sCR Data in CLOSEREQ MAY be processed (8.3.)
196 * sCG -> sCG Data in CLOSING MAY be processed (8.3.)
197 * sTW -> sIV
198 *
199 * sNO, sRQ, sRS, sPO, sOP, sCR, sCG, sTW */
200 sIV, sIV, sIV, sIV, sOP, sCR, sCG, sIV,
201 },
202 [DCCP_PKT_DATAACK] = {
203 /*
204 * sNO -> sIV No connection
205 * sRQ -> sIV No connection
206 * sRS -> sPO Ack for Response, move to PARTOPEN (8.1.5.)
207 * sPO -> sPO Remain in PARTOPEN state
208 * sOP -> sOP Regular DataAck packet in OPEN state
209 * sCR -> sCR DataAck in CLOSEREQ MAY be processed (8.3.)
210 * sCG -> sCG DataAck in CLOSING MAY be processed (8.3.)
211 * sTW -> sIV
212 *
213 * sNO, sRQ, sRS, sPO, sOP, sCR, sCG, sTW */
214 sIV, sIV, sPO, sPO, sOP, sCR, sCG, sIV
215 },
216 [DCCP_PKT_CLOSEREQ] = {
217 /*
218 * CLOSEREQ may only be sent by the server.
219 *
220 * sNO, sRQ, sRS, sPO, sOP, sCR, sCG, sTW */
221 sIV, sIV, sIV, sIV, sIV, sIV, sIV, sIV
222 },
223 [DCCP_PKT_CLOSE] = {
224 /*
225 * sNO -> sIV No connection
226 * sRQ -> sIV No connection
227 * sRS -> sIV No connection
228 * sPO -> sCG Client-initiated close
229 * sOP -> sCG Client-initiated close
230 * sCR -> sCG Close in response to CloseReq (8.3.)
231 * sCG -> sCG Retransmit
232 * sTW -> sIV Late retransmit, already in TIME_WAIT
233 *
234 * sNO, sRQ, sRS, sPO, sOP, sCR, sCG, sTW */
235 sIV, sIV, sIV, sCG, sCG, sCG, sIV, sIV
236 },
237 [DCCP_PKT_RESET] = {
238 /*
239 * sNO -> sIV No connection
240 * sRQ -> sTW Sync received or timeout, SHOULD send Reset (8.1.1.)
241 * sRS -> sTW Response received without Request
242 * sPO -> sTW Timeout, SHOULD send Reset (8.1.5.)
243 * sOP -> sTW Connection reset
244 * sCR -> sTW Connection reset
245 * sCG -> sTW Connection reset
246 * sTW -> sIG Ignore (don't refresh timer)
247 *
248 * sNO, sRQ, sRS, sPO, sOP, sCR, sCG, sTW */
249 sIV, sTW, sTW, sTW, sTW, sTW, sTW, sIG
250 },
251 [DCCP_PKT_SYNC] = {
252 /*
253 * We currently ignore Sync packets
254 *
255 * sNO, sRQ, sRS, sPO, sOP, sCR, sCG, sTW */
256 sIG, sIG, sIG, sIG, sIG, sIG, sIG, sIG,
257 },
258 [DCCP_PKT_SYNCACK] = {
259 /*
260 * We currently ignore SyncAck packets
261 *
262 * sNO, sRQ, sRS, sPO, sOP, sCR, sCG, sTW */
263 sIG, sIG, sIG, sIG, sIG, sIG, sIG, sIG,
264 },
265 },
266 [CT_DCCP_ROLE_SERVER] = {
267 [DCCP_PKT_REQUEST] = {
268 /*
269 * sNO -> sIV Invalid
270 * sRQ -> sIG Ignore, conntrack might be out of sync
271 * sRS -> sIG Ignore, conntrack might be out of sync
272 * sPO -> sIG Ignore, conntrack might be out of sync
273 * sOP -> sIG Ignore, conntrack might be out of sync
274 * sCR -> sIG Ignore, conntrack might be out of sync
275 * sCG -> sIG Ignore, conntrack might be out of sync
276 * sTW -> sRQ Reincarnation, must reverse roles
277 *
278 * sNO, sRQ, sRS, sPO, sOP, sCR, sCG, sTW */
279 sIV, sIG, sIG, sIG, sIG, sIG, sIG, sRQ
280 },
281 [DCCP_PKT_RESPONSE] = {
282 /*
283 * sNO -> sIV Response without Request
284 * sRQ -> sRS Response to clients Request
285 * sRS -> sRS Retransmitted Response (8.1.3. SHOULD NOT)
286 * sPO -> sIG Response to an ignored Request or late retransmit
287 * sOP -> sIG Ignore, might be response to ignored Request
288 * sCR -> sIG Ignore, might be response to ignored Request
289 * sCG -> sIG Ignore, might be response to ignored Request
290 * sTW -> sIV Invalid, Request from client in sTW moves to sRQ
291 *
292 * sNO, sRQ, sRS, sPO, sOP, sCR, sCG, sTW */
293 sIV, sRS, sRS, sIG, sIG, sIG, sIG, sIV
294 },
295 [DCCP_PKT_ACK] = {
296 /*
297 * sNO -> sIV No connection
298 * sRQ -> sIV No connection
299 * sRS -> sIV No connection
300 * sPO -> sOP Enter OPEN state (8.1.5.)
301 * sOP -> sOP Regular Ack in OPEN state
302 * sCR -> sIV Waiting for Close from client
303 * sCG -> sCG Ack in CLOSING MAY be processed (8.3.)
304 * sTW -> sIV
305 *
306 * sNO, sRQ, sRS, sPO, sOP, sCR, sCG, sTW */
307 sIV, sIV, sIV, sOP, sOP, sIV, sCG, sIV
308 },
309 [DCCP_PKT_DATA] = {
310 /*
311 * sNO -> sIV No connection
312 * sRQ -> sIV No connection
313 * sRS -> sIV No connection
314 * sPO -> sOP Enter OPEN state (8.1.5.)
315 * sOP -> sOP Regular Data packet in OPEN state
316 * sCR -> sIV Waiting for Close from client
317 * sCG -> sCG Data in CLOSING MAY be processed (8.3.)
318 * sTW -> sIV
319 *
320 * sNO, sRQ, sRS, sPO, sOP, sCR, sCG, sTW */
321 sIV, sIV, sIV, sOP, sOP, sIV, sCG, sIV
322 },
323 [DCCP_PKT_DATAACK] = {
324 /*
325 * sNO -> sIV No connection
326 * sRQ -> sIV No connection
327 * sRS -> sIV No connection
328 * sPO -> sOP Enter OPEN state (8.1.5.)
329 * sOP -> sOP Regular DataAck in OPEN state
330 * sCR -> sIV Waiting for Close from client
331 * sCG -> sCG Data in CLOSING MAY be processed (8.3.)
332 * sTW -> sIV
333 *
334 * sNO, sRQ, sRS, sPO, sOP, sCR, sCG, sTW */
335 sIV, sIV, sIV, sOP, sOP, sIV, sCG, sIV
336 },
337 [DCCP_PKT_CLOSEREQ] = {
338 /*
339 * sNO -> sIV No connection
340 * sRQ -> sIV No connection
341 * sRS -> sIV No connection
342 * sPO -> sOP -> sCR Move directly to CLOSEREQ (8.1.5.)
343 * sOP -> sCR CloseReq in OPEN state
344 * sCR -> sCR Retransmit
345 * sCG -> sCR Simultaneous close, client sends another Close
346 * sTW -> sIV Already closed
347 *
348 * sNO, sRQ, sRS, sPO, sOP, sCR, sCG, sTW */
349 sIV, sIV, sIV, sCR, sCR, sCR, sCR, sIV
350 },
351 [DCCP_PKT_CLOSE] = {
352 /*
353 * sNO -> sIV No connection
354 * sRQ -> sIV No connection
355 * sRS -> sIV No connection
356 * sPO -> sOP -> sCG Move direcly to CLOSING
357 * sOP -> sCG Move to CLOSING
358 * sCR -> sIV Close after CloseReq is invalid
359 * sCG -> sCG Retransmit
360 * sTW -> sIV Already closed
361 *
362 * sNO, sRQ, sRS, sPO, sOP, sCR, sCG, sTW */
363 sIV, sIV, sIV, sCG, sCG, sIV, sCG, sIV
364 },
365 [DCCP_PKT_RESET] = {
366 /*
367 * sNO -> sIV No connection
368 * sRQ -> sTW Reset in response to Request
369 * sRS -> sTW Timeout, SHOULD send Reset (8.1.3.)
370 * sPO -> sTW Timeout, SHOULD send Reset (8.1.3.)
371 * sOP -> sTW
372 * sCR -> sTW
373 * sCG -> sTW
374 * sTW -> sIG Ignore (don't refresh timer)
375 *
376 * sNO, sRQ, sRS, sPO, sOP, sCR, sCG, sTW, sTW */
377 sIV, sTW, sTW, sTW, sTW, sTW, sTW, sTW, sIG
378 },
379 [DCCP_PKT_SYNC] = {
380 /*
381 * We currently ignore Sync packets
382 *
383 * sNO, sRQ, sRS, sPO, sOP, sCR, sCG, sTW */
384 sIG, sIG, sIG, sIG, sIG, sIG, sIG, sIG,
385 },
386 [DCCP_PKT_SYNCACK] = {
387 /*
388 * We currently ignore SyncAck packets
389 *
390 * sNO, sRQ, sRS, sPO, sOP, sCR, sCG, sTW */
391 sIG, sIG, sIG, sIG, sIG, sIG, sIG, sIG,
392 },
393 },
394};
395
396static int dccp_pkt_to_tuple(const struct sk_buff *skb, unsigned int dataoff,
397 struct nf_conntrack_tuple *tuple)
398{
399 struct dccp_hdr _hdr, *dh;
400
401 dh = skb_header_pointer(skb, dataoff, sizeof(_hdr), &_hdr);
402 if (dh == NULL)
403 return 0;
404
405 tuple->src.u.dccp.port = dh->dccph_sport;
406 tuple->dst.u.dccp.port = dh->dccph_dport;
407 return 1;
408}
409
410static int dccp_invert_tuple(struct nf_conntrack_tuple *inv,
411 const struct nf_conntrack_tuple *tuple)
412{
413 inv->src.u.dccp.port = tuple->dst.u.dccp.port;
414 inv->dst.u.dccp.port = tuple->src.u.dccp.port;
415 return 1;
416}
417
418static int dccp_new(struct nf_conn *ct, const struct sk_buff *skb,
419 unsigned int dataoff)
420{
2bc78049
PM
421 struct dccp_hdr _dh, *dh;
422 const char *msg;
423 u_int8_t state;
424
425 dh = skb_header_pointer(skb, dataoff, sizeof(_dh), &dh);
426 BUG_ON(dh == NULL);
427
428 state = dccp_state_table[CT_DCCP_ROLE_CLIENT][dh->dccph_type][CT_DCCP_NONE];
429 switch (state) {
430 default:
431 if (nf_ct_dccp_loose == 0) {
432 msg = "nf_ct_dccp: not picking up existing connection ";
433 goto out_invalid;
434 }
435 case CT_DCCP_REQUEST:
436 break;
437 case CT_DCCP_INVALID:
438 msg = "nf_ct_dccp: invalid state transition ";
439 goto out_invalid;
440 }
441
442 ct->proto.dccp.role[IP_CT_DIR_ORIGINAL] = CT_DCCP_ROLE_CLIENT;
443 ct->proto.dccp.role[IP_CT_DIR_REPLY] = CT_DCCP_ROLE_SERVER;
444 ct->proto.dccp.state = CT_DCCP_NONE;
445 return 1;
446
447out_invalid:
448 if (LOG_INVALID(IPPROTO_DCCP))
5e8fbe2a 449 nf_log_packet(nf_ct_l3num(ct), 0, skb, NULL, NULL, NULL, msg);
2bc78049
PM
450 return 0;
451}
452
453static u64 dccp_ack_seq(const struct dccp_hdr *dh)
454{
455 const struct dccp_hdr_ack_bits *dhack;
456
457 dhack = (void *)dh + __dccp_basic_hdr_len(dh);
458 return ((u64)ntohs(dhack->dccph_ack_nr_high) << 32) +
459 ntohl(dhack->dccph_ack_nr_low);
460}
461
462static int dccp_packet(struct nf_conn *ct, const struct sk_buff *skb,
463 unsigned int dataoff, enum ip_conntrack_info ctinfo,
464 int pf, unsigned int hooknum)
465{
466 enum ip_conntrack_dir dir = CTINFO2DIR(ctinfo);
467 struct dccp_hdr _dh, *dh;
468 u_int8_t type, old_state, new_state;
469 enum ct_dccp_roles role;
470
471 dh = skb_header_pointer(skb, dataoff, sizeof(_dh), &dh);
472 BUG_ON(dh == NULL);
473 type = dh->dccph_type;
474
475 if (type == DCCP_PKT_RESET &&
476 !test_bit(IPS_SEEN_REPLY_BIT, &ct->status)) {
477 /* Tear down connection immediately if only reply is a RESET */
478 if (del_timer(&ct->timeout))
479 ct->timeout.function((unsigned long)ct);
480 return NF_ACCEPT;
481 }
482
483 write_lock_bh(&dccp_lock);
484
485 role = ct->proto.dccp.role[dir];
486 old_state = ct->proto.dccp.state;
487 new_state = dccp_state_table[role][type][old_state];
488
489 switch (new_state) {
490 case CT_DCCP_REQUEST:
491 if (old_state == CT_DCCP_TIMEWAIT &&
492 role == CT_DCCP_ROLE_SERVER) {
493 /* Reincarnation in the reverse direction: reopen and
494 * reverse client/server roles. */
495 ct->proto.dccp.role[dir] = CT_DCCP_ROLE_CLIENT;
496 ct->proto.dccp.role[!dir] = CT_DCCP_ROLE_SERVER;
497 }
498 break;
499 case CT_DCCP_RESPOND:
500 if (old_state == CT_DCCP_REQUEST)
501 ct->proto.dccp.handshake_seq = dccp_hdr_seq(dh);
502 break;
503 case CT_DCCP_PARTOPEN:
504 if (old_state == CT_DCCP_RESPOND &&
505 type == DCCP_PKT_ACK &&
506 dccp_ack_seq(dh) == ct->proto.dccp.handshake_seq)
507 set_bit(IPS_ASSURED_BIT, &ct->status);
508 break;
509 case CT_DCCP_IGNORE:
510 /*
511 * Connection tracking might be out of sync, so we ignore
512 * packets that might establish a new connection and resync
513 * if the server responds with a valid Response.
514 */
515 if (ct->proto.dccp.last_dir == !dir &&
516 ct->proto.dccp.last_pkt == DCCP_PKT_REQUEST &&
517 type == DCCP_PKT_RESPONSE) {
518 ct->proto.dccp.role[!dir] = CT_DCCP_ROLE_CLIENT;
519 ct->proto.dccp.role[dir] = CT_DCCP_ROLE_SERVER;
520 ct->proto.dccp.handshake_seq = dccp_hdr_seq(dh);
521 new_state = CT_DCCP_RESPOND;
522 break;
523 }
524 ct->proto.dccp.last_dir = dir;
525 ct->proto.dccp.last_pkt = type;
526
527 write_unlock_bh(&dccp_lock);
528 if (LOG_INVALID(IPPROTO_DCCP))
529 nf_log_packet(pf, 0, skb, NULL, NULL, NULL,
530 "nf_ct_dccp: invalid packet ignored ");
531 return NF_ACCEPT;
532 case CT_DCCP_INVALID:
533 write_unlock_bh(&dccp_lock);
534 if (LOG_INVALID(IPPROTO_DCCP))
535 nf_log_packet(pf, 0, skb, NULL, NULL, NULL,
536 "nf_ct_dccp: invalid state transition ");
537 return -NF_ACCEPT;
538 }
539
540 ct->proto.dccp.last_dir = dir;
541 ct->proto.dccp.last_pkt = type;
542 ct->proto.dccp.state = new_state;
543 write_unlock_bh(&dccp_lock);
544 nf_ct_refresh_acct(ct, ctinfo, skb, dccp_timeout[new_state]);
545
546 return NF_ACCEPT;
547}
548
549static int dccp_error(struct sk_buff *skb, unsigned int dataoff,
550 enum ip_conntrack_info *ctinfo, int pf,
551 unsigned int hooknum)
552{
553 struct dccp_hdr _dh, *dh;
554 unsigned int dccp_len = skb->len - dataoff;
555 unsigned int cscov;
556 const char *msg;
557
558 dh = skb_header_pointer(skb, dataoff, sizeof(_dh), &dh);
559 if (dh == NULL) {
560 msg = "nf_ct_dccp: short packet ";
561 goto out_invalid;
562 }
563
564 if (dh->dccph_doff * 4 < sizeof(struct dccp_hdr) ||
565 dh->dccph_doff * 4 > dccp_len) {
566 msg = "nf_ct_dccp: truncated/malformed packet ";
567 goto out_invalid;
568 }
569
570 cscov = dccp_len;
571 if (dh->dccph_cscov) {
572 cscov = (dh->dccph_cscov - 1) * 4;
573 if (cscov > dccp_len) {
574 msg = "nf_ct_dccp: bad checksum coverage ";
575 goto out_invalid;
576 }
577 }
578
579 if (nf_conntrack_checksum && hooknum == NF_INET_PRE_ROUTING &&
580 nf_checksum_partial(skb, hooknum, dataoff, cscov, IPPROTO_DCCP,
581 pf)) {
582 msg = "nf_ct_dccp: bad checksum ";
583 goto out_invalid;
584 }
585
586 if (dh->dccph_type >= DCCP_PKT_INVALID) {
587 msg = "nf_ct_dccp: reserved packet type ";
588 goto out_invalid;
589 }
590
591 return NF_ACCEPT;
592
593out_invalid:
594 if (LOG_INVALID(IPPROTO_DCCP))
595 nf_log_packet(pf, 0, skb, NULL, NULL, NULL, msg);
596 return -NF_ACCEPT;
597}
598
599static int dccp_print_tuple(struct seq_file *s,
600 const struct nf_conntrack_tuple *tuple)
601{
602 return seq_printf(s, "sport=%hu dport=%hu ",
603 ntohs(tuple->src.u.dccp.port),
604 ntohs(tuple->dst.u.dccp.port));
605}
606
607static int dccp_print_conntrack(struct seq_file *s, const struct nf_conn *ct)
608{
609 return seq_printf(s, "%s ", dccp_state_names[ct->proto.dccp.state]);
610}
611
612#if defined(CONFIG_NF_CT_NETLINK) || defined(CONFIG_NF_CT_NETLINK_MODULE)
613static int dccp_to_nlattr(struct sk_buff *skb, struct nlattr *nla,
614 const struct nf_conn *ct)
615{
616 struct nlattr *nest_parms;
617
618 read_lock_bh(&dccp_lock);
619 nest_parms = nla_nest_start(skb, CTA_PROTOINFO_DCCP | NLA_F_NESTED);
620 if (!nest_parms)
621 goto nla_put_failure;
622 NLA_PUT_U8(skb, CTA_PROTOINFO_DCCP_STATE, ct->proto.dccp.state);
623 nla_nest_end(skb, nest_parms);
624 read_unlock_bh(&dccp_lock);
625 return 0;
626
627nla_put_failure:
628 read_unlock_bh(&dccp_lock);
629 return -1;
630}
631
632static const struct nla_policy dccp_nla_policy[CTA_PROTOINFO_DCCP_MAX + 1] = {
633 [CTA_PROTOINFO_DCCP_STATE] = { .type = NLA_U8 },
634};
635
636static int nlattr_to_dccp(struct nlattr *cda[], struct nf_conn *ct)
637{
638 struct nlattr *attr = cda[CTA_PROTOINFO_DCCP];
639 struct nlattr *tb[CTA_PROTOINFO_DCCP_MAX + 1];
640 int err;
641
642 if (!attr)
643 return 0;
644
645 err = nla_parse_nested(tb, CTA_PROTOINFO_DCCP_MAX, attr,
646 dccp_nla_policy);
647 if (err < 0)
648 return err;
649
650 if (!tb[CTA_PROTOINFO_DCCP_STATE] ||
651 nla_get_u8(tb[CTA_PROTOINFO_DCCP_STATE]) >= CT_DCCP_IGNORE)
652 return -EINVAL;
653
654 write_lock_bh(&dccp_lock);
655 ct->proto.dccp.state = nla_get_u8(tb[CTA_PROTOINFO_DCCP_STATE]);
656 write_unlock_bh(&dccp_lock);
657 return 0;
658}
659#endif
660
661#ifdef CONFIG_SYSCTL
662static unsigned int dccp_sysctl_table_users;
663static struct ctl_table_header *dccp_sysctl_header;
664static ctl_table dccp_sysctl_table[] = {
665 {
666 .ctl_name = CTL_UNNUMBERED,
667 .procname = "nf_conntrack_dccp_timeout_request",
668 .data = &dccp_timeout[CT_DCCP_REQUEST],
669 .maxlen = sizeof(unsigned int),
670 .mode = 0644,
671 .proc_handler = proc_dointvec_jiffies,
672 },
673 {
674 .ctl_name = CTL_UNNUMBERED,
675 .procname = "nf_conntrack_dccp_timeout_respond",
676 .data = &dccp_timeout[CT_DCCP_RESPOND],
677 .maxlen = sizeof(unsigned int),
678 .mode = 0644,
679 .proc_handler = proc_dointvec_jiffies,
680 },
681 {
682 .ctl_name = CTL_UNNUMBERED,
683 .procname = "nf_conntrack_dccp_timeout_partopen",
684 .data = &dccp_timeout[CT_DCCP_PARTOPEN],
685 .maxlen = sizeof(unsigned int),
686 .mode = 0644,
687 .proc_handler = proc_dointvec_jiffies,
688 },
689 {
690 .ctl_name = CTL_UNNUMBERED,
691 .procname = "nf_conntrack_dccp_timeout_open",
692 .data = &dccp_timeout[CT_DCCP_OPEN],
693 .maxlen = sizeof(unsigned int),
694 .mode = 0644,
695 .proc_handler = proc_dointvec_jiffies,
696 },
697 {
698 .ctl_name = CTL_UNNUMBERED,
699 .procname = "nf_conntrack_dccp_timeout_closereq",
700 .data = &dccp_timeout[CT_DCCP_CLOSEREQ],
701 .maxlen = sizeof(unsigned int),
702 .mode = 0644,
703 .proc_handler = proc_dointvec_jiffies,
704 },
705 {
706 .ctl_name = CTL_UNNUMBERED,
707 .procname = "nf_conntrack_dccp_timeout_closing",
708 .data = &dccp_timeout[CT_DCCP_CLOSING],
709 .maxlen = sizeof(unsigned int),
710 .mode = 0644,
711 .proc_handler = proc_dointvec_jiffies,
712 },
713 {
714 .ctl_name = CTL_UNNUMBERED,
715 .procname = "nf_conntrack_dccp_timeout_timewait",
716 .data = &dccp_timeout[CT_DCCP_TIMEWAIT],
717 .maxlen = sizeof(unsigned int),
718 .mode = 0644,
719 .proc_handler = proc_dointvec_jiffies,
720 },
721 {
722 .ctl_name = CTL_UNNUMBERED,
723 .procname = "nf_conntrack_dccp_loose",
724 .data = &nf_ct_dccp_loose,
725 .maxlen = sizeof(nf_ct_dccp_loose),
726 .mode = 0644,
727 .proc_handler = proc_dointvec,
728 },
729 {
730 .ctl_name = 0,
731 }
732};
733#endif /* CONFIG_SYSCTL */
734
735static struct nf_conntrack_l4proto dccp_proto4 __read_mostly = {
736 .l3proto = AF_INET,
737 .l4proto = IPPROTO_DCCP,
738 .name = "dccp",
739 .pkt_to_tuple = dccp_pkt_to_tuple,
740 .invert_tuple = dccp_invert_tuple,
741 .new = dccp_new,
742 .packet = dccp_packet,
743 .error = dccp_error,
744 .print_tuple = dccp_print_tuple,
745 .print_conntrack = dccp_print_conntrack,
746#if defined(CONFIG_NF_CT_NETLINK) || defined(CONFIG_NF_CT_NETLINK_MODULE)
747 .to_nlattr = dccp_to_nlattr,
748 .from_nlattr = nlattr_to_dccp,
749 .tuple_to_nlattr = nf_ct_port_tuple_to_nlattr,
750 .nlattr_to_tuple = nf_ct_port_nlattr_to_tuple,
751 .nla_policy = nf_ct_port_nla_policy,
752#endif
753#ifdef CONFIG_SYSCTL
754 .ctl_table_users = &dccp_sysctl_table_users,
755 .ctl_table_header = &dccp_sysctl_header,
756 .ctl_table = dccp_sysctl_table,
757#endif
758};
759
760static struct nf_conntrack_l4proto dccp_proto6 __read_mostly = {
761 .l3proto = AF_INET6,
762 .l4proto = IPPROTO_DCCP,
763 .name = "dccp",
764 .pkt_to_tuple = dccp_pkt_to_tuple,
765 .invert_tuple = dccp_invert_tuple,
766 .new = dccp_new,
767 .packet = dccp_packet,
768 .error = dccp_error,
769 .print_tuple = dccp_print_tuple,
770 .print_conntrack = dccp_print_conntrack,
771#if defined(CONFIG_NF_CT_NETLINK) || defined(CONFIG_NF_CT_NETLINK_MODULE)
772 .to_nlattr = dccp_to_nlattr,
773 .from_nlattr = nlattr_to_dccp,
774 .tuple_to_nlattr = nf_ct_port_tuple_to_nlattr,
775 .nlattr_to_tuple = nf_ct_port_nlattr_to_tuple,
776 .nla_policy = nf_ct_port_nla_policy,
777#endif
778#ifdef CONFIG_SYSCTL
779 .ctl_table_users = &dccp_sysctl_table_users,
780 .ctl_table_header = &dccp_sysctl_header,
781 .ctl_table = dccp_sysctl_table,
782#endif
783};
784
785static int __init nf_conntrack_proto_dccp_init(void)
786{
787 int err;
788
789 err = nf_conntrack_l4proto_register(&dccp_proto4);
790 if (err < 0)
791 goto err1;
792
793 err = nf_conntrack_l4proto_register(&dccp_proto6);
794 if (err < 0)
795 goto err2;
796 return 0;
797
798err2:
799 nf_conntrack_l4proto_unregister(&dccp_proto4);
800err1:
801 return err;
802}
803
804static void __exit nf_conntrack_proto_dccp_fini(void)
805{
806 nf_conntrack_l4proto_unregister(&dccp_proto6);
807 nf_conntrack_l4proto_unregister(&dccp_proto4);
808}
809
810module_init(nf_conntrack_proto_dccp_init);
811module_exit(nf_conntrack_proto_dccp_fini);
812
813MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
814MODULE_DESCRIPTION("DCCP connection tracking protocol helper");
815MODULE_LICENSE("GPL");