blob: f5fd5b3147cc64a2052e0033e4508c7557d3b076 [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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535};
536
Thomas Grafc0144be2007-08-22 13:55:43 -0700537static int copy_sec_ctx(struct xfrm_sec_ctx *s, struct sk_buff *skb)
538{
Thomas Grafc0144be2007-08-22 13:55:43 -0700539 struct xfrm_user_sec_ctx *uctx;
540 struct nlattr *attr;
Herbert Xu68325d32007-10-09 13:30:57 -0700541 int ctx_size = sizeof(*uctx) + s->ctx_len;
Thomas Grafc0144be2007-08-22 13:55:43 -0700542
543 attr = nla_reserve(skb, XFRMA_SEC_CTX, ctx_size);
544 if (attr == NULL)
545 return -EMSGSIZE;
546
547 uctx = nla_data(attr);
548 uctx->exttype = XFRMA_SEC_CTX;
549 uctx->len = ctx_size;
550 uctx->ctx_doi = s->ctx_doi;
551 uctx->ctx_alg = s->ctx_alg;
552 uctx->ctx_len = s->ctx_len;
553 memcpy(uctx + 1, s->ctx_str, s->ctx_len);
554
555 return 0;
556}
557
Herbert Xu68325d32007-10-09 13:30:57 -0700558/* Don't change this without updating xfrm_sa_len! */
559static int copy_to_user_state_extra(struct xfrm_state *x,
560 struct xfrm_usersa_info *p,
561 struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 copy_to_user_state(x, p);
564
Herbert Xu050f0092007-10-09 13:31:47 -0700565 if (x->coaddr)
566 NLA_PUT(skb, XFRMA_COADDR, sizeof(*x->coaddr), x->coaddr);
567
568 if (x->lastused)
569 NLA_PUT_U64(skb, XFRMA_LASTUSED, x->lastused);
Herbert Xu050f0092007-10-09 13:31:47 -0700570
Herbert Xu1a6509d2008-01-28 19:37:29 -0800571 if (x->aead)
572 NLA_PUT(skb, XFRMA_ALG_AEAD, aead_len(x->aead), x->aead);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 if (x->aalg)
Eric Dumazet0f99be02008-01-08 23:39:06 -0800574 NLA_PUT(skb, XFRMA_ALG_AUTH, xfrm_alg_len(x->aalg), x->aalg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 if (x->ealg)
Eric Dumazet0f99be02008-01-08 23:39:06 -0800576 NLA_PUT(skb, XFRMA_ALG_CRYPT, xfrm_alg_len(x->ealg), x->ealg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 if (x->calg)
Thomas Grafc0144be2007-08-22 13:55:43 -0700578 NLA_PUT(skb, XFRMA_ALG_COMP, sizeof(*(x->calg)), x->calg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579
580 if (x->encap)
Thomas Grafc0144be2007-08-22 13:55:43 -0700581 NLA_PUT(skb, XFRMA_ENCAP, sizeof(*x->encap), x->encap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582
Thomas Grafc0144be2007-08-22 13:55:43 -0700583 if (x->security && copy_sec_ctx(x->security, skb) < 0)
584 goto nla_put_failure;
Noriaki TAKAMIYA060f02a2006-08-23 18:18:55 -0700585
Herbert Xu68325d32007-10-09 13:30:57 -0700586 return 0;
587
588nla_put_failure:
589 return -EMSGSIZE;
590}
591
592static int dump_one_state(struct xfrm_state *x, int count, void *ptr)
593{
594 struct xfrm_dump_info *sp = ptr;
595 struct sk_buff *in_skb = sp->in_skb;
596 struct sk_buff *skb = sp->out_skb;
597 struct xfrm_usersa_info *p;
598 struct nlmsghdr *nlh;
599 int err;
600
Herbert Xu68325d32007-10-09 13:30:57 -0700601 nlh = nlmsg_put(skb, NETLINK_CB(in_skb).pid, sp->nlmsg_seq,
602 XFRM_MSG_NEWSA, sizeof(*p), sp->nlmsg_flags);
603 if (nlh == NULL)
604 return -EMSGSIZE;
605
606 p = nlmsg_data(nlh);
607
608 err = copy_to_user_state_extra(x, p, skb);
609 if (err)
610 goto nla_put_failure;
611
Thomas Graf98250692007-08-22 12:47:26 -0700612 nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 return 0;
614
Thomas Grafc0144be2007-08-22 13:55:43 -0700615nla_put_failure:
Thomas Graf98250692007-08-22 12:47:26 -0700616 nlmsg_cancel(skb, nlh);
Herbert Xu68325d32007-10-09 13:30:57 -0700617 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618}
619
Timo Teras4c563f72008-02-28 21:31:08 -0800620static int xfrm_dump_sa_done(struct netlink_callback *cb)
621{
622 struct xfrm_state_walk *walk = (struct xfrm_state_walk *) &cb->args[1];
623 xfrm_state_walk_done(walk);
624 return 0;
625}
626
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627static int xfrm_dump_sa(struct sk_buff *skb, struct netlink_callback *cb)
628{
Timo Teras4c563f72008-02-28 21:31:08 -0800629 struct xfrm_state_walk *walk = (struct xfrm_state_walk *) &cb->args[1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 struct xfrm_dump_info info;
631
Timo Teras4c563f72008-02-28 21:31:08 -0800632 BUILD_BUG_ON(sizeof(struct xfrm_state_walk) >
633 sizeof(cb->args) - sizeof(cb->args[0]));
634
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 info.in_skb = cb->skb;
636 info.out_skb = skb;
637 info.nlmsg_seq = cb->nlh->nlmsg_seq;
638 info.nlmsg_flags = NLM_F_MULTI;
Timo Teras4c563f72008-02-28 21:31:08 -0800639
640 if (!cb->args[0]) {
641 cb->args[0] = 1;
642 xfrm_state_walk_init(walk, 0);
643 }
644
645 (void) xfrm_state_walk(walk, dump_one_state, &info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646
647 return skb->len;
648}
649
650static struct sk_buff *xfrm_state_netlink(struct sk_buff *in_skb,
651 struct xfrm_state *x, u32 seq)
652{
653 struct xfrm_dump_info info;
654 struct sk_buff *skb;
655
Thomas Graf7deb2262007-08-22 13:57:39 -0700656 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 if (!skb)
658 return ERR_PTR(-ENOMEM);
659
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 info.in_skb = in_skb;
661 info.out_skb = skb;
662 info.nlmsg_seq = seq;
663 info.nlmsg_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664
665 if (dump_one_state(x, 0, &info)) {
666 kfree_skb(skb);
667 return NULL;
668 }
669
670 return skb;
671}
672
Thomas Graf7deb2262007-08-22 13:57:39 -0700673static inline size_t xfrm_spdinfo_msgsize(void)
674{
675 return NLMSG_ALIGN(4)
676 + nla_total_size(sizeof(struct xfrmu_spdinfo))
677 + nla_total_size(sizeof(struct xfrmu_spdhinfo));
678}
679
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700680static int build_spdinfo(struct sk_buff *skb, u32 pid, u32 seq, u32 flags)
681{
Jamal Hadi Salim5a6d3412007-05-04 12:55:39 -0700682 struct xfrmk_spdinfo si;
683 struct xfrmu_spdinfo spc;
684 struct xfrmu_spdhinfo sph;
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700685 struct nlmsghdr *nlh;
686 u32 *f;
687
688 nlh = nlmsg_put(skb, pid, seq, XFRM_MSG_NEWSPDINFO, sizeof(u32), 0);
689 if (nlh == NULL) /* shouldnt really happen ... */
690 return -EMSGSIZE;
691
692 f = nlmsg_data(nlh);
693 *f = flags;
694 xfrm_spd_getinfo(&si);
Jamal Hadi Salim5a6d3412007-05-04 12:55:39 -0700695 spc.incnt = si.incnt;
696 spc.outcnt = si.outcnt;
697 spc.fwdcnt = si.fwdcnt;
698 spc.inscnt = si.inscnt;
699 spc.outscnt = si.outscnt;
700 spc.fwdscnt = si.fwdscnt;
701 sph.spdhcnt = si.spdhcnt;
702 sph.spdhmcnt = si.spdhmcnt;
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700703
Jamal Hadi Salim5a6d3412007-05-04 12:55:39 -0700704 NLA_PUT(skb, XFRMA_SPD_INFO, sizeof(spc), &spc);
705 NLA_PUT(skb, XFRMA_SPD_HINFO, sizeof(sph), &sph);
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700706
707 return nlmsg_end(skb, nlh);
708
709nla_put_failure:
710 nlmsg_cancel(skb, nlh);
711 return -EMSGSIZE;
712}
713
714static int xfrm_get_spdinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -0700715 struct nlattr **attrs)
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700716{
717 struct sk_buff *r_skb;
Thomas Graf7b67c852007-08-22 13:53:52 -0700718 u32 *flags = nlmsg_data(nlh);
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700719 u32 spid = NETLINK_CB(skb).pid;
720 u32 seq = nlh->nlmsg_seq;
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700721
Thomas Graf7deb2262007-08-22 13:57:39 -0700722 r_skb = nlmsg_new(xfrm_spdinfo_msgsize(), GFP_ATOMIC);
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700723 if (r_skb == NULL)
724 return -ENOMEM;
725
726 if (build_spdinfo(r_skb, spid, seq, *flags) < 0)
727 BUG();
728
729 return nlmsg_unicast(xfrm_nl, r_skb, spid);
730}
731
Thomas Graf7deb2262007-08-22 13:57:39 -0700732static inline size_t xfrm_sadinfo_msgsize(void)
733{
734 return NLMSG_ALIGN(4)
735 + nla_total_size(sizeof(struct xfrmu_sadhinfo))
736 + nla_total_size(4); /* XFRMA_SAD_CNT */
737}
738
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700739static int build_sadinfo(struct sk_buff *skb, u32 pid, u32 seq, u32 flags)
740{
Jamal Hadi Salimaf11e312007-05-04 12:55:13 -0700741 struct xfrmk_sadinfo si;
742 struct xfrmu_sadhinfo sh;
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700743 struct nlmsghdr *nlh;
744 u32 *f;
745
746 nlh = nlmsg_put(skb, pid, seq, XFRM_MSG_NEWSADINFO, sizeof(u32), 0);
747 if (nlh == NULL) /* shouldnt really happen ... */
748 return -EMSGSIZE;
749
750 f = nlmsg_data(nlh);
751 *f = flags;
752 xfrm_sad_getinfo(&si);
753
Jamal Hadi Salimaf11e312007-05-04 12:55:13 -0700754 sh.sadhmcnt = si.sadhmcnt;
755 sh.sadhcnt = si.sadhcnt;
756
757 NLA_PUT_U32(skb, XFRMA_SAD_CNT, si.sadcnt);
758 NLA_PUT(skb, XFRMA_SAD_HINFO, sizeof(sh), &sh);
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700759
760 return nlmsg_end(skb, nlh);
761
762nla_put_failure:
763 nlmsg_cancel(skb, nlh);
764 return -EMSGSIZE;
765}
766
767static int xfrm_get_sadinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -0700768 struct nlattr **attrs)
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700769{
770 struct sk_buff *r_skb;
Thomas Graf7b67c852007-08-22 13:53:52 -0700771 u32 *flags = nlmsg_data(nlh);
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700772 u32 spid = NETLINK_CB(skb).pid;
773 u32 seq = nlh->nlmsg_seq;
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700774
Thomas Graf7deb2262007-08-22 13:57:39 -0700775 r_skb = nlmsg_new(xfrm_sadinfo_msgsize(), GFP_ATOMIC);
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700776 if (r_skb == NULL)
777 return -ENOMEM;
778
779 if (build_sadinfo(r_skb, spid, seq, *flags) < 0)
780 BUG();
781
782 return nlmsg_unicast(xfrm_nl, r_skb, spid);
783}
784
Christoph Hellwig22e70052007-01-02 15:22:30 -0800785static int xfrm_get_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -0700786 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787{
Thomas Graf7b67c852007-08-22 13:53:52 -0700788 struct xfrm_usersa_id *p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 struct xfrm_state *x;
790 struct sk_buff *resp_skb;
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700791 int err = -ESRCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792
Thomas Graf35a7aa02007-08-22 14:00:40 -0700793 x = xfrm_user_state_lookup(p, attrs, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 if (x == NULL)
795 goto out_noput;
796
797 resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
798 if (IS_ERR(resp_skb)) {
799 err = PTR_ERR(resp_skb);
800 } else {
Thomas Graf082a1ad2007-08-22 13:54:36 -0700801 err = nlmsg_unicast(xfrm_nl, resp_skb, NETLINK_CB(skb).pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 }
803 xfrm_state_put(x);
804out_noput:
805 return err;
806}
807
808static int verify_userspi_info(struct xfrm_userspi_info *p)
809{
810 switch (p->info.id.proto) {
811 case IPPROTO_AH:
812 case IPPROTO_ESP:
813 break;
814
815 case IPPROTO_COMP:
816 /* IPCOMP spi is 16-bits. */
817 if (p->max >= 0x10000)
818 return -EINVAL;
819 break;
820
821 default:
822 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700823 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824
825 if (p->min > p->max)
826 return -EINVAL;
827
828 return 0;
829}
830
Christoph Hellwig22e70052007-01-02 15:22:30 -0800831static int xfrm_alloc_userspi(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -0700832 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833{
834 struct xfrm_state *x;
835 struct xfrm_userspi_info *p;
836 struct sk_buff *resp_skb;
837 xfrm_address_t *daddr;
838 int family;
839 int err;
840
Thomas Graf7b67c852007-08-22 13:53:52 -0700841 p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 err = verify_userspi_info(p);
843 if (err)
844 goto out_noput;
845
846 family = p->info.family;
847 daddr = &p->info.id.daddr;
848
849 x = NULL;
850 if (p->info.seq) {
851 x = xfrm_find_acq_byseq(p->info.seq);
852 if (x && xfrm_addr_cmp(&x->id.daddr, daddr, family)) {
853 xfrm_state_put(x);
854 x = NULL;
855 }
856 }
857
858 if (!x)
859 x = xfrm_find_acq(p->info.mode, p->info.reqid,
860 p->info.id.proto, daddr,
861 &p->info.saddr, 1,
862 family);
863 err = -ENOENT;
864 if (x == NULL)
865 goto out_noput;
866
Herbert Xu658b2192007-10-09 13:29:52 -0700867 err = xfrm_alloc_spi(x, p->min, p->max);
868 if (err)
869 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870
Herbert Xu658b2192007-10-09 13:29:52 -0700871 resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 if (IS_ERR(resp_skb)) {
873 err = PTR_ERR(resp_skb);
874 goto out;
875 }
876
Thomas Graf082a1ad2007-08-22 13:54:36 -0700877 err = nlmsg_unicast(xfrm_nl, resp_skb, NETLINK_CB(skb).pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878
879out:
880 xfrm_state_put(x);
881out_noput:
882 return err;
883}
884
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -0800885static int verify_policy_dir(u8 dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886{
887 switch (dir) {
888 case XFRM_POLICY_IN:
889 case XFRM_POLICY_OUT:
890 case XFRM_POLICY_FWD:
891 break;
892
893 default:
894 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700895 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896
897 return 0;
898}
899
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -0800900static int verify_policy_type(u8 type)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -0700901{
902 switch (type) {
903 case XFRM_POLICY_TYPE_MAIN:
904#ifdef CONFIG_XFRM_SUB_POLICY
905 case XFRM_POLICY_TYPE_SUB:
906#endif
907 break;
908
909 default:
910 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700911 }
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -0700912
913 return 0;
914}
915
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916static int verify_newpolicy_info(struct xfrm_userpolicy_info *p)
917{
918 switch (p->share) {
919 case XFRM_SHARE_ANY:
920 case XFRM_SHARE_SESSION:
921 case XFRM_SHARE_USER:
922 case XFRM_SHARE_UNIQUE:
923 break;
924
925 default:
926 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700927 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928
929 switch (p->action) {
930 case XFRM_POLICY_ALLOW:
931 case XFRM_POLICY_BLOCK:
932 break;
933
934 default:
935 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700936 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937
938 switch (p->sel.family) {
939 case AF_INET:
940 break;
941
942 case AF_INET6:
943#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
944 break;
945#else
946 return -EAFNOSUPPORT;
947#endif
948
949 default:
950 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700951 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952
953 return verify_policy_dir(p->dir);
954}
955
Thomas Graf5424f322007-08-22 14:01:33 -0700956static int copy_from_user_sec_ctx(struct xfrm_policy *pol, struct nlattr **attrs)
Trent Jaegerdf718372005-12-13 23:12:27 -0800957{
Thomas Graf5424f322007-08-22 14:01:33 -0700958 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
Trent Jaegerdf718372005-12-13 23:12:27 -0800959 struct xfrm_user_sec_ctx *uctx;
960
961 if (!rt)
962 return 0;
963
Thomas Graf5424f322007-08-22 14:01:33 -0700964 uctx = nla_data(rt);
Trent Jaegerdf718372005-12-13 23:12:27 -0800965 return security_xfrm_policy_alloc(pol, uctx);
966}
967
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968static void copy_templates(struct xfrm_policy *xp, struct xfrm_user_tmpl *ut,
969 int nr)
970{
971 int i;
972
973 xp->xfrm_nr = nr;
974 for (i = 0; i < nr; i++, ut++) {
975 struct xfrm_tmpl *t = &xp->xfrm_vec[i];
976
977 memcpy(&t->id, &ut->id, sizeof(struct xfrm_id));
978 memcpy(&t->saddr, &ut->saddr,
979 sizeof(xfrm_address_t));
980 t->reqid = ut->reqid;
981 t->mode = ut->mode;
982 t->share = ut->share;
983 t->optional = ut->optional;
984 t->aalgos = ut->aalgos;
985 t->ealgos = ut->ealgos;
986 t->calgos = ut->calgos;
Miika Komu8511d012006-11-30 16:40:51 -0800987 t->encap_family = ut->family;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 }
989}
990
David S. Millerb4ad86bf2006-12-03 19:19:26 -0800991static int validate_tmpl(int nr, struct xfrm_user_tmpl *ut, u16 family)
992{
993 int i;
994
995 if (nr > XFRM_MAX_DEPTH)
996 return -EINVAL;
997
998 for (i = 0; i < nr; i++) {
999 /* We never validated the ut->family value, so many
1000 * applications simply leave it at zero. The check was
1001 * never made and ut->family was ignored because all
1002 * templates could be assumed to have the same family as
1003 * the policy itself. Now that we will have ipv4-in-ipv6
1004 * and ipv6-in-ipv4 tunnels, this is no longer true.
1005 */
1006 if (!ut[i].family)
1007 ut[i].family = family;
1008
1009 switch (ut[i].family) {
1010 case AF_INET:
1011 break;
1012#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1013 case AF_INET6:
1014 break;
1015#endif
1016 default:
1017 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001018 }
David S. Millerb4ad86bf2006-12-03 19:19:26 -08001019 }
1020
1021 return 0;
1022}
1023
Thomas Graf5424f322007-08-22 14:01:33 -07001024static int copy_from_user_tmpl(struct xfrm_policy *pol, struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025{
Thomas Graf5424f322007-08-22 14:01:33 -07001026 struct nlattr *rt = attrs[XFRMA_TMPL];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027
1028 if (!rt) {
1029 pol->xfrm_nr = 0;
1030 } else {
Thomas Graf5424f322007-08-22 14:01:33 -07001031 struct xfrm_user_tmpl *utmpl = nla_data(rt);
1032 int nr = nla_len(rt) / sizeof(*utmpl);
David S. Millerb4ad86bf2006-12-03 19:19:26 -08001033 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034
David S. Millerb4ad86bf2006-12-03 19:19:26 -08001035 err = validate_tmpl(nr, utmpl, pol->family);
1036 if (err)
1037 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038
Thomas Graf5424f322007-08-22 14:01:33 -07001039 copy_templates(pol, utmpl, nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 }
1041 return 0;
1042}
1043
Thomas Graf5424f322007-08-22 14:01:33 -07001044static int copy_from_user_policy_type(u8 *tp, struct nlattr **attrs)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001045{
Thomas Graf5424f322007-08-22 14:01:33 -07001046 struct nlattr *rt = attrs[XFRMA_POLICY_TYPE];
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001047 struct xfrm_userpolicy_type *upt;
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001048 u8 type = XFRM_POLICY_TYPE_MAIN;
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001049 int err;
1050
1051 if (rt) {
Thomas Graf5424f322007-08-22 14:01:33 -07001052 upt = nla_data(rt);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001053 type = upt->type;
1054 }
1055
1056 err = verify_policy_type(type);
1057 if (err)
1058 return err;
1059
1060 *tp = type;
1061 return 0;
1062}
1063
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064static void copy_from_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p)
1065{
1066 xp->priority = p->priority;
1067 xp->index = p->index;
1068 memcpy(&xp->selector, &p->sel, sizeof(xp->selector));
1069 memcpy(&xp->lft, &p->lft, sizeof(xp->lft));
1070 xp->action = p->action;
1071 xp->flags = p->flags;
1072 xp->family = p->sel.family;
1073 /* XXX xp->share = p->share; */
1074}
1075
1076static void copy_to_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p, int dir)
1077{
1078 memcpy(&p->sel, &xp->selector, sizeof(p->sel));
1079 memcpy(&p->lft, &xp->lft, sizeof(p->lft));
1080 memcpy(&p->curlft, &xp->curlft, sizeof(p->curlft));
1081 p->priority = xp->priority;
1082 p->index = xp->index;
1083 p->sel.family = xp->family;
1084 p->dir = dir;
1085 p->action = xp->action;
1086 p->flags = xp->flags;
1087 p->share = XFRM_SHARE_ANY; /* XXX xp->share */
1088}
1089
Thomas Graf5424f322007-08-22 14:01:33 -07001090static struct xfrm_policy *xfrm_policy_construct(struct xfrm_userpolicy_info *p, struct nlattr **attrs, int *errp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091{
1092 struct xfrm_policy *xp = xfrm_policy_alloc(GFP_KERNEL);
1093 int err;
1094
1095 if (!xp) {
1096 *errp = -ENOMEM;
1097 return NULL;
1098 }
1099
1100 copy_from_user_policy(xp, p);
Trent Jaegerdf718372005-12-13 23:12:27 -08001101
Thomas Graf35a7aa02007-08-22 14:00:40 -07001102 err = copy_from_user_policy_type(&xp->type, attrs);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001103 if (err)
1104 goto error;
1105
Thomas Graf35a7aa02007-08-22 14:00:40 -07001106 if (!(err = copy_from_user_tmpl(xp, attrs)))
1107 err = copy_from_user_sec_ctx(xp, attrs);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001108 if (err)
1109 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110
1111 return xp;
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001112 error:
1113 *errp = err;
YOSHIFUJI Hideaki073a3712008-02-14 14:52:38 -08001114 xp->dead = 1;
WANG Cong64c31b32008-01-07 22:34:29 -08001115 xfrm_policy_destroy(xp);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001116 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117}
1118
Christoph Hellwig22e70052007-01-02 15:22:30 -08001119static int xfrm_add_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001120 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121{
Thomas Graf7b67c852007-08-22 13:53:52 -07001122 struct xfrm_userpolicy_info *p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 struct xfrm_policy *xp;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001124 struct km_event c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 int err;
1126 int excl;
1127
1128 err = verify_newpolicy_info(p);
1129 if (err)
1130 return err;
Thomas Graf35a7aa02007-08-22 14:00:40 -07001131 err = verify_sec_ctx_len(attrs);
Trent Jaegerdf718372005-12-13 23:12:27 -08001132 if (err)
1133 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134
Thomas Graf35a7aa02007-08-22 14:00:40 -07001135 xp = xfrm_policy_construct(p, attrs, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136 if (!xp)
1137 return err;
1138
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001139 /* shouldnt excl be based on nlh flags??
1140 * Aha! this is anti-netlink really i.e more pfkey derived
1141 * in netlink excl is a flag and you wouldnt need
1142 * a type XFRM_MSG_UPDPOLICY - JHS */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 excl = nlh->nlmsg_type == XFRM_MSG_NEWPOLICY;
1144 err = xfrm_policy_insert(p->dir, xp, excl);
Joy Lattenab5f5e82007-09-17 11:51:22 -07001145 xfrm_audit_policy_add(xp, err ? 0 : 1, NETLINK_CB(skb).loginuid,
1146 NETLINK_CB(skb).sid);
Joy Latten161a09e2006-11-27 13:11:54 -06001147
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 if (err) {
Trent Jaeger5f8ac642006-01-06 13:22:39 -08001149 security_xfrm_policy_free(xp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 kfree(xp);
1151 return err;
1152 }
1153
Herbert Xuf60f6b82005-06-18 22:44:37 -07001154 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001155 c.seq = nlh->nlmsg_seq;
1156 c.pid = nlh->nlmsg_pid;
1157 km_policy_notify(xp, p->dir, &c);
1158
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 xfrm_pol_put(xp);
1160
1161 return 0;
1162}
1163
1164static int copy_to_user_tmpl(struct xfrm_policy *xp, struct sk_buff *skb)
1165{
1166 struct xfrm_user_tmpl vec[XFRM_MAX_DEPTH];
1167 int i;
1168
1169 if (xp->xfrm_nr == 0)
1170 return 0;
1171
1172 for (i = 0; i < xp->xfrm_nr; i++) {
1173 struct xfrm_user_tmpl *up = &vec[i];
1174 struct xfrm_tmpl *kp = &xp->xfrm_vec[i];
1175
1176 memcpy(&up->id, &kp->id, sizeof(up->id));
Miika Komu8511d012006-11-30 16:40:51 -08001177 up->family = kp->encap_family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 memcpy(&up->saddr, &kp->saddr, sizeof(up->saddr));
1179 up->reqid = kp->reqid;
1180 up->mode = kp->mode;
1181 up->share = kp->share;
1182 up->optional = kp->optional;
1183 up->aalgos = kp->aalgos;
1184 up->ealgos = kp->ealgos;
1185 up->calgos = kp->calgos;
1186 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187
Thomas Grafc0144be2007-08-22 13:55:43 -07001188 return nla_put(skb, XFRMA_TMPL,
1189 sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr, vec);
Trent Jaegerdf718372005-12-13 23:12:27 -08001190}
1191
Serge Hallyn0d681622006-07-24 23:30:44 -07001192static inline int copy_to_user_state_sec_ctx(struct xfrm_state *x, struct sk_buff *skb)
1193{
1194 if (x->security) {
1195 return copy_sec_ctx(x->security, skb);
1196 }
1197 return 0;
1198}
1199
1200static inline int copy_to_user_sec_ctx(struct xfrm_policy *xp, struct sk_buff *skb)
1201{
1202 if (xp->security) {
1203 return copy_sec_ctx(xp->security, skb);
1204 }
1205 return 0;
1206}
Thomas Grafcfbfd452007-08-22 13:57:04 -07001207static inline size_t userpolicy_type_attrsize(void)
1208{
1209#ifdef CONFIG_XFRM_SUB_POLICY
1210 return nla_total_size(sizeof(struct xfrm_userpolicy_type));
1211#else
1212 return 0;
1213#endif
1214}
Serge Hallyn0d681622006-07-24 23:30:44 -07001215
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001216#ifdef CONFIG_XFRM_SUB_POLICY
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001217static int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001218{
Thomas Grafc0144be2007-08-22 13:55:43 -07001219 struct xfrm_userpolicy_type upt = {
1220 .type = type,
1221 };
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001222
Thomas Grafc0144be2007-08-22 13:55:43 -07001223 return nla_put(skb, XFRMA_POLICY_TYPE, sizeof(upt), &upt);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001224}
1225
1226#else
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001227static inline int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001228{
1229 return 0;
1230}
1231#endif
1232
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233static int dump_one_policy(struct xfrm_policy *xp, int dir, int count, void *ptr)
1234{
1235 struct xfrm_dump_info *sp = ptr;
1236 struct xfrm_userpolicy_info *p;
1237 struct sk_buff *in_skb = sp->in_skb;
1238 struct sk_buff *skb = sp->out_skb;
1239 struct nlmsghdr *nlh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240
Thomas Graf79b8b7f2007-08-22 12:46:53 -07001241 nlh = nlmsg_put(skb, NETLINK_CB(in_skb).pid, sp->nlmsg_seq,
1242 XFRM_MSG_NEWPOLICY, sizeof(*p), sp->nlmsg_flags);
1243 if (nlh == NULL)
1244 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245
Thomas Graf7b67c852007-08-22 13:53:52 -07001246 p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 copy_to_user_policy(xp, p, dir);
1248 if (copy_to_user_tmpl(xp, skb) < 0)
1249 goto nlmsg_failure;
Trent Jaegerdf718372005-12-13 23:12:27 -08001250 if (copy_to_user_sec_ctx(xp, skb))
1251 goto nlmsg_failure;
Jamal Hadi Salim1459bb32006-11-20 16:51:22 -08001252 if (copy_to_user_policy_type(xp->type, skb) < 0)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001253 goto nlmsg_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254
Thomas Graf98250692007-08-22 12:47:26 -07001255 nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 return 0;
1257
1258nlmsg_failure:
Thomas Graf98250692007-08-22 12:47:26 -07001259 nlmsg_cancel(skb, nlh);
1260 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261}
1262
Timo Teras4c563f72008-02-28 21:31:08 -08001263static int xfrm_dump_policy_done(struct netlink_callback *cb)
1264{
1265 struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *) &cb->args[1];
1266
1267 xfrm_policy_walk_done(walk);
1268 return 0;
1269}
1270
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271static int xfrm_dump_policy(struct sk_buff *skb, struct netlink_callback *cb)
1272{
Timo Teras4c563f72008-02-28 21:31:08 -08001273 struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *) &cb->args[1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 struct xfrm_dump_info info;
1275
Timo Teras4c563f72008-02-28 21:31:08 -08001276 BUILD_BUG_ON(sizeof(struct xfrm_policy_walk) >
1277 sizeof(cb->args) - sizeof(cb->args[0]));
1278
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 info.in_skb = cb->skb;
1280 info.out_skb = skb;
1281 info.nlmsg_seq = cb->nlh->nlmsg_seq;
1282 info.nlmsg_flags = NLM_F_MULTI;
Timo Teras4c563f72008-02-28 21:31:08 -08001283
1284 if (!cb->args[0]) {
1285 cb->args[0] = 1;
1286 xfrm_policy_walk_init(walk, XFRM_POLICY_TYPE_ANY);
1287 }
1288
1289 (void) xfrm_policy_walk(walk, dump_one_policy, &info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290
1291 return skb->len;
1292}
1293
1294static struct sk_buff *xfrm_policy_netlink(struct sk_buff *in_skb,
1295 struct xfrm_policy *xp,
1296 int dir, u32 seq)
1297{
1298 struct xfrm_dump_info info;
1299 struct sk_buff *skb;
1300
Thomas Graf7deb2262007-08-22 13:57:39 -07001301 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 if (!skb)
1303 return ERR_PTR(-ENOMEM);
1304
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305 info.in_skb = in_skb;
1306 info.out_skb = skb;
1307 info.nlmsg_seq = seq;
1308 info.nlmsg_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309
1310 if (dump_one_policy(xp, dir, 0, &info) < 0) {
1311 kfree_skb(skb);
1312 return NULL;
1313 }
1314
1315 return skb;
1316}
1317
Christoph Hellwig22e70052007-01-02 15:22:30 -08001318static int xfrm_get_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001319 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320{
1321 struct xfrm_policy *xp;
1322 struct xfrm_userpolicy_id *p;
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001323 u8 type = XFRM_POLICY_TYPE_MAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324 int err;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001325 struct km_event c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326 int delete;
1327
Thomas Graf7b67c852007-08-22 13:53:52 -07001328 p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329 delete = nlh->nlmsg_type == XFRM_MSG_DELPOLICY;
1330
Thomas Graf35a7aa02007-08-22 14:00:40 -07001331 err = copy_from_user_policy_type(&type, attrs);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001332 if (err)
1333 return err;
1334
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335 err = verify_policy_dir(p->dir);
1336 if (err)
1337 return err;
1338
1339 if (p->index)
Eric Parisef41aaa2007-03-07 15:37:58 -08001340 xp = xfrm_policy_byid(type, p->dir, p->index, delete, &err);
Trent Jaegerdf718372005-12-13 23:12:27 -08001341 else {
Thomas Graf5424f322007-08-22 14:01:33 -07001342 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
Trent Jaegerdf718372005-12-13 23:12:27 -08001343 struct xfrm_policy tmp;
1344
Thomas Graf35a7aa02007-08-22 14:00:40 -07001345 err = verify_sec_ctx_len(attrs);
Trent Jaegerdf718372005-12-13 23:12:27 -08001346 if (err)
1347 return err;
1348
1349 memset(&tmp, 0, sizeof(struct xfrm_policy));
1350 if (rt) {
Thomas Graf5424f322007-08-22 14:01:33 -07001351 struct xfrm_user_sec_ctx *uctx = nla_data(rt);
Trent Jaegerdf718372005-12-13 23:12:27 -08001352
1353 if ((err = security_xfrm_policy_alloc(&tmp, uctx)))
1354 return err;
1355 }
Eric Parisef41aaa2007-03-07 15:37:58 -08001356 xp = xfrm_policy_bysel_ctx(type, p->dir, &p->sel, tmp.security,
1357 delete, &err);
Trent Jaegerdf718372005-12-13 23:12:27 -08001358 security_xfrm_policy_free(&tmp);
1359 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360 if (xp == NULL)
1361 return -ENOENT;
1362
1363 if (!delete) {
1364 struct sk_buff *resp_skb;
1365
1366 resp_skb = xfrm_policy_netlink(skb, xp, p->dir, nlh->nlmsg_seq);
1367 if (IS_ERR(resp_skb)) {
1368 err = PTR_ERR(resp_skb);
1369 } else {
Thomas Graf082a1ad2007-08-22 13:54:36 -07001370 err = nlmsg_unicast(xfrm_nl, resp_skb,
1371 NETLINK_CB(skb).pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372 }
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001373 } else {
Joy Lattenab5f5e82007-09-17 11:51:22 -07001374 xfrm_audit_policy_delete(xp, err ? 0 : 1,
1375 NETLINK_CB(skb).loginuid,
1376 NETLINK_CB(skb).sid);
David S. Miller13fcfbb2007-02-12 13:53:54 -08001377
1378 if (err != 0)
Catherine Zhangc8c05a82006-06-08 23:39:49 -07001379 goto out;
David S. Miller13fcfbb2007-02-12 13:53:54 -08001380
Herbert Xue7443892005-06-18 22:44:18 -07001381 c.data.byid = p->index;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001382 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001383 c.seq = nlh->nlmsg_seq;
1384 c.pid = nlh->nlmsg_pid;
1385 km_policy_notify(xp, p->dir, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 }
1387
Catherine Zhangc8c05a82006-06-08 23:39:49 -07001388out:
Eric Parisef41aaa2007-03-07 15:37:58 -08001389 xfrm_pol_put(xp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390 return err;
1391}
1392
Christoph Hellwig22e70052007-01-02 15:22:30 -08001393static int xfrm_flush_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001394 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395{
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001396 struct km_event c;
Thomas Graf7b67c852007-08-22 13:53:52 -07001397 struct xfrm_usersa_flush *p = nlmsg_data(nlh);
Joy Latten161a09e2006-11-27 13:11:54 -06001398 struct xfrm_audit audit_info;
Joy Latten4aa2e622007-06-04 19:05:57 -04001399 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400
Joy Latten161a09e2006-11-27 13:11:54 -06001401 audit_info.loginuid = NETLINK_CB(skb).loginuid;
1402 audit_info.secid = NETLINK_CB(skb).sid;
Joy Latten4aa2e622007-06-04 19:05:57 -04001403 err = xfrm_state_flush(p->proto, &audit_info);
1404 if (err)
1405 return err;
Herbert Xubf088672005-06-18 22:44:00 -07001406 c.data.proto = p->proto;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001407 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001408 c.seq = nlh->nlmsg_seq;
1409 c.pid = nlh->nlmsg_pid;
1410 km_state_notify(NULL, &c);
1411
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412 return 0;
1413}
1414
Thomas Graf7deb2262007-08-22 13:57:39 -07001415static inline size_t xfrm_aevent_msgsize(void)
1416{
1417 return NLMSG_ALIGN(sizeof(struct xfrm_aevent_id))
1418 + nla_total_size(sizeof(struct xfrm_replay_state))
1419 + nla_total_size(sizeof(struct xfrm_lifetime_cur))
1420 + nla_total_size(4) /* XFRM_AE_RTHR */
1421 + nla_total_size(4); /* XFRM_AE_ETHR */
1422}
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001423
1424static int build_aevent(struct sk_buff *skb, struct xfrm_state *x, struct km_event *c)
1425{
1426 struct xfrm_aevent_id *id;
1427 struct nlmsghdr *nlh;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001428
Thomas Graf79b8b7f2007-08-22 12:46:53 -07001429 nlh = nlmsg_put(skb, c->pid, c->seq, XFRM_MSG_NEWAE, sizeof(*id), 0);
1430 if (nlh == NULL)
1431 return -EMSGSIZE;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001432
Thomas Graf7b67c852007-08-22 13:53:52 -07001433 id = nlmsg_data(nlh);
Jamal Hadi Salim2b5f6dc2006-12-02 22:22:25 -08001434 memcpy(&id->sa_id.daddr, &x->id.daddr,sizeof(x->id.daddr));
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001435 id->sa_id.spi = x->id.spi;
1436 id->sa_id.family = x->props.family;
1437 id->sa_id.proto = x->id.proto;
Jamal Hadi Salim2b5f6dc2006-12-02 22:22:25 -08001438 memcpy(&id->saddr, &x->props.saddr,sizeof(x->props.saddr));
1439 id->reqid = x->props.reqid;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001440 id->flags = c->data.aevent;
1441
Thomas Grafc0144be2007-08-22 13:55:43 -07001442 NLA_PUT(skb, XFRMA_REPLAY_VAL, sizeof(x->replay), &x->replay);
1443 NLA_PUT(skb, XFRMA_LTIME_VAL, sizeof(x->curlft), &x->curlft);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001444
Thomas Grafc0144be2007-08-22 13:55:43 -07001445 if (id->flags & XFRM_AE_RTHR)
1446 NLA_PUT_U32(skb, XFRMA_REPLAY_THRESH, x->replay_maxdiff);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001447
Thomas Grafc0144be2007-08-22 13:55:43 -07001448 if (id->flags & XFRM_AE_ETHR)
1449 NLA_PUT_U32(skb, XFRMA_ETIMER_THRESH,
1450 x->replay_maxage * 10 / HZ);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001451
Thomas Graf98250692007-08-22 12:47:26 -07001452 return nlmsg_end(skb, nlh);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001453
Thomas Grafc0144be2007-08-22 13:55:43 -07001454nla_put_failure:
Thomas Graf98250692007-08-22 12:47:26 -07001455 nlmsg_cancel(skb, nlh);
1456 return -EMSGSIZE;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001457}
1458
Christoph Hellwig22e70052007-01-02 15:22:30 -08001459static int xfrm_get_ae(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001460 struct nlattr **attrs)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001461{
1462 struct xfrm_state *x;
1463 struct sk_buff *r_skb;
1464 int err;
1465 struct km_event c;
Thomas Graf7b67c852007-08-22 13:53:52 -07001466 struct xfrm_aevent_id *p = nlmsg_data(nlh);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001467 struct xfrm_usersa_id *id = &p->sa_id;
1468
Thomas Graf7deb2262007-08-22 13:57:39 -07001469 r_skb = nlmsg_new(xfrm_aevent_msgsize(), GFP_ATOMIC);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001470 if (r_skb == NULL)
1471 return -ENOMEM;
1472
1473 x = xfrm_state_lookup(&id->daddr, id->spi, id->proto, id->family);
1474 if (x == NULL) {
Patrick McHardyb08d5842007-02-27 09:57:37 -08001475 kfree_skb(r_skb);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001476 return -ESRCH;
1477 }
1478
1479 /*
1480 * XXX: is this lock really needed - none of the other
1481 * gets lock (the concern is things getting updated
1482 * while we are still reading) - jhs
1483 */
1484 spin_lock_bh(&x->lock);
1485 c.data.aevent = p->flags;
1486 c.seq = nlh->nlmsg_seq;
1487 c.pid = nlh->nlmsg_pid;
1488
1489 if (build_aevent(r_skb, x, &c) < 0)
1490 BUG();
Thomas Graf082a1ad2007-08-22 13:54:36 -07001491 err = nlmsg_unicast(xfrm_nl, r_skb, NETLINK_CB(skb).pid);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001492 spin_unlock_bh(&x->lock);
1493 xfrm_state_put(x);
1494 return err;
1495}
1496
Christoph Hellwig22e70052007-01-02 15:22:30 -08001497static int xfrm_new_ae(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001498 struct nlattr **attrs)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001499{
1500 struct xfrm_state *x;
1501 struct km_event c;
1502 int err = - EINVAL;
Thomas Graf7b67c852007-08-22 13:53:52 -07001503 struct xfrm_aevent_id *p = nlmsg_data(nlh);
Thomas Graf5424f322007-08-22 14:01:33 -07001504 struct nlattr *rp = attrs[XFRMA_REPLAY_VAL];
1505 struct nlattr *lt = attrs[XFRMA_LTIME_VAL];
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001506
1507 if (!lt && !rp)
1508 return err;
1509
1510 /* pedantic mode - thou shalt sayeth replaceth */
1511 if (!(nlh->nlmsg_flags&NLM_F_REPLACE))
1512 return err;
1513
1514 x = xfrm_state_lookup(&p->sa_id.daddr, p->sa_id.spi, p->sa_id.proto, p->sa_id.family);
1515 if (x == NULL)
1516 return -ESRCH;
1517
1518 if (x->km.state != XFRM_STATE_VALID)
1519 goto out;
1520
1521 spin_lock_bh(&x->lock);
Thomas Graf35a7aa02007-08-22 14:00:40 -07001522 xfrm_update_ae_params(x, attrs);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001523 spin_unlock_bh(&x->lock);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001524
1525 c.event = nlh->nlmsg_type;
1526 c.seq = nlh->nlmsg_seq;
1527 c.pid = nlh->nlmsg_pid;
1528 c.data.aevent = XFRM_AE_CU;
1529 km_state_notify(x, &c);
1530 err = 0;
1531out:
1532 xfrm_state_put(x);
1533 return err;
1534}
1535
Christoph Hellwig22e70052007-01-02 15:22:30 -08001536static int xfrm_flush_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001537 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538{
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001539 struct km_event c;
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001540 u8 type = XFRM_POLICY_TYPE_MAIN;
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001541 int err;
Joy Latten161a09e2006-11-27 13:11:54 -06001542 struct xfrm_audit audit_info;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001543
Thomas Graf35a7aa02007-08-22 14:00:40 -07001544 err = copy_from_user_policy_type(&type, attrs);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001545 if (err)
1546 return err;
1547
Joy Latten161a09e2006-11-27 13:11:54 -06001548 audit_info.loginuid = NETLINK_CB(skb).loginuid;
1549 audit_info.secid = NETLINK_CB(skb).sid;
Joy Latten4aa2e622007-06-04 19:05:57 -04001550 err = xfrm_policy_flush(type, &audit_info);
1551 if (err)
1552 return err;
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001553 c.data.type = type;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001554 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001555 c.seq = nlh->nlmsg_seq;
1556 c.pid = nlh->nlmsg_pid;
1557 km_policy_notify(NULL, 0, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558 return 0;
1559}
1560
Christoph Hellwig22e70052007-01-02 15:22:30 -08001561static int xfrm_add_pol_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001562 struct nlattr **attrs)
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001563{
1564 struct xfrm_policy *xp;
Thomas Graf7b67c852007-08-22 13:53:52 -07001565 struct xfrm_user_polexpire *up = nlmsg_data(nlh);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001566 struct xfrm_userpolicy_info *p = &up->pol;
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001567 u8 type = XFRM_POLICY_TYPE_MAIN;
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001568 int err = -ENOENT;
1569
Thomas Graf35a7aa02007-08-22 14:00:40 -07001570 err = copy_from_user_policy_type(&type, attrs);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001571 if (err)
1572 return err;
1573
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001574 if (p->index)
Eric Parisef41aaa2007-03-07 15:37:58 -08001575 xp = xfrm_policy_byid(type, p->dir, p->index, 0, &err);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001576 else {
Thomas Graf5424f322007-08-22 14:01:33 -07001577 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001578 struct xfrm_policy tmp;
1579
Thomas Graf35a7aa02007-08-22 14:00:40 -07001580 err = verify_sec_ctx_len(attrs);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001581 if (err)
1582 return err;
1583
1584 memset(&tmp, 0, sizeof(struct xfrm_policy));
1585 if (rt) {
Thomas Graf5424f322007-08-22 14:01:33 -07001586 struct xfrm_user_sec_ctx *uctx = nla_data(rt);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001587
1588 if ((err = security_xfrm_policy_alloc(&tmp, uctx)))
1589 return err;
1590 }
Eric Parisef41aaa2007-03-07 15:37:58 -08001591 xp = xfrm_policy_bysel_ctx(type, p->dir, &p->sel, tmp.security,
1592 0, &err);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001593 security_xfrm_policy_free(&tmp);
1594 }
1595
1596 if (xp == NULL)
Eric Parisef41aaa2007-03-07 15:37:58 -08001597 return -ENOENT;
1598 read_lock(&xp->lock);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001599 if (xp->dead) {
1600 read_unlock(&xp->lock);
1601 goto out;
1602 }
1603
1604 read_unlock(&xp->lock);
1605 err = 0;
1606 if (up->hard) {
1607 xfrm_policy_delete(xp, p->dir);
Joy Lattenab5f5e82007-09-17 11:51:22 -07001608 xfrm_audit_policy_delete(xp, 1, NETLINK_CB(skb).loginuid,
1609 NETLINK_CB(skb).sid);
Joy Latten161a09e2006-11-27 13:11:54 -06001610
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001611 } else {
1612 // reset the timers here?
1613 printk("Dont know what to do with soft policy expire\n");
1614 }
1615 km_policy_expired(xp, p->dir, up->hard, current->pid);
1616
1617out:
1618 xfrm_pol_put(xp);
1619 return err;
1620}
1621
Christoph Hellwig22e70052007-01-02 15:22:30 -08001622static int xfrm_add_sa_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001623 struct nlattr **attrs)
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001624{
1625 struct xfrm_state *x;
1626 int err;
Thomas Graf7b67c852007-08-22 13:53:52 -07001627 struct xfrm_user_expire *ue = nlmsg_data(nlh);
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001628 struct xfrm_usersa_info *p = &ue->state;
1629
1630 x = xfrm_state_lookup(&p->id.daddr, p->id.spi, p->id.proto, p->family);
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001631
David S. Miller3a765aa2007-02-26 14:52:21 -08001632 err = -ENOENT;
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001633 if (x == NULL)
1634 return err;
1635
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001636 spin_lock_bh(&x->lock);
David S. Miller3a765aa2007-02-26 14:52:21 -08001637 err = -EINVAL;
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001638 if (x->km.state != XFRM_STATE_VALID)
1639 goto out;
1640 km_state_expired(x, ue->hard, current->pid);
1641
Joy Latten161a09e2006-11-27 13:11:54 -06001642 if (ue->hard) {
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001643 __xfrm_state_delete(x);
Joy Lattenab5f5e82007-09-17 11:51:22 -07001644 xfrm_audit_state_delete(x, 1, NETLINK_CB(skb).loginuid,
1645 NETLINK_CB(skb).sid);
Joy Latten161a09e2006-11-27 13:11:54 -06001646 }
David S. Miller3a765aa2007-02-26 14:52:21 -08001647 err = 0;
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001648out:
1649 spin_unlock_bh(&x->lock);
1650 xfrm_state_put(x);
1651 return err;
1652}
1653
Christoph Hellwig22e70052007-01-02 15:22:30 -08001654static int xfrm_add_acquire(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001655 struct nlattr **attrs)
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001656{
1657 struct xfrm_policy *xp;
1658 struct xfrm_user_tmpl *ut;
1659 int i;
Thomas Graf5424f322007-08-22 14:01:33 -07001660 struct nlattr *rt = attrs[XFRMA_TMPL];
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001661
Thomas Graf7b67c852007-08-22 13:53:52 -07001662 struct xfrm_user_acquire *ua = nlmsg_data(nlh);
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001663 struct xfrm_state *x = xfrm_state_alloc();
1664 int err = -ENOMEM;
1665
1666 if (!x)
1667 return err;
1668
1669 err = verify_newpolicy_info(&ua->policy);
1670 if (err) {
1671 printk("BAD policy passed\n");
1672 kfree(x);
1673 return err;
1674 }
1675
1676 /* build an XP */
Thomas Graf5424f322007-08-22 14:01:33 -07001677 xp = xfrm_policy_construct(&ua->policy, attrs, &err);
David S. Millerb4ad86bf2006-12-03 19:19:26 -08001678 if (!xp) {
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001679 kfree(x);
1680 return err;
1681 }
1682
1683 memcpy(&x->id, &ua->id, sizeof(ua->id));
1684 memcpy(&x->props.saddr, &ua->saddr, sizeof(ua->saddr));
1685 memcpy(&x->sel, &ua->sel, sizeof(ua->sel));
1686
Thomas Graf5424f322007-08-22 14:01:33 -07001687 ut = nla_data(rt);
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001688 /* extract the templates and for each call km_key */
1689 for (i = 0; i < xp->xfrm_nr; i++, ut++) {
1690 struct xfrm_tmpl *t = &xp->xfrm_vec[i];
1691 memcpy(&x->id, &t->id, sizeof(x->id));
1692 x->props.mode = t->mode;
1693 x->props.reqid = t->reqid;
1694 x->props.family = ut->family;
1695 t->aalgos = ua->aalgos;
1696 t->ealgos = ua->ealgos;
1697 t->calgos = ua->calgos;
1698 err = km_query(x, t, xp);
1699
1700 }
1701
1702 kfree(x);
1703 kfree(xp);
1704
1705 return 0;
1706}
1707
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001708#ifdef CONFIG_XFRM_MIGRATE
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001709static int copy_from_user_migrate(struct xfrm_migrate *ma,
Thomas Graf5424f322007-08-22 14:01:33 -07001710 struct nlattr **attrs, int *num)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001711{
Thomas Graf5424f322007-08-22 14:01:33 -07001712 struct nlattr *rt = attrs[XFRMA_MIGRATE];
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001713 struct xfrm_user_migrate *um;
1714 int i, num_migrate;
1715
Thomas Graf5424f322007-08-22 14:01:33 -07001716 um = nla_data(rt);
1717 num_migrate = nla_len(rt) / sizeof(*um);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001718
1719 if (num_migrate <= 0 || num_migrate > XFRM_MAX_DEPTH)
1720 return -EINVAL;
1721
1722 for (i = 0; i < num_migrate; i++, um++, ma++) {
1723 memcpy(&ma->old_daddr, &um->old_daddr, sizeof(ma->old_daddr));
1724 memcpy(&ma->old_saddr, &um->old_saddr, sizeof(ma->old_saddr));
1725 memcpy(&ma->new_daddr, &um->new_daddr, sizeof(ma->new_daddr));
1726 memcpy(&ma->new_saddr, &um->new_saddr, sizeof(ma->new_saddr));
1727
1728 ma->proto = um->proto;
1729 ma->mode = um->mode;
1730 ma->reqid = um->reqid;
1731
1732 ma->old_family = um->old_family;
1733 ma->new_family = um->new_family;
1734 }
1735
1736 *num = i;
1737 return 0;
1738}
1739
1740static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001741 struct nlattr **attrs)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001742{
Thomas Graf7b67c852007-08-22 13:53:52 -07001743 struct xfrm_userpolicy_id *pi = nlmsg_data(nlh);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001744 struct xfrm_migrate m[XFRM_MAX_DEPTH];
1745 u8 type;
1746 int err;
1747 int n = 0;
1748
Thomas Graf35a7aa02007-08-22 14:00:40 -07001749 if (attrs[XFRMA_MIGRATE] == NULL)
Thomas Grafcf5cb792007-08-22 13:59:04 -07001750 return -EINVAL;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001751
Thomas Graf5424f322007-08-22 14:01:33 -07001752 err = copy_from_user_policy_type(&type, attrs);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001753 if (err)
1754 return err;
1755
1756 err = copy_from_user_migrate((struct xfrm_migrate *)m,
Thomas Graf5424f322007-08-22 14:01:33 -07001757 attrs, &n);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001758 if (err)
1759 return err;
1760
1761 if (!n)
1762 return 0;
1763
1764 xfrm_migrate(&pi->sel, pi->dir, type, m, n);
1765
1766 return 0;
1767}
1768#else
1769static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001770 struct nlattr **attrs)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001771{
1772 return -ENOPROTOOPT;
1773}
1774#endif
1775
1776#ifdef CONFIG_XFRM_MIGRATE
1777static int copy_to_user_migrate(struct xfrm_migrate *m, struct sk_buff *skb)
1778{
1779 struct xfrm_user_migrate um;
1780
1781 memset(&um, 0, sizeof(um));
1782 um.proto = m->proto;
1783 um.mode = m->mode;
1784 um.reqid = m->reqid;
1785 um.old_family = m->old_family;
1786 memcpy(&um.old_daddr, &m->old_daddr, sizeof(um.old_daddr));
1787 memcpy(&um.old_saddr, &m->old_saddr, sizeof(um.old_saddr));
1788 um.new_family = m->new_family;
1789 memcpy(&um.new_daddr, &m->new_daddr, sizeof(um.new_daddr));
1790 memcpy(&um.new_saddr, &m->new_saddr, sizeof(um.new_saddr));
1791
Thomas Grafc0144be2007-08-22 13:55:43 -07001792 return nla_put(skb, XFRMA_MIGRATE, sizeof(um), &um);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001793}
1794
Thomas Graf7deb2262007-08-22 13:57:39 -07001795static inline size_t xfrm_migrate_msgsize(int num_migrate)
1796{
1797 return NLMSG_ALIGN(sizeof(struct xfrm_userpolicy_id))
1798 + nla_total_size(sizeof(struct xfrm_user_migrate) * num_migrate)
1799 + userpolicy_type_attrsize();
1800}
1801
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001802static int build_migrate(struct sk_buff *skb, struct xfrm_migrate *m,
1803 int num_migrate, struct xfrm_selector *sel,
1804 u8 dir, u8 type)
1805{
1806 struct xfrm_migrate *mp;
1807 struct xfrm_userpolicy_id *pol_id;
1808 struct nlmsghdr *nlh;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001809 int i;
1810
Thomas Graf79b8b7f2007-08-22 12:46:53 -07001811 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_MIGRATE, sizeof(*pol_id), 0);
1812 if (nlh == NULL)
1813 return -EMSGSIZE;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001814
Thomas Graf7b67c852007-08-22 13:53:52 -07001815 pol_id = nlmsg_data(nlh);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001816 /* copy data from selector, dir, and type to the pol_id */
1817 memset(pol_id, 0, sizeof(*pol_id));
1818 memcpy(&pol_id->sel, sel, sizeof(pol_id->sel));
1819 pol_id->dir = dir;
1820
1821 if (copy_to_user_policy_type(type, skb) < 0)
1822 goto nlmsg_failure;
1823
1824 for (i = 0, mp = m ; i < num_migrate; i++, mp++) {
1825 if (copy_to_user_migrate(mp, skb) < 0)
1826 goto nlmsg_failure;
1827 }
1828
Thomas Graf98250692007-08-22 12:47:26 -07001829 return nlmsg_end(skb, nlh);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001830nlmsg_failure:
Thomas Graf98250692007-08-22 12:47:26 -07001831 nlmsg_cancel(skb, nlh);
1832 return -EMSGSIZE;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001833}
1834
1835static int xfrm_send_migrate(struct xfrm_selector *sel, u8 dir, u8 type,
1836 struct xfrm_migrate *m, int num_migrate)
1837{
1838 struct sk_buff *skb;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001839
Thomas Graf7deb2262007-08-22 13:57:39 -07001840 skb = nlmsg_new(xfrm_migrate_msgsize(num_migrate), GFP_ATOMIC);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001841 if (skb == NULL)
1842 return -ENOMEM;
1843
1844 /* build migrate */
1845 if (build_migrate(skb, m, num_migrate, sel, dir, type) < 0)
1846 BUG();
1847
Thomas Graf082a1ad2007-08-22 13:54:36 -07001848 return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_MIGRATE, GFP_ATOMIC);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001849}
1850#else
1851static int xfrm_send_migrate(struct xfrm_selector *sel, u8 dir, u8 type,
1852 struct xfrm_migrate *m, int num_migrate)
1853{
1854 return -ENOPROTOOPT;
1855}
1856#endif
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001857
Thomas Grafa7bd9a42007-08-22 13:58:18 -07001858#define XMSGSIZE(type) sizeof(struct type)
Thomas Graf492b5582005-05-03 14:26:40 -07001859
1860static const int xfrm_msg_min[XFRM_NR_MSGTYPES] = {
1861 [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
1862 [XFRM_MSG_DELSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
1863 [XFRM_MSG_GETSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
1864 [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
1865 [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
1866 [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
1867 [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userspi_info),
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001868 [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_acquire),
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001869 [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_expire),
Thomas Graf492b5582005-05-03 14:26:40 -07001870 [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
1871 [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001872 [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_polexpire),
Thomas Graf492b5582005-05-03 14:26:40 -07001873 [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_flush),
Thomas Grafa7bd9a42007-08-22 13:58:18 -07001874 [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = 0,
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001875 [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
1876 [XFRM_MSG_GETAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07001877 [XFRM_MSG_REPORT - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_report),
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001878 [XFRM_MSG_MIGRATE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
Thomas Grafa7bd9a42007-08-22 13:58:18 -07001879 [XFRM_MSG_GETSADINFO - XFRM_MSG_BASE] = sizeof(u32),
1880 [XFRM_MSG_GETSPDINFO - XFRM_MSG_BASE] = sizeof(u32),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881};
1882
Thomas Graf492b5582005-05-03 14:26:40 -07001883#undef XMSGSIZE
1884
Thomas Grafcf5cb792007-08-22 13:59:04 -07001885static const struct nla_policy xfrma_policy[XFRMA_MAX+1] = {
Herbert Xu1a6509d2008-01-28 19:37:29 -08001886 [XFRMA_ALG_AEAD] = { .len = sizeof(struct xfrm_algo_aead) },
Thomas Grafcf5cb792007-08-22 13:59:04 -07001887 [XFRMA_ALG_AUTH] = { .len = sizeof(struct xfrm_algo) },
1888 [XFRMA_ALG_CRYPT] = { .len = sizeof(struct xfrm_algo) },
1889 [XFRMA_ALG_COMP] = { .len = sizeof(struct xfrm_algo) },
1890 [XFRMA_ENCAP] = { .len = sizeof(struct xfrm_encap_tmpl) },
1891 [XFRMA_TMPL] = { .len = sizeof(struct xfrm_user_tmpl) },
1892 [XFRMA_SEC_CTX] = { .len = sizeof(struct xfrm_sec_ctx) },
1893 [XFRMA_LTIME_VAL] = { .len = sizeof(struct xfrm_lifetime_cur) },
1894 [XFRMA_REPLAY_VAL] = { .len = sizeof(struct xfrm_replay_state) },
1895 [XFRMA_REPLAY_THRESH] = { .type = NLA_U32 },
1896 [XFRMA_ETIMER_THRESH] = { .type = NLA_U32 },
1897 [XFRMA_SRCADDR] = { .len = sizeof(xfrm_address_t) },
1898 [XFRMA_COADDR] = { .len = sizeof(xfrm_address_t) },
1899 [XFRMA_POLICY_TYPE] = { .len = sizeof(struct xfrm_userpolicy_type)},
1900 [XFRMA_MIGRATE] = { .len = sizeof(struct xfrm_user_migrate) },
1901};
1902
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903static struct xfrm_link {
Thomas Graf5424f322007-08-22 14:01:33 -07001904 int (*doit)(struct sk_buff *, struct nlmsghdr *, struct nlattr **);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905 int (*dump)(struct sk_buff *, struct netlink_callback *);
Timo Teras4c563f72008-02-28 21:31:08 -08001906 int (*done)(struct netlink_callback *);
Thomas Graf492b5582005-05-03 14:26:40 -07001907} xfrm_dispatch[XFRM_NR_MSGTYPES] = {
1908 [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa },
1909 [XFRM_MSG_DELSA - XFRM_MSG_BASE] = { .doit = xfrm_del_sa },
1910 [XFRM_MSG_GETSA - XFRM_MSG_BASE] = { .doit = xfrm_get_sa,
Timo Teras4c563f72008-02-28 21:31:08 -08001911 .dump = xfrm_dump_sa,
1912 .done = xfrm_dump_sa_done },
Thomas Graf492b5582005-05-03 14:26:40 -07001913 [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy },
1914 [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy },
1915 [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy,
Timo Teras4c563f72008-02-28 21:31:08 -08001916 .dump = xfrm_dump_policy,
1917 .done = xfrm_dump_policy_done },
Thomas Graf492b5582005-05-03 14:26:40 -07001918 [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = { .doit = xfrm_alloc_userspi },
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001919 [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_acquire },
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001920 [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_sa_expire },
Thomas Graf492b5582005-05-03 14:26:40 -07001921 [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy },
1922 [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa },
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001923 [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_pol_expire},
Thomas Graf492b5582005-05-03 14:26:40 -07001924 [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = { .doit = xfrm_flush_sa },
1925 [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_flush_policy },
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001926 [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = { .doit = xfrm_new_ae },
1927 [XFRM_MSG_GETAE - XFRM_MSG_BASE] = { .doit = xfrm_get_ae },
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001928 [XFRM_MSG_MIGRATE - XFRM_MSG_BASE] = { .doit = xfrm_do_migrate },
Jamal Hadi Salim566ec032007-04-26 14:12:15 -07001929 [XFRM_MSG_GETSADINFO - XFRM_MSG_BASE] = { .doit = xfrm_get_sadinfo },
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -07001930 [XFRM_MSG_GETSPDINFO - XFRM_MSG_BASE] = { .doit = xfrm_get_spdinfo },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931};
1932
Thomas Graf1d00a4e2007-03-22 23:30:12 -07001933static int xfrm_user_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934{
Thomas Graf35a7aa02007-08-22 14:00:40 -07001935 struct nlattr *attrs[XFRMA_MAX+1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936 struct xfrm_link *link;
Thomas Grafa7bd9a42007-08-22 13:58:18 -07001937 int type, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939 type = nlh->nlmsg_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940 if (type > XFRM_MSG_MAX)
Thomas Graf1d00a4e2007-03-22 23:30:12 -07001941 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942
1943 type -= XFRM_MSG_BASE;
1944 link = &xfrm_dispatch[type];
1945
1946 /* All operations require privileges, even GET */
Thomas Graf1d00a4e2007-03-22 23:30:12 -07001947 if (security_netlink_recv(skb, CAP_NET_ADMIN))
1948 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949
Thomas Graf492b5582005-05-03 14:26:40 -07001950 if ((type == (XFRM_MSG_GETSA - XFRM_MSG_BASE) ||
1951 type == (XFRM_MSG_GETPOLICY - XFRM_MSG_BASE)) &&
1952 (nlh->nlmsg_flags & NLM_F_DUMP)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953 if (link->dump == NULL)
Thomas Graf1d00a4e2007-03-22 23:30:12 -07001954 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955
Timo Teras4c563f72008-02-28 21:31:08 -08001956 return netlink_dump_start(xfrm_nl, skb, nlh, link->dump, link->done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957 }
1958
Thomas Graf35a7aa02007-08-22 14:00:40 -07001959 err = nlmsg_parse(nlh, xfrm_msg_min[type], attrs, XFRMA_MAX,
Thomas Grafcf5cb792007-08-22 13:59:04 -07001960 xfrma_policy);
Thomas Grafa7bd9a42007-08-22 13:58:18 -07001961 if (err < 0)
1962 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963
1964 if (link->doit == NULL)
Thomas Graf1d00a4e2007-03-22 23:30:12 -07001965 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966
Thomas Graf5424f322007-08-22 14:01:33 -07001967 return link->doit(skb, nlh, attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968}
1969
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07001970static void xfrm_netlink_rcv(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971{
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07001972 mutex_lock(&xfrm_cfg_mutex);
1973 netlink_rcv_skb(skb, &xfrm_user_rcv_msg);
1974 mutex_unlock(&xfrm_cfg_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975}
1976
Thomas Graf7deb2262007-08-22 13:57:39 -07001977static inline size_t xfrm_expire_msgsize(void)
1978{
1979 return NLMSG_ALIGN(sizeof(struct xfrm_user_expire));
1980}
1981
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001982static int build_expire(struct sk_buff *skb, struct xfrm_state *x, struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983{
1984 struct xfrm_user_expire *ue;
1985 struct nlmsghdr *nlh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986
Thomas Graf79b8b7f2007-08-22 12:46:53 -07001987 nlh = nlmsg_put(skb, c->pid, 0, XFRM_MSG_EXPIRE, sizeof(*ue), 0);
1988 if (nlh == NULL)
1989 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990
Thomas Graf7b67c852007-08-22 13:53:52 -07001991 ue = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992 copy_to_user_state(x, &ue->state);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001993 ue->hard = (c->data.hard != 0) ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994
Thomas Graf98250692007-08-22 12:47:26 -07001995 return nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996}
1997
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001998static int xfrm_exp_state_notify(struct xfrm_state *x, struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999{
2000 struct sk_buff *skb;
2001
Thomas Graf7deb2262007-08-22 13:57:39 -07002002 skb = nlmsg_new(xfrm_expire_msgsize(), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003 if (skb == NULL)
2004 return -ENOMEM;
2005
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002006 if (build_expire(skb, x, c) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007 BUG();
2008
Thomas Graf082a1ad2007-08-22 13:54:36 -07002009 return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_EXPIRE, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010}
2011
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002012static int xfrm_aevent_state_notify(struct xfrm_state *x, struct km_event *c)
2013{
2014 struct sk_buff *skb;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002015
Thomas Graf7deb2262007-08-22 13:57:39 -07002016 skb = nlmsg_new(xfrm_aevent_msgsize(), GFP_ATOMIC);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002017 if (skb == NULL)
2018 return -ENOMEM;
2019
2020 if (build_aevent(skb, x, c) < 0)
2021 BUG();
2022
Thomas Graf082a1ad2007-08-22 13:54:36 -07002023 return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_AEVENTS, GFP_ATOMIC);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002024}
2025
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002026static int xfrm_notify_sa_flush(struct km_event *c)
2027{
2028 struct xfrm_usersa_flush *p;
2029 struct nlmsghdr *nlh;
2030 struct sk_buff *skb;
Thomas Graf7deb2262007-08-22 13:57:39 -07002031 int len = NLMSG_ALIGN(sizeof(struct xfrm_usersa_flush));
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002032
Thomas Graf7deb2262007-08-22 13:57:39 -07002033 skb = nlmsg_new(len, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002034 if (skb == NULL)
2035 return -ENOMEM;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002036
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002037 nlh = nlmsg_put(skb, c->pid, c->seq, XFRM_MSG_FLUSHSA, sizeof(*p), 0);
2038 if (nlh == NULL) {
2039 kfree_skb(skb);
2040 return -EMSGSIZE;
2041 }
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002042
Thomas Graf7b67c852007-08-22 13:53:52 -07002043 p = nlmsg_data(nlh);
Herbert Xubf088672005-06-18 22:44:00 -07002044 p->proto = c->data.proto;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002045
Thomas Graf98250692007-08-22 12:47:26 -07002046 nlmsg_end(skb, nlh);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002047
Thomas Graf082a1ad2007-08-22 13:54:36 -07002048 return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_SA, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002049}
2050
Thomas Graf7deb2262007-08-22 13:57:39 -07002051static inline size_t xfrm_sa_len(struct xfrm_state *x)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002052{
Thomas Graf7deb2262007-08-22 13:57:39 -07002053 size_t l = 0;
Herbert Xu1a6509d2008-01-28 19:37:29 -08002054 if (x->aead)
2055 l += nla_total_size(aead_len(x->aead));
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002056 if (x->aalg)
Eric Dumazet0f99be02008-01-08 23:39:06 -08002057 l += nla_total_size(xfrm_alg_len(x->aalg));
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002058 if (x->ealg)
Eric Dumazet0f99be02008-01-08 23:39:06 -08002059 l += nla_total_size(xfrm_alg_len(x->ealg));
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002060 if (x->calg)
Thomas Graf7deb2262007-08-22 13:57:39 -07002061 l += nla_total_size(sizeof(*x->calg));
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002062 if (x->encap)
Thomas Graf7deb2262007-08-22 13:57:39 -07002063 l += nla_total_size(sizeof(*x->encap));
Herbert Xu68325d32007-10-09 13:30:57 -07002064 if (x->security)
2065 l += nla_total_size(sizeof(struct xfrm_user_sec_ctx) +
2066 x->security->ctx_len);
2067 if (x->coaddr)
2068 l += nla_total_size(sizeof(*x->coaddr));
2069
Herbert Xud26f3982007-11-13 21:47:08 -08002070 /* Must count x->lastused as it may become non-zero behind our back. */
2071 l += nla_total_size(sizeof(u64));
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002072
2073 return l;
2074}
2075
2076static int xfrm_notify_sa(struct xfrm_state *x, struct km_event *c)
2077{
2078 struct xfrm_usersa_info *p;
Herbert Xu0603eac2005-06-18 22:54:36 -07002079 struct xfrm_usersa_id *id;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002080 struct nlmsghdr *nlh;
2081 struct sk_buff *skb;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002082 int len = xfrm_sa_len(x);
Herbert Xu0603eac2005-06-18 22:54:36 -07002083 int headlen;
2084
2085 headlen = sizeof(*p);
2086 if (c->event == XFRM_MSG_DELSA) {
Thomas Graf7deb2262007-08-22 13:57:39 -07002087 len += nla_total_size(headlen);
Herbert Xu0603eac2005-06-18 22:54:36 -07002088 headlen = sizeof(*id);
2089 }
Thomas Graf7deb2262007-08-22 13:57:39 -07002090 len += NLMSG_ALIGN(headlen);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002091
Thomas Graf7deb2262007-08-22 13:57:39 -07002092 skb = nlmsg_new(len, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002093 if (skb == NULL)
2094 return -ENOMEM;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002095
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002096 nlh = nlmsg_put(skb, c->pid, c->seq, c->event, headlen, 0);
2097 if (nlh == NULL)
Thomas Grafc0144be2007-08-22 13:55:43 -07002098 goto nla_put_failure;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002099
Thomas Graf7b67c852007-08-22 13:53:52 -07002100 p = nlmsg_data(nlh);
Herbert Xu0603eac2005-06-18 22:54:36 -07002101 if (c->event == XFRM_MSG_DELSA) {
Thomas Grafc0144be2007-08-22 13:55:43 -07002102 struct nlattr *attr;
2103
Thomas Graf7b67c852007-08-22 13:53:52 -07002104 id = nlmsg_data(nlh);
Herbert Xu0603eac2005-06-18 22:54:36 -07002105 memcpy(&id->daddr, &x->id.daddr, sizeof(id->daddr));
2106 id->spi = x->id.spi;
2107 id->family = x->props.family;
2108 id->proto = x->id.proto;
2109
Thomas Grafc0144be2007-08-22 13:55:43 -07002110 attr = nla_reserve(skb, XFRMA_SA, sizeof(*p));
2111 if (attr == NULL)
2112 goto nla_put_failure;
2113
2114 p = nla_data(attr);
Herbert Xu0603eac2005-06-18 22:54:36 -07002115 }
2116
Herbert Xu68325d32007-10-09 13:30:57 -07002117 if (copy_to_user_state_extra(x, p, skb))
2118 goto nla_put_failure;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002119
Thomas Graf98250692007-08-22 12:47:26 -07002120 nlmsg_end(skb, nlh);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002121
Thomas Graf082a1ad2007-08-22 13:54:36 -07002122 return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_SA, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002123
Thomas Grafc0144be2007-08-22 13:55:43 -07002124nla_put_failure:
Herbert Xu68325d32007-10-09 13:30:57 -07002125 /* Somebody screwed up with xfrm_sa_len! */
2126 WARN_ON(1);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002127 kfree_skb(skb);
2128 return -1;
2129}
2130
2131static int xfrm_send_state_notify(struct xfrm_state *x, struct km_event *c)
2132{
2133
2134 switch (c->event) {
Herbert Xuf60f6b82005-06-18 22:44:37 -07002135 case XFRM_MSG_EXPIRE:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002136 return xfrm_exp_state_notify(x, c);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002137 case XFRM_MSG_NEWAE:
2138 return xfrm_aevent_state_notify(x, c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07002139 case XFRM_MSG_DELSA:
2140 case XFRM_MSG_UPDSA:
2141 case XFRM_MSG_NEWSA:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002142 return xfrm_notify_sa(x, c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07002143 case XFRM_MSG_FLUSHSA:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002144 return xfrm_notify_sa_flush(c);
2145 default:
2146 printk("xfrm_user: Unknown SA event %d\n", c->event);
2147 break;
2148 }
2149
2150 return 0;
2151
2152}
2153
Thomas Graf7deb2262007-08-22 13:57:39 -07002154static inline size_t xfrm_acquire_msgsize(struct xfrm_state *x,
2155 struct xfrm_policy *xp)
2156{
2157 return NLMSG_ALIGN(sizeof(struct xfrm_user_acquire))
2158 + nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr)
2159 + nla_total_size(xfrm_user_sec_ctx_size(x->security))
2160 + userpolicy_type_attrsize();
2161}
2162
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163static int build_acquire(struct sk_buff *skb, struct xfrm_state *x,
2164 struct xfrm_tmpl *xt, struct xfrm_policy *xp,
2165 int dir)
2166{
2167 struct xfrm_user_acquire *ua;
2168 struct nlmsghdr *nlh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169 __u32 seq = xfrm_get_acqseq();
2170
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002171 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_ACQUIRE, sizeof(*ua), 0);
2172 if (nlh == NULL)
2173 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174
Thomas Graf7b67c852007-08-22 13:53:52 -07002175 ua = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176 memcpy(&ua->id, &x->id, sizeof(ua->id));
2177 memcpy(&ua->saddr, &x->props.saddr, sizeof(ua->saddr));
2178 memcpy(&ua->sel, &x->sel, sizeof(ua->sel));
2179 copy_to_user_policy(xp, &ua->policy, dir);
2180 ua->aalgos = xt->aalgos;
2181 ua->ealgos = xt->ealgos;
2182 ua->calgos = xt->calgos;
2183 ua->seq = x->km.seq = seq;
2184
2185 if (copy_to_user_tmpl(xp, skb) < 0)
2186 goto nlmsg_failure;
Serge Hallyn0d681622006-07-24 23:30:44 -07002187 if (copy_to_user_state_sec_ctx(x, skb))
Trent Jaegerdf718372005-12-13 23:12:27 -08002188 goto nlmsg_failure;
Jamal Hadi Salim1459bb32006-11-20 16:51:22 -08002189 if (copy_to_user_policy_type(xp->type, skb) < 0)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07002190 goto nlmsg_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191
Thomas Graf98250692007-08-22 12:47:26 -07002192 return nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193
2194nlmsg_failure:
Thomas Graf98250692007-08-22 12:47:26 -07002195 nlmsg_cancel(skb, nlh);
2196 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197}
2198
2199static int xfrm_send_acquire(struct xfrm_state *x, struct xfrm_tmpl *xt,
2200 struct xfrm_policy *xp, int dir)
2201{
2202 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203
Thomas Graf7deb2262007-08-22 13:57:39 -07002204 skb = nlmsg_new(xfrm_acquire_msgsize(x, xp), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205 if (skb == NULL)
2206 return -ENOMEM;
2207
2208 if (build_acquire(skb, x, xt, xp, dir) < 0)
2209 BUG();
2210
Thomas Graf082a1ad2007-08-22 13:54:36 -07002211 return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_ACQUIRE, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212}
2213
2214/* User gives us xfrm_user_policy_info followed by an array of 0
2215 * or more templates.
2216 */
Venkat Yekkiralacb969f02006-07-24 23:32:20 -07002217static struct xfrm_policy *xfrm_compile_policy(struct sock *sk, int opt,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218 u8 *data, int len, int *dir)
2219{
2220 struct xfrm_userpolicy_info *p = (struct xfrm_userpolicy_info *)data;
2221 struct xfrm_user_tmpl *ut = (struct xfrm_user_tmpl *) (p + 1);
2222 struct xfrm_policy *xp;
2223 int nr;
2224
Venkat Yekkiralacb969f02006-07-24 23:32:20 -07002225 switch (sk->sk_family) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002226 case AF_INET:
2227 if (opt != IP_XFRM_POLICY) {
2228 *dir = -EOPNOTSUPP;
2229 return NULL;
2230 }
2231 break;
2232#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
2233 case AF_INET6:
2234 if (opt != IPV6_XFRM_POLICY) {
2235 *dir = -EOPNOTSUPP;
2236 return NULL;
2237 }
2238 break;
2239#endif
2240 default:
2241 *dir = -EINVAL;
2242 return NULL;
2243 }
2244
2245 *dir = -EINVAL;
2246
2247 if (len < sizeof(*p) ||
2248 verify_newpolicy_info(p))
2249 return NULL;
2250
2251 nr = ((len - sizeof(*p)) / sizeof(*ut));
David S. Millerb4ad86bf2006-12-03 19:19:26 -08002252 if (validate_tmpl(nr, ut, p->sel.family))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002253 return NULL;
2254
Herbert Xua4f1bac2005-07-26 15:43:17 -07002255 if (p->dir > XFRM_POLICY_OUT)
2256 return NULL;
2257
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258 xp = xfrm_policy_alloc(GFP_KERNEL);
2259 if (xp == NULL) {
2260 *dir = -ENOBUFS;
2261 return NULL;
2262 }
2263
2264 copy_from_user_policy(xp, p);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07002265 xp->type = XFRM_POLICY_TYPE_MAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266 copy_templates(xp, ut, nr);
2267
2268 *dir = p->dir;
2269
2270 return xp;
2271}
2272
Thomas Graf7deb2262007-08-22 13:57:39 -07002273static inline size_t xfrm_polexpire_msgsize(struct xfrm_policy *xp)
2274{
2275 return NLMSG_ALIGN(sizeof(struct xfrm_user_polexpire))
2276 + nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr)
2277 + nla_total_size(xfrm_user_sec_ctx_size(xp->security))
2278 + userpolicy_type_attrsize();
2279}
2280
Linus Torvalds1da177e2005-04-16 15:20:36 -07002281static int build_polexpire(struct sk_buff *skb, struct xfrm_policy *xp,
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002282 int dir, struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283{
2284 struct xfrm_user_polexpire *upe;
2285 struct nlmsghdr *nlh;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002286 int hard = c->data.hard;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002288 nlh = nlmsg_put(skb, c->pid, 0, XFRM_MSG_POLEXPIRE, sizeof(*upe), 0);
2289 if (nlh == NULL)
2290 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002291
Thomas Graf7b67c852007-08-22 13:53:52 -07002292 upe = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002293 copy_to_user_policy(xp, &upe->pol, dir);
2294 if (copy_to_user_tmpl(xp, skb) < 0)
2295 goto nlmsg_failure;
Trent Jaegerdf718372005-12-13 23:12:27 -08002296 if (copy_to_user_sec_ctx(xp, skb))
2297 goto nlmsg_failure;
Jamal Hadi Salim1459bb32006-11-20 16:51:22 -08002298 if (copy_to_user_policy_type(xp->type, skb) < 0)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07002299 goto nlmsg_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300 upe->hard = !!hard;
2301
Thomas Graf98250692007-08-22 12:47:26 -07002302 return nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002303
2304nlmsg_failure:
Thomas Graf98250692007-08-22 12:47:26 -07002305 nlmsg_cancel(skb, nlh);
2306 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307}
2308
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002309static int xfrm_exp_policy_notify(struct xfrm_policy *xp, int dir, struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310{
2311 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312
Thomas Graf7deb2262007-08-22 13:57:39 -07002313 skb = nlmsg_new(xfrm_polexpire_msgsize(xp), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314 if (skb == NULL)
2315 return -ENOMEM;
2316
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002317 if (build_polexpire(skb, xp, dir, c) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002318 BUG();
2319
Thomas Graf082a1ad2007-08-22 13:54:36 -07002320 return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_EXPIRE, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002321}
2322
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002323static int xfrm_notify_policy(struct xfrm_policy *xp, int dir, struct km_event *c)
2324{
2325 struct xfrm_userpolicy_info *p;
Herbert Xu0603eac2005-06-18 22:54:36 -07002326 struct xfrm_userpolicy_id *id;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002327 struct nlmsghdr *nlh;
2328 struct sk_buff *skb;
Thomas Graf7deb2262007-08-22 13:57:39 -07002329 int len = nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr);
Herbert Xu0603eac2005-06-18 22:54:36 -07002330 int headlen;
2331
2332 headlen = sizeof(*p);
2333 if (c->event == XFRM_MSG_DELPOLICY) {
Thomas Graf7deb2262007-08-22 13:57:39 -07002334 len += nla_total_size(headlen);
Herbert Xu0603eac2005-06-18 22:54:36 -07002335 headlen = sizeof(*id);
2336 }
Thomas Grafcfbfd452007-08-22 13:57:04 -07002337 len += userpolicy_type_attrsize();
Thomas Graf7deb2262007-08-22 13:57:39 -07002338 len += NLMSG_ALIGN(headlen);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002339
Thomas Graf7deb2262007-08-22 13:57:39 -07002340 skb = nlmsg_new(len, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002341 if (skb == NULL)
2342 return -ENOMEM;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002343
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002344 nlh = nlmsg_put(skb, c->pid, c->seq, c->event, headlen, 0);
2345 if (nlh == NULL)
2346 goto nlmsg_failure;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002347
Thomas Graf7b67c852007-08-22 13:53:52 -07002348 p = nlmsg_data(nlh);
Herbert Xu0603eac2005-06-18 22:54:36 -07002349 if (c->event == XFRM_MSG_DELPOLICY) {
Thomas Grafc0144be2007-08-22 13:55:43 -07002350 struct nlattr *attr;
2351
Thomas Graf7b67c852007-08-22 13:53:52 -07002352 id = nlmsg_data(nlh);
Herbert Xu0603eac2005-06-18 22:54:36 -07002353 memset(id, 0, sizeof(*id));
2354 id->dir = dir;
2355 if (c->data.byid)
2356 id->index = xp->index;
2357 else
2358 memcpy(&id->sel, &xp->selector, sizeof(id->sel));
2359
Thomas Grafc0144be2007-08-22 13:55:43 -07002360 attr = nla_reserve(skb, XFRMA_POLICY, sizeof(*p));
2361 if (attr == NULL)
2362 goto nlmsg_failure;
2363
2364 p = nla_data(attr);
Herbert Xu0603eac2005-06-18 22:54:36 -07002365 }
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002366
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002367 copy_to_user_policy(xp, p, dir);
2368 if (copy_to_user_tmpl(xp, skb) < 0)
2369 goto nlmsg_failure;
Jamal Hadi Salim1459bb32006-11-20 16:51:22 -08002370 if (copy_to_user_policy_type(xp->type, skb) < 0)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07002371 goto nlmsg_failure;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002372
Thomas Graf98250692007-08-22 12:47:26 -07002373 nlmsg_end(skb, nlh);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002374
Thomas Graf082a1ad2007-08-22 13:54:36 -07002375 return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_POLICY, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002376
2377nlmsg_failure:
2378 kfree_skb(skb);
2379 return -1;
2380}
2381
2382static int xfrm_notify_policy_flush(struct km_event *c)
2383{
2384 struct nlmsghdr *nlh;
2385 struct sk_buff *skb;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002386
Thomas Graf7deb2262007-08-22 13:57:39 -07002387 skb = nlmsg_new(userpolicy_type_attrsize(), GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002388 if (skb == NULL)
2389 return -ENOMEM;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002390
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002391 nlh = nlmsg_put(skb, c->pid, c->seq, XFRM_MSG_FLUSHPOLICY, 0, 0);
2392 if (nlh == NULL)
2393 goto nlmsg_failure;
Jamal Hadi Salim0c51f532006-11-27 12:58:20 -08002394 if (copy_to_user_policy_type(c->data.type, skb) < 0)
2395 goto nlmsg_failure;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002396
Thomas Graf98250692007-08-22 12:47:26 -07002397 nlmsg_end(skb, nlh);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002398
Thomas Graf082a1ad2007-08-22 13:54:36 -07002399 return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_POLICY, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002400
2401nlmsg_failure:
2402 kfree_skb(skb);
2403 return -1;
2404}
2405
2406static int xfrm_send_policy_notify(struct xfrm_policy *xp, int dir, struct km_event *c)
2407{
2408
2409 switch (c->event) {
Herbert Xuf60f6b82005-06-18 22:44:37 -07002410 case XFRM_MSG_NEWPOLICY:
2411 case XFRM_MSG_UPDPOLICY:
2412 case XFRM_MSG_DELPOLICY:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002413 return xfrm_notify_policy(xp, dir, c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07002414 case XFRM_MSG_FLUSHPOLICY:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002415 return xfrm_notify_policy_flush(c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07002416 case XFRM_MSG_POLEXPIRE:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002417 return xfrm_exp_policy_notify(xp, dir, c);
2418 default:
2419 printk("xfrm_user: Unknown Policy event %d\n", c->event);
2420 }
2421
2422 return 0;
2423
2424}
2425
Thomas Graf7deb2262007-08-22 13:57:39 -07002426static inline size_t xfrm_report_msgsize(void)
2427{
2428 return NLMSG_ALIGN(sizeof(struct xfrm_user_report));
2429}
2430
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002431static int build_report(struct sk_buff *skb, u8 proto,
2432 struct xfrm_selector *sel, xfrm_address_t *addr)
2433{
2434 struct xfrm_user_report *ur;
2435 struct nlmsghdr *nlh;
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002436
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002437 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_REPORT, sizeof(*ur), 0);
2438 if (nlh == NULL)
2439 return -EMSGSIZE;
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002440
Thomas Graf7b67c852007-08-22 13:53:52 -07002441 ur = nlmsg_data(nlh);
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002442 ur->proto = proto;
2443 memcpy(&ur->sel, sel, sizeof(ur->sel));
2444
2445 if (addr)
Thomas Grafc0144be2007-08-22 13:55:43 -07002446 NLA_PUT(skb, XFRMA_COADDR, sizeof(*addr), addr);
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002447
Thomas Graf98250692007-08-22 12:47:26 -07002448 return nlmsg_end(skb, nlh);
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002449
Thomas Grafc0144be2007-08-22 13:55:43 -07002450nla_put_failure:
Thomas Graf98250692007-08-22 12:47:26 -07002451 nlmsg_cancel(skb, nlh);
2452 return -EMSGSIZE;
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002453}
2454
2455static int xfrm_send_report(u8 proto, struct xfrm_selector *sel,
2456 xfrm_address_t *addr)
2457{
2458 struct sk_buff *skb;
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002459
Thomas Graf7deb2262007-08-22 13:57:39 -07002460 skb = nlmsg_new(xfrm_report_msgsize(), GFP_ATOMIC);
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002461 if (skb == NULL)
2462 return -ENOMEM;
2463
2464 if (build_report(skb, proto, sel, addr) < 0)
2465 BUG();
2466
Thomas Graf082a1ad2007-08-22 13:54:36 -07002467 return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_REPORT, GFP_ATOMIC);
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002468}
2469
Linus Torvalds1da177e2005-04-16 15:20:36 -07002470static struct xfrm_mgr netlink_mgr = {
2471 .id = "netlink",
2472 .notify = xfrm_send_state_notify,
2473 .acquire = xfrm_send_acquire,
2474 .compile_policy = xfrm_compile_policy,
2475 .notify_policy = xfrm_send_policy_notify,
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002476 .report = xfrm_send_report,
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002477 .migrate = xfrm_send_migrate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002478};
2479
2480static int __init xfrm_user_init(void)
2481{
Patrick McHardybe336902006-03-20 22:40:54 -08002482 struct sock *nlsk;
2483
Masahide NAKAMURA654b32c2006-08-23 19:12:56 -07002484 printk(KERN_INFO "Initializing XFRM netlink socket\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002485
Eric W. Biedermanb4b51022007-09-12 13:05:38 +02002486 nlsk = netlink_kernel_create(&init_net, NETLINK_XFRM, XFRMNLGRP_MAX,
Patrick McHardyaf65bdf2007-04-20 14:14:21 -07002487 xfrm_netlink_rcv, NULL, THIS_MODULE);
Patrick McHardybe336902006-03-20 22:40:54 -08002488 if (nlsk == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002489 return -ENOMEM;
Patrick McHardybe336902006-03-20 22:40:54 -08002490 rcu_assign_pointer(xfrm_nl, nlsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002491
2492 xfrm_register_km(&netlink_mgr);
2493
2494 return 0;
2495}
2496
2497static void __exit xfrm_user_exit(void)
2498{
Patrick McHardybe336902006-03-20 22:40:54 -08002499 struct sock *nlsk = xfrm_nl;
2500
Linus Torvalds1da177e2005-04-16 15:20:36 -07002501 xfrm_unregister_km(&netlink_mgr);
Patrick McHardybe336902006-03-20 22:40:54 -08002502 rcu_assign_pointer(xfrm_nl, NULL);
2503 synchronize_rcu();
Denis V. Lunevb7c6ba62008-01-28 14:41:19 -08002504 netlink_kernel_release(nlsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002505}
2506
2507module_init(xfrm_user_init);
2508module_exit(xfrm_user_exit);
2509MODULE_LICENSE("GPL");
Harald Welte4fdb3bb2005-08-09 19:40:55 -07002510MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK, NETLINK_XFRM);
Jamal Hadi Salimf8cd5482006-03-20 19:15:11 -08002511