blob: b976d9ed10e45a3a2e410a6b873f9128a18f6021 [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:
Linus Torvalds1da177e2005-04-16 15:20:36 -070053 case XFRMA_ALG_CRYPT:
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 case XFRMA_ALG_COMP:
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 break;
56
57 default:
58 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -070059 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070060
61 algp->alg_name[CRYPTO_MAX_ALG_NAME - 1] = '\0';
62 return 0;
63}
64
Herbert Xu1a6509d2008-01-28 19:37:29 -080065static int verify_aead(struct nlattr **attrs)
66{
67 struct nlattr *rt = attrs[XFRMA_ALG_AEAD];
68 struct xfrm_algo_aead *algp;
69
70 if (!rt)
71 return 0;
72
73 algp = nla_data(rt);
74 if (nla_len(rt) < aead_len(algp))
75 return -EINVAL;
76
77 algp->alg_name[CRYPTO_MAX_ALG_NAME - 1] = '\0';
78 return 0;
79}
80
Thomas Graf5424f322007-08-22 14:01:33 -070081static void verify_one_addr(struct nlattr **attrs, enum xfrm_attr_type_t type,
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -070082 xfrm_address_t **addrp)
83{
Thomas Graf5424f322007-08-22 14:01:33 -070084 struct nlattr *rt = attrs[type];
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -070085
Thomas Grafcf5cb792007-08-22 13:59:04 -070086 if (rt && addrp)
Thomas Graf5424f322007-08-22 14:01:33 -070087 *addrp = nla_data(rt);
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -070088}
Trent Jaegerdf718372005-12-13 23:12:27 -080089
Thomas Graf5424f322007-08-22 14:01:33 -070090static inline int verify_sec_ctx_len(struct nlattr **attrs)
Trent Jaegerdf718372005-12-13 23:12:27 -080091{
Thomas Graf5424f322007-08-22 14:01:33 -070092 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
Trent Jaegerdf718372005-12-13 23:12:27 -080093 struct xfrm_user_sec_ctx *uctx;
Trent Jaegerdf718372005-12-13 23:12:27 -080094
95 if (!rt)
96 return 0;
97
Thomas Graf5424f322007-08-22 14:01:33 -070098 uctx = nla_data(rt);
Thomas Grafcf5cb792007-08-22 13:59:04 -070099 if (uctx->len != (sizeof(struct xfrm_user_sec_ctx) + uctx->ctx_len))
Trent Jaegerdf718372005-12-13 23:12:27 -0800100 return -EINVAL;
101
102 return 0;
103}
104
105
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106static int verify_newsa_info(struct xfrm_usersa_info *p,
Thomas Graf5424f322007-08-22 14:01:33 -0700107 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108{
109 int err;
110
111 err = -EINVAL;
112 switch (p->family) {
113 case AF_INET:
114 break;
115
116 case AF_INET6:
117#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
118 break;
119#else
120 err = -EAFNOSUPPORT;
121 goto out;
122#endif
123
124 default:
125 goto out;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700126 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127
128 err = -EINVAL;
129 switch (p->id.proto) {
130 case IPPROTO_AH:
Thomas Graf35a7aa02007-08-22 14:00:40 -0700131 if (!attrs[XFRMA_ALG_AUTH] ||
Herbert Xu1a6509d2008-01-28 19:37:29 -0800132 attrs[XFRMA_ALG_AEAD] ||
Thomas Graf35a7aa02007-08-22 14:00:40 -0700133 attrs[XFRMA_ALG_CRYPT] ||
134 attrs[XFRMA_ALG_COMP])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 goto out;
136 break;
137
138 case IPPROTO_ESP:
Herbert Xu1a6509d2008-01-28 19:37:29 -0800139 if (attrs[XFRMA_ALG_COMP])
140 goto out;
141 if (!attrs[XFRMA_ALG_AUTH] &&
142 !attrs[XFRMA_ALG_CRYPT] &&
143 !attrs[XFRMA_ALG_AEAD])
144 goto out;
145 if ((attrs[XFRMA_ALG_AUTH] ||
146 attrs[XFRMA_ALG_CRYPT]) &&
147 attrs[XFRMA_ALG_AEAD])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 goto out;
149 break;
150
151 case IPPROTO_COMP:
Thomas Graf35a7aa02007-08-22 14:00:40 -0700152 if (!attrs[XFRMA_ALG_COMP] ||
Herbert Xu1a6509d2008-01-28 19:37:29 -0800153 attrs[XFRMA_ALG_AEAD] ||
Thomas Graf35a7aa02007-08-22 14:00:40 -0700154 attrs[XFRMA_ALG_AUTH] ||
155 attrs[XFRMA_ALG_CRYPT])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 goto out;
157 break;
158
Masahide NAKAMURAe23c7192006-08-23 20:33:28 -0700159#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
160 case IPPROTO_DSTOPTS:
161 case IPPROTO_ROUTING:
Thomas Graf35a7aa02007-08-22 14:00:40 -0700162 if (attrs[XFRMA_ALG_COMP] ||
163 attrs[XFRMA_ALG_AUTH] ||
Herbert Xu1a6509d2008-01-28 19:37:29 -0800164 attrs[XFRMA_ALG_AEAD] ||
Thomas Graf35a7aa02007-08-22 14:00:40 -0700165 attrs[XFRMA_ALG_CRYPT] ||
166 attrs[XFRMA_ENCAP] ||
167 attrs[XFRMA_SEC_CTX] ||
168 !attrs[XFRMA_COADDR])
Masahide NAKAMURAe23c7192006-08-23 20:33:28 -0700169 goto out;
170 break;
171#endif
172
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 default:
174 goto out;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700175 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176
Herbert Xu1a6509d2008-01-28 19:37:29 -0800177 if ((err = verify_aead(attrs)))
178 goto out;
Thomas Graf35a7aa02007-08-22 14:00:40 -0700179 if ((err = verify_one_alg(attrs, XFRMA_ALG_AUTH)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 goto out;
Thomas Graf35a7aa02007-08-22 14:00:40 -0700181 if ((err = verify_one_alg(attrs, XFRMA_ALG_CRYPT)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 goto out;
Thomas Graf35a7aa02007-08-22 14:00:40 -0700183 if ((err = verify_one_alg(attrs, XFRMA_ALG_COMP)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 goto out;
Thomas Graf35a7aa02007-08-22 14:00:40 -0700185 if ((err = verify_sec_ctx_len(attrs)))
Trent Jaegerdf718372005-12-13 23:12:27 -0800186 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187
188 err = -EINVAL;
189 switch (p->mode) {
Masahide NAKAMURA7e49e6d2006-09-22 15:05:15 -0700190 case XFRM_MODE_TRANSPORT:
191 case XFRM_MODE_TUNNEL:
Noriaki TAKAMIYA060f02a2006-08-23 18:18:55 -0700192 case XFRM_MODE_ROUTEOPTIMIZATION:
Diego Beltrami0a694522006-10-03 23:47:05 -0700193 case XFRM_MODE_BEET:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 break;
195
196 default:
197 goto out;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700198 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199
200 err = 0;
201
202out:
203 return err;
204}
205
206static int attach_one_algo(struct xfrm_algo **algpp, u8 *props,
207 struct xfrm_algo_desc *(*get_byname)(char *, int),
Thomas Graf5424f322007-08-22 14:01:33 -0700208 struct nlattr *rta)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 struct xfrm_algo *p, *ualg;
211 struct xfrm_algo_desc *algo;
212
213 if (!rta)
214 return 0;
215
Thomas Graf5424f322007-08-22 14:01:33 -0700216 ualg = nla_data(rta);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217
218 algo = get_byname(ualg->alg_name, 1);
219 if (!algo)
220 return -ENOSYS;
221 *props = algo->desc.sadb_alg_id;
222
Eric Dumazet0f99be02008-01-08 23:39:06 -0800223 p = kmemdup(ualg, xfrm_alg_len(ualg), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 if (!p)
225 return -ENOMEM;
226
Herbert Xu04ff1262006-08-13 08:50:00 +1000227 strcpy(p->alg_name, algo->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 *algpp = p;
229 return 0;
230}
231
Herbert Xu1a6509d2008-01-28 19:37:29 -0800232static int attach_aead(struct xfrm_algo_aead **algpp, u8 *props,
233 struct nlattr *rta)
234{
235 struct xfrm_algo_aead *p, *ualg;
236 struct xfrm_algo_desc *algo;
237
238 if (!rta)
239 return 0;
240
241 ualg = nla_data(rta);
242
243 algo = xfrm_aead_get_byname(ualg->alg_name, ualg->alg_icv_len, 1);
244 if (!algo)
245 return -ENOSYS;
246 *props = algo->desc.sadb_alg_id;
247
248 p = kmemdup(ualg, aead_len(ualg), GFP_KERNEL);
249 if (!p)
250 return -ENOMEM;
251
252 strcpy(p->alg_name, algo->name);
253 *algpp = p;
254 return 0;
255}
256
Joy Latten661697f2007-04-13 16:14:35 -0700257static inline int xfrm_user_sec_ctx_size(struct xfrm_sec_ctx *xfrm_ctx)
Trent Jaegerdf718372005-12-13 23:12:27 -0800258{
Trent Jaegerdf718372005-12-13 23:12:27 -0800259 int len = 0;
260
261 if (xfrm_ctx) {
262 len += sizeof(struct xfrm_user_sec_ctx);
263 len += xfrm_ctx->ctx_len;
264 }
265 return len;
266}
267
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268static void copy_from_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p)
269{
270 memcpy(&x->id, &p->id, sizeof(x->id));
271 memcpy(&x->sel, &p->sel, sizeof(x->sel));
272 memcpy(&x->lft, &p->lft, sizeof(x->lft));
273 x->props.mode = p->mode;
274 x->props.replay_window = p->replay_window;
275 x->props.reqid = p->reqid;
276 x->props.family = p->family;
David S. Miller54489c142006-10-27 15:29:47 -0700277 memcpy(&x->props.saddr, &p->saddr, sizeof(x->props.saddr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 x->props.flags = p->flags;
Herbert Xu196b0032007-07-31 02:04:32 -0700279
Patrick McHardybcf0dda2008-04-09 15:08:24 -0700280 if (!x->sel.family)
Herbert Xu196b0032007-07-31 02:04:32 -0700281 x->sel.family = p->family;
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -0700282
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283}
284
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800285/*
286 * someday when pfkey also has support, we could have the code
287 * somehow made shareable and move it to xfrm_state.c - JHS
288 *
289*/
Thomas Graf5424f322007-08-22 14:01:33 -0700290static void xfrm_update_ae_params(struct xfrm_state *x, struct nlattr **attrs)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800291{
Thomas Graf5424f322007-08-22 14:01:33 -0700292 struct nlattr *rp = attrs[XFRMA_REPLAY_VAL];
293 struct nlattr *lt = attrs[XFRMA_LTIME_VAL];
294 struct nlattr *et = attrs[XFRMA_ETIMER_THRESH];
295 struct nlattr *rt = attrs[XFRMA_REPLAY_THRESH];
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800296
297 if (rp) {
298 struct xfrm_replay_state *replay;
Thomas Graf5424f322007-08-22 14:01:33 -0700299 replay = nla_data(rp);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800300 memcpy(&x->replay, replay, sizeof(*replay));
301 memcpy(&x->preplay, replay, sizeof(*replay));
302 }
303
304 if (lt) {
305 struct xfrm_lifetime_cur *ltime;
Thomas Graf5424f322007-08-22 14:01:33 -0700306 ltime = nla_data(lt);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800307 x->curlft.bytes = ltime->bytes;
308 x->curlft.packets = ltime->packets;
309 x->curlft.add_time = ltime->add_time;
310 x->curlft.use_time = ltime->use_time;
311 }
312
Thomas Grafcf5cb792007-08-22 13:59:04 -0700313 if (et)
Thomas Graf5424f322007-08-22 14:01:33 -0700314 x->replay_maxage = nla_get_u32(et);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800315
Thomas Grafcf5cb792007-08-22 13:59:04 -0700316 if (rt)
Thomas Graf5424f322007-08-22 14:01:33 -0700317 x->replay_maxdiff = nla_get_u32(rt);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800318}
319
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320static struct xfrm_state *xfrm_state_construct(struct xfrm_usersa_info *p,
Thomas Graf5424f322007-08-22 14:01:33 -0700321 struct nlattr **attrs,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 int *errp)
323{
324 struct xfrm_state *x = xfrm_state_alloc();
325 int err = -ENOMEM;
326
327 if (!x)
328 goto error_no_put;
329
330 copy_from_user_state(x, p);
331
Herbert Xu1a6509d2008-01-28 19:37:29 -0800332 if ((err = attach_aead(&x->aead, &x->props.ealgo,
333 attrs[XFRMA_ALG_AEAD])))
334 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 if ((err = attach_one_algo(&x->aalg, &x->props.aalgo,
336 xfrm_aalg_get_byname,
Thomas Graf35a7aa02007-08-22 14:00:40 -0700337 attrs[XFRMA_ALG_AUTH])))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 goto error;
339 if ((err = attach_one_algo(&x->ealg, &x->props.ealgo,
340 xfrm_ealg_get_byname,
Thomas Graf35a7aa02007-08-22 14:00:40 -0700341 attrs[XFRMA_ALG_CRYPT])))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 goto error;
343 if ((err = attach_one_algo(&x->calg, &x->props.calgo,
344 xfrm_calg_get_byname,
Thomas Graf35a7aa02007-08-22 14:00:40 -0700345 attrs[XFRMA_ALG_COMP])))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 goto error;
Thomas Graffd211502007-09-06 03:28:08 -0700347
348 if (attrs[XFRMA_ENCAP]) {
349 x->encap = kmemdup(nla_data(attrs[XFRMA_ENCAP]),
350 sizeof(*x->encap), GFP_KERNEL);
351 if (x->encap == NULL)
352 goto error;
353 }
354
355 if (attrs[XFRMA_COADDR]) {
356 x->coaddr = kmemdup(nla_data(attrs[XFRMA_COADDR]),
357 sizeof(*x->coaddr), GFP_KERNEL);
358 if (x->coaddr == NULL)
359 goto error;
360 }
361
Herbert Xu72cb6962005-06-20 13:18:08 -0700362 err = xfrm_init_state(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 if (err)
364 goto error;
365
Thomas Graffd211502007-09-06 03:28:08 -0700366 if (attrs[XFRMA_SEC_CTX] &&
367 security_xfrm_state_alloc(x, nla_data(attrs[XFRMA_SEC_CTX])))
Trent Jaegerdf718372005-12-13 23:12:27 -0800368 goto error;
369
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 x->km.seq = p->seq;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800371 x->replay_maxdiff = sysctl_xfrm_aevent_rseqth;
372 /* sysctl_xfrm_aevent_etime is in 100ms units */
373 x->replay_maxage = (sysctl_xfrm_aevent_etime*HZ)/XFRM_AE_ETH_M;
374 x->preplay.bitmap = 0;
375 x->preplay.seq = x->replay.seq+x->replay_maxdiff;
376 x->preplay.oseq = x->replay.oseq +x->replay_maxdiff;
377
378 /* override default values from above */
379
Thomas Graf5424f322007-08-22 14:01:33 -0700380 xfrm_update_ae_params(x, attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381
382 return x;
383
384error:
385 x->km.state = XFRM_STATE_DEAD;
386 xfrm_state_put(x);
387error_no_put:
388 *errp = err;
389 return NULL;
390}
391
Christoph Hellwig22e70052007-01-02 15:22:30 -0800392static int xfrm_add_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -0700393 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394{
Thomas Graf7b67c852007-08-22 13:53:52 -0700395 struct xfrm_usersa_info *p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 struct xfrm_state *x;
397 int err;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700398 struct km_event c;
Eric Paris25323862008-04-18 10:09:25 -0400399 uid_t loginuid = NETLINK_CB(skb).loginuid;
400 u32 sessionid = NETLINK_CB(skb).sessionid;
401 u32 sid = NETLINK_CB(skb).sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402
Thomas Graf35a7aa02007-08-22 14:00:40 -0700403 err = verify_newsa_info(p, attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 if (err)
405 return err;
406
Thomas Graf35a7aa02007-08-22 14:00:40 -0700407 x = xfrm_state_construct(p, attrs, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 if (!x)
409 return err;
410
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700411 xfrm_state_hold(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 if (nlh->nlmsg_type == XFRM_MSG_NEWSA)
413 err = xfrm_state_add(x);
414 else
415 err = xfrm_state_update(x);
416
Eric Paris25323862008-04-18 10:09:25 -0400417 xfrm_audit_state_add(x, err ? 0 : 1, loginuid, sessionid, sid);
Joy Latten161a09e2006-11-27 13:11:54 -0600418
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 if (err < 0) {
420 x->km.state = XFRM_STATE_DEAD;
Herbert Xu21380b82006-02-22 14:47:13 -0800421 __xfrm_state_put(x);
Patrick McHardy7d6dfe12005-06-18 22:45:31 -0700422 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 }
424
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700425 c.seq = nlh->nlmsg_seq;
426 c.pid = nlh->nlmsg_pid;
Herbert Xuf60f6b82005-06-18 22:44:37 -0700427 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700428
429 km_state_notify(x, &c);
Patrick McHardy7d6dfe12005-06-18 22:45:31 -0700430out:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700431 xfrm_state_put(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 return err;
433}
434
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700435static struct xfrm_state *xfrm_user_state_lookup(struct xfrm_usersa_id *p,
Thomas Graf5424f322007-08-22 14:01:33 -0700436 struct nlattr **attrs,
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700437 int *errp)
438{
439 struct xfrm_state *x = NULL;
440 int err;
441
442 if (xfrm_id_proto_match(p->proto, IPSEC_PROTO_ANY)) {
443 err = -ESRCH;
444 x = xfrm_state_lookup(&p->daddr, p->spi, p->proto, p->family);
445 } else {
446 xfrm_address_t *saddr = NULL;
447
Thomas Graf35a7aa02007-08-22 14:00:40 -0700448 verify_one_addr(attrs, XFRMA_SRCADDR, &saddr);
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700449 if (!saddr) {
450 err = -EINVAL;
451 goto out;
452 }
453
Masahide NAKAMURA9abbffe2006-11-24 20:34:51 -0800454 err = -ESRCH;
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700455 x = xfrm_state_lookup_byaddr(&p->daddr, saddr, p->proto,
456 p->family);
457 }
458
459 out:
460 if (!x && errp)
461 *errp = err;
462 return x;
463}
464
Christoph Hellwig22e70052007-01-02 15:22:30 -0800465static int xfrm_del_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -0700466 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467{
468 struct xfrm_state *x;
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700469 int err = -ESRCH;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700470 struct km_event c;
Thomas Graf7b67c852007-08-22 13:53:52 -0700471 struct xfrm_usersa_id *p = nlmsg_data(nlh);
Eric Paris25323862008-04-18 10:09:25 -0400472 uid_t loginuid = NETLINK_CB(skb).loginuid;
473 u32 sessionid = NETLINK_CB(skb).sessionid;
474 u32 sid = NETLINK_CB(skb).sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475
Thomas Graf35a7aa02007-08-22 14:00:40 -0700476 x = xfrm_user_state_lookup(p, attrs, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 if (x == NULL)
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700478 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479
David S. Miller6f68dc32006-06-08 23:58:52 -0700480 if ((err = security_xfrm_state_delete(x)) != 0)
Catherine Zhangc8c05a82006-06-08 23:39:49 -0700481 goto out;
482
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 if (xfrm_state_kern(x)) {
Catherine Zhangc8c05a82006-06-08 23:39:49 -0700484 err = -EPERM;
485 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 }
487
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700488 err = xfrm_state_delete(x);
Joy Latten161a09e2006-11-27 13:11:54 -0600489
Catherine Zhangc8c05a82006-06-08 23:39:49 -0700490 if (err < 0)
491 goto out;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700492
493 c.seq = nlh->nlmsg_seq;
494 c.pid = nlh->nlmsg_pid;
Herbert Xuf60f6b82005-06-18 22:44:37 -0700495 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700496 km_state_notify(x, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497
Catherine Zhangc8c05a82006-06-08 23:39:49 -0700498out:
Eric Paris25323862008-04-18 10:09:25 -0400499 xfrm_audit_state_delete(x, err ? 0 : 1, loginuid, sessionid, sid);
Catherine Zhangc8c05a82006-06-08 23:39:49 -0700500 xfrm_state_put(x);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700501 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502}
503
504static void copy_to_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p)
505{
506 memcpy(&p->id, &x->id, sizeof(p->id));
507 memcpy(&p->sel, &x->sel, sizeof(p->sel));
508 memcpy(&p->lft, &x->lft, sizeof(p->lft));
509 memcpy(&p->curlft, &x->curlft, sizeof(p->curlft));
510 memcpy(&p->stats, &x->stats, sizeof(p->stats));
David S. Miller54489c142006-10-27 15:29:47 -0700511 memcpy(&p->saddr, &x->props.saddr, sizeof(p->saddr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 p->mode = x->props.mode;
513 p->replay_window = x->props.replay_window;
514 p->reqid = x->props.reqid;
515 p->family = x->props.family;
516 p->flags = x->props.flags;
517 p->seq = x->km.seq;
518}
519
520struct xfrm_dump_info {
521 struct sk_buff *in_skb;
522 struct sk_buff *out_skb;
523 u32 nlmsg_seq;
524 u16 nlmsg_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525};
526
Thomas Grafc0144be2007-08-22 13:55:43 -0700527static int copy_sec_ctx(struct xfrm_sec_ctx *s, struct sk_buff *skb)
528{
Thomas Grafc0144be2007-08-22 13:55:43 -0700529 struct xfrm_user_sec_ctx *uctx;
530 struct nlattr *attr;
Herbert Xu68325d32007-10-09 13:30:57 -0700531 int ctx_size = sizeof(*uctx) + s->ctx_len;
Thomas Grafc0144be2007-08-22 13:55:43 -0700532
533 attr = nla_reserve(skb, XFRMA_SEC_CTX, ctx_size);
534 if (attr == NULL)
535 return -EMSGSIZE;
536
537 uctx = nla_data(attr);
538 uctx->exttype = XFRMA_SEC_CTX;
539 uctx->len = ctx_size;
540 uctx->ctx_doi = s->ctx_doi;
541 uctx->ctx_alg = s->ctx_alg;
542 uctx->ctx_len = s->ctx_len;
543 memcpy(uctx + 1, s->ctx_str, s->ctx_len);
544
545 return 0;
546}
547
Herbert Xu68325d32007-10-09 13:30:57 -0700548/* Don't change this without updating xfrm_sa_len! */
549static int copy_to_user_state_extra(struct xfrm_state *x,
550 struct xfrm_usersa_info *p,
551 struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 copy_to_user_state(x, p);
554
Herbert Xu050f0092007-10-09 13:31:47 -0700555 if (x->coaddr)
556 NLA_PUT(skb, XFRMA_COADDR, sizeof(*x->coaddr), x->coaddr);
557
558 if (x->lastused)
559 NLA_PUT_U64(skb, XFRMA_LASTUSED, x->lastused);
Herbert Xu050f0092007-10-09 13:31:47 -0700560
Herbert Xu1a6509d2008-01-28 19:37:29 -0800561 if (x->aead)
562 NLA_PUT(skb, XFRMA_ALG_AEAD, aead_len(x->aead), x->aead);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 if (x->aalg)
Eric Dumazet0f99be02008-01-08 23:39:06 -0800564 NLA_PUT(skb, XFRMA_ALG_AUTH, xfrm_alg_len(x->aalg), x->aalg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 if (x->ealg)
Eric Dumazet0f99be02008-01-08 23:39:06 -0800566 NLA_PUT(skb, XFRMA_ALG_CRYPT, xfrm_alg_len(x->ealg), x->ealg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 if (x->calg)
Thomas Grafc0144be2007-08-22 13:55:43 -0700568 NLA_PUT(skb, XFRMA_ALG_COMP, sizeof(*(x->calg)), x->calg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569
570 if (x->encap)
Thomas Grafc0144be2007-08-22 13:55:43 -0700571 NLA_PUT(skb, XFRMA_ENCAP, sizeof(*x->encap), x->encap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572
Thomas Grafc0144be2007-08-22 13:55:43 -0700573 if (x->security && copy_sec_ctx(x->security, skb) < 0)
574 goto nla_put_failure;
Noriaki TAKAMIYA060f02a2006-08-23 18:18:55 -0700575
Herbert Xu68325d32007-10-09 13:30:57 -0700576 return 0;
577
578nla_put_failure:
579 return -EMSGSIZE;
580}
581
582static int dump_one_state(struct xfrm_state *x, int count, void *ptr)
583{
584 struct xfrm_dump_info *sp = ptr;
585 struct sk_buff *in_skb = sp->in_skb;
586 struct sk_buff *skb = sp->out_skb;
587 struct xfrm_usersa_info *p;
588 struct nlmsghdr *nlh;
589 int err;
590
Herbert Xu68325d32007-10-09 13:30:57 -0700591 nlh = nlmsg_put(skb, NETLINK_CB(in_skb).pid, sp->nlmsg_seq,
592 XFRM_MSG_NEWSA, sizeof(*p), sp->nlmsg_flags);
593 if (nlh == NULL)
594 return -EMSGSIZE;
595
596 p = nlmsg_data(nlh);
597
598 err = copy_to_user_state_extra(x, p, skb);
599 if (err)
600 goto nla_put_failure;
601
Thomas Graf98250692007-08-22 12:47:26 -0700602 nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 return 0;
604
Thomas Grafc0144be2007-08-22 13:55:43 -0700605nla_put_failure:
Thomas Graf98250692007-08-22 12:47:26 -0700606 nlmsg_cancel(skb, nlh);
Herbert Xu68325d32007-10-09 13:30:57 -0700607 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608}
609
Timo Teras4c563f72008-02-28 21:31:08 -0800610static int xfrm_dump_sa_done(struct netlink_callback *cb)
611{
612 struct xfrm_state_walk *walk = (struct xfrm_state_walk *) &cb->args[1];
613 xfrm_state_walk_done(walk);
614 return 0;
615}
616
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617static int xfrm_dump_sa(struct sk_buff *skb, struct netlink_callback *cb)
618{
Timo Teras4c563f72008-02-28 21:31:08 -0800619 struct xfrm_state_walk *walk = (struct xfrm_state_walk *) &cb->args[1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 struct xfrm_dump_info info;
621
Timo Teras4c563f72008-02-28 21:31:08 -0800622 BUILD_BUG_ON(sizeof(struct xfrm_state_walk) >
623 sizeof(cb->args) - sizeof(cb->args[0]));
624
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 info.in_skb = cb->skb;
626 info.out_skb = skb;
627 info.nlmsg_seq = cb->nlh->nlmsg_seq;
628 info.nlmsg_flags = NLM_F_MULTI;
Timo Teras4c563f72008-02-28 21:31:08 -0800629
630 if (!cb->args[0]) {
631 cb->args[0] = 1;
632 xfrm_state_walk_init(walk, 0);
633 }
634
635 (void) xfrm_state_walk(walk, dump_one_state, &info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636
637 return skb->len;
638}
639
640static struct sk_buff *xfrm_state_netlink(struct sk_buff *in_skb,
641 struct xfrm_state *x, u32 seq)
642{
643 struct xfrm_dump_info info;
644 struct sk_buff *skb;
645
Thomas Graf7deb2262007-08-22 13:57:39 -0700646 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 if (!skb)
648 return ERR_PTR(-ENOMEM);
649
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 info.in_skb = in_skb;
651 info.out_skb = skb;
652 info.nlmsg_seq = seq;
653 info.nlmsg_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654
655 if (dump_one_state(x, 0, &info)) {
656 kfree_skb(skb);
657 return NULL;
658 }
659
660 return skb;
661}
662
Thomas Graf7deb2262007-08-22 13:57:39 -0700663static inline size_t xfrm_spdinfo_msgsize(void)
664{
665 return NLMSG_ALIGN(4)
666 + nla_total_size(sizeof(struct xfrmu_spdinfo))
667 + nla_total_size(sizeof(struct xfrmu_spdhinfo));
668}
669
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700670static int build_spdinfo(struct sk_buff *skb, u32 pid, u32 seq, u32 flags)
671{
Jamal Hadi Salim5a6d3412007-05-04 12:55:39 -0700672 struct xfrmk_spdinfo si;
673 struct xfrmu_spdinfo spc;
674 struct xfrmu_spdhinfo sph;
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700675 struct nlmsghdr *nlh;
676 u32 *f;
677
678 nlh = nlmsg_put(skb, pid, seq, XFRM_MSG_NEWSPDINFO, sizeof(u32), 0);
679 if (nlh == NULL) /* shouldnt really happen ... */
680 return -EMSGSIZE;
681
682 f = nlmsg_data(nlh);
683 *f = flags;
684 xfrm_spd_getinfo(&si);
Jamal Hadi Salim5a6d3412007-05-04 12:55:39 -0700685 spc.incnt = si.incnt;
686 spc.outcnt = si.outcnt;
687 spc.fwdcnt = si.fwdcnt;
688 spc.inscnt = si.inscnt;
689 spc.outscnt = si.outscnt;
690 spc.fwdscnt = si.fwdscnt;
691 sph.spdhcnt = si.spdhcnt;
692 sph.spdhmcnt = si.spdhmcnt;
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700693
Jamal Hadi Salim5a6d3412007-05-04 12:55:39 -0700694 NLA_PUT(skb, XFRMA_SPD_INFO, sizeof(spc), &spc);
695 NLA_PUT(skb, XFRMA_SPD_HINFO, sizeof(sph), &sph);
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700696
697 return nlmsg_end(skb, nlh);
698
699nla_put_failure:
700 nlmsg_cancel(skb, nlh);
701 return -EMSGSIZE;
702}
703
704static int xfrm_get_spdinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -0700705 struct nlattr **attrs)
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700706{
707 struct sk_buff *r_skb;
Thomas Graf7b67c852007-08-22 13:53:52 -0700708 u32 *flags = nlmsg_data(nlh);
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700709 u32 spid = NETLINK_CB(skb).pid;
710 u32 seq = nlh->nlmsg_seq;
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700711
Thomas Graf7deb2262007-08-22 13:57:39 -0700712 r_skb = nlmsg_new(xfrm_spdinfo_msgsize(), GFP_ATOMIC);
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700713 if (r_skb == NULL)
714 return -ENOMEM;
715
716 if (build_spdinfo(r_skb, spid, seq, *flags) < 0)
717 BUG();
718
719 return nlmsg_unicast(xfrm_nl, r_skb, spid);
720}
721
Thomas Graf7deb2262007-08-22 13:57:39 -0700722static inline size_t xfrm_sadinfo_msgsize(void)
723{
724 return NLMSG_ALIGN(4)
725 + nla_total_size(sizeof(struct xfrmu_sadhinfo))
726 + nla_total_size(4); /* XFRMA_SAD_CNT */
727}
728
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700729static int build_sadinfo(struct sk_buff *skb, u32 pid, u32 seq, u32 flags)
730{
Jamal Hadi Salimaf11e312007-05-04 12:55:13 -0700731 struct xfrmk_sadinfo si;
732 struct xfrmu_sadhinfo sh;
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700733 struct nlmsghdr *nlh;
734 u32 *f;
735
736 nlh = nlmsg_put(skb, pid, seq, XFRM_MSG_NEWSADINFO, sizeof(u32), 0);
737 if (nlh == NULL) /* shouldnt really happen ... */
738 return -EMSGSIZE;
739
740 f = nlmsg_data(nlh);
741 *f = flags;
742 xfrm_sad_getinfo(&si);
743
Jamal Hadi Salimaf11e312007-05-04 12:55:13 -0700744 sh.sadhmcnt = si.sadhmcnt;
745 sh.sadhcnt = si.sadhcnt;
746
747 NLA_PUT_U32(skb, XFRMA_SAD_CNT, si.sadcnt);
748 NLA_PUT(skb, XFRMA_SAD_HINFO, sizeof(sh), &sh);
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700749
750 return nlmsg_end(skb, nlh);
751
752nla_put_failure:
753 nlmsg_cancel(skb, nlh);
754 return -EMSGSIZE;
755}
756
757static int xfrm_get_sadinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -0700758 struct nlattr **attrs)
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700759{
760 struct sk_buff *r_skb;
Thomas Graf7b67c852007-08-22 13:53:52 -0700761 u32 *flags = nlmsg_data(nlh);
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700762 u32 spid = NETLINK_CB(skb).pid;
763 u32 seq = nlh->nlmsg_seq;
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700764
Thomas Graf7deb2262007-08-22 13:57:39 -0700765 r_skb = nlmsg_new(xfrm_sadinfo_msgsize(), GFP_ATOMIC);
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700766 if (r_skb == NULL)
767 return -ENOMEM;
768
769 if (build_sadinfo(r_skb, spid, seq, *flags) < 0)
770 BUG();
771
772 return nlmsg_unicast(xfrm_nl, r_skb, spid);
773}
774
Christoph Hellwig22e70052007-01-02 15:22:30 -0800775static int xfrm_get_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -0700776 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777{
Thomas Graf7b67c852007-08-22 13:53:52 -0700778 struct xfrm_usersa_id *p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 struct xfrm_state *x;
780 struct sk_buff *resp_skb;
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700781 int err = -ESRCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782
Thomas Graf35a7aa02007-08-22 14:00:40 -0700783 x = xfrm_user_state_lookup(p, attrs, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 if (x == NULL)
785 goto out_noput;
786
787 resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
788 if (IS_ERR(resp_skb)) {
789 err = PTR_ERR(resp_skb);
790 } else {
Thomas Graf082a1ad2007-08-22 13:54:36 -0700791 err = nlmsg_unicast(xfrm_nl, resp_skb, NETLINK_CB(skb).pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 }
793 xfrm_state_put(x);
794out_noput:
795 return err;
796}
797
798static int verify_userspi_info(struct xfrm_userspi_info *p)
799{
800 switch (p->info.id.proto) {
801 case IPPROTO_AH:
802 case IPPROTO_ESP:
803 break;
804
805 case IPPROTO_COMP:
806 /* IPCOMP spi is 16-bits. */
807 if (p->max >= 0x10000)
808 return -EINVAL;
809 break;
810
811 default:
812 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700813 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814
815 if (p->min > p->max)
816 return -EINVAL;
817
818 return 0;
819}
820
Christoph Hellwig22e70052007-01-02 15:22:30 -0800821static int xfrm_alloc_userspi(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -0700822 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823{
824 struct xfrm_state *x;
825 struct xfrm_userspi_info *p;
826 struct sk_buff *resp_skb;
827 xfrm_address_t *daddr;
828 int family;
829 int err;
830
Thomas Graf7b67c852007-08-22 13:53:52 -0700831 p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 err = verify_userspi_info(p);
833 if (err)
834 goto out_noput;
835
836 family = p->info.family;
837 daddr = &p->info.id.daddr;
838
839 x = NULL;
840 if (p->info.seq) {
841 x = xfrm_find_acq_byseq(p->info.seq);
842 if (x && xfrm_addr_cmp(&x->id.daddr, daddr, family)) {
843 xfrm_state_put(x);
844 x = NULL;
845 }
846 }
847
848 if (!x)
849 x = xfrm_find_acq(p->info.mode, p->info.reqid,
850 p->info.id.proto, daddr,
851 &p->info.saddr, 1,
852 family);
853 err = -ENOENT;
854 if (x == NULL)
855 goto out_noput;
856
Herbert Xu658b2192007-10-09 13:29:52 -0700857 err = xfrm_alloc_spi(x, p->min, p->max);
858 if (err)
859 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860
Herbert Xu658b2192007-10-09 13:29:52 -0700861 resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 if (IS_ERR(resp_skb)) {
863 err = PTR_ERR(resp_skb);
864 goto out;
865 }
866
Thomas Graf082a1ad2007-08-22 13:54:36 -0700867 err = nlmsg_unicast(xfrm_nl, resp_skb, NETLINK_CB(skb).pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868
869out:
870 xfrm_state_put(x);
871out_noput:
872 return err;
873}
874
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -0800875static int verify_policy_dir(u8 dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876{
877 switch (dir) {
878 case XFRM_POLICY_IN:
879 case XFRM_POLICY_OUT:
880 case XFRM_POLICY_FWD:
881 break;
882
883 default:
884 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700885 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886
887 return 0;
888}
889
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -0800890static int verify_policy_type(u8 type)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -0700891{
892 switch (type) {
893 case XFRM_POLICY_TYPE_MAIN:
894#ifdef CONFIG_XFRM_SUB_POLICY
895 case XFRM_POLICY_TYPE_SUB:
896#endif
897 break;
898
899 default:
900 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700901 }
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -0700902
903 return 0;
904}
905
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906static int verify_newpolicy_info(struct xfrm_userpolicy_info *p)
907{
908 switch (p->share) {
909 case XFRM_SHARE_ANY:
910 case XFRM_SHARE_SESSION:
911 case XFRM_SHARE_USER:
912 case XFRM_SHARE_UNIQUE:
913 break;
914
915 default:
916 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700917 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918
919 switch (p->action) {
920 case XFRM_POLICY_ALLOW:
921 case XFRM_POLICY_BLOCK:
922 break;
923
924 default:
925 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700926 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927
928 switch (p->sel.family) {
929 case AF_INET:
930 break;
931
932 case AF_INET6:
933#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
934 break;
935#else
936 return -EAFNOSUPPORT;
937#endif
938
939 default:
940 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700941 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942
943 return verify_policy_dir(p->dir);
944}
945
Thomas Graf5424f322007-08-22 14:01:33 -0700946static int copy_from_user_sec_ctx(struct xfrm_policy *pol, struct nlattr **attrs)
Trent Jaegerdf718372005-12-13 23:12:27 -0800947{
Thomas Graf5424f322007-08-22 14:01:33 -0700948 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
Trent Jaegerdf718372005-12-13 23:12:27 -0800949 struct xfrm_user_sec_ctx *uctx;
950
951 if (!rt)
952 return 0;
953
Thomas Graf5424f322007-08-22 14:01:33 -0700954 uctx = nla_data(rt);
Paul Moore03e1ad72008-04-12 19:07:52 -0700955 return security_xfrm_policy_alloc(&pol->security, uctx);
Trent Jaegerdf718372005-12-13 23:12:27 -0800956}
957
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958static void copy_templates(struct xfrm_policy *xp, struct xfrm_user_tmpl *ut,
959 int nr)
960{
961 int i;
962
963 xp->xfrm_nr = nr;
964 for (i = 0; i < nr; i++, ut++) {
965 struct xfrm_tmpl *t = &xp->xfrm_vec[i];
966
967 memcpy(&t->id, &ut->id, sizeof(struct xfrm_id));
968 memcpy(&t->saddr, &ut->saddr,
969 sizeof(xfrm_address_t));
970 t->reqid = ut->reqid;
971 t->mode = ut->mode;
972 t->share = ut->share;
973 t->optional = ut->optional;
974 t->aalgos = ut->aalgos;
975 t->ealgos = ut->ealgos;
976 t->calgos = ut->calgos;
Herbert Xuc5d18e92008-04-22 00:46:42 -0700977 /* If all masks are ~0, then we allow all algorithms. */
978 t->allalgs = !~(t->aalgos & t->ealgos & t->calgos);
Miika Komu8511d012006-11-30 16:40:51 -0800979 t->encap_family = ut->family;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 }
981}
982
David S. Millerb4ad86bf2006-12-03 19:19:26 -0800983static int validate_tmpl(int nr, struct xfrm_user_tmpl *ut, u16 family)
984{
985 int i;
986
987 if (nr > XFRM_MAX_DEPTH)
988 return -EINVAL;
989
990 for (i = 0; i < nr; i++) {
991 /* We never validated the ut->family value, so many
992 * applications simply leave it at zero. The check was
993 * never made and ut->family was ignored because all
994 * templates could be assumed to have the same family as
995 * the policy itself. Now that we will have ipv4-in-ipv6
996 * and ipv6-in-ipv4 tunnels, this is no longer true.
997 */
998 if (!ut[i].family)
999 ut[i].family = family;
1000
1001 switch (ut[i].family) {
1002 case AF_INET:
1003 break;
1004#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1005 case AF_INET6:
1006 break;
1007#endif
1008 default:
1009 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001010 }
David S. Millerb4ad86bf2006-12-03 19:19:26 -08001011 }
1012
1013 return 0;
1014}
1015
Thomas Graf5424f322007-08-22 14:01:33 -07001016static int copy_from_user_tmpl(struct xfrm_policy *pol, struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017{
Thomas Graf5424f322007-08-22 14:01:33 -07001018 struct nlattr *rt = attrs[XFRMA_TMPL];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019
1020 if (!rt) {
1021 pol->xfrm_nr = 0;
1022 } else {
Thomas Graf5424f322007-08-22 14:01:33 -07001023 struct xfrm_user_tmpl *utmpl = nla_data(rt);
1024 int nr = nla_len(rt) / sizeof(*utmpl);
David S. Millerb4ad86bf2006-12-03 19:19:26 -08001025 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026
David S. Millerb4ad86bf2006-12-03 19:19:26 -08001027 err = validate_tmpl(nr, utmpl, pol->family);
1028 if (err)
1029 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030
Thomas Graf5424f322007-08-22 14:01:33 -07001031 copy_templates(pol, utmpl, nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 }
1033 return 0;
1034}
1035
Thomas Graf5424f322007-08-22 14:01:33 -07001036static int copy_from_user_policy_type(u8 *tp, struct nlattr **attrs)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001037{
Thomas Graf5424f322007-08-22 14:01:33 -07001038 struct nlattr *rt = attrs[XFRMA_POLICY_TYPE];
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001039 struct xfrm_userpolicy_type *upt;
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001040 u8 type = XFRM_POLICY_TYPE_MAIN;
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001041 int err;
1042
1043 if (rt) {
Thomas Graf5424f322007-08-22 14:01:33 -07001044 upt = nla_data(rt);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001045 type = upt->type;
1046 }
1047
1048 err = verify_policy_type(type);
1049 if (err)
1050 return err;
1051
1052 *tp = type;
1053 return 0;
1054}
1055
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056static void copy_from_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p)
1057{
1058 xp->priority = p->priority;
1059 xp->index = p->index;
1060 memcpy(&xp->selector, &p->sel, sizeof(xp->selector));
1061 memcpy(&xp->lft, &p->lft, sizeof(xp->lft));
1062 xp->action = p->action;
1063 xp->flags = p->flags;
1064 xp->family = p->sel.family;
1065 /* XXX xp->share = p->share; */
1066}
1067
1068static void copy_to_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p, int dir)
1069{
1070 memcpy(&p->sel, &xp->selector, sizeof(p->sel));
1071 memcpy(&p->lft, &xp->lft, sizeof(p->lft));
1072 memcpy(&p->curlft, &xp->curlft, sizeof(p->curlft));
1073 p->priority = xp->priority;
1074 p->index = xp->index;
1075 p->sel.family = xp->family;
1076 p->dir = dir;
1077 p->action = xp->action;
1078 p->flags = xp->flags;
1079 p->share = XFRM_SHARE_ANY; /* XXX xp->share */
1080}
1081
Thomas Graf5424f322007-08-22 14:01:33 -07001082static struct xfrm_policy *xfrm_policy_construct(struct xfrm_userpolicy_info *p, struct nlattr **attrs, int *errp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083{
1084 struct xfrm_policy *xp = xfrm_policy_alloc(GFP_KERNEL);
1085 int err;
1086
1087 if (!xp) {
1088 *errp = -ENOMEM;
1089 return NULL;
1090 }
1091
1092 copy_from_user_policy(xp, p);
Trent Jaegerdf718372005-12-13 23:12:27 -08001093
Thomas Graf35a7aa02007-08-22 14:00:40 -07001094 err = copy_from_user_policy_type(&xp->type, attrs);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001095 if (err)
1096 goto error;
1097
Thomas Graf35a7aa02007-08-22 14:00:40 -07001098 if (!(err = copy_from_user_tmpl(xp, attrs)))
1099 err = copy_from_user_sec_ctx(xp, attrs);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001100 if (err)
1101 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102
1103 return xp;
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001104 error:
1105 *errp = err;
YOSHIFUJI Hideaki073a3712008-02-14 14:52:38 -08001106 xp->dead = 1;
WANG Cong64c31b32008-01-07 22:34:29 -08001107 xfrm_policy_destroy(xp);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001108 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109}
1110
Christoph Hellwig22e70052007-01-02 15:22:30 -08001111static int xfrm_add_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001112 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113{
Thomas Graf7b67c852007-08-22 13:53:52 -07001114 struct xfrm_userpolicy_info *p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115 struct xfrm_policy *xp;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001116 struct km_event c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 int err;
1118 int excl;
Eric Paris25323862008-04-18 10:09:25 -04001119 uid_t loginuid = NETLINK_CB(skb).loginuid;
1120 u32 sessionid = NETLINK_CB(skb).sessionid;
1121 u32 sid = NETLINK_CB(skb).sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122
1123 err = verify_newpolicy_info(p);
1124 if (err)
1125 return err;
Thomas Graf35a7aa02007-08-22 14:00:40 -07001126 err = verify_sec_ctx_len(attrs);
Trent Jaegerdf718372005-12-13 23:12:27 -08001127 if (err)
1128 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129
Thomas Graf35a7aa02007-08-22 14:00:40 -07001130 xp = xfrm_policy_construct(p, attrs, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131 if (!xp)
1132 return err;
1133
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001134 /* shouldnt excl be based on nlh flags??
1135 * Aha! this is anti-netlink really i.e more pfkey derived
1136 * in netlink excl is a flag and you wouldnt need
1137 * a type XFRM_MSG_UPDPOLICY - JHS */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 excl = nlh->nlmsg_type == XFRM_MSG_NEWPOLICY;
1139 err = xfrm_policy_insert(p->dir, xp, excl);
Eric Paris25323862008-04-18 10:09:25 -04001140 xfrm_audit_policy_add(xp, err ? 0 : 1, loginuid, sessionid, sid);
Joy Latten161a09e2006-11-27 13:11:54 -06001141
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 if (err) {
Paul Moore03e1ad72008-04-12 19:07:52 -07001143 security_xfrm_policy_free(xp->security);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144 kfree(xp);
1145 return err;
1146 }
1147
Herbert Xuf60f6b82005-06-18 22:44:37 -07001148 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001149 c.seq = nlh->nlmsg_seq;
1150 c.pid = nlh->nlmsg_pid;
1151 km_policy_notify(xp, p->dir, &c);
1152
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 xfrm_pol_put(xp);
1154
1155 return 0;
1156}
1157
1158static int copy_to_user_tmpl(struct xfrm_policy *xp, struct sk_buff *skb)
1159{
1160 struct xfrm_user_tmpl vec[XFRM_MAX_DEPTH];
1161 int i;
1162
1163 if (xp->xfrm_nr == 0)
1164 return 0;
1165
1166 for (i = 0; i < xp->xfrm_nr; i++) {
1167 struct xfrm_user_tmpl *up = &vec[i];
1168 struct xfrm_tmpl *kp = &xp->xfrm_vec[i];
1169
1170 memcpy(&up->id, &kp->id, sizeof(up->id));
Miika Komu8511d012006-11-30 16:40:51 -08001171 up->family = kp->encap_family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 memcpy(&up->saddr, &kp->saddr, sizeof(up->saddr));
1173 up->reqid = kp->reqid;
1174 up->mode = kp->mode;
1175 up->share = kp->share;
1176 up->optional = kp->optional;
1177 up->aalgos = kp->aalgos;
1178 up->ealgos = kp->ealgos;
1179 up->calgos = kp->calgos;
1180 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181
Thomas Grafc0144be2007-08-22 13:55:43 -07001182 return nla_put(skb, XFRMA_TMPL,
1183 sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr, vec);
Trent Jaegerdf718372005-12-13 23:12:27 -08001184}
1185
Serge Hallyn0d681622006-07-24 23:30:44 -07001186static inline int copy_to_user_state_sec_ctx(struct xfrm_state *x, struct sk_buff *skb)
1187{
1188 if (x->security) {
1189 return copy_sec_ctx(x->security, skb);
1190 }
1191 return 0;
1192}
1193
1194static inline int copy_to_user_sec_ctx(struct xfrm_policy *xp, struct sk_buff *skb)
1195{
1196 if (xp->security) {
1197 return copy_sec_ctx(xp->security, skb);
1198 }
1199 return 0;
1200}
Thomas Grafcfbfd452007-08-22 13:57:04 -07001201static inline size_t userpolicy_type_attrsize(void)
1202{
1203#ifdef CONFIG_XFRM_SUB_POLICY
1204 return nla_total_size(sizeof(struct xfrm_userpolicy_type));
1205#else
1206 return 0;
1207#endif
1208}
Serge Hallyn0d681622006-07-24 23:30:44 -07001209
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001210#ifdef CONFIG_XFRM_SUB_POLICY
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001211static int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001212{
Thomas Grafc0144be2007-08-22 13:55:43 -07001213 struct xfrm_userpolicy_type upt = {
1214 .type = type,
1215 };
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001216
Thomas Grafc0144be2007-08-22 13:55:43 -07001217 return nla_put(skb, XFRMA_POLICY_TYPE, sizeof(upt), &upt);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001218}
1219
1220#else
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001221static inline int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001222{
1223 return 0;
1224}
1225#endif
1226
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227static int dump_one_policy(struct xfrm_policy *xp, int dir, int count, void *ptr)
1228{
1229 struct xfrm_dump_info *sp = ptr;
1230 struct xfrm_userpolicy_info *p;
1231 struct sk_buff *in_skb = sp->in_skb;
1232 struct sk_buff *skb = sp->out_skb;
1233 struct nlmsghdr *nlh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234
Thomas Graf79b8b7f2007-08-22 12:46:53 -07001235 nlh = nlmsg_put(skb, NETLINK_CB(in_skb).pid, sp->nlmsg_seq,
1236 XFRM_MSG_NEWPOLICY, sizeof(*p), sp->nlmsg_flags);
1237 if (nlh == NULL)
1238 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239
Thomas Graf7b67c852007-08-22 13:53:52 -07001240 p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241 copy_to_user_policy(xp, p, dir);
1242 if (copy_to_user_tmpl(xp, skb) < 0)
1243 goto nlmsg_failure;
Trent Jaegerdf718372005-12-13 23:12:27 -08001244 if (copy_to_user_sec_ctx(xp, skb))
1245 goto nlmsg_failure;
Jamal Hadi Salim1459bb32006-11-20 16:51:22 -08001246 if (copy_to_user_policy_type(xp->type, skb) < 0)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001247 goto nlmsg_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248
Thomas Graf98250692007-08-22 12:47:26 -07001249 nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 return 0;
1251
1252nlmsg_failure:
Thomas Graf98250692007-08-22 12:47:26 -07001253 nlmsg_cancel(skb, nlh);
1254 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255}
1256
Timo Teras4c563f72008-02-28 21:31:08 -08001257static int xfrm_dump_policy_done(struct netlink_callback *cb)
1258{
1259 struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *) &cb->args[1];
1260
1261 xfrm_policy_walk_done(walk);
1262 return 0;
1263}
1264
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265static int xfrm_dump_policy(struct sk_buff *skb, struct netlink_callback *cb)
1266{
Timo Teras4c563f72008-02-28 21:31:08 -08001267 struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *) &cb->args[1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 struct xfrm_dump_info info;
1269
Timo Teras4c563f72008-02-28 21:31:08 -08001270 BUILD_BUG_ON(sizeof(struct xfrm_policy_walk) >
1271 sizeof(cb->args) - sizeof(cb->args[0]));
1272
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 info.in_skb = cb->skb;
1274 info.out_skb = skb;
1275 info.nlmsg_seq = cb->nlh->nlmsg_seq;
1276 info.nlmsg_flags = NLM_F_MULTI;
Timo Teras4c563f72008-02-28 21:31:08 -08001277
1278 if (!cb->args[0]) {
1279 cb->args[0] = 1;
1280 xfrm_policy_walk_init(walk, XFRM_POLICY_TYPE_ANY);
1281 }
1282
1283 (void) xfrm_policy_walk(walk, dump_one_policy, &info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284
1285 return skb->len;
1286}
1287
1288static struct sk_buff *xfrm_policy_netlink(struct sk_buff *in_skb,
1289 struct xfrm_policy *xp,
1290 int dir, u32 seq)
1291{
1292 struct xfrm_dump_info info;
1293 struct sk_buff *skb;
1294
Thomas Graf7deb2262007-08-22 13:57:39 -07001295 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 if (!skb)
1297 return ERR_PTR(-ENOMEM);
1298
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299 info.in_skb = in_skb;
1300 info.out_skb = skb;
1301 info.nlmsg_seq = seq;
1302 info.nlmsg_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303
1304 if (dump_one_policy(xp, dir, 0, &info) < 0) {
1305 kfree_skb(skb);
1306 return NULL;
1307 }
1308
1309 return skb;
1310}
1311
Christoph Hellwig22e70052007-01-02 15:22:30 -08001312static int xfrm_get_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001313 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314{
1315 struct xfrm_policy *xp;
1316 struct xfrm_userpolicy_id *p;
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001317 u8 type = XFRM_POLICY_TYPE_MAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318 int err;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001319 struct km_event c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320 int delete;
1321
Thomas Graf7b67c852007-08-22 13:53:52 -07001322 p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 delete = nlh->nlmsg_type == XFRM_MSG_DELPOLICY;
1324
Thomas Graf35a7aa02007-08-22 14:00:40 -07001325 err = copy_from_user_policy_type(&type, attrs);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001326 if (err)
1327 return err;
1328
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329 err = verify_policy_dir(p->dir);
1330 if (err)
1331 return err;
1332
1333 if (p->index)
Eric Parisef41aaa2007-03-07 15:37:58 -08001334 xp = xfrm_policy_byid(type, p->dir, p->index, delete, &err);
Trent Jaegerdf718372005-12-13 23:12:27 -08001335 else {
Thomas Graf5424f322007-08-22 14:01:33 -07001336 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
Paul Moore03e1ad72008-04-12 19:07:52 -07001337 struct xfrm_sec_ctx *ctx;
Trent Jaegerdf718372005-12-13 23:12:27 -08001338
Thomas Graf35a7aa02007-08-22 14:00:40 -07001339 err = verify_sec_ctx_len(attrs);
Trent Jaegerdf718372005-12-13 23:12:27 -08001340 if (err)
1341 return err;
1342
Denis V. Lunev2c8dd112008-04-14 14:47:48 -07001343 ctx = NULL;
Trent Jaegerdf718372005-12-13 23:12:27 -08001344 if (rt) {
Thomas Graf5424f322007-08-22 14:01:33 -07001345 struct xfrm_user_sec_ctx *uctx = nla_data(rt);
Trent Jaegerdf718372005-12-13 23:12:27 -08001346
Paul Moore03e1ad72008-04-12 19:07:52 -07001347 err = security_xfrm_policy_alloc(&ctx, uctx);
1348 if (err)
Trent Jaegerdf718372005-12-13 23:12:27 -08001349 return err;
Denis V. Lunev2c8dd112008-04-14 14:47:48 -07001350 }
Paul Moore03e1ad72008-04-12 19:07:52 -07001351 xp = xfrm_policy_bysel_ctx(type, p->dir, &p->sel, ctx,
Eric Parisef41aaa2007-03-07 15:37:58 -08001352 delete, &err);
Paul Moore03e1ad72008-04-12 19:07:52 -07001353 security_xfrm_policy_free(ctx);
Trent Jaegerdf718372005-12-13 23:12:27 -08001354 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355 if (xp == NULL)
1356 return -ENOENT;
1357
1358 if (!delete) {
1359 struct sk_buff *resp_skb;
1360
1361 resp_skb = xfrm_policy_netlink(skb, xp, p->dir, nlh->nlmsg_seq);
1362 if (IS_ERR(resp_skb)) {
1363 err = PTR_ERR(resp_skb);
1364 } else {
Thomas Graf082a1ad2007-08-22 13:54:36 -07001365 err = nlmsg_unicast(xfrm_nl, resp_skb,
1366 NETLINK_CB(skb).pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367 }
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001368 } else {
Eric Paris25323862008-04-18 10:09:25 -04001369 uid_t loginuid = NETLINK_CB(skb).loginuid;
1370 u32 sessionid = NETLINK_CB(skb).sessionid;
1371 u32 sid = NETLINK_CB(skb).sid;
1372
1373 xfrm_audit_policy_delete(xp, err ? 0 : 1, loginuid, sessionid,
1374 sid);
David S. Miller13fcfbb2007-02-12 13:53:54 -08001375
1376 if (err != 0)
Catherine Zhangc8c05a82006-06-08 23:39:49 -07001377 goto out;
David S. Miller13fcfbb2007-02-12 13:53:54 -08001378
Herbert Xue7443892005-06-18 22:44:18 -07001379 c.data.byid = p->index;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001380 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001381 c.seq = nlh->nlmsg_seq;
1382 c.pid = nlh->nlmsg_pid;
1383 km_policy_notify(xp, p->dir, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384 }
1385
Catherine Zhangc8c05a82006-06-08 23:39:49 -07001386out:
Eric Parisef41aaa2007-03-07 15:37:58 -08001387 xfrm_pol_put(xp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388 return err;
1389}
1390
Christoph Hellwig22e70052007-01-02 15:22:30 -08001391static int xfrm_flush_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001392 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393{
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001394 struct km_event c;
Thomas Graf7b67c852007-08-22 13:53:52 -07001395 struct xfrm_usersa_flush *p = nlmsg_data(nlh);
Joy Latten161a09e2006-11-27 13:11:54 -06001396 struct xfrm_audit audit_info;
Joy Latten4aa2e622007-06-04 19:05:57 -04001397 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398
Joy Latten161a09e2006-11-27 13:11:54 -06001399 audit_info.loginuid = NETLINK_CB(skb).loginuid;
Eric Paris25323862008-04-18 10:09:25 -04001400 audit_info.sessionid = NETLINK_CB(skb).sessionid;
Joy Latten161a09e2006-11-27 13:11:54 -06001401 audit_info.secid = NETLINK_CB(skb).sid;
Joy Latten4aa2e622007-06-04 19:05:57 -04001402 err = xfrm_state_flush(p->proto, &audit_info);
1403 if (err)
1404 return err;
Herbert Xubf088672005-06-18 22:44:00 -07001405 c.data.proto = p->proto;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001406 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001407 c.seq = nlh->nlmsg_seq;
1408 c.pid = nlh->nlmsg_pid;
1409 km_state_notify(NULL, &c);
1410
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411 return 0;
1412}
1413
Thomas Graf7deb2262007-08-22 13:57:39 -07001414static inline size_t xfrm_aevent_msgsize(void)
1415{
1416 return NLMSG_ALIGN(sizeof(struct xfrm_aevent_id))
1417 + nla_total_size(sizeof(struct xfrm_replay_state))
1418 + nla_total_size(sizeof(struct xfrm_lifetime_cur))
1419 + nla_total_size(4) /* XFRM_AE_RTHR */
1420 + nla_total_size(4); /* XFRM_AE_ETHR */
1421}
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001422
1423static int build_aevent(struct sk_buff *skb, struct xfrm_state *x, struct km_event *c)
1424{
1425 struct xfrm_aevent_id *id;
1426 struct nlmsghdr *nlh;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001427
Thomas Graf79b8b7f2007-08-22 12:46:53 -07001428 nlh = nlmsg_put(skb, c->pid, c->seq, XFRM_MSG_NEWAE, sizeof(*id), 0);
1429 if (nlh == NULL)
1430 return -EMSGSIZE;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001431
Thomas Graf7b67c852007-08-22 13:53:52 -07001432 id = nlmsg_data(nlh);
Jamal Hadi Salim2b5f6dc2006-12-02 22:22:25 -08001433 memcpy(&id->sa_id.daddr, &x->id.daddr,sizeof(x->id.daddr));
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001434 id->sa_id.spi = x->id.spi;
1435 id->sa_id.family = x->props.family;
1436 id->sa_id.proto = x->id.proto;
Jamal Hadi Salim2b5f6dc2006-12-02 22:22:25 -08001437 memcpy(&id->saddr, &x->props.saddr,sizeof(x->props.saddr));
1438 id->reqid = x->props.reqid;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001439 id->flags = c->data.aevent;
1440
Thomas Grafc0144be2007-08-22 13:55:43 -07001441 NLA_PUT(skb, XFRMA_REPLAY_VAL, sizeof(x->replay), &x->replay);
1442 NLA_PUT(skb, XFRMA_LTIME_VAL, sizeof(x->curlft), &x->curlft);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001443
Thomas Grafc0144be2007-08-22 13:55:43 -07001444 if (id->flags & XFRM_AE_RTHR)
1445 NLA_PUT_U32(skb, XFRMA_REPLAY_THRESH, x->replay_maxdiff);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001446
Thomas Grafc0144be2007-08-22 13:55:43 -07001447 if (id->flags & XFRM_AE_ETHR)
1448 NLA_PUT_U32(skb, XFRMA_ETIMER_THRESH,
1449 x->replay_maxage * 10 / HZ);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001450
Thomas Graf98250692007-08-22 12:47:26 -07001451 return nlmsg_end(skb, nlh);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001452
Thomas Grafc0144be2007-08-22 13:55:43 -07001453nla_put_failure:
Thomas Graf98250692007-08-22 12:47:26 -07001454 nlmsg_cancel(skb, nlh);
1455 return -EMSGSIZE;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001456}
1457
Christoph Hellwig22e70052007-01-02 15:22:30 -08001458static int xfrm_get_ae(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001459 struct nlattr **attrs)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001460{
1461 struct xfrm_state *x;
1462 struct sk_buff *r_skb;
1463 int err;
1464 struct km_event c;
Thomas Graf7b67c852007-08-22 13:53:52 -07001465 struct xfrm_aevent_id *p = nlmsg_data(nlh);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001466 struct xfrm_usersa_id *id = &p->sa_id;
1467
Thomas Graf7deb2262007-08-22 13:57:39 -07001468 r_skb = nlmsg_new(xfrm_aevent_msgsize(), GFP_ATOMIC);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001469 if (r_skb == NULL)
1470 return -ENOMEM;
1471
1472 x = xfrm_state_lookup(&id->daddr, id->spi, id->proto, id->family);
1473 if (x == NULL) {
Patrick McHardyb08d5842007-02-27 09:57:37 -08001474 kfree_skb(r_skb);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001475 return -ESRCH;
1476 }
1477
1478 /*
1479 * XXX: is this lock really needed - none of the other
1480 * gets lock (the concern is things getting updated
1481 * while we are still reading) - jhs
1482 */
1483 spin_lock_bh(&x->lock);
1484 c.data.aevent = p->flags;
1485 c.seq = nlh->nlmsg_seq;
1486 c.pid = nlh->nlmsg_pid;
1487
1488 if (build_aevent(r_skb, x, &c) < 0)
1489 BUG();
Thomas Graf082a1ad2007-08-22 13:54:36 -07001490 err = nlmsg_unicast(xfrm_nl, r_skb, NETLINK_CB(skb).pid);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001491 spin_unlock_bh(&x->lock);
1492 xfrm_state_put(x);
1493 return err;
1494}
1495
Christoph Hellwig22e70052007-01-02 15:22:30 -08001496static int xfrm_new_ae(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001497 struct nlattr **attrs)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001498{
1499 struct xfrm_state *x;
1500 struct km_event c;
1501 int err = - EINVAL;
Thomas Graf7b67c852007-08-22 13:53:52 -07001502 struct xfrm_aevent_id *p = nlmsg_data(nlh);
Thomas Graf5424f322007-08-22 14:01:33 -07001503 struct nlattr *rp = attrs[XFRMA_REPLAY_VAL];
1504 struct nlattr *lt = attrs[XFRMA_LTIME_VAL];
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001505
1506 if (!lt && !rp)
1507 return err;
1508
1509 /* pedantic mode - thou shalt sayeth replaceth */
1510 if (!(nlh->nlmsg_flags&NLM_F_REPLACE))
1511 return err;
1512
1513 x = xfrm_state_lookup(&p->sa_id.daddr, p->sa_id.spi, p->sa_id.proto, p->sa_id.family);
1514 if (x == NULL)
1515 return -ESRCH;
1516
1517 if (x->km.state != XFRM_STATE_VALID)
1518 goto out;
1519
1520 spin_lock_bh(&x->lock);
Thomas Graf35a7aa02007-08-22 14:00:40 -07001521 xfrm_update_ae_params(x, attrs);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001522 spin_unlock_bh(&x->lock);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001523
1524 c.event = nlh->nlmsg_type;
1525 c.seq = nlh->nlmsg_seq;
1526 c.pid = nlh->nlmsg_pid;
1527 c.data.aevent = XFRM_AE_CU;
1528 km_state_notify(x, &c);
1529 err = 0;
1530out:
1531 xfrm_state_put(x);
1532 return err;
1533}
1534
Christoph Hellwig22e70052007-01-02 15:22:30 -08001535static int xfrm_flush_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001536 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537{
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001538 struct km_event c;
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001539 u8 type = XFRM_POLICY_TYPE_MAIN;
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001540 int err;
Joy Latten161a09e2006-11-27 13:11:54 -06001541 struct xfrm_audit audit_info;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001542
Thomas Graf35a7aa02007-08-22 14:00:40 -07001543 err = copy_from_user_policy_type(&type, attrs);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001544 if (err)
1545 return err;
1546
Joy Latten161a09e2006-11-27 13:11:54 -06001547 audit_info.loginuid = NETLINK_CB(skb).loginuid;
Eric Paris25323862008-04-18 10:09:25 -04001548 audit_info.sessionid = NETLINK_CB(skb).sessionid;
Joy Latten161a09e2006-11-27 13:11:54 -06001549 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];
Paul Moore03e1ad72008-04-12 19:07:52 -07001578 struct xfrm_sec_ctx *ctx;
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001579
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
Denis V. Lunev2c8dd112008-04-14 14:47:48 -07001584 ctx = NULL;
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001585 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
Paul Moore03e1ad72008-04-12 19:07:52 -07001588 err = security_xfrm_policy_alloc(&ctx, uctx);
1589 if (err)
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001590 return err;
Denis V. Lunev2c8dd112008-04-14 14:47:48 -07001591 }
Paul Moore03e1ad72008-04-12 19:07:52 -07001592 xp = xfrm_policy_bysel_ctx(type, p->dir, &p->sel, ctx, 0, &err);
1593 security_xfrm_policy_free(ctx);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001594 }
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001595 if (xp == NULL)
Eric Parisef41aaa2007-03-07 15:37:58 -08001596 return -ENOENT;
Paul Moore03e1ad72008-04-12 19:07:52 -07001597
Eric Parisef41aaa2007-03-07 15:37:58 -08001598 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) {
Eric Paris25323862008-04-18 10:09:25 -04001607 uid_t loginuid = NETLINK_CB(skb).loginuid;
1608 uid_t sessionid = NETLINK_CB(skb).sessionid;
1609 u32 sid = NETLINK_CB(skb).sid;
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001610 xfrm_policy_delete(xp, p->dir);
Eric Paris25323862008-04-18 10:09:25 -04001611 xfrm_audit_policy_delete(xp, 1, loginuid, sessionid, sid);
Joy Latten161a09e2006-11-27 13:11:54 -06001612
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001613 } else {
1614 // reset the timers here?
1615 printk("Dont know what to do with soft policy expire\n");
1616 }
1617 km_policy_expired(xp, p->dir, up->hard, current->pid);
1618
1619out:
1620 xfrm_pol_put(xp);
1621 return err;
1622}
1623
Christoph Hellwig22e70052007-01-02 15:22:30 -08001624static int xfrm_add_sa_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001625 struct nlattr **attrs)
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001626{
1627 struct xfrm_state *x;
1628 int err;
Thomas Graf7b67c852007-08-22 13:53:52 -07001629 struct xfrm_user_expire *ue = nlmsg_data(nlh);
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001630 struct xfrm_usersa_info *p = &ue->state;
1631
1632 x = xfrm_state_lookup(&p->id.daddr, p->id.spi, p->id.proto, p->family);
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001633
David S. Miller3a765aa2007-02-26 14:52:21 -08001634 err = -ENOENT;
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001635 if (x == NULL)
1636 return err;
1637
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001638 spin_lock_bh(&x->lock);
David S. Miller3a765aa2007-02-26 14:52:21 -08001639 err = -EINVAL;
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001640 if (x->km.state != XFRM_STATE_VALID)
1641 goto out;
1642 km_state_expired(x, ue->hard, current->pid);
1643
Joy Latten161a09e2006-11-27 13:11:54 -06001644 if (ue->hard) {
Eric Paris25323862008-04-18 10:09:25 -04001645 uid_t loginuid = NETLINK_CB(skb).loginuid;
1646 uid_t sessionid = NETLINK_CB(skb).sessionid;
1647 u32 sid = NETLINK_CB(skb).sid;
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001648 __xfrm_state_delete(x);
Eric Paris25323862008-04-18 10:09:25 -04001649 xfrm_audit_state_delete(x, 1, loginuid, sessionid, sid);
Joy Latten161a09e2006-11-27 13:11:54 -06001650 }
David S. Miller3a765aa2007-02-26 14:52:21 -08001651 err = 0;
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001652out:
1653 spin_unlock_bh(&x->lock);
1654 xfrm_state_put(x);
1655 return err;
1656}
1657
Christoph Hellwig22e70052007-01-02 15:22:30 -08001658static int xfrm_add_acquire(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001659 struct nlattr **attrs)
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001660{
1661 struct xfrm_policy *xp;
1662 struct xfrm_user_tmpl *ut;
1663 int i;
Thomas Graf5424f322007-08-22 14:01:33 -07001664 struct nlattr *rt = attrs[XFRMA_TMPL];
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001665
Thomas Graf7b67c852007-08-22 13:53:52 -07001666 struct xfrm_user_acquire *ua = nlmsg_data(nlh);
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001667 struct xfrm_state *x = xfrm_state_alloc();
1668 int err = -ENOMEM;
1669
1670 if (!x)
1671 return err;
1672
1673 err = verify_newpolicy_info(&ua->policy);
1674 if (err) {
1675 printk("BAD policy passed\n");
1676 kfree(x);
1677 return err;
1678 }
1679
1680 /* build an XP */
Thomas Graf5424f322007-08-22 14:01:33 -07001681 xp = xfrm_policy_construct(&ua->policy, attrs, &err);
David S. Millerb4ad86bf2006-12-03 19:19:26 -08001682 if (!xp) {
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001683 kfree(x);
1684 return err;
1685 }
1686
1687 memcpy(&x->id, &ua->id, sizeof(ua->id));
1688 memcpy(&x->props.saddr, &ua->saddr, sizeof(ua->saddr));
1689 memcpy(&x->sel, &ua->sel, sizeof(ua->sel));
1690
Thomas Graf5424f322007-08-22 14:01:33 -07001691 ut = nla_data(rt);
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001692 /* extract the templates and for each call km_key */
1693 for (i = 0; i < xp->xfrm_nr; i++, ut++) {
1694 struct xfrm_tmpl *t = &xp->xfrm_vec[i];
1695 memcpy(&x->id, &t->id, sizeof(x->id));
1696 x->props.mode = t->mode;
1697 x->props.reqid = t->reqid;
1698 x->props.family = ut->family;
1699 t->aalgos = ua->aalgos;
1700 t->ealgos = ua->ealgos;
1701 t->calgos = ua->calgos;
1702 err = km_query(x, t, xp);
1703
1704 }
1705
1706 kfree(x);
1707 kfree(xp);
1708
1709 return 0;
1710}
1711
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001712#ifdef CONFIG_XFRM_MIGRATE
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001713static int copy_from_user_migrate(struct xfrm_migrate *ma,
Thomas Graf5424f322007-08-22 14:01:33 -07001714 struct nlattr **attrs, int *num)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001715{
Thomas Graf5424f322007-08-22 14:01:33 -07001716 struct nlattr *rt = attrs[XFRMA_MIGRATE];
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001717 struct xfrm_user_migrate *um;
1718 int i, num_migrate;
1719
Thomas Graf5424f322007-08-22 14:01:33 -07001720 um = nla_data(rt);
1721 num_migrate = nla_len(rt) / sizeof(*um);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001722
1723 if (num_migrate <= 0 || num_migrate > XFRM_MAX_DEPTH)
1724 return -EINVAL;
1725
1726 for (i = 0; i < num_migrate; i++, um++, ma++) {
1727 memcpy(&ma->old_daddr, &um->old_daddr, sizeof(ma->old_daddr));
1728 memcpy(&ma->old_saddr, &um->old_saddr, sizeof(ma->old_saddr));
1729 memcpy(&ma->new_daddr, &um->new_daddr, sizeof(ma->new_daddr));
1730 memcpy(&ma->new_saddr, &um->new_saddr, sizeof(ma->new_saddr));
1731
1732 ma->proto = um->proto;
1733 ma->mode = um->mode;
1734 ma->reqid = um->reqid;
1735
1736 ma->old_family = um->old_family;
1737 ma->new_family = um->new_family;
1738 }
1739
1740 *num = i;
1741 return 0;
1742}
1743
1744static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001745 struct nlattr **attrs)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001746{
Thomas Graf7b67c852007-08-22 13:53:52 -07001747 struct xfrm_userpolicy_id *pi = nlmsg_data(nlh);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001748 struct xfrm_migrate m[XFRM_MAX_DEPTH];
1749 u8 type;
1750 int err;
1751 int n = 0;
1752
Thomas Graf35a7aa02007-08-22 14:00:40 -07001753 if (attrs[XFRMA_MIGRATE] == NULL)
Thomas Grafcf5cb792007-08-22 13:59:04 -07001754 return -EINVAL;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001755
Thomas Graf5424f322007-08-22 14:01:33 -07001756 err = copy_from_user_policy_type(&type, attrs);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001757 if (err)
1758 return err;
1759
1760 err = copy_from_user_migrate((struct xfrm_migrate *)m,
Thomas Graf5424f322007-08-22 14:01:33 -07001761 attrs, &n);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001762 if (err)
1763 return err;
1764
1765 if (!n)
1766 return 0;
1767
1768 xfrm_migrate(&pi->sel, pi->dir, type, m, n);
1769
1770 return 0;
1771}
1772#else
1773static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001774 struct nlattr **attrs)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001775{
1776 return -ENOPROTOOPT;
1777}
1778#endif
1779
1780#ifdef CONFIG_XFRM_MIGRATE
1781static int copy_to_user_migrate(struct xfrm_migrate *m, struct sk_buff *skb)
1782{
1783 struct xfrm_user_migrate um;
1784
1785 memset(&um, 0, sizeof(um));
1786 um.proto = m->proto;
1787 um.mode = m->mode;
1788 um.reqid = m->reqid;
1789 um.old_family = m->old_family;
1790 memcpy(&um.old_daddr, &m->old_daddr, sizeof(um.old_daddr));
1791 memcpy(&um.old_saddr, &m->old_saddr, sizeof(um.old_saddr));
1792 um.new_family = m->new_family;
1793 memcpy(&um.new_daddr, &m->new_daddr, sizeof(um.new_daddr));
1794 memcpy(&um.new_saddr, &m->new_saddr, sizeof(um.new_saddr));
1795
Thomas Grafc0144be2007-08-22 13:55:43 -07001796 return nla_put(skb, XFRMA_MIGRATE, sizeof(um), &um);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001797}
1798
Thomas Graf7deb2262007-08-22 13:57:39 -07001799static inline size_t xfrm_migrate_msgsize(int num_migrate)
1800{
1801 return NLMSG_ALIGN(sizeof(struct xfrm_userpolicy_id))
1802 + nla_total_size(sizeof(struct xfrm_user_migrate) * num_migrate)
1803 + userpolicy_type_attrsize();
1804}
1805
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001806static int build_migrate(struct sk_buff *skb, struct xfrm_migrate *m,
1807 int num_migrate, struct xfrm_selector *sel,
1808 u8 dir, u8 type)
1809{
1810 struct xfrm_migrate *mp;
1811 struct xfrm_userpolicy_id *pol_id;
1812 struct nlmsghdr *nlh;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001813 int i;
1814
Thomas Graf79b8b7f2007-08-22 12:46:53 -07001815 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_MIGRATE, sizeof(*pol_id), 0);
1816 if (nlh == NULL)
1817 return -EMSGSIZE;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001818
Thomas Graf7b67c852007-08-22 13:53:52 -07001819 pol_id = nlmsg_data(nlh);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001820 /* copy data from selector, dir, and type to the pol_id */
1821 memset(pol_id, 0, sizeof(*pol_id));
1822 memcpy(&pol_id->sel, sel, sizeof(pol_id->sel));
1823 pol_id->dir = dir;
1824
1825 if (copy_to_user_policy_type(type, skb) < 0)
1826 goto nlmsg_failure;
1827
1828 for (i = 0, mp = m ; i < num_migrate; i++, mp++) {
1829 if (copy_to_user_migrate(mp, skb) < 0)
1830 goto nlmsg_failure;
1831 }
1832
Thomas Graf98250692007-08-22 12:47:26 -07001833 return nlmsg_end(skb, nlh);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001834nlmsg_failure:
Thomas Graf98250692007-08-22 12:47:26 -07001835 nlmsg_cancel(skb, nlh);
1836 return -EMSGSIZE;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001837}
1838
1839static int xfrm_send_migrate(struct xfrm_selector *sel, u8 dir, u8 type,
1840 struct xfrm_migrate *m, int num_migrate)
1841{
1842 struct sk_buff *skb;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001843
Thomas Graf7deb2262007-08-22 13:57:39 -07001844 skb = nlmsg_new(xfrm_migrate_msgsize(num_migrate), GFP_ATOMIC);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001845 if (skb == NULL)
1846 return -ENOMEM;
1847
1848 /* build migrate */
1849 if (build_migrate(skb, m, num_migrate, sel, dir, type) < 0)
1850 BUG();
1851
Thomas Graf082a1ad2007-08-22 13:54:36 -07001852 return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_MIGRATE, GFP_ATOMIC);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001853}
1854#else
1855static int xfrm_send_migrate(struct xfrm_selector *sel, u8 dir, u8 type,
1856 struct xfrm_migrate *m, int num_migrate)
1857{
1858 return -ENOPROTOOPT;
1859}
1860#endif
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001861
Thomas Grafa7bd9a42007-08-22 13:58:18 -07001862#define XMSGSIZE(type) sizeof(struct type)
Thomas Graf492b5582005-05-03 14:26:40 -07001863
1864static const int xfrm_msg_min[XFRM_NR_MSGTYPES] = {
1865 [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
1866 [XFRM_MSG_DELSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
1867 [XFRM_MSG_GETSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
1868 [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
1869 [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
1870 [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
1871 [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userspi_info),
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001872 [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_acquire),
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001873 [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_expire),
Thomas Graf492b5582005-05-03 14:26:40 -07001874 [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
1875 [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001876 [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_polexpire),
Thomas Graf492b5582005-05-03 14:26:40 -07001877 [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_flush),
Thomas Grafa7bd9a42007-08-22 13:58:18 -07001878 [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = 0,
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001879 [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
1880 [XFRM_MSG_GETAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07001881 [XFRM_MSG_REPORT - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_report),
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001882 [XFRM_MSG_MIGRATE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
Thomas Grafa7bd9a42007-08-22 13:58:18 -07001883 [XFRM_MSG_GETSADINFO - XFRM_MSG_BASE] = sizeof(u32),
1884 [XFRM_MSG_GETSPDINFO - XFRM_MSG_BASE] = sizeof(u32),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885};
1886
Thomas Graf492b5582005-05-03 14:26:40 -07001887#undef XMSGSIZE
1888
Thomas Grafcf5cb792007-08-22 13:59:04 -07001889static const struct nla_policy xfrma_policy[XFRMA_MAX+1] = {
Herbert Xu1a6509d2008-01-28 19:37:29 -08001890 [XFRMA_ALG_AEAD] = { .len = sizeof(struct xfrm_algo_aead) },
Thomas Grafcf5cb792007-08-22 13:59:04 -07001891 [XFRMA_ALG_AUTH] = { .len = sizeof(struct xfrm_algo) },
1892 [XFRMA_ALG_CRYPT] = { .len = sizeof(struct xfrm_algo) },
1893 [XFRMA_ALG_COMP] = { .len = sizeof(struct xfrm_algo) },
1894 [XFRMA_ENCAP] = { .len = sizeof(struct xfrm_encap_tmpl) },
1895 [XFRMA_TMPL] = { .len = sizeof(struct xfrm_user_tmpl) },
1896 [XFRMA_SEC_CTX] = { .len = sizeof(struct xfrm_sec_ctx) },
1897 [XFRMA_LTIME_VAL] = { .len = sizeof(struct xfrm_lifetime_cur) },
1898 [XFRMA_REPLAY_VAL] = { .len = sizeof(struct xfrm_replay_state) },
1899 [XFRMA_REPLAY_THRESH] = { .type = NLA_U32 },
1900 [XFRMA_ETIMER_THRESH] = { .type = NLA_U32 },
1901 [XFRMA_SRCADDR] = { .len = sizeof(xfrm_address_t) },
1902 [XFRMA_COADDR] = { .len = sizeof(xfrm_address_t) },
1903 [XFRMA_POLICY_TYPE] = { .len = sizeof(struct xfrm_userpolicy_type)},
1904 [XFRMA_MIGRATE] = { .len = sizeof(struct xfrm_user_migrate) },
1905};
1906
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907static struct xfrm_link {
Thomas Graf5424f322007-08-22 14:01:33 -07001908 int (*doit)(struct sk_buff *, struct nlmsghdr *, struct nlattr **);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909 int (*dump)(struct sk_buff *, struct netlink_callback *);
Timo Teras4c563f72008-02-28 21:31:08 -08001910 int (*done)(struct netlink_callback *);
Thomas Graf492b5582005-05-03 14:26:40 -07001911} xfrm_dispatch[XFRM_NR_MSGTYPES] = {
1912 [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa },
1913 [XFRM_MSG_DELSA - XFRM_MSG_BASE] = { .doit = xfrm_del_sa },
1914 [XFRM_MSG_GETSA - XFRM_MSG_BASE] = { .doit = xfrm_get_sa,
Timo Teras4c563f72008-02-28 21:31:08 -08001915 .dump = xfrm_dump_sa,
1916 .done = xfrm_dump_sa_done },
Thomas Graf492b5582005-05-03 14:26:40 -07001917 [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy },
1918 [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy },
1919 [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy,
Timo Teras4c563f72008-02-28 21:31:08 -08001920 .dump = xfrm_dump_policy,
1921 .done = xfrm_dump_policy_done },
Thomas Graf492b5582005-05-03 14:26:40 -07001922 [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = { .doit = xfrm_alloc_userspi },
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001923 [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_acquire },
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001924 [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_sa_expire },
Thomas Graf492b5582005-05-03 14:26:40 -07001925 [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy },
1926 [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa },
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001927 [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_pol_expire},
Thomas Graf492b5582005-05-03 14:26:40 -07001928 [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = { .doit = xfrm_flush_sa },
1929 [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_flush_policy },
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001930 [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = { .doit = xfrm_new_ae },
1931 [XFRM_MSG_GETAE - XFRM_MSG_BASE] = { .doit = xfrm_get_ae },
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001932 [XFRM_MSG_MIGRATE - XFRM_MSG_BASE] = { .doit = xfrm_do_migrate },
Jamal Hadi Salim566ec032007-04-26 14:12:15 -07001933 [XFRM_MSG_GETSADINFO - XFRM_MSG_BASE] = { .doit = xfrm_get_sadinfo },
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -07001934 [XFRM_MSG_GETSPDINFO - XFRM_MSG_BASE] = { .doit = xfrm_get_spdinfo },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935};
1936
Thomas Graf1d00a4e2007-03-22 23:30:12 -07001937static int xfrm_user_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938{
Thomas Graf35a7aa02007-08-22 14:00:40 -07001939 struct nlattr *attrs[XFRMA_MAX+1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940 struct xfrm_link *link;
Thomas Grafa7bd9a42007-08-22 13:58:18 -07001941 int type, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943 type = nlh->nlmsg_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944 if (type > XFRM_MSG_MAX)
Thomas Graf1d00a4e2007-03-22 23:30:12 -07001945 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946
1947 type -= XFRM_MSG_BASE;
1948 link = &xfrm_dispatch[type];
1949
1950 /* All operations require privileges, even GET */
Thomas Graf1d00a4e2007-03-22 23:30:12 -07001951 if (security_netlink_recv(skb, CAP_NET_ADMIN))
1952 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953
Thomas Graf492b5582005-05-03 14:26:40 -07001954 if ((type == (XFRM_MSG_GETSA - XFRM_MSG_BASE) ||
1955 type == (XFRM_MSG_GETPOLICY - XFRM_MSG_BASE)) &&
1956 (nlh->nlmsg_flags & NLM_F_DUMP)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957 if (link->dump == NULL)
Thomas Graf1d00a4e2007-03-22 23:30:12 -07001958 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959
Timo Teras4c563f72008-02-28 21:31:08 -08001960 return netlink_dump_start(xfrm_nl, skb, nlh, link->dump, link->done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961 }
1962
Thomas Graf35a7aa02007-08-22 14:00:40 -07001963 err = nlmsg_parse(nlh, xfrm_msg_min[type], attrs, XFRMA_MAX,
Thomas Grafcf5cb792007-08-22 13:59:04 -07001964 xfrma_policy);
Thomas Grafa7bd9a42007-08-22 13:58:18 -07001965 if (err < 0)
1966 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967
1968 if (link->doit == NULL)
Thomas Graf1d00a4e2007-03-22 23:30:12 -07001969 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970
Thomas Graf5424f322007-08-22 14:01:33 -07001971 return link->doit(skb, nlh, attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972}
1973
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07001974static void xfrm_netlink_rcv(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975{
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07001976 mutex_lock(&xfrm_cfg_mutex);
1977 netlink_rcv_skb(skb, &xfrm_user_rcv_msg);
1978 mutex_unlock(&xfrm_cfg_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979}
1980
Thomas Graf7deb2262007-08-22 13:57:39 -07001981static inline size_t xfrm_expire_msgsize(void)
1982{
1983 return NLMSG_ALIGN(sizeof(struct xfrm_user_expire));
1984}
1985
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001986static int build_expire(struct sk_buff *skb, struct xfrm_state *x, struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987{
1988 struct xfrm_user_expire *ue;
1989 struct nlmsghdr *nlh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990
Thomas Graf79b8b7f2007-08-22 12:46:53 -07001991 nlh = nlmsg_put(skb, c->pid, 0, XFRM_MSG_EXPIRE, sizeof(*ue), 0);
1992 if (nlh == NULL)
1993 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994
Thomas Graf7b67c852007-08-22 13:53:52 -07001995 ue = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996 copy_to_user_state(x, &ue->state);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001997 ue->hard = (c->data.hard != 0) ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998
Thomas Graf98250692007-08-22 12:47:26 -07001999 return nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000}
2001
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002002static int xfrm_exp_state_notify(struct xfrm_state *x, struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003{
2004 struct sk_buff *skb;
2005
Thomas Graf7deb2262007-08-22 13:57:39 -07002006 skb = nlmsg_new(xfrm_expire_msgsize(), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007 if (skb == NULL)
2008 return -ENOMEM;
2009
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002010 if (build_expire(skb, x, c) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011 BUG();
2012
Thomas Graf082a1ad2007-08-22 13:54:36 -07002013 return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_EXPIRE, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014}
2015
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002016static int xfrm_aevent_state_notify(struct xfrm_state *x, struct km_event *c)
2017{
2018 struct sk_buff *skb;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002019
Thomas Graf7deb2262007-08-22 13:57:39 -07002020 skb = nlmsg_new(xfrm_aevent_msgsize(), GFP_ATOMIC);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002021 if (skb == NULL)
2022 return -ENOMEM;
2023
2024 if (build_aevent(skb, x, c) < 0)
2025 BUG();
2026
Thomas Graf082a1ad2007-08-22 13:54:36 -07002027 return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_AEVENTS, GFP_ATOMIC);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002028}
2029
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002030static int xfrm_notify_sa_flush(struct km_event *c)
2031{
2032 struct xfrm_usersa_flush *p;
2033 struct nlmsghdr *nlh;
2034 struct sk_buff *skb;
Thomas Graf7deb2262007-08-22 13:57:39 -07002035 int len = NLMSG_ALIGN(sizeof(struct xfrm_usersa_flush));
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002036
Thomas Graf7deb2262007-08-22 13:57:39 -07002037 skb = nlmsg_new(len, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002038 if (skb == NULL)
2039 return -ENOMEM;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002040
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002041 nlh = nlmsg_put(skb, c->pid, c->seq, XFRM_MSG_FLUSHSA, sizeof(*p), 0);
2042 if (nlh == NULL) {
2043 kfree_skb(skb);
2044 return -EMSGSIZE;
2045 }
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002046
Thomas Graf7b67c852007-08-22 13:53:52 -07002047 p = nlmsg_data(nlh);
Herbert Xubf088672005-06-18 22:44:00 -07002048 p->proto = c->data.proto;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002049
Thomas Graf98250692007-08-22 12:47:26 -07002050 nlmsg_end(skb, nlh);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002051
Thomas Graf082a1ad2007-08-22 13:54:36 -07002052 return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_SA, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002053}
2054
Thomas Graf7deb2262007-08-22 13:57:39 -07002055static inline size_t xfrm_sa_len(struct xfrm_state *x)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002056{
Thomas Graf7deb2262007-08-22 13:57:39 -07002057 size_t l = 0;
Herbert Xu1a6509d2008-01-28 19:37:29 -08002058 if (x->aead)
2059 l += nla_total_size(aead_len(x->aead));
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002060 if (x->aalg)
Eric Dumazet0f99be02008-01-08 23:39:06 -08002061 l += nla_total_size(xfrm_alg_len(x->aalg));
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002062 if (x->ealg)
Eric Dumazet0f99be02008-01-08 23:39:06 -08002063 l += nla_total_size(xfrm_alg_len(x->ealg));
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002064 if (x->calg)
Thomas Graf7deb2262007-08-22 13:57:39 -07002065 l += nla_total_size(sizeof(*x->calg));
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002066 if (x->encap)
Thomas Graf7deb2262007-08-22 13:57:39 -07002067 l += nla_total_size(sizeof(*x->encap));
Herbert Xu68325d32007-10-09 13:30:57 -07002068 if (x->security)
2069 l += nla_total_size(sizeof(struct xfrm_user_sec_ctx) +
2070 x->security->ctx_len);
2071 if (x->coaddr)
2072 l += nla_total_size(sizeof(*x->coaddr));
2073
Herbert Xud26f3982007-11-13 21:47:08 -08002074 /* Must count x->lastused as it may become non-zero behind our back. */
2075 l += nla_total_size(sizeof(u64));
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002076
2077 return l;
2078}
2079
2080static int xfrm_notify_sa(struct xfrm_state *x, struct km_event *c)
2081{
2082 struct xfrm_usersa_info *p;
Herbert Xu0603eac2005-06-18 22:54:36 -07002083 struct xfrm_usersa_id *id;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002084 struct nlmsghdr *nlh;
2085 struct sk_buff *skb;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002086 int len = xfrm_sa_len(x);
Herbert Xu0603eac2005-06-18 22:54:36 -07002087 int headlen;
2088
2089 headlen = sizeof(*p);
2090 if (c->event == XFRM_MSG_DELSA) {
Thomas Graf7deb2262007-08-22 13:57:39 -07002091 len += nla_total_size(headlen);
Herbert Xu0603eac2005-06-18 22:54:36 -07002092 headlen = sizeof(*id);
2093 }
Thomas Graf7deb2262007-08-22 13:57:39 -07002094 len += NLMSG_ALIGN(headlen);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002095
Thomas Graf7deb2262007-08-22 13:57:39 -07002096 skb = nlmsg_new(len, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002097 if (skb == NULL)
2098 return -ENOMEM;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002099
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002100 nlh = nlmsg_put(skb, c->pid, c->seq, c->event, headlen, 0);
2101 if (nlh == NULL)
Thomas Grafc0144be2007-08-22 13:55:43 -07002102 goto nla_put_failure;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002103
Thomas Graf7b67c852007-08-22 13:53:52 -07002104 p = nlmsg_data(nlh);
Herbert Xu0603eac2005-06-18 22:54:36 -07002105 if (c->event == XFRM_MSG_DELSA) {
Thomas Grafc0144be2007-08-22 13:55:43 -07002106 struct nlattr *attr;
2107
Thomas Graf7b67c852007-08-22 13:53:52 -07002108 id = nlmsg_data(nlh);
Herbert Xu0603eac2005-06-18 22:54:36 -07002109 memcpy(&id->daddr, &x->id.daddr, sizeof(id->daddr));
2110 id->spi = x->id.spi;
2111 id->family = x->props.family;
2112 id->proto = x->id.proto;
2113
Thomas Grafc0144be2007-08-22 13:55:43 -07002114 attr = nla_reserve(skb, XFRMA_SA, sizeof(*p));
2115 if (attr == NULL)
2116 goto nla_put_failure;
2117
2118 p = nla_data(attr);
Herbert Xu0603eac2005-06-18 22:54:36 -07002119 }
2120
Herbert Xu68325d32007-10-09 13:30:57 -07002121 if (copy_to_user_state_extra(x, p, skb))
2122 goto nla_put_failure;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002123
Thomas Graf98250692007-08-22 12:47:26 -07002124 nlmsg_end(skb, nlh);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002125
Thomas Graf082a1ad2007-08-22 13:54:36 -07002126 return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_SA, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002127
Thomas Grafc0144be2007-08-22 13:55:43 -07002128nla_put_failure:
Herbert Xu68325d32007-10-09 13:30:57 -07002129 /* Somebody screwed up with xfrm_sa_len! */
2130 WARN_ON(1);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002131 kfree_skb(skb);
2132 return -1;
2133}
2134
2135static int xfrm_send_state_notify(struct xfrm_state *x, struct km_event *c)
2136{
2137
2138 switch (c->event) {
Herbert Xuf60f6b82005-06-18 22:44:37 -07002139 case XFRM_MSG_EXPIRE:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002140 return xfrm_exp_state_notify(x, c);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002141 case XFRM_MSG_NEWAE:
2142 return xfrm_aevent_state_notify(x, c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07002143 case XFRM_MSG_DELSA:
2144 case XFRM_MSG_UPDSA:
2145 case XFRM_MSG_NEWSA:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002146 return xfrm_notify_sa(x, c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07002147 case XFRM_MSG_FLUSHSA:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002148 return xfrm_notify_sa_flush(c);
2149 default:
2150 printk("xfrm_user: Unknown SA event %d\n", c->event);
2151 break;
2152 }
2153
2154 return 0;
2155
2156}
2157
Thomas Graf7deb2262007-08-22 13:57:39 -07002158static inline size_t xfrm_acquire_msgsize(struct xfrm_state *x,
2159 struct xfrm_policy *xp)
2160{
2161 return NLMSG_ALIGN(sizeof(struct xfrm_user_acquire))
2162 + nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr)
2163 + nla_total_size(xfrm_user_sec_ctx_size(x->security))
2164 + userpolicy_type_attrsize();
2165}
2166
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167static int build_acquire(struct sk_buff *skb, struct xfrm_state *x,
2168 struct xfrm_tmpl *xt, struct xfrm_policy *xp,
2169 int dir)
2170{
2171 struct xfrm_user_acquire *ua;
2172 struct nlmsghdr *nlh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173 __u32 seq = xfrm_get_acqseq();
2174
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002175 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_ACQUIRE, sizeof(*ua), 0);
2176 if (nlh == NULL)
2177 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178
Thomas Graf7b67c852007-08-22 13:53:52 -07002179 ua = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180 memcpy(&ua->id, &x->id, sizeof(ua->id));
2181 memcpy(&ua->saddr, &x->props.saddr, sizeof(ua->saddr));
2182 memcpy(&ua->sel, &x->sel, sizeof(ua->sel));
2183 copy_to_user_policy(xp, &ua->policy, dir);
2184 ua->aalgos = xt->aalgos;
2185 ua->ealgos = xt->ealgos;
2186 ua->calgos = xt->calgos;
2187 ua->seq = x->km.seq = seq;
2188
2189 if (copy_to_user_tmpl(xp, skb) < 0)
2190 goto nlmsg_failure;
Serge Hallyn0d681622006-07-24 23:30:44 -07002191 if (copy_to_user_state_sec_ctx(x, skb))
Trent Jaegerdf718372005-12-13 23:12:27 -08002192 goto nlmsg_failure;
Jamal Hadi Salim1459bb32006-11-20 16:51:22 -08002193 if (copy_to_user_policy_type(xp->type, skb) < 0)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07002194 goto nlmsg_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195
Thomas Graf98250692007-08-22 12:47:26 -07002196 return nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197
2198nlmsg_failure:
Thomas Graf98250692007-08-22 12:47:26 -07002199 nlmsg_cancel(skb, nlh);
2200 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201}
2202
2203static int xfrm_send_acquire(struct xfrm_state *x, struct xfrm_tmpl *xt,
2204 struct xfrm_policy *xp, int dir)
2205{
2206 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207
Thomas Graf7deb2262007-08-22 13:57:39 -07002208 skb = nlmsg_new(xfrm_acquire_msgsize(x, xp), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209 if (skb == NULL)
2210 return -ENOMEM;
2211
2212 if (build_acquire(skb, x, xt, xp, dir) < 0)
2213 BUG();
2214
Thomas Graf082a1ad2007-08-22 13:54:36 -07002215 return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_ACQUIRE, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002216}
2217
2218/* User gives us xfrm_user_policy_info followed by an array of 0
2219 * or more templates.
2220 */
Venkat Yekkiralacb969f02006-07-24 23:32:20 -07002221static struct xfrm_policy *xfrm_compile_policy(struct sock *sk, int opt,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002222 u8 *data, int len, int *dir)
2223{
2224 struct xfrm_userpolicy_info *p = (struct xfrm_userpolicy_info *)data;
2225 struct xfrm_user_tmpl *ut = (struct xfrm_user_tmpl *) (p + 1);
2226 struct xfrm_policy *xp;
2227 int nr;
2228
Venkat Yekkiralacb969f02006-07-24 23:32:20 -07002229 switch (sk->sk_family) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002230 case AF_INET:
2231 if (opt != IP_XFRM_POLICY) {
2232 *dir = -EOPNOTSUPP;
2233 return NULL;
2234 }
2235 break;
2236#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
2237 case AF_INET6:
2238 if (opt != IPV6_XFRM_POLICY) {
2239 *dir = -EOPNOTSUPP;
2240 return NULL;
2241 }
2242 break;
2243#endif
2244 default:
2245 *dir = -EINVAL;
2246 return NULL;
2247 }
2248
2249 *dir = -EINVAL;
2250
2251 if (len < sizeof(*p) ||
2252 verify_newpolicy_info(p))
2253 return NULL;
2254
2255 nr = ((len - sizeof(*p)) / sizeof(*ut));
David S. Millerb4ad86bf2006-12-03 19:19:26 -08002256 if (validate_tmpl(nr, ut, p->sel.family))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257 return NULL;
2258
Herbert Xua4f1bac2005-07-26 15:43:17 -07002259 if (p->dir > XFRM_POLICY_OUT)
2260 return NULL;
2261
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262 xp = xfrm_policy_alloc(GFP_KERNEL);
2263 if (xp == NULL) {
2264 *dir = -ENOBUFS;
2265 return NULL;
2266 }
2267
2268 copy_from_user_policy(xp, p);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07002269 xp->type = XFRM_POLICY_TYPE_MAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002270 copy_templates(xp, ut, nr);
2271
2272 *dir = p->dir;
2273
2274 return xp;
2275}
2276
Thomas Graf7deb2262007-08-22 13:57:39 -07002277static inline size_t xfrm_polexpire_msgsize(struct xfrm_policy *xp)
2278{
2279 return NLMSG_ALIGN(sizeof(struct xfrm_user_polexpire))
2280 + nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr)
2281 + nla_total_size(xfrm_user_sec_ctx_size(xp->security))
2282 + userpolicy_type_attrsize();
2283}
2284
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285static int build_polexpire(struct sk_buff *skb, struct xfrm_policy *xp,
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002286 int dir, struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287{
2288 struct xfrm_user_polexpire *upe;
2289 struct nlmsghdr *nlh;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002290 int hard = c->data.hard;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002291
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002292 nlh = nlmsg_put(skb, c->pid, 0, XFRM_MSG_POLEXPIRE, sizeof(*upe), 0);
2293 if (nlh == NULL)
2294 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002295
Thomas Graf7b67c852007-08-22 13:53:52 -07002296 upe = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297 copy_to_user_policy(xp, &upe->pol, dir);
2298 if (copy_to_user_tmpl(xp, skb) < 0)
2299 goto nlmsg_failure;
Trent Jaegerdf718372005-12-13 23:12:27 -08002300 if (copy_to_user_sec_ctx(xp, skb))
2301 goto nlmsg_failure;
Jamal Hadi Salim1459bb32006-11-20 16:51:22 -08002302 if (copy_to_user_policy_type(xp->type, skb) < 0)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07002303 goto nlmsg_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304 upe->hard = !!hard;
2305
Thomas Graf98250692007-08-22 12:47:26 -07002306 return nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307
2308nlmsg_failure:
Thomas Graf98250692007-08-22 12:47:26 -07002309 nlmsg_cancel(skb, nlh);
2310 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002311}
2312
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002313static int xfrm_exp_policy_notify(struct xfrm_policy *xp, int dir, struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314{
2315 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002316
Thomas Graf7deb2262007-08-22 13:57:39 -07002317 skb = nlmsg_new(xfrm_polexpire_msgsize(xp), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002318 if (skb == NULL)
2319 return -ENOMEM;
2320
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002321 if (build_polexpire(skb, xp, dir, c) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322 BUG();
2323
Thomas Graf082a1ad2007-08-22 13:54:36 -07002324 return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_EXPIRE, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002325}
2326
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002327static int xfrm_notify_policy(struct xfrm_policy *xp, int dir, struct km_event *c)
2328{
2329 struct xfrm_userpolicy_info *p;
Herbert Xu0603eac2005-06-18 22:54:36 -07002330 struct xfrm_userpolicy_id *id;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002331 struct nlmsghdr *nlh;
2332 struct sk_buff *skb;
Thomas Graf7deb2262007-08-22 13:57:39 -07002333 int len = nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr);
Herbert Xu0603eac2005-06-18 22:54:36 -07002334 int headlen;
2335
2336 headlen = sizeof(*p);
2337 if (c->event == XFRM_MSG_DELPOLICY) {
Thomas Graf7deb2262007-08-22 13:57:39 -07002338 len += nla_total_size(headlen);
Herbert Xu0603eac2005-06-18 22:54:36 -07002339 headlen = sizeof(*id);
2340 }
Thomas Grafcfbfd452007-08-22 13:57:04 -07002341 len += userpolicy_type_attrsize();
Thomas Graf7deb2262007-08-22 13:57:39 -07002342 len += NLMSG_ALIGN(headlen);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002343
Thomas Graf7deb2262007-08-22 13:57:39 -07002344 skb = nlmsg_new(len, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002345 if (skb == NULL)
2346 return -ENOMEM;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002347
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002348 nlh = nlmsg_put(skb, c->pid, c->seq, c->event, headlen, 0);
2349 if (nlh == NULL)
2350 goto nlmsg_failure;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002351
Thomas Graf7b67c852007-08-22 13:53:52 -07002352 p = nlmsg_data(nlh);
Herbert Xu0603eac2005-06-18 22:54:36 -07002353 if (c->event == XFRM_MSG_DELPOLICY) {
Thomas Grafc0144be2007-08-22 13:55:43 -07002354 struct nlattr *attr;
2355
Thomas Graf7b67c852007-08-22 13:53:52 -07002356 id = nlmsg_data(nlh);
Herbert Xu0603eac2005-06-18 22:54:36 -07002357 memset(id, 0, sizeof(*id));
2358 id->dir = dir;
2359 if (c->data.byid)
2360 id->index = xp->index;
2361 else
2362 memcpy(&id->sel, &xp->selector, sizeof(id->sel));
2363
Thomas Grafc0144be2007-08-22 13:55:43 -07002364 attr = nla_reserve(skb, XFRMA_POLICY, sizeof(*p));
2365 if (attr == NULL)
2366 goto nlmsg_failure;
2367
2368 p = nla_data(attr);
Herbert Xu0603eac2005-06-18 22:54:36 -07002369 }
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002370
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002371 copy_to_user_policy(xp, p, dir);
2372 if (copy_to_user_tmpl(xp, skb) < 0)
2373 goto nlmsg_failure;
Jamal Hadi Salim1459bb32006-11-20 16:51:22 -08002374 if (copy_to_user_policy_type(xp->type, skb) < 0)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07002375 goto nlmsg_failure;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002376
Thomas Graf98250692007-08-22 12:47:26 -07002377 nlmsg_end(skb, nlh);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002378
Thomas Graf082a1ad2007-08-22 13:54:36 -07002379 return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_POLICY, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002380
2381nlmsg_failure:
2382 kfree_skb(skb);
2383 return -1;
2384}
2385
2386static int xfrm_notify_policy_flush(struct km_event *c)
2387{
2388 struct nlmsghdr *nlh;
2389 struct sk_buff *skb;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002390
Thomas Graf7deb2262007-08-22 13:57:39 -07002391 skb = nlmsg_new(userpolicy_type_attrsize(), GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002392 if (skb == NULL)
2393 return -ENOMEM;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002394
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002395 nlh = nlmsg_put(skb, c->pid, c->seq, XFRM_MSG_FLUSHPOLICY, 0, 0);
2396 if (nlh == NULL)
2397 goto nlmsg_failure;
Jamal Hadi Salim0c51f532006-11-27 12:58:20 -08002398 if (copy_to_user_policy_type(c->data.type, skb) < 0)
2399 goto nlmsg_failure;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002400
Thomas Graf98250692007-08-22 12:47:26 -07002401 nlmsg_end(skb, nlh);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002402
Thomas Graf082a1ad2007-08-22 13:54:36 -07002403 return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_POLICY, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002404
2405nlmsg_failure:
2406 kfree_skb(skb);
2407 return -1;
2408}
2409
2410static int xfrm_send_policy_notify(struct xfrm_policy *xp, int dir, struct km_event *c)
2411{
2412
2413 switch (c->event) {
Herbert Xuf60f6b82005-06-18 22:44:37 -07002414 case XFRM_MSG_NEWPOLICY:
2415 case XFRM_MSG_UPDPOLICY:
2416 case XFRM_MSG_DELPOLICY:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002417 return xfrm_notify_policy(xp, dir, c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07002418 case XFRM_MSG_FLUSHPOLICY:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002419 return xfrm_notify_policy_flush(c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07002420 case XFRM_MSG_POLEXPIRE:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002421 return xfrm_exp_policy_notify(xp, dir, c);
2422 default:
2423 printk("xfrm_user: Unknown Policy event %d\n", c->event);
2424 }
2425
2426 return 0;
2427
2428}
2429
Thomas Graf7deb2262007-08-22 13:57:39 -07002430static inline size_t xfrm_report_msgsize(void)
2431{
2432 return NLMSG_ALIGN(sizeof(struct xfrm_user_report));
2433}
2434
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002435static int build_report(struct sk_buff *skb, u8 proto,
2436 struct xfrm_selector *sel, xfrm_address_t *addr)
2437{
2438 struct xfrm_user_report *ur;
2439 struct nlmsghdr *nlh;
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002440
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002441 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_REPORT, sizeof(*ur), 0);
2442 if (nlh == NULL)
2443 return -EMSGSIZE;
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002444
Thomas Graf7b67c852007-08-22 13:53:52 -07002445 ur = nlmsg_data(nlh);
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002446 ur->proto = proto;
2447 memcpy(&ur->sel, sel, sizeof(ur->sel));
2448
2449 if (addr)
Thomas Grafc0144be2007-08-22 13:55:43 -07002450 NLA_PUT(skb, XFRMA_COADDR, sizeof(*addr), addr);
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002451
Thomas Graf98250692007-08-22 12:47:26 -07002452 return nlmsg_end(skb, nlh);
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002453
Thomas Grafc0144be2007-08-22 13:55:43 -07002454nla_put_failure:
Thomas Graf98250692007-08-22 12:47:26 -07002455 nlmsg_cancel(skb, nlh);
2456 return -EMSGSIZE;
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002457}
2458
2459static int xfrm_send_report(u8 proto, struct xfrm_selector *sel,
2460 xfrm_address_t *addr)
2461{
2462 struct sk_buff *skb;
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002463
Thomas Graf7deb2262007-08-22 13:57:39 -07002464 skb = nlmsg_new(xfrm_report_msgsize(), GFP_ATOMIC);
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002465 if (skb == NULL)
2466 return -ENOMEM;
2467
2468 if (build_report(skb, proto, sel, addr) < 0)
2469 BUG();
2470
Thomas Graf082a1ad2007-08-22 13:54:36 -07002471 return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_REPORT, GFP_ATOMIC);
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002472}
2473
Linus Torvalds1da177e2005-04-16 15:20:36 -07002474static struct xfrm_mgr netlink_mgr = {
2475 .id = "netlink",
2476 .notify = xfrm_send_state_notify,
2477 .acquire = xfrm_send_acquire,
2478 .compile_policy = xfrm_compile_policy,
2479 .notify_policy = xfrm_send_policy_notify,
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002480 .report = xfrm_send_report,
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002481 .migrate = xfrm_send_migrate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002482};
2483
2484static int __init xfrm_user_init(void)
2485{
Patrick McHardybe336902006-03-20 22:40:54 -08002486 struct sock *nlsk;
2487
Masahide NAKAMURA654b32c2006-08-23 19:12:56 -07002488 printk(KERN_INFO "Initializing XFRM netlink socket\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002489
Eric W. Biedermanb4b51022007-09-12 13:05:38 +02002490 nlsk = netlink_kernel_create(&init_net, NETLINK_XFRM, XFRMNLGRP_MAX,
Patrick McHardyaf65bdf2007-04-20 14:14:21 -07002491 xfrm_netlink_rcv, NULL, THIS_MODULE);
Patrick McHardybe336902006-03-20 22:40:54 -08002492 if (nlsk == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002493 return -ENOMEM;
Patrick McHardybe336902006-03-20 22:40:54 -08002494 rcu_assign_pointer(xfrm_nl, nlsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002495
2496 xfrm_register_km(&netlink_mgr);
2497
2498 return 0;
2499}
2500
2501static void __exit xfrm_user_exit(void)
2502{
Patrick McHardybe336902006-03-20 22:40:54 -08002503 struct sock *nlsk = xfrm_nl;
2504
Linus Torvalds1da177e2005-04-16 15:20:36 -07002505 xfrm_unregister_km(&netlink_mgr);
Patrick McHardybe336902006-03-20 22:40:54 -08002506 rcu_assign_pointer(xfrm_nl, NULL);
2507 synchronize_rcu();
Denis V. Lunevb7c6ba62008-01-28 14:41:19 -08002508 netlink_kernel_release(nlsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002509}
2510
2511module_init(xfrm_user_init);
2512module_exit(xfrm_user_exit);
2513MODULE_LICENSE("GPL");
Harald Welte4fdb3bb2005-08-09 19:40:55 -07002514MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK, NETLINK_XFRM);
Jamal Hadi Salimf8cd5482006-03-20 19:15:11 -08002515