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