2 * @file kernel_interface.h
4 * @brief Interface of kernel_interface_t.
9 * Copyright (C) 2006 Tobias Brunner, Daniel Roethlisberger
10 * Copyright (C) 2005-2006 Martin Willi
11 * Copyright (C) 2005 Jan Hutter
12 * Hochschule fuer Technik Rapperswil
14 * This program is free software; you can redistribute it and/or modify it
15 * under the terms of the GNU General Public License as published by the
16 * Free Software Foundation; either version 2 of the License, or (at your
17 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
19 * This program is distributed in the hope that it will be useful, but
20 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
21 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
25 #ifndef KERNEL_INTERFACE_H_
26 #define KERNEL_INTERFACE_H_
28 typedef enum policy_dir_t policy_dir_t
;
29 typedef struct kernel_interface_t kernel_interface_t
;
31 #include <utils/host.h>
32 #include <crypto/prf_plus.h>
33 #include <encoding/payloads/proposal_substructure.h>
37 * Direction of a policy. These are equal to those
38 * defined in xfrm.h, but we want to stay implementation
44 /** Policy for inbound traffic */
46 /** Policy for outbound traffic */
48 /** Policy for forwarded traffic */
53 * @brief Interface to the kernel.
55 * The kernel interface handles the communication with the kernel
56 * for SA and policy management. It allows setup of these, and provides
57 * further the handling of kernel events.
58 * Policy information are cached in the interface. This is necessary to do
59 * reference counting. The Linux kernel does not allow the same policy
60 * installed twice, but we need this as CHILD_SA exist multiple times
61 * when rekeying. Thats why we do reference counting of policies.
64 * - kernel_interface_create()
68 struct kernel_interface_t
{
71 * @brief Get a SPI from the kernel.
73 * @warning get_spi() implicitely creates an SA with
74 * the allocated SPI, therefore the replace flag
75 * in add_sa() must be set when installing this SA.
77 * @param this calling object
78 * @param src source address of SA
79 * @param dst destination address of SA
80 * @param protocol protocol for SA (ESP/AH)
81 * @param reqid unique ID for this SA
82 * @param[out] spi allocated spi
85 * - FAILED if kernel comm failed
87 status_t (*get_spi
)(kernel_interface_t
*this, host_t
*src
, host_t
*dst
,
88 protocol_id_t protocol
, u_int32_t reqid
, u_int32_t
*spi
);
91 * @brief Add an SA to the SAD.
93 * add_sa() may update an already allocated
94 * SPI (via get_spi). In this case, the replace
96 * This function does install a single SA for a
97 * single protocol in one direction. The kernel-interface
98 * gets the keys itself from the PRF, as we don't know
99 * his algorithms and key sizes.
101 * @param this calling object
102 * @param src source address for this SA
103 * @param dst destination address for this SA
104 * @param spi SPI allocated by us or remote peer
105 * @param protocol protocol for this SA (ESP/AH)
106 * @param reqid unique ID for this SA
107 * @param expire_soft lifetime in seconds before rekeying
108 * @param expire_hard lieftime in seconds before delete
109 * @param enc_alg Algorithm to use for encryption (ESP only)
110 * @param int_alg Algorithm to use for integrity protection
111 * @param prf_plus PRF to derive keys from
112 * @param mode mode of the SA (tunnel, transport)
113 * @param encap enable UDP encapsulation for NAT traversal
114 * @param replace Should an already installed SA be updated?
117 * - FAILED if kernel comm failed
119 status_t (*add_sa
) (kernel_interface_t
*this,
120 host_t
*src
, host_t
*dst
, u_int32_t spi
,
121 protocol_id_t protocol
, u_int32_t reqid
,
122 u_int64_t expire_soft
, u_int64_t expire_hard
,
123 algorithm_t
*enc_alg
, algorithm_t
*int_alg
,
124 prf_plus_t
*prf_plus
, mode_t mode
, bool encap
,
128 * @brief Update the hosts on an installed SA.
130 * We cannot directly update the destination address as the kernel
131 * requires the spi, the protocol AND the destination address (and family)
132 * to identify SAs. Therefore if the destination address changed we
133 * create a new SA and delete the old one.
135 * @param this calling object
136 * @param spi SPI of the SA
137 * @param protocol protocol for this SA (ESP/AH)
138 * @param src current source address
139 * @param dst current destination address
140 * @param new_src new source address
141 * @param new_dst new destination address
142 * @param encap use UDP encapsulation
145 * - FAILED if kernel comm failed
147 status_t (*update_sa
)(kernel_interface_t
*this,
148 u_int32_t spi
, protocol_id_t protocol
,
149 host_t
*src
, host_t
*dst
,
150 host_t
*new_src
, host_t
*new_dst
, bool encap
);
153 * @brief Query the use time of an SA.
155 * The use time of an SA is not the time of the last usage, but
156 * the time of the first usage of the SA.
158 * @param this calling object
159 * @param dst destination address for this SA
160 * @param spi SPI allocated by us or remote peer
161 * @param protocol protocol for this SA (ESP/AH)
162 * @param[out] use_time the time of this SA's last use
165 * - FAILED if kernel comm failed
167 status_t (*query_sa
) (kernel_interface_t
*this, host_t
*dst
, u_int32_t spi
,
168 protocol_id_t protocol
, u_int32_t
*use_time
);
171 * @brief Delete a previusly installed SA from the SAD.
173 * @param this calling object
174 * @param dst destination address for this SA
175 * @param spi SPI allocated by us or remote peer
176 * @param protocol protocol for this SA (ESP/AH)
179 * - FAILED if kernel comm failed
181 status_t (*del_sa
) (kernel_interface_t
*this, host_t
*dst
, u_int32_t spi
,
182 protocol_id_t protocol
);
185 * @brief Add a policy to the SPD.
187 * A policy is always associated to an SA. Traffic which matches a
188 * policy is handled by the SA with the same reqid.
190 * @param this calling object
191 * @param src source address of SA
192 * @param dst dest address of SA
193 * @param src_ts traffic selector to match traffic source
194 * @param dst_ts traffic selector to match traffic dest
195 * @param direction direction of traffic, POLICY_IN, POLICY_OUT, POLICY_FWD
196 * @param protocol protocol to use to protect traffic (AH/ESP)
197 * @param reqid uniqe ID of an SA to use to enforce policy
198 * @param high_prio if TRUE, uses a higher priority than any with FALSE
199 * @param mode mode of SA (tunnel, transport)
202 * - FAILED if kernel comm failed
204 status_t (*add_policy
) (kernel_interface_t
*this,
205 host_t
*src
, host_t
*dst
,
206 traffic_selector_t
*src_ts
,
207 traffic_selector_t
*dst_ts
,
208 policy_dir_t direction
, protocol_id_t protocol
,
209 u_int32_t reqid
, bool high_prio
, mode_t mode
);
212 * @brief Query the use time of a policy.
214 * The use time of a policy is the time the policy was used
217 * @param this calling object
218 * @param src_ts traffic selector to match traffic source
219 * @param dst_ts traffic selector to match traffic dest
220 * @param direction direction of traffic, POLICY_IN, POLICY_OUT, POLICY_FWD
221 * @param[out] use_time the time of this SA's last use
224 * - FAILED if kernel comm failed
226 status_t (*query_policy
) (kernel_interface_t
*this,
227 traffic_selector_t
*src_ts
,
228 traffic_selector_t
*dst_ts
,
229 policy_dir_t direction
, u_int32_t
*use_time
);
232 * @brief Remove a policy from the SPD.
234 * The kernel interface implements reference counting for policies.
235 * If the same policy is installed multiple times (in the case of rekeying),
236 * the reference counter is increased. del_policy() decreases the ref counter
237 * and removes the policy only when no more references are available.
239 * @param this calling object
240 * @param src_ts traffic selector to match traffic source
241 * @param dst_ts traffic selector to match traffic dest
242 * @param direction direction of traffic, POLICY_IN, POLICY_OUT, POLICY_FWD
245 * - FAILED if kernel comm failed
247 status_t (*del_policy
) (kernel_interface_t
*this,
248 traffic_selector_t
*src_ts
,
249 traffic_selector_t
*dst_ts
,
250 policy_dir_t direction
);
253 * @brief Get our outgoing source address for a destination.
255 * Does a route lookup to get the source address used to reach dest.
256 * The returned host is allocated and must be destroyed.
258 * @param this calling object
259 * @param dest target destination address
260 * @return outgoing source address, NULL if unreachable
262 host_t
* (*get_source_addr
)(kernel_interface_t
*this, host_t
*dest
);
265 * @brief Get the interface name of a local address.
267 * @param this calling object
268 * @param host address to get interface name from
269 * @return allocated interface name, or NULL if not found
271 char* (*get_interface
) (kernel_interface_t
*this, host_t
*host
);
274 * @brief Creates an iterator over all local addresses.
276 * This function blocks an internal cached address list until the
277 * iterator gets destroyed.
278 * These hosts are read-only, do not modify or free.
280 * @param this calling object
281 * @return iterator over host_t's
283 iterator_t
*(*create_address_iterator
) (kernel_interface_t
*this);
286 * @brief Add a virtual IP to an interface.
288 * Virtual IPs are attached to an interface. If an IP is added multiple
289 * times, the IP is refcounted and not removed until del_ip() was called
290 * as many times as add_ip().
291 * The virtual IP is attached to the interface where the iface_ip is found.
293 * @param this calling object
294 * @param virtual_ip virtual ip address to assign
295 * @param iface_ip IP of an interface to attach virtual IP
298 * - FAILED if kernel comm failed
300 status_t (*add_ip
) (kernel_interface_t
*this, host_t
*virtual_ip
,
304 * @brief Remove a virtual IP from an interface.
306 * The kernel interface uses refcounting, see add_ip().
308 * @param this calling object
309 * @param virtual_ip virtual ip address to assign
312 * - FAILED if kernel comm failed
314 status_t (*del_ip
) (kernel_interface_t
*this, host_t
*virtual_ip
);
317 * @brief Destroys a kernel_interface object.
319 * @param kernel_interface_t calling object
321 void (*destroy
) (kernel_interface_t
*kernel_interface
);
325 * @brief Creates an object of type kernel_interface_t.
329 kernel_interface_t
*kernel_interface_create(void);
331 #endif /*KERNEL_INTERFACE_H_*/