libhydra: Move kernel interface to libcharon
[strongswan.git] / src / frontends / android / app / src / main / jni / libandroidbridge / kernel / android_ipsec.c
1 /*
2 * Copyright (C) 2012 Tobias Brunner
3 * Copyright (C) 2012 Giuliano Grassi
4 * Copyright (C) 2012 Ralf Sager
5 * Hochschule fuer Technik Rapperswil
6 *
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2 of the License, or (at your
10 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>. *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * for more details.
15 */
16
17 #include "android_ipsec.h"
18 #include "../charonservice.h"
19
20 #include <utils/debug.h>
21 #include <library.h>
22 #include <daemon.h>
23 #include <ipsec.h>
24
25 typedef struct private_kernel_android_ipsec_t private_kernel_android_ipsec_t;
26
27 struct private_kernel_android_ipsec_t {
28
29 /**
30 * Public kernel interface
31 */
32 kernel_android_ipsec_t public;
33
34 /**
35 * Listener for lifetime expire events
36 */
37 ipsec_event_listener_t ipsec_listener;
38 };
39
40 /**
41 * Callback registrered with libipsec.
42 */
43 static void expire(u_int8_t protocol, u_int32_t spi, host_t *dst, bool hard)
44 {
45 charon->kernel->expire(charon->kernel, protocol, spi, dst, hard);
46 }
47
48 METHOD(kernel_ipsec_t, get_spi, status_t,
49 private_kernel_android_ipsec_t *this, host_t *src, host_t *dst,
50 u_int8_t protocol, u_int32_t *spi)
51 {
52 return ipsec->sas->get_spi(ipsec->sas, src, dst, protocol, spi);
53 }
54
55 METHOD(kernel_ipsec_t, get_cpi, status_t,
56 private_kernel_android_ipsec_t *this, host_t *src, host_t *dst,
57 u_int16_t *cpi)
58 {
59 return NOT_SUPPORTED;
60 }
61
62 METHOD(kernel_ipsec_t, add_sa, status_t,
63 private_kernel_android_ipsec_t *this, host_t *src, host_t *dst,
64 u_int32_t spi, u_int8_t protocol, u_int32_t reqid, mark_t mark,
65 u_int32_t tfc, lifetime_cfg_t *lifetime, u_int16_t enc_alg, chunk_t enc_key,
66 u_int16_t int_alg, chunk_t int_key, ipsec_mode_t mode,
67 u_int16_t ipcomp, u_int16_t cpi, u_int32_t replay_window,
68 bool initiator, bool encap, bool esn, bool inbound, bool update,
69 linked_list_t *src_ts, linked_list_t *dst_ts)
70 {
71 return ipsec->sas->add_sa(ipsec->sas, src, dst, spi, protocol, reqid, mark,
72 tfc, lifetime, enc_alg, enc_key, int_alg, int_key,
73 mode, ipcomp, cpi, initiator, encap, esn,
74 inbound, update);
75 }
76
77 METHOD(kernel_ipsec_t, update_sa, status_t,
78 private_kernel_android_ipsec_t *this, u_int32_t spi, u_int8_t protocol,
79 u_int16_t cpi, host_t *src, host_t *dst, host_t *new_src, host_t *new_dst,
80 bool encap, bool new_encap, mark_t mark)
81 {
82 return ipsec->sas->update_sa(ipsec->sas, spi, protocol, cpi, src, dst,
83 new_src, new_dst, encap, new_encap, mark);
84 }
85
86 METHOD(kernel_ipsec_t, query_sa, status_t,
87 private_kernel_android_ipsec_t *this, host_t *src, host_t *dst,
88 u_int32_t spi, u_int8_t protocol, mark_t mark,
89 u_int64_t *bytes, u_int64_t *packets, time_t *time)
90 {
91 return ipsec->sas->query_sa(ipsec->sas, src, dst, spi, protocol, mark,
92 bytes, packets, time);
93 }
94
95 METHOD(kernel_ipsec_t, del_sa, status_t,
96 private_kernel_android_ipsec_t *this, host_t *src, host_t *dst,
97 u_int32_t spi, u_int8_t protocol, u_int16_t cpi, mark_t mark)
98 {
99 return ipsec->sas->del_sa(ipsec->sas, src, dst, spi, protocol, cpi, mark);
100 }
101
102 METHOD(kernel_ipsec_t, flush_sas, status_t,
103 private_kernel_android_ipsec_t *this)
104 {
105 return ipsec->sas->flush_sas(ipsec->sas);
106 }
107
108 METHOD(kernel_ipsec_t, add_policy, status_t,
109 private_kernel_android_ipsec_t *this, host_t *src, host_t *dst,
110 traffic_selector_t *src_ts, traffic_selector_t *dst_ts,
111 policy_dir_t direction, policy_type_t type, ipsec_sa_cfg_t *sa, mark_t mark,
112 policy_priority_t priority)
113 {
114 return ipsec->policies->add_policy(ipsec->policies, src, dst, src_ts,
115 dst_ts, direction, type, sa, mark,
116 priority);
117 }
118
119 METHOD(kernel_ipsec_t, query_policy, status_t,
120 private_kernel_android_ipsec_t *this, traffic_selector_t *src_ts,
121 traffic_selector_t *dst_ts, policy_dir_t direction, mark_t mark,
122 time_t *use_time)
123 {
124 return NOT_SUPPORTED;
125 }
126
127 METHOD(kernel_ipsec_t, del_policy, status_t,
128 private_kernel_android_ipsec_t *this, host_t *src, host_t *dst,
129 traffic_selector_t *src_ts, traffic_selector_t *dst_ts,
130 policy_dir_t direction, policy_type_t type, ipsec_sa_cfg_t *sa,
131 mark_t mark, policy_priority_t priority)
132 {
133 return ipsec->policies->del_policy(ipsec->policies, src, dst, src_ts,
134 dst_ts, direction, type, sa, mark,
135 priority);
136 }
137
138 METHOD(kernel_ipsec_t, flush_policies, status_t,
139 private_kernel_android_ipsec_t *this)
140 {
141 ipsec->policies->flush_policies(ipsec->policies);
142 return SUCCESS;
143 }
144
145 METHOD(kernel_ipsec_t, bypass_socket, bool,
146 private_kernel_android_ipsec_t *this, int fd, int family)
147 {
148 return charonservice->bypass_socket(charonservice, fd, family);
149 }
150
151 METHOD(kernel_ipsec_t, enable_udp_decap, bool,
152 private_kernel_android_ipsec_t *this, int fd, int family, u_int16_t port)
153 {
154 return NOT_SUPPORTED;
155 }
156
157 METHOD(kernel_ipsec_t, destroy, void,
158 private_kernel_android_ipsec_t *this)
159 {
160 ipsec->events->unregister_listener(ipsec->events, &this->ipsec_listener);
161 free(this);
162 }
163
164 /*
165 * Described in header.
166 */
167 kernel_android_ipsec_t *kernel_android_ipsec_create()
168 {
169 private_kernel_android_ipsec_t *this;
170
171 INIT(this,
172 .public = {
173 .interface = {
174 .get_spi = _get_spi,
175 .get_cpi = _get_cpi,
176 .add_sa = _add_sa,
177 .update_sa = _update_sa,
178 .query_sa = _query_sa,
179 .del_sa = _del_sa,
180 .flush_sas = _flush_sas,
181 .add_policy = _add_policy,
182 .query_policy = _query_policy,
183 .del_policy = _del_policy,
184 .flush_policies = _flush_policies,
185 .bypass_socket = _bypass_socket,
186 .enable_udp_decap = _enable_udp_decap,
187 .destroy = _destroy,
188 },
189 },
190 .ipsec_listener = {
191 .expire = expire,
192 },
193 );
194
195 ipsec->events->register_listener(ipsec->events, &this->ipsec_listener);
196
197 return &this->public;
198 }