1 // SPDX-License-Identifier: GPL-2.0-or-later
5 * An implementation of the DCCP protocol
9 #include <linux/dccp.h>
10 #include <linux/icmp.h>
11 #include <linux/slab.h>
12 #include <linux/module.h>
13 #include <linux/skbuff.h>
14 #include <linux/random.h>
17 #include <net/inet_common.h>
18 #include <net/inet_hashtables.h>
19 #include <net/inet_sock.h>
20 #include <net/protocol.h>
22 #include <net/timewait_sock.h>
23 #include <net/tcp_states.h>
25 #include <net/secure_seq.h>
26 #include <net/netns/generic.h>
33 struct dccp_v4_pernet {
34 struct sock *v4_ctl_sk;
37 static unsigned int dccp_v4_pernet_id __read_mostly;
40 * The per-net v4_ctl_sk socket is used for responding to
41 * the Out-of-the-blue (OOTB) packets. A control sock will be created
42 * for this socket at the initialization time.
45 int dccp_v4_connect(struct sock *sk, struct sockaddr *uaddr, int addr_len)
47 const struct sockaddr_in *usin = (struct sockaddr_in *)uaddr;
48 struct inet_sock *inet = inet_sk(sk);
49 struct dccp_sock *dp = dccp_sk(sk);
50 __be16 orig_sport, orig_dport;
51 __be32 daddr, nexthop;
55 struct ip_options_rcu *inet_opt;
57 dp->dccps_role = DCCP_ROLE_CLIENT;
59 if (addr_len < sizeof(struct sockaddr_in))
62 if (usin->sin_family != AF_INET)
65 nexthop = daddr = usin->sin_addr.s_addr;
67 inet_opt = rcu_dereference_protected(inet->inet_opt,
68 lockdep_sock_is_held(sk));
69 if (inet_opt != NULL && inet_opt->opt.srr) {
72 nexthop = inet_opt->opt.faddr;
75 orig_sport = inet->inet_sport;
76 orig_dport = usin->sin_port;
77 fl4 = &inet->cork.fl.u.ip4;
78 rt = ip_route_connect(fl4, nexthop, inet->inet_saddr,
79 sk->sk_bound_dev_if, IPPROTO_DCCP, orig_sport,
84 if (rt->rt_flags & (RTCF_MULTICAST | RTCF_BROADCAST)) {
89 if (inet_opt == NULL || !inet_opt->opt.srr)
92 if (inet->inet_saddr == 0)
93 inet->inet_saddr = fl4->saddr;
94 sk_rcv_saddr_set(sk, inet->inet_saddr);
95 inet->inet_dport = usin->sin_port;
96 sk_daddr_set(sk, daddr);
98 inet_csk(sk)->icsk_ext_hdr_len = 0;
100 inet_csk(sk)->icsk_ext_hdr_len = inet_opt->opt.optlen;
102 * Socket identity is still unknown (sport may be zero).
103 * However we set state to DCCP_REQUESTING and not releasing socket
104 * lock select source port, enter ourselves into the hash tables and
105 * complete initialization after this.
107 dccp_set_state(sk, DCCP_REQUESTING);
108 err = inet_hash_connect(&dccp_death_row, sk);
112 rt = ip_route_newports(fl4, rt, orig_sport, orig_dport,
113 inet->inet_sport, inet->inet_dport, sk);
119 /* OK, now commit destination to socket. */
120 sk_setup_caps(sk, &rt->dst);
122 dp->dccps_iss = secure_dccp_sequence_number(inet->inet_saddr,
126 inet->inet_id = prandom_u32();
128 err = dccp_connect(sk);
136 * This unhashes the socket and releases the local port, if necessary.
138 dccp_set_state(sk, DCCP_CLOSED);
140 sk->sk_route_caps = 0;
141 inet->inet_dport = 0;
144 EXPORT_SYMBOL_GPL(dccp_v4_connect);
147 * This routine does path mtu discovery as defined in RFC1191.
149 static inline void dccp_do_pmtu_discovery(struct sock *sk,
150 const struct iphdr *iph,
153 struct dst_entry *dst;
154 const struct inet_sock *inet = inet_sk(sk);
155 const struct dccp_sock *dp = dccp_sk(sk);
157 /* We are not interested in DCCP_LISTEN and request_socks (RESPONSEs
158 * send out by Linux are always < 576bytes so they should go through
161 if (sk->sk_state == DCCP_LISTEN)
164 dst = inet_csk_update_pmtu(sk, mtu);
168 /* Something is about to be wrong... Remember soft error
169 * for the case, if this connection will not able to recover.
171 if (mtu < dst_mtu(dst) && ip_dont_fragment(sk, dst))
172 sk->sk_err_soft = EMSGSIZE;
176 if (inet->pmtudisc != IP_PMTUDISC_DONT &&
177 ip_sk_accept_pmtu(sk) &&
178 inet_csk(sk)->icsk_pmtu_cookie > mtu) {
179 dccp_sync_mss(sk, mtu);
182 * From RFC 4340, sec. 14.1:
184 * DCCP-Sync packets are the best choice for upward
185 * probing, since DCCP-Sync probes do not risk application
188 dccp_send_sync(sk, dp->dccps_gsr, DCCP_PKT_SYNC);
189 } /* else let the usual retransmit timer handle it */
192 static void dccp_do_redirect(struct sk_buff *skb, struct sock *sk)
194 struct dst_entry *dst = __sk_dst_check(sk, 0);
197 dst->ops->redirect(dst, sk, skb);
200 void dccp_req_err(struct sock *sk, u64 seq)
202 struct request_sock *req = inet_reqsk(sk);
203 struct net *net = sock_net(sk);
206 * ICMPs are not backlogged, hence we cannot get an established
209 if (!between48(seq, dccp_rsk(req)->dreq_iss, dccp_rsk(req)->dreq_gss)) {
210 __NET_INC_STATS(net, LINUX_MIB_OUTOFWINDOWICMPS);
213 * Still in RESPOND, just remove it silently.
214 * There is no good way to pass the error to the newly
215 * created socket, and POSIX does not want network
216 * errors returned from accept().
218 inet_csk_reqsk_queue_drop(req->rsk_listener, req);
222 EXPORT_SYMBOL(dccp_req_err);
225 * This routine is called by the ICMP module when it gets some sort of error
226 * condition. If err < 0 then the socket should be closed and the error
227 * returned to the user. If err > 0 it's just the icmp type << 8 | icmp code.
228 * After adjustment header points to the first 8 bytes of the tcp header. We
229 * need to find the appropriate port.
231 * The locking strategy used here is very "optimistic". When someone else
232 * accesses the socket the ICMP is just dropped and for some paths there is no
233 * check at all. A more general error queue to queue errors for later handling
234 * is probably better.
236 static int dccp_v4_err(struct sk_buff *skb, u32 info)
238 const struct iphdr *iph = (struct iphdr *)skb->data;
239 const u8 offset = iph->ihl << 2;
240 const struct dccp_hdr *dh;
241 struct dccp_sock *dp;
242 struct inet_sock *inet;
243 const int type = icmp_hdr(skb)->type;
244 const int code = icmp_hdr(skb)->code;
248 struct net *net = dev_net(skb->dev);
250 /* Only need dccph_dport & dccph_sport which are the first
251 * 4 bytes in dccp header.
252 * Our caller (icmp_socket_deliver()) already pulled 8 bytes for us.
254 BUILD_BUG_ON(offsetofend(struct dccp_hdr, dccph_sport) > 8);
255 BUILD_BUG_ON(offsetofend(struct dccp_hdr, dccph_dport) > 8);
256 dh = (struct dccp_hdr *)(skb->data + offset);
258 sk = __inet_lookup_established(net, &dccp_hashinfo,
259 iph->daddr, dh->dccph_dport,
260 iph->saddr, ntohs(dh->dccph_sport),
263 __ICMP_INC_STATS(net, ICMP_MIB_INERRORS);
267 if (sk->sk_state == DCCP_TIME_WAIT) {
268 inet_twsk_put(inet_twsk(sk));
271 seq = dccp_hdr_seq(dh);
272 if (sk->sk_state == DCCP_NEW_SYN_RECV) {
273 dccp_req_err(sk, seq);
278 /* If too many ICMPs get dropped on busy
279 * servers this needs to be solved differently.
281 if (sock_owned_by_user(sk))
282 __NET_INC_STATS(net, LINUX_MIB_LOCKDROPPEDICMPS);
284 if (sk->sk_state == DCCP_CLOSED)
288 if ((1 << sk->sk_state) & ~(DCCPF_REQUESTING | DCCPF_LISTEN) &&
289 !between48(seq, dp->dccps_awl, dp->dccps_awh)) {
290 __NET_INC_STATS(net, LINUX_MIB_OUTOFWINDOWICMPS);
296 if (!sock_owned_by_user(sk))
297 dccp_do_redirect(skb, sk);
299 case ICMP_SOURCE_QUENCH:
300 /* Just silently ignore these. */
302 case ICMP_PARAMETERPROB:
305 case ICMP_DEST_UNREACH:
306 if (code > NR_ICMP_UNREACH)
309 if (code == ICMP_FRAG_NEEDED) { /* PMTU discovery (RFC1191) */
310 if (!sock_owned_by_user(sk))
311 dccp_do_pmtu_discovery(sk, iph, info);
315 err = icmp_err_convert[code].errno;
317 case ICMP_TIME_EXCEEDED:
324 switch (sk->sk_state) {
325 case DCCP_REQUESTING:
327 if (!sock_owned_by_user(sk)) {
328 __DCCP_INC_STATS(DCCP_MIB_ATTEMPTFAILS);
335 sk->sk_err_soft = err;
339 /* If we've already connected we will keep trying
340 * until we time out, or the user gives up.
342 * rfc1122 4.2.3.9 allows to consider as hard errors
343 * only PROTO_UNREACH and PORT_UNREACH (well, FRAG_FAILED too,
344 * but it is obsoleted by pmtu discovery).
346 * Note, that in modern internet, where routing is unreliable
347 * and in each dark corner broken firewalls sit, sending random
348 * errors ordered by their masters even this two messages finally lose
349 * their original sense (even Linux sends invalid PORT_UNREACHs)
351 * Now we are in compliance with RFCs.
356 if (!sock_owned_by_user(sk) && inet->recverr) {
359 } else /* Only an error on timeout */
360 sk->sk_err_soft = err;
367 static inline __sum16 dccp_v4_csum_finish(struct sk_buff *skb,
368 __be32 src, __be32 dst)
370 return csum_tcpudp_magic(src, dst, skb->len, IPPROTO_DCCP, skb->csum);
373 void dccp_v4_send_check(struct sock *sk, struct sk_buff *skb)
375 const struct inet_sock *inet = inet_sk(sk);
376 struct dccp_hdr *dh = dccp_hdr(skb);
378 dccp_csum_outgoing(skb);
379 dh->dccph_checksum = dccp_v4_csum_finish(skb,
383 EXPORT_SYMBOL_GPL(dccp_v4_send_check);
385 static inline u64 dccp_v4_init_sequence(const struct sk_buff *skb)
387 return secure_dccp_sequence_number(ip_hdr(skb)->daddr,
389 dccp_hdr(skb)->dccph_dport,
390 dccp_hdr(skb)->dccph_sport);
394 * The three way handshake has completed - we got a valid ACK or DATAACK -
395 * now create the new socket.
397 * This is the equivalent of TCP's tcp_v4_syn_recv_sock
399 struct sock *dccp_v4_request_recv_sock(const struct sock *sk,
401 struct request_sock *req,
402 struct dst_entry *dst,
403 struct request_sock *req_unhash,
406 struct inet_request_sock *ireq;
407 struct inet_sock *newinet;
410 if (sk_acceptq_is_full(sk))
413 newsk = dccp_create_openreq_child(sk, req, skb);
417 newinet = inet_sk(newsk);
418 ireq = inet_rsk(req);
419 sk_daddr_set(newsk, ireq->ir_rmt_addr);
420 sk_rcv_saddr_set(newsk, ireq->ir_loc_addr);
421 newinet->inet_saddr = ireq->ir_loc_addr;
422 RCU_INIT_POINTER(newinet->inet_opt, rcu_dereference(ireq->ireq_opt));
423 newinet->mc_index = inet_iif(skb);
424 newinet->mc_ttl = ip_hdr(skb)->ttl;
425 newinet->inet_id = prandom_u32();
427 if (dst == NULL && (dst = inet_csk_route_child_sock(sk, newsk, req)) == NULL)
430 sk_setup_caps(newsk, dst);
432 dccp_sync_mss(newsk, dst_mtu(dst));
434 if (__inet_inherit_port(sk, newsk) < 0)
436 *own_req = inet_ehash_nolisten(newsk, req_to_sk(req_unhash), NULL);
438 ireq->ireq_opt = NULL;
440 newinet->inet_opt = NULL;
444 __NET_INC_STATS(sock_net(sk), LINUX_MIB_LISTENOVERFLOWS);
448 __NET_INC_STATS(sock_net(sk), LINUX_MIB_LISTENDROPS);
451 newinet->inet_opt = NULL;
452 inet_csk_prepare_forced_close(newsk);
456 EXPORT_SYMBOL_GPL(dccp_v4_request_recv_sock);
458 static struct dst_entry* dccp_v4_route_skb(struct net *net, struct sock *sk,
462 const struct iphdr *iph = ip_hdr(skb);
463 struct flowi4 fl4 = {
464 .flowi4_oif = inet_iif(skb),
467 .flowi4_tos = RT_CONN_FLAGS(sk),
468 .flowi4_proto = sk->sk_protocol,
469 .fl4_sport = dccp_hdr(skb)->dccph_dport,
470 .fl4_dport = dccp_hdr(skb)->dccph_sport,
473 security_skb_classify_flow(skb, flowi4_to_flowi_common(&fl4));
474 rt = ip_route_output_flow(net, &fl4, sk);
476 IP_INC_STATS(net, IPSTATS_MIB_OUTNOROUTES);
483 static int dccp_v4_send_response(const struct sock *sk, struct request_sock *req)
487 struct dst_entry *dst;
490 dst = inet_csk_route_req(sk, &fl4, req);
494 skb = dccp_make_response(sk, dst, req);
496 const struct inet_request_sock *ireq = inet_rsk(req);
497 struct dccp_hdr *dh = dccp_hdr(skb);
499 dh->dccph_checksum = dccp_v4_csum_finish(skb, ireq->ir_loc_addr,
502 err = ip_build_and_send_pkt(skb, sk, ireq->ir_loc_addr,
504 rcu_dereference(ireq->ireq_opt),
507 err = net_xmit_eval(err);
515 static void dccp_v4_ctl_send_reset(const struct sock *sk, struct sk_buff *rxskb)
518 const struct iphdr *rxiph;
520 struct dst_entry *dst;
521 struct net *net = dev_net(skb_dst(rxskb)->dev);
522 struct dccp_v4_pernet *pn;
525 /* Never send a reset in response to a reset. */
526 if (dccp_hdr(rxskb)->dccph_type == DCCP_PKT_RESET)
529 if (skb_rtable(rxskb)->rt_type != RTN_LOCAL)
532 pn = net_generic(net, dccp_v4_pernet_id);
533 ctl_sk = pn->v4_ctl_sk;
534 dst = dccp_v4_route_skb(net, ctl_sk, rxskb);
538 skb = dccp_ctl_make_reset(ctl_sk, rxskb);
542 rxiph = ip_hdr(rxskb);
543 dccp_hdr(skb)->dccph_checksum = dccp_v4_csum_finish(skb, rxiph->saddr,
545 skb_dst_set(skb, dst_clone(dst));
548 bh_lock_sock(ctl_sk);
549 err = ip_build_and_send_pkt(skb, ctl_sk,
550 rxiph->daddr, rxiph->saddr, NULL,
551 inet_sk(ctl_sk)->tos);
552 bh_unlock_sock(ctl_sk);
554 if (net_xmit_eval(err) == 0) {
555 __DCCP_INC_STATS(DCCP_MIB_OUTSEGS);
556 __DCCP_INC_STATS(DCCP_MIB_OUTRSTS);
563 static void dccp_v4_reqsk_destructor(struct request_sock *req)
565 dccp_feat_list_purge(&dccp_rsk(req)->dreq_featneg);
566 kfree(rcu_dereference_protected(inet_rsk(req)->ireq_opt, 1));
569 void dccp_syn_ack_timeout(const struct request_sock *req)
572 EXPORT_SYMBOL(dccp_syn_ack_timeout);
574 static struct request_sock_ops dccp_request_sock_ops __read_mostly = {
576 .obj_size = sizeof(struct dccp_request_sock),
577 .rtx_syn_ack = dccp_v4_send_response,
578 .send_ack = dccp_reqsk_send_ack,
579 .destructor = dccp_v4_reqsk_destructor,
580 .send_reset = dccp_v4_ctl_send_reset,
581 .syn_ack_timeout = dccp_syn_ack_timeout,
584 int dccp_v4_conn_request(struct sock *sk, struct sk_buff *skb)
586 struct inet_request_sock *ireq;
587 struct request_sock *req;
588 struct dccp_request_sock *dreq;
589 const __be32 service = dccp_hdr_request(skb)->dccph_req_service;
590 struct dccp_skb_cb *dcb = DCCP_SKB_CB(skb);
592 /* Never answer to DCCP_PKT_REQUESTs send to broadcast or multicast */
593 if (skb_rtable(skb)->rt_flags & (RTCF_BROADCAST | RTCF_MULTICAST))
594 return 0; /* discard, don't send a reset here */
596 if (dccp_bad_service_code(sk, service)) {
597 dcb->dccpd_reset_code = DCCP_RESET_CODE_BAD_SERVICE_CODE;
601 * TW buckets are converted to open requests without
602 * limitations, they conserve resources and peer is
603 * evidently real one.
605 dcb->dccpd_reset_code = DCCP_RESET_CODE_TOO_BUSY;
606 if (inet_csk_reqsk_queue_is_full(sk))
609 if (sk_acceptq_is_full(sk))
612 req = inet_reqsk_alloc(&dccp_request_sock_ops, sk, true);
616 if (dccp_reqsk_init(req, dccp_sk(sk), skb))
619 dreq = dccp_rsk(req);
620 if (dccp_parse_options(sk, dreq, skb))
623 if (security_inet_conn_request(sk, skb, req))
626 ireq = inet_rsk(req);
627 sk_rcv_saddr_set(req_to_sk(req), ip_hdr(skb)->daddr);
628 sk_daddr_set(req_to_sk(req), ip_hdr(skb)->saddr);
629 ireq->ir_mark = inet_request_mark(sk, skb);
630 ireq->ireq_family = AF_INET;
631 ireq->ir_iif = READ_ONCE(sk->sk_bound_dev_if);
634 * Step 3: Process LISTEN state
636 * Set S.ISR, S.GSR, S.SWL, S.SWH from packet or Init Cookie
638 * Setting S.SWL/S.SWH to is deferred to dccp_create_openreq_child().
640 dreq->dreq_isr = dcb->dccpd_seq;
641 dreq->dreq_gsr = dreq->dreq_isr;
642 dreq->dreq_iss = dccp_v4_init_sequence(skb);
643 dreq->dreq_gss = dreq->dreq_iss;
644 dreq->dreq_service = service;
646 if (dccp_v4_send_response(sk, req))
649 inet_csk_reqsk_queue_hash_add(sk, req, DCCP_TIMEOUT_INIT);
656 __DCCP_INC_STATS(DCCP_MIB_ATTEMPTFAILS);
659 EXPORT_SYMBOL_GPL(dccp_v4_conn_request);
661 int dccp_v4_do_rcv(struct sock *sk, struct sk_buff *skb)
663 struct dccp_hdr *dh = dccp_hdr(skb);
665 if (sk->sk_state == DCCP_OPEN) { /* Fast path */
666 if (dccp_rcv_established(sk, skb, dh, skb->len))
672 * Step 3: Process LISTEN state
673 * If P.type == Request or P contains a valid Init Cookie option,
674 * (* Must scan the packet's options to check for Init
675 * Cookies. Only Init Cookies are processed here,
676 * however; other options are processed in Step 8. This
677 * scan need only be performed if the endpoint uses Init
679 * (* Generate a new socket and switch to that socket *)
680 * Set S := new socket for this port pair
682 * Choose S.ISS (initial seqno) or set from Init Cookies
683 * Initialize S.GAR := S.ISS
684 * Set S.ISR, S.GSR, S.SWL, S.SWH from packet or Init Cookies
685 * Continue with S.state == RESPOND
686 * (* A Response packet will be generated in Step 11 *)
688 * Generate Reset(No Connection) unless P.type == Reset
689 * Drop packet and return
691 * NOTE: the check for the packet types is done in
692 * dccp_rcv_state_process
695 if (dccp_rcv_state_process(sk, skb, dh, skb->len))
700 dccp_v4_ctl_send_reset(sk, skb);
704 EXPORT_SYMBOL_GPL(dccp_v4_do_rcv);
707 * dccp_invalid_packet - check for malformed packets
708 * @skb: Packet to validate
710 * Implements RFC 4340, 8.5: Step 1: Check header basics
711 * Packets that fail these checks are ignored and do not receive Resets.
713 int dccp_invalid_packet(struct sk_buff *skb)
715 const struct dccp_hdr *dh;
719 if (skb->pkt_type != PACKET_HOST)
722 /* If the packet is shorter than 12 bytes, drop packet and return */
723 if (!pskb_may_pull(skb, sizeof(struct dccp_hdr))) {
724 DCCP_WARN("pskb_may_pull failed\n");
730 /* If P.type is not understood, drop packet and return */
731 if (dh->dccph_type >= DCCP_PKT_INVALID) {
732 DCCP_WARN("invalid packet type\n");
737 * If P.Data Offset is too small for packet type, drop packet and return
739 dccph_doff = dh->dccph_doff;
740 if (dccph_doff < dccp_hdr_len(skb) / sizeof(u32)) {
741 DCCP_WARN("P.Data Offset(%u) too small\n", dccph_doff);
745 * If P.Data Offset is too large for packet, drop packet and return
747 if (!pskb_may_pull(skb, dccph_doff * sizeof(u32))) {
748 DCCP_WARN("P.Data Offset(%u) too large\n", dccph_doff);
753 * If P.type is not Data, Ack, or DataAck and P.X == 0 (the packet
754 * has short sequence numbers), drop packet and return
756 if ((dh->dccph_type < DCCP_PKT_DATA ||
757 dh->dccph_type > DCCP_PKT_DATAACK) && dh->dccph_x == 0) {
758 DCCP_WARN("P.type (%s) not Data || [Data]Ack, while P.X == 0\n",
759 dccp_packet_name(dh->dccph_type));
764 * If P.CsCov is too large for the packet size, drop packet and return.
765 * This must come _before_ checksumming (not as RFC 4340 suggests).
767 cscov = dccp_csum_coverage(skb);
768 if (cscov > skb->len) {
769 DCCP_WARN("P.CsCov %u exceeds packet length %d\n",
770 dh->dccph_cscov, skb->len);
774 /* If header checksum is incorrect, drop packet and return.
775 * (This step is completed in the AF-dependent functions.) */
776 skb->csum = skb_checksum(skb, 0, cscov, 0);
780 EXPORT_SYMBOL_GPL(dccp_invalid_packet);
782 /* this is called when real data arrives */
783 static int dccp_v4_rcv(struct sk_buff *skb)
785 const struct dccp_hdr *dh;
786 const struct iphdr *iph;
791 /* Step 1: Check header basics */
793 if (dccp_invalid_packet(skb))
797 /* Step 1: If header checksum is incorrect, drop packet and return */
798 if (dccp_v4_csum_finish(skb, iph->saddr, iph->daddr)) {
799 DCCP_WARN("dropped packet with invalid checksum\n");
805 DCCP_SKB_CB(skb)->dccpd_seq = dccp_hdr_seq(dh);
806 DCCP_SKB_CB(skb)->dccpd_type = dh->dccph_type;
808 dccp_pr_debug("%8.8s src=%pI4@%-5d dst=%pI4@%-5d seq=%llu",
809 dccp_packet_name(dh->dccph_type),
810 &iph->saddr, ntohs(dh->dccph_sport),
811 &iph->daddr, ntohs(dh->dccph_dport),
812 (unsigned long long) DCCP_SKB_CB(skb)->dccpd_seq);
814 if (dccp_packet_without_ack(skb)) {
815 DCCP_SKB_CB(skb)->dccpd_ack_seq = DCCP_PKT_WITHOUT_ACK_SEQ;
816 dccp_pr_debug_cat("\n");
818 DCCP_SKB_CB(skb)->dccpd_ack_seq = dccp_hdr_ack_seq(skb);
819 dccp_pr_debug_cat(", ack=%llu\n", (unsigned long long)
820 DCCP_SKB_CB(skb)->dccpd_ack_seq);
824 sk = __inet_lookup_skb(&dccp_hashinfo, skb, __dccp_hdr_len(dh),
825 dh->dccph_sport, dh->dccph_dport, 0, &refcounted);
827 dccp_pr_debug("failed to look up flow ID in table and "
828 "get corresponding socket\n");
834 * ... or S.state == TIMEWAIT,
835 * Generate Reset(No Connection) unless P.type == Reset
836 * Drop packet and return
838 if (sk->sk_state == DCCP_TIME_WAIT) {
839 dccp_pr_debug("sk->sk_state == DCCP_TIME_WAIT: do_time_wait\n");
840 inet_twsk_put(inet_twsk(sk));
844 if (sk->sk_state == DCCP_NEW_SYN_RECV) {
845 struct request_sock *req = inet_reqsk(sk);
848 sk = req->rsk_listener;
849 if (unlikely(sk->sk_state != DCCP_LISTEN)) {
850 inet_csk_reqsk_queue_drop_and_put(sk, req);
855 nsk = dccp_check_req(sk, skb, req);
858 goto discard_and_relse;
862 } else if (dccp_child_process(sk, nsk, skb)) {
863 dccp_v4_ctl_send_reset(sk, skb);
864 goto discard_and_relse;
871 * RFC 4340, sec. 9.2.1: Minimum Checksum Coverage
872 * o if MinCsCov = 0, only packets with CsCov = 0 are accepted
873 * o if MinCsCov > 0, also accept packets with CsCov >= MinCsCov
875 min_cov = dccp_sk(sk)->dccps_pcrlen;
876 if (dh->dccph_cscov && (min_cov == 0 || dh->dccph_cscov < min_cov)) {
877 dccp_pr_debug("Packet CsCov %d does not satisfy MinCsCov %d\n",
878 dh->dccph_cscov, min_cov);
879 /* FIXME: "Such packets SHOULD be reported using Data Dropped
880 * options (Section 11.7) with Drop Code 0, Protocol
882 goto discard_and_relse;
885 if (!xfrm4_policy_check(sk, XFRM_POLICY_IN, skb))
886 goto discard_and_relse;
889 return __sk_receive_skb(sk, skb, 1, dh->dccph_doff * 4, refcounted);
892 if (!xfrm4_policy_check(NULL, XFRM_POLICY_IN, skb))
897 * Generate Reset(No Connection) unless P.type == Reset
898 * Drop packet and return
900 if (dh->dccph_type != DCCP_PKT_RESET) {
901 DCCP_SKB_CB(skb)->dccpd_reset_code =
902 DCCP_RESET_CODE_NO_CONNECTION;
903 dccp_v4_ctl_send_reset(sk, skb);
916 static const struct inet_connection_sock_af_ops dccp_ipv4_af_ops = {
917 .queue_xmit = ip_queue_xmit,
918 .send_check = dccp_v4_send_check,
919 .rebuild_header = inet_sk_rebuild_header,
920 .conn_request = dccp_v4_conn_request,
921 .syn_recv_sock = dccp_v4_request_recv_sock,
922 .net_header_len = sizeof(struct iphdr),
923 .setsockopt = ip_setsockopt,
924 .getsockopt = ip_getsockopt,
925 .addr2sockaddr = inet_csk_addr2sockaddr,
926 .sockaddr_len = sizeof(struct sockaddr_in),
929 static int dccp_v4_init_sock(struct sock *sk)
931 static __u8 dccp_v4_ctl_sock_initialized;
932 int err = dccp_init_sock(sk, dccp_v4_ctl_sock_initialized);
935 if (unlikely(!dccp_v4_ctl_sock_initialized))
936 dccp_v4_ctl_sock_initialized = 1;
937 inet_csk(sk)->icsk_af_ops = &dccp_ipv4_af_ops;
943 static struct timewait_sock_ops dccp_timewait_sock_ops = {
944 .twsk_obj_size = sizeof(struct inet_timewait_sock),
947 static struct proto dccp_v4_prot = {
949 .owner = THIS_MODULE,
951 .connect = dccp_v4_connect,
952 .disconnect = dccp_disconnect,
954 .init = dccp_v4_init_sock,
955 .setsockopt = dccp_setsockopt,
956 .getsockopt = dccp_getsockopt,
957 .sendmsg = dccp_sendmsg,
958 .recvmsg = dccp_recvmsg,
959 .backlog_rcv = dccp_v4_do_rcv,
961 .unhash = inet_unhash,
962 .accept = inet_csk_accept,
963 .get_port = inet_csk_get_port,
964 .shutdown = dccp_shutdown,
965 .destroy = dccp_destroy_sock,
966 .orphan_count = &dccp_orphan_count,
967 .max_header = MAX_DCCP_HEADER,
968 .obj_size = sizeof(struct dccp_sock),
969 .slab_flags = SLAB_TYPESAFE_BY_RCU,
970 .rsk_prot = &dccp_request_sock_ops,
971 .twsk_prot = &dccp_timewait_sock_ops,
972 .h.hashinfo = &dccp_hashinfo,
975 static const struct net_protocol dccp_v4_protocol = {
976 .handler = dccp_v4_rcv,
977 .err_handler = dccp_v4_err,
979 .icmp_strict_tag_validation = 1,
982 static const struct proto_ops inet_dccp_ops = {
984 .owner = THIS_MODULE,
985 .release = inet_release,
987 .connect = inet_stream_connect,
988 .socketpair = sock_no_socketpair,
989 .accept = inet_accept,
990 .getname = inet_getname,
991 /* FIXME: work on tcp_poll to rename it to inet_csk_poll */
994 .gettstamp = sock_gettstamp,
995 /* FIXME: work on inet_listen to rename it to sock_common_listen */
996 .listen = inet_dccp_listen,
997 .shutdown = inet_shutdown,
998 .setsockopt = sock_common_setsockopt,
999 .getsockopt = sock_common_getsockopt,
1000 .sendmsg = inet_sendmsg,
1001 .recvmsg = sock_common_recvmsg,
1002 .mmap = sock_no_mmap,
1003 .sendpage = sock_no_sendpage,
1006 static struct inet_protosw dccp_v4_protosw = {
1008 .protocol = IPPROTO_DCCP,
1009 .prot = &dccp_v4_prot,
1010 .ops = &inet_dccp_ops,
1011 .flags = INET_PROTOSW_ICSK,
1014 static int __net_init dccp_v4_init_net(struct net *net)
1016 struct dccp_v4_pernet *pn = net_generic(net, dccp_v4_pernet_id);
1018 if (dccp_hashinfo.bhash == NULL)
1019 return -ESOCKTNOSUPPORT;
1021 return inet_ctl_sock_create(&pn->v4_ctl_sk, PF_INET,
1022 SOCK_DCCP, IPPROTO_DCCP, net);
1025 static void __net_exit dccp_v4_exit_net(struct net *net)
1027 struct dccp_v4_pernet *pn = net_generic(net, dccp_v4_pernet_id);
1029 inet_ctl_sock_destroy(pn->v4_ctl_sk);
1032 static void __net_exit dccp_v4_exit_batch(struct list_head *net_exit_list)
1034 inet_twsk_purge(&dccp_hashinfo, AF_INET);
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),
1045 static int __init dccp_v4_init(void)
1047 int err = proto_register(&dccp_v4_prot, 1);
1052 inet_register_protosw(&dccp_v4_protosw);
1054 err = register_pernet_subsys(&dccp_v4_ops);
1056 goto out_destroy_ctl_sock;
1058 err = inet_add_protocol(&dccp_v4_protocol, IPPROTO_DCCP);
1060 goto out_proto_unregister;
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);
1072 static void __exit dccp_v4_exit(void)
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);
1080 module_init(dccp_v4_init);
1081 module_exit(dccp_v4_exit);
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
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");
1092 MODULE_DESCRIPTION("DCCP - Datagram Congestion Controlled Protocol");