blob: c4f6419b176943bd54752c644f0b4cc937587327 [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 Graf5424f322007-08-22 14:01:33 -070034static int verify_one_alg(struct nlattr **attrs, enum xfrm_attr_type_t type)
Linus Torvalds1da177e2005-04-16 15:20:36 -070035{
Thomas Graf5424f322007-08-22 14:01:33 -070036 struct nlattr *rt = attrs[type];
Linus Torvalds1da177e2005-04-16 15:20:36 -070037 struct xfrm_algo *algp;
38
39 if (!rt)
40 return 0;
41
Thomas Graf5424f322007-08-22 14:01:33 -070042 algp = nla_data(rt);
Eric Dumazet0f99be02008-01-08 23:39:06 -080043 if (nla_len(rt) < xfrm_alg_len(algp))
Herbert Xu31c26852005-05-19 12:39:49 -070044 return -EINVAL;
45
Linus Torvalds1da177e2005-04-16 15:20:36 -070046 switch (type) {
47 case XFRMA_ALG_AUTH:
48 if (!algp->alg_key_len &&
49 strcmp(algp->alg_name, "digest_null") != 0)
50 return -EINVAL;
51 break;
52
53 case XFRMA_ALG_CRYPT:
54 if (!algp->alg_key_len &&
55 strcmp(algp->alg_name, "cipher_null") != 0)
56 return -EINVAL;
57 break;
58
59 case XFRMA_ALG_COMP:
60 /* Zero length keys are legal. */
61 break;
62
63 default:
64 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -070065 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070066
67 algp->alg_name[CRYPTO_MAX_ALG_NAME - 1] = '\0';
68 return 0;
69}
70
Thomas Graf5424f322007-08-22 14:01:33 -070071static void verify_one_addr(struct nlattr **attrs, enum xfrm_attr_type_t type,
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -070072 xfrm_address_t **addrp)
73{
Thomas Graf5424f322007-08-22 14:01:33 -070074 struct nlattr *rt = attrs[type];
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -070075
Thomas Grafcf5cb792007-08-22 13:59:04 -070076 if (rt && addrp)
Thomas Graf5424f322007-08-22 14:01:33 -070077 *addrp = nla_data(rt);
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -070078}
Trent Jaegerdf718372005-12-13 23:12:27 -080079
Thomas Graf5424f322007-08-22 14:01:33 -070080static inline int verify_sec_ctx_len(struct nlattr **attrs)
Trent Jaegerdf718372005-12-13 23:12:27 -080081{
Thomas Graf5424f322007-08-22 14:01:33 -070082 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
Trent Jaegerdf718372005-12-13 23:12:27 -080083 struct xfrm_user_sec_ctx *uctx;
Trent Jaegerdf718372005-12-13 23:12:27 -080084
85 if (!rt)
86 return 0;
87
Thomas Graf5424f322007-08-22 14:01:33 -070088 uctx = nla_data(rt);
Thomas Grafcf5cb792007-08-22 13:59:04 -070089 if (uctx->len != (sizeof(struct xfrm_user_sec_ctx) + uctx->ctx_len))
Trent Jaegerdf718372005-12-13 23:12:27 -080090 return -EINVAL;
91
92 return 0;
93}
94
95
Linus Torvalds1da177e2005-04-16 15:20:36 -070096static int verify_newsa_info(struct xfrm_usersa_info *p,
Thomas Graf5424f322007-08-22 14:01:33 -070097 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -070098{
99 int err;
100
101 err = -EINVAL;
102 switch (p->family) {
103 case AF_INET:
104 break;
105
106 case AF_INET6:
107#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
108 break;
109#else
110 err = -EAFNOSUPPORT;
111 goto out;
112#endif
113
114 default:
115 goto out;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700116 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117
118 err = -EINVAL;
119 switch (p->id.proto) {
120 case IPPROTO_AH:
Thomas Graf35a7aa02007-08-22 14:00:40 -0700121 if (!attrs[XFRMA_ALG_AUTH] ||
122 attrs[XFRMA_ALG_CRYPT] ||
123 attrs[XFRMA_ALG_COMP])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 goto out;
125 break;
126
127 case IPPROTO_ESP:
Thomas Graf35a7aa02007-08-22 14:00:40 -0700128 if ((!attrs[XFRMA_ALG_AUTH] &&
129 !attrs[XFRMA_ALG_CRYPT]) ||
130 attrs[XFRMA_ALG_COMP])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 goto out;
132 break;
133
134 case IPPROTO_COMP:
Thomas Graf35a7aa02007-08-22 14:00:40 -0700135 if (!attrs[XFRMA_ALG_COMP] ||
136 attrs[XFRMA_ALG_AUTH] ||
137 attrs[XFRMA_ALG_CRYPT])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 goto out;
139 break;
140
Masahide NAKAMURAe23c7192006-08-23 20:33:28 -0700141#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
142 case IPPROTO_DSTOPTS:
143 case IPPROTO_ROUTING:
Thomas Graf35a7aa02007-08-22 14:00:40 -0700144 if (attrs[XFRMA_ALG_COMP] ||
145 attrs[XFRMA_ALG_AUTH] ||
146 attrs[XFRMA_ALG_CRYPT] ||
147 attrs[XFRMA_ENCAP] ||
148 attrs[XFRMA_SEC_CTX] ||
149 !attrs[XFRMA_COADDR])
Masahide NAKAMURAe23c7192006-08-23 20:33:28 -0700150 goto out;
151 break;
152#endif
153
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 default:
155 goto out;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700156 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157
Thomas Graf35a7aa02007-08-22 14:00:40 -0700158 if ((err = verify_one_alg(attrs, XFRMA_ALG_AUTH)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 goto out;
Thomas Graf35a7aa02007-08-22 14:00:40 -0700160 if ((err = verify_one_alg(attrs, XFRMA_ALG_CRYPT)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 goto out;
Thomas Graf35a7aa02007-08-22 14:00:40 -0700162 if ((err = verify_one_alg(attrs, XFRMA_ALG_COMP)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 goto out;
Thomas Graf35a7aa02007-08-22 14:00:40 -0700164 if ((err = verify_sec_ctx_len(attrs)))
Trent Jaegerdf718372005-12-13 23:12:27 -0800165 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166
167 err = -EINVAL;
168 switch (p->mode) {
Masahide NAKAMURA7e49e6d2006-09-22 15:05:15 -0700169 case XFRM_MODE_TRANSPORT:
170 case XFRM_MODE_TUNNEL:
Noriaki TAKAMIYA060f02a2006-08-23 18:18:55 -0700171 case XFRM_MODE_ROUTEOPTIMIZATION:
Diego Beltrami0a694522006-10-03 23:47:05 -0700172 case XFRM_MODE_BEET:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 break;
174
175 default:
176 goto out;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700177 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178
179 err = 0;
180
181out:
182 return err;
183}
184
185static int attach_one_algo(struct xfrm_algo **algpp, u8 *props,
186 struct xfrm_algo_desc *(*get_byname)(char *, int),
Thomas Graf5424f322007-08-22 14:01:33 -0700187 struct nlattr *rta)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 struct xfrm_algo *p, *ualg;
190 struct xfrm_algo_desc *algo;
191
192 if (!rta)
193 return 0;
194
Thomas Graf5424f322007-08-22 14:01:33 -0700195 ualg = nla_data(rta);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196
197 algo = get_byname(ualg->alg_name, 1);
198 if (!algo)
199 return -ENOSYS;
200 *props = algo->desc.sadb_alg_id;
201
Eric Dumazet0f99be02008-01-08 23:39:06 -0800202 p = kmemdup(ualg, xfrm_alg_len(ualg), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 if (!p)
204 return -ENOMEM;
205
Herbert Xu04ff1262006-08-13 08:50:00 +1000206 strcpy(p->alg_name, algo->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 *algpp = p;
208 return 0;
209}
210
Joy Latten661697f2007-04-13 16:14:35 -0700211static inline int xfrm_user_sec_ctx_size(struct xfrm_sec_ctx *xfrm_ctx)
Trent Jaegerdf718372005-12-13 23:12:27 -0800212{
Trent Jaegerdf718372005-12-13 23:12:27 -0800213 int len = 0;
214
215 if (xfrm_ctx) {
216 len += sizeof(struct xfrm_user_sec_ctx);
217 len += xfrm_ctx->ctx_len;
218 }
219 return len;
220}
221
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222static void copy_from_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p)
223{
224 memcpy(&x->id, &p->id, sizeof(x->id));
225 memcpy(&x->sel, &p->sel, sizeof(x->sel));
226 memcpy(&x->lft, &p->lft, sizeof(x->lft));
227 x->props.mode = p->mode;
228 x->props.replay_window = p->replay_window;
229 x->props.reqid = p->reqid;
230 x->props.family = p->family;
David S. Miller54489c142006-10-27 15:29:47 -0700231 memcpy(&x->props.saddr, &p->saddr, sizeof(x->props.saddr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 x->props.flags = p->flags;
Herbert Xu196b0032007-07-31 02:04:32 -0700233
234 /*
235 * Set inner address family if the KM left it as zero.
236 * See comment in validate_tmpl.
237 */
238 if (!x->sel.family)
239 x->sel.family = p->family;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240}
241
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800242/*
243 * someday when pfkey also has support, we could have the code
244 * somehow made shareable and move it to xfrm_state.c - JHS
245 *
246*/
Thomas Graf5424f322007-08-22 14:01:33 -0700247static void xfrm_update_ae_params(struct xfrm_state *x, struct nlattr **attrs)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800248{
Thomas Graf5424f322007-08-22 14:01:33 -0700249 struct nlattr *rp = attrs[XFRMA_REPLAY_VAL];
250 struct nlattr *lt = attrs[XFRMA_LTIME_VAL];
251 struct nlattr *et = attrs[XFRMA_ETIMER_THRESH];
252 struct nlattr *rt = attrs[XFRMA_REPLAY_THRESH];
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800253
254 if (rp) {
255 struct xfrm_replay_state *replay;
Thomas Graf5424f322007-08-22 14:01:33 -0700256 replay = nla_data(rp);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800257 memcpy(&x->replay, replay, sizeof(*replay));
258 memcpy(&x->preplay, replay, sizeof(*replay));
259 }
260
261 if (lt) {
262 struct xfrm_lifetime_cur *ltime;
Thomas Graf5424f322007-08-22 14:01:33 -0700263 ltime = nla_data(lt);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800264 x->curlft.bytes = ltime->bytes;
265 x->curlft.packets = ltime->packets;
266 x->curlft.add_time = ltime->add_time;
267 x->curlft.use_time = ltime->use_time;
268 }
269
Thomas Grafcf5cb792007-08-22 13:59:04 -0700270 if (et)
Thomas Graf5424f322007-08-22 14:01:33 -0700271 x->replay_maxage = nla_get_u32(et);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800272
Thomas Grafcf5cb792007-08-22 13:59:04 -0700273 if (rt)
Thomas Graf5424f322007-08-22 14:01:33 -0700274 x->replay_maxdiff = nla_get_u32(rt);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800275}
276
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277static struct xfrm_state *xfrm_state_construct(struct xfrm_usersa_info *p,
Thomas Graf5424f322007-08-22 14:01:33 -0700278 struct nlattr **attrs,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 int *errp)
280{
281 struct xfrm_state *x = xfrm_state_alloc();
282 int err = -ENOMEM;
283
284 if (!x)
285 goto error_no_put;
286
287 copy_from_user_state(x, p);
288
289 if ((err = attach_one_algo(&x->aalg, &x->props.aalgo,
290 xfrm_aalg_get_byname,
Thomas Graf35a7aa02007-08-22 14:00:40 -0700291 attrs[XFRMA_ALG_AUTH])))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 goto error;
293 if ((err = attach_one_algo(&x->ealg, &x->props.ealgo,
294 xfrm_ealg_get_byname,
Thomas Graf35a7aa02007-08-22 14:00:40 -0700295 attrs[XFRMA_ALG_CRYPT])))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 goto error;
297 if ((err = attach_one_algo(&x->calg, &x->props.calgo,
298 xfrm_calg_get_byname,
Thomas Graf35a7aa02007-08-22 14:00:40 -0700299 attrs[XFRMA_ALG_COMP])))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 goto error;
Thomas Graffd211502007-09-06 03:28:08 -0700301
302 if (attrs[XFRMA_ENCAP]) {
303 x->encap = kmemdup(nla_data(attrs[XFRMA_ENCAP]),
304 sizeof(*x->encap), GFP_KERNEL);
305 if (x->encap == NULL)
306 goto error;
307 }
308
309 if (attrs[XFRMA_COADDR]) {
310 x->coaddr = kmemdup(nla_data(attrs[XFRMA_COADDR]),
311 sizeof(*x->coaddr), GFP_KERNEL);
312 if (x->coaddr == NULL)
313 goto error;
314 }
315
Herbert Xu72cb6962005-06-20 13:18:08 -0700316 err = xfrm_init_state(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 if (err)
318 goto error;
319
Thomas Graffd211502007-09-06 03:28:08 -0700320 if (attrs[XFRMA_SEC_CTX] &&
321 security_xfrm_state_alloc(x, nla_data(attrs[XFRMA_SEC_CTX])))
Trent Jaegerdf718372005-12-13 23:12:27 -0800322 goto error;
323
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 x->km.seq = p->seq;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800325 x->replay_maxdiff = sysctl_xfrm_aevent_rseqth;
326 /* sysctl_xfrm_aevent_etime is in 100ms units */
327 x->replay_maxage = (sysctl_xfrm_aevent_etime*HZ)/XFRM_AE_ETH_M;
328 x->preplay.bitmap = 0;
329 x->preplay.seq = x->replay.seq+x->replay_maxdiff;
330 x->preplay.oseq = x->replay.oseq +x->replay_maxdiff;
331
332 /* override default values from above */
333
Thomas Graf5424f322007-08-22 14:01:33 -0700334 xfrm_update_ae_params(x, attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335
336 return x;
337
338error:
339 x->km.state = XFRM_STATE_DEAD;
340 xfrm_state_put(x);
341error_no_put:
342 *errp = err;
343 return NULL;
344}
345
Christoph Hellwig22e70052007-01-02 15:22:30 -0800346static int xfrm_add_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -0700347 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348{
Thomas Graf7b67c852007-08-22 13:53:52 -0700349 struct xfrm_usersa_info *p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 struct xfrm_state *x;
351 int err;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700352 struct km_event c;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353
Thomas Graf35a7aa02007-08-22 14:00:40 -0700354 err = verify_newsa_info(p, attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 if (err)
356 return err;
357
Thomas Graf35a7aa02007-08-22 14:00:40 -0700358 x = xfrm_state_construct(p, attrs, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 if (!x)
360 return err;
361
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700362 xfrm_state_hold(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 if (nlh->nlmsg_type == XFRM_MSG_NEWSA)
364 err = xfrm_state_add(x);
365 else
366 err = xfrm_state_update(x);
367
Joy Lattenab5f5e82007-09-17 11:51:22 -0700368 xfrm_audit_state_add(x, err ? 0 : 1, NETLINK_CB(skb).loginuid,
369 NETLINK_CB(skb).sid);
Joy Latten161a09e2006-11-27 13:11:54 -0600370
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 if (err < 0) {
372 x->km.state = XFRM_STATE_DEAD;
Herbert Xu21380b82006-02-22 14:47:13 -0800373 __xfrm_state_put(x);
Patrick McHardy7d6dfe12005-06-18 22:45:31 -0700374 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 }
376
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700377 c.seq = nlh->nlmsg_seq;
378 c.pid = nlh->nlmsg_pid;
Herbert Xuf60f6b82005-06-18 22:44:37 -0700379 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700380
381 km_state_notify(x, &c);
Patrick McHardy7d6dfe12005-06-18 22:45:31 -0700382out:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700383 xfrm_state_put(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 return err;
385}
386
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700387static struct xfrm_state *xfrm_user_state_lookup(struct xfrm_usersa_id *p,
Thomas Graf5424f322007-08-22 14:01:33 -0700388 struct nlattr **attrs,
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700389 int *errp)
390{
391 struct xfrm_state *x = NULL;
392 int err;
393
394 if (xfrm_id_proto_match(p->proto, IPSEC_PROTO_ANY)) {
395 err = -ESRCH;
396 x = xfrm_state_lookup(&p->daddr, p->spi, p->proto, p->family);
397 } else {
398 xfrm_address_t *saddr = NULL;
399
Thomas Graf35a7aa02007-08-22 14:00:40 -0700400 verify_one_addr(attrs, XFRMA_SRCADDR, &saddr);
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700401 if (!saddr) {
402 err = -EINVAL;
403 goto out;
404 }
405
Masahide NAKAMURA9abbffe2006-11-24 20:34:51 -0800406 err = -ESRCH;
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700407 x = xfrm_state_lookup_byaddr(&p->daddr, saddr, p->proto,
408 p->family);
409 }
410
411 out:
412 if (!x && errp)
413 *errp = err;
414 return x;
415}
416
Christoph Hellwig22e70052007-01-02 15:22:30 -0800417static int xfrm_del_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -0700418 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419{
420 struct xfrm_state *x;
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700421 int err = -ESRCH;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700422 struct km_event c;
Thomas Graf7b67c852007-08-22 13:53:52 -0700423 struct xfrm_usersa_id *p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424
Thomas Graf35a7aa02007-08-22 14:00:40 -0700425 x = xfrm_user_state_lookup(p, attrs, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 if (x == NULL)
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700427 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428
David S. Miller6f68dc32006-06-08 23:58:52 -0700429 if ((err = security_xfrm_state_delete(x)) != 0)
Catherine Zhangc8c05a82006-06-08 23:39:49 -0700430 goto out;
431
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 if (xfrm_state_kern(x)) {
Catherine Zhangc8c05a82006-06-08 23:39:49 -0700433 err = -EPERM;
434 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 }
436
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700437 err = xfrm_state_delete(x);
Joy Latten161a09e2006-11-27 13:11:54 -0600438
Catherine Zhangc8c05a82006-06-08 23:39:49 -0700439 if (err < 0)
440 goto out;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700441
442 c.seq = nlh->nlmsg_seq;
443 c.pid = nlh->nlmsg_pid;
Herbert Xuf60f6b82005-06-18 22:44:37 -0700444 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700445 km_state_notify(x, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446
Catherine Zhangc8c05a82006-06-08 23:39:49 -0700447out:
Joy Lattenab5f5e82007-09-17 11:51:22 -0700448 xfrm_audit_state_delete(x, err ? 0 : 1, NETLINK_CB(skb).loginuid,
449 NETLINK_CB(skb).sid);
Catherine Zhangc8c05a82006-06-08 23:39:49 -0700450 xfrm_state_put(x);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700451 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452}
453
454static void copy_to_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p)
455{
456 memcpy(&p->id, &x->id, sizeof(p->id));
457 memcpy(&p->sel, &x->sel, sizeof(p->sel));
458 memcpy(&p->lft, &x->lft, sizeof(p->lft));
459 memcpy(&p->curlft, &x->curlft, sizeof(p->curlft));
460 memcpy(&p->stats, &x->stats, sizeof(p->stats));
David S. Miller54489c142006-10-27 15:29:47 -0700461 memcpy(&p->saddr, &x->props.saddr, sizeof(p->saddr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 p->mode = x->props.mode;
463 p->replay_window = x->props.replay_window;
464 p->reqid = x->props.reqid;
465 p->family = x->props.family;
466 p->flags = x->props.flags;
467 p->seq = x->km.seq;
468}
469
470struct xfrm_dump_info {
471 struct sk_buff *in_skb;
472 struct sk_buff *out_skb;
473 u32 nlmsg_seq;
474 u16 nlmsg_flags;
475 int start_idx;
476 int this_idx;
477};
478
Thomas Grafc0144be2007-08-22 13:55:43 -0700479static int copy_sec_ctx(struct xfrm_sec_ctx *s, struct sk_buff *skb)
480{
Thomas Grafc0144be2007-08-22 13:55:43 -0700481 struct xfrm_user_sec_ctx *uctx;
482 struct nlattr *attr;
Herbert Xu68325d32007-10-09 13:30:57 -0700483 int ctx_size = sizeof(*uctx) + s->ctx_len;
Thomas Grafc0144be2007-08-22 13:55:43 -0700484
485 attr = nla_reserve(skb, XFRMA_SEC_CTX, ctx_size);
486 if (attr == NULL)
487 return -EMSGSIZE;
488
489 uctx = nla_data(attr);
490 uctx->exttype = XFRMA_SEC_CTX;
491 uctx->len = ctx_size;
492 uctx->ctx_doi = s->ctx_doi;
493 uctx->ctx_alg = s->ctx_alg;
494 uctx->ctx_len = s->ctx_len;
495 memcpy(uctx + 1, s->ctx_str, s->ctx_len);
496
497 return 0;
498}
499
Herbert Xu68325d32007-10-09 13:30:57 -0700500/* Don't change this without updating xfrm_sa_len! */
501static int copy_to_user_state_extra(struct xfrm_state *x,
502 struct xfrm_usersa_info *p,
503 struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 copy_to_user_state(x, p);
506
Herbert Xu050f0092007-10-09 13:31:47 -0700507 if (x->coaddr)
508 NLA_PUT(skb, XFRMA_COADDR, sizeof(*x->coaddr), x->coaddr);
509
510 if (x->lastused)
511 NLA_PUT_U64(skb, XFRMA_LASTUSED, x->lastused);
Herbert Xu050f0092007-10-09 13:31:47 -0700512
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 if (x->aalg)
Eric Dumazet0f99be02008-01-08 23:39:06 -0800514 NLA_PUT(skb, XFRMA_ALG_AUTH, xfrm_alg_len(x->aalg), x->aalg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 if (x->ealg)
Eric Dumazet0f99be02008-01-08 23:39:06 -0800516 NLA_PUT(skb, XFRMA_ALG_CRYPT, xfrm_alg_len(x->ealg), x->ealg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 if (x->calg)
Thomas Grafc0144be2007-08-22 13:55:43 -0700518 NLA_PUT(skb, XFRMA_ALG_COMP, sizeof(*(x->calg)), x->calg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519
520 if (x->encap)
Thomas Grafc0144be2007-08-22 13:55:43 -0700521 NLA_PUT(skb, XFRMA_ENCAP, sizeof(*x->encap), x->encap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522
Thomas Grafc0144be2007-08-22 13:55:43 -0700523 if (x->security && copy_sec_ctx(x->security, skb) < 0)
524 goto nla_put_failure;
Noriaki TAKAMIYA060f02a2006-08-23 18:18:55 -0700525
Herbert Xu68325d32007-10-09 13:30:57 -0700526 return 0;
527
528nla_put_failure:
529 return -EMSGSIZE;
530}
531
532static int dump_one_state(struct xfrm_state *x, int count, void *ptr)
533{
534 struct xfrm_dump_info *sp = ptr;
535 struct sk_buff *in_skb = sp->in_skb;
536 struct sk_buff *skb = sp->out_skb;
537 struct xfrm_usersa_info *p;
538 struct nlmsghdr *nlh;
539 int err;
540
541 if (sp->this_idx < sp->start_idx)
542 goto out;
543
544 nlh = nlmsg_put(skb, NETLINK_CB(in_skb).pid, sp->nlmsg_seq,
545 XFRM_MSG_NEWSA, sizeof(*p), sp->nlmsg_flags);
546 if (nlh == NULL)
547 return -EMSGSIZE;
548
549 p = nlmsg_data(nlh);
550
551 err = copy_to_user_state_extra(x, p, skb);
552 if (err)
553 goto nla_put_failure;
554
Thomas Graf98250692007-08-22 12:47:26 -0700555 nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556out:
557 sp->this_idx++;
558 return 0;
559
Thomas Grafc0144be2007-08-22 13:55:43 -0700560nla_put_failure:
Thomas Graf98250692007-08-22 12:47:26 -0700561 nlmsg_cancel(skb, nlh);
Herbert Xu68325d32007-10-09 13:30:57 -0700562 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563}
564
565static int xfrm_dump_sa(struct sk_buff *skb, struct netlink_callback *cb)
566{
567 struct xfrm_dump_info info;
568
569 info.in_skb = cb->skb;
570 info.out_skb = skb;
571 info.nlmsg_seq = cb->nlh->nlmsg_seq;
572 info.nlmsg_flags = NLM_F_MULTI;
573 info.this_idx = 0;
574 info.start_idx = cb->args[0];
Masahide NAKAMURAdc00a522006-08-23 17:49:52 -0700575 (void) xfrm_state_walk(0, dump_one_state, &info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 cb->args[0] = info.this_idx;
577
578 return skb->len;
579}
580
581static struct sk_buff *xfrm_state_netlink(struct sk_buff *in_skb,
582 struct xfrm_state *x, u32 seq)
583{
584 struct xfrm_dump_info info;
585 struct sk_buff *skb;
586
Thomas Graf7deb2262007-08-22 13:57:39 -0700587 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 if (!skb)
589 return ERR_PTR(-ENOMEM);
590
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 info.in_skb = in_skb;
592 info.out_skb = skb;
593 info.nlmsg_seq = seq;
594 info.nlmsg_flags = 0;
595 info.this_idx = info.start_idx = 0;
596
597 if (dump_one_state(x, 0, &info)) {
598 kfree_skb(skb);
599 return NULL;
600 }
601
602 return skb;
603}
604
Thomas Graf7deb2262007-08-22 13:57:39 -0700605static inline size_t xfrm_spdinfo_msgsize(void)
606{
607 return NLMSG_ALIGN(4)
608 + nla_total_size(sizeof(struct xfrmu_spdinfo))
609 + nla_total_size(sizeof(struct xfrmu_spdhinfo));
610}
611
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700612static int build_spdinfo(struct sk_buff *skb, u32 pid, u32 seq, u32 flags)
613{
Jamal Hadi Salim5a6d3412007-05-04 12:55:39 -0700614 struct xfrmk_spdinfo si;
615 struct xfrmu_spdinfo spc;
616 struct xfrmu_spdhinfo sph;
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700617 struct nlmsghdr *nlh;
618 u32 *f;
619
620 nlh = nlmsg_put(skb, pid, seq, XFRM_MSG_NEWSPDINFO, sizeof(u32), 0);
621 if (nlh == NULL) /* shouldnt really happen ... */
622 return -EMSGSIZE;
623
624 f = nlmsg_data(nlh);
625 *f = flags;
626 xfrm_spd_getinfo(&si);
Jamal Hadi Salim5a6d3412007-05-04 12:55:39 -0700627 spc.incnt = si.incnt;
628 spc.outcnt = si.outcnt;
629 spc.fwdcnt = si.fwdcnt;
630 spc.inscnt = si.inscnt;
631 spc.outscnt = si.outscnt;
632 spc.fwdscnt = si.fwdscnt;
633 sph.spdhcnt = si.spdhcnt;
634 sph.spdhmcnt = si.spdhmcnt;
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700635
Jamal Hadi Salim5a6d3412007-05-04 12:55:39 -0700636 NLA_PUT(skb, XFRMA_SPD_INFO, sizeof(spc), &spc);
637 NLA_PUT(skb, XFRMA_SPD_HINFO, sizeof(sph), &sph);
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700638
639 return nlmsg_end(skb, nlh);
640
641nla_put_failure:
642 nlmsg_cancel(skb, nlh);
643 return -EMSGSIZE;
644}
645
646static int xfrm_get_spdinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -0700647 struct nlattr **attrs)
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700648{
649 struct sk_buff *r_skb;
Thomas Graf7b67c852007-08-22 13:53:52 -0700650 u32 *flags = nlmsg_data(nlh);
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700651 u32 spid = NETLINK_CB(skb).pid;
652 u32 seq = nlh->nlmsg_seq;
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700653
Thomas Graf7deb2262007-08-22 13:57:39 -0700654 r_skb = nlmsg_new(xfrm_spdinfo_msgsize(), GFP_ATOMIC);
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700655 if (r_skb == NULL)
656 return -ENOMEM;
657
658 if (build_spdinfo(r_skb, spid, seq, *flags) < 0)
659 BUG();
660
661 return nlmsg_unicast(xfrm_nl, r_skb, spid);
662}
663
Thomas Graf7deb2262007-08-22 13:57:39 -0700664static inline size_t xfrm_sadinfo_msgsize(void)
665{
666 return NLMSG_ALIGN(4)
667 + nla_total_size(sizeof(struct xfrmu_sadhinfo))
668 + nla_total_size(4); /* XFRMA_SAD_CNT */
669}
670
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700671static int build_sadinfo(struct sk_buff *skb, u32 pid, u32 seq, u32 flags)
672{
Jamal Hadi Salimaf11e312007-05-04 12:55:13 -0700673 struct xfrmk_sadinfo si;
674 struct xfrmu_sadhinfo sh;
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700675 struct nlmsghdr *nlh;
676 u32 *f;
677
678 nlh = nlmsg_put(skb, pid, seq, XFRM_MSG_NEWSADINFO, sizeof(u32), 0);
679 if (nlh == NULL) /* shouldnt really happen ... */
680 return -EMSGSIZE;
681
682 f = nlmsg_data(nlh);
683 *f = flags;
684 xfrm_sad_getinfo(&si);
685
Jamal Hadi Salimaf11e312007-05-04 12:55:13 -0700686 sh.sadhmcnt = si.sadhmcnt;
687 sh.sadhcnt = si.sadhcnt;
688
689 NLA_PUT_U32(skb, XFRMA_SAD_CNT, si.sadcnt);
690 NLA_PUT(skb, XFRMA_SAD_HINFO, sizeof(sh), &sh);
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700691
692 return nlmsg_end(skb, nlh);
693
694nla_put_failure:
695 nlmsg_cancel(skb, nlh);
696 return -EMSGSIZE;
697}
698
699static int xfrm_get_sadinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -0700700 struct nlattr **attrs)
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700701{
702 struct sk_buff *r_skb;
Thomas Graf7b67c852007-08-22 13:53:52 -0700703 u32 *flags = nlmsg_data(nlh);
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700704 u32 spid = NETLINK_CB(skb).pid;
705 u32 seq = nlh->nlmsg_seq;
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700706
Thomas Graf7deb2262007-08-22 13:57:39 -0700707 r_skb = nlmsg_new(xfrm_sadinfo_msgsize(), GFP_ATOMIC);
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700708 if (r_skb == NULL)
709 return -ENOMEM;
710
711 if (build_sadinfo(r_skb, spid, seq, *flags) < 0)
712 BUG();
713
714 return nlmsg_unicast(xfrm_nl, r_skb, spid);
715}
716
Christoph Hellwig22e70052007-01-02 15:22:30 -0800717static int xfrm_get_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -0700718 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719{
Thomas Graf7b67c852007-08-22 13:53:52 -0700720 struct xfrm_usersa_id *p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 struct xfrm_state *x;
722 struct sk_buff *resp_skb;
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700723 int err = -ESRCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724
Thomas Graf35a7aa02007-08-22 14:00:40 -0700725 x = xfrm_user_state_lookup(p, attrs, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 if (x == NULL)
727 goto out_noput;
728
729 resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
730 if (IS_ERR(resp_skb)) {
731 err = PTR_ERR(resp_skb);
732 } else {
Thomas Graf082a1ad2007-08-22 13:54:36 -0700733 err = nlmsg_unicast(xfrm_nl, resp_skb, NETLINK_CB(skb).pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 }
735 xfrm_state_put(x);
736out_noput:
737 return err;
738}
739
740static int verify_userspi_info(struct xfrm_userspi_info *p)
741{
742 switch (p->info.id.proto) {
743 case IPPROTO_AH:
744 case IPPROTO_ESP:
745 break;
746
747 case IPPROTO_COMP:
748 /* IPCOMP spi is 16-bits. */
749 if (p->max >= 0x10000)
750 return -EINVAL;
751 break;
752
753 default:
754 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700755 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756
757 if (p->min > p->max)
758 return -EINVAL;
759
760 return 0;
761}
762
Christoph Hellwig22e70052007-01-02 15:22:30 -0800763static int xfrm_alloc_userspi(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -0700764 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765{
766 struct xfrm_state *x;
767 struct xfrm_userspi_info *p;
768 struct sk_buff *resp_skb;
769 xfrm_address_t *daddr;
770 int family;
771 int err;
772
Thomas Graf7b67c852007-08-22 13:53:52 -0700773 p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 err = verify_userspi_info(p);
775 if (err)
776 goto out_noput;
777
778 family = p->info.family;
779 daddr = &p->info.id.daddr;
780
781 x = NULL;
782 if (p->info.seq) {
783 x = xfrm_find_acq_byseq(p->info.seq);
784 if (x && xfrm_addr_cmp(&x->id.daddr, daddr, family)) {
785 xfrm_state_put(x);
786 x = NULL;
787 }
788 }
789
790 if (!x)
791 x = xfrm_find_acq(p->info.mode, p->info.reqid,
792 p->info.id.proto, daddr,
793 &p->info.saddr, 1,
794 family);
795 err = -ENOENT;
796 if (x == NULL)
797 goto out_noput;
798
Herbert Xu658b2192007-10-09 13:29:52 -0700799 err = xfrm_alloc_spi(x, p->min, p->max);
800 if (err)
801 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802
Herbert Xu658b2192007-10-09 13:29:52 -0700803 resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 if (IS_ERR(resp_skb)) {
805 err = PTR_ERR(resp_skb);
806 goto out;
807 }
808
Thomas Graf082a1ad2007-08-22 13:54:36 -0700809 err = nlmsg_unicast(xfrm_nl, resp_skb, NETLINK_CB(skb).pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810
811out:
812 xfrm_state_put(x);
813out_noput:
814 return err;
815}
816
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -0800817static int verify_policy_dir(u8 dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818{
819 switch (dir) {
820 case XFRM_POLICY_IN:
821 case XFRM_POLICY_OUT:
822 case XFRM_POLICY_FWD:
823 break;
824
825 default:
826 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700827 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828
829 return 0;
830}
831
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -0800832static int verify_policy_type(u8 type)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -0700833{
834 switch (type) {
835 case XFRM_POLICY_TYPE_MAIN:
836#ifdef CONFIG_XFRM_SUB_POLICY
837 case XFRM_POLICY_TYPE_SUB:
838#endif
839 break;
840
841 default:
842 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700843 }
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -0700844
845 return 0;
846}
847
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848static int verify_newpolicy_info(struct xfrm_userpolicy_info *p)
849{
850 switch (p->share) {
851 case XFRM_SHARE_ANY:
852 case XFRM_SHARE_SESSION:
853 case XFRM_SHARE_USER:
854 case XFRM_SHARE_UNIQUE:
855 break;
856
857 default:
858 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700859 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860
861 switch (p->action) {
862 case XFRM_POLICY_ALLOW:
863 case XFRM_POLICY_BLOCK:
864 break;
865
866 default:
867 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700868 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869
870 switch (p->sel.family) {
871 case AF_INET:
872 break;
873
874 case AF_INET6:
875#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
876 break;
877#else
878 return -EAFNOSUPPORT;
879#endif
880
881 default:
882 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700883 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884
885 return verify_policy_dir(p->dir);
886}
887
Thomas Graf5424f322007-08-22 14:01:33 -0700888static int copy_from_user_sec_ctx(struct xfrm_policy *pol, struct nlattr **attrs)
Trent Jaegerdf718372005-12-13 23:12:27 -0800889{
Thomas Graf5424f322007-08-22 14:01:33 -0700890 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
Trent Jaegerdf718372005-12-13 23:12:27 -0800891 struct xfrm_user_sec_ctx *uctx;
892
893 if (!rt)
894 return 0;
895
Thomas Graf5424f322007-08-22 14:01:33 -0700896 uctx = nla_data(rt);
Trent Jaegerdf718372005-12-13 23:12:27 -0800897 return security_xfrm_policy_alloc(pol, uctx);
898}
899
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900static void copy_templates(struct xfrm_policy *xp, struct xfrm_user_tmpl *ut,
901 int nr)
902{
903 int i;
904
905 xp->xfrm_nr = nr;
906 for (i = 0; i < nr; i++, ut++) {
907 struct xfrm_tmpl *t = &xp->xfrm_vec[i];
908
909 memcpy(&t->id, &ut->id, sizeof(struct xfrm_id));
910 memcpy(&t->saddr, &ut->saddr,
911 sizeof(xfrm_address_t));
912 t->reqid = ut->reqid;
913 t->mode = ut->mode;
914 t->share = ut->share;
915 t->optional = ut->optional;
916 t->aalgos = ut->aalgos;
917 t->ealgos = ut->ealgos;
918 t->calgos = ut->calgos;
Miika Komu8511d012006-11-30 16:40:51 -0800919 t->encap_family = ut->family;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 }
921}
922
David S. Millerb4ad86bf2006-12-03 19:19:26 -0800923static int validate_tmpl(int nr, struct xfrm_user_tmpl *ut, u16 family)
924{
925 int i;
926
927 if (nr > XFRM_MAX_DEPTH)
928 return -EINVAL;
929
930 for (i = 0; i < nr; i++) {
931 /* We never validated the ut->family value, so many
932 * applications simply leave it at zero. The check was
933 * never made and ut->family was ignored because all
934 * templates could be assumed to have the same family as
935 * the policy itself. Now that we will have ipv4-in-ipv6
936 * and ipv6-in-ipv4 tunnels, this is no longer true.
937 */
938 if (!ut[i].family)
939 ut[i].family = family;
940
941 switch (ut[i].family) {
942 case AF_INET:
943 break;
944#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
945 case AF_INET6:
946 break;
947#endif
948 default:
949 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700950 }
David S. Millerb4ad86bf2006-12-03 19:19:26 -0800951 }
952
953 return 0;
954}
955
Thomas Graf5424f322007-08-22 14:01:33 -0700956static int copy_from_user_tmpl(struct xfrm_policy *pol, struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957{
Thomas Graf5424f322007-08-22 14:01:33 -0700958 struct nlattr *rt = attrs[XFRMA_TMPL];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959
960 if (!rt) {
961 pol->xfrm_nr = 0;
962 } else {
Thomas Graf5424f322007-08-22 14:01:33 -0700963 struct xfrm_user_tmpl *utmpl = nla_data(rt);
964 int nr = nla_len(rt) / sizeof(*utmpl);
David S. Millerb4ad86bf2006-12-03 19:19:26 -0800965 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966
David S. Millerb4ad86bf2006-12-03 19:19:26 -0800967 err = validate_tmpl(nr, utmpl, pol->family);
968 if (err)
969 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970
Thomas Graf5424f322007-08-22 14:01:33 -0700971 copy_templates(pol, utmpl, nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 }
973 return 0;
974}
975
Thomas Graf5424f322007-08-22 14:01:33 -0700976static int copy_from_user_policy_type(u8 *tp, struct nlattr **attrs)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -0700977{
Thomas Graf5424f322007-08-22 14:01:33 -0700978 struct nlattr *rt = attrs[XFRMA_POLICY_TYPE];
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -0700979 struct xfrm_userpolicy_type *upt;
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -0800980 u8 type = XFRM_POLICY_TYPE_MAIN;
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -0700981 int err;
982
983 if (rt) {
Thomas Graf5424f322007-08-22 14:01:33 -0700984 upt = nla_data(rt);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -0700985 type = upt->type;
986 }
987
988 err = verify_policy_type(type);
989 if (err)
990 return err;
991
992 *tp = type;
993 return 0;
994}
995
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996static void copy_from_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p)
997{
998 xp->priority = p->priority;
999 xp->index = p->index;
1000 memcpy(&xp->selector, &p->sel, sizeof(xp->selector));
1001 memcpy(&xp->lft, &p->lft, sizeof(xp->lft));
1002 xp->action = p->action;
1003 xp->flags = p->flags;
1004 xp->family = p->sel.family;
1005 /* XXX xp->share = p->share; */
1006}
1007
1008static void copy_to_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p, int dir)
1009{
1010 memcpy(&p->sel, &xp->selector, sizeof(p->sel));
1011 memcpy(&p->lft, &xp->lft, sizeof(p->lft));
1012 memcpy(&p->curlft, &xp->curlft, sizeof(p->curlft));
1013 p->priority = xp->priority;
1014 p->index = xp->index;
1015 p->sel.family = xp->family;
1016 p->dir = dir;
1017 p->action = xp->action;
1018 p->flags = xp->flags;
1019 p->share = XFRM_SHARE_ANY; /* XXX xp->share */
1020}
1021
Thomas Graf5424f322007-08-22 14:01:33 -07001022static struct xfrm_policy *xfrm_policy_construct(struct xfrm_userpolicy_info *p, struct nlattr **attrs, int *errp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023{
1024 struct xfrm_policy *xp = xfrm_policy_alloc(GFP_KERNEL);
1025 int err;
1026
1027 if (!xp) {
1028 *errp = -ENOMEM;
1029 return NULL;
1030 }
1031
1032 copy_from_user_policy(xp, p);
Trent Jaegerdf718372005-12-13 23:12:27 -08001033
Thomas Graf35a7aa02007-08-22 14:00:40 -07001034 err = copy_from_user_policy_type(&xp->type, attrs);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001035 if (err)
1036 goto error;
1037
Thomas Graf35a7aa02007-08-22 14:00:40 -07001038 if (!(err = copy_from_user_tmpl(xp, attrs)))
1039 err = copy_from_user_sec_ctx(xp, attrs);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001040 if (err)
1041 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042
1043 return xp;
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001044 error:
1045 *errp = err;
1046 kfree(xp);
1047 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048}
1049
Christoph Hellwig22e70052007-01-02 15:22:30 -08001050static int xfrm_add_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001051 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052{
Thomas Graf7b67c852007-08-22 13:53:52 -07001053 struct xfrm_userpolicy_info *p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 struct xfrm_policy *xp;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001055 struct km_event c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 int err;
1057 int excl;
1058
1059 err = verify_newpolicy_info(p);
1060 if (err)
1061 return err;
Thomas Graf35a7aa02007-08-22 14:00:40 -07001062 err = verify_sec_ctx_len(attrs);
Trent Jaegerdf718372005-12-13 23:12:27 -08001063 if (err)
1064 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065
Thomas Graf35a7aa02007-08-22 14:00:40 -07001066 xp = xfrm_policy_construct(p, attrs, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 if (!xp)
1068 return err;
1069
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001070 /* shouldnt excl be based on nlh flags??
1071 * Aha! this is anti-netlink really i.e more pfkey derived
1072 * in netlink excl is a flag and you wouldnt need
1073 * a type XFRM_MSG_UPDPOLICY - JHS */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 excl = nlh->nlmsg_type == XFRM_MSG_NEWPOLICY;
1075 err = xfrm_policy_insert(p->dir, xp, excl);
Joy Lattenab5f5e82007-09-17 11:51:22 -07001076 xfrm_audit_policy_add(xp, err ? 0 : 1, NETLINK_CB(skb).loginuid,
1077 NETLINK_CB(skb).sid);
Joy Latten161a09e2006-11-27 13:11:54 -06001078
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 if (err) {
Trent Jaeger5f8ac642006-01-06 13:22:39 -08001080 security_xfrm_policy_free(xp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 kfree(xp);
1082 return err;
1083 }
1084
Herbert Xuf60f6b82005-06-18 22:44:37 -07001085 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001086 c.seq = nlh->nlmsg_seq;
1087 c.pid = nlh->nlmsg_pid;
1088 km_policy_notify(xp, p->dir, &c);
1089
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 xfrm_pol_put(xp);
1091
1092 return 0;
1093}
1094
1095static int copy_to_user_tmpl(struct xfrm_policy *xp, struct sk_buff *skb)
1096{
1097 struct xfrm_user_tmpl vec[XFRM_MAX_DEPTH];
1098 int i;
1099
1100 if (xp->xfrm_nr == 0)
1101 return 0;
1102
1103 for (i = 0; i < xp->xfrm_nr; i++) {
1104 struct xfrm_user_tmpl *up = &vec[i];
1105 struct xfrm_tmpl *kp = &xp->xfrm_vec[i];
1106
1107 memcpy(&up->id, &kp->id, sizeof(up->id));
Miika Komu8511d012006-11-30 16:40:51 -08001108 up->family = kp->encap_family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 memcpy(&up->saddr, &kp->saddr, sizeof(up->saddr));
1110 up->reqid = kp->reqid;
1111 up->mode = kp->mode;
1112 up->share = kp->share;
1113 up->optional = kp->optional;
1114 up->aalgos = kp->aalgos;
1115 up->ealgos = kp->ealgos;
1116 up->calgos = kp->calgos;
1117 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118
Thomas Grafc0144be2007-08-22 13:55:43 -07001119 return nla_put(skb, XFRMA_TMPL,
1120 sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr, vec);
Trent Jaegerdf718372005-12-13 23:12:27 -08001121}
1122
Serge Hallyn0d681622006-07-24 23:30:44 -07001123static inline int copy_to_user_state_sec_ctx(struct xfrm_state *x, struct sk_buff *skb)
1124{
1125 if (x->security) {
1126 return copy_sec_ctx(x->security, skb);
1127 }
1128 return 0;
1129}
1130
1131static inline int copy_to_user_sec_ctx(struct xfrm_policy *xp, struct sk_buff *skb)
1132{
1133 if (xp->security) {
1134 return copy_sec_ctx(xp->security, skb);
1135 }
1136 return 0;
1137}
Thomas Grafcfbfd452007-08-22 13:57:04 -07001138static inline size_t userpolicy_type_attrsize(void)
1139{
1140#ifdef CONFIG_XFRM_SUB_POLICY
1141 return nla_total_size(sizeof(struct xfrm_userpolicy_type));
1142#else
1143 return 0;
1144#endif
1145}
Serge Hallyn0d681622006-07-24 23:30:44 -07001146
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001147#ifdef CONFIG_XFRM_SUB_POLICY
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001148static int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001149{
Thomas Grafc0144be2007-08-22 13:55:43 -07001150 struct xfrm_userpolicy_type upt = {
1151 .type = type,
1152 };
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001153
Thomas Grafc0144be2007-08-22 13:55:43 -07001154 return nla_put(skb, XFRMA_POLICY_TYPE, sizeof(upt), &upt);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001155}
1156
1157#else
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001158static inline int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001159{
1160 return 0;
1161}
1162#endif
1163
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164static int dump_one_policy(struct xfrm_policy *xp, int dir, int count, void *ptr)
1165{
1166 struct xfrm_dump_info *sp = ptr;
1167 struct xfrm_userpolicy_info *p;
1168 struct sk_buff *in_skb = sp->in_skb;
1169 struct sk_buff *skb = sp->out_skb;
1170 struct nlmsghdr *nlh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171
1172 if (sp->this_idx < sp->start_idx)
1173 goto out;
1174
Thomas Graf79b8b7f2007-08-22 12:46:53 -07001175 nlh = nlmsg_put(skb, NETLINK_CB(in_skb).pid, sp->nlmsg_seq,
1176 XFRM_MSG_NEWPOLICY, sizeof(*p), sp->nlmsg_flags);
1177 if (nlh == NULL)
1178 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179
Thomas Graf7b67c852007-08-22 13:53:52 -07001180 p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 copy_to_user_policy(xp, p, dir);
1182 if (copy_to_user_tmpl(xp, skb) < 0)
1183 goto nlmsg_failure;
Trent Jaegerdf718372005-12-13 23:12:27 -08001184 if (copy_to_user_sec_ctx(xp, skb))
1185 goto nlmsg_failure;
Jamal Hadi Salim1459bb32006-11-20 16:51:22 -08001186 if (copy_to_user_policy_type(xp->type, skb) < 0)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001187 goto nlmsg_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188
Thomas Graf98250692007-08-22 12:47:26 -07001189 nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190out:
1191 sp->this_idx++;
1192 return 0;
1193
1194nlmsg_failure:
Thomas Graf98250692007-08-22 12:47:26 -07001195 nlmsg_cancel(skb, nlh);
1196 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197}
1198
1199static int xfrm_dump_policy(struct sk_buff *skb, struct netlink_callback *cb)
1200{
1201 struct xfrm_dump_info info;
1202
1203 info.in_skb = cb->skb;
1204 info.out_skb = skb;
1205 info.nlmsg_seq = cb->nlh->nlmsg_seq;
1206 info.nlmsg_flags = NLM_F_MULTI;
1207 info.this_idx = 0;
1208 info.start_idx = cb->args[0];
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001209 (void) xfrm_policy_walk(XFRM_POLICY_TYPE_MAIN, dump_one_policy, &info);
1210#ifdef CONFIG_XFRM_SUB_POLICY
1211 (void) xfrm_policy_walk(XFRM_POLICY_TYPE_SUB, dump_one_policy, &info);
1212#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213 cb->args[0] = info.this_idx;
1214
1215 return skb->len;
1216}
1217
1218static struct sk_buff *xfrm_policy_netlink(struct sk_buff *in_skb,
1219 struct xfrm_policy *xp,
1220 int dir, u32 seq)
1221{
1222 struct xfrm_dump_info info;
1223 struct sk_buff *skb;
1224
Thomas Graf7deb2262007-08-22 13:57:39 -07001225 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 if (!skb)
1227 return ERR_PTR(-ENOMEM);
1228
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 info.in_skb = in_skb;
1230 info.out_skb = skb;
1231 info.nlmsg_seq = seq;
1232 info.nlmsg_flags = 0;
1233 info.this_idx = info.start_idx = 0;
1234
1235 if (dump_one_policy(xp, dir, 0, &info) < 0) {
1236 kfree_skb(skb);
1237 return NULL;
1238 }
1239
1240 return skb;
1241}
1242
Christoph Hellwig22e70052007-01-02 15:22:30 -08001243static int xfrm_get_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001244 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245{
1246 struct xfrm_policy *xp;
1247 struct xfrm_userpolicy_id *p;
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001248 u8 type = XFRM_POLICY_TYPE_MAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 int err;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001250 struct km_event c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 int delete;
1252
Thomas Graf7b67c852007-08-22 13:53:52 -07001253 p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 delete = nlh->nlmsg_type == XFRM_MSG_DELPOLICY;
1255
Thomas Graf35a7aa02007-08-22 14:00:40 -07001256 err = copy_from_user_policy_type(&type, attrs);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001257 if (err)
1258 return err;
1259
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 err = verify_policy_dir(p->dir);
1261 if (err)
1262 return err;
1263
1264 if (p->index)
Eric Parisef41aaa2007-03-07 15:37:58 -08001265 xp = xfrm_policy_byid(type, p->dir, p->index, delete, &err);
Trent Jaegerdf718372005-12-13 23:12:27 -08001266 else {
Thomas Graf5424f322007-08-22 14:01:33 -07001267 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
Trent Jaegerdf718372005-12-13 23:12:27 -08001268 struct xfrm_policy tmp;
1269
Thomas Graf35a7aa02007-08-22 14:00:40 -07001270 err = verify_sec_ctx_len(attrs);
Trent Jaegerdf718372005-12-13 23:12:27 -08001271 if (err)
1272 return err;
1273
1274 memset(&tmp, 0, sizeof(struct xfrm_policy));
1275 if (rt) {
Thomas Graf5424f322007-08-22 14:01:33 -07001276 struct xfrm_user_sec_ctx *uctx = nla_data(rt);
Trent Jaegerdf718372005-12-13 23:12:27 -08001277
1278 if ((err = security_xfrm_policy_alloc(&tmp, uctx)))
1279 return err;
1280 }
Eric Parisef41aaa2007-03-07 15:37:58 -08001281 xp = xfrm_policy_bysel_ctx(type, p->dir, &p->sel, tmp.security,
1282 delete, &err);
Trent Jaegerdf718372005-12-13 23:12:27 -08001283 security_xfrm_policy_free(&tmp);
1284 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 if (xp == NULL)
1286 return -ENOENT;
1287
1288 if (!delete) {
1289 struct sk_buff *resp_skb;
1290
1291 resp_skb = xfrm_policy_netlink(skb, xp, p->dir, nlh->nlmsg_seq);
1292 if (IS_ERR(resp_skb)) {
1293 err = PTR_ERR(resp_skb);
1294 } else {
Thomas Graf082a1ad2007-08-22 13:54:36 -07001295 err = nlmsg_unicast(xfrm_nl, resp_skb,
1296 NETLINK_CB(skb).pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 }
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001298 } else {
Joy Lattenab5f5e82007-09-17 11:51:22 -07001299 xfrm_audit_policy_delete(xp, err ? 0 : 1,
1300 NETLINK_CB(skb).loginuid,
1301 NETLINK_CB(skb).sid);
David S. Miller13fcfbb02007-02-12 13:53:54 -08001302
1303 if (err != 0)
Catherine Zhangc8c05a82006-06-08 23:39:49 -07001304 goto out;
David S. Miller13fcfbb02007-02-12 13:53:54 -08001305
Herbert Xue7443892005-06-18 22:44:18 -07001306 c.data.byid = p->index;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001307 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001308 c.seq = nlh->nlmsg_seq;
1309 c.pid = nlh->nlmsg_pid;
1310 km_policy_notify(xp, p->dir, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311 }
1312
Catherine Zhangc8c05a82006-06-08 23:39:49 -07001313out:
Eric Parisef41aaa2007-03-07 15:37:58 -08001314 xfrm_pol_put(xp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315 return err;
1316}
1317
Christoph Hellwig22e70052007-01-02 15:22:30 -08001318static int xfrm_flush_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001319 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320{
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001321 struct km_event c;
Thomas Graf7b67c852007-08-22 13:53:52 -07001322 struct xfrm_usersa_flush *p = nlmsg_data(nlh);
Joy Latten161a09e2006-11-27 13:11:54 -06001323 struct xfrm_audit audit_info;
Joy Latten4aa2e622007-06-04 19:05:57 -04001324 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325
Joy Latten161a09e2006-11-27 13:11:54 -06001326 audit_info.loginuid = NETLINK_CB(skb).loginuid;
1327 audit_info.secid = NETLINK_CB(skb).sid;
Joy Latten4aa2e622007-06-04 19:05:57 -04001328 err = xfrm_state_flush(p->proto, &audit_info);
1329 if (err)
1330 return err;
Herbert Xubf088672005-06-18 22:44:00 -07001331 c.data.proto = p->proto;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001332 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001333 c.seq = nlh->nlmsg_seq;
1334 c.pid = nlh->nlmsg_pid;
1335 km_state_notify(NULL, &c);
1336
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 return 0;
1338}
1339
Thomas Graf7deb2262007-08-22 13:57:39 -07001340static inline size_t xfrm_aevent_msgsize(void)
1341{
1342 return NLMSG_ALIGN(sizeof(struct xfrm_aevent_id))
1343 + nla_total_size(sizeof(struct xfrm_replay_state))
1344 + nla_total_size(sizeof(struct xfrm_lifetime_cur))
1345 + nla_total_size(4) /* XFRM_AE_RTHR */
1346 + nla_total_size(4); /* XFRM_AE_ETHR */
1347}
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001348
1349static int build_aevent(struct sk_buff *skb, struct xfrm_state *x, struct km_event *c)
1350{
1351 struct xfrm_aevent_id *id;
1352 struct nlmsghdr *nlh;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001353
Thomas Graf79b8b7f2007-08-22 12:46:53 -07001354 nlh = nlmsg_put(skb, c->pid, c->seq, XFRM_MSG_NEWAE, sizeof(*id), 0);
1355 if (nlh == NULL)
1356 return -EMSGSIZE;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001357
Thomas Graf7b67c852007-08-22 13:53:52 -07001358 id = nlmsg_data(nlh);
Jamal Hadi Salim2b5f6dc2006-12-02 22:22:25 -08001359 memcpy(&id->sa_id.daddr, &x->id.daddr,sizeof(x->id.daddr));
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001360 id->sa_id.spi = x->id.spi;
1361 id->sa_id.family = x->props.family;
1362 id->sa_id.proto = x->id.proto;
Jamal Hadi Salim2b5f6dc2006-12-02 22:22:25 -08001363 memcpy(&id->saddr, &x->props.saddr,sizeof(x->props.saddr));
1364 id->reqid = x->props.reqid;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001365 id->flags = c->data.aevent;
1366
Thomas Grafc0144be2007-08-22 13:55:43 -07001367 NLA_PUT(skb, XFRMA_REPLAY_VAL, sizeof(x->replay), &x->replay);
1368 NLA_PUT(skb, XFRMA_LTIME_VAL, sizeof(x->curlft), &x->curlft);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001369
Thomas Grafc0144be2007-08-22 13:55:43 -07001370 if (id->flags & XFRM_AE_RTHR)
1371 NLA_PUT_U32(skb, XFRMA_REPLAY_THRESH, x->replay_maxdiff);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001372
Thomas Grafc0144be2007-08-22 13:55:43 -07001373 if (id->flags & XFRM_AE_ETHR)
1374 NLA_PUT_U32(skb, XFRMA_ETIMER_THRESH,
1375 x->replay_maxage * 10 / HZ);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001376
Thomas Graf98250692007-08-22 12:47:26 -07001377 return nlmsg_end(skb, nlh);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001378
Thomas Grafc0144be2007-08-22 13:55:43 -07001379nla_put_failure:
Thomas Graf98250692007-08-22 12:47:26 -07001380 nlmsg_cancel(skb, nlh);
1381 return -EMSGSIZE;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001382}
1383
Christoph Hellwig22e70052007-01-02 15:22:30 -08001384static int xfrm_get_ae(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001385 struct nlattr **attrs)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001386{
1387 struct xfrm_state *x;
1388 struct sk_buff *r_skb;
1389 int err;
1390 struct km_event c;
Thomas Graf7b67c852007-08-22 13:53:52 -07001391 struct xfrm_aevent_id *p = nlmsg_data(nlh);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001392 struct xfrm_usersa_id *id = &p->sa_id;
1393
Thomas Graf7deb2262007-08-22 13:57:39 -07001394 r_skb = nlmsg_new(xfrm_aevent_msgsize(), GFP_ATOMIC);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001395 if (r_skb == NULL)
1396 return -ENOMEM;
1397
1398 x = xfrm_state_lookup(&id->daddr, id->spi, id->proto, id->family);
1399 if (x == NULL) {
Patrick McHardyb08d5842007-02-27 09:57:37 -08001400 kfree_skb(r_skb);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001401 return -ESRCH;
1402 }
1403
1404 /*
1405 * XXX: is this lock really needed - none of the other
1406 * gets lock (the concern is things getting updated
1407 * while we are still reading) - jhs
1408 */
1409 spin_lock_bh(&x->lock);
1410 c.data.aevent = p->flags;
1411 c.seq = nlh->nlmsg_seq;
1412 c.pid = nlh->nlmsg_pid;
1413
1414 if (build_aevent(r_skb, x, &c) < 0)
1415 BUG();
Thomas Graf082a1ad2007-08-22 13:54:36 -07001416 err = nlmsg_unicast(xfrm_nl, r_skb, NETLINK_CB(skb).pid);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001417 spin_unlock_bh(&x->lock);
1418 xfrm_state_put(x);
1419 return err;
1420}
1421
Christoph Hellwig22e70052007-01-02 15:22:30 -08001422static int xfrm_new_ae(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001423 struct nlattr **attrs)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001424{
1425 struct xfrm_state *x;
1426 struct km_event c;
1427 int err = - EINVAL;
Thomas Graf7b67c852007-08-22 13:53:52 -07001428 struct xfrm_aevent_id *p = nlmsg_data(nlh);
Thomas Graf5424f322007-08-22 14:01:33 -07001429 struct nlattr *rp = attrs[XFRMA_REPLAY_VAL];
1430 struct nlattr *lt = attrs[XFRMA_LTIME_VAL];
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001431
1432 if (!lt && !rp)
1433 return err;
1434
1435 /* pedantic mode - thou shalt sayeth replaceth */
1436 if (!(nlh->nlmsg_flags&NLM_F_REPLACE))
1437 return err;
1438
1439 x = xfrm_state_lookup(&p->sa_id.daddr, p->sa_id.spi, p->sa_id.proto, p->sa_id.family);
1440 if (x == NULL)
1441 return -ESRCH;
1442
1443 if (x->km.state != XFRM_STATE_VALID)
1444 goto out;
1445
1446 spin_lock_bh(&x->lock);
Thomas Graf35a7aa02007-08-22 14:00:40 -07001447 xfrm_update_ae_params(x, attrs);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001448 spin_unlock_bh(&x->lock);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001449
1450 c.event = nlh->nlmsg_type;
1451 c.seq = nlh->nlmsg_seq;
1452 c.pid = nlh->nlmsg_pid;
1453 c.data.aevent = XFRM_AE_CU;
1454 km_state_notify(x, &c);
1455 err = 0;
1456out:
1457 xfrm_state_put(x);
1458 return err;
1459}
1460
Christoph Hellwig22e70052007-01-02 15:22:30 -08001461static int xfrm_flush_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001462 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463{
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001464 struct km_event c;
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001465 u8 type = XFRM_POLICY_TYPE_MAIN;
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001466 int err;
Joy Latten161a09e2006-11-27 13:11:54 -06001467 struct xfrm_audit audit_info;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001468
Thomas Graf35a7aa02007-08-22 14:00:40 -07001469 err = copy_from_user_policy_type(&type, attrs);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001470 if (err)
1471 return err;
1472
Joy Latten161a09e2006-11-27 13:11:54 -06001473 audit_info.loginuid = NETLINK_CB(skb).loginuid;
1474 audit_info.secid = NETLINK_CB(skb).sid;
Joy Latten4aa2e622007-06-04 19:05:57 -04001475 err = xfrm_policy_flush(type, &audit_info);
1476 if (err)
1477 return err;
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001478 c.data.type = type;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001479 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001480 c.seq = nlh->nlmsg_seq;
1481 c.pid = nlh->nlmsg_pid;
1482 km_policy_notify(NULL, 0, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483 return 0;
1484}
1485
Christoph Hellwig22e70052007-01-02 15:22:30 -08001486static int xfrm_add_pol_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001487 struct nlattr **attrs)
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001488{
1489 struct xfrm_policy *xp;
Thomas Graf7b67c852007-08-22 13:53:52 -07001490 struct xfrm_user_polexpire *up = nlmsg_data(nlh);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001491 struct xfrm_userpolicy_info *p = &up->pol;
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001492 u8 type = XFRM_POLICY_TYPE_MAIN;
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001493 int err = -ENOENT;
1494
Thomas Graf35a7aa02007-08-22 14:00:40 -07001495 err = copy_from_user_policy_type(&type, attrs);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001496 if (err)
1497 return err;
1498
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001499 if (p->index)
Eric Parisef41aaa2007-03-07 15:37:58 -08001500 xp = xfrm_policy_byid(type, p->dir, p->index, 0, &err);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001501 else {
Thomas Graf5424f322007-08-22 14:01:33 -07001502 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001503 struct xfrm_policy tmp;
1504
Thomas Graf35a7aa02007-08-22 14:00:40 -07001505 err = verify_sec_ctx_len(attrs);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001506 if (err)
1507 return err;
1508
1509 memset(&tmp, 0, sizeof(struct xfrm_policy));
1510 if (rt) {
Thomas Graf5424f322007-08-22 14:01:33 -07001511 struct xfrm_user_sec_ctx *uctx = nla_data(rt);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001512
1513 if ((err = security_xfrm_policy_alloc(&tmp, uctx)))
1514 return err;
1515 }
Eric Parisef41aaa2007-03-07 15:37:58 -08001516 xp = xfrm_policy_bysel_ctx(type, p->dir, &p->sel, tmp.security,
1517 0, &err);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001518 security_xfrm_policy_free(&tmp);
1519 }
1520
1521 if (xp == NULL)
Eric Parisef41aaa2007-03-07 15:37:58 -08001522 return -ENOENT;
1523 read_lock(&xp->lock);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001524 if (xp->dead) {
1525 read_unlock(&xp->lock);
1526 goto out;
1527 }
1528
1529 read_unlock(&xp->lock);
1530 err = 0;
1531 if (up->hard) {
1532 xfrm_policy_delete(xp, p->dir);
Joy Lattenab5f5e82007-09-17 11:51:22 -07001533 xfrm_audit_policy_delete(xp, 1, NETLINK_CB(skb).loginuid,
1534 NETLINK_CB(skb).sid);
Joy Latten161a09e2006-11-27 13:11:54 -06001535
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001536 } else {
1537 // reset the timers here?
1538 printk("Dont know what to do with soft policy expire\n");
1539 }
1540 km_policy_expired(xp, p->dir, up->hard, current->pid);
1541
1542out:
1543 xfrm_pol_put(xp);
1544 return err;
1545}
1546
Christoph Hellwig22e70052007-01-02 15:22:30 -08001547static int xfrm_add_sa_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001548 struct nlattr **attrs)
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001549{
1550 struct xfrm_state *x;
1551 int err;
Thomas Graf7b67c852007-08-22 13:53:52 -07001552 struct xfrm_user_expire *ue = nlmsg_data(nlh);
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001553 struct xfrm_usersa_info *p = &ue->state;
1554
1555 x = xfrm_state_lookup(&p->id.daddr, p->id.spi, p->id.proto, p->family);
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001556
David S. Miller3a765aa2007-02-26 14:52:21 -08001557 err = -ENOENT;
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001558 if (x == NULL)
1559 return err;
1560
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001561 spin_lock_bh(&x->lock);
David S. Miller3a765aa2007-02-26 14:52:21 -08001562 err = -EINVAL;
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001563 if (x->km.state != XFRM_STATE_VALID)
1564 goto out;
1565 km_state_expired(x, ue->hard, current->pid);
1566
Joy Latten161a09e2006-11-27 13:11:54 -06001567 if (ue->hard) {
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001568 __xfrm_state_delete(x);
Joy Lattenab5f5e82007-09-17 11:51:22 -07001569 xfrm_audit_state_delete(x, 1, NETLINK_CB(skb).loginuid,
1570 NETLINK_CB(skb).sid);
Joy Latten161a09e2006-11-27 13:11:54 -06001571 }
David S. Miller3a765aa2007-02-26 14:52:21 -08001572 err = 0;
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001573out:
1574 spin_unlock_bh(&x->lock);
1575 xfrm_state_put(x);
1576 return err;
1577}
1578
Christoph Hellwig22e70052007-01-02 15:22:30 -08001579static int xfrm_add_acquire(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001580 struct nlattr **attrs)
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001581{
1582 struct xfrm_policy *xp;
1583 struct xfrm_user_tmpl *ut;
1584 int i;
Thomas Graf5424f322007-08-22 14:01:33 -07001585 struct nlattr *rt = attrs[XFRMA_TMPL];
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001586
Thomas Graf7b67c852007-08-22 13:53:52 -07001587 struct xfrm_user_acquire *ua = nlmsg_data(nlh);
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001588 struct xfrm_state *x = xfrm_state_alloc();
1589 int err = -ENOMEM;
1590
1591 if (!x)
1592 return err;
1593
1594 err = verify_newpolicy_info(&ua->policy);
1595 if (err) {
1596 printk("BAD policy passed\n");
1597 kfree(x);
1598 return err;
1599 }
1600
1601 /* build an XP */
Thomas Graf5424f322007-08-22 14:01:33 -07001602 xp = xfrm_policy_construct(&ua->policy, attrs, &err);
David S. Millerb4ad86bf2006-12-03 19:19:26 -08001603 if (!xp) {
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001604 kfree(x);
1605 return err;
1606 }
1607
1608 memcpy(&x->id, &ua->id, sizeof(ua->id));
1609 memcpy(&x->props.saddr, &ua->saddr, sizeof(ua->saddr));
1610 memcpy(&x->sel, &ua->sel, sizeof(ua->sel));
1611
Thomas Graf5424f322007-08-22 14:01:33 -07001612 ut = nla_data(rt);
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001613 /* extract the templates and for each call km_key */
1614 for (i = 0; i < xp->xfrm_nr; i++, ut++) {
1615 struct xfrm_tmpl *t = &xp->xfrm_vec[i];
1616 memcpy(&x->id, &t->id, sizeof(x->id));
1617 x->props.mode = t->mode;
1618 x->props.reqid = t->reqid;
1619 x->props.family = ut->family;
1620 t->aalgos = ua->aalgos;
1621 t->ealgos = ua->ealgos;
1622 t->calgos = ua->calgos;
1623 err = km_query(x, t, xp);
1624
1625 }
1626
1627 kfree(x);
1628 kfree(xp);
1629
1630 return 0;
1631}
1632
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001633#ifdef CONFIG_XFRM_MIGRATE
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001634static int copy_from_user_migrate(struct xfrm_migrate *ma,
Thomas Graf5424f322007-08-22 14:01:33 -07001635 struct nlattr **attrs, int *num)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001636{
Thomas Graf5424f322007-08-22 14:01:33 -07001637 struct nlattr *rt = attrs[XFRMA_MIGRATE];
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001638 struct xfrm_user_migrate *um;
1639 int i, num_migrate;
1640
Thomas Graf5424f322007-08-22 14:01:33 -07001641 um = nla_data(rt);
1642 num_migrate = nla_len(rt) / sizeof(*um);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001643
1644 if (num_migrate <= 0 || num_migrate > XFRM_MAX_DEPTH)
1645 return -EINVAL;
1646
1647 for (i = 0; i < num_migrate; i++, um++, ma++) {
1648 memcpy(&ma->old_daddr, &um->old_daddr, sizeof(ma->old_daddr));
1649 memcpy(&ma->old_saddr, &um->old_saddr, sizeof(ma->old_saddr));
1650 memcpy(&ma->new_daddr, &um->new_daddr, sizeof(ma->new_daddr));
1651 memcpy(&ma->new_saddr, &um->new_saddr, sizeof(ma->new_saddr));
1652
1653 ma->proto = um->proto;
1654 ma->mode = um->mode;
1655 ma->reqid = um->reqid;
1656
1657 ma->old_family = um->old_family;
1658 ma->new_family = um->new_family;
1659 }
1660
1661 *num = i;
1662 return 0;
1663}
1664
1665static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001666 struct nlattr **attrs)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001667{
Thomas Graf7b67c852007-08-22 13:53:52 -07001668 struct xfrm_userpolicy_id *pi = nlmsg_data(nlh);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001669 struct xfrm_migrate m[XFRM_MAX_DEPTH];
1670 u8 type;
1671 int err;
1672 int n = 0;
1673
Thomas Graf35a7aa02007-08-22 14:00:40 -07001674 if (attrs[XFRMA_MIGRATE] == NULL)
Thomas Grafcf5cb792007-08-22 13:59:04 -07001675 return -EINVAL;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001676
Thomas Graf5424f322007-08-22 14:01:33 -07001677 err = copy_from_user_policy_type(&type, attrs);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001678 if (err)
1679 return err;
1680
1681 err = copy_from_user_migrate((struct xfrm_migrate *)m,
Thomas Graf5424f322007-08-22 14:01:33 -07001682 attrs, &n);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001683 if (err)
1684 return err;
1685
1686 if (!n)
1687 return 0;
1688
1689 xfrm_migrate(&pi->sel, pi->dir, type, m, n);
1690
1691 return 0;
1692}
1693#else
1694static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001695 struct nlattr **attrs)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001696{
1697 return -ENOPROTOOPT;
1698}
1699#endif
1700
1701#ifdef CONFIG_XFRM_MIGRATE
1702static int copy_to_user_migrate(struct xfrm_migrate *m, struct sk_buff *skb)
1703{
1704 struct xfrm_user_migrate um;
1705
1706 memset(&um, 0, sizeof(um));
1707 um.proto = m->proto;
1708 um.mode = m->mode;
1709 um.reqid = m->reqid;
1710 um.old_family = m->old_family;
1711 memcpy(&um.old_daddr, &m->old_daddr, sizeof(um.old_daddr));
1712 memcpy(&um.old_saddr, &m->old_saddr, sizeof(um.old_saddr));
1713 um.new_family = m->new_family;
1714 memcpy(&um.new_daddr, &m->new_daddr, sizeof(um.new_daddr));
1715 memcpy(&um.new_saddr, &m->new_saddr, sizeof(um.new_saddr));
1716
Thomas Grafc0144be2007-08-22 13:55:43 -07001717 return nla_put(skb, XFRMA_MIGRATE, sizeof(um), &um);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001718}
1719
Thomas Graf7deb2262007-08-22 13:57:39 -07001720static inline size_t xfrm_migrate_msgsize(int num_migrate)
1721{
1722 return NLMSG_ALIGN(sizeof(struct xfrm_userpolicy_id))
1723 + nla_total_size(sizeof(struct xfrm_user_migrate) * num_migrate)
1724 + userpolicy_type_attrsize();
1725}
1726
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001727static int build_migrate(struct sk_buff *skb, struct xfrm_migrate *m,
1728 int num_migrate, struct xfrm_selector *sel,
1729 u8 dir, u8 type)
1730{
1731 struct xfrm_migrate *mp;
1732 struct xfrm_userpolicy_id *pol_id;
1733 struct nlmsghdr *nlh;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001734 int i;
1735
Thomas Graf79b8b7f2007-08-22 12:46:53 -07001736 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_MIGRATE, sizeof(*pol_id), 0);
1737 if (nlh == NULL)
1738 return -EMSGSIZE;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001739
Thomas Graf7b67c852007-08-22 13:53:52 -07001740 pol_id = nlmsg_data(nlh);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001741 /* copy data from selector, dir, and type to the pol_id */
1742 memset(pol_id, 0, sizeof(*pol_id));
1743 memcpy(&pol_id->sel, sel, sizeof(pol_id->sel));
1744 pol_id->dir = dir;
1745
1746 if (copy_to_user_policy_type(type, skb) < 0)
1747 goto nlmsg_failure;
1748
1749 for (i = 0, mp = m ; i < num_migrate; i++, mp++) {
1750 if (copy_to_user_migrate(mp, skb) < 0)
1751 goto nlmsg_failure;
1752 }
1753
Thomas Graf98250692007-08-22 12:47:26 -07001754 return nlmsg_end(skb, nlh);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001755nlmsg_failure:
Thomas Graf98250692007-08-22 12:47:26 -07001756 nlmsg_cancel(skb, nlh);
1757 return -EMSGSIZE;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001758}
1759
1760static int xfrm_send_migrate(struct xfrm_selector *sel, u8 dir, u8 type,
1761 struct xfrm_migrate *m, int num_migrate)
1762{
1763 struct sk_buff *skb;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001764
Thomas Graf7deb2262007-08-22 13:57:39 -07001765 skb = nlmsg_new(xfrm_migrate_msgsize(num_migrate), GFP_ATOMIC);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001766 if (skb == NULL)
1767 return -ENOMEM;
1768
1769 /* build migrate */
1770 if (build_migrate(skb, m, num_migrate, sel, dir, type) < 0)
1771 BUG();
1772
Thomas Graf082a1ad2007-08-22 13:54:36 -07001773 return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_MIGRATE, GFP_ATOMIC);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001774}
1775#else
1776static int xfrm_send_migrate(struct xfrm_selector *sel, u8 dir, u8 type,
1777 struct xfrm_migrate *m, int num_migrate)
1778{
1779 return -ENOPROTOOPT;
1780}
1781#endif
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001782
Thomas Grafa7bd9a42007-08-22 13:58:18 -07001783#define XMSGSIZE(type) sizeof(struct type)
Thomas Graf492b5582005-05-03 14:26:40 -07001784
1785static const int xfrm_msg_min[XFRM_NR_MSGTYPES] = {
1786 [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
1787 [XFRM_MSG_DELSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
1788 [XFRM_MSG_GETSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
1789 [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
1790 [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
1791 [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
1792 [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userspi_info),
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001793 [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_acquire),
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001794 [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_expire),
Thomas Graf492b5582005-05-03 14:26:40 -07001795 [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
1796 [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001797 [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_polexpire),
Thomas Graf492b5582005-05-03 14:26:40 -07001798 [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_flush),
Thomas Grafa7bd9a42007-08-22 13:58:18 -07001799 [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = 0,
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001800 [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
1801 [XFRM_MSG_GETAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07001802 [XFRM_MSG_REPORT - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_report),
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001803 [XFRM_MSG_MIGRATE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
Thomas Grafa7bd9a42007-08-22 13:58:18 -07001804 [XFRM_MSG_GETSADINFO - XFRM_MSG_BASE] = sizeof(u32),
1805 [XFRM_MSG_GETSPDINFO - XFRM_MSG_BASE] = sizeof(u32),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806};
1807
Thomas Graf492b5582005-05-03 14:26:40 -07001808#undef XMSGSIZE
1809
Thomas Grafcf5cb792007-08-22 13:59:04 -07001810static const struct nla_policy xfrma_policy[XFRMA_MAX+1] = {
1811 [XFRMA_ALG_AUTH] = { .len = sizeof(struct xfrm_algo) },
1812 [XFRMA_ALG_CRYPT] = { .len = sizeof(struct xfrm_algo) },
1813 [XFRMA_ALG_COMP] = { .len = sizeof(struct xfrm_algo) },
1814 [XFRMA_ENCAP] = { .len = sizeof(struct xfrm_encap_tmpl) },
1815 [XFRMA_TMPL] = { .len = sizeof(struct xfrm_user_tmpl) },
1816 [XFRMA_SEC_CTX] = { .len = sizeof(struct xfrm_sec_ctx) },
1817 [XFRMA_LTIME_VAL] = { .len = sizeof(struct xfrm_lifetime_cur) },
1818 [XFRMA_REPLAY_VAL] = { .len = sizeof(struct xfrm_replay_state) },
1819 [XFRMA_REPLAY_THRESH] = { .type = NLA_U32 },
1820 [XFRMA_ETIMER_THRESH] = { .type = NLA_U32 },
1821 [XFRMA_SRCADDR] = { .len = sizeof(xfrm_address_t) },
1822 [XFRMA_COADDR] = { .len = sizeof(xfrm_address_t) },
1823 [XFRMA_POLICY_TYPE] = { .len = sizeof(struct xfrm_userpolicy_type)},
1824 [XFRMA_MIGRATE] = { .len = sizeof(struct xfrm_user_migrate) },
1825};
1826
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827static struct xfrm_link {
Thomas Graf5424f322007-08-22 14:01:33 -07001828 int (*doit)(struct sk_buff *, struct nlmsghdr *, struct nlattr **);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829 int (*dump)(struct sk_buff *, struct netlink_callback *);
Thomas Graf492b5582005-05-03 14:26:40 -07001830} xfrm_dispatch[XFRM_NR_MSGTYPES] = {
1831 [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa },
1832 [XFRM_MSG_DELSA - XFRM_MSG_BASE] = { .doit = xfrm_del_sa },
1833 [XFRM_MSG_GETSA - XFRM_MSG_BASE] = { .doit = xfrm_get_sa,
1834 .dump = xfrm_dump_sa },
1835 [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy },
1836 [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy },
1837 [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy,
1838 .dump = xfrm_dump_policy },
1839 [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = { .doit = xfrm_alloc_userspi },
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001840 [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_acquire },
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001841 [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_sa_expire },
Thomas Graf492b5582005-05-03 14:26:40 -07001842 [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy },
1843 [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa },
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001844 [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_pol_expire},
Thomas Graf492b5582005-05-03 14:26:40 -07001845 [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = { .doit = xfrm_flush_sa },
1846 [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_flush_policy },
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001847 [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = { .doit = xfrm_new_ae },
1848 [XFRM_MSG_GETAE - XFRM_MSG_BASE] = { .doit = xfrm_get_ae },
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001849 [XFRM_MSG_MIGRATE - XFRM_MSG_BASE] = { .doit = xfrm_do_migrate },
Jamal Hadi Salim566ec032007-04-26 14:12:15 -07001850 [XFRM_MSG_GETSADINFO - XFRM_MSG_BASE] = { .doit = xfrm_get_sadinfo },
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -07001851 [XFRM_MSG_GETSPDINFO - XFRM_MSG_BASE] = { .doit = xfrm_get_spdinfo },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852};
1853
Thomas Graf1d00a4e2007-03-22 23:30:12 -07001854static int xfrm_user_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855{
Thomas Graf35a7aa02007-08-22 14:00:40 -07001856 struct nlattr *attrs[XFRMA_MAX+1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857 struct xfrm_link *link;
Thomas Grafa7bd9a42007-08-22 13:58:18 -07001858 int type, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860 type = nlh->nlmsg_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861 if (type > XFRM_MSG_MAX)
Thomas Graf1d00a4e2007-03-22 23:30:12 -07001862 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863
1864 type -= XFRM_MSG_BASE;
1865 link = &xfrm_dispatch[type];
1866
1867 /* All operations require privileges, even GET */
Thomas Graf1d00a4e2007-03-22 23:30:12 -07001868 if (security_netlink_recv(skb, CAP_NET_ADMIN))
1869 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870
Thomas Graf492b5582005-05-03 14:26:40 -07001871 if ((type == (XFRM_MSG_GETSA - XFRM_MSG_BASE) ||
1872 type == (XFRM_MSG_GETPOLICY - XFRM_MSG_BASE)) &&
1873 (nlh->nlmsg_flags & NLM_F_DUMP)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874 if (link->dump == NULL)
Thomas Graf1d00a4e2007-03-22 23:30:12 -07001875 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876
Thomas Grafc702e802007-03-22 23:30:55 -07001877 return netlink_dump_start(xfrm_nl, skb, nlh, link->dump, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878 }
1879
Thomas Graf35a7aa02007-08-22 14:00:40 -07001880 err = nlmsg_parse(nlh, xfrm_msg_min[type], attrs, XFRMA_MAX,
Thomas Grafcf5cb792007-08-22 13:59:04 -07001881 xfrma_policy);
Thomas Grafa7bd9a42007-08-22 13:58:18 -07001882 if (err < 0)
1883 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884
1885 if (link->doit == NULL)
Thomas Graf1d00a4e2007-03-22 23:30:12 -07001886 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887
Thomas Graf5424f322007-08-22 14:01:33 -07001888 return link->doit(skb, nlh, attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889}
1890
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07001891static void xfrm_netlink_rcv(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892{
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07001893 mutex_lock(&xfrm_cfg_mutex);
1894 netlink_rcv_skb(skb, &xfrm_user_rcv_msg);
1895 mutex_unlock(&xfrm_cfg_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896}
1897
Thomas Graf7deb2262007-08-22 13:57:39 -07001898static inline size_t xfrm_expire_msgsize(void)
1899{
1900 return NLMSG_ALIGN(sizeof(struct xfrm_user_expire));
1901}
1902
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001903static int build_expire(struct sk_buff *skb, struct xfrm_state *x, struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904{
1905 struct xfrm_user_expire *ue;
1906 struct nlmsghdr *nlh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907
Thomas Graf79b8b7f2007-08-22 12:46:53 -07001908 nlh = nlmsg_put(skb, c->pid, 0, XFRM_MSG_EXPIRE, sizeof(*ue), 0);
1909 if (nlh == NULL)
1910 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911
Thomas Graf7b67c852007-08-22 13:53:52 -07001912 ue = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913 copy_to_user_state(x, &ue->state);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001914 ue->hard = (c->data.hard != 0) ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915
Thomas Graf98250692007-08-22 12:47:26 -07001916 return nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917}
1918
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001919static int xfrm_exp_state_notify(struct xfrm_state *x, struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920{
1921 struct sk_buff *skb;
1922
Thomas Graf7deb2262007-08-22 13:57:39 -07001923 skb = nlmsg_new(xfrm_expire_msgsize(), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924 if (skb == NULL)
1925 return -ENOMEM;
1926
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001927 if (build_expire(skb, x, c) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928 BUG();
1929
Thomas Graf082a1ad2007-08-22 13:54:36 -07001930 return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_EXPIRE, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931}
1932
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001933static int xfrm_aevent_state_notify(struct xfrm_state *x, struct km_event *c)
1934{
1935 struct sk_buff *skb;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001936
Thomas Graf7deb2262007-08-22 13:57:39 -07001937 skb = nlmsg_new(xfrm_aevent_msgsize(), GFP_ATOMIC);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001938 if (skb == NULL)
1939 return -ENOMEM;
1940
1941 if (build_aevent(skb, x, c) < 0)
1942 BUG();
1943
Thomas Graf082a1ad2007-08-22 13:54:36 -07001944 return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_AEVENTS, GFP_ATOMIC);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001945}
1946
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001947static int xfrm_notify_sa_flush(struct km_event *c)
1948{
1949 struct xfrm_usersa_flush *p;
1950 struct nlmsghdr *nlh;
1951 struct sk_buff *skb;
Thomas Graf7deb2262007-08-22 13:57:39 -07001952 int len = NLMSG_ALIGN(sizeof(struct xfrm_usersa_flush));
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001953
Thomas Graf7deb2262007-08-22 13:57:39 -07001954 skb = nlmsg_new(len, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001955 if (skb == NULL)
1956 return -ENOMEM;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001957
Thomas Graf79b8b7f2007-08-22 12:46:53 -07001958 nlh = nlmsg_put(skb, c->pid, c->seq, XFRM_MSG_FLUSHSA, sizeof(*p), 0);
1959 if (nlh == NULL) {
1960 kfree_skb(skb);
1961 return -EMSGSIZE;
1962 }
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001963
Thomas Graf7b67c852007-08-22 13:53:52 -07001964 p = nlmsg_data(nlh);
Herbert Xubf088672005-06-18 22:44:00 -07001965 p->proto = c->data.proto;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001966
Thomas Graf98250692007-08-22 12:47:26 -07001967 nlmsg_end(skb, nlh);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001968
Thomas Graf082a1ad2007-08-22 13:54:36 -07001969 return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_SA, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001970}
1971
Thomas Graf7deb2262007-08-22 13:57:39 -07001972static inline size_t xfrm_sa_len(struct xfrm_state *x)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001973{
Thomas Graf7deb2262007-08-22 13:57:39 -07001974 size_t l = 0;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001975 if (x->aalg)
Eric Dumazet0f99be02008-01-08 23:39:06 -08001976 l += nla_total_size(xfrm_alg_len(x->aalg));
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001977 if (x->ealg)
Eric Dumazet0f99be02008-01-08 23:39:06 -08001978 l += nla_total_size(xfrm_alg_len(x->ealg));
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001979 if (x->calg)
Thomas Graf7deb2262007-08-22 13:57:39 -07001980 l += nla_total_size(sizeof(*x->calg));
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001981 if (x->encap)
Thomas Graf7deb2262007-08-22 13:57:39 -07001982 l += nla_total_size(sizeof(*x->encap));
Herbert Xu68325d32007-10-09 13:30:57 -07001983 if (x->security)
1984 l += nla_total_size(sizeof(struct xfrm_user_sec_ctx) +
1985 x->security->ctx_len);
1986 if (x->coaddr)
1987 l += nla_total_size(sizeof(*x->coaddr));
1988
1989 /* Must count this as this may become non-zero behind our back. */
1990 l += nla_total_size(sizeof(x->lastused));
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001991
1992 return l;
1993}
1994
1995static int xfrm_notify_sa(struct xfrm_state *x, struct km_event *c)
1996{
1997 struct xfrm_usersa_info *p;
Herbert Xu0603eac2005-06-18 22:54:36 -07001998 struct xfrm_usersa_id *id;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001999 struct nlmsghdr *nlh;
2000 struct sk_buff *skb;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002001 int len = xfrm_sa_len(x);
Herbert Xu0603eac2005-06-18 22:54:36 -07002002 int headlen;
2003
2004 headlen = sizeof(*p);
2005 if (c->event == XFRM_MSG_DELSA) {
Thomas Graf7deb2262007-08-22 13:57:39 -07002006 len += nla_total_size(headlen);
Herbert Xu0603eac2005-06-18 22:54:36 -07002007 headlen = sizeof(*id);
2008 }
Thomas Graf7deb2262007-08-22 13:57:39 -07002009 len += NLMSG_ALIGN(headlen);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002010
Thomas Graf7deb2262007-08-22 13:57:39 -07002011 skb = nlmsg_new(len, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002012 if (skb == NULL)
2013 return -ENOMEM;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002014
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002015 nlh = nlmsg_put(skb, c->pid, c->seq, c->event, headlen, 0);
2016 if (nlh == NULL)
Thomas Grafc0144be2007-08-22 13:55:43 -07002017 goto nla_put_failure;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002018
Thomas Graf7b67c852007-08-22 13:53:52 -07002019 p = nlmsg_data(nlh);
Herbert Xu0603eac2005-06-18 22:54:36 -07002020 if (c->event == XFRM_MSG_DELSA) {
Thomas Grafc0144be2007-08-22 13:55:43 -07002021 struct nlattr *attr;
2022
Thomas Graf7b67c852007-08-22 13:53:52 -07002023 id = nlmsg_data(nlh);
Herbert Xu0603eac2005-06-18 22:54:36 -07002024 memcpy(&id->daddr, &x->id.daddr, sizeof(id->daddr));
2025 id->spi = x->id.spi;
2026 id->family = x->props.family;
2027 id->proto = x->id.proto;
2028
Thomas Grafc0144be2007-08-22 13:55:43 -07002029 attr = nla_reserve(skb, XFRMA_SA, sizeof(*p));
2030 if (attr == NULL)
2031 goto nla_put_failure;
2032
2033 p = nla_data(attr);
Herbert Xu0603eac2005-06-18 22:54:36 -07002034 }
2035
Herbert Xu68325d32007-10-09 13:30:57 -07002036 if (copy_to_user_state_extra(x, p, skb))
2037 goto nla_put_failure;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002038
Thomas Graf98250692007-08-22 12:47:26 -07002039 nlmsg_end(skb, nlh);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002040
Thomas Graf082a1ad2007-08-22 13:54:36 -07002041 return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_SA, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002042
Thomas Grafc0144be2007-08-22 13:55:43 -07002043nla_put_failure:
Herbert Xu68325d32007-10-09 13:30:57 -07002044 /* Somebody screwed up with xfrm_sa_len! */
2045 WARN_ON(1);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002046 kfree_skb(skb);
2047 return -1;
2048}
2049
2050static int xfrm_send_state_notify(struct xfrm_state *x, struct km_event *c)
2051{
2052
2053 switch (c->event) {
Herbert Xuf60f6b82005-06-18 22:44:37 -07002054 case XFRM_MSG_EXPIRE:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002055 return xfrm_exp_state_notify(x, c);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002056 case XFRM_MSG_NEWAE:
2057 return xfrm_aevent_state_notify(x, c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07002058 case XFRM_MSG_DELSA:
2059 case XFRM_MSG_UPDSA:
2060 case XFRM_MSG_NEWSA:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002061 return xfrm_notify_sa(x, c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07002062 case XFRM_MSG_FLUSHSA:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002063 return xfrm_notify_sa_flush(c);
2064 default:
2065 printk("xfrm_user: Unknown SA event %d\n", c->event);
2066 break;
2067 }
2068
2069 return 0;
2070
2071}
2072
Thomas Graf7deb2262007-08-22 13:57:39 -07002073static inline size_t xfrm_acquire_msgsize(struct xfrm_state *x,
2074 struct xfrm_policy *xp)
2075{
2076 return NLMSG_ALIGN(sizeof(struct xfrm_user_acquire))
2077 + nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr)
2078 + nla_total_size(xfrm_user_sec_ctx_size(x->security))
2079 + userpolicy_type_attrsize();
2080}
2081
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082static int build_acquire(struct sk_buff *skb, struct xfrm_state *x,
2083 struct xfrm_tmpl *xt, struct xfrm_policy *xp,
2084 int dir)
2085{
2086 struct xfrm_user_acquire *ua;
2087 struct nlmsghdr *nlh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088 __u32 seq = xfrm_get_acqseq();
2089
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002090 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_ACQUIRE, sizeof(*ua), 0);
2091 if (nlh == NULL)
2092 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093
Thomas Graf7b67c852007-08-22 13:53:52 -07002094 ua = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095 memcpy(&ua->id, &x->id, sizeof(ua->id));
2096 memcpy(&ua->saddr, &x->props.saddr, sizeof(ua->saddr));
2097 memcpy(&ua->sel, &x->sel, sizeof(ua->sel));
2098 copy_to_user_policy(xp, &ua->policy, dir);
2099 ua->aalgos = xt->aalgos;
2100 ua->ealgos = xt->ealgos;
2101 ua->calgos = xt->calgos;
2102 ua->seq = x->km.seq = seq;
2103
2104 if (copy_to_user_tmpl(xp, skb) < 0)
2105 goto nlmsg_failure;
Serge Hallyn0d681622006-07-24 23:30:44 -07002106 if (copy_to_user_state_sec_ctx(x, skb))
Trent Jaegerdf718372005-12-13 23:12:27 -08002107 goto nlmsg_failure;
Jamal Hadi Salim1459bb32006-11-20 16:51:22 -08002108 if (copy_to_user_policy_type(xp->type, skb) < 0)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07002109 goto nlmsg_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110
Thomas Graf98250692007-08-22 12:47:26 -07002111 return nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112
2113nlmsg_failure:
Thomas Graf98250692007-08-22 12:47:26 -07002114 nlmsg_cancel(skb, nlh);
2115 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116}
2117
2118static int xfrm_send_acquire(struct xfrm_state *x, struct xfrm_tmpl *xt,
2119 struct xfrm_policy *xp, int dir)
2120{
2121 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122
Thomas Graf7deb2262007-08-22 13:57:39 -07002123 skb = nlmsg_new(xfrm_acquire_msgsize(x, xp), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124 if (skb == NULL)
2125 return -ENOMEM;
2126
2127 if (build_acquire(skb, x, xt, xp, dir) < 0)
2128 BUG();
2129
Thomas Graf082a1ad2007-08-22 13:54:36 -07002130 return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_ACQUIRE, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131}
2132
2133/* User gives us xfrm_user_policy_info followed by an array of 0
2134 * or more templates.
2135 */
Venkat Yekkiralacb969f02006-07-24 23:32:20 -07002136static struct xfrm_policy *xfrm_compile_policy(struct sock *sk, int opt,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137 u8 *data, int len, int *dir)
2138{
2139 struct xfrm_userpolicy_info *p = (struct xfrm_userpolicy_info *)data;
2140 struct xfrm_user_tmpl *ut = (struct xfrm_user_tmpl *) (p + 1);
2141 struct xfrm_policy *xp;
2142 int nr;
2143
Venkat Yekkiralacb969f02006-07-24 23:32:20 -07002144 switch (sk->sk_family) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145 case AF_INET:
2146 if (opt != IP_XFRM_POLICY) {
2147 *dir = -EOPNOTSUPP;
2148 return NULL;
2149 }
2150 break;
2151#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
2152 case AF_INET6:
2153 if (opt != IPV6_XFRM_POLICY) {
2154 *dir = -EOPNOTSUPP;
2155 return NULL;
2156 }
2157 break;
2158#endif
2159 default:
2160 *dir = -EINVAL;
2161 return NULL;
2162 }
2163
2164 *dir = -EINVAL;
2165
2166 if (len < sizeof(*p) ||
2167 verify_newpolicy_info(p))
2168 return NULL;
2169
2170 nr = ((len - sizeof(*p)) / sizeof(*ut));
David S. Millerb4ad86bf2006-12-03 19:19:26 -08002171 if (validate_tmpl(nr, ut, p->sel.family))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172 return NULL;
2173
Herbert Xua4f1bac2005-07-26 15:43:17 -07002174 if (p->dir > XFRM_POLICY_OUT)
2175 return NULL;
2176
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177 xp = xfrm_policy_alloc(GFP_KERNEL);
2178 if (xp == NULL) {
2179 *dir = -ENOBUFS;
2180 return NULL;
2181 }
2182
2183 copy_from_user_policy(xp, p);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07002184 xp->type = XFRM_POLICY_TYPE_MAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185 copy_templates(xp, ut, nr);
2186
2187 *dir = p->dir;
2188
2189 return xp;
2190}
2191
Thomas Graf7deb2262007-08-22 13:57:39 -07002192static inline size_t xfrm_polexpire_msgsize(struct xfrm_policy *xp)
2193{
2194 return NLMSG_ALIGN(sizeof(struct xfrm_user_polexpire))
2195 + nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr)
2196 + nla_total_size(xfrm_user_sec_ctx_size(xp->security))
2197 + userpolicy_type_attrsize();
2198}
2199
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200static int build_polexpire(struct sk_buff *skb, struct xfrm_policy *xp,
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002201 int dir, struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202{
2203 struct xfrm_user_polexpire *upe;
2204 struct nlmsghdr *nlh;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002205 int hard = c->data.hard;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002207 nlh = nlmsg_put(skb, c->pid, 0, XFRM_MSG_POLEXPIRE, sizeof(*upe), 0);
2208 if (nlh == NULL)
2209 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002210
Thomas Graf7b67c852007-08-22 13:53:52 -07002211 upe = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212 copy_to_user_policy(xp, &upe->pol, dir);
2213 if (copy_to_user_tmpl(xp, skb) < 0)
2214 goto nlmsg_failure;
Trent Jaegerdf718372005-12-13 23:12:27 -08002215 if (copy_to_user_sec_ctx(xp, skb))
2216 goto nlmsg_failure;
Jamal Hadi Salim1459bb32006-11-20 16:51:22 -08002217 if (copy_to_user_policy_type(xp->type, skb) < 0)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07002218 goto nlmsg_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002219 upe->hard = !!hard;
2220
Thomas Graf98250692007-08-22 12:47:26 -07002221 return nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002222
2223nlmsg_failure:
Thomas Graf98250692007-08-22 12:47:26 -07002224 nlmsg_cancel(skb, nlh);
2225 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002226}
2227
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002228static int xfrm_exp_policy_notify(struct xfrm_policy *xp, int dir, struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229{
2230 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231
Thomas Graf7deb2262007-08-22 13:57:39 -07002232 skb = nlmsg_new(xfrm_polexpire_msgsize(xp), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233 if (skb == NULL)
2234 return -ENOMEM;
2235
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002236 if (build_polexpire(skb, xp, dir, c) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237 BUG();
2238
Thomas Graf082a1ad2007-08-22 13:54:36 -07002239 return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_EXPIRE, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240}
2241
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002242static int xfrm_notify_policy(struct xfrm_policy *xp, int dir, struct km_event *c)
2243{
2244 struct xfrm_userpolicy_info *p;
Herbert Xu0603eac2005-06-18 22:54:36 -07002245 struct xfrm_userpolicy_id *id;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002246 struct nlmsghdr *nlh;
2247 struct sk_buff *skb;
Thomas Graf7deb2262007-08-22 13:57:39 -07002248 int len = nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr);
Herbert Xu0603eac2005-06-18 22:54:36 -07002249 int headlen;
2250
2251 headlen = sizeof(*p);
2252 if (c->event == XFRM_MSG_DELPOLICY) {
Thomas Graf7deb2262007-08-22 13:57:39 -07002253 len += nla_total_size(headlen);
Herbert Xu0603eac2005-06-18 22:54:36 -07002254 headlen = sizeof(*id);
2255 }
Thomas Grafcfbfd452007-08-22 13:57:04 -07002256 len += userpolicy_type_attrsize();
Thomas Graf7deb2262007-08-22 13:57:39 -07002257 len += NLMSG_ALIGN(headlen);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002258
Thomas Graf7deb2262007-08-22 13:57:39 -07002259 skb = nlmsg_new(len, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002260 if (skb == NULL)
2261 return -ENOMEM;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002262
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002263 nlh = nlmsg_put(skb, c->pid, c->seq, c->event, headlen, 0);
2264 if (nlh == NULL)
2265 goto nlmsg_failure;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002266
Thomas Graf7b67c852007-08-22 13:53:52 -07002267 p = nlmsg_data(nlh);
Herbert Xu0603eac2005-06-18 22:54:36 -07002268 if (c->event == XFRM_MSG_DELPOLICY) {
Thomas Grafc0144be2007-08-22 13:55:43 -07002269 struct nlattr *attr;
2270
Thomas Graf7b67c852007-08-22 13:53:52 -07002271 id = nlmsg_data(nlh);
Herbert Xu0603eac2005-06-18 22:54:36 -07002272 memset(id, 0, sizeof(*id));
2273 id->dir = dir;
2274 if (c->data.byid)
2275 id->index = xp->index;
2276 else
2277 memcpy(&id->sel, &xp->selector, sizeof(id->sel));
2278
Thomas Grafc0144be2007-08-22 13:55:43 -07002279 attr = nla_reserve(skb, XFRMA_POLICY, sizeof(*p));
2280 if (attr == NULL)
2281 goto nlmsg_failure;
2282
2283 p = nla_data(attr);
Herbert Xu0603eac2005-06-18 22:54:36 -07002284 }
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002285
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002286 copy_to_user_policy(xp, p, dir);
2287 if (copy_to_user_tmpl(xp, skb) < 0)
2288 goto nlmsg_failure;
Jamal Hadi Salim1459bb32006-11-20 16:51:22 -08002289 if (copy_to_user_policy_type(xp->type, skb) < 0)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07002290 goto nlmsg_failure;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002291
Thomas Graf98250692007-08-22 12:47:26 -07002292 nlmsg_end(skb, nlh);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002293
Thomas Graf082a1ad2007-08-22 13:54:36 -07002294 return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_POLICY, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002295
2296nlmsg_failure:
2297 kfree_skb(skb);
2298 return -1;
2299}
2300
2301static int xfrm_notify_policy_flush(struct km_event *c)
2302{
2303 struct nlmsghdr *nlh;
2304 struct sk_buff *skb;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002305
Thomas Graf7deb2262007-08-22 13:57:39 -07002306 skb = nlmsg_new(userpolicy_type_attrsize(), GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002307 if (skb == NULL)
2308 return -ENOMEM;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002309
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002310 nlh = nlmsg_put(skb, c->pid, c->seq, XFRM_MSG_FLUSHPOLICY, 0, 0);
2311 if (nlh == NULL)
2312 goto nlmsg_failure;
Jamal Hadi Salim0c51f532006-11-27 12:58:20 -08002313 if (copy_to_user_policy_type(c->data.type, skb) < 0)
2314 goto nlmsg_failure;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002315
Thomas Graf98250692007-08-22 12:47:26 -07002316 nlmsg_end(skb, nlh);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002317
Thomas Graf082a1ad2007-08-22 13:54:36 -07002318 return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_POLICY, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002319
2320nlmsg_failure:
2321 kfree_skb(skb);
2322 return -1;
2323}
2324
2325static int xfrm_send_policy_notify(struct xfrm_policy *xp, int dir, struct km_event *c)
2326{
2327
2328 switch (c->event) {
Herbert Xuf60f6b82005-06-18 22:44:37 -07002329 case XFRM_MSG_NEWPOLICY:
2330 case XFRM_MSG_UPDPOLICY:
2331 case XFRM_MSG_DELPOLICY:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002332 return xfrm_notify_policy(xp, dir, c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07002333 case XFRM_MSG_FLUSHPOLICY:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002334 return xfrm_notify_policy_flush(c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07002335 case XFRM_MSG_POLEXPIRE:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002336 return xfrm_exp_policy_notify(xp, dir, c);
2337 default:
2338 printk("xfrm_user: Unknown Policy event %d\n", c->event);
2339 }
2340
2341 return 0;
2342
2343}
2344
Thomas Graf7deb2262007-08-22 13:57:39 -07002345static inline size_t xfrm_report_msgsize(void)
2346{
2347 return NLMSG_ALIGN(sizeof(struct xfrm_user_report));
2348}
2349
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002350static int build_report(struct sk_buff *skb, u8 proto,
2351 struct xfrm_selector *sel, xfrm_address_t *addr)
2352{
2353 struct xfrm_user_report *ur;
2354 struct nlmsghdr *nlh;
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002355
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002356 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_REPORT, sizeof(*ur), 0);
2357 if (nlh == NULL)
2358 return -EMSGSIZE;
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002359
Thomas Graf7b67c852007-08-22 13:53:52 -07002360 ur = nlmsg_data(nlh);
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002361 ur->proto = proto;
2362 memcpy(&ur->sel, sel, sizeof(ur->sel));
2363
2364 if (addr)
Thomas Grafc0144be2007-08-22 13:55:43 -07002365 NLA_PUT(skb, XFRMA_COADDR, sizeof(*addr), addr);
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002366
Thomas Graf98250692007-08-22 12:47:26 -07002367 return nlmsg_end(skb, nlh);
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002368
Thomas Grafc0144be2007-08-22 13:55:43 -07002369nla_put_failure:
Thomas Graf98250692007-08-22 12:47:26 -07002370 nlmsg_cancel(skb, nlh);
2371 return -EMSGSIZE;
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002372}
2373
2374static int xfrm_send_report(u8 proto, struct xfrm_selector *sel,
2375 xfrm_address_t *addr)
2376{
2377 struct sk_buff *skb;
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002378
Thomas Graf7deb2262007-08-22 13:57:39 -07002379 skb = nlmsg_new(xfrm_report_msgsize(), GFP_ATOMIC);
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002380 if (skb == NULL)
2381 return -ENOMEM;
2382
2383 if (build_report(skb, proto, sel, addr) < 0)
2384 BUG();
2385
Thomas Graf082a1ad2007-08-22 13:54:36 -07002386 return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_REPORT, GFP_ATOMIC);
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002387}
2388
Linus Torvalds1da177e2005-04-16 15:20:36 -07002389static struct xfrm_mgr netlink_mgr = {
2390 .id = "netlink",
2391 .notify = xfrm_send_state_notify,
2392 .acquire = xfrm_send_acquire,
2393 .compile_policy = xfrm_compile_policy,
2394 .notify_policy = xfrm_send_policy_notify,
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002395 .report = xfrm_send_report,
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002396 .migrate = xfrm_send_migrate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002397};
2398
2399static int __init xfrm_user_init(void)
2400{
Patrick McHardybe336902006-03-20 22:40:54 -08002401 struct sock *nlsk;
2402
Masahide NAKAMURA654b32c2006-08-23 19:12:56 -07002403 printk(KERN_INFO "Initializing XFRM netlink socket\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002404
Eric W. Biedermanb4b51022007-09-12 13:05:38 +02002405 nlsk = netlink_kernel_create(&init_net, NETLINK_XFRM, XFRMNLGRP_MAX,
Patrick McHardyaf65bdf2007-04-20 14:14:21 -07002406 xfrm_netlink_rcv, NULL, THIS_MODULE);
Patrick McHardybe336902006-03-20 22:40:54 -08002407 if (nlsk == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002408 return -ENOMEM;
Patrick McHardybe336902006-03-20 22:40:54 -08002409 rcu_assign_pointer(xfrm_nl, nlsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002410
2411 xfrm_register_km(&netlink_mgr);
2412
2413 return 0;
2414}
2415
2416static void __exit xfrm_user_exit(void)
2417{
Patrick McHardybe336902006-03-20 22:40:54 -08002418 struct sock *nlsk = xfrm_nl;
2419
Linus Torvalds1da177e2005-04-16 15:20:36 -07002420 xfrm_unregister_km(&netlink_mgr);
Patrick McHardybe336902006-03-20 22:40:54 -08002421 rcu_assign_pointer(xfrm_nl, NULL);
2422 synchronize_rcu();
2423 sock_release(nlsk->sk_socket);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002424}
2425
2426module_init(xfrm_user_init);
2427module_exit(xfrm_user_exit);
2428MODULE_LICENSE("GPL");
Harald Welte4fdb3bb2005-08-09 19:40:55 -07002429MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK, NETLINK_XFRM);
Jamal Hadi Salimf8cd5482006-03-20 19:15:11 -08002430