xref: /linux/net/xfrm/xfrm_user.c (revision cfbfd45a8c4c0c8dd8ed491caefdeffd94acf9e4)
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/rtnetlink.h>
231da177e4SLinus Torvalds #include <linux/pfkeyv2.h>
241da177e4SLinus Torvalds #include <linux/ipsec.h>
251da177e4SLinus Torvalds #include <linux/init.h>
261da177e4SLinus Torvalds #include <linux/security.h>
271da177e4SLinus Torvalds #include <net/sock.h>
281da177e4SLinus Torvalds #include <net/xfrm.h>
2988fc2c84SThomas Graf #include <net/netlink.h>
301da177e4SLinus Torvalds #include <asm/uaccess.h>
31e23c7194SMasahide NAKAMURA #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
32e23c7194SMasahide NAKAMURA #include <linux/in6.h>
33e23c7194SMasahide NAKAMURA #endif
34161a09e7SJoy Latten #include <linux/audit.h>
351da177e4SLinus Torvalds 
36c26445acSThomas Graf static inline int alg_len(struct xfrm_algo *alg)
37c26445acSThomas Graf {
38c26445acSThomas Graf 	return sizeof(*alg) + ((alg->alg_key_len + 7) / 8);
39c26445acSThomas Graf }
40c26445acSThomas Graf 
411da177e4SLinus Torvalds static int verify_one_alg(struct rtattr **xfrma, enum xfrm_attr_type_t type)
421da177e4SLinus Torvalds {
431da177e4SLinus Torvalds 	struct rtattr *rt = xfrma[type - 1];
441da177e4SLinus Torvalds 	struct xfrm_algo *algp;
4531c26852SHerbert Xu 	int len;
461da177e4SLinus Torvalds 
471da177e4SLinus Torvalds 	if (!rt)
481da177e4SLinus Torvalds 		return 0;
491da177e4SLinus Torvalds 
5031c26852SHerbert Xu 	len = (rt->rta_len - sizeof(*rt)) - sizeof(*algp);
5131c26852SHerbert Xu 	if (len < 0)
521da177e4SLinus Torvalds 		return -EINVAL;
531da177e4SLinus Torvalds 
541da177e4SLinus Torvalds 	algp = RTA_DATA(rt);
5531c26852SHerbert Xu 
5631c26852SHerbert Xu 	len -= (algp->alg_key_len + 7U) / 8;
5731c26852SHerbert Xu 	if (len < 0)
5831c26852SHerbert Xu 		return -EINVAL;
5931c26852SHerbert Xu 
601da177e4SLinus Torvalds 	switch (type) {
611da177e4SLinus Torvalds 	case XFRMA_ALG_AUTH:
621da177e4SLinus Torvalds 		if (!algp->alg_key_len &&
631da177e4SLinus Torvalds 		    strcmp(algp->alg_name, "digest_null") != 0)
641da177e4SLinus Torvalds 			return -EINVAL;
651da177e4SLinus Torvalds 		break;
661da177e4SLinus Torvalds 
671da177e4SLinus Torvalds 	case XFRMA_ALG_CRYPT:
681da177e4SLinus Torvalds 		if (!algp->alg_key_len &&
691da177e4SLinus Torvalds 		    strcmp(algp->alg_name, "cipher_null") != 0)
701da177e4SLinus Torvalds 			return -EINVAL;
711da177e4SLinus Torvalds 		break;
721da177e4SLinus Torvalds 
731da177e4SLinus Torvalds 	case XFRMA_ALG_COMP:
741da177e4SLinus Torvalds 		/* Zero length keys are legal.  */
751da177e4SLinus Torvalds 		break;
761da177e4SLinus Torvalds 
771da177e4SLinus Torvalds 	default:
781da177e4SLinus Torvalds 		return -EINVAL;
793ff50b79SStephen Hemminger 	}
801da177e4SLinus Torvalds 
811da177e4SLinus Torvalds 	algp->alg_name[CRYPTO_MAX_ALG_NAME - 1] = '\0';
821da177e4SLinus Torvalds 	return 0;
831da177e4SLinus Torvalds }
841da177e4SLinus Torvalds 
851da177e4SLinus Torvalds static int verify_encap_tmpl(struct rtattr **xfrma)
861da177e4SLinus Torvalds {
871da177e4SLinus Torvalds 	struct rtattr *rt = xfrma[XFRMA_ENCAP - 1];
881da177e4SLinus Torvalds 	struct xfrm_encap_tmpl *encap;
891da177e4SLinus Torvalds 
901da177e4SLinus Torvalds 	if (!rt)
911da177e4SLinus Torvalds 		return 0;
921da177e4SLinus Torvalds 
931da177e4SLinus Torvalds 	if ((rt->rta_len - sizeof(*rt)) < sizeof(*encap))
941da177e4SLinus Torvalds 		return -EINVAL;
951da177e4SLinus Torvalds 
961da177e4SLinus Torvalds 	return 0;
971da177e4SLinus Torvalds }
981da177e4SLinus Torvalds 
99eb2971b6SMasahide NAKAMURA static int verify_one_addr(struct rtattr **xfrma, enum xfrm_attr_type_t type,
100eb2971b6SMasahide NAKAMURA 			   xfrm_address_t **addrp)
101eb2971b6SMasahide NAKAMURA {
102eb2971b6SMasahide NAKAMURA 	struct rtattr *rt = xfrma[type - 1];
103eb2971b6SMasahide NAKAMURA 
104eb2971b6SMasahide NAKAMURA 	if (!rt)
105eb2971b6SMasahide NAKAMURA 		return 0;
106eb2971b6SMasahide NAKAMURA 
107eb2971b6SMasahide NAKAMURA 	if ((rt->rta_len - sizeof(*rt)) < sizeof(**addrp))
108eb2971b6SMasahide NAKAMURA 		return -EINVAL;
109eb2971b6SMasahide NAKAMURA 
110eb2971b6SMasahide NAKAMURA 	if (addrp)
111eb2971b6SMasahide NAKAMURA 		*addrp = RTA_DATA(rt);
112eb2971b6SMasahide NAKAMURA 
113eb2971b6SMasahide NAKAMURA 	return 0;
114eb2971b6SMasahide NAKAMURA }
115df71837dSTrent Jaeger 
116df71837dSTrent Jaeger static inline int verify_sec_ctx_len(struct rtattr **xfrma)
117df71837dSTrent Jaeger {
118df71837dSTrent Jaeger 	struct rtattr *rt = xfrma[XFRMA_SEC_CTX - 1];
119df71837dSTrent Jaeger 	struct xfrm_user_sec_ctx *uctx;
120df71837dSTrent Jaeger 	int len = 0;
121df71837dSTrent Jaeger 
122df71837dSTrent Jaeger 	if (!rt)
123df71837dSTrent Jaeger 		return 0;
124df71837dSTrent Jaeger 
125df71837dSTrent Jaeger 	if (rt->rta_len < sizeof(*uctx))
126df71837dSTrent Jaeger 		return -EINVAL;
127df71837dSTrent Jaeger 
128df71837dSTrent Jaeger 	uctx = RTA_DATA(rt);
129df71837dSTrent Jaeger 
130df71837dSTrent Jaeger 	len += sizeof(struct xfrm_user_sec_ctx);
131df71837dSTrent Jaeger 	len += uctx->ctx_len;
132df71837dSTrent Jaeger 
133df71837dSTrent Jaeger 	if (uctx->len != len)
134df71837dSTrent Jaeger 		return -EINVAL;
135df71837dSTrent Jaeger 
136df71837dSTrent Jaeger 	return 0;
137df71837dSTrent Jaeger }
138df71837dSTrent Jaeger 
139df71837dSTrent Jaeger 
1401da177e4SLinus Torvalds static int verify_newsa_info(struct xfrm_usersa_info *p,
1411da177e4SLinus Torvalds 			     struct rtattr **xfrma)
1421da177e4SLinus Torvalds {
1431da177e4SLinus Torvalds 	int err;
1441da177e4SLinus Torvalds 
1451da177e4SLinus Torvalds 	err = -EINVAL;
1461da177e4SLinus Torvalds 	switch (p->family) {
1471da177e4SLinus Torvalds 	case AF_INET:
1481da177e4SLinus Torvalds 		break;
1491da177e4SLinus Torvalds 
1501da177e4SLinus Torvalds 	case AF_INET6:
1511da177e4SLinus Torvalds #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1521da177e4SLinus Torvalds 		break;
1531da177e4SLinus Torvalds #else
1541da177e4SLinus Torvalds 		err = -EAFNOSUPPORT;
1551da177e4SLinus Torvalds 		goto out;
1561da177e4SLinus Torvalds #endif
1571da177e4SLinus Torvalds 
1581da177e4SLinus Torvalds 	default:
1591da177e4SLinus Torvalds 		goto out;
1603ff50b79SStephen Hemminger 	}
1611da177e4SLinus Torvalds 
1621da177e4SLinus Torvalds 	err = -EINVAL;
1631da177e4SLinus Torvalds 	switch (p->id.proto) {
1641da177e4SLinus Torvalds 	case IPPROTO_AH:
1651da177e4SLinus Torvalds 		if (!xfrma[XFRMA_ALG_AUTH-1]	||
1661da177e4SLinus Torvalds 		    xfrma[XFRMA_ALG_CRYPT-1]	||
1671da177e4SLinus Torvalds 		    xfrma[XFRMA_ALG_COMP-1])
1681da177e4SLinus Torvalds 			goto out;
1691da177e4SLinus Torvalds 		break;
1701da177e4SLinus Torvalds 
1711da177e4SLinus Torvalds 	case IPPROTO_ESP:
1721da177e4SLinus Torvalds 		if ((!xfrma[XFRMA_ALG_AUTH-1] &&
1731da177e4SLinus Torvalds 		     !xfrma[XFRMA_ALG_CRYPT-1])	||
1741da177e4SLinus Torvalds 		    xfrma[XFRMA_ALG_COMP-1])
1751da177e4SLinus Torvalds 			goto out;
1761da177e4SLinus Torvalds 		break;
1771da177e4SLinus Torvalds 
1781da177e4SLinus Torvalds 	case IPPROTO_COMP:
1791da177e4SLinus Torvalds 		if (!xfrma[XFRMA_ALG_COMP-1]	||
1801da177e4SLinus Torvalds 		    xfrma[XFRMA_ALG_AUTH-1]	||
1811da177e4SLinus Torvalds 		    xfrma[XFRMA_ALG_CRYPT-1])
1821da177e4SLinus Torvalds 			goto out;
1831da177e4SLinus Torvalds 		break;
1841da177e4SLinus Torvalds 
185e23c7194SMasahide NAKAMURA #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
186e23c7194SMasahide NAKAMURA 	case IPPROTO_DSTOPTS:
187e23c7194SMasahide NAKAMURA 	case IPPROTO_ROUTING:
188e23c7194SMasahide NAKAMURA 		if (xfrma[XFRMA_ALG_COMP-1]	||
189e23c7194SMasahide NAKAMURA 		    xfrma[XFRMA_ALG_AUTH-1]	||
190e23c7194SMasahide NAKAMURA 		    xfrma[XFRMA_ALG_CRYPT-1]	||
191e23c7194SMasahide NAKAMURA 		    xfrma[XFRMA_ENCAP-1]	||
192e23c7194SMasahide NAKAMURA 		    xfrma[XFRMA_SEC_CTX-1]	||
193e23c7194SMasahide NAKAMURA 		    !xfrma[XFRMA_COADDR-1])
194e23c7194SMasahide NAKAMURA 			goto out;
195e23c7194SMasahide NAKAMURA 		break;
196e23c7194SMasahide NAKAMURA #endif
197e23c7194SMasahide NAKAMURA 
1981da177e4SLinus Torvalds 	default:
1991da177e4SLinus Torvalds 		goto out;
2003ff50b79SStephen Hemminger 	}
2011da177e4SLinus Torvalds 
2021da177e4SLinus Torvalds 	if ((err = verify_one_alg(xfrma, XFRMA_ALG_AUTH)))
2031da177e4SLinus Torvalds 		goto out;
2041da177e4SLinus Torvalds 	if ((err = verify_one_alg(xfrma, XFRMA_ALG_CRYPT)))
2051da177e4SLinus Torvalds 		goto out;
2061da177e4SLinus Torvalds 	if ((err = verify_one_alg(xfrma, XFRMA_ALG_COMP)))
2071da177e4SLinus Torvalds 		goto out;
2081da177e4SLinus Torvalds 	if ((err = verify_encap_tmpl(xfrma)))
2091da177e4SLinus Torvalds 		goto out;
210df71837dSTrent Jaeger 	if ((err = verify_sec_ctx_len(xfrma)))
211df71837dSTrent Jaeger 		goto out;
212060f02a3SNoriaki TAKAMIYA 	if ((err = verify_one_addr(xfrma, XFRMA_COADDR, NULL)))
213060f02a3SNoriaki TAKAMIYA 		goto out;
2141da177e4SLinus Torvalds 
2151da177e4SLinus Torvalds 	err = -EINVAL;
2161da177e4SLinus Torvalds 	switch (p->mode) {
2177e49e6deSMasahide NAKAMURA 	case XFRM_MODE_TRANSPORT:
2187e49e6deSMasahide NAKAMURA 	case XFRM_MODE_TUNNEL:
219060f02a3SNoriaki TAKAMIYA 	case XFRM_MODE_ROUTEOPTIMIZATION:
2200a69452cSDiego Beltrami 	case XFRM_MODE_BEET:
2211da177e4SLinus Torvalds 		break;
2221da177e4SLinus Torvalds 
2231da177e4SLinus Torvalds 	default:
2241da177e4SLinus Torvalds 		goto out;
2253ff50b79SStephen Hemminger 	}
2261da177e4SLinus Torvalds 
2271da177e4SLinus Torvalds 	err = 0;
2281da177e4SLinus Torvalds 
2291da177e4SLinus Torvalds out:
2301da177e4SLinus Torvalds 	return err;
2311da177e4SLinus Torvalds }
2321da177e4SLinus Torvalds 
2331da177e4SLinus Torvalds static int attach_one_algo(struct xfrm_algo **algpp, u8 *props,
2341da177e4SLinus Torvalds 			   struct xfrm_algo_desc *(*get_byname)(char *, int),
2351da177e4SLinus Torvalds 			   struct rtattr *u_arg)
2361da177e4SLinus Torvalds {
2371da177e4SLinus Torvalds 	struct rtattr *rta = u_arg;
2381da177e4SLinus Torvalds 	struct xfrm_algo *p, *ualg;
2391da177e4SLinus Torvalds 	struct xfrm_algo_desc *algo;
2401da177e4SLinus Torvalds 
2411da177e4SLinus Torvalds 	if (!rta)
2421da177e4SLinus Torvalds 		return 0;
2431da177e4SLinus Torvalds 
2441da177e4SLinus Torvalds 	ualg = RTA_DATA(rta);
2451da177e4SLinus Torvalds 
2461da177e4SLinus Torvalds 	algo = get_byname(ualg->alg_name, 1);
2471da177e4SLinus Torvalds 	if (!algo)
2481da177e4SLinus Torvalds 		return -ENOSYS;
2491da177e4SLinus Torvalds 	*props = algo->desc.sadb_alg_id;
2501da177e4SLinus Torvalds 
251c26445acSThomas Graf 	p = kmemdup(ualg, alg_len(ualg), GFP_KERNEL);
2521da177e4SLinus Torvalds 	if (!p)
2531da177e4SLinus Torvalds 		return -ENOMEM;
2541da177e4SLinus Torvalds 
25504ff1260SHerbert Xu 	strcpy(p->alg_name, algo->name);
2561da177e4SLinus Torvalds 	*algpp = p;
2571da177e4SLinus Torvalds 	return 0;
2581da177e4SLinus Torvalds }
2591da177e4SLinus Torvalds 
2601da177e4SLinus Torvalds static int attach_encap_tmpl(struct xfrm_encap_tmpl **encapp, struct rtattr *u_arg)
2611da177e4SLinus Torvalds {
2621da177e4SLinus Torvalds 	struct rtattr *rta = u_arg;
2631da177e4SLinus Torvalds 	struct xfrm_encap_tmpl *p, *uencap;
2641da177e4SLinus Torvalds 
2651da177e4SLinus Torvalds 	if (!rta)
2661da177e4SLinus Torvalds 		return 0;
2671da177e4SLinus Torvalds 
2681da177e4SLinus Torvalds 	uencap = RTA_DATA(rta);
269cdbc6daeSArnaldo Carvalho de Melo 	p = kmemdup(uencap, sizeof(*p), GFP_KERNEL);
2701da177e4SLinus Torvalds 	if (!p)
2711da177e4SLinus Torvalds 		return -ENOMEM;
2721da177e4SLinus Torvalds 
2731da177e4SLinus Torvalds 	*encapp = p;
2741da177e4SLinus Torvalds 	return 0;
2751da177e4SLinus Torvalds }
2761da177e4SLinus Torvalds 
277df71837dSTrent Jaeger 
278661697f7SJoy Latten static inline int xfrm_user_sec_ctx_size(struct xfrm_sec_ctx *xfrm_ctx)
279df71837dSTrent Jaeger {
280df71837dSTrent Jaeger 	int len = 0;
281df71837dSTrent Jaeger 
282df71837dSTrent Jaeger 	if (xfrm_ctx) {
283df71837dSTrent Jaeger 		len += sizeof(struct xfrm_user_sec_ctx);
284df71837dSTrent Jaeger 		len += xfrm_ctx->ctx_len;
285df71837dSTrent Jaeger 	}
286df71837dSTrent Jaeger 	return len;
287df71837dSTrent Jaeger }
288df71837dSTrent Jaeger 
289df71837dSTrent Jaeger static int attach_sec_ctx(struct xfrm_state *x, struct rtattr *u_arg)
290df71837dSTrent Jaeger {
291df71837dSTrent Jaeger 	struct xfrm_user_sec_ctx *uctx;
292df71837dSTrent Jaeger 
293df71837dSTrent Jaeger 	if (!u_arg)
294df71837dSTrent Jaeger 		return 0;
295df71837dSTrent Jaeger 
296df71837dSTrent Jaeger 	uctx = RTA_DATA(u_arg);
297df71837dSTrent Jaeger 	return security_xfrm_state_alloc(x, uctx);
298df71837dSTrent Jaeger }
299df71837dSTrent Jaeger 
300060f02a3SNoriaki TAKAMIYA static int attach_one_addr(xfrm_address_t **addrpp, struct rtattr *u_arg)
301060f02a3SNoriaki TAKAMIYA {
302060f02a3SNoriaki TAKAMIYA 	struct rtattr *rta = u_arg;
303060f02a3SNoriaki TAKAMIYA 	xfrm_address_t *p, *uaddrp;
304060f02a3SNoriaki TAKAMIYA 
305060f02a3SNoriaki TAKAMIYA 	if (!rta)
306060f02a3SNoriaki TAKAMIYA 		return 0;
307060f02a3SNoriaki TAKAMIYA 
308060f02a3SNoriaki TAKAMIYA 	uaddrp = RTA_DATA(rta);
309cdbc6daeSArnaldo Carvalho de Melo 	p = kmemdup(uaddrp, sizeof(*p), GFP_KERNEL);
310060f02a3SNoriaki TAKAMIYA 	if (!p)
311060f02a3SNoriaki TAKAMIYA 		return -ENOMEM;
312060f02a3SNoriaki TAKAMIYA 
313060f02a3SNoriaki TAKAMIYA 	*addrpp = p;
314060f02a3SNoriaki TAKAMIYA 	return 0;
315060f02a3SNoriaki TAKAMIYA }
316060f02a3SNoriaki TAKAMIYA 
3171da177e4SLinus Torvalds static void copy_from_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p)
3181da177e4SLinus Torvalds {
3191da177e4SLinus Torvalds 	memcpy(&x->id, &p->id, sizeof(x->id));
3201da177e4SLinus Torvalds 	memcpy(&x->sel, &p->sel, sizeof(x->sel));
3211da177e4SLinus Torvalds 	memcpy(&x->lft, &p->lft, sizeof(x->lft));
3221da177e4SLinus Torvalds 	x->props.mode = p->mode;
3231da177e4SLinus Torvalds 	x->props.replay_window = p->replay_window;
3241da177e4SLinus Torvalds 	x->props.reqid = p->reqid;
3251da177e4SLinus Torvalds 	x->props.family = p->family;
32654489c14SDavid S. Miller 	memcpy(&x->props.saddr, &p->saddr, sizeof(x->props.saddr));
3271da177e4SLinus Torvalds 	x->props.flags = p->flags;
328196b0036SHerbert Xu 
329196b0036SHerbert Xu 	/*
330196b0036SHerbert Xu 	 * Set inner address family if the KM left it as zero.
331196b0036SHerbert Xu 	 * See comment in validate_tmpl.
332196b0036SHerbert Xu 	 */
333196b0036SHerbert Xu 	if (!x->sel.family)
334196b0036SHerbert Xu 		x->sel.family = p->family;
3351da177e4SLinus Torvalds }
3361da177e4SLinus Torvalds 
337d51d081dSJamal Hadi Salim /*
338d51d081dSJamal Hadi Salim  * someday when pfkey also has support, we could have the code
339d51d081dSJamal Hadi Salim  * somehow made shareable and move it to xfrm_state.c - JHS
340d51d081dSJamal Hadi Salim  *
341d51d081dSJamal Hadi Salim */
342d51d081dSJamal Hadi Salim static int xfrm_update_ae_params(struct xfrm_state *x, struct rtattr **xfrma)
343d51d081dSJamal Hadi Salim {
344d51d081dSJamal Hadi Salim 	int err = - EINVAL;
345d51d081dSJamal Hadi Salim 	struct rtattr *rp = xfrma[XFRMA_REPLAY_VAL-1];
346d51d081dSJamal Hadi Salim 	struct rtattr *lt = xfrma[XFRMA_LTIME_VAL-1];
347d51d081dSJamal Hadi Salim 	struct rtattr *et = xfrma[XFRMA_ETIMER_THRESH-1];
348d51d081dSJamal Hadi Salim 	struct rtattr *rt = xfrma[XFRMA_REPLAY_THRESH-1];
349d51d081dSJamal Hadi Salim 
350d51d081dSJamal Hadi Salim 	if (rp) {
351d51d081dSJamal Hadi Salim 		struct xfrm_replay_state *replay;
352d51d081dSJamal Hadi Salim 		if (RTA_PAYLOAD(rp) < sizeof(*replay))
353d51d081dSJamal Hadi Salim 			goto error;
354d51d081dSJamal Hadi Salim 		replay = RTA_DATA(rp);
355d51d081dSJamal Hadi Salim 		memcpy(&x->replay, replay, sizeof(*replay));
356d51d081dSJamal Hadi Salim 		memcpy(&x->preplay, replay, sizeof(*replay));
357d51d081dSJamal Hadi Salim 	}
358d51d081dSJamal Hadi Salim 
359d51d081dSJamal Hadi Salim 	if (lt) {
360d51d081dSJamal Hadi Salim 		struct xfrm_lifetime_cur *ltime;
361d51d081dSJamal Hadi Salim 		if (RTA_PAYLOAD(lt) < sizeof(*ltime))
362d51d081dSJamal Hadi Salim 			goto error;
363d51d081dSJamal Hadi Salim 		ltime = RTA_DATA(lt);
364d51d081dSJamal Hadi Salim 		x->curlft.bytes = ltime->bytes;
365d51d081dSJamal Hadi Salim 		x->curlft.packets = ltime->packets;
366d51d081dSJamal Hadi Salim 		x->curlft.add_time = ltime->add_time;
367d51d081dSJamal Hadi Salim 		x->curlft.use_time = ltime->use_time;
368d51d081dSJamal Hadi Salim 	}
369d51d081dSJamal Hadi Salim 
370d51d081dSJamal Hadi Salim 	if (et) {
371d51d081dSJamal Hadi Salim 		if (RTA_PAYLOAD(et) < sizeof(u32))
372d51d081dSJamal Hadi Salim 			goto error;
373d51d081dSJamal Hadi Salim 		x->replay_maxage = *(u32*)RTA_DATA(et);
374d51d081dSJamal Hadi Salim 	}
375d51d081dSJamal Hadi Salim 
376d51d081dSJamal Hadi Salim 	if (rt) {
377d51d081dSJamal Hadi Salim 		if (RTA_PAYLOAD(rt) < sizeof(u32))
378d51d081dSJamal Hadi Salim 			goto error;
379d51d081dSJamal Hadi Salim 		x->replay_maxdiff = *(u32*)RTA_DATA(rt);
380d51d081dSJamal Hadi Salim 	}
381d51d081dSJamal Hadi Salim 
382d51d081dSJamal Hadi Salim 	return 0;
383d51d081dSJamal Hadi Salim error:
384d51d081dSJamal Hadi Salim 	return err;
385d51d081dSJamal Hadi Salim }
386d51d081dSJamal Hadi Salim 
3871da177e4SLinus Torvalds static struct xfrm_state *xfrm_state_construct(struct xfrm_usersa_info *p,
3881da177e4SLinus Torvalds 					       struct rtattr **xfrma,
3891da177e4SLinus Torvalds 					       int *errp)
3901da177e4SLinus Torvalds {
3911da177e4SLinus Torvalds 	struct xfrm_state *x = xfrm_state_alloc();
3921da177e4SLinus Torvalds 	int err = -ENOMEM;
3931da177e4SLinus Torvalds 
3941da177e4SLinus Torvalds 	if (!x)
3951da177e4SLinus Torvalds 		goto error_no_put;
3961da177e4SLinus Torvalds 
3971da177e4SLinus Torvalds 	copy_from_user_state(x, p);
3981da177e4SLinus Torvalds 
3991da177e4SLinus Torvalds 	if ((err = attach_one_algo(&x->aalg, &x->props.aalgo,
4001da177e4SLinus Torvalds 				   xfrm_aalg_get_byname,
4011da177e4SLinus Torvalds 				   xfrma[XFRMA_ALG_AUTH-1])))
4021da177e4SLinus Torvalds 		goto error;
4031da177e4SLinus Torvalds 	if ((err = attach_one_algo(&x->ealg, &x->props.ealgo,
4041da177e4SLinus Torvalds 				   xfrm_ealg_get_byname,
4051da177e4SLinus Torvalds 				   xfrma[XFRMA_ALG_CRYPT-1])))
4061da177e4SLinus Torvalds 		goto error;
4071da177e4SLinus Torvalds 	if ((err = attach_one_algo(&x->calg, &x->props.calgo,
4081da177e4SLinus Torvalds 				   xfrm_calg_get_byname,
4091da177e4SLinus Torvalds 				   xfrma[XFRMA_ALG_COMP-1])))
4101da177e4SLinus Torvalds 		goto error;
4111da177e4SLinus Torvalds 	if ((err = attach_encap_tmpl(&x->encap, xfrma[XFRMA_ENCAP-1])))
4121da177e4SLinus Torvalds 		goto error;
413060f02a3SNoriaki TAKAMIYA 	if ((err = attach_one_addr(&x->coaddr, xfrma[XFRMA_COADDR-1])))
414060f02a3SNoriaki TAKAMIYA 		goto error;
41572cb6962SHerbert Xu 	err = xfrm_init_state(x);
4161da177e4SLinus Torvalds 	if (err)
4171da177e4SLinus Torvalds 		goto error;
4181da177e4SLinus Torvalds 
419df71837dSTrent Jaeger 	if ((err = attach_sec_ctx(x, xfrma[XFRMA_SEC_CTX-1])))
420df71837dSTrent Jaeger 		goto error;
421df71837dSTrent Jaeger 
4221da177e4SLinus Torvalds 	x->km.seq = p->seq;
423d51d081dSJamal Hadi Salim 	x->replay_maxdiff = sysctl_xfrm_aevent_rseqth;
424d51d081dSJamal Hadi Salim 	/* sysctl_xfrm_aevent_etime is in 100ms units */
425d51d081dSJamal Hadi Salim 	x->replay_maxage = (sysctl_xfrm_aevent_etime*HZ)/XFRM_AE_ETH_M;
426d51d081dSJamal Hadi Salim 	x->preplay.bitmap = 0;
427d51d081dSJamal Hadi Salim 	x->preplay.seq = x->replay.seq+x->replay_maxdiff;
428d51d081dSJamal Hadi Salim 	x->preplay.oseq = x->replay.oseq +x->replay_maxdiff;
429d51d081dSJamal Hadi Salim 
430d51d081dSJamal Hadi Salim 	/* override default values from above */
431d51d081dSJamal Hadi Salim 
432d51d081dSJamal Hadi Salim 	err = xfrm_update_ae_params(x, (struct rtattr **)xfrma);
433d51d081dSJamal Hadi Salim 	if (err	< 0)
434d51d081dSJamal Hadi Salim 		goto error;
4351da177e4SLinus Torvalds 
4361da177e4SLinus Torvalds 	return x;
4371da177e4SLinus Torvalds 
4381da177e4SLinus Torvalds error:
4391da177e4SLinus Torvalds 	x->km.state = XFRM_STATE_DEAD;
4401da177e4SLinus Torvalds 	xfrm_state_put(x);
4411da177e4SLinus Torvalds error_no_put:
4421da177e4SLinus Torvalds 	*errp = err;
4431da177e4SLinus Torvalds 	return NULL;
4441da177e4SLinus Torvalds }
4451da177e4SLinus Torvalds 
44622e70050SChristoph Hellwig static int xfrm_add_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
44722e70050SChristoph Hellwig 		struct rtattr **xfrma)
4481da177e4SLinus Torvalds {
4497b67c857SThomas Graf 	struct xfrm_usersa_info *p = nlmsg_data(nlh);
4501da177e4SLinus Torvalds 	struct xfrm_state *x;
4511da177e4SLinus Torvalds 	int err;
45226b15dadSJamal Hadi Salim 	struct km_event c;
4531da177e4SLinus Torvalds 
45422e70050SChristoph Hellwig 	err = verify_newsa_info(p, xfrma);
4551da177e4SLinus Torvalds 	if (err)
4561da177e4SLinus Torvalds 		return err;
4571da177e4SLinus Torvalds 
45822e70050SChristoph Hellwig 	x = xfrm_state_construct(p, xfrma, &err);
4591da177e4SLinus Torvalds 	if (!x)
4601da177e4SLinus Torvalds 		return err;
4611da177e4SLinus Torvalds 
46226b15dadSJamal Hadi Salim 	xfrm_state_hold(x);
4631da177e4SLinus Torvalds 	if (nlh->nlmsg_type == XFRM_MSG_NEWSA)
4641da177e4SLinus Torvalds 		err = xfrm_state_add(x);
4651da177e4SLinus Torvalds 	else
4661da177e4SLinus Torvalds 		err = xfrm_state_update(x);
4671da177e4SLinus Torvalds 
468161a09e7SJoy Latten 	xfrm_audit_log(NETLINK_CB(skb).loginuid, NETLINK_CB(skb).sid,
469161a09e7SJoy Latten 		       AUDIT_MAC_IPSEC_ADDSA, err ? 0 : 1, NULL, x);
470161a09e7SJoy Latten 
4711da177e4SLinus Torvalds 	if (err < 0) {
4721da177e4SLinus Torvalds 		x->km.state = XFRM_STATE_DEAD;
47321380b81SHerbert Xu 		__xfrm_state_put(x);
4747d6dfe1fSPatrick McHardy 		goto out;
4751da177e4SLinus Torvalds 	}
4761da177e4SLinus Torvalds 
47726b15dadSJamal Hadi Salim 	c.seq = nlh->nlmsg_seq;
47826b15dadSJamal Hadi Salim 	c.pid = nlh->nlmsg_pid;
479f60f6b8fSHerbert Xu 	c.event = nlh->nlmsg_type;
48026b15dadSJamal Hadi Salim 
48126b15dadSJamal Hadi Salim 	km_state_notify(x, &c);
4827d6dfe1fSPatrick McHardy out:
48326b15dadSJamal Hadi Salim 	xfrm_state_put(x);
4841da177e4SLinus Torvalds 	return err;
4851da177e4SLinus Torvalds }
4861da177e4SLinus Torvalds 
487eb2971b6SMasahide NAKAMURA static struct xfrm_state *xfrm_user_state_lookup(struct xfrm_usersa_id *p,
488eb2971b6SMasahide NAKAMURA 						 struct rtattr **xfrma,
489eb2971b6SMasahide NAKAMURA 						 int *errp)
490eb2971b6SMasahide NAKAMURA {
491eb2971b6SMasahide NAKAMURA 	struct xfrm_state *x = NULL;
492eb2971b6SMasahide NAKAMURA 	int err;
493eb2971b6SMasahide NAKAMURA 
494eb2971b6SMasahide NAKAMURA 	if (xfrm_id_proto_match(p->proto, IPSEC_PROTO_ANY)) {
495eb2971b6SMasahide NAKAMURA 		err = -ESRCH;
496eb2971b6SMasahide NAKAMURA 		x = xfrm_state_lookup(&p->daddr, p->spi, p->proto, p->family);
497eb2971b6SMasahide NAKAMURA 	} else {
498eb2971b6SMasahide NAKAMURA 		xfrm_address_t *saddr = NULL;
499eb2971b6SMasahide NAKAMURA 
500eb2971b6SMasahide NAKAMURA 		err = verify_one_addr(xfrma, XFRMA_SRCADDR, &saddr);
501eb2971b6SMasahide NAKAMURA 		if (err)
502eb2971b6SMasahide NAKAMURA 			goto out;
503eb2971b6SMasahide NAKAMURA 
504eb2971b6SMasahide NAKAMURA 		if (!saddr) {
505eb2971b6SMasahide NAKAMURA 			err = -EINVAL;
506eb2971b6SMasahide NAKAMURA 			goto out;
507eb2971b6SMasahide NAKAMURA 		}
508eb2971b6SMasahide NAKAMURA 
5099abbffeeSMasahide NAKAMURA 		err = -ESRCH;
510eb2971b6SMasahide NAKAMURA 		x = xfrm_state_lookup_byaddr(&p->daddr, saddr, p->proto,
511eb2971b6SMasahide NAKAMURA 					     p->family);
512eb2971b6SMasahide NAKAMURA 	}
513eb2971b6SMasahide NAKAMURA 
514eb2971b6SMasahide NAKAMURA  out:
515eb2971b6SMasahide NAKAMURA 	if (!x && errp)
516eb2971b6SMasahide NAKAMURA 		*errp = err;
517eb2971b6SMasahide NAKAMURA 	return x;
518eb2971b6SMasahide NAKAMURA }
519eb2971b6SMasahide NAKAMURA 
52022e70050SChristoph Hellwig static int xfrm_del_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
52122e70050SChristoph Hellwig 		struct rtattr **xfrma)
5221da177e4SLinus Torvalds {
5231da177e4SLinus Torvalds 	struct xfrm_state *x;
524eb2971b6SMasahide NAKAMURA 	int err = -ESRCH;
52526b15dadSJamal Hadi Salim 	struct km_event c;
5267b67c857SThomas Graf 	struct xfrm_usersa_id *p = nlmsg_data(nlh);
5271da177e4SLinus Torvalds 
52822e70050SChristoph Hellwig 	x = xfrm_user_state_lookup(p, xfrma, &err);
5291da177e4SLinus Torvalds 	if (x == NULL)
530eb2971b6SMasahide NAKAMURA 		return err;
5311da177e4SLinus Torvalds 
5326f68dc37SDavid S. Miller 	if ((err = security_xfrm_state_delete(x)) != 0)
533c8c05a8eSCatherine Zhang 		goto out;
534c8c05a8eSCatherine Zhang 
5351da177e4SLinus Torvalds 	if (xfrm_state_kern(x)) {
536c8c05a8eSCatherine Zhang 		err = -EPERM;
537c8c05a8eSCatherine Zhang 		goto out;
5381da177e4SLinus Torvalds 	}
5391da177e4SLinus Torvalds 
54026b15dadSJamal Hadi Salim 	err = xfrm_state_delete(x);
541161a09e7SJoy Latten 
542c8c05a8eSCatherine Zhang 	if (err < 0)
543c8c05a8eSCatherine Zhang 		goto out;
54426b15dadSJamal Hadi Salim 
54526b15dadSJamal Hadi Salim 	c.seq = nlh->nlmsg_seq;
54626b15dadSJamal Hadi Salim 	c.pid = nlh->nlmsg_pid;
547f60f6b8fSHerbert Xu 	c.event = nlh->nlmsg_type;
54826b15dadSJamal Hadi Salim 	km_state_notify(x, &c);
5491da177e4SLinus Torvalds 
550c8c05a8eSCatherine Zhang out:
55116bec31dSEric Paris 	xfrm_audit_log(NETLINK_CB(skb).loginuid, NETLINK_CB(skb).sid,
55216bec31dSEric Paris 		       AUDIT_MAC_IPSEC_DELSA, err ? 0 : 1, NULL, x);
553c8c05a8eSCatherine Zhang 	xfrm_state_put(x);
55426b15dadSJamal Hadi Salim 	return err;
5551da177e4SLinus Torvalds }
5561da177e4SLinus Torvalds 
5571da177e4SLinus Torvalds static void copy_to_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p)
5581da177e4SLinus Torvalds {
5591da177e4SLinus Torvalds 	memcpy(&p->id, &x->id, sizeof(p->id));
5601da177e4SLinus Torvalds 	memcpy(&p->sel, &x->sel, sizeof(p->sel));
5611da177e4SLinus Torvalds 	memcpy(&p->lft, &x->lft, sizeof(p->lft));
5621da177e4SLinus Torvalds 	memcpy(&p->curlft, &x->curlft, sizeof(p->curlft));
5631da177e4SLinus Torvalds 	memcpy(&p->stats, &x->stats, sizeof(p->stats));
56454489c14SDavid S. Miller 	memcpy(&p->saddr, &x->props.saddr, sizeof(p->saddr));
5651da177e4SLinus Torvalds 	p->mode = x->props.mode;
5661da177e4SLinus Torvalds 	p->replay_window = x->props.replay_window;
5671da177e4SLinus Torvalds 	p->reqid = x->props.reqid;
5681da177e4SLinus Torvalds 	p->family = x->props.family;
5691da177e4SLinus Torvalds 	p->flags = x->props.flags;
5701da177e4SLinus Torvalds 	p->seq = x->km.seq;
5711da177e4SLinus Torvalds }
5721da177e4SLinus Torvalds 
5731da177e4SLinus Torvalds struct xfrm_dump_info {
5741da177e4SLinus Torvalds 	struct sk_buff *in_skb;
5751da177e4SLinus Torvalds 	struct sk_buff *out_skb;
5761da177e4SLinus Torvalds 	u32 nlmsg_seq;
5771da177e4SLinus Torvalds 	u16 nlmsg_flags;
5781da177e4SLinus Torvalds 	int start_idx;
5791da177e4SLinus Torvalds 	int this_idx;
5801da177e4SLinus Torvalds };
5811da177e4SLinus Torvalds 
582c0144beaSThomas Graf static int copy_sec_ctx(struct xfrm_sec_ctx *s, struct sk_buff *skb)
583c0144beaSThomas Graf {
584c0144beaSThomas Graf 	int ctx_size = sizeof(struct xfrm_sec_ctx) + s->ctx_len;
585c0144beaSThomas Graf 	struct xfrm_user_sec_ctx *uctx;
586c0144beaSThomas Graf 	struct nlattr *attr;
587c0144beaSThomas Graf 
588c0144beaSThomas Graf 	attr = nla_reserve(skb, XFRMA_SEC_CTX, ctx_size);
589c0144beaSThomas Graf 	if (attr == NULL)
590c0144beaSThomas Graf 		return -EMSGSIZE;
591c0144beaSThomas Graf 
592c0144beaSThomas Graf 	uctx = nla_data(attr);
593c0144beaSThomas Graf 	uctx->exttype = XFRMA_SEC_CTX;
594c0144beaSThomas Graf 	uctx->len = ctx_size;
595c0144beaSThomas Graf 	uctx->ctx_doi = s->ctx_doi;
596c0144beaSThomas Graf 	uctx->ctx_alg = s->ctx_alg;
597c0144beaSThomas Graf 	uctx->ctx_len = s->ctx_len;
598c0144beaSThomas Graf 	memcpy(uctx + 1, s->ctx_str, s->ctx_len);
599c0144beaSThomas Graf 
600c0144beaSThomas Graf 	return 0;
601c0144beaSThomas Graf }
602c0144beaSThomas Graf 
6031da177e4SLinus Torvalds static int dump_one_state(struct xfrm_state *x, int count, void *ptr)
6041da177e4SLinus Torvalds {
6051da177e4SLinus Torvalds 	struct xfrm_dump_info *sp = ptr;
6061da177e4SLinus Torvalds 	struct sk_buff *in_skb = sp->in_skb;
6071da177e4SLinus Torvalds 	struct sk_buff *skb = sp->out_skb;
6081da177e4SLinus Torvalds 	struct xfrm_usersa_info *p;
6091da177e4SLinus Torvalds 	struct nlmsghdr *nlh;
6101da177e4SLinus Torvalds 
6111da177e4SLinus Torvalds 	if (sp->this_idx < sp->start_idx)
6121da177e4SLinus Torvalds 		goto out;
6131da177e4SLinus Torvalds 
61479b8b7f4SThomas Graf 	nlh = nlmsg_put(skb, NETLINK_CB(in_skb).pid, sp->nlmsg_seq,
61579b8b7f4SThomas Graf 			XFRM_MSG_NEWSA, sizeof(*p), sp->nlmsg_flags);
61679b8b7f4SThomas Graf 	if (nlh == NULL)
61779b8b7f4SThomas Graf 		return -EMSGSIZE;
6181da177e4SLinus Torvalds 
6197b67c857SThomas Graf 	p = nlmsg_data(nlh);
6201da177e4SLinus Torvalds 	copy_to_user_state(x, p);
6211da177e4SLinus Torvalds 
6221da177e4SLinus Torvalds 	if (x->aalg)
623c26445acSThomas Graf 		NLA_PUT(skb, XFRMA_ALG_AUTH, alg_len(x->aalg), x->aalg);
6241da177e4SLinus Torvalds 	if (x->ealg)
625c26445acSThomas Graf 		NLA_PUT(skb, XFRMA_ALG_CRYPT, alg_len(x->ealg), x->ealg);
6261da177e4SLinus Torvalds 	if (x->calg)
627c0144beaSThomas Graf 		NLA_PUT(skb, XFRMA_ALG_COMP, sizeof(*(x->calg)), x->calg);
6281da177e4SLinus Torvalds 
6291da177e4SLinus Torvalds 	if (x->encap)
630c0144beaSThomas Graf 		NLA_PUT(skb, XFRMA_ENCAP, sizeof(*x->encap), x->encap);
6311da177e4SLinus Torvalds 
632c0144beaSThomas Graf 	if (x->security && copy_sec_ctx(x->security, skb) < 0)
633c0144beaSThomas Graf 		goto nla_put_failure;
634060f02a3SNoriaki TAKAMIYA 
635060f02a3SNoriaki TAKAMIYA 	if (x->coaddr)
636c0144beaSThomas Graf 		NLA_PUT(skb, XFRMA_COADDR, sizeof(*x->coaddr), x->coaddr);
637060f02a3SNoriaki TAKAMIYA 
6389afaca05SMasahide NAKAMURA 	if (x->lastused)
639c0144beaSThomas Graf 		NLA_PUT_U64(skb, XFRMA_LASTUSED, x->lastused);
6409afaca05SMasahide NAKAMURA 
6419825069dSThomas Graf 	nlmsg_end(skb, nlh);
6421da177e4SLinus Torvalds out:
6431da177e4SLinus Torvalds 	sp->this_idx++;
6441da177e4SLinus Torvalds 	return 0;
6451da177e4SLinus Torvalds 
646c0144beaSThomas Graf nla_put_failure:
6479825069dSThomas Graf 	nlmsg_cancel(skb, nlh);
6489825069dSThomas Graf 	return -EMSGSIZE;
6491da177e4SLinus Torvalds }
6501da177e4SLinus Torvalds 
6511da177e4SLinus Torvalds static int xfrm_dump_sa(struct sk_buff *skb, struct netlink_callback *cb)
6521da177e4SLinus Torvalds {
6531da177e4SLinus Torvalds 	struct xfrm_dump_info info;
6541da177e4SLinus Torvalds 
6551da177e4SLinus Torvalds 	info.in_skb = cb->skb;
6561da177e4SLinus Torvalds 	info.out_skb = skb;
6571da177e4SLinus Torvalds 	info.nlmsg_seq = cb->nlh->nlmsg_seq;
6581da177e4SLinus Torvalds 	info.nlmsg_flags = NLM_F_MULTI;
6591da177e4SLinus Torvalds 	info.this_idx = 0;
6601da177e4SLinus Torvalds 	info.start_idx = cb->args[0];
661dc00a525SMasahide NAKAMURA 	(void) xfrm_state_walk(0, dump_one_state, &info);
6621da177e4SLinus Torvalds 	cb->args[0] = info.this_idx;
6631da177e4SLinus Torvalds 
6641da177e4SLinus Torvalds 	return skb->len;
6651da177e4SLinus Torvalds }
6661da177e4SLinus Torvalds 
6671da177e4SLinus Torvalds static struct sk_buff *xfrm_state_netlink(struct sk_buff *in_skb,
6681da177e4SLinus Torvalds 					  struct xfrm_state *x, u32 seq)
6691da177e4SLinus Torvalds {
6701da177e4SLinus Torvalds 	struct xfrm_dump_info info;
6711da177e4SLinus Torvalds 	struct sk_buff *skb;
6721da177e4SLinus Torvalds 
6731da177e4SLinus Torvalds 	skb = alloc_skb(NLMSG_GOODSIZE, GFP_ATOMIC);
6741da177e4SLinus Torvalds 	if (!skb)
6751da177e4SLinus Torvalds 		return ERR_PTR(-ENOMEM);
6761da177e4SLinus Torvalds 
6771da177e4SLinus Torvalds 	info.in_skb = in_skb;
6781da177e4SLinus Torvalds 	info.out_skb = skb;
6791da177e4SLinus Torvalds 	info.nlmsg_seq = seq;
6801da177e4SLinus Torvalds 	info.nlmsg_flags = 0;
6811da177e4SLinus Torvalds 	info.this_idx = info.start_idx = 0;
6821da177e4SLinus Torvalds 
6831da177e4SLinus Torvalds 	if (dump_one_state(x, 0, &info)) {
6841da177e4SLinus Torvalds 		kfree_skb(skb);
6851da177e4SLinus Torvalds 		return NULL;
6861da177e4SLinus Torvalds 	}
6871da177e4SLinus Torvalds 
6881da177e4SLinus Torvalds 	return skb;
6891da177e4SLinus Torvalds }
6901da177e4SLinus Torvalds 
691ecfd6b18SJamal Hadi Salim static int build_spdinfo(struct sk_buff *skb, u32 pid, u32 seq, u32 flags)
692ecfd6b18SJamal Hadi Salim {
6935a6d3416SJamal Hadi Salim 	struct xfrmk_spdinfo si;
6945a6d3416SJamal Hadi Salim 	struct xfrmu_spdinfo spc;
6955a6d3416SJamal Hadi Salim 	struct xfrmu_spdhinfo sph;
696ecfd6b18SJamal Hadi Salim 	struct nlmsghdr *nlh;
697ecfd6b18SJamal Hadi Salim 	u32 *f;
698ecfd6b18SJamal Hadi Salim 
699ecfd6b18SJamal Hadi Salim 	nlh = nlmsg_put(skb, pid, seq, XFRM_MSG_NEWSPDINFO, sizeof(u32), 0);
700ecfd6b18SJamal Hadi Salim 	if (nlh == NULL) /* shouldnt really happen ... */
701ecfd6b18SJamal Hadi Salim 		return -EMSGSIZE;
702ecfd6b18SJamal Hadi Salim 
703ecfd6b18SJamal Hadi Salim 	f = nlmsg_data(nlh);
704ecfd6b18SJamal Hadi Salim 	*f = flags;
705ecfd6b18SJamal Hadi Salim 	xfrm_spd_getinfo(&si);
7065a6d3416SJamal Hadi Salim 	spc.incnt = si.incnt;
7075a6d3416SJamal Hadi Salim 	spc.outcnt = si.outcnt;
7085a6d3416SJamal Hadi Salim 	spc.fwdcnt = si.fwdcnt;
7095a6d3416SJamal Hadi Salim 	spc.inscnt = si.inscnt;
7105a6d3416SJamal Hadi Salim 	spc.outscnt = si.outscnt;
7115a6d3416SJamal Hadi Salim 	spc.fwdscnt = si.fwdscnt;
7125a6d3416SJamal Hadi Salim 	sph.spdhcnt = si.spdhcnt;
7135a6d3416SJamal Hadi Salim 	sph.spdhmcnt = si.spdhmcnt;
714ecfd6b18SJamal Hadi Salim 
7155a6d3416SJamal Hadi Salim 	NLA_PUT(skb, XFRMA_SPD_INFO, sizeof(spc), &spc);
7165a6d3416SJamal Hadi Salim 	NLA_PUT(skb, XFRMA_SPD_HINFO, sizeof(sph), &sph);
717ecfd6b18SJamal Hadi Salim 
718ecfd6b18SJamal Hadi Salim 	return nlmsg_end(skb, nlh);
719ecfd6b18SJamal Hadi Salim 
720ecfd6b18SJamal Hadi Salim nla_put_failure:
721ecfd6b18SJamal Hadi Salim 	nlmsg_cancel(skb, nlh);
722ecfd6b18SJamal Hadi Salim 	return -EMSGSIZE;
723ecfd6b18SJamal Hadi Salim }
724ecfd6b18SJamal Hadi Salim 
725ecfd6b18SJamal Hadi Salim static int xfrm_get_spdinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
726ecfd6b18SJamal Hadi Salim 		struct rtattr **xfrma)
727ecfd6b18SJamal Hadi Salim {
728ecfd6b18SJamal Hadi Salim 	struct sk_buff *r_skb;
7297b67c857SThomas Graf 	u32 *flags = nlmsg_data(nlh);
730ecfd6b18SJamal Hadi Salim 	u32 spid = NETLINK_CB(skb).pid;
731ecfd6b18SJamal Hadi Salim 	u32 seq = nlh->nlmsg_seq;
732ecfd6b18SJamal Hadi Salim 	int len = NLMSG_LENGTH(sizeof(u32));
733ecfd6b18SJamal Hadi Salim 
7345a6d3416SJamal Hadi Salim 	len += RTA_SPACE(sizeof(struct xfrmu_spdinfo));
7355a6d3416SJamal Hadi Salim 	len += RTA_SPACE(sizeof(struct xfrmu_spdhinfo));
736ecfd6b18SJamal Hadi Salim 
737ecfd6b18SJamal Hadi Salim 	r_skb = alloc_skb(len, GFP_ATOMIC);
738ecfd6b18SJamal Hadi Salim 	if (r_skb == NULL)
739ecfd6b18SJamal Hadi Salim 		return -ENOMEM;
740ecfd6b18SJamal Hadi Salim 
741ecfd6b18SJamal Hadi Salim 	if (build_spdinfo(r_skb, spid, seq, *flags) < 0)
742ecfd6b18SJamal Hadi Salim 		BUG();
743ecfd6b18SJamal Hadi Salim 
744ecfd6b18SJamal Hadi Salim 	return nlmsg_unicast(xfrm_nl, r_skb, spid);
745ecfd6b18SJamal Hadi Salim }
746ecfd6b18SJamal Hadi Salim 
74728d8909bSJamal Hadi Salim static int build_sadinfo(struct sk_buff *skb, u32 pid, u32 seq, u32 flags)
74828d8909bSJamal Hadi Salim {
749af11e316SJamal Hadi Salim 	struct xfrmk_sadinfo si;
750af11e316SJamal Hadi Salim 	struct xfrmu_sadhinfo sh;
75128d8909bSJamal Hadi Salim 	struct nlmsghdr *nlh;
75228d8909bSJamal Hadi Salim 	u32 *f;
75328d8909bSJamal Hadi Salim 
75428d8909bSJamal Hadi Salim 	nlh = nlmsg_put(skb, pid, seq, XFRM_MSG_NEWSADINFO, sizeof(u32), 0);
75528d8909bSJamal Hadi Salim 	if (nlh == NULL) /* shouldnt really happen ... */
75628d8909bSJamal Hadi Salim 		return -EMSGSIZE;
75728d8909bSJamal Hadi Salim 
75828d8909bSJamal Hadi Salim 	f = nlmsg_data(nlh);
75928d8909bSJamal Hadi Salim 	*f = flags;
76028d8909bSJamal Hadi Salim 	xfrm_sad_getinfo(&si);
76128d8909bSJamal Hadi Salim 
762af11e316SJamal Hadi Salim 	sh.sadhmcnt = si.sadhmcnt;
763af11e316SJamal Hadi Salim 	sh.sadhcnt = si.sadhcnt;
764af11e316SJamal Hadi Salim 
765af11e316SJamal Hadi Salim 	NLA_PUT_U32(skb, XFRMA_SAD_CNT, si.sadcnt);
766af11e316SJamal Hadi Salim 	NLA_PUT(skb, XFRMA_SAD_HINFO, sizeof(sh), &sh);
76728d8909bSJamal Hadi Salim 
76828d8909bSJamal Hadi Salim 	return nlmsg_end(skb, nlh);
76928d8909bSJamal Hadi Salim 
77028d8909bSJamal Hadi Salim nla_put_failure:
77128d8909bSJamal Hadi Salim 	nlmsg_cancel(skb, nlh);
77228d8909bSJamal Hadi Salim 	return -EMSGSIZE;
77328d8909bSJamal Hadi Salim }
77428d8909bSJamal Hadi Salim 
77528d8909bSJamal Hadi Salim static int xfrm_get_sadinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
77628d8909bSJamal Hadi Salim 		struct rtattr **xfrma)
77728d8909bSJamal Hadi Salim {
77828d8909bSJamal Hadi Salim 	struct sk_buff *r_skb;
7797b67c857SThomas Graf 	u32 *flags = nlmsg_data(nlh);
78028d8909bSJamal Hadi Salim 	u32 spid = NETLINK_CB(skb).pid;
78128d8909bSJamal Hadi Salim 	u32 seq = nlh->nlmsg_seq;
78228d8909bSJamal Hadi Salim 	int len = NLMSG_LENGTH(sizeof(u32));
78328d8909bSJamal Hadi Salim 
784af11e316SJamal Hadi Salim 	len += RTA_SPACE(sizeof(struct xfrmu_sadhinfo));
78528d8909bSJamal Hadi Salim 	len += RTA_SPACE(sizeof(u32));
78628d8909bSJamal Hadi Salim 
78728d8909bSJamal Hadi Salim 	r_skb = alloc_skb(len, GFP_ATOMIC);
78828d8909bSJamal Hadi Salim 
78928d8909bSJamal Hadi Salim 	if (r_skb == NULL)
79028d8909bSJamal Hadi Salim 		return -ENOMEM;
79128d8909bSJamal Hadi Salim 
79228d8909bSJamal Hadi Salim 	if (build_sadinfo(r_skb, spid, seq, *flags) < 0)
79328d8909bSJamal Hadi Salim 		BUG();
79428d8909bSJamal Hadi Salim 
79528d8909bSJamal Hadi Salim 	return nlmsg_unicast(xfrm_nl, r_skb, spid);
79628d8909bSJamal Hadi Salim }
79728d8909bSJamal Hadi Salim 
79822e70050SChristoph Hellwig static int xfrm_get_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
79922e70050SChristoph Hellwig 		struct rtattr **xfrma)
8001da177e4SLinus Torvalds {
8017b67c857SThomas Graf 	struct xfrm_usersa_id *p = nlmsg_data(nlh);
8021da177e4SLinus Torvalds 	struct xfrm_state *x;
8031da177e4SLinus Torvalds 	struct sk_buff *resp_skb;
804eb2971b6SMasahide NAKAMURA 	int err = -ESRCH;
8051da177e4SLinus Torvalds 
80622e70050SChristoph Hellwig 	x = xfrm_user_state_lookup(p, xfrma, &err);
8071da177e4SLinus Torvalds 	if (x == NULL)
8081da177e4SLinus Torvalds 		goto out_noput;
8091da177e4SLinus Torvalds 
8101da177e4SLinus Torvalds 	resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
8111da177e4SLinus Torvalds 	if (IS_ERR(resp_skb)) {
8121da177e4SLinus Torvalds 		err = PTR_ERR(resp_skb);
8131da177e4SLinus Torvalds 	} else {
814082a1ad5SThomas Graf 		err = nlmsg_unicast(xfrm_nl, resp_skb, NETLINK_CB(skb).pid);
8151da177e4SLinus Torvalds 	}
8161da177e4SLinus Torvalds 	xfrm_state_put(x);
8171da177e4SLinus Torvalds out_noput:
8181da177e4SLinus Torvalds 	return err;
8191da177e4SLinus Torvalds }
8201da177e4SLinus Torvalds 
8211da177e4SLinus Torvalds static int verify_userspi_info(struct xfrm_userspi_info *p)
8221da177e4SLinus Torvalds {
8231da177e4SLinus Torvalds 	switch (p->info.id.proto) {
8241da177e4SLinus Torvalds 	case IPPROTO_AH:
8251da177e4SLinus Torvalds 	case IPPROTO_ESP:
8261da177e4SLinus Torvalds 		break;
8271da177e4SLinus Torvalds 
8281da177e4SLinus Torvalds 	case IPPROTO_COMP:
8291da177e4SLinus Torvalds 		/* IPCOMP spi is 16-bits. */
8301da177e4SLinus Torvalds 		if (p->max >= 0x10000)
8311da177e4SLinus Torvalds 			return -EINVAL;
8321da177e4SLinus Torvalds 		break;
8331da177e4SLinus Torvalds 
8341da177e4SLinus Torvalds 	default:
8351da177e4SLinus Torvalds 		return -EINVAL;
8363ff50b79SStephen Hemminger 	}
8371da177e4SLinus Torvalds 
8381da177e4SLinus Torvalds 	if (p->min > p->max)
8391da177e4SLinus Torvalds 		return -EINVAL;
8401da177e4SLinus Torvalds 
8411da177e4SLinus Torvalds 	return 0;
8421da177e4SLinus Torvalds }
8431da177e4SLinus Torvalds 
84422e70050SChristoph Hellwig static int xfrm_alloc_userspi(struct sk_buff *skb, struct nlmsghdr *nlh,
84522e70050SChristoph Hellwig 		struct rtattr **xfrma)
8461da177e4SLinus Torvalds {
8471da177e4SLinus Torvalds 	struct xfrm_state *x;
8481da177e4SLinus Torvalds 	struct xfrm_userspi_info *p;
8491da177e4SLinus Torvalds 	struct sk_buff *resp_skb;
8501da177e4SLinus Torvalds 	xfrm_address_t *daddr;
8511da177e4SLinus Torvalds 	int family;
8521da177e4SLinus Torvalds 	int err;
8531da177e4SLinus Torvalds 
8547b67c857SThomas Graf 	p = nlmsg_data(nlh);
8551da177e4SLinus Torvalds 	err = verify_userspi_info(p);
8561da177e4SLinus Torvalds 	if (err)
8571da177e4SLinus Torvalds 		goto out_noput;
8581da177e4SLinus Torvalds 
8591da177e4SLinus Torvalds 	family = p->info.family;
8601da177e4SLinus Torvalds 	daddr = &p->info.id.daddr;
8611da177e4SLinus Torvalds 
8621da177e4SLinus Torvalds 	x = NULL;
8631da177e4SLinus Torvalds 	if (p->info.seq) {
8641da177e4SLinus Torvalds 		x = xfrm_find_acq_byseq(p->info.seq);
8651da177e4SLinus Torvalds 		if (x && xfrm_addr_cmp(&x->id.daddr, daddr, family)) {
8661da177e4SLinus Torvalds 			xfrm_state_put(x);
8671da177e4SLinus Torvalds 			x = NULL;
8681da177e4SLinus Torvalds 		}
8691da177e4SLinus Torvalds 	}
8701da177e4SLinus Torvalds 
8711da177e4SLinus Torvalds 	if (!x)
8721da177e4SLinus Torvalds 		x = xfrm_find_acq(p->info.mode, p->info.reqid,
8731da177e4SLinus Torvalds 				  p->info.id.proto, daddr,
8741da177e4SLinus Torvalds 				  &p->info.saddr, 1,
8751da177e4SLinus Torvalds 				  family);
8761da177e4SLinus Torvalds 	err = -ENOENT;
8771da177e4SLinus Torvalds 	if (x == NULL)
8781da177e4SLinus Torvalds 		goto out_noput;
8791da177e4SLinus Torvalds 
8801da177e4SLinus Torvalds 	resp_skb = ERR_PTR(-ENOENT);
8811da177e4SLinus Torvalds 
8821da177e4SLinus Torvalds 	spin_lock_bh(&x->lock);
8831da177e4SLinus Torvalds 	if (x->km.state != XFRM_STATE_DEAD) {
8841da177e4SLinus Torvalds 		xfrm_alloc_spi(x, htonl(p->min), htonl(p->max));
8851da177e4SLinus Torvalds 		if (x->id.spi)
8861da177e4SLinus Torvalds 			resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
8871da177e4SLinus Torvalds 	}
8881da177e4SLinus Torvalds 	spin_unlock_bh(&x->lock);
8891da177e4SLinus Torvalds 
8901da177e4SLinus Torvalds 	if (IS_ERR(resp_skb)) {
8911da177e4SLinus Torvalds 		err = PTR_ERR(resp_skb);
8921da177e4SLinus Torvalds 		goto out;
8931da177e4SLinus Torvalds 	}
8941da177e4SLinus Torvalds 
895082a1ad5SThomas Graf 	err = nlmsg_unicast(xfrm_nl, resp_skb, NETLINK_CB(skb).pid);
8961da177e4SLinus Torvalds 
8971da177e4SLinus Torvalds out:
8981da177e4SLinus Torvalds 	xfrm_state_put(x);
8991da177e4SLinus Torvalds out_noput:
9001da177e4SLinus Torvalds 	return err;
9011da177e4SLinus Torvalds }
9021da177e4SLinus Torvalds 
903b798a9edSJamal Hadi Salim static int verify_policy_dir(u8 dir)
9041da177e4SLinus Torvalds {
9051da177e4SLinus Torvalds 	switch (dir) {
9061da177e4SLinus Torvalds 	case XFRM_POLICY_IN:
9071da177e4SLinus Torvalds 	case XFRM_POLICY_OUT:
9081da177e4SLinus Torvalds 	case XFRM_POLICY_FWD:
9091da177e4SLinus Torvalds 		break;
9101da177e4SLinus Torvalds 
9111da177e4SLinus Torvalds 	default:
9121da177e4SLinus Torvalds 		return -EINVAL;
9133ff50b79SStephen Hemminger 	}
9141da177e4SLinus Torvalds 
9151da177e4SLinus Torvalds 	return 0;
9161da177e4SLinus Torvalds }
9171da177e4SLinus Torvalds 
918b798a9edSJamal Hadi Salim static int verify_policy_type(u8 type)
919f7b6983fSMasahide NAKAMURA {
920f7b6983fSMasahide NAKAMURA 	switch (type) {
921f7b6983fSMasahide NAKAMURA 	case XFRM_POLICY_TYPE_MAIN:
922f7b6983fSMasahide NAKAMURA #ifdef CONFIG_XFRM_SUB_POLICY
923f7b6983fSMasahide NAKAMURA 	case XFRM_POLICY_TYPE_SUB:
924f7b6983fSMasahide NAKAMURA #endif
925f7b6983fSMasahide NAKAMURA 		break;
926f7b6983fSMasahide NAKAMURA 
927f7b6983fSMasahide NAKAMURA 	default:
928f7b6983fSMasahide NAKAMURA 		return -EINVAL;
9293ff50b79SStephen Hemminger 	}
930f7b6983fSMasahide NAKAMURA 
931f7b6983fSMasahide NAKAMURA 	return 0;
932f7b6983fSMasahide NAKAMURA }
933f7b6983fSMasahide NAKAMURA 
9341da177e4SLinus Torvalds static int verify_newpolicy_info(struct xfrm_userpolicy_info *p)
9351da177e4SLinus Torvalds {
9361da177e4SLinus Torvalds 	switch (p->share) {
9371da177e4SLinus Torvalds 	case XFRM_SHARE_ANY:
9381da177e4SLinus Torvalds 	case XFRM_SHARE_SESSION:
9391da177e4SLinus Torvalds 	case XFRM_SHARE_USER:
9401da177e4SLinus Torvalds 	case XFRM_SHARE_UNIQUE:
9411da177e4SLinus Torvalds 		break;
9421da177e4SLinus Torvalds 
9431da177e4SLinus Torvalds 	default:
9441da177e4SLinus Torvalds 		return -EINVAL;
9453ff50b79SStephen Hemminger 	}
9461da177e4SLinus Torvalds 
9471da177e4SLinus Torvalds 	switch (p->action) {
9481da177e4SLinus Torvalds 	case XFRM_POLICY_ALLOW:
9491da177e4SLinus Torvalds 	case XFRM_POLICY_BLOCK:
9501da177e4SLinus Torvalds 		break;
9511da177e4SLinus Torvalds 
9521da177e4SLinus Torvalds 	default:
9531da177e4SLinus Torvalds 		return -EINVAL;
9543ff50b79SStephen Hemminger 	}
9551da177e4SLinus Torvalds 
9561da177e4SLinus Torvalds 	switch (p->sel.family) {
9571da177e4SLinus Torvalds 	case AF_INET:
9581da177e4SLinus Torvalds 		break;
9591da177e4SLinus Torvalds 
9601da177e4SLinus Torvalds 	case AF_INET6:
9611da177e4SLinus Torvalds #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
9621da177e4SLinus Torvalds 		break;
9631da177e4SLinus Torvalds #else
9641da177e4SLinus Torvalds 		return  -EAFNOSUPPORT;
9651da177e4SLinus Torvalds #endif
9661da177e4SLinus Torvalds 
9671da177e4SLinus Torvalds 	default:
9681da177e4SLinus Torvalds 		return -EINVAL;
9693ff50b79SStephen Hemminger 	}
9701da177e4SLinus Torvalds 
9711da177e4SLinus Torvalds 	return verify_policy_dir(p->dir);
9721da177e4SLinus Torvalds }
9731da177e4SLinus Torvalds 
974df71837dSTrent Jaeger static int copy_from_user_sec_ctx(struct xfrm_policy *pol, struct rtattr **xfrma)
975df71837dSTrent Jaeger {
976df71837dSTrent Jaeger 	struct rtattr *rt = xfrma[XFRMA_SEC_CTX-1];
977df71837dSTrent Jaeger 	struct xfrm_user_sec_ctx *uctx;
978df71837dSTrent Jaeger 
979df71837dSTrent Jaeger 	if (!rt)
980df71837dSTrent Jaeger 		return 0;
981df71837dSTrent Jaeger 
982df71837dSTrent Jaeger 	uctx = RTA_DATA(rt);
983df71837dSTrent Jaeger 	return security_xfrm_policy_alloc(pol, uctx);
984df71837dSTrent Jaeger }
985df71837dSTrent Jaeger 
9861da177e4SLinus Torvalds static void copy_templates(struct xfrm_policy *xp, struct xfrm_user_tmpl *ut,
9871da177e4SLinus Torvalds 			   int nr)
9881da177e4SLinus Torvalds {
9891da177e4SLinus Torvalds 	int i;
9901da177e4SLinus Torvalds 
9911da177e4SLinus Torvalds 	xp->xfrm_nr = nr;
9921da177e4SLinus Torvalds 	for (i = 0; i < nr; i++, ut++) {
9931da177e4SLinus Torvalds 		struct xfrm_tmpl *t = &xp->xfrm_vec[i];
9941da177e4SLinus Torvalds 
9951da177e4SLinus Torvalds 		memcpy(&t->id, &ut->id, sizeof(struct xfrm_id));
9961da177e4SLinus Torvalds 		memcpy(&t->saddr, &ut->saddr,
9971da177e4SLinus Torvalds 		       sizeof(xfrm_address_t));
9981da177e4SLinus Torvalds 		t->reqid = ut->reqid;
9991da177e4SLinus Torvalds 		t->mode = ut->mode;
10001da177e4SLinus Torvalds 		t->share = ut->share;
10011da177e4SLinus Torvalds 		t->optional = ut->optional;
10021da177e4SLinus Torvalds 		t->aalgos = ut->aalgos;
10031da177e4SLinus Torvalds 		t->ealgos = ut->ealgos;
10041da177e4SLinus Torvalds 		t->calgos = ut->calgos;
10058511d01dSMiika Komu 		t->encap_family = ut->family;
10061da177e4SLinus Torvalds 	}
10071da177e4SLinus Torvalds }
10081da177e4SLinus Torvalds 
1009b4ad86bfSDavid S. Miller static int validate_tmpl(int nr, struct xfrm_user_tmpl *ut, u16 family)
1010b4ad86bfSDavid S. Miller {
1011b4ad86bfSDavid S. Miller 	int i;
1012b4ad86bfSDavid S. Miller 
1013b4ad86bfSDavid S. Miller 	if (nr > XFRM_MAX_DEPTH)
1014b4ad86bfSDavid S. Miller 		return -EINVAL;
1015b4ad86bfSDavid S. Miller 
1016b4ad86bfSDavid S. Miller 	for (i = 0; i < nr; i++) {
1017b4ad86bfSDavid S. Miller 		/* We never validated the ut->family value, so many
1018b4ad86bfSDavid S. Miller 		 * applications simply leave it at zero.  The check was
1019b4ad86bfSDavid S. Miller 		 * never made and ut->family was ignored because all
1020b4ad86bfSDavid S. Miller 		 * templates could be assumed to have the same family as
1021b4ad86bfSDavid S. Miller 		 * the policy itself.  Now that we will have ipv4-in-ipv6
1022b4ad86bfSDavid S. Miller 		 * and ipv6-in-ipv4 tunnels, this is no longer true.
1023b4ad86bfSDavid S. Miller 		 */
1024b4ad86bfSDavid S. Miller 		if (!ut[i].family)
1025b4ad86bfSDavid S. Miller 			ut[i].family = family;
1026b4ad86bfSDavid S. Miller 
1027b4ad86bfSDavid S. Miller 		switch (ut[i].family) {
1028b4ad86bfSDavid S. Miller 		case AF_INET:
1029b4ad86bfSDavid S. Miller 			break;
1030b4ad86bfSDavid S. Miller #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1031b4ad86bfSDavid S. Miller 		case AF_INET6:
1032b4ad86bfSDavid S. Miller 			break;
1033b4ad86bfSDavid S. Miller #endif
1034b4ad86bfSDavid S. Miller 		default:
1035b4ad86bfSDavid S. Miller 			return -EINVAL;
10363ff50b79SStephen Hemminger 		}
1037b4ad86bfSDavid S. Miller 	}
1038b4ad86bfSDavid S. Miller 
1039b4ad86bfSDavid S. Miller 	return 0;
1040b4ad86bfSDavid S. Miller }
1041b4ad86bfSDavid S. Miller 
10421da177e4SLinus Torvalds static int copy_from_user_tmpl(struct xfrm_policy *pol, struct rtattr **xfrma)
10431da177e4SLinus Torvalds {
10441da177e4SLinus Torvalds 	struct rtattr *rt = xfrma[XFRMA_TMPL-1];
10451da177e4SLinus Torvalds 
10461da177e4SLinus Torvalds 	if (!rt) {
10471da177e4SLinus Torvalds 		pol->xfrm_nr = 0;
10481da177e4SLinus Torvalds 	} else {
1049b4ad86bfSDavid S. Miller 		struct xfrm_user_tmpl *utmpl = RTA_DATA(rt);
1050b4ad86bfSDavid S. Miller 		int nr = (rt->rta_len - sizeof(*rt)) / sizeof(*utmpl);
1051b4ad86bfSDavid S. Miller 		int err;
10521da177e4SLinus Torvalds 
1053b4ad86bfSDavid S. Miller 		err = validate_tmpl(nr, utmpl, pol->family);
1054b4ad86bfSDavid S. Miller 		if (err)
1055b4ad86bfSDavid S. Miller 			return err;
10561da177e4SLinus Torvalds 
10571da177e4SLinus Torvalds 		copy_templates(pol, RTA_DATA(rt), nr);
10581da177e4SLinus Torvalds 	}
10591da177e4SLinus Torvalds 	return 0;
10601da177e4SLinus Torvalds }
10611da177e4SLinus Torvalds 
1062f7b6983fSMasahide NAKAMURA static int copy_from_user_policy_type(u8 *tp, struct rtattr **xfrma)
1063f7b6983fSMasahide NAKAMURA {
1064f7b6983fSMasahide NAKAMURA 	struct rtattr *rt = xfrma[XFRMA_POLICY_TYPE-1];
1065f7b6983fSMasahide NAKAMURA 	struct xfrm_userpolicy_type *upt;
1066b798a9edSJamal Hadi Salim 	u8 type = XFRM_POLICY_TYPE_MAIN;
1067f7b6983fSMasahide NAKAMURA 	int err;
1068f7b6983fSMasahide NAKAMURA 
1069f7b6983fSMasahide NAKAMURA 	if (rt) {
1070f7b6983fSMasahide NAKAMURA 		if (rt->rta_len < sizeof(*upt))
1071f7b6983fSMasahide NAKAMURA 			return -EINVAL;
1072f7b6983fSMasahide NAKAMURA 
1073f7b6983fSMasahide NAKAMURA 		upt = RTA_DATA(rt);
1074f7b6983fSMasahide NAKAMURA 		type = upt->type;
1075f7b6983fSMasahide NAKAMURA 	}
1076f7b6983fSMasahide NAKAMURA 
1077f7b6983fSMasahide NAKAMURA 	err = verify_policy_type(type);
1078f7b6983fSMasahide NAKAMURA 	if (err)
1079f7b6983fSMasahide NAKAMURA 		return err;
1080f7b6983fSMasahide NAKAMURA 
1081f7b6983fSMasahide NAKAMURA 	*tp = type;
1082f7b6983fSMasahide NAKAMURA 	return 0;
1083f7b6983fSMasahide NAKAMURA }
1084f7b6983fSMasahide NAKAMURA 
10851da177e4SLinus Torvalds static void copy_from_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p)
10861da177e4SLinus Torvalds {
10871da177e4SLinus Torvalds 	xp->priority = p->priority;
10881da177e4SLinus Torvalds 	xp->index = p->index;
10891da177e4SLinus Torvalds 	memcpy(&xp->selector, &p->sel, sizeof(xp->selector));
10901da177e4SLinus Torvalds 	memcpy(&xp->lft, &p->lft, sizeof(xp->lft));
10911da177e4SLinus Torvalds 	xp->action = p->action;
10921da177e4SLinus Torvalds 	xp->flags = p->flags;
10931da177e4SLinus Torvalds 	xp->family = p->sel.family;
10941da177e4SLinus Torvalds 	/* XXX xp->share = p->share; */
10951da177e4SLinus Torvalds }
10961da177e4SLinus Torvalds 
10971da177e4SLinus Torvalds static void copy_to_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p, int dir)
10981da177e4SLinus Torvalds {
10991da177e4SLinus Torvalds 	memcpy(&p->sel, &xp->selector, sizeof(p->sel));
11001da177e4SLinus Torvalds 	memcpy(&p->lft, &xp->lft, sizeof(p->lft));
11011da177e4SLinus Torvalds 	memcpy(&p->curlft, &xp->curlft, sizeof(p->curlft));
11021da177e4SLinus Torvalds 	p->priority = xp->priority;
11031da177e4SLinus Torvalds 	p->index = xp->index;
11041da177e4SLinus Torvalds 	p->sel.family = xp->family;
11051da177e4SLinus Torvalds 	p->dir = dir;
11061da177e4SLinus Torvalds 	p->action = xp->action;
11071da177e4SLinus Torvalds 	p->flags = xp->flags;
11081da177e4SLinus Torvalds 	p->share = XFRM_SHARE_ANY; /* XXX xp->share */
11091da177e4SLinus Torvalds }
11101da177e4SLinus Torvalds 
11111da177e4SLinus Torvalds static struct xfrm_policy *xfrm_policy_construct(struct xfrm_userpolicy_info *p, struct rtattr **xfrma, int *errp)
11121da177e4SLinus Torvalds {
11131da177e4SLinus Torvalds 	struct xfrm_policy *xp = xfrm_policy_alloc(GFP_KERNEL);
11141da177e4SLinus Torvalds 	int err;
11151da177e4SLinus Torvalds 
11161da177e4SLinus Torvalds 	if (!xp) {
11171da177e4SLinus Torvalds 		*errp = -ENOMEM;
11181da177e4SLinus Torvalds 		return NULL;
11191da177e4SLinus Torvalds 	}
11201da177e4SLinus Torvalds 
11211da177e4SLinus Torvalds 	copy_from_user_policy(xp, p);
1122df71837dSTrent Jaeger 
1123f7b6983fSMasahide NAKAMURA 	err = copy_from_user_policy_type(&xp->type, xfrma);
1124f7b6983fSMasahide NAKAMURA 	if (err)
1125f7b6983fSMasahide NAKAMURA 		goto error;
1126f7b6983fSMasahide NAKAMURA 
1127df71837dSTrent Jaeger 	if (!(err = copy_from_user_tmpl(xp, xfrma)))
1128df71837dSTrent Jaeger 		err = copy_from_user_sec_ctx(xp, xfrma);
1129f7b6983fSMasahide NAKAMURA 	if (err)
1130f7b6983fSMasahide NAKAMURA 		goto error;
11311da177e4SLinus Torvalds 
11321da177e4SLinus Torvalds 	return xp;
1133f7b6983fSMasahide NAKAMURA  error:
1134f7b6983fSMasahide NAKAMURA 	*errp = err;
1135f7b6983fSMasahide NAKAMURA 	kfree(xp);
1136f7b6983fSMasahide NAKAMURA 	return NULL;
11371da177e4SLinus Torvalds }
11381da177e4SLinus Torvalds 
113922e70050SChristoph Hellwig static int xfrm_add_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
114022e70050SChristoph Hellwig 		struct rtattr **xfrma)
11411da177e4SLinus Torvalds {
11427b67c857SThomas Graf 	struct xfrm_userpolicy_info *p = nlmsg_data(nlh);
11431da177e4SLinus Torvalds 	struct xfrm_policy *xp;
114426b15dadSJamal Hadi Salim 	struct km_event c;
11451da177e4SLinus Torvalds 	int err;
11461da177e4SLinus Torvalds 	int excl;
11471da177e4SLinus Torvalds 
11481da177e4SLinus Torvalds 	err = verify_newpolicy_info(p);
11491da177e4SLinus Torvalds 	if (err)
11501da177e4SLinus Torvalds 		return err;
115122e70050SChristoph Hellwig 	err = verify_sec_ctx_len(xfrma);
1152df71837dSTrent Jaeger 	if (err)
1153df71837dSTrent Jaeger 		return err;
11541da177e4SLinus Torvalds 
115522e70050SChristoph Hellwig 	xp = xfrm_policy_construct(p, xfrma, &err);
11561da177e4SLinus Torvalds 	if (!xp)
11571da177e4SLinus Torvalds 		return err;
11581da177e4SLinus Torvalds 
115926b15dadSJamal Hadi Salim 	/* shouldnt excl be based on nlh flags??
116026b15dadSJamal Hadi Salim 	 * Aha! this is anti-netlink really i.e  more pfkey derived
116126b15dadSJamal Hadi Salim 	 * in netlink excl is a flag and you wouldnt need
116226b15dadSJamal Hadi Salim 	 * a type XFRM_MSG_UPDPOLICY - JHS */
11631da177e4SLinus Torvalds 	excl = nlh->nlmsg_type == XFRM_MSG_NEWPOLICY;
11641da177e4SLinus Torvalds 	err = xfrm_policy_insert(p->dir, xp, excl);
1165161a09e7SJoy Latten 	xfrm_audit_log(NETLINK_CB(skb).loginuid, NETLINK_CB(skb).sid,
1166161a09e7SJoy Latten 		       AUDIT_MAC_IPSEC_DELSPD, err ? 0 : 1, xp, NULL);
1167161a09e7SJoy Latten 
11681da177e4SLinus Torvalds 	if (err) {
11695f8ac64bSTrent Jaeger 		security_xfrm_policy_free(xp);
11701da177e4SLinus Torvalds 		kfree(xp);
11711da177e4SLinus Torvalds 		return err;
11721da177e4SLinus Torvalds 	}
11731da177e4SLinus Torvalds 
1174f60f6b8fSHerbert Xu 	c.event = nlh->nlmsg_type;
117526b15dadSJamal Hadi Salim 	c.seq = nlh->nlmsg_seq;
117626b15dadSJamal Hadi Salim 	c.pid = nlh->nlmsg_pid;
117726b15dadSJamal Hadi Salim 	km_policy_notify(xp, p->dir, &c);
117826b15dadSJamal Hadi Salim 
11791da177e4SLinus Torvalds 	xfrm_pol_put(xp);
11801da177e4SLinus Torvalds 
11811da177e4SLinus Torvalds 	return 0;
11821da177e4SLinus Torvalds }
11831da177e4SLinus Torvalds 
11841da177e4SLinus Torvalds static int copy_to_user_tmpl(struct xfrm_policy *xp, struct sk_buff *skb)
11851da177e4SLinus Torvalds {
11861da177e4SLinus Torvalds 	struct xfrm_user_tmpl vec[XFRM_MAX_DEPTH];
11871da177e4SLinus Torvalds 	int i;
11881da177e4SLinus Torvalds 
11891da177e4SLinus Torvalds 	if (xp->xfrm_nr == 0)
11901da177e4SLinus Torvalds 		return 0;
11911da177e4SLinus Torvalds 
11921da177e4SLinus Torvalds 	for (i = 0; i < xp->xfrm_nr; i++) {
11931da177e4SLinus Torvalds 		struct xfrm_user_tmpl *up = &vec[i];
11941da177e4SLinus Torvalds 		struct xfrm_tmpl *kp = &xp->xfrm_vec[i];
11951da177e4SLinus Torvalds 
11961da177e4SLinus Torvalds 		memcpy(&up->id, &kp->id, sizeof(up->id));
11978511d01dSMiika Komu 		up->family = kp->encap_family;
11981da177e4SLinus Torvalds 		memcpy(&up->saddr, &kp->saddr, sizeof(up->saddr));
11991da177e4SLinus Torvalds 		up->reqid = kp->reqid;
12001da177e4SLinus Torvalds 		up->mode = kp->mode;
12011da177e4SLinus Torvalds 		up->share = kp->share;
12021da177e4SLinus Torvalds 		up->optional = kp->optional;
12031da177e4SLinus Torvalds 		up->aalgos = kp->aalgos;
12041da177e4SLinus Torvalds 		up->ealgos = kp->ealgos;
12051da177e4SLinus Torvalds 		up->calgos = kp->calgos;
12061da177e4SLinus Torvalds 	}
12071da177e4SLinus Torvalds 
1208c0144beaSThomas Graf 	return nla_put(skb, XFRMA_TMPL,
1209c0144beaSThomas Graf 		       sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr, vec);
1210df71837dSTrent Jaeger }
1211df71837dSTrent Jaeger 
12120d681623SSerge Hallyn static inline int copy_to_user_state_sec_ctx(struct xfrm_state *x, struct sk_buff *skb)
12130d681623SSerge Hallyn {
12140d681623SSerge Hallyn 	if (x->security) {
12150d681623SSerge Hallyn 		return copy_sec_ctx(x->security, skb);
12160d681623SSerge Hallyn 	}
12170d681623SSerge Hallyn 	return 0;
12180d681623SSerge Hallyn }
12190d681623SSerge Hallyn 
12200d681623SSerge Hallyn static inline int copy_to_user_sec_ctx(struct xfrm_policy *xp, struct sk_buff *skb)
12210d681623SSerge Hallyn {
12220d681623SSerge Hallyn 	if (xp->security) {
12230d681623SSerge Hallyn 		return copy_sec_ctx(xp->security, skb);
12240d681623SSerge Hallyn 	}
12250d681623SSerge Hallyn 	return 0;
12260d681623SSerge Hallyn }
1227*cfbfd45aSThomas Graf static inline size_t userpolicy_type_attrsize(void)
1228*cfbfd45aSThomas Graf {
1229*cfbfd45aSThomas Graf #ifdef CONFIG_XFRM_SUB_POLICY
1230*cfbfd45aSThomas Graf 	return nla_total_size(sizeof(struct xfrm_userpolicy_type));
1231*cfbfd45aSThomas Graf #else
1232*cfbfd45aSThomas Graf 	return 0;
1233*cfbfd45aSThomas Graf #endif
1234*cfbfd45aSThomas Graf }
12350d681623SSerge Hallyn 
1236f7b6983fSMasahide NAKAMURA #ifdef CONFIG_XFRM_SUB_POLICY
1237b798a9edSJamal Hadi Salim static int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
1238f7b6983fSMasahide NAKAMURA {
1239c0144beaSThomas Graf 	struct xfrm_userpolicy_type upt = {
1240c0144beaSThomas Graf 		.type = type,
1241c0144beaSThomas Graf 	};
1242f7b6983fSMasahide NAKAMURA 
1243c0144beaSThomas Graf 	return nla_put(skb, XFRMA_POLICY_TYPE, sizeof(upt), &upt);
1244f7b6983fSMasahide NAKAMURA }
1245f7b6983fSMasahide NAKAMURA 
1246f7b6983fSMasahide NAKAMURA #else
1247b798a9edSJamal Hadi Salim static inline int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
1248f7b6983fSMasahide NAKAMURA {
1249f7b6983fSMasahide NAKAMURA 	return 0;
1250f7b6983fSMasahide NAKAMURA }
1251f7b6983fSMasahide NAKAMURA #endif
1252f7b6983fSMasahide NAKAMURA 
12531da177e4SLinus Torvalds static int dump_one_policy(struct xfrm_policy *xp, int dir, int count, void *ptr)
12541da177e4SLinus Torvalds {
12551da177e4SLinus Torvalds 	struct xfrm_dump_info *sp = ptr;
12561da177e4SLinus Torvalds 	struct xfrm_userpolicy_info *p;
12571da177e4SLinus Torvalds 	struct sk_buff *in_skb = sp->in_skb;
12581da177e4SLinus Torvalds 	struct sk_buff *skb = sp->out_skb;
12591da177e4SLinus Torvalds 	struct nlmsghdr *nlh;
12601da177e4SLinus Torvalds 
12611da177e4SLinus Torvalds 	if (sp->this_idx < sp->start_idx)
12621da177e4SLinus Torvalds 		goto out;
12631da177e4SLinus Torvalds 
126479b8b7f4SThomas Graf 	nlh = nlmsg_put(skb, NETLINK_CB(in_skb).pid, sp->nlmsg_seq,
126579b8b7f4SThomas Graf 			XFRM_MSG_NEWPOLICY, sizeof(*p), sp->nlmsg_flags);
126679b8b7f4SThomas Graf 	if (nlh == NULL)
126779b8b7f4SThomas Graf 		return -EMSGSIZE;
12681da177e4SLinus Torvalds 
12697b67c857SThomas Graf 	p = nlmsg_data(nlh);
12701da177e4SLinus Torvalds 	copy_to_user_policy(xp, p, dir);
12711da177e4SLinus Torvalds 	if (copy_to_user_tmpl(xp, skb) < 0)
12721da177e4SLinus Torvalds 		goto nlmsg_failure;
1273df71837dSTrent Jaeger 	if (copy_to_user_sec_ctx(xp, skb))
1274df71837dSTrent Jaeger 		goto nlmsg_failure;
12751459bb36SJamal Hadi Salim 	if (copy_to_user_policy_type(xp->type, skb) < 0)
1276f7b6983fSMasahide NAKAMURA 		goto nlmsg_failure;
12771da177e4SLinus Torvalds 
12789825069dSThomas Graf 	nlmsg_end(skb, nlh);
12791da177e4SLinus Torvalds out:
12801da177e4SLinus Torvalds 	sp->this_idx++;
12811da177e4SLinus Torvalds 	return 0;
12821da177e4SLinus Torvalds 
12831da177e4SLinus Torvalds nlmsg_failure:
12849825069dSThomas Graf 	nlmsg_cancel(skb, nlh);
12859825069dSThomas Graf 	return -EMSGSIZE;
12861da177e4SLinus Torvalds }
12871da177e4SLinus Torvalds 
12881da177e4SLinus Torvalds static int xfrm_dump_policy(struct sk_buff *skb, struct netlink_callback *cb)
12891da177e4SLinus Torvalds {
12901da177e4SLinus Torvalds 	struct xfrm_dump_info info;
12911da177e4SLinus Torvalds 
12921da177e4SLinus Torvalds 	info.in_skb = cb->skb;
12931da177e4SLinus Torvalds 	info.out_skb = skb;
12941da177e4SLinus Torvalds 	info.nlmsg_seq = cb->nlh->nlmsg_seq;
12951da177e4SLinus Torvalds 	info.nlmsg_flags = NLM_F_MULTI;
12961da177e4SLinus Torvalds 	info.this_idx = 0;
12971da177e4SLinus Torvalds 	info.start_idx = cb->args[0];
1298f7b6983fSMasahide NAKAMURA 	(void) xfrm_policy_walk(XFRM_POLICY_TYPE_MAIN, dump_one_policy, &info);
1299f7b6983fSMasahide NAKAMURA #ifdef CONFIG_XFRM_SUB_POLICY
1300f7b6983fSMasahide NAKAMURA 	(void) xfrm_policy_walk(XFRM_POLICY_TYPE_SUB, dump_one_policy, &info);
1301f7b6983fSMasahide NAKAMURA #endif
13021da177e4SLinus Torvalds 	cb->args[0] = info.this_idx;
13031da177e4SLinus Torvalds 
13041da177e4SLinus Torvalds 	return skb->len;
13051da177e4SLinus Torvalds }
13061da177e4SLinus Torvalds 
13071da177e4SLinus Torvalds static struct sk_buff *xfrm_policy_netlink(struct sk_buff *in_skb,
13081da177e4SLinus Torvalds 					  struct xfrm_policy *xp,
13091da177e4SLinus Torvalds 					  int dir, u32 seq)
13101da177e4SLinus Torvalds {
13111da177e4SLinus Torvalds 	struct xfrm_dump_info info;
13121da177e4SLinus Torvalds 	struct sk_buff *skb;
13131da177e4SLinus Torvalds 
13141da177e4SLinus Torvalds 	skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
13151da177e4SLinus Torvalds 	if (!skb)
13161da177e4SLinus Torvalds 		return ERR_PTR(-ENOMEM);
13171da177e4SLinus Torvalds 
13181da177e4SLinus Torvalds 	info.in_skb = in_skb;
13191da177e4SLinus Torvalds 	info.out_skb = skb;
13201da177e4SLinus Torvalds 	info.nlmsg_seq = seq;
13211da177e4SLinus Torvalds 	info.nlmsg_flags = 0;
13221da177e4SLinus Torvalds 	info.this_idx = info.start_idx = 0;
13231da177e4SLinus Torvalds 
13241da177e4SLinus Torvalds 	if (dump_one_policy(xp, dir, 0, &info) < 0) {
13251da177e4SLinus Torvalds 		kfree_skb(skb);
13261da177e4SLinus Torvalds 		return NULL;
13271da177e4SLinus Torvalds 	}
13281da177e4SLinus Torvalds 
13291da177e4SLinus Torvalds 	return skb;
13301da177e4SLinus Torvalds }
13311da177e4SLinus Torvalds 
133222e70050SChristoph Hellwig static int xfrm_get_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
133322e70050SChristoph Hellwig 		struct rtattr **xfrma)
13341da177e4SLinus Torvalds {
13351da177e4SLinus Torvalds 	struct xfrm_policy *xp;
13361da177e4SLinus Torvalds 	struct xfrm_userpolicy_id *p;
1337b798a9edSJamal Hadi Salim 	u8 type = XFRM_POLICY_TYPE_MAIN;
13381da177e4SLinus Torvalds 	int err;
133926b15dadSJamal Hadi Salim 	struct km_event c;
13401da177e4SLinus Torvalds 	int delete;
13411da177e4SLinus Torvalds 
13427b67c857SThomas Graf 	p = nlmsg_data(nlh);
13431da177e4SLinus Torvalds 	delete = nlh->nlmsg_type == XFRM_MSG_DELPOLICY;
13441da177e4SLinus Torvalds 
134522e70050SChristoph Hellwig 	err = copy_from_user_policy_type(&type, xfrma);
1346f7b6983fSMasahide NAKAMURA 	if (err)
1347f7b6983fSMasahide NAKAMURA 		return err;
1348f7b6983fSMasahide NAKAMURA 
13491da177e4SLinus Torvalds 	err = verify_policy_dir(p->dir);
13501da177e4SLinus Torvalds 	if (err)
13511da177e4SLinus Torvalds 		return err;
13521da177e4SLinus Torvalds 
13531da177e4SLinus Torvalds 	if (p->index)
1354ef41aaa0SEric Paris 		xp = xfrm_policy_byid(type, p->dir, p->index, delete, &err);
1355df71837dSTrent Jaeger 	else {
135622e70050SChristoph Hellwig 		struct rtattr *rt = xfrma[XFRMA_SEC_CTX-1];
1357df71837dSTrent Jaeger 		struct xfrm_policy tmp;
1358df71837dSTrent Jaeger 
135922e70050SChristoph Hellwig 		err = verify_sec_ctx_len(xfrma);
1360df71837dSTrent Jaeger 		if (err)
1361df71837dSTrent Jaeger 			return err;
1362df71837dSTrent Jaeger 
1363df71837dSTrent Jaeger 		memset(&tmp, 0, sizeof(struct xfrm_policy));
1364df71837dSTrent Jaeger 		if (rt) {
1365df71837dSTrent Jaeger 			struct xfrm_user_sec_ctx *uctx = RTA_DATA(rt);
1366df71837dSTrent Jaeger 
1367df71837dSTrent Jaeger 			if ((err = security_xfrm_policy_alloc(&tmp, uctx)))
1368df71837dSTrent Jaeger 				return err;
1369df71837dSTrent Jaeger 		}
1370ef41aaa0SEric Paris 		xp = xfrm_policy_bysel_ctx(type, p->dir, &p->sel, tmp.security,
1371ef41aaa0SEric Paris 					   delete, &err);
1372df71837dSTrent Jaeger 		security_xfrm_policy_free(&tmp);
1373df71837dSTrent Jaeger 	}
13741da177e4SLinus Torvalds 	if (xp == NULL)
13751da177e4SLinus Torvalds 		return -ENOENT;
13761da177e4SLinus Torvalds 
13771da177e4SLinus Torvalds 	if (!delete) {
13781da177e4SLinus Torvalds 		struct sk_buff *resp_skb;
13791da177e4SLinus Torvalds 
13801da177e4SLinus Torvalds 		resp_skb = xfrm_policy_netlink(skb, xp, p->dir, nlh->nlmsg_seq);
13811da177e4SLinus Torvalds 		if (IS_ERR(resp_skb)) {
13821da177e4SLinus Torvalds 			err = PTR_ERR(resp_skb);
13831da177e4SLinus Torvalds 		} else {
1384082a1ad5SThomas Graf 			err = nlmsg_unicast(xfrm_nl, resp_skb,
1385082a1ad5SThomas Graf 					    NETLINK_CB(skb).pid);
13861da177e4SLinus Torvalds 		}
138726b15dadSJamal Hadi Salim 	} else {
138813fcfbb0SDavid S. Miller 		xfrm_audit_log(NETLINK_CB(skb).loginuid, NETLINK_CB(skb).sid,
138913fcfbb0SDavid S. Miller 			       AUDIT_MAC_IPSEC_DELSPD, err ? 0 : 1, xp, NULL);
139013fcfbb0SDavid S. Miller 
139113fcfbb0SDavid S. Miller 		if (err != 0)
1392c8c05a8eSCatherine Zhang 			goto out;
139313fcfbb0SDavid S. Miller 
1394e7443892SHerbert Xu 		c.data.byid = p->index;
1395f60f6b8fSHerbert Xu 		c.event = nlh->nlmsg_type;
139626b15dadSJamal Hadi Salim 		c.seq = nlh->nlmsg_seq;
139726b15dadSJamal Hadi Salim 		c.pid = nlh->nlmsg_pid;
139826b15dadSJamal Hadi Salim 		km_policy_notify(xp, p->dir, &c);
13991da177e4SLinus Torvalds 	}
14001da177e4SLinus Torvalds 
1401c8c05a8eSCatherine Zhang out:
1402ef41aaa0SEric Paris 	xfrm_pol_put(xp);
14031da177e4SLinus Torvalds 	return err;
14041da177e4SLinus Torvalds }
14051da177e4SLinus Torvalds 
140622e70050SChristoph Hellwig static int xfrm_flush_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
140722e70050SChristoph Hellwig 		struct rtattr **xfrma)
14081da177e4SLinus Torvalds {
140926b15dadSJamal Hadi Salim 	struct km_event c;
14107b67c857SThomas Graf 	struct xfrm_usersa_flush *p = nlmsg_data(nlh);
1411161a09e7SJoy Latten 	struct xfrm_audit audit_info;
14124aa2e62cSJoy Latten 	int err;
14131da177e4SLinus Torvalds 
1414161a09e7SJoy Latten 	audit_info.loginuid = NETLINK_CB(skb).loginuid;
1415161a09e7SJoy Latten 	audit_info.secid = NETLINK_CB(skb).sid;
14164aa2e62cSJoy Latten 	err = xfrm_state_flush(p->proto, &audit_info);
14174aa2e62cSJoy Latten 	if (err)
14184aa2e62cSJoy Latten 		return err;
1419bf08867fSHerbert Xu 	c.data.proto = p->proto;
1420f60f6b8fSHerbert Xu 	c.event = nlh->nlmsg_type;
142126b15dadSJamal Hadi Salim 	c.seq = nlh->nlmsg_seq;
142226b15dadSJamal Hadi Salim 	c.pid = nlh->nlmsg_pid;
142326b15dadSJamal Hadi Salim 	km_state_notify(NULL, &c);
142426b15dadSJamal Hadi Salim 
14251da177e4SLinus Torvalds 	return 0;
14261da177e4SLinus Torvalds }
14271da177e4SLinus Torvalds 
1428d51d081dSJamal Hadi Salim 
1429d51d081dSJamal Hadi Salim static int build_aevent(struct sk_buff *skb, struct xfrm_state *x, struct km_event *c)
1430d51d081dSJamal Hadi Salim {
1431d51d081dSJamal Hadi Salim 	struct xfrm_aevent_id *id;
1432d51d081dSJamal Hadi Salim 	struct nlmsghdr *nlh;
1433d51d081dSJamal Hadi Salim 
143479b8b7f4SThomas Graf 	nlh = nlmsg_put(skb, c->pid, c->seq, XFRM_MSG_NEWAE, sizeof(*id), 0);
143579b8b7f4SThomas Graf 	if (nlh == NULL)
143679b8b7f4SThomas Graf 		return -EMSGSIZE;
1437d51d081dSJamal Hadi Salim 
14387b67c857SThomas Graf 	id = nlmsg_data(nlh);
14392b5f6dccSJamal Hadi Salim 	memcpy(&id->sa_id.daddr, &x->id.daddr,sizeof(x->id.daddr));
1440d51d081dSJamal Hadi Salim 	id->sa_id.spi = x->id.spi;
1441d51d081dSJamal Hadi Salim 	id->sa_id.family = x->props.family;
1442d51d081dSJamal Hadi Salim 	id->sa_id.proto = x->id.proto;
14432b5f6dccSJamal Hadi Salim 	memcpy(&id->saddr, &x->props.saddr,sizeof(x->props.saddr));
14442b5f6dccSJamal Hadi Salim 	id->reqid = x->props.reqid;
1445d51d081dSJamal Hadi Salim 	id->flags = c->data.aevent;
1446d51d081dSJamal Hadi Salim 
1447c0144beaSThomas Graf 	NLA_PUT(skb, XFRMA_REPLAY_VAL, sizeof(x->replay), &x->replay);
1448c0144beaSThomas Graf 	NLA_PUT(skb, XFRMA_LTIME_VAL, sizeof(x->curlft), &x->curlft);
1449d51d081dSJamal Hadi Salim 
1450c0144beaSThomas Graf 	if (id->flags & XFRM_AE_RTHR)
1451c0144beaSThomas Graf 		NLA_PUT_U32(skb, XFRMA_REPLAY_THRESH, x->replay_maxdiff);
1452d51d081dSJamal Hadi Salim 
1453c0144beaSThomas Graf 	if (id->flags & XFRM_AE_ETHR)
1454c0144beaSThomas Graf 		NLA_PUT_U32(skb, XFRMA_ETIMER_THRESH,
1455c0144beaSThomas Graf 			    x->replay_maxage * 10 / HZ);
1456d51d081dSJamal Hadi Salim 
14579825069dSThomas Graf 	return nlmsg_end(skb, nlh);
1458d51d081dSJamal Hadi Salim 
1459c0144beaSThomas Graf nla_put_failure:
14609825069dSThomas Graf 	nlmsg_cancel(skb, nlh);
14619825069dSThomas Graf 	return -EMSGSIZE;
1462d51d081dSJamal Hadi Salim }
1463d51d081dSJamal Hadi Salim 
146422e70050SChristoph Hellwig static int xfrm_get_ae(struct sk_buff *skb, struct nlmsghdr *nlh,
146522e70050SChristoph Hellwig 		struct rtattr **xfrma)
1466d51d081dSJamal Hadi Salim {
1467d51d081dSJamal Hadi Salim 	struct xfrm_state *x;
1468d51d081dSJamal Hadi Salim 	struct sk_buff *r_skb;
1469d51d081dSJamal Hadi Salim 	int err;
1470d51d081dSJamal Hadi Salim 	struct km_event c;
14717b67c857SThomas Graf 	struct xfrm_aevent_id *p = nlmsg_data(nlh);
1472d51d081dSJamal Hadi Salim 	int len = NLMSG_LENGTH(sizeof(struct xfrm_aevent_id));
1473d51d081dSJamal Hadi Salim 	struct xfrm_usersa_id *id = &p->sa_id;
1474d51d081dSJamal Hadi Salim 
1475d51d081dSJamal Hadi Salim 	len += RTA_SPACE(sizeof(struct xfrm_replay_state));
1476d51d081dSJamal Hadi Salim 	len += RTA_SPACE(sizeof(struct xfrm_lifetime_cur));
1477d51d081dSJamal Hadi Salim 
1478d51d081dSJamal Hadi Salim 	if (p->flags&XFRM_AE_RTHR)
1479d51d081dSJamal Hadi Salim 		len+=RTA_SPACE(sizeof(u32));
1480d51d081dSJamal Hadi Salim 
1481d51d081dSJamal Hadi Salim 	if (p->flags&XFRM_AE_ETHR)
1482d51d081dSJamal Hadi Salim 		len+=RTA_SPACE(sizeof(u32));
1483d51d081dSJamal Hadi Salim 
1484d51d081dSJamal Hadi Salim 	r_skb = alloc_skb(len, GFP_ATOMIC);
1485d51d081dSJamal Hadi Salim 	if (r_skb == NULL)
1486d51d081dSJamal Hadi Salim 		return -ENOMEM;
1487d51d081dSJamal Hadi Salim 
1488d51d081dSJamal Hadi Salim 	x = xfrm_state_lookup(&id->daddr, id->spi, id->proto, id->family);
1489d51d081dSJamal Hadi Salim 	if (x == NULL) {
1490b08d5840SPatrick McHardy 		kfree_skb(r_skb);
1491d51d081dSJamal Hadi Salim 		return -ESRCH;
1492d51d081dSJamal Hadi Salim 	}
1493d51d081dSJamal Hadi Salim 
1494d51d081dSJamal Hadi Salim 	/*
1495d51d081dSJamal Hadi Salim 	 * XXX: is this lock really needed - none of the other
1496d51d081dSJamal Hadi Salim 	 * gets lock (the concern is things getting updated
1497d51d081dSJamal Hadi Salim 	 * while we are still reading) - jhs
1498d51d081dSJamal Hadi Salim 	*/
1499d51d081dSJamal Hadi Salim 	spin_lock_bh(&x->lock);
1500d51d081dSJamal Hadi Salim 	c.data.aevent = p->flags;
1501d51d081dSJamal Hadi Salim 	c.seq = nlh->nlmsg_seq;
1502d51d081dSJamal Hadi Salim 	c.pid = nlh->nlmsg_pid;
1503d51d081dSJamal Hadi Salim 
1504d51d081dSJamal Hadi Salim 	if (build_aevent(r_skb, x, &c) < 0)
1505d51d081dSJamal Hadi Salim 		BUG();
1506082a1ad5SThomas Graf 	err = nlmsg_unicast(xfrm_nl, r_skb, NETLINK_CB(skb).pid);
1507d51d081dSJamal Hadi Salim 	spin_unlock_bh(&x->lock);
1508d51d081dSJamal Hadi Salim 	xfrm_state_put(x);
1509d51d081dSJamal Hadi Salim 	return err;
1510d51d081dSJamal Hadi Salim }
1511d51d081dSJamal Hadi Salim 
151222e70050SChristoph Hellwig static int xfrm_new_ae(struct sk_buff *skb, struct nlmsghdr *nlh,
151322e70050SChristoph Hellwig 		struct rtattr **xfrma)
1514d51d081dSJamal Hadi Salim {
1515d51d081dSJamal Hadi Salim 	struct xfrm_state *x;
1516d51d081dSJamal Hadi Salim 	struct km_event c;
1517d51d081dSJamal Hadi Salim 	int err = - EINVAL;
15187b67c857SThomas Graf 	struct xfrm_aevent_id *p = nlmsg_data(nlh);
1519d51d081dSJamal Hadi Salim 	struct rtattr *rp = xfrma[XFRMA_REPLAY_VAL-1];
1520d51d081dSJamal Hadi Salim 	struct rtattr *lt = xfrma[XFRMA_LTIME_VAL-1];
1521d51d081dSJamal Hadi Salim 
1522d51d081dSJamal Hadi Salim 	if (!lt && !rp)
1523d51d081dSJamal Hadi Salim 		return err;
1524d51d081dSJamal Hadi Salim 
1525d51d081dSJamal Hadi Salim 	/* pedantic mode - thou shalt sayeth replaceth */
1526d51d081dSJamal Hadi Salim 	if (!(nlh->nlmsg_flags&NLM_F_REPLACE))
1527d51d081dSJamal Hadi Salim 		return err;
1528d51d081dSJamal Hadi Salim 
1529d51d081dSJamal Hadi Salim 	x = xfrm_state_lookup(&p->sa_id.daddr, p->sa_id.spi, p->sa_id.proto, p->sa_id.family);
1530d51d081dSJamal Hadi Salim 	if (x == NULL)
1531d51d081dSJamal Hadi Salim 		return -ESRCH;
1532d51d081dSJamal Hadi Salim 
1533d51d081dSJamal Hadi Salim 	if (x->km.state != XFRM_STATE_VALID)
1534d51d081dSJamal Hadi Salim 		goto out;
1535d51d081dSJamal Hadi Salim 
1536d51d081dSJamal Hadi Salim 	spin_lock_bh(&x->lock);
153722e70050SChristoph Hellwig 	err = xfrm_update_ae_params(x, xfrma);
1538d51d081dSJamal Hadi Salim 	spin_unlock_bh(&x->lock);
1539d51d081dSJamal Hadi Salim 	if (err	< 0)
1540d51d081dSJamal Hadi Salim 		goto out;
1541d51d081dSJamal Hadi Salim 
1542d51d081dSJamal Hadi Salim 	c.event = nlh->nlmsg_type;
1543d51d081dSJamal Hadi Salim 	c.seq = nlh->nlmsg_seq;
1544d51d081dSJamal Hadi Salim 	c.pid = nlh->nlmsg_pid;
1545d51d081dSJamal Hadi Salim 	c.data.aevent = XFRM_AE_CU;
1546d51d081dSJamal Hadi Salim 	km_state_notify(x, &c);
1547d51d081dSJamal Hadi Salim 	err = 0;
1548d51d081dSJamal Hadi Salim out:
1549d51d081dSJamal Hadi Salim 	xfrm_state_put(x);
1550d51d081dSJamal Hadi Salim 	return err;
1551d51d081dSJamal Hadi Salim }
1552d51d081dSJamal Hadi Salim 
155322e70050SChristoph Hellwig static int xfrm_flush_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
155422e70050SChristoph Hellwig 		struct rtattr **xfrma)
15551da177e4SLinus Torvalds {
155626b15dadSJamal Hadi Salim 	struct km_event c;
1557b798a9edSJamal Hadi Salim 	u8 type = XFRM_POLICY_TYPE_MAIN;
1558f7b6983fSMasahide NAKAMURA 	int err;
1559161a09e7SJoy Latten 	struct xfrm_audit audit_info;
156026b15dadSJamal Hadi Salim 
156122e70050SChristoph Hellwig 	err = copy_from_user_policy_type(&type, xfrma);
1562f7b6983fSMasahide NAKAMURA 	if (err)
1563f7b6983fSMasahide NAKAMURA 		return err;
1564f7b6983fSMasahide NAKAMURA 
1565161a09e7SJoy Latten 	audit_info.loginuid = NETLINK_CB(skb).loginuid;
1566161a09e7SJoy Latten 	audit_info.secid = NETLINK_CB(skb).sid;
15674aa2e62cSJoy Latten 	err = xfrm_policy_flush(type, &audit_info);
15684aa2e62cSJoy Latten 	if (err)
15694aa2e62cSJoy Latten 		return err;
1570f7b6983fSMasahide NAKAMURA 	c.data.type = type;
1571f60f6b8fSHerbert Xu 	c.event = nlh->nlmsg_type;
157226b15dadSJamal Hadi Salim 	c.seq = nlh->nlmsg_seq;
157326b15dadSJamal Hadi Salim 	c.pid = nlh->nlmsg_pid;
157426b15dadSJamal Hadi Salim 	km_policy_notify(NULL, 0, &c);
15751da177e4SLinus Torvalds 	return 0;
15761da177e4SLinus Torvalds }
15771da177e4SLinus Torvalds 
157822e70050SChristoph Hellwig static int xfrm_add_pol_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
157922e70050SChristoph Hellwig 		struct rtattr **xfrma)
15806c5c8ca7SJamal Hadi Salim {
15816c5c8ca7SJamal Hadi Salim 	struct xfrm_policy *xp;
15827b67c857SThomas Graf 	struct xfrm_user_polexpire *up = nlmsg_data(nlh);
15836c5c8ca7SJamal Hadi Salim 	struct xfrm_userpolicy_info *p = &up->pol;
1584b798a9edSJamal Hadi Salim 	u8 type = XFRM_POLICY_TYPE_MAIN;
15856c5c8ca7SJamal Hadi Salim 	int err = -ENOENT;
15866c5c8ca7SJamal Hadi Salim 
158722e70050SChristoph Hellwig 	err = copy_from_user_policy_type(&type, xfrma);
1588f7b6983fSMasahide NAKAMURA 	if (err)
1589f7b6983fSMasahide NAKAMURA 		return err;
1590f7b6983fSMasahide NAKAMURA 
15916c5c8ca7SJamal Hadi Salim 	if (p->index)
1592ef41aaa0SEric Paris 		xp = xfrm_policy_byid(type, p->dir, p->index, 0, &err);
15936c5c8ca7SJamal Hadi Salim 	else {
159422e70050SChristoph Hellwig 		struct rtattr *rt = xfrma[XFRMA_SEC_CTX-1];
15956c5c8ca7SJamal Hadi Salim 		struct xfrm_policy tmp;
15966c5c8ca7SJamal Hadi Salim 
159722e70050SChristoph Hellwig 		err = verify_sec_ctx_len(xfrma);
15986c5c8ca7SJamal Hadi Salim 		if (err)
15996c5c8ca7SJamal Hadi Salim 			return err;
16006c5c8ca7SJamal Hadi Salim 
16016c5c8ca7SJamal Hadi Salim 		memset(&tmp, 0, sizeof(struct xfrm_policy));
16026c5c8ca7SJamal Hadi Salim 		if (rt) {
16036c5c8ca7SJamal Hadi Salim 			struct xfrm_user_sec_ctx *uctx = RTA_DATA(rt);
16046c5c8ca7SJamal Hadi Salim 
16056c5c8ca7SJamal Hadi Salim 			if ((err = security_xfrm_policy_alloc(&tmp, uctx)))
16066c5c8ca7SJamal Hadi Salim 				return err;
16076c5c8ca7SJamal Hadi Salim 		}
1608ef41aaa0SEric Paris 		xp = xfrm_policy_bysel_ctx(type, p->dir, &p->sel, tmp.security,
1609ef41aaa0SEric Paris 					   0, &err);
16106c5c8ca7SJamal Hadi Salim 		security_xfrm_policy_free(&tmp);
16116c5c8ca7SJamal Hadi Salim 	}
16126c5c8ca7SJamal Hadi Salim 
16136c5c8ca7SJamal Hadi Salim 	if (xp == NULL)
1614ef41aaa0SEric Paris 		return -ENOENT;
16156c5c8ca7SJamal Hadi Salim 	read_lock(&xp->lock);
16166c5c8ca7SJamal Hadi Salim 	if (xp->dead) {
16176c5c8ca7SJamal Hadi Salim 		read_unlock(&xp->lock);
16186c5c8ca7SJamal Hadi Salim 		goto out;
16196c5c8ca7SJamal Hadi Salim 	}
16206c5c8ca7SJamal Hadi Salim 
16216c5c8ca7SJamal Hadi Salim 	read_unlock(&xp->lock);
16226c5c8ca7SJamal Hadi Salim 	err = 0;
16236c5c8ca7SJamal Hadi Salim 	if (up->hard) {
16246c5c8ca7SJamal Hadi Salim 		xfrm_policy_delete(xp, p->dir);
1625161a09e7SJoy Latten 		xfrm_audit_log(NETLINK_CB(skb).loginuid, NETLINK_CB(skb).sid,
1626161a09e7SJoy Latten 				AUDIT_MAC_IPSEC_DELSPD, 1, xp, NULL);
1627161a09e7SJoy Latten 
16286c5c8ca7SJamal Hadi Salim 	} else {
16296c5c8ca7SJamal Hadi Salim 		// reset the timers here?
16306c5c8ca7SJamal Hadi Salim 		printk("Dont know what to do with soft policy expire\n");
16316c5c8ca7SJamal Hadi Salim 	}
16326c5c8ca7SJamal Hadi Salim 	km_policy_expired(xp, p->dir, up->hard, current->pid);
16336c5c8ca7SJamal Hadi Salim 
16346c5c8ca7SJamal Hadi Salim out:
16356c5c8ca7SJamal Hadi Salim 	xfrm_pol_put(xp);
16366c5c8ca7SJamal Hadi Salim 	return err;
16376c5c8ca7SJamal Hadi Salim }
16386c5c8ca7SJamal Hadi Salim 
163922e70050SChristoph Hellwig static int xfrm_add_sa_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
164022e70050SChristoph Hellwig 		struct rtattr **xfrma)
164153bc6b4dSJamal Hadi Salim {
164253bc6b4dSJamal Hadi Salim 	struct xfrm_state *x;
164353bc6b4dSJamal Hadi Salim 	int err;
16447b67c857SThomas Graf 	struct xfrm_user_expire *ue = nlmsg_data(nlh);
164553bc6b4dSJamal Hadi Salim 	struct xfrm_usersa_info *p = &ue->state;
164653bc6b4dSJamal Hadi Salim 
164753bc6b4dSJamal Hadi Salim 	x = xfrm_state_lookup(&p->id.daddr, p->id.spi, p->id.proto, p->family);
164853bc6b4dSJamal Hadi Salim 
16493a765aa5SDavid S. Miller 	err = -ENOENT;
165053bc6b4dSJamal Hadi Salim 	if (x == NULL)
165153bc6b4dSJamal Hadi Salim 		return err;
165253bc6b4dSJamal Hadi Salim 
165353bc6b4dSJamal Hadi Salim 	spin_lock_bh(&x->lock);
16543a765aa5SDavid S. Miller 	err = -EINVAL;
165553bc6b4dSJamal Hadi Salim 	if (x->km.state != XFRM_STATE_VALID)
165653bc6b4dSJamal Hadi Salim 		goto out;
165753bc6b4dSJamal Hadi Salim 	km_state_expired(x, ue->hard, current->pid);
165853bc6b4dSJamal Hadi Salim 
1659161a09e7SJoy Latten 	if (ue->hard) {
166053bc6b4dSJamal Hadi Salim 		__xfrm_state_delete(x);
1661161a09e7SJoy Latten 		xfrm_audit_log(NETLINK_CB(skb).loginuid, NETLINK_CB(skb).sid,
1662161a09e7SJoy Latten 			       AUDIT_MAC_IPSEC_DELSA, 1, NULL, x);
1663161a09e7SJoy Latten 	}
16643a765aa5SDavid S. Miller 	err = 0;
166553bc6b4dSJamal Hadi Salim out:
166653bc6b4dSJamal Hadi Salim 	spin_unlock_bh(&x->lock);
166753bc6b4dSJamal Hadi Salim 	xfrm_state_put(x);
166853bc6b4dSJamal Hadi Salim 	return err;
166953bc6b4dSJamal Hadi Salim }
167053bc6b4dSJamal Hadi Salim 
167122e70050SChristoph Hellwig static int xfrm_add_acquire(struct sk_buff *skb, struct nlmsghdr *nlh,
167222e70050SChristoph Hellwig 		struct rtattr **xfrma)
1673980ebd25SJamal Hadi Salim {
1674980ebd25SJamal Hadi Salim 	struct xfrm_policy *xp;
1675980ebd25SJamal Hadi Salim 	struct xfrm_user_tmpl *ut;
1676980ebd25SJamal Hadi Salim 	int i;
1677980ebd25SJamal Hadi Salim 	struct rtattr *rt = xfrma[XFRMA_TMPL-1];
1678980ebd25SJamal Hadi Salim 
16797b67c857SThomas Graf 	struct xfrm_user_acquire *ua = nlmsg_data(nlh);
1680980ebd25SJamal Hadi Salim 	struct xfrm_state *x = xfrm_state_alloc();
1681980ebd25SJamal Hadi Salim 	int err = -ENOMEM;
1682980ebd25SJamal Hadi Salim 
1683980ebd25SJamal Hadi Salim 	if (!x)
1684980ebd25SJamal Hadi Salim 		return err;
1685980ebd25SJamal Hadi Salim 
1686980ebd25SJamal Hadi Salim 	err = verify_newpolicy_info(&ua->policy);
1687980ebd25SJamal Hadi Salim 	if (err) {
1688980ebd25SJamal Hadi Salim 		printk("BAD policy passed\n");
1689980ebd25SJamal Hadi Salim 		kfree(x);
1690980ebd25SJamal Hadi Salim 		return err;
1691980ebd25SJamal Hadi Salim 	}
1692980ebd25SJamal Hadi Salim 
1693980ebd25SJamal Hadi Salim 	/*   build an XP */
1694b4ad86bfSDavid S. Miller 	xp = xfrm_policy_construct(&ua->policy, (struct rtattr **) xfrma, &err);
1695b4ad86bfSDavid S. Miller 	if (!xp) {
1696980ebd25SJamal Hadi Salim 		kfree(x);
1697980ebd25SJamal Hadi Salim 		return err;
1698980ebd25SJamal Hadi Salim 	}
1699980ebd25SJamal Hadi Salim 
1700980ebd25SJamal Hadi Salim 	memcpy(&x->id, &ua->id, sizeof(ua->id));
1701980ebd25SJamal Hadi Salim 	memcpy(&x->props.saddr, &ua->saddr, sizeof(ua->saddr));
1702980ebd25SJamal Hadi Salim 	memcpy(&x->sel, &ua->sel, sizeof(ua->sel));
1703980ebd25SJamal Hadi Salim 
1704980ebd25SJamal Hadi Salim 	ut = RTA_DATA(rt);
1705980ebd25SJamal Hadi Salim 	/* extract the templates and for each call km_key */
1706980ebd25SJamal Hadi Salim 	for (i = 0; i < xp->xfrm_nr; i++, ut++) {
1707980ebd25SJamal Hadi Salim 		struct xfrm_tmpl *t = &xp->xfrm_vec[i];
1708980ebd25SJamal Hadi Salim 		memcpy(&x->id, &t->id, sizeof(x->id));
1709980ebd25SJamal Hadi Salim 		x->props.mode = t->mode;
1710980ebd25SJamal Hadi Salim 		x->props.reqid = t->reqid;
1711980ebd25SJamal Hadi Salim 		x->props.family = ut->family;
1712980ebd25SJamal Hadi Salim 		t->aalgos = ua->aalgos;
1713980ebd25SJamal Hadi Salim 		t->ealgos = ua->ealgos;
1714980ebd25SJamal Hadi Salim 		t->calgos = ua->calgos;
1715980ebd25SJamal Hadi Salim 		err = km_query(x, t, xp);
1716980ebd25SJamal Hadi Salim 
1717980ebd25SJamal Hadi Salim 	}
1718980ebd25SJamal Hadi Salim 
1719980ebd25SJamal Hadi Salim 	kfree(x);
1720980ebd25SJamal Hadi Salim 	kfree(xp);
1721980ebd25SJamal Hadi Salim 
1722980ebd25SJamal Hadi Salim 	return 0;
1723980ebd25SJamal Hadi Salim }
1724980ebd25SJamal Hadi Salim 
17255c79de6eSShinta Sugimoto #ifdef CONFIG_XFRM_MIGRATE
17265c79de6eSShinta Sugimoto static int verify_user_migrate(struct rtattr **xfrma)
17275c79de6eSShinta Sugimoto {
17285c79de6eSShinta Sugimoto 	struct rtattr *rt = xfrma[XFRMA_MIGRATE-1];
17295c79de6eSShinta Sugimoto 	struct xfrm_user_migrate *um;
17305c79de6eSShinta Sugimoto 
17315c79de6eSShinta Sugimoto 	if (!rt)
17325c79de6eSShinta Sugimoto 		return -EINVAL;
17335c79de6eSShinta Sugimoto 
17345c79de6eSShinta Sugimoto 	if ((rt->rta_len - sizeof(*rt)) < sizeof(*um))
17355c79de6eSShinta Sugimoto 		return -EINVAL;
17365c79de6eSShinta Sugimoto 
17375c79de6eSShinta Sugimoto 	return 0;
17385c79de6eSShinta Sugimoto }
17395c79de6eSShinta Sugimoto 
17405c79de6eSShinta Sugimoto static int copy_from_user_migrate(struct xfrm_migrate *ma,
17415c79de6eSShinta Sugimoto 				  struct rtattr **xfrma, int *num)
17425c79de6eSShinta Sugimoto {
17435c79de6eSShinta Sugimoto 	struct rtattr *rt = xfrma[XFRMA_MIGRATE-1];
17445c79de6eSShinta Sugimoto 	struct xfrm_user_migrate *um;
17455c79de6eSShinta Sugimoto 	int i, num_migrate;
17465c79de6eSShinta Sugimoto 
17475c79de6eSShinta Sugimoto 	um = RTA_DATA(rt);
17485c79de6eSShinta Sugimoto 	num_migrate = (rt->rta_len - sizeof(*rt)) / sizeof(*um);
17495c79de6eSShinta Sugimoto 
17505c79de6eSShinta Sugimoto 	if (num_migrate <= 0 || num_migrate > XFRM_MAX_DEPTH)
17515c79de6eSShinta Sugimoto 		return -EINVAL;
17525c79de6eSShinta Sugimoto 
17535c79de6eSShinta Sugimoto 	for (i = 0; i < num_migrate; i++, um++, ma++) {
17545c79de6eSShinta Sugimoto 		memcpy(&ma->old_daddr, &um->old_daddr, sizeof(ma->old_daddr));
17555c79de6eSShinta Sugimoto 		memcpy(&ma->old_saddr, &um->old_saddr, sizeof(ma->old_saddr));
17565c79de6eSShinta Sugimoto 		memcpy(&ma->new_daddr, &um->new_daddr, sizeof(ma->new_daddr));
17575c79de6eSShinta Sugimoto 		memcpy(&ma->new_saddr, &um->new_saddr, sizeof(ma->new_saddr));
17585c79de6eSShinta Sugimoto 
17595c79de6eSShinta Sugimoto 		ma->proto = um->proto;
17605c79de6eSShinta Sugimoto 		ma->mode = um->mode;
17615c79de6eSShinta Sugimoto 		ma->reqid = um->reqid;
17625c79de6eSShinta Sugimoto 
17635c79de6eSShinta Sugimoto 		ma->old_family = um->old_family;
17645c79de6eSShinta Sugimoto 		ma->new_family = um->new_family;
17655c79de6eSShinta Sugimoto 	}
17665c79de6eSShinta Sugimoto 
17675c79de6eSShinta Sugimoto 	*num = i;
17685c79de6eSShinta Sugimoto 	return 0;
17695c79de6eSShinta Sugimoto }
17705c79de6eSShinta Sugimoto 
17715c79de6eSShinta Sugimoto static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh,
17725c79de6eSShinta Sugimoto 			   struct rtattr **xfrma)
17735c79de6eSShinta Sugimoto {
17747b67c857SThomas Graf 	struct xfrm_userpolicy_id *pi = nlmsg_data(nlh);
17755c79de6eSShinta Sugimoto 	struct xfrm_migrate m[XFRM_MAX_DEPTH];
17765c79de6eSShinta Sugimoto 	u8 type;
17775c79de6eSShinta Sugimoto 	int err;
17785c79de6eSShinta Sugimoto 	int n = 0;
17795c79de6eSShinta Sugimoto 
17805c79de6eSShinta Sugimoto 	err = verify_user_migrate((struct rtattr **)xfrma);
17815c79de6eSShinta Sugimoto 	if (err)
17825c79de6eSShinta Sugimoto 		return err;
17835c79de6eSShinta Sugimoto 
17845c79de6eSShinta Sugimoto 	err = copy_from_user_policy_type(&type, (struct rtattr **)xfrma);
17855c79de6eSShinta Sugimoto 	if (err)
17865c79de6eSShinta Sugimoto 		return err;
17875c79de6eSShinta Sugimoto 
17885c79de6eSShinta Sugimoto 	err = copy_from_user_migrate((struct xfrm_migrate *)m,
17895c79de6eSShinta Sugimoto 				     (struct rtattr **)xfrma, &n);
17905c79de6eSShinta Sugimoto 	if (err)
17915c79de6eSShinta Sugimoto 		return err;
17925c79de6eSShinta Sugimoto 
17935c79de6eSShinta Sugimoto 	if (!n)
17945c79de6eSShinta Sugimoto 		return 0;
17955c79de6eSShinta Sugimoto 
17965c79de6eSShinta Sugimoto 	xfrm_migrate(&pi->sel, pi->dir, type, m, n);
17975c79de6eSShinta Sugimoto 
17985c79de6eSShinta Sugimoto 	return 0;
17995c79de6eSShinta Sugimoto }
18005c79de6eSShinta Sugimoto #else
18015c79de6eSShinta Sugimoto static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh,
18025c79de6eSShinta Sugimoto 			   struct rtattr **xfrma)
18035c79de6eSShinta Sugimoto {
18045c79de6eSShinta Sugimoto 	return -ENOPROTOOPT;
18055c79de6eSShinta Sugimoto }
18065c79de6eSShinta Sugimoto #endif
18075c79de6eSShinta Sugimoto 
18085c79de6eSShinta Sugimoto #ifdef CONFIG_XFRM_MIGRATE
18095c79de6eSShinta Sugimoto static int copy_to_user_migrate(struct xfrm_migrate *m, struct sk_buff *skb)
18105c79de6eSShinta Sugimoto {
18115c79de6eSShinta Sugimoto 	struct xfrm_user_migrate um;
18125c79de6eSShinta Sugimoto 
18135c79de6eSShinta Sugimoto 	memset(&um, 0, sizeof(um));
18145c79de6eSShinta Sugimoto 	um.proto = m->proto;
18155c79de6eSShinta Sugimoto 	um.mode = m->mode;
18165c79de6eSShinta Sugimoto 	um.reqid = m->reqid;
18175c79de6eSShinta Sugimoto 	um.old_family = m->old_family;
18185c79de6eSShinta Sugimoto 	memcpy(&um.old_daddr, &m->old_daddr, sizeof(um.old_daddr));
18195c79de6eSShinta Sugimoto 	memcpy(&um.old_saddr, &m->old_saddr, sizeof(um.old_saddr));
18205c79de6eSShinta Sugimoto 	um.new_family = m->new_family;
18215c79de6eSShinta Sugimoto 	memcpy(&um.new_daddr, &m->new_daddr, sizeof(um.new_daddr));
18225c79de6eSShinta Sugimoto 	memcpy(&um.new_saddr, &m->new_saddr, sizeof(um.new_saddr));
18235c79de6eSShinta Sugimoto 
1824c0144beaSThomas Graf 	return nla_put(skb, XFRMA_MIGRATE, sizeof(um), &um);
18255c79de6eSShinta Sugimoto }
18265c79de6eSShinta Sugimoto 
18275c79de6eSShinta Sugimoto static int build_migrate(struct sk_buff *skb, struct xfrm_migrate *m,
18285c79de6eSShinta Sugimoto 			 int num_migrate, struct xfrm_selector *sel,
18295c79de6eSShinta Sugimoto 			 u8 dir, u8 type)
18305c79de6eSShinta Sugimoto {
18315c79de6eSShinta Sugimoto 	struct xfrm_migrate *mp;
18325c79de6eSShinta Sugimoto 	struct xfrm_userpolicy_id *pol_id;
18335c79de6eSShinta Sugimoto 	struct nlmsghdr *nlh;
18345c79de6eSShinta Sugimoto 	int i;
18355c79de6eSShinta Sugimoto 
183679b8b7f4SThomas Graf 	nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_MIGRATE, sizeof(*pol_id), 0);
183779b8b7f4SThomas Graf 	if (nlh == NULL)
183879b8b7f4SThomas Graf 		return -EMSGSIZE;
18395c79de6eSShinta Sugimoto 
18407b67c857SThomas Graf 	pol_id = nlmsg_data(nlh);
18415c79de6eSShinta Sugimoto 	/* copy data from selector, dir, and type to the pol_id */
18425c79de6eSShinta Sugimoto 	memset(pol_id, 0, sizeof(*pol_id));
18435c79de6eSShinta Sugimoto 	memcpy(&pol_id->sel, sel, sizeof(pol_id->sel));
18445c79de6eSShinta Sugimoto 	pol_id->dir = dir;
18455c79de6eSShinta Sugimoto 
18465c79de6eSShinta Sugimoto 	if (copy_to_user_policy_type(type, skb) < 0)
18475c79de6eSShinta Sugimoto 		goto nlmsg_failure;
18485c79de6eSShinta Sugimoto 
18495c79de6eSShinta Sugimoto 	for (i = 0, mp = m ; i < num_migrate; i++, mp++) {
18505c79de6eSShinta Sugimoto 		if (copy_to_user_migrate(mp, skb) < 0)
18515c79de6eSShinta Sugimoto 			goto nlmsg_failure;
18525c79de6eSShinta Sugimoto 	}
18535c79de6eSShinta Sugimoto 
18549825069dSThomas Graf 	return nlmsg_end(skb, nlh);
18555c79de6eSShinta Sugimoto nlmsg_failure:
18569825069dSThomas Graf 	nlmsg_cancel(skb, nlh);
18579825069dSThomas Graf 	return -EMSGSIZE;
18585c79de6eSShinta Sugimoto }
18595c79de6eSShinta Sugimoto 
18605c79de6eSShinta Sugimoto static int xfrm_send_migrate(struct xfrm_selector *sel, u8 dir, u8 type,
18615c79de6eSShinta Sugimoto 			     struct xfrm_migrate *m, int num_migrate)
18625c79de6eSShinta Sugimoto {
18635c79de6eSShinta Sugimoto 	struct sk_buff *skb;
18645c79de6eSShinta Sugimoto 	size_t len;
18655c79de6eSShinta Sugimoto 
18665c79de6eSShinta Sugimoto 	len = RTA_SPACE(sizeof(struct xfrm_user_migrate) * num_migrate);
18675c79de6eSShinta Sugimoto 	len += NLMSG_SPACE(sizeof(struct xfrm_userpolicy_id));
1868*cfbfd45aSThomas Graf 	len += userpolicy_type_attrsize();
18695c79de6eSShinta Sugimoto 	skb = alloc_skb(len, GFP_ATOMIC);
18705c79de6eSShinta Sugimoto 	if (skb == NULL)
18715c79de6eSShinta Sugimoto 		return -ENOMEM;
18725c79de6eSShinta Sugimoto 
18735c79de6eSShinta Sugimoto 	/* build migrate */
18745c79de6eSShinta Sugimoto 	if (build_migrate(skb, m, num_migrate, sel, dir, type) < 0)
18755c79de6eSShinta Sugimoto 		BUG();
18765c79de6eSShinta Sugimoto 
1877082a1ad5SThomas Graf 	return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_MIGRATE, GFP_ATOMIC);
18785c79de6eSShinta Sugimoto }
18795c79de6eSShinta Sugimoto #else
18805c79de6eSShinta Sugimoto static int xfrm_send_migrate(struct xfrm_selector *sel, u8 dir, u8 type,
18815c79de6eSShinta Sugimoto 			     struct xfrm_migrate *m, int num_migrate)
18825c79de6eSShinta Sugimoto {
18835c79de6eSShinta Sugimoto 	return -ENOPROTOOPT;
18845c79de6eSShinta Sugimoto }
18855c79de6eSShinta Sugimoto #endif
1886d51d081dSJamal Hadi Salim 
1887492b558bSThomas Graf #define XMSGSIZE(type) NLMSG_LENGTH(sizeof(struct type))
1888492b558bSThomas Graf 
1889492b558bSThomas Graf static const int xfrm_msg_min[XFRM_NR_MSGTYPES] = {
1890492b558bSThomas Graf 	[XFRM_MSG_NEWSA       - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
1891492b558bSThomas Graf 	[XFRM_MSG_DELSA       - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
1892492b558bSThomas Graf 	[XFRM_MSG_GETSA       - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
1893492b558bSThomas Graf 	[XFRM_MSG_NEWPOLICY   - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
1894492b558bSThomas Graf 	[XFRM_MSG_DELPOLICY   - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
1895492b558bSThomas Graf 	[XFRM_MSG_GETPOLICY   - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
1896492b558bSThomas Graf 	[XFRM_MSG_ALLOCSPI    - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userspi_info),
1897980ebd25SJamal Hadi Salim 	[XFRM_MSG_ACQUIRE     - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_acquire),
189853bc6b4dSJamal Hadi Salim 	[XFRM_MSG_EXPIRE      - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_expire),
1899492b558bSThomas Graf 	[XFRM_MSG_UPDPOLICY   - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
1900492b558bSThomas Graf 	[XFRM_MSG_UPDSA       - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
19016c5c8ca7SJamal Hadi Salim 	[XFRM_MSG_POLEXPIRE   - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_polexpire),
1902492b558bSThomas Graf 	[XFRM_MSG_FLUSHSA     - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_flush),
1903492b558bSThomas Graf 	[XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = NLMSG_LENGTH(0),
1904d51d081dSJamal Hadi Salim 	[XFRM_MSG_NEWAE       - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
1905d51d081dSJamal Hadi Salim 	[XFRM_MSG_GETAE       - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
190697a64b45SMasahide NAKAMURA 	[XFRM_MSG_REPORT      - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_report),
19075c79de6eSShinta Sugimoto 	[XFRM_MSG_MIGRATE     - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
1908566ec034SJamal Hadi Salim 	[XFRM_MSG_GETSADINFO  - XFRM_MSG_BASE] = NLMSG_LENGTH(sizeof(u32)),
1909ecfd6b18SJamal Hadi Salim 	[XFRM_MSG_GETSPDINFO  - XFRM_MSG_BASE] = NLMSG_LENGTH(sizeof(u32)),
19101da177e4SLinus Torvalds };
19111da177e4SLinus Torvalds 
1912492b558bSThomas Graf #undef XMSGSIZE
1913492b558bSThomas Graf 
19141da177e4SLinus Torvalds static struct xfrm_link {
191522e70050SChristoph Hellwig 	int (*doit)(struct sk_buff *, struct nlmsghdr *, struct rtattr **);
19161da177e4SLinus Torvalds 	int (*dump)(struct sk_buff *, struct netlink_callback *);
1917492b558bSThomas Graf } xfrm_dispatch[XFRM_NR_MSGTYPES] = {
1918492b558bSThomas Graf 	[XFRM_MSG_NEWSA       - XFRM_MSG_BASE] = { .doit = xfrm_add_sa        },
1919492b558bSThomas Graf 	[XFRM_MSG_DELSA       - XFRM_MSG_BASE] = { .doit = xfrm_del_sa        },
1920492b558bSThomas Graf 	[XFRM_MSG_GETSA       - XFRM_MSG_BASE] = { .doit = xfrm_get_sa,
1921492b558bSThomas Graf 						   .dump = xfrm_dump_sa       },
1922492b558bSThomas Graf 	[XFRM_MSG_NEWPOLICY   - XFRM_MSG_BASE] = { .doit = xfrm_add_policy    },
1923492b558bSThomas Graf 	[XFRM_MSG_DELPOLICY   - XFRM_MSG_BASE] = { .doit = xfrm_get_policy    },
1924492b558bSThomas Graf 	[XFRM_MSG_GETPOLICY   - XFRM_MSG_BASE] = { .doit = xfrm_get_policy,
1925492b558bSThomas Graf 						   .dump = xfrm_dump_policy   },
1926492b558bSThomas Graf 	[XFRM_MSG_ALLOCSPI    - XFRM_MSG_BASE] = { .doit = xfrm_alloc_userspi },
1927980ebd25SJamal Hadi Salim 	[XFRM_MSG_ACQUIRE     - XFRM_MSG_BASE] = { .doit = xfrm_add_acquire   },
192853bc6b4dSJamal Hadi Salim 	[XFRM_MSG_EXPIRE      - XFRM_MSG_BASE] = { .doit = xfrm_add_sa_expire },
1929492b558bSThomas Graf 	[XFRM_MSG_UPDPOLICY   - XFRM_MSG_BASE] = { .doit = xfrm_add_policy    },
1930492b558bSThomas Graf 	[XFRM_MSG_UPDSA       - XFRM_MSG_BASE] = { .doit = xfrm_add_sa        },
19316c5c8ca7SJamal Hadi Salim 	[XFRM_MSG_POLEXPIRE   - XFRM_MSG_BASE] = { .doit = xfrm_add_pol_expire},
1932492b558bSThomas Graf 	[XFRM_MSG_FLUSHSA     - XFRM_MSG_BASE] = { .doit = xfrm_flush_sa      },
1933492b558bSThomas Graf 	[XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_flush_policy  },
1934d51d081dSJamal Hadi Salim 	[XFRM_MSG_NEWAE       - XFRM_MSG_BASE] = { .doit = xfrm_new_ae  },
1935d51d081dSJamal Hadi Salim 	[XFRM_MSG_GETAE       - XFRM_MSG_BASE] = { .doit = xfrm_get_ae  },
19365c79de6eSShinta Sugimoto 	[XFRM_MSG_MIGRATE     - XFRM_MSG_BASE] = { .doit = xfrm_do_migrate    },
193728d8909bSJamal Hadi Salim 	[XFRM_MSG_GETSADINFO  - XFRM_MSG_BASE] = { .doit = xfrm_get_sadinfo   },
1938ecfd6b18SJamal Hadi Salim 	[XFRM_MSG_GETSPDINFO  - XFRM_MSG_BASE] = { .doit = xfrm_get_spdinfo   },
19391da177e4SLinus Torvalds };
19401da177e4SLinus Torvalds 
19411d00a4ebSThomas Graf static int xfrm_user_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
19421da177e4SLinus Torvalds {
19431da177e4SLinus Torvalds 	struct rtattr *xfrma[XFRMA_MAX];
19441da177e4SLinus Torvalds 	struct xfrm_link *link;
1945c702e804SThomas Graf 	int type, min_len;
19461da177e4SLinus Torvalds 
19471da177e4SLinus Torvalds 	type = nlh->nlmsg_type;
19481da177e4SLinus Torvalds 	if (type > XFRM_MSG_MAX)
19491d00a4ebSThomas Graf 		return -EINVAL;
19501da177e4SLinus Torvalds 
19511da177e4SLinus Torvalds 	type -= XFRM_MSG_BASE;
19521da177e4SLinus Torvalds 	link = &xfrm_dispatch[type];
19531da177e4SLinus Torvalds 
19541da177e4SLinus Torvalds 	/* All operations require privileges, even GET */
19551d00a4ebSThomas Graf 	if (security_netlink_recv(skb, CAP_NET_ADMIN))
19561d00a4ebSThomas Graf 		return -EPERM;
19571da177e4SLinus Torvalds 
1958492b558bSThomas Graf 	if ((type == (XFRM_MSG_GETSA - XFRM_MSG_BASE) ||
1959492b558bSThomas Graf 	     type == (XFRM_MSG_GETPOLICY - XFRM_MSG_BASE)) &&
1960492b558bSThomas Graf 	    (nlh->nlmsg_flags & NLM_F_DUMP)) {
19611da177e4SLinus Torvalds 		if (link->dump == NULL)
19621d00a4ebSThomas Graf 			return -EINVAL;
19631da177e4SLinus Torvalds 
1964c702e804SThomas Graf 		return netlink_dump_start(xfrm_nl, skb, nlh, link->dump, NULL);
19651da177e4SLinus Torvalds 	}
19661da177e4SLinus Torvalds 
19671da177e4SLinus Torvalds 	memset(xfrma, 0, sizeof(xfrma));
19681da177e4SLinus Torvalds 
19691da177e4SLinus Torvalds 	if (nlh->nlmsg_len < (min_len = xfrm_msg_min[type]))
19701d00a4ebSThomas Graf 		return -EINVAL;
19711da177e4SLinus Torvalds 
19721da177e4SLinus Torvalds 	if (nlh->nlmsg_len > min_len) {
19731da177e4SLinus Torvalds 		int attrlen = nlh->nlmsg_len - NLMSG_ALIGN(min_len);
19741da177e4SLinus Torvalds 		struct rtattr *attr = (void *) nlh + NLMSG_ALIGN(min_len);
19751da177e4SLinus Torvalds 
19761da177e4SLinus Torvalds 		while (RTA_OK(attr, attrlen)) {
19771da177e4SLinus Torvalds 			unsigned short flavor = attr->rta_type;
19781da177e4SLinus Torvalds 			if (flavor) {
19791da177e4SLinus Torvalds 				if (flavor > XFRMA_MAX)
19801d00a4ebSThomas Graf 					return -EINVAL;
19811da177e4SLinus Torvalds 				xfrma[flavor - 1] = attr;
19821da177e4SLinus Torvalds 			}
19831da177e4SLinus Torvalds 			attr = RTA_NEXT(attr, attrlen);
19841da177e4SLinus Torvalds 		}
19851da177e4SLinus Torvalds 	}
19861da177e4SLinus Torvalds 
19871da177e4SLinus Torvalds 	if (link->doit == NULL)
19881d00a4ebSThomas Graf 		return -EINVAL;
19891da177e4SLinus Torvalds 
19901d00a4ebSThomas Graf 	return link->doit(skb, nlh, xfrma);
19911da177e4SLinus Torvalds }
19921da177e4SLinus Torvalds 
19931da177e4SLinus Torvalds static void xfrm_netlink_rcv(struct sock *sk, int len)
19941da177e4SLinus Torvalds {
199588fc2c84SThomas Graf 	unsigned int qlen = 0;
19962a0a6ebeSHerbert Xu 
19971da177e4SLinus Torvalds 	do {
19984a3e2f71SArjan van de Ven 		mutex_lock(&xfrm_cfg_mutex);
199988fc2c84SThomas Graf 		netlink_run_queue(sk, &qlen, &xfrm_user_rcv_msg);
20004a3e2f71SArjan van de Ven 		mutex_unlock(&xfrm_cfg_mutex);
20011da177e4SLinus Torvalds 
20022a0a6ebeSHerbert Xu 	} while (qlen);
20031da177e4SLinus Torvalds }
20041da177e4SLinus Torvalds 
2005d51d081dSJamal Hadi Salim static int build_expire(struct sk_buff *skb, struct xfrm_state *x, struct km_event *c)
20061da177e4SLinus Torvalds {
20071da177e4SLinus Torvalds 	struct xfrm_user_expire *ue;
20081da177e4SLinus Torvalds 	struct nlmsghdr *nlh;
20091da177e4SLinus Torvalds 
201079b8b7f4SThomas Graf 	nlh = nlmsg_put(skb, c->pid, 0, XFRM_MSG_EXPIRE, sizeof(*ue), 0);
201179b8b7f4SThomas Graf 	if (nlh == NULL)
201279b8b7f4SThomas Graf 		return -EMSGSIZE;
20131da177e4SLinus Torvalds 
20147b67c857SThomas Graf 	ue = nlmsg_data(nlh);
20151da177e4SLinus Torvalds 	copy_to_user_state(x, &ue->state);
2016d51d081dSJamal Hadi Salim 	ue->hard = (c->data.hard != 0) ? 1 : 0;
20171da177e4SLinus Torvalds 
20189825069dSThomas Graf 	return nlmsg_end(skb, nlh);
20191da177e4SLinus Torvalds }
20201da177e4SLinus Torvalds 
202126b15dadSJamal Hadi Salim static int xfrm_exp_state_notify(struct xfrm_state *x, struct km_event *c)
20221da177e4SLinus Torvalds {
20231da177e4SLinus Torvalds 	struct sk_buff *skb;
2024ee57eef9SJamal Hadi Salim 	int len = NLMSG_LENGTH(sizeof(struct xfrm_user_expire));
20251da177e4SLinus Torvalds 
2026ee57eef9SJamal Hadi Salim 	skb = alloc_skb(len, GFP_ATOMIC);
20271da177e4SLinus Torvalds 	if (skb == NULL)
20281da177e4SLinus Torvalds 		return -ENOMEM;
20291da177e4SLinus Torvalds 
2030d51d081dSJamal Hadi Salim 	if (build_expire(skb, x, c) < 0)
20311da177e4SLinus Torvalds 		BUG();
20321da177e4SLinus Torvalds 
2033082a1ad5SThomas Graf 	return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_EXPIRE, GFP_ATOMIC);
20341da177e4SLinus Torvalds }
20351da177e4SLinus Torvalds 
2036d51d081dSJamal Hadi Salim static int xfrm_aevent_state_notify(struct xfrm_state *x, struct km_event *c)
2037d51d081dSJamal Hadi Salim {
2038d51d081dSJamal Hadi Salim 	struct sk_buff *skb;
2039d51d081dSJamal Hadi Salim 	int len = NLMSG_LENGTH(sizeof(struct xfrm_aevent_id));
2040d51d081dSJamal Hadi Salim 
2041d51d081dSJamal Hadi Salim 	len += RTA_SPACE(sizeof(struct xfrm_replay_state));
2042d51d081dSJamal Hadi Salim 	len += RTA_SPACE(sizeof(struct xfrm_lifetime_cur));
2043d51d081dSJamal Hadi Salim 	skb = alloc_skb(len, GFP_ATOMIC);
2044d51d081dSJamal Hadi Salim 	if (skb == NULL)
2045d51d081dSJamal Hadi Salim 		return -ENOMEM;
2046d51d081dSJamal Hadi Salim 
2047d51d081dSJamal Hadi Salim 	if (build_aevent(skb, x, c) < 0)
2048d51d081dSJamal Hadi Salim 		BUG();
2049d51d081dSJamal Hadi Salim 
2050082a1ad5SThomas Graf 	return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_AEVENTS, GFP_ATOMIC);
2051d51d081dSJamal Hadi Salim }
2052d51d081dSJamal Hadi Salim 
205326b15dadSJamal Hadi Salim static int xfrm_notify_sa_flush(struct km_event *c)
205426b15dadSJamal Hadi Salim {
205526b15dadSJamal Hadi Salim 	struct xfrm_usersa_flush *p;
205626b15dadSJamal Hadi Salim 	struct nlmsghdr *nlh;
205726b15dadSJamal Hadi Salim 	struct sk_buff *skb;
205826b15dadSJamal Hadi Salim 	int len = NLMSG_LENGTH(sizeof(struct xfrm_usersa_flush));
205926b15dadSJamal Hadi Salim 
206026b15dadSJamal Hadi Salim 	skb = alloc_skb(len, GFP_ATOMIC);
206126b15dadSJamal Hadi Salim 	if (skb == NULL)
206226b15dadSJamal Hadi Salim 		return -ENOMEM;
206326b15dadSJamal Hadi Salim 
206479b8b7f4SThomas Graf 	nlh = nlmsg_put(skb, c->pid, c->seq, XFRM_MSG_FLUSHSA, sizeof(*p), 0);
206579b8b7f4SThomas Graf 	if (nlh == NULL) {
206679b8b7f4SThomas Graf 		kfree_skb(skb);
206779b8b7f4SThomas Graf 		return -EMSGSIZE;
206879b8b7f4SThomas Graf 	}
206926b15dadSJamal Hadi Salim 
20707b67c857SThomas Graf 	p = nlmsg_data(nlh);
2071bf08867fSHerbert Xu 	p->proto = c->data.proto;
207226b15dadSJamal Hadi Salim 
20739825069dSThomas Graf 	nlmsg_end(skb, nlh);
207426b15dadSJamal Hadi Salim 
2075082a1ad5SThomas Graf 	return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_SA, GFP_ATOMIC);
207626b15dadSJamal Hadi Salim }
207726b15dadSJamal Hadi Salim 
2078b6f99a21SDave Jones static inline int xfrm_sa_len(struct xfrm_state *x)
207926b15dadSJamal Hadi Salim {
20800603eac0SHerbert Xu 	int l = 0;
208126b15dadSJamal Hadi Salim 	if (x->aalg)
2082c26445acSThomas Graf 		l += RTA_SPACE(alg_len(x->aalg));
208326b15dadSJamal Hadi Salim 	if (x->ealg)
2084c26445acSThomas Graf 		l += RTA_SPACE(alg_len(x->ealg));
208526b15dadSJamal Hadi Salim 	if (x->calg)
208626b15dadSJamal Hadi Salim 		l += RTA_SPACE(sizeof(*x->calg));
208726b15dadSJamal Hadi Salim 	if (x->encap)
208826b15dadSJamal Hadi Salim 		l += RTA_SPACE(sizeof(*x->encap));
208926b15dadSJamal Hadi Salim 
209026b15dadSJamal Hadi Salim 	return l;
209126b15dadSJamal Hadi Salim }
209226b15dadSJamal Hadi Salim 
209326b15dadSJamal Hadi Salim static int xfrm_notify_sa(struct xfrm_state *x, struct km_event *c)
209426b15dadSJamal Hadi Salim {
209526b15dadSJamal Hadi Salim 	struct xfrm_usersa_info *p;
20960603eac0SHerbert Xu 	struct xfrm_usersa_id *id;
209726b15dadSJamal Hadi Salim 	struct nlmsghdr *nlh;
209826b15dadSJamal Hadi Salim 	struct sk_buff *skb;
209926b15dadSJamal Hadi Salim 	int len = xfrm_sa_len(x);
21000603eac0SHerbert Xu 	int headlen;
21010603eac0SHerbert Xu 
21020603eac0SHerbert Xu 	headlen = sizeof(*p);
21030603eac0SHerbert Xu 	if (c->event == XFRM_MSG_DELSA) {
21040603eac0SHerbert Xu 		len += RTA_SPACE(headlen);
21050603eac0SHerbert Xu 		headlen = sizeof(*id);
21060603eac0SHerbert Xu 	}
21070603eac0SHerbert Xu 	len += NLMSG_SPACE(headlen);
210826b15dadSJamal Hadi Salim 
210926b15dadSJamal Hadi Salim 	skb = alloc_skb(len, GFP_ATOMIC);
211026b15dadSJamal Hadi Salim 	if (skb == NULL)
211126b15dadSJamal Hadi Salim 		return -ENOMEM;
211226b15dadSJamal Hadi Salim 
211379b8b7f4SThomas Graf 	nlh = nlmsg_put(skb, c->pid, c->seq, c->event, headlen, 0);
211479b8b7f4SThomas Graf 	if (nlh == NULL)
2115c0144beaSThomas Graf 		goto nla_put_failure;
211626b15dadSJamal Hadi Salim 
21177b67c857SThomas Graf 	p = nlmsg_data(nlh);
21180603eac0SHerbert Xu 	if (c->event == XFRM_MSG_DELSA) {
2119c0144beaSThomas Graf 		struct nlattr *attr;
2120c0144beaSThomas Graf 
21217b67c857SThomas Graf 		id = nlmsg_data(nlh);
21220603eac0SHerbert Xu 		memcpy(&id->daddr, &x->id.daddr, sizeof(id->daddr));
21230603eac0SHerbert Xu 		id->spi = x->id.spi;
21240603eac0SHerbert Xu 		id->family = x->props.family;
21250603eac0SHerbert Xu 		id->proto = x->id.proto;
21260603eac0SHerbert Xu 
2127c0144beaSThomas Graf 		attr = nla_reserve(skb, XFRMA_SA, sizeof(*p));
2128c0144beaSThomas Graf 		if (attr == NULL)
2129c0144beaSThomas Graf 			goto nla_put_failure;
2130c0144beaSThomas Graf 
2131c0144beaSThomas Graf 		p = nla_data(attr);
21320603eac0SHerbert Xu 	}
21330603eac0SHerbert Xu 
213426b15dadSJamal Hadi Salim 	copy_to_user_state(x, p);
213526b15dadSJamal Hadi Salim 
213626b15dadSJamal Hadi Salim 	if (x->aalg)
2137c26445acSThomas Graf 		NLA_PUT(skb, XFRMA_ALG_AUTH, alg_len(x->aalg), x->aalg);
213826b15dadSJamal Hadi Salim 	if (x->ealg)
2139c26445acSThomas Graf 		NLA_PUT(skb, XFRMA_ALG_CRYPT, alg_len(x->ealg), x->ealg);
214026b15dadSJamal Hadi Salim 	if (x->calg)
2141c0144beaSThomas Graf 		NLA_PUT(skb, XFRMA_ALG_COMP, sizeof(*(x->calg)), x->calg);
214226b15dadSJamal Hadi Salim 
214326b15dadSJamal Hadi Salim 	if (x->encap)
2144c0144beaSThomas Graf 		NLA_PUT(skb, XFRMA_ENCAP, sizeof(*x->encap), x->encap);
214526b15dadSJamal Hadi Salim 
21469825069dSThomas Graf 	nlmsg_end(skb, nlh);
214726b15dadSJamal Hadi Salim 
2148082a1ad5SThomas Graf 	return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_SA, GFP_ATOMIC);
214926b15dadSJamal Hadi Salim 
2150c0144beaSThomas Graf nla_put_failure:
215126b15dadSJamal Hadi Salim 	kfree_skb(skb);
215226b15dadSJamal Hadi Salim 	return -1;
215326b15dadSJamal Hadi Salim }
215426b15dadSJamal Hadi Salim 
215526b15dadSJamal Hadi Salim static int xfrm_send_state_notify(struct xfrm_state *x, struct km_event *c)
215626b15dadSJamal Hadi Salim {
215726b15dadSJamal Hadi Salim 
215826b15dadSJamal Hadi Salim 	switch (c->event) {
2159f60f6b8fSHerbert Xu 	case XFRM_MSG_EXPIRE:
216026b15dadSJamal Hadi Salim 		return xfrm_exp_state_notify(x, c);
2161d51d081dSJamal Hadi Salim 	case XFRM_MSG_NEWAE:
2162d51d081dSJamal Hadi Salim 		return xfrm_aevent_state_notify(x, c);
2163f60f6b8fSHerbert Xu 	case XFRM_MSG_DELSA:
2164f60f6b8fSHerbert Xu 	case XFRM_MSG_UPDSA:
2165f60f6b8fSHerbert Xu 	case XFRM_MSG_NEWSA:
216626b15dadSJamal Hadi Salim 		return xfrm_notify_sa(x, c);
2167f60f6b8fSHerbert Xu 	case XFRM_MSG_FLUSHSA:
216826b15dadSJamal Hadi Salim 		return xfrm_notify_sa_flush(c);
216926b15dadSJamal Hadi Salim 	default:
217026b15dadSJamal Hadi Salim 		 printk("xfrm_user: Unknown SA event %d\n", c->event);
217126b15dadSJamal Hadi Salim 		 break;
217226b15dadSJamal Hadi Salim 	}
217326b15dadSJamal Hadi Salim 
217426b15dadSJamal Hadi Salim 	return 0;
217526b15dadSJamal Hadi Salim 
217626b15dadSJamal Hadi Salim }
217726b15dadSJamal Hadi Salim 
21781da177e4SLinus Torvalds static int build_acquire(struct sk_buff *skb, struct xfrm_state *x,
21791da177e4SLinus Torvalds 			 struct xfrm_tmpl *xt, struct xfrm_policy *xp,
21801da177e4SLinus Torvalds 			 int dir)
21811da177e4SLinus Torvalds {
21821da177e4SLinus Torvalds 	struct xfrm_user_acquire *ua;
21831da177e4SLinus Torvalds 	struct nlmsghdr *nlh;
21841da177e4SLinus Torvalds 	__u32 seq = xfrm_get_acqseq();
21851da177e4SLinus Torvalds 
218679b8b7f4SThomas Graf 	nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_ACQUIRE, sizeof(*ua), 0);
218779b8b7f4SThomas Graf 	if (nlh == NULL)
218879b8b7f4SThomas Graf 		return -EMSGSIZE;
21891da177e4SLinus Torvalds 
21907b67c857SThomas Graf 	ua = nlmsg_data(nlh);
21911da177e4SLinus Torvalds 	memcpy(&ua->id, &x->id, sizeof(ua->id));
21921da177e4SLinus Torvalds 	memcpy(&ua->saddr, &x->props.saddr, sizeof(ua->saddr));
21931da177e4SLinus Torvalds 	memcpy(&ua->sel, &x->sel, sizeof(ua->sel));
21941da177e4SLinus Torvalds 	copy_to_user_policy(xp, &ua->policy, dir);
21951da177e4SLinus Torvalds 	ua->aalgos = xt->aalgos;
21961da177e4SLinus Torvalds 	ua->ealgos = xt->ealgos;
21971da177e4SLinus Torvalds 	ua->calgos = xt->calgos;
21981da177e4SLinus Torvalds 	ua->seq = x->km.seq = seq;
21991da177e4SLinus Torvalds 
22001da177e4SLinus Torvalds 	if (copy_to_user_tmpl(xp, skb) < 0)
22011da177e4SLinus Torvalds 		goto nlmsg_failure;
22020d681623SSerge Hallyn 	if (copy_to_user_state_sec_ctx(x, skb))
2203df71837dSTrent Jaeger 		goto nlmsg_failure;
22041459bb36SJamal Hadi Salim 	if (copy_to_user_policy_type(xp->type, skb) < 0)
2205f7b6983fSMasahide NAKAMURA 		goto nlmsg_failure;
22061da177e4SLinus Torvalds 
22079825069dSThomas Graf 	return nlmsg_end(skb, nlh);
22081da177e4SLinus Torvalds 
22091da177e4SLinus Torvalds nlmsg_failure:
22109825069dSThomas Graf 	nlmsg_cancel(skb, nlh);
22119825069dSThomas Graf 	return -EMSGSIZE;
22121da177e4SLinus Torvalds }
22131da177e4SLinus Torvalds 
22141da177e4SLinus Torvalds static int xfrm_send_acquire(struct xfrm_state *x, struct xfrm_tmpl *xt,
22151da177e4SLinus Torvalds 			     struct xfrm_policy *xp, int dir)
22161da177e4SLinus Torvalds {
22171da177e4SLinus Torvalds 	struct sk_buff *skb;
22181da177e4SLinus Torvalds 	size_t len;
22191da177e4SLinus Torvalds 
22201da177e4SLinus Torvalds 	len = RTA_SPACE(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr);
22211da177e4SLinus Torvalds 	len += NLMSG_SPACE(sizeof(struct xfrm_user_acquire));
2222661697f7SJoy Latten 	len += RTA_SPACE(xfrm_user_sec_ctx_size(x->security));
2223*cfbfd45aSThomas Graf 	len += userpolicy_type_attrsize();
22241da177e4SLinus Torvalds 	skb = alloc_skb(len, GFP_ATOMIC);
22251da177e4SLinus Torvalds 	if (skb == NULL)
22261da177e4SLinus Torvalds 		return -ENOMEM;
22271da177e4SLinus Torvalds 
22281da177e4SLinus Torvalds 	if (build_acquire(skb, x, xt, xp, dir) < 0)
22291da177e4SLinus Torvalds 		BUG();
22301da177e4SLinus Torvalds 
2231082a1ad5SThomas Graf 	return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_ACQUIRE, GFP_ATOMIC);
22321da177e4SLinus Torvalds }
22331da177e4SLinus Torvalds 
22341da177e4SLinus Torvalds /* User gives us xfrm_user_policy_info followed by an array of 0
22351da177e4SLinus Torvalds  * or more templates.
22361da177e4SLinus Torvalds  */
2237cb969f07SVenkat Yekkirala static struct xfrm_policy *xfrm_compile_policy(struct sock *sk, int opt,
22381da177e4SLinus Torvalds 					       u8 *data, int len, int *dir)
22391da177e4SLinus Torvalds {
22401da177e4SLinus Torvalds 	struct xfrm_userpolicy_info *p = (struct xfrm_userpolicy_info *)data;
22411da177e4SLinus Torvalds 	struct xfrm_user_tmpl *ut = (struct xfrm_user_tmpl *) (p + 1);
22421da177e4SLinus Torvalds 	struct xfrm_policy *xp;
22431da177e4SLinus Torvalds 	int nr;
22441da177e4SLinus Torvalds 
2245cb969f07SVenkat Yekkirala 	switch (sk->sk_family) {
22461da177e4SLinus Torvalds 	case AF_INET:
22471da177e4SLinus Torvalds 		if (opt != IP_XFRM_POLICY) {
22481da177e4SLinus Torvalds 			*dir = -EOPNOTSUPP;
22491da177e4SLinus Torvalds 			return NULL;
22501da177e4SLinus Torvalds 		}
22511da177e4SLinus Torvalds 		break;
22521da177e4SLinus Torvalds #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
22531da177e4SLinus Torvalds 	case AF_INET6:
22541da177e4SLinus Torvalds 		if (opt != IPV6_XFRM_POLICY) {
22551da177e4SLinus Torvalds 			*dir = -EOPNOTSUPP;
22561da177e4SLinus Torvalds 			return NULL;
22571da177e4SLinus Torvalds 		}
22581da177e4SLinus Torvalds 		break;
22591da177e4SLinus Torvalds #endif
22601da177e4SLinus Torvalds 	default:
22611da177e4SLinus Torvalds 		*dir = -EINVAL;
22621da177e4SLinus Torvalds 		return NULL;
22631da177e4SLinus Torvalds 	}
22641da177e4SLinus Torvalds 
22651da177e4SLinus Torvalds 	*dir = -EINVAL;
22661da177e4SLinus Torvalds 
22671da177e4SLinus Torvalds 	if (len < sizeof(*p) ||
22681da177e4SLinus Torvalds 	    verify_newpolicy_info(p))
22691da177e4SLinus Torvalds 		return NULL;
22701da177e4SLinus Torvalds 
22711da177e4SLinus Torvalds 	nr = ((len - sizeof(*p)) / sizeof(*ut));
2272b4ad86bfSDavid S. Miller 	if (validate_tmpl(nr, ut, p->sel.family))
22731da177e4SLinus Torvalds 		return NULL;
22741da177e4SLinus Torvalds 
2275a4f1bac6SHerbert Xu 	if (p->dir > XFRM_POLICY_OUT)
2276a4f1bac6SHerbert Xu 		return NULL;
2277a4f1bac6SHerbert Xu 
22781da177e4SLinus Torvalds 	xp = xfrm_policy_alloc(GFP_KERNEL);
22791da177e4SLinus Torvalds 	if (xp == NULL) {
22801da177e4SLinus Torvalds 		*dir = -ENOBUFS;
22811da177e4SLinus Torvalds 		return NULL;
22821da177e4SLinus Torvalds 	}
22831da177e4SLinus Torvalds 
22841da177e4SLinus Torvalds 	copy_from_user_policy(xp, p);
2285f7b6983fSMasahide NAKAMURA 	xp->type = XFRM_POLICY_TYPE_MAIN;
22861da177e4SLinus Torvalds 	copy_templates(xp, ut, nr);
22871da177e4SLinus Torvalds 
22881da177e4SLinus Torvalds 	*dir = p->dir;
22891da177e4SLinus Torvalds 
22901da177e4SLinus Torvalds 	return xp;
22911da177e4SLinus Torvalds }
22921da177e4SLinus Torvalds 
22931da177e4SLinus Torvalds static int build_polexpire(struct sk_buff *skb, struct xfrm_policy *xp,
2294d51d081dSJamal Hadi Salim 			   int dir, struct km_event *c)
22951da177e4SLinus Torvalds {
22961da177e4SLinus Torvalds 	struct xfrm_user_polexpire *upe;
22971da177e4SLinus Torvalds 	struct nlmsghdr *nlh;
2298d51d081dSJamal Hadi Salim 	int hard = c->data.hard;
22991da177e4SLinus Torvalds 
230079b8b7f4SThomas Graf 	nlh = nlmsg_put(skb, c->pid, 0, XFRM_MSG_POLEXPIRE, sizeof(*upe), 0);
230179b8b7f4SThomas Graf 	if (nlh == NULL)
230279b8b7f4SThomas Graf 		return -EMSGSIZE;
23031da177e4SLinus Torvalds 
23047b67c857SThomas Graf 	upe = nlmsg_data(nlh);
23051da177e4SLinus Torvalds 	copy_to_user_policy(xp, &upe->pol, dir);
23061da177e4SLinus Torvalds 	if (copy_to_user_tmpl(xp, skb) < 0)
23071da177e4SLinus Torvalds 		goto nlmsg_failure;
2308df71837dSTrent Jaeger 	if (copy_to_user_sec_ctx(xp, skb))
2309df71837dSTrent Jaeger 		goto nlmsg_failure;
23101459bb36SJamal Hadi Salim 	if (copy_to_user_policy_type(xp->type, skb) < 0)
2311f7b6983fSMasahide NAKAMURA 		goto nlmsg_failure;
23121da177e4SLinus Torvalds 	upe->hard = !!hard;
23131da177e4SLinus Torvalds 
23149825069dSThomas Graf 	return nlmsg_end(skb, nlh);
23151da177e4SLinus Torvalds 
23161da177e4SLinus Torvalds nlmsg_failure:
23179825069dSThomas Graf 	nlmsg_cancel(skb, nlh);
23189825069dSThomas Graf 	return -EMSGSIZE;
23191da177e4SLinus Torvalds }
23201da177e4SLinus Torvalds 
232126b15dadSJamal Hadi Salim static int xfrm_exp_policy_notify(struct xfrm_policy *xp, int dir, struct km_event *c)
23221da177e4SLinus Torvalds {
23231da177e4SLinus Torvalds 	struct sk_buff *skb;
23241da177e4SLinus Torvalds 	size_t len;
23251da177e4SLinus Torvalds 
23261da177e4SLinus Torvalds 	len = RTA_SPACE(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr);
23271da177e4SLinus Torvalds 	len += NLMSG_SPACE(sizeof(struct xfrm_user_polexpire));
2328661697f7SJoy Latten 	len += RTA_SPACE(xfrm_user_sec_ctx_size(xp->security));
2329*cfbfd45aSThomas Graf 	len += userpolicy_type_attrsize();
23301da177e4SLinus Torvalds 	skb = alloc_skb(len, GFP_ATOMIC);
23311da177e4SLinus Torvalds 	if (skb == NULL)
23321da177e4SLinus Torvalds 		return -ENOMEM;
23331da177e4SLinus Torvalds 
2334d51d081dSJamal Hadi Salim 	if (build_polexpire(skb, xp, dir, c) < 0)
23351da177e4SLinus Torvalds 		BUG();
23361da177e4SLinus Torvalds 
2337082a1ad5SThomas Graf 	return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_EXPIRE, GFP_ATOMIC);
23381da177e4SLinus Torvalds }
23391da177e4SLinus Torvalds 
234026b15dadSJamal Hadi Salim static int xfrm_notify_policy(struct xfrm_policy *xp, int dir, struct km_event *c)
234126b15dadSJamal Hadi Salim {
234226b15dadSJamal Hadi Salim 	struct xfrm_userpolicy_info *p;
23430603eac0SHerbert Xu 	struct xfrm_userpolicy_id *id;
234426b15dadSJamal Hadi Salim 	struct nlmsghdr *nlh;
234526b15dadSJamal Hadi Salim 	struct sk_buff *skb;
234626b15dadSJamal Hadi Salim 	int len = RTA_SPACE(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr);
23470603eac0SHerbert Xu 	int headlen;
23480603eac0SHerbert Xu 
23490603eac0SHerbert Xu 	headlen = sizeof(*p);
23500603eac0SHerbert Xu 	if (c->event == XFRM_MSG_DELPOLICY) {
23510603eac0SHerbert Xu 		len += RTA_SPACE(headlen);
23520603eac0SHerbert Xu 		headlen = sizeof(*id);
23530603eac0SHerbert Xu 	}
2354*cfbfd45aSThomas Graf 	len += userpolicy_type_attrsize();
23550603eac0SHerbert Xu 	len += NLMSG_SPACE(headlen);
235626b15dadSJamal Hadi Salim 
235726b15dadSJamal Hadi Salim 	skb = alloc_skb(len, GFP_ATOMIC);
235826b15dadSJamal Hadi Salim 	if (skb == NULL)
235926b15dadSJamal Hadi Salim 		return -ENOMEM;
236026b15dadSJamal Hadi Salim 
236179b8b7f4SThomas Graf 	nlh = nlmsg_put(skb, c->pid, c->seq, c->event, headlen, 0);
236279b8b7f4SThomas Graf 	if (nlh == NULL)
236379b8b7f4SThomas Graf 		goto nlmsg_failure;
236426b15dadSJamal Hadi Salim 
23657b67c857SThomas Graf 	p = nlmsg_data(nlh);
23660603eac0SHerbert Xu 	if (c->event == XFRM_MSG_DELPOLICY) {
2367c0144beaSThomas Graf 		struct nlattr *attr;
2368c0144beaSThomas Graf 
23697b67c857SThomas Graf 		id = nlmsg_data(nlh);
23700603eac0SHerbert Xu 		memset(id, 0, sizeof(*id));
23710603eac0SHerbert Xu 		id->dir = dir;
23720603eac0SHerbert Xu 		if (c->data.byid)
23730603eac0SHerbert Xu 			id->index = xp->index;
23740603eac0SHerbert Xu 		else
23750603eac0SHerbert Xu 			memcpy(&id->sel, &xp->selector, sizeof(id->sel));
23760603eac0SHerbert Xu 
2377c0144beaSThomas Graf 		attr = nla_reserve(skb, XFRMA_POLICY, sizeof(*p));
2378c0144beaSThomas Graf 		if (attr == NULL)
2379c0144beaSThomas Graf 			goto nlmsg_failure;
2380c0144beaSThomas Graf 
2381c0144beaSThomas Graf 		p = nla_data(attr);
23820603eac0SHerbert Xu 	}
238326b15dadSJamal Hadi Salim 
238426b15dadSJamal Hadi Salim 	copy_to_user_policy(xp, p, dir);
238526b15dadSJamal Hadi Salim 	if (copy_to_user_tmpl(xp, skb) < 0)
238626b15dadSJamal Hadi Salim 		goto nlmsg_failure;
23871459bb36SJamal Hadi Salim 	if (copy_to_user_policy_type(xp->type, skb) < 0)
2388f7b6983fSMasahide NAKAMURA 		goto nlmsg_failure;
238926b15dadSJamal Hadi Salim 
23909825069dSThomas Graf 	nlmsg_end(skb, nlh);
239126b15dadSJamal Hadi Salim 
2392082a1ad5SThomas Graf 	return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_POLICY, GFP_ATOMIC);
239326b15dadSJamal Hadi Salim 
239426b15dadSJamal Hadi Salim nlmsg_failure:
239526b15dadSJamal Hadi Salim 	kfree_skb(skb);
239626b15dadSJamal Hadi Salim 	return -1;
239726b15dadSJamal Hadi Salim }
239826b15dadSJamal Hadi Salim 
239926b15dadSJamal Hadi Salim static int xfrm_notify_policy_flush(struct km_event *c)
240026b15dadSJamal Hadi Salim {
240126b15dadSJamal Hadi Salim 	struct nlmsghdr *nlh;
240226b15dadSJamal Hadi Salim 	struct sk_buff *skb;
2403785fd8b8SJamal Hadi Salim 	int len = 0;
2404*cfbfd45aSThomas Graf 	len += userpolicy_type_attrsize();
2405785fd8b8SJamal Hadi Salim 	len += NLMSG_LENGTH(0);
240626b15dadSJamal Hadi Salim 
240726b15dadSJamal Hadi Salim 	skb = alloc_skb(len, GFP_ATOMIC);
240826b15dadSJamal Hadi Salim 	if (skb == NULL)
240926b15dadSJamal Hadi Salim 		return -ENOMEM;
241026b15dadSJamal Hadi Salim 
241179b8b7f4SThomas Graf 	nlh = nlmsg_put(skb, c->pid, c->seq, XFRM_MSG_FLUSHPOLICY, 0, 0);
241279b8b7f4SThomas Graf 	if (nlh == NULL)
241379b8b7f4SThomas Graf 		goto nlmsg_failure;
24140c51f53cSJamal Hadi Salim 	if (copy_to_user_policy_type(c->data.type, skb) < 0)
24150c51f53cSJamal Hadi Salim 		goto nlmsg_failure;
241626b15dadSJamal Hadi Salim 
24179825069dSThomas Graf 	nlmsg_end(skb, nlh);
241826b15dadSJamal Hadi Salim 
2419082a1ad5SThomas Graf 	return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_POLICY, GFP_ATOMIC);
242026b15dadSJamal Hadi Salim 
242126b15dadSJamal Hadi Salim nlmsg_failure:
242226b15dadSJamal Hadi Salim 	kfree_skb(skb);
242326b15dadSJamal Hadi Salim 	return -1;
242426b15dadSJamal Hadi Salim }
242526b15dadSJamal Hadi Salim 
242626b15dadSJamal Hadi Salim static int xfrm_send_policy_notify(struct xfrm_policy *xp, int dir, struct km_event *c)
242726b15dadSJamal Hadi Salim {
242826b15dadSJamal Hadi Salim 
242926b15dadSJamal Hadi Salim 	switch (c->event) {
2430f60f6b8fSHerbert Xu 	case XFRM_MSG_NEWPOLICY:
2431f60f6b8fSHerbert Xu 	case XFRM_MSG_UPDPOLICY:
2432f60f6b8fSHerbert Xu 	case XFRM_MSG_DELPOLICY:
243326b15dadSJamal Hadi Salim 		return xfrm_notify_policy(xp, dir, c);
2434f60f6b8fSHerbert Xu 	case XFRM_MSG_FLUSHPOLICY:
243526b15dadSJamal Hadi Salim 		return xfrm_notify_policy_flush(c);
2436f60f6b8fSHerbert Xu 	case XFRM_MSG_POLEXPIRE:
243726b15dadSJamal Hadi Salim 		return xfrm_exp_policy_notify(xp, dir, c);
243826b15dadSJamal Hadi Salim 	default:
243926b15dadSJamal Hadi Salim 		printk("xfrm_user: Unknown Policy event %d\n", c->event);
244026b15dadSJamal Hadi Salim 	}
244126b15dadSJamal Hadi Salim 
244226b15dadSJamal Hadi Salim 	return 0;
244326b15dadSJamal Hadi Salim 
244426b15dadSJamal Hadi Salim }
244526b15dadSJamal Hadi Salim 
244697a64b45SMasahide NAKAMURA static int build_report(struct sk_buff *skb, u8 proto,
244797a64b45SMasahide NAKAMURA 			struct xfrm_selector *sel, xfrm_address_t *addr)
244897a64b45SMasahide NAKAMURA {
244997a64b45SMasahide NAKAMURA 	struct xfrm_user_report *ur;
245097a64b45SMasahide NAKAMURA 	struct nlmsghdr *nlh;
245197a64b45SMasahide NAKAMURA 
245279b8b7f4SThomas Graf 	nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_REPORT, sizeof(*ur), 0);
245379b8b7f4SThomas Graf 	if (nlh == NULL)
245479b8b7f4SThomas Graf 		return -EMSGSIZE;
245597a64b45SMasahide NAKAMURA 
24567b67c857SThomas Graf 	ur = nlmsg_data(nlh);
245797a64b45SMasahide NAKAMURA 	ur->proto = proto;
245897a64b45SMasahide NAKAMURA 	memcpy(&ur->sel, sel, sizeof(ur->sel));
245997a64b45SMasahide NAKAMURA 
246097a64b45SMasahide NAKAMURA 	if (addr)
2461c0144beaSThomas Graf 		NLA_PUT(skb, XFRMA_COADDR, sizeof(*addr), addr);
246297a64b45SMasahide NAKAMURA 
24639825069dSThomas Graf 	return nlmsg_end(skb, nlh);
246497a64b45SMasahide NAKAMURA 
2465c0144beaSThomas Graf nla_put_failure:
24669825069dSThomas Graf 	nlmsg_cancel(skb, nlh);
24679825069dSThomas Graf 	return -EMSGSIZE;
246897a64b45SMasahide NAKAMURA }
246997a64b45SMasahide NAKAMURA 
247097a64b45SMasahide NAKAMURA static int xfrm_send_report(u8 proto, struct xfrm_selector *sel,
247197a64b45SMasahide NAKAMURA 			    xfrm_address_t *addr)
247297a64b45SMasahide NAKAMURA {
247397a64b45SMasahide NAKAMURA 	struct sk_buff *skb;
247497a64b45SMasahide NAKAMURA 	size_t len;
247597a64b45SMasahide NAKAMURA 
247697a64b45SMasahide NAKAMURA 	len = NLMSG_ALIGN(NLMSG_LENGTH(sizeof(struct xfrm_user_report)));
247797a64b45SMasahide NAKAMURA 	skb = alloc_skb(len, GFP_ATOMIC);
247897a64b45SMasahide NAKAMURA 	if (skb == NULL)
247997a64b45SMasahide NAKAMURA 		return -ENOMEM;
248097a64b45SMasahide NAKAMURA 
248197a64b45SMasahide NAKAMURA 	if (build_report(skb, proto, sel, addr) < 0)
248297a64b45SMasahide NAKAMURA 		BUG();
248397a64b45SMasahide NAKAMURA 
2484082a1ad5SThomas Graf 	return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_REPORT, GFP_ATOMIC);
248597a64b45SMasahide NAKAMURA }
248697a64b45SMasahide NAKAMURA 
24871da177e4SLinus Torvalds static struct xfrm_mgr netlink_mgr = {
24881da177e4SLinus Torvalds 	.id		= "netlink",
24891da177e4SLinus Torvalds 	.notify		= xfrm_send_state_notify,
24901da177e4SLinus Torvalds 	.acquire	= xfrm_send_acquire,
24911da177e4SLinus Torvalds 	.compile_policy	= xfrm_compile_policy,
24921da177e4SLinus Torvalds 	.notify_policy	= xfrm_send_policy_notify,
249397a64b45SMasahide NAKAMURA 	.report		= xfrm_send_report,
24945c79de6eSShinta Sugimoto 	.migrate	= xfrm_send_migrate,
24951da177e4SLinus Torvalds };
24961da177e4SLinus Torvalds 
24971da177e4SLinus Torvalds static int __init xfrm_user_init(void)
24981da177e4SLinus Torvalds {
2499be33690dSPatrick McHardy 	struct sock *nlsk;
2500be33690dSPatrick McHardy 
2501654b32c6SMasahide NAKAMURA 	printk(KERN_INFO "Initializing XFRM netlink socket\n");
25021da177e4SLinus Torvalds 
2503be33690dSPatrick McHardy 	nlsk = netlink_kernel_create(NETLINK_XFRM, XFRMNLGRP_MAX,
2504af65bdfcSPatrick McHardy 				     xfrm_netlink_rcv, NULL, THIS_MODULE);
2505be33690dSPatrick McHardy 	if (nlsk == NULL)
25061da177e4SLinus Torvalds 		return -ENOMEM;
2507be33690dSPatrick McHardy 	rcu_assign_pointer(xfrm_nl, nlsk);
25081da177e4SLinus Torvalds 
25091da177e4SLinus Torvalds 	xfrm_register_km(&netlink_mgr);
25101da177e4SLinus Torvalds 
25111da177e4SLinus Torvalds 	return 0;
25121da177e4SLinus Torvalds }
25131da177e4SLinus Torvalds 
25141da177e4SLinus Torvalds static void __exit xfrm_user_exit(void)
25151da177e4SLinus Torvalds {
2516be33690dSPatrick McHardy 	struct sock *nlsk = xfrm_nl;
2517be33690dSPatrick McHardy 
25181da177e4SLinus Torvalds 	xfrm_unregister_km(&netlink_mgr);
2519be33690dSPatrick McHardy 	rcu_assign_pointer(xfrm_nl, NULL);
2520be33690dSPatrick McHardy 	synchronize_rcu();
2521be33690dSPatrick McHardy 	sock_release(nlsk->sk_socket);
25221da177e4SLinus Torvalds }
25231da177e4SLinus Torvalds 
25241da177e4SLinus Torvalds module_init(xfrm_user_init);
25251da177e4SLinus Torvalds module_exit(xfrm_user_exit);
25261da177e4SLinus Torvalds MODULE_LICENSE("GPL");
25274fdb3bb7SHarald Welte MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK, NETLINK_XFRM);
2528f8cd5488SJamal Hadi Salim 
2529