blob: 8b5b01dfb77ae9ef25852736356e5525106cbd1c [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{
Alexey Dobriyan673c09b2008-11-25 17:15:16 -0800323 struct xfrm_state *x = xfrm_state_alloc(&init_net);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 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;
Alexey Dobriyan221df1e2008-11-25 17:30:50 -0800443 x = xfrm_state_lookup(&init_net, &p->daddr, p->spi, p->proto, p->family);
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700444 } 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;
Alexey Dobriyan221df1e2008-11-25 17:30:50 -0800454 x = xfrm_state_lookup_byaddr(&init_net, &p->daddr, saddr,
455 p->proto, p->family);
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700456 }
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
Alexey Dobriyan284fa7d2008-11-25 17:32:14 -0800634 (void) xfrm_state_walk(&init_net, 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{
Alexey Dobriyana6483b72008-11-25 17:38:20 -0800706 struct net *net = sock_net(skb->sk);
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700707 struct sk_buff *r_skb;
Thomas Graf7b67c852007-08-22 13:53:52 -0700708 u32 *flags = nlmsg_data(nlh);
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700709 u32 spid = NETLINK_CB(skb).pid;
710 u32 seq = nlh->nlmsg_seq;
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700711
Thomas Graf7deb2262007-08-22 13:57:39 -0700712 r_skb = nlmsg_new(xfrm_spdinfo_msgsize(), GFP_ATOMIC);
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700713 if (r_skb == NULL)
714 return -ENOMEM;
715
716 if (build_spdinfo(r_skb, spid, seq, *flags) < 0)
717 BUG();
718
Alexey Dobriyana6483b72008-11-25 17:38:20 -0800719 return nlmsg_unicast(net->xfrm.nlsk, r_skb, spid);
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700720}
721
Thomas Graf7deb2262007-08-22 13:57:39 -0700722static inline size_t xfrm_sadinfo_msgsize(void)
723{
724 return NLMSG_ALIGN(4)
725 + nla_total_size(sizeof(struct xfrmu_sadhinfo))
726 + nla_total_size(4); /* XFRMA_SAD_CNT */
727}
728
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700729static int build_sadinfo(struct sk_buff *skb, u32 pid, u32 seq, u32 flags)
730{
Jamal Hadi Salimaf11e312007-05-04 12:55:13 -0700731 struct xfrmk_sadinfo si;
732 struct xfrmu_sadhinfo sh;
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700733 struct nlmsghdr *nlh;
734 u32 *f;
735
736 nlh = nlmsg_put(skb, pid, seq, XFRM_MSG_NEWSADINFO, sizeof(u32), 0);
737 if (nlh == NULL) /* shouldnt really happen ... */
738 return -EMSGSIZE;
739
740 f = nlmsg_data(nlh);
741 *f = flags;
742 xfrm_sad_getinfo(&si);
743
Jamal Hadi Salimaf11e312007-05-04 12:55:13 -0700744 sh.sadhmcnt = si.sadhmcnt;
745 sh.sadhcnt = si.sadhcnt;
746
747 NLA_PUT_U32(skb, XFRMA_SAD_CNT, si.sadcnt);
748 NLA_PUT(skb, XFRMA_SAD_HINFO, sizeof(sh), &sh);
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700749
750 return nlmsg_end(skb, nlh);
751
752nla_put_failure:
753 nlmsg_cancel(skb, nlh);
754 return -EMSGSIZE;
755}
756
757static int xfrm_get_sadinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -0700758 struct nlattr **attrs)
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700759{
Alexey Dobriyana6483b72008-11-25 17:38:20 -0800760 struct net *net = sock_net(skb->sk);
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700761 struct sk_buff *r_skb;
Thomas Graf7b67c852007-08-22 13:53:52 -0700762 u32 *flags = nlmsg_data(nlh);
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700763 u32 spid = NETLINK_CB(skb).pid;
764 u32 seq = nlh->nlmsg_seq;
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700765
Thomas Graf7deb2262007-08-22 13:57:39 -0700766 r_skb = nlmsg_new(xfrm_sadinfo_msgsize(), GFP_ATOMIC);
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700767 if (r_skb == NULL)
768 return -ENOMEM;
769
770 if (build_sadinfo(r_skb, spid, seq, *flags) < 0)
771 BUG();
772
Alexey Dobriyana6483b72008-11-25 17:38:20 -0800773 return nlmsg_unicast(net->xfrm.nlsk, r_skb, spid);
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700774}
775
Christoph Hellwig22e70052007-01-02 15:22:30 -0800776static int xfrm_get_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -0700777 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778{
Alexey Dobriyana6483b72008-11-25 17:38:20 -0800779 struct net *net = &init_net;
Thomas Graf7b67c852007-08-22 13:53:52 -0700780 struct xfrm_usersa_id *p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 struct xfrm_state *x;
782 struct sk_buff *resp_skb;
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700783 int err = -ESRCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784
Thomas Graf35a7aa02007-08-22 14:00:40 -0700785 x = xfrm_user_state_lookup(p, attrs, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 if (x == NULL)
787 goto out_noput;
788
789 resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
790 if (IS_ERR(resp_skb)) {
791 err = PTR_ERR(resp_skb);
792 } else {
Alexey Dobriyana6483b72008-11-25 17:38:20 -0800793 err = nlmsg_unicast(net->xfrm.nlsk, resp_skb, NETLINK_CB(skb).pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 }
795 xfrm_state_put(x);
796out_noput:
797 return err;
798}
799
800static int verify_userspi_info(struct xfrm_userspi_info *p)
801{
802 switch (p->info.id.proto) {
803 case IPPROTO_AH:
804 case IPPROTO_ESP:
805 break;
806
807 case IPPROTO_COMP:
808 /* IPCOMP spi is 16-bits. */
809 if (p->max >= 0x10000)
810 return -EINVAL;
811 break;
812
813 default:
814 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700815 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816
817 if (p->min > p->max)
818 return -EINVAL;
819
820 return 0;
821}
822
Christoph Hellwig22e70052007-01-02 15:22:30 -0800823static int xfrm_alloc_userspi(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -0700824 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825{
Alexey Dobriyana6483b72008-11-25 17:38:20 -0800826 struct net *net = &init_net;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 struct xfrm_state *x;
828 struct xfrm_userspi_info *p;
829 struct sk_buff *resp_skb;
830 xfrm_address_t *daddr;
831 int family;
832 int err;
833
Thomas Graf7b67c852007-08-22 13:53:52 -0700834 p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 err = verify_userspi_info(p);
836 if (err)
837 goto out_noput;
838
839 family = p->info.family;
840 daddr = &p->info.id.daddr;
841
842 x = NULL;
843 if (p->info.seq) {
Alexey Dobriyana6483b72008-11-25 17:38:20 -0800844 x = xfrm_find_acq_byseq(net, p->info.seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 if (x && xfrm_addr_cmp(&x->id.daddr, daddr, family)) {
846 xfrm_state_put(x);
847 x = NULL;
848 }
849 }
850
851 if (!x)
Alexey Dobriyana6483b72008-11-25 17:38:20 -0800852 x = xfrm_find_acq(net, p->info.mode, p->info.reqid,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 p->info.id.proto, daddr,
854 &p->info.saddr, 1,
855 family);
856 err = -ENOENT;
857 if (x == NULL)
858 goto out_noput;
859
Herbert Xu658b2192007-10-09 13:29:52 -0700860 err = xfrm_alloc_spi(x, p->min, p->max);
861 if (err)
862 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863
Herbert Xu658b2192007-10-09 13:29:52 -0700864 resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 if (IS_ERR(resp_skb)) {
866 err = PTR_ERR(resp_skb);
867 goto out;
868 }
869
Alexey Dobriyana6483b72008-11-25 17:38:20 -0800870 err = nlmsg_unicast(net->xfrm.nlsk, resp_skb, NETLINK_CB(skb).pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871
872out:
873 xfrm_state_put(x);
874out_noput:
875 return err;
876}
877
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -0800878static int verify_policy_dir(u8 dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879{
880 switch (dir) {
881 case XFRM_POLICY_IN:
882 case XFRM_POLICY_OUT:
883 case XFRM_POLICY_FWD:
884 break;
885
886 default:
887 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700888 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889
890 return 0;
891}
892
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -0800893static int verify_policy_type(u8 type)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -0700894{
895 switch (type) {
896 case XFRM_POLICY_TYPE_MAIN:
897#ifdef CONFIG_XFRM_SUB_POLICY
898 case XFRM_POLICY_TYPE_SUB:
899#endif
900 break;
901
902 default:
903 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700904 }
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -0700905
906 return 0;
907}
908
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909static int verify_newpolicy_info(struct xfrm_userpolicy_info *p)
910{
911 switch (p->share) {
912 case XFRM_SHARE_ANY:
913 case XFRM_SHARE_SESSION:
914 case XFRM_SHARE_USER:
915 case XFRM_SHARE_UNIQUE:
916 break;
917
918 default:
919 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700920 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921
922 switch (p->action) {
923 case XFRM_POLICY_ALLOW:
924 case XFRM_POLICY_BLOCK:
925 break;
926
927 default:
928 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700929 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930
931 switch (p->sel.family) {
932 case AF_INET:
933 break;
934
935 case AF_INET6:
936#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
937 break;
938#else
939 return -EAFNOSUPPORT;
940#endif
941
942 default:
943 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700944 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945
946 return verify_policy_dir(p->dir);
947}
948
Thomas Graf5424f322007-08-22 14:01:33 -0700949static int copy_from_user_sec_ctx(struct xfrm_policy *pol, struct nlattr **attrs)
Trent Jaegerdf718372005-12-13 23:12:27 -0800950{
Thomas Graf5424f322007-08-22 14:01:33 -0700951 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
Trent Jaegerdf718372005-12-13 23:12:27 -0800952 struct xfrm_user_sec_ctx *uctx;
953
954 if (!rt)
955 return 0;
956
Thomas Graf5424f322007-08-22 14:01:33 -0700957 uctx = nla_data(rt);
Paul Moore03e1ad72008-04-12 19:07:52 -0700958 return security_xfrm_policy_alloc(&pol->security, uctx);
Trent Jaegerdf718372005-12-13 23:12:27 -0800959}
960
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961static void copy_templates(struct xfrm_policy *xp, struct xfrm_user_tmpl *ut,
962 int nr)
963{
964 int i;
965
966 xp->xfrm_nr = nr;
967 for (i = 0; i < nr; i++, ut++) {
968 struct xfrm_tmpl *t = &xp->xfrm_vec[i];
969
970 memcpy(&t->id, &ut->id, sizeof(struct xfrm_id));
971 memcpy(&t->saddr, &ut->saddr,
972 sizeof(xfrm_address_t));
973 t->reqid = ut->reqid;
974 t->mode = ut->mode;
975 t->share = ut->share;
976 t->optional = ut->optional;
977 t->aalgos = ut->aalgos;
978 t->ealgos = ut->ealgos;
979 t->calgos = ut->calgos;
Herbert Xuc5d18e92008-04-22 00:46:42 -0700980 /* If all masks are ~0, then we allow all algorithms. */
981 t->allalgs = !~(t->aalgos & t->ealgos & t->calgos);
Miika Komu8511d012006-11-30 16:40:51 -0800982 t->encap_family = ut->family;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 }
984}
985
David S. Millerb4ad86bf2006-12-03 19:19:26 -0800986static int validate_tmpl(int nr, struct xfrm_user_tmpl *ut, u16 family)
987{
988 int i;
989
990 if (nr > XFRM_MAX_DEPTH)
991 return -EINVAL;
992
993 for (i = 0; i < nr; i++) {
994 /* We never validated the ut->family value, so many
995 * applications simply leave it at zero. The check was
996 * never made and ut->family was ignored because all
997 * templates could be assumed to have the same family as
998 * the policy itself. Now that we will have ipv4-in-ipv6
999 * and ipv6-in-ipv4 tunnels, this is no longer true.
1000 */
1001 if (!ut[i].family)
1002 ut[i].family = family;
1003
1004 switch (ut[i].family) {
1005 case AF_INET:
1006 break;
1007#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1008 case AF_INET6:
1009 break;
1010#endif
1011 default:
1012 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001013 }
David S. Millerb4ad86bf2006-12-03 19:19:26 -08001014 }
1015
1016 return 0;
1017}
1018
Thomas Graf5424f322007-08-22 14:01:33 -07001019static int copy_from_user_tmpl(struct xfrm_policy *pol, struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020{
Thomas Graf5424f322007-08-22 14:01:33 -07001021 struct nlattr *rt = attrs[XFRMA_TMPL];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022
1023 if (!rt) {
1024 pol->xfrm_nr = 0;
1025 } else {
Thomas Graf5424f322007-08-22 14:01:33 -07001026 struct xfrm_user_tmpl *utmpl = nla_data(rt);
1027 int nr = nla_len(rt) / sizeof(*utmpl);
David S. Millerb4ad86bf2006-12-03 19:19:26 -08001028 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029
David S. Millerb4ad86bf2006-12-03 19:19:26 -08001030 err = validate_tmpl(nr, utmpl, pol->family);
1031 if (err)
1032 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033
Thomas Graf5424f322007-08-22 14:01:33 -07001034 copy_templates(pol, utmpl, nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 }
1036 return 0;
1037}
1038
Thomas Graf5424f322007-08-22 14:01:33 -07001039static int copy_from_user_policy_type(u8 *tp, struct nlattr **attrs)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001040{
Thomas Graf5424f322007-08-22 14:01:33 -07001041 struct nlattr *rt = attrs[XFRMA_POLICY_TYPE];
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001042 struct xfrm_userpolicy_type *upt;
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001043 u8 type = XFRM_POLICY_TYPE_MAIN;
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001044 int err;
1045
1046 if (rt) {
Thomas Graf5424f322007-08-22 14:01:33 -07001047 upt = nla_data(rt);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001048 type = upt->type;
1049 }
1050
1051 err = verify_policy_type(type);
1052 if (err)
1053 return err;
1054
1055 *tp = type;
1056 return 0;
1057}
1058
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059static void copy_from_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p)
1060{
1061 xp->priority = p->priority;
1062 xp->index = p->index;
1063 memcpy(&xp->selector, &p->sel, sizeof(xp->selector));
1064 memcpy(&xp->lft, &p->lft, sizeof(xp->lft));
1065 xp->action = p->action;
1066 xp->flags = p->flags;
1067 xp->family = p->sel.family;
1068 /* XXX xp->share = p->share; */
1069}
1070
1071static void copy_to_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p, int dir)
1072{
1073 memcpy(&p->sel, &xp->selector, sizeof(p->sel));
1074 memcpy(&p->lft, &xp->lft, sizeof(p->lft));
1075 memcpy(&p->curlft, &xp->curlft, sizeof(p->curlft));
1076 p->priority = xp->priority;
1077 p->index = xp->index;
1078 p->sel.family = xp->family;
1079 p->dir = dir;
1080 p->action = xp->action;
1081 p->flags = xp->flags;
1082 p->share = XFRM_SHARE_ANY; /* XXX xp->share */
1083}
1084
Thomas Graf5424f322007-08-22 14:01:33 -07001085static struct xfrm_policy *xfrm_policy_construct(struct xfrm_userpolicy_info *p, struct nlattr **attrs, int *errp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086{
Alexey Dobriyan0331b1f2008-11-25 17:21:45 -08001087 struct xfrm_policy *xp = xfrm_policy_alloc(&init_net, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 int err;
1089
1090 if (!xp) {
1091 *errp = -ENOMEM;
1092 return NULL;
1093 }
1094
1095 copy_from_user_policy(xp, p);
Trent Jaegerdf718372005-12-13 23:12:27 -08001096
Thomas Graf35a7aa02007-08-22 14:00:40 -07001097 err = copy_from_user_policy_type(&xp->type, attrs);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001098 if (err)
1099 goto error;
1100
Thomas Graf35a7aa02007-08-22 14:00:40 -07001101 if (!(err = copy_from_user_tmpl(xp, attrs)))
1102 err = copy_from_user_sec_ctx(xp, attrs);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001103 if (err)
1104 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105
1106 return xp;
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001107 error:
1108 *errp = err;
Herbert Xu12a169e2008-10-01 07:03:24 -07001109 xp->walk.dead = 1;
WANG Cong64c31b32008-01-07 22:34:29 -08001110 xfrm_policy_destroy(xp);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001111 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112}
1113
Christoph Hellwig22e70052007-01-02 15:22:30 -08001114static int xfrm_add_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001115 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116{
Thomas Graf7b67c852007-08-22 13:53:52 -07001117 struct xfrm_userpolicy_info *p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 struct xfrm_policy *xp;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001119 struct km_event c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 int err;
1121 int excl;
Eric Paris25323862008-04-18 10:09:25 -04001122 uid_t loginuid = NETLINK_CB(skb).loginuid;
1123 u32 sessionid = NETLINK_CB(skb).sessionid;
1124 u32 sid = NETLINK_CB(skb).sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125
1126 err = verify_newpolicy_info(p);
1127 if (err)
1128 return err;
Thomas Graf35a7aa02007-08-22 14:00:40 -07001129 err = verify_sec_ctx_len(attrs);
Trent Jaegerdf718372005-12-13 23:12:27 -08001130 if (err)
1131 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132
Thomas Graf35a7aa02007-08-22 14:00:40 -07001133 xp = xfrm_policy_construct(p, attrs, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 if (!xp)
1135 return err;
1136
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001137 /* shouldnt excl be based on nlh flags??
1138 * Aha! this is anti-netlink really i.e more pfkey derived
1139 * in netlink excl is a flag and you wouldnt need
1140 * a type XFRM_MSG_UPDPOLICY - JHS */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141 excl = nlh->nlmsg_type == XFRM_MSG_NEWPOLICY;
1142 err = xfrm_policy_insert(p->dir, xp, excl);
Eric Paris25323862008-04-18 10:09:25 -04001143 xfrm_audit_policy_add(xp, err ? 0 : 1, loginuid, sessionid, sid);
Joy Latten161a09e2006-11-27 13:11:54 -06001144
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 if (err) {
Paul Moore03e1ad72008-04-12 19:07:52 -07001146 security_xfrm_policy_free(xp->security);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 kfree(xp);
1148 return err;
1149 }
1150
Herbert Xuf60f6b82005-06-18 22:44:37 -07001151 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001152 c.seq = nlh->nlmsg_seq;
1153 c.pid = nlh->nlmsg_pid;
1154 km_policy_notify(xp, p->dir, &c);
1155
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 xfrm_pol_put(xp);
1157
1158 return 0;
1159}
1160
1161static int copy_to_user_tmpl(struct xfrm_policy *xp, struct sk_buff *skb)
1162{
1163 struct xfrm_user_tmpl vec[XFRM_MAX_DEPTH];
1164 int i;
1165
1166 if (xp->xfrm_nr == 0)
1167 return 0;
1168
1169 for (i = 0; i < xp->xfrm_nr; i++) {
1170 struct xfrm_user_tmpl *up = &vec[i];
1171 struct xfrm_tmpl *kp = &xp->xfrm_vec[i];
1172
1173 memcpy(&up->id, &kp->id, sizeof(up->id));
Miika Komu8511d012006-11-30 16:40:51 -08001174 up->family = kp->encap_family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 memcpy(&up->saddr, &kp->saddr, sizeof(up->saddr));
1176 up->reqid = kp->reqid;
1177 up->mode = kp->mode;
1178 up->share = kp->share;
1179 up->optional = kp->optional;
1180 up->aalgos = kp->aalgos;
1181 up->ealgos = kp->ealgos;
1182 up->calgos = kp->calgos;
1183 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184
Thomas Grafc0144be2007-08-22 13:55:43 -07001185 return nla_put(skb, XFRMA_TMPL,
1186 sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr, vec);
Trent Jaegerdf718372005-12-13 23:12:27 -08001187}
1188
Serge Hallyn0d681622006-07-24 23:30:44 -07001189static inline int copy_to_user_state_sec_ctx(struct xfrm_state *x, struct sk_buff *skb)
1190{
1191 if (x->security) {
1192 return copy_sec_ctx(x->security, skb);
1193 }
1194 return 0;
1195}
1196
1197static inline int copy_to_user_sec_ctx(struct xfrm_policy *xp, struct sk_buff *skb)
1198{
1199 if (xp->security) {
1200 return copy_sec_ctx(xp->security, skb);
1201 }
1202 return 0;
1203}
Thomas Grafcfbfd452007-08-22 13:57:04 -07001204static inline size_t userpolicy_type_attrsize(void)
1205{
1206#ifdef CONFIG_XFRM_SUB_POLICY
1207 return nla_total_size(sizeof(struct xfrm_userpolicy_type));
1208#else
1209 return 0;
1210#endif
1211}
Serge Hallyn0d681622006-07-24 23:30:44 -07001212
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001213#ifdef CONFIG_XFRM_SUB_POLICY
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001214static int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001215{
Thomas Grafc0144be2007-08-22 13:55:43 -07001216 struct xfrm_userpolicy_type upt = {
1217 .type = type,
1218 };
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001219
Thomas Grafc0144be2007-08-22 13:55:43 -07001220 return nla_put(skb, XFRMA_POLICY_TYPE, sizeof(upt), &upt);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001221}
1222
1223#else
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001224static inline int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001225{
1226 return 0;
1227}
1228#endif
1229
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230static int dump_one_policy(struct xfrm_policy *xp, int dir, int count, void *ptr)
1231{
1232 struct xfrm_dump_info *sp = ptr;
1233 struct xfrm_userpolicy_info *p;
1234 struct sk_buff *in_skb = sp->in_skb;
1235 struct sk_buff *skb = sp->out_skb;
1236 struct nlmsghdr *nlh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237
Thomas Graf79b8b7f2007-08-22 12:46:53 -07001238 nlh = nlmsg_put(skb, NETLINK_CB(in_skb).pid, sp->nlmsg_seq,
1239 XFRM_MSG_NEWPOLICY, sizeof(*p), sp->nlmsg_flags);
1240 if (nlh == NULL)
1241 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242
Thomas Graf7b67c852007-08-22 13:53:52 -07001243 p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 copy_to_user_policy(xp, p, dir);
1245 if (copy_to_user_tmpl(xp, skb) < 0)
1246 goto nlmsg_failure;
Trent Jaegerdf718372005-12-13 23:12:27 -08001247 if (copy_to_user_sec_ctx(xp, skb))
1248 goto nlmsg_failure;
Jamal Hadi Salim1459bb32006-11-20 16:51:22 -08001249 if (copy_to_user_policy_type(xp->type, skb) < 0)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001250 goto nlmsg_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251
Thomas Graf98250692007-08-22 12:47:26 -07001252 nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253 return 0;
1254
1255nlmsg_failure:
Thomas Graf98250692007-08-22 12:47:26 -07001256 nlmsg_cancel(skb, nlh);
1257 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258}
1259
Timo Teras4c563f72008-02-28 21:31:08 -08001260static int xfrm_dump_policy_done(struct netlink_callback *cb)
1261{
1262 struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *) &cb->args[1];
1263
1264 xfrm_policy_walk_done(walk);
1265 return 0;
1266}
1267
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268static int xfrm_dump_policy(struct sk_buff *skb, struct netlink_callback *cb)
1269{
Timo Teras4c563f72008-02-28 21:31:08 -08001270 struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *) &cb->args[1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 struct xfrm_dump_info info;
1272
Timo Teras4c563f72008-02-28 21:31:08 -08001273 BUILD_BUG_ON(sizeof(struct xfrm_policy_walk) >
1274 sizeof(cb->args) - sizeof(cb->args[0]));
1275
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 info.in_skb = cb->skb;
1277 info.out_skb = skb;
1278 info.nlmsg_seq = cb->nlh->nlmsg_seq;
1279 info.nlmsg_flags = NLM_F_MULTI;
Timo Teras4c563f72008-02-28 21:31:08 -08001280
1281 if (!cb->args[0]) {
1282 cb->args[0] = 1;
1283 xfrm_policy_walk_init(walk, XFRM_POLICY_TYPE_ANY);
1284 }
1285
Alexey Dobriyancdcbca72008-11-25 17:34:49 -08001286 (void) xfrm_policy_walk(&init_net, walk, dump_one_policy, &info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287
1288 return skb->len;
1289}
1290
1291static struct sk_buff *xfrm_policy_netlink(struct sk_buff *in_skb,
1292 struct xfrm_policy *xp,
1293 int dir, u32 seq)
1294{
1295 struct xfrm_dump_info info;
1296 struct sk_buff *skb;
1297
Thomas Graf7deb2262007-08-22 13:57:39 -07001298 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299 if (!skb)
1300 return ERR_PTR(-ENOMEM);
1301
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 info.in_skb = in_skb;
1303 info.out_skb = skb;
1304 info.nlmsg_seq = seq;
1305 info.nlmsg_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306
1307 if (dump_one_policy(xp, dir, 0, &info) < 0) {
1308 kfree_skb(skb);
1309 return NULL;
1310 }
1311
1312 return skb;
1313}
1314
Christoph Hellwig22e70052007-01-02 15:22:30 -08001315static int xfrm_get_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001316 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317{
Alexey Dobriyana6483b72008-11-25 17:38:20 -08001318 struct net *net = &init_net;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 struct xfrm_policy *xp;
1320 struct xfrm_userpolicy_id *p;
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001321 u8 type = XFRM_POLICY_TYPE_MAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322 int err;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001323 struct km_event c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324 int delete;
1325
Thomas Graf7b67c852007-08-22 13:53:52 -07001326 p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327 delete = nlh->nlmsg_type == XFRM_MSG_DELPOLICY;
1328
Thomas Graf35a7aa02007-08-22 14:00:40 -07001329 err = copy_from_user_policy_type(&type, attrs);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001330 if (err)
1331 return err;
1332
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333 err = verify_policy_dir(p->dir);
1334 if (err)
1335 return err;
1336
1337 if (p->index)
Alexey Dobriyana6483b72008-11-25 17:38:20 -08001338 xp = xfrm_policy_byid(net, type, p->dir, p->index, delete, &err);
Trent Jaegerdf718372005-12-13 23:12:27 -08001339 else {
Thomas Graf5424f322007-08-22 14:01:33 -07001340 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
Paul Moore03e1ad72008-04-12 19:07:52 -07001341 struct xfrm_sec_ctx *ctx;
Trent Jaegerdf718372005-12-13 23:12:27 -08001342
Thomas Graf35a7aa02007-08-22 14:00:40 -07001343 err = verify_sec_ctx_len(attrs);
Trent Jaegerdf718372005-12-13 23:12:27 -08001344 if (err)
1345 return err;
1346
Denis V. Lunev2c8dd112008-04-14 14:47:48 -07001347 ctx = NULL;
Trent Jaegerdf718372005-12-13 23:12:27 -08001348 if (rt) {
Thomas Graf5424f322007-08-22 14:01:33 -07001349 struct xfrm_user_sec_ctx *uctx = nla_data(rt);
Trent Jaegerdf718372005-12-13 23:12:27 -08001350
Paul Moore03e1ad72008-04-12 19:07:52 -07001351 err = security_xfrm_policy_alloc(&ctx, uctx);
1352 if (err)
Trent Jaegerdf718372005-12-13 23:12:27 -08001353 return err;
Denis V. Lunev2c8dd112008-04-14 14:47:48 -07001354 }
Alexey Dobriyana6483b72008-11-25 17:38:20 -08001355 xp = xfrm_policy_bysel_ctx(net, type, p->dir, &p->sel, ctx,
Eric Parisef41aaa2007-03-07 15:37:58 -08001356 delete, &err);
Paul Moore03e1ad72008-04-12 19:07:52 -07001357 security_xfrm_policy_free(ctx);
Trent Jaegerdf718372005-12-13 23:12:27 -08001358 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359 if (xp == NULL)
1360 return -ENOENT;
1361
1362 if (!delete) {
1363 struct sk_buff *resp_skb;
1364
1365 resp_skb = xfrm_policy_netlink(skb, xp, p->dir, nlh->nlmsg_seq);
1366 if (IS_ERR(resp_skb)) {
1367 err = PTR_ERR(resp_skb);
1368 } else {
Alexey Dobriyana6483b72008-11-25 17:38:20 -08001369 err = nlmsg_unicast(net->xfrm.nlsk, resp_skb,
Thomas Graf082a1ad2007-08-22 13:54:36 -07001370 NETLINK_CB(skb).pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 }
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001372 } else {
Eric Paris25323862008-04-18 10:09:25 -04001373 uid_t loginuid = NETLINK_CB(skb).loginuid;
1374 u32 sessionid = NETLINK_CB(skb).sessionid;
1375 u32 sid = NETLINK_CB(skb).sid;
1376
1377 xfrm_audit_policy_delete(xp, err ? 0 : 1, loginuid, sessionid,
1378 sid);
David S. Miller13fcfbb02007-02-12 13:53:54 -08001379
1380 if (err != 0)
Catherine Zhangc8c05a82006-06-08 23:39:49 -07001381 goto out;
David S. Miller13fcfbb02007-02-12 13:53:54 -08001382
Herbert Xue7443892005-06-18 22:44:18 -07001383 c.data.byid = p->index;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001384 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001385 c.seq = nlh->nlmsg_seq;
1386 c.pid = nlh->nlmsg_pid;
1387 km_policy_notify(xp, p->dir, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388 }
1389
Catherine Zhangc8c05a82006-06-08 23:39:49 -07001390out:
Eric Parisef41aaa2007-03-07 15:37:58 -08001391 xfrm_pol_put(xp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392 return err;
1393}
1394
Christoph Hellwig22e70052007-01-02 15:22:30 -08001395static int xfrm_flush_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001396 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397{
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001398 struct km_event c;
Thomas Graf7b67c852007-08-22 13:53:52 -07001399 struct xfrm_usersa_flush *p = nlmsg_data(nlh);
Joy Latten161a09e2006-11-27 13:11:54 -06001400 struct xfrm_audit audit_info;
Joy Latten4aa2e622007-06-04 19:05:57 -04001401 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402
Joy Latten161a09e2006-11-27 13:11:54 -06001403 audit_info.loginuid = NETLINK_CB(skb).loginuid;
Eric Paris25323862008-04-18 10:09:25 -04001404 audit_info.sessionid = NETLINK_CB(skb).sessionid;
Joy Latten161a09e2006-11-27 13:11:54 -06001405 audit_info.secid = NETLINK_CB(skb).sid;
Alexey Dobriyan0e602452008-11-25 17:30:18 -08001406 err = xfrm_state_flush(&init_net, p->proto, &audit_info);
Joy Latten4aa2e622007-06-04 19:05:57 -04001407 if (err)
1408 return err;
Herbert Xubf08867f92005-06-18 22:44:00 -07001409 c.data.proto = p->proto;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001410 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001411 c.seq = nlh->nlmsg_seq;
1412 c.pid = nlh->nlmsg_pid;
1413 km_state_notify(NULL, &c);
1414
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415 return 0;
1416}
1417
Thomas Graf7deb2262007-08-22 13:57:39 -07001418static inline size_t xfrm_aevent_msgsize(void)
1419{
1420 return NLMSG_ALIGN(sizeof(struct xfrm_aevent_id))
1421 + nla_total_size(sizeof(struct xfrm_replay_state))
1422 + nla_total_size(sizeof(struct xfrm_lifetime_cur))
1423 + nla_total_size(4) /* XFRM_AE_RTHR */
1424 + nla_total_size(4); /* XFRM_AE_ETHR */
1425}
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001426
1427static int build_aevent(struct sk_buff *skb, struct xfrm_state *x, struct km_event *c)
1428{
1429 struct xfrm_aevent_id *id;
1430 struct nlmsghdr *nlh;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001431
Thomas Graf79b8b7f2007-08-22 12:46:53 -07001432 nlh = nlmsg_put(skb, c->pid, c->seq, XFRM_MSG_NEWAE, sizeof(*id), 0);
1433 if (nlh == NULL)
1434 return -EMSGSIZE;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001435
Thomas Graf7b67c852007-08-22 13:53:52 -07001436 id = nlmsg_data(nlh);
Jamal Hadi Salim2b5f6dc2006-12-02 22:22:25 -08001437 memcpy(&id->sa_id.daddr, &x->id.daddr,sizeof(x->id.daddr));
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001438 id->sa_id.spi = x->id.spi;
1439 id->sa_id.family = x->props.family;
1440 id->sa_id.proto = x->id.proto;
Jamal Hadi Salim2b5f6dc2006-12-02 22:22:25 -08001441 memcpy(&id->saddr, &x->props.saddr,sizeof(x->props.saddr));
1442 id->reqid = x->props.reqid;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001443 id->flags = c->data.aevent;
1444
Thomas Grafc0144be2007-08-22 13:55:43 -07001445 NLA_PUT(skb, XFRMA_REPLAY_VAL, sizeof(x->replay), &x->replay);
1446 NLA_PUT(skb, XFRMA_LTIME_VAL, sizeof(x->curlft), &x->curlft);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001447
Thomas Grafc0144be2007-08-22 13:55:43 -07001448 if (id->flags & XFRM_AE_RTHR)
1449 NLA_PUT_U32(skb, XFRMA_REPLAY_THRESH, x->replay_maxdiff);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001450
Thomas Grafc0144be2007-08-22 13:55:43 -07001451 if (id->flags & XFRM_AE_ETHR)
1452 NLA_PUT_U32(skb, XFRMA_ETIMER_THRESH,
1453 x->replay_maxage * 10 / HZ);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001454
Thomas Graf98250692007-08-22 12:47:26 -07001455 return nlmsg_end(skb, nlh);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001456
Thomas Grafc0144be2007-08-22 13:55:43 -07001457nla_put_failure:
Thomas Graf98250692007-08-22 12:47:26 -07001458 nlmsg_cancel(skb, nlh);
1459 return -EMSGSIZE;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001460}
1461
Christoph Hellwig22e70052007-01-02 15:22:30 -08001462static int xfrm_get_ae(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001463 struct nlattr **attrs)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001464{
Alexey Dobriyana6483b72008-11-25 17:38:20 -08001465 struct net *net = &init_net;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001466 struct xfrm_state *x;
1467 struct sk_buff *r_skb;
1468 int err;
1469 struct km_event c;
Thomas Graf7b67c852007-08-22 13:53:52 -07001470 struct xfrm_aevent_id *p = nlmsg_data(nlh);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001471 struct xfrm_usersa_id *id = &p->sa_id;
1472
Thomas Graf7deb2262007-08-22 13:57:39 -07001473 r_skb = nlmsg_new(xfrm_aevent_msgsize(), GFP_ATOMIC);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001474 if (r_skb == NULL)
1475 return -ENOMEM;
1476
Alexey Dobriyana6483b72008-11-25 17:38:20 -08001477 x = xfrm_state_lookup(net, &id->daddr, id->spi, id->proto, id->family);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001478 if (x == NULL) {
Patrick McHardyb08d5842007-02-27 09:57:37 -08001479 kfree_skb(r_skb);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001480 return -ESRCH;
1481 }
1482
1483 /*
1484 * XXX: is this lock really needed - none of the other
1485 * gets lock (the concern is things getting updated
1486 * while we are still reading) - jhs
1487 */
1488 spin_lock_bh(&x->lock);
1489 c.data.aevent = p->flags;
1490 c.seq = nlh->nlmsg_seq;
1491 c.pid = nlh->nlmsg_pid;
1492
1493 if (build_aevent(r_skb, x, &c) < 0)
1494 BUG();
Alexey Dobriyana6483b72008-11-25 17:38:20 -08001495 err = nlmsg_unicast(net->xfrm.nlsk, r_skb, NETLINK_CB(skb).pid);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001496 spin_unlock_bh(&x->lock);
1497 xfrm_state_put(x);
1498 return err;
1499}
1500
Christoph Hellwig22e70052007-01-02 15:22:30 -08001501static int xfrm_new_ae(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001502 struct nlattr **attrs)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001503{
1504 struct xfrm_state *x;
1505 struct km_event c;
1506 int err = - EINVAL;
Thomas Graf7b67c852007-08-22 13:53:52 -07001507 struct xfrm_aevent_id *p = nlmsg_data(nlh);
Thomas Graf5424f322007-08-22 14:01:33 -07001508 struct nlattr *rp = attrs[XFRMA_REPLAY_VAL];
1509 struct nlattr *lt = attrs[XFRMA_LTIME_VAL];
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001510
1511 if (!lt && !rp)
1512 return err;
1513
1514 /* pedantic mode - thou shalt sayeth replaceth */
1515 if (!(nlh->nlmsg_flags&NLM_F_REPLACE))
1516 return err;
1517
Alexey Dobriyan221df1e2008-11-25 17:30:50 -08001518 x = xfrm_state_lookup(&init_net, &p->sa_id.daddr, p->sa_id.spi, p->sa_id.proto, p->sa_id.family);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001519 if (x == NULL)
1520 return -ESRCH;
1521
1522 if (x->km.state != XFRM_STATE_VALID)
1523 goto out;
1524
1525 spin_lock_bh(&x->lock);
Thomas Graf35a7aa02007-08-22 14:00:40 -07001526 xfrm_update_ae_params(x, attrs);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001527 spin_unlock_bh(&x->lock);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001528
1529 c.event = nlh->nlmsg_type;
1530 c.seq = nlh->nlmsg_seq;
1531 c.pid = nlh->nlmsg_pid;
1532 c.data.aevent = XFRM_AE_CU;
1533 km_state_notify(x, &c);
1534 err = 0;
1535out:
1536 xfrm_state_put(x);
1537 return err;
1538}
1539
Christoph Hellwig22e70052007-01-02 15:22:30 -08001540static int xfrm_flush_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001541 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542{
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001543 struct km_event c;
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001544 u8 type = XFRM_POLICY_TYPE_MAIN;
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001545 int err;
Joy Latten161a09e2006-11-27 13:11:54 -06001546 struct xfrm_audit audit_info;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001547
Thomas Graf35a7aa02007-08-22 14:00:40 -07001548 err = copy_from_user_policy_type(&type, attrs);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001549 if (err)
1550 return err;
1551
Joy Latten161a09e2006-11-27 13:11:54 -06001552 audit_info.loginuid = NETLINK_CB(skb).loginuid;
Eric Paris25323862008-04-18 10:09:25 -04001553 audit_info.sessionid = NETLINK_CB(skb).sessionid;
Joy Latten161a09e2006-11-27 13:11:54 -06001554 audit_info.secid = NETLINK_CB(skb).sid;
Alexey Dobriyan33ffbbd2008-11-25 17:33:32 -08001555 err = xfrm_policy_flush(&init_net, type, &audit_info);
Joy Latten4aa2e622007-06-04 19:05:57 -04001556 if (err)
1557 return err;
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001558 c.data.type = type;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001559 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001560 c.seq = nlh->nlmsg_seq;
1561 c.pid = nlh->nlmsg_pid;
1562 km_policy_notify(NULL, 0, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563 return 0;
1564}
1565
Christoph Hellwig22e70052007-01-02 15:22:30 -08001566static int xfrm_add_pol_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001567 struct nlattr **attrs)
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001568{
1569 struct xfrm_policy *xp;
Thomas Graf7b67c852007-08-22 13:53:52 -07001570 struct xfrm_user_polexpire *up = nlmsg_data(nlh);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001571 struct xfrm_userpolicy_info *p = &up->pol;
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001572 u8 type = XFRM_POLICY_TYPE_MAIN;
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001573 int err = -ENOENT;
1574
Thomas Graf35a7aa02007-08-22 14:00:40 -07001575 err = copy_from_user_policy_type(&type, attrs);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001576 if (err)
1577 return err;
1578
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001579 if (p->index)
Alexey Dobriyan8d1211a2008-11-25 17:34:20 -08001580 xp = xfrm_policy_byid(&init_net, type, p->dir, p->index, 0, &err);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001581 else {
Thomas Graf5424f322007-08-22 14:01:33 -07001582 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
Paul Moore03e1ad72008-04-12 19:07:52 -07001583 struct xfrm_sec_ctx *ctx;
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001584
Thomas Graf35a7aa02007-08-22 14:00:40 -07001585 err = verify_sec_ctx_len(attrs);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001586 if (err)
1587 return err;
1588
Denis V. Lunev2c8dd112008-04-14 14:47:48 -07001589 ctx = NULL;
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001590 if (rt) {
Thomas Graf5424f322007-08-22 14:01:33 -07001591 struct xfrm_user_sec_ctx *uctx = nla_data(rt);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001592
Paul Moore03e1ad72008-04-12 19:07:52 -07001593 err = security_xfrm_policy_alloc(&ctx, uctx);
1594 if (err)
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001595 return err;
Denis V. Lunev2c8dd112008-04-14 14:47:48 -07001596 }
Alexey Dobriyan8d1211a2008-11-25 17:34:20 -08001597 xp = xfrm_policy_bysel_ctx(&init_net, type, p->dir, &p->sel, ctx, 0, &err);
Paul Moore03e1ad72008-04-12 19:07:52 -07001598 security_xfrm_policy_free(ctx);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001599 }
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001600 if (xp == NULL)
Eric Parisef41aaa2007-03-07 15:37:58 -08001601 return -ENOENT;
Paul Moore03e1ad72008-04-12 19:07:52 -07001602
Eric Parisef41aaa2007-03-07 15:37:58 -08001603 read_lock(&xp->lock);
Herbert Xu12a169e2008-10-01 07:03:24 -07001604 if (xp->walk.dead) {
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001605 read_unlock(&xp->lock);
1606 goto out;
1607 }
1608
1609 read_unlock(&xp->lock);
1610 err = 0;
1611 if (up->hard) {
Eric Paris25323862008-04-18 10:09:25 -04001612 uid_t loginuid = NETLINK_CB(skb).loginuid;
1613 uid_t sessionid = NETLINK_CB(skb).sessionid;
1614 u32 sid = NETLINK_CB(skb).sid;
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001615 xfrm_policy_delete(xp, p->dir);
Eric Paris25323862008-04-18 10:09:25 -04001616 xfrm_audit_policy_delete(xp, 1, loginuid, sessionid, sid);
Joy Latten161a09e2006-11-27 13:11:54 -06001617
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001618 } else {
1619 // reset the timers here?
1620 printk("Dont know what to do with soft policy expire\n");
1621 }
1622 km_policy_expired(xp, p->dir, up->hard, current->pid);
1623
1624out:
1625 xfrm_pol_put(xp);
1626 return err;
1627}
1628
Christoph Hellwig22e70052007-01-02 15:22:30 -08001629static int xfrm_add_sa_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001630 struct nlattr **attrs)
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001631{
1632 struct xfrm_state *x;
1633 int err;
Thomas Graf7b67c852007-08-22 13:53:52 -07001634 struct xfrm_user_expire *ue = nlmsg_data(nlh);
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001635 struct xfrm_usersa_info *p = &ue->state;
1636
Alexey Dobriyan221df1e2008-11-25 17:30:50 -08001637 x = xfrm_state_lookup(&init_net, &p->id.daddr, p->id.spi, p->id.proto, p->family);
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001638
David S. Miller3a765aa2007-02-26 14:52:21 -08001639 err = -ENOENT;
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001640 if (x == NULL)
1641 return err;
1642
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001643 spin_lock_bh(&x->lock);
David S. Miller3a765aa2007-02-26 14:52:21 -08001644 err = -EINVAL;
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001645 if (x->km.state != XFRM_STATE_VALID)
1646 goto out;
1647 km_state_expired(x, ue->hard, current->pid);
1648
Joy Latten161a09e2006-11-27 13:11:54 -06001649 if (ue->hard) {
Eric Paris25323862008-04-18 10:09:25 -04001650 uid_t loginuid = NETLINK_CB(skb).loginuid;
1651 uid_t sessionid = NETLINK_CB(skb).sessionid;
1652 u32 sid = NETLINK_CB(skb).sid;
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001653 __xfrm_state_delete(x);
Eric Paris25323862008-04-18 10:09:25 -04001654 xfrm_audit_state_delete(x, 1, loginuid, sessionid, sid);
Joy Latten161a09e2006-11-27 13:11:54 -06001655 }
David S. Miller3a765aa2007-02-26 14:52:21 -08001656 err = 0;
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001657out:
1658 spin_unlock_bh(&x->lock);
1659 xfrm_state_put(x);
1660 return err;
1661}
1662
Christoph Hellwig22e70052007-01-02 15:22:30 -08001663static int xfrm_add_acquire(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001664 struct nlattr **attrs)
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001665{
1666 struct xfrm_policy *xp;
1667 struct xfrm_user_tmpl *ut;
1668 int i;
Thomas Graf5424f322007-08-22 14:01:33 -07001669 struct nlattr *rt = attrs[XFRMA_TMPL];
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001670
Thomas Graf7b67c852007-08-22 13:53:52 -07001671 struct xfrm_user_acquire *ua = nlmsg_data(nlh);
Alexey Dobriyan673c09b2008-11-25 17:15:16 -08001672 struct xfrm_state *x = xfrm_state_alloc(&init_net);
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001673 int err = -ENOMEM;
1674
1675 if (!x)
1676 return err;
1677
1678 err = verify_newpolicy_info(&ua->policy);
1679 if (err) {
1680 printk("BAD policy passed\n");
1681 kfree(x);
1682 return err;
1683 }
1684
1685 /* build an XP */
Thomas Graf5424f322007-08-22 14:01:33 -07001686 xp = xfrm_policy_construct(&ua->policy, attrs, &err);
David S. Millerb4ad86bf2006-12-03 19:19:26 -08001687 if (!xp) {
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001688 kfree(x);
1689 return err;
1690 }
1691
1692 memcpy(&x->id, &ua->id, sizeof(ua->id));
1693 memcpy(&x->props.saddr, &ua->saddr, sizeof(ua->saddr));
1694 memcpy(&x->sel, &ua->sel, sizeof(ua->sel));
1695
Thomas Graf5424f322007-08-22 14:01:33 -07001696 ut = nla_data(rt);
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001697 /* extract the templates and for each call km_key */
1698 for (i = 0; i < xp->xfrm_nr; i++, ut++) {
1699 struct xfrm_tmpl *t = &xp->xfrm_vec[i];
1700 memcpy(&x->id, &t->id, sizeof(x->id));
1701 x->props.mode = t->mode;
1702 x->props.reqid = t->reqid;
1703 x->props.family = ut->family;
1704 t->aalgos = ua->aalgos;
1705 t->ealgos = ua->ealgos;
1706 t->calgos = ua->calgos;
1707 err = km_query(x, t, xp);
1708
1709 }
1710
1711 kfree(x);
1712 kfree(xp);
1713
1714 return 0;
1715}
1716
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001717#ifdef CONFIG_XFRM_MIGRATE
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001718static int copy_from_user_migrate(struct xfrm_migrate *ma,
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07001719 struct xfrm_kmaddress *k,
Thomas Graf5424f322007-08-22 14:01:33 -07001720 struct nlattr **attrs, int *num)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001721{
Thomas Graf5424f322007-08-22 14:01:33 -07001722 struct nlattr *rt = attrs[XFRMA_MIGRATE];
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001723 struct xfrm_user_migrate *um;
1724 int i, num_migrate;
1725
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07001726 if (k != NULL) {
1727 struct xfrm_user_kmaddress *uk;
1728
1729 uk = nla_data(attrs[XFRMA_KMADDRESS]);
1730 memcpy(&k->local, &uk->local, sizeof(k->local));
1731 memcpy(&k->remote, &uk->remote, sizeof(k->remote));
1732 k->family = uk->family;
1733 k->reserved = uk->reserved;
1734 }
1735
Thomas Graf5424f322007-08-22 14:01:33 -07001736 um = nla_data(rt);
1737 num_migrate = nla_len(rt) / sizeof(*um);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001738
1739 if (num_migrate <= 0 || num_migrate > XFRM_MAX_DEPTH)
1740 return -EINVAL;
1741
1742 for (i = 0; i < num_migrate; i++, um++, ma++) {
1743 memcpy(&ma->old_daddr, &um->old_daddr, sizeof(ma->old_daddr));
1744 memcpy(&ma->old_saddr, &um->old_saddr, sizeof(ma->old_saddr));
1745 memcpy(&ma->new_daddr, &um->new_daddr, sizeof(ma->new_daddr));
1746 memcpy(&ma->new_saddr, &um->new_saddr, sizeof(ma->new_saddr));
1747
1748 ma->proto = um->proto;
1749 ma->mode = um->mode;
1750 ma->reqid = um->reqid;
1751
1752 ma->old_family = um->old_family;
1753 ma->new_family = um->new_family;
1754 }
1755
1756 *num = i;
1757 return 0;
1758}
1759
1760static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001761 struct nlattr **attrs)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001762{
Thomas Graf7b67c852007-08-22 13:53:52 -07001763 struct xfrm_userpolicy_id *pi = nlmsg_data(nlh);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001764 struct xfrm_migrate m[XFRM_MAX_DEPTH];
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07001765 struct xfrm_kmaddress km, *kmp;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001766 u8 type;
1767 int err;
1768 int n = 0;
1769
Thomas Graf35a7aa02007-08-22 14:00:40 -07001770 if (attrs[XFRMA_MIGRATE] == NULL)
Thomas Grafcf5cb792007-08-22 13:59:04 -07001771 return -EINVAL;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001772
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07001773 kmp = attrs[XFRMA_KMADDRESS] ? &km : NULL;
1774
Thomas Graf5424f322007-08-22 14:01:33 -07001775 err = copy_from_user_policy_type(&type, attrs);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001776 if (err)
1777 return err;
1778
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07001779 err = copy_from_user_migrate((struct xfrm_migrate *)m, kmp, attrs, &n);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001780 if (err)
1781 return err;
1782
1783 if (!n)
1784 return 0;
1785
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07001786 xfrm_migrate(&pi->sel, pi->dir, type, m, n, kmp);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001787
1788 return 0;
1789}
1790#else
1791static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001792 struct nlattr **attrs)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001793{
1794 return -ENOPROTOOPT;
1795}
1796#endif
1797
1798#ifdef CONFIG_XFRM_MIGRATE
1799static int copy_to_user_migrate(struct xfrm_migrate *m, struct sk_buff *skb)
1800{
1801 struct xfrm_user_migrate um;
1802
1803 memset(&um, 0, sizeof(um));
1804 um.proto = m->proto;
1805 um.mode = m->mode;
1806 um.reqid = m->reqid;
1807 um.old_family = m->old_family;
1808 memcpy(&um.old_daddr, &m->old_daddr, sizeof(um.old_daddr));
1809 memcpy(&um.old_saddr, &m->old_saddr, sizeof(um.old_saddr));
1810 um.new_family = m->new_family;
1811 memcpy(&um.new_daddr, &m->new_daddr, sizeof(um.new_daddr));
1812 memcpy(&um.new_saddr, &m->new_saddr, sizeof(um.new_saddr));
1813
Thomas Grafc0144be2007-08-22 13:55:43 -07001814 return nla_put(skb, XFRMA_MIGRATE, sizeof(um), &um);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001815}
1816
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07001817static int copy_to_user_kmaddress(struct xfrm_kmaddress *k, struct sk_buff *skb)
1818{
1819 struct xfrm_user_kmaddress uk;
1820
1821 memset(&uk, 0, sizeof(uk));
1822 uk.family = k->family;
1823 uk.reserved = k->reserved;
1824 memcpy(&uk.local, &k->local, sizeof(uk.local));
Arnaud Ebalarda1caa322008-11-03 01:30:23 -08001825 memcpy(&uk.remote, &k->remote, sizeof(uk.remote));
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07001826
1827 return nla_put(skb, XFRMA_KMADDRESS, sizeof(uk), &uk);
1828}
1829
1830static inline size_t xfrm_migrate_msgsize(int num_migrate, int with_kma)
Thomas Graf7deb2262007-08-22 13:57:39 -07001831{
1832 return NLMSG_ALIGN(sizeof(struct xfrm_userpolicy_id))
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07001833 + (with_kma ? nla_total_size(sizeof(struct xfrm_kmaddress)) : 0)
1834 + nla_total_size(sizeof(struct xfrm_user_migrate) * num_migrate)
1835 + userpolicy_type_attrsize();
Thomas Graf7deb2262007-08-22 13:57:39 -07001836}
1837
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001838static int build_migrate(struct sk_buff *skb, struct xfrm_migrate *m,
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07001839 int num_migrate, struct xfrm_kmaddress *k,
1840 struct xfrm_selector *sel, u8 dir, u8 type)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001841{
1842 struct xfrm_migrate *mp;
1843 struct xfrm_userpolicy_id *pol_id;
1844 struct nlmsghdr *nlh;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001845 int i;
1846
Thomas Graf79b8b7f2007-08-22 12:46:53 -07001847 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_MIGRATE, sizeof(*pol_id), 0);
1848 if (nlh == NULL)
1849 return -EMSGSIZE;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001850
Thomas Graf7b67c852007-08-22 13:53:52 -07001851 pol_id = nlmsg_data(nlh);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001852 /* copy data from selector, dir, and type to the pol_id */
1853 memset(pol_id, 0, sizeof(*pol_id));
1854 memcpy(&pol_id->sel, sel, sizeof(pol_id->sel));
1855 pol_id->dir = dir;
1856
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07001857 if (k != NULL && (copy_to_user_kmaddress(k, skb) < 0))
1858 goto nlmsg_failure;
1859
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001860 if (copy_to_user_policy_type(type, skb) < 0)
1861 goto nlmsg_failure;
1862
1863 for (i = 0, mp = m ; i < num_migrate; i++, mp++) {
1864 if (copy_to_user_migrate(mp, skb) < 0)
1865 goto nlmsg_failure;
1866 }
1867
Thomas Graf98250692007-08-22 12:47:26 -07001868 return nlmsg_end(skb, nlh);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001869nlmsg_failure:
Thomas Graf98250692007-08-22 12:47:26 -07001870 nlmsg_cancel(skb, nlh);
1871 return -EMSGSIZE;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001872}
1873
1874static int xfrm_send_migrate(struct xfrm_selector *sel, u8 dir, u8 type,
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07001875 struct xfrm_migrate *m, int num_migrate,
1876 struct xfrm_kmaddress *k)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001877{
Alexey Dobriyana6483b72008-11-25 17:38:20 -08001878 struct net *net = &init_net;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001879 struct sk_buff *skb;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001880
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07001881 skb = nlmsg_new(xfrm_migrate_msgsize(num_migrate, !!k), GFP_ATOMIC);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001882 if (skb == NULL)
1883 return -ENOMEM;
1884
1885 /* build migrate */
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07001886 if (build_migrate(skb, m, num_migrate, k, sel, dir, type) < 0)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001887 BUG();
1888
Alexey Dobriyana6483b72008-11-25 17:38:20 -08001889 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_MIGRATE, GFP_ATOMIC);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001890}
1891#else
1892static int xfrm_send_migrate(struct xfrm_selector *sel, u8 dir, u8 type,
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07001893 struct xfrm_migrate *m, int num_migrate,
1894 struct xfrm_kmaddress *k)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001895{
1896 return -ENOPROTOOPT;
1897}
1898#endif
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001899
Thomas Grafa7bd9a42007-08-22 13:58:18 -07001900#define XMSGSIZE(type) sizeof(struct type)
Thomas Graf492b5582005-05-03 14:26:40 -07001901
1902static const int xfrm_msg_min[XFRM_NR_MSGTYPES] = {
1903 [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
1904 [XFRM_MSG_DELSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
1905 [XFRM_MSG_GETSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
1906 [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
1907 [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
1908 [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
1909 [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userspi_info),
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001910 [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_acquire),
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001911 [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_expire),
Thomas Graf492b5582005-05-03 14:26:40 -07001912 [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
1913 [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001914 [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_polexpire),
Thomas Graf492b5582005-05-03 14:26:40 -07001915 [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_flush),
Thomas Grafa7bd9a42007-08-22 13:58:18 -07001916 [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = 0,
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001917 [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
1918 [XFRM_MSG_GETAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07001919 [XFRM_MSG_REPORT - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_report),
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001920 [XFRM_MSG_MIGRATE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
Thomas Grafa7bd9a42007-08-22 13:58:18 -07001921 [XFRM_MSG_GETSADINFO - XFRM_MSG_BASE] = sizeof(u32),
1922 [XFRM_MSG_GETSPDINFO - XFRM_MSG_BASE] = sizeof(u32),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923};
1924
Thomas Graf492b5582005-05-03 14:26:40 -07001925#undef XMSGSIZE
1926
Thomas Grafcf5cb792007-08-22 13:59:04 -07001927static const struct nla_policy xfrma_policy[XFRMA_MAX+1] = {
Herbert Xu1a6509d2008-01-28 19:37:29 -08001928 [XFRMA_ALG_AEAD] = { .len = sizeof(struct xfrm_algo_aead) },
Thomas Grafcf5cb792007-08-22 13:59:04 -07001929 [XFRMA_ALG_AUTH] = { .len = sizeof(struct xfrm_algo) },
1930 [XFRMA_ALG_CRYPT] = { .len = sizeof(struct xfrm_algo) },
1931 [XFRMA_ALG_COMP] = { .len = sizeof(struct xfrm_algo) },
1932 [XFRMA_ENCAP] = { .len = sizeof(struct xfrm_encap_tmpl) },
1933 [XFRMA_TMPL] = { .len = sizeof(struct xfrm_user_tmpl) },
1934 [XFRMA_SEC_CTX] = { .len = sizeof(struct xfrm_sec_ctx) },
1935 [XFRMA_LTIME_VAL] = { .len = sizeof(struct xfrm_lifetime_cur) },
1936 [XFRMA_REPLAY_VAL] = { .len = sizeof(struct xfrm_replay_state) },
1937 [XFRMA_REPLAY_THRESH] = { .type = NLA_U32 },
1938 [XFRMA_ETIMER_THRESH] = { .type = NLA_U32 },
1939 [XFRMA_SRCADDR] = { .len = sizeof(xfrm_address_t) },
1940 [XFRMA_COADDR] = { .len = sizeof(xfrm_address_t) },
1941 [XFRMA_POLICY_TYPE] = { .len = sizeof(struct xfrm_userpolicy_type)},
1942 [XFRMA_MIGRATE] = { .len = sizeof(struct xfrm_user_migrate) },
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07001943 [XFRMA_KMADDRESS] = { .len = sizeof(struct xfrm_user_kmaddress) },
Thomas Grafcf5cb792007-08-22 13:59:04 -07001944};
1945
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946static struct xfrm_link {
Thomas Graf5424f322007-08-22 14:01:33 -07001947 int (*doit)(struct sk_buff *, struct nlmsghdr *, struct nlattr **);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948 int (*dump)(struct sk_buff *, struct netlink_callback *);
Timo Teras4c563f72008-02-28 21:31:08 -08001949 int (*done)(struct netlink_callback *);
Thomas Graf492b5582005-05-03 14:26:40 -07001950} xfrm_dispatch[XFRM_NR_MSGTYPES] = {
1951 [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa },
1952 [XFRM_MSG_DELSA - XFRM_MSG_BASE] = { .doit = xfrm_del_sa },
1953 [XFRM_MSG_GETSA - XFRM_MSG_BASE] = { .doit = xfrm_get_sa,
Timo Teras4c563f72008-02-28 21:31:08 -08001954 .dump = xfrm_dump_sa,
1955 .done = xfrm_dump_sa_done },
Thomas Graf492b5582005-05-03 14:26:40 -07001956 [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy },
1957 [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy },
1958 [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy,
Timo Teras4c563f72008-02-28 21:31:08 -08001959 .dump = xfrm_dump_policy,
1960 .done = xfrm_dump_policy_done },
Thomas Graf492b5582005-05-03 14:26:40 -07001961 [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = { .doit = xfrm_alloc_userspi },
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001962 [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_acquire },
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001963 [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_sa_expire },
Thomas Graf492b5582005-05-03 14:26:40 -07001964 [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy },
1965 [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa },
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001966 [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_pol_expire},
Thomas Graf492b5582005-05-03 14:26:40 -07001967 [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = { .doit = xfrm_flush_sa },
1968 [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_flush_policy },
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001969 [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = { .doit = xfrm_new_ae },
1970 [XFRM_MSG_GETAE - XFRM_MSG_BASE] = { .doit = xfrm_get_ae },
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001971 [XFRM_MSG_MIGRATE - XFRM_MSG_BASE] = { .doit = xfrm_do_migrate },
Jamal Hadi Salim566ec032007-04-26 14:12:15 -07001972 [XFRM_MSG_GETSADINFO - XFRM_MSG_BASE] = { .doit = xfrm_get_sadinfo },
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -07001973 [XFRM_MSG_GETSPDINFO - XFRM_MSG_BASE] = { .doit = xfrm_get_spdinfo },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974};
1975
Thomas Graf1d00a4e2007-03-22 23:30:12 -07001976static int xfrm_user_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977{
Alexey Dobriyana6483b72008-11-25 17:38:20 -08001978 struct net *net = sock_net(skb->sk);
Thomas Graf35a7aa02007-08-22 14:00:40 -07001979 struct nlattr *attrs[XFRMA_MAX+1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980 struct xfrm_link *link;
Thomas Grafa7bd9a42007-08-22 13:58:18 -07001981 int type, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983 type = nlh->nlmsg_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984 if (type > XFRM_MSG_MAX)
Thomas Graf1d00a4e2007-03-22 23:30:12 -07001985 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986
1987 type -= XFRM_MSG_BASE;
1988 link = &xfrm_dispatch[type];
1989
1990 /* All operations require privileges, even GET */
Thomas Graf1d00a4e2007-03-22 23:30:12 -07001991 if (security_netlink_recv(skb, CAP_NET_ADMIN))
1992 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993
Thomas Graf492b5582005-05-03 14:26:40 -07001994 if ((type == (XFRM_MSG_GETSA - XFRM_MSG_BASE) ||
1995 type == (XFRM_MSG_GETPOLICY - XFRM_MSG_BASE)) &&
1996 (nlh->nlmsg_flags & NLM_F_DUMP)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997 if (link->dump == NULL)
Thomas Graf1d00a4e2007-03-22 23:30:12 -07001998 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002000 return netlink_dump_start(net->xfrm.nlsk, skb, nlh, link->dump, link->done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001 }
2002
Thomas Graf35a7aa02007-08-22 14:00:40 -07002003 err = nlmsg_parse(nlh, xfrm_msg_min[type], attrs, XFRMA_MAX,
Thomas Grafcf5cb792007-08-22 13:59:04 -07002004 xfrma_policy);
Thomas Grafa7bd9a42007-08-22 13:58:18 -07002005 if (err < 0)
2006 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007
2008 if (link->doit == NULL)
Thomas Graf1d00a4e2007-03-22 23:30:12 -07002009 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010
Thomas Graf5424f322007-08-22 14:01:33 -07002011 return link->doit(skb, nlh, attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012}
2013
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07002014static void xfrm_netlink_rcv(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015{
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07002016 mutex_lock(&xfrm_cfg_mutex);
2017 netlink_rcv_skb(skb, &xfrm_user_rcv_msg);
2018 mutex_unlock(&xfrm_cfg_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019}
2020
Thomas Graf7deb2262007-08-22 13:57:39 -07002021static inline size_t xfrm_expire_msgsize(void)
2022{
2023 return NLMSG_ALIGN(sizeof(struct xfrm_user_expire));
2024}
2025
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002026static int build_expire(struct sk_buff *skb, struct xfrm_state *x, struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027{
2028 struct xfrm_user_expire *ue;
2029 struct nlmsghdr *nlh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002031 nlh = nlmsg_put(skb, c->pid, 0, XFRM_MSG_EXPIRE, sizeof(*ue), 0);
2032 if (nlh == NULL)
2033 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034
Thomas Graf7b67c852007-08-22 13:53:52 -07002035 ue = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036 copy_to_user_state(x, &ue->state);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002037 ue->hard = (c->data.hard != 0) ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038
Thomas Graf98250692007-08-22 12:47:26 -07002039 return nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040}
2041
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002042static int xfrm_exp_state_notify(struct xfrm_state *x, struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043{
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002044 struct net *net = &init_net;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045 struct sk_buff *skb;
2046
Thomas Graf7deb2262007-08-22 13:57:39 -07002047 skb = nlmsg_new(xfrm_expire_msgsize(), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048 if (skb == NULL)
2049 return -ENOMEM;
2050
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002051 if (build_expire(skb, x, c) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052 BUG();
2053
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002054 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_EXPIRE, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055}
2056
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002057static int xfrm_aevent_state_notify(struct xfrm_state *x, struct km_event *c)
2058{
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002059 struct net *net = &init_net;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002060 struct sk_buff *skb;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002061
Thomas Graf7deb2262007-08-22 13:57:39 -07002062 skb = nlmsg_new(xfrm_aevent_msgsize(), GFP_ATOMIC);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002063 if (skb == NULL)
2064 return -ENOMEM;
2065
2066 if (build_aevent(skb, x, c) < 0)
2067 BUG();
2068
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002069 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_AEVENTS, GFP_ATOMIC);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002070}
2071
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002072static int xfrm_notify_sa_flush(struct km_event *c)
2073{
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002074 struct net *net = &init_net;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002075 struct xfrm_usersa_flush *p;
2076 struct nlmsghdr *nlh;
2077 struct sk_buff *skb;
Thomas Graf7deb2262007-08-22 13:57:39 -07002078 int len = NLMSG_ALIGN(sizeof(struct xfrm_usersa_flush));
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002079
Thomas Graf7deb2262007-08-22 13:57:39 -07002080 skb = nlmsg_new(len, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002081 if (skb == NULL)
2082 return -ENOMEM;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002083
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002084 nlh = nlmsg_put(skb, c->pid, c->seq, XFRM_MSG_FLUSHSA, sizeof(*p), 0);
2085 if (nlh == NULL) {
2086 kfree_skb(skb);
2087 return -EMSGSIZE;
2088 }
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002089
Thomas Graf7b67c852007-08-22 13:53:52 -07002090 p = nlmsg_data(nlh);
Herbert Xubf08867f92005-06-18 22:44:00 -07002091 p->proto = c->data.proto;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002092
Thomas Graf98250692007-08-22 12:47:26 -07002093 nlmsg_end(skb, nlh);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002094
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002095 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_SA, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002096}
2097
Thomas Graf7deb2262007-08-22 13:57:39 -07002098static inline size_t xfrm_sa_len(struct xfrm_state *x)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002099{
Thomas Graf7deb2262007-08-22 13:57:39 -07002100 size_t l = 0;
Herbert Xu1a6509d2008-01-28 19:37:29 -08002101 if (x->aead)
2102 l += nla_total_size(aead_len(x->aead));
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002103 if (x->aalg)
Eric Dumazet0f99be02008-01-08 23:39:06 -08002104 l += nla_total_size(xfrm_alg_len(x->aalg));
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002105 if (x->ealg)
Eric Dumazet0f99be02008-01-08 23:39:06 -08002106 l += nla_total_size(xfrm_alg_len(x->ealg));
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002107 if (x->calg)
Thomas Graf7deb2262007-08-22 13:57:39 -07002108 l += nla_total_size(sizeof(*x->calg));
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002109 if (x->encap)
Thomas Graf7deb2262007-08-22 13:57:39 -07002110 l += nla_total_size(sizeof(*x->encap));
Herbert Xu68325d32007-10-09 13:30:57 -07002111 if (x->security)
2112 l += nla_total_size(sizeof(struct xfrm_user_sec_ctx) +
2113 x->security->ctx_len);
2114 if (x->coaddr)
2115 l += nla_total_size(sizeof(*x->coaddr));
2116
Herbert Xud26f3982007-11-13 21:47:08 -08002117 /* Must count x->lastused as it may become non-zero behind our back. */
2118 l += nla_total_size(sizeof(u64));
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002119
2120 return l;
2121}
2122
2123static int xfrm_notify_sa(struct xfrm_state *x, struct km_event *c)
2124{
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002125 struct net *net = &init_net;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002126 struct xfrm_usersa_info *p;
Herbert Xu0603eac2005-06-18 22:54:36 -07002127 struct xfrm_usersa_id *id;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002128 struct nlmsghdr *nlh;
2129 struct sk_buff *skb;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002130 int len = xfrm_sa_len(x);
Herbert Xu0603eac2005-06-18 22:54:36 -07002131 int headlen;
2132
2133 headlen = sizeof(*p);
2134 if (c->event == XFRM_MSG_DELSA) {
Thomas Graf7deb2262007-08-22 13:57:39 -07002135 len += nla_total_size(headlen);
Herbert Xu0603eac2005-06-18 22:54:36 -07002136 headlen = sizeof(*id);
2137 }
Thomas Graf7deb2262007-08-22 13:57:39 -07002138 len += NLMSG_ALIGN(headlen);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002139
Thomas Graf7deb2262007-08-22 13:57:39 -07002140 skb = nlmsg_new(len, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002141 if (skb == NULL)
2142 return -ENOMEM;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002143
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002144 nlh = nlmsg_put(skb, c->pid, c->seq, c->event, headlen, 0);
2145 if (nlh == NULL)
Thomas Grafc0144be2007-08-22 13:55:43 -07002146 goto nla_put_failure;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002147
Thomas Graf7b67c852007-08-22 13:53:52 -07002148 p = nlmsg_data(nlh);
Herbert Xu0603eac2005-06-18 22:54:36 -07002149 if (c->event == XFRM_MSG_DELSA) {
Thomas Grafc0144be2007-08-22 13:55:43 -07002150 struct nlattr *attr;
2151
Thomas Graf7b67c852007-08-22 13:53:52 -07002152 id = nlmsg_data(nlh);
Herbert Xu0603eac2005-06-18 22:54:36 -07002153 memcpy(&id->daddr, &x->id.daddr, sizeof(id->daddr));
2154 id->spi = x->id.spi;
2155 id->family = x->props.family;
2156 id->proto = x->id.proto;
2157
Thomas Grafc0144be2007-08-22 13:55:43 -07002158 attr = nla_reserve(skb, XFRMA_SA, sizeof(*p));
2159 if (attr == NULL)
2160 goto nla_put_failure;
2161
2162 p = nla_data(attr);
Herbert Xu0603eac2005-06-18 22:54:36 -07002163 }
2164
Herbert Xu68325d32007-10-09 13:30:57 -07002165 if (copy_to_user_state_extra(x, p, skb))
2166 goto nla_put_failure;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002167
Thomas Graf98250692007-08-22 12:47:26 -07002168 nlmsg_end(skb, nlh);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002169
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002170 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_SA, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002171
Thomas Grafc0144be2007-08-22 13:55:43 -07002172nla_put_failure:
Herbert Xu68325d32007-10-09 13:30:57 -07002173 /* Somebody screwed up with xfrm_sa_len! */
2174 WARN_ON(1);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002175 kfree_skb(skb);
2176 return -1;
2177}
2178
2179static int xfrm_send_state_notify(struct xfrm_state *x, struct km_event *c)
2180{
2181
2182 switch (c->event) {
Herbert Xuf60f6b82005-06-18 22:44:37 -07002183 case XFRM_MSG_EXPIRE:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002184 return xfrm_exp_state_notify(x, c);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002185 case XFRM_MSG_NEWAE:
2186 return xfrm_aevent_state_notify(x, c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07002187 case XFRM_MSG_DELSA:
2188 case XFRM_MSG_UPDSA:
2189 case XFRM_MSG_NEWSA:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002190 return xfrm_notify_sa(x, c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07002191 case XFRM_MSG_FLUSHSA:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002192 return xfrm_notify_sa_flush(c);
2193 default:
2194 printk("xfrm_user: Unknown SA event %d\n", c->event);
2195 break;
2196 }
2197
2198 return 0;
2199
2200}
2201
Thomas Graf7deb2262007-08-22 13:57:39 -07002202static inline size_t xfrm_acquire_msgsize(struct xfrm_state *x,
2203 struct xfrm_policy *xp)
2204{
2205 return NLMSG_ALIGN(sizeof(struct xfrm_user_acquire))
2206 + nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr)
2207 + nla_total_size(xfrm_user_sec_ctx_size(x->security))
2208 + userpolicy_type_attrsize();
2209}
2210
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211static int build_acquire(struct sk_buff *skb, struct xfrm_state *x,
2212 struct xfrm_tmpl *xt, struct xfrm_policy *xp,
2213 int dir)
2214{
2215 struct xfrm_user_acquire *ua;
2216 struct nlmsghdr *nlh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217 __u32 seq = xfrm_get_acqseq();
2218
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002219 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_ACQUIRE, sizeof(*ua), 0);
2220 if (nlh == NULL)
2221 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002222
Thomas Graf7b67c852007-08-22 13:53:52 -07002223 ua = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002224 memcpy(&ua->id, &x->id, sizeof(ua->id));
2225 memcpy(&ua->saddr, &x->props.saddr, sizeof(ua->saddr));
2226 memcpy(&ua->sel, &x->sel, sizeof(ua->sel));
2227 copy_to_user_policy(xp, &ua->policy, dir);
2228 ua->aalgos = xt->aalgos;
2229 ua->ealgos = xt->ealgos;
2230 ua->calgos = xt->calgos;
2231 ua->seq = x->km.seq = seq;
2232
2233 if (copy_to_user_tmpl(xp, skb) < 0)
2234 goto nlmsg_failure;
Serge Hallyn0d681622006-07-24 23:30:44 -07002235 if (copy_to_user_state_sec_ctx(x, skb))
Trent Jaegerdf718372005-12-13 23:12:27 -08002236 goto nlmsg_failure;
Jamal Hadi Salim1459bb32006-11-20 16:51:22 -08002237 if (copy_to_user_policy_type(xp->type, skb) < 0)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07002238 goto nlmsg_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002239
Thomas Graf98250692007-08-22 12:47:26 -07002240 return nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241
2242nlmsg_failure:
Thomas Graf98250692007-08-22 12:47:26 -07002243 nlmsg_cancel(skb, nlh);
2244 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002245}
2246
2247static int xfrm_send_acquire(struct xfrm_state *x, struct xfrm_tmpl *xt,
2248 struct xfrm_policy *xp, int dir)
2249{
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002250 struct net *net = xs_net(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252
Thomas Graf7deb2262007-08-22 13:57:39 -07002253 skb = nlmsg_new(xfrm_acquire_msgsize(x, xp), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002254 if (skb == NULL)
2255 return -ENOMEM;
2256
2257 if (build_acquire(skb, x, xt, xp, dir) < 0)
2258 BUG();
2259
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002260 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_ACQUIRE, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002261}
2262
2263/* User gives us xfrm_user_policy_info followed by an array of 0
2264 * or more templates.
2265 */
Venkat Yekkiralacb969f02006-07-24 23:32:20 -07002266static struct xfrm_policy *xfrm_compile_policy(struct sock *sk, int opt,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267 u8 *data, int len, int *dir)
2268{
2269 struct xfrm_userpolicy_info *p = (struct xfrm_userpolicy_info *)data;
2270 struct xfrm_user_tmpl *ut = (struct xfrm_user_tmpl *) (p + 1);
2271 struct xfrm_policy *xp;
2272 int nr;
2273
Venkat Yekkiralacb969f02006-07-24 23:32:20 -07002274 switch (sk->sk_family) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275 case AF_INET:
2276 if (opt != IP_XFRM_POLICY) {
2277 *dir = -EOPNOTSUPP;
2278 return NULL;
2279 }
2280 break;
2281#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
2282 case AF_INET6:
2283 if (opt != IPV6_XFRM_POLICY) {
2284 *dir = -EOPNOTSUPP;
2285 return NULL;
2286 }
2287 break;
2288#endif
2289 default:
2290 *dir = -EINVAL;
2291 return NULL;
2292 }
2293
2294 *dir = -EINVAL;
2295
2296 if (len < sizeof(*p) ||
2297 verify_newpolicy_info(p))
2298 return NULL;
2299
2300 nr = ((len - sizeof(*p)) / sizeof(*ut));
David S. Millerb4ad86bf2006-12-03 19:19:26 -08002301 if (validate_tmpl(nr, ut, p->sel.family))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302 return NULL;
2303
Herbert Xua4f1bac2005-07-26 15:43:17 -07002304 if (p->dir > XFRM_POLICY_OUT)
2305 return NULL;
2306
Alexey Dobriyan0331b1f2008-11-25 17:21:45 -08002307 xp = xfrm_policy_alloc(&init_net, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308 if (xp == NULL) {
2309 *dir = -ENOBUFS;
2310 return NULL;
2311 }
2312
2313 copy_from_user_policy(xp, p);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07002314 xp->type = XFRM_POLICY_TYPE_MAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002315 copy_templates(xp, ut, nr);
2316
2317 *dir = p->dir;
2318
2319 return xp;
2320}
2321
Thomas Graf7deb2262007-08-22 13:57:39 -07002322static inline size_t xfrm_polexpire_msgsize(struct xfrm_policy *xp)
2323{
2324 return NLMSG_ALIGN(sizeof(struct xfrm_user_polexpire))
2325 + nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr)
2326 + nla_total_size(xfrm_user_sec_ctx_size(xp->security))
2327 + userpolicy_type_attrsize();
2328}
2329
Linus Torvalds1da177e2005-04-16 15:20:36 -07002330static int build_polexpire(struct sk_buff *skb, struct xfrm_policy *xp,
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002331 int dir, struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002332{
2333 struct xfrm_user_polexpire *upe;
2334 struct nlmsghdr *nlh;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002335 int hard = c->data.hard;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002336
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002337 nlh = nlmsg_put(skb, c->pid, 0, XFRM_MSG_POLEXPIRE, sizeof(*upe), 0);
2338 if (nlh == NULL)
2339 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002340
Thomas Graf7b67c852007-08-22 13:53:52 -07002341 upe = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002342 copy_to_user_policy(xp, &upe->pol, dir);
2343 if (copy_to_user_tmpl(xp, skb) < 0)
2344 goto nlmsg_failure;
Trent Jaegerdf718372005-12-13 23:12:27 -08002345 if (copy_to_user_sec_ctx(xp, skb))
2346 goto nlmsg_failure;
Jamal Hadi Salim1459bb32006-11-20 16:51:22 -08002347 if (copy_to_user_policy_type(xp->type, skb) < 0)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07002348 goto nlmsg_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002349 upe->hard = !!hard;
2350
Thomas Graf98250692007-08-22 12:47:26 -07002351 return nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002352
2353nlmsg_failure:
Thomas Graf98250692007-08-22 12:47:26 -07002354 nlmsg_cancel(skb, nlh);
2355 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002356}
2357
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002358static int xfrm_exp_policy_notify(struct xfrm_policy *xp, int dir, struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002359{
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002360 struct net *net = &init_net;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002361 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002362
Thomas Graf7deb2262007-08-22 13:57:39 -07002363 skb = nlmsg_new(xfrm_polexpire_msgsize(xp), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002364 if (skb == NULL)
2365 return -ENOMEM;
2366
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002367 if (build_polexpire(skb, xp, dir, c) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002368 BUG();
2369
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002370 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_EXPIRE, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002371}
2372
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002373static int xfrm_notify_policy(struct xfrm_policy *xp, int dir, struct km_event *c)
2374{
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002375 struct net *net = &init_net;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002376 struct xfrm_userpolicy_info *p;
Herbert Xu0603eac2005-06-18 22:54:36 -07002377 struct xfrm_userpolicy_id *id;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002378 struct nlmsghdr *nlh;
2379 struct sk_buff *skb;
Thomas Graf7deb2262007-08-22 13:57:39 -07002380 int len = nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr);
Herbert Xu0603eac2005-06-18 22:54:36 -07002381 int headlen;
2382
2383 headlen = sizeof(*p);
2384 if (c->event == XFRM_MSG_DELPOLICY) {
Thomas Graf7deb2262007-08-22 13:57:39 -07002385 len += nla_total_size(headlen);
Herbert Xu0603eac2005-06-18 22:54:36 -07002386 headlen = sizeof(*id);
2387 }
Thomas Grafcfbfd452007-08-22 13:57:04 -07002388 len += userpolicy_type_attrsize();
Thomas Graf7deb2262007-08-22 13:57:39 -07002389 len += NLMSG_ALIGN(headlen);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002390
Thomas Graf7deb2262007-08-22 13:57:39 -07002391 skb = nlmsg_new(len, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002392 if (skb == NULL)
2393 return -ENOMEM;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002394
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002395 nlh = nlmsg_put(skb, c->pid, c->seq, c->event, headlen, 0);
2396 if (nlh == NULL)
2397 goto nlmsg_failure;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002398
Thomas Graf7b67c852007-08-22 13:53:52 -07002399 p = nlmsg_data(nlh);
Herbert Xu0603eac2005-06-18 22:54:36 -07002400 if (c->event == XFRM_MSG_DELPOLICY) {
Thomas Grafc0144be2007-08-22 13:55:43 -07002401 struct nlattr *attr;
2402
Thomas Graf7b67c852007-08-22 13:53:52 -07002403 id = nlmsg_data(nlh);
Herbert Xu0603eac2005-06-18 22:54:36 -07002404 memset(id, 0, sizeof(*id));
2405 id->dir = dir;
2406 if (c->data.byid)
2407 id->index = xp->index;
2408 else
2409 memcpy(&id->sel, &xp->selector, sizeof(id->sel));
2410
Thomas Grafc0144be2007-08-22 13:55:43 -07002411 attr = nla_reserve(skb, XFRMA_POLICY, sizeof(*p));
2412 if (attr == NULL)
2413 goto nlmsg_failure;
2414
2415 p = nla_data(attr);
Herbert Xu0603eac2005-06-18 22:54:36 -07002416 }
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002417
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002418 copy_to_user_policy(xp, p, dir);
2419 if (copy_to_user_tmpl(xp, skb) < 0)
2420 goto nlmsg_failure;
Jamal Hadi Salim1459bb32006-11-20 16:51:22 -08002421 if (copy_to_user_policy_type(xp->type, skb) < 0)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07002422 goto nlmsg_failure;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002423
Thomas Graf98250692007-08-22 12:47:26 -07002424 nlmsg_end(skb, nlh);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002425
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002426 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_POLICY, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002427
2428nlmsg_failure:
2429 kfree_skb(skb);
2430 return -1;
2431}
2432
2433static int xfrm_notify_policy_flush(struct km_event *c)
2434{
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002435 struct net *net = &init_net;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002436 struct nlmsghdr *nlh;
2437 struct sk_buff *skb;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002438
Thomas Graf7deb2262007-08-22 13:57:39 -07002439 skb = nlmsg_new(userpolicy_type_attrsize(), GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002440 if (skb == NULL)
2441 return -ENOMEM;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002442
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002443 nlh = nlmsg_put(skb, c->pid, c->seq, XFRM_MSG_FLUSHPOLICY, 0, 0);
2444 if (nlh == NULL)
2445 goto nlmsg_failure;
Jamal Hadi Salim0c51f532006-11-27 12:58:20 -08002446 if (copy_to_user_policy_type(c->data.type, skb) < 0)
2447 goto nlmsg_failure;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002448
Thomas Graf98250692007-08-22 12:47:26 -07002449 nlmsg_end(skb, nlh);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002450
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002451 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_POLICY, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002452
2453nlmsg_failure:
2454 kfree_skb(skb);
2455 return -1;
2456}
2457
2458static int xfrm_send_policy_notify(struct xfrm_policy *xp, int dir, struct km_event *c)
2459{
2460
2461 switch (c->event) {
Herbert Xuf60f6b82005-06-18 22:44:37 -07002462 case XFRM_MSG_NEWPOLICY:
2463 case XFRM_MSG_UPDPOLICY:
2464 case XFRM_MSG_DELPOLICY:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002465 return xfrm_notify_policy(xp, dir, c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07002466 case XFRM_MSG_FLUSHPOLICY:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002467 return xfrm_notify_policy_flush(c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07002468 case XFRM_MSG_POLEXPIRE:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002469 return xfrm_exp_policy_notify(xp, dir, c);
2470 default:
2471 printk("xfrm_user: Unknown Policy event %d\n", c->event);
2472 }
2473
2474 return 0;
2475
2476}
2477
Thomas Graf7deb2262007-08-22 13:57:39 -07002478static inline size_t xfrm_report_msgsize(void)
2479{
2480 return NLMSG_ALIGN(sizeof(struct xfrm_user_report));
2481}
2482
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002483static int build_report(struct sk_buff *skb, u8 proto,
2484 struct xfrm_selector *sel, xfrm_address_t *addr)
2485{
2486 struct xfrm_user_report *ur;
2487 struct nlmsghdr *nlh;
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002488
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002489 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_REPORT, sizeof(*ur), 0);
2490 if (nlh == NULL)
2491 return -EMSGSIZE;
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002492
Thomas Graf7b67c852007-08-22 13:53:52 -07002493 ur = nlmsg_data(nlh);
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002494 ur->proto = proto;
2495 memcpy(&ur->sel, sel, sizeof(ur->sel));
2496
2497 if (addr)
Thomas Grafc0144be2007-08-22 13:55:43 -07002498 NLA_PUT(skb, XFRMA_COADDR, sizeof(*addr), addr);
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002499
Thomas Graf98250692007-08-22 12:47:26 -07002500 return nlmsg_end(skb, nlh);
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002501
Thomas Grafc0144be2007-08-22 13:55:43 -07002502nla_put_failure:
Thomas Graf98250692007-08-22 12:47:26 -07002503 nlmsg_cancel(skb, nlh);
2504 return -EMSGSIZE;
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002505}
2506
2507static int xfrm_send_report(u8 proto, struct xfrm_selector *sel,
2508 xfrm_address_t *addr)
2509{
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002510 struct net *net = &init_net;
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002511 struct sk_buff *skb;
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002512
Thomas Graf7deb2262007-08-22 13:57:39 -07002513 skb = nlmsg_new(xfrm_report_msgsize(), GFP_ATOMIC);
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002514 if (skb == NULL)
2515 return -ENOMEM;
2516
2517 if (build_report(skb, proto, sel, addr) < 0)
2518 BUG();
2519
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002520 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_REPORT, GFP_ATOMIC);
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002521}
2522
Martin Willi3a2dfbe2008-10-28 16:01:07 -07002523static inline size_t xfrm_mapping_msgsize(void)
2524{
2525 return NLMSG_ALIGN(sizeof(struct xfrm_user_mapping));
2526}
2527
2528static int build_mapping(struct sk_buff *skb, struct xfrm_state *x,
2529 xfrm_address_t *new_saddr, __be16 new_sport)
2530{
2531 struct xfrm_user_mapping *um;
2532 struct nlmsghdr *nlh;
2533
2534 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_MAPPING, sizeof(*um), 0);
2535 if (nlh == NULL)
2536 return -EMSGSIZE;
2537
2538 um = nlmsg_data(nlh);
2539
2540 memcpy(&um->id.daddr, &x->id.daddr, sizeof(um->id.daddr));
2541 um->id.spi = x->id.spi;
2542 um->id.family = x->props.family;
2543 um->id.proto = x->id.proto;
2544 memcpy(&um->new_saddr, new_saddr, sizeof(um->new_saddr));
2545 memcpy(&um->old_saddr, &x->props.saddr, sizeof(um->old_saddr));
2546 um->new_sport = new_sport;
2547 um->old_sport = x->encap->encap_sport;
2548 um->reqid = x->props.reqid;
2549
2550 return nlmsg_end(skb, nlh);
2551}
2552
2553static int xfrm_send_mapping(struct xfrm_state *x, xfrm_address_t *ipaddr,
2554 __be16 sport)
2555{
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002556 struct net *net = xs_net(x);
Martin Willi3a2dfbe2008-10-28 16:01:07 -07002557 struct sk_buff *skb;
2558
2559 if (x->id.proto != IPPROTO_ESP)
2560 return -EINVAL;
2561
2562 if (!x->encap)
2563 return -EINVAL;
2564
2565 skb = nlmsg_new(xfrm_mapping_msgsize(), GFP_ATOMIC);
2566 if (skb == NULL)
2567 return -ENOMEM;
2568
2569 if (build_mapping(skb, x, ipaddr, sport) < 0)
2570 BUG();
2571
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002572 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_MAPPING, GFP_ATOMIC);
Martin Willi3a2dfbe2008-10-28 16:01:07 -07002573}
2574
Linus Torvalds1da177e2005-04-16 15:20:36 -07002575static struct xfrm_mgr netlink_mgr = {
2576 .id = "netlink",
2577 .notify = xfrm_send_state_notify,
2578 .acquire = xfrm_send_acquire,
2579 .compile_policy = xfrm_compile_policy,
2580 .notify_policy = xfrm_send_policy_notify,
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002581 .report = xfrm_send_report,
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002582 .migrate = xfrm_send_migrate,
Martin Willi3a2dfbe2008-10-28 16:01:07 -07002583 .new_mapping = xfrm_send_mapping,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002584};
2585
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002586static int __net_init xfrm_user_net_init(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002587{
Patrick McHardybe336902006-03-20 22:40:54 -08002588 struct sock *nlsk;
2589
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002590 nlsk = netlink_kernel_create(net, NETLINK_XFRM, XFRMNLGRP_MAX,
Patrick McHardyaf65bdf2007-04-20 14:14:21 -07002591 xfrm_netlink_rcv, NULL, THIS_MODULE);
Patrick McHardybe336902006-03-20 22:40:54 -08002592 if (nlsk == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002593 return -ENOMEM;
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002594 rcu_assign_pointer(net->xfrm.nlsk, nlsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002595 return 0;
2596}
2597
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002598static void __net_exit xfrm_user_net_exit(struct net *net)
2599{
2600 struct sock *nlsk = net->xfrm.nlsk;
2601
2602 rcu_assign_pointer(net->xfrm.nlsk, NULL);
2603 synchronize_rcu();
2604 netlink_kernel_release(nlsk);
2605}
2606
2607static struct pernet_operations xfrm_user_net_ops = {
2608 .init = xfrm_user_net_init,
2609 .exit = xfrm_user_net_exit,
2610};
2611
2612static int __init xfrm_user_init(void)
2613{
2614 int rv;
2615
2616 printk(KERN_INFO "Initializing XFRM netlink socket\n");
2617
2618 rv = register_pernet_subsys(&xfrm_user_net_ops);
2619 if (rv < 0)
2620 return rv;
2621 rv = xfrm_register_km(&netlink_mgr);
2622 if (rv < 0)
2623 unregister_pernet_subsys(&xfrm_user_net_ops);
2624 return rv;
2625}
2626
Linus Torvalds1da177e2005-04-16 15:20:36 -07002627static void __exit xfrm_user_exit(void)
2628{
2629 xfrm_unregister_km(&netlink_mgr);
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002630 unregister_pernet_subsys(&xfrm_user_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002631}
2632
2633module_init(xfrm_user_init);
2634module_exit(xfrm_user_exit);
2635MODULE_LICENSE("GPL");
Harald Welte4fdb3bb2005-08-09 19:40:55 -07002636MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK, NETLINK_XFRM);
Jamal Hadi Salimf8cd5482006-03-20 19:15:11 -08002637