Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  *  net/dccp/ipv4.c
0004  *
0005  *  An implementation of the DCCP protocol
0006  *  Arnaldo Carvalho de Melo <acme@conectiva.com.br>
0007  */
0008 
0009 #include <linux/dccp.h>
0010 #include <linux/icmp.h>
0011 #include <linux/slab.h>
0012 #include <linux/module.h>
0013 #include <linux/skbuff.h>
0014 #include <linux/random.h>
0015 
0016 #include <net/icmp.h>
0017 #include <net/inet_common.h>
0018 #include <net/inet_hashtables.h>
0019 #include <net/inet_sock.h>
0020 #include <net/protocol.h>
0021 #include <net/sock.h>
0022 #include <net/timewait_sock.h>
0023 #include <net/tcp_states.h>
0024 #include <net/xfrm.h>
0025 #include <net/secure_seq.h>
0026 #include <net/netns/generic.h>
0027 
0028 #include "ackvec.h"
0029 #include "ccid.h"
0030 #include "dccp.h"
0031 #include "feat.h"
0032 
0033 struct dccp_v4_pernet {
0034     struct sock *v4_ctl_sk;
0035 };
0036 
0037 static unsigned int dccp_v4_pernet_id __read_mostly;
0038 
0039 /*
0040  * The per-net v4_ctl_sk socket is used for responding to
0041  * the Out-of-the-blue (OOTB) packets. A control sock will be created
0042  * for this socket at the initialization time.
0043  */
0044 
0045 int dccp_v4_connect(struct sock *sk, struct sockaddr *uaddr, int addr_len)
0046 {
0047     const struct sockaddr_in *usin = (struct sockaddr_in *)uaddr;
0048     struct inet_sock *inet = inet_sk(sk);
0049     struct dccp_sock *dp = dccp_sk(sk);
0050     __be16 orig_sport, orig_dport;
0051     __be32 daddr, nexthop;
0052     struct flowi4 *fl4;
0053     struct rtable *rt;
0054     int err;
0055     struct ip_options_rcu *inet_opt;
0056 
0057     dp->dccps_role = DCCP_ROLE_CLIENT;
0058 
0059     if (addr_len < sizeof(struct sockaddr_in))
0060         return -EINVAL;
0061 
0062     if (usin->sin_family != AF_INET)
0063         return -EAFNOSUPPORT;
0064 
0065     nexthop = daddr = usin->sin_addr.s_addr;
0066 
0067     inet_opt = rcu_dereference_protected(inet->inet_opt,
0068                          lockdep_sock_is_held(sk));
0069     if (inet_opt != NULL && inet_opt->opt.srr) {
0070         if (daddr == 0)
0071             return -EINVAL;
0072         nexthop = inet_opt->opt.faddr;
0073     }
0074 
0075     orig_sport = inet->inet_sport;
0076     orig_dport = usin->sin_port;
0077     fl4 = &inet->cork.fl.u.ip4;
0078     rt = ip_route_connect(fl4, nexthop, inet->inet_saddr,
0079                   sk->sk_bound_dev_if, IPPROTO_DCCP, orig_sport,
0080                   orig_dport, sk);
0081     if (IS_ERR(rt))
0082         return PTR_ERR(rt);
0083 
0084     if (rt->rt_flags & (RTCF_MULTICAST | RTCF_BROADCAST)) {
0085         ip_rt_put(rt);
0086         return -ENETUNREACH;
0087     }
0088 
0089     if (inet_opt == NULL || !inet_opt->opt.srr)
0090         daddr = fl4->daddr;
0091 
0092     if (inet->inet_saddr == 0)
0093         inet->inet_saddr = fl4->saddr;
0094     sk_rcv_saddr_set(sk, inet->inet_saddr);
0095     inet->inet_dport = usin->sin_port;
0096     sk_daddr_set(sk, daddr);
0097 
0098     inet_csk(sk)->icsk_ext_hdr_len = 0;
0099     if (inet_opt)
0100         inet_csk(sk)->icsk_ext_hdr_len = inet_opt->opt.optlen;
0101     /*
0102      * Socket identity is still unknown (sport may be zero).
0103      * However we set state to DCCP_REQUESTING and not releasing socket
0104      * lock select source port, enter ourselves into the hash tables and
0105      * complete initialization after this.
0106      */
0107     dccp_set_state(sk, DCCP_REQUESTING);
0108     err = inet_hash_connect(&dccp_death_row, sk);
0109     if (err != 0)
0110         goto failure;
0111 
0112     rt = ip_route_newports(fl4, rt, orig_sport, orig_dport,
0113                    inet->inet_sport, inet->inet_dport, sk);
0114     if (IS_ERR(rt)) {
0115         err = PTR_ERR(rt);
0116         rt = NULL;
0117         goto failure;
0118     }
0119     /* OK, now commit destination to socket.  */
0120     sk_setup_caps(sk, &rt->dst);
0121 
0122     dp->dccps_iss = secure_dccp_sequence_number(inet->inet_saddr,
0123                             inet->inet_daddr,
0124                             inet->inet_sport,
0125                             inet->inet_dport);
0126     inet->inet_id = prandom_u32();
0127 
0128     err = dccp_connect(sk);
0129     rt = NULL;
0130     if (err != 0)
0131         goto failure;
0132 out:
0133     return err;
0134 failure:
0135     /*
0136      * This unhashes the socket and releases the local port, if necessary.
0137      */
0138     dccp_set_state(sk, DCCP_CLOSED);
0139     ip_rt_put(rt);
0140     sk->sk_route_caps = 0;
0141     inet->inet_dport = 0;
0142     goto out;
0143 }
0144 EXPORT_SYMBOL_GPL(dccp_v4_connect);
0145 
0146 /*
0147  * This routine does path mtu discovery as defined in RFC1191.
0148  */
0149 static inline void dccp_do_pmtu_discovery(struct sock *sk,
0150                       const struct iphdr *iph,
0151                       u32 mtu)
0152 {
0153     struct dst_entry *dst;
0154     const struct inet_sock *inet = inet_sk(sk);
0155     const struct dccp_sock *dp = dccp_sk(sk);
0156 
0157     /* We are not interested in DCCP_LISTEN and request_socks (RESPONSEs
0158      * send out by Linux are always < 576bytes so they should go through
0159      * unfragmented).
0160      */
0161     if (sk->sk_state == DCCP_LISTEN)
0162         return;
0163 
0164     dst = inet_csk_update_pmtu(sk, mtu);
0165     if (!dst)
0166         return;
0167 
0168     /* Something is about to be wrong... Remember soft error
0169      * for the case, if this connection will not able to recover.
0170      */
0171     if (mtu < dst_mtu(dst) && ip_dont_fragment(sk, dst))
0172         sk->sk_err_soft = EMSGSIZE;
0173 
0174     mtu = dst_mtu(dst);
0175 
0176     if (inet->pmtudisc != IP_PMTUDISC_DONT &&
0177         ip_sk_accept_pmtu(sk) &&
0178         inet_csk(sk)->icsk_pmtu_cookie > mtu) {
0179         dccp_sync_mss(sk, mtu);
0180 
0181         /*
0182          * From RFC 4340, sec. 14.1:
0183          *
0184          *  DCCP-Sync packets are the best choice for upward
0185          *  probing, since DCCP-Sync probes do not risk application
0186          *  data loss.
0187          */
0188         dccp_send_sync(sk, dp->dccps_gsr, DCCP_PKT_SYNC);
0189     } /* else let the usual retransmit timer handle it */
0190 }
0191 
0192 static void dccp_do_redirect(struct sk_buff *skb, struct sock *sk)
0193 {
0194     struct dst_entry *dst = __sk_dst_check(sk, 0);
0195 
0196     if (dst)
0197         dst->ops->redirect(dst, sk, skb);
0198 }
0199 
0200 void dccp_req_err(struct sock *sk, u64 seq)
0201     {
0202     struct request_sock *req = inet_reqsk(sk);
0203     struct net *net = sock_net(sk);
0204 
0205     /*
0206      * ICMPs are not backlogged, hence we cannot get an established
0207      * socket here.
0208      */
0209     if (!between48(seq, dccp_rsk(req)->dreq_iss, dccp_rsk(req)->dreq_gss)) {
0210         __NET_INC_STATS(net, LINUX_MIB_OUTOFWINDOWICMPS);
0211     } else {
0212         /*
0213          * Still in RESPOND, just remove it silently.
0214          * There is no good way to pass the error to the newly
0215          * created socket, and POSIX does not want network
0216          * errors returned from accept().
0217          */
0218         inet_csk_reqsk_queue_drop(req->rsk_listener, req);
0219     }
0220     reqsk_put(req);
0221 }
0222 EXPORT_SYMBOL(dccp_req_err);
0223 
0224 /*
0225  * This routine is called by the ICMP module when it gets some sort of error
0226  * condition. If err < 0 then the socket should be closed and the error
0227  * returned to the user. If err > 0 it's just the icmp type << 8 | icmp code.
0228  * After adjustment header points to the first 8 bytes of the tcp header. We
0229  * need to find the appropriate port.
0230  *
0231  * The locking strategy used here is very "optimistic". When someone else
0232  * accesses the socket the ICMP is just dropped and for some paths there is no
0233  * check at all. A more general error queue to queue errors for later handling
0234  * is probably better.
0235  */
0236 static int dccp_v4_err(struct sk_buff *skb, u32 info)
0237 {
0238     const struct iphdr *iph = (struct iphdr *)skb->data;
0239     const u8 offset = iph->ihl << 2;
0240     const struct dccp_hdr *dh;
0241     struct dccp_sock *dp;
0242     struct inet_sock *inet;
0243     const int type = icmp_hdr(skb)->type;
0244     const int code = icmp_hdr(skb)->code;
0245     struct sock *sk;
0246     __u64 seq;
0247     int err;
0248     struct net *net = dev_net(skb->dev);
0249 
0250     /* Only need dccph_dport & dccph_sport which are the first
0251      * 4 bytes in dccp header.
0252      * Our caller (icmp_socket_deliver()) already pulled 8 bytes for us.
0253      */
0254     BUILD_BUG_ON(offsetofend(struct dccp_hdr, dccph_sport) > 8);
0255     BUILD_BUG_ON(offsetofend(struct dccp_hdr, dccph_dport) > 8);
0256     dh = (struct dccp_hdr *)(skb->data + offset);
0257 
0258     sk = __inet_lookup_established(net, &dccp_hashinfo,
0259                        iph->daddr, dh->dccph_dport,
0260                        iph->saddr, ntohs(dh->dccph_sport),
0261                        inet_iif(skb), 0);
0262     if (!sk) {
0263         __ICMP_INC_STATS(net, ICMP_MIB_INERRORS);
0264         return -ENOENT;
0265     }
0266 
0267     if (sk->sk_state == DCCP_TIME_WAIT) {
0268         inet_twsk_put(inet_twsk(sk));
0269         return 0;
0270     }
0271     seq = dccp_hdr_seq(dh);
0272     if (sk->sk_state == DCCP_NEW_SYN_RECV) {
0273         dccp_req_err(sk, seq);
0274         return 0;
0275     }
0276 
0277     bh_lock_sock(sk);
0278     /* If too many ICMPs get dropped on busy
0279      * servers this needs to be solved differently.
0280      */
0281     if (sock_owned_by_user(sk))
0282         __NET_INC_STATS(net, LINUX_MIB_LOCKDROPPEDICMPS);
0283 
0284     if (sk->sk_state == DCCP_CLOSED)
0285         goto out;
0286 
0287     dp = dccp_sk(sk);
0288     if ((1 << sk->sk_state) & ~(DCCPF_REQUESTING | DCCPF_LISTEN) &&
0289         !between48(seq, dp->dccps_awl, dp->dccps_awh)) {
0290         __NET_INC_STATS(net, LINUX_MIB_OUTOFWINDOWICMPS);
0291         goto out;
0292     }
0293 
0294     switch (type) {
0295     case ICMP_REDIRECT:
0296         if (!sock_owned_by_user(sk))
0297             dccp_do_redirect(skb, sk);
0298         goto out;
0299     case ICMP_SOURCE_QUENCH:
0300         /* Just silently ignore these. */
0301         goto out;
0302     case ICMP_PARAMETERPROB:
0303         err = EPROTO;
0304         break;
0305     case ICMP_DEST_UNREACH:
0306         if (code > NR_ICMP_UNREACH)
0307             goto out;
0308 
0309         if (code == ICMP_FRAG_NEEDED) { /* PMTU discovery (RFC1191) */
0310             if (!sock_owned_by_user(sk))
0311                 dccp_do_pmtu_discovery(sk, iph, info);
0312             goto out;
0313         }
0314 
0315         err = icmp_err_convert[code].errno;
0316         break;
0317     case ICMP_TIME_EXCEEDED:
0318         err = EHOSTUNREACH;
0319         break;
0320     default:
0321         goto out;
0322     }
0323 
0324     switch (sk->sk_state) {
0325     case DCCP_REQUESTING:
0326     case DCCP_RESPOND:
0327         if (!sock_owned_by_user(sk)) {
0328             __DCCP_INC_STATS(DCCP_MIB_ATTEMPTFAILS);
0329             sk->sk_err = err;
0330 
0331             sk_error_report(sk);
0332 
0333             dccp_done(sk);
0334         } else
0335             sk->sk_err_soft = err;
0336         goto out;
0337     }
0338 
0339     /* If we've already connected we will keep trying
0340      * until we time out, or the user gives up.
0341      *
0342      * rfc1122 4.2.3.9 allows to consider as hard errors
0343      * only PROTO_UNREACH and PORT_UNREACH (well, FRAG_FAILED too,
0344      * but it is obsoleted by pmtu discovery).
0345      *
0346      * Note, that in modern internet, where routing is unreliable
0347      * and in each dark corner broken firewalls sit, sending random
0348      * errors ordered by their masters even this two messages finally lose
0349      * their original sense (even Linux sends invalid PORT_UNREACHs)
0350      *
0351      * Now we are in compliance with RFCs.
0352      *                          --ANK (980905)
0353      */
0354 
0355     inet = inet_sk(sk);
0356     if (!sock_owned_by_user(sk) && inet->recverr) {
0357         sk->sk_err = err;
0358         sk_error_report(sk);
0359     } else /* Only an error on timeout */
0360         sk->sk_err_soft = err;
0361 out:
0362     bh_unlock_sock(sk);
0363     sock_put(sk);
0364     return 0;
0365 }
0366 
0367 static inline __sum16 dccp_v4_csum_finish(struct sk_buff *skb,
0368                       __be32 src, __be32 dst)
0369 {
0370     return csum_tcpudp_magic(src, dst, skb->len, IPPROTO_DCCP, skb->csum);
0371 }
0372 
0373 void dccp_v4_send_check(struct sock *sk, struct sk_buff *skb)
0374 {
0375     const struct inet_sock *inet = inet_sk(sk);
0376     struct dccp_hdr *dh = dccp_hdr(skb);
0377 
0378     dccp_csum_outgoing(skb);
0379     dh->dccph_checksum = dccp_v4_csum_finish(skb,
0380                          inet->inet_saddr,
0381                          inet->inet_daddr);
0382 }
0383 EXPORT_SYMBOL_GPL(dccp_v4_send_check);
0384 
0385 static inline u64 dccp_v4_init_sequence(const struct sk_buff *skb)
0386 {
0387     return secure_dccp_sequence_number(ip_hdr(skb)->daddr,
0388                        ip_hdr(skb)->saddr,
0389                        dccp_hdr(skb)->dccph_dport,
0390                        dccp_hdr(skb)->dccph_sport);
0391 }
0392 
0393 /*
0394  * The three way handshake has completed - we got a valid ACK or DATAACK -
0395  * now create the new socket.
0396  *
0397  * This is the equivalent of TCP's tcp_v4_syn_recv_sock
0398  */
0399 struct sock *dccp_v4_request_recv_sock(const struct sock *sk,
0400                        struct sk_buff *skb,
0401                        struct request_sock *req,
0402                        struct dst_entry *dst,
0403                        struct request_sock *req_unhash,
0404                        bool *own_req)
0405 {
0406     struct inet_request_sock *ireq;
0407     struct inet_sock *newinet;
0408     struct sock *newsk;
0409 
0410     if (sk_acceptq_is_full(sk))
0411         goto exit_overflow;
0412 
0413     newsk = dccp_create_openreq_child(sk, req, skb);
0414     if (newsk == NULL)
0415         goto exit_nonewsk;
0416 
0417     newinet        = inet_sk(newsk);
0418     ireq           = inet_rsk(req);
0419     sk_daddr_set(newsk, ireq->ir_rmt_addr);
0420     sk_rcv_saddr_set(newsk, ireq->ir_loc_addr);
0421     newinet->inet_saddr = ireq->ir_loc_addr;
0422     RCU_INIT_POINTER(newinet->inet_opt, rcu_dereference(ireq->ireq_opt));
0423     newinet->mc_index  = inet_iif(skb);
0424     newinet->mc_ttl    = ip_hdr(skb)->ttl;
0425     newinet->inet_id   = prandom_u32();
0426 
0427     if (dst == NULL && (dst = inet_csk_route_child_sock(sk, newsk, req)) == NULL)
0428         goto put_and_exit;
0429 
0430     sk_setup_caps(newsk, dst);
0431 
0432     dccp_sync_mss(newsk, dst_mtu(dst));
0433 
0434     if (__inet_inherit_port(sk, newsk) < 0)
0435         goto put_and_exit;
0436     *own_req = inet_ehash_nolisten(newsk, req_to_sk(req_unhash), NULL);
0437     if (*own_req)
0438         ireq->ireq_opt = NULL;
0439     else
0440         newinet->inet_opt = NULL;
0441     return newsk;
0442 
0443 exit_overflow:
0444     __NET_INC_STATS(sock_net(sk), LINUX_MIB_LISTENOVERFLOWS);
0445 exit_nonewsk:
0446     dst_release(dst);
0447 exit:
0448     __NET_INC_STATS(sock_net(sk), LINUX_MIB_LISTENDROPS);
0449     return NULL;
0450 put_and_exit:
0451     newinet->inet_opt = NULL;
0452     inet_csk_prepare_forced_close(newsk);
0453     dccp_done(newsk);
0454     goto exit;
0455 }
0456 EXPORT_SYMBOL_GPL(dccp_v4_request_recv_sock);
0457 
0458 static struct dst_entry* dccp_v4_route_skb(struct net *net, struct sock *sk,
0459                        struct sk_buff *skb)
0460 {
0461     struct rtable *rt;
0462     const struct iphdr *iph = ip_hdr(skb);
0463     struct flowi4 fl4 = {
0464         .flowi4_oif = inet_iif(skb),
0465         .daddr = iph->saddr,
0466         .saddr = iph->daddr,
0467         .flowi4_tos = RT_CONN_FLAGS(sk),
0468         .flowi4_proto = sk->sk_protocol,
0469         .fl4_sport = dccp_hdr(skb)->dccph_dport,
0470         .fl4_dport = dccp_hdr(skb)->dccph_sport,
0471     };
0472 
0473     security_skb_classify_flow(skb, flowi4_to_flowi_common(&fl4));
0474     rt = ip_route_output_flow(net, &fl4, sk);
0475     if (IS_ERR(rt)) {
0476         IP_INC_STATS(net, IPSTATS_MIB_OUTNOROUTES);
0477         return NULL;
0478     }
0479 
0480     return &rt->dst;
0481 }
0482 
0483 static int dccp_v4_send_response(const struct sock *sk, struct request_sock *req)
0484 {
0485     int err = -1;
0486     struct sk_buff *skb;
0487     struct dst_entry *dst;
0488     struct flowi4 fl4;
0489 
0490     dst = inet_csk_route_req(sk, &fl4, req);
0491     if (dst == NULL)
0492         goto out;
0493 
0494     skb = dccp_make_response(sk, dst, req);
0495     if (skb != NULL) {
0496         const struct inet_request_sock *ireq = inet_rsk(req);
0497         struct dccp_hdr *dh = dccp_hdr(skb);
0498 
0499         dh->dccph_checksum = dccp_v4_csum_finish(skb, ireq->ir_loc_addr,
0500                                   ireq->ir_rmt_addr);
0501         rcu_read_lock();
0502         err = ip_build_and_send_pkt(skb, sk, ireq->ir_loc_addr,
0503                         ireq->ir_rmt_addr,
0504                         rcu_dereference(ireq->ireq_opt),
0505                         inet_sk(sk)->tos);
0506         rcu_read_unlock();
0507         err = net_xmit_eval(err);
0508     }
0509 
0510 out:
0511     dst_release(dst);
0512     return err;
0513 }
0514 
0515 static void dccp_v4_ctl_send_reset(const struct sock *sk, struct sk_buff *rxskb)
0516 {
0517     int err;
0518     const struct iphdr *rxiph;
0519     struct sk_buff *skb;
0520     struct dst_entry *dst;
0521     struct net *net = dev_net(skb_dst(rxskb)->dev);
0522     struct dccp_v4_pernet *pn;
0523     struct sock *ctl_sk;
0524 
0525     /* Never send a reset in response to a reset. */
0526     if (dccp_hdr(rxskb)->dccph_type == DCCP_PKT_RESET)
0527         return;
0528 
0529     if (skb_rtable(rxskb)->rt_type != RTN_LOCAL)
0530         return;
0531 
0532     pn = net_generic(net, dccp_v4_pernet_id);
0533     ctl_sk = pn->v4_ctl_sk;
0534     dst = dccp_v4_route_skb(net, ctl_sk, rxskb);
0535     if (dst == NULL)
0536         return;
0537 
0538     skb = dccp_ctl_make_reset(ctl_sk, rxskb);
0539     if (skb == NULL)
0540         goto out;
0541 
0542     rxiph = ip_hdr(rxskb);
0543     dccp_hdr(skb)->dccph_checksum = dccp_v4_csum_finish(skb, rxiph->saddr,
0544                                  rxiph->daddr);
0545     skb_dst_set(skb, dst_clone(dst));
0546 
0547     local_bh_disable();
0548     bh_lock_sock(ctl_sk);
0549     err = ip_build_and_send_pkt(skb, ctl_sk,
0550                     rxiph->daddr, rxiph->saddr, NULL,
0551                     inet_sk(ctl_sk)->tos);
0552     bh_unlock_sock(ctl_sk);
0553 
0554     if (net_xmit_eval(err) == 0) {
0555         __DCCP_INC_STATS(DCCP_MIB_OUTSEGS);
0556         __DCCP_INC_STATS(DCCP_MIB_OUTRSTS);
0557     }
0558     local_bh_enable();
0559 out:
0560     dst_release(dst);
0561 }
0562 
0563 static void dccp_v4_reqsk_destructor(struct request_sock *req)
0564 {
0565     dccp_feat_list_purge(&dccp_rsk(req)->dreq_featneg);
0566     kfree(rcu_dereference_protected(inet_rsk(req)->ireq_opt, 1));
0567 }
0568 
0569 void dccp_syn_ack_timeout(const struct request_sock *req)
0570 {
0571 }
0572 EXPORT_SYMBOL(dccp_syn_ack_timeout);
0573 
0574 static struct request_sock_ops dccp_request_sock_ops __read_mostly = {
0575     .family     = PF_INET,
0576     .obj_size   = sizeof(struct dccp_request_sock),
0577     .rtx_syn_ack    = dccp_v4_send_response,
0578     .send_ack   = dccp_reqsk_send_ack,
0579     .destructor = dccp_v4_reqsk_destructor,
0580     .send_reset = dccp_v4_ctl_send_reset,
0581     .syn_ack_timeout = dccp_syn_ack_timeout,
0582 };
0583 
0584 int dccp_v4_conn_request(struct sock *sk, struct sk_buff *skb)
0585 {
0586     struct inet_request_sock *ireq;
0587     struct request_sock *req;
0588     struct dccp_request_sock *dreq;
0589     const __be32 service = dccp_hdr_request(skb)->dccph_req_service;
0590     struct dccp_skb_cb *dcb = DCCP_SKB_CB(skb);
0591 
0592     /* Never answer to DCCP_PKT_REQUESTs send to broadcast or multicast */
0593     if (skb_rtable(skb)->rt_flags & (RTCF_BROADCAST | RTCF_MULTICAST))
0594         return 0;   /* discard, don't send a reset here */
0595 
0596     if (dccp_bad_service_code(sk, service)) {
0597         dcb->dccpd_reset_code = DCCP_RESET_CODE_BAD_SERVICE_CODE;
0598         goto drop;
0599     }
0600     /*
0601      * TW buckets are converted to open requests without
0602      * limitations, they conserve resources and peer is
0603      * evidently real one.
0604      */
0605     dcb->dccpd_reset_code = DCCP_RESET_CODE_TOO_BUSY;
0606     if (inet_csk_reqsk_queue_is_full(sk))
0607         goto drop;
0608 
0609     if (sk_acceptq_is_full(sk))
0610         goto drop;
0611 
0612     req = inet_reqsk_alloc(&dccp_request_sock_ops, sk, true);
0613     if (req == NULL)
0614         goto drop;
0615 
0616     if (dccp_reqsk_init(req, dccp_sk(sk), skb))
0617         goto drop_and_free;
0618 
0619     dreq = dccp_rsk(req);
0620     if (dccp_parse_options(sk, dreq, skb))
0621         goto drop_and_free;
0622 
0623     if (security_inet_conn_request(sk, skb, req))
0624         goto drop_and_free;
0625 
0626     ireq = inet_rsk(req);
0627     sk_rcv_saddr_set(req_to_sk(req), ip_hdr(skb)->daddr);
0628     sk_daddr_set(req_to_sk(req), ip_hdr(skb)->saddr);
0629     ireq->ir_mark = inet_request_mark(sk, skb);
0630     ireq->ireq_family = AF_INET;
0631     ireq->ir_iif = READ_ONCE(sk->sk_bound_dev_if);
0632 
0633     /*
0634      * Step 3: Process LISTEN state
0635      *
0636      * Set S.ISR, S.GSR, S.SWL, S.SWH from packet or Init Cookie
0637      *
0638      * Setting S.SWL/S.SWH to is deferred to dccp_create_openreq_child().
0639      */
0640     dreq->dreq_isr     = dcb->dccpd_seq;
0641     dreq->dreq_gsr     = dreq->dreq_isr;
0642     dreq->dreq_iss     = dccp_v4_init_sequence(skb);
0643     dreq->dreq_gss     = dreq->dreq_iss;
0644     dreq->dreq_service = service;
0645 
0646     if (dccp_v4_send_response(sk, req))
0647         goto drop_and_free;
0648 
0649     inet_csk_reqsk_queue_hash_add(sk, req, DCCP_TIMEOUT_INIT);
0650     reqsk_put(req);
0651     return 0;
0652 
0653 drop_and_free:
0654     reqsk_free(req);
0655 drop:
0656     __DCCP_INC_STATS(DCCP_MIB_ATTEMPTFAILS);
0657     return -1;
0658 }
0659 EXPORT_SYMBOL_GPL(dccp_v4_conn_request);
0660 
0661 int dccp_v4_do_rcv(struct sock *sk, struct sk_buff *skb)
0662 {
0663     struct dccp_hdr *dh = dccp_hdr(skb);
0664 
0665     if (sk->sk_state == DCCP_OPEN) { /* Fast path */
0666         if (dccp_rcv_established(sk, skb, dh, skb->len))
0667             goto reset;
0668         return 0;
0669     }
0670 
0671     /*
0672      *  Step 3: Process LISTEN state
0673      *   If P.type == Request or P contains a valid Init Cookie option,
0674      *        (* Must scan the packet's options to check for Init
0675      *       Cookies.  Only Init Cookies are processed here,
0676      *       however; other options are processed in Step 8.  This
0677      *       scan need only be performed if the endpoint uses Init
0678      *       Cookies *)
0679      *        (* Generate a new socket and switch to that socket *)
0680      *        Set S := new socket for this port pair
0681      *        S.state = RESPOND
0682      *        Choose S.ISS (initial seqno) or set from Init Cookies
0683      *        Initialize S.GAR := S.ISS
0684      *        Set S.ISR, S.GSR, S.SWL, S.SWH from packet or Init Cookies
0685      *        Continue with S.state == RESPOND
0686      *        (* A Response packet will be generated in Step 11 *)
0687      *   Otherwise,
0688      *        Generate Reset(No Connection) unless P.type == Reset
0689      *        Drop packet and return
0690      *
0691      * NOTE: the check for the packet types is done in
0692      *   dccp_rcv_state_process
0693      */
0694 
0695     if (dccp_rcv_state_process(sk, skb, dh, skb->len))
0696         goto reset;
0697     return 0;
0698 
0699 reset:
0700     dccp_v4_ctl_send_reset(sk, skb);
0701     kfree_skb(skb);
0702     return 0;
0703 }
0704 EXPORT_SYMBOL_GPL(dccp_v4_do_rcv);
0705 
0706 /**
0707  *  dccp_invalid_packet  -  check for malformed packets
0708  *  @skb: Packet to validate
0709  *
0710  *  Implements RFC 4340, 8.5:  Step 1: Check header basics
0711  *  Packets that fail these checks are ignored and do not receive Resets.
0712  */
0713 int dccp_invalid_packet(struct sk_buff *skb)
0714 {
0715     const struct dccp_hdr *dh;
0716     unsigned int cscov;
0717     u8 dccph_doff;
0718 
0719     if (skb->pkt_type != PACKET_HOST)
0720         return 1;
0721 
0722     /* If the packet is shorter than 12 bytes, drop packet and return */
0723     if (!pskb_may_pull(skb, sizeof(struct dccp_hdr))) {
0724         DCCP_WARN("pskb_may_pull failed\n");
0725         return 1;
0726     }
0727 
0728     dh = dccp_hdr(skb);
0729 
0730     /* If P.type is not understood, drop packet and return */
0731     if (dh->dccph_type >= DCCP_PKT_INVALID) {
0732         DCCP_WARN("invalid packet type\n");
0733         return 1;
0734     }
0735 
0736     /*
0737      * If P.Data Offset is too small for packet type, drop packet and return
0738      */
0739     dccph_doff = dh->dccph_doff;
0740     if (dccph_doff < dccp_hdr_len(skb) / sizeof(u32)) {
0741         DCCP_WARN("P.Data Offset(%u) too small\n", dccph_doff);
0742         return 1;
0743     }
0744     /*
0745      * If P.Data Offset is too large for packet, drop packet and return
0746      */
0747     if (!pskb_may_pull(skb, dccph_doff * sizeof(u32))) {
0748         DCCP_WARN("P.Data Offset(%u) too large\n", dccph_doff);
0749         return 1;
0750     }
0751     dh = dccp_hdr(skb);
0752     /*
0753      * If P.type is not Data, Ack, or DataAck and P.X == 0 (the packet
0754      * has short sequence numbers), drop packet and return
0755      */
0756     if ((dh->dccph_type < DCCP_PKT_DATA    ||
0757         dh->dccph_type > DCCP_PKT_DATAACK) && dh->dccph_x == 0)  {
0758         DCCP_WARN("P.type (%s) not Data || [Data]Ack, while P.X == 0\n",
0759               dccp_packet_name(dh->dccph_type));
0760         return 1;
0761     }
0762 
0763     /*
0764      * If P.CsCov is too large for the packet size, drop packet and return.
0765      * This must come _before_ checksumming (not as RFC 4340 suggests).
0766      */
0767     cscov = dccp_csum_coverage(skb);
0768     if (cscov > skb->len) {
0769         DCCP_WARN("P.CsCov %u exceeds packet length %d\n",
0770               dh->dccph_cscov, skb->len);
0771         return 1;
0772     }
0773 
0774     /* If header checksum is incorrect, drop packet and return.
0775      * (This step is completed in the AF-dependent functions.) */
0776     skb->csum = skb_checksum(skb, 0, cscov, 0);
0777 
0778     return 0;
0779 }
0780 EXPORT_SYMBOL_GPL(dccp_invalid_packet);
0781 
0782 /* this is called when real data arrives */
0783 static int dccp_v4_rcv(struct sk_buff *skb)
0784 {
0785     const struct dccp_hdr *dh;
0786     const struct iphdr *iph;
0787     bool refcounted;
0788     struct sock *sk;
0789     int min_cov;
0790 
0791     /* Step 1: Check header basics */
0792 
0793     if (dccp_invalid_packet(skb))
0794         goto discard_it;
0795 
0796     iph = ip_hdr(skb);
0797     /* Step 1: If header checksum is incorrect, drop packet and return */
0798     if (dccp_v4_csum_finish(skb, iph->saddr, iph->daddr)) {
0799         DCCP_WARN("dropped packet with invalid checksum\n");
0800         goto discard_it;
0801     }
0802 
0803     dh = dccp_hdr(skb);
0804 
0805     DCCP_SKB_CB(skb)->dccpd_seq  = dccp_hdr_seq(dh);
0806     DCCP_SKB_CB(skb)->dccpd_type = dh->dccph_type;
0807 
0808     dccp_pr_debug("%8.8s src=%pI4@%-5d dst=%pI4@%-5d seq=%llu",
0809               dccp_packet_name(dh->dccph_type),
0810               &iph->saddr, ntohs(dh->dccph_sport),
0811               &iph->daddr, ntohs(dh->dccph_dport),
0812               (unsigned long long) DCCP_SKB_CB(skb)->dccpd_seq);
0813 
0814     if (dccp_packet_without_ack(skb)) {
0815         DCCP_SKB_CB(skb)->dccpd_ack_seq = DCCP_PKT_WITHOUT_ACK_SEQ;
0816         dccp_pr_debug_cat("\n");
0817     } else {
0818         DCCP_SKB_CB(skb)->dccpd_ack_seq = dccp_hdr_ack_seq(skb);
0819         dccp_pr_debug_cat(", ack=%llu\n", (unsigned long long)
0820                   DCCP_SKB_CB(skb)->dccpd_ack_seq);
0821     }
0822 
0823 lookup:
0824     sk = __inet_lookup_skb(&dccp_hashinfo, skb, __dccp_hdr_len(dh),
0825                    dh->dccph_sport, dh->dccph_dport, 0, &refcounted);
0826     if (!sk) {
0827         dccp_pr_debug("failed to look up flow ID in table and "
0828                   "get corresponding socket\n");
0829         goto no_dccp_socket;
0830     }
0831 
0832     /*
0833      * Step 2:
0834      *  ... or S.state == TIMEWAIT,
0835      *      Generate Reset(No Connection) unless P.type == Reset
0836      *      Drop packet and return
0837      */
0838     if (sk->sk_state == DCCP_TIME_WAIT) {
0839         dccp_pr_debug("sk->sk_state == DCCP_TIME_WAIT: do_time_wait\n");
0840         inet_twsk_put(inet_twsk(sk));
0841         goto no_dccp_socket;
0842     }
0843 
0844     if (sk->sk_state == DCCP_NEW_SYN_RECV) {
0845         struct request_sock *req = inet_reqsk(sk);
0846         struct sock *nsk;
0847 
0848         sk = req->rsk_listener;
0849         if (unlikely(sk->sk_state != DCCP_LISTEN)) {
0850             inet_csk_reqsk_queue_drop_and_put(sk, req);
0851             goto lookup;
0852         }
0853         sock_hold(sk);
0854         refcounted = true;
0855         nsk = dccp_check_req(sk, skb, req);
0856         if (!nsk) {
0857             reqsk_put(req);
0858             goto discard_and_relse;
0859         }
0860         if (nsk == sk) {
0861             reqsk_put(req);
0862         } else if (dccp_child_process(sk, nsk, skb)) {
0863             dccp_v4_ctl_send_reset(sk, skb);
0864             goto discard_and_relse;
0865         } else {
0866             sock_put(sk);
0867             return 0;
0868         }
0869     }
0870     /*
0871      * RFC 4340, sec. 9.2.1: Minimum Checksum Coverage
0872      *  o if MinCsCov = 0, only packets with CsCov = 0 are accepted
0873      *  o if MinCsCov > 0, also accept packets with CsCov >= MinCsCov
0874      */
0875     min_cov = dccp_sk(sk)->dccps_pcrlen;
0876     if (dh->dccph_cscov && (min_cov == 0 || dh->dccph_cscov < min_cov))  {
0877         dccp_pr_debug("Packet CsCov %d does not satisfy MinCsCov %d\n",
0878                   dh->dccph_cscov, min_cov);
0879         /* FIXME: "Such packets SHOULD be reported using Data Dropped
0880          *         options (Section 11.7) with Drop Code 0, Protocol
0881          *         Constraints."                                     */
0882         goto discard_and_relse;
0883     }
0884 
0885     if (!xfrm4_policy_check(sk, XFRM_POLICY_IN, skb))
0886         goto discard_and_relse;
0887     nf_reset_ct(skb);
0888 
0889     return __sk_receive_skb(sk, skb, 1, dh->dccph_doff * 4, refcounted);
0890 
0891 no_dccp_socket:
0892     if (!xfrm4_policy_check(NULL, XFRM_POLICY_IN, skb))
0893         goto discard_it;
0894     /*
0895      * Step 2:
0896      *  If no socket ...
0897      *      Generate Reset(No Connection) unless P.type == Reset
0898      *      Drop packet and return
0899      */
0900     if (dh->dccph_type != DCCP_PKT_RESET) {
0901         DCCP_SKB_CB(skb)->dccpd_reset_code =
0902                     DCCP_RESET_CODE_NO_CONNECTION;
0903         dccp_v4_ctl_send_reset(sk, skb);
0904     }
0905 
0906 discard_it:
0907     kfree_skb(skb);
0908     return 0;
0909 
0910 discard_and_relse:
0911     if (refcounted)
0912         sock_put(sk);
0913     goto discard_it;
0914 }
0915 
0916 static const struct inet_connection_sock_af_ops dccp_ipv4_af_ops = {
0917     .queue_xmit    = ip_queue_xmit,
0918     .send_check    = dccp_v4_send_check,
0919     .rebuild_header    = inet_sk_rebuild_header,
0920     .conn_request      = dccp_v4_conn_request,
0921     .syn_recv_sock     = dccp_v4_request_recv_sock,
0922     .net_header_len    = sizeof(struct iphdr),
0923     .setsockopt    = ip_setsockopt,
0924     .getsockopt    = ip_getsockopt,
0925     .addr2sockaddr     = inet_csk_addr2sockaddr,
0926     .sockaddr_len      = sizeof(struct sockaddr_in),
0927 };
0928 
0929 static int dccp_v4_init_sock(struct sock *sk)
0930 {
0931     static __u8 dccp_v4_ctl_sock_initialized;
0932     int err = dccp_init_sock(sk, dccp_v4_ctl_sock_initialized);
0933 
0934     if (err == 0) {
0935         if (unlikely(!dccp_v4_ctl_sock_initialized))
0936             dccp_v4_ctl_sock_initialized = 1;
0937         inet_csk(sk)->icsk_af_ops = &dccp_ipv4_af_ops;
0938     }
0939 
0940     return err;
0941 }
0942 
0943 static struct timewait_sock_ops dccp_timewait_sock_ops = {
0944     .twsk_obj_size  = sizeof(struct inet_timewait_sock),
0945 };
0946 
0947 static struct proto dccp_v4_prot = {
0948     .name           = "DCCP",
0949     .owner          = THIS_MODULE,
0950     .close          = dccp_close,
0951     .connect        = dccp_v4_connect,
0952     .disconnect     = dccp_disconnect,
0953     .ioctl          = dccp_ioctl,
0954     .init           = dccp_v4_init_sock,
0955     .setsockopt     = dccp_setsockopt,
0956     .getsockopt     = dccp_getsockopt,
0957     .sendmsg        = dccp_sendmsg,
0958     .recvmsg        = dccp_recvmsg,
0959     .backlog_rcv        = dccp_v4_do_rcv,
0960     .hash           = inet_hash,
0961     .unhash         = inet_unhash,
0962     .accept         = inet_csk_accept,
0963     .get_port       = inet_csk_get_port,
0964     .shutdown       = dccp_shutdown,
0965     .destroy        = dccp_destroy_sock,
0966     .orphan_count       = &dccp_orphan_count,
0967     .max_header     = MAX_DCCP_HEADER,
0968     .obj_size       = sizeof(struct dccp_sock),
0969     .slab_flags     = SLAB_TYPESAFE_BY_RCU,
0970     .rsk_prot       = &dccp_request_sock_ops,
0971     .twsk_prot      = &dccp_timewait_sock_ops,
0972     .h.hashinfo     = &dccp_hashinfo,
0973 };
0974 
0975 static const struct net_protocol dccp_v4_protocol = {
0976     .handler    = dccp_v4_rcv,
0977     .err_handler    = dccp_v4_err,
0978     .no_policy  = 1,
0979     .icmp_strict_tag_validation = 1,
0980 };
0981 
0982 static const struct proto_ops inet_dccp_ops = {
0983     .family        = PF_INET,
0984     .owner         = THIS_MODULE,
0985     .release       = inet_release,
0986     .bind          = inet_bind,
0987     .connect       = inet_stream_connect,
0988     .socketpair    = sock_no_socketpair,
0989     .accept        = inet_accept,
0990     .getname       = inet_getname,
0991     /* FIXME: work on tcp_poll to rename it to inet_csk_poll */
0992     .poll          = dccp_poll,
0993     .ioctl         = inet_ioctl,
0994     .gettstamp     = sock_gettstamp,
0995     /* FIXME: work on inet_listen to rename it to sock_common_listen */
0996     .listen        = inet_dccp_listen,
0997     .shutdown      = inet_shutdown,
0998     .setsockopt    = sock_common_setsockopt,
0999     .getsockopt    = sock_common_getsockopt,
1000     .sendmsg       = inet_sendmsg,
1001     .recvmsg       = sock_common_recvmsg,
1002     .mmap          = sock_no_mmap,
1003     .sendpage      = sock_no_sendpage,
1004 };
1005 
1006 static struct inet_protosw dccp_v4_protosw = {
1007     .type       = SOCK_DCCP,
1008     .protocol   = IPPROTO_DCCP,
1009     .prot       = &dccp_v4_prot,
1010     .ops        = &inet_dccp_ops,
1011     .flags      = INET_PROTOSW_ICSK,
1012 };
1013 
1014 static int __net_init dccp_v4_init_net(struct net *net)
1015 {
1016     struct dccp_v4_pernet *pn = net_generic(net, dccp_v4_pernet_id);
1017 
1018     if (dccp_hashinfo.bhash == NULL)
1019         return -ESOCKTNOSUPPORT;
1020 
1021     return inet_ctl_sock_create(&pn->v4_ctl_sk, PF_INET,
1022                     SOCK_DCCP, IPPROTO_DCCP, net);
1023 }
1024 
1025 static void __net_exit dccp_v4_exit_net(struct net *net)
1026 {
1027     struct dccp_v4_pernet *pn = net_generic(net, dccp_v4_pernet_id);
1028 
1029     inet_ctl_sock_destroy(pn->v4_ctl_sk);
1030 }
1031 
1032 static void __net_exit dccp_v4_exit_batch(struct list_head *net_exit_list)
1033 {
1034     inet_twsk_purge(&dccp_hashinfo, AF_INET);
1035 }
1036 
1037 static struct pernet_operations dccp_v4_ops = {
1038     .init   = dccp_v4_init_net,
1039     .exit   = dccp_v4_exit_net,
1040     .exit_batch = dccp_v4_exit_batch,
1041     .id = &dccp_v4_pernet_id,
1042     .size   = sizeof(struct dccp_v4_pernet),
1043 };
1044 
1045 static int __init dccp_v4_init(void)
1046 {
1047     int err = proto_register(&dccp_v4_prot, 1);
1048 
1049     if (err)
1050         goto out;
1051 
1052     inet_register_protosw(&dccp_v4_protosw);
1053 
1054     err = register_pernet_subsys(&dccp_v4_ops);
1055     if (err)
1056         goto out_destroy_ctl_sock;
1057 
1058     err = inet_add_protocol(&dccp_v4_protocol, IPPROTO_DCCP);
1059     if (err)
1060         goto out_proto_unregister;
1061 
1062 out:
1063     return err;
1064 out_proto_unregister:
1065     unregister_pernet_subsys(&dccp_v4_ops);
1066 out_destroy_ctl_sock:
1067     inet_unregister_protosw(&dccp_v4_protosw);
1068     proto_unregister(&dccp_v4_prot);
1069     goto out;
1070 }
1071 
1072 static void __exit dccp_v4_exit(void)
1073 {
1074     inet_del_protocol(&dccp_v4_protocol, IPPROTO_DCCP);
1075     unregister_pernet_subsys(&dccp_v4_ops);
1076     inet_unregister_protosw(&dccp_v4_protosw);
1077     proto_unregister(&dccp_v4_prot);
1078 }
1079 
1080 module_init(dccp_v4_init);
1081 module_exit(dccp_v4_exit);
1082 
1083 /*
1084  * __stringify doesn't likes enums, so use SOCK_DCCP (6) and IPPROTO_DCCP (33)
1085  * values directly, Also cover the case where the protocol is not specified,
1086  * i.e. net-pf-PF_INET-proto-0-type-SOCK_DCCP
1087  */
1088 MODULE_ALIAS_NET_PF_PROTO_TYPE(PF_INET, 33, 6);
1089 MODULE_ALIAS_NET_PF_PROTO_TYPE(PF_INET, 0, 6);
1090 MODULE_LICENSE("GPL");
1091 MODULE_AUTHOR("Arnaldo Carvalho de Melo <acme@mandriva.com>");
1092 MODULE_DESCRIPTION("DCCP - Datagram Congestion Controlled Protocol");