0001
0002 #ifndef _DCCP_H
0003 #define _DCCP_H
0004
0005
0006
0007
0008
0009
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
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
0057
0058
0059
0060
0061
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
0069 #define DCCP_FEATNEG_OVERHEAD (32 * sizeof(uint32_t))
0070
0071 #define DCCP_TIMEWAIT_LEN (60 * HZ)
0072
0073
0074
0075 #define DCCP_TIMEOUT_INIT ((unsigned int)(3 * HZ))
0076
0077
0078
0079
0080
0081
0082
0083
0084 #define DCCP_RTO_MAX ((unsigned int)(64 * HZ))
0085
0086
0087
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
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
0102
0103 #define INT48_MIN 0x800000000000LL
0104 #define UINT48_MAX 0xFFFFFFFFFFFFLL
0105 #define COMPLEMENT48(x) (0x1000000000000LL - (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
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
0125 static inline int before48(const u64 seq1, const u64 seq2)
0126 {
0127 return (s64)((seq2 << 16) - (seq1 << 16)) > 0;
0128 }
0129
0130
0131 #define after48(seq1, seq2) before48(seq2, seq1)
0132
0133
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
0141
0142
0143
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
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,
0166 DCCP_MIB_ESTABRESETS,
0167 DCCP_MIB_CURRESTAB,
0168 DCCP_MIB_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
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
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
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
0323
0324
0325
0326
0327
0328
0329
0330
0331
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
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
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
0408 dp->dccps_swl = SUB48(ADD48(dp->dccps_gsr, 1), dp->dccps_r_seq_win / 4);
0409
0410
0411
0412
0413
0414
0415
0416
0417
0418
0419
0420
0421
0422
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
0435 dp->dccps_awl = SUB48(ADD48(dp->dccps_gss, 1), dp->dccps_l_seq_win);
0436
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