1 /* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */
2 #ifndef _LINUX_XFRM_H
3 #define _LINUX_XFRM_H
4 
5 #include <linux/in6.h>
6 #include <linux/types.h>
7 
8 /* All of the structures in this file may not change size as they are
9  * passed into the kernel from userspace via netlink sockets.
10  */
11 
12 /* Structure to encapsulate addresses. I do not want to use
13  * "standard" structure. My apologies.
14  */
15 typedef union {
16 	uint32_t		a4;
17 	uint32_t		a6[4];
18 	struct in6_addr	in6;
19 } xfrm_address_t;
20 
21 /* Ident of a specific xfrm_state. It is used on input to lookup
22  * the state by (spi,daddr,ah/esp) or to store information about
23  * spi, protocol and tunnel address on output.
24  */
25 struct xfrm_id {
26 	xfrm_address_t	daddr;
27 	uint32_t		spi;
28 	uint8_t		proto;
29 };
30 
31 struct xfrm_sec_ctx {
32 	uint8_t	ctx_doi;
33 	uint8_t	ctx_alg;
34 	uint16_t	ctx_len;
35 	uint32_t	ctx_sid;
36 	char	ctx_str[0];
37 };
38 
39 /* Security Context Domains of Interpretation */
40 #define XFRM_SC_DOI_RESERVED 0
41 #define XFRM_SC_DOI_LSM 1
42 
43 /* Security Context Algorithms */
44 #define XFRM_SC_ALG_RESERVED 0
45 #define XFRM_SC_ALG_SELINUX 1
46 
47 /* Selector, used as selector both on policy rules (SPD) and SAs. */
48 
49 struct xfrm_selector {
50 	xfrm_address_t	daddr;
51 	xfrm_address_t	saddr;
52 	uint16_t	dport;
53 	uint16_t	dport_mask;
54 	uint16_t	sport;
55 	uint16_t	sport_mask;
56 	uint16_t	family;
57 	uint8_t	prefixlen_d;
58 	uint8_t	prefixlen_s;
59 	uint8_t	proto;
60 	int	ifindex;
61 	__kernel_uid32_t	user;
62 };
63 
64 #define XFRM_INF (~(uint64_t)0)
65 
66 struct xfrm_lifetime_cfg {
67 	uint64_t	soft_byte_limit;
68 	uint64_t	hard_byte_limit;
69 	uint64_t	soft_packet_limit;
70 	uint64_t	hard_packet_limit;
71 	uint64_t	soft_add_expires_seconds;
72 	uint64_t	hard_add_expires_seconds;
73 	uint64_t	soft_use_expires_seconds;
74 	uint64_t	hard_use_expires_seconds;
75 };
76 
77 struct xfrm_lifetime_cur {
78 	uint64_t	bytes;
79 	uint64_t	packets;
80 	uint64_t	add_time;
81 	uint64_t	use_time;
82 };
83 
84 struct xfrm_replay_state {
85 	uint32_t	oseq;
86 	uint32_t	seq;
87 	uint32_t	bitmap;
88 };
89 
90 #define XFRMA_REPLAY_ESN_MAX	4096
91 
92 struct xfrm_replay_state_esn {
93 	unsigned int	bmp_len;
94 	uint32_t		oseq;
95 	uint32_t		seq;
96 	uint32_t		oseq_hi;
97 	uint32_t		seq_hi;
98 	uint32_t		replay_window;
99 	uint32_t		bmp[0];
100 };
101 
102 struct xfrm_algo {
103 	char		alg_name[64];
104 	unsigned int	alg_key_len;    /* in bits */
105 	char		alg_key[0];
106 };
107 
108 struct xfrm_algo_auth {
109 	char		alg_name[64];
110 	unsigned int	alg_key_len;    /* in bits */
111 	unsigned int	alg_trunc_len;  /* in bits */
112 	char		alg_key[0];
113 };
114 
115 struct xfrm_algo_aead {
116 	char		alg_name[64];
117 	unsigned int	alg_key_len;	/* in bits */
118 	unsigned int	alg_icv_len;	/* in bits */
119 	char		alg_key[0];
120 };
121 
122 struct xfrm_stats {
123 	uint32_t	replay_window;
124 	uint32_t	replay;
125 	uint32_t	integrity_failed;
126 };
127 
128 enum {
129 	XFRM_POLICY_TYPE_MAIN	= 0,
130 	XFRM_POLICY_TYPE_SUB	= 1,
131 	XFRM_POLICY_TYPE_MAX	= 2,
132 	XFRM_POLICY_TYPE_ANY	= 255
133 };
134 
135 enum {
136 	XFRM_POLICY_IN	= 0,
137 	XFRM_POLICY_OUT	= 1,
138 	XFRM_POLICY_FWD	= 2,
139 	XFRM_POLICY_MASK = 3,
140 	XFRM_POLICY_MAX	= 3
141 };
142 
143 enum {
144 	XFRM_SHARE_ANY,		/* No limitations */
145 	XFRM_SHARE_SESSION,	/* For this session only */
146 	XFRM_SHARE_USER,	/* For this user only */
147 	XFRM_SHARE_UNIQUE	/* Use once */
148 };
149 
150 #define XFRM_MODE_TRANSPORT 0
151 #define XFRM_MODE_TUNNEL 1
152 #define XFRM_MODE_ROUTEOPTIMIZATION 2
153 #define XFRM_MODE_IN_TRIGGER 3
154 #define XFRM_MODE_BEET 4
155 #define XFRM_MODE_MAX 5
156 
157 /* Netlink configuration messages.  */
158 enum {
159 	XFRM_MSG_BASE = 0x10,
160 
161 	XFRM_MSG_NEWSA = 0x10,
162 #define XFRM_MSG_NEWSA XFRM_MSG_NEWSA
163 	XFRM_MSG_DELSA,
164 #define XFRM_MSG_DELSA XFRM_MSG_DELSA
165 	XFRM_MSG_GETSA,
166 #define XFRM_MSG_GETSA XFRM_MSG_GETSA
167 
168 	XFRM_MSG_NEWPOLICY,
169 #define XFRM_MSG_NEWPOLICY XFRM_MSG_NEWPOLICY
170 	XFRM_MSG_DELPOLICY,
171 #define XFRM_MSG_DELPOLICY XFRM_MSG_DELPOLICY
172 	XFRM_MSG_GETPOLICY,
173 #define XFRM_MSG_GETPOLICY XFRM_MSG_GETPOLICY
174 
175 	XFRM_MSG_ALLOCSPI,
176 #define XFRM_MSG_ALLOCSPI XFRM_MSG_ALLOCSPI
177 	XFRM_MSG_ACQUIRE,
178 #define XFRM_MSG_ACQUIRE XFRM_MSG_ACQUIRE
179 	XFRM_MSG_EXPIRE,
180 #define XFRM_MSG_EXPIRE XFRM_MSG_EXPIRE
181 
182 	XFRM_MSG_UPDPOLICY,
183 #define XFRM_MSG_UPDPOLICY XFRM_MSG_UPDPOLICY
184 	XFRM_MSG_UPDSA,
185 #define XFRM_MSG_UPDSA XFRM_MSG_UPDSA
186 
187 	XFRM_MSG_POLEXPIRE,
188 #define XFRM_MSG_POLEXPIRE XFRM_MSG_POLEXPIRE
189 
190 	XFRM_MSG_FLUSHSA,
191 #define XFRM_MSG_FLUSHSA XFRM_MSG_FLUSHSA
192 	XFRM_MSG_FLUSHPOLICY,
193 #define XFRM_MSG_FLUSHPOLICY XFRM_MSG_FLUSHPOLICY
194 
195 	XFRM_MSG_NEWAE,
196 #define XFRM_MSG_NEWAE XFRM_MSG_NEWAE
197 	XFRM_MSG_GETAE,
198 #define XFRM_MSG_GETAE XFRM_MSG_GETAE
199 
200 	XFRM_MSG_REPORT,
201 #define XFRM_MSG_REPORT XFRM_MSG_REPORT
202 
203 	XFRM_MSG_MIGRATE,
204 #define XFRM_MSG_MIGRATE XFRM_MSG_MIGRATE
205 
206 	XFRM_MSG_NEWSADINFO,
207 #define XFRM_MSG_NEWSADINFO XFRM_MSG_NEWSADINFO
208 	XFRM_MSG_GETSADINFO,
209 #define XFRM_MSG_GETSADINFO XFRM_MSG_GETSADINFO
210 
211 	XFRM_MSG_NEWSPDINFO,
212 #define XFRM_MSG_NEWSPDINFO XFRM_MSG_NEWSPDINFO
213 	XFRM_MSG_GETSPDINFO,
214 #define XFRM_MSG_GETSPDINFO XFRM_MSG_GETSPDINFO
215 
216 	XFRM_MSG_MAPPING,
217 #define XFRM_MSG_MAPPING XFRM_MSG_MAPPING
218 	__XFRM_MSG_MAX
219 };
220 #define XFRM_MSG_MAX (__XFRM_MSG_MAX - 1)
221 
222 #define XFRM_NR_MSGTYPES (XFRM_MSG_MAX + 1 - XFRM_MSG_BASE)
223 
224 /*
225  * Generic LSM security context for comunicating to user space
226  * NOTE: Same format as sadb_x_sec_ctx
227  */
228 struct xfrm_user_sec_ctx {
229 	uint16_t			len;
230 	uint16_t			exttype;
231 	uint8_t			ctx_alg;  /* LSMs: e.g., selinux == 1 */
232 	uint8_t			ctx_doi;
233 	uint16_t			ctx_len;
234 };
235 
236 struct xfrm_user_tmpl {
237 	struct xfrm_id		id;
238 	uint16_t			family;
239 	xfrm_address_t		saddr;
240 	uint32_t			reqid;
241 	uint8_t			mode;
242 	uint8_t			share;
243 	uint8_t			optional;
244 	uint32_t			aalgos;
245 	uint32_t			ealgos;
246 	uint32_t			calgos;
247 };
248 
249 struct xfrm_encap_tmpl {
250 	uint16_t		encap_type;
251 	uint16_t		encap_sport;
252 	uint16_t		encap_dport;
253 	xfrm_address_t	encap_oa;
254 };
255 
256 /* AEVENT flags  */
257 enum xfrm_ae_ftype_t {
258 	XFRM_AE_UNSPEC,
259 	XFRM_AE_RTHR=1,	/* replay threshold*/
260 	XFRM_AE_RVAL=2, /* replay value */
261 	XFRM_AE_LVAL=4, /* lifetime value */
262 	XFRM_AE_ETHR=8, /* expiry timer threshold */
263 	XFRM_AE_CR=16, /* Event cause is replay update */
264 	XFRM_AE_CE=32, /* Event cause is timer expiry */
265 	XFRM_AE_CU=64, /* Event cause is policy update */
266 	__XFRM_AE_MAX
267 
268 #define XFRM_AE_MAX (__XFRM_AE_MAX - 1)
269 };
270 
271 struct xfrm_userpolicy_type {
272 	uint8_t		type;
273 	uint16_t		reserved1;
274 	uint8_t		reserved2;
275 };
276 
277 /* Netlink message attributes.  */
278 enum xfrm_attr_type_t {
279 	XFRMA_UNSPEC,
280 	XFRMA_ALG_AUTH,		/* struct xfrm_algo */
281 	XFRMA_ALG_CRYPT,	/* struct xfrm_algo */
282 	XFRMA_ALG_COMP,		/* struct xfrm_algo */
283 	XFRMA_ENCAP,		/* struct xfrm_algo + struct xfrm_encap_tmpl */
284 	XFRMA_TMPL,		/* 1 or more struct xfrm_user_tmpl */
285 	XFRMA_SA,		/* struct xfrm_usersa_info  */
286 	XFRMA_POLICY,		/*struct xfrm_userpolicy_info */
287 	XFRMA_SEC_CTX,		/* struct xfrm_sec_ctx */
288 	XFRMA_LTIME_VAL,
289 	XFRMA_REPLAY_VAL,
290 	XFRMA_REPLAY_THRESH,
291 	XFRMA_ETIMER_THRESH,
292 	XFRMA_SRCADDR,		/* xfrm_address_t */
293 	XFRMA_COADDR,		/* xfrm_address_t */
294 	XFRMA_LASTUSED,		/* unsigned long  */
295 	XFRMA_POLICY_TYPE,	/* struct xfrm_userpolicy_type */
296 	XFRMA_MIGRATE,
297 	XFRMA_ALG_AEAD,		/* struct xfrm_algo_aead */
298 	XFRMA_KMADDRESS,        /* struct xfrm_user_kmaddress */
299 	XFRMA_ALG_AUTH_TRUNC,	/* struct xfrm_algo_auth */
300 	XFRMA_MARK,		/* struct xfrm_mark */
301 	XFRMA_TFCPAD,		/* uint32_t */
302 	XFRMA_REPLAY_ESN_VAL,	/* struct xfrm_replay_state_esn */
303 	XFRMA_SA_EXTRA_FLAGS,	/* uint32_t */
304 	XFRMA_PROTO,		/* uint8_t */
305 	XFRMA_ADDRESS_FILTER,	/* struct xfrm_address_filter */
306 	XFRMA_PAD,
307 	XFRMA_OFFLOAD_DEV,	/* struct xfrm_user_offload */
308 	XFRMA_SET_MARK,		/* uint32_t */
309 	XFRMA_SET_MARK_MASK,	/* uint32_t */
310 	XFRMA_IF_ID,		/* uint32_t */
311 	__XFRMA_MAX
312 
313 #define XFRMA_OUTPUT_MARK XFRMA_SET_MARK	/* Compatibility */
314 #define XFRMA_MAX (__XFRMA_MAX - 1)
315 };
316 
317 struct xfrm_mark {
318 	uint32_t           v; /* value */
319 	uint32_t           m; /* mask */
320 };
321 
322 enum xfrm_sadattr_type_t {
323 	XFRMA_SAD_UNSPEC,
324 	XFRMA_SAD_CNT,
325 	XFRMA_SAD_HINFO,
326 	__XFRMA_SAD_MAX
327 
328 #define XFRMA_SAD_MAX (__XFRMA_SAD_MAX - 1)
329 };
330 
331 struct xfrmu_sadhinfo {
332 	uint32_t sadhcnt; /* current hash bkts */
333 	uint32_t sadhmcnt; /* max allowed hash bkts */
334 };
335 
336 enum xfrm_spdattr_type_t {
337 	XFRMA_SPD_UNSPEC,
338 	XFRMA_SPD_INFO,
339 	XFRMA_SPD_HINFO,
340 	XFRMA_SPD_IPV4_HTHRESH,
341 	XFRMA_SPD_IPV6_HTHRESH,
342 	__XFRMA_SPD_MAX
343 
344 #define XFRMA_SPD_MAX (__XFRMA_SPD_MAX - 1)
345 };
346 
347 struct xfrmu_spdinfo {
348 	uint32_t incnt;
349 	uint32_t outcnt;
350 	uint32_t fwdcnt;
351 	uint32_t inscnt;
352 	uint32_t outscnt;
353 	uint32_t fwdscnt;
354 };
355 
356 struct xfrmu_spdhinfo {
357 	uint32_t spdhcnt;
358 	uint32_t spdhmcnt;
359 };
360 
361 struct xfrmu_spdhthresh {
362 	uint8_t lbits;
363 	uint8_t rbits;
364 };
365 
366 struct xfrm_usersa_info {
367 	struct xfrm_selector		sel;
368 	struct xfrm_id			id;
369 	xfrm_address_t			saddr;
370 	struct xfrm_lifetime_cfg	lft;
371 	struct xfrm_lifetime_cur	curlft;
372 	struct xfrm_stats		stats;
373 	uint32_t				seq;
374 	uint32_t				reqid;
375 	uint16_t				family;
376 	uint8_t				mode;		/* XFRM_MODE_xxx */
377 	uint8_t				replay_window;
378 	uint8_t				flags;
379 #define XFRM_STATE_NOECN	1
380 #define XFRM_STATE_DECAP_DSCP	2
381 #define XFRM_STATE_NOPMTUDISC	4
382 #define XFRM_STATE_WILDRECV	8
383 #define XFRM_STATE_ICMP		16
384 #define XFRM_STATE_AF_UNSPEC	32
385 #define XFRM_STATE_ALIGN4	64
386 #define XFRM_STATE_ESN		128
387 };
388 
389 #define XFRM_SA_XFLAG_DONT_ENCAP_DSCP	1
390 #define XFRM_SA_XFLAG_OSEQ_MAY_WRAP	2
391 
392 struct xfrm_usersa_id {
393 	xfrm_address_t			daddr;
394 	uint32_t				spi;
395 	uint16_t				family;
396 	uint8_t				proto;
397 };
398 
399 struct xfrm_aevent_id {
400 	struct xfrm_usersa_id		sa_id;
401 	xfrm_address_t			saddr;
402 	uint32_t				flags;
403 	uint32_t				reqid;
404 };
405 
406 struct xfrm_userspi_info {
407 	struct xfrm_usersa_info		info;
408 	uint32_t				min;
409 	uint32_t				max;
410 };
411 
412 struct xfrm_userpolicy_info {
413 	struct xfrm_selector		sel;
414 	struct xfrm_lifetime_cfg	lft;
415 	struct xfrm_lifetime_cur	curlft;
416 	uint32_t				priority;
417 	uint32_t				index;
418 	uint8_t				dir;
419 	uint8_t				action;
420 #define XFRM_POLICY_ALLOW	0
421 #define XFRM_POLICY_BLOCK	1
422 	uint8_t				flags;
423 #define XFRM_POLICY_LOCALOK	1	/* Allow user to override global policy */
424 	/* Automatically expand selector to include matching ICMP payloads. */
425 #define XFRM_POLICY_ICMP	2
426 	uint8_t				share;
427 };
428 
429 struct xfrm_userpolicy_id {
430 	struct xfrm_selector		sel;
431 	uint32_t				index;
432 	uint8_t				dir;
433 };
434 
435 struct xfrm_user_acquire {
436 	struct xfrm_id			id;
437 	xfrm_address_t			saddr;
438 	struct xfrm_selector		sel;
439 	struct xfrm_userpolicy_info	policy;
440 	uint32_t				aalgos;
441 	uint32_t				ealgos;
442 	uint32_t				calgos;
443 	uint32_t				seq;
444 };
445 
446 struct xfrm_user_expire {
447 	struct xfrm_usersa_info		state;
448 	uint8_t				hard;
449 };
450 
451 struct xfrm_user_polexpire {
452 	struct xfrm_userpolicy_info	pol;
453 	uint8_t				hard;
454 };
455 
456 struct xfrm_usersa_flush {
457 	uint8_t				proto;
458 };
459 
460 struct xfrm_user_report {
461 	uint8_t				proto;
462 	struct xfrm_selector		sel;
463 };
464 
465 /* Used by MIGRATE to pass addresses IKE should use to perform
466  * SA negotiation with the peer */
467 struct xfrm_user_kmaddress {
468 	xfrm_address_t                  local;
469 	xfrm_address_t                  remote;
470 	uint32_t				reserved;
471 	uint16_t				family;
472 };
473 
474 struct xfrm_user_migrate {
475 	xfrm_address_t			old_daddr;
476 	xfrm_address_t			old_saddr;
477 	xfrm_address_t			new_daddr;
478 	xfrm_address_t			new_saddr;
479 	uint8_t				proto;
480 	uint8_t				mode;
481 	uint16_t				reserved;
482 	uint32_t				reqid;
483 	uint16_t				old_family;
484 	uint16_t				new_family;
485 };
486 
487 struct xfrm_user_mapping {
488 	struct xfrm_usersa_id		id;
489 	uint32_t				reqid;
490 	xfrm_address_t			old_saddr;
491 	xfrm_address_t			new_saddr;
492 	uint16_t				old_sport;
493 	uint16_t				new_sport;
494 };
495 
496 struct xfrm_address_filter {
497 	xfrm_address_t			saddr;
498 	xfrm_address_t			daddr;
499 	uint16_t				family;
500 	uint8_t				splen;
501 	uint8_t				dplen;
502 };
503 
504 struct xfrm_user_offload {
505 	int				ifindex;
506 	uint8_t				flags;
507 };
508 #define XFRM_OFFLOAD_IPV6	1
509 #define XFRM_OFFLOAD_INBOUND	2
510 
511 #ifndef __KERNEL__
512 /* backwards compatibility for userspace */
513 #define XFRMGRP_ACQUIRE		1
514 #define XFRMGRP_EXPIRE		2
515 #define XFRMGRP_SA		4
516 #define XFRMGRP_POLICY		8
517 #define XFRMGRP_REPORT		0x20
518 #endif
519 
520 enum xfrm_nlgroups {
521 	XFRMNLGRP_NONE,
522 #define XFRMNLGRP_NONE		XFRMNLGRP_NONE
523 	XFRMNLGRP_ACQUIRE,
524 #define XFRMNLGRP_ACQUIRE	XFRMNLGRP_ACQUIRE
525 	XFRMNLGRP_EXPIRE,
526 #define XFRMNLGRP_EXPIRE	XFRMNLGRP_EXPIRE
527 	XFRMNLGRP_SA,
528 #define XFRMNLGRP_SA		XFRMNLGRP_SA
529 	XFRMNLGRP_POLICY,
530 #define XFRMNLGRP_POLICY	XFRMNLGRP_POLICY
531 	XFRMNLGRP_AEVENTS,
532 #define XFRMNLGRP_AEVENTS	XFRMNLGRP_AEVENTS
533 	XFRMNLGRP_REPORT,
534 #define XFRMNLGRP_REPORT	XFRMNLGRP_REPORT
535 	XFRMNLGRP_MIGRATE,
536 #define XFRMNLGRP_MIGRATE	XFRMNLGRP_MIGRATE
537 	XFRMNLGRP_MAPPING,
538 #define XFRMNLGRP_MAPPING	XFRMNLGRP_MAPPING
539 	__XFRMNLGRP_MAX
540 };
541 #define XFRMNLGRP_MAX	(__XFRMNLGRP_MAX - 1)
542 
543 #endif /* _LINUX_XFRM_H */
544