2 * Copyright (C) 2006-2008 Tobias Brunner
3 * Copyright (C) 2006 Daniel Roethlisberger
4 * Copyright (C) 2005-2006 Martin Willi
5 * Copyright (C) 2005 Jan Hutter
6 * Hochschule fuer Technik Rapperswil
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the
10 * Free Software Foundation; either version 2 of the License, or (at your
11 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22 * @defgroup kernel_interface kernel_interface
26 #ifndef KERNEL_INTERFACE_H_
27 #define KERNEL_INTERFACE_H_
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>
35 #include <kernel/kernel_ipsec.h>
36 #include <kernel/kernel_net.h>
39 * Constructor function for ipsec kernel interface
41 typedef kernel_ipsec_t
* (*kernel_ipsec_constructor_t
)(void);
44 * Constructor function for network kernel interface
46 typedef kernel_net_t
* (*kernel_net_constructor_t
)(void);
49 * Manager and wrapper for different kernel interfaces.
51 * The kernel interface handles the communication with the kernel
52 * for SA and policy management and interface and IP address management.
54 struct kernel_interface_t
{
57 * Get a SPI from the kernel.
59 * @param src source address of SA
60 * @param dst destination address of SA
61 * @param protocol protocol for SA (ESP/AH)
62 * @param reqid unique ID for this SA
63 * @param spi allocated spi
64 * @return SUCCESS if operation completed
66 status_t (*get_spi
)(kernel_interface_t
*this, host_t
*src
, host_t
*dst
,
67 protocol_id_t protocol
, u_int32_t reqid
, u_int32_t
*spi
);
70 * Get a Compression Parameter Index (CPI) from the kernel.
72 * @param src source address of SA
73 * @param dst destination address of SA
74 * @param reqid unique ID for the corresponding SA
75 * @param cpi allocated cpi
76 * @return SUCCESS if operation completed
78 status_t (*get_cpi
)(kernel_interface_t
*this, host_t
*src
, host_t
*dst
,
79 u_int32_t reqid
, u_int16_t
*cpi
);
82 * Add an SA to the SAD.
84 * add_sa() may update an already allocated
85 * SPI (via get_spi). In this case, the replace
87 * This function does install a single SA for a
88 * single protocol in one direction. The kernel-interface
89 * gets the keys itself from the PRF, as we don't know
90 * his algorithms and key sizes.
92 * @param src source address for this SA
93 * @param dst destination address for this SA
94 * @param spi SPI allocated by us or remote peer
95 * @param protocol protocol for this SA (ESP/AH)
96 * @param reqid unique ID for this SA
97 * @param expire_soft lifetime in seconds before rekeying
98 * @param expire_hard lifetime in seconds before delete
99 * @param enc_alg Algorithm to use for encryption (ESP only)
100 * @param enc_key key to use for encryption
101 * @param int_alg Algorithm to use for integrity protection
102 * @param int_key key to use for integrity protection
103 * @param mode mode of the SA (tunnel, transport)
104 * @param ipcomp IPComp transform to use
105 * @param cpi CPI for IPComp
106 * @param encap enable UDP encapsulation for NAT traversal
107 * @param inbound TRUE if this is an inbound SA
108 * @return SUCCESS if operation completed
110 status_t (*add_sa
) (kernel_interface_t
*this,
111 host_t
*src
, host_t
*dst
, u_int32_t spi
,
112 protocol_id_t protocol
, u_int32_t reqid
,
113 u_int64_t expire_soft
, u_int64_t expire_hard
,
114 u_int16_t enc_alg
, chunk_t enc_key
,
115 u_int16_t int_alg
, chunk_t int_key
,
116 ipsec_mode_t mode
, u_int16_t ipcomp
, u_int16_t cpi
,
117 bool encap
, bool inbound
);
120 * Update the hosts on an installed SA.
122 * We cannot directly update the destination address as the kernel
123 * requires the spi, the protocol AND the destination address (and family)
124 * to identify SAs. Therefore if the destination address changed we
125 * create a new SA and delete the old one.
127 * @param spi SPI of the SA
128 * @param protocol protocol for this SA (ESP/AH)
129 * @param cpi CPI for IPComp, 0 if no IPComp is used
130 * @param src current source address
131 * @param dst current destination address
132 * @param new_src new source address
133 * @param new_dst new destination address
134 * @param encap current use of UDP encapsulation
135 * @param new_encap new use of UDP encapsulation
136 * @return SUCCESS if operation completed, NOT_SUPPORTED if
137 * the kernel interface can't update the SA
139 status_t (*update_sa
)(kernel_interface_t
*this,
140 u_int32_t spi
, protocol_id_t protocol
, u_int16_t cpi
,
141 host_t
*src
, host_t
*dst
,
142 host_t
*new_src
, host_t
*new_dst
,
143 bool encap
, bool new_encap
);
146 * Delete a previously installed SA from the SAD.
148 * @param dst destination address for this SA
149 * @param spi SPI allocated by us or remote peer
150 * @param protocol protocol for this SA (ESP/AH)
151 * @param cpi CPI for IPComp or 0
152 * @return SUCCESS if operation completed
154 status_t (*del_sa
) (kernel_interface_t
*this, host_t
*dst
, u_int32_t spi
,
155 protocol_id_t protocol
, u_int16_t cpi
);
158 * Add a policy to the SPD.
160 * A policy is always associated to an SA. Traffic which matches a
161 * policy is handled by the SA with the same reqid.
163 * @param src source address of SA
164 * @param dst dest address of SA
165 * @param src_ts traffic selector to match traffic source
166 * @param dst_ts traffic selector to match traffic dest
167 * @param direction direction of traffic, POLICY_IN, POLICY_OUT, POLICY_FWD
168 * @param spi SPI of SA
169 * @param protocol protocol to use to protect traffic (AH/ESP)
170 * @param reqid unique ID of an SA to use to enforce policy
171 * @param mode mode of SA (tunnel, transport)
172 * @param ipcomp the IPComp transform used
173 * @param cpi CPI for IPComp
174 * @param routed TRUE, if this policy is routed in the kernel
175 * @return SUCCESS if operation completed
177 status_t (*add_policy
) (kernel_interface_t
*this,
178 host_t
*src
, host_t
*dst
,
179 traffic_selector_t
*src_ts
,
180 traffic_selector_t
*dst_ts
,
181 policy_dir_t direction
, u_int32_t spi
,
182 protocol_id_t protocol
, u_int32_t reqid
,
183 ipsec_mode_t mode
, u_int16_t ipcomp
, u_int16_t cpi
,
187 * Query the use time of a policy.
189 * The use time of a policy is the time the policy was used
192 * @param src_ts traffic selector to match traffic source
193 * @param dst_ts traffic selector to match traffic dest
194 * @param direction direction of traffic, POLICY_IN, POLICY_OUT, POLICY_FWD
195 * @param[out] use_time the time of this SA's last use
196 * @return SUCCESS if operation completed
198 status_t (*query_policy
) (kernel_interface_t
*this,
199 traffic_selector_t
*src_ts
,
200 traffic_selector_t
*dst_ts
,
201 policy_dir_t direction
, u_int32_t
*use_time
);
204 * Remove a policy from the SPD.
206 * The kernel interface implements reference counting for policies.
207 * If the same policy is installed multiple times (in the case of rekeying),
208 * the reference counter is increased. del_policy() decreases the ref counter
209 * and removes the policy only when no more references are available.
211 * @param src_ts traffic selector to match traffic source
212 * @param dst_ts traffic selector to match traffic dest
213 * @param direction direction of traffic, POLICY_IN, POLICY_OUT, POLICY_FWD
214 * @param unrouted TRUE, if this policy is unrouted from the kernel
215 * @return SUCCESS if operation completed
217 status_t (*del_policy
) (kernel_interface_t
*this,
218 traffic_selector_t
*src_ts
,
219 traffic_selector_t
*dst_ts
,
220 policy_dir_t direction
,
224 * Get our outgoing source address for a destination.
226 * Does a route lookup to get the source address used to reach dest.
227 * The returned host is allocated and must be destroyed.
228 * An optional src address can be used to check if a route is available
229 * for given source to dest.
231 * @param dest target destination address
232 * @param src source address to check, or NULL
233 * @return outgoing source address, NULL if unreachable
235 host_t
* (*get_source_addr
)(kernel_interface_t
*this,
236 host_t
*dest
, host_t
*src
);
239 * Get the next hop for a destination.
241 * Does a route lookup to get the next hop used to reach dest.
242 * The returned host is allocated and must be destroyed.
244 * @param dest target destination address
245 * @return next hop address, NULL if unreachable
247 host_t
* (*get_nexthop
)(kernel_interface_t
*this, host_t
*dest
);
250 * Get the interface name of a local address.
252 * @param host address to get interface name from
253 * @return allocated interface name, or NULL if not found
255 char* (*get_interface
) (kernel_interface_t
*this, host_t
*host
);
258 * Creates an enumerator over all local addresses.
260 * This function blocks an internal cached address list until the
261 * enumerator gets destroyed.
262 * The hosts are read-only, do not modify of free.
264 * @param include_down_ifaces TRUE to enumerate addresses from down interfaces
265 * @param include_virtual_ips TRUE to enumerate virtual ip addresses
266 * @return enumerator over host_t's
268 enumerator_t
*(*create_address_enumerator
) (kernel_interface_t
*this,
269 bool include_down_ifaces
, bool include_virtual_ips
);
272 * Add a virtual IP to an interface.
274 * Virtual IPs are attached to an interface. If an IP is added multiple
275 * times, the IP is refcounted and not removed until del_ip() was called
276 * as many times as add_ip().
277 * The virtual IP is attached to the interface where the iface_ip is found.
279 * @param virtual_ip virtual ip address to assign
280 * @param iface_ip IP of an interface to attach virtual IP
281 * @return SUCCESS if operation completed
283 status_t (*add_ip
) (kernel_interface_t
*this, host_t
*virtual_ip
,
287 * Remove a virtual IP from an interface.
289 * The kernel interface uses refcounting, see add_ip().
291 * @param virtual_ip virtual ip address to assign
292 * @return SUCCESS if operation completed
294 status_t (*del_ip
) (kernel_interface_t
*this, host_t
*virtual_ip
);
299 * @param dst_net destination net
300 * @param prefixlen destination net prefix length
301 * @param gateway gateway for this route
302 * @param src_ip sourc ip of the route
303 * @param if_name name of the interface the route is bound to
304 * @return SUCCESS if operation completed
305 * ALREADY_DONE if the route already exists
307 status_t (*add_route
) (kernel_interface_t
*this, chunk_t dst_net
, u_int8_t prefixlen
,
308 host_t
*gateway
, host_t
*src_ip
, char *if_name
);
313 * @param dst_net destination net
314 * @param prefixlen destination net prefix length
315 * @param gateway gateway for this route
316 * @param src_ip sourc ip of the route
317 * @param if_name name of the interface the route is bound to
318 * @return SUCCESS if operation completed
320 status_t (*del_route
) (kernel_interface_t
*this, chunk_t dst_net
, u_int8_t prefixlen
,
321 host_t
*gateway
, host_t
*src_ip
, char *if_name
);
328 * Tries to find an ip address of a local interface that is included in the
329 * supplied traffic selector.
331 * @param ts traffic selector
332 * @param ip returned ip (has to be destroyed)
333 * @return SUCCESS if address found
335 status_t (*get_address_by_ts
) (kernel_interface_t
*this,
336 traffic_selector_t
*ts
, host_t
**ip
);
339 * Register an ipsec kernel interface constructor on the manager.
341 * @param create constructor to register
343 void (*add_ipsec_interface
)(kernel_interface_t
*this, kernel_ipsec_constructor_t create
);
346 * Unregister an ipsec kernel interface constructor.
348 * @param create constructor to unregister
350 void (*remove_ipsec_interface
)(kernel_interface_t
*this, kernel_ipsec_constructor_t create
);
353 * Register a network kernel interface constructor on the manager.
355 * @param create constructor to register
357 void (*add_net_interface
)(kernel_interface_t
*this, kernel_net_constructor_t create
);
360 * Unregister a network kernel interface constructor.
362 * @param create constructor to unregister
364 void (*remove_net_interface
)(kernel_interface_t
*this, kernel_net_constructor_t create
);
367 * Create the kernel interfaces classes.
369 void (*create_interfaces
)(kernel_interface_t
*this);
372 * Destroys a kernel_interface_manager_t object.
374 void (*destroy
) (kernel_interface_t
*this);
378 * Creates an object of type kernel_interface_t.
380 kernel_interface_t
*kernel_interface_create(void);
382 #endif /* KERNEL_INTERFACE_H_ @} */