[TCP]: MD5 Signature Option (RFC2385) support.
[safe/jmp/linux-2.6] / net / dccp / ipv4.c
index d75ce8c..3598533 100644 (file)
@@ -113,13 +113,8 @@ int dccp_v4_connect(struct sock *sk, struct sockaddr *uaddr, int addr_len)
        /* OK, now commit destination to socket.  */
        sk_setup_caps(sk, &rt->u.dst);
 
-       dp->dccps_gar =
-               dp->dccps_iss = secure_dccp_sequence_number(inet->saddr,
-                                                           inet->daddr,
-                                                           inet->sport,
-                                                           usin->sin_port);
-       dccp_update_gss(sk, dp->dccps_iss);
-
+       dp->dccps_iss = secure_dccp_sequence_number(inet->saddr, inet->daddr,
+                                                   inet->sport, inet->dport);
        inet->id = dp->dccps_iss ^ jiffies;
 
        err = dccp_connect(sk);
@@ -349,19 +344,24 @@ out:
        sock_put(sk);
 }
 
-/* This routine computes an IPv4 DCCP checksum. */
-void dccp_v4_send_check(struct sock *sk, int len, struct sk_buff *skb)
+static inline u16 dccp_v4_csum_finish(struct sk_buff *skb,
+                                     __be32 src, __be32 dst)
+{
+       return csum_tcpudp_magic(src, dst, skb->len, IPPROTO_DCCP, skb->csum);
+}
+
+void dccp_v4_send_check(struct sock *sk, int unused, struct sk_buff *skb)
 {
        const struct inet_sock *inet = inet_sk(sk);
        struct dccp_hdr *dh = dccp_hdr(skb);
 
-       dh->dccph_checksum = dccp_v4_checksum(skb, inet->saddr, inet->daddr);
+       dccp_csum_outgoing(skb);
+       dh->dccph_checksum = dccp_v4_csum_finish(skb, inet->saddr, inet->daddr);
 }
 
 EXPORT_SYMBOL_GPL(dccp_v4_send_check);
 
