blob: 7319c7f8060dbee87d7fddd4d9827b78a3068c18 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* xfrm_user.c: User interface to configure xfrm engine.
2 *
3 * Copyright (C) 2002 David S. Miller (davem@redhat.com)
4 *
5 * Changes:
6 * Mitsuru KANDA @USAGI
7 * Kazunori MIYAZAWA @USAGI
8 * Kunihiro Ishiguro <kunihiro@ipinfusion.com>
9 * IPv6 support
Trent Jaegerdf718372005-12-13 23:12:27 -080010 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070011 */
12
Herbert Xu9409f382006-08-06 19:49:12 +100013#include <linux/crypto.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/module.h>
15#include <linux/kernel.h>
16#include <linux/types.h>
17#include <linux/slab.h>
18#include <linux/socket.h>
19#include <linux/string.h>
20#include <linux/net.h>
21#include <linux/skbuff.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/rtnetlink.h>
23#include <linux/pfkeyv2.h>
24#include <linux/ipsec.h>
25#include <linux/init.h>
26#include <linux/security.h>
27#include <net/sock.h>
28#include <net/xfrm.h>
Thomas Graf88fc2c82005-11-10 02:25:54 +010029#include <net/netlink.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <asm/uaccess.h>
Masahide NAKAMURAe23c7192006-08-23 20:33:28 -070031#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
32#include <linux/in6.h>
33#endif
Joy Latten161a09e2006-11-27 13:11:54 -060034#include <linux/audit.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035
Thomas Grafc26445a2007-08-22 13:56:23 -070036static inline int alg_len(struct xfrm_algo *alg)
37{
38 return sizeof(*alg) + ((alg->alg_key_len + 7) / 8);
39}
40
Thomas Graf35a7aa02007-08-22 14:00:40 -070041static int verify_one_alg(struct rtattr **attrs, enum xfrm_attr_type_t type)
Linus Torvalds1da177e2005-04-16 15:20:36 -070042{
Thomas Graf35a7aa02007-08-22 14:00:40 -070043 struct rtattr *rt = attrs[type];
Linus Torvalds1da177e2005-04-16 15:20:36 -070044 struct xfrm_algo *algp;
45
46 if (!rt)
47 return 0;
48
Linus Torvalds1da177e2005-04-16 15:20:36 -070049 algp = RTA_DATA(rt);
Thomas Grafcf5cb792007-08-22 13:59:04 -070050 if (RTA_PAYLOAD(rt) < alg_len(algp))
Herbert Xu31c26852005-05-19 12:39:49 -070051 return -EINVAL;
52
Linus Torvalds1da177e2005-04-16 15:20:36 -070053 switch (type) {
54 case XFRMA_ALG_AUTH:
55 if (!algp->alg_key_len &&
56 strcmp(algp->alg_name, "digest_null") != 0)
57 return -EINVAL;
58 break;
59
60 case XFRMA_ALG_CRYPT:
61 if (!algp->alg_key_len &&
62 strcmp(algp->alg_name, "cipher_null") != 0)
63 return -EINVAL;
64 break;
65
66 case XFRMA_ALG_COMP:
67 /* Zero length keys are legal. */
68 break;
69
70 default:
71 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -070072 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070073
74 algp->alg_name[CRYPTO_MAX_ALG_NAME - 1] = '\0';
75 return 0;
76}
77
Thomas Graf35a7aa02007-08-22 14:00:40 -070078static void verify_one_addr(struct rtattr **attrs, enum xfrm_attr_type_t type,
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -070079 xfrm_address_t **addrp)
80{
Thomas Graf35a7aa02007-08-22 14:00:40 -070081 struct rtattr *rt = attrs[type];
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -070082
Thomas Grafcf5cb792007-08-22 13:59:04 -070083 if (rt && addrp)
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -070084 *addrp = RTA_DATA(rt);
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -070085}
Trent Jaegerdf718372005-12-13 23:12:27 -080086
Thomas Graf35a7aa02007-08-22 14:00:40 -070087static inline int verify_sec_ctx_len(struct rtattr **attrs)
Trent Jaegerdf718372005-12-13 23:12:27 -080088{
Thomas Graf35a7aa02007-08-22 14:00:40 -070089 struct rtattr *rt = attrs[XFRMA_SEC_CTX];
Trent Jaegerdf718372005-12-13 23:12:27 -080090 struct xfrm_user_sec_ctx *uctx;
Trent Jaegerdf718372005-12-13 23:12:27 -080091
92 if (!rt)
93 return 0;
94
Trent Jaegerdf718372005-12-13 23:12:27 -080095 uctx = RTA_DATA(rt);
Thomas Grafcf5cb792007-08-22 13:59:04 -070096 if (uctx->len != (sizeof(struct xfrm_user_sec_ctx) + uctx->ctx_len))
Trent Jaegerdf718372005-12-13 23:12:27 -080097 return -EINVAL;
98
99 return 0;
100}
101
102
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103static int verify_newsa_info(struct xfrm_usersa_info *p,
Thomas Graf35a7aa02007-08-22 14:00:40 -0700104 struct rtattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105{
106 int err;
107
108 err = -EINVAL;
109 switch (p->family) {
110 case AF_INET:
111 break;
112
113 case AF_INET6:
114#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
115 break;
116#else
117 err = -EAFNOSUPPORT;
118 goto out;
119#endif
120
121 default:
122 goto out;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700123 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124
125 err = -EINVAL;
126 switch (p->id.proto) {
127 case IPPROTO_AH:
Thomas Graf35a7aa02007-08-22 14:00:40 -0700128 if (!attrs[XFRMA_ALG_AUTH] ||
129 attrs[XFRMA_ALG_CRYPT] ||
130 attrs[XFRMA_ALG_COMP])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 goto out;
132 break;
133
134 case IPPROTO_ESP:
Thomas Graf35a7aa02007-08-22 14:00:40 -0700135 if ((!attrs[XFRMA_ALG_AUTH] &&
136 !attrs[XFRMA_ALG_CRYPT]) ||
137 attrs[XFRMA_ALG_COMP])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 goto out;
139 break;
140
141 case IPPROTO_COMP:
Thomas Graf35a7aa02007-08-22 14:00:40 -0700142 if (!attrs[XFRMA_ALG_COMP] ||
143 attrs[XFRMA_ALG_AUTH] ||
144 attrs[XFRMA_ALG_CRYPT])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 goto out;
146 break;
147
Masahide NAKAMURAe23c7192006-08-23 20:33:28 -0700148#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
149 case IPPROTO_DSTOPTS:
150 case IPPROTO_ROUTING:
Thomas Graf35a7aa02007-08-22 14:00:40 -0700151 if (attrs[XFRMA_ALG_COMP] ||
152 attrs[XFRMA_ALG_AUTH] ||
153 attrs[XFRMA_ALG_CRYPT] ||
154 attrs[XFRMA_ENCAP] ||
155 attrs[XFRMA_SEC_CTX] ||
156 !attrs[XFRMA_COADDR])
Masahide NAKAMURAe23c7192006-08-23 20:33:28 -0700157 goto out;
158 break;
159#endif
160
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 default:
162 goto out;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700163 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164
Thomas Graf35a7aa02007-08-22 14:00:40 -0700165 if ((err = verify_one_alg(attrs, XFRMA_ALG_AUTH)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 goto out;
Thomas Graf35a7aa02007-08-22 14:00:40 -0700167 if ((err = verify_one_alg(attrs, XFRMA_ALG_CRYPT)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 goto out;
Thomas Graf35a7aa02007-08-22 14:00:40 -0700169 if ((err = verify_one_alg(attrs, XFRMA_ALG_COMP)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 goto out;
Thomas Graf35a7aa02007-08-22 14:00:40 -0700171 if ((err = verify_sec_ctx_len(attrs)))
Trent Jaegerdf718372005-12-13 23:12:27 -0800172 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173
174 err = -EINVAL;
175 switch (p->mode) {
Masahide NAKAMURA7e49e6d2006-09-22 15:05:15 -0700176 case XFRM_MODE_TRANSPORT:
177 case XFRM_MODE_TUNNEL:
Noriaki TAKAMIYA060f02a2006-08-23 18:18:55 -0700178 case XFRM_MODE_ROUTEOPTIMIZATION:
Diego Beltrami0a694522006-10-03 23:47:05 -0700179 case XFRM_MODE_BEET:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 break;
181
182 default:
183 goto out;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700184 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185
186 err = 0;
187
188out:
189 return err;
190}
191
192static int attach_one_algo(struct xfrm_algo **algpp, u8 *props,
193 struct xfrm_algo_desc *(*get_byname)(char *, int),
194 struct rtattr *u_arg)
195{
196 struct rtattr *rta = u_arg;
197 struct xfrm_algo *p, *ualg;
198 struct xfrm_algo_desc *algo;
199
200 if (!rta)
201 return 0;
202
203 ualg = RTA_DATA(rta);
204
205 algo = get_byname(ualg->alg_name, 1);
206 if (!algo)
207 return -ENOSYS;
208 *props = algo->desc.sadb_alg_id;
209
Thomas Grafc26445a2007-08-22 13:56:23 -0700210 p = kmemdup(ualg, alg_len(ualg), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 if (!p)
212 return -ENOMEM;
213
Herbert Xu04ff1262006-08-13 08:50:00 +1000214 strcpy(p->alg_name, algo->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 *algpp = p;
216 return 0;
217}
218
219static int attach_encap_tmpl(struct xfrm_encap_tmpl **encapp, struct rtattr *u_arg)
220{
221 struct rtattr *rta = u_arg;
222 struct xfrm_encap_tmpl *p, *uencap;
223
224 if (!rta)
225 return 0;
226
227 uencap = RTA_DATA(rta);
Arnaldo Carvalho de Melocdbc6da2006-11-21 01:22:51 -0200228 p = kmemdup(uencap, sizeof(*p), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 if (!p)
230 return -ENOMEM;
231
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 *encapp = p;
233 return 0;
234}
235
Trent Jaegerdf718372005-12-13 23:12:27 -0800236
Joy Latten661697f2007-04-13 16:14:35 -0700237static inline int xfrm_user_sec_ctx_size(struct xfrm_sec_ctx *xfrm_ctx)
Trent Jaegerdf718372005-12-13 23:12:27 -0800238{
Trent Jaegerdf718372005-12-13 23:12:27 -0800239 int len = 0;
240
241 if (xfrm_ctx) {
242 len += sizeof(struct xfrm_user_sec_ctx);
243 len += xfrm_ctx->ctx_len;
244 }
245 return len;
246}
247
248static int attach_sec_ctx(struct xfrm_state *x, struct rtattr *u_arg)
249{
250 struct xfrm_user_sec_ctx *uctx;
251
252 if (!u_arg)
253 return 0;
254
255 uctx = RTA_DATA(u_arg);
256 return security_xfrm_state_alloc(x, uctx);
257}
258
Noriaki TAKAMIYA060f02a2006-08-23 18:18:55 -0700259static int attach_one_addr(xfrm_address_t **addrpp, struct rtattr *u_arg)
260{
261 struct rtattr *rta = u_arg;
262 xfrm_address_t *p, *uaddrp;
263
264 if (!rta)
265 return 0;
266
267 uaddrp = RTA_DATA(rta);
Arnaldo Carvalho de Melocdbc6da2006-11-21 01:22:51 -0200268 p = kmemdup(uaddrp, sizeof(*p), GFP_KERNEL);
Noriaki TAKAMIYA060f02a2006-08-23 18:18:55 -0700269 if (!p)
270 return -ENOMEM;
271
Noriaki TAKAMIYA060f02a2006-08-23 18:18:55 -0700272 *addrpp = p;
273 return 0;
274}
275
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276static void copy_from_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p)
277{
278 memcpy(&x->id, &p->id, sizeof(x->id));
279 memcpy(&x->sel, &p->sel, sizeof(x->sel));
280 memcpy(&x->lft, &p->lft, sizeof(x->lft));
281 x->props.mode = p->mode;
282 x->props.replay_window = p->replay_window;
283 x->props.reqid = p->reqid;
284 x->props.family = p->family;
David S. Miller54489c142006-10-27 15:29:47 -0700285 memcpy(&x->props.saddr, &p->saddr, sizeof(x->props.saddr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 x->props.flags = p->flags;
Herbert Xu196b0032007-07-31 02:04:32 -0700287
288 /*
289 * Set inner address family if the KM left it as zero.
290 * See comment in validate_tmpl.
291 */
292 if (!x->sel.family)
293 x->sel.family = p->family;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294}
295
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800296/*
297 * someday when pfkey also has support, we could have the code
298 * somehow made shareable and move it to xfrm_state.c - JHS
299 *
300*/
Thomas Graf35a7aa02007-08-22 14:00:40 -0700301static void xfrm_update_ae_params(struct xfrm_state *x, struct rtattr **attrs)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800302{
Thomas Graf35a7aa02007-08-22 14:00:40 -0700303 struct rtattr *rp = attrs[XFRMA_REPLAY_VAL];
304 struct rtattr *lt = attrs[XFRMA_LTIME_VAL];
305 struct rtattr *et = attrs[XFRMA_ETIMER_THRESH];
306 struct rtattr *rt = attrs[XFRMA_REPLAY_THRESH];
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800307
308 if (rp) {
309 struct xfrm_replay_state *replay;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800310 replay = RTA_DATA(rp);
311 memcpy(&x->replay, replay, sizeof(*replay));
312 memcpy(&x->preplay, replay, sizeof(*replay));
313 }
314
315 if (lt) {
316 struct xfrm_lifetime_cur *ltime;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800317 ltime = RTA_DATA(lt);
318 x->curlft.bytes = ltime->bytes;
319 x->curlft.packets = ltime->packets;
320 x->curlft.add_time = ltime->add_time;
321 x->curlft.use_time = ltime->use_time;
322 }
323
Thomas Grafcf5cb792007-08-22 13:59:04 -0700324 if (et)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800325 x->replay_maxage = *(u32*)RTA_DATA(et);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800326
Thomas Grafcf5cb792007-08-22 13:59:04 -0700327 if (rt)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800328 x->replay_maxdiff = *(u32*)RTA_DATA(rt);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800329}
330
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331static struct xfrm_state *xfrm_state_construct(struct xfrm_usersa_info *p,
Thomas Graf35a7aa02007-08-22 14:00:40 -0700332 struct rtattr **attrs,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 int *errp)
334{
335 struct xfrm_state *x = xfrm_state_alloc();
336 int err = -ENOMEM;
337
338 if (!x)
339 goto error_no_put;
340
341 copy_from_user_state(x, p);
342
343 if ((err = attach_one_algo(&x->aalg, &x->props.aalgo,
344 xfrm_aalg_get_byname,
Thomas Graf35a7aa02007-08-22 14:00:40 -0700345 attrs[XFRMA_ALG_AUTH])))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 goto error;
347 if ((err = attach_one_algo(&x->ealg, &x->props.ealgo,
348 xfrm_ealg_get_byname,
Thomas Graf35a7aa02007-08-22 14:00:40 -0700349 attrs[XFRMA_ALG_CRYPT])))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 goto error;
351 if ((err = attach_one_algo(&x->calg, &x->props.calgo,
352 xfrm_calg_get_byname,
Thomas Graf35a7aa02007-08-22 14:00:40 -0700353 attrs[XFRMA_ALG_COMP])))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 goto error;
Thomas Graf35a7aa02007-08-22 14:00:40 -0700355 if ((err = attach_encap_tmpl(&x->encap, attrs[XFRMA_ENCAP])))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 goto error;
Thomas Graf35a7aa02007-08-22 14:00:40 -0700357 if ((err = attach_one_addr(&x->coaddr, attrs[XFRMA_COADDR])))
Noriaki TAKAMIYA060f02a2006-08-23 18:18:55 -0700358 goto error;
Herbert Xu72cb6962005-06-20 13:18:08 -0700359 err = xfrm_init_state(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 if (err)
361 goto error;
362
Thomas Graf35a7aa02007-08-22 14:00:40 -0700363 if ((err = attach_sec_ctx(x, attrs[XFRMA_SEC_CTX])))
Trent Jaegerdf718372005-12-13 23:12:27 -0800364 goto error;
365
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 x->km.seq = p->seq;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800367 x->replay_maxdiff = sysctl_xfrm_aevent_rseqth;
368 /* sysctl_xfrm_aevent_etime is in 100ms units */
369 x->replay_maxage = (sysctl_xfrm_aevent_etime*HZ)/XFRM_AE_ETH_M;
370 x->preplay.bitmap = 0;
371 x->preplay.seq = x->replay.seq+x->replay_maxdiff;
372 x->preplay.oseq = x->replay.oseq +x->replay_maxdiff;
373
374 /* override default values from above */
375
Thomas Graf35a7aa02007-08-22 14:00:40 -0700376 xfrm_update_ae_params(x, (struct rtattr **)attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377
378 return x;
379
380error:
381 x->km.state = XFRM_STATE_DEAD;
382 xfrm_state_put(x);
383error_no_put:
384 *errp = err;
385 return NULL;
386}
387
Christoph Hellwig22e70052007-01-02 15:22:30 -0800388static int xfrm_add_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf35a7aa02007-08-22 14:00:40 -0700389 struct rtattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390{
Thomas Graf7b67c852007-08-22 13:53:52 -0700391 struct xfrm_usersa_info *p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 struct xfrm_state *x;
393 int err;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700394 struct km_event c;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395
Thomas Graf35a7aa02007-08-22 14:00:40 -0700396 err = verify_newsa_info(p, attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 if (err)
398 return err;
399
Thomas Graf35a7aa02007-08-22 14:00:40 -0700400 x = xfrm_state_construct(p, attrs, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 if (!x)
402 return err;
403
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700404 xfrm_state_hold(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 if (nlh->nlmsg_type == XFRM_MSG_NEWSA)
406 err = xfrm_state_add(x);
407 else
408 err = xfrm_state_update(x);
409
Joy Latten161a09e2006-11-27 13:11:54 -0600410 xfrm_audit_log(NETLINK_CB(skb).loginuid, NETLINK_CB(skb).sid,
411 AUDIT_MAC_IPSEC_ADDSA, err ? 0 : 1, NULL, x);
412
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 if (err < 0) {
414 x->km.state = XFRM_STATE_DEAD;
Herbert Xu21380b82006-02-22 14:47:13 -0800415 __xfrm_state_put(x);
Patrick McHardy7d6dfe12005-06-18 22:45:31 -0700416 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 }
418
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700419 c.seq = nlh->nlmsg_seq;
420 c.pid = nlh->nlmsg_pid;
Herbert Xuf60f6b82005-06-18 22:44:37 -0700421 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700422
423 km_state_notify(x, &c);
Patrick McHardy7d6dfe12005-06-18 22:45:31 -0700424out:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700425 xfrm_state_put(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 return err;
427}
428
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700429static struct xfrm_state *xfrm_user_state_lookup(struct xfrm_usersa_id *p,
Thomas Graf35a7aa02007-08-22 14:00:40 -0700430 struct rtattr **attrs,
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700431 int *errp)
432{
433 struct xfrm_state *x = NULL;
434 int err;
435
436 if (xfrm_id_proto_match(p->proto, IPSEC_PROTO_ANY)) {
437 err = -ESRCH;
438 x = xfrm_state_lookup(&p->daddr, p->spi, p->proto, p->family);
439 } else {
440 xfrm_address_t *saddr = NULL;
441
Thomas Graf35a7aa02007-08-22 14:00:40 -0700442 verify_one_addr(attrs, XFRMA_SRCADDR, &saddr);
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700443 if (!saddr) {
444 err = -EINVAL;
445 goto out;
446 }
447
Masahide NAKAMURA9abbffe2006-11-24 20:34:51 -0800448 err = -ESRCH;
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700449 x = xfrm_state_lookup_byaddr(&p->daddr, saddr, p->proto,
450 p->family);
451 }
452
453 out:
454 if (!x && errp)
455 *errp = err;
456 return x;
457}
458
Christoph Hellwig22e70052007-01-02 15:22:30 -0800459static int xfrm_del_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf35a7aa02007-08-22 14:00:40 -0700460 struct rtattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461{
462 struct xfrm_state *x;
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700463 int err = -ESRCH;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700464 struct km_event c;
Thomas Graf7b67c852007-08-22 13:53:52 -0700465 struct xfrm_usersa_id *p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466
Thomas Graf35a7aa02007-08-22 14:00:40 -0700467 x = xfrm_user_state_lookup(p, attrs, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 if (x == NULL)
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700469 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470
David S. Miller6f68dc32006-06-08 23:58:52 -0700471 if ((err = security_xfrm_state_delete(x)) != 0)
Catherine Zhangc8c05a82006-06-08 23:39:49 -0700472 goto out;
473
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 if (xfrm_state_kern(x)) {
Catherine Zhangc8c05a82006-06-08 23:39:49 -0700475 err = -EPERM;
476 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 }
478
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700479 err = xfrm_state_delete(x);
Joy Latten161a09e2006-11-27 13:11:54 -0600480
Catherine Zhangc8c05a82006-06-08 23:39:49 -0700481 if (err < 0)
482 goto out;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700483
484 c.seq = nlh->nlmsg_seq;
485 c.pid = nlh->nlmsg_pid;
Herbert Xuf60f6b82005-06-18 22:44:37 -0700486 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700487 km_state_notify(x, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488
Catherine Zhangc8c05a82006-06-08 23:39:49 -0700489out:
Eric Paris16bec312007-03-07 16:02:16 -0800490 xfrm_audit_log(NETLINK_CB(skb).loginuid, NETLINK_CB(skb).sid,
491 AUDIT_MAC_IPSEC_DELSA, err ? 0 : 1, NULL, x);
Catherine Zhangc8c05a82006-06-08 23:39:49 -0700492 xfrm_state_put(x);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700493 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494}
495
496static void copy_to_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p)
497{
498 memcpy(&p->id, &x->id, sizeof(p->id));
499 memcpy(&p->sel, &x->sel, sizeof(p->sel));
500 memcpy(&p->lft, &x->lft, sizeof(p->lft));
501 memcpy(&p->curlft, &x->curlft, sizeof(p->curlft));
502 memcpy(&p->stats, &x->stats, sizeof(p->stats));
David S. Miller54489c142006-10-27 15:29:47 -0700503 memcpy(&p->saddr, &x->props.saddr, sizeof(p->saddr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 p->mode = x->props.mode;
505 p->replay_window = x->props.replay_window;
506 p->reqid = x->props.reqid;
507 p->family = x->props.family;
508 p->flags = x->props.flags;
509 p->seq = x->km.seq;
510}
511
512struct xfrm_dump_info {
513 struct sk_buff *in_skb;
514 struct sk_buff *out_skb;
515 u32 nlmsg_seq;
516 u16 nlmsg_flags;
517 int start_idx;
518 int this_idx;
519};
520
Thomas Grafc0144be2007-08-22 13:55:43 -0700521static int copy_sec_ctx(struct xfrm_sec_ctx *s, struct sk_buff *skb)
522{
523 int ctx_size = sizeof(struct xfrm_sec_ctx) + s->ctx_len;
524 struct xfrm_user_sec_ctx *uctx;
525 struct nlattr *attr;
526
527 attr = nla_reserve(skb, XFRMA_SEC_CTX, ctx_size);
528 if (attr == NULL)
529 return -EMSGSIZE;
530
531 uctx = nla_data(attr);
532 uctx->exttype = XFRMA_SEC_CTX;
533 uctx->len = ctx_size;
534 uctx->ctx_doi = s->ctx_doi;
535 uctx->ctx_alg = s->ctx_alg;
536 uctx->ctx_len = s->ctx_len;
537 memcpy(uctx + 1, s->ctx_str, s->ctx_len);
538
539 return 0;
540}
541
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542static int dump_one_state(struct xfrm_state *x, int count, void *ptr)
543{
544 struct xfrm_dump_info *sp = ptr;
545 struct sk_buff *in_skb = sp->in_skb;
546 struct sk_buff *skb = sp->out_skb;
547 struct xfrm_usersa_info *p;
548 struct nlmsghdr *nlh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549
550 if (sp->this_idx < sp->start_idx)
551 goto out;
552
Thomas Graf79b8b7f2007-08-22 12:46:53 -0700553 nlh = nlmsg_put(skb, NETLINK_CB(in_skb).pid, sp->nlmsg_seq,
554 XFRM_MSG_NEWSA, sizeof(*p), sp->nlmsg_flags);
555 if (nlh == NULL)
556 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557
Thomas Graf7b67c852007-08-22 13:53:52 -0700558 p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 copy_to_user_state(x, p);
560
561 if (x->aalg)
Thomas Grafc26445a2007-08-22 13:56:23 -0700562 NLA_PUT(skb, XFRMA_ALG_AUTH, alg_len(x->aalg), x->aalg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 if (x->ealg)
Thomas Grafc26445a2007-08-22 13:56:23 -0700564 NLA_PUT(skb, XFRMA_ALG_CRYPT, alg_len(x->ealg), x->ealg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 if (x->calg)
Thomas Grafc0144be2007-08-22 13:55:43 -0700566 NLA_PUT(skb, XFRMA_ALG_COMP, sizeof(*(x->calg)), x->calg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567
568 if (x->encap)
Thomas Grafc0144be2007-08-22 13:55:43 -0700569 NLA_PUT(skb, XFRMA_ENCAP, sizeof(*x->encap), x->encap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570
Thomas Grafc0144be2007-08-22 13:55:43 -0700571 if (x->security && copy_sec_ctx(x->security, skb) < 0)
572 goto nla_put_failure;
Noriaki TAKAMIYA060f02a2006-08-23 18:18:55 -0700573
574 if (x->coaddr)
Thomas Grafc0144be2007-08-22 13:55:43 -0700575 NLA_PUT(skb, XFRMA_COADDR, sizeof(*x->coaddr), x->coaddr);
Noriaki TAKAMIYA060f02a2006-08-23 18:18:55 -0700576
Masahide NAKAMURA9afaca02006-08-23 18:20:16 -0700577 if (x->lastused)
Thomas Grafc0144be2007-08-22 13:55:43 -0700578 NLA_PUT_U64(skb, XFRMA_LASTUSED, x->lastused);
Masahide NAKAMURA9afaca02006-08-23 18:20:16 -0700579
Thomas Graf98250692007-08-22 12:47:26 -0700580 nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581out:
582 sp->this_idx++;
583 return 0;
584
Thomas Grafc0144be2007-08-22 13:55:43 -0700585nla_put_failure:
Thomas Graf98250692007-08-22 12:47:26 -0700586 nlmsg_cancel(skb, nlh);
587 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588}
589
590static int xfrm_dump_sa(struct sk_buff *skb, struct netlink_callback *cb)
591{
592 struct xfrm_dump_info info;
593
594 info.in_skb = cb->skb;
595 info.out_skb = skb;
596 info.nlmsg_seq = cb->nlh->nlmsg_seq;
597 info.nlmsg_flags = NLM_F_MULTI;
598 info.this_idx = 0;
599 info.start_idx = cb->args[0];
Masahide NAKAMURAdc00a522006-08-23 17:49:52 -0700600 (void) xfrm_state_walk(0, dump_one_state, &info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 cb->args[0] = info.this_idx;
602
603 return skb->len;
604}
605
606static struct sk_buff *xfrm_state_netlink(struct sk_buff *in_skb,
607 struct xfrm_state *x, u32 seq)
608{
609 struct xfrm_dump_info info;
610 struct sk_buff *skb;
611
Thomas Graf7deb2262007-08-22 13:57:39 -0700612 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 if (!skb)
614 return ERR_PTR(-ENOMEM);
615
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 info.in_skb = in_skb;
617 info.out_skb = skb;
618 info.nlmsg_seq = seq;
619 info.nlmsg_flags = 0;
620 info.this_idx = info.start_idx = 0;
621
622 if (dump_one_state(x, 0, &info)) {
623 kfree_skb(skb);
624 return NULL;
625 }
626
627 return skb;
628}
629
Thomas Graf7deb2262007-08-22 13:57:39 -0700630static inline size_t xfrm_spdinfo_msgsize(void)
631{
632 return NLMSG_ALIGN(4)
633 + nla_total_size(sizeof(struct xfrmu_spdinfo))
634 + nla_total_size(sizeof(struct xfrmu_spdhinfo));
635}
636
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700637static int build_spdinfo(struct sk_buff *skb, u32 pid, u32 seq, u32 flags)
638{
Jamal Hadi Salim5a6d3412007-05-04 12:55:39 -0700639 struct xfrmk_spdinfo si;
640 struct xfrmu_spdinfo spc;
641 struct xfrmu_spdhinfo sph;
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700642 struct nlmsghdr *nlh;
643 u32 *f;
644
645 nlh = nlmsg_put(skb, pid, seq, XFRM_MSG_NEWSPDINFO, sizeof(u32), 0);
646 if (nlh == NULL) /* shouldnt really happen ... */
647 return -EMSGSIZE;
648
649 f = nlmsg_data(nlh);
650 *f = flags;
651 xfrm_spd_getinfo(&si);
Jamal Hadi Salim5a6d3412007-05-04 12:55:39 -0700652 spc.incnt = si.incnt;
653 spc.outcnt = si.outcnt;
654 spc.fwdcnt = si.fwdcnt;
655 spc.inscnt = si.inscnt;
656 spc.outscnt = si.outscnt;
657 spc.fwdscnt = si.fwdscnt;
658 sph.spdhcnt = si.spdhcnt;
659 sph.spdhmcnt = si.spdhmcnt;
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700660
Jamal Hadi Salim5a6d3412007-05-04 12:55:39 -0700661 NLA_PUT(skb, XFRMA_SPD_INFO, sizeof(spc), &spc);
662 NLA_PUT(skb, XFRMA_SPD_HINFO, sizeof(sph), &sph);
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700663
664 return nlmsg_end(skb, nlh);
665
666nla_put_failure:
667 nlmsg_cancel(skb, nlh);
668 return -EMSGSIZE;
669}
670
671static int xfrm_get_spdinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf35a7aa02007-08-22 14:00:40 -0700672 struct rtattr **attrs)
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700673{
674 struct sk_buff *r_skb;
Thomas Graf7b67c852007-08-22 13:53:52 -0700675 u32 *flags = nlmsg_data(nlh);
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700676 u32 spid = NETLINK_CB(skb).pid;
677 u32 seq = nlh->nlmsg_seq;
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700678
Thomas Graf7deb2262007-08-22 13:57:39 -0700679 r_skb = nlmsg_new(xfrm_spdinfo_msgsize(), GFP_ATOMIC);
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700680 if (r_skb == NULL)
681 return -ENOMEM;
682
683 if (build_spdinfo(r_skb, spid, seq, *flags) < 0)
684 BUG();
685
686 return nlmsg_unicast(xfrm_nl, r_skb, spid);
687}
688
Thomas Graf7deb2262007-08-22 13:57:39 -0700689static inline size_t xfrm_sadinfo_msgsize(void)
690{
691 return NLMSG_ALIGN(4)
692 + nla_total_size(sizeof(struct xfrmu_sadhinfo))
693 + nla_total_size(4); /* XFRMA_SAD_CNT */
694}
695
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700696static int build_sadinfo(struct sk_buff *skb, u32 pid, u32 seq, u32 flags)
697{
Jamal Hadi Salimaf11e312007-05-04 12:55:13 -0700698 struct xfrmk_sadinfo si;
699 struct xfrmu_sadhinfo sh;
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700700 struct nlmsghdr *nlh;
701 u32 *f;
702
703 nlh = nlmsg_put(skb, pid, seq, XFRM_MSG_NEWSADINFO, sizeof(u32), 0);
704 if (nlh == NULL) /* shouldnt really happen ... */
705 return -EMSGSIZE;
706
707 f = nlmsg_data(nlh);
708 *f = flags;
709 xfrm_sad_getinfo(&si);
710
Jamal Hadi Salimaf11e312007-05-04 12:55:13 -0700711 sh.sadhmcnt = si.sadhmcnt;
712 sh.sadhcnt = si.sadhcnt;
713
714 NLA_PUT_U32(skb, XFRMA_SAD_CNT, si.sadcnt);
715 NLA_PUT(skb, XFRMA_SAD_HINFO, sizeof(sh), &sh);
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700716
717 return nlmsg_end(skb, nlh);
718
719nla_put_failure:
720 nlmsg_cancel(skb, nlh);
721 return -EMSGSIZE;
722}
723
724static int xfrm_get_sadinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf35a7aa02007-08-22 14:00:40 -0700725 struct rtattr **attrs)
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700726{
727 struct sk_buff *r_skb;
Thomas Graf7b67c852007-08-22 13:53:52 -0700728 u32 *flags = nlmsg_data(nlh);
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700729 u32 spid = NETLINK_CB(skb).pid;
730 u32 seq = nlh->nlmsg_seq;
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700731
Thomas Graf7deb2262007-08-22 13:57:39 -0700732 r_skb = nlmsg_new(xfrm_sadinfo_msgsize(), GFP_ATOMIC);
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700733 if (r_skb == NULL)
734 return -ENOMEM;
735
736 if (build_sadinfo(r_skb, spid, seq, *flags) < 0)
737 BUG();
738
739 return nlmsg_unicast(xfrm_nl, r_skb, spid);
740}
741
Christoph Hellwig22e70052007-01-02 15:22:30 -0800742static int xfrm_get_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf35a7aa02007-08-22 14:00:40 -0700743 struct rtattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744{
Thomas Graf7b67c852007-08-22 13:53:52 -0700745 struct xfrm_usersa_id *p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 struct xfrm_state *x;
747 struct sk_buff *resp_skb;
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700748 int err = -ESRCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749
Thomas Graf35a7aa02007-08-22 14:00:40 -0700750 x = xfrm_user_state_lookup(p, attrs, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 if (x == NULL)
752 goto out_noput;
753
754 resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
755 if (IS_ERR(resp_skb)) {
756 err = PTR_ERR(resp_skb);
757 } else {
Thomas Graf082a1ad2007-08-22 13:54:36 -0700758 err = nlmsg_unicast(xfrm_nl, resp_skb, NETLINK_CB(skb).pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 }
760 xfrm_state_put(x);
761out_noput:
762 return err;
763}
764
765static int verify_userspi_info(struct xfrm_userspi_info *p)
766{
767 switch (p->info.id.proto) {
768 case IPPROTO_AH:
769 case IPPROTO_ESP:
770 break;
771
772 case IPPROTO_COMP:
773 /* IPCOMP spi is 16-bits. */
774 if (p->max >= 0x10000)
775 return -EINVAL;
776 break;
777
778 default:
779 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700780 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781
782 if (p->min > p->max)
783 return -EINVAL;
784
785 return 0;
786}
787
Christoph Hellwig22e70052007-01-02 15:22:30 -0800788static int xfrm_alloc_userspi(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf35a7aa02007-08-22 14:00:40 -0700789 struct rtattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790{
791 struct xfrm_state *x;
792 struct xfrm_userspi_info *p;
793 struct sk_buff *resp_skb;
794 xfrm_address_t *daddr;
795 int family;
796 int err;
797
Thomas Graf7b67c852007-08-22 13:53:52 -0700798 p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 err = verify_userspi_info(p);
800 if (err)
801 goto out_noput;
802
803 family = p->info.family;
804 daddr = &p->info.id.daddr;
805
806 x = NULL;
807 if (p->info.seq) {
808 x = xfrm_find_acq_byseq(p->info.seq);
809 if (x && xfrm_addr_cmp(&x->id.daddr, daddr, family)) {
810 xfrm_state_put(x);
811 x = NULL;
812 }
813 }
814
815 if (!x)
816 x = xfrm_find_acq(p->info.mode, p->info.reqid,
817 p->info.id.proto, daddr,
818 &p->info.saddr, 1,
819 family);
820 err = -ENOENT;
821 if (x == NULL)
822 goto out_noput;
823
824 resp_skb = ERR_PTR(-ENOENT);
825
826 spin_lock_bh(&x->lock);
827 if (x->km.state != XFRM_STATE_DEAD) {
828 xfrm_alloc_spi(x, htonl(p->min), htonl(p->max));
829 if (x->id.spi)
830 resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
831 }
832 spin_unlock_bh(&x->lock);
833
834 if (IS_ERR(resp_skb)) {
835 err = PTR_ERR(resp_skb);
836 goto out;
837 }
838
Thomas Graf082a1ad2007-08-22 13:54:36 -0700839 err = nlmsg_unicast(xfrm_nl, resp_skb, NETLINK_CB(skb).pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840
841out:
842 xfrm_state_put(x);
843out_noput:
844 return err;
845}
846
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -0800847static int verify_policy_dir(u8 dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848{
849 switch (dir) {
850 case XFRM_POLICY_IN:
851 case XFRM_POLICY_OUT:
852 case XFRM_POLICY_FWD:
853 break;
854
855 default:
856 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700857 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858
859 return 0;
860}
861
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -0800862static int verify_policy_type(u8 type)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -0700863{
864 switch (type) {
865 case XFRM_POLICY_TYPE_MAIN:
866#ifdef CONFIG_XFRM_SUB_POLICY
867 case XFRM_POLICY_TYPE_SUB:
868#endif
869 break;
870
871 default:
872 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700873 }
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -0700874
875 return 0;
876}
877
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878static int verify_newpolicy_info(struct xfrm_userpolicy_info *p)
879{
880 switch (p->share) {
881 case XFRM_SHARE_ANY:
882 case XFRM_SHARE_SESSION:
883 case XFRM_SHARE_USER:
884 case XFRM_SHARE_UNIQUE:
885 break;
886
887 default:
888 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700889 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890
891 switch (p->action) {
892 case XFRM_POLICY_ALLOW:
893 case XFRM_POLICY_BLOCK:
894 break;
895
896 default:
897 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700898 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899
900 switch (p->sel.family) {
901 case AF_INET:
902 break;
903
904 case AF_INET6:
905#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
906 break;
907#else
908 return -EAFNOSUPPORT;
909#endif
910
911 default:
912 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700913 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914
915 return verify_policy_dir(p->dir);
916}
917
Thomas Graf35a7aa02007-08-22 14:00:40 -0700918static int copy_from_user_sec_ctx(struct xfrm_policy *pol, struct rtattr **attrs)
Trent Jaegerdf718372005-12-13 23:12:27 -0800919{
Thomas Graf35a7aa02007-08-22 14:00:40 -0700920 struct rtattr *rt = attrs[XFRMA_SEC_CTX];
Trent Jaegerdf718372005-12-13 23:12:27 -0800921 struct xfrm_user_sec_ctx *uctx;
922
923 if (!rt)
924 return 0;
925
926 uctx = RTA_DATA(rt);
927 return security_xfrm_policy_alloc(pol, uctx);
928}
929
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930static void copy_templates(struct xfrm_policy *xp, struct xfrm_user_tmpl *ut,
931 int nr)
932{
933 int i;
934
935 xp->xfrm_nr = nr;
936 for (i = 0; i < nr; i++, ut++) {
937 struct xfrm_tmpl *t = &xp->xfrm_vec[i];
938
939 memcpy(&t->id, &ut->id, sizeof(struct xfrm_id));
940 memcpy(&t->saddr, &ut->saddr,
941 sizeof(xfrm_address_t));
942 t->reqid = ut->reqid;
943 t->mode = ut->mode;
944 t->share = ut->share;
945 t->optional = ut->optional;
946 t->aalgos = ut->aalgos;
947 t->ealgos = ut->ealgos;
948 t->calgos = ut->calgos;
Miika Komu8511d012006-11-30 16:40:51 -0800949 t->encap_family = ut->family;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 }
951}
952
David S. Millerb4ad86bf2006-12-03 19:19:26 -0800953static int validate_tmpl(int nr, struct xfrm_user_tmpl *ut, u16 family)
954{
955 int i;
956
957 if (nr > XFRM_MAX_DEPTH)
958 return -EINVAL;
959
960 for (i = 0; i < nr; i++) {
961 /* We never validated the ut->family value, so many
962 * applications simply leave it at zero. The check was
963 * never made and ut->family was ignored because all
964 * templates could be assumed to have the same family as
965 * the policy itself. Now that we will have ipv4-in-ipv6
966 * and ipv6-in-ipv4 tunnels, this is no longer true.
967 */
968 if (!ut[i].family)
969 ut[i].family = family;
970
971 switch (ut[i].family) {
972 case AF_INET:
973 break;
974#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
975 case AF_INET6:
976 break;
977#endif
978 default:
979 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700980 }
David S. Millerb4ad86bf2006-12-03 19:19:26 -0800981 }
982
983 return 0;
984}
985
Thomas Graf35a7aa02007-08-22 14:00:40 -0700986static int copy_from_user_tmpl(struct xfrm_policy *pol, struct rtattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987{
Thomas Graf35a7aa02007-08-22 14:00:40 -0700988 struct rtattr *rt = attrs[XFRMA_TMPL];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989
990 if (!rt) {
991 pol->xfrm_nr = 0;
992 } else {
David S. Millerb4ad86bf2006-12-03 19:19:26 -0800993 struct xfrm_user_tmpl *utmpl = RTA_DATA(rt);
994 int nr = (rt->rta_len - sizeof(*rt)) / sizeof(*utmpl);
995 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996
David S. Millerb4ad86bf2006-12-03 19:19:26 -0800997 err = validate_tmpl(nr, utmpl, pol->family);
998 if (err)
999 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000
1001 copy_templates(pol, RTA_DATA(rt), nr);
1002 }
1003 return 0;
1004}
1005
Thomas Graf35a7aa02007-08-22 14:00:40 -07001006static int copy_from_user_policy_type(u8 *tp, struct rtattr **attrs)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001007{
Thomas Graf35a7aa02007-08-22 14:00:40 -07001008 struct rtattr *rt = attrs[XFRMA_POLICY_TYPE];
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001009 struct xfrm_userpolicy_type *upt;
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001010 u8 type = XFRM_POLICY_TYPE_MAIN;
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001011 int err;
1012
1013 if (rt) {
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001014 upt = RTA_DATA(rt);
1015 type = upt->type;
1016 }
1017
1018 err = verify_policy_type(type);
1019 if (err)
1020 return err;
1021
1022 *tp = type;
1023 return 0;
1024}
1025
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026static void copy_from_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p)
1027{
1028 xp->priority = p->priority;
1029 xp->index = p->index;
1030 memcpy(&xp->selector, &p->sel, sizeof(xp->selector));
1031 memcpy(&xp->lft, &p->lft, sizeof(xp->lft));
1032 xp->action = p->action;
1033 xp->flags = p->flags;
1034 xp->family = p->sel.family;
1035 /* XXX xp->share = p->share; */
1036}
1037
1038static void copy_to_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p, int dir)
1039{
1040 memcpy(&p->sel, &xp->selector, sizeof(p->sel));
1041 memcpy(&p->lft, &xp->lft, sizeof(p->lft));
1042 memcpy(&p->curlft, &xp->curlft, sizeof(p->curlft));
1043 p->priority = xp->priority;
1044 p->index = xp->index;
1045 p->sel.family = xp->family;
1046 p->dir = dir;
1047 p->action = xp->action;
1048 p->flags = xp->flags;
1049 p->share = XFRM_SHARE_ANY; /* XXX xp->share */
1050}
1051
Thomas Graf35a7aa02007-08-22 14:00:40 -07001052static struct xfrm_policy *xfrm_policy_construct(struct xfrm_userpolicy_info *p, struct rtattr **attrs, int *errp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053{
1054 struct xfrm_policy *xp = xfrm_policy_alloc(GFP_KERNEL);
1055 int err;
1056
1057 if (!xp) {
1058 *errp = -ENOMEM;
1059 return NULL;
1060 }
1061
1062 copy_from_user_policy(xp, p);
Trent Jaegerdf718372005-12-13 23:12:27 -08001063
Thomas Graf35a7aa02007-08-22 14:00:40 -07001064 err = copy_from_user_policy_type(&xp->type, attrs);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001065 if (err)
1066 goto error;
1067
Thomas Graf35a7aa02007-08-22 14:00:40 -07001068 if (!(err = copy_from_user_tmpl(xp, attrs)))
1069 err = copy_from_user_sec_ctx(xp, attrs);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001070 if (err)
1071 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072
1073 return xp;
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001074 error:
1075 *errp = err;
1076 kfree(xp);
1077 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078}
1079
Christoph Hellwig22e70052007-01-02 15:22:30 -08001080static int xfrm_add_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf35a7aa02007-08-22 14:00:40 -07001081 struct rtattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082{
Thomas Graf7b67c852007-08-22 13:53:52 -07001083 struct xfrm_userpolicy_info *p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 struct xfrm_policy *xp;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001085 struct km_event c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 int err;
1087 int excl;
1088
1089 err = verify_newpolicy_info(p);
1090 if (err)
1091 return err;
Thomas Graf35a7aa02007-08-22 14:00:40 -07001092 err = verify_sec_ctx_len(attrs);
Trent Jaegerdf718372005-12-13 23:12:27 -08001093 if (err)
1094 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095
Thomas Graf35a7aa02007-08-22 14:00:40 -07001096 xp = xfrm_policy_construct(p, attrs, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 if (!xp)
1098 return err;
1099
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001100 /* shouldnt excl be based on nlh flags??
1101 * Aha! this is anti-netlink really i.e more pfkey derived
1102 * in netlink excl is a flag and you wouldnt need
1103 * a type XFRM_MSG_UPDPOLICY - JHS */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 excl = nlh->nlmsg_type == XFRM_MSG_NEWPOLICY;
1105 err = xfrm_policy_insert(p->dir, xp, excl);
Joy Latten161a09e2006-11-27 13:11:54 -06001106 xfrm_audit_log(NETLINK_CB(skb).loginuid, NETLINK_CB(skb).sid,
1107 AUDIT_MAC_IPSEC_DELSPD, err ? 0 : 1, xp, NULL);
1108
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 if (err) {
Trent Jaeger5f8ac642006-01-06 13:22:39 -08001110 security_xfrm_policy_free(xp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 kfree(xp);
1112 return err;
1113 }
1114
Herbert Xuf60f6b82005-06-18 22:44:37 -07001115 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001116 c.seq = nlh->nlmsg_seq;
1117 c.pid = nlh->nlmsg_pid;
1118 km_policy_notify(xp, p->dir, &c);
1119
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 xfrm_pol_put(xp);
1121
1122 return 0;
1123}
1124
1125static int copy_to_user_tmpl(struct xfrm_policy *xp, struct sk_buff *skb)
1126{
1127 struct xfrm_user_tmpl vec[XFRM_MAX_DEPTH];
1128 int i;
1129
1130 if (xp->xfrm_nr == 0)
1131 return 0;
1132
1133 for (i = 0; i < xp->xfrm_nr; i++) {
1134 struct xfrm_user_tmpl *up = &vec[i];
1135 struct xfrm_tmpl *kp = &xp->xfrm_vec[i];
1136
1137 memcpy(&up->id, &kp->id, sizeof(up->id));
Miika Komu8511d012006-11-30 16:40:51 -08001138 up->family = kp->encap_family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 memcpy(&up->saddr, &kp->saddr, sizeof(up->saddr));
1140 up->reqid = kp->reqid;
1141 up->mode = kp->mode;
1142 up->share = kp->share;
1143 up->optional = kp->optional;
1144 up->aalgos = kp->aalgos;
1145 up->ealgos = kp->ealgos;
1146 up->calgos = kp->calgos;
1147 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148
Thomas Grafc0144be2007-08-22 13:55:43 -07001149 return nla_put(skb, XFRMA_TMPL,
1150 sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr, vec);
Trent Jaegerdf718372005-12-13 23:12:27 -08001151}
1152
Serge Hallyn0d681622006-07-24 23:30:44 -07001153static inline int copy_to_user_state_sec_ctx(struct xfrm_state *x, struct sk_buff *skb)
1154{
1155 if (x->security) {
1156 return copy_sec_ctx(x->security, skb);
1157 }
1158 return 0;
1159}
1160
1161static inline int copy_to_user_sec_ctx(struct xfrm_policy *xp, struct sk_buff *skb)
1162{
1163 if (xp->security) {
1164 return copy_sec_ctx(xp->security, skb);
1165 }
1166 return 0;
1167}
Thomas Grafcfbfd452007-08-22 13:57:04 -07001168static inline size_t userpolicy_type_attrsize(void)
1169{
1170#ifdef CONFIG_XFRM_SUB_POLICY
1171 return nla_total_size(sizeof(struct xfrm_userpolicy_type));
1172#else
1173 return 0;
1174#endif
1175}
Serge Hallyn0d681622006-07-24 23:30:44 -07001176
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001177#ifdef CONFIG_XFRM_SUB_POLICY
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001178static int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001179{
Thomas Grafc0144be2007-08-22 13:55:43 -07001180 struct xfrm_userpolicy_type upt = {
1181 .type = type,
1182 };
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001183
Thomas Grafc0144be2007-08-22 13:55:43 -07001184 return nla_put(skb, XFRMA_POLICY_TYPE, sizeof(upt), &upt);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001185}
1186
1187#else
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001188static inline int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001189{
1190 return 0;
1191}
1192#endif
1193
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194static int dump_one_policy(struct xfrm_policy *xp, int dir, int count, void *ptr)
1195{
1196 struct xfrm_dump_info *sp = ptr;
1197 struct xfrm_userpolicy_info *p;
1198 struct sk_buff *in_skb = sp->in_skb;
1199 struct sk_buff *skb = sp->out_skb;
1200 struct nlmsghdr *nlh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201
1202 if (sp->this_idx < sp->start_idx)
1203 goto out;
1204
Thomas Graf79b8b7f2007-08-22 12:46:53 -07001205 nlh = nlmsg_put(skb, NETLINK_CB(in_skb).pid, sp->nlmsg_seq,
1206 XFRM_MSG_NEWPOLICY, sizeof(*p), sp->nlmsg_flags);
1207 if (nlh == NULL)
1208 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209
Thomas Graf7b67c852007-08-22 13:53:52 -07001210 p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 copy_to_user_policy(xp, p, dir);
1212 if (copy_to_user_tmpl(xp, skb) < 0)
1213 goto nlmsg_failure;
Trent Jaegerdf718372005-12-13 23:12:27 -08001214 if (copy_to_user_sec_ctx(xp, skb))
1215 goto nlmsg_failure;
Jamal Hadi Salim1459bb32006-11-20 16:51:22 -08001216 if (copy_to_user_policy_type(xp->type, skb) < 0)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001217 goto nlmsg_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218
Thomas Graf98250692007-08-22 12:47:26 -07001219 nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220out:
1221 sp->this_idx++;
1222 return 0;
1223
1224nlmsg_failure:
Thomas Graf98250692007-08-22 12:47:26 -07001225 nlmsg_cancel(skb, nlh);
1226 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227}
1228
1229static int xfrm_dump_policy(struct sk_buff *skb, struct netlink_callback *cb)
1230{
1231 struct xfrm_dump_info info;
1232
1233 info.in_skb = cb->skb;
1234 info.out_skb = skb;
1235 info.nlmsg_seq = cb->nlh->nlmsg_seq;
1236 info.nlmsg_flags = NLM_F_MULTI;
1237 info.this_idx = 0;
1238 info.start_idx = cb->args[0];
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001239 (void) xfrm_policy_walk(XFRM_POLICY_TYPE_MAIN, dump_one_policy, &info);
1240#ifdef CONFIG_XFRM_SUB_POLICY
1241 (void) xfrm_policy_walk(XFRM_POLICY_TYPE_SUB, dump_one_policy, &info);
1242#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 cb->args[0] = info.this_idx;
1244
1245 return skb->len;
1246}
1247
1248static struct sk_buff *xfrm_policy_netlink(struct sk_buff *in_skb,
1249 struct xfrm_policy *xp,
1250 int dir, u32 seq)
1251{
1252 struct xfrm_dump_info info;
1253 struct sk_buff *skb;
1254
Thomas Graf7deb2262007-08-22 13:57:39 -07001255 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 if (!skb)
1257 return ERR_PTR(-ENOMEM);
1258
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 info.in_skb = in_skb;
1260 info.out_skb = skb;
1261 info.nlmsg_seq = seq;
1262 info.nlmsg_flags = 0;
1263 info.this_idx = info.start_idx = 0;
1264
1265 if (dump_one_policy(xp, dir, 0, &info) < 0) {
1266 kfree_skb(skb);
1267 return NULL;
1268 }
1269
1270 return skb;
1271}
1272
Christoph Hellwig22e70052007-01-02 15:22:30 -08001273static int xfrm_get_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf35a7aa02007-08-22 14:00:40 -07001274 struct rtattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275{
1276 struct xfrm_policy *xp;
1277 struct xfrm_userpolicy_id *p;
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001278 u8 type = XFRM_POLICY_TYPE_MAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 int err;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001280 struct km_event c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 int delete;
1282
Thomas Graf7b67c852007-08-22 13:53:52 -07001283 p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 delete = nlh->nlmsg_type == XFRM_MSG_DELPOLICY;
1285
Thomas Graf35a7aa02007-08-22 14:00:40 -07001286 err = copy_from_user_policy_type(&type, attrs);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001287 if (err)
1288 return err;
1289
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290 err = verify_policy_dir(p->dir);
1291 if (err)
1292 return err;
1293
1294 if (p->index)
Eric Parisef41aaa2007-03-07 15:37:58 -08001295 xp = xfrm_policy_byid(type, p->dir, p->index, delete, &err);
Trent Jaegerdf718372005-12-13 23:12:27 -08001296 else {
Thomas Graf35a7aa02007-08-22 14:00:40 -07001297 struct rtattr *rt = attrs[XFRMA_SEC_CTX];
Trent Jaegerdf718372005-12-13 23:12:27 -08001298 struct xfrm_policy tmp;
1299
Thomas Graf35a7aa02007-08-22 14:00:40 -07001300 err = verify_sec_ctx_len(attrs);
Trent Jaegerdf718372005-12-13 23:12:27 -08001301 if (err)
1302 return err;
1303
1304 memset(&tmp, 0, sizeof(struct xfrm_policy));
1305 if (rt) {
1306 struct xfrm_user_sec_ctx *uctx = RTA_DATA(rt);
1307
1308 if ((err = security_xfrm_policy_alloc(&tmp, uctx)))
1309 return err;
1310 }
Eric Parisef41aaa2007-03-07 15:37:58 -08001311 xp = xfrm_policy_bysel_ctx(type, p->dir, &p->sel, tmp.security,
1312 delete, &err);
Trent Jaegerdf718372005-12-13 23:12:27 -08001313 security_xfrm_policy_free(&tmp);
1314 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315 if (xp == NULL)
1316 return -ENOENT;
1317
1318 if (!delete) {
1319 struct sk_buff *resp_skb;
1320
1321 resp_skb = xfrm_policy_netlink(skb, xp, p->dir, nlh->nlmsg_seq);
1322 if (IS_ERR(resp_skb)) {
1323 err = PTR_ERR(resp_skb);
1324 } else {
Thomas Graf082a1ad2007-08-22 13:54:36 -07001325 err = nlmsg_unicast(xfrm_nl, resp_skb,
1326 NETLINK_CB(skb).pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327 }
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001328 } else {
David S. Miller13fcfbb02007-02-12 13:53:54 -08001329 xfrm_audit_log(NETLINK_CB(skb).loginuid, NETLINK_CB(skb).sid,
1330 AUDIT_MAC_IPSEC_DELSPD, err ? 0 : 1, xp, NULL);
1331
1332 if (err != 0)
Catherine Zhangc8c05a82006-06-08 23:39:49 -07001333 goto out;
David S. Miller13fcfbb02007-02-12 13:53:54 -08001334
Herbert Xue7443892005-06-18 22:44:18 -07001335 c.data.byid = p->index;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001336 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001337 c.seq = nlh->nlmsg_seq;
1338 c.pid = nlh->nlmsg_pid;
1339 km_policy_notify(xp, p->dir, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340 }
1341
Catherine Zhangc8c05a82006-06-08 23:39:49 -07001342out:
Eric Parisef41aaa2007-03-07 15:37:58 -08001343 xfrm_pol_put(xp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344 return err;
1345}
1346
Christoph Hellwig22e70052007-01-02 15:22:30 -08001347static int xfrm_flush_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf35a7aa02007-08-22 14:00:40 -07001348 struct rtattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349{
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001350 struct km_event c;
Thomas Graf7b67c852007-08-22 13:53:52 -07001351 struct xfrm_usersa_flush *p = nlmsg_data(nlh);
Joy Latten161a09e2006-11-27 13:11:54 -06001352 struct xfrm_audit audit_info;
Joy Latten4aa2e622007-06-04 19:05:57 -04001353 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354
Joy Latten161a09e2006-11-27 13:11:54 -06001355 audit_info.loginuid = NETLINK_CB(skb).loginuid;
1356 audit_info.secid = NETLINK_CB(skb).sid;
Joy Latten4aa2e622007-06-04 19:05:57 -04001357 err = xfrm_state_flush(p->proto, &audit_info);
1358 if (err)
1359 return err;
Herbert Xubf088672005-06-18 22:44:00 -07001360 c.data.proto = p->proto;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001361 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001362 c.seq = nlh->nlmsg_seq;
1363 c.pid = nlh->nlmsg_pid;
1364 km_state_notify(NULL, &c);
1365
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 return 0;
1367}
1368
Thomas Graf7deb2262007-08-22 13:57:39 -07001369static inline size_t xfrm_aevent_msgsize(void)
1370{
1371 return NLMSG_ALIGN(sizeof(struct xfrm_aevent_id))
1372 + nla_total_size(sizeof(struct xfrm_replay_state))
1373 + nla_total_size(sizeof(struct xfrm_lifetime_cur))
1374 + nla_total_size(4) /* XFRM_AE_RTHR */
1375 + nla_total_size(4); /* XFRM_AE_ETHR */
1376}
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001377
1378static int build_aevent(struct sk_buff *skb, struct xfrm_state *x, struct km_event *c)
1379{
1380 struct xfrm_aevent_id *id;
1381 struct nlmsghdr *nlh;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001382
Thomas Graf79b8b7f2007-08-22 12:46:53 -07001383 nlh = nlmsg_put(skb, c->pid, c->seq, XFRM_MSG_NEWAE, sizeof(*id), 0);
1384 if (nlh == NULL)
1385 return -EMSGSIZE;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001386
Thomas Graf7b67c852007-08-22 13:53:52 -07001387 id = nlmsg_data(nlh);
Jamal Hadi Salim2b5f6dc2006-12-02 22:22:25 -08001388 memcpy(&id->sa_id.daddr, &x->id.daddr,sizeof(x->id.daddr));
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001389 id->sa_id.spi = x->id.spi;
1390 id->sa_id.family = x->props.family;
1391 id->sa_id.proto = x->id.proto;
Jamal Hadi Salim2b5f6dc2006-12-02 22:22:25 -08001392 memcpy(&id->saddr, &x->props.saddr,sizeof(x->props.saddr));
1393 id->reqid = x->props.reqid;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001394 id->flags = c->data.aevent;
1395
Thomas Grafc0144be2007-08-22 13:55:43 -07001396 NLA_PUT(skb, XFRMA_REPLAY_VAL, sizeof(x->replay), &x->replay);
1397 NLA_PUT(skb, XFRMA_LTIME_VAL, sizeof(x->curlft), &x->curlft);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001398
Thomas Grafc0144be2007-08-22 13:55:43 -07001399 if (id->flags & XFRM_AE_RTHR)
1400 NLA_PUT_U32(skb, XFRMA_REPLAY_THRESH, x->replay_maxdiff);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001401
Thomas Grafc0144be2007-08-22 13:55:43 -07001402 if (id->flags & XFRM_AE_ETHR)
1403 NLA_PUT_U32(skb, XFRMA_ETIMER_THRESH,
1404 x->replay_maxage * 10 / HZ);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001405
Thomas Graf98250692007-08-22 12:47:26 -07001406 return nlmsg_end(skb, nlh);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001407
Thomas Grafc0144be2007-08-22 13:55:43 -07001408nla_put_failure:
Thomas Graf98250692007-08-22 12:47:26 -07001409 nlmsg_cancel(skb, nlh);
1410 return -EMSGSIZE;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001411}
1412
Christoph Hellwig22e70052007-01-02 15:22:30 -08001413static int xfrm_get_ae(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf35a7aa02007-08-22 14:00:40 -07001414 struct rtattr **attrs)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001415{
1416 struct xfrm_state *x;
1417 struct sk_buff *r_skb;
1418 int err;
1419 struct km_event c;
Thomas Graf7b67c852007-08-22 13:53:52 -07001420 struct xfrm_aevent_id *p = nlmsg_data(nlh);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001421 struct xfrm_usersa_id *id = &p->sa_id;
1422
Thomas Graf7deb2262007-08-22 13:57:39 -07001423 r_skb = nlmsg_new(xfrm_aevent_msgsize(), GFP_ATOMIC);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001424 if (r_skb == NULL)
1425 return -ENOMEM;
1426
1427 x = xfrm_state_lookup(&id->daddr, id->spi, id->proto, id->family);
1428 if (x == NULL) {
Patrick McHardyb08d5842007-02-27 09:57:37 -08001429 kfree_skb(r_skb);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001430 return -ESRCH;
1431 }
1432
1433 /*
1434 * XXX: is this lock really needed - none of the other
1435 * gets lock (the concern is things getting updated
1436 * while we are still reading) - jhs
1437 */
1438 spin_lock_bh(&x->lock);
1439 c.data.aevent = p->flags;
1440 c.seq = nlh->nlmsg_seq;
1441 c.pid = nlh->nlmsg_pid;
1442
1443 if (build_aevent(r_skb, x, &c) < 0)
1444 BUG();
Thomas Graf082a1ad2007-08-22 13:54:36 -07001445 err = nlmsg_unicast(xfrm_nl, r_skb, NETLINK_CB(skb).pid);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001446 spin_unlock_bh(&x->lock);
1447 xfrm_state_put(x);
1448 return err;
1449}
1450
Christoph Hellwig22e70052007-01-02 15:22:30 -08001451static int xfrm_new_ae(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf35a7aa02007-08-22 14:00:40 -07001452 struct rtattr **attrs)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001453{
1454 struct xfrm_state *x;
1455 struct km_event c;
1456 int err = - EINVAL;
Thomas Graf7b67c852007-08-22 13:53:52 -07001457 struct xfrm_aevent_id *p = nlmsg_data(nlh);
Thomas Graf35a7aa02007-08-22 14:00:40 -07001458 struct rtattr *rp = attrs[XFRMA_REPLAY_VAL];
1459 struct rtattr *lt = attrs[XFRMA_LTIME_VAL];
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001460
1461 if (!lt && !rp)
1462 return err;
1463
1464 /* pedantic mode - thou shalt sayeth replaceth */
1465 if (!(nlh->nlmsg_flags&NLM_F_REPLACE))
1466 return err;
1467
1468 x = xfrm_state_lookup(&p->sa_id.daddr, p->sa_id.spi, p->sa_id.proto, p->sa_id.family);
1469 if (x == NULL)
1470 return -ESRCH;
1471
1472 if (x->km.state != XFRM_STATE_VALID)
1473 goto out;
1474
1475 spin_lock_bh(&x->lock);
Thomas Graf35a7aa02007-08-22 14:00:40 -07001476 xfrm_update_ae_params(x, attrs);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001477 spin_unlock_bh(&x->lock);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001478
1479 c.event = nlh->nlmsg_type;
1480 c.seq = nlh->nlmsg_seq;
1481 c.pid = nlh->nlmsg_pid;
1482 c.data.aevent = XFRM_AE_CU;
1483 km_state_notify(x, &c);
1484 err = 0;
1485out:
1486 xfrm_state_put(x);
1487 return err;
1488}
1489
Christoph Hellwig22e70052007-01-02 15:22:30 -08001490static int xfrm_flush_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf35a7aa02007-08-22 14:00:40 -07001491 struct rtattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492{
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001493 struct km_event c;
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001494 u8 type = XFRM_POLICY_TYPE_MAIN;
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001495 int err;
Joy Latten161a09e2006-11-27 13:11:54 -06001496 struct xfrm_audit audit_info;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001497
Thomas Graf35a7aa02007-08-22 14:00:40 -07001498 err = copy_from_user_policy_type(&type, attrs);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001499 if (err)
1500 return err;
1501
Joy Latten161a09e2006-11-27 13:11:54 -06001502 audit_info.loginuid = NETLINK_CB(skb).loginuid;
1503 audit_info.secid = NETLINK_CB(skb).sid;
Joy Latten4aa2e622007-06-04 19:05:57 -04001504 err = xfrm_policy_flush(type, &audit_info);
1505 if (err)
1506 return err;
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001507 c.data.type = type;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001508 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001509 c.seq = nlh->nlmsg_seq;
1510 c.pid = nlh->nlmsg_pid;
1511 km_policy_notify(NULL, 0, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512 return 0;
1513}
1514
Christoph Hellwig22e70052007-01-02 15:22:30 -08001515static int xfrm_add_pol_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf35a7aa02007-08-22 14:00:40 -07001516 struct rtattr **attrs)
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001517{
1518 struct xfrm_policy *xp;
Thomas Graf7b67c852007-08-22 13:53:52 -07001519 struct xfrm_user_polexpire *up = nlmsg_data(nlh);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001520 struct xfrm_userpolicy_info *p = &up->pol;
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001521 u8 type = XFRM_POLICY_TYPE_MAIN;
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001522 int err = -ENOENT;
1523
Thomas Graf35a7aa02007-08-22 14:00:40 -07001524 err = copy_from_user_policy_type(&type, attrs);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001525 if (err)
1526 return err;
1527
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001528 if (p->index)
Eric Parisef41aaa2007-03-07 15:37:58 -08001529 xp = xfrm_policy_byid(type, p->dir, p->index, 0, &err);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001530 else {
Thomas Graf35a7aa02007-08-22 14:00:40 -07001531 struct rtattr *rt = attrs[XFRMA_SEC_CTX];
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001532 struct xfrm_policy tmp;
1533
Thomas Graf35a7aa02007-08-22 14:00:40 -07001534 err = verify_sec_ctx_len(attrs);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001535 if (err)
1536 return err;
1537
1538 memset(&tmp, 0, sizeof(struct xfrm_policy));
1539 if (rt) {
1540 struct xfrm_user_sec_ctx *uctx = RTA_DATA(rt);
1541
1542 if ((err = security_xfrm_policy_alloc(&tmp, uctx)))
1543 return err;
1544 }
Eric Parisef41aaa2007-03-07 15:37:58 -08001545 xp = xfrm_policy_bysel_ctx(type, p->dir, &p->sel, tmp.security,
1546 0, &err);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001547 security_xfrm_policy_free(&tmp);
1548 }
1549
1550 if (xp == NULL)
Eric Parisef41aaa2007-03-07 15:37:58 -08001551 return -ENOENT;
1552 read_lock(&xp->lock);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001553 if (xp->dead) {
1554 read_unlock(&xp->lock);
1555 goto out;
1556 }
1557
1558 read_unlock(&xp->lock);
1559 err = 0;
1560 if (up->hard) {
1561 xfrm_policy_delete(xp, p->dir);
Joy Latten161a09e2006-11-27 13:11:54 -06001562 xfrm_audit_log(NETLINK_CB(skb).loginuid, NETLINK_CB(skb).sid,
1563 AUDIT_MAC_IPSEC_DELSPD, 1, xp, NULL);
1564
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001565 } else {
1566 // reset the timers here?
1567 printk("Dont know what to do with soft policy expire\n");
1568 }
1569 km_policy_expired(xp, p->dir, up->hard, current->pid);
1570
1571out:
1572 xfrm_pol_put(xp);
1573 return err;
1574}
1575
Christoph Hellwig22e70052007-01-02 15:22:30 -08001576static int xfrm_add_sa_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf35a7aa02007-08-22 14:00:40 -07001577 struct rtattr **attrs)
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001578{
1579 struct xfrm_state *x;
1580 int err;
Thomas Graf7b67c852007-08-22 13:53:52 -07001581 struct xfrm_user_expire *ue = nlmsg_data(nlh);
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001582 struct xfrm_usersa_info *p = &ue->state;
1583
1584 x = xfrm_state_lookup(&p->id.daddr, p->id.spi, p->id.proto, p->family);
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001585
David S. Miller3a765aa2007-02-26 14:52:21 -08001586 err = -ENOENT;
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001587 if (x == NULL)
1588 return err;
1589
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001590 spin_lock_bh(&x->lock);
David S. Miller3a765aa2007-02-26 14:52:21 -08001591 err = -EINVAL;
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001592 if (x->km.state != XFRM_STATE_VALID)
1593 goto out;
1594 km_state_expired(x, ue->hard, current->pid);
1595
Joy Latten161a09e2006-11-27 13:11:54 -06001596 if (ue->hard) {
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001597 __xfrm_state_delete(x);
Joy Latten161a09e2006-11-27 13:11:54 -06001598 xfrm_audit_log(NETLINK_CB(skb).loginuid, NETLINK_CB(skb).sid,
1599 AUDIT_MAC_IPSEC_DELSA, 1, NULL, x);
1600 }
David S. Miller3a765aa2007-02-26 14:52:21 -08001601 err = 0;
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001602out:
1603 spin_unlock_bh(&x->lock);
1604 xfrm_state_put(x);
1605 return err;
1606}
1607
Christoph Hellwig22e70052007-01-02 15:22:30 -08001608static int xfrm_add_acquire(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf35a7aa02007-08-22 14:00:40 -07001609 struct rtattr **attrs)
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001610{
1611 struct xfrm_policy *xp;
1612 struct xfrm_user_tmpl *ut;
1613 int i;
Thomas Graf35a7aa02007-08-22 14:00:40 -07001614 struct rtattr *rt = attrs[XFRMA_TMPL];
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001615
Thomas Graf7b67c852007-08-22 13:53:52 -07001616 struct xfrm_user_acquire *ua = nlmsg_data(nlh);
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001617 struct xfrm_state *x = xfrm_state_alloc();
1618 int err = -ENOMEM;
1619
1620 if (!x)
1621 return err;
1622
1623 err = verify_newpolicy_info(&ua->policy);
1624 if (err) {
1625 printk("BAD policy passed\n");
1626 kfree(x);
1627 return err;
1628 }
1629
1630 /* build an XP */
Thomas Graf35a7aa02007-08-22 14:00:40 -07001631 xp = xfrm_policy_construct(&ua->policy, (struct rtattr **) attrs, &err);
David S. Millerb4ad86bf2006-12-03 19:19:26 -08001632 if (!xp) {
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001633 kfree(x);
1634 return err;
1635 }
1636
1637 memcpy(&x->id, &ua->id, sizeof(ua->id));
1638 memcpy(&x->props.saddr, &ua->saddr, sizeof(ua->saddr));
1639 memcpy(&x->sel, &ua->sel, sizeof(ua->sel));
1640
1641 ut = RTA_DATA(rt);
1642 /* extract the templates and for each call km_key */
1643 for (i = 0; i < xp->xfrm_nr; i++, ut++) {
1644 struct xfrm_tmpl *t = &xp->xfrm_vec[i];
1645 memcpy(&x->id, &t->id, sizeof(x->id));
1646 x->props.mode = t->mode;
1647 x->props.reqid = t->reqid;
1648 x->props.family = ut->family;
1649 t->aalgos = ua->aalgos;
1650 t->ealgos = ua->ealgos;
1651 t->calgos = ua->calgos;
1652 err = km_query(x, t, xp);
1653
1654 }
1655
1656 kfree(x);
1657 kfree(xp);
1658
1659 return 0;
1660}
1661
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001662#ifdef CONFIG_XFRM_MIGRATE
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001663static int copy_from_user_migrate(struct xfrm_migrate *ma,
Thomas Graf35a7aa02007-08-22 14:00:40 -07001664 struct rtattr **attrs, int *num)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001665{
Thomas Graf35a7aa02007-08-22 14:00:40 -07001666 struct rtattr *rt = attrs[XFRMA_MIGRATE];
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001667 struct xfrm_user_migrate *um;
1668 int i, num_migrate;
1669
1670 um = RTA_DATA(rt);
1671 num_migrate = (rt->rta_len - sizeof(*rt)) / sizeof(*um);
1672
1673 if (num_migrate <= 0 || num_migrate > XFRM_MAX_DEPTH)
1674 return -EINVAL;
1675
1676 for (i = 0; i < num_migrate; i++, um++, ma++) {
1677 memcpy(&ma->old_daddr, &um->old_daddr, sizeof(ma->old_daddr));
1678 memcpy(&ma->old_saddr, &um->old_saddr, sizeof(ma->old_saddr));
1679 memcpy(&ma->new_daddr, &um->new_daddr, sizeof(ma->new_daddr));
1680 memcpy(&ma->new_saddr, &um->new_saddr, sizeof(ma->new_saddr));
1681
1682 ma->proto = um->proto;
1683 ma->mode = um->mode;
1684 ma->reqid = um->reqid;
1685
1686 ma->old_family = um->old_family;
1687 ma->new_family = um->new_family;
1688 }
1689
1690 *num = i;
1691 return 0;
1692}
1693
1694static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf35a7aa02007-08-22 14:00:40 -07001695 struct rtattr **attrs)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001696{
Thomas Graf7b67c852007-08-22 13:53:52 -07001697 struct xfrm_userpolicy_id *pi = nlmsg_data(nlh);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001698 struct xfrm_migrate m[XFRM_MAX_DEPTH];
1699 u8 type;
1700 int err;
1701 int n = 0;
1702
Thomas Graf35a7aa02007-08-22 14:00:40 -07001703 if (attrs[XFRMA_MIGRATE] == NULL)
Thomas Grafcf5cb792007-08-22 13:59:04 -07001704 return -EINVAL;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001705
Thomas Graf35a7aa02007-08-22 14:00:40 -07001706 err = copy_from_user_policy_type(&type, (struct rtattr **)attrs);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001707 if (err)
1708 return err;
1709
1710 err = copy_from_user_migrate((struct xfrm_migrate *)m,
Thomas Graf35a7aa02007-08-22 14:00:40 -07001711 (struct rtattr **)attrs, &n);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001712 if (err)
1713 return err;
1714
1715 if (!n)
1716 return 0;
1717
1718 xfrm_migrate(&pi->sel, pi->dir, type, m, n);
1719
1720 return 0;
1721}
1722#else
1723static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf35a7aa02007-08-22 14:00:40 -07001724 struct rtattr **attrs)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001725{
1726 return -ENOPROTOOPT;
1727}
1728#endif
1729
1730#ifdef CONFIG_XFRM_MIGRATE
1731static int copy_to_user_migrate(struct xfrm_migrate *m, struct sk_buff *skb)
1732{
1733 struct xfrm_user_migrate um;
1734
1735 memset(&um, 0, sizeof(um));
1736 um.proto = m->proto;
1737 um.mode = m->mode;
1738 um.reqid = m->reqid;
1739 um.old_family = m->old_family;
1740 memcpy(&um.old_daddr, &m->old_daddr, sizeof(um.old_daddr));
1741 memcpy(&um.old_saddr, &m->old_saddr, sizeof(um.old_saddr));
1742 um.new_family = m->new_family;
1743 memcpy(&um.new_daddr, &m->new_daddr, sizeof(um.new_daddr));
1744 memcpy(&um.new_saddr, &m->new_saddr, sizeof(um.new_saddr));
1745
Thomas Grafc0144be2007-08-22 13:55:43 -07001746 return nla_put(skb, XFRMA_MIGRATE, sizeof(um), &um);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001747}
1748
Thomas Graf7deb2262007-08-22 13:57:39 -07001749static inline size_t xfrm_migrate_msgsize(int num_migrate)
1750{
1751 return NLMSG_ALIGN(sizeof(struct xfrm_userpolicy_id))
1752 + nla_total_size(sizeof(struct xfrm_user_migrate) * num_migrate)
1753 + userpolicy_type_attrsize();
1754}
1755
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001756static int build_migrate(struct sk_buff *skb, struct xfrm_migrate *m,
1757 int num_migrate, struct xfrm_selector *sel,
1758 u8 dir, u8 type)
1759{
1760 struct xfrm_migrate *mp;
1761 struct xfrm_userpolicy_id *pol_id;
1762 struct nlmsghdr *nlh;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001763 int i;
1764
Thomas Graf79b8b7f2007-08-22 12:46:53 -07001765 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_MIGRATE, sizeof(*pol_id), 0);
1766 if (nlh == NULL)
1767 return -EMSGSIZE;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001768
Thomas Graf7b67c852007-08-22 13:53:52 -07001769 pol_id = nlmsg_data(nlh);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001770 /* copy data from selector, dir, and type to the pol_id */
1771 memset(pol_id, 0, sizeof(*pol_id));
1772 memcpy(&pol_id->sel, sel, sizeof(pol_id->sel));
1773 pol_id->dir = dir;
1774
1775 if (copy_to_user_policy_type(type, skb) < 0)
1776 goto nlmsg_failure;
1777
1778 for (i = 0, mp = m ; i < num_migrate; i++, mp++) {
1779 if (copy_to_user_migrate(mp, skb) < 0)
1780 goto nlmsg_failure;
1781 }
1782
Thomas Graf98250692007-08-22 12:47:26 -07001783 return nlmsg_end(skb, nlh);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001784nlmsg_failure:
Thomas Graf98250692007-08-22 12:47:26 -07001785 nlmsg_cancel(skb, nlh);
1786 return -EMSGSIZE;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001787}
1788
1789static int xfrm_send_migrate(struct xfrm_selector *sel, u8 dir, u8 type,
1790 struct xfrm_migrate *m, int num_migrate)
1791{
1792 struct sk_buff *skb;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001793
Thomas Graf7deb2262007-08-22 13:57:39 -07001794 skb = nlmsg_new(xfrm_migrate_msgsize(num_migrate), GFP_ATOMIC);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001795 if (skb == NULL)
1796 return -ENOMEM;
1797
1798 /* build migrate */
1799 if (build_migrate(skb, m, num_migrate, sel, dir, type) < 0)
1800 BUG();
1801
Thomas Graf082a1ad2007-08-22 13:54:36 -07001802 return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_MIGRATE, GFP_ATOMIC);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001803}
1804#else
1805static int xfrm_send_migrate(struct xfrm_selector *sel, u8 dir, u8 type,
1806 struct xfrm_migrate *m, int num_migrate)
1807{
1808 return -ENOPROTOOPT;
1809}
1810#endif
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001811
Thomas Grafa7bd9a42007-08-22 13:58:18 -07001812#define XMSGSIZE(type) sizeof(struct type)
Thomas Graf492b5582005-05-03 14:26:40 -07001813
1814static const int xfrm_msg_min[XFRM_NR_MSGTYPES] = {
1815 [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
1816 [XFRM_MSG_DELSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
1817 [XFRM_MSG_GETSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
1818 [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
1819 [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
1820 [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
1821 [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userspi_info),
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001822 [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_acquire),
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001823 [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_expire),
Thomas Graf492b5582005-05-03 14:26:40 -07001824 [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
1825 [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001826 [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_polexpire),
Thomas Graf492b5582005-05-03 14:26:40 -07001827 [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_flush),
Thomas Grafa7bd9a42007-08-22 13:58:18 -07001828 [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = 0,
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001829 [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
1830 [XFRM_MSG_GETAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07001831 [XFRM_MSG_REPORT - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_report),
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001832 [XFRM_MSG_MIGRATE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
Thomas Grafa7bd9a42007-08-22 13:58:18 -07001833 [XFRM_MSG_GETSADINFO - XFRM_MSG_BASE] = sizeof(u32),
1834 [XFRM_MSG_GETSPDINFO - XFRM_MSG_BASE] = sizeof(u32),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835};
1836
Thomas Graf492b5582005-05-03 14:26:40 -07001837#undef XMSGSIZE
1838
Thomas Grafcf5cb792007-08-22 13:59:04 -07001839static const struct nla_policy xfrma_policy[XFRMA_MAX+1] = {
1840 [XFRMA_ALG_AUTH] = { .len = sizeof(struct xfrm_algo) },
1841 [XFRMA_ALG_CRYPT] = { .len = sizeof(struct xfrm_algo) },
1842 [XFRMA_ALG_COMP] = { .len = sizeof(struct xfrm_algo) },
1843 [XFRMA_ENCAP] = { .len = sizeof(struct xfrm_encap_tmpl) },
1844 [XFRMA_TMPL] = { .len = sizeof(struct xfrm_user_tmpl) },
1845 [XFRMA_SEC_CTX] = { .len = sizeof(struct xfrm_sec_ctx) },
1846 [XFRMA_LTIME_VAL] = { .len = sizeof(struct xfrm_lifetime_cur) },
1847 [XFRMA_REPLAY_VAL] = { .len = sizeof(struct xfrm_replay_state) },
1848 [XFRMA_REPLAY_THRESH] = { .type = NLA_U32 },
1849 [XFRMA_ETIMER_THRESH] = { .type = NLA_U32 },
1850 [XFRMA_SRCADDR] = { .len = sizeof(xfrm_address_t) },
1851 [XFRMA_COADDR] = { .len = sizeof(xfrm_address_t) },
1852 [XFRMA_POLICY_TYPE] = { .len = sizeof(struct xfrm_userpolicy_type)},
1853 [XFRMA_MIGRATE] = { .len = sizeof(struct xfrm_user_migrate) },
1854};
1855
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856static struct xfrm_link {
Christoph Hellwig22e70052007-01-02 15:22:30 -08001857 int (*doit)(struct sk_buff *, struct nlmsghdr *, struct rtattr **);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858 int (*dump)(struct sk_buff *, struct netlink_callback *);
Thomas Graf492b5582005-05-03 14:26:40 -07001859} xfrm_dispatch[XFRM_NR_MSGTYPES] = {
1860 [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa },
1861 [XFRM_MSG_DELSA - XFRM_MSG_BASE] = { .doit = xfrm_del_sa },
1862 [XFRM_MSG_GETSA - XFRM_MSG_BASE] = { .doit = xfrm_get_sa,
1863 .dump = xfrm_dump_sa },
1864 [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy },
1865 [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy },
1866 [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy,
1867 .dump = xfrm_dump_policy },
1868 [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = { .doit = xfrm_alloc_userspi },
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001869 [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_acquire },
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001870 [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_sa_expire },
Thomas Graf492b5582005-05-03 14:26:40 -07001871 [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy },
1872 [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa },
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001873 [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_pol_expire},
Thomas Graf492b5582005-05-03 14:26:40 -07001874 [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = { .doit = xfrm_flush_sa },
1875 [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_flush_policy },
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001876 [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = { .doit = xfrm_new_ae },
1877 [XFRM_MSG_GETAE - XFRM_MSG_BASE] = { .doit = xfrm_get_ae },
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001878 [XFRM_MSG_MIGRATE - XFRM_MSG_BASE] = { .doit = xfrm_do_migrate },
Jamal Hadi Salim566ec032007-04-26 14:12:15 -07001879 [XFRM_MSG_GETSADINFO - XFRM_MSG_BASE] = { .doit = xfrm_get_sadinfo },
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -07001880 [XFRM_MSG_GETSPDINFO - XFRM_MSG_BASE] = { .doit = xfrm_get_spdinfo },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881};
1882
Thomas Graf1d00a4e2007-03-22 23:30:12 -07001883static int xfrm_user_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884{
Thomas Graf35a7aa02007-08-22 14:00:40 -07001885 struct nlattr *attrs[XFRMA_MAX+1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886 struct xfrm_link *link;
Thomas Grafa7bd9a42007-08-22 13:58:18 -07001887 int type, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889 type = nlh->nlmsg_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890 if (type > XFRM_MSG_MAX)
Thomas Graf1d00a4e2007-03-22 23:30:12 -07001891 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892
1893 type -= XFRM_MSG_BASE;
1894 link = &xfrm_dispatch[type];
1895
1896 /* All operations require privileges, even GET */
Thomas Graf1d00a4e2007-03-22 23:30:12 -07001897 if (security_netlink_recv(skb, CAP_NET_ADMIN))
1898 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899
Thomas Graf492b5582005-05-03 14:26:40 -07001900 if ((type == (XFRM_MSG_GETSA - XFRM_MSG_BASE) ||
1901 type == (XFRM_MSG_GETPOLICY - XFRM_MSG_BASE)) &&
1902 (nlh->nlmsg_flags & NLM_F_DUMP)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903 if (link->dump == NULL)
Thomas Graf1d00a4e2007-03-22 23:30:12 -07001904 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905
Thomas Grafc702e802007-03-22 23:30:55 -07001906 return netlink_dump_start(xfrm_nl, skb, nlh, link->dump, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907 }
1908
Thomas Graf35a7aa02007-08-22 14:00:40 -07001909 err = nlmsg_parse(nlh, xfrm_msg_min[type], attrs, XFRMA_MAX,
Thomas Grafcf5cb792007-08-22 13:59:04 -07001910 xfrma_policy);
Thomas Grafa7bd9a42007-08-22 13:58:18 -07001911 if (err < 0)
1912 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913
1914 if (link->doit == NULL)
Thomas Graf1d00a4e2007-03-22 23:30:12 -07001915 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916
Thomas Graf35a7aa02007-08-22 14:00:40 -07001917 return link->doit(skb, nlh, (struct rtattr **) attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918}
1919
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920static void xfrm_netlink_rcv(struct sock *sk, int len)
1921{
Thomas Graf88fc2c82005-11-10 02:25:54 +01001922 unsigned int qlen = 0;
Herbert Xu2a0a6eb2005-05-03 14:55:09 -07001923
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924 do {
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -08001925 mutex_lock(&xfrm_cfg_mutex);
Thomas Graf88fc2c82005-11-10 02:25:54 +01001926 netlink_run_queue(sk, &qlen, &xfrm_user_rcv_msg);
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -08001927 mutex_unlock(&xfrm_cfg_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928
Herbert Xu2a0a6eb2005-05-03 14:55:09 -07001929 } while (qlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930}
1931
Thomas Graf7deb2262007-08-22 13:57:39 -07001932static inline size_t xfrm_expire_msgsize(void)
1933{
1934 return NLMSG_ALIGN(sizeof(struct xfrm_user_expire));
1935}
1936
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001937static int build_expire(struct sk_buff *skb, struct xfrm_state *x, struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938{
1939 struct xfrm_user_expire *ue;
1940 struct nlmsghdr *nlh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941
Thomas Graf79b8b7f2007-08-22 12:46:53 -07001942 nlh = nlmsg_put(skb, c->pid, 0, XFRM_MSG_EXPIRE, sizeof(*ue), 0);
1943 if (nlh == NULL)
1944 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945
Thomas Graf7b67c852007-08-22 13:53:52 -07001946 ue = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947 copy_to_user_state(x, &ue->state);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001948 ue->hard = (c->data.hard != 0) ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949
Thomas Graf98250692007-08-22 12:47:26 -07001950 return nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951}
1952
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001953static int xfrm_exp_state_notify(struct xfrm_state *x, struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954{
1955 struct sk_buff *skb;
1956
Thomas Graf7deb2262007-08-22 13:57:39 -07001957 skb = nlmsg_new(xfrm_expire_msgsize(), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958 if (skb == NULL)
1959 return -ENOMEM;
1960
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001961 if (build_expire(skb, x, c) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962 BUG();
1963
Thomas Graf082a1ad2007-08-22 13:54:36 -07001964 return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_EXPIRE, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965}
1966
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001967static int xfrm_aevent_state_notify(struct xfrm_state *x, struct km_event *c)
1968{
1969 struct sk_buff *skb;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001970
Thomas Graf7deb2262007-08-22 13:57:39 -07001971 skb = nlmsg_new(xfrm_aevent_msgsize(), GFP_ATOMIC);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001972 if (skb == NULL)
1973 return -ENOMEM;
1974
1975 if (build_aevent(skb, x, c) < 0)
1976 BUG();
1977
Thomas Graf082a1ad2007-08-22 13:54:36 -07001978 return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_AEVENTS, GFP_ATOMIC);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001979}
1980
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001981static int xfrm_notify_sa_flush(struct km_event *c)
1982{
1983 struct xfrm_usersa_flush *p;
1984 struct nlmsghdr *nlh;
1985 struct sk_buff *skb;
Thomas Graf7deb2262007-08-22 13:57:39 -07001986 int len = NLMSG_ALIGN(sizeof(struct xfrm_usersa_flush));
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001987
Thomas Graf7deb2262007-08-22 13:57:39 -07001988 skb = nlmsg_new(len, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001989 if (skb == NULL)
1990 return -ENOMEM;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001991
Thomas Graf79b8b7f2007-08-22 12:46:53 -07001992 nlh = nlmsg_put(skb, c->pid, c->seq, XFRM_MSG_FLUSHSA, sizeof(*p), 0);
1993 if (nlh == NULL) {
1994 kfree_skb(skb);
1995 return -EMSGSIZE;
1996 }
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001997
Thomas Graf7b67c852007-08-22 13:53:52 -07001998 p = nlmsg_data(nlh);
Herbert Xubf088672005-06-18 22:44:00 -07001999 p->proto = c->data.proto;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002000
Thomas Graf98250692007-08-22 12:47:26 -07002001 nlmsg_end(skb, nlh);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002002
Thomas Graf082a1ad2007-08-22 13:54:36 -07002003 return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_SA, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002004}
2005
Thomas Graf7deb2262007-08-22 13:57:39 -07002006static inline size_t xfrm_sa_len(struct xfrm_state *x)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002007{
Thomas Graf7deb2262007-08-22 13:57:39 -07002008 size_t l = 0;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002009 if (x->aalg)
Thomas Graf7deb2262007-08-22 13:57:39 -07002010 l += nla_total_size(alg_len(x->aalg));
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002011 if (x->ealg)
Thomas Graf7deb2262007-08-22 13:57:39 -07002012 l += nla_total_size(alg_len(x->ealg));
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002013 if (x->calg)
Thomas Graf7deb2262007-08-22 13:57:39 -07002014 l += nla_total_size(sizeof(*x->calg));
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002015 if (x->encap)
Thomas Graf7deb2262007-08-22 13:57:39 -07002016 l += nla_total_size(sizeof(*x->encap));
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002017
2018 return l;
2019}
2020
2021static int xfrm_notify_sa(struct xfrm_state *x, struct km_event *c)
2022{
2023 struct xfrm_usersa_info *p;
Herbert Xu0603eac2005-06-18 22:54:36 -07002024 struct xfrm_usersa_id *id;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002025 struct nlmsghdr *nlh;
2026 struct sk_buff *skb;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002027 int len = xfrm_sa_len(x);
Herbert Xu0603eac2005-06-18 22:54:36 -07002028 int headlen;
2029
2030 headlen = sizeof(*p);
2031 if (c->event == XFRM_MSG_DELSA) {
Thomas Graf7deb2262007-08-22 13:57:39 -07002032 len += nla_total_size(headlen);
Herbert Xu0603eac2005-06-18 22:54:36 -07002033 headlen = sizeof(*id);
2034 }
Thomas Graf7deb2262007-08-22 13:57:39 -07002035 len += NLMSG_ALIGN(headlen);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002036
Thomas Graf7deb2262007-08-22 13:57:39 -07002037 skb = nlmsg_new(len, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002038 if (skb == NULL)
2039 return -ENOMEM;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002040
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002041 nlh = nlmsg_put(skb, c->pid, c->seq, c->event, headlen, 0);
2042 if (nlh == NULL)
Thomas Grafc0144be2007-08-22 13:55:43 -07002043 goto nla_put_failure;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002044
Thomas Graf7b67c852007-08-22 13:53:52 -07002045 p = nlmsg_data(nlh);
Herbert Xu0603eac2005-06-18 22:54:36 -07002046 if (c->event == XFRM_MSG_DELSA) {
Thomas Grafc0144be2007-08-22 13:55:43 -07002047 struct nlattr *attr;
2048
Thomas Graf7b67c852007-08-22 13:53:52 -07002049 id = nlmsg_data(nlh);
Herbert Xu0603eac2005-06-18 22:54:36 -07002050 memcpy(&id->daddr, &x->id.daddr, sizeof(id->daddr));
2051 id->spi = x->id.spi;
2052 id->family = x->props.family;
2053 id->proto = x->id.proto;
2054
Thomas Grafc0144be2007-08-22 13:55:43 -07002055 attr = nla_reserve(skb, XFRMA_SA, sizeof(*p));
2056 if (attr == NULL)
2057 goto nla_put_failure;
2058
2059 p = nla_data(attr);
Herbert Xu0603eac2005-06-18 22:54:36 -07002060 }
2061
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002062 copy_to_user_state(x, p);
2063
2064 if (x->aalg)
Thomas Grafc26445a2007-08-22 13:56:23 -07002065 NLA_PUT(skb, XFRMA_ALG_AUTH, alg_len(x->aalg), x->aalg);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002066 if (x->ealg)
Thomas Grafc26445a2007-08-22 13:56:23 -07002067 NLA_PUT(skb, XFRMA_ALG_CRYPT, alg_len(x->ealg), x->ealg);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002068 if (x->calg)
Thomas Grafc0144be2007-08-22 13:55:43 -07002069 NLA_PUT(skb, XFRMA_ALG_COMP, sizeof(*(x->calg)), x->calg);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002070
2071 if (x->encap)
Thomas Grafc0144be2007-08-22 13:55:43 -07002072 NLA_PUT(skb, XFRMA_ENCAP, sizeof(*x->encap), x->encap);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002073
Thomas Graf98250692007-08-22 12:47:26 -07002074 nlmsg_end(skb, nlh);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002075
Thomas Graf082a1ad2007-08-22 13:54:36 -07002076 return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_SA, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002077
Thomas Grafc0144be2007-08-22 13:55:43 -07002078nla_put_failure:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002079 kfree_skb(skb);
2080 return -1;
2081}
2082
2083static int xfrm_send_state_notify(struct xfrm_state *x, struct km_event *c)
2084{
2085
2086 switch (c->event) {
Herbert Xuf60f6b82005-06-18 22:44:37 -07002087 case XFRM_MSG_EXPIRE:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002088 return xfrm_exp_state_notify(x, c);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002089 case XFRM_MSG_NEWAE:
2090 return xfrm_aevent_state_notify(x, c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07002091 case XFRM_MSG_DELSA:
2092 case XFRM_MSG_UPDSA:
2093 case XFRM_MSG_NEWSA:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002094 return xfrm_notify_sa(x, c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07002095 case XFRM_MSG_FLUSHSA:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002096 return xfrm_notify_sa_flush(c);
2097 default:
2098 printk("xfrm_user: Unknown SA event %d\n", c->event);
2099 break;
2100 }
2101
2102 return 0;
2103
2104}
2105
Thomas Graf7deb2262007-08-22 13:57:39 -07002106static inline size_t xfrm_acquire_msgsize(struct xfrm_state *x,
2107 struct xfrm_policy *xp)
2108{
2109 return NLMSG_ALIGN(sizeof(struct xfrm_user_acquire))
2110 + nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr)
2111 + nla_total_size(xfrm_user_sec_ctx_size(x->security))
2112 + userpolicy_type_attrsize();
2113}
2114
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115static int build_acquire(struct sk_buff *skb, struct xfrm_state *x,
2116 struct xfrm_tmpl *xt, struct xfrm_policy *xp,
2117 int dir)
2118{
2119 struct xfrm_user_acquire *ua;
2120 struct nlmsghdr *nlh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121 __u32 seq = xfrm_get_acqseq();
2122
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002123 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_ACQUIRE, sizeof(*ua), 0);
2124 if (nlh == NULL)
2125 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126
Thomas Graf7b67c852007-08-22 13:53:52 -07002127 ua = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128 memcpy(&ua->id, &x->id, sizeof(ua->id));
2129 memcpy(&ua->saddr, &x->props.saddr, sizeof(ua->saddr));
2130 memcpy(&ua->sel, &x->sel, sizeof(ua->sel));
2131 copy_to_user_policy(xp, &ua->policy, dir);
2132 ua->aalgos = xt->aalgos;
2133 ua->ealgos = xt->ealgos;
2134 ua->calgos = xt->calgos;
2135 ua->seq = x->km.seq = seq;
2136
2137 if (copy_to_user_tmpl(xp, skb) < 0)
2138 goto nlmsg_failure;
Serge Hallyn0d681622006-07-24 23:30:44 -07002139 if (copy_to_user_state_sec_ctx(x, skb))
Trent Jaegerdf718372005-12-13 23:12:27 -08002140 goto nlmsg_failure;
Jamal Hadi Salim1459bb32006-11-20 16:51:22 -08002141 if (copy_to_user_policy_type(xp->type, skb) < 0)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07002142 goto nlmsg_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143
Thomas Graf98250692007-08-22 12:47:26 -07002144 return nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145
2146nlmsg_failure:
Thomas Graf98250692007-08-22 12:47:26 -07002147 nlmsg_cancel(skb, nlh);
2148 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149}
2150
2151static int xfrm_send_acquire(struct xfrm_state *x, struct xfrm_tmpl *xt,
2152 struct xfrm_policy *xp, int dir)
2153{
2154 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155
Thomas Graf7deb2262007-08-22 13:57:39 -07002156 skb = nlmsg_new(xfrm_acquire_msgsize(x, xp), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157 if (skb == NULL)
2158 return -ENOMEM;
2159
2160 if (build_acquire(skb, x, xt, xp, dir) < 0)
2161 BUG();
2162
Thomas Graf082a1ad2007-08-22 13:54:36 -07002163 return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_ACQUIRE, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164}
2165
2166/* User gives us xfrm_user_policy_info followed by an array of 0
2167 * or more templates.
2168 */
Venkat Yekkiralacb969f02006-07-24 23:32:20 -07002169static struct xfrm_policy *xfrm_compile_policy(struct sock *sk, int opt,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170 u8 *data, int len, int *dir)
2171{
2172 struct xfrm_userpolicy_info *p = (struct xfrm_userpolicy_info *)data;
2173 struct xfrm_user_tmpl *ut = (struct xfrm_user_tmpl *) (p + 1);
2174 struct xfrm_policy *xp;
2175 int nr;
2176
Venkat Yekkiralacb969f02006-07-24 23:32:20 -07002177 switch (sk->sk_family) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178 case AF_INET:
2179 if (opt != IP_XFRM_POLICY) {
2180 *dir = -EOPNOTSUPP;
2181 return NULL;
2182 }
2183 break;
2184#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
2185 case AF_INET6:
2186 if (opt != IPV6_XFRM_POLICY) {
2187 *dir = -EOPNOTSUPP;
2188 return NULL;
2189 }
2190 break;
2191#endif
2192 default:
2193 *dir = -EINVAL;
2194 return NULL;
2195 }
2196
2197 *dir = -EINVAL;
2198
2199 if (len < sizeof(*p) ||
2200 verify_newpolicy_info(p))
2201 return NULL;
2202
2203 nr = ((len - sizeof(*p)) / sizeof(*ut));
David S. Millerb4ad86bf2006-12-03 19:19:26 -08002204 if (validate_tmpl(nr, ut, p->sel.family))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205 return NULL;
2206
Herbert Xua4f1bac2005-07-26 15:43:17 -07002207 if (p->dir > XFRM_POLICY_OUT)
2208 return NULL;
2209
Linus Torvalds1da177e2005-04-16 15:20:36 -07002210 xp = xfrm_policy_alloc(GFP_KERNEL);
2211 if (xp == NULL) {
2212 *dir = -ENOBUFS;
2213 return NULL;
2214 }
2215
2216 copy_from_user_policy(xp, p);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07002217 xp->type = XFRM_POLICY_TYPE_MAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218 copy_templates(xp, ut, nr);
2219
2220 *dir = p->dir;
2221
2222 return xp;
2223}
2224
Thomas Graf7deb2262007-08-22 13:57:39 -07002225static inline size_t xfrm_polexpire_msgsize(struct xfrm_policy *xp)
2226{
2227 return NLMSG_ALIGN(sizeof(struct xfrm_user_polexpire))
2228 + nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr)
2229 + nla_total_size(xfrm_user_sec_ctx_size(xp->security))
2230 + userpolicy_type_attrsize();
2231}
2232
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233static int build_polexpire(struct sk_buff *skb, struct xfrm_policy *xp,
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002234 int dir, struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235{
2236 struct xfrm_user_polexpire *upe;
2237 struct nlmsghdr *nlh;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002238 int hard = c->data.hard;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002239
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002240 nlh = nlmsg_put(skb, c->pid, 0, XFRM_MSG_POLEXPIRE, sizeof(*upe), 0);
2241 if (nlh == NULL)
2242 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243
Thomas Graf7b67c852007-08-22 13:53:52 -07002244 upe = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002245 copy_to_user_policy(xp, &upe->pol, dir);
2246 if (copy_to_user_tmpl(xp, skb) < 0)
2247 goto nlmsg_failure;
Trent Jaegerdf718372005-12-13 23:12:27 -08002248 if (copy_to_user_sec_ctx(xp, skb))
2249 goto nlmsg_failure;
Jamal Hadi Salim1459bb32006-11-20 16:51:22 -08002250 if (copy_to_user_policy_type(xp->type, skb) < 0)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07002251 goto nlmsg_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252 upe->hard = !!hard;
2253
Thomas Graf98250692007-08-22 12:47:26 -07002254 return nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255
2256nlmsg_failure:
Thomas Graf98250692007-08-22 12:47:26 -07002257 nlmsg_cancel(skb, nlh);
2258 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002259}
2260
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002261static int xfrm_exp_policy_notify(struct xfrm_policy *xp, int dir, struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262{
2263 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264
Thomas Graf7deb2262007-08-22 13:57:39 -07002265 skb = nlmsg_new(xfrm_polexpire_msgsize(xp), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266 if (skb == NULL)
2267 return -ENOMEM;
2268
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002269 if (build_polexpire(skb, xp, dir, c) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002270 BUG();
2271
Thomas Graf082a1ad2007-08-22 13:54:36 -07002272 return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_EXPIRE, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002273}
2274
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002275static int xfrm_notify_policy(struct xfrm_policy *xp, int dir, struct km_event *c)
2276{
2277 struct xfrm_userpolicy_info *p;
Herbert Xu0603eac2005-06-18 22:54:36 -07002278 struct xfrm_userpolicy_id *id;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002279 struct nlmsghdr *nlh;
2280 struct sk_buff *skb;
Thomas Graf7deb2262007-08-22 13:57:39 -07002281 int len = nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr);
Herbert Xu0603eac2005-06-18 22:54:36 -07002282 int headlen;
2283
2284 headlen = sizeof(*p);
2285 if (c->event == XFRM_MSG_DELPOLICY) {
Thomas Graf7deb2262007-08-22 13:57:39 -07002286 len += nla_total_size(headlen);
Herbert Xu0603eac2005-06-18 22:54:36 -07002287 headlen = sizeof(*id);
2288 }
Thomas Grafcfbfd452007-08-22 13:57:04 -07002289 len += userpolicy_type_attrsize();
Thomas Graf7deb2262007-08-22 13:57:39 -07002290 len += NLMSG_ALIGN(headlen);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002291
Thomas Graf7deb2262007-08-22 13:57:39 -07002292 skb = nlmsg_new(len, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002293 if (skb == NULL)
2294 return -ENOMEM;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002295
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002296 nlh = nlmsg_put(skb, c->pid, c->seq, c->event, headlen, 0);
2297 if (nlh == NULL)
2298 goto nlmsg_failure;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002299
Thomas Graf7b67c852007-08-22 13:53:52 -07002300 p = nlmsg_data(nlh);
Herbert Xu0603eac2005-06-18 22:54:36 -07002301 if (c->event == XFRM_MSG_DELPOLICY) {
Thomas Grafc0144be2007-08-22 13:55:43 -07002302 struct nlattr *attr;
2303
Thomas Graf7b67c852007-08-22 13:53:52 -07002304 id = nlmsg_data(nlh);
Herbert Xu0603eac2005-06-18 22:54:36 -07002305 memset(id, 0, sizeof(*id));
2306 id->dir = dir;
2307 if (c->data.byid)
2308 id->index = xp->index;
2309 else
2310 memcpy(&id->sel, &xp->selector, sizeof(id->sel));
2311
Thomas Grafc0144be2007-08-22 13:55:43 -07002312 attr = nla_reserve(skb, XFRMA_POLICY, sizeof(*p));
2313 if (attr == NULL)
2314 goto nlmsg_failure;
2315
2316 p = nla_data(attr);
Herbert Xu0603eac2005-06-18 22:54:36 -07002317 }
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002318
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002319 copy_to_user_policy(xp, p, dir);
2320 if (copy_to_user_tmpl(xp, skb) < 0)
2321 goto nlmsg_failure;
Jamal Hadi Salim1459bb32006-11-20 16:51:22 -08002322 if (copy_to_user_policy_type(xp->type, skb) < 0)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07002323 goto nlmsg_failure;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002324
Thomas Graf98250692007-08-22 12:47:26 -07002325 nlmsg_end(skb, nlh);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002326
Thomas Graf082a1ad2007-08-22 13:54:36 -07002327 return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_POLICY, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002328
2329nlmsg_failure:
2330 kfree_skb(skb);
2331 return -1;
2332}
2333
2334static int xfrm_notify_policy_flush(struct km_event *c)
2335{
2336 struct nlmsghdr *nlh;
2337 struct sk_buff *skb;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002338
Thomas Graf7deb2262007-08-22 13:57:39 -07002339 skb = nlmsg_new(userpolicy_type_attrsize(), GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002340 if (skb == NULL)
2341 return -ENOMEM;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002342
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002343 nlh = nlmsg_put(skb, c->pid, c->seq, XFRM_MSG_FLUSHPOLICY, 0, 0);
2344 if (nlh == NULL)
2345 goto nlmsg_failure;
Jamal Hadi Salim0c51f532006-11-27 12:58:20 -08002346 if (copy_to_user_policy_type(c->data.type, skb) < 0)
2347 goto nlmsg_failure;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002348
Thomas Graf98250692007-08-22 12:47:26 -07002349 nlmsg_end(skb, nlh);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002350
Thomas Graf082a1ad2007-08-22 13:54:36 -07002351 return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_POLICY, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002352
2353nlmsg_failure:
2354 kfree_skb(skb);
2355 return -1;
2356}
2357
2358static int xfrm_send_policy_notify(struct xfrm_policy *xp, int dir, struct km_event *c)
2359{
2360
2361 switch (c->event) {
Herbert Xuf60f6b82005-06-18 22:44:37 -07002362 case XFRM_MSG_NEWPOLICY:
2363 case XFRM_MSG_UPDPOLICY:
2364 case XFRM_MSG_DELPOLICY:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002365 return xfrm_notify_policy(xp, dir, c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07002366 case XFRM_MSG_FLUSHPOLICY:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002367 return xfrm_notify_policy_flush(c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07002368 case XFRM_MSG_POLEXPIRE:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002369 return xfrm_exp_policy_notify(xp, dir, c);
2370 default:
2371 printk("xfrm_user: Unknown Policy event %d\n", c->event);
2372 }
2373
2374 return 0;
2375
2376}
2377
Thomas Graf7deb2262007-08-22 13:57:39 -07002378static inline size_t xfrm_report_msgsize(void)
2379{
2380 return NLMSG_ALIGN(sizeof(struct xfrm_user_report));
2381}
2382
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002383static int build_report(struct sk_buff *skb, u8 proto,
2384 struct xfrm_selector *sel, xfrm_address_t *addr)
2385{
2386 struct xfrm_user_report *ur;
2387 struct nlmsghdr *nlh;
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002388
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002389 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_REPORT, sizeof(*ur), 0);
2390 if (nlh == NULL)
2391 return -EMSGSIZE;
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002392
Thomas Graf7b67c852007-08-22 13:53:52 -07002393 ur = nlmsg_data(nlh);
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002394 ur->proto = proto;
2395 memcpy(&ur->sel, sel, sizeof(ur->sel));
2396
2397 if (addr)
Thomas Grafc0144be2007-08-22 13:55:43 -07002398 NLA_PUT(skb, XFRMA_COADDR, sizeof(*addr), addr);
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002399
Thomas Graf98250692007-08-22 12:47:26 -07002400 return nlmsg_end(skb, nlh);
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002401
Thomas Grafc0144be2007-08-22 13:55:43 -07002402nla_put_failure:
Thomas Graf98250692007-08-22 12:47:26 -07002403 nlmsg_cancel(skb, nlh);
2404 return -EMSGSIZE;
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002405}
2406
2407static int xfrm_send_report(u8 proto, struct xfrm_selector *sel,
2408 xfrm_address_t *addr)
2409{
2410 struct sk_buff *skb;
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002411
Thomas Graf7deb2262007-08-22 13:57:39 -07002412 skb = nlmsg_new(xfrm_report_msgsize(), GFP_ATOMIC);
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002413 if (skb == NULL)
2414 return -ENOMEM;
2415
2416 if (build_report(skb, proto, sel, addr) < 0)
2417 BUG();
2418
Thomas Graf082a1ad2007-08-22 13:54:36 -07002419 return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_REPORT, GFP_ATOMIC);
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002420}
2421
Linus Torvalds1da177e2005-04-16 15:20:36 -07002422static struct xfrm_mgr netlink_mgr = {
2423 .id = "netlink",
2424 .notify = xfrm_send_state_notify,
2425 .acquire = xfrm_send_acquire,
2426 .compile_policy = xfrm_compile_policy,
2427 .notify_policy = xfrm_send_policy_notify,
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002428 .report = xfrm_send_report,
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002429 .migrate = xfrm_send_migrate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002430};
2431
2432static int __init xfrm_user_init(void)
2433{
Patrick McHardybe336902006-03-20 22:40:54 -08002434 struct sock *nlsk;
2435
Masahide NAKAMURA654b32c2006-08-23 19:12:56 -07002436 printk(KERN_INFO "Initializing XFRM netlink socket\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002437
Patrick McHardybe336902006-03-20 22:40:54 -08002438 nlsk = netlink_kernel_create(NETLINK_XFRM, XFRMNLGRP_MAX,
Patrick McHardyaf65bdf2007-04-20 14:14:21 -07002439 xfrm_netlink_rcv, NULL, THIS_MODULE);
Patrick McHardybe336902006-03-20 22:40:54 -08002440 if (nlsk == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002441 return -ENOMEM;
Patrick McHardybe336902006-03-20 22:40:54 -08002442 rcu_assign_pointer(xfrm_nl, nlsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002443
2444 xfrm_register_km(&netlink_mgr);
2445
2446 return 0;
2447}
2448
2449static void __exit xfrm_user_exit(void)
2450{
Patrick McHardybe336902006-03-20 22:40:54 -08002451 struct sock *nlsk = xfrm_nl;
2452
Linus Torvalds1da177e2005-04-16 15:20:36 -07002453 xfrm_unregister_km(&netlink_mgr);
Patrick McHardybe336902006-03-20 22:40:54 -08002454 rcu_assign_pointer(xfrm_nl, NULL);
2455 synchronize_rcu();
2456 sock_release(nlsk->sk_socket);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002457}
2458
2459module_init(xfrm_user_init);
2460module_exit(xfrm_user_exit);
2461MODULE_LICENSE("GPL");
Harald Welte4fdb3bb2005-08-09 19:40:55 -07002462MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK, NETLINK_XFRM);
Jamal Hadi Salimf8cd5482006-03-20 19:15:11 -08002463