xref: /linux/net/xfrm/xfrm_user.c (revision 4447bb33f09444920a8f1d89e1540137429351b6)
11da177e4SLinus Torvalds /* xfrm_user.c: User interface to configure xfrm engine.
21da177e4SLinus Torvalds  *
31da177e4SLinus Torvalds  * Copyright (C) 2002 David S. Miller (davem@redhat.com)
41da177e4SLinus Torvalds  *
51da177e4SLinus Torvalds  * Changes:
61da177e4SLinus Torvalds  *	Mitsuru KANDA @USAGI
71da177e4SLinus Torvalds  * 	Kazunori MIYAZAWA @USAGI
81da177e4SLinus Torvalds  * 	Kunihiro Ishiguro <kunihiro@ipinfusion.com>
91da177e4SLinus Torvalds  * 		IPv6 support
101da177e4SLinus Torvalds  *
111da177e4SLinus Torvalds  */
121da177e4SLinus Torvalds 
139409f38aSHerbert Xu #include <linux/crypto.h>
141da177e4SLinus Torvalds #include <linux/module.h>
151da177e4SLinus Torvalds #include <linux/kernel.h>
161da177e4SLinus Torvalds #include <linux/types.h>
171da177e4SLinus Torvalds #include <linux/slab.h>
181da177e4SLinus Torvalds #include <linux/socket.h>
191da177e4SLinus Torvalds #include <linux/string.h>
201da177e4SLinus Torvalds #include <linux/net.h>
211da177e4SLinus Torvalds #include <linux/skbuff.h>
221da177e4SLinus Torvalds #include <linux/pfkeyv2.h>
231da177e4SLinus Torvalds #include <linux/ipsec.h>
241da177e4SLinus Torvalds #include <linux/init.h>
251da177e4SLinus Torvalds #include <linux/security.h>
261da177e4SLinus Torvalds #include <net/sock.h>
271da177e4SLinus Torvalds #include <net/xfrm.h>
2888fc2c84SThomas Graf #include <net/netlink.h>
291da177e4SLinus Torvalds #include <asm/uaccess.h>
30e23c7194SMasahide NAKAMURA #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
31e23c7194SMasahide NAKAMURA #include <linux/in6.h>
32e23c7194SMasahide NAKAMURA #endif
331da177e4SLinus Torvalds 
341a6509d9SHerbert Xu static inline int aead_len(struct xfrm_algo_aead *alg)
351a6509d9SHerbert Xu {
361a6509d9SHerbert Xu 	return sizeof(*alg) + ((alg->alg_key_len + 7) / 8);
371a6509d9SHerbert Xu }
381a6509d9SHerbert Xu 
395424f32eSThomas Graf static int verify_one_alg(struct nlattr **attrs, enum xfrm_attr_type_t type)
401da177e4SLinus Torvalds {
415424f32eSThomas Graf 	struct nlattr *rt = attrs[type];
421da177e4SLinus Torvalds 	struct xfrm_algo *algp;
431da177e4SLinus Torvalds 
441da177e4SLinus Torvalds 	if (!rt)
451da177e4SLinus Torvalds 		return 0;
461da177e4SLinus Torvalds 
475424f32eSThomas Graf 	algp = nla_data(rt);
480f99be0dSEric Dumazet 	if (nla_len(rt) < xfrm_alg_len(algp))
4931c26852SHerbert Xu 		return -EINVAL;
5031c26852SHerbert Xu 
511da177e4SLinus Torvalds 	switch (type) {
521da177e4SLinus Torvalds 	case XFRMA_ALG_AUTH:
531da177e4SLinus Torvalds 	case XFRMA_ALG_CRYPT:
541da177e4SLinus Torvalds 	case XFRMA_ALG_COMP:
551da177e4SLinus Torvalds 		break;
561da177e4SLinus Torvalds 
571da177e4SLinus Torvalds 	default:
581da177e4SLinus Torvalds 		return -EINVAL;
593ff50b79SStephen Hemminger 	}
601da177e4SLinus Torvalds 
611da177e4SLinus Torvalds 	algp->alg_name[CRYPTO_MAX_ALG_NAME - 1] = '\0';
621da177e4SLinus Torvalds 	return 0;
631da177e4SLinus Torvalds }
641da177e4SLinus Torvalds 
65*4447bb33SMartin Willi static int verify_auth_trunc(struct nlattr **attrs)
66*4447bb33SMartin Willi {
67*4447bb33SMartin Willi 	struct nlattr *rt = attrs[XFRMA_ALG_AUTH_TRUNC];
68*4447bb33SMartin Willi 	struct xfrm_algo_auth *algp;
69*4447bb33SMartin Willi 
70*4447bb33SMartin Willi 	if (!rt)
71*4447bb33SMartin Willi 		return 0;
72*4447bb33SMartin Willi 
73*4447bb33SMartin Willi 	algp = nla_data(rt);
74*4447bb33SMartin Willi 	if (nla_len(rt) < xfrm_alg_auth_len(algp))
75*4447bb33SMartin Willi 		return -EINVAL;
76*4447bb33SMartin Willi 
77*4447bb33SMartin Willi 	algp->alg_name[CRYPTO_MAX_ALG_NAME - 1] = '\0';
78*4447bb33SMartin Willi 	return 0;
79*4447bb33SMartin Willi }
80*4447bb33SMartin Willi 
811a6509d9SHerbert Xu static int verify_aead(struct nlattr **attrs)
821a6509d9SHerbert Xu {
831a6509d9SHerbert Xu 	struct nlattr *rt = attrs[XFRMA_ALG_AEAD];
841a6509d9SHerbert Xu 	struct xfrm_algo_aead *algp;
851a6509d9SHerbert Xu 
861a6509d9SHerbert Xu 	if (!rt)
871a6509d9SHerbert Xu 		return 0;
881a6509d9SHerbert Xu 
891a6509d9SHerbert Xu 	algp = nla_data(rt);
901a6509d9SHerbert Xu 	if (nla_len(rt) < aead_len(algp))
911a6509d9SHerbert Xu 		return -EINVAL;
921a6509d9SHerbert Xu 
931a6509d9SHerbert Xu 	algp->alg_name[CRYPTO_MAX_ALG_NAME - 1] = '\0';
941a6509d9SHerbert Xu 	return 0;
951a6509d9SHerbert Xu }
961a6509d9SHerbert Xu 
975424f32eSThomas Graf static void verify_one_addr(struct nlattr **attrs, enum xfrm_attr_type_t type,
98eb2971b6SMasahide NAKAMURA 			   xfrm_address_t **addrp)
99eb2971b6SMasahide NAKAMURA {
1005424f32eSThomas Graf 	struct nlattr *rt = attrs[type];
101eb2971b6SMasahide NAKAMURA 
102cf5cb79fSThomas Graf 	if (rt && addrp)
1035424f32eSThomas Graf 		*addrp = nla_data(rt);
104eb2971b6SMasahide NAKAMURA }
105df71837dSTrent Jaeger 
1065424f32eSThomas Graf static inline int verify_sec_ctx_len(struct nlattr **attrs)
107df71837dSTrent Jaeger {
1085424f32eSThomas Graf 	struct nlattr *rt = attrs[XFRMA_SEC_CTX];
109df71837dSTrent Jaeger 	struct xfrm_user_sec_ctx *uctx;
110df71837dSTrent Jaeger 
111df71837dSTrent Jaeger 	if (!rt)
112df71837dSTrent Jaeger 		return 0;
113df71837dSTrent Jaeger 
1145424f32eSThomas Graf 	uctx = nla_data(rt);
115cf5cb79fSThomas Graf 	if (uctx->len != (sizeof(struct xfrm_user_sec_ctx) + uctx->ctx_len))
116df71837dSTrent Jaeger 		return -EINVAL;
117df71837dSTrent Jaeger 
118df71837dSTrent Jaeger 	return 0;
119df71837dSTrent Jaeger }
120df71837dSTrent Jaeger 
121df71837dSTrent Jaeger 
1221da177e4SLinus Torvalds static int verify_newsa_info(struct xfrm_usersa_info *p,
1235424f32eSThomas Graf 			     struct nlattr **attrs)
1241da177e4SLinus Torvalds {
1251da177e4SLinus Torvalds 	int err;
1261da177e4SLinus Torvalds 
1271da177e4SLinus Torvalds 	err = -EINVAL;
1281da177e4SLinus Torvalds 	switch (p->family) {
1291da177e4SLinus Torvalds 	case AF_INET:
1301da177e4SLinus Torvalds 		break;
1311da177e4SLinus Torvalds 
1321da177e4SLinus Torvalds 	case AF_INET6:
1331da177e4SLinus Torvalds #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1341da177e4SLinus Torvalds 		break;
1351da177e4SLinus Torvalds #else
1361da177e4SLinus Torvalds 		err = -EAFNOSUPPORT;
1371da177e4SLinus Torvalds 		goto out;
1381da177e4SLinus Torvalds #endif
1391da177e4SLinus Torvalds 
1401da177e4SLinus Torvalds 	default:
1411da177e4SLinus Torvalds 		goto out;
1423ff50b79SStephen Hemminger 	}
1431da177e4SLinus Torvalds 
1441da177e4SLinus Torvalds 	err = -EINVAL;
1451da177e4SLinus Torvalds 	switch (p->id.proto) {
1461da177e4SLinus Torvalds 	case IPPROTO_AH:
147*4447bb33SMartin Willi 		if ((!attrs[XFRMA_ALG_AUTH]	&&
148*4447bb33SMartin Willi 		     !attrs[XFRMA_ALG_AUTH_TRUNC]) ||
1491a6509d9SHerbert Xu 		    attrs[XFRMA_ALG_AEAD]	||
15035a7aa08SThomas Graf 		    attrs[XFRMA_ALG_CRYPT]	||
15135a7aa08SThomas Graf 		    attrs[XFRMA_ALG_COMP])
1521da177e4SLinus Torvalds 			goto out;
1531da177e4SLinus Torvalds 		break;
1541da177e4SLinus Torvalds 
1551da177e4SLinus Torvalds 	case IPPROTO_ESP:
1561a6509d9SHerbert Xu 		if (attrs[XFRMA_ALG_COMP])
1571a6509d9SHerbert Xu 			goto out;
1581a6509d9SHerbert Xu 		if (!attrs[XFRMA_ALG_AUTH] &&
159*4447bb33SMartin Willi 		    !attrs[XFRMA_ALG_AUTH_TRUNC] &&
1601a6509d9SHerbert Xu 		    !attrs[XFRMA_ALG_CRYPT] &&
1611a6509d9SHerbert Xu 		    !attrs[XFRMA_ALG_AEAD])
1621a6509d9SHerbert Xu 			goto out;
1631a6509d9SHerbert Xu 		if ((attrs[XFRMA_ALG_AUTH] ||
164*4447bb33SMartin Willi 		     attrs[XFRMA_ALG_AUTH_TRUNC] ||
1651a6509d9SHerbert Xu 		     attrs[XFRMA_ALG_CRYPT]) &&
1661a6509d9SHerbert Xu 		    attrs[XFRMA_ALG_AEAD])
1671da177e4SLinus Torvalds 			goto out;
1681da177e4SLinus Torvalds 		break;
1691da177e4SLinus Torvalds 
1701da177e4SLinus Torvalds 	case IPPROTO_COMP:
17135a7aa08SThomas Graf 		if (!attrs[XFRMA_ALG_COMP]	||
1721a6509d9SHerbert Xu 		    attrs[XFRMA_ALG_AEAD]	||
17335a7aa08SThomas Graf 		    attrs[XFRMA_ALG_AUTH]	||
174*4447bb33SMartin Willi 		    attrs[XFRMA_ALG_AUTH_TRUNC]	||
17535a7aa08SThomas Graf 		    attrs[XFRMA_ALG_CRYPT])
1761da177e4SLinus Torvalds 			goto out;
1771da177e4SLinus Torvalds 		break;
1781da177e4SLinus Torvalds 
179e23c7194SMasahide NAKAMURA #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
180e23c7194SMasahide NAKAMURA 	case IPPROTO_DSTOPTS:
181e23c7194SMasahide NAKAMURA 	case IPPROTO_ROUTING:
18235a7aa08SThomas Graf 		if (attrs[XFRMA_ALG_COMP]	||
18335a7aa08SThomas Graf 		    attrs[XFRMA_ALG_AUTH]	||
184*4447bb33SMartin Willi 		    attrs[XFRMA_ALG_AUTH_TRUNC]	||
1851a6509d9SHerbert Xu 		    attrs[XFRMA_ALG_AEAD]	||
18635a7aa08SThomas Graf 		    attrs[XFRMA_ALG_CRYPT]	||
18735a7aa08SThomas Graf 		    attrs[XFRMA_ENCAP]		||
18835a7aa08SThomas Graf 		    attrs[XFRMA_SEC_CTX]	||
18935a7aa08SThomas Graf 		    !attrs[XFRMA_COADDR])
190e23c7194SMasahide NAKAMURA 			goto out;
191e23c7194SMasahide NAKAMURA 		break;
192e23c7194SMasahide NAKAMURA #endif
193e23c7194SMasahide NAKAMURA 
1941da177e4SLinus Torvalds 	default:
1951da177e4SLinus Torvalds 		goto out;
1963ff50b79SStephen Hemminger 	}
1971da177e4SLinus Torvalds 
1981a6509d9SHerbert Xu 	if ((err = verify_aead(attrs)))
1991a6509d9SHerbert Xu 		goto out;
200*4447bb33SMartin Willi 	if ((err = verify_auth_trunc(attrs)))
201*4447bb33SMartin Willi 		goto out;
20235a7aa08SThomas Graf 	if ((err = verify_one_alg(attrs, XFRMA_ALG_AUTH)))
2031da177e4SLinus Torvalds 		goto out;
20435a7aa08SThomas Graf 	if ((err = verify_one_alg(attrs, XFRMA_ALG_CRYPT)))
2051da177e4SLinus Torvalds 		goto out;
20635a7aa08SThomas Graf 	if ((err = verify_one_alg(attrs, XFRMA_ALG_COMP)))
2071da177e4SLinus Torvalds 		goto out;
20835a7aa08SThomas Graf 	if ((err = verify_sec_ctx_len(attrs)))
209df71837dSTrent Jaeger 		goto out;
2101da177e4SLinus Torvalds 
2111da177e4SLinus Torvalds 	err = -EINVAL;
2121da177e4SLinus Torvalds 	switch (p->mode) {
2137e49e6deSMasahide NAKAMURA 	case XFRM_MODE_TRANSPORT:
2147e49e6deSMasahide NAKAMURA 	case XFRM_MODE_TUNNEL:
215060f02a3SNoriaki TAKAMIYA 	case XFRM_MODE_ROUTEOPTIMIZATION:
2160a69452cSDiego Beltrami 	case XFRM_MODE_BEET:
2171da177e4SLinus Torvalds 		break;
2181da177e4SLinus Torvalds 
2191da177e4SLinus Torvalds 	default:
2201da177e4SLinus Torvalds 		goto out;
2213ff50b79SStephen Hemminger 	}
2221da177e4SLinus Torvalds 
2231da177e4SLinus Torvalds 	err = 0;
2241da177e4SLinus Torvalds 
2251da177e4SLinus Torvalds out:
2261da177e4SLinus Torvalds 	return err;
2271da177e4SLinus Torvalds }
2281da177e4SLinus Torvalds 
2291da177e4SLinus Torvalds static int attach_one_algo(struct xfrm_algo **algpp, u8 *props,
2301da177e4SLinus Torvalds 			   struct xfrm_algo_desc *(*get_byname)(char *, int),
2315424f32eSThomas Graf 			   struct nlattr *rta)
2321da177e4SLinus Torvalds {
2331da177e4SLinus Torvalds 	struct xfrm_algo *p, *ualg;
2341da177e4SLinus Torvalds 	struct xfrm_algo_desc *algo;
2351da177e4SLinus Torvalds 
2361da177e4SLinus Torvalds 	if (!rta)
2371da177e4SLinus Torvalds 		return 0;
2381da177e4SLinus Torvalds 
2395424f32eSThomas Graf 	ualg = nla_data(rta);
2401da177e4SLinus Torvalds 
2411da177e4SLinus Torvalds 	algo = get_byname(ualg->alg_name, 1);
2421da177e4SLinus Torvalds 	if (!algo)
2431da177e4SLinus Torvalds 		return -ENOSYS;
2441da177e4SLinus Torvalds 	*props = algo->desc.sadb_alg_id;
2451da177e4SLinus Torvalds 
2460f99be0dSEric Dumazet 	p = kmemdup(ualg, xfrm_alg_len(ualg), GFP_KERNEL);
2471da177e4SLinus Torvalds 	if (!p)
2481da177e4SLinus Torvalds 		return -ENOMEM;
2491da177e4SLinus Torvalds 
25004ff1260SHerbert Xu 	strcpy(p->alg_name, algo->name);
2511da177e4SLinus Torvalds 	*algpp = p;
2521da177e4SLinus Torvalds 	return 0;
2531da177e4SLinus Torvalds }
2541da177e4SLinus Torvalds 
255*4447bb33SMartin Willi static int attach_auth(struct xfrm_algo_auth **algpp, u8 *props,
256*4447bb33SMartin Willi 		       struct nlattr *rta)
257*4447bb33SMartin Willi {
258*4447bb33SMartin Willi 	struct xfrm_algo *ualg;
259*4447bb33SMartin Willi 	struct xfrm_algo_auth *p;
260*4447bb33SMartin Willi 	struct xfrm_algo_desc *algo;
261*4447bb33SMartin Willi 
262*4447bb33SMartin Willi 	if (!rta)
263*4447bb33SMartin Willi 		return 0;
264*4447bb33SMartin Willi 
265*4447bb33SMartin Willi 	ualg = nla_data(rta);
266*4447bb33SMartin Willi 
267*4447bb33SMartin Willi 	algo = xfrm_aalg_get_byname(ualg->alg_name, 1);
268*4447bb33SMartin Willi 	if (!algo)
269*4447bb33SMartin Willi 		return -ENOSYS;
270*4447bb33SMartin Willi 	*props = algo->desc.sadb_alg_id;
271*4447bb33SMartin Willi 
272*4447bb33SMartin Willi 	p = kmalloc(sizeof(*p) + (ualg->alg_key_len + 7) / 8, GFP_KERNEL);
273*4447bb33SMartin Willi 	if (!p)
274*4447bb33SMartin Willi 		return -ENOMEM;
275*4447bb33SMartin Willi 
276*4447bb33SMartin Willi 	strcpy(p->alg_name, algo->name);
277*4447bb33SMartin Willi 	p->alg_key_len = ualg->alg_key_len;
278*4447bb33SMartin Willi 	p->alg_trunc_len = algo->uinfo.auth.icv_truncbits;
279*4447bb33SMartin Willi 	memcpy(p->alg_key, ualg->alg_key, (ualg->alg_key_len + 7) / 8);
280*4447bb33SMartin Willi 
281*4447bb33SMartin Willi 	*algpp = p;
282*4447bb33SMartin Willi 	return 0;
283*4447bb33SMartin Willi }
284*4447bb33SMartin Willi 
285*4447bb33SMartin Willi static int attach_auth_trunc(struct xfrm_algo_auth **algpp, u8 *props,
286*4447bb33SMartin Willi 			     struct nlattr *rta)
287*4447bb33SMartin Willi {
288*4447bb33SMartin Willi 	struct xfrm_algo_auth *p, *ualg;
289*4447bb33SMartin Willi 	struct xfrm_algo_desc *algo;
290*4447bb33SMartin Willi 
291*4447bb33SMartin Willi 	if (!rta)
292*4447bb33SMartin Willi 		return 0;
293*4447bb33SMartin Willi 
294*4447bb33SMartin Willi 	ualg = nla_data(rta);
295*4447bb33SMartin Willi 
296*4447bb33SMartin Willi 	algo = xfrm_aalg_get_byname(ualg->alg_name, 1);
297*4447bb33SMartin Willi 	if (!algo)
298*4447bb33SMartin Willi 		return -ENOSYS;
299*4447bb33SMartin Willi 	if (ualg->alg_trunc_len > algo->uinfo.auth.icv_fullbits)
300*4447bb33SMartin Willi 		return -EINVAL;
301*4447bb33SMartin Willi 	*props = algo->desc.sadb_alg_id;
302*4447bb33SMartin Willi 
303*4447bb33SMartin Willi 	p = kmemdup(ualg, xfrm_alg_auth_len(ualg), GFP_KERNEL);
304*4447bb33SMartin Willi 	if (!p)
305*4447bb33SMartin Willi 		return -ENOMEM;
306*4447bb33SMartin Willi 
307*4447bb33SMartin Willi 	strcpy(p->alg_name, algo->name);
308*4447bb33SMartin Willi 	if (!p->alg_trunc_len)
309*4447bb33SMartin Willi 		p->alg_trunc_len = algo->uinfo.auth.icv_truncbits;
310*4447bb33SMartin Willi 
311*4447bb33SMartin Willi 	*algpp = p;
312*4447bb33SMartin Willi 	return 0;
313*4447bb33SMartin Willi }
314*4447bb33SMartin Willi 
3151a6509d9SHerbert Xu static int attach_aead(struct xfrm_algo_aead **algpp, u8 *props,
3161a6509d9SHerbert Xu 		       struct nlattr *rta)
3171a6509d9SHerbert Xu {
3181a6509d9SHerbert Xu 	struct xfrm_algo_aead *p, *ualg;
3191a6509d9SHerbert Xu 	struct xfrm_algo_desc *algo;
3201a6509d9SHerbert Xu 
3211a6509d9SHerbert Xu 	if (!rta)
3221a6509d9SHerbert Xu 		return 0;
3231a6509d9SHerbert Xu 
3241a6509d9SHerbert Xu 	ualg = nla_data(rta);
3251a6509d9SHerbert Xu 
3261a6509d9SHerbert Xu 	algo = xfrm_aead_get_byname(ualg->alg_name, ualg->alg_icv_len, 1);
3271a6509d9SHerbert Xu 	if (!algo)
3281a6509d9SHerbert Xu 		return -ENOSYS;
3291a6509d9SHerbert Xu 	*props = algo->desc.sadb_alg_id;
3301a6509d9SHerbert Xu 
3311a6509d9SHerbert Xu 	p = kmemdup(ualg, aead_len(ualg), GFP_KERNEL);
3321a6509d9SHerbert Xu 	if (!p)
3331a6509d9SHerbert Xu 		return -ENOMEM;
3341a6509d9SHerbert Xu 
3351a6509d9SHerbert Xu 	strcpy(p->alg_name, algo->name);
3361a6509d9SHerbert Xu 	*algpp = p;
3371a6509d9SHerbert Xu 	return 0;
3381a6509d9SHerbert Xu }
3391a6509d9SHerbert Xu 
340661697f7SJoy Latten static inline int xfrm_user_sec_ctx_size(struct xfrm_sec_ctx *xfrm_ctx)
341df71837dSTrent Jaeger {
342df71837dSTrent Jaeger 	int len = 0;
343df71837dSTrent Jaeger 
344df71837dSTrent Jaeger 	if (xfrm_ctx) {
345df71837dSTrent Jaeger 		len += sizeof(struct xfrm_user_sec_ctx);
346df71837dSTrent Jaeger 		len += xfrm_ctx->ctx_len;
347df71837dSTrent Jaeger 	}
348df71837dSTrent Jaeger 	return len;
349df71837dSTrent Jaeger }
350df71837dSTrent Jaeger 
3511da177e4SLinus Torvalds static void copy_from_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p)
3521da177e4SLinus Torvalds {
3531da177e4SLinus Torvalds 	memcpy(&x->id, &p->id, sizeof(x->id));
3541da177e4SLinus Torvalds 	memcpy(&x->sel, &p->sel, sizeof(x->sel));
3551da177e4SLinus Torvalds 	memcpy(&x->lft, &p->lft, sizeof(x->lft));
3561da177e4SLinus Torvalds 	x->props.mode = p->mode;
3571da177e4SLinus Torvalds 	x->props.replay_window = p->replay_window;
3581da177e4SLinus Torvalds 	x->props.reqid = p->reqid;
3591da177e4SLinus Torvalds 	x->props.family = p->family;
36054489c14SDavid S. Miller 	memcpy(&x->props.saddr, &p->saddr, sizeof(x->props.saddr));
3611da177e4SLinus Torvalds 	x->props.flags = p->flags;
362196b0036SHerbert Xu 
363ccf9b3b8SSteffen Klassert 	if (!x->sel.family && !(p->flags & XFRM_STATE_AF_UNSPEC))
364196b0036SHerbert Xu 		x->sel.family = p->family;
3651da177e4SLinus Torvalds }
3661da177e4SLinus Torvalds 
367d51d081dSJamal Hadi Salim /*
368d51d081dSJamal Hadi Salim  * someday when pfkey also has support, we could have the code
369d51d081dSJamal Hadi Salim  * somehow made shareable and move it to xfrm_state.c - JHS
370d51d081dSJamal Hadi Salim  *
371d51d081dSJamal Hadi Salim */
3725424f32eSThomas Graf static void xfrm_update_ae_params(struct xfrm_state *x, struct nlattr **attrs)
373d51d081dSJamal Hadi Salim {
3745424f32eSThomas Graf 	struct nlattr *rp = attrs[XFRMA_REPLAY_VAL];
3755424f32eSThomas Graf 	struct nlattr *lt = attrs[XFRMA_LTIME_VAL];
3765424f32eSThomas Graf 	struct nlattr *et = attrs[XFRMA_ETIMER_THRESH];
3775424f32eSThomas Graf 	struct nlattr *rt = attrs[XFRMA_REPLAY_THRESH];
378d51d081dSJamal Hadi Salim 
379d51d081dSJamal Hadi Salim 	if (rp) {
380d51d081dSJamal Hadi Salim 		struct xfrm_replay_state *replay;
3815424f32eSThomas Graf 		replay = nla_data(rp);
382d51d081dSJamal Hadi Salim 		memcpy(&x->replay, replay, sizeof(*replay));
383d51d081dSJamal Hadi Salim 		memcpy(&x->preplay, replay, sizeof(*replay));
384d51d081dSJamal Hadi Salim 	}
385d51d081dSJamal Hadi Salim 
386d51d081dSJamal Hadi Salim 	if (lt) {
387d51d081dSJamal Hadi Salim 		struct xfrm_lifetime_cur *ltime;
3885424f32eSThomas Graf 		ltime = nla_data(lt);
389d51d081dSJamal Hadi Salim 		x->curlft.bytes = ltime->bytes;
390d51d081dSJamal Hadi Salim 		x->curlft.packets = ltime->packets;
391d51d081dSJamal Hadi Salim 		x->curlft.add_time = ltime->add_time;
392d51d081dSJamal Hadi Salim 		x->curlft.use_time = ltime->use_time;
393d51d081dSJamal Hadi Salim 	}
394d51d081dSJamal Hadi Salim 
395cf5cb79fSThomas Graf 	if (et)
3965424f32eSThomas Graf 		x->replay_maxage = nla_get_u32(et);
397d51d081dSJamal Hadi Salim 
398cf5cb79fSThomas Graf 	if (rt)
3995424f32eSThomas Graf 		x->replay_maxdiff = nla_get_u32(rt);
400d51d081dSJamal Hadi Salim }
401d51d081dSJamal Hadi Salim 
402fc34acd3SAlexey Dobriyan static struct xfrm_state *xfrm_state_construct(struct net *net,
403fc34acd3SAlexey Dobriyan 					       struct xfrm_usersa_info *p,
4045424f32eSThomas Graf 					       struct nlattr **attrs,
4051da177e4SLinus Torvalds 					       int *errp)
4061da177e4SLinus Torvalds {
407fc34acd3SAlexey Dobriyan 	struct xfrm_state *x = xfrm_state_alloc(net);
4081da177e4SLinus Torvalds 	int err = -ENOMEM;
4091da177e4SLinus Torvalds 
4101da177e4SLinus Torvalds 	if (!x)
4111da177e4SLinus Torvalds 		goto error_no_put;
4121da177e4SLinus Torvalds 
4131da177e4SLinus Torvalds 	copy_from_user_state(x, p);
4141da177e4SLinus Torvalds 
4151a6509d9SHerbert Xu 	if ((err = attach_aead(&x->aead, &x->props.ealgo,
4161a6509d9SHerbert Xu 			       attrs[XFRMA_ALG_AEAD])))
4171a6509d9SHerbert Xu 		goto error;
418*4447bb33SMartin Willi 	if ((err = attach_auth_trunc(&x->aalg, &x->props.aalgo,
419*4447bb33SMartin Willi 				     attrs[XFRMA_ALG_AUTH_TRUNC])))
420*4447bb33SMartin Willi 		goto error;
421*4447bb33SMartin Willi 	if (!x->props.aalgo) {
422*4447bb33SMartin Willi 		if ((err = attach_auth(&x->aalg, &x->props.aalgo,
42335a7aa08SThomas Graf 				       attrs[XFRMA_ALG_AUTH])))
4241da177e4SLinus Torvalds 			goto error;
425*4447bb33SMartin Willi 	}
4261da177e4SLinus Torvalds 	if ((err = attach_one_algo(&x->ealg, &x->props.ealgo,
4271da177e4SLinus Torvalds 				   xfrm_ealg_get_byname,
42835a7aa08SThomas Graf 				   attrs[XFRMA_ALG_CRYPT])))
4291da177e4SLinus Torvalds 		goto error;
4301da177e4SLinus Torvalds 	if ((err = attach_one_algo(&x->calg, &x->props.calgo,
4311da177e4SLinus Torvalds 				   xfrm_calg_get_byname,
43235a7aa08SThomas Graf 				   attrs[XFRMA_ALG_COMP])))
4331da177e4SLinus Torvalds 		goto error;
434fd21150aSThomas Graf 
435fd21150aSThomas Graf 	if (attrs[XFRMA_ENCAP]) {
436fd21150aSThomas Graf 		x->encap = kmemdup(nla_data(attrs[XFRMA_ENCAP]),
437fd21150aSThomas Graf 				   sizeof(*x->encap), GFP_KERNEL);
438fd21150aSThomas Graf 		if (x->encap == NULL)
4391da177e4SLinus Torvalds 			goto error;
440fd21150aSThomas Graf 	}
441fd21150aSThomas Graf 
442fd21150aSThomas Graf 	if (attrs[XFRMA_COADDR]) {
443fd21150aSThomas Graf 		x->coaddr = kmemdup(nla_data(attrs[XFRMA_COADDR]),
444fd21150aSThomas Graf 				    sizeof(*x->coaddr), GFP_KERNEL);
445fd21150aSThomas Graf 		if (x->coaddr == NULL)
446060f02a3SNoriaki TAKAMIYA 			goto error;
447fd21150aSThomas Graf 	}
448fd21150aSThomas Graf 
44972cb6962SHerbert Xu 	err = xfrm_init_state(x);
4501da177e4SLinus Torvalds 	if (err)
4511da177e4SLinus Torvalds 		goto error;
4521da177e4SLinus Torvalds 
453fd21150aSThomas Graf 	if (attrs[XFRMA_SEC_CTX] &&
454fd21150aSThomas Graf 	    security_xfrm_state_alloc(x, nla_data(attrs[XFRMA_SEC_CTX])))
455df71837dSTrent Jaeger 		goto error;
456df71837dSTrent Jaeger 
4571da177e4SLinus Torvalds 	x->km.seq = p->seq;
458b27aeadbSAlexey Dobriyan 	x->replay_maxdiff = net->xfrm.sysctl_aevent_rseqth;
459d51d081dSJamal Hadi Salim 	/* sysctl_xfrm_aevent_etime is in 100ms units */
460b27aeadbSAlexey Dobriyan 	x->replay_maxage = (net->xfrm.sysctl_aevent_etime*HZ)/XFRM_AE_ETH_M;
461d51d081dSJamal Hadi Salim 	x->preplay.bitmap = 0;
462d51d081dSJamal Hadi Salim 	x->preplay.seq = x->replay.seq+x->replay_maxdiff;
463d51d081dSJamal Hadi Salim 	x->preplay.oseq = x->replay.oseq +x->replay_maxdiff;
464d51d081dSJamal Hadi Salim 
465d51d081dSJamal Hadi Salim 	/* override default values from above */
466d51d081dSJamal Hadi Salim 
4675424f32eSThomas Graf 	xfrm_update_ae_params(x, attrs);
4681da177e4SLinus Torvalds 
4691da177e4SLinus Torvalds 	return x;
4701da177e4SLinus Torvalds 
4711da177e4SLinus Torvalds error:
4721da177e4SLinus Torvalds 	x->km.state = XFRM_STATE_DEAD;
4731da177e4SLinus Torvalds 	xfrm_state_put(x);
4741da177e4SLinus Torvalds error_no_put:
4751da177e4SLinus Torvalds 	*errp = err;
4761da177e4SLinus Torvalds 	return NULL;
4771da177e4SLinus Torvalds }
4781da177e4SLinus Torvalds 
47922e70050SChristoph Hellwig static int xfrm_add_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
4805424f32eSThomas Graf 		struct nlattr **attrs)
4811da177e4SLinus Torvalds {
482fc34acd3SAlexey Dobriyan 	struct net *net = sock_net(skb->sk);
4837b67c857SThomas Graf 	struct xfrm_usersa_info *p = nlmsg_data(nlh);
4841da177e4SLinus Torvalds 	struct xfrm_state *x;
4851da177e4SLinus Torvalds 	int err;
48626b15dadSJamal Hadi Salim 	struct km_event c;
4872532386fSEric Paris 	uid_t loginuid = NETLINK_CB(skb).loginuid;
4882532386fSEric Paris 	u32 sessionid = NETLINK_CB(skb).sessionid;
4892532386fSEric Paris 	u32 sid = NETLINK_CB(skb).sid;
4901da177e4SLinus Torvalds 
49135a7aa08SThomas Graf 	err = verify_newsa_info(p, attrs);
4921da177e4SLinus Torvalds 	if (err)
4931da177e4SLinus Torvalds 		return err;
4941da177e4SLinus Torvalds 
495fc34acd3SAlexey Dobriyan 	x = xfrm_state_construct(net, p, attrs, &err);
4961da177e4SLinus Torvalds 	if (!x)
4971da177e4SLinus Torvalds 		return err;
4981da177e4SLinus Torvalds 
49926b15dadSJamal Hadi Salim 	xfrm_state_hold(x);
5001da177e4SLinus Torvalds 	if (nlh->nlmsg_type == XFRM_MSG_NEWSA)
5011da177e4SLinus Torvalds 		err = xfrm_state_add(x);
5021da177e4SLinus Torvalds 	else
5031da177e4SLinus Torvalds 		err = xfrm_state_update(x);
5041da177e4SLinus Torvalds 
5052532386fSEric Paris 	xfrm_audit_state_add(x, err ? 0 : 1, loginuid, sessionid, sid);
506161a09e7SJoy Latten 
5071da177e4SLinus Torvalds 	if (err < 0) {
5081da177e4SLinus Torvalds 		x->km.state = XFRM_STATE_DEAD;
50921380b81SHerbert Xu 		__xfrm_state_put(x);
5107d6dfe1fSPatrick McHardy 		goto out;
5111da177e4SLinus Torvalds 	}
5121da177e4SLinus Torvalds 
51326b15dadSJamal Hadi Salim 	c.seq = nlh->nlmsg_seq;
51426b15dadSJamal Hadi Salim 	c.pid = nlh->nlmsg_pid;
515f60f6b8fSHerbert Xu 	c.event = nlh->nlmsg_type;
51626b15dadSJamal Hadi Salim 
51726b15dadSJamal Hadi Salim 	km_state_notify(x, &c);
5187d6dfe1fSPatrick McHardy out:
51926b15dadSJamal Hadi Salim 	xfrm_state_put(x);
5201da177e4SLinus Torvalds 	return err;
5211da177e4SLinus Torvalds }
5221da177e4SLinus Torvalds 
523fc34acd3SAlexey Dobriyan static struct xfrm_state *xfrm_user_state_lookup(struct net *net,
524fc34acd3SAlexey Dobriyan 						 struct xfrm_usersa_id *p,
5255424f32eSThomas Graf 						 struct nlattr **attrs,
526eb2971b6SMasahide NAKAMURA 						 int *errp)
527eb2971b6SMasahide NAKAMURA {
528eb2971b6SMasahide NAKAMURA 	struct xfrm_state *x = NULL;
529eb2971b6SMasahide NAKAMURA 	int err;
530eb2971b6SMasahide NAKAMURA 
531eb2971b6SMasahide NAKAMURA 	if (xfrm_id_proto_match(p->proto, IPSEC_PROTO_ANY)) {
532eb2971b6SMasahide NAKAMURA 		err = -ESRCH;
533fc34acd3SAlexey Dobriyan 		x = xfrm_state_lookup(net, &p->daddr, p->spi, p->proto, p->family);
534eb2971b6SMasahide NAKAMURA 	} else {
535eb2971b6SMasahide NAKAMURA 		xfrm_address_t *saddr = NULL;
536eb2971b6SMasahide NAKAMURA 
53735a7aa08SThomas Graf 		verify_one_addr(attrs, XFRMA_SRCADDR, &saddr);
538eb2971b6SMasahide NAKAMURA 		if (!saddr) {
539eb2971b6SMasahide NAKAMURA 			err = -EINVAL;
540eb2971b6SMasahide NAKAMURA 			goto out;
541eb2971b6SMasahide NAKAMURA 		}
542eb2971b6SMasahide NAKAMURA 
5439abbffeeSMasahide NAKAMURA 		err = -ESRCH;
544fc34acd3SAlexey Dobriyan 		x = xfrm_state_lookup_byaddr(net, &p->daddr, saddr,
545221df1edSAlexey Dobriyan 					     p->proto, p->family);
546eb2971b6SMasahide NAKAMURA 	}
547eb2971b6SMasahide NAKAMURA 
548eb2971b6SMasahide NAKAMURA  out:
549eb2971b6SMasahide NAKAMURA 	if (!x && errp)
550eb2971b6SMasahide NAKAMURA 		*errp = err;
551eb2971b6SMasahide NAKAMURA 	return x;
552eb2971b6SMasahide NAKAMURA }
553eb2971b6SMasahide NAKAMURA 
55422e70050SChristoph Hellwig static int xfrm_del_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
5555424f32eSThomas Graf 		struct nlattr **attrs)
5561da177e4SLinus Torvalds {
557fc34acd3SAlexey Dobriyan 	struct net *net = sock_net(skb->sk);
5581da177e4SLinus Torvalds 	struct xfrm_state *x;
559eb2971b6SMasahide NAKAMURA 	int err = -ESRCH;
56026b15dadSJamal Hadi Salim 	struct km_event c;
5617b67c857SThomas Graf 	struct xfrm_usersa_id *p = nlmsg_data(nlh);
5622532386fSEric Paris 	uid_t loginuid = NETLINK_CB(skb).loginuid;
5632532386fSEric Paris 	u32 sessionid = NETLINK_CB(skb).sessionid;
5642532386fSEric Paris 	u32 sid = NETLINK_CB(skb).sid;
5651da177e4SLinus Torvalds 
566fc34acd3SAlexey Dobriyan 	x = xfrm_user_state_lookup(net, p, attrs, &err);
5671da177e4SLinus Torvalds 	if (x == NULL)
568eb2971b6SMasahide NAKAMURA 		return err;
5691da177e4SLinus Torvalds 
5706f68dc37SDavid S. Miller 	if ((err = security_xfrm_state_delete(x)) != 0)
571c8c05a8eSCatherine Zhang 		goto out;
572c8c05a8eSCatherine Zhang 
5731da177e4SLinus Torvalds 	if (xfrm_state_kern(x)) {
574c8c05a8eSCatherine Zhang 		err = -EPERM;
575c8c05a8eSCatherine Zhang 		goto out;
5761da177e4SLinus Torvalds 	}
5771da177e4SLinus Torvalds 
57826b15dadSJamal Hadi Salim 	err = xfrm_state_delete(x);
579161a09e7SJoy Latten 
580c8c05a8eSCatherine Zhang 	if (err < 0)
581c8c05a8eSCatherine Zhang 		goto out;
58226b15dadSJamal Hadi Salim 
58326b15dadSJamal Hadi Salim 	c.seq = nlh->nlmsg_seq;
58426b15dadSJamal Hadi Salim 	c.pid = nlh->nlmsg_pid;
585f60f6b8fSHerbert Xu 	c.event = nlh->nlmsg_type;
58626b15dadSJamal Hadi Salim 	km_state_notify(x, &c);
5871da177e4SLinus Torvalds 
588c8c05a8eSCatherine Zhang out:
5892532386fSEric Paris 	xfrm_audit_state_delete(x, err ? 0 : 1, loginuid, sessionid, sid);
590c8c05a8eSCatherine Zhang 	xfrm_state_put(x);
59126b15dadSJamal Hadi Salim 	return err;
5921da177e4SLinus Torvalds }
5931da177e4SLinus Torvalds 
5941da177e4SLinus Torvalds static void copy_to_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p)
5951da177e4SLinus Torvalds {
5961da177e4SLinus Torvalds 	memcpy(&p->id, &x->id, sizeof(p->id));
5971da177e4SLinus Torvalds 	memcpy(&p->sel, &x->sel, sizeof(p->sel));
5981da177e4SLinus Torvalds 	memcpy(&p->lft, &x->lft, sizeof(p->lft));
5991da177e4SLinus Torvalds 	memcpy(&p->curlft, &x->curlft, sizeof(p->curlft));
6001da177e4SLinus Torvalds 	memcpy(&p->stats, &x->stats, sizeof(p->stats));
60154489c14SDavid S. Miller 	memcpy(&p->saddr, &x->props.saddr, sizeof(p->saddr));
6021da177e4SLinus Torvalds 	p->mode = x->props.mode;
6031da177e4SLinus Torvalds 	p->replay_window = x->props.replay_window;
6041da177e4SLinus Torvalds 	p->reqid = x->props.reqid;
6051da177e4SLinus Torvalds 	p->family = x->props.family;
6061da177e4SLinus Torvalds 	p->flags = x->props.flags;
6071da177e4SLinus Torvalds 	p->seq = x->km.seq;
6081da177e4SLinus Torvalds }
6091da177e4SLinus Torvalds 
6101da177e4SLinus Torvalds struct xfrm_dump_info {
6111da177e4SLinus Torvalds 	struct sk_buff *in_skb;
6121da177e4SLinus Torvalds 	struct sk_buff *out_skb;
6131da177e4SLinus Torvalds 	u32 nlmsg_seq;
6141da177e4SLinus Torvalds 	u16 nlmsg_flags;
6151da177e4SLinus Torvalds };
6161da177e4SLinus Torvalds 
617c0144beaSThomas Graf static int copy_sec_ctx(struct xfrm_sec_ctx *s, struct sk_buff *skb)
618c0144beaSThomas Graf {
619c0144beaSThomas Graf 	struct xfrm_user_sec_ctx *uctx;
620c0144beaSThomas Graf 	struct nlattr *attr;
62168325d3bSHerbert Xu 	int ctx_size = sizeof(*uctx) + s->ctx_len;
622c0144beaSThomas Graf 
623c0144beaSThomas Graf 	attr = nla_reserve(skb, XFRMA_SEC_CTX, ctx_size);
624c0144beaSThomas Graf 	if (attr == NULL)
625c0144beaSThomas Graf 		return -EMSGSIZE;
626c0144beaSThomas Graf 
627c0144beaSThomas Graf 	uctx = nla_data(attr);
628c0144beaSThomas Graf 	uctx->exttype = XFRMA_SEC_CTX;
629c0144beaSThomas Graf 	uctx->len = ctx_size;
630c0144beaSThomas Graf 	uctx->ctx_doi = s->ctx_doi;
631c0144beaSThomas Graf 	uctx->ctx_alg = s->ctx_alg;
632c0144beaSThomas Graf 	uctx->ctx_len = s->ctx_len;
633c0144beaSThomas Graf 	memcpy(uctx + 1, s->ctx_str, s->ctx_len);
634c0144beaSThomas Graf 
635c0144beaSThomas Graf 	return 0;
636c0144beaSThomas Graf }
637c0144beaSThomas Graf 
638*4447bb33SMartin Willi static int copy_to_user_auth(struct xfrm_algo_auth *auth, struct sk_buff *skb)
639*4447bb33SMartin Willi {
640*4447bb33SMartin Willi 	struct xfrm_algo *algo;
641*4447bb33SMartin Willi 	struct nlattr *nla;
642*4447bb33SMartin Willi 
643*4447bb33SMartin Willi 	nla = nla_reserve(skb, XFRMA_ALG_AUTH,
644*4447bb33SMartin Willi 			  sizeof(*algo) + (auth->alg_key_len + 7) / 8);
645*4447bb33SMartin Willi 	if (!nla)
646*4447bb33SMartin Willi 		return -EMSGSIZE;
647*4447bb33SMartin Willi 
648*4447bb33SMartin Willi 	algo = nla_data(nla);
649*4447bb33SMartin Willi 	strcpy(algo->alg_name, auth->alg_name);
650*4447bb33SMartin Willi 	memcpy(algo->alg_key, auth->alg_key, (auth->alg_key_len + 7) / 8);
651*4447bb33SMartin Willi 	algo->alg_key_len = auth->alg_key_len;
652*4447bb33SMartin Willi 
653*4447bb33SMartin Willi 	return 0;
654*4447bb33SMartin Willi }
655*4447bb33SMartin Willi 
65668325d3bSHerbert Xu /* Don't change this without updating xfrm_sa_len! */
65768325d3bSHerbert Xu static int copy_to_user_state_extra(struct xfrm_state *x,
65868325d3bSHerbert Xu 				    struct xfrm_usersa_info *p,
65968325d3bSHerbert Xu 				    struct sk_buff *skb)
6601da177e4SLinus Torvalds {
6611da177e4SLinus Torvalds 	copy_to_user_state(x, p);
6621da177e4SLinus Torvalds 
663050f009eSHerbert Xu 	if (x->coaddr)
664050f009eSHerbert Xu 		NLA_PUT(skb, XFRMA_COADDR, sizeof(*x->coaddr), x->coaddr);
665050f009eSHerbert Xu 
666050f009eSHerbert Xu 	if (x->lastused)
667050f009eSHerbert Xu 		NLA_PUT_U64(skb, XFRMA_LASTUSED, x->lastused);
668050f009eSHerbert Xu 
6691a6509d9SHerbert Xu 	if (x->aead)
6701a6509d9SHerbert Xu 		NLA_PUT(skb, XFRMA_ALG_AEAD, aead_len(x->aead), x->aead);
671*4447bb33SMartin Willi 	if (x->aalg) {
672*4447bb33SMartin Willi 		if (copy_to_user_auth(x->aalg, skb))
673*4447bb33SMartin Willi 			goto nla_put_failure;
674*4447bb33SMartin Willi 
675*4447bb33SMartin Willi 		NLA_PUT(skb, XFRMA_ALG_AUTH_TRUNC,
676*4447bb33SMartin Willi 			xfrm_alg_auth_len(x->aalg), x->aalg);
677*4447bb33SMartin Willi 	}
6781da177e4SLinus Torvalds 	if (x->ealg)
6790f99be0dSEric Dumazet 		NLA_PUT(skb, XFRMA_ALG_CRYPT, xfrm_alg_len(x->ealg), x->ealg);
6801da177e4SLinus Torvalds 	if (x->calg)
681c0144beaSThomas Graf 		NLA_PUT(skb, XFRMA_ALG_COMP, sizeof(*(x->calg)), x->calg);
6821da177e4SLinus Torvalds 
6831da177e4SLinus Torvalds 	if (x->encap)
684c0144beaSThomas Graf 		NLA_PUT(skb, XFRMA_ENCAP, sizeof(*x->encap), x->encap);
6851da177e4SLinus Torvalds 
686c0144beaSThomas Graf 	if (x->security && copy_sec_ctx(x->security, skb) < 0)
687c0144beaSThomas Graf 		goto nla_put_failure;
688060f02a3SNoriaki TAKAMIYA 
68968325d3bSHerbert Xu 	return 0;
69068325d3bSHerbert Xu 
69168325d3bSHerbert Xu nla_put_failure:
69268325d3bSHerbert Xu 	return -EMSGSIZE;
69368325d3bSHerbert Xu }
69468325d3bSHerbert Xu 
69568325d3bSHerbert Xu static int dump_one_state(struct xfrm_state *x, int count, void *ptr)
69668325d3bSHerbert Xu {
69768325d3bSHerbert Xu 	struct xfrm_dump_info *sp = ptr;
69868325d3bSHerbert Xu 	struct sk_buff *in_skb = sp->in_skb;
69968325d3bSHerbert Xu 	struct sk_buff *skb = sp->out_skb;
70068325d3bSHerbert Xu 	struct xfrm_usersa_info *p;
70168325d3bSHerbert Xu 	struct nlmsghdr *nlh;
70268325d3bSHerbert Xu 	int err;
70368325d3bSHerbert Xu 
70468325d3bSHerbert Xu 	nlh = nlmsg_put(skb, NETLINK_CB(in_skb).pid, sp->nlmsg_seq,
70568325d3bSHerbert Xu 			XFRM_MSG_NEWSA, sizeof(*p), sp->nlmsg_flags);
70668325d3bSHerbert Xu 	if (nlh == NULL)
70768325d3bSHerbert Xu 		return -EMSGSIZE;
70868325d3bSHerbert Xu 
70968325d3bSHerbert Xu 	p = nlmsg_data(nlh);
71068325d3bSHerbert Xu 
71168325d3bSHerbert Xu 	err = copy_to_user_state_extra(x, p, skb);
71268325d3bSHerbert Xu 	if (err)
71368325d3bSHerbert Xu 		goto nla_put_failure;
71468325d3bSHerbert Xu 
7159825069dSThomas Graf 	nlmsg_end(skb, nlh);
7161da177e4SLinus Torvalds 	return 0;
7171da177e4SLinus Torvalds 
718c0144beaSThomas Graf nla_put_failure:
7199825069dSThomas Graf 	nlmsg_cancel(skb, nlh);
72068325d3bSHerbert Xu 	return err;
7211da177e4SLinus Torvalds }
7221da177e4SLinus Torvalds 
7234c563f76STimo Teras static int xfrm_dump_sa_done(struct netlink_callback *cb)
7244c563f76STimo Teras {
7254c563f76STimo Teras 	struct xfrm_state_walk *walk = (struct xfrm_state_walk *) &cb->args[1];
7264c563f76STimo Teras 	xfrm_state_walk_done(walk);
7274c563f76STimo Teras 	return 0;
7284c563f76STimo Teras }
7294c563f76STimo Teras 
7301da177e4SLinus Torvalds static int xfrm_dump_sa(struct sk_buff *skb, struct netlink_callback *cb)
7311da177e4SLinus Torvalds {
732fc34acd3SAlexey Dobriyan 	struct net *net = sock_net(skb->sk);
7334c563f76STimo Teras 	struct xfrm_state_walk *walk = (struct xfrm_state_walk *) &cb->args[1];
7341da177e4SLinus Torvalds 	struct xfrm_dump_info info;
7351da177e4SLinus Torvalds 
7364c563f76STimo Teras 	BUILD_BUG_ON(sizeof(struct xfrm_state_walk) >
7374c563f76STimo Teras 		     sizeof(cb->args) - sizeof(cb->args[0]));
7384c563f76STimo Teras 
7391da177e4SLinus Torvalds 	info.in_skb = cb->skb;
7401da177e4SLinus Torvalds 	info.out_skb = skb;
7411da177e4SLinus Torvalds 	info.nlmsg_seq = cb->nlh->nlmsg_seq;
7421da177e4SLinus Torvalds 	info.nlmsg_flags = NLM_F_MULTI;
7434c563f76STimo Teras 
7444c563f76STimo Teras 	if (!cb->args[0]) {
7454c563f76STimo Teras 		cb->args[0] = 1;
7464c563f76STimo Teras 		xfrm_state_walk_init(walk, 0);
7474c563f76STimo Teras 	}
7484c563f76STimo Teras 
749fc34acd3SAlexey Dobriyan 	(void) xfrm_state_walk(net, walk, dump_one_state, &info);
7501da177e4SLinus Torvalds 
7511da177e4SLinus Torvalds 	return skb->len;
7521da177e4SLinus Torvalds }
7531da177e4SLinus Torvalds 
7541da177e4SLinus Torvalds static struct sk_buff *xfrm_state_netlink(struct sk_buff *in_skb,
7551da177e4SLinus Torvalds 					  struct xfrm_state *x, u32 seq)
7561da177e4SLinus Torvalds {
7571da177e4SLinus Torvalds 	struct xfrm_dump_info info;
7581da177e4SLinus Torvalds 	struct sk_buff *skb;
7591da177e4SLinus Torvalds 
7607deb2264SThomas Graf 	skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
7611da177e4SLinus Torvalds 	if (!skb)
7621da177e4SLinus Torvalds 		return ERR_PTR(-ENOMEM);
7631da177e4SLinus Torvalds 
7641da177e4SLinus Torvalds 	info.in_skb = in_skb;
7651da177e4SLinus Torvalds 	info.out_skb = skb;
7661da177e4SLinus Torvalds 	info.nlmsg_seq = seq;
7671da177e4SLinus Torvalds 	info.nlmsg_flags = 0;
7681da177e4SLinus Torvalds 
7691da177e4SLinus Torvalds 	if (dump_one_state(x, 0, &info)) {
7701da177e4SLinus Torvalds 		kfree_skb(skb);
7711da177e4SLinus Torvalds 		return NULL;
7721da177e4SLinus Torvalds 	}
7731da177e4SLinus Torvalds 
7741da177e4SLinus Torvalds 	return skb;
7751da177e4SLinus Torvalds }
7761da177e4SLinus Torvalds 
7777deb2264SThomas Graf static inline size_t xfrm_spdinfo_msgsize(void)
7787deb2264SThomas Graf {
7797deb2264SThomas Graf 	return NLMSG_ALIGN(4)
7807deb2264SThomas Graf 	       + nla_total_size(sizeof(struct xfrmu_spdinfo))
7817deb2264SThomas Graf 	       + nla_total_size(sizeof(struct xfrmu_spdhinfo));
7827deb2264SThomas Graf }
7837deb2264SThomas Graf 
784ecfd6b18SJamal Hadi Salim static int build_spdinfo(struct sk_buff *skb, u32 pid, u32 seq, u32 flags)
785ecfd6b18SJamal Hadi Salim {
7865a6d3416SJamal Hadi Salim 	struct xfrmk_spdinfo si;
7875a6d3416SJamal Hadi Salim 	struct xfrmu_spdinfo spc;
7885a6d3416SJamal Hadi Salim 	struct xfrmu_spdhinfo sph;
789ecfd6b18SJamal Hadi Salim 	struct nlmsghdr *nlh;
790ecfd6b18SJamal Hadi Salim 	u32 *f;
791ecfd6b18SJamal Hadi Salim 
792ecfd6b18SJamal Hadi Salim 	nlh = nlmsg_put(skb, pid, seq, XFRM_MSG_NEWSPDINFO, sizeof(u32), 0);
793ecfd6b18SJamal Hadi Salim 	if (nlh == NULL) /* shouldnt really happen ... */
794ecfd6b18SJamal Hadi Salim 		return -EMSGSIZE;
795ecfd6b18SJamal Hadi Salim 
796ecfd6b18SJamal Hadi Salim 	f = nlmsg_data(nlh);
797ecfd6b18SJamal Hadi Salim 	*f = flags;
798ecfd6b18SJamal Hadi Salim 	xfrm_spd_getinfo(&si);
7995a6d3416SJamal Hadi Salim 	spc.incnt = si.incnt;
8005a6d3416SJamal Hadi Salim 	spc.outcnt = si.outcnt;
8015a6d3416SJamal Hadi Salim 	spc.fwdcnt = si.fwdcnt;
8025a6d3416SJamal Hadi Salim 	spc.inscnt = si.inscnt;
8035a6d3416SJamal Hadi Salim 	spc.outscnt = si.outscnt;
8045a6d3416SJamal Hadi Salim 	spc.fwdscnt = si.fwdscnt;
8055a6d3416SJamal Hadi Salim 	sph.spdhcnt = si.spdhcnt;
8065a6d3416SJamal Hadi Salim 	sph.spdhmcnt = si.spdhmcnt;
807ecfd6b18SJamal Hadi Salim 
8085a6d3416SJamal Hadi Salim 	NLA_PUT(skb, XFRMA_SPD_INFO, sizeof(spc), &spc);
8095a6d3416SJamal Hadi Salim 	NLA_PUT(skb, XFRMA_SPD_HINFO, sizeof(sph), &sph);
810ecfd6b18SJamal Hadi Salim 
811ecfd6b18SJamal Hadi Salim 	return nlmsg_end(skb, nlh);
812ecfd6b18SJamal Hadi Salim 
813ecfd6b18SJamal Hadi Salim nla_put_failure:
814ecfd6b18SJamal Hadi Salim 	nlmsg_cancel(skb, nlh);
815ecfd6b18SJamal Hadi Salim 	return -EMSGSIZE;
816ecfd6b18SJamal Hadi Salim }
817ecfd6b18SJamal Hadi Salim 
818ecfd6b18SJamal Hadi Salim static int xfrm_get_spdinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
8195424f32eSThomas Graf 		struct nlattr **attrs)
820ecfd6b18SJamal Hadi Salim {
821a6483b79SAlexey Dobriyan 	struct net *net = sock_net(skb->sk);
822ecfd6b18SJamal Hadi Salim 	struct sk_buff *r_skb;
8237b67c857SThomas Graf 	u32 *flags = nlmsg_data(nlh);
824ecfd6b18SJamal Hadi Salim 	u32 spid = NETLINK_CB(skb).pid;
825ecfd6b18SJamal Hadi Salim 	u32 seq = nlh->nlmsg_seq;
826ecfd6b18SJamal Hadi Salim 
8277deb2264SThomas Graf 	r_skb = nlmsg_new(xfrm_spdinfo_msgsize(), GFP_ATOMIC);
828ecfd6b18SJamal Hadi Salim 	if (r_skb == NULL)
829ecfd6b18SJamal Hadi Salim 		return -ENOMEM;
830ecfd6b18SJamal Hadi Salim 
831ecfd6b18SJamal Hadi Salim 	if (build_spdinfo(r_skb, spid, seq, *flags) < 0)
832ecfd6b18SJamal Hadi Salim 		BUG();
833ecfd6b18SJamal Hadi Salim 
834a6483b79SAlexey Dobriyan 	return nlmsg_unicast(net->xfrm.nlsk, r_skb, spid);
835ecfd6b18SJamal Hadi Salim }
836ecfd6b18SJamal Hadi Salim 
8377deb2264SThomas Graf static inline size_t xfrm_sadinfo_msgsize(void)
8387deb2264SThomas Graf {
8397deb2264SThomas Graf 	return NLMSG_ALIGN(4)
8407deb2264SThomas Graf 	       + nla_total_size(sizeof(struct xfrmu_sadhinfo))
8417deb2264SThomas Graf 	       + nla_total_size(4); /* XFRMA_SAD_CNT */
8427deb2264SThomas Graf }
8437deb2264SThomas Graf 
84428d8909bSJamal Hadi Salim static int build_sadinfo(struct sk_buff *skb, u32 pid, u32 seq, u32 flags)
84528d8909bSJamal Hadi Salim {
846af11e316SJamal Hadi Salim 	struct xfrmk_sadinfo si;
847af11e316SJamal Hadi Salim 	struct xfrmu_sadhinfo sh;
84828d8909bSJamal Hadi Salim 	struct nlmsghdr *nlh;
84928d8909bSJamal Hadi Salim 	u32 *f;
85028d8909bSJamal Hadi Salim 
85128d8909bSJamal Hadi Salim 	nlh = nlmsg_put(skb, pid, seq, XFRM_MSG_NEWSADINFO, sizeof(u32), 0);
85228d8909bSJamal Hadi Salim 	if (nlh == NULL) /* shouldnt really happen ... */
85328d8909bSJamal Hadi Salim 		return -EMSGSIZE;
85428d8909bSJamal Hadi Salim 
85528d8909bSJamal Hadi Salim 	f = nlmsg_data(nlh);
85628d8909bSJamal Hadi Salim 	*f = flags;
85728d8909bSJamal Hadi Salim 	xfrm_sad_getinfo(&si);
85828d8909bSJamal Hadi Salim 
859af11e316SJamal Hadi Salim 	sh.sadhmcnt = si.sadhmcnt;
860af11e316SJamal Hadi Salim 	sh.sadhcnt = si.sadhcnt;
861af11e316SJamal Hadi Salim 
862af11e316SJamal Hadi Salim 	NLA_PUT_U32(skb, XFRMA_SAD_CNT, si.sadcnt);
863af11e316SJamal Hadi Salim 	NLA_PUT(skb, XFRMA_SAD_HINFO, sizeof(sh), &sh);
86428d8909bSJamal Hadi Salim 
86528d8909bSJamal Hadi Salim 	return nlmsg_end(skb, nlh);
86628d8909bSJamal Hadi Salim 
86728d8909bSJamal Hadi Salim nla_put_failure:
86828d8909bSJamal Hadi Salim 	nlmsg_cancel(skb, nlh);
86928d8909bSJamal Hadi Salim 	return -EMSGSIZE;
87028d8909bSJamal Hadi Salim }
87128d8909bSJamal Hadi Salim 
87228d8909bSJamal Hadi Salim static int xfrm_get_sadinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
8735424f32eSThomas Graf 		struct nlattr **attrs)
87428d8909bSJamal Hadi Salim {
875a6483b79SAlexey Dobriyan 	struct net *net = sock_net(skb->sk);
87628d8909bSJamal Hadi Salim 	struct sk_buff *r_skb;
8777b67c857SThomas Graf 	u32 *flags = nlmsg_data(nlh);
87828d8909bSJamal Hadi Salim 	u32 spid = NETLINK_CB(skb).pid;
87928d8909bSJamal Hadi Salim 	u32 seq = nlh->nlmsg_seq;
88028d8909bSJamal Hadi Salim 
8817deb2264SThomas Graf 	r_skb = nlmsg_new(xfrm_sadinfo_msgsize(), GFP_ATOMIC);
88228d8909bSJamal Hadi Salim 	if (r_skb == NULL)
88328d8909bSJamal Hadi Salim 		return -ENOMEM;
88428d8909bSJamal Hadi Salim 
88528d8909bSJamal Hadi Salim 	if (build_sadinfo(r_skb, spid, seq, *flags) < 0)
88628d8909bSJamal Hadi Salim 		BUG();
88728d8909bSJamal Hadi Salim 
888a6483b79SAlexey Dobriyan 	return nlmsg_unicast(net->xfrm.nlsk, r_skb, spid);
88928d8909bSJamal Hadi Salim }
89028d8909bSJamal Hadi Salim 
89122e70050SChristoph Hellwig static int xfrm_get_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
8925424f32eSThomas Graf 		struct nlattr **attrs)
8931da177e4SLinus Torvalds {
894fc34acd3SAlexey Dobriyan 	struct net *net = sock_net(skb->sk);
8957b67c857SThomas Graf 	struct xfrm_usersa_id *p = nlmsg_data(nlh);
8961da177e4SLinus Torvalds 	struct xfrm_state *x;
8971da177e4SLinus Torvalds 	struct sk_buff *resp_skb;
898eb2971b6SMasahide NAKAMURA 	int err = -ESRCH;
8991da177e4SLinus Torvalds 
900fc34acd3SAlexey Dobriyan 	x = xfrm_user_state_lookup(net, p, attrs, &err);
9011da177e4SLinus Torvalds 	if (x == NULL)
9021da177e4SLinus Torvalds 		goto out_noput;
9031da177e4SLinus Torvalds 
9041da177e4SLinus Torvalds 	resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
9051da177e4SLinus Torvalds 	if (IS_ERR(resp_skb)) {
9061da177e4SLinus Torvalds 		err = PTR_ERR(resp_skb);
9071da177e4SLinus Torvalds 	} else {
908a6483b79SAlexey Dobriyan 		err = nlmsg_unicast(net->xfrm.nlsk, resp_skb, NETLINK_CB(skb).pid);
9091da177e4SLinus Torvalds 	}
9101da177e4SLinus Torvalds 	xfrm_state_put(x);
9111da177e4SLinus Torvalds out_noput:
9121da177e4SLinus Torvalds 	return err;
9131da177e4SLinus Torvalds }
9141da177e4SLinus Torvalds 
9151da177e4SLinus Torvalds static int verify_userspi_info(struct xfrm_userspi_info *p)
9161da177e4SLinus Torvalds {
9171da177e4SLinus Torvalds 	switch (p->info.id.proto) {
9181da177e4SLinus Torvalds 	case IPPROTO_AH:
9191da177e4SLinus Torvalds 	case IPPROTO_ESP:
9201da177e4SLinus Torvalds 		break;
9211da177e4SLinus Torvalds 
9221da177e4SLinus Torvalds 	case IPPROTO_COMP:
9231da177e4SLinus Torvalds 		/* IPCOMP spi is 16-bits. */
9241da177e4SLinus Torvalds 		if (p->max >= 0x10000)
9251da177e4SLinus Torvalds 			return -EINVAL;
9261da177e4SLinus Torvalds 		break;
9271da177e4SLinus Torvalds 
9281da177e4SLinus Torvalds 	default:
9291da177e4SLinus Torvalds 		return -EINVAL;
9303ff50b79SStephen Hemminger 	}
9311da177e4SLinus Torvalds 
9321da177e4SLinus Torvalds 	if (p->min > p->max)
9331da177e4SLinus Torvalds 		return -EINVAL;
9341da177e4SLinus Torvalds 
9351da177e4SLinus Torvalds 	return 0;
9361da177e4SLinus Torvalds }
9371da177e4SLinus Torvalds 
93822e70050SChristoph Hellwig static int xfrm_alloc_userspi(struct sk_buff *skb, struct nlmsghdr *nlh,
9395424f32eSThomas Graf 		struct nlattr **attrs)
9401da177e4SLinus Torvalds {
941fc34acd3SAlexey Dobriyan 	struct net *net = sock_net(skb->sk);
9421da177e4SLinus Torvalds 	struct xfrm_state *x;
9431da177e4SLinus Torvalds 	struct xfrm_userspi_info *p;
9441da177e4SLinus Torvalds 	struct sk_buff *resp_skb;
9451da177e4SLinus Torvalds 	xfrm_address_t *daddr;
9461da177e4SLinus Torvalds 	int family;
9471da177e4SLinus Torvalds 	int err;
9481da177e4SLinus Torvalds 
9497b67c857SThomas Graf 	p = nlmsg_data(nlh);
9501da177e4SLinus Torvalds 	err = verify_userspi_info(p);
9511da177e4SLinus Torvalds 	if (err)
9521da177e4SLinus Torvalds 		goto out_noput;
9531da177e4SLinus Torvalds 
9541da177e4SLinus Torvalds 	family = p->info.family;
9551da177e4SLinus Torvalds 	daddr = &p->info.id.daddr;
9561da177e4SLinus Torvalds 
9571da177e4SLinus Torvalds 	x = NULL;
9581da177e4SLinus Torvalds 	if (p->info.seq) {
959a6483b79SAlexey Dobriyan 		x = xfrm_find_acq_byseq(net, p->info.seq);
9601da177e4SLinus Torvalds 		if (x && xfrm_addr_cmp(&x->id.daddr, daddr, family)) {
9611da177e4SLinus Torvalds 			xfrm_state_put(x);
9621da177e4SLinus Torvalds 			x = NULL;
9631da177e4SLinus Torvalds 		}
9641da177e4SLinus Torvalds 	}
9651da177e4SLinus Torvalds 
9661da177e4SLinus Torvalds 	if (!x)
967a6483b79SAlexey Dobriyan 		x = xfrm_find_acq(net, p->info.mode, p->info.reqid,
9681da177e4SLinus Torvalds 				  p->info.id.proto, daddr,
9691da177e4SLinus Torvalds 				  &p->info.saddr, 1,
9701da177e4SLinus Torvalds 				  family);
9711da177e4SLinus Torvalds 	err = -ENOENT;
9721da177e4SLinus Torvalds 	if (x == NULL)
9731da177e4SLinus Torvalds 		goto out_noput;
9741da177e4SLinus Torvalds 
975658b219eSHerbert Xu 	err = xfrm_alloc_spi(x, p->min, p->max);
976658b219eSHerbert Xu 	if (err)
977658b219eSHerbert Xu 		goto out;
9781da177e4SLinus Torvalds 
9791da177e4SLinus Torvalds 	resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
9801da177e4SLinus Torvalds 	if (IS_ERR(resp_skb)) {
9811da177e4SLinus Torvalds 		err = PTR_ERR(resp_skb);
9821da177e4SLinus Torvalds 		goto out;
9831da177e4SLinus Torvalds 	}
9841da177e4SLinus Torvalds 
985a6483b79SAlexey Dobriyan 	err = nlmsg_unicast(net->xfrm.nlsk, resp_skb, NETLINK_CB(skb).pid);
9861da177e4SLinus Torvalds 
9871da177e4SLinus Torvalds out:
9881da177e4SLinus Torvalds 	xfrm_state_put(x);
9891da177e4SLinus Torvalds out_noput:
9901da177e4SLinus Torvalds 	return err;
9911da177e4SLinus Torvalds }
9921da177e4SLinus Torvalds 
993b798a9edSJamal Hadi Salim static int verify_policy_dir(u8 dir)
9941da177e4SLinus Torvalds {
9951da177e4SLinus Torvalds 	switch (dir) {
9961da177e4SLinus Torvalds 	case XFRM_POLICY_IN:
9971da177e4SLinus Torvalds 	case XFRM_POLICY_OUT:
9981da177e4SLinus Torvalds 	case XFRM_POLICY_FWD:
9991da177e4SLinus Torvalds 		break;
10001da177e4SLinus Torvalds 
10011da177e4SLinus Torvalds 	default:
10021da177e4SLinus Torvalds 		return -EINVAL;
10033ff50b79SStephen Hemminger 	}
10041da177e4SLinus Torvalds 
10051da177e4SLinus Torvalds 	return 0;
10061da177e4SLinus Torvalds }
10071da177e4SLinus Torvalds 
1008b798a9edSJamal Hadi Salim static int verify_policy_type(u8 type)
1009f7b6983fSMasahide NAKAMURA {
1010f7b6983fSMasahide NAKAMURA 	switch (type) {
1011f7b6983fSMasahide NAKAMURA 	case XFRM_POLICY_TYPE_MAIN:
1012f7b6983fSMasahide NAKAMURA #ifdef CONFIG_XFRM_SUB_POLICY
1013f7b6983fSMasahide NAKAMURA 	case XFRM_POLICY_TYPE_SUB:
1014f7b6983fSMasahide NAKAMURA #endif
1015f7b6983fSMasahide NAKAMURA 		break;
1016f7b6983fSMasahide NAKAMURA 
1017f7b6983fSMasahide NAKAMURA 	default:
1018f7b6983fSMasahide NAKAMURA 		return -EINVAL;
10193ff50b79SStephen Hemminger 	}
1020f7b6983fSMasahide NAKAMURA 
1021f7b6983fSMasahide NAKAMURA 	return 0;
1022f7b6983fSMasahide NAKAMURA }
1023f7b6983fSMasahide NAKAMURA 
10241da177e4SLinus Torvalds static int verify_newpolicy_info(struct xfrm_userpolicy_info *p)
10251da177e4SLinus Torvalds {
10261da177e4SLinus Torvalds 	switch (p->share) {
10271da177e4SLinus Torvalds 	case XFRM_SHARE_ANY:
10281da177e4SLinus Torvalds 	case XFRM_SHARE_SESSION:
10291da177e4SLinus Torvalds 	case XFRM_SHARE_USER:
10301da177e4SLinus Torvalds 	case XFRM_SHARE_UNIQUE:
10311da177e4SLinus Torvalds 		break;
10321da177e4SLinus Torvalds 
10331da177e4SLinus Torvalds 	default:
10341da177e4SLinus Torvalds 		return -EINVAL;
10353ff50b79SStephen Hemminger 	}
10361da177e4SLinus Torvalds 
10371da177e4SLinus Torvalds 	switch (p->action) {
10381da177e4SLinus Torvalds 	case XFRM_POLICY_ALLOW:
10391da177e4SLinus Torvalds 	case XFRM_POLICY_BLOCK:
10401da177e4SLinus Torvalds 		break;
10411da177e4SLinus Torvalds 
10421da177e4SLinus Torvalds 	default:
10431da177e4SLinus Torvalds 		return -EINVAL;
10443ff50b79SStephen Hemminger 	}
10451da177e4SLinus Torvalds 
10461da177e4SLinus Torvalds 	switch (p->sel.family) {
10471da177e4SLinus Torvalds 	case AF_INET:
10481da177e4SLinus Torvalds 		break;
10491da177e4SLinus Torvalds 
10501da177e4SLinus Torvalds 	case AF_INET6:
10511da177e4SLinus Torvalds #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
10521da177e4SLinus Torvalds 		break;
10531da177e4SLinus Torvalds #else
10541da177e4SLinus Torvalds 		return  -EAFNOSUPPORT;
10551da177e4SLinus Torvalds #endif
10561da177e4SLinus Torvalds 
10571da177e4SLinus Torvalds 	default:
10581da177e4SLinus Torvalds 		return -EINVAL;
10593ff50b79SStephen Hemminger 	}
10601da177e4SLinus Torvalds 
10611da177e4SLinus Torvalds 	return verify_policy_dir(p->dir);
10621da177e4SLinus Torvalds }
10631da177e4SLinus Torvalds 
10645424f32eSThomas Graf static int copy_from_user_sec_ctx(struct xfrm_policy *pol, struct nlattr **attrs)
1065df71837dSTrent Jaeger {
10665424f32eSThomas Graf 	struct nlattr *rt = attrs[XFRMA_SEC_CTX];
1067df71837dSTrent Jaeger 	struct xfrm_user_sec_ctx *uctx;
1068df71837dSTrent Jaeger 
1069df71837dSTrent Jaeger 	if (!rt)
1070df71837dSTrent Jaeger 		return 0;
1071df71837dSTrent Jaeger 
10725424f32eSThomas Graf 	uctx = nla_data(rt);
107303e1ad7bSPaul Moore 	return security_xfrm_policy_alloc(&pol->security, uctx);
1074df71837dSTrent Jaeger }
1075df71837dSTrent Jaeger 
10761da177e4SLinus Torvalds static void copy_templates(struct xfrm_policy *xp, struct xfrm_user_tmpl *ut,
10771da177e4SLinus Torvalds 			   int nr)
10781da177e4SLinus Torvalds {
10791da177e4SLinus Torvalds 	int i;
10801da177e4SLinus Torvalds 
10811da177e4SLinus Torvalds 	xp->xfrm_nr = nr;
10821da177e4SLinus Torvalds 	for (i = 0; i < nr; i++, ut++) {
10831da177e4SLinus Torvalds 		struct xfrm_tmpl *t = &xp->xfrm_vec[i];
10841da177e4SLinus Torvalds 
10851da177e4SLinus Torvalds 		memcpy(&t->id, &ut->id, sizeof(struct xfrm_id));
10861da177e4SLinus Torvalds 		memcpy(&t->saddr, &ut->saddr,
10871da177e4SLinus Torvalds 		       sizeof(xfrm_address_t));
10881da177e4SLinus Torvalds 		t->reqid = ut->reqid;
10891da177e4SLinus Torvalds 		t->mode = ut->mode;
10901da177e4SLinus Torvalds 		t->share = ut->share;
10911da177e4SLinus Torvalds 		t->optional = ut->optional;
10921da177e4SLinus Torvalds 		t->aalgos = ut->aalgos;
10931da177e4SLinus Torvalds 		t->ealgos = ut->ealgos;
10941da177e4SLinus Torvalds 		t->calgos = ut->calgos;
1095c5d18e98SHerbert Xu 		/* If all masks are ~0, then we allow all algorithms. */
1096c5d18e98SHerbert Xu 		t->allalgs = !~(t->aalgos & t->ealgos & t->calgos);
10978511d01dSMiika Komu 		t->encap_family = ut->family;
10981da177e4SLinus Torvalds 	}
10991da177e4SLinus Torvalds }
11001da177e4SLinus Torvalds 
1101b4ad86bfSDavid S. Miller static int validate_tmpl(int nr, struct xfrm_user_tmpl *ut, u16 family)
1102b4ad86bfSDavid S. Miller {
1103b4ad86bfSDavid S. Miller 	int i;
1104b4ad86bfSDavid S. Miller 
1105b4ad86bfSDavid S. Miller 	if (nr > XFRM_MAX_DEPTH)
1106b4ad86bfSDavid S. Miller 		return -EINVAL;
1107b4ad86bfSDavid S. Miller 
1108b4ad86bfSDavid S. Miller 	for (i = 0; i < nr; i++) {
1109b4ad86bfSDavid S. Miller 		/* We never validated the ut->family value, so many
1110b4ad86bfSDavid S. Miller 		 * applications simply leave it at zero.  The check was
1111b4ad86bfSDavid S. Miller 		 * never made and ut->family was ignored because all
1112b4ad86bfSDavid S. Miller 		 * templates could be assumed to have the same family as
1113b4ad86bfSDavid S. Miller 		 * the policy itself.  Now that we will have ipv4-in-ipv6
1114b4ad86bfSDavid S. Miller 		 * and ipv6-in-ipv4 tunnels, this is no longer true.
1115b4ad86bfSDavid S. Miller 		 */
1116b4ad86bfSDavid S. Miller 		if (!ut[i].family)
1117b4ad86bfSDavid S. Miller 			ut[i].family = family;
1118b4ad86bfSDavid S. Miller 
1119b4ad86bfSDavid S. Miller 		switch (ut[i].family) {
1120b4ad86bfSDavid S. Miller 		case AF_INET:
1121b4ad86bfSDavid S. Miller 			break;
1122b4ad86bfSDavid S. Miller #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1123b4ad86bfSDavid S. Miller 		case AF_INET6:
1124b4ad86bfSDavid S. Miller 			break;
1125b4ad86bfSDavid S. Miller #endif
1126b4ad86bfSDavid S. Miller 		default:
1127b4ad86bfSDavid S. Miller 			return -EINVAL;
11283ff50b79SStephen Hemminger 		}
1129b4ad86bfSDavid S. Miller 	}
1130b4ad86bfSDavid S. Miller 
1131b4ad86bfSDavid S. Miller 	return 0;
1132b4ad86bfSDavid S. Miller }
1133b4ad86bfSDavid S. Miller 
11345424f32eSThomas Graf static int copy_from_user_tmpl(struct xfrm_policy *pol, struct nlattr **attrs)
11351da177e4SLinus Torvalds {
11365424f32eSThomas Graf 	struct nlattr *rt = attrs[XFRMA_TMPL];
11371da177e4SLinus Torvalds 
11381da177e4SLinus Torvalds 	if (!rt) {
11391da177e4SLinus Torvalds 		pol->xfrm_nr = 0;
11401da177e4SLinus Torvalds 	} else {
11415424f32eSThomas Graf 		struct xfrm_user_tmpl *utmpl = nla_data(rt);
11425424f32eSThomas Graf 		int nr = nla_len(rt) / sizeof(*utmpl);
1143b4ad86bfSDavid S. Miller 		int err;
11441da177e4SLinus Torvalds 
1145b4ad86bfSDavid S. Miller 		err = validate_tmpl(nr, utmpl, pol->family);
1146b4ad86bfSDavid S. Miller 		if (err)
1147b4ad86bfSDavid S. Miller 			return err;
11481da177e4SLinus Torvalds 
11495424f32eSThomas Graf 		copy_templates(pol, utmpl, nr);
11501da177e4SLinus Torvalds 	}
11511da177e4SLinus Torvalds 	return 0;
11521da177e4SLinus Torvalds }
11531da177e4SLinus Torvalds 
11545424f32eSThomas Graf static int copy_from_user_policy_type(u8 *tp, struct nlattr **attrs)
1155f7b6983fSMasahide NAKAMURA {
11565424f32eSThomas Graf 	struct nlattr *rt = attrs[XFRMA_POLICY_TYPE];
1157f7b6983fSMasahide NAKAMURA 	struct xfrm_userpolicy_type *upt;
1158b798a9edSJamal Hadi Salim 	u8 type = XFRM_POLICY_TYPE_MAIN;
1159f7b6983fSMasahide NAKAMURA 	int err;
1160f7b6983fSMasahide NAKAMURA 
1161f7b6983fSMasahide NAKAMURA 	if (rt) {
11625424f32eSThomas Graf 		upt = nla_data(rt);
1163f7b6983fSMasahide NAKAMURA 		type = upt->type;
1164f7b6983fSMasahide NAKAMURA 	}
1165f7b6983fSMasahide NAKAMURA 
1166f7b6983fSMasahide NAKAMURA 	err = verify_policy_type(type);
1167f7b6983fSMasahide NAKAMURA 	if (err)
1168f7b6983fSMasahide NAKAMURA 		return err;
1169f7b6983fSMasahide NAKAMURA 
1170f7b6983fSMasahide NAKAMURA 	*tp = type;
1171f7b6983fSMasahide NAKAMURA 	return 0;
1172f7b6983fSMasahide NAKAMURA }
1173f7b6983fSMasahide NAKAMURA 
11741da177e4SLinus Torvalds static void copy_from_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p)
11751da177e4SLinus Torvalds {
11761da177e4SLinus Torvalds 	xp->priority = p->priority;
11771da177e4SLinus Torvalds 	xp->index = p->index;
11781da177e4SLinus Torvalds 	memcpy(&xp->selector, &p->sel, sizeof(xp->selector));
11791da177e4SLinus Torvalds 	memcpy(&xp->lft, &p->lft, sizeof(xp->lft));
11801da177e4SLinus Torvalds 	xp->action = p->action;
11811da177e4SLinus Torvalds 	xp->flags = p->flags;
11821da177e4SLinus Torvalds 	xp->family = p->sel.family;
11831da177e4SLinus Torvalds 	/* XXX xp->share = p->share; */
11841da177e4SLinus Torvalds }
11851da177e4SLinus Torvalds 
11861da177e4SLinus Torvalds static void copy_to_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p, int dir)
11871da177e4SLinus Torvalds {
11881da177e4SLinus Torvalds 	memcpy(&p->sel, &xp->selector, sizeof(p->sel));
11891da177e4SLinus Torvalds 	memcpy(&p->lft, &xp->lft, sizeof(p->lft));
11901da177e4SLinus Torvalds 	memcpy(&p->curlft, &xp->curlft, sizeof(p->curlft));
11911da177e4SLinus Torvalds 	p->priority = xp->priority;
11921da177e4SLinus Torvalds 	p->index = xp->index;
11931da177e4SLinus Torvalds 	p->sel.family = xp->family;
11941da177e4SLinus Torvalds 	p->dir = dir;
11951da177e4SLinus Torvalds 	p->action = xp->action;
11961da177e4SLinus Torvalds 	p->flags = xp->flags;
11971da177e4SLinus Torvalds 	p->share = XFRM_SHARE_ANY; /* XXX xp->share */
11981da177e4SLinus Torvalds }
11991da177e4SLinus Torvalds 
1200fc34acd3SAlexey Dobriyan static struct xfrm_policy *xfrm_policy_construct(struct net *net, struct xfrm_userpolicy_info *p, struct nlattr **attrs, int *errp)
12011da177e4SLinus Torvalds {
1202fc34acd3SAlexey Dobriyan 	struct xfrm_policy *xp = xfrm_policy_alloc(net, GFP_KERNEL);
12031da177e4SLinus Torvalds 	int err;
12041da177e4SLinus Torvalds 
12051da177e4SLinus Torvalds 	if (!xp) {
12061da177e4SLinus Torvalds 		*errp = -ENOMEM;
12071da177e4SLinus Torvalds 		return NULL;
12081da177e4SLinus Torvalds 	}
12091da177e4SLinus Torvalds 
12101da177e4SLinus Torvalds 	copy_from_user_policy(xp, p);
1211df71837dSTrent Jaeger 
121235a7aa08SThomas Graf 	err = copy_from_user_policy_type(&xp->type, attrs);
1213f7b6983fSMasahide NAKAMURA 	if (err)
1214f7b6983fSMasahide NAKAMURA 		goto error;
1215f7b6983fSMasahide NAKAMURA 
121635a7aa08SThomas Graf 	if (!(err = copy_from_user_tmpl(xp, attrs)))
121735a7aa08SThomas Graf 		err = copy_from_user_sec_ctx(xp, attrs);
1218f7b6983fSMasahide NAKAMURA 	if (err)
1219f7b6983fSMasahide NAKAMURA 		goto error;
12201da177e4SLinus Torvalds 
12211da177e4SLinus Torvalds 	return xp;
1222f7b6983fSMasahide NAKAMURA  error:
1223f7b6983fSMasahide NAKAMURA 	*errp = err;
122412a169e7SHerbert Xu 	xp->walk.dead = 1;
122564c31b3fSWANG Cong 	xfrm_policy_destroy(xp);
1226f7b6983fSMasahide NAKAMURA 	return NULL;
12271da177e4SLinus Torvalds }
12281da177e4SLinus Torvalds 
122922e70050SChristoph Hellwig static int xfrm_add_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
12305424f32eSThomas Graf 		struct nlattr **attrs)
12311da177e4SLinus Torvalds {
1232fc34acd3SAlexey Dobriyan 	struct net *net = sock_net(skb->sk);
12337b67c857SThomas Graf 	struct xfrm_userpolicy_info *p = nlmsg_data(nlh);
12341da177e4SLinus Torvalds 	struct xfrm_policy *xp;
123526b15dadSJamal Hadi Salim 	struct km_event c;
12361da177e4SLinus Torvalds 	int err;
12371da177e4SLinus Torvalds 	int excl;
12382532386fSEric Paris 	uid_t loginuid = NETLINK_CB(skb).loginuid;
12392532386fSEric Paris 	u32 sessionid = NETLINK_CB(skb).sessionid;
12402532386fSEric Paris 	u32 sid = NETLINK_CB(skb).sid;
12411da177e4SLinus Torvalds 
12421da177e4SLinus Torvalds 	err = verify_newpolicy_info(p);
12431da177e4SLinus Torvalds 	if (err)
12441da177e4SLinus Torvalds 		return err;
124535a7aa08SThomas Graf 	err = verify_sec_ctx_len(attrs);
1246df71837dSTrent Jaeger 	if (err)
1247df71837dSTrent Jaeger 		return err;
12481da177e4SLinus Torvalds 
1249fc34acd3SAlexey Dobriyan 	xp = xfrm_policy_construct(net, p, attrs, &err);
12501da177e4SLinus Torvalds 	if (!xp)
12511da177e4SLinus Torvalds 		return err;
12521da177e4SLinus Torvalds 
125326b15dadSJamal Hadi Salim 	/* shouldnt excl be based on nlh flags??
125426b15dadSJamal Hadi Salim 	 * Aha! this is anti-netlink really i.e  more pfkey derived
125526b15dadSJamal Hadi Salim 	 * in netlink excl is a flag and you wouldnt need
125626b15dadSJamal Hadi Salim 	 * a type XFRM_MSG_UPDPOLICY - JHS */
12571da177e4SLinus Torvalds 	excl = nlh->nlmsg_type == XFRM_MSG_NEWPOLICY;
12581da177e4SLinus Torvalds 	err = xfrm_policy_insert(p->dir, xp, excl);
12592532386fSEric Paris 	xfrm_audit_policy_add(xp, err ? 0 : 1, loginuid, sessionid, sid);
1260161a09e7SJoy Latten 
12611da177e4SLinus Torvalds 	if (err) {
126203e1ad7bSPaul Moore 		security_xfrm_policy_free(xp->security);
12631da177e4SLinus Torvalds 		kfree(xp);
12641da177e4SLinus Torvalds 		return err;
12651da177e4SLinus Torvalds 	}
12661da177e4SLinus Torvalds 
1267f60f6b8fSHerbert Xu 	c.event = nlh->nlmsg_type;
126826b15dadSJamal Hadi Salim 	c.seq = nlh->nlmsg_seq;
126926b15dadSJamal Hadi Salim 	c.pid = nlh->nlmsg_pid;
127026b15dadSJamal Hadi Salim 	km_policy_notify(xp, p->dir, &c);
127126b15dadSJamal Hadi Salim 
12721da177e4SLinus Torvalds 	xfrm_pol_put(xp);
12731da177e4SLinus Torvalds 
12741da177e4SLinus Torvalds 	return 0;
12751da177e4SLinus Torvalds }
12761da177e4SLinus Torvalds 
12771da177e4SLinus Torvalds static int copy_to_user_tmpl(struct xfrm_policy *xp, struct sk_buff *skb)
12781da177e4SLinus Torvalds {
12791da177e4SLinus Torvalds 	struct xfrm_user_tmpl vec[XFRM_MAX_DEPTH];
12801da177e4SLinus Torvalds 	int i;
12811da177e4SLinus Torvalds 
12821da177e4SLinus Torvalds 	if (xp->xfrm_nr == 0)
12831da177e4SLinus Torvalds 		return 0;
12841da177e4SLinus Torvalds 
12851da177e4SLinus Torvalds 	for (i = 0; i < xp->xfrm_nr; i++) {
12861da177e4SLinus Torvalds 		struct xfrm_user_tmpl *up = &vec[i];
12871da177e4SLinus Torvalds 		struct xfrm_tmpl *kp = &xp->xfrm_vec[i];
12881da177e4SLinus Torvalds 
12891da177e4SLinus Torvalds 		memcpy(&up->id, &kp->id, sizeof(up->id));
12908511d01dSMiika Komu 		up->family = kp->encap_family;
12911da177e4SLinus Torvalds 		memcpy(&up->saddr, &kp->saddr, sizeof(up->saddr));
12921da177e4SLinus Torvalds 		up->reqid = kp->reqid;
12931da177e4SLinus Torvalds 		up->mode = kp->mode;
12941da177e4SLinus Torvalds 		up->share = kp->share;
12951da177e4SLinus Torvalds 		up->optional = kp->optional;
12961da177e4SLinus Torvalds 		up->aalgos = kp->aalgos;
12971da177e4SLinus Torvalds 		up->ealgos = kp->ealgos;
12981da177e4SLinus Torvalds 		up->calgos = kp->calgos;
12991da177e4SLinus Torvalds 	}
13001da177e4SLinus Torvalds 
1301c0144beaSThomas Graf 	return nla_put(skb, XFRMA_TMPL,
1302c0144beaSThomas Graf 		       sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr, vec);
1303df71837dSTrent Jaeger }
1304df71837dSTrent Jaeger 
13050d681623SSerge Hallyn static inline int copy_to_user_state_sec_ctx(struct xfrm_state *x, struct sk_buff *skb)
13060d681623SSerge Hallyn {
13070d681623SSerge Hallyn 	if (x->security) {
13080d681623SSerge Hallyn 		return copy_sec_ctx(x->security, skb);
13090d681623SSerge Hallyn 	}
13100d681623SSerge Hallyn 	return 0;
13110d681623SSerge Hallyn }
13120d681623SSerge Hallyn 
13130d681623SSerge Hallyn static inline int copy_to_user_sec_ctx(struct xfrm_policy *xp, struct sk_buff *skb)
13140d681623SSerge Hallyn {
13150d681623SSerge Hallyn 	if (xp->security) {
13160d681623SSerge Hallyn 		return copy_sec_ctx(xp->security, skb);
13170d681623SSerge Hallyn 	}
13180d681623SSerge Hallyn 	return 0;
13190d681623SSerge Hallyn }
1320cfbfd45aSThomas Graf static inline size_t userpolicy_type_attrsize(void)
1321cfbfd45aSThomas Graf {
1322cfbfd45aSThomas Graf #ifdef CONFIG_XFRM_SUB_POLICY
1323cfbfd45aSThomas Graf 	return nla_total_size(sizeof(struct xfrm_userpolicy_type));
1324cfbfd45aSThomas Graf #else
1325cfbfd45aSThomas Graf 	return 0;
1326cfbfd45aSThomas Graf #endif
1327cfbfd45aSThomas Graf }
13280d681623SSerge Hallyn 
1329f7b6983fSMasahide NAKAMURA #ifdef CONFIG_XFRM_SUB_POLICY
1330b798a9edSJamal Hadi Salim static int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
1331f7b6983fSMasahide NAKAMURA {
1332c0144beaSThomas Graf 	struct xfrm_userpolicy_type upt = {
1333c0144beaSThomas Graf 		.type = type,
1334c0144beaSThomas Graf 	};
1335f7b6983fSMasahide NAKAMURA 
1336c0144beaSThomas Graf 	return nla_put(skb, XFRMA_POLICY_TYPE, sizeof(upt), &upt);
1337f7b6983fSMasahide NAKAMURA }
1338f7b6983fSMasahide NAKAMURA 
1339f7b6983fSMasahide NAKAMURA #else
1340b798a9edSJamal Hadi Salim static inline int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
1341f7b6983fSMasahide NAKAMURA {
1342f7b6983fSMasahide NAKAMURA 	return 0;
1343f7b6983fSMasahide NAKAMURA }
1344f7b6983fSMasahide NAKAMURA #endif
1345f7b6983fSMasahide NAKAMURA 
13461da177e4SLinus Torvalds static int dump_one_policy(struct xfrm_policy *xp, int dir, int count, void *ptr)
13471da177e4SLinus Torvalds {
13481da177e4SLinus Torvalds 	struct xfrm_dump_info *sp = ptr;
13491da177e4SLinus Torvalds 	struct xfrm_userpolicy_info *p;
13501da177e4SLinus Torvalds 	struct sk_buff *in_skb = sp->in_skb;
13511da177e4SLinus Torvalds 	struct sk_buff *skb = sp->out_skb;
13521da177e4SLinus Torvalds 	struct nlmsghdr *nlh;
13531da177e4SLinus Torvalds 
135479b8b7f4SThomas Graf 	nlh = nlmsg_put(skb, NETLINK_CB(in_skb).pid, sp->nlmsg_seq,
135579b8b7f4SThomas Graf 			XFRM_MSG_NEWPOLICY, sizeof(*p), sp->nlmsg_flags);
135679b8b7f4SThomas Graf 	if (nlh == NULL)
135779b8b7f4SThomas Graf 		return -EMSGSIZE;
13581da177e4SLinus Torvalds 
13597b67c857SThomas Graf 	p = nlmsg_data(nlh);
13601da177e4SLinus Torvalds 	copy_to_user_policy(xp, p, dir);
13611da177e4SLinus Torvalds 	if (copy_to_user_tmpl(xp, skb) < 0)
13621da177e4SLinus Torvalds 		goto nlmsg_failure;
1363df71837dSTrent Jaeger 	if (copy_to_user_sec_ctx(xp, skb))
1364df71837dSTrent Jaeger 		goto nlmsg_failure;
13651459bb36SJamal Hadi Salim 	if (copy_to_user_policy_type(xp->type, skb) < 0)
1366f7b6983fSMasahide NAKAMURA 		goto nlmsg_failure;
13671da177e4SLinus Torvalds 
13689825069dSThomas Graf 	nlmsg_end(skb, nlh);
13691da177e4SLinus Torvalds 	return 0;
13701da177e4SLinus Torvalds 
13711da177e4SLinus Torvalds nlmsg_failure:
13729825069dSThomas Graf 	nlmsg_cancel(skb, nlh);
13739825069dSThomas Graf 	return -EMSGSIZE;
13741da177e4SLinus Torvalds }
13751da177e4SLinus Torvalds 
13764c563f76STimo Teras static int xfrm_dump_policy_done(struct netlink_callback *cb)
13774c563f76STimo Teras {
13784c563f76STimo Teras 	struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *) &cb->args[1];
13794c563f76STimo Teras 
13804c563f76STimo Teras 	xfrm_policy_walk_done(walk);
13814c563f76STimo Teras 	return 0;
13824c563f76STimo Teras }
13834c563f76STimo Teras 
13841da177e4SLinus Torvalds static int xfrm_dump_policy(struct sk_buff *skb, struct netlink_callback *cb)
13851da177e4SLinus Torvalds {
1386fc34acd3SAlexey Dobriyan 	struct net *net = sock_net(skb->sk);
13874c563f76STimo Teras 	struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *) &cb->args[1];
13881da177e4SLinus Torvalds 	struct xfrm_dump_info info;
13891da177e4SLinus Torvalds 
13904c563f76STimo Teras 	BUILD_BUG_ON(sizeof(struct xfrm_policy_walk) >
13914c563f76STimo Teras 		     sizeof(cb->args) - sizeof(cb->args[0]));
13924c563f76STimo Teras 
13931da177e4SLinus Torvalds 	info.in_skb = cb->skb;
13941da177e4SLinus Torvalds 	info.out_skb = skb;
13951da177e4SLinus Torvalds 	info.nlmsg_seq = cb->nlh->nlmsg_seq;
13961da177e4SLinus Torvalds 	info.nlmsg_flags = NLM_F_MULTI;
13974c563f76STimo Teras 
13984c563f76STimo Teras 	if (!cb->args[0]) {
13994c563f76STimo Teras 		cb->args[0] = 1;
14004c563f76STimo Teras 		xfrm_policy_walk_init(walk, XFRM_POLICY_TYPE_ANY);
14014c563f76STimo Teras 	}
14024c563f76STimo Teras 
1403fc34acd3SAlexey Dobriyan 	(void) xfrm_policy_walk(net, walk, dump_one_policy, &info);
14041da177e4SLinus Torvalds 
14051da177e4SLinus Torvalds 	return skb->len;
14061da177e4SLinus Torvalds }
14071da177e4SLinus Torvalds 
14081da177e4SLinus Torvalds static struct sk_buff *xfrm_policy_netlink(struct sk_buff *in_skb,
14091da177e4SLinus Torvalds 					  struct xfrm_policy *xp,
14101da177e4SLinus Torvalds 					  int dir, u32 seq)
14111da177e4SLinus Torvalds {
14121da177e4SLinus Torvalds 	struct xfrm_dump_info info;
14131da177e4SLinus Torvalds 	struct sk_buff *skb;
14141da177e4SLinus Torvalds 
14157deb2264SThomas Graf 	skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
14161da177e4SLinus Torvalds 	if (!skb)
14171da177e4SLinus Torvalds 		return ERR_PTR(-ENOMEM);
14181da177e4SLinus Torvalds 
14191da177e4SLinus Torvalds 	info.in_skb = in_skb;
14201da177e4SLinus Torvalds 	info.out_skb = skb;
14211da177e4SLinus Torvalds 	info.nlmsg_seq = seq;
14221da177e4SLinus Torvalds 	info.nlmsg_flags = 0;
14231da177e4SLinus Torvalds 
14241da177e4SLinus Torvalds 	if (dump_one_policy(xp, dir, 0, &info) < 0) {
14251da177e4SLinus Torvalds 		kfree_skb(skb);
14261da177e4SLinus Torvalds 		return NULL;
14271da177e4SLinus Torvalds 	}
14281da177e4SLinus Torvalds 
14291da177e4SLinus Torvalds 	return skb;
14301da177e4SLinus Torvalds }
14311da177e4SLinus Torvalds 
143222e70050SChristoph Hellwig static int xfrm_get_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
14335424f32eSThomas Graf 		struct nlattr **attrs)
14341da177e4SLinus Torvalds {
1435fc34acd3SAlexey Dobriyan 	struct net *net = sock_net(skb->sk);
14361da177e4SLinus Torvalds 	struct xfrm_policy *xp;
14371da177e4SLinus Torvalds 	struct xfrm_userpolicy_id *p;
1438b798a9edSJamal Hadi Salim 	u8 type = XFRM_POLICY_TYPE_MAIN;
14391da177e4SLinus Torvalds 	int err;
144026b15dadSJamal Hadi Salim 	struct km_event c;
14411da177e4SLinus Torvalds 	int delete;
14421da177e4SLinus Torvalds 
14437b67c857SThomas Graf 	p = nlmsg_data(nlh);
14441da177e4SLinus Torvalds 	delete = nlh->nlmsg_type == XFRM_MSG_DELPOLICY;
14451da177e4SLinus Torvalds 
144635a7aa08SThomas Graf 	err = copy_from_user_policy_type(&type, attrs);
1447f7b6983fSMasahide NAKAMURA 	if (err)
1448f7b6983fSMasahide NAKAMURA 		return err;
1449f7b6983fSMasahide NAKAMURA 
14501da177e4SLinus Torvalds 	err = verify_policy_dir(p->dir);
14511da177e4SLinus Torvalds 	if (err)
14521da177e4SLinus Torvalds 		return err;
14531da177e4SLinus Torvalds 
14541da177e4SLinus Torvalds 	if (p->index)
1455a6483b79SAlexey Dobriyan 		xp = xfrm_policy_byid(net, type, p->dir, p->index, delete, &err);
1456df71837dSTrent Jaeger 	else {
14575424f32eSThomas Graf 		struct nlattr *rt = attrs[XFRMA_SEC_CTX];
145803e1ad7bSPaul Moore 		struct xfrm_sec_ctx *ctx;
1459df71837dSTrent Jaeger 
146035a7aa08SThomas Graf 		err = verify_sec_ctx_len(attrs);
1461df71837dSTrent Jaeger 		if (err)
1462df71837dSTrent Jaeger 			return err;
1463df71837dSTrent Jaeger 
14642c8dd116SDenis V. Lunev 		ctx = NULL;
1465df71837dSTrent Jaeger 		if (rt) {
14665424f32eSThomas Graf 			struct xfrm_user_sec_ctx *uctx = nla_data(rt);
1467df71837dSTrent Jaeger 
146803e1ad7bSPaul Moore 			err = security_xfrm_policy_alloc(&ctx, uctx);
146903e1ad7bSPaul Moore 			if (err)
1470df71837dSTrent Jaeger 				return err;
14712c8dd116SDenis V. Lunev 		}
1472a6483b79SAlexey Dobriyan 		xp = xfrm_policy_bysel_ctx(net, type, p->dir, &p->sel, ctx,
1473ef41aaa0SEric Paris 					   delete, &err);
147403e1ad7bSPaul Moore 		security_xfrm_policy_free(ctx);
1475df71837dSTrent Jaeger 	}
14761da177e4SLinus Torvalds 	if (xp == NULL)
14771da177e4SLinus Torvalds 		return -ENOENT;
14781da177e4SLinus Torvalds 
14791da177e4SLinus Torvalds 	if (!delete) {
14801da177e4SLinus Torvalds 		struct sk_buff *resp_skb;
14811da177e4SLinus Torvalds 
14821da177e4SLinus Torvalds 		resp_skb = xfrm_policy_netlink(skb, xp, p->dir, nlh->nlmsg_seq);
14831da177e4SLinus Torvalds 		if (IS_ERR(resp_skb)) {
14841da177e4SLinus Torvalds 			err = PTR_ERR(resp_skb);
14851da177e4SLinus Torvalds 		} else {
1486a6483b79SAlexey Dobriyan 			err = nlmsg_unicast(net->xfrm.nlsk, resp_skb,
1487082a1ad5SThomas Graf 					    NETLINK_CB(skb).pid);
14881da177e4SLinus Torvalds 		}
148926b15dadSJamal Hadi Salim 	} else {
14902532386fSEric Paris 		uid_t loginuid = NETLINK_CB(skb).loginuid;
14912532386fSEric Paris 		u32 sessionid = NETLINK_CB(skb).sessionid;
14922532386fSEric Paris 		u32 sid = NETLINK_CB(skb).sid;
14932532386fSEric Paris 
14942532386fSEric Paris 		xfrm_audit_policy_delete(xp, err ? 0 : 1, loginuid, sessionid,
14952532386fSEric Paris 					 sid);
149613fcfbb0SDavid S. Miller 
149713fcfbb0SDavid S. Miller 		if (err != 0)
1498c8c05a8eSCatherine Zhang 			goto out;
149913fcfbb0SDavid S. Miller 
1500e7443892SHerbert Xu 		c.data.byid = p->index;
1501f60f6b8fSHerbert Xu 		c.event = nlh->nlmsg_type;
150226b15dadSJamal Hadi Salim 		c.seq = nlh->nlmsg_seq;
150326b15dadSJamal Hadi Salim 		c.pid = nlh->nlmsg_pid;
150426b15dadSJamal Hadi Salim 		km_policy_notify(xp, p->dir, &c);
15051da177e4SLinus Torvalds 	}
15061da177e4SLinus Torvalds 
1507c8c05a8eSCatherine Zhang out:
1508ef41aaa0SEric Paris 	xfrm_pol_put(xp);
15091da177e4SLinus Torvalds 	return err;
15101da177e4SLinus Torvalds }
15111da177e4SLinus Torvalds 
151222e70050SChristoph Hellwig static int xfrm_flush_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
15135424f32eSThomas Graf 		struct nlattr **attrs)
15141da177e4SLinus Torvalds {
1515fc34acd3SAlexey Dobriyan 	struct net *net = sock_net(skb->sk);
151626b15dadSJamal Hadi Salim 	struct km_event c;
15177b67c857SThomas Graf 	struct xfrm_usersa_flush *p = nlmsg_data(nlh);
1518161a09e7SJoy Latten 	struct xfrm_audit audit_info;
15194aa2e62cSJoy Latten 	int err;
15201da177e4SLinus Torvalds 
1521161a09e7SJoy Latten 	audit_info.loginuid = NETLINK_CB(skb).loginuid;
15222532386fSEric Paris 	audit_info.sessionid = NETLINK_CB(skb).sessionid;
1523161a09e7SJoy Latten 	audit_info.secid = NETLINK_CB(skb).sid;
1524fc34acd3SAlexey Dobriyan 	err = xfrm_state_flush(net, p->proto, &audit_info);
15254aa2e62cSJoy Latten 	if (err)
15264aa2e62cSJoy Latten 		return err;
1527bf08867fSHerbert Xu 	c.data.proto = p->proto;
1528f60f6b8fSHerbert Xu 	c.event = nlh->nlmsg_type;
152926b15dadSJamal Hadi Salim 	c.seq = nlh->nlmsg_seq;
153026b15dadSJamal Hadi Salim 	c.pid = nlh->nlmsg_pid;
15317067802eSAlexey Dobriyan 	c.net = net;
153226b15dadSJamal Hadi Salim 	km_state_notify(NULL, &c);
153326b15dadSJamal Hadi Salim 
15341da177e4SLinus Torvalds 	return 0;
15351da177e4SLinus Torvalds }
15361da177e4SLinus Torvalds 
15377deb2264SThomas Graf static inline size_t xfrm_aevent_msgsize(void)
15387deb2264SThomas Graf {
15397deb2264SThomas Graf 	return NLMSG_ALIGN(sizeof(struct xfrm_aevent_id))
15407deb2264SThomas Graf 	       + nla_total_size(sizeof(struct xfrm_replay_state))
15417deb2264SThomas Graf 	       + nla_total_size(sizeof(struct xfrm_lifetime_cur))
15427deb2264SThomas Graf 	       + nla_total_size(4) /* XFRM_AE_RTHR */
15437deb2264SThomas Graf 	       + nla_total_size(4); /* XFRM_AE_ETHR */
15447deb2264SThomas Graf }
1545d51d081dSJamal Hadi Salim 
1546d51d081dSJamal Hadi Salim static int build_aevent(struct sk_buff *skb, struct xfrm_state *x, struct km_event *c)
1547d51d081dSJamal Hadi Salim {
1548d51d081dSJamal Hadi Salim 	struct xfrm_aevent_id *id;
1549d51d081dSJamal Hadi Salim 	struct nlmsghdr *nlh;
1550d51d081dSJamal Hadi Salim 
155179b8b7f4SThomas Graf 	nlh = nlmsg_put(skb, c->pid, c->seq, XFRM_MSG_NEWAE, sizeof(*id), 0);
155279b8b7f4SThomas Graf 	if (nlh == NULL)
155379b8b7f4SThomas Graf 		return -EMSGSIZE;
1554d51d081dSJamal Hadi Salim 
15557b67c857SThomas Graf 	id = nlmsg_data(nlh);
15562b5f6dccSJamal Hadi Salim 	memcpy(&id->sa_id.daddr, &x->id.daddr,sizeof(x->id.daddr));
1557d51d081dSJamal Hadi Salim 	id->sa_id.spi = x->id.spi;
1558d51d081dSJamal Hadi Salim 	id->sa_id.family = x->props.family;
1559d51d081dSJamal Hadi Salim 	id->sa_id.proto = x->id.proto;
15602b5f6dccSJamal Hadi Salim 	memcpy(&id->saddr, &x->props.saddr,sizeof(x->props.saddr));
15612b5f6dccSJamal Hadi Salim 	id->reqid = x->props.reqid;
1562d51d081dSJamal Hadi Salim 	id->flags = c->data.aevent;
1563d51d081dSJamal Hadi Salim 
1564c0144beaSThomas Graf 	NLA_PUT(skb, XFRMA_REPLAY_VAL, sizeof(x->replay), &x->replay);
1565c0144beaSThomas Graf 	NLA_PUT(skb, XFRMA_LTIME_VAL, sizeof(x->curlft), &x->curlft);
1566d51d081dSJamal Hadi Salim 
1567c0144beaSThomas Graf 	if (id->flags & XFRM_AE_RTHR)
1568c0144beaSThomas Graf 		NLA_PUT_U32(skb, XFRMA_REPLAY_THRESH, x->replay_maxdiff);
1569d51d081dSJamal Hadi Salim 
1570c0144beaSThomas Graf 	if (id->flags & XFRM_AE_ETHR)
1571c0144beaSThomas Graf 		NLA_PUT_U32(skb, XFRMA_ETIMER_THRESH,
1572c0144beaSThomas Graf 			    x->replay_maxage * 10 / HZ);
1573d51d081dSJamal Hadi Salim 
15749825069dSThomas Graf 	return nlmsg_end(skb, nlh);
1575d51d081dSJamal Hadi Salim 
1576c0144beaSThomas Graf nla_put_failure:
15779825069dSThomas Graf 	nlmsg_cancel(skb, nlh);
15789825069dSThomas Graf 	return -EMSGSIZE;
1579d51d081dSJamal Hadi Salim }
1580d51d081dSJamal Hadi Salim 
158122e70050SChristoph Hellwig static int xfrm_get_ae(struct sk_buff *skb, struct nlmsghdr *nlh,
15825424f32eSThomas Graf 		struct nlattr **attrs)
1583d51d081dSJamal Hadi Salim {
1584fc34acd3SAlexey Dobriyan 	struct net *net = sock_net(skb->sk);
1585d51d081dSJamal Hadi Salim 	struct xfrm_state *x;
1586d51d081dSJamal Hadi Salim 	struct sk_buff *r_skb;
1587d51d081dSJamal Hadi Salim 	int err;
1588d51d081dSJamal Hadi Salim 	struct km_event c;
15897b67c857SThomas Graf 	struct xfrm_aevent_id *p = nlmsg_data(nlh);
1590d51d081dSJamal Hadi Salim 	struct xfrm_usersa_id *id = &p->sa_id;
1591d51d081dSJamal Hadi Salim 
15927deb2264SThomas Graf 	r_skb = nlmsg_new(xfrm_aevent_msgsize(), GFP_ATOMIC);
1593d51d081dSJamal Hadi Salim 	if (r_skb == NULL)
1594d51d081dSJamal Hadi Salim 		return -ENOMEM;
1595d51d081dSJamal Hadi Salim 
1596a6483b79SAlexey Dobriyan 	x = xfrm_state_lookup(net, &id->daddr, id->spi, id->proto, id->family);
1597d51d081dSJamal Hadi Salim 	if (x == NULL) {
1598b08d5840SPatrick McHardy 		kfree_skb(r_skb);
1599d51d081dSJamal Hadi Salim 		return -ESRCH;
1600d51d081dSJamal Hadi Salim 	}
1601d51d081dSJamal Hadi Salim 
1602d51d081dSJamal Hadi Salim 	/*
1603d51d081dSJamal Hadi Salim 	 * XXX: is this lock really needed - none of the other
1604d51d081dSJamal Hadi Salim 	 * gets lock (the concern is things getting updated
1605d51d081dSJamal Hadi Salim 	 * while we are still reading) - jhs
1606d51d081dSJamal Hadi Salim 	*/
1607d51d081dSJamal Hadi Salim 	spin_lock_bh(&x->lock);
1608d51d081dSJamal Hadi Salim 	c.data.aevent = p->flags;
1609d51d081dSJamal Hadi Salim 	c.seq = nlh->nlmsg_seq;
1610d51d081dSJamal Hadi Salim 	c.pid = nlh->nlmsg_pid;
1611d51d081dSJamal Hadi Salim 
1612d51d081dSJamal Hadi Salim 	if (build_aevent(r_skb, x, &c) < 0)
1613d51d081dSJamal Hadi Salim 		BUG();
1614a6483b79SAlexey Dobriyan 	err = nlmsg_unicast(net->xfrm.nlsk, r_skb, NETLINK_CB(skb).pid);
1615d51d081dSJamal Hadi Salim 	spin_unlock_bh(&x->lock);
1616d51d081dSJamal Hadi Salim 	xfrm_state_put(x);
1617d51d081dSJamal Hadi Salim 	return err;
1618d51d081dSJamal Hadi Salim }
1619d51d081dSJamal Hadi Salim 
162022e70050SChristoph Hellwig static int xfrm_new_ae(struct sk_buff *skb, struct nlmsghdr *nlh,
16215424f32eSThomas Graf 		struct nlattr **attrs)
1622d51d081dSJamal Hadi Salim {
1623fc34acd3SAlexey Dobriyan 	struct net *net = sock_net(skb->sk);
1624d51d081dSJamal Hadi Salim 	struct xfrm_state *x;
1625d51d081dSJamal Hadi Salim 	struct km_event c;
1626d51d081dSJamal Hadi Salim 	int err = - EINVAL;
16277b67c857SThomas Graf 	struct xfrm_aevent_id *p = nlmsg_data(nlh);
16285424f32eSThomas Graf 	struct nlattr *rp = attrs[XFRMA_REPLAY_VAL];
16295424f32eSThomas Graf 	struct nlattr *lt = attrs[XFRMA_LTIME_VAL];
1630d51d081dSJamal Hadi Salim 
1631d51d081dSJamal Hadi Salim 	if (!lt && !rp)
1632d51d081dSJamal Hadi Salim 		return err;
1633d51d081dSJamal Hadi Salim 
1634d51d081dSJamal Hadi Salim 	/* pedantic mode - thou shalt sayeth replaceth */
1635d51d081dSJamal Hadi Salim 	if (!(nlh->nlmsg_flags&NLM_F_REPLACE))
1636d51d081dSJamal Hadi Salim 		return err;
1637d51d081dSJamal Hadi Salim 
1638fc34acd3SAlexey Dobriyan 	x = xfrm_state_lookup(net, &p->sa_id.daddr, p->sa_id.spi, p->sa_id.proto, p->sa_id.family);
1639d51d081dSJamal Hadi Salim 	if (x == NULL)
1640d51d081dSJamal Hadi Salim 		return -ESRCH;
1641d51d081dSJamal Hadi Salim 
1642d51d081dSJamal Hadi Salim 	if (x->km.state != XFRM_STATE_VALID)
1643d51d081dSJamal Hadi Salim 		goto out;
1644d51d081dSJamal Hadi Salim 
1645d51d081dSJamal Hadi Salim 	spin_lock_bh(&x->lock);
164635a7aa08SThomas Graf 	xfrm_update_ae_params(x, attrs);
1647d51d081dSJamal Hadi Salim 	spin_unlock_bh(&x->lock);
1648d51d081dSJamal Hadi Salim 
1649d51d081dSJamal Hadi Salim 	c.event = nlh->nlmsg_type;
1650d51d081dSJamal Hadi Salim 	c.seq = nlh->nlmsg_seq;
1651d51d081dSJamal Hadi Salim 	c.pid = nlh->nlmsg_pid;
1652d51d081dSJamal Hadi Salim 	c.data.aevent = XFRM_AE_CU;
1653d51d081dSJamal Hadi Salim 	km_state_notify(x, &c);
1654d51d081dSJamal Hadi Salim 	err = 0;
1655d51d081dSJamal Hadi Salim out:
1656d51d081dSJamal Hadi Salim 	xfrm_state_put(x);
1657d51d081dSJamal Hadi Salim 	return err;
1658d51d081dSJamal Hadi Salim }
1659d51d081dSJamal Hadi Salim 
166022e70050SChristoph Hellwig static int xfrm_flush_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
16615424f32eSThomas Graf 		struct nlattr **attrs)
16621da177e4SLinus Torvalds {
1663fc34acd3SAlexey Dobriyan 	struct net *net = sock_net(skb->sk);
166426b15dadSJamal Hadi Salim 	struct km_event c;
1665b798a9edSJamal Hadi Salim 	u8 type = XFRM_POLICY_TYPE_MAIN;
1666f7b6983fSMasahide NAKAMURA 	int err;
1667161a09e7SJoy Latten 	struct xfrm_audit audit_info;
166826b15dadSJamal Hadi Salim 
166935a7aa08SThomas Graf 	err = copy_from_user_policy_type(&type, attrs);
1670f7b6983fSMasahide NAKAMURA 	if (err)
1671f7b6983fSMasahide NAKAMURA 		return err;
1672f7b6983fSMasahide NAKAMURA 
1673161a09e7SJoy Latten 	audit_info.loginuid = NETLINK_CB(skb).loginuid;
16742532386fSEric Paris 	audit_info.sessionid = NETLINK_CB(skb).sessionid;
1675161a09e7SJoy Latten 	audit_info.secid = NETLINK_CB(skb).sid;
1676fc34acd3SAlexey Dobriyan 	err = xfrm_policy_flush(net, type, &audit_info);
16774aa2e62cSJoy Latten 	if (err)
16784aa2e62cSJoy Latten 		return err;
1679f7b6983fSMasahide NAKAMURA 	c.data.type = type;
1680f60f6b8fSHerbert Xu 	c.event = nlh->nlmsg_type;
168126b15dadSJamal Hadi Salim 	c.seq = nlh->nlmsg_seq;
168226b15dadSJamal Hadi Salim 	c.pid = nlh->nlmsg_pid;
16837067802eSAlexey Dobriyan 	c.net = net;
168426b15dadSJamal Hadi Salim 	km_policy_notify(NULL, 0, &c);
16851da177e4SLinus Torvalds 	return 0;
16861da177e4SLinus Torvalds }
16871da177e4SLinus Torvalds 
168822e70050SChristoph Hellwig static int xfrm_add_pol_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
16895424f32eSThomas Graf 		struct nlattr **attrs)
16906c5c8ca7SJamal Hadi Salim {
1691fc34acd3SAlexey Dobriyan 	struct net *net = sock_net(skb->sk);
16926c5c8ca7SJamal Hadi Salim 	struct xfrm_policy *xp;
16937b67c857SThomas Graf 	struct xfrm_user_polexpire *up = nlmsg_data(nlh);
16946c5c8ca7SJamal Hadi Salim 	struct xfrm_userpolicy_info *p = &up->pol;
1695b798a9edSJamal Hadi Salim 	u8 type = XFRM_POLICY_TYPE_MAIN;
16966c5c8ca7SJamal Hadi Salim 	int err = -ENOENT;
16976c5c8ca7SJamal Hadi Salim 
169835a7aa08SThomas Graf 	err = copy_from_user_policy_type(&type, attrs);
1699f7b6983fSMasahide NAKAMURA 	if (err)
1700f7b6983fSMasahide NAKAMURA 		return err;
1701f7b6983fSMasahide NAKAMURA 
17026c5c8ca7SJamal Hadi Salim 	if (p->index)
1703fc34acd3SAlexey Dobriyan 		xp = xfrm_policy_byid(net, type, p->dir, p->index, 0, &err);
17046c5c8ca7SJamal Hadi Salim 	else {
17055424f32eSThomas Graf 		struct nlattr *rt = attrs[XFRMA_SEC_CTX];
170603e1ad7bSPaul Moore 		struct xfrm_sec_ctx *ctx;
17076c5c8ca7SJamal Hadi Salim 
170835a7aa08SThomas Graf 		err = verify_sec_ctx_len(attrs);
17096c5c8ca7SJamal Hadi Salim 		if (err)
17106c5c8ca7SJamal Hadi Salim 			return err;
17116c5c8ca7SJamal Hadi Salim 
17122c8dd116SDenis V. Lunev 		ctx = NULL;
17136c5c8ca7SJamal Hadi Salim 		if (rt) {
17145424f32eSThomas Graf 			struct xfrm_user_sec_ctx *uctx = nla_data(rt);
17156c5c8ca7SJamal Hadi Salim 
171603e1ad7bSPaul Moore 			err = security_xfrm_policy_alloc(&ctx, uctx);
171703e1ad7bSPaul Moore 			if (err)
17186c5c8ca7SJamal Hadi Salim 				return err;
17192c8dd116SDenis V. Lunev 		}
1720fc34acd3SAlexey Dobriyan 		xp = xfrm_policy_bysel_ctx(net, type, p->dir, &p->sel, ctx, 0, &err);
172103e1ad7bSPaul Moore 		security_xfrm_policy_free(ctx);
17226c5c8ca7SJamal Hadi Salim 	}
17236c5c8ca7SJamal Hadi Salim 	if (xp == NULL)
1724ef41aaa0SEric Paris 		return -ENOENT;
172503e1ad7bSPaul Moore 
17266c5c8ca7SJamal Hadi Salim 	read_lock(&xp->lock);
172712a169e7SHerbert Xu 	if (xp->walk.dead) {
17286c5c8ca7SJamal Hadi Salim 		read_unlock(&xp->lock);
17296c5c8ca7SJamal Hadi Salim 		goto out;
17306c5c8ca7SJamal Hadi Salim 	}
17316c5c8ca7SJamal Hadi Salim 
17326c5c8ca7SJamal Hadi Salim 	read_unlock(&xp->lock);
17336c5c8ca7SJamal Hadi Salim 	err = 0;
17346c5c8ca7SJamal Hadi Salim 	if (up->hard) {
17352532386fSEric Paris 		uid_t loginuid = NETLINK_CB(skb).loginuid;
17362532386fSEric Paris 		uid_t sessionid = NETLINK_CB(skb).sessionid;
17372532386fSEric Paris 		u32 sid = NETLINK_CB(skb).sid;
17386c5c8ca7SJamal Hadi Salim 		xfrm_policy_delete(xp, p->dir);
17392532386fSEric Paris 		xfrm_audit_policy_delete(xp, 1, loginuid, sessionid, sid);
1740161a09e7SJoy Latten 
17416c5c8ca7SJamal Hadi Salim 	} else {
17426c5c8ca7SJamal Hadi Salim 		// reset the timers here?
17436c5c8ca7SJamal Hadi Salim 		printk("Dont know what to do with soft policy expire\n");
17446c5c8ca7SJamal Hadi Salim 	}
17456c5c8ca7SJamal Hadi Salim 	km_policy_expired(xp, p->dir, up->hard, current->pid);
17466c5c8ca7SJamal Hadi Salim 
17476c5c8ca7SJamal Hadi Salim out:
17486c5c8ca7SJamal Hadi Salim 	xfrm_pol_put(xp);
17496c5c8ca7SJamal Hadi Salim 	return err;
17506c5c8ca7SJamal Hadi Salim }
17516c5c8ca7SJamal Hadi Salim 
175222e70050SChristoph Hellwig static int xfrm_add_sa_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
17535424f32eSThomas Graf 		struct nlattr **attrs)
175453bc6b4dSJamal Hadi Salim {
1755fc34acd3SAlexey Dobriyan 	struct net *net = sock_net(skb->sk);
175653bc6b4dSJamal Hadi Salim 	struct xfrm_state *x;
175753bc6b4dSJamal Hadi Salim 	int err;
17587b67c857SThomas Graf 	struct xfrm_user_expire *ue = nlmsg_data(nlh);
175953bc6b4dSJamal Hadi Salim 	struct xfrm_usersa_info *p = &ue->state;
176053bc6b4dSJamal Hadi Salim 
1761fc34acd3SAlexey Dobriyan 	x = xfrm_state_lookup(net, &p->id.daddr, p->id.spi, p->id.proto, p->family);
176253bc6b4dSJamal Hadi Salim 
17633a765aa5SDavid S. Miller 	err = -ENOENT;
176453bc6b4dSJamal Hadi Salim 	if (x == NULL)
176553bc6b4dSJamal Hadi Salim 		return err;
176653bc6b4dSJamal Hadi Salim 
176753bc6b4dSJamal Hadi Salim 	spin_lock_bh(&x->lock);
17683a765aa5SDavid S. Miller 	err = -EINVAL;
176953bc6b4dSJamal Hadi Salim 	if (x->km.state != XFRM_STATE_VALID)
177053bc6b4dSJamal Hadi Salim 		goto out;
177153bc6b4dSJamal Hadi Salim 	km_state_expired(x, ue->hard, current->pid);
177253bc6b4dSJamal Hadi Salim 
1773161a09e7SJoy Latten 	if (ue->hard) {
17742532386fSEric Paris 		uid_t loginuid = NETLINK_CB(skb).loginuid;
17752532386fSEric Paris 		uid_t sessionid = NETLINK_CB(skb).sessionid;
17762532386fSEric Paris 		u32 sid = NETLINK_CB(skb).sid;
177753bc6b4dSJamal Hadi Salim 		__xfrm_state_delete(x);
17782532386fSEric Paris 		xfrm_audit_state_delete(x, 1, loginuid, sessionid, sid);
1779161a09e7SJoy Latten 	}
17803a765aa5SDavid S. Miller 	err = 0;
178153bc6b4dSJamal Hadi Salim out:
178253bc6b4dSJamal Hadi Salim 	spin_unlock_bh(&x->lock);
178353bc6b4dSJamal Hadi Salim 	xfrm_state_put(x);
178453bc6b4dSJamal Hadi Salim 	return err;
178553bc6b4dSJamal Hadi Salim }
178653bc6b4dSJamal Hadi Salim 
178722e70050SChristoph Hellwig static int xfrm_add_acquire(struct sk_buff *skb, struct nlmsghdr *nlh,
17885424f32eSThomas Graf 		struct nlattr **attrs)
1789980ebd25SJamal Hadi Salim {
1790fc34acd3SAlexey Dobriyan 	struct net *net = sock_net(skb->sk);
1791980ebd25SJamal Hadi Salim 	struct xfrm_policy *xp;
1792980ebd25SJamal Hadi Salim 	struct xfrm_user_tmpl *ut;
1793980ebd25SJamal Hadi Salim 	int i;
17945424f32eSThomas Graf 	struct nlattr *rt = attrs[XFRMA_TMPL];
1795980ebd25SJamal Hadi Salim 
17967b67c857SThomas Graf 	struct xfrm_user_acquire *ua = nlmsg_data(nlh);
1797fc34acd3SAlexey Dobriyan 	struct xfrm_state *x = xfrm_state_alloc(net);
1798980ebd25SJamal Hadi Salim 	int err = -ENOMEM;
1799980ebd25SJamal Hadi Salim 
1800980ebd25SJamal Hadi Salim 	if (!x)
1801d8eb9307SIlpo Järvinen 		goto nomem;
1802980ebd25SJamal Hadi Salim 
1803980ebd25SJamal Hadi Salim 	err = verify_newpolicy_info(&ua->policy);
1804d8eb9307SIlpo Järvinen 	if (err)
1805d8eb9307SIlpo Järvinen 		goto bad_policy;
1806980ebd25SJamal Hadi Salim 
1807980ebd25SJamal Hadi Salim 	/*   build an XP */
1808fc34acd3SAlexey Dobriyan 	xp = xfrm_policy_construct(net, &ua->policy, attrs, &err);
1809d8eb9307SIlpo Järvinen 	if (!xp)
1810d8eb9307SIlpo Järvinen 		goto free_state;
1811980ebd25SJamal Hadi Salim 
1812980ebd25SJamal Hadi Salim 	memcpy(&x->id, &ua->id, sizeof(ua->id));
1813980ebd25SJamal Hadi Salim 	memcpy(&x->props.saddr, &ua->saddr, sizeof(ua->saddr));
1814980ebd25SJamal Hadi Salim 	memcpy(&x->sel, &ua->sel, sizeof(ua->sel));
1815980ebd25SJamal Hadi Salim 
18165424f32eSThomas Graf 	ut = nla_data(rt);
1817980ebd25SJamal Hadi Salim 	/* extract the templates and for each call km_key */
1818980ebd25SJamal Hadi Salim 	for (i = 0; i < xp->xfrm_nr; i++, ut++) {
1819980ebd25SJamal Hadi Salim 		struct xfrm_tmpl *t = &xp->xfrm_vec[i];
1820980ebd25SJamal Hadi Salim 		memcpy(&x->id, &t->id, sizeof(x->id));
1821980ebd25SJamal Hadi Salim 		x->props.mode = t->mode;
1822980ebd25SJamal Hadi Salim 		x->props.reqid = t->reqid;
1823980ebd25SJamal Hadi Salim 		x->props.family = ut->family;
1824980ebd25SJamal Hadi Salim 		t->aalgos = ua->aalgos;
1825980ebd25SJamal Hadi Salim 		t->ealgos = ua->ealgos;
1826980ebd25SJamal Hadi Salim 		t->calgos = ua->calgos;
1827980ebd25SJamal Hadi Salim 		err = km_query(x, t, xp);
1828980ebd25SJamal Hadi Salim 
1829980ebd25SJamal Hadi Salim 	}
1830980ebd25SJamal Hadi Salim 
1831980ebd25SJamal Hadi Salim 	kfree(x);
1832980ebd25SJamal Hadi Salim 	kfree(xp);
1833980ebd25SJamal Hadi Salim 
1834980ebd25SJamal Hadi Salim 	return 0;
1835d8eb9307SIlpo Järvinen 
1836d8eb9307SIlpo Järvinen bad_policy:
1837d8eb9307SIlpo Järvinen 	printk("BAD policy passed\n");
1838d8eb9307SIlpo Järvinen free_state:
1839d8eb9307SIlpo Järvinen 	kfree(x);
1840d8eb9307SIlpo Järvinen nomem:
1841d8eb9307SIlpo Järvinen 	return err;
1842980ebd25SJamal Hadi Salim }
1843980ebd25SJamal Hadi Salim 
18445c79de6eSShinta Sugimoto #ifdef CONFIG_XFRM_MIGRATE
18455c79de6eSShinta Sugimoto static int copy_from_user_migrate(struct xfrm_migrate *ma,
184613c1d189SArnaud Ebalard 				  struct xfrm_kmaddress *k,
18475424f32eSThomas Graf 				  struct nlattr **attrs, int *num)
18485c79de6eSShinta Sugimoto {
18495424f32eSThomas Graf 	struct nlattr *rt = attrs[XFRMA_MIGRATE];
18505c79de6eSShinta Sugimoto 	struct xfrm_user_migrate *um;
18515c79de6eSShinta Sugimoto 	int i, num_migrate;
18525c79de6eSShinta Sugimoto 
185313c1d189SArnaud Ebalard 	if (k != NULL) {
185413c1d189SArnaud Ebalard 		struct xfrm_user_kmaddress *uk;
185513c1d189SArnaud Ebalard 
185613c1d189SArnaud Ebalard 		uk = nla_data(attrs[XFRMA_KMADDRESS]);
185713c1d189SArnaud Ebalard 		memcpy(&k->local, &uk->local, sizeof(k->local));
185813c1d189SArnaud Ebalard 		memcpy(&k->remote, &uk->remote, sizeof(k->remote));
185913c1d189SArnaud Ebalard 		k->family = uk->family;
186013c1d189SArnaud Ebalard 		k->reserved = uk->reserved;
186113c1d189SArnaud Ebalard 	}
186213c1d189SArnaud Ebalard 
18635424f32eSThomas Graf 	um = nla_data(rt);
18645424f32eSThomas Graf 	num_migrate = nla_len(rt) / sizeof(*um);
18655c79de6eSShinta Sugimoto 
18665c79de6eSShinta Sugimoto 	if (num_migrate <= 0 || num_migrate > XFRM_MAX_DEPTH)
18675c79de6eSShinta Sugimoto 		return -EINVAL;
18685c79de6eSShinta Sugimoto 
18695c79de6eSShinta Sugimoto 	for (i = 0; i < num_migrate; i++, um++, ma++) {
18705c79de6eSShinta Sugimoto 		memcpy(&ma->old_daddr, &um->old_daddr, sizeof(ma->old_daddr));
18715c79de6eSShinta Sugimoto 		memcpy(&ma->old_saddr, &um->old_saddr, sizeof(ma->old_saddr));
18725c79de6eSShinta Sugimoto 		memcpy(&ma->new_daddr, &um->new_daddr, sizeof(ma->new_daddr));
18735c79de6eSShinta Sugimoto 		memcpy(&ma->new_saddr, &um->new_saddr, sizeof(ma->new_saddr));
18745c79de6eSShinta Sugimoto 
18755c79de6eSShinta Sugimoto 		ma->proto = um->proto;
18765c79de6eSShinta Sugimoto 		ma->mode = um->mode;
18775c79de6eSShinta Sugimoto 		ma->reqid = um->reqid;
18785c79de6eSShinta Sugimoto 
18795c79de6eSShinta Sugimoto 		ma->old_family = um->old_family;
18805c79de6eSShinta Sugimoto 		ma->new_family = um->new_family;
18815c79de6eSShinta Sugimoto 	}
18825c79de6eSShinta Sugimoto 
18835c79de6eSShinta Sugimoto 	*num = i;
18845c79de6eSShinta Sugimoto 	return 0;
18855c79de6eSShinta Sugimoto }
18865c79de6eSShinta Sugimoto 
18875c79de6eSShinta Sugimoto static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh,
18885424f32eSThomas Graf 			   struct nlattr **attrs)
18895c79de6eSShinta Sugimoto {
18907b67c857SThomas Graf 	struct xfrm_userpolicy_id *pi = nlmsg_data(nlh);
18915c79de6eSShinta Sugimoto 	struct xfrm_migrate m[XFRM_MAX_DEPTH];
189213c1d189SArnaud Ebalard 	struct xfrm_kmaddress km, *kmp;
18935c79de6eSShinta Sugimoto 	u8 type;
18945c79de6eSShinta Sugimoto 	int err;
18955c79de6eSShinta Sugimoto 	int n = 0;
18965c79de6eSShinta Sugimoto 
189735a7aa08SThomas Graf 	if (attrs[XFRMA_MIGRATE] == NULL)
1898cf5cb79fSThomas Graf 		return -EINVAL;
18995c79de6eSShinta Sugimoto 
190013c1d189SArnaud Ebalard 	kmp = attrs[XFRMA_KMADDRESS] ? &km : NULL;
190113c1d189SArnaud Ebalard 
19025424f32eSThomas Graf 	err = copy_from_user_policy_type(&type, attrs);
19035c79de6eSShinta Sugimoto 	if (err)
19045c79de6eSShinta Sugimoto 		return err;
19055c79de6eSShinta Sugimoto 
190613c1d189SArnaud Ebalard 	err = copy_from_user_migrate((struct xfrm_migrate *)m, kmp, attrs, &n);
19075c79de6eSShinta Sugimoto 	if (err)
19085c79de6eSShinta Sugimoto 		return err;
19095c79de6eSShinta Sugimoto 
19105c79de6eSShinta Sugimoto 	if (!n)
19115c79de6eSShinta Sugimoto 		return 0;
19125c79de6eSShinta Sugimoto 
191313c1d189SArnaud Ebalard 	xfrm_migrate(&pi->sel, pi->dir, type, m, n, kmp);
19145c79de6eSShinta Sugimoto 
19155c79de6eSShinta Sugimoto 	return 0;
19165c79de6eSShinta Sugimoto }
19175c79de6eSShinta Sugimoto #else
19185c79de6eSShinta Sugimoto static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh,
19195424f32eSThomas Graf 			   struct nlattr **attrs)
19205c79de6eSShinta Sugimoto {
19215c79de6eSShinta Sugimoto 	return -ENOPROTOOPT;
19225c79de6eSShinta Sugimoto }
19235c79de6eSShinta Sugimoto #endif
19245c79de6eSShinta Sugimoto 
19255c79de6eSShinta Sugimoto #ifdef CONFIG_XFRM_MIGRATE
19265c79de6eSShinta Sugimoto static int copy_to_user_migrate(struct xfrm_migrate *m, struct sk_buff *skb)
19275c79de6eSShinta Sugimoto {
19285c79de6eSShinta Sugimoto 	struct xfrm_user_migrate um;
19295c79de6eSShinta Sugimoto 
19305c79de6eSShinta Sugimoto 	memset(&um, 0, sizeof(um));
19315c79de6eSShinta Sugimoto 	um.proto = m->proto;
19325c79de6eSShinta Sugimoto 	um.mode = m->mode;
19335c79de6eSShinta Sugimoto 	um.reqid = m->reqid;
19345c79de6eSShinta Sugimoto 	um.old_family = m->old_family;
19355c79de6eSShinta Sugimoto 	memcpy(&um.old_daddr, &m->old_daddr, sizeof(um.old_daddr));
19365c79de6eSShinta Sugimoto 	memcpy(&um.old_saddr, &m->old_saddr, sizeof(um.old_saddr));
19375c79de6eSShinta Sugimoto 	um.new_family = m->new_family;
19385c79de6eSShinta Sugimoto 	memcpy(&um.new_daddr, &m->new_daddr, sizeof(um.new_daddr));
19395c79de6eSShinta Sugimoto 	memcpy(&um.new_saddr, &m->new_saddr, sizeof(um.new_saddr));
19405c79de6eSShinta Sugimoto 
1941c0144beaSThomas Graf 	return nla_put(skb, XFRMA_MIGRATE, sizeof(um), &um);
19425c79de6eSShinta Sugimoto }
19435c79de6eSShinta Sugimoto 
194413c1d189SArnaud Ebalard static int copy_to_user_kmaddress(struct xfrm_kmaddress *k, struct sk_buff *skb)
194513c1d189SArnaud Ebalard {
194613c1d189SArnaud Ebalard 	struct xfrm_user_kmaddress uk;
194713c1d189SArnaud Ebalard 
194813c1d189SArnaud Ebalard 	memset(&uk, 0, sizeof(uk));
194913c1d189SArnaud Ebalard 	uk.family = k->family;
195013c1d189SArnaud Ebalard 	uk.reserved = k->reserved;
195113c1d189SArnaud Ebalard 	memcpy(&uk.local, &k->local, sizeof(uk.local));
1952a1caa322SArnaud Ebalard 	memcpy(&uk.remote, &k->remote, sizeof(uk.remote));
195313c1d189SArnaud Ebalard 
195413c1d189SArnaud Ebalard 	return nla_put(skb, XFRMA_KMADDRESS, sizeof(uk), &uk);
195513c1d189SArnaud Ebalard }
195613c1d189SArnaud Ebalard 
195713c1d189SArnaud Ebalard static inline size_t xfrm_migrate_msgsize(int num_migrate, int with_kma)
19587deb2264SThomas Graf {
19597deb2264SThomas Graf 	return NLMSG_ALIGN(sizeof(struct xfrm_userpolicy_id))
196013c1d189SArnaud Ebalard 	      + (with_kma ? nla_total_size(sizeof(struct xfrm_kmaddress)) : 0)
19617deb2264SThomas Graf 	      + nla_total_size(sizeof(struct xfrm_user_migrate) * num_migrate)
19627deb2264SThomas Graf 	      + userpolicy_type_attrsize();
19637deb2264SThomas Graf }
19647deb2264SThomas Graf 
19655c79de6eSShinta Sugimoto static int build_migrate(struct sk_buff *skb, struct xfrm_migrate *m,
196613c1d189SArnaud Ebalard 			 int num_migrate, struct xfrm_kmaddress *k,
196713c1d189SArnaud Ebalard 			 struct xfrm_selector *sel, u8 dir, u8 type)
19685c79de6eSShinta Sugimoto {
19695c79de6eSShinta Sugimoto 	struct xfrm_migrate *mp;
19705c79de6eSShinta Sugimoto 	struct xfrm_userpolicy_id *pol_id;
19715c79de6eSShinta Sugimoto 	struct nlmsghdr *nlh;
19725c79de6eSShinta Sugimoto 	int i;
19735c79de6eSShinta Sugimoto 
197479b8b7f4SThomas Graf 	nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_MIGRATE, sizeof(*pol_id), 0);
197579b8b7f4SThomas Graf 	if (nlh == NULL)
197679b8b7f4SThomas Graf 		return -EMSGSIZE;
19775c79de6eSShinta Sugimoto 
19787b67c857SThomas Graf 	pol_id = nlmsg_data(nlh);
19795c79de6eSShinta Sugimoto 	/* copy data from selector, dir, and type to the pol_id */
19805c79de6eSShinta Sugimoto 	memset(pol_id, 0, sizeof(*pol_id));
19815c79de6eSShinta Sugimoto 	memcpy(&pol_id->sel, sel, sizeof(pol_id->sel));
19825c79de6eSShinta Sugimoto 	pol_id->dir = dir;
19835c79de6eSShinta Sugimoto 
198413c1d189SArnaud Ebalard 	if (k != NULL && (copy_to_user_kmaddress(k, skb) < 0))
198513c1d189SArnaud Ebalard 			goto nlmsg_failure;
198613c1d189SArnaud Ebalard 
19875c79de6eSShinta Sugimoto 	if (copy_to_user_policy_type(type, skb) < 0)
19885c79de6eSShinta Sugimoto 		goto nlmsg_failure;
19895c79de6eSShinta Sugimoto 
19905c79de6eSShinta Sugimoto 	for (i = 0, mp = m ; i < num_migrate; i++, mp++) {
19915c79de6eSShinta Sugimoto 		if (copy_to_user_migrate(mp, skb) < 0)
19925c79de6eSShinta Sugimoto 			goto nlmsg_failure;
19935c79de6eSShinta Sugimoto 	}
19945c79de6eSShinta Sugimoto 
19959825069dSThomas Graf 	return nlmsg_end(skb, nlh);
19965c79de6eSShinta Sugimoto nlmsg_failure:
19979825069dSThomas Graf 	nlmsg_cancel(skb, nlh);
19989825069dSThomas Graf 	return -EMSGSIZE;
19995c79de6eSShinta Sugimoto }
20005c79de6eSShinta Sugimoto 
20015c79de6eSShinta Sugimoto static int xfrm_send_migrate(struct xfrm_selector *sel, u8 dir, u8 type,
200213c1d189SArnaud Ebalard 			     struct xfrm_migrate *m, int num_migrate,
200313c1d189SArnaud Ebalard 			     struct xfrm_kmaddress *k)
20045c79de6eSShinta Sugimoto {
2005a6483b79SAlexey Dobriyan 	struct net *net = &init_net;
20065c79de6eSShinta Sugimoto 	struct sk_buff *skb;
20075c79de6eSShinta Sugimoto 
200813c1d189SArnaud Ebalard 	skb = nlmsg_new(xfrm_migrate_msgsize(num_migrate, !!k), GFP_ATOMIC);
20095c79de6eSShinta Sugimoto 	if (skb == NULL)
20105c79de6eSShinta Sugimoto 		return -ENOMEM;
20115c79de6eSShinta Sugimoto 
20125c79de6eSShinta Sugimoto 	/* build migrate */
201313c1d189SArnaud Ebalard 	if (build_migrate(skb, m, num_migrate, k, sel, dir, type) < 0)
20145c79de6eSShinta Sugimoto 		BUG();
20155c79de6eSShinta Sugimoto 
2016a6483b79SAlexey Dobriyan 	return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_MIGRATE, GFP_ATOMIC);
20175c79de6eSShinta Sugimoto }
20185c79de6eSShinta Sugimoto #else
20195c79de6eSShinta Sugimoto static int xfrm_send_migrate(struct xfrm_selector *sel, u8 dir, u8 type,
202013c1d189SArnaud Ebalard 			     struct xfrm_migrate *m, int num_migrate,
202113c1d189SArnaud Ebalard 			     struct xfrm_kmaddress *k)
20225c79de6eSShinta Sugimoto {
20235c79de6eSShinta Sugimoto 	return -ENOPROTOOPT;
20245c79de6eSShinta Sugimoto }
20255c79de6eSShinta Sugimoto #endif
2026d51d081dSJamal Hadi Salim 
2027a7bd9a45SThomas Graf #define XMSGSIZE(type) sizeof(struct type)
2028492b558bSThomas Graf 
2029492b558bSThomas Graf static const int xfrm_msg_min[XFRM_NR_MSGTYPES] = {
203066f9a259SDavid S. Miller 	[XFRM_MSG_NEWSA       - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
2031492b558bSThomas Graf 	[XFRM_MSG_DELSA       - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
2032492b558bSThomas Graf 	[XFRM_MSG_GETSA       - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
2033492b558bSThomas Graf 	[XFRM_MSG_NEWPOLICY   - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
2034492b558bSThomas Graf 	[XFRM_MSG_DELPOLICY   - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
2035492b558bSThomas Graf 	[XFRM_MSG_GETPOLICY   - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
2036492b558bSThomas Graf 	[XFRM_MSG_ALLOCSPI    - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userspi_info),
2037980ebd25SJamal Hadi Salim 	[XFRM_MSG_ACQUIRE     - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_acquire),
203853bc6b4dSJamal Hadi Salim 	[XFRM_MSG_EXPIRE      - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_expire),
2039492b558bSThomas Graf 	[XFRM_MSG_UPDPOLICY   - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
204066f9a259SDavid S. Miller 	[XFRM_MSG_UPDSA       - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
20416c5c8ca7SJamal Hadi Salim 	[XFRM_MSG_POLEXPIRE   - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_polexpire),
2042492b558bSThomas Graf 	[XFRM_MSG_FLUSHSA     - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_flush),
2043a7bd9a45SThomas Graf 	[XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = 0,
2044d51d081dSJamal Hadi Salim 	[XFRM_MSG_NEWAE       - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
2045d51d081dSJamal Hadi Salim 	[XFRM_MSG_GETAE       - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
204697a64b45SMasahide NAKAMURA 	[XFRM_MSG_REPORT      - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_report),
20475c79de6eSShinta Sugimoto 	[XFRM_MSG_MIGRATE     - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
2048a7bd9a45SThomas Graf 	[XFRM_MSG_GETSADINFO  - XFRM_MSG_BASE] = sizeof(u32),
2049a7bd9a45SThomas Graf 	[XFRM_MSG_GETSPDINFO  - XFRM_MSG_BASE] = sizeof(u32),
20501da177e4SLinus Torvalds };
20511da177e4SLinus Torvalds 
2052492b558bSThomas Graf #undef XMSGSIZE
2053492b558bSThomas Graf 
2054cf5cb79fSThomas Graf static const struct nla_policy xfrma_policy[XFRMA_MAX+1] = {
20551a6509d9SHerbert Xu 	[XFRMA_ALG_AEAD]	= { .len = sizeof(struct xfrm_algo_aead) },
2056cf5cb79fSThomas Graf 	[XFRMA_ALG_AUTH]	= { .len = sizeof(struct xfrm_algo) },
2057cf5cb79fSThomas Graf 	[XFRMA_ALG_CRYPT]	= { .len = sizeof(struct xfrm_algo) },
2058cf5cb79fSThomas Graf 	[XFRMA_ALG_COMP]	= { .len = sizeof(struct xfrm_algo) },
2059cf5cb79fSThomas Graf 	[XFRMA_ENCAP]		= { .len = sizeof(struct xfrm_encap_tmpl) },
2060cf5cb79fSThomas Graf 	[XFRMA_TMPL]		= { .len = sizeof(struct xfrm_user_tmpl) },
2061cf5cb79fSThomas Graf 	[XFRMA_SEC_CTX]		= { .len = sizeof(struct xfrm_sec_ctx) },
2062cf5cb79fSThomas Graf 	[XFRMA_LTIME_VAL]	= { .len = sizeof(struct xfrm_lifetime_cur) },
2063cf5cb79fSThomas Graf 	[XFRMA_REPLAY_VAL]	= { .len = sizeof(struct xfrm_replay_state) },
2064cf5cb79fSThomas Graf 	[XFRMA_REPLAY_THRESH]	= { .type = NLA_U32 },
2065cf5cb79fSThomas Graf 	[XFRMA_ETIMER_THRESH]	= { .type = NLA_U32 },
2066cf5cb79fSThomas Graf 	[XFRMA_SRCADDR]		= { .len = sizeof(xfrm_address_t) },
2067cf5cb79fSThomas Graf 	[XFRMA_COADDR]		= { .len = sizeof(xfrm_address_t) },
2068cf5cb79fSThomas Graf 	[XFRMA_POLICY_TYPE]	= { .len = sizeof(struct xfrm_userpolicy_type)},
2069cf5cb79fSThomas Graf 	[XFRMA_MIGRATE]		= { .len = sizeof(struct xfrm_user_migrate) },
207013c1d189SArnaud Ebalard 	[XFRMA_KMADDRESS]	= { .len = sizeof(struct xfrm_user_kmaddress) },
2071cf5cb79fSThomas Graf };
2072cf5cb79fSThomas Graf 
20731da177e4SLinus Torvalds static struct xfrm_link {
20745424f32eSThomas Graf 	int (*doit)(struct sk_buff *, struct nlmsghdr *, struct nlattr **);
20751da177e4SLinus Torvalds 	int (*dump)(struct sk_buff *, struct netlink_callback *);
20764c563f76STimo Teras 	int (*done)(struct netlink_callback *);
2077492b558bSThomas Graf } xfrm_dispatch[XFRM_NR_MSGTYPES] = {
2078492b558bSThomas Graf 	[XFRM_MSG_NEWSA       - XFRM_MSG_BASE] = { .doit = xfrm_add_sa        },
2079492b558bSThomas Graf 	[XFRM_MSG_DELSA       - XFRM_MSG_BASE] = { .doit = xfrm_del_sa        },
2080492b558bSThomas Graf 	[XFRM_MSG_GETSA       - XFRM_MSG_BASE] = { .doit = xfrm_get_sa,
20814c563f76STimo Teras 						   .dump = xfrm_dump_sa,
20824c563f76STimo Teras 						   .done = xfrm_dump_sa_done  },
2083492b558bSThomas Graf 	[XFRM_MSG_NEWPOLICY   - XFRM_MSG_BASE] = { .doit = xfrm_add_policy    },
2084492b558bSThomas Graf 	[XFRM_MSG_DELPOLICY   - XFRM_MSG_BASE] = { .doit = xfrm_get_policy    },
2085492b558bSThomas Graf 	[XFRM_MSG_GETPOLICY   - XFRM_MSG_BASE] = { .doit = xfrm_get_policy,
20864c563f76STimo Teras 						   .dump = xfrm_dump_policy,
20874c563f76STimo Teras 						   .done = xfrm_dump_policy_done },
2088492b558bSThomas Graf 	[XFRM_MSG_ALLOCSPI    - XFRM_MSG_BASE] = { .doit = xfrm_alloc_userspi },
2089980ebd25SJamal Hadi Salim 	[XFRM_MSG_ACQUIRE     - XFRM_MSG_BASE] = { .doit = xfrm_add_acquire   },
209053bc6b4dSJamal Hadi Salim 	[XFRM_MSG_EXPIRE      - XFRM_MSG_BASE] = { .doit = xfrm_add_sa_expire },
2091492b558bSThomas Graf 	[XFRM_MSG_UPDPOLICY   - XFRM_MSG_BASE] = { .doit = xfrm_add_policy    },
2092492b558bSThomas Graf 	[XFRM_MSG_UPDSA       - XFRM_MSG_BASE] = { .doit = xfrm_add_sa        },
20936c5c8ca7SJamal Hadi Salim 	[XFRM_MSG_POLEXPIRE   - XFRM_MSG_BASE] = { .doit = xfrm_add_pol_expire},
2094492b558bSThomas Graf 	[XFRM_MSG_FLUSHSA     - XFRM_MSG_BASE] = { .doit = xfrm_flush_sa      },
2095492b558bSThomas Graf 	[XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_flush_policy  },
2096d51d081dSJamal Hadi Salim 	[XFRM_MSG_NEWAE       - XFRM_MSG_BASE] = { .doit = xfrm_new_ae  },
2097d51d081dSJamal Hadi Salim 	[XFRM_MSG_GETAE       - XFRM_MSG_BASE] = { .doit = xfrm_get_ae  },
20985c79de6eSShinta Sugimoto 	[XFRM_MSG_MIGRATE     - XFRM_MSG_BASE] = { .doit = xfrm_do_migrate    },
209928d8909bSJamal Hadi Salim 	[XFRM_MSG_GETSADINFO  - XFRM_MSG_BASE] = { .doit = xfrm_get_sadinfo   },
2100ecfd6b18SJamal Hadi Salim 	[XFRM_MSG_GETSPDINFO  - XFRM_MSG_BASE] = { .doit = xfrm_get_spdinfo   },
21011da177e4SLinus Torvalds };
21021da177e4SLinus Torvalds 
21031d00a4ebSThomas Graf static int xfrm_user_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
21041da177e4SLinus Torvalds {
2105a6483b79SAlexey Dobriyan 	struct net *net = sock_net(skb->sk);
210635a7aa08SThomas Graf 	struct nlattr *attrs[XFRMA_MAX+1];
21071da177e4SLinus Torvalds 	struct xfrm_link *link;
2108a7bd9a45SThomas Graf 	int type, err;
21091da177e4SLinus Torvalds 
21101da177e4SLinus Torvalds 	type = nlh->nlmsg_type;
21111da177e4SLinus Torvalds 	if (type > XFRM_MSG_MAX)
21121d00a4ebSThomas Graf 		return -EINVAL;
21131da177e4SLinus Torvalds 
21141da177e4SLinus Torvalds 	type -= XFRM_MSG_BASE;
21151da177e4SLinus Torvalds 	link = &xfrm_dispatch[type];
21161da177e4SLinus Torvalds 
21171da177e4SLinus Torvalds 	/* All operations require privileges, even GET */
21181d00a4ebSThomas Graf 	if (security_netlink_recv(skb, CAP_NET_ADMIN))
21191d00a4ebSThomas Graf 		return -EPERM;
21201da177e4SLinus Torvalds 
2121492b558bSThomas Graf 	if ((type == (XFRM_MSG_GETSA - XFRM_MSG_BASE) ||
2122492b558bSThomas Graf 	     type == (XFRM_MSG_GETPOLICY - XFRM_MSG_BASE)) &&
2123492b558bSThomas Graf 	    (nlh->nlmsg_flags & NLM_F_DUMP)) {
21241da177e4SLinus Torvalds 		if (link->dump == NULL)
21251d00a4ebSThomas Graf 			return -EINVAL;
21261da177e4SLinus Torvalds 
2127a6483b79SAlexey Dobriyan 		return netlink_dump_start(net->xfrm.nlsk, skb, nlh, link->dump, link->done);
21281da177e4SLinus Torvalds 	}
21291da177e4SLinus Torvalds 
213035a7aa08SThomas Graf 	err = nlmsg_parse(nlh, xfrm_msg_min[type], attrs, XFRMA_MAX,
2131cf5cb79fSThomas Graf 			  xfrma_policy);
2132a7bd9a45SThomas Graf 	if (err < 0)
2133a7bd9a45SThomas Graf 		return err;
21341da177e4SLinus Torvalds 
21351da177e4SLinus Torvalds 	if (link->doit == NULL)
21361d00a4ebSThomas Graf 		return -EINVAL;
21371da177e4SLinus Torvalds 
21385424f32eSThomas Graf 	return link->doit(skb, nlh, attrs);
21391da177e4SLinus Torvalds }
21401da177e4SLinus Torvalds 
2141cd40b7d3SDenis V. Lunev static void xfrm_netlink_rcv(struct sk_buff *skb)
21421da177e4SLinus Torvalds {
21434a3e2f71SArjan van de Ven 	mutex_lock(&xfrm_cfg_mutex);
2144cd40b7d3SDenis V. Lunev 	netlink_rcv_skb(skb, &xfrm_user_rcv_msg);
21454a3e2f71SArjan van de Ven 	mutex_unlock(&xfrm_cfg_mutex);
21461da177e4SLinus Torvalds }
21471da177e4SLinus Torvalds 
21487deb2264SThomas Graf static inline size_t xfrm_expire_msgsize(void)
21497deb2264SThomas Graf {
21507deb2264SThomas Graf 	return NLMSG_ALIGN(sizeof(struct xfrm_user_expire));
21517deb2264SThomas Graf }
21527deb2264SThomas Graf 
2153d51d081dSJamal Hadi Salim static int build_expire(struct sk_buff *skb, struct xfrm_state *x, struct km_event *c)
21541da177e4SLinus Torvalds {
21551da177e4SLinus Torvalds 	struct xfrm_user_expire *ue;
21561da177e4SLinus Torvalds 	struct nlmsghdr *nlh;
21571da177e4SLinus Torvalds 
215879b8b7f4SThomas Graf 	nlh = nlmsg_put(skb, c->pid, 0, XFRM_MSG_EXPIRE, sizeof(*ue), 0);
215979b8b7f4SThomas Graf 	if (nlh == NULL)
216079b8b7f4SThomas Graf 		return -EMSGSIZE;
21611da177e4SLinus Torvalds 
21627b67c857SThomas Graf 	ue = nlmsg_data(nlh);
21631da177e4SLinus Torvalds 	copy_to_user_state(x, &ue->state);
2164d51d081dSJamal Hadi Salim 	ue->hard = (c->data.hard != 0) ? 1 : 0;
21651da177e4SLinus Torvalds 
21669825069dSThomas Graf 	return nlmsg_end(skb, nlh);
21671da177e4SLinus Torvalds }
21681da177e4SLinus Torvalds 
216926b15dadSJamal Hadi Salim static int xfrm_exp_state_notify(struct xfrm_state *x, struct km_event *c)
21701da177e4SLinus Torvalds {
2171fc34acd3SAlexey Dobriyan 	struct net *net = xs_net(x);
21721da177e4SLinus Torvalds 	struct sk_buff *skb;
21731da177e4SLinus Torvalds 
21747deb2264SThomas Graf 	skb = nlmsg_new(xfrm_expire_msgsize(), GFP_ATOMIC);
21751da177e4SLinus Torvalds 	if (skb == NULL)
21761da177e4SLinus Torvalds 		return -ENOMEM;
21771da177e4SLinus Torvalds 
2178d51d081dSJamal Hadi Salim 	if (build_expire(skb, x, c) < 0)
21791da177e4SLinus Torvalds 		BUG();
21801da177e4SLinus Torvalds 
2181a6483b79SAlexey Dobriyan 	return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_EXPIRE, GFP_ATOMIC);
21821da177e4SLinus Torvalds }
21831da177e4SLinus Torvalds 
2184d51d081dSJamal Hadi Salim static int xfrm_aevent_state_notify(struct xfrm_state *x, struct km_event *c)
2185d51d081dSJamal Hadi Salim {
2186fc34acd3SAlexey Dobriyan 	struct net *net = xs_net(x);
2187d51d081dSJamal Hadi Salim 	struct sk_buff *skb;
2188d51d081dSJamal Hadi Salim 
21897deb2264SThomas Graf 	skb = nlmsg_new(xfrm_aevent_msgsize(), GFP_ATOMIC);
2190d51d081dSJamal Hadi Salim 	if (skb == NULL)
2191d51d081dSJamal Hadi Salim 		return -ENOMEM;
2192d51d081dSJamal Hadi Salim 
2193d51d081dSJamal Hadi Salim 	if (build_aevent(skb, x, c) < 0)
2194d51d081dSJamal Hadi Salim 		BUG();
2195d51d081dSJamal Hadi Salim 
2196a6483b79SAlexey Dobriyan 	return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_AEVENTS, GFP_ATOMIC);
2197d51d081dSJamal Hadi Salim }
2198d51d081dSJamal Hadi Salim 
219926b15dadSJamal Hadi Salim static int xfrm_notify_sa_flush(struct km_event *c)
220026b15dadSJamal Hadi Salim {
22017067802eSAlexey Dobriyan 	struct net *net = c->net;
220226b15dadSJamal Hadi Salim 	struct xfrm_usersa_flush *p;
220326b15dadSJamal Hadi Salim 	struct nlmsghdr *nlh;
220426b15dadSJamal Hadi Salim 	struct sk_buff *skb;
22057deb2264SThomas Graf 	int len = NLMSG_ALIGN(sizeof(struct xfrm_usersa_flush));
220626b15dadSJamal Hadi Salim 
22077deb2264SThomas Graf 	skb = nlmsg_new(len, GFP_ATOMIC);
220826b15dadSJamal Hadi Salim 	if (skb == NULL)
220926b15dadSJamal Hadi Salim 		return -ENOMEM;
221026b15dadSJamal Hadi Salim 
221179b8b7f4SThomas Graf 	nlh = nlmsg_put(skb, c->pid, c->seq, XFRM_MSG_FLUSHSA, sizeof(*p), 0);
221279b8b7f4SThomas Graf 	if (nlh == NULL) {
221379b8b7f4SThomas Graf 		kfree_skb(skb);
221479b8b7f4SThomas Graf 		return -EMSGSIZE;
221579b8b7f4SThomas Graf 	}
221626b15dadSJamal Hadi Salim 
22177b67c857SThomas Graf 	p = nlmsg_data(nlh);
2218bf08867fSHerbert Xu 	p->proto = c->data.proto;
221926b15dadSJamal Hadi Salim 
22209825069dSThomas Graf 	nlmsg_end(skb, nlh);
222126b15dadSJamal Hadi Salim 
2222a6483b79SAlexey Dobriyan 	return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_SA, GFP_ATOMIC);
222326b15dadSJamal Hadi Salim }
222426b15dadSJamal Hadi Salim 
22257deb2264SThomas Graf static inline size_t xfrm_sa_len(struct xfrm_state *x)
222626b15dadSJamal Hadi Salim {
22277deb2264SThomas Graf 	size_t l = 0;
22281a6509d9SHerbert Xu 	if (x->aead)
22291a6509d9SHerbert Xu 		l += nla_total_size(aead_len(x->aead));
2230*4447bb33SMartin Willi 	if (x->aalg) {
2231*4447bb33SMartin Willi 		l += nla_total_size(sizeof(struct xfrm_algo) +
2232*4447bb33SMartin Willi 				    (x->aalg->alg_key_len + 7) / 8);
2233*4447bb33SMartin Willi 		l += nla_total_size(xfrm_alg_auth_len(x->aalg));
2234*4447bb33SMartin Willi 	}
223526b15dadSJamal Hadi Salim 	if (x->ealg)
22360f99be0dSEric Dumazet 		l += nla_total_size(xfrm_alg_len(x->ealg));
223726b15dadSJamal Hadi Salim 	if (x->calg)
22387deb2264SThomas Graf 		l += nla_total_size(sizeof(*x->calg));
223926b15dadSJamal Hadi Salim 	if (x->encap)
22407deb2264SThomas Graf 		l += nla_total_size(sizeof(*x->encap));
224168325d3bSHerbert Xu 	if (x->security)
224268325d3bSHerbert Xu 		l += nla_total_size(sizeof(struct xfrm_user_sec_ctx) +
224368325d3bSHerbert Xu 				    x->security->ctx_len);
224468325d3bSHerbert Xu 	if (x->coaddr)
224568325d3bSHerbert Xu 		l += nla_total_size(sizeof(*x->coaddr));
224668325d3bSHerbert Xu 
2247d26f3984SHerbert Xu 	/* Must count x->lastused as it may become non-zero behind our back. */
2248d26f3984SHerbert Xu 	l += nla_total_size(sizeof(u64));
224926b15dadSJamal Hadi Salim 
225026b15dadSJamal Hadi Salim 	return l;
225126b15dadSJamal Hadi Salim }
225226b15dadSJamal Hadi Salim 
225326b15dadSJamal Hadi Salim static int xfrm_notify_sa(struct xfrm_state *x, struct km_event *c)
225426b15dadSJamal Hadi Salim {
2255fc34acd3SAlexey Dobriyan 	struct net *net = xs_net(x);
225626b15dadSJamal Hadi Salim 	struct xfrm_usersa_info *p;
22570603eac0SHerbert Xu 	struct xfrm_usersa_id *id;
225826b15dadSJamal Hadi Salim 	struct nlmsghdr *nlh;
225926b15dadSJamal Hadi Salim 	struct sk_buff *skb;
226026b15dadSJamal Hadi Salim 	int len = xfrm_sa_len(x);
22610603eac0SHerbert Xu 	int headlen;
22620603eac0SHerbert Xu 
22630603eac0SHerbert Xu 	headlen = sizeof(*p);
22640603eac0SHerbert Xu 	if (c->event == XFRM_MSG_DELSA) {
22657deb2264SThomas Graf 		len += nla_total_size(headlen);
22660603eac0SHerbert Xu 		headlen = sizeof(*id);
22670603eac0SHerbert Xu 	}
22687deb2264SThomas Graf 	len += NLMSG_ALIGN(headlen);
226926b15dadSJamal Hadi Salim 
22707deb2264SThomas Graf 	skb = nlmsg_new(len, GFP_ATOMIC);
227126b15dadSJamal Hadi Salim 	if (skb == NULL)
227226b15dadSJamal Hadi Salim 		return -ENOMEM;
227326b15dadSJamal Hadi Salim 
227479b8b7f4SThomas Graf 	nlh = nlmsg_put(skb, c->pid, c->seq, c->event, headlen, 0);
227579b8b7f4SThomas Graf 	if (nlh == NULL)
2276c0144beaSThomas Graf 		goto nla_put_failure;
227726b15dadSJamal Hadi Salim 
22787b67c857SThomas Graf 	p = nlmsg_data(nlh);
22790603eac0SHerbert Xu 	if (c->event == XFRM_MSG_DELSA) {
2280c0144beaSThomas Graf 		struct nlattr *attr;
2281c0144beaSThomas Graf 
22827b67c857SThomas Graf 		id = nlmsg_data(nlh);
22830603eac0SHerbert Xu 		memcpy(&id->daddr, &x->id.daddr, sizeof(id->daddr));
22840603eac0SHerbert Xu 		id->spi = x->id.spi;
22850603eac0SHerbert Xu 		id->family = x->props.family;
22860603eac0SHerbert Xu 		id->proto = x->id.proto;
22870603eac0SHerbert Xu 
2288c0144beaSThomas Graf 		attr = nla_reserve(skb, XFRMA_SA, sizeof(*p));
2289c0144beaSThomas Graf 		if (attr == NULL)
2290c0144beaSThomas Graf 			goto nla_put_failure;
2291c0144beaSThomas Graf 
2292c0144beaSThomas Graf 		p = nla_data(attr);
22930603eac0SHerbert Xu 	}
22940603eac0SHerbert Xu 
229568325d3bSHerbert Xu 	if (copy_to_user_state_extra(x, p, skb))
229668325d3bSHerbert Xu 		goto nla_put_failure;
229726b15dadSJamal Hadi Salim 
22989825069dSThomas Graf 	nlmsg_end(skb, nlh);
229926b15dadSJamal Hadi Salim 
2300a6483b79SAlexey Dobriyan 	return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_SA, GFP_ATOMIC);
230126b15dadSJamal Hadi Salim 
2302c0144beaSThomas Graf nla_put_failure:
230368325d3bSHerbert Xu 	/* Somebody screwed up with xfrm_sa_len! */
230468325d3bSHerbert Xu 	WARN_ON(1);
230526b15dadSJamal Hadi Salim 	kfree_skb(skb);
230626b15dadSJamal Hadi Salim 	return -1;
230726b15dadSJamal Hadi Salim }
230826b15dadSJamal Hadi Salim 
230926b15dadSJamal Hadi Salim static int xfrm_send_state_notify(struct xfrm_state *x, struct km_event *c)
231026b15dadSJamal Hadi Salim {
231126b15dadSJamal Hadi Salim 
231226b15dadSJamal Hadi Salim 	switch (c->event) {
2313f60f6b8fSHerbert Xu 	case XFRM_MSG_EXPIRE:
231426b15dadSJamal Hadi Salim 		return xfrm_exp_state_notify(x, c);
2315d51d081dSJamal Hadi Salim 	case XFRM_MSG_NEWAE:
2316d51d081dSJamal Hadi Salim 		return xfrm_aevent_state_notify(x, c);
2317f60f6b8fSHerbert Xu 	case XFRM_MSG_DELSA:
2318f60f6b8fSHerbert Xu 	case XFRM_MSG_UPDSA:
2319f60f6b8fSHerbert Xu 	case XFRM_MSG_NEWSA:
232026b15dadSJamal Hadi Salim 		return xfrm_notify_sa(x, c);
2321f60f6b8fSHerbert Xu 	case XFRM_MSG_FLUSHSA:
232226b15dadSJamal Hadi Salim 		return xfrm_notify_sa_flush(c);
232326b15dadSJamal Hadi Salim 	default:
232426b15dadSJamal Hadi Salim 		 printk("xfrm_user: Unknown SA event %d\n", c->event);
232526b15dadSJamal Hadi Salim 		 break;
232626b15dadSJamal Hadi Salim 	}
232726b15dadSJamal Hadi Salim 
232826b15dadSJamal Hadi Salim 	return 0;
232926b15dadSJamal Hadi Salim 
233026b15dadSJamal Hadi Salim }
233126b15dadSJamal Hadi Salim 
23327deb2264SThomas Graf static inline size_t xfrm_acquire_msgsize(struct xfrm_state *x,
23337deb2264SThomas Graf 					  struct xfrm_policy *xp)
23347deb2264SThomas Graf {
23357deb2264SThomas Graf 	return NLMSG_ALIGN(sizeof(struct xfrm_user_acquire))
23367deb2264SThomas Graf 	       + nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr)
23377deb2264SThomas Graf 	       + nla_total_size(xfrm_user_sec_ctx_size(x->security))
23387deb2264SThomas Graf 	       + userpolicy_type_attrsize();
23397deb2264SThomas Graf }
23407deb2264SThomas Graf 
23411da177e4SLinus Torvalds static int build_acquire(struct sk_buff *skb, struct xfrm_state *x,
23421da177e4SLinus Torvalds 			 struct xfrm_tmpl *xt, struct xfrm_policy *xp,
23431da177e4SLinus Torvalds 			 int dir)
23441da177e4SLinus Torvalds {
23451da177e4SLinus Torvalds 	struct xfrm_user_acquire *ua;
23461da177e4SLinus Torvalds 	struct nlmsghdr *nlh;
23471da177e4SLinus Torvalds 	__u32 seq = xfrm_get_acqseq();
23481da177e4SLinus Torvalds 
234979b8b7f4SThomas Graf 	nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_ACQUIRE, sizeof(*ua), 0);
235079b8b7f4SThomas Graf 	if (nlh == NULL)
235179b8b7f4SThomas Graf 		return -EMSGSIZE;
23521da177e4SLinus Torvalds 
23537b67c857SThomas Graf 	ua = nlmsg_data(nlh);
23541da177e4SLinus Torvalds 	memcpy(&ua->id, &x->id, sizeof(ua->id));
23551da177e4SLinus Torvalds 	memcpy(&ua->saddr, &x->props.saddr, sizeof(ua->saddr));
23561da177e4SLinus Torvalds 	memcpy(&ua->sel, &x->sel, sizeof(ua->sel));
23571da177e4SLinus Torvalds 	copy_to_user_policy(xp, &ua->policy, dir);
23581da177e4SLinus Torvalds 	ua->aalgos = xt->aalgos;
23591da177e4SLinus Torvalds 	ua->ealgos = xt->ealgos;
23601da177e4SLinus Torvalds 	ua->calgos = xt->calgos;
23611da177e4SLinus Torvalds 	ua->seq = x->km.seq = seq;
23621da177e4SLinus Torvalds 
23631da177e4SLinus Torvalds 	if (copy_to_user_tmpl(xp, skb) < 0)
23641da177e4SLinus Torvalds 		goto nlmsg_failure;
23650d681623SSerge Hallyn 	if (copy_to_user_state_sec_ctx(x, skb))
2366df71837dSTrent Jaeger 		goto nlmsg_failure;
23671459bb36SJamal Hadi Salim 	if (copy_to_user_policy_type(xp->type, skb) < 0)
2368f7b6983fSMasahide NAKAMURA 		goto nlmsg_failure;
23691da177e4SLinus Torvalds 
23709825069dSThomas Graf 	return nlmsg_end(skb, nlh);
23711da177e4SLinus Torvalds 
23721da177e4SLinus Torvalds nlmsg_failure:
23739825069dSThomas Graf 	nlmsg_cancel(skb, nlh);
23749825069dSThomas Graf 	return -EMSGSIZE;
23751da177e4SLinus Torvalds }
23761da177e4SLinus Torvalds 
23771da177e4SLinus Torvalds static int xfrm_send_acquire(struct xfrm_state *x, struct xfrm_tmpl *xt,
23781da177e4SLinus Torvalds 			     struct xfrm_policy *xp, int dir)
23791da177e4SLinus Torvalds {
2380a6483b79SAlexey Dobriyan 	struct net *net = xs_net(x);
23811da177e4SLinus Torvalds 	struct sk_buff *skb;
23821da177e4SLinus Torvalds 
23837deb2264SThomas Graf 	skb = nlmsg_new(xfrm_acquire_msgsize(x, xp), GFP_ATOMIC);
23841da177e4SLinus Torvalds 	if (skb == NULL)
23851da177e4SLinus Torvalds 		return -ENOMEM;
23861da177e4SLinus Torvalds 
23871da177e4SLinus Torvalds 	if (build_acquire(skb, x, xt, xp, dir) < 0)
23881da177e4SLinus Torvalds 		BUG();
23891da177e4SLinus Torvalds 
2390a6483b79SAlexey Dobriyan 	return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_ACQUIRE, GFP_ATOMIC);
23911da177e4SLinus Torvalds }
23921da177e4SLinus Torvalds 
23931da177e4SLinus Torvalds /* User gives us xfrm_user_policy_info followed by an array of 0
23941da177e4SLinus Torvalds  * or more templates.
23951da177e4SLinus Torvalds  */
2396cb969f07SVenkat Yekkirala static struct xfrm_policy *xfrm_compile_policy(struct sock *sk, int opt,
23971da177e4SLinus Torvalds 					       u8 *data, int len, int *dir)
23981da177e4SLinus Torvalds {
2399fc34acd3SAlexey Dobriyan 	struct net *net = sock_net(sk);
24001da177e4SLinus Torvalds 	struct xfrm_userpolicy_info *p = (struct xfrm_userpolicy_info *)data;
24011da177e4SLinus Torvalds 	struct xfrm_user_tmpl *ut = (struct xfrm_user_tmpl *) (p + 1);
24021da177e4SLinus Torvalds 	struct xfrm_policy *xp;
24031da177e4SLinus Torvalds 	int nr;
24041da177e4SLinus Torvalds 
2405cb969f07SVenkat Yekkirala 	switch (sk->sk_family) {
24061da177e4SLinus Torvalds 	case AF_INET:
24071da177e4SLinus Torvalds 		if (opt != IP_XFRM_POLICY) {
24081da177e4SLinus Torvalds 			*dir = -EOPNOTSUPP;
24091da177e4SLinus Torvalds 			return NULL;
24101da177e4SLinus Torvalds 		}
24111da177e4SLinus Torvalds 		break;
24121da177e4SLinus Torvalds #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
24131da177e4SLinus Torvalds 	case AF_INET6:
24141da177e4SLinus Torvalds 		if (opt != IPV6_XFRM_POLICY) {
24151da177e4SLinus Torvalds 			*dir = -EOPNOTSUPP;
24161da177e4SLinus Torvalds 			return NULL;
24171da177e4SLinus Torvalds 		}
24181da177e4SLinus Torvalds 		break;
24191da177e4SLinus Torvalds #endif
24201da177e4SLinus Torvalds 	default:
24211da177e4SLinus Torvalds 		*dir = -EINVAL;
24221da177e4SLinus Torvalds 		return NULL;
24231da177e4SLinus Torvalds 	}
24241da177e4SLinus Torvalds 
24251da177e4SLinus Torvalds 	*dir = -EINVAL;
24261da177e4SLinus Torvalds 
24271da177e4SLinus Torvalds 	if (len < sizeof(*p) ||
24281da177e4SLinus Torvalds 	    verify_newpolicy_info(p))
24291da177e4SLinus Torvalds 		return NULL;
24301da177e4SLinus Torvalds 
24311da177e4SLinus Torvalds 	nr = ((len - sizeof(*p)) / sizeof(*ut));
2432b4ad86bfSDavid S. Miller 	if (validate_tmpl(nr, ut, p->sel.family))
24331da177e4SLinus Torvalds 		return NULL;
24341da177e4SLinus Torvalds 
2435a4f1bac6SHerbert Xu 	if (p->dir > XFRM_POLICY_OUT)
2436a4f1bac6SHerbert Xu 		return NULL;
2437a4f1bac6SHerbert Xu 
2438fc34acd3SAlexey Dobriyan 	xp = xfrm_policy_alloc(net, GFP_KERNEL);
24391da177e4SLinus Torvalds 	if (xp == NULL) {
24401da177e4SLinus Torvalds 		*dir = -ENOBUFS;
24411da177e4SLinus Torvalds 		return NULL;
24421da177e4SLinus Torvalds 	}
24431da177e4SLinus Torvalds 
24441da177e4SLinus Torvalds 	copy_from_user_policy(xp, p);
2445f7b6983fSMasahide NAKAMURA 	xp->type = XFRM_POLICY_TYPE_MAIN;
24461da177e4SLinus Torvalds 	copy_templates(xp, ut, nr);
24471da177e4SLinus Torvalds 
24481da177e4SLinus Torvalds 	*dir = p->dir;
24491da177e4SLinus Torvalds 
24501da177e4SLinus Torvalds 	return xp;
24511da177e4SLinus Torvalds }
24521da177e4SLinus Torvalds 
24537deb2264SThomas Graf static inline size_t xfrm_polexpire_msgsize(struct xfrm_policy *xp)
24547deb2264SThomas Graf {
24557deb2264SThomas Graf 	return NLMSG_ALIGN(sizeof(struct xfrm_user_polexpire))
24567deb2264SThomas Graf 	       + nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr)
24577deb2264SThomas Graf 	       + nla_total_size(xfrm_user_sec_ctx_size(xp->security))
24587deb2264SThomas Graf 	       + userpolicy_type_attrsize();
24597deb2264SThomas Graf }
24607deb2264SThomas Graf 
24611da177e4SLinus Torvalds static int build_polexpire(struct sk_buff *skb, struct xfrm_policy *xp,
2462d51d081dSJamal Hadi Salim 			   int dir, struct km_event *c)
24631da177e4SLinus Torvalds {
24641da177e4SLinus Torvalds 	struct xfrm_user_polexpire *upe;
24651da177e4SLinus Torvalds 	struct nlmsghdr *nlh;
2466d51d081dSJamal Hadi Salim 	int hard = c->data.hard;
24671da177e4SLinus Torvalds 
246879b8b7f4SThomas Graf 	nlh = nlmsg_put(skb, c->pid, 0, XFRM_MSG_POLEXPIRE, sizeof(*upe), 0);
246979b8b7f4SThomas Graf 	if (nlh == NULL)
247079b8b7f4SThomas Graf 		return -EMSGSIZE;
24711da177e4SLinus Torvalds 
24727b67c857SThomas Graf 	upe = nlmsg_data(nlh);
24731da177e4SLinus Torvalds 	copy_to_user_policy(xp, &upe->pol, dir);
24741da177e4SLinus Torvalds 	if (copy_to_user_tmpl(xp, skb) < 0)
24751da177e4SLinus Torvalds 		goto nlmsg_failure;
2476df71837dSTrent Jaeger 	if (copy_to_user_sec_ctx(xp, skb))
2477df71837dSTrent Jaeger 		goto nlmsg_failure;
24781459bb36SJamal Hadi Salim 	if (copy_to_user_policy_type(xp->type, skb) < 0)
2479f7b6983fSMasahide NAKAMURA 		goto nlmsg_failure;
24801da177e4SLinus Torvalds 	upe->hard = !!hard;
24811da177e4SLinus Torvalds 
24829825069dSThomas Graf 	return nlmsg_end(skb, nlh);
24831da177e4SLinus Torvalds 
24841da177e4SLinus Torvalds nlmsg_failure:
24859825069dSThomas Graf 	nlmsg_cancel(skb, nlh);
24869825069dSThomas Graf 	return -EMSGSIZE;
24871da177e4SLinus Torvalds }
24881da177e4SLinus Torvalds 
248926b15dadSJamal Hadi Salim static int xfrm_exp_policy_notify(struct xfrm_policy *xp, int dir, struct km_event *c)
24901da177e4SLinus Torvalds {
2491fc34acd3SAlexey Dobriyan 	struct net *net = xp_net(xp);
24921da177e4SLinus Torvalds 	struct sk_buff *skb;
24931da177e4SLinus Torvalds 
24947deb2264SThomas Graf 	skb = nlmsg_new(xfrm_polexpire_msgsize(xp), GFP_ATOMIC);
24951da177e4SLinus Torvalds 	if (skb == NULL)
24961da177e4SLinus Torvalds 		return -ENOMEM;
24971da177e4SLinus Torvalds 
2498d51d081dSJamal Hadi Salim 	if (build_polexpire(skb, xp, dir, c) < 0)
24991da177e4SLinus Torvalds 		BUG();
25001da177e4SLinus Torvalds 
2501a6483b79SAlexey Dobriyan 	return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_EXPIRE, GFP_ATOMIC);
25021da177e4SLinus Torvalds }
25031da177e4SLinus Torvalds 
250426b15dadSJamal Hadi Salim static int xfrm_notify_policy(struct xfrm_policy *xp, int dir, struct km_event *c)
250526b15dadSJamal Hadi Salim {
2506fc34acd3SAlexey Dobriyan 	struct net *net = xp_net(xp);
250726b15dadSJamal Hadi Salim 	struct xfrm_userpolicy_info *p;
25080603eac0SHerbert Xu 	struct xfrm_userpolicy_id *id;
250926b15dadSJamal Hadi Salim 	struct nlmsghdr *nlh;
251026b15dadSJamal Hadi Salim 	struct sk_buff *skb;
25117deb2264SThomas Graf 	int len = nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr);
25120603eac0SHerbert Xu 	int headlen;
25130603eac0SHerbert Xu 
25140603eac0SHerbert Xu 	headlen = sizeof(*p);
25150603eac0SHerbert Xu 	if (c->event == XFRM_MSG_DELPOLICY) {
25167deb2264SThomas Graf 		len += nla_total_size(headlen);
25170603eac0SHerbert Xu 		headlen = sizeof(*id);
25180603eac0SHerbert Xu 	}
2519cfbfd45aSThomas Graf 	len += userpolicy_type_attrsize();
25207deb2264SThomas Graf 	len += NLMSG_ALIGN(headlen);
252126b15dadSJamal Hadi Salim 
25227deb2264SThomas Graf 	skb = nlmsg_new(len, GFP_ATOMIC);
252326b15dadSJamal Hadi Salim 	if (skb == NULL)
252426b15dadSJamal Hadi Salim 		return -ENOMEM;
252526b15dadSJamal Hadi Salim 
252679b8b7f4SThomas Graf 	nlh = nlmsg_put(skb, c->pid, c->seq, c->event, headlen, 0);
252779b8b7f4SThomas Graf 	if (nlh == NULL)
252879b8b7f4SThomas Graf 		goto nlmsg_failure;
252926b15dadSJamal Hadi Salim 
25307b67c857SThomas Graf 	p = nlmsg_data(nlh);
25310603eac0SHerbert Xu 	if (c->event == XFRM_MSG_DELPOLICY) {
2532c0144beaSThomas Graf 		struct nlattr *attr;
2533c0144beaSThomas Graf 
25347b67c857SThomas Graf 		id = nlmsg_data(nlh);
25350603eac0SHerbert Xu 		memset(id, 0, sizeof(*id));
25360603eac0SHerbert Xu 		id->dir = dir;
25370603eac0SHerbert Xu 		if (c->data.byid)
25380603eac0SHerbert Xu 			id->index = xp->index;
25390603eac0SHerbert Xu 		else
25400603eac0SHerbert Xu 			memcpy(&id->sel, &xp->selector, sizeof(id->sel));
25410603eac0SHerbert Xu 
2542c0144beaSThomas Graf 		attr = nla_reserve(skb, XFRMA_POLICY, sizeof(*p));
2543c0144beaSThomas Graf 		if (attr == NULL)
2544c0144beaSThomas Graf 			goto nlmsg_failure;
2545c0144beaSThomas Graf 
2546c0144beaSThomas Graf 		p = nla_data(attr);
25470603eac0SHerbert Xu 	}
254826b15dadSJamal Hadi Salim 
254926b15dadSJamal Hadi Salim 	copy_to_user_policy(xp, p, dir);
255026b15dadSJamal Hadi Salim 	if (copy_to_user_tmpl(xp, skb) < 0)
255126b15dadSJamal Hadi Salim 		goto nlmsg_failure;
25521459bb36SJamal Hadi Salim 	if (copy_to_user_policy_type(xp->type, skb) < 0)
2553f7b6983fSMasahide NAKAMURA 		goto nlmsg_failure;
255426b15dadSJamal Hadi Salim 
25559825069dSThomas Graf 	nlmsg_end(skb, nlh);
255626b15dadSJamal Hadi Salim 
2557a6483b79SAlexey Dobriyan 	return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_POLICY, GFP_ATOMIC);
255826b15dadSJamal Hadi Salim 
255926b15dadSJamal Hadi Salim nlmsg_failure:
256026b15dadSJamal Hadi Salim 	kfree_skb(skb);
256126b15dadSJamal Hadi Salim 	return -1;
256226b15dadSJamal Hadi Salim }
256326b15dadSJamal Hadi Salim 
256426b15dadSJamal Hadi Salim static int xfrm_notify_policy_flush(struct km_event *c)
256526b15dadSJamal Hadi Salim {
25667067802eSAlexey Dobriyan 	struct net *net = c->net;
256726b15dadSJamal Hadi Salim 	struct nlmsghdr *nlh;
256826b15dadSJamal Hadi Salim 	struct sk_buff *skb;
256926b15dadSJamal Hadi Salim 
25707deb2264SThomas Graf 	skb = nlmsg_new(userpolicy_type_attrsize(), GFP_ATOMIC);
257126b15dadSJamal Hadi Salim 	if (skb == NULL)
257226b15dadSJamal Hadi Salim 		return -ENOMEM;
257326b15dadSJamal Hadi Salim 
257479b8b7f4SThomas Graf 	nlh = nlmsg_put(skb, c->pid, c->seq, XFRM_MSG_FLUSHPOLICY, 0, 0);
257579b8b7f4SThomas Graf 	if (nlh == NULL)
257679b8b7f4SThomas Graf 		goto nlmsg_failure;
25770c51f53cSJamal Hadi Salim 	if (copy_to_user_policy_type(c->data.type, skb) < 0)
25780c51f53cSJamal Hadi Salim 		goto nlmsg_failure;
257926b15dadSJamal Hadi Salim 
25809825069dSThomas Graf 	nlmsg_end(skb, nlh);
258126b15dadSJamal Hadi Salim 
2582a6483b79SAlexey Dobriyan 	return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_POLICY, GFP_ATOMIC);
258326b15dadSJamal Hadi Salim 
258426b15dadSJamal Hadi Salim nlmsg_failure:
258526b15dadSJamal Hadi Salim 	kfree_skb(skb);
258626b15dadSJamal Hadi Salim 	return -1;
258726b15dadSJamal Hadi Salim }
258826b15dadSJamal Hadi Salim 
258926b15dadSJamal Hadi Salim static int xfrm_send_policy_notify(struct xfrm_policy *xp, int dir, struct km_event *c)
259026b15dadSJamal Hadi Salim {
259126b15dadSJamal Hadi Salim 
259226b15dadSJamal Hadi Salim 	switch (c->event) {
2593f60f6b8fSHerbert Xu 	case XFRM_MSG_NEWPOLICY:
2594f60f6b8fSHerbert Xu 	case XFRM_MSG_UPDPOLICY:
2595f60f6b8fSHerbert Xu 	case XFRM_MSG_DELPOLICY:
259626b15dadSJamal Hadi Salim 		return xfrm_notify_policy(xp, dir, c);
2597f60f6b8fSHerbert Xu 	case XFRM_MSG_FLUSHPOLICY:
259826b15dadSJamal Hadi Salim 		return xfrm_notify_policy_flush(c);
2599f60f6b8fSHerbert Xu 	case XFRM_MSG_POLEXPIRE:
260026b15dadSJamal Hadi Salim 		return xfrm_exp_policy_notify(xp, dir, c);
260126b15dadSJamal Hadi Salim 	default:
260226b15dadSJamal Hadi Salim 		printk("xfrm_user: Unknown Policy event %d\n", c->event);
260326b15dadSJamal Hadi Salim 	}
260426b15dadSJamal Hadi Salim 
260526b15dadSJamal Hadi Salim 	return 0;
260626b15dadSJamal Hadi Salim 
260726b15dadSJamal Hadi Salim }
260826b15dadSJamal Hadi Salim 
26097deb2264SThomas Graf static inline size_t xfrm_report_msgsize(void)
26107deb2264SThomas Graf {
26117deb2264SThomas Graf 	return NLMSG_ALIGN(sizeof(struct xfrm_user_report));
26127deb2264SThomas Graf }
26137deb2264SThomas Graf 
261497a64b45SMasahide NAKAMURA static int build_report(struct sk_buff *skb, u8 proto,
261597a64b45SMasahide NAKAMURA 			struct xfrm_selector *sel, xfrm_address_t *addr)
261697a64b45SMasahide NAKAMURA {
261797a64b45SMasahide NAKAMURA 	struct xfrm_user_report *ur;
261897a64b45SMasahide NAKAMURA 	struct nlmsghdr *nlh;
261997a64b45SMasahide NAKAMURA 
262079b8b7f4SThomas Graf 	nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_REPORT, sizeof(*ur), 0);
262179b8b7f4SThomas Graf 	if (nlh == NULL)
262279b8b7f4SThomas Graf 		return -EMSGSIZE;
262397a64b45SMasahide NAKAMURA 
26247b67c857SThomas Graf 	ur = nlmsg_data(nlh);
262597a64b45SMasahide NAKAMURA 	ur->proto = proto;
262697a64b45SMasahide NAKAMURA 	memcpy(&ur->sel, sel, sizeof(ur->sel));
262797a64b45SMasahide NAKAMURA 
262897a64b45SMasahide NAKAMURA 	if (addr)
2629c0144beaSThomas Graf 		NLA_PUT(skb, XFRMA_COADDR, sizeof(*addr), addr);
263097a64b45SMasahide NAKAMURA 
26319825069dSThomas Graf 	return nlmsg_end(skb, nlh);
263297a64b45SMasahide NAKAMURA 
2633c0144beaSThomas Graf nla_put_failure:
26349825069dSThomas Graf 	nlmsg_cancel(skb, nlh);
26359825069dSThomas Graf 	return -EMSGSIZE;
263697a64b45SMasahide NAKAMURA }
263797a64b45SMasahide NAKAMURA 
2638db983c11SAlexey Dobriyan static int xfrm_send_report(struct net *net, u8 proto,
2639db983c11SAlexey Dobriyan 			    struct xfrm_selector *sel, xfrm_address_t *addr)
264097a64b45SMasahide NAKAMURA {
264197a64b45SMasahide NAKAMURA 	struct sk_buff *skb;
264297a64b45SMasahide NAKAMURA 
26437deb2264SThomas Graf 	skb = nlmsg_new(xfrm_report_msgsize(), GFP_ATOMIC);
264497a64b45SMasahide NAKAMURA 	if (skb == NULL)
264597a64b45SMasahide NAKAMURA 		return -ENOMEM;
264697a64b45SMasahide NAKAMURA 
264797a64b45SMasahide NAKAMURA 	if (build_report(skb, proto, sel, addr) < 0)
264897a64b45SMasahide NAKAMURA 		BUG();
264997a64b45SMasahide NAKAMURA 
2650a6483b79SAlexey Dobriyan 	return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_REPORT, GFP_ATOMIC);
265197a64b45SMasahide NAKAMURA }
265297a64b45SMasahide NAKAMURA 
26533a2dfbe8SMartin Willi static inline size_t xfrm_mapping_msgsize(void)
26543a2dfbe8SMartin Willi {
26553a2dfbe8SMartin Willi 	return NLMSG_ALIGN(sizeof(struct xfrm_user_mapping));
26563a2dfbe8SMartin Willi }
26573a2dfbe8SMartin Willi 
26583a2dfbe8SMartin Willi static int build_mapping(struct sk_buff *skb, struct xfrm_state *x,
26593a2dfbe8SMartin Willi 			 xfrm_address_t *new_saddr, __be16 new_sport)
26603a2dfbe8SMartin Willi {
26613a2dfbe8SMartin Willi 	struct xfrm_user_mapping *um;
26623a2dfbe8SMartin Willi 	struct nlmsghdr *nlh;
26633a2dfbe8SMartin Willi 
26643a2dfbe8SMartin Willi 	nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_MAPPING, sizeof(*um), 0);
26653a2dfbe8SMartin Willi 	if (nlh == NULL)
26663a2dfbe8SMartin Willi 		return -EMSGSIZE;
26673a2dfbe8SMartin Willi 
26683a2dfbe8SMartin Willi 	um = nlmsg_data(nlh);
26693a2dfbe8SMartin Willi 
26703a2dfbe8SMartin Willi 	memcpy(&um->id.daddr, &x->id.daddr, sizeof(um->id.daddr));
26713a2dfbe8SMartin Willi 	um->id.spi = x->id.spi;
26723a2dfbe8SMartin Willi 	um->id.family = x->props.family;
26733a2dfbe8SMartin Willi 	um->id.proto = x->id.proto;
26743a2dfbe8SMartin Willi 	memcpy(&um->new_saddr, new_saddr, sizeof(um->new_saddr));
26753a2dfbe8SMartin Willi 	memcpy(&um->old_saddr, &x->props.saddr, sizeof(um->old_saddr));
26763a2dfbe8SMartin Willi 	um->new_sport = new_sport;
26773a2dfbe8SMartin Willi 	um->old_sport = x->encap->encap_sport;
26783a2dfbe8SMartin Willi 	um->reqid = x->props.reqid;
26793a2dfbe8SMartin Willi 
26803a2dfbe8SMartin Willi 	return nlmsg_end(skb, nlh);
26813a2dfbe8SMartin Willi }
26823a2dfbe8SMartin Willi 
26833a2dfbe8SMartin Willi static int xfrm_send_mapping(struct xfrm_state *x, xfrm_address_t *ipaddr,
26843a2dfbe8SMartin Willi 			     __be16 sport)
26853a2dfbe8SMartin Willi {
2686a6483b79SAlexey Dobriyan 	struct net *net = xs_net(x);
26873a2dfbe8SMartin Willi 	struct sk_buff *skb;
26883a2dfbe8SMartin Willi 
26893a2dfbe8SMartin Willi 	if (x->id.proto != IPPROTO_ESP)
26903a2dfbe8SMartin Willi 		return -EINVAL;
26913a2dfbe8SMartin Willi 
26923a2dfbe8SMartin Willi 	if (!x->encap)
26933a2dfbe8SMartin Willi 		return -EINVAL;
26943a2dfbe8SMartin Willi 
26953a2dfbe8SMartin Willi 	skb = nlmsg_new(xfrm_mapping_msgsize(), GFP_ATOMIC);
26963a2dfbe8SMartin Willi 	if (skb == NULL)
26973a2dfbe8SMartin Willi 		return -ENOMEM;
26983a2dfbe8SMartin Willi 
26993a2dfbe8SMartin Willi 	if (build_mapping(skb, x, ipaddr, sport) < 0)
27003a2dfbe8SMartin Willi 		BUG();
27013a2dfbe8SMartin Willi 
2702a6483b79SAlexey Dobriyan 	return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_MAPPING, GFP_ATOMIC);
27033a2dfbe8SMartin Willi }
27043a2dfbe8SMartin Willi 
27051da177e4SLinus Torvalds static struct xfrm_mgr netlink_mgr = {
27061da177e4SLinus Torvalds 	.id		= "netlink",
27071da177e4SLinus Torvalds 	.notify		= xfrm_send_state_notify,
27081da177e4SLinus Torvalds 	.acquire	= xfrm_send_acquire,
27091da177e4SLinus Torvalds 	.compile_policy	= xfrm_compile_policy,
27101da177e4SLinus Torvalds 	.notify_policy	= xfrm_send_policy_notify,
271197a64b45SMasahide NAKAMURA 	.report		= xfrm_send_report,
27125c79de6eSShinta Sugimoto 	.migrate	= xfrm_send_migrate,
27133a2dfbe8SMartin Willi 	.new_mapping	= xfrm_send_mapping,
27141da177e4SLinus Torvalds };
27151da177e4SLinus Torvalds 
2716a6483b79SAlexey Dobriyan static int __net_init xfrm_user_net_init(struct net *net)
27171da177e4SLinus Torvalds {
2718be33690dSPatrick McHardy 	struct sock *nlsk;
2719be33690dSPatrick McHardy 
2720a6483b79SAlexey Dobriyan 	nlsk = netlink_kernel_create(net, NETLINK_XFRM, XFRMNLGRP_MAX,
2721af65bdfcSPatrick McHardy 				     xfrm_netlink_rcv, NULL, THIS_MODULE);
2722be33690dSPatrick McHardy 	if (nlsk == NULL)
27231da177e4SLinus Torvalds 		return -ENOMEM;
2724a6483b79SAlexey Dobriyan 	rcu_assign_pointer(net->xfrm.nlsk, nlsk);
27251da177e4SLinus Torvalds 	return 0;
27261da177e4SLinus Torvalds }
27271da177e4SLinus Torvalds 
2728a6483b79SAlexey Dobriyan static void __net_exit xfrm_user_net_exit(struct net *net)
2729a6483b79SAlexey Dobriyan {
2730a6483b79SAlexey Dobriyan 	struct sock *nlsk = net->xfrm.nlsk;
2731a6483b79SAlexey Dobriyan 
2732a6483b79SAlexey Dobriyan 	rcu_assign_pointer(net->xfrm.nlsk, NULL);
2733a6483b79SAlexey Dobriyan 	synchronize_rcu();
2734a6483b79SAlexey Dobriyan 	netlink_kernel_release(nlsk);
2735a6483b79SAlexey Dobriyan }
2736a6483b79SAlexey Dobriyan 
2737a6483b79SAlexey Dobriyan static struct pernet_operations xfrm_user_net_ops = {
2738a6483b79SAlexey Dobriyan 	.init = xfrm_user_net_init,
2739a6483b79SAlexey Dobriyan 	.exit = xfrm_user_net_exit,
2740a6483b79SAlexey Dobriyan };
2741a6483b79SAlexey Dobriyan 
2742a6483b79SAlexey Dobriyan static int __init xfrm_user_init(void)
2743a6483b79SAlexey Dobriyan {
2744a6483b79SAlexey Dobriyan 	int rv;
2745a6483b79SAlexey Dobriyan 
2746a6483b79SAlexey Dobriyan 	printk(KERN_INFO "Initializing XFRM netlink socket\n");
2747a6483b79SAlexey Dobriyan 
2748a6483b79SAlexey Dobriyan 	rv = register_pernet_subsys(&xfrm_user_net_ops);
2749a6483b79SAlexey Dobriyan 	if (rv < 0)
2750a6483b79SAlexey Dobriyan 		return rv;
2751a6483b79SAlexey Dobriyan 	rv = xfrm_register_km(&netlink_mgr);
2752a6483b79SAlexey Dobriyan 	if (rv < 0)
2753a6483b79SAlexey Dobriyan 		unregister_pernet_subsys(&xfrm_user_net_ops);
2754a6483b79SAlexey Dobriyan 	return rv;
2755a6483b79SAlexey Dobriyan }
2756a6483b79SAlexey Dobriyan 
27571da177e4SLinus Torvalds static void __exit xfrm_user_exit(void)
27581da177e4SLinus Torvalds {
27591da177e4SLinus Torvalds 	xfrm_unregister_km(&netlink_mgr);
2760a6483b79SAlexey Dobriyan 	unregister_pernet_subsys(&xfrm_user_net_ops);
27611da177e4SLinus Torvalds }
27621da177e4SLinus Torvalds 
27631da177e4SLinus Torvalds module_init(xfrm_user_init);
27641da177e4SLinus Torvalds module_exit(xfrm_user_exit);
27651da177e4SLinus Torvalds MODULE_LICENSE("GPL");
27664fdb3bb7SHarald Welte MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK, NETLINK_XFRM);
2767f8cd5488SJamal Hadi Salim 
2768