xref: /linux/net/xfrm/xfrm_user.c (revision 22e700502380b80cc81a44e219282762429ebe71)
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 
361da177e4SLinus Torvalds static int verify_one_alg(struct rtattr **xfrma, enum xfrm_attr_type_t type)
371da177e4SLinus Torvalds {
381da177e4SLinus Torvalds 	struct rtattr *rt = xfrma[type - 1];
391da177e4SLinus Torvalds 	struct xfrm_algo *algp;
4031c26852SHerbert Xu 	int len;
411da177e4SLinus Torvalds 
421da177e4SLinus Torvalds 	if (!rt)
431da177e4SLinus Torvalds 		return 0;
441da177e4SLinus Torvalds 
4531c26852SHerbert Xu 	len = (rt->rta_len - sizeof(*rt)) - sizeof(*algp);
4631c26852SHerbert Xu 	if (len < 0)
471da177e4SLinus Torvalds 		return -EINVAL;
481da177e4SLinus Torvalds 
491da177e4SLinus Torvalds 	algp = RTA_DATA(rt);
5031c26852SHerbert Xu 
5131c26852SHerbert Xu 	len -= (algp->alg_key_len + 7U) / 8;
5231c26852SHerbert Xu 	if (len < 0)
5331c26852SHerbert Xu 		return -EINVAL;
5431c26852SHerbert Xu 
551da177e4SLinus Torvalds 	switch (type) {
561da177e4SLinus Torvalds 	case XFRMA_ALG_AUTH:
571da177e4SLinus Torvalds 		if (!algp->alg_key_len &&
581da177e4SLinus Torvalds 		    strcmp(algp->alg_name, "digest_null") != 0)
591da177e4SLinus Torvalds 			return -EINVAL;
601da177e4SLinus Torvalds 		break;
611da177e4SLinus Torvalds 
621da177e4SLinus Torvalds 	case XFRMA_ALG_CRYPT:
631da177e4SLinus Torvalds 		if (!algp->alg_key_len &&
641da177e4SLinus Torvalds 		    strcmp(algp->alg_name, "cipher_null") != 0)
651da177e4SLinus Torvalds 			return -EINVAL;
661da177e4SLinus Torvalds 		break;
671da177e4SLinus Torvalds 
681da177e4SLinus Torvalds 	case XFRMA_ALG_COMP:
691da177e4SLinus Torvalds 		/* Zero length keys are legal.  */
701da177e4SLinus Torvalds 		break;
711da177e4SLinus Torvalds 
721da177e4SLinus Torvalds 	default:
731da177e4SLinus Torvalds 		return -EINVAL;
741da177e4SLinus Torvalds 	};
751da177e4SLinus Torvalds 
761da177e4SLinus Torvalds 	algp->alg_name[CRYPTO_MAX_ALG_NAME - 1] = '\0';
771da177e4SLinus Torvalds 	return 0;
781da177e4SLinus Torvalds }
791da177e4SLinus Torvalds 
801da177e4SLinus Torvalds static int verify_encap_tmpl(struct rtattr **xfrma)
811da177e4SLinus Torvalds {
821da177e4SLinus Torvalds 	struct rtattr *rt = xfrma[XFRMA_ENCAP - 1];
831da177e4SLinus Torvalds 	struct xfrm_encap_tmpl *encap;
841da177e4SLinus Torvalds 
851da177e4SLinus Torvalds 	if (!rt)
861da177e4SLinus Torvalds 		return 0;
871da177e4SLinus Torvalds 
881da177e4SLinus Torvalds 	if ((rt->rta_len - sizeof(*rt)) < sizeof(*encap))
891da177e4SLinus Torvalds 		return -EINVAL;
901da177e4SLinus Torvalds 
911da177e4SLinus Torvalds 	return 0;
921da177e4SLinus Torvalds }
931da177e4SLinus Torvalds 
94eb2971b6SMasahide NAKAMURA static int verify_one_addr(struct rtattr **xfrma, enum xfrm_attr_type_t type,
95eb2971b6SMasahide NAKAMURA 			   xfrm_address_t **addrp)
96eb2971b6SMasahide NAKAMURA {
97eb2971b6SMasahide NAKAMURA 	struct rtattr *rt = xfrma[type - 1];
98eb2971b6SMasahide NAKAMURA 
99eb2971b6SMasahide NAKAMURA 	if (!rt)
100eb2971b6SMasahide NAKAMURA 		return 0;
101eb2971b6SMasahide NAKAMURA 
102eb2971b6SMasahide NAKAMURA 	if ((rt->rta_len - sizeof(*rt)) < sizeof(**addrp))
103eb2971b6SMasahide NAKAMURA 		return -EINVAL;
104eb2971b6SMasahide NAKAMURA 
105eb2971b6SMasahide NAKAMURA 	if (addrp)
106eb2971b6SMasahide NAKAMURA 		*addrp = RTA_DATA(rt);
107eb2971b6SMasahide NAKAMURA 
108eb2971b6SMasahide NAKAMURA 	return 0;
109eb2971b6SMasahide NAKAMURA }
110df71837dSTrent Jaeger 
111df71837dSTrent Jaeger static inline int verify_sec_ctx_len(struct rtattr **xfrma)
112df71837dSTrent Jaeger {
113df71837dSTrent Jaeger 	struct rtattr *rt = xfrma[XFRMA_SEC_CTX - 1];
114df71837dSTrent Jaeger 	struct xfrm_user_sec_ctx *uctx;
115df71837dSTrent Jaeger 	int len = 0;
116df71837dSTrent Jaeger 
117df71837dSTrent Jaeger 	if (!rt)
118df71837dSTrent Jaeger 		return 0;
119df71837dSTrent Jaeger 
120df71837dSTrent Jaeger 	if (rt->rta_len < sizeof(*uctx))
121df71837dSTrent Jaeger 		return -EINVAL;
122df71837dSTrent Jaeger 
123df71837dSTrent Jaeger 	uctx = RTA_DATA(rt);
124df71837dSTrent Jaeger 
125df71837dSTrent Jaeger 	len += sizeof(struct xfrm_user_sec_ctx);
126df71837dSTrent Jaeger 	len += uctx->ctx_len;
127df71837dSTrent Jaeger 
128df71837dSTrent Jaeger 	if (uctx->len != len)
129df71837dSTrent Jaeger 		return -EINVAL;
130df71837dSTrent Jaeger 
131df71837dSTrent Jaeger 	return 0;
132df71837dSTrent Jaeger }
133df71837dSTrent Jaeger 
134df71837dSTrent Jaeger 
1351da177e4SLinus Torvalds static int verify_newsa_info(struct xfrm_usersa_info *p,
1361da177e4SLinus Torvalds 			     struct rtattr **xfrma)
1371da177e4SLinus Torvalds {
1381da177e4SLinus Torvalds 	int err;
1391da177e4SLinus Torvalds 
1401da177e4SLinus Torvalds 	err = -EINVAL;
1411da177e4SLinus Torvalds 	switch (p->family) {
1421da177e4SLinus Torvalds 	case AF_INET:
1431da177e4SLinus Torvalds 		break;
1441da177e4SLinus Torvalds 
1451da177e4SLinus Torvalds 	case AF_INET6:
1461da177e4SLinus Torvalds #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1471da177e4SLinus Torvalds 		break;
1481da177e4SLinus Torvalds #else
1491da177e4SLinus Torvalds 		err = -EAFNOSUPPORT;
1501da177e4SLinus Torvalds 		goto out;
1511da177e4SLinus Torvalds #endif
1521da177e4SLinus Torvalds 
1531da177e4SLinus Torvalds 	default:
1541da177e4SLinus Torvalds 		goto out;
1551da177e4SLinus Torvalds 	};
1561da177e4SLinus Torvalds 
1571da177e4SLinus Torvalds 	err = -EINVAL;
1581da177e4SLinus Torvalds 	switch (p->id.proto) {
1591da177e4SLinus Torvalds 	case IPPROTO_AH:
1601da177e4SLinus Torvalds 		if (!xfrma[XFRMA_ALG_AUTH-1]	||
1611da177e4SLinus Torvalds 		    xfrma[XFRMA_ALG_CRYPT-1]	||
1621da177e4SLinus Torvalds 		    xfrma[XFRMA_ALG_COMP-1])
1631da177e4SLinus Torvalds 			goto out;
1641da177e4SLinus Torvalds 		break;
1651da177e4SLinus Torvalds 
1661da177e4SLinus Torvalds 	case IPPROTO_ESP:
1671da177e4SLinus Torvalds 		if ((!xfrma[XFRMA_ALG_AUTH-1] &&
1681da177e4SLinus Torvalds 		     !xfrma[XFRMA_ALG_CRYPT-1])	||
1691da177e4SLinus Torvalds 		    xfrma[XFRMA_ALG_COMP-1])
1701da177e4SLinus Torvalds 			goto out;
1711da177e4SLinus Torvalds 		break;
1721da177e4SLinus Torvalds 
1731da177e4SLinus Torvalds 	case IPPROTO_COMP:
1741da177e4SLinus Torvalds 		if (!xfrma[XFRMA_ALG_COMP-1]	||
1751da177e4SLinus Torvalds 		    xfrma[XFRMA_ALG_AUTH-1]	||
1761da177e4SLinus Torvalds 		    xfrma[XFRMA_ALG_CRYPT-1])
1771da177e4SLinus Torvalds 			goto out;
1781da177e4SLinus Torvalds 		break;
1791da177e4SLinus Torvalds 
180e23c7194SMasahide NAKAMURA #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
181e23c7194SMasahide NAKAMURA 	case IPPROTO_DSTOPTS:
182e23c7194SMasahide NAKAMURA 	case IPPROTO_ROUTING:
183e23c7194SMasahide NAKAMURA 		if (xfrma[XFRMA_ALG_COMP-1]	||
184e23c7194SMasahide NAKAMURA 		    xfrma[XFRMA_ALG_AUTH-1]	||
185e23c7194SMasahide NAKAMURA 		    xfrma[XFRMA_ALG_CRYPT-1]	||
186e23c7194SMasahide NAKAMURA 		    xfrma[XFRMA_ENCAP-1]	||
187e23c7194SMasahide NAKAMURA 		    xfrma[XFRMA_SEC_CTX-1]	||
188e23c7194SMasahide NAKAMURA 		    !xfrma[XFRMA_COADDR-1])
189e23c7194SMasahide NAKAMURA 			goto out;
190e23c7194SMasahide NAKAMURA 		break;
191e23c7194SMasahide NAKAMURA #endif
192e23c7194SMasahide NAKAMURA 
1931da177e4SLinus Torvalds 	default:
1941da177e4SLinus Torvalds 		goto out;
1951da177e4SLinus Torvalds 	};
1961da177e4SLinus Torvalds 
1971da177e4SLinus Torvalds 	if ((err = verify_one_alg(xfrma, XFRMA_ALG_AUTH)))
1981da177e4SLinus Torvalds 		goto out;
1991da177e4SLinus Torvalds 	if ((err = verify_one_alg(xfrma, XFRMA_ALG_CRYPT)))
2001da177e4SLinus Torvalds 		goto out;
2011da177e4SLinus Torvalds 	if ((err = verify_one_alg(xfrma, XFRMA_ALG_COMP)))
2021da177e4SLinus Torvalds 		goto out;
2031da177e4SLinus Torvalds 	if ((err = verify_encap_tmpl(xfrma)))
2041da177e4SLinus Torvalds 		goto out;
205df71837dSTrent Jaeger 	if ((err = verify_sec_ctx_len(xfrma)))
206df71837dSTrent Jaeger 		goto out;
207060f02a3SNoriaki TAKAMIYA 	if ((err = verify_one_addr(xfrma, XFRMA_COADDR, NULL)))
208060f02a3SNoriaki TAKAMIYA 		goto out;
2091da177e4SLinus Torvalds 
2101da177e4SLinus Torvalds 	err = -EINVAL;
2111da177e4SLinus Torvalds 	switch (p->mode) {
2127e49e6deSMasahide NAKAMURA 	case XFRM_MODE_TRANSPORT:
2137e49e6deSMasahide NAKAMURA 	case XFRM_MODE_TUNNEL:
214060f02a3SNoriaki TAKAMIYA 	case XFRM_MODE_ROUTEOPTIMIZATION:
2150a69452cSDiego Beltrami 	case XFRM_MODE_BEET:
2161da177e4SLinus Torvalds 		break;
2171da177e4SLinus Torvalds 
2181da177e4SLinus Torvalds 	default:
2191da177e4SLinus Torvalds 		goto out;
2201da177e4SLinus Torvalds 	};
2211da177e4SLinus Torvalds 
2221da177e4SLinus Torvalds 	err = 0;
2231da177e4SLinus Torvalds 
2241da177e4SLinus Torvalds out:
2251da177e4SLinus Torvalds 	return err;
2261da177e4SLinus Torvalds }
2271da177e4SLinus Torvalds 
2281da177e4SLinus Torvalds static int attach_one_algo(struct xfrm_algo **algpp, u8 *props,
2291da177e4SLinus Torvalds 			   struct xfrm_algo_desc *(*get_byname)(char *, int),
2301da177e4SLinus Torvalds 			   struct rtattr *u_arg)
2311da177e4SLinus Torvalds {
2321da177e4SLinus Torvalds 	struct rtattr *rta = u_arg;
2331da177e4SLinus Torvalds 	struct xfrm_algo *p, *ualg;
2341da177e4SLinus Torvalds 	struct xfrm_algo_desc *algo;
235b9e9deadSHerbert Xu 	int len;
2361da177e4SLinus Torvalds 
2371da177e4SLinus Torvalds 	if (!rta)
2381da177e4SLinus Torvalds 		return 0;
2391da177e4SLinus Torvalds 
2401da177e4SLinus Torvalds 	ualg = RTA_DATA(rta);
2411da177e4SLinus Torvalds 
2421da177e4SLinus Torvalds 	algo = get_byname(ualg->alg_name, 1);
2431da177e4SLinus Torvalds 	if (!algo)
2441da177e4SLinus Torvalds 		return -ENOSYS;
2451da177e4SLinus Torvalds 	*props = algo->desc.sadb_alg_id;
2461da177e4SLinus Torvalds 
247b9e9deadSHerbert Xu 	len = sizeof(*ualg) + (ualg->alg_key_len + 7U) / 8;
248cdbc6daeSArnaldo Carvalho de Melo 	p = kmemdup(ualg, len, GFP_KERNEL);
2491da177e4SLinus Torvalds 	if (!p)
2501da177e4SLinus Torvalds 		return -ENOMEM;
2511da177e4SLinus Torvalds 
25204ff1260SHerbert Xu 	strcpy(p->alg_name, algo->name);
2531da177e4SLinus Torvalds 	*algpp = p;
2541da177e4SLinus Torvalds 	return 0;
2551da177e4SLinus Torvalds }
2561da177e4SLinus Torvalds 
2571da177e4SLinus Torvalds static int attach_encap_tmpl(struct xfrm_encap_tmpl **encapp, struct rtattr *u_arg)
2581da177e4SLinus Torvalds {
2591da177e4SLinus Torvalds 	struct rtattr *rta = u_arg;
2601da177e4SLinus Torvalds 	struct xfrm_encap_tmpl *p, *uencap;
2611da177e4SLinus Torvalds 
2621da177e4SLinus Torvalds 	if (!rta)
2631da177e4SLinus Torvalds 		return 0;
2641da177e4SLinus Torvalds 
2651da177e4SLinus Torvalds 	uencap = RTA_DATA(rta);
266cdbc6daeSArnaldo Carvalho de Melo 	p = kmemdup(uencap, sizeof(*p), GFP_KERNEL);
2671da177e4SLinus Torvalds 	if (!p)
2681da177e4SLinus Torvalds 		return -ENOMEM;
2691da177e4SLinus Torvalds 
2701da177e4SLinus Torvalds 	*encapp = p;
2711da177e4SLinus Torvalds 	return 0;
2721da177e4SLinus Torvalds }
2731da177e4SLinus Torvalds 
274df71837dSTrent Jaeger 
275df71837dSTrent Jaeger static inline int xfrm_user_sec_ctx_size(struct xfrm_policy *xp)
276df71837dSTrent Jaeger {
277df71837dSTrent Jaeger 	struct xfrm_sec_ctx *xfrm_ctx = xp->security;
278df71837dSTrent Jaeger 	int len = 0;
279df71837dSTrent Jaeger 
280df71837dSTrent Jaeger 	if (xfrm_ctx) {
281df71837dSTrent Jaeger 		len += sizeof(struct xfrm_user_sec_ctx);
282df71837dSTrent Jaeger 		len += xfrm_ctx->ctx_len;
283df71837dSTrent Jaeger 	}
284df71837dSTrent Jaeger 	return len;
285df71837dSTrent Jaeger }
286df71837dSTrent Jaeger 
287df71837dSTrent Jaeger static int attach_sec_ctx(struct xfrm_state *x, struct rtattr *u_arg)
288df71837dSTrent Jaeger {
289df71837dSTrent Jaeger 	struct xfrm_user_sec_ctx *uctx;
290df71837dSTrent Jaeger 
291df71837dSTrent Jaeger 	if (!u_arg)
292df71837dSTrent Jaeger 		return 0;
293df71837dSTrent Jaeger 
294df71837dSTrent Jaeger 	uctx = RTA_DATA(u_arg);
295df71837dSTrent Jaeger 	return security_xfrm_state_alloc(x, uctx);
296df71837dSTrent Jaeger }
297df71837dSTrent Jaeger 
298060f02a3SNoriaki TAKAMIYA static int attach_one_addr(xfrm_address_t **addrpp, struct rtattr *u_arg)
299060f02a3SNoriaki TAKAMIYA {
300060f02a3SNoriaki TAKAMIYA 	struct rtattr *rta = u_arg;
301060f02a3SNoriaki TAKAMIYA 	xfrm_address_t *p, *uaddrp;
302060f02a3SNoriaki TAKAMIYA 
303060f02a3SNoriaki TAKAMIYA 	if (!rta)
304060f02a3SNoriaki TAKAMIYA 		return 0;
305060f02a3SNoriaki TAKAMIYA 
306060f02a3SNoriaki TAKAMIYA 	uaddrp = RTA_DATA(rta);
307cdbc6daeSArnaldo Carvalho de Melo 	p = kmemdup(uaddrp, sizeof(*p), GFP_KERNEL);
308060f02a3SNoriaki TAKAMIYA 	if (!p)
309060f02a3SNoriaki TAKAMIYA 		return -ENOMEM;
310060f02a3SNoriaki TAKAMIYA 
311060f02a3SNoriaki TAKAMIYA 	*addrpp = p;
312060f02a3SNoriaki TAKAMIYA 	return 0;
313060f02a3SNoriaki TAKAMIYA }
314060f02a3SNoriaki TAKAMIYA 
3151da177e4SLinus Torvalds static void copy_from_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p)
3161da177e4SLinus Torvalds {
3171da177e4SLinus Torvalds 	memcpy(&x->id, &p->id, sizeof(x->id));
3181da177e4SLinus Torvalds 	memcpy(&x->sel, &p->sel, sizeof(x->sel));
3191da177e4SLinus Torvalds 	memcpy(&x->lft, &p->lft, sizeof(x->lft));
3201da177e4SLinus Torvalds 	x->props.mode = p->mode;
3211da177e4SLinus Torvalds 	x->props.replay_window = p->replay_window;
3221da177e4SLinus Torvalds 	x->props.reqid = p->reqid;
3231da177e4SLinus Torvalds 	x->props.family = p->family;
32454489c14SDavid S. Miller 	memcpy(&x->props.saddr, &p->saddr, sizeof(x->props.saddr));
3251da177e4SLinus Torvalds 	x->props.flags = p->flags;
3261da177e4SLinus Torvalds }
3271da177e4SLinus Torvalds 
328d51d081dSJamal Hadi Salim /*
329d51d081dSJamal Hadi Salim  * someday when pfkey also has support, we could have the code
330d51d081dSJamal Hadi Salim  * somehow made shareable and move it to xfrm_state.c - JHS
331d51d081dSJamal Hadi Salim  *
332d51d081dSJamal Hadi Salim */
333d51d081dSJamal Hadi Salim static int xfrm_update_ae_params(struct xfrm_state *x, struct rtattr **xfrma)
334d51d081dSJamal Hadi Salim {
335d51d081dSJamal Hadi Salim 	int err = - EINVAL;
336d51d081dSJamal Hadi Salim 	struct rtattr *rp = xfrma[XFRMA_REPLAY_VAL-1];
337d51d081dSJamal Hadi Salim 	struct rtattr *lt = xfrma[XFRMA_LTIME_VAL-1];
338d51d081dSJamal Hadi Salim 	struct rtattr *et = xfrma[XFRMA_ETIMER_THRESH-1];
339d51d081dSJamal Hadi Salim 	struct rtattr *rt = xfrma[XFRMA_REPLAY_THRESH-1];
340d51d081dSJamal Hadi Salim 
341d51d081dSJamal Hadi Salim 	if (rp) {
342d51d081dSJamal Hadi Salim 		struct xfrm_replay_state *replay;
343d51d081dSJamal Hadi Salim 		if (RTA_PAYLOAD(rp) < sizeof(*replay))
344d51d081dSJamal Hadi Salim 			goto error;
345d51d081dSJamal Hadi Salim 		replay = RTA_DATA(rp);
346d51d081dSJamal Hadi Salim 		memcpy(&x->replay, replay, sizeof(*replay));
347d51d081dSJamal Hadi Salim 		memcpy(&x->preplay, replay, sizeof(*replay));
348d51d081dSJamal Hadi Salim 	}
349d51d081dSJamal Hadi Salim 
350d51d081dSJamal Hadi Salim 	if (lt) {
351d51d081dSJamal Hadi Salim 		struct xfrm_lifetime_cur *ltime;
352d51d081dSJamal Hadi Salim 		if (RTA_PAYLOAD(lt) < sizeof(*ltime))
353d51d081dSJamal Hadi Salim 			goto error;
354d51d081dSJamal Hadi Salim 		ltime = RTA_DATA(lt);
355d51d081dSJamal Hadi Salim 		x->curlft.bytes = ltime->bytes;
356d51d081dSJamal Hadi Salim 		x->curlft.packets = ltime->packets;
357d51d081dSJamal Hadi Salim 		x->curlft.add_time = ltime->add_time;
358d51d081dSJamal Hadi Salim 		x->curlft.use_time = ltime->use_time;
359d51d081dSJamal Hadi Salim 	}
360d51d081dSJamal Hadi Salim 
361d51d081dSJamal Hadi Salim 	if (et) {
362d51d081dSJamal Hadi Salim 		if (RTA_PAYLOAD(et) < sizeof(u32))
363d51d081dSJamal Hadi Salim 			goto error;
364d51d081dSJamal Hadi Salim 		x->replay_maxage = *(u32*)RTA_DATA(et);
365d51d081dSJamal Hadi Salim 	}
366d51d081dSJamal Hadi Salim 
367d51d081dSJamal Hadi Salim 	if (rt) {
368d51d081dSJamal Hadi Salim 		if (RTA_PAYLOAD(rt) < sizeof(u32))
369d51d081dSJamal Hadi Salim 			goto error;
370d51d081dSJamal Hadi Salim 		x->replay_maxdiff = *(u32*)RTA_DATA(rt);
371d51d081dSJamal Hadi Salim 	}
372d51d081dSJamal Hadi Salim 
373d51d081dSJamal Hadi Salim 	return 0;
374d51d081dSJamal Hadi Salim error:
375d51d081dSJamal Hadi Salim 	return err;
376d51d081dSJamal Hadi Salim }
377d51d081dSJamal Hadi Salim 
3781da177e4SLinus Torvalds static struct xfrm_state *xfrm_state_construct(struct xfrm_usersa_info *p,
3791da177e4SLinus Torvalds 					       struct rtattr **xfrma,
3801da177e4SLinus Torvalds 					       int *errp)
3811da177e4SLinus Torvalds {
3821da177e4SLinus Torvalds 	struct xfrm_state *x = xfrm_state_alloc();
3831da177e4SLinus Torvalds 	int err = -ENOMEM;
3841da177e4SLinus Torvalds 
3851da177e4SLinus Torvalds 	if (!x)
3861da177e4SLinus Torvalds 		goto error_no_put;
3871da177e4SLinus Torvalds 
3881da177e4SLinus Torvalds 	copy_from_user_state(x, p);
3891da177e4SLinus Torvalds 
3901da177e4SLinus Torvalds 	if ((err = attach_one_algo(&x->aalg, &x->props.aalgo,
3911da177e4SLinus Torvalds 				   xfrm_aalg_get_byname,
3921da177e4SLinus Torvalds 				   xfrma[XFRMA_ALG_AUTH-1])))
3931da177e4SLinus Torvalds 		goto error;
3941da177e4SLinus Torvalds 	if ((err = attach_one_algo(&x->ealg, &x->props.ealgo,
3951da177e4SLinus Torvalds 				   xfrm_ealg_get_byname,
3961da177e4SLinus Torvalds 				   xfrma[XFRMA_ALG_CRYPT-1])))
3971da177e4SLinus Torvalds 		goto error;
3981da177e4SLinus Torvalds 	if ((err = attach_one_algo(&x->calg, &x->props.calgo,
3991da177e4SLinus Torvalds 				   xfrm_calg_get_byname,
4001da177e4SLinus Torvalds 				   xfrma[XFRMA_ALG_COMP-1])))
4011da177e4SLinus Torvalds 		goto error;
4021da177e4SLinus Torvalds 	if ((err = attach_encap_tmpl(&x->encap, xfrma[XFRMA_ENCAP-1])))
4031da177e4SLinus Torvalds 		goto error;
404060f02a3SNoriaki TAKAMIYA 	if ((err = attach_one_addr(&x->coaddr, xfrma[XFRMA_COADDR-1])))
405060f02a3SNoriaki TAKAMIYA 		goto error;
40672cb6962SHerbert Xu 	err = xfrm_init_state(x);
4071da177e4SLinus Torvalds 	if (err)
4081da177e4SLinus Torvalds 		goto error;
4091da177e4SLinus Torvalds 
410df71837dSTrent Jaeger 	if ((err = attach_sec_ctx(x, xfrma[XFRMA_SEC_CTX-1])))
411df71837dSTrent Jaeger 		goto error;
412df71837dSTrent Jaeger 
4131da177e4SLinus Torvalds 	x->km.seq = p->seq;
414d51d081dSJamal Hadi Salim 	x->replay_maxdiff = sysctl_xfrm_aevent_rseqth;
415d51d081dSJamal Hadi Salim 	/* sysctl_xfrm_aevent_etime is in 100ms units */
416d51d081dSJamal Hadi Salim 	x->replay_maxage = (sysctl_xfrm_aevent_etime*HZ)/XFRM_AE_ETH_M;
417d51d081dSJamal Hadi Salim 	x->preplay.bitmap = 0;
418d51d081dSJamal Hadi Salim 	x->preplay.seq = x->replay.seq+x->replay_maxdiff;
419d51d081dSJamal Hadi Salim 	x->preplay.oseq = x->replay.oseq +x->replay_maxdiff;
420d51d081dSJamal Hadi Salim 
421d51d081dSJamal Hadi Salim 	/* override default values from above */
422d51d081dSJamal Hadi Salim 
423d51d081dSJamal Hadi Salim 	err = xfrm_update_ae_params(x, (struct rtattr **)xfrma);
424d51d081dSJamal Hadi Salim 	if (err	< 0)
425d51d081dSJamal Hadi Salim 		goto error;
4261da177e4SLinus Torvalds 
4271da177e4SLinus Torvalds 	return x;
4281da177e4SLinus Torvalds 
4291da177e4SLinus Torvalds error:
4301da177e4SLinus Torvalds 	x->km.state = XFRM_STATE_DEAD;
4311da177e4SLinus Torvalds 	xfrm_state_put(x);
4321da177e4SLinus Torvalds error_no_put:
4331da177e4SLinus Torvalds 	*errp = err;
4341da177e4SLinus Torvalds 	return NULL;
4351da177e4SLinus Torvalds }
4361da177e4SLinus Torvalds 
437*22e70050SChristoph Hellwig static int xfrm_add_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
438*22e70050SChristoph Hellwig 		struct rtattr **xfrma)
4391da177e4SLinus Torvalds {
4401da177e4SLinus Torvalds 	struct xfrm_usersa_info *p = NLMSG_DATA(nlh);
4411da177e4SLinus Torvalds 	struct xfrm_state *x;
4421da177e4SLinus Torvalds 	int err;
44326b15dadSJamal Hadi Salim 	struct km_event c;
4441da177e4SLinus Torvalds 
445*22e70050SChristoph Hellwig 	err = verify_newsa_info(p, xfrma);
4461da177e4SLinus Torvalds 	if (err)
4471da177e4SLinus Torvalds 		return err;
4481da177e4SLinus Torvalds 
449*22e70050SChristoph Hellwig 	x = xfrm_state_construct(p, xfrma, &err);
4501da177e4SLinus Torvalds 	if (!x)
4511da177e4SLinus Torvalds 		return err;
4521da177e4SLinus Torvalds 
45326b15dadSJamal Hadi Salim 	xfrm_state_hold(x);
4541da177e4SLinus Torvalds 	if (nlh->nlmsg_type == XFRM_MSG_NEWSA)
4551da177e4SLinus Torvalds 		err = xfrm_state_add(x);
4561da177e4SLinus Torvalds 	else
4571da177e4SLinus Torvalds 		err = xfrm_state_update(x);
4581da177e4SLinus Torvalds 
459161a09e7SJoy Latten 	xfrm_audit_log(NETLINK_CB(skb).loginuid, NETLINK_CB(skb).sid,
460161a09e7SJoy Latten 		       AUDIT_MAC_IPSEC_ADDSA, err ? 0 : 1, NULL, x);
461161a09e7SJoy Latten 
4621da177e4SLinus Torvalds 	if (err < 0) {
4631da177e4SLinus Torvalds 		x->km.state = XFRM_STATE_DEAD;
46421380b81SHerbert Xu 		__xfrm_state_put(x);
4657d6dfe1fSPatrick McHardy 		goto out;
4661da177e4SLinus Torvalds 	}
4671da177e4SLinus Torvalds 
46826b15dadSJamal Hadi Salim 	c.seq = nlh->nlmsg_seq;
46926b15dadSJamal Hadi Salim 	c.pid = nlh->nlmsg_pid;
470f60f6b8fSHerbert Xu 	c.event = nlh->nlmsg_type;
47126b15dadSJamal Hadi Salim 
47226b15dadSJamal Hadi Salim 	km_state_notify(x, &c);
4737d6dfe1fSPatrick McHardy out:
47426b15dadSJamal Hadi Salim 	xfrm_state_put(x);
4751da177e4SLinus Torvalds 	return err;
4761da177e4SLinus Torvalds }
4771da177e4SLinus Torvalds 
478eb2971b6SMasahide NAKAMURA static struct xfrm_state *xfrm_user_state_lookup(struct xfrm_usersa_id *p,
479eb2971b6SMasahide NAKAMURA 						 struct rtattr **xfrma,
480eb2971b6SMasahide NAKAMURA 						 int *errp)
481eb2971b6SMasahide NAKAMURA {
482eb2971b6SMasahide NAKAMURA 	struct xfrm_state *x = NULL;
483eb2971b6SMasahide NAKAMURA 	int err;
484eb2971b6SMasahide NAKAMURA 
485eb2971b6SMasahide NAKAMURA 	if (xfrm_id_proto_match(p->proto, IPSEC_PROTO_ANY)) {
486eb2971b6SMasahide NAKAMURA 		err = -ESRCH;
487eb2971b6SMasahide NAKAMURA 		x = xfrm_state_lookup(&p->daddr, p->spi, p->proto, p->family);
488eb2971b6SMasahide NAKAMURA 	} else {
489eb2971b6SMasahide NAKAMURA 		xfrm_address_t *saddr = NULL;
490eb2971b6SMasahide NAKAMURA 
491eb2971b6SMasahide NAKAMURA 		err = verify_one_addr(xfrma, XFRMA_SRCADDR, &saddr);
492eb2971b6SMasahide NAKAMURA 		if (err)
493eb2971b6SMasahide NAKAMURA 			goto out;
494eb2971b6SMasahide NAKAMURA 
495eb2971b6SMasahide NAKAMURA 		if (!saddr) {
496eb2971b6SMasahide NAKAMURA 			err = -EINVAL;
497eb2971b6SMasahide NAKAMURA 			goto out;
498eb2971b6SMasahide NAKAMURA 		}
499eb2971b6SMasahide NAKAMURA 
5009abbffeeSMasahide NAKAMURA 		err = -ESRCH;
501eb2971b6SMasahide NAKAMURA 		x = xfrm_state_lookup_byaddr(&p->daddr, saddr, p->proto,
502eb2971b6SMasahide NAKAMURA 					     p->family);
503eb2971b6SMasahide NAKAMURA 	}
504eb2971b6SMasahide NAKAMURA 
505eb2971b6SMasahide NAKAMURA  out:
506eb2971b6SMasahide NAKAMURA 	if (!x && errp)
507eb2971b6SMasahide NAKAMURA 		*errp = err;
508eb2971b6SMasahide NAKAMURA 	return x;
509eb2971b6SMasahide NAKAMURA }
510eb2971b6SMasahide NAKAMURA 
511*22e70050SChristoph Hellwig static int xfrm_del_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
512*22e70050SChristoph Hellwig 		struct rtattr **xfrma)
5131da177e4SLinus Torvalds {
5141da177e4SLinus Torvalds 	struct xfrm_state *x;
515eb2971b6SMasahide NAKAMURA 	int err = -ESRCH;
51626b15dadSJamal Hadi Salim 	struct km_event c;
5171da177e4SLinus Torvalds 	struct xfrm_usersa_id *p = NLMSG_DATA(nlh);
5181da177e4SLinus Torvalds 
519*22e70050SChristoph Hellwig 	x = xfrm_user_state_lookup(p, xfrma, &err);
5201da177e4SLinus Torvalds 	if (x == NULL)
521eb2971b6SMasahide NAKAMURA 		return err;
5221da177e4SLinus Torvalds 
5236f68dc37SDavid S. Miller 	if ((err = security_xfrm_state_delete(x)) != 0)
524c8c05a8eSCatherine Zhang 		goto out;
525c8c05a8eSCatherine Zhang 
5261da177e4SLinus Torvalds 	if (xfrm_state_kern(x)) {
527c8c05a8eSCatherine Zhang 		err = -EPERM;
528c8c05a8eSCatherine Zhang 		goto out;
5291da177e4SLinus Torvalds 	}
5301da177e4SLinus Torvalds 
53126b15dadSJamal Hadi Salim 	err = xfrm_state_delete(x);
532161a09e7SJoy Latten 
533161a09e7SJoy Latten 	xfrm_audit_log(NETLINK_CB(skb).loginuid, NETLINK_CB(skb).sid,
534161a09e7SJoy Latten 		       AUDIT_MAC_IPSEC_DELSA, err ? 0 : 1, NULL, x);
535161a09e7SJoy Latten 
536c8c05a8eSCatherine Zhang 	if (err < 0)
537c8c05a8eSCatherine Zhang 		goto out;
53826b15dadSJamal Hadi Salim 
53926b15dadSJamal Hadi Salim 	c.seq = nlh->nlmsg_seq;
54026b15dadSJamal Hadi Salim 	c.pid = nlh->nlmsg_pid;
541f60f6b8fSHerbert Xu 	c.event = nlh->nlmsg_type;
54226b15dadSJamal Hadi Salim 	km_state_notify(x, &c);
5431da177e4SLinus Torvalds 
544c8c05a8eSCatherine Zhang out:
545c8c05a8eSCatherine Zhang 	xfrm_state_put(x);
54626b15dadSJamal Hadi Salim 	return err;
5471da177e4SLinus Torvalds }
5481da177e4SLinus Torvalds 
5491da177e4SLinus Torvalds static void copy_to_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p)
5501da177e4SLinus Torvalds {
5511da177e4SLinus Torvalds 	memcpy(&p->id, &x->id, sizeof(p->id));
5521da177e4SLinus Torvalds 	memcpy(&p->sel, &x->sel, sizeof(p->sel));
5531da177e4SLinus Torvalds 	memcpy(&p->lft, &x->lft, sizeof(p->lft));
5541da177e4SLinus Torvalds 	memcpy(&p->curlft, &x->curlft, sizeof(p->curlft));
5551da177e4SLinus Torvalds 	memcpy(&p->stats, &x->stats, sizeof(p->stats));
55654489c14SDavid S. Miller 	memcpy(&p->saddr, &x->props.saddr, sizeof(p->saddr));
5571da177e4SLinus Torvalds 	p->mode = x->props.mode;
5581da177e4SLinus Torvalds 	p->replay_window = x->props.replay_window;
5591da177e4SLinus Torvalds 	p->reqid = x->props.reqid;
5601da177e4SLinus Torvalds 	p->family = x->props.family;
5611da177e4SLinus Torvalds 	p->flags = x->props.flags;
5621da177e4SLinus Torvalds 	p->seq = x->km.seq;
5631da177e4SLinus Torvalds }
5641da177e4SLinus Torvalds 
5651da177e4SLinus Torvalds struct xfrm_dump_info {
5661da177e4SLinus Torvalds 	struct sk_buff *in_skb;
5671da177e4SLinus Torvalds 	struct sk_buff *out_skb;
5681da177e4SLinus Torvalds 	u32 nlmsg_seq;
5691da177e4SLinus Torvalds 	u16 nlmsg_flags;
5701da177e4SLinus Torvalds 	int start_idx;
5711da177e4SLinus Torvalds 	int this_idx;
5721da177e4SLinus Torvalds };
5731da177e4SLinus Torvalds 
5741da177e4SLinus Torvalds static int dump_one_state(struct xfrm_state *x, int count, void *ptr)
5751da177e4SLinus Torvalds {
5761da177e4SLinus Torvalds 	struct xfrm_dump_info *sp = ptr;
5771da177e4SLinus Torvalds 	struct sk_buff *in_skb = sp->in_skb;
5781da177e4SLinus Torvalds 	struct sk_buff *skb = sp->out_skb;
5791da177e4SLinus Torvalds 	struct xfrm_usersa_info *p;
5801da177e4SLinus Torvalds 	struct nlmsghdr *nlh;
5811da177e4SLinus Torvalds 	unsigned char *b = skb->tail;
5821da177e4SLinus Torvalds 
5831da177e4SLinus Torvalds 	if (sp->this_idx < sp->start_idx)
5841da177e4SLinus Torvalds 		goto out;
5851da177e4SLinus Torvalds 
5861da177e4SLinus Torvalds 	nlh = NLMSG_PUT(skb, NETLINK_CB(in_skb).pid,
5871da177e4SLinus Torvalds 			sp->nlmsg_seq,
5881da177e4SLinus Torvalds 			XFRM_MSG_NEWSA, sizeof(*p));
5891da177e4SLinus Torvalds 	nlh->nlmsg_flags = sp->nlmsg_flags;
5901da177e4SLinus Torvalds 
5911da177e4SLinus Torvalds 	p = NLMSG_DATA(nlh);
5921da177e4SLinus Torvalds 	copy_to_user_state(x, p);
5931da177e4SLinus Torvalds 
5941da177e4SLinus Torvalds 	if (x->aalg)
5951da177e4SLinus Torvalds 		RTA_PUT(skb, XFRMA_ALG_AUTH,
5961da177e4SLinus Torvalds 			sizeof(*(x->aalg))+(x->aalg->alg_key_len+7)/8, x->aalg);
5971da177e4SLinus Torvalds 	if (x->ealg)
5981da177e4SLinus Torvalds 		RTA_PUT(skb, XFRMA_ALG_CRYPT,
5991da177e4SLinus Torvalds 			sizeof(*(x->ealg))+(x->ealg->alg_key_len+7)/8, x->ealg);
6001da177e4SLinus Torvalds 	if (x->calg)
6011da177e4SLinus Torvalds 		RTA_PUT(skb, XFRMA_ALG_COMP, sizeof(*(x->calg)), x->calg);
6021da177e4SLinus Torvalds 
6031da177e4SLinus Torvalds 	if (x->encap)
6041da177e4SLinus Torvalds 		RTA_PUT(skb, XFRMA_ENCAP, sizeof(*x->encap), x->encap);
6051da177e4SLinus Torvalds 
606df71837dSTrent Jaeger 	if (x->security) {
607df71837dSTrent Jaeger 		int ctx_size = sizeof(struct xfrm_sec_ctx) +
608df71837dSTrent Jaeger 				x->security->ctx_len;
609df71837dSTrent Jaeger 		struct rtattr *rt = __RTA_PUT(skb, XFRMA_SEC_CTX, ctx_size);
610df71837dSTrent Jaeger 		struct xfrm_user_sec_ctx *uctx = RTA_DATA(rt);
611df71837dSTrent Jaeger 
612df71837dSTrent Jaeger 		uctx->exttype = XFRMA_SEC_CTX;
613df71837dSTrent Jaeger 		uctx->len = ctx_size;
614df71837dSTrent Jaeger 		uctx->ctx_doi = x->security->ctx_doi;
615df71837dSTrent Jaeger 		uctx->ctx_alg = x->security->ctx_alg;
616df71837dSTrent Jaeger 		uctx->ctx_len = x->security->ctx_len;
617df71837dSTrent Jaeger 		memcpy(uctx + 1, x->security->ctx_str, x->security->ctx_len);
618df71837dSTrent Jaeger 	}
619060f02a3SNoriaki TAKAMIYA 
620060f02a3SNoriaki TAKAMIYA 	if (x->coaddr)
621060f02a3SNoriaki TAKAMIYA 		RTA_PUT(skb, XFRMA_COADDR, sizeof(*x->coaddr), x->coaddr);
622060f02a3SNoriaki TAKAMIYA 
6239afaca05SMasahide NAKAMURA 	if (x->lastused)
6249afaca05SMasahide NAKAMURA 		RTA_PUT(skb, XFRMA_LASTUSED, sizeof(x->lastused), &x->lastused);
6259afaca05SMasahide NAKAMURA 
6261da177e4SLinus Torvalds 	nlh->nlmsg_len = skb->tail - b;
6271da177e4SLinus Torvalds out:
6281da177e4SLinus Torvalds 	sp->this_idx++;
6291da177e4SLinus Torvalds 	return 0;
6301da177e4SLinus Torvalds 
6311da177e4SLinus Torvalds nlmsg_failure:
6321da177e4SLinus Torvalds rtattr_failure:
6331da177e4SLinus Torvalds 	skb_trim(skb, b - skb->data);
6341da177e4SLinus Torvalds 	return -1;
6351da177e4SLinus Torvalds }
6361da177e4SLinus Torvalds 
6371da177e4SLinus Torvalds static int xfrm_dump_sa(struct sk_buff *skb, struct netlink_callback *cb)
6381da177e4SLinus Torvalds {
6391da177e4SLinus Torvalds 	struct xfrm_dump_info info;
6401da177e4SLinus Torvalds 
6411da177e4SLinus Torvalds 	info.in_skb = cb->skb;
6421da177e4SLinus Torvalds 	info.out_skb = skb;
6431da177e4SLinus Torvalds 	info.nlmsg_seq = cb->nlh->nlmsg_seq;
6441da177e4SLinus Torvalds 	info.nlmsg_flags = NLM_F_MULTI;
6451da177e4SLinus Torvalds 	info.this_idx = 0;
6461da177e4SLinus Torvalds 	info.start_idx = cb->args[0];
647dc00a525SMasahide NAKAMURA 	(void) xfrm_state_walk(0, dump_one_state, &info);
6481da177e4SLinus Torvalds 	cb->args[0] = info.this_idx;
6491da177e4SLinus Torvalds 
6501da177e4SLinus Torvalds 	return skb->len;
6511da177e4SLinus Torvalds }
6521da177e4SLinus Torvalds 
6531da177e4SLinus Torvalds static struct sk_buff *xfrm_state_netlink(struct sk_buff *in_skb,
6541da177e4SLinus Torvalds 					  struct xfrm_state *x, u32 seq)
6551da177e4SLinus Torvalds {
6561da177e4SLinus Torvalds 	struct xfrm_dump_info info;
6571da177e4SLinus Torvalds 	struct sk_buff *skb;
6581da177e4SLinus Torvalds 
6591da177e4SLinus Torvalds 	skb = alloc_skb(NLMSG_GOODSIZE, GFP_ATOMIC);
6601da177e4SLinus Torvalds 	if (!skb)
6611da177e4SLinus Torvalds 		return ERR_PTR(-ENOMEM);
6621da177e4SLinus Torvalds 
6631da177e4SLinus Torvalds 	info.in_skb = in_skb;
6641da177e4SLinus Torvalds 	info.out_skb = skb;
6651da177e4SLinus Torvalds 	info.nlmsg_seq = seq;
6661da177e4SLinus Torvalds 	info.nlmsg_flags = 0;
6671da177e4SLinus Torvalds 	info.this_idx = info.start_idx = 0;
6681da177e4SLinus Torvalds 
6691da177e4SLinus Torvalds 	if (dump_one_state(x, 0, &info)) {
6701da177e4SLinus Torvalds 		kfree_skb(skb);
6711da177e4SLinus Torvalds 		return NULL;
6721da177e4SLinus Torvalds 	}
6731da177e4SLinus Torvalds 
6741da177e4SLinus Torvalds 	return skb;
6751da177e4SLinus Torvalds }
6761da177e4SLinus Torvalds 
677*22e70050SChristoph Hellwig static int xfrm_get_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
678*22e70050SChristoph Hellwig 		struct rtattr **xfrma)
6791da177e4SLinus Torvalds {
6801da177e4SLinus Torvalds 	struct xfrm_usersa_id *p = NLMSG_DATA(nlh);
6811da177e4SLinus Torvalds 	struct xfrm_state *x;
6821da177e4SLinus Torvalds 	struct sk_buff *resp_skb;
683eb2971b6SMasahide NAKAMURA 	int err = -ESRCH;
6841da177e4SLinus Torvalds 
685*22e70050SChristoph Hellwig 	x = xfrm_user_state_lookup(p, xfrma, &err);
6861da177e4SLinus Torvalds 	if (x == NULL)
6871da177e4SLinus Torvalds 		goto out_noput;
6881da177e4SLinus Torvalds 
6891da177e4SLinus Torvalds 	resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
6901da177e4SLinus Torvalds 	if (IS_ERR(resp_skb)) {
6911da177e4SLinus Torvalds 		err = PTR_ERR(resp_skb);
6921da177e4SLinus Torvalds 	} else {
6931da177e4SLinus Torvalds 		err = netlink_unicast(xfrm_nl, resp_skb,
6941da177e4SLinus Torvalds 				      NETLINK_CB(skb).pid, MSG_DONTWAIT);
6951da177e4SLinus Torvalds 	}
6961da177e4SLinus Torvalds 	xfrm_state_put(x);
6971da177e4SLinus Torvalds out_noput:
6981da177e4SLinus Torvalds 	return err;
6991da177e4SLinus Torvalds }
7001da177e4SLinus Torvalds 
7011da177e4SLinus Torvalds static int verify_userspi_info(struct xfrm_userspi_info *p)
7021da177e4SLinus Torvalds {
7031da177e4SLinus Torvalds 	switch (p->info.id.proto) {
7041da177e4SLinus Torvalds 	case IPPROTO_AH:
7051da177e4SLinus Torvalds 	case IPPROTO_ESP:
7061da177e4SLinus Torvalds 		break;
7071da177e4SLinus Torvalds 
7081da177e4SLinus Torvalds 	case IPPROTO_COMP:
7091da177e4SLinus Torvalds 		/* IPCOMP spi is 16-bits. */
7101da177e4SLinus Torvalds 		if (p->max >= 0x10000)
7111da177e4SLinus Torvalds 			return -EINVAL;
7121da177e4SLinus Torvalds 		break;
7131da177e4SLinus Torvalds 
7141da177e4SLinus Torvalds 	default:
7151da177e4SLinus Torvalds 		return -EINVAL;
7161da177e4SLinus Torvalds 	};
7171da177e4SLinus Torvalds 
7181da177e4SLinus Torvalds 	if (p->min > p->max)
7191da177e4SLinus Torvalds 		return -EINVAL;
7201da177e4SLinus Torvalds 
7211da177e4SLinus Torvalds 	return 0;
7221da177e4SLinus Torvalds }
7231da177e4SLinus Torvalds 
724*22e70050SChristoph Hellwig static int xfrm_alloc_userspi(struct sk_buff *skb, struct nlmsghdr *nlh,
725*22e70050SChristoph Hellwig 		struct rtattr **xfrma)
7261da177e4SLinus Torvalds {
7271da177e4SLinus Torvalds 	struct xfrm_state *x;
7281da177e4SLinus Torvalds 	struct xfrm_userspi_info *p;
7291da177e4SLinus Torvalds 	struct sk_buff *resp_skb;
7301da177e4SLinus Torvalds 	xfrm_address_t *daddr;
7311da177e4SLinus Torvalds 	int family;
7321da177e4SLinus Torvalds 	int err;
7331da177e4SLinus Torvalds 
7341da177e4SLinus Torvalds 	p = NLMSG_DATA(nlh);
7351da177e4SLinus Torvalds 	err = verify_userspi_info(p);
7361da177e4SLinus Torvalds 	if (err)
7371da177e4SLinus Torvalds 		goto out_noput;
7381da177e4SLinus Torvalds 
7391da177e4SLinus Torvalds 	family = p->info.family;
7401da177e4SLinus Torvalds 	daddr = &p->info.id.daddr;
7411da177e4SLinus Torvalds 
7421da177e4SLinus Torvalds 	x = NULL;
7431da177e4SLinus Torvalds 	if (p->info.seq) {
7441da177e4SLinus Torvalds 		x = xfrm_find_acq_byseq(p->info.seq);
7451da177e4SLinus Torvalds 		if (x && xfrm_addr_cmp(&x->id.daddr, daddr, family)) {
7461da177e4SLinus Torvalds 			xfrm_state_put(x);
7471da177e4SLinus Torvalds 			x = NULL;
7481da177e4SLinus Torvalds 		}
7491da177e4SLinus Torvalds 	}
7501da177e4SLinus Torvalds 
7511da177e4SLinus Torvalds 	if (!x)
7521da177e4SLinus Torvalds 		x = xfrm_find_acq(p->info.mode, p->info.reqid,
7531da177e4SLinus Torvalds 				  p->info.id.proto, daddr,
7541da177e4SLinus Torvalds 				  &p->info.saddr, 1,
7551da177e4SLinus Torvalds 				  family);
7561da177e4SLinus Torvalds 	err = -ENOENT;
7571da177e4SLinus Torvalds 	if (x == NULL)
7581da177e4SLinus Torvalds 		goto out_noput;
7591da177e4SLinus Torvalds 
7601da177e4SLinus Torvalds 	resp_skb = ERR_PTR(-ENOENT);
7611da177e4SLinus Torvalds 
7621da177e4SLinus Torvalds 	spin_lock_bh(&x->lock);
7631da177e4SLinus Torvalds 	if (x->km.state != XFRM_STATE_DEAD) {
7641da177e4SLinus Torvalds 		xfrm_alloc_spi(x, htonl(p->min), htonl(p->max));
7651da177e4SLinus Torvalds 		if (x->id.spi)
7661da177e4SLinus Torvalds 			resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
7671da177e4SLinus Torvalds 	}
7681da177e4SLinus Torvalds 	spin_unlock_bh(&x->lock);
7691da177e4SLinus Torvalds 
7701da177e4SLinus Torvalds 	if (IS_ERR(resp_skb)) {
7711da177e4SLinus Torvalds 		err = PTR_ERR(resp_skb);
7721da177e4SLinus Torvalds 		goto out;
7731da177e4SLinus Torvalds 	}
7741da177e4SLinus Torvalds 
7751da177e4SLinus Torvalds 	err = netlink_unicast(xfrm_nl, resp_skb,
7761da177e4SLinus Torvalds 			      NETLINK_CB(skb).pid, MSG_DONTWAIT);
7771da177e4SLinus Torvalds 
7781da177e4SLinus Torvalds out:
7791da177e4SLinus Torvalds 	xfrm_state_put(x);
7801da177e4SLinus Torvalds out_noput:
7811da177e4SLinus Torvalds 	return err;
7821da177e4SLinus Torvalds }
7831da177e4SLinus Torvalds 
784b798a9edSJamal Hadi Salim static int verify_policy_dir(u8 dir)
7851da177e4SLinus Torvalds {
7861da177e4SLinus Torvalds 	switch (dir) {
7871da177e4SLinus Torvalds 	case XFRM_POLICY_IN:
7881da177e4SLinus Torvalds 	case XFRM_POLICY_OUT:
7891da177e4SLinus Torvalds 	case XFRM_POLICY_FWD:
7901da177e4SLinus Torvalds 		break;
7911da177e4SLinus Torvalds 
7921da177e4SLinus Torvalds 	default:
7931da177e4SLinus Torvalds 		return -EINVAL;
7941da177e4SLinus Torvalds 	};
7951da177e4SLinus Torvalds 
7961da177e4SLinus Torvalds 	return 0;
7971da177e4SLinus Torvalds }
7981da177e4SLinus Torvalds 
799b798a9edSJamal Hadi Salim static int verify_policy_type(u8 type)
800f7b6983fSMasahide NAKAMURA {
801f7b6983fSMasahide NAKAMURA 	switch (type) {
802f7b6983fSMasahide NAKAMURA 	case XFRM_POLICY_TYPE_MAIN:
803f7b6983fSMasahide NAKAMURA #ifdef CONFIG_XFRM_SUB_POLICY
804f7b6983fSMasahide NAKAMURA 	case XFRM_POLICY_TYPE_SUB:
805f7b6983fSMasahide NAKAMURA #endif
806f7b6983fSMasahide NAKAMURA 		break;
807f7b6983fSMasahide NAKAMURA 
808f7b6983fSMasahide NAKAMURA 	default:
809f7b6983fSMasahide NAKAMURA 		return -EINVAL;
810f7b6983fSMasahide NAKAMURA 	};
811f7b6983fSMasahide NAKAMURA 
812f7b6983fSMasahide NAKAMURA 	return 0;
813f7b6983fSMasahide NAKAMURA }
814f7b6983fSMasahide NAKAMURA 
8151da177e4SLinus Torvalds static int verify_newpolicy_info(struct xfrm_userpolicy_info *p)
8161da177e4SLinus Torvalds {
8171da177e4SLinus Torvalds 	switch (p->share) {
8181da177e4SLinus Torvalds 	case XFRM_SHARE_ANY:
8191da177e4SLinus Torvalds 	case XFRM_SHARE_SESSION:
8201da177e4SLinus Torvalds 	case XFRM_SHARE_USER:
8211da177e4SLinus Torvalds 	case XFRM_SHARE_UNIQUE:
8221da177e4SLinus Torvalds 		break;
8231da177e4SLinus Torvalds 
8241da177e4SLinus Torvalds 	default:
8251da177e4SLinus Torvalds 		return -EINVAL;
8261da177e4SLinus Torvalds 	};
8271da177e4SLinus Torvalds 
8281da177e4SLinus Torvalds 	switch (p->action) {
8291da177e4SLinus Torvalds 	case XFRM_POLICY_ALLOW:
8301da177e4SLinus Torvalds 	case XFRM_POLICY_BLOCK:
8311da177e4SLinus Torvalds 		break;
8321da177e4SLinus Torvalds 
8331da177e4SLinus Torvalds 	default:
8341da177e4SLinus Torvalds 		return -EINVAL;
8351da177e4SLinus Torvalds 	};
8361da177e4SLinus Torvalds 
8371da177e4SLinus Torvalds 	switch (p->sel.family) {
8381da177e4SLinus Torvalds 	case AF_INET:
8391da177e4SLinus Torvalds 		break;
8401da177e4SLinus Torvalds 
8411da177e4SLinus Torvalds 	case AF_INET6:
8421da177e4SLinus Torvalds #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
8431da177e4SLinus Torvalds 		break;
8441da177e4SLinus Torvalds #else
8451da177e4SLinus Torvalds 		return  -EAFNOSUPPORT;
8461da177e4SLinus Torvalds #endif
8471da177e4SLinus Torvalds 
8481da177e4SLinus Torvalds 	default:
8491da177e4SLinus Torvalds 		return -EINVAL;
8501da177e4SLinus Torvalds 	};
8511da177e4SLinus Torvalds 
8521da177e4SLinus Torvalds 	return verify_policy_dir(p->dir);
8531da177e4SLinus Torvalds }
8541da177e4SLinus Torvalds 
855df71837dSTrent Jaeger static int copy_from_user_sec_ctx(struct xfrm_policy *pol, struct rtattr **xfrma)
856df71837dSTrent Jaeger {
857df71837dSTrent Jaeger 	struct rtattr *rt = xfrma[XFRMA_SEC_CTX-1];
858df71837dSTrent Jaeger 	struct xfrm_user_sec_ctx *uctx;
859df71837dSTrent Jaeger 
860df71837dSTrent Jaeger 	if (!rt)
861df71837dSTrent Jaeger 		return 0;
862df71837dSTrent Jaeger 
863df71837dSTrent Jaeger 	uctx = RTA_DATA(rt);
864df71837dSTrent Jaeger 	return security_xfrm_policy_alloc(pol, uctx);
865df71837dSTrent Jaeger }
866df71837dSTrent Jaeger 
8671da177e4SLinus Torvalds static void copy_templates(struct xfrm_policy *xp, struct xfrm_user_tmpl *ut,
8681da177e4SLinus Torvalds 			   int nr)
8691da177e4SLinus Torvalds {
8701da177e4SLinus Torvalds 	int i;
8711da177e4SLinus Torvalds 
8721da177e4SLinus Torvalds 	xp->xfrm_nr = nr;
8731da177e4SLinus Torvalds 	for (i = 0; i < nr; i++, ut++) {
8741da177e4SLinus Torvalds 		struct xfrm_tmpl *t = &xp->xfrm_vec[i];
8751da177e4SLinus Torvalds 
8761da177e4SLinus Torvalds 		memcpy(&t->id, &ut->id, sizeof(struct xfrm_id));
8771da177e4SLinus Torvalds 		memcpy(&t->saddr, &ut->saddr,
8781da177e4SLinus Torvalds 		       sizeof(xfrm_address_t));
8791da177e4SLinus Torvalds 		t->reqid = ut->reqid;
8801da177e4SLinus Torvalds 		t->mode = ut->mode;
8811da177e4SLinus Torvalds 		t->share = ut->share;
8821da177e4SLinus Torvalds 		t->optional = ut->optional;
8831da177e4SLinus Torvalds 		t->aalgos = ut->aalgos;
8841da177e4SLinus Torvalds 		t->ealgos = ut->ealgos;
8851da177e4SLinus Torvalds 		t->calgos = ut->calgos;
8868511d01dSMiika Komu 		t->encap_family = ut->family;
8871da177e4SLinus Torvalds 	}
8881da177e4SLinus Torvalds }
8891da177e4SLinus Torvalds 
890b4ad86bfSDavid S. Miller static int validate_tmpl(int nr, struct xfrm_user_tmpl *ut, u16 family)
891b4ad86bfSDavid S. Miller {
892b4ad86bfSDavid S. Miller 	int i;
893b4ad86bfSDavid S. Miller 
894b4ad86bfSDavid S. Miller 	if (nr > XFRM_MAX_DEPTH)
895b4ad86bfSDavid S. Miller 		return -EINVAL;
896b4ad86bfSDavid S. Miller 
897b4ad86bfSDavid S. Miller 	for (i = 0; i < nr; i++) {
898b4ad86bfSDavid S. Miller 		/* We never validated the ut->family value, so many
899b4ad86bfSDavid S. Miller 		 * applications simply leave it at zero.  The check was
900b4ad86bfSDavid S. Miller 		 * never made and ut->family was ignored because all
901b4ad86bfSDavid S. Miller 		 * templates could be assumed to have the same family as
902b4ad86bfSDavid S. Miller 		 * the policy itself.  Now that we will have ipv4-in-ipv6
903b4ad86bfSDavid S. Miller 		 * and ipv6-in-ipv4 tunnels, this is no longer true.
904b4ad86bfSDavid S. Miller 		 */
905b4ad86bfSDavid S. Miller 		if (!ut[i].family)
906b4ad86bfSDavid S. Miller 			ut[i].family = family;
907b4ad86bfSDavid S. Miller 
908b4ad86bfSDavid S. Miller 		switch (ut[i].family) {
909b4ad86bfSDavid S. Miller 		case AF_INET:
910b4ad86bfSDavid S. Miller 			break;
911b4ad86bfSDavid S. Miller #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
912b4ad86bfSDavid S. Miller 		case AF_INET6:
913b4ad86bfSDavid S. Miller 			break;
914b4ad86bfSDavid S. Miller #endif
915b4ad86bfSDavid S. Miller 		default:
916b4ad86bfSDavid S. Miller 			return -EINVAL;
917b4ad86bfSDavid S. Miller 		};
918b4ad86bfSDavid S. Miller 	}
919b4ad86bfSDavid S. Miller 
920b4ad86bfSDavid S. Miller 	return 0;
921b4ad86bfSDavid S. Miller }
922b4ad86bfSDavid S. Miller 
9231da177e4SLinus Torvalds static int copy_from_user_tmpl(struct xfrm_policy *pol, struct rtattr **xfrma)
9241da177e4SLinus Torvalds {
9251da177e4SLinus Torvalds 	struct rtattr *rt = xfrma[XFRMA_TMPL-1];
9261da177e4SLinus Torvalds 
9271da177e4SLinus Torvalds 	if (!rt) {
9281da177e4SLinus Torvalds 		pol->xfrm_nr = 0;
9291da177e4SLinus Torvalds 	} else {
930b4ad86bfSDavid S. Miller 		struct xfrm_user_tmpl *utmpl = RTA_DATA(rt);
931b4ad86bfSDavid S. Miller 		int nr = (rt->rta_len - sizeof(*rt)) / sizeof(*utmpl);
932b4ad86bfSDavid S. Miller 		int err;
9331da177e4SLinus Torvalds 
934b4ad86bfSDavid S. Miller 		err = validate_tmpl(nr, utmpl, pol->family);
935b4ad86bfSDavid S. Miller 		if (err)
936b4ad86bfSDavid S. Miller 			return err;
9371da177e4SLinus Torvalds 
9381da177e4SLinus Torvalds 		copy_templates(pol, RTA_DATA(rt), nr);
9391da177e4SLinus Torvalds 	}
9401da177e4SLinus Torvalds 	return 0;
9411da177e4SLinus Torvalds }
9421da177e4SLinus Torvalds 
943f7b6983fSMasahide NAKAMURA static int copy_from_user_policy_type(u8 *tp, struct rtattr **xfrma)
944f7b6983fSMasahide NAKAMURA {
945f7b6983fSMasahide NAKAMURA 	struct rtattr *rt = xfrma[XFRMA_POLICY_TYPE-1];
946f7b6983fSMasahide NAKAMURA 	struct xfrm_userpolicy_type *upt;
947b798a9edSJamal Hadi Salim 	u8 type = XFRM_POLICY_TYPE_MAIN;
948f7b6983fSMasahide NAKAMURA 	int err;
949f7b6983fSMasahide NAKAMURA 
950f7b6983fSMasahide NAKAMURA 	if (rt) {
951f7b6983fSMasahide NAKAMURA 		if (rt->rta_len < sizeof(*upt))
952f7b6983fSMasahide NAKAMURA 			return -EINVAL;
953f7b6983fSMasahide NAKAMURA 
954f7b6983fSMasahide NAKAMURA 		upt = RTA_DATA(rt);
955f7b6983fSMasahide NAKAMURA 		type = upt->type;
956f7b6983fSMasahide NAKAMURA 	}
957f7b6983fSMasahide NAKAMURA 
958f7b6983fSMasahide NAKAMURA 	err = verify_policy_type(type);
959f7b6983fSMasahide NAKAMURA 	if (err)
960f7b6983fSMasahide NAKAMURA 		return err;
961f7b6983fSMasahide NAKAMURA 
962f7b6983fSMasahide NAKAMURA 	*tp = type;
963f7b6983fSMasahide NAKAMURA 	return 0;
964f7b6983fSMasahide NAKAMURA }
965f7b6983fSMasahide NAKAMURA 
9661da177e4SLinus Torvalds static void copy_from_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p)
9671da177e4SLinus Torvalds {
9681da177e4SLinus Torvalds 	xp->priority = p->priority;
9691da177e4SLinus Torvalds 	xp->index = p->index;
9701da177e4SLinus Torvalds 	memcpy(&xp->selector, &p->sel, sizeof(xp->selector));
9711da177e4SLinus Torvalds 	memcpy(&xp->lft, &p->lft, sizeof(xp->lft));
9721da177e4SLinus Torvalds 	xp->action = p->action;
9731da177e4SLinus Torvalds 	xp->flags = p->flags;
9741da177e4SLinus Torvalds 	xp->family = p->sel.family;
9751da177e4SLinus Torvalds 	/* XXX xp->share = p->share; */
9761da177e4SLinus Torvalds }
9771da177e4SLinus Torvalds 
9781da177e4SLinus Torvalds static void copy_to_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p, int dir)
9791da177e4SLinus Torvalds {
9801da177e4SLinus Torvalds 	memcpy(&p->sel, &xp->selector, sizeof(p->sel));
9811da177e4SLinus Torvalds 	memcpy(&p->lft, &xp->lft, sizeof(p->lft));
9821da177e4SLinus Torvalds 	memcpy(&p->curlft, &xp->curlft, sizeof(p->curlft));
9831da177e4SLinus Torvalds 	p->priority = xp->priority;
9841da177e4SLinus Torvalds 	p->index = xp->index;
9851da177e4SLinus Torvalds 	p->sel.family = xp->family;
9861da177e4SLinus Torvalds 	p->dir = dir;
9871da177e4SLinus Torvalds 	p->action = xp->action;
9881da177e4SLinus Torvalds 	p->flags = xp->flags;
9891da177e4SLinus Torvalds 	p->share = XFRM_SHARE_ANY; /* XXX xp->share */
9901da177e4SLinus Torvalds }
9911da177e4SLinus Torvalds 
9921da177e4SLinus Torvalds static struct xfrm_policy *xfrm_policy_construct(struct xfrm_userpolicy_info *p, struct rtattr **xfrma, int *errp)
9931da177e4SLinus Torvalds {
9941da177e4SLinus Torvalds 	struct xfrm_policy *xp = xfrm_policy_alloc(GFP_KERNEL);
9951da177e4SLinus Torvalds 	int err;
9961da177e4SLinus Torvalds 
9971da177e4SLinus Torvalds 	if (!xp) {
9981da177e4SLinus Torvalds 		*errp = -ENOMEM;
9991da177e4SLinus Torvalds 		return NULL;
10001da177e4SLinus Torvalds 	}
10011da177e4SLinus Torvalds 
10021da177e4SLinus Torvalds 	copy_from_user_policy(xp, p);
1003df71837dSTrent Jaeger 
1004f7b6983fSMasahide NAKAMURA 	err = copy_from_user_policy_type(&xp->type, xfrma);
1005f7b6983fSMasahide NAKAMURA 	if (err)
1006f7b6983fSMasahide NAKAMURA 		goto error;
1007f7b6983fSMasahide NAKAMURA 
1008df71837dSTrent Jaeger 	if (!(err = copy_from_user_tmpl(xp, xfrma)))
1009df71837dSTrent Jaeger 		err = copy_from_user_sec_ctx(xp, xfrma);
1010f7b6983fSMasahide NAKAMURA 	if (err)
1011f7b6983fSMasahide NAKAMURA 		goto error;
10121da177e4SLinus Torvalds 
10131da177e4SLinus Torvalds 	return xp;
1014f7b6983fSMasahide NAKAMURA  error:
1015f7b6983fSMasahide NAKAMURA 	*errp = err;
1016f7b6983fSMasahide NAKAMURA 	kfree(xp);
1017f7b6983fSMasahide NAKAMURA 	return NULL;
10181da177e4SLinus Torvalds }
10191da177e4SLinus Torvalds 
1020*22e70050SChristoph Hellwig static int xfrm_add_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
1021*22e70050SChristoph Hellwig 		struct rtattr **xfrma)
10221da177e4SLinus Torvalds {
10231da177e4SLinus Torvalds 	struct xfrm_userpolicy_info *p = NLMSG_DATA(nlh);
10241da177e4SLinus Torvalds 	struct xfrm_policy *xp;
102526b15dadSJamal Hadi Salim 	struct km_event c;
10261da177e4SLinus Torvalds 	int err;
10271da177e4SLinus Torvalds 	int excl;
10281da177e4SLinus Torvalds 
10291da177e4SLinus Torvalds 	err = verify_newpolicy_info(p);
10301da177e4SLinus Torvalds 	if (err)
10311da177e4SLinus Torvalds 		return err;
1032*22e70050SChristoph Hellwig 	err = verify_sec_ctx_len(xfrma);
1033df71837dSTrent Jaeger 	if (err)
1034df71837dSTrent Jaeger 		return err;
10351da177e4SLinus Torvalds 
1036*22e70050SChristoph Hellwig 	xp = xfrm_policy_construct(p, xfrma, &err);
10371da177e4SLinus Torvalds 	if (!xp)
10381da177e4SLinus Torvalds 		return err;
10391da177e4SLinus Torvalds 
104026b15dadSJamal Hadi Salim 	/* shouldnt excl be based on nlh flags??
104126b15dadSJamal Hadi Salim 	 * Aha! this is anti-netlink really i.e  more pfkey derived
104226b15dadSJamal Hadi Salim 	 * in netlink excl is a flag and you wouldnt need
104326b15dadSJamal Hadi Salim 	 * a type XFRM_MSG_UPDPOLICY - JHS */
10441da177e4SLinus Torvalds 	excl = nlh->nlmsg_type == XFRM_MSG_NEWPOLICY;
10451da177e4SLinus Torvalds 	err = xfrm_policy_insert(p->dir, xp, excl);
1046161a09e7SJoy Latten 	xfrm_audit_log(NETLINK_CB(skb).loginuid, NETLINK_CB(skb).sid,
1047161a09e7SJoy Latten 		       AUDIT_MAC_IPSEC_DELSPD, err ? 0 : 1, xp, NULL);
1048161a09e7SJoy Latten 
10491da177e4SLinus Torvalds 	if (err) {
10505f8ac64bSTrent Jaeger 		security_xfrm_policy_free(xp);
10511da177e4SLinus Torvalds 		kfree(xp);
10521da177e4SLinus Torvalds 		return err;
10531da177e4SLinus Torvalds 	}
10541da177e4SLinus Torvalds 
1055f60f6b8fSHerbert Xu 	c.event = nlh->nlmsg_type;
105626b15dadSJamal Hadi Salim 	c.seq = nlh->nlmsg_seq;
105726b15dadSJamal Hadi Salim 	c.pid = nlh->nlmsg_pid;
105826b15dadSJamal Hadi Salim 	km_policy_notify(xp, p->dir, &c);
105926b15dadSJamal Hadi Salim 
10601da177e4SLinus Torvalds 	xfrm_pol_put(xp);
10611da177e4SLinus Torvalds 
10621da177e4SLinus Torvalds 	return 0;
10631da177e4SLinus Torvalds }
10641da177e4SLinus Torvalds 
10651da177e4SLinus Torvalds static int copy_to_user_tmpl(struct xfrm_policy *xp, struct sk_buff *skb)
10661da177e4SLinus Torvalds {
10671da177e4SLinus Torvalds 	struct xfrm_user_tmpl vec[XFRM_MAX_DEPTH];
10681da177e4SLinus Torvalds 	int i;
10691da177e4SLinus Torvalds 
10701da177e4SLinus Torvalds 	if (xp->xfrm_nr == 0)
10711da177e4SLinus Torvalds 		return 0;
10721da177e4SLinus Torvalds 
10731da177e4SLinus Torvalds 	for (i = 0; i < xp->xfrm_nr; i++) {
10741da177e4SLinus Torvalds 		struct xfrm_user_tmpl *up = &vec[i];
10751da177e4SLinus Torvalds 		struct xfrm_tmpl *kp = &xp->xfrm_vec[i];
10761da177e4SLinus Torvalds 
10771da177e4SLinus Torvalds 		memcpy(&up->id, &kp->id, sizeof(up->id));
10788511d01dSMiika Komu 		up->family = kp->encap_family;
10791da177e4SLinus Torvalds 		memcpy(&up->saddr, &kp->saddr, sizeof(up->saddr));
10801da177e4SLinus Torvalds 		up->reqid = kp->reqid;
10811da177e4SLinus Torvalds 		up->mode = kp->mode;
10821da177e4SLinus Torvalds 		up->share = kp->share;
10831da177e4SLinus Torvalds 		up->optional = kp->optional;
10841da177e4SLinus Torvalds 		up->aalgos = kp->aalgos;
10851da177e4SLinus Torvalds 		up->ealgos = kp->ealgos;
10861da177e4SLinus Torvalds 		up->calgos = kp->calgos;
10871da177e4SLinus Torvalds 	}
10881da177e4SLinus Torvalds 	RTA_PUT(skb, XFRMA_TMPL,
10891da177e4SLinus Torvalds 		(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr),
10901da177e4SLinus Torvalds 		vec);
10911da177e4SLinus Torvalds 
10921da177e4SLinus Torvalds 	return 0;
10931da177e4SLinus Torvalds 
10941da177e4SLinus Torvalds rtattr_failure:
10951da177e4SLinus Torvalds 	return -1;
10961da177e4SLinus Torvalds }
10971da177e4SLinus Torvalds 
10980d681623SSerge Hallyn static int copy_sec_ctx(struct xfrm_sec_ctx *s, struct sk_buff *skb)
1099df71837dSTrent Jaeger {
11000d681623SSerge Hallyn 	int ctx_size = sizeof(struct xfrm_sec_ctx) + s->ctx_len;
1101df71837dSTrent Jaeger 	struct rtattr *rt = __RTA_PUT(skb, XFRMA_SEC_CTX, ctx_size);
1102df71837dSTrent Jaeger 	struct xfrm_user_sec_ctx *uctx = RTA_DATA(rt);
1103df71837dSTrent Jaeger 
1104df71837dSTrent Jaeger 	uctx->exttype = XFRMA_SEC_CTX;
1105df71837dSTrent Jaeger 	uctx->len = ctx_size;
11060d681623SSerge Hallyn 	uctx->ctx_doi = s->ctx_doi;
11070d681623SSerge Hallyn 	uctx->ctx_alg = s->ctx_alg;
11080d681623SSerge Hallyn 	uctx->ctx_len = s->ctx_len;
11090d681623SSerge Hallyn 	memcpy(uctx + 1, s->ctx_str, s->ctx_len);
1110df71837dSTrent Jaeger  	return 0;
1111df71837dSTrent Jaeger 
1112df71837dSTrent Jaeger  rtattr_failure:
1113df71837dSTrent Jaeger 	return -1;
1114df71837dSTrent Jaeger }
1115df71837dSTrent Jaeger 
11160d681623SSerge Hallyn static inline int copy_to_user_state_sec_ctx(struct xfrm_state *x, struct sk_buff *skb)
11170d681623SSerge Hallyn {
11180d681623SSerge Hallyn 	if (x->security) {
11190d681623SSerge Hallyn 		return copy_sec_ctx(x->security, skb);
11200d681623SSerge Hallyn 	}
11210d681623SSerge Hallyn 	return 0;
11220d681623SSerge Hallyn }
11230d681623SSerge Hallyn 
11240d681623SSerge Hallyn static inline int copy_to_user_sec_ctx(struct xfrm_policy *xp, struct sk_buff *skb)
11250d681623SSerge Hallyn {
11260d681623SSerge Hallyn 	if (xp->security) {
11270d681623SSerge Hallyn 		return copy_sec_ctx(xp->security, skb);
11280d681623SSerge Hallyn 	}
11290d681623SSerge Hallyn 	return 0;
11300d681623SSerge Hallyn }
11310d681623SSerge Hallyn 
1132f7b6983fSMasahide NAKAMURA #ifdef CONFIG_XFRM_SUB_POLICY
1133b798a9edSJamal Hadi Salim static int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
1134f7b6983fSMasahide NAKAMURA {
1135f7b6983fSMasahide NAKAMURA 	struct xfrm_userpolicy_type upt;
1136f7b6983fSMasahide NAKAMURA 
1137f7b6983fSMasahide NAKAMURA 	memset(&upt, 0, sizeof(upt));
11381459bb36SJamal Hadi Salim 	upt.type = type;
1139f7b6983fSMasahide NAKAMURA 
1140f7b6983fSMasahide NAKAMURA 	RTA_PUT(skb, XFRMA_POLICY_TYPE, sizeof(upt), &upt);
1141f7b6983fSMasahide NAKAMURA 
1142f7b6983fSMasahide NAKAMURA 	return 0;
1143f7b6983fSMasahide NAKAMURA 
1144f7b6983fSMasahide NAKAMURA rtattr_failure:
1145f7b6983fSMasahide NAKAMURA 	return -1;
1146f7b6983fSMasahide NAKAMURA }
1147f7b6983fSMasahide NAKAMURA 
1148f7b6983fSMasahide NAKAMURA #else
1149b798a9edSJamal Hadi Salim static inline int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
1150f7b6983fSMasahide NAKAMURA {
1151f7b6983fSMasahide NAKAMURA 	return 0;
1152f7b6983fSMasahide NAKAMURA }
1153f7b6983fSMasahide NAKAMURA #endif
1154f7b6983fSMasahide NAKAMURA 
11551da177e4SLinus Torvalds static int dump_one_policy(struct xfrm_policy *xp, int dir, int count, void *ptr)
11561da177e4SLinus Torvalds {
11571da177e4SLinus Torvalds 	struct xfrm_dump_info *sp = ptr;
11581da177e4SLinus Torvalds 	struct xfrm_userpolicy_info *p;
11591da177e4SLinus Torvalds 	struct sk_buff *in_skb = sp->in_skb;
11601da177e4SLinus Torvalds 	struct sk_buff *skb = sp->out_skb;
11611da177e4SLinus Torvalds 	struct nlmsghdr *nlh;
11621da177e4SLinus Torvalds 	unsigned char *b = skb->tail;
11631da177e4SLinus Torvalds 
11641da177e4SLinus Torvalds 	if (sp->this_idx < sp->start_idx)
11651da177e4SLinus Torvalds 		goto out;
11661da177e4SLinus Torvalds 
11671da177e4SLinus Torvalds 	nlh = NLMSG_PUT(skb, NETLINK_CB(in_skb).pid,
11681da177e4SLinus Torvalds 			sp->nlmsg_seq,
11691da177e4SLinus Torvalds 			XFRM_MSG_NEWPOLICY, sizeof(*p));
11701da177e4SLinus Torvalds 	p = NLMSG_DATA(nlh);
11711da177e4SLinus Torvalds 	nlh->nlmsg_flags = sp->nlmsg_flags;
11721da177e4SLinus Torvalds 
11731da177e4SLinus Torvalds 	copy_to_user_policy(xp, p, dir);
11741da177e4SLinus Torvalds 	if (copy_to_user_tmpl(xp, skb) < 0)
11751da177e4SLinus Torvalds 		goto nlmsg_failure;
1176df71837dSTrent Jaeger 	if (copy_to_user_sec_ctx(xp, skb))
1177df71837dSTrent Jaeger 		goto nlmsg_failure;
11781459bb36SJamal Hadi Salim 	if (copy_to_user_policy_type(xp->type, skb) < 0)
1179f7b6983fSMasahide NAKAMURA 		goto nlmsg_failure;
11801da177e4SLinus Torvalds 
11811da177e4SLinus Torvalds 	nlh->nlmsg_len = skb->tail - b;
11821da177e4SLinus Torvalds out:
11831da177e4SLinus Torvalds 	sp->this_idx++;
11841da177e4SLinus Torvalds 	return 0;
11851da177e4SLinus Torvalds 
11861da177e4SLinus Torvalds nlmsg_failure:
11871da177e4SLinus Torvalds 	skb_trim(skb, b - skb->data);
11881da177e4SLinus Torvalds 	return -1;
11891da177e4SLinus Torvalds }
11901da177e4SLinus Torvalds 
11911da177e4SLinus Torvalds static int xfrm_dump_policy(struct sk_buff *skb, struct netlink_callback *cb)
11921da177e4SLinus Torvalds {
11931da177e4SLinus Torvalds 	struct xfrm_dump_info info;
11941da177e4SLinus Torvalds 
11951da177e4SLinus Torvalds 	info.in_skb = cb->skb;
11961da177e4SLinus Torvalds 	info.out_skb = skb;
11971da177e4SLinus Torvalds 	info.nlmsg_seq = cb->nlh->nlmsg_seq;
11981da177e4SLinus Torvalds 	info.nlmsg_flags = NLM_F_MULTI;
11991da177e4SLinus Torvalds 	info.this_idx = 0;
12001da177e4SLinus Torvalds 	info.start_idx = cb->args[0];
1201f7b6983fSMasahide NAKAMURA 	(void) xfrm_policy_walk(XFRM_POLICY_TYPE_MAIN, dump_one_policy, &info);
1202f7b6983fSMasahide NAKAMURA #ifdef CONFIG_XFRM_SUB_POLICY
1203f7b6983fSMasahide NAKAMURA 	(void) xfrm_policy_walk(XFRM_POLICY_TYPE_SUB, dump_one_policy, &info);
1204f7b6983fSMasahide NAKAMURA #endif
12051da177e4SLinus Torvalds 	cb->args[0] = info.this_idx;
12061da177e4SLinus Torvalds 
12071da177e4SLinus Torvalds 	return skb->len;
12081da177e4SLinus Torvalds }
12091da177e4SLinus Torvalds 
12101da177e4SLinus Torvalds static struct sk_buff *xfrm_policy_netlink(struct sk_buff *in_skb,
12111da177e4SLinus Torvalds 					  struct xfrm_policy *xp,
12121da177e4SLinus Torvalds 					  int dir, u32 seq)
12131da177e4SLinus Torvalds {
12141da177e4SLinus Torvalds 	struct xfrm_dump_info info;
12151da177e4SLinus Torvalds 	struct sk_buff *skb;
12161da177e4SLinus Torvalds 
12171da177e4SLinus Torvalds 	skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
12181da177e4SLinus Torvalds 	if (!skb)
12191da177e4SLinus Torvalds 		return ERR_PTR(-ENOMEM);
12201da177e4SLinus Torvalds 
12211da177e4SLinus Torvalds 	info.in_skb = in_skb;
12221da177e4SLinus Torvalds 	info.out_skb = skb;
12231da177e4SLinus Torvalds 	info.nlmsg_seq = seq;
12241da177e4SLinus Torvalds 	info.nlmsg_flags = 0;
12251da177e4SLinus Torvalds 	info.this_idx = info.start_idx = 0;
12261da177e4SLinus Torvalds 
12271da177e4SLinus Torvalds 	if (dump_one_policy(xp, dir, 0, &info) < 0) {
12281da177e4SLinus Torvalds 		kfree_skb(skb);
12291da177e4SLinus Torvalds 		return NULL;
12301da177e4SLinus Torvalds 	}
12311da177e4SLinus Torvalds 
12321da177e4SLinus Torvalds 	return skb;
12331da177e4SLinus Torvalds }
12341da177e4SLinus Torvalds 
1235*22e70050SChristoph Hellwig static int xfrm_get_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
1236*22e70050SChristoph Hellwig 		struct rtattr **xfrma)
12371da177e4SLinus Torvalds {
12381da177e4SLinus Torvalds 	struct xfrm_policy *xp;
12391da177e4SLinus Torvalds 	struct xfrm_userpolicy_id *p;
1240b798a9edSJamal Hadi Salim 	u8 type = XFRM_POLICY_TYPE_MAIN;
12411da177e4SLinus Torvalds 	int err;
124226b15dadSJamal Hadi Salim 	struct km_event c;
12431da177e4SLinus Torvalds 	int delete;
12441da177e4SLinus Torvalds 
12451da177e4SLinus Torvalds 	p = NLMSG_DATA(nlh);
12461da177e4SLinus Torvalds 	delete = nlh->nlmsg_type == XFRM_MSG_DELPOLICY;
12471da177e4SLinus Torvalds 
1248*22e70050SChristoph Hellwig 	err = copy_from_user_policy_type(&type, xfrma);
1249f7b6983fSMasahide NAKAMURA 	if (err)
1250f7b6983fSMasahide NAKAMURA 		return err;
1251f7b6983fSMasahide NAKAMURA 
12521da177e4SLinus Torvalds 	err = verify_policy_dir(p->dir);
12531da177e4SLinus Torvalds 	if (err)
12541da177e4SLinus Torvalds 		return err;
12551da177e4SLinus Torvalds 
12561da177e4SLinus Torvalds 	if (p->index)
1257f7b6983fSMasahide NAKAMURA 		xp = xfrm_policy_byid(type, p->dir, p->index, delete);
1258df71837dSTrent Jaeger 	else {
1259*22e70050SChristoph Hellwig 		struct rtattr *rt = xfrma[XFRMA_SEC_CTX-1];
1260df71837dSTrent Jaeger 		struct xfrm_policy tmp;
1261df71837dSTrent Jaeger 
1262*22e70050SChristoph Hellwig 		err = verify_sec_ctx_len(xfrma);
1263df71837dSTrent Jaeger 		if (err)
1264df71837dSTrent Jaeger 			return err;
1265df71837dSTrent Jaeger 
1266df71837dSTrent Jaeger 		memset(&tmp, 0, sizeof(struct xfrm_policy));
1267df71837dSTrent Jaeger 		if (rt) {
1268df71837dSTrent Jaeger 			struct xfrm_user_sec_ctx *uctx = RTA_DATA(rt);
1269df71837dSTrent Jaeger 
1270df71837dSTrent Jaeger 			if ((err = security_xfrm_policy_alloc(&tmp, uctx)))
1271df71837dSTrent Jaeger 				return err;
1272df71837dSTrent Jaeger 		}
1273f7b6983fSMasahide NAKAMURA 		xp = xfrm_policy_bysel_ctx(type, p->dir, &p->sel, tmp.security, delete);
1274df71837dSTrent Jaeger 		security_xfrm_policy_free(&tmp);
1275df71837dSTrent Jaeger 	}
1276161a09e7SJoy Latten 	if (delete)
1277161a09e7SJoy Latten 		xfrm_audit_log(NETLINK_CB(skb).loginuid, NETLINK_CB(skb).sid,
1278161a09e7SJoy Latten 			       AUDIT_MAC_IPSEC_DELSPD, (xp) ? 1 : 0, xp, NULL);
1279161a09e7SJoy Latten 
12801da177e4SLinus Torvalds 	if (xp == NULL)
12811da177e4SLinus Torvalds 		return -ENOENT;
12821da177e4SLinus Torvalds 
12831da177e4SLinus Torvalds 	if (!delete) {
12841da177e4SLinus Torvalds 		struct sk_buff *resp_skb;
12851da177e4SLinus Torvalds 
12861da177e4SLinus Torvalds 		resp_skb = xfrm_policy_netlink(skb, xp, p->dir, nlh->nlmsg_seq);
12871da177e4SLinus Torvalds 		if (IS_ERR(resp_skb)) {
12881da177e4SLinus Torvalds 			err = PTR_ERR(resp_skb);
12891da177e4SLinus Torvalds 		} else {
12901da177e4SLinus Torvalds 			err = netlink_unicast(xfrm_nl, resp_skb,
12911da177e4SLinus Torvalds 					      NETLINK_CB(skb).pid,
12921da177e4SLinus Torvalds 					      MSG_DONTWAIT);
12931da177e4SLinus Torvalds 		}
129426b15dadSJamal Hadi Salim 	} else {
12956f68dc37SDavid S. Miller 		if ((err = security_xfrm_policy_delete(xp)) != 0)
1296c8c05a8eSCatherine Zhang 			goto out;
1297e7443892SHerbert Xu 		c.data.byid = p->index;
1298f60f6b8fSHerbert Xu 		c.event = nlh->nlmsg_type;
129926b15dadSJamal Hadi Salim 		c.seq = nlh->nlmsg_seq;
130026b15dadSJamal Hadi Salim 		c.pid = nlh->nlmsg_pid;
130126b15dadSJamal Hadi Salim 		km_policy_notify(xp, p->dir, &c);
13021da177e4SLinus Torvalds 	}
13031da177e4SLinus Torvalds 
13041da177e4SLinus Torvalds 	xfrm_pol_put(xp);
13051da177e4SLinus Torvalds 
1306c8c05a8eSCatherine Zhang out:
13071da177e4SLinus Torvalds 	return err;
13081da177e4SLinus Torvalds }
13091da177e4SLinus Torvalds 
1310*22e70050SChristoph Hellwig static int xfrm_flush_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
1311*22e70050SChristoph Hellwig 		struct rtattr **xfrma)
13121da177e4SLinus Torvalds {
131326b15dadSJamal Hadi Salim 	struct km_event c;
13141da177e4SLinus Torvalds 	struct xfrm_usersa_flush *p = NLMSG_DATA(nlh);
1315161a09e7SJoy Latten 	struct xfrm_audit audit_info;
13161da177e4SLinus Torvalds 
1317161a09e7SJoy Latten 	audit_info.loginuid = NETLINK_CB(skb).loginuid;
1318161a09e7SJoy Latten 	audit_info.secid = NETLINK_CB(skb).sid;
1319161a09e7SJoy Latten 	xfrm_state_flush(p->proto, &audit_info);
1320bf08867fSHerbert Xu 	c.data.proto = p->proto;
1321f60f6b8fSHerbert Xu 	c.event = nlh->nlmsg_type;
132226b15dadSJamal Hadi Salim 	c.seq = nlh->nlmsg_seq;
132326b15dadSJamal Hadi Salim 	c.pid = nlh->nlmsg_pid;
132426b15dadSJamal Hadi Salim 	km_state_notify(NULL, &c);
132526b15dadSJamal Hadi Salim 
13261da177e4SLinus Torvalds 	return 0;
13271da177e4SLinus Torvalds }
13281da177e4SLinus Torvalds 
1329d51d081dSJamal Hadi Salim 
1330d51d081dSJamal Hadi Salim static int build_aevent(struct sk_buff *skb, struct xfrm_state *x, struct km_event *c)
1331d51d081dSJamal Hadi Salim {
1332d51d081dSJamal Hadi Salim 	struct xfrm_aevent_id *id;
1333d51d081dSJamal Hadi Salim 	struct nlmsghdr *nlh;
1334d51d081dSJamal Hadi Salim 	struct xfrm_lifetime_cur ltime;
1335d51d081dSJamal Hadi Salim 	unsigned char *b = skb->tail;
1336d51d081dSJamal Hadi Salim 
1337d51d081dSJamal Hadi Salim 	nlh = NLMSG_PUT(skb, c->pid, c->seq, XFRM_MSG_NEWAE, sizeof(*id));
1338d51d081dSJamal Hadi Salim 	id = NLMSG_DATA(nlh);
1339d51d081dSJamal Hadi Salim 	nlh->nlmsg_flags = 0;
1340d51d081dSJamal Hadi Salim 
13412b5f6dccSJamal Hadi Salim 	memcpy(&id->sa_id.daddr, &x->id.daddr,sizeof(x->id.daddr));
1342d51d081dSJamal Hadi Salim 	id->sa_id.spi = x->id.spi;
1343d51d081dSJamal Hadi Salim 	id->sa_id.family = x->props.family;
1344d51d081dSJamal Hadi Salim 	id->sa_id.proto = x->id.proto;
13452b5f6dccSJamal Hadi Salim 	memcpy(&id->saddr, &x->props.saddr,sizeof(x->props.saddr));
13462b5f6dccSJamal Hadi Salim 	id->reqid = x->props.reqid;
1347d51d081dSJamal Hadi Salim 	id->flags = c->data.aevent;
1348d51d081dSJamal Hadi Salim 
1349d51d081dSJamal Hadi Salim 	RTA_PUT(skb, XFRMA_REPLAY_VAL, sizeof(x->replay), &x->replay);
1350d51d081dSJamal Hadi Salim 
1351d51d081dSJamal Hadi Salim 	ltime.bytes = x->curlft.bytes;
1352d51d081dSJamal Hadi Salim 	ltime.packets = x->curlft.packets;
1353d51d081dSJamal Hadi Salim 	ltime.add_time = x->curlft.add_time;
1354d51d081dSJamal Hadi Salim 	ltime.use_time = x->curlft.use_time;
1355d51d081dSJamal Hadi Salim 
1356d51d081dSJamal Hadi Salim 	RTA_PUT(skb, XFRMA_LTIME_VAL, sizeof(struct xfrm_lifetime_cur), &ltime);
1357d51d081dSJamal Hadi Salim 
1358d51d081dSJamal Hadi Salim 	if (id->flags&XFRM_AE_RTHR) {
1359d51d081dSJamal Hadi Salim 		RTA_PUT(skb,XFRMA_REPLAY_THRESH,sizeof(u32),&x->replay_maxdiff);
1360d51d081dSJamal Hadi Salim 	}
1361d51d081dSJamal Hadi Salim 
1362d51d081dSJamal Hadi Salim 	if (id->flags&XFRM_AE_ETHR) {
1363d51d081dSJamal Hadi Salim 		u32 etimer = x->replay_maxage*10/HZ;
1364d51d081dSJamal Hadi Salim 		RTA_PUT(skb,XFRMA_ETIMER_THRESH,sizeof(u32),&etimer);
1365d51d081dSJamal Hadi Salim 	}
1366d51d081dSJamal Hadi Salim 
1367d51d081dSJamal Hadi Salim 	nlh->nlmsg_len = skb->tail - b;
1368d51d081dSJamal Hadi Salim 	return skb->len;
1369d51d081dSJamal Hadi Salim 
1370d51d081dSJamal Hadi Salim rtattr_failure:
1371d51d081dSJamal Hadi Salim nlmsg_failure:
1372d51d081dSJamal Hadi Salim 	skb_trim(skb, b - skb->data);
1373d51d081dSJamal Hadi Salim 	return -1;
1374d51d081dSJamal Hadi Salim }
1375d51d081dSJamal Hadi Salim 
1376*22e70050SChristoph Hellwig static int xfrm_get_ae(struct sk_buff *skb, struct nlmsghdr *nlh,
1377*22e70050SChristoph Hellwig 		struct rtattr **xfrma)
1378d51d081dSJamal Hadi Salim {
1379d51d081dSJamal Hadi Salim 	struct xfrm_state *x;
1380d51d081dSJamal Hadi Salim 	struct sk_buff *r_skb;
1381d51d081dSJamal Hadi Salim 	int err;
1382d51d081dSJamal Hadi Salim 	struct km_event c;
1383d51d081dSJamal Hadi Salim 	struct xfrm_aevent_id *p = NLMSG_DATA(nlh);
1384d51d081dSJamal Hadi Salim 	int len = NLMSG_LENGTH(sizeof(struct xfrm_aevent_id));
1385d51d081dSJamal Hadi Salim 	struct xfrm_usersa_id *id = &p->sa_id;
1386d51d081dSJamal Hadi Salim 
1387d51d081dSJamal Hadi Salim 	len += RTA_SPACE(sizeof(struct xfrm_replay_state));
1388d51d081dSJamal Hadi Salim 	len += RTA_SPACE(sizeof(struct xfrm_lifetime_cur));
1389d51d081dSJamal Hadi Salim 
1390d51d081dSJamal Hadi Salim 	if (p->flags&XFRM_AE_RTHR)
1391d51d081dSJamal Hadi Salim 		len+=RTA_SPACE(sizeof(u32));
1392d51d081dSJamal Hadi Salim 
1393d51d081dSJamal Hadi Salim 	if (p->flags&XFRM_AE_ETHR)
1394d51d081dSJamal Hadi Salim 		len+=RTA_SPACE(sizeof(u32));
1395d51d081dSJamal Hadi Salim 
1396d51d081dSJamal Hadi Salim 	r_skb = alloc_skb(len, GFP_ATOMIC);
1397d51d081dSJamal Hadi Salim 	if (r_skb == NULL)
1398d51d081dSJamal Hadi Salim 		return -ENOMEM;
1399d51d081dSJamal Hadi Salim 
1400d51d081dSJamal Hadi Salim 	x = xfrm_state_lookup(&id->daddr, id->spi, id->proto, id->family);
1401d51d081dSJamal Hadi Salim 	if (x == NULL) {
1402d51d081dSJamal Hadi Salim 		kfree(r_skb);
1403d51d081dSJamal Hadi Salim 		return -ESRCH;
1404d51d081dSJamal Hadi Salim 	}
1405d51d081dSJamal Hadi Salim 
1406d51d081dSJamal Hadi Salim 	/*
1407d51d081dSJamal Hadi Salim 	 * XXX: is this lock really needed - none of the other
1408d51d081dSJamal Hadi Salim 	 * gets lock (the concern is things getting updated
1409d51d081dSJamal Hadi Salim 	 * while we are still reading) - jhs
1410d51d081dSJamal Hadi Salim 	*/
1411d51d081dSJamal Hadi Salim 	spin_lock_bh(&x->lock);
1412d51d081dSJamal Hadi Salim 	c.data.aevent = p->flags;
1413d51d081dSJamal Hadi Salim 	c.seq = nlh->nlmsg_seq;
1414d51d081dSJamal Hadi Salim 	c.pid = nlh->nlmsg_pid;
1415d51d081dSJamal Hadi Salim 
1416d51d081dSJamal Hadi Salim 	if (build_aevent(r_skb, x, &c) < 0)
1417d51d081dSJamal Hadi Salim 		BUG();
1418d51d081dSJamal Hadi Salim 	err = netlink_unicast(xfrm_nl, r_skb,
1419d51d081dSJamal Hadi Salim 			      NETLINK_CB(skb).pid, MSG_DONTWAIT);
1420d51d081dSJamal Hadi Salim 	spin_unlock_bh(&x->lock);
1421d51d081dSJamal Hadi Salim 	xfrm_state_put(x);
1422d51d081dSJamal Hadi Salim 	return err;
1423d51d081dSJamal Hadi Salim }
1424d51d081dSJamal Hadi Salim 
1425*22e70050SChristoph Hellwig static int xfrm_new_ae(struct sk_buff *skb, struct nlmsghdr *nlh,
1426*22e70050SChristoph Hellwig 		struct rtattr **xfrma)
1427d51d081dSJamal Hadi Salim {
1428d51d081dSJamal Hadi Salim 	struct xfrm_state *x;
1429d51d081dSJamal Hadi Salim 	struct km_event c;
1430d51d081dSJamal Hadi Salim 	int err = - EINVAL;
1431d51d081dSJamal Hadi Salim 	struct xfrm_aevent_id *p = NLMSG_DATA(nlh);
1432d51d081dSJamal Hadi Salim 	struct rtattr *rp = xfrma[XFRMA_REPLAY_VAL-1];
1433d51d081dSJamal Hadi Salim 	struct rtattr *lt = xfrma[XFRMA_LTIME_VAL-1];
1434d51d081dSJamal Hadi Salim 
1435d51d081dSJamal Hadi Salim 	if (!lt && !rp)
1436d51d081dSJamal Hadi Salim 		return err;
1437d51d081dSJamal Hadi Salim 
1438d51d081dSJamal Hadi Salim 	/* pedantic mode - thou shalt sayeth replaceth */
1439d51d081dSJamal Hadi Salim 	if (!(nlh->nlmsg_flags&NLM_F_REPLACE))
1440d51d081dSJamal Hadi Salim 		return err;
1441d51d081dSJamal Hadi Salim 
1442d51d081dSJamal Hadi Salim 	x = xfrm_state_lookup(&p->sa_id.daddr, p->sa_id.spi, p->sa_id.proto, p->sa_id.family);
1443d51d081dSJamal Hadi Salim 	if (x == NULL)
1444d51d081dSJamal Hadi Salim 		return -ESRCH;
1445d51d081dSJamal Hadi Salim 
1446d51d081dSJamal Hadi Salim 	if (x->km.state != XFRM_STATE_VALID)
1447d51d081dSJamal Hadi Salim 		goto out;
1448d51d081dSJamal Hadi Salim 
1449d51d081dSJamal Hadi Salim 	spin_lock_bh(&x->lock);
1450*22e70050SChristoph Hellwig 	err = xfrm_update_ae_params(x, xfrma);
1451d51d081dSJamal Hadi Salim 	spin_unlock_bh(&x->lock);
1452d51d081dSJamal Hadi Salim 	if (err	< 0)
1453d51d081dSJamal Hadi Salim 		goto out;
1454d51d081dSJamal Hadi Salim 
1455d51d081dSJamal Hadi Salim 	c.event = nlh->nlmsg_type;
1456d51d081dSJamal Hadi Salim 	c.seq = nlh->nlmsg_seq;
1457d51d081dSJamal Hadi Salim 	c.pid = nlh->nlmsg_pid;
1458d51d081dSJamal Hadi Salim 	c.data.aevent = XFRM_AE_CU;
1459d51d081dSJamal Hadi Salim 	km_state_notify(x, &c);
1460d51d081dSJamal Hadi Salim 	err = 0;
1461d51d081dSJamal Hadi Salim out:
1462d51d081dSJamal Hadi Salim 	xfrm_state_put(x);
1463d51d081dSJamal Hadi Salim 	return err;
1464d51d081dSJamal Hadi Salim }
1465d51d081dSJamal Hadi Salim 
1466*22e70050SChristoph Hellwig static int xfrm_flush_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
1467*22e70050SChristoph Hellwig 		struct rtattr **xfrma)
14681da177e4SLinus Torvalds {
146926b15dadSJamal Hadi Salim 	struct km_event c;
1470b798a9edSJamal Hadi Salim 	u8 type = XFRM_POLICY_TYPE_MAIN;
1471f7b6983fSMasahide NAKAMURA 	int err;
1472161a09e7SJoy Latten 	struct xfrm_audit audit_info;
147326b15dadSJamal Hadi Salim 
1474*22e70050SChristoph Hellwig 	err = copy_from_user_policy_type(&type, xfrma);
1475f7b6983fSMasahide NAKAMURA 	if (err)
1476f7b6983fSMasahide NAKAMURA 		return err;
1477f7b6983fSMasahide NAKAMURA 
1478161a09e7SJoy Latten 	audit_info.loginuid = NETLINK_CB(skb).loginuid;
1479161a09e7SJoy Latten 	audit_info.secid = NETLINK_CB(skb).sid;
1480161a09e7SJoy Latten 	xfrm_policy_flush(type, &audit_info);
1481f7b6983fSMasahide NAKAMURA 	c.data.type = type;
1482f60f6b8fSHerbert Xu 	c.event = nlh->nlmsg_type;
148326b15dadSJamal Hadi Salim 	c.seq = nlh->nlmsg_seq;
148426b15dadSJamal Hadi Salim 	c.pid = nlh->nlmsg_pid;
148526b15dadSJamal Hadi Salim 	km_policy_notify(NULL, 0, &c);
14861da177e4SLinus Torvalds 	return 0;
14871da177e4SLinus Torvalds }
14881da177e4SLinus Torvalds 
1489*22e70050SChristoph Hellwig static int xfrm_add_pol_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
1490*22e70050SChristoph Hellwig 		struct rtattr **xfrma)
14916c5c8ca7SJamal Hadi Salim {
14926c5c8ca7SJamal Hadi Salim 	struct xfrm_policy *xp;
14936c5c8ca7SJamal Hadi Salim 	struct xfrm_user_polexpire *up = NLMSG_DATA(nlh);
14946c5c8ca7SJamal Hadi Salim 	struct xfrm_userpolicy_info *p = &up->pol;
1495b798a9edSJamal Hadi Salim 	u8 type = XFRM_POLICY_TYPE_MAIN;
14966c5c8ca7SJamal Hadi Salim 	int err = -ENOENT;
14976c5c8ca7SJamal Hadi Salim 
1498*22e70050SChristoph Hellwig 	err = copy_from_user_policy_type(&type, xfrma);
1499f7b6983fSMasahide NAKAMURA 	if (err)
1500f7b6983fSMasahide NAKAMURA 		return err;
1501f7b6983fSMasahide NAKAMURA 
15026c5c8ca7SJamal Hadi Salim 	if (p->index)
1503f7b6983fSMasahide NAKAMURA 		xp = xfrm_policy_byid(type, p->dir, p->index, 0);
15046c5c8ca7SJamal Hadi Salim 	else {
1505*22e70050SChristoph Hellwig 		struct rtattr *rt = xfrma[XFRMA_SEC_CTX-1];
15066c5c8ca7SJamal Hadi Salim 		struct xfrm_policy tmp;
15076c5c8ca7SJamal Hadi Salim 
1508*22e70050SChristoph Hellwig 		err = verify_sec_ctx_len(xfrma);
15096c5c8ca7SJamal Hadi Salim 		if (err)
15106c5c8ca7SJamal Hadi Salim 			return err;
15116c5c8ca7SJamal Hadi Salim 
15126c5c8ca7SJamal Hadi Salim 		memset(&tmp, 0, sizeof(struct xfrm_policy));
15136c5c8ca7SJamal Hadi Salim 		if (rt) {
15146c5c8ca7SJamal Hadi Salim 			struct xfrm_user_sec_ctx *uctx = RTA_DATA(rt);
15156c5c8ca7SJamal Hadi Salim 
15166c5c8ca7SJamal Hadi Salim 			if ((err = security_xfrm_policy_alloc(&tmp, uctx)))
15176c5c8ca7SJamal Hadi Salim 				return err;
15186c5c8ca7SJamal Hadi Salim 		}
1519f7b6983fSMasahide NAKAMURA 		xp = xfrm_policy_bysel_ctx(type, p->dir, &p->sel, tmp.security, 0);
15206c5c8ca7SJamal Hadi Salim 		security_xfrm_policy_free(&tmp);
15216c5c8ca7SJamal Hadi Salim 	}
15226c5c8ca7SJamal Hadi Salim 
15236c5c8ca7SJamal Hadi Salim 	if (xp == NULL)
15246c5c8ca7SJamal Hadi Salim 		return err;
15256c5c8ca7SJamal Hadi Salim 											read_lock(&xp->lock);
15266c5c8ca7SJamal Hadi Salim 	if (xp->dead) {
15276c5c8ca7SJamal Hadi Salim 		read_unlock(&xp->lock);
15286c5c8ca7SJamal Hadi Salim 		goto out;
15296c5c8ca7SJamal Hadi Salim 	}
15306c5c8ca7SJamal Hadi Salim 
15316c5c8ca7SJamal Hadi Salim 	read_unlock(&xp->lock);
15326c5c8ca7SJamal Hadi Salim 	err = 0;
15336c5c8ca7SJamal Hadi Salim 	if (up->hard) {
15346c5c8ca7SJamal Hadi Salim 		xfrm_policy_delete(xp, p->dir);
1535161a09e7SJoy Latten 		xfrm_audit_log(NETLINK_CB(skb).loginuid, NETLINK_CB(skb).sid,
1536161a09e7SJoy Latten 				AUDIT_MAC_IPSEC_DELSPD, 1, xp, NULL);
1537161a09e7SJoy Latten 
15386c5c8ca7SJamal Hadi Salim 	} else {
15396c5c8ca7SJamal Hadi Salim 		// reset the timers here?
15406c5c8ca7SJamal Hadi Salim 		printk("Dont know what to do with soft policy expire\n");
15416c5c8ca7SJamal Hadi Salim 	}
15426c5c8ca7SJamal Hadi Salim 	km_policy_expired(xp, p->dir, up->hard, current->pid);
15436c5c8ca7SJamal Hadi Salim 
15446c5c8ca7SJamal Hadi Salim out:
15456c5c8ca7SJamal Hadi Salim 	xfrm_pol_put(xp);
15466c5c8ca7SJamal Hadi Salim 	return err;
15476c5c8ca7SJamal Hadi Salim }
15486c5c8ca7SJamal Hadi Salim 
1549*22e70050SChristoph Hellwig static int xfrm_add_sa_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
1550*22e70050SChristoph Hellwig 		struct rtattr **xfrma)
155153bc6b4dSJamal Hadi Salim {
155253bc6b4dSJamal Hadi Salim 	struct xfrm_state *x;
155353bc6b4dSJamal Hadi Salim 	int err;
155453bc6b4dSJamal Hadi Salim 	struct xfrm_user_expire *ue = NLMSG_DATA(nlh);
155553bc6b4dSJamal Hadi Salim 	struct xfrm_usersa_info *p = &ue->state;
155653bc6b4dSJamal Hadi Salim 
155753bc6b4dSJamal Hadi Salim 	x = xfrm_state_lookup(&p->id.daddr, p->id.spi, p->id.proto, p->family);
155853bc6b4dSJamal Hadi Salim 		err = -ENOENT;
155953bc6b4dSJamal Hadi Salim 
156053bc6b4dSJamal Hadi Salim 	if (x == NULL)
156153bc6b4dSJamal Hadi Salim 		return err;
156253bc6b4dSJamal Hadi Salim 
156353bc6b4dSJamal Hadi Salim 	err = -EINVAL;
156453bc6b4dSJamal Hadi Salim 
156553bc6b4dSJamal Hadi Salim 	spin_lock_bh(&x->lock);
156653bc6b4dSJamal Hadi Salim 	if (x->km.state != XFRM_STATE_VALID)
156753bc6b4dSJamal Hadi Salim 		goto out;
156853bc6b4dSJamal Hadi Salim 	km_state_expired(x, ue->hard, current->pid);
156953bc6b4dSJamal Hadi Salim 
1570161a09e7SJoy Latten 	if (ue->hard) {
157153bc6b4dSJamal Hadi Salim 		__xfrm_state_delete(x);
1572161a09e7SJoy Latten 		xfrm_audit_log(NETLINK_CB(skb).loginuid, NETLINK_CB(skb).sid,
1573161a09e7SJoy Latten 			       AUDIT_MAC_IPSEC_DELSA, 1, NULL, x);
1574161a09e7SJoy Latten 	}
157553bc6b4dSJamal Hadi Salim out:
157653bc6b4dSJamal Hadi Salim 	spin_unlock_bh(&x->lock);
157753bc6b4dSJamal Hadi Salim 	xfrm_state_put(x);
157853bc6b4dSJamal Hadi Salim 	return err;
157953bc6b4dSJamal Hadi Salim }
158053bc6b4dSJamal Hadi Salim 
1581*22e70050SChristoph Hellwig static int xfrm_add_acquire(struct sk_buff *skb, struct nlmsghdr *nlh,
1582*22e70050SChristoph Hellwig 		struct rtattr **xfrma)
1583980ebd25SJamal Hadi Salim {
1584980ebd25SJamal Hadi Salim 	struct xfrm_policy *xp;
1585980ebd25SJamal Hadi Salim 	struct xfrm_user_tmpl *ut;
1586980ebd25SJamal Hadi Salim 	int i;
1587980ebd25SJamal Hadi Salim 	struct rtattr *rt = xfrma[XFRMA_TMPL-1];
1588980ebd25SJamal Hadi Salim 
1589980ebd25SJamal Hadi Salim 	struct xfrm_user_acquire *ua = NLMSG_DATA(nlh);
1590980ebd25SJamal Hadi Salim 	struct xfrm_state *x = xfrm_state_alloc();
1591980ebd25SJamal Hadi Salim 	int err = -ENOMEM;
1592980ebd25SJamal Hadi Salim 
1593980ebd25SJamal Hadi Salim 	if (!x)
1594980ebd25SJamal Hadi Salim 		return err;
1595980ebd25SJamal Hadi Salim 
1596980ebd25SJamal Hadi Salim 	err = verify_newpolicy_info(&ua->policy);
1597980ebd25SJamal Hadi Salim 	if (err) {
1598980ebd25SJamal Hadi Salim 		printk("BAD policy passed\n");
1599980ebd25SJamal Hadi Salim 		kfree(x);
1600980ebd25SJamal Hadi Salim 		return err;
1601980ebd25SJamal Hadi Salim 	}
1602980ebd25SJamal Hadi Salim 
1603980ebd25SJamal Hadi Salim 	/*   build an XP */
1604b4ad86bfSDavid S. Miller 	xp = xfrm_policy_construct(&ua->policy, (struct rtattr **) xfrma, &err);
1605b4ad86bfSDavid S. Miller 	if (!xp) {
1606980ebd25SJamal Hadi Salim 		kfree(x);
1607980ebd25SJamal Hadi Salim 		return err;
1608980ebd25SJamal Hadi Salim 	}
1609980ebd25SJamal Hadi Salim 
1610980ebd25SJamal Hadi Salim 	memcpy(&x->id, &ua->id, sizeof(ua->id));
1611980ebd25SJamal Hadi Salim 	memcpy(&x->props.saddr, &ua->saddr, sizeof(ua->saddr));
1612980ebd25SJamal Hadi Salim 	memcpy(&x->sel, &ua->sel, sizeof(ua->sel));
1613980ebd25SJamal Hadi Salim 
1614980ebd25SJamal Hadi Salim 	ut = RTA_DATA(rt);
1615980ebd25SJamal Hadi Salim 	/* extract the templates and for each call km_key */
1616980ebd25SJamal Hadi Salim 	for (i = 0; i < xp->xfrm_nr; i++, ut++) {
1617980ebd25SJamal Hadi Salim 		struct xfrm_tmpl *t = &xp->xfrm_vec[i];
1618980ebd25SJamal Hadi Salim 		memcpy(&x->id, &t->id, sizeof(x->id));
1619980ebd25SJamal Hadi Salim 		x->props.mode = t->mode;
1620980ebd25SJamal Hadi Salim 		x->props.reqid = t->reqid;
1621980ebd25SJamal Hadi Salim 		x->props.family = ut->family;
1622980ebd25SJamal Hadi Salim 		t->aalgos = ua->aalgos;
1623980ebd25SJamal Hadi Salim 		t->ealgos = ua->ealgos;
1624980ebd25SJamal Hadi Salim 		t->calgos = ua->calgos;
1625980ebd25SJamal Hadi Salim 		err = km_query(x, t, xp);
1626980ebd25SJamal Hadi Salim 
1627980ebd25SJamal Hadi Salim 	}
1628980ebd25SJamal Hadi Salim 
1629980ebd25SJamal Hadi Salim 	kfree(x);
1630980ebd25SJamal Hadi Salim 	kfree(xp);
1631980ebd25SJamal Hadi Salim 
1632980ebd25SJamal Hadi Salim 	return 0;
1633980ebd25SJamal Hadi Salim }
1634980ebd25SJamal Hadi Salim 
1635d51d081dSJamal Hadi Salim 
1636492b558bSThomas Graf #define XMSGSIZE(type) NLMSG_LENGTH(sizeof(struct type))
1637492b558bSThomas Graf 
1638492b558bSThomas Graf static const int xfrm_msg_min[XFRM_NR_MSGTYPES] = {
1639492b558bSThomas Graf 	[XFRM_MSG_NEWSA       - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
1640492b558bSThomas Graf 	[XFRM_MSG_DELSA       - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
1641492b558bSThomas Graf 	[XFRM_MSG_GETSA       - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
1642492b558bSThomas Graf 	[XFRM_MSG_NEWPOLICY   - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
1643492b558bSThomas Graf 	[XFRM_MSG_DELPOLICY   - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
1644492b558bSThomas Graf 	[XFRM_MSG_GETPOLICY   - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
1645492b558bSThomas Graf 	[XFRM_MSG_ALLOCSPI    - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userspi_info),
1646980ebd25SJamal Hadi Salim 	[XFRM_MSG_ACQUIRE     - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_acquire),
164753bc6b4dSJamal Hadi Salim 	[XFRM_MSG_EXPIRE      - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_expire),
1648492b558bSThomas Graf 	[XFRM_MSG_UPDPOLICY   - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
1649492b558bSThomas Graf 	[XFRM_MSG_UPDSA       - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
16506c5c8ca7SJamal Hadi Salim 	[XFRM_MSG_POLEXPIRE   - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_polexpire),
1651492b558bSThomas Graf 	[XFRM_MSG_FLUSHSA     - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_flush),
1652492b558bSThomas Graf 	[XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = NLMSG_LENGTH(0),
1653d51d081dSJamal Hadi Salim 	[XFRM_MSG_NEWAE       - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
1654d51d081dSJamal Hadi Salim 	[XFRM_MSG_GETAE       - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
165597a64b45SMasahide NAKAMURA 	[XFRM_MSG_REPORT      - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_report),
16561da177e4SLinus Torvalds };
16571da177e4SLinus Torvalds 
1658492b558bSThomas Graf #undef XMSGSIZE
1659492b558bSThomas Graf 
16601da177e4SLinus Torvalds static struct xfrm_link {
1661*22e70050SChristoph Hellwig 	int (*doit)(struct sk_buff *, struct nlmsghdr *, struct rtattr **);
16621da177e4SLinus Torvalds 	int (*dump)(struct sk_buff *, struct netlink_callback *);
1663492b558bSThomas Graf } xfrm_dispatch[XFRM_NR_MSGTYPES] = {
1664492b558bSThomas Graf 	[XFRM_MSG_NEWSA       - XFRM_MSG_BASE] = { .doit = xfrm_add_sa        },
1665492b558bSThomas Graf 	[XFRM_MSG_DELSA       - XFRM_MSG_BASE] = { .doit = xfrm_del_sa        },
1666492b558bSThomas Graf 	[XFRM_MSG_GETSA       - XFRM_MSG_BASE] = { .doit = xfrm_get_sa,
1667492b558bSThomas Graf 						   .dump = xfrm_dump_sa       },
1668492b558bSThomas Graf 	[XFRM_MSG_NEWPOLICY   - XFRM_MSG_BASE] = { .doit = xfrm_add_policy    },
1669492b558bSThomas Graf 	[XFRM_MSG_DELPOLICY   - XFRM_MSG_BASE] = { .doit = xfrm_get_policy    },
1670492b558bSThomas Graf 	[XFRM_MSG_GETPOLICY   - XFRM_MSG_BASE] = { .doit = xfrm_get_policy,
1671492b558bSThomas Graf 						   .dump = xfrm_dump_policy   },
1672492b558bSThomas Graf 	[XFRM_MSG_ALLOCSPI    - XFRM_MSG_BASE] = { .doit = xfrm_alloc_userspi },
1673980ebd25SJamal Hadi Salim 	[XFRM_MSG_ACQUIRE     - XFRM_MSG_BASE] = { .doit = xfrm_add_acquire   },
167453bc6b4dSJamal Hadi Salim 	[XFRM_MSG_EXPIRE      - XFRM_MSG_BASE] = { .doit = xfrm_add_sa_expire },
1675492b558bSThomas Graf 	[XFRM_MSG_UPDPOLICY   - XFRM_MSG_BASE] = { .doit = xfrm_add_policy    },
1676492b558bSThomas Graf 	[XFRM_MSG_UPDSA       - XFRM_MSG_BASE] = { .doit = xfrm_add_sa        },
16776c5c8ca7SJamal Hadi Salim 	[XFRM_MSG_POLEXPIRE   - XFRM_MSG_BASE] = { .doit = xfrm_add_pol_expire},
1678492b558bSThomas Graf 	[XFRM_MSG_FLUSHSA     - XFRM_MSG_BASE] = { .doit = xfrm_flush_sa      },
1679492b558bSThomas Graf 	[XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_flush_policy  },
1680d51d081dSJamal Hadi Salim 	[XFRM_MSG_NEWAE       - XFRM_MSG_BASE] = { .doit = xfrm_new_ae  },
1681d51d081dSJamal Hadi Salim 	[XFRM_MSG_GETAE       - XFRM_MSG_BASE] = { .doit = xfrm_get_ae  },
16821da177e4SLinus Torvalds };
16831da177e4SLinus Torvalds 
16841da177e4SLinus Torvalds static int xfrm_user_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh, int *errp)
16851da177e4SLinus Torvalds {
16861da177e4SLinus Torvalds 	struct rtattr *xfrma[XFRMA_MAX];
16871da177e4SLinus Torvalds 	struct xfrm_link *link;
16881da177e4SLinus Torvalds 	int type, min_len;
16891da177e4SLinus Torvalds 
16901da177e4SLinus Torvalds 	if (!(nlh->nlmsg_flags & NLM_F_REQUEST))
16911da177e4SLinus Torvalds 		return 0;
16921da177e4SLinus Torvalds 
16931da177e4SLinus Torvalds 	type = nlh->nlmsg_type;
16941da177e4SLinus Torvalds 
16951da177e4SLinus Torvalds 	/* A control message: ignore them */
16961da177e4SLinus Torvalds 	if (type < XFRM_MSG_BASE)
16971da177e4SLinus Torvalds 		return 0;
16981da177e4SLinus Torvalds 
16991da177e4SLinus Torvalds 	/* Unknown message: reply with EINVAL */
17001da177e4SLinus Torvalds 	if (type > XFRM_MSG_MAX)
17011da177e4SLinus Torvalds 		goto err_einval;
17021da177e4SLinus Torvalds 
17031da177e4SLinus Torvalds 	type -= XFRM_MSG_BASE;
17041da177e4SLinus Torvalds 	link = &xfrm_dispatch[type];
17051da177e4SLinus Torvalds 
17061da177e4SLinus Torvalds 	/* All operations require privileges, even GET */
1707c7bdb545SDarrel Goeddel 	if (security_netlink_recv(skb, CAP_NET_ADMIN)) {
17081da177e4SLinus Torvalds 		*errp = -EPERM;
17091da177e4SLinus Torvalds 		return -1;
17101da177e4SLinus Torvalds 	}
17111da177e4SLinus Torvalds 
1712492b558bSThomas Graf 	if ((type == (XFRM_MSG_GETSA - XFRM_MSG_BASE) ||
1713492b558bSThomas Graf 	     type == (XFRM_MSG_GETPOLICY - XFRM_MSG_BASE)) &&
1714492b558bSThomas Graf 	    (nlh->nlmsg_flags & NLM_F_DUMP)) {
17151da177e4SLinus Torvalds 		if (link->dump == NULL)
17161da177e4SLinus Torvalds 			goto err_einval;
17171da177e4SLinus Torvalds 
17181da177e4SLinus Torvalds 		if ((*errp = netlink_dump_start(xfrm_nl, skb, nlh,
1719a8f74b22SThomas Graf 						link->dump, NULL)) != 0) {
17201da177e4SLinus Torvalds 			return -1;
17211da177e4SLinus Torvalds 		}
172288fc2c84SThomas Graf 
172388fc2c84SThomas Graf 		netlink_queue_skip(nlh, skb);
17241da177e4SLinus Torvalds 		return -1;
17251da177e4SLinus Torvalds 	}
17261da177e4SLinus Torvalds 
17271da177e4SLinus Torvalds 	memset(xfrma, 0, sizeof(xfrma));
17281da177e4SLinus Torvalds 
17291da177e4SLinus Torvalds 	if (nlh->nlmsg_len < (min_len = xfrm_msg_min[type]))
17301da177e4SLinus Torvalds 		goto err_einval;
17311da177e4SLinus Torvalds 
17321da177e4SLinus Torvalds 	if (nlh->nlmsg_len > min_len) {
17331da177e4SLinus Torvalds 		int attrlen = nlh->nlmsg_len - NLMSG_ALIGN(min_len);
17341da177e4SLinus Torvalds 		struct rtattr *attr = (void *) nlh + NLMSG_ALIGN(min_len);
17351da177e4SLinus Torvalds 
17361da177e4SLinus Torvalds 		while (RTA_OK(attr, attrlen)) {
17371da177e4SLinus Torvalds 			unsigned short flavor = attr->rta_type;
17381da177e4SLinus Torvalds 			if (flavor) {
17391da177e4SLinus Torvalds 				if (flavor > XFRMA_MAX)
17401da177e4SLinus Torvalds 					goto err_einval;
17411da177e4SLinus Torvalds 				xfrma[flavor - 1] = attr;
17421da177e4SLinus Torvalds 			}
17431da177e4SLinus Torvalds 			attr = RTA_NEXT(attr, attrlen);
17441da177e4SLinus Torvalds 		}
17451da177e4SLinus Torvalds 	}
17461da177e4SLinus Torvalds 
17471da177e4SLinus Torvalds 	if (link->doit == NULL)
17481da177e4SLinus Torvalds 		goto err_einval;
1749*22e70050SChristoph Hellwig 	*errp = link->doit(skb, nlh, xfrma);
17501da177e4SLinus Torvalds 
17511da177e4SLinus Torvalds 	return *errp;
17521da177e4SLinus Torvalds 
17531da177e4SLinus Torvalds err_einval:
17541da177e4SLinus Torvalds 	*errp = -EINVAL;
17551da177e4SLinus Torvalds 	return -1;
17561da177e4SLinus Torvalds }
17571da177e4SLinus Torvalds 
17581da177e4SLinus Torvalds static void xfrm_netlink_rcv(struct sock *sk, int len)
17591da177e4SLinus Torvalds {
176088fc2c84SThomas Graf 	unsigned int qlen = 0;
17612a0a6ebeSHerbert Xu 
17621da177e4SLinus Torvalds 	do {
17634a3e2f71SArjan van de Ven 		mutex_lock(&xfrm_cfg_mutex);
176488fc2c84SThomas Graf 		netlink_run_queue(sk, &qlen, &xfrm_user_rcv_msg);
17654a3e2f71SArjan van de Ven 		mutex_unlock(&xfrm_cfg_mutex);
17661da177e4SLinus Torvalds 
17672a0a6ebeSHerbert Xu 	} while (qlen);
17681da177e4SLinus Torvalds }
17691da177e4SLinus Torvalds 
1770d51d081dSJamal Hadi Salim static int build_expire(struct sk_buff *skb, struct xfrm_state *x, struct km_event *c)
17711da177e4SLinus Torvalds {
17721da177e4SLinus Torvalds 	struct xfrm_user_expire *ue;
17731da177e4SLinus Torvalds 	struct nlmsghdr *nlh;
17741da177e4SLinus Torvalds 	unsigned char *b = skb->tail;
17751da177e4SLinus Torvalds 
1776d51d081dSJamal Hadi Salim 	nlh = NLMSG_PUT(skb, c->pid, 0, XFRM_MSG_EXPIRE,
17771da177e4SLinus Torvalds 			sizeof(*ue));
17781da177e4SLinus Torvalds 	ue = NLMSG_DATA(nlh);
17791da177e4SLinus Torvalds 	nlh->nlmsg_flags = 0;
17801da177e4SLinus Torvalds 
17811da177e4SLinus Torvalds 	copy_to_user_state(x, &ue->state);
1782d51d081dSJamal Hadi Salim 	ue->hard = (c->data.hard != 0) ? 1 : 0;
17831da177e4SLinus Torvalds 
17841da177e4SLinus Torvalds 	nlh->nlmsg_len = skb->tail - b;
17851da177e4SLinus Torvalds 	return skb->len;
17861da177e4SLinus Torvalds 
17871da177e4SLinus Torvalds nlmsg_failure:
17881da177e4SLinus Torvalds 	skb_trim(skb, b - skb->data);
17891da177e4SLinus Torvalds 	return -1;
17901da177e4SLinus Torvalds }
17911da177e4SLinus Torvalds 
179226b15dadSJamal Hadi Salim static int xfrm_exp_state_notify(struct xfrm_state *x, struct km_event *c)
17931da177e4SLinus Torvalds {
17941da177e4SLinus Torvalds 	struct sk_buff *skb;
1795ee57eef9SJamal Hadi Salim 	int len = NLMSG_LENGTH(sizeof(struct xfrm_user_expire));
17961da177e4SLinus Torvalds 
1797ee57eef9SJamal Hadi Salim 	skb = alloc_skb(len, GFP_ATOMIC);
17981da177e4SLinus Torvalds 	if (skb == NULL)
17991da177e4SLinus Torvalds 		return -ENOMEM;
18001da177e4SLinus Torvalds 
1801d51d081dSJamal Hadi Salim 	if (build_expire(skb, x, c) < 0)
18021da177e4SLinus Torvalds 		BUG();
18031da177e4SLinus Torvalds 
1804ac6d439dSPatrick McHardy 	NETLINK_CB(skb).dst_group = XFRMNLGRP_EXPIRE;
1805ac6d439dSPatrick McHardy 	return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_EXPIRE, GFP_ATOMIC);
18061da177e4SLinus Torvalds }
18071da177e4SLinus Torvalds 
1808d51d081dSJamal Hadi Salim static int xfrm_aevent_state_notify(struct xfrm_state *x, struct km_event *c)
1809d51d081dSJamal Hadi Salim {
1810d51d081dSJamal Hadi Salim 	struct sk_buff *skb;
1811d51d081dSJamal Hadi Salim 	int len = NLMSG_LENGTH(sizeof(struct xfrm_aevent_id));
1812d51d081dSJamal Hadi Salim 
1813d51d081dSJamal Hadi Salim 	len += RTA_SPACE(sizeof(struct xfrm_replay_state));
1814d51d081dSJamal Hadi Salim 	len += RTA_SPACE(sizeof(struct xfrm_lifetime_cur));
1815d51d081dSJamal Hadi Salim 	skb = alloc_skb(len, GFP_ATOMIC);
1816d51d081dSJamal Hadi Salim 	if (skb == NULL)
1817d51d081dSJamal Hadi Salim 		return -ENOMEM;
1818d51d081dSJamal Hadi Salim 
1819d51d081dSJamal Hadi Salim 	if (build_aevent(skb, x, c) < 0)
1820d51d081dSJamal Hadi Salim 		BUG();
1821d51d081dSJamal Hadi Salim 
1822d51d081dSJamal Hadi Salim 	NETLINK_CB(skb).dst_group = XFRMNLGRP_AEVENTS;
1823d51d081dSJamal Hadi Salim 	return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_AEVENTS, GFP_ATOMIC);
1824d51d081dSJamal Hadi Salim }
1825d51d081dSJamal Hadi Salim 
182626b15dadSJamal Hadi Salim static int xfrm_notify_sa_flush(struct km_event *c)
182726b15dadSJamal Hadi Salim {
182826b15dadSJamal Hadi Salim 	struct xfrm_usersa_flush *p;
182926b15dadSJamal Hadi Salim 	struct nlmsghdr *nlh;
183026b15dadSJamal Hadi Salim 	struct sk_buff *skb;
183126b15dadSJamal Hadi Salim 	unsigned char *b;
183226b15dadSJamal Hadi Salim 	int len = NLMSG_LENGTH(sizeof(struct xfrm_usersa_flush));
183326b15dadSJamal Hadi Salim 
183426b15dadSJamal Hadi Salim 	skb = alloc_skb(len, GFP_ATOMIC);
183526b15dadSJamal Hadi Salim 	if (skb == NULL)
183626b15dadSJamal Hadi Salim 		return -ENOMEM;
183726b15dadSJamal Hadi Salim 	b = skb->tail;
183826b15dadSJamal Hadi Salim 
183926b15dadSJamal Hadi Salim 	nlh = NLMSG_PUT(skb, c->pid, c->seq,
184026b15dadSJamal Hadi Salim 			XFRM_MSG_FLUSHSA, sizeof(*p));
184126b15dadSJamal Hadi Salim 	nlh->nlmsg_flags = 0;
184226b15dadSJamal Hadi Salim 
184326b15dadSJamal Hadi Salim 	p = NLMSG_DATA(nlh);
1844bf08867fSHerbert Xu 	p->proto = c->data.proto;
184526b15dadSJamal Hadi Salim 
184626b15dadSJamal Hadi Salim 	nlh->nlmsg_len = skb->tail - b;
184726b15dadSJamal Hadi Salim 
1848ac6d439dSPatrick McHardy 	NETLINK_CB(skb).dst_group = XFRMNLGRP_SA;
1849ac6d439dSPatrick McHardy 	return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_SA, GFP_ATOMIC);
185026b15dadSJamal Hadi Salim 
185126b15dadSJamal Hadi Salim nlmsg_failure:
185226b15dadSJamal Hadi Salim 	kfree_skb(skb);
185326b15dadSJamal Hadi Salim 	return -1;
185426b15dadSJamal Hadi Salim }
185526b15dadSJamal Hadi Salim 
185626b15dadSJamal Hadi Salim static int inline xfrm_sa_len(struct xfrm_state *x)
185726b15dadSJamal Hadi Salim {
18580603eac0SHerbert Xu 	int l = 0;
185926b15dadSJamal Hadi Salim 	if (x->aalg)
186026b15dadSJamal Hadi Salim 		l += RTA_SPACE(sizeof(*x->aalg) + (x->aalg->alg_key_len+7)/8);
186126b15dadSJamal Hadi Salim 	if (x->ealg)
186226b15dadSJamal Hadi Salim 		l += RTA_SPACE(sizeof(*x->ealg) + (x->ealg->alg_key_len+7)/8);
186326b15dadSJamal Hadi Salim 	if (x->calg)
186426b15dadSJamal Hadi Salim 		l += RTA_SPACE(sizeof(*x->calg));
186526b15dadSJamal Hadi Salim 	if (x->encap)
186626b15dadSJamal Hadi Salim 		l += RTA_SPACE(sizeof(*x->encap));
186726b15dadSJamal Hadi Salim 
186826b15dadSJamal Hadi Salim 	return l;
186926b15dadSJamal Hadi Salim }
187026b15dadSJamal Hadi Salim 
187126b15dadSJamal Hadi Salim static int xfrm_notify_sa(struct xfrm_state *x, struct km_event *c)
187226b15dadSJamal Hadi Salim {
187326b15dadSJamal Hadi Salim 	struct xfrm_usersa_info *p;
18740603eac0SHerbert Xu 	struct xfrm_usersa_id *id;
187526b15dadSJamal Hadi Salim 	struct nlmsghdr *nlh;
187626b15dadSJamal Hadi Salim 	struct sk_buff *skb;
187726b15dadSJamal Hadi Salim 	unsigned char *b;
187826b15dadSJamal Hadi Salim 	int len = xfrm_sa_len(x);
18790603eac0SHerbert Xu 	int headlen;
18800603eac0SHerbert Xu 
18810603eac0SHerbert Xu 	headlen = sizeof(*p);
18820603eac0SHerbert Xu 	if (c->event == XFRM_MSG_DELSA) {
18830603eac0SHerbert Xu 		len += RTA_SPACE(headlen);
18840603eac0SHerbert Xu 		headlen = sizeof(*id);
18850603eac0SHerbert Xu 	}
18860603eac0SHerbert Xu 	len += NLMSG_SPACE(headlen);
188726b15dadSJamal Hadi Salim 
188826b15dadSJamal Hadi Salim 	skb = alloc_skb(len, GFP_ATOMIC);
188926b15dadSJamal Hadi Salim 	if (skb == NULL)
189026b15dadSJamal Hadi Salim 		return -ENOMEM;
189126b15dadSJamal Hadi Salim 	b = skb->tail;
189226b15dadSJamal Hadi Salim 
18930603eac0SHerbert Xu 	nlh = NLMSG_PUT(skb, c->pid, c->seq, c->event, headlen);
189426b15dadSJamal Hadi Salim 	nlh->nlmsg_flags = 0;
189526b15dadSJamal Hadi Salim 
189626b15dadSJamal Hadi Salim 	p = NLMSG_DATA(nlh);
18970603eac0SHerbert Xu 	if (c->event == XFRM_MSG_DELSA) {
18980603eac0SHerbert Xu 		id = NLMSG_DATA(nlh);
18990603eac0SHerbert Xu 		memcpy(&id->daddr, &x->id.daddr, sizeof(id->daddr));
19000603eac0SHerbert Xu 		id->spi = x->id.spi;
19010603eac0SHerbert Xu 		id->family = x->props.family;
19020603eac0SHerbert Xu 		id->proto = x->id.proto;
19030603eac0SHerbert Xu 
19040603eac0SHerbert Xu 		p = RTA_DATA(__RTA_PUT(skb, XFRMA_SA, sizeof(*p)));
19050603eac0SHerbert Xu 	}
19060603eac0SHerbert Xu 
190726b15dadSJamal Hadi Salim 	copy_to_user_state(x, p);
190826b15dadSJamal Hadi Salim 
190926b15dadSJamal Hadi Salim 	if (x->aalg)
191026b15dadSJamal Hadi Salim 		RTA_PUT(skb, XFRMA_ALG_AUTH,
191126b15dadSJamal Hadi Salim 			sizeof(*(x->aalg))+(x->aalg->alg_key_len+7)/8, x->aalg);
191226b15dadSJamal Hadi Salim 	if (x->ealg)
191326b15dadSJamal Hadi Salim 		RTA_PUT(skb, XFRMA_ALG_CRYPT,
191426b15dadSJamal Hadi Salim 			sizeof(*(x->ealg))+(x->ealg->alg_key_len+7)/8, x->ealg);
191526b15dadSJamal Hadi Salim 	if (x->calg)
191626b15dadSJamal Hadi Salim 		RTA_PUT(skb, XFRMA_ALG_COMP, sizeof(*(x->calg)), x->calg);
191726b15dadSJamal Hadi Salim 
191826b15dadSJamal Hadi Salim 	if (x->encap)
191926b15dadSJamal Hadi Salim 		RTA_PUT(skb, XFRMA_ENCAP, sizeof(*x->encap), x->encap);
192026b15dadSJamal Hadi Salim 
192126b15dadSJamal Hadi Salim 	nlh->nlmsg_len = skb->tail - b;
192226b15dadSJamal Hadi Salim 
1923ac6d439dSPatrick McHardy 	NETLINK_CB(skb).dst_group = XFRMNLGRP_SA;
1924ac6d439dSPatrick McHardy 	return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_SA, GFP_ATOMIC);
192526b15dadSJamal Hadi Salim 
192626b15dadSJamal Hadi Salim nlmsg_failure:
192726b15dadSJamal Hadi Salim rtattr_failure:
192826b15dadSJamal Hadi Salim 	kfree_skb(skb);
192926b15dadSJamal Hadi Salim 	return -1;
193026b15dadSJamal Hadi Salim }
193126b15dadSJamal Hadi Salim 
193226b15dadSJamal Hadi Salim static int xfrm_send_state_notify(struct xfrm_state *x, struct km_event *c)
193326b15dadSJamal Hadi Salim {
193426b15dadSJamal Hadi Salim 
193526b15dadSJamal Hadi Salim 	switch (c->event) {
1936f60f6b8fSHerbert Xu 	case XFRM_MSG_EXPIRE:
193726b15dadSJamal Hadi Salim 		return xfrm_exp_state_notify(x, c);
1938d51d081dSJamal Hadi Salim 	case XFRM_MSG_NEWAE:
1939d51d081dSJamal Hadi Salim 		return xfrm_aevent_state_notify(x, c);
1940f60f6b8fSHerbert Xu 	case XFRM_MSG_DELSA:
1941f60f6b8fSHerbert Xu 	case XFRM_MSG_UPDSA:
1942f60f6b8fSHerbert Xu 	case XFRM_MSG_NEWSA:
194326b15dadSJamal Hadi Salim 		return xfrm_notify_sa(x, c);
1944f60f6b8fSHerbert Xu 	case XFRM_MSG_FLUSHSA:
194526b15dadSJamal Hadi Salim 		return xfrm_notify_sa_flush(c);
194626b15dadSJamal Hadi Salim 	default:
194726b15dadSJamal Hadi Salim 		 printk("xfrm_user: Unknown SA event %d\n", c->event);
194826b15dadSJamal Hadi Salim 		 break;
194926b15dadSJamal Hadi Salim 	}
195026b15dadSJamal Hadi Salim 
195126b15dadSJamal Hadi Salim 	return 0;
195226b15dadSJamal Hadi Salim 
195326b15dadSJamal Hadi Salim }
195426b15dadSJamal Hadi Salim 
19551da177e4SLinus Torvalds static int build_acquire(struct sk_buff *skb, struct xfrm_state *x,
19561da177e4SLinus Torvalds 			 struct xfrm_tmpl *xt, struct xfrm_policy *xp,
19571da177e4SLinus Torvalds 			 int dir)
19581da177e4SLinus Torvalds {
19591da177e4SLinus Torvalds 	struct xfrm_user_acquire *ua;
19601da177e4SLinus Torvalds 	struct nlmsghdr *nlh;
19611da177e4SLinus Torvalds 	unsigned char *b = skb->tail;
19621da177e4SLinus Torvalds 	__u32 seq = xfrm_get_acqseq();
19631da177e4SLinus Torvalds 
19641da177e4SLinus Torvalds 	nlh = NLMSG_PUT(skb, 0, 0, XFRM_MSG_ACQUIRE,
19651da177e4SLinus Torvalds 			sizeof(*ua));
19661da177e4SLinus Torvalds 	ua = NLMSG_DATA(nlh);
19671da177e4SLinus Torvalds 	nlh->nlmsg_flags = 0;
19681da177e4SLinus Torvalds 
19691da177e4SLinus Torvalds 	memcpy(&ua->id, &x->id, sizeof(ua->id));
19701da177e4SLinus Torvalds 	memcpy(&ua->saddr, &x->props.saddr, sizeof(ua->saddr));
19711da177e4SLinus Torvalds 	memcpy(&ua->sel, &x->sel, sizeof(ua->sel));
19721da177e4SLinus Torvalds 	copy_to_user_policy(xp, &ua->policy, dir);
19731da177e4SLinus Torvalds 	ua->aalgos = xt->aalgos;
19741da177e4SLinus Torvalds 	ua->ealgos = xt->ealgos;
19751da177e4SLinus Torvalds 	ua->calgos = xt->calgos;
19761da177e4SLinus Torvalds 	ua->seq = x->km.seq = seq;
19771da177e4SLinus Torvalds 
19781da177e4SLinus Torvalds 	if (copy_to_user_tmpl(xp, skb) < 0)
19791da177e4SLinus Torvalds 		goto nlmsg_failure;
19800d681623SSerge Hallyn 	if (copy_to_user_state_sec_ctx(x, skb))
1981df71837dSTrent Jaeger 		goto nlmsg_failure;
19821459bb36SJamal Hadi Salim 	if (copy_to_user_policy_type(xp->type, skb) < 0)
1983f7b6983fSMasahide NAKAMURA 		goto nlmsg_failure;
19841da177e4SLinus Torvalds 
19851da177e4SLinus Torvalds 	nlh->nlmsg_len = skb->tail - b;
19861da177e4SLinus Torvalds 	return skb->len;
19871da177e4SLinus Torvalds 
19881da177e4SLinus Torvalds nlmsg_failure:
19891da177e4SLinus Torvalds 	skb_trim(skb, b - skb->data);
19901da177e4SLinus Torvalds 	return -1;
19911da177e4SLinus Torvalds }
19921da177e4SLinus Torvalds 
19931da177e4SLinus Torvalds static int xfrm_send_acquire(struct xfrm_state *x, struct xfrm_tmpl *xt,
19941da177e4SLinus Torvalds 			     struct xfrm_policy *xp, int dir)
19951da177e4SLinus Torvalds {
19961da177e4SLinus Torvalds 	struct sk_buff *skb;
19971da177e4SLinus Torvalds 	size_t len;
19981da177e4SLinus Torvalds 
19991da177e4SLinus Torvalds 	len = RTA_SPACE(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr);
20001da177e4SLinus Torvalds 	len += NLMSG_SPACE(sizeof(struct xfrm_user_acquire));
2001df71837dSTrent Jaeger 	len += RTA_SPACE(xfrm_user_sec_ctx_size(xp));
2002785fd8b8SJamal Hadi Salim #ifdef CONFIG_XFRM_SUB_POLICY
2003785fd8b8SJamal Hadi Salim 	len += RTA_SPACE(sizeof(struct xfrm_userpolicy_type));
2004785fd8b8SJamal Hadi Salim #endif
20051da177e4SLinus Torvalds 	skb = alloc_skb(len, GFP_ATOMIC);
20061da177e4SLinus Torvalds 	if (skb == NULL)
20071da177e4SLinus Torvalds 		return -ENOMEM;
20081da177e4SLinus Torvalds 
20091da177e4SLinus Torvalds 	if (build_acquire(skb, x, xt, xp, dir) < 0)
20101da177e4SLinus Torvalds 		BUG();
20111da177e4SLinus Torvalds 
2012ac6d439dSPatrick McHardy 	NETLINK_CB(skb).dst_group = XFRMNLGRP_ACQUIRE;
2013ac6d439dSPatrick McHardy 	return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_ACQUIRE, GFP_ATOMIC);
20141da177e4SLinus Torvalds }
20151da177e4SLinus Torvalds 
20161da177e4SLinus Torvalds /* User gives us xfrm_user_policy_info followed by an array of 0
20171da177e4SLinus Torvalds  * or more templates.
20181da177e4SLinus Torvalds  */
2019cb969f07SVenkat Yekkirala static struct xfrm_policy *xfrm_compile_policy(struct sock *sk, int opt,
20201da177e4SLinus Torvalds 					       u8 *data, int len, int *dir)
20211da177e4SLinus Torvalds {
20221da177e4SLinus Torvalds 	struct xfrm_userpolicy_info *p = (struct xfrm_userpolicy_info *)data;
20231da177e4SLinus Torvalds 	struct xfrm_user_tmpl *ut = (struct xfrm_user_tmpl *) (p + 1);
20241da177e4SLinus Torvalds 	struct xfrm_policy *xp;
20251da177e4SLinus Torvalds 	int nr;
20261da177e4SLinus Torvalds 
2027cb969f07SVenkat Yekkirala 	switch (sk->sk_family) {
20281da177e4SLinus Torvalds 	case AF_INET:
20291da177e4SLinus Torvalds 		if (opt != IP_XFRM_POLICY) {
20301da177e4SLinus Torvalds 			*dir = -EOPNOTSUPP;
20311da177e4SLinus Torvalds 			return NULL;
20321da177e4SLinus Torvalds 		}
20331da177e4SLinus Torvalds 		break;
20341da177e4SLinus Torvalds #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
20351da177e4SLinus Torvalds 	case AF_INET6:
20361da177e4SLinus Torvalds 		if (opt != IPV6_XFRM_POLICY) {
20371da177e4SLinus Torvalds 			*dir = -EOPNOTSUPP;
20381da177e4SLinus Torvalds 			return NULL;
20391da177e4SLinus Torvalds 		}
20401da177e4SLinus Torvalds 		break;
20411da177e4SLinus Torvalds #endif
20421da177e4SLinus Torvalds 	default:
20431da177e4SLinus Torvalds 		*dir = -EINVAL;
20441da177e4SLinus Torvalds 		return NULL;
20451da177e4SLinus Torvalds 	}
20461da177e4SLinus Torvalds 
20471da177e4SLinus Torvalds 	*dir = -EINVAL;
20481da177e4SLinus Torvalds 
20491da177e4SLinus Torvalds 	if (len < sizeof(*p) ||
20501da177e4SLinus Torvalds 	    verify_newpolicy_info(p))
20511da177e4SLinus Torvalds 		return NULL;
20521da177e4SLinus Torvalds 
20531da177e4SLinus Torvalds 	nr = ((len - sizeof(*p)) / sizeof(*ut));
2054b4ad86bfSDavid S. Miller 	if (validate_tmpl(nr, ut, p->sel.family))
20551da177e4SLinus Torvalds 		return NULL;
20561da177e4SLinus Torvalds 
2057a4f1bac6SHerbert Xu 	if (p->dir > XFRM_POLICY_OUT)
2058a4f1bac6SHerbert Xu 		return NULL;
2059a4f1bac6SHerbert Xu 
20601da177e4SLinus Torvalds 	xp = xfrm_policy_alloc(GFP_KERNEL);
20611da177e4SLinus Torvalds 	if (xp == NULL) {
20621da177e4SLinus Torvalds 		*dir = -ENOBUFS;
20631da177e4SLinus Torvalds 		return NULL;
20641da177e4SLinus Torvalds 	}
20651da177e4SLinus Torvalds 
20661da177e4SLinus Torvalds 	copy_from_user_policy(xp, p);
2067f7b6983fSMasahide NAKAMURA 	xp->type = XFRM_POLICY_TYPE_MAIN;
20681da177e4SLinus Torvalds 	copy_templates(xp, ut, nr);
20691da177e4SLinus Torvalds 
20701da177e4SLinus Torvalds 	*dir = p->dir;
20711da177e4SLinus Torvalds 
20721da177e4SLinus Torvalds 	return xp;
20731da177e4SLinus Torvalds }
20741da177e4SLinus Torvalds 
20751da177e4SLinus Torvalds static int build_polexpire(struct sk_buff *skb, struct xfrm_policy *xp,
2076d51d081dSJamal Hadi Salim 			   int dir, struct km_event *c)
20771da177e4SLinus Torvalds {
20781da177e4SLinus Torvalds 	struct xfrm_user_polexpire *upe;
20791da177e4SLinus Torvalds 	struct nlmsghdr *nlh;
2080d51d081dSJamal Hadi Salim 	int hard = c->data.hard;
20811da177e4SLinus Torvalds 	unsigned char *b = skb->tail;
20821da177e4SLinus Torvalds 
2083d51d081dSJamal Hadi Salim 	nlh = NLMSG_PUT(skb, c->pid, 0, XFRM_MSG_POLEXPIRE, sizeof(*upe));
20841da177e4SLinus Torvalds 	upe = NLMSG_DATA(nlh);
20851da177e4SLinus Torvalds 	nlh->nlmsg_flags = 0;
20861da177e4SLinus Torvalds 
20871da177e4SLinus Torvalds 	copy_to_user_policy(xp, &upe->pol, dir);
20881da177e4SLinus Torvalds 	if (copy_to_user_tmpl(xp, skb) < 0)
20891da177e4SLinus Torvalds 		goto nlmsg_failure;
2090df71837dSTrent Jaeger 	if (copy_to_user_sec_ctx(xp, skb))
2091df71837dSTrent Jaeger 		goto nlmsg_failure;
20921459bb36SJamal Hadi Salim 	if (copy_to_user_policy_type(xp->type, skb) < 0)
2093f7b6983fSMasahide NAKAMURA 		goto nlmsg_failure;
20941da177e4SLinus Torvalds 	upe->hard = !!hard;
20951da177e4SLinus Torvalds 
20961da177e4SLinus Torvalds 	nlh->nlmsg_len = skb->tail - b;
20971da177e4SLinus Torvalds 	return skb->len;
20981da177e4SLinus Torvalds 
20991da177e4SLinus Torvalds nlmsg_failure:
21001da177e4SLinus Torvalds 	skb_trim(skb, b - skb->data);
21011da177e4SLinus Torvalds 	return -1;
21021da177e4SLinus Torvalds }
21031da177e4SLinus Torvalds 
210426b15dadSJamal Hadi Salim static int xfrm_exp_policy_notify(struct xfrm_policy *xp, int dir, struct km_event *c)
21051da177e4SLinus Torvalds {
21061da177e4SLinus Torvalds 	struct sk_buff *skb;
21071da177e4SLinus Torvalds 	size_t len;
21081da177e4SLinus Torvalds 
21091da177e4SLinus Torvalds 	len = RTA_SPACE(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr);
21101da177e4SLinus Torvalds 	len += NLMSG_SPACE(sizeof(struct xfrm_user_polexpire));
2111df71837dSTrent Jaeger 	len += RTA_SPACE(xfrm_user_sec_ctx_size(xp));
2112785fd8b8SJamal Hadi Salim #ifdef CONFIG_XFRM_SUB_POLICY
2113785fd8b8SJamal Hadi Salim 	len += RTA_SPACE(sizeof(struct xfrm_userpolicy_type));
2114785fd8b8SJamal Hadi Salim #endif
21151da177e4SLinus Torvalds 	skb = alloc_skb(len, GFP_ATOMIC);
21161da177e4SLinus Torvalds 	if (skb == NULL)
21171da177e4SLinus Torvalds 		return -ENOMEM;
21181da177e4SLinus Torvalds 
2119d51d081dSJamal Hadi Salim 	if (build_polexpire(skb, xp, dir, c) < 0)
21201da177e4SLinus Torvalds 		BUG();
21211da177e4SLinus Torvalds 
2122ac6d439dSPatrick McHardy 	NETLINK_CB(skb).dst_group = XFRMNLGRP_EXPIRE;
2123ac6d439dSPatrick McHardy 	return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_EXPIRE, GFP_ATOMIC);
21241da177e4SLinus Torvalds }
21251da177e4SLinus Torvalds 
212626b15dadSJamal Hadi Salim static int xfrm_notify_policy(struct xfrm_policy *xp, int dir, struct km_event *c)
212726b15dadSJamal Hadi Salim {
212826b15dadSJamal Hadi Salim 	struct xfrm_userpolicy_info *p;
21290603eac0SHerbert Xu 	struct xfrm_userpolicy_id *id;
213026b15dadSJamal Hadi Salim 	struct nlmsghdr *nlh;
213126b15dadSJamal Hadi Salim 	struct sk_buff *skb;
213226b15dadSJamal Hadi Salim 	unsigned char *b;
213326b15dadSJamal Hadi Salim 	int len = RTA_SPACE(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr);
21340603eac0SHerbert Xu 	int headlen;
21350603eac0SHerbert Xu 
21360603eac0SHerbert Xu 	headlen = sizeof(*p);
21370603eac0SHerbert Xu 	if (c->event == XFRM_MSG_DELPOLICY) {
21380603eac0SHerbert Xu 		len += RTA_SPACE(headlen);
21390603eac0SHerbert Xu 		headlen = sizeof(*id);
21400603eac0SHerbert Xu 	}
2141334f3d45SJamal Hadi Salim #ifdef CONFIG_XFRM_SUB_POLICY
2142334f3d45SJamal Hadi Salim 	len += RTA_SPACE(sizeof(struct xfrm_userpolicy_type));
2143334f3d45SJamal Hadi Salim #endif
21440603eac0SHerbert Xu 	len += NLMSG_SPACE(headlen);
214526b15dadSJamal Hadi Salim 
214626b15dadSJamal Hadi Salim 	skb = alloc_skb(len, GFP_ATOMIC);
214726b15dadSJamal Hadi Salim 	if (skb == NULL)
214826b15dadSJamal Hadi Salim 		return -ENOMEM;
214926b15dadSJamal Hadi Salim 	b = skb->tail;
215026b15dadSJamal Hadi Salim 
21510603eac0SHerbert Xu 	nlh = NLMSG_PUT(skb, c->pid, c->seq, c->event, headlen);
215226b15dadSJamal Hadi Salim 
215326b15dadSJamal Hadi Salim 	p = NLMSG_DATA(nlh);
21540603eac0SHerbert Xu 	if (c->event == XFRM_MSG_DELPOLICY) {
21550603eac0SHerbert Xu 		id = NLMSG_DATA(nlh);
21560603eac0SHerbert Xu 		memset(id, 0, sizeof(*id));
21570603eac0SHerbert Xu 		id->dir = dir;
21580603eac0SHerbert Xu 		if (c->data.byid)
21590603eac0SHerbert Xu 			id->index = xp->index;
21600603eac0SHerbert Xu 		else
21610603eac0SHerbert Xu 			memcpy(&id->sel, &xp->selector, sizeof(id->sel));
21620603eac0SHerbert Xu 
21630603eac0SHerbert Xu 		p = RTA_DATA(__RTA_PUT(skb, XFRMA_POLICY, sizeof(*p)));
21640603eac0SHerbert Xu 	}
216526b15dadSJamal Hadi Salim 
216626b15dadSJamal Hadi Salim 	nlh->nlmsg_flags = 0;
216726b15dadSJamal Hadi Salim 
216826b15dadSJamal Hadi Salim 	copy_to_user_policy(xp, p, dir);
216926b15dadSJamal Hadi Salim 	if (copy_to_user_tmpl(xp, skb) < 0)
217026b15dadSJamal Hadi Salim 		goto nlmsg_failure;
21711459bb36SJamal Hadi Salim 	if (copy_to_user_policy_type(xp->type, skb) < 0)
2172f7b6983fSMasahide NAKAMURA 		goto nlmsg_failure;
217326b15dadSJamal Hadi Salim 
217426b15dadSJamal Hadi Salim 	nlh->nlmsg_len = skb->tail - b;
217526b15dadSJamal Hadi Salim 
2176ac6d439dSPatrick McHardy 	NETLINK_CB(skb).dst_group = XFRMNLGRP_POLICY;
2177ac6d439dSPatrick McHardy 	return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_POLICY, GFP_ATOMIC);
217826b15dadSJamal Hadi Salim 
217926b15dadSJamal Hadi Salim nlmsg_failure:
21800603eac0SHerbert Xu rtattr_failure:
218126b15dadSJamal Hadi Salim 	kfree_skb(skb);
218226b15dadSJamal Hadi Salim 	return -1;
218326b15dadSJamal Hadi Salim }
218426b15dadSJamal Hadi Salim 
218526b15dadSJamal Hadi Salim static int xfrm_notify_policy_flush(struct km_event *c)
218626b15dadSJamal Hadi Salim {
218726b15dadSJamal Hadi Salim 	struct nlmsghdr *nlh;
218826b15dadSJamal Hadi Salim 	struct sk_buff *skb;
218926b15dadSJamal Hadi Salim 	unsigned char *b;
2190785fd8b8SJamal Hadi Salim 	int len = 0;
2191f7b6983fSMasahide NAKAMURA #ifdef CONFIG_XFRM_SUB_POLICY
2192785fd8b8SJamal Hadi Salim 	len += RTA_SPACE(sizeof(struct xfrm_userpolicy_type));
2193f7b6983fSMasahide NAKAMURA #endif
2194785fd8b8SJamal Hadi Salim 	len += NLMSG_LENGTH(0);
219526b15dadSJamal Hadi Salim 
219626b15dadSJamal Hadi Salim 	skb = alloc_skb(len, GFP_ATOMIC);
219726b15dadSJamal Hadi Salim 	if (skb == NULL)
219826b15dadSJamal Hadi Salim 		return -ENOMEM;
219926b15dadSJamal Hadi Salim 	b = skb->tail;
220026b15dadSJamal Hadi Salim 
220126b15dadSJamal Hadi Salim 
220226b15dadSJamal Hadi Salim 	nlh = NLMSG_PUT(skb, c->pid, c->seq, XFRM_MSG_FLUSHPOLICY, 0);
2203f7b6983fSMasahide NAKAMURA 	nlh->nlmsg_flags = 0;
22040c51f53cSJamal Hadi Salim 	if (copy_to_user_policy_type(c->data.type, skb) < 0)
22050c51f53cSJamal Hadi Salim 		goto nlmsg_failure;
220626b15dadSJamal Hadi Salim 
220726b15dadSJamal Hadi Salim 	nlh->nlmsg_len = skb->tail - b;
220826b15dadSJamal Hadi Salim 
2209ac6d439dSPatrick McHardy 	NETLINK_CB(skb).dst_group = XFRMNLGRP_POLICY;
2210ac6d439dSPatrick McHardy 	return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_POLICY, GFP_ATOMIC);
221126b15dadSJamal Hadi Salim 
221226b15dadSJamal Hadi Salim nlmsg_failure:
221326b15dadSJamal Hadi Salim 	kfree_skb(skb);
221426b15dadSJamal Hadi Salim 	return -1;
221526b15dadSJamal Hadi Salim }
221626b15dadSJamal Hadi Salim 
221726b15dadSJamal Hadi Salim static int xfrm_send_policy_notify(struct xfrm_policy *xp, int dir, struct km_event *c)
221826b15dadSJamal Hadi Salim {
221926b15dadSJamal Hadi Salim 
222026b15dadSJamal Hadi Salim 	switch (c->event) {
2221f60f6b8fSHerbert Xu 	case XFRM_MSG_NEWPOLICY:
2222f60f6b8fSHerbert Xu 	case XFRM_MSG_UPDPOLICY:
2223f60f6b8fSHerbert Xu 	case XFRM_MSG_DELPOLICY:
222426b15dadSJamal Hadi Salim 		return xfrm_notify_policy(xp, dir, c);
2225f60f6b8fSHerbert Xu 	case XFRM_MSG_FLUSHPOLICY:
222626b15dadSJamal Hadi Salim 		return xfrm_notify_policy_flush(c);
2227f60f6b8fSHerbert Xu 	case XFRM_MSG_POLEXPIRE:
222826b15dadSJamal Hadi Salim 		return xfrm_exp_policy_notify(xp, dir, c);
222926b15dadSJamal Hadi Salim 	default:
223026b15dadSJamal Hadi Salim 		printk("xfrm_user: Unknown Policy event %d\n", c->event);
223126b15dadSJamal Hadi Salim 	}
223226b15dadSJamal Hadi Salim 
223326b15dadSJamal Hadi Salim 	return 0;
223426b15dadSJamal Hadi Salim 
223526b15dadSJamal Hadi Salim }
223626b15dadSJamal Hadi Salim 
223797a64b45SMasahide NAKAMURA static int build_report(struct sk_buff *skb, u8 proto,
223897a64b45SMasahide NAKAMURA 			struct xfrm_selector *sel, xfrm_address_t *addr)
223997a64b45SMasahide NAKAMURA {
224097a64b45SMasahide NAKAMURA 	struct xfrm_user_report *ur;
224197a64b45SMasahide NAKAMURA 	struct nlmsghdr *nlh;
224297a64b45SMasahide NAKAMURA 	unsigned char *b = skb->tail;
224397a64b45SMasahide NAKAMURA 
224497a64b45SMasahide NAKAMURA 	nlh = NLMSG_PUT(skb, 0, 0, XFRM_MSG_REPORT, sizeof(*ur));
224597a64b45SMasahide NAKAMURA 	ur = NLMSG_DATA(nlh);
224697a64b45SMasahide NAKAMURA 	nlh->nlmsg_flags = 0;
224797a64b45SMasahide NAKAMURA 
224897a64b45SMasahide NAKAMURA 	ur->proto = proto;
224997a64b45SMasahide NAKAMURA 	memcpy(&ur->sel, sel, sizeof(ur->sel));
225097a64b45SMasahide NAKAMURA 
225197a64b45SMasahide NAKAMURA 	if (addr)
225297a64b45SMasahide NAKAMURA 		RTA_PUT(skb, XFRMA_COADDR, sizeof(*addr), addr);
225397a64b45SMasahide NAKAMURA 
225497a64b45SMasahide NAKAMURA 	nlh->nlmsg_len = skb->tail - b;
225597a64b45SMasahide NAKAMURA 	return skb->len;
225697a64b45SMasahide NAKAMURA 
225797a64b45SMasahide NAKAMURA nlmsg_failure:
225897a64b45SMasahide NAKAMURA rtattr_failure:
225997a64b45SMasahide NAKAMURA 	skb_trim(skb, b - skb->data);
226097a64b45SMasahide NAKAMURA 	return -1;
226197a64b45SMasahide NAKAMURA }
226297a64b45SMasahide NAKAMURA 
226397a64b45SMasahide NAKAMURA static int xfrm_send_report(u8 proto, struct xfrm_selector *sel,
226497a64b45SMasahide NAKAMURA 			    xfrm_address_t *addr)
226597a64b45SMasahide NAKAMURA {
226697a64b45SMasahide NAKAMURA 	struct sk_buff *skb;
226797a64b45SMasahide NAKAMURA 	size_t len;
226897a64b45SMasahide NAKAMURA 
226997a64b45SMasahide NAKAMURA 	len = NLMSG_ALIGN(NLMSG_LENGTH(sizeof(struct xfrm_user_report)));
227097a64b45SMasahide NAKAMURA 	skb = alloc_skb(len, GFP_ATOMIC);
227197a64b45SMasahide NAKAMURA 	if (skb == NULL)
227297a64b45SMasahide NAKAMURA 		return -ENOMEM;
227397a64b45SMasahide NAKAMURA 
227497a64b45SMasahide NAKAMURA 	if (build_report(skb, proto, sel, addr) < 0)
227597a64b45SMasahide NAKAMURA 		BUG();
227697a64b45SMasahide NAKAMURA 
227797a64b45SMasahide NAKAMURA 	NETLINK_CB(skb).dst_group = XFRMNLGRP_REPORT;
227897a64b45SMasahide NAKAMURA 	return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_REPORT, GFP_ATOMIC);
227997a64b45SMasahide NAKAMURA }
228097a64b45SMasahide NAKAMURA 
22811da177e4SLinus Torvalds static struct xfrm_mgr netlink_mgr = {
22821da177e4SLinus Torvalds 	.id		= "netlink",
22831da177e4SLinus Torvalds 	.notify		= xfrm_send_state_notify,
22841da177e4SLinus Torvalds 	.acquire	= xfrm_send_acquire,
22851da177e4SLinus Torvalds 	.compile_policy	= xfrm_compile_policy,
22861da177e4SLinus Torvalds 	.notify_policy	= xfrm_send_policy_notify,
228797a64b45SMasahide NAKAMURA 	.report		= xfrm_send_report,
22881da177e4SLinus Torvalds };
22891da177e4SLinus Torvalds 
22901da177e4SLinus Torvalds static int __init xfrm_user_init(void)
22911da177e4SLinus Torvalds {
2292be33690dSPatrick McHardy 	struct sock *nlsk;
2293be33690dSPatrick McHardy 
2294654b32c6SMasahide NAKAMURA 	printk(KERN_INFO "Initializing XFRM netlink socket\n");
22951da177e4SLinus Torvalds 
2296be33690dSPatrick McHardy 	nlsk = netlink_kernel_create(NETLINK_XFRM, XFRMNLGRP_MAX,
229706628607SPatrick McHardy 	                             xfrm_netlink_rcv, THIS_MODULE);
2298be33690dSPatrick McHardy 	if (nlsk == NULL)
22991da177e4SLinus Torvalds 		return -ENOMEM;
2300be33690dSPatrick McHardy 	rcu_assign_pointer(xfrm_nl, nlsk);
23011da177e4SLinus Torvalds 
23021da177e4SLinus Torvalds 	xfrm_register_km(&netlink_mgr);
23031da177e4SLinus Torvalds 
23041da177e4SLinus Torvalds 	return 0;
23051da177e4SLinus Torvalds }
23061da177e4SLinus Torvalds 
23071da177e4SLinus Torvalds static void __exit xfrm_user_exit(void)
23081da177e4SLinus Torvalds {
2309be33690dSPatrick McHardy 	struct sock *nlsk = xfrm_nl;
2310be33690dSPatrick McHardy 
23111da177e4SLinus Torvalds 	xfrm_unregister_km(&netlink_mgr);
2312be33690dSPatrick McHardy 	rcu_assign_pointer(xfrm_nl, NULL);
2313be33690dSPatrick McHardy 	synchronize_rcu();
2314be33690dSPatrick McHardy 	sock_release(nlsk->sk_socket);
23151da177e4SLinus Torvalds }
23161da177e4SLinus Torvalds 
23171da177e4SLinus Torvalds module_init(xfrm_user_init);
23181da177e4SLinus Torvalds module_exit(xfrm_user_exit);
23191da177e4SLinus Torvalds MODULE_LICENSE("GPL");
23204fdb3bb7SHarald Welte MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK, NETLINK_XFRM);
2321f8cd5488SJamal Hadi Salim 
2322