diff options
author | Arnaldo Carvalho de Melo <acme@ghostprotocols.net> | 2005-08-09 23:14:34 -0400 |
---|---|---|
committer | David S. Miller <davem@sunset.davemloft.net> | 2005-08-29 18:49:46 -0400 |
commit | 7c657876b63cb1d8a2ec06f8fc6c37bb8412e66c (patch) | |
tree | 3cb2732870c9cf8f976cb6fa57e0223f1c648e2a /net/dccp/ipv4.c | |
parent | c4365c9235f80128c3c3d5993074173941b1c1f0 (diff) |
[DCCP]: Initial implementation
Development to this point was done on a subversion repository at:
http://oops.ghostprotocols.net:81/cgi-bin/viewcvs.cgi/dccp-2.6/
This repository will be kept at this site for the foreseable future,
so that interested parties can see the history of this code,
attributions, etc.
If I ever decide to take this offline I'll provide the full history at
some other suitable place.
Signed-off-by: Arnaldo Carvalho de Melo <acme@ghostprotocols.net>
Signed-off-by: David S. Miller <davem@davemloft.net>
Diffstat (limited to 'net/dccp/ipv4.c')
-rw-r--r-- | net/dccp/ipv4.c | 1289 |
1 files changed, 1289 insertions, 0 deletions
diff --git a/net/dccp/ipv4.c b/net/dccp/ipv4.c new file mode 100644 index 000000000000..083bacaecb3b --- /dev/null +++ b/net/dccp/ipv4.c | |||
@@ -0,0 +1,1289 @@ | |||
1 | /* | ||
2 | * net/dccp/ipv4.c | ||
3 | * | ||
4 | * An implementation of the DCCP protocol | ||
5 | * Arnaldo Carvalho de Melo <acme@conectiva.com.br> | ||
6 | * | ||
7 | * This program is free software; you can redistribute it and/or | ||
8 | * modify it under the terms of the GNU General Public License | ||
9 | * as published by the Free Software Foundation; either version | ||
10 | * 2 of the License, or (at your option) any later version. | ||
11 | */ | ||
12 | |||
13 | #include <linux/config.h> | ||
14 | #include <linux/dccp.h> | ||
15 | #include <linux/icmp.h> | ||
16 | #include <linux/module.h> | ||
17 | #include <linux/skbuff.h> | ||
18 | #include <linux/random.h> | ||
19 | |||
20 | #include <net/icmp.h> | ||
21 | #include <net/inet_hashtables.h> | ||
22 | #include <net/sock.h> | ||
23 | #include <net/tcp_states.h> | ||
24 | #include <net/xfrm.h> | ||
25 | |||
26 | #include "ccid.h" | ||
27 | #include "dccp.h" | ||
28 | |||
29 | struct inet_hashinfo __cacheline_aligned dccp_hashinfo = { | ||
30 | .lhash_lock = RW_LOCK_UNLOCKED, | ||
31 | .lhash_users = ATOMIC_INIT(0), | ||
32 | .lhash_wait = __WAIT_QUEUE_HEAD_INITIALIZER(dccp_hashinfo.lhash_wait), | ||
33 | .portalloc_lock = SPIN_LOCK_UNLOCKED, | ||
34 | .port_rover = 1024 - 1, | ||
35 | }; | ||
36 | |||
37 | static int dccp_v4_get_port(struct sock *sk, const unsigned short snum) | ||
38 | { | ||
39 | return inet_csk_get_port(&dccp_hashinfo, sk, snum); | ||
40 | } | ||
41 | |||
42 | static void dccp_v4_hash(struct sock *sk) | ||
43 | { | ||
44 | inet_hash(&dccp_hashinfo, sk); | ||
45 | } | ||
46 | |||
47 | static void dccp_v4_unhash(struct sock *sk) | ||
48 | { | ||
49 | inet_unhash(&dccp_hashinfo, sk); | ||
50 | } | ||
51 | |||
52 | /* called with local bh disabled */ | ||
53 | static int __dccp_v4_check_established(struct sock *sk, const __u16 lport, | ||
54 | struct inet_timewait_sock **twp) | ||
55 | { | ||
56 | struct inet_sock *inet = inet_sk(sk); | ||
57 | const u32 daddr = inet->rcv_saddr; | ||
58 | const u32 saddr = inet->daddr; | ||
59 | const int dif = sk->sk_bound_dev_if; | ||
60 | INET_ADDR_COOKIE(acookie, saddr, daddr) | ||
61 | const __u32 ports = INET_COMBINED_PORTS(inet->dport, lport); | ||
62 | const int hash = inet_ehashfn(daddr, lport, saddr, inet->dport, dccp_hashinfo.ehash_size); | ||
63 | struct inet_ehash_bucket *head = &dccp_hashinfo.ehash[hash]; | ||
64 | const struct sock *sk2; | ||
65 | const struct hlist_node *node; | ||
66 | struct inet_timewait_sock *tw; | ||
67 | |||
68 | write_lock(&head->lock); | ||
69 | |||
70 | /* Check TIME-WAIT sockets first. */ | ||
71 | sk_for_each(sk2, node, &(head + dccp_hashinfo.ehash_size)->chain) { | ||
72 | tw = inet_twsk(sk2); | ||
73 | |||
74 | if (INET_TW_MATCH(sk2, acookie, saddr, daddr, ports, dif)) | ||
75 | goto not_unique; | ||
76 | } | ||
77 | tw = NULL; | ||
78 | |||
79 | /* And established part... */ | ||
80 | sk_for_each(sk2, node, &head->chain) { | ||
81 | if (INET_MATCH(sk2, acookie, saddr, daddr, ports, dif)) | ||
82 | goto not_unique; | ||
83 | } | ||
84 | |||
85 | /* Must record num and sport now. Otherwise we will see | ||
86 | * in hash table socket with a funny identity. */ | ||
87 | inet->num = lport; | ||
88 | inet->sport = htons(lport); | ||
89 | sk->sk_hashent = hash; | ||
90 | BUG_TRAP(sk_unhashed(sk)); | ||
91 | __sk_add_node(sk, &head->chain); | ||
92 | sock_prot_inc_use(sk->sk_prot); | ||
93 | write_unlock(&head->lock); | ||
94 | |||
95 | if (twp != NULL) { | ||
96 | *twp = tw; | ||
97 | NET_INC_STATS_BH(LINUX_MIB_TIMEWAITRECYCLED); | ||
98 | } else if (tw != NULL) { | ||
99 | /* Silly. Should hash-dance instead... */ | ||
100 | dccp_tw_deschedule(tw); | ||
101 | NET_INC_STATS_BH(LINUX_MIB_TIMEWAITRECYCLED); | ||
102 | |||
103 | inet_twsk_put(tw); | ||
104 | } | ||
105 | |||
106 | return 0; | ||
107 | |||
108 | not_unique: | ||
109 | write_unlock(&head->lock); | ||
110 | return -EADDRNOTAVAIL; | ||
111 | } | ||
112 | |||
113 | /* | ||
114 | * Bind a port for a connect operation and hash it. | ||
115 | */ | ||
116 | static int dccp_v4_hash_connect(struct sock *sk) | ||
117 | { | ||
118 | const unsigned short snum = inet_sk(sk)->num; | ||
119 | struct inet_bind_hashbucket *head; | ||
120 | struct inet_bind_bucket *tb; | ||
121 | int ret; | ||
122 | |||
123 | if (snum == 0) { | ||
124 | int rover; | ||
125 | int low = sysctl_local_port_range[0]; | ||
126 | int high = sysctl_local_port_range[1]; | ||
127 | int remaining = (high - low) + 1; | ||
128 | struct hlist_node *node; | ||
129 | struct inet_timewait_sock *tw = NULL; | ||
130 | |||
131 | local_bh_disable(); | ||
132 | |||
133 | /* TODO. Actually it is not so bad idea to remove | ||
134 | * dccp_hashinfo.portalloc_lock before next submission to Linus. | ||
135 | * As soon as we touch this place at all it is time to think. | ||
136 | * | ||
137 | * Now it protects single _advisory_ variable dccp_hashinfo.port_rover, | ||
138 | * hence it is mostly useless. | ||
139 | * Code will work nicely if we just delete it, but | ||
140 | * I am afraid in contented case it will work not better or | ||
141 | * even worse: another cpu just will hit the same bucket | ||
142 | * and spin there. | ||
143 | * So some cpu salt could remove both contention and | ||
144 | * memory pingpong. Any ideas how to do this in a nice way? | ||
145 | */ | ||
146 | spin_lock(&dccp_hashinfo.portalloc_lock); | ||
147 | rover = dccp_hashinfo.port_rover; | ||
148 | |||
149 | do { | ||
150 | rover++; | ||
151 | if ((rover < low) || (rover > high)) | ||
152 | rover = low; | ||
153 | head = &dccp_hashinfo.bhash[inet_bhashfn(rover, dccp_hashinfo.bhash_size)]; | ||
154 | spin_lock(&head->lock); | ||
155 | |||
156 | /* Does not bother with rcv_saddr checks, | ||
157 | * because the established check is already | ||
158 | * unique enough. | ||
159 | */ | ||
160 | inet_bind_bucket_for_each(tb, node, &head->chain) { | ||
161 | if (tb->port == rover) { | ||
162 | BUG_TRAP(!hlist_empty(&tb->owners)); | ||
163 | if (tb->fastreuse >= 0) | ||
164 | goto next_port; | ||
165 | if (!__dccp_v4_check_established(sk, | ||
166 | rover, | ||
167 | &tw)) | ||
168 | goto ok; | ||
169 | goto next_port; | ||
170 | } | ||
171 | } | ||
172 | |||
173 | tb = inet_bind_bucket_create(dccp_hashinfo.bind_bucket_cachep, head, rover); | ||
174 | if (tb == NULL) { | ||
175 | spin_unlock(&head->lock); | ||
176 | break; | ||
177 | } | ||
178 | tb->fastreuse = -1; | ||
179 | goto ok; | ||
180 | |||
181 | next_port: | ||
182 | spin_unlock(&head->lock); | ||
183 | } while (--remaining > 0); | ||
184 | dccp_hashinfo.port_rover = rover; | ||
185 | spin_unlock(&dccp_hashinfo.portalloc_lock); | ||
186 | |||
187 | local_bh_enable(); | ||
188 | |||
189 | return -EADDRNOTAVAIL; | ||
190 | |||
191 | ok: | ||
192 | /* All locks still held and bhs disabled */ | ||
193 | dccp_hashinfo.port_rover = rover; | ||
194 | spin_unlock(&dccp_hashinfo.portalloc_lock); | ||
195 | |||
196 | inet_bind_hash(sk, tb, rover); | ||
197 | if (sk_unhashed(sk)) { | ||
198 | inet_sk(sk)->sport = htons(rover); | ||
199 | __inet_hash(&dccp_hashinfo, sk, 0); | ||
200 | } | ||
201 | spin_unlock(&head->lock); | ||
202 | |||
203 | if (tw != NULL) { | ||
204 | dccp_tw_deschedule(tw); | ||
205 | inet_twsk_put(tw); | ||
206 | } | ||
207 | |||
208 | ret = 0; | ||
209 | goto out; | ||
210 | } | ||
211 | |||
212 | head = &dccp_hashinfo.bhash[inet_bhashfn(snum, dccp_hashinfo.bhash_size)]; | ||
213 | tb = inet_csk(sk)->icsk_bind_hash; | ||
214 | spin_lock_bh(&head->lock); | ||
215 | if (sk_head(&tb->owners) == sk && sk->sk_bind_node.next == NULL) { | ||
216 | __inet_hash(&dccp_hashinfo, sk, 0); | ||
217 | spin_unlock_bh(&head->lock); | ||
218 | return 0; | ||
219 | } else { | ||
220 | spin_unlock(&head->lock); | ||
221 | /* No definite answer... Walk to established hash table */ | ||
222 | ret = __dccp_v4_check_established(sk, snum, NULL); | ||
223 | out: | ||
224 | local_bh_enable(); | ||
225 | return ret; | ||
226 | } | ||
227 | } | ||
228 | |||
229 | static int dccp_v4_connect(struct sock *sk, struct sockaddr *uaddr, | ||
230 | int addr_len) | ||
231 | { | ||
232 | struct inet_sock *inet = inet_sk(sk); | ||
233 | struct dccp_sock *dp = dccp_sk(sk); | ||
234 | const struct sockaddr_in *usin = (struct sockaddr_in *)uaddr; | ||
235 | struct rtable *rt; | ||
236 | u32 daddr, nexthop; | ||
237 | int tmp; | ||
238 | int err; | ||
239 | |||
240 | dp->dccps_role = DCCP_ROLE_CLIENT; | ||
241 | |||
242 | if (addr_len < sizeof(struct sockaddr_in)) | ||
243 | return -EINVAL; | ||
244 | |||
245 | if (usin->sin_family != AF_INET) | ||
246 | return -EAFNOSUPPORT; | ||
247 | |||
248 | nexthop = daddr = usin->sin_addr.s_addr; | ||
249 | if (inet->opt != NULL && inet->opt->srr) { | ||
250 | if (daddr == 0) | ||
251 | return -EINVAL; | ||
252 | nexthop = inet->opt->faddr; | ||
253 | } | ||
254 | |||
255 | tmp = ip_route_connect(&rt, nexthop, inet->saddr, | ||
256 | RT_CONN_FLAGS(sk), sk->sk_bound_dev_if, | ||
257 | IPPROTO_DCCP, | ||
258 | inet->sport, usin->sin_port, sk); | ||
259 | if (tmp < 0) | ||
260 | return tmp; | ||
261 | |||
262 | if (rt->rt_flags & (RTCF_MULTICAST | RTCF_BROADCAST)) { | ||
263 | ip_rt_put(rt); | ||
264 | return -ENETUNREACH; | ||
265 | } | ||
266 | |||
267 | if (inet->opt == NULL || !inet->opt->srr) | ||
268 | daddr = rt->rt_dst; | ||
269 | |||
270 | if (inet->saddr == 0) | ||
271 | inet->saddr = rt->rt_src; | ||
272 | inet->rcv_saddr = inet->saddr; | ||
273 | |||
274 | inet->dport = usin->sin_port; | ||
275 | inet->daddr = daddr; | ||
276 | |||
277 | dp->dccps_ext_header_len = 0; | ||
278 | if (inet->opt != NULL) | ||
279 | dp->dccps_ext_header_len = inet->opt->optlen; | ||
280 | /* | ||
281 | * Socket identity is still unknown (sport may be zero). | ||
282 | * However we set state to DCCP_REQUESTING and not releasing socket | ||
283 | * lock select source port, enter ourselves into the hash tables and | ||
284 | * complete initialization after this. | ||
285 | */ | ||
286 | dccp_set_state(sk, DCCP_REQUESTING); | ||
287 | err = dccp_v4_hash_connect(sk); | ||
288 | if (err != 0) | ||
289 | goto failure; | ||
290 | |||
291 | err = ip_route_newports(&rt, inet->sport, inet->dport, sk); | ||
292 | if (err != 0) | ||
293 | goto failure; | ||
294 | |||
295 | /* OK, now commit destination to socket. */ | ||
296 | sk_setup_caps(sk, &rt->u.dst); | ||
297 | |||
298 | dp->dccps_gar = | ||
299 | dp->dccps_iss = secure_dccp_sequence_number(inet->saddr, | ||
300 | inet->daddr, | ||
301 | inet->sport, | ||
302 | usin->sin_port); | ||
303 | dccp_update_gss(sk, dp->dccps_iss); | ||
304 | |||
305 | inet->id = dp->dccps_iss ^ jiffies; | ||
306 | |||
307 | err = dccp_connect(sk); | ||
308 | rt = NULL; | ||
309 | if (err != 0) | ||
310 | goto failure; | ||
311 | out: | ||
312 | return err; | ||
313 | failure: | ||
314 | /* This unhashes the socket and releases the local port, if necessary. */ | ||
315 | dccp_set_state(sk, DCCP_CLOSED); | ||
316 | ip_rt_put(rt); | ||
317 | sk->sk_route_caps = 0; | ||
318 | inet->dport = 0; | ||
319 | goto out; | ||
320 | } | ||
321 | |||
322 | /* | ||
323 | * This routine does path mtu discovery as defined in RFC1191. | ||
324 | */ | ||
325 | static inline void dccp_do_pmtu_discovery(struct sock *sk, | ||
326 | const struct iphdr *iph, | ||
327 | u32 mtu) | ||
328 | { | ||
329 | struct dst_entry *dst; | ||
330 | const struct inet_sock *inet = inet_sk(sk); | ||
331 | const struct dccp_sock *dp = dccp_sk(sk); | ||
332 | |||
333 | /* We are not interested in DCCP_LISTEN and request_socks (RESPONSEs | ||
334 | * send out by Linux are always < 576bytes so they should go through | ||
335 | * unfragmented). | ||
336 | */ | ||
337 | if (sk->sk_state == DCCP_LISTEN) | ||
338 | return; | ||
339 | |||
340 | /* We don't check in the destentry if pmtu discovery is forbidden | ||
341 | * on this route. We just assume that no packet_to_big packets | ||
342 | * are send back when pmtu discovery is not active. | ||
343 | * There is a small race when the user changes this flag in the | ||
344 | * route, but I think that's acceptable. | ||
345 | */ | ||
346 | if ((dst = __sk_dst_check(sk, 0)) == NULL) | ||
347 | return; | ||
348 | |||
349 | dst->ops->update_pmtu(dst, mtu); | ||
350 | |||
351 | /* Something is about to be wrong... Remember soft error | ||
352 | * for the case, if this connection will not able to recover. | ||
353 | */ | ||
354 | if (mtu < dst_mtu(dst) && ip_dont_fragment(sk, dst)) | ||
355 | sk->sk_err_soft = EMSGSIZE; | ||
356 | |||
357 | mtu = dst_mtu(dst); | ||
358 | |||
359 | if (inet->pmtudisc != IP_PMTUDISC_DONT && | ||
360 | dp->dccps_pmtu_cookie > mtu) { | ||
361 | dccp_sync_mss(sk, mtu); | ||
362 | |||
363 | /* | ||
364 | * From: draft-ietf-dccp-spec-11.txt | ||
365 | * | ||
366 | * DCCP-Sync packets are the best choice for upward probing, | ||
367 | * since DCCP-Sync probes do not risk application data loss. | ||
368 | */ | ||
369 | dccp_send_sync(sk, dp->dccps_gsr); | ||
370 | } /* else let the usual retransmit timer handle it */ | ||
371 | } | ||
372 | |||
373 | static void dccp_v4_ctl_send_ack(struct sk_buff *rxskb) | ||
374 | { | ||
375 | int err; | ||
376 | struct dccp_hdr *rxdh = dccp_hdr(rxskb), *dh; | ||
377 | const int dccp_hdr_ack_len = sizeof(struct dccp_hdr) + | ||
378 | sizeof(struct dccp_hdr_ext) + | ||
379 | sizeof(struct dccp_hdr_ack_bits); | ||
380 | struct sk_buff *skb; | ||
381 | |||
382 | if (((struct rtable *)rxskb->dst)->rt_type != RTN_LOCAL) | ||
383 | return; | ||
384 | |||
385 | skb = alloc_skb(MAX_DCCP_HEADER + 15, GFP_ATOMIC); | ||
386 | if (skb == NULL) | ||
387 | return; | ||
388 | |||
389 | /* Reserve space for headers. */ | ||
390 | skb_reserve(skb, MAX_DCCP_HEADER); | ||
391 | |||
392 | skb->dst = dst_clone(rxskb->dst); | ||
393 | |||
394 | skb->h.raw = skb_push(skb, dccp_hdr_ack_len); | ||
395 | dh = dccp_hdr(skb); | ||
396 | memset(dh, 0, dccp_hdr_ack_len); | ||
397 | |||
398 | /* Build DCCP header and checksum it. */ | ||
399 | dh->dccph_type = DCCP_PKT_ACK; | ||
400 | dh->dccph_sport = rxdh->dccph_dport; | ||
401 | dh->dccph_dport = rxdh->dccph_sport; | ||
402 | dh->dccph_doff = dccp_hdr_ack_len / 4; | ||
403 | dh->dccph_x = 1; | ||
404 | |||
405 | dccp_hdr_set_seq(dh, DCCP_SKB_CB(rxskb)->dccpd_ack_seq); | ||
406 | dccp_hdr_set_ack(dccp_hdr_ack_bits(skb), DCCP_SKB_CB(rxskb)->dccpd_seq); | ||
407 | |||
408 | bh_lock_sock(dccp_ctl_socket->sk); | ||
409 | err = ip_build_and_send_pkt(skb, dccp_ctl_socket->sk, | ||
410 | rxskb->nh.iph->daddr, rxskb->nh.iph->saddr, NULL); | ||
411 | bh_unlock_sock(dccp_ctl_socket->sk); | ||
412 | |||
413 | if (err == NET_XMIT_CN || err == 0) { | ||
414 | DCCP_INC_STATS_BH(DCCP_MIB_OUTSEGS); | ||
415 | DCCP_INC_STATS_BH(DCCP_MIB_OUTRSTS); | ||
416 | } | ||
417 | } | ||
418 | |||
419 | static void dccp_v4_reqsk_send_ack(struct sk_buff *skb, struct request_sock *req) | ||
420 | { | ||
421 | dccp_v4_ctl_send_ack(skb); | ||
422 | } | ||
423 | |||
424 | static int dccp_v4_send_response(struct sock *sk, struct request_sock *req, | ||
425 | struct dst_entry *dst) | ||
426 | { | ||
427 | int err = -1; | ||
428 | struct sk_buff *skb; | ||
429 | |||
430 | /* First, grab a route. */ | ||
431 | |||
432 | if (dst == NULL && (dst = inet_csk_route_req(sk, req)) == NULL) | ||
433 | goto out; | ||
434 | |||
435 | skb = dccp_make_response(sk, dst, req); | ||
436 | if (skb != NULL) { | ||
437 | const struct inet_request_sock *ireq = inet_rsk(req); | ||
438 | |||
439 | err = ip_build_and_send_pkt(skb, sk, ireq->loc_addr, | ||
440 | ireq->rmt_addr, | ||
441 | ireq->opt); | ||
442 | if (err == NET_XMIT_CN) | ||
443 | err = 0; | ||
444 | } | ||
445 | |||
446 | out: | ||
447 | dst_release(dst); | ||
448 | return err; | ||
449 | } | ||
450 | |||
451 | /* | ||
452 | * This routine is called by the ICMP module when it gets some sort of error | ||
453 | * condition. If err < 0 then the socket should be closed and the error | ||
454 | * returned to the user. If err > 0 it's just the icmp type << 8 | icmp code. | ||
455 | * After adjustment header points to the first 8 bytes of the tcp header. We | ||
456 | * need to find the appropriate port. | ||
457 | * | ||
458 | * The locking strategy used here is very "optimistic". When someone else | ||
459 | * accesses the socket the ICMP is just dropped and for some paths there is no | ||
460 | * check at all. A more general error queue to queue errors for later handling | ||
461 | * is probably better. | ||
462 | */ | ||
463 | void dccp_v4_err(struct sk_buff *skb, u32 info) | ||
464 | { | ||
465 | const struct iphdr *iph = (struct iphdr *)skb->data; | ||
466 | const struct dccp_hdr *dh = (struct dccp_hdr *)(skb->data + (iph->ihl << 2)); | ||
467 | struct dccp_sock *dp; | ||
468 | struct inet_sock *inet; | ||
469 | const int type = skb->h.icmph->type; | ||
470 | const int code = skb->h.icmph->code; | ||
471 | struct sock *sk; | ||
472 | __u64 seq; | ||
473 | int err; | ||
474 | |||
475 | if (skb->len < (iph->ihl << 2) + 8) { | ||
476 | ICMP_INC_STATS_BH(ICMP_MIB_INERRORS); | ||
477 | return; | ||
478 | } | ||
479 | |||
480 | sk = inet_lookup(&dccp_hashinfo, iph->daddr, dh->dccph_dport, | ||
481 | iph->saddr, dh->dccph_sport, inet_iif(skb)); | ||
482 | if (sk == NULL) { | ||
483 | ICMP_INC_STATS_BH(ICMP_MIB_INERRORS); | ||
484 | return; | ||
485 | } | ||
486 | |||
487 | if (sk->sk_state == DCCP_TIME_WAIT) { | ||
488 | inet_twsk_put((struct inet_timewait_sock *)sk); | ||
489 | return; | ||
490 | } | ||
491 | |||
492 | bh_lock_sock(sk); | ||
493 | /* If too many ICMPs get dropped on busy | ||
494 | * servers this needs to be solved differently. | ||
495 | */ | ||
496 | if (sock_owned_by_user(sk)) | ||
497 | NET_INC_STATS_BH(LINUX_MIB_LOCKDROPPEDICMPS); | ||
498 | |||
499 | if (sk->sk_state == DCCP_CLOSED) | ||
500 | goto out; | ||
501 | |||
502 | dp = dccp_sk(sk); | ||
503 | seq = dccp_hdr_seq(skb); | ||
504 | if (sk->sk_state != DCCP_LISTEN && | ||
505 | !between48(seq, dp->dccps_swl, dp->dccps_swh)) { | ||
506 | NET_INC_STATS(LINUX_MIB_OUTOFWINDOWICMPS); | ||
507 | goto out; | ||
508 | } | ||
509 | |||
510 | switch (type) { | ||
511 | case ICMP_SOURCE_QUENCH: | ||
512 | /* Just silently ignore these. */ | ||
513 | goto out; | ||
514 | case ICMP_PARAMETERPROB: | ||
515 | err = EPROTO; | ||
516 | break; | ||
517 | case ICMP_DEST_UNREACH: | ||
518 | if (code > NR_ICMP_UNREACH) | ||
519 | goto out; | ||
520 | |||
521 | if (code == ICMP_FRAG_NEEDED) { /* PMTU discovery (RFC1191) */ | ||
522 | if (!sock_owned_by_user(sk)) | ||
523 | dccp_do_pmtu_discovery(sk, iph, info); | ||
524 | goto out; | ||
525 | } | ||
526 | |||
527 | err = icmp_err_convert[code].errno; | ||
528 | break; | ||
529 | case ICMP_TIME_EXCEEDED: | ||
530 | err = EHOSTUNREACH; | ||
531 | break; | ||
532 | default: | ||
533 | goto out; | ||
534 | } | ||
535 | |||
536 | switch (sk->sk_state) { | ||
537 | struct request_sock *req , **prev; | ||
538 | case DCCP_LISTEN: | ||
539 | if (sock_owned_by_user(sk)) | ||
540 | goto out; | ||
541 | req = inet_csk_search_req(sk, &prev, dh->dccph_dport, | ||
542 | iph->daddr, iph->saddr); | ||
543 | if (!req) | ||
544 | goto out; | ||
545 | |||
546 | /* | ||
547 | * ICMPs are not backlogged, hence we cannot get an established | ||
548 | * socket here. | ||
549 | */ | ||
550 | BUG_TRAP(!req->sk); | ||
551 | |||
552 | if (seq != dccp_rsk(req)->dreq_iss) { | ||
553 | NET_INC_STATS_BH(LINUX_MIB_OUTOFWINDOWICMPS); | ||
554 | goto out; | ||
555 | } | ||
556 | /* | ||
557 | * Still in RESPOND, just remove it silently. | ||
558 | * There is no good way to pass the error to the newly | ||
559 | * created socket, and POSIX does not want network | ||
560 | * errors returned from accept(). | ||
561 | */ | ||
562 | inet_csk_reqsk_queue_drop(sk, req, prev); | ||
563 | goto out; | ||
564 | |||
565 | case DCCP_REQUESTING: | ||
566 | case DCCP_RESPOND: | ||
567 | if (!sock_owned_by_user(sk)) { | ||
568 | DCCP_INC_STATS_BH(DCCP_MIB_ATTEMPTFAILS); | ||
569 | sk->sk_err = err; | ||
570 | |||
571 | sk->sk_error_report(sk); | ||
572 | |||
573 | dccp_done(sk); | ||
574 | } else | ||
575 | sk->sk_err_soft = err; | ||
576 | goto out; | ||
577 | } | ||
578 | |||
579 | /* If we've already connected we will keep trying | ||
580 | * until we time out, or the user gives up. | ||
581 | * | ||
582 | * rfc1122 4.2.3.9 allows to consider as hard errors | ||
583 | * only PROTO_UNREACH and PORT_UNREACH (well, FRAG_FAILED too, | ||
584 | * but it is obsoleted by pmtu discovery). | ||
585 | * | ||
586 | * Note, that in modern internet, where routing is unreliable | ||
587 | * and in each dark corner broken firewalls sit, sending random | ||
588 | * errors ordered by their masters even this two messages finally lose | ||
589 | * their original sense (even Linux sends invalid PORT_UNREACHs) | ||
590 | * | ||
591 | * Now we are in compliance with RFCs. | ||
592 | * --ANK (980905) | ||
593 | */ | ||
594 | |||
595 | inet = inet_sk(sk); | ||
596 | if (!sock_owned_by_user(sk) && inet->recverr) { | ||
597 | sk->sk_err = err; | ||
598 | sk->sk_error_report(sk); | ||
599 | } else /* Only an error on timeout */ | ||
600 | sk->sk_err_soft = err; | ||
601 | out: | ||
602 | bh_unlock_sock(sk); | ||
603 | sock_put(sk); | ||
604 | } | ||
605 | |||
606 | extern struct sk_buff *dccp_make_reset(struct sock *sk, struct dst_entry *dst, enum dccp_reset_codes code); | ||
607 | |||
608 | int dccp_v4_send_reset(struct sock *sk, enum dccp_reset_codes code) | ||
609 | { | ||
610 | struct sk_buff *skb; | ||
611 | /* | ||
612 | * FIXME: what if rebuild_header fails? | ||
613 | * Should we be doing a rebuild_header here? | ||
614 | */ | ||
615 | int err = inet_sk_rebuild_header(sk); | ||
616 | |||
617 | if (err != 0) | ||
618 | return err; | ||
619 | |||
620 | skb = dccp_make_reset(sk, sk->sk_dst_cache, code); | ||
621 | if (skb != NULL) { | ||
622 | const struct dccp_sock *dp = dccp_sk(sk); | ||
623 | const struct inet_sock *inet = inet_sk(sk); | ||
624 | |||
625 | err = ip_build_and_send_pkt(skb, sk, | ||
626 | inet->saddr, inet->daddr, NULL); | ||
627 | if (err == NET_XMIT_CN) | ||
628 | err = 0; | ||
629 | |||
630 | ccid_hc_rx_exit(dp->dccps_hc_rx_ccid, sk); | ||
631 | ccid_hc_tx_exit(dp->dccps_hc_tx_ccid, sk); | ||
632 | } | ||
633 | |||
634 | return err; | ||
635 | } | ||
636 | |||
637 | static inline u64 dccp_v4_init_sequence(const struct sock *sk, | ||
638 | const struct sk_buff *skb) | ||
639 | { | ||
640 | return secure_dccp_sequence_number(skb->nh.iph->daddr, | ||
641 | skb->nh.iph->saddr, | ||
642 | dccp_hdr(skb)->dccph_dport, | ||
643 | dccp_hdr(skb)->dccph_sport); | ||
644 | } | ||
645 | |||
646 | int dccp_v4_conn_request(struct sock *sk, struct sk_buff *skb) | ||
647 | { | ||
648 | struct inet_request_sock *ireq; | ||
649 | struct dccp_sock dp; | ||
650 | struct request_sock *req; | ||
651 | struct dccp_request_sock *dreq; | ||
652 | const __u32 saddr = skb->nh.iph->saddr; | ||
653 | const __u32 daddr = skb->nh.iph->daddr; | ||
654 | struct dst_entry *dst = NULL; | ||
655 | |||
656 | /* Never answer to DCCP_PKT_REQUESTs send to broadcast or multicast */ | ||
657 | if (((struct rtable *)skb->dst)->rt_flags & | ||
658 | (RTCF_BROADCAST | RTCF_MULTICAST)) | ||
659 | goto drop; | ||
660 | |||
661 | /* | ||
662 | * TW buckets are converted to open requests without | ||
663 | * limitations, they conserve resources and peer is | ||
664 | * evidently real one. | ||
665 | */ | ||
666 | if (inet_csk_reqsk_queue_is_full(sk)) | ||
667 | goto drop; | ||
668 | |||
669 | /* | ||
670 | * Accept backlog is full. If we have already queued enough | ||
671 | * of warm entries in syn queue, drop request. It is better than | ||
672 | * clogging syn queue with openreqs with exponentially increasing | ||
673 | * timeout. | ||
674 | */ | ||
675 | if (sk_acceptq_is_full(sk) && inet_csk_reqsk_queue_young(sk) > 1) | ||
676 | goto drop; | ||
677 | |||
678 | req = reqsk_alloc(sk->sk_prot->rsk_prot); | ||
679 | if (req == NULL) | ||
680 | goto drop; | ||
681 | |||
682 | /* FIXME: process options */ | ||
683 | |||
684 | dccp_openreq_init(req, &dp, skb); | ||
685 | |||
686 | ireq = inet_rsk(req); | ||
687 | ireq->loc_addr = daddr; | ||
688 | ireq->rmt_addr = saddr; | ||
689 | /* FIXME: Merge Aristeu's option parsing code when ready */ | ||
690 | req->rcv_wnd = 100; /* Fake, option parsing will get the right value */ | ||
691 | ireq->opt = NULL; | ||
692 | |||
693 | /* | ||
694 | * Step 3: Process LISTEN state | ||
695 | * | ||
696 | * Set S.ISR, S.GSR, S.SWL, S.SWH from packet or Init Cookie | ||
697 | * | ||
698 | * In fact we defer setting S.GSR, S.SWL, S.SWH to | ||
699 | * dccp_create_openreq_child. | ||
700 | */ | ||
701 | dreq = dccp_rsk(req); | ||
702 | dreq->dreq_isr = DCCP_SKB_CB(skb)->dccpd_seq; | ||
703 | dreq->dreq_iss = dccp_v4_init_sequence(sk, skb); | ||
704 | dreq->dreq_service = dccp_hdr_request(skb)->dccph_req_service; | ||
705 | |||
706 | if (dccp_v4_send_response(sk, req, dst)) | ||
707 | goto drop_and_free; | ||
708 | |||
709 | inet_csk_reqsk_queue_hash_add(sk, req, DCCP_TIMEOUT_INIT); | ||
710 | return 0; | ||
711 | |||
712 | drop_and_free: | ||
713 | /* | ||
714 | * FIXME: should be reqsk_free after implementing req->rsk_ops | ||
715 | */ | ||
716 | __reqsk_free(req); | ||
717 | drop: | ||
718 | DCCP_INC_STATS_BH(DCCP_MIB_ATTEMPTFAILS); | ||
719 | return -1; | ||
720 | } | ||
721 | |||
722 | /* | ||
723 | * The three way handshake has completed - we got a valid ACK or DATAACK - | ||
724 | * now create the new socket. | ||
725 | * | ||
726 | * This is the equivalent of TCP's tcp_v4_syn_recv_sock | ||
727 | */ | ||
728 | struct sock *dccp_v4_request_recv_sock(struct sock *sk, struct sk_buff *skb, | ||
729 | struct request_sock *req, | ||
730 | struct dst_entry *dst) | ||
731 | { | ||
732 | struct inet_request_sock *ireq; | ||
733 | struct inet_sock *newinet; | ||
734 | struct dccp_sock *newdp; | ||
735 | struct sock *newsk; | ||
736 | |||
737 | if (sk_acceptq_is_full(sk)) | ||
738 | goto exit_overflow; | ||
739 | |||
740 | if (dst == NULL && (dst = inet_csk_route_req(sk, req)) == NULL) | ||
741 | goto exit; | ||
742 | |||
743 | newsk = dccp_create_openreq_child(sk, req, skb); | ||
744 | if (newsk == NULL) | ||
745 | goto exit; | ||
746 | |||
747 | sk_setup_caps(newsk, dst); | ||
748 | |||
749 | newdp = dccp_sk(newsk); | ||
750 | newinet = inet_sk(newsk); | ||
751 | ireq = inet_rsk(req); | ||
752 | newinet->daddr = ireq->rmt_addr; | ||
753 | newinet->rcv_saddr = ireq->loc_addr; | ||
754 | newinet->saddr = ireq->loc_addr; | ||
755 | newinet->opt = ireq->opt; | ||
756 | ireq->opt = NULL; | ||
757 | newinet->mc_index = inet_iif(skb); | ||
758 | newinet->mc_ttl = skb->nh.iph->ttl; | ||
759 | newinet->id = jiffies; | ||
760 | |||
761 | dccp_sync_mss(newsk, dst_mtu(dst)); | ||
762 | |||
763 | __inet_hash(&dccp_hashinfo, newsk, 0); | ||
764 | __inet_inherit_port(&dccp_hashinfo, sk, newsk); | ||
765 | |||
766 | return newsk; | ||
767 | |||
768 | exit_overflow: | ||
769 | NET_INC_STATS_BH(LINUX_MIB_LISTENOVERFLOWS); | ||
770 | exit: | ||
771 | NET_INC_STATS_BH(LINUX_MIB_LISTENDROPS); | ||
772 | dst_release(dst); | ||
773 | return NULL; | ||
774 | } | ||
775 | |||
776 | static struct sock *dccp_v4_hnd_req(struct sock *sk, struct sk_buff *skb) | ||
777 | { | ||
778 | const struct dccp_hdr *dh = dccp_hdr(skb); | ||
779 | const struct iphdr *iph = skb->nh.iph; | ||
780 | struct sock *nsk; | ||
781 | struct request_sock **prev; | ||
782 | /* Find possible connection requests. */ | ||
783 | struct request_sock *req = inet_csk_search_req(sk, &prev, | ||
784 | dh->dccph_sport, | ||
785 | iph->saddr, iph->daddr); | ||
786 | if (req != NULL) | ||
787 | return dccp_check_req(sk, skb, req, prev); | ||
788 | |||
789 | nsk = __inet_lookup_established(&dccp_hashinfo, | ||
790 | iph->saddr, dh->dccph_sport, | ||
791 | iph->daddr, ntohs(dh->dccph_dport), | ||
792 | inet_iif(skb)); | ||
793 | if (nsk != NULL) { | ||
794 | if (nsk->sk_state != DCCP_TIME_WAIT) { | ||
795 | bh_lock_sock(nsk); | ||
796 | return nsk; | ||
797 | } | ||
798 | inet_twsk_put((struct inet_timewait_sock *)nsk); | ||
799 | return NULL; | ||
800 | } | ||
801 | |||
802 | return sk; | ||
803 | } | ||
804 | |||
805 | int dccp_v4_checksum(struct sk_buff *skb) | ||
806 | { | ||
807 | struct dccp_hdr* dh = dccp_hdr(skb); | ||
808 | int checksum_len; | ||
809 | u32 tmp; | ||
810 | |||
811 | if (dh->dccph_cscov == 0) | ||
812 | checksum_len = skb->len; | ||
813 | else { | ||
814 | checksum_len = (dh->dccph_cscov + dh->dccph_x) * sizeof(u32); | ||
815 | checksum_len = checksum_len < skb->len ? checksum_len : skb->len; | ||
816 | } | ||
817 | |||
818 | tmp = csum_partial((unsigned char *)dh, checksum_len, 0); | ||
819 | return csum_fold(tmp); | ||
820 | } | ||
821 | |||
822 | static int dccp_v4_verify_checksum(struct sk_buff *skb) | ||
823 | { | ||
824 | struct dccp_hdr *th = dccp_hdr(skb); | ||
825 | const u16 remote_checksum = th->dccph_checksum; | ||
826 | u16 local_checksum; | ||
827 | |||
828 | /* FIXME: don't mess with skb payload */ | ||
829 | th->dccph_checksum = 0; /* zero it for computation */ | ||
830 | |||
831 | local_checksum = dccp_v4_checksum(skb); | ||
832 | |||
833 | /* FIXME: don't mess with skb payload */ | ||
834 | th->dccph_checksum = remote_checksum; /* put it back */ | ||
835 | |||
836 | return remote_checksum == local_checksum ? 0 : -1; | ||
837 | } | ||
838 | |||
839 | static struct dst_entry* dccp_v4_route_skb(struct sock *sk, | ||
840 | struct sk_buff *skb) | ||
841 | { | ||
842 | struct rtable *rt; | ||
843 | struct flowi fl = { .oif = ((struct rtable *)skb->dst)->rt_iif, | ||
844 | .nl_u = { .ip4_u = | ||
845 | { .daddr = skb->nh.iph->saddr, | ||
846 | .saddr = skb->nh.iph->daddr, | ||
847 | .tos = RT_CONN_FLAGS(sk) } }, | ||
848 | .proto = sk->sk_protocol, | ||
849 | .uli_u = { .ports = | ||
850 | { .sport = dccp_hdr(skb)->dccph_dport, | ||
851 | .dport = dccp_hdr(skb)->dccph_sport } } }; | ||
852 | |||
853 | if (ip_route_output_flow(&rt, &fl, sk, 0)) { | ||
854 | IP_INC_STATS_BH(IPSTATS_MIB_OUTNOROUTES); | ||
855 | return NULL; | ||
856 | } | ||
857 | |||
858 | return &rt->u.dst; | ||
859 | } | ||
860 | |||
861 | void dccp_v4_ctl_send_reset(struct sk_buff *rxskb) | ||
862 | { | ||
863 | int err; | ||
864 | struct dccp_hdr *rxdh = dccp_hdr(rxskb), *dh; | ||
865 | const int dccp_hdr_reset_len = sizeof(struct dccp_hdr) + | ||
866 | sizeof(struct dccp_hdr_ext) + | ||
867 | sizeof(struct dccp_hdr_reset); | ||
868 | struct sk_buff *skb; | ||
869 | struct dst_entry *dst; | ||
870 | |||
871 | /* Never send a reset in response to a reset. */ | ||
872 | if (rxdh->dccph_type == DCCP_PKT_RESET) | ||
873 | return; | ||
874 | |||
875 | if (((struct rtable *)rxskb->dst)->rt_type != RTN_LOCAL) | ||
876 | return; | ||
877 | |||
878 | dst = dccp_v4_route_skb(dccp_ctl_socket->sk, rxskb); | ||
879 | if (dst == NULL) | ||
880 | return; | ||
881 | |||
882 | skb = alloc_skb(MAX_DCCP_HEADER + 15, GFP_ATOMIC); | ||
883 | if (skb == NULL) | ||
884 | goto out; | ||
885 | |||
886 | /* Reserve space for headers. */ | ||
887 | skb_reserve(skb, MAX_DCCP_HEADER); | ||
888 | skb->dst = dst_clone(dst); | ||
889 | |||
890 | skb->h.raw = skb_push(skb, dccp_hdr_reset_len); | ||
891 | dh = dccp_hdr(skb); | ||
892 | memset(dh, 0, dccp_hdr_reset_len); | ||
893 | |||
894 | /* Build DCCP header and checksum it. */ | ||
895 | dh->dccph_type = DCCP_PKT_RESET; | ||
896 | dh->dccph_sport = rxdh->dccph_dport; | ||
897 | dh->dccph_dport = rxdh->dccph_sport; | ||
898 | dh->dccph_doff = dccp_hdr_reset_len / 4; | ||
899 | dh->dccph_x = 1; | ||
900 | dccp_hdr_reset(skb)->dccph_reset_code = DCCP_SKB_CB(rxskb)->dccpd_reset_code; | ||
901 | |||
902 | dccp_hdr_set_seq(dh, DCCP_SKB_CB(rxskb)->dccpd_ack_seq); | ||
903 | dccp_hdr_set_ack(dccp_hdr_ack_bits(skb), DCCP_SKB_CB(rxskb)->dccpd_seq); | ||
904 | |||
905 | dh->dccph_checksum = dccp_v4_checksum(skb); | ||
906 | |||
907 | bh_lock_sock(dccp_ctl_socket->sk); | ||
908 | err = ip_build_and_send_pkt(skb, dccp_ctl_socket->sk, | ||
909 | rxskb->nh.iph->daddr, rxskb->nh.iph->saddr, NULL); | ||
910 | bh_unlock_sock(dccp_ctl_socket->sk); | ||
911 | |||
912 | if (err == NET_XMIT_CN || err == 0) { | ||
913 | DCCP_INC_STATS_BH(DCCP_MIB_OUTSEGS); | ||
914 | DCCP_INC_STATS_BH(DCCP_MIB_OUTRSTS); | ||
915 | } | ||
916 | out: | ||
917 | dst_release(dst); | ||
918 | } | ||
919 | |||
920 | int dccp_v4_do_rcv(struct sock *sk, struct sk_buff *skb) | ||
921 | { | ||
922 | struct dccp_hdr *dh = dccp_hdr(skb); | ||
923 | |||
924 | if (sk->sk_state == DCCP_OPEN) { /* Fast path */ | ||
925 | if (dccp_rcv_established(sk, skb, dh, skb->len)) | ||
926 | goto reset; | ||
927 | return 0; | ||
928 | } | ||
929 | |||
930 | /* | ||
931 | * Step 3: Process LISTEN state | ||
932 | * If S.state == LISTEN, | ||
933 | * If P.type == Request or P contains a valid Init Cookie option, | ||
934 | * * Must scan the packet's options to check for an Init | ||
935 | * Cookie. Only the Init Cookie is processed here, | ||
936 | * however; other options are processed in Step 8. This | ||
937 | * scan need only be performed if the endpoint uses Init | ||
938 | * Cookies * | ||
939 | * * Generate a new socket and switch to that socket * | ||
940 | * Set S := new socket for this port pair | ||
941 | * S.state = RESPOND | ||
942 | * Choose S.ISS (initial seqno) or set from Init Cookie | ||
943 | * Set S.ISR, S.GSR, S.SWL, S.SWH from packet or Init Cookie | ||
944 | * Continue with S.state == RESPOND | ||
945 | * * A Response packet will be generated in Step 11 * | ||
946 | * Otherwise, | ||
947 | * Generate Reset(No Connection) unless P.type == Reset | ||
948 | * Drop packet and return | ||
949 | * | ||
950 | * NOTE: the check for the packet types is done in dccp_rcv_state_process | ||
951 | */ | ||
952 | if (sk->sk_state == DCCP_LISTEN) { | ||
953 | struct sock *nsk = dccp_v4_hnd_req(sk, skb); | ||
954 | |||
955 | if (nsk == NULL) | ||
956 | goto discard; | ||
957 | |||
958 | if (nsk != sk) { | ||
959 | if (dccp_child_process(sk, nsk, skb)) | ||
960 | goto reset; | ||
961 | return 0; | ||
962 | } | ||
963 | } | ||
964 | |||
965 | if (dccp_rcv_state_process(sk, skb, dh, skb->len)) | ||
966 | goto reset; | ||
967 | return 0; | ||
968 | |||
969 | reset: | ||
970 | DCCP_SKB_CB(skb)->dccpd_reset_code = DCCP_RESET_CODE_NO_CONNECTION; | ||
971 | dccp_v4_ctl_send_reset(skb); | ||
972 | discard: | ||
973 | kfree_skb(skb); | ||
974 | return 0; | ||
975 | } | ||
976 | |||
977 | static inline int dccp_invalid_packet(struct sk_buff *skb) | ||
978 | { | ||
979 | const struct dccp_hdr *dh; | ||
980 | |||
981 | if (skb->pkt_type != PACKET_HOST) | ||
982 | return 1; | ||
983 | |||
984 | if (!pskb_may_pull(skb, sizeof(struct dccp_hdr))) { | ||
985 | dccp_pr_debug("pskb_may_pull failed\n"); | ||
986 | return 1; | ||
987 | } | ||
988 | |||
989 | dh = dccp_hdr(skb); | ||
990 | |||
991 | /* If the packet type is not understood, drop packet and return */ | ||
992 | if (dh->dccph_type >= DCCP_PKT_INVALID) { | ||
993 | dccp_pr_debug("invalid packet type\n"); | ||
994 | return 1; | ||
995 | } | ||
996 | |||
997 | /* | ||
998 | * If P.Data Offset is too small for packet type, or too large for | ||
999 | * packet, drop packet and return | ||
1000 | */ | ||
1001 | if (dh->dccph_doff < dccp_hdr_len(skb) / sizeof(u32)) { | ||
1002 | dccp_pr_debug("Offset(%u) too small 1\n", dh->dccph_doff); | ||
1003 | return 1; | ||
1004 | } | ||
1005 | |||
1006 | if (!pskb_may_pull(skb, dh->dccph_doff * sizeof(u32))) { | ||
1007 | dccp_pr_debug("P.Data Offset(%u) too small 2\n", dh->dccph_doff); | ||
1008 | return 1; | ||
1009 | } | ||
1010 | |||
1011 | dh = dccp_hdr(skb); | ||
1012 | |||
1013 | /* | ||
1014 | * If P.type is not Data, Ack, or DataAck and P.X == 0 (the packet | ||
1015 | * has short sequence numbers), drop packet and return | ||
1016 | */ | ||
1017 | if (dh->dccph_x == 0 && | ||
1018 | dh->dccph_type != DCCP_PKT_DATA && | ||
1019 | dh->dccph_type != DCCP_PKT_ACK && | ||
1020 | dh->dccph_type != DCCP_PKT_DATAACK) { | ||
1021 | dccp_pr_debug("P.type (%s) not Data, Ack nor DataAck and P.X == 0\n", | ||
1022 | dccp_packet_name(dh->dccph_type)); | ||
1023 | return 1; | ||
1024 | } | ||
1025 | |||
1026 | /* If the header checksum is incorrect, drop packet and return */ | ||
1027 | if (dccp_v4_verify_checksum(skb) < 0) { | ||
1028 | dccp_pr_debug("header checksum is incorrect\n"); | ||
1029 | return 1; | ||
1030 | } | ||
1031 | |||
1032 | return 0; | ||
1033 | } | ||
1034 | |||
1035 | /* this is called when real data arrives */ | ||
1036 | int dccp_v4_rcv(struct sk_buff *skb) | ||
1037 | { | ||
1038 | const struct dccp_hdr *dh; | ||
1039 | struct sock *sk; | ||
1040 | int rc; | ||
1041 | |||
1042 | /* Step 1: Check header basics: */ | ||
1043 | |||
1044 | if (dccp_invalid_packet(skb)) | ||
1045 | goto discard_it; | ||
1046 | |||
1047 | dh = dccp_hdr(skb); | ||
1048 | #if 0 | ||
1049 | /* | ||
1050 | * Use something like this to simulate some DATA/DATAACK loss to test | ||
1051 | * dccp_ackpkts_add, you'll get something like this on a session that | ||
1052 | * sends 10 DATA/DATAACK packets: | ||
1053 | * | ||
1054 | * dccp_ackpkts_print: 281473596467422 |0,0|3,0|0,0|3,0|0,0|3,0|0,0|3,0|0,1| | ||
1055 | * | ||
1056 | * 0, 0 means: DCCP_ACKPKTS_STATE_RECEIVED, RLE == just this packet | ||
1057 | * 0, 1 means: DCCP_ACKPKTS_STATE_RECEIVED, RLE == two adjacent packets with the same state | ||
1058 | * 3, 0 means: DCCP_ACKPKTS_STATE_NOT_RECEIVED, RLE == just this packet | ||
1059 | * | ||
1060 | * So... | ||
1061 | * | ||
1062 | * 281473596467422 was received | ||
1063 | * 281473596467421 was not received | ||
1064 | * 281473596467420 was received | ||
1065 | * 281473596467419 was not received | ||
1066 | * 281473596467418 was received | ||
1067 | * 281473596467417 was not received | ||
1068 | * 281473596467416 was received | ||
1069 | * 281473596467415 was not received | ||
1070 | * 281473596467414 was received | ||
1071 | * 281473596467413 was received (this one was the 3way handshake RESPONSE) | ||
1072 | * | ||
1073 | */ | ||
1074 | if (dh->dccph_type == DCCP_PKT_DATA || dh->dccph_type == DCCP_PKT_DATAACK) { | ||
1075 | static int discard = 0; | ||
1076 | |||
1077 | if (discard) { | ||
1078 | discard = 0; | ||
1079 | goto discard_it; | ||
1080 | } | ||
1081 | discard = 1; | ||
1082 | } | ||
1083 | #endif | ||
1084 | DCCP_SKB_CB(skb)->dccpd_seq = dccp_hdr_seq(skb); | ||
1085 | DCCP_SKB_CB(skb)->dccpd_type = dh->dccph_type; | ||
1086 | |||
1087 | dccp_pr_debug("%8.8s " | ||
1088 | "src=%u.%u.%u.%u@%-5d " | ||
1089 | "dst=%u.%u.%u.%u@%-5d seq=%llu", | ||
1090 | dccp_packet_name(dh->dccph_type), | ||
1091 | NIPQUAD(skb->nh.iph->saddr), ntohs(dh->dccph_sport), | ||
1092 | NIPQUAD(skb->nh.iph->daddr), ntohs(dh->dccph_dport), | ||
1093 | DCCP_SKB_CB(skb)->dccpd_seq); | ||
1094 | |||
1095 | if (dccp_packet_without_ack(skb)) { | ||
1096 | DCCP_SKB_CB(skb)->dccpd_ack_seq = DCCP_PKT_WITHOUT_ACK_SEQ; | ||
1097 | dccp_pr_debug_cat("\n"); | ||
1098 | } else { | ||
1099 | DCCP_SKB_CB(skb)->dccpd_ack_seq = dccp_hdr_ack_seq(skb); | ||
1100 | dccp_pr_debug_cat(", ack=%llu\n", DCCP_SKB_CB(skb)->dccpd_ack_seq); | ||
1101 | } | ||
1102 | |||
1103 | /* Step 2: | ||
1104 | * Look up flow ID in table and get corresponding socket */ | ||
1105 | sk = __inet_lookup(&dccp_hashinfo, | ||
1106 | skb->nh.iph->saddr, dh->dccph_sport, | ||
1107 | skb->nh.iph->daddr, ntohs(dh->dccph_dport), | ||
1108 | inet_iif(skb)); | ||
1109 | |||
1110 | /* | ||
1111 | * Step 2: | ||
1112 | * If no socket ... | ||
1113 | * Generate Reset(No Connection) unless P.type == Reset | ||
1114 | * Drop packet and return | ||
1115 | */ | ||
1116 | if (sk == NULL) { | ||
1117 | dccp_pr_debug("failed to look up flow ID in table and " | ||
1118 | "get corresponding socket\n"); | ||
1119 | goto no_dccp_socket; | ||
1120 | } | ||
1121 | |||
1122 | /* | ||
1123 | * Step 2: | ||
1124 | * ... or S.state == TIMEWAIT, | ||
1125 | * Generate Reset(No Connection) unless P.type == Reset | ||
1126 | * Drop packet and return | ||
1127 | */ | ||
1128 | |||
1129 | if (sk->sk_state == DCCP_TIME_WAIT) { | ||
1130 | dccp_pr_debug("sk->sk_state == DCCP_TIME_WAIT: discard_and_relse\n"); | ||
1131 | goto discard_and_relse; | ||
1132 | } | ||
1133 | |||
1134 | if (!xfrm4_policy_check(sk, XFRM_POLICY_IN, skb)) { | ||
1135 | dccp_pr_debug("xfrm4_policy_check failed\n"); | ||
1136 | goto discard_and_relse; | ||
1137 | } | ||
1138 | |||
1139 | if (sk_filter(sk, skb, 0)) { | ||
1140 | dccp_pr_debug("sk_filter failed\n"); | ||
1141 | goto discard_and_relse; | ||
1142 | } | ||
1143 | |||
1144 | skb->dev = NULL; | ||
1145 | |||
1146 | bh_lock_sock(sk); | ||
1147 | rc = 0; | ||
1148 | if (!sock_owned_by_user(sk)) | ||
1149 | rc = dccp_v4_do_rcv(sk, skb); | ||
1150 | else | ||
1151 | sk_add_backlog(sk, skb); | ||
1152 | bh_unlock_sock(sk); | ||
1153 | |||
1154 | sock_put(sk); | ||
1155 | return rc; | ||
1156 | |||
1157 | no_dccp_socket: | ||
1158 | if (!xfrm4_policy_check(NULL, XFRM_POLICY_IN, skb)) | ||
1159 | goto discard_it; | ||
1160 | /* | ||
1161 | * Step 2: | ||
1162 | * Generate Reset(No Connection) unless P.type == Reset | ||
1163 | * Drop packet and return | ||
1164 | */ | ||
1165 | if (dh->dccph_type != DCCP_PKT_RESET) { | ||
1166 | DCCP_SKB_CB(skb)->dccpd_reset_code = DCCP_RESET_CODE_NO_CONNECTION; | ||
1167 | dccp_v4_ctl_send_reset(skb); | ||
1168 | } | ||
1169 | |||
1170 | discard_it: | ||
1171 | /* Discard frame. */ | ||
1172 | kfree_skb(skb); | ||
1173 | return 0; | ||
1174 | |||
1175 | discard_and_relse: | ||
1176 | sock_put(sk); | ||
1177 | goto discard_it; | ||
1178 | } | ||
1179 | |||
1180 | static int dccp_v4_init_sock(struct sock *sk) | ||
1181 | { | ||
1182 | struct dccp_sock *dp = dccp_sk(sk); | ||
1183 | static int dccp_ctl_socket_init = 1; | ||
1184 | |||
1185 | dccp_options_init(&dp->dccps_options); | ||
1186 | |||
1187 | if (dp->dccps_options.dccpo_send_ack_vector) { | ||
1188 | dp->dccps_hc_rx_ackpkts = dccp_ackpkts_alloc(DCCP_MAX_ACK_VECTOR_LEN, | ||
1189 | GFP_KERNEL); | ||
1190 | |||
1191 | if (dp->dccps_hc_rx_ackpkts == NULL) | ||
1192 | return -ENOMEM; | ||
1193 | } | ||
1194 | |||
1195 | /* | ||
1196 | * FIXME: We're hardcoding the CCID, and doing this at this point makes | ||
1197 | * the listening (master) sock get CCID control blocks, which is not | ||
1198 | * necessary, but for now, to not mess with the test userspace apps, | ||
1199 | * lets leave it here, later the real solution is to do this in a | ||
1200 | * setsockopt(CCIDs-I-want/accept). -acme | ||
1201 | */ | ||
1202 | if (likely(!dccp_ctl_socket_init)) { | ||
1203 | dp->dccps_hc_rx_ccid = ccid_init(dp->dccps_options.dccpo_ccid, sk); | ||
1204 | dp->dccps_hc_tx_ccid = ccid_init(dp->dccps_options.dccpo_ccid, sk); | ||
1205 | if (dp->dccps_hc_rx_ccid == NULL || | ||
1206 | dp->dccps_hc_tx_ccid == NULL) { | ||
1207 | ccid_exit(dp->dccps_hc_rx_ccid, sk); | ||
1208 | ccid_exit(dp->dccps_hc_tx_ccid, sk); | ||
1209 | dccp_ackpkts_free(dp->dccps_hc_rx_ackpkts); | ||
1210 | dp->dccps_hc_rx_ackpkts = NULL; | ||
1211 | dp->dccps_hc_rx_ccid = dp->dccps_hc_tx_ccid = NULL; | ||
1212 | return -ENOMEM; | ||
1213 | } | ||
1214 | } else | ||
1215 | dccp_ctl_socket_init = 0; | ||
1216 | |||
1217 | dccp_init_xmit_timers(sk); | ||
1218 | sk->sk_state = DCCP_CLOSED; | ||
1219 | dp->dccps_mss_cache = 536; | ||
1220 | dp->dccps_role = DCCP_ROLE_UNDEFINED; | ||
1221 | |||
1222 | return 0; | ||
1223 | } | ||
1224 | |||
1225 | int dccp_v4_destroy_sock(struct sock *sk) | ||
1226 | { | ||
1227 | struct dccp_sock *dp = dccp_sk(sk); | ||
1228 | |||
1229 | /* | ||
1230 | * DCCP doesn't use sk_qrite_queue, just sk_send_head | ||
1231 | * for retransmissions | ||
1232 | */ | ||
1233 | if (sk->sk_send_head != NULL) { | ||
1234 | kfree_skb(sk->sk_send_head); | ||
1235 | sk->sk_send_head = NULL; | ||
1236 | } | ||
1237 | |||
1238 | /* Clean up a referenced DCCP bind bucket. */ | ||
1239 | if (inet_csk(sk)->icsk_bind_hash != NULL) | ||
1240 | inet_put_port(&dccp_hashinfo, sk); | ||
1241 | |||
1242 | dccp_ackpkts_free(dp->dccps_hc_rx_ackpkts); | ||
1243 | dp->dccps_hc_rx_ackpkts = NULL; | ||
1244 | ccid_exit(dp->dccps_hc_rx_ccid, sk); | ||
1245 | ccid_exit(dp->dccps_hc_tx_ccid, sk); | ||
1246 | dp->dccps_hc_rx_ccid = dp->dccps_hc_tx_ccid = NULL; | ||
1247 | |||
1248 | return 0; | ||
1249 | } | ||
1250 | |||
1251 | static void dccp_v4_reqsk_destructor(struct request_sock *req) | ||
1252 | { | ||
1253 | kfree(inet_rsk(req)->opt); | ||
1254 | } | ||
1255 | |||
1256 | static struct request_sock_ops dccp_request_sock_ops = { | ||
1257 | .family = PF_INET, | ||
1258 | .obj_size = sizeof(struct dccp_request_sock), | ||
1259 | .rtx_syn_ack = dccp_v4_send_response, | ||
1260 | .send_ack = dccp_v4_reqsk_send_ack, | ||
1261 | .destructor = dccp_v4_reqsk_destructor, | ||
1262 | .send_reset = dccp_v4_ctl_send_reset, | ||
1263 | }; | ||
1264 | |||
1265 | struct proto dccp_v4_prot = { | ||
1266 | .name = "DCCP", | ||
1267 | .owner = THIS_MODULE, | ||
1268 | .close = dccp_close, | ||
1269 | .connect = dccp_v4_connect, | ||
1270 | .disconnect = dccp_disconnect, | ||
1271 | .ioctl = dccp_ioctl, | ||
1272 | .init = dccp_v4_init_sock, | ||
1273 | .setsockopt = dccp_setsockopt, | ||
1274 | .getsockopt = dccp_getsockopt, | ||
1275 | .sendmsg = dccp_sendmsg, | ||
1276 | .recvmsg = dccp_recvmsg, | ||
1277 | .backlog_rcv = dccp_v4_do_rcv, | ||
1278 | .hash = dccp_v4_hash, | ||
1279 | .unhash = dccp_v4_unhash, | ||
1280 | .accept = inet_csk_accept, | ||
1281 | .get_port = dccp_v4_get_port, | ||
1282 | .shutdown = dccp_shutdown, | ||
1283 | .destroy = dccp_v4_destroy_sock, | ||
1284 | .orphan_count = &dccp_orphan_count, | ||
1285 | .max_header = MAX_DCCP_HEADER, | ||
1286 | .obj_size = sizeof(struct dccp_sock), | ||
1287 | .rsk_prot = &dccp_request_sock_ops, | ||
1288 | .twsk_obj_size = sizeof(struct inet_timewait_sock), /* FIXME! create dccp_timewait_sock */ | ||
1289 | }; | ||