1 /*- 2 * Copyright (c) 2016 Yandex LLC 3 * Copyright (c) 2016 Andrey V. Elsukov <ae@FreeBSD.org> 4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 */ 27 28 #include <sys/cdefs.h> 29 __FBSDID("$FreeBSD$"); 30 31 #include <sys/param.h> 32 #include <sys/systm.h> 33 #include <sys/counter.h> 34 #include <sys/errno.h> 35 #include <sys/kernel.h> 36 #include <sys/lock.h> 37 #include <sys/malloc.h> 38 #include <sys/mbuf.h> 39 #include <sys/module.h> 40 #include <sys/rmlock.h> 41 #include <sys/rwlock.h> 42 #include <sys/socket.h> 43 #include <sys/queue.h> 44 #include <sys/syslog.h> 45 #include <sys/sysctl.h> 46 47 #include <net/if.h> 48 #include <net/if_var.h> 49 #include <net/netisr.h> 50 #include <net/pfil.h> 51 #include <net/vnet.h> 52 53 #include <netinet/in.h> 54 #include <netinet/ip_var.h> 55 #include <netinet/ip_fw.h> 56 #include <netinet/ip6.h> 57 #include <netinet/icmp6.h> 58 #include <netinet6/in6_var.h> 59 #include <netinet6/ip6_var.h> 60 61 #include <netpfil/ipfw/ip_fw_private.h> 62 #include <netpfil/ipfw/nptv6/nptv6.h> 63 64 static VNET_DEFINE(uint16_t, nptv6_eid) = 0; 65 #define V_nptv6_eid VNET(nptv6_eid) 66 #define IPFW_TLV_NPTV6_NAME IPFW_TLV_EACTION_NAME(V_nptv6_eid) 67 68 static struct nptv6_cfg *nptv6_alloc_config(const char *name, uint8_t set); 69 static void nptv6_free_config(struct nptv6_cfg *cfg); 70 static struct nptv6_cfg *nptv6_find(struct namedobj_instance *ni, 71 const char *name, uint8_t set); 72 static int nptv6_rewrite_internal(struct nptv6_cfg *cfg, struct mbuf **mp, 73 int offset); 74 static int nptv6_rewrite_external(struct nptv6_cfg *cfg, struct mbuf **mp, 75 int offset); 76 77 #define NPTV6_LOOKUP(chain, cmd) \ 78 (struct nptv6_cfg *)SRV_OBJECT((chain), (cmd)->arg1) 79 80 #ifndef IN6_MASK_ADDR 81 #define IN6_MASK_ADDR(a, m) do { \ 82 (a)->s6_addr32[0] &= (m)->s6_addr32[0]; \ 83 (a)->s6_addr32[1] &= (m)->s6_addr32[1]; \ 84 (a)->s6_addr32[2] &= (m)->s6_addr32[2]; \ 85 (a)->s6_addr32[3] &= (m)->s6_addr32[3]; \ 86 } while (0) 87 #endif 88 #ifndef IN6_ARE_MASKED_ADDR_EQUAL 89 #define IN6_ARE_MASKED_ADDR_EQUAL(d, a, m) ( \ 90 (((d)->s6_addr32[0] ^ (a)->s6_addr32[0]) & (m)->s6_addr32[0]) == 0 && \ 91 (((d)->s6_addr32[1] ^ (a)->s6_addr32[1]) & (m)->s6_addr32[1]) == 0 && \ 92 (((d)->s6_addr32[2] ^ (a)->s6_addr32[2]) & (m)->s6_addr32[2]) == 0 && \ 93 (((d)->s6_addr32[3] ^ (a)->s6_addr32[3]) & (m)->s6_addr32[3]) == 0 ) 94 #endif 95 96 #if 0 97 #define NPTV6_DEBUG(fmt, ...) do { \ 98 printf("%s: " fmt "\n", __func__, ## __VA_ARGS__); \ 99 } while (0) 100 #define NPTV6_IPDEBUG(fmt, ...) do { \ 101 char _s[INET6_ADDRSTRLEN], _d[INET6_ADDRSTRLEN]; \ 102 printf("%s: " fmt "\n", __func__, ## __VA_ARGS__); \ 103 } while (0) 104 #else 105 #define NPTV6_DEBUG(fmt, ...) 106 #define NPTV6_IPDEBUG(fmt, ...) 107 #endif 108 109 static int 110 nptv6_getlasthdr(struct nptv6_cfg *cfg, struct mbuf *m, int *offset) 111 { 112 struct ip6_hdr *ip6; 113 struct ip6_hbh *hbh; 114 int proto, hlen; 115 116 hlen = (offset == NULL) ? 0: *offset; 117 if (m->m_len < hlen) 118 return (-1); 119 ip6 = mtodo(m, hlen); 120 hlen += sizeof(*ip6); 121 proto = ip6->ip6_nxt; 122 while (proto == IPPROTO_HOPOPTS || proto == IPPROTO_ROUTING || 123 proto == IPPROTO_DSTOPTS) { 124 hbh = mtodo(m, hlen); 125 if (m->m_len < hlen) 126 return (-1); 127 proto = hbh->ip6h_nxt; 128 hlen += hbh->ip6h_len << 3; 129 } 130 if (offset != NULL) 131 *offset = hlen; 132 return (proto); 133 } 134 135 static int 136 nptv6_translate_icmpv6(struct nptv6_cfg *cfg, struct mbuf **mp, int offset) 137 { 138 struct icmp6_hdr *icmp6; 139 struct ip6_hdr *ip6; 140 struct mbuf *m; 141 142 m = *mp; 143 if (offset > m->m_len) 144 return (-1); 145 icmp6 = mtodo(m, offset); 146 NPTV6_DEBUG("ICMPv6 type %d", icmp6->icmp6_type); 147 switch (icmp6->icmp6_type) { 148 case ICMP6_DST_UNREACH: 149 case ICMP6_PACKET_TOO_BIG: 150 case ICMP6_TIME_EXCEEDED: 151 case ICMP6_PARAM_PROB: 152 break; 153 case ICMP6_ECHO_REQUEST: 154 case ICMP6_ECHO_REPLY: 155 /* nothing to translate */ 156 return (0); 157 default: 158 /* 159 * XXX: We can add some checks to not translate NDP and MLD 160 * messages. Currently user must explicitly allow these message 161 * types, otherwise packets will be dropped. 162 */ 163 return (-1); 164 } 165 offset += sizeof(*icmp6); 166 if (offset + sizeof(*ip6) > m->m_pkthdr.len) 167 return (-1); 168 if (offset + sizeof(*ip6) > m->m_len) 169 *mp = m = m_pullup(m, offset + sizeof(*ip6)); 170 if (m == NULL) 171 return (-1); 172 ip6 = mtodo(m, offset); 173 NPTV6_IPDEBUG("offset %d, %s -> %s %d", offset, 174 inet_ntop(AF_INET6, &ip6->ip6_src, _s, sizeof(_s)), 175 inet_ntop(AF_INET6, &ip6->ip6_dst, _d, sizeof(_d)), 176 ip6->ip6_nxt); 177 if (IN6_ARE_MASKED_ADDR_EQUAL(&ip6->ip6_src, 178 &cfg->external, &cfg->mask)) 179 return (nptv6_rewrite_external(cfg, mp, offset)); 180 else if (IN6_ARE_MASKED_ADDR_EQUAL(&ip6->ip6_dst, 181 &cfg->internal, &cfg->mask)) 182 return (nptv6_rewrite_internal(cfg, mp, offset)); 183 /* 184 * Addresses in the inner IPv6 header doesn't matched to 185 * our prefixes. 186 */ 187 return (-1); 188 } 189 190 static int 191 nptv6_search_index(struct nptv6_cfg *cfg, struct in6_addr *a) 192 { 193 int idx; 194 195 if (cfg->flags & NPTV6_48PLEN) 196 return (3); 197 198 /* Search suitable word index for adjustment */ 199 for (idx = 4; idx < 8; idx++) 200 if (a->s6_addr16[idx] != 0xffff) 201 break; 202 /* 203 * RFC 6296 p3.7: If an NPTv6 Translator discovers a datagram with 204 * an IID of all-zeros while performing address mapping, that 205 * datagram MUST be dropped, and an ICMPv6 Parameter Problem error 206 * SHOULD be generated. 207 */ 208 if (idx == 8 || 209 (a->s6_addr32[2] == 0 && a->s6_addr32[3] == 0)) 210 return (-1); 211 return (idx); 212 } 213 214 static void 215 nptv6_copy_addr(struct in6_addr *src, struct in6_addr *dst, 216 struct in6_addr *mask) 217 { 218 int i; 219 220 for (i = 0; i < 8 && mask->s6_addr8[i] != 0; i++) { 221 dst->s6_addr8[i] &= ~mask->s6_addr8[i]; 222 dst->s6_addr8[i] |= src->s6_addr8[i] & mask->s6_addr8[i]; 223 } 224 } 225 226 static int 227 nptv6_rewrite_internal(struct nptv6_cfg *cfg, struct mbuf **mp, int offset) 228 { 229 struct in6_addr *addr; 230 struct ip6_hdr *ip6; 231 int idx, proto; 232 uint16_t adj; 233 234 ip6 = mtodo(*mp, offset); 235 NPTV6_IPDEBUG("offset %d, %s -> %s %d", offset, 236 inet_ntop(AF_INET6, &ip6->ip6_src, _s, sizeof(_s)), 237 inet_ntop(AF_INET6, &ip6->ip6_dst, _d, sizeof(_d)), 238 ip6->ip6_nxt); 239 if (offset == 0) 240 addr = &ip6->ip6_src; 241 else { 242 /* 243 * When we rewriting inner IPv6 header, we need to rewrite 244 * destination address back to external prefix. The datagram in 245 * the ICMPv6 payload should looks like it was send from 246 * external prefix. 247 */ 248 addr = &ip6->ip6_dst; 249 } 250 idx = nptv6_search_index(cfg, addr); 251 if (idx < 0) { 252 /* 253 * Do not send ICMPv6 error when offset isn't zero. 254 * This means we are rewriting inner IPv6 header in the 255 * ICMPv6 error message. 256 */ 257 if (offset == 0) { 258 icmp6_error2(*mp, ICMP6_DST_UNREACH, 259 ICMP6_DST_UNREACH_ADDR, 0, (*mp)->m_pkthdr.rcvif); 260 *mp = NULL; 261 } 262 return (IP_FW_DENY); 263 } 264 adj = addr->s6_addr16[idx]; 265 nptv6_copy_addr(&cfg->external, addr, &cfg->mask); 266 adj = cksum_add(adj, cfg->adjustment); 267 if (adj == 0xffff) 268 adj = 0; 269 addr->s6_addr16[idx] = adj; 270 if (offset == 0) { 271 /* 272 * We may need to translate addresses in the inner IPv6 273 * header for ICMPv6 error messages. 274 */ 275 proto = nptv6_getlasthdr(cfg, *mp, &offset); 276 if (proto < 0 || (proto == IPPROTO_ICMPV6 && 277 nptv6_translate_icmpv6(cfg, mp, offset) != 0)) 278 return (IP_FW_DENY); 279 NPTV6STAT_INC(cfg, in2ex); 280 } 281 return (0); 282 } 283 284 static int 285 nptv6_rewrite_external(struct nptv6_cfg *cfg, struct mbuf **mp, int offset) 286 { 287 struct in6_addr *addr; 288 struct ip6_hdr *ip6; 289 int idx, proto; 290 uint16_t adj; 291 292 ip6 = mtodo(*mp, offset); 293 NPTV6_IPDEBUG("offset %d, %s -> %s %d", offset, 294 inet_ntop(AF_INET6, &ip6->ip6_src, _s, sizeof(_s)), 295 inet_ntop(AF_INET6, &ip6->ip6_dst, _d, sizeof(_d)), 296 ip6->ip6_nxt); 297 if (offset == 0) 298 addr = &ip6->ip6_dst; 299 else { 300 /* 301 * When we rewriting inner IPv6 header, we need to rewrite 302 * source address back to internal prefix. The datagram in 303 * the ICMPv6 payload should looks like it was send from 304 * internal prefix. 305 */ 306 addr = &ip6->ip6_src; 307 } 308 idx = nptv6_search_index(cfg, addr); 309 if (idx < 0) { 310 /* 311 * Do not send ICMPv6 error when offset isn't zero. 312 * This means we are rewriting inner IPv6 header in the 313 * ICMPv6 error message. 314 */ 315 if (offset == 0) { 316 icmp6_error2(*mp, ICMP6_DST_UNREACH, 317 ICMP6_DST_UNREACH_ADDR, 0, (*mp)->m_pkthdr.rcvif); 318 *mp = NULL; 319 } 320 return (IP_FW_DENY); 321 } 322 adj = addr->s6_addr16[idx]; 323 nptv6_copy_addr(&cfg->internal, addr, &cfg->mask); 324 adj = cksum_add(adj, ~cfg->adjustment); 325 if (adj == 0xffff) 326 adj = 0; 327 addr->s6_addr16[idx] = adj; 328 if (offset == 0) { 329 /* 330 * We may need to translate addresses in the inner IPv6 331 * header for ICMPv6 error messages. 332 */ 333 proto = nptv6_getlasthdr(cfg, *mp, &offset); 334 if (proto < 0 || (proto == IPPROTO_ICMPV6 && 335 nptv6_translate_icmpv6(cfg, mp, offset) != 0)) 336 return (IP_FW_DENY); 337 NPTV6STAT_INC(cfg, ex2in); 338 } 339 return (0); 340 } 341 342 /* 343 * ipfw external action handler. 344 */ 345 static int 346 ipfw_nptv6(struct ip_fw_chain *chain, struct ip_fw_args *args, 347 ipfw_insn *cmd, int *done) 348 { 349 struct ip6_hdr *ip6; 350 struct nptv6_cfg *cfg; 351 ipfw_insn *icmd; 352 int ret; 353 354 *done = 0; /* try next rule if not matched */ 355 icmd = cmd + 1; 356 if (cmd->opcode != O_EXTERNAL_ACTION || 357 cmd->arg1 != V_nptv6_eid || 358 icmd->opcode != O_EXTERNAL_INSTANCE || 359 (cfg = NPTV6_LOOKUP(chain, icmd)) == NULL) 360 return (0); 361 /* 362 * We need act as router, so when forwarding is disabled - 363 * do nothing. 364 */ 365 if (V_ip6_forwarding == 0 || args->f_id.addr_type != 6) 366 return (0); 367 /* 368 * NOTE: we expect ipfw_chk() did m_pullup() up to upper level 369 * protocol's headers. Also we skip some checks, that ip6_input(), 370 * ip6_forward(), ip6_fastfwd() and ipfw_chk() already did. 371 */ 372 ret = IP_FW_DENY; 373 ip6 = mtod(args->m, struct ip6_hdr *); 374 NPTV6_IPDEBUG("eid %u, oid %u, %s -> %s %d", 375 cmd->arg1, icmd->arg1, 376 inet_ntop(AF_INET6, &ip6->ip6_src, _s, sizeof(_s)), 377 inet_ntop(AF_INET6, &ip6->ip6_dst, _d, sizeof(_d)), 378 ip6->ip6_nxt); 379 if (IN6_ARE_MASKED_ADDR_EQUAL(&ip6->ip6_src, 380 &cfg->internal, &cfg->mask)) { 381 /* 382 * XXX: Do not translate packets when both src and dst 383 * are from internal prefix. 384 */ 385 if (IN6_ARE_MASKED_ADDR_EQUAL(&ip6->ip6_dst, 386 &cfg->internal, &cfg->mask)) 387 return (0); 388 ret = nptv6_rewrite_internal(cfg, &args->m, 0); 389 } else if (IN6_ARE_MASKED_ADDR_EQUAL(&ip6->ip6_dst, 390 &cfg->external, &cfg->mask)) 391 ret = nptv6_rewrite_external(cfg, &args->m, 0); 392 else 393 return (0); 394 /* 395 * If address wasn't rewrited - free mbuf. 396 */ 397 if (ret != 0) { 398 if (args->m != NULL) { 399 m_freem(args->m); 400 args->m = NULL; /* mark mbuf as consumed */ 401 } 402 NPTV6STAT_INC(cfg, dropped); 403 } 404 /* Terminate the search if one_pass is set */ 405 *done = V_fw_one_pass; 406 /* Update args->f_id when one_pass is off */ 407 if (*done == 0 && ret == 0) { 408 ip6 = mtod(args->m, struct ip6_hdr *); 409 args->f_id.src_ip6 = ip6->ip6_src; 410 args->f_id.dst_ip6 = ip6->ip6_dst; 411 } 412 return (ret); 413 } 414 415 static struct nptv6_cfg * 416 nptv6_alloc_config(const char *name, uint8_t set) 417 { 418 struct nptv6_cfg *cfg; 419 420 cfg = malloc(sizeof(struct nptv6_cfg), M_IPFW, M_WAITOK | M_ZERO); 421 COUNTER_ARRAY_ALLOC(cfg->stats, NPTV6STATS, M_WAITOK); 422 cfg->no.name = cfg->name; 423 cfg->no.etlv = IPFW_TLV_NPTV6_NAME; 424 cfg->no.set = set; 425 strlcpy(cfg->name, name, sizeof(cfg->name)); 426 return (cfg); 427 } 428 429 static void 430 nptv6_free_config(struct nptv6_cfg *cfg) 431 { 432 433 COUNTER_ARRAY_FREE(cfg->stats, NPTV6STATS); 434 free(cfg, M_IPFW); 435 } 436 437 static void 438 nptv6_export_config(struct ip_fw_chain *ch, struct nptv6_cfg *cfg, 439 ipfw_nptv6_cfg *uc) 440 { 441 442 uc->internal = cfg->internal; 443 uc->external = cfg->external; 444 uc->plen = cfg->plen; 445 uc->flags = cfg->flags & NPTV6_FLAGSMASK; 446 uc->set = cfg->no.set; 447 strlcpy(uc->name, cfg->no.name, sizeof(uc->name)); 448 } 449 450 struct nptv6_dump_arg { 451 struct ip_fw_chain *ch; 452 struct sockopt_data *sd; 453 }; 454 455 static int 456 export_config_cb(struct namedobj_instance *ni, struct named_object *no, 457 void *arg) 458 { 459 struct nptv6_dump_arg *da = (struct nptv6_dump_arg *)arg; 460 ipfw_nptv6_cfg *uc; 461 462 uc = (ipfw_nptv6_cfg *)ipfw_get_sopt_space(da->sd, sizeof(*uc)); 463 nptv6_export_config(da->ch, (struct nptv6_cfg *)no, uc); 464 return (0); 465 } 466 467 static struct nptv6_cfg * 468 nptv6_find(struct namedobj_instance *ni, const char *name, uint8_t set) 469 { 470 struct nptv6_cfg *cfg; 471 472 cfg = (struct nptv6_cfg *)ipfw_objhash_lookup_name_type(ni, set, 473 IPFW_TLV_NPTV6_NAME, name); 474 475 return (cfg); 476 } 477 478 static void 479 nptv6_calculate_adjustment(struct nptv6_cfg *cfg) 480 { 481 uint16_t i, e; 482 uint16_t *p; 483 484 /* Calculate checksum of internal prefix */ 485 for (i = 0, p = (uint16_t *)&cfg->internal; 486 p < (uint16_t *)(&cfg->internal + 1); p++) 487 i = cksum_add(i, *p); 488 489 /* Calculate checksum of external prefix */ 490 for (e = 0, p = (uint16_t *)&cfg->external; 491 p < (uint16_t *)(&cfg->external + 1); p++) 492 e = cksum_add(e, *p); 493 494 /* Adjustment value for Int->Ext direction */ 495 cfg->adjustment = cksum_add(~e, i); 496 } 497 498 /* 499 * Creates new NPTv6 instance. 500 * Data layout (v0)(current): 501 * Request: [ ipfw_obj_lheader ipfw_nptv6_cfg ] 502 * 503 * Returns 0 on success 504 */ 505 static int 506 nptv6_create(struct ip_fw_chain *ch, ip_fw3_opheader *op3, 507 struct sockopt_data *sd) 508 { 509 struct in6_addr mask; 510 ipfw_obj_lheader *olh; 511 ipfw_nptv6_cfg *uc; 512 struct namedobj_instance *ni; 513 struct nptv6_cfg *cfg; 514 515 if (sd->valsize != sizeof(*olh) + sizeof(*uc)) 516 return (EINVAL); 517 518 olh = (ipfw_obj_lheader *)sd->kbuf; 519 uc = (ipfw_nptv6_cfg *)(olh + 1); 520 if (ipfw_check_object_name_generic(uc->name) != 0) 521 return (EINVAL); 522 if (uc->plen < 8 || uc->plen > 64 || uc->set >= IPFW_MAX_SETS) 523 return (EINVAL); 524 if (IN6_IS_ADDR_MULTICAST(&uc->internal) || 525 IN6_IS_ADDR_MULTICAST(&uc->external) || 526 IN6_IS_ADDR_UNSPECIFIED(&uc->internal) || 527 IN6_IS_ADDR_UNSPECIFIED(&uc->external) || 528 IN6_IS_ADDR_LINKLOCAL(&uc->internal) || 529 IN6_IS_ADDR_LINKLOCAL(&uc->external)) 530 return (EINVAL); 531 in6_prefixlen2mask(&mask, uc->plen); 532 if (IN6_ARE_MASKED_ADDR_EQUAL(&uc->internal, &uc->external, &mask)) 533 return (EINVAL); 534 535 ni = CHAIN_TO_SRV(ch); 536 IPFW_UH_RLOCK(ch); 537 if (nptv6_find(ni, uc->name, uc->set) != NULL) { 538 IPFW_UH_RUNLOCK(ch); 539 return (EEXIST); 540 } 541 IPFW_UH_RUNLOCK(ch); 542 543 cfg = nptv6_alloc_config(uc->name, uc->set); 544 cfg->plen = uc->plen; 545 if (cfg->plen <= 48) 546 cfg->flags |= NPTV6_48PLEN; 547 cfg->internal = uc->internal; 548 cfg->external = uc->external; 549 cfg->mask = mask; 550 IN6_MASK_ADDR(&cfg->internal, &mask); 551 IN6_MASK_ADDR(&cfg->external, &mask); 552 nptv6_calculate_adjustment(cfg); 553 554 IPFW_UH_WLOCK(ch); 555 if (ipfw_objhash_alloc_idx(ni, &cfg->no.kidx) != 0) { 556 IPFW_UH_WUNLOCK(ch); 557 nptv6_free_config(cfg); 558 return (ENOSPC); 559 } 560 ipfw_objhash_add(ni, &cfg->no); 561 IPFW_WLOCK(ch); 562 SRV_OBJECT(ch, cfg->no.kidx) = cfg; 563 IPFW_WUNLOCK(ch); 564 IPFW_UH_WUNLOCK(ch); 565 return (0); 566 } 567 568 /* 569 * Destroys NPTv6 instance. 570 * Data layout (v0)(current): 571 * Request: [ ipfw_obj_header ] 572 * 573 * Returns 0 on success 574 */ 575 static int 576 nptv6_destroy(struct ip_fw_chain *ch, ip_fw3_opheader *op3, 577 struct sockopt_data *sd) 578 { 579 ipfw_obj_header *oh; 580 struct nptv6_cfg *cfg; 581 582 if (sd->valsize != sizeof(*oh)) 583 return (EINVAL); 584 585 oh = (ipfw_obj_header *)sd->kbuf; 586 if (ipfw_check_object_name_generic(oh->ntlv.name) != 0) 587 return (EINVAL); 588 589 IPFW_UH_WLOCK(ch); 590 cfg = nptv6_find(CHAIN_TO_SRV(ch), oh->ntlv.name, oh->ntlv.set); 591 if (cfg == NULL) { 592 IPFW_UH_WUNLOCK(ch); 593 return (ESRCH); 594 } 595 if (cfg->no.refcnt > 0) { 596 IPFW_UH_WUNLOCK(ch); 597 return (EBUSY); 598 } 599 600 IPFW_WLOCK(ch); 601 SRV_OBJECT(ch, cfg->no.kidx) = NULL; 602 IPFW_WUNLOCK(ch); 603 604 ipfw_objhash_del(CHAIN_TO_SRV(ch), &cfg->no); 605 ipfw_objhash_free_idx(CHAIN_TO_SRV(ch), cfg->no.kidx); 606 IPFW_UH_WUNLOCK(ch); 607 608 nptv6_free_config(cfg); 609 return (0); 610 } 611 612 /* 613 * Get or change nptv6 instance config. 614 * Request: [ ipfw_obj_header [ ipfw_nptv6_cfg ] ] 615 */ 616 static int 617 nptv6_config(struct ip_fw_chain *chain, ip_fw3_opheader *op, 618 struct sockopt_data *sd) 619 { 620 621 return (EOPNOTSUPP); 622 } 623 624 /* 625 * Lists all NPTv6 instances currently available in kernel. 626 * Data layout (v0)(current): 627 * Request: [ ipfw_obj_lheader ] 628 * Reply: [ ipfw_obj_lheader ipfw_nptv6_cfg x N ] 629 * 630 * Returns 0 on success 631 */ 632 static int 633 nptv6_list(struct ip_fw_chain *ch, ip_fw3_opheader *op3, 634 struct sockopt_data *sd) 635 { 636 ipfw_obj_lheader *olh; 637 struct nptv6_dump_arg da; 638 639 /* Check minimum header size */ 640 if (sd->valsize < sizeof(ipfw_obj_lheader)) 641 return (EINVAL); 642 643 olh = (ipfw_obj_lheader *)ipfw_get_sopt_header(sd, sizeof(*olh)); 644 645 IPFW_UH_RLOCK(ch); 646 olh->count = ipfw_objhash_count_type(CHAIN_TO_SRV(ch), 647 IPFW_TLV_NPTV6_NAME); 648 olh->objsize = sizeof(ipfw_nptv6_cfg); 649 olh->size = sizeof(*olh) + olh->count * olh->objsize; 650 651 if (sd->valsize < olh->size) { 652 IPFW_UH_RUNLOCK(ch); 653 return (ENOMEM); 654 } 655 memset(&da, 0, sizeof(da)); 656 da.ch = ch; 657 da.sd = sd; 658 ipfw_objhash_foreach_type(CHAIN_TO_SRV(ch), export_config_cb, 659 &da, IPFW_TLV_NPTV6_NAME); 660 IPFW_UH_RUNLOCK(ch); 661 662 return (0); 663 } 664 665 #define __COPY_STAT_FIELD(_cfg, _stats, _field) \ 666 (_stats)->_field = NPTV6STAT_FETCH(_cfg, _field) 667 static void 668 export_stats(struct ip_fw_chain *ch, struct nptv6_cfg *cfg, 669 struct ipfw_nptv6_stats *stats) 670 { 671 672 __COPY_STAT_FIELD(cfg, stats, in2ex); 673 __COPY_STAT_FIELD(cfg, stats, ex2in); 674 __COPY_STAT_FIELD(cfg, stats, dropped); 675 } 676 677 /* 678 * Get NPTv6 statistics. 679 * Data layout (v0)(current): 680 * Request: [ ipfw_obj_header ] 681 * Reply: [ ipfw_obj_header ipfw_obj_ctlv [ uint64_t x N ]] 682 * 683 * Returns 0 on success 684 */ 685 static int 686 nptv6_stats(struct ip_fw_chain *ch, ip_fw3_opheader *op, 687 struct sockopt_data *sd) 688 { 689 struct ipfw_nptv6_stats stats; 690 struct nptv6_cfg *cfg; 691 ipfw_obj_header *oh; 692 ipfw_obj_ctlv *ctlv; 693 size_t sz; 694 695 sz = sizeof(ipfw_obj_header) + sizeof(ipfw_obj_ctlv) + sizeof(stats); 696 if (sd->valsize % sizeof(uint64_t)) 697 return (EINVAL); 698 if (sd->valsize < sz) 699 return (ENOMEM); 700 oh = (ipfw_obj_header *)ipfw_get_sopt_header(sd, sz); 701 if (oh == NULL) 702 return (EINVAL); 703 if (ipfw_check_object_name_generic(oh->ntlv.name) != 0 || 704 oh->ntlv.set >= IPFW_MAX_SETS) 705 return (EINVAL); 706 memset(&stats, 0, sizeof(stats)); 707 708 IPFW_UH_RLOCK(ch); 709 cfg = nptv6_find(CHAIN_TO_SRV(ch), oh->ntlv.name, oh->ntlv.set); 710 if (cfg == NULL) { 711 IPFW_UH_RUNLOCK(ch); 712 return (ESRCH); 713 } 714 export_stats(ch, cfg, &stats); 715 IPFW_UH_RUNLOCK(ch); 716 717 ctlv = (ipfw_obj_ctlv *)(oh + 1); 718 memset(ctlv, 0, sizeof(*ctlv)); 719 ctlv->head.type = IPFW_TLV_COUNTERS; 720 ctlv->head.length = sz - sizeof(ipfw_obj_header); 721 ctlv->count = sizeof(stats) / sizeof(uint64_t); 722 ctlv->objsize = sizeof(uint64_t); 723 ctlv->version = 1; 724 memcpy(ctlv + 1, &stats, sizeof(stats)); 725 return (0); 726 } 727 728 /* 729 * Reset NPTv6 statistics. 730 * Data layout (v0)(current): 731 * Request: [ ipfw_obj_header ] 732 * 733 * Returns 0 on success 734 */ 735 static int 736 nptv6_reset_stats(struct ip_fw_chain *ch, ip_fw3_opheader *op, 737 struct sockopt_data *sd) 738 { 739 struct nptv6_cfg *cfg; 740 ipfw_obj_header *oh; 741 742 if (sd->valsize != sizeof(*oh)) 743 return (EINVAL); 744 oh = (ipfw_obj_header *)sd->kbuf; 745 if (ipfw_check_object_name_generic(oh->ntlv.name) != 0 || 746 oh->ntlv.set >= IPFW_MAX_SETS) 747 return (EINVAL); 748 749 IPFW_UH_WLOCK(ch); 750 cfg = nptv6_find(CHAIN_TO_SRV(ch), oh->ntlv.name, oh->ntlv.set); 751 if (cfg == NULL) { 752 IPFW_UH_WUNLOCK(ch); 753 return (ESRCH); 754 } 755 COUNTER_ARRAY_ZERO(cfg->stats, NPTV6STATS); 756 IPFW_UH_WUNLOCK(ch); 757 return (0); 758 } 759 760 static struct ipfw_sopt_handler scodes[] = { 761 { IP_FW_NPTV6_CREATE, 0, HDIR_SET, nptv6_create }, 762 { IP_FW_NPTV6_DESTROY,0, HDIR_SET, nptv6_destroy }, 763 { IP_FW_NPTV6_CONFIG, 0, HDIR_BOTH, nptv6_config }, 764 { IP_FW_NPTV6_LIST, 0, HDIR_GET, nptv6_list }, 765 { IP_FW_NPTV6_STATS, 0, HDIR_GET, nptv6_stats }, 766 { IP_FW_NPTV6_RESET_STATS,0, HDIR_SET, nptv6_reset_stats }, 767 }; 768 769 static int 770 nptv6_classify(ipfw_insn *cmd, uint16_t *puidx, uint8_t *ptype) 771 { 772 ipfw_insn *icmd; 773 774 icmd = cmd - 1; 775 NPTV6_DEBUG("opcode %d, arg1 %d, opcode0 %d, arg1 %d", 776 cmd->opcode, cmd->arg1, icmd->opcode, icmd->arg1); 777 if (icmd->opcode != O_EXTERNAL_ACTION || 778 icmd->arg1 != V_nptv6_eid) 779 return (1); 780 781 *puidx = cmd->arg1; 782 *ptype = 0; 783 return (0); 784 } 785 786 static void 787 nptv6_update_arg1(ipfw_insn *cmd, uint16_t idx) 788 { 789 790 cmd->arg1 = idx; 791 NPTV6_DEBUG("opcode %d, arg1 -> %d", cmd->opcode, cmd->arg1); 792 } 793 794 static int 795 nptv6_findbyname(struct ip_fw_chain *ch, struct tid_info *ti, 796 struct named_object **pno) 797 { 798 int err; 799 800 err = ipfw_objhash_find_type(CHAIN_TO_SRV(ch), ti, 801 IPFW_TLV_NPTV6_NAME, pno); 802 NPTV6_DEBUG("uidx %u, type %u, err %d", ti->uidx, ti->type, err); 803 return (err); 804 } 805 806 static struct named_object * 807 nptv6_findbykidx(struct ip_fw_chain *ch, uint16_t idx) 808 { 809 struct namedobj_instance *ni; 810 struct named_object *no; 811 812 IPFW_UH_WLOCK_ASSERT(ch); 813 ni = CHAIN_TO_SRV(ch); 814 no = ipfw_objhash_lookup_kidx(ni, idx); 815 KASSERT(no != NULL, ("NPT with index %d not found", idx)); 816 817 NPTV6_DEBUG("kidx %u -> %s", idx, no->name); 818 return (no); 819 } 820 821 static int 822 nptv6_manage_sets(struct ip_fw_chain *ch, uint16_t set, uint8_t new_set, 823 enum ipfw_sets_cmd cmd) 824 { 825 826 return (ipfw_obj_manage_sets(CHAIN_TO_SRV(ch), IPFW_TLV_NPTV6_NAME, 827 set, new_set, cmd)); 828 } 829 830 static struct opcode_obj_rewrite opcodes[] = { 831 { 832 .opcode = O_EXTERNAL_INSTANCE, 833 .etlv = IPFW_TLV_EACTION /* just show it isn't table */, 834 .classifier = nptv6_classify, 835 .update = nptv6_update_arg1, 836 .find_byname = nptv6_findbyname, 837 .find_bykidx = nptv6_findbykidx, 838 .manage_sets = nptv6_manage_sets, 839 }, 840 }; 841 842 static int 843 destroy_config_cb(struct namedobj_instance *ni, struct named_object *no, 844 void *arg) 845 { 846 struct nptv6_cfg *cfg; 847 struct ip_fw_chain *ch; 848 849 ch = (struct ip_fw_chain *)arg; 850 IPFW_UH_WLOCK_ASSERT(ch); 851 852 cfg = (struct nptv6_cfg *)SRV_OBJECT(ch, no->kidx); 853 SRV_OBJECT(ch, no->kidx) = NULL; 854 ipfw_objhash_del(ni, &cfg->no); 855 ipfw_objhash_free_idx(ni, cfg->no.kidx); 856 nptv6_free_config(cfg); 857 return (0); 858 } 859 860 int 861 nptv6_init(struct ip_fw_chain *ch, int first) 862 { 863 864 V_nptv6_eid = ipfw_add_eaction(ch, ipfw_nptv6, "nptv6"); 865 if (V_nptv6_eid == 0) 866 return (ENXIO); 867 IPFW_ADD_SOPT_HANDLER(first, scodes); 868 IPFW_ADD_OBJ_REWRITER(first, opcodes); 869 return (0); 870 } 871 872 void 873 nptv6_uninit(struct ip_fw_chain *ch, int last) 874 { 875 876 IPFW_DEL_OBJ_REWRITER(last, opcodes); 877 IPFW_DEL_SOPT_HANDLER(last, scodes); 878 ipfw_del_eaction(ch, V_nptv6_eid); 879 /* 880 * Since we already have deregistered external action, 881 * our named objects become unaccessible via rules, because 882 * all rules were truncated by ipfw_del_eaction(). 883 * So, we can unlink and destroy our named objects without holding 884 * IPFW_WLOCK(). 885 */ 886 IPFW_UH_WLOCK(ch); 887 ipfw_objhash_foreach_type(CHAIN_TO_SRV(ch), destroy_config_cb, ch, 888 IPFW_TLV_NPTV6_NAME); 889 V_nptv6_eid = 0; 890 IPFW_UH_WUNLOCK(ch); 891 } 892 893