dccp.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483
  1. /* SPDX-License-Identifier: GPL-2.0-only */
  2. #ifndef _DCCP_H
  3. #define _DCCP_H
  4. /*
  5. * net/dccp/dccp.h
  6. *
  7. * An implementation of the DCCP protocol
  8. * Copyright (c) 2005 Arnaldo Carvalho de Melo <[email protected]>
  9. * Copyright (c) 2005-6 Ian McDonald <[email protected]>
  10. */
  11. #include <linux/dccp.h>
  12. #include <linux/ktime.h>
  13. #include <net/snmp.h>
  14. #include <net/sock.h>
  15. #include <net/tcp.h>
  16. #include "ackvec.h"
  17. /*
  18. * DCCP - specific warning and debugging macros.
  19. */
  20. #define DCCP_WARN(fmt, ...) \
  21. net_warn_ratelimited("%s: " fmt, __func__, ##__VA_ARGS__)
  22. #define DCCP_CRIT(fmt, a...) printk(KERN_CRIT fmt " at %s:%d/%s()\n", ##a, \
  23. __FILE__, __LINE__, __func__)
  24. #define DCCP_BUG(a...) do { DCCP_CRIT("BUG: " a); dump_stack(); } while(0)
  25. #define DCCP_BUG_ON(cond) do { if (unlikely((cond) != 0)) \
  26. DCCP_BUG("\"%s\" holds (exception!)", \
  27. __stringify(cond)); \
  28. } while (0)
  29. #define DCCP_PRINTK(enable, fmt, args...) do { if (enable) \
  30. printk(fmt, ##args); \
  31. } while(0)
  32. #define DCCP_PR_DEBUG(enable, fmt, a...) DCCP_PRINTK(enable, KERN_DEBUG \
  33. "%s: " fmt, __func__, ##a)
  34. #ifdef CONFIG_IP_DCCP_DEBUG
  35. extern bool dccp_debug;
  36. #define dccp_pr_debug(format, a...) DCCP_PR_DEBUG(dccp_debug, format, ##a)
  37. #define dccp_pr_debug_cat(format, a...) DCCP_PRINTK(dccp_debug, format, ##a)
  38. #define dccp_debug(fmt, a...) dccp_pr_debug_cat(KERN_DEBUG fmt, ##a)
  39. #else
  40. #define dccp_pr_debug(format, a...) do {} while (0)
  41. #define dccp_pr_debug_cat(format, a...) do {} while (0)
  42. #define dccp_debug(format, a...) do {} while (0)
  43. #endif
  44. extern struct inet_hashinfo dccp_hashinfo;
  45. DECLARE_PER_CPU(unsigned int, dccp_orphan_count);
  46. void dccp_time_wait(struct sock *sk, int state, int timeo);
  47. /*
  48. * Set safe upper bounds for header and option length. Since Data Offset is 8
  49. * bits (RFC 4340, sec. 5.1), the total header length can never be more than
  50. * 4 * 255 = 1020 bytes. The largest possible header length is 28 bytes (X=1):
  51. * - DCCP-Response with ACK Subheader and 4 bytes of Service code OR
  52. * - DCCP-Reset with ACK Subheader and 4 bytes of Reset Code fields
  53. * Hence a safe upper bound for the maximum option length is 1020-28 = 992
  54. */
  55. #define MAX_DCCP_SPECIFIC_HEADER (255 * sizeof(uint32_t))
  56. #define DCCP_MAX_PACKET_HDR 28
  57. #define DCCP_MAX_OPT_LEN (MAX_DCCP_SPECIFIC_HEADER - DCCP_MAX_PACKET_HDR)
  58. #define MAX_DCCP_HEADER (MAX_DCCP_SPECIFIC_HEADER + MAX_HEADER)
  59. /* Upper bound for initial feature-negotiation overhead (padded to 32 bits) */
  60. #define DCCP_FEATNEG_OVERHEAD (32 * sizeof(uint32_t))
  61. #define DCCP_TIMEWAIT_LEN (60 * HZ) /* how long to wait to destroy TIME-WAIT
  62. * state, about 60 seconds */
  63. /* RFC 1122, 4.2.3.1 initial RTO value */
  64. #define DCCP_TIMEOUT_INIT ((unsigned int)(3 * HZ))
  65. /*
  66. * The maximum back-off value for retransmissions. This is needed for
  67. * - retransmitting client-Requests (sec. 8.1.1),
  68. * - retransmitting Close/CloseReq when closing (sec. 8.3),
  69. * - feature-negotiation retransmission (sec. 6.6.3),
  70. * - Acks in client-PARTOPEN state (sec. 8.1.5).
  71. */
  72. #define DCCP_RTO_MAX ((unsigned int)(64 * HZ))
  73. /*
  74. * RTT sampling: sanity bounds and fallback RTT value from RFC 4340, section 3.4
  75. */
  76. #define DCCP_SANE_RTT_MIN 100
  77. #define DCCP_FALLBACK_RTT (USEC_PER_SEC / 5)
  78. #define DCCP_SANE_RTT_MAX (3 * USEC_PER_SEC)
  79. /* sysctl variables for DCCP */
  80. extern int sysctl_dccp_request_retries;
  81. extern int sysctl_dccp_retries1;
  82. extern int sysctl_dccp_retries2;
  83. extern int sysctl_dccp_tx_qlen;
  84. extern int sysctl_dccp_sync_ratelimit;
  85. /*
  86. * 48-bit sequence number arithmetic (signed and unsigned)
  87. */
  88. #define INT48_MIN 0x800000000000LL /* 2^47 */
  89. #define UINT48_MAX 0xFFFFFFFFFFFFLL /* 2^48 - 1 */
  90. #define COMPLEMENT48(x) (0x1000000000000LL - (x)) /* 2^48 - x */
  91. #define TO_SIGNED48(x) (((x) < INT48_MIN)? (x) : -COMPLEMENT48( (x)))
  92. #define TO_UNSIGNED48(x) (((x) >= 0)? (x) : COMPLEMENT48(-(x)))
  93. #define ADD48(a, b) (((a) + (b)) & UINT48_MAX)
  94. #define SUB48(a, b) ADD48((a), COMPLEMENT48(b))
  95. static inline void dccp_inc_seqno(u64 *seqno)
  96. {
  97. *seqno = ADD48(*seqno, 1);
  98. }
  99. /* signed mod-2^48 distance: pos. if seqno1 < seqno2, neg. if seqno1 > seqno2 */
  100. static inline s64 dccp_delta_seqno(const u64 seqno1, const u64 seqno2)
  101. {
  102. u64 delta = SUB48(seqno2, seqno1);
  103. return TO_SIGNED48(delta);
  104. }
  105. /* is seq1 < seq2 ? */
  106. static inline int before48(const u64 seq1, const u64 seq2)
  107. {
  108. return (s64)((seq2 << 16) - (seq1 << 16)) > 0;
  109. }
  110. /* is seq1 > seq2 ? */
  111. #define after48(seq1, seq2) before48(seq2, seq1)
  112. /* is seq2 <= seq1 <= seq3 ? */
  113. static inline int between48(const u64 seq1, const u64 seq2, const u64 seq3)
  114. {
  115. return (seq3 << 16) - (seq2 << 16) >= (seq1 << 16) - (seq2 << 16);
  116. }
  117. /**
  118. * dccp_loss_count - Approximate the number of lost data packets in a burst loss
  119. * @s1: last known sequence number before the loss ('hole')
  120. * @s2: first sequence number seen after the 'hole'
  121. * @ndp: NDP count on packet with sequence number @s2
  122. */
  123. static inline u64 dccp_loss_count(const u64 s1, const u64 s2, const u64 ndp)
  124. {
  125. s64 delta = dccp_delta_seqno(s1, s2);
  126. WARN_ON(delta < 0);
  127. delta -= ndp + 1;
  128. return delta > 0 ? delta : 0;
  129. }
  130. /**
  131. * dccp_loss_free - Evaluate condition for data loss from RFC 4340, 7.7.1
  132. */
  133. static inline bool dccp_loss_free(const u64 s1, const u64 s2, const u64 ndp)
  134. {
  135. return dccp_loss_count(s1, s2, ndp) == 0;
  136. }
  137. enum {
  138. DCCP_MIB_NUM = 0,
  139. DCCP_MIB_ACTIVEOPENS, /* ActiveOpens */
  140. DCCP_MIB_ESTABRESETS, /* EstabResets */
  141. DCCP_MIB_CURRESTAB, /* CurrEstab */
  142. DCCP_MIB_OUTSEGS, /* OutSegs */
  143. DCCP_MIB_OUTRSTS,
  144. DCCP_MIB_ABORTONTIMEOUT,
  145. DCCP_MIB_TIMEOUTS,
  146. DCCP_MIB_ABORTFAILED,
  147. DCCP_MIB_PASSIVEOPENS,
  148. DCCP_MIB_ATTEMPTFAILS,
  149. DCCP_MIB_OUTDATAGRAMS,
  150. DCCP_MIB_INERRS,
  151. DCCP_MIB_OPTMANDATORYERROR,
  152. DCCP_MIB_INVALIDOPT,
  153. __DCCP_MIB_MAX
  154. };
  155. #define DCCP_MIB_MAX __DCCP_MIB_MAX
  156. struct dccp_mib {
  157. unsigned long mibs[DCCP_MIB_MAX];
  158. };
  159. DECLARE_SNMP_STAT(struct dccp_mib, dccp_statistics);
  160. #define DCCP_INC_STATS(field) SNMP_INC_STATS(dccp_statistics, field)
  161. #define __DCCP_INC_STATS(field) __SNMP_INC_STATS(dccp_statistics, field)
  162. #define DCCP_DEC_STATS(field) SNMP_DEC_STATS(dccp_statistics, field)
  163. /*
  164. * Checksumming routines
  165. */
  166. static inline unsigned int dccp_csum_coverage(const struct sk_buff *skb)
  167. {
  168. const struct dccp_hdr* dh = dccp_hdr(skb);
  169. if (dh->dccph_cscov == 0)
  170. return skb->len;
  171. return (dh->dccph_doff + dh->dccph_cscov - 1) * sizeof(u32);
  172. }
  173. static inline void dccp_csum_outgoing(struct sk_buff *skb)
  174. {
  175. unsigned int cov = dccp_csum_coverage(skb);
  176. if (cov >= skb->len)
  177. dccp_hdr(skb)->dccph_cscov = 0;
  178. skb->csum = skb_checksum(skb, 0, (cov > skb->len)? skb->len : cov, 0);
  179. }
  180. void dccp_v4_send_check(struct sock *sk, struct sk_buff *skb);
  181. int dccp_retransmit_skb(struct sock *sk);
  182. void dccp_send_ack(struct sock *sk);
  183. void dccp_reqsk_send_ack(const struct sock *sk, struct sk_buff *skb,
  184. struct request_sock *rsk);
  185. void dccp_send_sync(struct sock *sk, const u64 seq,
  186. const enum dccp_pkt_type pkt_type);
  187. /*
  188. * TX Packet Dequeueing Interface
  189. */
  190. void dccp_qpolicy_push(struct sock *sk, struct sk_buff *skb);
  191. bool dccp_qpolicy_full(struct sock *sk);
  192. void dccp_qpolicy_drop(struct sock *sk, struct sk_buff *skb);
  193. struct sk_buff *dccp_qpolicy_top(struct sock *sk);
  194. struct sk_buff *dccp_qpolicy_pop(struct sock *sk);
  195. bool dccp_qpolicy_param_ok(struct sock *sk, __be32 param);
  196. /*
  197. * TX Packet Output and TX Timers
  198. */
  199. void dccp_write_xmit(struct sock *sk);
  200. void dccp_write_space(struct sock *sk);
  201. void dccp_flush_write_queue(struct sock *sk, long *time_budget);
  202. void dccp_init_xmit_timers(struct sock *sk);
  203. static inline void dccp_clear_xmit_timers(struct sock *sk)
  204. {
  205. inet_csk_clear_xmit_timers(sk);
  206. }
  207. unsigned int dccp_sync_mss(struct sock *sk, u32 pmtu);
  208. const char *dccp_packet_name(const int type);
  209. void dccp_set_state(struct sock *sk, const int state);
  210. void dccp_done(struct sock *sk);
  211. int dccp_reqsk_init(struct request_sock *rq, struct dccp_sock const *dp,
  212. struct sk_buff const *skb);
  213. int dccp_v4_conn_request(struct sock *sk, struct sk_buff *skb);
  214. struct sock *dccp_create_openreq_child(const struct sock *sk,
  215. const struct request_sock *req,
  216. const struct sk_buff *skb);
  217. int dccp_v4_do_rcv(struct sock *sk, struct sk_buff *skb);
  218. struct sock *dccp_v4_request_recv_sock(const struct sock *sk, struct sk_buff *skb,
  219. struct request_sock *req,
  220. struct dst_entry *dst,
  221. struct request_sock *req_unhash,
  222. bool *own_req);
  223. struct sock *dccp_check_req(struct sock *sk, struct sk_buff *skb,
  224. struct request_sock *req);
  225. int dccp_child_process(struct sock *parent, struct sock *child,
  226. struct sk_buff *skb);
  227. int dccp_rcv_state_process(struct sock *sk, struct sk_buff *skb,
  228. struct dccp_hdr *dh, unsigned int len);
  229. int dccp_rcv_established(struct sock *sk, struct sk_buff *skb,
  230. const struct dccp_hdr *dh, const unsigned int len);
  231. void dccp_destruct_common(struct sock *sk);
  232. int dccp_init_sock(struct sock *sk, const __u8 ctl_sock_initialized);
  233. void dccp_destroy_sock(struct sock *sk);
  234. void dccp_close(struct sock *sk, long timeout);
  235. struct sk_buff *dccp_make_response(const struct sock *sk, struct dst_entry *dst,
  236. struct request_sock *req);
  237. int dccp_connect(struct sock *sk);
  238. int dccp_disconnect(struct sock *sk, int flags);
  239. int dccp_getsockopt(struct sock *sk, int level, int optname,
  240. char __user *optval, int __user *optlen);
  241. int dccp_setsockopt(struct sock *sk, int level, int optname,
  242. sockptr_t optval, unsigned int optlen);
  243. int dccp_ioctl(struct sock *sk, int cmd, unsigned long arg);
  244. int dccp_sendmsg(struct sock *sk, struct msghdr *msg, size_t size);
  245. int dccp_recvmsg(struct sock *sk, struct msghdr *msg, size_t len, int flags,
  246. int *addr_len);
  247. void dccp_shutdown(struct sock *sk, int how);
  248. int inet_dccp_listen(struct socket *sock, int backlog);
  249. __poll_t dccp_poll(struct file *file, struct socket *sock,
  250. poll_table *wait);
  251. int dccp_v4_connect(struct sock *sk, struct sockaddr *uaddr, int addr_len);
  252. void dccp_req_err(struct sock *sk, u64 seq);
  253. struct sk_buff *dccp_ctl_make_reset(struct sock *sk, struct sk_buff *skb);
  254. int dccp_send_reset(struct sock *sk, enum dccp_reset_codes code);
  255. void dccp_send_close(struct sock *sk, const int active);
  256. int dccp_invalid_packet(struct sk_buff *skb);
  257. u32 dccp_sample_rtt(struct sock *sk, long delta);
  258. static inline bool dccp_bad_service_code(const struct sock *sk,
  259. const __be32 service)
  260. {
  261. const struct dccp_sock *dp = dccp_sk(sk);
  262. if (dp->dccps_service == service)
  263. return false;
  264. return !dccp_list_has_service(dp->dccps_service_list, service);
  265. }
  266. /**
  267. * dccp_skb_cb - DCCP per-packet control information
  268. * @dccpd_type: one of %dccp_pkt_type (or unknown)
  269. * @dccpd_ccval: CCVal field (5.1), see e.g. RFC 4342, 8.1
  270. * @dccpd_reset_code: one of %dccp_reset_codes
  271. * @dccpd_reset_data: Data1..3 fields (depend on @dccpd_reset_code)
  272. * @dccpd_opt_len: total length of all options (5.8) in the packet
  273. * @dccpd_seq: sequence number
  274. * @dccpd_ack_seq: acknowledgment number subheader field value
  275. *
  276. * This is used for transmission as well as for reception.
  277. */
  278. struct dccp_skb_cb {
  279. union {
  280. struct inet_skb_parm h4;
  281. #if IS_ENABLED(CONFIG_IPV6)
  282. struct inet6_skb_parm h6;
  283. #endif
  284. } header;
  285. __u8 dccpd_type:4;
  286. __u8 dccpd_ccval:4;
  287. __u8 dccpd_reset_code,
  288. dccpd_reset_data[3];
  289. __u16 dccpd_opt_len;
  290. __u64 dccpd_seq;
  291. __u64 dccpd_ack_seq;
  292. };
  293. #define DCCP_SKB_CB(__skb) ((struct dccp_skb_cb *)&((__skb)->cb[0]))
  294. /* RFC 4340, sec. 7.7 */
  295. static inline int dccp_non_data_packet(const struct sk_buff *skb)
  296. {
  297. const __u8 type = DCCP_SKB_CB(skb)->dccpd_type;
  298. return type == DCCP_PKT_ACK ||
  299. type == DCCP_PKT_CLOSE ||
  300. type == DCCP_PKT_CLOSEREQ ||
  301. type == DCCP_PKT_RESET ||
  302. type == DCCP_PKT_SYNC ||
  303. type == DCCP_PKT_SYNCACK;
  304. }
  305. /* RFC 4340, sec. 7.7 */
  306. static inline int dccp_data_packet(const struct sk_buff *skb)
  307. {
  308. const __u8 type = DCCP_SKB_CB(skb)->dccpd_type;
  309. return type == DCCP_PKT_DATA ||
  310. type == DCCP_PKT_DATAACK ||
  311. type == DCCP_PKT_REQUEST ||
  312. type == DCCP_PKT_RESPONSE;
  313. }
  314. static inline int dccp_packet_without_ack(const struct sk_buff *skb)
  315. {
  316. const __u8 type = DCCP_SKB_CB(skb)->dccpd_type;
  317. return type == DCCP_PKT_DATA || type == DCCP_PKT_REQUEST;
  318. }
  319. #define DCCP_PKT_WITHOUT_ACK_SEQ (UINT48_MAX << 2)
  320. static inline void dccp_hdr_set_seq(struct dccp_hdr *dh, const u64 gss)
  321. {
  322. struct dccp_hdr_ext *dhx = (struct dccp_hdr_ext *)((void *)dh +
  323. sizeof(*dh));
  324. dh->dccph_seq2 = 0;
  325. dh->dccph_seq = htons((gss >> 32) & 0xfffff);
  326. dhx->dccph_seq_low = htonl(gss & 0xffffffff);
  327. }
  328. static inline void dccp_hdr_set_ack(struct dccp_hdr_ack_bits *dhack,
  329. const u64 gsr)
  330. {
  331. dhack->dccph_reserved1 = 0;
  332. dhack->dccph_ack_nr_high = htons(gsr >> 32);
  333. dhack->dccph_ack_nr_low = htonl(gsr & 0xffffffff);
  334. }
  335. static inline void dccp_update_gsr(struct sock *sk, u64 seq)
  336. {
  337. struct dccp_sock *dp = dccp_sk(sk);
  338. if (after48(seq, dp->dccps_gsr))
  339. dp->dccps_gsr = seq;
  340. /* Sequence validity window depends on remote Sequence Window (7.5.1) */
  341. dp->dccps_swl = SUB48(ADD48(dp->dccps_gsr, 1), dp->dccps_r_seq_win / 4);
  342. /*
  343. * Adjust SWL so that it is not below ISR. In contrast to RFC 4340,
  344. * 7.5.1 we perform this check beyond the initial handshake: W/W' are
  345. * always > 32, so for the first W/W' packets in the lifetime of a
  346. * connection we always have to adjust SWL.
  347. * A second reason why we are doing this is that the window depends on
  348. * the feature-remote value of Sequence Window: nothing stops the peer
  349. * from updating this value while we are busy adjusting SWL for the
  350. * first W packets (we would have to count from scratch again then).
  351. * Therefore it is safer to always make sure that the Sequence Window
  352. * is not artificially extended by a peer who grows SWL downwards by
  353. * continually updating the feature-remote Sequence-Window.
  354. * If sequence numbers wrap it is bad luck. But that will take a while
  355. * (48 bit), and this measure prevents Sequence-number attacks.
  356. */
  357. if (before48(dp->dccps_swl, dp->dccps_isr))
  358. dp->dccps_swl = dp->dccps_isr;
  359. dp->dccps_swh = ADD48(dp->dccps_gsr, (3 * dp->dccps_r_seq_win) / 4);
  360. }
  361. static inline void dccp_update_gss(struct sock *sk, u64 seq)
  362. {
  363. struct dccp_sock *dp = dccp_sk(sk);
  364. dp->dccps_gss = seq;
  365. /* Ack validity window depends on local Sequence Window value (7.5.1) */
  366. dp->dccps_awl = SUB48(ADD48(dp->dccps_gss, 1), dp->dccps_l_seq_win);
  367. /* Adjust AWL so that it is not below ISS - see comment above for SWL */
  368. if (before48(dp->dccps_awl, dp->dccps_iss))
  369. dp->dccps_awl = dp->dccps_iss;
  370. dp->dccps_awh = dp->dccps_gss;
  371. }
  372. static inline int dccp_ackvec_pending(const struct sock *sk)
  373. {
  374. return dccp_sk(sk)->dccps_hc_rx_ackvec != NULL &&
  375. !dccp_ackvec_is_empty(dccp_sk(sk)->dccps_hc_rx_ackvec);
  376. }
  377. static inline int dccp_ack_pending(const struct sock *sk)
  378. {
  379. return dccp_ackvec_pending(sk) || inet_csk_ack_scheduled(sk);
  380. }
  381. int dccp_feat_signal_nn_change(struct sock *sk, u8 feat, u64 nn_val);
  382. int dccp_feat_finalise_settings(struct dccp_sock *dp);
  383. int dccp_feat_server_ccid_dependencies(struct dccp_request_sock *dreq);
  384. int dccp_feat_insert_opts(struct dccp_sock*, struct dccp_request_sock*,
  385. struct sk_buff *skb);
  386. int dccp_feat_activate_values(struct sock *sk, struct list_head *fn);
  387. void dccp_feat_list_purge(struct list_head *fn_list);
  388. int dccp_insert_options(struct sock *sk, struct sk_buff *skb);
  389. int dccp_insert_options_rsk(struct dccp_request_sock *, struct sk_buff *);
  390. u32 dccp_timestamp(void);
  391. void dccp_timestamping_init(void);
  392. int dccp_insert_option(struct sk_buff *skb, unsigned char option,
  393. const void *value, unsigned char len);
  394. #ifdef CONFIG_SYSCTL
  395. int dccp_sysctl_init(void);
  396. void dccp_sysctl_exit(void);
  397. #else
  398. static inline int dccp_sysctl_init(void)
  399. {
  400. return 0;
  401. }
  402. static inline void dccp_sysctl_exit(void)
  403. {
  404. }
  405. #endif
  406. #endif /* _DCCP_H */