]>
Commit | Line | Data |
---|---|---|
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 | ||
24 | static DEFINE_RWLOCK(dccp_lock); | |
25 | ||
26 | static 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 | ||
75 | static 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 | ||
85 | static 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 | */ | |
141 | static const u_int8_t | |
142 | dccp_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 | ||
09f263cd JE |
396 | static bool dccp_pkt_to_tuple(const struct sk_buff *skb, unsigned int dataoff, |
397 | struct nf_conntrack_tuple *tuple) | |
2bc78049 PM |
398 | { |
399 | struct dccp_hdr _hdr, *dh; | |
400 | ||
401 | dh = skb_header_pointer(skb, dataoff, sizeof(_hdr), &_hdr); | |
402 | if (dh == NULL) | |
09f263cd | 403 | return false; |
2bc78049 PM |
404 | |
405 | tuple->src.u.dccp.port = dh->dccph_sport; | |
406 | tuple->dst.u.dccp.port = dh->dccph_dport; | |
09f263cd | 407 | return true; |
2bc78049 PM |
408 | } |
409 | ||
09f263cd JE |
410 | static bool dccp_invert_tuple(struct nf_conntrack_tuple *inv, |
411 | const struct nf_conntrack_tuple *tuple) | |
2bc78049 PM |
412 | { |
413 | inv->src.u.dccp.port = tuple->dst.u.dccp.port; | |
414 | inv->dst.u.dccp.port = tuple->src.u.dccp.port; | |
09f263cd | 415 | return true; |
2bc78049 PM |
416 | } |
417 | ||
09f263cd JE |
418 | static bool dccp_new(struct nf_conn *ct, const struct sk_buff *skb, |
419 | unsigned int dataoff) | |
2bc78049 | 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; | |
09f263cd | 445 | return true; |
2bc78049 PM |
446 | |
447 | out_invalid: | |
448 | if (LOG_INVALID(IPPROTO_DCCP)) | |
5e8fbe2a | 449 | nf_log_packet(nf_ct_l3num(ct), 0, skb, NULL, NULL, NULL, msg); |
09f263cd | 450 | return false; |
2bc78049 PM |
451 | } |
452 | ||
453 | static 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 | ||
462 | static int dccp_packet(struct nf_conn *ct, const struct sk_buff *skb, | |
463 | unsigned int dataoff, enum ip_conntrack_info ctinfo, | |
76108cea | 464 | u_int8_t pf, unsigned int hooknum) |
2bc78049 PM |
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 */ | |
718d4ad9 | 478 | nf_ct_kill_acct(ct, ctinfo, skb); |
2bc78049 PM |
479 | return NF_ACCEPT; |
480 | } | |
481 | ||
482 | write_lock_bh(&dccp_lock); | |
483 | ||
484 | role = ct->proto.dccp.role[dir]; | |
485 | old_state = ct->proto.dccp.state; | |
486 | new_state = dccp_state_table[role][type][old_state]; | |
487 | ||
488 | switch (new_state) { | |
489 | case CT_DCCP_REQUEST: | |
490 | if (old_state == CT_DCCP_TIMEWAIT && | |
491 | role == CT_DCCP_ROLE_SERVER) { | |
492 | /* Reincarnation in the reverse direction: reopen and | |
493 | * reverse client/server roles. */ | |
494 | ct->proto.dccp.role[dir] = CT_DCCP_ROLE_CLIENT; | |
495 | ct->proto.dccp.role[!dir] = CT_DCCP_ROLE_SERVER; | |
496 | } | |
497 | break; | |
498 | case CT_DCCP_RESPOND: | |
499 | if (old_state == CT_DCCP_REQUEST) | |
500 | ct->proto.dccp.handshake_seq = dccp_hdr_seq(dh); | |
501 | break; | |
502 | case CT_DCCP_PARTOPEN: | |
503 | if (old_state == CT_DCCP_RESPOND && | |
504 | type == DCCP_PKT_ACK && | |
505 | dccp_ack_seq(dh) == ct->proto.dccp.handshake_seq) | |
506 | set_bit(IPS_ASSURED_BIT, &ct->status); | |
507 | break; | |
508 | case CT_DCCP_IGNORE: | |
509 | /* | |
510 | * Connection tracking might be out of sync, so we ignore | |
511 | * packets that might establish a new connection and resync | |
512 | * if the server responds with a valid Response. | |
513 | */ | |
514 | if (ct->proto.dccp.last_dir == !dir && | |
515 | ct->proto.dccp.last_pkt == DCCP_PKT_REQUEST && | |
516 | type == DCCP_PKT_RESPONSE) { | |
517 | ct->proto.dccp.role[!dir] = CT_DCCP_ROLE_CLIENT; | |
518 | ct->proto.dccp.role[dir] = CT_DCCP_ROLE_SERVER; | |
519 | ct->proto.dccp.handshake_seq = dccp_hdr_seq(dh); | |
520 | new_state = CT_DCCP_RESPOND; | |
521 | break; | |
522 | } | |
523 | ct->proto.dccp.last_dir = dir; | |
524 | ct->proto.dccp.last_pkt = type; | |
525 | ||
526 | write_unlock_bh(&dccp_lock); | |
527 | if (LOG_INVALID(IPPROTO_DCCP)) | |
528 | nf_log_packet(pf, 0, skb, NULL, NULL, NULL, | |
529 | "nf_ct_dccp: invalid packet ignored "); | |
530 | return NF_ACCEPT; | |
531 | case CT_DCCP_INVALID: | |
532 | write_unlock_bh(&dccp_lock); | |
533 | if (LOG_INVALID(IPPROTO_DCCP)) | |
534 | nf_log_packet(pf, 0, skb, NULL, NULL, NULL, | |
535 | "nf_ct_dccp: invalid state transition "); | |
536 | return -NF_ACCEPT; | |
537 | } | |
538 | ||
539 | ct->proto.dccp.last_dir = dir; | |
540 | ct->proto.dccp.last_pkt = type; | |
541 | ct->proto.dccp.state = new_state; | |
542 | write_unlock_bh(&dccp_lock); | |
543 | nf_ct_refresh_acct(ct, ctinfo, skb, dccp_timeout[new_state]); | |
544 | ||
545 | return NF_ACCEPT; | |
546 | } | |
547 | ||
74c51a14 AD |
548 | static int dccp_error(struct net *net, struct sk_buff *skb, |
549 | unsigned int dataoff, enum ip_conntrack_info *ctinfo, | |
550 | u_int8_t pf, unsigned int hooknum) | |
2bc78049 PM |
551 | { |
552 | struct dccp_hdr _dh, *dh; | |
553 | unsigned int dccp_len = skb->len - dataoff; | |
554 | unsigned int cscov; | |
555 | const char *msg; | |
556 | ||
557 | dh = skb_header_pointer(skb, dataoff, sizeof(_dh), &dh); | |
558 | if (dh == NULL) { | |
559 | msg = "nf_ct_dccp: short packet "; | |
560 | goto out_invalid; | |
561 | } | |
562 | ||
563 | if (dh->dccph_doff * 4 < sizeof(struct dccp_hdr) || | |
564 | dh->dccph_doff * 4 > dccp_len) { | |
565 | msg = "nf_ct_dccp: truncated/malformed packet "; | |
566 | goto out_invalid; | |
567 | } | |
568 | ||
569 | cscov = dccp_len; | |
570 | if (dh->dccph_cscov) { | |
571 | cscov = (dh->dccph_cscov - 1) * 4; | |
572 | if (cscov > dccp_len) { | |
573 | msg = "nf_ct_dccp: bad checksum coverage "; | |
574 | goto out_invalid; | |
575 | } | |
576 | } | |
577 | ||
c04d0552 | 578 | if (net->ct.sysctl_checksum && hooknum == NF_INET_PRE_ROUTING && |
2bc78049 PM |
579 | nf_checksum_partial(skb, hooknum, dataoff, cscov, IPPROTO_DCCP, |
580 | pf)) { | |
581 | msg = "nf_ct_dccp: bad checksum "; | |
582 | goto out_invalid; | |
583 | } | |
584 | ||
585 | if (dh->dccph_type >= DCCP_PKT_INVALID) { | |
586 | msg = "nf_ct_dccp: reserved packet type "; | |
587 | goto out_invalid; | |
588 | } | |
589 | ||
590 | return NF_ACCEPT; | |
591 | ||
592 | out_invalid: | |
593 | if (LOG_INVALID(IPPROTO_DCCP)) | |
594 | nf_log_packet(pf, 0, skb, NULL, NULL, NULL, msg); | |
595 | return -NF_ACCEPT; | |
596 | } | |
597 | ||
598 | static int dccp_print_tuple(struct seq_file *s, | |
599 | const struct nf_conntrack_tuple *tuple) | |
600 | { | |
601 | return seq_printf(s, "sport=%hu dport=%hu ", | |
602 | ntohs(tuple->src.u.dccp.port), | |
603 | ntohs(tuple->dst.u.dccp.port)); | |
604 | } | |
605 | ||
606 | static int dccp_print_conntrack(struct seq_file *s, const struct nf_conn *ct) | |
607 | { | |
608 | return seq_printf(s, "%s ", dccp_state_names[ct->proto.dccp.state]); | |
609 | } | |
610 | ||
611 | #if defined(CONFIG_NF_CT_NETLINK) || defined(CONFIG_NF_CT_NETLINK_MODULE) | |
612 | static int dccp_to_nlattr(struct sk_buff *skb, struct nlattr *nla, | |
613 | const struct nf_conn *ct) | |
614 | { | |
615 | struct nlattr *nest_parms; | |
616 | ||
617 | read_lock_bh(&dccp_lock); | |
618 | nest_parms = nla_nest_start(skb, CTA_PROTOINFO_DCCP | NLA_F_NESTED); | |
619 | if (!nest_parms) | |
620 | goto nla_put_failure; | |
621 | NLA_PUT_U8(skb, CTA_PROTOINFO_DCCP_STATE, ct->proto.dccp.state); | |
622 | nla_nest_end(skb, nest_parms); | |
623 | read_unlock_bh(&dccp_lock); | |
624 | return 0; | |
625 | ||
626 | nla_put_failure: | |
627 | read_unlock_bh(&dccp_lock); | |
628 | return -1; | |
629 | } | |
630 | ||
631 | static const struct nla_policy dccp_nla_policy[CTA_PROTOINFO_DCCP_MAX + 1] = { | |
632 | [CTA_PROTOINFO_DCCP_STATE] = { .type = NLA_U8 }, | |
633 | }; | |
634 | ||
635 | static int nlattr_to_dccp(struct nlattr *cda[], struct nf_conn *ct) | |
636 | { | |
637 | struct nlattr *attr = cda[CTA_PROTOINFO_DCCP]; | |
638 | struct nlattr *tb[CTA_PROTOINFO_DCCP_MAX + 1]; | |
639 | int err; | |
640 | ||
641 | if (!attr) | |
642 | return 0; | |
643 | ||
644 | err = nla_parse_nested(tb, CTA_PROTOINFO_DCCP_MAX, attr, | |
645 | dccp_nla_policy); | |
646 | if (err < 0) | |
647 | return err; | |
648 | ||
649 | if (!tb[CTA_PROTOINFO_DCCP_STATE] || | |
650 | nla_get_u8(tb[CTA_PROTOINFO_DCCP_STATE]) >= CT_DCCP_IGNORE) | |
651 | return -EINVAL; | |
652 | ||
653 | write_lock_bh(&dccp_lock); | |
654 | ct->proto.dccp.state = nla_get_u8(tb[CTA_PROTOINFO_DCCP_STATE]); | |
655 | write_unlock_bh(&dccp_lock); | |
656 | return 0; | |
657 | } | |
658 | #endif | |
659 | ||
660 | #ifdef CONFIG_SYSCTL | |
661 | static unsigned int dccp_sysctl_table_users; | |
662 | static struct ctl_table_header *dccp_sysctl_header; | |
663 | static ctl_table dccp_sysctl_table[] = { | |
664 | { | |
665 | .ctl_name = CTL_UNNUMBERED, | |
666 | .procname = "nf_conntrack_dccp_timeout_request", | |
667 | .data = &dccp_timeout[CT_DCCP_REQUEST], | |
668 | .maxlen = sizeof(unsigned int), | |
669 | .mode = 0644, | |
670 | .proc_handler = proc_dointvec_jiffies, | |
671 | }, | |
672 | { | |
673 | .ctl_name = CTL_UNNUMBERED, | |
674 | .procname = "nf_conntrack_dccp_timeout_respond", | |
675 | .data = &dccp_timeout[CT_DCCP_RESPOND], | |
676 | .maxlen = sizeof(unsigned int), | |
677 | .mode = 0644, | |
678 | .proc_handler = proc_dointvec_jiffies, | |
679 | }, | |
680 | { | |
681 | .ctl_name = CTL_UNNUMBERED, | |
682 | .procname = "nf_conntrack_dccp_timeout_partopen", | |
683 | .data = &dccp_timeout[CT_DCCP_PARTOPEN], | |
684 | .maxlen = sizeof(unsigned int), | |
685 | .mode = 0644, | |
686 | .proc_handler = proc_dointvec_jiffies, | |
687 | }, | |
688 | { | |
689 | .ctl_name = CTL_UNNUMBERED, | |
690 | .procname = "nf_conntrack_dccp_timeout_open", | |
691 | .data = &dccp_timeout[CT_DCCP_OPEN], | |
692 | .maxlen = sizeof(unsigned int), | |
693 | .mode = 0644, | |
694 | .proc_handler = proc_dointvec_jiffies, | |
695 | }, | |
696 | { | |
697 | .ctl_name = CTL_UNNUMBERED, | |
698 | .procname = "nf_conntrack_dccp_timeout_closereq", | |
699 | .data = &dccp_timeout[CT_DCCP_CLOSEREQ], | |
700 | .maxlen = sizeof(unsigned int), | |
701 | .mode = 0644, | |
702 | .proc_handler = proc_dointvec_jiffies, | |
703 | }, | |
704 | { | |
705 | .ctl_name = CTL_UNNUMBERED, | |
706 | .procname = "nf_conntrack_dccp_timeout_closing", | |
707 | .data = &dccp_timeout[CT_DCCP_CLOSING], | |
708 | .maxlen = sizeof(unsigned int), | |
709 | .mode = 0644, | |
710 | .proc_handler = proc_dointvec_jiffies, | |
711 | }, | |
712 | { | |
713 | .ctl_name = CTL_UNNUMBERED, | |
714 | .procname = "nf_conntrack_dccp_timeout_timewait", | |
715 | .data = &dccp_timeout[CT_DCCP_TIMEWAIT], | |
716 | .maxlen = sizeof(unsigned int), | |
717 | .mode = 0644, | |
718 | .proc_handler = proc_dointvec_jiffies, | |
719 | }, | |
720 | { | |
721 | .ctl_name = CTL_UNNUMBERED, | |
722 | .procname = "nf_conntrack_dccp_loose", | |
723 | .data = &nf_ct_dccp_loose, | |
724 | .maxlen = sizeof(nf_ct_dccp_loose), | |
725 | .mode = 0644, | |
726 | .proc_handler = proc_dointvec, | |
727 | }, | |
728 | { | |
729 | .ctl_name = 0, | |
730 | } | |
731 | }; | |
732 | #endif /* CONFIG_SYSCTL */ | |
733 | ||
734 | static struct nf_conntrack_l4proto dccp_proto4 __read_mostly = { | |
735 | .l3proto = AF_INET, | |
736 | .l4proto = IPPROTO_DCCP, | |
737 | .name = "dccp", | |
738 | .pkt_to_tuple = dccp_pkt_to_tuple, | |
739 | .invert_tuple = dccp_invert_tuple, | |
740 | .new = dccp_new, | |
741 | .packet = dccp_packet, | |
742 | .error = dccp_error, | |
743 | .print_tuple = dccp_print_tuple, | |
744 | .print_conntrack = dccp_print_conntrack, | |
745 | #if defined(CONFIG_NF_CT_NETLINK) || defined(CONFIG_NF_CT_NETLINK_MODULE) | |
746 | .to_nlattr = dccp_to_nlattr, | |
747 | .from_nlattr = nlattr_to_dccp, | |
748 | .tuple_to_nlattr = nf_ct_port_tuple_to_nlattr, | |
749 | .nlattr_to_tuple = nf_ct_port_nlattr_to_tuple, | |
750 | .nla_policy = nf_ct_port_nla_policy, | |
751 | #endif | |
752 | #ifdef CONFIG_SYSCTL | |
753 | .ctl_table_users = &dccp_sysctl_table_users, | |
754 | .ctl_table_header = &dccp_sysctl_header, | |
755 | .ctl_table = dccp_sysctl_table, | |
756 | #endif | |
757 | }; | |
758 | ||
759 | static struct nf_conntrack_l4proto dccp_proto6 __read_mostly = { | |
760 | .l3proto = AF_INET6, | |
761 | .l4proto = IPPROTO_DCCP, | |
762 | .name = "dccp", | |
763 | .pkt_to_tuple = dccp_pkt_to_tuple, | |
764 | .invert_tuple = dccp_invert_tuple, | |
765 | .new = dccp_new, | |
766 | .packet = dccp_packet, | |
767 | .error = dccp_error, | |
768 | .print_tuple = dccp_print_tuple, | |
769 | .print_conntrack = dccp_print_conntrack, | |
770 | #if defined(CONFIG_NF_CT_NETLINK) || defined(CONFIG_NF_CT_NETLINK_MODULE) | |
771 | .to_nlattr = dccp_to_nlattr, | |
772 | .from_nlattr = nlattr_to_dccp, | |
773 | .tuple_to_nlattr = nf_ct_port_tuple_to_nlattr, | |
774 | .nlattr_to_tuple = nf_ct_port_nlattr_to_tuple, | |
775 | .nla_policy = nf_ct_port_nla_policy, | |
776 | #endif | |
777 | #ifdef CONFIG_SYSCTL | |
778 | .ctl_table_users = &dccp_sysctl_table_users, | |
779 | .ctl_table_header = &dccp_sysctl_header, | |
780 | .ctl_table = dccp_sysctl_table, | |
781 | #endif | |
782 | }; | |
783 | ||
784 | static int __init nf_conntrack_proto_dccp_init(void) | |
785 | { | |
786 | int err; | |
787 | ||
788 | err = nf_conntrack_l4proto_register(&dccp_proto4); | |
789 | if (err < 0) | |
790 | goto err1; | |
791 | ||
792 | err = nf_conntrack_l4proto_register(&dccp_proto6); | |
793 | if (err < 0) | |
794 | goto err2; | |
795 | return 0; | |
796 | ||
797 | err2: | |
798 | nf_conntrack_l4proto_unregister(&dccp_proto4); | |
799 | err1: | |
800 | return err; | |
801 | } | |
802 | ||
803 | static void __exit nf_conntrack_proto_dccp_fini(void) | |
804 | { | |
805 | nf_conntrack_l4proto_unregister(&dccp_proto6); | |
806 | nf_conntrack_l4proto_unregister(&dccp_proto4); | |
807 | } | |
808 | ||
809 | module_init(nf_conntrack_proto_dccp_init); | |
810 | module_exit(nf_conntrack_proto_dccp_fini); | |
811 | ||
812 | MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>"); | |
813 | MODULE_DESCRIPTION("DCCP connection tracking protocol helper"); | |
814 | MODULE_LICENSE("GPL"); |