blob: 2cbbe5e93a7bb5ecdc59653378435d97cc32fa60 [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{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 copy_to_user_state(x, p);
511
512 if (x->aalg)
Thomas Grafc26445a2007-08-22 13:56:23 -0700513 NLA_PUT(skb, XFRMA_ALG_AUTH, alg_len(x->aalg), x->aalg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 if (x->ealg)
Thomas Grafc26445a2007-08-22 13:56:23 -0700515 NLA_PUT(skb, XFRMA_ALG_CRYPT, alg_len(x->ealg), x->ealg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 if (x->calg)
Thomas Grafc0144be2007-08-22 13:55:43 -0700517 NLA_PUT(skb, XFRMA_ALG_COMP, sizeof(*(x->calg)), x->calg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518
519 if (x->encap)
Thomas Grafc0144be2007-08-22 13:55:43 -0700520 NLA_PUT(skb, XFRMA_ENCAP, sizeof(*x->encap), x->encap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521
Thomas Grafc0144be2007-08-22 13:55:43 -0700522 if (x->security && copy_sec_ctx(x->security, skb) < 0)
523 goto nla_put_failure;
Noriaki TAKAMIYA060f02a2006-08-23 18:18:55 -0700524
525 if (x->coaddr)
Thomas Grafc0144be2007-08-22 13:55:43 -0700526 NLA_PUT(skb, XFRMA_COADDR, sizeof(*x->coaddr), x->coaddr);
Noriaki TAKAMIYA060f02a2006-08-23 18:18:55 -0700527
Masahide NAKAMURA9afaca02006-08-23 18:20:16 -0700528 if (x->lastused)
Thomas Grafc0144be2007-08-22 13:55:43 -0700529 NLA_PUT_U64(skb, XFRMA_LASTUSED, x->lastused);
Masahide NAKAMURA9afaca02006-08-23 18:20:16 -0700530
Herbert Xu68325d32007-10-09 13:30:57 -0700531 return 0;
532
533nla_put_failure:
534 return -EMSGSIZE;
535}
536
537static int dump_one_state(struct xfrm_state *x, int count, void *ptr)
538{
539 struct xfrm_dump_info *sp = ptr;
540 struct sk_buff *in_skb = sp->in_skb;
541 struct sk_buff *skb = sp->out_skb;
542 struct xfrm_usersa_info *p;
543 struct nlmsghdr *nlh;
544 int err;
545
546 if (sp->this_idx < sp->start_idx)
547 goto out;
548
549 nlh = nlmsg_put(skb, NETLINK_CB(in_skb).pid, sp->nlmsg_seq,
550 XFRM_MSG_NEWSA, sizeof(*p), sp->nlmsg_flags);
551 if (nlh == NULL)
552 return -EMSGSIZE;
553
554 p = nlmsg_data(nlh);
555
556 err = copy_to_user_state_extra(x, p, skb);
557 if (err)
558 goto nla_put_failure;
559
Thomas Graf98250692007-08-22 12:47:26 -0700560 nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561out:
562 sp->this_idx++;
563 return 0;
564
Thomas Grafc0144be2007-08-22 13:55:43 -0700565nla_put_failure:
Thomas Graf98250692007-08-22 12:47:26 -0700566 nlmsg_cancel(skb, nlh);
Herbert Xu68325d32007-10-09 13:30:57 -0700567 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568}
569
570static int xfrm_dump_sa(struct sk_buff *skb, struct netlink_callback *cb)
571{
572 struct xfrm_dump_info info;
573
574 info.in_skb = cb->skb;
575 info.out_skb = skb;
576 info.nlmsg_seq = cb->nlh->nlmsg_seq;
577 info.nlmsg_flags = NLM_F_MULTI;
578 info.this_idx = 0;
579 info.start_idx = cb->args[0];
Masahide NAKAMURAdc00a522006-08-23 17:49:52 -0700580 (void) xfrm_state_walk(0, dump_one_state, &info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 cb->args[0] = info.this_idx;
582
583 return skb->len;
584}
585
586static struct sk_buff *xfrm_state_netlink(struct sk_buff *in_skb,
587 struct xfrm_state *x, u32 seq)
588{
589 struct xfrm_dump_info info;
590 struct sk_buff *skb;
591
Thomas Graf7deb2262007-08-22 13:57:39 -0700592 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 if (!skb)
594 return ERR_PTR(-ENOMEM);
595
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 info.in_skb = in_skb;
597 info.out_skb = skb;
598 info.nlmsg_seq = seq;
599 info.nlmsg_flags = 0;
600 info.this_idx = info.start_idx = 0;
601
602 if (dump_one_state(x, 0, &info)) {
603 kfree_skb(skb);
604 return NULL;
605 }
606
607 return skb;
608}
609
Thomas Graf7deb2262007-08-22 13:57:39 -0700610static inline size_t xfrm_spdinfo_msgsize(void)
611{
612 return NLMSG_ALIGN(4)
613 + nla_total_size(sizeof(struct xfrmu_spdinfo))
614 + nla_total_size(sizeof(struct xfrmu_spdhinfo));
615}
616
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700617static int build_spdinfo(struct sk_buff *skb, u32 pid, u32 seq, u32 flags)
618{
Jamal Hadi Salim5a6d3412007-05-04 12:55:39 -0700619 struct xfrmk_spdinfo si;
620 struct xfrmu_spdinfo spc;
621 struct xfrmu_spdhinfo sph;
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700622 struct nlmsghdr *nlh;
623 u32 *f;
624
625 nlh = nlmsg_put(skb, pid, seq, XFRM_MSG_NEWSPDINFO, sizeof(u32), 0);
626 if (nlh == NULL) /* shouldnt really happen ... */
627 return -EMSGSIZE;
628
629 f = nlmsg_data(nlh);
630 *f = flags;
631 xfrm_spd_getinfo(&si);
Jamal Hadi Salim5a6d3412007-05-04 12:55:39 -0700632 spc.incnt = si.incnt;
633 spc.outcnt = si.outcnt;
634 spc.fwdcnt = si.fwdcnt;
635 spc.inscnt = si.inscnt;
636 spc.outscnt = si.outscnt;
637 spc.fwdscnt = si.fwdscnt;
638 sph.spdhcnt = si.spdhcnt;
639 sph.spdhmcnt = si.spdhmcnt;
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700640
Jamal Hadi Salim5a6d3412007-05-04 12:55:39 -0700641 NLA_PUT(skb, XFRMA_SPD_INFO, sizeof(spc), &spc);
642 NLA_PUT(skb, XFRMA_SPD_HINFO, sizeof(sph), &sph);
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700643
644 return nlmsg_end(skb, nlh);
645
646nla_put_failure:
647 nlmsg_cancel(skb, nlh);
648 return -EMSGSIZE;
649}
650
651static int xfrm_get_spdinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -0700652 struct nlattr **attrs)
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700653{
654 struct sk_buff *r_skb;
Thomas Graf7b67c852007-08-22 13:53:52 -0700655 u32 *flags = nlmsg_data(nlh);
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700656 u32 spid = NETLINK_CB(skb).pid;
657 u32 seq = nlh->nlmsg_seq;
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700658
Thomas Graf7deb2262007-08-22 13:57:39 -0700659 r_skb = nlmsg_new(xfrm_spdinfo_msgsize(), GFP_ATOMIC);
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700660 if (r_skb == NULL)
661 return -ENOMEM;
662
663 if (build_spdinfo(r_skb, spid, seq, *flags) < 0)
664 BUG();
665
666 return nlmsg_unicast(xfrm_nl, r_skb, spid);
667}
668
Thomas Graf7deb2262007-08-22 13:57:39 -0700669static inline size_t xfrm_sadinfo_msgsize(void)
670{
671 return NLMSG_ALIGN(4)
672 + nla_total_size(sizeof(struct xfrmu_sadhinfo))
673 + nla_total_size(4); /* XFRMA_SAD_CNT */
674}
675
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700676static int build_sadinfo(struct sk_buff *skb, u32 pid, u32 seq, u32 flags)
677{
Jamal Hadi Salimaf11e312007-05-04 12:55:13 -0700678 struct xfrmk_sadinfo si;
679 struct xfrmu_sadhinfo sh;
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700680 struct nlmsghdr *nlh;
681 u32 *f;
682
683 nlh = nlmsg_put(skb, pid, seq, XFRM_MSG_NEWSADINFO, sizeof(u32), 0);
684 if (nlh == NULL) /* shouldnt really happen ... */
685 return -EMSGSIZE;
686
687 f = nlmsg_data(nlh);
688 *f = flags;
689 xfrm_sad_getinfo(&si);
690
Jamal Hadi Salimaf11e312007-05-04 12:55:13 -0700691 sh.sadhmcnt = si.sadhmcnt;
692 sh.sadhcnt = si.sadhcnt;
693
694 NLA_PUT_U32(skb, XFRMA_SAD_CNT, si.sadcnt);
695 NLA_PUT(skb, XFRMA_SAD_HINFO, sizeof(sh), &sh);
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700696
697 return nlmsg_end(skb, nlh);
698
699nla_put_failure:
700 nlmsg_cancel(skb, nlh);
701 return -EMSGSIZE;
702}
703
704static int xfrm_get_sadinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -0700705 struct nlattr **attrs)
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700706{
707 struct sk_buff *r_skb;
Thomas Graf7b67c852007-08-22 13:53:52 -0700708 u32 *flags = nlmsg_data(nlh);
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700709 u32 spid = NETLINK_CB(skb).pid;
710 u32 seq = nlh->nlmsg_seq;
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700711
Thomas Graf7deb2262007-08-22 13:57:39 -0700712 r_skb = nlmsg_new(xfrm_sadinfo_msgsize(), GFP_ATOMIC);
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700713 if (r_skb == NULL)
714 return -ENOMEM;
715
716 if (build_sadinfo(r_skb, spid, seq, *flags) < 0)
717 BUG();
718
719 return nlmsg_unicast(xfrm_nl, r_skb, spid);
720}
721
Christoph Hellwig22e70052007-01-02 15:22:30 -0800722static int xfrm_get_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -0700723 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724{
Thomas Graf7b67c852007-08-22 13:53:52 -0700725 struct xfrm_usersa_id *p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 struct xfrm_state *x;
727 struct sk_buff *resp_skb;
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700728 int err = -ESRCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729
Thomas Graf35a7aa02007-08-22 14:00:40 -0700730 x = xfrm_user_state_lookup(p, attrs, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 if (x == NULL)
732 goto out_noput;
733
734 resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
735 if (IS_ERR(resp_skb)) {
736 err = PTR_ERR(resp_skb);
737 } else {
Thomas Graf082a1ad2007-08-22 13:54:36 -0700738 err = nlmsg_unicast(xfrm_nl, resp_skb, NETLINK_CB(skb).pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 }
740 xfrm_state_put(x);
741out_noput:
742 return err;
743}
744
745static int verify_userspi_info(struct xfrm_userspi_info *p)
746{
747 switch (p->info.id.proto) {
748 case IPPROTO_AH:
749 case IPPROTO_ESP:
750 break;
751
752 case IPPROTO_COMP:
753 /* IPCOMP spi is 16-bits. */
754 if (p->max >= 0x10000)
755 return -EINVAL;
756 break;
757
758 default:
759 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700760 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761
762 if (p->min > p->max)
763 return -EINVAL;
764
765 return 0;
766}
767
Christoph Hellwig22e70052007-01-02 15:22:30 -0800768static int xfrm_alloc_userspi(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -0700769 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770{
771 struct xfrm_state *x;
772 struct xfrm_userspi_info *p;
773 struct sk_buff *resp_skb;
774 xfrm_address_t *daddr;
775 int family;
776 int err;
777
Thomas Graf7b67c852007-08-22 13:53:52 -0700778 p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 err = verify_userspi_info(p);
780 if (err)
781 goto out_noput;
782
783 family = p->info.family;
784 daddr = &p->info.id.daddr;
785
786 x = NULL;
787 if (p->info.seq) {
788 x = xfrm_find_acq_byseq(p->info.seq);
789 if (x && xfrm_addr_cmp(&x->id.daddr, daddr, family)) {
790 xfrm_state_put(x);
791 x = NULL;
792 }
793 }
794
795 if (!x)
796 x = xfrm_find_acq(p->info.mode, p->info.reqid,
797 p->info.id.proto, daddr,
798 &p->info.saddr, 1,
799 family);
800 err = -ENOENT;
801 if (x == NULL)
802 goto out_noput;
803
Herbert Xu658b2192007-10-09 13:29:52 -0700804 err = xfrm_alloc_spi(x, p->min, p->max);
805 if (err)
806 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807
Herbert Xu658b2192007-10-09 13:29:52 -0700808 resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 if (IS_ERR(resp_skb)) {
810 err = PTR_ERR(resp_skb);
811 goto out;
812 }
813
Thomas Graf082a1ad2007-08-22 13:54:36 -0700814 err = nlmsg_unicast(xfrm_nl, resp_skb, NETLINK_CB(skb).pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815
816out:
817 xfrm_state_put(x);
818out_noput:
819 return err;
820}
821
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -0800822static int verify_policy_dir(u8 dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823{
824 switch (dir) {
825 case XFRM_POLICY_IN:
826 case XFRM_POLICY_OUT:
827 case XFRM_POLICY_FWD:
828 break;
829
830 default:
831 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700832 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833
834 return 0;
835}
836
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -0800837static int verify_policy_type(u8 type)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -0700838{
839 switch (type) {
840 case XFRM_POLICY_TYPE_MAIN:
841#ifdef CONFIG_XFRM_SUB_POLICY
842 case XFRM_POLICY_TYPE_SUB:
843#endif
844 break;
845
846 default:
847 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700848 }
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -0700849
850 return 0;
851}
852
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853static int verify_newpolicy_info(struct xfrm_userpolicy_info *p)
854{
855 switch (p->share) {
856 case XFRM_SHARE_ANY:
857 case XFRM_SHARE_SESSION:
858 case XFRM_SHARE_USER:
859 case XFRM_SHARE_UNIQUE:
860 break;
861
862 default:
863 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700864 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865
866 switch (p->action) {
867 case XFRM_POLICY_ALLOW:
868 case XFRM_POLICY_BLOCK:
869 break;
870
871 default:
872 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700873 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874
875 switch (p->sel.family) {
876 case AF_INET:
877 break;
878
879 case AF_INET6:
880#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
881 break;
882#else
883 return -EAFNOSUPPORT;
884#endif
885
886 default:
887 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700888 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889
890 return verify_policy_dir(p->dir);
891}
892
Thomas Graf5424f322007-08-22 14:01:33 -0700893static int copy_from_user_sec_ctx(struct xfrm_policy *pol, struct nlattr **attrs)
Trent Jaegerdf718372005-12-13 23:12:27 -0800894{
Thomas Graf5424f322007-08-22 14:01:33 -0700895 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
Trent Jaegerdf718372005-12-13 23:12:27 -0800896 struct xfrm_user_sec_ctx *uctx;
897
898 if (!rt)
899 return 0;
900
Thomas Graf5424f322007-08-22 14:01:33 -0700901 uctx = nla_data(rt);
Trent Jaegerdf718372005-12-13 23:12:27 -0800902 return security_xfrm_policy_alloc(pol, uctx);
903}
904
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905static void copy_templates(struct xfrm_policy *xp, struct xfrm_user_tmpl *ut,
906 int nr)
907{
908 int i;
909
910 xp->xfrm_nr = nr;
911 for (i = 0; i < nr; i++, ut++) {
912 struct xfrm_tmpl *t = &xp->xfrm_vec[i];
913
914 memcpy(&t->id, &ut->id, sizeof(struct xfrm_id));
915 memcpy(&t->saddr, &ut->saddr,
916 sizeof(xfrm_address_t));
917 t->reqid = ut->reqid;
918 t->mode = ut->mode;
919 t->share = ut->share;
920 t->optional = ut->optional;
921 t->aalgos = ut->aalgos;
922 t->ealgos = ut->ealgos;
923 t->calgos = ut->calgos;
Miika Komu8511d012006-11-30 16:40:51 -0800924 t->encap_family = ut->family;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 }
926}
927
David S. Millerb4ad86bf2006-12-03 19:19:26 -0800928static int validate_tmpl(int nr, struct xfrm_user_tmpl *ut, u16 family)
929{
930 int i;
931
932 if (nr > XFRM_MAX_DEPTH)
933 return -EINVAL;
934
935 for (i = 0; i < nr; i++) {
936 /* We never validated the ut->family value, so many
937 * applications simply leave it at zero. The check was
938 * never made and ut->family was ignored because all
939 * templates could be assumed to have the same family as
940 * the policy itself. Now that we will have ipv4-in-ipv6
941 * and ipv6-in-ipv4 tunnels, this is no longer true.
942 */
943 if (!ut[i].family)
944 ut[i].family = family;
945
946 switch (ut[i].family) {
947 case AF_INET:
948 break;
949#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
950 case AF_INET6:
951 break;
952#endif
953 default:
954 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700955 }
David S. Millerb4ad86bf2006-12-03 19:19:26 -0800956 }
957
958 return 0;
959}
960
Thomas Graf5424f322007-08-22 14:01:33 -0700961static int copy_from_user_tmpl(struct xfrm_policy *pol, struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962{
Thomas Graf5424f322007-08-22 14:01:33 -0700963 struct nlattr *rt = attrs[XFRMA_TMPL];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964
965 if (!rt) {
966 pol->xfrm_nr = 0;
967 } else {
Thomas Graf5424f322007-08-22 14:01:33 -0700968 struct xfrm_user_tmpl *utmpl = nla_data(rt);
969 int nr = nla_len(rt) / sizeof(*utmpl);
David S. Millerb4ad86bf2006-12-03 19:19:26 -0800970 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971
David S. Millerb4ad86bf2006-12-03 19:19:26 -0800972 err = validate_tmpl(nr, utmpl, pol->family);
973 if (err)
974 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975
Thomas Graf5424f322007-08-22 14:01:33 -0700976 copy_templates(pol, utmpl, nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 }
978 return 0;
979}
980
Thomas Graf5424f322007-08-22 14:01:33 -0700981static int copy_from_user_policy_type(u8 *tp, struct nlattr **attrs)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -0700982{
Thomas Graf5424f322007-08-22 14:01:33 -0700983 struct nlattr *rt = attrs[XFRMA_POLICY_TYPE];
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -0700984 struct xfrm_userpolicy_type *upt;
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -0800985 u8 type = XFRM_POLICY_TYPE_MAIN;
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -0700986 int err;
987
988 if (rt) {
Thomas Graf5424f322007-08-22 14:01:33 -0700989 upt = nla_data(rt);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -0700990 type = upt->type;
991 }
992
993 err = verify_policy_type(type);
994 if (err)
995 return err;
996
997 *tp = type;
998 return 0;
999}
1000
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001static void copy_from_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p)
1002{
1003 xp->priority = p->priority;
1004 xp->index = p->index;
1005 memcpy(&xp->selector, &p->sel, sizeof(xp->selector));
1006 memcpy(&xp->lft, &p->lft, sizeof(xp->lft));
1007 xp->action = p->action;
1008 xp->flags = p->flags;
1009 xp->family = p->sel.family;
1010 /* XXX xp->share = p->share; */
1011}
1012
1013static void copy_to_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p, int dir)
1014{
1015 memcpy(&p->sel, &xp->selector, sizeof(p->sel));
1016 memcpy(&p->lft, &xp->lft, sizeof(p->lft));
1017 memcpy(&p->curlft, &xp->curlft, sizeof(p->curlft));
1018 p->priority = xp->priority;
1019 p->index = xp->index;
1020 p->sel.family = xp->family;
1021 p->dir = dir;
1022 p->action = xp->action;
1023 p->flags = xp->flags;
1024 p->share = XFRM_SHARE_ANY; /* XXX xp->share */
1025}
1026
Thomas Graf5424f322007-08-22 14:01:33 -07001027static struct xfrm_policy *xfrm_policy_construct(struct xfrm_userpolicy_info *p, struct nlattr **attrs, int *errp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028{
1029 struct xfrm_policy *xp = xfrm_policy_alloc(GFP_KERNEL);
1030 int err;
1031
1032 if (!xp) {
1033 *errp = -ENOMEM;
1034 return NULL;
1035 }
1036
1037 copy_from_user_policy(xp, p);
Trent Jaegerdf718372005-12-13 23:12:27 -08001038
Thomas Graf35a7aa02007-08-22 14:00:40 -07001039 err = copy_from_user_policy_type(&xp->type, attrs);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001040 if (err)
1041 goto error;
1042
Thomas Graf35a7aa02007-08-22 14:00:40 -07001043 if (!(err = copy_from_user_tmpl(xp, attrs)))
1044 err = copy_from_user_sec_ctx(xp, attrs);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001045 if (err)
1046 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047
1048 return xp;
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001049 error:
1050 *errp = err;
1051 kfree(xp);
1052 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053}
1054
Christoph Hellwig22e70052007-01-02 15:22:30 -08001055static int xfrm_add_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001056 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057{
Thomas Graf7b67c852007-08-22 13:53:52 -07001058 struct xfrm_userpolicy_info *p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 struct xfrm_policy *xp;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001060 struct km_event c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 int err;
1062 int excl;
1063
1064 err = verify_newpolicy_info(p);
1065 if (err)
1066 return err;
Thomas Graf35a7aa02007-08-22 14:00:40 -07001067 err = verify_sec_ctx_len(attrs);
Trent Jaegerdf718372005-12-13 23:12:27 -08001068 if (err)
1069 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070
Thomas Graf35a7aa02007-08-22 14:00:40 -07001071 xp = xfrm_policy_construct(p, attrs, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 if (!xp)
1073 return err;
1074
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001075 /* shouldnt excl be based on nlh flags??
1076 * Aha! this is anti-netlink really i.e more pfkey derived
1077 * in netlink excl is a flag and you wouldnt need
1078 * a type XFRM_MSG_UPDPOLICY - JHS */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 excl = nlh->nlmsg_type == XFRM_MSG_NEWPOLICY;
1080 err = xfrm_policy_insert(p->dir, xp, excl);
Joy Lattenab5f5e82007-09-17 11:51:22 -07001081 xfrm_audit_policy_add(xp, err ? 0 : 1, NETLINK_CB(skb).loginuid,
1082 NETLINK_CB(skb).sid);
Joy Latten161a09e2006-11-27 13:11:54 -06001083
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 if (err) {
Trent Jaeger5f8ac642006-01-06 13:22:39 -08001085 security_xfrm_policy_free(xp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 kfree(xp);
1087 return err;
1088 }
1089
Herbert Xuf60f6b82005-06-18 22:44:37 -07001090 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001091 c.seq = nlh->nlmsg_seq;
1092 c.pid = nlh->nlmsg_pid;
1093 km_policy_notify(xp, p->dir, &c);
1094
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 xfrm_pol_put(xp);
1096
1097 return 0;
1098}
1099
1100static int copy_to_user_tmpl(struct xfrm_policy *xp, struct sk_buff *skb)
1101{
1102 struct xfrm_user_tmpl vec[XFRM_MAX_DEPTH];
1103 int i;
1104
1105 if (xp->xfrm_nr == 0)
1106 return 0;
1107
1108 for (i = 0; i < xp->xfrm_nr; i++) {
1109 struct xfrm_user_tmpl *up = &vec[i];
1110 struct xfrm_tmpl *kp = &xp->xfrm_vec[i];
1111
1112 memcpy(&up->id, &kp->id, sizeof(up->id));
Miika Komu8511d012006-11-30 16:40:51 -08001113 up->family = kp->encap_family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 memcpy(&up->saddr, &kp->saddr, sizeof(up->saddr));
1115 up->reqid = kp->reqid;
1116 up->mode = kp->mode;
1117 up->share = kp->share;
1118 up->optional = kp->optional;
1119 up->aalgos = kp->aalgos;
1120 up->ealgos = kp->ealgos;
1121 up->calgos = kp->calgos;
1122 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123
Thomas Grafc0144be2007-08-22 13:55:43 -07001124 return nla_put(skb, XFRMA_TMPL,
1125 sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr, vec);
Trent Jaegerdf718372005-12-13 23:12:27 -08001126}
1127
Serge Hallyn0d681622006-07-24 23:30:44 -07001128static inline int copy_to_user_state_sec_ctx(struct xfrm_state *x, struct sk_buff *skb)
1129{
1130 if (x->security) {
1131 return copy_sec_ctx(x->security, skb);
1132 }
1133 return 0;
1134}
1135
1136static inline int copy_to_user_sec_ctx(struct xfrm_policy *xp, struct sk_buff *skb)
1137{
1138 if (xp->security) {
1139 return copy_sec_ctx(xp->security, skb);
1140 }
1141 return 0;
1142}
Thomas Grafcfbfd452007-08-22 13:57:04 -07001143static inline size_t userpolicy_type_attrsize(void)
1144{
1145#ifdef CONFIG_XFRM_SUB_POLICY
1146 return nla_total_size(sizeof(struct xfrm_userpolicy_type));
1147#else
1148 return 0;
1149#endif
1150}
Serge Hallyn0d681622006-07-24 23:30:44 -07001151
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001152#ifdef CONFIG_XFRM_SUB_POLICY
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001153static int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001154{
Thomas Grafc0144be2007-08-22 13:55:43 -07001155 struct xfrm_userpolicy_type upt = {
1156 .type = type,
1157 };
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001158
Thomas Grafc0144be2007-08-22 13:55:43 -07001159 return nla_put(skb, XFRMA_POLICY_TYPE, sizeof(upt), &upt);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001160}
1161
1162#else
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001163static inline int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001164{
1165 return 0;
1166}
1167#endif
1168
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169static int dump_one_policy(struct xfrm_policy *xp, int dir, int count, void *ptr)
1170{
1171 struct xfrm_dump_info *sp = ptr;
1172 struct xfrm_userpolicy_info *p;
1173 struct sk_buff *in_skb = sp->in_skb;
1174 struct sk_buff *skb = sp->out_skb;
1175 struct nlmsghdr *nlh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176
1177 if (sp->this_idx < sp->start_idx)
1178 goto out;
1179
Thomas Graf79b8b7f2007-08-22 12:46:53 -07001180 nlh = nlmsg_put(skb, NETLINK_CB(in_skb).pid, sp->nlmsg_seq,
1181 XFRM_MSG_NEWPOLICY, sizeof(*p), sp->nlmsg_flags);
1182 if (nlh == NULL)
1183 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184
Thomas Graf7b67c852007-08-22 13:53:52 -07001185 p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 copy_to_user_policy(xp, p, dir);
1187 if (copy_to_user_tmpl(xp, skb) < 0)
1188 goto nlmsg_failure;
Trent Jaegerdf718372005-12-13 23:12:27 -08001189 if (copy_to_user_sec_ctx(xp, skb))
1190 goto nlmsg_failure;
Jamal Hadi Salim1459bb32006-11-20 16:51:22 -08001191 if (copy_to_user_policy_type(xp->type, skb) < 0)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001192 goto nlmsg_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193
Thomas Graf98250692007-08-22 12:47:26 -07001194 nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195out:
1196 sp->this_idx++;
1197 return 0;
1198
1199nlmsg_failure:
Thomas Graf98250692007-08-22 12:47:26 -07001200 nlmsg_cancel(skb, nlh);
1201 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202}
1203
1204static int xfrm_dump_policy(struct sk_buff *skb, struct netlink_callback *cb)
1205{
1206 struct xfrm_dump_info info;
1207
1208 info.in_skb = cb->skb;
1209 info.out_skb = skb;
1210 info.nlmsg_seq = cb->nlh->nlmsg_seq;
1211 info.nlmsg_flags = NLM_F_MULTI;
1212 info.this_idx = 0;
1213 info.start_idx = cb->args[0];
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001214 (void) xfrm_policy_walk(XFRM_POLICY_TYPE_MAIN, dump_one_policy, &info);
1215#ifdef CONFIG_XFRM_SUB_POLICY
1216 (void) xfrm_policy_walk(XFRM_POLICY_TYPE_SUB, dump_one_policy, &info);
1217#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218 cb->args[0] = info.this_idx;
1219
1220 return skb->len;
1221}
1222
1223static struct sk_buff *xfrm_policy_netlink(struct sk_buff *in_skb,
1224 struct xfrm_policy *xp,
1225 int dir, u32 seq)
1226{
1227 struct xfrm_dump_info info;
1228 struct sk_buff *skb;
1229
Thomas Graf7deb2262007-08-22 13:57:39 -07001230 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 if (!skb)
1232 return ERR_PTR(-ENOMEM);
1233
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234 info.in_skb = in_skb;
1235 info.out_skb = skb;
1236 info.nlmsg_seq = seq;
1237 info.nlmsg_flags = 0;
1238 info.this_idx = info.start_idx = 0;
1239
1240 if (dump_one_policy(xp, dir, 0, &info) < 0) {
1241 kfree_skb(skb);
1242 return NULL;
1243 }
1244
1245 return skb;
1246}
1247
Christoph Hellwig22e70052007-01-02 15:22:30 -08001248static int xfrm_get_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001249 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250{
1251 struct xfrm_policy *xp;
1252 struct xfrm_userpolicy_id *p;
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001253 u8 type = XFRM_POLICY_TYPE_MAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 int err;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001255 struct km_event c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 int delete;
1257
Thomas Graf7b67c852007-08-22 13:53:52 -07001258 p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 delete = nlh->nlmsg_type == XFRM_MSG_DELPOLICY;
1260
Thomas Graf35a7aa02007-08-22 14:00:40 -07001261 err = copy_from_user_policy_type(&type, attrs);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001262 if (err)
1263 return err;
1264
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 err = verify_policy_dir(p->dir);
1266 if (err)
1267 return err;
1268
1269 if (p->index)
Eric Parisef41aaa2007-03-07 15:37:58 -08001270 xp = xfrm_policy_byid(type, p->dir, p->index, delete, &err);
Trent Jaegerdf718372005-12-13 23:12:27 -08001271 else {
Thomas Graf5424f322007-08-22 14:01:33 -07001272 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
Trent Jaegerdf718372005-12-13 23:12:27 -08001273 struct xfrm_policy tmp;
1274
Thomas Graf35a7aa02007-08-22 14:00:40 -07001275 err = verify_sec_ctx_len(attrs);
Trent Jaegerdf718372005-12-13 23:12:27 -08001276 if (err)
1277 return err;
1278
1279 memset(&tmp, 0, sizeof(struct xfrm_policy));
1280 if (rt) {
Thomas Graf5424f322007-08-22 14:01:33 -07001281 struct xfrm_user_sec_ctx *uctx = nla_data(rt);
Trent Jaegerdf718372005-12-13 23:12:27 -08001282
1283 if ((err = security_xfrm_policy_alloc(&tmp, uctx)))
1284 return err;
1285 }
Eric Parisef41aaa2007-03-07 15:37:58 -08001286 xp = xfrm_policy_bysel_ctx(type, p->dir, &p->sel, tmp.security,
1287 delete, &err);
Trent Jaegerdf718372005-12-13 23:12:27 -08001288 security_xfrm_policy_free(&tmp);
1289 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290 if (xp == NULL)
1291 return -ENOENT;
1292
1293 if (!delete) {
1294 struct sk_buff *resp_skb;
1295
1296 resp_skb = xfrm_policy_netlink(skb, xp, p->dir, nlh->nlmsg_seq);
1297 if (IS_ERR(resp_skb)) {
1298 err = PTR_ERR(resp_skb);
1299 } else {
Thomas Graf082a1ad2007-08-22 13:54:36 -07001300 err = nlmsg_unicast(xfrm_nl, resp_skb,
1301 NETLINK_CB(skb).pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 }
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001303 } else {
Joy Lattenab5f5e82007-09-17 11:51:22 -07001304 xfrm_audit_policy_delete(xp, err ? 0 : 1,
1305 NETLINK_CB(skb).loginuid,
1306 NETLINK_CB(skb).sid);
David S. Miller13fcfbb02007-02-12 13:53:54 -08001307
1308 if (err != 0)
Catherine Zhangc8c05a82006-06-08 23:39:49 -07001309 goto out;
David S. Miller13fcfbb02007-02-12 13:53:54 -08001310
Herbert Xue7443892005-06-18 22:44:18 -07001311 c.data.byid = p->index;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001312 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001313 c.seq = nlh->nlmsg_seq;
1314 c.pid = nlh->nlmsg_pid;
1315 km_policy_notify(xp, p->dir, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 }
1317
Catherine Zhangc8c05a82006-06-08 23:39:49 -07001318out:
Eric Parisef41aaa2007-03-07 15:37:58 -08001319 xfrm_pol_put(xp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320 return err;
1321}
1322
Christoph Hellwig22e70052007-01-02 15:22:30 -08001323static int xfrm_flush_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001324 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325{
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001326 struct km_event c;
Thomas Graf7b67c852007-08-22 13:53:52 -07001327 struct xfrm_usersa_flush *p = nlmsg_data(nlh);
Joy Latten161a09e2006-11-27 13:11:54 -06001328 struct xfrm_audit audit_info;
Joy Latten4aa2e622007-06-04 19:05:57 -04001329 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330
Joy Latten161a09e2006-11-27 13:11:54 -06001331 audit_info.loginuid = NETLINK_CB(skb).loginuid;
1332 audit_info.secid = NETLINK_CB(skb).sid;
Joy Latten4aa2e622007-06-04 19:05:57 -04001333 err = xfrm_state_flush(p->proto, &audit_info);
1334 if (err)
1335 return err;
Herbert Xubf088672005-06-18 22:44:00 -07001336 c.data.proto = p->proto;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001337 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001338 c.seq = nlh->nlmsg_seq;
1339 c.pid = nlh->nlmsg_pid;
1340 km_state_notify(NULL, &c);
1341
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342 return 0;
1343}
1344
Thomas Graf7deb2262007-08-22 13:57:39 -07001345static inline size_t xfrm_aevent_msgsize(void)
1346{
1347 return NLMSG_ALIGN(sizeof(struct xfrm_aevent_id))
1348 + nla_total_size(sizeof(struct xfrm_replay_state))
1349 + nla_total_size(sizeof(struct xfrm_lifetime_cur))
1350 + nla_total_size(4) /* XFRM_AE_RTHR */
1351 + nla_total_size(4); /* XFRM_AE_ETHR */
1352}
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001353
1354static int build_aevent(struct sk_buff *skb, struct xfrm_state *x, struct km_event *c)
1355{
1356 struct xfrm_aevent_id *id;
1357 struct nlmsghdr *nlh;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001358
Thomas Graf79b8b7f2007-08-22 12:46:53 -07001359 nlh = nlmsg_put(skb, c->pid, c->seq, XFRM_MSG_NEWAE, sizeof(*id), 0);
1360 if (nlh == NULL)
1361 return -EMSGSIZE;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001362
Thomas Graf7b67c852007-08-22 13:53:52 -07001363 id = nlmsg_data(nlh);
Jamal Hadi Salim2b5f6dc2006-12-02 22:22:25 -08001364 memcpy(&id->sa_id.daddr, &x->id.daddr,sizeof(x->id.daddr));
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001365 id->sa_id.spi = x->id.spi;
1366 id->sa_id.family = x->props.family;
1367 id->sa_id.proto = x->id.proto;
Jamal Hadi Salim2b5f6dc2006-12-02 22:22:25 -08001368 memcpy(&id->saddr, &x->props.saddr,sizeof(x->props.saddr));
1369 id->reqid = x->props.reqid;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001370 id->flags = c->data.aevent;
1371
Thomas Grafc0144be2007-08-22 13:55:43 -07001372 NLA_PUT(skb, XFRMA_REPLAY_VAL, sizeof(x->replay), &x->replay);
1373 NLA_PUT(skb, XFRMA_LTIME_VAL, sizeof(x->curlft), &x->curlft);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001374
Thomas Grafc0144be2007-08-22 13:55:43 -07001375 if (id->flags & XFRM_AE_RTHR)
1376 NLA_PUT_U32(skb, XFRMA_REPLAY_THRESH, x->replay_maxdiff);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001377
Thomas Grafc0144be2007-08-22 13:55:43 -07001378 if (id->flags & XFRM_AE_ETHR)
1379 NLA_PUT_U32(skb, XFRMA_ETIMER_THRESH,
1380 x->replay_maxage * 10 / HZ);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001381
Thomas Graf98250692007-08-22 12:47:26 -07001382 return nlmsg_end(skb, nlh);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001383
Thomas Grafc0144be2007-08-22 13:55:43 -07001384nla_put_failure:
Thomas Graf98250692007-08-22 12:47:26 -07001385 nlmsg_cancel(skb, nlh);
1386 return -EMSGSIZE;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001387}
1388
Christoph Hellwig22e70052007-01-02 15:22:30 -08001389static int xfrm_get_ae(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001390 struct nlattr **attrs)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001391{
1392 struct xfrm_state *x;
1393 struct sk_buff *r_skb;
1394 int err;
1395 struct km_event c;
Thomas Graf7b67c852007-08-22 13:53:52 -07001396 struct xfrm_aevent_id *p = nlmsg_data(nlh);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001397 struct xfrm_usersa_id *id = &p->sa_id;
1398
Thomas Graf7deb2262007-08-22 13:57:39 -07001399 r_skb = nlmsg_new(xfrm_aevent_msgsize(), GFP_ATOMIC);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001400 if (r_skb == NULL)
1401 return -ENOMEM;
1402
1403 x = xfrm_state_lookup(&id->daddr, id->spi, id->proto, id->family);
1404 if (x == NULL) {
Patrick McHardyb08d5842007-02-27 09:57:37 -08001405 kfree_skb(r_skb);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001406 return -ESRCH;
1407 }
1408
1409 /*
1410 * XXX: is this lock really needed - none of the other
1411 * gets lock (the concern is things getting updated
1412 * while we are still reading) - jhs
1413 */
1414 spin_lock_bh(&x->lock);
1415 c.data.aevent = p->flags;
1416 c.seq = nlh->nlmsg_seq;
1417 c.pid = nlh->nlmsg_pid;
1418
1419 if (build_aevent(r_skb, x, &c) < 0)
1420 BUG();
Thomas Graf082a1ad2007-08-22 13:54:36 -07001421 err = nlmsg_unicast(xfrm_nl, r_skb, NETLINK_CB(skb).pid);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001422 spin_unlock_bh(&x->lock);
1423 xfrm_state_put(x);
1424 return err;
1425}
1426
Christoph Hellwig22e70052007-01-02 15:22:30 -08001427static int xfrm_new_ae(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001428 struct nlattr **attrs)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001429{
1430 struct xfrm_state *x;
1431 struct km_event c;
1432 int err = - EINVAL;
Thomas Graf7b67c852007-08-22 13:53:52 -07001433 struct xfrm_aevent_id *p = nlmsg_data(nlh);
Thomas Graf5424f322007-08-22 14:01:33 -07001434 struct nlattr *rp = attrs[XFRMA_REPLAY_VAL];
1435 struct nlattr *lt = attrs[XFRMA_LTIME_VAL];
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001436
1437 if (!lt && !rp)
1438 return err;
1439
1440 /* pedantic mode - thou shalt sayeth replaceth */
1441 if (!(nlh->nlmsg_flags&NLM_F_REPLACE))
1442 return err;
1443
1444 x = xfrm_state_lookup(&p->sa_id.daddr, p->sa_id.spi, p->sa_id.proto, p->sa_id.family);
1445 if (x == NULL)
1446 return -ESRCH;
1447
1448 if (x->km.state != XFRM_STATE_VALID)
1449 goto out;
1450
1451 spin_lock_bh(&x->lock);
Thomas Graf35a7aa02007-08-22 14:00:40 -07001452 xfrm_update_ae_params(x, attrs);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001453 spin_unlock_bh(&x->lock);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001454
1455 c.event = nlh->nlmsg_type;
1456 c.seq = nlh->nlmsg_seq;
1457 c.pid = nlh->nlmsg_pid;
1458 c.data.aevent = XFRM_AE_CU;
1459 km_state_notify(x, &c);
1460 err = 0;
1461out:
1462 xfrm_state_put(x);
1463 return err;
1464}
1465
Christoph Hellwig22e70052007-01-02 15:22:30 -08001466static int xfrm_flush_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001467 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468{
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001469 struct km_event c;
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001470 u8 type = XFRM_POLICY_TYPE_MAIN;
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001471 int err;
Joy Latten161a09e2006-11-27 13:11:54 -06001472 struct xfrm_audit audit_info;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001473
Thomas Graf35a7aa02007-08-22 14:00:40 -07001474 err = copy_from_user_policy_type(&type, attrs);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001475 if (err)
1476 return err;
1477
Joy Latten161a09e2006-11-27 13:11:54 -06001478 audit_info.loginuid = NETLINK_CB(skb).loginuid;
1479 audit_info.secid = NETLINK_CB(skb).sid;
Joy Latten4aa2e622007-06-04 19:05:57 -04001480 err = xfrm_policy_flush(type, &audit_info);
1481 if (err)
1482 return err;
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001483 c.data.type = type;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001484 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001485 c.seq = nlh->nlmsg_seq;
1486 c.pid = nlh->nlmsg_pid;
1487 km_policy_notify(NULL, 0, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488 return 0;
1489}
1490
Christoph Hellwig22e70052007-01-02 15:22:30 -08001491static int xfrm_add_pol_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001492 struct nlattr **attrs)
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001493{
1494 struct xfrm_policy *xp;
Thomas Graf7b67c852007-08-22 13:53:52 -07001495 struct xfrm_user_polexpire *up = nlmsg_data(nlh);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001496 struct xfrm_userpolicy_info *p = &up->pol;
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001497 u8 type = XFRM_POLICY_TYPE_MAIN;
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001498 int err = -ENOENT;
1499
Thomas Graf35a7aa02007-08-22 14:00:40 -07001500 err = copy_from_user_policy_type(&type, attrs);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001501 if (err)
1502 return err;
1503
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001504 if (p->index)
Eric Parisef41aaa2007-03-07 15:37:58 -08001505 xp = xfrm_policy_byid(type, p->dir, p->index, 0, &err);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001506 else {
Thomas Graf5424f322007-08-22 14:01:33 -07001507 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001508 struct xfrm_policy tmp;
1509
Thomas Graf35a7aa02007-08-22 14:00:40 -07001510 err = verify_sec_ctx_len(attrs);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001511 if (err)
1512 return err;
1513
1514 memset(&tmp, 0, sizeof(struct xfrm_policy));
1515 if (rt) {
Thomas Graf5424f322007-08-22 14:01:33 -07001516 struct xfrm_user_sec_ctx *uctx = nla_data(rt);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001517
1518 if ((err = security_xfrm_policy_alloc(&tmp, uctx)))
1519 return err;
1520 }
Eric Parisef41aaa2007-03-07 15:37:58 -08001521 xp = xfrm_policy_bysel_ctx(type, p->dir, &p->sel, tmp.security,
1522 0, &err);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001523 security_xfrm_policy_free(&tmp);
1524 }
1525
1526 if (xp == NULL)
Eric Parisef41aaa2007-03-07 15:37:58 -08001527 return -ENOENT;
1528 read_lock(&xp->lock);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001529 if (xp->dead) {
1530 read_unlock(&xp->lock);
1531 goto out;
1532 }
1533
1534 read_unlock(&xp->lock);
1535 err = 0;
1536 if (up->hard) {
1537 xfrm_policy_delete(xp, p->dir);
Joy Lattenab5f5e82007-09-17 11:51:22 -07001538 xfrm_audit_policy_delete(xp, 1, NETLINK_CB(skb).loginuid,
1539 NETLINK_CB(skb).sid);
Joy Latten161a09e2006-11-27 13:11:54 -06001540
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001541 } else {
1542 // reset the timers here?
1543 printk("Dont know what to do with soft policy expire\n");
1544 }
1545 km_policy_expired(xp, p->dir, up->hard, current->pid);
1546
1547out:
1548 xfrm_pol_put(xp);
1549 return err;
1550}
1551
Christoph Hellwig22e70052007-01-02 15:22:30 -08001552static int xfrm_add_sa_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001553 struct nlattr **attrs)
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001554{
1555 struct xfrm_state *x;
1556 int err;
Thomas Graf7b67c852007-08-22 13:53:52 -07001557 struct xfrm_user_expire *ue = nlmsg_data(nlh);
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001558 struct xfrm_usersa_info *p = &ue->state;
1559
1560 x = xfrm_state_lookup(&p->id.daddr, p->id.spi, p->id.proto, p->family);
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001561
David S. Miller3a765aa2007-02-26 14:52:21 -08001562 err = -ENOENT;
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001563 if (x == NULL)
1564 return err;
1565
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001566 spin_lock_bh(&x->lock);
David S. Miller3a765aa2007-02-26 14:52:21 -08001567 err = -EINVAL;
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001568 if (x->km.state != XFRM_STATE_VALID)
1569 goto out;
1570 km_state_expired(x, ue->hard, current->pid);
1571
Joy Latten161a09e2006-11-27 13:11:54 -06001572 if (ue->hard) {
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001573 __xfrm_state_delete(x);
Joy Lattenab5f5e82007-09-17 11:51:22 -07001574 xfrm_audit_state_delete(x, 1, NETLINK_CB(skb).loginuid,
1575 NETLINK_CB(skb).sid);
Joy Latten161a09e2006-11-27 13:11:54 -06001576 }
David S. Miller3a765aa2007-02-26 14:52:21 -08001577 err = 0;
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001578out:
1579 spin_unlock_bh(&x->lock);
1580 xfrm_state_put(x);
1581 return err;
1582}
1583
Christoph Hellwig22e70052007-01-02 15:22:30 -08001584static int xfrm_add_acquire(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001585 struct nlattr **attrs)
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001586{
1587 struct xfrm_policy *xp;
1588 struct xfrm_user_tmpl *ut;
1589 int i;
Thomas Graf5424f322007-08-22 14:01:33 -07001590 struct nlattr *rt = attrs[XFRMA_TMPL];
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001591
Thomas Graf7b67c852007-08-22 13:53:52 -07001592 struct xfrm_user_acquire *ua = nlmsg_data(nlh);
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001593 struct xfrm_state *x = xfrm_state_alloc();
1594 int err = -ENOMEM;
1595
1596 if (!x)
1597 return err;
1598
1599 err = verify_newpolicy_info(&ua->policy);
1600 if (err) {
1601 printk("BAD policy passed\n");
1602 kfree(x);
1603 return err;
1604 }
1605
1606 /* build an XP */
Thomas Graf5424f322007-08-22 14:01:33 -07001607 xp = xfrm_policy_construct(&ua->policy, attrs, &err);
David S. Millerb4ad86bf2006-12-03 19:19:26 -08001608 if (!xp) {
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001609 kfree(x);
1610 return err;
1611 }
1612
1613 memcpy(&x->id, &ua->id, sizeof(ua->id));
1614 memcpy(&x->props.saddr, &ua->saddr, sizeof(ua->saddr));
1615 memcpy(&x->sel, &ua->sel, sizeof(ua->sel));
1616
Thomas Graf5424f322007-08-22 14:01:33 -07001617 ut = nla_data(rt);
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001618 /* extract the templates and for each call km_key */
1619 for (i = 0; i < xp->xfrm_nr; i++, ut++) {
1620 struct xfrm_tmpl *t = &xp->xfrm_vec[i];
1621 memcpy(&x->id, &t->id, sizeof(x->id));
1622 x->props.mode = t->mode;
1623 x->props.reqid = t->reqid;
1624 x->props.family = ut->family;
1625 t->aalgos = ua->aalgos;
1626 t->ealgos = ua->ealgos;
1627 t->calgos = ua->calgos;
1628 err = km_query(x, t, xp);
1629
1630 }
1631
1632 kfree(x);
1633 kfree(xp);
1634
1635 return 0;
1636}
1637
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001638#ifdef CONFIG_XFRM_MIGRATE
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001639static int copy_from_user_migrate(struct xfrm_migrate *ma,
Thomas Graf5424f322007-08-22 14:01:33 -07001640 struct nlattr **attrs, int *num)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001641{
Thomas Graf5424f322007-08-22 14:01:33 -07001642 struct nlattr *rt = attrs[XFRMA_MIGRATE];
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001643 struct xfrm_user_migrate *um;
1644 int i, num_migrate;
1645
Thomas Graf5424f322007-08-22 14:01:33 -07001646 um = nla_data(rt);
1647 num_migrate = nla_len(rt) / sizeof(*um);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001648
1649 if (num_migrate <= 0 || num_migrate > XFRM_MAX_DEPTH)
1650 return -EINVAL;
1651
1652 for (i = 0; i < num_migrate; i++, um++, ma++) {
1653 memcpy(&ma->old_daddr, &um->old_daddr, sizeof(ma->old_daddr));
1654 memcpy(&ma->old_saddr, &um->old_saddr, sizeof(ma->old_saddr));
1655 memcpy(&ma->new_daddr, &um->new_daddr, sizeof(ma->new_daddr));
1656 memcpy(&ma->new_saddr, &um->new_saddr, sizeof(ma->new_saddr));
1657
1658 ma->proto = um->proto;
1659 ma->mode = um->mode;
1660 ma->reqid = um->reqid;
1661
1662 ma->old_family = um->old_family;
1663 ma->new_family = um->new_family;
1664 }
1665
1666 *num = i;
1667 return 0;
1668}
1669
1670static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001671 struct nlattr **attrs)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001672{
Thomas Graf7b67c852007-08-22 13:53:52 -07001673 struct xfrm_userpolicy_id *pi = nlmsg_data(nlh);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001674 struct xfrm_migrate m[XFRM_MAX_DEPTH];
1675 u8 type;
1676 int err;
1677 int n = 0;
1678
Thomas Graf35a7aa02007-08-22 14:00:40 -07001679 if (attrs[XFRMA_MIGRATE] == NULL)
Thomas Grafcf5cb792007-08-22 13:59:04 -07001680 return -EINVAL;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001681
Thomas Graf5424f322007-08-22 14:01:33 -07001682 err = copy_from_user_policy_type(&type, attrs);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001683 if (err)
1684 return err;
1685
1686 err = copy_from_user_migrate((struct xfrm_migrate *)m,
Thomas Graf5424f322007-08-22 14:01:33 -07001687 attrs, &n);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001688 if (err)
1689 return err;
1690
1691 if (!n)
1692 return 0;
1693
1694 xfrm_migrate(&pi->sel, pi->dir, type, m, n);
1695
1696 return 0;
1697}
1698#else
1699static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001700 struct nlattr **attrs)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001701{
1702 return -ENOPROTOOPT;
1703}
1704#endif
1705
1706#ifdef CONFIG_XFRM_MIGRATE
1707static int copy_to_user_migrate(struct xfrm_migrate *m, struct sk_buff *skb)
1708{
1709 struct xfrm_user_migrate um;
1710
1711 memset(&um, 0, sizeof(um));
1712 um.proto = m->proto;
1713 um.mode = m->mode;
1714 um.reqid = m->reqid;
1715 um.old_family = m->old_family;
1716 memcpy(&um.old_daddr, &m->old_daddr, sizeof(um.old_daddr));
1717 memcpy(&um.old_saddr, &m->old_saddr, sizeof(um.old_saddr));
1718 um.new_family = m->new_family;
1719 memcpy(&um.new_daddr, &m->new_daddr, sizeof(um.new_daddr));
1720 memcpy(&um.new_saddr, &m->new_saddr, sizeof(um.new_saddr));
1721
Thomas Grafc0144be2007-08-22 13:55:43 -07001722 return nla_put(skb, XFRMA_MIGRATE, sizeof(um), &um);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001723}
1724
Thomas Graf7deb2262007-08-22 13:57:39 -07001725static inline size_t xfrm_migrate_msgsize(int num_migrate)
1726{
1727 return NLMSG_ALIGN(sizeof(struct xfrm_userpolicy_id))
1728 + nla_total_size(sizeof(struct xfrm_user_migrate) * num_migrate)
1729 + userpolicy_type_attrsize();
1730}
1731
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001732static int build_migrate(struct sk_buff *skb, struct xfrm_migrate *m,
1733 int num_migrate, struct xfrm_selector *sel,
1734 u8 dir, u8 type)
1735{
1736 struct xfrm_migrate *mp;
1737 struct xfrm_userpolicy_id *pol_id;
1738 struct nlmsghdr *nlh;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001739 int i;
1740
Thomas Graf79b8b7f2007-08-22 12:46:53 -07001741 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_MIGRATE, sizeof(*pol_id), 0);
1742 if (nlh == NULL)
1743 return -EMSGSIZE;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001744
Thomas Graf7b67c852007-08-22 13:53:52 -07001745 pol_id = nlmsg_data(nlh);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001746 /* copy data from selector, dir, and type to the pol_id */
1747 memset(pol_id, 0, sizeof(*pol_id));
1748 memcpy(&pol_id->sel, sel, sizeof(pol_id->sel));
1749 pol_id->dir = dir;
1750
1751 if (copy_to_user_policy_type(type, skb) < 0)
1752 goto nlmsg_failure;
1753
1754 for (i = 0, mp = m ; i < num_migrate; i++, mp++) {
1755 if (copy_to_user_migrate(mp, skb) < 0)
1756 goto nlmsg_failure;
1757 }
1758
Thomas Graf98250692007-08-22 12:47:26 -07001759 return nlmsg_end(skb, nlh);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001760nlmsg_failure:
Thomas Graf98250692007-08-22 12:47:26 -07001761 nlmsg_cancel(skb, nlh);
1762 return -EMSGSIZE;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001763}
1764
1765static int xfrm_send_migrate(struct xfrm_selector *sel, u8 dir, u8 type,
1766 struct xfrm_migrate *m, int num_migrate)
1767{
1768 struct sk_buff *skb;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001769
Thomas Graf7deb2262007-08-22 13:57:39 -07001770 skb = nlmsg_new(xfrm_migrate_msgsize(num_migrate), GFP_ATOMIC);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001771 if (skb == NULL)
1772 return -ENOMEM;
1773
1774 /* build migrate */
1775 if (build_migrate(skb, m, num_migrate, sel, dir, type) < 0)
1776 BUG();
1777
Thomas Graf082a1ad2007-08-22 13:54:36 -07001778 return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_MIGRATE, GFP_ATOMIC);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001779}
1780#else
1781static int xfrm_send_migrate(struct xfrm_selector *sel, u8 dir, u8 type,
1782 struct xfrm_migrate *m, int num_migrate)
1783{
1784 return -ENOPROTOOPT;
1785}
1786#endif
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001787
Thomas Grafa7bd9a42007-08-22 13:58:18 -07001788#define XMSGSIZE(type) sizeof(struct type)
Thomas Graf492b5582005-05-03 14:26:40 -07001789
1790static const int xfrm_msg_min[XFRM_NR_MSGTYPES] = {
1791 [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
1792 [XFRM_MSG_DELSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
1793 [XFRM_MSG_GETSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
1794 [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
1795 [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
1796 [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
1797 [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userspi_info),
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001798 [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_acquire),
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001799 [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_expire),
Thomas Graf492b5582005-05-03 14:26:40 -07001800 [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
1801 [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001802 [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_polexpire),
Thomas Graf492b5582005-05-03 14:26:40 -07001803 [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_flush),
Thomas Grafa7bd9a42007-08-22 13:58:18 -07001804 [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = 0,
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001805 [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
1806 [XFRM_MSG_GETAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07001807 [XFRM_MSG_REPORT - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_report),
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001808 [XFRM_MSG_MIGRATE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
Thomas Grafa7bd9a42007-08-22 13:58:18 -07001809 [XFRM_MSG_GETSADINFO - XFRM_MSG_BASE] = sizeof(u32),
1810 [XFRM_MSG_GETSPDINFO - XFRM_MSG_BASE] = sizeof(u32),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811};
1812
Thomas Graf492b5582005-05-03 14:26:40 -07001813#undef XMSGSIZE
1814
Thomas Grafcf5cb792007-08-22 13:59:04 -07001815static const struct nla_policy xfrma_policy[XFRMA_MAX+1] = {
1816 [XFRMA_ALG_AUTH] = { .len = sizeof(struct xfrm_algo) },
1817 [XFRMA_ALG_CRYPT] = { .len = sizeof(struct xfrm_algo) },
1818 [XFRMA_ALG_COMP] = { .len = sizeof(struct xfrm_algo) },
1819 [XFRMA_ENCAP] = { .len = sizeof(struct xfrm_encap_tmpl) },
1820 [XFRMA_TMPL] = { .len = sizeof(struct xfrm_user_tmpl) },
1821 [XFRMA_SEC_CTX] = { .len = sizeof(struct xfrm_sec_ctx) },
1822 [XFRMA_LTIME_VAL] = { .len = sizeof(struct xfrm_lifetime_cur) },
1823 [XFRMA_REPLAY_VAL] = { .len = sizeof(struct xfrm_replay_state) },
1824 [XFRMA_REPLAY_THRESH] = { .type = NLA_U32 },
1825 [XFRMA_ETIMER_THRESH] = { .type = NLA_U32 },
1826 [XFRMA_SRCADDR] = { .len = sizeof(xfrm_address_t) },
1827 [XFRMA_COADDR] = { .len = sizeof(xfrm_address_t) },
1828 [XFRMA_POLICY_TYPE] = { .len = sizeof(struct xfrm_userpolicy_type)},
1829 [XFRMA_MIGRATE] = { .len = sizeof(struct xfrm_user_migrate) },
1830};
1831
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832static struct xfrm_link {
Thomas Graf5424f322007-08-22 14:01:33 -07001833 int (*doit)(struct sk_buff *, struct nlmsghdr *, struct nlattr **);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834 int (*dump)(struct sk_buff *, struct netlink_callback *);
Thomas Graf492b5582005-05-03 14:26:40 -07001835} xfrm_dispatch[XFRM_NR_MSGTYPES] = {
1836 [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa },
1837 [XFRM_MSG_DELSA - XFRM_MSG_BASE] = { .doit = xfrm_del_sa },
1838 [XFRM_MSG_GETSA - XFRM_MSG_BASE] = { .doit = xfrm_get_sa,
1839 .dump = xfrm_dump_sa },
1840 [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy },
1841 [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy },
1842 [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy,
1843 .dump = xfrm_dump_policy },
1844 [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = { .doit = xfrm_alloc_userspi },
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001845 [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_acquire },
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001846 [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_sa_expire },
Thomas Graf492b5582005-05-03 14:26:40 -07001847 [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy },
1848 [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa },
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001849 [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_pol_expire},
Thomas Graf492b5582005-05-03 14:26:40 -07001850 [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = { .doit = xfrm_flush_sa },
1851 [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_flush_policy },
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001852 [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = { .doit = xfrm_new_ae },
1853 [XFRM_MSG_GETAE - XFRM_MSG_BASE] = { .doit = xfrm_get_ae },
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001854 [XFRM_MSG_MIGRATE - XFRM_MSG_BASE] = { .doit = xfrm_do_migrate },
Jamal Hadi Salim566ec032007-04-26 14:12:15 -07001855 [XFRM_MSG_GETSADINFO - XFRM_MSG_BASE] = { .doit = xfrm_get_sadinfo },
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -07001856 [XFRM_MSG_GETSPDINFO - XFRM_MSG_BASE] = { .doit = xfrm_get_spdinfo },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857};
1858
Thomas Graf1d00a4e2007-03-22 23:30:12 -07001859static int xfrm_user_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860{
Thomas Graf35a7aa02007-08-22 14:00:40 -07001861 struct nlattr *attrs[XFRMA_MAX+1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862 struct xfrm_link *link;
Thomas Grafa7bd9a42007-08-22 13:58:18 -07001863 int type, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865 type = nlh->nlmsg_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866 if (type > XFRM_MSG_MAX)
Thomas Graf1d00a4e2007-03-22 23:30:12 -07001867 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868
1869 type -= XFRM_MSG_BASE;
1870 link = &xfrm_dispatch[type];
1871
1872 /* All operations require privileges, even GET */
Thomas Graf1d00a4e2007-03-22 23:30:12 -07001873 if (security_netlink_recv(skb, CAP_NET_ADMIN))
1874 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875
Thomas Graf492b5582005-05-03 14:26:40 -07001876 if ((type == (XFRM_MSG_GETSA - XFRM_MSG_BASE) ||
1877 type == (XFRM_MSG_GETPOLICY - XFRM_MSG_BASE)) &&
1878 (nlh->nlmsg_flags & NLM_F_DUMP)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879 if (link->dump == NULL)
Thomas Graf1d00a4e2007-03-22 23:30:12 -07001880 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881
Thomas Grafc702e802007-03-22 23:30:55 -07001882 return netlink_dump_start(xfrm_nl, skb, nlh, link->dump, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883 }
1884
Thomas Graf35a7aa02007-08-22 14:00:40 -07001885 err = nlmsg_parse(nlh, xfrm_msg_min[type], attrs, XFRMA_MAX,
Thomas Grafcf5cb792007-08-22 13:59:04 -07001886 xfrma_policy);
Thomas Grafa7bd9a42007-08-22 13:58:18 -07001887 if (err < 0)
1888 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889
1890 if (link->doit == NULL)
Thomas Graf1d00a4e2007-03-22 23:30:12 -07001891 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892
Thomas Graf5424f322007-08-22 14:01:33 -07001893 return link->doit(skb, nlh, attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894}
1895
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896static void xfrm_netlink_rcv(struct sock *sk, int len)
1897{
Thomas Graf88fc2c82005-11-10 02:25:54 +01001898 unsigned int qlen = 0;
Herbert Xu2a0a6eb2005-05-03 14:55:09 -07001899
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900 do {
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -08001901 mutex_lock(&xfrm_cfg_mutex);
Herbert Xu0cfad072007-09-16 16:24:44 -07001902 qlen = netlink_run_queue(sk, qlen, &xfrm_user_rcv_msg);
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -08001903 mutex_unlock(&xfrm_cfg_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904
Herbert Xu2a0a6eb2005-05-03 14:55:09 -07001905 } while (qlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906}
1907
Thomas Graf7deb2262007-08-22 13:57:39 -07001908static inline size_t xfrm_expire_msgsize(void)
1909{
1910 return NLMSG_ALIGN(sizeof(struct xfrm_user_expire));
1911}
1912
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001913static int build_expire(struct sk_buff *skb, struct xfrm_state *x, struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914{
1915 struct xfrm_user_expire *ue;
1916 struct nlmsghdr *nlh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917
Thomas Graf79b8b7f2007-08-22 12:46:53 -07001918 nlh = nlmsg_put(skb, c->pid, 0, XFRM_MSG_EXPIRE, sizeof(*ue), 0);
1919 if (nlh == NULL)
1920 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921
Thomas Graf7b67c852007-08-22 13:53:52 -07001922 ue = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923 copy_to_user_state(x, &ue->state);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001924 ue->hard = (c->data.hard != 0) ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925
Thomas Graf98250692007-08-22 12:47:26 -07001926 return nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927}
1928
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001929static int xfrm_exp_state_notify(struct xfrm_state *x, struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930{
1931 struct sk_buff *skb;
1932
Thomas Graf7deb2262007-08-22 13:57:39 -07001933 skb = nlmsg_new(xfrm_expire_msgsize(), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934 if (skb == NULL)
1935 return -ENOMEM;
1936
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001937 if (build_expire(skb, x, c) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938 BUG();
1939
Thomas Graf082a1ad2007-08-22 13:54:36 -07001940 return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_EXPIRE, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941}
1942
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001943static int xfrm_aevent_state_notify(struct xfrm_state *x, struct km_event *c)
1944{
1945 struct sk_buff *skb;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001946
Thomas Graf7deb2262007-08-22 13:57:39 -07001947 skb = nlmsg_new(xfrm_aevent_msgsize(), GFP_ATOMIC);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001948 if (skb == NULL)
1949 return -ENOMEM;
1950
1951 if (build_aevent(skb, x, c) < 0)
1952 BUG();
1953
Thomas Graf082a1ad2007-08-22 13:54:36 -07001954 return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_AEVENTS, GFP_ATOMIC);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001955}
1956
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001957static int xfrm_notify_sa_flush(struct km_event *c)
1958{
1959 struct xfrm_usersa_flush *p;
1960 struct nlmsghdr *nlh;
1961 struct sk_buff *skb;
Thomas Graf7deb2262007-08-22 13:57:39 -07001962 int len = NLMSG_ALIGN(sizeof(struct xfrm_usersa_flush));
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001963
Thomas Graf7deb2262007-08-22 13:57:39 -07001964 skb = nlmsg_new(len, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001965 if (skb == NULL)
1966 return -ENOMEM;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001967
Thomas Graf79b8b7f2007-08-22 12:46:53 -07001968 nlh = nlmsg_put(skb, c->pid, c->seq, XFRM_MSG_FLUSHSA, sizeof(*p), 0);
1969 if (nlh == NULL) {
1970 kfree_skb(skb);
1971 return -EMSGSIZE;
1972 }
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001973
Thomas Graf7b67c852007-08-22 13:53:52 -07001974 p = nlmsg_data(nlh);
Herbert Xubf088672005-06-18 22:44:00 -07001975 p->proto = c->data.proto;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001976
Thomas Graf98250692007-08-22 12:47:26 -07001977 nlmsg_end(skb, nlh);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001978
Thomas Graf082a1ad2007-08-22 13:54:36 -07001979 return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_SA, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001980}
1981
Thomas Graf7deb2262007-08-22 13:57:39 -07001982static inline size_t xfrm_sa_len(struct xfrm_state *x)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001983{
Thomas Graf7deb2262007-08-22 13:57:39 -07001984 size_t l = 0;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001985 if (x->aalg)
Thomas Graf7deb2262007-08-22 13:57:39 -07001986 l += nla_total_size(alg_len(x->aalg));
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001987 if (x->ealg)
Thomas Graf7deb2262007-08-22 13:57:39 -07001988 l += nla_total_size(alg_len(x->ealg));
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001989 if (x->calg)
Thomas Graf7deb2262007-08-22 13:57:39 -07001990 l += nla_total_size(sizeof(*x->calg));
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001991 if (x->encap)
Thomas Graf7deb2262007-08-22 13:57:39 -07001992 l += nla_total_size(sizeof(*x->encap));
Herbert Xu68325d32007-10-09 13:30:57 -07001993 if (x->security)
1994 l += nla_total_size(sizeof(struct xfrm_user_sec_ctx) +
1995 x->security->ctx_len);
1996 if (x->coaddr)
1997 l += nla_total_size(sizeof(*x->coaddr));
1998
1999 /* Must count this as this may become non-zero behind our back. */
2000 l += nla_total_size(sizeof(x->lastused));
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002001
2002 return l;
2003}
2004
2005static int xfrm_notify_sa(struct xfrm_state *x, struct km_event *c)
2006{
2007 struct xfrm_usersa_info *p;
Herbert Xu0603eac2005-06-18 22:54:36 -07002008 struct xfrm_usersa_id *id;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002009 struct nlmsghdr *nlh;
2010 struct sk_buff *skb;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002011 int len = xfrm_sa_len(x);
Herbert Xu0603eac2005-06-18 22:54:36 -07002012 int headlen;
2013
2014 headlen = sizeof(*p);
2015 if (c->event == XFRM_MSG_DELSA) {
Thomas Graf7deb2262007-08-22 13:57:39 -07002016 len += nla_total_size(headlen);
Herbert Xu0603eac2005-06-18 22:54:36 -07002017 headlen = sizeof(*id);
2018 }
Thomas Graf7deb2262007-08-22 13:57:39 -07002019 len += NLMSG_ALIGN(headlen);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002020
Thomas Graf7deb2262007-08-22 13:57:39 -07002021 skb = nlmsg_new(len, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002022 if (skb == NULL)
2023 return -ENOMEM;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002024
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002025 nlh = nlmsg_put(skb, c->pid, c->seq, c->event, headlen, 0);
2026 if (nlh == NULL)
Thomas Grafc0144be2007-08-22 13:55:43 -07002027 goto nla_put_failure;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002028
Thomas Graf7b67c852007-08-22 13:53:52 -07002029 p = nlmsg_data(nlh);
Herbert Xu0603eac2005-06-18 22:54:36 -07002030 if (c->event == XFRM_MSG_DELSA) {
Thomas Grafc0144be2007-08-22 13:55:43 -07002031 struct nlattr *attr;
2032
Thomas Graf7b67c852007-08-22 13:53:52 -07002033 id = nlmsg_data(nlh);
Herbert Xu0603eac2005-06-18 22:54:36 -07002034 memcpy(&id->daddr, &x->id.daddr, sizeof(id->daddr));
2035 id->spi = x->id.spi;
2036 id->family = x->props.family;
2037 id->proto = x->id.proto;
2038
Thomas Grafc0144be2007-08-22 13:55:43 -07002039 attr = nla_reserve(skb, XFRMA_SA, sizeof(*p));
2040 if (attr == NULL)
2041 goto nla_put_failure;
2042
2043 p = nla_data(attr);
Herbert Xu0603eac2005-06-18 22:54:36 -07002044 }
2045
Herbert Xu68325d32007-10-09 13:30:57 -07002046 if (copy_to_user_state_extra(x, p, skb))
2047 goto nla_put_failure;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002048
Thomas Graf98250692007-08-22 12:47:26 -07002049 nlmsg_end(skb, nlh);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002050
Thomas Graf082a1ad2007-08-22 13:54:36 -07002051 return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_SA, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002052
Thomas Grafc0144be2007-08-22 13:55:43 -07002053nla_put_failure:
Herbert Xu68325d32007-10-09 13:30:57 -07002054 /* Somebody screwed up with xfrm_sa_len! */
2055 WARN_ON(1);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002056 kfree_skb(skb);
2057 return -1;
2058}
2059
2060static int xfrm_send_state_notify(struct xfrm_state *x, struct km_event *c)
2061{
2062
2063 switch (c->event) {
Herbert Xuf60f6b82005-06-18 22:44:37 -07002064 case XFRM_MSG_EXPIRE:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002065 return xfrm_exp_state_notify(x, c);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002066 case XFRM_MSG_NEWAE:
2067 return xfrm_aevent_state_notify(x, c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07002068 case XFRM_MSG_DELSA:
2069 case XFRM_MSG_UPDSA:
2070 case XFRM_MSG_NEWSA:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002071 return xfrm_notify_sa(x, c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07002072 case XFRM_MSG_FLUSHSA:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002073 return xfrm_notify_sa_flush(c);
2074 default:
2075 printk("xfrm_user: Unknown SA event %d\n", c->event);
2076 break;
2077 }
2078
2079 return 0;
2080
2081}
2082
Thomas Graf7deb2262007-08-22 13:57:39 -07002083static inline size_t xfrm_acquire_msgsize(struct xfrm_state *x,
2084 struct xfrm_policy *xp)
2085{
2086 return NLMSG_ALIGN(sizeof(struct xfrm_user_acquire))
2087 + nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr)
2088 + nla_total_size(xfrm_user_sec_ctx_size(x->security))
2089 + userpolicy_type_attrsize();
2090}
2091
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092static int build_acquire(struct sk_buff *skb, struct xfrm_state *x,
2093 struct xfrm_tmpl *xt, struct xfrm_policy *xp,
2094 int dir)
2095{
2096 struct xfrm_user_acquire *ua;
2097 struct nlmsghdr *nlh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098 __u32 seq = xfrm_get_acqseq();
2099
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002100 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_ACQUIRE, sizeof(*ua), 0);
2101 if (nlh == NULL)
2102 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103
Thomas Graf7b67c852007-08-22 13:53:52 -07002104 ua = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105 memcpy(&ua->id, &x->id, sizeof(ua->id));
2106 memcpy(&ua->saddr, &x->props.saddr, sizeof(ua->saddr));
2107 memcpy(&ua->sel, &x->sel, sizeof(ua->sel));
2108 copy_to_user_policy(xp, &ua->policy, dir);
2109 ua->aalgos = xt->aalgos;
2110 ua->ealgos = xt->ealgos;
2111 ua->calgos = xt->calgos;
2112 ua->seq = x->km.seq = seq;
2113
2114 if (copy_to_user_tmpl(xp, skb) < 0)
2115 goto nlmsg_failure;
Serge Hallyn0d681622006-07-24 23:30:44 -07002116 if (copy_to_user_state_sec_ctx(x, skb))
Trent Jaegerdf718372005-12-13 23:12:27 -08002117 goto nlmsg_failure;
Jamal Hadi Salim1459bb32006-11-20 16:51:22 -08002118 if (copy_to_user_policy_type(xp->type, skb) < 0)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07002119 goto nlmsg_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120
Thomas Graf98250692007-08-22 12:47:26 -07002121 return nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122
2123nlmsg_failure:
Thomas Graf98250692007-08-22 12:47:26 -07002124 nlmsg_cancel(skb, nlh);
2125 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126}
2127
2128static int xfrm_send_acquire(struct xfrm_state *x, struct xfrm_tmpl *xt,
2129 struct xfrm_policy *xp, int dir)
2130{
2131 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132
Thomas Graf7deb2262007-08-22 13:57:39 -07002133 skb = nlmsg_new(xfrm_acquire_msgsize(x, xp), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134 if (skb == NULL)
2135 return -ENOMEM;
2136
2137 if (build_acquire(skb, x, xt, xp, dir) < 0)
2138 BUG();
2139
Thomas Graf082a1ad2007-08-22 13:54:36 -07002140 return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_ACQUIRE, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141}
2142
2143/* User gives us xfrm_user_policy_info followed by an array of 0
2144 * or more templates.
2145 */
Venkat Yekkiralacb969f02006-07-24 23:32:20 -07002146static struct xfrm_policy *xfrm_compile_policy(struct sock *sk, int opt,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147 u8 *data, int len, int *dir)
2148{
2149 struct xfrm_userpolicy_info *p = (struct xfrm_userpolicy_info *)data;
2150 struct xfrm_user_tmpl *ut = (struct xfrm_user_tmpl *) (p + 1);
2151 struct xfrm_policy *xp;
2152 int nr;
2153
Venkat Yekkiralacb969f02006-07-24 23:32:20 -07002154 switch (sk->sk_family) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155 case AF_INET:
2156 if (opt != IP_XFRM_POLICY) {
2157 *dir = -EOPNOTSUPP;
2158 return NULL;
2159 }
2160 break;
2161#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
2162 case AF_INET6:
2163 if (opt != IPV6_XFRM_POLICY) {
2164 *dir = -EOPNOTSUPP;
2165 return NULL;
2166 }
2167 break;
2168#endif
2169 default:
2170 *dir = -EINVAL;
2171 return NULL;
2172 }
2173
2174 *dir = -EINVAL;
2175
2176 if (len < sizeof(*p) ||
2177 verify_newpolicy_info(p))
2178 return NULL;
2179
2180 nr = ((len - sizeof(*p)) / sizeof(*ut));
David S. Millerb4ad86bf2006-12-03 19:19:26 -08002181 if (validate_tmpl(nr, ut, p->sel.family))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182 return NULL;
2183
Herbert Xua4f1bac2005-07-26 15:43:17 -07002184 if (p->dir > XFRM_POLICY_OUT)
2185 return NULL;
2186
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187 xp = xfrm_policy_alloc(GFP_KERNEL);
2188 if (xp == NULL) {
2189 *dir = -ENOBUFS;
2190 return NULL;
2191 }
2192
2193 copy_from_user_policy(xp, p);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07002194 xp->type = XFRM_POLICY_TYPE_MAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195 copy_templates(xp, ut, nr);
2196
2197 *dir = p->dir;
2198
2199 return xp;
2200}
2201
Thomas Graf7deb2262007-08-22 13:57:39 -07002202static inline size_t xfrm_polexpire_msgsize(struct xfrm_policy *xp)
2203{
2204 return NLMSG_ALIGN(sizeof(struct xfrm_user_polexpire))
2205 + nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr)
2206 + nla_total_size(xfrm_user_sec_ctx_size(xp->security))
2207 + userpolicy_type_attrsize();
2208}
2209
Linus Torvalds1da177e2005-04-16 15:20:36 -07002210static int build_polexpire(struct sk_buff *skb, struct xfrm_policy *xp,
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002211 int dir, struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212{
2213 struct xfrm_user_polexpire *upe;
2214 struct nlmsghdr *nlh;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002215 int hard = c->data.hard;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002216
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002217 nlh = nlmsg_put(skb, c->pid, 0, XFRM_MSG_POLEXPIRE, sizeof(*upe), 0);
2218 if (nlh == NULL)
2219 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220
Thomas Graf7b67c852007-08-22 13:53:52 -07002221 upe = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002222 copy_to_user_policy(xp, &upe->pol, dir);
2223 if (copy_to_user_tmpl(xp, skb) < 0)
2224 goto nlmsg_failure;
Trent Jaegerdf718372005-12-13 23:12:27 -08002225 if (copy_to_user_sec_ctx(xp, skb))
2226 goto nlmsg_failure;
Jamal Hadi Salim1459bb32006-11-20 16:51:22 -08002227 if (copy_to_user_policy_type(xp->type, skb) < 0)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07002228 goto nlmsg_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229 upe->hard = !!hard;
2230
Thomas Graf98250692007-08-22 12:47:26 -07002231 return nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002232
2233nlmsg_failure:
Thomas Graf98250692007-08-22 12:47:26 -07002234 nlmsg_cancel(skb, nlh);
2235 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236}
2237
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002238static int xfrm_exp_policy_notify(struct xfrm_policy *xp, int dir, struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002239{
2240 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241
Thomas Graf7deb2262007-08-22 13:57:39 -07002242 skb = nlmsg_new(xfrm_polexpire_msgsize(xp), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243 if (skb == NULL)
2244 return -ENOMEM;
2245
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002246 if (build_polexpire(skb, xp, dir, c) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247 BUG();
2248
Thomas Graf082a1ad2007-08-22 13:54:36 -07002249 return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_EXPIRE, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002250}
2251
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002252static int xfrm_notify_policy(struct xfrm_policy *xp, int dir, struct km_event *c)
2253{
2254 struct xfrm_userpolicy_info *p;
Herbert Xu0603eac2005-06-18 22:54:36 -07002255 struct xfrm_userpolicy_id *id;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002256 struct nlmsghdr *nlh;
2257 struct sk_buff *skb;
Thomas Graf7deb2262007-08-22 13:57:39 -07002258 int len = nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr);
Herbert Xu0603eac2005-06-18 22:54:36 -07002259 int headlen;
2260
2261 headlen = sizeof(*p);
2262 if (c->event == XFRM_MSG_DELPOLICY) {
Thomas Graf7deb2262007-08-22 13:57:39 -07002263 len += nla_total_size(headlen);
Herbert Xu0603eac2005-06-18 22:54:36 -07002264 headlen = sizeof(*id);
2265 }
Thomas Grafcfbfd452007-08-22 13:57:04 -07002266 len += userpolicy_type_attrsize();
Thomas Graf7deb2262007-08-22 13:57:39 -07002267 len += NLMSG_ALIGN(headlen);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002268
Thomas Graf7deb2262007-08-22 13:57:39 -07002269 skb = nlmsg_new(len, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002270 if (skb == NULL)
2271 return -ENOMEM;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002272
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002273 nlh = nlmsg_put(skb, c->pid, c->seq, c->event, headlen, 0);
2274 if (nlh == NULL)
2275 goto nlmsg_failure;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002276
Thomas Graf7b67c852007-08-22 13:53:52 -07002277 p = nlmsg_data(nlh);
Herbert Xu0603eac2005-06-18 22:54:36 -07002278 if (c->event == XFRM_MSG_DELPOLICY) {
Thomas Grafc0144be2007-08-22 13:55:43 -07002279 struct nlattr *attr;
2280
Thomas Graf7b67c852007-08-22 13:53:52 -07002281 id = nlmsg_data(nlh);
Herbert Xu0603eac2005-06-18 22:54:36 -07002282 memset(id, 0, sizeof(*id));
2283 id->dir = dir;
2284 if (c->data.byid)
2285 id->index = xp->index;
2286 else
2287 memcpy(&id->sel, &xp->selector, sizeof(id->sel));
2288
Thomas Grafc0144be2007-08-22 13:55:43 -07002289 attr = nla_reserve(skb, XFRMA_POLICY, sizeof(*p));
2290 if (attr == NULL)
2291 goto nlmsg_failure;
2292
2293 p = nla_data(attr);
Herbert Xu0603eac2005-06-18 22:54:36 -07002294 }
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002295
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002296 copy_to_user_policy(xp, p, dir);
2297 if (copy_to_user_tmpl(xp, skb) < 0)
2298 goto nlmsg_failure;
Jamal Hadi Salim1459bb32006-11-20 16:51:22 -08002299 if (copy_to_user_policy_type(xp->type, skb) < 0)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07002300 goto nlmsg_failure;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002301
Thomas Graf98250692007-08-22 12:47:26 -07002302 nlmsg_end(skb, nlh);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002303
Thomas Graf082a1ad2007-08-22 13:54:36 -07002304 return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_POLICY, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002305
2306nlmsg_failure:
2307 kfree_skb(skb);
2308 return -1;
2309}
2310
2311static int xfrm_notify_policy_flush(struct km_event *c)
2312{
2313 struct nlmsghdr *nlh;
2314 struct sk_buff *skb;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002315
Thomas Graf7deb2262007-08-22 13:57:39 -07002316 skb = nlmsg_new(userpolicy_type_attrsize(), GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002317 if (skb == NULL)
2318 return -ENOMEM;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002319
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002320 nlh = nlmsg_put(skb, c->pid, c->seq, XFRM_MSG_FLUSHPOLICY, 0, 0);
2321 if (nlh == NULL)
2322 goto nlmsg_failure;
Jamal Hadi Salim0c51f532006-11-27 12:58:20 -08002323 if (copy_to_user_policy_type(c->data.type, skb) < 0)
2324 goto nlmsg_failure;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002325
Thomas Graf98250692007-08-22 12:47:26 -07002326 nlmsg_end(skb, nlh);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002327
Thomas Graf082a1ad2007-08-22 13:54:36 -07002328 return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_POLICY, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002329
2330nlmsg_failure:
2331 kfree_skb(skb);
2332 return -1;
2333}
2334
2335static int xfrm_send_policy_notify(struct xfrm_policy *xp, int dir, struct km_event *c)
2336{
2337
2338 switch (c->event) {
Herbert Xuf60f6b82005-06-18 22:44:37 -07002339 case XFRM_MSG_NEWPOLICY:
2340 case XFRM_MSG_UPDPOLICY:
2341 case XFRM_MSG_DELPOLICY:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002342 return xfrm_notify_policy(xp, dir, c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07002343 case XFRM_MSG_FLUSHPOLICY:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002344 return xfrm_notify_policy_flush(c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07002345 case XFRM_MSG_POLEXPIRE:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002346 return xfrm_exp_policy_notify(xp, dir, c);
2347 default:
2348 printk("xfrm_user: Unknown Policy event %d\n", c->event);
2349 }
2350
2351 return 0;
2352
2353}
2354
Thomas Graf7deb2262007-08-22 13:57:39 -07002355static inline size_t xfrm_report_msgsize(void)
2356{
2357 return NLMSG_ALIGN(sizeof(struct xfrm_user_report));
2358}
2359
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002360static int build_report(struct sk_buff *skb, u8 proto,
2361 struct xfrm_selector *sel, xfrm_address_t *addr)
2362{
2363 struct xfrm_user_report *ur;
2364 struct nlmsghdr *nlh;
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002365
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002366 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_REPORT, sizeof(*ur), 0);
2367 if (nlh == NULL)
2368 return -EMSGSIZE;
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002369
Thomas Graf7b67c852007-08-22 13:53:52 -07002370 ur = nlmsg_data(nlh);
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002371 ur->proto = proto;
2372 memcpy(&ur->sel, sel, sizeof(ur->sel));
2373
2374 if (addr)
Thomas Grafc0144be2007-08-22 13:55:43 -07002375 NLA_PUT(skb, XFRMA_COADDR, sizeof(*addr), addr);
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002376
Thomas Graf98250692007-08-22 12:47:26 -07002377 return nlmsg_end(skb, nlh);
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002378
Thomas Grafc0144be2007-08-22 13:55:43 -07002379nla_put_failure:
Thomas Graf98250692007-08-22 12:47:26 -07002380 nlmsg_cancel(skb, nlh);
2381 return -EMSGSIZE;
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002382}
2383
2384static int xfrm_send_report(u8 proto, struct xfrm_selector *sel,
2385 xfrm_address_t *addr)
2386{
2387 struct sk_buff *skb;
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002388
Thomas Graf7deb2262007-08-22 13:57:39 -07002389 skb = nlmsg_new(xfrm_report_msgsize(), GFP_ATOMIC);
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002390 if (skb == NULL)
2391 return -ENOMEM;
2392
2393 if (build_report(skb, proto, sel, addr) < 0)
2394 BUG();
2395
Thomas Graf082a1ad2007-08-22 13:54:36 -07002396 return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_REPORT, GFP_ATOMIC);
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002397}
2398
Linus Torvalds1da177e2005-04-16 15:20:36 -07002399static struct xfrm_mgr netlink_mgr = {
2400 .id = "netlink",
2401 .notify = xfrm_send_state_notify,
2402 .acquire = xfrm_send_acquire,
2403 .compile_policy = xfrm_compile_policy,
2404 .notify_policy = xfrm_send_policy_notify,
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002405 .report = xfrm_send_report,
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002406 .migrate = xfrm_send_migrate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002407};
2408
2409static int __init xfrm_user_init(void)
2410{
Patrick McHardybe336902006-03-20 22:40:54 -08002411 struct sock *nlsk;
2412
Masahide NAKAMURA654b32c2006-08-23 19:12:56 -07002413 printk(KERN_INFO "Initializing XFRM netlink socket\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002414
Eric W. Biedermanb4b51022007-09-12 13:05:38 +02002415 nlsk = netlink_kernel_create(&init_net, NETLINK_XFRM, XFRMNLGRP_MAX,
Patrick McHardyaf65bdf2007-04-20 14:14:21 -07002416 xfrm_netlink_rcv, NULL, THIS_MODULE);
Patrick McHardybe336902006-03-20 22:40:54 -08002417 if (nlsk == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002418 return -ENOMEM;
Patrick McHardybe336902006-03-20 22:40:54 -08002419 rcu_assign_pointer(xfrm_nl, nlsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002420
2421 xfrm_register_km(&netlink_mgr);
2422
2423 return 0;
2424}
2425
2426static void __exit xfrm_user_exit(void)
2427{
Patrick McHardybe336902006-03-20 22:40:54 -08002428 struct sock *nlsk = xfrm_nl;
2429
Linus Torvalds1da177e2005-04-16 15:20:36 -07002430 xfrm_unregister_km(&netlink_mgr);
Patrick McHardybe336902006-03-20 22:40:54 -08002431 rcu_assign_pointer(xfrm_nl, NULL);
2432 synchronize_rcu();
2433 sock_release(nlsk->sk_socket);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002434}
2435
2436module_init(xfrm_user_init);
2437module_exit(xfrm_user_exit);
2438MODULE_LICENSE("GPL");
Harald Welte4fdb3bb2005-08-09 19:40:55 -07002439MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK, NETLINK_XFRM);
Jamal Hadi Salimf8cd5482006-03-20 19:15:11 -08002440