^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-or-later
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * net/dccp/ipv4.c
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * An implementation of the DCCP protocol
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) * Arnaldo Carvalho de Melo <acme@conectiva.com.br>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <linux/dccp.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <linux/icmp.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/skbuff.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/random.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <net/icmp.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include <net/inet_common.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include <net/inet_hashtables.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #include <net/inet_sock.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #include <net/protocol.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) #include <net/sock.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) #include <net/timewait_sock.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) #include <net/tcp_states.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) #include <net/xfrm.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) #include <net/secure_seq.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) #include "ackvec.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) #include "ccid.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) #include "dccp.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) #include "feat.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) * The per-net dccp.v4_ctl_sk socket is used for responding to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) * the Out-of-the-blue (OOTB) packets. A control sock will be created
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) * for this socket at the initialization time.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) int dccp_v4_connect(struct sock *sk, struct sockaddr *uaddr, int addr_len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) const struct sockaddr_in *usin = (struct sockaddr_in *)uaddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) struct inet_sock *inet = inet_sk(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) struct dccp_sock *dp = dccp_sk(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) __be16 orig_sport, orig_dport;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) __be32 daddr, nexthop;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) struct flowi4 *fl4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) struct rtable *rt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) struct ip_options_rcu *inet_opt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) dp->dccps_role = DCCP_ROLE_CLIENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) if (addr_len < sizeof(struct sockaddr_in))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) if (usin->sin_family != AF_INET)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) return -EAFNOSUPPORT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) nexthop = daddr = usin->sin_addr.s_addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) inet_opt = rcu_dereference_protected(inet->inet_opt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) lockdep_sock_is_held(sk));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) if (inet_opt != NULL && inet_opt->opt.srr) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) if (daddr == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) nexthop = inet_opt->opt.faddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) orig_sport = inet->inet_sport;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) orig_dport = usin->sin_port;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) fl4 = &inet->cork.fl.u.ip4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) rt = ip_route_connect(fl4, nexthop, inet->inet_saddr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) RT_CONN_FLAGS(sk), sk->sk_bound_dev_if,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) IPPROTO_DCCP,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) orig_sport, orig_dport, sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) if (IS_ERR(rt))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) return PTR_ERR(rt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) if (rt->rt_flags & (RTCF_MULTICAST | RTCF_BROADCAST)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) ip_rt_put(rt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) return -ENETUNREACH;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) if (inet_opt == NULL || !inet_opt->opt.srr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) daddr = fl4->daddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) if (inet->inet_saddr == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) inet->inet_saddr = fl4->saddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) sk_rcv_saddr_set(sk, inet->inet_saddr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) inet->inet_dport = usin->sin_port;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) sk_daddr_set(sk, daddr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) inet_csk(sk)->icsk_ext_hdr_len = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) if (inet_opt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) inet_csk(sk)->icsk_ext_hdr_len = inet_opt->opt.optlen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) * Socket identity is still unknown (sport may be zero).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) * However we set state to DCCP_REQUESTING and not releasing socket
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) * lock select source port, enter ourselves into the hash tables and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) * complete initialization after this.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) dccp_set_state(sk, DCCP_REQUESTING);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) err = inet_hash_connect(&dccp_death_row, sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) if (err != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) goto failure;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) rt = ip_route_newports(fl4, rt, orig_sport, orig_dport,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) inet->inet_sport, inet->inet_dport, sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) if (IS_ERR(rt)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) err = PTR_ERR(rt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) rt = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) goto failure;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) /* OK, now commit destination to socket. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) sk_setup_caps(sk, &rt->dst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) dp->dccps_iss = secure_dccp_sequence_number(inet->inet_saddr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) inet->inet_daddr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) inet->inet_sport,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) inet->inet_dport);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) inet->inet_id = prandom_u32();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) err = dccp_connect(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) rt = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) if (err != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) goto failure;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) failure:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) * This unhashes the socket and releases the local port, if necessary.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) dccp_set_state(sk, DCCP_CLOSED);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) ip_rt_put(rt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) sk->sk_route_caps = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) inet->inet_dport = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) EXPORT_SYMBOL_GPL(dccp_v4_connect);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) * This routine does path mtu discovery as defined in RFC1191.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) static inline void dccp_do_pmtu_discovery(struct sock *sk,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) const struct iphdr *iph,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) u32 mtu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) struct dst_entry *dst;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) const struct inet_sock *inet = inet_sk(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) const struct dccp_sock *dp = dccp_sk(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) /* We are not interested in DCCP_LISTEN and request_socks (RESPONSEs
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) * send out by Linux are always < 576bytes so they should go through
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) * unfragmented).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) if (sk->sk_state == DCCP_LISTEN)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) dst = inet_csk_update_pmtu(sk, mtu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) if (!dst)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) /* Something is about to be wrong... Remember soft error
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) * for the case, if this connection will not able to recover.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) if (mtu < dst_mtu(dst) && ip_dont_fragment(sk, dst))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) sk->sk_err_soft = EMSGSIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) mtu = dst_mtu(dst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) if (inet->pmtudisc != IP_PMTUDISC_DONT &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) ip_sk_accept_pmtu(sk) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) inet_csk(sk)->icsk_pmtu_cookie > mtu) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) dccp_sync_mss(sk, mtu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) * From RFC 4340, sec. 14.1:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) * DCCP-Sync packets are the best choice for upward
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) * probing, since DCCP-Sync probes do not risk application
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) * data loss.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) dccp_send_sync(sk, dp->dccps_gsr, DCCP_PKT_SYNC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) } /* else let the usual retransmit timer handle it */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) static void dccp_do_redirect(struct sk_buff *skb, struct sock *sk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) struct dst_entry *dst = __sk_dst_check(sk, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) if (dst)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) dst->ops->redirect(dst, sk, skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) void dccp_req_err(struct sock *sk, u64 seq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) struct request_sock *req = inet_reqsk(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) struct net *net = sock_net(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) * ICMPs are not backlogged, hence we cannot get an established
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) * socket here.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) if (!between48(seq, dccp_rsk(req)->dreq_iss, dccp_rsk(req)->dreq_gss)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) __NET_INC_STATS(net, LINUX_MIB_OUTOFWINDOWICMPS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) * Still in RESPOND, just remove it silently.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) * There is no good way to pass the error to the newly
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) * created socket, and POSIX does not want network
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) * errors returned from accept().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) inet_csk_reqsk_queue_drop(req->rsk_listener, req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) reqsk_put(req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) EXPORT_SYMBOL(dccp_req_err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) * This routine is called by the ICMP module when it gets some sort of error
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) * condition. If err < 0 then the socket should be closed and the error
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) * returned to the user. If err > 0 it's just the icmp type << 8 | icmp code.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) * After adjustment header points to the first 8 bytes of the tcp header. We
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) * need to find the appropriate port.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) * The locking strategy used here is very "optimistic". When someone else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) * accesses the socket the ICMP is just dropped and for some paths there is no
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) * check at all. A more general error queue to queue errors for later handling
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) * is probably better.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) static int dccp_v4_err(struct sk_buff *skb, u32 info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) const struct iphdr *iph = (struct iphdr *)skb->data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) const u8 offset = iph->ihl << 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) const struct dccp_hdr *dh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) struct dccp_sock *dp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) struct inet_sock *inet;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) const int type = icmp_hdr(skb)->type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) const int code = icmp_hdr(skb)->code;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) struct sock *sk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) __u64 seq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) struct net *net = dev_net(skb->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) /* Only need dccph_dport & dccph_sport which are the first
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) * 4 bytes in dccp header.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) * Our caller (icmp_socket_deliver()) already pulled 8 bytes for us.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) BUILD_BUG_ON(offsetofend(struct dccp_hdr, dccph_sport) > 8);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) BUILD_BUG_ON(offsetofend(struct dccp_hdr, dccph_dport) > 8);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) dh = (struct dccp_hdr *)(skb->data + offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) sk = __inet_lookup_established(net, &dccp_hashinfo,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) iph->daddr, dh->dccph_dport,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) iph->saddr, ntohs(dh->dccph_sport),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) inet_iif(skb), 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) if (!sk) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) __ICMP_INC_STATS(net, ICMP_MIB_INERRORS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) return -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) if (sk->sk_state == DCCP_TIME_WAIT) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) inet_twsk_put(inet_twsk(sk));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) seq = dccp_hdr_seq(dh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) if (sk->sk_state == DCCP_NEW_SYN_RECV) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) dccp_req_err(sk, seq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) bh_lock_sock(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) /* If too many ICMPs get dropped on busy
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) * servers this needs to be solved differently.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) if (sock_owned_by_user(sk))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) __NET_INC_STATS(net, LINUX_MIB_LOCKDROPPEDICMPS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) if (sk->sk_state == DCCP_CLOSED)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) dp = dccp_sk(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) if ((1 << sk->sk_state) & ~(DCCPF_REQUESTING | DCCPF_LISTEN) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) !between48(seq, dp->dccps_awl, dp->dccps_awh)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) __NET_INC_STATS(net, LINUX_MIB_OUTOFWINDOWICMPS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) switch (type) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) case ICMP_REDIRECT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) if (!sock_owned_by_user(sk))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) dccp_do_redirect(skb, sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) case ICMP_SOURCE_QUENCH:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) /* Just silently ignore these. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) case ICMP_PARAMETERPROB:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) err = EPROTO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) case ICMP_DEST_UNREACH:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) if (code > NR_ICMP_UNREACH)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) if (code == ICMP_FRAG_NEEDED) { /* PMTU discovery (RFC1191) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) if (!sock_owned_by_user(sk))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) dccp_do_pmtu_discovery(sk, iph, info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) err = icmp_err_convert[code].errno;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) case ICMP_TIME_EXCEEDED:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) err = EHOSTUNREACH;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) switch (sk->sk_state) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) case DCCP_REQUESTING:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) case DCCP_RESPOND:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) if (!sock_owned_by_user(sk)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) __DCCP_INC_STATS(DCCP_MIB_ATTEMPTFAILS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) sk->sk_err = err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) sk->sk_error_report(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) dccp_done(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) } else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) sk->sk_err_soft = err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) /* If we've already connected we will keep trying
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) * until we time out, or the user gives up.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) * rfc1122 4.2.3.9 allows to consider as hard errors
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) * only PROTO_UNREACH and PORT_UNREACH (well, FRAG_FAILED too,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) * but it is obsoleted by pmtu discovery).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) * Note, that in modern internet, where routing is unreliable
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) * and in each dark corner broken firewalls sit, sending random
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) * errors ordered by their masters even this two messages finally lose
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) * their original sense (even Linux sends invalid PORT_UNREACHs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) * Now we are in compliance with RFCs.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) * --ANK (980905)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) inet = inet_sk(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) if (!sock_owned_by_user(sk) && inet->recverr) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) sk->sk_err = err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) sk->sk_error_report(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) } else /* Only an error on timeout */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) sk->sk_err_soft = err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) bh_unlock_sock(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) sock_put(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) static inline __sum16 dccp_v4_csum_finish(struct sk_buff *skb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) __be32 src, __be32 dst)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) return csum_tcpudp_magic(src, dst, skb->len, IPPROTO_DCCP, skb->csum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) void dccp_v4_send_check(struct sock *sk, struct sk_buff *skb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) const struct inet_sock *inet = inet_sk(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) struct dccp_hdr *dh = dccp_hdr(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) dccp_csum_outgoing(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) dh->dccph_checksum = dccp_v4_csum_finish(skb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) inet->inet_saddr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) inet->inet_daddr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) EXPORT_SYMBOL_GPL(dccp_v4_send_check);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) static inline u64 dccp_v4_init_sequence(const struct sk_buff *skb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) return secure_dccp_sequence_number(ip_hdr(skb)->daddr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) ip_hdr(skb)->saddr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) dccp_hdr(skb)->dccph_dport,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) dccp_hdr(skb)->dccph_sport);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) * The three way handshake has completed - we got a valid ACK or DATAACK -
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) * now create the new socket.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) * This is the equivalent of TCP's tcp_v4_syn_recv_sock
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) struct sock *dccp_v4_request_recv_sock(const struct sock *sk,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) struct sk_buff *skb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) struct request_sock *req,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) struct dst_entry *dst,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) struct request_sock *req_unhash,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) bool *own_req)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) struct inet_request_sock *ireq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) struct inet_sock *newinet;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) struct sock *newsk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) if (sk_acceptq_is_full(sk))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) goto exit_overflow;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) newsk = dccp_create_openreq_child(sk, req, skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) if (newsk == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) goto exit_nonewsk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) newinet = inet_sk(newsk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) ireq = inet_rsk(req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) sk_daddr_set(newsk, ireq->ir_rmt_addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) sk_rcv_saddr_set(newsk, ireq->ir_loc_addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) newinet->inet_saddr = ireq->ir_loc_addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) RCU_INIT_POINTER(newinet->inet_opt, rcu_dereference(ireq->ireq_opt));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) newinet->mc_index = inet_iif(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) newinet->mc_ttl = ip_hdr(skb)->ttl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) newinet->inet_id = prandom_u32();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) if (dst == NULL && (dst = inet_csk_route_child_sock(sk, newsk, req)) == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) goto put_and_exit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) sk_setup_caps(newsk, dst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) dccp_sync_mss(newsk, dst_mtu(dst));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) if (__inet_inherit_port(sk, newsk) < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) goto put_and_exit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) *own_req = inet_ehash_nolisten(newsk, req_to_sk(req_unhash), NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) if (*own_req)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) ireq->ireq_opt = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) newinet->inet_opt = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) return newsk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) exit_overflow:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) __NET_INC_STATS(sock_net(sk), LINUX_MIB_LISTENOVERFLOWS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) exit_nonewsk:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) dst_release(dst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) exit:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) __NET_INC_STATS(sock_net(sk), LINUX_MIB_LISTENDROPS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) put_and_exit:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) newinet->inet_opt = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) inet_csk_prepare_forced_close(newsk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) dccp_done(newsk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) goto exit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) EXPORT_SYMBOL_GPL(dccp_v4_request_recv_sock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) static struct dst_entry* dccp_v4_route_skb(struct net *net, struct sock *sk,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) struct sk_buff *skb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) struct rtable *rt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) const struct iphdr *iph = ip_hdr(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) struct flowi4 fl4 = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) .flowi4_oif = inet_iif(skb),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) .daddr = iph->saddr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) .saddr = iph->daddr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) .flowi4_tos = RT_CONN_FLAGS(sk),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) .flowi4_proto = sk->sk_protocol,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) .fl4_sport = dccp_hdr(skb)->dccph_dport,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) .fl4_dport = dccp_hdr(skb)->dccph_sport,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) security_skb_classify_flow(skb, flowi4_to_flowi(&fl4));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) rt = ip_route_output_flow(net, &fl4, sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) if (IS_ERR(rt)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) IP_INC_STATS(net, IPSTATS_MIB_OUTNOROUTES);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) return &rt->dst;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) static int dccp_v4_send_response(const struct sock *sk, struct request_sock *req)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) int err = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) struct sk_buff *skb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) struct dst_entry *dst;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) struct flowi4 fl4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) dst = inet_csk_route_req(sk, &fl4, req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) if (dst == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) skb = dccp_make_response(sk, dst, req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) if (skb != NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) const struct inet_request_sock *ireq = inet_rsk(req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) struct dccp_hdr *dh = dccp_hdr(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) dh->dccph_checksum = dccp_v4_csum_finish(skb, ireq->ir_loc_addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) ireq->ir_rmt_addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) rcu_read_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) err = ip_build_and_send_pkt(skb, sk, ireq->ir_loc_addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) ireq->ir_rmt_addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) rcu_dereference(ireq->ireq_opt),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) inet_sk(sk)->tos);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) rcu_read_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) err = net_xmit_eval(err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) dst_release(dst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) static void dccp_v4_ctl_send_reset(const struct sock *sk, struct sk_buff *rxskb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) const struct iphdr *rxiph;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) struct sk_buff *skb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) struct dst_entry *dst;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) struct net *net = dev_net(skb_dst(rxskb)->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) struct sock *ctl_sk = net->dccp.v4_ctl_sk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) /* Never send a reset in response to a reset. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) if (dccp_hdr(rxskb)->dccph_type == DCCP_PKT_RESET)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) if (skb_rtable(rxskb)->rt_type != RTN_LOCAL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) dst = dccp_v4_route_skb(net, ctl_sk, rxskb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) if (dst == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) skb = dccp_ctl_make_reset(ctl_sk, rxskb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) if (skb == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) rxiph = ip_hdr(rxskb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) dccp_hdr(skb)->dccph_checksum = dccp_v4_csum_finish(skb, rxiph->saddr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) rxiph->daddr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) skb_dst_set(skb, dst_clone(dst));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) local_bh_disable();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) bh_lock_sock(ctl_sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) err = ip_build_and_send_pkt(skb, ctl_sk,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) rxiph->daddr, rxiph->saddr, NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) inet_sk(ctl_sk)->tos);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) bh_unlock_sock(ctl_sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) if (net_xmit_eval(err) == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) __DCCP_INC_STATS(DCCP_MIB_OUTSEGS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) __DCCP_INC_STATS(DCCP_MIB_OUTRSTS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) local_bh_enable();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) dst_release(dst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) static void dccp_v4_reqsk_destructor(struct request_sock *req)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) dccp_feat_list_purge(&dccp_rsk(req)->dreq_featneg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557) kfree(rcu_dereference_protected(inet_rsk(req)->ireq_opt, 1));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560) void dccp_syn_ack_timeout(const struct request_sock *req)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) EXPORT_SYMBOL(dccp_syn_ack_timeout);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) static struct request_sock_ops dccp_request_sock_ops __read_mostly = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) .family = PF_INET,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567) .obj_size = sizeof(struct dccp_request_sock),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568) .rtx_syn_ack = dccp_v4_send_response,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) .send_ack = dccp_reqsk_send_ack,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) .destructor = dccp_v4_reqsk_destructor,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571) .send_reset = dccp_v4_ctl_send_reset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572) .syn_ack_timeout = dccp_syn_ack_timeout,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575) int dccp_v4_conn_request(struct sock *sk, struct sk_buff *skb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577) struct inet_request_sock *ireq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578) struct request_sock *req;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579) struct dccp_request_sock *dreq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580) const __be32 service = dccp_hdr_request(skb)->dccph_req_service;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581) struct dccp_skb_cb *dcb = DCCP_SKB_CB(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583) /* Never answer to DCCP_PKT_REQUESTs send to broadcast or multicast */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584) if (skb_rtable(skb)->rt_flags & (RTCF_BROADCAST | RTCF_MULTICAST))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585) return 0; /* discard, don't send a reset here */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587) if (dccp_bad_service_code(sk, service)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588) dcb->dccpd_reset_code = DCCP_RESET_CODE_BAD_SERVICE_CODE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589) goto drop;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592) * TW buckets are converted to open requests without
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593) * limitations, they conserve resources and peer is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594) * evidently real one.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596) dcb->dccpd_reset_code = DCCP_RESET_CODE_TOO_BUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597) if (inet_csk_reqsk_queue_is_full(sk))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 598) goto drop;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 599)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 600) if (sk_acceptq_is_full(sk))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 601) goto drop;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 602)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 603) req = inet_reqsk_alloc(&dccp_request_sock_ops, sk, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 604) if (req == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 605) goto drop;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 606)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 607) if (dccp_reqsk_init(req, dccp_sk(sk), skb))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 608) goto drop_and_free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 609)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 610) dreq = dccp_rsk(req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 611) if (dccp_parse_options(sk, dreq, skb))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 612) goto drop_and_free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 613)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 614) if (security_inet_conn_request(sk, skb, req))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 615) goto drop_and_free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 616)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 617) ireq = inet_rsk(req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 618) sk_rcv_saddr_set(req_to_sk(req), ip_hdr(skb)->daddr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 619) sk_daddr_set(req_to_sk(req), ip_hdr(skb)->saddr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 620) ireq->ir_mark = inet_request_mark(sk, skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 621) ireq->ireq_family = AF_INET;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 622) ireq->ir_iif = sk->sk_bound_dev_if;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 623)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 624) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 625) * Step 3: Process LISTEN state
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 626) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 627) * Set S.ISR, S.GSR, S.SWL, S.SWH from packet or Init Cookie
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 628) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 629) * Setting S.SWL/S.SWH to is deferred to dccp_create_openreq_child().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 630) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 631) dreq->dreq_isr = dcb->dccpd_seq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 632) dreq->dreq_gsr = dreq->dreq_isr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 633) dreq->dreq_iss = dccp_v4_init_sequence(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 634) dreq->dreq_gss = dreq->dreq_iss;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 635) dreq->dreq_service = service;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 636)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 637) if (dccp_v4_send_response(sk, req))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 638) goto drop_and_free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 639)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 640) inet_csk_reqsk_queue_hash_add(sk, req, DCCP_TIMEOUT_INIT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 641) reqsk_put(req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 642) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 643)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 644) drop_and_free:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 645) reqsk_free(req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 646) drop:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 647) __DCCP_INC_STATS(DCCP_MIB_ATTEMPTFAILS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 648) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 649) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 650) EXPORT_SYMBOL_GPL(dccp_v4_conn_request);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 651)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 652) int dccp_v4_do_rcv(struct sock *sk, struct sk_buff *skb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 653) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 654) struct dccp_hdr *dh = dccp_hdr(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 655)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 656) if (sk->sk_state == DCCP_OPEN) { /* Fast path */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 657) if (dccp_rcv_established(sk, skb, dh, skb->len))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 658) goto reset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 659) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 660) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 661)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 662) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 663) * Step 3: Process LISTEN state
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 664) * If P.type == Request or P contains a valid Init Cookie option,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 665) * (* Must scan the packet's options to check for Init
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 666) * Cookies. Only Init Cookies are processed here,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 667) * however; other options are processed in Step 8. This
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 668) * scan need only be performed if the endpoint uses Init
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 669) * Cookies *)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 670) * (* Generate a new socket and switch to that socket *)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 671) * Set S := new socket for this port pair
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 672) * S.state = RESPOND
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 673) * Choose S.ISS (initial seqno) or set from Init Cookies
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 674) * Initialize S.GAR := S.ISS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 675) * Set S.ISR, S.GSR, S.SWL, S.SWH from packet or Init Cookies
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 676) * Continue with S.state == RESPOND
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 677) * (* A Response packet will be generated in Step 11 *)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 678) * Otherwise,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 679) * Generate Reset(No Connection) unless P.type == Reset
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 680) * Drop packet and return
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 681) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 682) * NOTE: the check for the packet types is done in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 683) * dccp_rcv_state_process
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 684) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 685)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 686) if (dccp_rcv_state_process(sk, skb, dh, skb->len))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 687) goto reset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 688) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 689)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 690) reset:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 691) dccp_v4_ctl_send_reset(sk, skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 692) kfree_skb(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 693) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 694) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 695) EXPORT_SYMBOL_GPL(dccp_v4_do_rcv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 696)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 697) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 698) * dccp_invalid_packet - check for malformed packets
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 699) * @skb: Packet to validate
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 700) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 701) * Implements RFC 4340, 8.5: Step 1: Check header basics
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 702) * Packets that fail these checks are ignored and do not receive Resets.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 703) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 704) int dccp_invalid_packet(struct sk_buff *skb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 705) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 706) const struct dccp_hdr *dh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 707) unsigned int cscov;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 708) u8 dccph_doff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 709)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 710) if (skb->pkt_type != PACKET_HOST)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 711) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 712)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 713) /* If the packet is shorter than 12 bytes, drop packet and return */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 714) if (!pskb_may_pull(skb, sizeof(struct dccp_hdr))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 715) DCCP_WARN("pskb_may_pull failed\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 716) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 717) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 718)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 719) dh = dccp_hdr(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 720)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 721) /* If P.type is not understood, drop packet and return */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 722) if (dh->dccph_type >= DCCP_PKT_INVALID) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 723) DCCP_WARN("invalid packet type\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 724) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 725) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 726)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 727) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 728) * If P.Data Offset is too small for packet type, drop packet and return
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 729) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 730) dccph_doff = dh->dccph_doff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 731) if (dccph_doff < dccp_hdr_len(skb) / sizeof(u32)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 732) DCCP_WARN("P.Data Offset(%u) too small\n", dccph_doff);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 733) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 734) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 735) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 736) * If P.Data Offset is too large for packet, drop packet and return
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 737) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 738) if (!pskb_may_pull(skb, dccph_doff * sizeof(u32))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 739) DCCP_WARN("P.Data Offset(%u) too large\n", dccph_doff);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 740) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 741) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 742) dh = dccp_hdr(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 743) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 744) * If P.type is not Data, Ack, or DataAck and P.X == 0 (the packet
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 745) * has short sequence numbers), drop packet and return
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 746) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 747) if ((dh->dccph_type < DCCP_PKT_DATA ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 748) dh->dccph_type > DCCP_PKT_DATAACK) && dh->dccph_x == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 749) DCCP_WARN("P.type (%s) not Data || [Data]Ack, while P.X == 0\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 750) dccp_packet_name(dh->dccph_type));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 751) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 752) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 753)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 754) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 755) * If P.CsCov is too large for the packet size, drop packet and return.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 756) * This must come _before_ checksumming (not as RFC 4340 suggests).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 757) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 758) cscov = dccp_csum_coverage(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 759) if (cscov > skb->len) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 760) DCCP_WARN("P.CsCov %u exceeds packet length %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 761) dh->dccph_cscov, skb->len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 762) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 763) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 764)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 765) /* If header checksum is incorrect, drop packet and return.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 766) * (This step is completed in the AF-dependent functions.) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 767) skb->csum = skb_checksum(skb, 0, cscov, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 768)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 769) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 770) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 771) EXPORT_SYMBOL_GPL(dccp_invalid_packet);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 772)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 773) /* this is called when real data arrives */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 774) static int dccp_v4_rcv(struct sk_buff *skb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 775) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 776) const struct dccp_hdr *dh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 777) const struct iphdr *iph;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 778) bool refcounted;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 779) struct sock *sk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 780) int min_cov;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 781)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 782) /* Step 1: Check header basics */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 783)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 784) if (dccp_invalid_packet(skb))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 785) goto discard_it;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 786)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 787) iph = ip_hdr(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 788) /* Step 1: If header checksum is incorrect, drop packet and return */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 789) if (dccp_v4_csum_finish(skb, iph->saddr, iph->daddr)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 790) DCCP_WARN("dropped packet with invalid checksum\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 791) goto discard_it;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 792) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 793)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 794) dh = dccp_hdr(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 795)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 796) DCCP_SKB_CB(skb)->dccpd_seq = dccp_hdr_seq(dh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 797) DCCP_SKB_CB(skb)->dccpd_type = dh->dccph_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 798)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 799) dccp_pr_debug("%8.8s src=%pI4@%-5d dst=%pI4@%-5d seq=%llu",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 800) dccp_packet_name(dh->dccph_type),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 801) &iph->saddr, ntohs(dh->dccph_sport),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 802) &iph->daddr, ntohs(dh->dccph_dport),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 803) (unsigned long long) DCCP_SKB_CB(skb)->dccpd_seq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 804)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 805) if (dccp_packet_without_ack(skb)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 806) DCCP_SKB_CB(skb)->dccpd_ack_seq = DCCP_PKT_WITHOUT_ACK_SEQ;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 807) dccp_pr_debug_cat("\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 808) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 809) DCCP_SKB_CB(skb)->dccpd_ack_seq = dccp_hdr_ack_seq(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 810) dccp_pr_debug_cat(", ack=%llu\n", (unsigned long long)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 811) DCCP_SKB_CB(skb)->dccpd_ack_seq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 812) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 813)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 814) lookup:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 815) sk = __inet_lookup_skb(&dccp_hashinfo, skb, __dccp_hdr_len(dh),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 816) dh->dccph_sport, dh->dccph_dport, 0, &refcounted);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 817) if (!sk) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 818) dccp_pr_debug("failed to look up flow ID in table and "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 819) "get corresponding socket\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 820) goto no_dccp_socket;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 821) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 822)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 823) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 824) * Step 2:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 825) * ... or S.state == TIMEWAIT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 826) * Generate Reset(No Connection) unless P.type == Reset
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 827) * Drop packet and return
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 828) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 829) if (sk->sk_state == DCCP_TIME_WAIT) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 830) dccp_pr_debug("sk->sk_state == DCCP_TIME_WAIT: do_time_wait\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 831) inet_twsk_put(inet_twsk(sk));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 832) goto no_dccp_socket;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 833) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 834)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 835) if (sk->sk_state == DCCP_NEW_SYN_RECV) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 836) struct request_sock *req = inet_reqsk(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 837) struct sock *nsk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 838)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 839) sk = req->rsk_listener;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 840) if (unlikely(sk->sk_state != DCCP_LISTEN)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 841) inet_csk_reqsk_queue_drop_and_put(sk, req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 842) goto lookup;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 843) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 844) sock_hold(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 845) refcounted = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 846) nsk = dccp_check_req(sk, skb, req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 847) if (!nsk) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 848) reqsk_put(req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 849) goto discard_and_relse;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 850) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 851) if (nsk == sk) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 852) reqsk_put(req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 853) } else if (dccp_child_process(sk, nsk, skb)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 854) dccp_v4_ctl_send_reset(sk, skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 855) goto discard_and_relse;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 856) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 857) sock_put(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 858) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 859) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 860) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 861) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 862) * RFC 4340, sec. 9.2.1: Minimum Checksum Coverage
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 863) * o if MinCsCov = 0, only packets with CsCov = 0 are accepted
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 864) * o if MinCsCov > 0, also accept packets with CsCov >= MinCsCov
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 865) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 866) min_cov = dccp_sk(sk)->dccps_pcrlen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 867) if (dh->dccph_cscov && (min_cov == 0 || dh->dccph_cscov < min_cov)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 868) dccp_pr_debug("Packet CsCov %d does not satisfy MinCsCov %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 869) dh->dccph_cscov, min_cov);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 870) /* FIXME: "Such packets SHOULD be reported using Data Dropped
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 871) * options (Section 11.7) with Drop Code 0, Protocol
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 872) * Constraints." */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 873) goto discard_and_relse;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 874) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 875)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 876) if (!xfrm4_policy_check(sk, XFRM_POLICY_IN, skb))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 877) goto discard_and_relse;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 878) nf_reset_ct(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 879)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 880) return __sk_receive_skb(sk, skb, 1, dh->dccph_doff * 4, refcounted);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 881)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 882) no_dccp_socket:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 883) if (!xfrm4_policy_check(NULL, XFRM_POLICY_IN, skb))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 884) goto discard_it;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 885) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 886) * Step 2:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 887) * If no socket ...
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 888) * Generate Reset(No Connection) unless P.type == Reset
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 889) * Drop packet and return
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 890) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 891) if (dh->dccph_type != DCCP_PKT_RESET) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 892) DCCP_SKB_CB(skb)->dccpd_reset_code =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 893) DCCP_RESET_CODE_NO_CONNECTION;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 894) dccp_v4_ctl_send_reset(sk, skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 895) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 896)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 897) discard_it:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 898) kfree_skb(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 899) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 900)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 901) discard_and_relse:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 902) if (refcounted)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 903) sock_put(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 904) goto discard_it;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 905) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 906)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 907) static const struct inet_connection_sock_af_ops dccp_ipv4_af_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 908) .queue_xmit = ip_queue_xmit,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 909) .send_check = dccp_v4_send_check,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 910) .rebuild_header = inet_sk_rebuild_header,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 911) .conn_request = dccp_v4_conn_request,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 912) .syn_recv_sock = dccp_v4_request_recv_sock,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 913) .net_header_len = sizeof(struct iphdr),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 914) .setsockopt = ip_setsockopt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 915) .getsockopt = ip_getsockopt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 916) .addr2sockaddr = inet_csk_addr2sockaddr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 917) .sockaddr_len = sizeof(struct sockaddr_in),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 918) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 919)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 920) static int dccp_v4_init_sock(struct sock *sk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 921) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 922) static __u8 dccp_v4_ctl_sock_initialized;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 923) int err = dccp_init_sock(sk, dccp_v4_ctl_sock_initialized);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 924)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 925) if (err == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 926) if (unlikely(!dccp_v4_ctl_sock_initialized))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 927) dccp_v4_ctl_sock_initialized = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 928) inet_csk(sk)->icsk_af_ops = &dccp_ipv4_af_ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 929) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 930)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 931) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 932) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 933)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 934) static struct timewait_sock_ops dccp_timewait_sock_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 935) .twsk_obj_size = sizeof(struct inet_timewait_sock),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 936) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 937)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 938) static struct proto dccp_v4_prot = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 939) .name = "DCCP",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 940) .owner = THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 941) .close = dccp_close,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 942) .connect = dccp_v4_connect,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 943) .disconnect = dccp_disconnect,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 944) .ioctl = dccp_ioctl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 945) .init = dccp_v4_init_sock,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 946) .setsockopt = dccp_setsockopt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 947) .getsockopt = dccp_getsockopt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 948) .sendmsg = dccp_sendmsg,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 949) .recvmsg = dccp_recvmsg,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 950) .backlog_rcv = dccp_v4_do_rcv,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 951) .hash = inet_hash,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 952) .unhash = inet_unhash,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 953) .accept = inet_csk_accept,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 954) .get_port = inet_csk_get_port,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 955) .shutdown = dccp_shutdown,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 956) .destroy = dccp_destroy_sock,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 957) .orphan_count = &dccp_orphan_count,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 958) .max_header = MAX_DCCP_HEADER,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 959) .obj_size = sizeof(struct dccp_sock),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 960) .slab_flags = SLAB_TYPESAFE_BY_RCU,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 961) .rsk_prot = &dccp_request_sock_ops,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 962) .twsk_prot = &dccp_timewait_sock_ops,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 963) .h.hashinfo = &dccp_hashinfo,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 964) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 965)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 966) static const struct net_protocol dccp_v4_protocol = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 967) .handler = dccp_v4_rcv,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 968) .err_handler = dccp_v4_err,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 969) .no_policy = 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 970) .netns_ok = 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 971) .icmp_strict_tag_validation = 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 972) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 973)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 974) static const struct proto_ops inet_dccp_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 975) .family = PF_INET,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 976) .owner = THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 977) .release = inet_release,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 978) .bind = inet_bind,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 979) .connect = inet_stream_connect,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 980) .socketpair = sock_no_socketpair,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 981) .accept = inet_accept,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 982) .getname = inet_getname,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 983) /* FIXME: work on tcp_poll to rename it to inet_csk_poll */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 984) .poll = dccp_poll,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 985) .ioctl = inet_ioctl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 986) .gettstamp = sock_gettstamp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 987) /* FIXME: work on inet_listen to rename it to sock_common_listen */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 988) .listen = inet_dccp_listen,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 989) .shutdown = inet_shutdown,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 990) .setsockopt = sock_common_setsockopt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 991) .getsockopt = sock_common_getsockopt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 992) .sendmsg = inet_sendmsg,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 993) .recvmsg = sock_common_recvmsg,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 994) .mmap = sock_no_mmap,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 995) .sendpage = sock_no_sendpage,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 996) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 997)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 998) static struct inet_protosw dccp_v4_protosw = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 999) .type = SOCK_DCCP,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1000) .protocol = IPPROTO_DCCP,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1001) .prot = &dccp_v4_prot,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1002) .ops = &inet_dccp_ops,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1003) .flags = INET_PROTOSW_ICSK,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1004) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1005)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1006) static int __net_init dccp_v4_init_net(struct net *net)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1007) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1008) if (dccp_hashinfo.bhash == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1009) return -ESOCKTNOSUPPORT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1010)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1011) return inet_ctl_sock_create(&net->dccp.v4_ctl_sk, PF_INET,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1012) SOCK_DCCP, IPPROTO_DCCP, net);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1013) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1014)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1015) static void __net_exit dccp_v4_exit_net(struct net *net)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1016) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1017) inet_ctl_sock_destroy(net->dccp.v4_ctl_sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1018) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1019)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1020) static void __net_exit dccp_v4_exit_batch(struct list_head *net_exit_list)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1021) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1022) inet_twsk_purge(&dccp_hashinfo, AF_INET);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1023) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1024)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1025) static struct pernet_operations dccp_v4_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1026) .init = dccp_v4_init_net,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1027) .exit = dccp_v4_exit_net,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1028) .exit_batch = dccp_v4_exit_batch,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1029) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1030)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1031) static int __init dccp_v4_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1032) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1033) int err = proto_register(&dccp_v4_prot, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1034)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1035) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1036) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1037)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1038) inet_register_protosw(&dccp_v4_protosw);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1039)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1040) err = register_pernet_subsys(&dccp_v4_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1041) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1042) goto out_destroy_ctl_sock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1043)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1044) err = inet_add_protocol(&dccp_v4_protocol, IPPROTO_DCCP);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1045) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1046) goto out_proto_unregister;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1047)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1048) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1049) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1050) out_proto_unregister:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1051) unregister_pernet_subsys(&dccp_v4_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1052) out_destroy_ctl_sock:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1053) inet_unregister_protosw(&dccp_v4_protosw);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1054) proto_unregister(&dccp_v4_prot);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1055) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1056) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1057)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1058) static void __exit dccp_v4_exit(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1059) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1060) inet_del_protocol(&dccp_v4_protocol, IPPROTO_DCCP);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1061) unregister_pernet_subsys(&dccp_v4_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1062) inet_unregister_protosw(&dccp_v4_protosw);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1063) proto_unregister(&dccp_v4_prot);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1064) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1065)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1066) module_init(dccp_v4_init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1067) module_exit(dccp_v4_exit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1068)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1069) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1070) * __stringify doesn't likes enums, so use SOCK_DCCP (6) and IPPROTO_DCCP (33)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1071) * values directly, Also cover the case where the protocol is not specified,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1072) * i.e. net-pf-PF_INET-proto-0-type-SOCK_DCCP
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1073) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1074) MODULE_ALIAS_NET_PF_PROTO_TYPE(PF_INET, 33, 6);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1075) MODULE_ALIAS_NET_PF_PROTO_TYPE(PF_INET, 0, 6);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1076) MODULE_LICENSE("GPL");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1077) MODULE_AUTHOR("Arnaldo Carvalho de Melo <acme@mandriva.com>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1078) MODULE_DESCRIPTION("DCCP - Datagram Congestion Controlled Protocol");