]>
Commit | Line | Data |
---|---|---|
7c657876 ACM |
1 | /* |
2 | * net/dccp/ipv4.c | |
3 | * | |
4 | * An implementation of the DCCP protocol | |
5 | * Arnaldo Carvalho de Melo <[email protected]> | |
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 | ||
03ace394 ACM |
312 | /* |
313 | * SWL and AWL are initially adjusted so that they are not less than | |
314 | * the initial Sequence Numbers received and sent, respectively: | |
315 | * SWL := max(GSR + 1 - floor(W/4), ISR), | |
316 | * AWL := max(GSS - W' + 1, ISS). | |
317 | * These adjustments MUST be applied only at the beginning of the | |
318 | * connection. | |
319 | */ | |
320 | dccp_set_seqno(&dp->dccps_awl, max48(dp->dccps_awl, dp->dccps_iss)); | |
321 | ||
7c657876 ACM |
322 | inet->id = dp->dccps_iss ^ jiffies; |
323 | ||
324 | err = dccp_connect(sk); | |
325 | rt = NULL; | |
326 | if (err != 0) | |
327 | goto failure; | |
328 | out: | |
329 | return err; | |
330 | failure: | |
7690af3f ACM |
331 | /* |
332 | * This unhashes the socket and releases the local port, if necessary. | |
333 | */ | |
7c657876 ACM |
334 | dccp_set_state(sk, DCCP_CLOSED); |
335 | ip_rt_put(rt); | |
336 | sk->sk_route_caps = 0; | |
337 | inet->dport = 0; | |
338 | goto out; | |
339 | } | |
340 | ||
341 | /* | |
342 | * This routine does path mtu discovery as defined in RFC1191. | |
343 | */ | |
344 | static inline void dccp_do_pmtu_discovery(struct sock *sk, | |
345 | const struct iphdr *iph, | |
346 | u32 mtu) | |
347 | { | |
348 | struct dst_entry *dst; | |
349 | const struct inet_sock *inet = inet_sk(sk); | |
350 | const struct dccp_sock *dp = dccp_sk(sk); | |
351 | ||
352 | /* We are not interested in DCCP_LISTEN and request_socks (RESPONSEs | |
353 | * send out by Linux are always < 576bytes so they should go through | |
354 | * unfragmented). | |
355 | */ | |
356 | if (sk->sk_state == DCCP_LISTEN) | |
357 | return; | |
358 | ||
359 | /* We don't check in the destentry if pmtu discovery is forbidden | |
360 | * on this route. We just assume that no packet_to_big packets | |
361 | * are send back when pmtu discovery is not active. | |
362 | * There is a small race when the user changes this flag in the | |
363 | * route, but I think that's acceptable. | |
364 | */ | |
365 | if ((dst = __sk_dst_check(sk, 0)) == NULL) | |
366 | return; | |
367 | ||
368 | dst->ops->update_pmtu(dst, mtu); | |
369 | ||
370 | /* Something is about to be wrong... Remember soft error | |
371 | * for the case, if this connection will not able to recover. | |
372 | */ | |
373 | if (mtu < dst_mtu(dst) && ip_dont_fragment(sk, dst)) | |
374 | sk->sk_err_soft = EMSGSIZE; | |
375 | ||
376 | mtu = dst_mtu(dst); | |
377 | ||
378 | if (inet->pmtudisc != IP_PMTUDISC_DONT && | |
379 | dp->dccps_pmtu_cookie > mtu) { | |
380 | dccp_sync_mss(sk, mtu); | |
381 | ||
382 | /* | |
383 | * From: draft-ietf-dccp-spec-11.txt | |
384 | * | |
7690af3f ACM |
385 | * DCCP-Sync packets are the best choice for upward |
386 | * probing, since DCCP-Sync probes do not risk application | |
387 | * data loss. | |
7c657876 | 388 | */ |
e92ae93a | 389 | dccp_send_sync(sk, dp->dccps_gsr, DCCP_PKT_SYNC); |
7c657876 ACM |
390 | } /* else let the usual retransmit timer handle it */ |
391 | } | |
392 | ||
393 | static void dccp_v4_ctl_send_ack(struct sk_buff *rxskb) | |
394 | { | |
395 | int err; | |
396 | struct dccp_hdr *rxdh = dccp_hdr(rxskb), *dh; | |
397 | const int dccp_hdr_ack_len = sizeof(struct dccp_hdr) + | |
398 | sizeof(struct dccp_hdr_ext) + | |
399 | sizeof(struct dccp_hdr_ack_bits); | |
400 | struct sk_buff *skb; | |
401 | ||
402 | if (((struct rtable *)rxskb->dst)->rt_type != RTN_LOCAL) | |
403 | return; | |
404 | ||
405 | skb = alloc_skb(MAX_DCCP_HEADER + 15, GFP_ATOMIC); | |
406 | if (skb == NULL) | |
407 | return; | |
408 | ||
409 | /* Reserve space for headers. */ | |
410 | skb_reserve(skb, MAX_DCCP_HEADER); | |
411 | ||
412 | skb->dst = dst_clone(rxskb->dst); | |
413 | ||
414 | skb->h.raw = skb_push(skb, dccp_hdr_ack_len); | |
415 | dh = dccp_hdr(skb); | |
416 | memset(dh, 0, dccp_hdr_ack_len); | |
417 | ||
418 | /* Build DCCP header and checksum it. */ | |
419 | dh->dccph_type = DCCP_PKT_ACK; | |
420 | dh->dccph_sport = rxdh->dccph_dport; | |
421 | dh->dccph_dport = rxdh->dccph_sport; | |
422 | dh->dccph_doff = dccp_hdr_ack_len / 4; | |
423 | dh->dccph_x = 1; | |
424 | ||
425 | dccp_hdr_set_seq(dh, DCCP_SKB_CB(rxskb)->dccpd_ack_seq); | |
7690af3f ACM |
426 | dccp_hdr_set_ack(dccp_hdr_ack_bits(skb), |
427 | DCCP_SKB_CB(rxskb)->dccpd_seq); | |
7c657876 ACM |
428 | |
429 | bh_lock_sock(dccp_ctl_socket->sk); | |
430 | err = ip_build_and_send_pkt(skb, dccp_ctl_socket->sk, | |
7690af3f ACM |
431 | rxskb->nh.iph->daddr, |
432 | rxskb->nh.iph->saddr, NULL); | |
7c657876 ACM |
433 | bh_unlock_sock(dccp_ctl_socket->sk); |
434 | ||
435 | if (err == NET_XMIT_CN || err == 0) { | |
436 | DCCP_INC_STATS_BH(DCCP_MIB_OUTSEGS); | |
437 | DCCP_INC_STATS_BH(DCCP_MIB_OUTRSTS); | |
438 | } | |
439 | } | |
440 | ||
7690af3f ACM |
441 | static void dccp_v4_reqsk_send_ack(struct sk_buff *skb, |
442 | struct request_sock *req) | |
7c657876 ACM |
443 | { |
444 | dccp_v4_ctl_send_ack(skb); | |
445 | } | |
446 | ||
447 | static int dccp_v4_send_response(struct sock *sk, struct request_sock *req, | |
448 | struct dst_entry *dst) | |
449 | { | |
450 | int err = -1; | |
451 | struct sk_buff *skb; | |
452 | ||
453 | /* First, grab a route. */ | |
454 | ||
455 | if (dst == NULL && (dst = inet_csk_route_req(sk, req)) == NULL) | |
456 | goto out; | |
457 | ||
458 | skb = dccp_make_response(sk, dst, req); | |
459 | if (skb != NULL) { | |
460 | const struct inet_request_sock *ireq = inet_rsk(req); | |
461 | ||
462 | err = ip_build_and_send_pkt(skb, sk, ireq->loc_addr, | |
463 | ireq->rmt_addr, | |
464 | ireq->opt); | |
465 | if (err == NET_XMIT_CN) | |
466 | err = 0; | |
467 | } | |
468 | ||
469 | out: | |
470 | dst_release(dst); | |
471 | return err; | |
472 | } | |
473 | ||
474 | /* | |
475 | * This routine is called by the ICMP module when it gets some sort of error | |
476 | * condition. If err < 0 then the socket should be closed and the error | |
477 | * returned to the user. If err > 0 it's just the icmp type << 8 | icmp code. | |
478 | * After adjustment header points to the first 8 bytes of the tcp header. We | |
479 | * need to find the appropriate port. | |
480 | * | |
481 | * The locking strategy used here is very "optimistic". When someone else | |
482 | * accesses the socket the ICMP is just dropped and for some paths there is no | |
483 | * check at all. A more general error queue to queue errors for later handling | |
484 | * is probably better. | |
485 | */ | |
486 | void dccp_v4_err(struct sk_buff *skb, u32 info) | |
487 | { | |
488 | const struct iphdr *iph = (struct iphdr *)skb->data; | |
7690af3f ACM |
489 | const struct dccp_hdr *dh = (struct dccp_hdr *)(skb->data + |
490 | (iph->ihl << 2)); | |
7c657876 ACM |
491 | struct dccp_sock *dp; |
492 | struct inet_sock *inet; | |
493 | const int type = skb->h.icmph->type; | |
494 | const int code = skb->h.icmph->code; | |
495 | struct sock *sk; | |
496 | __u64 seq; | |
497 | int err; | |
498 | ||
499 | if (skb->len < (iph->ihl << 2) + 8) { | |
500 | ICMP_INC_STATS_BH(ICMP_MIB_INERRORS); | |
501 | return; | |
502 | } | |
503 | ||
504 | sk = inet_lookup(&dccp_hashinfo, iph->daddr, dh->dccph_dport, | |
505 | iph->saddr, dh->dccph_sport, inet_iif(skb)); | |
506 | if (sk == NULL) { | |
507 | ICMP_INC_STATS_BH(ICMP_MIB_INERRORS); | |
508 | return; | |
509 | } | |
510 | ||
511 | if (sk->sk_state == DCCP_TIME_WAIT) { | |
512 | inet_twsk_put((struct inet_timewait_sock *)sk); | |
513 | return; | |
514 | } | |
515 | ||
516 | bh_lock_sock(sk); | |
517 | /* If too many ICMPs get dropped on busy | |
518 | * servers this needs to be solved differently. | |
519 | */ | |
520 | if (sock_owned_by_user(sk)) | |
521 | NET_INC_STATS_BH(LINUX_MIB_LOCKDROPPEDICMPS); | |
522 | ||
523 | if (sk->sk_state == DCCP_CLOSED) | |
524 | goto out; | |
525 | ||
526 | dp = dccp_sk(sk); | |
527 | seq = dccp_hdr_seq(skb); | |
528 | if (sk->sk_state != DCCP_LISTEN && | |
529 | !between48(seq, dp->dccps_swl, dp->dccps_swh)) { | |
530 | NET_INC_STATS(LINUX_MIB_OUTOFWINDOWICMPS); | |
531 | goto out; | |
532 | } | |
533 | ||
534 | switch (type) { | |
535 | case ICMP_SOURCE_QUENCH: | |
536 | /* Just silently ignore these. */ | |
537 | goto out; | |
538 | case ICMP_PARAMETERPROB: | |
539 | err = EPROTO; | |
540 | break; | |
541 | case ICMP_DEST_UNREACH: | |
542 | if (code > NR_ICMP_UNREACH) | |
543 | goto out; | |
544 | ||
545 | if (code == ICMP_FRAG_NEEDED) { /* PMTU discovery (RFC1191) */ | |
546 | if (!sock_owned_by_user(sk)) | |
547 | dccp_do_pmtu_discovery(sk, iph, info); | |
548 | goto out; | |
549 | } | |
550 | ||
551 | err = icmp_err_convert[code].errno; | |
552 | break; | |
553 | case ICMP_TIME_EXCEEDED: | |
554 | err = EHOSTUNREACH; | |
555 | break; | |
556 | default: | |
557 | goto out; | |
558 | } | |
559 | ||
560 | switch (sk->sk_state) { | |
561 | struct request_sock *req , **prev; | |
562 | case DCCP_LISTEN: | |
563 | if (sock_owned_by_user(sk)) | |
564 | goto out; | |
565 | req = inet_csk_search_req(sk, &prev, dh->dccph_dport, | |
566 | iph->daddr, iph->saddr); | |
567 | if (!req) | |
568 | goto out; | |
569 | ||
570 | /* | |
571 | * ICMPs are not backlogged, hence we cannot get an established | |
572 | * socket here. | |
573 | */ | |
574 | BUG_TRAP(!req->sk); | |
575 | ||
576 | if (seq != dccp_rsk(req)->dreq_iss) { | |
577 | NET_INC_STATS_BH(LINUX_MIB_OUTOFWINDOWICMPS); | |
578 | goto out; | |
579 | } | |
580 | /* | |
581 | * Still in RESPOND, just remove it silently. | |
582 | * There is no good way to pass the error to the newly | |
583 | * created socket, and POSIX does not want network | |
584 | * errors returned from accept(). | |
585 | */ | |
586 | inet_csk_reqsk_queue_drop(sk, req, prev); | |
587 | goto out; | |
588 | ||
589 | case DCCP_REQUESTING: | |
590 | case DCCP_RESPOND: | |
591 | if (!sock_owned_by_user(sk)) { | |
592 | DCCP_INC_STATS_BH(DCCP_MIB_ATTEMPTFAILS); | |
593 | sk->sk_err = err; | |
594 | ||
595 | sk->sk_error_report(sk); | |
596 | ||
597 | dccp_done(sk); | |
598 | } else | |
599 | sk->sk_err_soft = err; | |
600 | goto out; | |
601 | } | |
602 | ||
603 | /* If we've already connected we will keep trying | |
604 | * until we time out, or the user gives up. | |
605 | * | |
606 | * rfc1122 4.2.3.9 allows to consider as hard errors | |
607 | * only PROTO_UNREACH and PORT_UNREACH (well, FRAG_FAILED too, | |
608 | * but it is obsoleted by pmtu discovery). | |
609 | * | |
610 | * Note, that in modern internet, where routing is unreliable | |
611 | * and in each dark corner broken firewalls sit, sending random | |
612 | * errors ordered by their masters even this two messages finally lose | |
613 | * their original sense (even Linux sends invalid PORT_UNREACHs) | |
614 | * | |
615 | * Now we are in compliance with RFCs. | |
616 | * --ANK (980905) | |
617 | */ | |
618 | ||
619 | inet = inet_sk(sk); | |
620 | if (!sock_owned_by_user(sk) && inet->recverr) { | |
621 | sk->sk_err = err; | |
622 | sk->sk_error_report(sk); | |
623 | } else /* Only an error on timeout */ | |
624 | sk->sk_err_soft = err; | |
625 | out: | |
626 | bh_unlock_sock(sk); | |
627 | sock_put(sk); | |
628 | } | |
629 | ||
7c657876 ACM |
630 | int dccp_v4_send_reset(struct sock *sk, enum dccp_reset_codes code) |
631 | { | |
632 | struct sk_buff *skb; | |
633 | /* | |
634 | * FIXME: what if rebuild_header fails? | |
635 | * Should we be doing a rebuild_header here? | |
636 | */ | |
637 | int err = inet_sk_rebuild_header(sk); | |
638 | ||
639 | if (err != 0) | |
640 | return err; | |
641 | ||
642 | skb = dccp_make_reset(sk, sk->sk_dst_cache, code); | |
643 | if (skb != NULL) { | |
644 | const struct dccp_sock *dp = dccp_sk(sk); | |
645 | const struct inet_sock *inet = inet_sk(sk); | |
646 | ||
647 | err = ip_build_and_send_pkt(skb, sk, | |
648 | inet->saddr, inet->daddr, NULL); | |
649 | if (err == NET_XMIT_CN) | |
650 | err = 0; | |
651 | ||
652 | ccid_hc_rx_exit(dp->dccps_hc_rx_ccid, sk); | |
653 | ccid_hc_tx_exit(dp->dccps_hc_tx_ccid, sk); | |
654 | } | |
655 | ||
656 | return err; | |
657 | } | |
658 | ||
659 | static inline u64 dccp_v4_init_sequence(const struct sock *sk, | |
660 | const struct sk_buff *skb) | |
661 | { | |
662 | return secure_dccp_sequence_number(skb->nh.iph->daddr, | |
663 | skb->nh.iph->saddr, | |
664 | dccp_hdr(skb)->dccph_dport, | |
665 | dccp_hdr(skb)->dccph_sport); | |
666 | } | |
667 | ||
668 | int dccp_v4_conn_request(struct sock *sk, struct sk_buff *skb) | |
669 | { | |
670 | struct inet_request_sock *ireq; | |
671 | struct dccp_sock dp; | |
672 | struct request_sock *req; | |
673 | struct dccp_request_sock *dreq; | |
674 | const __u32 saddr = skb->nh.iph->saddr; | |
675 | const __u32 daddr = skb->nh.iph->daddr; | |
676 | struct dst_entry *dst = NULL; | |
677 | ||
678 | /* Never answer to DCCP_PKT_REQUESTs send to broadcast or multicast */ | |
679 | if (((struct rtable *)skb->dst)->rt_flags & | |
680 | (RTCF_BROADCAST | RTCF_MULTICAST)) | |
681 | goto drop; | |
682 | ||
683 | /* | |
684 | * TW buckets are converted to open requests without | |
685 | * limitations, they conserve resources and peer is | |
686 | * evidently real one. | |
687 | */ | |
688 | if (inet_csk_reqsk_queue_is_full(sk)) | |
689 | goto drop; | |
690 | ||
691 | /* | |
692 | * Accept backlog is full. If we have already queued enough | |
693 | * of warm entries in syn queue, drop request. It is better than | |
694 | * clogging syn queue with openreqs with exponentially increasing | |
695 | * timeout. | |
696 | */ | |
697 | if (sk_acceptq_is_full(sk) && inet_csk_reqsk_queue_young(sk) > 1) | |
698 | goto drop; | |
699 | ||
700 | req = reqsk_alloc(sk->sk_prot->rsk_prot); | |
701 | if (req == NULL) | |
702 | goto drop; | |
703 | ||
704 | /* FIXME: process options */ | |
705 | ||
706 | dccp_openreq_init(req, &dp, skb); | |
707 | ||
708 | ireq = inet_rsk(req); | |
709 | ireq->loc_addr = daddr; | |
710 | ireq->rmt_addr = saddr; | |
711 | /* FIXME: Merge Aristeu's option parsing code when ready */ | |
7690af3f ACM |
712 | req->rcv_wnd = 100; /* Fake, option parsing will get the |
713 | right value */ | |
7c657876 ACM |
714 | ireq->opt = NULL; |
715 | ||
716 | /* | |
717 | * Step 3: Process LISTEN state | |
718 | * | |
719 | * Set S.ISR, S.GSR, S.SWL, S.SWH from packet or Init Cookie | |
720 | * | |
721 | * In fact we defer setting S.GSR, S.SWL, S.SWH to | |
722 | * dccp_create_openreq_child. | |
723 | */ | |
724 | dreq = dccp_rsk(req); | |
725 | dreq->dreq_isr = DCCP_SKB_CB(skb)->dccpd_seq; | |
726 | dreq->dreq_iss = dccp_v4_init_sequence(sk, skb); | |
727 | dreq->dreq_service = dccp_hdr_request(skb)->dccph_req_service; | |
728 | ||
729 | if (dccp_v4_send_response(sk, req, dst)) | |
730 | goto drop_and_free; | |
731 | ||
732 | inet_csk_reqsk_queue_hash_add(sk, req, DCCP_TIMEOUT_INIT); | |
733 | return 0; | |
734 | ||
735 | drop_and_free: | |
736 | /* | |
737 | * FIXME: should be reqsk_free after implementing req->rsk_ops | |
738 | */ | |
739 | __reqsk_free(req); | |
740 | drop: | |
741 | DCCP_INC_STATS_BH(DCCP_MIB_ATTEMPTFAILS); | |
742 | return -1; | |
743 | } | |
744 | ||
745 | /* | |
746 | * The three way handshake has completed - we got a valid ACK or DATAACK - | |
747 | * now create the new socket. | |
748 | * | |
749 | * This is the equivalent of TCP's tcp_v4_syn_recv_sock | |
750 | */ | |
751 | struct sock *dccp_v4_request_recv_sock(struct sock *sk, struct sk_buff *skb, | |
752 | struct request_sock *req, | |
753 | struct dst_entry *dst) | |
754 | { | |
755 | struct inet_request_sock *ireq; | |
756 | struct inet_sock *newinet; | |
757 | struct dccp_sock *newdp; | |
758 | struct sock *newsk; | |
759 | ||
760 | if (sk_acceptq_is_full(sk)) | |
761 | goto exit_overflow; | |
762 | ||
763 | if (dst == NULL && (dst = inet_csk_route_req(sk, req)) == NULL) | |
764 | goto exit; | |
765 | ||
766 | newsk = dccp_create_openreq_child(sk, req, skb); | |
767 | if (newsk == NULL) | |
768 | goto exit; | |
769 | ||
770 | sk_setup_caps(newsk, dst); | |
771 | ||
772 | newdp = dccp_sk(newsk); | |
773 | newinet = inet_sk(newsk); | |
774 | ireq = inet_rsk(req); | |
775 | newinet->daddr = ireq->rmt_addr; | |
776 | newinet->rcv_saddr = ireq->loc_addr; | |
777 | newinet->saddr = ireq->loc_addr; | |
778 | newinet->opt = ireq->opt; | |
779 | ireq->opt = NULL; | |
780 | newinet->mc_index = inet_iif(skb); | |
781 | newinet->mc_ttl = skb->nh.iph->ttl; | |
782 | newinet->id = jiffies; | |
783 | ||
784 | dccp_sync_mss(newsk, dst_mtu(dst)); | |
785 | ||
786 | __inet_hash(&dccp_hashinfo, newsk, 0); | |
787 | __inet_inherit_port(&dccp_hashinfo, sk, newsk); | |
788 | ||
789 | return newsk; | |
790 | ||
791 | exit_overflow: | |
792 | NET_INC_STATS_BH(LINUX_MIB_LISTENOVERFLOWS); | |
793 | exit: | |
794 | NET_INC_STATS_BH(LINUX_MIB_LISTENDROPS); | |
795 | dst_release(dst); | |
796 | return NULL; | |
797 | } | |
798 | ||
799 | static struct sock *dccp_v4_hnd_req(struct sock *sk, struct sk_buff *skb) | |
800 | { | |
801 | const struct dccp_hdr *dh = dccp_hdr(skb); | |
802 | const struct iphdr *iph = skb->nh.iph; | |
803 | struct sock *nsk; | |
804 | struct request_sock **prev; | |
805 | /* Find possible connection requests. */ | |
806 | struct request_sock *req = inet_csk_search_req(sk, &prev, | |
807 | dh->dccph_sport, | |
808 | iph->saddr, iph->daddr); | |
809 | if (req != NULL) | |
810 | return dccp_check_req(sk, skb, req, prev); | |
811 | ||
812 | nsk = __inet_lookup_established(&dccp_hashinfo, | |
813 | iph->saddr, dh->dccph_sport, | |
814 | iph->daddr, ntohs(dh->dccph_dport), | |
815 | inet_iif(skb)); | |
816 | if (nsk != NULL) { | |
817 | if (nsk->sk_state != DCCP_TIME_WAIT) { | |
818 | bh_lock_sock(nsk); | |
819 | return nsk; | |
820 | } | |
821 | inet_twsk_put((struct inet_timewait_sock *)nsk); | |
822 | return NULL; | |
823 | } | |
824 | ||
825 | return sk; | |
826 | } | |
827 | ||
7690af3f ACM |
828 | int dccp_v4_checksum(const struct sk_buff *skb, const u32 saddr, |
829 | const u32 daddr) | |
7c657876 | 830 | { |
95b81ef7 | 831 | const struct dccp_hdr* dh = dccp_hdr(skb); |
7c657876 ACM |
832 | int checksum_len; |
833 | u32 tmp; | |
834 | ||
835 | if (dh->dccph_cscov == 0) | |
836 | checksum_len = skb->len; | |
837 | else { | |
838 | checksum_len = (dh->dccph_cscov + dh->dccph_x) * sizeof(u32); | |
7690af3f ACM |
839 | checksum_len = checksum_len < skb->len ? checksum_len : |
840 | skb->len; | |
7c657876 ACM |
841 | } |
842 | ||
843 | tmp = csum_partial((unsigned char *)dh, checksum_len, 0); | |
7690af3f ACM |
844 | return csum_tcpudp_magic(saddr, daddr, checksum_len, |
845 | IPPROTO_DCCP, tmp); | |
7c657876 ACM |
846 | } |
847 | ||
95b81ef7 YN |
848 | static int dccp_v4_verify_checksum(struct sk_buff *skb, |
849 | const u32 saddr, const u32 daddr) | |
7c657876 | 850 | { |
95b81ef7 YN |
851 | struct dccp_hdr *dh = dccp_hdr(skb); |
852 | int checksum_len; | |
853 | u32 tmp; | |
7c657876 | 854 | |
95b81ef7 YN |
855 | if (dh->dccph_cscov == 0) |
856 | checksum_len = skb->len; | |
857 | else { | |
858 | checksum_len = (dh->dccph_cscov + dh->dccph_x) * sizeof(u32); | |
7690af3f ACM |
859 | checksum_len = checksum_len < skb->len ? checksum_len : |
860 | skb->len; | |
95b81ef7 YN |
861 | } |
862 | tmp = csum_partial((unsigned char *)dh, checksum_len, 0); | |
7690af3f ACM |
863 | return csum_tcpudp_magic(saddr, daddr, checksum_len, |
864 | IPPROTO_DCCP, tmp) == 0 ? 0 : -1; | |
7c657876 ACM |
865 | } |
866 | ||
867 | static struct dst_entry* dccp_v4_route_skb(struct sock *sk, | |
868 | struct sk_buff *skb) | |
869 | { | |
870 | struct rtable *rt; | |
871 | struct flowi fl = { .oif = ((struct rtable *)skb->dst)->rt_iif, | |
872 | .nl_u = { .ip4_u = | |
873 | { .daddr = skb->nh.iph->saddr, | |
874 | .saddr = skb->nh.iph->daddr, | |
875 | .tos = RT_CONN_FLAGS(sk) } }, | |
876 | .proto = sk->sk_protocol, | |
877 | .uli_u = { .ports = | |
878 | { .sport = dccp_hdr(skb)->dccph_dport, | |
7690af3f ACM |
879 | .dport = dccp_hdr(skb)->dccph_sport } |
880 | } | |
881 | }; | |
7c657876 ACM |
882 | |
883 | if (ip_route_output_flow(&rt, &fl, sk, 0)) { | |
884 | IP_INC_STATS_BH(IPSTATS_MIB_OUTNOROUTES); | |
885 | return NULL; | |
886 | } | |
887 | ||
888 | return &rt->u.dst; | |
889 | } | |
890 | ||
a1d3a355 | 891 | static void dccp_v4_ctl_send_reset(struct sk_buff *rxskb) |
7c657876 ACM |
892 | { |
893 | int err; | |
894 | struct dccp_hdr *rxdh = dccp_hdr(rxskb), *dh; | |
895 | const int dccp_hdr_reset_len = sizeof(struct dccp_hdr) + | |
896 | sizeof(struct dccp_hdr_ext) + | |
897 | sizeof(struct dccp_hdr_reset); | |
898 | struct sk_buff *skb; | |
899 | struct dst_entry *dst; | |
2807d4ff | 900 | u64 seqno; |
7c657876 ACM |
901 | |
902 | /* Never send a reset in response to a reset. */ | |
903 | if (rxdh->dccph_type == DCCP_PKT_RESET) | |
904 | return; | |
905 | ||
906 | if (((struct rtable *)rxskb->dst)->rt_type != RTN_LOCAL) | |
907 | return; | |
908 | ||
909 | dst = dccp_v4_route_skb(dccp_ctl_socket->sk, rxskb); | |
910 | if (dst == NULL) | |
911 | return; | |
912 | ||
913 | skb = alloc_skb(MAX_DCCP_HEADER + 15, GFP_ATOMIC); | |
914 | if (skb == NULL) | |
915 | goto out; | |
916 | ||
917 | /* Reserve space for headers. */ | |
918 | skb_reserve(skb, MAX_DCCP_HEADER); | |
919 | skb->dst = dst_clone(dst); | |
920 | ||
921 | skb->h.raw = skb_push(skb, dccp_hdr_reset_len); | |
922 | dh = dccp_hdr(skb); | |
923 | memset(dh, 0, dccp_hdr_reset_len); | |
924 | ||
925 | /* Build DCCP header and checksum it. */ | |
926 | dh->dccph_type = DCCP_PKT_RESET; | |
927 | dh->dccph_sport = rxdh->dccph_dport; | |
928 | dh->dccph_dport = rxdh->dccph_sport; | |
929 | dh->dccph_doff = dccp_hdr_reset_len / 4; | |
930 | dh->dccph_x = 1; | |
7690af3f ACM |
931 | dccp_hdr_reset(skb)->dccph_reset_code = |
932 | DCCP_SKB_CB(rxskb)->dccpd_reset_code; | |
7c657876 | 933 | |
2807d4ff ACM |
934 | /* See "8.3.1. Abnormal Termination" in draft-ietf-dccp-spec-11 */ |
935 | seqno = 0; | |
936 | if (DCCP_SKB_CB(rxskb)->dccpd_ack_seq != DCCP_PKT_WITHOUT_ACK_SEQ) | |
937 | dccp_set_seqno(&seqno, DCCP_SKB_CB(rxskb)->dccpd_ack_seq + 1); | |
938 | ||
939 | dccp_hdr_set_seq(dh, seqno); | |
7690af3f ACM |
940 | dccp_hdr_set_ack(dccp_hdr_ack_bits(skb), |
941 | DCCP_SKB_CB(rxskb)->dccpd_seq); | |
7c657876 | 942 | |
95b81ef7 YN |
943 | dh->dccph_checksum = dccp_v4_checksum(skb, rxskb->nh.iph->saddr, |
944 | rxskb->nh.iph->daddr); | |
7c657876 ACM |
945 | |
946 | bh_lock_sock(dccp_ctl_socket->sk); | |
947 | err = ip_build_and_send_pkt(skb, dccp_ctl_socket->sk, | |
7690af3f ACM |
948 | rxskb->nh.iph->daddr, |
949 | rxskb->nh.iph->saddr, NULL); | |
7c657876 ACM |
950 | bh_unlock_sock(dccp_ctl_socket->sk); |
951 | ||
952 | if (err == NET_XMIT_CN || err == 0) { | |
953 | DCCP_INC_STATS_BH(DCCP_MIB_OUTSEGS); | |
954 | DCCP_INC_STATS_BH(DCCP_MIB_OUTRSTS); | |
955 | } | |
956 | out: | |
957 | dst_release(dst); | |
958 | } | |
959 | ||
960 | int dccp_v4_do_rcv(struct sock *sk, struct sk_buff *skb) | |
961 | { | |
962 | struct dccp_hdr *dh = dccp_hdr(skb); | |
963 | ||
964 | if (sk->sk_state == DCCP_OPEN) { /* Fast path */ | |
965 | if (dccp_rcv_established(sk, skb, dh, skb->len)) | |
966 | goto reset; | |
967 | return 0; | |
968 | } | |
969 | ||
970 | /* | |
971 | * Step 3: Process LISTEN state | |
972 | * If S.state == LISTEN, | |
7690af3f ACM |
973 | * If P.type == Request or P contains a valid Init Cookie |
974 | * option, | |
7c657876 ACM |
975 | * * Must scan the packet's options to check for an Init |
976 | * Cookie. Only the Init Cookie is processed here, | |
977 | * however; other options are processed in Step 8. This | |
978 | * scan need only be performed if the endpoint uses Init | |
979 | * Cookies * | |
980 | * * Generate a new socket and switch to that socket * | |
981 | * Set S := new socket for this port pair | |
982 | * S.state = RESPOND | |
983 | * Choose S.ISS (initial seqno) or set from Init Cookie | |
984 | * Set S.ISR, S.GSR, S.SWL, S.SWH from packet or Init Cookie | |
985 | * Continue with S.state == RESPOND | |
986 | * * A Response packet will be generated in Step 11 * | |
987 | * Otherwise, | |
988 | * Generate Reset(No Connection) unless P.type == Reset | |
989 | * Drop packet and return | |
990 | * | |
7690af3f ACM |
991 | * NOTE: the check for the packet types is done in |
992 | * dccp_rcv_state_process | |
7c657876 ACM |
993 | */ |
994 | if (sk->sk_state == DCCP_LISTEN) { | |
995 | struct sock *nsk = dccp_v4_hnd_req(sk, skb); | |
996 | ||
997 | if (nsk == NULL) | |
998 | goto discard; | |
999 | ||
1000 | if (nsk != sk) { | |
1001 | if (dccp_child_process(sk, nsk, skb)) | |
1002 | goto reset; | |
1003 | return 0; | |
1004 | } | |
1005 | } | |
1006 | ||
1007 | if (dccp_rcv_state_process(sk, skb, dh, skb->len)) | |
1008 | goto reset; | |
1009 | return 0; | |
1010 | ||
1011 | reset: | |
1012 | DCCP_SKB_CB(skb)->dccpd_reset_code = DCCP_RESET_CODE_NO_CONNECTION; | |
1013 | dccp_v4_ctl_send_reset(skb); | |
1014 | discard: | |
1015 | kfree_skb(skb); | |
1016 | return 0; | |
1017 | } | |
1018 | ||
1019 | static inline int dccp_invalid_packet(struct sk_buff *skb) | |
1020 | { | |
1021 | const struct dccp_hdr *dh; | |
1022 | ||
1023 | if (skb->pkt_type != PACKET_HOST) | |
1024 | return 1; | |
1025 | ||
1026 | if (!pskb_may_pull(skb, sizeof(struct dccp_hdr))) { | |
c59eab46 | 1027 | LIMIT_NETDEBUG(KERN_WARNING "DCCP: pskb_may_pull failed\n"); |
7c657876 ACM |
1028 | return 1; |
1029 | } | |
1030 | ||
1031 | dh = dccp_hdr(skb); | |
1032 | ||
1033 | /* If the packet type is not understood, drop packet and return */ | |
1034 | if (dh->dccph_type >= DCCP_PKT_INVALID) { | |
c59eab46 | 1035 | LIMIT_NETDEBUG(KERN_WARNING "DCCP: invalid packet type\n"); |
7c657876 ACM |
1036 | return 1; |
1037 | } | |
1038 | ||
1039 | /* | |
1040 | * If P.Data Offset is too small for packet type, or too large for | |
1041 | * packet, drop packet and return | |
1042 | */ | |
1043 | if (dh->dccph_doff < dccp_hdr_len(skb) / sizeof(u32)) { | |
c59eab46 ACM |
1044 | LIMIT_NETDEBUG(KERN_WARNING "DCCP: P.Data Offset(%u) " |
1045 | "too small 1\n", | |
1046 | dh->dccph_doff); | |
7c657876 ACM |
1047 | return 1; |
1048 | } | |
1049 | ||
1050 | if (!pskb_may_pull(skb, dh->dccph_doff * sizeof(u32))) { | |
c59eab46 ACM |
1051 | LIMIT_NETDEBUG(KERN_WARNING "DCCP: P.Data Offset(%u) " |
1052 | "too small 2\n", | |
1053 | dh->dccph_doff); | |
7c657876 ACM |
1054 | return 1; |
1055 | } | |
1056 | ||
1057 | dh = dccp_hdr(skb); | |
1058 | ||
1059 | /* | |
1060 | * If P.type is not Data, Ack, or DataAck and P.X == 0 (the packet | |
1061 | * has short sequence numbers), drop packet and return | |
1062 | */ | |
1063 | if (dh->dccph_x == 0 && | |
1064 | dh->dccph_type != DCCP_PKT_DATA && | |
1065 | dh->dccph_type != DCCP_PKT_ACK && | |
1066 | dh->dccph_type != DCCP_PKT_DATAACK) { | |
c59eab46 ACM |
1067 | LIMIT_NETDEBUG(KERN_WARNING "DCCP: P.type (%s) not Data, Ack " |
1068 | "nor DataAck and P.X == 0\n", | |
1069 | dccp_packet_name(dh->dccph_type)); | |
7c657876 ACM |
1070 | return 1; |
1071 | } | |
1072 | ||
1073 | /* If the header checksum is incorrect, drop packet and return */ | |
95b81ef7 YN |
1074 | if (dccp_v4_verify_checksum(skb, skb->nh.iph->saddr, |
1075 | skb->nh.iph->daddr) < 0) { | |
c59eab46 ACM |
1076 | LIMIT_NETDEBUG(KERN_WARNING "DCCP: header checksum is " |
1077 | "incorrect\n"); | |
7c657876 ACM |
1078 | return 1; |
1079 | } | |
1080 | ||
1081 | return 0; | |
1082 | } | |
1083 | ||
1084 | /* this is called when real data arrives */ | |
1085 | int dccp_v4_rcv(struct sk_buff *skb) | |
1086 | { | |
1087 | const struct dccp_hdr *dh; | |
1088 | struct sock *sk; | |
1089 | int rc; | |
1090 | ||
1091 | /* Step 1: Check header basics: */ | |
1092 | ||
1093 | if (dccp_invalid_packet(skb)) | |
1094 | goto discard_it; | |
1095 | ||
1096 | dh = dccp_hdr(skb); | |
1097 | #if 0 | |
1098 | /* | |
1099 | * Use something like this to simulate some DATA/DATAACK loss to test | |
1100 | * dccp_ackpkts_add, you'll get something like this on a session that | |
1101 | * sends 10 DATA/DATAACK packets: | |
1102 | * | |
7690af3f | 1103 | * ackpkts_print: 281473596467422 |0,0|3,0|0,0|3,0|0,0|3,0|0,0|3,0|0,1| |
7c657876 ACM |
1104 | * |
1105 | * 0, 0 means: DCCP_ACKPKTS_STATE_RECEIVED, RLE == just this packet | |
7690af3f ACM |
1106 | * 0, 1 means: DCCP_ACKPKTS_STATE_RECEIVED, RLE == two adjacent packets |
1107 | * with the same state | |
7c657876 ACM |
1108 | * 3, 0 means: DCCP_ACKPKTS_STATE_NOT_RECEIVED, RLE == just this packet |
1109 | * | |
1110 | * So... | |
1111 | * | |
1112 | * 281473596467422 was received | |
1113 | * 281473596467421 was not received | |
1114 | * 281473596467420 was received | |
1115 | * 281473596467419 was not received | |
1116 | * 281473596467418 was received | |
1117 | * 281473596467417 was not received | |
1118 | * 281473596467416 was received | |
1119 | * 281473596467415 was not received | |
1120 | * 281473596467414 was received | |
7690af3f ACM |
1121 | * 281473596467413 was received (this one was the 3way handshake |
1122 | * RESPONSE) | |
7c657876 ACM |
1123 | * |
1124 | */ | |
7690af3f ACM |
1125 | if (dh->dccph_type == DCCP_PKT_DATA || |
1126 | dh->dccph_type == DCCP_PKT_DATAACK) { | |
7c657876 ACM |
1127 | static int discard = 0; |
1128 | ||
1129 | if (discard) { | |
1130 | discard = 0; | |
1131 | goto discard_it; | |
1132 | } | |
1133 | discard = 1; | |
1134 | } | |
1135 | #endif | |
1136 | DCCP_SKB_CB(skb)->dccpd_seq = dccp_hdr_seq(skb); | |
1137 | DCCP_SKB_CB(skb)->dccpd_type = dh->dccph_type; | |
1138 | ||
1139 | dccp_pr_debug("%8.8s " | |
1140 | "src=%u.%u.%u.%u@%-5d " | |
1141 | "dst=%u.%u.%u.%u@%-5d seq=%llu", | |
1142 | dccp_packet_name(dh->dccph_type), | |
1143 | NIPQUAD(skb->nh.iph->saddr), ntohs(dh->dccph_sport), | |
1144 | NIPQUAD(skb->nh.iph->daddr), ntohs(dh->dccph_dport), | |
f6ccf554 | 1145 | (unsigned long long) DCCP_SKB_CB(skb)->dccpd_seq); |
7c657876 ACM |
1146 | |
1147 | if (dccp_packet_without_ack(skb)) { | |
1148 | DCCP_SKB_CB(skb)->dccpd_ack_seq = DCCP_PKT_WITHOUT_ACK_SEQ; | |
1149 | dccp_pr_debug_cat("\n"); | |
1150 | } else { | |
1151 | DCCP_SKB_CB(skb)->dccpd_ack_seq = dccp_hdr_ack_seq(skb); | |
f6ccf554 DM |
1152 | dccp_pr_debug_cat(", ack=%llu\n", |
1153 | (unsigned long long) | |
1154 | DCCP_SKB_CB(skb)->dccpd_ack_seq); | |
7c657876 ACM |
1155 | } |
1156 | ||
1157 | /* Step 2: | |
1158 | * Look up flow ID in table and get corresponding socket */ | |
1159 | sk = __inet_lookup(&dccp_hashinfo, | |
1160 | skb->nh.iph->saddr, dh->dccph_sport, | |
1161 | skb->nh.iph->daddr, ntohs(dh->dccph_dport), | |
1162 | inet_iif(skb)); | |
1163 | ||
1164 | /* | |
1165 | * Step 2: | |
1166 | * If no socket ... | |
1167 | * Generate Reset(No Connection) unless P.type == Reset | |
1168 | * Drop packet and return | |
1169 | */ | |
1170 | if (sk == NULL) { | |
1171 | dccp_pr_debug("failed to look up flow ID in table and " | |
1172 | "get corresponding socket\n"); | |
1173 | goto no_dccp_socket; | |
1174 | } | |
1175 | ||
1176 | /* | |
1177 | * Step 2: | |
1178 | * ... or S.state == TIMEWAIT, | |
1179 | * Generate Reset(No Connection) unless P.type == Reset | |
1180 | * Drop packet and return | |
1181 | */ | |
1182 | ||
1183 | if (sk->sk_state == DCCP_TIME_WAIT) { | |
64cf1e5d ACM |
1184 | dccp_pr_debug("sk->sk_state == DCCP_TIME_WAIT: " |
1185 | "do_time_wait\n"); | |
1186 | goto do_time_wait; | |
7c657876 ACM |
1187 | } |
1188 | ||
1189 | if (!xfrm4_policy_check(sk, XFRM_POLICY_IN, skb)) { | |
1190 | dccp_pr_debug("xfrm4_policy_check failed\n"); | |
1191 | goto discard_and_relse; | |
1192 | } | |
1193 | ||
1194 | if (sk_filter(sk, skb, 0)) { | |
1195 | dccp_pr_debug("sk_filter failed\n"); | |
1196 | goto discard_and_relse; | |
1197 | } | |
1198 | ||
1199 | skb->dev = NULL; | |
1200 | ||
1201 | bh_lock_sock(sk); | |
1202 | rc = 0; | |
1203 | if (!sock_owned_by_user(sk)) | |
1204 | rc = dccp_v4_do_rcv(sk, skb); | |
1205 | else | |
1206 | sk_add_backlog(sk, skb); | |
1207 | bh_unlock_sock(sk); | |
1208 | ||
1209 | sock_put(sk); | |
1210 | return rc; | |
1211 | ||
1212 | no_dccp_socket: | |
1213 | if (!xfrm4_policy_check(NULL, XFRM_POLICY_IN, skb)) | |
1214 | goto discard_it; | |
1215 | /* | |
1216 | * Step 2: | |
1217 | * Generate Reset(No Connection) unless P.type == Reset | |
1218 | * Drop packet and return | |
1219 | */ | |
1220 | if (dh->dccph_type != DCCP_PKT_RESET) { | |
7690af3f ACM |
1221 | DCCP_SKB_CB(skb)->dccpd_reset_code = |
1222 | DCCP_RESET_CODE_NO_CONNECTION; | |
7c657876 ACM |
1223 | dccp_v4_ctl_send_reset(skb); |
1224 | } | |
1225 | ||
1226 | discard_it: | |
1227 | /* Discard frame. */ | |
1228 | kfree_skb(skb); | |
1229 | return 0; | |
1230 | ||
1231 | discard_and_relse: | |
1232 | sock_put(sk); | |
1233 | goto discard_it; | |
64cf1e5d ACM |
1234 | |
1235 | do_time_wait: | |
1236 | inet_twsk_put((struct inet_timewait_sock *)sk); | |
1237 | goto no_dccp_socket; | |
7c657876 ACM |
1238 | } |
1239 | ||
1240 | static int dccp_v4_init_sock(struct sock *sk) | |
1241 | { | |
1242 | struct dccp_sock *dp = dccp_sk(sk); | |
1243 | static int dccp_ctl_socket_init = 1; | |
1244 | ||
1245 | dccp_options_init(&dp->dccps_options); | |
1246 | ||
1247 | if (dp->dccps_options.dccpo_send_ack_vector) { | |
7690af3f ACM |
1248 | dp->dccps_hc_rx_ackpkts = |
1249 | dccp_ackpkts_alloc(DCCP_MAX_ACK_VECTOR_LEN, | |
1250 | GFP_KERNEL); | |
7c657876 ACM |
1251 | |
1252 | if (dp->dccps_hc_rx_ackpkts == NULL) | |
1253 | return -ENOMEM; | |
1254 | } | |
1255 | ||
1256 | /* | |
1257 | * FIXME: We're hardcoding the CCID, and doing this at this point makes | |
1258 | * the listening (master) sock get CCID control blocks, which is not | |
1259 | * necessary, but for now, to not mess with the test userspace apps, | |
1260 | * lets leave it here, later the real solution is to do this in a | |
1261 | * setsockopt(CCIDs-I-want/accept). -acme | |
1262 | */ | |
1263 | if (likely(!dccp_ctl_socket_init)) { | |
7690af3f ACM |
1264 | dp->dccps_hc_rx_ccid = ccid_init(dp->dccps_options.dccpo_ccid, |
1265 | sk); | |
1266 | dp->dccps_hc_tx_ccid = ccid_init(dp->dccps_options.dccpo_ccid, | |
1267 | sk); | |
7c657876 ACM |
1268 | if (dp->dccps_hc_rx_ccid == NULL || |
1269 | dp->dccps_hc_tx_ccid == NULL) { | |
1270 | ccid_exit(dp->dccps_hc_rx_ccid, sk); | |
1271 | ccid_exit(dp->dccps_hc_tx_ccid, sk); | |
1272 | dccp_ackpkts_free(dp->dccps_hc_rx_ackpkts); | |
1273 | dp->dccps_hc_rx_ackpkts = NULL; | |
1274 | dp->dccps_hc_rx_ccid = dp->dccps_hc_tx_ccid = NULL; | |
1275 | return -ENOMEM; | |
1276 | } | |
1277 | } else | |
1278 | dccp_ctl_socket_init = 0; | |
1279 | ||
1280 | dccp_init_xmit_timers(sk); | |
0b4e03bf | 1281 | inet_csk(sk)->icsk_rto = DCCP_TIMEOUT_INIT; |
7c657876 ACM |
1282 | sk->sk_state = DCCP_CLOSED; |
1283 | dp->dccps_mss_cache = 536; | |
1284 | dp->dccps_role = DCCP_ROLE_UNDEFINED; | |
1285 | ||
1286 | return 0; | |
1287 | } | |
1288 | ||
a1d3a355 | 1289 | static int dccp_v4_destroy_sock(struct sock *sk) |
7c657876 ACM |
1290 | { |
1291 | struct dccp_sock *dp = dccp_sk(sk); | |
1292 | ||
1293 | /* | |
1294 | * DCCP doesn't use sk_qrite_queue, just sk_send_head | |
1295 | * for retransmissions | |
1296 | */ | |
1297 | if (sk->sk_send_head != NULL) { | |
1298 | kfree_skb(sk->sk_send_head); | |
1299 | sk->sk_send_head = NULL; | |
1300 | } | |
1301 | ||
1302 | /* Clean up a referenced DCCP bind bucket. */ | |
1303 | if (inet_csk(sk)->icsk_bind_hash != NULL) | |
1304 | inet_put_port(&dccp_hashinfo, sk); | |
1305 | ||
8efa544f ACM |
1306 | ccid_hc_rx_exit(dp->dccps_hc_rx_ccid, sk); |
1307 | ccid_hc_tx_exit(dp->dccps_hc_tx_ccid, sk); | |
7c657876 ACM |
1308 | dccp_ackpkts_free(dp->dccps_hc_rx_ackpkts); |
1309 | dp->dccps_hc_rx_ackpkts = NULL; | |
1310 | ccid_exit(dp->dccps_hc_rx_ccid, sk); | |
1311 | ccid_exit(dp->dccps_hc_tx_ccid, sk); | |
1312 | dp->dccps_hc_rx_ccid = dp->dccps_hc_tx_ccid = NULL; | |
1313 | ||
1314 | return 0; | |
1315 | } | |
1316 | ||
1317 | static void dccp_v4_reqsk_destructor(struct request_sock *req) | |
1318 | { | |
1319 | kfree(inet_rsk(req)->opt); | |
1320 | } | |
1321 | ||
1322 | static struct request_sock_ops dccp_request_sock_ops = { | |
1323 | .family = PF_INET, | |
1324 | .obj_size = sizeof(struct dccp_request_sock), | |
1325 | .rtx_syn_ack = dccp_v4_send_response, | |
1326 | .send_ack = dccp_v4_reqsk_send_ack, | |
1327 | .destructor = dccp_v4_reqsk_destructor, | |
1328 | .send_reset = dccp_v4_ctl_send_reset, | |
1329 | }; | |
1330 | ||
1331 | struct proto dccp_v4_prot = { | |
1332 | .name = "DCCP", | |
1333 | .owner = THIS_MODULE, | |
1334 | .close = dccp_close, | |
1335 | .connect = dccp_v4_connect, | |
1336 | .disconnect = dccp_disconnect, | |
1337 | .ioctl = dccp_ioctl, | |
1338 | .init = dccp_v4_init_sock, | |
1339 | .setsockopt = dccp_setsockopt, | |
1340 | .getsockopt = dccp_getsockopt, | |
1341 | .sendmsg = dccp_sendmsg, | |
1342 | .recvmsg = dccp_recvmsg, | |
1343 | .backlog_rcv = dccp_v4_do_rcv, | |
1344 | .hash = dccp_v4_hash, | |
1345 | .unhash = dccp_v4_unhash, | |
1346 | .accept = inet_csk_accept, | |
1347 | .get_port = dccp_v4_get_port, | |
1348 | .shutdown = dccp_shutdown, | |
1349 | .destroy = dccp_v4_destroy_sock, | |
1350 | .orphan_count = &dccp_orphan_count, | |
1351 | .max_header = MAX_DCCP_HEADER, | |
1352 | .obj_size = sizeof(struct dccp_sock), | |
1353 | .rsk_prot = &dccp_request_sock_ops, | |
64cf1e5d | 1354 | .twsk_obj_size = sizeof(struct inet_timewait_sock), |
7c657876 | 1355 | }; |