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