-static inline u64 dccp_v4_init_sequence(const struct sock *sk,
-                                       const struct sk_buff *skb)
+static inline u64 dccp_v4_init_sequence(const struct sk_buff *skb)
 {
        return secure_dccp_sequence_number(skb->nh.iph->daddr,
                                           skb->nh.iph->saddr,
@@ -454,47 +454,6 @@ static struct sock *dccp_v4_hnd_req(struct sock *sk, struct sk_buff *skb)
        return sk;
 }
 
-int dccp_v4_checksum(const struct sk_buff *skb, const __be32 saddr,
-                    const __be32 daddr)
-{
-       const struct dccp_hdr* dh = dccp_hdr(skb);
-       int checksum_len;
-       u32 tmp;
-
-       if (dh->dccph_cscov == 0)
-               checksum_len = skb->len;
-       else {
-               checksum_len = (dh->dccph_cscov + dh->dccph_x) * sizeof(u32);
-               checksum_len = checksum_len < skb->len ? checksum_len :
-                                                        skb->len;
-       }
-
-       tmp = csum_partial((unsigned char *)dh, checksum_len, 0);
-       return csum_tcpudp_magic(saddr, daddr, checksum_len,
-                                IPPROTO_DCCP, tmp);
-}
-
-EXPORT_SYMBOL_GPL(dccp_v4_checksum);
-
-static int dccp_v4_verify_checksum(struct sk_buff *skb,
-                                  const __be32 saddr, const __be32 daddr)
-{
-       struct dccp_hdr *dh = dccp_hdr(skb);
-       int checksum_len;
-       u32 tmp;
-
-       if (dh->dccph_cscov == 0)
-               checksum_len = skb->len;
-       else {
-               checksum_len = (dh->dccph_cscov + dh->dccph_x) * sizeof(u32);
-               checksum_len = checksum_len < skb->len ? checksum_len :
-                                                        skb->len;
-       }
-       tmp = csum_partial((unsigned char *)dh, checksum_len, 0);
-       return csum_tcpudp_magic(saddr, daddr, checksum_len,
-                                IPPROTO_DCCP, tmp) == 0 ? 0 : -1;
-}
-
 static struct dst_entry* dccp_v4_route_skb(struct sock *sk,
                                           struct sk_buff *skb)
 {
@@ -536,14 +495,13 @@ static int dccp_v4_send_response(struct sock *sk, struct request_sock *req,
                const struct inet_request_sock *ireq = inet_rsk(req);
                struct dccp_hdr *dh = dccp_hdr(skb);
 
-               dh->dccph_checksum = dccp_v4_checksum(skb, ireq->loc_addr,
-                                                     ireq->rmt_addr);
+               dh->dccph_checksum = dccp_v4_csum_finish(skb, ireq->loc_addr,
+                                                             ireq->rmt_addr);
                memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
                err = ip_build_and_send_pkt(skb, sk, ireq->loc_addr,
                                            ireq->rmt_addr,
                                            ireq->opt);
-               if (err == NET_XMIT_CN)
-                       err = 0;
+               err = net_xmit_eval(err);
        }
 
 out:
@@ -551,7 +509,7 @@ out:
        return err;
 }
 
-static void dccp_v4_ctl_send_reset(struct sk_buff *rxskb)
+static void dccp_v4_ctl_send_reset(struct sock *sk, struct sk_buff *rxskb)
 {
        int err;
        struct dccp_hdr *rxdh = dccp_hdr(rxskb), *dh;
@@ -560,7 +518,7 @@ static void dccp_v4_ctl_send_reset(struct sk_buff *rxskb)
                                       sizeof(struct dccp_hdr_reset);
        struct sk_buff *skb;
        struct dst_entry *dst;
-       u64 seqno;
+       u64 seqno = 0;
 
        /* Never send a reset in response to a reset. */
        if (rxdh->dccph_type == DCCP_PKT_RESET)
@@ -594,16 +552,15 @@ static void dccp_v4_ctl_send_reset(struct sk_buff *rxskb)
                                DCCP_SKB_CB(rxskb)->dccpd_reset_code;
 
        /* See "8.3.1. Abnormal Termination" in RFC 4340 */
-       seqno = 0;
        if (DCCP_SKB_CB(rxskb)->dccpd_ack_seq != DCCP_PKT_WITHOUT_ACK_SEQ)
                dccp_set_seqno(&seqno, DCCP_SKB_CB(rxskb)->dccpd_ack_seq + 1);
 
        dccp_hdr_set_seq(dh, seqno);
-       dccp_hdr_set_ack(dccp_hdr_ack_bits(skb),
-                        DCCP_SKB_CB(rxskb)->dccpd_seq);
+       dccp_hdr_set_ack(dccp_hdr_ack_bits(skb), DCCP_SKB_CB(rxskb)->dccpd_seq);
 
-       dh->dccph_checksum = dccp_v4_checksum(skb, rxskb->nh.iph->saddr,
-                                             rxskb->nh.iph->daddr);
+       dccp_csum_outgoing(skb);
+       dh->dccph_checksum = dccp_v4_csum_finish(skb, rxskb->nh.iph->saddr,
+                                                     rxskb->nh.iph->daddr);
 
        bh_lock_sock(dccp_v4_ctl_socket->sk);
        err = ip_build_and_send_pkt(skb, dccp_v4_ctl_socket->sk,
@@ -611,7 +568,7 @@ static void dccp_v4_ctl_send_reset(struct sk_buff *rxskb)
                                    rxskb->nh.iph->saddr, NULL);
        bh_unlock_sock(dccp_v4_ctl_socket->sk);
 
-       if (err == NET_XMIT_CN || err == 0) {
+       if (net_xmit_eval(err) == 0) {
                DCCP_INC_STATS_BH(DCCP_MIB_OUTSEGS);
                DCCP_INC_STATS_BH(DCCP_MIB_OUTRSTS);
        }
@@ -685,7 +642,6 @@ int dccp_v4_conn_request(struct sock *sk, struct sk_buff *skb)
        ireq = inet_rsk(req);
        ireq->loc_addr = skb->nh.iph->daddr;
        ireq->rmt_addr = skb->nh.iph->saddr;
-       req->rcv_wnd    = dccp_feat_default_sequence_window;
        ireq->opt       = NULL;
 
        /* 
@@ -698,7 +654,7 @@ int dccp_v4_conn_request(struct sock *sk, struct sk_buff *skb)
         */
        dreq = dccp_rsk(req);
        dreq->dreq_isr     = dcb->dccpd_seq;
-       dreq->dreq_iss     = dccp_v4_init_sequence(sk, skb);
+       dreq->dreq_iss     = dccp_v4_init_sequence(skb);
        dreq->dreq_service = service;
 
        if (dccp_v4_send_response(sk, req, NULL))
@@ -729,24 +685,23 @@ int dccp_v4_do_rcv(struct sock *sk, struct sk_buff *skb)
 
        /*
         *  Step 3: Process LISTEN state
-        *     If S.state == LISTEN,
-        *        If P.type == Request or P contains a valid Init Cookie
-        *              option,
-        *           * Must scan the packet's options to check for an Init
-        *              Cookie.  Only the Init Cookie is processed here,
-        *              however; other options are processed in Step 8.  This
-        *              scan need only be performed if the endpoint uses Init
-        *              Cookies *
-        *           * Generate a new socket and switch to that socket *
-        *           Set S := new socket for this port pair
-        *           S.state = RESPOND
-        *           Choose S.ISS (initial seqno) or set from Init Cookie
-        *           Set S.ISR, S.GSR, S.SWL, S.SWH from packet or Init Cookie
-        *           Continue with S.state == RESPOND
-        *           * A Response packet will be generated in Step 11 *
-        *        Otherwise,
-        *           Generate Reset(No Connection) unless P.type == Reset
-        *           Drop packet and return
+        *       If P.type == Request or P contains a valid Init Cookie option,
+        *            (* Must scan the packet's options to check for Init
+        *               Cookies.  Only Init Cookies are processed here,
+        *               however; other options are processed in Step 8.  This
+        *               scan need only be performed if the endpoint uses Init
+        *               Cookies *)
+        *            (* Generate a new socket and switch to that socket *)
+        *            Set S := new socket for this port pair
+        *            S.state = RESPOND
+        *            Choose S.ISS (initial seqno) or set from Init Cookies
+        *            Initialize S.GAR := S.ISS
+        *            Set S.ISR, S.GSR, S.SWL, S.SWH from packet or Init Cookies
+        *            Continue with S.state == RESPOND
+        *            (* A Response packet will be generated in Step 11 *)
+        *       Otherwise,
+        *            Generate Reset(No Connection) unless P.type == Reset
+        *            Drop packet and return
         *
         * NOTE: the check for the packet types is done in
         *       dccp_rcv_state_process
@@ -769,7 +724,7 @@ int dccp_v4_do_rcv(struct sock *sk, struct sk_buff *skb)
        return 0;
 
 reset:
-       dccp_v4_ctl_send_reset(skb);
+       dccp_v4_ctl_send_reset(sk, skb);
 discard:
        kfree_skb(skb);
        return 0;
@@ -777,13 +732,20 @@ discard:
 
 EXPORT_SYMBOL_GPL(dccp_v4_do_rcv);
 
+/**
+ *     dccp_invalid_packet  -  check for malformed packets
+ *     Implements RFC 4340, 8.5:  Step 1: Check header basics
+ *     Packets that fail these checks are ignored and do not receive Resets.
+ */
 int dccp_invalid_packet(struct sk_buff *skb)
 {
        const struct dccp_hdr *dh;
+       unsigned int cscov;
 
        if (skb->pkt_type != PACKET_HOST)
                return 1;
 
+       /* If the packet is shorter than 12 bytes, drop packet and return */
        if (!pskb_may_pull(skb, sizeof(struct dccp_hdr))) {
                LIMIT_NETDEBUG(KERN_WARNING "DCCP: pskb_may_pull failed\n");
                return 1;
@@ -791,46 +753,57 @@ int dccp_invalid_packet(struct sk_buff *skb)
 
        dh = dccp_hdr(skb);
 
-       /* If the packet type is not understood, drop packet and return */
+       /* If P.type is not understood, drop packet and return */
        if (dh->dccph_type >= DCCP_PKT_INVALID) {
                LIMIT_NETDEBUG(KERN_WARNING "DCCP: invalid packet type\n");
                return 1;
        }
 
        /*
-        * If P.Data Offset is too small for packet type, or too large for
-        * packet, drop packet and return
+        * If P.Data Offset is too small for packet type, drop packet and return
         */
        if (dh->dccph_doff < dccp_hdr_len(skb) / sizeof(u32)) {
                LIMIT_NETDEBUG(KERN_WARNING "DCCP: P.Data Offset(%u) "
-                                           "too small 1\n",
-                              dh->dccph_doff);
+                                           "too small\n", dh->dccph_doff);
                return 1;
        }
-
+       /*
+        * If P.Data Offset is too too large for packet, drop packet and return
+        */
        if (!pskb_may_pull(skb, dh->dccph_doff * sizeof(u32))) {
                LIMIT_NETDEBUG(KERN_WARNING "DCCP: P.Data Offset(%u) "
-                                           "too small 2\n",
-                              dh->dccph_doff);
+                                           "too large\n", dh->dccph_doff);
                return 1;
        }
 
-       dh = dccp_hdr(skb);
-
        /*
         * If P.type is not Data, Ack, or DataAck and P.X == 0 (the packet
         * has short sequence numbers), drop packet and return
         */
-       if (dh->dccph_x == 0 &&
-           dh->dccph_type != DCCP_PKT_DATA &&
-           dh->dccph_type != DCCP_PKT_ACK &&
-           dh->dccph_type != DCCP_PKT_DATAACK) {
-               LIMIT_NETDEBUG(KERN_WARNING "DCCP: P.type (%s) not Data, Ack "
-                                           "nor DataAck and P.X == 0\n",
+       if (dh->dccph_type >= DCCP_PKT_DATA    &&
+           dh->dccph_type <= DCCP_PKT_DATAACK && dh->dccph_x == 0)  {
+               LIMIT_NETDEBUG(KERN_WARNING "DCCP: P.type (%s) not Data||Ack||"
+                                           "DataAck, while P.X == 0\n",
                               dccp_packet_name(dh->dccph_type));
                return 1;
        }
 
+       /*
+        * If P.CsCov is too large for the packet size, drop packet and return.
+        * This must come _before_ checksumming (not as RFC 4340 suggests).
+        */
+       cscov = dccp_csum_coverage(skb);
+       if (cscov > skb->len) {
+               LIMIT_NETDEBUG(KERN_WARNING
+                              "DCCP: P.CsCov %u exceeds packet length %d\n",
+                              dh->dccph_cscov, skb->len);
+               return 1;
+       }
+
+       /* If header checksum is incorrect, drop packet and return.
+        * (This step is completed in the AF-dependent functions.) */
+       skb->csum = skb_checksum(skb, 0, cscov, 0);
+
        return 0;
 }
 
@@ -841,16 +814,17 @@ static int dccp_v4_rcv(struct sk_buff *skb)
 {
        const struct dccp_hdr *dh;
        struct sock *sk;
+       int min_cov;
 
-       /* Step 1: Check header basics: */
+       /* Step 1: Check header basics */
 
        if (dccp_invalid_packet(skb))
                goto discard_it;
 
-       /* If the header checksum is incorrect, drop packet and return */
-       if (dccp_v4_verify_checksum(skb, skb->nh.iph->saddr,
-                                   skb->nh.iph->daddr) < 0) {
-               LIMIT_NETDEBUG(KERN_WARNING "%s: incorrect header checksum\n",
+       /* Step 1: If header checksum is incorrect, drop packet and return */
+       if (dccp_v4_csum_finish(skb, skb->nh.iph->saddr, skb->nh.iph->daddr)) {
+               LIMIT_NETDEBUG(KERN_WARNING
+                              "%s: dropped packet with invalid checksum\n",
                               __FUNCTION__);
                goto discard_it;
        }
@@ -887,8 +861,6 @@ static int dccp_v4_rcv(struct sk_buff *skb)
        /* 
         * Step 2:
         *      If no socket ...
-        *              Generate Reset(No Connection) unless P.type == Reset
-        *              Drop packet and return
         */
        if (sk == NULL) {
                dccp_pr_debug("failed to look up flow ID in table and "
@@ -908,6 +880,21 @@ static int dccp_v4_rcv(struct sk_buff *skb)
                goto no_dccp_socket;
        }
 
+       /*
+        * RFC 4340, sec. 9.2.1: Minimum Checksum Coverage
+        *      o if MinCsCov = 0, only packets with CsCov = 0 are accepted
+        *      o if MinCsCov > 0, also accept packets with CsCov >= MinCsCov
+        */
+       min_cov = dccp_sk(sk)->dccps_pcrlen;
+       if (dh->dccph_cscov && (min_cov == 0 || dh->dccph_cscov < min_cov))  {
+               dccp_pr_debug("Packet CsCov %d does not satisfy MinCsCov %d\n",
+                             dh->dccph_cscov, min_cov);
+               /* FIXME: "Such packets SHOULD be reported using Data Dropped
+                *         options (Section 11.7) with Drop Code 0, Protocol
+                *         Constraints."                                     */
+               goto discard_and_relse;
+       }
+
        if (!xfrm4_policy_check(sk, XFRM_POLICY_IN, skb))
                goto discard_and_relse;
        nf_reset(skb);
@@ -919,13 +906,14 @@ no_dccp_socket:
                goto discard_it;
        /*
         * Step 2:
+        *      If no socket ...
         *              Generate Reset(No Connection) unless P.type == Reset
         *              Drop packet and return
         */
        if (dh->dccph_type != DCCP_PKT_RESET) {
                DCCP_SKB_CB(skb)->dccpd_reset_code =
                                        DCCP_RESET_CODE_NO_CONNECTION;
-               dccp_v4_ctl_send_reset(skb);
+               dccp_v4_ctl_send_reset(sk, skb);
        }
 
 discard_it: