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 34*161a09e7SJoy 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 4371da177e4SLinus Torvalds static int xfrm_add_sa(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma) 4381da177e4SLinus Torvalds { 4391da177e4SLinus Torvalds struct xfrm_usersa_info *p = NLMSG_DATA(nlh); 4401da177e4SLinus Torvalds struct xfrm_state *x; 4411da177e4SLinus Torvalds int err; 44226b15dadSJamal Hadi Salim struct km_event c; 4431da177e4SLinus Torvalds 4441da177e4SLinus Torvalds err = verify_newsa_info(p, (struct rtattr **)xfrma); 4451da177e4SLinus Torvalds if (err) 4461da177e4SLinus Torvalds return err; 4471da177e4SLinus Torvalds 4481da177e4SLinus Torvalds x = xfrm_state_construct(p, (struct rtattr **)xfrma, &err); 4491da177e4SLinus Torvalds if (!x) 4501da177e4SLinus Torvalds return err; 4511da177e4SLinus Torvalds 45226b15dadSJamal Hadi Salim xfrm_state_hold(x); 4531da177e4SLinus Torvalds if (nlh->nlmsg_type == XFRM_MSG_NEWSA) 4541da177e4SLinus Torvalds err = xfrm_state_add(x); 4551da177e4SLinus Torvalds else 4561da177e4SLinus Torvalds err = xfrm_state_update(x); 4571da177e4SLinus Torvalds 458*161a09e7SJoy Latten xfrm_audit_log(NETLINK_CB(skb).loginuid, NETLINK_CB(skb).sid, 459*161a09e7SJoy Latten AUDIT_MAC_IPSEC_ADDSA, err ? 0 : 1, NULL, x); 460*161a09e7SJoy Latten 4611da177e4SLinus Torvalds if (err < 0) { 4621da177e4SLinus Torvalds x->km.state = XFRM_STATE_DEAD; 46321380b81SHerbert Xu __xfrm_state_put(x); 4647d6dfe1fSPatrick McHardy goto out; 4651da177e4SLinus Torvalds } 4661da177e4SLinus Torvalds 46726b15dadSJamal Hadi Salim c.seq = nlh->nlmsg_seq; 46826b15dadSJamal Hadi Salim c.pid = nlh->nlmsg_pid; 469f60f6b8fSHerbert Xu c.event = nlh->nlmsg_type; 47026b15dadSJamal Hadi Salim 47126b15dadSJamal Hadi Salim km_state_notify(x, &c); 4727d6dfe1fSPatrick McHardy out: 47326b15dadSJamal Hadi Salim xfrm_state_put(x); 4741da177e4SLinus Torvalds return err; 4751da177e4SLinus Torvalds } 4761da177e4SLinus Torvalds 477eb2971b6SMasahide NAKAMURA static struct xfrm_state *xfrm_user_state_lookup(struct xfrm_usersa_id *p, 478eb2971b6SMasahide NAKAMURA struct rtattr **xfrma, 479eb2971b6SMasahide NAKAMURA int *errp) 480eb2971b6SMasahide NAKAMURA { 481eb2971b6SMasahide NAKAMURA struct xfrm_state *x = NULL; 482eb2971b6SMasahide NAKAMURA int err; 483eb2971b6SMasahide NAKAMURA 484eb2971b6SMasahide NAKAMURA if (xfrm_id_proto_match(p->proto, IPSEC_PROTO_ANY)) { 485eb2971b6SMasahide NAKAMURA err = -ESRCH; 486eb2971b6SMasahide NAKAMURA x = xfrm_state_lookup(&p->daddr, p->spi, p->proto, p->family); 487eb2971b6SMasahide NAKAMURA } else { 488eb2971b6SMasahide NAKAMURA xfrm_address_t *saddr = NULL; 489eb2971b6SMasahide NAKAMURA 490eb2971b6SMasahide NAKAMURA err = verify_one_addr(xfrma, XFRMA_SRCADDR, &saddr); 491eb2971b6SMasahide NAKAMURA if (err) 492eb2971b6SMasahide NAKAMURA goto out; 493eb2971b6SMasahide NAKAMURA 494eb2971b6SMasahide NAKAMURA if (!saddr) { 495eb2971b6SMasahide NAKAMURA err = -EINVAL; 496eb2971b6SMasahide NAKAMURA goto out; 497eb2971b6SMasahide NAKAMURA } 498eb2971b6SMasahide NAKAMURA 4999abbffeeSMasahide NAKAMURA err = -ESRCH; 500eb2971b6SMasahide NAKAMURA x = xfrm_state_lookup_byaddr(&p->daddr, saddr, p->proto, 501eb2971b6SMasahide NAKAMURA p->family); 502eb2971b6SMasahide NAKAMURA } 503eb2971b6SMasahide NAKAMURA 504eb2971b6SMasahide NAKAMURA out: 505eb2971b6SMasahide NAKAMURA if (!x && errp) 506eb2971b6SMasahide NAKAMURA *errp = err; 507eb2971b6SMasahide NAKAMURA return x; 508eb2971b6SMasahide NAKAMURA } 509eb2971b6SMasahide NAKAMURA 5101da177e4SLinus Torvalds static int xfrm_del_sa(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma) 5111da177e4SLinus Torvalds { 5121da177e4SLinus Torvalds struct xfrm_state *x; 513eb2971b6SMasahide NAKAMURA int err = -ESRCH; 51426b15dadSJamal Hadi Salim struct km_event c; 5151da177e4SLinus Torvalds struct xfrm_usersa_id *p = NLMSG_DATA(nlh); 5161da177e4SLinus Torvalds 517eb2971b6SMasahide NAKAMURA x = xfrm_user_state_lookup(p, (struct rtattr **)xfrma, &err); 5181da177e4SLinus Torvalds if (x == NULL) 519eb2971b6SMasahide NAKAMURA return err; 5201da177e4SLinus Torvalds 5216f68dc37SDavid S. Miller if ((err = security_xfrm_state_delete(x)) != 0) 522c8c05a8eSCatherine Zhang goto out; 523c8c05a8eSCatherine Zhang 5241da177e4SLinus Torvalds if (xfrm_state_kern(x)) { 525c8c05a8eSCatherine Zhang err = -EPERM; 526c8c05a8eSCatherine Zhang goto out; 5271da177e4SLinus Torvalds } 5281da177e4SLinus Torvalds 52926b15dadSJamal Hadi Salim err = xfrm_state_delete(x); 530*161a09e7SJoy Latten 531*161a09e7SJoy Latten xfrm_audit_log(NETLINK_CB(skb).loginuid, NETLINK_CB(skb).sid, 532*161a09e7SJoy Latten AUDIT_MAC_IPSEC_DELSA, err ? 0 : 1, NULL, x); 533*161a09e7SJoy Latten 534c8c05a8eSCatherine Zhang if (err < 0) 535c8c05a8eSCatherine Zhang goto out; 53626b15dadSJamal Hadi Salim 53726b15dadSJamal Hadi Salim c.seq = nlh->nlmsg_seq; 53826b15dadSJamal Hadi Salim c.pid = nlh->nlmsg_pid; 539f60f6b8fSHerbert Xu c.event = nlh->nlmsg_type; 54026b15dadSJamal Hadi Salim km_state_notify(x, &c); 5411da177e4SLinus Torvalds 542c8c05a8eSCatherine Zhang out: 543c8c05a8eSCatherine Zhang xfrm_state_put(x); 54426b15dadSJamal Hadi Salim return err; 5451da177e4SLinus Torvalds } 5461da177e4SLinus Torvalds 5471da177e4SLinus Torvalds static void copy_to_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p) 5481da177e4SLinus Torvalds { 5491da177e4SLinus Torvalds memcpy(&p->id, &x->id, sizeof(p->id)); 5501da177e4SLinus Torvalds memcpy(&p->sel, &x->sel, sizeof(p->sel)); 5511da177e4SLinus Torvalds memcpy(&p->lft, &x->lft, sizeof(p->lft)); 5521da177e4SLinus Torvalds memcpy(&p->curlft, &x->curlft, sizeof(p->curlft)); 5531da177e4SLinus Torvalds memcpy(&p->stats, &x->stats, sizeof(p->stats)); 55454489c14SDavid S. Miller memcpy(&p->saddr, &x->props.saddr, sizeof(p->saddr)); 5551da177e4SLinus Torvalds p->mode = x->props.mode; 5561da177e4SLinus Torvalds p->replay_window = x->props.replay_window; 5571da177e4SLinus Torvalds p->reqid = x->props.reqid; 5581da177e4SLinus Torvalds p->family = x->props.family; 5591da177e4SLinus Torvalds p->flags = x->props.flags; 5601da177e4SLinus Torvalds p->seq = x->km.seq; 5611da177e4SLinus Torvalds } 5621da177e4SLinus Torvalds 5631da177e4SLinus Torvalds struct xfrm_dump_info { 5641da177e4SLinus Torvalds struct sk_buff *in_skb; 5651da177e4SLinus Torvalds struct sk_buff *out_skb; 5661da177e4SLinus Torvalds u32 nlmsg_seq; 5671da177e4SLinus Torvalds u16 nlmsg_flags; 5681da177e4SLinus Torvalds int start_idx; 5691da177e4SLinus Torvalds int this_idx; 5701da177e4SLinus Torvalds }; 5711da177e4SLinus Torvalds 5721da177e4SLinus Torvalds static int dump_one_state(struct xfrm_state *x, int count, void *ptr) 5731da177e4SLinus Torvalds { 5741da177e4SLinus Torvalds struct xfrm_dump_info *sp = ptr; 5751da177e4SLinus Torvalds struct sk_buff *in_skb = sp->in_skb; 5761da177e4SLinus Torvalds struct sk_buff *skb = sp->out_skb; 5771da177e4SLinus Torvalds struct xfrm_usersa_info *p; 5781da177e4SLinus Torvalds struct nlmsghdr *nlh; 5791da177e4SLinus Torvalds unsigned char *b = skb->tail; 5801da177e4SLinus Torvalds 5811da177e4SLinus Torvalds if (sp->this_idx < sp->start_idx) 5821da177e4SLinus Torvalds goto out; 5831da177e4SLinus Torvalds 5841da177e4SLinus Torvalds nlh = NLMSG_PUT(skb, NETLINK_CB(in_skb).pid, 5851da177e4SLinus Torvalds sp->nlmsg_seq, 5861da177e4SLinus Torvalds XFRM_MSG_NEWSA, sizeof(*p)); 5871da177e4SLinus Torvalds nlh->nlmsg_flags = sp->nlmsg_flags; 5881da177e4SLinus Torvalds 5891da177e4SLinus Torvalds p = NLMSG_DATA(nlh); 5901da177e4SLinus Torvalds copy_to_user_state(x, p); 5911da177e4SLinus Torvalds 5921da177e4SLinus Torvalds if (x->aalg) 5931da177e4SLinus Torvalds RTA_PUT(skb, XFRMA_ALG_AUTH, 5941da177e4SLinus Torvalds sizeof(*(x->aalg))+(x->aalg->alg_key_len+7)/8, x->aalg); 5951da177e4SLinus Torvalds if (x->ealg) 5961da177e4SLinus Torvalds RTA_PUT(skb, XFRMA_ALG_CRYPT, 5971da177e4SLinus Torvalds sizeof(*(x->ealg))+(x->ealg->alg_key_len+7)/8, x->ealg); 5981da177e4SLinus Torvalds if (x->calg) 5991da177e4SLinus Torvalds RTA_PUT(skb, XFRMA_ALG_COMP, sizeof(*(x->calg)), x->calg); 6001da177e4SLinus Torvalds 6011da177e4SLinus Torvalds if (x->encap) 6021da177e4SLinus Torvalds RTA_PUT(skb, XFRMA_ENCAP, sizeof(*x->encap), x->encap); 6031da177e4SLinus Torvalds 604df71837dSTrent Jaeger if (x->security) { 605df71837dSTrent Jaeger int ctx_size = sizeof(struct xfrm_sec_ctx) + 606df71837dSTrent Jaeger x->security->ctx_len; 607df71837dSTrent Jaeger struct rtattr *rt = __RTA_PUT(skb, XFRMA_SEC_CTX, ctx_size); 608df71837dSTrent Jaeger struct xfrm_user_sec_ctx *uctx = RTA_DATA(rt); 609df71837dSTrent Jaeger 610df71837dSTrent Jaeger uctx->exttype = XFRMA_SEC_CTX; 611df71837dSTrent Jaeger uctx->len = ctx_size; 612df71837dSTrent Jaeger uctx->ctx_doi = x->security->ctx_doi; 613df71837dSTrent Jaeger uctx->ctx_alg = x->security->ctx_alg; 614df71837dSTrent Jaeger uctx->ctx_len = x->security->ctx_len; 615df71837dSTrent Jaeger memcpy(uctx + 1, x->security->ctx_str, x->security->ctx_len); 616df71837dSTrent Jaeger } 617060f02a3SNoriaki TAKAMIYA 618060f02a3SNoriaki TAKAMIYA if (x->coaddr) 619060f02a3SNoriaki TAKAMIYA RTA_PUT(skb, XFRMA_COADDR, sizeof(*x->coaddr), x->coaddr); 620060f02a3SNoriaki TAKAMIYA 6219afaca05SMasahide NAKAMURA if (x->lastused) 6229afaca05SMasahide NAKAMURA RTA_PUT(skb, XFRMA_LASTUSED, sizeof(x->lastused), &x->lastused); 6239afaca05SMasahide NAKAMURA 6241da177e4SLinus Torvalds nlh->nlmsg_len = skb->tail - b; 6251da177e4SLinus Torvalds out: 6261da177e4SLinus Torvalds sp->this_idx++; 6271da177e4SLinus Torvalds return 0; 6281da177e4SLinus Torvalds 6291da177e4SLinus Torvalds nlmsg_failure: 6301da177e4SLinus Torvalds rtattr_failure: 6311da177e4SLinus Torvalds skb_trim(skb, b - skb->data); 6321da177e4SLinus Torvalds return -1; 6331da177e4SLinus Torvalds } 6341da177e4SLinus Torvalds 6351da177e4SLinus Torvalds static int xfrm_dump_sa(struct sk_buff *skb, struct netlink_callback *cb) 6361da177e4SLinus Torvalds { 6371da177e4SLinus Torvalds struct xfrm_dump_info info; 6381da177e4SLinus Torvalds 6391da177e4SLinus Torvalds info.in_skb = cb->skb; 6401da177e4SLinus Torvalds info.out_skb = skb; 6411da177e4SLinus Torvalds info.nlmsg_seq = cb->nlh->nlmsg_seq; 6421da177e4SLinus Torvalds info.nlmsg_flags = NLM_F_MULTI; 6431da177e4SLinus Torvalds info.this_idx = 0; 6441da177e4SLinus Torvalds info.start_idx = cb->args[0]; 645dc00a525SMasahide NAKAMURA (void) xfrm_state_walk(0, dump_one_state, &info); 6461da177e4SLinus Torvalds cb->args[0] = info.this_idx; 6471da177e4SLinus Torvalds 6481da177e4SLinus Torvalds return skb->len; 6491da177e4SLinus Torvalds } 6501da177e4SLinus Torvalds 6511da177e4SLinus Torvalds static struct sk_buff *xfrm_state_netlink(struct sk_buff *in_skb, 6521da177e4SLinus Torvalds struct xfrm_state *x, u32 seq) 6531da177e4SLinus Torvalds { 6541da177e4SLinus Torvalds struct xfrm_dump_info info; 6551da177e4SLinus Torvalds struct sk_buff *skb; 6561da177e4SLinus Torvalds 6571da177e4SLinus Torvalds skb = alloc_skb(NLMSG_GOODSIZE, GFP_ATOMIC); 6581da177e4SLinus Torvalds if (!skb) 6591da177e4SLinus Torvalds return ERR_PTR(-ENOMEM); 6601da177e4SLinus Torvalds 6611da177e4SLinus Torvalds info.in_skb = in_skb; 6621da177e4SLinus Torvalds info.out_skb = skb; 6631da177e4SLinus Torvalds info.nlmsg_seq = seq; 6641da177e4SLinus Torvalds info.nlmsg_flags = 0; 6651da177e4SLinus Torvalds info.this_idx = info.start_idx = 0; 6661da177e4SLinus Torvalds 6671da177e4SLinus Torvalds if (dump_one_state(x, 0, &info)) { 6681da177e4SLinus Torvalds kfree_skb(skb); 6691da177e4SLinus Torvalds return NULL; 6701da177e4SLinus Torvalds } 6711da177e4SLinus Torvalds 6721da177e4SLinus Torvalds return skb; 6731da177e4SLinus Torvalds } 6741da177e4SLinus Torvalds 6751da177e4SLinus Torvalds static int xfrm_get_sa(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma) 6761da177e4SLinus Torvalds { 6771da177e4SLinus Torvalds struct xfrm_usersa_id *p = NLMSG_DATA(nlh); 6781da177e4SLinus Torvalds struct xfrm_state *x; 6791da177e4SLinus Torvalds struct sk_buff *resp_skb; 680eb2971b6SMasahide NAKAMURA int err = -ESRCH; 6811da177e4SLinus Torvalds 682eb2971b6SMasahide NAKAMURA x = xfrm_user_state_lookup(p, (struct rtattr **)xfrma, &err); 6831da177e4SLinus Torvalds if (x == NULL) 6841da177e4SLinus Torvalds goto out_noput; 6851da177e4SLinus Torvalds 6861da177e4SLinus Torvalds resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq); 6871da177e4SLinus Torvalds if (IS_ERR(resp_skb)) { 6881da177e4SLinus Torvalds err = PTR_ERR(resp_skb); 6891da177e4SLinus Torvalds } else { 6901da177e4SLinus Torvalds err = netlink_unicast(xfrm_nl, resp_skb, 6911da177e4SLinus Torvalds NETLINK_CB(skb).pid, MSG_DONTWAIT); 6921da177e4SLinus Torvalds } 6931da177e4SLinus Torvalds xfrm_state_put(x); 6941da177e4SLinus Torvalds out_noput: 6951da177e4SLinus Torvalds return err; 6961da177e4SLinus Torvalds } 6971da177e4SLinus Torvalds 6981da177e4SLinus Torvalds static int verify_userspi_info(struct xfrm_userspi_info *p) 6991da177e4SLinus Torvalds { 7001da177e4SLinus Torvalds switch (p->info.id.proto) { 7011da177e4SLinus Torvalds case IPPROTO_AH: 7021da177e4SLinus Torvalds case IPPROTO_ESP: 7031da177e4SLinus Torvalds break; 7041da177e4SLinus Torvalds 7051da177e4SLinus Torvalds case IPPROTO_COMP: 7061da177e4SLinus Torvalds /* IPCOMP spi is 16-bits. */ 7071da177e4SLinus Torvalds if (p->max >= 0x10000) 7081da177e4SLinus Torvalds return -EINVAL; 7091da177e4SLinus Torvalds break; 7101da177e4SLinus Torvalds 7111da177e4SLinus Torvalds default: 7121da177e4SLinus Torvalds return -EINVAL; 7131da177e4SLinus Torvalds }; 7141da177e4SLinus Torvalds 7151da177e4SLinus Torvalds if (p->min > p->max) 7161da177e4SLinus Torvalds return -EINVAL; 7171da177e4SLinus Torvalds 7181da177e4SLinus Torvalds return 0; 7191da177e4SLinus Torvalds } 7201da177e4SLinus Torvalds 7211da177e4SLinus Torvalds static int xfrm_alloc_userspi(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma) 7221da177e4SLinus Torvalds { 7231da177e4SLinus Torvalds struct xfrm_state *x; 7241da177e4SLinus Torvalds struct xfrm_userspi_info *p; 7251da177e4SLinus Torvalds struct sk_buff *resp_skb; 7261da177e4SLinus Torvalds xfrm_address_t *daddr; 7271da177e4SLinus Torvalds int family; 7281da177e4SLinus Torvalds int err; 7291da177e4SLinus Torvalds 7301da177e4SLinus Torvalds p = NLMSG_DATA(nlh); 7311da177e4SLinus Torvalds err = verify_userspi_info(p); 7321da177e4SLinus Torvalds if (err) 7331da177e4SLinus Torvalds goto out_noput; 7341da177e4SLinus Torvalds 7351da177e4SLinus Torvalds family = p->info.family; 7361da177e4SLinus Torvalds daddr = &p->info.id.daddr; 7371da177e4SLinus Torvalds 7381da177e4SLinus Torvalds x = NULL; 7391da177e4SLinus Torvalds if (p->info.seq) { 7401da177e4SLinus Torvalds x = xfrm_find_acq_byseq(p->info.seq); 7411da177e4SLinus Torvalds if (x && xfrm_addr_cmp(&x->id.daddr, daddr, family)) { 7421da177e4SLinus Torvalds xfrm_state_put(x); 7431da177e4SLinus Torvalds x = NULL; 7441da177e4SLinus Torvalds } 7451da177e4SLinus Torvalds } 7461da177e4SLinus Torvalds 7471da177e4SLinus Torvalds if (!x) 7481da177e4SLinus Torvalds x = xfrm_find_acq(p->info.mode, p->info.reqid, 7491da177e4SLinus Torvalds p->info.id.proto, daddr, 7501da177e4SLinus Torvalds &p->info.saddr, 1, 7511da177e4SLinus Torvalds family); 7521da177e4SLinus Torvalds err = -ENOENT; 7531da177e4SLinus Torvalds if (x == NULL) 7541da177e4SLinus Torvalds goto out_noput; 7551da177e4SLinus Torvalds 7561da177e4SLinus Torvalds resp_skb = ERR_PTR(-ENOENT); 7571da177e4SLinus Torvalds 7581da177e4SLinus Torvalds spin_lock_bh(&x->lock); 7591da177e4SLinus Torvalds if (x->km.state != XFRM_STATE_DEAD) { 7601da177e4SLinus Torvalds xfrm_alloc_spi(x, htonl(p->min), htonl(p->max)); 7611da177e4SLinus Torvalds if (x->id.spi) 7621da177e4SLinus Torvalds resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq); 7631da177e4SLinus Torvalds } 7641da177e4SLinus Torvalds spin_unlock_bh(&x->lock); 7651da177e4SLinus Torvalds 7661da177e4SLinus Torvalds if (IS_ERR(resp_skb)) { 7671da177e4SLinus Torvalds err = PTR_ERR(resp_skb); 7681da177e4SLinus Torvalds goto out; 7691da177e4SLinus Torvalds } 7701da177e4SLinus Torvalds 7711da177e4SLinus Torvalds err = netlink_unicast(xfrm_nl, resp_skb, 7721da177e4SLinus Torvalds NETLINK_CB(skb).pid, MSG_DONTWAIT); 7731da177e4SLinus Torvalds 7741da177e4SLinus Torvalds out: 7751da177e4SLinus Torvalds xfrm_state_put(x); 7761da177e4SLinus Torvalds out_noput: 7771da177e4SLinus Torvalds return err; 7781da177e4SLinus Torvalds } 7791da177e4SLinus Torvalds 780b798a9edSJamal Hadi Salim static int verify_policy_dir(u8 dir) 7811da177e4SLinus Torvalds { 7821da177e4SLinus Torvalds switch (dir) { 7831da177e4SLinus Torvalds case XFRM_POLICY_IN: 7841da177e4SLinus Torvalds case XFRM_POLICY_OUT: 7851da177e4SLinus Torvalds case XFRM_POLICY_FWD: 7861da177e4SLinus Torvalds break; 7871da177e4SLinus Torvalds 7881da177e4SLinus Torvalds default: 7891da177e4SLinus Torvalds return -EINVAL; 7901da177e4SLinus Torvalds }; 7911da177e4SLinus Torvalds 7921da177e4SLinus Torvalds return 0; 7931da177e4SLinus Torvalds } 7941da177e4SLinus Torvalds 795b798a9edSJamal Hadi Salim static int verify_policy_type(u8 type) 796f7b6983fSMasahide NAKAMURA { 797f7b6983fSMasahide NAKAMURA switch (type) { 798f7b6983fSMasahide NAKAMURA case XFRM_POLICY_TYPE_MAIN: 799f7b6983fSMasahide NAKAMURA #ifdef CONFIG_XFRM_SUB_POLICY 800f7b6983fSMasahide NAKAMURA case XFRM_POLICY_TYPE_SUB: 801f7b6983fSMasahide NAKAMURA #endif 802f7b6983fSMasahide NAKAMURA break; 803f7b6983fSMasahide NAKAMURA 804f7b6983fSMasahide NAKAMURA default: 805f7b6983fSMasahide NAKAMURA return -EINVAL; 806f7b6983fSMasahide NAKAMURA }; 807f7b6983fSMasahide NAKAMURA 808f7b6983fSMasahide NAKAMURA return 0; 809f7b6983fSMasahide NAKAMURA } 810f7b6983fSMasahide NAKAMURA 8111da177e4SLinus Torvalds static int verify_newpolicy_info(struct xfrm_userpolicy_info *p) 8121da177e4SLinus Torvalds { 8131da177e4SLinus Torvalds switch (p->share) { 8141da177e4SLinus Torvalds case XFRM_SHARE_ANY: 8151da177e4SLinus Torvalds case XFRM_SHARE_SESSION: 8161da177e4SLinus Torvalds case XFRM_SHARE_USER: 8171da177e4SLinus Torvalds case XFRM_SHARE_UNIQUE: 8181da177e4SLinus Torvalds break; 8191da177e4SLinus Torvalds 8201da177e4SLinus Torvalds default: 8211da177e4SLinus Torvalds return -EINVAL; 8221da177e4SLinus Torvalds }; 8231da177e4SLinus Torvalds 8241da177e4SLinus Torvalds switch (p->action) { 8251da177e4SLinus Torvalds case XFRM_POLICY_ALLOW: 8261da177e4SLinus Torvalds case XFRM_POLICY_BLOCK: 8271da177e4SLinus Torvalds break; 8281da177e4SLinus Torvalds 8291da177e4SLinus Torvalds default: 8301da177e4SLinus Torvalds return -EINVAL; 8311da177e4SLinus Torvalds }; 8321da177e4SLinus Torvalds 8331da177e4SLinus Torvalds switch (p->sel.family) { 8341da177e4SLinus Torvalds case AF_INET: 8351da177e4SLinus Torvalds break; 8361da177e4SLinus Torvalds 8371da177e4SLinus Torvalds case AF_INET6: 8381da177e4SLinus Torvalds #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) 8391da177e4SLinus Torvalds break; 8401da177e4SLinus Torvalds #else 8411da177e4SLinus Torvalds return -EAFNOSUPPORT; 8421da177e4SLinus Torvalds #endif 8431da177e4SLinus Torvalds 8441da177e4SLinus Torvalds default: 8451da177e4SLinus Torvalds return -EINVAL; 8461da177e4SLinus Torvalds }; 8471da177e4SLinus Torvalds 8481da177e4SLinus Torvalds return verify_policy_dir(p->dir); 8491da177e4SLinus Torvalds } 8501da177e4SLinus Torvalds 851df71837dSTrent Jaeger static int copy_from_user_sec_ctx(struct xfrm_policy *pol, struct rtattr **xfrma) 852df71837dSTrent Jaeger { 853df71837dSTrent Jaeger struct rtattr *rt = xfrma[XFRMA_SEC_CTX-1]; 854df71837dSTrent Jaeger struct xfrm_user_sec_ctx *uctx; 855df71837dSTrent Jaeger 856df71837dSTrent Jaeger if (!rt) 857df71837dSTrent Jaeger return 0; 858df71837dSTrent Jaeger 859df71837dSTrent Jaeger uctx = RTA_DATA(rt); 860df71837dSTrent Jaeger return security_xfrm_policy_alloc(pol, uctx); 861df71837dSTrent Jaeger } 862df71837dSTrent Jaeger 8631da177e4SLinus Torvalds static void copy_templates(struct xfrm_policy *xp, struct xfrm_user_tmpl *ut, 8641da177e4SLinus Torvalds int nr) 8651da177e4SLinus Torvalds { 8661da177e4SLinus Torvalds int i; 8671da177e4SLinus Torvalds 8681da177e4SLinus Torvalds xp->xfrm_nr = nr; 8691da177e4SLinus Torvalds for (i = 0; i < nr; i++, ut++) { 8701da177e4SLinus Torvalds struct xfrm_tmpl *t = &xp->xfrm_vec[i]; 8711da177e4SLinus Torvalds 8721da177e4SLinus Torvalds memcpy(&t->id, &ut->id, sizeof(struct xfrm_id)); 8731da177e4SLinus Torvalds memcpy(&t->saddr, &ut->saddr, 8741da177e4SLinus Torvalds sizeof(xfrm_address_t)); 8751da177e4SLinus Torvalds t->reqid = ut->reqid; 8761da177e4SLinus Torvalds t->mode = ut->mode; 8771da177e4SLinus Torvalds t->share = ut->share; 8781da177e4SLinus Torvalds t->optional = ut->optional; 8791da177e4SLinus Torvalds t->aalgos = ut->aalgos; 8801da177e4SLinus Torvalds t->ealgos = ut->ealgos; 8811da177e4SLinus Torvalds t->calgos = ut->calgos; 8828511d01dSMiika Komu t->encap_family = ut->family; 8831da177e4SLinus Torvalds } 8841da177e4SLinus Torvalds } 8851da177e4SLinus Torvalds 886b4ad86bfSDavid S. Miller static int validate_tmpl(int nr, struct xfrm_user_tmpl *ut, u16 family) 887b4ad86bfSDavid S. Miller { 888b4ad86bfSDavid S. Miller int i; 889b4ad86bfSDavid S. Miller 890b4ad86bfSDavid S. Miller if (nr > XFRM_MAX_DEPTH) 891b4ad86bfSDavid S. Miller return -EINVAL; 892b4ad86bfSDavid S. Miller 893b4ad86bfSDavid S. Miller for (i = 0; i < nr; i++) { 894b4ad86bfSDavid S. Miller /* We never validated the ut->family value, so many 895b4ad86bfSDavid S. Miller * applications simply leave it at zero. The check was 896b4ad86bfSDavid S. Miller * never made and ut->family was ignored because all 897b4ad86bfSDavid S. Miller * templates could be assumed to have the same family as 898b4ad86bfSDavid S. Miller * the policy itself. Now that we will have ipv4-in-ipv6 899b4ad86bfSDavid S. Miller * and ipv6-in-ipv4 tunnels, this is no longer true. 900b4ad86bfSDavid S. Miller */ 901b4ad86bfSDavid S. Miller if (!ut[i].family) 902b4ad86bfSDavid S. Miller ut[i].family = family; 903b4ad86bfSDavid S. Miller 904b4ad86bfSDavid S. Miller switch (ut[i].family) { 905b4ad86bfSDavid S. Miller case AF_INET: 906b4ad86bfSDavid S. Miller break; 907b4ad86bfSDavid S. Miller #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) 908b4ad86bfSDavid S. Miller case AF_INET6: 909b4ad86bfSDavid S. Miller break; 910b4ad86bfSDavid S. Miller #endif 911b4ad86bfSDavid S. Miller default: 912b4ad86bfSDavid S. Miller return -EINVAL; 913b4ad86bfSDavid S. Miller }; 914b4ad86bfSDavid S. Miller } 915b4ad86bfSDavid S. Miller 916b4ad86bfSDavid S. Miller return 0; 917b4ad86bfSDavid S. Miller } 918b4ad86bfSDavid S. Miller 9191da177e4SLinus Torvalds static int copy_from_user_tmpl(struct xfrm_policy *pol, struct rtattr **xfrma) 9201da177e4SLinus Torvalds { 9211da177e4SLinus Torvalds struct rtattr *rt = xfrma[XFRMA_TMPL-1]; 9221da177e4SLinus Torvalds 9231da177e4SLinus Torvalds if (!rt) { 9241da177e4SLinus Torvalds pol->xfrm_nr = 0; 9251da177e4SLinus Torvalds } else { 926b4ad86bfSDavid S. Miller struct xfrm_user_tmpl *utmpl = RTA_DATA(rt); 927b4ad86bfSDavid S. Miller int nr = (rt->rta_len - sizeof(*rt)) / sizeof(*utmpl); 928b4ad86bfSDavid S. Miller int err; 9291da177e4SLinus Torvalds 930b4ad86bfSDavid S. Miller err = validate_tmpl(nr, utmpl, pol->family); 931b4ad86bfSDavid S. Miller if (err) 932b4ad86bfSDavid S. Miller return err; 9331da177e4SLinus Torvalds 9341da177e4SLinus Torvalds copy_templates(pol, RTA_DATA(rt), nr); 9351da177e4SLinus Torvalds } 9361da177e4SLinus Torvalds return 0; 9371da177e4SLinus Torvalds } 9381da177e4SLinus Torvalds 939f7b6983fSMasahide NAKAMURA static int copy_from_user_policy_type(u8 *tp, struct rtattr **xfrma) 940f7b6983fSMasahide NAKAMURA { 941f7b6983fSMasahide NAKAMURA struct rtattr *rt = xfrma[XFRMA_POLICY_TYPE-1]; 942f7b6983fSMasahide NAKAMURA struct xfrm_userpolicy_type *upt; 943b798a9edSJamal Hadi Salim u8 type = XFRM_POLICY_TYPE_MAIN; 944f7b6983fSMasahide NAKAMURA int err; 945f7b6983fSMasahide NAKAMURA 946f7b6983fSMasahide NAKAMURA if (rt) { 947f7b6983fSMasahide NAKAMURA if (rt->rta_len < sizeof(*upt)) 948f7b6983fSMasahide NAKAMURA return -EINVAL; 949f7b6983fSMasahide NAKAMURA 950f7b6983fSMasahide NAKAMURA upt = RTA_DATA(rt); 951f7b6983fSMasahide NAKAMURA type = upt->type; 952f7b6983fSMasahide NAKAMURA } 953f7b6983fSMasahide NAKAMURA 954f7b6983fSMasahide NAKAMURA err = verify_policy_type(type); 955f7b6983fSMasahide NAKAMURA if (err) 956f7b6983fSMasahide NAKAMURA return err; 957f7b6983fSMasahide NAKAMURA 958f7b6983fSMasahide NAKAMURA *tp = type; 959f7b6983fSMasahide NAKAMURA return 0; 960f7b6983fSMasahide NAKAMURA } 961f7b6983fSMasahide NAKAMURA 9621da177e4SLinus Torvalds static void copy_from_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p) 9631da177e4SLinus Torvalds { 9641da177e4SLinus Torvalds xp->priority = p->priority; 9651da177e4SLinus Torvalds xp->index = p->index; 9661da177e4SLinus Torvalds memcpy(&xp->selector, &p->sel, sizeof(xp->selector)); 9671da177e4SLinus Torvalds memcpy(&xp->lft, &p->lft, sizeof(xp->lft)); 9681da177e4SLinus Torvalds xp->action = p->action; 9691da177e4SLinus Torvalds xp->flags = p->flags; 9701da177e4SLinus Torvalds xp->family = p->sel.family; 9711da177e4SLinus Torvalds /* XXX xp->share = p->share; */ 9721da177e4SLinus Torvalds } 9731da177e4SLinus Torvalds 9741da177e4SLinus Torvalds static void copy_to_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p, int dir) 9751da177e4SLinus Torvalds { 9761da177e4SLinus Torvalds memcpy(&p->sel, &xp->selector, sizeof(p->sel)); 9771da177e4SLinus Torvalds memcpy(&p->lft, &xp->lft, sizeof(p->lft)); 9781da177e4SLinus Torvalds memcpy(&p->curlft, &xp->curlft, sizeof(p->curlft)); 9791da177e4SLinus Torvalds p->priority = xp->priority; 9801da177e4SLinus Torvalds p->index = xp->index; 9811da177e4SLinus Torvalds p->sel.family = xp->family; 9821da177e4SLinus Torvalds p->dir = dir; 9831da177e4SLinus Torvalds p->action = xp->action; 9841da177e4SLinus Torvalds p->flags = xp->flags; 9851da177e4SLinus Torvalds p->share = XFRM_SHARE_ANY; /* XXX xp->share */ 9861da177e4SLinus Torvalds } 9871da177e4SLinus Torvalds 9881da177e4SLinus Torvalds static struct xfrm_policy *xfrm_policy_construct(struct xfrm_userpolicy_info *p, struct rtattr **xfrma, int *errp) 9891da177e4SLinus Torvalds { 9901da177e4SLinus Torvalds struct xfrm_policy *xp = xfrm_policy_alloc(GFP_KERNEL); 9911da177e4SLinus Torvalds int err; 9921da177e4SLinus Torvalds 9931da177e4SLinus Torvalds if (!xp) { 9941da177e4SLinus Torvalds *errp = -ENOMEM; 9951da177e4SLinus Torvalds return NULL; 9961da177e4SLinus Torvalds } 9971da177e4SLinus Torvalds 9981da177e4SLinus Torvalds copy_from_user_policy(xp, p); 999df71837dSTrent Jaeger 1000f7b6983fSMasahide NAKAMURA err = copy_from_user_policy_type(&xp->type, xfrma); 1001f7b6983fSMasahide NAKAMURA if (err) 1002f7b6983fSMasahide NAKAMURA goto error; 1003f7b6983fSMasahide NAKAMURA 1004df71837dSTrent Jaeger if (!(err = copy_from_user_tmpl(xp, xfrma))) 1005df71837dSTrent Jaeger err = copy_from_user_sec_ctx(xp, xfrma); 1006f7b6983fSMasahide NAKAMURA if (err) 1007f7b6983fSMasahide NAKAMURA goto error; 10081da177e4SLinus Torvalds 10091da177e4SLinus Torvalds return xp; 1010f7b6983fSMasahide NAKAMURA error: 1011f7b6983fSMasahide NAKAMURA *errp = err; 1012f7b6983fSMasahide NAKAMURA kfree(xp); 1013f7b6983fSMasahide NAKAMURA return NULL; 10141da177e4SLinus Torvalds } 10151da177e4SLinus Torvalds 10161da177e4SLinus Torvalds static int xfrm_add_policy(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma) 10171da177e4SLinus Torvalds { 10181da177e4SLinus Torvalds struct xfrm_userpolicy_info *p = NLMSG_DATA(nlh); 10191da177e4SLinus Torvalds struct xfrm_policy *xp; 102026b15dadSJamal Hadi Salim struct km_event c; 10211da177e4SLinus Torvalds int err; 10221da177e4SLinus Torvalds int excl; 10231da177e4SLinus Torvalds 10241da177e4SLinus Torvalds err = verify_newpolicy_info(p); 10251da177e4SLinus Torvalds if (err) 10261da177e4SLinus Torvalds return err; 1027df71837dSTrent Jaeger err = verify_sec_ctx_len((struct rtattr **)xfrma); 1028df71837dSTrent Jaeger if (err) 1029df71837dSTrent Jaeger return err; 10301da177e4SLinus Torvalds 10311da177e4SLinus Torvalds xp = xfrm_policy_construct(p, (struct rtattr **)xfrma, &err); 10321da177e4SLinus Torvalds if (!xp) 10331da177e4SLinus Torvalds return err; 10341da177e4SLinus Torvalds 103526b15dadSJamal Hadi Salim /* shouldnt excl be based on nlh flags?? 103626b15dadSJamal Hadi Salim * Aha! this is anti-netlink really i.e more pfkey derived 103726b15dadSJamal Hadi Salim * in netlink excl is a flag and you wouldnt need 103826b15dadSJamal Hadi Salim * a type XFRM_MSG_UPDPOLICY - JHS */ 10391da177e4SLinus Torvalds excl = nlh->nlmsg_type == XFRM_MSG_NEWPOLICY; 10401da177e4SLinus Torvalds err = xfrm_policy_insert(p->dir, xp, excl); 1041*161a09e7SJoy Latten xfrm_audit_log(NETLINK_CB(skb).loginuid, NETLINK_CB(skb).sid, 1042*161a09e7SJoy Latten AUDIT_MAC_IPSEC_DELSPD, err ? 0 : 1, xp, NULL); 1043*161a09e7SJoy Latten 10441da177e4SLinus Torvalds if (err) { 10455f8ac64bSTrent Jaeger security_xfrm_policy_free(xp); 10461da177e4SLinus Torvalds kfree(xp); 10471da177e4SLinus Torvalds return err; 10481da177e4SLinus Torvalds } 10491da177e4SLinus Torvalds 1050f60f6b8fSHerbert Xu c.event = nlh->nlmsg_type; 105126b15dadSJamal Hadi Salim c.seq = nlh->nlmsg_seq; 105226b15dadSJamal Hadi Salim c.pid = nlh->nlmsg_pid; 105326b15dadSJamal Hadi Salim km_policy_notify(xp, p->dir, &c); 105426b15dadSJamal Hadi Salim 10551da177e4SLinus Torvalds xfrm_pol_put(xp); 10561da177e4SLinus Torvalds 10571da177e4SLinus Torvalds return 0; 10581da177e4SLinus Torvalds } 10591da177e4SLinus Torvalds 10601da177e4SLinus Torvalds static int copy_to_user_tmpl(struct xfrm_policy *xp, struct sk_buff *skb) 10611da177e4SLinus Torvalds { 10621da177e4SLinus Torvalds struct xfrm_user_tmpl vec[XFRM_MAX_DEPTH]; 10631da177e4SLinus Torvalds int i; 10641da177e4SLinus Torvalds 10651da177e4SLinus Torvalds if (xp->xfrm_nr == 0) 10661da177e4SLinus Torvalds return 0; 10671da177e4SLinus Torvalds 10681da177e4SLinus Torvalds for (i = 0; i < xp->xfrm_nr; i++) { 10691da177e4SLinus Torvalds struct xfrm_user_tmpl *up = &vec[i]; 10701da177e4SLinus Torvalds struct xfrm_tmpl *kp = &xp->xfrm_vec[i]; 10711da177e4SLinus Torvalds 10721da177e4SLinus Torvalds memcpy(&up->id, &kp->id, sizeof(up->id)); 10738511d01dSMiika Komu up->family = kp->encap_family; 10741da177e4SLinus Torvalds memcpy(&up->saddr, &kp->saddr, sizeof(up->saddr)); 10751da177e4SLinus Torvalds up->reqid = kp->reqid; 10761da177e4SLinus Torvalds up->mode = kp->mode; 10771da177e4SLinus Torvalds up->share = kp->share; 10781da177e4SLinus Torvalds up->optional = kp->optional; 10791da177e4SLinus Torvalds up->aalgos = kp->aalgos; 10801da177e4SLinus Torvalds up->ealgos = kp->ealgos; 10811da177e4SLinus Torvalds up->calgos = kp->calgos; 10821da177e4SLinus Torvalds } 10831da177e4SLinus Torvalds RTA_PUT(skb, XFRMA_TMPL, 10841da177e4SLinus Torvalds (sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr), 10851da177e4SLinus Torvalds vec); 10861da177e4SLinus Torvalds 10871da177e4SLinus Torvalds return 0; 10881da177e4SLinus Torvalds 10891da177e4SLinus Torvalds rtattr_failure: 10901da177e4SLinus Torvalds return -1; 10911da177e4SLinus Torvalds } 10921da177e4SLinus Torvalds 10930d681623SSerge Hallyn static int copy_sec_ctx(struct xfrm_sec_ctx *s, struct sk_buff *skb) 1094df71837dSTrent Jaeger { 10950d681623SSerge Hallyn int ctx_size = sizeof(struct xfrm_sec_ctx) + s->ctx_len; 1096df71837dSTrent Jaeger struct rtattr *rt = __RTA_PUT(skb, XFRMA_SEC_CTX, ctx_size); 1097df71837dSTrent Jaeger struct xfrm_user_sec_ctx *uctx = RTA_DATA(rt); 1098df71837dSTrent Jaeger 1099df71837dSTrent Jaeger uctx->exttype = XFRMA_SEC_CTX; 1100df71837dSTrent Jaeger uctx->len = ctx_size; 11010d681623SSerge Hallyn uctx->ctx_doi = s->ctx_doi; 11020d681623SSerge Hallyn uctx->ctx_alg = s->ctx_alg; 11030d681623SSerge Hallyn uctx->ctx_len = s->ctx_len; 11040d681623SSerge Hallyn memcpy(uctx + 1, s->ctx_str, s->ctx_len); 1105df71837dSTrent Jaeger return 0; 1106df71837dSTrent Jaeger 1107df71837dSTrent Jaeger rtattr_failure: 1108df71837dSTrent Jaeger return -1; 1109df71837dSTrent Jaeger } 1110df71837dSTrent Jaeger 11110d681623SSerge Hallyn static inline int copy_to_user_state_sec_ctx(struct xfrm_state *x, struct sk_buff *skb) 11120d681623SSerge Hallyn { 11130d681623SSerge Hallyn if (x->security) { 11140d681623SSerge Hallyn return copy_sec_ctx(x->security, skb); 11150d681623SSerge Hallyn } 11160d681623SSerge Hallyn return 0; 11170d681623SSerge Hallyn } 11180d681623SSerge Hallyn 11190d681623SSerge Hallyn static inline int copy_to_user_sec_ctx(struct xfrm_policy *xp, struct sk_buff *skb) 11200d681623SSerge Hallyn { 11210d681623SSerge Hallyn if (xp->security) { 11220d681623SSerge Hallyn return copy_sec_ctx(xp->security, skb); 11230d681623SSerge Hallyn } 11240d681623SSerge Hallyn return 0; 11250d681623SSerge Hallyn } 11260d681623SSerge Hallyn 1127f7b6983fSMasahide NAKAMURA #ifdef CONFIG_XFRM_SUB_POLICY 1128b798a9edSJamal Hadi Salim static int copy_to_user_policy_type(u8 type, struct sk_buff *skb) 1129f7b6983fSMasahide NAKAMURA { 1130f7b6983fSMasahide NAKAMURA struct xfrm_userpolicy_type upt; 1131f7b6983fSMasahide NAKAMURA 1132f7b6983fSMasahide NAKAMURA memset(&upt, 0, sizeof(upt)); 11331459bb36SJamal Hadi Salim upt.type = type; 1134f7b6983fSMasahide NAKAMURA 1135f7b6983fSMasahide NAKAMURA RTA_PUT(skb, XFRMA_POLICY_TYPE, sizeof(upt), &upt); 1136f7b6983fSMasahide NAKAMURA 1137f7b6983fSMasahide NAKAMURA return 0; 1138f7b6983fSMasahide NAKAMURA 1139f7b6983fSMasahide NAKAMURA rtattr_failure: 1140f7b6983fSMasahide NAKAMURA return -1; 1141f7b6983fSMasahide NAKAMURA } 1142f7b6983fSMasahide NAKAMURA 1143f7b6983fSMasahide NAKAMURA #else 1144b798a9edSJamal Hadi Salim static inline int copy_to_user_policy_type(u8 type, struct sk_buff *skb) 1145f7b6983fSMasahide NAKAMURA { 1146f7b6983fSMasahide NAKAMURA return 0; 1147f7b6983fSMasahide NAKAMURA } 1148f7b6983fSMasahide NAKAMURA #endif 1149f7b6983fSMasahide NAKAMURA 11501da177e4SLinus Torvalds static int dump_one_policy(struct xfrm_policy *xp, int dir, int count, void *ptr) 11511da177e4SLinus Torvalds { 11521da177e4SLinus Torvalds struct xfrm_dump_info *sp = ptr; 11531da177e4SLinus Torvalds struct xfrm_userpolicy_info *p; 11541da177e4SLinus Torvalds struct sk_buff *in_skb = sp->in_skb; 11551da177e4SLinus Torvalds struct sk_buff *skb = sp->out_skb; 11561da177e4SLinus Torvalds struct nlmsghdr *nlh; 11571da177e4SLinus Torvalds unsigned char *b = skb->tail; 11581da177e4SLinus Torvalds 11591da177e4SLinus Torvalds if (sp->this_idx < sp->start_idx) 11601da177e4SLinus Torvalds goto out; 11611da177e4SLinus Torvalds 11621da177e4SLinus Torvalds nlh = NLMSG_PUT(skb, NETLINK_CB(in_skb).pid, 11631da177e4SLinus Torvalds sp->nlmsg_seq, 11641da177e4SLinus Torvalds XFRM_MSG_NEWPOLICY, sizeof(*p)); 11651da177e4SLinus Torvalds p = NLMSG_DATA(nlh); 11661da177e4SLinus Torvalds nlh->nlmsg_flags = sp->nlmsg_flags; 11671da177e4SLinus Torvalds 11681da177e4SLinus Torvalds copy_to_user_policy(xp, p, dir); 11691da177e4SLinus Torvalds if (copy_to_user_tmpl(xp, skb) < 0) 11701da177e4SLinus Torvalds goto nlmsg_failure; 1171df71837dSTrent Jaeger if (copy_to_user_sec_ctx(xp, skb)) 1172df71837dSTrent Jaeger goto nlmsg_failure; 11731459bb36SJamal Hadi Salim if (copy_to_user_policy_type(xp->type, skb) < 0) 1174f7b6983fSMasahide NAKAMURA goto nlmsg_failure; 11751da177e4SLinus Torvalds 11761da177e4SLinus Torvalds nlh->nlmsg_len = skb->tail - b; 11771da177e4SLinus Torvalds out: 11781da177e4SLinus Torvalds sp->this_idx++; 11791da177e4SLinus Torvalds return 0; 11801da177e4SLinus Torvalds 11811da177e4SLinus Torvalds nlmsg_failure: 11821da177e4SLinus Torvalds skb_trim(skb, b - skb->data); 11831da177e4SLinus Torvalds return -1; 11841da177e4SLinus Torvalds } 11851da177e4SLinus Torvalds 11861da177e4SLinus Torvalds static int xfrm_dump_policy(struct sk_buff *skb, struct netlink_callback *cb) 11871da177e4SLinus Torvalds { 11881da177e4SLinus Torvalds struct xfrm_dump_info info; 11891da177e4SLinus Torvalds 11901da177e4SLinus Torvalds info.in_skb = cb->skb; 11911da177e4SLinus Torvalds info.out_skb = skb; 11921da177e4SLinus Torvalds info.nlmsg_seq = cb->nlh->nlmsg_seq; 11931da177e4SLinus Torvalds info.nlmsg_flags = NLM_F_MULTI; 11941da177e4SLinus Torvalds info.this_idx = 0; 11951da177e4SLinus Torvalds info.start_idx = cb->args[0]; 1196f7b6983fSMasahide NAKAMURA (void) xfrm_policy_walk(XFRM_POLICY_TYPE_MAIN, dump_one_policy, &info); 1197f7b6983fSMasahide NAKAMURA #ifdef CONFIG_XFRM_SUB_POLICY 1198f7b6983fSMasahide NAKAMURA (void) xfrm_policy_walk(XFRM_POLICY_TYPE_SUB, dump_one_policy, &info); 1199f7b6983fSMasahide NAKAMURA #endif 12001da177e4SLinus Torvalds cb->args[0] = info.this_idx; 12011da177e4SLinus Torvalds 12021da177e4SLinus Torvalds return skb->len; 12031da177e4SLinus Torvalds } 12041da177e4SLinus Torvalds 12051da177e4SLinus Torvalds static struct sk_buff *xfrm_policy_netlink(struct sk_buff *in_skb, 12061da177e4SLinus Torvalds struct xfrm_policy *xp, 12071da177e4SLinus Torvalds int dir, u32 seq) 12081da177e4SLinus Torvalds { 12091da177e4SLinus Torvalds struct xfrm_dump_info info; 12101da177e4SLinus Torvalds struct sk_buff *skb; 12111da177e4SLinus Torvalds 12121da177e4SLinus Torvalds skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL); 12131da177e4SLinus Torvalds if (!skb) 12141da177e4SLinus Torvalds return ERR_PTR(-ENOMEM); 12151da177e4SLinus Torvalds 12161da177e4SLinus Torvalds info.in_skb = in_skb; 12171da177e4SLinus Torvalds info.out_skb = skb; 12181da177e4SLinus Torvalds info.nlmsg_seq = seq; 12191da177e4SLinus Torvalds info.nlmsg_flags = 0; 12201da177e4SLinus Torvalds info.this_idx = info.start_idx = 0; 12211da177e4SLinus Torvalds 12221da177e4SLinus Torvalds if (dump_one_policy(xp, dir, 0, &info) < 0) { 12231da177e4SLinus Torvalds kfree_skb(skb); 12241da177e4SLinus Torvalds return NULL; 12251da177e4SLinus Torvalds } 12261da177e4SLinus Torvalds 12271da177e4SLinus Torvalds return skb; 12281da177e4SLinus Torvalds } 12291da177e4SLinus Torvalds 12301da177e4SLinus Torvalds static int xfrm_get_policy(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma) 12311da177e4SLinus Torvalds { 12321da177e4SLinus Torvalds struct xfrm_policy *xp; 12331da177e4SLinus Torvalds struct xfrm_userpolicy_id *p; 1234b798a9edSJamal Hadi Salim u8 type = XFRM_POLICY_TYPE_MAIN; 12351da177e4SLinus Torvalds int err; 123626b15dadSJamal Hadi Salim struct km_event c; 12371da177e4SLinus Torvalds int delete; 12381da177e4SLinus Torvalds 12391da177e4SLinus Torvalds p = NLMSG_DATA(nlh); 12401da177e4SLinus Torvalds delete = nlh->nlmsg_type == XFRM_MSG_DELPOLICY; 12411da177e4SLinus Torvalds 1242f7b6983fSMasahide NAKAMURA err = copy_from_user_policy_type(&type, (struct rtattr **)xfrma); 1243f7b6983fSMasahide NAKAMURA if (err) 1244f7b6983fSMasahide NAKAMURA return err; 1245f7b6983fSMasahide NAKAMURA 12461da177e4SLinus Torvalds err = verify_policy_dir(p->dir); 12471da177e4SLinus Torvalds if (err) 12481da177e4SLinus Torvalds return err; 12491da177e4SLinus Torvalds 12501da177e4SLinus Torvalds if (p->index) 1251f7b6983fSMasahide NAKAMURA xp = xfrm_policy_byid(type, p->dir, p->index, delete); 1252df71837dSTrent Jaeger else { 1253df71837dSTrent Jaeger struct rtattr **rtattrs = (struct rtattr **)xfrma; 1254df71837dSTrent Jaeger struct rtattr *rt = rtattrs[XFRMA_SEC_CTX-1]; 1255df71837dSTrent Jaeger struct xfrm_policy tmp; 1256df71837dSTrent Jaeger 1257df71837dSTrent Jaeger err = verify_sec_ctx_len(rtattrs); 1258df71837dSTrent Jaeger if (err) 1259df71837dSTrent Jaeger return err; 1260df71837dSTrent Jaeger 1261df71837dSTrent Jaeger memset(&tmp, 0, sizeof(struct xfrm_policy)); 1262df71837dSTrent Jaeger if (rt) { 1263df71837dSTrent Jaeger struct xfrm_user_sec_ctx *uctx = RTA_DATA(rt); 1264df71837dSTrent Jaeger 1265df71837dSTrent Jaeger if ((err = security_xfrm_policy_alloc(&tmp, uctx))) 1266df71837dSTrent Jaeger return err; 1267df71837dSTrent Jaeger } 1268f7b6983fSMasahide NAKAMURA xp = xfrm_policy_bysel_ctx(type, p->dir, &p->sel, tmp.security, delete); 1269df71837dSTrent Jaeger security_xfrm_policy_free(&tmp); 1270df71837dSTrent Jaeger } 1271*161a09e7SJoy Latten if (delete) 1272*161a09e7SJoy Latten xfrm_audit_log(NETLINK_CB(skb).loginuid, NETLINK_CB(skb).sid, 1273*161a09e7SJoy Latten AUDIT_MAC_IPSEC_DELSPD, (xp) ? 1 : 0, xp, NULL); 1274*161a09e7SJoy Latten 12751da177e4SLinus Torvalds if (xp == NULL) 12761da177e4SLinus Torvalds return -ENOENT; 12771da177e4SLinus Torvalds 12781da177e4SLinus Torvalds if (!delete) { 12791da177e4SLinus Torvalds struct sk_buff *resp_skb; 12801da177e4SLinus Torvalds 12811da177e4SLinus Torvalds resp_skb = xfrm_policy_netlink(skb, xp, p->dir, nlh->nlmsg_seq); 12821da177e4SLinus Torvalds if (IS_ERR(resp_skb)) { 12831da177e4SLinus Torvalds err = PTR_ERR(resp_skb); 12841da177e4SLinus Torvalds } else { 12851da177e4SLinus Torvalds err = netlink_unicast(xfrm_nl, resp_skb, 12861da177e4SLinus Torvalds NETLINK_CB(skb).pid, 12871da177e4SLinus Torvalds MSG_DONTWAIT); 12881da177e4SLinus Torvalds } 128926b15dadSJamal Hadi Salim } else { 12906f68dc37SDavid S. Miller if ((err = security_xfrm_policy_delete(xp)) != 0) 1291c8c05a8eSCatherine Zhang goto out; 1292e7443892SHerbert Xu c.data.byid = p->index; 1293f60f6b8fSHerbert Xu c.event = nlh->nlmsg_type; 129426b15dadSJamal Hadi Salim c.seq = nlh->nlmsg_seq; 129526b15dadSJamal Hadi Salim c.pid = nlh->nlmsg_pid; 129626b15dadSJamal Hadi Salim km_policy_notify(xp, p->dir, &c); 12971da177e4SLinus Torvalds } 12981da177e4SLinus Torvalds 12991da177e4SLinus Torvalds xfrm_pol_put(xp); 13001da177e4SLinus Torvalds 1301c8c05a8eSCatherine Zhang out: 13021da177e4SLinus Torvalds return err; 13031da177e4SLinus Torvalds } 13041da177e4SLinus Torvalds 13051da177e4SLinus Torvalds static int xfrm_flush_sa(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma) 13061da177e4SLinus Torvalds { 130726b15dadSJamal Hadi Salim struct km_event c; 13081da177e4SLinus Torvalds struct xfrm_usersa_flush *p = NLMSG_DATA(nlh); 1309*161a09e7SJoy Latten struct xfrm_audit audit_info; 13101da177e4SLinus Torvalds 1311*161a09e7SJoy Latten audit_info.loginuid = NETLINK_CB(skb).loginuid; 1312*161a09e7SJoy Latten audit_info.secid = NETLINK_CB(skb).sid; 1313*161a09e7SJoy Latten xfrm_state_flush(p->proto, &audit_info); 1314bf08867fSHerbert Xu c.data.proto = p->proto; 1315f60f6b8fSHerbert Xu c.event = nlh->nlmsg_type; 131626b15dadSJamal Hadi Salim c.seq = nlh->nlmsg_seq; 131726b15dadSJamal Hadi Salim c.pid = nlh->nlmsg_pid; 131826b15dadSJamal Hadi Salim km_state_notify(NULL, &c); 131926b15dadSJamal Hadi Salim 13201da177e4SLinus Torvalds return 0; 13211da177e4SLinus Torvalds } 13221da177e4SLinus Torvalds 1323d51d081dSJamal Hadi Salim 1324d51d081dSJamal Hadi Salim static int build_aevent(struct sk_buff *skb, struct xfrm_state *x, struct km_event *c) 1325d51d081dSJamal Hadi Salim { 1326d51d081dSJamal Hadi Salim struct xfrm_aevent_id *id; 1327d51d081dSJamal Hadi Salim struct nlmsghdr *nlh; 1328d51d081dSJamal Hadi Salim struct xfrm_lifetime_cur ltime; 1329d51d081dSJamal Hadi Salim unsigned char *b = skb->tail; 1330d51d081dSJamal Hadi Salim 1331d51d081dSJamal Hadi Salim nlh = NLMSG_PUT(skb, c->pid, c->seq, XFRM_MSG_NEWAE, sizeof(*id)); 1332d51d081dSJamal Hadi Salim id = NLMSG_DATA(nlh); 1333d51d081dSJamal Hadi Salim nlh->nlmsg_flags = 0; 1334d51d081dSJamal Hadi Salim 13352b5f6dccSJamal Hadi Salim memcpy(&id->sa_id.daddr, &x->id.daddr,sizeof(x->id.daddr)); 1336d51d081dSJamal Hadi Salim id->sa_id.spi = x->id.spi; 1337d51d081dSJamal Hadi Salim id->sa_id.family = x->props.family; 1338d51d081dSJamal Hadi Salim id->sa_id.proto = x->id.proto; 13392b5f6dccSJamal Hadi Salim memcpy(&id->saddr, &x->props.saddr,sizeof(x->props.saddr)); 13402b5f6dccSJamal Hadi Salim id->reqid = x->props.reqid; 1341d51d081dSJamal Hadi Salim id->flags = c->data.aevent; 1342d51d081dSJamal Hadi Salim 1343d51d081dSJamal Hadi Salim RTA_PUT(skb, XFRMA_REPLAY_VAL, sizeof(x->replay), &x->replay); 1344d51d081dSJamal Hadi Salim 1345d51d081dSJamal Hadi Salim ltime.bytes = x->curlft.bytes; 1346d51d081dSJamal Hadi Salim ltime.packets = x->curlft.packets; 1347d51d081dSJamal Hadi Salim ltime.add_time = x->curlft.add_time; 1348d51d081dSJamal Hadi Salim ltime.use_time = x->curlft.use_time; 1349d51d081dSJamal Hadi Salim 1350d51d081dSJamal Hadi Salim RTA_PUT(skb, XFRMA_LTIME_VAL, sizeof(struct xfrm_lifetime_cur), <ime); 1351d51d081dSJamal Hadi Salim 1352d51d081dSJamal Hadi Salim if (id->flags&XFRM_AE_RTHR) { 1353d51d081dSJamal Hadi Salim RTA_PUT(skb,XFRMA_REPLAY_THRESH,sizeof(u32),&x->replay_maxdiff); 1354d51d081dSJamal Hadi Salim } 1355d51d081dSJamal Hadi Salim 1356d51d081dSJamal Hadi Salim if (id->flags&XFRM_AE_ETHR) { 1357d51d081dSJamal Hadi Salim u32 etimer = x->replay_maxage*10/HZ; 1358d51d081dSJamal Hadi Salim RTA_PUT(skb,XFRMA_ETIMER_THRESH,sizeof(u32),&etimer); 1359d51d081dSJamal Hadi Salim } 1360d51d081dSJamal Hadi Salim 1361d51d081dSJamal Hadi Salim nlh->nlmsg_len = skb->tail - b; 1362d51d081dSJamal Hadi Salim return skb->len; 1363d51d081dSJamal Hadi Salim 1364d51d081dSJamal Hadi Salim rtattr_failure: 1365d51d081dSJamal Hadi Salim nlmsg_failure: 1366d51d081dSJamal Hadi Salim skb_trim(skb, b - skb->data); 1367d51d081dSJamal Hadi Salim return -1; 1368d51d081dSJamal Hadi Salim } 1369d51d081dSJamal Hadi Salim 1370d51d081dSJamal Hadi Salim static int xfrm_get_ae(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma) 1371d51d081dSJamal Hadi Salim { 1372d51d081dSJamal Hadi Salim struct xfrm_state *x; 1373d51d081dSJamal Hadi Salim struct sk_buff *r_skb; 1374d51d081dSJamal Hadi Salim int err; 1375d51d081dSJamal Hadi Salim struct km_event c; 1376d51d081dSJamal Hadi Salim struct xfrm_aevent_id *p = NLMSG_DATA(nlh); 1377d51d081dSJamal Hadi Salim int len = NLMSG_LENGTH(sizeof(struct xfrm_aevent_id)); 1378d51d081dSJamal Hadi Salim struct xfrm_usersa_id *id = &p->sa_id; 1379d51d081dSJamal Hadi Salim 1380d51d081dSJamal Hadi Salim len += RTA_SPACE(sizeof(struct xfrm_replay_state)); 1381d51d081dSJamal Hadi Salim len += RTA_SPACE(sizeof(struct xfrm_lifetime_cur)); 1382d51d081dSJamal Hadi Salim 1383d51d081dSJamal Hadi Salim if (p->flags&XFRM_AE_RTHR) 1384d51d081dSJamal Hadi Salim len+=RTA_SPACE(sizeof(u32)); 1385d51d081dSJamal Hadi Salim 1386d51d081dSJamal Hadi Salim if (p->flags&XFRM_AE_ETHR) 1387d51d081dSJamal Hadi Salim len+=RTA_SPACE(sizeof(u32)); 1388d51d081dSJamal Hadi Salim 1389d51d081dSJamal Hadi Salim r_skb = alloc_skb(len, GFP_ATOMIC); 1390d51d081dSJamal Hadi Salim if (r_skb == NULL) 1391d51d081dSJamal Hadi Salim return -ENOMEM; 1392d51d081dSJamal Hadi Salim 1393d51d081dSJamal Hadi Salim x = xfrm_state_lookup(&id->daddr, id->spi, id->proto, id->family); 1394d51d081dSJamal Hadi Salim if (x == NULL) { 1395d51d081dSJamal Hadi Salim kfree(r_skb); 1396d51d081dSJamal Hadi Salim return -ESRCH; 1397d51d081dSJamal Hadi Salim } 1398d51d081dSJamal Hadi Salim 1399d51d081dSJamal Hadi Salim /* 1400d51d081dSJamal Hadi Salim * XXX: is this lock really needed - none of the other 1401d51d081dSJamal Hadi Salim * gets lock (the concern is things getting updated 1402d51d081dSJamal Hadi Salim * while we are still reading) - jhs 1403d51d081dSJamal Hadi Salim */ 1404d51d081dSJamal Hadi Salim spin_lock_bh(&x->lock); 1405d51d081dSJamal Hadi Salim c.data.aevent = p->flags; 1406d51d081dSJamal Hadi Salim c.seq = nlh->nlmsg_seq; 1407d51d081dSJamal Hadi Salim c.pid = nlh->nlmsg_pid; 1408d51d081dSJamal Hadi Salim 1409d51d081dSJamal Hadi Salim if (build_aevent(r_skb, x, &c) < 0) 1410d51d081dSJamal Hadi Salim BUG(); 1411d51d081dSJamal Hadi Salim err = netlink_unicast(xfrm_nl, r_skb, 1412d51d081dSJamal Hadi Salim NETLINK_CB(skb).pid, MSG_DONTWAIT); 1413d51d081dSJamal Hadi Salim spin_unlock_bh(&x->lock); 1414d51d081dSJamal Hadi Salim xfrm_state_put(x); 1415d51d081dSJamal Hadi Salim return err; 1416d51d081dSJamal Hadi Salim } 1417d51d081dSJamal Hadi Salim 1418d51d081dSJamal Hadi Salim static int xfrm_new_ae(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma) 1419d51d081dSJamal Hadi Salim { 1420d51d081dSJamal Hadi Salim struct xfrm_state *x; 1421d51d081dSJamal Hadi Salim struct km_event c; 1422d51d081dSJamal Hadi Salim int err = - EINVAL; 1423d51d081dSJamal Hadi Salim struct xfrm_aevent_id *p = NLMSG_DATA(nlh); 1424d51d081dSJamal Hadi Salim struct rtattr *rp = xfrma[XFRMA_REPLAY_VAL-1]; 1425d51d081dSJamal Hadi Salim struct rtattr *lt = xfrma[XFRMA_LTIME_VAL-1]; 1426d51d081dSJamal Hadi Salim 1427d51d081dSJamal Hadi Salim if (!lt && !rp) 1428d51d081dSJamal Hadi Salim return err; 1429d51d081dSJamal Hadi Salim 1430d51d081dSJamal Hadi Salim /* pedantic mode - thou shalt sayeth replaceth */ 1431d51d081dSJamal Hadi Salim if (!(nlh->nlmsg_flags&NLM_F_REPLACE)) 1432d51d081dSJamal Hadi Salim return err; 1433d51d081dSJamal Hadi Salim 1434d51d081dSJamal Hadi Salim x = xfrm_state_lookup(&p->sa_id.daddr, p->sa_id.spi, p->sa_id.proto, p->sa_id.family); 1435d51d081dSJamal Hadi Salim if (x == NULL) 1436d51d081dSJamal Hadi Salim return -ESRCH; 1437d51d081dSJamal Hadi Salim 1438d51d081dSJamal Hadi Salim if (x->km.state != XFRM_STATE_VALID) 1439d51d081dSJamal Hadi Salim goto out; 1440d51d081dSJamal Hadi Salim 1441d51d081dSJamal Hadi Salim spin_lock_bh(&x->lock); 1442d51d081dSJamal Hadi Salim err = xfrm_update_ae_params(x,(struct rtattr **)xfrma); 1443d51d081dSJamal Hadi Salim spin_unlock_bh(&x->lock); 1444d51d081dSJamal Hadi Salim if (err < 0) 1445d51d081dSJamal Hadi Salim goto out; 1446d51d081dSJamal Hadi Salim 1447d51d081dSJamal Hadi Salim c.event = nlh->nlmsg_type; 1448d51d081dSJamal Hadi Salim c.seq = nlh->nlmsg_seq; 1449d51d081dSJamal Hadi Salim c.pid = nlh->nlmsg_pid; 1450d51d081dSJamal Hadi Salim c.data.aevent = XFRM_AE_CU; 1451d51d081dSJamal Hadi Salim km_state_notify(x, &c); 1452d51d081dSJamal Hadi Salim err = 0; 1453d51d081dSJamal Hadi Salim out: 1454d51d081dSJamal Hadi Salim xfrm_state_put(x); 1455d51d081dSJamal Hadi Salim return err; 1456d51d081dSJamal Hadi Salim } 1457d51d081dSJamal Hadi Salim 14581da177e4SLinus Torvalds static int xfrm_flush_policy(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma) 14591da177e4SLinus Torvalds { 146026b15dadSJamal Hadi Salim struct km_event c; 1461b798a9edSJamal Hadi Salim u8 type = XFRM_POLICY_TYPE_MAIN; 1462f7b6983fSMasahide NAKAMURA int err; 1463*161a09e7SJoy Latten struct xfrm_audit audit_info; 146426b15dadSJamal Hadi Salim 1465f7b6983fSMasahide NAKAMURA err = copy_from_user_policy_type(&type, (struct rtattr **)xfrma); 1466f7b6983fSMasahide NAKAMURA if (err) 1467f7b6983fSMasahide NAKAMURA return err; 1468f7b6983fSMasahide NAKAMURA 1469*161a09e7SJoy Latten audit_info.loginuid = NETLINK_CB(skb).loginuid; 1470*161a09e7SJoy Latten audit_info.secid = NETLINK_CB(skb).sid; 1471*161a09e7SJoy Latten xfrm_policy_flush(type, &audit_info); 1472f7b6983fSMasahide NAKAMURA c.data.type = type; 1473f60f6b8fSHerbert Xu c.event = nlh->nlmsg_type; 147426b15dadSJamal Hadi Salim c.seq = nlh->nlmsg_seq; 147526b15dadSJamal Hadi Salim c.pid = nlh->nlmsg_pid; 147626b15dadSJamal Hadi Salim km_policy_notify(NULL, 0, &c); 14771da177e4SLinus Torvalds return 0; 14781da177e4SLinus Torvalds } 14791da177e4SLinus Torvalds 14806c5c8ca7SJamal Hadi Salim static int xfrm_add_pol_expire(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma) 14816c5c8ca7SJamal Hadi Salim { 14826c5c8ca7SJamal Hadi Salim struct xfrm_policy *xp; 14836c5c8ca7SJamal Hadi Salim struct xfrm_user_polexpire *up = NLMSG_DATA(nlh); 14846c5c8ca7SJamal Hadi Salim struct xfrm_userpolicy_info *p = &up->pol; 1485b798a9edSJamal Hadi Salim u8 type = XFRM_POLICY_TYPE_MAIN; 14866c5c8ca7SJamal Hadi Salim int err = -ENOENT; 14876c5c8ca7SJamal Hadi Salim 1488f7b6983fSMasahide NAKAMURA err = copy_from_user_policy_type(&type, (struct rtattr **)xfrma); 1489f7b6983fSMasahide NAKAMURA if (err) 1490f7b6983fSMasahide NAKAMURA return err; 1491f7b6983fSMasahide NAKAMURA 14926c5c8ca7SJamal Hadi Salim if (p->index) 1493f7b6983fSMasahide NAKAMURA xp = xfrm_policy_byid(type, p->dir, p->index, 0); 14946c5c8ca7SJamal Hadi Salim else { 14956c5c8ca7SJamal Hadi Salim struct rtattr **rtattrs = (struct rtattr **)xfrma; 14966c5c8ca7SJamal Hadi Salim struct rtattr *rt = rtattrs[XFRMA_SEC_CTX-1]; 14976c5c8ca7SJamal Hadi Salim struct xfrm_policy tmp; 14986c5c8ca7SJamal Hadi Salim 14996c5c8ca7SJamal Hadi Salim err = verify_sec_ctx_len(rtattrs); 15006c5c8ca7SJamal Hadi Salim if (err) 15016c5c8ca7SJamal Hadi Salim return err; 15026c5c8ca7SJamal Hadi Salim 15036c5c8ca7SJamal Hadi Salim memset(&tmp, 0, sizeof(struct xfrm_policy)); 15046c5c8ca7SJamal Hadi Salim if (rt) { 15056c5c8ca7SJamal Hadi Salim struct xfrm_user_sec_ctx *uctx = RTA_DATA(rt); 15066c5c8ca7SJamal Hadi Salim 15076c5c8ca7SJamal Hadi Salim if ((err = security_xfrm_policy_alloc(&tmp, uctx))) 15086c5c8ca7SJamal Hadi Salim return err; 15096c5c8ca7SJamal Hadi Salim } 1510f7b6983fSMasahide NAKAMURA xp = xfrm_policy_bysel_ctx(type, p->dir, &p->sel, tmp.security, 0); 15116c5c8ca7SJamal Hadi Salim security_xfrm_policy_free(&tmp); 15126c5c8ca7SJamal Hadi Salim } 15136c5c8ca7SJamal Hadi Salim 15146c5c8ca7SJamal Hadi Salim if (xp == NULL) 15156c5c8ca7SJamal Hadi Salim return err; 15166c5c8ca7SJamal Hadi Salim read_lock(&xp->lock); 15176c5c8ca7SJamal Hadi Salim if (xp->dead) { 15186c5c8ca7SJamal Hadi Salim read_unlock(&xp->lock); 15196c5c8ca7SJamal Hadi Salim goto out; 15206c5c8ca7SJamal Hadi Salim } 15216c5c8ca7SJamal Hadi Salim 15226c5c8ca7SJamal Hadi Salim read_unlock(&xp->lock); 15236c5c8ca7SJamal Hadi Salim err = 0; 15246c5c8ca7SJamal Hadi Salim if (up->hard) { 15256c5c8ca7SJamal Hadi Salim xfrm_policy_delete(xp, p->dir); 1526*161a09e7SJoy Latten xfrm_audit_log(NETLINK_CB(skb).loginuid, NETLINK_CB(skb).sid, 1527*161a09e7SJoy Latten AUDIT_MAC_IPSEC_DELSPD, 1, xp, NULL); 1528*161a09e7SJoy Latten 15296c5c8ca7SJamal Hadi Salim } else { 15306c5c8ca7SJamal Hadi Salim // reset the timers here? 15316c5c8ca7SJamal Hadi Salim printk("Dont know what to do with soft policy expire\n"); 15326c5c8ca7SJamal Hadi Salim } 15336c5c8ca7SJamal Hadi Salim km_policy_expired(xp, p->dir, up->hard, current->pid); 15346c5c8ca7SJamal Hadi Salim 15356c5c8ca7SJamal Hadi Salim out: 15366c5c8ca7SJamal Hadi Salim xfrm_pol_put(xp); 15376c5c8ca7SJamal Hadi Salim return err; 15386c5c8ca7SJamal Hadi Salim } 15396c5c8ca7SJamal Hadi Salim 154053bc6b4dSJamal Hadi Salim static int xfrm_add_sa_expire(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma) 154153bc6b4dSJamal Hadi Salim { 154253bc6b4dSJamal Hadi Salim struct xfrm_state *x; 154353bc6b4dSJamal Hadi Salim int err; 154453bc6b4dSJamal Hadi Salim struct xfrm_user_expire *ue = NLMSG_DATA(nlh); 154553bc6b4dSJamal Hadi Salim struct xfrm_usersa_info *p = &ue->state; 154653bc6b4dSJamal Hadi Salim 154753bc6b4dSJamal Hadi Salim x = xfrm_state_lookup(&p->id.daddr, p->id.spi, p->id.proto, p->family); 154853bc6b4dSJamal Hadi Salim err = -ENOENT; 154953bc6b4dSJamal Hadi Salim 155053bc6b4dSJamal Hadi Salim if (x == NULL) 155153bc6b4dSJamal Hadi Salim return err; 155253bc6b4dSJamal Hadi Salim 155353bc6b4dSJamal Hadi Salim err = -EINVAL; 155453bc6b4dSJamal Hadi Salim 155553bc6b4dSJamal Hadi Salim spin_lock_bh(&x->lock); 155653bc6b4dSJamal Hadi Salim if (x->km.state != XFRM_STATE_VALID) 155753bc6b4dSJamal Hadi Salim goto out; 155853bc6b4dSJamal Hadi Salim km_state_expired(x, ue->hard, current->pid); 155953bc6b4dSJamal Hadi Salim 1560*161a09e7SJoy Latten if (ue->hard) { 156153bc6b4dSJamal Hadi Salim __xfrm_state_delete(x); 1562*161a09e7SJoy Latten xfrm_audit_log(NETLINK_CB(skb).loginuid, NETLINK_CB(skb).sid, 1563*161a09e7SJoy Latten AUDIT_MAC_IPSEC_DELSA, 1, NULL, x); 1564*161a09e7SJoy Latten } 156553bc6b4dSJamal Hadi Salim out: 156653bc6b4dSJamal Hadi Salim spin_unlock_bh(&x->lock); 156753bc6b4dSJamal Hadi Salim xfrm_state_put(x); 156853bc6b4dSJamal Hadi Salim return err; 156953bc6b4dSJamal Hadi Salim } 157053bc6b4dSJamal Hadi Salim 1571980ebd25SJamal Hadi Salim static int xfrm_add_acquire(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma) 1572980ebd25SJamal Hadi Salim { 1573980ebd25SJamal Hadi Salim struct xfrm_policy *xp; 1574980ebd25SJamal Hadi Salim struct xfrm_user_tmpl *ut; 1575980ebd25SJamal Hadi Salim int i; 1576980ebd25SJamal Hadi Salim struct rtattr *rt = xfrma[XFRMA_TMPL-1]; 1577980ebd25SJamal Hadi Salim 1578980ebd25SJamal Hadi Salim struct xfrm_user_acquire *ua = NLMSG_DATA(nlh); 1579980ebd25SJamal Hadi Salim struct xfrm_state *x = xfrm_state_alloc(); 1580980ebd25SJamal Hadi Salim int err = -ENOMEM; 1581980ebd25SJamal Hadi Salim 1582980ebd25SJamal Hadi Salim if (!x) 1583980ebd25SJamal Hadi Salim return err; 1584980ebd25SJamal Hadi Salim 1585980ebd25SJamal Hadi Salim err = verify_newpolicy_info(&ua->policy); 1586980ebd25SJamal Hadi Salim if (err) { 1587980ebd25SJamal Hadi Salim printk("BAD policy passed\n"); 1588980ebd25SJamal Hadi Salim kfree(x); 1589980ebd25SJamal Hadi Salim return err; 1590980ebd25SJamal Hadi Salim } 1591980ebd25SJamal Hadi Salim 1592980ebd25SJamal Hadi Salim /* build an XP */ 1593b4ad86bfSDavid S. Miller xp = xfrm_policy_construct(&ua->policy, (struct rtattr **) xfrma, &err); 1594b4ad86bfSDavid S. Miller if (!xp) { 1595980ebd25SJamal Hadi Salim kfree(x); 1596980ebd25SJamal Hadi Salim return err; 1597980ebd25SJamal Hadi Salim } 1598980ebd25SJamal Hadi Salim 1599980ebd25SJamal Hadi Salim memcpy(&x->id, &ua->id, sizeof(ua->id)); 1600980ebd25SJamal Hadi Salim memcpy(&x->props.saddr, &ua->saddr, sizeof(ua->saddr)); 1601980ebd25SJamal Hadi Salim memcpy(&x->sel, &ua->sel, sizeof(ua->sel)); 1602980ebd25SJamal Hadi Salim 1603980ebd25SJamal Hadi Salim ut = RTA_DATA(rt); 1604980ebd25SJamal Hadi Salim /* extract the templates and for each call km_key */ 1605980ebd25SJamal Hadi Salim for (i = 0; i < xp->xfrm_nr; i++, ut++) { 1606980ebd25SJamal Hadi Salim struct xfrm_tmpl *t = &xp->xfrm_vec[i]; 1607980ebd25SJamal Hadi Salim memcpy(&x->id, &t->id, sizeof(x->id)); 1608980ebd25SJamal Hadi Salim x->props.mode = t->mode; 1609980ebd25SJamal Hadi Salim x->props.reqid = t->reqid; 1610980ebd25SJamal Hadi Salim x->props.family = ut->family; 1611980ebd25SJamal Hadi Salim t->aalgos = ua->aalgos; 1612980ebd25SJamal Hadi Salim t->ealgos = ua->ealgos; 1613980ebd25SJamal Hadi Salim t->calgos = ua->calgos; 1614980ebd25SJamal Hadi Salim err = km_query(x, t, xp); 1615980ebd25SJamal Hadi Salim 1616980ebd25SJamal Hadi Salim } 1617980ebd25SJamal Hadi Salim 1618980ebd25SJamal Hadi Salim kfree(x); 1619980ebd25SJamal Hadi Salim kfree(xp); 1620980ebd25SJamal Hadi Salim 1621980ebd25SJamal Hadi Salim return 0; 1622980ebd25SJamal Hadi Salim } 1623980ebd25SJamal Hadi Salim 1624d51d081dSJamal Hadi Salim 1625492b558bSThomas Graf #define XMSGSIZE(type) NLMSG_LENGTH(sizeof(struct type)) 1626492b558bSThomas Graf 1627492b558bSThomas Graf static const int xfrm_msg_min[XFRM_NR_MSGTYPES] = { 1628492b558bSThomas Graf [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info), 1629492b558bSThomas Graf [XFRM_MSG_DELSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id), 1630492b558bSThomas Graf [XFRM_MSG_GETSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id), 1631492b558bSThomas Graf [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info), 1632492b558bSThomas Graf [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id), 1633492b558bSThomas Graf [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id), 1634492b558bSThomas Graf [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userspi_info), 1635980ebd25SJamal Hadi Salim [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_acquire), 163653bc6b4dSJamal Hadi Salim [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_expire), 1637492b558bSThomas Graf [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info), 1638492b558bSThomas Graf [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info), 16396c5c8ca7SJamal Hadi Salim [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_polexpire), 1640492b558bSThomas Graf [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_flush), 1641492b558bSThomas Graf [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = NLMSG_LENGTH(0), 1642d51d081dSJamal Hadi Salim [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id), 1643d51d081dSJamal Hadi Salim [XFRM_MSG_GETAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id), 164497a64b45SMasahide NAKAMURA [XFRM_MSG_REPORT - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_report), 16451da177e4SLinus Torvalds }; 16461da177e4SLinus Torvalds 1647492b558bSThomas Graf #undef XMSGSIZE 1648492b558bSThomas Graf 16491da177e4SLinus Torvalds static struct xfrm_link { 16501da177e4SLinus Torvalds int (*doit)(struct sk_buff *, struct nlmsghdr *, void **); 16511da177e4SLinus Torvalds int (*dump)(struct sk_buff *, struct netlink_callback *); 1652492b558bSThomas Graf } xfrm_dispatch[XFRM_NR_MSGTYPES] = { 1653492b558bSThomas Graf [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa }, 1654492b558bSThomas Graf [XFRM_MSG_DELSA - XFRM_MSG_BASE] = { .doit = xfrm_del_sa }, 1655492b558bSThomas Graf [XFRM_MSG_GETSA - XFRM_MSG_BASE] = { .doit = xfrm_get_sa, 1656492b558bSThomas Graf .dump = xfrm_dump_sa }, 1657492b558bSThomas Graf [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy }, 1658492b558bSThomas Graf [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy }, 1659492b558bSThomas Graf [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy, 1660492b558bSThomas Graf .dump = xfrm_dump_policy }, 1661492b558bSThomas Graf [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = { .doit = xfrm_alloc_userspi }, 1662980ebd25SJamal Hadi Salim [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_acquire }, 166353bc6b4dSJamal Hadi Salim [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_sa_expire }, 1664492b558bSThomas Graf [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy }, 1665492b558bSThomas Graf [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa }, 16666c5c8ca7SJamal Hadi Salim [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_pol_expire}, 1667492b558bSThomas Graf [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = { .doit = xfrm_flush_sa }, 1668492b558bSThomas Graf [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_flush_policy }, 1669d51d081dSJamal Hadi Salim [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = { .doit = xfrm_new_ae }, 1670d51d081dSJamal Hadi Salim [XFRM_MSG_GETAE - XFRM_MSG_BASE] = { .doit = xfrm_get_ae }, 16711da177e4SLinus Torvalds }; 16721da177e4SLinus Torvalds 16731da177e4SLinus Torvalds static int xfrm_user_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh, int *errp) 16741da177e4SLinus Torvalds { 16751da177e4SLinus Torvalds struct rtattr *xfrma[XFRMA_MAX]; 16761da177e4SLinus Torvalds struct xfrm_link *link; 16771da177e4SLinus Torvalds int type, min_len; 16781da177e4SLinus Torvalds 16791da177e4SLinus Torvalds if (!(nlh->nlmsg_flags & NLM_F_REQUEST)) 16801da177e4SLinus Torvalds return 0; 16811da177e4SLinus Torvalds 16821da177e4SLinus Torvalds type = nlh->nlmsg_type; 16831da177e4SLinus Torvalds 16841da177e4SLinus Torvalds /* A control message: ignore them */ 16851da177e4SLinus Torvalds if (type < XFRM_MSG_BASE) 16861da177e4SLinus Torvalds return 0; 16871da177e4SLinus Torvalds 16881da177e4SLinus Torvalds /* Unknown message: reply with EINVAL */ 16891da177e4SLinus Torvalds if (type > XFRM_MSG_MAX) 16901da177e4SLinus Torvalds goto err_einval; 16911da177e4SLinus Torvalds 16921da177e4SLinus Torvalds type -= XFRM_MSG_BASE; 16931da177e4SLinus Torvalds link = &xfrm_dispatch[type]; 16941da177e4SLinus Torvalds 16951da177e4SLinus Torvalds /* All operations require privileges, even GET */ 1696c7bdb545SDarrel Goeddel if (security_netlink_recv(skb, CAP_NET_ADMIN)) { 16971da177e4SLinus Torvalds *errp = -EPERM; 16981da177e4SLinus Torvalds return -1; 16991da177e4SLinus Torvalds } 17001da177e4SLinus Torvalds 1701492b558bSThomas Graf if ((type == (XFRM_MSG_GETSA - XFRM_MSG_BASE) || 1702492b558bSThomas Graf type == (XFRM_MSG_GETPOLICY - XFRM_MSG_BASE)) && 1703492b558bSThomas Graf (nlh->nlmsg_flags & NLM_F_DUMP)) { 17041da177e4SLinus Torvalds if (link->dump == NULL) 17051da177e4SLinus Torvalds goto err_einval; 17061da177e4SLinus Torvalds 17071da177e4SLinus Torvalds if ((*errp = netlink_dump_start(xfrm_nl, skb, nlh, 1708a8f74b22SThomas Graf link->dump, NULL)) != 0) { 17091da177e4SLinus Torvalds return -1; 17101da177e4SLinus Torvalds } 171188fc2c84SThomas Graf 171288fc2c84SThomas Graf netlink_queue_skip(nlh, skb); 17131da177e4SLinus Torvalds return -1; 17141da177e4SLinus Torvalds } 17151da177e4SLinus Torvalds 17161da177e4SLinus Torvalds memset(xfrma, 0, sizeof(xfrma)); 17171da177e4SLinus Torvalds 17181da177e4SLinus Torvalds if (nlh->nlmsg_len < (min_len = xfrm_msg_min[type])) 17191da177e4SLinus Torvalds goto err_einval; 17201da177e4SLinus Torvalds 17211da177e4SLinus Torvalds if (nlh->nlmsg_len > min_len) { 17221da177e4SLinus Torvalds int attrlen = nlh->nlmsg_len - NLMSG_ALIGN(min_len); 17231da177e4SLinus Torvalds struct rtattr *attr = (void *) nlh + NLMSG_ALIGN(min_len); 17241da177e4SLinus Torvalds 17251da177e4SLinus Torvalds while (RTA_OK(attr, attrlen)) { 17261da177e4SLinus Torvalds unsigned short flavor = attr->rta_type; 17271da177e4SLinus Torvalds if (flavor) { 17281da177e4SLinus Torvalds if (flavor > XFRMA_MAX) 17291da177e4SLinus Torvalds goto err_einval; 17301da177e4SLinus Torvalds xfrma[flavor - 1] = attr; 17311da177e4SLinus Torvalds } 17321da177e4SLinus Torvalds attr = RTA_NEXT(attr, attrlen); 17331da177e4SLinus Torvalds } 17341da177e4SLinus Torvalds } 17351da177e4SLinus Torvalds 17361da177e4SLinus Torvalds if (link->doit == NULL) 17371da177e4SLinus Torvalds goto err_einval; 17381da177e4SLinus Torvalds *errp = link->doit(skb, nlh, (void **) &xfrma); 17391da177e4SLinus Torvalds 17401da177e4SLinus Torvalds return *errp; 17411da177e4SLinus Torvalds 17421da177e4SLinus Torvalds err_einval: 17431da177e4SLinus Torvalds *errp = -EINVAL; 17441da177e4SLinus Torvalds return -1; 17451da177e4SLinus Torvalds } 17461da177e4SLinus Torvalds 17471da177e4SLinus Torvalds static void xfrm_netlink_rcv(struct sock *sk, int len) 17481da177e4SLinus Torvalds { 174988fc2c84SThomas Graf unsigned int qlen = 0; 17502a0a6ebeSHerbert Xu 17511da177e4SLinus Torvalds do { 17524a3e2f71SArjan van de Ven mutex_lock(&xfrm_cfg_mutex); 175388fc2c84SThomas Graf netlink_run_queue(sk, &qlen, &xfrm_user_rcv_msg); 17544a3e2f71SArjan van de Ven mutex_unlock(&xfrm_cfg_mutex); 17551da177e4SLinus Torvalds 17562a0a6ebeSHerbert Xu } while (qlen); 17571da177e4SLinus Torvalds } 17581da177e4SLinus Torvalds 1759d51d081dSJamal Hadi Salim static int build_expire(struct sk_buff *skb, struct xfrm_state *x, struct km_event *c) 17601da177e4SLinus Torvalds { 17611da177e4SLinus Torvalds struct xfrm_user_expire *ue; 17621da177e4SLinus Torvalds struct nlmsghdr *nlh; 17631da177e4SLinus Torvalds unsigned char *b = skb->tail; 17641da177e4SLinus Torvalds 1765d51d081dSJamal Hadi Salim nlh = NLMSG_PUT(skb, c->pid, 0, XFRM_MSG_EXPIRE, 17661da177e4SLinus Torvalds sizeof(*ue)); 17671da177e4SLinus Torvalds ue = NLMSG_DATA(nlh); 17681da177e4SLinus Torvalds nlh->nlmsg_flags = 0; 17691da177e4SLinus Torvalds 17701da177e4SLinus Torvalds copy_to_user_state(x, &ue->state); 1771d51d081dSJamal Hadi Salim ue->hard = (c->data.hard != 0) ? 1 : 0; 17721da177e4SLinus Torvalds 17731da177e4SLinus Torvalds nlh->nlmsg_len = skb->tail - b; 17741da177e4SLinus Torvalds return skb->len; 17751da177e4SLinus Torvalds 17761da177e4SLinus Torvalds nlmsg_failure: 17771da177e4SLinus Torvalds skb_trim(skb, b - skb->data); 17781da177e4SLinus Torvalds return -1; 17791da177e4SLinus Torvalds } 17801da177e4SLinus Torvalds 178126b15dadSJamal Hadi Salim static int xfrm_exp_state_notify(struct xfrm_state *x, struct km_event *c) 17821da177e4SLinus Torvalds { 17831da177e4SLinus Torvalds struct sk_buff *skb; 1784ee57eef9SJamal Hadi Salim int len = NLMSG_LENGTH(sizeof(struct xfrm_user_expire)); 17851da177e4SLinus Torvalds 1786ee57eef9SJamal Hadi Salim skb = alloc_skb(len, GFP_ATOMIC); 17871da177e4SLinus Torvalds if (skb == NULL) 17881da177e4SLinus Torvalds return -ENOMEM; 17891da177e4SLinus Torvalds 1790d51d081dSJamal Hadi Salim if (build_expire(skb, x, c) < 0) 17911da177e4SLinus Torvalds BUG(); 17921da177e4SLinus Torvalds 1793ac6d439dSPatrick McHardy NETLINK_CB(skb).dst_group = XFRMNLGRP_EXPIRE; 1794ac6d439dSPatrick McHardy return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_EXPIRE, GFP_ATOMIC); 17951da177e4SLinus Torvalds } 17961da177e4SLinus Torvalds 1797d51d081dSJamal Hadi Salim static int xfrm_aevent_state_notify(struct xfrm_state *x, struct km_event *c) 1798d51d081dSJamal Hadi Salim { 1799d51d081dSJamal Hadi Salim struct sk_buff *skb; 1800d51d081dSJamal Hadi Salim int len = NLMSG_LENGTH(sizeof(struct xfrm_aevent_id)); 1801d51d081dSJamal Hadi Salim 1802d51d081dSJamal Hadi Salim len += RTA_SPACE(sizeof(struct xfrm_replay_state)); 1803d51d081dSJamal Hadi Salim len += RTA_SPACE(sizeof(struct xfrm_lifetime_cur)); 1804d51d081dSJamal Hadi Salim skb = alloc_skb(len, GFP_ATOMIC); 1805d51d081dSJamal Hadi Salim if (skb == NULL) 1806d51d081dSJamal Hadi Salim return -ENOMEM; 1807d51d081dSJamal Hadi Salim 1808d51d081dSJamal Hadi Salim if (build_aevent(skb, x, c) < 0) 1809d51d081dSJamal Hadi Salim BUG(); 1810d51d081dSJamal Hadi Salim 1811d51d081dSJamal Hadi Salim NETLINK_CB(skb).dst_group = XFRMNLGRP_AEVENTS; 1812d51d081dSJamal Hadi Salim return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_AEVENTS, GFP_ATOMIC); 1813d51d081dSJamal Hadi Salim } 1814d51d081dSJamal Hadi Salim 181526b15dadSJamal Hadi Salim static int xfrm_notify_sa_flush(struct km_event *c) 181626b15dadSJamal Hadi Salim { 181726b15dadSJamal Hadi Salim struct xfrm_usersa_flush *p; 181826b15dadSJamal Hadi Salim struct nlmsghdr *nlh; 181926b15dadSJamal Hadi Salim struct sk_buff *skb; 182026b15dadSJamal Hadi Salim unsigned char *b; 182126b15dadSJamal Hadi Salim int len = NLMSG_LENGTH(sizeof(struct xfrm_usersa_flush)); 182226b15dadSJamal Hadi Salim 182326b15dadSJamal Hadi Salim skb = alloc_skb(len, GFP_ATOMIC); 182426b15dadSJamal Hadi Salim if (skb == NULL) 182526b15dadSJamal Hadi Salim return -ENOMEM; 182626b15dadSJamal Hadi Salim b = skb->tail; 182726b15dadSJamal Hadi Salim 182826b15dadSJamal Hadi Salim nlh = NLMSG_PUT(skb, c->pid, c->seq, 182926b15dadSJamal Hadi Salim XFRM_MSG_FLUSHSA, sizeof(*p)); 183026b15dadSJamal Hadi Salim nlh->nlmsg_flags = 0; 183126b15dadSJamal Hadi Salim 183226b15dadSJamal Hadi Salim p = NLMSG_DATA(nlh); 1833bf08867fSHerbert Xu p->proto = c->data.proto; 183426b15dadSJamal Hadi Salim 183526b15dadSJamal Hadi Salim nlh->nlmsg_len = skb->tail - b; 183626b15dadSJamal Hadi Salim 1837ac6d439dSPatrick McHardy NETLINK_CB(skb).dst_group = XFRMNLGRP_SA; 1838ac6d439dSPatrick McHardy return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_SA, GFP_ATOMIC); 183926b15dadSJamal Hadi Salim 184026b15dadSJamal Hadi Salim nlmsg_failure: 184126b15dadSJamal Hadi Salim kfree_skb(skb); 184226b15dadSJamal Hadi Salim return -1; 184326b15dadSJamal Hadi Salim } 184426b15dadSJamal Hadi Salim 184526b15dadSJamal Hadi Salim static int inline xfrm_sa_len(struct xfrm_state *x) 184626b15dadSJamal Hadi Salim { 18470603eac0SHerbert Xu int l = 0; 184826b15dadSJamal Hadi Salim if (x->aalg) 184926b15dadSJamal Hadi Salim l += RTA_SPACE(sizeof(*x->aalg) + (x->aalg->alg_key_len+7)/8); 185026b15dadSJamal Hadi Salim if (x->ealg) 185126b15dadSJamal Hadi Salim l += RTA_SPACE(sizeof(*x->ealg) + (x->ealg->alg_key_len+7)/8); 185226b15dadSJamal Hadi Salim if (x->calg) 185326b15dadSJamal Hadi Salim l += RTA_SPACE(sizeof(*x->calg)); 185426b15dadSJamal Hadi Salim if (x->encap) 185526b15dadSJamal Hadi Salim l += RTA_SPACE(sizeof(*x->encap)); 185626b15dadSJamal Hadi Salim 185726b15dadSJamal Hadi Salim return l; 185826b15dadSJamal Hadi Salim } 185926b15dadSJamal Hadi Salim 186026b15dadSJamal Hadi Salim static int xfrm_notify_sa(struct xfrm_state *x, struct km_event *c) 186126b15dadSJamal Hadi Salim { 186226b15dadSJamal Hadi Salim struct xfrm_usersa_info *p; 18630603eac0SHerbert Xu struct xfrm_usersa_id *id; 186426b15dadSJamal Hadi Salim struct nlmsghdr *nlh; 186526b15dadSJamal Hadi Salim struct sk_buff *skb; 186626b15dadSJamal Hadi Salim unsigned char *b; 186726b15dadSJamal Hadi Salim int len = xfrm_sa_len(x); 18680603eac0SHerbert Xu int headlen; 18690603eac0SHerbert Xu 18700603eac0SHerbert Xu headlen = sizeof(*p); 18710603eac0SHerbert Xu if (c->event == XFRM_MSG_DELSA) { 18720603eac0SHerbert Xu len += RTA_SPACE(headlen); 18730603eac0SHerbert Xu headlen = sizeof(*id); 18740603eac0SHerbert Xu } 18750603eac0SHerbert Xu len += NLMSG_SPACE(headlen); 187626b15dadSJamal Hadi Salim 187726b15dadSJamal Hadi Salim skb = alloc_skb(len, GFP_ATOMIC); 187826b15dadSJamal Hadi Salim if (skb == NULL) 187926b15dadSJamal Hadi Salim return -ENOMEM; 188026b15dadSJamal Hadi Salim b = skb->tail; 188126b15dadSJamal Hadi Salim 18820603eac0SHerbert Xu nlh = NLMSG_PUT(skb, c->pid, c->seq, c->event, headlen); 188326b15dadSJamal Hadi Salim nlh->nlmsg_flags = 0; 188426b15dadSJamal Hadi Salim 188526b15dadSJamal Hadi Salim p = NLMSG_DATA(nlh); 18860603eac0SHerbert Xu if (c->event == XFRM_MSG_DELSA) { 18870603eac0SHerbert Xu id = NLMSG_DATA(nlh); 18880603eac0SHerbert Xu memcpy(&id->daddr, &x->id.daddr, sizeof(id->daddr)); 18890603eac0SHerbert Xu id->spi = x->id.spi; 18900603eac0SHerbert Xu id->family = x->props.family; 18910603eac0SHerbert Xu id->proto = x->id.proto; 18920603eac0SHerbert Xu 18930603eac0SHerbert Xu p = RTA_DATA(__RTA_PUT(skb, XFRMA_SA, sizeof(*p))); 18940603eac0SHerbert Xu } 18950603eac0SHerbert Xu 189626b15dadSJamal Hadi Salim copy_to_user_state(x, p); 189726b15dadSJamal Hadi Salim 189826b15dadSJamal Hadi Salim if (x->aalg) 189926b15dadSJamal Hadi Salim RTA_PUT(skb, XFRMA_ALG_AUTH, 190026b15dadSJamal Hadi Salim sizeof(*(x->aalg))+(x->aalg->alg_key_len+7)/8, x->aalg); 190126b15dadSJamal Hadi Salim if (x->ealg) 190226b15dadSJamal Hadi Salim RTA_PUT(skb, XFRMA_ALG_CRYPT, 190326b15dadSJamal Hadi Salim sizeof(*(x->ealg))+(x->ealg->alg_key_len+7)/8, x->ealg); 190426b15dadSJamal Hadi Salim if (x->calg) 190526b15dadSJamal Hadi Salim RTA_PUT(skb, XFRMA_ALG_COMP, sizeof(*(x->calg)), x->calg); 190626b15dadSJamal Hadi Salim 190726b15dadSJamal Hadi Salim if (x->encap) 190826b15dadSJamal Hadi Salim RTA_PUT(skb, XFRMA_ENCAP, sizeof(*x->encap), x->encap); 190926b15dadSJamal Hadi Salim 191026b15dadSJamal Hadi Salim nlh->nlmsg_len = skb->tail - b; 191126b15dadSJamal Hadi Salim 1912ac6d439dSPatrick McHardy NETLINK_CB(skb).dst_group = XFRMNLGRP_SA; 1913ac6d439dSPatrick McHardy return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_SA, GFP_ATOMIC); 191426b15dadSJamal Hadi Salim 191526b15dadSJamal Hadi Salim nlmsg_failure: 191626b15dadSJamal Hadi Salim rtattr_failure: 191726b15dadSJamal Hadi Salim kfree_skb(skb); 191826b15dadSJamal Hadi Salim return -1; 191926b15dadSJamal Hadi Salim } 192026b15dadSJamal Hadi Salim 192126b15dadSJamal Hadi Salim static int xfrm_send_state_notify(struct xfrm_state *x, struct km_event *c) 192226b15dadSJamal Hadi Salim { 192326b15dadSJamal Hadi Salim 192426b15dadSJamal Hadi Salim switch (c->event) { 1925f60f6b8fSHerbert Xu case XFRM_MSG_EXPIRE: 192626b15dadSJamal Hadi Salim return xfrm_exp_state_notify(x, c); 1927d51d081dSJamal Hadi Salim case XFRM_MSG_NEWAE: 1928d51d081dSJamal Hadi Salim return xfrm_aevent_state_notify(x, c); 1929f60f6b8fSHerbert Xu case XFRM_MSG_DELSA: 1930f60f6b8fSHerbert Xu case XFRM_MSG_UPDSA: 1931f60f6b8fSHerbert Xu case XFRM_MSG_NEWSA: 193226b15dadSJamal Hadi Salim return xfrm_notify_sa(x, c); 1933f60f6b8fSHerbert Xu case XFRM_MSG_FLUSHSA: 193426b15dadSJamal Hadi Salim return xfrm_notify_sa_flush(c); 193526b15dadSJamal Hadi Salim default: 193626b15dadSJamal Hadi Salim printk("xfrm_user: Unknown SA event %d\n", c->event); 193726b15dadSJamal Hadi Salim break; 193826b15dadSJamal Hadi Salim } 193926b15dadSJamal Hadi Salim 194026b15dadSJamal Hadi Salim return 0; 194126b15dadSJamal Hadi Salim 194226b15dadSJamal Hadi Salim } 194326b15dadSJamal Hadi Salim 19441da177e4SLinus Torvalds static int build_acquire(struct sk_buff *skb, struct xfrm_state *x, 19451da177e4SLinus Torvalds struct xfrm_tmpl *xt, struct xfrm_policy *xp, 19461da177e4SLinus Torvalds int dir) 19471da177e4SLinus Torvalds { 19481da177e4SLinus Torvalds struct xfrm_user_acquire *ua; 19491da177e4SLinus Torvalds struct nlmsghdr *nlh; 19501da177e4SLinus Torvalds unsigned char *b = skb->tail; 19511da177e4SLinus Torvalds __u32 seq = xfrm_get_acqseq(); 19521da177e4SLinus Torvalds 19531da177e4SLinus Torvalds nlh = NLMSG_PUT(skb, 0, 0, XFRM_MSG_ACQUIRE, 19541da177e4SLinus Torvalds sizeof(*ua)); 19551da177e4SLinus Torvalds ua = NLMSG_DATA(nlh); 19561da177e4SLinus Torvalds nlh->nlmsg_flags = 0; 19571da177e4SLinus Torvalds 19581da177e4SLinus Torvalds memcpy(&ua->id, &x->id, sizeof(ua->id)); 19591da177e4SLinus Torvalds memcpy(&ua->saddr, &x->props.saddr, sizeof(ua->saddr)); 19601da177e4SLinus Torvalds memcpy(&ua->sel, &x->sel, sizeof(ua->sel)); 19611da177e4SLinus Torvalds copy_to_user_policy(xp, &ua->policy, dir); 19621da177e4SLinus Torvalds ua->aalgos = xt->aalgos; 19631da177e4SLinus Torvalds ua->ealgos = xt->ealgos; 19641da177e4SLinus Torvalds ua->calgos = xt->calgos; 19651da177e4SLinus Torvalds ua->seq = x->km.seq = seq; 19661da177e4SLinus Torvalds 19671da177e4SLinus Torvalds if (copy_to_user_tmpl(xp, skb) < 0) 19681da177e4SLinus Torvalds goto nlmsg_failure; 19690d681623SSerge Hallyn if (copy_to_user_state_sec_ctx(x, skb)) 1970df71837dSTrent Jaeger goto nlmsg_failure; 19711459bb36SJamal Hadi Salim if (copy_to_user_policy_type(xp->type, skb) < 0) 1972f7b6983fSMasahide NAKAMURA goto nlmsg_failure; 19731da177e4SLinus Torvalds 19741da177e4SLinus Torvalds nlh->nlmsg_len = skb->tail - b; 19751da177e4SLinus Torvalds return skb->len; 19761da177e4SLinus Torvalds 19771da177e4SLinus Torvalds nlmsg_failure: 19781da177e4SLinus Torvalds skb_trim(skb, b - skb->data); 19791da177e4SLinus Torvalds return -1; 19801da177e4SLinus Torvalds } 19811da177e4SLinus Torvalds 19821da177e4SLinus Torvalds static int xfrm_send_acquire(struct xfrm_state *x, struct xfrm_tmpl *xt, 19831da177e4SLinus Torvalds struct xfrm_policy *xp, int dir) 19841da177e4SLinus Torvalds { 19851da177e4SLinus Torvalds struct sk_buff *skb; 19861da177e4SLinus Torvalds size_t len; 19871da177e4SLinus Torvalds 19881da177e4SLinus Torvalds len = RTA_SPACE(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr); 19891da177e4SLinus Torvalds len += NLMSG_SPACE(sizeof(struct xfrm_user_acquire)); 1990df71837dSTrent Jaeger len += RTA_SPACE(xfrm_user_sec_ctx_size(xp)); 1991785fd8b8SJamal Hadi Salim #ifdef CONFIG_XFRM_SUB_POLICY 1992785fd8b8SJamal Hadi Salim len += RTA_SPACE(sizeof(struct xfrm_userpolicy_type)); 1993785fd8b8SJamal Hadi Salim #endif 19941da177e4SLinus Torvalds skb = alloc_skb(len, GFP_ATOMIC); 19951da177e4SLinus Torvalds if (skb == NULL) 19961da177e4SLinus Torvalds return -ENOMEM; 19971da177e4SLinus Torvalds 19981da177e4SLinus Torvalds if (build_acquire(skb, x, xt, xp, dir) < 0) 19991da177e4SLinus Torvalds BUG(); 20001da177e4SLinus Torvalds 2001ac6d439dSPatrick McHardy NETLINK_CB(skb).dst_group = XFRMNLGRP_ACQUIRE; 2002ac6d439dSPatrick McHardy return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_ACQUIRE, GFP_ATOMIC); 20031da177e4SLinus Torvalds } 20041da177e4SLinus Torvalds 20051da177e4SLinus Torvalds /* User gives us xfrm_user_policy_info followed by an array of 0 20061da177e4SLinus Torvalds * or more templates. 20071da177e4SLinus Torvalds */ 2008cb969f07SVenkat Yekkirala static struct xfrm_policy *xfrm_compile_policy(struct sock *sk, int opt, 20091da177e4SLinus Torvalds u8 *data, int len, int *dir) 20101da177e4SLinus Torvalds { 20111da177e4SLinus Torvalds struct xfrm_userpolicy_info *p = (struct xfrm_userpolicy_info *)data; 20121da177e4SLinus Torvalds struct xfrm_user_tmpl *ut = (struct xfrm_user_tmpl *) (p + 1); 20131da177e4SLinus Torvalds struct xfrm_policy *xp; 20141da177e4SLinus Torvalds int nr; 20151da177e4SLinus Torvalds 2016cb969f07SVenkat Yekkirala switch (sk->sk_family) { 20171da177e4SLinus Torvalds case AF_INET: 20181da177e4SLinus Torvalds if (opt != IP_XFRM_POLICY) { 20191da177e4SLinus Torvalds *dir = -EOPNOTSUPP; 20201da177e4SLinus Torvalds return NULL; 20211da177e4SLinus Torvalds } 20221da177e4SLinus Torvalds break; 20231da177e4SLinus Torvalds #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) 20241da177e4SLinus Torvalds case AF_INET6: 20251da177e4SLinus Torvalds if (opt != IPV6_XFRM_POLICY) { 20261da177e4SLinus Torvalds *dir = -EOPNOTSUPP; 20271da177e4SLinus Torvalds return NULL; 20281da177e4SLinus Torvalds } 20291da177e4SLinus Torvalds break; 20301da177e4SLinus Torvalds #endif 20311da177e4SLinus Torvalds default: 20321da177e4SLinus Torvalds *dir = -EINVAL; 20331da177e4SLinus Torvalds return NULL; 20341da177e4SLinus Torvalds } 20351da177e4SLinus Torvalds 20361da177e4SLinus Torvalds *dir = -EINVAL; 20371da177e4SLinus Torvalds 20381da177e4SLinus Torvalds if (len < sizeof(*p) || 20391da177e4SLinus Torvalds verify_newpolicy_info(p)) 20401da177e4SLinus Torvalds return NULL; 20411da177e4SLinus Torvalds 20421da177e4SLinus Torvalds nr = ((len - sizeof(*p)) / sizeof(*ut)); 2043b4ad86bfSDavid S. Miller if (validate_tmpl(nr, ut, p->sel.family)) 20441da177e4SLinus Torvalds return NULL; 20451da177e4SLinus Torvalds 2046a4f1bac6SHerbert Xu if (p->dir > XFRM_POLICY_OUT) 2047a4f1bac6SHerbert Xu return NULL; 2048a4f1bac6SHerbert Xu 20491da177e4SLinus Torvalds xp = xfrm_policy_alloc(GFP_KERNEL); 20501da177e4SLinus Torvalds if (xp == NULL) { 20511da177e4SLinus Torvalds *dir = -ENOBUFS; 20521da177e4SLinus Torvalds return NULL; 20531da177e4SLinus Torvalds } 20541da177e4SLinus Torvalds 20551da177e4SLinus Torvalds copy_from_user_policy(xp, p); 2056f7b6983fSMasahide NAKAMURA xp->type = XFRM_POLICY_TYPE_MAIN; 20571da177e4SLinus Torvalds copy_templates(xp, ut, nr); 20581da177e4SLinus Torvalds 20591da177e4SLinus Torvalds *dir = p->dir; 20601da177e4SLinus Torvalds 20611da177e4SLinus Torvalds return xp; 20621da177e4SLinus Torvalds } 20631da177e4SLinus Torvalds 20641da177e4SLinus Torvalds static int build_polexpire(struct sk_buff *skb, struct xfrm_policy *xp, 2065d51d081dSJamal Hadi Salim int dir, struct km_event *c) 20661da177e4SLinus Torvalds { 20671da177e4SLinus Torvalds struct xfrm_user_polexpire *upe; 20681da177e4SLinus Torvalds struct nlmsghdr *nlh; 2069d51d081dSJamal Hadi Salim int hard = c->data.hard; 20701da177e4SLinus Torvalds unsigned char *b = skb->tail; 20711da177e4SLinus Torvalds 2072d51d081dSJamal Hadi Salim nlh = NLMSG_PUT(skb, c->pid, 0, XFRM_MSG_POLEXPIRE, sizeof(*upe)); 20731da177e4SLinus Torvalds upe = NLMSG_DATA(nlh); 20741da177e4SLinus Torvalds nlh->nlmsg_flags = 0; 20751da177e4SLinus Torvalds 20761da177e4SLinus Torvalds copy_to_user_policy(xp, &upe->pol, dir); 20771da177e4SLinus Torvalds if (copy_to_user_tmpl(xp, skb) < 0) 20781da177e4SLinus Torvalds goto nlmsg_failure; 2079df71837dSTrent Jaeger if (copy_to_user_sec_ctx(xp, skb)) 2080df71837dSTrent Jaeger goto nlmsg_failure; 20811459bb36SJamal Hadi Salim if (copy_to_user_policy_type(xp->type, skb) < 0) 2082f7b6983fSMasahide NAKAMURA goto nlmsg_failure; 20831da177e4SLinus Torvalds upe->hard = !!hard; 20841da177e4SLinus Torvalds 20851da177e4SLinus Torvalds nlh->nlmsg_len = skb->tail - b; 20861da177e4SLinus Torvalds return skb->len; 20871da177e4SLinus Torvalds 20881da177e4SLinus Torvalds nlmsg_failure: 20891da177e4SLinus Torvalds skb_trim(skb, b - skb->data); 20901da177e4SLinus Torvalds return -1; 20911da177e4SLinus Torvalds } 20921da177e4SLinus Torvalds 209326b15dadSJamal Hadi Salim static int xfrm_exp_policy_notify(struct xfrm_policy *xp, int dir, struct km_event *c) 20941da177e4SLinus Torvalds { 20951da177e4SLinus Torvalds struct sk_buff *skb; 20961da177e4SLinus Torvalds size_t len; 20971da177e4SLinus Torvalds 20981da177e4SLinus Torvalds len = RTA_SPACE(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr); 20991da177e4SLinus Torvalds len += NLMSG_SPACE(sizeof(struct xfrm_user_polexpire)); 2100df71837dSTrent Jaeger len += RTA_SPACE(xfrm_user_sec_ctx_size(xp)); 2101785fd8b8SJamal Hadi Salim #ifdef CONFIG_XFRM_SUB_POLICY 2102785fd8b8SJamal Hadi Salim len += RTA_SPACE(sizeof(struct xfrm_userpolicy_type)); 2103785fd8b8SJamal Hadi Salim #endif 21041da177e4SLinus Torvalds skb = alloc_skb(len, GFP_ATOMIC); 21051da177e4SLinus Torvalds if (skb == NULL) 21061da177e4SLinus Torvalds return -ENOMEM; 21071da177e4SLinus Torvalds 2108d51d081dSJamal Hadi Salim if (build_polexpire(skb, xp, dir, c) < 0) 21091da177e4SLinus Torvalds BUG(); 21101da177e4SLinus Torvalds 2111ac6d439dSPatrick McHardy NETLINK_CB(skb).dst_group = XFRMNLGRP_EXPIRE; 2112ac6d439dSPatrick McHardy return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_EXPIRE, GFP_ATOMIC); 21131da177e4SLinus Torvalds } 21141da177e4SLinus Torvalds 211526b15dadSJamal Hadi Salim static int xfrm_notify_policy(struct xfrm_policy *xp, int dir, struct km_event *c) 211626b15dadSJamal Hadi Salim { 211726b15dadSJamal Hadi Salim struct xfrm_userpolicy_info *p; 21180603eac0SHerbert Xu struct xfrm_userpolicy_id *id; 211926b15dadSJamal Hadi Salim struct nlmsghdr *nlh; 212026b15dadSJamal Hadi Salim struct sk_buff *skb; 212126b15dadSJamal Hadi Salim unsigned char *b; 212226b15dadSJamal Hadi Salim int len = RTA_SPACE(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr); 21230603eac0SHerbert Xu int headlen; 21240603eac0SHerbert Xu 21250603eac0SHerbert Xu headlen = sizeof(*p); 21260603eac0SHerbert Xu if (c->event == XFRM_MSG_DELPOLICY) { 21270603eac0SHerbert Xu len += RTA_SPACE(headlen); 21280603eac0SHerbert Xu headlen = sizeof(*id); 21290603eac0SHerbert Xu } 2130334f3d45SJamal Hadi Salim #ifdef CONFIG_XFRM_SUB_POLICY 2131334f3d45SJamal Hadi Salim len += RTA_SPACE(sizeof(struct xfrm_userpolicy_type)); 2132334f3d45SJamal Hadi Salim #endif 21330603eac0SHerbert Xu len += NLMSG_SPACE(headlen); 213426b15dadSJamal Hadi Salim 213526b15dadSJamal Hadi Salim skb = alloc_skb(len, GFP_ATOMIC); 213626b15dadSJamal Hadi Salim if (skb == NULL) 213726b15dadSJamal Hadi Salim return -ENOMEM; 213826b15dadSJamal Hadi Salim b = skb->tail; 213926b15dadSJamal Hadi Salim 21400603eac0SHerbert Xu nlh = NLMSG_PUT(skb, c->pid, c->seq, c->event, headlen); 214126b15dadSJamal Hadi Salim 214226b15dadSJamal Hadi Salim p = NLMSG_DATA(nlh); 21430603eac0SHerbert Xu if (c->event == XFRM_MSG_DELPOLICY) { 21440603eac0SHerbert Xu id = NLMSG_DATA(nlh); 21450603eac0SHerbert Xu memset(id, 0, sizeof(*id)); 21460603eac0SHerbert Xu id->dir = dir; 21470603eac0SHerbert Xu if (c->data.byid) 21480603eac0SHerbert Xu id->index = xp->index; 21490603eac0SHerbert Xu else 21500603eac0SHerbert Xu memcpy(&id->sel, &xp->selector, sizeof(id->sel)); 21510603eac0SHerbert Xu 21520603eac0SHerbert Xu p = RTA_DATA(__RTA_PUT(skb, XFRMA_POLICY, sizeof(*p))); 21530603eac0SHerbert Xu } 215426b15dadSJamal Hadi Salim 215526b15dadSJamal Hadi Salim nlh->nlmsg_flags = 0; 215626b15dadSJamal Hadi Salim 215726b15dadSJamal Hadi Salim copy_to_user_policy(xp, p, dir); 215826b15dadSJamal Hadi Salim if (copy_to_user_tmpl(xp, skb) < 0) 215926b15dadSJamal Hadi Salim goto nlmsg_failure; 21601459bb36SJamal Hadi Salim if (copy_to_user_policy_type(xp->type, skb) < 0) 2161f7b6983fSMasahide NAKAMURA goto nlmsg_failure; 216226b15dadSJamal Hadi Salim 216326b15dadSJamal Hadi Salim nlh->nlmsg_len = skb->tail - b; 216426b15dadSJamal Hadi Salim 2165ac6d439dSPatrick McHardy NETLINK_CB(skb).dst_group = XFRMNLGRP_POLICY; 2166ac6d439dSPatrick McHardy return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_POLICY, GFP_ATOMIC); 216726b15dadSJamal Hadi Salim 216826b15dadSJamal Hadi Salim nlmsg_failure: 21690603eac0SHerbert Xu rtattr_failure: 217026b15dadSJamal Hadi Salim kfree_skb(skb); 217126b15dadSJamal Hadi Salim return -1; 217226b15dadSJamal Hadi Salim } 217326b15dadSJamal Hadi Salim 217426b15dadSJamal Hadi Salim static int xfrm_notify_policy_flush(struct km_event *c) 217526b15dadSJamal Hadi Salim { 217626b15dadSJamal Hadi Salim struct nlmsghdr *nlh; 217726b15dadSJamal Hadi Salim struct sk_buff *skb; 217826b15dadSJamal Hadi Salim unsigned char *b; 2179785fd8b8SJamal Hadi Salim int len = 0; 2180f7b6983fSMasahide NAKAMURA #ifdef CONFIG_XFRM_SUB_POLICY 2181785fd8b8SJamal Hadi Salim len += RTA_SPACE(sizeof(struct xfrm_userpolicy_type)); 2182f7b6983fSMasahide NAKAMURA #endif 2183785fd8b8SJamal Hadi Salim len += NLMSG_LENGTH(0); 218426b15dadSJamal Hadi Salim 218526b15dadSJamal Hadi Salim skb = alloc_skb(len, GFP_ATOMIC); 218626b15dadSJamal Hadi Salim if (skb == NULL) 218726b15dadSJamal Hadi Salim return -ENOMEM; 218826b15dadSJamal Hadi Salim b = skb->tail; 218926b15dadSJamal Hadi Salim 219026b15dadSJamal Hadi Salim 219126b15dadSJamal Hadi Salim nlh = NLMSG_PUT(skb, c->pid, c->seq, XFRM_MSG_FLUSHPOLICY, 0); 2192f7b6983fSMasahide NAKAMURA nlh->nlmsg_flags = 0; 21930c51f53cSJamal Hadi Salim if (copy_to_user_policy_type(c->data.type, skb) < 0) 21940c51f53cSJamal Hadi Salim goto nlmsg_failure; 219526b15dadSJamal Hadi Salim 219626b15dadSJamal Hadi Salim nlh->nlmsg_len = skb->tail - b; 219726b15dadSJamal Hadi Salim 2198ac6d439dSPatrick McHardy NETLINK_CB(skb).dst_group = XFRMNLGRP_POLICY; 2199ac6d439dSPatrick McHardy return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_POLICY, GFP_ATOMIC); 220026b15dadSJamal Hadi Salim 220126b15dadSJamal Hadi Salim nlmsg_failure: 220226b15dadSJamal Hadi Salim kfree_skb(skb); 220326b15dadSJamal Hadi Salim return -1; 220426b15dadSJamal Hadi Salim } 220526b15dadSJamal Hadi Salim 220626b15dadSJamal Hadi Salim static int xfrm_send_policy_notify(struct xfrm_policy *xp, int dir, struct km_event *c) 220726b15dadSJamal Hadi Salim { 220826b15dadSJamal Hadi Salim 220926b15dadSJamal Hadi Salim switch (c->event) { 2210f60f6b8fSHerbert Xu case XFRM_MSG_NEWPOLICY: 2211f60f6b8fSHerbert Xu case XFRM_MSG_UPDPOLICY: 2212f60f6b8fSHerbert Xu case XFRM_MSG_DELPOLICY: 221326b15dadSJamal Hadi Salim return xfrm_notify_policy(xp, dir, c); 2214f60f6b8fSHerbert Xu case XFRM_MSG_FLUSHPOLICY: 221526b15dadSJamal Hadi Salim return xfrm_notify_policy_flush(c); 2216f60f6b8fSHerbert Xu case XFRM_MSG_POLEXPIRE: 221726b15dadSJamal Hadi Salim return xfrm_exp_policy_notify(xp, dir, c); 221826b15dadSJamal Hadi Salim default: 221926b15dadSJamal Hadi Salim printk("xfrm_user: Unknown Policy event %d\n", c->event); 222026b15dadSJamal Hadi Salim } 222126b15dadSJamal Hadi Salim 222226b15dadSJamal Hadi Salim return 0; 222326b15dadSJamal Hadi Salim 222426b15dadSJamal Hadi Salim } 222526b15dadSJamal Hadi Salim 222697a64b45SMasahide NAKAMURA static int build_report(struct sk_buff *skb, u8 proto, 222797a64b45SMasahide NAKAMURA struct xfrm_selector *sel, xfrm_address_t *addr) 222897a64b45SMasahide NAKAMURA { 222997a64b45SMasahide NAKAMURA struct xfrm_user_report *ur; 223097a64b45SMasahide NAKAMURA struct nlmsghdr *nlh; 223197a64b45SMasahide NAKAMURA unsigned char *b = skb->tail; 223297a64b45SMasahide NAKAMURA 223397a64b45SMasahide NAKAMURA nlh = NLMSG_PUT(skb, 0, 0, XFRM_MSG_REPORT, sizeof(*ur)); 223497a64b45SMasahide NAKAMURA ur = NLMSG_DATA(nlh); 223597a64b45SMasahide NAKAMURA nlh->nlmsg_flags = 0; 223697a64b45SMasahide NAKAMURA 223797a64b45SMasahide NAKAMURA ur->proto = proto; 223897a64b45SMasahide NAKAMURA memcpy(&ur->sel, sel, sizeof(ur->sel)); 223997a64b45SMasahide NAKAMURA 224097a64b45SMasahide NAKAMURA if (addr) 224197a64b45SMasahide NAKAMURA RTA_PUT(skb, XFRMA_COADDR, sizeof(*addr), addr); 224297a64b45SMasahide NAKAMURA 224397a64b45SMasahide NAKAMURA nlh->nlmsg_len = skb->tail - b; 224497a64b45SMasahide NAKAMURA return skb->len; 224597a64b45SMasahide NAKAMURA 224697a64b45SMasahide NAKAMURA nlmsg_failure: 224797a64b45SMasahide NAKAMURA rtattr_failure: 224897a64b45SMasahide NAKAMURA skb_trim(skb, b - skb->data); 224997a64b45SMasahide NAKAMURA return -1; 225097a64b45SMasahide NAKAMURA } 225197a64b45SMasahide NAKAMURA 225297a64b45SMasahide NAKAMURA static int xfrm_send_report(u8 proto, struct xfrm_selector *sel, 225397a64b45SMasahide NAKAMURA xfrm_address_t *addr) 225497a64b45SMasahide NAKAMURA { 225597a64b45SMasahide NAKAMURA struct sk_buff *skb; 225697a64b45SMasahide NAKAMURA size_t len; 225797a64b45SMasahide NAKAMURA 225897a64b45SMasahide NAKAMURA len = NLMSG_ALIGN(NLMSG_LENGTH(sizeof(struct xfrm_user_report))); 225997a64b45SMasahide NAKAMURA skb = alloc_skb(len, GFP_ATOMIC); 226097a64b45SMasahide NAKAMURA if (skb == NULL) 226197a64b45SMasahide NAKAMURA return -ENOMEM; 226297a64b45SMasahide NAKAMURA 226397a64b45SMasahide NAKAMURA if (build_report(skb, proto, sel, addr) < 0) 226497a64b45SMasahide NAKAMURA BUG(); 226597a64b45SMasahide NAKAMURA 226697a64b45SMasahide NAKAMURA NETLINK_CB(skb).dst_group = XFRMNLGRP_REPORT; 226797a64b45SMasahide NAKAMURA return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_REPORT, GFP_ATOMIC); 226897a64b45SMasahide NAKAMURA } 226997a64b45SMasahide NAKAMURA 22701da177e4SLinus Torvalds static struct xfrm_mgr netlink_mgr = { 22711da177e4SLinus Torvalds .id = "netlink", 22721da177e4SLinus Torvalds .notify = xfrm_send_state_notify, 22731da177e4SLinus Torvalds .acquire = xfrm_send_acquire, 22741da177e4SLinus Torvalds .compile_policy = xfrm_compile_policy, 22751da177e4SLinus Torvalds .notify_policy = xfrm_send_policy_notify, 227697a64b45SMasahide NAKAMURA .report = xfrm_send_report, 22771da177e4SLinus Torvalds }; 22781da177e4SLinus Torvalds 22791da177e4SLinus Torvalds static int __init xfrm_user_init(void) 22801da177e4SLinus Torvalds { 2281be33690dSPatrick McHardy struct sock *nlsk; 2282be33690dSPatrick McHardy 2283654b32c6SMasahide NAKAMURA printk(KERN_INFO "Initializing XFRM netlink socket\n"); 22841da177e4SLinus Torvalds 2285be33690dSPatrick McHardy nlsk = netlink_kernel_create(NETLINK_XFRM, XFRMNLGRP_MAX, 228606628607SPatrick McHardy xfrm_netlink_rcv, THIS_MODULE); 2287be33690dSPatrick McHardy if (nlsk == NULL) 22881da177e4SLinus Torvalds return -ENOMEM; 2289be33690dSPatrick McHardy rcu_assign_pointer(xfrm_nl, nlsk); 22901da177e4SLinus Torvalds 22911da177e4SLinus Torvalds xfrm_register_km(&netlink_mgr); 22921da177e4SLinus Torvalds 22931da177e4SLinus Torvalds return 0; 22941da177e4SLinus Torvalds } 22951da177e4SLinus Torvalds 22961da177e4SLinus Torvalds static void __exit xfrm_user_exit(void) 22971da177e4SLinus Torvalds { 2298be33690dSPatrick McHardy struct sock *nlsk = xfrm_nl; 2299be33690dSPatrick McHardy 23001da177e4SLinus Torvalds xfrm_unregister_km(&netlink_mgr); 2301be33690dSPatrick McHardy rcu_assign_pointer(xfrm_nl, NULL); 2302be33690dSPatrick McHardy synchronize_rcu(); 2303be33690dSPatrick McHardy sock_release(nlsk->sk_socket); 23041da177e4SLinus Torvalds } 23051da177e4SLinus Torvalds 23061da177e4SLinus Torvalds module_init(xfrm_user_init); 23071da177e4SLinus Torvalds module_exit(xfrm_user_exit); 23081da177e4SLinus Torvalds MODULE_LICENSE("GPL"); 23094fdb3bb7SHarald Welte MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK, NETLINK_XFRM); 2310f8cd5488SJamal Hadi Salim 2311