Generate an IPsec policy specification structure from a readable string
#include <netinet6/ipsec.h> char* ipsec_set_policy( char *policy, int len );
libipsec
Use the -l ipsec option to qcc to link against this library.
The ipsec_set_policy() function generates an IPsec policy specification structure, namely a struct sadb_x_policy and potentially a struct sadb_x_ipsecrequest from a policy specification given as a C string. The ipsec_set_policy() function allocates a buffer, stores the corresponding IPsec policy specification structure in it, and returns a pointer to the buffer.
You should release the buffer returned by ipsec_set_policy() by calling free(). See the example below. |
The policy specifications can include the following variables:
The policy specifications include the following:
protocol / mode / src - dst [/level]
The elements of the string are as follows:
unique: number
where number must be between 1 and 32767.
If the request string is nonambiguous, you can omit the level and the slash (/) before it. However, it's best specify the level explicitly, in order to avoid unintended behavior.
If you omit the level, the default (which you can use sysctl to get or set) is used. For example:
Here's an example of policy information:
in discard out ipsec esp/transport//require in ipsec ah/transport//require out ipsec esp/tunnel/10.1.1.2-10.1.1.1/use in ipsec ipcom/transport//use esp/transport//use
A pointer to the allocated buffer for policy specification, or NULL if an error occurred.
#include <netinet6/ipsec.h> #include <sys/socket.h> #include <stdio.h> #include <malloc.h> #include <string.h> int main(void) { char *sadb; char *policy = "in discard"; int len; sadb = ipsec_set_policy(policy, strlen(policy)); if (sadb == NULL) { fprintf(stderr, "ipsec_set_policy: %s\n", ipsec_strerror()); return 1; } len = ipsec_get_policylen(sadb); printf("len: %d\n", len); policy = NULL; policy = ipsec_dump_policy(sadb, NULL); if (policy == NULL) { fprintf(stderr, "ipsec_dump_policy: %s\n", ipsec_strerror()); return 1; } printf("policy: %s\n", policy); free(policy); free(sadb); return 0; }
Safety: | |
---|---|
Cancellation point | No |
Interrupt handler | No |
Signal handler | No |
Thread | Yes |
setkey in the Utilities Reference