8dd9f5aa27a2a3512cc3d1a76726a18ae5d004db
[powerpc.git] / net / dccp / ipv4.c
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/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>
20 #include <net/inet_common.h>
21 #include <net/inet_hashtables.h>
22 #include <net/inet_sock.h>
23 #include <net/protocol.h>
24 #include <net/sock.h>
25 #include <net/timewait_sock.h>
26 #include <net/tcp_states.h>
27 #include <net/xfrm.h>
28
29 #include "ackvec.h"
30 #include "ccid.h"
31 #include "dccp.h"
32 #include "feat.h"
33
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  */
39 static struct socket *dccp_v4_ctl_socket;
40
41 static int dccp_v4_get_port(struct sock *sk, const unsigned short snum)
42 {
43         return inet_csk_get_port(&dccp_hashinfo, sk, snum,
44                                  inet_csk_bind_conflict);
45 }
46
47 int dccp_v4_connect(struct sock *sk, struct sockaddr *uaddr, int addr_len)
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;
53         __be32 daddr, nexthop;
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
94         inet_csk(sk)->icsk_ext_hdr_len = 0;
95         if (inet->opt != NULL)
96                 inet_csk(sk)->icsk_ext_hdr_len = inet->opt->optlen;
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);
104         err = inet_hash_connect(&dccp_death_row, sk);
105         if (err != 0)
106                 goto failure;
107
108         err = ip_route_newports(&rt, IPPROTO_DCCP, inet->sport, inet->dport,
109                                 sk);
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;
129 out:
130         return err;
131 failure:
132         /*
133          * This unhashes the socket and releases the local port, if necessary.
134          */
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
142 EXPORT_SYMBOL_GPL(dccp_v4_connect);
143
144 /*
145  * This routine does path mtu discovery as defined in RFC1191.
146  */
147 static 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 &&
182             inet_csk(sk)->icsk_pmtu_cookie > mtu) {
183                 dccp_sync_mss(sk, mtu);
184
185                 /*
186                  * From RFC 4340, sec. 14.1:
187                  *
188                  *      DCCP-Sync packets are the best choice for upward
189                  *      probing, since DCCP-Sync probes do not risk application
190                  *      data loss.
191                  */
192                 dccp_send_sync(sk, dp->dccps_gsr, DCCP_PKT_SYNC);
193         } /* else let the usual retransmit timer handle it */
194 }
195
196 static 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);
210                 struct dccp_hdr *dh = dccp_hdr(skb);
211
212                 dh->dccph_checksum = dccp_v4_checksum(skb, ireq->loc_addr,
213                                                       ireq->rmt_addr);
214                 memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
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
222 out:
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  */
239 static void dccp_v4_err(struct sk_buff *skb, u32 info)
240 {
241         const struct iphdr *iph = (struct iphdr *)skb->data;
242         const struct dccp_hdr *dh = (struct dccp_hdr *)(skb->data +
243                                                         (iph->ihl << 2));
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) {
265                 inet_twsk_put(inet_twsk(sk));
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;
378 out:
379         bh_unlock_sock(sk);
380         sock_put(sk);
381 }
382
383 /* This routine computes an IPv4 DCCP checksum. */
384 void dccp_v4_send_check(struct sock *sk, int len, struct sk_buff *skb)
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
392 EXPORT_SYMBOL_GPL(dccp_v4_send_check);
393
394 static 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
403 static struct request_sock_ops dccp_request_sock_ops;
404
405 int 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;
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;
414         struct dccp_skb_cb *dcb = DCCP_SKB_CB(skb);
415         __u8 reset_code = DCCP_RESET_CODE_TOO_BUSY;
416
417         /* Never answer to DCCP_PKT_REQUESTs send to broadcast or multicast */
418         if (((struct rtable *)skb->dst)->rt_flags &
419             (RTCF_BROADCAST | RTCF_MULTICAST)) {
420                 reset_code = DCCP_RESET_CODE_NO_CONNECTION;
421                 goto drop;
422         }
423
424         if (dccp_bad_service_code(sk, service)) {
425                 reset_code = DCCP_RESET_CODE_BAD_SERVICE_CODE;
426                 goto drop;
427         }
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
445         req = reqsk_alloc(&dccp_request_sock_ops);
446         if (req == NULL)
447                 goto drop;
448
449         if (dccp_parse_options(sk, skb))
450                 goto drop_and_free;
451
452         dccp_openreq_init(req, &dp, skb);
453
454         if (security_inet_conn_request(sk, skb, req))
455                 goto drop_and_free;
456
457         ireq = inet_rsk(req);
458         ireq->loc_addr = daddr;
459         ireq->rmt_addr = saddr;
460         req->rcv_wnd    = dccp_feat_default_sequence_window;
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);
472         dreq->dreq_isr     = dcb->dccpd_seq;
473         dreq->dreq_iss     = dccp_v4_init_sequence(sk, skb);
474         dreq->dreq_service = service;
475
476         if (dccp_v4_send_response(sk, req, NULL))
477                 goto drop_and_free;
478
479         inet_csk_reqsk_queue_hash_add(sk, req, DCCP_TIMEOUT_INIT);
480         return 0;
481
482 drop_and_free:
483         reqsk_free(req);
484 drop:
485         DCCP_INC_STATS_BH(DCCP_MIB_ATTEMPTFAILS);
486         dcb->dccpd_reset_code = reset_code;
487         return -1;
488 }
489
490 EXPORT_SYMBOL_GPL(dccp_v4_conn_request);
491
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  */
498 struct 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
538 exit_overflow:
539         NET_INC_STATS_BH(LINUX_MIB_LISTENOVERFLOWS);
540 exit:
541         NET_INC_STATS_BH(LINUX_MIB_LISTENDROPS);
542         dst_release(dst);
543         return NULL;
544 }
545
546 EXPORT_SYMBOL_GPL(dccp_v4_request_recv_sock);
547
548 static 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
561         nsk = inet_lookup_established(&dccp_hashinfo,
562                                       iph->saddr, dh->dccph_sport,
563                                       iph->daddr, dh->dccph_dport,
564                                       inet_iif(skb));
565         if (nsk != NULL) {
566                 if (nsk->sk_state != DCCP_TIME_WAIT) {
567                         bh_lock_sock(nsk);
568                         return nsk;
569                 }
570                 inet_twsk_put(inet_twsk(nsk));
571                 return NULL;
572         }
573
574         return sk;
575 }
576
577 int dccp_v4_checksum(const struct sk_buff *skb, const __be32 saddr,
578                      const __be32 daddr)
579 {
580         const struct dccp_hdr* dh = dccp_hdr(skb);
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);
588                 checksum_len = checksum_len < skb->len ? checksum_len :
589                                                          skb->len;
590         }
591
592         tmp = csum_partial((unsigned char *)dh, checksum_len, 0);
593         return csum_tcpudp_magic(saddr, daddr, checksum_len,
594                                  IPPROTO_DCCP, tmp);
595 }
596
597 EXPORT_SYMBOL_GPL(dccp_v4_checksum);
598
599 static int dccp_v4_verify_checksum(struct sk_buff *skb,
600                                    const __be32 saddr, const __be32 daddr)
601 {
602         struct dccp_hdr *dh = dccp_hdr(skb);
603         int checksum_len;
604         u32 tmp;
605
606         if (dh->dccph_cscov == 0)
607                 checksum_len = skb->len;
608         else {
609                 checksum_len = (dh->dccph_cscov + dh->dccph_x) * sizeof(u32);
610                 checksum_len = checksum_len < skb->len ? checksum_len :
611                                                          skb->len;
612         }
613         tmp = csum_partial((unsigned char *)dh, checksum_len, 0);
614         return csum_tcpudp_magic(saddr, daddr, checksum_len,
615                                  IPPROTO_DCCP, tmp) == 0 ? 0 : -1;
616 }
617
618 static 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,
630                                          .dport = dccp_hdr(skb)->dccph_sport }
631                                      }
632                           };
633
634         security_skb_classify_flow(skb, &fl);
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
643 static void dccp_v4_ctl_send_reset(struct sk_buff *rxskb)
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;
652         u64 seqno;
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
661         dst = dccp_v4_route_skb(dccp_v4_ctl_socket->sk, rxskb);
662         if (dst == NULL)
663                 return;
664
665         skb = alloc_skb(dccp_v4_ctl_socket->sk->sk_prot->max_header,
666                         GFP_ATOMIC);
667         if (skb == NULL)
668                 goto out;
669
670         /* Reserve space for headers. */
671         skb_reserve(skb, dccp_v4_ctl_socket->sk->sk_prot->max_header);
672         skb->dst = dst_clone(dst);
673
674         dh = dccp_zeroed_hdr(skb, dccp_hdr_reset_len);
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;
682         dccp_hdr_reset(skb)->dccph_reset_code =
683                                 DCCP_SKB_CB(rxskb)->dccpd_reset_code;
684
685         /* See "8.3.1. Abnormal Termination" in RFC 4340 */
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);
691         dccp_hdr_set_ack(dccp_hdr_ack_bits(skb),
692                          DCCP_SKB_CB(rxskb)->dccpd_seq);
693
694         dh->dccph_checksum = dccp_v4_checksum(skb, rxskb->nh.iph->saddr,
695                                               rxskb->nh.iph->daddr);
696
697         bh_lock_sock(dccp_v4_ctl_socket->sk);
698         err = ip_build_and_send_pkt(skb, dccp_v4_ctl_socket->sk,
699                                     rxskb->nh.iph->daddr,
700                                     rxskb->nh.iph->saddr, NULL);
701         bh_unlock_sock(dccp_v4_ctl_socket->sk);
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         }
707 out:
708          dst_release(dst);
709 }
710
711 int 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,
724          *        If P.type == Request or P contains a valid Init Cookie
725          *              option,
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          *
742          * NOTE: the check for the packet types is done in
743          *       dccp_rcv_state_process
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
762 reset:
763         dccp_v4_ctl_send_reset(skb);
764 discard:
765         kfree_skb(skb);
766         return 0;
767 }
768
769 EXPORT_SYMBOL_GPL(dccp_v4_do_rcv);
770
771 int dccp_invalid_packet(struct sk_buff *skb)
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))) {
779                 LIMIT_NETDEBUG(KERN_WARNING "DCCP: pskb_may_pull failed\n");
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) {
787                 LIMIT_NETDEBUG(KERN_WARNING "DCCP: invalid packet type\n");
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)) {
796                 LIMIT_NETDEBUG(KERN_WARNING "DCCP: P.Data Offset(%u) "
797                                             "too small 1\n",
798                                dh->dccph_doff);
799                 return 1;
800         }
801
802         if (!pskb_may_pull(skb, dh->dccph_doff * sizeof(u32))) {
803                 LIMIT_NETDEBUG(KERN_WARNING "DCCP: P.Data Offset(%u) "
804                                             "too small 2\n",
805                                dh->dccph_doff);
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) {
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));
822                 return 1;
823         }
824
825         return 0;
826 }
827
828 EXPORT_SYMBOL_GPL(dccp_invalid_packet);
829
830 /* this is called when real data arrives */
831 static int dccp_v4_rcv(struct sk_buff *skb)
832 {
833         const struct dccp_hdr *dh;
834         struct sock *sk;
835
836         /* Step 1: Check header basics: */
837
838         if (dccp_invalid_packet(skb))
839                 goto discard_it;
840
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
849         dh = dccp_hdr(skb);
850
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),
860                       (unsigned long long) DCCP_SKB_CB(skb)->dccpd_seq);
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);
867                 dccp_pr_debug_cat(", ack=%llu\n", (unsigned long long)
868                                   DCCP_SKB_CB(skb)->dccpd_ack_seq);
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,
875                            skb->nh.iph->daddr, dh->dccph_dport,
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          */
896         if (sk->sk_state == DCCP_TIME_WAIT) {
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;
900         }
901
902         if (!xfrm4_policy_check(sk, XFRM_POLICY_IN, skb))
903                 goto discard_and_relse;
904         nf_reset(skb);
905
906         return sk_receive_skb(sk, skb);
907
908 no_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) {
917                 DCCP_SKB_CB(skb)->dccpd_reset_code =
918                                         DCCP_RESET_CODE_NO_CONNECTION;
919                 dccp_v4_ctl_send_reset(skb);
920         }
921
922 discard_it:
923         kfree_skb(skb);
924         return 0;
925
926 discard_and_relse:
927         sock_put(sk);
928         goto discard_it;
929 }
930
931 static struct inet_connection_sock_af_ops dccp_ipv4_af_ops = {
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),
942 #ifdef CONFIG_COMPAT
943         .compat_setsockopt = compat_ip_setsockopt,
944         .compat_getsockopt = compat_ip_getsockopt,
945 #endif
946 };
947
948 static int dccp_v4_init_sock(struct sock *sk)
949 {
950         static __u8 dccp_v4_ctl_sock_initialized;
951         int err = dccp_init_sock(sk, dccp_v4_ctl_sock_initialized);
952
953         if (err == 0) {
954                 if (unlikely(!dccp_v4_ctl_sock_initialized))
955                         dccp_v4_ctl_sock_initialized = 1;
956                 inet_csk(sk)->icsk_af_ops = &dccp_ipv4_af_ops;
957         }
958
959         return err;
960 }
961
962 static void dccp_v4_reqsk_destructor(struct request_sock *req)
963 {
964         kfree(inet_rsk(req)->opt);
965 }
966
967 static struct request_sock_ops dccp_request_sock_ops __read_mostly = {
968         .family         = PF_INET,
969         .obj_size       = sizeof(struct dccp_request_sock),
970         .rtx_syn_ack    = dccp_v4_send_response,
971         .send_ack       = dccp_reqsk_send_ack,
972         .destructor     = dccp_v4_reqsk_destructor,
973         .send_reset     = dccp_v4_ctl_send_reset,
974 };
975
976 static struct timewait_sock_ops dccp_timewait_sock_ops = {
977         .twsk_obj_size  = sizeof(struct inet_timewait_sock),
978 };
979
980 static struct proto dccp_v4_prot = {
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,
993         .hash                   = dccp_hash,
994         .unhash                 = dccp_unhash,
995         .accept                 = inet_csk_accept,
996         .get_port               = dccp_v4_get_port,
997         .shutdown               = dccp_shutdown,
998         .destroy                = dccp_destroy_sock,
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,
1003         .twsk_prot              = &dccp_timewait_sock_ops,
1004 #ifdef CONFIG_COMPAT
1005         .compat_setsockopt      = compat_dccp_setsockopt,
1006         .compat_getsockopt      = compat_dccp_getsockopt,
1007 #endif
1008 };
1009
1010 static struct net_protocol dccp_v4_protocol = {
1011         .handler        = dccp_v4_rcv,
1012         .err_handler    = dccp_v4_err,
1013         .no_policy      = 1,
1014 };
1015
1016 static const struct proto_ops inet_dccp_ops = {
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,
1025         /* FIXME: work on tcp_poll to rename it to inet_csk_poll */
1026         .poll              = dccp_poll,
1027         .ioctl             = inet_ioctl,
1028         /* FIXME: work on inet_listen to rename it to sock_common_listen */
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,
1037 #ifdef CONFIG_COMPAT
1038         .compat_setsockopt = compat_sock_common_setsockopt,
1039         .compat_getsockopt = compat_sock_common_getsockopt,
1040 #endif
1041 };
1042
1043 static 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
1053 static 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
1066         err = inet_csk_ctl_sock_create(&dccp_v4_ctl_socket, PF_INET,
1067                                        SOCK_DCCP, IPPROTO_DCCP);
1068         if (err)
1069                 goto out_unregister_protosw;
1070 out:
1071         return err;
1072 out_unregister_protosw:
1073         inet_unregister_protosw(&dccp_v4_protosw);
1074         inet_del_protocol(&dccp_v4_protocol, IPPROTO_DCCP);
1075 out_proto_unregister:
1076         proto_unregister(&dccp_v4_prot);
1077         goto out;
1078 }
1079
1080 static 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
1087 module_init(dccp_v4_init);
1088 module_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  */
1095 MODULE_ALIAS("net-pf-" __stringify(PF_INET) "-proto-33-type-6");
1096 MODULE_ALIAS("net-pf-" __stringify(PF_INET) "-proto-0-type-6");
1097 MODULE_LICENSE("GPL");
1098 MODULE_AUTHOR("Arnaldo Carvalho de Melo <acme@mandriva.com>");
1099 MODULE_DESCRIPTION("DCCP - Datagram Congestion Controlled Protocol");