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