blob: b95a2d64eb59c3f86e066acc86fee8e2e7fb61e1 [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
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800319static struct xfrm_state *xfrm_state_construct(struct net *net,
320 struct xfrm_usersa_info *p,
Thomas Graf5424f322007-08-22 14:01:33 -0700321 struct nlattr **attrs,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 int *errp)
323{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800324 struct xfrm_state *x = xfrm_state_alloc(net);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 int err = -ENOMEM;
326
327 if (!x)
328 goto error_no_put;
329
330 copy_from_user_state(x, p);
331
Herbert Xu1a6509d2008-01-28 19:37:29 -0800332 if ((err = attach_aead(&x->aead, &x->props.ealgo,
333 attrs[XFRMA_ALG_AEAD])))
334 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 if ((err = attach_one_algo(&x->aalg, &x->props.aalgo,
336 xfrm_aalg_get_byname,
Thomas Graf35a7aa02007-08-22 14:00:40 -0700337 attrs[XFRMA_ALG_AUTH])))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 goto error;
339 if ((err = attach_one_algo(&x->ealg, &x->props.ealgo,
340 xfrm_ealg_get_byname,
Thomas Graf35a7aa02007-08-22 14:00:40 -0700341 attrs[XFRMA_ALG_CRYPT])))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 goto error;
343 if ((err = attach_one_algo(&x->calg, &x->props.calgo,
344 xfrm_calg_get_byname,
Thomas Graf35a7aa02007-08-22 14:00:40 -0700345 attrs[XFRMA_ALG_COMP])))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 goto error;
Thomas Graffd211502007-09-06 03:28:08 -0700347
348 if (attrs[XFRMA_ENCAP]) {
349 x->encap = kmemdup(nla_data(attrs[XFRMA_ENCAP]),
350 sizeof(*x->encap), GFP_KERNEL);
351 if (x->encap == NULL)
352 goto error;
353 }
354
355 if (attrs[XFRMA_COADDR]) {
356 x->coaddr = kmemdup(nla_data(attrs[XFRMA_COADDR]),
357 sizeof(*x->coaddr), GFP_KERNEL);
358 if (x->coaddr == NULL)
359 goto error;
360 }
361
Herbert Xu72cb6962005-06-20 13:18:08 -0700362 err = xfrm_init_state(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 if (err)
364 goto error;
365
Thomas Graffd211502007-09-06 03:28:08 -0700366 if (attrs[XFRMA_SEC_CTX] &&
367 security_xfrm_state_alloc(x, nla_data(attrs[XFRMA_SEC_CTX])))
Trent Jaegerdf718372005-12-13 23:12:27 -0800368 goto error;
369
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 x->km.seq = p->seq;
Alexey Dobriyanb27aead2008-11-25 18:00:48 -0800371 x->replay_maxdiff = net->xfrm.sysctl_aevent_rseqth;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800372 /* sysctl_xfrm_aevent_etime is in 100ms units */
Alexey Dobriyanb27aead2008-11-25 18:00:48 -0800373 x->replay_maxage = (net->xfrm.sysctl_aevent_etime*HZ)/XFRM_AE_ETH_M;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800374 x->preplay.bitmap = 0;
375 x->preplay.seq = x->replay.seq+x->replay_maxdiff;
376 x->preplay.oseq = x->replay.oseq +x->replay_maxdiff;
377
378 /* override default values from above */
379
Thomas Graf5424f322007-08-22 14:01:33 -0700380 xfrm_update_ae_params(x, attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381
382 return x;
383
384error:
385 x->km.state = XFRM_STATE_DEAD;
386 xfrm_state_put(x);
387error_no_put:
388 *errp = err;
389 return NULL;
390}
391
Christoph Hellwig22e70052007-01-02 15:22:30 -0800392static int xfrm_add_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -0700393 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800395 struct net *net = sock_net(skb->sk);
Thomas Graf7b67c852007-08-22 13:53:52 -0700396 struct xfrm_usersa_info *p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 struct xfrm_state *x;
398 int err;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700399 struct km_event c;
Eric Paris25323862008-04-18 10:09:25 -0400400 uid_t loginuid = NETLINK_CB(skb).loginuid;
401 u32 sessionid = NETLINK_CB(skb).sessionid;
402 u32 sid = NETLINK_CB(skb).sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403
Thomas Graf35a7aa02007-08-22 14:00:40 -0700404 err = verify_newsa_info(p, attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 if (err)
406 return err;
407
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800408 x = xfrm_state_construct(net, p, attrs, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 if (!x)
410 return err;
411
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700412 xfrm_state_hold(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 if (nlh->nlmsg_type == XFRM_MSG_NEWSA)
414 err = xfrm_state_add(x);
415 else
416 err = xfrm_state_update(x);
417
Eric Paris25323862008-04-18 10:09:25 -0400418 xfrm_audit_state_add(x, err ? 0 : 1, loginuid, sessionid, sid);
Joy Latten161a09e2006-11-27 13:11:54 -0600419
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 if (err < 0) {
421 x->km.state = XFRM_STATE_DEAD;
Herbert Xu21380b82006-02-22 14:47:13 -0800422 __xfrm_state_put(x);
Patrick McHardy7d6dfe12005-06-18 22:45:31 -0700423 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 }
425
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700426 c.seq = nlh->nlmsg_seq;
427 c.pid = nlh->nlmsg_pid;
Herbert Xuf60f6b82005-06-18 22:44:37 -0700428 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700429
430 km_state_notify(x, &c);
Patrick McHardy7d6dfe12005-06-18 22:45:31 -0700431out:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700432 xfrm_state_put(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 return err;
434}
435
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800436static struct xfrm_state *xfrm_user_state_lookup(struct net *net,
437 struct xfrm_usersa_id *p,
Thomas Graf5424f322007-08-22 14:01:33 -0700438 struct nlattr **attrs,
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700439 int *errp)
440{
441 struct xfrm_state *x = NULL;
442 int err;
443
444 if (xfrm_id_proto_match(p->proto, IPSEC_PROTO_ANY)) {
445 err = -ESRCH;
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800446 x = xfrm_state_lookup(net, &p->daddr, p->spi, p->proto, p->family);
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700447 } else {
448 xfrm_address_t *saddr = NULL;
449
Thomas Graf35a7aa02007-08-22 14:00:40 -0700450 verify_one_addr(attrs, XFRMA_SRCADDR, &saddr);
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700451 if (!saddr) {
452 err = -EINVAL;
453 goto out;
454 }
455
Masahide NAKAMURA9abbffe2006-11-24 20:34:51 -0800456 err = -ESRCH;
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800457 x = xfrm_state_lookup_byaddr(net, &p->daddr, saddr,
Alexey Dobriyan221df1e2008-11-25 17:30:50 -0800458 p->proto, p->family);
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700459 }
460
461 out:
462 if (!x && errp)
463 *errp = err;
464 return x;
465}
466
Christoph Hellwig22e70052007-01-02 15:22:30 -0800467static int xfrm_del_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -0700468 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800470 struct net *net = sock_net(skb->sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 struct xfrm_state *x;
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700472 int err = -ESRCH;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700473 struct km_event c;
Thomas Graf7b67c852007-08-22 13:53:52 -0700474 struct xfrm_usersa_id *p = nlmsg_data(nlh);
Eric Paris25323862008-04-18 10:09:25 -0400475 uid_t loginuid = NETLINK_CB(skb).loginuid;
476 u32 sessionid = NETLINK_CB(skb).sessionid;
477 u32 sid = NETLINK_CB(skb).sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800479 x = xfrm_user_state_lookup(net, p, attrs, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 if (x == NULL)
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700481 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482
David S. Miller6f68dc32006-06-08 23:58:52 -0700483 if ((err = security_xfrm_state_delete(x)) != 0)
Catherine Zhangc8c05a82006-06-08 23:39:49 -0700484 goto out;
485
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 if (xfrm_state_kern(x)) {
Catherine Zhangc8c05a82006-06-08 23:39:49 -0700487 err = -EPERM;
488 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 }
490
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700491 err = xfrm_state_delete(x);
Joy Latten161a09e2006-11-27 13:11:54 -0600492
Catherine Zhangc8c05a82006-06-08 23:39:49 -0700493 if (err < 0)
494 goto out;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700495
496 c.seq = nlh->nlmsg_seq;
497 c.pid = nlh->nlmsg_pid;
Herbert Xuf60f6b82005-06-18 22:44:37 -0700498 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700499 km_state_notify(x, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500
Catherine Zhangc8c05a82006-06-08 23:39:49 -0700501out:
Eric Paris25323862008-04-18 10:09:25 -0400502 xfrm_audit_state_delete(x, err ? 0 : 1, loginuid, sessionid, sid);
Catherine Zhangc8c05a82006-06-08 23:39:49 -0700503 xfrm_state_put(x);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700504 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505}
506
507static void copy_to_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p)
508{
509 memcpy(&p->id, &x->id, sizeof(p->id));
510 memcpy(&p->sel, &x->sel, sizeof(p->sel));
511 memcpy(&p->lft, &x->lft, sizeof(p->lft));
512 memcpy(&p->curlft, &x->curlft, sizeof(p->curlft));
513 memcpy(&p->stats, &x->stats, sizeof(p->stats));
David S. Miller54489c142006-10-27 15:29:47 -0700514 memcpy(&p->saddr, &x->props.saddr, sizeof(p->saddr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 p->mode = x->props.mode;
516 p->replay_window = x->props.replay_window;
517 p->reqid = x->props.reqid;
518 p->family = x->props.family;
519 p->flags = x->props.flags;
520 p->seq = x->km.seq;
521}
522
523struct xfrm_dump_info {
524 struct sk_buff *in_skb;
525 struct sk_buff *out_skb;
526 u32 nlmsg_seq;
527 u16 nlmsg_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528};
529
Thomas Grafc0144be2007-08-22 13:55:43 -0700530static int copy_sec_ctx(struct xfrm_sec_ctx *s, struct sk_buff *skb)
531{
Thomas Grafc0144be2007-08-22 13:55:43 -0700532 struct xfrm_user_sec_ctx *uctx;
533 struct nlattr *attr;
Herbert Xu68325d32007-10-09 13:30:57 -0700534 int ctx_size = sizeof(*uctx) + s->ctx_len;
Thomas Grafc0144be2007-08-22 13:55:43 -0700535
536 attr = nla_reserve(skb, XFRMA_SEC_CTX, ctx_size);
537 if (attr == NULL)
538 return -EMSGSIZE;
539
540 uctx = nla_data(attr);
541 uctx->exttype = XFRMA_SEC_CTX;
542 uctx->len = ctx_size;
543 uctx->ctx_doi = s->ctx_doi;
544 uctx->ctx_alg = s->ctx_alg;
545 uctx->ctx_len = s->ctx_len;
546 memcpy(uctx + 1, s->ctx_str, s->ctx_len);
547
548 return 0;
549}
550
Herbert Xu68325d32007-10-09 13:30:57 -0700551/* Don't change this without updating xfrm_sa_len! */
552static int copy_to_user_state_extra(struct xfrm_state *x,
553 struct xfrm_usersa_info *p,
554 struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 copy_to_user_state(x, p);
557
Herbert Xu050f0092007-10-09 13:31:47 -0700558 if (x->coaddr)
559 NLA_PUT(skb, XFRMA_COADDR, sizeof(*x->coaddr), x->coaddr);
560
561 if (x->lastused)
562 NLA_PUT_U64(skb, XFRMA_LASTUSED, x->lastused);
Herbert Xu050f0092007-10-09 13:31:47 -0700563
Herbert Xu1a6509d2008-01-28 19:37:29 -0800564 if (x->aead)
565 NLA_PUT(skb, XFRMA_ALG_AEAD, aead_len(x->aead), x->aead);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 if (x->aalg)
Eric Dumazet0f99be02008-01-08 23:39:06 -0800567 NLA_PUT(skb, XFRMA_ALG_AUTH, xfrm_alg_len(x->aalg), x->aalg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 if (x->ealg)
Eric Dumazet0f99be02008-01-08 23:39:06 -0800569 NLA_PUT(skb, XFRMA_ALG_CRYPT, xfrm_alg_len(x->ealg), x->ealg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 if (x->calg)
Thomas Grafc0144be2007-08-22 13:55:43 -0700571 NLA_PUT(skb, XFRMA_ALG_COMP, sizeof(*(x->calg)), x->calg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572
573 if (x->encap)
Thomas Grafc0144be2007-08-22 13:55:43 -0700574 NLA_PUT(skb, XFRMA_ENCAP, sizeof(*x->encap), x->encap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575
Thomas Grafc0144be2007-08-22 13:55:43 -0700576 if (x->security && copy_sec_ctx(x->security, skb) < 0)
577 goto nla_put_failure;
Noriaki TAKAMIYA060f02a2006-08-23 18:18:55 -0700578
Herbert Xu68325d32007-10-09 13:30:57 -0700579 return 0;
580
581nla_put_failure:
582 return -EMSGSIZE;
583}
584
585static int dump_one_state(struct xfrm_state *x, int count, void *ptr)
586{
587 struct xfrm_dump_info *sp = ptr;
588 struct sk_buff *in_skb = sp->in_skb;
589 struct sk_buff *skb = sp->out_skb;
590 struct xfrm_usersa_info *p;
591 struct nlmsghdr *nlh;
592 int err;
593
Herbert Xu68325d32007-10-09 13:30:57 -0700594 nlh = nlmsg_put(skb, NETLINK_CB(in_skb).pid, sp->nlmsg_seq,
595 XFRM_MSG_NEWSA, sizeof(*p), sp->nlmsg_flags);
596 if (nlh == NULL)
597 return -EMSGSIZE;
598
599 p = nlmsg_data(nlh);
600
601 err = copy_to_user_state_extra(x, p, skb);
602 if (err)
603 goto nla_put_failure;
604
Thomas Graf98250692007-08-22 12:47:26 -0700605 nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 return 0;
607
Thomas Grafc0144be2007-08-22 13:55:43 -0700608nla_put_failure:
Thomas Graf98250692007-08-22 12:47:26 -0700609 nlmsg_cancel(skb, nlh);
Herbert Xu68325d32007-10-09 13:30:57 -0700610 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611}
612
Timo Teras4c563f72008-02-28 21:31:08 -0800613static int xfrm_dump_sa_done(struct netlink_callback *cb)
614{
615 struct xfrm_state_walk *walk = (struct xfrm_state_walk *) &cb->args[1];
616 xfrm_state_walk_done(walk);
617 return 0;
618}
619
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620static int xfrm_dump_sa(struct sk_buff *skb, struct netlink_callback *cb)
621{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800622 struct net *net = sock_net(skb->sk);
Timo Teras4c563f72008-02-28 21:31:08 -0800623 struct xfrm_state_walk *walk = (struct xfrm_state_walk *) &cb->args[1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 struct xfrm_dump_info info;
625
Timo Teras4c563f72008-02-28 21:31:08 -0800626 BUILD_BUG_ON(sizeof(struct xfrm_state_walk) >
627 sizeof(cb->args) - sizeof(cb->args[0]));
628
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 info.in_skb = cb->skb;
630 info.out_skb = skb;
631 info.nlmsg_seq = cb->nlh->nlmsg_seq;
632 info.nlmsg_flags = NLM_F_MULTI;
Timo Teras4c563f72008-02-28 21:31:08 -0800633
634 if (!cb->args[0]) {
635 cb->args[0] = 1;
636 xfrm_state_walk_init(walk, 0);
637 }
638
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800639 (void) xfrm_state_walk(net, walk, dump_one_state, &info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640
641 return skb->len;
642}
643
644static struct sk_buff *xfrm_state_netlink(struct sk_buff *in_skb,
645 struct xfrm_state *x, u32 seq)
646{
647 struct xfrm_dump_info info;
648 struct sk_buff *skb;
649
Thomas Graf7deb2262007-08-22 13:57:39 -0700650 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 if (!skb)
652 return ERR_PTR(-ENOMEM);
653
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 info.in_skb = in_skb;
655 info.out_skb = skb;
656 info.nlmsg_seq = seq;
657 info.nlmsg_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658
659 if (dump_one_state(x, 0, &info)) {
660 kfree_skb(skb);
661 return NULL;
662 }
663
664 return skb;
665}
666
Thomas Graf7deb2262007-08-22 13:57:39 -0700667static inline size_t xfrm_spdinfo_msgsize(void)
668{
669 return NLMSG_ALIGN(4)
670 + nla_total_size(sizeof(struct xfrmu_spdinfo))
671 + nla_total_size(sizeof(struct xfrmu_spdhinfo));
672}
673
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700674static int build_spdinfo(struct sk_buff *skb, u32 pid, u32 seq, u32 flags)
675{
Jamal Hadi Salim5a6d3412007-05-04 12:55:39 -0700676 struct xfrmk_spdinfo si;
677 struct xfrmu_spdinfo spc;
678 struct xfrmu_spdhinfo sph;
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700679 struct nlmsghdr *nlh;
680 u32 *f;
681
682 nlh = nlmsg_put(skb, pid, seq, XFRM_MSG_NEWSPDINFO, sizeof(u32), 0);
683 if (nlh == NULL) /* shouldnt really happen ... */
684 return -EMSGSIZE;
685
686 f = nlmsg_data(nlh);
687 *f = flags;
688 xfrm_spd_getinfo(&si);
Jamal Hadi Salim5a6d3412007-05-04 12:55:39 -0700689 spc.incnt = si.incnt;
690 spc.outcnt = si.outcnt;
691 spc.fwdcnt = si.fwdcnt;
692 spc.inscnt = si.inscnt;
693 spc.outscnt = si.outscnt;
694 spc.fwdscnt = si.fwdscnt;
695 sph.spdhcnt = si.spdhcnt;
696 sph.spdhmcnt = si.spdhmcnt;
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700697
Jamal Hadi Salim5a6d3412007-05-04 12:55:39 -0700698 NLA_PUT(skb, XFRMA_SPD_INFO, sizeof(spc), &spc);
699 NLA_PUT(skb, XFRMA_SPD_HINFO, sizeof(sph), &sph);
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700700
701 return nlmsg_end(skb, nlh);
702
703nla_put_failure:
704 nlmsg_cancel(skb, nlh);
705 return -EMSGSIZE;
706}
707
708static int xfrm_get_spdinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -0700709 struct nlattr **attrs)
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700710{
Alexey Dobriyana6483b72008-11-25 17:38:20 -0800711 struct net *net = sock_net(skb->sk);
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700712 struct sk_buff *r_skb;
Thomas Graf7b67c852007-08-22 13:53:52 -0700713 u32 *flags = nlmsg_data(nlh);
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700714 u32 spid = NETLINK_CB(skb).pid;
715 u32 seq = nlh->nlmsg_seq;
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700716
Thomas Graf7deb2262007-08-22 13:57:39 -0700717 r_skb = nlmsg_new(xfrm_spdinfo_msgsize(), GFP_ATOMIC);
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700718 if (r_skb == NULL)
719 return -ENOMEM;
720
721 if (build_spdinfo(r_skb, spid, seq, *flags) < 0)
722 BUG();
723
Alexey Dobriyana6483b72008-11-25 17:38:20 -0800724 return nlmsg_unicast(net->xfrm.nlsk, r_skb, spid);
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700725}
726
Thomas Graf7deb2262007-08-22 13:57:39 -0700727static inline size_t xfrm_sadinfo_msgsize(void)
728{
729 return NLMSG_ALIGN(4)
730 + nla_total_size(sizeof(struct xfrmu_sadhinfo))
731 + nla_total_size(4); /* XFRMA_SAD_CNT */
732}
733
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700734static int build_sadinfo(struct sk_buff *skb, u32 pid, u32 seq, u32 flags)
735{
Jamal Hadi Salimaf11e312007-05-04 12:55:13 -0700736 struct xfrmk_sadinfo si;
737 struct xfrmu_sadhinfo sh;
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700738 struct nlmsghdr *nlh;
739 u32 *f;
740
741 nlh = nlmsg_put(skb, pid, seq, XFRM_MSG_NEWSADINFO, sizeof(u32), 0);
742 if (nlh == NULL) /* shouldnt really happen ... */
743 return -EMSGSIZE;
744
745 f = nlmsg_data(nlh);
746 *f = flags;
747 xfrm_sad_getinfo(&si);
748
Jamal Hadi Salimaf11e312007-05-04 12:55:13 -0700749 sh.sadhmcnt = si.sadhmcnt;
750 sh.sadhcnt = si.sadhcnt;
751
752 NLA_PUT_U32(skb, XFRMA_SAD_CNT, si.sadcnt);
753 NLA_PUT(skb, XFRMA_SAD_HINFO, sizeof(sh), &sh);
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700754
755 return nlmsg_end(skb, nlh);
756
757nla_put_failure:
758 nlmsg_cancel(skb, nlh);
759 return -EMSGSIZE;
760}
761
762static int xfrm_get_sadinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -0700763 struct nlattr **attrs)
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700764{
Alexey Dobriyana6483b72008-11-25 17:38:20 -0800765 struct net *net = sock_net(skb->sk);
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700766 struct sk_buff *r_skb;
Thomas Graf7b67c852007-08-22 13:53:52 -0700767 u32 *flags = nlmsg_data(nlh);
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700768 u32 spid = NETLINK_CB(skb).pid;
769 u32 seq = nlh->nlmsg_seq;
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700770
Thomas Graf7deb2262007-08-22 13:57:39 -0700771 r_skb = nlmsg_new(xfrm_sadinfo_msgsize(), GFP_ATOMIC);
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700772 if (r_skb == NULL)
773 return -ENOMEM;
774
775 if (build_sadinfo(r_skb, spid, seq, *flags) < 0)
776 BUG();
777
Alexey Dobriyana6483b72008-11-25 17:38:20 -0800778 return nlmsg_unicast(net->xfrm.nlsk, r_skb, spid);
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700779}
780
Christoph Hellwig22e70052007-01-02 15:22:30 -0800781static int xfrm_get_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -0700782 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800784 struct net *net = sock_net(skb->sk);
Thomas Graf7b67c852007-08-22 13:53:52 -0700785 struct xfrm_usersa_id *p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 struct xfrm_state *x;
787 struct sk_buff *resp_skb;
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700788 int err = -ESRCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800790 x = xfrm_user_state_lookup(net, p, attrs, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 if (x == NULL)
792 goto out_noput;
793
794 resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
795 if (IS_ERR(resp_skb)) {
796 err = PTR_ERR(resp_skb);
797 } else {
Alexey Dobriyana6483b72008-11-25 17:38:20 -0800798 err = nlmsg_unicast(net->xfrm.nlsk, resp_skb, NETLINK_CB(skb).pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 }
800 xfrm_state_put(x);
801out_noput:
802 return err;
803}
804
805static int verify_userspi_info(struct xfrm_userspi_info *p)
806{
807 switch (p->info.id.proto) {
808 case IPPROTO_AH:
809 case IPPROTO_ESP:
810 break;
811
812 case IPPROTO_COMP:
813 /* IPCOMP spi is 16-bits. */
814 if (p->max >= 0x10000)
815 return -EINVAL;
816 break;
817
818 default:
819 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700820 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821
822 if (p->min > p->max)
823 return -EINVAL;
824
825 return 0;
826}
827
Christoph Hellwig22e70052007-01-02 15:22:30 -0800828static int xfrm_alloc_userspi(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -0700829 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800831 struct net *net = sock_net(skb->sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 struct xfrm_state *x;
833 struct xfrm_userspi_info *p;
834 struct sk_buff *resp_skb;
835 xfrm_address_t *daddr;
836 int family;
837 int err;
838
Thomas Graf7b67c852007-08-22 13:53:52 -0700839 p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 err = verify_userspi_info(p);
841 if (err)
842 goto out_noput;
843
844 family = p->info.family;
845 daddr = &p->info.id.daddr;
846
847 x = NULL;
848 if (p->info.seq) {
Alexey Dobriyana6483b72008-11-25 17:38:20 -0800849 x = xfrm_find_acq_byseq(net, p->info.seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 if (x && xfrm_addr_cmp(&x->id.daddr, daddr, family)) {
851 xfrm_state_put(x);
852 x = NULL;
853 }
854 }
855
856 if (!x)
Alexey Dobriyana6483b72008-11-25 17:38:20 -0800857 x = xfrm_find_acq(net, p->info.mode, p->info.reqid,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 p->info.id.proto, daddr,
859 &p->info.saddr, 1,
860 family);
861 err = -ENOENT;
862 if (x == NULL)
863 goto out_noput;
864
Herbert Xu658b2192007-10-09 13:29:52 -0700865 err = xfrm_alloc_spi(x, p->min, p->max);
866 if (err)
867 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868
Herbert Xu658b2192007-10-09 13:29:52 -0700869 resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 if (IS_ERR(resp_skb)) {
871 err = PTR_ERR(resp_skb);
872 goto out;
873 }
874
Alexey Dobriyana6483b72008-11-25 17:38:20 -0800875 err = nlmsg_unicast(net->xfrm.nlsk, resp_skb, NETLINK_CB(skb).pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876
877out:
878 xfrm_state_put(x);
879out_noput:
880 return err;
881}
882
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -0800883static int verify_policy_dir(u8 dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884{
885 switch (dir) {
886 case XFRM_POLICY_IN:
887 case XFRM_POLICY_OUT:
888 case XFRM_POLICY_FWD:
889 break;
890
891 default:
892 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700893 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894
895 return 0;
896}
897
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -0800898static int verify_policy_type(u8 type)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -0700899{
900 switch (type) {
901 case XFRM_POLICY_TYPE_MAIN:
902#ifdef CONFIG_XFRM_SUB_POLICY
903 case XFRM_POLICY_TYPE_SUB:
904#endif
905 break;
906
907 default:
908 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700909 }
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -0700910
911 return 0;
912}
913
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914static int verify_newpolicy_info(struct xfrm_userpolicy_info *p)
915{
916 switch (p->share) {
917 case XFRM_SHARE_ANY:
918 case XFRM_SHARE_SESSION:
919 case XFRM_SHARE_USER:
920 case XFRM_SHARE_UNIQUE:
921 break;
922
923 default:
924 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700925 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926
927 switch (p->action) {
928 case XFRM_POLICY_ALLOW:
929 case XFRM_POLICY_BLOCK:
930 break;
931
932 default:
933 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700934 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935
936 switch (p->sel.family) {
937 case AF_INET:
938 break;
939
940 case AF_INET6:
941#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
942 break;
943#else
944 return -EAFNOSUPPORT;
945#endif
946
947 default:
948 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700949 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950
951 return verify_policy_dir(p->dir);
952}
953
Thomas Graf5424f322007-08-22 14:01:33 -0700954static int copy_from_user_sec_ctx(struct xfrm_policy *pol, struct nlattr **attrs)
Trent Jaegerdf718372005-12-13 23:12:27 -0800955{
Thomas Graf5424f322007-08-22 14:01:33 -0700956 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
Trent Jaegerdf718372005-12-13 23:12:27 -0800957 struct xfrm_user_sec_ctx *uctx;
958
959 if (!rt)
960 return 0;
961
Thomas Graf5424f322007-08-22 14:01:33 -0700962 uctx = nla_data(rt);
Paul Moore03e1ad72008-04-12 19:07:52 -0700963 return security_xfrm_policy_alloc(&pol->security, uctx);
Trent Jaegerdf718372005-12-13 23:12:27 -0800964}
965
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966static void copy_templates(struct xfrm_policy *xp, struct xfrm_user_tmpl *ut,
967 int nr)
968{
969 int i;
970
971 xp->xfrm_nr = nr;
972 for (i = 0; i < nr; i++, ut++) {
973 struct xfrm_tmpl *t = &xp->xfrm_vec[i];
974
975 memcpy(&t->id, &ut->id, sizeof(struct xfrm_id));
976 memcpy(&t->saddr, &ut->saddr,
977 sizeof(xfrm_address_t));
978 t->reqid = ut->reqid;
979 t->mode = ut->mode;
980 t->share = ut->share;
981 t->optional = ut->optional;
982 t->aalgos = ut->aalgos;
983 t->ealgos = ut->ealgos;
984 t->calgos = ut->calgos;
Herbert Xuc5d18e92008-04-22 00:46:42 -0700985 /* If all masks are ~0, then we allow all algorithms. */
986 t->allalgs = !~(t->aalgos & t->ealgos & t->calgos);
Miika Komu8511d012006-11-30 16:40:51 -0800987 t->encap_family = ut->family;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 }
989}
990
David S. Millerb4ad86bf2006-12-03 19:19:26 -0800991static int validate_tmpl(int nr, struct xfrm_user_tmpl *ut, u16 family)
992{
993 int i;
994
995 if (nr > XFRM_MAX_DEPTH)
996 return -EINVAL;
997
998 for (i = 0; i < nr; i++) {
999 /* We never validated the ut->family value, so many
1000 * applications simply leave it at zero. The check was
1001 * never made and ut->family was ignored because all
1002 * templates could be assumed to have the same family as
1003 * the policy itself. Now that we will have ipv4-in-ipv6
1004 * and ipv6-in-ipv4 tunnels, this is no longer true.
1005 */
1006 if (!ut[i].family)
1007 ut[i].family = family;
1008
1009 switch (ut[i].family) {
1010 case AF_INET:
1011 break;
1012#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1013 case AF_INET6:
1014 break;
1015#endif
1016 default:
1017 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001018 }
David S. Millerb4ad86bf2006-12-03 19:19:26 -08001019 }
1020
1021 return 0;
1022}
1023
Thomas Graf5424f322007-08-22 14:01:33 -07001024static int copy_from_user_tmpl(struct xfrm_policy *pol, struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025{
Thomas Graf5424f322007-08-22 14:01:33 -07001026 struct nlattr *rt = attrs[XFRMA_TMPL];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027
1028 if (!rt) {
1029 pol->xfrm_nr = 0;
1030 } else {
Thomas Graf5424f322007-08-22 14:01:33 -07001031 struct xfrm_user_tmpl *utmpl = nla_data(rt);
1032 int nr = nla_len(rt) / sizeof(*utmpl);
David S. Millerb4ad86bf2006-12-03 19:19:26 -08001033 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034
David S. Millerb4ad86bf2006-12-03 19:19:26 -08001035 err = validate_tmpl(nr, utmpl, pol->family);
1036 if (err)
1037 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038
Thomas Graf5424f322007-08-22 14:01:33 -07001039 copy_templates(pol, utmpl, nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 }
1041 return 0;
1042}
1043
Thomas Graf5424f322007-08-22 14:01:33 -07001044static int copy_from_user_policy_type(u8 *tp, struct nlattr **attrs)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001045{
Thomas Graf5424f322007-08-22 14:01:33 -07001046 struct nlattr *rt = attrs[XFRMA_POLICY_TYPE];
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001047 struct xfrm_userpolicy_type *upt;
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001048 u8 type = XFRM_POLICY_TYPE_MAIN;
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001049 int err;
1050
1051 if (rt) {
Thomas Graf5424f322007-08-22 14:01:33 -07001052 upt = nla_data(rt);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001053 type = upt->type;
1054 }
1055
1056 err = verify_policy_type(type);
1057 if (err)
1058 return err;
1059
1060 *tp = type;
1061 return 0;
1062}
1063
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064static void copy_from_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p)
1065{
1066 xp->priority = p->priority;
1067 xp->index = p->index;
1068 memcpy(&xp->selector, &p->sel, sizeof(xp->selector));
1069 memcpy(&xp->lft, &p->lft, sizeof(xp->lft));
1070 xp->action = p->action;
1071 xp->flags = p->flags;
1072 xp->family = p->sel.family;
1073 /* XXX xp->share = p->share; */
1074}
1075
1076static void copy_to_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p, int dir)
1077{
1078 memcpy(&p->sel, &xp->selector, sizeof(p->sel));
1079 memcpy(&p->lft, &xp->lft, sizeof(p->lft));
1080 memcpy(&p->curlft, &xp->curlft, sizeof(p->curlft));
1081 p->priority = xp->priority;
1082 p->index = xp->index;
1083 p->sel.family = xp->family;
1084 p->dir = dir;
1085 p->action = xp->action;
1086 p->flags = xp->flags;
1087 p->share = XFRM_SHARE_ANY; /* XXX xp->share */
1088}
1089
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001090static struct xfrm_policy *xfrm_policy_construct(struct net *net, struct xfrm_userpolicy_info *p, struct nlattr **attrs, int *errp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001092 struct xfrm_policy *xp = xfrm_policy_alloc(net, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 int err;
1094
1095 if (!xp) {
1096 *errp = -ENOMEM;
1097 return NULL;
1098 }
1099
1100 copy_from_user_policy(xp, p);
Trent Jaegerdf718372005-12-13 23:12:27 -08001101
Thomas Graf35a7aa02007-08-22 14:00:40 -07001102 err = copy_from_user_policy_type(&xp->type, attrs);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001103 if (err)
1104 goto error;
1105
Thomas Graf35a7aa02007-08-22 14:00:40 -07001106 if (!(err = copy_from_user_tmpl(xp, attrs)))
1107 err = copy_from_user_sec_ctx(xp, attrs);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001108 if (err)
1109 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110
1111 return xp;
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001112 error:
1113 *errp = err;
Herbert Xu12a169e2008-10-01 07:03:24 -07001114 xp->walk.dead = 1;
WANG Cong64c31b32008-01-07 22:34:29 -08001115 xfrm_policy_destroy(xp);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001116 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117}
1118
Christoph Hellwig22e70052007-01-02 15:22:30 -08001119static int xfrm_add_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001120 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001122 struct net *net = sock_net(skb->sk);
Thomas Graf7b67c852007-08-22 13:53:52 -07001123 struct xfrm_userpolicy_info *p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 struct xfrm_policy *xp;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001125 struct km_event c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 int err;
1127 int excl;
Eric Paris25323862008-04-18 10:09:25 -04001128 uid_t loginuid = NETLINK_CB(skb).loginuid;
1129 u32 sessionid = NETLINK_CB(skb).sessionid;
1130 u32 sid = NETLINK_CB(skb).sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131
1132 err = verify_newpolicy_info(p);
1133 if (err)
1134 return err;
Thomas Graf35a7aa02007-08-22 14:00:40 -07001135 err = verify_sec_ctx_len(attrs);
Trent Jaegerdf718372005-12-13 23:12:27 -08001136 if (err)
1137 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001139 xp = xfrm_policy_construct(net, p, attrs, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 if (!xp)
1141 return err;
1142
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001143 /* shouldnt excl be based on nlh flags??
1144 * Aha! this is anti-netlink really i.e more pfkey derived
1145 * in netlink excl is a flag and you wouldnt need
1146 * a type XFRM_MSG_UPDPOLICY - JHS */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 excl = nlh->nlmsg_type == XFRM_MSG_NEWPOLICY;
1148 err = xfrm_policy_insert(p->dir, xp, excl);
Eric Paris25323862008-04-18 10:09:25 -04001149 xfrm_audit_policy_add(xp, err ? 0 : 1, loginuid, sessionid, sid);
Joy Latten161a09e2006-11-27 13:11:54 -06001150
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 if (err) {
Paul Moore03e1ad72008-04-12 19:07:52 -07001152 security_xfrm_policy_free(xp->security);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 kfree(xp);
1154 return err;
1155 }
1156
Herbert Xuf60f6b82005-06-18 22:44:37 -07001157 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001158 c.seq = nlh->nlmsg_seq;
1159 c.pid = nlh->nlmsg_pid;
1160 km_policy_notify(xp, p->dir, &c);
1161
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 xfrm_pol_put(xp);
1163
1164 return 0;
1165}
1166
1167static int copy_to_user_tmpl(struct xfrm_policy *xp, struct sk_buff *skb)
1168{
1169 struct xfrm_user_tmpl vec[XFRM_MAX_DEPTH];
1170 int i;
1171
1172 if (xp->xfrm_nr == 0)
1173 return 0;
1174
1175 for (i = 0; i < xp->xfrm_nr; i++) {
1176 struct xfrm_user_tmpl *up = &vec[i];
1177 struct xfrm_tmpl *kp = &xp->xfrm_vec[i];
1178
1179 memcpy(&up->id, &kp->id, sizeof(up->id));
Miika Komu8511d012006-11-30 16:40:51 -08001180 up->family = kp->encap_family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 memcpy(&up->saddr, &kp->saddr, sizeof(up->saddr));
1182 up->reqid = kp->reqid;
1183 up->mode = kp->mode;
1184 up->share = kp->share;
1185 up->optional = kp->optional;
1186 up->aalgos = kp->aalgos;
1187 up->ealgos = kp->ealgos;
1188 up->calgos = kp->calgos;
1189 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190
Thomas Grafc0144be2007-08-22 13:55:43 -07001191 return nla_put(skb, XFRMA_TMPL,
1192 sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr, vec);
Trent Jaegerdf718372005-12-13 23:12:27 -08001193}
1194
Serge Hallyn0d681622006-07-24 23:30:44 -07001195static inline int copy_to_user_state_sec_ctx(struct xfrm_state *x, struct sk_buff *skb)
1196{
1197 if (x->security) {
1198 return copy_sec_ctx(x->security, skb);
1199 }
1200 return 0;
1201}
1202
1203static inline int copy_to_user_sec_ctx(struct xfrm_policy *xp, struct sk_buff *skb)
1204{
1205 if (xp->security) {
1206 return copy_sec_ctx(xp->security, skb);
1207 }
1208 return 0;
1209}
Thomas Grafcfbfd452007-08-22 13:57:04 -07001210static inline size_t userpolicy_type_attrsize(void)
1211{
1212#ifdef CONFIG_XFRM_SUB_POLICY
1213 return nla_total_size(sizeof(struct xfrm_userpolicy_type));
1214#else
1215 return 0;
1216#endif
1217}
Serge Hallyn0d681622006-07-24 23:30:44 -07001218
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001219#ifdef CONFIG_XFRM_SUB_POLICY
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001220static int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001221{
Thomas Grafc0144be2007-08-22 13:55:43 -07001222 struct xfrm_userpolicy_type upt = {
1223 .type = type,
1224 };
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001225
Thomas Grafc0144be2007-08-22 13:55:43 -07001226 return nla_put(skb, XFRMA_POLICY_TYPE, sizeof(upt), &upt);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001227}
1228
1229#else
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001230static inline int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001231{
1232 return 0;
1233}
1234#endif
1235
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236static int dump_one_policy(struct xfrm_policy *xp, int dir, int count, void *ptr)
1237{
1238 struct xfrm_dump_info *sp = ptr;
1239 struct xfrm_userpolicy_info *p;
1240 struct sk_buff *in_skb = sp->in_skb;
1241 struct sk_buff *skb = sp->out_skb;
1242 struct nlmsghdr *nlh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243
Thomas Graf79b8b7f2007-08-22 12:46:53 -07001244 nlh = nlmsg_put(skb, NETLINK_CB(in_skb).pid, sp->nlmsg_seq,
1245 XFRM_MSG_NEWPOLICY, sizeof(*p), sp->nlmsg_flags);
1246 if (nlh == NULL)
1247 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248
Thomas Graf7b67c852007-08-22 13:53:52 -07001249 p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 copy_to_user_policy(xp, p, dir);
1251 if (copy_to_user_tmpl(xp, skb) < 0)
1252 goto nlmsg_failure;
Trent Jaegerdf718372005-12-13 23:12:27 -08001253 if (copy_to_user_sec_ctx(xp, skb))
1254 goto nlmsg_failure;
Jamal Hadi Salim1459bb32006-11-20 16:51:22 -08001255 if (copy_to_user_policy_type(xp->type, skb) < 0)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001256 goto nlmsg_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257
Thomas Graf98250692007-08-22 12:47:26 -07001258 nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 return 0;
1260
1261nlmsg_failure:
Thomas Graf98250692007-08-22 12:47:26 -07001262 nlmsg_cancel(skb, nlh);
1263 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264}
1265
Timo Teras4c563f72008-02-28 21:31:08 -08001266static int xfrm_dump_policy_done(struct netlink_callback *cb)
1267{
1268 struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *) &cb->args[1];
1269
1270 xfrm_policy_walk_done(walk);
1271 return 0;
1272}
1273
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274static int xfrm_dump_policy(struct sk_buff *skb, struct netlink_callback *cb)
1275{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001276 struct net *net = sock_net(skb->sk);
Timo Teras4c563f72008-02-28 21:31:08 -08001277 struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *) &cb->args[1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278 struct xfrm_dump_info info;
1279
Timo Teras4c563f72008-02-28 21:31:08 -08001280 BUILD_BUG_ON(sizeof(struct xfrm_policy_walk) >
1281 sizeof(cb->args) - sizeof(cb->args[0]));
1282
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 info.in_skb = cb->skb;
1284 info.out_skb = skb;
1285 info.nlmsg_seq = cb->nlh->nlmsg_seq;
1286 info.nlmsg_flags = NLM_F_MULTI;
Timo Teras4c563f72008-02-28 21:31:08 -08001287
1288 if (!cb->args[0]) {
1289 cb->args[0] = 1;
1290 xfrm_policy_walk_init(walk, XFRM_POLICY_TYPE_ANY);
1291 }
1292
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001293 (void) xfrm_policy_walk(net, walk, dump_one_policy, &info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294
1295 return skb->len;
1296}
1297
1298static struct sk_buff *xfrm_policy_netlink(struct sk_buff *in_skb,
1299 struct xfrm_policy *xp,
1300 int dir, u32 seq)
1301{
1302 struct xfrm_dump_info info;
1303 struct sk_buff *skb;
1304
Thomas Graf7deb2262007-08-22 13:57:39 -07001305 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 if (!skb)
1307 return ERR_PTR(-ENOMEM);
1308
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309 info.in_skb = in_skb;
1310 info.out_skb = skb;
1311 info.nlmsg_seq = seq;
1312 info.nlmsg_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313
1314 if (dump_one_policy(xp, dir, 0, &info) < 0) {
1315 kfree_skb(skb);
1316 return NULL;
1317 }
1318
1319 return skb;
1320}
1321
Christoph Hellwig22e70052007-01-02 15:22:30 -08001322static int xfrm_get_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001323 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001325 struct net *net = sock_net(skb->sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326 struct xfrm_policy *xp;
1327 struct xfrm_userpolicy_id *p;
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001328 u8 type = XFRM_POLICY_TYPE_MAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329 int err;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001330 struct km_event c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331 int delete;
1332
Thomas Graf7b67c852007-08-22 13:53:52 -07001333 p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334 delete = nlh->nlmsg_type == XFRM_MSG_DELPOLICY;
1335
Thomas Graf35a7aa02007-08-22 14:00:40 -07001336 err = copy_from_user_policy_type(&type, attrs);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001337 if (err)
1338 return err;
1339
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340 err = verify_policy_dir(p->dir);
1341 if (err)
1342 return err;
1343
1344 if (p->index)
Alexey Dobriyana6483b72008-11-25 17:38:20 -08001345 xp = xfrm_policy_byid(net, type, p->dir, p->index, delete, &err);
Trent Jaegerdf718372005-12-13 23:12:27 -08001346 else {
Thomas Graf5424f322007-08-22 14:01:33 -07001347 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
Paul Moore03e1ad72008-04-12 19:07:52 -07001348 struct xfrm_sec_ctx *ctx;
Trent Jaegerdf718372005-12-13 23:12:27 -08001349
Thomas Graf35a7aa02007-08-22 14:00:40 -07001350 err = verify_sec_ctx_len(attrs);
Trent Jaegerdf718372005-12-13 23:12:27 -08001351 if (err)
1352 return err;
1353
Denis V. Lunev2c8dd112008-04-14 14:47:48 -07001354 ctx = NULL;
Trent Jaegerdf718372005-12-13 23:12:27 -08001355 if (rt) {
Thomas Graf5424f322007-08-22 14:01:33 -07001356 struct xfrm_user_sec_ctx *uctx = nla_data(rt);
Trent Jaegerdf718372005-12-13 23:12:27 -08001357
Paul Moore03e1ad72008-04-12 19:07:52 -07001358 err = security_xfrm_policy_alloc(&ctx, uctx);
1359 if (err)
Trent Jaegerdf718372005-12-13 23:12:27 -08001360 return err;
Denis V. Lunev2c8dd112008-04-14 14:47:48 -07001361 }
Alexey Dobriyana6483b72008-11-25 17:38:20 -08001362 xp = xfrm_policy_bysel_ctx(net, type, p->dir, &p->sel, ctx,
Eric Parisef41aaa2007-03-07 15:37:58 -08001363 delete, &err);
Paul Moore03e1ad72008-04-12 19:07:52 -07001364 security_xfrm_policy_free(ctx);
Trent Jaegerdf718372005-12-13 23:12:27 -08001365 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 if (xp == NULL)
1367 return -ENOENT;
1368
1369 if (!delete) {
1370 struct sk_buff *resp_skb;
1371
1372 resp_skb = xfrm_policy_netlink(skb, xp, p->dir, nlh->nlmsg_seq);
1373 if (IS_ERR(resp_skb)) {
1374 err = PTR_ERR(resp_skb);
1375 } else {
Alexey Dobriyana6483b72008-11-25 17:38:20 -08001376 err = nlmsg_unicast(net->xfrm.nlsk, resp_skb,
Thomas Graf082a1ad2007-08-22 13:54:36 -07001377 NETLINK_CB(skb).pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378 }
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001379 } else {
Eric Paris25323862008-04-18 10:09:25 -04001380 uid_t loginuid = NETLINK_CB(skb).loginuid;
1381 u32 sessionid = NETLINK_CB(skb).sessionid;
1382 u32 sid = NETLINK_CB(skb).sid;
1383
1384 xfrm_audit_policy_delete(xp, err ? 0 : 1, loginuid, sessionid,
1385 sid);
David S. Miller13fcfbb2007-02-12 13:53:54 -08001386
1387 if (err != 0)
Catherine Zhangc8c05a82006-06-08 23:39:49 -07001388 goto out;
David S. Miller13fcfbb2007-02-12 13:53:54 -08001389
Herbert Xue7443892005-06-18 22:44:18 -07001390 c.data.byid = p->index;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001391 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001392 c.seq = nlh->nlmsg_seq;
1393 c.pid = nlh->nlmsg_pid;
1394 km_policy_notify(xp, p->dir, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395 }
1396
Catherine Zhangc8c05a82006-06-08 23:39:49 -07001397out:
Eric Parisef41aaa2007-03-07 15:37:58 -08001398 xfrm_pol_put(xp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399 return err;
1400}
1401
Christoph Hellwig22e70052007-01-02 15:22:30 -08001402static int xfrm_flush_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001403 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001405 struct net *net = sock_net(skb->sk);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001406 struct km_event c;
Thomas Graf7b67c852007-08-22 13:53:52 -07001407 struct xfrm_usersa_flush *p = nlmsg_data(nlh);
Joy Latten161a09e2006-11-27 13:11:54 -06001408 struct xfrm_audit audit_info;
Joy Latten4aa2e622007-06-04 19:05:57 -04001409 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410
Joy Latten161a09e2006-11-27 13:11:54 -06001411 audit_info.loginuid = NETLINK_CB(skb).loginuid;
Eric Paris25323862008-04-18 10:09:25 -04001412 audit_info.sessionid = NETLINK_CB(skb).sessionid;
Joy Latten161a09e2006-11-27 13:11:54 -06001413 audit_info.secid = NETLINK_CB(skb).sid;
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001414 err = xfrm_state_flush(net, p->proto, &audit_info);
Joy Latten4aa2e622007-06-04 19:05:57 -04001415 if (err)
1416 return err;
Herbert Xubf088672005-06-18 22:44:00 -07001417 c.data.proto = p->proto;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001418 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001419 c.seq = nlh->nlmsg_seq;
1420 c.pid = nlh->nlmsg_pid;
Alexey Dobriyan70678022008-11-25 17:50:36 -08001421 c.net = net;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001422 km_state_notify(NULL, &c);
1423
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424 return 0;
1425}
1426
Thomas Graf7deb2262007-08-22 13:57:39 -07001427static inline size_t xfrm_aevent_msgsize(void)
1428{
1429 return NLMSG_ALIGN(sizeof(struct xfrm_aevent_id))
1430 + nla_total_size(sizeof(struct xfrm_replay_state))
1431 + nla_total_size(sizeof(struct xfrm_lifetime_cur))
1432 + nla_total_size(4) /* XFRM_AE_RTHR */
1433 + nla_total_size(4); /* XFRM_AE_ETHR */
1434}
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001435
1436static int build_aevent(struct sk_buff *skb, struct xfrm_state *x, struct km_event *c)
1437{
1438 struct xfrm_aevent_id *id;
1439 struct nlmsghdr *nlh;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001440
Thomas Graf79b8b7f2007-08-22 12:46:53 -07001441 nlh = nlmsg_put(skb, c->pid, c->seq, XFRM_MSG_NEWAE, sizeof(*id), 0);
1442 if (nlh == NULL)
1443 return -EMSGSIZE;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001444
Thomas Graf7b67c852007-08-22 13:53:52 -07001445 id = nlmsg_data(nlh);
Jamal Hadi Salim2b5f6dc2006-12-02 22:22:25 -08001446 memcpy(&id->sa_id.daddr, &x->id.daddr,sizeof(x->id.daddr));
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001447 id->sa_id.spi = x->id.spi;
1448 id->sa_id.family = x->props.family;
1449 id->sa_id.proto = x->id.proto;
Jamal Hadi Salim2b5f6dc2006-12-02 22:22:25 -08001450 memcpy(&id->saddr, &x->props.saddr,sizeof(x->props.saddr));
1451 id->reqid = x->props.reqid;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001452 id->flags = c->data.aevent;
1453
Thomas Grafc0144be2007-08-22 13:55:43 -07001454 NLA_PUT(skb, XFRMA_REPLAY_VAL, sizeof(x->replay), &x->replay);
1455 NLA_PUT(skb, XFRMA_LTIME_VAL, sizeof(x->curlft), &x->curlft);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001456
Thomas Grafc0144be2007-08-22 13:55:43 -07001457 if (id->flags & XFRM_AE_RTHR)
1458 NLA_PUT_U32(skb, XFRMA_REPLAY_THRESH, x->replay_maxdiff);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001459
Thomas Grafc0144be2007-08-22 13:55:43 -07001460 if (id->flags & XFRM_AE_ETHR)
1461 NLA_PUT_U32(skb, XFRMA_ETIMER_THRESH,
1462 x->replay_maxage * 10 / HZ);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001463
Thomas Graf98250692007-08-22 12:47:26 -07001464 return nlmsg_end(skb, nlh);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001465
Thomas Grafc0144be2007-08-22 13:55:43 -07001466nla_put_failure:
Thomas Graf98250692007-08-22 12:47:26 -07001467 nlmsg_cancel(skb, nlh);
1468 return -EMSGSIZE;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001469}
1470
Christoph Hellwig22e70052007-01-02 15:22:30 -08001471static int xfrm_get_ae(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001472 struct nlattr **attrs)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001473{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001474 struct net *net = sock_net(skb->sk);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001475 struct xfrm_state *x;
1476 struct sk_buff *r_skb;
1477 int err;
1478 struct km_event c;
Thomas Graf7b67c852007-08-22 13:53:52 -07001479 struct xfrm_aevent_id *p = nlmsg_data(nlh);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001480 struct xfrm_usersa_id *id = &p->sa_id;
1481
Thomas Graf7deb2262007-08-22 13:57:39 -07001482 r_skb = nlmsg_new(xfrm_aevent_msgsize(), GFP_ATOMIC);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001483 if (r_skb == NULL)
1484 return -ENOMEM;
1485
Alexey Dobriyana6483b72008-11-25 17:38:20 -08001486 x = xfrm_state_lookup(net, &id->daddr, id->spi, id->proto, id->family);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001487 if (x == NULL) {
Patrick McHardyb08d5842007-02-27 09:57:37 -08001488 kfree_skb(r_skb);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001489 return -ESRCH;
1490 }
1491
1492 /*
1493 * XXX: is this lock really needed - none of the other
1494 * gets lock (the concern is things getting updated
1495 * while we are still reading) - jhs
1496 */
1497 spin_lock_bh(&x->lock);
1498 c.data.aevent = p->flags;
1499 c.seq = nlh->nlmsg_seq;
1500 c.pid = nlh->nlmsg_pid;
1501
1502 if (build_aevent(r_skb, x, &c) < 0)
1503 BUG();
Alexey Dobriyana6483b72008-11-25 17:38:20 -08001504 err = nlmsg_unicast(net->xfrm.nlsk, r_skb, NETLINK_CB(skb).pid);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001505 spin_unlock_bh(&x->lock);
1506 xfrm_state_put(x);
1507 return err;
1508}
1509
Christoph Hellwig22e70052007-01-02 15:22:30 -08001510static int xfrm_new_ae(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001511 struct nlattr **attrs)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001512{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001513 struct net *net = sock_net(skb->sk);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001514 struct xfrm_state *x;
1515 struct km_event c;
1516 int err = - EINVAL;
Thomas Graf7b67c852007-08-22 13:53:52 -07001517 struct xfrm_aevent_id *p = nlmsg_data(nlh);
Thomas Graf5424f322007-08-22 14:01:33 -07001518 struct nlattr *rp = attrs[XFRMA_REPLAY_VAL];
1519 struct nlattr *lt = attrs[XFRMA_LTIME_VAL];
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001520
1521 if (!lt && !rp)
1522 return err;
1523
1524 /* pedantic mode - thou shalt sayeth replaceth */
1525 if (!(nlh->nlmsg_flags&NLM_F_REPLACE))
1526 return err;
1527
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001528 x = xfrm_state_lookup(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 -08001529 if (x == NULL)
1530 return -ESRCH;
1531
1532 if (x->km.state != XFRM_STATE_VALID)
1533 goto out;
1534
1535 spin_lock_bh(&x->lock);
Thomas Graf35a7aa02007-08-22 14:00:40 -07001536 xfrm_update_ae_params(x, attrs);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001537 spin_unlock_bh(&x->lock);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001538
1539 c.event = nlh->nlmsg_type;
1540 c.seq = nlh->nlmsg_seq;
1541 c.pid = nlh->nlmsg_pid;
1542 c.data.aevent = XFRM_AE_CU;
1543 km_state_notify(x, &c);
1544 err = 0;
1545out:
1546 xfrm_state_put(x);
1547 return err;
1548}
1549
Christoph Hellwig22e70052007-01-02 15:22:30 -08001550static int xfrm_flush_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001551 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001553 struct net *net = sock_net(skb->sk);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001554 struct km_event c;
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001555 u8 type = XFRM_POLICY_TYPE_MAIN;
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001556 int err;
Joy Latten161a09e2006-11-27 13:11:54 -06001557 struct xfrm_audit audit_info;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001558
Thomas Graf35a7aa02007-08-22 14:00:40 -07001559 err = copy_from_user_policy_type(&type, attrs);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001560 if (err)
1561 return err;
1562
Joy Latten161a09e2006-11-27 13:11:54 -06001563 audit_info.loginuid = NETLINK_CB(skb).loginuid;
Eric Paris25323862008-04-18 10:09:25 -04001564 audit_info.sessionid = NETLINK_CB(skb).sessionid;
Joy Latten161a09e2006-11-27 13:11:54 -06001565 audit_info.secid = NETLINK_CB(skb).sid;
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001566 err = xfrm_policy_flush(net, type, &audit_info);
Joy Latten4aa2e622007-06-04 19:05:57 -04001567 if (err)
1568 return err;
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001569 c.data.type = type;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001570 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001571 c.seq = nlh->nlmsg_seq;
1572 c.pid = nlh->nlmsg_pid;
Alexey Dobriyan70678022008-11-25 17:50:36 -08001573 c.net = net;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001574 km_policy_notify(NULL, 0, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575 return 0;
1576}
1577
Christoph Hellwig22e70052007-01-02 15:22:30 -08001578static int xfrm_add_pol_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001579 struct nlattr **attrs)
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001580{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001581 struct net *net = sock_net(skb->sk);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001582 struct xfrm_policy *xp;
Thomas Graf7b67c852007-08-22 13:53:52 -07001583 struct xfrm_user_polexpire *up = nlmsg_data(nlh);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001584 struct xfrm_userpolicy_info *p = &up->pol;
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001585 u8 type = XFRM_POLICY_TYPE_MAIN;
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001586 int err = -ENOENT;
1587
Thomas Graf35a7aa02007-08-22 14:00:40 -07001588 err = copy_from_user_policy_type(&type, attrs);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001589 if (err)
1590 return err;
1591
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001592 if (p->index)
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001593 xp = xfrm_policy_byid(net, type, p->dir, p->index, 0, &err);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001594 else {
Thomas Graf5424f322007-08-22 14:01:33 -07001595 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
Paul Moore03e1ad72008-04-12 19:07:52 -07001596 struct xfrm_sec_ctx *ctx;
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001597
Thomas Graf35a7aa02007-08-22 14:00:40 -07001598 err = verify_sec_ctx_len(attrs);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001599 if (err)
1600 return err;
1601
Denis V. Lunev2c8dd112008-04-14 14:47:48 -07001602 ctx = NULL;
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001603 if (rt) {
Thomas Graf5424f322007-08-22 14:01:33 -07001604 struct xfrm_user_sec_ctx *uctx = nla_data(rt);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001605
Paul Moore03e1ad72008-04-12 19:07:52 -07001606 err = security_xfrm_policy_alloc(&ctx, uctx);
1607 if (err)
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001608 return err;
Denis V. Lunev2c8dd112008-04-14 14:47:48 -07001609 }
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001610 xp = xfrm_policy_bysel_ctx(net, type, p->dir, &p->sel, ctx, 0, &err);
Paul Moore03e1ad72008-04-12 19:07:52 -07001611 security_xfrm_policy_free(ctx);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001612 }
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001613 if (xp == NULL)
Eric Parisef41aaa2007-03-07 15:37:58 -08001614 return -ENOENT;
Paul Moore03e1ad72008-04-12 19:07:52 -07001615
Eric Parisef41aaa2007-03-07 15:37:58 -08001616 read_lock(&xp->lock);
Herbert Xu12a169e2008-10-01 07:03:24 -07001617 if (xp->walk.dead) {
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001618 read_unlock(&xp->lock);
1619 goto out;
1620 }
1621
1622 read_unlock(&xp->lock);
1623 err = 0;
1624 if (up->hard) {
Eric Paris25323862008-04-18 10:09:25 -04001625 uid_t loginuid = NETLINK_CB(skb).loginuid;
1626 uid_t sessionid = NETLINK_CB(skb).sessionid;
1627 u32 sid = NETLINK_CB(skb).sid;
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001628 xfrm_policy_delete(xp, p->dir);
Eric Paris25323862008-04-18 10:09:25 -04001629 xfrm_audit_policy_delete(xp, 1, loginuid, sessionid, sid);
Joy Latten161a09e2006-11-27 13:11:54 -06001630
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001631 } else {
1632 // reset the timers here?
1633 printk("Dont know what to do with soft policy expire\n");
1634 }
1635 km_policy_expired(xp, p->dir, up->hard, current->pid);
1636
1637out:
1638 xfrm_pol_put(xp);
1639 return err;
1640}
1641
Christoph Hellwig22e70052007-01-02 15:22:30 -08001642static int xfrm_add_sa_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001643 struct nlattr **attrs)
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001644{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001645 struct net *net = sock_net(skb->sk);
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001646 struct xfrm_state *x;
1647 int err;
Thomas Graf7b67c852007-08-22 13:53:52 -07001648 struct xfrm_user_expire *ue = nlmsg_data(nlh);
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001649 struct xfrm_usersa_info *p = &ue->state;
1650
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001651 x = xfrm_state_lookup(net, &p->id.daddr, p->id.spi, p->id.proto, p->family);
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001652
David S. Miller3a765aa2007-02-26 14:52:21 -08001653 err = -ENOENT;
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001654 if (x == NULL)
1655 return err;
1656
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001657 spin_lock_bh(&x->lock);
David S. Miller3a765aa2007-02-26 14:52:21 -08001658 err = -EINVAL;
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001659 if (x->km.state != XFRM_STATE_VALID)
1660 goto out;
1661 km_state_expired(x, ue->hard, current->pid);
1662
Joy Latten161a09e2006-11-27 13:11:54 -06001663 if (ue->hard) {
Eric Paris25323862008-04-18 10:09:25 -04001664 uid_t loginuid = NETLINK_CB(skb).loginuid;
1665 uid_t sessionid = NETLINK_CB(skb).sessionid;
1666 u32 sid = NETLINK_CB(skb).sid;
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001667 __xfrm_state_delete(x);
Eric Paris25323862008-04-18 10:09:25 -04001668 xfrm_audit_state_delete(x, 1, loginuid, sessionid, sid);
Joy Latten161a09e2006-11-27 13:11:54 -06001669 }
David S. Miller3a765aa2007-02-26 14:52:21 -08001670 err = 0;
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001671out:
1672 spin_unlock_bh(&x->lock);
1673 xfrm_state_put(x);
1674 return err;
1675}
1676
Christoph Hellwig22e70052007-01-02 15:22:30 -08001677static int xfrm_add_acquire(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001678 struct nlattr **attrs)
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001679{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001680 struct net *net = sock_net(skb->sk);
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001681 struct xfrm_policy *xp;
1682 struct xfrm_user_tmpl *ut;
1683 int i;
Thomas Graf5424f322007-08-22 14:01:33 -07001684 struct nlattr *rt = attrs[XFRMA_TMPL];
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001685
Thomas Graf7b67c852007-08-22 13:53:52 -07001686 struct xfrm_user_acquire *ua = nlmsg_data(nlh);
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001687 struct xfrm_state *x = xfrm_state_alloc(net);
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001688 int err = -ENOMEM;
1689
1690 if (!x)
Ilpo Järvinend8eb9302008-12-14 23:16:22 -08001691 goto nomem;
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001692
1693 err = verify_newpolicy_info(&ua->policy);
Ilpo Järvinend8eb9302008-12-14 23:16:22 -08001694 if (err)
1695 goto bad_policy;
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001696
1697 /* build an XP */
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001698 xp = xfrm_policy_construct(net, &ua->policy, attrs, &err);
Ilpo Järvinend8eb9302008-12-14 23:16:22 -08001699 if (!xp)
1700 goto free_state;
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001701
1702 memcpy(&x->id, &ua->id, sizeof(ua->id));
1703 memcpy(&x->props.saddr, &ua->saddr, sizeof(ua->saddr));
1704 memcpy(&x->sel, &ua->sel, sizeof(ua->sel));
1705
Thomas Graf5424f322007-08-22 14:01:33 -07001706 ut = nla_data(rt);
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001707 /* extract the templates and for each call km_key */
1708 for (i = 0; i < xp->xfrm_nr; i++, ut++) {
1709 struct xfrm_tmpl *t = &xp->xfrm_vec[i];
1710 memcpy(&x->id, &t->id, sizeof(x->id));
1711 x->props.mode = t->mode;
1712 x->props.reqid = t->reqid;
1713 x->props.family = ut->family;
1714 t->aalgos = ua->aalgos;
1715 t->ealgos = ua->ealgos;
1716 t->calgos = ua->calgos;
1717 err = km_query(x, t, xp);
1718
1719 }
1720
1721 kfree(x);
1722 kfree(xp);
1723
1724 return 0;
Ilpo Järvinend8eb9302008-12-14 23:16:22 -08001725
1726bad_policy:
1727 printk("BAD policy passed\n");
1728free_state:
1729 kfree(x);
1730nomem:
1731 return err;
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001732}
1733
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001734#ifdef CONFIG_XFRM_MIGRATE
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001735static int copy_from_user_migrate(struct xfrm_migrate *ma,
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07001736 struct xfrm_kmaddress *k,
Thomas Graf5424f322007-08-22 14:01:33 -07001737 struct nlattr **attrs, int *num)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001738{
Thomas Graf5424f322007-08-22 14:01:33 -07001739 struct nlattr *rt = attrs[XFRMA_MIGRATE];
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001740 struct xfrm_user_migrate *um;
1741 int i, num_migrate;
1742
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07001743 if (k != NULL) {
1744 struct xfrm_user_kmaddress *uk;
1745
1746 uk = nla_data(attrs[XFRMA_KMADDRESS]);
1747 memcpy(&k->local, &uk->local, sizeof(k->local));
1748 memcpy(&k->remote, &uk->remote, sizeof(k->remote));
1749 k->family = uk->family;
1750 k->reserved = uk->reserved;
1751 }
1752
Thomas Graf5424f322007-08-22 14:01:33 -07001753 um = nla_data(rt);
1754 num_migrate = nla_len(rt) / sizeof(*um);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001755
1756 if (num_migrate <= 0 || num_migrate > XFRM_MAX_DEPTH)
1757 return -EINVAL;
1758
1759 for (i = 0; i < num_migrate; i++, um++, ma++) {
1760 memcpy(&ma->old_daddr, &um->old_daddr, sizeof(ma->old_daddr));
1761 memcpy(&ma->old_saddr, &um->old_saddr, sizeof(ma->old_saddr));
1762 memcpy(&ma->new_daddr, &um->new_daddr, sizeof(ma->new_daddr));
1763 memcpy(&ma->new_saddr, &um->new_saddr, sizeof(ma->new_saddr));
1764
1765 ma->proto = um->proto;
1766 ma->mode = um->mode;
1767 ma->reqid = um->reqid;
1768
1769 ma->old_family = um->old_family;
1770 ma->new_family = um->new_family;
1771 }
1772
1773 *num = i;
1774 return 0;
1775}
1776
1777static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001778 struct nlattr **attrs)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001779{
Thomas Graf7b67c852007-08-22 13:53:52 -07001780 struct xfrm_userpolicy_id *pi = nlmsg_data(nlh);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001781 struct xfrm_migrate m[XFRM_MAX_DEPTH];
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07001782 struct xfrm_kmaddress km, *kmp;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001783 u8 type;
1784 int err;
1785 int n = 0;
1786
Thomas Graf35a7aa02007-08-22 14:00:40 -07001787 if (attrs[XFRMA_MIGRATE] == NULL)
Thomas Grafcf5cb792007-08-22 13:59:04 -07001788 return -EINVAL;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001789
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07001790 kmp = attrs[XFRMA_KMADDRESS] ? &km : NULL;
1791
Thomas Graf5424f322007-08-22 14:01:33 -07001792 err = copy_from_user_policy_type(&type, attrs);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001793 if (err)
1794 return err;
1795
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07001796 err = copy_from_user_migrate((struct xfrm_migrate *)m, kmp, attrs, &n);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001797 if (err)
1798 return err;
1799
1800 if (!n)
1801 return 0;
1802
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07001803 xfrm_migrate(&pi->sel, pi->dir, type, m, n, kmp);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001804
1805 return 0;
1806}
1807#else
1808static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001809 struct nlattr **attrs)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001810{
1811 return -ENOPROTOOPT;
1812}
1813#endif
1814
1815#ifdef CONFIG_XFRM_MIGRATE
1816static int copy_to_user_migrate(struct xfrm_migrate *m, struct sk_buff *skb)
1817{
1818 struct xfrm_user_migrate um;
1819
1820 memset(&um, 0, sizeof(um));
1821 um.proto = m->proto;
1822 um.mode = m->mode;
1823 um.reqid = m->reqid;
1824 um.old_family = m->old_family;
1825 memcpy(&um.old_daddr, &m->old_daddr, sizeof(um.old_daddr));
1826 memcpy(&um.old_saddr, &m->old_saddr, sizeof(um.old_saddr));
1827 um.new_family = m->new_family;
1828 memcpy(&um.new_daddr, &m->new_daddr, sizeof(um.new_daddr));
1829 memcpy(&um.new_saddr, &m->new_saddr, sizeof(um.new_saddr));
1830
Thomas Grafc0144be2007-08-22 13:55:43 -07001831 return nla_put(skb, XFRMA_MIGRATE, sizeof(um), &um);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001832}
1833
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07001834static int copy_to_user_kmaddress(struct xfrm_kmaddress *k, struct sk_buff *skb)
1835{
1836 struct xfrm_user_kmaddress uk;
1837
1838 memset(&uk, 0, sizeof(uk));
1839 uk.family = k->family;
1840 uk.reserved = k->reserved;
1841 memcpy(&uk.local, &k->local, sizeof(uk.local));
Arnaud Ebalarda1caa322008-11-03 01:30:23 -08001842 memcpy(&uk.remote, &k->remote, sizeof(uk.remote));
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07001843
1844 return nla_put(skb, XFRMA_KMADDRESS, sizeof(uk), &uk);
1845}
1846
1847static inline size_t xfrm_migrate_msgsize(int num_migrate, int with_kma)
Thomas Graf7deb2262007-08-22 13:57:39 -07001848{
1849 return NLMSG_ALIGN(sizeof(struct xfrm_userpolicy_id))
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07001850 + (with_kma ? nla_total_size(sizeof(struct xfrm_kmaddress)) : 0)
1851 + nla_total_size(sizeof(struct xfrm_user_migrate) * num_migrate)
1852 + userpolicy_type_attrsize();
Thomas Graf7deb2262007-08-22 13:57:39 -07001853}
1854
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001855static int build_migrate(struct sk_buff *skb, struct xfrm_migrate *m,
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07001856 int num_migrate, struct xfrm_kmaddress *k,
1857 struct xfrm_selector *sel, u8 dir, u8 type)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001858{
1859 struct xfrm_migrate *mp;
1860 struct xfrm_userpolicy_id *pol_id;
1861 struct nlmsghdr *nlh;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001862 int i;
1863
Thomas Graf79b8b7f2007-08-22 12:46:53 -07001864 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_MIGRATE, sizeof(*pol_id), 0);
1865 if (nlh == NULL)
1866 return -EMSGSIZE;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001867
Thomas Graf7b67c852007-08-22 13:53:52 -07001868 pol_id = nlmsg_data(nlh);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001869 /* copy data from selector, dir, and type to the pol_id */
1870 memset(pol_id, 0, sizeof(*pol_id));
1871 memcpy(&pol_id->sel, sel, sizeof(pol_id->sel));
1872 pol_id->dir = dir;
1873
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07001874 if (k != NULL && (copy_to_user_kmaddress(k, skb) < 0))
1875 goto nlmsg_failure;
1876
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001877 if (copy_to_user_policy_type(type, skb) < 0)
1878 goto nlmsg_failure;
1879
1880 for (i = 0, mp = m ; i < num_migrate; i++, mp++) {
1881 if (copy_to_user_migrate(mp, skb) < 0)
1882 goto nlmsg_failure;
1883 }
1884
Thomas Graf98250692007-08-22 12:47:26 -07001885 return nlmsg_end(skb, nlh);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001886nlmsg_failure:
Thomas Graf98250692007-08-22 12:47:26 -07001887 nlmsg_cancel(skb, nlh);
1888 return -EMSGSIZE;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001889}
1890
1891static int xfrm_send_migrate(struct xfrm_selector *sel, u8 dir, u8 type,
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07001892 struct xfrm_migrate *m, int num_migrate,
1893 struct xfrm_kmaddress *k)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001894{
Alexey Dobriyana6483b72008-11-25 17:38:20 -08001895 struct net *net = &init_net;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001896 struct sk_buff *skb;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001897
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07001898 skb = nlmsg_new(xfrm_migrate_msgsize(num_migrate, !!k), GFP_ATOMIC);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001899 if (skb == NULL)
1900 return -ENOMEM;
1901
1902 /* build migrate */
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07001903 if (build_migrate(skb, m, num_migrate, k, sel, dir, type) < 0)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001904 BUG();
1905
Alexey Dobriyana6483b72008-11-25 17:38:20 -08001906 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_MIGRATE, GFP_ATOMIC);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001907}
1908#else
1909static int xfrm_send_migrate(struct xfrm_selector *sel, u8 dir, u8 type,
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07001910 struct xfrm_migrate *m, int num_migrate,
1911 struct xfrm_kmaddress *k)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001912{
1913 return -ENOPROTOOPT;
1914}
1915#endif
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001916
Thomas Grafa7bd9a42007-08-22 13:58:18 -07001917#define XMSGSIZE(type) sizeof(struct type)
Thomas Graf492b5582005-05-03 14:26:40 -07001918
1919static const int xfrm_msg_min[XFRM_NR_MSGTYPES] = {
David S. Miller66f9a252009-01-20 09:49:51 -08001920 [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
Thomas Graf492b5582005-05-03 14:26:40 -07001921 [XFRM_MSG_DELSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
1922 [XFRM_MSG_GETSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
1923 [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
1924 [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
1925 [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
1926 [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userspi_info),
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001927 [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_acquire),
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001928 [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_expire),
Thomas Graf492b5582005-05-03 14:26:40 -07001929 [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
David S. Miller66f9a252009-01-20 09:49:51 -08001930 [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001931 [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_polexpire),
Thomas Graf492b5582005-05-03 14:26:40 -07001932 [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_flush),
Thomas Grafa7bd9a42007-08-22 13:58:18 -07001933 [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = 0,
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001934 [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
1935 [XFRM_MSG_GETAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07001936 [XFRM_MSG_REPORT - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_report),
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001937 [XFRM_MSG_MIGRATE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
Thomas Grafa7bd9a42007-08-22 13:58:18 -07001938 [XFRM_MSG_GETSADINFO - XFRM_MSG_BASE] = sizeof(u32),
1939 [XFRM_MSG_GETSPDINFO - XFRM_MSG_BASE] = sizeof(u32),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940};
1941
Thomas Graf492b5582005-05-03 14:26:40 -07001942#undef XMSGSIZE
1943
Thomas Grafcf5cb792007-08-22 13:59:04 -07001944static const struct nla_policy xfrma_policy[XFRMA_MAX+1] = {
Herbert Xu1a6509d2008-01-28 19:37:29 -08001945 [XFRMA_ALG_AEAD] = { .len = sizeof(struct xfrm_algo_aead) },
Thomas Grafcf5cb792007-08-22 13:59:04 -07001946 [XFRMA_ALG_AUTH] = { .len = sizeof(struct xfrm_algo) },
1947 [XFRMA_ALG_CRYPT] = { .len = sizeof(struct xfrm_algo) },
1948 [XFRMA_ALG_COMP] = { .len = sizeof(struct xfrm_algo) },
1949 [XFRMA_ENCAP] = { .len = sizeof(struct xfrm_encap_tmpl) },
1950 [XFRMA_TMPL] = { .len = sizeof(struct xfrm_user_tmpl) },
1951 [XFRMA_SEC_CTX] = { .len = sizeof(struct xfrm_sec_ctx) },
1952 [XFRMA_LTIME_VAL] = { .len = sizeof(struct xfrm_lifetime_cur) },
1953 [XFRMA_REPLAY_VAL] = { .len = sizeof(struct xfrm_replay_state) },
1954 [XFRMA_REPLAY_THRESH] = { .type = NLA_U32 },
1955 [XFRMA_ETIMER_THRESH] = { .type = NLA_U32 },
1956 [XFRMA_SRCADDR] = { .len = sizeof(xfrm_address_t) },
1957 [XFRMA_COADDR] = { .len = sizeof(xfrm_address_t) },
1958 [XFRMA_POLICY_TYPE] = { .len = sizeof(struct xfrm_userpolicy_type)},
1959 [XFRMA_MIGRATE] = { .len = sizeof(struct xfrm_user_migrate) },
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07001960 [XFRMA_KMADDRESS] = { .len = sizeof(struct xfrm_user_kmaddress) },
Thomas Grafcf5cb792007-08-22 13:59:04 -07001961};
1962
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963static struct xfrm_link {
Thomas Graf5424f322007-08-22 14:01:33 -07001964 int (*doit)(struct sk_buff *, struct nlmsghdr *, struct nlattr **);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965 int (*dump)(struct sk_buff *, struct netlink_callback *);
Timo Teras4c563f72008-02-28 21:31:08 -08001966 int (*done)(struct netlink_callback *);
Thomas Graf492b5582005-05-03 14:26:40 -07001967} xfrm_dispatch[XFRM_NR_MSGTYPES] = {
1968 [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa },
1969 [XFRM_MSG_DELSA - XFRM_MSG_BASE] = { .doit = xfrm_del_sa },
1970 [XFRM_MSG_GETSA - XFRM_MSG_BASE] = { .doit = xfrm_get_sa,
Timo Teras4c563f72008-02-28 21:31:08 -08001971 .dump = xfrm_dump_sa,
1972 .done = xfrm_dump_sa_done },
Thomas Graf492b5582005-05-03 14:26:40 -07001973 [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy },
1974 [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy },
1975 [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy,
Timo Teras4c563f72008-02-28 21:31:08 -08001976 .dump = xfrm_dump_policy,
1977 .done = xfrm_dump_policy_done },
Thomas Graf492b5582005-05-03 14:26:40 -07001978 [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = { .doit = xfrm_alloc_userspi },
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001979 [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_acquire },
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001980 [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_sa_expire },
Thomas Graf492b5582005-05-03 14:26:40 -07001981 [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy },
1982 [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa },
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001983 [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_pol_expire},
Thomas Graf492b5582005-05-03 14:26:40 -07001984 [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = { .doit = xfrm_flush_sa },
1985 [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_flush_policy },
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001986 [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = { .doit = xfrm_new_ae },
1987 [XFRM_MSG_GETAE - XFRM_MSG_BASE] = { .doit = xfrm_get_ae },
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001988 [XFRM_MSG_MIGRATE - XFRM_MSG_BASE] = { .doit = xfrm_do_migrate },
Jamal Hadi Salim566ec032007-04-26 14:12:15 -07001989 [XFRM_MSG_GETSADINFO - XFRM_MSG_BASE] = { .doit = xfrm_get_sadinfo },
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -07001990 [XFRM_MSG_GETSPDINFO - XFRM_MSG_BASE] = { .doit = xfrm_get_spdinfo },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991};
1992
Thomas Graf1d00a4e2007-03-22 23:30:12 -07001993static int xfrm_user_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994{
Alexey Dobriyana6483b72008-11-25 17:38:20 -08001995 struct net *net = sock_net(skb->sk);
Thomas Graf35a7aa02007-08-22 14:00:40 -07001996 struct nlattr *attrs[XFRMA_MAX+1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997 struct xfrm_link *link;
Thomas Grafa7bd9a42007-08-22 13:58:18 -07001998 int type, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000 type = nlh->nlmsg_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001 if (type > XFRM_MSG_MAX)
Thomas Graf1d00a4e2007-03-22 23:30:12 -07002002 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003
2004 type -= XFRM_MSG_BASE;
2005 link = &xfrm_dispatch[type];
2006
2007 /* All operations require privileges, even GET */
Thomas Graf1d00a4e2007-03-22 23:30:12 -07002008 if (security_netlink_recv(skb, CAP_NET_ADMIN))
2009 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010
Thomas Graf492b5582005-05-03 14:26:40 -07002011 if ((type == (XFRM_MSG_GETSA - XFRM_MSG_BASE) ||
2012 type == (XFRM_MSG_GETPOLICY - XFRM_MSG_BASE)) &&
2013 (nlh->nlmsg_flags & NLM_F_DUMP)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014 if (link->dump == NULL)
Thomas Graf1d00a4e2007-03-22 23:30:12 -07002015 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002017 return netlink_dump_start(net->xfrm.nlsk, skb, nlh, link->dump, link->done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018 }
2019
Thomas Graf35a7aa02007-08-22 14:00:40 -07002020 err = nlmsg_parse(nlh, xfrm_msg_min[type], attrs, XFRMA_MAX,
Thomas Grafcf5cb792007-08-22 13:59:04 -07002021 xfrma_policy);
Thomas Grafa7bd9a42007-08-22 13:58:18 -07002022 if (err < 0)
2023 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024
2025 if (link->doit == NULL)
Thomas Graf1d00a4e2007-03-22 23:30:12 -07002026 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027
Thomas Graf5424f322007-08-22 14:01:33 -07002028 return link->doit(skb, nlh, attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029}
2030
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07002031static void xfrm_netlink_rcv(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032{
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07002033 mutex_lock(&xfrm_cfg_mutex);
2034 netlink_rcv_skb(skb, &xfrm_user_rcv_msg);
2035 mutex_unlock(&xfrm_cfg_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036}
2037
Thomas Graf7deb2262007-08-22 13:57:39 -07002038static inline size_t xfrm_expire_msgsize(void)
2039{
2040 return NLMSG_ALIGN(sizeof(struct xfrm_user_expire));
2041}
2042
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002043static int build_expire(struct sk_buff *skb, struct xfrm_state *x, struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044{
2045 struct xfrm_user_expire *ue;
2046 struct nlmsghdr *nlh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002048 nlh = nlmsg_put(skb, c->pid, 0, XFRM_MSG_EXPIRE, sizeof(*ue), 0);
2049 if (nlh == NULL)
2050 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051
Thomas Graf7b67c852007-08-22 13:53:52 -07002052 ue = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053 copy_to_user_state(x, &ue->state);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002054 ue->hard = (c->data.hard != 0) ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055
Thomas Graf98250692007-08-22 12:47:26 -07002056 return nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057}
2058
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002059static int xfrm_exp_state_notify(struct xfrm_state *x, struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002061 struct net *net = xs_net(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062 struct sk_buff *skb;
2063
Thomas Graf7deb2262007-08-22 13:57:39 -07002064 skb = nlmsg_new(xfrm_expire_msgsize(), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065 if (skb == NULL)
2066 return -ENOMEM;
2067
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002068 if (build_expire(skb, x, c) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069 BUG();
2070
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002071 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_EXPIRE, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072}
2073
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002074static int xfrm_aevent_state_notify(struct xfrm_state *x, struct km_event *c)
2075{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002076 struct net *net = xs_net(x);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002077 struct sk_buff *skb;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002078
Thomas Graf7deb2262007-08-22 13:57:39 -07002079 skb = nlmsg_new(xfrm_aevent_msgsize(), GFP_ATOMIC);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002080 if (skb == NULL)
2081 return -ENOMEM;
2082
2083 if (build_aevent(skb, x, c) < 0)
2084 BUG();
2085
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002086 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_AEVENTS, GFP_ATOMIC);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002087}
2088
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002089static int xfrm_notify_sa_flush(struct km_event *c)
2090{
Alexey Dobriyan70678022008-11-25 17:50:36 -08002091 struct net *net = c->net;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002092 struct xfrm_usersa_flush *p;
2093 struct nlmsghdr *nlh;
2094 struct sk_buff *skb;
Thomas Graf7deb2262007-08-22 13:57:39 -07002095 int len = NLMSG_ALIGN(sizeof(struct xfrm_usersa_flush));
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002096
Thomas Graf7deb2262007-08-22 13:57:39 -07002097 skb = nlmsg_new(len, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002098 if (skb == NULL)
2099 return -ENOMEM;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002100
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002101 nlh = nlmsg_put(skb, c->pid, c->seq, XFRM_MSG_FLUSHSA, sizeof(*p), 0);
2102 if (nlh == NULL) {
2103 kfree_skb(skb);
2104 return -EMSGSIZE;
2105 }
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002106
Thomas Graf7b67c852007-08-22 13:53:52 -07002107 p = nlmsg_data(nlh);
Herbert Xubf088672005-06-18 22:44:00 -07002108 p->proto = c->data.proto;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002109
Thomas Graf98250692007-08-22 12:47:26 -07002110 nlmsg_end(skb, nlh);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002111
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002112 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_SA, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002113}
2114
Thomas Graf7deb2262007-08-22 13:57:39 -07002115static inline size_t xfrm_sa_len(struct xfrm_state *x)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002116{
Thomas Graf7deb2262007-08-22 13:57:39 -07002117 size_t l = 0;
Herbert Xu1a6509d2008-01-28 19:37:29 -08002118 if (x->aead)
2119 l += nla_total_size(aead_len(x->aead));
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002120 if (x->aalg)
Eric Dumazet0f99be02008-01-08 23:39:06 -08002121 l += nla_total_size(xfrm_alg_len(x->aalg));
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002122 if (x->ealg)
Eric Dumazet0f99be02008-01-08 23:39:06 -08002123 l += nla_total_size(xfrm_alg_len(x->ealg));
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002124 if (x->calg)
Thomas Graf7deb2262007-08-22 13:57:39 -07002125 l += nla_total_size(sizeof(*x->calg));
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002126 if (x->encap)
Thomas Graf7deb2262007-08-22 13:57:39 -07002127 l += nla_total_size(sizeof(*x->encap));
Herbert Xu68325d32007-10-09 13:30:57 -07002128 if (x->security)
2129 l += nla_total_size(sizeof(struct xfrm_user_sec_ctx) +
2130 x->security->ctx_len);
2131 if (x->coaddr)
2132 l += nla_total_size(sizeof(*x->coaddr));
2133
Herbert Xud26f3982007-11-13 21:47:08 -08002134 /* Must count x->lastused as it may become non-zero behind our back. */
2135 l += nla_total_size(sizeof(u64));
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002136
2137 return l;
2138}
2139
2140static int xfrm_notify_sa(struct xfrm_state *x, struct km_event *c)
2141{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002142 struct net *net = xs_net(x);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002143 struct xfrm_usersa_info *p;
Herbert Xu0603eac2005-06-18 22:54:36 -07002144 struct xfrm_usersa_id *id;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002145 struct nlmsghdr *nlh;
2146 struct sk_buff *skb;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002147 int len = xfrm_sa_len(x);
Herbert Xu0603eac2005-06-18 22:54:36 -07002148 int headlen;
2149
2150 headlen = sizeof(*p);
2151 if (c->event == XFRM_MSG_DELSA) {
Thomas Graf7deb2262007-08-22 13:57:39 -07002152 len += nla_total_size(headlen);
Herbert Xu0603eac2005-06-18 22:54:36 -07002153 headlen = sizeof(*id);
2154 }
Thomas Graf7deb2262007-08-22 13:57:39 -07002155 len += NLMSG_ALIGN(headlen);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002156
Thomas Graf7deb2262007-08-22 13:57:39 -07002157 skb = nlmsg_new(len, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002158 if (skb == NULL)
2159 return -ENOMEM;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002160
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002161 nlh = nlmsg_put(skb, c->pid, c->seq, c->event, headlen, 0);
2162 if (nlh == NULL)
Thomas Grafc0144be2007-08-22 13:55:43 -07002163 goto nla_put_failure;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002164
Thomas Graf7b67c852007-08-22 13:53:52 -07002165 p = nlmsg_data(nlh);
Herbert Xu0603eac2005-06-18 22:54:36 -07002166 if (c->event == XFRM_MSG_DELSA) {
Thomas Grafc0144be2007-08-22 13:55:43 -07002167 struct nlattr *attr;
2168
Thomas Graf7b67c852007-08-22 13:53:52 -07002169 id = nlmsg_data(nlh);
Herbert Xu0603eac2005-06-18 22:54:36 -07002170 memcpy(&id->daddr, &x->id.daddr, sizeof(id->daddr));
2171 id->spi = x->id.spi;
2172 id->family = x->props.family;
2173 id->proto = x->id.proto;
2174
Thomas Grafc0144be2007-08-22 13:55:43 -07002175 attr = nla_reserve(skb, XFRMA_SA, sizeof(*p));
2176 if (attr == NULL)
2177 goto nla_put_failure;
2178
2179 p = nla_data(attr);
Herbert Xu0603eac2005-06-18 22:54:36 -07002180 }
2181
Herbert Xu68325d32007-10-09 13:30:57 -07002182 if (copy_to_user_state_extra(x, p, skb))
2183 goto nla_put_failure;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002184
Thomas Graf98250692007-08-22 12:47:26 -07002185 nlmsg_end(skb, nlh);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002186
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002187 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_SA, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002188
Thomas Grafc0144be2007-08-22 13:55:43 -07002189nla_put_failure:
Herbert Xu68325d32007-10-09 13:30:57 -07002190 /* Somebody screwed up with xfrm_sa_len! */
2191 WARN_ON(1);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002192 kfree_skb(skb);
2193 return -1;
2194}
2195
2196static int xfrm_send_state_notify(struct xfrm_state *x, struct km_event *c)
2197{
2198
2199 switch (c->event) {
Herbert Xuf60f6b82005-06-18 22:44:37 -07002200 case XFRM_MSG_EXPIRE:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002201 return xfrm_exp_state_notify(x, c);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002202 case XFRM_MSG_NEWAE:
2203 return xfrm_aevent_state_notify(x, c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07002204 case XFRM_MSG_DELSA:
2205 case XFRM_MSG_UPDSA:
2206 case XFRM_MSG_NEWSA:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002207 return xfrm_notify_sa(x, c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07002208 case XFRM_MSG_FLUSHSA:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002209 return xfrm_notify_sa_flush(c);
2210 default:
2211 printk("xfrm_user: Unknown SA event %d\n", c->event);
2212 break;
2213 }
2214
2215 return 0;
2216
2217}
2218
Thomas Graf7deb2262007-08-22 13:57:39 -07002219static inline size_t xfrm_acquire_msgsize(struct xfrm_state *x,
2220 struct xfrm_policy *xp)
2221{
2222 return NLMSG_ALIGN(sizeof(struct xfrm_user_acquire))
2223 + nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr)
2224 + nla_total_size(xfrm_user_sec_ctx_size(x->security))
2225 + userpolicy_type_attrsize();
2226}
2227
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228static int build_acquire(struct sk_buff *skb, struct xfrm_state *x,
2229 struct xfrm_tmpl *xt, struct xfrm_policy *xp,
2230 int dir)
2231{
2232 struct xfrm_user_acquire *ua;
2233 struct nlmsghdr *nlh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002234 __u32 seq = xfrm_get_acqseq();
2235
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002236 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_ACQUIRE, sizeof(*ua), 0);
2237 if (nlh == NULL)
2238 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002239
Thomas Graf7b67c852007-08-22 13:53:52 -07002240 ua = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241 memcpy(&ua->id, &x->id, sizeof(ua->id));
2242 memcpy(&ua->saddr, &x->props.saddr, sizeof(ua->saddr));
2243 memcpy(&ua->sel, &x->sel, sizeof(ua->sel));
2244 copy_to_user_policy(xp, &ua->policy, dir);
2245 ua->aalgos = xt->aalgos;
2246 ua->ealgos = xt->ealgos;
2247 ua->calgos = xt->calgos;
2248 ua->seq = x->km.seq = seq;
2249
2250 if (copy_to_user_tmpl(xp, skb) < 0)
2251 goto nlmsg_failure;
Serge Hallyn0d681622006-07-24 23:30:44 -07002252 if (copy_to_user_state_sec_ctx(x, skb))
Trent Jaegerdf718372005-12-13 23:12:27 -08002253 goto nlmsg_failure;
Jamal Hadi Salim1459bb32006-11-20 16:51:22 -08002254 if (copy_to_user_policy_type(xp->type, skb) < 0)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07002255 goto nlmsg_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256
Thomas Graf98250692007-08-22 12:47:26 -07002257 return nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258
2259nlmsg_failure:
Thomas Graf98250692007-08-22 12:47:26 -07002260 nlmsg_cancel(skb, nlh);
2261 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262}
2263
2264static int xfrm_send_acquire(struct xfrm_state *x, struct xfrm_tmpl *xt,
2265 struct xfrm_policy *xp, int dir)
2266{
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002267 struct net *net = xs_net(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002268 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002269
Thomas Graf7deb2262007-08-22 13:57:39 -07002270 skb = nlmsg_new(xfrm_acquire_msgsize(x, xp), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271 if (skb == NULL)
2272 return -ENOMEM;
2273
2274 if (build_acquire(skb, x, xt, xp, dir) < 0)
2275 BUG();
2276
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002277 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_ACQUIRE, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278}
2279
2280/* User gives us xfrm_user_policy_info followed by an array of 0
2281 * or more templates.
2282 */
Venkat Yekkiralacb969f02006-07-24 23:32:20 -07002283static struct xfrm_policy *xfrm_compile_policy(struct sock *sk, int opt,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002284 u8 *data, int len, int *dir)
2285{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002286 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287 struct xfrm_userpolicy_info *p = (struct xfrm_userpolicy_info *)data;
2288 struct xfrm_user_tmpl *ut = (struct xfrm_user_tmpl *) (p + 1);
2289 struct xfrm_policy *xp;
2290 int nr;
2291
Venkat Yekkiralacb969f02006-07-24 23:32:20 -07002292 switch (sk->sk_family) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002293 case AF_INET:
2294 if (opt != IP_XFRM_POLICY) {
2295 *dir = -EOPNOTSUPP;
2296 return NULL;
2297 }
2298 break;
2299#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
2300 case AF_INET6:
2301 if (opt != IPV6_XFRM_POLICY) {
2302 *dir = -EOPNOTSUPP;
2303 return NULL;
2304 }
2305 break;
2306#endif
2307 default:
2308 *dir = -EINVAL;
2309 return NULL;
2310 }
2311
2312 *dir = -EINVAL;
2313
2314 if (len < sizeof(*p) ||
2315 verify_newpolicy_info(p))
2316 return NULL;
2317
2318 nr = ((len - sizeof(*p)) / sizeof(*ut));
David S. Millerb4ad86bf2006-12-03 19:19:26 -08002319 if (validate_tmpl(nr, ut, p->sel.family))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320 return NULL;
2321
Herbert Xua4f1bac2005-07-26 15:43:17 -07002322 if (p->dir > XFRM_POLICY_OUT)
2323 return NULL;
2324
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002325 xp = xfrm_policy_alloc(net, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002326 if (xp == NULL) {
2327 *dir = -ENOBUFS;
2328 return NULL;
2329 }
2330
2331 copy_from_user_policy(xp, p);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07002332 xp->type = XFRM_POLICY_TYPE_MAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002333 copy_templates(xp, ut, nr);
2334
2335 *dir = p->dir;
2336
2337 return xp;
2338}
2339
Thomas Graf7deb2262007-08-22 13:57:39 -07002340static inline size_t xfrm_polexpire_msgsize(struct xfrm_policy *xp)
2341{
2342 return NLMSG_ALIGN(sizeof(struct xfrm_user_polexpire))
2343 + nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr)
2344 + nla_total_size(xfrm_user_sec_ctx_size(xp->security))
2345 + userpolicy_type_attrsize();
2346}
2347
Linus Torvalds1da177e2005-04-16 15:20:36 -07002348static int build_polexpire(struct sk_buff *skb, struct xfrm_policy *xp,
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002349 int dir, struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002350{
2351 struct xfrm_user_polexpire *upe;
2352 struct nlmsghdr *nlh;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002353 int hard = c->data.hard;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002354
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002355 nlh = nlmsg_put(skb, c->pid, 0, XFRM_MSG_POLEXPIRE, sizeof(*upe), 0);
2356 if (nlh == NULL)
2357 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002358
Thomas Graf7b67c852007-08-22 13:53:52 -07002359 upe = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002360 copy_to_user_policy(xp, &upe->pol, dir);
2361 if (copy_to_user_tmpl(xp, skb) < 0)
2362 goto nlmsg_failure;
Trent Jaegerdf718372005-12-13 23:12:27 -08002363 if (copy_to_user_sec_ctx(xp, skb))
2364 goto nlmsg_failure;
Jamal Hadi Salim1459bb32006-11-20 16:51:22 -08002365 if (copy_to_user_policy_type(xp->type, skb) < 0)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07002366 goto nlmsg_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367 upe->hard = !!hard;
2368
Thomas Graf98250692007-08-22 12:47:26 -07002369 return nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002370
2371nlmsg_failure:
Thomas Graf98250692007-08-22 12:47:26 -07002372 nlmsg_cancel(skb, nlh);
2373 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002374}
2375
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002376static int xfrm_exp_policy_notify(struct xfrm_policy *xp, int dir, struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002377{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002378 struct net *net = xp_net(xp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002379 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002380
Thomas Graf7deb2262007-08-22 13:57:39 -07002381 skb = nlmsg_new(xfrm_polexpire_msgsize(xp), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002382 if (skb == NULL)
2383 return -ENOMEM;
2384
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002385 if (build_polexpire(skb, xp, dir, c) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002386 BUG();
2387
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002388 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_EXPIRE, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002389}
2390
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002391static int xfrm_notify_policy(struct xfrm_policy *xp, int dir, struct km_event *c)
2392{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002393 struct net *net = xp_net(xp);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002394 struct xfrm_userpolicy_info *p;
Herbert Xu0603eac2005-06-18 22:54:36 -07002395 struct xfrm_userpolicy_id *id;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002396 struct nlmsghdr *nlh;
2397 struct sk_buff *skb;
Thomas Graf7deb2262007-08-22 13:57:39 -07002398 int len = nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr);
Herbert Xu0603eac2005-06-18 22:54:36 -07002399 int headlen;
2400
2401 headlen = sizeof(*p);
2402 if (c->event == XFRM_MSG_DELPOLICY) {
Thomas Graf7deb2262007-08-22 13:57:39 -07002403 len += nla_total_size(headlen);
Herbert Xu0603eac2005-06-18 22:54:36 -07002404 headlen = sizeof(*id);
2405 }
Thomas Grafcfbfd452007-08-22 13:57:04 -07002406 len += userpolicy_type_attrsize();
Thomas Graf7deb2262007-08-22 13:57:39 -07002407 len += NLMSG_ALIGN(headlen);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002408
Thomas Graf7deb2262007-08-22 13:57:39 -07002409 skb = nlmsg_new(len, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002410 if (skb == NULL)
2411 return -ENOMEM;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002412
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002413 nlh = nlmsg_put(skb, c->pid, c->seq, c->event, headlen, 0);
2414 if (nlh == NULL)
2415 goto nlmsg_failure;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002416
Thomas Graf7b67c852007-08-22 13:53:52 -07002417 p = nlmsg_data(nlh);
Herbert Xu0603eac2005-06-18 22:54:36 -07002418 if (c->event == XFRM_MSG_DELPOLICY) {
Thomas Grafc0144be2007-08-22 13:55:43 -07002419 struct nlattr *attr;
2420
Thomas Graf7b67c852007-08-22 13:53:52 -07002421 id = nlmsg_data(nlh);
Herbert Xu0603eac2005-06-18 22:54:36 -07002422 memset(id, 0, sizeof(*id));
2423 id->dir = dir;
2424 if (c->data.byid)
2425 id->index = xp->index;
2426 else
2427 memcpy(&id->sel, &xp->selector, sizeof(id->sel));
2428
Thomas Grafc0144be2007-08-22 13:55:43 -07002429 attr = nla_reserve(skb, XFRMA_POLICY, sizeof(*p));
2430 if (attr == NULL)
2431 goto nlmsg_failure;
2432
2433 p = nla_data(attr);
Herbert Xu0603eac2005-06-18 22:54:36 -07002434 }
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002435
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002436 copy_to_user_policy(xp, p, dir);
2437 if (copy_to_user_tmpl(xp, skb) < 0)
2438 goto nlmsg_failure;
Jamal Hadi Salim1459bb32006-11-20 16:51:22 -08002439 if (copy_to_user_policy_type(xp->type, skb) < 0)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07002440 goto nlmsg_failure;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002441
Thomas Graf98250692007-08-22 12:47:26 -07002442 nlmsg_end(skb, nlh);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002443
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002444 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_POLICY, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002445
2446nlmsg_failure:
2447 kfree_skb(skb);
2448 return -1;
2449}
2450
2451static int xfrm_notify_policy_flush(struct km_event *c)
2452{
Alexey Dobriyan70678022008-11-25 17:50:36 -08002453 struct net *net = c->net;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002454 struct nlmsghdr *nlh;
2455 struct sk_buff *skb;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002456
Thomas Graf7deb2262007-08-22 13:57:39 -07002457 skb = nlmsg_new(userpolicy_type_attrsize(), GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002458 if (skb == NULL)
2459 return -ENOMEM;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002460
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002461 nlh = nlmsg_put(skb, c->pid, c->seq, XFRM_MSG_FLUSHPOLICY, 0, 0);
2462 if (nlh == NULL)
2463 goto nlmsg_failure;
Jamal Hadi Salim0c51f532006-11-27 12:58:20 -08002464 if (copy_to_user_policy_type(c->data.type, skb) < 0)
2465 goto nlmsg_failure;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002466
Thomas Graf98250692007-08-22 12:47:26 -07002467 nlmsg_end(skb, nlh);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002468
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002469 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_POLICY, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002470
2471nlmsg_failure:
2472 kfree_skb(skb);
2473 return -1;
2474}
2475
2476static int xfrm_send_policy_notify(struct xfrm_policy *xp, int dir, struct km_event *c)
2477{
2478
2479 switch (c->event) {
Herbert Xuf60f6b82005-06-18 22:44:37 -07002480 case XFRM_MSG_NEWPOLICY:
2481 case XFRM_MSG_UPDPOLICY:
2482 case XFRM_MSG_DELPOLICY:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002483 return xfrm_notify_policy(xp, dir, c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07002484 case XFRM_MSG_FLUSHPOLICY:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002485 return xfrm_notify_policy_flush(c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07002486 case XFRM_MSG_POLEXPIRE:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002487 return xfrm_exp_policy_notify(xp, dir, c);
2488 default:
2489 printk("xfrm_user: Unknown Policy event %d\n", c->event);
2490 }
2491
2492 return 0;
2493
2494}
2495
Thomas Graf7deb2262007-08-22 13:57:39 -07002496static inline size_t xfrm_report_msgsize(void)
2497{
2498 return NLMSG_ALIGN(sizeof(struct xfrm_user_report));
2499}
2500
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002501static int build_report(struct sk_buff *skb, u8 proto,
2502 struct xfrm_selector *sel, xfrm_address_t *addr)
2503{
2504 struct xfrm_user_report *ur;
2505 struct nlmsghdr *nlh;
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002506
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002507 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_REPORT, sizeof(*ur), 0);
2508 if (nlh == NULL)
2509 return -EMSGSIZE;
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002510
Thomas Graf7b67c852007-08-22 13:53:52 -07002511 ur = nlmsg_data(nlh);
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002512 ur->proto = proto;
2513 memcpy(&ur->sel, sel, sizeof(ur->sel));
2514
2515 if (addr)
Thomas Grafc0144be2007-08-22 13:55:43 -07002516 NLA_PUT(skb, XFRMA_COADDR, sizeof(*addr), addr);
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002517
Thomas Graf98250692007-08-22 12:47:26 -07002518 return nlmsg_end(skb, nlh);
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002519
Thomas Grafc0144be2007-08-22 13:55:43 -07002520nla_put_failure:
Thomas Graf98250692007-08-22 12:47:26 -07002521 nlmsg_cancel(skb, nlh);
2522 return -EMSGSIZE;
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002523}
2524
Alexey Dobriyandb983c12008-11-25 17:51:01 -08002525static int xfrm_send_report(struct net *net, u8 proto,
2526 struct xfrm_selector *sel, xfrm_address_t *addr)
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002527{
2528 struct sk_buff *skb;
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002529
Thomas Graf7deb2262007-08-22 13:57:39 -07002530 skb = nlmsg_new(xfrm_report_msgsize(), GFP_ATOMIC);
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002531 if (skb == NULL)
2532 return -ENOMEM;
2533
2534 if (build_report(skb, proto, sel, addr) < 0)
2535 BUG();
2536
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002537 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_REPORT, GFP_ATOMIC);
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002538}
2539
Martin Willi3a2dfbe2008-10-28 16:01:07 -07002540static inline size_t xfrm_mapping_msgsize(void)
2541{
2542 return NLMSG_ALIGN(sizeof(struct xfrm_user_mapping));
2543}
2544
2545static int build_mapping(struct sk_buff *skb, struct xfrm_state *x,
2546 xfrm_address_t *new_saddr, __be16 new_sport)
2547{
2548 struct xfrm_user_mapping *um;
2549 struct nlmsghdr *nlh;
2550
2551 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_MAPPING, sizeof(*um), 0);
2552 if (nlh == NULL)
2553 return -EMSGSIZE;
2554
2555 um = nlmsg_data(nlh);
2556
2557 memcpy(&um->id.daddr, &x->id.daddr, sizeof(um->id.daddr));
2558 um->id.spi = x->id.spi;
2559 um->id.family = x->props.family;
2560 um->id.proto = x->id.proto;
2561 memcpy(&um->new_saddr, new_saddr, sizeof(um->new_saddr));
2562 memcpy(&um->old_saddr, &x->props.saddr, sizeof(um->old_saddr));
2563 um->new_sport = new_sport;
2564 um->old_sport = x->encap->encap_sport;
2565 um->reqid = x->props.reqid;
2566
2567 return nlmsg_end(skb, nlh);
2568}
2569
2570static int xfrm_send_mapping(struct xfrm_state *x, xfrm_address_t *ipaddr,
2571 __be16 sport)
2572{
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002573 struct net *net = xs_net(x);
Martin Willi3a2dfbe2008-10-28 16:01:07 -07002574 struct sk_buff *skb;
2575
2576 if (x->id.proto != IPPROTO_ESP)
2577 return -EINVAL;
2578
2579 if (!x->encap)
2580 return -EINVAL;
2581
2582 skb = nlmsg_new(xfrm_mapping_msgsize(), GFP_ATOMIC);
2583 if (skb == NULL)
2584 return -ENOMEM;
2585
2586 if (build_mapping(skb, x, ipaddr, sport) < 0)
2587 BUG();
2588
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002589 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_MAPPING, GFP_ATOMIC);
Martin Willi3a2dfbe2008-10-28 16:01:07 -07002590}
2591
Linus Torvalds1da177e2005-04-16 15:20:36 -07002592static struct xfrm_mgr netlink_mgr = {
2593 .id = "netlink",
2594 .notify = xfrm_send_state_notify,
2595 .acquire = xfrm_send_acquire,
2596 .compile_policy = xfrm_compile_policy,
2597 .notify_policy = xfrm_send_policy_notify,
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002598 .report = xfrm_send_report,
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002599 .migrate = xfrm_send_migrate,
Martin Willi3a2dfbe2008-10-28 16:01:07 -07002600 .new_mapping = xfrm_send_mapping,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002601};
2602
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002603static int __net_init xfrm_user_net_init(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002604{
Patrick McHardybe336902006-03-20 22:40:54 -08002605 struct sock *nlsk;
2606
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002607 nlsk = netlink_kernel_create(net, NETLINK_XFRM, XFRMNLGRP_MAX,
Patrick McHardyaf65bdf2007-04-20 14:14:21 -07002608 xfrm_netlink_rcv, NULL, THIS_MODULE);
Patrick McHardybe336902006-03-20 22:40:54 -08002609 if (nlsk == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002610 return -ENOMEM;
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002611 rcu_assign_pointer(net->xfrm.nlsk, nlsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002612 return 0;
2613}
2614
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002615static void __net_exit xfrm_user_net_exit(struct net *net)
2616{
2617 struct sock *nlsk = net->xfrm.nlsk;
2618
2619 rcu_assign_pointer(net->xfrm.nlsk, NULL);
2620 synchronize_rcu();
2621 netlink_kernel_release(nlsk);
2622}
2623
2624static struct pernet_operations xfrm_user_net_ops = {
2625 .init = xfrm_user_net_init,
2626 .exit = xfrm_user_net_exit,
2627};
2628
2629static int __init xfrm_user_init(void)
2630{
2631 int rv;
2632
2633 printk(KERN_INFO "Initializing XFRM netlink socket\n");
2634
2635 rv = register_pernet_subsys(&xfrm_user_net_ops);
2636 if (rv < 0)
2637 return rv;
2638 rv = xfrm_register_km(&netlink_mgr);
2639 if (rv < 0)
2640 unregister_pernet_subsys(&xfrm_user_net_ops);
2641 return rv;
2642}
2643
Linus Torvalds1da177e2005-04-16 15:20:36 -07002644static void __exit xfrm_user_exit(void)
2645{
2646 xfrm_unregister_km(&netlink_mgr);
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002647 unregister_pernet_subsys(&xfrm_user_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002648}
2649
2650module_init(xfrm_user_init);
2651module_exit(xfrm_user_exit);
2652MODULE_LICENSE("GPL");
Harald Welte4fdb3bb2005-08-09 19:40:55 -07002653MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK, NETLINK_XFRM);
Jamal Hadi Salimf8cd5482006-03-20 19:15:11 -08002654