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