dccp.h 16 KB

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