blob: 78338079b7f579d07ad4267ae782a1bfea2613e2 [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/pfkeyv2.h>
23#include <linux/ipsec.h>
24#include <linux/init.h>
25#include <linux/security.h>
26#include <net/sock.h>
27#include <net/xfrm.h>
Thomas Graf88fc2c82005-11-10 02:25:54 +010028#include <net/netlink.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <asm/uaccess.h>
Masahide NAKAMURAe23c7192006-08-23 20:33:28 -070030#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
31#include <linux/in6.h>
32#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070033
Herbert Xu1a6509d2008-01-28 19:37:29 -080034static inline int aead_len(struct xfrm_algo_aead *alg)
35{
36 return sizeof(*alg) + ((alg->alg_key_len + 7) / 8);
37}
38
Thomas Graf5424f322007-08-22 14:01:33 -070039static int verify_one_alg(struct nlattr **attrs, enum xfrm_attr_type_t type)
Linus Torvalds1da177e2005-04-16 15:20:36 -070040{
Thomas Graf5424f322007-08-22 14:01:33 -070041 struct nlattr *rt = attrs[type];
Linus Torvalds1da177e2005-04-16 15:20:36 -070042 struct xfrm_algo *algp;
43
44 if (!rt)
45 return 0;
46
Thomas Graf5424f322007-08-22 14:01:33 -070047 algp = nla_data(rt);
Eric Dumazet0f99be02008-01-08 23:39:06 -080048 if (nla_len(rt) < xfrm_alg_len(algp))
Herbert Xu31c26852005-05-19 12:39:49 -070049 return -EINVAL;
50
Linus Torvalds1da177e2005-04-16 15:20:36 -070051 switch (type) {
52 case XFRMA_ALG_AUTH:
53 if (!algp->alg_key_len &&
54 strcmp(algp->alg_name, "digest_null") != 0)
55 return -EINVAL;
56 break;
57
58 case XFRMA_ALG_CRYPT:
59 if (!algp->alg_key_len &&
60 strcmp(algp->alg_name, "cipher_null") != 0)
61 return -EINVAL;
62 break;
63
64 case XFRMA_ALG_COMP:
65 /* Zero length keys are legal. */
66 break;
67
68 default:
69 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -070070 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
72 algp->alg_name[CRYPTO_MAX_ALG_NAME - 1] = '\0';
73 return 0;
74}
75
Herbert Xu1a6509d2008-01-28 19:37:29 -080076static int verify_aead(struct nlattr **attrs)
77{
78 struct nlattr *rt = attrs[XFRMA_ALG_AEAD];
79 struct xfrm_algo_aead *algp;
80
81 if (!rt)
82 return 0;
83
84 algp = nla_data(rt);
85 if (nla_len(rt) < aead_len(algp))
86 return -EINVAL;
87
88 algp->alg_name[CRYPTO_MAX_ALG_NAME - 1] = '\0';
89 return 0;
90}
91
Thomas Graf5424f322007-08-22 14:01:33 -070092static void verify_one_addr(struct nlattr **attrs, enum xfrm_attr_type_t type,
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -070093 xfrm_address_t **addrp)
94{
Thomas Graf5424f322007-08-22 14:01:33 -070095 struct nlattr *rt = attrs[type];
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -070096
Thomas Grafcf5cb792007-08-22 13:59:04 -070097 if (rt && addrp)
Thomas Graf5424f322007-08-22 14:01:33 -070098 *addrp = nla_data(rt);
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -070099}
Trent Jaegerdf718372005-12-13 23:12:27 -0800100
Thomas Graf5424f322007-08-22 14:01:33 -0700101static inline int verify_sec_ctx_len(struct nlattr **attrs)
Trent Jaegerdf718372005-12-13 23:12:27 -0800102{
Thomas Graf5424f322007-08-22 14:01:33 -0700103 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
Trent Jaegerdf718372005-12-13 23:12:27 -0800104 struct xfrm_user_sec_ctx *uctx;
Trent Jaegerdf718372005-12-13 23:12:27 -0800105
106 if (!rt)
107 return 0;
108
Thomas Graf5424f322007-08-22 14:01:33 -0700109 uctx = nla_data(rt);
Thomas Grafcf5cb792007-08-22 13:59:04 -0700110 if (uctx->len != (sizeof(struct xfrm_user_sec_ctx) + uctx->ctx_len))
Trent Jaegerdf718372005-12-13 23:12:27 -0800111 return -EINVAL;
112
113 return 0;
114}
115
116
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117static int verify_newsa_info(struct xfrm_usersa_info *p,
Thomas Graf5424f322007-08-22 14:01:33 -0700118 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119{
120 int err;
121
122 err = -EINVAL;
123 switch (p->family) {
124 case AF_INET:
125 break;
126
127 case AF_INET6:
128#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
129 break;
130#else
131 err = -EAFNOSUPPORT;
132 goto out;
133#endif
134
135 default:
136 goto out;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700137 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138
139 err = -EINVAL;
140 switch (p->id.proto) {
141 case IPPROTO_AH:
Thomas Graf35a7aa02007-08-22 14:00:40 -0700142 if (!attrs[XFRMA_ALG_AUTH] ||
Herbert Xu1a6509d2008-01-28 19:37:29 -0800143 attrs[XFRMA_ALG_AEAD] ||
Thomas Graf35a7aa02007-08-22 14:00:40 -0700144 attrs[XFRMA_ALG_CRYPT] ||
145 attrs[XFRMA_ALG_COMP])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 goto out;
147 break;
148
149 case IPPROTO_ESP:
Herbert Xu1a6509d2008-01-28 19:37:29 -0800150 if (attrs[XFRMA_ALG_COMP])
151 goto out;
152 if (!attrs[XFRMA_ALG_AUTH] &&
153 !attrs[XFRMA_ALG_CRYPT] &&
154 !attrs[XFRMA_ALG_AEAD])
155 goto out;
156 if ((attrs[XFRMA_ALG_AUTH] ||
157 attrs[XFRMA_ALG_CRYPT]) &&
158 attrs[XFRMA_ALG_AEAD])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 goto out;
160 break;
161
162 case IPPROTO_COMP:
Thomas Graf35a7aa02007-08-22 14:00:40 -0700163 if (!attrs[XFRMA_ALG_COMP] ||
Herbert Xu1a6509d2008-01-28 19:37:29 -0800164 attrs[XFRMA_ALG_AEAD] ||
Thomas Graf35a7aa02007-08-22 14:00:40 -0700165 attrs[XFRMA_ALG_AUTH] ||
166 attrs[XFRMA_ALG_CRYPT])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 goto out;
168 break;
169
Masahide NAKAMURAe23c7192006-08-23 20:33:28 -0700170#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
171 case IPPROTO_DSTOPTS:
172 case IPPROTO_ROUTING:
Thomas Graf35a7aa02007-08-22 14:00:40 -0700173 if (attrs[XFRMA_ALG_COMP] ||
174 attrs[XFRMA_ALG_AUTH] ||
Herbert Xu1a6509d2008-01-28 19:37:29 -0800175 attrs[XFRMA_ALG_AEAD] ||
Thomas Graf35a7aa02007-08-22 14:00:40 -0700176 attrs[XFRMA_ALG_CRYPT] ||
177 attrs[XFRMA_ENCAP] ||
178 attrs[XFRMA_SEC_CTX] ||
179 !attrs[XFRMA_COADDR])
Masahide NAKAMURAe23c7192006-08-23 20:33:28 -0700180 goto out;
181 break;
182#endif
183
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 default:
185 goto out;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700186 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187
Herbert Xu1a6509d2008-01-28 19:37:29 -0800188 if ((err = verify_aead(attrs)))
189 goto out;
Thomas Graf35a7aa02007-08-22 14:00:40 -0700190 if ((err = verify_one_alg(attrs, XFRMA_ALG_AUTH)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 goto out;
Thomas Graf35a7aa02007-08-22 14:00:40 -0700192 if ((err = verify_one_alg(attrs, XFRMA_ALG_CRYPT)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 goto out;
Thomas Graf35a7aa02007-08-22 14:00:40 -0700194 if ((err = verify_one_alg(attrs, XFRMA_ALG_COMP)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 goto out;
Thomas Graf35a7aa02007-08-22 14:00:40 -0700196 if ((err = verify_sec_ctx_len(attrs)))
Trent Jaegerdf718372005-12-13 23:12:27 -0800197 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198
199 err = -EINVAL;
200 switch (p->mode) {
Masahide NAKAMURA7e49e6d2006-09-22 15:05:15 -0700201 case XFRM_MODE_TRANSPORT:
202 case XFRM_MODE_TUNNEL:
Noriaki TAKAMIYA060f02a2006-08-23 18:18:55 -0700203 case XFRM_MODE_ROUTEOPTIMIZATION:
Diego Beltrami0a694522006-10-03 23:47:05 -0700204 case XFRM_MODE_BEET:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 break;
206
207 default:
208 goto out;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700209 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210
211 err = 0;
212
213out:
214 return err;
215}
216
217static int attach_one_algo(struct xfrm_algo **algpp, u8 *props,
218 struct xfrm_algo_desc *(*get_byname)(char *, int),
Thomas Graf5424f322007-08-22 14:01:33 -0700219 struct nlattr *rta)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 struct xfrm_algo *p, *ualg;
222 struct xfrm_algo_desc *algo;
223
224 if (!rta)
225 return 0;
226
Thomas Graf5424f322007-08-22 14:01:33 -0700227 ualg = nla_data(rta);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228
229 algo = get_byname(ualg->alg_name, 1);
230 if (!algo)
231 return -ENOSYS;
232 *props = algo->desc.sadb_alg_id;
233
Eric Dumazet0f99be02008-01-08 23:39:06 -0800234 p = kmemdup(ualg, xfrm_alg_len(ualg), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 if (!p)
236 return -ENOMEM;
237
Herbert Xu04ff1262006-08-13 08:50:00 +1000238 strcpy(p->alg_name, algo->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 *algpp = p;
240 return 0;
241}
242
Herbert Xu1a6509d2008-01-28 19:37:29 -0800243static int attach_aead(struct xfrm_algo_aead **algpp, u8 *props,
244 struct nlattr *rta)
245{
246 struct xfrm_algo_aead *p, *ualg;
247 struct xfrm_algo_desc *algo;
248
249 if (!rta)
250 return 0;
251
252 ualg = nla_data(rta);
253
254 algo = xfrm_aead_get_byname(ualg->alg_name, ualg->alg_icv_len, 1);
255 if (!algo)
256 return -ENOSYS;
257 *props = algo->desc.sadb_alg_id;
258
259 p = kmemdup(ualg, aead_len(ualg), GFP_KERNEL);
260 if (!p)
261 return -ENOMEM;
262
263 strcpy(p->alg_name, algo->name);
264 *algpp = p;
265 return 0;
266}
267
Joy Latten661697f2007-04-13 16:14:35 -0700268static inline int xfrm_user_sec_ctx_size(struct xfrm_sec_ctx *xfrm_ctx)
Trent Jaegerdf718372005-12-13 23:12:27 -0800269{
Trent Jaegerdf718372005-12-13 23:12:27 -0800270 int len = 0;
271
272 if (xfrm_ctx) {
273 len += sizeof(struct xfrm_user_sec_ctx);
274 len += xfrm_ctx->ctx_len;
275 }
276 return len;
277}
278
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279static void copy_from_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p)
280{
281 memcpy(&x->id, &p->id, sizeof(x->id));
282 memcpy(&x->sel, &p->sel, sizeof(x->sel));
283 memcpy(&x->lft, &p->lft, sizeof(x->lft));
284 x->props.mode = p->mode;
285 x->props.replay_window = p->replay_window;
286 x->props.reqid = p->reqid;
287 x->props.family = p->family;
David S. Miller54489c142006-10-27 15:29:47 -0700288 memcpy(&x->props.saddr, &p->saddr, sizeof(x->props.saddr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 x->props.flags = p->flags;
Herbert Xu196b0032007-07-31 02:04:32 -0700290
291 /*
292 * Set inner address family if the KM left it as zero.
293 * See comment in validate_tmpl.
294 */
295 if (!x->sel.family)
296 x->sel.family = p->family;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297}
298
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800299/*
300 * someday when pfkey also has support, we could have the code
301 * somehow made shareable and move it to xfrm_state.c - JHS
302 *
303*/
Thomas Graf5424f322007-08-22 14:01:33 -0700304static void xfrm_update_ae_params(struct xfrm_state *x, struct nlattr **attrs)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800305{
Thomas Graf5424f322007-08-22 14:01:33 -0700306 struct nlattr *rp = attrs[XFRMA_REPLAY_VAL];
307 struct nlattr *lt = attrs[XFRMA_LTIME_VAL];
308 struct nlattr *et = attrs[XFRMA_ETIMER_THRESH];
309 struct nlattr *rt = attrs[XFRMA_REPLAY_THRESH];
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800310
311 if (rp) {
312 struct xfrm_replay_state *replay;
Thomas Graf5424f322007-08-22 14:01:33 -0700313 replay = nla_data(rp);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800314 memcpy(&x->replay, replay, sizeof(*replay));
315 memcpy(&x->preplay, replay, sizeof(*replay));
316 }
317
318 if (lt) {
319 struct xfrm_lifetime_cur *ltime;
Thomas Graf5424f322007-08-22 14:01:33 -0700320 ltime = nla_data(lt);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800321 x->curlft.bytes = ltime->bytes;
322 x->curlft.packets = ltime->packets;
323 x->curlft.add_time = ltime->add_time;
324 x->curlft.use_time = ltime->use_time;
325 }
326
Thomas Grafcf5cb792007-08-22 13:59:04 -0700327 if (et)
Thomas Graf5424f322007-08-22 14:01:33 -0700328 x->replay_maxage = nla_get_u32(et);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800329
Thomas Grafcf5cb792007-08-22 13:59:04 -0700330 if (rt)
Thomas Graf5424f322007-08-22 14:01:33 -0700331 x->replay_maxdiff = nla_get_u32(rt);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800332}
333
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334static struct xfrm_state *xfrm_state_construct(struct xfrm_usersa_info *p,
Thomas Graf5424f322007-08-22 14:01:33 -0700335 struct nlattr **attrs,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 int *errp)
337{
338 struct xfrm_state *x = xfrm_state_alloc();
339 int err = -ENOMEM;
340
341 if (!x)
342 goto error_no_put;
343
344 copy_from_user_state(x, p);
345
Herbert Xu1a6509d2008-01-28 19:37:29 -0800346 if ((err = attach_aead(&x->aead, &x->props.ealgo,
347 attrs[XFRMA_ALG_AEAD])))
348 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 if ((err = attach_one_algo(&x->aalg, &x->props.aalgo,
350 xfrm_aalg_get_byname,
Thomas Graf35a7aa02007-08-22 14:00:40 -0700351 attrs[XFRMA_ALG_AUTH])))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 goto error;
353 if ((err = attach_one_algo(&x->ealg, &x->props.ealgo,
354 xfrm_ealg_get_byname,
Thomas Graf35a7aa02007-08-22 14:00:40 -0700355 attrs[XFRMA_ALG_CRYPT])))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 goto error;
357 if ((err = attach_one_algo(&x->calg, &x->props.calgo,
358 xfrm_calg_get_byname,
Thomas Graf35a7aa02007-08-22 14:00:40 -0700359 attrs[XFRMA_ALG_COMP])))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 goto error;
Thomas Graffd211502007-09-06 03:28:08 -0700361
362 if (attrs[XFRMA_ENCAP]) {
363 x->encap = kmemdup(nla_data(attrs[XFRMA_ENCAP]),
364 sizeof(*x->encap), GFP_KERNEL);
365 if (x->encap == NULL)
366 goto error;
367 }
368
369 if (attrs[XFRMA_COADDR]) {
370 x->coaddr = kmemdup(nla_data(attrs[XFRMA_COADDR]),
371 sizeof(*x->coaddr), GFP_KERNEL);
372 if (x->coaddr == NULL)
373 goto error;
374 }
375
Herbert Xu72cb6962005-06-20 13:18:08 -0700376 err = xfrm_init_state(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 if (err)
378 goto error;
379
Thomas Graffd211502007-09-06 03:28:08 -0700380 if (attrs[XFRMA_SEC_CTX] &&
381 security_xfrm_state_alloc(x, nla_data(attrs[XFRMA_SEC_CTX])))
Trent Jaegerdf718372005-12-13 23:12:27 -0800382 goto error;
383
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 x->km.seq = p->seq;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800385 x->replay_maxdiff = sysctl_xfrm_aevent_rseqth;
386 /* sysctl_xfrm_aevent_etime is in 100ms units */
387 x->replay_maxage = (sysctl_xfrm_aevent_etime*HZ)/XFRM_AE_ETH_M;
388 x->preplay.bitmap = 0;
389 x->preplay.seq = x->replay.seq+x->replay_maxdiff;
390 x->preplay.oseq = x->replay.oseq +x->replay_maxdiff;
391
392 /* override default values from above */
393
Thomas Graf5424f322007-08-22 14:01:33 -0700394 xfrm_update_ae_params(x, attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395
396 return x;
397
398error:
399 x->km.state = XFRM_STATE_DEAD;
400 xfrm_state_put(x);
401error_no_put:
402 *errp = err;
403 return NULL;
404}
405
Christoph Hellwig22e70052007-01-02 15:22:30 -0800406static int xfrm_add_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -0700407 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408{
Thomas Graf7b67c852007-08-22 13:53:52 -0700409 struct xfrm_usersa_info *p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 struct xfrm_state *x;
411 int err;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700412 struct km_event c;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413
Thomas Graf35a7aa02007-08-22 14:00:40 -0700414 err = verify_newsa_info(p, attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 if (err)
416 return err;
417
Thomas Graf35a7aa02007-08-22 14:00:40 -0700418 x = xfrm_state_construct(p, attrs, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 if (!x)
420 return err;
421
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700422 xfrm_state_hold(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 if (nlh->nlmsg_type == XFRM_MSG_NEWSA)
424 err = xfrm_state_add(x);
425 else
426 err = xfrm_state_update(x);
427
Joy Lattenab5f5e82007-09-17 11:51:22 -0700428 xfrm_audit_state_add(x, err ? 0 : 1, NETLINK_CB(skb).loginuid,
429 NETLINK_CB(skb).sid);
Joy Latten161a09e2006-11-27 13:11:54 -0600430
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 if (err < 0) {
432 x->km.state = XFRM_STATE_DEAD;
Herbert Xu21380b82006-02-22 14:47:13 -0800433 __xfrm_state_put(x);
Patrick McHardy7d6dfe12005-06-18 22:45:31 -0700434 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 }
436
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700437 c.seq = nlh->nlmsg_seq;
438 c.pid = nlh->nlmsg_pid;
Herbert Xuf60f6b82005-06-18 22:44:37 -0700439 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700440
441 km_state_notify(x, &c);
Patrick McHardy7d6dfe12005-06-18 22:45:31 -0700442out:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700443 xfrm_state_put(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 return err;
445}
446
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700447static struct xfrm_state *xfrm_user_state_lookup(struct xfrm_usersa_id *p,
Thomas Graf5424f322007-08-22 14:01:33 -0700448 struct nlattr **attrs,
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700449 int *errp)
450{
451 struct xfrm_state *x = NULL;
452 int err;
453
454 if (xfrm_id_proto_match(p->proto, IPSEC_PROTO_ANY)) {
455 err = -ESRCH;
456 x = xfrm_state_lookup(&p->daddr, p->spi, p->proto, p->family);
457 } else {
458 xfrm_address_t *saddr = NULL;
459
Thomas Graf35a7aa02007-08-22 14:00:40 -0700460 verify_one_addr(attrs, XFRMA_SRCADDR, &saddr);
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700461 if (!saddr) {
462 err = -EINVAL;
463 goto out;
464 }
465
Masahide NAKAMURA9abbffe2006-11-24 20:34:51 -0800466 err = -ESRCH;
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700467 x = xfrm_state_lookup_byaddr(&p->daddr, saddr, p->proto,
468 p->family);
469 }
470
471 out:
472 if (!x && errp)
473 *errp = err;
474 return x;
475}
476
Christoph Hellwig22e70052007-01-02 15:22:30 -0800477static int xfrm_del_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -0700478 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479{
480 struct xfrm_state *x;
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700481 int err = -ESRCH;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700482 struct km_event c;
Thomas Graf7b67c852007-08-22 13:53:52 -0700483 struct xfrm_usersa_id *p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484
Thomas Graf35a7aa02007-08-22 14:00:40 -0700485 x = xfrm_user_state_lookup(p, attrs, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 if (x == NULL)
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700487 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488
David S. Miller6f68dc32006-06-08 23:58:52 -0700489 if ((err = security_xfrm_state_delete(x)) != 0)
Catherine Zhangc8c05a82006-06-08 23:39:49 -0700490 goto out;
491
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 if (xfrm_state_kern(x)) {
Catherine Zhangc8c05a82006-06-08 23:39:49 -0700493 err = -EPERM;
494 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 }
496
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700497 err = xfrm_state_delete(x);
Joy Latten161a09e2006-11-27 13:11:54 -0600498
Catherine Zhangc8c05a82006-06-08 23:39:49 -0700499 if (err < 0)
500 goto out;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700501
502 c.seq = nlh->nlmsg_seq;
503 c.pid = nlh->nlmsg_pid;
Herbert Xuf60f6b82005-06-18 22:44:37 -0700504 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700505 km_state_notify(x, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506
Catherine Zhangc8c05a82006-06-08 23:39:49 -0700507out:
Joy Lattenab5f5e82007-09-17 11:51:22 -0700508 xfrm_audit_state_delete(x, err ? 0 : 1, NETLINK_CB(skb).loginuid,
509 NETLINK_CB(skb).sid);
Catherine Zhangc8c05a82006-06-08 23:39:49 -0700510 xfrm_state_put(x);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700511 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512}
513
514static void copy_to_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p)
515{
516 memcpy(&p->id, &x->id, sizeof(p->id));
517 memcpy(&p->sel, &x->sel, sizeof(p->sel));
518 memcpy(&p->lft, &x->lft, sizeof(p->lft));
519 memcpy(&p->curlft, &x->curlft, sizeof(p->curlft));
520 memcpy(&p->stats, &x->stats, sizeof(p->stats));
David S. Miller54489c142006-10-27 15:29:47 -0700521 memcpy(&p->saddr, &x->props.saddr, sizeof(p->saddr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 p->mode = x->props.mode;
523 p->replay_window = x->props.replay_window;
524 p->reqid = x->props.reqid;
525 p->family = x->props.family;
526 p->flags = x->props.flags;
527 p->seq = x->km.seq;
528}
529
530struct xfrm_dump_info {
531 struct sk_buff *in_skb;
532 struct sk_buff *out_skb;
533 u32 nlmsg_seq;
534 u16 nlmsg_flags;
535 int start_idx;
536 int this_idx;
537};
538
Thomas Grafc0144be2007-08-22 13:55:43 -0700539static int copy_sec_ctx(struct xfrm_sec_ctx *s, struct sk_buff *skb)
540{
Thomas Grafc0144be2007-08-22 13:55:43 -0700541 struct xfrm_user_sec_ctx *uctx;
542 struct nlattr *attr;
Herbert Xu68325d32007-10-09 13:30:57 -0700543 int ctx_size = sizeof(*uctx) + s->ctx_len;
Thomas Grafc0144be2007-08-22 13:55:43 -0700544
545 attr = nla_reserve(skb, XFRMA_SEC_CTX, ctx_size);
546 if (attr == NULL)
547 return -EMSGSIZE;
548
549 uctx = nla_data(attr);
550 uctx->exttype = XFRMA_SEC_CTX;
551 uctx->len = ctx_size;
552 uctx->ctx_doi = s->ctx_doi;
553 uctx->ctx_alg = s->ctx_alg;
554 uctx->ctx_len = s->ctx_len;
555 memcpy(uctx + 1, s->ctx_str, s->ctx_len);
556
557 return 0;
558}
559
Herbert Xu68325d32007-10-09 13:30:57 -0700560/* Don't change this without updating xfrm_sa_len! */
561static int copy_to_user_state_extra(struct xfrm_state *x,
562 struct xfrm_usersa_info *p,
563 struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 copy_to_user_state(x, p);
566
Herbert Xu050f0092007-10-09 13:31:47 -0700567 if (x->coaddr)
568 NLA_PUT(skb, XFRMA_COADDR, sizeof(*x->coaddr), x->coaddr);
569
570 if (x->lastused)
571 NLA_PUT_U64(skb, XFRMA_LASTUSED, x->lastused);
Herbert Xu050f0092007-10-09 13:31:47 -0700572
Herbert Xu1a6509d2008-01-28 19:37:29 -0800573 if (x->aead)
574 NLA_PUT(skb, XFRMA_ALG_AEAD, aead_len(x->aead), x->aead);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 if (x->aalg)
Eric Dumazet0f99be02008-01-08 23:39:06 -0800576 NLA_PUT(skb, XFRMA_ALG_AUTH, xfrm_alg_len(x->aalg), x->aalg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 if (x->ealg)
Eric Dumazet0f99be02008-01-08 23:39:06 -0800578 NLA_PUT(skb, XFRMA_ALG_CRYPT, xfrm_alg_len(x->ealg), x->ealg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 if (x->calg)
Thomas Grafc0144be2007-08-22 13:55:43 -0700580 NLA_PUT(skb, XFRMA_ALG_COMP, sizeof(*(x->calg)), x->calg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581
582 if (x->encap)
Thomas Grafc0144be2007-08-22 13:55:43 -0700583 NLA_PUT(skb, XFRMA_ENCAP, sizeof(*x->encap), x->encap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584
Thomas Grafc0144be2007-08-22 13:55:43 -0700585 if (x->security && copy_sec_ctx(x->security, skb) < 0)
586 goto nla_put_failure;
Noriaki TAKAMIYA060f02a2006-08-23 18:18:55 -0700587
Herbert Xu68325d32007-10-09 13:30:57 -0700588 return 0;
589
590nla_put_failure:
591 return -EMSGSIZE;
592}
593
594static int dump_one_state(struct xfrm_state *x, int count, void *ptr)
595{
596 struct xfrm_dump_info *sp = ptr;
597 struct sk_buff *in_skb = sp->in_skb;
598 struct sk_buff *skb = sp->out_skb;
599 struct xfrm_usersa_info *p;
600 struct nlmsghdr *nlh;
601 int err;
602
603 if (sp->this_idx < sp->start_idx)
604 goto out;
605
606 nlh = nlmsg_put(skb, NETLINK_CB(in_skb).pid, sp->nlmsg_seq,
607 XFRM_MSG_NEWSA, sizeof(*p), sp->nlmsg_flags);
608 if (nlh == NULL)
609 return -EMSGSIZE;
610
611 p = nlmsg_data(nlh);
612
613 err = copy_to_user_state_extra(x, p, skb);
614 if (err)
615 goto nla_put_failure;
616
Thomas Graf98250692007-08-22 12:47:26 -0700617 nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618out:
619 sp->this_idx++;
620 return 0;
621
Thomas Grafc0144be2007-08-22 13:55:43 -0700622nla_put_failure:
Thomas Graf98250692007-08-22 12:47:26 -0700623 nlmsg_cancel(skb, nlh);
Herbert Xu68325d32007-10-09 13:30:57 -0700624 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625}
626
627static int xfrm_dump_sa(struct sk_buff *skb, struct netlink_callback *cb)
628{
629 struct xfrm_dump_info info;
630
631 info.in_skb = cb->skb;
632 info.out_skb = skb;
633 info.nlmsg_seq = cb->nlh->nlmsg_seq;
634 info.nlmsg_flags = NLM_F_MULTI;
635 info.this_idx = 0;
636 info.start_idx = cb->args[0];
Masahide NAKAMURAdc00a522006-08-23 17:49:52 -0700637 (void) xfrm_state_walk(0, dump_one_state, &info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 cb->args[0] = info.this_idx;
639
640 return skb->len;
641}
642
643static struct sk_buff *xfrm_state_netlink(struct sk_buff *in_skb,
644 struct xfrm_state *x, u32 seq)
645{
646 struct xfrm_dump_info info;
647 struct sk_buff *skb;
648
Thomas Graf7deb2262007-08-22 13:57:39 -0700649 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 if (!skb)
651 return ERR_PTR(-ENOMEM);
652
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 info.in_skb = in_skb;
654 info.out_skb = skb;
655 info.nlmsg_seq = seq;
656 info.nlmsg_flags = 0;
657 info.this_idx = info.start_idx = 0;
658
659 if (dump_one_state(x, 0, &info)) {
660 kfree_skb(skb);
661 return NULL;
662 }
663
664 return skb;
665}
666
Thomas Graf7deb2262007-08-22 13:57:39 -0700667static inline size_t xfrm_spdinfo_msgsize(void)
668{
669 return NLMSG_ALIGN(4)
670 + nla_total_size(sizeof(struct xfrmu_spdinfo))
671 + nla_total_size(sizeof(struct xfrmu_spdhinfo));
672}
673
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700674static int build_spdinfo(struct sk_buff *skb, u32 pid, u32 seq, u32 flags)
675{
Jamal Hadi Salim5a6d3412007-05-04 12:55:39 -0700676 struct xfrmk_spdinfo si;
677 struct xfrmu_spdinfo spc;
678 struct xfrmu_spdhinfo sph;
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700679 struct nlmsghdr *nlh;
680 u32 *f;
681
682 nlh = nlmsg_put(skb, pid, seq, XFRM_MSG_NEWSPDINFO, sizeof(u32), 0);
683 if (nlh == NULL) /* shouldnt really happen ... */
684 return -EMSGSIZE;
685
686 f = nlmsg_data(nlh);
687 *f = flags;
688 xfrm_spd_getinfo(&si);
Jamal Hadi Salim5a6d3412007-05-04 12:55:39 -0700689 spc.incnt = si.incnt;
690 spc.outcnt = si.outcnt;
691 spc.fwdcnt = si.fwdcnt;
692 spc.inscnt = si.inscnt;
693 spc.outscnt = si.outscnt;
694 spc.fwdscnt = si.fwdscnt;
695 sph.spdhcnt = si.spdhcnt;
696 sph.spdhmcnt = si.spdhmcnt;
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700697
Jamal Hadi Salim5a6d3412007-05-04 12:55:39 -0700698 NLA_PUT(skb, XFRMA_SPD_INFO, sizeof(spc), &spc);
699 NLA_PUT(skb, XFRMA_SPD_HINFO, sizeof(sph), &sph);
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700700
701 return nlmsg_end(skb, nlh);
702
703nla_put_failure:
704 nlmsg_cancel(skb, nlh);
705 return -EMSGSIZE;
706}
707
708static int xfrm_get_spdinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -0700709 struct nlattr **attrs)
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700710{
711 struct sk_buff *r_skb;
Thomas Graf7b67c852007-08-22 13:53:52 -0700712 u32 *flags = nlmsg_data(nlh);
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700713 u32 spid = NETLINK_CB(skb).pid;
714 u32 seq = nlh->nlmsg_seq;
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700715
Thomas Graf7deb2262007-08-22 13:57:39 -0700716 r_skb = nlmsg_new(xfrm_spdinfo_msgsize(), GFP_ATOMIC);
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700717 if (r_skb == NULL)
718 return -ENOMEM;
719
720 if (build_spdinfo(r_skb, spid, seq, *flags) < 0)
721 BUG();
722
723 return nlmsg_unicast(xfrm_nl, r_skb, spid);
724}
725
Thomas Graf7deb2262007-08-22 13:57:39 -0700726static inline size_t xfrm_sadinfo_msgsize(void)
727{
728 return NLMSG_ALIGN(4)
729 + nla_total_size(sizeof(struct xfrmu_sadhinfo))
730 + nla_total_size(4); /* XFRMA_SAD_CNT */
731}
732
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700733static int build_sadinfo(struct sk_buff *skb, u32 pid, u32 seq, u32 flags)
734{
Jamal Hadi Salimaf11e312007-05-04 12:55:13 -0700735 struct xfrmk_sadinfo si;
736 struct xfrmu_sadhinfo sh;
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700737 struct nlmsghdr *nlh;
738 u32 *f;
739
740 nlh = nlmsg_put(skb, pid, seq, XFRM_MSG_NEWSADINFO, sizeof(u32), 0);
741 if (nlh == NULL) /* shouldnt really happen ... */
742 return -EMSGSIZE;
743
744 f = nlmsg_data(nlh);
745 *f = flags;
746 xfrm_sad_getinfo(&si);
747
Jamal Hadi Salimaf11e312007-05-04 12:55:13 -0700748 sh.sadhmcnt = si.sadhmcnt;
749 sh.sadhcnt = si.sadhcnt;
750
751 NLA_PUT_U32(skb, XFRMA_SAD_CNT, si.sadcnt);
752 NLA_PUT(skb, XFRMA_SAD_HINFO, sizeof(sh), &sh);
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700753
754 return nlmsg_end(skb, nlh);
755
756nla_put_failure:
757 nlmsg_cancel(skb, nlh);
758 return -EMSGSIZE;
759}
760
761static int xfrm_get_sadinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -0700762 struct nlattr **attrs)
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700763{
764 struct sk_buff *r_skb;
Thomas Graf7b67c852007-08-22 13:53:52 -0700765 u32 *flags = nlmsg_data(nlh);
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700766 u32 spid = NETLINK_CB(skb).pid;
767 u32 seq = nlh->nlmsg_seq;
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700768
Thomas Graf7deb2262007-08-22 13:57:39 -0700769 r_skb = nlmsg_new(xfrm_sadinfo_msgsize(), GFP_ATOMIC);
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700770 if (r_skb == NULL)
771 return -ENOMEM;
772
773 if (build_sadinfo(r_skb, spid, seq, *flags) < 0)
774 BUG();
775
776 return nlmsg_unicast(xfrm_nl, r_skb, spid);
777}
778
Christoph Hellwig22e70052007-01-02 15:22:30 -0800779static int xfrm_get_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -0700780 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781{
Thomas Graf7b67c852007-08-22 13:53:52 -0700782 struct xfrm_usersa_id *p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 struct xfrm_state *x;
784 struct sk_buff *resp_skb;
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700785 int err = -ESRCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786
Thomas Graf35a7aa02007-08-22 14:00:40 -0700787 x = xfrm_user_state_lookup(p, attrs, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 if (x == NULL)
789 goto out_noput;
790
791 resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
792 if (IS_ERR(resp_skb)) {
793 err = PTR_ERR(resp_skb);
794 } else {
Thomas Graf082a1ad2007-08-22 13:54:36 -0700795 err = nlmsg_unicast(xfrm_nl, resp_skb, NETLINK_CB(skb).pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 }
797 xfrm_state_put(x);
798out_noput:
799 return err;
800}
801
802static int verify_userspi_info(struct xfrm_userspi_info *p)
803{
804 switch (p->info.id.proto) {
805 case IPPROTO_AH:
806 case IPPROTO_ESP:
807 break;
808
809 case IPPROTO_COMP:
810 /* IPCOMP spi is 16-bits. */
811 if (p->max >= 0x10000)
812 return -EINVAL;
813 break;
814
815 default:
816 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700817 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818
819 if (p->min > p->max)
820 return -EINVAL;
821
822 return 0;
823}
824
Christoph Hellwig22e70052007-01-02 15:22:30 -0800825static int xfrm_alloc_userspi(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -0700826 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827{
828 struct xfrm_state *x;
829 struct xfrm_userspi_info *p;
830 struct sk_buff *resp_skb;
831 xfrm_address_t *daddr;
832 int family;
833 int err;
834
Thomas Graf7b67c852007-08-22 13:53:52 -0700835 p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 err = verify_userspi_info(p);
837 if (err)
838 goto out_noput;
839
840 family = p->info.family;
841 daddr = &p->info.id.daddr;
842
843 x = NULL;
844 if (p->info.seq) {
845 x = xfrm_find_acq_byseq(p->info.seq);
846 if (x && xfrm_addr_cmp(&x->id.daddr, daddr, family)) {
847 xfrm_state_put(x);
848 x = NULL;
849 }
850 }
851
852 if (!x)
853 x = xfrm_find_acq(p->info.mode, p->info.reqid,
854 p->info.id.proto, daddr,
855 &p->info.saddr, 1,
856 family);
857 err = -ENOENT;
858 if (x == NULL)
859 goto out_noput;
860
Herbert Xu658b2192007-10-09 13:29:52 -0700861 err = xfrm_alloc_spi(x, p->min, p->max);
862 if (err)
863 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864
Herbert Xu658b2192007-10-09 13:29:52 -0700865 resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 if (IS_ERR(resp_skb)) {
867 err = PTR_ERR(resp_skb);
868 goto out;
869 }
870
Thomas Graf082a1ad2007-08-22 13:54:36 -0700871 err = nlmsg_unicast(xfrm_nl, resp_skb, NETLINK_CB(skb).pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872
873out:
874 xfrm_state_put(x);
875out_noput:
876 return err;
877}
878
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -0800879static int verify_policy_dir(u8 dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880{
881 switch (dir) {
882 case XFRM_POLICY_IN:
883 case XFRM_POLICY_OUT:
884 case XFRM_POLICY_FWD:
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 return 0;
892}
893
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -0800894static int verify_policy_type(u8 type)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -0700895{
896 switch (type) {
897 case XFRM_POLICY_TYPE_MAIN:
898#ifdef CONFIG_XFRM_SUB_POLICY
899 case XFRM_POLICY_TYPE_SUB:
900#endif
901 break;
902
903 default:
904 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700905 }
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -0700906
907 return 0;
908}
909
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910static int verify_newpolicy_info(struct xfrm_userpolicy_info *p)
911{
912 switch (p->share) {
913 case XFRM_SHARE_ANY:
914 case XFRM_SHARE_SESSION:
915 case XFRM_SHARE_USER:
916 case XFRM_SHARE_UNIQUE:
917 break;
918
919 default:
920 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700921 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922
923 switch (p->action) {
924 case XFRM_POLICY_ALLOW:
925 case XFRM_POLICY_BLOCK:
926 break;
927
928 default:
929 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700930 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931
932 switch (p->sel.family) {
933 case AF_INET:
934 break;
935
936 case AF_INET6:
937#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
938 break;
939#else
940 return -EAFNOSUPPORT;
941#endif
942
943 default:
944 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700945 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946
947 return verify_policy_dir(p->dir);
948}
949
Thomas Graf5424f322007-08-22 14:01:33 -0700950static int copy_from_user_sec_ctx(struct xfrm_policy *pol, struct nlattr **attrs)
Trent Jaegerdf718372005-12-13 23:12:27 -0800951{
Thomas Graf5424f322007-08-22 14:01:33 -0700952 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
Trent Jaegerdf718372005-12-13 23:12:27 -0800953 struct xfrm_user_sec_ctx *uctx;
954
955 if (!rt)
956 return 0;
957
Thomas Graf5424f322007-08-22 14:01:33 -0700958 uctx = nla_data(rt);
Trent Jaegerdf718372005-12-13 23:12:27 -0800959 return security_xfrm_policy_alloc(pol, uctx);
960}
961
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962static void copy_templates(struct xfrm_policy *xp, struct xfrm_user_tmpl *ut,
963 int nr)
964{
965 int i;
966
967 xp->xfrm_nr = nr;
968 for (i = 0; i < nr; i++, ut++) {
969 struct xfrm_tmpl *t = &xp->xfrm_vec[i];
970
971 memcpy(&t->id, &ut->id, sizeof(struct xfrm_id));
972 memcpy(&t->saddr, &ut->saddr,
973 sizeof(xfrm_address_t));
974 t->reqid = ut->reqid;
975 t->mode = ut->mode;
976 t->share = ut->share;
977 t->optional = ut->optional;
978 t->aalgos = ut->aalgos;
979 t->ealgos = ut->ealgos;
980 t->calgos = ut->calgos;
Miika Komu8511d012006-11-30 16:40:51 -0800981 t->encap_family = ut->family;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 }
983}
984
David S. Millerb4ad86bf2006-12-03 19:19:26 -0800985static int validate_tmpl(int nr, struct xfrm_user_tmpl *ut, u16 family)
986{
987 int i;
988
989 if (nr > XFRM_MAX_DEPTH)
990 return -EINVAL;
991
992 for (i = 0; i < nr; i++) {
993 /* We never validated the ut->family value, so many
994 * applications simply leave it at zero. The check was
995 * never made and ut->family was ignored because all
996 * templates could be assumed to have the same family as
997 * the policy itself. Now that we will have ipv4-in-ipv6
998 * and ipv6-in-ipv4 tunnels, this is no longer true.
999 */
1000 if (!ut[i].family)
1001 ut[i].family = family;
1002
1003 switch (ut[i].family) {
1004 case AF_INET:
1005 break;
1006#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1007 case AF_INET6:
1008 break;
1009#endif
1010 default:
1011 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001012 }
David S. Millerb4ad86bf2006-12-03 19:19:26 -08001013 }
1014
1015 return 0;
1016}
1017
Thomas Graf5424f322007-08-22 14:01:33 -07001018static int copy_from_user_tmpl(struct xfrm_policy *pol, struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019{
Thomas Graf5424f322007-08-22 14:01:33 -07001020 struct nlattr *rt = attrs[XFRMA_TMPL];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021
1022 if (!rt) {
1023 pol->xfrm_nr = 0;
1024 } else {
Thomas Graf5424f322007-08-22 14:01:33 -07001025 struct xfrm_user_tmpl *utmpl = nla_data(rt);
1026 int nr = nla_len(rt) / sizeof(*utmpl);
David S. Millerb4ad86bf2006-12-03 19:19:26 -08001027 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028
David S. Millerb4ad86bf2006-12-03 19:19:26 -08001029 err = validate_tmpl(nr, utmpl, pol->family);
1030 if (err)
1031 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032
Thomas Graf5424f322007-08-22 14:01:33 -07001033 copy_templates(pol, utmpl, nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 }
1035 return 0;
1036}
1037
Thomas Graf5424f322007-08-22 14:01:33 -07001038static int copy_from_user_policy_type(u8 *tp, struct nlattr **attrs)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001039{
Thomas Graf5424f322007-08-22 14:01:33 -07001040 struct nlattr *rt = attrs[XFRMA_POLICY_TYPE];
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001041 struct xfrm_userpolicy_type *upt;
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001042 u8 type = XFRM_POLICY_TYPE_MAIN;
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001043 int err;
1044
1045 if (rt) {
Thomas Graf5424f322007-08-22 14:01:33 -07001046 upt = nla_data(rt);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001047 type = upt->type;
1048 }
1049
1050 err = verify_policy_type(type);
1051 if (err)
1052 return err;
1053
1054 *tp = type;
1055 return 0;
1056}
1057
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058static void copy_from_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p)
1059{
1060 xp->priority = p->priority;
1061 xp->index = p->index;
1062 memcpy(&xp->selector, &p->sel, sizeof(xp->selector));
1063 memcpy(&xp->lft, &p->lft, sizeof(xp->lft));
1064 xp->action = p->action;
1065 xp->flags = p->flags;
1066 xp->family = p->sel.family;
1067 /* XXX xp->share = p->share; */
1068}
1069
1070static void copy_to_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p, int dir)
1071{
1072 memcpy(&p->sel, &xp->selector, sizeof(p->sel));
1073 memcpy(&p->lft, &xp->lft, sizeof(p->lft));
1074 memcpy(&p->curlft, &xp->curlft, sizeof(p->curlft));
1075 p->priority = xp->priority;
1076 p->index = xp->index;
1077 p->sel.family = xp->family;
1078 p->dir = dir;
1079 p->action = xp->action;
1080 p->flags = xp->flags;
1081 p->share = XFRM_SHARE_ANY; /* XXX xp->share */
1082}
1083
Thomas Graf5424f322007-08-22 14:01:33 -07001084static struct xfrm_policy *xfrm_policy_construct(struct xfrm_userpolicy_info *p, struct nlattr **attrs, int *errp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085{
1086 struct xfrm_policy *xp = xfrm_policy_alloc(GFP_KERNEL);
1087 int err;
1088
1089 if (!xp) {
1090 *errp = -ENOMEM;
1091 return NULL;
1092 }
1093
1094 copy_from_user_policy(xp, p);
Trent Jaegerdf718372005-12-13 23:12:27 -08001095
Thomas Graf35a7aa02007-08-22 14:00:40 -07001096 err = copy_from_user_policy_type(&xp->type, attrs);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001097 if (err)
1098 goto error;
1099
Thomas Graf35a7aa02007-08-22 14:00:40 -07001100 if (!(err = copy_from_user_tmpl(xp, attrs)))
1101 err = copy_from_user_sec_ctx(xp, attrs);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001102 if (err)
1103 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104
1105 return xp;
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001106 error:
1107 *errp = err;
WANG Cong64c31b32008-01-07 22:34:29 -08001108 xfrm_policy_destroy(xp);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001109 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110}
1111
Christoph Hellwig22e70052007-01-02 15:22:30 -08001112static int xfrm_add_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001113 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114{
Thomas Graf7b67c852007-08-22 13:53:52 -07001115 struct xfrm_userpolicy_info *p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 struct xfrm_policy *xp;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001117 struct km_event c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 int err;
1119 int excl;
1120
1121 err = verify_newpolicy_info(p);
1122 if (err)
1123 return err;
Thomas Graf35a7aa02007-08-22 14:00:40 -07001124 err = verify_sec_ctx_len(attrs);
Trent Jaegerdf718372005-12-13 23:12:27 -08001125 if (err)
1126 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127
Thomas Graf35a7aa02007-08-22 14:00:40 -07001128 xp = xfrm_policy_construct(p, attrs, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 if (!xp)
1130 return err;
1131
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001132 /* shouldnt excl be based on nlh flags??
1133 * Aha! this is anti-netlink really i.e more pfkey derived
1134 * in netlink excl is a flag and you wouldnt need
1135 * a type XFRM_MSG_UPDPOLICY - JHS */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136 excl = nlh->nlmsg_type == XFRM_MSG_NEWPOLICY;
1137 err = xfrm_policy_insert(p->dir, xp, excl);
Joy Lattenab5f5e82007-09-17 11:51:22 -07001138 xfrm_audit_policy_add(xp, err ? 0 : 1, NETLINK_CB(skb).loginuid,
1139 NETLINK_CB(skb).sid);
Joy Latten161a09e2006-11-27 13:11:54 -06001140
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141 if (err) {
Trent Jaeger5f8ac642006-01-06 13:22:39 -08001142 security_xfrm_policy_free(xp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 kfree(xp);
1144 return err;
1145 }
1146
Herbert Xuf60f6b82005-06-18 22:44:37 -07001147 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001148 c.seq = nlh->nlmsg_seq;
1149 c.pid = nlh->nlmsg_pid;
1150 km_policy_notify(xp, p->dir, &c);
1151
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 xfrm_pol_put(xp);
1153
1154 return 0;
1155}
1156
1157static int copy_to_user_tmpl(struct xfrm_policy *xp, struct sk_buff *skb)
1158{
1159 struct xfrm_user_tmpl vec[XFRM_MAX_DEPTH];
1160 int i;
1161
1162 if (xp->xfrm_nr == 0)
1163 return 0;
1164
1165 for (i = 0; i < xp->xfrm_nr; i++) {
1166 struct xfrm_user_tmpl *up = &vec[i];
1167 struct xfrm_tmpl *kp = &xp->xfrm_vec[i];
1168
1169 memcpy(&up->id, &kp->id, sizeof(up->id));
Miika Komu8511d012006-11-30 16:40:51 -08001170 up->family = kp->encap_family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 memcpy(&up->saddr, &kp->saddr, sizeof(up->saddr));
1172 up->reqid = kp->reqid;
1173 up->mode = kp->mode;
1174 up->share = kp->share;
1175 up->optional = kp->optional;
1176 up->aalgos = kp->aalgos;
1177 up->ealgos = kp->ealgos;
1178 up->calgos = kp->calgos;
1179 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180
Thomas Grafc0144be2007-08-22 13:55:43 -07001181 return nla_put(skb, XFRMA_TMPL,
1182 sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr, vec);
Trent Jaegerdf718372005-12-13 23:12:27 -08001183}
1184
Serge Hallyn0d681622006-07-24 23:30:44 -07001185static inline int copy_to_user_state_sec_ctx(struct xfrm_state *x, struct sk_buff *skb)
1186{
1187 if (x->security) {
1188 return copy_sec_ctx(x->security, skb);
1189 }
1190 return 0;
1191}
1192
1193static inline int copy_to_user_sec_ctx(struct xfrm_policy *xp, struct sk_buff *skb)
1194{
1195 if (xp->security) {
1196 return copy_sec_ctx(xp->security, skb);
1197 }
1198 return 0;
1199}
Thomas Grafcfbfd452007-08-22 13:57:04 -07001200static inline size_t userpolicy_type_attrsize(void)
1201{
1202#ifdef CONFIG_XFRM_SUB_POLICY
1203 return nla_total_size(sizeof(struct xfrm_userpolicy_type));
1204#else
1205 return 0;
1206#endif
1207}
Serge Hallyn0d681622006-07-24 23:30:44 -07001208
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001209#ifdef CONFIG_XFRM_SUB_POLICY
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001210static int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001211{
Thomas Grafc0144be2007-08-22 13:55:43 -07001212 struct xfrm_userpolicy_type upt = {
1213 .type = type,
1214 };
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001215
Thomas Grafc0144be2007-08-22 13:55:43 -07001216 return nla_put(skb, XFRMA_POLICY_TYPE, sizeof(upt), &upt);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001217}
1218
1219#else
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001220static inline int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001221{
1222 return 0;
1223}
1224#endif
1225
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226static int dump_one_policy(struct xfrm_policy *xp, int dir, int count, void *ptr)
1227{
1228 struct xfrm_dump_info *sp = ptr;
1229 struct xfrm_userpolicy_info *p;
1230 struct sk_buff *in_skb = sp->in_skb;
1231 struct sk_buff *skb = sp->out_skb;
1232 struct nlmsghdr *nlh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233
1234 if (sp->this_idx < sp->start_idx)
1235 goto out;
1236
Thomas Graf79b8b7f2007-08-22 12:46:53 -07001237 nlh = nlmsg_put(skb, NETLINK_CB(in_skb).pid, sp->nlmsg_seq,
1238 XFRM_MSG_NEWPOLICY, sizeof(*p), sp->nlmsg_flags);
1239 if (nlh == NULL)
1240 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241
Thomas Graf7b67c852007-08-22 13:53:52 -07001242 p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 copy_to_user_policy(xp, p, dir);
1244 if (copy_to_user_tmpl(xp, skb) < 0)
1245 goto nlmsg_failure;
Trent Jaegerdf718372005-12-13 23:12:27 -08001246 if (copy_to_user_sec_ctx(xp, skb))
1247 goto nlmsg_failure;
Jamal Hadi Salim1459bb32006-11-20 16:51:22 -08001248 if (copy_to_user_policy_type(xp->type, skb) < 0)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001249 goto nlmsg_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250
Thomas Graf98250692007-08-22 12:47:26 -07001251 nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252out:
1253 sp->this_idx++;
1254 return 0;
1255
1256nlmsg_failure:
Thomas Graf98250692007-08-22 12:47:26 -07001257 nlmsg_cancel(skb, nlh);
1258 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259}
1260
1261static int xfrm_dump_policy(struct sk_buff *skb, struct netlink_callback *cb)
1262{
1263 struct xfrm_dump_info info;
1264
1265 info.in_skb = cb->skb;
1266 info.out_skb = skb;
1267 info.nlmsg_seq = cb->nlh->nlmsg_seq;
1268 info.nlmsg_flags = NLM_F_MULTI;
1269 info.this_idx = 0;
1270 info.start_idx = cb->args[0];
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001271 (void) xfrm_policy_walk(XFRM_POLICY_TYPE_MAIN, dump_one_policy, &info);
1272#ifdef CONFIG_XFRM_SUB_POLICY
1273 (void) xfrm_policy_walk(XFRM_POLICY_TYPE_SUB, dump_one_policy, &info);
1274#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 cb->args[0] = info.this_idx;
1276
1277 return skb->len;
1278}
1279
1280static struct sk_buff *xfrm_policy_netlink(struct sk_buff *in_skb,
1281 struct xfrm_policy *xp,
1282 int dir, u32 seq)
1283{
1284 struct xfrm_dump_info info;
1285 struct sk_buff *skb;
1286
Thomas Graf7deb2262007-08-22 13:57:39 -07001287 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288 if (!skb)
1289 return ERR_PTR(-ENOMEM);
1290
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 info.in_skb = in_skb;
1292 info.out_skb = skb;
1293 info.nlmsg_seq = seq;
1294 info.nlmsg_flags = 0;
1295 info.this_idx = info.start_idx = 0;
1296
1297 if (dump_one_policy(xp, dir, 0, &info) < 0) {
1298 kfree_skb(skb);
1299 return NULL;
1300 }
1301
1302 return skb;
1303}
1304
Christoph Hellwig22e70052007-01-02 15:22:30 -08001305static int xfrm_get_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001306 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307{
1308 struct xfrm_policy *xp;
1309 struct xfrm_userpolicy_id *p;
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001310 u8 type = XFRM_POLICY_TYPE_MAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311 int err;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001312 struct km_event c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313 int delete;
1314
Thomas Graf7b67c852007-08-22 13:53:52 -07001315 p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 delete = nlh->nlmsg_type == XFRM_MSG_DELPOLICY;
1317
Thomas Graf35a7aa02007-08-22 14:00:40 -07001318 err = copy_from_user_policy_type(&type, attrs);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001319 if (err)
1320 return err;
1321
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322 err = verify_policy_dir(p->dir);
1323 if (err)
1324 return err;
1325
1326 if (p->index)
Eric Parisef41aaa2007-03-07 15:37:58 -08001327 xp = xfrm_policy_byid(type, p->dir, p->index, delete, &err);
Trent Jaegerdf718372005-12-13 23:12:27 -08001328 else {
Thomas Graf5424f322007-08-22 14:01:33 -07001329 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
Trent Jaegerdf718372005-12-13 23:12:27 -08001330 struct xfrm_policy tmp;
1331
Thomas Graf35a7aa02007-08-22 14:00:40 -07001332 err = verify_sec_ctx_len(attrs);
Trent Jaegerdf718372005-12-13 23:12:27 -08001333 if (err)
1334 return err;
1335
1336 memset(&tmp, 0, sizeof(struct xfrm_policy));
1337 if (rt) {
Thomas Graf5424f322007-08-22 14:01:33 -07001338 struct xfrm_user_sec_ctx *uctx = nla_data(rt);
Trent Jaegerdf718372005-12-13 23:12:27 -08001339
1340 if ((err = security_xfrm_policy_alloc(&tmp, uctx)))
1341 return err;
1342 }
Eric Parisef41aaa2007-03-07 15:37:58 -08001343 xp = xfrm_policy_bysel_ctx(type, p->dir, &p->sel, tmp.security,
1344 delete, &err);
Trent Jaegerdf718372005-12-13 23:12:27 -08001345 security_xfrm_policy_free(&tmp);
1346 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 if (xp == NULL)
1348 return -ENOENT;
1349
1350 if (!delete) {
1351 struct sk_buff *resp_skb;
1352
1353 resp_skb = xfrm_policy_netlink(skb, xp, p->dir, nlh->nlmsg_seq);
1354 if (IS_ERR(resp_skb)) {
1355 err = PTR_ERR(resp_skb);
1356 } else {
Thomas Graf082a1ad2007-08-22 13:54:36 -07001357 err = nlmsg_unicast(xfrm_nl, resp_skb,
1358 NETLINK_CB(skb).pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359 }
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001360 } else {
Joy Lattenab5f5e82007-09-17 11:51:22 -07001361 xfrm_audit_policy_delete(xp, err ? 0 : 1,
1362 NETLINK_CB(skb).loginuid,
1363 NETLINK_CB(skb).sid);
David S. Miller13fcfbb02007-02-12 13:53:54 -08001364
1365 if (err != 0)
Catherine Zhangc8c05a82006-06-08 23:39:49 -07001366 goto out;
David S. Miller13fcfbb02007-02-12 13:53:54 -08001367
Herbert Xue7443892005-06-18 22:44:18 -07001368 c.data.byid = p->index;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001369 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001370 c.seq = nlh->nlmsg_seq;
1371 c.pid = nlh->nlmsg_pid;
1372 km_policy_notify(xp, p->dir, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373 }
1374
Catherine Zhangc8c05a82006-06-08 23:39:49 -07001375out:
Eric Parisef41aaa2007-03-07 15:37:58 -08001376 xfrm_pol_put(xp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377 return err;
1378}
1379
Christoph Hellwig22e70052007-01-02 15:22:30 -08001380static int xfrm_flush_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001381 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382{
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001383 struct km_event c;
Thomas Graf7b67c852007-08-22 13:53:52 -07001384 struct xfrm_usersa_flush *p = nlmsg_data(nlh);
Joy Latten161a09e2006-11-27 13:11:54 -06001385 struct xfrm_audit audit_info;
Joy Latten4aa2e622007-06-04 19:05:57 -04001386 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387
Joy Latten161a09e2006-11-27 13:11:54 -06001388 audit_info.loginuid = NETLINK_CB(skb).loginuid;
1389 audit_info.secid = NETLINK_CB(skb).sid;
Joy Latten4aa2e622007-06-04 19:05:57 -04001390 err = xfrm_state_flush(p->proto, &audit_info);
1391 if (err)
1392 return err;
Herbert Xubf088672005-06-18 22:44:00 -07001393 c.data.proto = p->proto;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001394 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001395 c.seq = nlh->nlmsg_seq;
1396 c.pid = nlh->nlmsg_pid;
1397 km_state_notify(NULL, &c);
1398
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399 return 0;
1400}
1401
Thomas Graf7deb2262007-08-22 13:57:39 -07001402static inline size_t xfrm_aevent_msgsize(void)
1403{
1404 return NLMSG_ALIGN(sizeof(struct xfrm_aevent_id))
1405 + nla_total_size(sizeof(struct xfrm_replay_state))
1406 + nla_total_size(sizeof(struct xfrm_lifetime_cur))
1407 + nla_total_size(4) /* XFRM_AE_RTHR */
1408 + nla_total_size(4); /* XFRM_AE_ETHR */
1409}
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001410
1411static int build_aevent(struct sk_buff *skb, struct xfrm_state *x, struct km_event *c)
1412{
1413 struct xfrm_aevent_id *id;
1414 struct nlmsghdr *nlh;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001415
Thomas Graf79b8b7f2007-08-22 12:46:53 -07001416 nlh = nlmsg_put(skb, c->pid, c->seq, XFRM_MSG_NEWAE, sizeof(*id), 0);
1417 if (nlh == NULL)
1418 return -EMSGSIZE;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001419
Thomas Graf7b67c852007-08-22 13:53:52 -07001420 id = nlmsg_data(nlh);
Jamal Hadi Salim2b5f6dc2006-12-02 22:22:25 -08001421 memcpy(&id->sa_id.daddr, &x->id.daddr,sizeof(x->id.daddr));
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001422 id->sa_id.spi = x->id.spi;
1423 id->sa_id.family = x->props.family;
1424 id->sa_id.proto = x->id.proto;
Jamal Hadi Salim2b5f6dc2006-12-02 22:22:25 -08001425 memcpy(&id->saddr, &x->props.saddr,sizeof(x->props.saddr));
1426 id->reqid = x->props.reqid;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001427 id->flags = c->data.aevent;
1428
Thomas Grafc0144be2007-08-22 13:55:43 -07001429 NLA_PUT(skb, XFRMA_REPLAY_VAL, sizeof(x->replay), &x->replay);
1430 NLA_PUT(skb, XFRMA_LTIME_VAL, sizeof(x->curlft), &x->curlft);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001431
Thomas Grafc0144be2007-08-22 13:55:43 -07001432 if (id->flags & XFRM_AE_RTHR)
1433 NLA_PUT_U32(skb, XFRMA_REPLAY_THRESH, x->replay_maxdiff);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001434
Thomas Grafc0144be2007-08-22 13:55:43 -07001435 if (id->flags & XFRM_AE_ETHR)
1436 NLA_PUT_U32(skb, XFRMA_ETIMER_THRESH,
1437 x->replay_maxage * 10 / HZ);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001438
Thomas Graf98250692007-08-22 12:47:26 -07001439 return nlmsg_end(skb, nlh);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001440
Thomas Grafc0144be2007-08-22 13:55:43 -07001441nla_put_failure:
Thomas Graf98250692007-08-22 12:47:26 -07001442 nlmsg_cancel(skb, nlh);
1443 return -EMSGSIZE;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001444}
1445
Christoph Hellwig22e70052007-01-02 15:22:30 -08001446static int xfrm_get_ae(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001447 struct nlattr **attrs)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001448{
1449 struct xfrm_state *x;
1450 struct sk_buff *r_skb;
1451 int err;
1452 struct km_event c;
Thomas Graf7b67c852007-08-22 13:53:52 -07001453 struct xfrm_aevent_id *p = nlmsg_data(nlh);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001454 struct xfrm_usersa_id *id = &p->sa_id;
1455
Thomas Graf7deb2262007-08-22 13:57:39 -07001456 r_skb = nlmsg_new(xfrm_aevent_msgsize(), GFP_ATOMIC);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001457 if (r_skb == NULL)
1458 return -ENOMEM;
1459
1460 x = xfrm_state_lookup(&id->daddr, id->spi, id->proto, id->family);
1461 if (x == NULL) {
Patrick McHardyb08d5842007-02-27 09:57:37 -08001462 kfree_skb(r_skb);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001463 return -ESRCH;
1464 }
1465
1466 /*
1467 * XXX: is this lock really needed - none of the other
1468 * gets lock (the concern is things getting updated
1469 * while we are still reading) - jhs
1470 */
1471 spin_lock_bh(&x->lock);
1472 c.data.aevent = p->flags;
1473 c.seq = nlh->nlmsg_seq;
1474 c.pid = nlh->nlmsg_pid;
1475
1476 if (build_aevent(r_skb, x, &c) < 0)
1477 BUG();
Thomas Graf082a1ad2007-08-22 13:54:36 -07001478 err = nlmsg_unicast(xfrm_nl, r_skb, NETLINK_CB(skb).pid);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001479 spin_unlock_bh(&x->lock);
1480 xfrm_state_put(x);
1481 return err;
1482}
1483
Christoph Hellwig22e70052007-01-02 15:22:30 -08001484static int xfrm_new_ae(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001485 struct nlattr **attrs)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001486{
1487 struct xfrm_state *x;
1488 struct km_event c;
1489 int err = - EINVAL;
Thomas Graf7b67c852007-08-22 13:53:52 -07001490 struct xfrm_aevent_id *p = nlmsg_data(nlh);
Thomas Graf5424f322007-08-22 14:01:33 -07001491 struct nlattr *rp = attrs[XFRMA_REPLAY_VAL];
1492 struct nlattr *lt = attrs[XFRMA_LTIME_VAL];
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001493
1494 if (!lt && !rp)
1495 return err;
1496
1497 /* pedantic mode - thou shalt sayeth replaceth */
1498 if (!(nlh->nlmsg_flags&NLM_F_REPLACE))
1499 return err;
1500
1501 x = xfrm_state_lookup(&p->sa_id.daddr, p->sa_id.spi, p->sa_id.proto, p->sa_id.family);
1502 if (x == NULL)
1503 return -ESRCH;
1504
1505 if (x->km.state != XFRM_STATE_VALID)
1506 goto out;
1507
1508 spin_lock_bh(&x->lock);
Thomas Graf35a7aa02007-08-22 14:00:40 -07001509 xfrm_update_ae_params(x, attrs);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001510 spin_unlock_bh(&x->lock);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001511
1512 c.event = nlh->nlmsg_type;
1513 c.seq = nlh->nlmsg_seq;
1514 c.pid = nlh->nlmsg_pid;
1515 c.data.aevent = XFRM_AE_CU;
1516 km_state_notify(x, &c);
1517 err = 0;
1518out:
1519 xfrm_state_put(x);
1520 return err;
1521}
1522
Christoph Hellwig22e70052007-01-02 15:22:30 -08001523static int xfrm_flush_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001524 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525{
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001526 struct km_event c;
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001527 u8 type = XFRM_POLICY_TYPE_MAIN;
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001528 int err;
Joy Latten161a09e2006-11-27 13:11:54 -06001529 struct xfrm_audit audit_info;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001530
Thomas Graf35a7aa02007-08-22 14:00:40 -07001531 err = copy_from_user_policy_type(&type, attrs);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001532 if (err)
1533 return err;
1534
Joy Latten161a09e2006-11-27 13:11:54 -06001535 audit_info.loginuid = NETLINK_CB(skb).loginuid;
1536 audit_info.secid = NETLINK_CB(skb).sid;
Joy Latten4aa2e622007-06-04 19:05:57 -04001537 err = xfrm_policy_flush(type, &audit_info);
1538 if (err)
1539 return err;
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001540 c.data.type = type;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001541 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001542 c.seq = nlh->nlmsg_seq;
1543 c.pid = nlh->nlmsg_pid;
1544 km_policy_notify(NULL, 0, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545 return 0;
1546}
1547
Christoph Hellwig22e70052007-01-02 15:22:30 -08001548static int xfrm_add_pol_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001549 struct nlattr **attrs)
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001550{
1551 struct xfrm_policy *xp;
Thomas Graf7b67c852007-08-22 13:53:52 -07001552 struct xfrm_user_polexpire *up = nlmsg_data(nlh);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001553 struct xfrm_userpolicy_info *p = &up->pol;
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001554 u8 type = XFRM_POLICY_TYPE_MAIN;
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001555 int err = -ENOENT;
1556
Thomas Graf35a7aa02007-08-22 14:00:40 -07001557 err = copy_from_user_policy_type(&type, attrs);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001558 if (err)
1559 return err;
1560
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001561 if (p->index)
Eric Parisef41aaa2007-03-07 15:37:58 -08001562 xp = xfrm_policy_byid(type, p->dir, p->index, 0, &err);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001563 else {
Thomas Graf5424f322007-08-22 14:01:33 -07001564 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001565 struct xfrm_policy tmp;
1566
Thomas Graf35a7aa02007-08-22 14:00:40 -07001567 err = verify_sec_ctx_len(attrs);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001568 if (err)
1569 return err;
1570
1571 memset(&tmp, 0, sizeof(struct xfrm_policy));
1572 if (rt) {
Thomas Graf5424f322007-08-22 14:01:33 -07001573 struct xfrm_user_sec_ctx *uctx = nla_data(rt);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001574
1575 if ((err = security_xfrm_policy_alloc(&tmp, uctx)))
1576 return err;
1577 }
Eric Parisef41aaa2007-03-07 15:37:58 -08001578 xp = xfrm_policy_bysel_ctx(type, p->dir, &p->sel, tmp.security,
1579 0, &err);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001580 security_xfrm_policy_free(&tmp);
1581 }
1582
1583 if (xp == NULL)
Eric Parisef41aaa2007-03-07 15:37:58 -08001584 return -ENOENT;
1585 read_lock(&xp->lock);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001586 if (xp->dead) {
1587 read_unlock(&xp->lock);
1588 goto out;
1589 }
1590
1591 read_unlock(&xp->lock);
1592 err = 0;
1593 if (up->hard) {
1594 xfrm_policy_delete(xp, p->dir);
Joy Lattenab5f5e82007-09-17 11:51:22 -07001595 xfrm_audit_policy_delete(xp, 1, NETLINK_CB(skb).loginuid,
1596 NETLINK_CB(skb).sid);
Joy Latten161a09e2006-11-27 13:11:54 -06001597
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001598 } else {
1599 // reset the timers here?
1600 printk("Dont know what to do with soft policy expire\n");
1601 }
1602 km_policy_expired(xp, p->dir, up->hard, current->pid);
1603
1604out:
1605 xfrm_pol_put(xp);
1606 return err;
1607}
1608
Christoph Hellwig22e70052007-01-02 15:22:30 -08001609static int xfrm_add_sa_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001610 struct nlattr **attrs)
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001611{
1612 struct xfrm_state *x;
1613 int err;
Thomas Graf7b67c852007-08-22 13:53:52 -07001614 struct xfrm_user_expire *ue = nlmsg_data(nlh);
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001615 struct xfrm_usersa_info *p = &ue->state;
1616
1617 x = xfrm_state_lookup(&p->id.daddr, p->id.spi, p->id.proto, p->family);
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001618
David S. Miller3a765aa2007-02-26 14:52:21 -08001619 err = -ENOENT;
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001620 if (x == NULL)
1621 return err;
1622
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001623 spin_lock_bh(&x->lock);
David S. Miller3a765aa2007-02-26 14:52:21 -08001624 err = -EINVAL;
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001625 if (x->km.state != XFRM_STATE_VALID)
1626 goto out;
1627 km_state_expired(x, ue->hard, current->pid);
1628
Joy Latten161a09e2006-11-27 13:11:54 -06001629 if (ue->hard) {
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001630 __xfrm_state_delete(x);
Joy Lattenab5f5e82007-09-17 11:51:22 -07001631 xfrm_audit_state_delete(x, 1, NETLINK_CB(skb).loginuid,
1632 NETLINK_CB(skb).sid);
Joy Latten161a09e2006-11-27 13:11:54 -06001633 }
David S. Miller3a765aa2007-02-26 14:52:21 -08001634 err = 0;
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001635out:
1636 spin_unlock_bh(&x->lock);
1637 xfrm_state_put(x);
1638 return err;
1639}
1640
Christoph Hellwig22e70052007-01-02 15:22:30 -08001641static int xfrm_add_acquire(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001642 struct nlattr **attrs)
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001643{
1644 struct xfrm_policy *xp;
1645 struct xfrm_user_tmpl *ut;
1646 int i;
Thomas Graf5424f322007-08-22 14:01:33 -07001647 struct nlattr *rt = attrs[XFRMA_TMPL];
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001648
Thomas Graf7b67c852007-08-22 13:53:52 -07001649 struct xfrm_user_acquire *ua = nlmsg_data(nlh);
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001650 struct xfrm_state *x = xfrm_state_alloc();
1651 int err = -ENOMEM;
1652
1653 if (!x)
1654 return err;
1655
1656 err = verify_newpolicy_info(&ua->policy);
1657 if (err) {
1658 printk("BAD policy passed\n");
1659 kfree(x);
1660 return err;
1661 }
1662
1663 /* build an XP */
Thomas Graf5424f322007-08-22 14:01:33 -07001664 xp = xfrm_policy_construct(&ua->policy, attrs, &err);
David S. Millerb4ad86bf2006-12-03 19:19:26 -08001665 if (!xp) {
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001666 kfree(x);
1667 return err;
1668 }
1669
1670 memcpy(&x->id, &ua->id, sizeof(ua->id));
1671 memcpy(&x->props.saddr, &ua->saddr, sizeof(ua->saddr));
1672 memcpy(&x->sel, &ua->sel, sizeof(ua->sel));
1673
Thomas Graf5424f322007-08-22 14:01:33 -07001674 ut = nla_data(rt);
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001675 /* extract the templates and for each call km_key */
1676 for (i = 0; i < xp->xfrm_nr; i++, ut++) {
1677 struct xfrm_tmpl *t = &xp->xfrm_vec[i];
1678 memcpy(&x->id, &t->id, sizeof(x->id));
1679 x->props.mode = t->mode;
1680 x->props.reqid = t->reqid;
1681 x->props.family = ut->family;
1682 t->aalgos = ua->aalgos;
1683 t->ealgos = ua->ealgos;
1684 t->calgos = ua->calgos;
1685 err = km_query(x, t, xp);
1686
1687 }
1688
1689 kfree(x);
1690 kfree(xp);
1691
1692 return 0;
1693}
1694
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001695#ifdef CONFIG_XFRM_MIGRATE
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001696static int copy_from_user_migrate(struct xfrm_migrate *ma,
Thomas Graf5424f322007-08-22 14:01:33 -07001697 struct nlattr **attrs, int *num)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001698{
Thomas Graf5424f322007-08-22 14:01:33 -07001699 struct nlattr *rt = attrs[XFRMA_MIGRATE];
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001700 struct xfrm_user_migrate *um;
1701 int i, num_migrate;
1702
Thomas Graf5424f322007-08-22 14:01:33 -07001703 um = nla_data(rt);
1704 num_migrate = nla_len(rt) / sizeof(*um);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001705
1706 if (num_migrate <= 0 || num_migrate > XFRM_MAX_DEPTH)
1707 return -EINVAL;
1708
1709 for (i = 0; i < num_migrate; i++, um++, ma++) {
1710 memcpy(&ma->old_daddr, &um->old_daddr, sizeof(ma->old_daddr));
1711 memcpy(&ma->old_saddr, &um->old_saddr, sizeof(ma->old_saddr));
1712 memcpy(&ma->new_daddr, &um->new_daddr, sizeof(ma->new_daddr));
1713 memcpy(&ma->new_saddr, &um->new_saddr, sizeof(ma->new_saddr));
1714
1715 ma->proto = um->proto;
1716 ma->mode = um->mode;
1717 ma->reqid = um->reqid;
1718
1719 ma->old_family = um->old_family;
1720 ma->new_family = um->new_family;
1721 }
1722
1723 *num = i;
1724 return 0;
1725}
1726
1727static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001728 struct nlattr **attrs)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001729{
Thomas Graf7b67c852007-08-22 13:53:52 -07001730 struct xfrm_userpolicy_id *pi = nlmsg_data(nlh);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001731 struct xfrm_migrate m[XFRM_MAX_DEPTH];
1732 u8 type;
1733 int err;
1734 int n = 0;
1735
Thomas Graf35a7aa02007-08-22 14:00:40 -07001736 if (attrs[XFRMA_MIGRATE] == NULL)
Thomas Grafcf5cb792007-08-22 13:59:04 -07001737 return -EINVAL;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001738
Thomas Graf5424f322007-08-22 14:01:33 -07001739 err = copy_from_user_policy_type(&type, attrs);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001740 if (err)
1741 return err;
1742
1743 err = copy_from_user_migrate((struct xfrm_migrate *)m,
Thomas Graf5424f322007-08-22 14:01:33 -07001744 attrs, &n);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001745 if (err)
1746 return err;
1747
1748 if (!n)
1749 return 0;
1750
1751 xfrm_migrate(&pi->sel, pi->dir, type, m, n);
1752
1753 return 0;
1754}
1755#else
1756static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001757 struct nlattr **attrs)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001758{
1759 return -ENOPROTOOPT;
1760}
1761#endif
1762
1763#ifdef CONFIG_XFRM_MIGRATE
1764static int copy_to_user_migrate(struct xfrm_migrate *m, struct sk_buff *skb)
1765{
1766 struct xfrm_user_migrate um;
1767
1768 memset(&um, 0, sizeof(um));
1769 um.proto = m->proto;
1770 um.mode = m->mode;
1771 um.reqid = m->reqid;
1772 um.old_family = m->old_family;
1773 memcpy(&um.old_daddr, &m->old_daddr, sizeof(um.old_daddr));
1774 memcpy(&um.old_saddr, &m->old_saddr, sizeof(um.old_saddr));
1775 um.new_family = m->new_family;
1776 memcpy(&um.new_daddr, &m->new_daddr, sizeof(um.new_daddr));
1777 memcpy(&um.new_saddr, &m->new_saddr, sizeof(um.new_saddr));
1778
Thomas Grafc0144be2007-08-22 13:55:43 -07001779 return nla_put(skb, XFRMA_MIGRATE, sizeof(um), &um);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001780}
1781
Thomas Graf7deb2262007-08-22 13:57:39 -07001782static inline size_t xfrm_migrate_msgsize(int num_migrate)
1783{
1784 return NLMSG_ALIGN(sizeof(struct xfrm_userpolicy_id))
1785 + nla_total_size(sizeof(struct xfrm_user_migrate) * num_migrate)
1786 + userpolicy_type_attrsize();
1787}
1788
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001789static int build_migrate(struct sk_buff *skb, struct xfrm_migrate *m,
1790 int num_migrate, struct xfrm_selector *sel,
1791 u8 dir, u8 type)
1792{
1793 struct xfrm_migrate *mp;
1794 struct xfrm_userpolicy_id *pol_id;
1795 struct nlmsghdr *nlh;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001796 int i;
1797
Thomas Graf79b8b7f2007-08-22 12:46:53 -07001798 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_MIGRATE, sizeof(*pol_id), 0);
1799 if (nlh == NULL)
1800 return -EMSGSIZE;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001801
Thomas Graf7b67c852007-08-22 13:53:52 -07001802 pol_id = nlmsg_data(nlh);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001803 /* copy data from selector, dir, and type to the pol_id */
1804 memset(pol_id, 0, sizeof(*pol_id));
1805 memcpy(&pol_id->sel, sel, sizeof(pol_id->sel));
1806 pol_id->dir = dir;
1807
1808 if (copy_to_user_policy_type(type, skb) < 0)
1809 goto nlmsg_failure;
1810
1811 for (i = 0, mp = m ; i < num_migrate; i++, mp++) {
1812 if (copy_to_user_migrate(mp, skb) < 0)
1813 goto nlmsg_failure;
1814 }
1815
Thomas Graf98250692007-08-22 12:47:26 -07001816 return nlmsg_end(skb, nlh);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001817nlmsg_failure:
Thomas Graf98250692007-08-22 12:47:26 -07001818 nlmsg_cancel(skb, nlh);
1819 return -EMSGSIZE;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001820}
1821
1822static int xfrm_send_migrate(struct xfrm_selector *sel, u8 dir, u8 type,
1823 struct xfrm_migrate *m, int num_migrate)
1824{
1825 struct sk_buff *skb;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001826
Thomas Graf7deb2262007-08-22 13:57:39 -07001827 skb = nlmsg_new(xfrm_migrate_msgsize(num_migrate), GFP_ATOMIC);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001828 if (skb == NULL)
1829 return -ENOMEM;
1830
1831 /* build migrate */
1832 if (build_migrate(skb, m, num_migrate, sel, dir, type) < 0)
1833 BUG();
1834
Thomas Graf082a1ad2007-08-22 13:54:36 -07001835 return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_MIGRATE, GFP_ATOMIC);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001836}
1837#else
1838static int xfrm_send_migrate(struct xfrm_selector *sel, u8 dir, u8 type,
1839 struct xfrm_migrate *m, int num_migrate)
1840{
1841 return -ENOPROTOOPT;
1842}
1843#endif
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001844
Thomas Grafa7bd9a42007-08-22 13:58:18 -07001845#define XMSGSIZE(type) sizeof(struct type)
Thomas Graf492b5582005-05-03 14:26:40 -07001846
1847static const int xfrm_msg_min[XFRM_NR_MSGTYPES] = {
1848 [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
1849 [XFRM_MSG_DELSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
1850 [XFRM_MSG_GETSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
1851 [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
1852 [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
1853 [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
1854 [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userspi_info),
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001855 [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_acquire),
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001856 [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_expire),
Thomas Graf492b5582005-05-03 14:26:40 -07001857 [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
1858 [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001859 [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_polexpire),
Thomas Graf492b5582005-05-03 14:26:40 -07001860 [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_flush),
Thomas Grafa7bd9a42007-08-22 13:58:18 -07001861 [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = 0,
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001862 [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
1863 [XFRM_MSG_GETAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07001864 [XFRM_MSG_REPORT - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_report),
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001865 [XFRM_MSG_MIGRATE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
Thomas Grafa7bd9a42007-08-22 13:58:18 -07001866 [XFRM_MSG_GETSADINFO - XFRM_MSG_BASE] = sizeof(u32),
1867 [XFRM_MSG_GETSPDINFO - XFRM_MSG_BASE] = sizeof(u32),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868};
1869
Thomas Graf492b5582005-05-03 14:26:40 -07001870#undef XMSGSIZE
1871
Thomas Grafcf5cb792007-08-22 13:59:04 -07001872static const struct nla_policy xfrma_policy[XFRMA_MAX+1] = {
Herbert Xu1a6509d2008-01-28 19:37:29 -08001873 [XFRMA_ALG_AEAD] = { .len = sizeof(struct xfrm_algo_aead) },
Thomas Grafcf5cb792007-08-22 13:59:04 -07001874 [XFRMA_ALG_AUTH] = { .len = sizeof(struct xfrm_algo) },
1875 [XFRMA_ALG_CRYPT] = { .len = sizeof(struct xfrm_algo) },
1876 [XFRMA_ALG_COMP] = { .len = sizeof(struct xfrm_algo) },
1877 [XFRMA_ENCAP] = { .len = sizeof(struct xfrm_encap_tmpl) },
1878 [XFRMA_TMPL] = { .len = sizeof(struct xfrm_user_tmpl) },
1879 [XFRMA_SEC_CTX] = { .len = sizeof(struct xfrm_sec_ctx) },
1880 [XFRMA_LTIME_VAL] = { .len = sizeof(struct xfrm_lifetime_cur) },
1881 [XFRMA_REPLAY_VAL] = { .len = sizeof(struct xfrm_replay_state) },
1882 [XFRMA_REPLAY_THRESH] = { .type = NLA_U32 },
1883 [XFRMA_ETIMER_THRESH] = { .type = NLA_U32 },
1884 [XFRMA_SRCADDR] = { .len = sizeof(xfrm_address_t) },
1885 [XFRMA_COADDR] = { .len = sizeof(xfrm_address_t) },
1886 [XFRMA_POLICY_TYPE] = { .len = sizeof(struct xfrm_userpolicy_type)},
1887 [XFRMA_MIGRATE] = { .len = sizeof(struct xfrm_user_migrate) },
1888};
1889
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890static struct xfrm_link {
Thomas Graf5424f322007-08-22 14:01:33 -07001891 int (*doit)(struct sk_buff *, struct nlmsghdr *, struct nlattr **);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892 int (*dump)(struct sk_buff *, struct netlink_callback *);
Thomas Graf492b5582005-05-03 14:26:40 -07001893} xfrm_dispatch[XFRM_NR_MSGTYPES] = {
1894 [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa },
1895 [XFRM_MSG_DELSA - XFRM_MSG_BASE] = { .doit = xfrm_del_sa },
1896 [XFRM_MSG_GETSA - XFRM_MSG_BASE] = { .doit = xfrm_get_sa,
1897 .dump = xfrm_dump_sa },
1898 [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy },
1899 [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy },
1900 [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy,
1901 .dump = xfrm_dump_policy },
1902 [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = { .doit = xfrm_alloc_userspi },
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001903 [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_acquire },
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001904 [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_sa_expire },
Thomas Graf492b5582005-05-03 14:26:40 -07001905 [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy },
1906 [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa },
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001907 [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_pol_expire},
Thomas Graf492b5582005-05-03 14:26:40 -07001908 [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = { .doit = xfrm_flush_sa },
1909 [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_flush_policy },
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001910 [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = { .doit = xfrm_new_ae },
1911 [XFRM_MSG_GETAE - XFRM_MSG_BASE] = { .doit = xfrm_get_ae },
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001912 [XFRM_MSG_MIGRATE - XFRM_MSG_BASE] = { .doit = xfrm_do_migrate },
Jamal Hadi Salim566ec032007-04-26 14:12:15 -07001913 [XFRM_MSG_GETSADINFO - XFRM_MSG_BASE] = { .doit = xfrm_get_sadinfo },
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -07001914 [XFRM_MSG_GETSPDINFO - XFRM_MSG_BASE] = { .doit = xfrm_get_spdinfo },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915};
1916
Thomas Graf1d00a4e2007-03-22 23:30:12 -07001917static int xfrm_user_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918{
Thomas Graf35a7aa02007-08-22 14:00:40 -07001919 struct nlattr *attrs[XFRMA_MAX+1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920 struct xfrm_link *link;
Thomas Grafa7bd9a42007-08-22 13:58:18 -07001921 int type, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923 type = nlh->nlmsg_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924 if (type > XFRM_MSG_MAX)
Thomas Graf1d00a4e2007-03-22 23:30:12 -07001925 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926
1927 type -= XFRM_MSG_BASE;
1928 link = &xfrm_dispatch[type];
1929
1930 /* All operations require privileges, even GET */
Thomas Graf1d00a4e2007-03-22 23:30:12 -07001931 if (security_netlink_recv(skb, CAP_NET_ADMIN))
1932 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933
Thomas Graf492b5582005-05-03 14:26:40 -07001934 if ((type == (XFRM_MSG_GETSA - XFRM_MSG_BASE) ||
1935 type == (XFRM_MSG_GETPOLICY - XFRM_MSG_BASE)) &&
1936 (nlh->nlmsg_flags & NLM_F_DUMP)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937 if (link->dump == NULL)
Thomas Graf1d00a4e2007-03-22 23:30:12 -07001938 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939
Thomas Grafc702e802007-03-22 23:30:55 -07001940 return netlink_dump_start(xfrm_nl, skb, nlh, link->dump, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941 }
1942
Thomas Graf35a7aa02007-08-22 14:00:40 -07001943 err = nlmsg_parse(nlh, xfrm_msg_min[type], attrs, XFRMA_MAX,
Thomas Grafcf5cb792007-08-22 13:59:04 -07001944 xfrma_policy);
Thomas Grafa7bd9a42007-08-22 13:58:18 -07001945 if (err < 0)
1946 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947
1948 if (link->doit == NULL)
Thomas Graf1d00a4e2007-03-22 23:30:12 -07001949 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950
Thomas Graf5424f322007-08-22 14:01:33 -07001951 return link->doit(skb, nlh, attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952}
1953
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07001954static void xfrm_netlink_rcv(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955{
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07001956 mutex_lock(&xfrm_cfg_mutex);
1957 netlink_rcv_skb(skb, &xfrm_user_rcv_msg);
1958 mutex_unlock(&xfrm_cfg_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959}
1960
Thomas Graf7deb2262007-08-22 13:57:39 -07001961static inline size_t xfrm_expire_msgsize(void)
1962{
1963 return NLMSG_ALIGN(sizeof(struct xfrm_user_expire));
1964}
1965
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001966static int build_expire(struct sk_buff *skb, struct xfrm_state *x, struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967{
1968 struct xfrm_user_expire *ue;
1969 struct nlmsghdr *nlh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970
Thomas Graf79b8b7f2007-08-22 12:46:53 -07001971 nlh = nlmsg_put(skb, c->pid, 0, XFRM_MSG_EXPIRE, sizeof(*ue), 0);
1972 if (nlh == NULL)
1973 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974
Thomas Graf7b67c852007-08-22 13:53:52 -07001975 ue = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976 copy_to_user_state(x, &ue->state);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001977 ue->hard = (c->data.hard != 0) ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978
Thomas Graf98250692007-08-22 12:47:26 -07001979 return nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980}
1981
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001982static int xfrm_exp_state_notify(struct xfrm_state *x, struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983{
1984 struct sk_buff *skb;
1985
Thomas Graf7deb2262007-08-22 13:57:39 -07001986 skb = nlmsg_new(xfrm_expire_msgsize(), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987 if (skb == NULL)
1988 return -ENOMEM;
1989
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001990 if (build_expire(skb, x, c) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991 BUG();
1992
Thomas Graf082a1ad2007-08-22 13:54:36 -07001993 return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_EXPIRE, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994}
1995
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001996static int xfrm_aevent_state_notify(struct xfrm_state *x, struct km_event *c)
1997{
1998 struct sk_buff *skb;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001999
Thomas Graf7deb2262007-08-22 13:57:39 -07002000 skb = nlmsg_new(xfrm_aevent_msgsize(), GFP_ATOMIC);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002001 if (skb == NULL)
2002 return -ENOMEM;
2003
2004 if (build_aevent(skb, x, c) < 0)
2005 BUG();
2006
Thomas Graf082a1ad2007-08-22 13:54:36 -07002007 return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_AEVENTS, GFP_ATOMIC);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002008}
2009
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002010static int xfrm_notify_sa_flush(struct km_event *c)
2011{
2012 struct xfrm_usersa_flush *p;
2013 struct nlmsghdr *nlh;
2014 struct sk_buff *skb;
Thomas Graf7deb2262007-08-22 13:57:39 -07002015 int len = NLMSG_ALIGN(sizeof(struct xfrm_usersa_flush));
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002016
Thomas Graf7deb2262007-08-22 13:57:39 -07002017 skb = nlmsg_new(len, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002018 if (skb == NULL)
2019 return -ENOMEM;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002020
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002021 nlh = nlmsg_put(skb, c->pid, c->seq, XFRM_MSG_FLUSHSA, sizeof(*p), 0);
2022 if (nlh == NULL) {
2023 kfree_skb(skb);
2024 return -EMSGSIZE;
2025 }
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002026
Thomas Graf7b67c852007-08-22 13:53:52 -07002027 p = nlmsg_data(nlh);
Herbert Xubf088672005-06-18 22:44:00 -07002028 p->proto = c->data.proto;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002029
Thomas Graf98250692007-08-22 12:47:26 -07002030 nlmsg_end(skb, nlh);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002031
Thomas Graf082a1ad2007-08-22 13:54:36 -07002032 return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_SA, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002033}
2034
Thomas Graf7deb2262007-08-22 13:57:39 -07002035static inline size_t xfrm_sa_len(struct xfrm_state *x)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002036{
Thomas Graf7deb2262007-08-22 13:57:39 -07002037 size_t l = 0;
Herbert Xu1a6509d2008-01-28 19:37:29 -08002038 if (x->aead)
2039 l += nla_total_size(aead_len(x->aead));
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002040 if (x->aalg)
Eric Dumazet0f99be02008-01-08 23:39:06 -08002041 l += nla_total_size(xfrm_alg_len(x->aalg));
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002042 if (x->ealg)
Eric Dumazet0f99be02008-01-08 23:39:06 -08002043 l += nla_total_size(xfrm_alg_len(x->ealg));
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002044 if (x->calg)
Thomas Graf7deb2262007-08-22 13:57:39 -07002045 l += nla_total_size(sizeof(*x->calg));
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002046 if (x->encap)
Thomas Graf7deb2262007-08-22 13:57:39 -07002047 l += nla_total_size(sizeof(*x->encap));
Herbert Xu68325d32007-10-09 13:30:57 -07002048 if (x->security)
2049 l += nla_total_size(sizeof(struct xfrm_user_sec_ctx) +
2050 x->security->ctx_len);
2051 if (x->coaddr)
2052 l += nla_total_size(sizeof(*x->coaddr));
2053
Herbert Xud26f3982007-11-13 21:47:08 -08002054 /* Must count x->lastused as it may become non-zero behind our back. */
2055 l += nla_total_size(sizeof(u64));
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002056
2057 return l;
2058}
2059
2060static int xfrm_notify_sa(struct xfrm_state *x, struct km_event *c)
2061{
2062 struct xfrm_usersa_info *p;
Herbert Xu0603eac2005-06-18 22:54:36 -07002063 struct xfrm_usersa_id *id;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002064 struct nlmsghdr *nlh;
2065 struct sk_buff *skb;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002066 int len = xfrm_sa_len(x);
Herbert Xu0603eac2005-06-18 22:54:36 -07002067 int headlen;
2068
2069 headlen = sizeof(*p);
2070 if (c->event == XFRM_MSG_DELSA) {
Thomas Graf7deb2262007-08-22 13:57:39 -07002071 len += nla_total_size(headlen);
Herbert Xu0603eac2005-06-18 22:54:36 -07002072 headlen = sizeof(*id);
2073 }
Thomas Graf7deb2262007-08-22 13:57:39 -07002074 len += NLMSG_ALIGN(headlen);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002075
Thomas Graf7deb2262007-08-22 13:57:39 -07002076 skb = nlmsg_new(len, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002077 if (skb == NULL)
2078 return -ENOMEM;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002079
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002080 nlh = nlmsg_put(skb, c->pid, c->seq, c->event, headlen, 0);
2081 if (nlh == NULL)
Thomas Grafc0144be2007-08-22 13:55:43 -07002082 goto nla_put_failure;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002083
Thomas Graf7b67c852007-08-22 13:53:52 -07002084 p = nlmsg_data(nlh);
Herbert Xu0603eac2005-06-18 22:54:36 -07002085 if (c->event == XFRM_MSG_DELSA) {
Thomas Grafc0144be2007-08-22 13:55:43 -07002086 struct nlattr *attr;
2087
Thomas Graf7b67c852007-08-22 13:53:52 -07002088 id = nlmsg_data(nlh);
Herbert Xu0603eac2005-06-18 22:54:36 -07002089 memcpy(&id->daddr, &x->id.daddr, sizeof(id->daddr));
2090 id->spi = x->id.spi;
2091 id->family = x->props.family;
2092 id->proto = x->id.proto;
2093
Thomas Grafc0144be2007-08-22 13:55:43 -07002094 attr = nla_reserve(skb, XFRMA_SA, sizeof(*p));
2095 if (attr == NULL)
2096 goto nla_put_failure;
2097
2098 p = nla_data(attr);
Herbert Xu0603eac2005-06-18 22:54:36 -07002099 }
2100
Herbert Xu68325d32007-10-09 13:30:57 -07002101 if (copy_to_user_state_extra(x, p, skb))
2102 goto nla_put_failure;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002103
Thomas Graf98250692007-08-22 12:47:26 -07002104 nlmsg_end(skb, nlh);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002105
Thomas Graf082a1ad2007-08-22 13:54:36 -07002106 return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_SA, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002107
Thomas Grafc0144be2007-08-22 13:55:43 -07002108nla_put_failure:
Herbert Xu68325d32007-10-09 13:30:57 -07002109 /* Somebody screwed up with xfrm_sa_len! */
2110 WARN_ON(1);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002111 kfree_skb(skb);
2112 return -1;
2113}
2114
2115static int xfrm_send_state_notify(struct xfrm_state *x, struct km_event *c)
2116{
2117
2118 switch (c->event) {
Herbert Xuf60f6b82005-06-18 22:44:37 -07002119 case XFRM_MSG_EXPIRE:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002120 return xfrm_exp_state_notify(x, c);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002121 case XFRM_MSG_NEWAE:
2122 return xfrm_aevent_state_notify(x, c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07002123 case XFRM_MSG_DELSA:
2124 case XFRM_MSG_UPDSA:
2125 case XFRM_MSG_NEWSA:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002126 return xfrm_notify_sa(x, c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07002127 case XFRM_MSG_FLUSHSA:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002128 return xfrm_notify_sa_flush(c);
2129 default:
2130 printk("xfrm_user: Unknown SA event %d\n", c->event);
2131 break;
2132 }
2133
2134 return 0;
2135
2136}
2137
Thomas Graf7deb2262007-08-22 13:57:39 -07002138static inline size_t xfrm_acquire_msgsize(struct xfrm_state *x,
2139 struct xfrm_policy *xp)
2140{
2141 return NLMSG_ALIGN(sizeof(struct xfrm_user_acquire))
2142 + nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr)
2143 + nla_total_size(xfrm_user_sec_ctx_size(x->security))
2144 + userpolicy_type_attrsize();
2145}
2146
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147static int build_acquire(struct sk_buff *skb, struct xfrm_state *x,
2148 struct xfrm_tmpl *xt, struct xfrm_policy *xp,
2149 int dir)
2150{
2151 struct xfrm_user_acquire *ua;
2152 struct nlmsghdr *nlh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153 __u32 seq = xfrm_get_acqseq();
2154
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002155 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_ACQUIRE, sizeof(*ua), 0);
2156 if (nlh == NULL)
2157 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158
Thomas Graf7b67c852007-08-22 13:53:52 -07002159 ua = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160 memcpy(&ua->id, &x->id, sizeof(ua->id));
2161 memcpy(&ua->saddr, &x->props.saddr, sizeof(ua->saddr));
2162 memcpy(&ua->sel, &x->sel, sizeof(ua->sel));
2163 copy_to_user_policy(xp, &ua->policy, dir);
2164 ua->aalgos = xt->aalgos;
2165 ua->ealgos = xt->ealgos;
2166 ua->calgos = xt->calgos;
2167 ua->seq = x->km.seq = seq;
2168
2169 if (copy_to_user_tmpl(xp, skb) < 0)
2170 goto nlmsg_failure;
Serge Hallyn0d681622006-07-24 23:30:44 -07002171 if (copy_to_user_state_sec_ctx(x, skb))
Trent Jaegerdf718372005-12-13 23:12:27 -08002172 goto nlmsg_failure;
Jamal Hadi Salim1459bb32006-11-20 16:51:22 -08002173 if (copy_to_user_policy_type(xp->type, skb) < 0)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07002174 goto nlmsg_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175
Thomas Graf98250692007-08-22 12:47:26 -07002176 return nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177
2178nlmsg_failure:
Thomas Graf98250692007-08-22 12:47:26 -07002179 nlmsg_cancel(skb, nlh);
2180 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181}
2182
2183static int xfrm_send_acquire(struct xfrm_state *x, struct xfrm_tmpl *xt,
2184 struct xfrm_policy *xp, int dir)
2185{
2186 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187
Thomas Graf7deb2262007-08-22 13:57:39 -07002188 skb = nlmsg_new(xfrm_acquire_msgsize(x, xp), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189 if (skb == NULL)
2190 return -ENOMEM;
2191
2192 if (build_acquire(skb, x, xt, xp, dir) < 0)
2193 BUG();
2194
Thomas Graf082a1ad2007-08-22 13:54:36 -07002195 return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_ACQUIRE, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002196}
2197
2198/* User gives us xfrm_user_policy_info followed by an array of 0
2199 * or more templates.
2200 */
Venkat Yekkiralacb969f02006-07-24 23:32:20 -07002201static struct xfrm_policy *xfrm_compile_policy(struct sock *sk, int opt,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202 u8 *data, int len, int *dir)
2203{
2204 struct xfrm_userpolicy_info *p = (struct xfrm_userpolicy_info *)data;
2205 struct xfrm_user_tmpl *ut = (struct xfrm_user_tmpl *) (p + 1);
2206 struct xfrm_policy *xp;
2207 int nr;
2208
Venkat Yekkiralacb969f02006-07-24 23:32:20 -07002209 switch (sk->sk_family) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002210 case AF_INET:
2211 if (opt != IP_XFRM_POLICY) {
2212 *dir = -EOPNOTSUPP;
2213 return NULL;
2214 }
2215 break;
2216#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
2217 case AF_INET6:
2218 if (opt != IPV6_XFRM_POLICY) {
2219 *dir = -EOPNOTSUPP;
2220 return NULL;
2221 }
2222 break;
2223#endif
2224 default:
2225 *dir = -EINVAL;
2226 return NULL;
2227 }
2228
2229 *dir = -EINVAL;
2230
2231 if (len < sizeof(*p) ||
2232 verify_newpolicy_info(p))
2233 return NULL;
2234
2235 nr = ((len - sizeof(*p)) / sizeof(*ut));
David S. Millerb4ad86bf2006-12-03 19:19:26 -08002236 if (validate_tmpl(nr, ut, p->sel.family))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237 return NULL;
2238
Herbert Xua4f1bac2005-07-26 15:43:17 -07002239 if (p->dir > XFRM_POLICY_OUT)
2240 return NULL;
2241
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242 xp = xfrm_policy_alloc(GFP_KERNEL);
2243 if (xp == NULL) {
2244 *dir = -ENOBUFS;
2245 return NULL;
2246 }
2247
2248 copy_from_user_policy(xp, p);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07002249 xp->type = XFRM_POLICY_TYPE_MAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002250 copy_templates(xp, ut, nr);
2251
2252 *dir = p->dir;
2253
2254 return xp;
2255}
2256
Thomas Graf7deb2262007-08-22 13:57:39 -07002257static inline size_t xfrm_polexpire_msgsize(struct xfrm_policy *xp)
2258{
2259 return NLMSG_ALIGN(sizeof(struct xfrm_user_polexpire))
2260 + nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr)
2261 + nla_total_size(xfrm_user_sec_ctx_size(xp->security))
2262 + userpolicy_type_attrsize();
2263}
2264
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265static int build_polexpire(struct sk_buff *skb, struct xfrm_policy *xp,
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002266 int dir, struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267{
2268 struct xfrm_user_polexpire *upe;
2269 struct nlmsghdr *nlh;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002270 int hard = c->data.hard;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002272 nlh = nlmsg_put(skb, c->pid, 0, XFRM_MSG_POLEXPIRE, sizeof(*upe), 0);
2273 if (nlh == NULL)
2274 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275
Thomas Graf7b67c852007-08-22 13:53:52 -07002276 upe = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002277 copy_to_user_policy(xp, &upe->pol, dir);
2278 if (copy_to_user_tmpl(xp, skb) < 0)
2279 goto nlmsg_failure;
Trent Jaegerdf718372005-12-13 23:12:27 -08002280 if (copy_to_user_sec_ctx(xp, skb))
2281 goto nlmsg_failure;
Jamal Hadi Salim1459bb32006-11-20 16:51:22 -08002282 if (copy_to_user_policy_type(xp->type, skb) < 0)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07002283 goto nlmsg_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002284 upe->hard = !!hard;
2285
Thomas Graf98250692007-08-22 12:47:26 -07002286 return nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287
2288nlmsg_failure:
Thomas Graf98250692007-08-22 12:47:26 -07002289 nlmsg_cancel(skb, nlh);
2290 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002291}
2292
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002293static int xfrm_exp_policy_notify(struct xfrm_policy *xp, int dir, struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002294{
2295 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002296
Thomas Graf7deb2262007-08-22 13:57:39 -07002297 skb = nlmsg_new(xfrm_polexpire_msgsize(xp), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298 if (skb == NULL)
2299 return -ENOMEM;
2300
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002301 if (build_polexpire(skb, xp, dir, c) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302 BUG();
2303
Thomas Graf082a1ad2007-08-22 13:54:36 -07002304 return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_EXPIRE, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002305}
2306
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002307static int xfrm_notify_policy(struct xfrm_policy *xp, int dir, struct km_event *c)
2308{
2309 struct xfrm_userpolicy_info *p;
Herbert Xu0603eac2005-06-18 22:54:36 -07002310 struct xfrm_userpolicy_id *id;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002311 struct nlmsghdr *nlh;
2312 struct sk_buff *skb;
Thomas Graf7deb2262007-08-22 13:57:39 -07002313 int len = nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr);
Herbert Xu0603eac2005-06-18 22:54:36 -07002314 int headlen;
2315
2316 headlen = sizeof(*p);
2317 if (c->event == XFRM_MSG_DELPOLICY) {
Thomas Graf7deb2262007-08-22 13:57:39 -07002318 len += nla_total_size(headlen);
Herbert Xu0603eac2005-06-18 22:54:36 -07002319 headlen = sizeof(*id);
2320 }
Thomas Grafcfbfd452007-08-22 13:57:04 -07002321 len += userpolicy_type_attrsize();
Thomas Graf7deb2262007-08-22 13:57:39 -07002322 len += NLMSG_ALIGN(headlen);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002323
Thomas Graf7deb2262007-08-22 13:57:39 -07002324 skb = nlmsg_new(len, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002325 if (skb == NULL)
2326 return -ENOMEM;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002327
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002328 nlh = nlmsg_put(skb, c->pid, c->seq, c->event, headlen, 0);
2329 if (nlh == NULL)
2330 goto nlmsg_failure;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002331
Thomas Graf7b67c852007-08-22 13:53:52 -07002332 p = nlmsg_data(nlh);
Herbert Xu0603eac2005-06-18 22:54:36 -07002333 if (c->event == XFRM_MSG_DELPOLICY) {
Thomas Grafc0144be2007-08-22 13:55:43 -07002334 struct nlattr *attr;
2335
Thomas Graf7b67c852007-08-22 13:53:52 -07002336 id = nlmsg_data(nlh);
Herbert Xu0603eac2005-06-18 22:54:36 -07002337 memset(id, 0, sizeof(*id));
2338 id->dir = dir;
2339 if (c->data.byid)
2340 id->index = xp->index;
2341 else
2342 memcpy(&id->sel, &xp->selector, sizeof(id->sel));
2343
Thomas Grafc0144be2007-08-22 13:55:43 -07002344 attr = nla_reserve(skb, XFRMA_POLICY, sizeof(*p));
2345 if (attr == NULL)
2346 goto nlmsg_failure;
2347
2348 p = nla_data(attr);
Herbert Xu0603eac2005-06-18 22:54:36 -07002349 }
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002350
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002351 copy_to_user_policy(xp, p, dir);
2352 if (copy_to_user_tmpl(xp, skb) < 0)
2353 goto nlmsg_failure;
Jamal Hadi Salim1459bb32006-11-20 16:51:22 -08002354 if (copy_to_user_policy_type(xp->type, skb) < 0)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07002355 goto nlmsg_failure;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002356
Thomas Graf98250692007-08-22 12:47:26 -07002357 nlmsg_end(skb, nlh);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002358
Thomas Graf082a1ad2007-08-22 13:54:36 -07002359 return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_POLICY, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002360
2361nlmsg_failure:
2362 kfree_skb(skb);
2363 return -1;
2364}
2365
2366static int xfrm_notify_policy_flush(struct km_event *c)
2367{
2368 struct nlmsghdr *nlh;
2369 struct sk_buff *skb;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002370
Thomas Graf7deb2262007-08-22 13:57:39 -07002371 skb = nlmsg_new(userpolicy_type_attrsize(), GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002372 if (skb == NULL)
2373 return -ENOMEM;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002374
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002375 nlh = nlmsg_put(skb, c->pid, c->seq, XFRM_MSG_FLUSHPOLICY, 0, 0);
2376 if (nlh == NULL)
2377 goto nlmsg_failure;
Jamal Hadi Salim0c51f532006-11-27 12:58:20 -08002378 if (copy_to_user_policy_type(c->data.type, skb) < 0)
2379 goto nlmsg_failure;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002380
Thomas Graf98250692007-08-22 12:47:26 -07002381 nlmsg_end(skb, nlh);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002382
Thomas Graf082a1ad2007-08-22 13:54:36 -07002383 return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_POLICY, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002384
2385nlmsg_failure:
2386 kfree_skb(skb);
2387 return -1;
2388}
2389
2390static int xfrm_send_policy_notify(struct xfrm_policy *xp, int dir, struct km_event *c)
2391{
2392
2393 switch (c->event) {
Herbert Xuf60f6b82005-06-18 22:44:37 -07002394 case XFRM_MSG_NEWPOLICY:
2395 case XFRM_MSG_UPDPOLICY:
2396 case XFRM_MSG_DELPOLICY:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002397 return xfrm_notify_policy(xp, dir, c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07002398 case XFRM_MSG_FLUSHPOLICY:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002399 return xfrm_notify_policy_flush(c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07002400 case XFRM_MSG_POLEXPIRE:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002401 return xfrm_exp_policy_notify(xp, dir, c);
2402 default:
2403 printk("xfrm_user: Unknown Policy event %d\n", c->event);
2404 }
2405
2406 return 0;
2407
2408}
2409
Thomas Graf7deb2262007-08-22 13:57:39 -07002410static inline size_t xfrm_report_msgsize(void)
2411{
2412 return NLMSG_ALIGN(sizeof(struct xfrm_user_report));
2413}
2414
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002415static int build_report(struct sk_buff *skb, u8 proto,
2416 struct xfrm_selector *sel, xfrm_address_t *addr)
2417{
2418 struct xfrm_user_report *ur;
2419 struct nlmsghdr *nlh;
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002420
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002421 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_REPORT, sizeof(*ur), 0);
2422 if (nlh == NULL)
2423 return -EMSGSIZE;
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002424
Thomas Graf7b67c852007-08-22 13:53:52 -07002425 ur = nlmsg_data(nlh);
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002426 ur->proto = proto;
2427 memcpy(&ur->sel, sel, sizeof(ur->sel));
2428
2429 if (addr)
Thomas Grafc0144be2007-08-22 13:55:43 -07002430 NLA_PUT(skb, XFRMA_COADDR, sizeof(*addr), addr);
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002431
Thomas Graf98250692007-08-22 12:47:26 -07002432 return nlmsg_end(skb, nlh);
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002433
Thomas Grafc0144be2007-08-22 13:55:43 -07002434nla_put_failure:
Thomas Graf98250692007-08-22 12:47:26 -07002435 nlmsg_cancel(skb, nlh);
2436 return -EMSGSIZE;
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002437}
2438
2439static int xfrm_send_report(u8 proto, struct xfrm_selector *sel,
2440 xfrm_address_t *addr)
2441{
2442 struct sk_buff *skb;
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002443
Thomas Graf7deb2262007-08-22 13:57:39 -07002444 skb = nlmsg_new(xfrm_report_msgsize(), GFP_ATOMIC);
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002445 if (skb == NULL)
2446 return -ENOMEM;
2447
2448 if (build_report(skb, proto, sel, addr) < 0)
2449 BUG();
2450
Thomas Graf082a1ad2007-08-22 13:54:36 -07002451 return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_REPORT, GFP_ATOMIC);
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002452}
2453
Linus Torvalds1da177e2005-04-16 15:20:36 -07002454static struct xfrm_mgr netlink_mgr = {
2455 .id = "netlink",
2456 .notify = xfrm_send_state_notify,
2457 .acquire = xfrm_send_acquire,
2458 .compile_policy = xfrm_compile_policy,
2459 .notify_policy = xfrm_send_policy_notify,
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002460 .report = xfrm_send_report,
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002461 .migrate = xfrm_send_migrate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002462};
2463
2464static int __init xfrm_user_init(void)
2465{
Patrick McHardybe336902006-03-20 22:40:54 -08002466 struct sock *nlsk;
2467
Masahide NAKAMURA654b32c2006-08-23 19:12:56 -07002468 printk(KERN_INFO "Initializing XFRM netlink socket\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002469
Eric W. Biedermanb4b51022007-09-12 13:05:38 +02002470 nlsk = netlink_kernel_create(&init_net, NETLINK_XFRM, XFRMNLGRP_MAX,
Patrick McHardyaf65bdf2007-04-20 14:14:21 -07002471 xfrm_netlink_rcv, NULL, THIS_MODULE);
Patrick McHardybe336902006-03-20 22:40:54 -08002472 if (nlsk == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002473 return -ENOMEM;
Patrick McHardybe336902006-03-20 22:40:54 -08002474 rcu_assign_pointer(xfrm_nl, nlsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002475
2476 xfrm_register_km(&netlink_mgr);
2477
2478 return 0;
2479}
2480
2481static void __exit xfrm_user_exit(void)
2482{
Patrick McHardybe336902006-03-20 22:40:54 -08002483 struct sock *nlsk = xfrm_nl;
2484
Linus Torvalds1da177e2005-04-16 15:20:36 -07002485 xfrm_unregister_km(&netlink_mgr);
Patrick McHardybe336902006-03-20 22:40:54 -08002486 rcu_assign_pointer(xfrm_nl, NULL);
2487 synchronize_rcu();
Denis V. Lunevb7c6ba62008-01-28 14:41:19 -08002488 netlink_kernel_release(nlsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002489}
2490
2491module_init(xfrm_user_init);
2492module_exit(xfrm_user_exit);
2493MODULE_LICENSE("GPL");
Harald Welte4fdb3bb2005-08-09 19:40:55 -07002494MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK, NETLINK_XFRM);
Jamal Hadi Salimf8cd5482006-03-20 19:15:11 -08002495