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