blob: e46bcf02ac69bbd75f934830adfbec8ce14dd97a [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/rtnetlink.h>
23#include <linux/pfkeyv2.h>
24#include <linux/ipsec.h>
25#include <linux/init.h>
26#include <linux/security.h>
27#include <net/sock.h>
28#include <net/xfrm.h>
Thomas Graf88fc2c82005-11-10 02:25:54 +010029#include <net/netlink.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <asm/uaccess.h>
Masahide NAKAMURAe23c7192006-08-23 20:33:28 -070031#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
32#include <linux/in6.h>
33#endif
Joy Latten161a09e2006-11-27 13:11:54 -060034#include <linux/audit.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035
Thomas Grafc26445a2007-08-22 13:56:23 -070036static inline int alg_len(struct xfrm_algo *alg)
37{
38 return sizeof(*alg) + ((alg->alg_key_len + 7) / 8);
39}
40
Thomas Graf5424f322007-08-22 14:01:33 -070041static int verify_one_alg(struct nlattr **attrs, enum xfrm_attr_type_t type)
Linus Torvalds1da177e2005-04-16 15:20:36 -070042{
Thomas Graf5424f322007-08-22 14:01:33 -070043 struct nlattr *rt = attrs[type];
Linus Torvalds1da177e2005-04-16 15:20:36 -070044 struct xfrm_algo *algp;
45
46 if (!rt)
47 return 0;
48
Thomas Graf5424f322007-08-22 14:01:33 -070049 algp = nla_data(rt);
50 if (nla_len(rt) < alg_len(algp))
Herbert Xu31c26852005-05-19 12:39:49 -070051 return -EINVAL;
52
Linus Torvalds1da177e2005-04-16 15:20:36 -070053 switch (type) {
54 case XFRMA_ALG_AUTH:
55 if (!algp->alg_key_len &&
56 strcmp(algp->alg_name, "digest_null") != 0)
57 return -EINVAL;
58 break;
59
60 case XFRMA_ALG_CRYPT:
61 if (!algp->alg_key_len &&
62 strcmp(algp->alg_name, "cipher_null") != 0)
63 return -EINVAL;
64 break;
65
66 case XFRMA_ALG_COMP:
67 /* Zero length keys are legal. */
68 break;
69
70 default:
71 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -070072 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070073
74 algp->alg_name[CRYPTO_MAX_ALG_NAME - 1] = '\0';
75 return 0;
76}
77
Thomas Graf5424f322007-08-22 14:01:33 -070078static void verify_one_addr(struct nlattr **attrs, enum xfrm_attr_type_t type,
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -070079 xfrm_address_t **addrp)
80{
Thomas Graf5424f322007-08-22 14:01:33 -070081 struct nlattr *rt = attrs[type];
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -070082
Thomas Grafcf5cb792007-08-22 13:59:04 -070083 if (rt && addrp)
Thomas Graf5424f322007-08-22 14:01:33 -070084 *addrp = nla_data(rt);
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -070085}
Trent Jaegerdf718372005-12-13 23:12:27 -080086
Thomas Graf5424f322007-08-22 14:01:33 -070087static inline int verify_sec_ctx_len(struct nlattr **attrs)
Trent Jaegerdf718372005-12-13 23:12:27 -080088{
Thomas Graf5424f322007-08-22 14:01:33 -070089 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
Trent Jaegerdf718372005-12-13 23:12:27 -080090 struct xfrm_user_sec_ctx *uctx;
Trent Jaegerdf718372005-12-13 23:12:27 -080091
92 if (!rt)
93 return 0;
94
Thomas Graf5424f322007-08-22 14:01:33 -070095 uctx = nla_data(rt);
Thomas Grafcf5cb792007-08-22 13:59:04 -070096 if (uctx->len != (sizeof(struct xfrm_user_sec_ctx) + uctx->ctx_len))
Trent Jaegerdf718372005-12-13 23:12:27 -080097 return -EINVAL;
98
99 return 0;
100}
101
102
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103static int verify_newsa_info(struct xfrm_usersa_info *p,
Thomas Graf5424f322007-08-22 14:01:33 -0700104 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105{
106 int err;
107
108 err = -EINVAL;
109 switch (p->family) {
110 case AF_INET:
111 break;
112
113 case AF_INET6:
114#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
115 break;
116#else
117 err = -EAFNOSUPPORT;
118 goto out;
119#endif
120
121 default:
122 goto out;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700123 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124
125 err = -EINVAL;
126 switch (p->id.proto) {
127 case IPPROTO_AH:
Thomas Graf35a7aa02007-08-22 14:00:40 -0700128 if (!attrs[XFRMA_ALG_AUTH] ||
129 attrs[XFRMA_ALG_CRYPT] ||
130 attrs[XFRMA_ALG_COMP])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 goto out;
132 break;
133
134 case IPPROTO_ESP:
Thomas Graf35a7aa02007-08-22 14:00:40 -0700135 if ((!attrs[XFRMA_ALG_AUTH] &&
136 !attrs[XFRMA_ALG_CRYPT]) ||
137 attrs[XFRMA_ALG_COMP])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 goto out;
139 break;
140
141 case IPPROTO_COMP:
Thomas Graf35a7aa02007-08-22 14:00:40 -0700142 if (!attrs[XFRMA_ALG_COMP] ||
143 attrs[XFRMA_ALG_AUTH] ||
144 attrs[XFRMA_ALG_CRYPT])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 goto out;
146 break;
147
Masahide NAKAMURAe23c7192006-08-23 20:33:28 -0700148#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
149 case IPPROTO_DSTOPTS:
150 case IPPROTO_ROUTING:
Thomas Graf35a7aa02007-08-22 14:00:40 -0700151 if (attrs[XFRMA_ALG_COMP] ||
152 attrs[XFRMA_ALG_AUTH] ||
153 attrs[XFRMA_ALG_CRYPT] ||
154 attrs[XFRMA_ENCAP] ||
155 attrs[XFRMA_SEC_CTX] ||
156 !attrs[XFRMA_COADDR])
Masahide NAKAMURAe23c7192006-08-23 20:33:28 -0700157 goto out;
158 break;
159#endif
160
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 default:
162 goto out;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700163 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164
Thomas Graf35a7aa02007-08-22 14:00:40 -0700165 if ((err = verify_one_alg(attrs, XFRMA_ALG_AUTH)))
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_CRYPT)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 goto out;
Thomas Graf35a7aa02007-08-22 14:00:40 -0700169 if ((err = verify_one_alg(attrs, XFRMA_ALG_COMP)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 goto out;
Thomas Graf35a7aa02007-08-22 14:00:40 -0700171 if ((err = verify_sec_ctx_len(attrs)))
Trent Jaegerdf718372005-12-13 23:12:27 -0800172 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173
174 err = -EINVAL;
175 switch (p->mode) {
Masahide NAKAMURA7e49e6d2006-09-22 15:05:15 -0700176 case XFRM_MODE_TRANSPORT:
177 case XFRM_MODE_TUNNEL:
Noriaki TAKAMIYA060f02a2006-08-23 18:18:55 -0700178 case XFRM_MODE_ROUTEOPTIMIZATION:
Diego Beltrami0a694522006-10-03 23:47:05 -0700179 case XFRM_MODE_BEET:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 break;
181
182 default:
183 goto out;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700184 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185
186 err = 0;
187
188out:
189 return err;
190}
191
192static int attach_one_algo(struct xfrm_algo **algpp, u8 *props,
193 struct xfrm_algo_desc *(*get_byname)(char *, int),
Thomas Graf5424f322007-08-22 14:01:33 -0700194 struct nlattr *rta)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 struct xfrm_algo *p, *ualg;
197 struct xfrm_algo_desc *algo;
198
199 if (!rta)
200 return 0;
201
Thomas Graf5424f322007-08-22 14:01:33 -0700202 ualg = nla_data(rta);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203
204 algo = get_byname(ualg->alg_name, 1);
205 if (!algo)
206 return -ENOSYS;
207 *props = algo->desc.sadb_alg_id;
208
Thomas Grafc26445a2007-08-22 13:56:23 -0700209 p = kmemdup(ualg, alg_len(ualg), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 if (!p)
211 return -ENOMEM;
212
Herbert Xu04ff1262006-08-13 08:50:00 +1000213 strcpy(p->alg_name, algo->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 *algpp = p;
215 return 0;
216}
217
Thomas Graf5424f322007-08-22 14:01:33 -0700218static int attach_encap_tmpl(struct xfrm_encap_tmpl **encapp, struct nlattr *rta)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 struct xfrm_encap_tmpl *p, *uencap;
221
222 if (!rta)
223 return 0;
224
Thomas Graf5424f322007-08-22 14:01:33 -0700225 uencap = nla_data(rta);
Arnaldo Carvalho de Melocdbc6da2006-11-21 01:22:51 -0200226 p = kmemdup(uencap, sizeof(*p), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 if (!p)
228 return -ENOMEM;
229
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 *encapp = p;
231 return 0;
232}
233
Trent Jaegerdf718372005-12-13 23:12:27 -0800234
Joy Latten661697f2007-04-13 16:14:35 -0700235static inline int xfrm_user_sec_ctx_size(struct xfrm_sec_ctx *xfrm_ctx)
Trent Jaegerdf718372005-12-13 23:12:27 -0800236{
Trent Jaegerdf718372005-12-13 23:12:27 -0800237 int len = 0;
238
239 if (xfrm_ctx) {
240 len += sizeof(struct xfrm_user_sec_ctx);
241 len += xfrm_ctx->ctx_len;
242 }
243 return len;
244}
245
Thomas Graf5424f322007-08-22 14:01:33 -0700246static int attach_sec_ctx(struct xfrm_state *x, struct nlattr *u_arg)
Trent Jaegerdf718372005-12-13 23:12:27 -0800247{
248 struct xfrm_user_sec_ctx *uctx;
249
250 if (!u_arg)
251 return 0;
252
Thomas Graf5424f322007-08-22 14:01:33 -0700253 uctx = nla_data(u_arg);
Trent Jaegerdf718372005-12-13 23:12:27 -0800254 return security_xfrm_state_alloc(x, uctx);
255}
256
Thomas Graf5424f322007-08-22 14:01:33 -0700257static int attach_one_addr(xfrm_address_t **addrpp, struct nlattr *rta)
Noriaki TAKAMIYA060f02a2006-08-23 18:18:55 -0700258{
Noriaki TAKAMIYA060f02a2006-08-23 18:18:55 -0700259 xfrm_address_t *p, *uaddrp;
260
261 if (!rta)
262 return 0;
263
Thomas Graf5424f322007-08-22 14:01:33 -0700264 uaddrp = nla_data(rta);
Arnaldo Carvalho de Melocdbc6da2006-11-21 01:22:51 -0200265 p = kmemdup(uaddrp, sizeof(*p), GFP_KERNEL);
Noriaki TAKAMIYA060f02a2006-08-23 18:18:55 -0700266 if (!p)
267 return -ENOMEM;
268
Noriaki TAKAMIYA060f02a2006-08-23 18:18:55 -0700269 *addrpp = p;
270 return 0;
271}
272
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273static void copy_from_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p)
274{
275 memcpy(&x->id, &p->id, sizeof(x->id));
276 memcpy(&x->sel, &p->sel, sizeof(x->sel));
277 memcpy(&x->lft, &p->lft, sizeof(x->lft));
278 x->props.mode = p->mode;
279 x->props.replay_window = p->replay_window;
280 x->props.reqid = p->reqid;
281 x->props.family = p->family;
David S. Miller54489c142006-10-27 15:29:47 -0700282 memcpy(&x->props.saddr, &p->saddr, sizeof(x->props.saddr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 x->props.flags = p->flags;
Herbert Xu196b0032007-07-31 02:04:32 -0700284
285 /*
286 * Set inner address family if the KM left it as zero.
287 * See comment in validate_tmpl.
288 */
289 if (!x->sel.family)
290 x->sel.family = p->family;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291}
292
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800293/*
294 * someday when pfkey also has support, we could have the code
295 * somehow made shareable and move it to xfrm_state.c - JHS
296 *
297*/
Thomas Graf5424f322007-08-22 14:01:33 -0700298static void xfrm_update_ae_params(struct xfrm_state *x, struct nlattr **attrs)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800299{
Thomas Graf5424f322007-08-22 14:01:33 -0700300 struct nlattr *rp = attrs[XFRMA_REPLAY_VAL];
301 struct nlattr *lt = attrs[XFRMA_LTIME_VAL];
302 struct nlattr *et = attrs[XFRMA_ETIMER_THRESH];
303 struct nlattr *rt = attrs[XFRMA_REPLAY_THRESH];
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800304
305 if (rp) {
306 struct xfrm_replay_state *replay;
Thomas Graf5424f322007-08-22 14:01:33 -0700307 replay = nla_data(rp);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800308 memcpy(&x->replay, replay, sizeof(*replay));
309 memcpy(&x->preplay, replay, sizeof(*replay));
310 }
311
312 if (lt) {
313 struct xfrm_lifetime_cur *ltime;
Thomas Graf5424f322007-08-22 14:01:33 -0700314 ltime = nla_data(lt);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800315 x->curlft.bytes = ltime->bytes;
316 x->curlft.packets = ltime->packets;
317 x->curlft.add_time = ltime->add_time;
318 x->curlft.use_time = ltime->use_time;
319 }
320
Thomas Grafcf5cb792007-08-22 13:59:04 -0700321 if (et)
Thomas Graf5424f322007-08-22 14:01:33 -0700322 x->replay_maxage = nla_get_u32(et);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800323
Thomas Grafcf5cb792007-08-22 13:59:04 -0700324 if (rt)
Thomas Graf5424f322007-08-22 14:01:33 -0700325 x->replay_maxdiff = nla_get_u32(rt);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800326}
327
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328static struct xfrm_state *xfrm_state_construct(struct xfrm_usersa_info *p,
Thomas Graf5424f322007-08-22 14:01:33 -0700329 struct nlattr **attrs,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 int *errp)
331{
332 struct xfrm_state *x = xfrm_state_alloc();
333 int err = -ENOMEM;
334
335 if (!x)
336 goto error_no_put;
337
338 copy_from_user_state(x, p);
339
340 if ((err = attach_one_algo(&x->aalg, &x->props.aalgo,
341 xfrm_aalg_get_byname,
Thomas Graf35a7aa02007-08-22 14:00:40 -0700342 attrs[XFRMA_ALG_AUTH])))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 goto error;
344 if ((err = attach_one_algo(&x->ealg, &x->props.ealgo,
345 xfrm_ealg_get_byname,
Thomas Graf35a7aa02007-08-22 14:00:40 -0700346 attrs[XFRMA_ALG_CRYPT])))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 goto error;
348 if ((err = attach_one_algo(&x->calg, &x->props.calgo,
349 xfrm_calg_get_byname,
Thomas Graf35a7aa02007-08-22 14:00:40 -0700350 attrs[XFRMA_ALG_COMP])))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 goto error;
Thomas Graf35a7aa02007-08-22 14:00:40 -0700352 if ((err = attach_encap_tmpl(&x->encap, attrs[XFRMA_ENCAP])))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 goto error;
Thomas Graf35a7aa02007-08-22 14:00:40 -0700354 if ((err = attach_one_addr(&x->coaddr, attrs[XFRMA_COADDR])))
Noriaki TAKAMIYA060f02a2006-08-23 18:18:55 -0700355 goto error;
Herbert Xu72cb6962005-06-20 13:18:08 -0700356 err = xfrm_init_state(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 if (err)
358 goto error;
359
Thomas Graf35a7aa02007-08-22 14:00:40 -0700360 if ((err = attach_sec_ctx(x, attrs[XFRMA_SEC_CTX])))
Trent Jaegerdf718372005-12-13 23:12:27 -0800361 goto error;
362
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 x->km.seq = p->seq;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800364 x->replay_maxdiff = sysctl_xfrm_aevent_rseqth;
365 /* sysctl_xfrm_aevent_etime is in 100ms units */
366 x->replay_maxage = (sysctl_xfrm_aevent_etime*HZ)/XFRM_AE_ETH_M;
367 x->preplay.bitmap = 0;
368 x->preplay.seq = x->replay.seq+x->replay_maxdiff;
369 x->preplay.oseq = x->replay.oseq +x->replay_maxdiff;
370
371 /* override default values from above */
372
Thomas Graf5424f322007-08-22 14:01:33 -0700373 xfrm_update_ae_params(x, attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374
375 return x;
376
377error:
378 x->km.state = XFRM_STATE_DEAD;
379 xfrm_state_put(x);
380error_no_put:
381 *errp = err;
382 return NULL;
383}
384
Christoph Hellwig22e70052007-01-02 15:22:30 -0800385static int xfrm_add_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -0700386 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387{
Thomas Graf7b67c852007-08-22 13:53:52 -0700388 struct xfrm_usersa_info *p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 struct xfrm_state *x;
390 int err;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700391 struct km_event c;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392
Thomas Graf35a7aa02007-08-22 14:00:40 -0700393 err = verify_newsa_info(p, attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 if (err)
395 return err;
396
Thomas Graf35a7aa02007-08-22 14:00:40 -0700397 x = xfrm_state_construct(p, attrs, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 if (!x)
399 return err;
400
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700401 xfrm_state_hold(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 if (nlh->nlmsg_type == XFRM_MSG_NEWSA)
403 err = xfrm_state_add(x);
404 else
405 err = xfrm_state_update(x);
406
Joy Latten161a09e2006-11-27 13:11:54 -0600407 xfrm_audit_log(NETLINK_CB(skb).loginuid, NETLINK_CB(skb).sid,
408 AUDIT_MAC_IPSEC_ADDSA, err ? 0 : 1, NULL, x);
409
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 if (err < 0) {
411 x->km.state = XFRM_STATE_DEAD;
Herbert Xu21380b82006-02-22 14:47:13 -0800412 __xfrm_state_put(x);
Patrick McHardy7d6dfe12005-06-18 22:45:31 -0700413 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 }
415
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700416 c.seq = nlh->nlmsg_seq;
417 c.pid = nlh->nlmsg_pid;
Herbert Xuf60f6b82005-06-18 22:44:37 -0700418 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700419
420 km_state_notify(x, &c);
Patrick McHardy7d6dfe12005-06-18 22:45:31 -0700421out:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700422 xfrm_state_put(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 return err;
424}
425
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700426static struct xfrm_state *xfrm_user_state_lookup(struct xfrm_usersa_id *p,
Thomas Graf5424f322007-08-22 14:01:33 -0700427 struct nlattr **attrs,
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700428 int *errp)
429{
430 struct xfrm_state *x = NULL;
431 int err;
432
433 if (xfrm_id_proto_match(p->proto, IPSEC_PROTO_ANY)) {
434 err = -ESRCH;
435 x = xfrm_state_lookup(&p->daddr, p->spi, p->proto, p->family);
436 } else {
437 xfrm_address_t *saddr = NULL;
438
Thomas Graf35a7aa02007-08-22 14:00:40 -0700439 verify_one_addr(attrs, XFRMA_SRCADDR, &saddr);
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700440 if (!saddr) {
441 err = -EINVAL;
442 goto out;
443 }
444
Masahide NAKAMURA9abbffe2006-11-24 20:34:51 -0800445 err = -ESRCH;
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700446 x = xfrm_state_lookup_byaddr(&p->daddr, saddr, p->proto,
447 p->family);
448 }
449
450 out:
451 if (!x && errp)
452 *errp = err;
453 return x;
454}
455
Christoph Hellwig22e70052007-01-02 15:22:30 -0800456static int xfrm_del_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -0700457 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458{
459 struct xfrm_state *x;
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700460 int err = -ESRCH;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700461 struct km_event c;
Thomas Graf7b67c852007-08-22 13:53:52 -0700462 struct xfrm_usersa_id *p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463
Thomas Graf35a7aa02007-08-22 14:00:40 -0700464 x = xfrm_user_state_lookup(p, attrs, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 if (x == NULL)
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700466 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467
David S. Miller6f68dc32006-06-08 23:58:52 -0700468 if ((err = security_xfrm_state_delete(x)) != 0)
Catherine Zhangc8c05a82006-06-08 23:39:49 -0700469 goto out;
470
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 if (xfrm_state_kern(x)) {
Catherine Zhangc8c05a82006-06-08 23:39:49 -0700472 err = -EPERM;
473 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 }
475
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700476 err = xfrm_state_delete(x);
Joy Latten161a09e2006-11-27 13:11:54 -0600477
Catherine Zhangc8c05a82006-06-08 23:39:49 -0700478 if (err < 0)
479 goto out;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700480
481 c.seq = nlh->nlmsg_seq;
482 c.pid = nlh->nlmsg_pid;
Herbert Xuf60f6b82005-06-18 22:44:37 -0700483 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700484 km_state_notify(x, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485
Catherine Zhangc8c05a82006-06-08 23:39:49 -0700486out:
Eric Paris16bec312007-03-07 16:02:16 -0800487 xfrm_audit_log(NETLINK_CB(skb).loginuid, NETLINK_CB(skb).sid,
488 AUDIT_MAC_IPSEC_DELSA, err ? 0 : 1, NULL, x);
Catherine Zhangc8c05a82006-06-08 23:39:49 -0700489 xfrm_state_put(x);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700490 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491}
492
493static void copy_to_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p)
494{
495 memcpy(&p->id, &x->id, sizeof(p->id));
496 memcpy(&p->sel, &x->sel, sizeof(p->sel));
497 memcpy(&p->lft, &x->lft, sizeof(p->lft));
498 memcpy(&p->curlft, &x->curlft, sizeof(p->curlft));
499 memcpy(&p->stats, &x->stats, sizeof(p->stats));
David S. Miller54489c142006-10-27 15:29:47 -0700500 memcpy(&p->saddr, &x->props.saddr, sizeof(p->saddr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 p->mode = x->props.mode;
502 p->replay_window = x->props.replay_window;
503 p->reqid = x->props.reqid;
504 p->family = x->props.family;
505 p->flags = x->props.flags;
506 p->seq = x->km.seq;
507}
508
509struct xfrm_dump_info {
510 struct sk_buff *in_skb;
511 struct sk_buff *out_skb;
512 u32 nlmsg_seq;
513 u16 nlmsg_flags;
514 int start_idx;
515 int this_idx;
516};
517
Thomas Grafc0144be2007-08-22 13:55:43 -0700518static int copy_sec_ctx(struct xfrm_sec_ctx *s, struct sk_buff *skb)
519{
520 int ctx_size = sizeof(struct xfrm_sec_ctx) + s->ctx_len;
521 struct xfrm_user_sec_ctx *uctx;
522 struct nlattr *attr;
523
524 attr = nla_reserve(skb, XFRMA_SEC_CTX, ctx_size);
525 if (attr == NULL)
526 return -EMSGSIZE;
527
528 uctx = nla_data(attr);
529 uctx->exttype = XFRMA_SEC_CTX;
530 uctx->len = ctx_size;
531 uctx->ctx_doi = s->ctx_doi;
532 uctx->ctx_alg = s->ctx_alg;
533 uctx->ctx_len = s->ctx_len;
534 memcpy(uctx + 1, s->ctx_str, s->ctx_len);
535
536 return 0;
537}
538
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539static int dump_one_state(struct xfrm_state *x, int count, void *ptr)
540{
541 struct xfrm_dump_info *sp = ptr;
542 struct sk_buff *in_skb = sp->in_skb;
543 struct sk_buff *skb = sp->out_skb;
544 struct xfrm_usersa_info *p;
545 struct nlmsghdr *nlh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546
547 if (sp->this_idx < sp->start_idx)
548 goto out;
549
Thomas Graf79b8b7f2007-08-22 12:46:53 -0700550 nlh = nlmsg_put(skb, NETLINK_CB(in_skb).pid, sp->nlmsg_seq,
551 XFRM_MSG_NEWSA, sizeof(*p), sp->nlmsg_flags);
552 if (nlh == NULL)
553 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554
Thomas Graf7b67c852007-08-22 13:53:52 -0700555 p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 copy_to_user_state(x, p);
557
558 if (x->aalg)
Thomas Grafc26445a2007-08-22 13:56:23 -0700559 NLA_PUT(skb, XFRMA_ALG_AUTH, alg_len(x->aalg), x->aalg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 if (x->ealg)
Thomas Grafc26445a2007-08-22 13:56:23 -0700561 NLA_PUT(skb, XFRMA_ALG_CRYPT, alg_len(x->ealg), x->ealg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 if (x->calg)
Thomas Grafc0144be2007-08-22 13:55:43 -0700563 NLA_PUT(skb, XFRMA_ALG_COMP, sizeof(*(x->calg)), x->calg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564
565 if (x->encap)
Thomas Grafc0144be2007-08-22 13:55:43 -0700566 NLA_PUT(skb, XFRMA_ENCAP, sizeof(*x->encap), x->encap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567
Thomas Grafc0144be2007-08-22 13:55:43 -0700568 if (x->security && copy_sec_ctx(x->security, skb) < 0)
569 goto nla_put_failure;
Noriaki TAKAMIYA060f02a2006-08-23 18:18:55 -0700570
571 if (x->coaddr)
Thomas Grafc0144be2007-08-22 13:55:43 -0700572 NLA_PUT(skb, XFRMA_COADDR, sizeof(*x->coaddr), x->coaddr);
Noriaki TAKAMIYA060f02a2006-08-23 18:18:55 -0700573
Masahide NAKAMURA9afaca02006-08-23 18:20:16 -0700574 if (x->lastused)
Thomas Grafc0144be2007-08-22 13:55:43 -0700575 NLA_PUT_U64(skb, XFRMA_LASTUSED, x->lastused);
Masahide NAKAMURA9afaca02006-08-23 18:20:16 -0700576
Thomas Graf98250692007-08-22 12:47:26 -0700577 nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578out:
579 sp->this_idx++;
580 return 0;
581
Thomas Grafc0144be2007-08-22 13:55:43 -0700582nla_put_failure:
Thomas Graf98250692007-08-22 12:47:26 -0700583 nlmsg_cancel(skb, nlh);
584 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585}
586
587static int xfrm_dump_sa(struct sk_buff *skb, struct netlink_callback *cb)
588{
589 struct xfrm_dump_info info;
590
591 info.in_skb = cb->skb;
592 info.out_skb = skb;
593 info.nlmsg_seq = cb->nlh->nlmsg_seq;
594 info.nlmsg_flags = NLM_F_MULTI;
595 info.this_idx = 0;
596 info.start_idx = cb->args[0];
Masahide NAKAMURAdc00a522006-08-23 17:49:52 -0700597 (void) xfrm_state_walk(0, dump_one_state, &info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 cb->args[0] = info.this_idx;
599
600 return skb->len;
601}
602
603static struct sk_buff *xfrm_state_netlink(struct sk_buff *in_skb,
604 struct xfrm_state *x, u32 seq)
605{
606 struct xfrm_dump_info info;
607 struct sk_buff *skb;
608
Thomas Graf7deb2262007-08-22 13:57:39 -0700609 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 if (!skb)
611 return ERR_PTR(-ENOMEM);
612
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 info.in_skb = in_skb;
614 info.out_skb = skb;
615 info.nlmsg_seq = seq;
616 info.nlmsg_flags = 0;
617 info.this_idx = info.start_idx = 0;
618
619 if (dump_one_state(x, 0, &info)) {
620 kfree_skb(skb);
621 return NULL;
622 }
623
624 return skb;
625}
626
Thomas Graf7deb2262007-08-22 13:57:39 -0700627static inline size_t xfrm_spdinfo_msgsize(void)
628{
629 return NLMSG_ALIGN(4)
630 + nla_total_size(sizeof(struct xfrmu_spdinfo))
631 + nla_total_size(sizeof(struct xfrmu_spdhinfo));
632}
633
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700634static int build_spdinfo(struct sk_buff *skb, u32 pid, u32 seq, u32 flags)
635{
Jamal Hadi Salim5a6d3412007-05-04 12:55:39 -0700636 struct xfrmk_spdinfo si;
637 struct xfrmu_spdinfo spc;
638 struct xfrmu_spdhinfo sph;
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700639 struct nlmsghdr *nlh;
640 u32 *f;
641
642 nlh = nlmsg_put(skb, pid, seq, XFRM_MSG_NEWSPDINFO, sizeof(u32), 0);
643 if (nlh == NULL) /* shouldnt really happen ... */
644 return -EMSGSIZE;
645
646 f = nlmsg_data(nlh);
647 *f = flags;
648 xfrm_spd_getinfo(&si);
Jamal Hadi Salim5a6d3412007-05-04 12:55:39 -0700649 spc.incnt = si.incnt;
650 spc.outcnt = si.outcnt;
651 spc.fwdcnt = si.fwdcnt;
652 spc.inscnt = si.inscnt;
653 spc.outscnt = si.outscnt;
654 spc.fwdscnt = si.fwdscnt;
655 sph.spdhcnt = si.spdhcnt;
656 sph.spdhmcnt = si.spdhmcnt;
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700657
Jamal Hadi Salim5a6d3412007-05-04 12:55:39 -0700658 NLA_PUT(skb, XFRMA_SPD_INFO, sizeof(spc), &spc);
659 NLA_PUT(skb, XFRMA_SPD_HINFO, sizeof(sph), &sph);
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700660
661 return nlmsg_end(skb, nlh);
662
663nla_put_failure:
664 nlmsg_cancel(skb, nlh);
665 return -EMSGSIZE;
666}
667
668static int xfrm_get_spdinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -0700669 struct nlattr **attrs)
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700670{
671 struct sk_buff *r_skb;
Thomas Graf7b67c852007-08-22 13:53:52 -0700672 u32 *flags = nlmsg_data(nlh);
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700673 u32 spid = NETLINK_CB(skb).pid;
674 u32 seq = nlh->nlmsg_seq;
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700675
Thomas Graf7deb2262007-08-22 13:57:39 -0700676 r_skb = nlmsg_new(xfrm_spdinfo_msgsize(), GFP_ATOMIC);
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700677 if (r_skb == NULL)
678 return -ENOMEM;
679
680 if (build_spdinfo(r_skb, spid, seq, *flags) < 0)
681 BUG();
682
683 return nlmsg_unicast(xfrm_nl, r_skb, spid);
684}
685
Thomas Graf7deb2262007-08-22 13:57:39 -0700686static inline size_t xfrm_sadinfo_msgsize(void)
687{
688 return NLMSG_ALIGN(4)
689 + nla_total_size(sizeof(struct xfrmu_sadhinfo))
690 + nla_total_size(4); /* XFRMA_SAD_CNT */
691}
692
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700693static int build_sadinfo(struct sk_buff *skb, u32 pid, u32 seq, u32 flags)
694{
Jamal Hadi Salimaf11e312007-05-04 12:55:13 -0700695 struct xfrmk_sadinfo si;
696 struct xfrmu_sadhinfo sh;
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700697 struct nlmsghdr *nlh;
698 u32 *f;
699
700 nlh = nlmsg_put(skb, pid, seq, XFRM_MSG_NEWSADINFO, sizeof(u32), 0);
701 if (nlh == NULL) /* shouldnt really happen ... */
702 return -EMSGSIZE;
703
704 f = nlmsg_data(nlh);
705 *f = flags;
706 xfrm_sad_getinfo(&si);
707
Jamal Hadi Salimaf11e312007-05-04 12:55:13 -0700708 sh.sadhmcnt = si.sadhmcnt;
709 sh.sadhcnt = si.sadhcnt;
710
711 NLA_PUT_U32(skb, XFRMA_SAD_CNT, si.sadcnt);
712 NLA_PUT(skb, XFRMA_SAD_HINFO, sizeof(sh), &sh);
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700713
714 return nlmsg_end(skb, nlh);
715
716nla_put_failure:
717 nlmsg_cancel(skb, nlh);
718 return -EMSGSIZE;
719}
720
721static int xfrm_get_sadinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -0700722 struct nlattr **attrs)
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700723{
724 struct sk_buff *r_skb;
Thomas Graf7b67c852007-08-22 13:53:52 -0700725 u32 *flags = nlmsg_data(nlh);
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700726 u32 spid = NETLINK_CB(skb).pid;
727 u32 seq = nlh->nlmsg_seq;
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700728
Thomas Graf7deb2262007-08-22 13:57:39 -0700729 r_skb = nlmsg_new(xfrm_sadinfo_msgsize(), GFP_ATOMIC);
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700730 if (r_skb == NULL)
731 return -ENOMEM;
732
733 if (build_sadinfo(r_skb, spid, seq, *flags) < 0)
734 BUG();
735
736 return nlmsg_unicast(xfrm_nl, r_skb, spid);
737}
738
Christoph Hellwig22e70052007-01-02 15:22:30 -0800739static int xfrm_get_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -0700740 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741{
Thomas Graf7b67c852007-08-22 13:53:52 -0700742 struct xfrm_usersa_id *p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 struct xfrm_state *x;
744 struct sk_buff *resp_skb;
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700745 int err = -ESRCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746
Thomas Graf35a7aa02007-08-22 14:00:40 -0700747 x = xfrm_user_state_lookup(p, attrs, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 if (x == NULL)
749 goto out_noput;
750
751 resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
752 if (IS_ERR(resp_skb)) {
753 err = PTR_ERR(resp_skb);
754 } else {
Thomas Graf082a1ad2007-08-22 13:54:36 -0700755 err = nlmsg_unicast(xfrm_nl, resp_skb, NETLINK_CB(skb).pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 }
757 xfrm_state_put(x);
758out_noput:
759 return err;
760}
761
762static int verify_userspi_info(struct xfrm_userspi_info *p)
763{
764 switch (p->info.id.proto) {
765 case IPPROTO_AH:
766 case IPPROTO_ESP:
767 break;
768
769 case IPPROTO_COMP:
770 /* IPCOMP spi is 16-bits. */
771 if (p->max >= 0x10000)
772 return -EINVAL;
773 break;
774
775 default:
776 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700777 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778
779 if (p->min > p->max)
780 return -EINVAL;
781
782 return 0;
783}
784
Christoph Hellwig22e70052007-01-02 15:22:30 -0800785static int xfrm_alloc_userspi(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -0700786 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787{
788 struct xfrm_state *x;
789 struct xfrm_userspi_info *p;
790 struct sk_buff *resp_skb;
791 xfrm_address_t *daddr;
792 int family;
793 int err;
794
Thomas Graf7b67c852007-08-22 13:53:52 -0700795 p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 err = verify_userspi_info(p);
797 if (err)
798 goto out_noput;
799
800 family = p->info.family;
801 daddr = &p->info.id.daddr;
802
803 x = NULL;
804 if (p->info.seq) {
805 x = xfrm_find_acq_byseq(p->info.seq);
806 if (x && xfrm_addr_cmp(&x->id.daddr, daddr, family)) {
807 xfrm_state_put(x);
808 x = NULL;
809 }
810 }
811
812 if (!x)
813 x = xfrm_find_acq(p->info.mode, p->info.reqid,
814 p->info.id.proto, daddr,
815 &p->info.saddr, 1,
816 family);
817 err = -ENOENT;
818 if (x == NULL)
819 goto out_noput;
820
821 resp_skb = ERR_PTR(-ENOENT);
822
823 spin_lock_bh(&x->lock);
824 if (x->km.state != XFRM_STATE_DEAD) {
825 xfrm_alloc_spi(x, htonl(p->min), htonl(p->max));
826 if (x->id.spi)
827 resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
828 }
829 spin_unlock_bh(&x->lock);
830
831 if (IS_ERR(resp_skb)) {
832 err = PTR_ERR(resp_skb);
833 goto out;
834 }
835
Thomas Graf082a1ad2007-08-22 13:54:36 -0700836 err = nlmsg_unicast(xfrm_nl, resp_skb, NETLINK_CB(skb).pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837
838out:
839 xfrm_state_put(x);
840out_noput:
841 return err;
842}
843
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -0800844static int verify_policy_dir(u8 dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845{
846 switch (dir) {
847 case XFRM_POLICY_IN:
848 case XFRM_POLICY_OUT:
849 case XFRM_POLICY_FWD:
850 break;
851
852 default:
853 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700854 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855
856 return 0;
857}
858
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -0800859static int verify_policy_type(u8 type)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -0700860{
861 switch (type) {
862 case XFRM_POLICY_TYPE_MAIN:
863#ifdef CONFIG_XFRM_SUB_POLICY
864 case XFRM_POLICY_TYPE_SUB:
865#endif
866 break;
867
868 default:
869 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700870 }
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -0700871
872 return 0;
873}
874
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875static int verify_newpolicy_info(struct xfrm_userpolicy_info *p)
876{
877 switch (p->share) {
878 case XFRM_SHARE_ANY:
879 case XFRM_SHARE_SESSION:
880 case XFRM_SHARE_USER:
881 case XFRM_SHARE_UNIQUE:
882 break;
883
884 default:
885 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700886 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887
888 switch (p->action) {
889 case XFRM_POLICY_ALLOW:
890 case XFRM_POLICY_BLOCK:
891 break;
892
893 default:
894 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700895 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896
897 switch (p->sel.family) {
898 case AF_INET:
899 break;
900
901 case AF_INET6:
902#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
903 break;
904#else
905 return -EAFNOSUPPORT;
906#endif
907
908 default:
909 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700910 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911
912 return verify_policy_dir(p->dir);
913}
914
Thomas Graf5424f322007-08-22 14:01:33 -0700915static int copy_from_user_sec_ctx(struct xfrm_policy *pol, struct nlattr **attrs)
Trent Jaegerdf718372005-12-13 23:12:27 -0800916{
Thomas Graf5424f322007-08-22 14:01:33 -0700917 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
Trent Jaegerdf718372005-12-13 23:12:27 -0800918 struct xfrm_user_sec_ctx *uctx;
919
920 if (!rt)
921 return 0;
922
Thomas Graf5424f322007-08-22 14:01:33 -0700923 uctx = nla_data(rt);
Trent Jaegerdf718372005-12-13 23:12:27 -0800924 return security_xfrm_policy_alloc(pol, uctx);
925}
926
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927static void copy_templates(struct xfrm_policy *xp, struct xfrm_user_tmpl *ut,
928 int nr)
929{
930 int i;
931
932 xp->xfrm_nr = nr;
933 for (i = 0; i < nr; i++, ut++) {
934 struct xfrm_tmpl *t = &xp->xfrm_vec[i];
935
936 memcpy(&t->id, &ut->id, sizeof(struct xfrm_id));
937 memcpy(&t->saddr, &ut->saddr,
938 sizeof(xfrm_address_t));
939 t->reqid = ut->reqid;
940 t->mode = ut->mode;
941 t->share = ut->share;
942 t->optional = ut->optional;
943 t->aalgos = ut->aalgos;
944 t->ealgos = ut->ealgos;
945 t->calgos = ut->calgos;
Miika Komu8511d012006-11-30 16:40:51 -0800946 t->encap_family = ut->family;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 }
948}
949
David S. Millerb4ad86bf2006-12-03 19:19:26 -0800950static int validate_tmpl(int nr, struct xfrm_user_tmpl *ut, u16 family)
951{
952 int i;
953
954 if (nr > XFRM_MAX_DEPTH)
955 return -EINVAL;
956
957 for (i = 0; i < nr; i++) {
958 /* We never validated the ut->family value, so many
959 * applications simply leave it at zero. The check was
960 * never made and ut->family was ignored because all
961 * templates could be assumed to have the same family as
962 * the policy itself. Now that we will have ipv4-in-ipv6
963 * and ipv6-in-ipv4 tunnels, this is no longer true.
964 */
965 if (!ut[i].family)
966 ut[i].family = family;
967
968 switch (ut[i].family) {
969 case AF_INET:
970 break;
971#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
972 case AF_INET6:
973 break;
974#endif
975 default:
976 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700977 }
David S. Millerb4ad86bf2006-12-03 19:19:26 -0800978 }
979
980 return 0;
981}
982
Thomas Graf5424f322007-08-22 14:01:33 -0700983static int copy_from_user_tmpl(struct xfrm_policy *pol, struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984{
Thomas Graf5424f322007-08-22 14:01:33 -0700985 struct nlattr *rt = attrs[XFRMA_TMPL];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986
987 if (!rt) {
988 pol->xfrm_nr = 0;
989 } else {
Thomas Graf5424f322007-08-22 14:01:33 -0700990 struct xfrm_user_tmpl *utmpl = nla_data(rt);
991 int nr = nla_len(rt) / sizeof(*utmpl);
David S. Millerb4ad86bf2006-12-03 19:19:26 -0800992 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993
David S. Millerb4ad86bf2006-12-03 19:19:26 -0800994 err = validate_tmpl(nr, utmpl, pol->family);
995 if (err)
996 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997
Thomas Graf5424f322007-08-22 14:01:33 -0700998 copy_templates(pol, utmpl, nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 }
1000 return 0;
1001}
1002
Thomas Graf5424f322007-08-22 14:01:33 -07001003static int copy_from_user_policy_type(u8 *tp, struct nlattr **attrs)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001004{
Thomas Graf5424f322007-08-22 14:01:33 -07001005 struct nlattr *rt = attrs[XFRMA_POLICY_TYPE];
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001006 struct xfrm_userpolicy_type *upt;
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001007 u8 type = XFRM_POLICY_TYPE_MAIN;
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001008 int err;
1009
1010 if (rt) {
Thomas Graf5424f322007-08-22 14:01:33 -07001011 upt = nla_data(rt);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001012 type = upt->type;
1013 }
1014
1015 err = verify_policy_type(type);
1016 if (err)
1017 return err;
1018
1019 *tp = type;
1020 return 0;
1021}
1022
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023static void copy_from_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p)
1024{
1025 xp->priority = p->priority;
1026 xp->index = p->index;
1027 memcpy(&xp->selector, &p->sel, sizeof(xp->selector));
1028 memcpy(&xp->lft, &p->lft, sizeof(xp->lft));
1029 xp->action = p->action;
1030 xp->flags = p->flags;
1031 xp->family = p->sel.family;
1032 /* XXX xp->share = p->share; */
1033}
1034
1035static void copy_to_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p, int dir)
1036{
1037 memcpy(&p->sel, &xp->selector, sizeof(p->sel));
1038 memcpy(&p->lft, &xp->lft, sizeof(p->lft));
1039 memcpy(&p->curlft, &xp->curlft, sizeof(p->curlft));
1040 p->priority = xp->priority;
1041 p->index = xp->index;
1042 p->sel.family = xp->family;
1043 p->dir = dir;
1044 p->action = xp->action;
1045 p->flags = xp->flags;
1046 p->share = XFRM_SHARE_ANY; /* XXX xp->share */
1047}
1048
Thomas Graf5424f322007-08-22 14:01:33 -07001049static struct xfrm_policy *xfrm_policy_construct(struct xfrm_userpolicy_info *p, struct nlattr **attrs, int *errp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050{
1051 struct xfrm_policy *xp = xfrm_policy_alloc(GFP_KERNEL);
1052 int err;
1053
1054 if (!xp) {
1055 *errp = -ENOMEM;
1056 return NULL;
1057 }
1058
1059 copy_from_user_policy(xp, p);
Trent Jaegerdf718372005-12-13 23:12:27 -08001060
Thomas Graf35a7aa02007-08-22 14:00:40 -07001061 err = copy_from_user_policy_type(&xp->type, attrs);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001062 if (err)
1063 goto error;
1064
Thomas Graf35a7aa02007-08-22 14:00:40 -07001065 if (!(err = copy_from_user_tmpl(xp, attrs)))
1066 err = copy_from_user_sec_ctx(xp, attrs);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001067 if (err)
1068 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069
1070 return xp;
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001071 error:
1072 *errp = err;
1073 kfree(xp);
1074 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075}
1076
Christoph Hellwig22e70052007-01-02 15:22:30 -08001077static int xfrm_add_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001078 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079{
Thomas Graf7b67c852007-08-22 13:53:52 -07001080 struct xfrm_userpolicy_info *p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 struct xfrm_policy *xp;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001082 struct km_event c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 int err;
1084 int excl;
1085
1086 err = verify_newpolicy_info(p);
1087 if (err)
1088 return err;
Thomas Graf35a7aa02007-08-22 14:00:40 -07001089 err = verify_sec_ctx_len(attrs);
Trent Jaegerdf718372005-12-13 23:12:27 -08001090 if (err)
1091 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092
Thomas Graf35a7aa02007-08-22 14:00:40 -07001093 xp = xfrm_policy_construct(p, attrs, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 if (!xp)
1095 return err;
1096
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001097 /* shouldnt excl be based on nlh flags??
1098 * Aha! this is anti-netlink really i.e more pfkey derived
1099 * in netlink excl is a flag and you wouldnt need
1100 * a type XFRM_MSG_UPDPOLICY - JHS */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 excl = nlh->nlmsg_type == XFRM_MSG_NEWPOLICY;
1102 err = xfrm_policy_insert(p->dir, xp, excl);
Joy Latten161a09e2006-11-27 13:11:54 -06001103 xfrm_audit_log(NETLINK_CB(skb).loginuid, NETLINK_CB(skb).sid,
1104 AUDIT_MAC_IPSEC_DELSPD, err ? 0 : 1, xp, NULL);
1105
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 if (err) {
Trent Jaeger5f8ac642006-01-06 13:22:39 -08001107 security_xfrm_policy_free(xp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 kfree(xp);
1109 return err;
1110 }
1111
Herbert Xuf60f6b82005-06-18 22:44:37 -07001112 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001113 c.seq = nlh->nlmsg_seq;
1114 c.pid = nlh->nlmsg_pid;
1115 km_policy_notify(xp, p->dir, &c);
1116
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 xfrm_pol_put(xp);
1118
1119 return 0;
1120}
1121
1122static int copy_to_user_tmpl(struct xfrm_policy *xp, struct sk_buff *skb)
1123{
1124 struct xfrm_user_tmpl vec[XFRM_MAX_DEPTH];
1125 int i;
1126
1127 if (xp->xfrm_nr == 0)
1128 return 0;
1129
1130 for (i = 0; i < xp->xfrm_nr; i++) {
1131 struct xfrm_user_tmpl *up = &vec[i];
1132 struct xfrm_tmpl *kp = &xp->xfrm_vec[i];
1133
1134 memcpy(&up->id, &kp->id, sizeof(up->id));
Miika Komu8511d012006-11-30 16:40:51 -08001135 up->family = kp->encap_family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136 memcpy(&up->saddr, &kp->saddr, sizeof(up->saddr));
1137 up->reqid = kp->reqid;
1138 up->mode = kp->mode;
1139 up->share = kp->share;
1140 up->optional = kp->optional;
1141 up->aalgos = kp->aalgos;
1142 up->ealgos = kp->ealgos;
1143 up->calgos = kp->calgos;
1144 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145
Thomas Grafc0144be2007-08-22 13:55:43 -07001146 return nla_put(skb, XFRMA_TMPL,
1147 sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr, vec);
Trent Jaegerdf718372005-12-13 23:12:27 -08001148}
1149
Serge Hallyn0d681622006-07-24 23:30:44 -07001150static inline int copy_to_user_state_sec_ctx(struct xfrm_state *x, struct sk_buff *skb)
1151{
1152 if (x->security) {
1153 return copy_sec_ctx(x->security, skb);
1154 }
1155 return 0;
1156}
1157
1158static inline int copy_to_user_sec_ctx(struct xfrm_policy *xp, struct sk_buff *skb)
1159{
1160 if (xp->security) {
1161 return copy_sec_ctx(xp->security, skb);
1162 }
1163 return 0;
1164}
Thomas Grafcfbfd452007-08-22 13:57:04 -07001165static inline size_t userpolicy_type_attrsize(void)
1166{
1167#ifdef CONFIG_XFRM_SUB_POLICY
1168 return nla_total_size(sizeof(struct xfrm_userpolicy_type));
1169#else
1170 return 0;
1171#endif
1172}
Serge Hallyn0d681622006-07-24 23:30:44 -07001173
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001174#ifdef CONFIG_XFRM_SUB_POLICY
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001175static int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001176{
Thomas Grafc0144be2007-08-22 13:55:43 -07001177 struct xfrm_userpolicy_type upt = {
1178 .type = type,
1179 };
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001180
Thomas Grafc0144be2007-08-22 13:55:43 -07001181 return nla_put(skb, XFRMA_POLICY_TYPE, sizeof(upt), &upt);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001182}
1183
1184#else
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001185static inline int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001186{
1187 return 0;
1188}
1189#endif
1190
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191static int dump_one_policy(struct xfrm_policy *xp, int dir, int count, void *ptr)
1192{
1193 struct xfrm_dump_info *sp = ptr;
1194 struct xfrm_userpolicy_info *p;
1195 struct sk_buff *in_skb = sp->in_skb;
1196 struct sk_buff *skb = sp->out_skb;
1197 struct nlmsghdr *nlh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198
1199 if (sp->this_idx < sp->start_idx)
1200 goto out;
1201
Thomas Graf79b8b7f2007-08-22 12:46:53 -07001202 nlh = nlmsg_put(skb, NETLINK_CB(in_skb).pid, sp->nlmsg_seq,
1203 XFRM_MSG_NEWPOLICY, sizeof(*p), sp->nlmsg_flags);
1204 if (nlh == NULL)
1205 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206
Thomas Graf7b67c852007-08-22 13:53:52 -07001207 p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208 copy_to_user_policy(xp, p, dir);
1209 if (copy_to_user_tmpl(xp, skb) < 0)
1210 goto nlmsg_failure;
Trent Jaegerdf718372005-12-13 23:12:27 -08001211 if (copy_to_user_sec_ctx(xp, skb))
1212 goto nlmsg_failure;
Jamal Hadi Salim1459bb32006-11-20 16:51:22 -08001213 if (copy_to_user_policy_type(xp->type, skb) < 0)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001214 goto nlmsg_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215
Thomas Graf98250692007-08-22 12:47:26 -07001216 nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217out:
1218 sp->this_idx++;
1219 return 0;
1220
1221nlmsg_failure:
Thomas Graf98250692007-08-22 12:47:26 -07001222 nlmsg_cancel(skb, nlh);
1223 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224}
1225
1226static int xfrm_dump_policy(struct sk_buff *skb, struct netlink_callback *cb)
1227{
1228 struct xfrm_dump_info info;
1229
1230 info.in_skb = cb->skb;
1231 info.out_skb = skb;
1232 info.nlmsg_seq = cb->nlh->nlmsg_seq;
1233 info.nlmsg_flags = NLM_F_MULTI;
1234 info.this_idx = 0;
1235 info.start_idx = cb->args[0];
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001236 (void) xfrm_policy_walk(XFRM_POLICY_TYPE_MAIN, dump_one_policy, &info);
1237#ifdef CONFIG_XFRM_SUB_POLICY
1238 (void) xfrm_policy_walk(XFRM_POLICY_TYPE_SUB, dump_one_policy, &info);
1239#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240 cb->args[0] = info.this_idx;
1241
1242 return skb->len;
1243}
1244
1245static struct sk_buff *xfrm_policy_netlink(struct sk_buff *in_skb,
1246 struct xfrm_policy *xp,
1247 int dir, u32 seq)
1248{
1249 struct xfrm_dump_info info;
1250 struct sk_buff *skb;
1251
Thomas Graf7deb2262007-08-22 13:57:39 -07001252 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253 if (!skb)
1254 return ERR_PTR(-ENOMEM);
1255
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 info.in_skb = in_skb;
1257 info.out_skb = skb;
1258 info.nlmsg_seq = seq;
1259 info.nlmsg_flags = 0;
1260 info.this_idx = info.start_idx = 0;
1261
1262 if (dump_one_policy(xp, dir, 0, &info) < 0) {
1263 kfree_skb(skb);
1264 return NULL;
1265 }
1266
1267 return skb;
1268}
1269
Christoph Hellwig22e70052007-01-02 15:22:30 -08001270static int xfrm_get_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001271 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272{
1273 struct xfrm_policy *xp;
1274 struct xfrm_userpolicy_id *p;
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001275 u8 type = XFRM_POLICY_TYPE_MAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 int err;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001277 struct km_event c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278 int delete;
1279
Thomas Graf7b67c852007-08-22 13:53:52 -07001280 p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 delete = nlh->nlmsg_type == XFRM_MSG_DELPOLICY;
1282
Thomas Graf35a7aa02007-08-22 14:00:40 -07001283 err = copy_from_user_policy_type(&type, attrs);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001284 if (err)
1285 return err;
1286
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287 err = verify_policy_dir(p->dir);
1288 if (err)
1289 return err;
1290
1291 if (p->index)
Eric Parisef41aaa2007-03-07 15:37:58 -08001292 xp = xfrm_policy_byid(type, p->dir, p->index, delete, &err);
Trent Jaegerdf718372005-12-13 23:12:27 -08001293 else {
Thomas Graf5424f322007-08-22 14:01:33 -07001294 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
Trent Jaegerdf718372005-12-13 23:12:27 -08001295 struct xfrm_policy tmp;
1296
Thomas Graf35a7aa02007-08-22 14:00:40 -07001297 err = verify_sec_ctx_len(attrs);
Trent Jaegerdf718372005-12-13 23:12:27 -08001298 if (err)
1299 return err;
1300
1301 memset(&tmp, 0, sizeof(struct xfrm_policy));
1302 if (rt) {
Thomas Graf5424f322007-08-22 14:01:33 -07001303 struct xfrm_user_sec_ctx *uctx = nla_data(rt);
Trent Jaegerdf718372005-12-13 23:12:27 -08001304
1305 if ((err = security_xfrm_policy_alloc(&tmp, uctx)))
1306 return err;
1307 }
Eric Parisef41aaa2007-03-07 15:37:58 -08001308 xp = xfrm_policy_bysel_ctx(type, p->dir, &p->sel, tmp.security,
1309 delete, &err);
Trent Jaegerdf718372005-12-13 23:12:27 -08001310 security_xfrm_policy_free(&tmp);
1311 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312 if (xp == NULL)
1313 return -ENOENT;
1314
1315 if (!delete) {
1316 struct sk_buff *resp_skb;
1317
1318 resp_skb = xfrm_policy_netlink(skb, xp, p->dir, nlh->nlmsg_seq);
1319 if (IS_ERR(resp_skb)) {
1320 err = PTR_ERR(resp_skb);
1321 } else {
Thomas Graf082a1ad2007-08-22 13:54:36 -07001322 err = nlmsg_unicast(xfrm_nl, resp_skb,
1323 NETLINK_CB(skb).pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324 }
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001325 } else {
David S. Miller13fcfbb02007-02-12 13:53:54 -08001326 xfrm_audit_log(NETLINK_CB(skb).loginuid, NETLINK_CB(skb).sid,
1327 AUDIT_MAC_IPSEC_DELSPD, err ? 0 : 1, xp, NULL);
1328
1329 if (err != 0)
Catherine Zhangc8c05a82006-06-08 23:39:49 -07001330 goto out;
David S. Miller13fcfbb02007-02-12 13:53:54 -08001331
Herbert Xue7443892005-06-18 22:44:18 -07001332 c.data.byid = p->index;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001333 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001334 c.seq = nlh->nlmsg_seq;
1335 c.pid = nlh->nlmsg_pid;
1336 km_policy_notify(xp, p->dir, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 }
1338
Catherine Zhangc8c05a82006-06-08 23:39:49 -07001339out:
Eric Parisef41aaa2007-03-07 15:37:58 -08001340 xfrm_pol_put(xp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341 return err;
1342}
1343
Christoph Hellwig22e70052007-01-02 15:22:30 -08001344static int xfrm_flush_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001345 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346{
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001347 struct km_event c;
Thomas Graf7b67c852007-08-22 13:53:52 -07001348 struct xfrm_usersa_flush *p = nlmsg_data(nlh);
Joy Latten161a09e2006-11-27 13:11:54 -06001349 struct xfrm_audit audit_info;
Joy Latten4aa2e622007-06-04 19:05:57 -04001350 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351
Joy Latten161a09e2006-11-27 13:11:54 -06001352 audit_info.loginuid = NETLINK_CB(skb).loginuid;
1353 audit_info.secid = NETLINK_CB(skb).sid;
Joy Latten4aa2e622007-06-04 19:05:57 -04001354 err = xfrm_state_flush(p->proto, &audit_info);
1355 if (err)
1356 return err;
Herbert Xubf088672005-06-18 22:44:00 -07001357 c.data.proto = p->proto;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001358 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001359 c.seq = nlh->nlmsg_seq;
1360 c.pid = nlh->nlmsg_pid;
1361 km_state_notify(NULL, &c);
1362
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363 return 0;
1364}
1365
Thomas Graf7deb2262007-08-22 13:57:39 -07001366static inline size_t xfrm_aevent_msgsize(void)
1367{
1368 return NLMSG_ALIGN(sizeof(struct xfrm_aevent_id))
1369 + nla_total_size(sizeof(struct xfrm_replay_state))
1370 + nla_total_size(sizeof(struct xfrm_lifetime_cur))
1371 + nla_total_size(4) /* XFRM_AE_RTHR */
1372 + nla_total_size(4); /* XFRM_AE_ETHR */
1373}
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001374
1375static int build_aevent(struct sk_buff *skb, struct xfrm_state *x, struct km_event *c)
1376{
1377 struct xfrm_aevent_id *id;
1378 struct nlmsghdr *nlh;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001379
Thomas Graf79b8b7f2007-08-22 12:46:53 -07001380 nlh = nlmsg_put(skb, c->pid, c->seq, XFRM_MSG_NEWAE, sizeof(*id), 0);
1381 if (nlh == NULL)
1382 return -EMSGSIZE;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001383
Thomas Graf7b67c852007-08-22 13:53:52 -07001384 id = nlmsg_data(nlh);
Jamal Hadi Salim2b5f6dc2006-12-02 22:22:25 -08001385 memcpy(&id->sa_id.daddr, &x->id.daddr,sizeof(x->id.daddr));
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001386 id->sa_id.spi = x->id.spi;
1387 id->sa_id.family = x->props.family;
1388 id->sa_id.proto = x->id.proto;
Jamal Hadi Salim2b5f6dc2006-12-02 22:22:25 -08001389 memcpy(&id->saddr, &x->props.saddr,sizeof(x->props.saddr));
1390 id->reqid = x->props.reqid;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001391 id->flags = c->data.aevent;
1392
Thomas Grafc0144be2007-08-22 13:55:43 -07001393 NLA_PUT(skb, XFRMA_REPLAY_VAL, sizeof(x->replay), &x->replay);
1394 NLA_PUT(skb, XFRMA_LTIME_VAL, sizeof(x->curlft), &x->curlft);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001395
Thomas Grafc0144be2007-08-22 13:55:43 -07001396 if (id->flags & XFRM_AE_RTHR)
1397 NLA_PUT_U32(skb, XFRMA_REPLAY_THRESH, x->replay_maxdiff);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001398
Thomas Grafc0144be2007-08-22 13:55:43 -07001399 if (id->flags & XFRM_AE_ETHR)
1400 NLA_PUT_U32(skb, XFRMA_ETIMER_THRESH,
1401 x->replay_maxage * 10 / HZ);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001402
Thomas Graf98250692007-08-22 12:47:26 -07001403 return nlmsg_end(skb, nlh);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001404
Thomas Grafc0144be2007-08-22 13:55:43 -07001405nla_put_failure:
Thomas Graf98250692007-08-22 12:47:26 -07001406 nlmsg_cancel(skb, nlh);
1407 return -EMSGSIZE;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001408}
1409
Christoph Hellwig22e70052007-01-02 15:22:30 -08001410static int xfrm_get_ae(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001411 struct nlattr **attrs)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001412{
1413 struct xfrm_state *x;
1414 struct sk_buff *r_skb;
1415 int err;
1416 struct km_event c;
Thomas Graf7b67c852007-08-22 13:53:52 -07001417 struct xfrm_aevent_id *p = nlmsg_data(nlh);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001418 struct xfrm_usersa_id *id = &p->sa_id;
1419
Thomas Graf7deb2262007-08-22 13:57:39 -07001420 r_skb = nlmsg_new(xfrm_aevent_msgsize(), GFP_ATOMIC);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001421 if (r_skb == NULL)
1422 return -ENOMEM;
1423
1424 x = xfrm_state_lookup(&id->daddr, id->spi, id->proto, id->family);
1425 if (x == NULL) {
Patrick McHardyb08d5842007-02-27 09:57:37 -08001426 kfree_skb(r_skb);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001427 return -ESRCH;
1428 }
1429
1430 /*
1431 * XXX: is this lock really needed - none of the other
1432 * gets lock (the concern is things getting updated
1433 * while we are still reading) - jhs
1434 */
1435 spin_lock_bh(&x->lock);
1436 c.data.aevent = p->flags;
1437 c.seq = nlh->nlmsg_seq;
1438 c.pid = nlh->nlmsg_pid;
1439
1440 if (build_aevent(r_skb, x, &c) < 0)
1441 BUG();
Thomas Graf082a1ad2007-08-22 13:54:36 -07001442 err = nlmsg_unicast(xfrm_nl, r_skb, NETLINK_CB(skb).pid);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001443 spin_unlock_bh(&x->lock);
1444 xfrm_state_put(x);
1445 return err;
1446}
1447
Christoph Hellwig22e70052007-01-02 15:22:30 -08001448static int xfrm_new_ae(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001449 struct nlattr **attrs)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001450{
1451 struct xfrm_state *x;
1452 struct km_event c;
1453 int err = - EINVAL;
Thomas Graf7b67c852007-08-22 13:53:52 -07001454 struct xfrm_aevent_id *p = nlmsg_data(nlh);
Thomas Graf5424f322007-08-22 14:01:33 -07001455 struct nlattr *rp = attrs[XFRMA_REPLAY_VAL];
1456 struct nlattr *lt = attrs[XFRMA_LTIME_VAL];
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001457
1458 if (!lt && !rp)
1459 return err;
1460
1461 /* pedantic mode - thou shalt sayeth replaceth */
1462 if (!(nlh->nlmsg_flags&NLM_F_REPLACE))
1463 return err;
1464
1465 x = xfrm_state_lookup(&p->sa_id.daddr, p->sa_id.spi, p->sa_id.proto, p->sa_id.family);
1466 if (x == NULL)
1467 return -ESRCH;
1468
1469 if (x->km.state != XFRM_STATE_VALID)
1470 goto out;
1471
1472 spin_lock_bh(&x->lock);
Thomas Graf35a7aa02007-08-22 14:00:40 -07001473 xfrm_update_ae_params(x, attrs);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001474 spin_unlock_bh(&x->lock);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001475
1476 c.event = nlh->nlmsg_type;
1477 c.seq = nlh->nlmsg_seq;
1478 c.pid = nlh->nlmsg_pid;
1479 c.data.aevent = XFRM_AE_CU;
1480 km_state_notify(x, &c);
1481 err = 0;
1482out:
1483 xfrm_state_put(x);
1484 return err;
1485}
1486
Christoph Hellwig22e70052007-01-02 15:22:30 -08001487static int xfrm_flush_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001488 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489{
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001490 struct km_event c;
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001491 u8 type = XFRM_POLICY_TYPE_MAIN;
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001492 int err;
Joy Latten161a09e2006-11-27 13:11:54 -06001493 struct xfrm_audit audit_info;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001494
Thomas Graf35a7aa02007-08-22 14:00:40 -07001495 err = copy_from_user_policy_type(&type, attrs);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001496 if (err)
1497 return err;
1498
Joy Latten161a09e2006-11-27 13:11:54 -06001499 audit_info.loginuid = NETLINK_CB(skb).loginuid;
1500 audit_info.secid = NETLINK_CB(skb).sid;
Joy Latten4aa2e622007-06-04 19:05:57 -04001501 err = xfrm_policy_flush(type, &audit_info);
1502 if (err)
1503 return err;
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001504 c.data.type = type;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001505 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001506 c.seq = nlh->nlmsg_seq;
1507 c.pid = nlh->nlmsg_pid;
1508 km_policy_notify(NULL, 0, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509 return 0;
1510}
1511
Christoph Hellwig22e70052007-01-02 15:22:30 -08001512static int xfrm_add_pol_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001513 struct nlattr **attrs)
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001514{
1515 struct xfrm_policy *xp;
Thomas Graf7b67c852007-08-22 13:53:52 -07001516 struct xfrm_user_polexpire *up = nlmsg_data(nlh);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001517 struct xfrm_userpolicy_info *p = &up->pol;
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001518 u8 type = XFRM_POLICY_TYPE_MAIN;
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001519 int err = -ENOENT;
1520
Thomas Graf35a7aa02007-08-22 14:00:40 -07001521 err = copy_from_user_policy_type(&type, attrs);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001522 if (err)
1523 return err;
1524
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001525 if (p->index)
Eric Parisef41aaa2007-03-07 15:37:58 -08001526 xp = xfrm_policy_byid(type, p->dir, p->index, 0, &err);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001527 else {
Thomas Graf5424f322007-08-22 14:01:33 -07001528 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001529 struct xfrm_policy tmp;
1530
Thomas Graf35a7aa02007-08-22 14:00:40 -07001531 err = verify_sec_ctx_len(attrs);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001532 if (err)
1533 return err;
1534
1535 memset(&tmp, 0, sizeof(struct xfrm_policy));
1536 if (rt) {
Thomas Graf5424f322007-08-22 14:01:33 -07001537 struct xfrm_user_sec_ctx *uctx = nla_data(rt);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001538
1539 if ((err = security_xfrm_policy_alloc(&tmp, uctx)))
1540 return err;
1541 }
Eric Parisef41aaa2007-03-07 15:37:58 -08001542 xp = xfrm_policy_bysel_ctx(type, p->dir, &p->sel, tmp.security,
1543 0, &err);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001544 security_xfrm_policy_free(&tmp);
1545 }
1546
1547 if (xp == NULL)
Eric Parisef41aaa2007-03-07 15:37:58 -08001548 return -ENOENT;
1549 read_lock(&xp->lock);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001550 if (xp->dead) {
1551 read_unlock(&xp->lock);
1552 goto out;
1553 }
1554
1555 read_unlock(&xp->lock);
1556 err = 0;
1557 if (up->hard) {
1558 xfrm_policy_delete(xp, p->dir);
Joy Latten161a09e2006-11-27 13:11:54 -06001559 xfrm_audit_log(NETLINK_CB(skb).loginuid, NETLINK_CB(skb).sid,
1560 AUDIT_MAC_IPSEC_DELSPD, 1, xp, NULL);
1561
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001562 } else {
1563 // reset the timers here?
1564 printk("Dont know what to do with soft policy expire\n");
1565 }
1566 km_policy_expired(xp, p->dir, up->hard, current->pid);
1567
1568out:
1569 xfrm_pol_put(xp);
1570 return err;
1571}
1572
Christoph Hellwig22e70052007-01-02 15:22:30 -08001573static int xfrm_add_sa_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001574 struct nlattr **attrs)
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001575{
1576 struct xfrm_state *x;
1577 int err;
Thomas Graf7b67c852007-08-22 13:53:52 -07001578 struct xfrm_user_expire *ue = nlmsg_data(nlh);
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001579 struct xfrm_usersa_info *p = &ue->state;
1580
1581 x = xfrm_state_lookup(&p->id.daddr, p->id.spi, p->id.proto, p->family);
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001582
David S. Miller3a765aa2007-02-26 14:52:21 -08001583 err = -ENOENT;
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001584 if (x == NULL)
1585 return err;
1586
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001587 spin_lock_bh(&x->lock);
David S. Miller3a765aa2007-02-26 14:52:21 -08001588 err = -EINVAL;
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001589 if (x->km.state != XFRM_STATE_VALID)
1590 goto out;
1591 km_state_expired(x, ue->hard, current->pid);
1592
Joy Latten161a09e2006-11-27 13:11:54 -06001593 if (ue->hard) {
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001594 __xfrm_state_delete(x);
Joy Latten161a09e2006-11-27 13:11:54 -06001595 xfrm_audit_log(NETLINK_CB(skb).loginuid, NETLINK_CB(skb).sid,
1596 AUDIT_MAC_IPSEC_DELSA, 1, NULL, x);
1597 }
David S. Miller3a765aa2007-02-26 14:52:21 -08001598 err = 0;
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001599out:
1600 spin_unlock_bh(&x->lock);
1601 xfrm_state_put(x);
1602 return err;
1603}
1604
Christoph Hellwig22e70052007-01-02 15:22:30 -08001605static int xfrm_add_acquire(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001606 struct nlattr **attrs)
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001607{
1608 struct xfrm_policy *xp;
1609 struct xfrm_user_tmpl *ut;
1610 int i;
Thomas Graf5424f322007-08-22 14:01:33 -07001611 struct nlattr *rt = attrs[XFRMA_TMPL];
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001612
Thomas Graf7b67c852007-08-22 13:53:52 -07001613 struct xfrm_user_acquire *ua = nlmsg_data(nlh);
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001614 struct xfrm_state *x = xfrm_state_alloc();
1615 int err = -ENOMEM;
1616
1617 if (!x)
1618 return err;
1619
1620 err = verify_newpolicy_info(&ua->policy);
1621 if (err) {
1622 printk("BAD policy passed\n");
1623 kfree(x);
1624 return err;
1625 }
1626
1627 /* build an XP */
Thomas Graf5424f322007-08-22 14:01:33 -07001628 xp = xfrm_policy_construct(&ua->policy, attrs, &err);
David S. Millerb4ad86bf2006-12-03 19:19:26 -08001629 if (!xp) {
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001630 kfree(x);
1631 return err;
1632 }
1633
1634 memcpy(&x->id, &ua->id, sizeof(ua->id));
1635 memcpy(&x->props.saddr, &ua->saddr, sizeof(ua->saddr));
1636 memcpy(&x->sel, &ua->sel, sizeof(ua->sel));
1637
Thomas Graf5424f322007-08-22 14:01:33 -07001638 ut = nla_data(rt);
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001639 /* extract the templates and for each call km_key */
1640 for (i = 0; i < xp->xfrm_nr; i++, ut++) {
1641 struct xfrm_tmpl *t = &xp->xfrm_vec[i];
1642 memcpy(&x->id, &t->id, sizeof(x->id));
1643 x->props.mode = t->mode;
1644 x->props.reqid = t->reqid;
1645 x->props.family = ut->family;
1646 t->aalgos = ua->aalgos;
1647 t->ealgos = ua->ealgos;
1648 t->calgos = ua->calgos;
1649 err = km_query(x, t, xp);
1650
1651 }
1652
1653 kfree(x);
1654 kfree(xp);
1655
1656 return 0;
1657}
1658
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001659#ifdef CONFIG_XFRM_MIGRATE
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001660static int copy_from_user_migrate(struct xfrm_migrate *ma,
Thomas Graf5424f322007-08-22 14:01:33 -07001661 struct nlattr **attrs, int *num)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001662{
Thomas Graf5424f322007-08-22 14:01:33 -07001663 struct nlattr *rt = attrs[XFRMA_MIGRATE];
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001664 struct xfrm_user_migrate *um;
1665 int i, num_migrate;
1666
Thomas Graf5424f322007-08-22 14:01:33 -07001667 um = nla_data(rt);
1668 num_migrate = nla_len(rt) / sizeof(*um);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001669
1670 if (num_migrate <= 0 || num_migrate > XFRM_MAX_DEPTH)
1671 return -EINVAL;
1672
1673 for (i = 0; i < num_migrate; i++, um++, ma++) {
1674 memcpy(&ma->old_daddr, &um->old_daddr, sizeof(ma->old_daddr));
1675 memcpy(&ma->old_saddr, &um->old_saddr, sizeof(ma->old_saddr));
1676 memcpy(&ma->new_daddr, &um->new_daddr, sizeof(ma->new_daddr));
1677 memcpy(&ma->new_saddr, &um->new_saddr, sizeof(ma->new_saddr));
1678
1679 ma->proto = um->proto;
1680 ma->mode = um->mode;
1681 ma->reqid = um->reqid;
1682
1683 ma->old_family = um->old_family;
1684 ma->new_family = um->new_family;
1685 }
1686
1687 *num = i;
1688 return 0;
1689}
1690
1691static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001692 struct nlattr **attrs)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001693{
Thomas Graf7b67c852007-08-22 13:53:52 -07001694 struct xfrm_userpolicy_id *pi = nlmsg_data(nlh);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001695 struct xfrm_migrate m[XFRM_MAX_DEPTH];
1696 u8 type;
1697 int err;
1698 int n = 0;
1699
Thomas Graf35a7aa02007-08-22 14:00:40 -07001700 if (attrs[XFRMA_MIGRATE] == NULL)
Thomas Grafcf5cb792007-08-22 13:59:04 -07001701 return -EINVAL;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001702
Thomas Graf5424f322007-08-22 14:01:33 -07001703 err = copy_from_user_policy_type(&type, attrs);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001704 if (err)
1705 return err;
1706
1707 err = copy_from_user_migrate((struct xfrm_migrate *)m,
Thomas Graf5424f322007-08-22 14:01:33 -07001708 attrs, &n);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001709 if (err)
1710 return err;
1711
1712 if (!n)
1713 return 0;
1714
1715 xfrm_migrate(&pi->sel, pi->dir, type, m, n);
1716
1717 return 0;
1718}
1719#else
1720static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001721 struct nlattr **attrs)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001722{
1723 return -ENOPROTOOPT;
1724}
1725#endif
1726
1727#ifdef CONFIG_XFRM_MIGRATE
1728static int copy_to_user_migrate(struct xfrm_migrate *m, struct sk_buff *skb)
1729{
1730 struct xfrm_user_migrate um;
1731
1732 memset(&um, 0, sizeof(um));
1733 um.proto = m->proto;
1734 um.mode = m->mode;
1735 um.reqid = m->reqid;
1736 um.old_family = m->old_family;
1737 memcpy(&um.old_daddr, &m->old_daddr, sizeof(um.old_daddr));
1738 memcpy(&um.old_saddr, &m->old_saddr, sizeof(um.old_saddr));
1739 um.new_family = m->new_family;
1740 memcpy(&um.new_daddr, &m->new_daddr, sizeof(um.new_daddr));
1741 memcpy(&um.new_saddr, &m->new_saddr, sizeof(um.new_saddr));
1742
Thomas Grafc0144be2007-08-22 13:55:43 -07001743 return nla_put(skb, XFRMA_MIGRATE, sizeof(um), &um);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001744}
1745
Thomas Graf7deb2262007-08-22 13:57:39 -07001746static inline size_t xfrm_migrate_msgsize(int num_migrate)
1747{
1748 return NLMSG_ALIGN(sizeof(struct xfrm_userpolicy_id))
1749 + nla_total_size(sizeof(struct xfrm_user_migrate) * num_migrate)
1750 + userpolicy_type_attrsize();
1751}
1752
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001753static int build_migrate(struct sk_buff *skb, struct xfrm_migrate *m,
1754 int num_migrate, struct xfrm_selector *sel,
1755 u8 dir, u8 type)
1756{
1757 struct xfrm_migrate *mp;
1758 struct xfrm_userpolicy_id *pol_id;
1759 struct nlmsghdr *nlh;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001760 int i;
1761
Thomas Graf79b8b7f2007-08-22 12:46:53 -07001762 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_MIGRATE, sizeof(*pol_id), 0);
1763 if (nlh == NULL)
1764 return -EMSGSIZE;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001765
Thomas Graf7b67c852007-08-22 13:53:52 -07001766 pol_id = nlmsg_data(nlh);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001767 /* copy data from selector, dir, and type to the pol_id */
1768 memset(pol_id, 0, sizeof(*pol_id));
1769 memcpy(&pol_id->sel, sel, sizeof(pol_id->sel));
1770 pol_id->dir = dir;
1771
1772 if (copy_to_user_policy_type(type, skb) < 0)
1773 goto nlmsg_failure;
1774
1775 for (i = 0, mp = m ; i < num_migrate; i++, mp++) {
1776 if (copy_to_user_migrate(mp, skb) < 0)
1777 goto nlmsg_failure;
1778 }
1779
Thomas Graf98250692007-08-22 12:47:26 -07001780 return nlmsg_end(skb, nlh);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001781nlmsg_failure:
Thomas Graf98250692007-08-22 12:47:26 -07001782 nlmsg_cancel(skb, nlh);
1783 return -EMSGSIZE;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001784}
1785
1786static int xfrm_send_migrate(struct xfrm_selector *sel, u8 dir, u8 type,
1787 struct xfrm_migrate *m, int num_migrate)
1788{
1789 struct sk_buff *skb;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001790
Thomas Graf7deb2262007-08-22 13:57:39 -07001791 skb = nlmsg_new(xfrm_migrate_msgsize(num_migrate), GFP_ATOMIC);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001792 if (skb == NULL)
1793 return -ENOMEM;
1794
1795 /* build migrate */
1796 if (build_migrate(skb, m, num_migrate, sel, dir, type) < 0)
1797 BUG();
1798
Thomas Graf082a1ad2007-08-22 13:54:36 -07001799 return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_MIGRATE, GFP_ATOMIC);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001800}
1801#else
1802static int xfrm_send_migrate(struct xfrm_selector *sel, u8 dir, u8 type,
1803 struct xfrm_migrate *m, int num_migrate)
1804{
1805 return -ENOPROTOOPT;
1806}
1807#endif
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001808
Thomas Grafa7bd9a42007-08-22 13:58:18 -07001809#define XMSGSIZE(type) sizeof(struct type)
Thomas Graf492b5582005-05-03 14:26:40 -07001810
1811static const int xfrm_msg_min[XFRM_NR_MSGTYPES] = {
1812 [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
1813 [XFRM_MSG_DELSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
1814 [XFRM_MSG_GETSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
1815 [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
1816 [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
1817 [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
1818 [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userspi_info),
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001819 [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_acquire),
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001820 [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_expire),
Thomas Graf492b5582005-05-03 14:26:40 -07001821 [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
1822 [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001823 [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_polexpire),
Thomas Graf492b5582005-05-03 14:26:40 -07001824 [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_flush),
Thomas Grafa7bd9a42007-08-22 13:58:18 -07001825 [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = 0,
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001826 [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
1827 [XFRM_MSG_GETAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07001828 [XFRM_MSG_REPORT - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_report),
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001829 [XFRM_MSG_MIGRATE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
Thomas Grafa7bd9a42007-08-22 13:58:18 -07001830 [XFRM_MSG_GETSADINFO - XFRM_MSG_BASE] = sizeof(u32),
1831 [XFRM_MSG_GETSPDINFO - XFRM_MSG_BASE] = sizeof(u32),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832};
1833
Thomas Graf492b5582005-05-03 14:26:40 -07001834#undef XMSGSIZE
1835
Thomas Grafcf5cb792007-08-22 13:59:04 -07001836static const struct nla_policy xfrma_policy[XFRMA_MAX+1] = {
1837 [XFRMA_ALG_AUTH] = { .len = sizeof(struct xfrm_algo) },
1838 [XFRMA_ALG_CRYPT] = { .len = sizeof(struct xfrm_algo) },
1839 [XFRMA_ALG_COMP] = { .len = sizeof(struct xfrm_algo) },
1840 [XFRMA_ENCAP] = { .len = sizeof(struct xfrm_encap_tmpl) },
1841 [XFRMA_TMPL] = { .len = sizeof(struct xfrm_user_tmpl) },
1842 [XFRMA_SEC_CTX] = { .len = sizeof(struct xfrm_sec_ctx) },
1843 [XFRMA_LTIME_VAL] = { .len = sizeof(struct xfrm_lifetime_cur) },
1844 [XFRMA_REPLAY_VAL] = { .len = sizeof(struct xfrm_replay_state) },
1845 [XFRMA_REPLAY_THRESH] = { .type = NLA_U32 },
1846 [XFRMA_ETIMER_THRESH] = { .type = NLA_U32 },
1847 [XFRMA_SRCADDR] = { .len = sizeof(xfrm_address_t) },
1848 [XFRMA_COADDR] = { .len = sizeof(xfrm_address_t) },
1849 [XFRMA_POLICY_TYPE] = { .len = sizeof(struct xfrm_userpolicy_type)},
1850 [XFRMA_MIGRATE] = { .len = sizeof(struct xfrm_user_migrate) },
1851};
1852
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853static struct xfrm_link {
Thomas Graf5424f322007-08-22 14:01:33 -07001854 int (*doit)(struct sk_buff *, struct nlmsghdr *, struct nlattr **);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855 int (*dump)(struct sk_buff *, struct netlink_callback *);
Thomas Graf492b5582005-05-03 14:26:40 -07001856} xfrm_dispatch[XFRM_NR_MSGTYPES] = {
1857 [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa },
1858 [XFRM_MSG_DELSA - XFRM_MSG_BASE] = { .doit = xfrm_del_sa },
1859 [XFRM_MSG_GETSA - XFRM_MSG_BASE] = { .doit = xfrm_get_sa,
1860 .dump = xfrm_dump_sa },
1861 [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy },
1862 [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy },
1863 [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy,
1864 .dump = xfrm_dump_policy },
1865 [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = { .doit = xfrm_alloc_userspi },
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001866 [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_acquire },
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001867 [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_sa_expire },
Thomas Graf492b5582005-05-03 14:26:40 -07001868 [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy },
1869 [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa },
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001870 [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_pol_expire},
Thomas Graf492b5582005-05-03 14:26:40 -07001871 [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = { .doit = xfrm_flush_sa },
1872 [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_flush_policy },
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001873 [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = { .doit = xfrm_new_ae },
1874 [XFRM_MSG_GETAE - XFRM_MSG_BASE] = { .doit = xfrm_get_ae },
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001875 [XFRM_MSG_MIGRATE - XFRM_MSG_BASE] = { .doit = xfrm_do_migrate },
Jamal Hadi Salim566ec032007-04-26 14:12:15 -07001876 [XFRM_MSG_GETSADINFO - XFRM_MSG_BASE] = { .doit = xfrm_get_sadinfo },
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -07001877 [XFRM_MSG_GETSPDINFO - XFRM_MSG_BASE] = { .doit = xfrm_get_spdinfo },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878};
1879
Thomas Graf1d00a4e2007-03-22 23:30:12 -07001880static int xfrm_user_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881{
Thomas Graf35a7aa02007-08-22 14:00:40 -07001882 struct nlattr *attrs[XFRMA_MAX+1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883 struct xfrm_link *link;
Thomas Grafa7bd9a42007-08-22 13:58:18 -07001884 int type, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886 type = nlh->nlmsg_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887 if (type > XFRM_MSG_MAX)
Thomas Graf1d00a4e2007-03-22 23:30:12 -07001888 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889
1890 type -= XFRM_MSG_BASE;
1891 link = &xfrm_dispatch[type];
1892
1893 /* All operations require privileges, even GET */
Thomas Graf1d00a4e2007-03-22 23:30:12 -07001894 if (security_netlink_recv(skb, CAP_NET_ADMIN))
1895 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896
Thomas Graf492b5582005-05-03 14:26:40 -07001897 if ((type == (XFRM_MSG_GETSA - XFRM_MSG_BASE) ||
1898 type == (XFRM_MSG_GETPOLICY - XFRM_MSG_BASE)) &&
1899 (nlh->nlmsg_flags & NLM_F_DUMP)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900 if (link->dump == NULL)
Thomas Graf1d00a4e2007-03-22 23:30:12 -07001901 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902
Thomas Grafc702e802007-03-22 23:30:55 -07001903 return netlink_dump_start(xfrm_nl, skb, nlh, link->dump, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904 }
1905
Thomas Graf35a7aa02007-08-22 14:00:40 -07001906 err = nlmsg_parse(nlh, xfrm_msg_min[type], attrs, XFRMA_MAX,
Thomas Grafcf5cb792007-08-22 13:59:04 -07001907 xfrma_policy);
Thomas Grafa7bd9a42007-08-22 13:58:18 -07001908 if (err < 0)
1909 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910
1911 if (link->doit == NULL)
Thomas Graf1d00a4e2007-03-22 23:30:12 -07001912 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913
Thomas Graf5424f322007-08-22 14:01:33 -07001914 return link->doit(skb, nlh, attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915}
1916
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917static void xfrm_netlink_rcv(struct sock *sk, int len)
1918{
Thomas Graf88fc2c82005-11-10 02:25:54 +01001919 unsigned int qlen = 0;
Herbert Xu2a0a6eb2005-05-03 14:55:09 -07001920
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921 do {
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -08001922 mutex_lock(&xfrm_cfg_mutex);
Thomas Graf88fc2c82005-11-10 02:25:54 +01001923 netlink_run_queue(sk, &qlen, &xfrm_user_rcv_msg);
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -08001924 mutex_unlock(&xfrm_cfg_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925
Herbert Xu2a0a6eb2005-05-03 14:55:09 -07001926 } while (qlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927}
1928
Thomas Graf7deb2262007-08-22 13:57:39 -07001929static inline size_t xfrm_expire_msgsize(void)
1930{
1931 return NLMSG_ALIGN(sizeof(struct xfrm_user_expire));
1932}
1933
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001934static int build_expire(struct sk_buff *skb, struct xfrm_state *x, struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935{
1936 struct xfrm_user_expire *ue;
1937 struct nlmsghdr *nlh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938
Thomas Graf79b8b7f2007-08-22 12:46:53 -07001939 nlh = nlmsg_put(skb, c->pid, 0, XFRM_MSG_EXPIRE, sizeof(*ue), 0);
1940 if (nlh == NULL)
1941 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942
Thomas Graf7b67c852007-08-22 13:53:52 -07001943 ue = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944 copy_to_user_state(x, &ue->state);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001945 ue->hard = (c->data.hard != 0) ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946
Thomas Graf98250692007-08-22 12:47:26 -07001947 return nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948}
1949
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001950static int xfrm_exp_state_notify(struct xfrm_state *x, struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951{
1952 struct sk_buff *skb;
1953
Thomas Graf7deb2262007-08-22 13:57:39 -07001954 skb = nlmsg_new(xfrm_expire_msgsize(), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955 if (skb == NULL)
1956 return -ENOMEM;
1957
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001958 if (build_expire(skb, x, c) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959 BUG();
1960
Thomas Graf082a1ad2007-08-22 13:54:36 -07001961 return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_EXPIRE, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962}
1963
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001964static int xfrm_aevent_state_notify(struct xfrm_state *x, struct km_event *c)
1965{
1966 struct sk_buff *skb;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001967
Thomas Graf7deb2262007-08-22 13:57:39 -07001968 skb = nlmsg_new(xfrm_aevent_msgsize(), GFP_ATOMIC);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001969 if (skb == NULL)
1970 return -ENOMEM;
1971
1972 if (build_aevent(skb, x, c) < 0)
1973 BUG();
1974
Thomas Graf082a1ad2007-08-22 13:54:36 -07001975 return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_AEVENTS, GFP_ATOMIC);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001976}
1977
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001978static int xfrm_notify_sa_flush(struct km_event *c)
1979{
1980 struct xfrm_usersa_flush *p;
1981 struct nlmsghdr *nlh;
1982 struct sk_buff *skb;
Thomas Graf7deb2262007-08-22 13:57:39 -07001983 int len = NLMSG_ALIGN(sizeof(struct xfrm_usersa_flush));
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001984
Thomas Graf7deb2262007-08-22 13:57:39 -07001985 skb = nlmsg_new(len, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001986 if (skb == NULL)
1987 return -ENOMEM;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001988
Thomas Graf79b8b7f2007-08-22 12:46:53 -07001989 nlh = nlmsg_put(skb, c->pid, c->seq, XFRM_MSG_FLUSHSA, sizeof(*p), 0);
1990 if (nlh == NULL) {
1991 kfree_skb(skb);
1992 return -EMSGSIZE;
1993 }
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001994
Thomas Graf7b67c852007-08-22 13:53:52 -07001995 p = nlmsg_data(nlh);
Herbert Xubf088672005-06-18 22:44:00 -07001996 p->proto = c->data.proto;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001997
Thomas Graf98250692007-08-22 12:47:26 -07001998 nlmsg_end(skb, nlh);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001999
Thomas Graf082a1ad2007-08-22 13:54:36 -07002000 return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_SA, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002001}
2002
Thomas Graf7deb2262007-08-22 13:57:39 -07002003static inline size_t xfrm_sa_len(struct xfrm_state *x)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002004{
Thomas Graf7deb2262007-08-22 13:57:39 -07002005 size_t l = 0;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002006 if (x->aalg)
Thomas Graf7deb2262007-08-22 13:57:39 -07002007 l += nla_total_size(alg_len(x->aalg));
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002008 if (x->ealg)
Thomas Graf7deb2262007-08-22 13:57:39 -07002009 l += nla_total_size(alg_len(x->ealg));
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002010 if (x->calg)
Thomas Graf7deb2262007-08-22 13:57:39 -07002011 l += nla_total_size(sizeof(*x->calg));
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002012 if (x->encap)
Thomas Graf7deb2262007-08-22 13:57:39 -07002013 l += nla_total_size(sizeof(*x->encap));
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002014
2015 return l;
2016}
2017
2018static int xfrm_notify_sa(struct xfrm_state *x, struct km_event *c)
2019{
2020 struct xfrm_usersa_info *p;
Herbert Xu0603eac2005-06-18 22:54:36 -07002021 struct xfrm_usersa_id *id;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002022 struct nlmsghdr *nlh;
2023 struct sk_buff *skb;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002024 int len = xfrm_sa_len(x);
Herbert Xu0603eac2005-06-18 22:54:36 -07002025 int headlen;
2026
2027 headlen = sizeof(*p);
2028 if (c->event == XFRM_MSG_DELSA) {
Thomas Graf7deb2262007-08-22 13:57:39 -07002029 len += nla_total_size(headlen);
Herbert Xu0603eac2005-06-18 22:54:36 -07002030 headlen = sizeof(*id);
2031 }
Thomas Graf7deb2262007-08-22 13:57:39 -07002032 len += NLMSG_ALIGN(headlen);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002033
Thomas Graf7deb2262007-08-22 13:57:39 -07002034 skb = nlmsg_new(len, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002035 if (skb == NULL)
2036 return -ENOMEM;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002037
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002038 nlh = nlmsg_put(skb, c->pid, c->seq, c->event, headlen, 0);
2039 if (nlh == NULL)
Thomas Grafc0144be2007-08-22 13:55:43 -07002040 goto nla_put_failure;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002041
Thomas Graf7b67c852007-08-22 13:53:52 -07002042 p = nlmsg_data(nlh);
Herbert Xu0603eac2005-06-18 22:54:36 -07002043 if (c->event == XFRM_MSG_DELSA) {
Thomas Grafc0144be2007-08-22 13:55:43 -07002044 struct nlattr *attr;
2045
Thomas Graf7b67c852007-08-22 13:53:52 -07002046 id = nlmsg_data(nlh);
Herbert Xu0603eac2005-06-18 22:54:36 -07002047 memcpy(&id->daddr, &x->id.daddr, sizeof(id->daddr));
2048 id->spi = x->id.spi;
2049 id->family = x->props.family;
2050 id->proto = x->id.proto;
2051
Thomas Grafc0144be2007-08-22 13:55:43 -07002052 attr = nla_reserve(skb, XFRMA_SA, sizeof(*p));
2053 if (attr == NULL)
2054 goto nla_put_failure;
2055
2056 p = nla_data(attr);
Herbert Xu0603eac2005-06-18 22:54:36 -07002057 }
2058
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002059 copy_to_user_state(x, p);
2060
2061 if (x->aalg)
Thomas Grafc26445a2007-08-22 13:56:23 -07002062 NLA_PUT(skb, XFRMA_ALG_AUTH, alg_len(x->aalg), x->aalg);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002063 if (x->ealg)
Thomas Grafc26445a2007-08-22 13:56:23 -07002064 NLA_PUT(skb, XFRMA_ALG_CRYPT, alg_len(x->ealg), x->ealg);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002065 if (x->calg)
Thomas Grafc0144be2007-08-22 13:55:43 -07002066 NLA_PUT(skb, XFRMA_ALG_COMP, sizeof(*(x->calg)), x->calg);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002067
2068 if (x->encap)
Thomas Grafc0144be2007-08-22 13:55:43 -07002069 NLA_PUT(skb, XFRMA_ENCAP, sizeof(*x->encap), x->encap);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002070
Thomas Graf98250692007-08-22 12:47:26 -07002071 nlmsg_end(skb, nlh);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002072
Thomas Graf082a1ad2007-08-22 13:54:36 -07002073 return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_SA, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002074
Thomas Grafc0144be2007-08-22 13:55:43 -07002075nla_put_failure:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002076 kfree_skb(skb);
2077 return -1;
2078}
2079
2080static int xfrm_send_state_notify(struct xfrm_state *x, struct km_event *c)
2081{
2082
2083 switch (c->event) {
Herbert Xuf60f6b82005-06-18 22:44:37 -07002084 case XFRM_MSG_EXPIRE:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002085 return xfrm_exp_state_notify(x, c);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002086 case XFRM_MSG_NEWAE:
2087 return xfrm_aevent_state_notify(x, c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07002088 case XFRM_MSG_DELSA:
2089 case XFRM_MSG_UPDSA:
2090 case XFRM_MSG_NEWSA:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002091 return xfrm_notify_sa(x, c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07002092 case XFRM_MSG_FLUSHSA:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002093 return xfrm_notify_sa_flush(c);
2094 default:
2095 printk("xfrm_user: Unknown SA event %d\n", c->event);
2096 break;
2097 }
2098
2099 return 0;
2100
2101}
2102
Thomas Graf7deb2262007-08-22 13:57:39 -07002103static inline size_t xfrm_acquire_msgsize(struct xfrm_state *x,
2104 struct xfrm_policy *xp)
2105{
2106 return NLMSG_ALIGN(sizeof(struct xfrm_user_acquire))
2107 + nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr)
2108 + nla_total_size(xfrm_user_sec_ctx_size(x->security))
2109 + userpolicy_type_attrsize();
2110}
2111
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112static int build_acquire(struct sk_buff *skb, struct xfrm_state *x,
2113 struct xfrm_tmpl *xt, struct xfrm_policy *xp,
2114 int dir)
2115{
2116 struct xfrm_user_acquire *ua;
2117 struct nlmsghdr *nlh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118 __u32 seq = xfrm_get_acqseq();
2119
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002120 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_ACQUIRE, sizeof(*ua), 0);
2121 if (nlh == NULL)
2122 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123
Thomas Graf7b67c852007-08-22 13:53:52 -07002124 ua = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125 memcpy(&ua->id, &x->id, sizeof(ua->id));
2126 memcpy(&ua->saddr, &x->props.saddr, sizeof(ua->saddr));
2127 memcpy(&ua->sel, &x->sel, sizeof(ua->sel));
2128 copy_to_user_policy(xp, &ua->policy, dir);
2129 ua->aalgos = xt->aalgos;
2130 ua->ealgos = xt->ealgos;
2131 ua->calgos = xt->calgos;
2132 ua->seq = x->km.seq = seq;
2133
2134 if (copy_to_user_tmpl(xp, skb) < 0)
2135 goto nlmsg_failure;
Serge Hallyn0d681622006-07-24 23:30:44 -07002136 if (copy_to_user_state_sec_ctx(x, skb))
Trent Jaegerdf718372005-12-13 23:12:27 -08002137 goto nlmsg_failure;
Jamal Hadi Salim1459bb32006-11-20 16:51:22 -08002138 if (copy_to_user_policy_type(xp->type, skb) < 0)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07002139 goto nlmsg_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140
Thomas Graf98250692007-08-22 12:47:26 -07002141 return nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142
2143nlmsg_failure:
Thomas Graf98250692007-08-22 12:47:26 -07002144 nlmsg_cancel(skb, nlh);
2145 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146}
2147
2148static int xfrm_send_acquire(struct xfrm_state *x, struct xfrm_tmpl *xt,
2149 struct xfrm_policy *xp, int dir)
2150{
2151 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152
Thomas Graf7deb2262007-08-22 13:57:39 -07002153 skb = nlmsg_new(xfrm_acquire_msgsize(x, xp), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154 if (skb == NULL)
2155 return -ENOMEM;
2156
2157 if (build_acquire(skb, x, xt, xp, dir) < 0)
2158 BUG();
2159
Thomas Graf082a1ad2007-08-22 13:54:36 -07002160 return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_ACQUIRE, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161}
2162
2163/* User gives us xfrm_user_policy_info followed by an array of 0
2164 * or more templates.
2165 */
Venkat Yekkiralacb969f02006-07-24 23:32:20 -07002166static struct xfrm_policy *xfrm_compile_policy(struct sock *sk, int opt,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167 u8 *data, int len, int *dir)
2168{
2169 struct xfrm_userpolicy_info *p = (struct xfrm_userpolicy_info *)data;
2170 struct xfrm_user_tmpl *ut = (struct xfrm_user_tmpl *) (p + 1);
2171 struct xfrm_policy *xp;
2172 int nr;
2173
Venkat Yekkiralacb969f02006-07-24 23:32:20 -07002174 switch (sk->sk_family) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175 case AF_INET:
2176 if (opt != IP_XFRM_POLICY) {
2177 *dir = -EOPNOTSUPP;
2178 return NULL;
2179 }
2180 break;
2181#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
2182 case AF_INET6:
2183 if (opt != IPV6_XFRM_POLICY) {
2184 *dir = -EOPNOTSUPP;
2185 return NULL;
2186 }
2187 break;
2188#endif
2189 default:
2190 *dir = -EINVAL;
2191 return NULL;
2192 }
2193
2194 *dir = -EINVAL;
2195
2196 if (len < sizeof(*p) ||
2197 verify_newpolicy_info(p))
2198 return NULL;
2199
2200 nr = ((len - sizeof(*p)) / sizeof(*ut));
David S. Millerb4ad86bf2006-12-03 19:19:26 -08002201 if (validate_tmpl(nr, ut, p->sel.family))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202 return NULL;
2203
Herbert Xua4f1bac2005-07-26 15:43:17 -07002204 if (p->dir > XFRM_POLICY_OUT)
2205 return NULL;
2206
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207 xp = xfrm_policy_alloc(GFP_KERNEL);
2208 if (xp == NULL) {
2209 *dir = -ENOBUFS;
2210 return NULL;
2211 }
2212
2213 copy_from_user_policy(xp, p);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07002214 xp->type = XFRM_POLICY_TYPE_MAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215 copy_templates(xp, ut, nr);
2216
2217 *dir = p->dir;
2218
2219 return xp;
2220}
2221
Thomas Graf7deb2262007-08-22 13:57:39 -07002222static inline size_t xfrm_polexpire_msgsize(struct xfrm_policy *xp)
2223{
2224 return NLMSG_ALIGN(sizeof(struct xfrm_user_polexpire))
2225 + nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr)
2226 + nla_total_size(xfrm_user_sec_ctx_size(xp->security))
2227 + userpolicy_type_attrsize();
2228}
2229
Linus Torvalds1da177e2005-04-16 15:20:36 -07002230static int build_polexpire(struct sk_buff *skb, struct xfrm_policy *xp,
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002231 int dir, struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002232{
2233 struct xfrm_user_polexpire *upe;
2234 struct nlmsghdr *nlh;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002235 int hard = c->data.hard;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002237 nlh = nlmsg_put(skb, c->pid, 0, XFRM_MSG_POLEXPIRE, sizeof(*upe), 0);
2238 if (nlh == NULL)
2239 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240
Thomas Graf7b67c852007-08-22 13:53:52 -07002241 upe = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242 copy_to_user_policy(xp, &upe->pol, dir);
2243 if (copy_to_user_tmpl(xp, skb) < 0)
2244 goto nlmsg_failure;
Trent Jaegerdf718372005-12-13 23:12:27 -08002245 if (copy_to_user_sec_ctx(xp, skb))
2246 goto nlmsg_failure;
Jamal Hadi Salim1459bb32006-11-20 16:51:22 -08002247 if (copy_to_user_policy_type(xp->type, skb) < 0)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07002248 goto nlmsg_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002249 upe->hard = !!hard;
2250
Thomas Graf98250692007-08-22 12:47:26 -07002251 return nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252
2253nlmsg_failure:
Thomas Graf98250692007-08-22 12:47:26 -07002254 nlmsg_cancel(skb, nlh);
2255 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256}
2257
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002258static int xfrm_exp_policy_notify(struct xfrm_policy *xp, int dir, struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002259{
2260 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002261
Thomas Graf7deb2262007-08-22 13:57:39 -07002262 skb = nlmsg_new(xfrm_polexpire_msgsize(xp), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002263 if (skb == NULL)
2264 return -ENOMEM;
2265
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002266 if (build_polexpire(skb, xp, dir, c) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267 BUG();
2268
Thomas Graf082a1ad2007-08-22 13:54:36 -07002269 return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_EXPIRE, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002270}
2271
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002272static int xfrm_notify_policy(struct xfrm_policy *xp, int dir, struct km_event *c)
2273{
2274 struct xfrm_userpolicy_info *p;
Herbert Xu0603eac2005-06-18 22:54:36 -07002275 struct xfrm_userpolicy_id *id;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002276 struct nlmsghdr *nlh;
2277 struct sk_buff *skb;
Thomas Graf7deb2262007-08-22 13:57:39 -07002278 int len = nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr);
Herbert Xu0603eac2005-06-18 22:54:36 -07002279 int headlen;
2280
2281 headlen = sizeof(*p);
2282 if (c->event == XFRM_MSG_DELPOLICY) {
Thomas Graf7deb2262007-08-22 13:57:39 -07002283 len += nla_total_size(headlen);
Herbert Xu0603eac2005-06-18 22:54:36 -07002284 headlen = sizeof(*id);
2285 }
Thomas Grafcfbfd452007-08-22 13:57:04 -07002286 len += userpolicy_type_attrsize();
Thomas Graf7deb2262007-08-22 13:57:39 -07002287 len += NLMSG_ALIGN(headlen);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002288
Thomas Graf7deb2262007-08-22 13:57:39 -07002289 skb = nlmsg_new(len, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002290 if (skb == NULL)
2291 return -ENOMEM;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002292
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002293 nlh = nlmsg_put(skb, c->pid, c->seq, c->event, headlen, 0);
2294 if (nlh == NULL)
2295 goto nlmsg_failure;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002296
Thomas Graf7b67c852007-08-22 13:53:52 -07002297 p = nlmsg_data(nlh);
Herbert Xu0603eac2005-06-18 22:54:36 -07002298 if (c->event == XFRM_MSG_DELPOLICY) {
Thomas Grafc0144be2007-08-22 13:55:43 -07002299 struct nlattr *attr;
2300
Thomas Graf7b67c852007-08-22 13:53:52 -07002301 id = nlmsg_data(nlh);
Herbert Xu0603eac2005-06-18 22:54:36 -07002302 memset(id, 0, sizeof(*id));
2303 id->dir = dir;
2304 if (c->data.byid)
2305 id->index = xp->index;
2306 else
2307 memcpy(&id->sel, &xp->selector, sizeof(id->sel));
2308
Thomas Grafc0144be2007-08-22 13:55:43 -07002309 attr = nla_reserve(skb, XFRMA_POLICY, sizeof(*p));
2310 if (attr == NULL)
2311 goto nlmsg_failure;
2312
2313 p = nla_data(attr);
Herbert Xu0603eac2005-06-18 22:54:36 -07002314 }
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002315
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002316 copy_to_user_policy(xp, p, dir);
2317 if (copy_to_user_tmpl(xp, skb) < 0)
2318 goto nlmsg_failure;
Jamal Hadi Salim1459bb32006-11-20 16:51:22 -08002319 if (copy_to_user_policy_type(xp->type, skb) < 0)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07002320 goto nlmsg_failure;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002321
Thomas Graf98250692007-08-22 12:47:26 -07002322 nlmsg_end(skb, nlh);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002323
Thomas Graf082a1ad2007-08-22 13:54:36 -07002324 return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_POLICY, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002325
2326nlmsg_failure:
2327 kfree_skb(skb);
2328 return -1;
2329}
2330
2331static int xfrm_notify_policy_flush(struct km_event *c)
2332{
2333 struct nlmsghdr *nlh;
2334 struct sk_buff *skb;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002335
Thomas Graf7deb2262007-08-22 13:57:39 -07002336 skb = nlmsg_new(userpolicy_type_attrsize(), GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002337 if (skb == NULL)
2338 return -ENOMEM;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002339
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002340 nlh = nlmsg_put(skb, c->pid, c->seq, XFRM_MSG_FLUSHPOLICY, 0, 0);
2341 if (nlh == NULL)
2342 goto nlmsg_failure;
Jamal Hadi Salim0c51f532006-11-27 12:58:20 -08002343 if (copy_to_user_policy_type(c->data.type, skb) < 0)
2344 goto nlmsg_failure;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002345
Thomas Graf98250692007-08-22 12:47:26 -07002346 nlmsg_end(skb, nlh);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002347
Thomas Graf082a1ad2007-08-22 13:54:36 -07002348 return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_POLICY, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002349
2350nlmsg_failure:
2351 kfree_skb(skb);
2352 return -1;
2353}
2354
2355static int xfrm_send_policy_notify(struct xfrm_policy *xp, int dir, struct km_event *c)
2356{
2357
2358 switch (c->event) {
Herbert Xuf60f6b82005-06-18 22:44:37 -07002359 case XFRM_MSG_NEWPOLICY:
2360 case XFRM_MSG_UPDPOLICY:
2361 case XFRM_MSG_DELPOLICY:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002362 return xfrm_notify_policy(xp, dir, c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07002363 case XFRM_MSG_FLUSHPOLICY:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002364 return xfrm_notify_policy_flush(c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07002365 case XFRM_MSG_POLEXPIRE:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002366 return xfrm_exp_policy_notify(xp, dir, c);
2367 default:
2368 printk("xfrm_user: Unknown Policy event %d\n", c->event);
2369 }
2370
2371 return 0;
2372
2373}
2374
Thomas Graf7deb2262007-08-22 13:57:39 -07002375static inline size_t xfrm_report_msgsize(void)
2376{
2377 return NLMSG_ALIGN(sizeof(struct xfrm_user_report));
2378}
2379
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002380static int build_report(struct sk_buff *skb, u8 proto,
2381 struct xfrm_selector *sel, xfrm_address_t *addr)
2382{
2383 struct xfrm_user_report *ur;
2384 struct nlmsghdr *nlh;
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002385
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002386 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_REPORT, sizeof(*ur), 0);
2387 if (nlh == NULL)
2388 return -EMSGSIZE;
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002389
Thomas Graf7b67c852007-08-22 13:53:52 -07002390 ur = nlmsg_data(nlh);
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002391 ur->proto = proto;
2392 memcpy(&ur->sel, sel, sizeof(ur->sel));
2393
2394 if (addr)
Thomas Grafc0144be2007-08-22 13:55:43 -07002395 NLA_PUT(skb, XFRMA_COADDR, sizeof(*addr), addr);
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002396
Thomas Graf98250692007-08-22 12:47:26 -07002397 return nlmsg_end(skb, nlh);
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002398
Thomas Grafc0144be2007-08-22 13:55:43 -07002399nla_put_failure:
Thomas Graf98250692007-08-22 12:47:26 -07002400 nlmsg_cancel(skb, nlh);
2401 return -EMSGSIZE;
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002402}
2403
2404static int xfrm_send_report(u8 proto, struct xfrm_selector *sel,
2405 xfrm_address_t *addr)
2406{
2407 struct sk_buff *skb;
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002408
Thomas Graf7deb2262007-08-22 13:57:39 -07002409 skb = nlmsg_new(xfrm_report_msgsize(), GFP_ATOMIC);
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002410 if (skb == NULL)
2411 return -ENOMEM;
2412
2413 if (build_report(skb, proto, sel, addr) < 0)
2414 BUG();
2415
Thomas Graf082a1ad2007-08-22 13:54:36 -07002416 return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_REPORT, GFP_ATOMIC);
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002417}
2418
Linus Torvalds1da177e2005-04-16 15:20:36 -07002419static struct xfrm_mgr netlink_mgr = {
2420 .id = "netlink",
2421 .notify = xfrm_send_state_notify,
2422 .acquire = xfrm_send_acquire,
2423 .compile_policy = xfrm_compile_policy,
2424 .notify_policy = xfrm_send_policy_notify,
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002425 .report = xfrm_send_report,
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002426 .migrate = xfrm_send_migrate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002427};
2428
2429static int __init xfrm_user_init(void)
2430{
Patrick McHardybe336902006-03-20 22:40:54 -08002431 struct sock *nlsk;
2432
Masahide NAKAMURA654b32c2006-08-23 19:12:56 -07002433 printk(KERN_INFO "Initializing XFRM netlink socket\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002434
Patrick McHardybe336902006-03-20 22:40:54 -08002435 nlsk = netlink_kernel_create(NETLINK_XFRM, XFRMNLGRP_MAX,
Patrick McHardyaf65bdf2007-04-20 14:14:21 -07002436 xfrm_netlink_rcv, NULL, THIS_MODULE);
Patrick McHardybe336902006-03-20 22:40:54 -08002437 if (nlsk == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002438 return -ENOMEM;
Patrick McHardybe336902006-03-20 22:40:54 -08002439 rcu_assign_pointer(xfrm_nl, nlsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002440
2441 xfrm_register_km(&netlink_mgr);
2442
2443 return 0;
2444}
2445
2446static void __exit xfrm_user_exit(void)
2447{
Patrick McHardybe336902006-03-20 22:40:54 -08002448 struct sock *nlsk = xfrm_nl;
2449
Linus Torvalds1da177e2005-04-16 15:20:36 -07002450 xfrm_unregister_km(&netlink_mgr);
Patrick McHardybe336902006-03-20 22:40:54 -08002451 rcu_assign_pointer(xfrm_nl, NULL);
2452 synchronize_rcu();
2453 sock_release(nlsk->sk_socket);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002454}
2455
2456module_init(xfrm_user_init);
2457module_exit(xfrm_user_exit);
2458MODULE_LICENSE("GPL");
Harald Welte4fdb3bb2005-08-09 19:40:55 -07002459MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK, NETLINK_XFRM);
Jamal Hadi Salimf8cd5482006-03-20 19:15:11 -08002460