dccp.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  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(const struct sk_buff *skb,
  199. const u32 saddr, const u32 daddr);
  200. extern int dccp_v4_send_reset(struct sock *sk, enum dccp_reset_codes code);
  201. extern void dccp_send_close(struct sock *sk);
  202. struct dccp_skb_cb {
  203. __u8 dccpd_type;
  204. __u8 dccpd_reset_code;
  205. __u8 dccpd_service;
  206. __u8 dccpd_ccval;
  207. __u64 dccpd_seq;
  208. __u64 dccpd_ack_seq;
  209. int dccpd_opt_len;
  210. };
  211. #define DCCP_SKB_CB(__skb) ((struct dccp_skb_cb *)&((__skb)->cb[0]))
  212. static inline int dccp_non_data_packet(const struct sk_buff *skb)
  213. {
  214. const __u8 type = DCCP_SKB_CB(skb)->dccpd_type;
  215. return type == DCCP_PKT_ACK ||
  216. type == DCCP_PKT_CLOSE ||
  217. type == DCCP_PKT_CLOSEREQ ||
  218. type == DCCP_PKT_RESET ||
  219. type == DCCP_PKT_SYNC ||
  220. type == DCCP_PKT_SYNCACK;
  221. }
  222. static inline int dccp_packet_without_ack(const struct sk_buff *skb)
  223. {
  224. const __u8 type = DCCP_SKB_CB(skb)->dccpd_type;
  225. return type == DCCP_PKT_DATA || type == DCCP_PKT_REQUEST;
  226. }
  227. #define DCCP_MAX_SEQNO ((((u64)1) << 48) - 1)
  228. #define DCCP_PKT_WITHOUT_ACK_SEQ (DCCP_MAX_SEQNO << 2)
  229. static inline void dccp_set_seqno(u64 *seqno, u64 value)
  230. {
  231. if (value > DCCP_MAX_SEQNO)
  232. value -= DCCP_MAX_SEQNO + 1;
  233. *seqno = value;
  234. }
  235. static inline u64 dccp_delta_seqno(u64 seqno1, u64 seqno2)
  236. {
  237. return ((seqno2 << 16) - (seqno1 << 16)) >> 16;
  238. }
  239. static inline void dccp_inc_seqno(u64 *seqno)
  240. {
  241. if (++*seqno > DCCP_MAX_SEQNO)
  242. *seqno = 0;
  243. }
  244. static inline void dccp_hdr_set_seq(struct dccp_hdr *dh, const u64 gss)
  245. {
  246. struct dccp_hdr_ext *dhx = (struct dccp_hdr_ext *)((void *)dh + sizeof(*dh));
  247. #if defined(__LITTLE_ENDIAN_BITFIELD)
  248. dh->dccph_seq = htonl((gss >> 32)) >> 8;
  249. #elif defined(__BIG_ENDIAN_BITFIELD)
  250. dh->dccph_seq = htonl((gss >> 32));
  251. #else
  252. #error "Adjust your <asm/byteorder.h> defines"
  253. #endif
  254. dhx->dccph_seq_low = htonl(gss & 0xffffffff);
  255. }
  256. static inline void dccp_hdr_set_ack(struct dccp_hdr_ack_bits *dhack, const u64 gsr)
  257. {
  258. #if defined(__LITTLE_ENDIAN_BITFIELD)
  259. dhack->dccph_ack_nr_high = htonl((gsr >> 32)) >> 8;
  260. #elif defined(__BIG_ENDIAN_BITFIELD)
  261. dhack->dccph_ack_nr_high = htonl((gsr >> 32));
  262. #else
  263. #error "Adjust your <asm/byteorder.h> defines"
  264. #endif
  265. dhack->dccph_ack_nr_low = htonl(gsr & 0xffffffff);
  266. }
  267. static inline void dccp_update_gsr(struct sock *sk, u64 seq)
  268. {
  269. struct dccp_sock *dp = dccp_sk(sk);
  270. u64 tmp_gsr;
  271. dccp_set_seqno(&tmp_gsr, dp->dccps_gsr + 1 - (dp->dccps_options.dccpo_sequence_window / 4));
  272. dp->dccps_gsr = seq;
  273. dccp_set_seqno(&dp->dccps_swl, max48(tmp_gsr, dp->dccps_isr));
  274. dccp_set_seqno(&dp->dccps_swh,
  275. dp->dccps_gsr + (3 * dp->dccps_options.dccpo_sequence_window) / 4);
  276. }
  277. static inline void dccp_update_gss(struct sock *sk, u64 seq)
  278. {
  279. struct dccp_sock *dp = dccp_sk(sk);
  280. u64 tmp_gss;
  281. dccp_set_seqno(&tmp_gss, dp->dccps_gss - dp->dccps_options.dccpo_sequence_window + 1);
  282. dp->dccps_awl = max48(tmp_gss, dp->dccps_iss);
  283. dp->dccps_awh = dp->dccps_gss = seq;
  284. }
  285. extern void dccp_insert_options(struct sock *sk, struct sk_buff *skb);
  286. extern void dccp_insert_option_elapsed_time(struct sock *sk,
  287. struct sk_buff *skb,
  288. u32 elapsed_time);
  289. extern void dccp_insert_option(struct sock *sk, struct sk_buff *skb,
  290. unsigned char option,
  291. const void *value, unsigned char len);
  292. extern struct socket *dccp_ctl_socket;
  293. #define DCCP_ACKPKTS_STATE_RECEIVED 0
  294. #define DCCP_ACKPKTS_STATE_ECN_MARKED (1 << 6)
  295. #define DCCP_ACKPKTS_STATE_NOT_RECEIVED (3 << 6)
  296. #define DCCP_ACKPKTS_STATE_MASK 0xC0 /* 11000000 */
  297. #define DCCP_ACKPKTS_LEN_MASK 0x3F /* 00111111 */
  298. /** struct dccp_ackpkts - acknowledgeable packets
  299. *
  300. * This data structure is the one defined in the DCCP draft
  301. * Appendix A.
  302. *
  303. * @dccpap_buf_head - circular buffer head
  304. * @dccpap_buf_tail - circular buffer tail
  305. * @dccpap_buf_ackno - ack # of the most recent packet acknoldgeable in the buffer (i.e. %dccpap_buf_head)
  306. * @dccpap_buf_nonce - the one-bit sum of the ECN Nonces on all packets acked by the buffer with State 0
  307. *
  308. * Additionally, the HC-Receiver must keep some information about the
  309. * Ack Vectors it has recently sent. For each packet sent carrying an
  310. * Ack Vector, it remembers four variables:
  311. *
  312. * @dccpap_ack_seqno - the Sequence Number used for the packet (HC-Receiver seqno)
  313. * @dccpap_ack_ptr - the value of buf_head at the time of acknowledgement.
  314. * @dccpap_ack_ackno - the Acknowledgement Number used for the packet (HC-Sender seqno)
  315. * @dccpap_ack_nonce - the one-bit sum of the ECN Nonces for all State 0.
  316. *
  317. * @dccpap_buf_len - circular buffer length
  318. * @dccpap_buf - circular buffer of acknowledgeable packets
  319. */
  320. struct dccp_ackpkts {
  321. unsigned int dccpap_buf_head;
  322. unsigned int dccpap_buf_tail;
  323. u64 dccpap_buf_ackno;
  324. u64 dccpap_ack_seqno;
  325. u64 dccpap_ack_ackno;
  326. unsigned int dccpap_ack_ptr;
  327. unsigned int dccpap_buf_vector_len;
  328. unsigned int dccpap_ack_vector_len;
  329. unsigned int dccpap_buf_len;
  330. unsigned long dccpap_time;
  331. u8 dccpap_buf_nonce;
  332. u8 dccpap_ack_nonce;
  333. u8 dccpap_buf[0];
  334. };
  335. extern struct dccp_ackpkts *dccp_ackpkts_alloc(unsigned int len, int priority);
  336. extern void dccp_ackpkts_free(struct dccp_ackpkts *ap);
  337. extern int dccp_ackpkts_add(struct dccp_ackpkts *ap, u64 ackno, u8 state);
  338. extern void dccp_ackpkts_check_rcv_ackno(struct dccp_ackpkts *ap,
  339. struct sock *sk, u64 ackno);
  340. #ifdef DCCP_DEBUG
  341. extern void dccp_ackvector_print(const u64 ackno,
  342. const unsigned char *vector, int len);
  343. extern void dccp_ackpkts_print(const struct dccp_ackpkts *ap);
  344. #else
  345. static inline void dccp_ackvector_print(const u64 ackno,
  346. const unsigned char *vector,
  347. int len) { }
  348. static inline void dccp_ackpkts_print(const struct dccp_ackpkts *ap) { }
  349. #endif
  350. #endif /* _DCCP_H */