blob: 70dca1e482420fe11aac31ef16e64aa1d9a7ccc7 [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
Joy Latten161a09e2006-11-27 13:11:54 -060033#include <linux/audit.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034
Thomas Grafc26445a2007-08-22 13:56:23 -070035static inline int alg_len(struct xfrm_algo *alg)
36{
37 return sizeof(*alg) + ((alg->alg_key_len + 7) / 8);
38}
39
Thomas Graf5424f322007-08-22 14:01:33 -070040static int verify_one_alg(struct nlattr **attrs, enum xfrm_attr_type_t type)
Linus Torvalds1da177e2005-04-16 15:20:36 -070041{
Thomas Graf5424f322007-08-22 14:01:33 -070042 struct nlattr *rt = attrs[type];
Linus Torvalds1da177e2005-04-16 15:20:36 -070043 struct xfrm_algo *algp;
44
45 if (!rt)
46 return 0;
47
Thomas Graf5424f322007-08-22 14:01:33 -070048 algp = nla_data(rt);
49 if (nla_len(rt) < alg_len(algp))
Herbert Xu31c26852005-05-19 12:39:49 -070050 return -EINVAL;
51
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 switch (type) {
53 case XFRMA_ALG_AUTH:
54 if (!algp->alg_key_len &&
55 strcmp(algp->alg_name, "digest_null") != 0)
56 return -EINVAL;
57 break;
58
59 case XFRMA_ALG_CRYPT:
60 if (!algp->alg_key_len &&
61 strcmp(algp->alg_name, "cipher_null") != 0)
62 return -EINVAL;
63 break;
64
65 case XFRMA_ALG_COMP:
66 /* Zero length keys are legal. */
67 break;
68
69 default:
70 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -070071 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070072
73 algp->alg_name[CRYPTO_MAX_ALG_NAME - 1] = '\0';
74 return 0;
75}
76
Thomas Graf5424f322007-08-22 14:01:33 -070077static void verify_one_addr(struct nlattr **attrs, enum xfrm_attr_type_t type,
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -070078 xfrm_address_t **addrp)
79{
Thomas Graf5424f322007-08-22 14:01:33 -070080 struct nlattr *rt = attrs[type];
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -070081
Thomas Grafcf5cb792007-08-22 13:59:04 -070082 if (rt && addrp)
Thomas Graf5424f322007-08-22 14:01:33 -070083 *addrp = nla_data(rt);
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -070084}
Trent Jaegerdf718372005-12-13 23:12:27 -080085
Thomas Graf5424f322007-08-22 14:01:33 -070086static inline int verify_sec_ctx_len(struct nlattr **attrs)
Trent Jaegerdf718372005-12-13 23:12:27 -080087{
Thomas Graf5424f322007-08-22 14:01:33 -070088 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
Trent Jaegerdf718372005-12-13 23:12:27 -080089 struct xfrm_user_sec_ctx *uctx;
Trent Jaegerdf718372005-12-13 23:12:27 -080090
91 if (!rt)
92 return 0;
93
Thomas Graf5424f322007-08-22 14:01:33 -070094 uctx = nla_data(rt);
Thomas Grafcf5cb792007-08-22 13:59:04 -070095 if (uctx->len != (sizeof(struct xfrm_user_sec_ctx) + uctx->ctx_len))
Trent Jaegerdf718372005-12-13 23:12:27 -080096 return -EINVAL;
97
98 return 0;
99}
100
101
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102static int verify_newsa_info(struct xfrm_usersa_info *p,
Thomas Graf5424f322007-08-22 14:01:33 -0700103 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104{
105 int err;
106
107 err = -EINVAL;
108 switch (p->family) {
109 case AF_INET:
110 break;
111
112 case AF_INET6:
113#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
114 break;
115#else
116 err = -EAFNOSUPPORT;
117 goto out;
118#endif
119
120 default:
121 goto out;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700122 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123
124 err = -EINVAL;
125 switch (p->id.proto) {
126 case IPPROTO_AH:
Thomas Graf35a7aa02007-08-22 14:00:40 -0700127 if (!attrs[XFRMA_ALG_AUTH] ||
128 attrs[XFRMA_ALG_CRYPT] ||
129 attrs[XFRMA_ALG_COMP])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 goto out;
131 break;
132
133 case IPPROTO_ESP:
Thomas Graf35a7aa02007-08-22 14:00:40 -0700134 if ((!attrs[XFRMA_ALG_AUTH] &&
135 !attrs[XFRMA_ALG_CRYPT]) ||
136 attrs[XFRMA_ALG_COMP])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 goto out;
138 break;
139
140 case IPPROTO_COMP:
Thomas Graf35a7aa02007-08-22 14:00:40 -0700141 if (!attrs[XFRMA_ALG_COMP] ||
142 attrs[XFRMA_ALG_AUTH] ||
143 attrs[XFRMA_ALG_CRYPT])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 goto out;
145 break;
146
Masahide NAKAMURAe23c7192006-08-23 20:33:28 -0700147#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
148 case IPPROTO_DSTOPTS:
149 case IPPROTO_ROUTING:
Thomas Graf35a7aa02007-08-22 14:00:40 -0700150 if (attrs[XFRMA_ALG_COMP] ||
151 attrs[XFRMA_ALG_AUTH] ||
152 attrs[XFRMA_ALG_CRYPT] ||
153 attrs[XFRMA_ENCAP] ||
154 attrs[XFRMA_SEC_CTX] ||
155 !attrs[XFRMA_COADDR])
Masahide NAKAMURAe23c7192006-08-23 20:33:28 -0700156 goto out;
157 break;
158#endif
159
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 default:
161 goto out;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700162 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163
Thomas Graf35a7aa02007-08-22 14:00:40 -0700164 if ((err = verify_one_alg(attrs, XFRMA_ALG_AUTH)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 goto out;
Thomas Graf35a7aa02007-08-22 14:00:40 -0700166 if ((err = verify_one_alg(attrs, XFRMA_ALG_CRYPT)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 goto out;
Thomas Graf35a7aa02007-08-22 14:00:40 -0700168 if ((err = verify_one_alg(attrs, XFRMA_ALG_COMP)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 goto out;
Thomas Graf35a7aa02007-08-22 14:00:40 -0700170 if ((err = verify_sec_ctx_len(attrs)))
Trent Jaegerdf718372005-12-13 23:12:27 -0800171 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172
173 err = -EINVAL;
174 switch (p->mode) {
Masahide NAKAMURA7e49e6d2006-09-22 15:05:15 -0700175 case XFRM_MODE_TRANSPORT:
176 case XFRM_MODE_TUNNEL:
Noriaki TAKAMIYA060f02a2006-08-23 18:18:55 -0700177 case XFRM_MODE_ROUTEOPTIMIZATION:
Diego Beltrami0a694522006-10-03 23:47:05 -0700178 case XFRM_MODE_BEET:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 break;
180
181 default:
182 goto out;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700183 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184
185 err = 0;
186
187out:
188 return err;
189}
190
191static int attach_one_algo(struct xfrm_algo **algpp, u8 *props,
192 struct xfrm_algo_desc *(*get_byname)(char *, int),
Thomas Graf5424f322007-08-22 14:01:33 -0700193 struct nlattr *rta)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 struct xfrm_algo *p, *ualg;
196 struct xfrm_algo_desc *algo;
197
198 if (!rta)
199 return 0;
200
Thomas Graf5424f322007-08-22 14:01:33 -0700201 ualg = nla_data(rta);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202
203 algo = get_byname(ualg->alg_name, 1);
204 if (!algo)
205 return -ENOSYS;
206 *props = algo->desc.sadb_alg_id;
207
Thomas Grafc26445a2007-08-22 13:56:23 -0700208 p = kmemdup(ualg, alg_len(ualg), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 if (!p)
210 return -ENOMEM;
211
Herbert Xu04ff1262006-08-13 08:50:00 +1000212 strcpy(p->alg_name, algo->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 *algpp = p;
214 return 0;
215}
216
Thomas Graf5424f322007-08-22 14:01:33 -0700217static int attach_encap_tmpl(struct xfrm_encap_tmpl **encapp, struct nlattr *rta)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 struct xfrm_encap_tmpl *p, *uencap;
220
221 if (!rta)
222 return 0;
223
Thomas Graf5424f322007-08-22 14:01:33 -0700224 uencap = nla_data(rta);
Arnaldo Carvalho de Melocdbc6da2006-11-21 01:22:51 -0200225 p = kmemdup(uencap, sizeof(*p), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 if (!p)
227 return -ENOMEM;
228
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 *encapp = p;
230 return 0;
231}
232
Trent Jaegerdf718372005-12-13 23:12:27 -0800233
Joy Latten661697f2007-04-13 16:14:35 -0700234static inline int xfrm_user_sec_ctx_size(struct xfrm_sec_ctx *xfrm_ctx)
Trent Jaegerdf718372005-12-13 23:12:27 -0800235{
Trent Jaegerdf718372005-12-13 23:12:27 -0800236 int len = 0;
237
238 if (xfrm_ctx) {
239 len += sizeof(struct xfrm_user_sec_ctx);
240 len += xfrm_ctx->ctx_len;
241 }
242 return len;
243}
244
Thomas Graf5424f322007-08-22 14:01:33 -0700245static int attach_sec_ctx(struct xfrm_state *x, struct nlattr *u_arg)
Trent Jaegerdf718372005-12-13 23:12:27 -0800246{
247 struct xfrm_user_sec_ctx *uctx;
248
249 if (!u_arg)
250 return 0;
251
Thomas Graf5424f322007-08-22 14:01:33 -0700252 uctx = nla_data(u_arg);
Trent Jaegerdf718372005-12-13 23:12:27 -0800253 return security_xfrm_state_alloc(x, uctx);
254}
255
Thomas Graf5424f322007-08-22 14:01:33 -0700256static int attach_one_addr(xfrm_address_t **addrpp, struct nlattr *rta)
Noriaki TAKAMIYA060f02a2006-08-23 18:18:55 -0700257{
Noriaki TAKAMIYA060f02a2006-08-23 18:18:55 -0700258 xfrm_address_t *p, *uaddrp;
259
260 if (!rta)
261 return 0;
262
Thomas Graf5424f322007-08-22 14:01:33 -0700263 uaddrp = nla_data(rta);
Arnaldo Carvalho de Melocdbc6da2006-11-21 01:22:51 -0200264 p = kmemdup(uaddrp, sizeof(*p), GFP_KERNEL);
Noriaki TAKAMIYA060f02a2006-08-23 18:18:55 -0700265 if (!p)
266 return -ENOMEM;
267
Noriaki TAKAMIYA060f02a2006-08-23 18:18:55 -0700268 *addrpp = p;
269 return 0;
270}
271
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272static void copy_from_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p)
273{
274 memcpy(&x->id, &p->id, sizeof(x->id));
275 memcpy(&x->sel, &p->sel, sizeof(x->sel));
276 memcpy(&x->lft, &p->lft, sizeof(x->lft));
277 x->props.mode = p->mode;
278 x->props.replay_window = p->replay_window;
279 x->props.reqid = p->reqid;
280 x->props.family = p->family;
David S. Miller54489c142006-10-27 15:29:47 -0700281 memcpy(&x->props.saddr, &p->saddr, sizeof(x->props.saddr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 x->props.flags = p->flags;
Herbert Xu196b0032007-07-31 02:04:32 -0700283
284 /*
285 * Set inner address family if the KM left it as zero.
286 * See comment in validate_tmpl.
287 */
288 if (!x->sel.family)
289 x->sel.family = p->family;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290}
291
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800292/*
293 * someday when pfkey also has support, we could have the code
294 * somehow made shareable and move it to xfrm_state.c - JHS
295 *
296*/
Thomas Graf5424f322007-08-22 14:01:33 -0700297static void xfrm_update_ae_params(struct xfrm_state *x, struct nlattr **attrs)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800298{
Thomas Graf5424f322007-08-22 14:01:33 -0700299 struct nlattr *rp = attrs[XFRMA_REPLAY_VAL];
300 struct nlattr *lt = attrs[XFRMA_LTIME_VAL];
301 struct nlattr *et = attrs[XFRMA_ETIMER_THRESH];
302 struct nlattr *rt = attrs[XFRMA_REPLAY_THRESH];
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800303
304 if (rp) {
305 struct xfrm_replay_state *replay;
Thomas Graf5424f322007-08-22 14:01:33 -0700306 replay = nla_data(rp);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800307 memcpy(&x->replay, replay, sizeof(*replay));
308 memcpy(&x->preplay, replay, sizeof(*replay));
309 }
310
311 if (lt) {
312 struct xfrm_lifetime_cur *ltime;
Thomas Graf5424f322007-08-22 14:01:33 -0700313 ltime = nla_data(lt);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800314 x->curlft.bytes = ltime->bytes;
315 x->curlft.packets = ltime->packets;
316 x->curlft.add_time = ltime->add_time;
317 x->curlft.use_time = ltime->use_time;
318 }
319
Thomas Grafcf5cb792007-08-22 13:59:04 -0700320 if (et)
Thomas Graf5424f322007-08-22 14:01:33 -0700321 x->replay_maxage = nla_get_u32(et);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800322
Thomas Grafcf5cb792007-08-22 13:59:04 -0700323 if (rt)
Thomas Graf5424f322007-08-22 14:01:33 -0700324 x->replay_maxdiff = nla_get_u32(rt);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800325}
326
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327static struct xfrm_state *xfrm_state_construct(struct xfrm_usersa_info *p,
Thomas Graf5424f322007-08-22 14:01:33 -0700328 struct nlattr **attrs,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 int *errp)
330{
331 struct xfrm_state *x = xfrm_state_alloc();
332 int err = -ENOMEM;
333
334 if (!x)
335 goto error_no_put;
336
337 copy_from_user_state(x, p);
338
339 if ((err = attach_one_algo(&x->aalg, &x->props.aalgo,
340 xfrm_aalg_get_byname,
Thomas Graf35a7aa02007-08-22 14:00:40 -0700341 attrs[XFRMA_ALG_AUTH])))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 goto error;
343 if ((err = attach_one_algo(&x->ealg, &x->props.ealgo,
344 xfrm_ealg_get_byname,
Thomas Graf35a7aa02007-08-22 14:00:40 -0700345 attrs[XFRMA_ALG_CRYPT])))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 goto error;
347 if ((err = attach_one_algo(&x->calg, &x->props.calgo,
348 xfrm_calg_get_byname,
Thomas Graf35a7aa02007-08-22 14:00:40 -0700349 attrs[XFRMA_ALG_COMP])))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 goto error;
Thomas Graf35a7aa02007-08-22 14:00:40 -0700351 if ((err = attach_encap_tmpl(&x->encap, attrs[XFRMA_ENCAP])))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 goto error;
Thomas Graf35a7aa02007-08-22 14:00:40 -0700353 if ((err = attach_one_addr(&x->coaddr, attrs[XFRMA_COADDR])))
Noriaki TAKAMIYA060f02a2006-08-23 18:18:55 -0700354 goto error;
Herbert Xu72cb6962005-06-20 13:18:08 -0700355 err = xfrm_init_state(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 if (err)
357 goto error;
358
Thomas Graf35a7aa02007-08-22 14:00:40 -0700359 if ((err = attach_sec_ctx(x, attrs[XFRMA_SEC_CTX])))
Trent Jaegerdf718372005-12-13 23:12:27 -0800360 goto error;
361
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 x->km.seq = p->seq;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800363 x->replay_maxdiff = sysctl_xfrm_aevent_rseqth;
364 /* sysctl_xfrm_aevent_etime is in 100ms units */
365 x->replay_maxage = (sysctl_xfrm_aevent_etime*HZ)/XFRM_AE_ETH_M;
366 x->preplay.bitmap = 0;
367 x->preplay.seq = x->replay.seq+x->replay_maxdiff;
368 x->preplay.oseq = x->replay.oseq +x->replay_maxdiff;
369
370 /* override default values from above */
371
Thomas Graf5424f322007-08-22 14:01:33 -0700372 xfrm_update_ae_params(x, attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373
374 return x;
375
376error:
377 x->km.state = XFRM_STATE_DEAD;
378 xfrm_state_put(x);
379error_no_put:
380 *errp = err;
381 return NULL;
382}
383
Christoph Hellwig22e70052007-01-02 15:22:30 -0800384static int xfrm_add_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -0700385 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386{
Thomas Graf7b67c852007-08-22 13:53:52 -0700387 struct xfrm_usersa_info *p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 struct xfrm_state *x;
389 int err;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700390 struct km_event c;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391
Thomas Graf35a7aa02007-08-22 14:00:40 -0700392 err = verify_newsa_info(p, attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 if (err)
394 return err;
395
Thomas Graf35a7aa02007-08-22 14:00:40 -0700396 x = xfrm_state_construct(p, attrs, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 if (!x)
398 return err;
399
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700400 xfrm_state_hold(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 if (nlh->nlmsg_type == XFRM_MSG_NEWSA)
402 err = xfrm_state_add(x);
403 else
404 err = xfrm_state_update(x);
405
Joy Latten161a09e2006-11-27 13:11:54 -0600406 xfrm_audit_log(NETLINK_CB(skb).loginuid, NETLINK_CB(skb).sid,
407 AUDIT_MAC_IPSEC_ADDSA, err ? 0 : 1, NULL, x);
408
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 if (err < 0) {
410 x->km.state = XFRM_STATE_DEAD;
Herbert Xu21380b82006-02-22 14:47:13 -0800411 __xfrm_state_put(x);
Patrick McHardy7d6dfe12005-06-18 22:45:31 -0700412 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 }
414
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700415 c.seq = nlh->nlmsg_seq;
416 c.pid = nlh->nlmsg_pid;
Herbert Xuf60f6b82005-06-18 22:44:37 -0700417 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700418
419 km_state_notify(x, &c);
Patrick McHardy7d6dfe12005-06-18 22:45:31 -0700420out:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700421 xfrm_state_put(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 return err;
423}
424
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700425static struct xfrm_state *xfrm_user_state_lookup(struct xfrm_usersa_id *p,
Thomas Graf5424f322007-08-22 14:01:33 -0700426 struct nlattr **attrs,
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700427 int *errp)
428{
429 struct xfrm_state *x = NULL;
430 int err;
431
432 if (xfrm_id_proto_match(p->proto, IPSEC_PROTO_ANY)) {
433 err = -ESRCH;
434 x = xfrm_state_lookup(&p->daddr, p->spi, p->proto, p->family);
435 } else {
436 xfrm_address_t *saddr = NULL;
437
Thomas Graf35a7aa02007-08-22 14:00:40 -0700438 verify_one_addr(attrs, XFRMA_SRCADDR, &saddr);
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700439 if (!saddr) {
440 err = -EINVAL;
441 goto out;
442 }
443
Masahide NAKAMURA9abbffe2006-11-24 20:34:51 -0800444 err = -ESRCH;
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700445 x = xfrm_state_lookup_byaddr(&p->daddr, saddr, p->proto,
446 p->family);
447 }
448
449 out:
450 if (!x && errp)
451 *errp = err;
452 return x;
453}
454
Christoph Hellwig22e70052007-01-02 15:22:30 -0800455static int xfrm_del_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -0700456 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457{
458 struct xfrm_state *x;
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700459 int err = -ESRCH;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700460 struct km_event c;
Thomas Graf7b67c852007-08-22 13:53:52 -0700461 struct xfrm_usersa_id *p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462
Thomas Graf35a7aa02007-08-22 14:00:40 -0700463 x = xfrm_user_state_lookup(p, attrs, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 if (x == NULL)
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700465 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466
David S. Miller6f68dc32006-06-08 23:58:52 -0700467 if ((err = security_xfrm_state_delete(x)) != 0)
Catherine Zhangc8c05a82006-06-08 23:39:49 -0700468 goto out;
469
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 if (xfrm_state_kern(x)) {
Catherine Zhangc8c05a82006-06-08 23:39:49 -0700471 err = -EPERM;
472 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 }
474
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700475 err = xfrm_state_delete(x);
Joy Latten161a09e2006-11-27 13:11:54 -0600476
Catherine Zhangc8c05a82006-06-08 23:39:49 -0700477 if (err < 0)
478 goto out;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700479
480 c.seq = nlh->nlmsg_seq;
481 c.pid = nlh->nlmsg_pid;
Herbert Xuf60f6b82005-06-18 22:44:37 -0700482 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700483 km_state_notify(x, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484
Catherine Zhangc8c05a82006-06-08 23:39:49 -0700485out:
Eric Paris16bec312007-03-07 16:02:16 -0800486 xfrm_audit_log(NETLINK_CB(skb).loginuid, NETLINK_CB(skb).sid,
487 AUDIT_MAC_IPSEC_DELSA, err ? 0 : 1, NULL, x);
Catherine Zhangc8c05a82006-06-08 23:39:49 -0700488 xfrm_state_put(x);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700489 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490}
491
492static void copy_to_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p)
493{
494 memcpy(&p->id, &x->id, sizeof(p->id));
495 memcpy(&p->sel, &x->sel, sizeof(p->sel));
496 memcpy(&p->lft, &x->lft, sizeof(p->lft));
497 memcpy(&p->curlft, &x->curlft, sizeof(p->curlft));
498 memcpy(&p->stats, &x->stats, sizeof(p->stats));
David S. Miller54489c142006-10-27 15:29:47 -0700499 memcpy(&p->saddr, &x->props.saddr, sizeof(p->saddr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 p->mode = x->props.mode;
501 p->replay_window = x->props.replay_window;
502 p->reqid = x->props.reqid;
503 p->family = x->props.family;
504 p->flags = x->props.flags;
505 p->seq = x->km.seq;
506}
507
508struct xfrm_dump_info {
509 struct sk_buff *in_skb;
510 struct sk_buff *out_skb;
511 u32 nlmsg_seq;
512 u16 nlmsg_flags;
513 int start_idx;
514 int this_idx;
515};
516
Thomas Grafc0144be2007-08-22 13:55:43 -0700517static int copy_sec_ctx(struct xfrm_sec_ctx *s, struct sk_buff *skb)
518{
519 int ctx_size = sizeof(struct xfrm_sec_ctx) + s->ctx_len;
520 struct xfrm_user_sec_ctx *uctx;
521 struct nlattr *attr;
522
523 attr = nla_reserve(skb, XFRMA_SEC_CTX, ctx_size);
524 if (attr == NULL)
525 return -EMSGSIZE;
526
527 uctx = nla_data(attr);
528 uctx->exttype = XFRMA_SEC_CTX;
529 uctx->len = ctx_size;
530 uctx->ctx_doi = s->ctx_doi;
531 uctx->ctx_alg = s->ctx_alg;
532 uctx->ctx_len = s->ctx_len;
533 memcpy(uctx + 1, s->ctx_str, s->ctx_len);
534
535 return 0;
536}
537
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538static int dump_one_state(struct xfrm_state *x, int count, void *ptr)
539{
540 struct xfrm_dump_info *sp = ptr;
541 struct sk_buff *in_skb = sp->in_skb;
542 struct sk_buff *skb = sp->out_skb;
543 struct xfrm_usersa_info *p;
544 struct nlmsghdr *nlh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545
546 if (sp->this_idx < sp->start_idx)
547 goto out;
548
Thomas Graf79b8b7f2007-08-22 12:46:53 -0700549 nlh = nlmsg_put(skb, NETLINK_CB(in_skb).pid, sp->nlmsg_seq,
550 XFRM_MSG_NEWSA, sizeof(*p), sp->nlmsg_flags);
551 if (nlh == NULL)
552 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553
Thomas Graf7b67c852007-08-22 13:53:52 -0700554 p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 copy_to_user_state(x, p);
556
557 if (x->aalg)
Thomas Grafc26445a2007-08-22 13:56:23 -0700558 NLA_PUT(skb, XFRMA_ALG_AUTH, alg_len(x->aalg), x->aalg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 if (x->ealg)
Thomas Grafc26445a2007-08-22 13:56:23 -0700560 NLA_PUT(skb, XFRMA_ALG_CRYPT, alg_len(x->ealg), x->ealg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 if (x->calg)
Thomas Grafc0144be2007-08-22 13:55:43 -0700562 NLA_PUT(skb, XFRMA_ALG_COMP, sizeof(*(x->calg)), x->calg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563
564 if (x->encap)
Thomas Grafc0144be2007-08-22 13:55:43 -0700565 NLA_PUT(skb, XFRMA_ENCAP, sizeof(*x->encap), x->encap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566
Thomas Grafc0144be2007-08-22 13:55:43 -0700567 if (x->security && copy_sec_ctx(x->security, skb) < 0)
568 goto nla_put_failure;
Noriaki TAKAMIYA060f02a2006-08-23 18:18:55 -0700569
570 if (x->coaddr)
Thomas Grafc0144be2007-08-22 13:55:43 -0700571 NLA_PUT(skb, XFRMA_COADDR, sizeof(*x->coaddr), x->coaddr);
Noriaki TAKAMIYA060f02a2006-08-23 18:18:55 -0700572
Masahide NAKAMURA9afaca02006-08-23 18:20:16 -0700573 if (x->lastused)
Thomas Grafc0144be2007-08-22 13:55:43 -0700574 NLA_PUT_U64(skb, XFRMA_LASTUSED, x->lastused);
Masahide NAKAMURA9afaca02006-08-23 18:20:16 -0700575
Thomas Graf98250692007-08-22 12:47:26 -0700576 nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577out:
578 sp->this_idx++;
579 return 0;
580
Thomas Grafc0144be2007-08-22 13:55:43 -0700581nla_put_failure:
Thomas Graf98250692007-08-22 12:47:26 -0700582 nlmsg_cancel(skb, nlh);
583 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584}
585
586static int xfrm_dump_sa(struct sk_buff *skb, struct netlink_callback *cb)
587{
588 struct xfrm_dump_info info;
589
590 info.in_skb = cb->skb;
591 info.out_skb = skb;
592 info.nlmsg_seq = cb->nlh->nlmsg_seq;
593 info.nlmsg_flags = NLM_F_MULTI;
594 info.this_idx = 0;
595 info.start_idx = cb->args[0];
Masahide NAKAMURAdc00a522006-08-23 17:49:52 -0700596 (void) xfrm_state_walk(0, dump_one_state, &info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 cb->args[0] = info.this_idx;
598
599 return skb->len;
600}
601
602static struct sk_buff *xfrm_state_netlink(struct sk_buff *in_skb,
603 struct xfrm_state *x, u32 seq)
604{
605 struct xfrm_dump_info info;
606 struct sk_buff *skb;
607
Thomas Graf7deb2262007-08-22 13:57:39 -0700608 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 if (!skb)
610 return ERR_PTR(-ENOMEM);
611
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 info.in_skb = in_skb;
613 info.out_skb = skb;
614 info.nlmsg_seq = seq;
615 info.nlmsg_flags = 0;
616 info.this_idx = info.start_idx = 0;
617
618 if (dump_one_state(x, 0, &info)) {
619 kfree_skb(skb);
620 return NULL;
621 }
622
623 return skb;
624}
625
Thomas Graf7deb2262007-08-22 13:57:39 -0700626static inline size_t xfrm_spdinfo_msgsize(void)
627{
628 return NLMSG_ALIGN(4)
629 + nla_total_size(sizeof(struct xfrmu_spdinfo))
630 + nla_total_size(sizeof(struct xfrmu_spdhinfo));
631}
632
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700633static int build_spdinfo(struct sk_buff *skb, u32 pid, u32 seq, u32 flags)
634{
Jamal Hadi Salim5a6d3412007-05-04 12:55:39 -0700635 struct xfrmk_spdinfo si;
636 struct xfrmu_spdinfo spc;
637 struct xfrmu_spdhinfo sph;
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700638 struct nlmsghdr *nlh;
639 u32 *f;
640
641 nlh = nlmsg_put(skb, pid, seq, XFRM_MSG_NEWSPDINFO, sizeof(u32), 0);
642 if (nlh == NULL) /* shouldnt really happen ... */
643 return -EMSGSIZE;
644
645 f = nlmsg_data(nlh);
646 *f = flags;
647 xfrm_spd_getinfo(&si);
Jamal Hadi Salim5a6d3412007-05-04 12:55:39 -0700648 spc.incnt = si.incnt;
649 spc.outcnt = si.outcnt;
650 spc.fwdcnt = si.fwdcnt;
651 spc.inscnt = si.inscnt;
652 spc.outscnt = si.outscnt;
653 spc.fwdscnt = si.fwdscnt;
654 sph.spdhcnt = si.spdhcnt;
655 sph.spdhmcnt = si.spdhmcnt;
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700656
Jamal Hadi Salim5a6d3412007-05-04 12:55:39 -0700657 NLA_PUT(skb, XFRMA_SPD_INFO, sizeof(spc), &spc);
658 NLA_PUT(skb, XFRMA_SPD_HINFO, sizeof(sph), &sph);
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700659
660 return nlmsg_end(skb, nlh);
661
662nla_put_failure:
663 nlmsg_cancel(skb, nlh);
664 return -EMSGSIZE;
665}
666
667static int xfrm_get_spdinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -0700668 struct nlattr **attrs)
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700669{
670 struct sk_buff *r_skb;
Thomas Graf7b67c852007-08-22 13:53:52 -0700671 u32 *flags = nlmsg_data(nlh);
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700672 u32 spid = NETLINK_CB(skb).pid;
673 u32 seq = nlh->nlmsg_seq;
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700674
Thomas Graf7deb2262007-08-22 13:57:39 -0700675 r_skb = nlmsg_new(xfrm_spdinfo_msgsize(), GFP_ATOMIC);
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700676 if (r_skb == NULL)
677 return -ENOMEM;
678
679 if (build_spdinfo(r_skb, spid, seq, *flags) < 0)
680 BUG();
681
682 return nlmsg_unicast(xfrm_nl, r_skb, spid);
683}
684
Thomas Graf7deb2262007-08-22 13:57:39 -0700685static inline size_t xfrm_sadinfo_msgsize(void)
686{
687 return NLMSG_ALIGN(4)
688 + nla_total_size(sizeof(struct xfrmu_sadhinfo))
689 + nla_total_size(4); /* XFRMA_SAD_CNT */
690}
691
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700692static int build_sadinfo(struct sk_buff *skb, u32 pid, u32 seq, u32 flags)
693{
Jamal Hadi Salimaf11e312007-05-04 12:55:13 -0700694 struct xfrmk_sadinfo si;
695 struct xfrmu_sadhinfo sh;
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700696 struct nlmsghdr *nlh;
697 u32 *f;
698
699 nlh = nlmsg_put(skb, pid, seq, XFRM_MSG_NEWSADINFO, sizeof(u32), 0);
700 if (nlh == NULL) /* shouldnt really happen ... */
701 return -EMSGSIZE;
702
703 f = nlmsg_data(nlh);
704 *f = flags;
705 xfrm_sad_getinfo(&si);
706
Jamal Hadi Salimaf11e312007-05-04 12:55:13 -0700707 sh.sadhmcnt = si.sadhmcnt;
708 sh.sadhcnt = si.sadhcnt;
709
710 NLA_PUT_U32(skb, XFRMA_SAD_CNT, si.sadcnt);
711 NLA_PUT(skb, XFRMA_SAD_HINFO, sizeof(sh), &sh);
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700712
713 return nlmsg_end(skb, nlh);
714
715nla_put_failure:
716 nlmsg_cancel(skb, nlh);
717 return -EMSGSIZE;
718}
719
720static int xfrm_get_sadinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -0700721 struct nlattr **attrs)
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700722{
723 struct sk_buff *r_skb;
Thomas Graf7b67c852007-08-22 13:53:52 -0700724 u32 *flags = nlmsg_data(nlh);
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700725 u32 spid = NETLINK_CB(skb).pid;
726 u32 seq = nlh->nlmsg_seq;
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700727
Thomas Graf7deb2262007-08-22 13:57:39 -0700728 r_skb = nlmsg_new(xfrm_sadinfo_msgsize(), GFP_ATOMIC);
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700729 if (r_skb == NULL)
730 return -ENOMEM;
731
732 if (build_sadinfo(r_skb, spid, seq, *flags) < 0)
733 BUG();
734
735 return nlmsg_unicast(xfrm_nl, r_skb, spid);
736}
737
Christoph Hellwig22e70052007-01-02 15:22:30 -0800738static int xfrm_get_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -0700739 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740{
Thomas Graf7b67c852007-08-22 13:53:52 -0700741 struct xfrm_usersa_id *p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 struct xfrm_state *x;
743 struct sk_buff *resp_skb;
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700744 int err = -ESRCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745
Thomas Graf35a7aa02007-08-22 14:00:40 -0700746 x = xfrm_user_state_lookup(p, attrs, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 if (x == NULL)
748 goto out_noput;
749
750 resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
751 if (IS_ERR(resp_skb)) {
752 err = PTR_ERR(resp_skb);
753 } else {
Thomas Graf082a1ad2007-08-22 13:54:36 -0700754 err = nlmsg_unicast(xfrm_nl, resp_skb, NETLINK_CB(skb).pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 }
756 xfrm_state_put(x);
757out_noput:
758 return err;
759}
760
761static int verify_userspi_info(struct xfrm_userspi_info *p)
762{
763 switch (p->info.id.proto) {
764 case IPPROTO_AH:
765 case IPPROTO_ESP:
766 break;
767
768 case IPPROTO_COMP:
769 /* IPCOMP spi is 16-bits. */
770 if (p->max >= 0x10000)
771 return -EINVAL;
772 break;
773
774 default:
775 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700776 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777
778 if (p->min > p->max)
779 return -EINVAL;
780
781 return 0;
782}
783
Christoph Hellwig22e70052007-01-02 15:22:30 -0800784static int xfrm_alloc_userspi(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -0700785 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786{
787 struct xfrm_state *x;
788 struct xfrm_userspi_info *p;
789 struct sk_buff *resp_skb;
790 xfrm_address_t *daddr;
791 int family;
792 int err;
793
Thomas Graf7b67c852007-08-22 13:53:52 -0700794 p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 err = verify_userspi_info(p);
796 if (err)
797 goto out_noput;
798
799 family = p->info.family;
800 daddr = &p->info.id.daddr;
801
802 x = NULL;
803 if (p->info.seq) {
804 x = xfrm_find_acq_byseq(p->info.seq);
805 if (x && xfrm_addr_cmp(&x->id.daddr, daddr, family)) {
806 xfrm_state_put(x);
807 x = NULL;
808 }
809 }
810
811 if (!x)
812 x = xfrm_find_acq(p->info.mode, p->info.reqid,
813 p->info.id.proto, daddr,
814 &p->info.saddr, 1,
815 family);
816 err = -ENOENT;
817 if (x == NULL)
818 goto out_noput;
819
820 resp_skb = ERR_PTR(-ENOENT);
821
822 spin_lock_bh(&x->lock);
823 if (x->km.state != XFRM_STATE_DEAD) {
824 xfrm_alloc_spi(x, htonl(p->min), htonl(p->max));
825 if (x->id.spi)
826 resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
827 }
828 spin_unlock_bh(&x->lock);
829
830 if (IS_ERR(resp_skb)) {
831 err = PTR_ERR(resp_skb);
832 goto out;
833 }
834
Thomas Graf082a1ad2007-08-22 13:54:36 -0700835 err = nlmsg_unicast(xfrm_nl, resp_skb, NETLINK_CB(skb).pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836
837out:
838 xfrm_state_put(x);
839out_noput:
840 return err;
841}
842
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -0800843static int verify_policy_dir(u8 dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844{
845 switch (dir) {
846 case XFRM_POLICY_IN:
847 case XFRM_POLICY_OUT:
848 case XFRM_POLICY_FWD:
849 break;
850
851 default:
852 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700853 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854
855 return 0;
856}
857
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -0800858static int verify_policy_type(u8 type)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -0700859{
860 switch (type) {
861 case XFRM_POLICY_TYPE_MAIN:
862#ifdef CONFIG_XFRM_SUB_POLICY
863 case XFRM_POLICY_TYPE_SUB:
864#endif
865 break;
866
867 default:
868 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700869 }
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -0700870
871 return 0;
872}
873
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874static int verify_newpolicy_info(struct xfrm_userpolicy_info *p)
875{
876 switch (p->share) {
877 case XFRM_SHARE_ANY:
878 case XFRM_SHARE_SESSION:
879 case XFRM_SHARE_USER:
880 case XFRM_SHARE_UNIQUE:
881 break;
882
883 default:
884 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700885 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886
887 switch (p->action) {
888 case XFRM_POLICY_ALLOW:
889 case XFRM_POLICY_BLOCK:
890 break;
891
892 default:
893 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700894 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895
896 switch (p->sel.family) {
897 case AF_INET:
898 break;
899
900 case AF_INET6:
901#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
902 break;
903#else
904 return -EAFNOSUPPORT;
905#endif
906
907 default:
908 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700909 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910
911 return verify_policy_dir(p->dir);
912}
913
Thomas Graf5424f322007-08-22 14:01:33 -0700914static int copy_from_user_sec_ctx(struct xfrm_policy *pol, struct nlattr **attrs)
Trent Jaegerdf718372005-12-13 23:12:27 -0800915{
Thomas Graf5424f322007-08-22 14:01:33 -0700916 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
Trent Jaegerdf718372005-12-13 23:12:27 -0800917 struct xfrm_user_sec_ctx *uctx;
918
919 if (!rt)
920 return 0;
921
Thomas Graf5424f322007-08-22 14:01:33 -0700922 uctx = nla_data(rt);
Trent Jaegerdf718372005-12-13 23:12:27 -0800923 return security_xfrm_policy_alloc(pol, uctx);
924}
925
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926static void copy_templates(struct xfrm_policy *xp, struct xfrm_user_tmpl *ut,
927 int nr)
928{
929 int i;
930
931 xp->xfrm_nr = nr;
932 for (i = 0; i < nr; i++, ut++) {
933 struct xfrm_tmpl *t = &xp->xfrm_vec[i];
934
935 memcpy(&t->id, &ut->id, sizeof(struct xfrm_id));
936 memcpy(&t->saddr, &ut->saddr,
937 sizeof(xfrm_address_t));
938 t->reqid = ut->reqid;
939 t->mode = ut->mode;
940 t->share = ut->share;
941 t->optional = ut->optional;
942 t->aalgos = ut->aalgos;
943 t->ealgos = ut->ealgos;
944 t->calgos = ut->calgos;
Miika Komu8511d012006-11-30 16:40:51 -0800945 t->encap_family = ut->family;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 }
947}
948
David S. Millerb4ad86bf2006-12-03 19:19:26 -0800949static int validate_tmpl(int nr, struct xfrm_user_tmpl *ut, u16 family)
950{
951 int i;
952
953 if (nr > XFRM_MAX_DEPTH)
954 return -EINVAL;
955
956 for (i = 0; i < nr; i++) {
957 /* We never validated the ut->family value, so many
958 * applications simply leave it at zero. The check was
959 * never made and ut->family was ignored because all
960 * templates could be assumed to have the same family as
961 * the policy itself. Now that we will have ipv4-in-ipv6
962 * and ipv6-in-ipv4 tunnels, this is no longer true.
963 */
964 if (!ut[i].family)
965 ut[i].family = family;
966
967 switch (ut[i].family) {
968 case AF_INET:
969 break;
970#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
971 case AF_INET6:
972 break;
973#endif
974 default:
975 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700976 }
David S. Millerb4ad86bf2006-12-03 19:19:26 -0800977 }
978
979 return 0;
980}
981
Thomas Graf5424f322007-08-22 14:01:33 -0700982static int copy_from_user_tmpl(struct xfrm_policy *pol, struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983{
Thomas Graf5424f322007-08-22 14:01:33 -0700984 struct nlattr *rt = attrs[XFRMA_TMPL];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985
986 if (!rt) {
987 pol->xfrm_nr = 0;
988 } else {
Thomas Graf5424f322007-08-22 14:01:33 -0700989 struct xfrm_user_tmpl *utmpl = nla_data(rt);
990 int nr = nla_len(rt) / sizeof(*utmpl);
David S. Millerb4ad86bf2006-12-03 19:19:26 -0800991 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992
David S. Millerb4ad86bf2006-12-03 19:19:26 -0800993 err = validate_tmpl(nr, utmpl, pol->family);
994 if (err)
995 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996
Thomas Graf5424f322007-08-22 14:01:33 -0700997 copy_templates(pol, utmpl, nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 }
999 return 0;
1000}
1001
Thomas Graf5424f322007-08-22 14:01:33 -07001002static int copy_from_user_policy_type(u8 *tp, struct nlattr **attrs)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001003{
Thomas Graf5424f322007-08-22 14:01:33 -07001004 struct nlattr *rt = attrs[XFRMA_POLICY_TYPE];
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001005 struct xfrm_userpolicy_type *upt;
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001006 u8 type = XFRM_POLICY_TYPE_MAIN;
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001007 int err;
1008
1009 if (rt) {
Thomas Graf5424f322007-08-22 14:01:33 -07001010 upt = nla_data(rt);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001011 type = upt->type;
1012 }
1013
1014 err = verify_policy_type(type);
1015 if (err)
1016 return err;
1017
1018 *tp = type;
1019 return 0;
1020}
1021
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022static void copy_from_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p)
1023{
1024 xp->priority = p->priority;
1025 xp->index = p->index;
1026 memcpy(&xp->selector, &p->sel, sizeof(xp->selector));
1027 memcpy(&xp->lft, &p->lft, sizeof(xp->lft));
1028 xp->action = p->action;
1029 xp->flags = p->flags;
1030 xp->family = p->sel.family;
1031 /* XXX xp->share = p->share; */
1032}
1033
1034static void copy_to_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p, int dir)
1035{
1036 memcpy(&p->sel, &xp->selector, sizeof(p->sel));
1037 memcpy(&p->lft, &xp->lft, sizeof(p->lft));
1038 memcpy(&p->curlft, &xp->curlft, sizeof(p->curlft));
1039 p->priority = xp->priority;
1040 p->index = xp->index;
1041 p->sel.family = xp->family;
1042 p->dir = dir;
1043 p->action = xp->action;
1044 p->flags = xp->flags;
1045 p->share = XFRM_SHARE_ANY; /* XXX xp->share */
1046}
1047
Thomas Graf5424f322007-08-22 14:01:33 -07001048static struct xfrm_policy *xfrm_policy_construct(struct xfrm_userpolicy_info *p, struct nlattr **attrs, int *errp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049{
1050 struct xfrm_policy *xp = xfrm_policy_alloc(GFP_KERNEL);
1051 int err;
1052
1053 if (!xp) {
1054 *errp = -ENOMEM;
1055 return NULL;
1056 }
1057
1058 copy_from_user_policy(xp, p);
Trent Jaegerdf718372005-12-13 23:12:27 -08001059
Thomas Graf35a7aa02007-08-22 14:00:40 -07001060 err = copy_from_user_policy_type(&xp->type, attrs);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001061 if (err)
1062 goto error;
1063
Thomas Graf35a7aa02007-08-22 14:00:40 -07001064 if (!(err = copy_from_user_tmpl(xp, attrs)))
1065 err = copy_from_user_sec_ctx(xp, attrs);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001066 if (err)
1067 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068
1069 return xp;
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001070 error:
1071 *errp = err;
1072 kfree(xp);
1073 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074}
1075
Christoph Hellwig22e70052007-01-02 15:22:30 -08001076static int xfrm_add_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001077 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078{
Thomas Graf7b67c852007-08-22 13:53:52 -07001079 struct xfrm_userpolicy_info *p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 struct xfrm_policy *xp;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001081 struct km_event c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 int err;
1083 int excl;
1084
1085 err = verify_newpolicy_info(p);
1086 if (err)
1087 return err;
Thomas Graf35a7aa02007-08-22 14:00:40 -07001088 err = verify_sec_ctx_len(attrs);
Trent Jaegerdf718372005-12-13 23:12:27 -08001089 if (err)
1090 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091
Thomas Graf35a7aa02007-08-22 14:00:40 -07001092 xp = xfrm_policy_construct(p, attrs, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 if (!xp)
1094 return err;
1095
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001096 /* shouldnt excl be based on nlh flags??
1097 * Aha! this is anti-netlink really i.e more pfkey derived
1098 * in netlink excl is a flag and you wouldnt need
1099 * a type XFRM_MSG_UPDPOLICY - JHS */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 excl = nlh->nlmsg_type == XFRM_MSG_NEWPOLICY;
1101 err = xfrm_policy_insert(p->dir, xp, excl);
Joy Latten161a09e2006-11-27 13:11:54 -06001102 xfrm_audit_log(NETLINK_CB(skb).loginuid, NETLINK_CB(skb).sid,
1103 AUDIT_MAC_IPSEC_DELSPD, err ? 0 : 1, xp, NULL);
1104
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 if (err) {
Trent Jaeger5f8ac642006-01-06 13:22:39 -08001106 security_xfrm_policy_free(xp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 kfree(xp);
1108 return err;
1109 }
1110
Herbert Xuf60f6b82005-06-18 22:44:37 -07001111 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001112 c.seq = nlh->nlmsg_seq;
1113 c.pid = nlh->nlmsg_pid;
1114 km_policy_notify(xp, p->dir, &c);
1115
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 xfrm_pol_put(xp);
1117
1118 return 0;
1119}
1120
1121static int copy_to_user_tmpl(struct xfrm_policy *xp, struct sk_buff *skb)
1122{
1123 struct xfrm_user_tmpl vec[XFRM_MAX_DEPTH];
1124 int i;
1125
1126 if (xp->xfrm_nr == 0)
1127 return 0;
1128
1129 for (i = 0; i < xp->xfrm_nr; i++) {
1130 struct xfrm_user_tmpl *up = &vec[i];
1131 struct xfrm_tmpl *kp = &xp->xfrm_vec[i];
1132
1133 memcpy(&up->id, &kp->id, sizeof(up->id));
Miika Komu8511d012006-11-30 16:40:51 -08001134 up->family = kp->encap_family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135 memcpy(&up->saddr, &kp->saddr, sizeof(up->saddr));
1136 up->reqid = kp->reqid;
1137 up->mode = kp->mode;
1138 up->share = kp->share;
1139 up->optional = kp->optional;
1140 up->aalgos = kp->aalgos;
1141 up->ealgos = kp->ealgos;
1142 up->calgos = kp->calgos;
1143 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144
Thomas Grafc0144be2007-08-22 13:55:43 -07001145 return nla_put(skb, XFRMA_TMPL,
1146 sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr, vec);
Trent Jaegerdf718372005-12-13 23:12:27 -08001147}
1148
Serge Hallyn0d681622006-07-24 23:30:44 -07001149static inline int copy_to_user_state_sec_ctx(struct xfrm_state *x, struct sk_buff *skb)
1150{
1151 if (x->security) {
1152 return copy_sec_ctx(x->security, skb);
1153 }
1154 return 0;
1155}
1156
1157static inline int copy_to_user_sec_ctx(struct xfrm_policy *xp, struct sk_buff *skb)
1158{
1159 if (xp->security) {
1160 return copy_sec_ctx(xp->security, skb);
1161 }
1162 return 0;
1163}
Thomas Grafcfbfd452007-08-22 13:57:04 -07001164static inline size_t userpolicy_type_attrsize(void)
1165{
1166#ifdef CONFIG_XFRM_SUB_POLICY
1167 return nla_total_size(sizeof(struct xfrm_userpolicy_type));
1168#else
1169 return 0;
1170#endif
1171}
Serge Hallyn0d681622006-07-24 23:30:44 -07001172
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001173#ifdef CONFIG_XFRM_SUB_POLICY
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001174static int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001175{
Thomas Grafc0144be2007-08-22 13:55:43 -07001176 struct xfrm_userpolicy_type upt = {
1177 .type = type,
1178 };
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001179
Thomas Grafc0144be2007-08-22 13:55:43 -07001180 return nla_put(skb, XFRMA_POLICY_TYPE, sizeof(upt), &upt);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001181}
1182
1183#else
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001184static inline int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001185{
1186 return 0;
1187}
1188#endif
1189
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190static int dump_one_policy(struct xfrm_policy *xp, int dir, int count, void *ptr)
1191{
1192 struct xfrm_dump_info *sp = ptr;
1193 struct xfrm_userpolicy_info *p;
1194 struct sk_buff *in_skb = sp->in_skb;
1195 struct sk_buff *skb = sp->out_skb;
1196 struct nlmsghdr *nlh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197
1198 if (sp->this_idx < sp->start_idx)
1199 goto out;
1200
Thomas Graf79b8b7f2007-08-22 12:46:53 -07001201 nlh = nlmsg_put(skb, NETLINK_CB(in_skb).pid, sp->nlmsg_seq,
1202 XFRM_MSG_NEWPOLICY, sizeof(*p), sp->nlmsg_flags);
1203 if (nlh == NULL)
1204 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205
Thomas Graf7b67c852007-08-22 13:53:52 -07001206 p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207 copy_to_user_policy(xp, p, dir);
1208 if (copy_to_user_tmpl(xp, skb) < 0)
1209 goto nlmsg_failure;
Trent Jaegerdf718372005-12-13 23:12:27 -08001210 if (copy_to_user_sec_ctx(xp, skb))
1211 goto nlmsg_failure;
Jamal Hadi Salim1459bb32006-11-20 16:51:22 -08001212 if (copy_to_user_policy_type(xp->type, skb) < 0)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001213 goto nlmsg_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214
Thomas Graf98250692007-08-22 12:47:26 -07001215 nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216out:
1217 sp->this_idx++;
1218 return 0;
1219
1220nlmsg_failure:
Thomas Graf98250692007-08-22 12:47:26 -07001221 nlmsg_cancel(skb, nlh);
1222 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223}
1224
1225static int xfrm_dump_policy(struct sk_buff *skb, struct netlink_callback *cb)
1226{
1227 struct xfrm_dump_info info;
1228
1229 info.in_skb = cb->skb;
1230 info.out_skb = skb;
1231 info.nlmsg_seq = cb->nlh->nlmsg_seq;
1232 info.nlmsg_flags = NLM_F_MULTI;
1233 info.this_idx = 0;
1234 info.start_idx = cb->args[0];
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001235 (void) xfrm_policy_walk(XFRM_POLICY_TYPE_MAIN, dump_one_policy, &info);
1236#ifdef CONFIG_XFRM_SUB_POLICY
1237 (void) xfrm_policy_walk(XFRM_POLICY_TYPE_SUB, dump_one_policy, &info);
1238#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 cb->args[0] = info.this_idx;
1240
1241 return skb->len;
1242}
1243
1244static struct sk_buff *xfrm_policy_netlink(struct sk_buff *in_skb,
1245 struct xfrm_policy *xp,
1246 int dir, u32 seq)
1247{
1248 struct xfrm_dump_info info;
1249 struct sk_buff *skb;
1250
Thomas Graf7deb2262007-08-22 13:57:39 -07001251 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 if (!skb)
1253 return ERR_PTR(-ENOMEM);
1254
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255 info.in_skb = in_skb;
1256 info.out_skb = skb;
1257 info.nlmsg_seq = seq;
1258 info.nlmsg_flags = 0;
1259 info.this_idx = info.start_idx = 0;
1260
1261 if (dump_one_policy(xp, dir, 0, &info) < 0) {
1262 kfree_skb(skb);
1263 return NULL;
1264 }
1265
1266 return skb;
1267}
1268
Christoph Hellwig22e70052007-01-02 15:22:30 -08001269static int xfrm_get_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001270 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271{
1272 struct xfrm_policy *xp;
1273 struct xfrm_userpolicy_id *p;
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001274 u8 type = XFRM_POLICY_TYPE_MAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 int err;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001276 struct km_event c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 int delete;
1278
Thomas Graf7b67c852007-08-22 13:53:52 -07001279 p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 delete = nlh->nlmsg_type == XFRM_MSG_DELPOLICY;
1281
Thomas Graf35a7aa02007-08-22 14:00:40 -07001282 err = copy_from_user_policy_type(&type, attrs);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001283 if (err)
1284 return err;
1285
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286 err = verify_policy_dir(p->dir);
1287 if (err)
1288 return err;
1289
1290 if (p->index)
Eric Parisef41aaa2007-03-07 15:37:58 -08001291 xp = xfrm_policy_byid(type, p->dir, p->index, delete, &err);
Trent Jaegerdf718372005-12-13 23:12:27 -08001292 else {
Thomas Graf5424f322007-08-22 14:01:33 -07001293 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
Trent Jaegerdf718372005-12-13 23:12:27 -08001294 struct xfrm_policy tmp;
1295
Thomas Graf35a7aa02007-08-22 14:00:40 -07001296 err = verify_sec_ctx_len(attrs);
Trent Jaegerdf718372005-12-13 23:12:27 -08001297 if (err)
1298 return err;
1299
1300 memset(&tmp, 0, sizeof(struct xfrm_policy));
1301 if (rt) {
Thomas Graf5424f322007-08-22 14:01:33 -07001302 struct xfrm_user_sec_ctx *uctx = nla_data(rt);
Trent Jaegerdf718372005-12-13 23:12:27 -08001303
1304 if ((err = security_xfrm_policy_alloc(&tmp, uctx)))
1305 return err;
1306 }
Eric Parisef41aaa2007-03-07 15:37:58 -08001307 xp = xfrm_policy_bysel_ctx(type, p->dir, &p->sel, tmp.security,
1308 delete, &err);
Trent Jaegerdf718372005-12-13 23:12:27 -08001309 security_xfrm_policy_free(&tmp);
1310 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311 if (xp == NULL)
1312 return -ENOENT;
1313
1314 if (!delete) {
1315 struct sk_buff *resp_skb;
1316
1317 resp_skb = xfrm_policy_netlink(skb, xp, p->dir, nlh->nlmsg_seq);
1318 if (IS_ERR(resp_skb)) {
1319 err = PTR_ERR(resp_skb);
1320 } else {
Thomas Graf082a1ad2007-08-22 13:54:36 -07001321 err = nlmsg_unicast(xfrm_nl, resp_skb,
1322 NETLINK_CB(skb).pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 }
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001324 } else {
David S. Miller13fcfbb2007-02-12 13:53:54 -08001325 xfrm_audit_log(NETLINK_CB(skb).loginuid, NETLINK_CB(skb).sid,
1326 AUDIT_MAC_IPSEC_DELSPD, err ? 0 : 1, xp, NULL);
1327
1328 if (err != 0)
Catherine Zhangc8c05a82006-06-08 23:39:49 -07001329 goto out;
David S. Miller13fcfbb2007-02-12 13:53:54 -08001330
Herbert Xue7443892005-06-18 22:44:18 -07001331 c.data.byid = p->index;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001332 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001333 c.seq = nlh->nlmsg_seq;
1334 c.pid = nlh->nlmsg_pid;
1335 km_policy_notify(xp, p->dir, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336 }
1337
Catherine Zhangc8c05a82006-06-08 23:39:49 -07001338out:
Eric Parisef41aaa2007-03-07 15:37:58 -08001339 xfrm_pol_put(xp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340 return err;
1341}
1342
Christoph Hellwig22e70052007-01-02 15:22:30 -08001343static int xfrm_flush_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001344 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345{
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001346 struct km_event c;
Thomas Graf7b67c852007-08-22 13:53:52 -07001347 struct xfrm_usersa_flush *p = nlmsg_data(nlh);
Joy Latten161a09e2006-11-27 13:11:54 -06001348 struct xfrm_audit audit_info;
Joy Latten4aa2e622007-06-04 19:05:57 -04001349 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350
Joy Latten161a09e2006-11-27 13:11:54 -06001351 audit_info.loginuid = NETLINK_CB(skb).loginuid;
1352 audit_info.secid = NETLINK_CB(skb).sid;
Joy Latten4aa2e622007-06-04 19:05:57 -04001353 err = xfrm_state_flush(p->proto, &audit_info);
1354 if (err)
1355 return err;
Herbert Xubf088672005-06-18 22:44:00 -07001356 c.data.proto = p->proto;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001357 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001358 c.seq = nlh->nlmsg_seq;
1359 c.pid = nlh->nlmsg_pid;
1360 km_state_notify(NULL, &c);
1361
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 return 0;
1363}
1364
Thomas Graf7deb2262007-08-22 13:57:39 -07001365static inline size_t xfrm_aevent_msgsize(void)
1366{
1367 return NLMSG_ALIGN(sizeof(struct xfrm_aevent_id))
1368 + nla_total_size(sizeof(struct xfrm_replay_state))
1369 + nla_total_size(sizeof(struct xfrm_lifetime_cur))
1370 + nla_total_size(4) /* XFRM_AE_RTHR */
1371 + nla_total_size(4); /* XFRM_AE_ETHR */
1372}
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001373
1374static int build_aevent(struct sk_buff *skb, struct xfrm_state *x, struct km_event *c)
1375{
1376 struct xfrm_aevent_id *id;
1377 struct nlmsghdr *nlh;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001378
Thomas Graf79b8b7f2007-08-22 12:46:53 -07001379 nlh = nlmsg_put(skb, c->pid, c->seq, XFRM_MSG_NEWAE, sizeof(*id), 0);
1380 if (nlh == NULL)
1381 return -EMSGSIZE;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001382
Thomas Graf7b67c852007-08-22 13:53:52 -07001383 id = nlmsg_data(nlh);
Jamal Hadi Salim2b5f6dc2006-12-02 22:22:25 -08001384 memcpy(&id->sa_id.daddr, &x->id.daddr,sizeof(x->id.daddr));
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001385 id->sa_id.spi = x->id.spi;
1386 id->sa_id.family = x->props.family;
1387 id->sa_id.proto = x->id.proto;
Jamal Hadi Salim2b5f6dc2006-12-02 22:22:25 -08001388 memcpy(&id->saddr, &x->props.saddr,sizeof(x->props.saddr));
1389 id->reqid = x->props.reqid;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001390 id->flags = c->data.aevent;
1391
Thomas Grafc0144be2007-08-22 13:55:43 -07001392 NLA_PUT(skb, XFRMA_REPLAY_VAL, sizeof(x->replay), &x->replay);
1393 NLA_PUT(skb, XFRMA_LTIME_VAL, sizeof(x->curlft), &x->curlft);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001394
Thomas Grafc0144be2007-08-22 13:55:43 -07001395 if (id->flags & XFRM_AE_RTHR)
1396 NLA_PUT_U32(skb, XFRMA_REPLAY_THRESH, x->replay_maxdiff);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001397
Thomas Grafc0144be2007-08-22 13:55:43 -07001398 if (id->flags & XFRM_AE_ETHR)
1399 NLA_PUT_U32(skb, XFRMA_ETIMER_THRESH,
1400 x->replay_maxage * 10 / HZ);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001401
Thomas Graf98250692007-08-22 12:47:26 -07001402 return nlmsg_end(skb, nlh);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001403
Thomas Grafc0144be2007-08-22 13:55:43 -07001404nla_put_failure:
Thomas Graf98250692007-08-22 12:47:26 -07001405 nlmsg_cancel(skb, nlh);
1406 return -EMSGSIZE;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001407}
1408
Christoph Hellwig22e70052007-01-02 15:22:30 -08001409static int xfrm_get_ae(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001410 struct nlattr **attrs)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001411{
1412 struct xfrm_state *x;
1413 struct sk_buff *r_skb;
1414 int err;
1415 struct km_event c;
Thomas Graf7b67c852007-08-22 13:53:52 -07001416 struct xfrm_aevent_id *p = nlmsg_data(nlh);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001417 struct xfrm_usersa_id *id = &p->sa_id;
1418
Thomas Graf7deb2262007-08-22 13:57:39 -07001419 r_skb = nlmsg_new(xfrm_aevent_msgsize(), GFP_ATOMIC);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001420 if (r_skb == NULL)
1421 return -ENOMEM;
1422
1423 x = xfrm_state_lookup(&id->daddr, id->spi, id->proto, id->family);
1424 if (x == NULL) {
Patrick McHardyb08d5842007-02-27 09:57:37 -08001425 kfree_skb(r_skb);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001426 return -ESRCH;
1427 }
1428
1429 /*
1430 * XXX: is this lock really needed - none of the other
1431 * gets lock (the concern is things getting updated
1432 * while we are still reading) - jhs
1433 */
1434 spin_lock_bh(&x->lock);
1435 c.data.aevent = p->flags;
1436 c.seq = nlh->nlmsg_seq;
1437 c.pid = nlh->nlmsg_pid;
1438
1439 if (build_aevent(r_skb, x, &c) < 0)
1440 BUG();
Thomas Graf082a1ad2007-08-22 13:54:36 -07001441 err = nlmsg_unicast(xfrm_nl, r_skb, NETLINK_CB(skb).pid);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001442 spin_unlock_bh(&x->lock);
1443 xfrm_state_put(x);
1444 return err;
1445}
1446
Christoph Hellwig22e70052007-01-02 15:22:30 -08001447static int xfrm_new_ae(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001448 struct nlattr **attrs)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001449{
1450 struct xfrm_state *x;
1451 struct km_event c;
1452 int err = - EINVAL;
Thomas Graf7b67c852007-08-22 13:53:52 -07001453 struct xfrm_aevent_id *p = nlmsg_data(nlh);
Thomas Graf5424f322007-08-22 14:01:33 -07001454 struct nlattr *rp = attrs[XFRMA_REPLAY_VAL];
1455 struct nlattr *lt = attrs[XFRMA_LTIME_VAL];
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001456
1457 if (!lt && !rp)
1458 return err;
1459
1460 /* pedantic mode - thou shalt sayeth replaceth */
1461 if (!(nlh->nlmsg_flags&NLM_F_REPLACE))
1462 return err;
1463
1464 x = xfrm_state_lookup(&p->sa_id.daddr, p->sa_id.spi, p->sa_id.proto, p->sa_id.family);
1465 if (x == NULL)
1466 return -ESRCH;
1467
1468 if (x->km.state != XFRM_STATE_VALID)
1469 goto out;
1470
1471 spin_lock_bh(&x->lock);
Thomas Graf35a7aa02007-08-22 14:00:40 -07001472 xfrm_update_ae_params(x, attrs);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001473 spin_unlock_bh(&x->lock);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001474
1475 c.event = nlh->nlmsg_type;
1476 c.seq = nlh->nlmsg_seq;
1477 c.pid = nlh->nlmsg_pid;
1478 c.data.aevent = XFRM_AE_CU;
1479 km_state_notify(x, &c);
1480 err = 0;
1481out:
1482 xfrm_state_put(x);
1483 return err;
1484}
1485
Christoph Hellwig22e70052007-01-02 15:22:30 -08001486static int xfrm_flush_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001487 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488{
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001489 struct km_event c;
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001490 u8 type = XFRM_POLICY_TYPE_MAIN;
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001491 int err;
Joy Latten161a09e2006-11-27 13:11:54 -06001492 struct xfrm_audit audit_info;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001493
Thomas Graf35a7aa02007-08-22 14:00:40 -07001494 err = copy_from_user_policy_type(&type, attrs);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001495 if (err)
1496 return err;
1497
Joy Latten161a09e2006-11-27 13:11:54 -06001498 audit_info.loginuid = NETLINK_CB(skb).loginuid;
1499 audit_info.secid = NETLINK_CB(skb).sid;
Joy Latten4aa2e622007-06-04 19:05:57 -04001500 err = xfrm_policy_flush(type, &audit_info);
1501 if (err)
1502 return err;
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001503 c.data.type = type;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001504 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001505 c.seq = nlh->nlmsg_seq;
1506 c.pid = nlh->nlmsg_pid;
1507 km_policy_notify(NULL, 0, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508 return 0;
1509}
1510
Christoph Hellwig22e70052007-01-02 15:22:30 -08001511static int xfrm_add_pol_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001512 struct nlattr **attrs)
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001513{
1514 struct xfrm_policy *xp;
Thomas Graf7b67c852007-08-22 13:53:52 -07001515 struct xfrm_user_polexpire *up = nlmsg_data(nlh);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001516 struct xfrm_userpolicy_info *p = &up->pol;
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001517 u8 type = XFRM_POLICY_TYPE_MAIN;
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001518 int err = -ENOENT;
1519
Thomas Graf35a7aa02007-08-22 14:00:40 -07001520 err = copy_from_user_policy_type(&type, attrs);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001521 if (err)
1522 return err;
1523
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001524 if (p->index)
Eric Parisef41aaa2007-03-07 15:37:58 -08001525 xp = xfrm_policy_byid(type, p->dir, p->index, 0, &err);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001526 else {
Thomas Graf5424f322007-08-22 14:01:33 -07001527 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001528 struct xfrm_policy tmp;
1529
Thomas Graf35a7aa02007-08-22 14:00:40 -07001530 err = verify_sec_ctx_len(attrs);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001531 if (err)
1532 return err;
1533
1534 memset(&tmp, 0, sizeof(struct xfrm_policy));
1535 if (rt) {
Thomas Graf5424f322007-08-22 14:01:33 -07001536 struct xfrm_user_sec_ctx *uctx = nla_data(rt);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001537
1538 if ((err = security_xfrm_policy_alloc(&tmp, uctx)))
1539 return err;
1540 }
Eric Parisef41aaa2007-03-07 15:37:58 -08001541 xp = xfrm_policy_bysel_ctx(type, p->dir, &p->sel, tmp.security,
1542 0, &err);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001543 security_xfrm_policy_free(&tmp);
1544 }
1545
1546 if (xp == NULL)
Eric Parisef41aaa2007-03-07 15:37:58 -08001547 return -ENOENT;
1548 read_lock(&xp->lock);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001549 if (xp->dead) {
1550 read_unlock(&xp->lock);
1551 goto out;
1552 }
1553
1554 read_unlock(&xp->lock);
1555 err = 0;
1556 if (up->hard) {
1557 xfrm_policy_delete(xp, p->dir);
Joy Latten161a09e2006-11-27 13:11:54 -06001558 xfrm_audit_log(NETLINK_CB(skb).loginuid, NETLINK_CB(skb).sid,
1559 AUDIT_MAC_IPSEC_DELSPD, 1, xp, NULL);
1560
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001561 } else {
1562 // reset the timers here?
1563 printk("Dont know what to do with soft policy expire\n");
1564 }
1565 km_policy_expired(xp, p->dir, up->hard, current->pid);
1566
1567out:
1568 xfrm_pol_put(xp);
1569 return err;
1570}
1571
Christoph Hellwig22e70052007-01-02 15:22:30 -08001572static int xfrm_add_sa_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001573 struct nlattr **attrs)
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001574{
1575 struct xfrm_state *x;
1576 int err;
Thomas Graf7b67c852007-08-22 13:53:52 -07001577 struct xfrm_user_expire *ue = nlmsg_data(nlh);
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001578 struct xfrm_usersa_info *p = &ue->state;
1579
1580 x = xfrm_state_lookup(&p->id.daddr, p->id.spi, p->id.proto, p->family);
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001581
David S. Miller3a765aa2007-02-26 14:52:21 -08001582 err = -ENOENT;
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001583 if (x == NULL)
1584 return err;
1585
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001586 spin_lock_bh(&x->lock);
David S. Miller3a765aa2007-02-26 14:52:21 -08001587 err = -EINVAL;
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001588 if (x->km.state != XFRM_STATE_VALID)
1589 goto out;
1590 km_state_expired(x, ue->hard, current->pid);
1591
Joy Latten161a09e2006-11-27 13:11:54 -06001592 if (ue->hard) {
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001593 __xfrm_state_delete(x);
Joy Latten161a09e2006-11-27 13:11:54 -06001594 xfrm_audit_log(NETLINK_CB(skb).loginuid, NETLINK_CB(skb).sid,
1595 AUDIT_MAC_IPSEC_DELSA, 1, NULL, x);
1596 }
David S. Miller3a765aa2007-02-26 14:52:21 -08001597 err = 0;
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001598out:
1599 spin_unlock_bh(&x->lock);
1600 xfrm_state_put(x);
1601 return err;
1602}
1603
Christoph Hellwig22e70052007-01-02 15:22:30 -08001604static int xfrm_add_acquire(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001605 struct nlattr **attrs)
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001606{
1607 struct xfrm_policy *xp;
1608 struct xfrm_user_tmpl *ut;
1609 int i;
Thomas Graf5424f322007-08-22 14:01:33 -07001610 struct nlattr *rt = attrs[XFRMA_TMPL];
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001611
Thomas Graf7b67c852007-08-22 13:53:52 -07001612 struct xfrm_user_acquire *ua = nlmsg_data(nlh);
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001613 struct xfrm_state *x = xfrm_state_alloc();
1614 int err = -ENOMEM;
1615
1616 if (!x)
1617 return err;
1618
1619 err = verify_newpolicy_info(&ua->policy);
1620 if (err) {
1621 printk("BAD policy passed\n");
1622 kfree(x);
1623 return err;
1624 }
1625
1626 /* build an XP */
Thomas Graf5424f322007-08-22 14:01:33 -07001627 xp = xfrm_policy_construct(&ua->policy, attrs, &err);
David S. Millerb4ad86bf2006-12-03 19:19:26 -08001628 if (!xp) {
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001629 kfree(x);
1630 return err;
1631 }
1632
1633 memcpy(&x->id, &ua->id, sizeof(ua->id));
1634 memcpy(&x->props.saddr, &ua->saddr, sizeof(ua->saddr));
1635 memcpy(&x->sel, &ua->sel, sizeof(ua->sel));
1636
Thomas Graf5424f322007-08-22 14:01:33 -07001637 ut = nla_data(rt);
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001638 /* extract the templates and for each call km_key */
1639 for (i = 0; i < xp->xfrm_nr; i++, ut++) {
1640 struct xfrm_tmpl *t = &xp->xfrm_vec[i];
1641 memcpy(&x->id, &t->id, sizeof(x->id));
1642 x->props.mode = t->mode;
1643 x->props.reqid = t->reqid;
1644 x->props.family = ut->family;
1645 t->aalgos = ua->aalgos;
1646 t->ealgos = ua->ealgos;
1647 t->calgos = ua->calgos;
1648 err = km_query(x, t, xp);
1649
1650 }
1651
1652 kfree(x);
1653 kfree(xp);
1654
1655 return 0;
1656}
1657
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001658#ifdef CONFIG_XFRM_MIGRATE
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001659static int copy_from_user_migrate(struct xfrm_migrate *ma,
Thomas Graf5424f322007-08-22 14:01:33 -07001660 struct nlattr **attrs, int *num)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001661{
Thomas Graf5424f322007-08-22 14:01:33 -07001662 struct nlattr *rt = attrs[XFRMA_MIGRATE];
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001663 struct xfrm_user_migrate *um;
1664 int i, num_migrate;
1665
Thomas Graf5424f322007-08-22 14:01:33 -07001666 um = nla_data(rt);
1667 num_migrate = nla_len(rt) / sizeof(*um);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001668
1669 if (num_migrate <= 0 || num_migrate > XFRM_MAX_DEPTH)
1670 return -EINVAL;
1671
1672 for (i = 0; i < num_migrate; i++, um++, ma++) {
1673 memcpy(&ma->old_daddr, &um->old_daddr, sizeof(ma->old_daddr));
1674 memcpy(&ma->old_saddr, &um->old_saddr, sizeof(ma->old_saddr));
1675 memcpy(&ma->new_daddr, &um->new_daddr, sizeof(ma->new_daddr));
1676 memcpy(&ma->new_saddr, &um->new_saddr, sizeof(ma->new_saddr));
1677
1678 ma->proto = um->proto;
1679 ma->mode = um->mode;
1680 ma->reqid = um->reqid;
1681
1682 ma->old_family = um->old_family;
1683 ma->new_family = um->new_family;
1684 }
1685
1686 *num = i;
1687 return 0;
1688}
1689
1690static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001691 struct nlattr **attrs)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001692{
Thomas Graf7b67c852007-08-22 13:53:52 -07001693 struct xfrm_userpolicy_id *pi = nlmsg_data(nlh);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001694 struct xfrm_migrate m[XFRM_MAX_DEPTH];
1695 u8 type;
1696 int err;
1697 int n = 0;
1698
Thomas Graf35a7aa02007-08-22 14:00:40 -07001699 if (attrs[XFRMA_MIGRATE] == NULL)
Thomas Grafcf5cb792007-08-22 13:59:04 -07001700 return -EINVAL;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001701
Thomas Graf5424f322007-08-22 14:01:33 -07001702 err = copy_from_user_policy_type(&type, attrs);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001703 if (err)
1704 return err;
1705
1706 err = copy_from_user_migrate((struct xfrm_migrate *)m,
Thomas Graf5424f322007-08-22 14:01:33 -07001707 attrs, &n);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001708 if (err)
1709 return err;
1710
1711 if (!n)
1712 return 0;
1713
1714 xfrm_migrate(&pi->sel, pi->dir, type, m, n);
1715
1716 return 0;
1717}
1718#else
1719static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001720 struct nlattr **attrs)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001721{
1722 return -ENOPROTOOPT;
1723}
1724#endif
1725
1726#ifdef CONFIG_XFRM_MIGRATE
1727static int copy_to_user_migrate(struct xfrm_migrate *m, struct sk_buff *skb)
1728{
1729 struct xfrm_user_migrate um;
1730
1731 memset(&um, 0, sizeof(um));
1732 um.proto = m->proto;
1733 um.mode = m->mode;
1734 um.reqid = m->reqid;
1735 um.old_family = m->old_family;
1736 memcpy(&um.old_daddr, &m->old_daddr, sizeof(um.old_daddr));
1737 memcpy(&um.old_saddr, &m->old_saddr, sizeof(um.old_saddr));
1738 um.new_family = m->new_family;
1739 memcpy(&um.new_daddr, &m->new_daddr, sizeof(um.new_daddr));
1740 memcpy(&um.new_saddr, &m->new_saddr, sizeof(um.new_saddr));
1741
Thomas Grafc0144be2007-08-22 13:55:43 -07001742 return nla_put(skb, XFRMA_MIGRATE, sizeof(um), &um);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001743}
1744
Thomas Graf7deb2262007-08-22 13:57:39 -07001745static inline size_t xfrm_migrate_msgsize(int num_migrate)
1746{
1747 return NLMSG_ALIGN(sizeof(struct xfrm_userpolicy_id))
1748 + nla_total_size(sizeof(struct xfrm_user_migrate) * num_migrate)
1749 + userpolicy_type_attrsize();
1750}
1751
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001752static int build_migrate(struct sk_buff *skb, struct xfrm_migrate *m,
1753 int num_migrate, struct xfrm_selector *sel,
1754 u8 dir, u8 type)
1755{
1756 struct xfrm_migrate *mp;
1757 struct xfrm_userpolicy_id *pol_id;
1758 struct nlmsghdr *nlh;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001759 int i;
1760
Thomas Graf79b8b7f2007-08-22 12:46:53 -07001761 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_MIGRATE, sizeof(*pol_id), 0);
1762 if (nlh == NULL)
1763 return -EMSGSIZE;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001764
Thomas Graf7b67c852007-08-22 13:53:52 -07001765 pol_id = nlmsg_data(nlh);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001766 /* copy data from selector, dir, and type to the pol_id */
1767 memset(pol_id, 0, sizeof(*pol_id));
1768 memcpy(&pol_id->sel, sel, sizeof(pol_id->sel));
1769 pol_id->dir = dir;
1770
1771 if (copy_to_user_policy_type(type, skb) < 0)
1772 goto nlmsg_failure;
1773
1774 for (i = 0, mp = m ; i < num_migrate; i++, mp++) {
1775 if (copy_to_user_migrate(mp, skb) < 0)
1776 goto nlmsg_failure;
1777 }
1778
Thomas Graf98250692007-08-22 12:47:26 -07001779 return nlmsg_end(skb, nlh);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001780nlmsg_failure:
Thomas Graf98250692007-08-22 12:47:26 -07001781 nlmsg_cancel(skb, nlh);
1782 return -EMSGSIZE;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001783}
1784
1785static int xfrm_send_migrate(struct xfrm_selector *sel, u8 dir, u8 type,
1786 struct xfrm_migrate *m, int num_migrate)
1787{
1788 struct sk_buff *skb;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001789
Thomas Graf7deb2262007-08-22 13:57:39 -07001790 skb = nlmsg_new(xfrm_migrate_msgsize(num_migrate), GFP_ATOMIC);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001791 if (skb == NULL)
1792 return -ENOMEM;
1793
1794 /* build migrate */
1795 if (build_migrate(skb, m, num_migrate, sel, dir, type) < 0)
1796 BUG();
1797
Thomas Graf082a1ad2007-08-22 13:54:36 -07001798 return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_MIGRATE, GFP_ATOMIC);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001799}
1800#else
1801static int xfrm_send_migrate(struct xfrm_selector *sel, u8 dir, u8 type,
1802 struct xfrm_migrate *m, int num_migrate)
1803{
1804 return -ENOPROTOOPT;
1805}
1806#endif
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001807
Thomas Grafa7bd9a42007-08-22 13:58:18 -07001808#define XMSGSIZE(type) sizeof(struct type)
Thomas Graf492b5582005-05-03 14:26:40 -07001809
1810static const int xfrm_msg_min[XFRM_NR_MSGTYPES] = {
1811 [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
1812 [XFRM_MSG_DELSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
1813 [XFRM_MSG_GETSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
1814 [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
1815 [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
1816 [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
1817 [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userspi_info),
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001818 [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_acquire),
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001819 [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_expire),
Thomas Graf492b5582005-05-03 14:26:40 -07001820 [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
1821 [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001822 [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_polexpire),
Thomas Graf492b5582005-05-03 14:26:40 -07001823 [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_flush),
Thomas Grafa7bd9a42007-08-22 13:58:18 -07001824 [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = 0,
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001825 [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
1826 [XFRM_MSG_GETAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07001827 [XFRM_MSG_REPORT - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_report),
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001828 [XFRM_MSG_MIGRATE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
Thomas Grafa7bd9a42007-08-22 13:58:18 -07001829 [XFRM_MSG_GETSADINFO - XFRM_MSG_BASE] = sizeof(u32),
1830 [XFRM_MSG_GETSPDINFO - XFRM_MSG_BASE] = sizeof(u32),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831};
1832
Thomas Graf492b5582005-05-03 14:26:40 -07001833#undef XMSGSIZE
1834
Thomas Grafcf5cb792007-08-22 13:59:04 -07001835static const struct nla_policy xfrma_policy[XFRMA_MAX+1] = {
1836 [XFRMA_ALG_AUTH] = { .len = sizeof(struct xfrm_algo) },
1837 [XFRMA_ALG_CRYPT] = { .len = sizeof(struct xfrm_algo) },
1838 [XFRMA_ALG_COMP] = { .len = sizeof(struct xfrm_algo) },
1839 [XFRMA_ENCAP] = { .len = sizeof(struct xfrm_encap_tmpl) },
1840 [XFRMA_TMPL] = { .len = sizeof(struct xfrm_user_tmpl) },
1841 [XFRMA_SEC_CTX] = { .len = sizeof(struct xfrm_sec_ctx) },
1842 [XFRMA_LTIME_VAL] = { .len = sizeof(struct xfrm_lifetime_cur) },
1843 [XFRMA_REPLAY_VAL] = { .len = sizeof(struct xfrm_replay_state) },
1844 [XFRMA_REPLAY_THRESH] = { .type = NLA_U32 },
1845 [XFRMA_ETIMER_THRESH] = { .type = NLA_U32 },
1846 [XFRMA_SRCADDR] = { .len = sizeof(xfrm_address_t) },
1847 [XFRMA_COADDR] = { .len = sizeof(xfrm_address_t) },
1848 [XFRMA_POLICY_TYPE] = { .len = sizeof(struct xfrm_userpolicy_type)},
1849 [XFRMA_MIGRATE] = { .len = sizeof(struct xfrm_user_migrate) },
1850};
1851
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852static struct xfrm_link {
Thomas Graf5424f322007-08-22 14:01:33 -07001853 int (*doit)(struct sk_buff *, struct nlmsghdr *, struct nlattr **);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854 int (*dump)(struct sk_buff *, struct netlink_callback *);
Thomas Graf492b5582005-05-03 14:26:40 -07001855} xfrm_dispatch[XFRM_NR_MSGTYPES] = {
1856 [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa },
1857 [XFRM_MSG_DELSA - XFRM_MSG_BASE] = { .doit = xfrm_del_sa },
1858 [XFRM_MSG_GETSA - XFRM_MSG_BASE] = { .doit = xfrm_get_sa,
1859 .dump = xfrm_dump_sa },
1860 [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy },
1861 [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy },
1862 [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy,
1863 .dump = xfrm_dump_policy },
1864 [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = { .doit = xfrm_alloc_userspi },
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001865 [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_acquire },
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001866 [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_sa_expire },
Thomas Graf492b5582005-05-03 14:26:40 -07001867 [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy },
1868 [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa },
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001869 [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_pol_expire},
Thomas Graf492b5582005-05-03 14:26:40 -07001870 [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = { .doit = xfrm_flush_sa },
1871 [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_flush_policy },
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001872 [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = { .doit = xfrm_new_ae },
1873 [XFRM_MSG_GETAE - XFRM_MSG_BASE] = { .doit = xfrm_get_ae },
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001874 [XFRM_MSG_MIGRATE - XFRM_MSG_BASE] = { .doit = xfrm_do_migrate },
Jamal Hadi Salim566ec032007-04-26 14:12:15 -07001875 [XFRM_MSG_GETSADINFO - XFRM_MSG_BASE] = { .doit = xfrm_get_sadinfo },
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -07001876 [XFRM_MSG_GETSPDINFO - XFRM_MSG_BASE] = { .doit = xfrm_get_spdinfo },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877};
1878
Thomas Graf1d00a4e2007-03-22 23:30:12 -07001879static int xfrm_user_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880{
Thomas Graf35a7aa02007-08-22 14:00:40 -07001881 struct nlattr *attrs[XFRMA_MAX+1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882 struct xfrm_link *link;
Thomas Grafa7bd9a42007-08-22 13:58:18 -07001883 int type, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885 type = nlh->nlmsg_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886 if (type > XFRM_MSG_MAX)
Thomas Graf1d00a4e2007-03-22 23:30:12 -07001887 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888
1889 type -= XFRM_MSG_BASE;
1890 link = &xfrm_dispatch[type];
1891
1892 /* All operations require privileges, even GET */
Thomas Graf1d00a4e2007-03-22 23:30:12 -07001893 if (security_netlink_recv(skb, CAP_NET_ADMIN))
1894 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895
Thomas Graf492b5582005-05-03 14:26:40 -07001896 if ((type == (XFRM_MSG_GETSA - XFRM_MSG_BASE) ||
1897 type == (XFRM_MSG_GETPOLICY - XFRM_MSG_BASE)) &&
1898 (nlh->nlmsg_flags & NLM_F_DUMP)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899 if (link->dump == NULL)
Thomas Graf1d00a4e2007-03-22 23:30:12 -07001900 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901
Thomas Grafc702e802007-03-22 23:30:55 -07001902 return netlink_dump_start(xfrm_nl, skb, nlh, link->dump, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903 }
1904
Thomas Graf35a7aa02007-08-22 14:00:40 -07001905 err = nlmsg_parse(nlh, xfrm_msg_min[type], attrs, XFRMA_MAX,
Thomas Grafcf5cb792007-08-22 13:59:04 -07001906 xfrma_policy);
Thomas Grafa7bd9a42007-08-22 13:58:18 -07001907 if (err < 0)
1908 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909
1910 if (link->doit == NULL)
Thomas Graf1d00a4e2007-03-22 23:30:12 -07001911 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912
Thomas Graf5424f322007-08-22 14:01:33 -07001913 return link->doit(skb, nlh, attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914}
1915
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916static void xfrm_netlink_rcv(struct sock *sk, int len)
1917{
Thomas Graf88fc2c82005-11-10 02:25:54 +01001918 unsigned int qlen = 0;
Herbert Xu2a0a6eb2005-05-03 14:55:09 -07001919
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920 do {
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -08001921 mutex_lock(&xfrm_cfg_mutex);
Thomas Graf88fc2c82005-11-10 02:25:54 +01001922 netlink_run_queue(sk, &qlen, &xfrm_user_rcv_msg);
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -08001923 mutex_unlock(&xfrm_cfg_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924
Herbert Xu2a0a6eb2005-05-03 14:55:09 -07001925 } while (qlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926}
1927
Thomas Graf7deb2262007-08-22 13:57:39 -07001928static inline size_t xfrm_expire_msgsize(void)
1929{
1930 return NLMSG_ALIGN(sizeof(struct xfrm_user_expire));
1931}
1932
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001933static int build_expire(struct sk_buff *skb, struct xfrm_state *x, struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934{
1935 struct xfrm_user_expire *ue;
1936 struct nlmsghdr *nlh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937
Thomas Graf79b8b7f2007-08-22 12:46:53 -07001938 nlh = nlmsg_put(skb, c->pid, 0, XFRM_MSG_EXPIRE, sizeof(*ue), 0);
1939 if (nlh == NULL)
1940 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941
Thomas Graf7b67c852007-08-22 13:53:52 -07001942 ue = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943 copy_to_user_state(x, &ue->state);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001944 ue->hard = (c->data.hard != 0) ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945
Thomas Graf98250692007-08-22 12:47:26 -07001946 return nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947}
1948
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001949static int xfrm_exp_state_notify(struct xfrm_state *x, struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950{
1951 struct sk_buff *skb;
1952
Thomas Graf7deb2262007-08-22 13:57:39 -07001953 skb = nlmsg_new(xfrm_expire_msgsize(), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954 if (skb == NULL)
1955 return -ENOMEM;
1956
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001957 if (build_expire(skb, x, c) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958 BUG();
1959
Thomas Graf082a1ad2007-08-22 13:54:36 -07001960 return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_EXPIRE, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961}
1962
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001963static int xfrm_aevent_state_notify(struct xfrm_state *x, struct km_event *c)
1964{
1965 struct sk_buff *skb;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001966
Thomas Graf7deb2262007-08-22 13:57:39 -07001967 skb = nlmsg_new(xfrm_aevent_msgsize(), GFP_ATOMIC);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001968 if (skb == NULL)
1969 return -ENOMEM;
1970
1971 if (build_aevent(skb, x, c) < 0)
1972 BUG();
1973
Thomas Graf082a1ad2007-08-22 13:54:36 -07001974 return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_AEVENTS, GFP_ATOMIC);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001975}
1976
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001977static int xfrm_notify_sa_flush(struct km_event *c)
1978{
1979 struct xfrm_usersa_flush *p;
1980 struct nlmsghdr *nlh;
1981 struct sk_buff *skb;
Thomas Graf7deb2262007-08-22 13:57:39 -07001982 int len = NLMSG_ALIGN(sizeof(struct xfrm_usersa_flush));
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001983
Thomas Graf7deb2262007-08-22 13:57:39 -07001984 skb = nlmsg_new(len, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001985 if (skb == NULL)
1986 return -ENOMEM;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001987
Thomas Graf79b8b7f2007-08-22 12:46:53 -07001988 nlh = nlmsg_put(skb, c->pid, c->seq, XFRM_MSG_FLUSHSA, sizeof(*p), 0);
1989 if (nlh == NULL) {
1990 kfree_skb(skb);
1991 return -EMSGSIZE;
1992 }
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001993
Thomas Graf7b67c852007-08-22 13:53:52 -07001994 p = nlmsg_data(nlh);
Herbert Xubf088672005-06-18 22:44:00 -07001995 p->proto = c->data.proto;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001996
Thomas Graf98250692007-08-22 12:47:26 -07001997 nlmsg_end(skb, nlh);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001998
Thomas Graf082a1ad2007-08-22 13:54:36 -07001999 return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_SA, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002000}
2001
Thomas Graf7deb2262007-08-22 13:57:39 -07002002static inline size_t xfrm_sa_len(struct xfrm_state *x)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002003{
Thomas Graf7deb2262007-08-22 13:57:39 -07002004 size_t l = 0;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002005 if (x->aalg)
Thomas Graf7deb2262007-08-22 13:57:39 -07002006 l += nla_total_size(alg_len(x->aalg));
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002007 if (x->ealg)
Thomas Graf7deb2262007-08-22 13:57:39 -07002008 l += nla_total_size(alg_len(x->ealg));
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002009 if (x->calg)
Thomas Graf7deb2262007-08-22 13:57:39 -07002010 l += nla_total_size(sizeof(*x->calg));
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002011 if (x->encap)
Thomas Graf7deb2262007-08-22 13:57:39 -07002012 l += nla_total_size(sizeof(*x->encap));
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002013
2014 return l;
2015}
2016
2017static int xfrm_notify_sa(struct xfrm_state *x, struct km_event *c)
2018{
2019 struct xfrm_usersa_info *p;
Herbert Xu0603eac2005-06-18 22:54:36 -07002020 struct xfrm_usersa_id *id;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002021 struct nlmsghdr *nlh;
2022 struct sk_buff *skb;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002023 int len = xfrm_sa_len(x);
Herbert Xu0603eac2005-06-18 22:54:36 -07002024 int headlen;
2025
2026 headlen = sizeof(*p);
2027 if (c->event == XFRM_MSG_DELSA) {
Thomas Graf7deb2262007-08-22 13:57:39 -07002028 len += nla_total_size(headlen);
Herbert Xu0603eac2005-06-18 22:54:36 -07002029 headlen = sizeof(*id);
2030 }
Thomas Graf7deb2262007-08-22 13:57:39 -07002031 len += NLMSG_ALIGN(headlen);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002032
Thomas Graf7deb2262007-08-22 13:57:39 -07002033 skb = nlmsg_new(len, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002034 if (skb == NULL)
2035 return -ENOMEM;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002036
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002037 nlh = nlmsg_put(skb, c->pid, c->seq, c->event, headlen, 0);
2038 if (nlh == NULL)
Thomas Grafc0144be2007-08-22 13:55:43 -07002039 goto nla_put_failure;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002040
Thomas Graf7b67c852007-08-22 13:53:52 -07002041 p = nlmsg_data(nlh);
Herbert Xu0603eac2005-06-18 22:54:36 -07002042 if (c->event == XFRM_MSG_DELSA) {
Thomas Grafc0144be2007-08-22 13:55:43 -07002043 struct nlattr *attr;
2044
Thomas Graf7b67c852007-08-22 13:53:52 -07002045 id = nlmsg_data(nlh);
Herbert Xu0603eac2005-06-18 22:54:36 -07002046 memcpy(&id->daddr, &x->id.daddr, sizeof(id->daddr));
2047 id->spi = x->id.spi;
2048 id->family = x->props.family;
2049 id->proto = x->id.proto;
2050
Thomas Grafc0144be2007-08-22 13:55:43 -07002051 attr = nla_reserve(skb, XFRMA_SA, sizeof(*p));
2052 if (attr == NULL)
2053 goto nla_put_failure;
2054
2055 p = nla_data(attr);
Herbert Xu0603eac2005-06-18 22:54:36 -07002056 }
2057
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002058 copy_to_user_state(x, p);
2059
2060 if (x->aalg)
Thomas Grafc26445a2007-08-22 13:56:23 -07002061 NLA_PUT(skb, XFRMA_ALG_AUTH, alg_len(x->aalg), x->aalg);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002062 if (x->ealg)
Thomas Grafc26445a2007-08-22 13:56:23 -07002063 NLA_PUT(skb, XFRMA_ALG_CRYPT, alg_len(x->ealg), x->ealg);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002064 if (x->calg)
Thomas Grafc0144be2007-08-22 13:55:43 -07002065 NLA_PUT(skb, XFRMA_ALG_COMP, sizeof(*(x->calg)), x->calg);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002066
2067 if (x->encap)
Thomas Grafc0144be2007-08-22 13:55:43 -07002068 NLA_PUT(skb, XFRMA_ENCAP, sizeof(*x->encap), x->encap);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002069
Thomas Graf98250692007-08-22 12:47:26 -07002070 nlmsg_end(skb, nlh);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002071
Thomas Graf082a1ad2007-08-22 13:54:36 -07002072 return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_SA, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002073
Thomas Grafc0144be2007-08-22 13:55:43 -07002074nla_put_failure:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002075 kfree_skb(skb);
2076 return -1;
2077}
2078
2079static int xfrm_send_state_notify(struct xfrm_state *x, struct km_event *c)
2080{
2081
2082 switch (c->event) {
Herbert Xuf60f6b82005-06-18 22:44:37 -07002083 case XFRM_MSG_EXPIRE:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002084 return xfrm_exp_state_notify(x, c);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002085 case XFRM_MSG_NEWAE:
2086 return xfrm_aevent_state_notify(x, c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07002087 case XFRM_MSG_DELSA:
2088 case XFRM_MSG_UPDSA:
2089 case XFRM_MSG_NEWSA:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002090 return xfrm_notify_sa(x, c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07002091 case XFRM_MSG_FLUSHSA:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002092 return xfrm_notify_sa_flush(c);
2093 default:
2094 printk("xfrm_user: Unknown SA event %d\n", c->event);
2095 break;
2096 }
2097
2098 return 0;
2099
2100}
2101
Thomas Graf7deb2262007-08-22 13:57:39 -07002102static inline size_t xfrm_acquire_msgsize(struct xfrm_state *x,
2103 struct xfrm_policy *xp)
2104{
2105 return NLMSG_ALIGN(sizeof(struct xfrm_user_acquire))
2106 + nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr)
2107 + nla_total_size(xfrm_user_sec_ctx_size(x->security))
2108 + userpolicy_type_attrsize();
2109}
2110
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111static int build_acquire(struct sk_buff *skb, struct xfrm_state *x,
2112 struct xfrm_tmpl *xt, struct xfrm_policy *xp,
2113 int dir)
2114{
2115 struct xfrm_user_acquire *ua;
2116 struct nlmsghdr *nlh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117 __u32 seq = xfrm_get_acqseq();
2118
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002119 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_ACQUIRE, sizeof(*ua), 0);
2120 if (nlh == NULL)
2121 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122
Thomas Graf7b67c852007-08-22 13:53:52 -07002123 ua = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124 memcpy(&ua->id, &x->id, sizeof(ua->id));
2125 memcpy(&ua->saddr, &x->props.saddr, sizeof(ua->saddr));
2126 memcpy(&ua->sel, &x->sel, sizeof(ua->sel));
2127 copy_to_user_policy(xp, &ua->policy, dir);
2128 ua->aalgos = xt->aalgos;
2129 ua->ealgos = xt->ealgos;
2130 ua->calgos = xt->calgos;
2131 ua->seq = x->km.seq = seq;
2132
2133 if (copy_to_user_tmpl(xp, skb) < 0)
2134 goto nlmsg_failure;
Serge Hallyn0d681622006-07-24 23:30:44 -07002135 if (copy_to_user_state_sec_ctx(x, skb))
Trent Jaegerdf718372005-12-13 23:12:27 -08002136 goto nlmsg_failure;
Jamal Hadi Salim1459bb32006-11-20 16:51:22 -08002137 if (copy_to_user_policy_type(xp->type, skb) < 0)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07002138 goto nlmsg_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139
Thomas Graf98250692007-08-22 12:47:26 -07002140 return nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141
2142nlmsg_failure:
Thomas Graf98250692007-08-22 12:47:26 -07002143 nlmsg_cancel(skb, nlh);
2144 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145}
2146
2147static int xfrm_send_acquire(struct xfrm_state *x, struct xfrm_tmpl *xt,
2148 struct xfrm_policy *xp, int dir)
2149{
2150 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151
Thomas Graf7deb2262007-08-22 13:57:39 -07002152 skb = nlmsg_new(xfrm_acquire_msgsize(x, xp), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153 if (skb == NULL)
2154 return -ENOMEM;
2155
2156 if (build_acquire(skb, x, xt, xp, dir) < 0)
2157 BUG();
2158
Thomas Graf082a1ad2007-08-22 13:54:36 -07002159 return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_ACQUIRE, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160}
2161
2162/* User gives us xfrm_user_policy_info followed by an array of 0
2163 * or more templates.
2164 */
Venkat Yekkiralacb969f02006-07-24 23:32:20 -07002165static struct xfrm_policy *xfrm_compile_policy(struct sock *sk, int opt,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166 u8 *data, int len, int *dir)
2167{
2168 struct xfrm_userpolicy_info *p = (struct xfrm_userpolicy_info *)data;
2169 struct xfrm_user_tmpl *ut = (struct xfrm_user_tmpl *) (p + 1);
2170 struct xfrm_policy *xp;
2171 int nr;
2172
Venkat Yekkiralacb969f02006-07-24 23:32:20 -07002173 switch (sk->sk_family) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174 case AF_INET:
2175 if (opt != IP_XFRM_POLICY) {
2176 *dir = -EOPNOTSUPP;
2177 return NULL;
2178 }
2179 break;
2180#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
2181 case AF_INET6:
2182 if (opt != IPV6_XFRM_POLICY) {
2183 *dir = -EOPNOTSUPP;
2184 return NULL;
2185 }
2186 break;
2187#endif
2188 default:
2189 *dir = -EINVAL;
2190 return NULL;
2191 }
2192
2193 *dir = -EINVAL;
2194
2195 if (len < sizeof(*p) ||
2196 verify_newpolicy_info(p))
2197 return NULL;
2198
2199 nr = ((len - sizeof(*p)) / sizeof(*ut));
David S. Millerb4ad86bf2006-12-03 19:19:26 -08002200 if (validate_tmpl(nr, ut, p->sel.family))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201 return NULL;
2202
Herbert Xua4f1bac2005-07-26 15:43:17 -07002203 if (p->dir > XFRM_POLICY_OUT)
2204 return NULL;
2205
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206 xp = xfrm_policy_alloc(GFP_KERNEL);
2207 if (xp == NULL) {
2208 *dir = -ENOBUFS;
2209 return NULL;
2210 }
2211
2212 copy_from_user_policy(xp, p);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07002213 xp->type = XFRM_POLICY_TYPE_MAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002214 copy_templates(xp, ut, nr);
2215
2216 *dir = p->dir;
2217
2218 return xp;
2219}
2220
Thomas Graf7deb2262007-08-22 13:57:39 -07002221static inline size_t xfrm_polexpire_msgsize(struct xfrm_policy *xp)
2222{
2223 return NLMSG_ALIGN(sizeof(struct xfrm_user_polexpire))
2224 + nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr)
2225 + nla_total_size(xfrm_user_sec_ctx_size(xp->security))
2226 + userpolicy_type_attrsize();
2227}
2228
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229static int build_polexpire(struct sk_buff *skb, struct xfrm_policy *xp,
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002230 int dir, struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231{
2232 struct xfrm_user_polexpire *upe;
2233 struct nlmsghdr *nlh;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002234 int hard = c->data.hard;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002236 nlh = nlmsg_put(skb, c->pid, 0, XFRM_MSG_POLEXPIRE, sizeof(*upe), 0);
2237 if (nlh == NULL)
2238 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002239
Thomas Graf7b67c852007-08-22 13:53:52 -07002240 upe = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241 copy_to_user_policy(xp, &upe->pol, dir);
2242 if (copy_to_user_tmpl(xp, skb) < 0)
2243 goto nlmsg_failure;
Trent Jaegerdf718372005-12-13 23:12:27 -08002244 if (copy_to_user_sec_ctx(xp, skb))
2245 goto nlmsg_failure;
Jamal Hadi Salim1459bb32006-11-20 16:51:22 -08002246 if (copy_to_user_policy_type(xp->type, skb) < 0)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07002247 goto nlmsg_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002248 upe->hard = !!hard;
2249
Thomas Graf98250692007-08-22 12:47:26 -07002250 return nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251
2252nlmsg_failure:
Thomas Graf98250692007-08-22 12:47:26 -07002253 nlmsg_cancel(skb, nlh);
2254 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255}
2256
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002257static int xfrm_exp_policy_notify(struct xfrm_policy *xp, int dir, struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258{
2259 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260
Thomas Graf7deb2262007-08-22 13:57:39 -07002261 skb = nlmsg_new(xfrm_polexpire_msgsize(xp), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262 if (skb == NULL)
2263 return -ENOMEM;
2264
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002265 if (build_polexpire(skb, xp, dir, c) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266 BUG();
2267
Thomas Graf082a1ad2007-08-22 13:54:36 -07002268 return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_EXPIRE, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002269}
2270
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002271static int xfrm_notify_policy(struct xfrm_policy *xp, int dir, struct km_event *c)
2272{
2273 struct xfrm_userpolicy_info *p;
Herbert Xu0603eac2005-06-18 22:54:36 -07002274 struct xfrm_userpolicy_id *id;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002275 struct nlmsghdr *nlh;
2276 struct sk_buff *skb;
Thomas Graf7deb2262007-08-22 13:57:39 -07002277 int len = nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr);
Herbert Xu0603eac2005-06-18 22:54:36 -07002278 int headlen;
2279
2280 headlen = sizeof(*p);
2281 if (c->event == XFRM_MSG_DELPOLICY) {
Thomas Graf7deb2262007-08-22 13:57:39 -07002282 len += nla_total_size(headlen);
Herbert Xu0603eac2005-06-18 22:54:36 -07002283 headlen = sizeof(*id);
2284 }
Thomas Grafcfbfd452007-08-22 13:57:04 -07002285 len += userpolicy_type_attrsize();
Thomas Graf7deb2262007-08-22 13:57:39 -07002286 len += NLMSG_ALIGN(headlen);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002287
Thomas Graf7deb2262007-08-22 13:57:39 -07002288 skb = nlmsg_new(len, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002289 if (skb == NULL)
2290 return -ENOMEM;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002291
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002292 nlh = nlmsg_put(skb, c->pid, c->seq, c->event, headlen, 0);
2293 if (nlh == NULL)
2294 goto nlmsg_failure;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002295
Thomas Graf7b67c852007-08-22 13:53:52 -07002296 p = nlmsg_data(nlh);
Herbert Xu0603eac2005-06-18 22:54:36 -07002297 if (c->event == XFRM_MSG_DELPOLICY) {
Thomas Grafc0144be2007-08-22 13:55:43 -07002298 struct nlattr *attr;
2299
Thomas Graf7b67c852007-08-22 13:53:52 -07002300 id = nlmsg_data(nlh);
Herbert Xu0603eac2005-06-18 22:54:36 -07002301 memset(id, 0, sizeof(*id));
2302 id->dir = dir;
2303 if (c->data.byid)
2304 id->index = xp->index;
2305 else
2306 memcpy(&id->sel, &xp->selector, sizeof(id->sel));
2307
Thomas Grafc0144be2007-08-22 13:55:43 -07002308 attr = nla_reserve(skb, XFRMA_POLICY, sizeof(*p));
2309 if (attr == NULL)
2310 goto nlmsg_failure;
2311
2312 p = nla_data(attr);
Herbert Xu0603eac2005-06-18 22:54:36 -07002313 }
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002314
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002315 copy_to_user_policy(xp, p, dir);
2316 if (copy_to_user_tmpl(xp, skb) < 0)
2317 goto nlmsg_failure;
Jamal Hadi Salim1459bb32006-11-20 16:51:22 -08002318 if (copy_to_user_policy_type(xp->type, skb) < 0)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07002319 goto nlmsg_failure;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002320
Thomas Graf98250692007-08-22 12:47:26 -07002321 nlmsg_end(skb, nlh);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002322
Thomas Graf082a1ad2007-08-22 13:54:36 -07002323 return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_POLICY, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002324
2325nlmsg_failure:
2326 kfree_skb(skb);
2327 return -1;
2328}
2329
2330static int xfrm_notify_policy_flush(struct km_event *c)
2331{
2332 struct nlmsghdr *nlh;
2333 struct sk_buff *skb;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002334
Thomas Graf7deb2262007-08-22 13:57:39 -07002335 skb = nlmsg_new(userpolicy_type_attrsize(), GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002336 if (skb == NULL)
2337 return -ENOMEM;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002338
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002339 nlh = nlmsg_put(skb, c->pid, c->seq, XFRM_MSG_FLUSHPOLICY, 0, 0);
2340 if (nlh == NULL)
2341 goto nlmsg_failure;
Jamal Hadi Salim0c51f532006-11-27 12:58:20 -08002342 if (copy_to_user_policy_type(c->data.type, skb) < 0)
2343 goto nlmsg_failure;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002344
Thomas Graf98250692007-08-22 12:47:26 -07002345 nlmsg_end(skb, nlh);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002346
Thomas Graf082a1ad2007-08-22 13:54:36 -07002347 return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_POLICY, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002348
2349nlmsg_failure:
2350 kfree_skb(skb);
2351 return -1;
2352}
2353
2354static int xfrm_send_policy_notify(struct xfrm_policy *xp, int dir, struct km_event *c)
2355{
2356
2357 switch (c->event) {
Herbert Xuf60f6b82005-06-18 22:44:37 -07002358 case XFRM_MSG_NEWPOLICY:
2359 case XFRM_MSG_UPDPOLICY:
2360 case XFRM_MSG_DELPOLICY:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002361 return xfrm_notify_policy(xp, dir, c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07002362 case XFRM_MSG_FLUSHPOLICY:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002363 return xfrm_notify_policy_flush(c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07002364 case XFRM_MSG_POLEXPIRE:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002365 return xfrm_exp_policy_notify(xp, dir, c);
2366 default:
2367 printk("xfrm_user: Unknown Policy event %d\n", c->event);
2368 }
2369
2370 return 0;
2371
2372}
2373
Thomas Graf7deb2262007-08-22 13:57:39 -07002374static inline size_t xfrm_report_msgsize(void)
2375{
2376 return NLMSG_ALIGN(sizeof(struct xfrm_user_report));
2377}
2378
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002379static int build_report(struct sk_buff *skb, u8 proto,
2380 struct xfrm_selector *sel, xfrm_address_t *addr)
2381{
2382 struct xfrm_user_report *ur;
2383 struct nlmsghdr *nlh;
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002384
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002385 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_REPORT, sizeof(*ur), 0);
2386 if (nlh == NULL)
2387 return -EMSGSIZE;
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002388
Thomas Graf7b67c852007-08-22 13:53:52 -07002389 ur = nlmsg_data(nlh);
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002390 ur->proto = proto;
2391 memcpy(&ur->sel, sel, sizeof(ur->sel));
2392
2393 if (addr)
Thomas Grafc0144be2007-08-22 13:55:43 -07002394 NLA_PUT(skb, XFRMA_COADDR, sizeof(*addr), addr);
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002395
Thomas Graf98250692007-08-22 12:47:26 -07002396 return nlmsg_end(skb, nlh);
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002397
Thomas Grafc0144be2007-08-22 13:55:43 -07002398nla_put_failure:
Thomas Graf98250692007-08-22 12:47:26 -07002399 nlmsg_cancel(skb, nlh);
2400 return -EMSGSIZE;
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002401}
2402
2403static int xfrm_send_report(u8 proto, struct xfrm_selector *sel,
2404 xfrm_address_t *addr)
2405{
2406 struct sk_buff *skb;
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002407
Thomas Graf7deb2262007-08-22 13:57:39 -07002408 skb = nlmsg_new(xfrm_report_msgsize(), GFP_ATOMIC);
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002409 if (skb == NULL)
2410 return -ENOMEM;
2411
2412 if (build_report(skb, proto, sel, addr) < 0)
2413 BUG();
2414
Thomas Graf082a1ad2007-08-22 13:54:36 -07002415 return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_REPORT, GFP_ATOMIC);
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002416}
2417
Linus Torvalds1da177e2005-04-16 15:20:36 -07002418static struct xfrm_mgr netlink_mgr = {
2419 .id = "netlink",
2420 .notify = xfrm_send_state_notify,
2421 .acquire = xfrm_send_acquire,
2422 .compile_policy = xfrm_compile_policy,
2423 .notify_policy = xfrm_send_policy_notify,
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002424 .report = xfrm_send_report,
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002425 .migrate = xfrm_send_migrate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002426};
2427
2428static int __init xfrm_user_init(void)
2429{
Patrick McHardybe336902006-03-20 22:40:54 -08002430 struct sock *nlsk;
2431
Masahide NAKAMURA654b32c2006-08-23 19:12:56 -07002432 printk(KERN_INFO "Initializing XFRM netlink socket\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002433
Patrick McHardybe336902006-03-20 22:40:54 -08002434 nlsk = netlink_kernel_create(NETLINK_XFRM, XFRMNLGRP_MAX,
Patrick McHardyaf65bdf2007-04-20 14:14:21 -07002435 xfrm_netlink_rcv, NULL, THIS_MODULE);
Patrick McHardybe336902006-03-20 22:40:54 -08002436 if (nlsk == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002437 return -ENOMEM;
Patrick McHardybe336902006-03-20 22:40:54 -08002438 rcu_assign_pointer(xfrm_nl, nlsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002439
2440 xfrm_register_km(&netlink_mgr);
2441
2442 return 0;
2443}
2444
2445static void __exit xfrm_user_exit(void)
2446{
Patrick McHardybe336902006-03-20 22:40:54 -08002447 struct sock *nlsk = xfrm_nl;
2448
Linus Torvalds1da177e2005-04-16 15:20:36 -07002449 xfrm_unregister_km(&netlink_mgr);
Patrick McHardybe336902006-03-20 22:40:54 -08002450 rcu_assign_pointer(xfrm_nl, NULL);
2451 synchronize_rcu();
2452 sock_release(nlsk->sk_socket);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002453}
2454
2455module_init(xfrm_user_init);
2456module_exit(xfrm_user_exit);
2457MODULE_LICENSE("GPL");
Harald Welte4fdb3bb2005-08-09 19:40:55 -07002458MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK, NETLINK_XFRM);
Jamal Hadi Salimf8cd5482006-03-20 19:15:11 -08002459