blob: 04c41504f84c44b8b84d8be9b6cc602d568d3ce1 [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
Steffen Klassertccf9b3b2008-07-10 16:55:37 -0700280 if (!x->sel.family && !(p->flags & XFRM_STATE_AF_UNSPEC))
Herbert Xu196b0032007-07-31 02:04:32 -0700281 x->sel.family = p->family;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282}
283
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800284/*
285 * someday when pfkey also has support, we could have the code
286 * somehow made shareable and move it to xfrm_state.c - JHS
287 *
288*/
Thomas Graf5424f322007-08-22 14:01:33 -0700289static void xfrm_update_ae_params(struct xfrm_state *x, struct nlattr **attrs)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800290{
Thomas Graf5424f322007-08-22 14:01:33 -0700291 struct nlattr *rp = attrs[XFRMA_REPLAY_VAL];
292 struct nlattr *lt = attrs[XFRMA_LTIME_VAL];
293 struct nlattr *et = attrs[XFRMA_ETIMER_THRESH];
294 struct nlattr *rt = attrs[XFRMA_REPLAY_THRESH];
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800295
296 if (rp) {
297 struct xfrm_replay_state *replay;
Thomas Graf5424f322007-08-22 14:01:33 -0700298 replay = nla_data(rp);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800299 memcpy(&x->replay, replay, sizeof(*replay));
300 memcpy(&x->preplay, replay, sizeof(*replay));
301 }
302
303 if (lt) {
304 struct xfrm_lifetime_cur *ltime;
Thomas Graf5424f322007-08-22 14:01:33 -0700305 ltime = nla_data(lt);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800306 x->curlft.bytes = ltime->bytes;
307 x->curlft.packets = ltime->packets;
308 x->curlft.add_time = ltime->add_time;
309 x->curlft.use_time = ltime->use_time;
310 }
311
Thomas Grafcf5cb792007-08-22 13:59:04 -0700312 if (et)
Thomas Graf5424f322007-08-22 14:01:33 -0700313 x->replay_maxage = nla_get_u32(et);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800314
Thomas Grafcf5cb792007-08-22 13:59:04 -0700315 if (rt)
Thomas Graf5424f322007-08-22 14:01:33 -0700316 x->replay_maxdiff = nla_get_u32(rt);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800317}
318
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319static struct xfrm_state *xfrm_state_construct(struct xfrm_usersa_info *p,
Thomas Graf5424f322007-08-22 14:01:33 -0700320 struct nlattr **attrs,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 int *errp)
322{
323 struct xfrm_state *x = xfrm_state_alloc();
324 int err = -ENOMEM;
325
326 if (!x)
327 goto error_no_put;
328
329 copy_from_user_state(x, p);
330
Herbert Xu1a6509d2008-01-28 19:37:29 -0800331 if ((err = attach_aead(&x->aead, &x->props.ealgo,
332 attrs[XFRMA_ALG_AEAD])))
333 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 if ((err = attach_one_algo(&x->aalg, &x->props.aalgo,
335 xfrm_aalg_get_byname,
Thomas Graf35a7aa02007-08-22 14:00:40 -0700336 attrs[XFRMA_ALG_AUTH])))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 goto error;
338 if ((err = attach_one_algo(&x->ealg, &x->props.ealgo,
339 xfrm_ealg_get_byname,
Thomas Graf35a7aa02007-08-22 14:00:40 -0700340 attrs[XFRMA_ALG_CRYPT])))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 goto error;
342 if ((err = attach_one_algo(&x->calg, &x->props.calgo,
343 xfrm_calg_get_byname,
Thomas Graf35a7aa02007-08-22 14:00:40 -0700344 attrs[XFRMA_ALG_COMP])))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 goto error;
Thomas Graffd211502007-09-06 03:28:08 -0700346
347 if (attrs[XFRMA_ENCAP]) {
348 x->encap = kmemdup(nla_data(attrs[XFRMA_ENCAP]),
349 sizeof(*x->encap), GFP_KERNEL);
350 if (x->encap == NULL)
351 goto error;
352 }
353
354 if (attrs[XFRMA_COADDR]) {
355 x->coaddr = kmemdup(nla_data(attrs[XFRMA_COADDR]),
356 sizeof(*x->coaddr), GFP_KERNEL);
357 if (x->coaddr == NULL)
358 goto error;
359 }
360
Herbert Xu72cb6962005-06-20 13:18:08 -0700361 err = xfrm_init_state(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 if (err)
363 goto error;
364
Thomas Graffd211502007-09-06 03:28:08 -0700365 if (attrs[XFRMA_SEC_CTX] &&
366 security_xfrm_state_alloc(x, nla_data(attrs[XFRMA_SEC_CTX])))
Trent Jaegerdf718372005-12-13 23:12:27 -0800367 goto error;
368
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 x->km.seq = p->seq;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800370 x->replay_maxdiff = sysctl_xfrm_aevent_rseqth;
371 /* sysctl_xfrm_aevent_etime is in 100ms units */
372 x->replay_maxage = (sysctl_xfrm_aevent_etime*HZ)/XFRM_AE_ETH_M;
373 x->preplay.bitmap = 0;
374 x->preplay.seq = x->replay.seq+x->replay_maxdiff;
375 x->preplay.oseq = x->replay.oseq +x->replay_maxdiff;
376
377 /* override default values from above */
378
Thomas Graf5424f322007-08-22 14:01:33 -0700379 xfrm_update_ae_params(x, attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380
381 return x;
382
383error:
384 x->km.state = XFRM_STATE_DEAD;
385 xfrm_state_put(x);
386error_no_put:
387 *errp = err;
388 return NULL;
389}
390
Christoph Hellwig22e70052007-01-02 15:22:30 -0800391static int xfrm_add_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -0700392 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393{
Thomas Graf7b67c852007-08-22 13:53:52 -0700394 struct xfrm_usersa_info *p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 struct xfrm_state *x;
396 int err;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700397 struct km_event c;
Eric Paris25323862008-04-18 10:09:25 -0400398 uid_t loginuid = NETLINK_CB(skb).loginuid;
399 u32 sessionid = NETLINK_CB(skb).sessionid;
400 u32 sid = NETLINK_CB(skb).sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401
Thomas Graf35a7aa02007-08-22 14:00:40 -0700402 err = verify_newsa_info(p, attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 if (err)
404 return err;
405
Thomas Graf35a7aa02007-08-22 14:00:40 -0700406 x = xfrm_state_construct(p, attrs, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 if (!x)
408 return err;
409
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700410 xfrm_state_hold(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 if (nlh->nlmsg_type == XFRM_MSG_NEWSA)
412 err = xfrm_state_add(x);
413 else
414 err = xfrm_state_update(x);
415
Eric Paris25323862008-04-18 10:09:25 -0400416 xfrm_audit_state_add(x, err ? 0 : 1, loginuid, sessionid, sid);
Joy Latten161a09e2006-11-27 13:11:54 -0600417
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 if (err < 0) {
419 x->km.state = XFRM_STATE_DEAD;
Herbert Xu21380b82006-02-22 14:47:13 -0800420 __xfrm_state_put(x);
Patrick McHardy7d6dfe12005-06-18 22:45:31 -0700421 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 }
423
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700424 c.seq = nlh->nlmsg_seq;
425 c.pid = nlh->nlmsg_pid;
Herbert Xuf60f6b82005-06-18 22:44:37 -0700426 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700427
428 km_state_notify(x, &c);
Patrick McHardy7d6dfe12005-06-18 22:45:31 -0700429out:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700430 xfrm_state_put(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 return err;
432}
433
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700434static struct xfrm_state *xfrm_user_state_lookup(struct xfrm_usersa_id *p,
Thomas Graf5424f322007-08-22 14:01:33 -0700435 struct nlattr **attrs,
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700436 int *errp)
437{
438 struct xfrm_state *x = NULL;
439 int err;
440
441 if (xfrm_id_proto_match(p->proto, IPSEC_PROTO_ANY)) {
442 err = -ESRCH;
443 x = xfrm_state_lookup(&p->daddr, p->spi, p->proto, p->family);
444 } else {
445 xfrm_address_t *saddr = NULL;
446
Thomas Graf35a7aa02007-08-22 14:00:40 -0700447 verify_one_addr(attrs, XFRMA_SRCADDR, &saddr);
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700448 if (!saddr) {
449 err = -EINVAL;
450 goto out;
451 }
452
Masahide NAKAMURA9abbffe2006-11-24 20:34:51 -0800453 err = -ESRCH;
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700454 x = xfrm_state_lookup_byaddr(&p->daddr, saddr, p->proto,
455 p->family);
456 }
457
458 out:
459 if (!x && errp)
460 *errp = err;
461 return x;
462}
463
Christoph Hellwig22e70052007-01-02 15:22:30 -0800464static int xfrm_del_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -0700465 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466{
467 struct xfrm_state *x;
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700468 int err = -ESRCH;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700469 struct km_event c;
Thomas Graf7b67c852007-08-22 13:53:52 -0700470 struct xfrm_usersa_id *p = nlmsg_data(nlh);
Eric Paris25323862008-04-18 10:09:25 -0400471 uid_t loginuid = NETLINK_CB(skb).loginuid;
472 u32 sessionid = NETLINK_CB(skb).sessionid;
473 u32 sid = NETLINK_CB(skb).sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474
Thomas Graf35a7aa02007-08-22 14:00:40 -0700475 x = xfrm_user_state_lookup(p, attrs, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 if (x == NULL)
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700477 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478
David S. Miller6f68dc32006-06-08 23:58:52 -0700479 if ((err = security_xfrm_state_delete(x)) != 0)
Catherine Zhangc8c05a82006-06-08 23:39:49 -0700480 goto out;
481
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 if (xfrm_state_kern(x)) {
Catherine Zhangc8c05a82006-06-08 23:39:49 -0700483 err = -EPERM;
484 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 }
486
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700487 err = xfrm_state_delete(x);
Joy Latten161a09e2006-11-27 13:11:54 -0600488
Catherine Zhangc8c05a82006-06-08 23:39:49 -0700489 if (err < 0)
490 goto out;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700491
492 c.seq = nlh->nlmsg_seq;
493 c.pid = nlh->nlmsg_pid;
Herbert Xuf60f6b82005-06-18 22:44:37 -0700494 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700495 km_state_notify(x, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496
Catherine Zhangc8c05a82006-06-08 23:39:49 -0700497out:
Eric Paris25323862008-04-18 10:09:25 -0400498 xfrm_audit_state_delete(x, err ? 0 : 1, loginuid, sessionid, sid);
Catherine Zhangc8c05a82006-06-08 23:39:49 -0700499 xfrm_state_put(x);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700500 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501}
502
503static void copy_to_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p)
504{
505 memcpy(&p->id, &x->id, sizeof(p->id));
506 memcpy(&p->sel, &x->sel, sizeof(p->sel));
507 memcpy(&p->lft, &x->lft, sizeof(p->lft));
508 memcpy(&p->curlft, &x->curlft, sizeof(p->curlft));
509 memcpy(&p->stats, &x->stats, sizeof(p->stats));
David S. Miller54489c142006-10-27 15:29:47 -0700510 memcpy(&p->saddr, &x->props.saddr, sizeof(p->saddr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 p->mode = x->props.mode;
512 p->replay_window = x->props.replay_window;
513 p->reqid = x->props.reqid;
514 p->family = x->props.family;
515 p->flags = x->props.flags;
516 p->seq = x->km.seq;
517}
518
519struct xfrm_dump_info {
520 struct sk_buff *in_skb;
521 struct sk_buff *out_skb;
522 u32 nlmsg_seq;
523 u16 nlmsg_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524};
525
Thomas Grafc0144be2007-08-22 13:55:43 -0700526static int copy_sec_ctx(struct xfrm_sec_ctx *s, struct sk_buff *skb)
527{
Thomas Grafc0144be2007-08-22 13:55:43 -0700528 struct xfrm_user_sec_ctx *uctx;
529 struct nlattr *attr;
Herbert Xu68325d32007-10-09 13:30:57 -0700530 int ctx_size = sizeof(*uctx) + s->ctx_len;
Thomas Grafc0144be2007-08-22 13:55:43 -0700531
532 attr = nla_reserve(skb, XFRMA_SEC_CTX, ctx_size);
533 if (attr == NULL)
534 return -EMSGSIZE;
535
536 uctx = nla_data(attr);
537 uctx->exttype = XFRMA_SEC_CTX;
538 uctx->len = ctx_size;
539 uctx->ctx_doi = s->ctx_doi;
540 uctx->ctx_alg = s->ctx_alg;
541 uctx->ctx_len = s->ctx_len;
542 memcpy(uctx + 1, s->ctx_str, s->ctx_len);
543
544 return 0;
545}
546
Herbert Xu68325d32007-10-09 13:30:57 -0700547/* Don't change this without updating xfrm_sa_len! */
548static int copy_to_user_state_extra(struct xfrm_state *x,
549 struct xfrm_usersa_info *p,
550 struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 copy_to_user_state(x, p);
553
Herbert Xu050f0092007-10-09 13:31:47 -0700554 if (x->coaddr)
555 NLA_PUT(skb, XFRMA_COADDR, sizeof(*x->coaddr), x->coaddr);
556
557 if (x->lastused)
558 NLA_PUT_U64(skb, XFRMA_LASTUSED, x->lastused);
Herbert Xu050f0092007-10-09 13:31:47 -0700559
Herbert Xu1a6509d2008-01-28 19:37:29 -0800560 if (x->aead)
561 NLA_PUT(skb, XFRMA_ALG_AEAD, aead_len(x->aead), x->aead);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 if (x->aalg)
Eric Dumazet0f99be02008-01-08 23:39:06 -0800563 NLA_PUT(skb, XFRMA_ALG_AUTH, xfrm_alg_len(x->aalg), x->aalg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 if (x->ealg)
Eric Dumazet0f99be02008-01-08 23:39:06 -0800565 NLA_PUT(skb, XFRMA_ALG_CRYPT, xfrm_alg_len(x->ealg), x->ealg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 if (x->calg)
Thomas Grafc0144be2007-08-22 13:55:43 -0700567 NLA_PUT(skb, XFRMA_ALG_COMP, sizeof(*(x->calg)), x->calg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568
569 if (x->encap)
Thomas Grafc0144be2007-08-22 13:55:43 -0700570 NLA_PUT(skb, XFRMA_ENCAP, sizeof(*x->encap), x->encap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571
Thomas Grafc0144be2007-08-22 13:55:43 -0700572 if (x->security && copy_sec_ctx(x->security, skb) < 0)
573 goto nla_put_failure;
Noriaki TAKAMIYA060f02a2006-08-23 18:18:55 -0700574
Herbert Xu68325d32007-10-09 13:30:57 -0700575 return 0;
576
577nla_put_failure:
578 return -EMSGSIZE;
579}
580
581static int dump_one_state(struct xfrm_state *x, int count, void *ptr)
582{
583 struct xfrm_dump_info *sp = ptr;
584 struct sk_buff *in_skb = sp->in_skb;
585 struct sk_buff *skb = sp->out_skb;
586 struct xfrm_usersa_info *p;
587 struct nlmsghdr *nlh;
588 int err;
589
Herbert Xu68325d32007-10-09 13:30:57 -0700590 nlh = nlmsg_put(skb, NETLINK_CB(in_skb).pid, sp->nlmsg_seq,
591 XFRM_MSG_NEWSA, sizeof(*p), sp->nlmsg_flags);
592 if (nlh == NULL)
593 return -EMSGSIZE;
594
595 p = nlmsg_data(nlh);
596
597 err = copy_to_user_state_extra(x, p, skb);
598 if (err)
599 goto nla_put_failure;
600
Thomas Graf98250692007-08-22 12:47:26 -0700601 nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 return 0;
603
Thomas Grafc0144be2007-08-22 13:55:43 -0700604nla_put_failure:
Thomas Graf98250692007-08-22 12:47:26 -0700605 nlmsg_cancel(skb, nlh);
Herbert Xu68325d32007-10-09 13:30:57 -0700606 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607}
608
Timo Teras4c563f72008-02-28 21:31:08 -0800609static int xfrm_dump_sa_done(struct netlink_callback *cb)
610{
611 struct xfrm_state_walk *walk = (struct xfrm_state_walk *) &cb->args[1];
612 xfrm_state_walk_done(walk);
613 return 0;
614}
615
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616static int xfrm_dump_sa(struct sk_buff *skb, struct netlink_callback *cb)
617{
Timo Teras4c563f72008-02-28 21:31:08 -0800618 struct xfrm_state_walk *walk = (struct xfrm_state_walk *) &cb->args[1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 struct xfrm_dump_info info;
620
Timo Teras4c563f72008-02-28 21:31:08 -0800621 BUILD_BUG_ON(sizeof(struct xfrm_state_walk) >
622 sizeof(cb->args) - sizeof(cb->args[0]));
623
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 info.in_skb = cb->skb;
625 info.out_skb = skb;
626 info.nlmsg_seq = cb->nlh->nlmsg_seq;
627 info.nlmsg_flags = NLM_F_MULTI;
Timo Teras4c563f72008-02-28 21:31:08 -0800628
629 if (!cb->args[0]) {
630 cb->args[0] = 1;
631 xfrm_state_walk_init(walk, 0);
632 }
633
634 (void) xfrm_state_walk(walk, dump_one_state, &info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635
636 return skb->len;
637}
638
639static struct sk_buff *xfrm_state_netlink(struct sk_buff *in_skb,
640 struct xfrm_state *x, u32 seq)
641{
642 struct xfrm_dump_info info;
643 struct sk_buff *skb;
644
Thomas Graf7deb2262007-08-22 13:57:39 -0700645 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 if (!skb)
647 return ERR_PTR(-ENOMEM);
648
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 info.in_skb = in_skb;
650 info.out_skb = skb;
651 info.nlmsg_seq = seq;
652 info.nlmsg_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653
654 if (dump_one_state(x, 0, &info)) {
655 kfree_skb(skb);
656 return NULL;
657 }
658
659 return skb;
660}
661
Thomas Graf7deb2262007-08-22 13:57:39 -0700662static inline size_t xfrm_spdinfo_msgsize(void)
663{
664 return NLMSG_ALIGN(4)
665 + nla_total_size(sizeof(struct xfrmu_spdinfo))
666 + nla_total_size(sizeof(struct xfrmu_spdhinfo));
667}
668
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700669static int build_spdinfo(struct sk_buff *skb, u32 pid, u32 seq, u32 flags)
670{
Jamal Hadi Salim5a6d3412007-05-04 12:55:39 -0700671 struct xfrmk_spdinfo si;
672 struct xfrmu_spdinfo spc;
673 struct xfrmu_spdhinfo sph;
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700674 struct nlmsghdr *nlh;
675 u32 *f;
676
677 nlh = nlmsg_put(skb, pid, seq, XFRM_MSG_NEWSPDINFO, sizeof(u32), 0);
678 if (nlh == NULL) /* shouldnt really happen ... */
679 return -EMSGSIZE;
680
681 f = nlmsg_data(nlh);
682 *f = flags;
683 xfrm_spd_getinfo(&si);
Jamal Hadi Salim5a6d3412007-05-04 12:55:39 -0700684 spc.incnt = si.incnt;
685 spc.outcnt = si.outcnt;
686 spc.fwdcnt = si.fwdcnt;
687 spc.inscnt = si.inscnt;
688 spc.outscnt = si.outscnt;
689 spc.fwdscnt = si.fwdscnt;
690 sph.spdhcnt = si.spdhcnt;
691 sph.spdhmcnt = si.spdhmcnt;
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700692
Jamal Hadi Salim5a6d3412007-05-04 12:55:39 -0700693 NLA_PUT(skb, XFRMA_SPD_INFO, sizeof(spc), &spc);
694 NLA_PUT(skb, XFRMA_SPD_HINFO, sizeof(sph), &sph);
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700695
696 return nlmsg_end(skb, nlh);
697
698nla_put_failure:
699 nlmsg_cancel(skb, nlh);
700 return -EMSGSIZE;
701}
702
703static int xfrm_get_spdinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -0700704 struct nlattr **attrs)
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700705{
706 struct sk_buff *r_skb;
Thomas Graf7b67c852007-08-22 13:53:52 -0700707 u32 *flags = nlmsg_data(nlh);
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700708 u32 spid = NETLINK_CB(skb).pid;
709 u32 seq = nlh->nlmsg_seq;
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700710
Thomas Graf7deb2262007-08-22 13:57:39 -0700711 r_skb = nlmsg_new(xfrm_spdinfo_msgsize(), GFP_ATOMIC);
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700712 if (r_skb == NULL)
713 return -ENOMEM;
714
715 if (build_spdinfo(r_skb, spid, seq, *flags) < 0)
716 BUG();
717
718 return nlmsg_unicast(xfrm_nl, r_skb, spid);
719}
720
Thomas Graf7deb2262007-08-22 13:57:39 -0700721static inline size_t xfrm_sadinfo_msgsize(void)
722{
723 return NLMSG_ALIGN(4)
724 + nla_total_size(sizeof(struct xfrmu_sadhinfo))
725 + nla_total_size(4); /* XFRMA_SAD_CNT */
726}
727
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700728static int build_sadinfo(struct sk_buff *skb, u32 pid, u32 seq, u32 flags)
729{
Jamal Hadi Salimaf11e312007-05-04 12:55:13 -0700730 struct xfrmk_sadinfo si;
731 struct xfrmu_sadhinfo sh;
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700732 struct nlmsghdr *nlh;
733 u32 *f;
734
735 nlh = nlmsg_put(skb, pid, seq, XFRM_MSG_NEWSADINFO, sizeof(u32), 0);
736 if (nlh == NULL) /* shouldnt really happen ... */
737 return -EMSGSIZE;
738
739 f = nlmsg_data(nlh);
740 *f = flags;
741 xfrm_sad_getinfo(&si);
742
Jamal Hadi Salimaf11e312007-05-04 12:55:13 -0700743 sh.sadhmcnt = si.sadhmcnt;
744 sh.sadhcnt = si.sadhcnt;
745
746 NLA_PUT_U32(skb, XFRMA_SAD_CNT, si.sadcnt);
747 NLA_PUT(skb, XFRMA_SAD_HINFO, sizeof(sh), &sh);
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700748
749 return nlmsg_end(skb, nlh);
750
751nla_put_failure:
752 nlmsg_cancel(skb, nlh);
753 return -EMSGSIZE;
754}
755
756static int xfrm_get_sadinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -0700757 struct nlattr **attrs)
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700758{
759 struct sk_buff *r_skb;
Thomas Graf7b67c852007-08-22 13:53:52 -0700760 u32 *flags = nlmsg_data(nlh);
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700761 u32 spid = NETLINK_CB(skb).pid;
762 u32 seq = nlh->nlmsg_seq;
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700763
Thomas Graf7deb2262007-08-22 13:57:39 -0700764 r_skb = nlmsg_new(xfrm_sadinfo_msgsize(), GFP_ATOMIC);
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700765 if (r_skb == NULL)
766 return -ENOMEM;
767
768 if (build_sadinfo(r_skb, spid, seq, *flags) < 0)
769 BUG();
770
771 return nlmsg_unicast(xfrm_nl, r_skb, spid);
772}
773
Christoph Hellwig22e70052007-01-02 15:22:30 -0800774static int xfrm_get_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -0700775 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776{
Thomas Graf7b67c852007-08-22 13:53:52 -0700777 struct xfrm_usersa_id *p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 struct xfrm_state *x;
779 struct sk_buff *resp_skb;
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700780 int err = -ESRCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781
Thomas Graf35a7aa02007-08-22 14:00:40 -0700782 x = xfrm_user_state_lookup(p, attrs, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 if (x == NULL)
784 goto out_noput;
785
786 resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
787 if (IS_ERR(resp_skb)) {
788 err = PTR_ERR(resp_skb);
789 } else {
Thomas Graf082a1ad2007-08-22 13:54:36 -0700790 err = nlmsg_unicast(xfrm_nl, resp_skb, NETLINK_CB(skb).pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 }
792 xfrm_state_put(x);
793out_noput:
794 return err;
795}
796
797static int verify_userspi_info(struct xfrm_userspi_info *p)
798{
799 switch (p->info.id.proto) {
800 case IPPROTO_AH:
801 case IPPROTO_ESP:
802 break;
803
804 case IPPROTO_COMP:
805 /* IPCOMP spi is 16-bits. */
806 if (p->max >= 0x10000)
807 return -EINVAL;
808 break;
809
810 default:
811 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700812 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813
814 if (p->min > p->max)
815 return -EINVAL;
816
817 return 0;
818}
819
Christoph Hellwig22e70052007-01-02 15:22:30 -0800820static int xfrm_alloc_userspi(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -0700821 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822{
823 struct xfrm_state *x;
824 struct xfrm_userspi_info *p;
825 struct sk_buff *resp_skb;
826 xfrm_address_t *daddr;
827 int family;
828 int err;
829
Thomas Graf7b67c852007-08-22 13:53:52 -0700830 p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 err = verify_userspi_info(p);
832 if (err)
833 goto out_noput;
834
835 family = p->info.family;
836 daddr = &p->info.id.daddr;
837
838 x = NULL;
839 if (p->info.seq) {
840 x = xfrm_find_acq_byseq(p->info.seq);
841 if (x && xfrm_addr_cmp(&x->id.daddr, daddr, family)) {
842 xfrm_state_put(x);
843 x = NULL;
844 }
845 }
846
847 if (!x)
848 x = xfrm_find_acq(p->info.mode, p->info.reqid,
849 p->info.id.proto, daddr,
850 &p->info.saddr, 1,
851 family);
852 err = -ENOENT;
853 if (x == NULL)
854 goto out_noput;
855
Herbert Xu658b2192007-10-09 13:29:52 -0700856 err = xfrm_alloc_spi(x, p->min, p->max);
857 if (err)
858 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859
Herbert Xu658b2192007-10-09 13:29:52 -0700860 resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 if (IS_ERR(resp_skb)) {
862 err = PTR_ERR(resp_skb);
863 goto out;
864 }
865
Thomas Graf082a1ad2007-08-22 13:54:36 -0700866 err = nlmsg_unicast(xfrm_nl, resp_skb, NETLINK_CB(skb).pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867
868out:
869 xfrm_state_put(x);
870out_noput:
871 return err;
872}
873
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -0800874static int verify_policy_dir(u8 dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875{
876 switch (dir) {
877 case XFRM_POLICY_IN:
878 case XFRM_POLICY_OUT:
879 case XFRM_POLICY_FWD:
880 break;
881
882 default:
883 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700884 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885
886 return 0;
887}
888
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -0800889static int verify_policy_type(u8 type)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -0700890{
891 switch (type) {
892 case XFRM_POLICY_TYPE_MAIN:
893#ifdef CONFIG_XFRM_SUB_POLICY
894 case XFRM_POLICY_TYPE_SUB:
895#endif
896 break;
897
898 default:
899 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700900 }
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -0700901
902 return 0;
903}
904
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905static int verify_newpolicy_info(struct xfrm_userpolicy_info *p)
906{
907 switch (p->share) {
908 case XFRM_SHARE_ANY:
909 case XFRM_SHARE_SESSION:
910 case XFRM_SHARE_USER:
911 case XFRM_SHARE_UNIQUE:
912 break;
913
914 default:
915 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700916 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917
918 switch (p->action) {
919 case XFRM_POLICY_ALLOW:
920 case XFRM_POLICY_BLOCK:
921 break;
922
923 default:
924 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700925 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926
927 switch (p->sel.family) {
928 case AF_INET:
929 break;
930
931 case AF_INET6:
932#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
933 break;
934#else
935 return -EAFNOSUPPORT;
936#endif
937
938 default:
939 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700940 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941
942 return verify_policy_dir(p->dir);
943}
944
Thomas Graf5424f322007-08-22 14:01:33 -0700945static int copy_from_user_sec_ctx(struct xfrm_policy *pol, struct nlattr **attrs)
Trent Jaegerdf718372005-12-13 23:12:27 -0800946{
Thomas Graf5424f322007-08-22 14:01:33 -0700947 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
Trent Jaegerdf718372005-12-13 23:12:27 -0800948 struct xfrm_user_sec_ctx *uctx;
949
950 if (!rt)
951 return 0;
952
Thomas Graf5424f322007-08-22 14:01:33 -0700953 uctx = nla_data(rt);
Paul Moore03e1ad72008-04-12 19:07:52 -0700954 return security_xfrm_policy_alloc(&pol->security, uctx);
Trent Jaegerdf718372005-12-13 23:12:27 -0800955}
956
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957static void copy_templates(struct xfrm_policy *xp, struct xfrm_user_tmpl *ut,
958 int nr)
959{
960 int i;
961
962 xp->xfrm_nr = nr;
963 for (i = 0; i < nr; i++, ut++) {
964 struct xfrm_tmpl *t = &xp->xfrm_vec[i];
965
966 memcpy(&t->id, &ut->id, sizeof(struct xfrm_id));
967 memcpy(&t->saddr, &ut->saddr,
968 sizeof(xfrm_address_t));
969 t->reqid = ut->reqid;
970 t->mode = ut->mode;
971 t->share = ut->share;
972 t->optional = ut->optional;
973 t->aalgos = ut->aalgos;
974 t->ealgos = ut->ealgos;
975 t->calgos = ut->calgos;
Herbert Xuc5d18e92008-04-22 00:46:42 -0700976 /* If all masks are ~0, then we allow all algorithms. */
977 t->allalgs = !~(t->aalgos & t->ealgos & t->calgos);
Miika Komu8511d012006-11-30 16:40:51 -0800978 t->encap_family = ut->family;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 }
980}
981
David S. Millerb4ad86bf2006-12-03 19:19:26 -0800982static int validate_tmpl(int nr, struct xfrm_user_tmpl *ut, u16 family)
983{
984 int i;
985
986 if (nr > XFRM_MAX_DEPTH)
987 return -EINVAL;
988
989 for (i = 0; i < nr; i++) {
990 /* We never validated the ut->family value, so many
991 * applications simply leave it at zero. The check was
992 * never made and ut->family was ignored because all
993 * templates could be assumed to have the same family as
994 * the policy itself. Now that we will have ipv4-in-ipv6
995 * and ipv6-in-ipv4 tunnels, this is no longer true.
996 */
997 if (!ut[i].family)
998 ut[i].family = family;
999
1000 switch (ut[i].family) {
1001 case AF_INET:
1002 break;
1003#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1004 case AF_INET6:
1005 break;
1006#endif
1007 default:
1008 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001009 }
David S. Millerb4ad86bf2006-12-03 19:19:26 -08001010 }
1011
1012 return 0;
1013}
1014
Thomas Graf5424f322007-08-22 14:01:33 -07001015static int copy_from_user_tmpl(struct xfrm_policy *pol, struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016{
Thomas Graf5424f322007-08-22 14:01:33 -07001017 struct nlattr *rt = attrs[XFRMA_TMPL];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018
1019 if (!rt) {
1020 pol->xfrm_nr = 0;
1021 } else {
Thomas Graf5424f322007-08-22 14:01:33 -07001022 struct xfrm_user_tmpl *utmpl = nla_data(rt);
1023 int nr = nla_len(rt) / sizeof(*utmpl);
David S. Millerb4ad86bf2006-12-03 19:19:26 -08001024 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025
David S. Millerb4ad86bf2006-12-03 19:19:26 -08001026 err = validate_tmpl(nr, utmpl, pol->family);
1027 if (err)
1028 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029
Thomas Graf5424f322007-08-22 14:01:33 -07001030 copy_templates(pol, utmpl, nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 }
1032 return 0;
1033}
1034
Thomas Graf5424f322007-08-22 14:01:33 -07001035static int copy_from_user_policy_type(u8 *tp, struct nlattr **attrs)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001036{
Thomas Graf5424f322007-08-22 14:01:33 -07001037 struct nlattr *rt = attrs[XFRMA_POLICY_TYPE];
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001038 struct xfrm_userpolicy_type *upt;
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001039 u8 type = XFRM_POLICY_TYPE_MAIN;
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001040 int err;
1041
1042 if (rt) {
Thomas Graf5424f322007-08-22 14:01:33 -07001043 upt = nla_data(rt);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001044 type = upt->type;
1045 }
1046
1047 err = verify_policy_type(type);
1048 if (err)
1049 return err;
1050
1051 *tp = type;
1052 return 0;
1053}
1054
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055static void copy_from_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p)
1056{
1057 xp->priority = p->priority;
1058 xp->index = p->index;
1059 memcpy(&xp->selector, &p->sel, sizeof(xp->selector));
1060 memcpy(&xp->lft, &p->lft, sizeof(xp->lft));
1061 xp->action = p->action;
1062 xp->flags = p->flags;
1063 xp->family = p->sel.family;
1064 /* XXX xp->share = p->share; */
1065}
1066
1067static void copy_to_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p, int dir)
1068{
1069 memcpy(&p->sel, &xp->selector, sizeof(p->sel));
1070 memcpy(&p->lft, &xp->lft, sizeof(p->lft));
1071 memcpy(&p->curlft, &xp->curlft, sizeof(p->curlft));
1072 p->priority = xp->priority;
1073 p->index = xp->index;
1074 p->sel.family = xp->family;
1075 p->dir = dir;
1076 p->action = xp->action;
1077 p->flags = xp->flags;
1078 p->share = XFRM_SHARE_ANY; /* XXX xp->share */
1079}
1080
Thomas Graf5424f322007-08-22 14:01:33 -07001081static struct xfrm_policy *xfrm_policy_construct(struct xfrm_userpolicy_info *p, struct nlattr **attrs, int *errp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082{
1083 struct xfrm_policy *xp = xfrm_policy_alloc(GFP_KERNEL);
1084 int err;
1085
1086 if (!xp) {
1087 *errp = -ENOMEM;
1088 return NULL;
1089 }
1090
1091 copy_from_user_policy(xp, p);
Trent Jaegerdf718372005-12-13 23:12:27 -08001092
Thomas Graf35a7aa02007-08-22 14:00:40 -07001093 err = copy_from_user_policy_type(&xp->type, attrs);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001094 if (err)
1095 goto error;
1096
Thomas Graf35a7aa02007-08-22 14:00:40 -07001097 if (!(err = copy_from_user_tmpl(xp, attrs)))
1098 err = copy_from_user_sec_ctx(xp, attrs);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001099 if (err)
1100 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101
1102 return xp;
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001103 error:
1104 *errp = err;
YOSHIFUJI Hideaki073a3712008-02-14 14:52:38 -08001105 xp->dead = 1;
WANG Cong64c31b32008-01-07 22:34:29 -08001106 xfrm_policy_destroy(xp);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001107 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108}
1109
Christoph Hellwig22e70052007-01-02 15:22:30 -08001110static int xfrm_add_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001111 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112{
Thomas Graf7b67c852007-08-22 13:53:52 -07001113 struct xfrm_userpolicy_info *p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 struct xfrm_policy *xp;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001115 struct km_event c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 int err;
1117 int excl;
Eric Paris25323862008-04-18 10:09:25 -04001118 uid_t loginuid = NETLINK_CB(skb).loginuid;
1119 u32 sessionid = NETLINK_CB(skb).sessionid;
1120 u32 sid = NETLINK_CB(skb).sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121
1122 err = verify_newpolicy_info(p);
1123 if (err)
1124 return err;
Thomas Graf35a7aa02007-08-22 14:00:40 -07001125 err = verify_sec_ctx_len(attrs);
Trent Jaegerdf718372005-12-13 23:12:27 -08001126 if (err)
1127 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128
Thomas Graf35a7aa02007-08-22 14:00:40 -07001129 xp = xfrm_policy_construct(p, attrs, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 if (!xp)
1131 return err;
1132
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001133 /* shouldnt excl be based on nlh flags??
1134 * Aha! this is anti-netlink really i.e more pfkey derived
1135 * in netlink excl is a flag and you wouldnt need
1136 * a type XFRM_MSG_UPDPOLICY - JHS */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137 excl = nlh->nlmsg_type == XFRM_MSG_NEWPOLICY;
1138 err = xfrm_policy_insert(p->dir, xp, excl);
Eric Paris25323862008-04-18 10:09:25 -04001139 xfrm_audit_policy_add(xp, err ? 0 : 1, loginuid, sessionid, sid);
Joy Latten161a09e2006-11-27 13:11:54 -06001140
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141 if (err) {
Paul Moore03e1ad72008-04-12 19:07:52 -07001142 security_xfrm_policy_free(xp->security);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 kfree(xp);
1144 return err;
1145 }
1146
Herbert Xuf60f6b82005-06-18 22:44:37 -07001147 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001148 c.seq = nlh->nlmsg_seq;
1149 c.pid = nlh->nlmsg_pid;
1150 km_policy_notify(xp, p->dir, &c);
1151
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 xfrm_pol_put(xp);
1153
1154 return 0;
1155}
1156
1157static int copy_to_user_tmpl(struct xfrm_policy *xp, struct sk_buff *skb)
1158{
1159 struct xfrm_user_tmpl vec[XFRM_MAX_DEPTH];
1160 int i;
1161
1162 if (xp->xfrm_nr == 0)
1163 return 0;
1164
1165 for (i = 0; i < xp->xfrm_nr; i++) {
1166 struct xfrm_user_tmpl *up = &vec[i];
1167 struct xfrm_tmpl *kp = &xp->xfrm_vec[i];
1168
1169 memcpy(&up->id, &kp->id, sizeof(up->id));
Miika Komu8511d012006-11-30 16:40:51 -08001170 up->family = kp->encap_family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 memcpy(&up->saddr, &kp->saddr, sizeof(up->saddr));
1172 up->reqid = kp->reqid;
1173 up->mode = kp->mode;
1174 up->share = kp->share;
1175 up->optional = kp->optional;
1176 up->aalgos = kp->aalgos;
1177 up->ealgos = kp->ealgos;
1178 up->calgos = kp->calgos;
1179 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180
Thomas Grafc0144be2007-08-22 13:55:43 -07001181 return nla_put(skb, XFRMA_TMPL,
1182 sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr, vec);
Trent Jaegerdf718372005-12-13 23:12:27 -08001183}
1184
Serge Hallyn0d681622006-07-24 23:30:44 -07001185static inline int copy_to_user_state_sec_ctx(struct xfrm_state *x, struct sk_buff *skb)
1186{
1187 if (x->security) {
1188 return copy_sec_ctx(x->security, skb);
1189 }
1190 return 0;
1191}
1192
1193static inline int copy_to_user_sec_ctx(struct xfrm_policy *xp, struct sk_buff *skb)
1194{
1195 if (xp->security) {
1196 return copy_sec_ctx(xp->security, skb);
1197 }
1198 return 0;
1199}
Thomas Grafcfbfd452007-08-22 13:57:04 -07001200static inline size_t userpolicy_type_attrsize(void)
1201{
1202#ifdef CONFIG_XFRM_SUB_POLICY
1203 return nla_total_size(sizeof(struct xfrm_userpolicy_type));
1204#else
1205 return 0;
1206#endif
1207}
Serge Hallyn0d681622006-07-24 23:30:44 -07001208
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001209#ifdef CONFIG_XFRM_SUB_POLICY
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001210static int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001211{
Thomas Grafc0144be2007-08-22 13:55:43 -07001212 struct xfrm_userpolicy_type upt = {
1213 .type = type,
1214 };
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001215
Thomas Grafc0144be2007-08-22 13:55:43 -07001216 return nla_put(skb, XFRMA_POLICY_TYPE, sizeof(upt), &upt);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001217}
1218
1219#else
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001220static inline int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001221{
1222 return 0;
1223}
1224#endif
1225
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226static int dump_one_policy(struct xfrm_policy *xp, int dir, int count, void *ptr)
1227{
1228 struct xfrm_dump_info *sp = ptr;
1229 struct xfrm_userpolicy_info *p;
1230 struct sk_buff *in_skb = sp->in_skb;
1231 struct sk_buff *skb = sp->out_skb;
1232 struct nlmsghdr *nlh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233
Thomas Graf79b8b7f2007-08-22 12:46:53 -07001234 nlh = nlmsg_put(skb, NETLINK_CB(in_skb).pid, sp->nlmsg_seq,
1235 XFRM_MSG_NEWPOLICY, sizeof(*p), sp->nlmsg_flags);
1236 if (nlh == NULL)
1237 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238
Thomas Graf7b67c852007-08-22 13:53:52 -07001239 p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240 copy_to_user_policy(xp, p, dir);
1241 if (copy_to_user_tmpl(xp, skb) < 0)
1242 goto nlmsg_failure;
Trent Jaegerdf718372005-12-13 23:12:27 -08001243 if (copy_to_user_sec_ctx(xp, skb))
1244 goto nlmsg_failure;
Jamal Hadi Salim1459bb32006-11-20 16:51:22 -08001245 if (copy_to_user_policy_type(xp->type, skb) < 0)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001246 goto nlmsg_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247
Thomas Graf98250692007-08-22 12:47:26 -07001248 nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 return 0;
1250
1251nlmsg_failure:
Thomas Graf98250692007-08-22 12:47:26 -07001252 nlmsg_cancel(skb, nlh);
1253 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254}
1255
Timo Teras4c563f72008-02-28 21:31:08 -08001256static int xfrm_dump_policy_done(struct netlink_callback *cb)
1257{
1258 struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *) &cb->args[1];
1259
1260 xfrm_policy_walk_done(walk);
1261 return 0;
1262}
1263
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264static int xfrm_dump_policy(struct sk_buff *skb, struct netlink_callback *cb)
1265{
Timo Teras4c563f72008-02-28 21:31:08 -08001266 struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *) &cb->args[1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267 struct xfrm_dump_info info;
1268
Timo Teras4c563f72008-02-28 21:31:08 -08001269 BUILD_BUG_ON(sizeof(struct xfrm_policy_walk) >
1270 sizeof(cb->args) - sizeof(cb->args[0]));
1271
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272 info.in_skb = cb->skb;
1273 info.out_skb = skb;
1274 info.nlmsg_seq = cb->nlh->nlmsg_seq;
1275 info.nlmsg_flags = NLM_F_MULTI;
Timo Teras4c563f72008-02-28 21:31:08 -08001276
1277 if (!cb->args[0]) {
1278 cb->args[0] = 1;
1279 xfrm_policy_walk_init(walk, XFRM_POLICY_TYPE_ANY);
1280 }
1281
1282 (void) xfrm_policy_walk(walk, dump_one_policy, &info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283
1284 return skb->len;
1285}
1286
1287static struct sk_buff *xfrm_policy_netlink(struct sk_buff *in_skb,
1288 struct xfrm_policy *xp,
1289 int dir, u32 seq)
1290{
1291 struct xfrm_dump_info info;
1292 struct sk_buff *skb;
1293
Thomas Graf7deb2262007-08-22 13:57:39 -07001294 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 if (!skb)
1296 return ERR_PTR(-ENOMEM);
1297
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 info.in_skb = in_skb;
1299 info.out_skb = skb;
1300 info.nlmsg_seq = seq;
1301 info.nlmsg_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302
1303 if (dump_one_policy(xp, dir, 0, &info) < 0) {
1304 kfree_skb(skb);
1305 return NULL;
1306 }
1307
1308 return skb;
1309}
1310
Christoph Hellwig22e70052007-01-02 15:22:30 -08001311static int xfrm_get_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001312 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313{
1314 struct xfrm_policy *xp;
1315 struct xfrm_userpolicy_id *p;
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001316 u8 type = XFRM_POLICY_TYPE_MAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317 int err;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001318 struct km_event c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 int delete;
1320
Thomas Graf7b67c852007-08-22 13:53:52 -07001321 p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322 delete = nlh->nlmsg_type == XFRM_MSG_DELPOLICY;
1323
Thomas Graf35a7aa02007-08-22 14:00:40 -07001324 err = copy_from_user_policy_type(&type, attrs);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001325 if (err)
1326 return err;
1327
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328 err = verify_policy_dir(p->dir);
1329 if (err)
1330 return err;
1331
1332 if (p->index)
Eric Parisef41aaa2007-03-07 15:37:58 -08001333 xp = xfrm_policy_byid(type, p->dir, p->index, delete, &err);
Trent Jaegerdf718372005-12-13 23:12:27 -08001334 else {
Thomas Graf5424f322007-08-22 14:01:33 -07001335 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
Paul Moore03e1ad72008-04-12 19:07:52 -07001336 struct xfrm_sec_ctx *ctx;
Trent Jaegerdf718372005-12-13 23:12:27 -08001337
Thomas Graf35a7aa02007-08-22 14:00:40 -07001338 err = verify_sec_ctx_len(attrs);
Trent Jaegerdf718372005-12-13 23:12:27 -08001339 if (err)
1340 return err;
1341
Denis V. Lunev2c8dd112008-04-14 14:47:48 -07001342 ctx = NULL;
Trent Jaegerdf718372005-12-13 23:12:27 -08001343 if (rt) {
Thomas Graf5424f322007-08-22 14:01:33 -07001344 struct xfrm_user_sec_ctx *uctx = nla_data(rt);
Trent Jaegerdf718372005-12-13 23:12:27 -08001345
Paul Moore03e1ad72008-04-12 19:07:52 -07001346 err = security_xfrm_policy_alloc(&ctx, uctx);
1347 if (err)
Trent Jaegerdf718372005-12-13 23:12:27 -08001348 return err;
Denis V. Lunev2c8dd112008-04-14 14:47:48 -07001349 }
Paul Moore03e1ad72008-04-12 19:07:52 -07001350 xp = xfrm_policy_bysel_ctx(type, p->dir, &p->sel, ctx,
Eric Parisef41aaa2007-03-07 15:37:58 -08001351 delete, &err);
Paul Moore03e1ad72008-04-12 19:07:52 -07001352 security_xfrm_policy_free(ctx);
Trent Jaegerdf718372005-12-13 23:12:27 -08001353 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354 if (xp == NULL)
1355 return -ENOENT;
1356
1357 if (!delete) {
1358 struct sk_buff *resp_skb;
1359
1360 resp_skb = xfrm_policy_netlink(skb, xp, p->dir, nlh->nlmsg_seq);
1361 if (IS_ERR(resp_skb)) {
1362 err = PTR_ERR(resp_skb);
1363 } else {
Thomas Graf082a1ad2007-08-22 13:54:36 -07001364 err = nlmsg_unicast(xfrm_nl, resp_skb,
1365 NETLINK_CB(skb).pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 }
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001367 } else {
Eric Paris25323862008-04-18 10:09:25 -04001368 uid_t loginuid = NETLINK_CB(skb).loginuid;
1369 u32 sessionid = NETLINK_CB(skb).sessionid;
1370 u32 sid = NETLINK_CB(skb).sid;
1371
1372 xfrm_audit_policy_delete(xp, err ? 0 : 1, loginuid, sessionid,
1373 sid);
David S. Miller13fcfbb02007-02-12 13:53:54 -08001374
1375 if (err != 0)
Catherine Zhangc8c05a82006-06-08 23:39:49 -07001376 goto out;
David S. Miller13fcfbb02007-02-12 13:53:54 -08001377
Herbert Xue7443892005-06-18 22:44:18 -07001378 c.data.byid = p->index;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001379 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001380 c.seq = nlh->nlmsg_seq;
1381 c.pid = nlh->nlmsg_pid;
1382 km_policy_notify(xp, p->dir, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383 }
1384
Catherine Zhangc8c05a82006-06-08 23:39:49 -07001385out:
Eric Parisef41aaa2007-03-07 15:37:58 -08001386 xfrm_pol_put(xp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387 return err;
1388}
1389
Christoph Hellwig22e70052007-01-02 15:22:30 -08001390static int xfrm_flush_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001391 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392{
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001393 struct km_event c;
Thomas Graf7b67c852007-08-22 13:53:52 -07001394 struct xfrm_usersa_flush *p = nlmsg_data(nlh);
Joy Latten161a09e2006-11-27 13:11:54 -06001395 struct xfrm_audit audit_info;
Joy Latten4aa2e622007-06-04 19:05:57 -04001396 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397
Joy Latten161a09e2006-11-27 13:11:54 -06001398 audit_info.loginuid = NETLINK_CB(skb).loginuid;
Eric Paris25323862008-04-18 10:09:25 -04001399 audit_info.sessionid = NETLINK_CB(skb).sessionid;
Joy Latten161a09e2006-11-27 13:11:54 -06001400 audit_info.secid = NETLINK_CB(skb).sid;
Joy Latten4aa2e622007-06-04 19:05:57 -04001401 err = xfrm_state_flush(p->proto, &audit_info);
1402 if (err)
1403 return err;
Herbert Xubf088672005-06-18 22:44:00 -07001404 c.data.proto = p->proto;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001405 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001406 c.seq = nlh->nlmsg_seq;
1407 c.pid = nlh->nlmsg_pid;
1408 km_state_notify(NULL, &c);
1409
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410 return 0;
1411}
1412
Thomas Graf7deb2262007-08-22 13:57:39 -07001413static inline size_t xfrm_aevent_msgsize(void)
1414{
1415 return NLMSG_ALIGN(sizeof(struct xfrm_aevent_id))
1416 + nla_total_size(sizeof(struct xfrm_replay_state))
1417 + nla_total_size(sizeof(struct xfrm_lifetime_cur))
1418 + nla_total_size(4) /* XFRM_AE_RTHR */
1419 + nla_total_size(4); /* XFRM_AE_ETHR */
1420}
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001421
1422static int build_aevent(struct sk_buff *skb, struct xfrm_state *x, struct km_event *c)
1423{
1424 struct xfrm_aevent_id *id;
1425 struct nlmsghdr *nlh;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001426
Thomas Graf79b8b7f2007-08-22 12:46:53 -07001427 nlh = nlmsg_put(skb, c->pid, c->seq, XFRM_MSG_NEWAE, sizeof(*id), 0);
1428 if (nlh == NULL)
1429 return -EMSGSIZE;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001430
Thomas Graf7b67c852007-08-22 13:53:52 -07001431 id = nlmsg_data(nlh);
Jamal Hadi Salim2b5f6dc2006-12-02 22:22:25 -08001432 memcpy(&id->sa_id.daddr, &x->id.daddr,sizeof(x->id.daddr));
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001433 id->sa_id.spi = x->id.spi;
1434 id->sa_id.family = x->props.family;
1435 id->sa_id.proto = x->id.proto;
Jamal Hadi Salim2b5f6dc2006-12-02 22:22:25 -08001436 memcpy(&id->saddr, &x->props.saddr,sizeof(x->props.saddr));
1437 id->reqid = x->props.reqid;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001438 id->flags = c->data.aevent;
1439
Thomas Grafc0144be2007-08-22 13:55:43 -07001440 NLA_PUT(skb, XFRMA_REPLAY_VAL, sizeof(x->replay), &x->replay);
1441 NLA_PUT(skb, XFRMA_LTIME_VAL, sizeof(x->curlft), &x->curlft);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001442
Thomas Grafc0144be2007-08-22 13:55:43 -07001443 if (id->flags & XFRM_AE_RTHR)
1444 NLA_PUT_U32(skb, XFRMA_REPLAY_THRESH, x->replay_maxdiff);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001445
Thomas Grafc0144be2007-08-22 13:55:43 -07001446 if (id->flags & XFRM_AE_ETHR)
1447 NLA_PUT_U32(skb, XFRMA_ETIMER_THRESH,
1448 x->replay_maxage * 10 / HZ);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001449
Thomas Graf98250692007-08-22 12:47:26 -07001450 return nlmsg_end(skb, nlh);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001451
Thomas Grafc0144be2007-08-22 13:55:43 -07001452nla_put_failure:
Thomas Graf98250692007-08-22 12:47:26 -07001453 nlmsg_cancel(skb, nlh);
1454 return -EMSGSIZE;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001455}
1456
Christoph Hellwig22e70052007-01-02 15:22:30 -08001457static int xfrm_get_ae(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001458 struct nlattr **attrs)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001459{
1460 struct xfrm_state *x;
1461 struct sk_buff *r_skb;
1462 int err;
1463 struct km_event c;
Thomas Graf7b67c852007-08-22 13:53:52 -07001464 struct xfrm_aevent_id *p = nlmsg_data(nlh);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001465 struct xfrm_usersa_id *id = &p->sa_id;
1466
Thomas Graf7deb2262007-08-22 13:57:39 -07001467 r_skb = nlmsg_new(xfrm_aevent_msgsize(), GFP_ATOMIC);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001468 if (r_skb == NULL)
1469 return -ENOMEM;
1470
1471 x = xfrm_state_lookup(&id->daddr, id->spi, id->proto, id->family);
1472 if (x == NULL) {
Patrick McHardyb08d5842007-02-27 09:57:37 -08001473 kfree_skb(r_skb);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001474 return -ESRCH;
1475 }
1476
1477 /*
1478 * XXX: is this lock really needed - none of the other
1479 * gets lock (the concern is things getting updated
1480 * while we are still reading) - jhs
1481 */
1482 spin_lock_bh(&x->lock);
1483 c.data.aevent = p->flags;
1484 c.seq = nlh->nlmsg_seq;
1485 c.pid = nlh->nlmsg_pid;
1486
1487 if (build_aevent(r_skb, x, &c) < 0)
1488 BUG();
Thomas Graf082a1ad2007-08-22 13:54:36 -07001489 err = nlmsg_unicast(xfrm_nl, r_skb, NETLINK_CB(skb).pid);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001490 spin_unlock_bh(&x->lock);
1491 xfrm_state_put(x);
1492 return err;
1493}
1494
Christoph Hellwig22e70052007-01-02 15:22:30 -08001495static int xfrm_new_ae(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001496 struct nlattr **attrs)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001497{
1498 struct xfrm_state *x;
1499 struct km_event c;
1500 int err = - EINVAL;
Thomas Graf7b67c852007-08-22 13:53:52 -07001501 struct xfrm_aevent_id *p = nlmsg_data(nlh);
Thomas Graf5424f322007-08-22 14:01:33 -07001502 struct nlattr *rp = attrs[XFRMA_REPLAY_VAL];
1503 struct nlattr *lt = attrs[XFRMA_LTIME_VAL];
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001504
1505 if (!lt && !rp)
1506 return err;
1507
1508 /* pedantic mode - thou shalt sayeth replaceth */
1509 if (!(nlh->nlmsg_flags&NLM_F_REPLACE))
1510 return err;
1511
1512 x = xfrm_state_lookup(&p->sa_id.daddr, p->sa_id.spi, p->sa_id.proto, p->sa_id.family);
1513 if (x == NULL)
1514 return -ESRCH;
1515
1516 if (x->km.state != XFRM_STATE_VALID)
1517 goto out;
1518
1519 spin_lock_bh(&x->lock);
Thomas Graf35a7aa02007-08-22 14:00:40 -07001520 xfrm_update_ae_params(x, attrs);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001521 spin_unlock_bh(&x->lock);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001522
1523 c.event = nlh->nlmsg_type;
1524 c.seq = nlh->nlmsg_seq;
1525 c.pid = nlh->nlmsg_pid;
1526 c.data.aevent = XFRM_AE_CU;
1527 km_state_notify(x, &c);
1528 err = 0;
1529out:
1530 xfrm_state_put(x);
1531 return err;
1532}
1533
Christoph Hellwig22e70052007-01-02 15:22:30 -08001534static int xfrm_flush_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001535 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536{
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001537 struct km_event c;
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001538 u8 type = XFRM_POLICY_TYPE_MAIN;
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001539 int err;
Joy Latten161a09e2006-11-27 13:11:54 -06001540 struct xfrm_audit audit_info;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001541
Thomas Graf35a7aa02007-08-22 14:00:40 -07001542 err = copy_from_user_policy_type(&type, attrs);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001543 if (err)
1544 return err;
1545
Joy Latten161a09e2006-11-27 13:11:54 -06001546 audit_info.loginuid = NETLINK_CB(skb).loginuid;
Eric Paris25323862008-04-18 10:09:25 -04001547 audit_info.sessionid = NETLINK_CB(skb).sessionid;
Joy Latten161a09e2006-11-27 13:11:54 -06001548 audit_info.secid = NETLINK_CB(skb).sid;
Joy Latten4aa2e622007-06-04 19:05:57 -04001549 err = xfrm_policy_flush(type, &audit_info);
1550 if (err)
1551 return err;
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001552 c.data.type = type;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001553 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001554 c.seq = nlh->nlmsg_seq;
1555 c.pid = nlh->nlmsg_pid;
1556 km_policy_notify(NULL, 0, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557 return 0;
1558}
1559
Christoph Hellwig22e70052007-01-02 15:22:30 -08001560static int xfrm_add_pol_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001561 struct nlattr **attrs)
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001562{
1563 struct xfrm_policy *xp;
Thomas Graf7b67c852007-08-22 13:53:52 -07001564 struct xfrm_user_polexpire *up = nlmsg_data(nlh);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001565 struct xfrm_userpolicy_info *p = &up->pol;
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001566 u8 type = XFRM_POLICY_TYPE_MAIN;
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001567 int err = -ENOENT;
1568
Thomas Graf35a7aa02007-08-22 14:00:40 -07001569 err = copy_from_user_policy_type(&type, attrs);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001570 if (err)
1571 return err;
1572
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001573 if (p->index)
Eric Parisef41aaa2007-03-07 15:37:58 -08001574 xp = xfrm_policy_byid(type, p->dir, p->index, 0, &err);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001575 else {
Thomas Graf5424f322007-08-22 14:01:33 -07001576 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
Paul Moore03e1ad72008-04-12 19:07:52 -07001577 struct xfrm_sec_ctx *ctx;
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001578
Thomas Graf35a7aa02007-08-22 14:00:40 -07001579 err = verify_sec_ctx_len(attrs);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001580 if (err)
1581 return err;
1582
Denis V. Lunev2c8dd112008-04-14 14:47:48 -07001583 ctx = NULL;
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001584 if (rt) {
Thomas Graf5424f322007-08-22 14:01:33 -07001585 struct xfrm_user_sec_ctx *uctx = nla_data(rt);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001586
Paul Moore03e1ad72008-04-12 19:07:52 -07001587 err = security_xfrm_policy_alloc(&ctx, uctx);
1588 if (err)
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001589 return err;
Denis V. Lunev2c8dd112008-04-14 14:47:48 -07001590 }
Paul Moore03e1ad72008-04-12 19:07:52 -07001591 xp = xfrm_policy_bysel_ctx(type, p->dir, &p->sel, ctx, 0, &err);
1592 security_xfrm_policy_free(ctx);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001593 }
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001594 if (xp == NULL)
Eric Parisef41aaa2007-03-07 15:37:58 -08001595 return -ENOENT;
Paul Moore03e1ad72008-04-12 19:07:52 -07001596
Eric Parisef41aaa2007-03-07 15:37:58 -08001597 read_lock(&xp->lock);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001598 if (xp->dead) {
1599 read_unlock(&xp->lock);
1600 goto out;
1601 }
1602
1603 read_unlock(&xp->lock);
1604 err = 0;
1605 if (up->hard) {
Eric Paris25323862008-04-18 10:09:25 -04001606 uid_t loginuid = NETLINK_CB(skb).loginuid;
1607 uid_t sessionid = NETLINK_CB(skb).sessionid;
1608 u32 sid = NETLINK_CB(skb).sid;
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001609 xfrm_policy_delete(xp, p->dir);
Eric Paris25323862008-04-18 10:09:25 -04001610 xfrm_audit_policy_delete(xp, 1, loginuid, sessionid, sid);
Joy Latten161a09e2006-11-27 13:11:54 -06001611
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001612 } else {
1613 // reset the timers here?
1614 printk("Dont know what to do with soft policy expire\n");
1615 }
1616 km_policy_expired(xp, p->dir, up->hard, current->pid);
1617
1618out:
1619 xfrm_pol_put(xp);
1620 return err;
1621}
1622
Christoph Hellwig22e70052007-01-02 15:22:30 -08001623static int xfrm_add_sa_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001624 struct nlattr **attrs)
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001625{
1626 struct xfrm_state *x;
1627 int err;
Thomas Graf7b67c852007-08-22 13:53:52 -07001628 struct xfrm_user_expire *ue = nlmsg_data(nlh);
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001629 struct xfrm_usersa_info *p = &ue->state;
1630
1631 x = xfrm_state_lookup(&p->id.daddr, p->id.spi, p->id.proto, p->family);
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001632
David S. Miller3a765aa2007-02-26 14:52:21 -08001633 err = -ENOENT;
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001634 if (x == NULL)
1635 return err;
1636
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001637 spin_lock_bh(&x->lock);
David S. Miller3a765aa2007-02-26 14:52:21 -08001638 err = -EINVAL;
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001639 if (x->km.state != XFRM_STATE_VALID)
1640 goto out;
1641 km_state_expired(x, ue->hard, current->pid);
1642
Joy Latten161a09e2006-11-27 13:11:54 -06001643 if (ue->hard) {
Eric Paris25323862008-04-18 10:09:25 -04001644 uid_t loginuid = NETLINK_CB(skb).loginuid;
1645 uid_t sessionid = NETLINK_CB(skb).sessionid;
1646 u32 sid = NETLINK_CB(skb).sid;
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001647 __xfrm_state_delete(x);
Eric Paris25323862008-04-18 10:09:25 -04001648 xfrm_audit_state_delete(x, 1, loginuid, sessionid, sid);
Joy Latten161a09e2006-11-27 13:11:54 -06001649 }
David S. Miller3a765aa2007-02-26 14:52:21 -08001650 err = 0;
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001651out:
1652 spin_unlock_bh(&x->lock);
1653 xfrm_state_put(x);
1654 return err;
1655}
1656
Christoph Hellwig22e70052007-01-02 15:22:30 -08001657static int xfrm_add_acquire(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001658 struct nlattr **attrs)
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001659{
1660 struct xfrm_policy *xp;
1661 struct xfrm_user_tmpl *ut;
1662 int i;
Thomas Graf5424f322007-08-22 14:01:33 -07001663 struct nlattr *rt = attrs[XFRMA_TMPL];
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001664
Thomas Graf7b67c852007-08-22 13:53:52 -07001665 struct xfrm_user_acquire *ua = nlmsg_data(nlh);
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001666 struct xfrm_state *x = xfrm_state_alloc();
1667 int err = -ENOMEM;
1668
1669 if (!x)
1670 return err;
1671
1672 err = verify_newpolicy_info(&ua->policy);
1673 if (err) {
1674 printk("BAD policy passed\n");
1675 kfree(x);
1676 return err;
1677 }
1678
1679 /* build an XP */
Thomas Graf5424f322007-08-22 14:01:33 -07001680 xp = xfrm_policy_construct(&ua->policy, attrs, &err);
David S. Millerb4ad86bf2006-12-03 19:19:26 -08001681 if (!xp) {
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001682 kfree(x);
1683 return err;
1684 }
1685
1686 memcpy(&x->id, &ua->id, sizeof(ua->id));
1687 memcpy(&x->props.saddr, &ua->saddr, sizeof(ua->saddr));
1688 memcpy(&x->sel, &ua->sel, sizeof(ua->sel));
1689
Thomas Graf5424f322007-08-22 14:01:33 -07001690 ut = nla_data(rt);
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001691 /* extract the templates and for each call km_key */
1692 for (i = 0; i < xp->xfrm_nr; i++, ut++) {
1693 struct xfrm_tmpl *t = &xp->xfrm_vec[i];
1694 memcpy(&x->id, &t->id, sizeof(x->id));
1695 x->props.mode = t->mode;
1696 x->props.reqid = t->reqid;
1697 x->props.family = ut->family;
1698 t->aalgos = ua->aalgos;
1699 t->ealgos = ua->ealgos;
1700 t->calgos = ua->calgos;
1701 err = km_query(x, t, xp);
1702
1703 }
1704
1705 kfree(x);
1706 kfree(xp);
1707
1708 return 0;
1709}
1710
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001711#ifdef CONFIG_XFRM_MIGRATE
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001712static int copy_from_user_migrate(struct xfrm_migrate *ma,
Thomas Graf5424f322007-08-22 14:01:33 -07001713 struct nlattr **attrs, int *num)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001714{
Thomas Graf5424f322007-08-22 14:01:33 -07001715 struct nlattr *rt = attrs[XFRMA_MIGRATE];
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001716 struct xfrm_user_migrate *um;
1717 int i, num_migrate;
1718
Thomas Graf5424f322007-08-22 14:01:33 -07001719 um = nla_data(rt);
1720 num_migrate = nla_len(rt) / sizeof(*um);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001721
1722 if (num_migrate <= 0 || num_migrate > XFRM_MAX_DEPTH)
1723 return -EINVAL;
1724
1725 for (i = 0; i < num_migrate; i++, um++, ma++) {
1726 memcpy(&ma->old_daddr, &um->old_daddr, sizeof(ma->old_daddr));
1727 memcpy(&ma->old_saddr, &um->old_saddr, sizeof(ma->old_saddr));
1728 memcpy(&ma->new_daddr, &um->new_daddr, sizeof(ma->new_daddr));
1729 memcpy(&ma->new_saddr, &um->new_saddr, sizeof(ma->new_saddr));
1730
1731 ma->proto = um->proto;
1732 ma->mode = um->mode;
1733 ma->reqid = um->reqid;
1734
1735 ma->old_family = um->old_family;
1736 ma->new_family = um->new_family;
1737 }
1738
1739 *num = i;
1740 return 0;
1741}
1742
1743static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001744 struct nlattr **attrs)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001745{
Thomas Graf7b67c852007-08-22 13:53:52 -07001746 struct xfrm_userpolicy_id *pi = nlmsg_data(nlh);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001747 struct xfrm_migrate m[XFRM_MAX_DEPTH];
1748 u8 type;
1749 int err;
1750 int n = 0;
1751
Thomas Graf35a7aa02007-08-22 14:00:40 -07001752 if (attrs[XFRMA_MIGRATE] == NULL)
Thomas Grafcf5cb792007-08-22 13:59:04 -07001753 return -EINVAL;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001754
Thomas Graf5424f322007-08-22 14:01:33 -07001755 err = copy_from_user_policy_type(&type, attrs);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001756 if (err)
1757 return err;
1758
1759 err = copy_from_user_migrate((struct xfrm_migrate *)m,
Thomas Graf5424f322007-08-22 14:01:33 -07001760 attrs, &n);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001761 if (err)
1762 return err;
1763
1764 if (!n)
1765 return 0;
1766
1767 xfrm_migrate(&pi->sel, pi->dir, type, m, n);
1768
1769 return 0;
1770}
1771#else
1772static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001773 struct nlattr **attrs)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001774{
1775 return -ENOPROTOOPT;
1776}
1777#endif
1778
1779#ifdef CONFIG_XFRM_MIGRATE
1780static int copy_to_user_migrate(struct xfrm_migrate *m, struct sk_buff *skb)
1781{
1782 struct xfrm_user_migrate um;
1783
1784 memset(&um, 0, sizeof(um));
1785 um.proto = m->proto;
1786 um.mode = m->mode;
1787 um.reqid = m->reqid;
1788 um.old_family = m->old_family;
1789 memcpy(&um.old_daddr, &m->old_daddr, sizeof(um.old_daddr));
1790 memcpy(&um.old_saddr, &m->old_saddr, sizeof(um.old_saddr));
1791 um.new_family = m->new_family;
1792 memcpy(&um.new_daddr, &m->new_daddr, sizeof(um.new_daddr));
1793 memcpy(&um.new_saddr, &m->new_saddr, sizeof(um.new_saddr));
1794
Thomas Grafc0144be2007-08-22 13:55:43 -07001795 return nla_put(skb, XFRMA_MIGRATE, sizeof(um), &um);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001796}
1797
Thomas Graf7deb2262007-08-22 13:57:39 -07001798static inline size_t xfrm_migrate_msgsize(int num_migrate)
1799{
1800 return NLMSG_ALIGN(sizeof(struct xfrm_userpolicy_id))
1801 + nla_total_size(sizeof(struct xfrm_user_migrate) * num_migrate)
1802 + userpolicy_type_attrsize();
1803}
1804
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001805static int build_migrate(struct sk_buff *skb, struct xfrm_migrate *m,
1806 int num_migrate, struct xfrm_selector *sel,
1807 u8 dir, u8 type)
1808{
1809 struct xfrm_migrate *mp;
1810 struct xfrm_userpolicy_id *pol_id;
1811 struct nlmsghdr *nlh;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001812 int i;
1813
Thomas Graf79b8b7f2007-08-22 12:46:53 -07001814 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_MIGRATE, sizeof(*pol_id), 0);
1815 if (nlh == NULL)
1816 return -EMSGSIZE;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001817
Thomas Graf7b67c852007-08-22 13:53:52 -07001818 pol_id = nlmsg_data(nlh);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001819 /* copy data from selector, dir, and type to the pol_id */
1820 memset(pol_id, 0, sizeof(*pol_id));
1821 memcpy(&pol_id->sel, sel, sizeof(pol_id->sel));
1822 pol_id->dir = dir;
1823
1824 if (copy_to_user_policy_type(type, skb) < 0)
1825 goto nlmsg_failure;
1826
1827 for (i = 0, mp = m ; i < num_migrate; i++, mp++) {
1828 if (copy_to_user_migrate(mp, skb) < 0)
1829 goto nlmsg_failure;
1830 }
1831
Thomas Graf98250692007-08-22 12:47:26 -07001832 return nlmsg_end(skb, nlh);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001833nlmsg_failure:
Thomas Graf98250692007-08-22 12:47:26 -07001834 nlmsg_cancel(skb, nlh);
1835 return -EMSGSIZE;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001836}
1837
1838static int xfrm_send_migrate(struct xfrm_selector *sel, u8 dir, u8 type,
1839 struct xfrm_migrate *m, int num_migrate)
1840{
1841 struct sk_buff *skb;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001842
Thomas Graf7deb2262007-08-22 13:57:39 -07001843 skb = nlmsg_new(xfrm_migrate_msgsize(num_migrate), GFP_ATOMIC);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001844 if (skb == NULL)
1845 return -ENOMEM;
1846
1847 /* build migrate */
1848 if (build_migrate(skb, m, num_migrate, sel, dir, type) < 0)
1849 BUG();
1850
Thomas Graf082a1ad2007-08-22 13:54:36 -07001851 return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_MIGRATE, GFP_ATOMIC);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001852}
1853#else
1854static int xfrm_send_migrate(struct xfrm_selector *sel, u8 dir, u8 type,
1855 struct xfrm_migrate *m, int num_migrate)
1856{
1857 return -ENOPROTOOPT;
1858}
1859#endif
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001860
Thomas Grafa7bd9a42007-08-22 13:58:18 -07001861#define XMSGSIZE(type) sizeof(struct type)
Thomas Graf492b5582005-05-03 14:26:40 -07001862
1863static const int xfrm_msg_min[XFRM_NR_MSGTYPES] = {
1864 [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
1865 [XFRM_MSG_DELSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
1866 [XFRM_MSG_GETSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
1867 [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
1868 [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
1869 [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
1870 [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userspi_info),
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001871 [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_acquire),
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001872 [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_expire),
Thomas Graf492b5582005-05-03 14:26:40 -07001873 [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
1874 [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001875 [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_polexpire),
Thomas Graf492b5582005-05-03 14:26:40 -07001876 [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_flush),
Thomas Grafa7bd9a42007-08-22 13:58:18 -07001877 [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = 0,
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001878 [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
1879 [XFRM_MSG_GETAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07001880 [XFRM_MSG_REPORT - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_report),
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001881 [XFRM_MSG_MIGRATE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
Thomas Grafa7bd9a42007-08-22 13:58:18 -07001882 [XFRM_MSG_GETSADINFO - XFRM_MSG_BASE] = sizeof(u32),
1883 [XFRM_MSG_GETSPDINFO - XFRM_MSG_BASE] = sizeof(u32),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884};
1885
Thomas Graf492b5582005-05-03 14:26:40 -07001886#undef XMSGSIZE
1887
Thomas Grafcf5cb792007-08-22 13:59:04 -07001888static const struct nla_policy xfrma_policy[XFRMA_MAX+1] = {
Herbert Xu1a6509d2008-01-28 19:37:29 -08001889 [XFRMA_ALG_AEAD] = { .len = sizeof(struct xfrm_algo_aead) },
Thomas Grafcf5cb792007-08-22 13:59:04 -07001890 [XFRMA_ALG_AUTH] = { .len = sizeof(struct xfrm_algo) },
1891 [XFRMA_ALG_CRYPT] = { .len = sizeof(struct xfrm_algo) },
1892 [XFRMA_ALG_COMP] = { .len = sizeof(struct xfrm_algo) },
1893 [XFRMA_ENCAP] = { .len = sizeof(struct xfrm_encap_tmpl) },
1894 [XFRMA_TMPL] = { .len = sizeof(struct xfrm_user_tmpl) },
1895 [XFRMA_SEC_CTX] = { .len = sizeof(struct xfrm_sec_ctx) },
1896 [XFRMA_LTIME_VAL] = { .len = sizeof(struct xfrm_lifetime_cur) },
1897 [XFRMA_REPLAY_VAL] = { .len = sizeof(struct xfrm_replay_state) },
1898 [XFRMA_REPLAY_THRESH] = { .type = NLA_U32 },
1899 [XFRMA_ETIMER_THRESH] = { .type = NLA_U32 },
1900 [XFRMA_SRCADDR] = { .len = sizeof(xfrm_address_t) },
1901 [XFRMA_COADDR] = { .len = sizeof(xfrm_address_t) },
1902 [XFRMA_POLICY_TYPE] = { .len = sizeof(struct xfrm_userpolicy_type)},
1903 [XFRMA_MIGRATE] = { .len = sizeof(struct xfrm_user_migrate) },
1904};
1905
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906static struct xfrm_link {
Thomas Graf5424f322007-08-22 14:01:33 -07001907 int (*doit)(struct sk_buff *, struct nlmsghdr *, struct nlattr **);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908 int (*dump)(struct sk_buff *, struct netlink_callback *);
Timo Teras4c563f72008-02-28 21:31:08 -08001909 int (*done)(struct netlink_callback *);
Thomas Graf492b5582005-05-03 14:26:40 -07001910} xfrm_dispatch[XFRM_NR_MSGTYPES] = {
1911 [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa },
1912 [XFRM_MSG_DELSA - XFRM_MSG_BASE] = { .doit = xfrm_del_sa },
1913 [XFRM_MSG_GETSA - XFRM_MSG_BASE] = { .doit = xfrm_get_sa,
Timo Teras4c563f72008-02-28 21:31:08 -08001914 .dump = xfrm_dump_sa,
1915 .done = xfrm_dump_sa_done },
Thomas Graf492b5582005-05-03 14:26:40 -07001916 [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy },
1917 [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy },
1918 [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy,
Timo Teras4c563f72008-02-28 21:31:08 -08001919 .dump = xfrm_dump_policy,
1920 .done = xfrm_dump_policy_done },
Thomas Graf492b5582005-05-03 14:26:40 -07001921 [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = { .doit = xfrm_alloc_userspi },
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001922 [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_acquire },
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001923 [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_sa_expire },
Thomas Graf492b5582005-05-03 14:26:40 -07001924 [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy },
1925 [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa },
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001926 [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_pol_expire},
Thomas Graf492b5582005-05-03 14:26:40 -07001927 [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = { .doit = xfrm_flush_sa },
1928 [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_flush_policy },
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001929 [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = { .doit = xfrm_new_ae },
1930 [XFRM_MSG_GETAE - XFRM_MSG_BASE] = { .doit = xfrm_get_ae },
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001931 [XFRM_MSG_MIGRATE - XFRM_MSG_BASE] = { .doit = xfrm_do_migrate },
Jamal Hadi Salim566ec032007-04-26 14:12:15 -07001932 [XFRM_MSG_GETSADINFO - XFRM_MSG_BASE] = { .doit = xfrm_get_sadinfo },
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -07001933 [XFRM_MSG_GETSPDINFO - XFRM_MSG_BASE] = { .doit = xfrm_get_spdinfo },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934};
1935
Thomas Graf1d00a4e2007-03-22 23:30:12 -07001936static int xfrm_user_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937{
Thomas Graf35a7aa02007-08-22 14:00:40 -07001938 struct nlattr *attrs[XFRMA_MAX+1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939 struct xfrm_link *link;
Thomas Grafa7bd9a42007-08-22 13:58:18 -07001940 int type, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942 type = nlh->nlmsg_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943 if (type > XFRM_MSG_MAX)
Thomas Graf1d00a4e2007-03-22 23:30:12 -07001944 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945
1946 type -= XFRM_MSG_BASE;
1947 link = &xfrm_dispatch[type];
1948
1949 /* All operations require privileges, even GET */
Thomas Graf1d00a4e2007-03-22 23:30:12 -07001950 if (security_netlink_recv(skb, CAP_NET_ADMIN))
1951 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952
Thomas Graf492b5582005-05-03 14:26:40 -07001953 if ((type == (XFRM_MSG_GETSA - XFRM_MSG_BASE) ||
1954 type == (XFRM_MSG_GETPOLICY - XFRM_MSG_BASE)) &&
1955 (nlh->nlmsg_flags & NLM_F_DUMP)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956 if (link->dump == NULL)
Thomas Graf1d00a4e2007-03-22 23:30:12 -07001957 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958
Timo Teras4c563f72008-02-28 21:31:08 -08001959 return netlink_dump_start(xfrm_nl, skb, nlh, link->dump, link->done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960 }
1961
Thomas Graf35a7aa02007-08-22 14:00:40 -07001962 err = nlmsg_parse(nlh, xfrm_msg_min[type], attrs, XFRMA_MAX,
Thomas Grafcf5cb792007-08-22 13:59:04 -07001963 xfrma_policy);
Thomas Grafa7bd9a42007-08-22 13:58:18 -07001964 if (err < 0)
1965 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966
1967 if (link->doit == NULL)
Thomas Graf1d00a4e2007-03-22 23:30:12 -07001968 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969
Thomas Graf5424f322007-08-22 14:01:33 -07001970 return link->doit(skb, nlh, attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971}
1972
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07001973static void xfrm_netlink_rcv(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974{
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07001975 mutex_lock(&xfrm_cfg_mutex);
1976 netlink_rcv_skb(skb, &xfrm_user_rcv_msg);
1977 mutex_unlock(&xfrm_cfg_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978}
1979
Thomas Graf7deb2262007-08-22 13:57:39 -07001980static inline size_t xfrm_expire_msgsize(void)
1981{
1982 return NLMSG_ALIGN(sizeof(struct xfrm_user_expire));
1983}
1984
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001985static int build_expire(struct sk_buff *skb, struct xfrm_state *x, struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986{
1987 struct xfrm_user_expire *ue;
1988 struct nlmsghdr *nlh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989
Thomas Graf79b8b7f2007-08-22 12:46:53 -07001990 nlh = nlmsg_put(skb, c->pid, 0, XFRM_MSG_EXPIRE, sizeof(*ue), 0);
1991 if (nlh == NULL)
1992 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993
Thomas Graf7b67c852007-08-22 13:53:52 -07001994 ue = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995 copy_to_user_state(x, &ue->state);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001996 ue->hard = (c->data.hard != 0) ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997
Thomas Graf98250692007-08-22 12:47:26 -07001998 return nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999}
2000
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002001static int xfrm_exp_state_notify(struct xfrm_state *x, struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002{
2003 struct sk_buff *skb;
2004
Thomas Graf7deb2262007-08-22 13:57:39 -07002005 skb = nlmsg_new(xfrm_expire_msgsize(), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006 if (skb == NULL)
2007 return -ENOMEM;
2008
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002009 if (build_expire(skb, x, c) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010 BUG();
2011
Thomas Graf082a1ad2007-08-22 13:54:36 -07002012 return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_EXPIRE, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013}
2014
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002015static int xfrm_aevent_state_notify(struct xfrm_state *x, struct km_event *c)
2016{
2017 struct sk_buff *skb;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002018
Thomas Graf7deb2262007-08-22 13:57:39 -07002019 skb = nlmsg_new(xfrm_aevent_msgsize(), GFP_ATOMIC);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002020 if (skb == NULL)
2021 return -ENOMEM;
2022
2023 if (build_aevent(skb, x, c) < 0)
2024 BUG();
2025
Thomas Graf082a1ad2007-08-22 13:54:36 -07002026 return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_AEVENTS, GFP_ATOMIC);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002027}
2028
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002029static int xfrm_notify_sa_flush(struct km_event *c)
2030{
2031 struct xfrm_usersa_flush *p;
2032 struct nlmsghdr *nlh;
2033 struct sk_buff *skb;
Thomas Graf7deb2262007-08-22 13:57:39 -07002034 int len = NLMSG_ALIGN(sizeof(struct xfrm_usersa_flush));
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002035
Thomas Graf7deb2262007-08-22 13:57:39 -07002036 skb = nlmsg_new(len, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002037 if (skb == NULL)
2038 return -ENOMEM;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002039
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002040 nlh = nlmsg_put(skb, c->pid, c->seq, XFRM_MSG_FLUSHSA, sizeof(*p), 0);
2041 if (nlh == NULL) {
2042 kfree_skb(skb);
2043 return -EMSGSIZE;
2044 }
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002045
Thomas Graf7b67c852007-08-22 13:53:52 -07002046 p = nlmsg_data(nlh);
Herbert Xubf088672005-06-18 22:44:00 -07002047 p->proto = c->data.proto;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002048
Thomas Graf98250692007-08-22 12:47:26 -07002049 nlmsg_end(skb, nlh);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002050
Thomas Graf082a1ad2007-08-22 13:54:36 -07002051 return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_SA, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002052}
2053
Thomas Graf7deb2262007-08-22 13:57:39 -07002054static inline size_t xfrm_sa_len(struct xfrm_state *x)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002055{
Thomas Graf7deb2262007-08-22 13:57:39 -07002056 size_t l = 0;
Herbert Xu1a6509d2008-01-28 19:37:29 -08002057 if (x->aead)
2058 l += nla_total_size(aead_len(x->aead));
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002059 if (x->aalg)
Eric Dumazet0f99be02008-01-08 23:39:06 -08002060 l += nla_total_size(xfrm_alg_len(x->aalg));
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002061 if (x->ealg)
Eric Dumazet0f99be02008-01-08 23:39:06 -08002062 l += nla_total_size(xfrm_alg_len(x->ealg));
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002063 if (x->calg)
Thomas Graf7deb2262007-08-22 13:57:39 -07002064 l += nla_total_size(sizeof(*x->calg));
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002065 if (x->encap)
Thomas Graf7deb2262007-08-22 13:57:39 -07002066 l += nla_total_size(sizeof(*x->encap));
Herbert Xu68325d32007-10-09 13:30:57 -07002067 if (x->security)
2068 l += nla_total_size(sizeof(struct xfrm_user_sec_ctx) +
2069 x->security->ctx_len);
2070 if (x->coaddr)
2071 l += nla_total_size(sizeof(*x->coaddr));
2072
Herbert Xud26f3982007-11-13 21:47:08 -08002073 /* Must count x->lastused as it may become non-zero behind our back. */
2074 l += nla_total_size(sizeof(u64));
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002075
2076 return l;
2077}
2078
2079static int xfrm_notify_sa(struct xfrm_state *x, struct km_event *c)
2080{
2081 struct xfrm_usersa_info *p;
Herbert Xu0603eac2005-06-18 22:54:36 -07002082 struct xfrm_usersa_id *id;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002083 struct nlmsghdr *nlh;
2084 struct sk_buff *skb;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002085 int len = xfrm_sa_len(x);
Herbert Xu0603eac2005-06-18 22:54:36 -07002086 int headlen;
2087
2088 headlen = sizeof(*p);
2089 if (c->event == XFRM_MSG_DELSA) {
Thomas Graf7deb2262007-08-22 13:57:39 -07002090 len += nla_total_size(headlen);
Herbert Xu0603eac2005-06-18 22:54:36 -07002091 headlen = sizeof(*id);
2092 }
Thomas Graf7deb2262007-08-22 13:57:39 -07002093 len += NLMSG_ALIGN(headlen);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002094
Thomas Graf7deb2262007-08-22 13:57:39 -07002095 skb = nlmsg_new(len, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002096 if (skb == NULL)
2097 return -ENOMEM;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002098
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002099 nlh = nlmsg_put(skb, c->pid, c->seq, c->event, headlen, 0);
2100 if (nlh == NULL)
Thomas Grafc0144be2007-08-22 13:55:43 -07002101 goto nla_put_failure;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002102
Thomas Graf7b67c852007-08-22 13:53:52 -07002103 p = nlmsg_data(nlh);
Herbert Xu0603eac2005-06-18 22:54:36 -07002104 if (c->event == XFRM_MSG_DELSA) {
Thomas Grafc0144be2007-08-22 13:55:43 -07002105 struct nlattr *attr;
2106
Thomas Graf7b67c852007-08-22 13:53:52 -07002107 id = nlmsg_data(nlh);
Herbert Xu0603eac2005-06-18 22:54:36 -07002108 memcpy(&id->daddr, &x->id.daddr, sizeof(id->daddr));
2109 id->spi = x->id.spi;
2110 id->family = x->props.family;
2111 id->proto = x->id.proto;
2112
Thomas Grafc0144be2007-08-22 13:55:43 -07002113 attr = nla_reserve(skb, XFRMA_SA, sizeof(*p));
2114 if (attr == NULL)
2115 goto nla_put_failure;
2116
2117 p = nla_data(attr);
Herbert Xu0603eac2005-06-18 22:54:36 -07002118 }
2119
Herbert Xu68325d32007-10-09 13:30:57 -07002120 if (copy_to_user_state_extra(x, p, skb))
2121 goto nla_put_failure;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002122
Thomas Graf98250692007-08-22 12:47:26 -07002123 nlmsg_end(skb, nlh);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002124
Thomas Graf082a1ad2007-08-22 13:54:36 -07002125 return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_SA, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002126
Thomas Grafc0144be2007-08-22 13:55:43 -07002127nla_put_failure:
Herbert Xu68325d32007-10-09 13:30:57 -07002128 /* Somebody screwed up with xfrm_sa_len! */
2129 WARN_ON(1);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002130 kfree_skb(skb);
2131 return -1;
2132}
2133
2134static int xfrm_send_state_notify(struct xfrm_state *x, struct km_event *c)
2135{
2136
2137 switch (c->event) {
Herbert Xuf60f6b82005-06-18 22:44:37 -07002138 case XFRM_MSG_EXPIRE:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002139 return xfrm_exp_state_notify(x, c);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002140 case XFRM_MSG_NEWAE:
2141 return xfrm_aevent_state_notify(x, c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07002142 case XFRM_MSG_DELSA:
2143 case XFRM_MSG_UPDSA:
2144 case XFRM_MSG_NEWSA:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002145 return xfrm_notify_sa(x, c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07002146 case XFRM_MSG_FLUSHSA:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002147 return xfrm_notify_sa_flush(c);
2148 default:
2149 printk("xfrm_user: Unknown SA event %d\n", c->event);
2150 break;
2151 }
2152
2153 return 0;
2154
2155}
2156
Thomas Graf7deb2262007-08-22 13:57:39 -07002157static inline size_t xfrm_acquire_msgsize(struct xfrm_state *x,
2158 struct xfrm_policy *xp)
2159{
2160 return NLMSG_ALIGN(sizeof(struct xfrm_user_acquire))
2161 + nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr)
2162 + nla_total_size(xfrm_user_sec_ctx_size(x->security))
2163 + userpolicy_type_attrsize();
2164}
2165
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166static int build_acquire(struct sk_buff *skb, struct xfrm_state *x,
2167 struct xfrm_tmpl *xt, struct xfrm_policy *xp,
2168 int dir)
2169{
2170 struct xfrm_user_acquire *ua;
2171 struct nlmsghdr *nlh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172 __u32 seq = xfrm_get_acqseq();
2173
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002174 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_ACQUIRE, sizeof(*ua), 0);
2175 if (nlh == NULL)
2176 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177
Thomas Graf7b67c852007-08-22 13:53:52 -07002178 ua = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179 memcpy(&ua->id, &x->id, sizeof(ua->id));
2180 memcpy(&ua->saddr, &x->props.saddr, sizeof(ua->saddr));
2181 memcpy(&ua->sel, &x->sel, sizeof(ua->sel));
2182 copy_to_user_policy(xp, &ua->policy, dir);
2183 ua->aalgos = xt->aalgos;
2184 ua->ealgos = xt->ealgos;
2185 ua->calgos = xt->calgos;
2186 ua->seq = x->km.seq = seq;
2187
2188 if (copy_to_user_tmpl(xp, skb) < 0)
2189 goto nlmsg_failure;
Serge Hallyn0d681622006-07-24 23:30:44 -07002190 if (copy_to_user_state_sec_ctx(x, skb))
Trent Jaegerdf718372005-12-13 23:12:27 -08002191 goto nlmsg_failure;
Jamal Hadi Salim1459bb32006-11-20 16:51:22 -08002192 if (copy_to_user_policy_type(xp->type, skb) < 0)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07002193 goto nlmsg_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194
Thomas Graf98250692007-08-22 12:47:26 -07002195 return nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002196
2197nlmsg_failure:
Thomas Graf98250692007-08-22 12:47:26 -07002198 nlmsg_cancel(skb, nlh);
2199 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200}
2201
2202static int xfrm_send_acquire(struct xfrm_state *x, struct xfrm_tmpl *xt,
2203 struct xfrm_policy *xp, int dir)
2204{
2205 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206
Thomas Graf7deb2262007-08-22 13:57:39 -07002207 skb = nlmsg_new(xfrm_acquire_msgsize(x, xp), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208 if (skb == NULL)
2209 return -ENOMEM;
2210
2211 if (build_acquire(skb, x, xt, xp, dir) < 0)
2212 BUG();
2213
Thomas Graf082a1ad2007-08-22 13:54:36 -07002214 return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_ACQUIRE, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215}
2216
2217/* User gives us xfrm_user_policy_info followed by an array of 0
2218 * or more templates.
2219 */
Venkat Yekkiralacb969f02006-07-24 23:32:20 -07002220static struct xfrm_policy *xfrm_compile_policy(struct sock *sk, int opt,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002221 u8 *data, int len, int *dir)
2222{
2223 struct xfrm_userpolicy_info *p = (struct xfrm_userpolicy_info *)data;
2224 struct xfrm_user_tmpl *ut = (struct xfrm_user_tmpl *) (p + 1);
2225 struct xfrm_policy *xp;
2226 int nr;
2227
Venkat Yekkiralacb969f02006-07-24 23:32:20 -07002228 switch (sk->sk_family) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229 case AF_INET:
2230 if (opt != IP_XFRM_POLICY) {
2231 *dir = -EOPNOTSUPP;
2232 return NULL;
2233 }
2234 break;
2235#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
2236 case AF_INET6:
2237 if (opt != IPV6_XFRM_POLICY) {
2238 *dir = -EOPNOTSUPP;
2239 return NULL;
2240 }
2241 break;
2242#endif
2243 default:
2244 *dir = -EINVAL;
2245 return NULL;
2246 }
2247
2248 *dir = -EINVAL;
2249
2250 if (len < sizeof(*p) ||
2251 verify_newpolicy_info(p))
2252 return NULL;
2253
2254 nr = ((len - sizeof(*p)) / sizeof(*ut));
David S. Millerb4ad86bf2006-12-03 19:19:26 -08002255 if (validate_tmpl(nr, ut, p->sel.family))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256 return NULL;
2257
Herbert Xua4f1bac2005-07-26 15:43:17 -07002258 if (p->dir > XFRM_POLICY_OUT)
2259 return NULL;
2260
Linus Torvalds1da177e2005-04-16 15:20:36 -07002261 xp = xfrm_policy_alloc(GFP_KERNEL);
2262 if (xp == NULL) {
2263 *dir = -ENOBUFS;
2264 return NULL;
2265 }
2266
2267 copy_from_user_policy(xp, p);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07002268 xp->type = XFRM_POLICY_TYPE_MAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002269 copy_templates(xp, ut, nr);
2270
2271 *dir = p->dir;
2272
2273 return xp;
2274}
2275
Thomas Graf7deb2262007-08-22 13:57:39 -07002276static inline size_t xfrm_polexpire_msgsize(struct xfrm_policy *xp)
2277{
2278 return NLMSG_ALIGN(sizeof(struct xfrm_user_polexpire))
2279 + nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr)
2280 + nla_total_size(xfrm_user_sec_ctx_size(xp->security))
2281 + userpolicy_type_attrsize();
2282}
2283
Linus Torvalds1da177e2005-04-16 15:20:36 -07002284static int build_polexpire(struct sk_buff *skb, struct xfrm_policy *xp,
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002285 int dir, struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002286{
2287 struct xfrm_user_polexpire *upe;
2288 struct nlmsghdr *nlh;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002289 int hard = c->data.hard;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002291 nlh = nlmsg_put(skb, c->pid, 0, XFRM_MSG_POLEXPIRE, sizeof(*upe), 0);
2292 if (nlh == NULL)
2293 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002294
Thomas Graf7b67c852007-08-22 13:53:52 -07002295 upe = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002296 copy_to_user_policy(xp, &upe->pol, dir);
2297 if (copy_to_user_tmpl(xp, skb) < 0)
2298 goto nlmsg_failure;
Trent Jaegerdf718372005-12-13 23:12:27 -08002299 if (copy_to_user_sec_ctx(xp, skb))
2300 goto nlmsg_failure;
Jamal Hadi Salim1459bb32006-11-20 16:51:22 -08002301 if (copy_to_user_policy_type(xp->type, skb) < 0)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07002302 goto nlmsg_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002303 upe->hard = !!hard;
2304
Thomas Graf98250692007-08-22 12:47:26 -07002305 return nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306
2307nlmsg_failure:
Thomas Graf98250692007-08-22 12:47:26 -07002308 nlmsg_cancel(skb, nlh);
2309 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310}
2311
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002312static int xfrm_exp_policy_notify(struct xfrm_policy *xp, int dir, struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002313{
2314 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002315
Thomas Graf7deb2262007-08-22 13:57:39 -07002316 skb = nlmsg_new(xfrm_polexpire_msgsize(xp), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002317 if (skb == NULL)
2318 return -ENOMEM;
2319
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002320 if (build_polexpire(skb, xp, dir, c) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002321 BUG();
2322
Thomas Graf082a1ad2007-08-22 13:54:36 -07002323 return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_EXPIRE, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002324}
2325
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002326static int xfrm_notify_policy(struct xfrm_policy *xp, int dir, struct km_event *c)
2327{
2328 struct xfrm_userpolicy_info *p;
Herbert Xu0603eac2005-06-18 22:54:36 -07002329 struct xfrm_userpolicy_id *id;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002330 struct nlmsghdr *nlh;
2331 struct sk_buff *skb;
Thomas Graf7deb2262007-08-22 13:57:39 -07002332 int len = nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr);
Herbert Xu0603eac2005-06-18 22:54:36 -07002333 int headlen;
2334
2335 headlen = sizeof(*p);
2336 if (c->event == XFRM_MSG_DELPOLICY) {
Thomas Graf7deb2262007-08-22 13:57:39 -07002337 len += nla_total_size(headlen);
Herbert Xu0603eac2005-06-18 22:54:36 -07002338 headlen = sizeof(*id);
2339 }
Thomas Grafcfbfd452007-08-22 13:57:04 -07002340 len += userpolicy_type_attrsize();
Thomas Graf7deb2262007-08-22 13:57:39 -07002341 len += NLMSG_ALIGN(headlen);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002342
Thomas Graf7deb2262007-08-22 13:57:39 -07002343 skb = nlmsg_new(len, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002344 if (skb == NULL)
2345 return -ENOMEM;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002346
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002347 nlh = nlmsg_put(skb, c->pid, c->seq, c->event, headlen, 0);
2348 if (nlh == NULL)
2349 goto nlmsg_failure;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002350
Thomas Graf7b67c852007-08-22 13:53:52 -07002351 p = nlmsg_data(nlh);
Herbert Xu0603eac2005-06-18 22:54:36 -07002352 if (c->event == XFRM_MSG_DELPOLICY) {
Thomas Grafc0144be2007-08-22 13:55:43 -07002353 struct nlattr *attr;
2354
Thomas Graf7b67c852007-08-22 13:53:52 -07002355 id = nlmsg_data(nlh);
Herbert Xu0603eac2005-06-18 22:54:36 -07002356 memset(id, 0, sizeof(*id));
2357 id->dir = dir;
2358 if (c->data.byid)
2359 id->index = xp->index;
2360 else
2361 memcpy(&id->sel, &xp->selector, sizeof(id->sel));
2362
Thomas Grafc0144be2007-08-22 13:55:43 -07002363 attr = nla_reserve(skb, XFRMA_POLICY, sizeof(*p));
2364 if (attr == NULL)
2365 goto nlmsg_failure;
2366
2367 p = nla_data(attr);
Herbert Xu0603eac2005-06-18 22:54:36 -07002368 }
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002369
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002370 copy_to_user_policy(xp, p, dir);
2371 if (copy_to_user_tmpl(xp, skb) < 0)
2372 goto nlmsg_failure;
Jamal Hadi Salim1459bb32006-11-20 16:51:22 -08002373 if (copy_to_user_policy_type(xp->type, skb) < 0)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07002374 goto nlmsg_failure;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002375
Thomas Graf98250692007-08-22 12:47:26 -07002376 nlmsg_end(skb, nlh);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002377
Thomas Graf082a1ad2007-08-22 13:54:36 -07002378 return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_POLICY, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002379
2380nlmsg_failure:
2381 kfree_skb(skb);
2382 return -1;
2383}
2384
2385static int xfrm_notify_policy_flush(struct km_event *c)
2386{
2387 struct nlmsghdr *nlh;
2388 struct sk_buff *skb;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002389
Thomas Graf7deb2262007-08-22 13:57:39 -07002390 skb = nlmsg_new(userpolicy_type_attrsize(), GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002391 if (skb == NULL)
2392 return -ENOMEM;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002393
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002394 nlh = nlmsg_put(skb, c->pid, c->seq, XFRM_MSG_FLUSHPOLICY, 0, 0);
2395 if (nlh == NULL)
2396 goto nlmsg_failure;
Jamal Hadi Salim0c51f532006-11-27 12:58:20 -08002397 if (copy_to_user_policy_type(c->data.type, skb) < 0)
2398 goto nlmsg_failure;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002399
Thomas Graf98250692007-08-22 12:47:26 -07002400 nlmsg_end(skb, nlh);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002401
Thomas Graf082a1ad2007-08-22 13:54:36 -07002402 return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_POLICY, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002403
2404nlmsg_failure:
2405 kfree_skb(skb);
2406 return -1;
2407}
2408
2409static int xfrm_send_policy_notify(struct xfrm_policy *xp, int dir, struct km_event *c)
2410{
2411
2412 switch (c->event) {
Herbert Xuf60f6b82005-06-18 22:44:37 -07002413 case XFRM_MSG_NEWPOLICY:
2414 case XFRM_MSG_UPDPOLICY:
2415 case XFRM_MSG_DELPOLICY:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002416 return xfrm_notify_policy(xp, dir, c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07002417 case XFRM_MSG_FLUSHPOLICY:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002418 return xfrm_notify_policy_flush(c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07002419 case XFRM_MSG_POLEXPIRE:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002420 return xfrm_exp_policy_notify(xp, dir, c);
2421 default:
2422 printk("xfrm_user: Unknown Policy event %d\n", c->event);
2423 }
2424
2425 return 0;
2426
2427}
2428
Thomas Graf7deb2262007-08-22 13:57:39 -07002429static inline size_t xfrm_report_msgsize(void)
2430{
2431 return NLMSG_ALIGN(sizeof(struct xfrm_user_report));
2432}
2433
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002434static int build_report(struct sk_buff *skb, u8 proto,
2435 struct xfrm_selector *sel, xfrm_address_t *addr)
2436{
2437 struct xfrm_user_report *ur;
2438 struct nlmsghdr *nlh;
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002439
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002440 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_REPORT, sizeof(*ur), 0);
2441 if (nlh == NULL)
2442 return -EMSGSIZE;
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002443
Thomas Graf7b67c852007-08-22 13:53:52 -07002444 ur = nlmsg_data(nlh);
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002445 ur->proto = proto;
2446 memcpy(&ur->sel, sel, sizeof(ur->sel));
2447
2448 if (addr)
Thomas Grafc0144be2007-08-22 13:55:43 -07002449 NLA_PUT(skb, XFRMA_COADDR, sizeof(*addr), addr);
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002450
Thomas Graf98250692007-08-22 12:47:26 -07002451 return nlmsg_end(skb, nlh);
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002452
Thomas Grafc0144be2007-08-22 13:55:43 -07002453nla_put_failure:
Thomas Graf98250692007-08-22 12:47:26 -07002454 nlmsg_cancel(skb, nlh);
2455 return -EMSGSIZE;
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002456}
2457
2458static int xfrm_send_report(u8 proto, struct xfrm_selector *sel,
2459 xfrm_address_t *addr)
2460{
2461 struct sk_buff *skb;
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002462
Thomas Graf7deb2262007-08-22 13:57:39 -07002463 skb = nlmsg_new(xfrm_report_msgsize(), GFP_ATOMIC);
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002464 if (skb == NULL)
2465 return -ENOMEM;
2466
2467 if (build_report(skb, proto, sel, addr) < 0)
2468 BUG();
2469
Thomas Graf082a1ad2007-08-22 13:54:36 -07002470 return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_REPORT, GFP_ATOMIC);
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002471}
2472
Linus Torvalds1da177e2005-04-16 15:20:36 -07002473static struct xfrm_mgr netlink_mgr = {
2474 .id = "netlink",
2475 .notify = xfrm_send_state_notify,
2476 .acquire = xfrm_send_acquire,
2477 .compile_policy = xfrm_compile_policy,
2478 .notify_policy = xfrm_send_policy_notify,
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002479 .report = xfrm_send_report,
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002480 .migrate = xfrm_send_migrate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002481};
2482
2483static int __init xfrm_user_init(void)
2484{
Patrick McHardybe336902006-03-20 22:40:54 -08002485 struct sock *nlsk;
2486
Masahide NAKAMURA654b32c2006-08-23 19:12:56 -07002487 printk(KERN_INFO "Initializing XFRM netlink socket\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002488
Eric W. Biedermanb4b51022007-09-12 13:05:38 +02002489 nlsk = netlink_kernel_create(&init_net, NETLINK_XFRM, XFRMNLGRP_MAX,
Patrick McHardyaf65bdf2007-04-20 14:14:21 -07002490 xfrm_netlink_rcv, NULL, THIS_MODULE);
Patrick McHardybe336902006-03-20 22:40:54 -08002491 if (nlsk == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002492 return -ENOMEM;
Patrick McHardybe336902006-03-20 22:40:54 -08002493 rcu_assign_pointer(xfrm_nl, nlsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002494
2495 xfrm_register_km(&netlink_mgr);
2496
2497 return 0;
2498}
2499
2500static void __exit xfrm_user_exit(void)
2501{
Patrick McHardybe336902006-03-20 22:40:54 -08002502 struct sock *nlsk = xfrm_nl;
2503
Linus Torvalds1da177e2005-04-16 15:20:36 -07002504 xfrm_unregister_km(&netlink_mgr);
Patrick McHardybe336902006-03-20 22:40:54 -08002505 rcu_assign_pointer(xfrm_nl, NULL);
2506 synchronize_rcu();
Denis V. Lunevb7c6ba62008-01-28 14:41:19 -08002507 netlink_kernel_release(nlsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002508}
2509
2510module_init(xfrm_user_init);
2511module_exit(xfrm_user_exit);
2512MODULE_LICENSE("GPL");
Harald Welte4fdb3bb2005-08-09 19:40:55 -07002513MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK, NETLINK_XFRM);
Jamal Hadi Salimf8cd5482006-03-20 19:15:11 -08002514