dccp.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  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, a...) LIMIT_NETDEBUG(KERN_WARNING "%s: " fmt, \
  24. __func__, ##a)
  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 int 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. #else
  42. #define dccp_pr_debug(format, a...)
  43. #define dccp_pr_debug_cat(format, a...)
  44. #endif
  45. extern struct inet_hashinfo dccp_hashinfo;
  46. extern atomic_t dccp_orphan_count;
  47. extern void dccp_time_wait(struct sock *sk, int state, int timeo);
  48. /*
  49. * Set safe upper bounds for header and option length. Since Data Offset is 8
  50. * bits (RFC 4340, sec. 5.1), the total header length can never be more than
  51. * 4 * 255 = 1020 bytes. The largest possible header length is 28 bytes (X=1):
  52. * - DCCP-Response with ACK Subheader and 4 bytes of Service code OR
  53. * - DCCP-Reset with ACK Subheader and 4 bytes of Reset Code fields
  54. * Hence a safe upper bound for the maximum option length is 1020-28 = 992
  55. */
  56. #define MAX_DCCP_SPECIFIC_HEADER (255 * sizeof(int))
  57. #define DCCP_MAX_PACKET_HDR 28
  58. #define DCCP_MAX_OPT_LEN (MAX_DCCP_SPECIFIC_HEADER - DCCP_MAX_PACKET_HDR)
  59. #define MAX_DCCP_HEADER (MAX_DCCP_SPECIFIC_HEADER + MAX_HEADER)
  60. #define DCCP_TIMEWAIT_LEN (60 * HZ) /* how long to wait to destroy TIME-WAIT
  61. * state, about 60 seconds */
  62. /* RFC 1122, 4.2.3.1 initial RTO value */
  63. #define DCCP_TIMEOUT_INIT ((unsigned)(3 * HZ))
  64. /*
  65. * The maximum back-off value for retransmissions. This is needed for
  66. * - retransmitting client-Requests (sec. 8.1.1),
  67. * - retransmitting Close/CloseReq when closing (sec. 8.3),
  68. * - feature-negotiation retransmission (sec. 6.6.3),
  69. * - Acks in client-PARTOPEN state (sec. 8.1.5).
  70. */
  71. #define DCCP_RTO_MAX ((unsigned)(64 * HZ))
  72. /*
  73. * RTT sampling: sanity bounds and fallback RTT value from RFC 4340, section 3.4
  74. */
  75. #define DCCP_SANE_RTT_MIN 100
  76. #define DCCP_FALLBACK_RTT (USEC_PER_SEC / 5)
  77. #define DCCP_SANE_RTT_MAX (3 * USEC_PER_SEC)
  78. /* Maximal interval between probes for local resources. */
  79. #define DCCP_RESOURCE_PROBE_INTERVAL ((unsigned)(HZ / 2U))
  80. /* sysctl variables for DCCP */
  81. extern int sysctl_dccp_request_retries;
  82. extern int sysctl_dccp_retries1;
  83. extern int sysctl_dccp_retries2;
  84. extern int sysctl_dccp_feat_sequence_window;
  85. extern int sysctl_dccp_feat_rx_ccid;
  86. extern int sysctl_dccp_feat_tx_ccid;
  87. extern int sysctl_dccp_feat_ack_ratio;
  88. extern int sysctl_dccp_feat_send_ack_vector;
  89. extern int sysctl_dccp_feat_send_ndp_count;
  90. extern int sysctl_dccp_tx_qlen;
  91. extern int sysctl_dccp_sync_ratelimit;
  92. /*
  93. * 48-bit sequence number arithmetic (signed and unsigned)
  94. */
  95. #define INT48_MIN 0x800000000000LL /* 2^47 */
  96. #define UINT48_MAX 0xFFFFFFFFFFFFLL /* 2^48 - 1 */
  97. #define COMPLEMENT48(x) (0x1000000000000LL - (x)) /* 2^48 - x */
  98. #define TO_SIGNED48(x) (((x) < INT48_MIN)? (x) : -COMPLEMENT48( (x)))
  99. #define TO_UNSIGNED48(x) (((x) >= 0)? (x) : COMPLEMENT48(-(x)))
  100. #define ADD48(a, b) (((a) + (b)) & UINT48_MAX)
  101. #define SUB48(a, b) ADD48((a), COMPLEMENT48(b))
  102. static inline void dccp_set_seqno(u64 *seqno, u64 value)
  103. {
  104. *seqno = value & UINT48_MAX;
  105. }
  106. static inline void dccp_inc_seqno(u64 *seqno)
  107. {
  108. *seqno = ADD48(*seqno, 1);
  109. }
  110. /* signed mod-2^48 distance: pos. if seqno1 < seqno2, neg. if seqno1 > seqno2 */
  111. static inline s64 dccp_delta_seqno(const u64 seqno1, const u64 seqno2)
  112. {
  113. u64 delta = SUB48(seqno2, seqno1);
  114. return TO_SIGNED48(delta);
  115. }
  116. /* is seq1 < seq2 ? */
  117. static inline int before48(const u64 seq1, const u64 seq2)
  118. {
  119. return (s64)((seq2 << 16) - (seq1 << 16)) > 0;
  120. }
  121. /* is seq1 > seq2 ? */
  122. #define after48(seq1, seq2) before48(seq2, seq1)
  123. /* is seq2 <= seq1 <= seq3 ? */
  124. static inline int between48(const u64 seq1, const u64 seq2, const u64 seq3)
  125. {
  126. return (seq3 << 16) - (seq2 << 16) >= (seq1 << 16) - (seq2 << 16);
  127. }
  128. static inline u64 max48(const u64 seq1, const u64 seq2)
  129. {
  130. return after48(seq1, seq2) ? seq1 : seq2;
  131. }
  132. /**
  133. * dccp_loss_free - Evaluates condition for data loss from RFC 4340, 7.7.1
  134. * @s1: start sequence number
  135. * @s2: end sequence number
  136. * @ndp: NDP count on packet with sequence number @s2
  137. * Returns true if the sequence range s1...s2 has no data loss.
  138. */
  139. static inline bool dccp_loss_free(const u64 s1, const u64 s2, const u64 ndp)
  140. {
  141. s64 delta = dccp_delta_seqno(s1, s2);
  142. WARN_ON(delta < 0);
  143. return (u64)delta <= ndp + 1;
  144. }
  145. enum {
  146. DCCP_MIB_NUM = 0,
  147. DCCP_MIB_ACTIVEOPENS, /* ActiveOpens */
  148. DCCP_MIB_ESTABRESETS, /* EstabResets */
  149. DCCP_MIB_CURRESTAB, /* CurrEstab */
  150. DCCP_MIB_OUTSEGS, /* OutSegs */
  151. DCCP_MIB_OUTRSTS,
  152. DCCP_MIB_ABORTONTIMEOUT,
  153. DCCP_MIB_TIMEOUTS,
  154. DCCP_MIB_ABORTFAILED,
  155. DCCP_MIB_PASSIVEOPENS,
  156. DCCP_MIB_ATTEMPTFAILS,
  157. DCCP_MIB_OUTDATAGRAMS,
  158. DCCP_MIB_INERRS,
  159. DCCP_MIB_OPTMANDATORYERROR,
  160. DCCP_MIB_INVALIDOPT,
  161. __DCCP_MIB_MAX
  162. };
  163. #define DCCP_MIB_MAX __DCCP_MIB_MAX
  164. struct dccp_mib {
  165. unsigned long mibs[DCCP_MIB_MAX];
  166. } __SNMP_MIB_ALIGN__;
  167. DECLARE_SNMP_STAT(struct dccp_mib, dccp_statistics);
  168. #define DCCP_INC_STATS(field) SNMP_INC_STATS(dccp_statistics, field)
  169. #define DCCP_INC_STATS_BH(field) SNMP_INC_STATS_BH(dccp_statistics, field)
  170. #define DCCP_INC_STATS_USER(field) SNMP_INC_STATS_USER(dccp_statistics, field)
  171. #define DCCP_DEC_STATS(field) SNMP_DEC_STATS(dccp_statistics, field)
  172. #define DCCP_ADD_STATS_BH(field, val) \
  173. SNMP_ADD_STATS_BH(dccp_statistics, field, val)
  174. #define DCCP_ADD_STATS_USER(field, val) \
  175. SNMP_ADD_STATS_USER(dccp_statistics, field, val)
  176. /*
  177. * Checksumming routines
  178. */
  179. static inline unsigned int dccp_csum_coverage(const struct sk_buff *skb)
  180. {
  181. const struct dccp_hdr* dh = dccp_hdr(skb);
  182. if (dh->dccph_cscov == 0)
  183. return skb->len;
  184. return (dh->dccph_doff + dh->dccph_cscov - 1) * sizeof(u32);
  185. }
  186. static inline void dccp_csum_outgoing(struct sk_buff *skb)
  187. {
  188. unsigned int cov = dccp_csum_coverage(skb);
  189. if (cov >= skb->len)
  190. dccp_hdr(skb)->dccph_cscov = 0;
  191. skb->csum = skb_checksum(skb, 0, (cov > skb->len)? skb->len : cov, 0);
  192. }
  193. extern void dccp_v4_send_check(struct sock *sk, int len, struct sk_buff *skb);
  194. extern int dccp_retransmit_skb(struct sock *sk);
  195. extern void dccp_send_ack(struct sock *sk);
  196. extern void dccp_reqsk_send_ack(struct sock *sk, struct sk_buff *skb,
  197. struct request_sock *rsk);
  198. extern void dccp_send_sync(struct sock *sk, const u64 seq,
  199. const enum dccp_pkt_type pkt_type);
  200. extern void dccp_write_xmit(struct sock *sk, int block);
  201. extern void dccp_write_space(struct sock *sk);
  202. extern 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. extern unsigned int dccp_sync_mss(struct sock *sk, u32 pmtu);
  208. extern const char *dccp_packet_name(const int type);
  209. extern const char *dccp_state_name(const int state);
  210. extern void dccp_set_state(struct sock *sk, const int state);
  211. extern void dccp_done(struct sock *sk);
  212. extern void dccp_reqsk_init(struct request_sock *req, struct sk_buff *skb);
  213. extern int dccp_v4_conn_request(struct sock *sk, struct sk_buff *skb);
  214. extern struct sock *dccp_create_openreq_child(struct sock *sk,
  215. const struct request_sock *req,
  216. const struct sk_buff *skb);
  217. extern int dccp_v4_do_rcv(struct sock *sk, struct sk_buff *skb);
  218. extern struct sock *dccp_v4_request_recv_sock(struct sock *sk,
  219. struct sk_buff *skb,
  220. struct request_sock *req,
  221. struct dst_entry *dst);
  222. extern struct sock *dccp_check_req(struct sock *sk, struct sk_buff *skb,
  223. struct request_sock *req,
  224. struct request_sock **prev);
  225. extern int dccp_child_process(struct sock *parent, struct sock *child,
  226. struct sk_buff *skb);
  227. extern int dccp_rcv_state_process(struct sock *sk, struct sk_buff *skb,
  228. struct dccp_hdr *dh, unsigned len);
  229. extern int dccp_rcv_established(struct sock *sk, struct sk_buff *skb,
  230. const struct dccp_hdr *dh, const unsigned len);
  231. extern int dccp_init_sock(struct sock *sk, const __u8 ctl_sock_initialized);
  232. extern void dccp_destroy_sock(struct sock *sk);
  233. extern void dccp_close(struct sock *sk, long timeout);
  234. extern struct sk_buff *dccp_make_response(struct sock *sk,
  235. struct dst_entry *dst,
  236. struct request_sock *req);
  237. extern int dccp_connect(struct sock *sk);
  238. extern int dccp_disconnect(struct sock *sk, int flags);
  239. extern int dccp_getsockopt(struct sock *sk, int level, int optname,
  240. char __user *optval, int __user *optlen);
  241. extern int dccp_setsockopt(struct sock *sk, int level, int optname,
  242. char __user *optval, int optlen);
  243. #ifdef CONFIG_COMPAT
  244. extern int compat_dccp_getsockopt(struct sock *sk,
  245. int level, int optname,
  246. char __user *optval, int __user *optlen);
  247. extern int compat_dccp_setsockopt(struct sock *sk,
  248. int level, int optname,
  249. char __user *optval, int optlen);
  250. #endif
  251. extern int dccp_ioctl(struct sock *sk, int cmd, unsigned long arg);
  252. extern int dccp_sendmsg(struct kiocb *iocb, struct sock *sk,
  253. struct msghdr *msg, size_t size);
  254. extern int dccp_recvmsg(struct kiocb *iocb, struct sock *sk,
  255. struct msghdr *msg, size_t len, int nonblock,
  256. int flags, int *addr_len);
  257. extern void dccp_shutdown(struct sock *sk, int how);
  258. extern int inet_dccp_listen(struct socket *sock, int backlog);
  259. extern unsigned int dccp_poll(struct file *file, struct socket *sock,
  260. poll_table *wait);
  261. extern int dccp_v4_connect(struct sock *sk, struct sockaddr *uaddr,
  262. int addr_len);
  263. extern struct sk_buff *dccp_ctl_make_reset(struct sock *sk,
  264. struct sk_buff *skb);
  265. extern int dccp_send_reset(struct sock *sk, enum dccp_reset_codes code);
  266. extern void dccp_send_close(struct sock *sk, const int active);
  267. extern int dccp_invalid_packet(struct sk_buff *skb);
  268. extern u32 dccp_sample_rtt(struct sock *sk, long delta);
  269. static inline int dccp_bad_service_code(const struct sock *sk,
  270. const __be32 service)
  271. {
  272. const struct dccp_sock *dp = dccp_sk(sk);
  273. if (dp->dccps_service == service)
  274. return 0;
  275. return !dccp_list_has_service(dp->dccps_service_list, service);
  276. }
  277. /**
  278. * dccp_skb_cb - DCCP per-packet control information
  279. * @dccpd_type: one of %dccp_pkt_type (or unknown)
  280. * @dccpd_ccval: CCVal field (5.1), see e.g. RFC 4342, 8.1
  281. * @dccpd_reset_code: one of %dccp_reset_codes
  282. * @dccpd_reset_data: Data1..3 fields (depend on @dccpd_reset_code)
  283. * @dccpd_opt_len: total length of all options (5.8) in the packet
  284. * @dccpd_seq: sequence number
  285. * @dccpd_ack_seq: acknowledgment number subheader field value
  286. * This is used for transmission as well as for reception.
  287. */
  288. struct dccp_skb_cb {
  289. union {
  290. struct inet_skb_parm h4;
  291. #if defined(CONFIG_IPV6) || defined (CONFIG_IPV6_MODULE)
  292. struct inet6_skb_parm h6;
  293. #endif
  294. } header;
  295. __u8 dccpd_type:4;
  296. __u8 dccpd_ccval:4;
  297. __u8 dccpd_reset_code,
  298. dccpd_reset_data[3];
  299. __u16 dccpd_opt_len;
  300. __u64 dccpd_seq;
  301. __u64 dccpd_ack_seq;
  302. };
  303. #define DCCP_SKB_CB(__skb) ((struct dccp_skb_cb *)&((__skb)->cb[0]))
  304. /* RFC 4340, sec. 7.7 */
  305. static inline int dccp_non_data_packet(const struct sk_buff *skb)
  306. {
  307. const __u8 type = DCCP_SKB_CB(skb)->dccpd_type;
  308. return type == DCCP_PKT_ACK ||
  309. type == DCCP_PKT_CLOSE ||
  310. type == DCCP_PKT_CLOSEREQ ||
  311. type == DCCP_PKT_RESET ||
  312. type == DCCP_PKT_SYNC ||
  313. type == DCCP_PKT_SYNCACK;
  314. }
  315. /* RFC 4340, sec. 7.7 */
  316. static inline int dccp_data_packet(const struct sk_buff *skb)
  317. {
  318. const __u8 type = DCCP_SKB_CB(skb)->dccpd_type;
  319. return type == DCCP_PKT_DATA ||
  320. type == DCCP_PKT_DATAACK ||
  321. type == DCCP_PKT_REQUEST ||
  322. type == DCCP_PKT_RESPONSE;
  323. }
  324. static inline int dccp_packet_without_ack(const struct sk_buff *skb)
  325. {
  326. const __u8 type = DCCP_SKB_CB(skb)->dccpd_type;
  327. return type == DCCP_PKT_DATA || type == DCCP_PKT_REQUEST;
  328. }
  329. #define DCCP_PKT_WITHOUT_ACK_SEQ (UINT48_MAX << 2)
  330. static inline void dccp_hdr_set_seq(struct dccp_hdr *dh, const u64 gss)
  331. {
  332. struct dccp_hdr_ext *dhx = (struct dccp_hdr_ext *)((void *)dh +
  333. sizeof(*dh));
  334. dh->dccph_seq2 = 0;
  335. dh->dccph_seq = htons((gss >> 32) & 0xfffff);
  336. dhx->dccph_seq_low = htonl(gss & 0xffffffff);
  337. }
  338. static inline void dccp_hdr_set_ack(struct dccp_hdr_ack_bits *dhack,
  339. const u64 gsr)
  340. {
  341. dhack->dccph_reserved1 = 0;
  342. dhack->dccph_ack_nr_high = htons(gsr >> 32);
  343. dhack->dccph_ack_nr_low = htonl(gsr & 0xffffffff);
  344. }
  345. static inline void dccp_update_gsr(struct sock *sk, u64 seq)
  346. {
  347. struct dccp_sock *dp = dccp_sk(sk);
  348. const struct dccp_minisock *dmsk = dccp_msk(sk);
  349. dp->dccps_gsr = seq;
  350. dccp_set_seqno(&dp->dccps_swl,
  351. dp->dccps_gsr + 1 - (dmsk->dccpms_sequence_window / 4));
  352. dccp_set_seqno(&dp->dccps_swh,
  353. dp->dccps_gsr + (3 * dmsk->dccpms_sequence_window) / 4);
  354. }
  355. static inline void dccp_update_gss(struct sock *sk, u64 seq)
  356. {
  357. struct dccp_sock *dp = dccp_sk(sk);
  358. dp->dccps_awh = dp->dccps_gss = seq;
  359. dccp_set_seqno(&dp->dccps_awl,
  360. (dp->dccps_gss -
  361. dccp_msk(sk)->dccpms_sequence_window + 1));
  362. }
  363. static inline int dccp_ack_pending(const struct sock *sk)
  364. {
  365. const struct dccp_sock *dp = dccp_sk(sk);
  366. return dp->dccps_timestamp_echo != 0 ||
  367. #ifdef CONFIG_IP_DCCP_ACKVEC
  368. (dccp_msk(sk)->dccpms_send_ack_vector &&
  369. dccp_ackvec_pending(dp->dccps_hc_rx_ackvec)) ||
  370. #endif
  371. inet_csk_ack_scheduled(sk);
  372. }
  373. extern int dccp_insert_options(struct sock *sk, struct sk_buff *skb);
  374. extern int dccp_insert_options_rsk(struct dccp_request_sock*, struct sk_buff*);
  375. extern int dccp_insert_option_elapsed_time(struct sock *sk,
  376. struct sk_buff *skb,
  377. u32 elapsed_time);
  378. extern u32 dccp_timestamp(void);
  379. extern void dccp_timestamping_init(void);
  380. extern int dccp_insert_option_timestamp(struct sock *sk,
  381. struct sk_buff *skb);
  382. extern int dccp_insert_option(struct sock *sk, struct sk_buff *skb,
  383. unsigned char option,
  384. const void *value, unsigned char len);
  385. #ifdef CONFIG_SYSCTL
  386. extern int dccp_sysctl_init(void);
  387. extern void dccp_sysctl_exit(void);
  388. #else
  389. static inline int dccp_sysctl_init(void)
  390. {
  391. return 0;
  392. }
  393. static inline void dccp_sysctl_exit(void)
  394. {
  395. }
  396. #endif
  397. #endif /* _DCCP_H */