xref: /linux/net/xfrm/xfrm_user.c (revision d3623099d3509fa68fa28235366049dd3156c63a)
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>
29fa6dd8a2SNicolas Dichtel #include <net/ah.h>
301da177e4SLinus Torvalds #include <asm/uaccess.h>
31dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6)
32e23c7194SMasahide NAKAMURA #include <linux/in6.h>
33e23c7194SMasahide NAKAMURA #endif
341da177e4SLinus Torvalds 
351a6509d9SHerbert Xu static inline int aead_len(struct xfrm_algo_aead *alg)
361a6509d9SHerbert Xu {
371a6509d9SHerbert Xu 	return sizeof(*alg) + ((alg->alg_key_len + 7) / 8);
381a6509d9SHerbert Xu }
391a6509d9SHerbert Xu 
405424f32eSThomas Graf static int verify_one_alg(struct nlattr **attrs, enum xfrm_attr_type_t type)
411da177e4SLinus Torvalds {
425424f32eSThomas Graf 	struct nlattr *rt = attrs[type];
431da177e4SLinus Torvalds 	struct xfrm_algo *algp;
441da177e4SLinus Torvalds 
451da177e4SLinus Torvalds 	if (!rt)
461da177e4SLinus Torvalds 		return 0;
471da177e4SLinus Torvalds 
485424f32eSThomas Graf 	algp = nla_data(rt);
490f99be0dSEric Dumazet 	if (nla_len(rt) < xfrm_alg_len(algp))
5031c26852SHerbert Xu 		return -EINVAL;
5131c26852SHerbert Xu 
521da177e4SLinus Torvalds 	switch (type) {
531da177e4SLinus Torvalds 	case XFRMA_ALG_AUTH:
541da177e4SLinus Torvalds 	case XFRMA_ALG_CRYPT:
551da177e4SLinus Torvalds 	case XFRMA_ALG_COMP:
561da177e4SLinus Torvalds 		break;
571da177e4SLinus Torvalds 
581da177e4SLinus Torvalds 	default:
591da177e4SLinus Torvalds 		return -EINVAL;
603ff50b79SStephen Hemminger 	}
611da177e4SLinus Torvalds 
621da177e4SLinus Torvalds 	algp->alg_name[CRYPTO_MAX_ALG_NAME - 1] = '\0';
631da177e4SLinus Torvalds 	return 0;
641da177e4SLinus Torvalds }
651da177e4SLinus Torvalds 
664447bb33SMartin Willi static int verify_auth_trunc(struct nlattr **attrs)
674447bb33SMartin Willi {
684447bb33SMartin Willi 	struct nlattr *rt = attrs[XFRMA_ALG_AUTH_TRUNC];
694447bb33SMartin Willi 	struct xfrm_algo_auth *algp;
704447bb33SMartin Willi 
714447bb33SMartin Willi 	if (!rt)
724447bb33SMartin Willi 		return 0;
734447bb33SMartin Willi 
744447bb33SMartin Willi 	algp = nla_data(rt);
754447bb33SMartin Willi 	if (nla_len(rt) < xfrm_alg_auth_len(algp))
764447bb33SMartin Willi 		return -EINVAL;
774447bb33SMartin Willi 
784447bb33SMartin Willi 	algp->alg_name[CRYPTO_MAX_ALG_NAME - 1] = '\0';
794447bb33SMartin Willi 	return 0;
804447bb33SMartin Willi }
814447bb33SMartin Willi 
821a6509d9SHerbert Xu static int verify_aead(struct nlattr **attrs)
831a6509d9SHerbert Xu {
841a6509d9SHerbert Xu 	struct nlattr *rt = attrs[XFRMA_ALG_AEAD];
851a6509d9SHerbert Xu 	struct xfrm_algo_aead *algp;
861a6509d9SHerbert Xu 
871a6509d9SHerbert Xu 	if (!rt)
881a6509d9SHerbert Xu 		return 0;
891a6509d9SHerbert Xu 
901a6509d9SHerbert Xu 	algp = nla_data(rt);
911a6509d9SHerbert Xu 	if (nla_len(rt) < aead_len(algp))
921a6509d9SHerbert Xu 		return -EINVAL;
931a6509d9SHerbert Xu 
941a6509d9SHerbert Xu 	algp->alg_name[CRYPTO_MAX_ALG_NAME - 1] = '\0';
951a6509d9SHerbert Xu 	return 0;
961a6509d9SHerbert Xu }
971a6509d9SHerbert Xu 
985424f32eSThomas Graf static void verify_one_addr(struct nlattr **attrs, enum xfrm_attr_type_t type,
99eb2971b6SMasahide NAKAMURA 			   xfrm_address_t **addrp)
100eb2971b6SMasahide NAKAMURA {
1015424f32eSThomas Graf 	struct nlattr *rt = attrs[type];
102eb2971b6SMasahide NAKAMURA 
103cf5cb79fSThomas Graf 	if (rt && addrp)
1045424f32eSThomas Graf 		*addrp = nla_data(rt);
105eb2971b6SMasahide NAKAMURA }
106df71837dSTrent Jaeger 
1075424f32eSThomas Graf static inline int verify_sec_ctx_len(struct nlattr **attrs)
108df71837dSTrent Jaeger {
1095424f32eSThomas Graf 	struct nlattr *rt = attrs[XFRMA_SEC_CTX];
110df71837dSTrent Jaeger 	struct xfrm_user_sec_ctx *uctx;
111df71837dSTrent Jaeger 
112df71837dSTrent Jaeger 	if (!rt)
113df71837dSTrent Jaeger 		return 0;
114df71837dSTrent Jaeger 
1155424f32eSThomas Graf 	uctx = nla_data(rt);
116cf5cb79fSThomas Graf 	if (uctx->len != (sizeof(struct xfrm_user_sec_ctx) + uctx->ctx_len))
117df71837dSTrent Jaeger 		return -EINVAL;
118df71837dSTrent Jaeger 
119df71837dSTrent Jaeger 	return 0;
120df71837dSTrent Jaeger }
121df71837dSTrent Jaeger 
122d8647b79SSteffen Klassert static inline int verify_replay(struct xfrm_usersa_info *p,
123d8647b79SSteffen Klassert 				struct nlattr **attrs)
124d8647b79SSteffen Klassert {
125d8647b79SSteffen Klassert 	struct nlattr *rt = attrs[XFRMA_REPLAY_ESN_VAL];
126ecd79187SMathias Krause 	struct xfrm_replay_state_esn *rs;
127d8647b79SSteffen Klassert 
128ecd79187SMathias Krause 	if (p->flags & XFRM_STATE_ESN) {
129ecd79187SMathias Krause 		if (!rt)
1307833aa05SSteffen Klassert 			return -EINVAL;
1317833aa05SSteffen Klassert 
132ecd79187SMathias Krause 		rs = nla_data(rt);
133ecd79187SMathias Krause 
134ecd79187SMathias Krause 		if (rs->bmp_len > XFRMA_REPLAY_ESN_MAX / sizeof(rs->bmp[0]) / 8)
135ecd79187SMathias Krause 			return -EINVAL;
136ecd79187SMathias Krause 
137ecd79187SMathias Krause 		if (nla_len(rt) < xfrm_replay_state_esn_len(rs) &&
138ecd79187SMathias Krause 		    nla_len(rt) != sizeof(*rs))
139ecd79187SMathias Krause 			return -EINVAL;
140ecd79187SMathias Krause 	}
141ecd79187SMathias Krause 
142d8647b79SSteffen Klassert 	if (!rt)
143d8647b79SSteffen Klassert 		return 0;
144d8647b79SSteffen Klassert 
14501714109SFan Du 	/* As only ESP and AH support ESN feature. */
14601714109SFan Du 	if ((p->id.proto != IPPROTO_ESP) && (p->id.proto != IPPROTO_AH))
14702aadf72SSteffen Klassert 		return -EINVAL;
14802aadf72SSteffen Klassert 
149d8647b79SSteffen Klassert 	if (p->replay_window != 0)
150d8647b79SSteffen Klassert 		return -EINVAL;
151d8647b79SSteffen Klassert 
152d8647b79SSteffen Klassert 	return 0;
153d8647b79SSteffen Klassert }
154df71837dSTrent Jaeger 
1551da177e4SLinus Torvalds static int verify_newsa_info(struct xfrm_usersa_info *p,
1565424f32eSThomas Graf 			     struct nlattr **attrs)
1571da177e4SLinus Torvalds {
1581da177e4SLinus Torvalds 	int err;
1591da177e4SLinus Torvalds 
1601da177e4SLinus Torvalds 	err = -EINVAL;
1611da177e4SLinus Torvalds 	switch (p->family) {
1621da177e4SLinus Torvalds 	case AF_INET:
1631da177e4SLinus Torvalds 		break;
1641da177e4SLinus Torvalds 
1651da177e4SLinus Torvalds 	case AF_INET6:
166dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6)
1671da177e4SLinus Torvalds 		break;
1681da177e4SLinus Torvalds #else
1691da177e4SLinus Torvalds 		err = -EAFNOSUPPORT;
1701da177e4SLinus Torvalds 		goto out;
1711da177e4SLinus Torvalds #endif
1721da177e4SLinus Torvalds 
1731da177e4SLinus Torvalds 	default:
1741da177e4SLinus Torvalds 		goto out;
1753ff50b79SStephen Hemminger 	}
1761da177e4SLinus Torvalds 
1771da177e4SLinus Torvalds 	err = -EINVAL;
1781da177e4SLinus Torvalds 	switch (p->id.proto) {
1791da177e4SLinus Torvalds 	case IPPROTO_AH:
1804447bb33SMartin Willi 		if ((!attrs[XFRMA_ALG_AUTH]	&&
1814447bb33SMartin Willi 		     !attrs[XFRMA_ALG_AUTH_TRUNC]) ||
1821a6509d9SHerbert Xu 		    attrs[XFRMA_ALG_AEAD]	||
18335a7aa08SThomas Graf 		    attrs[XFRMA_ALG_CRYPT]	||
18435d2856bSMartin Willi 		    attrs[XFRMA_ALG_COMP]	||
185ea9884b3SFan Du 		    attrs[XFRMA_TFCPAD]		||
186ea9884b3SFan Du 		    (ntohl(p->id.spi) >= 0x10000))
187ea9884b3SFan Du 
1881da177e4SLinus Torvalds 			goto out;
1891da177e4SLinus Torvalds 		break;
1901da177e4SLinus Torvalds 
1911da177e4SLinus Torvalds 	case IPPROTO_ESP:
1921a6509d9SHerbert Xu 		if (attrs[XFRMA_ALG_COMP])
1931a6509d9SHerbert Xu 			goto out;
1941a6509d9SHerbert Xu 		if (!attrs[XFRMA_ALG_AUTH] &&
1954447bb33SMartin Willi 		    !attrs[XFRMA_ALG_AUTH_TRUNC] &&
1961a6509d9SHerbert Xu 		    !attrs[XFRMA_ALG_CRYPT] &&
1971a6509d9SHerbert Xu 		    !attrs[XFRMA_ALG_AEAD])
1981a6509d9SHerbert Xu 			goto out;
1991a6509d9SHerbert Xu 		if ((attrs[XFRMA_ALG_AUTH] ||
2004447bb33SMartin Willi 		     attrs[XFRMA_ALG_AUTH_TRUNC] ||
2011a6509d9SHerbert Xu 		     attrs[XFRMA_ALG_CRYPT]) &&
2021a6509d9SHerbert Xu 		    attrs[XFRMA_ALG_AEAD])
2031da177e4SLinus Torvalds 			goto out;
20435d2856bSMartin Willi 		if (attrs[XFRMA_TFCPAD] &&
20535d2856bSMartin Willi 		    p->mode != XFRM_MODE_TUNNEL)
20635d2856bSMartin Willi 			goto out;
2071da177e4SLinus Torvalds 		break;
2081da177e4SLinus Torvalds 
2091da177e4SLinus Torvalds 	case IPPROTO_COMP:
21035a7aa08SThomas Graf 		if (!attrs[XFRMA_ALG_COMP]	||
2111a6509d9SHerbert Xu 		    attrs[XFRMA_ALG_AEAD]	||
21235a7aa08SThomas Graf 		    attrs[XFRMA_ALG_AUTH]	||
2134447bb33SMartin Willi 		    attrs[XFRMA_ALG_AUTH_TRUNC]	||
21435d2856bSMartin Willi 		    attrs[XFRMA_ALG_CRYPT]	||
21535d2856bSMartin Willi 		    attrs[XFRMA_TFCPAD])
2161da177e4SLinus Torvalds 			goto out;
2171da177e4SLinus Torvalds 		break;
2181da177e4SLinus Torvalds 
219dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6)
220e23c7194SMasahide NAKAMURA 	case IPPROTO_DSTOPTS:
221e23c7194SMasahide NAKAMURA 	case IPPROTO_ROUTING:
22235a7aa08SThomas Graf 		if (attrs[XFRMA_ALG_COMP]	||
22335a7aa08SThomas Graf 		    attrs[XFRMA_ALG_AUTH]	||
2244447bb33SMartin Willi 		    attrs[XFRMA_ALG_AUTH_TRUNC]	||
2251a6509d9SHerbert Xu 		    attrs[XFRMA_ALG_AEAD]	||
22635a7aa08SThomas Graf 		    attrs[XFRMA_ALG_CRYPT]	||
22735a7aa08SThomas Graf 		    attrs[XFRMA_ENCAP]		||
22835a7aa08SThomas Graf 		    attrs[XFRMA_SEC_CTX]	||
22935d2856bSMartin Willi 		    attrs[XFRMA_TFCPAD]		||
23035a7aa08SThomas Graf 		    !attrs[XFRMA_COADDR])
231e23c7194SMasahide NAKAMURA 			goto out;
232e23c7194SMasahide NAKAMURA 		break;
233e23c7194SMasahide NAKAMURA #endif
234e23c7194SMasahide NAKAMURA 
2351da177e4SLinus Torvalds 	default:
2361da177e4SLinus Torvalds 		goto out;
2373ff50b79SStephen Hemminger 	}
2381da177e4SLinus Torvalds 
2391a6509d9SHerbert Xu 	if ((err = verify_aead(attrs)))
2401a6509d9SHerbert Xu 		goto out;
2414447bb33SMartin Willi 	if ((err = verify_auth_trunc(attrs)))
2424447bb33SMartin Willi 		goto out;
24335a7aa08SThomas Graf 	if ((err = verify_one_alg(attrs, XFRMA_ALG_AUTH)))
2441da177e4SLinus Torvalds 		goto out;
24535a7aa08SThomas Graf 	if ((err = verify_one_alg(attrs, XFRMA_ALG_CRYPT)))
2461da177e4SLinus Torvalds 		goto out;
24735a7aa08SThomas Graf 	if ((err = verify_one_alg(attrs, XFRMA_ALG_COMP)))
2481da177e4SLinus Torvalds 		goto out;
24935a7aa08SThomas Graf 	if ((err = verify_sec_ctx_len(attrs)))
250df71837dSTrent Jaeger 		goto out;
251d8647b79SSteffen Klassert 	if ((err = verify_replay(p, attrs)))
252d8647b79SSteffen Klassert 		goto out;
2531da177e4SLinus Torvalds 
2541da177e4SLinus Torvalds 	err = -EINVAL;
2551da177e4SLinus Torvalds 	switch (p->mode) {
2567e49e6deSMasahide NAKAMURA 	case XFRM_MODE_TRANSPORT:
2577e49e6deSMasahide NAKAMURA 	case XFRM_MODE_TUNNEL:
258060f02a3SNoriaki TAKAMIYA 	case XFRM_MODE_ROUTEOPTIMIZATION:
2590a69452cSDiego Beltrami 	case XFRM_MODE_BEET:
2601da177e4SLinus Torvalds 		break;
2611da177e4SLinus Torvalds 
2621da177e4SLinus Torvalds 	default:
2631da177e4SLinus Torvalds 		goto out;
2643ff50b79SStephen Hemminger 	}
2651da177e4SLinus Torvalds 
2661da177e4SLinus Torvalds 	err = 0;
2671da177e4SLinus Torvalds 
2681da177e4SLinus Torvalds out:
2691da177e4SLinus Torvalds 	return err;
2701da177e4SLinus Torvalds }
2711da177e4SLinus Torvalds 
2721da177e4SLinus Torvalds static int attach_one_algo(struct xfrm_algo **algpp, u8 *props,
2736f2f19edSDavid S. Miller 			   struct xfrm_algo_desc *(*get_byname)(const char *, int),
2745424f32eSThomas Graf 			   struct nlattr *rta)
2751da177e4SLinus Torvalds {
2761da177e4SLinus Torvalds 	struct xfrm_algo *p, *ualg;
2771da177e4SLinus Torvalds 	struct xfrm_algo_desc *algo;
2781da177e4SLinus Torvalds 
2791da177e4SLinus Torvalds 	if (!rta)
2801da177e4SLinus Torvalds 		return 0;
2811da177e4SLinus Torvalds 
2825424f32eSThomas Graf 	ualg = nla_data(rta);
2831da177e4SLinus Torvalds 
2841da177e4SLinus Torvalds 	algo = get_byname(ualg->alg_name, 1);
2851da177e4SLinus Torvalds 	if (!algo)
2861da177e4SLinus Torvalds 		return -ENOSYS;
2871da177e4SLinus Torvalds 	*props = algo->desc.sadb_alg_id;
2881da177e4SLinus Torvalds 
2890f99be0dSEric Dumazet 	p = kmemdup(ualg, xfrm_alg_len(ualg), GFP_KERNEL);
2901da177e4SLinus Torvalds 	if (!p)
2911da177e4SLinus Torvalds 		return -ENOMEM;
2921da177e4SLinus Torvalds 
29304ff1260SHerbert Xu 	strcpy(p->alg_name, algo->name);
2941da177e4SLinus Torvalds 	*algpp = p;
2951da177e4SLinus Torvalds 	return 0;
2961da177e4SLinus Torvalds }
2971da177e4SLinus Torvalds 
2984447bb33SMartin Willi static int attach_auth(struct xfrm_algo_auth **algpp, u8 *props,
2994447bb33SMartin Willi 		       struct nlattr *rta)
3004447bb33SMartin Willi {
3014447bb33SMartin Willi 	struct xfrm_algo *ualg;
3024447bb33SMartin Willi 	struct xfrm_algo_auth *p;
3034447bb33SMartin Willi 	struct xfrm_algo_desc *algo;
3044447bb33SMartin Willi 
3054447bb33SMartin Willi 	if (!rta)
3064447bb33SMartin Willi 		return 0;
3074447bb33SMartin Willi 
3084447bb33SMartin Willi 	ualg = nla_data(rta);
3094447bb33SMartin Willi 
3104447bb33SMartin Willi 	algo = xfrm_aalg_get_byname(ualg->alg_name, 1);
3114447bb33SMartin Willi 	if (!algo)
3124447bb33SMartin Willi 		return -ENOSYS;
3134447bb33SMartin Willi 	*props = algo->desc.sadb_alg_id;
3144447bb33SMartin Willi 
3154447bb33SMartin Willi 	p = kmalloc(sizeof(*p) + (ualg->alg_key_len + 7) / 8, GFP_KERNEL);
3164447bb33SMartin Willi 	if (!p)
3174447bb33SMartin Willi 		return -ENOMEM;
3184447bb33SMartin Willi 
3194447bb33SMartin Willi 	strcpy(p->alg_name, algo->name);
3204447bb33SMartin Willi 	p->alg_key_len = ualg->alg_key_len;
3214447bb33SMartin Willi 	p->alg_trunc_len = algo->uinfo.auth.icv_truncbits;
3224447bb33SMartin Willi 	memcpy(p->alg_key, ualg->alg_key, (ualg->alg_key_len + 7) / 8);
3234447bb33SMartin Willi 
3244447bb33SMartin Willi 	*algpp = p;
3254447bb33SMartin Willi 	return 0;
3264447bb33SMartin Willi }
3274447bb33SMartin Willi 
3284447bb33SMartin Willi static int attach_auth_trunc(struct xfrm_algo_auth **algpp, u8 *props,
3294447bb33SMartin Willi 			     struct nlattr *rta)
3304447bb33SMartin Willi {
3314447bb33SMartin Willi 	struct xfrm_algo_auth *p, *ualg;
3324447bb33SMartin Willi 	struct xfrm_algo_desc *algo;
3334447bb33SMartin Willi 
3344447bb33SMartin Willi 	if (!rta)
3354447bb33SMartin Willi 		return 0;
3364447bb33SMartin Willi 
3374447bb33SMartin Willi 	ualg = nla_data(rta);
3384447bb33SMartin Willi 
3394447bb33SMartin Willi 	algo = xfrm_aalg_get_byname(ualg->alg_name, 1);
3404447bb33SMartin Willi 	if (!algo)
3414447bb33SMartin Willi 		return -ENOSYS;
342fa6dd8a2SNicolas Dichtel 	if ((ualg->alg_trunc_len / 8) > MAX_AH_AUTH_LEN ||
343fa6dd8a2SNicolas Dichtel 	    ualg->alg_trunc_len > algo->uinfo.auth.icv_fullbits)
3444447bb33SMartin Willi 		return -EINVAL;
3454447bb33SMartin Willi 	*props = algo->desc.sadb_alg_id;
3464447bb33SMartin Willi 
3474447bb33SMartin Willi 	p = kmemdup(ualg, xfrm_alg_auth_len(ualg), GFP_KERNEL);
3484447bb33SMartin Willi 	if (!p)
3494447bb33SMartin Willi 		return -ENOMEM;
3504447bb33SMartin Willi 
3514447bb33SMartin Willi 	strcpy(p->alg_name, algo->name);
3524447bb33SMartin Willi 	if (!p->alg_trunc_len)
3534447bb33SMartin Willi 		p->alg_trunc_len = algo->uinfo.auth.icv_truncbits;
3544447bb33SMartin Willi 
3554447bb33SMartin Willi 	*algpp = p;
3564447bb33SMartin Willi 	return 0;
3574447bb33SMartin Willi }
3584447bb33SMartin Willi 
3591a6509d9SHerbert Xu static int attach_aead(struct xfrm_algo_aead **algpp, u8 *props,
3601a6509d9SHerbert Xu 		       struct nlattr *rta)
3611a6509d9SHerbert Xu {
3621a6509d9SHerbert Xu 	struct xfrm_algo_aead *p, *ualg;
3631a6509d9SHerbert Xu 	struct xfrm_algo_desc *algo;
3641a6509d9SHerbert Xu 
3651a6509d9SHerbert Xu 	if (!rta)
3661a6509d9SHerbert Xu 		return 0;
3671a6509d9SHerbert Xu 
3681a6509d9SHerbert Xu 	ualg = nla_data(rta);
3691a6509d9SHerbert Xu 
3701a6509d9SHerbert Xu 	algo = xfrm_aead_get_byname(ualg->alg_name, ualg->alg_icv_len, 1);
3711a6509d9SHerbert Xu 	if (!algo)
3721a6509d9SHerbert Xu 		return -ENOSYS;
3731a6509d9SHerbert Xu 	*props = algo->desc.sadb_alg_id;
3741a6509d9SHerbert Xu 
3751a6509d9SHerbert Xu 	p = kmemdup(ualg, aead_len(ualg), GFP_KERNEL);
3761a6509d9SHerbert Xu 	if (!p)
3771a6509d9SHerbert Xu 		return -ENOMEM;
3781a6509d9SHerbert Xu 
3791a6509d9SHerbert Xu 	strcpy(p->alg_name, algo->name);
3801a6509d9SHerbert Xu 	*algpp = p;
3811a6509d9SHerbert Xu 	return 0;
3821a6509d9SHerbert Xu }
3831a6509d9SHerbert Xu 
384e2b19125SSteffen Klassert static inline int xfrm_replay_verify_len(struct xfrm_replay_state_esn *replay_esn,
385e2b19125SSteffen Klassert 					 struct nlattr *rp)
386e2b19125SSteffen Klassert {
387e2b19125SSteffen Klassert 	struct xfrm_replay_state_esn *up;
388ecd79187SMathias Krause 	int ulen;
389e2b19125SSteffen Klassert 
390e2b19125SSteffen Klassert 	if (!replay_esn || !rp)
391e2b19125SSteffen Klassert 		return 0;
392e2b19125SSteffen Klassert 
393e2b19125SSteffen Klassert 	up = nla_data(rp);
394ecd79187SMathias Krause 	ulen = xfrm_replay_state_esn_len(up);
395e2b19125SSteffen Klassert 
396ecd79187SMathias Krause 	if (nla_len(rp) < ulen || xfrm_replay_state_esn_len(replay_esn) != ulen)
397e2b19125SSteffen Klassert 		return -EINVAL;
398e2b19125SSteffen Klassert 
399e2b19125SSteffen Klassert 	return 0;
400e2b19125SSteffen Klassert }
401e2b19125SSteffen Klassert 
402d8647b79SSteffen Klassert static int xfrm_alloc_replay_state_esn(struct xfrm_replay_state_esn **replay_esn,
403d8647b79SSteffen Klassert 				       struct xfrm_replay_state_esn **preplay_esn,
404d8647b79SSteffen Klassert 				       struct nlattr *rta)
405d8647b79SSteffen Klassert {
406d8647b79SSteffen Klassert 	struct xfrm_replay_state_esn *p, *pp, *up;
407ecd79187SMathias Krause 	int klen, ulen;
408d8647b79SSteffen Klassert 
409d8647b79SSteffen Klassert 	if (!rta)
410d8647b79SSteffen Klassert 		return 0;
411d8647b79SSteffen Klassert 
412d8647b79SSteffen Klassert 	up = nla_data(rta);
413ecd79187SMathias Krause 	klen = xfrm_replay_state_esn_len(up);
414ecd79187SMathias Krause 	ulen = nla_len(rta) >= klen ? klen : sizeof(*up);
415d8647b79SSteffen Klassert 
416ecd79187SMathias Krause 	p = kzalloc(klen, GFP_KERNEL);
417d8647b79SSteffen Klassert 	if (!p)
418d8647b79SSteffen Klassert 		return -ENOMEM;
419d8647b79SSteffen Klassert 
420ecd79187SMathias Krause 	pp = kzalloc(klen, GFP_KERNEL);
421d8647b79SSteffen Klassert 	if (!pp) {
422d8647b79SSteffen Klassert 		kfree(p);
423d8647b79SSteffen Klassert 		return -ENOMEM;
424d8647b79SSteffen Klassert 	}
425d8647b79SSteffen Klassert 
426ecd79187SMathias Krause 	memcpy(p, up, ulen);
427ecd79187SMathias Krause 	memcpy(pp, up, ulen);
428ecd79187SMathias Krause 
429d8647b79SSteffen Klassert 	*replay_esn = p;
430d8647b79SSteffen Klassert 	*preplay_esn = pp;
431d8647b79SSteffen Klassert 
432d8647b79SSteffen Klassert 	return 0;
433d8647b79SSteffen Klassert }
434d8647b79SSteffen Klassert 
435661697f7SJoy Latten static inline int xfrm_user_sec_ctx_size(struct xfrm_sec_ctx *xfrm_ctx)
436df71837dSTrent Jaeger {
437df71837dSTrent Jaeger 	int len = 0;
438df71837dSTrent Jaeger 
439df71837dSTrent Jaeger 	if (xfrm_ctx) {
440df71837dSTrent Jaeger 		len += sizeof(struct xfrm_user_sec_ctx);
441df71837dSTrent Jaeger 		len += xfrm_ctx->ctx_len;
442df71837dSTrent Jaeger 	}
443df71837dSTrent Jaeger 	return len;
444df71837dSTrent Jaeger }
445df71837dSTrent Jaeger 
4461da177e4SLinus Torvalds static void copy_from_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p)
4471da177e4SLinus Torvalds {
4481da177e4SLinus Torvalds 	memcpy(&x->id, &p->id, sizeof(x->id));
4491da177e4SLinus Torvalds 	memcpy(&x->sel, &p->sel, sizeof(x->sel));
4501da177e4SLinus Torvalds 	memcpy(&x->lft, &p->lft, sizeof(x->lft));
4511da177e4SLinus Torvalds 	x->props.mode = p->mode;
45233fce60dSFan Du 	x->props.replay_window = min_t(unsigned int, p->replay_window,
45333fce60dSFan Du 					sizeof(x->replay.bitmap) * 8);
4541da177e4SLinus Torvalds 	x->props.reqid = p->reqid;
4551da177e4SLinus Torvalds 	x->props.family = p->family;
45654489c14SDavid S. Miller 	memcpy(&x->props.saddr, &p->saddr, sizeof(x->props.saddr));
4571da177e4SLinus Torvalds 	x->props.flags = p->flags;
458196b0036SHerbert Xu 
459ccf9b3b8SSteffen Klassert 	if (!x->sel.family && !(p->flags & XFRM_STATE_AF_UNSPEC))
460196b0036SHerbert Xu 		x->sel.family = p->family;
4611da177e4SLinus Torvalds }
4621da177e4SLinus Torvalds 
463d51d081dSJamal Hadi Salim /*
464d51d081dSJamal Hadi Salim  * someday when pfkey also has support, we could have the code
465d51d081dSJamal Hadi Salim  * somehow made shareable and move it to xfrm_state.c - JHS
466d51d081dSJamal Hadi Salim  *
467d51d081dSJamal Hadi Salim */
468e3ac104dSMathias Krause static void xfrm_update_ae_params(struct xfrm_state *x, struct nlattr **attrs,
469e3ac104dSMathias Krause 				  int update_esn)
470d51d081dSJamal Hadi Salim {
4715424f32eSThomas Graf 	struct nlattr *rp = attrs[XFRMA_REPLAY_VAL];
472e3ac104dSMathias Krause 	struct nlattr *re = update_esn ? attrs[XFRMA_REPLAY_ESN_VAL] : NULL;
4735424f32eSThomas Graf 	struct nlattr *lt = attrs[XFRMA_LTIME_VAL];
4745424f32eSThomas Graf 	struct nlattr *et = attrs[XFRMA_ETIMER_THRESH];
4755424f32eSThomas Graf 	struct nlattr *rt = attrs[XFRMA_REPLAY_THRESH];
476d51d081dSJamal Hadi Salim 
477d8647b79SSteffen Klassert 	if (re) {
478d8647b79SSteffen Klassert 		struct xfrm_replay_state_esn *replay_esn;
479d8647b79SSteffen Klassert 		replay_esn = nla_data(re);
480d8647b79SSteffen Klassert 		memcpy(x->replay_esn, replay_esn,
481d8647b79SSteffen Klassert 		       xfrm_replay_state_esn_len(replay_esn));
482d8647b79SSteffen Klassert 		memcpy(x->preplay_esn, replay_esn,
483d8647b79SSteffen Klassert 		       xfrm_replay_state_esn_len(replay_esn));
484d8647b79SSteffen Klassert 	}
485d8647b79SSteffen Klassert 
486d51d081dSJamal Hadi Salim 	if (rp) {
487d51d081dSJamal Hadi Salim 		struct xfrm_replay_state *replay;
4885424f32eSThomas Graf 		replay = nla_data(rp);
489d51d081dSJamal Hadi Salim 		memcpy(&x->replay, replay, sizeof(*replay));
490d51d081dSJamal Hadi Salim 		memcpy(&x->preplay, replay, sizeof(*replay));
491d51d081dSJamal Hadi Salim 	}
492d51d081dSJamal Hadi Salim 
493d51d081dSJamal Hadi Salim 	if (lt) {
494d51d081dSJamal Hadi Salim 		struct xfrm_lifetime_cur *ltime;
4955424f32eSThomas Graf 		ltime = nla_data(lt);
496d51d081dSJamal Hadi Salim 		x->curlft.bytes = ltime->bytes;
497d51d081dSJamal Hadi Salim 		x->curlft.packets = ltime->packets;
498d51d081dSJamal Hadi Salim 		x->curlft.add_time = ltime->add_time;
499d51d081dSJamal Hadi Salim 		x->curlft.use_time = ltime->use_time;
500d51d081dSJamal Hadi Salim 	}
501d51d081dSJamal Hadi Salim 
502cf5cb79fSThomas Graf 	if (et)
5035424f32eSThomas Graf 		x->replay_maxage = nla_get_u32(et);
504d51d081dSJamal Hadi Salim 
505cf5cb79fSThomas Graf 	if (rt)
5065424f32eSThomas Graf 		x->replay_maxdiff = nla_get_u32(rt);
507d51d081dSJamal Hadi Salim }
508d51d081dSJamal Hadi Salim 
509fc34acd3SAlexey Dobriyan static struct xfrm_state *xfrm_state_construct(struct net *net,
510fc34acd3SAlexey Dobriyan 					       struct xfrm_usersa_info *p,
5115424f32eSThomas Graf 					       struct nlattr **attrs,
5121da177e4SLinus Torvalds 					       int *errp)
5131da177e4SLinus Torvalds {
514fc34acd3SAlexey Dobriyan 	struct xfrm_state *x = xfrm_state_alloc(net);
5151da177e4SLinus Torvalds 	int err = -ENOMEM;
5161da177e4SLinus Torvalds 
5171da177e4SLinus Torvalds 	if (!x)
5181da177e4SLinus Torvalds 		goto error_no_put;
5191da177e4SLinus Torvalds 
5201da177e4SLinus Torvalds 	copy_from_user_state(x, p);
5211da177e4SLinus Torvalds 
522a947b0a9SNicolas Dichtel 	if (attrs[XFRMA_SA_EXTRA_FLAGS])
523a947b0a9SNicolas Dichtel 		x->props.extra_flags = nla_get_u32(attrs[XFRMA_SA_EXTRA_FLAGS]);
524a947b0a9SNicolas Dichtel 
5251a6509d9SHerbert Xu 	if ((err = attach_aead(&x->aead, &x->props.ealgo,
5261a6509d9SHerbert Xu 			       attrs[XFRMA_ALG_AEAD])))
5271a6509d9SHerbert Xu 		goto error;
5284447bb33SMartin Willi 	if ((err = attach_auth_trunc(&x->aalg, &x->props.aalgo,
5294447bb33SMartin Willi 				     attrs[XFRMA_ALG_AUTH_TRUNC])))
5304447bb33SMartin Willi 		goto error;
5314447bb33SMartin Willi 	if (!x->props.aalgo) {
5324447bb33SMartin Willi 		if ((err = attach_auth(&x->aalg, &x->props.aalgo,
53335a7aa08SThomas Graf 				       attrs[XFRMA_ALG_AUTH])))
5341da177e4SLinus Torvalds 			goto error;
5354447bb33SMartin Willi 	}
5361da177e4SLinus Torvalds 	if ((err = attach_one_algo(&x->ealg, &x->props.ealgo,
5371da177e4SLinus Torvalds 				   xfrm_ealg_get_byname,
53835a7aa08SThomas Graf 				   attrs[XFRMA_ALG_CRYPT])))
5391da177e4SLinus Torvalds 		goto error;
5401da177e4SLinus Torvalds 	if ((err = attach_one_algo(&x->calg, &x->props.calgo,
5411da177e4SLinus Torvalds 				   xfrm_calg_get_byname,
54235a7aa08SThomas Graf 				   attrs[XFRMA_ALG_COMP])))
5431da177e4SLinus Torvalds 		goto error;
544fd21150aSThomas Graf 
545fd21150aSThomas Graf 	if (attrs[XFRMA_ENCAP]) {
546fd21150aSThomas Graf 		x->encap = kmemdup(nla_data(attrs[XFRMA_ENCAP]),
547fd21150aSThomas Graf 				   sizeof(*x->encap), GFP_KERNEL);
548fd21150aSThomas Graf 		if (x->encap == NULL)
5491da177e4SLinus Torvalds 			goto error;
550fd21150aSThomas Graf 	}
551fd21150aSThomas Graf 
55235d2856bSMartin Willi 	if (attrs[XFRMA_TFCPAD])
55335d2856bSMartin Willi 		x->tfcpad = nla_get_u32(attrs[XFRMA_TFCPAD]);
55435d2856bSMartin Willi 
555fd21150aSThomas Graf 	if (attrs[XFRMA_COADDR]) {
556fd21150aSThomas Graf 		x->coaddr = kmemdup(nla_data(attrs[XFRMA_COADDR]),
557fd21150aSThomas Graf 				    sizeof(*x->coaddr), GFP_KERNEL);
558fd21150aSThomas Graf 		if (x->coaddr == NULL)
559060f02a3SNoriaki TAKAMIYA 			goto error;
560fd21150aSThomas Graf 	}
561fd21150aSThomas Graf 
5626f26b61eSJamal Hadi Salim 	xfrm_mark_get(attrs, &x->mark);
5636f26b61eSJamal Hadi Salim 
564a454f0ccSWei Yongjun 	err = __xfrm_init_state(x, false);
5651da177e4SLinus Torvalds 	if (err)
5661da177e4SLinus Torvalds 		goto error;
5671da177e4SLinus Torvalds 
568fd21150aSThomas Graf 	if (attrs[XFRMA_SEC_CTX] &&
569fd21150aSThomas Graf 	    security_xfrm_state_alloc(x, nla_data(attrs[XFRMA_SEC_CTX])))
570df71837dSTrent Jaeger 		goto error;
571df71837dSTrent Jaeger 
572d8647b79SSteffen Klassert 	if ((err = xfrm_alloc_replay_state_esn(&x->replay_esn, &x->preplay_esn,
573d8647b79SSteffen Klassert 					       attrs[XFRMA_REPLAY_ESN_VAL])))
574d8647b79SSteffen Klassert 		goto error;
575d8647b79SSteffen Klassert 
5761da177e4SLinus Torvalds 	x->km.seq = p->seq;
577b27aeadbSAlexey Dobriyan 	x->replay_maxdiff = net->xfrm.sysctl_aevent_rseqth;
578d51d081dSJamal Hadi Salim 	/* sysctl_xfrm_aevent_etime is in 100ms units */
579b27aeadbSAlexey Dobriyan 	x->replay_maxage = (net->xfrm.sysctl_aevent_etime*HZ)/XFRM_AE_ETH_M;
580d51d081dSJamal Hadi Salim 
5819fdc4883SSteffen Klassert 	if ((err = xfrm_init_replay(x)))
5829fdc4883SSteffen Klassert 		goto error;
583d51d081dSJamal Hadi Salim 
5849fdc4883SSteffen Klassert 	/* override default values from above */
585e3ac104dSMathias Krause 	xfrm_update_ae_params(x, attrs, 0);
5861da177e4SLinus Torvalds 
5871da177e4SLinus Torvalds 	return x;
5881da177e4SLinus Torvalds 
5891da177e4SLinus Torvalds error:
5901da177e4SLinus Torvalds 	x->km.state = XFRM_STATE_DEAD;
5911da177e4SLinus Torvalds 	xfrm_state_put(x);
5921da177e4SLinus Torvalds error_no_put:
5931da177e4SLinus Torvalds 	*errp = err;
5941da177e4SLinus Torvalds 	return NULL;
5951da177e4SLinus Torvalds }
5961da177e4SLinus Torvalds 
59722e70050SChristoph Hellwig static int xfrm_add_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
5985424f32eSThomas Graf 		struct nlattr **attrs)
5991da177e4SLinus Torvalds {
600fc34acd3SAlexey Dobriyan 	struct net *net = sock_net(skb->sk);
6017b67c857SThomas Graf 	struct xfrm_usersa_info *p = nlmsg_data(nlh);
6021da177e4SLinus Torvalds 	struct xfrm_state *x;
6031da177e4SLinus Torvalds 	int err;
60426b15dadSJamal Hadi Salim 	struct km_event c;
605e1760bd5SEric W. Biederman 	kuid_t loginuid = audit_get_loginuid(current);
6064440e854SEric Paris 	unsigned int sessionid = audit_get_sessionid(current);
607c53fa1edSPatrick McHardy 	u32 sid;
6081da177e4SLinus Torvalds 
60935a7aa08SThomas Graf 	err = verify_newsa_info(p, attrs);
6101da177e4SLinus Torvalds 	if (err)
6111da177e4SLinus Torvalds 		return err;
6121da177e4SLinus Torvalds 
613fc34acd3SAlexey Dobriyan 	x = xfrm_state_construct(net, p, attrs, &err);
6141da177e4SLinus Torvalds 	if (!x)
6151da177e4SLinus Torvalds 		return err;
6161da177e4SLinus Torvalds 
61726b15dadSJamal Hadi Salim 	xfrm_state_hold(x);
6181da177e4SLinus Torvalds 	if (nlh->nlmsg_type == XFRM_MSG_NEWSA)
6191da177e4SLinus Torvalds 		err = xfrm_state_add(x);
6201da177e4SLinus Torvalds 	else
6211da177e4SLinus Torvalds 		err = xfrm_state_update(x);
6221da177e4SLinus Torvalds 
623c53fa1edSPatrick McHardy 	security_task_getsecid(current, &sid);
6242532386fSEric Paris 	xfrm_audit_state_add(x, err ? 0 : 1, loginuid, sessionid, sid);
625161a09e7SJoy Latten 
6261da177e4SLinus Torvalds 	if (err < 0) {
6271da177e4SLinus Torvalds 		x->km.state = XFRM_STATE_DEAD;
62821380b81SHerbert Xu 		__xfrm_state_put(x);
6297d6dfe1fSPatrick McHardy 		goto out;
6301da177e4SLinus Torvalds 	}
6311da177e4SLinus Torvalds 
63226b15dadSJamal Hadi Salim 	c.seq = nlh->nlmsg_seq;
63315e47304SEric W. Biederman 	c.portid = nlh->nlmsg_pid;
634f60f6b8fSHerbert Xu 	c.event = nlh->nlmsg_type;
63526b15dadSJamal Hadi Salim 
63626b15dadSJamal Hadi Salim 	km_state_notify(x, &c);
6377d6dfe1fSPatrick McHardy out:
63826b15dadSJamal Hadi Salim 	xfrm_state_put(x);
6391da177e4SLinus Torvalds 	return err;
6401da177e4SLinus Torvalds }
6411da177e4SLinus Torvalds 
642fc34acd3SAlexey Dobriyan static struct xfrm_state *xfrm_user_state_lookup(struct net *net,
643fc34acd3SAlexey Dobriyan 						 struct xfrm_usersa_id *p,
6445424f32eSThomas Graf 						 struct nlattr **attrs,
645eb2971b6SMasahide NAKAMURA 						 int *errp)
646eb2971b6SMasahide NAKAMURA {
647eb2971b6SMasahide NAKAMURA 	struct xfrm_state *x = NULL;
6486f26b61eSJamal Hadi Salim 	struct xfrm_mark m;
649eb2971b6SMasahide NAKAMURA 	int err;
6506f26b61eSJamal Hadi Salim 	u32 mark = xfrm_mark_get(attrs, &m);
651eb2971b6SMasahide NAKAMURA 
652eb2971b6SMasahide NAKAMURA 	if (xfrm_id_proto_match(p->proto, IPSEC_PROTO_ANY)) {
653eb2971b6SMasahide NAKAMURA 		err = -ESRCH;
6546f26b61eSJamal Hadi Salim 		x = xfrm_state_lookup(net, mark, &p->daddr, p->spi, p->proto, p->family);
655eb2971b6SMasahide NAKAMURA 	} else {
656eb2971b6SMasahide NAKAMURA 		xfrm_address_t *saddr = NULL;
657eb2971b6SMasahide NAKAMURA 
65835a7aa08SThomas Graf 		verify_one_addr(attrs, XFRMA_SRCADDR, &saddr);
659eb2971b6SMasahide NAKAMURA 		if (!saddr) {
660eb2971b6SMasahide NAKAMURA 			err = -EINVAL;
661eb2971b6SMasahide NAKAMURA 			goto out;
662eb2971b6SMasahide NAKAMURA 		}
663eb2971b6SMasahide NAKAMURA 
6649abbffeeSMasahide NAKAMURA 		err = -ESRCH;
6656f26b61eSJamal Hadi Salim 		x = xfrm_state_lookup_byaddr(net, mark,
6666f26b61eSJamal Hadi Salim 					     &p->daddr, saddr,
667221df1edSAlexey Dobriyan 					     p->proto, p->family);
668eb2971b6SMasahide NAKAMURA 	}
669eb2971b6SMasahide NAKAMURA 
670eb2971b6SMasahide NAKAMURA  out:
671eb2971b6SMasahide NAKAMURA 	if (!x && errp)
672eb2971b6SMasahide NAKAMURA 		*errp = err;
673eb2971b6SMasahide NAKAMURA 	return x;
674eb2971b6SMasahide NAKAMURA }
675eb2971b6SMasahide NAKAMURA 
67622e70050SChristoph Hellwig static int xfrm_del_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
6775424f32eSThomas Graf 		struct nlattr **attrs)
6781da177e4SLinus Torvalds {
679fc34acd3SAlexey Dobriyan 	struct net *net = sock_net(skb->sk);
6801da177e4SLinus Torvalds 	struct xfrm_state *x;
681eb2971b6SMasahide NAKAMURA 	int err = -ESRCH;
68226b15dadSJamal Hadi Salim 	struct km_event c;
6837b67c857SThomas Graf 	struct xfrm_usersa_id *p = nlmsg_data(nlh);
684e1760bd5SEric W. Biederman 	kuid_t loginuid = audit_get_loginuid(current);
6854440e854SEric Paris 	unsigned int sessionid = audit_get_sessionid(current);
686c53fa1edSPatrick McHardy 	u32 sid;
6871da177e4SLinus Torvalds 
688fc34acd3SAlexey Dobriyan 	x = xfrm_user_state_lookup(net, p, attrs, &err);
6891da177e4SLinus Torvalds 	if (x == NULL)
690eb2971b6SMasahide NAKAMURA 		return err;
6911da177e4SLinus Torvalds 
6926f68dc37SDavid S. Miller 	if ((err = security_xfrm_state_delete(x)) != 0)
693c8c05a8eSCatherine Zhang 		goto out;
694c8c05a8eSCatherine Zhang 
6951da177e4SLinus Torvalds 	if (xfrm_state_kern(x)) {
696c8c05a8eSCatherine Zhang 		err = -EPERM;
697c8c05a8eSCatherine Zhang 		goto out;
6981da177e4SLinus Torvalds 	}
6991da177e4SLinus Torvalds 
70026b15dadSJamal Hadi Salim 	err = xfrm_state_delete(x);
701161a09e7SJoy Latten 
702c8c05a8eSCatherine Zhang 	if (err < 0)
703c8c05a8eSCatherine Zhang 		goto out;
70426b15dadSJamal Hadi Salim 
70526b15dadSJamal Hadi Salim 	c.seq = nlh->nlmsg_seq;
70615e47304SEric W. Biederman 	c.portid = nlh->nlmsg_pid;
707f60f6b8fSHerbert Xu 	c.event = nlh->nlmsg_type;
70826b15dadSJamal Hadi Salim 	km_state_notify(x, &c);
7091da177e4SLinus Torvalds 
710c8c05a8eSCatherine Zhang out:
711c53fa1edSPatrick McHardy 	security_task_getsecid(current, &sid);
7122532386fSEric Paris 	xfrm_audit_state_delete(x, err ? 0 : 1, loginuid, sessionid, sid);
713c8c05a8eSCatherine Zhang 	xfrm_state_put(x);
71426b15dadSJamal Hadi Salim 	return err;
7151da177e4SLinus Torvalds }
7161da177e4SLinus Torvalds 
7171da177e4SLinus Torvalds static void copy_to_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p)
7181da177e4SLinus Torvalds {
719f778a636SMathias Krause 	memset(p, 0, sizeof(*p));
7201da177e4SLinus Torvalds 	memcpy(&p->id, &x->id, sizeof(p->id));
7211da177e4SLinus Torvalds 	memcpy(&p->sel, &x->sel, sizeof(p->sel));
7221da177e4SLinus Torvalds 	memcpy(&p->lft, &x->lft, sizeof(p->lft));
7231da177e4SLinus Torvalds 	memcpy(&p->curlft, &x->curlft, sizeof(p->curlft));
7241da177e4SLinus Torvalds 	memcpy(&p->stats, &x->stats, sizeof(p->stats));
72554489c14SDavid S. Miller 	memcpy(&p->saddr, &x->props.saddr, sizeof(p->saddr));
7261da177e4SLinus Torvalds 	p->mode = x->props.mode;
7271da177e4SLinus Torvalds 	p->replay_window = x->props.replay_window;
7281da177e4SLinus Torvalds 	p->reqid = x->props.reqid;
7291da177e4SLinus Torvalds 	p->family = x->props.family;
7301da177e4SLinus Torvalds 	p->flags = x->props.flags;
7311da177e4SLinus Torvalds 	p->seq = x->km.seq;
7321da177e4SLinus Torvalds }
7331da177e4SLinus Torvalds 
7341da177e4SLinus Torvalds struct xfrm_dump_info {
7351da177e4SLinus Torvalds 	struct sk_buff *in_skb;
7361da177e4SLinus Torvalds 	struct sk_buff *out_skb;
7371da177e4SLinus Torvalds 	u32 nlmsg_seq;
7381da177e4SLinus Torvalds 	u16 nlmsg_flags;
7391da177e4SLinus Torvalds };
7401da177e4SLinus Torvalds 
741c0144beaSThomas Graf static int copy_sec_ctx(struct xfrm_sec_ctx *s, struct sk_buff *skb)
742c0144beaSThomas Graf {
743c0144beaSThomas Graf 	struct xfrm_user_sec_ctx *uctx;
744c0144beaSThomas Graf 	struct nlattr *attr;
74568325d3bSHerbert Xu 	int ctx_size = sizeof(*uctx) + s->ctx_len;
746c0144beaSThomas Graf 
747c0144beaSThomas Graf 	attr = nla_reserve(skb, XFRMA_SEC_CTX, ctx_size);
748c0144beaSThomas Graf 	if (attr == NULL)
749c0144beaSThomas Graf 		return -EMSGSIZE;
750c0144beaSThomas Graf 
751c0144beaSThomas Graf 	uctx = nla_data(attr);
752c0144beaSThomas Graf 	uctx->exttype = XFRMA_SEC_CTX;
753c0144beaSThomas Graf 	uctx->len = ctx_size;
754c0144beaSThomas Graf 	uctx->ctx_doi = s->ctx_doi;
755c0144beaSThomas Graf 	uctx->ctx_alg = s->ctx_alg;
756c0144beaSThomas Graf 	uctx->ctx_len = s->ctx_len;
757c0144beaSThomas Graf 	memcpy(uctx + 1, s->ctx_str, s->ctx_len);
758c0144beaSThomas Graf 
759c0144beaSThomas Graf 	return 0;
760c0144beaSThomas Graf }
761c0144beaSThomas Graf 
7624447bb33SMartin Willi static int copy_to_user_auth(struct xfrm_algo_auth *auth, struct sk_buff *skb)
7634447bb33SMartin Willi {
7644447bb33SMartin Willi 	struct xfrm_algo *algo;
7654447bb33SMartin Willi 	struct nlattr *nla;
7664447bb33SMartin Willi 
7674447bb33SMartin Willi 	nla = nla_reserve(skb, XFRMA_ALG_AUTH,
7684447bb33SMartin Willi 			  sizeof(*algo) + (auth->alg_key_len + 7) / 8);
7694447bb33SMartin Willi 	if (!nla)
7704447bb33SMartin Willi 		return -EMSGSIZE;
7714447bb33SMartin Willi 
7724447bb33SMartin Willi 	algo = nla_data(nla);
7734c87308bSMathias Krause 	strncpy(algo->alg_name, auth->alg_name, sizeof(algo->alg_name));
7744447bb33SMartin Willi 	memcpy(algo->alg_key, auth->alg_key, (auth->alg_key_len + 7) / 8);
7754447bb33SMartin Willi 	algo->alg_key_len = auth->alg_key_len;
7764447bb33SMartin Willi 
7774447bb33SMartin Willi 	return 0;
7784447bb33SMartin Willi }
7794447bb33SMartin Willi 
78068325d3bSHerbert Xu /* Don't change this without updating xfrm_sa_len! */
78168325d3bSHerbert Xu static int copy_to_user_state_extra(struct xfrm_state *x,
78268325d3bSHerbert Xu 				    struct xfrm_usersa_info *p,
78368325d3bSHerbert Xu 				    struct sk_buff *skb)
7841da177e4SLinus Torvalds {
7851d1e34ddSDavid S. Miller 	int ret = 0;
7861d1e34ddSDavid S. Miller 
7871da177e4SLinus Torvalds 	copy_to_user_state(x, p);
7881da177e4SLinus Torvalds 
789a947b0a9SNicolas Dichtel 	if (x->props.extra_flags) {
790a947b0a9SNicolas Dichtel 		ret = nla_put_u32(skb, XFRMA_SA_EXTRA_FLAGS,
791a947b0a9SNicolas Dichtel 				  x->props.extra_flags);
792a947b0a9SNicolas Dichtel 		if (ret)
793a947b0a9SNicolas Dichtel 			goto out;
794a947b0a9SNicolas Dichtel 	}
795a947b0a9SNicolas Dichtel 
7961d1e34ddSDavid S. Miller 	if (x->coaddr) {
7971d1e34ddSDavid S. Miller 		ret = nla_put(skb, XFRMA_COADDR, sizeof(*x->coaddr), x->coaddr);
7981d1e34ddSDavid S. Miller 		if (ret)
7991d1e34ddSDavid S. Miller 			goto out;
8001d1e34ddSDavid S. Miller 	}
8011d1e34ddSDavid S. Miller 	if (x->lastused) {
8021d1e34ddSDavid S. Miller 		ret = nla_put_u64(skb, XFRMA_LASTUSED, x->lastused);
8031d1e34ddSDavid S. Miller 		if (ret)
8041d1e34ddSDavid S. Miller 			goto out;
8051d1e34ddSDavid S. Miller 	}
8061d1e34ddSDavid S. Miller 	if (x->aead) {
8071d1e34ddSDavid S. Miller 		ret = nla_put(skb, XFRMA_ALG_AEAD, aead_len(x->aead), x->aead);
8081d1e34ddSDavid S. Miller 		if (ret)
8091d1e34ddSDavid S. Miller 			goto out;
8101d1e34ddSDavid S. Miller 	}
8111d1e34ddSDavid S. Miller 	if (x->aalg) {
8121d1e34ddSDavid S. Miller 		ret = copy_to_user_auth(x->aalg, skb);
8131d1e34ddSDavid S. Miller 		if (!ret)
8141d1e34ddSDavid S. Miller 			ret = nla_put(skb, XFRMA_ALG_AUTH_TRUNC,
8151d1e34ddSDavid S. Miller 				      xfrm_alg_auth_len(x->aalg), x->aalg);
8161d1e34ddSDavid S. Miller 		if (ret)
8171d1e34ddSDavid S. Miller 			goto out;
8181d1e34ddSDavid S. Miller 	}
8191d1e34ddSDavid S. Miller 	if (x->ealg) {
8201d1e34ddSDavid S. Miller 		ret = nla_put(skb, XFRMA_ALG_CRYPT, xfrm_alg_len(x->ealg), x->ealg);
8211d1e34ddSDavid S. Miller 		if (ret)
8221d1e34ddSDavid S. Miller 			goto out;
8231d1e34ddSDavid S. Miller 	}
8241d1e34ddSDavid S. Miller 	if (x->calg) {
8251d1e34ddSDavid S. Miller 		ret = nla_put(skb, XFRMA_ALG_COMP, sizeof(*(x->calg)), x->calg);
8261d1e34ddSDavid S. Miller 		if (ret)
8271d1e34ddSDavid S. Miller 			goto out;
8281d1e34ddSDavid S. Miller 	}
8291d1e34ddSDavid S. Miller 	if (x->encap) {
8301d1e34ddSDavid S. Miller 		ret = nla_put(skb, XFRMA_ENCAP, sizeof(*x->encap), x->encap);
8311d1e34ddSDavid S. Miller 		if (ret)
8321d1e34ddSDavid S. Miller 			goto out;
8331d1e34ddSDavid S. Miller 	}
8341d1e34ddSDavid S. Miller 	if (x->tfcpad) {
8351d1e34ddSDavid S. Miller 		ret = nla_put_u32(skb, XFRMA_TFCPAD, x->tfcpad);
8361d1e34ddSDavid S. Miller 		if (ret)
8371d1e34ddSDavid S. Miller 			goto out;
8381d1e34ddSDavid S. Miller 	}
8391d1e34ddSDavid S. Miller 	ret = xfrm_mark_put(skb, &x->mark);
8401d1e34ddSDavid S. Miller 	if (ret)
8411d1e34ddSDavid S. Miller 		goto out;
8421d1e34ddSDavid S. Miller 	if (x->replay_esn) {
8431d1e34ddSDavid S. Miller 		ret = nla_put(skb, XFRMA_REPLAY_ESN_VAL,
844d0fde795SDavid S. Miller 			      xfrm_replay_state_esn_len(x->replay_esn),
8451d1e34ddSDavid S. Miller 			      x->replay_esn);
8461d1e34ddSDavid S. Miller 		if (ret)
8471d1e34ddSDavid S. Miller 			goto out;
8481d1e34ddSDavid S. Miller 	}
8491d1e34ddSDavid S. Miller 	if (x->security)
8501d1e34ddSDavid S. Miller 		ret = copy_sec_ctx(x->security, skb);
8511d1e34ddSDavid S. Miller out:
8521d1e34ddSDavid S. Miller 	return ret;
85368325d3bSHerbert Xu }
85468325d3bSHerbert Xu 
85568325d3bSHerbert Xu static int dump_one_state(struct xfrm_state *x, int count, void *ptr)
85668325d3bSHerbert Xu {
85768325d3bSHerbert Xu 	struct xfrm_dump_info *sp = ptr;
85868325d3bSHerbert Xu 	struct sk_buff *in_skb = sp->in_skb;
85968325d3bSHerbert Xu 	struct sk_buff *skb = sp->out_skb;
86068325d3bSHerbert Xu 	struct xfrm_usersa_info *p;
86168325d3bSHerbert Xu 	struct nlmsghdr *nlh;
86268325d3bSHerbert Xu 	int err;
86368325d3bSHerbert Xu 
86415e47304SEric W. Biederman 	nlh = nlmsg_put(skb, NETLINK_CB(in_skb).portid, sp->nlmsg_seq,
86568325d3bSHerbert Xu 			XFRM_MSG_NEWSA, sizeof(*p), sp->nlmsg_flags);
86668325d3bSHerbert Xu 	if (nlh == NULL)
86768325d3bSHerbert Xu 		return -EMSGSIZE;
86868325d3bSHerbert Xu 
86968325d3bSHerbert Xu 	p = nlmsg_data(nlh);
87068325d3bSHerbert Xu 
87168325d3bSHerbert Xu 	err = copy_to_user_state_extra(x, p, skb);
8721d1e34ddSDavid S. Miller 	if (err) {
8739825069dSThomas Graf 		nlmsg_cancel(skb, nlh);
87468325d3bSHerbert Xu 		return err;
8751da177e4SLinus Torvalds 	}
8761d1e34ddSDavid S. Miller 	nlmsg_end(skb, nlh);
8771d1e34ddSDavid S. Miller 	return 0;
8781d1e34ddSDavid S. Miller }
8791da177e4SLinus Torvalds 
8804c563f76STimo Teras static int xfrm_dump_sa_done(struct netlink_callback *cb)
8814c563f76STimo Teras {
8824c563f76STimo Teras 	struct xfrm_state_walk *walk = (struct xfrm_state_walk *) &cb->args[1];
883283bc9f3SFan Du 	struct sock *sk = cb->skb->sk;
884283bc9f3SFan Du 	struct net *net = sock_net(sk);
885283bc9f3SFan Du 
886283bc9f3SFan Du 	xfrm_state_walk_done(walk, net);
8874c563f76STimo Teras 	return 0;
8884c563f76STimo Teras }
8894c563f76STimo Teras 
890*d3623099SNicolas Dichtel static const struct nla_policy xfrma_policy[XFRMA_MAX+1];
8911da177e4SLinus Torvalds static int xfrm_dump_sa(struct sk_buff *skb, struct netlink_callback *cb)
8921da177e4SLinus Torvalds {
893fc34acd3SAlexey Dobriyan 	struct net *net = sock_net(skb->sk);
8944c563f76STimo Teras 	struct xfrm_state_walk *walk = (struct xfrm_state_walk *) &cb->args[1];
8951da177e4SLinus Torvalds 	struct xfrm_dump_info info;
8961da177e4SLinus Torvalds 
8974c563f76STimo Teras 	BUILD_BUG_ON(sizeof(struct xfrm_state_walk) >
8984c563f76STimo Teras 		     sizeof(cb->args) - sizeof(cb->args[0]));
8994c563f76STimo Teras 
9001da177e4SLinus Torvalds 	info.in_skb = cb->skb;
9011da177e4SLinus Torvalds 	info.out_skb = skb;
9021da177e4SLinus Torvalds 	info.nlmsg_seq = cb->nlh->nlmsg_seq;
9031da177e4SLinus Torvalds 	info.nlmsg_flags = NLM_F_MULTI;
9044c563f76STimo Teras 
9054c563f76STimo Teras 	if (!cb->args[0]) {
906*d3623099SNicolas Dichtel 		struct nlattr *attrs[XFRMA_MAX+1];
907*d3623099SNicolas Dichtel 		struct xfrm_filter *filter = NULL;
908*d3623099SNicolas Dichtel 		u8 proto = 0;
909*d3623099SNicolas Dichtel 		int err;
910*d3623099SNicolas Dichtel 
9114c563f76STimo Teras 		cb->args[0] = 1;
912*d3623099SNicolas Dichtel 
913*d3623099SNicolas Dichtel 		err = nlmsg_parse(cb->nlh, 0, attrs, XFRMA_MAX,
914*d3623099SNicolas Dichtel 				  xfrma_policy);
915*d3623099SNicolas Dichtel 		if (err < 0)
916*d3623099SNicolas Dichtel 			return err;
917*d3623099SNicolas Dichtel 
918*d3623099SNicolas Dichtel 		if (attrs[XFRMA_FILTER]) {
919*d3623099SNicolas Dichtel 			filter = kmalloc(sizeof(*filter), GFP_KERNEL);
920*d3623099SNicolas Dichtel 			if (filter == NULL)
921*d3623099SNicolas Dichtel 				return -ENOMEM;
922*d3623099SNicolas Dichtel 
923*d3623099SNicolas Dichtel 			memcpy(filter, nla_data(attrs[XFRMA_FILTER]),
924*d3623099SNicolas Dichtel 			       sizeof(*filter));
925*d3623099SNicolas Dichtel 		}
926*d3623099SNicolas Dichtel 
927*d3623099SNicolas Dichtel 		if (attrs[XFRMA_PROTO])
928*d3623099SNicolas Dichtel 			proto = nla_get_u8(attrs[XFRMA_PROTO]);
929*d3623099SNicolas Dichtel 
930*d3623099SNicolas Dichtel 		xfrm_state_walk_init(walk, proto, filter);
9314c563f76STimo Teras 	}
9324c563f76STimo Teras 
933fc34acd3SAlexey Dobriyan 	(void) xfrm_state_walk(net, walk, dump_one_state, &info);
9341da177e4SLinus Torvalds 
9351da177e4SLinus Torvalds 	return skb->len;
9361da177e4SLinus Torvalds }
9371da177e4SLinus Torvalds 
9381da177e4SLinus Torvalds static struct sk_buff *xfrm_state_netlink(struct sk_buff *in_skb,
9391da177e4SLinus Torvalds 					  struct xfrm_state *x, u32 seq)
9401da177e4SLinus Torvalds {
9411da177e4SLinus Torvalds 	struct xfrm_dump_info info;
9421da177e4SLinus Torvalds 	struct sk_buff *skb;
943864745d2SMathias Krause 	int err;
9441da177e4SLinus Torvalds 
9457deb2264SThomas Graf 	skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
9461da177e4SLinus Torvalds 	if (!skb)
9471da177e4SLinus Torvalds 		return ERR_PTR(-ENOMEM);
9481da177e4SLinus Torvalds 
9491da177e4SLinus Torvalds 	info.in_skb = in_skb;
9501da177e4SLinus Torvalds 	info.out_skb = skb;
9511da177e4SLinus Torvalds 	info.nlmsg_seq = seq;
9521da177e4SLinus Torvalds 	info.nlmsg_flags = 0;
9531da177e4SLinus Torvalds 
954864745d2SMathias Krause 	err = dump_one_state(x, 0, &info);
955864745d2SMathias Krause 	if (err) {
9561da177e4SLinus Torvalds 		kfree_skb(skb);
957864745d2SMathias Krause 		return ERR_PTR(err);
9581da177e4SLinus Torvalds 	}
9591da177e4SLinus Torvalds 
9601da177e4SLinus Torvalds 	return skb;
9611da177e4SLinus Torvalds }
9621da177e4SLinus Torvalds 
9637deb2264SThomas Graf static inline size_t xfrm_spdinfo_msgsize(void)
9647deb2264SThomas Graf {
9657deb2264SThomas Graf 	return NLMSG_ALIGN(4)
9667deb2264SThomas Graf 	       + nla_total_size(sizeof(struct xfrmu_spdinfo))
9677deb2264SThomas Graf 	       + nla_total_size(sizeof(struct xfrmu_spdhinfo));
9687deb2264SThomas Graf }
9697deb2264SThomas Graf 
970e071041bSAlexey Dobriyan static int build_spdinfo(struct sk_buff *skb, struct net *net,
97115e47304SEric W. Biederman 			 u32 portid, u32 seq, u32 flags)
972ecfd6b18SJamal Hadi Salim {
9735a6d3416SJamal Hadi Salim 	struct xfrmk_spdinfo si;
9745a6d3416SJamal Hadi Salim 	struct xfrmu_spdinfo spc;
9755a6d3416SJamal Hadi Salim 	struct xfrmu_spdhinfo sph;
976ecfd6b18SJamal Hadi Salim 	struct nlmsghdr *nlh;
9771d1e34ddSDavid S. Miller 	int err;
978ecfd6b18SJamal Hadi Salim 	u32 *f;
979ecfd6b18SJamal Hadi Salim 
98015e47304SEric W. Biederman 	nlh = nlmsg_put(skb, portid, seq, XFRM_MSG_NEWSPDINFO, sizeof(u32), 0);
98125985edcSLucas De Marchi 	if (nlh == NULL) /* shouldn't really happen ... */
982ecfd6b18SJamal Hadi Salim 		return -EMSGSIZE;
983ecfd6b18SJamal Hadi Salim 
984ecfd6b18SJamal Hadi Salim 	f = nlmsg_data(nlh);
985ecfd6b18SJamal Hadi Salim 	*f = flags;
986e071041bSAlexey Dobriyan 	xfrm_spd_getinfo(net, &si);
9875a6d3416SJamal Hadi Salim 	spc.incnt = si.incnt;
9885a6d3416SJamal Hadi Salim 	spc.outcnt = si.outcnt;
9895a6d3416SJamal Hadi Salim 	spc.fwdcnt = si.fwdcnt;
9905a6d3416SJamal Hadi Salim 	spc.inscnt = si.inscnt;
9915a6d3416SJamal Hadi Salim 	spc.outscnt = si.outscnt;
9925a6d3416SJamal Hadi Salim 	spc.fwdscnt = si.fwdscnt;
9935a6d3416SJamal Hadi Salim 	sph.spdhcnt = si.spdhcnt;
9945a6d3416SJamal Hadi Salim 	sph.spdhmcnt = si.spdhmcnt;
995ecfd6b18SJamal Hadi Salim 
9961d1e34ddSDavid S. Miller 	err = nla_put(skb, XFRMA_SPD_INFO, sizeof(spc), &spc);
9971d1e34ddSDavid S. Miller 	if (!err)
9981d1e34ddSDavid S. Miller 		err = nla_put(skb, XFRMA_SPD_HINFO, sizeof(sph), &sph);
9991d1e34ddSDavid S. Miller 	if (err) {
10001d1e34ddSDavid S. Miller 		nlmsg_cancel(skb, nlh);
10011d1e34ddSDavid S. Miller 		return err;
10021d1e34ddSDavid S. Miller 	}
1003ecfd6b18SJamal Hadi Salim 
1004ecfd6b18SJamal Hadi Salim 	return nlmsg_end(skb, nlh);
1005ecfd6b18SJamal Hadi Salim }
1006ecfd6b18SJamal Hadi Salim 
1007ecfd6b18SJamal Hadi Salim static int xfrm_get_spdinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
10085424f32eSThomas Graf 		struct nlattr **attrs)
1009ecfd6b18SJamal Hadi Salim {
1010a6483b79SAlexey Dobriyan 	struct net *net = sock_net(skb->sk);
1011ecfd6b18SJamal Hadi Salim 	struct sk_buff *r_skb;
10127b67c857SThomas Graf 	u32 *flags = nlmsg_data(nlh);
101315e47304SEric W. Biederman 	u32 sportid = NETLINK_CB(skb).portid;
1014ecfd6b18SJamal Hadi Salim 	u32 seq = nlh->nlmsg_seq;
1015ecfd6b18SJamal Hadi Salim 
10167deb2264SThomas Graf 	r_skb = nlmsg_new(xfrm_spdinfo_msgsize(), GFP_ATOMIC);
1017ecfd6b18SJamal Hadi Salim 	if (r_skb == NULL)
1018ecfd6b18SJamal Hadi Salim 		return -ENOMEM;
1019ecfd6b18SJamal Hadi Salim 
102015e47304SEric W. Biederman 	if (build_spdinfo(r_skb, net, sportid, seq, *flags) < 0)
1021ecfd6b18SJamal Hadi Salim 		BUG();
1022ecfd6b18SJamal Hadi Salim 
102315e47304SEric W. Biederman 	return nlmsg_unicast(net->xfrm.nlsk, r_skb, sportid);
1024ecfd6b18SJamal Hadi Salim }
1025ecfd6b18SJamal Hadi Salim 
10267deb2264SThomas Graf static inline size_t xfrm_sadinfo_msgsize(void)
10277deb2264SThomas Graf {
10287deb2264SThomas Graf 	return NLMSG_ALIGN(4)
10297deb2264SThomas Graf 	       + nla_total_size(sizeof(struct xfrmu_sadhinfo))
10307deb2264SThomas Graf 	       + nla_total_size(4); /* XFRMA_SAD_CNT */
10317deb2264SThomas Graf }
10327deb2264SThomas Graf 
1033e071041bSAlexey Dobriyan static int build_sadinfo(struct sk_buff *skb, struct net *net,
103415e47304SEric W. Biederman 			 u32 portid, u32 seq, u32 flags)
103528d8909bSJamal Hadi Salim {
1036af11e316SJamal Hadi Salim 	struct xfrmk_sadinfo si;
1037af11e316SJamal Hadi Salim 	struct xfrmu_sadhinfo sh;
103828d8909bSJamal Hadi Salim 	struct nlmsghdr *nlh;
10391d1e34ddSDavid S. Miller 	int err;
104028d8909bSJamal Hadi Salim 	u32 *f;
104128d8909bSJamal Hadi Salim 
104215e47304SEric W. Biederman 	nlh = nlmsg_put(skb, portid, seq, XFRM_MSG_NEWSADINFO, sizeof(u32), 0);
104325985edcSLucas De Marchi 	if (nlh == NULL) /* shouldn't really happen ... */
104428d8909bSJamal Hadi Salim 		return -EMSGSIZE;
104528d8909bSJamal Hadi Salim 
104628d8909bSJamal Hadi Salim 	f = nlmsg_data(nlh);
104728d8909bSJamal Hadi Salim 	*f = flags;
1048e071041bSAlexey Dobriyan 	xfrm_sad_getinfo(net, &si);
104928d8909bSJamal Hadi Salim 
1050af11e316SJamal Hadi Salim 	sh.sadhmcnt = si.sadhmcnt;
1051af11e316SJamal Hadi Salim 	sh.sadhcnt = si.sadhcnt;
1052af11e316SJamal Hadi Salim 
10531d1e34ddSDavid S. Miller 	err = nla_put_u32(skb, XFRMA_SAD_CNT, si.sadcnt);
10541d1e34ddSDavid S. Miller 	if (!err)
10551d1e34ddSDavid S. Miller 		err = nla_put(skb, XFRMA_SAD_HINFO, sizeof(sh), &sh);
10561d1e34ddSDavid S. Miller 	if (err) {
10571d1e34ddSDavid S. Miller 		nlmsg_cancel(skb, nlh);
10581d1e34ddSDavid S. Miller 		return err;
10591d1e34ddSDavid S. Miller 	}
106028d8909bSJamal Hadi Salim 
106128d8909bSJamal Hadi Salim 	return nlmsg_end(skb, nlh);
106228d8909bSJamal Hadi Salim }
106328d8909bSJamal Hadi Salim 
106428d8909bSJamal Hadi Salim static int xfrm_get_sadinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
10655424f32eSThomas Graf 		struct nlattr **attrs)
106628d8909bSJamal Hadi Salim {
1067a6483b79SAlexey Dobriyan 	struct net *net = sock_net(skb->sk);
106828d8909bSJamal Hadi Salim 	struct sk_buff *r_skb;
10697b67c857SThomas Graf 	u32 *flags = nlmsg_data(nlh);
107015e47304SEric W. Biederman 	u32 sportid = NETLINK_CB(skb).portid;
107128d8909bSJamal Hadi Salim 	u32 seq = nlh->nlmsg_seq;
107228d8909bSJamal Hadi Salim 
10737deb2264SThomas Graf 	r_skb = nlmsg_new(xfrm_sadinfo_msgsize(), GFP_ATOMIC);
107428d8909bSJamal Hadi Salim 	if (r_skb == NULL)
107528d8909bSJamal Hadi Salim 		return -ENOMEM;
107628d8909bSJamal Hadi Salim 
107715e47304SEric W. Biederman 	if (build_sadinfo(r_skb, net, sportid, seq, *flags) < 0)
107828d8909bSJamal Hadi Salim 		BUG();
107928d8909bSJamal Hadi Salim 
108015e47304SEric W. Biederman 	return nlmsg_unicast(net->xfrm.nlsk, r_skb, sportid);
108128d8909bSJamal Hadi Salim }
108228d8909bSJamal Hadi Salim 
108322e70050SChristoph Hellwig static int xfrm_get_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
10845424f32eSThomas Graf 		struct nlattr **attrs)
10851da177e4SLinus Torvalds {
1086fc34acd3SAlexey Dobriyan 	struct net *net = sock_net(skb->sk);
10877b67c857SThomas Graf 	struct xfrm_usersa_id *p = nlmsg_data(nlh);
10881da177e4SLinus Torvalds 	struct xfrm_state *x;
10891da177e4SLinus Torvalds 	struct sk_buff *resp_skb;
1090eb2971b6SMasahide NAKAMURA 	int err = -ESRCH;
10911da177e4SLinus Torvalds 
1092fc34acd3SAlexey Dobriyan 	x = xfrm_user_state_lookup(net, p, attrs, &err);
10931da177e4SLinus Torvalds 	if (x == NULL)
10941da177e4SLinus Torvalds 		goto out_noput;
10951da177e4SLinus Torvalds 
10961da177e4SLinus Torvalds 	resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
10971da177e4SLinus Torvalds 	if (IS_ERR(resp_skb)) {
10981da177e4SLinus Torvalds 		err = PTR_ERR(resp_skb);
10991da177e4SLinus Torvalds 	} else {
110015e47304SEric W. Biederman 		err = nlmsg_unicast(net->xfrm.nlsk, resp_skb, NETLINK_CB(skb).portid);
11011da177e4SLinus Torvalds 	}
11021da177e4SLinus Torvalds 	xfrm_state_put(x);
11031da177e4SLinus Torvalds out_noput:
11041da177e4SLinus Torvalds 	return err;
11051da177e4SLinus Torvalds }
11061da177e4SLinus Torvalds 
110722e70050SChristoph Hellwig static int xfrm_alloc_userspi(struct sk_buff *skb, struct nlmsghdr *nlh,
11085424f32eSThomas Graf 		struct nlattr **attrs)
11091da177e4SLinus Torvalds {
1110fc34acd3SAlexey Dobriyan 	struct net *net = sock_net(skb->sk);
11111da177e4SLinus Torvalds 	struct xfrm_state *x;
11121da177e4SLinus Torvalds 	struct xfrm_userspi_info *p;
11131da177e4SLinus Torvalds 	struct sk_buff *resp_skb;
11141da177e4SLinus Torvalds 	xfrm_address_t *daddr;
11151da177e4SLinus Torvalds 	int family;
11161da177e4SLinus Torvalds 	int err;
11176f26b61eSJamal Hadi Salim 	u32 mark;
11186f26b61eSJamal Hadi Salim 	struct xfrm_mark m;
11191da177e4SLinus Torvalds 
11207b67c857SThomas Graf 	p = nlmsg_data(nlh);
1121776e9dd9SFan Du 	err = verify_spi_info(p->info.id.proto, p->min, p->max);
11221da177e4SLinus Torvalds 	if (err)
11231da177e4SLinus Torvalds 		goto out_noput;
11241da177e4SLinus Torvalds 
11251da177e4SLinus Torvalds 	family = p->info.family;
11261da177e4SLinus Torvalds 	daddr = &p->info.id.daddr;
11271da177e4SLinus Torvalds 
11281da177e4SLinus Torvalds 	x = NULL;
11296f26b61eSJamal Hadi Salim 
11306f26b61eSJamal Hadi Salim 	mark = xfrm_mark_get(attrs, &m);
11311da177e4SLinus Torvalds 	if (p->info.seq) {
11326f26b61eSJamal Hadi Salim 		x = xfrm_find_acq_byseq(net, mark, p->info.seq);
113370e94e66SYOSHIFUJI Hideaki / 吉藤英明 		if (x && !xfrm_addr_equal(&x->id.daddr, daddr, family)) {
11341da177e4SLinus Torvalds 			xfrm_state_put(x);
11351da177e4SLinus Torvalds 			x = NULL;
11361da177e4SLinus Torvalds 		}
11371da177e4SLinus Torvalds 	}
11381da177e4SLinus Torvalds 
11391da177e4SLinus Torvalds 	if (!x)
11406f26b61eSJamal Hadi Salim 		x = xfrm_find_acq(net, &m, p->info.mode, p->info.reqid,
11411da177e4SLinus Torvalds 				  p->info.id.proto, daddr,
11421da177e4SLinus Torvalds 				  &p->info.saddr, 1,
11431da177e4SLinus Torvalds 				  family);
11441da177e4SLinus Torvalds 	err = -ENOENT;
11451da177e4SLinus Torvalds 	if (x == NULL)
11461da177e4SLinus Torvalds 		goto out_noput;
11471da177e4SLinus Torvalds 
1148658b219eSHerbert Xu 	err = xfrm_alloc_spi(x, p->min, p->max);
1149658b219eSHerbert Xu 	if (err)
1150658b219eSHerbert Xu 		goto out;
11511da177e4SLinus Torvalds 
11521da177e4SLinus Torvalds 	resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
11531da177e4SLinus Torvalds 	if (IS_ERR(resp_skb)) {
11541da177e4SLinus Torvalds 		err = PTR_ERR(resp_skb);
11551da177e4SLinus Torvalds 		goto out;
11561da177e4SLinus Torvalds 	}
11571da177e4SLinus Torvalds 
115815e47304SEric W. Biederman 	err = nlmsg_unicast(net->xfrm.nlsk, resp_skb, NETLINK_CB(skb).portid);
11591da177e4SLinus Torvalds 
11601da177e4SLinus Torvalds out:
11611da177e4SLinus Torvalds 	xfrm_state_put(x);
11621da177e4SLinus Torvalds out_noput:
11631da177e4SLinus Torvalds 	return err;
11641da177e4SLinus Torvalds }
11651da177e4SLinus Torvalds 
1166b798a9edSJamal Hadi Salim static int verify_policy_dir(u8 dir)
11671da177e4SLinus Torvalds {
11681da177e4SLinus Torvalds 	switch (dir) {
11691da177e4SLinus Torvalds 	case XFRM_POLICY_IN:
11701da177e4SLinus Torvalds 	case XFRM_POLICY_OUT:
11711da177e4SLinus Torvalds 	case XFRM_POLICY_FWD:
11721da177e4SLinus Torvalds 		break;
11731da177e4SLinus Torvalds 
11741da177e4SLinus Torvalds 	default:
11751da177e4SLinus Torvalds 		return -EINVAL;
11763ff50b79SStephen Hemminger 	}
11771da177e4SLinus Torvalds 
11781da177e4SLinus Torvalds 	return 0;
11791da177e4SLinus Torvalds }
11801da177e4SLinus Torvalds 
1181b798a9edSJamal Hadi Salim static int verify_policy_type(u8 type)
1182f7b6983fSMasahide NAKAMURA {
1183f7b6983fSMasahide NAKAMURA 	switch (type) {
1184f7b6983fSMasahide NAKAMURA 	case XFRM_POLICY_TYPE_MAIN:
1185f7b6983fSMasahide NAKAMURA #ifdef CONFIG_XFRM_SUB_POLICY
1186f7b6983fSMasahide NAKAMURA 	case XFRM_POLICY_TYPE_SUB:
1187f7b6983fSMasahide NAKAMURA #endif
1188f7b6983fSMasahide NAKAMURA 		break;
1189f7b6983fSMasahide NAKAMURA 
1190f7b6983fSMasahide NAKAMURA 	default:
1191f7b6983fSMasahide NAKAMURA 		return -EINVAL;
11923ff50b79SStephen Hemminger 	}
1193f7b6983fSMasahide NAKAMURA 
1194f7b6983fSMasahide NAKAMURA 	return 0;
1195f7b6983fSMasahide NAKAMURA }
1196f7b6983fSMasahide NAKAMURA 
11971da177e4SLinus Torvalds static int verify_newpolicy_info(struct xfrm_userpolicy_info *p)
11981da177e4SLinus Torvalds {
1199e682adf0SFan Du 	int ret;
1200e682adf0SFan Du 
12011da177e4SLinus Torvalds 	switch (p->share) {
12021da177e4SLinus Torvalds 	case XFRM_SHARE_ANY:
12031da177e4SLinus Torvalds 	case XFRM_SHARE_SESSION:
12041da177e4SLinus Torvalds 	case XFRM_SHARE_USER:
12051da177e4SLinus Torvalds 	case XFRM_SHARE_UNIQUE:
12061da177e4SLinus Torvalds 		break;
12071da177e4SLinus Torvalds 
12081da177e4SLinus Torvalds 	default:
12091da177e4SLinus Torvalds 		return -EINVAL;
12103ff50b79SStephen Hemminger 	}
12111da177e4SLinus Torvalds 
12121da177e4SLinus Torvalds 	switch (p->action) {
12131da177e4SLinus Torvalds 	case XFRM_POLICY_ALLOW:
12141da177e4SLinus Torvalds 	case XFRM_POLICY_BLOCK:
12151da177e4SLinus Torvalds 		break;
12161da177e4SLinus Torvalds 
12171da177e4SLinus Torvalds 	default:
12181da177e4SLinus Torvalds 		return -EINVAL;
12193ff50b79SStephen Hemminger 	}
12201da177e4SLinus Torvalds 
12211da177e4SLinus Torvalds 	switch (p->sel.family) {
12221da177e4SLinus Torvalds 	case AF_INET:
12231da177e4SLinus Torvalds 		break;
12241da177e4SLinus Torvalds 
12251da177e4SLinus Torvalds 	case AF_INET6:
1226dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6)
12271da177e4SLinus Torvalds 		break;
12281da177e4SLinus Torvalds #else
12291da177e4SLinus Torvalds 		return  -EAFNOSUPPORT;
12301da177e4SLinus Torvalds #endif
12311da177e4SLinus Torvalds 
12321da177e4SLinus Torvalds 	default:
12331da177e4SLinus Torvalds 		return -EINVAL;
12343ff50b79SStephen Hemminger 	}
12351da177e4SLinus Torvalds 
1236e682adf0SFan Du 	ret = verify_policy_dir(p->dir);
1237e682adf0SFan Du 	if (ret)
1238e682adf0SFan Du 		return ret;
1239e682adf0SFan Du 	if (p->index && ((p->index & XFRM_POLICY_MAX) != p->dir))
1240e682adf0SFan Du 		return -EINVAL;
1241e682adf0SFan Du 
1242e682adf0SFan Du 	return 0;
12431da177e4SLinus Torvalds }
12441da177e4SLinus Torvalds 
12455424f32eSThomas Graf static int copy_from_user_sec_ctx(struct xfrm_policy *pol, struct nlattr **attrs)
1246df71837dSTrent Jaeger {
12475424f32eSThomas Graf 	struct nlattr *rt = attrs[XFRMA_SEC_CTX];
1248df71837dSTrent Jaeger 	struct xfrm_user_sec_ctx *uctx;
1249df71837dSTrent Jaeger 
1250df71837dSTrent Jaeger 	if (!rt)
1251df71837dSTrent Jaeger 		return 0;
1252df71837dSTrent Jaeger 
12535424f32eSThomas Graf 	uctx = nla_data(rt);
125403e1ad7bSPaul Moore 	return security_xfrm_policy_alloc(&pol->security, uctx);
1255df71837dSTrent Jaeger }
1256df71837dSTrent Jaeger 
12571da177e4SLinus Torvalds static void copy_templates(struct xfrm_policy *xp, struct xfrm_user_tmpl *ut,
12581da177e4SLinus Torvalds 			   int nr)
12591da177e4SLinus Torvalds {
12601da177e4SLinus Torvalds 	int i;
12611da177e4SLinus Torvalds 
12621da177e4SLinus Torvalds 	xp->xfrm_nr = nr;
12631da177e4SLinus Torvalds 	for (i = 0; i < nr; i++, ut++) {
12641da177e4SLinus Torvalds 		struct xfrm_tmpl *t = &xp->xfrm_vec[i];
12651da177e4SLinus Torvalds 
12661da177e4SLinus Torvalds 		memcpy(&t->id, &ut->id, sizeof(struct xfrm_id));
12671da177e4SLinus Torvalds 		memcpy(&t->saddr, &ut->saddr,
12681da177e4SLinus Torvalds 		       sizeof(xfrm_address_t));
12691da177e4SLinus Torvalds 		t->reqid = ut->reqid;
12701da177e4SLinus Torvalds 		t->mode = ut->mode;
12711da177e4SLinus Torvalds 		t->share = ut->share;
12721da177e4SLinus Torvalds 		t->optional = ut->optional;
12731da177e4SLinus Torvalds 		t->aalgos = ut->aalgos;
12741da177e4SLinus Torvalds 		t->ealgos = ut->ealgos;
12751da177e4SLinus Torvalds 		t->calgos = ut->calgos;
1276c5d18e98SHerbert Xu 		/* If all masks are ~0, then we allow all algorithms. */
1277c5d18e98SHerbert Xu 		t->allalgs = !~(t->aalgos & t->ealgos & t->calgos);
12788511d01dSMiika Komu 		t->encap_family = ut->family;
12791da177e4SLinus Torvalds 	}
12801da177e4SLinus Torvalds }
12811da177e4SLinus Torvalds 
1282b4ad86bfSDavid S. Miller static int validate_tmpl(int nr, struct xfrm_user_tmpl *ut, u16 family)
1283b4ad86bfSDavid S. Miller {
1284b4ad86bfSDavid S. Miller 	int i;
1285b4ad86bfSDavid S. Miller 
1286b4ad86bfSDavid S. Miller 	if (nr > XFRM_MAX_DEPTH)
1287b4ad86bfSDavid S. Miller 		return -EINVAL;
1288b4ad86bfSDavid S. Miller 
1289b4ad86bfSDavid S. Miller 	for (i = 0; i < nr; i++) {
1290b4ad86bfSDavid S. Miller 		/* We never validated the ut->family value, so many
1291b4ad86bfSDavid S. Miller 		 * applications simply leave it at zero.  The check was
1292b4ad86bfSDavid S. Miller 		 * never made and ut->family was ignored because all
1293b4ad86bfSDavid S. Miller 		 * templates could be assumed to have the same family as
1294b4ad86bfSDavid S. Miller 		 * the policy itself.  Now that we will have ipv4-in-ipv6
1295b4ad86bfSDavid S. Miller 		 * and ipv6-in-ipv4 tunnels, this is no longer true.
1296b4ad86bfSDavid S. Miller 		 */
1297b4ad86bfSDavid S. Miller 		if (!ut[i].family)
1298b4ad86bfSDavid S. Miller 			ut[i].family = family;
1299b4ad86bfSDavid S. Miller 
1300b4ad86bfSDavid S. Miller 		switch (ut[i].family) {
1301b4ad86bfSDavid S. Miller 		case AF_INET:
1302b4ad86bfSDavid S. Miller 			break;
1303dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6)
1304b4ad86bfSDavid S. Miller 		case AF_INET6:
1305b4ad86bfSDavid S. Miller 			break;
1306b4ad86bfSDavid S. Miller #endif
1307b4ad86bfSDavid S. Miller 		default:
1308b4ad86bfSDavid S. Miller 			return -EINVAL;
13093ff50b79SStephen Hemminger 		}
1310b4ad86bfSDavid S. Miller 	}
1311b4ad86bfSDavid S. Miller 
1312b4ad86bfSDavid S. Miller 	return 0;
1313b4ad86bfSDavid S. Miller }
1314b4ad86bfSDavid S. Miller 
13155424f32eSThomas Graf static int copy_from_user_tmpl(struct xfrm_policy *pol, struct nlattr **attrs)
13161da177e4SLinus Torvalds {
13175424f32eSThomas Graf 	struct nlattr *rt = attrs[XFRMA_TMPL];
13181da177e4SLinus Torvalds 
13191da177e4SLinus Torvalds 	if (!rt) {
13201da177e4SLinus Torvalds 		pol->xfrm_nr = 0;
13211da177e4SLinus Torvalds 	} else {
13225424f32eSThomas Graf 		struct xfrm_user_tmpl *utmpl = nla_data(rt);
13235424f32eSThomas Graf 		int nr = nla_len(rt) / sizeof(*utmpl);
1324b4ad86bfSDavid S. Miller 		int err;
13251da177e4SLinus Torvalds 
1326b4ad86bfSDavid S. Miller 		err = validate_tmpl(nr, utmpl, pol->family);
1327b4ad86bfSDavid S. Miller 		if (err)
1328b4ad86bfSDavid S. Miller 			return err;
13291da177e4SLinus Torvalds 
13305424f32eSThomas Graf 		copy_templates(pol, utmpl, nr);
13311da177e4SLinus Torvalds 	}
13321da177e4SLinus Torvalds 	return 0;
13331da177e4SLinus Torvalds }
13341da177e4SLinus Torvalds 
13355424f32eSThomas Graf static int copy_from_user_policy_type(u8 *tp, struct nlattr **attrs)
1336f7b6983fSMasahide NAKAMURA {
13375424f32eSThomas Graf 	struct nlattr *rt = attrs[XFRMA_POLICY_TYPE];
1338f7b6983fSMasahide NAKAMURA 	struct xfrm_userpolicy_type *upt;
1339b798a9edSJamal Hadi Salim 	u8 type = XFRM_POLICY_TYPE_MAIN;
1340f7b6983fSMasahide NAKAMURA 	int err;
1341f7b6983fSMasahide NAKAMURA 
1342f7b6983fSMasahide NAKAMURA 	if (rt) {
13435424f32eSThomas Graf 		upt = nla_data(rt);
1344f7b6983fSMasahide NAKAMURA 		type = upt->type;
1345f7b6983fSMasahide NAKAMURA 	}
1346f7b6983fSMasahide NAKAMURA 
1347f7b6983fSMasahide NAKAMURA 	err = verify_policy_type(type);
1348f7b6983fSMasahide NAKAMURA 	if (err)
1349f7b6983fSMasahide NAKAMURA 		return err;
1350f7b6983fSMasahide NAKAMURA 
1351f7b6983fSMasahide NAKAMURA 	*tp = type;
1352f7b6983fSMasahide NAKAMURA 	return 0;
1353f7b6983fSMasahide NAKAMURA }
1354f7b6983fSMasahide NAKAMURA 
13551da177e4SLinus Torvalds static void copy_from_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p)
13561da177e4SLinus Torvalds {
13571da177e4SLinus Torvalds 	xp->priority = p->priority;
13581da177e4SLinus Torvalds 	xp->index = p->index;
13591da177e4SLinus Torvalds 	memcpy(&xp->selector, &p->sel, sizeof(xp->selector));
13601da177e4SLinus Torvalds 	memcpy(&xp->lft, &p->lft, sizeof(xp->lft));
13611da177e4SLinus Torvalds 	xp->action = p->action;
13621da177e4SLinus Torvalds 	xp->flags = p->flags;
13631da177e4SLinus Torvalds 	xp->family = p->sel.family;
13641da177e4SLinus Torvalds 	/* XXX xp->share = p->share; */
13651da177e4SLinus Torvalds }
13661da177e4SLinus Torvalds 
13671da177e4SLinus Torvalds static void copy_to_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p, int dir)
13681da177e4SLinus Torvalds {
13697b789836SMathias Krause 	memset(p, 0, sizeof(*p));
13701da177e4SLinus Torvalds 	memcpy(&p->sel, &xp->selector, sizeof(p->sel));
13711da177e4SLinus Torvalds 	memcpy(&p->lft, &xp->lft, sizeof(p->lft));
13721da177e4SLinus Torvalds 	memcpy(&p->curlft, &xp->curlft, sizeof(p->curlft));
13731da177e4SLinus Torvalds 	p->priority = xp->priority;
13741da177e4SLinus Torvalds 	p->index = xp->index;
13751da177e4SLinus Torvalds 	p->sel.family = xp->family;
13761da177e4SLinus Torvalds 	p->dir = dir;
13771da177e4SLinus Torvalds 	p->action = xp->action;
13781da177e4SLinus Torvalds 	p->flags = xp->flags;
13791da177e4SLinus Torvalds 	p->share = XFRM_SHARE_ANY; /* XXX xp->share */
13801da177e4SLinus Torvalds }
13811da177e4SLinus Torvalds 
1382fc34acd3SAlexey Dobriyan static struct xfrm_policy *xfrm_policy_construct(struct net *net, struct xfrm_userpolicy_info *p, struct nlattr **attrs, int *errp)
13831da177e4SLinus Torvalds {
1384fc34acd3SAlexey Dobriyan 	struct xfrm_policy *xp = xfrm_policy_alloc(net, GFP_KERNEL);
13851da177e4SLinus Torvalds 	int err;
13861da177e4SLinus Torvalds 
13871da177e4SLinus Torvalds 	if (!xp) {
13881da177e4SLinus Torvalds 		*errp = -ENOMEM;
13891da177e4SLinus Torvalds 		return NULL;
13901da177e4SLinus Torvalds 	}
13911da177e4SLinus Torvalds 
13921da177e4SLinus Torvalds 	copy_from_user_policy(xp, p);
1393df71837dSTrent Jaeger 
139435a7aa08SThomas Graf 	err = copy_from_user_policy_type(&xp->type, attrs);
1395f7b6983fSMasahide NAKAMURA 	if (err)
1396f7b6983fSMasahide NAKAMURA 		goto error;
1397f7b6983fSMasahide NAKAMURA 
139835a7aa08SThomas Graf 	if (!(err = copy_from_user_tmpl(xp, attrs)))
139935a7aa08SThomas Graf 		err = copy_from_user_sec_ctx(xp, attrs);
1400f7b6983fSMasahide NAKAMURA 	if (err)
1401f7b6983fSMasahide NAKAMURA 		goto error;
14021da177e4SLinus Torvalds 
1403295fae56SJamal Hadi Salim 	xfrm_mark_get(attrs, &xp->mark);
1404295fae56SJamal Hadi Salim 
14051da177e4SLinus Torvalds 	return xp;
1406f7b6983fSMasahide NAKAMURA  error:
1407f7b6983fSMasahide NAKAMURA 	*errp = err;
140812a169e7SHerbert Xu 	xp->walk.dead = 1;
140964c31b3fSWANG Cong 	xfrm_policy_destroy(xp);
1410f7b6983fSMasahide NAKAMURA 	return NULL;
14111da177e4SLinus Torvalds }
14121da177e4SLinus Torvalds 
141322e70050SChristoph Hellwig static int xfrm_add_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
14145424f32eSThomas Graf 		struct nlattr **attrs)
14151da177e4SLinus Torvalds {
1416fc34acd3SAlexey Dobriyan 	struct net *net = sock_net(skb->sk);
14177b67c857SThomas Graf 	struct xfrm_userpolicy_info *p = nlmsg_data(nlh);
14181da177e4SLinus Torvalds 	struct xfrm_policy *xp;
141926b15dadSJamal Hadi Salim 	struct km_event c;
14201da177e4SLinus Torvalds 	int err;
14211da177e4SLinus Torvalds 	int excl;
1422e1760bd5SEric W. Biederman 	kuid_t loginuid = audit_get_loginuid(current);
14234440e854SEric Paris 	unsigned int sessionid = audit_get_sessionid(current);
1424c53fa1edSPatrick McHardy 	u32 sid;
14251da177e4SLinus Torvalds 
14261da177e4SLinus Torvalds 	err = verify_newpolicy_info(p);
14271da177e4SLinus Torvalds 	if (err)
14281da177e4SLinus Torvalds 		return err;
142935a7aa08SThomas Graf 	err = verify_sec_ctx_len(attrs);
1430df71837dSTrent Jaeger 	if (err)
1431df71837dSTrent Jaeger 		return err;
14321da177e4SLinus Torvalds 
1433fc34acd3SAlexey Dobriyan 	xp = xfrm_policy_construct(net, p, attrs, &err);
14341da177e4SLinus Torvalds 	if (!xp)
14351da177e4SLinus Torvalds 		return err;
14361da177e4SLinus Torvalds 
143725985edcSLucas De Marchi 	/* shouldn't excl be based on nlh flags??
143826b15dadSJamal Hadi Salim 	 * Aha! this is anti-netlink really i.e  more pfkey derived
143926b15dadSJamal Hadi Salim 	 * in netlink excl is a flag and you wouldnt need
144026b15dadSJamal Hadi Salim 	 * a type XFRM_MSG_UPDPOLICY - JHS */
14411da177e4SLinus Torvalds 	excl = nlh->nlmsg_type == XFRM_MSG_NEWPOLICY;
14421da177e4SLinus Torvalds 	err = xfrm_policy_insert(p->dir, xp, excl);
1443c53fa1edSPatrick McHardy 	security_task_getsecid(current, &sid);
14442532386fSEric Paris 	xfrm_audit_policy_add(xp, err ? 0 : 1, loginuid, sessionid, sid);
1445161a09e7SJoy Latten 
14461da177e4SLinus Torvalds 	if (err) {
144703e1ad7bSPaul Moore 		security_xfrm_policy_free(xp->security);
14481da177e4SLinus Torvalds 		kfree(xp);
14491da177e4SLinus Torvalds 		return err;
14501da177e4SLinus Torvalds 	}
14511da177e4SLinus Torvalds 
1452f60f6b8fSHerbert Xu 	c.event = nlh->nlmsg_type;
145326b15dadSJamal Hadi Salim 	c.seq = nlh->nlmsg_seq;
145415e47304SEric W. Biederman 	c.portid = nlh->nlmsg_pid;
145526b15dadSJamal Hadi Salim 	km_policy_notify(xp, p->dir, &c);
145626b15dadSJamal Hadi Salim 
14571da177e4SLinus Torvalds 	xfrm_pol_put(xp);
14581da177e4SLinus Torvalds 
14591da177e4SLinus Torvalds 	return 0;
14601da177e4SLinus Torvalds }
14611da177e4SLinus Torvalds 
14621da177e4SLinus Torvalds static int copy_to_user_tmpl(struct xfrm_policy *xp, struct sk_buff *skb)
14631da177e4SLinus Torvalds {
14641da177e4SLinus Torvalds 	struct xfrm_user_tmpl vec[XFRM_MAX_DEPTH];
14651da177e4SLinus Torvalds 	int i;
14661da177e4SLinus Torvalds 
14671da177e4SLinus Torvalds 	if (xp->xfrm_nr == 0)
14681da177e4SLinus Torvalds 		return 0;
14691da177e4SLinus Torvalds 
14701da177e4SLinus Torvalds 	for (i = 0; i < xp->xfrm_nr; i++) {
14711da177e4SLinus Torvalds 		struct xfrm_user_tmpl *up = &vec[i];
14721da177e4SLinus Torvalds 		struct xfrm_tmpl *kp = &xp->xfrm_vec[i];
14731da177e4SLinus Torvalds 
14741f86840fSMathias Krause 		memset(up, 0, sizeof(*up));
14751da177e4SLinus Torvalds 		memcpy(&up->id, &kp->id, sizeof(up->id));
14768511d01dSMiika Komu 		up->family = kp->encap_family;
14771da177e4SLinus Torvalds 		memcpy(&up->saddr, &kp->saddr, sizeof(up->saddr));
14781da177e4SLinus Torvalds 		up->reqid = kp->reqid;
14791da177e4SLinus Torvalds 		up->mode = kp->mode;
14801da177e4SLinus Torvalds 		up->share = kp->share;
14811da177e4SLinus Torvalds 		up->optional = kp->optional;
14821da177e4SLinus Torvalds 		up->aalgos = kp->aalgos;
14831da177e4SLinus Torvalds 		up->ealgos = kp->ealgos;
14841da177e4SLinus Torvalds 		up->calgos = kp->calgos;
14851da177e4SLinus Torvalds 	}
14861da177e4SLinus Torvalds 
1487c0144beaSThomas Graf 	return nla_put(skb, XFRMA_TMPL,
1488c0144beaSThomas Graf 		       sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr, vec);
1489df71837dSTrent Jaeger }
1490df71837dSTrent Jaeger 
14910d681623SSerge Hallyn static inline int copy_to_user_state_sec_ctx(struct xfrm_state *x, struct sk_buff *skb)
14920d681623SSerge Hallyn {
14930d681623SSerge Hallyn 	if (x->security) {
14940d681623SSerge Hallyn 		return copy_sec_ctx(x->security, skb);
14950d681623SSerge Hallyn 	}
14960d681623SSerge Hallyn 	return 0;
14970d681623SSerge Hallyn }
14980d681623SSerge Hallyn 
14990d681623SSerge Hallyn static inline int copy_to_user_sec_ctx(struct xfrm_policy *xp, struct sk_buff *skb)
15000d681623SSerge Hallyn {
15011d1e34ddSDavid S. Miller 	if (xp->security)
15020d681623SSerge Hallyn 		return copy_sec_ctx(xp->security, skb);
15030d681623SSerge Hallyn 	return 0;
15040d681623SSerge Hallyn }
1505cfbfd45aSThomas Graf static inline size_t userpolicy_type_attrsize(void)
1506cfbfd45aSThomas Graf {
1507cfbfd45aSThomas Graf #ifdef CONFIG_XFRM_SUB_POLICY
1508cfbfd45aSThomas Graf 	return nla_total_size(sizeof(struct xfrm_userpolicy_type));
1509cfbfd45aSThomas Graf #else
1510cfbfd45aSThomas Graf 	return 0;
1511cfbfd45aSThomas Graf #endif
1512cfbfd45aSThomas Graf }
15130d681623SSerge Hallyn 
1514f7b6983fSMasahide NAKAMURA #ifdef CONFIG_XFRM_SUB_POLICY
1515b798a9edSJamal Hadi Salim static int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
1516f7b6983fSMasahide NAKAMURA {
1517c0144beaSThomas Graf 	struct xfrm_userpolicy_type upt = {
1518c0144beaSThomas Graf 		.type = type,
1519c0144beaSThomas Graf 	};
1520f7b6983fSMasahide NAKAMURA 
1521c0144beaSThomas Graf 	return nla_put(skb, XFRMA_POLICY_TYPE, sizeof(upt), &upt);
1522f7b6983fSMasahide NAKAMURA }
1523f7b6983fSMasahide NAKAMURA 
1524f7b6983fSMasahide NAKAMURA #else
1525b798a9edSJamal Hadi Salim static inline int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
1526f7b6983fSMasahide NAKAMURA {
1527f7b6983fSMasahide NAKAMURA 	return 0;
1528f7b6983fSMasahide NAKAMURA }
1529f7b6983fSMasahide NAKAMURA #endif
1530f7b6983fSMasahide NAKAMURA 
15311da177e4SLinus Torvalds static int dump_one_policy(struct xfrm_policy *xp, int dir, int count, void *ptr)
15321da177e4SLinus Torvalds {
15331da177e4SLinus Torvalds 	struct xfrm_dump_info *sp = ptr;
15341da177e4SLinus Torvalds 	struct xfrm_userpolicy_info *p;
15351da177e4SLinus Torvalds 	struct sk_buff *in_skb = sp->in_skb;
15361da177e4SLinus Torvalds 	struct sk_buff *skb = sp->out_skb;
15371da177e4SLinus Torvalds 	struct nlmsghdr *nlh;
15381d1e34ddSDavid S. Miller 	int err;
15391da177e4SLinus Torvalds 
154015e47304SEric W. Biederman 	nlh = nlmsg_put(skb, NETLINK_CB(in_skb).portid, sp->nlmsg_seq,
154179b8b7f4SThomas Graf 			XFRM_MSG_NEWPOLICY, sizeof(*p), sp->nlmsg_flags);
154279b8b7f4SThomas Graf 	if (nlh == NULL)
154379b8b7f4SThomas Graf 		return -EMSGSIZE;
15441da177e4SLinus Torvalds 
15457b67c857SThomas Graf 	p = nlmsg_data(nlh);
15461da177e4SLinus Torvalds 	copy_to_user_policy(xp, p, dir);
15471d1e34ddSDavid S. Miller 	err = copy_to_user_tmpl(xp, skb);
15481d1e34ddSDavid S. Miller 	if (!err)
15491d1e34ddSDavid S. Miller 		err = copy_to_user_sec_ctx(xp, skb);
15501d1e34ddSDavid S. Miller 	if (!err)
15511d1e34ddSDavid S. Miller 		err = copy_to_user_policy_type(xp->type, skb);
15521d1e34ddSDavid S. Miller 	if (!err)
15531d1e34ddSDavid S. Miller 		err = xfrm_mark_put(skb, &xp->mark);
15541d1e34ddSDavid S. Miller 	if (err) {
15551d1e34ddSDavid S. Miller 		nlmsg_cancel(skb, nlh);
15561d1e34ddSDavid S. Miller 		return err;
15571d1e34ddSDavid S. Miller 	}
15589825069dSThomas Graf 	nlmsg_end(skb, nlh);
15591da177e4SLinus Torvalds 	return 0;
15601da177e4SLinus Torvalds }
15611da177e4SLinus Torvalds 
15624c563f76STimo Teras static int xfrm_dump_policy_done(struct netlink_callback *cb)
15634c563f76STimo Teras {
15644c563f76STimo Teras 	struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *) &cb->args[1];
1565283bc9f3SFan Du 	struct net *net = sock_net(cb->skb->sk);
15664c563f76STimo Teras 
1567283bc9f3SFan Du 	xfrm_policy_walk_done(walk, net);
15684c563f76STimo Teras 	return 0;
15694c563f76STimo Teras }
15704c563f76STimo Teras 
15711da177e4SLinus Torvalds static int xfrm_dump_policy(struct sk_buff *skb, struct netlink_callback *cb)
15721da177e4SLinus Torvalds {
1573fc34acd3SAlexey Dobriyan 	struct net *net = sock_net(skb->sk);
15744c563f76STimo Teras 	struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *) &cb->args[1];
15751da177e4SLinus Torvalds 	struct xfrm_dump_info info;
15761da177e4SLinus Torvalds 
15774c563f76STimo Teras 	BUILD_BUG_ON(sizeof(struct xfrm_policy_walk) >
15784c563f76STimo Teras 		     sizeof(cb->args) - sizeof(cb->args[0]));
15794c563f76STimo Teras 
15801da177e4SLinus Torvalds 	info.in_skb = cb->skb;
15811da177e4SLinus Torvalds 	info.out_skb = skb;
15821da177e4SLinus Torvalds 	info.nlmsg_seq = cb->nlh->nlmsg_seq;
15831da177e4SLinus Torvalds 	info.nlmsg_flags = NLM_F_MULTI;
15844c563f76STimo Teras 
15854c563f76STimo Teras 	if (!cb->args[0]) {
15864c563f76STimo Teras 		cb->args[0] = 1;
15874c563f76STimo Teras 		xfrm_policy_walk_init(walk, XFRM_POLICY_TYPE_ANY);
15884c563f76STimo Teras 	}
15894c563f76STimo Teras 
1590fc34acd3SAlexey Dobriyan 	(void) xfrm_policy_walk(net, walk, dump_one_policy, &info);
15911da177e4SLinus Torvalds 
15921da177e4SLinus Torvalds 	return skb->len;
15931da177e4SLinus Torvalds }
15941da177e4SLinus Torvalds 
15951da177e4SLinus Torvalds static struct sk_buff *xfrm_policy_netlink(struct sk_buff *in_skb,
15961da177e4SLinus Torvalds 					  struct xfrm_policy *xp,
15971da177e4SLinus Torvalds 					  int dir, u32 seq)
15981da177e4SLinus Torvalds {
15991da177e4SLinus Torvalds 	struct xfrm_dump_info info;
16001da177e4SLinus Torvalds 	struct sk_buff *skb;
1601c2546372SMathias Krause 	int err;
16021da177e4SLinus Torvalds 
16037deb2264SThomas Graf 	skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
16041da177e4SLinus Torvalds 	if (!skb)
16051da177e4SLinus Torvalds 		return ERR_PTR(-ENOMEM);
16061da177e4SLinus Torvalds 
16071da177e4SLinus Torvalds 	info.in_skb = in_skb;
16081da177e4SLinus Torvalds 	info.out_skb = skb;
16091da177e4SLinus Torvalds 	info.nlmsg_seq = seq;
16101da177e4SLinus Torvalds 	info.nlmsg_flags = 0;
16111da177e4SLinus Torvalds 
1612c2546372SMathias Krause 	err = dump_one_policy(xp, dir, 0, &info);
1613c2546372SMathias Krause 	if (err) {
16141da177e4SLinus Torvalds 		kfree_skb(skb);
1615c2546372SMathias Krause 		return ERR_PTR(err);
16161da177e4SLinus Torvalds 	}
16171da177e4SLinus Torvalds 
16181da177e4SLinus Torvalds 	return skb;
16191da177e4SLinus Torvalds }
16201da177e4SLinus Torvalds 
162122e70050SChristoph Hellwig static int xfrm_get_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
16225424f32eSThomas Graf 		struct nlattr **attrs)
16231da177e4SLinus Torvalds {
1624fc34acd3SAlexey Dobriyan 	struct net *net = sock_net(skb->sk);
16251da177e4SLinus Torvalds 	struct xfrm_policy *xp;
16261da177e4SLinus Torvalds 	struct xfrm_userpolicy_id *p;
1627b798a9edSJamal Hadi Salim 	u8 type = XFRM_POLICY_TYPE_MAIN;
16281da177e4SLinus Torvalds 	int err;
162926b15dadSJamal Hadi Salim 	struct km_event c;
16301da177e4SLinus Torvalds 	int delete;
1631295fae56SJamal Hadi Salim 	struct xfrm_mark m;
1632295fae56SJamal Hadi Salim 	u32 mark = xfrm_mark_get(attrs, &m);
16331da177e4SLinus Torvalds 
16347b67c857SThomas Graf 	p = nlmsg_data(nlh);
16351da177e4SLinus Torvalds 	delete = nlh->nlmsg_type == XFRM_MSG_DELPOLICY;
16361da177e4SLinus Torvalds 
163735a7aa08SThomas Graf 	err = copy_from_user_policy_type(&type, attrs);
1638f7b6983fSMasahide NAKAMURA 	if (err)
1639f7b6983fSMasahide NAKAMURA 		return err;
1640f7b6983fSMasahide NAKAMURA 
16411da177e4SLinus Torvalds 	err = verify_policy_dir(p->dir);
16421da177e4SLinus Torvalds 	if (err)
16431da177e4SLinus Torvalds 		return err;
16441da177e4SLinus Torvalds 
16451da177e4SLinus Torvalds 	if (p->index)
1646295fae56SJamal Hadi Salim 		xp = xfrm_policy_byid(net, mark, type, p->dir, p->index, delete, &err);
1647df71837dSTrent Jaeger 	else {
16485424f32eSThomas Graf 		struct nlattr *rt = attrs[XFRMA_SEC_CTX];
164903e1ad7bSPaul Moore 		struct xfrm_sec_ctx *ctx;
1650df71837dSTrent Jaeger 
165135a7aa08SThomas Graf 		err = verify_sec_ctx_len(attrs);
1652df71837dSTrent Jaeger 		if (err)
1653df71837dSTrent Jaeger 			return err;
1654df71837dSTrent Jaeger 
16552c8dd116SDenis V. Lunev 		ctx = NULL;
1656df71837dSTrent Jaeger 		if (rt) {
16575424f32eSThomas Graf 			struct xfrm_user_sec_ctx *uctx = nla_data(rt);
1658df71837dSTrent Jaeger 
165903e1ad7bSPaul Moore 			err = security_xfrm_policy_alloc(&ctx, uctx);
166003e1ad7bSPaul Moore 			if (err)
1661df71837dSTrent Jaeger 				return err;
16622c8dd116SDenis V. Lunev 		}
1663295fae56SJamal Hadi Salim 		xp = xfrm_policy_bysel_ctx(net, mark, type, p->dir, &p->sel,
16646f26b61eSJamal Hadi Salim 					   ctx, delete, &err);
166503e1ad7bSPaul Moore 		security_xfrm_policy_free(ctx);
1666df71837dSTrent Jaeger 	}
16671da177e4SLinus Torvalds 	if (xp == NULL)
16681da177e4SLinus Torvalds 		return -ENOENT;
16691da177e4SLinus Torvalds 
16701da177e4SLinus Torvalds 	if (!delete) {
16711da177e4SLinus Torvalds 		struct sk_buff *resp_skb;
16721da177e4SLinus Torvalds 
16731da177e4SLinus Torvalds 		resp_skb = xfrm_policy_netlink(skb, xp, p->dir, nlh->nlmsg_seq);
16741da177e4SLinus Torvalds 		if (IS_ERR(resp_skb)) {
16751da177e4SLinus Torvalds 			err = PTR_ERR(resp_skb);
16761da177e4SLinus Torvalds 		} else {
1677a6483b79SAlexey Dobriyan 			err = nlmsg_unicast(net->xfrm.nlsk, resp_skb,
167815e47304SEric W. Biederman 					    NETLINK_CB(skb).portid);
16791da177e4SLinus Torvalds 		}
168026b15dadSJamal Hadi Salim 	} else {
1681e1760bd5SEric W. Biederman 		kuid_t loginuid = audit_get_loginuid(current);
16824440e854SEric Paris 		unsigned int sessionid = audit_get_sessionid(current);
1683c53fa1edSPatrick McHardy 		u32 sid;
16842532386fSEric Paris 
1685c53fa1edSPatrick McHardy 		security_task_getsecid(current, &sid);
16862532386fSEric Paris 		xfrm_audit_policy_delete(xp, err ? 0 : 1, loginuid, sessionid,
16872532386fSEric Paris 					 sid);
168813fcfbb0SDavid S. Miller 
168913fcfbb0SDavid S. Miller 		if (err != 0)
1690c8c05a8eSCatherine Zhang 			goto out;
169113fcfbb0SDavid S. Miller 
1692e7443892SHerbert Xu 		c.data.byid = p->index;
1693f60f6b8fSHerbert Xu 		c.event = nlh->nlmsg_type;
169426b15dadSJamal Hadi Salim 		c.seq = nlh->nlmsg_seq;
169515e47304SEric W. Biederman 		c.portid = nlh->nlmsg_pid;
169626b15dadSJamal Hadi Salim 		km_policy_notify(xp, p->dir, &c);
16971da177e4SLinus Torvalds 	}
16981da177e4SLinus Torvalds 
1699c8c05a8eSCatherine Zhang out:
1700ef41aaa0SEric Paris 	xfrm_pol_put(xp);
1701e4c17216SPaul Moore 	if (delete && err == 0)
1702e4c17216SPaul Moore 		xfrm_garbage_collect(net);
17031da177e4SLinus Torvalds 	return err;
17041da177e4SLinus Torvalds }
17051da177e4SLinus Torvalds 
170622e70050SChristoph Hellwig static int xfrm_flush_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
17075424f32eSThomas Graf 		struct nlattr **attrs)
17081da177e4SLinus Torvalds {
1709fc34acd3SAlexey Dobriyan 	struct net *net = sock_net(skb->sk);
171026b15dadSJamal Hadi Salim 	struct km_event c;
17117b67c857SThomas Graf 	struct xfrm_usersa_flush *p = nlmsg_data(nlh);
1712161a09e7SJoy Latten 	struct xfrm_audit audit_info;
17134aa2e62cSJoy Latten 	int err;
17141da177e4SLinus Torvalds 
1715c53fa1edSPatrick McHardy 	audit_info.loginuid = audit_get_loginuid(current);
1716c53fa1edSPatrick McHardy 	audit_info.sessionid = audit_get_sessionid(current);
1717c53fa1edSPatrick McHardy 	security_task_getsecid(current, &audit_info.secid);
1718fc34acd3SAlexey Dobriyan 	err = xfrm_state_flush(net, p->proto, &audit_info);
17199e64cc95SJamal Hadi Salim 	if (err) {
17209e64cc95SJamal Hadi Salim 		if (err == -ESRCH) /* empty table */
17219e64cc95SJamal Hadi Salim 			return 0;
1722069c474eSDavid S. Miller 		return err;
17239e64cc95SJamal Hadi Salim 	}
1724bf08867fSHerbert Xu 	c.data.proto = p->proto;
1725f60f6b8fSHerbert Xu 	c.event = nlh->nlmsg_type;
172626b15dadSJamal Hadi Salim 	c.seq = nlh->nlmsg_seq;
172715e47304SEric W. Biederman 	c.portid = nlh->nlmsg_pid;
17287067802eSAlexey Dobriyan 	c.net = net;
172926b15dadSJamal Hadi Salim 	km_state_notify(NULL, &c);
173026b15dadSJamal Hadi Salim 
17311da177e4SLinus Torvalds 	return 0;
17321da177e4SLinus Torvalds }
17331da177e4SLinus Torvalds 
1734d8647b79SSteffen Klassert static inline size_t xfrm_aevent_msgsize(struct xfrm_state *x)
17357deb2264SThomas Graf {
1736d8647b79SSteffen Klassert 	size_t replay_size = x->replay_esn ?
1737d8647b79SSteffen Klassert 			      xfrm_replay_state_esn_len(x->replay_esn) :
1738d8647b79SSteffen Klassert 			      sizeof(struct xfrm_replay_state);
1739d8647b79SSteffen Klassert 
17407deb2264SThomas Graf 	return NLMSG_ALIGN(sizeof(struct xfrm_aevent_id))
1741d8647b79SSteffen Klassert 	       + nla_total_size(replay_size)
17427deb2264SThomas Graf 	       + nla_total_size(sizeof(struct xfrm_lifetime_cur))
17436f26b61eSJamal Hadi Salim 	       + nla_total_size(sizeof(struct xfrm_mark))
17447deb2264SThomas Graf 	       + nla_total_size(4) /* XFRM_AE_RTHR */
17457deb2264SThomas Graf 	       + nla_total_size(4); /* XFRM_AE_ETHR */
17467deb2264SThomas Graf }
1747d51d081dSJamal Hadi Salim 
1748214e005bSDavid S. Miller static int build_aevent(struct sk_buff *skb, struct xfrm_state *x, const struct km_event *c)
1749d51d081dSJamal Hadi Salim {
1750d51d081dSJamal Hadi Salim 	struct xfrm_aevent_id *id;
1751d51d081dSJamal Hadi Salim 	struct nlmsghdr *nlh;
17521d1e34ddSDavid S. Miller 	int err;
1753d51d081dSJamal Hadi Salim 
175415e47304SEric W. Biederman 	nlh = nlmsg_put(skb, c->portid, c->seq, XFRM_MSG_NEWAE, sizeof(*id), 0);
175579b8b7f4SThomas Graf 	if (nlh == NULL)
175679b8b7f4SThomas Graf 		return -EMSGSIZE;
1757d51d081dSJamal Hadi Salim 
17587b67c857SThomas Graf 	id = nlmsg_data(nlh);
17592b5f6dccSJamal Hadi Salim 	memcpy(&id->sa_id.daddr, &x->id.daddr, sizeof(x->id.daddr));
1760d51d081dSJamal Hadi Salim 	id->sa_id.spi = x->id.spi;
1761d51d081dSJamal Hadi Salim 	id->sa_id.family = x->props.family;
1762d51d081dSJamal Hadi Salim 	id->sa_id.proto = x->id.proto;
17632b5f6dccSJamal Hadi Salim 	memcpy(&id->saddr, &x->props.saddr, sizeof(x->props.saddr));
17642b5f6dccSJamal Hadi Salim 	id->reqid = x->props.reqid;
1765d51d081dSJamal Hadi Salim 	id->flags = c->data.aevent;
1766d51d081dSJamal Hadi Salim 
1767d0fde795SDavid S. Miller 	if (x->replay_esn) {
17681d1e34ddSDavid S. Miller 		err = nla_put(skb, XFRMA_REPLAY_ESN_VAL,
1769d8647b79SSteffen Klassert 			      xfrm_replay_state_esn_len(x->replay_esn),
17701d1e34ddSDavid S. Miller 			      x->replay_esn);
1771d0fde795SDavid S. Miller 	} else {
17721d1e34ddSDavid S. Miller 		err = nla_put(skb, XFRMA_REPLAY_VAL, sizeof(x->replay),
17731d1e34ddSDavid S. Miller 			      &x->replay);
1774d0fde795SDavid S. Miller 	}
17751d1e34ddSDavid S. Miller 	if (err)
17761d1e34ddSDavid S. Miller 		goto out_cancel;
17771d1e34ddSDavid S. Miller 	err = nla_put(skb, XFRMA_LTIME_VAL, sizeof(x->curlft), &x->curlft);
17781d1e34ddSDavid S. Miller 	if (err)
17791d1e34ddSDavid S. Miller 		goto out_cancel;
1780d8647b79SSteffen Klassert 
17811d1e34ddSDavid S. Miller 	if (id->flags & XFRM_AE_RTHR) {
17821d1e34ddSDavid S. Miller 		err = nla_put_u32(skb, XFRMA_REPLAY_THRESH, x->replay_maxdiff);
17831d1e34ddSDavid S. Miller 		if (err)
17841d1e34ddSDavid S. Miller 			goto out_cancel;
17851d1e34ddSDavid S. Miller 	}
17861d1e34ddSDavid S. Miller 	if (id->flags & XFRM_AE_ETHR) {
17871d1e34ddSDavid S. Miller 		err = nla_put_u32(skb, XFRMA_ETIMER_THRESH,
17881d1e34ddSDavid S. Miller 				  x->replay_maxage * 10 / HZ);
17891d1e34ddSDavid S. Miller 		if (err)
17901d1e34ddSDavid S. Miller 			goto out_cancel;
17911d1e34ddSDavid S. Miller 	}
17921d1e34ddSDavid S. Miller 	err = xfrm_mark_put(skb, &x->mark);
17931d1e34ddSDavid S. Miller 	if (err)
17941d1e34ddSDavid S. Miller 		goto out_cancel;
17956f26b61eSJamal Hadi Salim 
17969825069dSThomas Graf 	return nlmsg_end(skb, nlh);
1797d51d081dSJamal Hadi Salim 
17981d1e34ddSDavid S. Miller out_cancel:
17999825069dSThomas Graf 	nlmsg_cancel(skb, nlh);
18001d1e34ddSDavid S. Miller 	return err;
1801d51d081dSJamal Hadi Salim }
1802d51d081dSJamal Hadi Salim 
180322e70050SChristoph Hellwig static int xfrm_get_ae(struct sk_buff *skb, struct nlmsghdr *nlh,
18045424f32eSThomas Graf 		struct nlattr **attrs)
1805d51d081dSJamal Hadi Salim {
1806fc34acd3SAlexey Dobriyan 	struct net *net = sock_net(skb->sk);
1807d51d081dSJamal Hadi Salim 	struct xfrm_state *x;
1808d51d081dSJamal Hadi Salim 	struct sk_buff *r_skb;
1809d51d081dSJamal Hadi Salim 	int err;
1810d51d081dSJamal Hadi Salim 	struct km_event c;
18116f26b61eSJamal Hadi Salim 	u32 mark;
18126f26b61eSJamal Hadi Salim 	struct xfrm_mark m;
18137b67c857SThomas Graf 	struct xfrm_aevent_id *p = nlmsg_data(nlh);
1814d51d081dSJamal Hadi Salim 	struct xfrm_usersa_id *id = &p->sa_id;
1815d51d081dSJamal Hadi Salim 
18166f26b61eSJamal Hadi Salim 	mark = xfrm_mark_get(attrs, &m);
18176f26b61eSJamal Hadi Salim 
18186f26b61eSJamal Hadi Salim 	x = xfrm_state_lookup(net, mark, &id->daddr, id->spi, id->proto, id->family);
1819d8647b79SSteffen Klassert 	if (x == NULL)
1820d51d081dSJamal Hadi Salim 		return -ESRCH;
1821d8647b79SSteffen Klassert 
1822d8647b79SSteffen Klassert 	r_skb = nlmsg_new(xfrm_aevent_msgsize(x), GFP_ATOMIC);
1823d8647b79SSteffen Klassert 	if (r_skb == NULL) {
1824d8647b79SSteffen Klassert 		xfrm_state_put(x);
1825d8647b79SSteffen Klassert 		return -ENOMEM;
1826d51d081dSJamal Hadi Salim 	}
1827d51d081dSJamal Hadi Salim 
1828d51d081dSJamal Hadi Salim 	/*
1829d51d081dSJamal Hadi Salim 	 * XXX: is this lock really needed - none of the other
1830d51d081dSJamal Hadi Salim 	 * gets lock (the concern is things getting updated
1831d51d081dSJamal Hadi Salim 	 * while we are still reading) - jhs
1832d51d081dSJamal Hadi Salim 	*/
1833d51d081dSJamal Hadi Salim 	spin_lock_bh(&x->lock);
1834d51d081dSJamal Hadi Salim 	c.data.aevent = p->flags;
1835d51d081dSJamal Hadi Salim 	c.seq = nlh->nlmsg_seq;
183615e47304SEric W. Biederman 	c.portid = nlh->nlmsg_pid;
1837d51d081dSJamal Hadi Salim 
1838d51d081dSJamal Hadi Salim 	if (build_aevent(r_skb, x, &c) < 0)
1839d51d081dSJamal Hadi Salim 		BUG();
184015e47304SEric W. Biederman 	err = nlmsg_unicast(net->xfrm.nlsk, r_skb, NETLINK_CB(skb).portid);
1841d51d081dSJamal Hadi Salim 	spin_unlock_bh(&x->lock);
1842d51d081dSJamal Hadi Salim 	xfrm_state_put(x);
1843d51d081dSJamal Hadi Salim 	return err;
1844d51d081dSJamal Hadi Salim }
1845d51d081dSJamal Hadi Salim 
184622e70050SChristoph Hellwig static int xfrm_new_ae(struct sk_buff *skb, struct nlmsghdr *nlh,
18475424f32eSThomas Graf 		struct nlattr **attrs)
1848d51d081dSJamal Hadi Salim {
1849fc34acd3SAlexey Dobriyan 	struct net *net = sock_net(skb->sk);
1850d51d081dSJamal Hadi Salim 	struct xfrm_state *x;
1851d51d081dSJamal Hadi Salim 	struct km_event c;
1852d51d081dSJamal Hadi Salim 	int err = -EINVAL;
18536f26b61eSJamal Hadi Salim 	u32 mark = 0;
18546f26b61eSJamal Hadi Salim 	struct xfrm_mark m;
18557b67c857SThomas Graf 	struct xfrm_aevent_id *p = nlmsg_data(nlh);
18565424f32eSThomas Graf 	struct nlattr *rp = attrs[XFRMA_REPLAY_VAL];
1857d8647b79SSteffen Klassert 	struct nlattr *re = attrs[XFRMA_REPLAY_ESN_VAL];
18585424f32eSThomas Graf 	struct nlattr *lt = attrs[XFRMA_LTIME_VAL];
1859d51d081dSJamal Hadi Salim 
1860d8647b79SSteffen Klassert 	if (!lt && !rp && !re)
1861d51d081dSJamal Hadi Salim 		return err;
1862d51d081dSJamal Hadi Salim 
1863d51d081dSJamal Hadi Salim 	/* pedantic mode - thou shalt sayeth replaceth */
1864d51d081dSJamal Hadi Salim 	if (!(nlh->nlmsg_flags&NLM_F_REPLACE))
1865d51d081dSJamal Hadi Salim 		return err;
1866d51d081dSJamal Hadi Salim 
18676f26b61eSJamal Hadi Salim 	mark = xfrm_mark_get(attrs, &m);
18686f26b61eSJamal Hadi Salim 
18696f26b61eSJamal Hadi Salim 	x = xfrm_state_lookup(net, mark, &p->sa_id.daddr, p->sa_id.spi, p->sa_id.proto, p->sa_id.family);
1870d51d081dSJamal Hadi Salim 	if (x == NULL)
1871d51d081dSJamal Hadi Salim 		return -ESRCH;
1872d51d081dSJamal Hadi Salim 
1873d51d081dSJamal Hadi Salim 	if (x->km.state != XFRM_STATE_VALID)
1874d51d081dSJamal Hadi Salim 		goto out;
1875d51d081dSJamal Hadi Salim 
18764479ff76SSteffen Klassert 	err = xfrm_replay_verify_len(x->replay_esn, re);
1877e2b19125SSteffen Klassert 	if (err)
1878e2b19125SSteffen Klassert 		goto out;
1879e2b19125SSteffen Klassert 
1880d51d081dSJamal Hadi Salim 	spin_lock_bh(&x->lock);
1881e3ac104dSMathias Krause 	xfrm_update_ae_params(x, attrs, 1);
1882d51d081dSJamal Hadi Salim 	spin_unlock_bh(&x->lock);
1883d51d081dSJamal Hadi Salim 
1884d51d081dSJamal Hadi Salim 	c.event = nlh->nlmsg_type;
1885d51d081dSJamal Hadi Salim 	c.seq = nlh->nlmsg_seq;
188615e47304SEric W. Biederman 	c.portid = nlh->nlmsg_pid;
1887d51d081dSJamal Hadi Salim 	c.data.aevent = XFRM_AE_CU;
1888d51d081dSJamal Hadi Salim 	km_state_notify(x, &c);
1889d51d081dSJamal Hadi Salim 	err = 0;
1890d51d081dSJamal Hadi Salim out:
1891d51d081dSJamal Hadi Salim 	xfrm_state_put(x);
1892d51d081dSJamal Hadi Salim 	return err;
1893d51d081dSJamal Hadi Salim }
1894d51d081dSJamal Hadi Salim 
189522e70050SChristoph Hellwig static int xfrm_flush_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
18965424f32eSThomas Graf 		struct nlattr **attrs)
18971da177e4SLinus Torvalds {
1898fc34acd3SAlexey Dobriyan 	struct net *net = sock_net(skb->sk);
189926b15dadSJamal Hadi Salim 	struct km_event c;
1900b798a9edSJamal Hadi Salim 	u8 type = XFRM_POLICY_TYPE_MAIN;
1901f7b6983fSMasahide NAKAMURA 	int err;
1902161a09e7SJoy Latten 	struct xfrm_audit audit_info;
190326b15dadSJamal Hadi Salim 
190435a7aa08SThomas Graf 	err = copy_from_user_policy_type(&type, attrs);
1905f7b6983fSMasahide NAKAMURA 	if (err)
1906f7b6983fSMasahide NAKAMURA 		return err;
1907f7b6983fSMasahide NAKAMURA 
1908c53fa1edSPatrick McHardy 	audit_info.loginuid = audit_get_loginuid(current);
1909c53fa1edSPatrick McHardy 	audit_info.sessionid = audit_get_sessionid(current);
1910c53fa1edSPatrick McHardy 	security_task_getsecid(current, &audit_info.secid);
1911fc34acd3SAlexey Dobriyan 	err = xfrm_policy_flush(net, type, &audit_info);
19122f1eb65fSJamal Hadi Salim 	if (err) {
19132f1eb65fSJamal Hadi Salim 		if (err == -ESRCH) /* empty table */
19142f1eb65fSJamal Hadi Salim 			return 0;
1915069c474eSDavid S. Miller 		return err;
19162f1eb65fSJamal Hadi Salim 	}
19172f1eb65fSJamal Hadi Salim 
1918f7b6983fSMasahide NAKAMURA 	c.data.type = type;
1919f60f6b8fSHerbert Xu 	c.event = nlh->nlmsg_type;
192026b15dadSJamal Hadi Salim 	c.seq = nlh->nlmsg_seq;
192115e47304SEric W. Biederman 	c.portid = nlh->nlmsg_pid;
19227067802eSAlexey Dobriyan 	c.net = net;
192326b15dadSJamal Hadi Salim 	km_policy_notify(NULL, 0, &c);
19241da177e4SLinus Torvalds 	return 0;
19251da177e4SLinus Torvalds }
19261da177e4SLinus Torvalds 
192722e70050SChristoph Hellwig static int xfrm_add_pol_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
19285424f32eSThomas Graf 		struct nlattr **attrs)
19296c5c8ca7SJamal Hadi Salim {
1930fc34acd3SAlexey Dobriyan 	struct net *net = sock_net(skb->sk);
19316c5c8ca7SJamal Hadi Salim 	struct xfrm_policy *xp;
19327b67c857SThomas Graf 	struct xfrm_user_polexpire *up = nlmsg_data(nlh);
19336c5c8ca7SJamal Hadi Salim 	struct xfrm_userpolicy_info *p = &up->pol;
1934b798a9edSJamal Hadi Salim 	u8 type = XFRM_POLICY_TYPE_MAIN;
19356c5c8ca7SJamal Hadi Salim 	int err = -ENOENT;
1936295fae56SJamal Hadi Salim 	struct xfrm_mark m;
1937295fae56SJamal Hadi Salim 	u32 mark = xfrm_mark_get(attrs, &m);
19386c5c8ca7SJamal Hadi Salim 
193935a7aa08SThomas Graf 	err = copy_from_user_policy_type(&type, attrs);
1940f7b6983fSMasahide NAKAMURA 	if (err)
1941f7b6983fSMasahide NAKAMURA 		return err;
1942f7b6983fSMasahide NAKAMURA 
1943c8bf4d04STimo Teräs 	err = verify_policy_dir(p->dir);
1944c8bf4d04STimo Teräs 	if (err)
1945c8bf4d04STimo Teräs 		return err;
1946c8bf4d04STimo Teräs 
19476c5c8ca7SJamal Hadi Salim 	if (p->index)
1948295fae56SJamal Hadi Salim 		xp = xfrm_policy_byid(net, mark, type, p->dir, p->index, 0, &err);
19496c5c8ca7SJamal Hadi Salim 	else {
19505424f32eSThomas Graf 		struct nlattr *rt = attrs[XFRMA_SEC_CTX];
195103e1ad7bSPaul Moore 		struct xfrm_sec_ctx *ctx;
19526c5c8ca7SJamal Hadi Salim 
195335a7aa08SThomas Graf 		err = verify_sec_ctx_len(attrs);
19546c5c8ca7SJamal Hadi Salim 		if (err)
19556c5c8ca7SJamal Hadi Salim 			return err;
19566c5c8ca7SJamal Hadi Salim 
19572c8dd116SDenis V. Lunev 		ctx = NULL;
19586c5c8ca7SJamal Hadi Salim 		if (rt) {
19595424f32eSThomas Graf 			struct xfrm_user_sec_ctx *uctx = nla_data(rt);
19606c5c8ca7SJamal Hadi Salim 
196103e1ad7bSPaul Moore 			err = security_xfrm_policy_alloc(&ctx, uctx);
196203e1ad7bSPaul Moore 			if (err)
19636c5c8ca7SJamal Hadi Salim 				return err;
19642c8dd116SDenis V. Lunev 		}
1965295fae56SJamal Hadi Salim 		xp = xfrm_policy_bysel_ctx(net, mark, type, p->dir,
19666f26b61eSJamal Hadi Salim 					   &p->sel, ctx, 0, &err);
196703e1ad7bSPaul Moore 		security_xfrm_policy_free(ctx);
19686c5c8ca7SJamal Hadi Salim 	}
19696c5c8ca7SJamal Hadi Salim 	if (xp == NULL)
1970ef41aaa0SEric Paris 		return -ENOENT;
197103e1ad7bSPaul Moore 
1972ea2dea9dSTimo Teräs 	if (unlikely(xp->walk.dead))
19736c5c8ca7SJamal Hadi Salim 		goto out;
19746c5c8ca7SJamal Hadi Salim 
19756c5c8ca7SJamal Hadi Salim 	err = 0;
19766c5c8ca7SJamal Hadi Salim 	if (up->hard) {
1977e1760bd5SEric W. Biederman 		kuid_t loginuid = audit_get_loginuid(current);
19784440e854SEric Paris 		unsigned int sessionid = audit_get_sessionid(current);
1979c53fa1edSPatrick McHardy 		u32 sid;
1980c53fa1edSPatrick McHardy 
1981c53fa1edSPatrick McHardy 		security_task_getsecid(current, &sid);
19826c5c8ca7SJamal Hadi Salim 		xfrm_policy_delete(xp, p->dir);
19832532386fSEric Paris 		xfrm_audit_policy_delete(xp, 1, loginuid, sessionid, sid);
1984161a09e7SJoy Latten 
19856c5c8ca7SJamal Hadi Salim 	} else {
19866c5c8ca7SJamal Hadi Salim 		// reset the timers here?
198762db5cfdSstephen hemminger 		WARN(1, "Dont know what to do with soft policy expire\n");
19886c5c8ca7SJamal Hadi Salim 	}
1989c6bb8136SEric W. Biederman 	km_policy_expired(xp, p->dir, up->hard, nlh->nlmsg_pid);
19906c5c8ca7SJamal Hadi Salim 
19916c5c8ca7SJamal Hadi Salim out:
19926c5c8ca7SJamal Hadi Salim 	xfrm_pol_put(xp);
19936c5c8ca7SJamal Hadi Salim 	return err;
19946c5c8ca7SJamal Hadi Salim }
19956c5c8ca7SJamal Hadi Salim 
199622e70050SChristoph Hellwig static int xfrm_add_sa_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
19975424f32eSThomas Graf 		struct nlattr **attrs)
199853bc6b4dSJamal Hadi Salim {
1999fc34acd3SAlexey Dobriyan 	struct net *net = sock_net(skb->sk);
200053bc6b4dSJamal Hadi Salim 	struct xfrm_state *x;
200153bc6b4dSJamal Hadi Salim 	int err;
20027b67c857SThomas Graf 	struct xfrm_user_expire *ue = nlmsg_data(nlh);
200353bc6b4dSJamal Hadi Salim 	struct xfrm_usersa_info *p = &ue->state;
20046f26b61eSJamal Hadi Salim 	struct xfrm_mark m;
2005928497f0SNicolas Dichtel 	u32 mark = xfrm_mark_get(attrs, &m);
200653bc6b4dSJamal Hadi Salim 
20076f26b61eSJamal Hadi Salim 	x = xfrm_state_lookup(net, mark, &p->id.daddr, p->id.spi, p->id.proto, p->family);
200853bc6b4dSJamal Hadi Salim 
20093a765aa5SDavid S. Miller 	err = -ENOENT;
201053bc6b4dSJamal Hadi Salim 	if (x == NULL)
201153bc6b4dSJamal Hadi Salim 		return err;
201253bc6b4dSJamal Hadi Salim 
201353bc6b4dSJamal Hadi Salim 	spin_lock_bh(&x->lock);
20143a765aa5SDavid S. Miller 	err = -EINVAL;
201553bc6b4dSJamal Hadi Salim 	if (x->km.state != XFRM_STATE_VALID)
201653bc6b4dSJamal Hadi Salim 		goto out;
2017c6bb8136SEric W. Biederman 	km_state_expired(x, ue->hard, nlh->nlmsg_pid);
201853bc6b4dSJamal Hadi Salim 
2019161a09e7SJoy Latten 	if (ue->hard) {
2020e1760bd5SEric W. Biederman 		kuid_t loginuid = audit_get_loginuid(current);
20214440e854SEric Paris 		unsigned int sessionid = audit_get_sessionid(current);
2022c53fa1edSPatrick McHardy 		u32 sid;
2023c53fa1edSPatrick McHardy 
2024c53fa1edSPatrick McHardy 		security_task_getsecid(current, &sid);
202553bc6b4dSJamal Hadi Salim 		__xfrm_state_delete(x);
20262532386fSEric Paris 		xfrm_audit_state_delete(x, 1, loginuid, sessionid, sid);
2027161a09e7SJoy Latten 	}
20283a765aa5SDavid S. Miller 	err = 0;
202953bc6b4dSJamal Hadi Salim out:
203053bc6b4dSJamal Hadi Salim 	spin_unlock_bh(&x->lock);
203153bc6b4dSJamal Hadi Salim 	xfrm_state_put(x);
203253bc6b4dSJamal Hadi Salim 	return err;
203353bc6b4dSJamal Hadi Salim }
203453bc6b4dSJamal Hadi Salim 
203522e70050SChristoph Hellwig static int xfrm_add_acquire(struct sk_buff *skb, struct nlmsghdr *nlh,
20365424f32eSThomas Graf 		struct nlattr **attrs)
2037980ebd25SJamal Hadi Salim {
2038fc34acd3SAlexey Dobriyan 	struct net *net = sock_net(skb->sk);
2039980ebd25SJamal Hadi Salim 	struct xfrm_policy *xp;
2040980ebd25SJamal Hadi Salim 	struct xfrm_user_tmpl *ut;
2041980ebd25SJamal Hadi Salim 	int i;
20425424f32eSThomas Graf 	struct nlattr *rt = attrs[XFRMA_TMPL];
20436f26b61eSJamal Hadi Salim 	struct xfrm_mark mark;
2044980ebd25SJamal Hadi Salim 
20457b67c857SThomas Graf 	struct xfrm_user_acquire *ua = nlmsg_data(nlh);
2046fc34acd3SAlexey Dobriyan 	struct xfrm_state *x = xfrm_state_alloc(net);
2047980ebd25SJamal Hadi Salim 	int err = -ENOMEM;
2048980ebd25SJamal Hadi Salim 
2049980ebd25SJamal Hadi Salim 	if (!x)
2050d8eb9307SIlpo Järvinen 		goto nomem;
2051980ebd25SJamal Hadi Salim 
20526f26b61eSJamal Hadi Salim 	xfrm_mark_get(attrs, &mark);
20536f26b61eSJamal Hadi Salim 
2054980ebd25SJamal Hadi Salim 	err = verify_newpolicy_info(&ua->policy);
2055d8eb9307SIlpo Järvinen 	if (err)
2056d8eb9307SIlpo Järvinen 		goto bad_policy;
2057980ebd25SJamal Hadi Salim 
2058980ebd25SJamal Hadi Salim 	/*   build an XP */
2059fc34acd3SAlexey Dobriyan 	xp = xfrm_policy_construct(net, &ua->policy, attrs, &err);
2060d8eb9307SIlpo Järvinen 	if (!xp)
2061d8eb9307SIlpo Järvinen 		goto free_state;
2062980ebd25SJamal Hadi Salim 
2063980ebd25SJamal Hadi Salim 	memcpy(&x->id, &ua->id, sizeof(ua->id));
2064980ebd25SJamal Hadi Salim 	memcpy(&x->props.saddr, &ua->saddr, sizeof(ua->saddr));
2065980ebd25SJamal Hadi Salim 	memcpy(&x->sel, &ua->sel, sizeof(ua->sel));
20666f26b61eSJamal Hadi Salim 	xp->mark.m = x->mark.m = mark.m;
20676f26b61eSJamal Hadi Salim 	xp->mark.v = x->mark.v = mark.v;
20685424f32eSThomas Graf 	ut = nla_data(rt);
2069980ebd25SJamal Hadi Salim 	/* extract the templates and for each call km_key */
2070980ebd25SJamal Hadi Salim 	for (i = 0; i < xp->xfrm_nr; i++, ut++) {
2071980ebd25SJamal Hadi Salim 		struct xfrm_tmpl *t = &xp->xfrm_vec[i];
2072980ebd25SJamal Hadi Salim 		memcpy(&x->id, &t->id, sizeof(x->id));
2073980ebd25SJamal Hadi Salim 		x->props.mode = t->mode;
2074980ebd25SJamal Hadi Salim 		x->props.reqid = t->reqid;
2075980ebd25SJamal Hadi Salim 		x->props.family = ut->family;
2076980ebd25SJamal Hadi Salim 		t->aalgos = ua->aalgos;
2077980ebd25SJamal Hadi Salim 		t->ealgos = ua->ealgos;
2078980ebd25SJamal Hadi Salim 		t->calgos = ua->calgos;
2079980ebd25SJamal Hadi Salim 		err = km_query(x, t, xp);
2080980ebd25SJamal Hadi Salim 
2081980ebd25SJamal Hadi Salim 	}
2082980ebd25SJamal Hadi Salim 
2083980ebd25SJamal Hadi Salim 	kfree(x);
2084980ebd25SJamal Hadi Salim 	kfree(xp);
2085980ebd25SJamal Hadi Salim 
2086980ebd25SJamal Hadi Salim 	return 0;
2087d8eb9307SIlpo Järvinen 
2088d8eb9307SIlpo Järvinen bad_policy:
208962db5cfdSstephen hemminger 	WARN(1, "BAD policy passed\n");
2090d8eb9307SIlpo Järvinen free_state:
2091d8eb9307SIlpo Järvinen 	kfree(x);
2092d8eb9307SIlpo Järvinen nomem:
2093d8eb9307SIlpo Järvinen 	return err;
2094980ebd25SJamal Hadi Salim }
2095980ebd25SJamal Hadi Salim 
20965c79de6eSShinta Sugimoto #ifdef CONFIG_XFRM_MIGRATE
20975c79de6eSShinta Sugimoto static int copy_from_user_migrate(struct xfrm_migrate *ma,
209813c1d189SArnaud Ebalard 				  struct xfrm_kmaddress *k,
20995424f32eSThomas Graf 				  struct nlattr **attrs, int *num)
21005c79de6eSShinta Sugimoto {
21015424f32eSThomas Graf 	struct nlattr *rt = attrs[XFRMA_MIGRATE];
21025c79de6eSShinta Sugimoto 	struct xfrm_user_migrate *um;
21035c79de6eSShinta Sugimoto 	int i, num_migrate;
21045c79de6eSShinta Sugimoto 
210513c1d189SArnaud Ebalard 	if (k != NULL) {
210613c1d189SArnaud Ebalard 		struct xfrm_user_kmaddress *uk;
210713c1d189SArnaud Ebalard 
210813c1d189SArnaud Ebalard 		uk = nla_data(attrs[XFRMA_KMADDRESS]);
210913c1d189SArnaud Ebalard 		memcpy(&k->local, &uk->local, sizeof(k->local));
211013c1d189SArnaud Ebalard 		memcpy(&k->remote, &uk->remote, sizeof(k->remote));
211113c1d189SArnaud Ebalard 		k->family = uk->family;
211213c1d189SArnaud Ebalard 		k->reserved = uk->reserved;
211313c1d189SArnaud Ebalard 	}
211413c1d189SArnaud Ebalard 
21155424f32eSThomas Graf 	um = nla_data(rt);
21165424f32eSThomas Graf 	num_migrate = nla_len(rt) / sizeof(*um);
21175c79de6eSShinta Sugimoto 
21185c79de6eSShinta Sugimoto 	if (num_migrate <= 0 || num_migrate > XFRM_MAX_DEPTH)
21195c79de6eSShinta Sugimoto 		return -EINVAL;
21205c79de6eSShinta Sugimoto 
21215c79de6eSShinta Sugimoto 	for (i = 0; i < num_migrate; i++, um++, ma++) {
21225c79de6eSShinta Sugimoto 		memcpy(&ma->old_daddr, &um->old_daddr, sizeof(ma->old_daddr));
21235c79de6eSShinta Sugimoto 		memcpy(&ma->old_saddr, &um->old_saddr, sizeof(ma->old_saddr));
21245c79de6eSShinta Sugimoto 		memcpy(&ma->new_daddr, &um->new_daddr, sizeof(ma->new_daddr));
21255c79de6eSShinta Sugimoto 		memcpy(&ma->new_saddr, &um->new_saddr, sizeof(ma->new_saddr));
21265c79de6eSShinta Sugimoto 
21275c79de6eSShinta Sugimoto 		ma->proto = um->proto;
21285c79de6eSShinta Sugimoto 		ma->mode = um->mode;
21295c79de6eSShinta Sugimoto 		ma->reqid = um->reqid;
21305c79de6eSShinta Sugimoto 
21315c79de6eSShinta Sugimoto 		ma->old_family = um->old_family;
21325c79de6eSShinta Sugimoto 		ma->new_family = um->new_family;
21335c79de6eSShinta Sugimoto 	}
21345c79de6eSShinta Sugimoto 
21355c79de6eSShinta Sugimoto 	*num = i;
21365c79de6eSShinta Sugimoto 	return 0;
21375c79de6eSShinta Sugimoto }
21385c79de6eSShinta Sugimoto 
21395c79de6eSShinta Sugimoto static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh,
21405424f32eSThomas Graf 			   struct nlattr **attrs)
21415c79de6eSShinta Sugimoto {
21427b67c857SThomas Graf 	struct xfrm_userpolicy_id *pi = nlmsg_data(nlh);
21435c79de6eSShinta Sugimoto 	struct xfrm_migrate m[XFRM_MAX_DEPTH];
214413c1d189SArnaud Ebalard 	struct xfrm_kmaddress km, *kmp;
21455c79de6eSShinta Sugimoto 	u8 type;
21465c79de6eSShinta Sugimoto 	int err;
21475c79de6eSShinta Sugimoto 	int n = 0;
21488d549c4fSFan Du 	struct net *net = sock_net(skb->sk);
21495c79de6eSShinta Sugimoto 
215035a7aa08SThomas Graf 	if (attrs[XFRMA_MIGRATE] == NULL)
2151cf5cb79fSThomas Graf 		return -EINVAL;
21525c79de6eSShinta Sugimoto 
215313c1d189SArnaud Ebalard 	kmp = attrs[XFRMA_KMADDRESS] ? &km : NULL;
215413c1d189SArnaud Ebalard 
21555424f32eSThomas Graf 	err = copy_from_user_policy_type(&type, attrs);
21565c79de6eSShinta Sugimoto 	if (err)
21575c79de6eSShinta Sugimoto 		return err;
21585c79de6eSShinta Sugimoto 
215913c1d189SArnaud Ebalard 	err = copy_from_user_migrate((struct xfrm_migrate *)m, kmp, attrs, &n);
21605c79de6eSShinta Sugimoto 	if (err)
21615c79de6eSShinta Sugimoto 		return err;
21625c79de6eSShinta Sugimoto 
21635c79de6eSShinta Sugimoto 	if (!n)
21645c79de6eSShinta Sugimoto 		return 0;
21655c79de6eSShinta Sugimoto 
21668d549c4fSFan Du 	xfrm_migrate(&pi->sel, pi->dir, type, m, n, kmp, net);
21675c79de6eSShinta Sugimoto 
21685c79de6eSShinta Sugimoto 	return 0;
21695c79de6eSShinta Sugimoto }
21705c79de6eSShinta Sugimoto #else
21715c79de6eSShinta Sugimoto static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh,
21725424f32eSThomas Graf 			   struct nlattr **attrs)
21735c79de6eSShinta Sugimoto {
21745c79de6eSShinta Sugimoto 	return -ENOPROTOOPT;
21755c79de6eSShinta Sugimoto }
21765c79de6eSShinta Sugimoto #endif
21775c79de6eSShinta Sugimoto 
21785c79de6eSShinta Sugimoto #ifdef CONFIG_XFRM_MIGRATE
2179183cad12SDavid S. Miller static int copy_to_user_migrate(const struct xfrm_migrate *m, struct sk_buff *skb)
21805c79de6eSShinta Sugimoto {
21815c79de6eSShinta Sugimoto 	struct xfrm_user_migrate um;
21825c79de6eSShinta Sugimoto 
21835c79de6eSShinta Sugimoto 	memset(&um, 0, sizeof(um));
21845c79de6eSShinta Sugimoto 	um.proto = m->proto;
21855c79de6eSShinta Sugimoto 	um.mode = m->mode;
21865c79de6eSShinta Sugimoto 	um.reqid = m->reqid;
21875c79de6eSShinta Sugimoto 	um.old_family = m->old_family;
21885c79de6eSShinta Sugimoto 	memcpy(&um.old_daddr, &m->old_daddr, sizeof(um.old_daddr));
21895c79de6eSShinta Sugimoto 	memcpy(&um.old_saddr, &m->old_saddr, sizeof(um.old_saddr));
21905c79de6eSShinta Sugimoto 	um.new_family = m->new_family;
21915c79de6eSShinta Sugimoto 	memcpy(&um.new_daddr, &m->new_daddr, sizeof(um.new_daddr));
21925c79de6eSShinta Sugimoto 	memcpy(&um.new_saddr, &m->new_saddr, sizeof(um.new_saddr));
21935c79de6eSShinta Sugimoto 
2194c0144beaSThomas Graf 	return nla_put(skb, XFRMA_MIGRATE, sizeof(um), &um);
21955c79de6eSShinta Sugimoto }
21965c79de6eSShinta Sugimoto 
2197183cad12SDavid S. Miller static int copy_to_user_kmaddress(const struct xfrm_kmaddress *k, struct sk_buff *skb)
219813c1d189SArnaud Ebalard {
219913c1d189SArnaud Ebalard 	struct xfrm_user_kmaddress uk;
220013c1d189SArnaud Ebalard 
220113c1d189SArnaud Ebalard 	memset(&uk, 0, sizeof(uk));
220213c1d189SArnaud Ebalard 	uk.family = k->family;
220313c1d189SArnaud Ebalard 	uk.reserved = k->reserved;
220413c1d189SArnaud Ebalard 	memcpy(&uk.local, &k->local, sizeof(uk.local));
2205a1caa322SArnaud Ebalard 	memcpy(&uk.remote, &k->remote, sizeof(uk.remote));
220613c1d189SArnaud Ebalard 
220713c1d189SArnaud Ebalard 	return nla_put(skb, XFRMA_KMADDRESS, sizeof(uk), &uk);
220813c1d189SArnaud Ebalard }
220913c1d189SArnaud Ebalard 
221013c1d189SArnaud Ebalard static inline size_t xfrm_migrate_msgsize(int num_migrate, int with_kma)
22117deb2264SThomas Graf {
22127deb2264SThomas Graf 	return NLMSG_ALIGN(sizeof(struct xfrm_userpolicy_id))
221313c1d189SArnaud Ebalard 	      + (with_kma ? nla_total_size(sizeof(struct xfrm_kmaddress)) : 0)
22147deb2264SThomas Graf 	      + nla_total_size(sizeof(struct xfrm_user_migrate) * num_migrate)
22157deb2264SThomas Graf 	      + userpolicy_type_attrsize();
22167deb2264SThomas Graf }
22177deb2264SThomas Graf 
2218183cad12SDavid S. Miller static int build_migrate(struct sk_buff *skb, const struct xfrm_migrate *m,
2219183cad12SDavid S. Miller 			 int num_migrate, const struct xfrm_kmaddress *k,
2220183cad12SDavid S. Miller 			 const struct xfrm_selector *sel, u8 dir, u8 type)
22215c79de6eSShinta Sugimoto {
2222183cad12SDavid S. Miller 	const struct xfrm_migrate *mp;
22235c79de6eSShinta Sugimoto 	struct xfrm_userpolicy_id *pol_id;
22245c79de6eSShinta Sugimoto 	struct nlmsghdr *nlh;
22251d1e34ddSDavid S. Miller 	int i, err;
22265c79de6eSShinta Sugimoto 
222779b8b7f4SThomas Graf 	nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_MIGRATE, sizeof(*pol_id), 0);
222879b8b7f4SThomas Graf 	if (nlh == NULL)
222979b8b7f4SThomas Graf 		return -EMSGSIZE;
22305c79de6eSShinta Sugimoto 
22317b67c857SThomas Graf 	pol_id = nlmsg_data(nlh);
22325c79de6eSShinta Sugimoto 	/* copy data from selector, dir, and type to the pol_id */
22335c79de6eSShinta Sugimoto 	memset(pol_id, 0, sizeof(*pol_id));
22345c79de6eSShinta Sugimoto 	memcpy(&pol_id->sel, sel, sizeof(pol_id->sel));
22355c79de6eSShinta Sugimoto 	pol_id->dir = dir;
22365c79de6eSShinta Sugimoto 
22371d1e34ddSDavid S. Miller 	if (k != NULL) {
22381d1e34ddSDavid S. Miller 		err = copy_to_user_kmaddress(k, skb);
22391d1e34ddSDavid S. Miller 		if (err)
22401d1e34ddSDavid S. Miller 			goto out_cancel;
22411d1e34ddSDavid S. Miller 	}
22421d1e34ddSDavid S. Miller 	err = copy_to_user_policy_type(type, skb);
22431d1e34ddSDavid S. Miller 	if (err)
22441d1e34ddSDavid S. Miller 		goto out_cancel;
22455c79de6eSShinta Sugimoto 	for (i = 0, mp = m ; i < num_migrate; i++, mp++) {
22461d1e34ddSDavid S. Miller 		err = copy_to_user_migrate(mp, skb);
22471d1e34ddSDavid S. Miller 		if (err)
22481d1e34ddSDavid S. Miller 			goto out_cancel;
22495c79de6eSShinta Sugimoto 	}
22505c79de6eSShinta Sugimoto 
22519825069dSThomas Graf 	return nlmsg_end(skb, nlh);
22521d1e34ddSDavid S. Miller 
22531d1e34ddSDavid S. Miller out_cancel:
22549825069dSThomas Graf 	nlmsg_cancel(skb, nlh);
22551d1e34ddSDavid S. Miller 	return err;
22565c79de6eSShinta Sugimoto }
22575c79de6eSShinta Sugimoto 
2258183cad12SDavid S. Miller static int xfrm_send_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
2259183cad12SDavid S. Miller 			     const struct xfrm_migrate *m, int num_migrate,
2260183cad12SDavid S. Miller 			     const struct xfrm_kmaddress *k)
22615c79de6eSShinta Sugimoto {
2262a6483b79SAlexey Dobriyan 	struct net *net = &init_net;
22635c79de6eSShinta Sugimoto 	struct sk_buff *skb;
22645c79de6eSShinta Sugimoto 
226513c1d189SArnaud Ebalard 	skb = nlmsg_new(xfrm_migrate_msgsize(num_migrate, !!k), GFP_ATOMIC);
22665c79de6eSShinta Sugimoto 	if (skb == NULL)
22675c79de6eSShinta Sugimoto 		return -ENOMEM;
22685c79de6eSShinta Sugimoto 
22695c79de6eSShinta Sugimoto 	/* build migrate */
227013c1d189SArnaud Ebalard 	if (build_migrate(skb, m, num_migrate, k, sel, dir, type) < 0)
22715c79de6eSShinta Sugimoto 		BUG();
22725c79de6eSShinta Sugimoto 
2273a6483b79SAlexey Dobriyan 	return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_MIGRATE, GFP_ATOMIC);
22745c79de6eSShinta Sugimoto }
22755c79de6eSShinta Sugimoto #else
2276183cad12SDavid S. Miller static int xfrm_send_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
2277183cad12SDavid S. Miller 			     const struct xfrm_migrate *m, int num_migrate,
2278183cad12SDavid S. Miller 			     const struct xfrm_kmaddress *k)
22795c79de6eSShinta Sugimoto {
22805c79de6eSShinta Sugimoto 	return -ENOPROTOOPT;
22815c79de6eSShinta Sugimoto }
22825c79de6eSShinta Sugimoto #endif
2283d51d081dSJamal Hadi Salim 
2284a7bd9a45SThomas Graf #define XMSGSIZE(type) sizeof(struct type)
2285492b558bSThomas Graf 
2286492b558bSThomas Graf static const int xfrm_msg_min[XFRM_NR_MSGTYPES] = {
228766f9a259SDavid S. Miller 	[XFRM_MSG_NEWSA       - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
2288492b558bSThomas Graf 	[XFRM_MSG_DELSA       - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
2289492b558bSThomas Graf 	[XFRM_MSG_GETSA       - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
2290492b558bSThomas Graf 	[XFRM_MSG_NEWPOLICY   - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
2291492b558bSThomas Graf 	[XFRM_MSG_DELPOLICY   - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
2292492b558bSThomas Graf 	[XFRM_MSG_GETPOLICY   - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
2293492b558bSThomas Graf 	[XFRM_MSG_ALLOCSPI    - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userspi_info),
2294980ebd25SJamal Hadi Salim 	[XFRM_MSG_ACQUIRE     - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_acquire),
229553bc6b4dSJamal Hadi Salim 	[XFRM_MSG_EXPIRE      - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_expire),
2296492b558bSThomas Graf 	[XFRM_MSG_UPDPOLICY   - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
229766f9a259SDavid S. Miller 	[XFRM_MSG_UPDSA       - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
22986c5c8ca7SJamal Hadi Salim 	[XFRM_MSG_POLEXPIRE   - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_polexpire),
2299492b558bSThomas Graf 	[XFRM_MSG_FLUSHSA     - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_flush),
2300a7bd9a45SThomas Graf 	[XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = 0,
2301d51d081dSJamal Hadi Salim 	[XFRM_MSG_NEWAE       - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
2302d51d081dSJamal Hadi Salim 	[XFRM_MSG_GETAE       - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
230397a64b45SMasahide NAKAMURA 	[XFRM_MSG_REPORT      - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_report),
23045c79de6eSShinta Sugimoto 	[XFRM_MSG_MIGRATE     - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
2305a7bd9a45SThomas Graf 	[XFRM_MSG_GETSADINFO  - XFRM_MSG_BASE] = sizeof(u32),
2306a7bd9a45SThomas Graf 	[XFRM_MSG_GETSPDINFO  - XFRM_MSG_BASE] = sizeof(u32),
23071da177e4SLinus Torvalds };
23081da177e4SLinus Torvalds 
2309492b558bSThomas Graf #undef XMSGSIZE
2310492b558bSThomas Graf 
2311cf5cb79fSThomas Graf static const struct nla_policy xfrma_policy[XFRMA_MAX+1] = {
2312c28e9304Sjamal 	[XFRMA_SA]		= { .len = sizeof(struct xfrm_usersa_info)},
2313c28e9304Sjamal 	[XFRMA_POLICY]		= { .len = sizeof(struct xfrm_userpolicy_info)},
2314c28e9304Sjamal 	[XFRMA_LASTUSED]	= { .type = NLA_U64},
2315c28e9304Sjamal 	[XFRMA_ALG_AUTH_TRUNC]	= { .len = sizeof(struct xfrm_algo_auth)},
23161a6509d9SHerbert Xu 	[XFRMA_ALG_AEAD]	= { .len = sizeof(struct xfrm_algo_aead) },
2317cf5cb79fSThomas Graf 	[XFRMA_ALG_AUTH]	= { .len = sizeof(struct xfrm_algo) },
2318cf5cb79fSThomas Graf 	[XFRMA_ALG_CRYPT]	= { .len = sizeof(struct xfrm_algo) },
2319cf5cb79fSThomas Graf 	[XFRMA_ALG_COMP]	= { .len = sizeof(struct xfrm_algo) },
2320cf5cb79fSThomas Graf 	[XFRMA_ENCAP]		= { .len = sizeof(struct xfrm_encap_tmpl) },
2321cf5cb79fSThomas Graf 	[XFRMA_TMPL]		= { .len = sizeof(struct xfrm_user_tmpl) },
2322cf5cb79fSThomas Graf 	[XFRMA_SEC_CTX]		= { .len = sizeof(struct xfrm_sec_ctx) },
2323cf5cb79fSThomas Graf 	[XFRMA_LTIME_VAL]	= { .len = sizeof(struct xfrm_lifetime_cur) },
2324cf5cb79fSThomas Graf 	[XFRMA_REPLAY_VAL]	= { .len = sizeof(struct xfrm_replay_state) },
2325cf5cb79fSThomas Graf 	[XFRMA_REPLAY_THRESH]	= { .type = NLA_U32 },
2326cf5cb79fSThomas Graf 	[XFRMA_ETIMER_THRESH]	= { .type = NLA_U32 },
2327cf5cb79fSThomas Graf 	[XFRMA_SRCADDR]		= { .len = sizeof(xfrm_address_t) },
2328cf5cb79fSThomas Graf 	[XFRMA_COADDR]		= { .len = sizeof(xfrm_address_t) },
2329cf5cb79fSThomas Graf 	[XFRMA_POLICY_TYPE]	= { .len = sizeof(struct xfrm_userpolicy_type)},
2330cf5cb79fSThomas Graf 	[XFRMA_MIGRATE]		= { .len = sizeof(struct xfrm_user_migrate) },
233113c1d189SArnaud Ebalard 	[XFRMA_KMADDRESS]	= { .len = sizeof(struct xfrm_user_kmaddress) },
23326f26b61eSJamal Hadi Salim 	[XFRMA_MARK]		= { .len = sizeof(struct xfrm_mark) },
233335d2856bSMartin Willi 	[XFRMA_TFCPAD]		= { .type = NLA_U32 },
2334d8647b79SSteffen Klassert 	[XFRMA_REPLAY_ESN_VAL]	= { .len = sizeof(struct xfrm_replay_state_esn) },
2335a947b0a9SNicolas Dichtel 	[XFRMA_SA_EXTRA_FLAGS]	= { .type = NLA_U32 },
2336*d3623099SNicolas Dichtel 	[XFRMA_PROTO]		= { .type = NLA_U8 },
2337*d3623099SNicolas Dichtel 	[XFRMA_FILTER]		= { .len = sizeof(struct xfrm_filter) },
2338cf5cb79fSThomas Graf };
2339cf5cb79fSThomas Graf 
234005600a79SMathias Krause static const struct xfrm_link {
23415424f32eSThomas Graf 	int (*doit)(struct sk_buff *, struct nlmsghdr *, struct nlattr **);
23421da177e4SLinus Torvalds 	int (*dump)(struct sk_buff *, struct netlink_callback *);
23434c563f76STimo Teras 	int (*done)(struct netlink_callback *);
2344492b558bSThomas Graf } xfrm_dispatch[XFRM_NR_MSGTYPES] = {
2345492b558bSThomas Graf 	[XFRM_MSG_NEWSA       - XFRM_MSG_BASE] = { .doit = xfrm_add_sa        },
2346492b558bSThomas Graf 	[XFRM_MSG_DELSA       - XFRM_MSG_BASE] = { .doit = xfrm_del_sa        },
2347492b558bSThomas Graf 	[XFRM_MSG_GETSA       - XFRM_MSG_BASE] = { .doit = xfrm_get_sa,
23484c563f76STimo Teras 						   .dump = xfrm_dump_sa,
23494c563f76STimo Teras 						   .done = xfrm_dump_sa_done  },
2350492b558bSThomas Graf 	[XFRM_MSG_NEWPOLICY   - XFRM_MSG_BASE] = { .doit = xfrm_add_policy    },
2351492b558bSThomas Graf 	[XFRM_MSG_DELPOLICY   - XFRM_MSG_BASE] = { .doit = xfrm_get_policy    },
2352492b558bSThomas Graf 	[XFRM_MSG_GETPOLICY   - XFRM_MSG_BASE] = { .doit = xfrm_get_policy,
23534c563f76STimo Teras 						   .dump = xfrm_dump_policy,
23544c563f76STimo Teras 						   .done = xfrm_dump_policy_done },
2355492b558bSThomas Graf 	[XFRM_MSG_ALLOCSPI    - XFRM_MSG_BASE] = { .doit = xfrm_alloc_userspi },
2356980ebd25SJamal Hadi Salim 	[XFRM_MSG_ACQUIRE     - XFRM_MSG_BASE] = { .doit = xfrm_add_acquire   },
235753bc6b4dSJamal Hadi Salim 	[XFRM_MSG_EXPIRE      - XFRM_MSG_BASE] = { .doit = xfrm_add_sa_expire },
2358492b558bSThomas Graf 	[XFRM_MSG_UPDPOLICY   - XFRM_MSG_BASE] = { .doit = xfrm_add_policy    },
2359492b558bSThomas Graf 	[XFRM_MSG_UPDSA       - XFRM_MSG_BASE] = { .doit = xfrm_add_sa        },
23606c5c8ca7SJamal Hadi Salim 	[XFRM_MSG_POLEXPIRE   - XFRM_MSG_BASE] = { .doit = xfrm_add_pol_expire},
2361492b558bSThomas Graf 	[XFRM_MSG_FLUSHSA     - XFRM_MSG_BASE] = { .doit = xfrm_flush_sa      },
2362492b558bSThomas Graf 	[XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_flush_policy  },
2363d51d081dSJamal Hadi Salim 	[XFRM_MSG_NEWAE       - XFRM_MSG_BASE] = { .doit = xfrm_new_ae  },
2364d51d081dSJamal Hadi Salim 	[XFRM_MSG_GETAE       - XFRM_MSG_BASE] = { .doit = xfrm_get_ae  },
23655c79de6eSShinta Sugimoto 	[XFRM_MSG_MIGRATE     - XFRM_MSG_BASE] = { .doit = xfrm_do_migrate    },
236628d8909bSJamal Hadi Salim 	[XFRM_MSG_GETSADINFO  - XFRM_MSG_BASE] = { .doit = xfrm_get_sadinfo   },
2367ecfd6b18SJamal Hadi Salim 	[XFRM_MSG_GETSPDINFO  - XFRM_MSG_BASE] = { .doit = xfrm_get_spdinfo   },
23681da177e4SLinus Torvalds };
23691da177e4SLinus Torvalds 
23701d00a4ebSThomas Graf static int xfrm_user_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
23711da177e4SLinus Torvalds {
2372a6483b79SAlexey Dobriyan 	struct net *net = sock_net(skb->sk);
237335a7aa08SThomas Graf 	struct nlattr *attrs[XFRMA_MAX+1];
237405600a79SMathias Krause 	const struct xfrm_link *link;
2375a7bd9a45SThomas Graf 	int type, err;
23761da177e4SLinus Torvalds 
23771da177e4SLinus Torvalds 	type = nlh->nlmsg_type;
23781da177e4SLinus Torvalds 	if (type > XFRM_MSG_MAX)
23791d00a4ebSThomas Graf 		return -EINVAL;
23801da177e4SLinus Torvalds 
23811da177e4SLinus Torvalds 	type -= XFRM_MSG_BASE;
23821da177e4SLinus Torvalds 	link = &xfrm_dispatch[type];
23831da177e4SLinus Torvalds 
23841da177e4SLinus Torvalds 	/* All operations require privileges, even GET */
2385df008c91SEric W. Biederman 	if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
23861d00a4ebSThomas Graf 		return -EPERM;
23871da177e4SLinus Torvalds 
2388492b558bSThomas Graf 	if ((type == (XFRM_MSG_GETSA - XFRM_MSG_BASE) ||
2389492b558bSThomas Graf 	     type == (XFRM_MSG_GETPOLICY - XFRM_MSG_BASE)) &&
2390b8f3ab42SDavid S. Miller 	    (nlh->nlmsg_flags & NLM_F_DUMP)) {
23911da177e4SLinus Torvalds 		if (link->dump == NULL)
23921d00a4ebSThomas Graf 			return -EINVAL;
23931da177e4SLinus Torvalds 
239480d326faSPablo Neira Ayuso 		{
239580d326faSPablo Neira Ayuso 			struct netlink_dump_control c = {
239680d326faSPablo Neira Ayuso 				.dump = link->dump,
239780d326faSPablo Neira Ayuso 				.done = link->done,
239880d326faSPablo Neira Ayuso 			};
239980d326faSPablo Neira Ayuso 			return netlink_dump_start(net->xfrm.nlsk, skb, nlh, &c);
240080d326faSPablo Neira Ayuso 		}
24011da177e4SLinus Torvalds 	}
24021da177e4SLinus Torvalds 
240335a7aa08SThomas Graf 	err = nlmsg_parse(nlh, xfrm_msg_min[type], attrs, XFRMA_MAX,
2404cf5cb79fSThomas Graf 			  xfrma_policy);
2405a7bd9a45SThomas Graf 	if (err < 0)
2406a7bd9a45SThomas Graf 		return err;
24071da177e4SLinus Torvalds 
24081da177e4SLinus Torvalds 	if (link->doit == NULL)
24091d00a4ebSThomas Graf 		return -EINVAL;
24101da177e4SLinus Torvalds 
24115424f32eSThomas Graf 	return link->doit(skb, nlh, attrs);
24121da177e4SLinus Torvalds }
24131da177e4SLinus Torvalds 
2414cd40b7d3SDenis V. Lunev static void xfrm_netlink_rcv(struct sk_buff *skb)
24151da177e4SLinus Torvalds {
2416283bc9f3SFan Du 	struct net *net = sock_net(skb->sk);
2417283bc9f3SFan Du 
2418283bc9f3SFan Du 	mutex_lock(&net->xfrm.xfrm_cfg_mutex);
2419cd40b7d3SDenis V. Lunev 	netlink_rcv_skb(skb, &xfrm_user_rcv_msg);
2420283bc9f3SFan Du 	mutex_unlock(&net->xfrm.xfrm_cfg_mutex);
24211da177e4SLinus Torvalds }
24221da177e4SLinus Torvalds 
24237deb2264SThomas Graf static inline size_t xfrm_expire_msgsize(void)
24247deb2264SThomas Graf {
24256f26b61eSJamal Hadi Salim 	return NLMSG_ALIGN(sizeof(struct xfrm_user_expire))
24266f26b61eSJamal Hadi Salim 	       + nla_total_size(sizeof(struct xfrm_mark));
24277deb2264SThomas Graf }
24287deb2264SThomas Graf 
2429214e005bSDavid S. Miller static int build_expire(struct sk_buff *skb, struct xfrm_state *x, const struct km_event *c)
24301da177e4SLinus Torvalds {
24311da177e4SLinus Torvalds 	struct xfrm_user_expire *ue;
24321da177e4SLinus Torvalds 	struct nlmsghdr *nlh;
24331d1e34ddSDavid S. Miller 	int err;
24341da177e4SLinus Torvalds 
243515e47304SEric W. Biederman 	nlh = nlmsg_put(skb, c->portid, 0, XFRM_MSG_EXPIRE, sizeof(*ue), 0);
243679b8b7f4SThomas Graf 	if (nlh == NULL)
243779b8b7f4SThomas Graf 		return -EMSGSIZE;
24381da177e4SLinus Torvalds 
24397b67c857SThomas Graf 	ue = nlmsg_data(nlh);
24401da177e4SLinus Torvalds 	copy_to_user_state(x, &ue->state);
2441d51d081dSJamal Hadi Salim 	ue->hard = (c->data.hard != 0) ? 1 : 0;
24421da177e4SLinus Torvalds 
24431d1e34ddSDavid S. Miller 	err = xfrm_mark_put(skb, &x->mark);
24441d1e34ddSDavid S. Miller 	if (err)
24451d1e34ddSDavid S. Miller 		return err;
24466f26b61eSJamal Hadi Salim 
24479825069dSThomas Graf 	return nlmsg_end(skb, nlh);
24481da177e4SLinus Torvalds }
24491da177e4SLinus Torvalds 
2450214e005bSDavid S. Miller static int xfrm_exp_state_notify(struct xfrm_state *x, const struct km_event *c)
24511da177e4SLinus Torvalds {
2452fc34acd3SAlexey Dobriyan 	struct net *net = xs_net(x);
24531da177e4SLinus Torvalds 	struct sk_buff *skb;
24541da177e4SLinus Torvalds 
24557deb2264SThomas Graf 	skb = nlmsg_new(xfrm_expire_msgsize(), GFP_ATOMIC);
24561da177e4SLinus Torvalds 	if (skb == NULL)
24571da177e4SLinus Torvalds 		return -ENOMEM;
24581da177e4SLinus Torvalds 
24596f26b61eSJamal Hadi Salim 	if (build_expire(skb, x, c) < 0) {
24606f26b61eSJamal Hadi Salim 		kfree_skb(skb);
24616f26b61eSJamal Hadi Salim 		return -EMSGSIZE;
24626f26b61eSJamal Hadi Salim 	}
24631da177e4SLinus Torvalds 
2464a6483b79SAlexey Dobriyan 	return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_EXPIRE, GFP_ATOMIC);
24651da177e4SLinus Torvalds }
24661da177e4SLinus Torvalds 
2467214e005bSDavid S. Miller static int xfrm_aevent_state_notify(struct xfrm_state *x, const struct km_event *c)
2468d51d081dSJamal Hadi Salim {
2469fc34acd3SAlexey Dobriyan 	struct net *net = xs_net(x);
2470d51d081dSJamal Hadi Salim 	struct sk_buff *skb;
2471d51d081dSJamal Hadi Salim 
2472d8647b79SSteffen Klassert 	skb = nlmsg_new(xfrm_aevent_msgsize(x), GFP_ATOMIC);
2473d51d081dSJamal Hadi Salim 	if (skb == NULL)
2474d51d081dSJamal Hadi Salim 		return -ENOMEM;
2475d51d081dSJamal Hadi Salim 
2476d51d081dSJamal Hadi Salim 	if (build_aevent(skb, x, c) < 0)
2477d51d081dSJamal Hadi Salim 		BUG();
2478d51d081dSJamal Hadi Salim 
2479a6483b79SAlexey Dobriyan 	return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_AEVENTS, GFP_ATOMIC);
2480d51d081dSJamal Hadi Salim }
2481d51d081dSJamal Hadi Salim 
2482214e005bSDavid S. Miller static int xfrm_notify_sa_flush(const struct km_event *c)
248326b15dadSJamal Hadi Salim {
24847067802eSAlexey Dobriyan 	struct net *net = c->net;
248526b15dadSJamal Hadi Salim 	struct xfrm_usersa_flush *p;
248626b15dadSJamal Hadi Salim 	struct nlmsghdr *nlh;
248726b15dadSJamal Hadi Salim 	struct sk_buff *skb;
24887deb2264SThomas Graf 	int len = NLMSG_ALIGN(sizeof(struct xfrm_usersa_flush));
248926b15dadSJamal Hadi Salim 
24907deb2264SThomas Graf 	skb = nlmsg_new(len, GFP_ATOMIC);
249126b15dadSJamal Hadi Salim 	if (skb == NULL)
249226b15dadSJamal Hadi Salim 		return -ENOMEM;
249326b15dadSJamal Hadi Salim 
249415e47304SEric W. Biederman 	nlh = nlmsg_put(skb, c->portid, c->seq, XFRM_MSG_FLUSHSA, sizeof(*p), 0);
249579b8b7f4SThomas Graf 	if (nlh == NULL) {
249679b8b7f4SThomas Graf 		kfree_skb(skb);
249779b8b7f4SThomas Graf 		return -EMSGSIZE;
249879b8b7f4SThomas Graf 	}
249926b15dadSJamal Hadi Salim 
25007b67c857SThomas Graf 	p = nlmsg_data(nlh);
2501bf08867fSHerbert Xu 	p->proto = c->data.proto;
250226b15dadSJamal Hadi Salim 
25039825069dSThomas Graf 	nlmsg_end(skb, nlh);
250426b15dadSJamal Hadi Salim 
2505a6483b79SAlexey Dobriyan 	return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_SA, GFP_ATOMIC);
250626b15dadSJamal Hadi Salim }
250726b15dadSJamal Hadi Salim 
25087deb2264SThomas Graf static inline size_t xfrm_sa_len(struct xfrm_state *x)
250926b15dadSJamal Hadi Salim {
25107deb2264SThomas Graf 	size_t l = 0;
25111a6509d9SHerbert Xu 	if (x->aead)
25121a6509d9SHerbert Xu 		l += nla_total_size(aead_len(x->aead));
25134447bb33SMartin Willi 	if (x->aalg) {
25144447bb33SMartin Willi 		l += nla_total_size(sizeof(struct xfrm_algo) +
25154447bb33SMartin Willi 				    (x->aalg->alg_key_len + 7) / 8);
25164447bb33SMartin Willi 		l += nla_total_size(xfrm_alg_auth_len(x->aalg));
25174447bb33SMartin Willi 	}
251826b15dadSJamal Hadi Salim 	if (x->ealg)
25190f99be0dSEric Dumazet 		l += nla_total_size(xfrm_alg_len(x->ealg));
252026b15dadSJamal Hadi Salim 	if (x->calg)
25217deb2264SThomas Graf 		l += nla_total_size(sizeof(*x->calg));
252226b15dadSJamal Hadi Salim 	if (x->encap)
25237deb2264SThomas Graf 		l += nla_total_size(sizeof(*x->encap));
252435d2856bSMartin Willi 	if (x->tfcpad)
252535d2856bSMartin Willi 		l += nla_total_size(sizeof(x->tfcpad));
2526d8647b79SSteffen Klassert 	if (x->replay_esn)
2527d8647b79SSteffen Klassert 		l += nla_total_size(xfrm_replay_state_esn_len(x->replay_esn));
252868325d3bSHerbert Xu 	if (x->security)
252968325d3bSHerbert Xu 		l += nla_total_size(sizeof(struct xfrm_user_sec_ctx) +
253068325d3bSHerbert Xu 				    x->security->ctx_len);
253168325d3bSHerbert Xu 	if (x->coaddr)
253268325d3bSHerbert Xu 		l += nla_total_size(sizeof(*x->coaddr));
2533a947b0a9SNicolas Dichtel 	if (x->props.extra_flags)
2534a947b0a9SNicolas Dichtel 		l += nla_total_size(sizeof(x->props.extra_flags));
253568325d3bSHerbert Xu 
2536d26f3984SHerbert Xu 	/* Must count x->lastused as it may become non-zero behind our back. */
2537d26f3984SHerbert Xu 	l += nla_total_size(sizeof(u64));
253826b15dadSJamal Hadi Salim 
253926b15dadSJamal Hadi Salim 	return l;
254026b15dadSJamal Hadi Salim }
254126b15dadSJamal Hadi Salim 
2542214e005bSDavid S. Miller static int xfrm_notify_sa(struct xfrm_state *x, const struct km_event *c)
254326b15dadSJamal Hadi Salim {
2544fc34acd3SAlexey Dobriyan 	struct net *net = xs_net(x);
254526b15dadSJamal Hadi Salim 	struct xfrm_usersa_info *p;
25460603eac0SHerbert Xu 	struct xfrm_usersa_id *id;
254726b15dadSJamal Hadi Salim 	struct nlmsghdr *nlh;
254826b15dadSJamal Hadi Salim 	struct sk_buff *skb;
254926b15dadSJamal Hadi Salim 	int len = xfrm_sa_len(x);
25501d1e34ddSDavid S. Miller 	int headlen, err;
25510603eac0SHerbert Xu 
25520603eac0SHerbert Xu 	headlen = sizeof(*p);
25530603eac0SHerbert Xu 	if (c->event == XFRM_MSG_DELSA) {
25547deb2264SThomas Graf 		len += nla_total_size(headlen);
25550603eac0SHerbert Xu 		headlen = sizeof(*id);
25566f26b61eSJamal Hadi Salim 		len += nla_total_size(sizeof(struct xfrm_mark));
25570603eac0SHerbert Xu 	}
25587deb2264SThomas Graf 	len += NLMSG_ALIGN(headlen);
255926b15dadSJamal Hadi Salim 
25607deb2264SThomas Graf 	skb = nlmsg_new(len, GFP_ATOMIC);
256126b15dadSJamal Hadi Salim 	if (skb == NULL)
256226b15dadSJamal Hadi Salim 		return -ENOMEM;
256326b15dadSJamal Hadi Salim 
256415e47304SEric W. Biederman 	nlh = nlmsg_put(skb, c->portid, c->seq, c->event, headlen, 0);
25651d1e34ddSDavid S. Miller 	err = -EMSGSIZE;
256679b8b7f4SThomas Graf 	if (nlh == NULL)
25671d1e34ddSDavid S. Miller 		goto out_free_skb;
256826b15dadSJamal Hadi Salim 
25697b67c857SThomas Graf 	p = nlmsg_data(nlh);
25700603eac0SHerbert Xu 	if (c->event == XFRM_MSG_DELSA) {
2571c0144beaSThomas Graf 		struct nlattr *attr;
2572c0144beaSThomas Graf 
25737b67c857SThomas Graf 		id = nlmsg_data(nlh);
25740603eac0SHerbert Xu 		memcpy(&id->daddr, &x->id.daddr, sizeof(id->daddr));
25750603eac0SHerbert Xu 		id->spi = x->id.spi;
25760603eac0SHerbert Xu 		id->family = x->props.family;
25770603eac0SHerbert Xu 		id->proto = x->id.proto;
25780603eac0SHerbert Xu 
2579c0144beaSThomas Graf 		attr = nla_reserve(skb, XFRMA_SA, sizeof(*p));
25801d1e34ddSDavid S. Miller 		err = -EMSGSIZE;
2581c0144beaSThomas Graf 		if (attr == NULL)
25821d1e34ddSDavid S. Miller 			goto out_free_skb;
2583c0144beaSThomas Graf 
2584c0144beaSThomas Graf 		p = nla_data(attr);
25850603eac0SHerbert Xu 	}
25861d1e34ddSDavid S. Miller 	err = copy_to_user_state_extra(x, p, skb);
25871d1e34ddSDavid S. Miller 	if (err)
25881d1e34ddSDavid S. Miller 		goto out_free_skb;
258926b15dadSJamal Hadi Salim 
25909825069dSThomas Graf 	nlmsg_end(skb, nlh);
259126b15dadSJamal Hadi Salim 
2592a6483b79SAlexey Dobriyan 	return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_SA, GFP_ATOMIC);
259326b15dadSJamal Hadi Salim 
25941d1e34ddSDavid S. Miller out_free_skb:
259526b15dadSJamal Hadi Salim 	kfree_skb(skb);
25961d1e34ddSDavid S. Miller 	return err;
259726b15dadSJamal Hadi Salim }
259826b15dadSJamal Hadi Salim 
2599214e005bSDavid S. Miller static int xfrm_send_state_notify(struct xfrm_state *x, const struct km_event *c)
260026b15dadSJamal Hadi Salim {
260126b15dadSJamal Hadi Salim 
260226b15dadSJamal Hadi Salim 	switch (c->event) {
2603f60f6b8fSHerbert Xu 	case XFRM_MSG_EXPIRE:
260426b15dadSJamal Hadi Salim 		return xfrm_exp_state_notify(x, c);
2605d51d081dSJamal Hadi Salim 	case XFRM_MSG_NEWAE:
2606d51d081dSJamal Hadi Salim 		return xfrm_aevent_state_notify(x, c);
2607f60f6b8fSHerbert Xu 	case XFRM_MSG_DELSA:
2608f60f6b8fSHerbert Xu 	case XFRM_MSG_UPDSA:
2609f60f6b8fSHerbert Xu 	case XFRM_MSG_NEWSA:
261026b15dadSJamal Hadi Salim 		return xfrm_notify_sa(x, c);
2611f60f6b8fSHerbert Xu 	case XFRM_MSG_FLUSHSA:
261226b15dadSJamal Hadi Salim 		return xfrm_notify_sa_flush(c);
261326b15dadSJamal Hadi Salim 	default:
261462db5cfdSstephen hemminger 		printk(KERN_NOTICE "xfrm_user: Unknown SA event %d\n",
261562db5cfdSstephen hemminger 		       c->event);
261626b15dadSJamal Hadi Salim 		break;
261726b15dadSJamal Hadi Salim 	}
261826b15dadSJamal Hadi Salim 
261926b15dadSJamal Hadi Salim 	return 0;
262026b15dadSJamal Hadi Salim 
262126b15dadSJamal Hadi Salim }
262226b15dadSJamal Hadi Salim 
26237deb2264SThomas Graf static inline size_t xfrm_acquire_msgsize(struct xfrm_state *x,
26247deb2264SThomas Graf 					  struct xfrm_policy *xp)
26257deb2264SThomas Graf {
26267deb2264SThomas Graf 	return NLMSG_ALIGN(sizeof(struct xfrm_user_acquire))
26277deb2264SThomas Graf 	       + nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr)
26286f26b61eSJamal Hadi Salim 	       + nla_total_size(sizeof(struct xfrm_mark))
26297deb2264SThomas Graf 	       + nla_total_size(xfrm_user_sec_ctx_size(x->security))
26307deb2264SThomas Graf 	       + userpolicy_type_attrsize();
26317deb2264SThomas Graf }
26327deb2264SThomas Graf 
26331da177e4SLinus Torvalds static int build_acquire(struct sk_buff *skb, struct xfrm_state *x,
263465e0736bSFan Du 			 struct xfrm_tmpl *xt, struct xfrm_policy *xp)
26351da177e4SLinus Torvalds {
26361d1e34ddSDavid S. Miller 	__u32 seq = xfrm_get_acqseq();
26371da177e4SLinus Torvalds 	struct xfrm_user_acquire *ua;
26381da177e4SLinus Torvalds 	struct nlmsghdr *nlh;
26391d1e34ddSDavid S. Miller 	int err;
26401da177e4SLinus Torvalds 
264179b8b7f4SThomas Graf 	nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_ACQUIRE, sizeof(*ua), 0);
264279b8b7f4SThomas Graf 	if (nlh == NULL)
264379b8b7f4SThomas Graf 		return -EMSGSIZE;
26441da177e4SLinus Torvalds 
26457b67c857SThomas Graf 	ua = nlmsg_data(nlh);
26461da177e4SLinus Torvalds 	memcpy(&ua->id, &x->id, sizeof(ua->id));
26471da177e4SLinus Torvalds 	memcpy(&ua->saddr, &x->props.saddr, sizeof(ua->saddr));
26481da177e4SLinus Torvalds 	memcpy(&ua->sel, &x->sel, sizeof(ua->sel));
264965e0736bSFan Du 	copy_to_user_policy(xp, &ua->policy, XFRM_POLICY_OUT);
26501da177e4SLinus Torvalds 	ua->aalgos = xt->aalgos;
26511da177e4SLinus Torvalds 	ua->ealgos = xt->ealgos;
26521da177e4SLinus Torvalds 	ua->calgos = xt->calgos;
26531da177e4SLinus Torvalds 	ua->seq = x->km.seq = seq;
26541da177e4SLinus Torvalds 
26551d1e34ddSDavid S. Miller 	err = copy_to_user_tmpl(xp, skb);
26561d1e34ddSDavid S. Miller 	if (!err)
26571d1e34ddSDavid S. Miller 		err = copy_to_user_state_sec_ctx(x, skb);
26581d1e34ddSDavid S. Miller 	if (!err)
26591d1e34ddSDavid S. Miller 		err = copy_to_user_policy_type(xp->type, skb);
26601d1e34ddSDavid S. Miller 	if (!err)
26611d1e34ddSDavid S. Miller 		err = xfrm_mark_put(skb, &xp->mark);
26621d1e34ddSDavid S. Miller 	if (err) {
26631d1e34ddSDavid S. Miller 		nlmsg_cancel(skb, nlh);
26641d1e34ddSDavid S. Miller 		return err;
26651d1e34ddSDavid S. Miller 	}
26661da177e4SLinus Torvalds 
26679825069dSThomas Graf 	return nlmsg_end(skb, nlh);
26681da177e4SLinus Torvalds }
26691da177e4SLinus Torvalds 
26701da177e4SLinus Torvalds static int xfrm_send_acquire(struct xfrm_state *x, struct xfrm_tmpl *xt,
267165e0736bSFan Du 			     struct xfrm_policy *xp)
26721da177e4SLinus Torvalds {
2673a6483b79SAlexey Dobriyan 	struct net *net = xs_net(x);
26741da177e4SLinus Torvalds 	struct sk_buff *skb;
26751da177e4SLinus Torvalds 
26767deb2264SThomas Graf 	skb = nlmsg_new(xfrm_acquire_msgsize(x, xp), GFP_ATOMIC);
26771da177e4SLinus Torvalds 	if (skb == NULL)
26781da177e4SLinus Torvalds 		return -ENOMEM;
26791da177e4SLinus Torvalds 
268065e0736bSFan Du 	if (build_acquire(skb, x, xt, xp) < 0)
26811da177e4SLinus Torvalds 		BUG();
26821da177e4SLinus Torvalds 
2683a6483b79SAlexey Dobriyan 	return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_ACQUIRE, GFP_ATOMIC);
26841da177e4SLinus Torvalds }
26851da177e4SLinus Torvalds 
26861da177e4SLinus Torvalds /* User gives us xfrm_user_policy_info followed by an array of 0
26871da177e4SLinus Torvalds  * or more templates.
26881da177e4SLinus Torvalds  */
2689cb969f07SVenkat Yekkirala static struct xfrm_policy *xfrm_compile_policy(struct sock *sk, int opt,
26901da177e4SLinus Torvalds 					       u8 *data, int len, int *dir)
26911da177e4SLinus Torvalds {
2692fc34acd3SAlexey Dobriyan 	struct net *net = sock_net(sk);
26931da177e4SLinus Torvalds 	struct xfrm_userpolicy_info *p = (struct xfrm_userpolicy_info *)data;
26941da177e4SLinus Torvalds 	struct xfrm_user_tmpl *ut = (struct xfrm_user_tmpl *) (p + 1);
26951da177e4SLinus Torvalds 	struct xfrm_policy *xp;
26961da177e4SLinus Torvalds 	int nr;
26971da177e4SLinus Torvalds 
2698cb969f07SVenkat Yekkirala 	switch (sk->sk_family) {
26991da177e4SLinus Torvalds 	case AF_INET:
27001da177e4SLinus Torvalds 		if (opt != IP_XFRM_POLICY) {
27011da177e4SLinus Torvalds 			*dir = -EOPNOTSUPP;
27021da177e4SLinus Torvalds 			return NULL;
27031da177e4SLinus Torvalds 		}
27041da177e4SLinus Torvalds 		break;
2705dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6)
27061da177e4SLinus Torvalds 	case AF_INET6:
27071da177e4SLinus Torvalds 		if (opt != IPV6_XFRM_POLICY) {
27081da177e4SLinus Torvalds 			*dir = -EOPNOTSUPP;
27091da177e4SLinus Torvalds 			return NULL;
27101da177e4SLinus Torvalds 		}
27111da177e4SLinus Torvalds 		break;
27121da177e4SLinus Torvalds #endif
27131da177e4SLinus Torvalds 	default:
27141da177e4SLinus Torvalds 		*dir = -EINVAL;
27151da177e4SLinus Torvalds 		return NULL;
27161da177e4SLinus Torvalds 	}
27171da177e4SLinus Torvalds 
27181da177e4SLinus Torvalds 	*dir = -EINVAL;
27191da177e4SLinus Torvalds 
27201da177e4SLinus Torvalds 	if (len < sizeof(*p) ||
27211da177e4SLinus Torvalds 	    verify_newpolicy_info(p))
27221da177e4SLinus Torvalds 		return NULL;
27231da177e4SLinus Torvalds 
27241da177e4SLinus Torvalds 	nr = ((len - sizeof(*p)) / sizeof(*ut));
2725b4ad86bfSDavid S. Miller 	if (validate_tmpl(nr, ut, p->sel.family))
27261da177e4SLinus Torvalds 		return NULL;
27271da177e4SLinus Torvalds 
2728a4f1bac6SHerbert Xu 	if (p->dir > XFRM_POLICY_OUT)
2729a4f1bac6SHerbert Xu 		return NULL;
2730a4f1bac6SHerbert Xu 
27312f09a4d5SHerbert Xu 	xp = xfrm_policy_alloc(net, GFP_ATOMIC);
27321da177e4SLinus Torvalds 	if (xp == NULL) {
27331da177e4SLinus Torvalds 		*dir = -ENOBUFS;
27341da177e4SLinus Torvalds 		return NULL;
27351da177e4SLinus Torvalds 	}
27361da177e4SLinus Torvalds 
27371da177e4SLinus Torvalds 	copy_from_user_policy(xp, p);
2738f7b6983fSMasahide NAKAMURA 	xp->type = XFRM_POLICY_TYPE_MAIN;
27391da177e4SLinus Torvalds 	copy_templates(xp, ut, nr);
27401da177e4SLinus Torvalds 
27411da177e4SLinus Torvalds 	*dir = p->dir;
27421da177e4SLinus Torvalds 
27431da177e4SLinus Torvalds 	return xp;
27441da177e4SLinus Torvalds }
27451da177e4SLinus Torvalds 
27467deb2264SThomas Graf static inline size_t xfrm_polexpire_msgsize(struct xfrm_policy *xp)
27477deb2264SThomas Graf {
27487deb2264SThomas Graf 	return NLMSG_ALIGN(sizeof(struct xfrm_user_polexpire))
27497deb2264SThomas Graf 	       + nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr)
27507deb2264SThomas Graf 	       + nla_total_size(xfrm_user_sec_ctx_size(xp->security))
2751295fae56SJamal Hadi Salim 	       + nla_total_size(sizeof(struct xfrm_mark))
27527deb2264SThomas Graf 	       + userpolicy_type_attrsize();
27537deb2264SThomas Graf }
27547deb2264SThomas Graf 
27551da177e4SLinus Torvalds static int build_polexpire(struct sk_buff *skb, struct xfrm_policy *xp,
2756214e005bSDavid S. Miller 			   int dir, const struct km_event *c)
27571da177e4SLinus Torvalds {
27581da177e4SLinus Torvalds 	struct xfrm_user_polexpire *upe;
2759d51d081dSJamal Hadi Salim 	int hard = c->data.hard;
27601d1e34ddSDavid S. Miller 	struct nlmsghdr *nlh;
27611d1e34ddSDavid S. Miller 	int err;
27621da177e4SLinus Torvalds 
276315e47304SEric W. Biederman 	nlh = nlmsg_put(skb, c->portid, 0, XFRM_MSG_POLEXPIRE, sizeof(*upe), 0);
276479b8b7f4SThomas Graf 	if (nlh == NULL)
276579b8b7f4SThomas Graf 		return -EMSGSIZE;
27661da177e4SLinus Torvalds 
27677b67c857SThomas Graf 	upe = nlmsg_data(nlh);
27681da177e4SLinus Torvalds 	copy_to_user_policy(xp, &upe->pol, dir);
27691d1e34ddSDavid S. Miller 	err = copy_to_user_tmpl(xp, skb);
27701d1e34ddSDavid S. Miller 	if (!err)
27711d1e34ddSDavid S. Miller 		err = copy_to_user_sec_ctx(xp, skb);
27721d1e34ddSDavid S. Miller 	if (!err)
27731d1e34ddSDavid S. Miller 		err = copy_to_user_policy_type(xp->type, skb);
27741d1e34ddSDavid S. Miller 	if (!err)
27751d1e34ddSDavid S. Miller 		err = xfrm_mark_put(skb, &xp->mark);
27761d1e34ddSDavid S. Miller 	if (err) {
27771d1e34ddSDavid S. Miller 		nlmsg_cancel(skb, nlh);
27781d1e34ddSDavid S. Miller 		return err;
27791d1e34ddSDavid S. Miller 	}
27801da177e4SLinus Torvalds 	upe->hard = !!hard;
27811da177e4SLinus Torvalds 
27829825069dSThomas Graf 	return nlmsg_end(skb, nlh);
27831da177e4SLinus Torvalds }
27841da177e4SLinus Torvalds 
2785214e005bSDavid S. Miller static int xfrm_exp_policy_notify(struct xfrm_policy *xp, int dir, const struct km_event *c)
27861da177e4SLinus Torvalds {
2787fc34acd3SAlexey Dobriyan 	struct net *net = xp_net(xp);
27881da177e4SLinus Torvalds 	struct sk_buff *skb;
27891da177e4SLinus Torvalds 
27907deb2264SThomas Graf 	skb = nlmsg_new(xfrm_polexpire_msgsize(xp), GFP_ATOMIC);
27911da177e4SLinus Torvalds 	if (skb == NULL)
27921da177e4SLinus Torvalds 		return -ENOMEM;
27931da177e4SLinus Torvalds 
2794d51d081dSJamal Hadi Salim 	if (build_polexpire(skb, xp, dir, c) < 0)
27951da177e4SLinus Torvalds 		BUG();
27961da177e4SLinus Torvalds 
2797a6483b79SAlexey Dobriyan 	return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_EXPIRE, GFP_ATOMIC);
27981da177e4SLinus Torvalds }
27991da177e4SLinus Torvalds 
2800214e005bSDavid S. Miller static int xfrm_notify_policy(struct xfrm_policy *xp, int dir, const struct km_event *c)
280126b15dadSJamal Hadi Salim {
28021d1e34ddSDavid S. Miller 	int len = nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr);
2803fc34acd3SAlexey Dobriyan 	struct net *net = xp_net(xp);
280426b15dadSJamal Hadi Salim 	struct xfrm_userpolicy_info *p;
28050603eac0SHerbert Xu 	struct xfrm_userpolicy_id *id;
280626b15dadSJamal Hadi Salim 	struct nlmsghdr *nlh;
280726b15dadSJamal Hadi Salim 	struct sk_buff *skb;
28081d1e34ddSDavid S. Miller 	int headlen, err;
28090603eac0SHerbert Xu 
28100603eac0SHerbert Xu 	headlen = sizeof(*p);
28110603eac0SHerbert Xu 	if (c->event == XFRM_MSG_DELPOLICY) {
28127deb2264SThomas Graf 		len += nla_total_size(headlen);
28130603eac0SHerbert Xu 		headlen = sizeof(*id);
28140603eac0SHerbert Xu 	}
2815cfbfd45aSThomas Graf 	len += userpolicy_type_attrsize();
2816295fae56SJamal Hadi Salim 	len += nla_total_size(sizeof(struct xfrm_mark));
28177deb2264SThomas Graf 	len += NLMSG_ALIGN(headlen);
281826b15dadSJamal Hadi Salim 
28197deb2264SThomas Graf 	skb = nlmsg_new(len, GFP_ATOMIC);
282026b15dadSJamal Hadi Salim 	if (skb == NULL)
282126b15dadSJamal Hadi Salim 		return -ENOMEM;
282226b15dadSJamal Hadi Salim 
282315e47304SEric W. Biederman 	nlh = nlmsg_put(skb, c->portid, c->seq, c->event, headlen, 0);
28241d1e34ddSDavid S. Miller 	err = -EMSGSIZE;
282579b8b7f4SThomas Graf 	if (nlh == NULL)
28261d1e34ddSDavid S. Miller 		goto out_free_skb;
282726b15dadSJamal Hadi Salim 
28287b67c857SThomas Graf 	p = nlmsg_data(nlh);
28290603eac0SHerbert Xu 	if (c->event == XFRM_MSG_DELPOLICY) {
2830c0144beaSThomas Graf 		struct nlattr *attr;
2831c0144beaSThomas Graf 
28327b67c857SThomas Graf 		id = nlmsg_data(nlh);
28330603eac0SHerbert Xu 		memset(id, 0, sizeof(*id));
28340603eac0SHerbert Xu 		id->dir = dir;
28350603eac0SHerbert Xu 		if (c->data.byid)
28360603eac0SHerbert Xu 			id->index = xp->index;
28370603eac0SHerbert Xu 		else
28380603eac0SHerbert Xu 			memcpy(&id->sel, &xp->selector, sizeof(id->sel));
28390603eac0SHerbert Xu 
2840c0144beaSThomas Graf 		attr = nla_reserve(skb, XFRMA_POLICY, sizeof(*p));
28411d1e34ddSDavid S. Miller 		err = -EMSGSIZE;
2842c0144beaSThomas Graf 		if (attr == NULL)
28431d1e34ddSDavid S. Miller 			goto out_free_skb;
2844c0144beaSThomas Graf 
2845c0144beaSThomas Graf 		p = nla_data(attr);
28460603eac0SHerbert Xu 	}
284726b15dadSJamal Hadi Salim 
284826b15dadSJamal Hadi Salim 	copy_to_user_policy(xp, p, dir);
28491d1e34ddSDavid S. Miller 	err = copy_to_user_tmpl(xp, skb);
28501d1e34ddSDavid S. Miller 	if (!err)
28511d1e34ddSDavid S. Miller 		err = copy_to_user_policy_type(xp->type, skb);
28521d1e34ddSDavid S. Miller 	if (!err)
28531d1e34ddSDavid S. Miller 		err = xfrm_mark_put(skb, &xp->mark);
28541d1e34ddSDavid S. Miller 	if (err)
28551d1e34ddSDavid S. Miller 		goto out_free_skb;
2856295fae56SJamal Hadi Salim 
28579825069dSThomas Graf 	nlmsg_end(skb, nlh);
285826b15dadSJamal Hadi Salim 
2859a6483b79SAlexey Dobriyan 	return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_POLICY, GFP_ATOMIC);
286026b15dadSJamal Hadi Salim 
28611d1e34ddSDavid S. Miller out_free_skb:
286226b15dadSJamal Hadi Salim 	kfree_skb(skb);
28631d1e34ddSDavid S. Miller 	return err;
286426b15dadSJamal Hadi Salim }
286526b15dadSJamal Hadi Salim 
2866214e005bSDavid S. Miller static int xfrm_notify_policy_flush(const struct km_event *c)
286726b15dadSJamal Hadi Salim {
28687067802eSAlexey Dobriyan 	struct net *net = c->net;
286926b15dadSJamal Hadi Salim 	struct nlmsghdr *nlh;
287026b15dadSJamal Hadi Salim 	struct sk_buff *skb;
28711d1e34ddSDavid S. Miller 	int err;
287226b15dadSJamal Hadi Salim 
28737deb2264SThomas Graf 	skb = nlmsg_new(userpolicy_type_attrsize(), GFP_ATOMIC);
287426b15dadSJamal Hadi Salim 	if (skb == NULL)
287526b15dadSJamal Hadi Salim 		return -ENOMEM;
287626b15dadSJamal Hadi Salim 
287715e47304SEric W. Biederman 	nlh = nlmsg_put(skb, c->portid, c->seq, XFRM_MSG_FLUSHPOLICY, 0, 0);
28781d1e34ddSDavid S. Miller 	err = -EMSGSIZE;
287979b8b7f4SThomas Graf 	if (nlh == NULL)
28801d1e34ddSDavid S. Miller 		goto out_free_skb;
28811d1e34ddSDavid S. Miller 	err = copy_to_user_policy_type(c->data.type, skb);
28821d1e34ddSDavid S. Miller 	if (err)
28831d1e34ddSDavid S. Miller 		goto out_free_skb;
288426b15dadSJamal Hadi Salim 
28859825069dSThomas Graf 	nlmsg_end(skb, nlh);
288626b15dadSJamal Hadi Salim 
2887a6483b79SAlexey Dobriyan 	return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_POLICY, GFP_ATOMIC);
288826b15dadSJamal Hadi Salim 
28891d1e34ddSDavid S. Miller out_free_skb:
289026b15dadSJamal Hadi Salim 	kfree_skb(skb);
28911d1e34ddSDavid S. Miller 	return err;
289226b15dadSJamal Hadi Salim }
289326b15dadSJamal Hadi Salim 
2894214e005bSDavid S. Miller static int xfrm_send_policy_notify(struct xfrm_policy *xp, int dir, const struct km_event *c)
289526b15dadSJamal Hadi Salim {
289626b15dadSJamal Hadi Salim 
289726b15dadSJamal Hadi Salim 	switch (c->event) {
2898f60f6b8fSHerbert Xu 	case XFRM_MSG_NEWPOLICY:
2899f60f6b8fSHerbert Xu 	case XFRM_MSG_UPDPOLICY:
2900f60f6b8fSHerbert Xu 	case XFRM_MSG_DELPOLICY:
290126b15dadSJamal Hadi Salim 		return xfrm_notify_policy(xp, dir, c);
2902f60f6b8fSHerbert Xu 	case XFRM_MSG_FLUSHPOLICY:
290326b15dadSJamal Hadi Salim 		return xfrm_notify_policy_flush(c);
2904f60f6b8fSHerbert Xu 	case XFRM_MSG_POLEXPIRE:
290526b15dadSJamal Hadi Salim 		return xfrm_exp_policy_notify(xp, dir, c);
290626b15dadSJamal Hadi Salim 	default:
290762db5cfdSstephen hemminger 		printk(KERN_NOTICE "xfrm_user: Unknown Policy event %d\n",
290862db5cfdSstephen hemminger 		       c->event);
290926b15dadSJamal Hadi Salim 	}
291026b15dadSJamal Hadi Salim 
291126b15dadSJamal Hadi Salim 	return 0;
291226b15dadSJamal Hadi Salim 
291326b15dadSJamal Hadi Salim }
291426b15dadSJamal Hadi Salim 
29157deb2264SThomas Graf static inline size_t xfrm_report_msgsize(void)
29167deb2264SThomas Graf {
29177deb2264SThomas Graf 	return NLMSG_ALIGN(sizeof(struct xfrm_user_report));
29187deb2264SThomas Graf }
29197deb2264SThomas Graf 
292097a64b45SMasahide NAKAMURA static int build_report(struct sk_buff *skb, u8 proto,
292197a64b45SMasahide NAKAMURA 			struct xfrm_selector *sel, xfrm_address_t *addr)
292297a64b45SMasahide NAKAMURA {
292397a64b45SMasahide NAKAMURA 	struct xfrm_user_report *ur;
292497a64b45SMasahide NAKAMURA 	struct nlmsghdr *nlh;
292597a64b45SMasahide NAKAMURA 
292679b8b7f4SThomas Graf 	nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_REPORT, sizeof(*ur), 0);
292779b8b7f4SThomas Graf 	if (nlh == NULL)
292879b8b7f4SThomas Graf 		return -EMSGSIZE;
292997a64b45SMasahide NAKAMURA 
29307b67c857SThomas Graf 	ur = nlmsg_data(nlh);
293197a64b45SMasahide NAKAMURA 	ur->proto = proto;
293297a64b45SMasahide NAKAMURA 	memcpy(&ur->sel, sel, sizeof(ur->sel));
293397a64b45SMasahide NAKAMURA 
29341d1e34ddSDavid S. Miller 	if (addr) {
29351d1e34ddSDavid S. Miller 		int err = nla_put(skb, XFRMA_COADDR, sizeof(*addr), addr);
29361d1e34ddSDavid S. Miller 		if (err) {
29379825069dSThomas Graf 			nlmsg_cancel(skb, nlh);
29381d1e34ddSDavid S. Miller 			return err;
29391d1e34ddSDavid S. Miller 		}
29401d1e34ddSDavid S. Miller 	}
29411d1e34ddSDavid S. Miller 	return nlmsg_end(skb, nlh);
294297a64b45SMasahide NAKAMURA }
294397a64b45SMasahide NAKAMURA 
2944db983c11SAlexey Dobriyan static int xfrm_send_report(struct net *net, u8 proto,
2945db983c11SAlexey Dobriyan 			    struct xfrm_selector *sel, xfrm_address_t *addr)
294697a64b45SMasahide NAKAMURA {
294797a64b45SMasahide NAKAMURA 	struct sk_buff *skb;
294897a64b45SMasahide NAKAMURA 
29497deb2264SThomas Graf 	skb = nlmsg_new(xfrm_report_msgsize(), GFP_ATOMIC);
295097a64b45SMasahide NAKAMURA 	if (skb == NULL)
295197a64b45SMasahide NAKAMURA 		return -ENOMEM;
295297a64b45SMasahide NAKAMURA 
295397a64b45SMasahide NAKAMURA 	if (build_report(skb, proto, sel, addr) < 0)
295497a64b45SMasahide NAKAMURA 		BUG();
295597a64b45SMasahide NAKAMURA 
2956a6483b79SAlexey Dobriyan 	return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_REPORT, GFP_ATOMIC);
295797a64b45SMasahide NAKAMURA }
295897a64b45SMasahide NAKAMURA 
29593a2dfbe8SMartin Willi static inline size_t xfrm_mapping_msgsize(void)
29603a2dfbe8SMartin Willi {
29613a2dfbe8SMartin Willi 	return NLMSG_ALIGN(sizeof(struct xfrm_user_mapping));
29623a2dfbe8SMartin Willi }
29633a2dfbe8SMartin Willi 
29643a2dfbe8SMartin Willi static int build_mapping(struct sk_buff *skb, struct xfrm_state *x,
29653a2dfbe8SMartin Willi 			 xfrm_address_t *new_saddr, __be16 new_sport)
29663a2dfbe8SMartin Willi {
29673a2dfbe8SMartin Willi 	struct xfrm_user_mapping *um;
29683a2dfbe8SMartin Willi 	struct nlmsghdr *nlh;
29693a2dfbe8SMartin Willi 
29703a2dfbe8SMartin Willi 	nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_MAPPING, sizeof(*um), 0);
29713a2dfbe8SMartin Willi 	if (nlh == NULL)
29723a2dfbe8SMartin Willi 		return -EMSGSIZE;
29733a2dfbe8SMartin Willi 
29743a2dfbe8SMartin Willi 	um = nlmsg_data(nlh);
29753a2dfbe8SMartin Willi 
29763a2dfbe8SMartin Willi 	memcpy(&um->id.daddr, &x->id.daddr, sizeof(um->id.daddr));
29773a2dfbe8SMartin Willi 	um->id.spi = x->id.spi;
29783a2dfbe8SMartin Willi 	um->id.family = x->props.family;
29793a2dfbe8SMartin Willi 	um->id.proto = x->id.proto;
29803a2dfbe8SMartin Willi 	memcpy(&um->new_saddr, new_saddr, sizeof(um->new_saddr));
29813a2dfbe8SMartin Willi 	memcpy(&um->old_saddr, &x->props.saddr, sizeof(um->old_saddr));
29823a2dfbe8SMartin Willi 	um->new_sport = new_sport;
29833a2dfbe8SMartin Willi 	um->old_sport = x->encap->encap_sport;
29843a2dfbe8SMartin Willi 	um->reqid = x->props.reqid;
29853a2dfbe8SMartin Willi 
29863a2dfbe8SMartin Willi 	return nlmsg_end(skb, nlh);
29873a2dfbe8SMartin Willi }
29883a2dfbe8SMartin Willi 
29893a2dfbe8SMartin Willi static int xfrm_send_mapping(struct xfrm_state *x, xfrm_address_t *ipaddr,
29903a2dfbe8SMartin Willi 			     __be16 sport)
29913a2dfbe8SMartin Willi {
2992a6483b79SAlexey Dobriyan 	struct net *net = xs_net(x);
29933a2dfbe8SMartin Willi 	struct sk_buff *skb;
29943a2dfbe8SMartin Willi 
29953a2dfbe8SMartin Willi 	if (x->id.proto != IPPROTO_ESP)
29963a2dfbe8SMartin Willi 		return -EINVAL;
29973a2dfbe8SMartin Willi 
29983a2dfbe8SMartin Willi 	if (!x->encap)
29993a2dfbe8SMartin Willi 		return -EINVAL;
30003a2dfbe8SMartin Willi 
30013a2dfbe8SMartin Willi 	skb = nlmsg_new(xfrm_mapping_msgsize(), GFP_ATOMIC);
30023a2dfbe8SMartin Willi 	if (skb == NULL)
30033a2dfbe8SMartin Willi 		return -ENOMEM;
30043a2dfbe8SMartin Willi 
30053a2dfbe8SMartin Willi 	if (build_mapping(skb, x, ipaddr, sport) < 0)
30063a2dfbe8SMartin Willi 		BUG();
30073a2dfbe8SMartin Willi 
3008a6483b79SAlexey Dobriyan 	return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_MAPPING, GFP_ATOMIC);
30093a2dfbe8SMartin Willi }
30103a2dfbe8SMartin Willi 
30110f24558eSHoria Geanta static bool xfrm_is_alive(const struct km_event *c)
30120f24558eSHoria Geanta {
30130f24558eSHoria Geanta 	return (bool)xfrm_acquire_is_on(c->net);
30140f24558eSHoria Geanta }
30150f24558eSHoria Geanta 
30161da177e4SLinus Torvalds static struct xfrm_mgr netlink_mgr = {
30171da177e4SLinus Torvalds 	.id		= "netlink",
30181da177e4SLinus Torvalds 	.notify		= xfrm_send_state_notify,
30191da177e4SLinus Torvalds 	.acquire	= xfrm_send_acquire,
30201da177e4SLinus Torvalds 	.compile_policy	= xfrm_compile_policy,
30211da177e4SLinus Torvalds 	.notify_policy	= xfrm_send_policy_notify,
302297a64b45SMasahide NAKAMURA 	.report		= xfrm_send_report,
30235c79de6eSShinta Sugimoto 	.migrate	= xfrm_send_migrate,
30243a2dfbe8SMartin Willi 	.new_mapping	= xfrm_send_mapping,
30250f24558eSHoria Geanta 	.is_alive	= xfrm_is_alive,
30261da177e4SLinus Torvalds };
30271da177e4SLinus Torvalds 
3028a6483b79SAlexey Dobriyan static int __net_init xfrm_user_net_init(struct net *net)
30291da177e4SLinus Torvalds {
3030be33690dSPatrick McHardy 	struct sock *nlsk;
3031a31f2d17SPablo Neira Ayuso 	struct netlink_kernel_cfg cfg = {
3032a31f2d17SPablo Neira Ayuso 		.groups	= XFRMNLGRP_MAX,
3033a31f2d17SPablo Neira Ayuso 		.input	= xfrm_netlink_rcv,
3034a31f2d17SPablo Neira Ayuso 	};
3035be33690dSPatrick McHardy 
30369f00d977SPablo Neira Ayuso 	nlsk = netlink_kernel_create(net, NETLINK_XFRM, &cfg);
3037be33690dSPatrick McHardy 	if (nlsk == NULL)
30381da177e4SLinus Torvalds 		return -ENOMEM;
3039d79d792eSEric W. Biederman 	net->xfrm.nlsk_stash = nlsk; /* Don't set to NULL */
3040cf778b00SEric Dumazet 	rcu_assign_pointer(net->xfrm.nlsk, nlsk);
30411da177e4SLinus Torvalds 	return 0;
30421da177e4SLinus Torvalds }
30431da177e4SLinus Torvalds 
3044d79d792eSEric W. Biederman static void __net_exit xfrm_user_net_exit(struct list_head *net_exit_list)
3045a6483b79SAlexey Dobriyan {
3046d79d792eSEric W. Biederman 	struct net *net;
3047d79d792eSEric W. Biederman 	list_for_each_entry(net, net_exit_list, exit_list)
3048a9b3cd7fSStephen Hemminger 		RCU_INIT_POINTER(net->xfrm.nlsk, NULL);
3049d79d792eSEric W. Biederman 	synchronize_net();
3050d79d792eSEric W. Biederman 	list_for_each_entry(net, net_exit_list, exit_list)
3051d79d792eSEric W. Biederman 		netlink_kernel_release(net->xfrm.nlsk_stash);
3052a6483b79SAlexey Dobriyan }
3053a6483b79SAlexey Dobriyan 
3054a6483b79SAlexey Dobriyan static struct pernet_operations xfrm_user_net_ops = {
3055a6483b79SAlexey Dobriyan 	.init	    = xfrm_user_net_init,
3056d79d792eSEric W. Biederman 	.exit_batch = xfrm_user_net_exit,
3057a6483b79SAlexey Dobriyan };
3058a6483b79SAlexey Dobriyan 
3059a6483b79SAlexey Dobriyan static int __init xfrm_user_init(void)
3060a6483b79SAlexey Dobriyan {
3061a6483b79SAlexey Dobriyan 	int rv;
3062a6483b79SAlexey Dobriyan 
3063a6483b79SAlexey Dobriyan 	printk(KERN_INFO "Initializing XFRM netlink socket\n");
3064a6483b79SAlexey Dobriyan 
3065a6483b79SAlexey Dobriyan 	rv = register_pernet_subsys(&xfrm_user_net_ops);
3066a6483b79SAlexey Dobriyan 	if (rv < 0)
3067a6483b79SAlexey Dobriyan 		return rv;
3068a6483b79SAlexey Dobriyan 	rv = xfrm_register_km(&netlink_mgr);
3069a6483b79SAlexey Dobriyan 	if (rv < 0)
3070a6483b79SAlexey Dobriyan 		unregister_pernet_subsys(&xfrm_user_net_ops);
3071a6483b79SAlexey Dobriyan 	return rv;
3072a6483b79SAlexey Dobriyan }
3073a6483b79SAlexey Dobriyan 
30741da177e4SLinus Torvalds static void __exit xfrm_user_exit(void)
30751da177e4SLinus Torvalds {
30761da177e4SLinus Torvalds 	xfrm_unregister_km(&netlink_mgr);
3077a6483b79SAlexey Dobriyan 	unregister_pernet_subsys(&xfrm_user_net_ops);
30781da177e4SLinus Torvalds }
30791da177e4SLinus Torvalds 
30801da177e4SLinus Torvalds module_init(xfrm_user_init);
30811da177e4SLinus Torvalds module_exit(xfrm_user_exit);
30821da177e4SLinus Torvalds MODULE_LICENSE("GPL");
30834fdb3bb7SHarald Welte MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK, NETLINK_XFRM);
3084f8cd5488SJamal Hadi Salim 
3085