xref: /linux/net/xfrm/xfrm_user.c (revision 8d549c4f5d92d80fc6f888fd314e10972ae0ec37)
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 
14502aadf72SSteffen Klassert 	if (p->id.proto != IPPROTO_ESP)
14602aadf72SSteffen Klassert 		return -EINVAL;
14702aadf72SSteffen Klassert 
148d8647b79SSteffen Klassert 	if (p->replay_window != 0)
149d8647b79SSteffen Klassert 		return -EINVAL;
150d8647b79SSteffen Klassert 
151d8647b79SSteffen Klassert 	return 0;
152d8647b79SSteffen Klassert }
153df71837dSTrent Jaeger 
1541da177e4SLinus Torvalds static int verify_newsa_info(struct xfrm_usersa_info *p,
1555424f32eSThomas Graf 			     struct nlattr **attrs)
1561da177e4SLinus Torvalds {
1571da177e4SLinus Torvalds 	int err;
1581da177e4SLinus Torvalds 
1591da177e4SLinus Torvalds 	err = -EINVAL;
1601da177e4SLinus Torvalds 	switch (p->family) {
1611da177e4SLinus Torvalds 	case AF_INET:
1621da177e4SLinus Torvalds 		break;
1631da177e4SLinus Torvalds 
1641da177e4SLinus Torvalds 	case AF_INET6:
165dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6)
1661da177e4SLinus Torvalds 		break;
1671da177e4SLinus Torvalds #else
1681da177e4SLinus Torvalds 		err = -EAFNOSUPPORT;
1691da177e4SLinus Torvalds 		goto out;
1701da177e4SLinus Torvalds #endif
1711da177e4SLinus Torvalds 
1721da177e4SLinus Torvalds 	default:
1731da177e4SLinus Torvalds 		goto out;
1743ff50b79SStephen Hemminger 	}
1751da177e4SLinus Torvalds 
1761da177e4SLinus Torvalds 	err = -EINVAL;
1771da177e4SLinus Torvalds 	switch (p->id.proto) {
1781da177e4SLinus Torvalds 	case IPPROTO_AH:
1794447bb33SMartin Willi 		if ((!attrs[XFRMA_ALG_AUTH]	&&
1804447bb33SMartin Willi 		     !attrs[XFRMA_ALG_AUTH_TRUNC]) ||
1811a6509d9SHerbert Xu 		    attrs[XFRMA_ALG_AEAD]	||
18235a7aa08SThomas Graf 		    attrs[XFRMA_ALG_CRYPT]	||
18335d2856bSMartin Willi 		    attrs[XFRMA_ALG_COMP]	||
18435d2856bSMartin Willi 		    attrs[XFRMA_TFCPAD])
1851da177e4SLinus Torvalds 			goto out;
1861da177e4SLinus Torvalds 		break;
1871da177e4SLinus Torvalds 
1881da177e4SLinus Torvalds 	case IPPROTO_ESP:
1891a6509d9SHerbert Xu 		if (attrs[XFRMA_ALG_COMP])
1901a6509d9SHerbert Xu 			goto out;
1911a6509d9SHerbert Xu 		if (!attrs[XFRMA_ALG_AUTH] &&
1924447bb33SMartin Willi 		    !attrs[XFRMA_ALG_AUTH_TRUNC] &&
1931a6509d9SHerbert Xu 		    !attrs[XFRMA_ALG_CRYPT] &&
1941a6509d9SHerbert Xu 		    !attrs[XFRMA_ALG_AEAD])
1951a6509d9SHerbert Xu 			goto out;
1961a6509d9SHerbert Xu 		if ((attrs[XFRMA_ALG_AUTH] ||
1974447bb33SMartin Willi 		     attrs[XFRMA_ALG_AUTH_TRUNC] ||
1981a6509d9SHerbert Xu 		     attrs[XFRMA_ALG_CRYPT]) &&
1991a6509d9SHerbert Xu 		    attrs[XFRMA_ALG_AEAD])
2001da177e4SLinus Torvalds 			goto out;
20135d2856bSMartin Willi 		if (attrs[XFRMA_TFCPAD] &&
20235d2856bSMartin Willi 		    p->mode != XFRM_MODE_TUNNEL)
20335d2856bSMartin Willi 			goto out;
2041da177e4SLinus Torvalds 		break;
2051da177e4SLinus Torvalds 
2061da177e4SLinus Torvalds 	case IPPROTO_COMP:
20735a7aa08SThomas Graf 		if (!attrs[XFRMA_ALG_COMP]	||
2081a6509d9SHerbert Xu 		    attrs[XFRMA_ALG_AEAD]	||
20935a7aa08SThomas Graf 		    attrs[XFRMA_ALG_AUTH]	||
2104447bb33SMartin Willi 		    attrs[XFRMA_ALG_AUTH_TRUNC]	||
21135d2856bSMartin Willi 		    attrs[XFRMA_ALG_CRYPT]	||
21235d2856bSMartin Willi 		    attrs[XFRMA_TFCPAD])
2131da177e4SLinus Torvalds 			goto out;
2141da177e4SLinus Torvalds 		break;
2151da177e4SLinus Torvalds 
216dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6)
217e23c7194SMasahide NAKAMURA 	case IPPROTO_DSTOPTS:
218e23c7194SMasahide NAKAMURA 	case IPPROTO_ROUTING:
21935a7aa08SThomas Graf 		if (attrs[XFRMA_ALG_COMP]	||
22035a7aa08SThomas Graf 		    attrs[XFRMA_ALG_AUTH]	||
2214447bb33SMartin Willi 		    attrs[XFRMA_ALG_AUTH_TRUNC]	||
2221a6509d9SHerbert Xu 		    attrs[XFRMA_ALG_AEAD]	||
22335a7aa08SThomas Graf 		    attrs[XFRMA_ALG_CRYPT]	||
22435a7aa08SThomas Graf 		    attrs[XFRMA_ENCAP]		||
22535a7aa08SThomas Graf 		    attrs[XFRMA_SEC_CTX]	||
22635d2856bSMartin Willi 		    attrs[XFRMA_TFCPAD]		||
22735a7aa08SThomas Graf 		    !attrs[XFRMA_COADDR])
228e23c7194SMasahide NAKAMURA 			goto out;
229e23c7194SMasahide NAKAMURA 		break;
230e23c7194SMasahide NAKAMURA #endif
231e23c7194SMasahide NAKAMURA 
2321da177e4SLinus Torvalds 	default:
2331da177e4SLinus Torvalds 		goto out;
2343ff50b79SStephen Hemminger 	}
2351da177e4SLinus Torvalds 
2361a6509d9SHerbert Xu 	if ((err = verify_aead(attrs)))
2371a6509d9SHerbert Xu 		goto out;
2384447bb33SMartin Willi 	if ((err = verify_auth_trunc(attrs)))
2394447bb33SMartin Willi 		goto out;
24035a7aa08SThomas Graf 	if ((err = verify_one_alg(attrs, XFRMA_ALG_AUTH)))
2411da177e4SLinus Torvalds 		goto out;
24235a7aa08SThomas Graf 	if ((err = verify_one_alg(attrs, XFRMA_ALG_CRYPT)))
2431da177e4SLinus Torvalds 		goto out;
24435a7aa08SThomas Graf 	if ((err = verify_one_alg(attrs, XFRMA_ALG_COMP)))
2451da177e4SLinus Torvalds 		goto out;
24635a7aa08SThomas Graf 	if ((err = verify_sec_ctx_len(attrs)))
247df71837dSTrent Jaeger 		goto out;
248d8647b79SSteffen Klassert 	if ((err = verify_replay(p, attrs)))
249d8647b79SSteffen Klassert 		goto out;
2501da177e4SLinus Torvalds 
2511da177e4SLinus Torvalds 	err = -EINVAL;
2521da177e4SLinus Torvalds 	switch (p->mode) {
2537e49e6deSMasahide NAKAMURA 	case XFRM_MODE_TRANSPORT:
2547e49e6deSMasahide NAKAMURA 	case XFRM_MODE_TUNNEL:
255060f02a3SNoriaki TAKAMIYA 	case XFRM_MODE_ROUTEOPTIMIZATION:
2560a69452cSDiego Beltrami 	case XFRM_MODE_BEET:
2571da177e4SLinus Torvalds 		break;
2581da177e4SLinus Torvalds 
2591da177e4SLinus Torvalds 	default:
2601da177e4SLinus Torvalds 		goto out;
2613ff50b79SStephen Hemminger 	}
2621da177e4SLinus Torvalds 
2631da177e4SLinus Torvalds 	err = 0;
2641da177e4SLinus Torvalds 
2651da177e4SLinus Torvalds out:
2661da177e4SLinus Torvalds 	return err;
2671da177e4SLinus Torvalds }
2681da177e4SLinus Torvalds 
2691da177e4SLinus Torvalds static int attach_one_algo(struct xfrm_algo **algpp, u8 *props,
2706f2f19edSDavid S. Miller 			   struct xfrm_algo_desc *(*get_byname)(const char *, int),
2715424f32eSThomas Graf 			   struct nlattr *rta)
2721da177e4SLinus Torvalds {
2731da177e4SLinus Torvalds 	struct xfrm_algo *p, *ualg;
2741da177e4SLinus Torvalds 	struct xfrm_algo_desc *algo;
2751da177e4SLinus Torvalds 
2761da177e4SLinus Torvalds 	if (!rta)
2771da177e4SLinus Torvalds 		return 0;
2781da177e4SLinus Torvalds 
2795424f32eSThomas Graf 	ualg = nla_data(rta);
2801da177e4SLinus Torvalds 
2811da177e4SLinus Torvalds 	algo = get_byname(ualg->alg_name, 1);
2821da177e4SLinus Torvalds 	if (!algo)
2831da177e4SLinus Torvalds 		return -ENOSYS;
2841da177e4SLinus Torvalds 	*props = algo->desc.sadb_alg_id;
2851da177e4SLinus Torvalds 
2860f99be0dSEric Dumazet 	p = kmemdup(ualg, xfrm_alg_len(ualg), GFP_KERNEL);
2871da177e4SLinus Torvalds 	if (!p)
2881da177e4SLinus Torvalds 		return -ENOMEM;
2891da177e4SLinus Torvalds 
29004ff1260SHerbert Xu 	strcpy(p->alg_name, algo->name);
2911da177e4SLinus Torvalds 	*algpp = p;
2921da177e4SLinus Torvalds 	return 0;
2931da177e4SLinus Torvalds }
2941da177e4SLinus Torvalds 
2954447bb33SMartin Willi static int attach_auth(struct xfrm_algo_auth **algpp, u8 *props,
2964447bb33SMartin Willi 		       struct nlattr *rta)
2974447bb33SMartin Willi {
2984447bb33SMartin Willi 	struct xfrm_algo *ualg;
2994447bb33SMartin Willi 	struct xfrm_algo_auth *p;
3004447bb33SMartin Willi 	struct xfrm_algo_desc *algo;
3014447bb33SMartin Willi 
3024447bb33SMartin Willi 	if (!rta)
3034447bb33SMartin Willi 		return 0;
3044447bb33SMartin Willi 
3054447bb33SMartin Willi 	ualg = nla_data(rta);
3064447bb33SMartin Willi 
3074447bb33SMartin Willi 	algo = xfrm_aalg_get_byname(ualg->alg_name, 1);
3084447bb33SMartin Willi 	if (!algo)
3094447bb33SMartin Willi 		return -ENOSYS;
3104447bb33SMartin Willi 	*props = algo->desc.sadb_alg_id;
3114447bb33SMartin Willi 
3124447bb33SMartin Willi 	p = kmalloc(sizeof(*p) + (ualg->alg_key_len + 7) / 8, GFP_KERNEL);
3134447bb33SMartin Willi 	if (!p)
3144447bb33SMartin Willi 		return -ENOMEM;
3154447bb33SMartin Willi 
3164447bb33SMartin Willi 	strcpy(p->alg_name, algo->name);
3174447bb33SMartin Willi 	p->alg_key_len = ualg->alg_key_len;
3184447bb33SMartin Willi 	p->alg_trunc_len = algo->uinfo.auth.icv_truncbits;
3194447bb33SMartin Willi 	memcpy(p->alg_key, ualg->alg_key, (ualg->alg_key_len + 7) / 8);
3204447bb33SMartin Willi 
3214447bb33SMartin Willi 	*algpp = p;
3224447bb33SMartin Willi 	return 0;
3234447bb33SMartin Willi }
3244447bb33SMartin Willi 
3254447bb33SMartin Willi static int attach_auth_trunc(struct xfrm_algo_auth **algpp, u8 *props,
3264447bb33SMartin Willi 			     struct nlattr *rta)
3274447bb33SMartin Willi {
3284447bb33SMartin Willi 	struct xfrm_algo_auth *p, *ualg;
3294447bb33SMartin Willi 	struct xfrm_algo_desc *algo;
3304447bb33SMartin Willi 
3314447bb33SMartin Willi 	if (!rta)
3324447bb33SMartin Willi 		return 0;
3334447bb33SMartin Willi 
3344447bb33SMartin Willi 	ualg = nla_data(rta);
3354447bb33SMartin Willi 
3364447bb33SMartin Willi 	algo = xfrm_aalg_get_byname(ualg->alg_name, 1);
3374447bb33SMartin Willi 	if (!algo)
3384447bb33SMartin Willi 		return -ENOSYS;
339fa6dd8a2SNicolas Dichtel 	if ((ualg->alg_trunc_len / 8) > MAX_AH_AUTH_LEN ||
340fa6dd8a2SNicolas Dichtel 	    ualg->alg_trunc_len > algo->uinfo.auth.icv_fullbits)
3414447bb33SMartin Willi 		return -EINVAL;
3424447bb33SMartin Willi 	*props = algo->desc.sadb_alg_id;
3434447bb33SMartin Willi 
3444447bb33SMartin Willi 	p = kmemdup(ualg, xfrm_alg_auth_len(ualg), GFP_KERNEL);
3454447bb33SMartin Willi 	if (!p)
3464447bb33SMartin Willi 		return -ENOMEM;
3474447bb33SMartin Willi 
3484447bb33SMartin Willi 	strcpy(p->alg_name, algo->name);
3494447bb33SMartin Willi 	if (!p->alg_trunc_len)
3504447bb33SMartin Willi 		p->alg_trunc_len = algo->uinfo.auth.icv_truncbits;
3514447bb33SMartin Willi 
3524447bb33SMartin Willi 	*algpp = p;
3534447bb33SMartin Willi 	return 0;
3544447bb33SMartin Willi }
3554447bb33SMartin Willi 
3561a6509d9SHerbert Xu static int attach_aead(struct xfrm_algo_aead **algpp, u8 *props,
3571a6509d9SHerbert Xu 		       struct nlattr *rta)
3581a6509d9SHerbert Xu {
3591a6509d9SHerbert Xu 	struct xfrm_algo_aead *p, *ualg;
3601a6509d9SHerbert Xu 	struct xfrm_algo_desc *algo;
3611a6509d9SHerbert Xu 
3621a6509d9SHerbert Xu 	if (!rta)
3631a6509d9SHerbert Xu 		return 0;
3641a6509d9SHerbert Xu 
3651a6509d9SHerbert Xu 	ualg = nla_data(rta);
3661a6509d9SHerbert Xu 
3671a6509d9SHerbert Xu 	algo = xfrm_aead_get_byname(ualg->alg_name, ualg->alg_icv_len, 1);
3681a6509d9SHerbert Xu 	if (!algo)
3691a6509d9SHerbert Xu 		return -ENOSYS;
3701a6509d9SHerbert Xu 	*props = algo->desc.sadb_alg_id;
3711a6509d9SHerbert Xu 
3721a6509d9SHerbert Xu 	p = kmemdup(ualg, aead_len(ualg), GFP_KERNEL);
3731a6509d9SHerbert Xu 	if (!p)
3741a6509d9SHerbert Xu 		return -ENOMEM;
3751a6509d9SHerbert Xu 
3761a6509d9SHerbert Xu 	strcpy(p->alg_name, algo->name);
3771a6509d9SHerbert Xu 	*algpp = p;
3781a6509d9SHerbert Xu 	return 0;
3791a6509d9SHerbert Xu }
3801a6509d9SHerbert Xu 
381e2b19125SSteffen Klassert static inline int xfrm_replay_verify_len(struct xfrm_replay_state_esn *replay_esn,
382e2b19125SSteffen Klassert 					 struct nlattr *rp)
383e2b19125SSteffen Klassert {
384e2b19125SSteffen Klassert 	struct xfrm_replay_state_esn *up;
385ecd79187SMathias Krause 	int ulen;
386e2b19125SSteffen Klassert 
387e2b19125SSteffen Klassert 	if (!replay_esn || !rp)
388e2b19125SSteffen Klassert 		return 0;
389e2b19125SSteffen Klassert 
390e2b19125SSteffen Klassert 	up = nla_data(rp);
391ecd79187SMathias Krause 	ulen = xfrm_replay_state_esn_len(up);
392e2b19125SSteffen Klassert 
393ecd79187SMathias Krause 	if (nla_len(rp) < ulen || xfrm_replay_state_esn_len(replay_esn) != ulen)
394e2b19125SSteffen Klassert 		return -EINVAL;
395e2b19125SSteffen Klassert 
396e2b19125SSteffen Klassert 	return 0;
397e2b19125SSteffen Klassert }
398e2b19125SSteffen Klassert 
399d8647b79SSteffen Klassert static int xfrm_alloc_replay_state_esn(struct xfrm_replay_state_esn **replay_esn,
400d8647b79SSteffen Klassert 				       struct xfrm_replay_state_esn **preplay_esn,
401d8647b79SSteffen Klassert 				       struct nlattr *rta)
402d8647b79SSteffen Klassert {
403d8647b79SSteffen Klassert 	struct xfrm_replay_state_esn *p, *pp, *up;
404ecd79187SMathias Krause 	int klen, ulen;
405d8647b79SSteffen Klassert 
406d8647b79SSteffen Klassert 	if (!rta)
407d8647b79SSteffen Klassert 		return 0;
408d8647b79SSteffen Klassert 
409d8647b79SSteffen Klassert 	up = nla_data(rta);
410ecd79187SMathias Krause 	klen = xfrm_replay_state_esn_len(up);
411ecd79187SMathias Krause 	ulen = nla_len(rta) >= klen ? klen : sizeof(*up);
412d8647b79SSteffen Klassert 
413ecd79187SMathias Krause 	p = kzalloc(klen, GFP_KERNEL);
414d8647b79SSteffen Klassert 	if (!p)
415d8647b79SSteffen Klassert 		return -ENOMEM;
416d8647b79SSteffen Klassert 
417ecd79187SMathias Krause 	pp = kzalloc(klen, GFP_KERNEL);
418d8647b79SSteffen Klassert 	if (!pp) {
419d8647b79SSteffen Klassert 		kfree(p);
420d8647b79SSteffen Klassert 		return -ENOMEM;
421d8647b79SSteffen Klassert 	}
422d8647b79SSteffen Klassert 
423ecd79187SMathias Krause 	memcpy(p, up, ulen);
424ecd79187SMathias Krause 	memcpy(pp, up, ulen);
425ecd79187SMathias Krause 
426d8647b79SSteffen Klassert 	*replay_esn = p;
427d8647b79SSteffen Klassert 	*preplay_esn = pp;
428d8647b79SSteffen Klassert 
429d8647b79SSteffen Klassert 	return 0;
430d8647b79SSteffen Klassert }
431d8647b79SSteffen Klassert 
432661697f7SJoy Latten static inline int xfrm_user_sec_ctx_size(struct xfrm_sec_ctx *xfrm_ctx)
433df71837dSTrent Jaeger {
434df71837dSTrent Jaeger 	int len = 0;
435df71837dSTrent Jaeger 
436df71837dSTrent Jaeger 	if (xfrm_ctx) {
437df71837dSTrent Jaeger 		len += sizeof(struct xfrm_user_sec_ctx);
438df71837dSTrent Jaeger 		len += xfrm_ctx->ctx_len;
439df71837dSTrent Jaeger 	}
440df71837dSTrent Jaeger 	return len;
441df71837dSTrent Jaeger }
442df71837dSTrent Jaeger 
4431da177e4SLinus Torvalds static void copy_from_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p)
4441da177e4SLinus Torvalds {
4451da177e4SLinus Torvalds 	memcpy(&x->id, &p->id, sizeof(x->id));
4461da177e4SLinus Torvalds 	memcpy(&x->sel, &p->sel, sizeof(x->sel));
4471da177e4SLinus Torvalds 	memcpy(&x->lft, &p->lft, sizeof(x->lft));
4481da177e4SLinus Torvalds 	x->props.mode = p->mode;
44933fce60dSFan Du 	x->props.replay_window = min_t(unsigned int, p->replay_window,
45033fce60dSFan Du 					sizeof(x->replay.bitmap) * 8);
4511da177e4SLinus Torvalds 	x->props.reqid = p->reqid;
4521da177e4SLinus Torvalds 	x->props.family = p->family;
45354489c14SDavid S. Miller 	memcpy(&x->props.saddr, &p->saddr, sizeof(x->props.saddr));
4541da177e4SLinus Torvalds 	x->props.flags = p->flags;
455196b0036SHerbert Xu 
456ccf9b3b8SSteffen Klassert 	if (!x->sel.family && !(p->flags & XFRM_STATE_AF_UNSPEC))
457196b0036SHerbert Xu 		x->sel.family = p->family;
4581da177e4SLinus Torvalds }
4591da177e4SLinus Torvalds 
460d51d081dSJamal Hadi Salim /*
461d51d081dSJamal Hadi Salim  * someday when pfkey also has support, we could have the code
462d51d081dSJamal Hadi Salim  * somehow made shareable and move it to xfrm_state.c - JHS
463d51d081dSJamal Hadi Salim  *
464d51d081dSJamal Hadi Salim */
465e3ac104dSMathias Krause static void xfrm_update_ae_params(struct xfrm_state *x, struct nlattr **attrs,
466e3ac104dSMathias Krause 				  int update_esn)
467d51d081dSJamal Hadi Salim {
4685424f32eSThomas Graf 	struct nlattr *rp = attrs[XFRMA_REPLAY_VAL];
469e3ac104dSMathias Krause 	struct nlattr *re = update_esn ? attrs[XFRMA_REPLAY_ESN_VAL] : NULL;
4705424f32eSThomas Graf 	struct nlattr *lt = attrs[XFRMA_LTIME_VAL];
4715424f32eSThomas Graf 	struct nlattr *et = attrs[XFRMA_ETIMER_THRESH];
4725424f32eSThomas Graf 	struct nlattr *rt = attrs[XFRMA_REPLAY_THRESH];
473d51d081dSJamal Hadi Salim 
474d8647b79SSteffen Klassert 	if (re) {
475d8647b79SSteffen Klassert 		struct xfrm_replay_state_esn *replay_esn;
476d8647b79SSteffen Klassert 		replay_esn = nla_data(re);
477d8647b79SSteffen Klassert 		memcpy(x->replay_esn, replay_esn,
478d8647b79SSteffen Klassert 		       xfrm_replay_state_esn_len(replay_esn));
479d8647b79SSteffen Klassert 		memcpy(x->preplay_esn, replay_esn,
480d8647b79SSteffen Klassert 		       xfrm_replay_state_esn_len(replay_esn));
481d8647b79SSteffen Klassert 	}
482d8647b79SSteffen Klassert 
483d51d081dSJamal Hadi Salim 	if (rp) {
484d51d081dSJamal Hadi Salim 		struct xfrm_replay_state *replay;
4855424f32eSThomas Graf 		replay = nla_data(rp);
486d51d081dSJamal Hadi Salim 		memcpy(&x->replay, replay, sizeof(*replay));
487d51d081dSJamal Hadi Salim 		memcpy(&x->preplay, replay, sizeof(*replay));
488d51d081dSJamal Hadi Salim 	}
489d51d081dSJamal Hadi Salim 
490d51d081dSJamal Hadi Salim 	if (lt) {
491d51d081dSJamal Hadi Salim 		struct xfrm_lifetime_cur *ltime;
4925424f32eSThomas Graf 		ltime = nla_data(lt);
493d51d081dSJamal Hadi Salim 		x->curlft.bytes = ltime->bytes;
494d51d081dSJamal Hadi Salim 		x->curlft.packets = ltime->packets;
495d51d081dSJamal Hadi Salim 		x->curlft.add_time = ltime->add_time;
496d51d081dSJamal Hadi Salim 		x->curlft.use_time = ltime->use_time;
497d51d081dSJamal Hadi Salim 	}
498d51d081dSJamal Hadi Salim 
499cf5cb79fSThomas Graf 	if (et)
5005424f32eSThomas Graf 		x->replay_maxage = nla_get_u32(et);
501d51d081dSJamal Hadi Salim 
502cf5cb79fSThomas Graf 	if (rt)
5035424f32eSThomas Graf 		x->replay_maxdiff = nla_get_u32(rt);
504d51d081dSJamal Hadi Salim }
505d51d081dSJamal Hadi Salim 
506fc34acd3SAlexey Dobriyan static struct xfrm_state *xfrm_state_construct(struct net *net,
507fc34acd3SAlexey Dobriyan 					       struct xfrm_usersa_info *p,
5085424f32eSThomas Graf 					       struct nlattr **attrs,
5091da177e4SLinus Torvalds 					       int *errp)
5101da177e4SLinus Torvalds {
511fc34acd3SAlexey Dobriyan 	struct xfrm_state *x = xfrm_state_alloc(net);
5121da177e4SLinus Torvalds 	int err = -ENOMEM;
5131da177e4SLinus Torvalds 
5141da177e4SLinus Torvalds 	if (!x)
5151da177e4SLinus Torvalds 		goto error_no_put;
5161da177e4SLinus Torvalds 
5171da177e4SLinus Torvalds 	copy_from_user_state(x, p);
5181da177e4SLinus Torvalds 
519a947b0a9SNicolas Dichtel 	if (attrs[XFRMA_SA_EXTRA_FLAGS])
520a947b0a9SNicolas Dichtel 		x->props.extra_flags = nla_get_u32(attrs[XFRMA_SA_EXTRA_FLAGS]);
521a947b0a9SNicolas Dichtel 
5221a6509d9SHerbert Xu 	if ((err = attach_aead(&x->aead, &x->props.ealgo,
5231a6509d9SHerbert Xu 			       attrs[XFRMA_ALG_AEAD])))
5241a6509d9SHerbert Xu 		goto error;
5254447bb33SMartin Willi 	if ((err = attach_auth_trunc(&x->aalg, &x->props.aalgo,
5264447bb33SMartin Willi 				     attrs[XFRMA_ALG_AUTH_TRUNC])))
5274447bb33SMartin Willi 		goto error;
5284447bb33SMartin Willi 	if (!x->props.aalgo) {
5294447bb33SMartin Willi 		if ((err = attach_auth(&x->aalg, &x->props.aalgo,
53035a7aa08SThomas Graf 				       attrs[XFRMA_ALG_AUTH])))
5311da177e4SLinus Torvalds 			goto error;
5324447bb33SMartin Willi 	}
5331da177e4SLinus Torvalds 	if ((err = attach_one_algo(&x->ealg, &x->props.ealgo,
5341da177e4SLinus Torvalds 				   xfrm_ealg_get_byname,
53535a7aa08SThomas Graf 				   attrs[XFRMA_ALG_CRYPT])))
5361da177e4SLinus Torvalds 		goto error;
5371da177e4SLinus Torvalds 	if ((err = attach_one_algo(&x->calg, &x->props.calgo,
5381da177e4SLinus Torvalds 				   xfrm_calg_get_byname,
53935a7aa08SThomas Graf 				   attrs[XFRMA_ALG_COMP])))
5401da177e4SLinus Torvalds 		goto error;
541fd21150aSThomas Graf 
542fd21150aSThomas Graf 	if (attrs[XFRMA_ENCAP]) {
543fd21150aSThomas Graf 		x->encap = kmemdup(nla_data(attrs[XFRMA_ENCAP]),
544fd21150aSThomas Graf 				   sizeof(*x->encap), GFP_KERNEL);
545fd21150aSThomas Graf 		if (x->encap == NULL)
5461da177e4SLinus Torvalds 			goto error;
547fd21150aSThomas Graf 	}
548fd21150aSThomas Graf 
54935d2856bSMartin Willi 	if (attrs[XFRMA_TFCPAD])
55035d2856bSMartin Willi 		x->tfcpad = nla_get_u32(attrs[XFRMA_TFCPAD]);
55135d2856bSMartin Willi 
552fd21150aSThomas Graf 	if (attrs[XFRMA_COADDR]) {
553fd21150aSThomas Graf 		x->coaddr = kmemdup(nla_data(attrs[XFRMA_COADDR]),
554fd21150aSThomas Graf 				    sizeof(*x->coaddr), GFP_KERNEL);
555fd21150aSThomas Graf 		if (x->coaddr == NULL)
556060f02a3SNoriaki TAKAMIYA 			goto error;
557fd21150aSThomas Graf 	}
558fd21150aSThomas Graf 
5596f26b61eSJamal Hadi Salim 	xfrm_mark_get(attrs, &x->mark);
5606f26b61eSJamal Hadi Salim 
561a454f0ccSWei Yongjun 	err = __xfrm_init_state(x, false);
5621da177e4SLinus Torvalds 	if (err)
5631da177e4SLinus Torvalds 		goto error;
5641da177e4SLinus Torvalds 
565fd21150aSThomas Graf 	if (attrs[XFRMA_SEC_CTX] &&
566fd21150aSThomas Graf 	    security_xfrm_state_alloc(x, nla_data(attrs[XFRMA_SEC_CTX])))
567df71837dSTrent Jaeger 		goto error;
568df71837dSTrent Jaeger 
569d8647b79SSteffen Klassert 	if ((err = xfrm_alloc_replay_state_esn(&x->replay_esn, &x->preplay_esn,
570d8647b79SSteffen Klassert 					       attrs[XFRMA_REPLAY_ESN_VAL])))
571d8647b79SSteffen Klassert 		goto error;
572d8647b79SSteffen Klassert 
5731da177e4SLinus Torvalds 	x->km.seq = p->seq;
574b27aeadbSAlexey Dobriyan 	x->replay_maxdiff = net->xfrm.sysctl_aevent_rseqth;
575d51d081dSJamal Hadi Salim 	/* sysctl_xfrm_aevent_etime is in 100ms units */
576b27aeadbSAlexey Dobriyan 	x->replay_maxage = (net->xfrm.sysctl_aevent_etime*HZ)/XFRM_AE_ETH_M;
577d51d081dSJamal Hadi Salim 
5789fdc4883SSteffen Klassert 	if ((err = xfrm_init_replay(x)))
5799fdc4883SSteffen Klassert 		goto error;
580d51d081dSJamal Hadi Salim 
5819fdc4883SSteffen Klassert 	/* override default values from above */
582e3ac104dSMathias Krause 	xfrm_update_ae_params(x, attrs, 0);
5831da177e4SLinus Torvalds 
5841da177e4SLinus Torvalds 	return x;
5851da177e4SLinus Torvalds 
5861da177e4SLinus Torvalds error:
5871da177e4SLinus Torvalds 	x->km.state = XFRM_STATE_DEAD;
5881da177e4SLinus Torvalds 	xfrm_state_put(x);
5891da177e4SLinus Torvalds error_no_put:
5901da177e4SLinus Torvalds 	*errp = err;
5911da177e4SLinus Torvalds 	return NULL;
5921da177e4SLinus Torvalds }
5931da177e4SLinus Torvalds 
59422e70050SChristoph Hellwig static int xfrm_add_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
5955424f32eSThomas Graf 		struct nlattr **attrs)
5961da177e4SLinus Torvalds {
597fc34acd3SAlexey Dobriyan 	struct net *net = sock_net(skb->sk);
5987b67c857SThomas Graf 	struct xfrm_usersa_info *p = nlmsg_data(nlh);
5991da177e4SLinus Torvalds 	struct xfrm_state *x;
6001da177e4SLinus Torvalds 	int err;
60126b15dadSJamal Hadi Salim 	struct km_event c;
602e1760bd5SEric W. Biederman 	kuid_t loginuid = audit_get_loginuid(current);
603c53fa1edSPatrick McHardy 	u32 sessionid = audit_get_sessionid(current);
604c53fa1edSPatrick McHardy 	u32 sid;
6051da177e4SLinus Torvalds 
60635a7aa08SThomas Graf 	err = verify_newsa_info(p, attrs);
6071da177e4SLinus Torvalds 	if (err)
6081da177e4SLinus Torvalds 		return err;
6091da177e4SLinus Torvalds 
610fc34acd3SAlexey Dobriyan 	x = xfrm_state_construct(net, p, attrs, &err);
6111da177e4SLinus Torvalds 	if (!x)
6121da177e4SLinus Torvalds 		return err;
6131da177e4SLinus Torvalds 
61426b15dadSJamal Hadi Salim 	xfrm_state_hold(x);
6151da177e4SLinus Torvalds 	if (nlh->nlmsg_type == XFRM_MSG_NEWSA)
6161da177e4SLinus Torvalds 		err = xfrm_state_add(x);
6171da177e4SLinus Torvalds 	else
6181da177e4SLinus Torvalds 		err = xfrm_state_update(x);
6191da177e4SLinus Torvalds 
620c53fa1edSPatrick McHardy 	security_task_getsecid(current, &sid);
6212532386fSEric Paris 	xfrm_audit_state_add(x, err ? 0 : 1, loginuid, sessionid, sid);
622161a09e7SJoy Latten 
6231da177e4SLinus Torvalds 	if (err < 0) {
6241da177e4SLinus Torvalds 		x->km.state = XFRM_STATE_DEAD;
62521380b81SHerbert Xu 		__xfrm_state_put(x);
6267d6dfe1fSPatrick McHardy 		goto out;
6271da177e4SLinus Torvalds 	}
6281da177e4SLinus Torvalds 
62926b15dadSJamal Hadi Salim 	c.seq = nlh->nlmsg_seq;
63015e47304SEric W. Biederman 	c.portid = nlh->nlmsg_pid;
631f60f6b8fSHerbert Xu 	c.event = nlh->nlmsg_type;
63226b15dadSJamal Hadi Salim 
63326b15dadSJamal Hadi Salim 	km_state_notify(x, &c);
6347d6dfe1fSPatrick McHardy out:
63526b15dadSJamal Hadi Salim 	xfrm_state_put(x);
6361da177e4SLinus Torvalds 	return err;
6371da177e4SLinus Torvalds }
6381da177e4SLinus Torvalds 
639fc34acd3SAlexey Dobriyan static struct xfrm_state *xfrm_user_state_lookup(struct net *net,
640fc34acd3SAlexey Dobriyan 						 struct xfrm_usersa_id *p,
6415424f32eSThomas Graf 						 struct nlattr **attrs,
642eb2971b6SMasahide NAKAMURA 						 int *errp)
643eb2971b6SMasahide NAKAMURA {
644eb2971b6SMasahide NAKAMURA 	struct xfrm_state *x = NULL;
6456f26b61eSJamal Hadi Salim 	struct xfrm_mark m;
646eb2971b6SMasahide NAKAMURA 	int err;
6476f26b61eSJamal Hadi Salim 	u32 mark = xfrm_mark_get(attrs, &m);
648eb2971b6SMasahide NAKAMURA 
649eb2971b6SMasahide NAKAMURA 	if (xfrm_id_proto_match(p->proto, IPSEC_PROTO_ANY)) {
650eb2971b6SMasahide NAKAMURA 		err = -ESRCH;
6516f26b61eSJamal Hadi Salim 		x = xfrm_state_lookup(net, mark, &p->daddr, p->spi, p->proto, p->family);
652eb2971b6SMasahide NAKAMURA 	} else {
653eb2971b6SMasahide NAKAMURA 		xfrm_address_t *saddr = NULL;
654eb2971b6SMasahide NAKAMURA 
65535a7aa08SThomas Graf 		verify_one_addr(attrs, XFRMA_SRCADDR, &saddr);
656eb2971b6SMasahide NAKAMURA 		if (!saddr) {
657eb2971b6SMasahide NAKAMURA 			err = -EINVAL;
658eb2971b6SMasahide NAKAMURA 			goto out;
659eb2971b6SMasahide NAKAMURA 		}
660eb2971b6SMasahide NAKAMURA 
6619abbffeeSMasahide NAKAMURA 		err = -ESRCH;
6626f26b61eSJamal Hadi Salim 		x = xfrm_state_lookup_byaddr(net, mark,
6636f26b61eSJamal Hadi Salim 					     &p->daddr, saddr,
664221df1edSAlexey Dobriyan 					     p->proto, p->family);
665eb2971b6SMasahide NAKAMURA 	}
666eb2971b6SMasahide NAKAMURA 
667eb2971b6SMasahide NAKAMURA  out:
668eb2971b6SMasahide NAKAMURA 	if (!x && errp)
669eb2971b6SMasahide NAKAMURA 		*errp = err;
670eb2971b6SMasahide NAKAMURA 	return x;
671eb2971b6SMasahide NAKAMURA }
672eb2971b6SMasahide NAKAMURA 
67322e70050SChristoph Hellwig static int xfrm_del_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
6745424f32eSThomas Graf 		struct nlattr **attrs)
6751da177e4SLinus Torvalds {
676fc34acd3SAlexey Dobriyan 	struct net *net = sock_net(skb->sk);
6771da177e4SLinus Torvalds 	struct xfrm_state *x;
678eb2971b6SMasahide NAKAMURA 	int err = -ESRCH;
67926b15dadSJamal Hadi Salim 	struct km_event c;
6807b67c857SThomas Graf 	struct xfrm_usersa_id *p = nlmsg_data(nlh);
681e1760bd5SEric W. Biederman 	kuid_t loginuid = audit_get_loginuid(current);
682c53fa1edSPatrick McHardy 	u32 sessionid = audit_get_sessionid(current);
683c53fa1edSPatrick McHardy 	u32 sid;
6841da177e4SLinus Torvalds 
685fc34acd3SAlexey Dobriyan 	x = xfrm_user_state_lookup(net, p, attrs, &err);
6861da177e4SLinus Torvalds 	if (x == NULL)
687eb2971b6SMasahide NAKAMURA 		return err;
6881da177e4SLinus Torvalds 
6896f68dc37SDavid S. Miller 	if ((err = security_xfrm_state_delete(x)) != 0)
690c8c05a8eSCatherine Zhang 		goto out;
691c8c05a8eSCatherine Zhang 
6921da177e4SLinus Torvalds 	if (xfrm_state_kern(x)) {
693c8c05a8eSCatherine Zhang 		err = -EPERM;
694c8c05a8eSCatherine Zhang 		goto out;
6951da177e4SLinus Torvalds 	}
6961da177e4SLinus Torvalds 
69726b15dadSJamal Hadi Salim 	err = xfrm_state_delete(x);
698161a09e7SJoy Latten 
699c8c05a8eSCatherine Zhang 	if (err < 0)
700c8c05a8eSCatherine Zhang 		goto out;
70126b15dadSJamal Hadi Salim 
70226b15dadSJamal Hadi Salim 	c.seq = nlh->nlmsg_seq;
70315e47304SEric W. Biederman 	c.portid = nlh->nlmsg_pid;
704f60f6b8fSHerbert Xu 	c.event = nlh->nlmsg_type;
70526b15dadSJamal Hadi Salim 	km_state_notify(x, &c);
7061da177e4SLinus Torvalds 
707c8c05a8eSCatherine Zhang out:
708c53fa1edSPatrick McHardy 	security_task_getsecid(current, &sid);
7092532386fSEric Paris 	xfrm_audit_state_delete(x, err ? 0 : 1, loginuid, sessionid, sid);
710c8c05a8eSCatherine Zhang 	xfrm_state_put(x);
71126b15dadSJamal Hadi Salim 	return err;
7121da177e4SLinus Torvalds }
7131da177e4SLinus Torvalds 
7141da177e4SLinus Torvalds static void copy_to_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p)
7151da177e4SLinus Torvalds {
716f778a636SMathias Krause 	memset(p, 0, sizeof(*p));
7171da177e4SLinus Torvalds 	memcpy(&p->id, &x->id, sizeof(p->id));
7181da177e4SLinus Torvalds 	memcpy(&p->sel, &x->sel, sizeof(p->sel));
7191da177e4SLinus Torvalds 	memcpy(&p->lft, &x->lft, sizeof(p->lft));
7201da177e4SLinus Torvalds 	memcpy(&p->curlft, &x->curlft, sizeof(p->curlft));
7211da177e4SLinus Torvalds 	memcpy(&p->stats, &x->stats, sizeof(p->stats));
72254489c14SDavid S. Miller 	memcpy(&p->saddr, &x->props.saddr, sizeof(p->saddr));
7231da177e4SLinus Torvalds 	p->mode = x->props.mode;
7241da177e4SLinus Torvalds 	p->replay_window = x->props.replay_window;
7251da177e4SLinus Torvalds 	p->reqid = x->props.reqid;
7261da177e4SLinus Torvalds 	p->family = x->props.family;
7271da177e4SLinus Torvalds 	p->flags = x->props.flags;
7281da177e4SLinus Torvalds 	p->seq = x->km.seq;
7291da177e4SLinus Torvalds }
7301da177e4SLinus Torvalds 
7311da177e4SLinus Torvalds struct xfrm_dump_info {
7321da177e4SLinus Torvalds 	struct sk_buff *in_skb;
7331da177e4SLinus Torvalds 	struct sk_buff *out_skb;
7341da177e4SLinus Torvalds 	u32 nlmsg_seq;
7351da177e4SLinus Torvalds 	u16 nlmsg_flags;
7361da177e4SLinus Torvalds };
7371da177e4SLinus Torvalds 
738c0144beaSThomas Graf static int copy_sec_ctx(struct xfrm_sec_ctx *s, struct sk_buff *skb)
739c0144beaSThomas Graf {
740c0144beaSThomas Graf 	struct xfrm_user_sec_ctx *uctx;
741c0144beaSThomas Graf 	struct nlattr *attr;
74268325d3bSHerbert Xu 	int ctx_size = sizeof(*uctx) + s->ctx_len;
743c0144beaSThomas Graf 
744c0144beaSThomas Graf 	attr = nla_reserve(skb, XFRMA_SEC_CTX, ctx_size);
745c0144beaSThomas Graf 	if (attr == NULL)
746c0144beaSThomas Graf 		return -EMSGSIZE;
747c0144beaSThomas Graf 
748c0144beaSThomas Graf 	uctx = nla_data(attr);
749c0144beaSThomas Graf 	uctx->exttype = XFRMA_SEC_CTX;
750c0144beaSThomas Graf 	uctx->len = ctx_size;
751c0144beaSThomas Graf 	uctx->ctx_doi = s->ctx_doi;
752c0144beaSThomas Graf 	uctx->ctx_alg = s->ctx_alg;
753c0144beaSThomas Graf 	uctx->ctx_len = s->ctx_len;
754c0144beaSThomas Graf 	memcpy(uctx + 1, s->ctx_str, s->ctx_len);
755c0144beaSThomas Graf 
756c0144beaSThomas Graf 	return 0;
757c0144beaSThomas Graf }
758c0144beaSThomas Graf 
7594447bb33SMartin Willi static int copy_to_user_auth(struct xfrm_algo_auth *auth, struct sk_buff *skb)
7604447bb33SMartin Willi {
7614447bb33SMartin Willi 	struct xfrm_algo *algo;
7624447bb33SMartin Willi 	struct nlattr *nla;
7634447bb33SMartin Willi 
7644447bb33SMartin Willi 	nla = nla_reserve(skb, XFRMA_ALG_AUTH,
7654447bb33SMartin Willi 			  sizeof(*algo) + (auth->alg_key_len + 7) / 8);
7664447bb33SMartin Willi 	if (!nla)
7674447bb33SMartin Willi 		return -EMSGSIZE;
7684447bb33SMartin Willi 
7694447bb33SMartin Willi 	algo = nla_data(nla);
7704c87308bSMathias Krause 	strncpy(algo->alg_name, auth->alg_name, sizeof(algo->alg_name));
7714447bb33SMartin Willi 	memcpy(algo->alg_key, auth->alg_key, (auth->alg_key_len + 7) / 8);
7724447bb33SMartin Willi 	algo->alg_key_len = auth->alg_key_len;
7734447bb33SMartin Willi 
7744447bb33SMartin Willi 	return 0;
7754447bb33SMartin Willi }
7764447bb33SMartin Willi 
77768325d3bSHerbert Xu /* Don't change this without updating xfrm_sa_len! */
77868325d3bSHerbert Xu static int copy_to_user_state_extra(struct xfrm_state *x,
77968325d3bSHerbert Xu 				    struct xfrm_usersa_info *p,
78068325d3bSHerbert Xu 				    struct sk_buff *skb)
7811da177e4SLinus Torvalds {
7821d1e34ddSDavid S. Miller 	int ret = 0;
7831d1e34ddSDavid S. Miller 
7841da177e4SLinus Torvalds 	copy_to_user_state(x, p);
7851da177e4SLinus Torvalds 
786a947b0a9SNicolas Dichtel 	if (x->props.extra_flags) {
787a947b0a9SNicolas Dichtel 		ret = nla_put_u32(skb, XFRMA_SA_EXTRA_FLAGS,
788a947b0a9SNicolas Dichtel 				  x->props.extra_flags);
789a947b0a9SNicolas Dichtel 		if (ret)
790a947b0a9SNicolas Dichtel 			goto out;
791a947b0a9SNicolas Dichtel 	}
792a947b0a9SNicolas Dichtel 
7931d1e34ddSDavid S. Miller 	if (x->coaddr) {
7941d1e34ddSDavid S. Miller 		ret = nla_put(skb, XFRMA_COADDR, sizeof(*x->coaddr), x->coaddr);
7951d1e34ddSDavid S. Miller 		if (ret)
7961d1e34ddSDavid S. Miller 			goto out;
7971d1e34ddSDavid S. Miller 	}
7981d1e34ddSDavid S. Miller 	if (x->lastused) {
7991d1e34ddSDavid S. Miller 		ret = nla_put_u64(skb, XFRMA_LASTUSED, x->lastused);
8001d1e34ddSDavid S. Miller 		if (ret)
8011d1e34ddSDavid S. Miller 			goto out;
8021d1e34ddSDavid S. Miller 	}
8031d1e34ddSDavid S. Miller 	if (x->aead) {
8041d1e34ddSDavid S. Miller 		ret = nla_put(skb, XFRMA_ALG_AEAD, aead_len(x->aead), x->aead);
8051d1e34ddSDavid S. Miller 		if (ret)
8061d1e34ddSDavid S. Miller 			goto out;
8071d1e34ddSDavid S. Miller 	}
8081d1e34ddSDavid S. Miller 	if (x->aalg) {
8091d1e34ddSDavid S. Miller 		ret = copy_to_user_auth(x->aalg, skb);
8101d1e34ddSDavid S. Miller 		if (!ret)
8111d1e34ddSDavid S. Miller 			ret = nla_put(skb, XFRMA_ALG_AUTH_TRUNC,
8121d1e34ddSDavid S. Miller 				      xfrm_alg_auth_len(x->aalg), x->aalg);
8131d1e34ddSDavid S. Miller 		if (ret)
8141d1e34ddSDavid S. Miller 			goto out;
8151d1e34ddSDavid S. Miller 	}
8161d1e34ddSDavid S. Miller 	if (x->ealg) {
8171d1e34ddSDavid S. Miller 		ret = nla_put(skb, XFRMA_ALG_CRYPT, xfrm_alg_len(x->ealg), x->ealg);
8181d1e34ddSDavid S. Miller 		if (ret)
8191d1e34ddSDavid S. Miller 			goto out;
8201d1e34ddSDavid S. Miller 	}
8211d1e34ddSDavid S. Miller 	if (x->calg) {
8221d1e34ddSDavid S. Miller 		ret = nla_put(skb, XFRMA_ALG_COMP, sizeof(*(x->calg)), x->calg);
8231d1e34ddSDavid S. Miller 		if (ret)
8241d1e34ddSDavid S. Miller 			goto out;
8251d1e34ddSDavid S. Miller 	}
8261d1e34ddSDavid S. Miller 	if (x->encap) {
8271d1e34ddSDavid S. Miller 		ret = nla_put(skb, XFRMA_ENCAP, sizeof(*x->encap), x->encap);
8281d1e34ddSDavid S. Miller 		if (ret)
8291d1e34ddSDavid S. Miller 			goto out;
8301d1e34ddSDavid S. Miller 	}
8311d1e34ddSDavid S. Miller 	if (x->tfcpad) {
8321d1e34ddSDavid S. Miller 		ret = nla_put_u32(skb, XFRMA_TFCPAD, x->tfcpad);
8331d1e34ddSDavid S. Miller 		if (ret)
8341d1e34ddSDavid S. Miller 			goto out;
8351d1e34ddSDavid S. Miller 	}
8361d1e34ddSDavid S. Miller 	ret = xfrm_mark_put(skb, &x->mark);
8371d1e34ddSDavid S. Miller 	if (ret)
8381d1e34ddSDavid S. Miller 		goto out;
8391d1e34ddSDavid S. Miller 	if (x->replay_esn) {
8401d1e34ddSDavid S. Miller 		ret = nla_put(skb, XFRMA_REPLAY_ESN_VAL,
841d0fde795SDavid S. Miller 			      xfrm_replay_state_esn_len(x->replay_esn),
8421d1e34ddSDavid S. Miller 			      x->replay_esn);
8431d1e34ddSDavid S. Miller 		if (ret)
8441d1e34ddSDavid S. Miller 			goto out;
8451d1e34ddSDavid S. Miller 	}
8461d1e34ddSDavid S. Miller 	if (x->security)
8471d1e34ddSDavid S. Miller 		ret = copy_sec_ctx(x->security, skb);
8481d1e34ddSDavid S. Miller out:
8491d1e34ddSDavid S. Miller 	return ret;
85068325d3bSHerbert Xu }
85168325d3bSHerbert Xu 
85268325d3bSHerbert Xu static int dump_one_state(struct xfrm_state *x, int count, void *ptr)
85368325d3bSHerbert Xu {
85468325d3bSHerbert Xu 	struct xfrm_dump_info *sp = ptr;
85568325d3bSHerbert Xu 	struct sk_buff *in_skb = sp->in_skb;
85668325d3bSHerbert Xu 	struct sk_buff *skb = sp->out_skb;
85768325d3bSHerbert Xu 	struct xfrm_usersa_info *p;
85868325d3bSHerbert Xu 	struct nlmsghdr *nlh;
85968325d3bSHerbert Xu 	int err;
86068325d3bSHerbert Xu 
86115e47304SEric W. Biederman 	nlh = nlmsg_put(skb, NETLINK_CB(in_skb).portid, sp->nlmsg_seq,
86268325d3bSHerbert Xu 			XFRM_MSG_NEWSA, sizeof(*p), sp->nlmsg_flags);
86368325d3bSHerbert Xu 	if (nlh == NULL)
86468325d3bSHerbert Xu 		return -EMSGSIZE;
86568325d3bSHerbert Xu 
86668325d3bSHerbert Xu 	p = nlmsg_data(nlh);
86768325d3bSHerbert Xu 
86868325d3bSHerbert Xu 	err = copy_to_user_state_extra(x, p, skb);
8691d1e34ddSDavid S. Miller 	if (err) {
8709825069dSThomas Graf 		nlmsg_cancel(skb, nlh);
87168325d3bSHerbert Xu 		return err;
8721da177e4SLinus Torvalds 	}
8731d1e34ddSDavid S. Miller 	nlmsg_end(skb, nlh);
8741d1e34ddSDavid S. Miller 	return 0;
8751d1e34ddSDavid S. Miller }
8761da177e4SLinus Torvalds 
8774c563f76STimo Teras static int xfrm_dump_sa_done(struct netlink_callback *cb)
8784c563f76STimo Teras {
8794c563f76STimo Teras 	struct xfrm_state_walk *walk = (struct xfrm_state_walk *) &cb->args[1];
8804c563f76STimo Teras 	xfrm_state_walk_done(walk);
8814c563f76STimo Teras 	return 0;
8824c563f76STimo Teras }
8834c563f76STimo Teras 
8841da177e4SLinus Torvalds static int xfrm_dump_sa(struct sk_buff *skb, struct netlink_callback *cb)
8851da177e4SLinus Torvalds {
886fc34acd3SAlexey Dobriyan 	struct net *net = sock_net(skb->sk);
8874c563f76STimo Teras 	struct xfrm_state_walk *walk = (struct xfrm_state_walk *) &cb->args[1];
8881da177e4SLinus Torvalds 	struct xfrm_dump_info info;
8891da177e4SLinus Torvalds 
8904c563f76STimo Teras 	BUILD_BUG_ON(sizeof(struct xfrm_state_walk) >
8914c563f76STimo Teras 		     sizeof(cb->args) - sizeof(cb->args[0]));
8924c563f76STimo Teras 
8931da177e4SLinus Torvalds 	info.in_skb = cb->skb;
8941da177e4SLinus Torvalds 	info.out_skb = skb;
8951da177e4SLinus Torvalds 	info.nlmsg_seq = cb->nlh->nlmsg_seq;
8961da177e4SLinus Torvalds 	info.nlmsg_flags = NLM_F_MULTI;
8974c563f76STimo Teras 
8984c563f76STimo Teras 	if (!cb->args[0]) {
8994c563f76STimo Teras 		cb->args[0] = 1;
9004c563f76STimo Teras 		xfrm_state_walk_init(walk, 0);
9014c563f76STimo Teras 	}
9024c563f76STimo Teras 
903fc34acd3SAlexey Dobriyan 	(void) xfrm_state_walk(net, walk, dump_one_state, &info);
9041da177e4SLinus Torvalds 
9051da177e4SLinus Torvalds 	return skb->len;
9061da177e4SLinus Torvalds }
9071da177e4SLinus Torvalds 
9081da177e4SLinus Torvalds static struct sk_buff *xfrm_state_netlink(struct sk_buff *in_skb,
9091da177e4SLinus Torvalds 					  struct xfrm_state *x, u32 seq)
9101da177e4SLinus Torvalds {
9111da177e4SLinus Torvalds 	struct xfrm_dump_info info;
9121da177e4SLinus Torvalds 	struct sk_buff *skb;
913864745d2SMathias Krause 	int err;
9141da177e4SLinus Torvalds 
9157deb2264SThomas Graf 	skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
9161da177e4SLinus Torvalds 	if (!skb)
9171da177e4SLinus Torvalds 		return ERR_PTR(-ENOMEM);
9181da177e4SLinus Torvalds 
9191da177e4SLinus Torvalds 	info.in_skb = in_skb;
9201da177e4SLinus Torvalds 	info.out_skb = skb;
9211da177e4SLinus Torvalds 	info.nlmsg_seq = seq;
9221da177e4SLinus Torvalds 	info.nlmsg_flags = 0;
9231da177e4SLinus Torvalds 
924864745d2SMathias Krause 	err = dump_one_state(x, 0, &info);
925864745d2SMathias Krause 	if (err) {
9261da177e4SLinus Torvalds 		kfree_skb(skb);
927864745d2SMathias Krause 		return ERR_PTR(err);
9281da177e4SLinus Torvalds 	}
9291da177e4SLinus Torvalds 
9301da177e4SLinus Torvalds 	return skb;
9311da177e4SLinus Torvalds }
9321da177e4SLinus Torvalds 
9337deb2264SThomas Graf static inline size_t xfrm_spdinfo_msgsize(void)
9347deb2264SThomas Graf {
9357deb2264SThomas Graf 	return NLMSG_ALIGN(4)
9367deb2264SThomas Graf 	       + nla_total_size(sizeof(struct xfrmu_spdinfo))
9377deb2264SThomas Graf 	       + nla_total_size(sizeof(struct xfrmu_spdhinfo));
9387deb2264SThomas Graf }
9397deb2264SThomas Graf 
940e071041bSAlexey Dobriyan static int build_spdinfo(struct sk_buff *skb, struct net *net,
94115e47304SEric W. Biederman 			 u32 portid, u32 seq, u32 flags)
942ecfd6b18SJamal Hadi Salim {
9435a6d3416SJamal Hadi Salim 	struct xfrmk_spdinfo si;
9445a6d3416SJamal Hadi Salim 	struct xfrmu_spdinfo spc;
9455a6d3416SJamal Hadi Salim 	struct xfrmu_spdhinfo sph;
946ecfd6b18SJamal Hadi Salim 	struct nlmsghdr *nlh;
9471d1e34ddSDavid S. Miller 	int err;
948ecfd6b18SJamal Hadi Salim 	u32 *f;
949ecfd6b18SJamal Hadi Salim 
95015e47304SEric W. Biederman 	nlh = nlmsg_put(skb, portid, seq, XFRM_MSG_NEWSPDINFO, sizeof(u32), 0);
95125985edcSLucas De Marchi 	if (nlh == NULL) /* shouldn't really happen ... */
952ecfd6b18SJamal Hadi Salim 		return -EMSGSIZE;
953ecfd6b18SJamal Hadi Salim 
954ecfd6b18SJamal Hadi Salim 	f = nlmsg_data(nlh);
955ecfd6b18SJamal Hadi Salim 	*f = flags;
956e071041bSAlexey Dobriyan 	xfrm_spd_getinfo(net, &si);
9575a6d3416SJamal Hadi Salim 	spc.incnt = si.incnt;
9585a6d3416SJamal Hadi Salim 	spc.outcnt = si.outcnt;
9595a6d3416SJamal Hadi Salim 	spc.fwdcnt = si.fwdcnt;
9605a6d3416SJamal Hadi Salim 	spc.inscnt = si.inscnt;
9615a6d3416SJamal Hadi Salim 	spc.outscnt = si.outscnt;
9625a6d3416SJamal Hadi Salim 	spc.fwdscnt = si.fwdscnt;
9635a6d3416SJamal Hadi Salim 	sph.spdhcnt = si.spdhcnt;
9645a6d3416SJamal Hadi Salim 	sph.spdhmcnt = si.spdhmcnt;
965ecfd6b18SJamal Hadi Salim 
9661d1e34ddSDavid S. Miller 	err = nla_put(skb, XFRMA_SPD_INFO, sizeof(spc), &spc);
9671d1e34ddSDavid S. Miller 	if (!err)
9681d1e34ddSDavid S. Miller 		err = nla_put(skb, XFRMA_SPD_HINFO, sizeof(sph), &sph);
9691d1e34ddSDavid S. Miller 	if (err) {
9701d1e34ddSDavid S. Miller 		nlmsg_cancel(skb, nlh);
9711d1e34ddSDavid S. Miller 		return err;
9721d1e34ddSDavid S. Miller 	}
973ecfd6b18SJamal Hadi Salim 
974ecfd6b18SJamal Hadi Salim 	return nlmsg_end(skb, nlh);
975ecfd6b18SJamal Hadi Salim }
976ecfd6b18SJamal Hadi Salim 
977ecfd6b18SJamal Hadi Salim static int xfrm_get_spdinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
9785424f32eSThomas Graf 		struct nlattr **attrs)
979ecfd6b18SJamal Hadi Salim {
980a6483b79SAlexey Dobriyan 	struct net *net = sock_net(skb->sk);
981ecfd6b18SJamal Hadi Salim 	struct sk_buff *r_skb;
9827b67c857SThomas Graf 	u32 *flags = nlmsg_data(nlh);
98315e47304SEric W. Biederman 	u32 sportid = NETLINK_CB(skb).portid;
984ecfd6b18SJamal Hadi Salim 	u32 seq = nlh->nlmsg_seq;
985ecfd6b18SJamal Hadi Salim 
9867deb2264SThomas Graf 	r_skb = nlmsg_new(xfrm_spdinfo_msgsize(), GFP_ATOMIC);
987ecfd6b18SJamal Hadi Salim 	if (r_skb == NULL)
988ecfd6b18SJamal Hadi Salim 		return -ENOMEM;
989ecfd6b18SJamal Hadi Salim 
99015e47304SEric W. Biederman 	if (build_spdinfo(r_skb, net, sportid, seq, *flags) < 0)
991ecfd6b18SJamal Hadi Salim 		BUG();
992ecfd6b18SJamal Hadi Salim 
99315e47304SEric W. Biederman 	return nlmsg_unicast(net->xfrm.nlsk, r_skb, sportid);
994ecfd6b18SJamal Hadi Salim }
995ecfd6b18SJamal Hadi Salim 
9967deb2264SThomas Graf static inline size_t xfrm_sadinfo_msgsize(void)
9977deb2264SThomas Graf {
9987deb2264SThomas Graf 	return NLMSG_ALIGN(4)
9997deb2264SThomas Graf 	       + nla_total_size(sizeof(struct xfrmu_sadhinfo))
10007deb2264SThomas Graf 	       + nla_total_size(4); /* XFRMA_SAD_CNT */
10017deb2264SThomas Graf }
10027deb2264SThomas Graf 
1003e071041bSAlexey Dobriyan static int build_sadinfo(struct sk_buff *skb, struct net *net,
100415e47304SEric W. Biederman 			 u32 portid, u32 seq, u32 flags)
100528d8909bSJamal Hadi Salim {
1006af11e316SJamal Hadi Salim 	struct xfrmk_sadinfo si;
1007af11e316SJamal Hadi Salim 	struct xfrmu_sadhinfo sh;
100828d8909bSJamal Hadi Salim 	struct nlmsghdr *nlh;
10091d1e34ddSDavid S. Miller 	int err;
101028d8909bSJamal Hadi Salim 	u32 *f;
101128d8909bSJamal Hadi Salim 
101215e47304SEric W. Biederman 	nlh = nlmsg_put(skb, portid, seq, XFRM_MSG_NEWSADINFO, sizeof(u32), 0);
101325985edcSLucas De Marchi 	if (nlh == NULL) /* shouldn't really happen ... */
101428d8909bSJamal Hadi Salim 		return -EMSGSIZE;
101528d8909bSJamal Hadi Salim 
101628d8909bSJamal Hadi Salim 	f = nlmsg_data(nlh);
101728d8909bSJamal Hadi Salim 	*f = flags;
1018e071041bSAlexey Dobriyan 	xfrm_sad_getinfo(net, &si);
101928d8909bSJamal Hadi Salim 
1020af11e316SJamal Hadi Salim 	sh.sadhmcnt = si.sadhmcnt;
1021af11e316SJamal Hadi Salim 	sh.sadhcnt = si.sadhcnt;
1022af11e316SJamal Hadi Salim 
10231d1e34ddSDavid S. Miller 	err = nla_put_u32(skb, XFRMA_SAD_CNT, si.sadcnt);
10241d1e34ddSDavid S. Miller 	if (!err)
10251d1e34ddSDavid S. Miller 		err = nla_put(skb, XFRMA_SAD_HINFO, sizeof(sh), &sh);
10261d1e34ddSDavid S. Miller 	if (err) {
10271d1e34ddSDavid S. Miller 		nlmsg_cancel(skb, nlh);
10281d1e34ddSDavid S. Miller 		return err;
10291d1e34ddSDavid S. Miller 	}
103028d8909bSJamal Hadi Salim 
103128d8909bSJamal Hadi Salim 	return nlmsg_end(skb, nlh);
103228d8909bSJamal Hadi Salim }
103328d8909bSJamal Hadi Salim 
103428d8909bSJamal Hadi Salim static int xfrm_get_sadinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
10355424f32eSThomas Graf 		struct nlattr **attrs)
103628d8909bSJamal Hadi Salim {
1037a6483b79SAlexey Dobriyan 	struct net *net = sock_net(skb->sk);
103828d8909bSJamal Hadi Salim 	struct sk_buff *r_skb;
10397b67c857SThomas Graf 	u32 *flags = nlmsg_data(nlh);
104015e47304SEric W. Biederman 	u32 sportid = NETLINK_CB(skb).portid;
104128d8909bSJamal Hadi Salim 	u32 seq = nlh->nlmsg_seq;
104228d8909bSJamal Hadi Salim 
10437deb2264SThomas Graf 	r_skb = nlmsg_new(xfrm_sadinfo_msgsize(), GFP_ATOMIC);
104428d8909bSJamal Hadi Salim 	if (r_skb == NULL)
104528d8909bSJamal Hadi Salim 		return -ENOMEM;
104628d8909bSJamal Hadi Salim 
104715e47304SEric W. Biederman 	if (build_sadinfo(r_skb, net, sportid, seq, *flags) < 0)
104828d8909bSJamal Hadi Salim 		BUG();
104928d8909bSJamal Hadi Salim 
105015e47304SEric W. Biederman 	return nlmsg_unicast(net->xfrm.nlsk, r_skb, sportid);
105128d8909bSJamal Hadi Salim }
105228d8909bSJamal Hadi Salim 
105322e70050SChristoph Hellwig static int xfrm_get_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
10545424f32eSThomas Graf 		struct nlattr **attrs)
10551da177e4SLinus Torvalds {
1056fc34acd3SAlexey Dobriyan 	struct net *net = sock_net(skb->sk);
10577b67c857SThomas Graf 	struct xfrm_usersa_id *p = nlmsg_data(nlh);
10581da177e4SLinus Torvalds 	struct xfrm_state *x;
10591da177e4SLinus Torvalds 	struct sk_buff *resp_skb;
1060eb2971b6SMasahide NAKAMURA 	int err = -ESRCH;
10611da177e4SLinus Torvalds 
1062fc34acd3SAlexey Dobriyan 	x = xfrm_user_state_lookup(net, p, attrs, &err);
10631da177e4SLinus Torvalds 	if (x == NULL)
10641da177e4SLinus Torvalds 		goto out_noput;
10651da177e4SLinus Torvalds 
10661da177e4SLinus Torvalds 	resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
10671da177e4SLinus Torvalds 	if (IS_ERR(resp_skb)) {
10681da177e4SLinus Torvalds 		err = PTR_ERR(resp_skb);
10691da177e4SLinus Torvalds 	} else {
107015e47304SEric W. Biederman 		err = nlmsg_unicast(net->xfrm.nlsk, resp_skb, NETLINK_CB(skb).portid);
10711da177e4SLinus Torvalds 	}
10721da177e4SLinus Torvalds 	xfrm_state_put(x);
10731da177e4SLinus Torvalds out_noput:
10741da177e4SLinus Torvalds 	return err;
10751da177e4SLinus Torvalds }
10761da177e4SLinus Torvalds 
10771da177e4SLinus Torvalds static int verify_userspi_info(struct xfrm_userspi_info *p)
10781da177e4SLinus Torvalds {
10791da177e4SLinus Torvalds 	switch (p->info.id.proto) {
10801da177e4SLinus Torvalds 	case IPPROTO_AH:
10811da177e4SLinus Torvalds 	case IPPROTO_ESP:
10821da177e4SLinus Torvalds 		break;
10831da177e4SLinus Torvalds 
10841da177e4SLinus Torvalds 	case IPPROTO_COMP:
10851da177e4SLinus Torvalds 		/* IPCOMP spi is 16-bits. */
10861da177e4SLinus Torvalds 		if (p->max >= 0x10000)
10871da177e4SLinus Torvalds 			return -EINVAL;
10881da177e4SLinus Torvalds 		break;
10891da177e4SLinus Torvalds 
10901da177e4SLinus Torvalds 	default:
10911da177e4SLinus Torvalds 		return -EINVAL;
10923ff50b79SStephen Hemminger 	}
10931da177e4SLinus Torvalds 
10941da177e4SLinus Torvalds 	if (p->min > p->max)
10951da177e4SLinus Torvalds 		return -EINVAL;
10961da177e4SLinus Torvalds 
10971da177e4SLinus Torvalds 	return 0;
10981da177e4SLinus Torvalds }
10991da177e4SLinus Torvalds 
110022e70050SChristoph Hellwig static int xfrm_alloc_userspi(struct sk_buff *skb, struct nlmsghdr *nlh,
11015424f32eSThomas Graf 		struct nlattr **attrs)
11021da177e4SLinus Torvalds {
1103fc34acd3SAlexey Dobriyan 	struct net *net = sock_net(skb->sk);
11041da177e4SLinus Torvalds 	struct xfrm_state *x;
11051da177e4SLinus Torvalds 	struct xfrm_userspi_info *p;
11061da177e4SLinus Torvalds 	struct sk_buff *resp_skb;
11071da177e4SLinus Torvalds 	xfrm_address_t *daddr;
11081da177e4SLinus Torvalds 	int family;
11091da177e4SLinus Torvalds 	int err;
11106f26b61eSJamal Hadi Salim 	u32 mark;
11116f26b61eSJamal Hadi Salim 	struct xfrm_mark m;
11121da177e4SLinus Torvalds 
11137b67c857SThomas Graf 	p = nlmsg_data(nlh);
11141da177e4SLinus Torvalds 	err = verify_userspi_info(p);
11151da177e4SLinus Torvalds 	if (err)
11161da177e4SLinus Torvalds 		goto out_noput;
11171da177e4SLinus Torvalds 
11181da177e4SLinus Torvalds 	family = p->info.family;
11191da177e4SLinus Torvalds 	daddr = &p->info.id.daddr;
11201da177e4SLinus Torvalds 
11211da177e4SLinus Torvalds 	x = NULL;
11226f26b61eSJamal Hadi Salim 
11236f26b61eSJamal Hadi Salim 	mark = xfrm_mark_get(attrs, &m);
11241da177e4SLinus Torvalds 	if (p->info.seq) {
11256f26b61eSJamal Hadi Salim 		x = xfrm_find_acq_byseq(net, mark, p->info.seq);
112670e94e66SYOSHIFUJI Hideaki / 吉藤英明 		if (x && !xfrm_addr_equal(&x->id.daddr, daddr, family)) {
11271da177e4SLinus Torvalds 			xfrm_state_put(x);
11281da177e4SLinus Torvalds 			x = NULL;
11291da177e4SLinus Torvalds 		}
11301da177e4SLinus Torvalds 	}
11311da177e4SLinus Torvalds 
11321da177e4SLinus Torvalds 	if (!x)
11336f26b61eSJamal Hadi Salim 		x = xfrm_find_acq(net, &m, p->info.mode, p->info.reqid,
11341da177e4SLinus Torvalds 				  p->info.id.proto, daddr,
11351da177e4SLinus Torvalds 				  &p->info.saddr, 1,
11361da177e4SLinus Torvalds 				  family);
11371da177e4SLinus Torvalds 	err = -ENOENT;
11381da177e4SLinus Torvalds 	if (x == NULL)
11391da177e4SLinus Torvalds 		goto out_noput;
11401da177e4SLinus Torvalds 
1141658b219eSHerbert Xu 	err = xfrm_alloc_spi(x, p->min, p->max);
1142658b219eSHerbert Xu 	if (err)
1143658b219eSHerbert Xu 		goto out;
11441da177e4SLinus Torvalds 
11451da177e4SLinus Torvalds 	resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
11461da177e4SLinus Torvalds 	if (IS_ERR(resp_skb)) {
11471da177e4SLinus Torvalds 		err = PTR_ERR(resp_skb);
11481da177e4SLinus Torvalds 		goto out;
11491da177e4SLinus Torvalds 	}
11501da177e4SLinus Torvalds 
115115e47304SEric W. Biederman 	err = nlmsg_unicast(net->xfrm.nlsk, resp_skb, NETLINK_CB(skb).portid);
11521da177e4SLinus Torvalds 
11531da177e4SLinus Torvalds out:
11541da177e4SLinus Torvalds 	xfrm_state_put(x);
11551da177e4SLinus Torvalds out_noput:
11561da177e4SLinus Torvalds 	return err;
11571da177e4SLinus Torvalds }
11581da177e4SLinus Torvalds 
1159b798a9edSJamal Hadi Salim static int verify_policy_dir(u8 dir)
11601da177e4SLinus Torvalds {
11611da177e4SLinus Torvalds 	switch (dir) {
11621da177e4SLinus Torvalds 	case XFRM_POLICY_IN:
11631da177e4SLinus Torvalds 	case XFRM_POLICY_OUT:
11641da177e4SLinus Torvalds 	case XFRM_POLICY_FWD:
11651da177e4SLinus Torvalds 		break;
11661da177e4SLinus Torvalds 
11671da177e4SLinus Torvalds 	default:
11681da177e4SLinus Torvalds 		return -EINVAL;
11693ff50b79SStephen Hemminger 	}
11701da177e4SLinus Torvalds 
11711da177e4SLinus Torvalds 	return 0;
11721da177e4SLinus Torvalds }
11731da177e4SLinus Torvalds 
1174b798a9edSJamal Hadi Salim static int verify_policy_type(u8 type)
1175f7b6983fSMasahide NAKAMURA {
1176f7b6983fSMasahide NAKAMURA 	switch (type) {
1177f7b6983fSMasahide NAKAMURA 	case XFRM_POLICY_TYPE_MAIN:
1178f7b6983fSMasahide NAKAMURA #ifdef CONFIG_XFRM_SUB_POLICY
1179f7b6983fSMasahide NAKAMURA 	case XFRM_POLICY_TYPE_SUB:
1180f7b6983fSMasahide NAKAMURA #endif
1181f7b6983fSMasahide NAKAMURA 		break;
1182f7b6983fSMasahide NAKAMURA 
1183f7b6983fSMasahide NAKAMURA 	default:
1184f7b6983fSMasahide NAKAMURA 		return -EINVAL;
11853ff50b79SStephen Hemminger 	}
1186f7b6983fSMasahide NAKAMURA 
1187f7b6983fSMasahide NAKAMURA 	return 0;
1188f7b6983fSMasahide NAKAMURA }
1189f7b6983fSMasahide NAKAMURA 
11901da177e4SLinus Torvalds static int verify_newpolicy_info(struct xfrm_userpolicy_info *p)
11911da177e4SLinus Torvalds {
1192e682adf0SFan Du 	int ret;
1193e682adf0SFan Du 
11941da177e4SLinus Torvalds 	switch (p->share) {
11951da177e4SLinus Torvalds 	case XFRM_SHARE_ANY:
11961da177e4SLinus Torvalds 	case XFRM_SHARE_SESSION:
11971da177e4SLinus Torvalds 	case XFRM_SHARE_USER:
11981da177e4SLinus Torvalds 	case XFRM_SHARE_UNIQUE:
11991da177e4SLinus Torvalds 		break;
12001da177e4SLinus Torvalds 
12011da177e4SLinus Torvalds 	default:
12021da177e4SLinus Torvalds 		return -EINVAL;
12033ff50b79SStephen Hemminger 	}
12041da177e4SLinus Torvalds 
12051da177e4SLinus Torvalds 	switch (p->action) {
12061da177e4SLinus Torvalds 	case XFRM_POLICY_ALLOW:
12071da177e4SLinus Torvalds 	case XFRM_POLICY_BLOCK:
12081da177e4SLinus Torvalds 		break;
12091da177e4SLinus Torvalds 
12101da177e4SLinus Torvalds 	default:
12111da177e4SLinus Torvalds 		return -EINVAL;
12123ff50b79SStephen Hemminger 	}
12131da177e4SLinus Torvalds 
12141da177e4SLinus Torvalds 	switch (p->sel.family) {
12151da177e4SLinus Torvalds 	case AF_INET:
12161da177e4SLinus Torvalds 		break;
12171da177e4SLinus Torvalds 
12181da177e4SLinus Torvalds 	case AF_INET6:
1219dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6)
12201da177e4SLinus Torvalds 		break;
12211da177e4SLinus Torvalds #else
12221da177e4SLinus Torvalds 		return  -EAFNOSUPPORT;
12231da177e4SLinus Torvalds #endif
12241da177e4SLinus Torvalds 
12251da177e4SLinus Torvalds 	default:
12261da177e4SLinus Torvalds 		return -EINVAL;
12273ff50b79SStephen Hemminger 	}
12281da177e4SLinus Torvalds 
1229e682adf0SFan Du 	ret = verify_policy_dir(p->dir);
1230e682adf0SFan Du 	if (ret)
1231e682adf0SFan Du 		return ret;
1232e682adf0SFan Du 	if (p->index && ((p->index & XFRM_POLICY_MAX) != p->dir))
1233e682adf0SFan Du 		return -EINVAL;
1234e682adf0SFan Du 
1235e682adf0SFan Du 	return 0;
12361da177e4SLinus Torvalds }
12371da177e4SLinus Torvalds 
12385424f32eSThomas Graf static int copy_from_user_sec_ctx(struct xfrm_policy *pol, struct nlattr **attrs)
1239df71837dSTrent Jaeger {
12405424f32eSThomas Graf 	struct nlattr *rt = attrs[XFRMA_SEC_CTX];
1241df71837dSTrent Jaeger 	struct xfrm_user_sec_ctx *uctx;
1242df71837dSTrent Jaeger 
1243df71837dSTrent Jaeger 	if (!rt)
1244df71837dSTrent Jaeger 		return 0;
1245df71837dSTrent Jaeger 
12465424f32eSThomas Graf 	uctx = nla_data(rt);
124703e1ad7bSPaul Moore 	return security_xfrm_policy_alloc(&pol->security, uctx);
1248df71837dSTrent Jaeger }
1249df71837dSTrent Jaeger 
12501da177e4SLinus Torvalds static void copy_templates(struct xfrm_policy *xp, struct xfrm_user_tmpl *ut,
12511da177e4SLinus Torvalds 			   int nr)
12521da177e4SLinus Torvalds {
12531da177e4SLinus Torvalds 	int i;
12541da177e4SLinus Torvalds 
12551da177e4SLinus Torvalds 	xp->xfrm_nr = nr;
12561da177e4SLinus Torvalds 	for (i = 0; i < nr; i++, ut++) {
12571da177e4SLinus Torvalds 		struct xfrm_tmpl *t = &xp->xfrm_vec[i];
12581da177e4SLinus Torvalds 
12591da177e4SLinus Torvalds 		memcpy(&t->id, &ut->id, sizeof(struct xfrm_id));
12601da177e4SLinus Torvalds 		memcpy(&t->saddr, &ut->saddr,
12611da177e4SLinus Torvalds 		       sizeof(xfrm_address_t));
12621da177e4SLinus Torvalds 		t->reqid = ut->reqid;
12631da177e4SLinus Torvalds 		t->mode = ut->mode;
12641da177e4SLinus Torvalds 		t->share = ut->share;
12651da177e4SLinus Torvalds 		t->optional = ut->optional;
12661da177e4SLinus Torvalds 		t->aalgos = ut->aalgos;
12671da177e4SLinus Torvalds 		t->ealgos = ut->ealgos;
12681da177e4SLinus Torvalds 		t->calgos = ut->calgos;
1269c5d18e98SHerbert Xu 		/* If all masks are ~0, then we allow all algorithms. */
1270c5d18e98SHerbert Xu 		t->allalgs = !~(t->aalgos & t->ealgos & t->calgos);
12718511d01dSMiika Komu 		t->encap_family = ut->family;
12721da177e4SLinus Torvalds 	}
12731da177e4SLinus Torvalds }
12741da177e4SLinus Torvalds 
1275b4ad86bfSDavid S. Miller static int validate_tmpl(int nr, struct xfrm_user_tmpl *ut, u16 family)
1276b4ad86bfSDavid S. Miller {
1277b4ad86bfSDavid S. Miller 	int i;
1278b4ad86bfSDavid S. Miller 
1279b4ad86bfSDavid S. Miller 	if (nr > XFRM_MAX_DEPTH)
1280b4ad86bfSDavid S. Miller 		return -EINVAL;
1281b4ad86bfSDavid S. Miller 
1282b4ad86bfSDavid S. Miller 	for (i = 0; i < nr; i++) {
1283b4ad86bfSDavid S. Miller 		/* We never validated the ut->family value, so many
1284b4ad86bfSDavid S. Miller 		 * applications simply leave it at zero.  The check was
1285b4ad86bfSDavid S. Miller 		 * never made and ut->family was ignored because all
1286b4ad86bfSDavid S. Miller 		 * templates could be assumed to have the same family as
1287b4ad86bfSDavid S. Miller 		 * the policy itself.  Now that we will have ipv4-in-ipv6
1288b4ad86bfSDavid S. Miller 		 * and ipv6-in-ipv4 tunnels, this is no longer true.
1289b4ad86bfSDavid S. Miller 		 */
1290b4ad86bfSDavid S. Miller 		if (!ut[i].family)
1291b4ad86bfSDavid S. Miller 			ut[i].family = family;
1292b4ad86bfSDavid S. Miller 
1293b4ad86bfSDavid S. Miller 		switch (ut[i].family) {
1294b4ad86bfSDavid S. Miller 		case AF_INET:
1295b4ad86bfSDavid S. Miller 			break;
1296dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6)
1297b4ad86bfSDavid S. Miller 		case AF_INET6:
1298b4ad86bfSDavid S. Miller 			break;
1299b4ad86bfSDavid S. Miller #endif
1300b4ad86bfSDavid S. Miller 		default:
1301b4ad86bfSDavid S. Miller 			return -EINVAL;
13023ff50b79SStephen Hemminger 		}
1303b4ad86bfSDavid S. Miller 	}
1304b4ad86bfSDavid S. Miller 
1305b4ad86bfSDavid S. Miller 	return 0;
1306b4ad86bfSDavid S. Miller }
1307b4ad86bfSDavid S. Miller 
13085424f32eSThomas Graf static int copy_from_user_tmpl(struct xfrm_policy *pol, struct nlattr **attrs)
13091da177e4SLinus Torvalds {
13105424f32eSThomas Graf 	struct nlattr *rt = attrs[XFRMA_TMPL];
13111da177e4SLinus Torvalds 
13121da177e4SLinus Torvalds 	if (!rt) {
13131da177e4SLinus Torvalds 		pol->xfrm_nr = 0;
13141da177e4SLinus Torvalds 	} else {
13155424f32eSThomas Graf 		struct xfrm_user_tmpl *utmpl = nla_data(rt);
13165424f32eSThomas Graf 		int nr = nla_len(rt) / sizeof(*utmpl);
1317b4ad86bfSDavid S. Miller 		int err;
13181da177e4SLinus Torvalds 
1319b4ad86bfSDavid S. Miller 		err = validate_tmpl(nr, utmpl, pol->family);
1320b4ad86bfSDavid S. Miller 		if (err)
1321b4ad86bfSDavid S. Miller 			return err;
13221da177e4SLinus Torvalds 
13235424f32eSThomas Graf 		copy_templates(pol, utmpl, nr);
13241da177e4SLinus Torvalds 	}
13251da177e4SLinus Torvalds 	return 0;
13261da177e4SLinus Torvalds }
13271da177e4SLinus Torvalds 
13285424f32eSThomas Graf static int copy_from_user_policy_type(u8 *tp, struct nlattr **attrs)
1329f7b6983fSMasahide NAKAMURA {
13305424f32eSThomas Graf 	struct nlattr *rt = attrs[XFRMA_POLICY_TYPE];
1331f7b6983fSMasahide NAKAMURA 	struct xfrm_userpolicy_type *upt;
1332b798a9edSJamal Hadi Salim 	u8 type = XFRM_POLICY_TYPE_MAIN;
1333f7b6983fSMasahide NAKAMURA 	int err;
1334f7b6983fSMasahide NAKAMURA 
1335f7b6983fSMasahide NAKAMURA 	if (rt) {
13365424f32eSThomas Graf 		upt = nla_data(rt);
1337f7b6983fSMasahide NAKAMURA 		type = upt->type;
1338f7b6983fSMasahide NAKAMURA 	}
1339f7b6983fSMasahide NAKAMURA 
1340f7b6983fSMasahide NAKAMURA 	err = verify_policy_type(type);
1341f7b6983fSMasahide NAKAMURA 	if (err)
1342f7b6983fSMasahide NAKAMURA 		return err;
1343f7b6983fSMasahide NAKAMURA 
1344f7b6983fSMasahide NAKAMURA 	*tp = type;
1345f7b6983fSMasahide NAKAMURA 	return 0;
1346f7b6983fSMasahide NAKAMURA }
1347f7b6983fSMasahide NAKAMURA 
13481da177e4SLinus Torvalds static void copy_from_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p)
13491da177e4SLinus Torvalds {
13501da177e4SLinus Torvalds 	xp->priority = p->priority;
13511da177e4SLinus Torvalds 	xp->index = p->index;
13521da177e4SLinus Torvalds 	memcpy(&xp->selector, &p->sel, sizeof(xp->selector));
13531da177e4SLinus Torvalds 	memcpy(&xp->lft, &p->lft, sizeof(xp->lft));
13541da177e4SLinus Torvalds 	xp->action = p->action;
13551da177e4SLinus Torvalds 	xp->flags = p->flags;
13561da177e4SLinus Torvalds 	xp->family = p->sel.family;
13571da177e4SLinus Torvalds 	/* XXX xp->share = p->share; */
13581da177e4SLinus Torvalds }
13591da177e4SLinus Torvalds 
13601da177e4SLinus Torvalds static void copy_to_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p, int dir)
13611da177e4SLinus Torvalds {
13627b789836SMathias Krause 	memset(p, 0, sizeof(*p));
13631da177e4SLinus Torvalds 	memcpy(&p->sel, &xp->selector, sizeof(p->sel));
13641da177e4SLinus Torvalds 	memcpy(&p->lft, &xp->lft, sizeof(p->lft));
13651da177e4SLinus Torvalds 	memcpy(&p->curlft, &xp->curlft, sizeof(p->curlft));
13661da177e4SLinus Torvalds 	p->priority = xp->priority;
13671da177e4SLinus Torvalds 	p->index = xp->index;
13681da177e4SLinus Torvalds 	p->sel.family = xp->family;
13691da177e4SLinus Torvalds 	p->dir = dir;
13701da177e4SLinus Torvalds 	p->action = xp->action;
13711da177e4SLinus Torvalds 	p->flags = xp->flags;
13721da177e4SLinus Torvalds 	p->share = XFRM_SHARE_ANY; /* XXX xp->share */
13731da177e4SLinus Torvalds }
13741da177e4SLinus Torvalds 
1375fc34acd3SAlexey Dobriyan static struct xfrm_policy *xfrm_policy_construct(struct net *net, struct xfrm_userpolicy_info *p, struct nlattr **attrs, int *errp)
13761da177e4SLinus Torvalds {
1377fc34acd3SAlexey Dobriyan 	struct xfrm_policy *xp = xfrm_policy_alloc(net, GFP_KERNEL);
13781da177e4SLinus Torvalds 	int err;
13791da177e4SLinus Torvalds 
13801da177e4SLinus Torvalds 	if (!xp) {
13811da177e4SLinus Torvalds 		*errp = -ENOMEM;
13821da177e4SLinus Torvalds 		return NULL;
13831da177e4SLinus Torvalds 	}
13841da177e4SLinus Torvalds 
13851da177e4SLinus Torvalds 	copy_from_user_policy(xp, p);
1386df71837dSTrent Jaeger 
138735a7aa08SThomas Graf 	err = copy_from_user_policy_type(&xp->type, attrs);
1388f7b6983fSMasahide NAKAMURA 	if (err)
1389f7b6983fSMasahide NAKAMURA 		goto error;
1390f7b6983fSMasahide NAKAMURA 
139135a7aa08SThomas Graf 	if (!(err = copy_from_user_tmpl(xp, attrs)))
139235a7aa08SThomas Graf 		err = copy_from_user_sec_ctx(xp, attrs);
1393f7b6983fSMasahide NAKAMURA 	if (err)
1394f7b6983fSMasahide NAKAMURA 		goto error;
13951da177e4SLinus Torvalds 
1396295fae56SJamal Hadi Salim 	xfrm_mark_get(attrs, &xp->mark);
1397295fae56SJamal Hadi Salim 
13981da177e4SLinus Torvalds 	return xp;
1399f7b6983fSMasahide NAKAMURA  error:
1400f7b6983fSMasahide NAKAMURA 	*errp = err;
140112a169e7SHerbert Xu 	xp->walk.dead = 1;
140264c31b3fSWANG Cong 	xfrm_policy_destroy(xp);
1403f7b6983fSMasahide NAKAMURA 	return NULL;
14041da177e4SLinus Torvalds }
14051da177e4SLinus Torvalds 
140622e70050SChristoph Hellwig static int xfrm_add_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
14075424f32eSThomas Graf 		struct nlattr **attrs)
14081da177e4SLinus Torvalds {
1409fc34acd3SAlexey Dobriyan 	struct net *net = sock_net(skb->sk);
14107b67c857SThomas Graf 	struct xfrm_userpolicy_info *p = nlmsg_data(nlh);
14111da177e4SLinus Torvalds 	struct xfrm_policy *xp;
141226b15dadSJamal Hadi Salim 	struct km_event c;
14131da177e4SLinus Torvalds 	int err;
14141da177e4SLinus Torvalds 	int excl;
1415e1760bd5SEric W. Biederman 	kuid_t loginuid = audit_get_loginuid(current);
1416c53fa1edSPatrick McHardy 	u32 sessionid = audit_get_sessionid(current);
1417c53fa1edSPatrick McHardy 	u32 sid;
14181da177e4SLinus Torvalds 
14191da177e4SLinus Torvalds 	err = verify_newpolicy_info(p);
14201da177e4SLinus Torvalds 	if (err)
14211da177e4SLinus Torvalds 		return err;
142235a7aa08SThomas Graf 	err = verify_sec_ctx_len(attrs);
1423df71837dSTrent Jaeger 	if (err)
1424df71837dSTrent Jaeger 		return err;
14251da177e4SLinus Torvalds 
1426fc34acd3SAlexey Dobriyan 	xp = xfrm_policy_construct(net, p, attrs, &err);
14271da177e4SLinus Torvalds 	if (!xp)
14281da177e4SLinus Torvalds 		return err;
14291da177e4SLinus Torvalds 
143025985edcSLucas De Marchi 	/* shouldn't excl be based on nlh flags??
143126b15dadSJamal Hadi Salim 	 * Aha! this is anti-netlink really i.e  more pfkey derived
143226b15dadSJamal Hadi Salim 	 * in netlink excl is a flag and you wouldnt need
143326b15dadSJamal Hadi Salim 	 * a type XFRM_MSG_UPDPOLICY - JHS */
14341da177e4SLinus Torvalds 	excl = nlh->nlmsg_type == XFRM_MSG_NEWPOLICY;
14351da177e4SLinus Torvalds 	err = xfrm_policy_insert(p->dir, xp, excl);
1436c53fa1edSPatrick McHardy 	security_task_getsecid(current, &sid);
14372532386fSEric Paris 	xfrm_audit_policy_add(xp, err ? 0 : 1, loginuid, sessionid, sid);
1438161a09e7SJoy Latten 
14391da177e4SLinus Torvalds 	if (err) {
144003e1ad7bSPaul Moore 		security_xfrm_policy_free(xp->security);
14411da177e4SLinus Torvalds 		kfree(xp);
14421da177e4SLinus Torvalds 		return err;
14431da177e4SLinus Torvalds 	}
14441da177e4SLinus Torvalds 
1445f60f6b8fSHerbert Xu 	c.event = nlh->nlmsg_type;
144626b15dadSJamal Hadi Salim 	c.seq = nlh->nlmsg_seq;
144715e47304SEric W. Biederman 	c.portid = nlh->nlmsg_pid;
144826b15dadSJamal Hadi Salim 	km_policy_notify(xp, p->dir, &c);
144926b15dadSJamal Hadi Salim 
14501da177e4SLinus Torvalds 	xfrm_pol_put(xp);
14511da177e4SLinus Torvalds 
14521da177e4SLinus Torvalds 	return 0;
14531da177e4SLinus Torvalds }
14541da177e4SLinus Torvalds 
14551da177e4SLinus Torvalds static int copy_to_user_tmpl(struct xfrm_policy *xp, struct sk_buff *skb)
14561da177e4SLinus Torvalds {
14571da177e4SLinus Torvalds 	struct xfrm_user_tmpl vec[XFRM_MAX_DEPTH];
14581da177e4SLinus Torvalds 	int i;
14591da177e4SLinus Torvalds 
14601da177e4SLinus Torvalds 	if (xp->xfrm_nr == 0)
14611da177e4SLinus Torvalds 		return 0;
14621da177e4SLinus Torvalds 
14631da177e4SLinus Torvalds 	for (i = 0; i < xp->xfrm_nr; i++) {
14641da177e4SLinus Torvalds 		struct xfrm_user_tmpl *up = &vec[i];
14651da177e4SLinus Torvalds 		struct xfrm_tmpl *kp = &xp->xfrm_vec[i];
14661da177e4SLinus Torvalds 
14671f86840fSMathias Krause 		memset(up, 0, sizeof(*up));
14681da177e4SLinus Torvalds 		memcpy(&up->id, &kp->id, sizeof(up->id));
14698511d01dSMiika Komu 		up->family = kp->encap_family;
14701da177e4SLinus Torvalds 		memcpy(&up->saddr, &kp->saddr, sizeof(up->saddr));
14711da177e4SLinus Torvalds 		up->reqid = kp->reqid;
14721da177e4SLinus Torvalds 		up->mode = kp->mode;
14731da177e4SLinus Torvalds 		up->share = kp->share;
14741da177e4SLinus Torvalds 		up->optional = kp->optional;
14751da177e4SLinus Torvalds 		up->aalgos = kp->aalgos;
14761da177e4SLinus Torvalds 		up->ealgos = kp->ealgos;
14771da177e4SLinus Torvalds 		up->calgos = kp->calgos;
14781da177e4SLinus Torvalds 	}
14791da177e4SLinus Torvalds 
1480c0144beaSThomas Graf 	return nla_put(skb, XFRMA_TMPL,
1481c0144beaSThomas Graf 		       sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr, vec);
1482df71837dSTrent Jaeger }
1483df71837dSTrent Jaeger 
14840d681623SSerge Hallyn static inline int copy_to_user_state_sec_ctx(struct xfrm_state *x, struct sk_buff *skb)
14850d681623SSerge Hallyn {
14860d681623SSerge Hallyn 	if (x->security) {
14870d681623SSerge Hallyn 		return copy_sec_ctx(x->security, skb);
14880d681623SSerge Hallyn 	}
14890d681623SSerge Hallyn 	return 0;
14900d681623SSerge Hallyn }
14910d681623SSerge Hallyn 
14920d681623SSerge Hallyn static inline int copy_to_user_sec_ctx(struct xfrm_policy *xp, struct sk_buff *skb)
14930d681623SSerge Hallyn {
14941d1e34ddSDavid S. Miller 	if (xp->security)
14950d681623SSerge Hallyn 		return copy_sec_ctx(xp->security, skb);
14960d681623SSerge Hallyn 	return 0;
14970d681623SSerge Hallyn }
1498cfbfd45aSThomas Graf static inline size_t userpolicy_type_attrsize(void)
1499cfbfd45aSThomas Graf {
1500cfbfd45aSThomas Graf #ifdef CONFIG_XFRM_SUB_POLICY
1501cfbfd45aSThomas Graf 	return nla_total_size(sizeof(struct xfrm_userpolicy_type));
1502cfbfd45aSThomas Graf #else
1503cfbfd45aSThomas Graf 	return 0;
1504cfbfd45aSThomas Graf #endif
1505cfbfd45aSThomas Graf }
15060d681623SSerge Hallyn 
1507f7b6983fSMasahide NAKAMURA #ifdef CONFIG_XFRM_SUB_POLICY
1508b798a9edSJamal Hadi Salim static int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
1509f7b6983fSMasahide NAKAMURA {
1510c0144beaSThomas Graf 	struct xfrm_userpolicy_type upt = {
1511c0144beaSThomas Graf 		.type = type,
1512c0144beaSThomas Graf 	};
1513f7b6983fSMasahide NAKAMURA 
1514c0144beaSThomas Graf 	return nla_put(skb, XFRMA_POLICY_TYPE, sizeof(upt), &upt);
1515f7b6983fSMasahide NAKAMURA }
1516f7b6983fSMasahide NAKAMURA 
1517f7b6983fSMasahide NAKAMURA #else
1518b798a9edSJamal Hadi Salim static inline int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
1519f7b6983fSMasahide NAKAMURA {
1520f7b6983fSMasahide NAKAMURA 	return 0;
1521f7b6983fSMasahide NAKAMURA }
1522f7b6983fSMasahide NAKAMURA #endif
1523f7b6983fSMasahide NAKAMURA 
15241da177e4SLinus Torvalds static int dump_one_policy(struct xfrm_policy *xp, int dir, int count, void *ptr)
15251da177e4SLinus Torvalds {
15261da177e4SLinus Torvalds 	struct xfrm_dump_info *sp = ptr;
15271da177e4SLinus Torvalds 	struct xfrm_userpolicy_info *p;
15281da177e4SLinus Torvalds 	struct sk_buff *in_skb = sp->in_skb;
15291da177e4SLinus Torvalds 	struct sk_buff *skb = sp->out_skb;
15301da177e4SLinus Torvalds 	struct nlmsghdr *nlh;
15311d1e34ddSDavid S. Miller 	int err;
15321da177e4SLinus Torvalds 
153315e47304SEric W. Biederman 	nlh = nlmsg_put(skb, NETLINK_CB(in_skb).portid, sp->nlmsg_seq,
153479b8b7f4SThomas Graf 			XFRM_MSG_NEWPOLICY, sizeof(*p), sp->nlmsg_flags);
153579b8b7f4SThomas Graf 	if (nlh == NULL)
153679b8b7f4SThomas Graf 		return -EMSGSIZE;
15371da177e4SLinus Torvalds 
15387b67c857SThomas Graf 	p = nlmsg_data(nlh);
15391da177e4SLinus Torvalds 	copy_to_user_policy(xp, p, dir);
15401d1e34ddSDavid S. Miller 	err = copy_to_user_tmpl(xp, skb);
15411d1e34ddSDavid S. Miller 	if (!err)
15421d1e34ddSDavid S. Miller 		err = copy_to_user_sec_ctx(xp, skb);
15431d1e34ddSDavid S. Miller 	if (!err)
15441d1e34ddSDavid S. Miller 		err = copy_to_user_policy_type(xp->type, skb);
15451d1e34ddSDavid S. Miller 	if (!err)
15461d1e34ddSDavid S. Miller 		err = xfrm_mark_put(skb, &xp->mark);
15471d1e34ddSDavid S. Miller 	if (err) {
15481d1e34ddSDavid S. Miller 		nlmsg_cancel(skb, nlh);
15491d1e34ddSDavid S. Miller 		return err;
15501d1e34ddSDavid S. Miller 	}
15519825069dSThomas Graf 	nlmsg_end(skb, nlh);
15521da177e4SLinus Torvalds 	return 0;
15531da177e4SLinus Torvalds }
15541da177e4SLinus Torvalds 
15554c563f76STimo Teras static int xfrm_dump_policy_done(struct netlink_callback *cb)
15564c563f76STimo Teras {
15574c563f76STimo Teras 	struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *) &cb->args[1];
15584c563f76STimo Teras 
15594c563f76STimo Teras 	xfrm_policy_walk_done(walk);
15604c563f76STimo Teras 	return 0;
15614c563f76STimo Teras }
15624c563f76STimo Teras 
15631da177e4SLinus Torvalds static int xfrm_dump_policy(struct sk_buff *skb, struct netlink_callback *cb)
15641da177e4SLinus Torvalds {
1565fc34acd3SAlexey Dobriyan 	struct net *net = sock_net(skb->sk);
15664c563f76STimo Teras 	struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *) &cb->args[1];
15671da177e4SLinus Torvalds 	struct xfrm_dump_info info;
15681da177e4SLinus Torvalds 
15694c563f76STimo Teras 	BUILD_BUG_ON(sizeof(struct xfrm_policy_walk) >
15704c563f76STimo Teras 		     sizeof(cb->args) - sizeof(cb->args[0]));
15714c563f76STimo Teras 
15721da177e4SLinus Torvalds 	info.in_skb = cb->skb;
15731da177e4SLinus Torvalds 	info.out_skb = skb;
15741da177e4SLinus Torvalds 	info.nlmsg_seq = cb->nlh->nlmsg_seq;
15751da177e4SLinus Torvalds 	info.nlmsg_flags = NLM_F_MULTI;
15764c563f76STimo Teras 
15774c563f76STimo Teras 	if (!cb->args[0]) {
15784c563f76STimo Teras 		cb->args[0] = 1;
15794c563f76STimo Teras 		xfrm_policy_walk_init(walk, XFRM_POLICY_TYPE_ANY);
15804c563f76STimo Teras 	}
15814c563f76STimo Teras 
1582fc34acd3SAlexey Dobriyan 	(void) xfrm_policy_walk(net, walk, dump_one_policy, &info);
15831da177e4SLinus Torvalds 
15841da177e4SLinus Torvalds 	return skb->len;
15851da177e4SLinus Torvalds }
15861da177e4SLinus Torvalds 
15871da177e4SLinus Torvalds static struct sk_buff *xfrm_policy_netlink(struct sk_buff *in_skb,
15881da177e4SLinus Torvalds 					  struct xfrm_policy *xp,
15891da177e4SLinus Torvalds 					  int dir, u32 seq)
15901da177e4SLinus Torvalds {
15911da177e4SLinus Torvalds 	struct xfrm_dump_info info;
15921da177e4SLinus Torvalds 	struct sk_buff *skb;
1593c2546372SMathias Krause 	int err;
15941da177e4SLinus Torvalds 
15957deb2264SThomas Graf 	skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
15961da177e4SLinus Torvalds 	if (!skb)
15971da177e4SLinus Torvalds 		return ERR_PTR(-ENOMEM);
15981da177e4SLinus Torvalds 
15991da177e4SLinus Torvalds 	info.in_skb = in_skb;
16001da177e4SLinus Torvalds 	info.out_skb = skb;
16011da177e4SLinus Torvalds 	info.nlmsg_seq = seq;
16021da177e4SLinus Torvalds 	info.nlmsg_flags = 0;
16031da177e4SLinus Torvalds 
1604c2546372SMathias Krause 	err = dump_one_policy(xp, dir, 0, &info);
1605c2546372SMathias Krause 	if (err) {
16061da177e4SLinus Torvalds 		kfree_skb(skb);
1607c2546372SMathias Krause 		return ERR_PTR(err);
16081da177e4SLinus Torvalds 	}
16091da177e4SLinus Torvalds 
16101da177e4SLinus Torvalds 	return skb;
16111da177e4SLinus Torvalds }
16121da177e4SLinus Torvalds 
161322e70050SChristoph Hellwig static int xfrm_get_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
16145424f32eSThomas Graf 		struct nlattr **attrs)
16151da177e4SLinus Torvalds {
1616fc34acd3SAlexey Dobriyan 	struct net *net = sock_net(skb->sk);
16171da177e4SLinus Torvalds 	struct xfrm_policy *xp;
16181da177e4SLinus Torvalds 	struct xfrm_userpolicy_id *p;
1619b798a9edSJamal Hadi Salim 	u8 type = XFRM_POLICY_TYPE_MAIN;
16201da177e4SLinus Torvalds 	int err;
162126b15dadSJamal Hadi Salim 	struct km_event c;
16221da177e4SLinus Torvalds 	int delete;
1623295fae56SJamal Hadi Salim 	struct xfrm_mark m;
1624295fae56SJamal Hadi Salim 	u32 mark = xfrm_mark_get(attrs, &m);
16251da177e4SLinus Torvalds 
16267b67c857SThomas Graf 	p = nlmsg_data(nlh);
16271da177e4SLinus Torvalds 	delete = nlh->nlmsg_type == XFRM_MSG_DELPOLICY;
16281da177e4SLinus Torvalds 
162935a7aa08SThomas Graf 	err = copy_from_user_policy_type(&type, attrs);
1630f7b6983fSMasahide NAKAMURA 	if (err)
1631f7b6983fSMasahide NAKAMURA 		return err;
1632f7b6983fSMasahide NAKAMURA 
16331da177e4SLinus Torvalds 	err = verify_policy_dir(p->dir);
16341da177e4SLinus Torvalds 	if (err)
16351da177e4SLinus Torvalds 		return err;
16361da177e4SLinus Torvalds 
16371da177e4SLinus Torvalds 	if (p->index)
1638295fae56SJamal Hadi Salim 		xp = xfrm_policy_byid(net, mark, type, p->dir, p->index, delete, &err);
1639df71837dSTrent Jaeger 	else {
16405424f32eSThomas Graf 		struct nlattr *rt = attrs[XFRMA_SEC_CTX];
164103e1ad7bSPaul Moore 		struct xfrm_sec_ctx *ctx;
1642df71837dSTrent Jaeger 
164335a7aa08SThomas Graf 		err = verify_sec_ctx_len(attrs);
1644df71837dSTrent Jaeger 		if (err)
1645df71837dSTrent Jaeger 			return err;
1646df71837dSTrent Jaeger 
16472c8dd116SDenis V. Lunev 		ctx = NULL;
1648df71837dSTrent Jaeger 		if (rt) {
16495424f32eSThomas Graf 			struct xfrm_user_sec_ctx *uctx = nla_data(rt);
1650df71837dSTrent Jaeger 
165103e1ad7bSPaul Moore 			err = security_xfrm_policy_alloc(&ctx, uctx);
165203e1ad7bSPaul Moore 			if (err)
1653df71837dSTrent Jaeger 				return err;
16542c8dd116SDenis V. Lunev 		}
1655295fae56SJamal Hadi Salim 		xp = xfrm_policy_bysel_ctx(net, mark, type, p->dir, &p->sel,
16566f26b61eSJamal Hadi Salim 					   ctx, delete, &err);
165703e1ad7bSPaul Moore 		security_xfrm_policy_free(ctx);
1658df71837dSTrent Jaeger 	}
16591da177e4SLinus Torvalds 	if (xp == NULL)
16601da177e4SLinus Torvalds 		return -ENOENT;
16611da177e4SLinus Torvalds 
16621da177e4SLinus Torvalds 	if (!delete) {
16631da177e4SLinus Torvalds 		struct sk_buff *resp_skb;
16641da177e4SLinus Torvalds 
16651da177e4SLinus Torvalds 		resp_skb = xfrm_policy_netlink(skb, xp, p->dir, nlh->nlmsg_seq);
16661da177e4SLinus Torvalds 		if (IS_ERR(resp_skb)) {
16671da177e4SLinus Torvalds 			err = PTR_ERR(resp_skb);
16681da177e4SLinus Torvalds 		} else {
1669a6483b79SAlexey Dobriyan 			err = nlmsg_unicast(net->xfrm.nlsk, resp_skb,
167015e47304SEric W. Biederman 					    NETLINK_CB(skb).portid);
16711da177e4SLinus Torvalds 		}
167226b15dadSJamal Hadi Salim 	} else {
1673e1760bd5SEric W. Biederman 		kuid_t loginuid = audit_get_loginuid(current);
1674c53fa1edSPatrick McHardy 		u32 sessionid = audit_get_sessionid(current);
1675c53fa1edSPatrick McHardy 		u32 sid;
16762532386fSEric Paris 
1677c53fa1edSPatrick McHardy 		security_task_getsecid(current, &sid);
16782532386fSEric Paris 		xfrm_audit_policy_delete(xp, err ? 0 : 1, loginuid, sessionid,
16792532386fSEric Paris 					 sid);
168013fcfbb0SDavid S. Miller 
168113fcfbb0SDavid S. Miller 		if (err != 0)
1682c8c05a8eSCatherine Zhang 			goto out;
168313fcfbb0SDavid S. Miller 
1684e7443892SHerbert Xu 		c.data.byid = p->index;
1685f60f6b8fSHerbert Xu 		c.event = nlh->nlmsg_type;
168626b15dadSJamal Hadi Salim 		c.seq = nlh->nlmsg_seq;
168715e47304SEric W. Biederman 		c.portid = nlh->nlmsg_pid;
168826b15dadSJamal Hadi Salim 		km_policy_notify(xp, p->dir, &c);
16891da177e4SLinus Torvalds 	}
16901da177e4SLinus Torvalds 
1691c8c05a8eSCatherine Zhang out:
1692ef41aaa0SEric Paris 	xfrm_pol_put(xp);
1693e4c17216SPaul Moore 	if (delete && err == 0)
1694e4c17216SPaul Moore 		xfrm_garbage_collect(net);
16951da177e4SLinus Torvalds 	return err;
16961da177e4SLinus Torvalds }
16971da177e4SLinus Torvalds 
169822e70050SChristoph Hellwig static int xfrm_flush_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
16995424f32eSThomas Graf 		struct nlattr **attrs)
17001da177e4SLinus Torvalds {
1701fc34acd3SAlexey Dobriyan 	struct net *net = sock_net(skb->sk);
170226b15dadSJamal Hadi Salim 	struct km_event c;
17037b67c857SThomas Graf 	struct xfrm_usersa_flush *p = nlmsg_data(nlh);
1704161a09e7SJoy Latten 	struct xfrm_audit audit_info;
17054aa2e62cSJoy Latten 	int err;
17061da177e4SLinus Torvalds 
1707c53fa1edSPatrick McHardy 	audit_info.loginuid = audit_get_loginuid(current);
1708c53fa1edSPatrick McHardy 	audit_info.sessionid = audit_get_sessionid(current);
1709c53fa1edSPatrick McHardy 	security_task_getsecid(current, &audit_info.secid);
1710fc34acd3SAlexey Dobriyan 	err = xfrm_state_flush(net, p->proto, &audit_info);
17119e64cc95SJamal Hadi Salim 	if (err) {
17129e64cc95SJamal Hadi Salim 		if (err == -ESRCH) /* empty table */
17139e64cc95SJamal Hadi Salim 			return 0;
1714069c474eSDavid S. Miller 		return err;
17159e64cc95SJamal Hadi Salim 	}
1716bf08867fSHerbert Xu 	c.data.proto = p->proto;
1717f60f6b8fSHerbert Xu 	c.event = nlh->nlmsg_type;
171826b15dadSJamal Hadi Salim 	c.seq = nlh->nlmsg_seq;
171915e47304SEric W. Biederman 	c.portid = nlh->nlmsg_pid;
17207067802eSAlexey Dobriyan 	c.net = net;
172126b15dadSJamal Hadi Salim 	km_state_notify(NULL, &c);
172226b15dadSJamal Hadi Salim 
17231da177e4SLinus Torvalds 	return 0;
17241da177e4SLinus Torvalds }
17251da177e4SLinus Torvalds 
1726d8647b79SSteffen Klassert static inline size_t xfrm_aevent_msgsize(struct xfrm_state *x)
17277deb2264SThomas Graf {
1728d8647b79SSteffen Klassert 	size_t replay_size = x->replay_esn ?
1729d8647b79SSteffen Klassert 			      xfrm_replay_state_esn_len(x->replay_esn) :
1730d8647b79SSteffen Klassert 			      sizeof(struct xfrm_replay_state);
1731d8647b79SSteffen Klassert 
17327deb2264SThomas Graf 	return NLMSG_ALIGN(sizeof(struct xfrm_aevent_id))
1733d8647b79SSteffen Klassert 	       + nla_total_size(replay_size)
17347deb2264SThomas Graf 	       + nla_total_size(sizeof(struct xfrm_lifetime_cur))
17356f26b61eSJamal Hadi Salim 	       + nla_total_size(sizeof(struct xfrm_mark))
17367deb2264SThomas Graf 	       + nla_total_size(4) /* XFRM_AE_RTHR */
17377deb2264SThomas Graf 	       + nla_total_size(4); /* XFRM_AE_ETHR */
17387deb2264SThomas Graf }
1739d51d081dSJamal Hadi Salim 
1740214e005bSDavid S. Miller static int build_aevent(struct sk_buff *skb, struct xfrm_state *x, const struct km_event *c)
1741d51d081dSJamal Hadi Salim {
1742d51d081dSJamal Hadi Salim 	struct xfrm_aevent_id *id;
1743d51d081dSJamal Hadi Salim 	struct nlmsghdr *nlh;
17441d1e34ddSDavid S. Miller 	int err;
1745d51d081dSJamal Hadi Salim 
174615e47304SEric W. Biederman 	nlh = nlmsg_put(skb, c->portid, c->seq, XFRM_MSG_NEWAE, sizeof(*id), 0);
174779b8b7f4SThomas Graf 	if (nlh == NULL)
174879b8b7f4SThomas Graf 		return -EMSGSIZE;
1749d51d081dSJamal Hadi Salim 
17507b67c857SThomas Graf 	id = nlmsg_data(nlh);
17512b5f6dccSJamal Hadi Salim 	memcpy(&id->sa_id.daddr, &x->id.daddr,sizeof(x->id.daddr));
1752d51d081dSJamal Hadi Salim 	id->sa_id.spi = x->id.spi;
1753d51d081dSJamal Hadi Salim 	id->sa_id.family = x->props.family;
1754d51d081dSJamal Hadi Salim 	id->sa_id.proto = x->id.proto;
17552b5f6dccSJamal Hadi Salim 	memcpy(&id->saddr, &x->props.saddr,sizeof(x->props.saddr));
17562b5f6dccSJamal Hadi Salim 	id->reqid = x->props.reqid;
1757d51d081dSJamal Hadi Salim 	id->flags = c->data.aevent;
1758d51d081dSJamal Hadi Salim 
1759d0fde795SDavid S. Miller 	if (x->replay_esn) {
17601d1e34ddSDavid S. Miller 		err = nla_put(skb, XFRMA_REPLAY_ESN_VAL,
1761d8647b79SSteffen Klassert 			      xfrm_replay_state_esn_len(x->replay_esn),
17621d1e34ddSDavid S. Miller 			      x->replay_esn);
1763d0fde795SDavid S. Miller 	} else {
17641d1e34ddSDavid S. Miller 		err = nla_put(skb, XFRMA_REPLAY_VAL, sizeof(x->replay),
17651d1e34ddSDavid S. Miller 			      &x->replay);
1766d0fde795SDavid S. Miller 	}
17671d1e34ddSDavid S. Miller 	if (err)
17681d1e34ddSDavid S. Miller 		goto out_cancel;
17691d1e34ddSDavid S. Miller 	err = nla_put(skb, XFRMA_LTIME_VAL, sizeof(x->curlft), &x->curlft);
17701d1e34ddSDavid S. Miller 	if (err)
17711d1e34ddSDavid S. Miller 		goto out_cancel;
1772d8647b79SSteffen Klassert 
17731d1e34ddSDavid S. Miller 	if (id->flags & XFRM_AE_RTHR) {
17741d1e34ddSDavid S. Miller 		err = nla_put_u32(skb, XFRMA_REPLAY_THRESH, x->replay_maxdiff);
17751d1e34ddSDavid S. Miller 		if (err)
17761d1e34ddSDavid S. Miller 			goto out_cancel;
17771d1e34ddSDavid S. Miller 	}
17781d1e34ddSDavid S. Miller 	if (id->flags & XFRM_AE_ETHR) {
17791d1e34ddSDavid S. Miller 		err = nla_put_u32(skb, XFRMA_ETIMER_THRESH,
17801d1e34ddSDavid S. Miller 				  x->replay_maxage * 10 / HZ);
17811d1e34ddSDavid S. Miller 		if (err)
17821d1e34ddSDavid S. Miller 			goto out_cancel;
17831d1e34ddSDavid S. Miller 	}
17841d1e34ddSDavid S. Miller 	err = xfrm_mark_put(skb, &x->mark);
17851d1e34ddSDavid S. Miller 	if (err)
17861d1e34ddSDavid S. Miller 		goto out_cancel;
17876f26b61eSJamal Hadi Salim 
17889825069dSThomas Graf 	return nlmsg_end(skb, nlh);
1789d51d081dSJamal Hadi Salim 
17901d1e34ddSDavid S. Miller out_cancel:
17919825069dSThomas Graf 	nlmsg_cancel(skb, nlh);
17921d1e34ddSDavid S. Miller 	return err;
1793d51d081dSJamal Hadi Salim }
1794d51d081dSJamal Hadi Salim 
179522e70050SChristoph Hellwig static int xfrm_get_ae(struct sk_buff *skb, struct nlmsghdr *nlh,
17965424f32eSThomas Graf 		struct nlattr **attrs)
1797d51d081dSJamal Hadi Salim {
1798fc34acd3SAlexey Dobriyan 	struct net *net = sock_net(skb->sk);
1799d51d081dSJamal Hadi Salim 	struct xfrm_state *x;
1800d51d081dSJamal Hadi Salim 	struct sk_buff *r_skb;
1801d51d081dSJamal Hadi Salim 	int err;
1802d51d081dSJamal Hadi Salim 	struct km_event c;
18036f26b61eSJamal Hadi Salim 	u32 mark;
18046f26b61eSJamal Hadi Salim 	struct xfrm_mark m;
18057b67c857SThomas Graf 	struct xfrm_aevent_id *p = nlmsg_data(nlh);
1806d51d081dSJamal Hadi Salim 	struct xfrm_usersa_id *id = &p->sa_id;
1807d51d081dSJamal Hadi Salim 
18086f26b61eSJamal Hadi Salim 	mark = xfrm_mark_get(attrs, &m);
18096f26b61eSJamal Hadi Salim 
18106f26b61eSJamal Hadi Salim 	x = xfrm_state_lookup(net, mark, &id->daddr, id->spi, id->proto, id->family);
1811d8647b79SSteffen Klassert 	if (x == NULL)
1812d51d081dSJamal Hadi Salim 		return -ESRCH;
1813d8647b79SSteffen Klassert 
1814d8647b79SSteffen Klassert 	r_skb = nlmsg_new(xfrm_aevent_msgsize(x), GFP_ATOMIC);
1815d8647b79SSteffen Klassert 	if (r_skb == NULL) {
1816d8647b79SSteffen Klassert 		xfrm_state_put(x);
1817d8647b79SSteffen Klassert 		return -ENOMEM;
1818d51d081dSJamal Hadi Salim 	}
1819d51d081dSJamal Hadi Salim 
1820d51d081dSJamal Hadi Salim 	/*
1821d51d081dSJamal Hadi Salim 	 * XXX: is this lock really needed - none of the other
1822d51d081dSJamal Hadi Salim 	 * gets lock (the concern is things getting updated
1823d51d081dSJamal Hadi Salim 	 * while we are still reading) - jhs
1824d51d081dSJamal Hadi Salim 	*/
1825d51d081dSJamal Hadi Salim 	spin_lock_bh(&x->lock);
1826d51d081dSJamal Hadi Salim 	c.data.aevent = p->flags;
1827d51d081dSJamal Hadi Salim 	c.seq = nlh->nlmsg_seq;
182815e47304SEric W. Biederman 	c.portid = nlh->nlmsg_pid;
1829d51d081dSJamal Hadi Salim 
1830d51d081dSJamal Hadi Salim 	if (build_aevent(r_skb, x, &c) < 0)
1831d51d081dSJamal Hadi Salim 		BUG();
183215e47304SEric W. Biederman 	err = nlmsg_unicast(net->xfrm.nlsk, r_skb, NETLINK_CB(skb).portid);
1833d51d081dSJamal Hadi Salim 	spin_unlock_bh(&x->lock);
1834d51d081dSJamal Hadi Salim 	xfrm_state_put(x);
1835d51d081dSJamal Hadi Salim 	return err;
1836d51d081dSJamal Hadi Salim }
1837d51d081dSJamal Hadi Salim 
183822e70050SChristoph Hellwig static int xfrm_new_ae(struct sk_buff *skb, struct nlmsghdr *nlh,
18395424f32eSThomas Graf 		struct nlattr **attrs)
1840d51d081dSJamal Hadi Salim {
1841fc34acd3SAlexey Dobriyan 	struct net *net = sock_net(skb->sk);
1842d51d081dSJamal Hadi Salim 	struct xfrm_state *x;
1843d51d081dSJamal Hadi Salim 	struct km_event c;
1844d51d081dSJamal Hadi Salim 	int err = - EINVAL;
18456f26b61eSJamal Hadi Salim 	u32 mark = 0;
18466f26b61eSJamal Hadi Salim 	struct xfrm_mark m;
18477b67c857SThomas Graf 	struct xfrm_aevent_id *p = nlmsg_data(nlh);
18485424f32eSThomas Graf 	struct nlattr *rp = attrs[XFRMA_REPLAY_VAL];
1849d8647b79SSteffen Klassert 	struct nlattr *re = attrs[XFRMA_REPLAY_ESN_VAL];
18505424f32eSThomas Graf 	struct nlattr *lt = attrs[XFRMA_LTIME_VAL];
1851d51d081dSJamal Hadi Salim 
1852d8647b79SSteffen Klassert 	if (!lt && !rp && !re)
1853d51d081dSJamal Hadi Salim 		return err;
1854d51d081dSJamal Hadi Salim 
1855d51d081dSJamal Hadi Salim 	/* pedantic mode - thou shalt sayeth replaceth */
1856d51d081dSJamal Hadi Salim 	if (!(nlh->nlmsg_flags&NLM_F_REPLACE))
1857d51d081dSJamal Hadi Salim 		return err;
1858d51d081dSJamal Hadi Salim 
18596f26b61eSJamal Hadi Salim 	mark = xfrm_mark_get(attrs, &m);
18606f26b61eSJamal Hadi Salim 
18616f26b61eSJamal Hadi Salim 	x = xfrm_state_lookup(net, mark, &p->sa_id.daddr, p->sa_id.spi, p->sa_id.proto, p->sa_id.family);
1862d51d081dSJamal Hadi Salim 	if (x == NULL)
1863d51d081dSJamal Hadi Salim 		return -ESRCH;
1864d51d081dSJamal Hadi Salim 
1865d51d081dSJamal Hadi Salim 	if (x->km.state != XFRM_STATE_VALID)
1866d51d081dSJamal Hadi Salim 		goto out;
1867d51d081dSJamal Hadi Salim 
18684479ff76SSteffen Klassert 	err = xfrm_replay_verify_len(x->replay_esn, re);
1869e2b19125SSteffen Klassert 	if (err)
1870e2b19125SSteffen Klassert 		goto out;
1871e2b19125SSteffen Klassert 
1872d51d081dSJamal Hadi Salim 	spin_lock_bh(&x->lock);
1873e3ac104dSMathias Krause 	xfrm_update_ae_params(x, attrs, 1);
1874d51d081dSJamal Hadi Salim 	spin_unlock_bh(&x->lock);
1875d51d081dSJamal Hadi Salim 
1876d51d081dSJamal Hadi Salim 	c.event = nlh->nlmsg_type;
1877d51d081dSJamal Hadi Salim 	c.seq = nlh->nlmsg_seq;
187815e47304SEric W. Biederman 	c.portid = nlh->nlmsg_pid;
1879d51d081dSJamal Hadi Salim 	c.data.aevent = XFRM_AE_CU;
1880d51d081dSJamal Hadi Salim 	km_state_notify(x, &c);
1881d51d081dSJamal Hadi Salim 	err = 0;
1882d51d081dSJamal Hadi Salim out:
1883d51d081dSJamal Hadi Salim 	xfrm_state_put(x);
1884d51d081dSJamal Hadi Salim 	return err;
1885d51d081dSJamal Hadi Salim }
1886d51d081dSJamal Hadi Salim 
188722e70050SChristoph Hellwig static int xfrm_flush_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
18885424f32eSThomas Graf 		struct nlattr **attrs)
18891da177e4SLinus Torvalds {
1890fc34acd3SAlexey Dobriyan 	struct net *net = sock_net(skb->sk);
189126b15dadSJamal Hadi Salim 	struct km_event c;
1892b798a9edSJamal Hadi Salim 	u8 type = XFRM_POLICY_TYPE_MAIN;
1893f7b6983fSMasahide NAKAMURA 	int err;
1894161a09e7SJoy Latten 	struct xfrm_audit audit_info;
189526b15dadSJamal Hadi Salim 
189635a7aa08SThomas Graf 	err = copy_from_user_policy_type(&type, attrs);
1897f7b6983fSMasahide NAKAMURA 	if (err)
1898f7b6983fSMasahide NAKAMURA 		return err;
1899f7b6983fSMasahide NAKAMURA 
1900c53fa1edSPatrick McHardy 	audit_info.loginuid = audit_get_loginuid(current);
1901c53fa1edSPatrick McHardy 	audit_info.sessionid = audit_get_sessionid(current);
1902c53fa1edSPatrick McHardy 	security_task_getsecid(current, &audit_info.secid);
1903fc34acd3SAlexey Dobriyan 	err = xfrm_policy_flush(net, type, &audit_info);
19042f1eb65fSJamal Hadi Salim 	if (err) {
19052f1eb65fSJamal Hadi Salim 		if (err == -ESRCH) /* empty table */
19062f1eb65fSJamal Hadi Salim 			return 0;
1907069c474eSDavid S. Miller 		return err;
19082f1eb65fSJamal Hadi Salim 	}
19092f1eb65fSJamal Hadi Salim 
1910f7b6983fSMasahide NAKAMURA 	c.data.type = type;
1911f60f6b8fSHerbert Xu 	c.event = nlh->nlmsg_type;
191226b15dadSJamal Hadi Salim 	c.seq = nlh->nlmsg_seq;
191315e47304SEric W. Biederman 	c.portid = nlh->nlmsg_pid;
19147067802eSAlexey Dobriyan 	c.net = net;
191526b15dadSJamal Hadi Salim 	km_policy_notify(NULL, 0, &c);
19161da177e4SLinus Torvalds 	return 0;
19171da177e4SLinus Torvalds }
19181da177e4SLinus Torvalds 
191922e70050SChristoph Hellwig static int xfrm_add_pol_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
19205424f32eSThomas Graf 		struct nlattr **attrs)
19216c5c8ca7SJamal Hadi Salim {
1922fc34acd3SAlexey Dobriyan 	struct net *net = sock_net(skb->sk);
19236c5c8ca7SJamal Hadi Salim 	struct xfrm_policy *xp;
19247b67c857SThomas Graf 	struct xfrm_user_polexpire *up = nlmsg_data(nlh);
19256c5c8ca7SJamal Hadi Salim 	struct xfrm_userpolicy_info *p = &up->pol;
1926b798a9edSJamal Hadi Salim 	u8 type = XFRM_POLICY_TYPE_MAIN;
19276c5c8ca7SJamal Hadi Salim 	int err = -ENOENT;
1928295fae56SJamal Hadi Salim 	struct xfrm_mark m;
1929295fae56SJamal Hadi Salim 	u32 mark = xfrm_mark_get(attrs, &m);
19306c5c8ca7SJamal Hadi Salim 
193135a7aa08SThomas Graf 	err = copy_from_user_policy_type(&type, attrs);
1932f7b6983fSMasahide NAKAMURA 	if (err)
1933f7b6983fSMasahide NAKAMURA 		return err;
1934f7b6983fSMasahide NAKAMURA 
1935c8bf4d04STimo Teräs 	err = verify_policy_dir(p->dir);
1936c8bf4d04STimo Teräs 	if (err)
1937c8bf4d04STimo Teräs 		return err;
1938c8bf4d04STimo Teräs 
19396c5c8ca7SJamal Hadi Salim 	if (p->index)
1940295fae56SJamal Hadi Salim 		xp = xfrm_policy_byid(net, mark, type, p->dir, p->index, 0, &err);
19416c5c8ca7SJamal Hadi Salim 	else {
19425424f32eSThomas Graf 		struct nlattr *rt = attrs[XFRMA_SEC_CTX];
194303e1ad7bSPaul Moore 		struct xfrm_sec_ctx *ctx;
19446c5c8ca7SJamal Hadi Salim 
194535a7aa08SThomas Graf 		err = verify_sec_ctx_len(attrs);
19466c5c8ca7SJamal Hadi Salim 		if (err)
19476c5c8ca7SJamal Hadi Salim 			return err;
19486c5c8ca7SJamal Hadi Salim 
19492c8dd116SDenis V. Lunev 		ctx = NULL;
19506c5c8ca7SJamal Hadi Salim 		if (rt) {
19515424f32eSThomas Graf 			struct xfrm_user_sec_ctx *uctx = nla_data(rt);
19526c5c8ca7SJamal Hadi Salim 
195303e1ad7bSPaul Moore 			err = security_xfrm_policy_alloc(&ctx, uctx);
195403e1ad7bSPaul Moore 			if (err)
19556c5c8ca7SJamal Hadi Salim 				return err;
19562c8dd116SDenis V. Lunev 		}
1957295fae56SJamal Hadi Salim 		xp = xfrm_policy_bysel_ctx(net, mark, type, p->dir,
19586f26b61eSJamal Hadi Salim 					   &p->sel, ctx, 0, &err);
195903e1ad7bSPaul Moore 		security_xfrm_policy_free(ctx);
19606c5c8ca7SJamal Hadi Salim 	}
19616c5c8ca7SJamal Hadi Salim 	if (xp == NULL)
1962ef41aaa0SEric Paris 		return -ENOENT;
196303e1ad7bSPaul Moore 
1964ea2dea9dSTimo Teräs 	if (unlikely(xp->walk.dead))
19656c5c8ca7SJamal Hadi Salim 		goto out;
19666c5c8ca7SJamal Hadi Salim 
19676c5c8ca7SJamal Hadi Salim 	err = 0;
19686c5c8ca7SJamal Hadi Salim 	if (up->hard) {
1969e1760bd5SEric W. Biederman 		kuid_t loginuid = audit_get_loginuid(current);
1970c53fa1edSPatrick McHardy 		u32 sessionid = audit_get_sessionid(current);
1971c53fa1edSPatrick McHardy 		u32 sid;
1972c53fa1edSPatrick McHardy 
1973c53fa1edSPatrick McHardy 		security_task_getsecid(current, &sid);
19746c5c8ca7SJamal Hadi Salim 		xfrm_policy_delete(xp, p->dir);
19752532386fSEric Paris 		xfrm_audit_policy_delete(xp, 1, loginuid, sessionid, sid);
1976161a09e7SJoy Latten 
19776c5c8ca7SJamal Hadi Salim 	} else {
19786c5c8ca7SJamal Hadi Salim 		// reset the timers here?
197962db5cfdSstephen hemminger 		WARN(1, "Dont know what to do with soft policy expire\n");
19806c5c8ca7SJamal Hadi Salim 	}
1981c6bb8136SEric W. Biederman 	km_policy_expired(xp, p->dir, up->hard, nlh->nlmsg_pid);
19826c5c8ca7SJamal Hadi Salim 
19836c5c8ca7SJamal Hadi Salim out:
19846c5c8ca7SJamal Hadi Salim 	xfrm_pol_put(xp);
19856c5c8ca7SJamal Hadi Salim 	return err;
19866c5c8ca7SJamal Hadi Salim }
19876c5c8ca7SJamal Hadi Salim 
198822e70050SChristoph Hellwig static int xfrm_add_sa_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
19895424f32eSThomas Graf 		struct nlattr **attrs)
199053bc6b4dSJamal Hadi Salim {
1991fc34acd3SAlexey Dobriyan 	struct net *net = sock_net(skb->sk);
199253bc6b4dSJamal Hadi Salim 	struct xfrm_state *x;
199353bc6b4dSJamal Hadi Salim 	int err;
19947b67c857SThomas Graf 	struct xfrm_user_expire *ue = nlmsg_data(nlh);
199553bc6b4dSJamal Hadi Salim 	struct xfrm_usersa_info *p = &ue->state;
19966f26b61eSJamal Hadi Salim 	struct xfrm_mark m;
1997928497f0SNicolas Dichtel 	u32 mark = xfrm_mark_get(attrs, &m);
199853bc6b4dSJamal Hadi Salim 
19996f26b61eSJamal Hadi Salim 	x = xfrm_state_lookup(net, mark, &p->id.daddr, p->id.spi, p->id.proto, p->family);
200053bc6b4dSJamal Hadi Salim 
20013a765aa5SDavid S. Miller 	err = -ENOENT;
200253bc6b4dSJamal Hadi Salim 	if (x == NULL)
200353bc6b4dSJamal Hadi Salim 		return err;
200453bc6b4dSJamal Hadi Salim 
200553bc6b4dSJamal Hadi Salim 	spin_lock_bh(&x->lock);
20063a765aa5SDavid S. Miller 	err = -EINVAL;
200753bc6b4dSJamal Hadi Salim 	if (x->km.state != XFRM_STATE_VALID)
200853bc6b4dSJamal Hadi Salim 		goto out;
2009c6bb8136SEric W. Biederman 	km_state_expired(x, ue->hard, nlh->nlmsg_pid);
201053bc6b4dSJamal Hadi Salim 
2011161a09e7SJoy Latten 	if (ue->hard) {
2012e1760bd5SEric W. Biederman 		kuid_t loginuid = audit_get_loginuid(current);
2013c53fa1edSPatrick McHardy 		u32 sessionid = audit_get_sessionid(current);
2014c53fa1edSPatrick McHardy 		u32 sid;
2015c53fa1edSPatrick McHardy 
2016c53fa1edSPatrick McHardy 		security_task_getsecid(current, &sid);
201753bc6b4dSJamal Hadi Salim 		__xfrm_state_delete(x);
20182532386fSEric Paris 		xfrm_audit_state_delete(x, 1, loginuid, sessionid, sid);
2019161a09e7SJoy Latten 	}
20203a765aa5SDavid S. Miller 	err = 0;
202153bc6b4dSJamal Hadi Salim out:
202253bc6b4dSJamal Hadi Salim 	spin_unlock_bh(&x->lock);
202353bc6b4dSJamal Hadi Salim 	xfrm_state_put(x);
202453bc6b4dSJamal Hadi Salim 	return err;
202553bc6b4dSJamal Hadi Salim }
202653bc6b4dSJamal Hadi Salim 
202722e70050SChristoph Hellwig static int xfrm_add_acquire(struct sk_buff *skb, struct nlmsghdr *nlh,
20285424f32eSThomas Graf 		struct nlattr **attrs)
2029980ebd25SJamal Hadi Salim {
2030fc34acd3SAlexey Dobriyan 	struct net *net = sock_net(skb->sk);
2031980ebd25SJamal Hadi Salim 	struct xfrm_policy *xp;
2032980ebd25SJamal Hadi Salim 	struct xfrm_user_tmpl *ut;
2033980ebd25SJamal Hadi Salim 	int i;
20345424f32eSThomas Graf 	struct nlattr *rt = attrs[XFRMA_TMPL];
20356f26b61eSJamal Hadi Salim 	struct xfrm_mark mark;
2036980ebd25SJamal Hadi Salim 
20377b67c857SThomas Graf 	struct xfrm_user_acquire *ua = nlmsg_data(nlh);
2038fc34acd3SAlexey Dobriyan 	struct xfrm_state *x = xfrm_state_alloc(net);
2039980ebd25SJamal Hadi Salim 	int err = -ENOMEM;
2040980ebd25SJamal Hadi Salim 
2041980ebd25SJamal Hadi Salim 	if (!x)
2042d8eb9307SIlpo Järvinen 		goto nomem;
2043980ebd25SJamal Hadi Salim 
20446f26b61eSJamal Hadi Salim 	xfrm_mark_get(attrs, &mark);
20456f26b61eSJamal Hadi Salim 
2046980ebd25SJamal Hadi Salim 	err = verify_newpolicy_info(&ua->policy);
2047d8eb9307SIlpo Järvinen 	if (err)
2048d8eb9307SIlpo Järvinen 		goto bad_policy;
2049980ebd25SJamal Hadi Salim 
2050980ebd25SJamal Hadi Salim 	/*   build an XP */
2051fc34acd3SAlexey Dobriyan 	xp = xfrm_policy_construct(net, &ua->policy, attrs, &err);
2052d8eb9307SIlpo Järvinen 	if (!xp)
2053d8eb9307SIlpo Järvinen 		goto free_state;
2054980ebd25SJamal Hadi Salim 
2055980ebd25SJamal Hadi Salim 	memcpy(&x->id, &ua->id, sizeof(ua->id));
2056980ebd25SJamal Hadi Salim 	memcpy(&x->props.saddr, &ua->saddr, sizeof(ua->saddr));
2057980ebd25SJamal Hadi Salim 	memcpy(&x->sel, &ua->sel, sizeof(ua->sel));
20586f26b61eSJamal Hadi Salim 	xp->mark.m = x->mark.m = mark.m;
20596f26b61eSJamal Hadi Salim 	xp->mark.v = x->mark.v = mark.v;
20605424f32eSThomas Graf 	ut = nla_data(rt);
2061980ebd25SJamal Hadi Salim 	/* extract the templates and for each call km_key */
2062980ebd25SJamal Hadi Salim 	for (i = 0; i < xp->xfrm_nr; i++, ut++) {
2063980ebd25SJamal Hadi Salim 		struct xfrm_tmpl *t = &xp->xfrm_vec[i];
2064980ebd25SJamal Hadi Salim 		memcpy(&x->id, &t->id, sizeof(x->id));
2065980ebd25SJamal Hadi Salim 		x->props.mode = t->mode;
2066980ebd25SJamal Hadi Salim 		x->props.reqid = t->reqid;
2067980ebd25SJamal Hadi Salim 		x->props.family = ut->family;
2068980ebd25SJamal Hadi Salim 		t->aalgos = ua->aalgos;
2069980ebd25SJamal Hadi Salim 		t->ealgos = ua->ealgos;
2070980ebd25SJamal Hadi Salim 		t->calgos = ua->calgos;
2071980ebd25SJamal Hadi Salim 		err = km_query(x, t, xp);
2072980ebd25SJamal Hadi Salim 
2073980ebd25SJamal Hadi Salim 	}
2074980ebd25SJamal Hadi Salim 
2075980ebd25SJamal Hadi Salim 	kfree(x);
2076980ebd25SJamal Hadi Salim 	kfree(xp);
2077980ebd25SJamal Hadi Salim 
2078980ebd25SJamal Hadi Salim 	return 0;
2079d8eb9307SIlpo Järvinen 
2080d8eb9307SIlpo Järvinen bad_policy:
208162db5cfdSstephen hemminger 	WARN(1, "BAD policy passed\n");
2082d8eb9307SIlpo Järvinen free_state:
2083d8eb9307SIlpo Järvinen 	kfree(x);
2084d8eb9307SIlpo Järvinen nomem:
2085d8eb9307SIlpo Järvinen 	return err;
2086980ebd25SJamal Hadi Salim }
2087980ebd25SJamal Hadi Salim 
20885c79de6eSShinta Sugimoto #ifdef CONFIG_XFRM_MIGRATE
20895c79de6eSShinta Sugimoto static int copy_from_user_migrate(struct xfrm_migrate *ma,
209013c1d189SArnaud Ebalard 				  struct xfrm_kmaddress *k,
20915424f32eSThomas Graf 				  struct nlattr **attrs, int *num)
20925c79de6eSShinta Sugimoto {
20935424f32eSThomas Graf 	struct nlattr *rt = attrs[XFRMA_MIGRATE];
20945c79de6eSShinta Sugimoto 	struct xfrm_user_migrate *um;
20955c79de6eSShinta Sugimoto 	int i, num_migrate;
20965c79de6eSShinta Sugimoto 
209713c1d189SArnaud Ebalard 	if (k != NULL) {
209813c1d189SArnaud Ebalard 		struct xfrm_user_kmaddress *uk;
209913c1d189SArnaud Ebalard 
210013c1d189SArnaud Ebalard 		uk = nla_data(attrs[XFRMA_KMADDRESS]);
210113c1d189SArnaud Ebalard 		memcpy(&k->local, &uk->local, sizeof(k->local));
210213c1d189SArnaud Ebalard 		memcpy(&k->remote, &uk->remote, sizeof(k->remote));
210313c1d189SArnaud Ebalard 		k->family = uk->family;
210413c1d189SArnaud Ebalard 		k->reserved = uk->reserved;
210513c1d189SArnaud Ebalard 	}
210613c1d189SArnaud Ebalard 
21075424f32eSThomas Graf 	um = nla_data(rt);
21085424f32eSThomas Graf 	num_migrate = nla_len(rt) / sizeof(*um);
21095c79de6eSShinta Sugimoto 
21105c79de6eSShinta Sugimoto 	if (num_migrate <= 0 || num_migrate > XFRM_MAX_DEPTH)
21115c79de6eSShinta Sugimoto 		return -EINVAL;
21125c79de6eSShinta Sugimoto 
21135c79de6eSShinta Sugimoto 	for (i = 0; i < num_migrate; i++, um++, ma++) {
21145c79de6eSShinta Sugimoto 		memcpy(&ma->old_daddr, &um->old_daddr, sizeof(ma->old_daddr));
21155c79de6eSShinta Sugimoto 		memcpy(&ma->old_saddr, &um->old_saddr, sizeof(ma->old_saddr));
21165c79de6eSShinta Sugimoto 		memcpy(&ma->new_daddr, &um->new_daddr, sizeof(ma->new_daddr));
21175c79de6eSShinta Sugimoto 		memcpy(&ma->new_saddr, &um->new_saddr, sizeof(ma->new_saddr));
21185c79de6eSShinta Sugimoto 
21195c79de6eSShinta Sugimoto 		ma->proto = um->proto;
21205c79de6eSShinta Sugimoto 		ma->mode = um->mode;
21215c79de6eSShinta Sugimoto 		ma->reqid = um->reqid;
21225c79de6eSShinta Sugimoto 
21235c79de6eSShinta Sugimoto 		ma->old_family = um->old_family;
21245c79de6eSShinta Sugimoto 		ma->new_family = um->new_family;
21255c79de6eSShinta Sugimoto 	}
21265c79de6eSShinta Sugimoto 
21275c79de6eSShinta Sugimoto 	*num = i;
21285c79de6eSShinta Sugimoto 	return 0;
21295c79de6eSShinta Sugimoto }
21305c79de6eSShinta Sugimoto 
21315c79de6eSShinta Sugimoto static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh,
21325424f32eSThomas Graf 			   struct nlattr **attrs)
21335c79de6eSShinta Sugimoto {
21347b67c857SThomas Graf 	struct xfrm_userpolicy_id *pi = nlmsg_data(nlh);
21355c79de6eSShinta Sugimoto 	struct xfrm_migrate m[XFRM_MAX_DEPTH];
213613c1d189SArnaud Ebalard 	struct xfrm_kmaddress km, *kmp;
21375c79de6eSShinta Sugimoto 	u8 type;
21385c79de6eSShinta Sugimoto 	int err;
21395c79de6eSShinta Sugimoto 	int n = 0;
2140*8d549c4fSFan Du 	struct net *net = sock_net(skb->sk);
21415c79de6eSShinta Sugimoto 
214235a7aa08SThomas Graf 	if (attrs[XFRMA_MIGRATE] == NULL)
2143cf5cb79fSThomas Graf 		return -EINVAL;
21445c79de6eSShinta Sugimoto 
214513c1d189SArnaud Ebalard 	kmp = attrs[XFRMA_KMADDRESS] ? &km : NULL;
214613c1d189SArnaud Ebalard 
21475424f32eSThomas Graf 	err = copy_from_user_policy_type(&type, attrs);
21485c79de6eSShinta Sugimoto 	if (err)
21495c79de6eSShinta Sugimoto 		return err;
21505c79de6eSShinta Sugimoto 
215113c1d189SArnaud Ebalard 	err = copy_from_user_migrate((struct xfrm_migrate *)m, kmp, attrs, &n);
21525c79de6eSShinta Sugimoto 	if (err)
21535c79de6eSShinta Sugimoto 		return err;
21545c79de6eSShinta Sugimoto 
21555c79de6eSShinta Sugimoto 	if (!n)
21565c79de6eSShinta Sugimoto 		return 0;
21575c79de6eSShinta Sugimoto 
2158*8d549c4fSFan Du 	xfrm_migrate(&pi->sel, pi->dir, type, m, n, kmp, net);
21595c79de6eSShinta Sugimoto 
21605c79de6eSShinta Sugimoto 	return 0;
21615c79de6eSShinta Sugimoto }
21625c79de6eSShinta Sugimoto #else
21635c79de6eSShinta Sugimoto static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh,
21645424f32eSThomas Graf 			   struct nlattr **attrs)
21655c79de6eSShinta Sugimoto {
21665c79de6eSShinta Sugimoto 	return -ENOPROTOOPT;
21675c79de6eSShinta Sugimoto }
21685c79de6eSShinta Sugimoto #endif
21695c79de6eSShinta Sugimoto 
21705c79de6eSShinta Sugimoto #ifdef CONFIG_XFRM_MIGRATE
2171183cad12SDavid S. Miller static int copy_to_user_migrate(const struct xfrm_migrate *m, struct sk_buff *skb)
21725c79de6eSShinta Sugimoto {
21735c79de6eSShinta Sugimoto 	struct xfrm_user_migrate um;
21745c79de6eSShinta Sugimoto 
21755c79de6eSShinta Sugimoto 	memset(&um, 0, sizeof(um));
21765c79de6eSShinta Sugimoto 	um.proto = m->proto;
21775c79de6eSShinta Sugimoto 	um.mode = m->mode;
21785c79de6eSShinta Sugimoto 	um.reqid = m->reqid;
21795c79de6eSShinta Sugimoto 	um.old_family = m->old_family;
21805c79de6eSShinta Sugimoto 	memcpy(&um.old_daddr, &m->old_daddr, sizeof(um.old_daddr));
21815c79de6eSShinta Sugimoto 	memcpy(&um.old_saddr, &m->old_saddr, sizeof(um.old_saddr));
21825c79de6eSShinta Sugimoto 	um.new_family = m->new_family;
21835c79de6eSShinta Sugimoto 	memcpy(&um.new_daddr, &m->new_daddr, sizeof(um.new_daddr));
21845c79de6eSShinta Sugimoto 	memcpy(&um.new_saddr, &m->new_saddr, sizeof(um.new_saddr));
21855c79de6eSShinta Sugimoto 
2186c0144beaSThomas Graf 	return nla_put(skb, XFRMA_MIGRATE, sizeof(um), &um);
21875c79de6eSShinta Sugimoto }
21885c79de6eSShinta Sugimoto 
2189183cad12SDavid S. Miller static int copy_to_user_kmaddress(const struct xfrm_kmaddress *k, struct sk_buff *skb)
219013c1d189SArnaud Ebalard {
219113c1d189SArnaud Ebalard 	struct xfrm_user_kmaddress uk;
219213c1d189SArnaud Ebalard 
219313c1d189SArnaud Ebalard 	memset(&uk, 0, sizeof(uk));
219413c1d189SArnaud Ebalard 	uk.family = k->family;
219513c1d189SArnaud Ebalard 	uk.reserved = k->reserved;
219613c1d189SArnaud Ebalard 	memcpy(&uk.local, &k->local, sizeof(uk.local));
2197a1caa322SArnaud Ebalard 	memcpy(&uk.remote, &k->remote, sizeof(uk.remote));
219813c1d189SArnaud Ebalard 
219913c1d189SArnaud Ebalard 	return nla_put(skb, XFRMA_KMADDRESS, sizeof(uk), &uk);
220013c1d189SArnaud Ebalard }
220113c1d189SArnaud Ebalard 
220213c1d189SArnaud Ebalard static inline size_t xfrm_migrate_msgsize(int num_migrate, int with_kma)
22037deb2264SThomas Graf {
22047deb2264SThomas Graf 	return NLMSG_ALIGN(sizeof(struct xfrm_userpolicy_id))
220513c1d189SArnaud Ebalard 	      + (with_kma ? nla_total_size(sizeof(struct xfrm_kmaddress)) : 0)
22067deb2264SThomas Graf 	      + nla_total_size(sizeof(struct xfrm_user_migrate) * num_migrate)
22077deb2264SThomas Graf 	      + userpolicy_type_attrsize();
22087deb2264SThomas Graf }
22097deb2264SThomas Graf 
2210183cad12SDavid S. Miller static int build_migrate(struct sk_buff *skb, const struct xfrm_migrate *m,
2211183cad12SDavid S. Miller 			 int num_migrate, const struct xfrm_kmaddress *k,
2212183cad12SDavid S. Miller 			 const struct xfrm_selector *sel, u8 dir, u8 type)
22135c79de6eSShinta Sugimoto {
2214183cad12SDavid S. Miller 	const struct xfrm_migrate *mp;
22155c79de6eSShinta Sugimoto 	struct xfrm_userpolicy_id *pol_id;
22165c79de6eSShinta Sugimoto 	struct nlmsghdr *nlh;
22171d1e34ddSDavid S. Miller 	int i, err;
22185c79de6eSShinta Sugimoto 
221979b8b7f4SThomas Graf 	nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_MIGRATE, sizeof(*pol_id), 0);
222079b8b7f4SThomas Graf 	if (nlh == NULL)
222179b8b7f4SThomas Graf 		return -EMSGSIZE;
22225c79de6eSShinta Sugimoto 
22237b67c857SThomas Graf 	pol_id = nlmsg_data(nlh);
22245c79de6eSShinta Sugimoto 	/* copy data from selector, dir, and type to the pol_id */
22255c79de6eSShinta Sugimoto 	memset(pol_id, 0, sizeof(*pol_id));
22265c79de6eSShinta Sugimoto 	memcpy(&pol_id->sel, sel, sizeof(pol_id->sel));
22275c79de6eSShinta Sugimoto 	pol_id->dir = dir;
22285c79de6eSShinta Sugimoto 
22291d1e34ddSDavid S. Miller 	if (k != NULL) {
22301d1e34ddSDavid S. Miller 		err = copy_to_user_kmaddress(k, skb);
22311d1e34ddSDavid S. Miller 		if (err)
22321d1e34ddSDavid S. Miller 			goto out_cancel;
22331d1e34ddSDavid S. Miller 	}
22341d1e34ddSDavid S. Miller 	err = copy_to_user_policy_type(type, skb);
22351d1e34ddSDavid S. Miller 	if (err)
22361d1e34ddSDavid S. Miller 		goto out_cancel;
22375c79de6eSShinta Sugimoto 	for (i = 0, mp = m ; i < num_migrate; i++, mp++) {
22381d1e34ddSDavid S. Miller 		err = copy_to_user_migrate(mp, skb);
22391d1e34ddSDavid S. Miller 		if (err)
22401d1e34ddSDavid S. Miller 			goto out_cancel;
22415c79de6eSShinta Sugimoto 	}
22425c79de6eSShinta Sugimoto 
22439825069dSThomas Graf 	return nlmsg_end(skb, nlh);
22441d1e34ddSDavid S. Miller 
22451d1e34ddSDavid S. Miller out_cancel:
22469825069dSThomas Graf 	nlmsg_cancel(skb, nlh);
22471d1e34ddSDavid S. Miller 	return err;
22485c79de6eSShinta Sugimoto }
22495c79de6eSShinta Sugimoto 
2250183cad12SDavid S. Miller static int xfrm_send_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
2251183cad12SDavid S. Miller 			     const struct xfrm_migrate *m, int num_migrate,
2252183cad12SDavid S. Miller 			     const struct xfrm_kmaddress *k)
22535c79de6eSShinta Sugimoto {
2254a6483b79SAlexey Dobriyan 	struct net *net = &init_net;
22555c79de6eSShinta Sugimoto 	struct sk_buff *skb;
22565c79de6eSShinta Sugimoto 
225713c1d189SArnaud Ebalard 	skb = nlmsg_new(xfrm_migrate_msgsize(num_migrate, !!k), GFP_ATOMIC);
22585c79de6eSShinta Sugimoto 	if (skb == NULL)
22595c79de6eSShinta Sugimoto 		return -ENOMEM;
22605c79de6eSShinta Sugimoto 
22615c79de6eSShinta Sugimoto 	/* build migrate */
226213c1d189SArnaud Ebalard 	if (build_migrate(skb, m, num_migrate, k, sel, dir, type) < 0)
22635c79de6eSShinta Sugimoto 		BUG();
22645c79de6eSShinta Sugimoto 
2265a6483b79SAlexey Dobriyan 	return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_MIGRATE, GFP_ATOMIC);
22665c79de6eSShinta Sugimoto }
22675c79de6eSShinta Sugimoto #else
2268183cad12SDavid S. Miller static int xfrm_send_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
2269183cad12SDavid S. Miller 			     const struct xfrm_migrate *m, int num_migrate,
2270183cad12SDavid S. Miller 			     const struct xfrm_kmaddress *k)
22715c79de6eSShinta Sugimoto {
22725c79de6eSShinta Sugimoto 	return -ENOPROTOOPT;
22735c79de6eSShinta Sugimoto }
22745c79de6eSShinta Sugimoto #endif
2275d51d081dSJamal Hadi Salim 
2276a7bd9a45SThomas Graf #define XMSGSIZE(type) sizeof(struct type)
2277492b558bSThomas Graf 
2278492b558bSThomas Graf static const int xfrm_msg_min[XFRM_NR_MSGTYPES] = {
227966f9a259SDavid S. Miller 	[XFRM_MSG_NEWSA       - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
2280492b558bSThomas Graf 	[XFRM_MSG_DELSA       - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
2281492b558bSThomas Graf 	[XFRM_MSG_GETSA       - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
2282492b558bSThomas Graf 	[XFRM_MSG_NEWPOLICY   - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
2283492b558bSThomas Graf 	[XFRM_MSG_DELPOLICY   - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
2284492b558bSThomas Graf 	[XFRM_MSG_GETPOLICY   - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
2285492b558bSThomas Graf 	[XFRM_MSG_ALLOCSPI    - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userspi_info),
2286980ebd25SJamal Hadi Salim 	[XFRM_MSG_ACQUIRE     - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_acquire),
228753bc6b4dSJamal Hadi Salim 	[XFRM_MSG_EXPIRE      - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_expire),
2288492b558bSThomas Graf 	[XFRM_MSG_UPDPOLICY   - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
228966f9a259SDavid S. Miller 	[XFRM_MSG_UPDSA       - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
22906c5c8ca7SJamal Hadi Salim 	[XFRM_MSG_POLEXPIRE   - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_polexpire),
2291492b558bSThomas Graf 	[XFRM_MSG_FLUSHSA     - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_flush),
2292a7bd9a45SThomas Graf 	[XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = 0,
2293d51d081dSJamal Hadi Salim 	[XFRM_MSG_NEWAE       - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
2294d51d081dSJamal Hadi Salim 	[XFRM_MSG_GETAE       - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
229597a64b45SMasahide NAKAMURA 	[XFRM_MSG_REPORT      - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_report),
22965c79de6eSShinta Sugimoto 	[XFRM_MSG_MIGRATE     - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
2297a7bd9a45SThomas Graf 	[XFRM_MSG_GETSADINFO  - XFRM_MSG_BASE] = sizeof(u32),
2298a7bd9a45SThomas Graf 	[XFRM_MSG_GETSPDINFO  - XFRM_MSG_BASE] = sizeof(u32),
22991da177e4SLinus Torvalds };
23001da177e4SLinus Torvalds 
2301492b558bSThomas Graf #undef XMSGSIZE
2302492b558bSThomas Graf 
2303cf5cb79fSThomas Graf static const struct nla_policy xfrma_policy[XFRMA_MAX+1] = {
2304c28e9304Sjamal 	[XFRMA_SA]		= { .len = sizeof(struct xfrm_usersa_info)},
2305c28e9304Sjamal 	[XFRMA_POLICY]		= { .len = sizeof(struct xfrm_userpolicy_info)},
2306c28e9304Sjamal 	[XFRMA_LASTUSED]	= { .type = NLA_U64},
2307c28e9304Sjamal 	[XFRMA_ALG_AUTH_TRUNC]	= { .len = sizeof(struct xfrm_algo_auth)},
23081a6509d9SHerbert Xu 	[XFRMA_ALG_AEAD]	= { .len = sizeof(struct xfrm_algo_aead) },
2309cf5cb79fSThomas Graf 	[XFRMA_ALG_AUTH]	= { .len = sizeof(struct xfrm_algo) },
2310cf5cb79fSThomas Graf 	[XFRMA_ALG_CRYPT]	= { .len = sizeof(struct xfrm_algo) },
2311cf5cb79fSThomas Graf 	[XFRMA_ALG_COMP]	= { .len = sizeof(struct xfrm_algo) },
2312cf5cb79fSThomas Graf 	[XFRMA_ENCAP]		= { .len = sizeof(struct xfrm_encap_tmpl) },
2313cf5cb79fSThomas Graf 	[XFRMA_TMPL]		= { .len = sizeof(struct xfrm_user_tmpl) },
2314cf5cb79fSThomas Graf 	[XFRMA_SEC_CTX]		= { .len = sizeof(struct xfrm_sec_ctx) },
2315cf5cb79fSThomas Graf 	[XFRMA_LTIME_VAL]	= { .len = sizeof(struct xfrm_lifetime_cur) },
2316cf5cb79fSThomas Graf 	[XFRMA_REPLAY_VAL]	= { .len = sizeof(struct xfrm_replay_state) },
2317cf5cb79fSThomas Graf 	[XFRMA_REPLAY_THRESH]	= { .type = NLA_U32 },
2318cf5cb79fSThomas Graf 	[XFRMA_ETIMER_THRESH]	= { .type = NLA_U32 },
2319cf5cb79fSThomas Graf 	[XFRMA_SRCADDR]		= { .len = sizeof(xfrm_address_t) },
2320cf5cb79fSThomas Graf 	[XFRMA_COADDR]		= { .len = sizeof(xfrm_address_t) },
2321cf5cb79fSThomas Graf 	[XFRMA_POLICY_TYPE]	= { .len = sizeof(struct xfrm_userpolicy_type)},
2322cf5cb79fSThomas Graf 	[XFRMA_MIGRATE]		= { .len = sizeof(struct xfrm_user_migrate) },
232313c1d189SArnaud Ebalard 	[XFRMA_KMADDRESS]	= { .len = sizeof(struct xfrm_user_kmaddress) },
23246f26b61eSJamal Hadi Salim 	[XFRMA_MARK]		= { .len = sizeof(struct xfrm_mark) },
232535d2856bSMartin Willi 	[XFRMA_TFCPAD]		= { .type = NLA_U32 },
2326d8647b79SSteffen Klassert 	[XFRMA_REPLAY_ESN_VAL]	= { .len = sizeof(struct xfrm_replay_state_esn) },
2327a947b0a9SNicolas Dichtel 	[XFRMA_SA_EXTRA_FLAGS]	= { .type = NLA_U32 },
2328cf5cb79fSThomas Graf };
2329cf5cb79fSThomas Graf 
233005600a79SMathias Krause static const struct xfrm_link {
23315424f32eSThomas Graf 	int (*doit)(struct sk_buff *, struct nlmsghdr *, struct nlattr **);
23321da177e4SLinus Torvalds 	int (*dump)(struct sk_buff *, struct netlink_callback *);
23334c563f76STimo Teras 	int (*done)(struct netlink_callback *);
2334492b558bSThomas Graf } xfrm_dispatch[XFRM_NR_MSGTYPES] = {
2335492b558bSThomas Graf 	[XFRM_MSG_NEWSA       - XFRM_MSG_BASE] = { .doit = xfrm_add_sa        },
2336492b558bSThomas Graf 	[XFRM_MSG_DELSA       - XFRM_MSG_BASE] = { .doit = xfrm_del_sa        },
2337492b558bSThomas Graf 	[XFRM_MSG_GETSA       - XFRM_MSG_BASE] = { .doit = xfrm_get_sa,
23384c563f76STimo Teras 						   .dump = xfrm_dump_sa,
23394c563f76STimo Teras 						   .done = xfrm_dump_sa_done  },
2340492b558bSThomas Graf 	[XFRM_MSG_NEWPOLICY   - XFRM_MSG_BASE] = { .doit = xfrm_add_policy    },
2341492b558bSThomas Graf 	[XFRM_MSG_DELPOLICY   - XFRM_MSG_BASE] = { .doit = xfrm_get_policy    },
2342492b558bSThomas Graf 	[XFRM_MSG_GETPOLICY   - XFRM_MSG_BASE] = { .doit = xfrm_get_policy,
23434c563f76STimo Teras 						   .dump = xfrm_dump_policy,
23444c563f76STimo Teras 						   .done = xfrm_dump_policy_done },
2345492b558bSThomas Graf 	[XFRM_MSG_ALLOCSPI    - XFRM_MSG_BASE] = { .doit = xfrm_alloc_userspi },
2346980ebd25SJamal Hadi Salim 	[XFRM_MSG_ACQUIRE     - XFRM_MSG_BASE] = { .doit = xfrm_add_acquire   },
234753bc6b4dSJamal Hadi Salim 	[XFRM_MSG_EXPIRE      - XFRM_MSG_BASE] = { .doit = xfrm_add_sa_expire },
2348492b558bSThomas Graf 	[XFRM_MSG_UPDPOLICY   - XFRM_MSG_BASE] = { .doit = xfrm_add_policy    },
2349492b558bSThomas Graf 	[XFRM_MSG_UPDSA       - XFRM_MSG_BASE] = { .doit = xfrm_add_sa        },
23506c5c8ca7SJamal Hadi Salim 	[XFRM_MSG_POLEXPIRE   - XFRM_MSG_BASE] = { .doit = xfrm_add_pol_expire},
2351492b558bSThomas Graf 	[XFRM_MSG_FLUSHSA     - XFRM_MSG_BASE] = { .doit = xfrm_flush_sa      },
2352492b558bSThomas Graf 	[XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_flush_policy  },
2353d51d081dSJamal Hadi Salim 	[XFRM_MSG_NEWAE       - XFRM_MSG_BASE] = { .doit = xfrm_new_ae  },
2354d51d081dSJamal Hadi Salim 	[XFRM_MSG_GETAE       - XFRM_MSG_BASE] = { .doit = xfrm_get_ae  },
23555c79de6eSShinta Sugimoto 	[XFRM_MSG_MIGRATE     - XFRM_MSG_BASE] = { .doit = xfrm_do_migrate    },
235628d8909bSJamal Hadi Salim 	[XFRM_MSG_GETSADINFO  - XFRM_MSG_BASE] = { .doit = xfrm_get_sadinfo   },
2357ecfd6b18SJamal Hadi Salim 	[XFRM_MSG_GETSPDINFO  - XFRM_MSG_BASE] = { .doit = xfrm_get_spdinfo   },
23581da177e4SLinus Torvalds };
23591da177e4SLinus Torvalds 
23601d00a4ebSThomas Graf static int xfrm_user_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
23611da177e4SLinus Torvalds {
2362a6483b79SAlexey Dobriyan 	struct net *net = sock_net(skb->sk);
236335a7aa08SThomas Graf 	struct nlattr *attrs[XFRMA_MAX+1];
236405600a79SMathias Krause 	const struct xfrm_link *link;
2365a7bd9a45SThomas Graf 	int type, err;
23661da177e4SLinus Torvalds 
23671da177e4SLinus Torvalds 	type = nlh->nlmsg_type;
23681da177e4SLinus Torvalds 	if (type > XFRM_MSG_MAX)
23691d00a4ebSThomas Graf 		return -EINVAL;
23701da177e4SLinus Torvalds 
23711da177e4SLinus Torvalds 	type -= XFRM_MSG_BASE;
23721da177e4SLinus Torvalds 	link = &xfrm_dispatch[type];
23731da177e4SLinus Torvalds 
23741da177e4SLinus Torvalds 	/* All operations require privileges, even GET */
2375df008c91SEric W. Biederman 	if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
23761d00a4ebSThomas Graf 		return -EPERM;
23771da177e4SLinus Torvalds 
2378492b558bSThomas Graf 	if ((type == (XFRM_MSG_GETSA - XFRM_MSG_BASE) ||
2379492b558bSThomas Graf 	     type == (XFRM_MSG_GETPOLICY - XFRM_MSG_BASE)) &&
2380b8f3ab42SDavid S. Miller 	    (nlh->nlmsg_flags & NLM_F_DUMP)) {
23811da177e4SLinus Torvalds 		if (link->dump == NULL)
23821d00a4ebSThomas Graf 			return -EINVAL;
23831da177e4SLinus Torvalds 
238480d326faSPablo Neira Ayuso 		{
238580d326faSPablo Neira Ayuso 			struct netlink_dump_control c = {
238680d326faSPablo Neira Ayuso 				.dump = link->dump,
238780d326faSPablo Neira Ayuso 				.done = link->done,
238880d326faSPablo Neira Ayuso 			};
238980d326faSPablo Neira Ayuso 			return netlink_dump_start(net->xfrm.nlsk, skb, nlh, &c);
239080d326faSPablo Neira Ayuso 		}
23911da177e4SLinus Torvalds 	}
23921da177e4SLinus Torvalds 
239335a7aa08SThomas Graf 	err = nlmsg_parse(nlh, xfrm_msg_min[type], attrs, XFRMA_MAX,
2394cf5cb79fSThomas Graf 			  xfrma_policy);
2395a7bd9a45SThomas Graf 	if (err < 0)
2396a7bd9a45SThomas Graf 		return err;
23971da177e4SLinus Torvalds 
23981da177e4SLinus Torvalds 	if (link->doit == NULL)
23991d00a4ebSThomas Graf 		return -EINVAL;
24001da177e4SLinus Torvalds 
24015424f32eSThomas Graf 	return link->doit(skb, nlh, attrs);
24021da177e4SLinus Torvalds }
24031da177e4SLinus Torvalds 
2404cd40b7d3SDenis V. Lunev static void xfrm_netlink_rcv(struct sk_buff *skb)
24051da177e4SLinus Torvalds {
24064a3e2f71SArjan van de Ven 	mutex_lock(&xfrm_cfg_mutex);
2407cd40b7d3SDenis V. Lunev 	netlink_rcv_skb(skb, &xfrm_user_rcv_msg);
24084a3e2f71SArjan van de Ven 	mutex_unlock(&xfrm_cfg_mutex);
24091da177e4SLinus Torvalds }
24101da177e4SLinus Torvalds 
24117deb2264SThomas Graf static inline size_t xfrm_expire_msgsize(void)
24127deb2264SThomas Graf {
24136f26b61eSJamal Hadi Salim 	return NLMSG_ALIGN(sizeof(struct xfrm_user_expire))
24146f26b61eSJamal Hadi Salim 	       + nla_total_size(sizeof(struct xfrm_mark));
24157deb2264SThomas Graf }
24167deb2264SThomas Graf 
2417214e005bSDavid S. Miller static int build_expire(struct sk_buff *skb, struct xfrm_state *x, const struct km_event *c)
24181da177e4SLinus Torvalds {
24191da177e4SLinus Torvalds 	struct xfrm_user_expire *ue;
24201da177e4SLinus Torvalds 	struct nlmsghdr *nlh;
24211d1e34ddSDavid S. Miller 	int err;
24221da177e4SLinus Torvalds 
242315e47304SEric W. Biederman 	nlh = nlmsg_put(skb, c->portid, 0, XFRM_MSG_EXPIRE, sizeof(*ue), 0);
242479b8b7f4SThomas Graf 	if (nlh == NULL)
242579b8b7f4SThomas Graf 		return -EMSGSIZE;
24261da177e4SLinus Torvalds 
24277b67c857SThomas Graf 	ue = nlmsg_data(nlh);
24281da177e4SLinus Torvalds 	copy_to_user_state(x, &ue->state);
2429d51d081dSJamal Hadi Salim 	ue->hard = (c->data.hard != 0) ? 1 : 0;
24301da177e4SLinus Torvalds 
24311d1e34ddSDavid S. Miller 	err = xfrm_mark_put(skb, &x->mark);
24321d1e34ddSDavid S. Miller 	if (err)
24331d1e34ddSDavid S. Miller 		return err;
24346f26b61eSJamal Hadi Salim 
24359825069dSThomas Graf 	return nlmsg_end(skb, nlh);
24361da177e4SLinus Torvalds }
24371da177e4SLinus Torvalds 
2438214e005bSDavid S. Miller static int xfrm_exp_state_notify(struct xfrm_state *x, const struct km_event *c)
24391da177e4SLinus Torvalds {
2440fc34acd3SAlexey Dobriyan 	struct net *net = xs_net(x);
24411da177e4SLinus Torvalds 	struct sk_buff *skb;
24421da177e4SLinus Torvalds 
24437deb2264SThomas Graf 	skb = nlmsg_new(xfrm_expire_msgsize(), GFP_ATOMIC);
24441da177e4SLinus Torvalds 	if (skb == NULL)
24451da177e4SLinus Torvalds 		return -ENOMEM;
24461da177e4SLinus Torvalds 
24476f26b61eSJamal Hadi Salim 	if (build_expire(skb, x, c) < 0) {
24486f26b61eSJamal Hadi Salim 		kfree_skb(skb);
24496f26b61eSJamal Hadi Salim 		return -EMSGSIZE;
24506f26b61eSJamal Hadi Salim 	}
24511da177e4SLinus Torvalds 
2452a6483b79SAlexey Dobriyan 	return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_EXPIRE, GFP_ATOMIC);
24531da177e4SLinus Torvalds }
24541da177e4SLinus Torvalds 
2455214e005bSDavid S. Miller static int xfrm_aevent_state_notify(struct xfrm_state *x, const struct km_event *c)
2456d51d081dSJamal Hadi Salim {
2457fc34acd3SAlexey Dobriyan 	struct net *net = xs_net(x);
2458d51d081dSJamal Hadi Salim 	struct sk_buff *skb;
2459d51d081dSJamal Hadi Salim 
2460d8647b79SSteffen Klassert 	skb = nlmsg_new(xfrm_aevent_msgsize(x), GFP_ATOMIC);
2461d51d081dSJamal Hadi Salim 	if (skb == NULL)
2462d51d081dSJamal Hadi Salim 		return -ENOMEM;
2463d51d081dSJamal Hadi Salim 
2464d51d081dSJamal Hadi Salim 	if (build_aevent(skb, x, c) < 0)
2465d51d081dSJamal Hadi Salim 		BUG();
2466d51d081dSJamal Hadi Salim 
2467a6483b79SAlexey Dobriyan 	return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_AEVENTS, GFP_ATOMIC);
2468d51d081dSJamal Hadi Salim }
2469d51d081dSJamal Hadi Salim 
2470214e005bSDavid S. Miller static int xfrm_notify_sa_flush(const struct km_event *c)
247126b15dadSJamal Hadi Salim {
24727067802eSAlexey Dobriyan 	struct net *net = c->net;
247326b15dadSJamal Hadi Salim 	struct xfrm_usersa_flush *p;
247426b15dadSJamal Hadi Salim 	struct nlmsghdr *nlh;
247526b15dadSJamal Hadi Salim 	struct sk_buff *skb;
24767deb2264SThomas Graf 	int len = NLMSG_ALIGN(sizeof(struct xfrm_usersa_flush));
247726b15dadSJamal Hadi Salim 
24787deb2264SThomas Graf 	skb = nlmsg_new(len, GFP_ATOMIC);
247926b15dadSJamal Hadi Salim 	if (skb == NULL)
248026b15dadSJamal Hadi Salim 		return -ENOMEM;
248126b15dadSJamal Hadi Salim 
248215e47304SEric W. Biederman 	nlh = nlmsg_put(skb, c->portid, c->seq, XFRM_MSG_FLUSHSA, sizeof(*p), 0);
248379b8b7f4SThomas Graf 	if (nlh == NULL) {
248479b8b7f4SThomas Graf 		kfree_skb(skb);
248579b8b7f4SThomas Graf 		return -EMSGSIZE;
248679b8b7f4SThomas Graf 	}
248726b15dadSJamal Hadi Salim 
24887b67c857SThomas Graf 	p = nlmsg_data(nlh);
2489bf08867fSHerbert Xu 	p->proto = c->data.proto;
249026b15dadSJamal Hadi Salim 
24919825069dSThomas Graf 	nlmsg_end(skb, nlh);
249226b15dadSJamal Hadi Salim 
2493a6483b79SAlexey Dobriyan 	return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_SA, GFP_ATOMIC);
249426b15dadSJamal Hadi Salim }
249526b15dadSJamal Hadi Salim 
24967deb2264SThomas Graf static inline size_t xfrm_sa_len(struct xfrm_state *x)
249726b15dadSJamal Hadi Salim {
24987deb2264SThomas Graf 	size_t l = 0;
24991a6509d9SHerbert Xu 	if (x->aead)
25001a6509d9SHerbert Xu 		l += nla_total_size(aead_len(x->aead));
25014447bb33SMartin Willi 	if (x->aalg) {
25024447bb33SMartin Willi 		l += nla_total_size(sizeof(struct xfrm_algo) +
25034447bb33SMartin Willi 				    (x->aalg->alg_key_len + 7) / 8);
25044447bb33SMartin Willi 		l += nla_total_size(xfrm_alg_auth_len(x->aalg));
25054447bb33SMartin Willi 	}
250626b15dadSJamal Hadi Salim 	if (x->ealg)
25070f99be0dSEric Dumazet 		l += nla_total_size(xfrm_alg_len(x->ealg));
250826b15dadSJamal Hadi Salim 	if (x->calg)
25097deb2264SThomas Graf 		l += nla_total_size(sizeof(*x->calg));
251026b15dadSJamal Hadi Salim 	if (x->encap)
25117deb2264SThomas Graf 		l += nla_total_size(sizeof(*x->encap));
251235d2856bSMartin Willi 	if (x->tfcpad)
251335d2856bSMartin Willi 		l += nla_total_size(sizeof(x->tfcpad));
2514d8647b79SSteffen Klassert 	if (x->replay_esn)
2515d8647b79SSteffen Klassert 		l += nla_total_size(xfrm_replay_state_esn_len(x->replay_esn));
251668325d3bSHerbert Xu 	if (x->security)
251768325d3bSHerbert Xu 		l += nla_total_size(sizeof(struct xfrm_user_sec_ctx) +
251868325d3bSHerbert Xu 				    x->security->ctx_len);
251968325d3bSHerbert Xu 	if (x->coaddr)
252068325d3bSHerbert Xu 		l += nla_total_size(sizeof(*x->coaddr));
2521a947b0a9SNicolas Dichtel 	if (x->props.extra_flags)
2522a947b0a9SNicolas Dichtel 		l += nla_total_size(sizeof(x->props.extra_flags));
252368325d3bSHerbert Xu 
2524d26f3984SHerbert Xu 	/* Must count x->lastused as it may become non-zero behind our back. */
2525d26f3984SHerbert Xu 	l += nla_total_size(sizeof(u64));
252626b15dadSJamal Hadi Salim 
252726b15dadSJamal Hadi Salim 	return l;
252826b15dadSJamal Hadi Salim }
252926b15dadSJamal Hadi Salim 
2530214e005bSDavid S. Miller static int xfrm_notify_sa(struct xfrm_state *x, const struct km_event *c)
253126b15dadSJamal Hadi Salim {
2532fc34acd3SAlexey Dobriyan 	struct net *net = xs_net(x);
253326b15dadSJamal Hadi Salim 	struct xfrm_usersa_info *p;
25340603eac0SHerbert Xu 	struct xfrm_usersa_id *id;
253526b15dadSJamal Hadi Salim 	struct nlmsghdr *nlh;
253626b15dadSJamal Hadi Salim 	struct sk_buff *skb;
253726b15dadSJamal Hadi Salim 	int len = xfrm_sa_len(x);
25381d1e34ddSDavid S. Miller 	int headlen, err;
25390603eac0SHerbert Xu 
25400603eac0SHerbert Xu 	headlen = sizeof(*p);
25410603eac0SHerbert Xu 	if (c->event == XFRM_MSG_DELSA) {
25427deb2264SThomas Graf 		len += nla_total_size(headlen);
25430603eac0SHerbert Xu 		headlen = sizeof(*id);
25446f26b61eSJamal Hadi Salim 		len += nla_total_size(sizeof(struct xfrm_mark));
25450603eac0SHerbert Xu 	}
25467deb2264SThomas Graf 	len += NLMSG_ALIGN(headlen);
254726b15dadSJamal Hadi Salim 
25487deb2264SThomas Graf 	skb = nlmsg_new(len, GFP_ATOMIC);
254926b15dadSJamal Hadi Salim 	if (skb == NULL)
255026b15dadSJamal Hadi Salim 		return -ENOMEM;
255126b15dadSJamal Hadi Salim 
255215e47304SEric W. Biederman 	nlh = nlmsg_put(skb, c->portid, c->seq, c->event, headlen, 0);
25531d1e34ddSDavid S. Miller 	err = -EMSGSIZE;
255479b8b7f4SThomas Graf 	if (nlh == NULL)
25551d1e34ddSDavid S. Miller 		goto out_free_skb;
255626b15dadSJamal Hadi Salim 
25577b67c857SThomas Graf 	p = nlmsg_data(nlh);
25580603eac0SHerbert Xu 	if (c->event == XFRM_MSG_DELSA) {
2559c0144beaSThomas Graf 		struct nlattr *attr;
2560c0144beaSThomas Graf 
25617b67c857SThomas Graf 		id = nlmsg_data(nlh);
25620603eac0SHerbert Xu 		memcpy(&id->daddr, &x->id.daddr, sizeof(id->daddr));
25630603eac0SHerbert Xu 		id->spi = x->id.spi;
25640603eac0SHerbert Xu 		id->family = x->props.family;
25650603eac0SHerbert Xu 		id->proto = x->id.proto;
25660603eac0SHerbert Xu 
2567c0144beaSThomas Graf 		attr = nla_reserve(skb, XFRMA_SA, sizeof(*p));
25681d1e34ddSDavid S. Miller 		err = -EMSGSIZE;
2569c0144beaSThomas Graf 		if (attr == NULL)
25701d1e34ddSDavid S. Miller 			goto out_free_skb;
2571c0144beaSThomas Graf 
2572c0144beaSThomas Graf 		p = nla_data(attr);
25730603eac0SHerbert Xu 	}
25741d1e34ddSDavid S. Miller 	err = copy_to_user_state_extra(x, p, skb);
25751d1e34ddSDavid S. Miller 	if (err)
25761d1e34ddSDavid S. Miller 		goto out_free_skb;
257726b15dadSJamal Hadi Salim 
25789825069dSThomas Graf 	nlmsg_end(skb, nlh);
257926b15dadSJamal Hadi Salim 
2580a6483b79SAlexey Dobriyan 	return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_SA, GFP_ATOMIC);
258126b15dadSJamal Hadi Salim 
25821d1e34ddSDavid S. Miller out_free_skb:
258326b15dadSJamal Hadi Salim 	kfree_skb(skb);
25841d1e34ddSDavid S. Miller 	return err;
258526b15dadSJamal Hadi Salim }
258626b15dadSJamal Hadi Salim 
2587214e005bSDavid S. Miller static int xfrm_send_state_notify(struct xfrm_state *x, const struct km_event *c)
258826b15dadSJamal Hadi Salim {
258926b15dadSJamal Hadi Salim 
259026b15dadSJamal Hadi Salim 	switch (c->event) {
2591f60f6b8fSHerbert Xu 	case XFRM_MSG_EXPIRE:
259226b15dadSJamal Hadi Salim 		return xfrm_exp_state_notify(x, c);
2593d51d081dSJamal Hadi Salim 	case XFRM_MSG_NEWAE:
2594d51d081dSJamal Hadi Salim 		return xfrm_aevent_state_notify(x, c);
2595f60f6b8fSHerbert Xu 	case XFRM_MSG_DELSA:
2596f60f6b8fSHerbert Xu 	case XFRM_MSG_UPDSA:
2597f60f6b8fSHerbert Xu 	case XFRM_MSG_NEWSA:
259826b15dadSJamal Hadi Salim 		return xfrm_notify_sa(x, c);
2599f60f6b8fSHerbert Xu 	case XFRM_MSG_FLUSHSA:
260026b15dadSJamal Hadi Salim 		return xfrm_notify_sa_flush(c);
260126b15dadSJamal Hadi Salim 	default:
260262db5cfdSstephen hemminger 		printk(KERN_NOTICE "xfrm_user: Unknown SA event %d\n",
260362db5cfdSstephen hemminger 		       c->event);
260426b15dadSJamal Hadi Salim 		break;
260526b15dadSJamal Hadi Salim 	}
260626b15dadSJamal Hadi Salim 
260726b15dadSJamal Hadi Salim 	return 0;
260826b15dadSJamal Hadi Salim 
260926b15dadSJamal Hadi Salim }
261026b15dadSJamal Hadi Salim 
26117deb2264SThomas Graf static inline size_t xfrm_acquire_msgsize(struct xfrm_state *x,
26127deb2264SThomas Graf 					  struct xfrm_policy *xp)
26137deb2264SThomas Graf {
26147deb2264SThomas Graf 	return NLMSG_ALIGN(sizeof(struct xfrm_user_acquire))
26157deb2264SThomas Graf 	       + nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr)
26166f26b61eSJamal Hadi Salim 	       + nla_total_size(sizeof(struct xfrm_mark))
26177deb2264SThomas Graf 	       + nla_total_size(xfrm_user_sec_ctx_size(x->security))
26187deb2264SThomas Graf 	       + userpolicy_type_attrsize();
26197deb2264SThomas Graf }
26207deb2264SThomas Graf 
26211da177e4SLinus Torvalds static int build_acquire(struct sk_buff *skb, struct xfrm_state *x,
262265e0736bSFan Du 			 struct xfrm_tmpl *xt, struct xfrm_policy *xp)
26231da177e4SLinus Torvalds {
26241d1e34ddSDavid S. Miller 	__u32 seq = xfrm_get_acqseq();
26251da177e4SLinus Torvalds 	struct xfrm_user_acquire *ua;
26261da177e4SLinus Torvalds 	struct nlmsghdr *nlh;
26271d1e34ddSDavid S. Miller 	int err;
26281da177e4SLinus Torvalds 
262979b8b7f4SThomas Graf 	nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_ACQUIRE, sizeof(*ua), 0);
263079b8b7f4SThomas Graf 	if (nlh == NULL)
263179b8b7f4SThomas Graf 		return -EMSGSIZE;
26321da177e4SLinus Torvalds 
26337b67c857SThomas Graf 	ua = nlmsg_data(nlh);
26341da177e4SLinus Torvalds 	memcpy(&ua->id, &x->id, sizeof(ua->id));
26351da177e4SLinus Torvalds 	memcpy(&ua->saddr, &x->props.saddr, sizeof(ua->saddr));
26361da177e4SLinus Torvalds 	memcpy(&ua->sel, &x->sel, sizeof(ua->sel));
263765e0736bSFan Du 	copy_to_user_policy(xp, &ua->policy, XFRM_POLICY_OUT);
26381da177e4SLinus Torvalds 	ua->aalgos = xt->aalgos;
26391da177e4SLinus Torvalds 	ua->ealgos = xt->ealgos;
26401da177e4SLinus Torvalds 	ua->calgos = xt->calgos;
26411da177e4SLinus Torvalds 	ua->seq = x->km.seq = seq;
26421da177e4SLinus Torvalds 
26431d1e34ddSDavid S. Miller 	err = copy_to_user_tmpl(xp, skb);
26441d1e34ddSDavid S. Miller 	if (!err)
26451d1e34ddSDavid S. Miller 		err = copy_to_user_state_sec_ctx(x, skb);
26461d1e34ddSDavid S. Miller 	if (!err)
26471d1e34ddSDavid S. Miller 		err = copy_to_user_policy_type(xp->type, skb);
26481d1e34ddSDavid S. Miller 	if (!err)
26491d1e34ddSDavid S. Miller 		err = xfrm_mark_put(skb, &xp->mark);
26501d1e34ddSDavid S. Miller 	if (err) {
26511d1e34ddSDavid S. Miller 		nlmsg_cancel(skb, nlh);
26521d1e34ddSDavid S. Miller 		return err;
26531d1e34ddSDavid S. Miller 	}
26541da177e4SLinus Torvalds 
26559825069dSThomas Graf 	return nlmsg_end(skb, nlh);
26561da177e4SLinus Torvalds }
26571da177e4SLinus Torvalds 
26581da177e4SLinus Torvalds static int xfrm_send_acquire(struct xfrm_state *x, struct xfrm_tmpl *xt,
265965e0736bSFan Du 			     struct xfrm_policy *xp)
26601da177e4SLinus Torvalds {
2661a6483b79SAlexey Dobriyan 	struct net *net = xs_net(x);
26621da177e4SLinus Torvalds 	struct sk_buff *skb;
26631da177e4SLinus Torvalds 
26647deb2264SThomas Graf 	skb = nlmsg_new(xfrm_acquire_msgsize(x, xp), GFP_ATOMIC);
26651da177e4SLinus Torvalds 	if (skb == NULL)
26661da177e4SLinus Torvalds 		return -ENOMEM;
26671da177e4SLinus Torvalds 
266865e0736bSFan Du 	if (build_acquire(skb, x, xt, xp) < 0)
26691da177e4SLinus Torvalds 		BUG();
26701da177e4SLinus Torvalds 
2671a6483b79SAlexey Dobriyan 	return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_ACQUIRE, GFP_ATOMIC);
26721da177e4SLinus Torvalds }
26731da177e4SLinus Torvalds 
26741da177e4SLinus Torvalds /* User gives us xfrm_user_policy_info followed by an array of 0
26751da177e4SLinus Torvalds  * or more templates.
26761da177e4SLinus Torvalds  */
2677cb969f07SVenkat Yekkirala static struct xfrm_policy *xfrm_compile_policy(struct sock *sk, int opt,
26781da177e4SLinus Torvalds 					       u8 *data, int len, int *dir)
26791da177e4SLinus Torvalds {
2680fc34acd3SAlexey Dobriyan 	struct net *net = sock_net(sk);
26811da177e4SLinus Torvalds 	struct xfrm_userpolicy_info *p = (struct xfrm_userpolicy_info *)data;
26821da177e4SLinus Torvalds 	struct xfrm_user_tmpl *ut = (struct xfrm_user_tmpl *) (p + 1);
26831da177e4SLinus Torvalds 	struct xfrm_policy *xp;
26841da177e4SLinus Torvalds 	int nr;
26851da177e4SLinus Torvalds 
2686cb969f07SVenkat Yekkirala 	switch (sk->sk_family) {
26871da177e4SLinus Torvalds 	case AF_INET:
26881da177e4SLinus Torvalds 		if (opt != IP_XFRM_POLICY) {
26891da177e4SLinus Torvalds 			*dir = -EOPNOTSUPP;
26901da177e4SLinus Torvalds 			return NULL;
26911da177e4SLinus Torvalds 		}
26921da177e4SLinus Torvalds 		break;
2693dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6)
26941da177e4SLinus Torvalds 	case AF_INET6:
26951da177e4SLinus Torvalds 		if (opt != IPV6_XFRM_POLICY) {
26961da177e4SLinus Torvalds 			*dir = -EOPNOTSUPP;
26971da177e4SLinus Torvalds 			return NULL;
26981da177e4SLinus Torvalds 		}
26991da177e4SLinus Torvalds 		break;
27001da177e4SLinus Torvalds #endif
27011da177e4SLinus Torvalds 	default:
27021da177e4SLinus Torvalds 		*dir = -EINVAL;
27031da177e4SLinus Torvalds 		return NULL;
27041da177e4SLinus Torvalds 	}
27051da177e4SLinus Torvalds 
27061da177e4SLinus Torvalds 	*dir = -EINVAL;
27071da177e4SLinus Torvalds 
27081da177e4SLinus Torvalds 	if (len < sizeof(*p) ||
27091da177e4SLinus Torvalds 	    verify_newpolicy_info(p))
27101da177e4SLinus Torvalds 		return NULL;
27111da177e4SLinus Torvalds 
27121da177e4SLinus Torvalds 	nr = ((len - sizeof(*p)) / sizeof(*ut));
2713b4ad86bfSDavid S. Miller 	if (validate_tmpl(nr, ut, p->sel.family))
27141da177e4SLinus Torvalds 		return NULL;
27151da177e4SLinus Torvalds 
2716a4f1bac6SHerbert Xu 	if (p->dir > XFRM_POLICY_OUT)
2717a4f1bac6SHerbert Xu 		return NULL;
2718a4f1bac6SHerbert Xu 
27192f09a4d5SHerbert Xu 	xp = xfrm_policy_alloc(net, GFP_ATOMIC);
27201da177e4SLinus Torvalds 	if (xp == NULL) {
27211da177e4SLinus Torvalds 		*dir = -ENOBUFS;
27221da177e4SLinus Torvalds 		return NULL;
27231da177e4SLinus Torvalds 	}
27241da177e4SLinus Torvalds 
27251da177e4SLinus Torvalds 	copy_from_user_policy(xp, p);
2726f7b6983fSMasahide NAKAMURA 	xp->type = XFRM_POLICY_TYPE_MAIN;
27271da177e4SLinus Torvalds 	copy_templates(xp, ut, nr);
27281da177e4SLinus Torvalds 
27291da177e4SLinus Torvalds 	*dir = p->dir;
27301da177e4SLinus Torvalds 
27311da177e4SLinus Torvalds 	return xp;
27321da177e4SLinus Torvalds }
27331da177e4SLinus Torvalds 
27347deb2264SThomas Graf static inline size_t xfrm_polexpire_msgsize(struct xfrm_policy *xp)
27357deb2264SThomas Graf {
27367deb2264SThomas Graf 	return NLMSG_ALIGN(sizeof(struct xfrm_user_polexpire))
27377deb2264SThomas Graf 	       + nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr)
27387deb2264SThomas Graf 	       + nla_total_size(xfrm_user_sec_ctx_size(xp->security))
2739295fae56SJamal Hadi Salim 	       + nla_total_size(sizeof(struct xfrm_mark))
27407deb2264SThomas Graf 	       + userpolicy_type_attrsize();
27417deb2264SThomas Graf }
27427deb2264SThomas Graf 
27431da177e4SLinus Torvalds static int build_polexpire(struct sk_buff *skb, struct xfrm_policy *xp,
2744214e005bSDavid S. Miller 			   int dir, const struct km_event *c)
27451da177e4SLinus Torvalds {
27461da177e4SLinus Torvalds 	struct xfrm_user_polexpire *upe;
2747d51d081dSJamal Hadi Salim 	int hard = c->data.hard;
27481d1e34ddSDavid S. Miller 	struct nlmsghdr *nlh;
27491d1e34ddSDavid S. Miller 	int err;
27501da177e4SLinus Torvalds 
275115e47304SEric W. Biederman 	nlh = nlmsg_put(skb, c->portid, 0, XFRM_MSG_POLEXPIRE, sizeof(*upe), 0);
275279b8b7f4SThomas Graf 	if (nlh == NULL)
275379b8b7f4SThomas Graf 		return -EMSGSIZE;
27541da177e4SLinus Torvalds 
27557b67c857SThomas Graf 	upe = nlmsg_data(nlh);
27561da177e4SLinus Torvalds 	copy_to_user_policy(xp, &upe->pol, dir);
27571d1e34ddSDavid S. Miller 	err = copy_to_user_tmpl(xp, skb);
27581d1e34ddSDavid S. Miller 	if (!err)
27591d1e34ddSDavid S. Miller 		err = copy_to_user_sec_ctx(xp, skb);
27601d1e34ddSDavid S. Miller 	if (!err)
27611d1e34ddSDavid S. Miller 		err = copy_to_user_policy_type(xp->type, skb);
27621d1e34ddSDavid S. Miller 	if (!err)
27631d1e34ddSDavid S. Miller 		err = xfrm_mark_put(skb, &xp->mark);
27641d1e34ddSDavid S. Miller 	if (err) {
27651d1e34ddSDavid S. Miller 		nlmsg_cancel(skb, nlh);
27661d1e34ddSDavid S. Miller 		return err;
27671d1e34ddSDavid S. Miller 	}
27681da177e4SLinus Torvalds 	upe->hard = !!hard;
27691da177e4SLinus Torvalds 
27709825069dSThomas Graf 	return nlmsg_end(skb, nlh);
27711da177e4SLinus Torvalds }
27721da177e4SLinus Torvalds 
2773214e005bSDavid S. Miller static int xfrm_exp_policy_notify(struct xfrm_policy *xp, int dir, const struct km_event *c)
27741da177e4SLinus Torvalds {
2775fc34acd3SAlexey Dobriyan 	struct net *net = xp_net(xp);
27761da177e4SLinus Torvalds 	struct sk_buff *skb;
27771da177e4SLinus Torvalds 
27787deb2264SThomas Graf 	skb = nlmsg_new(xfrm_polexpire_msgsize(xp), GFP_ATOMIC);
27791da177e4SLinus Torvalds 	if (skb == NULL)
27801da177e4SLinus Torvalds 		return -ENOMEM;
27811da177e4SLinus Torvalds 
2782d51d081dSJamal Hadi Salim 	if (build_polexpire(skb, xp, dir, c) < 0)
27831da177e4SLinus Torvalds 		BUG();
27841da177e4SLinus Torvalds 
2785a6483b79SAlexey Dobriyan 	return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_EXPIRE, GFP_ATOMIC);
27861da177e4SLinus Torvalds }
27871da177e4SLinus Torvalds 
2788214e005bSDavid S. Miller static int xfrm_notify_policy(struct xfrm_policy *xp, int dir, const struct km_event *c)
278926b15dadSJamal Hadi Salim {
27901d1e34ddSDavid S. Miller 	int len = nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr);
2791fc34acd3SAlexey Dobriyan 	struct net *net = xp_net(xp);
279226b15dadSJamal Hadi Salim 	struct xfrm_userpolicy_info *p;
27930603eac0SHerbert Xu 	struct xfrm_userpolicy_id *id;
279426b15dadSJamal Hadi Salim 	struct nlmsghdr *nlh;
279526b15dadSJamal Hadi Salim 	struct sk_buff *skb;
27961d1e34ddSDavid S. Miller 	int headlen, err;
27970603eac0SHerbert Xu 
27980603eac0SHerbert Xu 	headlen = sizeof(*p);
27990603eac0SHerbert Xu 	if (c->event == XFRM_MSG_DELPOLICY) {
28007deb2264SThomas Graf 		len += nla_total_size(headlen);
28010603eac0SHerbert Xu 		headlen = sizeof(*id);
28020603eac0SHerbert Xu 	}
2803cfbfd45aSThomas Graf 	len += userpolicy_type_attrsize();
2804295fae56SJamal Hadi Salim 	len += nla_total_size(sizeof(struct xfrm_mark));
28057deb2264SThomas Graf 	len += NLMSG_ALIGN(headlen);
280626b15dadSJamal Hadi Salim 
28077deb2264SThomas Graf 	skb = nlmsg_new(len, GFP_ATOMIC);
280826b15dadSJamal Hadi Salim 	if (skb == NULL)
280926b15dadSJamal Hadi Salim 		return -ENOMEM;
281026b15dadSJamal Hadi Salim 
281115e47304SEric W. Biederman 	nlh = nlmsg_put(skb, c->portid, c->seq, c->event, headlen, 0);
28121d1e34ddSDavid S. Miller 	err = -EMSGSIZE;
281379b8b7f4SThomas Graf 	if (nlh == NULL)
28141d1e34ddSDavid S. Miller 		goto out_free_skb;
281526b15dadSJamal Hadi Salim 
28167b67c857SThomas Graf 	p = nlmsg_data(nlh);
28170603eac0SHerbert Xu 	if (c->event == XFRM_MSG_DELPOLICY) {
2818c0144beaSThomas Graf 		struct nlattr *attr;
2819c0144beaSThomas Graf 
28207b67c857SThomas Graf 		id = nlmsg_data(nlh);
28210603eac0SHerbert Xu 		memset(id, 0, sizeof(*id));
28220603eac0SHerbert Xu 		id->dir = dir;
28230603eac0SHerbert Xu 		if (c->data.byid)
28240603eac0SHerbert Xu 			id->index = xp->index;
28250603eac0SHerbert Xu 		else
28260603eac0SHerbert Xu 			memcpy(&id->sel, &xp->selector, sizeof(id->sel));
28270603eac0SHerbert Xu 
2828c0144beaSThomas Graf 		attr = nla_reserve(skb, XFRMA_POLICY, sizeof(*p));
28291d1e34ddSDavid S. Miller 		err = -EMSGSIZE;
2830c0144beaSThomas Graf 		if (attr == NULL)
28311d1e34ddSDavid S. Miller 			goto out_free_skb;
2832c0144beaSThomas Graf 
2833c0144beaSThomas Graf 		p = nla_data(attr);
28340603eac0SHerbert Xu 	}
283526b15dadSJamal Hadi Salim 
283626b15dadSJamal Hadi Salim 	copy_to_user_policy(xp, p, dir);
28371d1e34ddSDavid S. Miller 	err = copy_to_user_tmpl(xp, skb);
28381d1e34ddSDavid S. Miller 	if (!err)
28391d1e34ddSDavid S. Miller 		err = copy_to_user_policy_type(xp->type, skb);
28401d1e34ddSDavid S. Miller 	if (!err)
28411d1e34ddSDavid S. Miller 		err = xfrm_mark_put(skb, &xp->mark);
28421d1e34ddSDavid S. Miller 	if (err)
28431d1e34ddSDavid S. Miller 		goto out_free_skb;
2844295fae56SJamal Hadi Salim 
28459825069dSThomas Graf 	nlmsg_end(skb, nlh);
284626b15dadSJamal Hadi Salim 
2847a6483b79SAlexey Dobriyan 	return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_POLICY, GFP_ATOMIC);
284826b15dadSJamal Hadi Salim 
28491d1e34ddSDavid S. Miller out_free_skb:
285026b15dadSJamal Hadi Salim 	kfree_skb(skb);
28511d1e34ddSDavid S. Miller 	return err;
285226b15dadSJamal Hadi Salim }
285326b15dadSJamal Hadi Salim 
2854214e005bSDavid S. Miller static int xfrm_notify_policy_flush(const struct km_event *c)
285526b15dadSJamal Hadi Salim {
28567067802eSAlexey Dobriyan 	struct net *net = c->net;
285726b15dadSJamal Hadi Salim 	struct nlmsghdr *nlh;
285826b15dadSJamal Hadi Salim 	struct sk_buff *skb;
28591d1e34ddSDavid S. Miller 	int err;
286026b15dadSJamal Hadi Salim 
28617deb2264SThomas Graf 	skb = nlmsg_new(userpolicy_type_attrsize(), GFP_ATOMIC);
286226b15dadSJamal Hadi Salim 	if (skb == NULL)
286326b15dadSJamal Hadi Salim 		return -ENOMEM;
286426b15dadSJamal Hadi Salim 
286515e47304SEric W. Biederman 	nlh = nlmsg_put(skb, c->portid, c->seq, XFRM_MSG_FLUSHPOLICY, 0, 0);
28661d1e34ddSDavid S. Miller 	err = -EMSGSIZE;
286779b8b7f4SThomas Graf 	if (nlh == NULL)
28681d1e34ddSDavid S. Miller 		goto out_free_skb;
28691d1e34ddSDavid S. Miller 	err = copy_to_user_policy_type(c->data.type, skb);
28701d1e34ddSDavid S. Miller 	if (err)
28711d1e34ddSDavid S. Miller 		goto out_free_skb;
287226b15dadSJamal Hadi Salim 
28739825069dSThomas Graf 	nlmsg_end(skb, nlh);
287426b15dadSJamal Hadi Salim 
2875a6483b79SAlexey Dobriyan 	return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_POLICY, GFP_ATOMIC);
287626b15dadSJamal Hadi Salim 
28771d1e34ddSDavid S. Miller out_free_skb:
287826b15dadSJamal Hadi Salim 	kfree_skb(skb);
28791d1e34ddSDavid S. Miller 	return err;
288026b15dadSJamal Hadi Salim }
288126b15dadSJamal Hadi Salim 
2882214e005bSDavid S. Miller static int xfrm_send_policy_notify(struct xfrm_policy *xp, int dir, const struct km_event *c)
288326b15dadSJamal Hadi Salim {
288426b15dadSJamal Hadi Salim 
288526b15dadSJamal Hadi Salim 	switch (c->event) {
2886f60f6b8fSHerbert Xu 	case XFRM_MSG_NEWPOLICY:
2887f60f6b8fSHerbert Xu 	case XFRM_MSG_UPDPOLICY:
2888f60f6b8fSHerbert Xu 	case XFRM_MSG_DELPOLICY:
288926b15dadSJamal Hadi Salim 		return xfrm_notify_policy(xp, dir, c);
2890f60f6b8fSHerbert Xu 	case XFRM_MSG_FLUSHPOLICY:
289126b15dadSJamal Hadi Salim 		return xfrm_notify_policy_flush(c);
2892f60f6b8fSHerbert Xu 	case XFRM_MSG_POLEXPIRE:
289326b15dadSJamal Hadi Salim 		return xfrm_exp_policy_notify(xp, dir, c);
289426b15dadSJamal Hadi Salim 	default:
289562db5cfdSstephen hemminger 		printk(KERN_NOTICE "xfrm_user: Unknown Policy event %d\n",
289662db5cfdSstephen hemminger 		       c->event);
289726b15dadSJamal Hadi Salim 	}
289826b15dadSJamal Hadi Salim 
289926b15dadSJamal Hadi Salim 	return 0;
290026b15dadSJamal Hadi Salim 
290126b15dadSJamal Hadi Salim }
290226b15dadSJamal Hadi Salim 
29037deb2264SThomas Graf static inline size_t xfrm_report_msgsize(void)
29047deb2264SThomas Graf {
29057deb2264SThomas Graf 	return NLMSG_ALIGN(sizeof(struct xfrm_user_report));
29067deb2264SThomas Graf }
29077deb2264SThomas Graf 
290897a64b45SMasahide NAKAMURA static int build_report(struct sk_buff *skb, u8 proto,
290997a64b45SMasahide NAKAMURA 			struct xfrm_selector *sel, xfrm_address_t *addr)
291097a64b45SMasahide NAKAMURA {
291197a64b45SMasahide NAKAMURA 	struct xfrm_user_report *ur;
291297a64b45SMasahide NAKAMURA 	struct nlmsghdr *nlh;
291397a64b45SMasahide NAKAMURA 
291479b8b7f4SThomas Graf 	nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_REPORT, sizeof(*ur), 0);
291579b8b7f4SThomas Graf 	if (nlh == NULL)
291679b8b7f4SThomas Graf 		return -EMSGSIZE;
291797a64b45SMasahide NAKAMURA 
29187b67c857SThomas Graf 	ur = nlmsg_data(nlh);
291997a64b45SMasahide NAKAMURA 	ur->proto = proto;
292097a64b45SMasahide NAKAMURA 	memcpy(&ur->sel, sel, sizeof(ur->sel));
292197a64b45SMasahide NAKAMURA 
29221d1e34ddSDavid S. Miller 	if (addr) {
29231d1e34ddSDavid S. Miller 		int err = nla_put(skb, XFRMA_COADDR, sizeof(*addr), addr);
29241d1e34ddSDavid S. Miller 		if (err) {
29259825069dSThomas Graf 			nlmsg_cancel(skb, nlh);
29261d1e34ddSDavid S. Miller 			return err;
29271d1e34ddSDavid S. Miller 		}
29281d1e34ddSDavid S. Miller 	}
29291d1e34ddSDavid S. Miller 	return nlmsg_end(skb, nlh);
293097a64b45SMasahide NAKAMURA }
293197a64b45SMasahide NAKAMURA 
2932db983c11SAlexey Dobriyan static int xfrm_send_report(struct net *net, u8 proto,
2933db983c11SAlexey Dobriyan 			    struct xfrm_selector *sel, xfrm_address_t *addr)
293497a64b45SMasahide NAKAMURA {
293597a64b45SMasahide NAKAMURA 	struct sk_buff *skb;
293697a64b45SMasahide NAKAMURA 
29377deb2264SThomas Graf 	skb = nlmsg_new(xfrm_report_msgsize(), GFP_ATOMIC);
293897a64b45SMasahide NAKAMURA 	if (skb == NULL)
293997a64b45SMasahide NAKAMURA 		return -ENOMEM;
294097a64b45SMasahide NAKAMURA 
294197a64b45SMasahide NAKAMURA 	if (build_report(skb, proto, sel, addr) < 0)
294297a64b45SMasahide NAKAMURA 		BUG();
294397a64b45SMasahide NAKAMURA 
2944a6483b79SAlexey Dobriyan 	return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_REPORT, GFP_ATOMIC);
294597a64b45SMasahide NAKAMURA }
294697a64b45SMasahide NAKAMURA 
29473a2dfbe8SMartin Willi static inline size_t xfrm_mapping_msgsize(void)
29483a2dfbe8SMartin Willi {
29493a2dfbe8SMartin Willi 	return NLMSG_ALIGN(sizeof(struct xfrm_user_mapping));
29503a2dfbe8SMartin Willi }
29513a2dfbe8SMartin Willi 
29523a2dfbe8SMartin Willi static int build_mapping(struct sk_buff *skb, struct xfrm_state *x,
29533a2dfbe8SMartin Willi 			 xfrm_address_t *new_saddr, __be16 new_sport)
29543a2dfbe8SMartin Willi {
29553a2dfbe8SMartin Willi 	struct xfrm_user_mapping *um;
29563a2dfbe8SMartin Willi 	struct nlmsghdr *nlh;
29573a2dfbe8SMartin Willi 
29583a2dfbe8SMartin Willi 	nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_MAPPING, sizeof(*um), 0);
29593a2dfbe8SMartin Willi 	if (nlh == NULL)
29603a2dfbe8SMartin Willi 		return -EMSGSIZE;
29613a2dfbe8SMartin Willi 
29623a2dfbe8SMartin Willi 	um = nlmsg_data(nlh);
29633a2dfbe8SMartin Willi 
29643a2dfbe8SMartin Willi 	memcpy(&um->id.daddr, &x->id.daddr, sizeof(um->id.daddr));
29653a2dfbe8SMartin Willi 	um->id.spi = x->id.spi;
29663a2dfbe8SMartin Willi 	um->id.family = x->props.family;
29673a2dfbe8SMartin Willi 	um->id.proto = x->id.proto;
29683a2dfbe8SMartin Willi 	memcpy(&um->new_saddr, new_saddr, sizeof(um->new_saddr));
29693a2dfbe8SMartin Willi 	memcpy(&um->old_saddr, &x->props.saddr, sizeof(um->old_saddr));
29703a2dfbe8SMartin Willi 	um->new_sport = new_sport;
29713a2dfbe8SMartin Willi 	um->old_sport = x->encap->encap_sport;
29723a2dfbe8SMartin Willi 	um->reqid = x->props.reqid;
29733a2dfbe8SMartin Willi 
29743a2dfbe8SMartin Willi 	return nlmsg_end(skb, nlh);
29753a2dfbe8SMartin Willi }
29763a2dfbe8SMartin Willi 
29773a2dfbe8SMartin Willi static int xfrm_send_mapping(struct xfrm_state *x, xfrm_address_t *ipaddr,
29783a2dfbe8SMartin Willi 			     __be16 sport)
29793a2dfbe8SMartin Willi {
2980a6483b79SAlexey Dobriyan 	struct net *net = xs_net(x);
29813a2dfbe8SMartin Willi 	struct sk_buff *skb;
29823a2dfbe8SMartin Willi 
29833a2dfbe8SMartin Willi 	if (x->id.proto != IPPROTO_ESP)
29843a2dfbe8SMartin Willi 		return -EINVAL;
29853a2dfbe8SMartin Willi 
29863a2dfbe8SMartin Willi 	if (!x->encap)
29873a2dfbe8SMartin Willi 		return -EINVAL;
29883a2dfbe8SMartin Willi 
29893a2dfbe8SMartin Willi 	skb = nlmsg_new(xfrm_mapping_msgsize(), GFP_ATOMIC);
29903a2dfbe8SMartin Willi 	if (skb == NULL)
29913a2dfbe8SMartin Willi 		return -ENOMEM;
29923a2dfbe8SMartin Willi 
29933a2dfbe8SMartin Willi 	if (build_mapping(skb, x, ipaddr, sport) < 0)
29943a2dfbe8SMartin Willi 		BUG();
29953a2dfbe8SMartin Willi 
2996a6483b79SAlexey Dobriyan 	return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_MAPPING, GFP_ATOMIC);
29973a2dfbe8SMartin Willi }
29983a2dfbe8SMartin Willi 
29991da177e4SLinus Torvalds static struct xfrm_mgr netlink_mgr = {
30001da177e4SLinus Torvalds 	.id		= "netlink",
30011da177e4SLinus Torvalds 	.notify		= xfrm_send_state_notify,
30021da177e4SLinus Torvalds 	.acquire	= xfrm_send_acquire,
30031da177e4SLinus Torvalds 	.compile_policy	= xfrm_compile_policy,
30041da177e4SLinus Torvalds 	.notify_policy	= xfrm_send_policy_notify,
300597a64b45SMasahide NAKAMURA 	.report		= xfrm_send_report,
30065c79de6eSShinta Sugimoto 	.migrate	= xfrm_send_migrate,
30073a2dfbe8SMartin Willi 	.new_mapping	= xfrm_send_mapping,
30081da177e4SLinus Torvalds };
30091da177e4SLinus Torvalds 
3010a6483b79SAlexey Dobriyan static int __net_init xfrm_user_net_init(struct net *net)
30111da177e4SLinus Torvalds {
3012be33690dSPatrick McHardy 	struct sock *nlsk;
3013a31f2d17SPablo Neira Ayuso 	struct netlink_kernel_cfg cfg = {
3014a31f2d17SPablo Neira Ayuso 		.groups	= XFRMNLGRP_MAX,
3015a31f2d17SPablo Neira Ayuso 		.input	= xfrm_netlink_rcv,
3016a31f2d17SPablo Neira Ayuso 	};
3017be33690dSPatrick McHardy 
30189f00d977SPablo Neira Ayuso 	nlsk = netlink_kernel_create(net, NETLINK_XFRM, &cfg);
3019be33690dSPatrick McHardy 	if (nlsk == NULL)
30201da177e4SLinus Torvalds 		return -ENOMEM;
3021d79d792eSEric W. Biederman 	net->xfrm.nlsk_stash = nlsk; /* Don't set to NULL */
3022cf778b00SEric Dumazet 	rcu_assign_pointer(net->xfrm.nlsk, nlsk);
30231da177e4SLinus Torvalds 	return 0;
30241da177e4SLinus Torvalds }
30251da177e4SLinus Torvalds 
3026d79d792eSEric W. Biederman static void __net_exit xfrm_user_net_exit(struct list_head *net_exit_list)
3027a6483b79SAlexey Dobriyan {
3028d79d792eSEric W. Biederman 	struct net *net;
3029d79d792eSEric W. Biederman 	list_for_each_entry(net, net_exit_list, exit_list)
3030a9b3cd7fSStephen Hemminger 		RCU_INIT_POINTER(net->xfrm.nlsk, NULL);
3031d79d792eSEric W. Biederman 	synchronize_net();
3032d79d792eSEric W. Biederman 	list_for_each_entry(net, net_exit_list, exit_list)
3033d79d792eSEric W. Biederman 		netlink_kernel_release(net->xfrm.nlsk_stash);
3034a6483b79SAlexey Dobriyan }
3035a6483b79SAlexey Dobriyan 
3036a6483b79SAlexey Dobriyan static struct pernet_operations xfrm_user_net_ops = {
3037a6483b79SAlexey Dobriyan 	.init	    = xfrm_user_net_init,
3038d79d792eSEric W. Biederman 	.exit_batch = xfrm_user_net_exit,
3039a6483b79SAlexey Dobriyan };
3040a6483b79SAlexey Dobriyan 
3041a6483b79SAlexey Dobriyan static int __init xfrm_user_init(void)
3042a6483b79SAlexey Dobriyan {
3043a6483b79SAlexey Dobriyan 	int rv;
3044a6483b79SAlexey Dobriyan 
3045a6483b79SAlexey Dobriyan 	printk(KERN_INFO "Initializing XFRM netlink socket\n");
3046a6483b79SAlexey Dobriyan 
3047a6483b79SAlexey Dobriyan 	rv = register_pernet_subsys(&xfrm_user_net_ops);
3048a6483b79SAlexey Dobriyan 	if (rv < 0)
3049a6483b79SAlexey Dobriyan 		return rv;
3050a6483b79SAlexey Dobriyan 	rv = xfrm_register_km(&netlink_mgr);
3051a6483b79SAlexey Dobriyan 	if (rv < 0)
3052a6483b79SAlexey Dobriyan 		unregister_pernet_subsys(&xfrm_user_net_ops);
3053a6483b79SAlexey Dobriyan 	return rv;
3054a6483b79SAlexey Dobriyan }
3055a6483b79SAlexey Dobriyan 
30561da177e4SLinus Torvalds static void __exit xfrm_user_exit(void)
30571da177e4SLinus Torvalds {
30581da177e4SLinus Torvalds 	xfrm_unregister_km(&netlink_mgr);
3059a6483b79SAlexey Dobriyan 	unregister_pernet_subsys(&xfrm_user_net_ops);
30601da177e4SLinus Torvalds }
30611da177e4SLinus Torvalds 
30621da177e4SLinus Torvalds module_init(xfrm_user_init);
30631da177e4SLinus Torvalds module_exit(xfrm_user_exit);
30641da177e4SLinus Torvalds MODULE_LICENSE("GPL");
30654fdb3bb7SHarald Welte MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK, NETLINK_XFRM);
3066f8cd5488SJamal Hadi Salim 
3067