dccp.h 13 KB

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