dccp.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  1. #ifndef _LINUX_DCCP_H
  2. #define _LINUX_DCCP_H
  3. #include <linux/types.h>
  4. #include <asm/byteorder.h>
  5. /* FIXME: this is utterly wrong */
  6. struct sockaddr_dccp {
  7. struct sockaddr_in in;
  8. unsigned int service;
  9. };
  10. /**
  11. * struct dccp_hdr - generic part of DCCP packet header
  12. *
  13. * @dccph_sport - Relevant port on the endpoint that sent this packet
  14. * @dccph_dport - Relevant port on the other endpoint
  15. * @dccph_doff - Data Offset from the start of the DCCP header, in 32-bit words
  16. * @dccph_ccval - Used by the HC-Sender CCID
  17. * @dccph_cscov - Parts of the packet that are covered by the Checksum field
  18. * @dccph_checksum - Internet checksum, depends on dccph_cscov
  19. * @dccph_x - 0 = 24 bit sequence number, 1 = 48
  20. * @dccph_type - packet type, see DCCP_PKT_ prefixed macros
  21. * @dccph_seq - sequence number high or low order 24 bits, depends on dccph_x
  22. */
  23. struct dccp_hdr {
  24. __u16 dccph_sport,
  25. dccph_dport;
  26. __u8 dccph_doff;
  27. #if defined(__LITTLE_ENDIAN_BITFIELD)
  28. __u8 dccph_cscov:4,
  29. dccph_ccval:4;
  30. #elif defined(__BIG_ENDIAN_BITFIELD)
  31. __u8 dccph_ccval:4,
  32. dccph_cscov:4;
  33. #else
  34. #error "Adjust your <asm/byteorder.h> defines"
  35. #endif
  36. __u16 dccph_checksum;
  37. #if defined(__LITTLE_ENDIAN_BITFIELD)
  38. __u32 dccph_x:1,
  39. dccph_type:4,
  40. dccph_reserved:3,
  41. dccph_seq:24;
  42. #elif defined(__BIG_ENDIAN_BITFIELD)
  43. __u32 dccph_reserved:3,
  44. dccph_type:4,
  45. dccph_x:1,
  46. dccph_seq:24;
  47. #else
  48. #error "Adjust your <asm/byteorder.h> defines"
  49. #endif
  50. };
  51. /**
  52. * struct dccp_hdr_ext - the low bits of a 48 bit seq packet
  53. *
  54. * @dccph_seq_low - low 24 bits of a 48 bit seq packet
  55. */
  56. struct dccp_hdr_ext {
  57. __u32 dccph_seq_low;
  58. };
  59. /**
  60. * struct dccp_hdr_request - Conection initiation request header
  61. *
  62. * @dccph_req_service - Service to which the client app wants to connect
  63. * @dccph_req_options - list of options (must be a multiple of 32 bits
  64. */
  65. struct dccp_hdr_request {
  66. __u32 dccph_req_service;
  67. };
  68. /**
  69. * struct dccp_hdr_ack_bits - acknowledgment bits common to most packets
  70. *
  71. * @dccph_resp_ack_nr_high - 48 bit ack number high order bits, contains GSR
  72. * @dccph_resp_ack_nr_low - 48 bit ack number low order bits, contains GSR
  73. */
  74. struct dccp_hdr_ack_bits {
  75. __u32 dccph_reserved1:8,
  76. dccph_ack_nr_high:24;
  77. __u32 dccph_ack_nr_low;
  78. };
  79. /**
  80. * struct dccp_hdr_response - Conection initiation response header
  81. *
  82. * @dccph_resp_ack_nr_high - 48 bit ack number high order bits, contains GSR
  83. * @dccph_resp_ack_nr_low - 48 bit ack number low order bits, contains GSR
  84. * @dccph_resp_service - Echoes the Service Code on a received DCCP-Request
  85. * @dccph_resp_options - list of options (must be a multiple of 32 bits
  86. */
  87. struct dccp_hdr_response {
  88. struct dccp_hdr_ack_bits dccph_resp_ack;
  89. __u32 dccph_resp_service;
  90. };
  91. /**
  92. * struct dccp_hdr_reset - Unconditionally shut down a connection
  93. *
  94. * @dccph_reset_service - Echoes the Service Code on a received DCCP-Request
  95. * @dccph_reset_options - list of options (must be a multiple of 32 bits
  96. */
  97. struct dccp_hdr_reset {
  98. struct dccp_hdr_ack_bits dccph_reset_ack;
  99. __u8 dccph_reset_code,
  100. dccph_reset_data[3];
  101. };
  102. enum dccp_pkt_type {
  103. DCCP_PKT_REQUEST = 0,
  104. DCCP_PKT_RESPONSE,
  105. DCCP_PKT_DATA,
  106. DCCP_PKT_ACK,
  107. DCCP_PKT_DATAACK,
  108. DCCP_PKT_CLOSEREQ,
  109. DCCP_PKT_CLOSE,
  110. DCCP_PKT_RESET,
  111. DCCP_PKT_SYNC,
  112. DCCP_PKT_SYNCACK,
  113. DCCP_PKT_INVALID,
  114. };
  115. #define DCCP_NR_PKT_TYPES DCCP_PKT_INVALID
  116. static inline unsigned int dccp_packet_hdr_len(const __u8 type)
  117. {
  118. if (type == DCCP_PKT_DATA)
  119. return 0;
  120. if (type == DCCP_PKT_DATAACK ||
  121. type == DCCP_PKT_ACK ||
  122. type == DCCP_PKT_SYNC ||
  123. type == DCCP_PKT_SYNCACK ||
  124. type == DCCP_PKT_CLOSE ||
  125. type == DCCP_PKT_CLOSEREQ)
  126. return sizeof(struct dccp_hdr_ack_bits);
  127. if (type == DCCP_PKT_REQUEST)
  128. return sizeof(struct dccp_hdr_request);
  129. if (type == DCCP_PKT_RESPONSE)
  130. return sizeof(struct dccp_hdr_response);
  131. return sizeof(struct dccp_hdr_reset);
  132. }
  133. enum dccp_reset_codes {
  134. DCCP_RESET_CODE_UNSPECIFIED = 0,
  135. DCCP_RESET_CODE_CLOSED,
  136. DCCP_RESET_CODE_ABORTED,
  137. DCCP_RESET_CODE_NO_CONNECTION,
  138. DCCP_RESET_CODE_PACKET_ERROR,
  139. DCCP_RESET_CODE_OPTION_ERROR,
  140. DCCP_RESET_CODE_MANDATORY_ERROR,
  141. DCCP_RESET_CODE_CONNECTION_REFUSED,
  142. DCCP_RESET_CODE_BAD_SERVICE_CODE,
  143. DCCP_RESET_CODE_TOO_BUSY,
  144. DCCP_RESET_CODE_BAD_INIT_COOKIE,
  145. DCCP_RESET_CODE_AGGRESSION_PENALTY,
  146. };
  147. /* DCCP options */
  148. enum {
  149. DCCPO_PADDING = 0,
  150. DCCPO_MANDATORY = 1,
  151. DCCPO_MIN_RESERVED = 3,
  152. DCCPO_MAX_RESERVED = 31,
  153. DCCPO_NDP_COUNT = 37,
  154. DCCPO_ACK_VECTOR_0 = 38,
  155. DCCPO_ACK_VECTOR_1 = 39,
  156. DCCPO_TIMESTAMP = 41,
  157. DCCPO_TIMESTAMP_ECHO = 42,
  158. DCCPO_ELAPSED_TIME = 43,
  159. DCCPO_MAX = 45,
  160. DCCPO_MIN_CCID_SPECIFIC = 128,
  161. DCCPO_MAX_CCID_SPECIFIC = 255,
  162. };
  163. /* DCCP features */
  164. enum {
  165. DCCPF_RESERVED = 0,
  166. DCCPF_SEQUENCE_WINDOW = 3,
  167. DCCPF_SEND_ACK_VECTOR = 6,
  168. DCCPF_SEND_NDP_COUNT = 7,
  169. /* 10-127 reserved */
  170. DCCPF_MIN_CCID_SPECIFIC = 128,
  171. DCCPF_MAX_CCID_SPECIFIC = 255,
  172. };
  173. #ifdef __KERNEL__
  174. #include <linux/in.h>
  175. #include <linux/list.h>
  176. #include <linux/uio.h>
  177. #include <linux/workqueue.h>
  178. #include <net/inet_connection_sock.h>
  179. #include <net/sock.h>
  180. #include <net/tcp_states.h>
  181. #include <net/tcp.h>
  182. enum dccp_state {
  183. DCCP_OPEN = TCP_ESTABLISHED,
  184. DCCP_REQUESTING = TCP_SYN_SENT,
  185. DCCP_PARTOPEN = TCP_FIN_WAIT1, /* FIXME:
  186. This mapping is horrible, but TCP has
  187. no matching state for DCCP_PARTOPEN,
  188. as TCP_SYN_RECV is already used by
  189. DCCP_RESPOND, why don't stop using TCP
  190. mapping of states? OK, now we don't use
  191. sk_stream_sendmsg anymore, so doesn't
  192. seem to exist any reason for us to
  193. do the TCP mapping here */
  194. DCCP_LISTEN = TCP_LISTEN,
  195. DCCP_RESPOND = TCP_SYN_RECV,
  196. DCCP_CLOSING = TCP_CLOSING,
  197. DCCP_TIME_WAIT = TCP_TIME_WAIT,
  198. DCCP_CLOSED = TCP_CLOSE,
  199. DCCP_MAX_STATES = TCP_MAX_STATES,
  200. };
  201. #define DCCP_STATE_MASK 0xf
  202. #define DCCP_ACTION_FIN (1<<7)
  203. enum {
  204. DCCPF_OPEN = TCPF_ESTABLISHED,
  205. DCCPF_REQUESTING = TCPF_SYN_SENT,
  206. DCCPF_PARTOPEN = TCPF_FIN_WAIT1,
  207. DCCPF_LISTEN = TCPF_LISTEN,
  208. DCCPF_RESPOND = TCPF_SYN_RECV,
  209. DCCPF_CLOSING = TCPF_CLOSING,
  210. DCCPF_TIME_WAIT = TCPF_TIME_WAIT,
  211. DCCPF_CLOSED = TCPF_CLOSE,
  212. };
  213. static inline struct dccp_hdr *dccp_hdr(const struct sk_buff *skb)
  214. {
  215. return (struct dccp_hdr *)skb->h.raw;
  216. }
  217. static inline struct dccp_hdr_ext *dccp_hdrx(const struct sk_buff *skb)
  218. {
  219. return (struct dccp_hdr_ext *)(skb->h.raw + sizeof(struct dccp_hdr));
  220. }
  221. static inline unsigned int dccp_basic_hdr_len(const struct sk_buff *skb)
  222. {
  223. const struct dccp_hdr *dh = dccp_hdr(skb);
  224. return sizeof(*dh) + (dh->dccph_x ? sizeof(struct dccp_hdr_ext) : 0);
  225. }
  226. static inline __u64 dccp_hdr_seq(const struct sk_buff *skb)
  227. {
  228. const struct dccp_hdr *dh = dccp_hdr(skb);
  229. #if defined(__LITTLE_ENDIAN_BITFIELD)
  230. __u64 seq_nr = ntohl(dh->dccph_seq << 8);
  231. #elif defined(__BIG_ENDIAN_BITFIELD)
  232. __u64 seq_nr = ntohl(dh->dccph_seq);
  233. #else
  234. #error "Adjust your <asm/byteorder.h> defines"
  235. #endif
  236. if (dh->dccph_x != 0)
  237. seq_nr = (seq_nr << 32) + ntohl(dccp_hdrx(skb)->dccph_seq_low);
  238. return seq_nr;
  239. }
  240. static inline struct dccp_hdr_request *dccp_hdr_request(struct sk_buff *skb)
  241. {
  242. return (struct dccp_hdr_request *)(skb->h.raw + dccp_basic_hdr_len(skb));
  243. }
  244. static inline struct dccp_hdr_ack_bits *dccp_hdr_ack_bits(const struct sk_buff *skb)
  245. {
  246. return (struct dccp_hdr_ack_bits *)(skb->h.raw + dccp_basic_hdr_len(skb));
  247. }
  248. static inline u64 dccp_hdr_ack_seq(const struct sk_buff *skb)
  249. {
  250. const struct dccp_hdr_ack_bits *dhack = dccp_hdr_ack_bits(skb);
  251. #if defined(__LITTLE_ENDIAN_BITFIELD)
  252. return (((u64)ntohl(dhack->dccph_ack_nr_high << 8)) << 32) + ntohl(dhack->dccph_ack_nr_low);
  253. #elif defined(__BIG_ENDIAN_BITFIELD)
  254. return (((u64)ntohl(dhack->dccph_ack_nr_high)) << 32) + ntohl(dhack->dccph_ack_nr_low);
  255. #else
  256. #error "Adjust your <asm/byteorder.h> defines"
  257. #endif
  258. }
  259. static inline struct dccp_hdr_response *dccp_hdr_response(struct sk_buff *skb)
  260. {
  261. return (struct dccp_hdr_response *)(skb->h.raw + dccp_basic_hdr_len(skb));
  262. }
  263. static inline struct dccp_hdr_reset *dccp_hdr_reset(struct sk_buff *skb)
  264. {
  265. return (struct dccp_hdr_reset *)(skb->h.raw + dccp_basic_hdr_len(skb));
  266. }
  267. static inline unsigned int dccp_hdr_len(const struct sk_buff *skb)
  268. {
  269. return dccp_basic_hdr_len(skb) +
  270. dccp_packet_hdr_len(dccp_hdr(skb)->dccph_type);
  271. }
  272. /* initial values for each feature */
  273. #define DCCPF_INITIAL_SEQUENCE_WINDOW 100
  274. /* FIXME: for now we're using CCID 3 (TFRC) */
  275. #define DCCPF_INITIAL_CCID 3
  276. #define DCCPF_INITIAL_SEND_ACK_VECTOR 0
  277. /* FIXME: for now we're default to 1 but it should really be 0 */
  278. #define DCCPF_INITIAL_SEND_NDP_COUNT 1
  279. #define DCCP_NDP_LIMIT 0xFFFFFF
  280. /**
  281. * struct dccp_options - option values for a DCCP connection
  282. * @dccpo_sequence_window - Sequence Window Feature (section 7.5.2)
  283. * @dccpo_ccid - Congestion Control Id (CCID) (section 10)
  284. * @dccpo_send_ack_vector - Send Ack Vector Feature (section 11.5)
  285. * @dccpo_send_ndp_count - Send NDP Count Feature (7.7.2)
  286. */
  287. struct dccp_options {
  288. __u64 dccpo_sequence_window;
  289. __u8 dccpo_ccid;
  290. __u8 dccpo_send_ack_vector;
  291. __u8 dccpo_send_ndp_count;
  292. };
  293. extern void __dccp_options_init(struct dccp_options *dccpo);
  294. extern void dccp_options_init(struct dccp_options *dccpo);
  295. extern int dccp_parse_options(struct sock *sk, struct sk_buff *skb);
  296. struct dccp_request_sock {
  297. struct inet_request_sock dreq_inet_rsk;
  298. __u64 dreq_iss;
  299. __u64 dreq_isr;
  300. __u32 dreq_service;
  301. };
  302. static inline struct dccp_request_sock *dccp_rsk(const struct request_sock *req)
  303. {
  304. return (struct dccp_request_sock *)req;
  305. }
  306. /* Read about the ECN nonce to see why it is 253 */
  307. #define DCCP_MAX_ACK_VECTOR_LEN 253
  308. struct dccp_options_received {
  309. u32 dccpor_ndp:24,
  310. dccpor_ack_vector_len:8;
  311. u32 dccpor_ack_vector_idx:10;
  312. /* 22 bits hole, try to pack */
  313. u32 dccpor_timestamp;
  314. u32 dccpor_timestamp_echo;
  315. u32 dccpor_elapsed_time;
  316. };
  317. struct ccid;
  318. enum dccp_role {
  319. DCCP_ROLE_UNDEFINED,
  320. DCCP_ROLE_LISTEN,
  321. DCCP_ROLE_CLIENT,
  322. DCCP_ROLE_SERVER,
  323. };
  324. /**
  325. * struct dccp_sock - DCCP socket state
  326. *
  327. * @dccps_swl - sequence number window low
  328. * @dccps_swh - sequence number window high
  329. * @dccps_awl - acknowledgement number window low
  330. * @dccps_awh - acknowledgement number window high
  331. * @dccps_iss - initial sequence number sent
  332. * @dccps_isr - initial sequence number received
  333. * @dccps_osr - first OPEN sequence number received
  334. * @dccps_gss - greatest sequence number sent
  335. * @dccps_gsr - greatest valid sequence number received
  336. * @dccps_gar - greatest valid ack number received on a non-Sync; initialized to %dccps_iss
  337. * @dccps_timestamp_time - time of latest TIMESTAMP option
  338. * @dccps_timestamp_echo - latest timestamp received on a TIMESTAMP option
  339. * @dccps_ext_header_len - network protocol overhead (IP/IPv6 options)
  340. * @dccps_pmtu_cookie - Last pmtu seen by socket
  341. * @dccps_avg_packet_size - FIXME: has to be set by the app thru some setsockopt or ioctl, CCID3 uses it
  342. * @dccps_role - Role of this sock, one of %dccp_role
  343. * @dccps_ndp_count - number of Non Data Packets since last data packet
  344. * @dccps_hc_rx_ackpkts - receiver half connection acked packets
  345. */
  346. struct dccp_sock {
  347. /* inet_connection_sock has to be the first member of dccp_sock */
  348. struct inet_connection_sock dccps_inet_connection;
  349. __u64 dccps_swl;
  350. __u64 dccps_swh;
  351. __u64 dccps_awl;
  352. __u64 dccps_awh;
  353. __u64 dccps_iss;
  354. __u64 dccps_isr;
  355. __u64 dccps_osr;
  356. __u64 dccps_gss;
  357. __u64 dccps_gsr;
  358. __u64 dccps_gar;
  359. unsigned long dccps_service;
  360. unsigned long dccps_timestamp_time;
  361. __u32 dccps_timestamp_echo;
  362. __u32 dccps_avg_packet_size;
  363. unsigned long dccps_ndp_count;
  364. __u16 dccps_ext_header_len;
  365. __u32 dccps_pmtu_cookie;
  366. __u32 dccps_mss_cache;
  367. struct dccp_options dccps_options;
  368. struct dccp_ackpkts *dccps_hc_rx_ackpkts;
  369. void *dccps_hc_rx_ccid_private;
  370. void *dccps_hc_tx_ccid_private;
  371. struct ccid *dccps_hc_rx_ccid;
  372. struct ccid *dccps_hc_tx_ccid;
  373. struct dccp_options_received dccps_options_received;
  374. enum dccp_role dccps_role:2;
  375. };
  376. static inline struct dccp_sock *dccp_sk(const struct sock *sk)
  377. {
  378. return (struct dccp_sock *)sk;
  379. }
  380. static inline const char *dccp_role(const struct sock *sk)
  381. {
  382. switch (dccp_sk(sk)->dccps_role) {
  383. case DCCP_ROLE_UNDEFINED: return "undefined";
  384. case DCCP_ROLE_LISTEN: return "listen";
  385. case DCCP_ROLE_SERVER: return "server";
  386. case DCCP_ROLE_CLIENT: return "client";
  387. }
  388. return NULL;
  389. }
  390. #endif /* __KERNEL__ */
  391. #endif /* _LINUX_DCCP_H */