Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0-only */
0002 #ifndef _DCCP_H
0003 #define _DCCP_H
0004 /*
0005  *  net/dccp/dccp.h
0006  *
0007  *  An implementation of the DCCP protocol
0008  *  Copyright (c) 2005 Arnaldo Carvalho de Melo <acme@conectiva.com.br>
0009  *  Copyright (c) 2005-6 Ian McDonald <ian.mcdonald@jandi.co.nz>
0010  */
0011 
0012 #include <linux/dccp.h>
0013 #include <linux/ktime.h>
0014 #include <net/snmp.h>
0015 #include <net/sock.h>
0016 #include <net/tcp.h>
0017 #include "ackvec.h"
0018 
0019 /*
0020  *  DCCP - specific warning and debugging macros.
0021  */
0022 #define DCCP_WARN(fmt, ...)                     \
0023     net_warn_ratelimited("%s: " fmt, __func__, ##__VA_ARGS__)
0024 #define DCCP_CRIT(fmt, a...) printk(KERN_CRIT fmt " at %s:%d/%s()\n", ##a, \
0025                      __FILE__, __LINE__, __func__)
0026 #define DCCP_BUG(a...)       do { DCCP_CRIT("BUG: " a); dump_stack(); } while(0)
0027 #define DCCP_BUG_ON(cond)    do { if (unlikely((cond) != 0))           \
0028                      DCCP_BUG("\"%s\" holds (exception!)", \
0029                           __stringify(cond));          \
0030                  } while (0)
0031 
0032 #define DCCP_PRINTK(enable, fmt, args...)   do { if (enable)         \
0033                             printk(fmt, ##args); \
0034                         } while(0)
0035 #define DCCP_PR_DEBUG(enable, fmt, a...)    DCCP_PRINTK(enable, KERN_DEBUG \
0036                           "%s: " fmt, __func__, ##a)
0037 
0038 #ifdef CONFIG_IP_DCCP_DEBUG
0039 extern bool dccp_debug;
0040 #define dccp_pr_debug(format, a...)   DCCP_PR_DEBUG(dccp_debug, format, ##a)
0041 #define dccp_pr_debug_cat(format, a...)   DCCP_PRINTK(dccp_debug, format, ##a)
0042 #define dccp_debug(fmt, a...)         dccp_pr_debug_cat(KERN_DEBUG fmt, ##a)
0043 #else
0044 #define dccp_pr_debug(format, a...)   do {} while (0)
0045 #define dccp_pr_debug_cat(format, a...)   do {} while (0)
0046 #define dccp_debug(format, a...)      do {} while (0)
0047 #endif
0048 
0049 extern struct inet_hashinfo dccp_hashinfo;
0050 
0051 DECLARE_PER_CPU(unsigned int, dccp_orphan_count);
0052 
0053 void dccp_time_wait(struct sock *sk, int state, int timeo);
0054 
0055 /*
0056  *  Set safe upper bounds for header and option length. Since Data Offset is 8
0057  *  bits (RFC 4340, sec. 5.1), the total header length can never be more than
0058  *  4 * 255 = 1020 bytes. The largest possible header length is 28 bytes (X=1):
0059  *    - DCCP-Response with ACK Subheader and 4 bytes of Service code      OR
0060  *    - DCCP-Reset    with ACK Subheader and 4 bytes of Reset Code fields
0061  *  Hence a safe upper bound for the maximum option length is 1020-28 = 992
0062  */
0063 #define MAX_DCCP_SPECIFIC_HEADER (255 * sizeof(uint32_t))
0064 #define DCCP_MAX_PACKET_HDR 28
0065 #define DCCP_MAX_OPT_LEN (MAX_DCCP_SPECIFIC_HEADER - DCCP_MAX_PACKET_HDR)
0066 #define MAX_DCCP_HEADER (MAX_DCCP_SPECIFIC_HEADER + MAX_HEADER)
0067 
0068 /* Upper bound for initial feature-negotiation overhead (padded to 32 bits) */
0069 #define DCCP_FEATNEG_OVERHEAD    (32 * sizeof(uint32_t))
0070 
0071 #define DCCP_TIMEWAIT_LEN (60 * HZ) /* how long to wait to destroy TIME-WAIT
0072                      * state, about 60 seconds */
0073 
0074 /* RFC 1122, 4.2.3.1 initial RTO value */
0075 #define DCCP_TIMEOUT_INIT ((unsigned int)(3 * HZ))
0076 
0077 /*
0078  * The maximum back-off value for retransmissions. This is needed for
0079  *  - retransmitting client-Requests (sec. 8.1.1),
0080  *  - retransmitting Close/CloseReq when closing (sec. 8.3),
0081  *  - feature-negotiation retransmission (sec. 6.6.3),
0082  *  - Acks in client-PARTOPEN state (sec. 8.1.5).
0083  */
0084 #define DCCP_RTO_MAX ((unsigned int)(64 * HZ))
0085 
0086 /*
0087  * RTT sampling: sanity bounds and fallback RTT value from RFC 4340, section 3.4
0088  */
0089 #define DCCP_SANE_RTT_MIN   100
0090 #define DCCP_FALLBACK_RTT   (USEC_PER_SEC / 5)
0091 #define DCCP_SANE_RTT_MAX   (3 * USEC_PER_SEC)
0092 
0093 /* sysctl variables for DCCP */
0094 extern int  sysctl_dccp_request_retries;
0095 extern int  sysctl_dccp_retries1;
0096 extern int  sysctl_dccp_retries2;
0097 extern int  sysctl_dccp_tx_qlen;
0098 extern int  sysctl_dccp_sync_ratelimit;
0099 
0100 /*
0101  *  48-bit sequence number arithmetic (signed and unsigned)
0102  */
0103 #define INT48_MIN     0x800000000000LL      /* 2^47     */
0104 #define UINT48_MAX    0xFFFFFFFFFFFFLL      /* 2^48 - 1 */
0105 #define COMPLEMENT48(x)  (0x1000000000000LL - (x))  /* 2^48 - x */
0106 #define TO_SIGNED48(x)   (((x) < INT48_MIN)? (x) : -COMPLEMENT48( (x)))
0107 #define TO_UNSIGNED48(x) (((x) >= 0)?        (x) :  COMPLEMENT48(-(x)))
0108 #define ADD48(a, b)  (((a) + (b)) & UINT48_MAX)
0109 #define SUB48(a, b)  ADD48((a), COMPLEMENT48(b))
0110 
0111 static inline void dccp_inc_seqno(u64 *seqno)
0112 {
0113     *seqno = ADD48(*seqno, 1);
0114 }
0115 
0116 /* signed mod-2^48 distance: pos. if seqno1 < seqno2, neg. if seqno1 > seqno2 */
0117 static inline s64 dccp_delta_seqno(const u64 seqno1, const u64 seqno2)
0118 {
0119     u64 delta = SUB48(seqno2, seqno1);
0120 
0121     return TO_SIGNED48(delta);
0122 }
0123 
0124 /* is seq1 < seq2 ? */
0125 static inline int before48(const u64 seq1, const u64 seq2)
0126 {
0127     return (s64)((seq2 << 16) - (seq1 << 16)) > 0;
0128 }
0129 
0130 /* is seq1 > seq2 ? */
0131 #define after48(seq1, seq2) before48(seq2, seq1)
0132 
0133 /* is seq2 <= seq1 <= seq3 ? */
0134 static inline int between48(const u64 seq1, const u64 seq2, const u64 seq3)
0135 {
0136     return (seq3 << 16) - (seq2 << 16) >= (seq1 << 16) - (seq2 << 16);
0137 }
0138 
0139 /**
0140  * dccp_loss_count - Approximate the number of lost data packets in a burst loss
0141  * @s1:  last known sequence number before the loss ('hole')
0142  * @s2:  first sequence number seen after the 'hole'
0143  * @ndp: NDP count on packet with sequence number @s2
0144  */
0145 static inline u64 dccp_loss_count(const u64 s1, const u64 s2, const u64 ndp)
0146 {
0147     s64 delta = dccp_delta_seqno(s1, s2);
0148 
0149     WARN_ON(delta < 0);
0150     delta -= ndp + 1;
0151 
0152     return delta > 0 ? delta : 0;
0153 }
0154 
0155 /**
0156  * dccp_loss_free - Evaluate condition for data loss from RFC 4340, 7.7.1
0157  */
0158 static inline bool dccp_loss_free(const u64 s1, const u64 s2, const u64 ndp)
0159 {
0160     return dccp_loss_count(s1, s2, ndp) == 0;
0161 }
0162 
0163 enum {
0164     DCCP_MIB_NUM = 0,
0165     DCCP_MIB_ACTIVEOPENS,           /* ActiveOpens */
0166     DCCP_MIB_ESTABRESETS,           /* EstabResets */
0167     DCCP_MIB_CURRESTAB,         /* CurrEstab */
0168     DCCP_MIB_OUTSEGS,           /* OutSegs */
0169     DCCP_MIB_OUTRSTS,
0170     DCCP_MIB_ABORTONTIMEOUT,
0171     DCCP_MIB_TIMEOUTS,
0172     DCCP_MIB_ABORTFAILED,
0173     DCCP_MIB_PASSIVEOPENS,
0174     DCCP_MIB_ATTEMPTFAILS,
0175     DCCP_MIB_OUTDATAGRAMS,
0176     DCCP_MIB_INERRS,
0177     DCCP_MIB_OPTMANDATORYERROR,
0178     DCCP_MIB_INVALIDOPT,
0179     __DCCP_MIB_MAX
0180 };
0181 
0182 #define DCCP_MIB_MAX    __DCCP_MIB_MAX
0183 struct dccp_mib {
0184     unsigned long   mibs[DCCP_MIB_MAX];
0185 };
0186 
0187 DECLARE_SNMP_STAT(struct dccp_mib, dccp_statistics);
0188 #define DCCP_INC_STATS(field)   SNMP_INC_STATS(dccp_statistics, field)
0189 #define __DCCP_INC_STATS(field) __SNMP_INC_STATS(dccp_statistics, field)
0190 #define DCCP_DEC_STATS(field)   SNMP_DEC_STATS(dccp_statistics, field)
0191 
0192 /*
0193  *  Checksumming routines
0194  */
0195 static inline unsigned int dccp_csum_coverage(const struct sk_buff *skb)
0196 {
0197     const struct dccp_hdr* dh = dccp_hdr(skb);
0198 
0199     if (dh->dccph_cscov == 0)
0200         return skb->len;
0201     return (dh->dccph_doff + dh->dccph_cscov - 1) * sizeof(u32);
0202 }
0203 
0204 static inline void dccp_csum_outgoing(struct sk_buff *skb)
0205 {
0206     unsigned int cov = dccp_csum_coverage(skb);
0207 
0208     if (cov >= skb->len)
0209         dccp_hdr(skb)->dccph_cscov = 0;
0210 
0211     skb->csum = skb_checksum(skb, 0, (cov > skb->len)? skb->len : cov, 0);
0212 }
0213 
0214 void dccp_v4_send_check(struct sock *sk, struct sk_buff *skb);
0215 
0216 int dccp_retransmit_skb(struct sock *sk);
0217 
0218 void dccp_send_ack(struct sock *sk);
0219 void dccp_reqsk_send_ack(const struct sock *sk, struct sk_buff *skb,
0220              struct request_sock *rsk);
0221 
0222 void dccp_send_sync(struct sock *sk, const u64 seq,
0223             const enum dccp_pkt_type pkt_type);
0224 
0225 /*
0226  * TX Packet Dequeueing Interface
0227  */
0228 void dccp_qpolicy_push(struct sock *sk, struct sk_buff *skb);
0229 bool dccp_qpolicy_full(struct sock *sk);
0230 void dccp_qpolicy_drop(struct sock *sk, struct sk_buff *skb);
0231 struct sk_buff *dccp_qpolicy_top(struct sock *sk);
0232 struct sk_buff *dccp_qpolicy_pop(struct sock *sk);
0233 bool dccp_qpolicy_param_ok(struct sock *sk, __be32 param);
0234 
0235 /*
0236  * TX Packet Output and TX Timers
0237  */
0238 void dccp_write_xmit(struct sock *sk);
0239 void dccp_write_space(struct sock *sk);
0240 void dccp_flush_write_queue(struct sock *sk, long *time_budget);
0241 
0242 void dccp_init_xmit_timers(struct sock *sk);
0243 static inline void dccp_clear_xmit_timers(struct sock *sk)
0244 {
0245     inet_csk_clear_xmit_timers(sk);
0246 }
0247 
0248 unsigned int dccp_sync_mss(struct sock *sk, u32 pmtu);
0249 
0250 const char *dccp_packet_name(const int type);
0251 
0252 void dccp_set_state(struct sock *sk, const int state);
0253 void dccp_done(struct sock *sk);
0254 
0255 int dccp_reqsk_init(struct request_sock *rq, struct dccp_sock const *dp,
0256             struct sk_buff const *skb);
0257 
0258 int dccp_v4_conn_request(struct sock *sk, struct sk_buff *skb);
0259 
0260 struct sock *dccp_create_openreq_child(const struct sock *sk,
0261                        const struct request_sock *req,
0262                        const struct sk_buff *skb);
0263 
0264 int dccp_v4_do_rcv(struct sock *sk, struct sk_buff *skb);
0265 
0266 struct sock *dccp_v4_request_recv_sock(const struct sock *sk, struct sk_buff *skb,
0267                        struct request_sock *req,
0268                        struct dst_entry *dst,
0269                        struct request_sock *req_unhash,
0270                        bool *own_req);
0271 struct sock *dccp_check_req(struct sock *sk, struct sk_buff *skb,
0272                 struct request_sock *req);
0273 
0274 int dccp_child_process(struct sock *parent, struct sock *child,
0275                struct sk_buff *skb);
0276 int dccp_rcv_state_process(struct sock *sk, struct sk_buff *skb,
0277                struct dccp_hdr *dh, unsigned int len);
0278 int dccp_rcv_established(struct sock *sk, struct sk_buff *skb,
0279              const struct dccp_hdr *dh, const unsigned int len);
0280 
0281 int dccp_init_sock(struct sock *sk, const __u8 ctl_sock_initialized);
0282 void dccp_destroy_sock(struct sock *sk);
0283 
0284 void dccp_close(struct sock *sk, long timeout);
0285 struct sk_buff *dccp_make_response(const struct sock *sk, struct dst_entry *dst,
0286                    struct request_sock *req);
0287 
0288 int dccp_connect(struct sock *sk);
0289 int dccp_disconnect(struct sock *sk, int flags);
0290 int dccp_getsockopt(struct sock *sk, int level, int optname,
0291             char __user *optval, int __user *optlen);
0292 int dccp_setsockopt(struct sock *sk, int level, int optname,
0293             sockptr_t optval, unsigned int optlen);
0294 int dccp_ioctl(struct sock *sk, int cmd, unsigned long arg);
0295 int dccp_sendmsg(struct sock *sk, struct msghdr *msg, size_t size);
0296 int dccp_recvmsg(struct sock *sk, struct msghdr *msg, size_t len, int flags,
0297          int *addr_len);
0298 void dccp_shutdown(struct sock *sk, int how);
0299 int inet_dccp_listen(struct socket *sock, int backlog);
0300 __poll_t dccp_poll(struct file *file, struct socket *sock,
0301                poll_table *wait);
0302 int dccp_v4_connect(struct sock *sk, struct sockaddr *uaddr, int addr_len);
0303 void dccp_req_err(struct sock *sk, u64 seq);
0304 
0305 struct sk_buff *dccp_ctl_make_reset(struct sock *sk, struct sk_buff *skb);
0306 int dccp_send_reset(struct sock *sk, enum dccp_reset_codes code);
0307 void dccp_send_close(struct sock *sk, const int active);
0308 int dccp_invalid_packet(struct sk_buff *skb);
0309 u32 dccp_sample_rtt(struct sock *sk, long delta);
0310 
0311 static inline bool dccp_bad_service_code(const struct sock *sk,
0312                     const __be32 service)
0313 {
0314     const struct dccp_sock *dp = dccp_sk(sk);
0315 
0316     if (dp->dccps_service == service)
0317         return false;
0318     return !dccp_list_has_service(dp->dccps_service_list, service);
0319 }
0320 
0321 /**
0322  * dccp_skb_cb  -  DCCP per-packet control information
0323  * @dccpd_type: one of %dccp_pkt_type (or unknown)
0324  * @dccpd_ccval: CCVal field (5.1), see e.g. RFC 4342, 8.1
0325  * @dccpd_reset_code: one of %dccp_reset_codes
0326  * @dccpd_reset_data: Data1..3 fields (depend on @dccpd_reset_code)
0327  * @dccpd_opt_len: total length of all options (5.8) in the packet
0328  * @dccpd_seq: sequence number
0329  * @dccpd_ack_seq: acknowledgment number subheader field value
0330  *
0331  * This is used for transmission as well as for reception.
0332  */
0333 struct dccp_skb_cb {
0334     union {
0335         struct inet_skb_parm    h4;
0336 #if IS_ENABLED(CONFIG_IPV6)
0337         struct inet6_skb_parm   h6;
0338 #endif
0339     } header;
0340     __u8  dccpd_type:4;
0341     __u8  dccpd_ccval:4;
0342     __u8  dccpd_reset_code,
0343           dccpd_reset_data[3];
0344     __u16 dccpd_opt_len;
0345     __u64 dccpd_seq;
0346     __u64 dccpd_ack_seq;
0347 };
0348 
0349 #define DCCP_SKB_CB(__skb) ((struct dccp_skb_cb *)&((__skb)->cb[0]))
0350 
0351 /* RFC 4340, sec. 7.7 */
0352 static inline int dccp_non_data_packet(const struct sk_buff *skb)
0353 {
0354     const __u8 type = DCCP_SKB_CB(skb)->dccpd_type;
0355 
0356     return type == DCCP_PKT_ACK  ||
0357            type == DCCP_PKT_CLOSE    ||
0358            type == DCCP_PKT_CLOSEREQ ||
0359            type == DCCP_PKT_RESET    ||
0360            type == DCCP_PKT_SYNC     ||
0361            type == DCCP_PKT_SYNCACK;
0362 }
0363 
0364 /* RFC 4340, sec. 7.7 */
0365 static inline int dccp_data_packet(const struct sk_buff *skb)
0366 {
0367     const __u8 type = DCCP_SKB_CB(skb)->dccpd_type;
0368 
0369     return type == DCCP_PKT_DATA     ||
0370            type == DCCP_PKT_DATAACK  ||
0371            type == DCCP_PKT_REQUEST  ||
0372            type == DCCP_PKT_RESPONSE;
0373 }
0374 
0375 static inline int dccp_packet_without_ack(const struct sk_buff *skb)
0376 {
0377     const __u8 type = DCCP_SKB_CB(skb)->dccpd_type;
0378 
0379     return type == DCCP_PKT_DATA || type == DCCP_PKT_REQUEST;
0380 }
0381 
0382 #define DCCP_PKT_WITHOUT_ACK_SEQ (UINT48_MAX << 2)
0383 
0384 static inline void dccp_hdr_set_seq(struct dccp_hdr *dh, const u64 gss)
0385 {
0386     struct dccp_hdr_ext *dhx = (struct dccp_hdr_ext *)((void *)dh +
0387                                sizeof(*dh));
0388     dh->dccph_seq2 = 0;
0389     dh->dccph_seq = htons((gss >> 32) & 0xfffff);
0390     dhx->dccph_seq_low = htonl(gss & 0xffffffff);
0391 }
0392 
0393 static inline void dccp_hdr_set_ack(struct dccp_hdr_ack_bits *dhack,
0394                     const u64 gsr)
0395 {
0396     dhack->dccph_reserved1 = 0;
0397     dhack->dccph_ack_nr_high = htons(gsr >> 32);
0398     dhack->dccph_ack_nr_low  = htonl(gsr & 0xffffffff);
0399 }
0400 
0401 static inline void dccp_update_gsr(struct sock *sk, u64 seq)
0402 {
0403     struct dccp_sock *dp = dccp_sk(sk);
0404 
0405     if (after48(seq, dp->dccps_gsr))
0406         dp->dccps_gsr = seq;
0407     /* Sequence validity window depends on remote Sequence Window (7.5.1) */
0408     dp->dccps_swl = SUB48(ADD48(dp->dccps_gsr, 1), dp->dccps_r_seq_win / 4);
0409     /*
0410      * Adjust SWL so that it is not below ISR. In contrast to RFC 4340,
0411      * 7.5.1 we perform this check beyond the initial handshake: W/W' are
0412      * always > 32, so for the first W/W' packets in the lifetime of a
0413      * connection we always have to adjust SWL.
0414      * A second reason why we are doing this is that the window depends on
0415      * the feature-remote value of Sequence Window: nothing stops the peer
0416      * from updating this value while we are busy adjusting SWL for the
0417      * first W packets (we would have to count from scratch again then).
0418      * Therefore it is safer to always make sure that the Sequence Window
0419      * is not artificially extended by a peer who grows SWL downwards by
0420      * continually updating the feature-remote Sequence-Window.
0421      * If sequence numbers wrap it is bad luck. But that will take a while
0422      * (48 bit), and this measure prevents Sequence-number attacks.
0423      */
0424     if (before48(dp->dccps_swl, dp->dccps_isr))
0425         dp->dccps_swl = dp->dccps_isr;
0426     dp->dccps_swh = ADD48(dp->dccps_gsr, (3 * dp->dccps_r_seq_win) / 4);
0427 }
0428 
0429 static inline void dccp_update_gss(struct sock *sk, u64 seq)
0430 {
0431     struct dccp_sock *dp = dccp_sk(sk);
0432 
0433     dp->dccps_gss = seq;
0434     /* Ack validity window depends on local Sequence Window value (7.5.1) */
0435     dp->dccps_awl = SUB48(ADD48(dp->dccps_gss, 1), dp->dccps_l_seq_win);
0436     /* Adjust AWL so that it is not below ISS - see comment above for SWL */
0437     if (before48(dp->dccps_awl, dp->dccps_iss))
0438         dp->dccps_awl = dp->dccps_iss;
0439     dp->dccps_awh = dp->dccps_gss;
0440 }
0441 
0442 static inline int dccp_ackvec_pending(const struct sock *sk)
0443 {
0444     return dccp_sk(sk)->dccps_hc_rx_ackvec != NULL &&
0445            !dccp_ackvec_is_empty(dccp_sk(sk)->dccps_hc_rx_ackvec);
0446 }
0447 
0448 static inline int dccp_ack_pending(const struct sock *sk)
0449 {
0450     return dccp_ackvec_pending(sk) || inet_csk_ack_scheduled(sk);
0451 }
0452 
0453 int dccp_feat_signal_nn_change(struct sock *sk, u8 feat, u64 nn_val);
0454 int dccp_feat_finalise_settings(struct dccp_sock *dp);
0455 int dccp_feat_server_ccid_dependencies(struct dccp_request_sock *dreq);
0456 int dccp_feat_insert_opts(struct dccp_sock*, struct dccp_request_sock*,
0457               struct sk_buff *skb);
0458 int dccp_feat_activate_values(struct sock *sk, struct list_head *fn);
0459 void dccp_feat_list_purge(struct list_head *fn_list);
0460 
0461 int dccp_insert_options(struct sock *sk, struct sk_buff *skb);
0462 int dccp_insert_options_rsk(struct dccp_request_sock *, struct sk_buff *);
0463 u32 dccp_timestamp(void);
0464 void dccp_timestamping_init(void);
0465 int dccp_insert_option(struct sk_buff *skb, unsigned char option,
0466                const void *value, unsigned char len);
0467 
0468 #ifdef CONFIG_SYSCTL
0469 int dccp_sysctl_init(void);
0470 void dccp_sysctl_exit(void);
0471 #else
0472 static inline int dccp_sysctl_init(void)
0473 {
0474     return 0;
0475 }
0476 
0477 static inline void dccp_sysctl_exit(void)
0478 {
0479 }
0480 #endif
0481 
0482 #endif /* _DCCP_H */