ccid.h 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. /* SPDX-License-Identifier: GPL-2.0-only */
  2. #ifndef _CCID_H
  3. #define _CCID_H
  4. /*
  5. * net/dccp/ccid.h
  6. *
  7. * An implementation of the DCCP protocol
  8. * Arnaldo Carvalho de Melo <[email protected]>
  9. *
  10. * CCID infrastructure
  11. */
  12. #include <net/sock.h>
  13. #include <linux/compiler.h>
  14. #include <linux/dccp.h>
  15. #include <linux/list.h>
  16. #include <linux/module.h>
  17. /* maximum value for a CCID (RFC 4340, 19.5) */
  18. #define CCID_MAX 255
  19. #define CCID_SLAB_NAME_LENGTH 32
  20. struct tcp_info;
  21. /**
  22. * struct ccid_operations - Interface to Congestion-Control Infrastructure
  23. *
  24. * @ccid_id: numerical CCID ID (up to %CCID_MAX, cf. table 5 in RFC 4340, 10.)
  25. * @ccid_ccmps: the CCMPS including network/transport headers (0 when disabled)
  26. * @ccid_name: alphabetical identifier string for @ccid_id
  27. * @ccid_hc_{r,t}x_slab: memory pool for the receiver/sender half-connection
  28. * @ccid_hc_{r,t}x_obj_size: size of the receiver/sender half-connection socket
  29. *
  30. * @ccid_hc_{r,t}x_init: CCID-specific initialisation routine (before startup)
  31. * @ccid_hc_{r,t}x_exit: CCID-specific cleanup routine (before destruction)
  32. * @ccid_hc_rx_packet_recv: implements the HC-receiver side
  33. * @ccid_hc_{r,t}x_parse_options: parsing routine for CCID/HC-specific options
  34. * @ccid_hc_{r,t}x_insert_options: insert routine for CCID/HC-specific options
  35. * @ccid_hc_tx_packet_recv: implements feedback processing for the HC-sender
  36. * @ccid_hc_tx_send_packet: implements the sending part of the HC-sender
  37. * @ccid_hc_tx_packet_sent: does accounting for packets in flight by HC-sender
  38. * @ccid_hc_{r,t}x_get_info: INET_DIAG information for HC-receiver/sender
  39. * @ccid_hc_{r,t}x_getsockopt: socket options specific to HC-receiver/sender
  40. */
  41. struct ccid_operations {
  42. unsigned char ccid_id;
  43. __u32 ccid_ccmps;
  44. const char *ccid_name;
  45. struct kmem_cache *ccid_hc_rx_slab,
  46. *ccid_hc_tx_slab;
  47. char ccid_hc_rx_slab_name[CCID_SLAB_NAME_LENGTH];
  48. char ccid_hc_tx_slab_name[CCID_SLAB_NAME_LENGTH];
  49. __u32 ccid_hc_rx_obj_size,
  50. ccid_hc_tx_obj_size;
  51. /* Interface Routines */
  52. int (*ccid_hc_rx_init)(struct ccid *ccid, struct sock *sk);
  53. int (*ccid_hc_tx_init)(struct ccid *ccid, struct sock *sk);
  54. void (*ccid_hc_rx_exit)(struct sock *sk);
  55. void (*ccid_hc_tx_exit)(struct sock *sk);
  56. void (*ccid_hc_rx_packet_recv)(struct sock *sk,
  57. struct sk_buff *skb);
  58. int (*ccid_hc_rx_parse_options)(struct sock *sk, u8 pkt,
  59. u8 opt, u8 *val, u8 len);
  60. int (*ccid_hc_rx_insert_options)(struct sock *sk,
  61. struct sk_buff *skb);
  62. void (*ccid_hc_tx_packet_recv)(struct sock *sk,
  63. struct sk_buff *skb);
  64. int (*ccid_hc_tx_parse_options)(struct sock *sk, u8 pkt,
  65. u8 opt, u8 *val, u8 len);
  66. int (*ccid_hc_tx_send_packet)(struct sock *sk,
  67. struct sk_buff *skb);
  68. void (*ccid_hc_tx_packet_sent)(struct sock *sk,
  69. unsigned int len);
  70. void (*ccid_hc_rx_get_info)(struct sock *sk,
  71. struct tcp_info *info);
  72. void (*ccid_hc_tx_get_info)(struct sock *sk,
  73. struct tcp_info *info);
  74. int (*ccid_hc_rx_getsockopt)(struct sock *sk,
  75. const int optname, int len,
  76. u32 __user *optval,
  77. int __user *optlen);
  78. int (*ccid_hc_tx_getsockopt)(struct sock *sk,
  79. const int optname, int len,
  80. u32 __user *optval,
  81. int __user *optlen);
  82. };
  83. extern struct ccid_operations ccid2_ops;
  84. #ifdef CONFIG_IP_DCCP_CCID3
  85. extern struct ccid_operations ccid3_ops;
  86. #endif
  87. int ccid_initialize_builtins(void);
  88. void ccid_cleanup_builtins(void);
  89. struct ccid {
  90. struct ccid_operations *ccid_ops;
  91. char ccid_priv[];
  92. };
  93. static inline void *ccid_priv(const struct ccid *ccid)
  94. {
  95. return (void *)ccid->ccid_priv;
  96. }
  97. bool ccid_support_check(u8 const *ccid_array, u8 array_len);
  98. int ccid_get_builtin_ccids(u8 **ccid_array, u8 *array_len);
  99. int ccid_getsockopt_builtin_ccids(struct sock *sk, int len,
  100. char __user *, int __user *);
  101. struct ccid *ccid_new(const u8 id, struct sock *sk, bool rx);
  102. static inline int ccid_get_current_rx_ccid(struct dccp_sock *dp)
  103. {
  104. struct ccid *ccid = dp->dccps_hc_rx_ccid;
  105. if (ccid == NULL || ccid->ccid_ops == NULL)
  106. return -1;
  107. return ccid->ccid_ops->ccid_id;
  108. }
  109. static inline int ccid_get_current_tx_ccid(struct dccp_sock *dp)
  110. {
  111. struct ccid *ccid = dp->dccps_hc_tx_ccid;
  112. if (ccid == NULL || ccid->ccid_ops == NULL)
  113. return -1;
  114. return ccid->ccid_ops->ccid_id;
  115. }
  116. void ccid_hc_rx_delete(struct ccid *ccid, struct sock *sk);
  117. void ccid_hc_tx_delete(struct ccid *ccid, struct sock *sk);
  118. /*
  119. * Congestion control of queued data packets via CCID decision.
  120. *
  121. * The TX CCID performs its congestion-control by indicating whether and when a
  122. * queued packet may be sent, using the return code of ccid_hc_tx_send_packet().
  123. * The following modes are supported via the symbolic constants below:
  124. * - timer-based pacing (CCID returns a delay value in milliseconds);
  125. * - autonomous dequeueing (CCID internally schedules dccps_xmitlet).
  126. */
  127. enum ccid_dequeueing_decision {
  128. CCID_PACKET_SEND_AT_ONCE = 0x00000, /* "green light": no delay */
  129. CCID_PACKET_DELAY_MAX = 0x0FFFF, /* maximum delay in msecs */
  130. CCID_PACKET_DELAY = 0x10000, /* CCID msec-delay mode */
  131. CCID_PACKET_WILL_DEQUEUE_LATER = 0x20000, /* CCID autonomous mode */
  132. CCID_PACKET_ERR = 0xF0000, /* error condition */
  133. };
  134. static inline int ccid_packet_dequeue_eval(const int return_code)
  135. {
  136. if (return_code < 0)
  137. return CCID_PACKET_ERR;
  138. if (return_code == 0)
  139. return CCID_PACKET_SEND_AT_ONCE;
  140. if (return_code <= CCID_PACKET_DELAY_MAX)
  141. return CCID_PACKET_DELAY;
  142. return return_code;
  143. }
  144. static inline int ccid_hc_tx_send_packet(struct ccid *ccid, struct sock *sk,
  145. struct sk_buff *skb)
  146. {
  147. if (ccid->ccid_ops->ccid_hc_tx_send_packet != NULL)
  148. return ccid->ccid_ops->ccid_hc_tx_send_packet(sk, skb);
  149. return CCID_PACKET_SEND_AT_ONCE;
  150. }
  151. static inline void ccid_hc_tx_packet_sent(struct ccid *ccid, struct sock *sk,
  152. unsigned int len)
  153. {
  154. if (ccid->ccid_ops->ccid_hc_tx_packet_sent != NULL)
  155. ccid->ccid_ops->ccid_hc_tx_packet_sent(sk, len);
  156. }
  157. static inline void ccid_hc_rx_packet_recv(struct ccid *ccid, struct sock *sk,
  158. struct sk_buff *skb)
  159. {
  160. if (ccid->ccid_ops->ccid_hc_rx_packet_recv != NULL)
  161. ccid->ccid_ops->ccid_hc_rx_packet_recv(sk, skb);
  162. }
  163. static inline void ccid_hc_tx_packet_recv(struct ccid *ccid, struct sock *sk,
  164. struct sk_buff *skb)
  165. {
  166. if (ccid->ccid_ops->ccid_hc_tx_packet_recv != NULL)
  167. ccid->ccid_ops->ccid_hc_tx_packet_recv(sk, skb);
  168. }
  169. /**
  170. * ccid_hc_tx_parse_options - Parse CCID-specific options sent by the receiver
  171. * @pkt: type of packet that @opt appears on (RFC 4340, 5.1)
  172. * @opt: the CCID-specific option type (RFC 4340, 5.8 and 10.3)
  173. * @val: value of @opt
  174. * @len: length of @val in bytes
  175. */
  176. static inline int ccid_hc_tx_parse_options(struct ccid *ccid, struct sock *sk,
  177. u8 pkt, u8 opt, u8 *val, u8 len)
  178. {
  179. if (!ccid || !ccid->ccid_ops->ccid_hc_tx_parse_options)
  180. return 0;
  181. return ccid->ccid_ops->ccid_hc_tx_parse_options(sk, pkt, opt, val, len);
  182. }
  183. /**
  184. * ccid_hc_rx_parse_options - Parse CCID-specific options sent by the sender
  185. * Arguments are analogous to ccid_hc_tx_parse_options()
  186. */
  187. static inline int ccid_hc_rx_parse_options(struct ccid *ccid, struct sock *sk,
  188. u8 pkt, u8 opt, u8 *val, u8 len)
  189. {
  190. if (!ccid || !ccid->ccid_ops->ccid_hc_rx_parse_options)
  191. return 0;
  192. return ccid->ccid_ops->ccid_hc_rx_parse_options(sk, pkt, opt, val, len);
  193. }
  194. static inline int ccid_hc_rx_insert_options(struct ccid *ccid, struct sock *sk,
  195. struct sk_buff *skb)
  196. {
  197. if (ccid->ccid_ops->ccid_hc_rx_insert_options != NULL)
  198. return ccid->ccid_ops->ccid_hc_rx_insert_options(sk, skb);
  199. return 0;
  200. }
  201. static inline void ccid_hc_rx_get_info(struct ccid *ccid, struct sock *sk,
  202. struct tcp_info *info)
  203. {
  204. if (ccid->ccid_ops->ccid_hc_rx_get_info != NULL)
  205. ccid->ccid_ops->ccid_hc_rx_get_info(sk, info);
  206. }
  207. static inline void ccid_hc_tx_get_info(struct ccid *ccid, struct sock *sk,
  208. struct tcp_info *info)
  209. {
  210. if (ccid->ccid_ops->ccid_hc_tx_get_info != NULL)
  211. ccid->ccid_ops->ccid_hc_tx_get_info(sk, info);
  212. }
  213. static inline int ccid_hc_rx_getsockopt(struct ccid *ccid, struct sock *sk,
  214. const int optname, int len,
  215. u32 __user *optval, int __user *optlen)
  216. {
  217. int rc = -ENOPROTOOPT;
  218. if (ccid != NULL && ccid->ccid_ops->ccid_hc_rx_getsockopt != NULL)
  219. rc = ccid->ccid_ops->ccid_hc_rx_getsockopt(sk, optname, len,
  220. optval, optlen);
  221. return rc;
  222. }
  223. static inline int ccid_hc_tx_getsockopt(struct ccid *ccid, struct sock *sk,
  224. const int optname, int len,
  225. u32 __user *optval, int __user *optlen)
  226. {
  227. int rc = -ENOPROTOOPT;
  228. if (ccid != NULL && ccid->ccid_ops->ccid_hc_tx_getsockopt != NULL)
  229. rc = ccid->ccid_ops->ccid_hc_tx_getsockopt(sk, optname, len,
  230. optval, optlen);
  231. return rc;
  232. }
  233. #endif /* _CCID_H */