dccp.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  1. #ifndef _DCCP_H
  2. #define _DCCP_H
  3. /*
  4. * net/dccp/dccp.h
  5. *
  6. * An implementation of the DCCP protocol
  7. * Arnaldo Carvalho de Melo <acme@conectiva.com.br>
  8. *
  9. * This program is free software; you can redistribute it and/or modify it
  10. * under the terms of the GNU General Public License version 2 as
  11. * published by the Free Software Foundation.
  12. */
  13. #include <linux/dccp.h>
  14. #include <net/snmp.h>
  15. #include <net/sock.h>
  16. #include <net/tcp.h>
  17. #define DCCP_DEBUG
  18. #ifdef DCCP_DEBUG
  19. extern int dccp_debug;
  20. #define dccp_pr_debug(format, a...) \
  21. do { if (dccp_debug) \
  22. printk(KERN_DEBUG "%s: " format, __FUNCTION__ , ##a); \
  23. } while (0)
  24. #define dccp_pr_debug_cat(format, a...) do { if (dccp_debug) printk(format, ##a); } while (0)
  25. #else
  26. #define dccp_pr_debug(format, a...)
  27. #define dccp_pr_debug_cat(format, a...)
  28. #endif
  29. extern struct inet_hashinfo dccp_hashinfo;
  30. extern atomic_t dccp_orphan_count;
  31. extern int dccp_tw_count;
  32. extern void dccp_tw_deschedule(struct inet_timewait_sock *tw);
  33. extern void dccp_time_wait(struct sock *sk, int state, int timeo);
  34. /* FIXME: Right size this */
  35. #define DCCP_MAX_OPT_LEN 128
  36. #define DCCP_MAX_PACKET_HDR 32
  37. #define MAX_DCCP_HEADER (DCCP_MAX_PACKET_HDR + DCCP_MAX_OPT_LEN + MAX_HEADER)
  38. #define DCCP_TIMEWAIT_LEN (60 * HZ) /* how long to wait to destroy TIME-WAIT
  39. * state, about 60 seconds */
  40. /* draft-ietf-dccp-spec-11.txt initial RTO value */
  41. #define DCCP_TIMEOUT_INIT ((unsigned)(3 * HZ))
  42. /* Maximal interval between probes for local resources. */
  43. #define DCCP_RESOURCE_PROBE_INTERVAL ((unsigned)(HZ / 2U))
  44. #define DCCP_RTO_MAX ((unsigned)(120 * HZ)) /* FIXME: using TCP value */
  45. extern struct proto dccp_v4_prot;
  46. /* is seq1 < seq2 ? */
  47. static inline const int before48(const u64 seq1, const u64 seq2)
  48. {
  49. return (const s64)((seq1 << 16) - (seq2 << 16)) < 0;
  50. }
  51. /* is seq1 > seq2 ? */
  52. static inline const int after48(const u64 seq1, const u64 seq2)
  53. {
  54. return (const s64)((seq2 << 16) - (seq1 << 16)) < 0;
  55. }
  56. /* is seq2 <= seq1 <= seq3 ? */
  57. static inline const int between48(const u64 seq1, const u64 seq2, const u64 seq3)
  58. {
  59. return (seq3 << 16) - (seq2 << 16) >= (seq1 << 16) - (seq2 << 16);
  60. }
  61. static inline u64 max48(const u64 seq1, const u64 seq2)
  62. {
  63. return after48(seq1, seq2) ? seq1 : seq2;
  64. }
  65. enum {
  66. DCCP_MIB_NUM = 0,
  67. DCCP_MIB_ACTIVEOPENS, /* ActiveOpens */
  68. DCCP_MIB_ESTABRESETS, /* EstabResets */
  69. DCCP_MIB_CURRESTAB, /* CurrEstab */
  70. DCCP_MIB_OUTSEGS, /* OutSegs */
  71. DCCP_MIB_OUTRSTS,
  72. DCCP_MIB_ABORTONTIMEOUT,
  73. DCCP_MIB_TIMEOUTS,
  74. DCCP_MIB_ABORTFAILED,
  75. DCCP_MIB_PASSIVEOPENS,
  76. DCCP_MIB_ATTEMPTFAILS,
  77. DCCP_MIB_OUTDATAGRAMS,
  78. DCCP_MIB_INERRS,
  79. DCCP_MIB_OPTMANDATORYERROR,
  80. DCCP_MIB_INVALIDOPT,
  81. __DCCP_MIB_MAX
  82. };
  83. #define DCCP_MIB_MAX __DCCP_MIB_MAX
  84. struct dccp_mib {
  85. unsigned long mibs[DCCP_MIB_MAX];
  86. } __SNMP_MIB_ALIGN__;
  87. DECLARE_SNMP_STAT(struct dccp_mib, dccp_statistics);
  88. #define DCCP_INC_STATS(field) SNMP_INC_STATS(dccp_statistics, field)
  89. #define DCCP_INC_STATS_BH(field) SNMP_INC_STATS_BH(dccp_statistics, field)
  90. #define DCCP_INC_STATS_USER(field) SNMP_INC_STATS_USER(dccp_statistics, field)
  91. #define DCCP_DEC_STATS(field) SNMP_DEC_STATS(dccp_statistics, field)
  92. #define DCCP_ADD_STATS_BH(field, val) SNMP_ADD_STATS_BH(dccp_statistics, field, val)
  93. #define DCCP_ADD_STATS_USER(field, val) SNMP_ADD_STATS_USER(dccp_statistics, field, val)
  94. extern int dccp_transmit_skb(struct sock *sk, struct sk_buff *skb);
  95. extern int dccp_retransmit_skb(struct sock *sk, struct sk_buff *skb);
  96. extern int dccp_send_response(struct sock *sk);
  97. extern void dccp_send_ack(struct sock *sk);
  98. extern void dccp_send_delayed_ack(struct sock *sk);
  99. extern void dccp_send_sync(struct sock *sk, u64 seq);
  100. extern void dccp_init_xmit_timers(struct sock *sk);
  101. static inline void dccp_clear_xmit_timers(struct sock *sk)
  102. {
  103. inet_csk_clear_xmit_timers(sk);
  104. }
  105. extern unsigned int dccp_sync_mss(struct sock *sk, u32 pmtu);
  106. extern const char *dccp_packet_name(const int type);
  107. extern const char *dccp_state_name(const int state);
  108. static inline void dccp_set_state(struct sock *sk, const int state)
  109. {
  110. const int oldstate = sk->sk_state;
  111. dccp_pr_debug("%s(%p) %-10.10s -> %s\n",
  112. dccp_role(sk), sk,
  113. dccp_state_name(oldstate), dccp_state_name(state));
  114. WARN_ON(state == oldstate);
  115. switch (state) {
  116. case DCCP_OPEN:
  117. if (oldstate != DCCP_OPEN)
  118. DCCP_INC_STATS(DCCP_MIB_CURRESTAB);
  119. break;
  120. case DCCP_CLOSED:
  121. if (oldstate == DCCP_CLOSING || oldstate == DCCP_OPEN)
  122. DCCP_INC_STATS(DCCP_MIB_ESTABRESETS);
  123. sk->sk_prot->unhash(sk);
  124. if (inet_csk(sk)->icsk_bind_hash != NULL &&
  125. !(sk->sk_userlocks & SOCK_BINDPORT_LOCK))
  126. inet_put_port(&dccp_hashinfo, sk);
  127. /* fall through */
  128. default:
  129. if (oldstate == DCCP_OPEN)
  130. DCCP_DEC_STATS(DCCP_MIB_CURRESTAB);
  131. }
  132. /* Change state AFTER socket is unhashed to avoid closed
  133. * socket sitting in hash tables.
  134. */
  135. sk->sk_state = state;
  136. }
  137. static inline void dccp_done(struct sock *sk)
  138. {
  139. dccp_set_state(sk, DCCP_CLOSED);
  140. dccp_clear_xmit_timers(sk);
  141. sk->sk_shutdown = SHUTDOWN_MASK;
  142. if (!sock_flag(sk, SOCK_DEAD))
  143. sk->sk_state_change(sk);
  144. else
  145. inet_csk_destroy_sock(sk);
  146. }
  147. static inline void dccp_openreq_init(struct request_sock *req,
  148. struct dccp_sock *dp,
  149. struct sk_buff *skb)
  150. {
  151. /*
  152. * FIXME: fill in the other req fields from the DCCP options
  153. * received
  154. */
  155. inet_rsk(req)->rmt_port = dccp_hdr(skb)->dccph_sport;
  156. inet_rsk(req)->acked = 0;
  157. req->rcv_wnd = 0;
  158. }
  159. extern void dccp_v4_send_check(struct sock *sk, struct dccp_hdr *dh, int len,
  160. struct sk_buff *skb);
  161. extern int dccp_v4_conn_request(struct sock *sk, struct sk_buff *skb);
  162. extern struct sock *dccp_create_openreq_child(struct sock *sk,
  163. const struct request_sock *req,
  164. const struct sk_buff *skb);
  165. extern int dccp_v4_do_rcv(struct sock *sk, struct sk_buff *skb);
  166. extern void dccp_v4_err(struct sk_buff *skb, u32);
  167. extern int dccp_v4_rcv(struct sk_buff *skb);
  168. extern struct sock *dccp_v4_request_recv_sock(struct sock *sk,
  169. struct sk_buff *skb,
  170. struct request_sock *req,
  171. struct dst_entry *dst);
  172. extern struct sock *dccp_check_req(struct sock *sk, struct sk_buff *skb,
  173. struct request_sock *req,
  174. struct request_sock **prev);
  175. extern int dccp_child_process(struct sock *parent, struct sock *child,
  176. struct sk_buff *skb);
  177. extern int dccp_rcv_state_process(struct sock *sk, struct sk_buff *skb,
  178. struct dccp_hdr *dh, unsigned len);
  179. extern int dccp_rcv_established(struct sock *sk, struct sk_buff *skb,
  180. const struct dccp_hdr *dh, const unsigned len);
  181. extern void dccp_close(struct sock *sk, long timeout);
  182. extern struct sk_buff *dccp_make_response(struct sock *sk,
  183. struct dst_entry *dst,
  184. struct request_sock *req);
  185. extern int dccp_connect(struct sock *sk);
  186. extern int dccp_disconnect(struct sock *sk, int flags);
  187. extern int dccp_getsockopt(struct sock *sk, int level, int optname,
  188. char *optval, int *optlen);
  189. extern int dccp_ioctl(struct sock *sk, int cmd, unsigned long arg);
  190. extern int dccp_sendmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *msg,
  191. size_t size);
  192. extern int dccp_recvmsg(struct kiocb *iocb, struct sock *sk,
  193. struct msghdr *msg, size_t len, int nonblock,
  194. int flags, int *addr_len);
  195. extern int dccp_setsockopt(struct sock *sk, int level, int optname,
  196. char *optval, int optlen);
  197. extern void dccp_shutdown(struct sock *sk, int how);
  198. extern int dccp_v4_checksum(struct sk_buff *skb);
  199. extern int dccp_v4_send_reset(struct sock *sk, enum dccp_reset_codes code);
  200. extern void dccp_send_close(struct sock *sk);
  201. struct dccp_skb_cb {
  202. __u8 dccpd_type;
  203. __u8 dccpd_reset_code;
  204. __u8 dccpd_service;
  205. __u8 dccpd_ccval;
  206. __u64 dccpd_seq;
  207. __u64 dccpd_ack_seq;
  208. int dccpd_opt_len;
  209. };
  210. #define DCCP_SKB_CB(__skb) ((struct dccp_skb_cb *)&((__skb)->cb[0]))
  211. static inline int dccp_non_data_packet(const struct sk_buff *skb)
  212. {
  213. const __u8 type = DCCP_SKB_CB(skb)->dccpd_type;
  214. return type == DCCP_PKT_ACK ||
  215. type == DCCP_PKT_CLOSE ||
  216. type == DCCP_PKT_CLOSEREQ ||
  217. type == DCCP_PKT_RESET ||
  218. type == DCCP_PKT_SYNC ||
  219. type == DCCP_PKT_SYNCACK;
  220. }
  221. static inline int dccp_packet_without_ack(const struct sk_buff *skb)
  222. {
  223. const __u8 type = DCCP_SKB_CB(skb)->dccpd_type;
  224. return type == DCCP_PKT_DATA || type == DCCP_PKT_REQUEST;
  225. }
  226. #define DCCP_MAX_SEQNO ((((u64)1) << 48) - 1)
  227. #define DCCP_PKT_WITHOUT_ACK_SEQ (DCCP_MAX_SEQNO << 2)
  228. static inline void dccp_set_seqno(u64 *seqno, u64 value)
  229. {
  230. if (value > DCCP_MAX_SEQNO)
  231. value -= DCCP_MAX_SEQNO + 1;
  232. *seqno = value;
  233. }
  234. static inline u64 dccp_delta_seqno(u64 seqno1, u64 seqno2)
  235. {
  236. return ((seqno2 << 16) - (seqno1 << 16)) >> 16;
  237. }
  238. static inline void dccp_inc_seqno(u64 *seqno)
  239. {
  240. if (++*seqno > DCCP_MAX_SEQNO)
  241. *seqno = 0;
  242. }
  243. static inline void dccp_hdr_set_seq(struct dccp_hdr *dh, const u64 gss)
  244. {
  245. struct dccp_hdr_ext *dhx = (struct dccp_hdr_ext *)((void *)dh + sizeof(*dh));
  246. #if defined(__LITTLE_ENDIAN_BITFIELD)
  247. dh->dccph_seq = htonl((gss >> 32)) >> 8;
  248. #elif defined(__BIG_ENDIAN_BITFIELD)
  249. dh->dccph_seq = htonl((gss >> 32));
  250. #else
  251. #error "Adjust your <asm/byteorder.h> defines"
  252. #endif
  253. dhx->dccph_seq_low = htonl(gss & 0xffffffff);
  254. }
  255. static inline void dccp_hdr_set_ack(struct dccp_hdr_ack_bits *dhack, const u64 gsr)
  256. {
  257. #if defined(__LITTLE_ENDIAN_BITFIELD)
  258. dhack->dccph_ack_nr_high = htonl((gsr >> 32)) >> 8;
  259. #elif defined(__BIG_ENDIAN_BITFIELD)
  260. dhack->dccph_ack_nr_high = htonl((gsr >> 32));
  261. #else
  262. #error "Adjust your <asm/byteorder.h> defines"
  263. #endif
  264. dhack->dccph_ack_nr_low = htonl(gsr & 0xffffffff);
  265. }
  266. static inline void dccp_update_gsr(struct sock *sk, u64 seq)
  267. {
  268. struct dccp_sock *dp = dccp_sk(sk);
  269. u64 tmp_gsr;
  270. dccp_set_seqno(&tmp_gsr, dp->dccps_gsr + 1 - (dp->dccps_options.dccpo_sequence_window / 4));
  271. dp->dccps_gsr = seq;
  272. dccp_set_seqno(&dp->dccps_swl, max48(tmp_gsr, dp->dccps_isr));
  273. dccp_set_seqno(&dp->dccps_swh,
  274. dp->dccps_gsr + (3 * dp->dccps_options.dccpo_sequence_window) / 4);
  275. }
  276. static inline void dccp_update_gss(struct sock *sk, u64 seq)
  277. {
  278. struct dccp_sock *dp = dccp_sk(sk);
  279. u64 tmp_gss;
  280. dccp_set_seqno(&tmp_gss, dp->dccps_gss - dp->dccps_options.dccpo_sequence_window + 1);
  281. dp->dccps_awl = max48(tmp_gss, dp->dccps_iss);
  282. dp->dccps_awh = dp->dccps_gss = seq;
  283. }
  284. extern void dccp_insert_options(struct sock *sk, struct sk_buff *skb);
  285. extern void dccp_insert_option_elapsed_time(struct sock *sk,
  286. struct sk_buff *skb,
  287. u32 elapsed_time);
  288. extern void dccp_insert_option(struct sock *sk, struct sk_buff *skb,
  289. unsigned char option,
  290. const void *value, unsigned char len);
  291. extern struct socket *dccp_ctl_socket;
  292. #define DCCP_ACKPKTS_STATE_RECEIVED 0
  293. #define DCCP_ACKPKTS_STATE_ECN_MARKED (1 << 6)
  294. #define DCCP_ACKPKTS_STATE_NOT_RECEIVED (3 << 6)
  295. #define DCCP_ACKPKTS_STATE_MASK 0xC0 /* 11000000 */
  296. #define DCCP_ACKPKTS_LEN_MASK 0x3F /* 00111111 */
  297. /** struct dccp_ackpkts - acknowledgeable packets
  298. *
  299. * This data structure is the one defined in the DCCP draft
  300. * Appendix A.
  301. *
  302. * @dccpap_buf_head - circular buffer head
  303. * @dccpap_buf_tail - circular buffer tail
  304. * @dccpap_buf_ackno - ack # of the most recent packet acknoldgeable in the buffer (i.e. %dccpap_buf_head)
  305. * @dccpap_buf_nonce - the one-bit sum of the ECN Nonces on all packets acked by the buffer with State 0
  306. *
  307. * Additionally, the HC-Receiver must keep some information about the
  308. * Ack Vectors it has recently sent. For each packet sent carrying an
  309. * Ack Vector, it remembers four variables:
  310. *
  311. * @dccpap_ack_seqno - the Sequence Number used for the packet (HC-Receiver seqno)
  312. * @dccpap_ack_ptr - the value of buf_head at the time of acknowledgement.
  313. * @dccpap_ack_ackno - the Acknowledgement Number used for the packet (HC-Sender seqno)
  314. * @dccpap_ack_nonce - the one-bit sum of the ECN Nonces for all State 0.
  315. *
  316. * @dccpap_buf_len - circular buffer length
  317. * @dccpap_buf - circular buffer of acknowledgeable packets
  318. */
  319. struct dccp_ackpkts {
  320. unsigned int dccpap_buf_head;
  321. unsigned int dccpap_buf_tail;
  322. u64 dccpap_buf_ackno;
  323. u64 dccpap_ack_seqno;
  324. u64 dccpap_ack_ackno;
  325. unsigned int dccpap_ack_ptr;
  326. unsigned int dccpap_buf_vector_len;
  327. unsigned int dccpap_ack_vector_len;
  328. unsigned int dccpap_buf_len;
  329. unsigned long dccpap_time;
  330. u8 dccpap_buf_nonce;
  331. u8 dccpap_ack_nonce;
  332. u8 dccpap_buf[0];
  333. };
  334. extern struct dccp_ackpkts *dccp_ackpkts_alloc(unsigned int len, int priority);
  335. extern void dccp_ackpkts_free(struct dccp_ackpkts *ap);
  336. extern int dccp_ackpkts_add(struct dccp_ackpkts *ap, u64 ackno, u8 state);
  337. extern void dccp_ackpkts_check_rcv_ackno(struct dccp_ackpkts *ap,
  338. struct sock *sk, u64 ackno);
  339. #ifdef DCCP_DEBUG
  340. extern void dccp_ackvector_print(const u64 ackno,
  341. const unsigned char *vector, int len);
  342. extern void dccp_ackpkts_print(const struct dccp_ackpkts *ap);
  343. #else
  344. static inline void dccp_ackvector_print(const u64 ackno,
  345. const unsigned char *vector,
  346. int len) { }
  347. static inline void dccp_ackpkts_print(const struct dccp_ackpkts *ap) { }
  348. #endif
  349. #endif /* _DCCP_H */