Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* xfrm_user.c: User interface to configure xfrm engine. |
| 2 | * |
| 3 | * Copyright (C) 2002 David S. Miller (davem@redhat.com) |
| 4 | * |
| 5 | * Changes: |
| 6 | * Mitsuru KANDA @USAGI |
| 7 | * Kazunori MIYAZAWA @USAGI |
| 8 | * Kunihiro Ishiguro <kunihiro@ipinfusion.com> |
| 9 | * IPv6 support |
| 10 | * |
| 11 | */ |
| 12 | |
| 13 | #include <linux/module.h> |
| 14 | #include <linux/kernel.h> |
| 15 | #include <linux/types.h> |
| 16 | #include <linux/slab.h> |
| 17 | #include <linux/socket.h> |
| 18 | #include <linux/string.h> |
| 19 | #include <linux/net.h> |
| 20 | #include <linux/skbuff.h> |
| 21 | #include <linux/netlink.h> |
| 22 | #include <linux/rtnetlink.h> |
| 23 | #include <linux/pfkeyv2.h> |
| 24 | #include <linux/ipsec.h> |
| 25 | #include <linux/init.h> |
| 26 | #include <linux/security.h> |
| 27 | #include <net/sock.h> |
| 28 | #include <net/xfrm.h> |
| 29 | #include <asm/uaccess.h> |
| 30 | |
| 31 | static struct sock *xfrm_nl; |
| 32 | |
| 33 | static int verify_one_alg(struct rtattr **xfrma, enum xfrm_attr_type_t type) |
| 34 | { |
| 35 | struct rtattr *rt = xfrma[type - 1]; |
| 36 | struct xfrm_algo *algp; |
Herbert Xu | 31c2685 | 2005-05-19 12:39:49 -0700 | [diff] [blame] | 37 | int len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | |
| 39 | if (!rt) |
| 40 | return 0; |
| 41 | |
Herbert Xu | 31c2685 | 2005-05-19 12:39:49 -0700 | [diff] [blame] | 42 | len = (rt->rta_len - sizeof(*rt)) - sizeof(*algp); |
| 43 | if (len < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | return -EINVAL; |
| 45 | |
| 46 | algp = RTA_DATA(rt); |
Herbert Xu | 31c2685 | 2005-05-19 12:39:49 -0700 | [diff] [blame] | 47 | |
| 48 | len -= (algp->alg_key_len + 7U) / 8; |
| 49 | if (len < 0) |
| 50 | return -EINVAL; |
| 51 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | switch (type) { |
| 53 | case XFRMA_ALG_AUTH: |
| 54 | if (!algp->alg_key_len && |
| 55 | strcmp(algp->alg_name, "digest_null") != 0) |
| 56 | return -EINVAL; |
| 57 | break; |
| 58 | |
| 59 | case XFRMA_ALG_CRYPT: |
| 60 | if (!algp->alg_key_len && |
| 61 | strcmp(algp->alg_name, "cipher_null") != 0) |
| 62 | return -EINVAL; |
| 63 | break; |
| 64 | |
| 65 | case XFRMA_ALG_COMP: |
| 66 | /* Zero length keys are legal. */ |
| 67 | break; |
| 68 | |
| 69 | default: |
| 70 | return -EINVAL; |
| 71 | }; |
| 72 | |
| 73 | algp->alg_name[CRYPTO_MAX_ALG_NAME - 1] = '\0'; |
| 74 | return 0; |
| 75 | } |
| 76 | |
| 77 | static int verify_encap_tmpl(struct rtattr **xfrma) |
| 78 | { |
| 79 | struct rtattr *rt = xfrma[XFRMA_ENCAP - 1]; |
| 80 | struct xfrm_encap_tmpl *encap; |
| 81 | |
| 82 | if (!rt) |
| 83 | return 0; |
| 84 | |
| 85 | if ((rt->rta_len - sizeof(*rt)) < sizeof(*encap)) |
| 86 | return -EINVAL; |
| 87 | |
| 88 | return 0; |
| 89 | } |
| 90 | |
| 91 | static int verify_newsa_info(struct xfrm_usersa_info *p, |
| 92 | struct rtattr **xfrma) |
| 93 | { |
| 94 | int err; |
| 95 | |
| 96 | err = -EINVAL; |
| 97 | switch (p->family) { |
| 98 | case AF_INET: |
| 99 | break; |
| 100 | |
| 101 | case AF_INET6: |
| 102 | #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) |
| 103 | break; |
| 104 | #else |
| 105 | err = -EAFNOSUPPORT; |
| 106 | goto out; |
| 107 | #endif |
| 108 | |
| 109 | default: |
| 110 | goto out; |
| 111 | }; |
| 112 | |
| 113 | err = -EINVAL; |
| 114 | switch (p->id.proto) { |
| 115 | case IPPROTO_AH: |
| 116 | if (!xfrma[XFRMA_ALG_AUTH-1] || |
| 117 | xfrma[XFRMA_ALG_CRYPT-1] || |
| 118 | xfrma[XFRMA_ALG_COMP-1]) |
| 119 | goto out; |
| 120 | break; |
| 121 | |
| 122 | case IPPROTO_ESP: |
| 123 | if ((!xfrma[XFRMA_ALG_AUTH-1] && |
| 124 | !xfrma[XFRMA_ALG_CRYPT-1]) || |
| 125 | xfrma[XFRMA_ALG_COMP-1]) |
| 126 | goto out; |
| 127 | break; |
| 128 | |
| 129 | case IPPROTO_COMP: |
| 130 | if (!xfrma[XFRMA_ALG_COMP-1] || |
| 131 | xfrma[XFRMA_ALG_AUTH-1] || |
| 132 | xfrma[XFRMA_ALG_CRYPT-1]) |
| 133 | goto out; |
| 134 | break; |
| 135 | |
| 136 | default: |
| 137 | goto out; |
| 138 | }; |
| 139 | |
| 140 | if ((err = verify_one_alg(xfrma, XFRMA_ALG_AUTH))) |
| 141 | goto out; |
| 142 | if ((err = verify_one_alg(xfrma, XFRMA_ALG_CRYPT))) |
| 143 | goto out; |
| 144 | if ((err = verify_one_alg(xfrma, XFRMA_ALG_COMP))) |
| 145 | goto out; |
| 146 | if ((err = verify_encap_tmpl(xfrma))) |
| 147 | goto out; |
| 148 | |
| 149 | err = -EINVAL; |
| 150 | switch (p->mode) { |
| 151 | case 0: |
| 152 | case 1: |
| 153 | break; |
| 154 | |
| 155 | default: |
| 156 | goto out; |
| 157 | }; |
| 158 | |
| 159 | err = 0; |
| 160 | |
| 161 | out: |
| 162 | return err; |
| 163 | } |
| 164 | |
| 165 | static int attach_one_algo(struct xfrm_algo **algpp, u8 *props, |
| 166 | struct xfrm_algo_desc *(*get_byname)(char *, int), |
| 167 | struct rtattr *u_arg) |
| 168 | { |
| 169 | struct rtattr *rta = u_arg; |
| 170 | struct xfrm_algo *p, *ualg; |
| 171 | struct xfrm_algo_desc *algo; |
Herbert Xu | b9e9dea | 2005-05-19 12:39:04 -0700 | [diff] [blame] | 172 | int len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 173 | |
| 174 | if (!rta) |
| 175 | return 0; |
| 176 | |
| 177 | ualg = RTA_DATA(rta); |
| 178 | |
| 179 | algo = get_byname(ualg->alg_name, 1); |
| 180 | if (!algo) |
| 181 | return -ENOSYS; |
| 182 | *props = algo->desc.sadb_alg_id; |
| 183 | |
Herbert Xu | b9e9dea | 2005-05-19 12:39:04 -0700 | [diff] [blame] | 184 | len = sizeof(*ualg) + (ualg->alg_key_len + 7U) / 8; |
| 185 | p = kmalloc(len, GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 186 | if (!p) |
| 187 | return -ENOMEM; |
| 188 | |
Herbert Xu | b9e9dea | 2005-05-19 12:39:04 -0700 | [diff] [blame] | 189 | memcpy(p, ualg, len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 190 | *algpp = p; |
| 191 | return 0; |
| 192 | } |
| 193 | |
| 194 | static int attach_encap_tmpl(struct xfrm_encap_tmpl **encapp, struct rtattr *u_arg) |
| 195 | { |
| 196 | struct rtattr *rta = u_arg; |
| 197 | struct xfrm_encap_tmpl *p, *uencap; |
| 198 | |
| 199 | if (!rta) |
| 200 | return 0; |
| 201 | |
| 202 | uencap = RTA_DATA(rta); |
| 203 | p = kmalloc(sizeof(*p), GFP_KERNEL); |
| 204 | if (!p) |
| 205 | return -ENOMEM; |
| 206 | |
| 207 | memcpy(p, uencap, sizeof(*p)); |
| 208 | *encapp = p; |
| 209 | return 0; |
| 210 | } |
| 211 | |
| 212 | static void copy_from_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p) |
| 213 | { |
| 214 | memcpy(&x->id, &p->id, sizeof(x->id)); |
| 215 | memcpy(&x->sel, &p->sel, sizeof(x->sel)); |
| 216 | memcpy(&x->lft, &p->lft, sizeof(x->lft)); |
| 217 | x->props.mode = p->mode; |
| 218 | x->props.replay_window = p->replay_window; |
| 219 | x->props.reqid = p->reqid; |
| 220 | x->props.family = p->family; |
| 221 | x->props.saddr = p->saddr; |
| 222 | x->props.flags = p->flags; |
| 223 | } |
| 224 | |
| 225 | static struct xfrm_state *xfrm_state_construct(struct xfrm_usersa_info *p, |
| 226 | struct rtattr **xfrma, |
| 227 | int *errp) |
| 228 | { |
| 229 | struct xfrm_state *x = xfrm_state_alloc(); |
| 230 | int err = -ENOMEM; |
| 231 | |
| 232 | if (!x) |
| 233 | goto error_no_put; |
| 234 | |
| 235 | copy_from_user_state(x, p); |
| 236 | |
| 237 | if ((err = attach_one_algo(&x->aalg, &x->props.aalgo, |
| 238 | xfrm_aalg_get_byname, |
| 239 | xfrma[XFRMA_ALG_AUTH-1]))) |
| 240 | goto error; |
| 241 | if ((err = attach_one_algo(&x->ealg, &x->props.ealgo, |
| 242 | xfrm_ealg_get_byname, |
| 243 | xfrma[XFRMA_ALG_CRYPT-1]))) |
| 244 | goto error; |
| 245 | if ((err = attach_one_algo(&x->calg, &x->props.calgo, |
| 246 | xfrm_calg_get_byname, |
| 247 | xfrma[XFRMA_ALG_COMP-1]))) |
| 248 | goto error; |
| 249 | if ((err = attach_encap_tmpl(&x->encap, xfrma[XFRMA_ENCAP-1]))) |
| 250 | goto error; |
| 251 | |
| 252 | err = -ENOENT; |
| 253 | x->type = xfrm_get_type(x->id.proto, x->props.family); |
| 254 | if (x->type == NULL) |
| 255 | goto error; |
| 256 | |
| 257 | err = x->type->init_state(x, NULL); |
| 258 | if (err) |
| 259 | goto error; |
| 260 | |
| 261 | x->curlft.add_time = (unsigned long) xtime.tv_sec; |
| 262 | x->km.state = XFRM_STATE_VALID; |
| 263 | x->km.seq = p->seq; |
| 264 | |
| 265 | return x; |
| 266 | |
| 267 | error: |
| 268 | x->km.state = XFRM_STATE_DEAD; |
| 269 | xfrm_state_put(x); |
| 270 | error_no_put: |
| 271 | *errp = err; |
| 272 | return NULL; |
| 273 | } |
| 274 | |
| 275 | static int xfrm_add_sa(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma) |
| 276 | { |
| 277 | struct xfrm_usersa_info *p = NLMSG_DATA(nlh); |
| 278 | struct xfrm_state *x; |
| 279 | int err; |
Jamal Hadi Salim | 26b15da | 2005-06-18 22:42:13 -0700 | [diff] [blame] | 280 | struct km_event c; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 281 | |
| 282 | err = verify_newsa_info(p, (struct rtattr **) xfrma); |
| 283 | if (err) |
| 284 | return err; |
| 285 | |
| 286 | x = xfrm_state_construct(p, (struct rtattr **) xfrma, &err); |
| 287 | if (!x) |
| 288 | return err; |
| 289 | |
Jamal Hadi Salim | 26b15da | 2005-06-18 22:42:13 -0700 | [diff] [blame] | 290 | xfrm_state_hold(x); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 291 | if (nlh->nlmsg_type == XFRM_MSG_NEWSA) |
| 292 | err = xfrm_state_add(x); |
| 293 | else |
| 294 | err = xfrm_state_update(x); |
| 295 | |
| 296 | if (err < 0) { |
| 297 | x->km.state = XFRM_STATE_DEAD; |
| 298 | xfrm_state_put(x); |
Patrick McHardy | 7d6dfe1 | 2005-06-18 22:45:31 -0700 | [diff] [blame] | 299 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 300 | } |
| 301 | |
Jamal Hadi Salim | 26b15da | 2005-06-18 22:42:13 -0700 | [diff] [blame] | 302 | c.seq = nlh->nlmsg_seq; |
| 303 | c.pid = nlh->nlmsg_pid; |
Herbert Xu | f60f6b8 | 2005-06-18 22:44:37 -0700 | [diff] [blame] | 304 | c.event = nlh->nlmsg_type; |
Jamal Hadi Salim | 26b15da | 2005-06-18 22:42:13 -0700 | [diff] [blame] | 305 | |
| 306 | km_state_notify(x, &c); |
Patrick McHardy | 7d6dfe1 | 2005-06-18 22:45:31 -0700 | [diff] [blame] | 307 | out: |
Jamal Hadi Salim | 26b15da | 2005-06-18 22:42:13 -0700 | [diff] [blame] | 308 | xfrm_state_put(x); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 309 | return err; |
| 310 | } |
| 311 | |
| 312 | static int xfrm_del_sa(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma) |
| 313 | { |
| 314 | struct xfrm_state *x; |
Jamal Hadi Salim | 26b15da | 2005-06-18 22:42:13 -0700 | [diff] [blame] | 315 | int err; |
| 316 | struct km_event c; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 317 | struct xfrm_usersa_id *p = NLMSG_DATA(nlh); |
| 318 | |
| 319 | x = xfrm_state_lookup(&p->daddr, p->spi, p->proto, p->family); |
| 320 | if (x == NULL) |
| 321 | return -ESRCH; |
| 322 | |
| 323 | if (xfrm_state_kern(x)) { |
| 324 | xfrm_state_put(x); |
| 325 | return -EPERM; |
| 326 | } |
| 327 | |
Jamal Hadi Salim | 26b15da | 2005-06-18 22:42:13 -0700 | [diff] [blame] | 328 | err = xfrm_state_delete(x); |
| 329 | if (err < 0) { |
| 330 | xfrm_state_put(x); |
| 331 | return err; |
| 332 | } |
| 333 | |
| 334 | c.seq = nlh->nlmsg_seq; |
| 335 | c.pid = nlh->nlmsg_pid; |
Herbert Xu | f60f6b8 | 2005-06-18 22:44:37 -0700 | [diff] [blame] | 336 | c.event = nlh->nlmsg_type; |
Jamal Hadi Salim | 26b15da | 2005-06-18 22:42:13 -0700 | [diff] [blame] | 337 | km_state_notify(x, &c); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 338 | xfrm_state_put(x); |
| 339 | |
Jamal Hadi Salim | 26b15da | 2005-06-18 22:42:13 -0700 | [diff] [blame] | 340 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 341 | } |
| 342 | |
| 343 | static void copy_to_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p) |
| 344 | { |
| 345 | memcpy(&p->id, &x->id, sizeof(p->id)); |
| 346 | memcpy(&p->sel, &x->sel, sizeof(p->sel)); |
| 347 | memcpy(&p->lft, &x->lft, sizeof(p->lft)); |
| 348 | memcpy(&p->curlft, &x->curlft, sizeof(p->curlft)); |
| 349 | memcpy(&p->stats, &x->stats, sizeof(p->stats)); |
| 350 | p->saddr = x->props.saddr; |
| 351 | p->mode = x->props.mode; |
| 352 | p->replay_window = x->props.replay_window; |
| 353 | p->reqid = x->props.reqid; |
| 354 | p->family = x->props.family; |
| 355 | p->flags = x->props.flags; |
| 356 | p->seq = x->km.seq; |
| 357 | } |
| 358 | |
| 359 | struct xfrm_dump_info { |
| 360 | struct sk_buff *in_skb; |
| 361 | struct sk_buff *out_skb; |
| 362 | u32 nlmsg_seq; |
| 363 | u16 nlmsg_flags; |
| 364 | int start_idx; |
| 365 | int this_idx; |
| 366 | }; |
| 367 | |
| 368 | static int dump_one_state(struct xfrm_state *x, int count, void *ptr) |
| 369 | { |
| 370 | struct xfrm_dump_info *sp = ptr; |
| 371 | struct sk_buff *in_skb = sp->in_skb; |
| 372 | struct sk_buff *skb = sp->out_skb; |
| 373 | struct xfrm_usersa_info *p; |
| 374 | struct nlmsghdr *nlh; |
| 375 | unsigned char *b = skb->tail; |
| 376 | |
| 377 | if (sp->this_idx < sp->start_idx) |
| 378 | goto out; |
| 379 | |
| 380 | nlh = NLMSG_PUT(skb, NETLINK_CB(in_skb).pid, |
| 381 | sp->nlmsg_seq, |
| 382 | XFRM_MSG_NEWSA, sizeof(*p)); |
| 383 | nlh->nlmsg_flags = sp->nlmsg_flags; |
| 384 | |
| 385 | p = NLMSG_DATA(nlh); |
| 386 | copy_to_user_state(x, p); |
| 387 | |
| 388 | if (x->aalg) |
| 389 | RTA_PUT(skb, XFRMA_ALG_AUTH, |
| 390 | sizeof(*(x->aalg))+(x->aalg->alg_key_len+7)/8, x->aalg); |
| 391 | if (x->ealg) |
| 392 | RTA_PUT(skb, XFRMA_ALG_CRYPT, |
| 393 | sizeof(*(x->ealg))+(x->ealg->alg_key_len+7)/8, x->ealg); |
| 394 | if (x->calg) |
| 395 | RTA_PUT(skb, XFRMA_ALG_COMP, sizeof(*(x->calg)), x->calg); |
| 396 | |
| 397 | if (x->encap) |
| 398 | RTA_PUT(skb, XFRMA_ENCAP, sizeof(*x->encap), x->encap); |
| 399 | |
| 400 | nlh->nlmsg_len = skb->tail - b; |
| 401 | out: |
| 402 | sp->this_idx++; |
| 403 | return 0; |
| 404 | |
| 405 | nlmsg_failure: |
| 406 | rtattr_failure: |
| 407 | skb_trim(skb, b - skb->data); |
| 408 | return -1; |
| 409 | } |
| 410 | |
| 411 | static int xfrm_dump_sa(struct sk_buff *skb, struct netlink_callback *cb) |
| 412 | { |
| 413 | struct xfrm_dump_info info; |
| 414 | |
| 415 | info.in_skb = cb->skb; |
| 416 | info.out_skb = skb; |
| 417 | info.nlmsg_seq = cb->nlh->nlmsg_seq; |
| 418 | info.nlmsg_flags = NLM_F_MULTI; |
| 419 | info.this_idx = 0; |
| 420 | info.start_idx = cb->args[0]; |
| 421 | (void) xfrm_state_walk(IPSEC_PROTO_ANY, dump_one_state, &info); |
| 422 | cb->args[0] = info.this_idx; |
| 423 | |
| 424 | return skb->len; |
| 425 | } |
| 426 | |
| 427 | static struct sk_buff *xfrm_state_netlink(struct sk_buff *in_skb, |
| 428 | struct xfrm_state *x, u32 seq) |
| 429 | { |
| 430 | struct xfrm_dump_info info; |
| 431 | struct sk_buff *skb; |
| 432 | |
| 433 | skb = alloc_skb(NLMSG_GOODSIZE, GFP_ATOMIC); |
| 434 | if (!skb) |
| 435 | return ERR_PTR(-ENOMEM); |
| 436 | |
| 437 | NETLINK_CB(skb).dst_pid = NETLINK_CB(in_skb).pid; |
| 438 | info.in_skb = in_skb; |
| 439 | info.out_skb = skb; |
| 440 | info.nlmsg_seq = seq; |
| 441 | info.nlmsg_flags = 0; |
| 442 | info.this_idx = info.start_idx = 0; |
| 443 | |
| 444 | if (dump_one_state(x, 0, &info)) { |
| 445 | kfree_skb(skb); |
| 446 | return NULL; |
| 447 | } |
| 448 | |
| 449 | return skb; |
| 450 | } |
| 451 | |
| 452 | static int xfrm_get_sa(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma) |
| 453 | { |
| 454 | struct xfrm_usersa_id *p = NLMSG_DATA(nlh); |
| 455 | struct xfrm_state *x; |
| 456 | struct sk_buff *resp_skb; |
| 457 | int err; |
| 458 | |
| 459 | x = xfrm_state_lookup(&p->daddr, p->spi, p->proto, p->family); |
| 460 | err = -ESRCH; |
| 461 | if (x == NULL) |
| 462 | goto out_noput; |
| 463 | |
| 464 | resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq); |
| 465 | if (IS_ERR(resp_skb)) { |
| 466 | err = PTR_ERR(resp_skb); |
| 467 | } else { |
| 468 | err = netlink_unicast(xfrm_nl, resp_skb, |
| 469 | NETLINK_CB(skb).pid, MSG_DONTWAIT); |
| 470 | } |
| 471 | xfrm_state_put(x); |
| 472 | out_noput: |
| 473 | return err; |
| 474 | } |
| 475 | |
| 476 | static int verify_userspi_info(struct xfrm_userspi_info *p) |
| 477 | { |
| 478 | switch (p->info.id.proto) { |
| 479 | case IPPROTO_AH: |
| 480 | case IPPROTO_ESP: |
| 481 | break; |
| 482 | |
| 483 | case IPPROTO_COMP: |
| 484 | /* IPCOMP spi is 16-bits. */ |
| 485 | if (p->max >= 0x10000) |
| 486 | return -EINVAL; |
| 487 | break; |
| 488 | |
| 489 | default: |
| 490 | return -EINVAL; |
| 491 | }; |
| 492 | |
| 493 | if (p->min > p->max) |
| 494 | return -EINVAL; |
| 495 | |
| 496 | return 0; |
| 497 | } |
| 498 | |
| 499 | static int xfrm_alloc_userspi(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma) |
| 500 | { |
| 501 | struct xfrm_state *x; |
| 502 | struct xfrm_userspi_info *p; |
| 503 | struct sk_buff *resp_skb; |
| 504 | xfrm_address_t *daddr; |
| 505 | int family; |
| 506 | int err; |
| 507 | |
| 508 | p = NLMSG_DATA(nlh); |
| 509 | err = verify_userspi_info(p); |
| 510 | if (err) |
| 511 | goto out_noput; |
| 512 | |
| 513 | family = p->info.family; |
| 514 | daddr = &p->info.id.daddr; |
| 515 | |
| 516 | x = NULL; |
| 517 | if (p->info.seq) { |
| 518 | x = xfrm_find_acq_byseq(p->info.seq); |
| 519 | if (x && xfrm_addr_cmp(&x->id.daddr, daddr, family)) { |
| 520 | xfrm_state_put(x); |
| 521 | x = NULL; |
| 522 | } |
| 523 | } |
| 524 | |
| 525 | if (!x) |
| 526 | x = xfrm_find_acq(p->info.mode, p->info.reqid, |
| 527 | p->info.id.proto, daddr, |
| 528 | &p->info.saddr, 1, |
| 529 | family); |
| 530 | err = -ENOENT; |
| 531 | if (x == NULL) |
| 532 | goto out_noput; |
| 533 | |
| 534 | resp_skb = ERR_PTR(-ENOENT); |
| 535 | |
| 536 | spin_lock_bh(&x->lock); |
| 537 | if (x->km.state != XFRM_STATE_DEAD) { |
| 538 | xfrm_alloc_spi(x, htonl(p->min), htonl(p->max)); |
| 539 | if (x->id.spi) |
| 540 | resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq); |
| 541 | } |
| 542 | spin_unlock_bh(&x->lock); |
| 543 | |
| 544 | if (IS_ERR(resp_skb)) { |
| 545 | err = PTR_ERR(resp_skb); |
| 546 | goto out; |
| 547 | } |
| 548 | |
| 549 | err = netlink_unicast(xfrm_nl, resp_skb, |
| 550 | NETLINK_CB(skb).pid, MSG_DONTWAIT); |
| 551 | |
| 552 | out: |
| 553 | xfrm_state_put(x); |
| 554 | out_noput: |
| 555 | return err; |
| 556 | } |
| 557 | |
| 558 | static int verify_policy_dir(__u8 dir) |
| 559 | { |
| 560 | switch (dir) { |
| 561 | case XFRM_POLICY_IN: |
| 562 | case XFRM_POLICY_OUT: |
| 563 | case XFRM_POLICY_FWD: |
| 564 | break; |
| 565 | |
| 566 | default: |
| 567 | return -EINVAL; |
| 568 | }; |
| 569 | |
| 570 | return 0; |
| 571 | } |
| 572 | |
| 573 | static int verify_newpolicy_info(struct xfrm_userpolicy_info *p) |
| 574 | { |
| 575 | switch (p->share) { |
| 576 | case XFRM_SHARE_ANY: |
| 577 | case XFRM_SHARE_SESSION: |
| 578 | case XFRM_SHARE_USER: |
| 579 | case XFRM_SHARE_UNIQUE: |
| 580 | break; |
| 581 | |
| 582 | default: |
| 583 | return -EINVAL; |
| 584 | }; |
| 585 | |
| 586 | switch (p->action) { |
| 587 | case XFRM_POLICY_ALLOW: |
| 588 | case XFRM_POLICY_BLOCK: |
| 589 | break; |
| 590 | |
| 591 | default: |
| 592 | return -EINVAL; |
| 593 | }; |
| 594 | |
| 595 | switch (p->sel.family) { |
| 596 | case AF_INET: |
| 597 | break; |
| 598 | |
| 599 | case AF_INET6: |
| 600 | #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) |
| 601 | break; |
| 602 | #else |
| 603 | return -EAFNOSUPPORT; |
| 604 | #endif |
| 605 | |
| 606 | default: |
| 607 | return -EINVAL; |
| 608 | }; |
| 609 | |
| 610 | return verify_policy_dir(p->dir); |
| 611 | } |
| 612 | |
| 613 | static void copy_templates(struct xfrm_policy *xp, struct xfrm_user_tmpl *ut, |
| 614 | int nr) |
| 615 | { |
| 616 | int i; |
| 617 | |
| 618 | xp->xfrm_nr = nr; |
| 619 | for (i = 0; i < nr; i++, ut++) { |
| 620 | struct xfrm_tmpl *t = &xp->xfrm_vec[i]; |
| 621 | |
| 622 | memcpy(&t->id, &ut->id, sizeof(struct xfrm_id)); |
| 623 | memcpy(&t->saddr, &ut->saddr, |
| 624 | sizeof(xfrm_address_t)); |
| 625 | t->reqid = ut->reqid; |
| 626 | t->mode = ut->mode; |
| 627 | t->share = ut->share; |
| 628 | t->optional = ut->optional; |
| 629 | t->aalgos = ut->aalgos; |
| 630 | t->ealgos = ut->ealgos; |
| 631 | t->calgos = ut->calgos; |
| 632 | } |
| 633 | } |
| 634 | |
| 635 | static int copy_from_user_tmpl(struct xfrm_policy *pol, struct rtattr **xfrma) |
| 636 | { |
| 637 | struct rtattr *rt = xfrma[XFRMA_TMPL-1]; |
| 638 | struct xfrm_user_tmpl *utmpl; |
| 639 | int nr; |
| 640 | |
| 641 | if (!rt) { |
| 642 | pol->xfrm_nr = 0; |
| 643 | } else { |
| 644 | nr = (rt->rta_len - sizeof(*rt)) / sizeof(*utmpl); |
| 645 | |
| 646 | if (nr > XFRM_MAX_DEPTH) |
| 647 | return -EINVAL; |
| 648 | |
| 649 | copy_templates(pol, RTA_DATA(rt), nr); |
| 650 | } |
| 651 | return 0; |
| 652 | } |
| 653 | |
| 654 | static void copy_from_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p) |
| 655 | { |
| 656 | xp->priority = p->priority; |
| 657 | xp->index = p->index; |
| 658 | memcpy(&xp->selector, &p->sel, sizeof(xp->selector)); |
| 659 | memcpy(&xp->lft, &p->lft, sizeof(xp->lft)); |
| 660 | xp->action = p->action; |
| 661 | xp->flags = p->flags; |
| 662 | xp->family = p->sel.family; |
| 663 | /* XXX xp->share = p->share; */ |
| 664 | } |
| 665 | |
| 666 | static void copy_to_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p, int dir) |
| 667 | { |
| 668 | memcpy(&p->sel, &xp->selector, sizeof(p->sel)); |
| 669 | memcpy(&p->lft, &xp->lft, sizeof(p->lft)); |
| 670 | memcpy(&p->curlft, &xp->curlft, sizeof(p->curlft)); |
| 671 | p->priority = xp->priority; |
| 672 | p->index = xp->index; |
| 673 | p->sel.family = xp->family; |
| 674 | p->dir = dir; |
| 675 | p->action = xp->action; |
| 676 | p->flags = xp->flags; |
| 677 | p->share = XFRM_SHARE_ANY; /* XXX xp->share */ |
| 678 | } |
| 679 | |
| 680 | static struct xfrm_policy *xfrm_policy_construct(struct xfrm_userpolicy_info *p, struct rtattr **xfrma, int *errp) |
| 681 | { |
| 682 | struct xfrm_policy *xp = xfrm_policy_alloc(GFP_KERNEL); |
| 683 | int err; |
| 684 | |
| 685 | if (!xp) { |
| 686 | *errp = -ENOMEM; |
| 687 | return NULL; |
| 688 | } |
| 689 | |
| 690 | copy_from_user_policy(xp, p); |
| 691 | err = copy_from_user_tmpl(xp, xfrma); |
| 692 | if (err) { |
| 693 | *errp = err; |
| 694 | kfree(xp); |
| 695 | xp = NULL; |
| 696 | } |
| 697 | |
| 698 | return xp; |
| 699 | } |
| 700 | |
| 701 | static int xfrm_add_policy(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma) |
| 702 | { |
| 703 | struct xfrm_userpolicy_info *p = NLMSG_DATA(nlh); |
| 704 | struct xfrm_policy *xp; |
Jamal Hadi Salim | 26b15da | 2005-06-18 22:42:13 -0700 | [diff] [blame] | 705 | struct km_event c; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 706 | int err; |
| 707 | int excl; |
| 708 | |
| 709 | err = verify_newpolicy_info(p); |
| 710 | if (err) |
| 711 | return err; |
| 712 | |
| 713 | xp = xfrm_policy_construct(p, (struct rtattr **) xfrma, &err); |
| 714 | if (!xp) |
| 715 | return err; |
| 716 | |
Jamal Hadi Salim | 26b15da | 2005-06-18 22:42:13 -0700 | [diff] [blame] | 717 | /* shouldnt excl be based on nlh flags?? |
| 718 | * Aha! this is anti-netlink really i.e more pfkey derived |
| 719 | * in netlink excl is a flag and you wouldnt need |
| 720 | * a type XFRM_MSG_UPDPOLICY - JHS */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 721 | excl = nlh->nlmsg_type == XFRM_MSG_NEWPOLICY; |
| 722 | err = xfrm_policy_insert(p->dir, xp, excl); |
| 723 | if (err) { |
| 724 | kfree(xp); |
| 725 | return err; |
| 726 | } |
| 727 | |
Herbert Xu | f60f6b8 | 2005-06-18 22:44:37 -0700 | [diff] [blame] | 728 | c.event = nlh->nlmsg_type; |
Jamal Hadi Salim | 26b15da | 2005-06-18 22:42:13 -0700 | [diff] [blame] | 729 | c.seq = nlh->nlmsg_seq; |
| 730 | c.pid = nlh->nlmsg_pid; |
| 731 | km_policy_notify(xp, p->dir, &c); |
| 732 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 733 | xfrm_pol_put(xp); |
| 734 | |
| 735 | return 0; |
| 736 | } |
| 737 | |
| 738 | static int copy_to_user_tmpl(struct xfrm_policy *xp, struct sk_buff *skb) |
| 739 | { |
| 740 | struct xfrm_user_tmpl vec[XFRM_MAX_DEPTH]; |
| 741 | int i; |
| 742 | |
| 743 | if (xp->xfrm_nr == 0) |
| 744 | return 0; |
| 745 | |
| 746 | for (i = 0; i < xp->xfrm_nr; i++) { |
| 747 | struct xfrm_user_tmpl *up = &vec[i]; |
| 748 | struct xfrm_tmpl *kp = &xp->xfrm_vec[i]; |
| 749 | |
| 750 | memcpy(&up->id, &kp->id, sizeof(up->id)); |
| 751 | up->family = xp->family; |
| 752 | memcpy(&up->saddr, &kp->saddr, sizeof(up->saddr)); |
| 753 | up->reqid = kp->reqid; |
| 754 | up->mode = kp->mode; |
| 755 | up->share = kp->share; |
| 756 | up->optional = kp->optional; |
| 757 | up->aalgos = kp->aalgos; |
| 758 | up->ealgos = kp->ealgos; |
| 759 | up->calgos = kp->calgos; |
| 760 | } |
| 761 | RTA_PUT(skb, XFRMA_TMPL, |
| 762 | (sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr), |
| 763 | vec); |
| 764 | |
| 765 | return 0; |
| 766 | |
| 767 | rtattr_failure: |
| 768 | return -1; |
| 769 | } |
| 770 | |
| 771 | static int dump_one_policy(struct xfrm_policy *xp, int dir, int count, void *ptr) |
| 772 | { |
| 773 | struct xfrm_dump_info *sp = ptr; |
| 774 | struct xfrm_userpolicy_info *p; |
| 775 | struct sk_buff *in_skb = sp->in_skb; |
| 776 | struct sk_buff *skb = sp->out_skb; |
| 777 | struct nlmsghdr *nlh; |
| 778 | unsigned char *b = skb->tail; |
| 779 | |
| 780 | if (sp->this_idx < sp->start_idx) |
| 781 | goto out; |
| 782 | |
| 783 | nlh = NLMSG_PUT(skb, NETLINK_CB(in_skb).pid, |
| 784 | sp->nlmsg_seq, |
| 785 | XFRM_MSG_NEWPOLICY, sizeof(*p)); |
| 786 | p = NLMSG_DATA(nlh); |
| 787 | nlh->nlmsg_flags = sp->nlmsg_flags; |
| 788 | |
| 789 | copy_to_user_policy(xp, p, dir); |
| 790 | if (copy_to_user_tmpl(xp, skb) < 0) |
| 791 | goto nlmsg_failure; |
| 792 | |
| 793 | nlh->nlmsg_len = skb->tail - b; |
| 794 | out: |
| 795 | sp->this_idx++; |
| 796 | return 0; |
| 797 | |
| 798 | nlmsg_failure: |
| 799 | skb_trim(skb, b - skb->data); |
| 800 | return -1; |
| 801 | } |
| 802 | |
| 803 | static int xfrm_dump_policy(struct sk_buff *skb, struct netlink_callback *cb) |
| 804 | { |
| 805 | struct xfrm_dump_info info; |
| 806 | |
| 807 | info.in_skb = cb->skb; |
| 808 | info.out_skb = skb; |
| 809 | info.nlmsg_seq = cb->nlh->nlmsg_seq; |
| 810 | info.nlmsg_flags = NLM_F_MULTI; |
| 811 | info.this_idx = 0; |
| 812 | info.start_idx = cb->args[0]; |
| 813 | (void) xfrm_policy_walk(dump_one_policy, &info); |
| 814 | cb->args[0] = info.this_idx; |
| 815 | |
| 816 | return skb->len; |
| 817 | } |
| 818 | |
| 819 | static struct sk_buff *xfrm_policy_netlink(struct sk_buff *in_skb, |
| 820 | struct xfrm_policy *xp, |
| 821 | int dir, u32 seq) |
| 822 | { |
| 823 | struct xfrm_dump_info info; |
| 824 | struct sk_buff *skb; |
| 825 | |
| 826 | skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL); |
| 827 | if (!skb) |
| 828 | return ERR_PTR(-ENOMEM); |
| 829 | |
| 830 | NETLINK_CB(skb).dst_pid = NETLINK_CB(in_skb).pid; |
| 831 | info.in_skb = in_skb; |
| 832 | info.out_skb = skb; |
| 833 | info.nlmsg_seq = seq; |
| 834 | info.nlmsg_flags = 0; |
| 835 | info.this_idx = info.start_idx = 0; |
| 836 | |
| 837 | if (dump_one_policy(xp, dir, 0, &info) < 0) { |
| 838 | kfree_skb(skb); |
| 839 | return NULL; |
| 840 | } |
| 841 | |
| 842 | return skb; |
| 843 | } |
| 844 | |
| 845 | static int xfrm_get_policy(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma) |
| 846 | { |
| 847 | struct xfrm_policy *xp; |
| 848 | struct xfrm_userpolicy_id *p; |
| 849 | int err; |
Jamal Hadi Salim | 26b15da | 2005-06-18 22:42:13 -0700 | [diff] [blame] | 850 | struct km_event c; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 851 | int delete; |
| 852 | |
| 853 | p = NLMSG_DATA(nlh); |
| 854 | delete = nlh->nlmsg_type == XFRM_MSG_DELPOLICY; |
| 855 | |
| 856 | err = verify_policy_dir(p->dir); |
| 857 | if (err) |
| 858 | return err; |
| 859 | |
| 860 | if (p->index) |
| 861 | xp = xfrm_policy_byid(p->dir, p->index, delete); |
| 862 | else |
| 863 | xp = xfrm_policy_bysel(p->dir, &p->sel, delete); |
| 864 | if (xp == NULL) |
| 865 | return -ENOENT; |
| 866 | |
| 867 | if (!delete) { |
| 868 | struct sk_buff *resp_skb; |
| 869 | |
| 870 | resp_skb = xfrm_policy_netlink(skb, xp, p->dir, nlh->nlmsg_seq); |
| 871 | if (IS_ERR(resp_skb)) { |
| 872 | err = PTR_ERR(resp_skb); |
| 873 | } else { |
| 874 | err = netlink_unicast(xfrm_nl, resp_skb, |
| 875 | NETLINK_CB(skb).pid, |
| 876 | MSG_DONTWAIT); |
| 877 | } |
Jamal Hadi Salim | 26b15da | 2005-06-18 22:42:13 -0700 | [diff] [blame] | 878 | } else { |
Herbert Xu | e744389 | 2005-06-18 22:44:18 -0700 | [diff] [blame] | 879 | c.data.byid = p->index; |
Herbert Xu | f60f6b8 | 2005-06-18 22:44:37 -0700 | [diff] [blame] | 880 | c.event = nlh->nlmsg_type; |
Jamal Hadi Salim | 26b15da | 2005-06-18 22:42:13 -0700 | [diff] [blame] | 881 | c.seq = nlh->nlmsg_seq; |
| 882 | c.pid = nlh->nlmsg_pid; |
| 883 | km_policy_notify(xp, p->dir, &c); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 884 | } |
| 885 | |
| 886 | xfrm_pol_put(xp); |
| 887 | |
| 888 | return err; |
| 889 | } |
| 890 | |
| 891 | static int xfrm_flush_sa(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma) |
| 892 | { |
Jamal Hadi Salim | 26b15da | 2005-06-18 22:42:13 -0700 | [diff] [blame] | 893 | struct km_event c; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 894 | struct xfrm_usersa_flush *p = NLMSG_DATA(nlh); |
| 895 | |
| 896 | xfrm_state_flush(p->proto); |
Herbert Xu | bf08867f9 | 2005-06-18 22:44:00 -0700 | [diff] [blame] | 897 | c.data.proto = p->proto; |
Herbert Xu | f60f6b8 | 2005-06-18 22:44:37 -0700 | [diff] [blame] | 898 | c.event = nlh->nlmsg_type; |
Jamal Hadi Salim | 26b15da | 2005-06-18 22:42:13 -0700 | [diff] [blame] | 899 | c.seq = nlh->nlmsg_seq; |
| 900 | c.pid = nlh->nlmsg_pid; |
| 901 | km_state_notify(NULL, &c); |
| 902 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 903 | return 0; |
| 904 | } |
| 905 | |
| 906 | static int xfrm_flush_policy(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma) |
| 907 | { |
Jamal Hadi Salim | 26b15da | 2005-06-18 22:42:13 -0700 | [diff] [blame] | 908 | struct km_event c; |
| 909 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 910 | xfrm_policy_flush(); |
Herbert Xu | f60f6b8 | 2005-06-18 22:44:37 -0700 | [diff] [blame] | 911 | c.event = nlh->nlmsg_type; |
Jamal Hadi Salim | 26b15da | 2005-06-18 22:42:13 -0700 | [diff] [blame] | 912 | c.seq = nlh->nlmsg_seq; |
| 913 | c.pid = nlh->nlmsg_pid; |
| 914 | km_policy_notify(NULL, 0, &c); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 915 | return 0; |
| 916 | } |
| 917 | |
Thomas Graf | 492b558 | 2005-05-03 14:26:40 -0700 | [diff] [blame] | 918 | #define XMSGSIZE(type) NLMSG_LENGTH(sizeof(struct type)) |
| 919 | |
| 920 | static const int xfrm_msg_min[XFRM_NR_MSGTYPES] = { |
| 921 | [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info), |
| 922 | [XFRM_MSG_DELSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id), |
| 923 | [XFRM_MSG_GETSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id), |
| 924 | [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info), |
| 925 | [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id), |
| 926 | [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id), |
| 927 | [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userspi_info), |
| 928 | [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_acquire), |
| 929 | [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_expire), |
| 930 | [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info), |
| 931 | [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info), |
| 932 | [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_polexpire), |
| 933 | [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_flush), |
| 934 | [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = NLMSG_LENGTH(0), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 935 | }; |
| 936 | |
Thomas Graf | 492b558 | 2005-05-03 14:26:40 -0700 | [diff] [blame] | 937 | #undef XMSGSIZE |
| 938 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 939 | static struct xfrm_link { |
| 940 | int (*doit)(struct sk_buff *, struct nlmsghdr *, void **); |
| 941 | int (*dump)(struct sk_buff *, struct netlink_callback *); |
Thomas Graf | 492b558 | 2005-05-03 14:26:40 -0700 | [diff] [blame] | 942 | } xfrm_dispatch[XFRM_NR_MSGTYPES] = { |
| 943 | [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa }, |
| 944 | [XFRM_MSG_DELSA - XFRM_MSG_BASE] = { .doit = xfrm_del_sa }, |
| 945 | [XFRM_MSG_GETSA - XFRM_MSG_BASE] = { .doit = xfrm_get_sa, |
| 946 | .dump = xfrm_dump_sa }, |
| 947 | [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy }, |
| 948 | [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy }, |
| 949 | [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy, |
| 950 | .dump = xfrm_dump_policy }, |
| 951 | [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = { .doit = xfrm_alloc_userspi }, |
| 952 | [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy }, |
| 953 | [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa }, |
| 954 | [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = { .doit = xfrm_flush_sa }, |
| 955 | [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_flush_policy }, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 956 | }; |
| 957 | |
| 958 | static int xfrm_done(struct netlink_callback *cb) |
| 959 | { |
| 960 | return 0; |
| 961 | } |
| 962 | |
| 963 | static int xfrm_user_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh, int *errp) |
| 964 | { |
| 965 | struct rtattr *xfrma[XFRMA_MAX]; |
| 966 | struct xfrm_link *link; |
| 967 | int type, min_len; |
| 968 | |
| 969 | if (!(nlh->nlmsg_flags & NLM_F_REQUEST)) |
| 970 | return 0; |
| 971 | |
| 972 | type = nlh->nlmsg_type; |
| 973 | |
| 974 | /* A control message: ignore them */ |
| 975 | if (type < XFRM_MSG_BASE) |
| 976 | return 0; |
| 977 | |
| 978 | /* Unknown message: reply with EINVAL */ |
| 979 | if (type > XFRM_MSG_MAX) |
| 980 | goto err_einval; |
| 981 | |
| 982 | type -= XFRM_MSG_BASE; |
| 983 | link = &xfrm_dispatch[type]; |
| 984 | |
| 985 | /* All operations require privileges, even GET */ |
| 986 | if (security_netlink_recv(skb)) { |
| 987 | *errp = -EPERM; |
| 988 | return -1; |
| 989 | } |
| 990 | |
Thomas Graf | 492b558 | 2005-05-03 14:26:40 -0700 | [diff] [blame] | 991 | if ((type == (XFRM_MSG_GETSA - XFRM_MSG_BASE) || |
| 992 | type == (XFRM_MSG_GETPOLICY - XFRM_MSG_BASE)) && |
| 993 | (nlh->nlmsg_flags & NLM_F_DUMP)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 994 | u32 rlen; |
| 995 | |
| 996 | if (link->dump == NULL) |
| 997 | goto err_einval; |
| 998 | |
| 999 | if ((*errp = netlink_dump_start(xfrm_nl, skb, nlh, |
| 1000 | link->dump, |
| 1001 | xfrm_done)) != 0) { |
| 1002 | return -1; |
| 1003 | } |
| 1004 | rlen = NLMSG_ALIGN(nlh->nlmsg_len); |
| 1005 | if (rlen > skb->len) |
| 1006 | rlen = skb->len; |
| 1007 | skb_pull(skb, rlen); |
| 1008 | return -1; |
| 1009 | } |
| 1010 | |
| 1011 | memset(xfrma, 0, sizeof(xfrma)); |
| 1012 | |
| 1013 | if (nlh->nlmsg_len < (min_len = xfrm_msg_min[type])) |
| 1014 | goto err_einval; |
| 1015 | |
| 1016 | if (nlh->nlmsg_len > min_len) { |
| 1017 | int attrlen = nlh->nlmsg_len - NLMSG_ALIGN(min_len); |
| 1018 | struct rtattr *attr = (void *) nlh + NLMSG_ALIGN(min_len); |
| 1019 | |
| 1020 | while (RTA_OK(attr, attrlen)) { |
| 1021 | unsigned short flavor = attr->rta_type; |
| 1022 | if (flavor) { |
| 1023 | if (flavor > XFRMA_MAX) |
| 1024 | goto err_einval; |
| 1025 | xfrma[flavor - 1] = attr; |
| 1026 | } |
| 1027 | attr = RTA_NEXT(attr, attrlen); |
| 1028 | } |
| 1029 | } |
| 1030 | |
| 1031 | if (link->doit == NULL) |
| 1032 | goto err_einval; |
| 1033 | *errp = link->doit(skb, nlh, (void **) &xfrma); |
| 1034 | |
| 1035 | return *errp; |
| 1036 | |
| 1037 | err_einval: |
| 1038 | *errp = -EINVAL; |
| 1039 | return -1; |
| 1040 | } |
| 1041 | |
| 1042 | static int xfrm_user_rcv_skb(struct sk_buff *skb) |
| 1043 | { |
| 1044 | int err; |
| 1045 | struct nlmsghdr *nlh; |
| 1046 | |
| 1047 | while (skb->len >= NLMSG_SPACE(0)) { |
| 1048 | u32 rlen; |
| 1049 | |
| 1050 | nlh = (struct nlmsghdr *) skb->data; |
| 1051 | if (nlh->nlmsg_len < sizeof(*nlh) || |
| 1052 | skb->len < nlh->nlmsg_len) |
| 1053 | return 0; |
| 1054 | rlen = NLMSG_ALIGN(nlh->nlmsg_len); |
| 1055 | if (rlen > skb->len) |
| 1056 | rlen = skb->len; |
| 1057 | if (xfrm_user_rcv_msg(skb, nlh, &err) < 0) { |
| 1058 | if (err == 0) |
| 1059 | return -1; |
| 1060 | netlink_ack(skb, nlh, err); |
| 1061 | } else if (nlh->nlmsg_flags & NLM_F_ACK) |
| 1062 | netlink_ack(skb, nlh, 0); |
| 1063 | skb_pull(skb, rlen); |
| 1064 | } |
| 1065 | |
| 1066 | return 0; |
| 1067 | } |
| 1068 | |
| 1069 | static void xfrm_netlink_rcv(struct sock *sk, int len) |
| 1070 | { |
Herbert Xu | 2a0a6eb | 2005-05-03 14:55:09 -0700 | [diff] [blame] | 1071 | unsigned int qlen = skb_queue_len(&sk->sk_receive_queue); |
| 1072 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1073 | do { |
| 1074 | struct sk_buff *skb; |
| 1075 | |
| 1076 | down(&xfrm_cfg_sem); |
| 1077 | |
Herbert Xu | 2a0a6eb | 2005-05-03 14:55:09 -0700 | [diff] [blame] | 1078 | if (qlen > skb_queue_len(&sk->sk_receive_queue)) |
| 1079 | qlen = skb_queue_len(&sk->sk_receive_queue); |
| 1080 | |
David S. Miller | 09e1430 | 2005-05-03 15:30:05 -0700 | [diff] [blame] | 1081 | for (; qlen; qlen--) { |
Herbert Xu | 2a0a6eb | 2005-05-03 14:55:09 -0700 | [diff] [blame] | 1082 | skb = skb_dequeue(&sk->sk_receive_queue); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1083 | if (xfrm_user_rcv_skb(skb)) { |
David S. Miller | 09e1430 | 2005-05-03 15:30:05 -0700 | [diff] [blame] | 1084 | if (skb->len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1085 | skb_queue_head(&sk->sk_receive_queue, |
| 1086 | skb); |
David S. Miller | 0f4821e | 2005-05-03 16:15:59 -0700 | [diff] [blame] | 1087 | else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1088 | kfree_skb(skb); |
David S. Miller | 0f4821e | 2005-05-03 16:15:59 -0700 | [diff] [blame] | 1089 | qlen--; |
| 1090 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1091 | break; |
| 1092 | } |
| 1093 | kfree_skb(skb); |
| 1094 | } |
| 1095 | |
| 1096 | up(&xfrm_cfg_sem); |
| 1097 | |
Herbert Xu | 2a0a6eb | 2005-05-03 14:55:09 -0700 | [diff] [blame] | 1098 | } while (qlen); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1099 | } |
| 1100 | |
| 1101 | static int build_expire(struct sk_buff *skb, struct xfrm_state *x, int hard) |
| 1102 | { |
| 1103 | struct xfrm_user_expire *ue; |
| 1104 | struct nlmsghdr *nlh; |
| 1105 | unsigned char *b = skb->tail; |
| 1106 | |
| 1107 | nlh = NLMSG_PUT(skb, 0, 0, XFRM_MSG_EXPIRE, |
| 1108 | sizeof(*ue)); |
| 1109 | ue = NLMSG_DATA(nlh); |
| 1110 | nlh->nlmsg_flags = 0; |
| 1111 | |
| 1112 | copy_to_user_state(x, &ue->state); |
| 1113 | ue->hard = (hard != 0) ? 1 : 0; |
| 1114 | |
| 1115 | nlh->nlmsg_len = skb->tail - b; |
| 1116 | return skb->len; |
| 1117 | |
| 1118 | nlmsg_failure: |
| 1119 | skb_trim(skb, b - skb->data); |
| 1120 | return -1; |
| 1121 | } |
| 1122 | |
Jamal Hadi Salim | 26b15da | 2005-06-18 22:42:13 -0700 | [diff] [blame] | 1123 | static int xfrm_exp_state_notify(struct xfrm_state *x, struct km_event *c) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1124 | { |
| 1125 | struct sk_buff *skb; |
Jamal Hadi Salim | ee57eef | 2005-06-18 22:45:56 -0700 | [diff] [blame] | 1126 | int len = NLMSG_LENGTH(sizeof(struct xfrm_user_expire)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1127 | |
Jamal Hadi Salim | ee57eef | 2005-06-18 22:45:56 -0700 | [diff] [blame] | 1128 | skb = alloc_skb(len, GFP_ATOMIC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1129 | if (skb == NULL) |
| 1130 | return -ENOMEM; |
| 1131 | |
Herbert Xu | bf08867f9 | 2005-06-18 22:44:00 -0700 | [diff] [blame] | 1132 | if (build_expire(skb, x, c->data.hard) < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1133 | BUG(); |
| 1134 | |
| 1135 | NETLINK_CB(skb).dst_groups = XFRMGRP_EXPIRE; |
| 1136 | |
| 1137 | return netlink_broadcast(xfrm_nl, skb, 0, XFRMGRP_EXPIRE, GFP_ATOMIC); |
| 1138 | } |
| 1139 | |
Jamal Hadi Salim | 26b15da | 2005-06-18 22:42:13 -0700 | [diff] [blame] | 1140 | static int xfrm_notify_sa_flush(struct km_event *c) |
| 1141 | { |
| 1142 | struct xfrm_usersa_flush *p; |
| 1143 | struct nlmsghdr *nlh; |
| 1144 | struct sk_buff *skb; |
| 1145 | unsigned char *b; |
| 1146 | int len = NLMSG_LENGTH(sizeof(struct xfrm_usersa_flush)); |
| 1147 | |
| 1148 | skb = alloc_skb(len, GFP_ATOMIC); |
| 1149 | if (skb == NULL) |
| 1150 | return -ENOMEM; |
| 1151 | b = skb->tail; |
| 1152 | |
| 1153 | nlh = NLMSG_PUT(skb, c->pid, c->seq, |
| 1154 | XFRM_MSG_FLUSHSA, sizeof(*p)); |
| 1155 | nlh->nlmsg_flags = 0; |
| 1156 | |
| 1157 | p = NLMSG_DATA(nlh); |
Herbert Xu | bf08867f9 | 2005-06-18 22:44:00 -0700 | [diff] [blame] | 1158 | p->proto = c->data.proto; |
Jamal Hadi Salim | 26b15da | 2005-06-18 22:42:13 -0700 | [diff] [blame] | 1159 | |
| 1160 | nlh->nlmsg_len = skb->tail - b; |
| 1161 | |
| 1162 | return netlink_broadcast(xfrm_nl, skb, 0, XFRMGRP_SA, GFP_ATOMIC); |
| 1163 | |
| 1164 | nlmsg_failure: |
| 1165 | kfree_skb(skb); |
| 1166 | return -1; |
| 1167 | } |
| 1168 | |
| 1169 | static int inline xfrm_sa_len(struct xfrm_state *x) |
| 1170 | { |
Herbert Xu | 0603eac | 2005-06-18 22:54:36 -0700 | [diff] [blame^] | 1171 | int l = 0; |
Jamal Hadi Salim | 26b15da | 2005-06-18 22:42:13 -0700 | [diff] [blame] | 1172 | if (x->aalg) |
| 1173 | l += RTA_SPACE(sizeof(*x->aalg) + (x->aalg->alg_key_len+7)/8); |
| 1174 | if (x->ealg) |
| 1175 | l += RTA_SPACE(sizeof(*x->ealg) + (x->ealg->alg_key_len+7)/8); |
| 1176 | if (x->calg) |
| 1177 | l += RTA_SPACE(sizeof(*x->calg)); |
| 1178 | if (x->encap) |
| 1179 | l += RTA_SPACE(sizeof(*x->encap)); |
| 1180 | |
| 1181 | return l; |
| 1182 | } |
| 1183 | |
| 1184 | static int xfrm_notify_sa(struct xfrm_state *x, struct km_event *c) |
| 1185 | { |
| 1186 | struct xfrm_usersa_info *p; |
Herbert Xu | 0603eac | 2005-06-18 22:54:36 -0700 | [diff] [blame^] | 1187 | struct xfrm_usersa_id *id; |
Jamal Hadi Salim | 26b15da | 2005-06-18 22:42:13 -0700 | [diff] [blame] | 1188 | struct nlmsghdr *nlh; |
| 1189 | struct sk_buff *skb; |
Jamal Hadi Salim | 26b15da | 2005-06-18 22:42:13 -0700 | [diff] [blame] | 1190 | unsigned char *b; |
| 1191 | int len = xfrm_sa_len(x); |
Herbert Xu | 0603eac | 2005-06-18 22:54:36 -0700 | [diff] [blame^] | 1192 | int headlen; |
| 1193 | |
| 1194 | headlen = sizeof(*p); |
| 1195 | if (c->event == XFRM_MSG_DELSA) { |
| 1196 | len += RTA_SPACE(headlen); |
| 1197 | headlen = sizeof(*id); |
| 1198 | } |
| 1199 | len += NLMSG_SPACE(headlen); |
Jamal Hadi Salim | 26b15da | 2005-06-18 22:42:13 -0700 | [diff] [blame] | 1200 | |
| 1201 | skb = alloc_skb(len, GFP_ATOMIC); |
| 1202 | if (skb == NULL) |
| 1203 | return -ENOMEM; |
| 1204 | b = skb->tail; |
| 1205 | |
Herbert Xu | 0603eac | 2005-06-18 22:54:36 -0700 | [diff] [blame^] | 1206 | nlh = NLMSG_PUT(skb, c->pid, c->seq, c->event, headlen); |
Jamal Hadi Salim | 26b15da | 2005-06-18 22:42:13 -0700 | [diff] [blame] | 1207 | nlh->nlmsg_flags = 0; |
| 1208 | |
| 1209 | p = NLMSG_DATA(nlh); |
Herbert Xu | 0603eac | 2005-06-18 22:54:36 -0700 | [diff] [blame^] | 1210 | if (c->event == XFRM_MSG_DELSA) { |
| 1211 | id = NLMSG_DATA(nlh); |
| 1212 | memcpy(&id->daddr, &x->id.daddr, sizeof(id->daddr)); |
| 1213 | id->spi = x->id.spi; |
| 1214 | id->family = x->props.family; |
| 1215 | id->proto = x->id.proto; |
| 1216 | |
| 1217 | p = RTA_DATA(__RTA_PUT(skb, XFRMA_SA, sizeof(*p))); |
| 1218 | } |
| 1219 | |
Jamal Hadi Salim | 26b15da | 2005-06-18 22:42:13 -0700 | [diff] [blame] | 1220 | copy_to_user_state(x, p); |
| 1221 | |
| 1222 | if (x->aalg) |
| 1223 | RTA_PUT(skb, XFRMA_ALG_AUTH, |
| 1224 | sizeof(*(x->aalg))+(x->aalg->alg_key_len+7)/8, x->aalg); |
| 1225 | if (x->ealg) |
| 1226 | RTA_PUT(skb, XFRMA_ALG_CRYPT, |
| 1227 | sizeof(*(x->ealg))+(x->ealg->alg_key_len+7)/8, x->ealg); |
| 1228 | if (x->calg) |
| 1229 | RTA_PUT(skb, XFRMA_ALG_COMP, sizeof(*(x->calg)), x->calg); |
| 1230 | |
| 1231 | if (x->encap) |
| 1232 | RTA_PUT(skb, XFRMA_ENCAP, sizeof(*x->encap), x->encap); |
| 1233 | |
| 1234 | nlh->nlmsg_len = skb->tail - b; |
| 1235 | |
| 1236 | return netlink_broadcast(xfrm_nl, skb, 0, XFRMGRP_SA, GFP_ATOMIC); |
| 1237 | |
| 1238 | nlmsg_failure: |
| 1239 | rtattr_failure: |
| 1240 | kfree_skb(skb); |
| 1241 | return -1; |
| 1242 | } |
| 1243 | |
| 1244 | static int xfrm_send_state_notify(struct xfrm_state *x, struct km_event *c) |
| 1245 | { |
| 1246 | |
| 1247 | switch (c->event) { |
Herbert Xu | f60f6b8 | 2005-06-18 22:44:37 -0700 | [diff] [blame] | 1248 | case XFRM_MSG_EXPIRE: |
Jamal Hadi Salim | 26b15da | 2005-06-18 22:42:13 -0700 | [diff] [blame] | 1249 | return xfrm_exp_state_notify(x, c); |
Herbert Xu | f60f6b8 | 2005-06-18 22:44:37 -0700 | [diff] [blame] | 1250 | case XFRM_MSG_DELSA: |
| 1251 | case XFRM_MSG_UPDSA: |
| 1252 | case XFRM_MSG_NEWSA: |
Jamal Hadi Salim | 26b15da | 2005-06-18 22:42:13 -0700 | [diff] [blame] | 1253 | return xfrm_notify_sa(x, c); |
Herbert Xu | f60f6b8 | 2005-06-18 22:44:37 -0700 | [diff] [blame] | 1254 | case XFRM_MSG_FLUSHSA: |
Jamal Hadi Salim | 26b15da | 2005-06-18 22:42:13 -0700 | [diff] [blame] | 1255 | return xfrm_notify_sa_flush(c); |
| 1256 | default: |
| 1257 | printk("xfrm_user: Unknown SA event %d\n", c->event); |
| 1258 | break; |
| 1259 | } |
| 1260 | |
| 1261 | return 0; |
| 1262 | |
| 1263 | } |
| 1264 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1265 | static int build_acquire(struct sk_buff *skb, struct xfrm_state *x, |
| 1266 | struct xfrm_tmpl *xt, struct xfrm_policy *xp, |
| 1267 | int dir) |
| 1268 | { |
| 1269 | struct xfrm_user_acquire *ua; |
| 1270 | struct nlmsghdr *nlh; |
| 1271 | unsigned char *b = skb->tail; |
| 1272 | __u32 seq = xfrm_get_acqseq(); |
| 1273 | |
| 1274 | nlh = NLMSG_PUT(skb, 0, 0, XFRM_MSG_ACQUIRE, |
| 1275 | sizeof(*ua)); |
| 1276 | ua = NLMSG_DATA(nlh); |
| 1277 | nlh->nlmsg_flags = 0; |
| 1278 | |
| 1279 | memcpy(&ua->id, &x->id, sizeof(ua->id)); |
| 1280 | memcpy(&ua->saddr, &x->props.saddr, sizeof(ua->saddr)); |
| 1281 | memcpy(&ua->sel, &x->sel, sizeof(ua->sel)); |
| 1282 | copy_to_user_policy(xp, &ua->policy, dir); |
| 1283 | ua->aalgos = xt->aalgos; |
| 1284 | ua->ealgos = xt->ealgos; |
| 1285 | ua->calgos = xt->calgos; |
| 1286 | ua->seq = x->km.seq = seq; |
| 1287 | |
| 1288 | if (copy_to_user_tmpl(xp, skb) < 0) |
| 1289 | goto nlmsg_failure; |
| 1290 | |
| 1291 | nlh->nlmsg_len = skb->tail - b; |
| 1292 | return skb->len; |
| 1293 | |
| 1294 | nlmsg_failure: |
| 1295 | skb_trim(skb, b - skb->data); |
| 1296 | return -1; |
| 1297 | } |
| 1298 | |
| 1299 | static int xfrm_send_acquire(struct xfrm_state *x, struct xfrm_tmpl *xt, |
| 1300 | struct xfrm_policy *xp, int dir) |
| 1301 | { |
| 1302 | struct sk_buff *skb; |
| 1303 | size_t len; |
| 1304 | |
| 1305 | len = RTA_SPACE(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr); |
| 1306 | len += NLMSG_SPACE(sizeof(struct xfrm_user_acquire)); |
| 1307 | skb = alloc_skb(len, GFP_ATOMIC); |
| 1308 | if (skb == NULL) |
| 1309 | return -ENOMEM; |
| 1310 | |
| 1311 | if (build_acquire(skb, x, xt, xp, dir) < 0) |
| 1312 | BUG(); |
| 1313 | |
| 1314 | NETLINK_CB(skb).dst_groups = XFRMGRP_ACQUIRE; |
| 1315 | |
| 1316 | return netlink_broadcast(xfrm_nl, skb, 0, XFRMGRP_ACQUIRE, GFP_ATOMIC); |
| 1317 | } |
| 1318 | |
| 1319 | /* User gives us xfrm_user_policy_info followed by an array of 0 |
| 1320 | * or more templates. |
| 1321 | */ |
| 1322 | static struct xfrm_policy *xfrm_compile_policy(u16 family, int opt, |
| 1323 | u8 *data, int len, int *dir) |
| 1324 | { |
| 1325 | struct xfrm_userpolicy_info *p = (struct xfrm_userpolicy_info *)data; |
| 1326 | struct xfrm_user_tmpl *ut = (struct xfrm_user_tmpl *) (p + 1); |
| 1327 | struct xfrm_policy *xp; |
| 1328 | int nr; |
| 1329 | |
| 1330 | switch (family) { |
| 1331 | case AF_INET: |
| 1332 | if (opt != IP_XFRM_POLICY) { |
| 1333 | *dir = -EOPNOTSUPP; |
| 1334 | return NULL; |
| 1335 | } |
| 1336 | break; |
| 1337 | #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) |
| 1338 | case AF_INET6: |
| 1339 | if (opt != IPV6_XFRM_POLICY) { |
| 1340 | *dir = -EOPNOTSUPP; |
| 1341 | return NULL; |
| 1342 | } |
| 1343 | break; |
| 1344 | #endif |
| 1345 | default: |
| 1346 | *dir = -EINVAL; |
| 1347 | return NULL; |
| 1348 | } |
| 1349 | |
| 1350 | *dir = -EINVAL; |
| 1351 | |
| 1352 | if (len < sizeof(*p) || |
| 1353 | verify_newpolicy_info(p)) |
| 1354 | return NULL; |
| 1355 | |
| 1356 | nr = ((len - sizeof(*p)) / sizeof(*ut)); |
| 1357 | if (nr > XFRM_MAX_DEPTH) |
| 1358 | return NULL; |
| 1359 | |
| 1360 | xp = xfrm_policy_alloc(GFP_KERNEL); |
| 1361 | if (xp == NULL) { |
| 1362 | *dir = -ENOBUFS; |
| 1363 | return NULL; |
| 1364 | } |
| 1365 | |
| 1366 | copy_from_user_policy(xp, p); |
| 1367 | copy_templates(xp, ut, nr); |
| 1368 | |
| 1369 | *dir = p->dir; |
| 1370 | |
| 1371 | return xp; |
| 1372 | } |
| 1373 | |
| 1374 | static int build_polexpire(struct sk_buff *skb, struct xfrm_policy *xp, |
| 1375 | int dir, int hard) |
| 1376 | { |
| 1377 | struct xfrm_user_polexpire *upe; |
| 1378 | struct nlmsghdr *nlh; |
| 1379 | unsigned char *b = skb->tail; |
| 1380 | |
| 1381 | nlh = NLMSG_PUT(skb, 0, 0, XFRM_MSG_POLEXPIRE, sizeof(*upe)); |
| 1382 | upe = NLMSG_DATA(nlh); |
| 1383 | nlh->nlmsg_flags = 0; |
| 1384 | |
| 1385 | copy_to_user_policy(xp, &upe->pol, dir); |
| 1386 | if (copy_to_user_tmpl(xp, skb) < 0) |
| 1387 | goto nlmsg_failure; |
| 1388 | upe->hard = !!hard; |
| 1389 | |
| 1390 | nlh->nlmsg_len = skb->tail - b; |
| 1391 | return skb->len; |
| 1392 | |
| 1393 | nlmsg_failure: |
| 1394 | skb_trim(skb, b - skb->data); |
| 1395 | return -1; |
| 1396 | } |
| 1397 | |
Jamal Hadi Salim | 26b15da | 2005-06-18 22:42:13 -0700 | [diff] [blame] | 1398 | static int xfrm_exp_policy_notify(struct xfrm_policy *xp, int dir, struct km_event *c) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1399 | { |
| 1400 | struct sk_buff *skb; |
| 1401 | size_t len; |
| 1402 | |
| 1403 | len = RTA_SPACE(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr); |
| 1404 | len += NLMSG_SPACE(sizeof(struct xfrm_user_polexpire)); |
| 1405 | skb = alloc_skb(len, GFP_ATOMIC); |
| 1406 | if (skb == NULL) |
| 1407 | return -ENOMEM; |
| 1408 | |
Herbert Xu | bf08867f9 | 2005-06-18 22:44:00 -0700 | [diff] [blame] | 1409 | if (build_polexpire(skb, xp, dir, c->data.hard) < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1410 | BUG(); |
| 1411 | |
| 1412 | NETLINK_CB(skb).dst_groups = XFRMGRP_EXPIRE; |
| 1413 | |
| 1414 | return netlink_broadcast(xfrm_nl, skb, 0, XFRMGRP_EXPIRE, GFP_ATOMIC); |
| 1415 | } |
| 1416 | |
Jamal Hadi Salim | 26b15da | 2005-06-18 22:42:13 -0700 | [diff] [blame] | 1417 | static int xfrm_notify_policy(struct xfrm_policy *xp, int dir, struct km_event *c) |
| 1418 | { |
| 1419 | struct xfrm_userpolicy_info *p; |
Herbert Xu | 0603eac | 2005-06-18 22:54:36 -0700 | [diff] [blame^] | 1420 | struct xfrm_userpolicy_id *id; |
Jamal Hadi Salim | 26b15da | 2005-06-18 22:42:13 -0700 | [diff] [blame] | 1421 | struct nlmsghdr *nlh; |
| 1422 | struct sk_buff *skb; |
Jamal Hadi Salim | 26b15da | 2005-06-18 22:42:13 -0700 | [diff] [blame] | 1423 | unsigned char *b; |
| 1424 | int len = RTA_SPACE(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr); |
Herbert Xu | 0603eac | 2005-06-18 22:54:36 -0700 | [diff] [blame^] | 1425 | int headlen; |
| 1426 | |
| 1427 | headlen = sizeof(*p); |
| 1428 | if (c->event == XFRM_MSG_DELPOLICY) { |
| 1429 | len += RTA_SPACE(headlen); |
| 1430 | headlen = sizeof(*id); |
| 1431 | } |
| 1432 | len += NLMSG_SPACE(headlen); |
Jamal Hadi Salim | 26b15da | 2005-06-18 22:42:13 -0700 | [diff] [blame] | 1433 | |
| 1434 | skb = alloc_skb(len, GFP_ATOMIC); |
| 1435 | if (skb == NULL) |
| 1436 | return -ENOMEM; |
| 1437 | b = skb->tail; |
| 1438 | |
Herbert Xu | 0603eac | 2005-06-18 22:54:36 -0700 | [diff] [blame^] | 1439 | nlh = NLMSG_PUT(skb, c->pid, c->seq, c->event, headlen); |
Jamal Hadi Salim | 26b15da | 2005-06-18 22:42:13 -0700 | [diff] [blame] | 1440 | |
| 1441 | p = NLMSG_DATA(nlh); |
Herbert Xu | 0603eac | 2005-06-18 22:54:36 -0700 | [diff] [blame^] | 1442 | if (c->event == XFRM_MSG_DELPOLICY) { |
| 1443 | id = NLMSG_DATA(nlh); |
| 1444 | memset(id, 0, sizeof(*id)); |
| 1445 | id->dir = dir; |
| 1446 | if (c->data.byid) |
| 1447 | id->index = xp->index; |
| 1448 | else |
| 1449 | memcpy(&id->sel, &xp->selector, sizeof(id->sel)); |
| 1450 | |
| 1451 | p = RTA_DATA(__RTA_PUT(skb, XFRMA_POLICY, sizeof(*p))); |
| 1452 | } |
Jamal Hadi Salim | 26b15da | 2005-06-18 22:42:13 -0700 | [diff] [blame] | 1453 | |
| 1454 | nlh->nlmsg_flags = 0; |
| 1455 | |
| 1456 | copy_to_user_policy(xp, p, dir); |
| 1457 | if (copy_to_user_tmpl(xp, skb) < 0) |
| 1458 | goto nlmsg_failure; |
| 1459 | |
| 1460 | nlh->nlmsg_len = skb->tail - b; |
| 1461 | |
| 1462 | return netlink_broadcast(xfrm_nl, skb, 0, XFRMGRP_POLICY, GFP_ATOMIC); |
| 1463 | |
| 1464 | nlmsg_failure: |
Herbert Xu | 0603eac | 2005-06-18 22:54:36 -0700 | [diff] [blame^] | 1465 | rtattr_failure: |
Jamal Hadi Salim | 26b15da | 2005-06-18 22:42:13 -0700 | [diff] [blame] | 1466 | kfree_skb(skb); |
| 1467 | return -1; |
| 1468 | } |
| 1469 | |
| 1470 | static int xfrm_notify_policy_flush(struct km_event *c) |
| 1471 | { |
| 1472 | struct nlmsghdr *nlh; |
| 1473 | struct sk_buff *skb; |
| 1474 | unsigned char *b; |
| 1475 | int len = NLMSG_LENGTH(0); |
| 1476 | |
| 1477 | skb = alloc_skb(len, GFP_ATOMIC); |
| 1478 | if (skb == NULL) |
| 1479 | return -ENOMEM; |
| 1480 | b = skb->tail; |
| 1481 | |
| 1482 | |
| 1483 | nlh = NLMSG_PUT(skb, c->pid, c->seq, XFRM_MSG_FLUSHPOLICY, 0); |
| 1484 | |
| 1485 | nlh->nlmsg_len = skb->tail - b; |
| 1486 | |
| 1487 | return netlink_broadcast(xfrm_nl, skb, 0, XFRMGRP_POLICY, GFP_ATOMIC); |
| 1488 | |
| 1489 | nlmsg_failure: |
| 1490 | kfree_skb(skb); |
| 1491 | return -1; |
| 1492 | } |
| 1493 | |
| 1494 | static int xfrm_send_policy_notify(struct xfrm_policy *xp, int dir, struct km_event *c) |
| 1495 | { |
| 1496 | |
| 1497 | switch (c->event) { |
Herbert Xu | f60f6b8 | 2005-06-18 22:44:37 -0700 | [diff] [blame] | 1498 | case XFRM_MSG_NEWPOLICY: |
| 1499 | case XFRM_MSG_UPDPOLICY: |
| 1500 | case XFRM_MSG_DELPOLICY: |
Jamal Hadi Salim | 26b15da | 2005-06-18 22:42:13 -0700 | [diff] [blame] | 1501 | return xfrm_notify_policy(xp, dir, c); |
Herbert Xu | f60f6b8 | 2005-06-18 22:44:37 -0700 | [diff] [blame] | 1502 | case XFRM_MSG_FLUSHPOLICY: |
Jamal Hadi Salim | 26b15da | 2005-06-18 22:42:13 -0700 | [diff] [blame] | 1503 | return xfrm_notify_policy_flush(c); |
Herbert Xu | f60f6b8 | 2005-06-18 22:44:37 -0700 | [diff] [blame] | 1504 | case XFRM_MSG_POLEXPIRE: |
Jamal Hadi Salim | 26b15da | 2005-06-18 22:42:13 -0700 | [diff] [blame] | 1505 | return xfrm_exp_policy_notify(xp, dir, c); |
| 1506 | default: |
| 1507 | printk("xfrm_user: Unknown Policy event %d\n", c->event); |
| 1508 | } |
| 1509 | |
| 1510 | return 0; |
| 1511 | |
| 1512 | } |
| 1513 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1514 | static struct xfrm_mgr netlink_mgr = { |
| 1515 | .id = "netlink", |
| 1516 | .notify = xfrm_send_state_notify, |
| 1517 | .acquire = xfrm_send_acquire, |
| 1518 | .compile_policy = xfrm_compile_policy, |
| 1519 | .notify_policy = xfrm_send_policy_notify, |
| 1520 | }; |
| 1521 | |
| 1522 | static int __init xfrm_user_init(void) |
| 1523 | { |
| 1524 | printk(KERN_INFO "Initializing IPsec netlink socket\n"); |
| 1525 | |
| 1526 | xfrm_nl = netlink_kernel_create(NETLINK_XFRM, xfrm_netlink_rcv); |
| 1527 | if (xfrm_nl == NULL) |
| 1528 | return -ENOMEM; |
| 1529 | |
| 1530 | xfrm_register_km(&netlink_mgr); |
| 1531 | |
| 1532 | return 0; |
| 1533 | } |
| 1534 | |
| 1535 | static void __exit xfrm_user_exit(void) |
| 1536 | { |
| 1537 | xfrm_unregister_km(&netlink_mgr); |
| 1538 | sock_release(xfrm_nl->sk_socket); |
| 1539 | } |
| 1540 | |
| 1541 | module_init(xfrm_user_init); |
| 1542 | module_exit(xfrm_user_exit); |
| 1543 | MODULE_LICENSE("GPL"); |