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