dccp.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476
  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 struct percpu_counter 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_tx_qlen;
  88. extern int sysctl_dccp_sync_ratelimit;
  89. /*
  90. * 48-bit sequence number arithmetic (signed and unsigned)
  91. */
  92. #define INT48_MIN 0x800000000000LL /* 2^47 */
  93. #define UINT48_MAX 0xFFFFFFFFFFFFLL /* 2^48 - 1 */
  94. #define COMPLEMENT48(x) (0x1000000000000LL - (x)) /* 2^48 - x */
  95. #define TO_SIGNED48(x) (((x) < INT48_MIN)? (x) : -COMPLEMENT48( (x)))
  96. #define TO_UNSIGNED48(x) (((x) >= 0)? (x) : COMPLEMENT48(-(x)))
  97. #define ADD48(a, b) (((a) + (b)) & UINT48_MAX)
  98. #define SUB48(a, b) ADD48((a), COMPLEMENT48(b))
  99. static inline void dccp_set_seqno(u64 *seqno, u64 value)
  100. {
  101. *seqno = value & UINT48_MAX;
  102. }
  103. static inline void dccp_inc_seqno(u64 *seqno)
  104. {
  105. *seqno = ADD48(*seqno, 1);
  106. }
  107. /* signed mod-2^48 distance: pos. if seqno1 < seqno2, neg. if seqno1 > seqno2 */
  108. static inline s64 dccp_delta_seqno(const u64 seqno1, const u64 seqno2)
  109. {
  110. u64 delta = SUB48(seqno2, seqno1);
  111. return TO_SIGNED48(delta);
  112. }
  113. /* is seq1 < seq2 ? */
  114. static inline int before48(const u64 seq1, const u64 seq2)
  115. {
  116. return (s64)((seq2 << 16) - (seq1 << 16)) > 0;
  117. }
  118. /* is seq1 > seq2 ? */
  119. #define after48(seq1, seq2) before48(seq2, seq1)
  120. /* is seq2 <= seq1 <= seq3 ? */
  121. static inline int between48(const u64 seq1, const u64 seq2, const u64 seq3)
  122. {
  123. return (seq3 << 16) - (seq2 << 16) >= (seq1 << 16) - (seq2 << 16);
  124. }
  125. static inline u64 max48(const u64 seq1, const u64 seq2)
  126. {
  127. return after48(seq1, seq2) ? seq1 : seq2;
  128. }
  129. /**
  130. * dccp_loss_free - Evaluates condition for data loss from RFC 4340, 7.7.1
  131. * @s1: start sequence number
  132. * @s2: end sequence number
  133. * @ndp: NDP count on packet with sequence number @s2
  134. * Returns true if the sequence range s1...s2 has no data loss.
  135. */
  136. static inline bool dccp_loss_free(const u64 s1, const u64 s2, const u64 ndp)
  137. {
  138. s64 delta = dccp_delta_seqno(s1, s2);
  139. WARN_ON(delta < 0);
  140. return (u64)delta <= ndp + 1;
  141. }
  142. enum {
  143. DCCP_MIB_NUM = 0,
  144. DCCP_MIB_ACTIVEOPENS, /* ActiveOpens */
  145. DCCP_MIB_ESTABRESETS, /* EstabResets */
  146. DCCP_MIB_CURRESTAB, /* CurrEstab */
  147. DCCP_MIB_OUTSEGS, /* OutSegs */
  148. DCCP_MIB_OUTRSTS,
  149. DCCP_MIB_ABORTONTIMEOUT,
  150. DCCP_MIB_TIMEOUTS,
  151. DCCP_MIB_ABORTFAILED,
  152. DCCP_MIB_PASSIVEOPENS,
  153. DCCP_MIB_ATTEMPTFAILS,
  154. DCCP_MIB_OUTDATAGRAMS,
  155. DCCP_MIB_INERRS,
  156. DCCP_MIB_OPTMANDATORYERROR,
  157. DCCP_MIB_INVALIDOPT,
  158. __DCCP_MIB_MAX
  159. };
  160. #define DCCP_MIB_MAX __DCCP_MIB_MAX
  161. struct dccp_mib {
  162. unsigned long mibs[DCCP_MIB_MAX];
  163. } __SNMP_MIB_ALIGN__;
  164. DECLARE_SNMP_STAT(struct dccp_mib, dccp_statistics);
  165. #define DCCP_INC_STATS(field) SNMP_INC_STATS(dccp_statistics, field)
  166. #define DCCP_INC_STATS_BH(field) SNMP_INC_STATS_BH(dccp_statistics, field)
  167. #define DCCP_INC_STATS_USER(field) SNMP_INC_STATS_USER(dccp_statistics, field)
  168. #define DCCP_DEC_STATS(field) SNMP_DEC_STATS(dccp_statistics, field)
  169. #define DCCP_ADD_STATS_BH(field, val) \
  170. SNMP_ADD_STATS_BH(dccp_statistics, field, val)
  171. #define DCCP_ADD_STATS_USER(field, val) \
  172. SNMP_ADD_STATS_USER(dccp_statistics, field, val)
  173. /*
  174. * Checksumming routines
  175. */
  176. static inline unsigned int dccp_csum_coverage(const struct sk_buff *skb)
  177. {
  178. const struct dccp_hdr* dh = dccp_hdr(skb);
  179. if (dh->dccph_cscov == 0)
  180. return skb->len;
  181. return (dh->dccph_doff + dh->dccph_cscov - 1) * sizeof(u32);
  182. }
  183. static inline void dccp_csum_outgoing(struct sk_buff *skb)
  184. {
  185. unsigned int cov = dccp_csum_coverage(skb);
  186. if (cov >= skb->len)
  187. dccp_hdr(skb)->dccph_cscov = 0;
  188. skb->csum = skb_checksum(skb, 0, (cov > skb->len)? skb->len : cov, 0);
  189. }
  190. extern void dccp_v4_send_check(struct sock *sk, int len, struct sk_buff *skb);
  191. extern int dccp_retransmit_skb(struct sock *sk);
  192. extern void dccp_send_ack(struct sock *sk);
  193. extern void dccp_reqsk_send_ack(struct sock *sk, struct sk_buff *skb,
  194. struct request_sock *rsk);
  195. extern void dccp_send_sync(struct sock *sk, const u64 seq,
  196. const enum dccp_pkt_type pkt_type);
  197. extern void dccp_write_xmit(struct sock *sk, int block);
  198. extern void dccp_write_space(struct sock *sk);
  199. extern void dccp_init_xmit_timers(struct sock *sk);
  200. static inline void dccp_clear_xmit_timers(struct sock *sk)
  201. {
  202. inet_csk_clear_xmit_timers(sk);
  203. }
  204. extern unsigned int dccp_sync_mss(struct sock *sk, u32 pmtu);
  205. extern const char *dccp_packet_name(const int type);
  206. extern const char *dccp_state_name(const int state);
  207. extern void dccp_set_state(struct sock *sk, const int state);
  208. extern void dccp_done(struct sock *sk);
  209. extern int dccp_reqsk_init(struct request_sock *rq, struct dccp_sock const *dp,
  210. struct sk_buff const *skb);
  211. extern int dccp_v4_conn_request(struct sock *sk, struct sk_buff *skb);
  212. extern struct sock *dccp_create_openreq_child(struct sock *sk,
  213. const struct request_sock *req,
  214. const struct sk_buff *skb);
  215. extern int dccp_v4_do_rcv(struct sock *sk, struct sk_buff *skb);
  216. extern struct sock *dccp_v4_request_recv_sock(struct sock *sk,
  217. struct sk_buff *skb,
  218. struct request_sock *req,
  219. struct dst_entry *dst);
  220. extern struct sock *dccp_check_req(struct sock *sk, struct sk_buff *skb,
  221. struct request_sock *req,
  222. struct request_sock **prev);
  223. extern int dccp_child_process(struct sock *parent, struct sock *child,
  224. struct sk_buff *skb);
  225. extern int dccp_rcv_state_process(struct sock *sk, struct sk_buff *skb,
  226. struct dccp_hdr *dh, unsigned len);
  227. extern int dccp_rcv_established(struct sock *sk, struct sk_buff *skb,
  228. const struct dccp_hdr *dh, const unsigned len);
  229. extern int dccp_init_sock(struct sock *sk, const __u8 ctl_sock_initialized);
  230. extern void dccp_destroy_sock(struct sock *sk);
  231. extern void dccp_close(struct sock *sk, long timeout);
  232. extern struct sk_buff *dccp_make_response(struct sock *sk,
  233. struct dst_entry *dst,
  234. struct request_sock *req);
  235. extern int dccp_connect(struct sock *sk);
  236. extern int dccp_disconnect(struct sock *sk, int flags);
  237. extern int dccp_getsockopt(struct sock *sk, int level, int optname,
  238. char __user *optval, int __user *optlen);
  239. extern int dccp_setsockopt(struct sock *sk, int level, int optname,
  240. char __user *optval, int optlen);
  241. #ifdef CONFIG_COMPAT
  242. extern int compat_dccp_getsockopt(struct sock *sk,
  243. int level, int optname,
  244. char __user *optval, int __user *optlen);
  245. extern int compat_dccp_setsockopt(struct sock *sk,
  246. int level, int optname,
  247. char __user *optval, int optlen);
  248. #endif
  249. extern int dccp_ioctl(struct sock *sk, int cmd, unsigned long arg);
  250. extern int dccp_sendmsg(struct kiocb *iocb, struct sock *sk,
  251. struct msghdr *msg, size_t size);
  252. extern int dccp_recvmsg(struct kiocb *iocb, struct sock *sk,
  253. struct msghdr *msg, size_t len, int nonblock,
  254. int flags, int *addr_len);
  255. extern void dccp_shutdown(struct sock *sk, int how);
  256. extern int inet_dccp_listen(struct socket *sock, int backlog);
  257. extern unsigned int dccp_poll(struct file *file, struct socket *sock,
  258. poll_table *wait);
  259. extern int dccp_v4_connect(struct sock *sk, struct sockaddr *uaddr,
  260. int addr_len);
  261. extern struct sk_buff *dccp_ctl_make_reset(struct sock *sk,
  262. struct sk_buff *skb);
  263. extern int dccp_send_reset(struct sock *sk, enum dccp_reset_codes code);
  264. extern void dccp_send_close(struct sock *sk, const int active);
  265. extern int dccp_invalid_packet(struct sk_buff *skb);
  266. extern u32 dccp_sample_rtt(struct sock *sk, long delta);
  267. static inline int dccp_bad_service_code(const struct sock *sk,
  268. const __be32 service)
  269. {
  270. const struct dccp_sock *dp = dccp_sk(sk);
  271. if (dp->dccps_service == service)
  272. return 0;
  273. return !dccp_list_has_service(dp->dccps_service_list, service);
  274. }
  275. /**
  276. * dccp_skb_cb - DCCP per-packet control information
  277. * @dccpd_type: one of %dccp_pkt_type (or unknown)
  278. * @dccpd_ccval: CCVal field (5.1), see e.g. RFC 4342, 8.1
  279. * @dccpd_reset_code: one of %dccp_reset_codes
  280. * @dccpd_reset_data: Data1..3 fields (depend on @dccpd_reset_code)
  281. * @dccpd_opt_len: total length of all options (5.8) in the packet
  282. * @dccpd_seq: sequence number
  283. * @dccpd_ack_seq: acknowledgment number subheader field value
  284. * This is used for transmission as well as for reception.
  285. */
  286. struct dccp_skb_cb {
  287. union {
  288. struct inet_skb_parm h4;
  289. #if defined(CONFIG_IPV6) || defined (CONFIG_IPV6_MODULE)
  290. struct inet6_skb_parm h6;
  291. #endif
  292. } header;
  293. __u8 dccpd_type:4;
  294. __u8 dccpd_ccval:4;
  295. __u8 dccpd_reset_code,
  296. dccpd_reset_data[3];
  297. __u16 dccpd_opt_len;
  298. __u64 dccpd_seq;
  299. __u64 dccpd_ack_seq;
  300. };
  301. #define DCCP_SKB_CB(__skb) ((struct dccp_skb_cb *)&((__skb)->cb[0]))
  302. /* RFC 4340, sec. 7.7 */
  303. static inline int dccp_non_data_packet(const struct sk_buff *skb)
  304. {
  305. const __u8 type = DCCP_SKB_CB(skb)->dccpd_type;
  306. return type == DCCP_PKT_ACK ||
  307. type == DCCP_PKT_CLOSE ||
  308. type == DCCP_PKT_CLOSEREQ ||
  309. type == DCCP_PKT_RESET ||
  310. type == DCCP_PKT_SYNC ||
  311. type == DCCP_PKT_SYNCACK;
  312. }
  313. /* RFC 4340, sec. 7.7 */
  314. static inline int dccp_data_packet(const struct sk_buff *skb)
  315. {
  316. const __u8 type = DCCP_SKB_CB(skb)->dccpd_type;
  317. return type == DCCP_PKT_DATA ||
  318. type == DCCP_PKT_DATAACK ||
  319. type == DCCP_PKT_REQUEST ||
  320. type == DCCP_PKT_RESPONSE;
  321. }
  322. static inline int dccp_packet_without_ack(const struct sk_buff *skb)
  323. {
  324. const __u8 type = DCCP_SKB_CB(skb)->dccpd_type;
  325. return type == DCCP_PKT_DATA || type == DCCP_PKT_REQUEST;
  326. }
  327. #define DCCP_PKT_WITHOUT_ACK_SEQ (UINT48_MAX << 2)
  328. static inline void dccp_hdr_set_seq(struct dccp_hdr *dh, const u64 gss)
  329. {
  330. struct dccp_hdr_ext *dhx = (struct dccp_hdr_ext *)((void *)dh +
  331. sizeof(*dh));
  332. dh->dccph_seq2 = 0;
  333. dh->dccph_seq = htons((gss >> 32) & 0xfffff);
  334. dhx->dccph_seq_low = htonl(gss & 0xffffffff);
  335. }
  336. static inline void dccp_hdr_set_ack(struct dccp_hdr_ack_bits *dhack,
  337. const u64 gsr)
  338. {
  339. dhack->dccph_reserved1 = 0;
  340. dhack->dccph_ack_nr_high = htons(gsr >> 32);
  341. dhack->dccph_ack_nr_low = htonl(gsr & 0xffffffff);
  342. }
  343. static inline void dccp_update_gsr(struct sock *sk, u64 seq)
  344. {
  345. struct dccp_sock *dp = dccp_sk(sk);
  346. const struct dccp_minisock *dmsk = dccp_msk(sk);
  347. dp->dccps_gsr = seq;
  348. dccp_set_seqno(&dp->dccps_swl,
  349. dp->dccps_gsr + 1 - (dmsk->dccpms_sequence_window / 4));
  350. dccp_set_seqno(&dp->dccps_swh,
  351. dp->dccps_gsr + (3 * dmsk->dccpms_sequence_window) / 4);
  352. }
  353. static inline void dccp_update_gss(struct sock *sk, u64 seq)
  354. {
  355. struct dccp_sock *dp = dccp_sk(sk);
  356. dp->dccps_awh = dp->dccps_gss = seq;
  357. dccp_set_seqno(&dp->dccps_awl,
  358. (dp->dccps_gss -
  359. dccp_msk(sk)->dccpms_sequence_window + 1));
  360. }
  361. static inline int dccp_ack_pending(const struct sock *sk)
  362. {
  363. const struct dccp_sock *dp = dccp_sk(sk);
  364. return dp->dccps_timestamp_echo != 0 ||
  365. #ifdef CONFIG_IP_DCCP_ACKVEC
  366. (dp->dccps_hc_rx_ackvec != NULL &&
  367. dccp_ackvec_pending(dp->dccps_hc_rx_ackvec)) ||
  368. #endif
  369. inet_csk_ack_scheduled(sk);
  370. }
  371. extern int dccp_feat_finalise_settings(struct dccp_sock *dp);
  372. extern int dccp_feat_server_ccid_dependencies(struct dccp_request_sock *dreq);
  373. extern int dccp_feat_insert_opts(struct dccp_sock*, struct dccp_request_sock*,
  374. struct sk_buff *skb);
  375. extern int dccp_feat_activate_values(struct sock *sk, struct list_head *fn);
  376. extern void dccp_feat_list_purge(struct list_head *fn_list);
  377. extern int dccp_insert_options(struct sock *sk, struct sk_buff *skb);
  378. extern int dccp_insert_options_rsk(struct dccp_request_sock*, struct sk_buff*);
  379. extern int dccp_insert_option_elapsed_time(struct sock *sk,
  380. struct sk_buff *skb,
  381. u32 elapsed_time);
  382. extern u32 dccp_timestamp(void);
  383. extern void dccp_timestamping_init(void);
  384. extern int dccp_insert_option_timestamp(struct sock *sk,
  385. struct sk_buff *skb);
  386. extern int dccp_insert_option(struct sock *sk, struct sk_buff *skb,
  387. unsigned char option,
  388. const void *value, unsigned char len);
  389. #ifdef CONFIG_SYSCTL
  390. extern int dccp_sysctl_init(void);
  391. extern void dccp_sysctl_exit(void);
  392. #else
  393. static inline int dccp_sysctl_init(void)
  394. {
  395. return 0;
  396. }
  397. static inline void dccp_sysctl_exit(void)
  398. {
  399. }
  400. #endif
  401. #endif /* _DCCP_H */