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