blob: d41588d101d01db756f5db3a1cd40d6e783883a9 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* xfrm_user.c: User interface to configure xfrm engine.
2 *
3 * Copyright (C) 2002 David S. Miller (davem@redhat.com)
4 *
5 * Changes:
6 * Mitsuru KANDA @USAGI
7 * Kazunori MIYAZAWA @USAGI
8 * Kunihiro Ishiguro <kunihiro@ipinfusion.com>
9 * IPv6 support
Trent Jaegerdf718372005-12-13 23:12:27 -080010 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070011 */
12
Herbert Xu9409f382006-08-06 19:49:12 +100013#include <linux/crypto.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/module.h>
15#include <linux/kernel.h>
16#include <linux/types.h>
17#include <linux/slab.h>
18#include <linux/socket.h>
19#include <linux/string.h>
20#include <linux/net.h>
21#include <linux/skbuff.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/pfkeyv2.h>
23#include <linux/ipsec.h>
24#include <linux/init.h>
25#include <linux/security.h>
26#include <net/sock.h>
27#include <net/xfrm.h>
Thomas Graf88fc2c82005-11-10 02:25:54 +010028#include <net/netlink.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <asm/uaccess.h>
Masahide NAKAMURAe23c7192006-08-23 20:33:28 -070030#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
31#include <linux/in6.h>
32#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070033
Thomas Grafc26445a2007-08-22 13:56:23 -070034static inline int alg_len(struct xfrm_algo *alg)
35{
36 return sizeof(*alg) + ((alg->alg_key_len + 7) / 8);
37}
38
Thomas Graf5424f322007-08-22 14:01:33 -070039static int verify_one_alg(struct nlattr **attrs, enum xfrm_attr_type_t type)
Linus Torvalds1da177e2005-04-16 15:20:36 -070040{
Thomas Graf5424f322007-08-22 14:01:33 -070041 struct nlattr *rt = attrs[type];
Linus Torvalds1da177e2005-04-16 15:20:36 -070042 struct xfrm_algo *algp;
43
44 if (!rt)
45 return 0;
46
Thomas Graf5424f322007-08-22 14:01:33 -070047 algp = nla_data(rt);
48 if (nla_len(rt) < alg_len(algp))
Herbert Xu31c26852005-05-19 12:39:49 -070049 return -EINVAL;
50
Linus Torvalds1da177e2005-04-16 15:20:36 -070051 switch (type) {
52 case XFRMA_ALG_AUTH:
53 if (!algp->alg_key_len &&
54 strcmp(algp->alg_name, "digest_null") != 0)
55 return -EINVAL;
56 break;
57
58 case XFRMA_ALG_CRYPT:
59 if (!algp->alg_key_len &&
60 strcmp(algp->alg_name, "cipher_null") != 0)
61 return -EINVAL;
62 break;
63
64 case XFRMA_ALG_COMP:
65 /* Zero length keys are legal. */
66 break;
67
68 default:
69 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -070070 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
72 algp->alg_name[CRYPTO_MAX_ALG_NAME - 1] = '\0';
73 return 0;
74}
75
Thomas Graf5424f322007-08-22 14:01:33 -070076static void verify_one_addr(struct nlattr **attrs, enum xfrm_attr_type_t type,
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -070077 xfrm_address_t **addrp)
78{
Thomas Graf5424f322007-08-22 14:01:33 -070079 struct nlattr *rt = attrs[type];
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -070080
Thomas Grafcf5cb792007-08-22 13:59:04 -070081 if (rt && addrp)
Thomas Graf5424f322007-08-22 14:01:33 -070082 *addrp = nla_data(rt);
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -070083}
Trent Jaegerdf718372005-12-13 23:12:27 -080084
Thomas Graf5424f322007-08-22 14:01:33 -070085static inline int verify_sec_ctx_len(struct nlattr **attrs)
Trent Jaegerdf718372005-12-13 23:12:27 -080086{
Thomas Graf5424f322007-08-22 14:01:33 -070087 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
Trent Jaegerdf718372005-12-13 23:12:27 -080088 struct xfrm_user_sec_ctx *uctx;
Trent Jaegerdf718372005-12-13 23:12:27 -080089
90 if (!rt)
91 return 0;
92
Thomas Graf5424f322007-08-22 14:01:33 -070093 uctx = nla_data(rt);
Thomas Grafcf5cb792007-08-22 13:59:04 -070094 if (uctx->len != (sizeof(struct xfrm_user_sec_ctx) + uctx->ctx_len))
Trent Jaegerdf718372005-12-13 23:12:27 -080095 return -EINVAL;
96
97 return 0;
98}
99
100
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101static int verify_newsa_info(struct xfrm_usersa_info *p,
Thomas Graf5424f322007-08-22 14:01:33 -0700102 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103{
104 int err;
105
106 err = -EINVAL;
107 switch (p->family) {
108 case AF_INET:
109 break;
110
111 case AF_INET6:
112#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
113 break;
114#else
115 err = -EAFNOSUPPORT;
116 goto out;
117#endif
118
119 default:
120 goto out;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700121 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122
123 err = -EINVAL;
124 switch (p->id.proto) {
125 case IPPROTO_AH:
Thomas Graf35a7aa02007-08-22 14:00:40 -0700126 if (!attrs[XFRMA_ALG_AUTH] ||
127 attrs[XFRMA_ALG_CRYPT] ||
128 attrs[XFRMA_ALG_COMP])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 goto out;
130 break;
131
132 case IPPROTO_ESP:
Thomas Graf35a7aa02007-08-22 14:00:40 -0700133 if ((!attrs[XFRMA_ALG_AUTH] &&
134 !attrs[XFRMA_ALG_CRYPT]) ||
135 attrs[XFRMA_ALG_COMP])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 goto out;
137 break;
138
139 case IPPROTO_COMP:
Thomas Graf35a7aa02007-08-22 14:00:40 -0700140 if (!attrs[XFRMA_ALG_COMP] ||
141 attrs[XFRMA_ALG_AUTH] ||
142 attrs[XFRMA_ALG_CRYPT])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 goto out;
144 break;
145
Masahide NAKAMURAe23c7192006-08-23 20:33:28 -0700146#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
147 case IPPROTO_DSTOPTS:
148 case IPPROTO_ROUTING:
Thomas Graf35a7aa02007-08-22 14:00:40 -0700149 if (attrs[XFRMA_ALG_COMP] ||
150 attrs[XFRMA_ALG_AUTH] ||
151 attrs[XFRMA_ALG_CRYPT] ||
152 attrs[XFRMA_ENCAP] ||
153 attrs[XFRMA_SEC_CTX] ||
154 !attrs[XFRMA_COADDR])
Masahide NAKAMURAe23c7192006-08-23 20:33:28 -0700155 goto out;
156 break;
157#endif
158
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 default:
160 goto out;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700161 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162
Thomas Graf35a7aa02007-08-22 14:00:40 -0700163 if ((err = verify_one_alg(attrs, XFRMA_ALG_AUTH)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 goto out;
Thomas Graf35a7aa02007-08-22 14:00:40 -0700165 if ((err = verify_one_alg(attrs, XFRMA_ALG_CRYPT)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 goto out;
Thomas Graf35a7aa02007-08-22 14:00:40 -0700167 if ((err = verify_one_alg(attrs, XFRMA_ALG_COMP)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 goto out;
Thomas Graf35a7aa02007-08-22 14:00:40 -0700169 if ((err = verify_sec_ctx_len(attrs)))
Trent Jaegerdf718372005-12-13 23:12:27 -0800170 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171
172 err = -EINVAL;
173 switch (p->mode) {
Masahide NAKAMURA7e49e6d2006-09-22 15:05:15 -0700174 case XFRM_MODE_TRANSPORT:
175 case XFRM_MODE_TUNNEL:
Noriaki TAKAMIYA060f02a2006-08-23 18:18:55 -0700176 case XFRM_MODE_ROUTEOPTIMIZATION:
Diego Beltrami0a694522006-10-03 23:47:05 -0700177 case XFRM_MODE_BEET:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 break;
179
180 default:
181 goto out;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700182 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183
184 err = 0;
185
186out:
187 return err;
188}
189
190static int attach_one_algo(struct xfrm_algo **algpp, u8 *props,
191 struct xfrm_algo_desc *(*get_byname)(char *, int),
Thomas Graf5424f322007-08-22 14:01:33 -0700192 struct nlattr *rta)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 struct xfrm_algo *p, *ualg;
195 struct xfrm_algo_desc *algo;
196
197 if (!rta)
198 return 0;
199
Thomas Graf5424f322007-08-22 14:01:33 -0700200 ualg = nla_data(rta);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201
202 algo = get_byname(ualg->alg_name, 1);
203 if (!algo)
204 return -ENOSYS;
205 *props = algo->desc.sadb_alg_id;
206
Thomas Grafc26445a2007-08-22 13:56:23 -0700207 p = kmemdup(ualg, alg_len(ualg), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 if (!p)
209 return -ENOMEM;
210
Herbert Xu04ff1262006-08-13 08:50:00 +1000211 strcpy(p->alg_name, algo->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 *algpp = p;
213 return 0;
214}
215
Joy Latten661697f2007-04-13 16:14:35 -0700216static inline int xfrm_user_sec_ctx_size(struct xfrm_sec_ctx *xfrm_ctx)
Trent Jaegerdf718372005-12-13 23:12:27 -0800217{
Trent Jaegerdf718372005-12-13 23:12:27 -0800218 int len = 0;
219
220 if (xfrm_ctx) {
221 len += sizeof(struct xfrm_user_sec_ctx);
222 len += xfrm_ctx->ctx_len;
223 }
224 return len;
225}
226
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227static void copy_from_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p)
228{
229 memcpy(&x->id, &p->id, sizeof(x->id));
230 memcpy(&x->sel, &p->sel, sizeof(x->sel));
231 memcpy(&x->lft, &p->lft, sizeof(x->lft));
232 x->props.mode = p->mode;
233 x->props.replay_window = p->replay_window;
234 x->props.reqid = p->reqid;
235 x->props.family = p->family;
David S. Miller54489c142006-10-27 15:29:47 -0700236 memcpy(&x->props.saddr, &p->saddr, sizeof(x->props.saddr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 x->props.flags = p->flags;
Herbert Xu196b0032007-07-31 02:04:32 -0700238
239 /*
240 * Set inner address family if the KM left it as zero.
241 * See comment in validate_tmpl.
242 */
243 if (!x->sel.family)
244 x->sel.family = p->family;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245}
246
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800247/*
248 * someday when pfkey also has support, we could have the code
249 * somehow made shareable and move it to xfrm_state.c - JHS
250 *
251*/
Thomas Graf5424f322007-08-22 14:01:33 -0700252static void xfrm_update_ae_params(struct xfrm_state *x, struct nlattr **attrs)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800253{
Thomas Graf5424f322007-08-22 14:01:33 -0700254 struct nlattr *rp = attrs[XFRMA_REPLAY_VAL];
255 struct nlattr *lt = attrs[XFRMA_LTIME_VAL];
256 struct nlattr *et = attrs[XFRMA_ETIMER_THRESH];
257 struct nlattr *rt = attrs[XFRMA_REPLAY_THRESH];
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800258
259 if (rp) {
260 struct xfrm_replay_state *replay;
Thomas Graf5424f322007-08-22 14:01:33 -0700261 replay = nla_data(rp);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800262 memcpy(&x->replay, replay, sizeof(*replay));
263 memcpy(&x->preplay, replay, sizeof(*replay));
264 }
265
266 if (lt) {
267 struct xfrm_lifetime_cur *ltime;
Thomas Graf5424f322007-08-22 14:01:33 -0700268 ltime = nla_data(lt);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800269 x->curlft.bytes = ltime->bytes;
270 x->curlft.packets = ltime->packets;
271 x->curlft.add_time = ltime->add_time;
272 x->curlft.use_time = ltime->use_time;
273 }
274
Thomas Grafcf5cb792007-08-22 13:59:04 -0700275 if (et)
Thomas Graf5424f322007-08-22 14:01:33 -0700276 x->replay_maxage = nla_get_u32(et);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800277
Thomas Grafcf5cb792007-08-22 13:59:04 -0700278 if (rt)
Thomas Graf5424f322007-08-22 14:01:33 -0700279 x->replay_maxdiff = nla_get_u32(rt);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800280}
281
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282static struct xfrm_state *xfrm_state_construct(struct xfrm_usersa_info *p,
Thomas Graf5424f322007-08-22 14:01:33 -0700283 struct nlattr **attrs,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 int *errp)
285{
286 struct xfrm_state *x = xfrm_state_alloc();
287 int err = -ENOMEM;
288
289 if (!x)
290 goto error_no_put;
291
292 copy_from_user_state(x, p);
293
294 if ((err = attach_one_algo(&x->aalg, &x->props.aalgo,
295 xfrm_aalg_get_byname,
Thomas Graf35a7aa02007-08-22 14:00:40 -0700296 attrs[XFRMA_ALG_AUTH])))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 goto error;
298 if ((err = attach_one_algo(&x->ealg, &x->props.ealgo,
299 xfrm_ealg_get_byname,
Thomas Graf35a7aa02007-08-22 14:00:40 -0700300 attrs[XFRMA_ALG_CRYPT])))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 goto error;
302 if ((err = attach_one_algo(&x->calg, &x->props.calgo,
303 xfrm_calg_get_byname,
Thomas Graf35a7aa02007-08-22 14:00:40 -0700304 attrs[XFRMA_ALG_COMP])))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 goto error;
Thomas Graffd211502007-09-06 03:28:08 -0700306
307 if (attrs[XFRMA_ENCAP]) {
308 x->encap = kmemdup(nla_data(attrs[XFRMA_ENCAP]),
309 sizeof(*x->encap), GFP_KERNEL);
310 if (x->encap == NULL)
311 goto error;
312 }
313
314 if (attrs[XFRMA_COADDR]) {
315 x->coaddr = kmemdup(nla_data(attrs[XFRMA_COADDR]),
316 sizeof(*x->coaddr), GFP_KERNEL);
317 if (x->coaddr == NULL)
318 goto error;
319 }
320
Herbert Xu72cb6962005-06-20 13:18:08 -0700321 err = xfrm_init_state(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 if (err)
323 goto error;
324
Thomas Graffd211502007-09-06 03:28:08 -0700325 if (attrs[XFRMA_SEC_CTX] &&
326 security_xfrm_state_alloc(x, nla_data(attrs[XFRMA_SEC_CTX])))
Trent Jaegerdf718372005-12-13 23:12:27 -0800327 goto error;
328
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 x->km.seq = p->seq;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800330 x->replay_maxdiff = sysctl_xfrm_aevent_rseqth;
331 /* sysctl_xfrm_aevent_etime is in 100ms units */
332 x->replay_maxage = (sysctl_xfrm_aevent_etime*HZ)/XFRM_AE_ETH_M;
333 x->preplay.bitmap = 0;
334 x->preplay.seq = x->replay.seq+x->replay_maxdiff;
335 x->preplay.oseq = x->replay.oseq +x->replay_maxdiff;
336
337 /* override default values from above */
338
Thomas Graf5424f322007-08-22 14:01:33 -0700339 xfrm_update_ae_params(x, attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340
341 return x;
342
343error:
344 x->km.state = XFRM_STATE_DEAD;
345 xfrm_state_put(x);
346error_no_put:
347 *errp = err;
348 return NULL;
349}
350
Christoph Hellwig22e70052007-01-02 15:22:30 -0800351static int xfrm_add_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -0700352 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353{
Thomas Graf7b67c852007-08-22 13:53:52 -0700354 struct xfrm_usersa_info *p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 struct xfrm_state *x;
356 int err;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700357 struct km_event c;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358
Thomas Graf35a7aa02007-08-22 14:00:40 -0700359 err = verify_newsa_info(p, attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 if (err)
361 return err;
362
Thomas Graf35a7aa02007-08-22 14:00:40 -0700363 x = xfrm_state_construct(p, attrs, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 if (!x)
365 return err;
366
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700367 xfrm_state_hold(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 if (nlh->nlmsg_type == XFRM_MSG_NEWSA)
369 err = xfrm_state_add(x);
370 else
371 err = xfrm_state_update(x);
372
Joy Lattenab5f5e82007-09-17 11:51:22 -0700373 xfrm_audit_state_add(x, err ? 0 : 1, NETLINK_CB(skb).loginuid,
374 NETLINK_CB(skb).sid);
Joy Latten161a09e2006-11-27 13:11:54 -0600375
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 if (err < 0) {
377 x->km.state = XFRM_STATE_DEAD;
Herbert Xu21380b82006-02-22 14:47:13 -0800378 __xfrm_state_put(x);
Patrick McHardy7d6dfe12005-06-18 22:45:31 -0700379 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 }
381
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700382 c.seq = nlh->nlmsg_seq;
383 c.pid = nlh->nlmsg_pid;
Herbert Xuf60f6b82005-06-18 22:44:37 -0700384 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700385
386 km_state_notify(x, &c);
Patrick McHardy7d6dfe12005-06-18 22:45:31 -0700387out:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700388 xfrm_state_put(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 return err;
390}
391
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700392static struct xfrm_state *xfrm_user_state_lookup(struct xfrm_usersa_id *p,
Thomas Graf5424f322007-08-22 14:01:33 -0700393 struct nlattr **attrs,
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700394 int *errp)
395{
396 struct xfrm_state *x = NULL;
397 int err;
398
399 if (xfrm_id_proto_match(p->proto, IPSEC_PROTO_ANY)) {
400 err = -ESRCH;
401 x = xfrm_state_lookup(&p->daddr, p->spi, p->proto, p->family);
402 } else {
403 xfrm_address_t *saddr = NULL;
404
Thomas Graf35a7aa02007-08-22 14:00:40 -0700405 verify_one_addr(attrs, XFRMA_SRCADDR, &saddr);
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700406 if (!saddr) {
407 err = -EINVAL;
408 goto out;
409 }
410
Masahide NAKAMURA9abbffe2006-11-24 20:34:51 -0800411 err = -ESRCH;
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700412 x = xfrm_state_lookup_byaddr(&p->daddr, saddr, p->proto,
413 p->family);
414 }
415
416 out:
417 if (!x && errp)
418 *errp = err;
419 return x;
420}
421
Christoph Hellwig22e70052007-01-02 15:22:30 -0800422static int xfrm_del_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -0700423 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424{
425 struct xfrm_state *x;
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700426 int err = -ESRCH;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700427 struct km_event c;
Thomas Graf7b67c852007-08-22 13:53:52 -0700428 struct xfrm_usersa_id *p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429
Thomas Graf35a7aa02007-08-22 14:00:40 -0700430 x = xfrm_user_state_lookup(p, attrs, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 if (x == NULL)
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700432 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433
David S. Miller6f68dc32006-06-08 23:58:52 -0700434 if ((err = security_xfrm_state_delete(x)) != 0)
Catherine Zhangc8c05a82006-06-08 23:39:49 -0700435 goto out;
436
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 if (xfrm_state_kern(x)) {
Catherine Zhangc8c05a82006-06-08 23:39:49 -0700438 err = -EPERM;
439 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 }
441
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700442 err = xfrm_state_delete(x);
Joy Latten161a09e2006-11-27 13:11:54 -0600443
Catherine Zhangc8c05a82006-06-08 23:39:49 -0700444 if (err < 0)
445 goto out;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700446
447 c.seq = nlh->nlmsg_seq;
448 c.pid = nlh->nlmsg_pid;
Herbert Xuf60f6b82005-06-18 22:44:37 -0700449 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700450 km_state_notify(x, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451
Catherine Zhangc8c05a82006-06-08 23:39:49 -0700452out:
Joy Lattenab5f5e82007-09-17 11:51:22 -0700453 xfrm_audit_state_delete(x, err ? 0 : 1, NETLINK_CB(skb).loginuid,
454 NETLINK_CB(skb).sid);
Catherine Zhangc8c05a82006-06-08 23:39:49 -0700455 xfrm_state_put(x);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700456 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457}
458
459static void copy_to_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p)
460{
461 memcpy(&p->id, &x->id, sizeof(p->id));
462 memcpy(&p->sel, &x->sel, sizeof(p->sel));
463 memcpy(&p->lft, &x->lft, sizeof(p->lft));
464 memcpy(&p->curlft, &x->curlft, sizeof(p->curlft));
465 memcpy(&p->stats, &x->stats, sizeof(p->stats));
David S. Miller54489c142006-10-27 15:29:47 -0700466 memcpy(&p->saddr, &x->props.saddr, sizeof(p->saddr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 p->mode = x->props.mode;
468 p->replay_window = x->props.replay_window;
469 p->reqid = x->props.reqid;
470 p->family = x->props.family;
471 p->flags = x->props.flags;
472 p->seq = x->km.seq;
473}
474
475struct xfrm_dump_info {
476 struct sk_buff *in_skb;
477 struct sk_buff *out_skb;
478 u32 nlmsg_seq;
479 u16 nlmsg_flags;
480 int start_idx;
481 int this_idx;
482};
483
Thomas Grafc0144be2007-08-22 13:55:43 -0700484static int copy_sec_ctx(struct xfrm_sec_ctx *s, struct sk_buff *skb)
485{
Thomas Grafc0144be2007-08-22 13:55:43 -0700486 struct xfrm_user_sec_ctx *uctx;
487 struct nlattr *attr;
Herbert Xu68325d32007-10-09 13:30:57 -0700488 int ctx_size = sizeof(*uctx) + s->ctx_len;
Thomas Grafc0144be2007-08-22 13:55:43 -0700489
490 attr = nla_reserve(skb, XFRMA_SEC_CTX, ctx_size);
491 if (attr == NULL)
492 return -EMSGSIZE;
493
494 uctx = nla_data(attr);
495 uctx->exttype = XFRMA_SEC_CTX;
496 uctx->len = ctx_size;
497 uctx->ctx_doi = s->ctx_doi;
498 uctx->ctx_alg = s->ctx_alg;
499 uctx->ctx_len = s->ctx_len;
500 memcpy(uctx + 1, s->ctx_str, s->ctx_len);
501
502 return 0;
503}
504
Herbert Xu68325d32007-10-09 13:30:57 -0700505/* Don't change this without updating xfrm_sa_len! */
506static int copy_to_user_state_extra(struct xfrm_state *x,
507 struct xfrm_usersa_info *p,
508 struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509{
Herbert Xu050f0092007-10-09 13:31:47 -0700510 spin_lock_bh(&x->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 copy_to_user_state(x, p);
512
Herbert Xu050f0092007-10-09 13:31:47 -0700513 if (x->coaddr)
514 NLA_PUT(skb, XFRMA_COADDR, sizeof(*x->coaddr), x->coaddr);
515
516 if (x->lastused)
517 NLA_PUT_U64(skb, XFRMA_LASTUSED, x->lastused);
518 spin_unlock_bh(&x->lock);
519
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 if (x->aalg)
Thomas Grafc26445a2007-08-22 13:56:23 -0700521 NLA_PUT(skb, XFRMA_ALG_AUTH, alg_len(x->aalg), x->aalg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 if (x->ealg)
Thomas Grafc26445a2007-08-22 13:56:23 -0700523 NLA_PUT(skb, XFRMA_ALG_CRYPT, alg_len(x->ealg), x->ealg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 if (x->calg)
Thomas Grafc0144be2007-08-22 13:55:43 -0700525 NLA_PUT(skb, XFRMA_ALG_COMP, sizeof(*(x->calg)), x->calg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526
527 if (x->encap)
Thomas Grafc0144be2007-08-22 13:55:43 -0700528 NLA_PUT(skb, XFRMA_ENCAP, sizeof(*x->encap), x->encap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529
Thomas Grafc0144be2007-08-22 13:55:43 -0700530 if (x->security && copy_sec_ctx(x->security, skb) < 0)
531 goto nla_put_failure;
Noriaki TAKAMIYA060f02a2006-08-23 18:18:55 -0700532
Herbert Xu68325d32007-10-09 13:30:57 -0700533 return 0;
534
535nla_put_failure:
536 return -EMSGSIZE;
537}
538
539static int dump_one_state(struct xfrm_state *x, int count, void *ptr)
540{
541 struct xfrm_dump_info *sp = ptr;
542 struct sk_buff *in_skb = sp->in_skb;
543 struct sk_buff *skb = sp->out_skb;
544 struct xfrm_usersa_info *p;
545 struct nlmsghdr *nlh;
546 int err;
547
548 if (sp->this_idx < sp->start_idx)
549 goto out;
550
551 nlh = nlmsg_put(skb, NETLINK_CB(in_skb).pid, sp->nlmsg_seq,
552 XFRM_MSG_NEWSA, sizeof(*p), sp->nlmsg_flags);
553 if (nlh == NULL)
554 return -EMSGSIZE;
555
556 p = nlmsg_data(nlh);
557
558 err = copy_to_user_state_extra(x, p, skb);
559 if (err)
560 goto nla_put_failure;
561
Thomas Graf98250692007-08-22 12:47:26 -0700562 nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563out:
564 sp->this_idx++;
565 return 0;
566
Thomas Grafc0144be2007-08-22 13:55:43 -0700567nla_put_failure:
Thomas Graf98250692007-08-22 12:47:26 -0700568 nlmsg_cancel(skb, nlh);
Herbert Xu68325d32007-10-09 13:30:57 -0700569 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570}
571
572static int xfrm_dump_sa(struct sk_buff *skb, struct netlink_callback *cb)
573{
574 struct xfrm_dump_info info;
575
576 info.in_skb = cb->skb;
577 info.out_skb = skb;
578 info.nlmsg_seq = cb->nlh->nlmsg_seq;
579 info.nlmsg_flags = NLM_F_MULTI;
580 info.this_idx = 0;
581 info.start_idx = cb->args[0];
Masahide NAKAMURAdc00a522006-08-23 17:49:52 -0700582 (void) xfrm_state_walk(0, dump_one_state, &info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 cb->args[0] = info.this_idx;
584
585 return skb->len;
586}
587
588static struct sk_buff *xfrm_state_netlink(struct sk_buff *in_skb,
589 struct xfrm_state *x, u32 seq)
590{
591 struct xfrm_dump_info info;
592 struct sk_buff *skb;
593
Thomas Graf7deb2262007-08-22 13:57:39 -0700594 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 if (!skb)
596 return ERR_PTR(-ENOMEM);
597
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 info.in_skb = in_skb;
599 info.out_skb = skb;
600 info.nlmsg_seq = seq;
601 info.nlmsg_flags = 0;
602 info.this_idx = info.start_idx = 0;
603
604 if (dump_one_state(x, 0, &info)) {
605 kfree_skb(skb);
606 return NULL;
607 }
608
609 return skb;
610}
611
Thomas Graf7deb2262007-08-22 13:57:39 -0700612static inline size_t xfrm_spdinfo_msgsize(void)
613{
614 return NLMSG_ALIGN(4)
615 + nla_total_size(sizeof(struct xfrmu_spdinfo))
616 + nla_total_size(sizeof(struct xfrmu_spdhinfo));
617}
618
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700619static int build_spdinfo(struct sk_buff *skb, u32 pid, u32 seq, u32 flags)
620{
Jamal Hadi Salim5a6d3412007-05-04 12:55:39 -0700621 struct xfrmk_spdinfo si;
622 struct xfrmu_spdinfo spc;
623 struct xfrmu_spdhinfo sph;
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700624 struct nlmsghdr *nlh;
625 u32 *f;
626
627 nlh = nlmsg_put(skb, pid, seq, XFRM_MSG_NEWSPDINFO, sizeof(u32), 0);
628 if (nlh == NULL) /* shouldnt really happen ... */
629 return -EMSGSIZE;
630
631 f = nlmsg_data(nlh);
632 *f = flags;
633 xfrm_spd_getinfo(&si);
Jamal Hadi Salim5a6d3412007-05-04 12:55:39 -0700634 spc.incnt = si.incnt;
635 spc.outcnt = si.outcnt;
636 spc.fwdcnt = si.fwdcnt;
637 spc.inscnt = si.inscnt;
638 spc.outscnt = si.outscnt;
639 spc.fwdscnt = si.fwdscnt;
640 sph.spdhcnt = si.spdhcnt;
641 sph.spdhmcnt = si.spdhmcnt;
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700642
Jamal Hadi Salim5a6d3412007-05-04 12:55:39 -0700643 NLA_PUT(skb, XFRMA_SPD_INFO, sizeof(spc), &spc);
644 NLA_PUT(skb, XFRMA_SPD_HINFO, sizeof(sph), &sph);
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700645
646 return nlmsg_end(skb, nlh);
647
648nla_put_failure:
649 nlmsg_cancel(skb, nlh);
650 return -EMSGSIZE;
651}
652
653static int xfrm_get_spdinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -0700654 struct nlattr **attrs)
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700655{
656 struct sk_buff *r_skb;
Thomas Graf7b67c852007-08-22 13:53:52 -0700657 u32 *flags = nlmsg_data(nlh);
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700658 u32 spid = NETLINK_CB(skb).pid;
659 u32 seq = nlh->nlmsg_seq;
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700660
Thomas Graf7deb2262007-08-22 13:57:39 -0700661 r_skb = nlmsg_new(xfrm_spdinfo_msgsize(), GFP_ATOMIC);
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700662 if (r_skb == NULL)
663 return -ENOMEM;
664
665 if (build_spdinfo(r_skb, spid, seq, *flags) < 0)
666 BUG();
667
668 return nlmsg_unicast(xfrm_nl, r_skb, spid);
669}
670
Thomas Graf7deb2262007-08-22 13:57:39 -0700671static inline size_t xfrm_sadinfo_msgsize(void)
672{
673 return NLMSG_ALIGN(4)
674 + nla_total_size(sizeof(struct xfrmu_sadhinfo))
675 + nla_total_size(4); /* XFRMA_SAD_CNT */
676}
677
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700678static int build_sadinfo(struct sk_buff *skb, u32 pid, u32 seq, u32 flags)
679{
Jamal Hadi Salimaf11e312007-05-04 12:55:13 -0700680 struct xfrmk_sadinfo si;
681 struct xfrmu_sadhinfo sh;
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700682 struct nlmsghdr *nlh;
683 u32 *f;
684
685 nlh = nlmsg_put(skb, pid, seq, XFRM_MSG_NEWSADINFO, sizeof(u32), 0);
686 if (nlh == NULL) /* shouldnt really happen ... */
687 return -EMSGSIZE;
688
689 f = nlmsg_data(nlh);
690 *f = flags;
691 xfrm_sad_getinfo(&si);
692
Jamal Hadi Salimaf11e312007-05-04 12:55:13 -0700693 sh.sadhmcnt = si.sadhmcnt;
694 sh.sadhcnt = si.sadhcnt;
695
696 NLA_PUT_U32(skb, XFRMA_SAD_CNT, si.sadcnt);
697 NLA_PUT(skb, XFRMA_SAD_HINFO, sizeof(sh), &sh);
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700698
699 return nlmsg_end(skb, nlh);
700
701nla_put_failure:
702 nlmsg_cancel(skb, nlh);
703 return -EMSGSIZE;
704}
705
706static int xfrm_get_sadinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -0700707 struct nlattr **attrs)
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700708{
709 struct sk_buff *r_skb;
Thomas Graf7b67c852007-08-22 13:53:52 -0700710 u32 *flags = nlmsg_data(nlh);
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700711 u32 spid = NETLINK_CB(skb).pid;
712 u32 seq = nlh->nlmsg_seq;
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700713
Thomas Graf7deb2262007-08-22 13:57:39 -0700714 r_skb = nlmsg_new(xfrm_sadinfo_msgsize(), GFP_ATOMIC);
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700715 if (r_skb == NULL)
716 return -ENOMEM;
717
718 if (build_sadinfo(r_skb, spid, seq, *flags) < 0)
719 BUG();
720
721 return nlmsg_unicast(xfrm_nl, r_skb, spid);
722}
723
Christoph Hellwig22e70052007-01-02 15:22:30 -0800724static int xfrm_get_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -0700725 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726{
Thomas Graf7b67c852007-08-22 13:53:52 -0700727 struct xfrm_usersa_id *p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 struct xfrm_state *x;
729 struct sk_buff *resp_skb;
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700730 int err = -ESRCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731
Thomas Graf35a7aa02007-08-22 14:00:40 -0700732 x = xfrm_user_state_lookup(p, attrs, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 if (x == NULL)
734 goto out_noput;
735
736 resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
737 if (IS_ERR(resp_skb)) {
738 err = PTR_ERR(resp_skb);
739 } else {
Thomas Graf082a1ad2007-08-22 13:54:36 -0700740 err = nlmsg_unicast(xfrm_nl, resp_skb, NETLINK_CB(skb).pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 }
742 xfrm_state_put(x);
743out_noput:
744 return err;
745}
746
747static int verify_userspi_info(struct xfrm_userspi_info *p)
748{
749 switch (p->info.id.proto) {
750 case IPPROTO_AH:
751 case IPPROTO_ESP:
752 break;
753
754 case IPPROTO_COMP:
755 /* IPCOMP spi is 16-bits. */
756 if (p->max >= 0x10000)
757 return -EINVAL;
758 break;
759
760 default:
761 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700762 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763
764 if (p->min > p->max)
765 return -EINVAL;
766
767 return 0;
768}
769
Christoph Hellwig22e70052007-01-02 15:22:30 -0800770static int xfrm_alloc_userspi(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -0700771 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772{
773 struct xfrm_state *x;
774 struct xfrm_userspi_info *p;
775 struct sk_buff *resp_skb;
776 xfrm_address_t *daddr;
777 int family;
778 int err;
779
Thomas Graf7b67c852007-08-22 13:53:52 -0700780 p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 err = verify_userspi_info(p);
782 if (err)
783 goto out_noput;
784
785 family = p->info.family;
786 daddr = &p->info.id.daddr;
787
788 x = NULL;
789 if (p->info.seq) {
790 x = xfrm_find_acq_byseq(p->info.seq);
791 if (x && xfrm_addr_cmp(&x->id.daddr, daddr, family)) {
792 xfrm_state_put(x);
793 x = NULL;
794 }
795 }
796
797 if (!x)
798 x = xfrm_find_acq(p->info.mode, p->info.reqid,
799 p->info.id.proto, daddr,
800 &p->info.saddr, 1,
801 family);
802 err = -ENOENT;
803 if (x == NULL)
804 goto out_noput;
805
Herbert Xu658b2192007-10-09 13:29:52 -0700806 err = xfrm_alloc_spi(x, p->min, p->max);
807 if (err)
808 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809
Herbert Xu658b2192007-10-09 13:29:52 -0700810 resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 if (IS_ERR(resp_skb)) {
812 err = PTR_ERR(resp_skb);
813 goto out;
814 }
815
Thomas Graf082a1ad2007-08-22 13:54:36 -0700816 err = nlmsg_unicast(xfrm_nl, resp_skb, NETLINK_CB(skb).pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817
818out:
819 xfrm_state_put(x);
820out_noput:
821 return err;
822}
823
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -0800824static int verify_policy_dir(u8 dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825{
826 switch (dir) {
827 case XFRM_POLICY_IN:
828 case XFRM_POLICY_OUT:
829 case XFRM_POLICY_FWD:
830 break;
831
832 default:
833 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700834 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835
836 return 0;
837}
838
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -0800839static int verify_policy_type(u8 type)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -0700840{
841 switch (type) {
842 case XFRM_POLICY_TYPE_MAIN:
843#ifdef CONFIG_XFRM_SUB_POLICY
844 case XFRM_POLICY_TYPE_SUB:
845#endif
846 break;
847
848 default:
849 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700850 }
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -0700851
852 return 0;
853}
854
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855static int verify_newpolicy_info(struct xfrm_userpolicy_info *p)
856{
857 switch (p->share) {
858 case XFRM_SHARE_ANY:
859 case XFRM_SHARE_SESSION:
860 case XFRM_SHARE_USER:
861 case XFRM_SHARE_UNIQUE:
862 break;
863
864 default:
865 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700866 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867
868 switch (p->action) {
869 case XFRM_POLICY_ALLOW:
870 case XFRM_POLICY_BLOCK:
871 break;
872
873 default:
874 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700875 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876
877 switch (p->sel.family) {
878 case AF_INET:
879 break;
880
881 case AF_INET6:
882#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
883 break;
884#else
885 return -EAFNOSUPPORT;
886#endif
887
888 default:
889 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700890 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891
892 return verify_policy_dir(p->dir);
893}
894
Thomas Graf5424f322007-08-22 14:01:33 -0700895static int copy_from_user_sec_ctx(struct xfrm_policy *pol, struct nlattr **attrs)
Trent Jaegerdf718372005-12-13 23:12:27 -0800896{
Thomas Graf5424f322007-08-22 14:01:33 -0700897 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
Trent Jaegerdf718372005-12-13 23:12:27 -0800898 struct xfrm_user_sec_ctx *uctx;
899
900 if (!rt)
901 return 0;
902
Thomas Graf5424f322007-08-22 14:01:33 -0700903 uctx = nla_data(rt);
Trent Jaegerdf718372005-12-13 23:12:27 -0800904 return security_xfrm_policy_alloc(pol, uctx);
905}
906
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907static void copy_templates(struct xfrm_policy *xp, struct xfrm_user_tmpl *ut,
908 int nr)
909{
910 int i;
911
912 xp->xfrm_nr = nr;
913 for (i = 0; i < nr; i++, ut++) {
914 struct xfrm_tmpl *t = &xp->xfrm_vec[i];
915
916 memcpy(&t->id, &ut->id, sizeof(struct xfrm_id));
917 memcpy(&t->saddr, &ut->saddr,
918 sizeof(xfrm_address_t));
919 t->reqid = ut->reqid;
920 t->mode = ut->mode;
921 t->share = ut->share;
922 t->optional = ut->optional;
923 t->aalgos = ut->aalgos;
924 t->ealgos = ut->ealgos;
925 t->calgos = ut->calgos;
Miika Komu8511d012006-11-30 16:40:51 -0800926 t->encap_family = ut->family;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 }
928}
929
David S. Millerb4ad86bf2006-12-03 19:19:26 -0800930static int validate_tmpl(int nr, struct xfrm_user_tmpl *ut, u16 family)
931{
932 int i;
933
934 if (nr > XFRM_MAX_DEPTH)
935 return -EINVAL;
936
937 for (i = 0; i < nr; i++) {
938 /* We never validated the ut->family value, so many
939 * applications simply leave it at zero. The check was
940 * never made and ut->family was ignored because all
941 * templates could be assumed to have the same family as
942 * the policy itself. Now that we will have ipv4-in-ipv6
943 * and ipv6-in-ipv4 tunnels, this is no longer true.
944 */
945 if (!ut[i].family)
946 ut[i].family = family;
947
948 switch (ut[i].family) {
949 case AF_INET:
950 break;
951#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
952 case AF_INET6:
953 break;
954#endif
955 default:
956 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700957 }
David S. Millerb4ad86bf2006-12-03 19:19:26 -0800958 }
959
960 return 0;
961}
962
Thomas Graf5424f322007-08-22 14:01:33 -0700963static int copy_from_user_tmpl(struct xfrm_policy *pol, struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964{
Thomas Graf5424f322007-08-22 14:01:33 -0700965 struct nlattr *rt = attrs[XFRMA_TMPL];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966
967 if (!rt) {
968 pol->xfrm_nr = 0;
969 } else {
Thomas Graf5424f322007-08-22 14:01:33 -0700970 struct xfrm_user_tmpl *utmpl = nla_data(rt);
971 int nr = nla_len(rt) / sizeof(*utmpl);
David S. Millerb4ad86bf2006-12-03 19:19:26 -0800972 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973
David S. Millerb4ad86bf2006-12-03 19:19:26 -0800974 err = validate_tmpl(nr, utmpl, pol->family);
975 if (err)
976 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977
Thomas Graf5424f322007-08-22 14:01:33 -0700978 copy_templates(pol, utmpl, nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 }
980 return 0;
981}
982
Thomas Graf5424f322007-08-22 14:01:33 -0700983static int copy_from_user_policy_type(u8 *tp, struct nlattr **attrs)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -0700984{
Thomas Graf5424f322007-08-22 14:01:33 -0700985 struct nlattr *rt = attrs[XFRMA_POLICY_TYPE];
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -0700986 struct xfrm_userpolicy_type *upt;
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -0800987 u8 type = XFRM_POLICY_TYPE_MAIN;
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -0700988 int err;
989
990 if (rt) {
Thomas Graf5424f322007-08-22 14:01:33 -0700991 upt = nla_data(rt);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -0700992 type = upt->type;
993 }
994
995 err = verify_policy_type(type);
996 if (err)
997 return err;
998
999 *tp = type;
1000 return 0;
1001}
1002
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003static void copy_from_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p)
1004{
1005 xp->priority = p->priority;
1006 xp->index = p->index;
1007 memcpy(&xp->selector, &p->sel, sizeof(xp->selector));
1008 memcpy(&xp->lft, &p->lft, sizeof(xp->lft));
1009 xp->action = p->action;
1010 xp->flags = p->flags;
1011 xp->family = p->sel.family;
1012 /* XXX xp->share = p->share; */
1013}
1014
1015static void copy_to_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p, int dir)
1016{
1017 memcpy(&p->sel, &xp->selector, sizeof(p->sel));
1018 memcpy(&p->lft, &xp->lft, sizeof(p->lft));
1019 memcpy(&p->curlft, &xp->curlft, sizeof(p->curlft));
1020 p->priority = xp->priority;
1021 p->index = xp->index;
1022 p->sel.family = xp->family;
1023 p->dir = dir;
1024 p->action = xp->action;
1025 p->flags = xp->flags;
1026 p->share = XFRM_SHARE_ANY; /* XXX xp->share */
1027}
1028
Thomas Graf5424f322007-08-22 14:01:33 -07001029static struct xfrm_policy *xfrm_policy_construct(struct xfrm_userpolicy_info *p, struct nlattr **attrs, int *errp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030{
1031 struct xfrm_policy *xp = xfrm_policy_alloc(GFP_KERNEL);
1032 int err;
1033
1034 if (!xp) {
1035 *errp = -ENOMEM;
1036 return NULL;
1037 }
1038
1039 copy_from_user_policy(xp, p);
Trent Jaegerdf718372005-12-13 23:12:27 -08001040
Thomas Graf35a7aa02007-08-22 14:00:40 -07001041 err = copy_from_user_policy_type(&xp->type, attrs);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001042 if (err)
1043 goto error;
1044
Thomas Graf35a7aa02007-08-22 14:00:40 -07001045 if (!(err = copy_from_user_tmpl(xp, attrs)))
1046 err = copy_from_user_sec_ctx(xp, attrs);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001047 if (err)
1048 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049
1050 return xp;
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001051 error:
1052 *errp = err;
1053 kfree(xp);
1054 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055}
1056
Christoph Hellwig22e70052007-01-02 15:22:30 -08001057static int xfrm_add_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001058 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059{
Thomas Graf7b67c852007-08-22 13:53:52 -07001060 struct xfrm_userpolicy_info *p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 struct xfrm_policy *xp;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001062 struct km_event c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 int err;
1064 int excl;
1065
1066 err = verify_newpolicy_info(p);
1067 if (err)
1068 return err;
Thomas Graf35a7aa02007-08-22 14:00:40 -07001069 err = verify_sec_ctx_len(attrs);
Trent Jaegerdf718372005-12-13 23:12:27 -08001070 if (err)
1071 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072
Thomas Graf35a7aa02007-08-22 14:00:40 -07001073 xp = xfrm_policy_construct(p, attrs, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 if (!xp)
1075 return err;
1076
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001077 /* shouldnt excl be based on nlh flags??
1078 * Aha! this is anti-netlink really i.e more pfkey derived
1079 * in netlink excl is a flag and you wouldnt need
1080 * a type XFRM_MSG_UPDPOLICY - JHS */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 excl = nlh->nlmsg_type == XFRM_MSG_NEWPOLICY;
1082 err = xfrm_policy_insert(p->dir, xp, excl);
Joy Lattenab5f5e82007-09-17 11:51:22 -07001083 xfrm_audit_policy_add(xp, err ? 0 : 1, NETLINK_CB(skb).loginuid,
1084 NETLINK_CB(skb).sid);
Joy Latten161a09e2006-11-27 13:11:54 -06001085
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 if (err) {
Trent Jaeger5f8ac642006-01-06 13:22:39 -08001087 security_xfrm_policy_free(xp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 kfree(xp);
1089 return err;
1090 }
1091
Herbert Xuf60f6b82005-06-18 22:44:37 -07001092 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001093 c.seq = nlh->nlmsg_seq;
1094 c.pid = nlh->nlmsg_pid;
1095 km_policy_notify(xp, p->dir, &c);
1096
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 xfrm_pol_put(xp);
1098
1099 return 0;
1100}
1101
1102static int copy_to_user_tmpl(struct xfrm_policy *xp, struct sk_buff *skb)
1103{
1104 struct xfrm_user_tmpl vec[XFRM_MAX_DEPTH];
1105 int i;
1106
1107 if (xp->xfrm_nr == 0)
1108 return 0;
1109
1110 for (i = 0; i < xp->xfrm_nr; i++) {
1111 struct xfrm_user_tmpl *up = &vec[i];
1112 struct xfrm_tmpl *kp = &xp->xfrm_vec[i];
1113
1114 memcpy(&up->id, &kp->id, sizeof(up->id));
Miika Komu8511d012006-11-30 16:40:51 -08001115 up->family = kp->encap_family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 memcpy(&up->saddr, &kp->saddr, sizeof(up->saddr));
1117 up->reqid = kp->reqid;
1118 up->mode = kp->mode;
1119 up->share = kp->share;
1120 up->optional = kp->optional;
1121 up->aalgos = kp->aalgos;
1122 up->ealgos = kp->ealgos;
1123 up->calgos = kp->calgos;
1124 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125
Thomas Grafc0144be2007-08-22 13:55:43 -07001126 return nla_put(skb, XFRMA_TMPL,
1127 sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr, vec);
Trent Jaegerdf718372005-12-13 23:12:27 -08001128}
1129
Serge Hallyn0d681622006-07-24 23:30:44 -07001130static inline int copy_to_user_state_sec_ctx(struct xfrm_state *x, struct sk_buff *skb)
1131{
1132 if (x->security) {
1133 return copy_sec_ctx(x->security, skb);
1134 }
1135 return 0;
1136}
1137
1138static inline int copy_to_user_sec_ctx(struct xfrm_policy *xp, struct sk_buff *skb)
1139{
1140 if (xp->security) {
1141 return copy_sec_ctx(xp->security, skb);
1142 }
1143 return 0;
1144}
Thomas Grafcfbfd452007-08-22 13:57:04 -07001145static inline size_t userpolicy_type_attrsize(void)
1146{
1147#ifdef CONFIG_XFRM_SUB_POLICY
1148 return nla_total_size(sizeof(struct xfrm_userpolicy_type));
1149#else
1150 return 0;
1151#endif
1152}
Serge Hallyn0d681622006-07-24 23:30:44 -07001153
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001154#ifdef CONFIG_XFRM_SUB_POLICY
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001155static int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001156{
Thomas Grafc0144be2007-08-22 13:55:43 -07001157 struct xfrm_userpolicy_type upt = {
1158 .type = type,
1159 };
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001160
Thomas Grafc0144be2007-08-22 13:55:43 -07001161 return nla_put(skb, XFRMA_POLICY_TYPE, sizeof(upt), &upt);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001162}
1163
1164#else
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001165static inline int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001166{
1167 return 0;
1168}
1169#endif
1170
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171static int dump_one_policy(struct xfrm_policy *xp, int dir, int count, void *ptr)
1172{
1173 struct xfrm_dump_info *sp = ptr;
1174 struct xfrm_userpolicy_info *p;
1175 struct sk_buff *in_skb = sp->in_skb;
1176 struct sk_buff *skb = sp->out_skb;
1177 struct nlmsghdr *nlh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178
1179 if (sp->this_idx < sp->start_idx)
1180 goto out;
1181
Thomas Graf79b8b7f2007-08-22 12:46:53 -07001182 nlh = nlmsg_put(skb, NETLINK_CB(in_skb).pid, sp->nlmsg_seq,
1183 XFRM_MSG_NEWPOLICY, sizeof(*p), sp->nlmsg_flags);
1184 if (nlh == NULL)
1185 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186
Thomas Graf7b67c852007-08-22 13:53:52 -07001187 p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188 copy_to_user_policy(xp, p, dir);
1189 if (copy_to_user_tmpl(xp, skb) < 0)
1190 goto nlmsg_failure;
Trent Jaegerdf718372005-12-13 23:12:27 -08001191 if (copy_to_user_sec_ctx(xp, skb))
1192 goto nlmsg_failure;
Jamal Hadi Salim1459bb32006-11-20 16:51:22 -08001193 if (copy_to_user_policy_type(xp->type, skb) < 0)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001194 goto nlmsg_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195
Thomas Graf98250692007-08-22 12:47:26 -07001196 nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197out:
1198 sp->this_idx++;
1199 return 0;
1200
1201nlmsg_failure:
Thomas Graf98250692007-08-22 12:47:26 -07001202 nlmsg_cancel(skb, nlh);
1203 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204}
1205
1206static int xfrm_dump_policy(struct sk_buff *skb, struct netlink_callback *cb)
1207{
1208 struct xfrm_dump_info info;
1209
1210 info.in_skb = cb->skb;
1211 info.out_skb = skb;
1212 info.nlmsg_seq = cb->nlh->nlmsg_seq;
1213 info.nlmsg_flags = NLM_F_MULTI;
1214 info.this_idx = 0;
1215 info.start_idx = cb->args[0];
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001216 (void) xfrm_policy_walk(XFRM_POLICY_TYPE_MAIN, dump_one_policy, &info);
1217#ifdef CONFIG_XFRM_SUB_POLICY
1218 (void) xfrm_policy_walk(XFRM_POLICY_TYPE_SUB, dump_one_policy, &info);
1219#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 cb->args[0] = info.this_idx;
1221
1222 return skb->len;
1223}
1224
1225static struct sk_buff *xfrm_policy_netlink(struct sk_buff *in_skb,
1226 struct xfrm_policy *xp,
1227 int dir, u32 seq)
1228{
1229 struct xfrm_dump_info info;
1230 struct sk_buff *skb;
1231
Thomas Graf7deb2262007-08-22 13:57:39 -07001232 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 if (!skb)
1234 return ERR_PTR(-ENOMEM);
1235
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 info.in_skb = in_skb;
1237 info.out_skb = skb;
1238 info.nlmsg_seq = seq;
1239 info.nlmsg_flags = 0;
1240 info.this_idx = info.start_idx = 0;
1241
1242 if (dump_one_policy(xp, dir, 0, &info) < 0) {
1243 kfree_skb(skb);
1244 return NULL;
1245 }
1246
1247 return skb;
1248}
1249
Christoph Hellwig22e70052007-01-02 15:22:30 -08001250static int xfrm_get_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001251 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252{
1253 struct xfrm_policy *xp;
1254 struct xfrm_userpolicy_id *p;
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001255 u8 type = XFRM_POLICY_TYPE_MAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 int err;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001257 struct km_event c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 int delete;
1259
Thomas Graf7b67c852007-08-22 13:53:52 -07001260 p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261 delete = nlh->nlmsg_type == XFRM_MSG_DELPOLICY;
1262
Thomas Graf35a7aa02007-08-22 14:00:40 -07001263 err = copy_from_user_policy_type(&type, attrs);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001264 if (err)
1265 return err;
1266
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267 err = verify_policy_dir(p->dir);
1268 if (err)
1269 return err;
1270
1271 if (p->index)
Eric Parisef41aaa2007-03-07 15:37:58 -08001272 xp = xfrm_policy_byid(type, p->dir, p->index, delete, &err);
Trent Jaegerdf718372005-12-13 23:12:27 -08001273 else {
Thomas Graf5424f322007-08-22 14:01:33 -07001274 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
Trent Jaegerdf718372005-12-13 23:12:27 -08001275 struct xfrm_policy tmp;
1276
Thomas Graf35a7aa02007-08-22 14:00:40 -07001277 err = verify_sec_ctx_len(attrs);
Trent Jaegerdf718372005-12-13 23:12:27 -08001278 if (err)
1279 return err;
1280
1281 memset(&tmp, 0, sizeof(struct xfrm_policy));
1282 if (rt) {
Thomas Graf5424f322007-08-22 14:01:33 -07001283 struct xfrm_user_sec_ctx *uctx = nla_data(rt);
Trent Jaegerdf718372005-12-13 23:12:27 -08001284
1285 if ((err = security_xfrm_policy_alloc(&tmp, uctx)))
1286 return err;
1287 }
Eric Parisef41aaa2007-03-07 15:37:58 -08001288 xp = xfrm_policy_bysel_ctx(type, p->dir, &p->sel, tmp.security,
1289 delete, &err);
Trent Jaegerdf718372005-12-13 23:12:27 -08001290 security_xfrm_policy_free(&tmp);
1291 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292 if (xp == NULL)
1293 return -ENOENT;
1294
1295 if (!delete) {
1296 struct sk_buff *resp_skb;
1297
1298 resp_skb = xfrm_policy_netlink(skb, xp, p->dir, nlh->nlmsg_seq);
1299 if (IS_ERR(resp_skb)) {
1300 err = PTR_ERR(resp_skb);
1301 } else {
Thomas Graf082a1ad2007-08-22 13:54:36 -07001302 err = nlmsg_unicast(xfrm_nl, resp_skb,
1303 NETLINK_CB(skb).pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 }
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001305 } else {
Joy Lattenab5f5e82007-09-17 11:51:22 -07001306 xfrm_audit_policy_delete(xp, err ? 0 : 1,
1307 NETLINK_CB(skb).loginuid,
1308 NETLINK_CB(skb).sid);
David S. Miller13fcfbb02007-02-12 13:53:54 -08001309
1310 if (err != 0)
Catherine Zhangc8c05a82006-06-08 23:39:49 -07001311 goto out;
David S. Miller13fcfbb02007-02-12 13:53:54 -08001312
Herbert Xue7443892005-06-18 22:44:18 -07001313 c.data.byid = p->index;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001314 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001315 c.seq = nlh->nlmsg_seq;
1316 c.pid = nlh->nlmsg_pid;
1317 km_policy_notify(xp, p->dir, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318 }
1319
Catherine Zhangc8c05a82006-06-08 23:39:49 -07001320out:
Eric Parisef41aaa2007-03-07 15:37:58 -08001321 xfrm_pol_put(xp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322 return err;
1323}
1324
Christoph Hellwig22e70052007-01-02 15:22:30 -08001325static int xfrm_flush_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001326 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327{
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001328 struct km_event c;
Thomas Graf7b67c852007-08-22 13:53:52 -07001329 struct xfrm_usersa_flush *p = nlmsg_data(nlh);
Joy Latten161a09e2006-11-27 13:11:54 -06001330 struct xfrm_audit audit_info;
Joy Latten4aa2e622007-06-04 19:05:57 -04001331 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332
Joy Latten161a09e2006-11-27 13:11:54 -06001333 audit_info.loginuid = NETLINK_CB(skb).loginuid;
1334 audit_info.secid = NETLINK_CB(skb).sid;
Joy Latten4aa2e622007-06-04 19:05:57 -04001335 err = xfrm_state_flush(p->proto, &audit_info);
1336 if (err)
1337 return err;
Herbert Xubf088672005-06-18 22:44:00 -07001338 c.data.proto = p->proto;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001339 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001340 c.seq = nlh->nlmsg_seq;
1341 c.pid = nlh->nlmsg_pid;
1342 km_state_notify(NULL, &c);
1343
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344 return 0;
1345}
1346
Thomas Graf7deb2262007-08-22 13:57:39 -07001347static inline size_t xfrm_aevent_msgsize(void)
1348{
1349 return NLMSG_ALIGN(sizeof(struct xfrm_aevent_id))
1350 + nla_total_size(sizeof(struct xfrm_replay_state))
1351 + nla_total_size(sizeof(struct xfrm_lifetime_cur))
1352 + nla_total_size(4) /* XFRM_AE_RTHR */
1353 + nla_total_size(4); /* XFRM_AE_ETHR */
1354}
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001355
1356static int build_aevent(struct sk_buff *skb, struct xfrm_state *x, struct km_event *c)
1357{
1358 struct xfrm_aevent_id *id;
1359 struct nlmsghdr *nlh;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001360
Thomas Graf79b8b7f2007-08-22 12:46:53 -07001361 nlh = nlmsg_put(skb, c->pid, c->seq, XFRM_MSG_NEWAE, sizeof(*id), 0);
1362 if (nlh == NULL)
1363 return -EMSGSIZE;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001364
Thomas Graf7b67c852007-08-22 13:53:52 -07001365 id = nlmsg_data(nlh);
Jamal Hadi Salim2b5f6dc2006-12-02 22:22:25 -08001366 memcpy(&id->sa_id.daddr, &x->id.daddr,sizeof(x->id.daddr));
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001367 id->sa_id.spi = x->id.spi;
1368 id->sa_id.family = x->props.family;
1369 id->sa_id.proto = x->id.proto;
Jamal Hadi Salim2b5f6dc2006-12-02 22:22:25 -08001370 memcpy(&id->saddr, &x->props.saddr,sizeof(x->props.saddr));
1371 id->reqid = x->props.reqid;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001372 id->flags = c->data.aevent;
1373
Thomas Grafc0144be2007-08-22 13:55:43 -07001374 NLA_PUT(skb, XFRMA_REPLAY_VAL, sizeof(x->replay), &x->replay);
1375 NLA_PUT(skb, XFRMA_LTIME_VAL, sizeof(x->curlft), &x->curlft);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001376
Thomas Grafc0144be2007-08-22 13:55:43 -07001377 if (id->flags & XFRM_AE_RTHR)
1378 NLA_PUT_U32(skb, XFRMA_REPLAY_THRESH, x->replay_maxdiff);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001379
Thomas Grafc0144be2007-08-22 13:55:43 -07001380 if (id->flags & XFRM_AE_ETHR)
1381 NLA_PUT_U32(skb, XFRMA_ETIMER_THRESH,
1382 x->replay_maxage * 10 / HZ);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001383
Thomas Graf98250692007-08-22 12:47:26 -07001384 return nlmsg_end(skb, nlh);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001385
Thomas Grafc0144be2007-08-22 13:55:43 -07001386nla_put_failure:
Thomas Graf98250692007-08-22 12:47:26 -07001387 nlmsg_cancel(skb, nlh);
1388 return -EMSGSIZE;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001389}
1390
Christoph Hellwig22e70052007-01-02 15:22:30 -08001391static int xfrm_get_ae(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001392 struct nlattr **attrs)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001393{
1394 struct xfrm_state *x;
1395 struct sk_buff *r_skb;
1396 int err;
1397 struct km_event c;
Thomas Graf7b67c852007-08-22 13:53:52 -07001398 struct xfrm_aevent_id *p = nlmsg_data(nlh);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001399 struct xfrm_usersa_id *id = &p->sa_id;
1400
Thomas Graf7deb2262007-08-22 13:57:39 -07001401 r_skb = nlmsg_new(xfrm_aevent_msgsize(), GFP_ATOMIC);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001402 if (r_skb == NULL)
1403 return -ENOMEM;
1404
1405 x = xfrm_state_lookup(&id->daddr, id->spi, id->proto, id->family);
1406 if (x == NULL) {
Patrick McHardyb08d5842007-02-27 09:57:37 -08001407 kfree_skb(r_skb);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001408 return -ESRCH;
1409 }
1410
1411 /*
1412 * XXX: is this lock really needed - none of the other
1413 * gets lock (the concern is things getting updated
1414 * while we are still reading) - jhs
1415 */
1416 spin_lock_bh(&x->lock);
1417 c.data.aevent = p->flags;
1418 c.seq = nlh->nlmsg_seq;
1419 c.pid = nlh->nlmsg_pid;
1420
1421 if (build_aevent(r_skb, x, &c) < 0)
1422 BUG();
Thomas Graf082a1ad2007-08-22 13:54:36 -07001423 err = nlmsg_unicast(xfrm_nl, r_skb, NETLINK_CB(skb).pid);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001424 spin_unlock_bh(&x->lock);
1425 xfrm_state_put(x);
1426 return err;
1427}
1428
Christoph Hellwig22e70052007-01-02 15:22:30 -08001429static int xfrm_new_ae(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001430 struct nlattr **attrs)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001431{
1432 struct xfrm_state *x;
1433 struct km_event c;
1434 int err = - EINVAL;
Thomas Graf7b67c852007-08-22 13:53:52 -07001435 struct xfrm_aevent_id *p = nlmsg_data(nlh);
Thomas Graf5424f322007-08-22 14:01:33 -07001436 struct nlattr *rp = attrs[XFRMA_REPLAY_VAL];
1437 struct nlattr *lt = attrs[XFRMA_LTIME_VAL];
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001438
1439 if (!lt && !rp)
1440 return err;
1441
1442 /* pedantic mode - thou shalt sayeth replaceth */
1443 if (!(nlh->nlmsg_flags&NLM_F_REPLACE))
1444 return err;
1445
1446 x = xfrm_state_lookup(&p->sa_id.daddr, p->sa_id.spi, p->sa_id.proto, p->sa_id.family);
1447 if (x == NULL)
1448 return -ESRCH;
1449
1450 if (x->km.state != XFRM_STATE_VALID)
1451 goto out;
1452
1453 spin_lock_bh(&x->lock);
Thomas Graf35a7aa02007-08-22 14:00:40 -07001454 xfrm_update_ae_params(x, attrs);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001455 spin_unlock_bh(&x->lock);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001456
1457 c.event = nlh->nlmsg_type;
1458 c.seq = nlh->nlmsg_seq;
1459 c.pid = nlh->nlmsg_pid;
1460 c.data.aevent = XFRM_AE_CU;
1461 km_state_notify(x, &c);
1462 err = 0;
1463out:
1464 xfrm_state_put(x);
1465 return err;
1466}
1467
Christoph Hellwig22e70052007-01-02 15:22:30 -08001468static int xfrm_flush_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001469 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470{
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001471 struct km_event c;
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001472 u8 type = XFRM_POLICY_TYPE_MAIN;
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001473 int err;
Joy Latten161a09e2006-11-27 13:11:54 -06001474 struct xfrm_audit audit_info;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001475
Thomas Graf35a7aa02007-08-22 14:00:40 -07001476 err = copy_from_user_policy_type(&type, attrs);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001477 if (err)
1478 return err;
1479
Joy Latten161a09e2006-11-27 13:11:54 -06001480 audit_info.loginuid = NETLINK_CB(skb).loginuid;
1481 audit_info.secid = NETLINK_CB(skb).sid;
Joy Latten4aa2e622007-06-04 19:05:57 -04001482 err = xfrm_policy_flush(type, &audit_info);
1483 if (err)
1484 return err;
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001485 c.data.type = type;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001486 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001487 c.seq = nlh->nlmsg_seq;
1488 c.pid = nlh->nlmsg_pid;
1489 km_policy_notify(NULL, 0, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490 return 0;
1491}
1492
Christoph Hellwig22e70052007-01-02 15:22:30 -08001493static int xfrm_add_pol_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001494 struct nlattr **attrs)
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001495{
1496 struct xfrm_policy *xp;
Thomas Graf7b67c852007-08-22 13:53:52 -07001497 struct xfrm_user_polexpire *up = nlmsg_data(nlh);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001498 struct xfrm_userpolicy_info *p = &up->pol;
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001499 u8 type = XFRM_POLICY_TYPE_MAIN;
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001500 int err = -ENOENT;
1501
Thomas Graf35a7aa02007-08-22 14:00:40 -07001502 err = copy_from_user_policy_type(&type, attrs);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001503 if (err)
1504 return err;
1505
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001506 if (p->index)
Eric Parisef41aaa2007-03-07 15:37:58 -08001507 xp = xfrm_policy_byid(type, p->dir, p->index, 0, &err);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001508 else {
Thomas Graf5424f322007-08-22 14:01:33 -07001509 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001510 struct xfrm_policy tmp;
1511
Thomas Graf35a7aa02007-08-22 14:00:40 -07001512 err = verify_sec_ctx_len(attrs);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001513 if (err)
1514 return err;
1515
1516 memset(&tmp, 0, sizeof(struct xfrm_policy));
1517 if (rt) {
Thomas Graf5424f322007-08-22 14:01:33 -07001518 struct xfrm_user_sec_ctx *uctx = nla_data(rt);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001519
1520 if ((err = security_xfrm_policy_alloc(&tmp, uctx)))
1521 return err;
1522 }
Eric Parisef41aaa2007-03-07 15:37:58 -08001523 xp = xfrm_policy_bysel_ctx(type, p->dir, &p->sel, tmp.security,
1524 0, &err);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001525 security_xfrm_policy_free(&tmp);
1526 }
1527
1528 if (xp == NULL)
Eric Parisef41aaa2007-03-07 15:37:58 -08001529 return -ENOENT;
1530 read_lock(&xp->lock);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001531 if (xp->dead) {
1532 read_unlock(&xp->lock);
1533 goto out;
1534 }
1535
1536 read_unlock(&xp->lock);
1537 err = 0;
1538 if (up->hard) {
1539 xfrm_policy_delete(xp, p->dir);
Joy Lattenab5f5e82007-09-17 11:51:22 -07001540 xfrm_audit_policy_delete(xp, 1, NETLINK_CB(skb).loginuid,
1541 NETLINK_CB(skb).sid);
Joy Latten161a09e2006-11-27 13:11:54 -06001542
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001543 } else {
1544 // reset the timers here?
1545 printk("Dont know what to do with soft policy expire\n");
1546 }
1547 km_policy_expired(xp, p->dir, up->hard, current->pid);
1548
1549out:
1550 xfrm_pol_put(xp);
1551 return err;
1552}
1553
Christoph Hellwig22e70052007-01-02 15:22:30 -08001554static int xfrm_add_sa_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001555 struct nlattr **attrs)
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001556{
1557 struct xfrm_state *x;
1558 int err;
Thomas Graf7b67c852007-08-22 13:53:52 -07001559 struct xfrm_user_expire *ue = nlmsg_data(nlh);
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001560 struct xfrm_usersa_info *p = &ue->state;
1561
1562 x = xfrm_state_lookup(&p->id.daddr, p->id.spi, p->id.proto, p->family);
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001563
David S. Miller3a765aa2007-02-26 14:52:21 -08001564 err = -ENOENT;
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001565 if (x == NULL)
1566 return err;
1567
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001568 spin_lock_bh(&x->lock);
David S. Miller3a765aa2007-02-26 14:52:21 -08001569 err = -EINVAL;
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001570 if (x->km.state != XFRM_STATE_VALID)
1571 goto out;
1572 km_state_expired(x, ue->hard, current->pid);
1573
Joy Latten161a09e2006-11-27 13:11:54 -06001574 if (ue->hard) {
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001575 __xfrm_state_delete(x);
Joy Lattenab5f5e82007-09-17 11:51:22 -07001576 xfrm_audit_state_delete(x, 1, NETLINK_CB(skb).loginuid,
1577 NETLINK_CB(skb).sid);
Joy Latten161a09e2006-11-27 13:11:54 -06001578 }
David S. Miller3a765aa2007-02-26 14:52:21 -08001579 err = 0;
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001580out:
1581 spin_unlock_bh(&x->lock);
1582 xfrm_state_put(x);
1583 return err;
1584}
1585
Christoph Hellwig22e70052007-01-02 15:22:30 -08001586static int xfrm_add_acquire(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001587 struct nlattr **attrs)
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001588{
1589 struct xfrm_policy *xp;
1590 struct xfrm_user_tmpl *ut;
1591 int i;
Thomas Graf5424f322007-08-22 14:01:33 -07001592 struct nlattr *rt = attrs[XFRMA_TMPL];
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001593
Thomas Graf7b67c852007-08-22 13:53:52 -07001594 struct xfrm_user_acquire *ua = nlmsg_data(nlh);
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001595 struct xfrm_state *x = xfrm_state_alloc();
1596 int err = -ENOMEM;
1597
1598 if (!x)
1599 return err;
1600
1601 err = verify_newpolicy_info(&ua->policy);
1602 if (err) {
1603 printk("BAD policy passed\n");
1604 kfree(x);
1605 return err;
1606 }
1607
1608 /* build an XP */
Thomas Graf5424f322007-08-22 14:01:33 -07001609 xp = xfrm_policy_construct(&ua->policy, attrs, &err);
David S. Millerb4ad86bf2006-12-03 19:19:26 -08001610 if (!xp) {
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001611 kfree(x);
1612 return err;
1613 }
1614
1615 memcpy(&x->id, &ua->id, sizeof(ua->id));
1616 memcpy(&x->props.saddr, &ua->saddr, sizeof(ua->saddr));
1617 memcpy(&x->sel, &ua->sel, sizeof(ua->sel));
1618
Thomas Graf5424f322007-08-22 14:01:33 -07001619 ut = nla_data(rt);
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001620 /* extract the templates and for each call km_key */
1621 for (i = 0; i < xp->xfrm_nr; i++, ut++) {
1622 struct xfrm_tmpl *t = &xp->xfrm_vec[i];
1623 memcpy(&x->id, &t->id, sizeof(x->id));
1624 x->props.mode = t->mode;
1625 x->props.reqid = t->reqid;
1626 x->props.family = ut->family;
1627 t->aalgos = ua->aalgos;
1628 t->ealgos = ua->ealgos;
1629 t->calgos = ua->calgos;
1630 err = km_query(x, t, xp);
1631
1632 }
1633
1634 kfree(x);
1635 kfree(xp);
1636
1637 return 0;
1638}
1639
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001640#ifdef CONFIG_XFRM_MIGRATE
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001641static int copy_from_user_migrate(struct xfrm_migrate *ma,
Thomas Graf5424f322007-08-22 14:01:33 -07001642 struct nlattr **attrs, int *num)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001643{
Thomas Graf5424f322007-08-22 14:01:33 -07001644 struct nlattr *rt = attrs[XFRMA_MIGRATE];
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001645 struct xfrm_user_migrate *um;
1646 int i, num_migrate;
1647
Thomas Graf5424f322007-08-22 14:01:33 -07001648 um = nla_data(rt);
1649 num_migrate = nla_len(rt) / sizeof(*um);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001650
1651 if (num_migrate <= 0 || num_migrate > XFRM_MAX_DEPTH)
1652 return -EINVAL;
1653
1654 for (i = 0; i < num_migrate; i++, um++, ma++) {
1655 memcpy(&ma->old_daddr, &um->old_daddr, sizeof(ma->old_daddr));
1656 memcpy(&ma->old_saddr, &um->old_saddr, sizeof(ma->old_saddr));
1657 memcpy(&ma->new_daddr, &um->new_daddr, sizeof(ma->new_daddr));
1658 memcpy(&ma->new_saddr, &um->new_saddr, sizeof(ma->new_saddr));
1659
1660 ma->proto = um->proto;
1661 ma->mode = um->mode;
1662 ma->reqid = um->reqid;
1663
1664 ma->old_family = um->old_family;
1665 ma->new_family = um->new_family;
1666 }
1667
1668 *num = i;
1669 return 0;
1670}
1671
1672static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001673 struct nlattr **attrs)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001674{
Thomas Graf7b67c852007-08-22 13:53:52 -07001675 struct xfrm_userpolicy_id *pi = nlmsg_data(nlh);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001676 struct xfrm_migrate m[XFRM_MAX_DEPTH];
1677 u8 type;
1678 int err;
1679 int n = 0;
1680
Thomas Graf35a7aa02007-08-22 14:00:40 -07001681 if (attrs[XFRMA_MIGRATE] == NULL)
Thomas Grafcf5cb792007-08-22 13:59:04 -07001682 return -EINVAL;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001683
Thomas Graf5424f322007-08-22 14:01:33 -07001684 err = copy_from_user_policy_type(&type, attrs);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001685 if (err)
1686 return err;
1687
1688 err = copy_from_user_migrate((struct xfrm_migrate *)m,
Thomas Graf5424f322007-08-22 14:01:33 -07001689 attrs, &n);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001690 if (err)
1691 return err;
1692
1693 if (!n)
1694 return 0;
1695
1696 xfrm_migrate(&pi->sel, pi->dir, type, m, n);
1697
1698 return 0;
1699}
1700#else
1701static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001702 struct nlattr **attrs)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001703{
1704 return -ENOPROTOOPT;
1705}
1706#endif
1707
1708#ifdef CONFIG_XFRM_MIGRATE
1709static int copy_to_user_migrate(struct xfrm_migrate *m, struct sk_buff *skb)
1710{
1711 struct xfrm_user_migrate um;
1712
1713 memset(&um, 0, sizeof(um));
1714 um.proto = m->proto;
1715 um.mode = m->mode;
1716 um.reqid = m->reqid;
1717 um.old_family = m->old_family;
1718 memcpy(&um.old_daddr, &m->old_daddr, sizeof(um.old_daddr));
1719 memcpy(&um.old_saddr, &m->old_saddr, sizeof(um.old_saddr));
1720 um.new_family = m->new_family;
1721 memcpy(&um.new_daddr, &m->new_daddr, sizeof(um.new_daddr));
1722 memcpy(&um.new_saddr, &m->new_saddr, sizeof(um.new_saddr));
1723
Thomas Grafc0144be2007-08-22 13:55:43 -07001724 return nla_put(skb, XFRMA_MIGRATE, sizeof(um), &um);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001725}
1726
Thomas Graf7deb2262007-08-22 13:57:39 -07001727static inline size_t xfrm_migrate_msgsize(int num_migrate)
1728{
1729 return NLMSG_ALIGN(sizeof(struct xfrm_userpolicy_id))
1730 + nla_total_size(sizeof(struct xfrm_user_migrate) * num_migrate)
1731 + userpolicy_type_attrsize();
1732}
1733
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001734static int build_migrate(struct sk_buff *skb, struct xfrm_migrate *m,
1735 int num_migrate, struct xfrm_selector *sel,
1736 u8 dir, u8 type)
1737{
1738 struct xfrm_migrate *mp;
1739 struct xfrm_userpolicy_id *pol_id;
1740 struct nlmsghdr *nlh;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001741 int i;
1742
Thomas Graf79b8b7f2007-08-22 12:46:53 -07001743 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_MIGRATE, sizeof(*pol_id), 0);
1744 if (nlh == NULL)
1745 return -EMSGSIZE;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001746
Thomas Graf7b67c852007-08-22 13:53:52 -07001747 pol_id = nlmsg_data(nlh);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001748 /* copy data from selector, dir, and type to the pol_id */
1749 memset(pol_id, 0, sizeof(*pol_id));
1750 memcpy(&pol_id->sel, sel, sizeof(pol_id->sel));
1751 pol_id->dir = dir;
1752
1753 if (copy_to_user_policy_type(type, skb) < 0)
1754 goto nlmsg_failure;
1755
1756 for (i = 0, mp = m ; i < num_migrate; i++, mp++) {
1757 if (copy_to_user_migrate(mp, skb) < 0)
1758 goto nlmsg_failure;
1759 }
1760
Thomas Graf98250692007-08-22 12:47:26 -07001761 return nlmsg_end(skb, nlh);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001762nlmsg_failure:
Thomas Graf98250692007-08-22 12:47:26 -07001763 nlmsg_cancel(skb, nlh);
1764 return -EMSGSIZE;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001765}
1766
1767static int xfrm_send_migrate(struct xfrm_selector *sel, u8 dir, u8 type,
1768 struct xfrm_migrate *m, int num_migrate)
1769{
1770 struct sk_buff *skb;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001771
Thomas Graf7deb2262007-08-22 13:57:39 -07001772 skb = nlmsg_new(xfrm_migrate_msgsize(num_migrate), GFP_ATOMIC);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001773 if (skb == NULL)
1774 return -ENOMEM;
1775
1776 /* build migrate */
1777 if (build_migrate(skb, m, num_migrate, sel, dir, type) < 0)
1778 BUG();
1779
Thomas Graf082a1ad2007-08-22 13:54:36 -07001780 return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_MIGRATE, GFP_ATOMIC);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001781}
1782#else
1783static int xfrm_send_migrate(struct xfrm_selector *sel, u8 dir, u8 type,
1784 struct xfrm_migrate *m, int num_migrate)
1785{
1786 return -ENOPROTOOPT;
1787}
1788#endif
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001789
Thomas Grafa7bd9a42007-08-22 13:58:18 -07001790#define XMSGSIZE(type) sizeof(struct type)
Thomas Graf492b5582005-05-03 14:26:40 -07001791
1792static const int xfrm_msg_min[XFRM_NR_MSGTYPES] = {
1793 [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
1794 [XFRM_MSG_DELSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
1795 [XFRM_MSG_GETSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
1796 [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
1797 [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
1798 [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
1799 [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userspi_info),
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001800 [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_acquire),
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001801 [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_expire),
Thomas Graf492b5582005-05-03 14:26:40 -07001802 [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
1803 [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001804 [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_polexpire),
Thomas Graf492b5582005-05-03 14:26:40 -07001805 [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_flush),
Thomas Grafa7bd9a42007-08-22 13:58:18 -07001806 [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = 0,
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001807 [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
1808 [XFRM_MSG_GETAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07001809 [XFRM_MSG_REPORT - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_report),
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001810 [XFRM_MSG_MIGRATE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
Thomas Grafa7bd9a42007-08-22 13:58:18 -07001811 [XFRM_MSG_GETSADINFO - XFRM_MSG_BASE] = sizeof(u32),
1812 [XFRM_MSG_GETSPDINFO - XFRM_MSG_BASE] = sizeof(u32),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813};
1814
Thomas Graf492b5582005-05-03 14:26:40 -07001815#undef XMSGSIZE
1816
Thomas Grafcf5cb792007-08-22 13:59:04 -07001817static const struct nla_policy xfrma_policy[XFRMA_MAX+1] = {
1818 [XFRMA_ALG_AUTH] = { .len = sizeof(struct xfrm_algo) },
1819 [XFRMA_ALG_CRYPT] = { .len = sizeof(struct xfrm_algo) },
1820 [XFRMA_ALG_COMP] = { .len = sizeof(struct xfrm_algo) },
1821 [XFRMA_ENCAP] = { .len = sizeof(struct xfrm_encap_tmpl) },
1822 [XFRMA_TMPL] = { .len = sizeof(struct xfrm_user_tmpl) },
1823 [XFRMA_SEC_CTX] = { .len = sizeof(struct xfrm_sec_ctx) },
1824 [XFRMA_LTIME_VAL] = { .len = sizeof(struct xfrm_lifetime_cur) },
1825 [XFRMA_REPLAY_VAL] = { .len = sizeof(struct xfrm_replay_state) },
1826 [XFRMA_REPLAY_THRESH] = { .type = NLA_U32 },
1827 [XFRMA_ETIMER_THRESH] = { .type = NLA_U32 },
1828 [XFRMA_SRCADDR] = { .len = sizeof(xfrm_address_t) },
1829 [XFRMA_COADDR] = { .len = sizeof(xfrm_address_t) },
1830 [XFRMA_POLICY_TYPE] = { .len = sizeof(struct xfrm_userpolicy_type)},
1831 [XFRMA_MIGRATE] = { .len = sizeof(struct xfrm_user_migrate) },
1832};
1833
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834static struct xfrm_link {
Thomas Graf5424f322007-08-22 14:01:33 -07001835 int (*doit)(struct sk_buff *, struct nlmsghdr *, struct nlattr **);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836 int (*dump)(struct sk_buff *, struct netlink_callback *);
Thomas Graf492b5582005-05-03 14:26:40 -07001837} xfrm_dispatch[XFRM_NR_MSGTYPES] = {
1838 [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa },
1839 [XFRM_MSG_DELSA - XFRM_MSG_BASE] = { .doit = xfrm_del_sa },
1840 [XFRM_MSG_GETSA - XFRM_MSG_BASE] = { .doit = xfrm_get_sa,
1841 .dump = xfrm_dump_sa },
1842 [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy },
1843 [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy },
1844 [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy,
1845 .dump = xfrm_dump_policy },
1846 [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = { .doit = xfrm_alloc_userspi },
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001847 [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_acquire },
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001848 [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_sa_expire },
Thomas Graf492b5582005-05-03 14:26:40 -07001849 [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy },
1850 [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa },
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001851 [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_pol_expire},
Thomas Graf492b5582005-05-03 14:26:40 -07001852 [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = { .doit = xfrm_flush_sa },
1853 [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_flush_policy },
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001854 [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = { .doit = xfrm_new_ae },
1855 [XFRM_MSG_GETAE - XFRM_MSG_BASE] = { .doit = xfrm_get_ae },
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001856 [XFRM_MSG_MIGRATE - XFRM_MSG_BASE] = { .doit = xfrm_do_migrate },
Jamal Hadi Salim566ec032007-04-26 14:12:15 -07001857 [XFRM_MSG_GETSADINFO - XFRM_MSG_BASE] = { .doit = xfrm_get_sadinfo },
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -07001858 [XFRM_MSG_GETSPDINFO - XFRM_MSG_BASE] = { .doit = xfrm_get_spdinfo },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859};
1860
Thomas Graf1d00a4e2007-03-22 23:30:12 -07001861static int xfrm_user_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862{
Thomas Graf35a7aa02007-08-22 14:00:40 -07001863 struct nlattr *attrs[XFRMA_MAX+1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864 struct xfrm_link *link;
Thomas Grafa7bd9a42007-08-22 13:58:18 -07001865 int type, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867 type = nlh->nlmsg_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868 if (type > XFRM_MSG_MAX)
Thomas Graf1d00a4e2007-03-22 23:30:12 -07001869 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870
1871 type -= XFRM_MSG_BASE;
1872 link = &xfrm_dispatch[type];
1873
1874 /* All operations require privileges, even GET */
Thomas Graf1d00a4e2007-03-22 23:30:12 -07001875 if (security_netlink_recv(skb, CAP_NET_ADMIN))
1876 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877
Thomas Graf492b5582005-05-03 14:26:40 -07001878 if ((type == (XFRM_MSG_GETSA - XFRM_MSG_BASE) ||
1879 type == (XFRM_MSG_GETPOLICY - XFRM_MSG_BASE)) &&
1880 (nlh->nlmsg_flags & NLM_F_DUMP)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881 if (link->dump == NULL)
Thomas Graf1d00a4e2007-03-22 23:30:12 -07001882 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883
Thomas Grafc702e802007-03-22 23:30:55 -07001884 return netlink_dump_start(xfrm_nl, skb, nlh, link->dump, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885 }
1886
Thomas Graf35a7aa02007-08-22 14:00:40 -07001887 err = nlmsg_parse(nlh, xfrm_msg_min[type], attrs, XFRMA_MAX,
Thomas Grafcf5cb792007-08-22 13:59:04 -07001888 xfrma_policy);
Thomas Grafa7bd9a42007-08-22 13:58:18 -07001889 if (err < 0)
1890 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891
1892 if (link->doit == NULL)
Thomas Graf1d00a4e2007-03-22 23:30:12 -07001893 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894
Thomas Graf5424f322007-08-22 14:01:33 -07001895 return link->doit(skb, nlh, attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896}
1897
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07001898static void xfrm_netlink_rcv(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899{
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07001900 mutex_lock(&xfrm_cfg_mutex);
1901 netlink_rcv_skb(skb, &xfrm_user_rcv_msg);
1902 mutex_unlock(&xfrm_cfg_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903}
1904
Thomas Graf7deb2262007-08-22 13:57:39 -07001905static inline size_t xfrm_expire_msgsize(void)
1906{
1907 return NLMSG_ALIGN(sizeof(struct xfrm_user_expire));
1908}
1909
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001910static int build_expire(struct sk_buff *skb, struct xfrm_state *x, struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911{
1912 struct xfrm_user_expire *ue;
1913 struct nlmsghdr *nlh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914
Thomas Graf79b8b7f2007-08-22 12:46:53 -07001915 nlh = nlmsg_put(skb, c->pid, 0, XFRM_MSG_EXPIRE, sizeof(*ue), 0);
1916 if (nlh == NULL)
1917 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918
Thomas Graf7b67c852007-08-22 13:53:52 -07001919 ue = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920 copy_to_user_state(x, &ue->state);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001921 ue->hard = (c->data.hard != 0) ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922
Thomas Graf98250692007-08-22 12:47:26 -07001923 return nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924}
1925
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001926static int xfrm_exp_state_notify(struct xfrm_state *x, struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927{
1928 struct sk_buff *skb;
1929
Thomas Graf7deb2262007-08-22 13:57:39 -07001930 skb = nlmsg_new(xfrm_expire_msgsize(), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931 if (skb == NULL)
1932 return -ENOMEM;
1933
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001934 if (build_expire(skb, x, c) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935 BUG();
1936
Thomas Graf082a1ad2007-08-22 13:54:36 -07001937 return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_EXPIRE, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938}
1939
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001940static int xfrm_aevent_state_notify(struct xfrm_state *x, struct km_event *c)
1941{
1942 struct sk_buff *skb;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001943
Thomas Graf7deb2262007-08-22 13:57:39 -07001944 skb = nlmsg_new(xfrm_aevent_msgsize(), GFP_ATOMIC);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001945 if (skb == NULL)
1946 return -ENOMEM;
1947
1948 if (build_aevent(skb, x, c) < 0)
1949 BUG();
1950
Thomas Graf082a1ad2007-08-22 13:54:36 -07001951 return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_AEVENTS, GFP_ATOMIC);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001952}
1953
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001954static int xfrm_notify_sa_flush(struct km_event *c)
1955{
1956 struct xfrm_usersa_flush *p;
1957 struct nlmsghdr *nlh;
1958 struct sk_buff *skb;
Thomas Graf7deb2262007-08-22 13:57:39 -07001959 int len = NLMSG_ALIGN(sizeof(struct xfrm_usersa_flush));
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001960
Thomas Graf7deb2262007-08-22 13:57:39 -07001961 skb = nlmsg_new(len, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001962 if (skb == NULL)
1963 return -ENOMEM;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001964
Thomas Graf79b8b7f2007-08-22 12:46:53 -07001965 nlh = nlmsg_put(skb, c->pid, c->seq, XFRM_MSG_FLUSHSA, sizeof(*p), 0);
1966 if (nlh == NULL) {
1967 kfree_skb(skb);
1968 return -EMSGSIZE;
1969 }
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001970
Thomas Graf7b67c852007-08-22 13:53:52 -07001971 p = nlmsg_data(nlh);
Herbert Xubf088672005-06-18 22:44:00 -07001972 p->proto = c->data.proto;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001973
Thomas Graf98250692007-08-22 12:47:26 -07001974 nlmsg_end(skb, nlh);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001975
Thomas Graf082a1ad2007-08-22 13:54:36 -07001976 return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_SA, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001977}
1978
Thomas Graf7deb2262007-08-22 13:57:39 -07001979static inline size_t xfrm_sa_len(struct xfrm_state *x)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001980{
Thomas Graf7deb2262007-08-22 13:57:39 -07001981 size_t l = 0;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001982 if (x->aalg)
Thomas Graf7deb2262007-08-22 13:57:39 -07001983 l += nla_total_size(alg_len(x->aalg));
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001984 if (x->ealg)
Thomas Graf7deb2262007-08-22 13:57:39 -07001985 l += nla_total_size(alg_len(x->ealg));
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001986 if (x->calg)
Thomas Graf7deb2262007-08-22 13:57:39 -07001987 l += nla_total_size(sizeof(*x->calg));
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001988 if (x->encap)
Thomas Graf7deb2262007-08-22 13:57:39 -07001989 l += nla_total_size(sizeof(*x->encap));
Herbert Xu68325d32007-10-09 13:30:57 -07001990 if (x->security)
1991 l += nla_total_size(sizeof(struct xfrm_user_sec_ctx) +
1992 x->security->ctx_len);
1993 if (x->coaddr)
1994 l += nla_total_size(sizeof(*x->coaddr));
1995
1996 /* Must count this as this may become non-zero behind our back. */
1997 l += nla_total_size(sizeof(x->lastused));
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001998
1999 return l;
2000}
2001
2002static int xfrm_notify_sa(struct xfrm_state *x, struct km_event *c)
2003{
2004 struct xfrm_usersa_info *p;
Herbert Xu0603eac2005-06-18 22:54:36 -07002005 struct xfrm_usersa_id *id;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002006 struct nlmsghdr *nlh;
2007 struct sk_buff *skb;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002008 int len = xfrm_sa_len(x);
Herbert Xu0603eac2005-06-18 22:54:36 -07002009 int headlen;
2010
2011 headlen = sizeof(*p);
2012 if (c->event == XFRM_MSG_DELSA) {
Thomas Graf7deb2262007-08-22 13:57:39 -07002013 len += nla_total_size(headlen);
Herbert Xu0603eac2005-06-18 22:54:36 -07002014 headlen = sizeof(*id);
2015 }
Thomas Graf7deb2262007-08-22 13:57:39 -07002016 len += NLMSG_ALIGN(headlen);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002017
Thomas Graf7deb2262007-08-22 13:57:39 -07002018 skb = nlmsg_new(len, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002019 if (skb == NULL)
2020 return -ENOMEM;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002021
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002022 nlh = nlmsg_put(skb, c->pid, c->seq, c->event, headlen, 0);
2023 if (nlh == NULL)
Thomas Grafc0144be2007-08-22 13:55:43 -07002024 goto nla_put_failure;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002025
Thomas Graf7b67c852007-08-22 13:53:52 -07002026 p = nlmsg_data(nlh);
Herbert Xu0603eac2005-06-18 22:54:36 -07002027 if (c->event == XFRM_MSG_DELSA) {
Thomas Grafc0144be2007-08-22 13:55:43 -07002028 struct nlattr *attr;
2029
Thomas Graf7b67c852007-08-22 13:53:52 -07002030 id = nlmsg_data(nlh);
Herbert Xu0603eac2005-06-18 22:54:36 -07002031 memcpy(&id->daddr, &x->id.daddr, sizeof(id->daddr));
2032 id->spi = x->id.spi;
2033 id->family = x->props.family;
2034 id->proto = x->id.proto;
2035
Thomas Grafc0144be2007-08-22 13:55:43 -07002036 attr = nla_reserve(skb, XFRMA_SA, sizeof(*p));
2037 if (attr == NULL)
2038 goto nla_put_failure;
2039
2040 p = nla_data(attr);
Herbert Xu0603eac2005-06-18 22:54:36 -07002041 }
2042
Herbert Xu68325d32007-10-09 13:30:57 -07002043 if (copy_to_user_state_extra(x, p, skb))
2044 goto nla_put_failure;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002045
Thomas Graf98250692007-08-22 12:47:26 -07002046 nlmsg_end(skb, nlh);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002047
Thomas Graf082a1ad2007-08-22 13:54:36 -07002048 return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_SA, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002049
Thomas Grafc0144be2007-08-22 13:55:43 -07002050nla_put_failure:
Herbert Xu68325d32007-10-09 13:30:57 -07002051 /* Somebody screwed up with xfrm_sa_len! */
2052 WARN_ON(1);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002053 kfree_skb(skb);
2054 return -1;
2055}
2056
2057static int xfrm_send_state_notify(struct xfrm_state *x, struct km_event *c)
2058{
2059
2060 switch (c->event) {
Herbert Xuf60f6b82005-06-18 22:44:37 -07002061 case XFRM_MSG_EXPIRE:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002062 return xfrm_exp_state_notify(x, c);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002063 case XFRM_MSG_NEWAE:
2064 return xfrm_aevent_state_notify(x, c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07002065 case XFRM_MSG_DELSA:
2066 case XFRM_MSG_UPDSA:
2067 case XFRM_MSG_NEWSA:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002068 return xfrm_notify_sa(x, c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07002069 case XFRM_MSG_FLUSHSA:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002070 return xfrm_notify_sa_flush(c);
2071 default:
2072 printk("xfrm_user: Unknown SA event %d\n", c->event);
2073 break;
2074 }
2075
2076 return 0;
2077
2078}
2079
Thomas Graf7deb2262007-08-22 13:57:39 -07002080static inline size_t xfrm_acquire_msgsize(struct xfrm_state *x,
2081 struct xfrm_policy *xp)
2082{
2083 return NLMSG_ALIGN(sizeof(struct xfrm_user_acquire))
2084 + nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr)
2085 + nla_total_size(xfrm_user_sec_ctx_size(x->security))
2086 + userpolicy_type_attrsize();
2087}
2088
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089static int build_acquire(struct sk_buff *skb, struct xfrm_state *x,
2090 struct xfrm_tmpl *xt, struct xfrm_policy *xp,
2091 int dir)
2092{
2093 struct xfrm_user_acquire *ua;
2094 struct nlmsghdr *nlh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095 __u32 seq = xfrm_get_acqseq();
2096
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002097 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_ACQUIRE, sizeof(*ua), 0);
2098 if (nlh == NULL)
2099 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100
Thomas Graf7b67c852007-08-22 13:53:52 -07002101 ua = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102 memcpy(&ua->id, &x->id, sizeof(ua->id));
2103 memcpy(&ua->saddr, &x->props.saddr, sizeof(ua->saddr));
2104 memcpy(&ua->sel, &x->sel, sizeof(ua->sel));
2105 copy_to_user_policy(xp, &ua->policy, dir);
2106 ua->aalgos = xt->aalgos;
2107 ua->ealgos = xt->ealgos;
2108 ua->calgos = xt->calgos;
2109 ua->seq = x->km.seq = seq;
2110
2111 if (copy_to_user_tmpl(xp, skb) < 0)
2112 goto nlmsg_failure;
Serge Hallyn0d681622006-07-24 23:30:44 -07002113 if (copy_to_user_state_sec_ctx(x, skb))
Trent Jaegerdf718372005-12-13 23:12:27 -08002114 goto nlmsg_failure;
Jamal Hadi Salim1459bb32006-11-20 16:51:22 -08002115 if (copy_to_user_policy_type(xp->type, skb) < 0)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07002116 goto nlmsg_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117
Thomas Graf98250692007-08-22 12:47:26 -07002118 return nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119
2120nlmsg_failure:
Thomas Graf98250692007-08-22 12:47:26 -07002121 nlmsg_cancel(skb, nlh);
2122 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123}
2124
2125static int xfrm_send_acquire(struct xfrm_state *x, struct xfrm_tmpl *xt,
2126 struct xfrm_policy *xp, int dir)
2127{
2128 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129
Thomas Graf7deb2262007-08-22 13:57:39 -07002130 skb = nlmsg_new(xfrm_acquire_msgsize(x, xp), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131 if (skb == NULL)
2132 return -ENOMEM;
2133
2134 if (build_acquire(skb, x, xt, xp, dir) < 0)
2135 BUG();
2136
Thomas Graf082a1ad2007-08-22 13:54:36 -07002137 return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_ACQUIRE, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138}
2139
2140/* User gives us xfrm_user_policy_info followed by an array of 0
2141 * or more templates.
2142 */
Venkat Yekkiralacb969f02006-07-24 23:32:20 -07002143static struct xfrm_policy *xfrm_compile_policy(struct sock *sk, int opt,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144 u8 *data, int len, int *dir)
2145{
2146 struct xfrm_userpolicy_info *p = (struct xfrm_userpolicy_info *)data;
2147 struct xfrm_user_tmpl *ut = (struct xfrm_user_tmpl *) (p + 1);
2148 struct xfrm_policy *xp;
2149 int nr;
2150
Venkat Yekkiralacb969f02006-07-24 23:32:20 -07002151 switch (sk->sk_family) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152 case AF_INET:
2153 if (opt != IP_XFRM_POLICY) {
2154 *dir = -EOPNOTSUPP;
2155 return NULL;
2156 }
2157 break;
2158#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
2159 case AF_INET6:
2160 if (opt != IPV6_XFRM_POLICY) {
2161 *dir = -EOPNOTSUPP;
2162 return NULL;
2163 }
2164 break;
2165#endif
2166 default:
2167 *dir = -EINVAL;
2168 return NULL;
2169 }
2170
2171 *dir = -EINVAL;
2172
2173 if (len < sizeof(*p) ||
2174 verify_newpolicy_info(p))
2175 return NULL;
2176
2177 nr = ((len - sizeof(*p)) / sizeof(*ut));
David S. Millerb4ad86bf2006-12-03 19:19:26 -08002178 if (validate_tmpl(nr, ut, p->sel.family))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179 return NULL;
2180
Herbert Xua4f1bac2005-07-26 15:43:17 -07002181 if (p->dir > XFRM_POLICY_OUT)
2182 return NULL;
2183
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184 xp = xfrm_policy_alloc(GFP_KERNEL);
2185 if (xp == NULL) {
2186 *dir = -ENOBUFS;
2187 return NULL;
2188 }
2189
2190 copy_from_user_policy(xp, p);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07002191 xp->type = XFRM_POLICY_TYPE_MAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192 copy_templates(xp, ut, nr);
2193
2194 *dir = p->dir;
2195
2196 return xp;
2197}
2198
Thomas Graf7deb2262007-08-22 13:57:39 -07002199static inline size_t xfrm_polexpire_msgsize(struct xfrm_policy *xp)
2200{
2201 return NLMSG_ALIGN(sizeof(struct xfrm_user_polexpire))
2202 + nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr)
2203 + nla_total_size(xfrm_user_sec_ctx_size(xp->security))
2204 + userpolicy_type_attrsize();
2205}
2206
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207static int build_polexpire(struct sk_buff *skb, struct xfrm_policy *xp,
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002208 int dir, struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209{
2210 struct xfrm_user_polexpire *upe;
2211 struct nlmsghdr *nlh;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002212 int hard = c->data.hard;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002214 nlh = nlmsg_put(skb, c->pid, 0, XFRM_MSG_POLEXPIRE, sizeof(*upe), 0);
2215 if (nlh == NULL)
2216 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217
Thomas Graf7b67c852007-08-22 13:53:52 -07002218 upe = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002219 copy_to_user_policy(xp, &upe->pol, dir);
2220 if (copy_to_user_tmpl(xp, skb) < 0)
2221 goto nlmsg_failure;
Trent Jaegerdf718372005-12-13 23:12:27 -08002222 if (copy_to_user_sec_ctx(xp, skb))
2223 goto nlmsg_failure;
Jamal Hadi Salim1459bb32006-11-20 16:51:22 -08002224 if (copy_to_user_policy_type(xp->type, skb) < 0)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07002225 goto nlmsg_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002226 upe->hard = !!hard;
2227
Thomas Graf98250692007-08-22 12:47:26 -07002228 return nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229
2230nlmsg_failure:
Thomas Graf98250692007-08-22 12:47:26 -07002231 nlmsg_cancel(skb, nlh);
2232 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233}
2234
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002235static int xfrm_exp_policy_notify(struct xfrm_policy *xp, int dir, struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236{
2237 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238
Thomas Graf7deb2262007-08-22 13:57:39 -07002239 skb = nlmsg_new(xfrm_polexpire_msgsize(xp), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240 if (skb == NULL)
2241 return -ENOMEM;
2242
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002243 if (build_polexpire(skb, xp, dir, c) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002244 BUG();
2245
Thomas Graf082a1ad2007-08-22 13:54:36 -07002246 return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_EXPIRE, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247}
2248
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002249static int xfrm_notify_policy(struct xfrm_policy *xp, int dir, struct km_event *c)
2250{
2251 struct xfrm_userpolicy_info *p;
Herbert Xu0603eac2005-06-18 22:54:36 -07002252 struct xfrm_userpolicy_id *id;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002253 struct nlmsghdr *nlh;
2254 struct sk_buff *skb;
Thomas Graf7deb2262007-08-22 13:57:39 -07002255 int len = nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr);
Herbert Xu0603eac2005-06-18 22:54:36 -07002256 int headlen;
2257
2258 headlen = sizeof(*p);
2259 if (c->event == XFRM_MSG_DELPOLICY) {
Thomas Graf7deb2262007-08-22 13:57:39 -07002260 len += nla_total_size(headlen);
Herbert Xu0603eac2005-06-18 22:54:36 -07002261 headlen = sizeof(*id);
2262 }
Thomas Grafcfbfd452007-08-22 13:57:04 -07002263 len += userpolicy_type_attrsize();
Thomas Graf7deb2262007-08-22 13:57:39 -07002264 len += NLMSG_ALIGN(headlen);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002265
Thomas Graf7deb2262007-08-22 13:57:39 -07002266 skb = nlmsg_new(len, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002267 if (skb == NULL)
2268 return -ENOMEM;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002269
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002270 nlh = nlmsg_put(skb, c->pid, c->seq, c->event, headlen, 0);
2271 if (nlh == NULL)
2272 goto nlmsg_failure;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002273
Thomas Graf7b67c852007-08-22 13:53:52 -07002274 p = nlmsg_data(nlh);
Herbert Xu0603eac2005-06-18 22:54:36 -07002275 if (c->event == XFRM_MSG_DELPOLICY) {
Thomas Grafc0144be2007-08-22 13:55:43 -07002276 struct nlattr *attr;
2277
Thomas Graf7b67c852007-08-22 13:53:52 -07002278 id = nlmsg_data(nlh);
Herbert Xu0603eac2005-06-18 22:54:36 -07002279 memset(id, 0, sizeof(*id));
2280 id->dir = dir;
2281 if (c->data.byid)
2282 id->index = xp->index;
2283 else
2284 memcpy(&id->sel, &xp->selector, sizeof(id->sel));
2285
Thomas Grafc0144be2007-08-22 13:55:43 -07002286 attr = nla_reserve(skb, XFRMA_POLICY, sizeof(*p));
2287 if (attr == NULL)
2288 goto nlmsg_failure;
2289
2290 p = nla_data(attr);
Herbert Xu0603eac2005-06-18 22:54:36 -07002291 }
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002292
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002293 copy_to_user_policy(xp, p, dir);
2294 if (copy_to_user_tmpl(xp, skb) < 0)
2295 goto nlmsg_failure;
Jamal Hadi Salim1459bb32006-11-20 16:51:22 -08002296 if (copy_to_user_policy_type(xp->type, skb) < 0)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07002297 goto nlmsg_failure;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002298
Thomas Graf98250692007-08-22 12:47:26 -07002299 nlmsg_end(skb, nlh);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002300
Thomas Graf082a1ad2007-08-22 13:54:36 -07002301 return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_POLICY, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002302
2303nlmsg_failure:
2304 kfree_skb(skb);
2305 return -1;
2306}
2307
2308static int xfrm_notify_policy_flush(struct km_event *c)
2309{
2310 struct nlmsghdr *nlh;
2311 struct sk_buff *skb;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002312
Thomas Graf7deb2262007-08-22 13:57:39 -07002313 skb = nlmsg_new(userpolicy_type_attrsize(), GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002314 if (skb == NULL)
2315 return -ENOMEM;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002316
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002317 nlh = nlmsg_put(skb, c->pid, c->seq, XFRM_MSG_FLUSHPOLICY, 0, 0);
2318 if (nlh == NULL)
2319 goto nlmsg_failure;
Jamal Hadi Salim0c51f532006-11-27 12:58:20 -08002320 if (copy_to_user_policy_type(c->data.type, skb) < 0)
2321 goto nlmsg_failure;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002322
Thomas Graf98250692007-08-22 12:47:26 -07002323 nlmsg_end(skb, nlh);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002324
Thomas Graf082a1ad2007-08-22 13:54:36 -07002325 return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_POLICY, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002326
2327nlmsg_failure:
2328 kfree_skb(skb);
2329 return -1;
2330}
2331
2332static int xfrm_send_policy_notify(struct xfrm_policy *xp, int dir, struct km_event *c)
2333{
2334
2335 switch (c->event) {
Herbert Xuf60f6b82005-06-18 22:44:37 -07002336 case XFRM_MSG_NEWPOLICY:
2337 case XFRM_MSG_UPDPOLICY:
2338 case XFRM_MSG_DELPOLICY:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002339 return xfrm_notify_policy(xp, dir, c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07002340 case XFRM_MSG_FLUSHPOLICY:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002341 return xfrm_notify_policy_flush(c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07002342 case XFRM_MSG_POLEXPIRE:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002343 return xfrm_exp_policy_notify(xp, dir, c);
2344 default:
2345 printk("xfrm_user: Unknown Policy event %d\n", c->event);
2346 }
2347
2348 return 0;
2349
2350}
2351
Thomas Graf7deb2262007-08-22 13:57:39 -07002352static inline size_t xfrm_report_msgsize(void)
2353{
2354 return NLMSG_ALIGN(sizeof(struct xfrm_user_report));
2355}
2356
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002357static int build_report(struct sk_buff *skb, u8 proto,
2358 struct xfrm_selector *sel, xfrm_address_t *addr)
2359{
2360 struct xfrm_user_report *ur;
2361 struct nlmsghdr *nlh;
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002362
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002363 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_REPORT, sizeof(*ur), 0);
2364 if (nlh == NULL)
2365 return -EMSGSIZE;
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002366
Thomas Graf7b67c852007-08-22 13:53:52 -07002367 ur = nlmsg_data(nlh);
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002368 ur->proto = proto;
2369 memcpy(&ur->sel, sel, sizeof(ur->sel));
2370
2371 if (addr)
Thomas Grafc0144be2007-08-22 13:55:43 -07002372 NLA_PUT(skb, XFRMA_COADDR, sizeof(*addr), addr);
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002373
Thomas Graf98250692007-08-22 12:47:26 -07002374 return nlmsg_end(skb, nlh);
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002375
Thomas Grafc0144be2007-08-22 13:55:43 -07002376nla_put_failure:
Thomas Graf98250692007-08-22 12:47:26 -07002377 nlmsg_cancel(skb, nlh);
2378 return -EMSGSIZE;
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002379}
2380
2381static int xfrm_send_report(u8 proto, struct xfrm_selector *sel,
2382 xfrm_address_t *addr)
2383{
2384 struct sk_buff *skb;
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002385
Thomas Graf7deb2262007-08-22 13:57:39 -07002386 skb = nlmsg_new(xfrm_report_msgsize(), GFP_ATOMIC);
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002387 if (skb == NULL)
2388 return -ENOMEM;
2389
2390 if (build_report(skb, proto, sel, addr) < 0)
2391 BUG();
2392
Thomas Graf082a1ad2007-08-22 13:54:36 -07002393 return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_REPORT, GFP_ATOMIC);
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002394}
2395
Linus Torvalds1da177e2005-04-16 15:20:36 -07002396static struct xfrm_mgr netlink_mgr = {
2397 .id = "netlink",
2398 .notify = xfrm_send_state_notify,
2399 .acquire = xfrm_send_acquire,
2400 .compile_policy = xfrm_compile_policy,
2401 .notify_policy = xfrm_send_policy_notify,
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002402 .report = xfrm_send_report,
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002403 .migrate = xfrm_send_migrate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002404};
2405
2406static int __init xfrm_user_init(void)
2407{
Patrick McHardybe336902006-03-20 22:40:54 -08002408 struct sock *nlsk;
2409
Masahide NAKAMURA654b32c2006-08-23 19:12:56 -07002410 printk(KERN_INFO "Initializing XFRM netlink socket\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002411
Eric W. Biedermanb4b51022007-09-12 13:05:38 +02002412 nlsk = netlink_kernel_create(&init_net, NETLINK_XFRM, XFRMNLGRP_MAX,
Patrick McHardyaf65bdf2007-04-20 14:14:21 -07002413 xfrm_netlink_rcv, NULL, THIS_MODULE);
Patrick McHardybe336902006-03-20 22:40:54 -08002414 if (nlsk == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002415 return -ENOMEM;
Patrick McHardybe336902006-03-20 22:40:54 -08002416 rcu_assign_pointer(xfrm_nl, nlsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002417
2418 xfrm_register_km(&netlink_mgr);
2419
2420 return 0;
2421}
2422
2423static void __exit xfrm_user_exit(void)
2424{
Patrick McHardybe336902006-03-20 22:40:54 -08002425 struct sock *nlsk = xfrm_nl;
2426
Linus Torvalds1da177e2005-04-16 15:20:36 -07002427 xfrm_unregister_km(&netlink_mgr);
Patrick McHardybe336902006-03-20 22:40:54 -08002428 rcu_assign_pointer(xfrm_nl, NULL);
2429 synchronize_rcu();
2430 sock_release(nlsk->sk_socket);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002431}
2432
2433module_init(xfrm_user_init);
2434module_exit(xfrm_user_exit);
2435MODULE_LICENSE("GPL");
Harald Welte4fdb3bb2005-08-09 19:40:55 -07002436MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK, NETLINK_XFRM);
Jamal Hadi Salimf8cd5482006-03-20 19:15:11 -08002437