IPSEC_SET_POLICY(3)
πΈ IPSEC_SET_POLICY(3) πΈ
ok so bestie, we're in the Library Functions Manual today, and this one's from the IPsec Policy Control Library (libipsec, -lipsec). Strap in, it's actually so good π
β¨ NAME β¨
ipsec_set_policy, ipsec_get_policylen, ipsec_dump_policy β they literally create an IPsec policy structure out of a human readable string. like, you speak English, it gives you a struct. iconic π
π¦ LIBRARY π¦
IPsec Policy Control Library (libipsec, -lipsec), duh π
π SYNOPSIS π
#include <netipsec/ipsec.h>
char *
ipsec_set_policy(const char *policy, int len);
int
ipsec_get_policylen(const char *buf);
char *
ipsec_dump_policy(c_caddr_t *buf, const char *delim);
π DESCRIPTION π
π§ the three functions, one by one
ipsec_set_policy() generates an IPsec policy specification structure β that's struct sadb_x_policy and/or struct sadb_x_ipsecrequest, no cap β from a human-readable policy specification. The policy spec has to be a C string, handed over in the policy argument, plus the length of that string in len. It returns a pointer to a buffer that contains a properly formed IPsec policy specification structure. Heads up though: that buffer is dynamically allocated, so you must free it with the free(3) library function or it's giving leak energy π
ipsec_get_policylen() returns the length of the buffer β which is literally the thing you need when you pass that specification structure to the setsockopt(2) system call. so important, don't skip it β¨
ipsec_dump_policy() flips it the other way around: it converts an IPsec policy structure into human readable form. The buf argument points to an IPsec policy structure, struct sadb_x_policy. delim is a delimiter string, which is usually a blank character. And if you set delim to NULL, a single white space is assumed. It returns a pointer to a dynamically allocated string, and it is the caller's responsibility to free that returned pointer using the free(3) library call. your pointer, your problem, bestie π«Ά
πΈ how you actually write a policy
A policy is given in the following way:
direction discardβ The direction must beinoroutand specifies which direction the policy needs to be applied, either on inbound or outbound packets. When the discard policy is selected, packets will be dropped if they match the policy. goodbye packet πdirection entrustβentrustmeans to consult the security policy database (SPD) in the kernel, as controlled bysetkey(8). let the kernel be the main character πdirection bypassβ A direction ofbypassindicates that IPsec processing should not occur and that the packet will be transmitted in clear. Thebypassoption is only available to privileged sockets. (>_<) so not for everyone, sorry!direction ipsec request ...β A direction ofipsecmeans that matching packets are processed by IPsec.ipseccan be followed by one or morerequeststring, which is formatted as:
protocol / mode / src - dst [/level]
so like, let's break that down because it's a lot:
- The
protocolis one of:ah,esporipcompβ indicating Authentication Header, Encapsulating Security Protocol or IP Compression protocol is used. pick your fighter β - The
modeis eithertransportortunnelβ the meanings of both modes are described inipsec(4). - The
srcanddstspecify the IP address, either v4 or v6, of the source and destination systems. Thesrcalways stands for the "sending node" anddstalways stands for the "receiving node". When direction isin,dstis this local node andsrcis the remote node or peer. If mode istransport, bothsrcanddstcan be omitted. convenient! - The
levelmust be set to one of the following:default,use,requireorunique.defaultmeans that the kernel should consult the default security policies as defined by a set ofsysctl(8)variables. The relevantsysctl(8)variables are described inipsec(4).- When
useis selected a relevant security association (SA) can be used when available but is not necessary. If the SA is available then packets will be handled by IPsec, i.e., encrypted and/or authenticated, but if an SA is not available then packets will be transmitted in the clear. π¨ Theuseoption is not recommended because it allows for accidental mis-configurations where an encrypted or authenticated link becomes unencrypted or unauthenticated. Therequirekeyword is recommended instead ofusewhere possible. Using therequirekeyword means that a relevant SA is required, and that the kernel must perform IPsec processing on all matching packets. - The
uniquekeyword has the same effect asrequire, but adds the restriction that the SA for outbound traffic is used only for this policy. You may need the identifier in order to relate the policy and the SA when you define the SA by manual keying usingsetkey(8). Put the decimal number as the identifier after theuniquekeyword in this way:unique: number, wherenumbermust be between 1 and 32767.
- If the request string is kept unambiguous,
leveland the slash prior tolevelcan be omitted, but you are encouraged to specify them explicitly to avoid unintended behaviors. Iflevelis omitted, it will be interpreted asdefault. (iykyk, just write it out π )
β οΈ Note that there is a difference between the specification allowed here and insetkey(8). When specifying security policies withsetkey(8), neitherentrustnorbypassare used. Refer tosetkey(8)for details. don't come for me, that's just how it is π
π₯ RETURN VALUES π₯
ipsec_set_policy()returns a pointer to the allocated buffer containing a the policy specification if successful; otherwise aNULLpointer is returned. (yes, "a the" β that's what the page says, I don't make the rules π)ipsec_get_policylen()returns a positive value, indicating the buffer size, on success, and a negative value on error.ipsec_dump_policy()returns a pointer to a dynamically allocated region containing a human readable security policy on success, andNULLon error.
π EXAMPLES π
Set a policy that all inbound packets are discarded.
in discard
All outbound packets are required to be processed by IPsec and transported using ESP.
out ipsec esp/transport//require
All inbound packets are required to be authenticated using the AH protocol.
in ipsec ah/transport//require
Tunnel packets outbound through the endpoints at 10.1.1.2 and 10.1.1.1.
out ipsec esp/tunnel/10.1.1.2-10.1.1.1/require
π SEE ALSO π
ipsec_strerror(3), ipsec(4), setkey(8) β go read them, they're lowkey essential π
π HISTORY π
These functions first appeared in WIDE/KAME IPv6 protocol stack kit.
IPv6 and IPsec support based on the KAME Project (http://www.kame.net/) stack was initially integrated into FreeBSD 4.0.
and that's the whole page, bestie π it's from Debian, dated February 14, 2006 β yes, Valentine's Day, the most romantic manpage release ever πΉ periodt.
π¦ where this page comes from
- freebsd 15.1: base 15.1
- Copyright (C) 1995, 1996, 1997, 1998, and 1999 WIDE Project
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms