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