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