blob: 52c7fce546417a3ff529e4a98d9e7c768c979c3e [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* xfrm_user.c: User interface to configure xfrm engine.
2 *
3 * Copyright (C) 2002 David S. Miller (davem@redhat.com)
4 *
5 * Changes:
6 * Mitsuru KANDA @USAGI
7 * Kazunori MIYAZAWA @USAGI
8 * Kunihiro Ishiguro <kunihiro@ipinfusion.com>
9 * IPv6 support
Trent Jaegerdf718372005-12-13 23:12:27 -080010 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070011 */
12
Herbert Xu9409f382006-08-06 19:49:12 +100013#include <linux/crypto.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/module.h>
15#include <linux/kernel.h>
16#include <linux/types.h>
17#include <linux/slab.h>
18#include <linux/socket.h>
19#include <linux/string.h>
20#include <linux/net.h>
21#include <linux/skbuff.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/pfkeyv2.h>
23#include <linux/ipsec.h>
24#include <linux/init.h>
25#include <linux/security.h>
26#include <net/sock.h>
27#include <net/xfrm.h>
Thomas Graf88fc2c82005-11-10 02:25:54 +010028#include <net/netlink.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <asm/uaccess.h>
Masahide NAKAMURAe23c7192006-08-23 20:33:28 -070030#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
31#include <linux/in6.h>
32#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070033
Thomas Grafc26445a2007-08-22 13:56:23 -070034static inline int alg_len(struct xfrm_algo *alg)
35{
36 return sizeof(*alg) + ((alg->alg_key_len + 7) / 8);
37}
38
Thomas Graf5424f322007-08-22 14:01:33 -070039static int verify_one_alg(struct nlattr **attrs, enum xfrm_attr_type_t type)
Linus Torvalds1da177e2005-04-16 15:20:36 -070040{
Thomas Graf5424f322007-08-22 14:01:33 -070041 struct nlattr *rt = attrs[type];
Linus Torvalds1da177e2005-04-16 15:20:36 -070042 struct xfrm_algo *algp;
43
44 if (!rt)
45 return 0;
46
Thomas Graf5424f322007-08-22 14:01:33 -070047 algp = nla_data(rt);
48 if (nla_len(rt) < alg_len(algp))
Herbert Xu31c26852005-05-19 12:39:49 -070049 return -EINVAL;
50
Linus Torvalds1da177e2005-04-16 15:20:36 -070051 switch (type) {
52 case XFRMA_ALG_AUTH:
53 if (!algp->alg_key_len &&
54 strcmp(algp->alg_name, "digest_null") != 0)
55 return -EINVAL;
56 break;
57
58 case XFRMA_ALG_CRYPT:
59 if (!algp->alg_key_len &&
60 strcmp(algp->alg_name, "cipher_null") != 0)
61 return -EINVAL;
62 break;
63
64 case XFRMA_ALG_COMP:
65 /* Zero length keys are legal. */
66 break;
67
68 default:
69 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -070070 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
72 algp->alg_name[CRYPTO_MAX_ALG_NAME - 1] = '\0';
73 return 0;
74}
75
Thomas Graf5424f322007-08-22 14:01:33 -070076static void verify_one_addr(struct nlattr **attrs, enum xfrm_attr_type_t type,
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -070077 xfrm_address_t **addrp)
78{
Thomas Graf5424f322007-08-22 14:01:33 -070079 struct nlattr *rt = attrs[type];
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -070080
Thomas Grafcf5cb792007-08-22 13:59:04 -070081 if (rt && addrp)
Thomas Graf5424f322007-08-22 14:01:33 -070082 *addrp = nla_data(rt);
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -070083}
Trent Jaegerdf718372005-12-13 23:12:27 -080084
Thomas Graf5424f322007-08-22 14:01:33 -070085static inline int verify_sec_ctx_len(struct nlattr **attrs)
Trent Jaegerdf718372005-12-13 23:12:27 -080086{
Thomas Graf5424f322007-08-22 14:01:33 -070087 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
Trent Jaegerdf718372005-12-13 23:12:27 -080088 struct xfrm_user_sec_ctx *uctx;
Trent Jaegerdf718372005-12-13 23:12:27 -080089
90 if (!rt)
91 return 0;
92
Thomas Graf5424f322007-08-22 14:01:33 -070093 uctx = nla_data(rt);
Thomas Grafcf5cb792007-08-22 13:59:04 -070094 if (uctx->len != (sizeof(struct xfrm_user_sec_ctx) + uctx->ctx_len))
Trent Jaegerdf718372005-12-13 23:12:27 -080095 return -EINVAL;
96
97 return 0;
98}
99
100
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101static int verify_newsa_info(struct xfrm_usersa_info *p,
Thomas Graf5424f322007-08-22 14:01:33 -0700102 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103{
104 int err;
105
106 err = -EINVAL;
107 switch (p->family) {
108 case AF_INET:
109 break;
110
111 case AF_INET6:
112#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
113 break;
114#else
115 err = -EAFNOSUPPORT;
116 goto out;
117#endif
118
119 default:
120 goto out;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700121 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122
123 err = -EINVAL;
124 switch (p->id.proto) {
125 case IPPROTO_AH:
Thomas Graf35a7aa02007-08-22 14:00:40 -0700126 if (!attrs[XFRMA_ALG_AUTH] ||
127 attrs[XFRMA_ALG_CRYPT] ||
128 attrs[XFRMA_ALG_COMP])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 goto out;
130 break;
131
132 case IPPROTO_ESP:
Thomas Graf35a7aa02007-08-22 14:00:40 -0700133 if ((!attrs[XFRMA_ALG_AUTH] &&
134 !attrs[XFRMA_ALG_CRYPT]) ||
135 attrs[XFRMA_ALG_COMP])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 goto out;
137 break;
138
139 case IPPROTO_COMP:
Thomas Graf35a7aa02007-08-22 14:00:40 -0700140 if (!attrs[XFRMA_ALG_COMP] ||
141 attrs[XFRMA_ALG_AUTH] ||
142 attrs[XFRMA_ALG_CRYPT])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 goto out;
144 break;
145
Masahide NAKAMURAe23c7192006-08-23 20:33:28 -0700146#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
147 case IPPROTO_DSTOPTS:
148 case IPPROTO_ROUTING:
Thomas Graf35a7aa02007-08-22 14:00:40 -0700149 if (attrs[XFRMA_ALG_COMP] ||
150 attrs[XFRMA_ALG_AUTH] ||
151 attrs[XFRMA_ALG_CRYPT] ||
152 attrs[XFRMA_ENCAP] ||
153 attrs[XFRMA_SEC_CTX] ||
154 !attrs[XFRMA_COADDR])
Masahide NAKAMURAe23c7192006-08-23 20:33:28 -0700155 goto out;
156 break;
157#endif
158
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 default:
160 goto out;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700161 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162
Thomas Graf35a7aa02007-08-22 14:00:40 -0700163 if ((err = verify_one_alg(attrs, XFRMA_ALG_AUTH)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 goto out;
Thomas Graf35a7aa02007-08-22 14:00:40 -0700165 if ((err = verify_one_alg(attrs, XFRMA_ALG_CRYPT)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 goto out;
Thomas Graf35a7aa02007-08-22 14:00:40 -0700167 if ((err = verify_one_alg(attrs, XFRMA_ALG_COMP)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 goto out;
Thomas Graf35a7aa02007-08-22 14:00:40 -0700169 if ((err = verify_sec_ctx_len(attrs)))
Trent Jaegerdf718372005-12-13 23:12:27 -0800170 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171
172 err = -EINVAL;
173 switch (p->mode) {
Masahide NAKAMURA7e49e6d2006-09-22 15:05:15 -0700174 case XFRM_MODE_TRANSPORT:
175 case XFRM_MODE_TUNNEL:
Noriaki TAKAMIYA060f02a2006-08-23 18:18:55 -0700176 case XFRM_MODE_ROUTEOPTIMIZATION:
Diego Beltrami0a694522006-10-03 23:47:05 -0700177 case XFRM_MODE_BEET:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 break;
179
180 default:
181 goto out;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700182 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183
184 err = 0;
185
186out:
187 return err;
188}
189
190static int attach_one_algo(struct xfrm_algo **algpp, u8 *props,
191 struct xfrm_algo_desc *(*get_byname)(char *, int),
Thomas Graf5424f322007-08-22 14:01:33 -0700192 struct nlattr *rta)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 struct xfrm_algo *p, *ualg;
195 struct xfrm_algo_desc *algo;
196
197 if (!rta)
198 return 0;
199
Thomas Graf5424f322007-08-22 14:01:33 -0700200 ualg = nla_data(rta);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201
202 algo = get_byname(ualg->alg_name, 1);
203 if (!algo)
204 return -ENOSYS;
205 *props = algo->desc.sadb_alg_id;
206
Thomas Grafc26445a2007-08-22 13:56:23 -0700207 p = kmemdup(ualg, alg_len(ualg), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 if (!p)
209 return -ENOMEM;
210
Herbert Xu04ff1262006-08-13 08:50:00 +1000211 strcpy(p->alg_name, algo->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 *algpp = p;
213 return 0;
214}
215
Joy Latten661697f2007-04-13 16:14:35 -0700216static inline int xfrm_user_sec_ctx_size(struct xfrm_sec_ctx *xfrm_ctx)
Trent Jaegerdf718372005-12-13 23:12:27 -0800217{
Trent Jaegerdf718372005-12-13 23:12:27 -0800218 int len = 0;
219
220 if (xfrm_ctx) {
221 len += sizeof(struct xfrm_user_sec_ctx);
222 len += xfrm_ctx->ctx_len;
223 }
224 return len;
225}
226
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227static void copy_from_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p)
228{
229 memcpy(&x->id, &p->id, sizeof(x->id));
230 memcpy(&x->sel, &p->sel, sizeof(x->sel));
231 memcpy(&x->lft, &p->lft, sizeof(x->lft));
232 x->props.mode = p->mode;
233 x->props.replay_window = p->replay_window;
234 x->props.reqid = p->reqid;
235 x->props.family = p->family;
David S. Miller54489c142006-10-27 15:29:47 -0700236 memcpy(&x->props.saddr, &p->saddr, sizeof(x->props.saddr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 x->props.flags = p->flags;
Herbert Xu196b0032007-07-31 02:04:32 -0700238
239 /*
240 * Set inner address family if the KM left it as zero.
241 * See comment in validate_tmpl.
242 */
243 if (!x->sel.family)
244 x->sel.family = p->family;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245}
246
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800247/*
248 * someday when pfkey also has support, we could have the code
249 * somehow made shareable and move it to xfrm_state.c - JHS
250 *
251*/
Thomas Graf5424f322007-08-22 14:01:33 -0700252static void xfrm_update_ae_params(struct xfrm_state *x, struct nlattr **attrs)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800253{
Thomas Graf5424f322007-08-22 14:01:33 -0700254 struct nlattr *rp = attrs[XFRMA_REPLAY_VAL];
255 struct nlattr *lt = attrs[XFRMA_LTIME_VAL];
256 struct nlattr *et = attrs[XFRMA_ETIMER_THRESH];
257 struct nlattr *rt = attrs[XFRMA_REPLAY_THRESH];
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800258
259 if (rp) {
260 struct xfrm_replay_state *replay;
Thomas Graf5424f322007-08-22 14:01:33 -0700261 replay = nla_data(rp);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800262 memcpy(&x->replay, replay, sizeof(*replay));
263 memcpy(&x->preplay, replay, sizeof(*replay));
264 }
265
266 if (lt) {
267 struct xfrm_lifetime_cur *ltime;
Thomas Graf5424f322007-08-22 14:01:33 -0700268 ltime = nla_data(lt);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800269 x->curlft.bytes = ltime->bytes;
270 x->curlft.packets = ltime->packets;
271 x->curlft.add_time = ltime->add_time;
272 x->curlft.use_time = ltime->use_time;
273 }
274
Thomas Grafcf5cb792007-08-22 13:59:04 -0700275 if (et)
Thomas Graf5424f322007-08-22 14:01:33 -0700276 x->replay_maxage = nla_get_u32(et);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800277
Thomas Grafcf5cb792007-08-22 13:59:04 -0700278 if (rt)
Thomas Graf5424f322007-08-22 14:01:33 -0700279 x->replay_maxdiff = nla_get_u32(rt);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800280}
281
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282static struct xfrm_state *xfrm_state_construct(struct xfrm_usersa_info *p,
Thomas Graf5424f322007-08-22 14:01:33 -0700283 struct nlattr **attrs,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 int *errp)
285{
286 struct xfrm_state *x = xfrm_state_alloc();
287 int err = -ENOMEM;
288
289 if (!x)
290 goto error_no_put;
291
292 copy_from_user_state(x, p);
293
294 if ((err = attach_one_algo(&x->aalg, &x->props.aalgo,
295 xfrm_aalg_get_byname,
Thomas Graf35a7aa02007-08-22 14:00:40 -0700296 attrs[XFRMA_ALG_AUTH])))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 goto error;
298 if ((err = attach_one_algo(&x->ealg, &x->props.ealgo,
299 xfrm_ealg_get_byname,
Thomas Graf35a7aa02007-08-22 14:00:40 -0700300 attrs[XFRMA_ALG_CRYPT])))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 goto error;
302 if ((err = attach_one_algo(&x->calg, &x->props.calgo,
303 xfrm_calg_get_byname,
Thomas Graf35a7aa02007-08-22 14:00:40 -0700304 attrs[XFRMA_ALG_COMP])))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 goto error;
Thomas Graffd211502007-09-06 03:28:08 -0700306
307 if (attrs[XFRMA_ENCAP]) {
308 x->encap = kmemdup(nla_data(attrs[XFRMA_ENCAP]),
309 sizeof(*x->encap), GFP_KERNEL);
310 if (x->encap == NULL)
311 goto error;
312 }
313
314 if (attrs[XFRMA_COADDR]) {
315 x->coaddr = kmemdup(nla_data(attrs[XFRMA_COADDR]),
316 sizeof(*x->coaddr), GFP_KERNEL);
317 if (x->coaddr == NULL)
318 goto error;
319 }
320
Herbert Xu72cb6962005-06-20 13:18:08 -0700321 err = xfrm_init_state(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 if (err)
323 goto error;
324
Thomas Graffd211502007-09-06 03:28:08 -0700325 if (attrs[XFRMA_SEC_CTX] &&
326 security_xfrm_state_alloc(x, nla_data(attrs[XFRMA_SEC_CTX])))
Trent Jaegerdf718372005-12-13 23:12:27 -0800327 goto error;
328
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 x->km.seq = p->seq;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800330 x->replay_maxdiff = sysctl_xfrm_aevent_rseqth;
331 /* sysctl_xfrm_aevent_etime is in 100ms units */
332 x->replay_maxage = (sysctl_xfrm_aevent_etime*HZ)/XFRM_AE_ETH_M;
333 x->preplay.bitmap = 0;
334 x->preplay.seq = x->replay.seq+x->replay_maxdiff;
335 x->preplay.oseq = x->replay.oseq +x->replay_maxdiff;
336
337 /* override default values from above */
338
Thomas Graf5424f322007-08-22 14:01:33 -0700339 xfrm_update_ae_params(x, attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340
341 return x;
342
343error:
344 x->km.state = XFRM_STATE_DEAD;
345 xfrm_state_put(x);
346error_no_put:
347 *errp = err;
348 return NULL;
349}
350
Christoph Hellwig22e70052007-01-02 15:22:30 -0800351static int xfrm_add_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -0700352 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353{
Thomas Graf7b67c852007-08-22 13:53:52 -0700354 struct xfrm_usersa_info *p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 struct xfrm_state *x;
356 int err;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700357 struct km_event c;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358
Thomas Graf35a7aa02007-08-22 14:00:40 -0700359 err = verify_newsa_info(p, attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 if (err)
361 return err;
362
Thomas Graf35a7aa02007-08-22 14:00:40 -0700363 x = xfrm_state_construct(p, attrs, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 if (!x)
365 return err;
366
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700367 xfrm_state_hold(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 if (nlh->nlmsg_type == XFRM_MSG_NEWSA)
369 err = xfrm_state_add(x);
370 else
371 err = xfrm_state_update(x);
372
Joy Lattenab5f5e82007-09-17 11:51:22 -0700373 xfrm_audit_state_add(x, err ? 0 : 1, NETLINK_CB(skb).loginuid,
374 NETLINK_CB(skb).sid);
Joy Latten161a09e2006-11-27 13:11:54 -0600375
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 if (err < 0) {
377 x->km.state = XFRM_STATE_DEAD;
Herbert Xu21380b82006-02-22 14:47:13 -0800378 __xfrm_state_put(x);
Patrick McHardy7d6dfe12005-06-18 22:45:31 -0700379 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 }
381
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700382 c.seq = nlh->nlmsg_seq;
383 c.pid = nlh->nlmsg_pid;
Herbert Xuf60f6b82005-06-18 22:44:37 -0700384 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700385
386 km_state_notify(x, &c);
Patrick McHardy7d6dfe12005-06-18 22:45:31 -0700387out:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700388 xfrm_state_put(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 return err;
390}
391
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700392static struct xfrm_state *xfrm_user_state_lookup(struct xfrm_usersa_id *p,
Thomas Graf5424f322007-08-22 14:01:33 -0700393 struct nlattr **attrs,
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700394 int *errp)
395{
396 struct xfrm_state *x = NULL;
397 int err;
398
399 if (xfrm_id_proto_match(p->proto, IPSEC_PROTO_ANY)) {
400 err = -ESRCH;
401 x = xfrm_state_lookup(&p->daddr, p->spi, p->proto, p->family);
402 } else {
403 xfrm_address_t *saddr = NULL;
404
Thomas Graf35a7aa02007-08-22 14:00:40 -0700405 verify_one_addr(attrs, XFRMA_SRCADDR, &saddr);
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700406 if (!saddr) {
407 err = -EINVAL;
408 goto out;
409 }
410
Masahide NAKAMURA9abbffe2006-11-24 20:34:51 -0800411 err = -ESRCH;
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700412 x = xfrm_state_lookup_byaddr(&p->daddr, saddr, p->proto,
413 p->family);
414 }
415
416 out:
417 if (!x && errp)
418 *errp = err;
419 return x;
420}
421
Christoph Hellwig22e70052007-01-02 15:22:30 -0800422static int xfrm_del_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -0700423 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424{
425 struct xfrm_state *x;
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700426 int err = -ESRCH;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700427 struct km_event c;
Thomas Graf7b67c852007-08-22 13:53:52 -0700428 struct xfrm_usersa_id *p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429
Thomas Graf35a7aa02007-08-22 14:00:40 -0700430 x = xfrm_user_state_lookup(p, attrs, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 if (x == NULL)
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700432 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433
David S. Miller6f68dc32006-06-08 23:58:52 -0700434 if ((err = security_xfrm_state_delete(x)) != 0)
Catherine Zhangc8c05a82006-06-08 23:39:49 -0700435 goto out;
436
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 if (xfrm_state_kern(x)) {
Catherine Zhangc8c05a82006-06-08 23:39:49 -0700438 err = -EPERM;
439 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 }
441
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700442 err = xfrm_state_delete(x);
Joy Latten161a09e2006-11-27 13:11:54 -0600443
Catherine Zhangc8c05a82006-06-08 23:39:49 -0700444 if (err < 0)
445 goto out;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700446
447 c.seq = nlh->nlmsg_seq;
448 c.pid = nlh->nlmsg_pid;
Herbert Xuf60f6b82005-06-18 22:44:37 -0700449 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700450 km_state_notify(x, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451
Catherine Zhangc8c05a82006-06-08 23:39:49 -0700452out:
Joy Lattenab5f5e82007-09-17 11:51:22 -0700453 xfrm_audit_state_delete(x, err ? 0 : 1, NETLINK_CB(skb).loginuid,
454 NETLINK_CB(skb).sid);
Catherine Zhangc8c05a82006-06-08 23:39:49 -0700455 xfrm_state_put(x);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700456 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457}
458
459static void copy_to_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p)
460{
461 memcpy(&p->id, &x->id, sizeof(p->id));
462 memcpy(&p->sel, &x->sel, sizeof(p->sel));
463 memcpy(&p->lft, &x->lft, sizeof(p->lft));
464 memcpy(&p->curlft, &x->curlft, sizeof(p->curlft));
465 memcpy(&p->stats, &x->stats, sizeof(p->stats));
David S. Miller54489c142006-10-27 15:29:47 -0700466 memcpy(&p->saddr, &x->props.saddr, sizeof(p->saddr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 p->mode = x->props.mode;
468 p->replay_window = x->props.replay_window;
469 p->reqid = x->props.reqid;
470 p->family = x->props.family;
471 p->flags = x->props.flags;
472 p->seq = x->km.seq;
473}
474
475struct xfrm_dump_info {
476 struct sk_buff *in_skb;
477 struct sk_buff *out_skb;
478 u32 nlmsg_seq;
479 u16 nlmsg_flags;
480 int start_idx;
481 int this_idx;
482};
483
Thomas Grafc0144be2007-08-22 13:55:43 -0700484static int copy_sec_ctx(struct xfrm_sec_ctx *s, struct sk_buff *skb)
485{
486 int ctx_size = sizeof(struct xfrm_sec_ctx) + s->ctx_len;
487 struct xfrm_user_sec_ctx *uctx;
488 struct nlattr *attr;
489
490 attr = nla_reserve(skb, XFRMA_SEC_CTX, ctx_size);
491 if (attr == NULL)
492 return -EMSGSIZE;
493
494 uctx = nla_data(attr);
495 uctx->exttype = XFRMA_SEC_CTX;
496 uctx->len = ctx_size;
497 uctx->ctx_doi = s->ctx_doi;
498 uctx->ctx_alg = s->ctx_alg;
499 uctx->ctx_len = s->ctx_len;
500 memcpy(uctx + 1, s->ctx_str, s->ctx_len);
501
502 return 0;
503}
504
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505static int dump_one_state(struct xfrm_state *x, int count, void *ptr)
506{
507 struct xfrm_dump_info *sp = ptr;
508 struct sk_buff *in_skb = sp->in_skb;
509 struct sk_buff *skb = sp->out_skb;
510 struct xfrm_usersa_info *p;
511 struct nlmsghdr *nlh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512
513 if (sp->this_idx < sp->start_idx)
514 goto out;
515
Thomas Graf79b8b7f2007-08-22 12:46:53 -0700516 nlh = nlmsg_put(skb, NETLINK_CB(in_skb).pid, sp->nlmsg_seq,
517 XFRM_MSG_NEWSA, sizeof(*p), sp->nlmsg_flags);
518 if (nlh == NULL)
519 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520
Thomas Graf7b67c852007-08-22 13:53:52 -0700521 p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 copy_to_user_state(x, p);
523
524 if (x->aalg)
Thomas Grafc26445a2007-08-22 13:56:23 -0700525 NLA_PUT(skb, XFRMA_ALG_AUTH, alg_len(x->aalg), x->aalg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 if (x->ealg)
Thomas Grafc26445a2007-08-22 13:56:23 -0700527 NLA_PUT(skb, XFRMA_ALG_CRYPT, alg_len(x->ealg), x->ealg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 if (x->calg)
Thomas Grafc0144be2007-08-22 13:55:43 -0700529 NLA_PUT(skb, XFRMA_ALG_COMP, sizeof(*(x->calg)), x->calg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530
531 if (x->encap)
Thomas Grafc0144be2007-08-22 13:55:43 -0700532 NLA_PUT(skb, XFRMA_ENCAP, sizeof(*x->encap), x->encap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533
Thomas Grafc0144be2007-08-22 13:55:43 -0700534 if (x->security && copy_sec_ctx(x->security, skb) < 0)
535 goto nla_put_failure;
Noriaki TAKAMIYA060f02a2006-08-23 18:18:55 -0700536
537 if (x->coaddr)
Thomas Grafc0144be2007-08-22 13:55:43 -0700538 NLA_PUT(skb, XFRMA_COADDR, sizeof(*x->coaddr), x->coaddr);
Noriaki TAKAMIYA060f02a2006-08-23 18:18:55 -0700539
Masahide NAKAMURA9afaca02006-08-23 18:20:16 -0700540 if (x->lastused)
Thomas Grafc0144be2007-08-22 13:55:43 -0700541 NLA_PUT_U64(skb, XFRMA_LASTUSED, x->lastused);
Masahide NAKAMURA9afaca02006-08-23 18:20:16 -0700542
Thomas Graf98250692007-08-22 12:47:26 -0700543 nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544out:
545 sp->this_idx++;
546 return 0;
547
Thomas Grafc0144be2007-08-22 13:55:43 -0700548nla_put_failure:
Thomas Graf98250692007-08-22 12:47:26 -0700549 nlmsg_cancel(skb, nlh);
550 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551}
552
553static int xfrm_dump_sa(struct sk_buff *skb, struct netlink_callback *cb)
554{
555 struct xfrm_dump_info info;
556
557 info.in_skb = cb->skb;
558 info.out_skb = skb;
559 info.nlmsg_seq = cb->nlh->nlmsg_seq;
560 info.nlmsg_flags = NLM_F_MULTI;
561 info.this_idx = 0;
562 info.start_idx = cb->args[0];
Masahide NAKAMURAdc00a522006-08-23 17:49:52 -0700563 (void) xfrm_state_walk(0, dump_one_state, &info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 cb->args[0] = info.this_idx;
565
566 return skb->len;
567}
568
569static struct sk_buff *xfrm_state_netlink(struct sk_buff *in_skb,
570 struct xfrm_state *x, u32 seq)
571{
572 struct xfrm_dump_info info;
573 struct sk_buff *skb;
574
Thomas Graf7deb2262007-08-22 13:57:39 -0700575 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 if (!skb)
577 return ERR_PTR(-ENOMEM);
578
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 info.in_skb = in_skb;
580 info.out_skb = skb;
581 info.nlmsg_seq = seq;
582 info.nlmsg_flags = 0;
583 info.this_idx = info.start_idx = 0;
584
585 if (dump_one_state(x, 0, &info)) {
586 kfree_skb(skb);
587 return NULL;
588 }
589
590 return skb;
591}
592
Thomas Graf7deb2262007-08-22 13:57:39 -0700593static inline size_t xfrm_spdinfo_msgsize(void)
594{
595 return NLMSG_ALIGN(4)
596 + nla_total_size(sizeof(struct xfrmu_spdinfo))
597 + nla_total_size(sizeof(struct xfrmu_spdhinfo));
598}
599
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700600static int build_spdinfo(struct sk_buff *skb, u32 pid, u32 seq, u32 flags)
601{
Jamal Hadi Salim5a6d3412007-05-04 12:55:39 -0700602 struct xfrmk_spdinfo si;
603 struct xfrmu_spdinfo spc;
604 struct xfrmu_spdhinfo sph;
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700605 struct nlmsghdr *nlh;
606 u32 *f;
607
608 nlh = nlmsg_put(skb, pid, seq, XFRM_MSG_NEWSPDINFO, sizeof(u32), 0);
609 if (nlh == NULL) /* shouldnt really happen ... */
610 return -EMSGSIZE;
611
612 f = nlmsg_data(nlh);
613 *f = flags;
614 xfrm_spd_getinfo(&si);
Jamal Hadi Salim5a6d3412007-05-04 12:55:39 -0700615 spc.incnt = si.incnt;
616 spc.outcnt = si.outcnt;
617 spc.fwdcnt = si.fwdcnt;
618 spc.inscnt = si.inscnt;
619 spc.outscnt = si.outscnt;
620 spc.fwdscnt = si.fwdscnt;
621 sph.spdhcnt = si.spdhcnt;
622 sph.spdhmcnt = si.spdhmcnt;
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700623
Jamal Hadi Salim5a6d3412007-05-04 12:55:39 -0700624 NLA_PUT(skb, XFRMA_SPD_INFO, sizeof(spc), &spc);
625 NLA_PUT(skb, XFRMA_SPD_HINFO, sizeof(sph), &sph);
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700626
627 return nlmsg_end(skb, nlh);
628
629nla_put_failure:
630 nlmsg_cancel(skb, nlh);
631 return -EMSGSIZE;
632}
633
634static int xfrm_get_spdinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -0700635 struct nlattr **attrs)
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700636{
637 struct sk_buff *r_skb;
Thomas Graf7b67c852007-08-22 13:53:52 -0700638 u32 *flags = nlmsg_data(nlh);
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700639 u32 spid = NETLINK_CB(skb).pid;
640 u32 seq = nlh->nlmsg_seq;
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700641
Thomas Graf7deb2262007-08-22 13:57:39 -0700642 r_skb = nlmsg_new(xfrm_spdinfo_msgsize(), GFP_ATOMIC);
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700643 if (r_skb == NULL)
644 return -ENOMEM;
645
646 if (build_spdinfo(r_skb, spid, seq, *flags) < 0)
647 BUG();
648
649 return nlmsg_unicast(xfrm_nl, r_skb, spid);
650}
651
Thomas Graf7deb2262007-08-22 13:57:39 -0700652static inline size_t xfrm_sadinfo_msgsize(void)
653{
654 return NLMSG_ALIGN(4)
655 + nla_total_size(sizeof(struct xfrmu_sadhinfo))
656 + nla_total_size(4); /* XFRMA_SAD_CNT */
657}
658
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700659static int build_sadinfo(struct sk_buff *skb, u32 pid, u32 seq, u32 flags)
660{
Jamal Hadi Salimaf11e312007-05-04 12:55:13 -0700661 struct xfrmk_sadinfo si;
662 struct xfrmu_sadhinfo sh;
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700663 struct nlmsghdr *nlh;
664 u32 *f;
665
666 nlh = nlmsg_put(skb, pid, seq, XFRM_MSG_NEWSADINFO, sizeof(u32), 0);
667 if (nlh == NULL) /* shouldnt really happen ... */
668 return -EMSGSIZE;
669
670 f = nlmsg_data(nlh);
671 *f = flags;
672 xfrm_sad_getinfo(&si);
673
Jamal Hadi Salimaf11e312007-05-04 12:55:13 -0700674 sh.sadhmcnt = si.sadhmcnt;
675 sh.sadhcnt = si.sadhcnt;
676
677 NLA_PUT_U32(skb, XFRMA_SAD_CNT, si.sadcnt);
678 NLA_PUT(skb, XFRMA_SAD_HINFO, sizeof(sh), &sh);
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700679
680 return nlmsg_end(skb, nlh);
681
682nla_put_failure:
683 nlmsg_cancel(skb, nlh);
684 return -EMSGSIZE;
685}
686
687static int xfrm_get_sadinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -0700688 struct nlattr **attrs)
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700689{
690 struct sk_buff *r_skb;
Thomas Graf7b67c852007-08-22 13:53:52 -0700691 u32 *flags = nlmsg_data(nlh);
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700692 u32 spid = NETLINK_CB(skb).pid;
693 u32 seq = nlh->nlmsg_seq;
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700694
Thomas Graf7deb2262007-08-22 13:57:39 -0700695 r_skb = nlmsg_new(xfrm_sadinfo_msgsize(), GFP_ATOMIC);
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700696 if (r_skb == NULL)
697 return -ENOMEM;
698
699 if (build_sadinfo(r_skb, spid, seq, *flags) < 0)
700 BUG();
701
702 return nlmsg_unicast(xfrm_nl, r_skb, spid);
703}
704
Christoph Hellwig22e70052007-01-02 15:22:30 -0800705static int xfrm_get_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -0700706 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707{
Thomas Graf7b67c852007-08-22 13:53:52 -0700708 struct xfrm_usersa_id *p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 struct xfrm_state *x;
710 struct sk_buff *resp_skb;
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700711 int err = -ESRCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712
Thomas Graf35a7aa02007-08-22 14:00:40 -0700713 x = xfrm_user_state_lookup(p, attrs, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 if (x == NULL)
715 goto out_noput;
716
717 resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
718 if (IS_ERR(resp_skb)) {
719 err = PTR_ERR(resp_skb);
720 } else {
Thomas Graf082a1ad2007-08-22 13:54:36 -0700721 err = nlmsg_unicast(xfrm_nl, resp_skb, NETLINK_CB(skb).pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 }
723 xfrm_state_put(x);
724out_noput:
725 return err;
726}
727
728static int verify_userspi_info(struct xfrm_userspi_info *p)
729{
730 switch (p->info.id.proto) {
731 case IPPROTO_AH:
732 case IPPROTO_ESP:
733 break;
734
735 case IPPROTO_COMP:
736 /* IPCOMP spi is 16-bits. */
737 if (p->max >= 0x10000)
738 return -EINVAL;
739 break;
740
741 default:
742 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700743 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744
745 if (p->min > p->max)
746 return -EINVAL;
747
748 return 0;
749}
750
Christoph Hellwig22e70052007-01-02 15:22:30 -0800751static int xfrm_alloc_userspi(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -0700752 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753{
754 struct xfrm_state *x;
755 struct xfrm_userspi_info *p;
756 struct sk_buff *resp_skb;
757 xfrm_address_t *daddr;
758 int family;
759 int err;
760
Thomas Graf7b67c852007-08-22 13:53:52 -0700761 p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 err = verify_userspi_info(p);
763 if (err)
764 goto out_noput;
765
766 family = p->info.family;
767 daddr = &p->info.id.daddr;
768
769 x = NULL;
770 if (p->info.seq) {
771 x = xfrm_find_acq_byseq(p->info.seq);
772 if (x && xfrm_addr_cmp(&x->id.daddr, daddr, family)) {
773 xfrm_state_put(x);
774 x = NULL;
775 }
776 }
777
778 if (!x)
779 x = xfrm_find_acq(p->info.mode, p->info.reqid,
780 p->info.id.proto, daddr,
781 &p->info.saddr, 1,
782 family);
783 err = -ENOENT;
784 if (x == NULL)
785 goto out_noput;
786
Herbert Xu658b2192007-10-09 13:29:52 -0700787 err = xfrm_alloc_spi(x, p->min, p->max);
788 if (err)
789 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790
Herbert Xu658b2192007-10-09 13:29:52 -0700791 resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 if (IS_ERR(resp_skb)) {
793 err = PTR_ERR(resp_skb);
794 goto out;
795 }
796
Thomas Graf082a1ad2007-08-22 13:54:36 -0700797 err = nlmsg_unicast(xfrm_nl, resp_skb, NETLINK_CB(skb).pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798
799out:
800 xfrm_state_put(x);
801out_noput:
802 return err;
803}
804
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -0800805static int verify_policy_dir(u8 dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806{
807 switch (dir) {
808 case XFRM_POLICY_IN:
809 case XFRM_POLICY_OUT:
810 case XFRM_POLICY_FWD:
811 break;
812
813 default:
814 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700815 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816
817 return 0;
818}
819
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -0800820static int verify_policy_type(u8 type)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -0700821{
822 switch (type) {
823 case XFRM_POLICY_TYPE_MAIN:
824#ifdef CONFIG_XFRM_SUB_POLICY
825 case XFRM_POLICY_TYPE_SUB:
826#endif
827 break;
828
829 default:
830 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700831 }
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -0700832
833 return 0;
834}
835
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836static int verify_newpolicy_info(struct xfrm_userpolicy_info *p)
837{
838 switch (p->share) {
839 case XFRM_SHARE_ANY:
840 case XFRM_SHARE_SESSION:
841 case XFRM_SHARE_USER:
842 case XFRM_SHARE_UNIQUE:
843 break;
844
845 default:
846 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700847 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848
849 switch (p->action) {
850 case XFRM_POLICY_ALLOW:
851 case XFRM_POLICY_BLOCK:
852 break;
853
854 default:
855 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700856 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857
858 switch (p->sel.family) {
859 case AF_INET:
860 break;
861
862 case AF_INET6:
863#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
864 break;
865#else
866 return -EAFNOSUPPORT;
867#endif
868
869 default:
870 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700871 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872
873 return verify_policy_dir(p->dir);
874}
875
Thomas Graf5424f322007-08-22 14:01:33 -0700876static int copy_from_user_sec_ctx(struct xfrm_policy *pol, struct nlattr **attrs)
Trent Jaegerdf718372005-12-13 23:12:27 -0800877{
Thomas Graf5424f322007-08-22 14:01:33 -0700878 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
Trent Jaegerdf718372005-12-13 23:12:27 -0800879 struct xfrm_user_sec_ctx *uctx;
880
881 if (!rt)
882 return 0;
883
Thomas Graf5424f322007-08-22 14:01:33 -0700884 uctx = nla_data(rt);
Trent Jaegerdf718372005-12-13 23:12:27 -0800885 return security_xfrm_policy_alloc(pol, uctx);
886}
887
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888static void copy_templates(struct xfrm_policy *xp, struct xfrm_user_tmpl *ut,
889 int nr)
890{
891 int i;
892
893 xp->xfrm_nr = nr;
894 for (i = 0; i < nr; i++, ut++) {
895 struct xfrm_tmpl *t = &xp->xfrm_vec[i];
896
897 memcpy(&t->id, &ut->id, sizeof(struct xfrm_id));
898 memcpy(&t->saddr, &ut->saddr,
899 sizeof(xfrm_address_t));
900 t->reqid = ut->reqid;
901 t->mode = ut->mode;
902 t->share = ut->share;
903 t->optional = ut->optional;
904 t->aalgos = ut->aalgos;
905 t->ealgos = ut->ealgos;
906 t->calgos = ut->calgos;
Miika Komu8511d012006-11-30 16:40:51 -0800907 t->encap_family = ut->family;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 }
909}
910
David S. Millerb4ad86bf2006-12-03 19:19:26 -0800911static int validate_tmpl(int nr, struct xfrm_user_tmpl *ut, u16 family)
912{
913 int i;
914
915 if (nr > XFRM_MAX_DEPTH)
916 return -EINVAL;
917
918 for (i = 0; i < nr; i++) {
919 /* We never validated the ut->family value, so many
920 * applications simply leave it at zero. The check was
921 * never made and ut->family was ignored because all
922 * templates could be assumed to have the same family as
923 * the policy itself. Now that we will have ipv4-in-ipv6
924 * and ipv6-in-ipv4 tunnels, this is no longer true.
925 */
926 if (!ut[i].family)
927 ut[i].family = family;
928
929 switch (ut[i].family) {
930 case AF_INET:
931 break;
932#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
933 case AF_INET6:
934 break;
935#endif
936 default:
937 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700938 }
David S. Millerb4ad86bf2006-12-03 19:19:26 -0800939 }
940
941 return 0;
942}
943
Thomas Graf5424f322007-08-22 14:01:33 -0700944static int copy_from_user_tmpl(struct xfrm_policy *pol, struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945{
Thomas Graf5424f322007-08-22 14:01:33 -0700946 struct nlattr *rt = attrs[XFRMA_TMPL];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947
948 if (!rt) {
949 pol->xfrm_nr = 0;
950 } else {
Thomas Graf5424f322007-08-22 14:01:33 -0700951 struct xfrm_user_tmpl *utmpl = nla_data(rt);
952 int nr = nla_len(rt) / sizeof(*utmpl);
David S. Millerb4ad86bf2006-12-03 19:19:26 -0800953 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954
David S. Millerb4ad86bf2006-12-03 19:19:26 -0800955 err = validate_tmpl(nr, utmpl, pol->family);
956 if (err)
957 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958
Thomas Graf5424f322007-08-22 14:01:33 -0700959 copy_templates(pol, utmpl, nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 }
961 return 0;
962}
963
Thomas Graf5424f322007-08-22 14:01:33 -0700964static int copy_from_user_policy_type(u8 *tp, struct nlattr **attrs)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -0700965{
Thomas Graf5424f322007-08-22 14:01:33 -0700966 struct nlattr *rt = attrs[XFRMA_POLICY_TYPE];
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -0700967 struct xfrm_userpolicy_type *upt;
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -0800968 u8 type = XFRM_POLICY_TYPE_MAIN;
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -0700969 int err;
970
971 if (rt) {
Thomas Graf5424f322007-08-22 14:01:33 -0700972 upt = nla_data(rt);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -0700973 type = upt->type;
974 }
975
976 err = verify_policy_type(type);
977 if (err)
978 return err;
979
980 *tp = type;
981 return 0;
982}
983
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984static void copy_from_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p)
985{
986 xp->priority = p->priority;
987 xp->index = p->index;
988 memcpy(&xp->selector, &p->sel, sizeof(xp->selector));
989 memcpy(&xp->lft, &p->lft, sizeof(xp->lft));
990 xp->action = p->action;
991 xp->flags = p->flags;
992 xp->family = p->sel.family;
993 /* XXX xp->share = p->share; */
994}
995
996static void copy_to_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p, int dir)
997{
998 memcpy(&p->sel, &xp->selector, sizeof(p->sel));
999 memcpy(&p->lft, &xp->lft, sizeof(p->lft));
1000 memcpy(&p->curlft, &xp->curlft, sizeof(p->curlft));
1001 p->priority = xp->priority;
1002 p->index = xp->index;
1003 p->sel.family = xp->family;
1004 p->dir = dir;
1005 p->action = xp->action;
1006 p->flags = xp->flags;
1007 p->share = XFRM_SHARE_ANY; /* XXX xp->share */
1008}
1009
Thomas Graf5424f322007-08-22 14:01:33 -07001010static struct xfrm_policy *xfrm_policy_construct(struct xfrm_userpolicy_info *p, struct nlattr **attrs, int *errp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011{
1012 struct xfrm_policy *xp = xfrm_policy_alloc(GFP_KERNEL);
1013 int err;
1014
1015 if (!xp) {
1016 *errp = -ENOMEM;
1017 return NULL;
1018 }
1019
1020 copy_from_user_policy(xp, p);
Trent Jaegerdf718372005-12-13 23:12:27 -08001021
Thomas Graf35a7aa02007-08-22 14:00:40 -07001022 err = copy_from_user_policy_type(&xp->type, attrs);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001023 if (err)
1024 goto error;
1025
Thomas Graf35a7aa02007-08-22 14:00:40 -07001026 if (!(err = copy_from_user_tmpl(xp, attrs)))
1027 err = copy_from_user_sec_ctx(xp, attrs);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001028 if (err)
1029 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030
1031 return xp;
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001032 error:
1033 *errp = err;
1034 kfree(xp);
1035 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036}
1037
Christoph Hellwig22e70052007-01-02 15:22:30 -08001038static int xfrm_add_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001039 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040{
Thomas Graf7b67c852007-08-22 13:53:52 -07001041 struct xfrm_userpolicy_info *p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 struct xfrm_policy *xp;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001043 struct km_event c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 int err;
1045 int excl;
1046
1047 err = verify_newpolicy_info(p);
1048 if (err)
1049 return err;
Thomas Graf35a7aa02007-08-22 14:00:40 -07001050 err = verify_sec_ctx_len(attrs);
Trent Jaegerdf718372005-12-13 23:12:27 -08001051 if (err)
1052 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053
Thomas Graf35a7aa02007-08-22 14:00:40 -07001054 xp = xfrm_policy_construct(p, attrs, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 if (!xp)
1056 return err;
1057
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001058 /* shouldnt excl be based on nlh flags??
1059 * Aha! this is anti-netlink really i.e more pfkey derived
1060 * in netlink excl is a flag and you wouldnt need
1061 * a type XFRM_MSG_UPDPOLICY - JHS */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 excl = nlh->nlmsg_type == XFRM_MSG_NEWPOLICY;
1063 err = xfrm_policy_insert(p->dir, xp, excl);
Joy Lattenab5f5e82007-09-17 11:51:22 -07001064 xfrm_audit_policy_add(xp, err ? 0 : 1, NETLINK_CB(skb).loginuid,
1065 NETLINK_CB(skb).sid);
Joy Latten161a09e2006-11-27 13:11:54 -06001066
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 if (err) {
Trent Jaeger5f8ac642006-01-06 13:22:39 -08001068 security_xfrm_policy_free(xp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 kfree(xp);
1070 return err;
1071 }
1072
Herbert Xuf60f6b82005-06-18 22:44:37 -07001073 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001074 c.seq = nlh->nlmsg_seq;
1075 c.pid = nlh->nlmsg_pid;
1076 km_policy_notify(xp, p->dir, &c);
1077
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 xfrm_pol_put(xp);
1079
1080 return 0;
1081}
1082
1083static int copy_to_user_tmpl(struct xfrm_policy *xp, struct sk_buff *skb)
1084{
1085 struct xfrm_user_tmpl vec[XFRM_MAX_DEPTH];
1086 int i;
1087
1088 if (xp->xfrm_nr == 0)
1089 return 0;
1090
1091 for (i = 0; i < xp->xfrm_nr; i++) {
1092 struct xfrm_user_tmpl *up = &vec[i];
1093 struct xfrm_tmpl *kp = &xp->xfrm_vec[i];
1094
1095 memcpy(&up->id, &kp->id, sizeof(up->id));
Miika Komu8511d012006-11-30 16:40:51 -08001096 up->family = kp->encap_family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 memcpy(&up->saddr, &kp->saddr, sizeof(up->saddr));
1098 up->reqid = kp->reqid;
1099 up->mode = kp->mode;
1100 up->share = kp->share;
1101 up->optional = kp->optional;
1102 up->aalgos = kp->aalgos;
1103 up->ealgos = kp->ealgos;
1104 up->calgos = kp->calgos;
1105 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106
Thomas Grafc0144be2007-08-22 13:55:43 -07001107 return nla_put(skb, XFRMA_TMPL,
1108 sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr, vec);
Trent Jaegerdf718372005-12-13 23:12:27 -08001109}
1110
Serge Hallyn0d681622006-07-24 23:30:44 -07001111static inline int copy_to_user_state_sec_ctx(struct xfrm_state *x, struct sk_buff *skb)
1112{
1113 if (x->security) {
1114 return copy_sec_ctx(x->security, skb);
1115 }
1116 return 0;
1117}
1118
1119static inline int copy_to_user_sec_ctx(struct xfrm_policy *xp, struct sk_buff *skb)
1120{
1121 if (xp->security) {
1122 return copy_sec_ctx(xp->security, skb);
1123 }
1124 return 0;
1125}
Thomas Grafcfbfd452007-08-22 13:57:04 -07001126static inline size_t userpolicy_type_attrsize(void)
1127{
1128#ifdef CONFIG_XFRM_SUB_POLICY
1129 return nla_total_size(sizeof(struct xfrm_userpolicy_type));
1130#else
1131 return 0;
1132#endif
1133}
Serge Hallyn0d681622006-07-24 23:30:44 -07001134
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001135#ifdef CONFIG_XFRM_SUB_POLICY
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001136static int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001137{
Thomas Grafc0144be2007-08-22 13:55:43 -07001138 struct xfrm_userpolicy_type upt = {
1139 .type = type,
1140 };
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001141
Thomas Grafc0144be2007-08-22 13:55:43 -07001142 return nla_put(skb, XFRMA_POLICY_TYPE, sizeof(upt), &upt);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001143}
1144
1145#else
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001146static inline int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001147{
1148 return 0;
1149}
1150#endif
1151
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152static int dump_one_policy(struct xfrm_policy *xp, int dir, int count, void *ptr)
1153{
1154 struct xfrm_dump_info *sp = ptr;
1155 struct xfrm_userpolicy_info *p;
1156 struct sk_buff *in_skb = sp->in_skb;
1157 struct sk_buff *skb = sp->out_skb;
1158 struct nlmsghdr *nlh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159
1160 if (sp->this_idx < sp->start_idx)
1161 goto out;
1162
Thomas Graf79b8b7f2007-08-22 12:46:53 -07001163 nlh = nlmsg_put(skb, NETLINK_CB(in_skb).pid, sp->nlmsg_seq,
1164 XFRM_MSG_NEWPOLICY, sizeof(*p), sp->nlmsg_flags);
1165 if (nlh == NULL)
1166 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167
Thomas Graf7b67c852007-08-22 13:53:52 -07001168 p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 copy_to_user_policy(xp, p, dir);
1170 if (copy_to_user_tmpl(xp, skb) < 0)
1171 goto nlmsg_failure;
Trent Jaegerdf718372005-12-13 23:12:27 -08001172 if (copy_to_user_sec_ctx(xp, skb))
1173 goto nlmsg_failure;
Jamal Hadi Salim1459bb32006-11-20 16:51:22 -08001174 if (copy_to_user_policy_type(xp->type, skb) < 0)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001175 goto nlmsg_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176
Thomas Graf98250692007-08-22 12:47:26 -07001177 nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178out:
1179 sp->this_idx++;
1180 return 0;
1181
1182nlmsg_failure:
Thomas Graf98250692007-08-22 12:47:26 -07001183 nlmsg_cancel(skb, nlh);
1184 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185}
1186
1187static int xfrm_dump_policy(struct sk_buff *skb, struct netlink_callback *cb)
1188{
1189 struct xfrm_dump_info info;
1190
1191 info.in_skb = cb->skb;
1192 info.out_skb = skb;
1193 info.nlmsg_seq = cb->nlh->nlmsg_seq;
1194 info.nlmsg_flags = NLM_F_MULTI;
1195 info.this_idx = 0;
1196 info.start_idx = cb->args[0];
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001197 (void) xfrm_policy_walk(XFRM_POLICY_TYPE_MAIN, dump_one_policy, &info);
1198#ifdef CONFIG_XFRM_SUB_POLICY
1199 (void) xfrm_policy_walk(XFRM_POLICY_TYPE_SUB, dump_one_policy, &info);
1200#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201 cb->args[0] = info.this_idx;
1202
1203 return skb->len;
1204}
1205
1206static struct sk_buff *xfrm_policy_netlink(struct sk_buff *in_skb,
1207 struct xfrm_policy *xp,
1208 int dir, u32 seq)
1209{
1210 struct xfrm_dump_info info;
1211 struct sk_buff *skb;
1212
Thomas Graf7deb2262007-08-22 13:57:39 -07001213 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 if (!skb)
1215 return ERR_PTR(-ENOMEM);
1216
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217 info.in_skb = in_skb;
1218 info.out_skb = skb;
1219 info.nlmsg_seq = seq;
1220 info.nlmsg_flags = 0;
1221 info.this_idx = info.start_idx = 0;
1222
1223 if (dump_one_policy(xp, dir, 0, &info) < 0) {
1224 kfree_skb(skb);
1225 return NULL;
1226 }
1227
1228 return skb;
1229}
1230
Christoph Hellwig22e70052007-01-02 15:22:30 -08001231static int xfrm_get_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001232 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233{
1234 struct xfrm_policy *xp;
1235 struct xfrm_userpolicy_id *p;
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001236 u8 type = XFRM_POLICY_TYPE_MAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237 int err;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001238 struct km_event c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 int delete;
1240
Thomas Graf7b67c852007-08-22 13:53:52 -07001241 p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 delete = nlh->nlmsg_type == XFRM_MSG_DELPOLICY;
1243
Thomas Graf35a7aa02007-08-22 14:00:40 -07001244 err = copy_from_user_policy_type(&type, attrs);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001245 if (err)
1246 return err;
1247
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248 err = verify_policy_dir(p->dir);
1249 if (err)
1250 return err;
1251
1252 if (p->index)
Eric Parisef41aaa2007-03-07 15:37:58 -08001253 xp = xfrm_policy_byid(type, p->dir, p->index, delete, &err);
Trent Jaegerdf718372005-12-13 23:12:27 -08001254 else {
Thomas Graf5424f322007-08-22 14:01:33 -07001255 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
Trent Jaegerdf718372005-12-13 23:12:27 -08001256 struct xfrm_policy tmp;
1257
Thomas Graf35a7aa02007-08-22 14:00:40 -07001258 err = verify_sec_ctx_len(attrs);
Trent Jaegerdf718372005-12-13 23:12:27 -08001259 if (err)
1260 return err;
1261
1262 memset(&tmp, 0, sizeof(struct xfrm_policy));
1263 if (rt) {
Thomas Graf5424f322007-08-22 14:01:33 -07001264 struct xfrm_user_sec_ctx *uctx = nla_data(rt);
Trent Jaegerdf718372005-12-13 23:12:27 -08001265
1266 if ((err = security_xfrm_policy_alloc(&tmp, uctx)))
1267 return err;
1268 }
Eric Parisef41aaa2007-03-07 15:37:58 -08001269 xp = xfrm_policy_bysel_ctx(type, p->dir, &p->sel, tmp.security,
1270 delete, &err);
Trent Jaegerdf718372005-12-13 23:12:27 -08001271 security_xfrm_policy_free(&tmp);
1272 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 if (xp == NULL)
1274 return -ENOENT;
1275
1276 if (!delete) {
1277 struct sk_buff *resp_skb;
1278
1279 resp_skb = xfrm_policy_netlink(skb, xp, p->dir, nlh->nlmsg_seq);
1280 if (IS_ERR(resp_skb)) {
1281 err = PTR_ERR(resp_skb);
1282 } else {
Thomas Graf082a1ad2007-08-22 13:54:36 -07001283 err = nlmsg_unicast(xfrm_nl, resp_skb,
1284 NETLINK_CB(skb).pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 }
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001286 } else {
Joy Lattenab5f5e82007-09-17 11:51:22 -07001287 xfrm_audit_policy_delete(xp, err ? 0 : 1,
1288 NETLINK_CB(skb).loginuid,
1289 NETLINK_CB(skb).sid);
David S. Miller13fcfbb02007-02-12 13:53:54 -08001290
1291 if (err != 0)
Catherine Zhangc8c05a82006-06-08 23:39:49 -07001292 goto out;
David S. Miller13fcfbb02007-02-12 13:53:54 -08001293
Herbert Xue7443892005-06-18 22:44:18 -07001294 c.data.byid = p->index;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001295 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001296 c.seq = nlh->nlmsg_seq;
1297 c.pid = nlh->nlmsg_pid;
1298 km_policy_notify(xp, p->dir, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299 }
1300
Catherine Zhangc8c05a82006-06-08 23:39:49 -07001301out:
Eric Parisef41aaa2007-03-07 15:37:58 -08001302 xfrm_pol_put(xp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303 return err;
1304}
1305
Christoph Hellwig22e70052007-01-02 15:22:30 -08001306static int xfrm_flush_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001307 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308{
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001309 struct km_event c;
Thomas Graf7b67c852007-08-22 13:53:52 -07001310 struct xfrm_usersa_flush *p = nlmsg_data(nlh);
Joy Latten161a09e2006-11-27 13:11:54 -06001311 struct xfrm_audit audit_info;
Joy Latten4aa2e622007-06-04 19:05:57 -04001312 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313
Joy Latten161a09e2006-11-27 13:11:54 -06001314 audit_info.loginuid = NETLINK_CB(skb).loginuid;
1315 audit_info.secid = NETLINK_CB(skb).sid;
Joy Latten4aa2e622007-06-04 19:05:57 -04001316 err = xfrm_state_flush(p->proto, &audit_info);
1317 if (err)
1318 return err;
Herbert Xubf088672005-06-18 22:44:00 -07001319 c.data.proto = p->proto;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001320 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001321 c.seq = nlh->nlmsg_seq;
1322 c.pid = nlh->nlmsg_pid;
1323 km_state_notify(NULL, &c);
1324
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325 return 0;
1326}
1327
Thomas Graf7deb2262007-08-22 13:57:39 -07001328static inline size_t xfrm_aevent_msgsize(void)
1329{
1330 return NLMSG_ALIGN(sizeof(struct xfrm_aevent_id))
1331 + nla_total_size(sizeof(struct xfrm_replay_state))
1332 + nla_total_size(sizeof(struct xfrm_lifetime_cur))
1333 + nla_total_size(4) /* XFRM_AE_RTHR */
1334 + nla_total_size(4); /* XFRM_AE_ETHR */
1335}
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001336
1337static int build_aevent(struct sk_buff *skb, struct xfrm_state *x, struct km_event *c)
1338{
1339 struct xfrm_aevent_id *id;
1340 struct nlmsghdr *nlh;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001341
Thomas Graf79b8b7f2007-08-22 12:46:53 -07001342 nlh = nlmsg_put(skb, c->pid, c->seq, XFRM_MSG_NEWAE, sizeof(*id), 0);
1343 if (nlh == NULL)
1344 return -EMSGSIZE;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001345
Thomas Graf7b67c852007-08-22 13:53:52 -07001346 id = nlmsg_data(nlh);
Jamal Hadi Salim2b5f6dc2006-12-02 22:22:25 -08001347 memcpy(&id->sa_id.daddr, &x->id.daddr,sizeof(x->id.daddr));
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001348 id->sa_id.spi = x->id.spi;
1349 id->sa_id.family = x->props.family;
1350 id->sa_id.proto = x->id.proto;
Jamal Hadi Salim2b5f6dc2006-12-02 22:22:25 -08001351 memcpy(&id->saddr, &x->props.saddr,sizeof(x->props.saddr));
1352 id->reqid = x->props.reqid;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001353 id->flags = c->data.aevent;
1354
Thomas Grafc0144be2007-08-22 13:55:43 -07001355 NLA_PUT(skb, XFRMA_REPLAY_VAL, sizeof(x->replay), &x->replay);
1356 NLA_PUT(skb, XFRMA_LTIME_VAL, sizeof(x->curlft), &x->curlft);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001357
Thomas Grafc0144be2007-08-22 13:55:43 -07001358 if (id->flags & XFRM_AE_RTHR)
1359 NLA_PUT_U32(skb, XFRMA_REPLAY_THRESH, x->replay_maxdiff);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001360
Thomas Grafc0144be2007-08-22 13:55:43 -07001361 if (id->flags & XFRM_AE_ETHR)
1362 NLA_PUT_U32(skb, XFRMA_ETIMER_THRESH,
1363 x->replay_maxage * 10 / HZ);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001364
Thomas Graf98250692007-08-22 12:47:26 -07001365 return nlmsg_end(skb, nlh);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001366
Thomas Grafc0144be2007-08-22 13:55:43 -07001367nla_put_failure:
Thomas Graf98250692007-08-22 12:47:26 -07001368 nlmsg_cancel(skb, nlh);
1369 return -EMSGSIZE;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001370}
1371
Christoph Hellwig22e70052007-01-02 15:22:30 -08001372static int xfrm_get_ae(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001373 struct nlattr **attrs)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001374{
1375 struct xfrm_state *x;
1376 struct sk_buff *r_skb;
1377 int err;
1378 struct km_event c;
Thomas Graf7b67c852007-08-22 13:53:52 -07001379 struct xfrm_aevent_id *p = nlmsg_data(nlh);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001380 struct xfrm_usersa_id *id = &p->sa_id;
1381
Thomas Graf7deb2262007-08-22 13:57:39 -07001382 r_skb = nlmsg_new(xfrm_aevent_msgsize(), GFP_ATOMIC);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001383 if (r_skb == NULL)
1384 return -ENOMEM;
1385
1386 x = xfrm_state_lookup(&id->daddr, id->spi, id->proto, id->family);
1387 if (x == NULL) {
Patrick McHardyb08d5842007-02-27 09:57:37 -08001388 kfree_skb(r_skb);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001389 return -ESRCH;
1390 }
1391
1392 /*
1393 * XXX: is this lock really needed - none of the other
1394 * gets lock (the concern is things getting updated
1395 * while we are still reading) - jhs
1396 */
1397 spin_lock_bh(&x->lock);
1398 c.data.aevent = p->flags;
1399 c.seq = nlh->nlmsg_seq;
1400 c.pid = nlh->nlmsg_pid;
1401
1402 if (build_aevent(r_skb, x, &c) < 0)
1403 BUG();
Thomas Graf082a1ad2007-08-22 13:54:36 -07001404 err = nlmsg_unicast(xfrm_nl, r_skb, NETLINK_CB(skb).pid);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001405 spin_unlock_bh(&x->lock);
1406 xfrm_state_put(x);
1407 return err;
1408}
1409
Christoph Hellwig22e70052007-01-02 15:22:30 -08001410static int xfrm_new_ae(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001411 struct nlattr **attrs)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001412{
1413 struct xfrm_state *x;
1414 struct km_event c;
1415 int err = - EINVAL;
Thomas Graf7b67c852007-08-22 13:53:52 -07001416 struct xfrm_aevent_id *p = nlmsg_data(nlh);
Thomas Graf5424f322007-08-22 14:01:33 -07001417 struct nlattr *rp = attrs[XFRMA_REPLAY_VAL];
1418 struct nlattr *lt = attrs[XFRMA_LTIME_VAL];
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001419
1420 if (!lt && !rp)
1421 return err;
1422
1423 /* pedantic mode - thou shalt sayeth replaceth */
1424 if (!(nlh->nlmsg_flags&NLM_F_REPLACE))
1425 return err;
1426
1427 x = xfrm_state_lookup(&p->sa_id.daddr, p->sa_id.spi, p->sa_id.proto, p->sa_id.family);
1428 if (x == NULL)
1429 return -ESRCH;
1430
1431 if (x->km.state != XFRM_STATE_VALID)
1432 goto out;
1433
1434 spin_lock_bh(&x->lock);
Thomas Graf35a7aa02007-08-22 14:00:40 -07001435 xfrm_update_ae_params(x, attrs);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001436 spin_unlock_bh(&x->lock);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001437
1438 c.event = nlh->nlmsg_type;
1439 c.seq = nlh->nlmsg_seq;
1440 c.pid = nlh->nlmsg_pid;
1441 c.data.aevent = XFRM_AE_CU;
1442 km_state_notify(x, &c);
1443 err = 0;
1444out:
1445 xfrm_state_put(x);
1446 return err;
1447}
1448
Christoph Hellwig22e70052007-01-02 15:22:30 -08001449static int xfrm_flush_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001450 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451{
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001452 struct km_event c;
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001453 u8 type = XFRM_POLICY_TYPE_MAIN;
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001454 int err;
Joy Latten161a09e2006-11-27 13:11:54 -06001455 struct xfrm_audit audit_info;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001456
Thomas Graf35a7aa02007-08-22 14:00:40 -07001457 err = copy_from_user_policy_type(&type, attrs);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001458 if (err)
1459 return err;
1460
Joy Latten161a09e2006-11-27 13:11:54 -06001461 audit_info.loginuid = NETLINK_CB(skb).loginuid;
1462 audit_info.secid = NETLINK_CB(skb).sid;
Joy Latten4aa2e622007-06-04 19:05:57 -04001463 err = xfrm_policy_flush(type, &audit_info);
1464 if (err)
1465 return err;
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001466 c.data.type = type;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001467 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001468 c.seq = nlh->nlmsg_seq;
1469 c.pid = nlh->nlmsg_pid;
1470 km_policy_notify(NULL, 0, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471 return 0;
1472}
1473
Christoph Hellwig22e70052007-01-02 15:22:30 -08001474static int xfrm_add_pol_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001475 struct nlattr **attrs)
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001476{
1477 struct xfrm_policy *xp;
Thomas Graf7b67c852007-08-22 13:53:52 -07001478 struct xfrm_user_polexpire *up = nlmsg_data(nlh);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001479 struct xfrm_userpolicy_info *p = &up->pol;
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001480 u8 type = XFRM_POLICY_TYPE_MAIN;
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001481 int err = -ENOENT;
1482
Thomas Graf35a7aa02007-08-22 14:00:40 -07001483 err = copy_from_user_policy_type(&type, attrs);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001484 if (err)
1485 return err;
1486
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001487 if (p->index)
Eric Parisef41aaa2007-03-07 15:37:58 -08001488 xp = xfrm_policy_byid(type, p->dir, p->index, 0, &err);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001489 else {
Thomas Graf5424f322007-08-22 14:01:33 -07001490 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001491 struct xfrm_policy tmp;
1492
Thomas Graf35a7aa02007-08-22 14:00:40 -07001493 err = verify_sec_ctx_len(attrs);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001494 if (err)
1495 return err;
1496
1497 memset(&tmp, 0, sizeof(struct xfrm_policy));
1498 if (rt) {
Thomas Graf5424f322007-08-22 14:01:33 -07001499 struct xfrm_user_sec_ctx *uctx = nla_data(rt);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001500
1501 if ((err = security_xfrm_policy_alloc(&tmp, uctx)))
1502 return err;
1503 }
Eric Parisef41aaa2007-03-07 15:37:58 -08001504 xp = xfrm_policy_bysel_ctx(type, p->dir, &p->sel, tmp.security,
1505 0, &err);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001506 security_xfrm_policy_free(&tmp);
1507 }
1508
1509 if (xp == NULL)
Eric Parisef41aaa2007-03-07 15:37:58 -08001510 return -ENOENT;
1511 read_lock(&xp->lock);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001512 if (xp->dead) {
1513 read_unlock(&xp->lock);
1514 goto out;
1515 }
1516
1517 read_unlock(&xp->lock);
1518 err = 0;
1519 if (up->hard) {
1520 xfrm_policy_delete(xp, p->dir);
Joy Lattenab5f5e82007-09-17 11:51:22 -07001521 xfrm_audit_policy_delete(xp, 1, NETLINK_CB(skb).loginuid,
1522 NETLINK_CB(skb).sid);
Joy Latten161a09e2006-11-27 13:11:54 -06001523
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001524 } else {
1525 // reset the timers here?
1526 printk("Dont know what to do with soft policy expire\n");
1527 }
1528 km_policy_expired(xp, p->dir, up->hard, current->pid);
1529
1530out:
1531 xfrm_pol_put(xp);
1532 return err;
1533}
1534
Christoph Hellwig22e70052007-01-02 15:22:30 -08001535static int xfrm_add_sa_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001536 struct nlattr **attrs)
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001537{
1538 struct xfrm_state *x;
1539 int err;
Thomas Graf7b67c852007-08-22 13:53:52 -07001540 struct xfrm_user_expire *ue = nlmsg_data(nlh);
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001541 struct xfrm_usersa_info *p = &ue->state;
1542
1543 x = xfrm_state_lookup(&p->id.daddr, p->id.spi, p->id.proto, p->family);
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001544
David S. Miller3a765aa2007-02-26 14:52:21 -08001545 err = -ENOENT;
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001546 if (x == NULL)
1547 return err;
1548
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001549 spin_lock_bh(&x->lock);
David S. Miller3a765aa2007-02-26 14:52:21 -08001550 err = -EINVAL;
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001551 if (x->km.state != XFRM_STATE_VALID)
1552 goto out;
1553 km_state_expired(x, ue->hard, current->pid);
1554
Joy Latten161a09e2006-11-27 13:11:54 -06001555 if (ue->hard) {
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001556 __xfrm_state_delete(x);
Joy Lattenab5f5e82007-09-17 11:51:22 -07001557 xfrm_audit_state_delete(x, 1, NETLINK_CB(skb).loginuid,
1558 NETLINK_CB(skb).sid);
Joy Latten161a09e2006-11-27 13:11:54 -06001559 }
David S. Miller3a765aa2007-02-26 14:52:21 -08001560 err = 0;
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001561out:
1562 spin_unlock_bh(&x->lock);
1563 xfrm_state_put(x);
1564 return err;
1565}
1566
Christoph Hellwig22e70052007-01-02 15:22:30 -08001567static int xfrm_add_acquire(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001568 struct nlattr **attrs)
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001569{
1570 struct xfrm_policy *xp;
1571 struct xfrm_user_tmpl *ut;
1572 int i;
Thomas Graf5424f322007-08-22 14:01:33 -07001573 struct nlattr *rt = attrs[XFRMA_TMPL];
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001574
Thomas Graf7b67c852007-08-22 13:53:52 -07001575 struct xfrm_user_acquire *ua = nlmsg_data(nlh);
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001576 struct xfrm_state *x = xfrm_state_alloc();
1577 int err = -ENOMEM;
1578
1579 if (!x)
1580 return err;
1581
1582 err = verify_newpolicy_info(&ua->policy);
1583 if (err) {
1584 printk("BAD policy passed\n");
1585 kfree(x);
1586 return err;
1587 }
1588
1589 /* build an XP */
Thomas Graf5424f322007-08-22 14:01:33 -07001590 xp = xfrm_policy_construct(&ua->policy, attrs, &err);
David S. Millerb4ad86bf2006-12-03 19:19:26 -08001591 if (!xp) {
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001592 kfree(x);
1593 return err;
1594 }
1595
1596 memcpy(&x->id, &ua->id, sizeof(ua->id));
1597 memcpy(&x->props.saddr, &ua->saddr, sizeof(ua->saddr));
1598 memcpy(&x->sel, &ua->sel, sizeof(ua->sel));
1599
Thomas Graf5424f322007-08-22 14:01:33 -07001600 ut = nla_data(rt);
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001601 /* extract the templates and for each call km_key */
1602 for (i = 0; i < xp->xfrm_nr; i++, ut++) {
1603 struct xfrm_tmpl *t = &xp->xfrm_vec[i];
1604 memcpy(&x->id, &t->id, sizeof(x->id));
1605 x->props.mode = t->mode;
1606 x->props.reqid = t->reqid;
1607 x->props.family = ut->family;
1608 t->aalgos = ua->aalgos;
1609 t->ealgos = ua->ealgos;
1610 t->calgos = ua->calgos;
1611 err = km_query(x, t, xp);
1612
1613 }
1614
1615 kfree(x);
1616 kfree(xp);
1617
1618 return 0;
1619}
1620
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001621#ifdef CONFIG_XFRM_MIGRATE
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001622static int copy_from_user_migrate(struct xfrm_migrate *ma,
Thomas Graf5424f322007-08-22 14:01:33 -07001623 struct nlattr **attrs, int *num)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001624{
Thomas Graf5424f322007-08-22 14:01:33 -07001625 struct nlattr *rt = attrs[XFRMA_MIGRATE];
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001626 struct xfrm_user_migrate *um;
1627 int i, num_migrate;
1628
Thomas Graf5424f322007-08-22 14:01:33 -07001629 um = nla_data(rt);
1630 num_migrate = nla_len(rt) / sizeof(*um);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001631
1632 if (num_migrate <= 0 || num_migrate > XFRM_MAX_DEPTH)
1633 return -EINVAL;
1634
1635 for (i = 0; i < num_migrate; i++, um++, ma++) {
1636 memcpy(&ma->old_daddr, &um->old_daddr, sizeof(ma->old_daddr));
1637 memcpy(&ma->old_saddr, &um->old_saddr, sizeof(ma->old_saddr));
1638 memcpy(&ma->new_daddr, &um->new_daddr, sizeof(ma->new_daddr));
1639 memcpy(&ma->new_saddr, &um->new_saddr, sizeof(ma->new_saddr));
1640
1641 ma->proto = um->proto;
1642 ma->mode = um->mode;
1643 ma->reqid = um->reqid;
1644
1645 ma->old_family = um->old_family;
1646 ma->new_family = um->new_family;
1647 }
1648
1649 *num = i;
1650 return 0;
1651}
1652
1653static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001654 struct nlattr **attrs)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001655{
Thomas Graf7b67c852007-08-22 13:53:52 -07001656 struct xfrm_userpolicy_id *pi = nlmsg_data(nlh);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001657 struct xfrm_migrate m[XFRM_MAX_DEPTH];
1658 u8 type;
1659 int err;
1660 int n = 0;
1661
Thomas Graf35a7aa02007-08-22 14:00:40 -07001662 if (attrs[XFRMA_MIGRATE] == NULL)
Thomas Grafcf5cb792007-08-22 13:59:04 -07001663 return -EINVAL;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001664
Thomas Graf5424f322007-08-22 14:01:33 -07001665 err = copy_from_user_policy_type(&type, attrs);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001666 if (err)
1667 return err;
1668
1669 err = copy_from_user_migrate((struct xfrm_migrate *)m,
Thomas Graf5424f322007-08-22 14:01:33 -07001670 attrs, &n);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001671 if (err)
1672 return err;
1673
1674 if (!n)
1675 return 0;
1676
1677 xfrm_migrate(&pi->sel, pi->dir, type, m, n);
1678
1679 return 0;
1680}
1681#else
1682static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001683 struct nlattr **attrs)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001684{
1685 return -ENOPROTOOPT;
1686}
1687#endif
1688
1689#ifdef CONFIG_XFRM_MIGRATE
1690static int copy_to_user_migrate(struct xfrm_migrate *m, struct sk_buff *skb)
1691{
1692 struct xfrm_user_migrate um;
1693
1694 memset(&um, 0, sizeof(um));
1695 um.proto = m->proto;
1696 um.mode = m->mode;
1697 um.reqid = m->reqid;
1698 um.old_family = m->old_family;
1699 memcpy(&um.old_daddr, &m->old_daddr, sizeof(um.old_daddr));
1700 memcpy(&um.old_saddr, &m->old_saddr, sizeof(um.old_saddr));
1701 um.new_family = m->new_family;
1702 memcpy(&um.new_daddr, &m->new_daddr, sizeof(um.new_daddr));
1703 memcpy(&um.new_saddr, &m->new_saddr, sizeof(um.new_saddr));
1704
Thomas Grafc0144be2007-08-22 13:55:43 -07001705 return nla_put(skb, XFRMA_MIGRATE, sizeof(um), &um);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001706}
1707
Thomas Graf7deb2262007-08-22 13:57:39 -07001708static inline size_t xfrm_migrate_msgsize(int num_migrate)
1709{
1710 return NLMSG_ALIGN(sizeof(struct xfrm_userpolicy_id))
1711 + nla_total_size(sizeof(struct xfrm_user_migrate) * num_migrate)
1712 + userpolicy_type_attrsize();
1713}
1714
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001715static int build_migrate(struct sk_buff *skb, struct xfrm_migrate *m,
1716 int num_migrate, struct xfrm_selector *sel,
1717 u8 dir, u8 type)
1718{
1719 struct xfrm_migrate *mp;
1720 struct xfrm_userpolicy_id *pol_id;
1721 struct nlmsghdr *nlh;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001722 int i;
1723
Thomas Graf79b8b7f2007-08-22 12:46:53 -07001724 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_MIGRATE, sizeof(*pol_id), 0);
1725 if (nlh == NULL)
1726 return -EMSGSIZE;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001727
Thomas Graf7b67c852007-08-22 13:53:52 -07001728 pol_id = nlmsg_data(nlh);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001729 /* copy data from selector, dir, and type to the pol_id */
1730 memset(pol_id, 0, sizeof(*pol_id));
1731 memcpy(&pol_id->sel, sel, sizeof(pol_id->sel));
1732 pol_id->dir = dir;
1733
1734 if (copy_to_user_policy_type(type, skb) < 0)
1735 goto nlmsg_failure;
1736
1737 for (i = 0, mp = m ; i < num_migrate; i++, mp++) {
1738 if (copy_to_user_migrate(mp, skb) < 0)
1739 goto nlmsg_failure;
1740 }
1741
Thomas Graf98250692007-08-22 12:47:26 -07001742 return nlmsg_end(skb, nlh);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001743nlmsg_failure:
Thomas Graf98250692007-08-22 12:47:26 -07001744 nlmsg_cancel(skb, nlh);
1745 return -EMSGSIZE;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001746}
1747
1748static int xfrm_send_migrate(struct xfrm_selector *sel, u8 dir, u8 type,
1749 struct xfrm_migrate *m, int num_migrate)
1750{
1751 struct sk_buff *skb;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001752
Thomas Graf7deb2262007-08-22 13:57:39 -07001753 skb = nlmsg_new(xfrm_migrate_msgsize(num_migrate), GFP_ATOMIC);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001754 if (skb == NULL)
1755 return -ENOMEM;
1756
1757 /* build migrate */
1758 if (build_migrate(skb, m, num_migrate, sel, dir, type) < 0)
1759 BUG();
1760
Thomas Graf082a1ad2007-08-22 13:54:36 -07001761 return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_MIGRATE, GFP_ATOMIC);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001762}
1763#else
1764static int xfrm_send_migrate(struct xfrm_selector *sel, u8 dir, u8 type,
1765 struct xfrm_migrate *m, int num_migrate)
1766{
1767 return -ENOPROTOOPT;
1768}
1769#endif
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001770
Thomas Grafa7bd9a42007-08-22 13:58:18 -07001771#define XMSGSIZE(type) sizeof(struct type)
Thomas Graf492b5582005-05-03 14:26:40 -07001772
1773static const int xfrm_msg_min[XFRM_NR_MSGTYPES] = {
1774 [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
1775 [XFRM_MSG_DELSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
1776 [XFRM_MSG_GETSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
1777 [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
1778 [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
1779 [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
1780 [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userspi_info),
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001781 [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_acquire),
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001782 [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_expire),
Thomas Graf492b5582005-05-03 14:26:40 -07001783 [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
1784 [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001785 [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_polexpire),
Thomas Graf492b5582005-05-03 14:26:40 -07001786 [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_flush),
Thomas Grafa7bd9a42007-08-22 13:58:18 -07001787 [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = 0,
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001788 [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
1789 [XFRM_MSG_GETAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07001790 [XFRM_MSG_REPORT - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_report),
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001791 [XFRM_MSG_MIGRATE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
Thomas Grafa7bd9a42007-08-22 13:58:18 -07001792 [XFRM_MSG_GETSADINFO - XFRM_MSG_BASE] = sizeof(u32),
1793 [XFRM_MSG_GETSPDINFO - XFRM_MSG_BASE] = sizeof(u32),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794};
1795
Thomas Graf492b5582005-05-03 14:26:40 -07001796#undef XMSGSIZE
1797
Thomas Grafcf5cb792007-08-22 13:59:04 -07001798static const struct nla_policy xfrma_policy[XFRMA_MAX+1] = {
1799 [XFRMA_ALG_AUTH] = { .len = sizeof(struct xfrm_algo) },
1800 [XFRMA_ALG_CRYPT] = { .len = sizeof(struct xfrm_algo) },
1801 [XFRMA_ALG_COMP] = { .len = sizeof(struct xfrm_algo) },
1802 [XFRMA_ENCAP] = { .len = sizeof(struct xfrm_encap_tmpl) },
1803 [XFRMA_TMPL] = { .len = sizeof(struct xfrm_user_tmpl) },
1804 [XFRMA_SEC_CTX] = { .len = sizeof(struct xfrm_sec_ctx) },
1805 [XFRMA_LTIME_VAL] = { .len = sizeof(struct xfrm_lifetime_cur) },
1806 [XFRMA_REPLAY_VAL] = { .len = sizeof(struct xfrm_replay_state) },
1807 [XFRMA_REPLAY_THRESH] = { .type = NLA_U32 },
1808 [XFRMA_ETIMER_THRESH] = { .type = NLA_U32 },
1809 [XFRMA_SRCADDR] = { .len = sizeof(xfrm_address_t) },
1810 [XFRMA_COADDR] = { .len = sizeof(xfrm_address_t) },
1811 [XFRMA_POLICY_TYPE] = { .len = sizeof(struct xfrm_userpolicy_type)},
1812 [XFRMA_MIGRATE] = { .len = sizeof(struct xfrm_user_migrate) },
1813};
1814
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815static struct xfrm_link {
Thomas Graf5424f322007-08-22 14:01:33 -07001816 int (*doit)(struct sk_buff *, struct nlmsghdr *, struct nlattr **);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817 int (*dump)(struct sk_buff *, struct netlink_callback *);
Thomas Graf492b5582005-05-03 14:26:40 -07001818} xfrm_dispatch[XFRM_NR_MSGTYPES] = {
1819 [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa },
1820 [XFRM_MSG_DELSA - XFRM_MSG_BASE] = { .doit = xfrm_del_sa },
1821 [XFRM_MSG_GETSA - XFRM_MSG_BASE] = { .doit = xfrm_get_sa,
1822 .dump = xfrm_dump_sa },
1823 [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy },
1824 [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy },
1825 [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy,
1826 .dump = xfrm_dump_policy },
1827 [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = { .doit = xfrm_alloc_userspi },
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001828 [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_acquire },
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001829 [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_sa_expire },
Thomas Graf492b5582005-05-03 14:26:40 -07001830 [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy },
1831 [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa },
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001832 [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_pol_expire},
Thomas Graf492b5582005-05-03 14:26:40 -07001833 [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = { .doit = xfrm_flush_sa },
1834 [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_flush_policy },
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001835 [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = { .doit = xfrm_new_ae },
1836 [XFRM_MSG_GETAE - XFRM_MSG_BASE] = { .doit = xfrm_get_ae },
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001837 [XFRM_MSG_MIGRATE - XFRM_MSG_BASE] = { .doit = xfrm_do_migrate },
Jamal Hadi Salim566ec032007-04-26 14:12:15 -07001838 [XFRM_MSG_GETSADINFO - XFRM_MSG_BASE] = { .doit = xfrm_get_sadinfo },
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -07001839 [XFRM_MSG_GETSPDINFO - XFRM_MSG_BASE] = { .doit = xfrm_get_spdinfo },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840};
1841
Thomas Graf1d00a4e2007-03-22 23:30:12 -07001842static int xfrm_user_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843{
Thomas Graf35a7aa02007-08-22 14:00:40 -07001844 struct nlattr *attrs[XFRMA_MAX+1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845 struct xfrm_link *link;
Thomas Grafa7bd9a42007-08-22 13:58:18 -07001846 int type, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848 type = nlh->nlmsg_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849 if (type > XFRM_MSG_MAX)
Thomas Graf1d00a4e2007-03-22 23:30:12 -07001850 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851
1852 type -= XFRM_MSG_BASE;
1853 link = &xfrm_dispatch[type];
1854
1855 /* All operations require privileges, even GET */
Thomas Graf1d00a4e2007-03-22 23:30:12 -07001856 if (security_netlink_recv(skb, CAP_NET_ADMIN))
1857 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858
Thomas Graf492b5582005-05-03 14:26:40 -07001859 if ((type == (XFRM_MSG_GETSA - XFRM_MSG_BASE) ||
1860 type == (XFRM_MSG_GETPOLICY - XFRM_MSG_BASE)) &&
1861 (nlh->nlmsg_flags & NLM_F_DUMP)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862 if (link->dump == NULL)
Thomas Graf1d00a4e2007-03-22 23:30:12 -07001863 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864
Thomas Grafc702e802007-03-22 23:30:55 -07001865 return netlink_dump_start(xfrm_nl, skb, nlh, link->dump, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866 }
1867
Thomas Graf35a7aa02007-08-22 14:00:40 -07001868 err = nlmsg_parse(nlh, xfrm_msg_min[type], attrs, XFRMA_MAX,
Thomas Grafcf5cb792007-08-22 13:59:04 -07001869 xfrma_policy);
Thomas Grafa7bd9a42007-08-22 13:58:18 -07001870 if (err < 0)
1871 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872
1873 if (link->doit == NULL)
Thomas Graf1d00a4e2007-03-22 23:30:12 -07001874 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875
Thomas Graf5424f322007-08-22 14:01:33 -07001876 return link->doit(skb, nlh, attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877}
1878
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879static void xfrm_netlink_rcv(struct sock *sk, int len)
1880{
Thomas Graf88fc2c82005-11-10 02:25:54 +01001881 unsigned int qlen = 0;
Herbert Xu2a0a6eb2005-05-03 14:55:09 -07001882
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883 do {
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -08001884 mutex_lock(&xfrm_cfg_mutex);
Herbert Xu0cfad072007-09-16 16:24:44 -07001885 qlen = netlink_run_queue(sk, qlen, &xfrm_user_rcv_msg);
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -08001886 mutex_unlock(&xfrm_cfg_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887
Herbert Xu2a0a6eb2005-05-03 14:55:09 -07001888 } while (qlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889}
1890
Thomas Graf7deb2262007-08-22 13:57:39 -07001891static inline size_t xfrm_expire_msgsize(void)
1892{
1893 return NLMSG_ALIGN(sizeof(struct xfrm_user_expire));
1894}
1895
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001896static int build_expire(struct sk_buff *skb, struct xfrm_state *x, struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897{
1898 struct xfrm_user_expire *ue;
1899 struct nlmsghdr *nlh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900
Thomas Graf79b8b7f2007-08-22 12:46:53 -07001901 nlh = nlmsg_put(skb, c->pid, 0, XFRM_MSG_EXPIRE, sizeof(*ue), 0);
1902 if (nlh == NULL)
1903 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904
Thomas Graf7b67c852007-08-22 13:53:52 -07001905 ue = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906 copy_to_user_state(x, &ue->state);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001907 ue->hard = (c->data.hard != 0) ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908
Thomas Graf98250692007-08-22 12:47:26 -07001909 return nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910}
1911
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001912static int xfrm_exp_state_notify(struct xfrm_state *x, struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913{
1914 struct sk_buff *skb;
1915
Thomas Graf7deb2262007-08-22 13:57:39 -07001916 skb = nlmsg_new(xfrm_expire_msgsize(), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917 if (skb == NULL)
1918 return -ENOMEM;
1919
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001920 if (build_expire(skb, x, c) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921 BUG();
1922
Thomas Graf082a1ad2007-08-22 13:54:36 -07001923 return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_EXPIRE, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924}
1925
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001926static int xfrm_aevent_state_notify(struct xfrm_state *x, struct km_event *c)
1927{
1928 struct sk_buff *skb;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001929
Thomas Graf7deb2262007-08-22 13:57:39 -07001930 skb = nlmsg_new(xfrm_aevent_msgsize(), GFP_ATOMIC);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001931 if (skb == NULL)
1932 return -ENOMEM;
1933
1934 if (build_aevent(skb, x, c) < 0)
1935 BUG();
1936
Thomas Graf082a1ad2007-08-22 13:54:36 -07001937 return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_AEVENTS, GFP_ATOMIC);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001938}
1939
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001940static int xfrm_notify_sa_flush(struct km_event *c)
1941{
1942 struct xfrm_usersa_flush *p;
1943 struct nlmsghdr *nlh;
1944 struct sk_buff *skb;
Thomas Graf7deb2262007-08-22 13:57:39 -07001945 int len = NLMSG_ALIGN(sizeof(struct xfrm_usersa_flush));
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001946
Thomas Graf7deb2262007-08-22 13:57:39 -07001947 skb = nlmsg_new(len, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001948 if (skb == NULL)
1949 return -ENOMEM;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001950
Thomas Graf79b8b7f2007-08-22 12:46:53 -07001951 nlh = nlmsg_put(skb, c->pid, c->seq, XFRM_MSG_FLUSHSA, sizeof(*p), 0);
1952 if (nlh == NULL) {
1953 kfree_skb(skb);
1954 return -EMSGSIZE;
1955 }
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001956
Thomas Graf7b67c852007-08-22 13:53:52 -07001957 p = nlmsg_data(nlh);
Herbert Xubf088672005-06-18 22:44:00 -07001958 p->proto = c->data.proto;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001959
Thomas Graf98250692007-08-22 12:47:26 -07001960 nlmsg_end(skb, nlh);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001961
Thomas Graf082a1ad2007-08-22 13:54:36 -07001962 return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_SA, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001963}
1964
Thomas Graf7deb2262007-08-22 13:57:39 -07001965static inline size_t xfrm_sa_len(struct xfrm_state *x)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001966{
Thomas Graf7deb2262007-08-22 13:57:39 -07001967 size_t l = 0;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001968 if (x->aalg)
Thomas Graf7deb2262007-08-22 13:57:39 -07001969 l += nla_total_size(alg_len(x->aalg));
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001970 if (x->ealg)
Thomas Graf7deb2262007-08-22 13:57:39 -07001971 l += nla_total_size(alg_len(x->ealg));
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001972 if (x->calg)
Thomas Graf7deb2262007-08-22 13:57:39 -07001973 l += nla_total_size(sizeof(*x->calg));
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001974 if (x->encap)
Thomas Graf7deb2262007-08-22 13:57:39 -07001975 l += nla_total_size(sizeof(*x->encap));
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001976
1977 return l;
1978}
1979
1980static int xfrm_notify_sa(struct xfrm_state *x, struct km_event *c)
1981{
1982 struct xfrm_usersa_info *p;
Herbert Xu0603eac2005-06-18 22:54:36 -07001983 struct xfrm_usersa_id *id;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001984 struct nlmsghdr *nlh;
1985 struct sk_buff *skb;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001986 int len = xfrm_sa_len(x);
Herbert Xu0603eac2005-06-18 22:54:36 -07001987 int headlen;
1988
1989 headlen = sizeof(*p);
1990 if (c->event == XFRM_MSG_DELSA) {
Thomas Graf7deb2262007-08-22 13:57:39 -07001991 len += nla_total_size(headlen);
Herbert Xu0603eac2005-06-18 22:54:36 -07001992 headlen = sizeof(*id);
1993 }
Thomas Graf7deb2262007-08-22 13:57:39 -07001994 len += NLMSG_ALIGN(headlen);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001995
Thomas Graf7deb2262007-08-22 13:57:39 -07001996 skb = nlmsg_new(len, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001997 if (skb == NULL)
1998 return -ENOMEM;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001999
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002000 nlh = nlmsg_put(skb, c->pid, c->seq, c->event, headlen, 0);
2001 if (nlh == NULL)
Thomas Grafc0144be2007-08-22 13:55:43 -07002002 goto nla_put_failure;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002003
Thomas Graf7b67c852007-08-22 13:53:52 -07002004 p = nlmsg_data(nlh);
Herbert Xu0603eac2005-06-18 22:54:36 -07002005 if (c->event == XFRM_MSG_DELSA) {
Thomas Grafc0144be2007-08-22 13:55:43 -07002006 struct nlattr *attr;
2007
Thomas Graf7b67c852007-08-22 13:53:52 -07002008 id = nlmsg_data(nlh);
Herbert Xu0603eac2005-06-18 22:54:36 -07002009 memcpy(&id->daddr, &x->id.daddr, sizeof(id->daddr));
2010 id->spi = x->id.spi;
2011 id->family = x->props.family;
2012 id->proto = x->id.proto;
2013
Thomas Grafc0144be2007-08-22 13:55:43 -07002014 attr = nla_reserve(skb, XFRMA_SA, sizeof(*p));
2015 if (attr == NULL)
2016 goto nla_put_failure;
2017
2018 p = nla_data(attr);
Herbert Xu0603eac2005-06-18 22:54:36 -07002019 }
2020
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002021 copy_to_user_state(x, p);
2022
2023 if (x->aalg)
Thomas Grafc26445a2007-08-22 13:56:23 -07002024 NLA_PUT(skb, XFRMA_ALG_AUTH, alg_len(x->aalg), x->aalg);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002025 if (x->ealg)
Thomas Grafc26445a2007-08-22 13:56:23 -07002026 NLA_PUT(skb, XFRMA_ALG_CRYPT, alg_len(x->ealg), x->ealg);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002027 if (x->calg)
Thomas Grafc0144be2007-08-22 13:55:43 -07002028 NLA_PUT(skb, XFRMA_ALG_COMP, sizeof(*(x->calg)), x->calg);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002029
2030 if (x->encap)
Thomas Grafc0144be2007-08-22 13:55:43 -07002031 NLA_PUT(skb, XFRMA_ENCAP, sizeof(*x->encap), x->encap);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002032
Thomas Graf98250692007-08-22 12:47:26 -07002033 nlmsg_end(skb, nlh);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002034
Thomas Graf082a1ad2007-08-22 13:54:36 -07002035 return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_SA, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002036
Thomas Grafc0144be2007-08-22 13:55:43 -07002037nla_put_failure:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002038 kfree_skb(skb);
2039 return -1;
2040}
2041
2042static int xfrm_send_state_notify(struct xfrm_state *x, struct km_event *c)
2043{
2044
2045 switch (c->event) {
Herbert Xuf60f6b82005-06-18 22:44:37 -07002046 case XFRM_MSG_EXPIRE:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002047 return xfrm_exp_state_notify(x, c);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002048 case XFRM_MSG_NEWAE:
2049 return xfrm_aevent_state_notify(x, c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07002050 case XFRM_MSG_DELSA:
2051 case XFRM_MSG_UPDSA:
2052 case XFRM_MSG_NEWSA:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002053 return xfrm_notify_sa(x, c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07002054 case XFRM_MSG_FLUSHSA:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002055 return xfrm_notify_sa_flush(c);
2056 default:
2057 printk("xfrm_user: Unknown SA event %d\n", c->event);
2058 break;
2059 }
2060
2061 return 0;
2062
2063}
2064
Thomas Graf7deb2262007-08-22 13:57:39 -07002065static inline size_t xfrm_acquire_msgsize(struct xfrm_state *x,
2066 struct xfrm_policy *xp)
2067{
2068 return NLMSG_ALIGN(sizeof(struct xfrm_user_acquire))
2069 + nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr)
2070 + nla_total_size(xfrm_user_sec_ctx_size(x->security))
2071 + userpolicy_type_attrsize();
2072}
2073
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074static int build_acquire(struct sk_buff *skb, struct xfrm_state *x,
2075 struct xfrm_tmpl *xt, struct xfrm_policy *xp,
2076 int dir)
2077{
2078 struct xfrm_user_acquire *ua;
2079 struct nlmsghdr *nlh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080 __u32 seq = xfrm_get_acqseq();
2081
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002082 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_ACQUIRE, sizeof(*ua), 0);
2083 if (nlh == NULL)
2084 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085
Thomas Graf7b67c852007-08-22 13:53:52 -07002086 ua = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087 memcpy(&ua->id, &x->id, sizeof(ua->id));
2088 memcpy(&ua->saddr, &x->props.saddr, sizeof(ua->saddr));
2089 memcpy(&ua->sel, &x->sel, sizeof(ua->sel));
2090 copy_to_user_policy(xp, &ua->policy, dir);
2091 ua->aalgos = xt->aalgos;
2092 ua->ealgos = xt->ealgos;
2093 ua->calgos = xt->calgos;
2094 ua->seq = x->km.seq = seq;
2095
2096 if (copy_to_user_tmpl(xp, skb) < 0)
2097 goto nlmsg_failure;
Serge Hallyn0d681622006-07-24 23:30:44 -07002098 if (copy_to_user_state_sec_ctx(x, skb))
Trent Jaegerdf718372005-12-13 23:12:27 -08002099 goto nlmsg_failure;
Jamal Hadi Salim1459bb32006-11-20 16:51:22 -08002100 if (copy_to_user_policy_type(xp->type, skb) < 0)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07002101 goto nlmsg_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102
Thomas Graf98250692007-08-22 12:47:26 -07002103 return nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104
2105nlmsg_failure:
Thomas Graf98250692007-08-22 12:47:26 -07002106 nlmsg_cancel(skb, nlh);
2107 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108}
2109
2110static int xfrm_send_acquire(struct xfrm_state *x, struct xfrm_tmpl *xt,
2111 struct xfrm_policy *xp, int dir)
2112{
2113 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114
Thomas Graf7deb2262007-08-22 13:57:39 -07002115 skb = nlmsg_new(xfrm_acquire_msgsize(x, xp), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116 if (skb == NULL)
2117 return -ENOMEM;
2118
2119 if (build_acquire(skb, x, xt, xp, dir) < 0)
2120 BUG();
2121
Thomas Graf082a1ad2007-08-22 13:54:36 -07002122 return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_ACQUIRE, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123}
2124
2125/* User gives us xfrm_user_policy_info followed by an array of 0
2126 * or more templates.
2127 */
Venkat Yekkiralacb969f02006-07-24 23:32:20 -07002128static struct xfrm_policy *xfrm_compile_policy(struct sock *sk, int opt,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129 u8 *data, int len, int *dir)
2130{
2131 struct xfrm_userpolicy_info *p = (struct xfrm_userpolicy_info *)data;
2132 struct xfrm_user_tmpl *ut = (struct xfrm_user_tmpl *) (p + 1);
2133 struct xfrm_policy *xp;
2134 int nr;
2135
Venkat Yekkiralacb969f02006-07-24 23:32:20 -07002136 switch (sk->sk_family) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137 case AF_INET:
2138 if (opt != IP_XFRM_POLICY) {
2139 *dir = -EOPNOTSUPP;
2140 return NULL;
2141 }
2142 break;
2143#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
2144 case AF_INET6:
2145 if (opt != IPV6_XFRM_POLICY) {
2146 *dir = -EOPNOTSUPP;
2147 return NULL;
2148 }
2149 break;
2150#endif
2151 default:
2152 *dir = -EINVAL;
2153 return NULL;
2154 }
2155
2156 *dir = -EINVAL;
2157
2158 if (len < sizeof(*p) ||
2159 verify_newpolicy_info(p))
2160 return NULL;
2161
2162 nr = ((len - sizeof(*p)) / sizeof(*ut));
David S. Millerb4ad86bf2006-12-03 19:19:26 -08002163 if (validate_tmpl(nr, ut, p->sel.family))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164 return NULL;
2165
Herbert Xua4f1bac2005-07-26 15:43:17 -07002166 if (p->dir > XFRM_POLICY_OUT)
2167 return NULL;
2168
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169 xp = xfrm_policy_alloc(GFP_KERNEL);
2170 if (xp == NULL) {
2171 *dir = -ENOBUFS;
2172 return NULL;
2173 }
2174
2175 copy_from_user_policy(xp, p);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07002176 xp->type = XFRM_POLICY_TYPE_MAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177 copy_templates(xp, ut, nr);
2178
2179 *dir = p->dir;
2180
2181 return xp;
2182}
2183
Thomas Graf7deb2262007-08-22 13:57:39 -07002184static inline size_t xfrm_polexpire_msgsize(struct xfrm_policy *xp)
2185{
2186 return NLMSG_ALIGN(sizeof(struct xfrm_user_polexpire))
2187 + nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr)
2188 + nla_total_size(xfrm_user_sec_ctx_size(xp->security))
2189 + userpolicy_type_attrsize();
2190}
2191
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192static int build_polexpire(struct sk_buff *skb, struct xfrm_policy *xp,
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002193 int dir, struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194{
2195 struct xfrm_user_polexpire *upe;
2196 struct nlmsghdr *nlh;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002197 int hard = c->data.hard;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002199 nlh = nlmsg_put(skb, c->pid, 0, XFRM_MSG_POLEXPIRE, sizeof(*upe), 0);
2200 if (nlh == NULL)
2201 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202
Thomas Graf7b67c852007-08-22 13:53:52 -07002203 upe = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204 copy_to_user_policy(xp, &upe->pol, dir);
2205 if (copy_to_user_tmpl(xp, skb) < 0)
2206 goto nlmsg_failure;
Trent Jaegerdf718372005-12-13 23:12:27 -08002207 if (copy_to_user_sec_ctx(xp, skb))
2208 goto nlmsg_failure;
Jamal Hadi Salim1459bb32006-11-20 16:51:22 -08002209 if (copy_to_user_policy_type(xp->type, skb) < 0)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07002210 goto nlmsg_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211 upe->hard = !!hard;
2212
Thomas Graf98250692007-08-22 12:47:26 -07002213 return nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002214
2215nlmsg_failure:
Thomas Graf98250692007-08-22 12:47:26 -07002216 nlmsg_cancel(skb, nlh);
2217 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218}
2219
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002220static int xfrm_exp_policy_notify(struct xfrm_policy *xp, int dir, struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002221{
2222 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223
Thomas Graf7deb2262007-08-22 13:57:39 -07002224 skb = nlmsg_new(xfrm_polexpire_msgsize(xp), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225 if (skb == NULL)
2226 return -ENOMEM;
2227
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002228 if (build_polexpire(skb, xp, dir, c) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229 BUG();
2230
Thomas Graf082a1ad2007-08-22 13:54:36 -07002231 return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_EXPIRE, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002232}
2233
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002234static int xfrm_notify_policy(struct xfrm_policy *xp, int dir, struct km_event *c)
2235{
2236 struct xfrm_userpolicy_info *p;
Herbert Xu0603eac2005-06-18 22:54:36 -07002237 struct xfrm_userpolicy_id *id;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002238 struct nlmsghdr *nlh;
2239 struct sk_buff *skb;
Thomas Graf7deb2262007-08-22 13:57:39 -07002240 int len = nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr);
Herbert Xu0603eac2005-06-18 22:54:36 -07002241 int headlen;
2242
2243 headlen = sizeof(*p);
2244 if (c->event == XFRM_MSG_DELPOLICY) {
Thomas Graf7deb2262007-08-22 13:57:39 -07002245 len += nla_total_size(headlen);
Herbert Xu0603eac2005-06-18 22:54:36 -07002246 headlen = sizeof(*id);
2247 }
Thomas Grafcfbfd452007-08-22 13:57:04 -07002248 len += userpolicy_type_attrsize();
Thomas Graf7deb2262007-08-22 13:57:39 -07002249 len += NLMSG_ALIGN(headlen);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002250
Thomas Graf7deb2262007-08-22 13:57:39 -07002251 skb = nlmsg_new(len, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002252 if (skb == NULL)
2253 return -ENOMEM;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002254
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002255 nlh = nlmsg_put(skb, c->pid, c->seq, c->event, headlen, 0);
2256 if (nlh == NULL)
2257 goto nlmsg_failure;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002258
Thomas Graf7b67c852007-08-22 13:53:52 -07002259 p = nlmsg_data(nlh);
Herbert Xu0603eac2005-06-18 22:54:36 -07002260 if (c->event == XFRM_MSG_DELPOLICY) {
Thomas Grafc0144be2007-08-22 13:55:43 -07002261 struct nlattr *attr;
2262
Thomas Graf7b67c852007-08-22 13:53:52 -07002263 id = nlmsg_data(nlh);
Herbert Xu0603eac2005-06-18 22:54:36 -07002264 memset(id, 0, sizeof(*id));
2265 id->dir = dir;
2266 if (c->data.byid)
2267 id->index = xp->index;
2268 else
2269 memcpy(&id->sel, &xp->selector, sizeof(id->sel));
2270
Thomas Grafc0144be2007-08-22 13:55:43 -07002271 attr = nla_reserve(skb, XFRMA_POLICY, sizeof(*p));
2272 if (attr == NULL)
2273 goto nlmsg_failure;
2274
2275 p = nla_data(attr);
Herbert Xu0603eac2005-06-18 22:54:36 -07002276 }
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002277
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002278 copy_to_user_policy(xp, p, dir);
2279 if (copy_to_user_tmpl(xp, skb) < 0)
2280 goto nlmsg_failure;
Jamal Hadi Salim1459bb32006-11-20 16:51:22 -08002281 if (copy_to_user_policy_type(xp->type, skb) < 0)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07002282 goto nlmsg_failure;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002283
Thomas Graf98250692007-08-22 12:47:26 -07002284 nlmsg_end(skb, nlh);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002285
Thomas Graf082a1ad2007-08-22 13:54:36 -07002286 return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_POLICY, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002287
2288nlmsg_failure:
2289 kfree_skb(skb);
2290 return -1;
2291}
2292
2293static int xfrm_notify_policy_flush(struct km_event *c)
2294{
2295 struct nlmsghdr *nlh;
2296 struct sk_buff *skb;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002297
Thomas Graf7deb2262007-08-22 13:57:39 -07002298 skb = nlmsg_new(userpolicy_type_attrsize(), GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002299 if (skb == NULL)
2300 return -ENOMEM;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002301
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002302 nlh = nlmsg_put(skb, c->pid, c->seq, XFRM_MSG_FLUSHPOLICY, 0, 0);
2303 if (nlh == NULL)
2304 goto nlmsg_failure;
Jamal Hadi Salim0c51f532006-11-27 12:58:20 -08002305 if (copy_to_user_policy_type(c->data.type, skb) < 0)
2306 goto nlmsg_failure;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002307
Thomas Graf98250692007-08-22 12:47:26 -07002308 nlmsg_end(skb, nlh);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002309
Thomas Graf082a1ad2007-08-22 13:54:36 -07002310 return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_POLICY, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002311
2312nlmsg_failure:
2313 kfree_skb(skb);
2314 return -1;
2315}
2316
2317static int xfrm_send_policy_notify(struct xfrm_policy *xp, int dir, struct km_event *c)
2318{
2319
2320 switch (c->event) {
Herbert Xuf60f6b82005-06-18 22:44:37 -07002321 case XFRM_MSG_NEWPOLICY:
2322 case XFRM_MSG_UPDPOLICY:
2323 case XFRM_MSG_DELPOLICY:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002324 return xfrm_notify_policy(xp, dir, c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07002325 case XFRM_MSG_FLUSHPOLICY:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002326 return xfrm_notify_policy_flush(c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07002327 case XFRM_MSG_POLEXPIRE:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002328 return xfrm_exp_policy_notify(xp, dir, c);
2329 default:
2330 printk("xfrm_user: Unknown Policy event %d\n", c->event);
2331 }
2332
2333 return 0;
2334
2335}
2336
Thomas Graf7deb2262007-08-22 13:57:39 -07002337static inline size_t xfrm_report_msgsize(void)
2338{
2339 return NLMSG_ALIGN(sizeof(struct xfrm_user_report));
2340}
2341
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002342static int build_report(struct sk_buff *skb, u8 proto,
2343 struct xfrm_selector *sel, xfrm_address_t *addr)
2344{
2345 struct xfrm_user_report *ur;
2346 struct nlmsghdr *nlh;
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002347
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002348 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_REPORT, sizeof(*ur), 0);
2349 if (nlh == NULL)
2350 return -EMSGSIZE;
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002351
Thomas Graf7b67c852007-08-22 13:53:52 -07002352 ur = nlmsg_data(nlh);
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002353 ur->proto = proto;
2354 memcpy(&ur->sel, sel, sizeof(ur->sel));
2355
2356 if (addr)
Thomas Grafc0144be2007-08-22 13:55:43 -07002357 NLA_PUT(skb, XFRMA_COADDR, sizeof(*addr), addr);
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002358
Thomas Graf98250692007-08-22 12:47:26 -07002359 return nlmsg_end(skb, nlh);
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002360
Thomas Grafc0144be2007-08-22 13:55:43 -07002361nla_put_failure:
Thomas Graf98250692007-08-22 12:47:26 -07002362 nlmsg_cancel(skb, nlh);
2363 return -EMSGSIZE;
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002364}
2365
2366static int xfrm_send_report(u8 proto, struct xfrm_selector *sel,
2367 xfrm_address_t *addr)
2368{
2369 struct sk_buff *skb;
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002370
Thomas Graf7deb2262007-08-22 13:57:39 -07002371 skb = nlmsg_new(xfrm_report_msgsize(), GFP_ATOMIC);
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002372 if (skb == NULL)
2373 return -ENOMEM;
2374
2375 if (build_report(skb, proto, sel, addr) < 0)
2376 BUG();
2377
Thomas Graf082a1ad2007-08-22 13:54:36 -07002378 return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_REPORT, GFP_ATOMIC);
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002379}
2380
Linus Torvalds1da177e2005-04-16 15:20:36 -07002381static struct xfrm_mgr netlink_mgr = {
2382 .id = "netlink",
2383 .notify = xfrm_send_state_notify,
2384 .acquire = xfrm_send_acquire,
2385 .compile_policy = xfrm_compile_policy,
2386 .notify_policy = xfrm_send_policy_notify,
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002387 .report = xfrm_send_report,
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002388 .migrate = xfrm_send_migrate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002389};
2390
2391static int __init xfrm_user_init(void)
2392{
Patrick McHardybe336902006-03-20 22:40:54 -08002393 struct sock *nlsk;
2394
Masahide NAKAMURA654b32c2006-08-23 19:12:56 -07002395 printk(KERN_INFO "Initializing XFRM netlink socket\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002396
Eric W. Biedermanb4b51022007-09-12 13:05:38 +02002397 nlsk = netlink_kernel_create(&init_net, NETLINK_XFRM, XFRMNLGRP_MAX,
Patrick McHardyaf65bdf2007-04-20 14:14:21 -07002398 xfrm_netlink_rcv, NULL, THIS_MODULE);
Patrick McHardybe336902006-03-20 22:40:54 -08002399 if (nlsk == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002400 return -ENOMEM;
Patrick McHardybe336902006-03-20 22:40:54 -08002401 rcu_assign_pointer(xfrm_nl, nlsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002402
2403 xfrm_register_km(&netlink_mgr);
2404
2405 return 0;
2406}
2407
2408static void __exit xfrm_user_exit(void)
2409{
Patrick McHardybe336902006-03-20 22:40:54 -08002410 struct sock *nlsk = xfrm_nl;
2411
Linus Torvalds1da177e2005-04-16 15:20:36 -07002412 xfrm_unregister_km(&netlink_mgr);
Patrick McHardybe336902006-03-20 22:40:54 -08002413 rcu_assign_pointer(xfrm_nl, NULL);
2414 synchronize_rcu();
2415 sock_release(nlsk->sk_socket);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002416}
2417
2418module_init(xfrm_user_init);
2419module_exit(xfrm_user_exit);
2420MODULE_LICENSE("GPL");
Harald Welte4fdb3bb2005-08-09 19:40:55 -07002421MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK, NETLINK_XFRM);
Jamal Hadi Salimf8cd5482006-03-20 19:15:11 -08002422