1 /*
2 * Copyright (c) 2018-2023 Apple Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
14 *
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28
29 /*
30 * Copyright (c) 2001 Daniel Hartmeier
31 * Copyright (c) 2002 - 2013 Henning Brauer
32 * NAT64 - Copyright (c) 2010 Viagenie Inc. (http://www.viagenie.ca)
33 * All rights reserved.
34 *
35 * Redistribution and use in source and binary forms, with or without
36 * modification, are permitted provided that the following conditions
37 * are met:
38 *
39 * - Redistributions of source code must retain the above copyright
40 * notice, this list of conditions and the following disclaimer.
41 * - Redistributions in binary form must reproduce the above
42 * copyright notice, this list of conditions and the following
43 * disclaimer in the documentation and/or other materials provided
44 * with the distribution.
45 *
46 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
47 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
48 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
49 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
50 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
51 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
52 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
53 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
54 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
55 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
56 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
57 * POSSIBILITY OF SUCH DAMAGE.
58 *
59 * Effort sponsored in part by the Defense Advanced Research Projects
60 * Agency (DARPA) and Air Force Research Laboratory, Air Force
61 * Materiel Command, USAF, under agreement number F30602-01-2-0537.
62 *
63 */
64 #include <sys/param.h>
65 #include <sys/types.h>
66 #include <sys/mbuf.h>
67
68 #include <net/if.h>
69 #include <net/if_types.h>
70 #include <net/dlil.h>
71 #include <net/nat464_utils.h>
72 #include <net/nwk_wq.h>
73
74 #include <netinet/in.h>
75 #include <netinet/in_var.h>
76 #include <netinet/in_systm.h>
77 #include <netinet/ip.h>
78 #include <netinet/ip6.h>
79 #include <netinet/ip_var.h>
80 #include <netinet/ip_icmp.h>
81 #include <netinet/in_pcb.h>
82 #include <netinet/icmp_var.h>
83 #include <netinet/icmp6.h>
84 #include <netinet/tcp.h>
85 #include <netinet/udp.h>
86 #include <netinet/udp_var.h>
87 #include <os/log.h>
88
89 int clat_debug = 0;
90
91 os_log_t nat_log_handle;
92
93 static void
94 nat464_addr_cksum_fixup(uint16_t *, struct nat464_addr *, struct nat464_addr *,
95 protocol_family_t, protocol_family_t, uint8_t, boolean_t);
96
97 /* Synthesize ipv6 from ipv4 */
98 int
nat464_synthesize_ipv6(ifnet_t ifp,const struct in_addr * addrv4,struct in6_addr * addr)99 nat464_synthesize_ipv6(ifnet_t ifp, const struct in_addr *addrv4, struct in6_addr *addr)
100 {
101 static const struct in6_addr well_known_prefix = {
102 .__u6_addr.__u6_addr8 = {0x00, 0x64, 0xff, 0x9b, 0x00, 0x00,
103 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
104 0x00, 0x00, 0x00, 0x00},
105 };
106
107 struct ipv6_prefix nat64prefixes[NAT64_MAX_NUM_PREFIXES];
108 int error = 0, i = 0;
109 /* Below call is not optimized as it creates a copy of prefixes */
110 if ((error = ifnet_get_nat64prefix(ifp, nat64prefixes)) != 0) {
111 return error;
112 }
113
114 for (i = 0; i < NAT64_MAX_NUM_PREFIXES; i++) {
115 if (nat64prefixes[i].prefix_len != 0) {
116 break;
117 }
118 }
119
120 VERIFY(i < NAT64_MAX_NUM_PREFIXES);
121
122 struct in6_addr prefix = nat64prefixes[i].ipv6_prefix;
123 int prefix_len = nat64prefixes[i].prefix_len;
124
125 char *ptrv4 = (char *)__DECONST(struct in_addr *__indexable, addrv4);
126 char *ptr = (char *)__DECONST(struct in6_addr *__indexable, addr);
127
128 if (IN_ZERONET(ntohl(addrv4->s_addr)) || // 0.0.0.0/8 Source hosts on local network
129 IN_LOOPBACK(ntohl(addrv4->s_addr)) || // 127.0.0.0/8 Loopback
130 IN_LINKLOCAL(ntohl(addrv4->s_addr)) || // 169.254.0.0/16 Link Local
131 IN_DS_LITE(ntohl(addrv4->s_addr)) || // 192.0.0.0/29 DS-Lite
132 IN_6TO4_RELAY_ANYCAST(ntohl(addrv4->s_addr)) || // 192.88.99.0/24 6to4 Relay Anycast
133 IN_MULTICAST(ntohl(addrv4->s_addr)) || // 224.0.0.0/4 Multicast
134 INADDR_BROADCAST == addrv4->s_addr) { // 255.255.255.255/32 Limited Broadcast
135 return -1;
136 }
137
138 /* Check for the well-known prefix */
139 if (prefix_len == NAT64_PREFIX_LEN_96 &&
140 IN6_ARE_ADDR_EQUAL(&prefix, &well_known_prefix)) { // https://tools.ietf.org/html/rfc6052#section-3.1
141 if (IN_PRIVATE(ntohl(addrv4->s_addr)) || // 10.0.0.0/8, 172.16.0.0/12, 192.168.0.0/16 Private-Use
142 IN_SHARED_ADDRESS_SPACE(ntohl(addrv4->s_addr))) { // 100.64.0.0/10 Shared Address Space
143 return -1;
144 }
145 }
146
147 memcpy(ptr, (char *)&prefix, prefix_len);
148
149 switch (prefix_len) {
150 case NAT64_PREFIX_LEN_96:
151 memcpy(ptr + 12, ptrv4, 4);
152 break;
153 case NAT64_PREFIX_LEN_64:
154 memcpy(ptr + 9, ptrv4, 4);
155 break;
156 case NAT64_PREFIX_LEN_56:
157 memcpy(ptr + 7, ptrv4, 1);
158 memcpy(ptr + 9, ptrv4 + 1, 3);
159 break;
160 case NAT64_PREFIX_LEN_48:
161 memcpy(ptr + 6, ptrv4, 2);
162 memcpy(ptr + 9, ptrv4 + 2, 2);
163 break;
164 case NAT64_PREFIX_LEN_40:
165 memcpy(ptr + 5, ptrv4, 3);
166 memcpy(ptr + 9, ptrv4 + 3, 1);
167 break;
168 case NAT64_PREFIX_LEN_32:
169 memcpy(ptr + 4, ptrv4, 4);
170 break;
171 default:
172 panic("NAT64-prefix len is wrong: %u", prefix_len);
173 }
174
175 if (clat_debug) {
176 char buf[MAX_IPv6_STR_LEN];
177 clat_log2((LOG_DEBUG, "%s synthesized %s\n", __func__,
178 inet_ntop(AF_INET6, (void *)addr, buf, sizeof(buf))));
179 }
180
181 return error;
182 }
183
184 /* Synthesize ipv4 from ipv6 */
185 int
nat464_synthesize_ipv4(ifnet_t ifp,const struct in6_addr * addr,struct in_addr * addrv4)186 nat464_synthesize_ipv4(ifnet_t ifp, const struct in6_addr *addr, struct in_addr *addrv4)
187 {
188 struct ipv6_prefix nat64prefixes[NAT64_MAX_NUM_PREFIXES];
189 int error = 0, i = 0;
190
191 /* Below call is not optimized as it creates a copy of prefixes */
192 if ((error = ifnet_get_nat64prefix(ifp, nat64prefixes)) != 0) {
193 return error;
194 }
195
196 for (i = 0; i < NAT64_MAX_NUM_PREFIXES; i++) {
197 if (nat64prefixes[i].prefix_len != 0) {
198 break;
199 }
200 }
201
202 VERIFY(i < NAT64_MAX_NUM_PREFIXES);
203
204 struct in6_addr prefix = nat64prefixes[i].ipv6_prefix;
205 int prefix_len = nat64prefixes[i].prefix_len;
206
207 VERIFY(prefix_len < sizeof(prefix));
208
209 char *ptrv4 = (char *)__DECONST(struct in_addr *__indexable, addrv4);
210 char *ptr = (char *)__DECONST(struct in6_addr *__indexable, addr);
211
212 /* -fbounds-safety:
213 * Override a warning about prefix_len being > 16 because
214 * we already checked that above.
215 */
216 if (memcmp((const struct in6_addr *__indexable)addr, &prefix, prefix_len) != 0) {
217 return -1;
218 }
219
220 switch (prefix_len) {
221 case NAT64_PREFIX_LEN_96:
222 memcpy(ptrv4, ptr + 12, 4);
223 break;
224 case NAT64_PREFIX_LEN_64:
225 memcpy(ptrv4, ptr + 9, 4);
226 break;
227 case NAT64_PREFIX_LEN_56:
228 memcpy(ptrv4, ptr + 7, 1);
229 memcpy(ptrv4 + 1, ptr + 9, 3);
230 break;
231 case NAT64_PREFIX_LEN_48:
232 memcpy(ptrv4, ptr + 6, 2);
233 memcpy(ptrv4 + 2, ptr + 9, 2);
234 break;
235 case NAT64_PREFIX_LEN_40:
236 memcpy(ptrv4, ptr + 5, 3);
237 memcpy(ptrv4 + 3, ptr + 9, 1);
238 break;
239 case NAT64_PREFIX_LEN_32:
240 memcpy(ptrv4, ptr + 4, 4);
241 break;
242 default:
243 panic("NAT64-prefix len is wrong: %u",
244 prefix_len);
245 }
246
247 if (clat_debug) {
248 char buf[MAX_IPv4_STR_LEN];
249 clat_log2((LOG_DEBUG, "%s desynthesized to %s\n", __func__,
250 inet_ntop(AF_INET, (void *)addrv4, buf, sizeof(buf))));
251 }
252 return error;
253 }
254
255 #define PTR_IP(field) ((int32_t)offsetof(struct ip, field))
256 #define PTR_IP6(field) ((int32_t)offsetof(struct ip6_hdr, field))
257
258 /*
259 * Translate the ICMP header
260 */
261 int
nat464_translate_icmp(int naf,void * arg)262 nat464_translate_icmp(int naf, void *arg)
263 {
264 struct icmp *__single icmp4;
265 struct icmp6_hdr *__single icmp6;
266 uint32_t mtu;
267 int32_t ptr = -1;
268 uint8_t type;
269 uint8_t code;
270
271 switch (naf) {
272 case AF_INET:
273 icmp6 = arg;
274 type = icmp6->icmp6_type;
275 code = icmp6->icmp6_code;
276 mtu = ntohl(icmp6->icmp6_mtu);
277
278 switch (type) {
279 case ICMP6_ECHO_REQUEST:
280 type = ICMP_ECHO;
281 break;
282 case ICMP6_ECHO_REPLY:
283 type = ICMP_ECHOREPLY;
284 break;
285 case ICMP6_DST_UNREACH:
286 type = ICMP_UNREACH;
287 switch (code) {
288 case ICMP6_DST_UNREACH_NOROUTE:
289 case ICMP6_DST_UNREACH_BEYONDSCOPE:
290 case ICMP6_DST_UNREACH_ADDR:
291 code = ICMP_UNREACH_HOST;
292 break;
293 case ICMP6_DST_UNREACH_ADMIN:
294 code = ICMP_UNREACH_HOST_PROHIB;
295 break;
296 case ICMP6_DST_UNREACH_NOPORT:
297 code = ICMP_UNREACH_PORT;
298 break;
299 default:
300 return -1;
301 }
302 break;
303 case ICMP6_PACKET_TOO_BIG:
304 type = ICMP_UNREACH;
305 code = ICMP_UNREACH_NEEDFRAG;
306 mtu -= 20;
307 break;
308 case ICMP6_TIME_EXCEEDED:
309 type = ICMP_TIMXCEED;
310 break;
311 case ICMP6_PARAM_PROB:
312 switch (code) {
313 case ICMP6_PARAMPROB_HEADER:
314 type = ICMP_PARAMPROB;
315 code = ICMP_PARAMPROB_ERRATPTR;
316 ptr = ntohl(icmp6->icmp6_pptr);
317
318 if (ptr == PTR_IP6(ip6_vfc)) {
319 ; /* preserve */
320 } else if (ptr == PTR_IP6(ip6_vfc) + 1) {
321 ptr = PTR_IP(ip_tos);
322 } else if (ptr == PTR_IP6(ip6_plen) ||
323 ptr == PTR_IP6(ip6_plen) + 1) {
324 ptr = PTR_IP(ip_len);
325 } else if (ptr == PTR_IP6(ip6_nxt)) {
326 ptr = PTR_IP(ip_p);
327 } else if (ptr == PTR_IP6(ip6_hlim)) {
328 ptr = PTR_IP(ip_ttl);
329 } else if (ptr >= PTR_IP6(ip6_src) &&
330 ptr < PTR_IP6(ip6_dst)) {
331 ptr = PTR_IP(ip_src);
332 } else if (ptr >= PTR_IP6(ip6_dst) &&
333 ptr < (int32_t)sizeof(struct ip6_hdr)) {
334 ptr = PTR_IP(ip_dst);
335 } else {
336 return -1;
337 }
338 break;
339 case ICMP6_PARAMPROB_NEXTHEADER:
340 type = ICMP_UNREACH;
341 code = ICMP_UNREACH_PROTOCOL;
342 break;
343 default:
344 return -1;
345 }
346 break;
347 default:
348 return -1;
349 }
350 icmp6->icmp6_type = type;
351 icmp6->icmp6_code = code;
352 /* aligns well with a icmpv4 nextmtu */
353 icmp6->icmp6_mtu = htonl(mtu);
354 /* icmpv4 pptr is a one most significant byte */
355 if (ptr >= 0) {
356 icmp6->icmp6_pptr = htonl(ptr << 24);
357 }
358 break;
359
360 case AF_INET6:
361 icmp4 = arg;
362 type = icmp4->icmp_type;
363 code = icmp4->icmp_code;
364 mtu = ntohs(icmp4->icmp_nextmtu);
365
366 switch (type) {
367 case ICMP_ECHO:
368 type = ICMP6_ECHO_REQUEST;
369 break;
370 case ICMP_ECHOREPLY:
371 type = ICMP6_ECHO_REPLY;
372 break;
373 case ICMP_UNREACH:
374 type = ICMP6_DST_UNREACH;
375 switch (code) {
376 case ICMP_UNREACH_NET:
377 case ICMP_UNREACH_HOST:
378 case ICMP_UNREACH_NET_UNKNOWN:
379 case ICMP_UNREACH_HOST_UNKNOWN:
380 case ICMP_UNREACH_ISOLATED:
381 case ICMP_UNREACH_TOSNET:
382 case ICMP_UNREACH_TOSHOST:
383 code = ICMP6_DST_UNREACH_NOROUTE;
384 break;
385 case ICMP_UNREACH_PORT:
386 code = ICMP6_DST_UNREACH_NOPORT;
387 break;
388 case ICMP_UNREACH_NET_PROHIB:
389 case ICMP_UNREACH_HOST_PROHIB:
390 case ICMP_UNREACH_FILTER_PROHIB:
391 case ICMP_UNREACH_PRECEDENCE_CUTOFF:
392 code = ICMP6_DST_UNREACH_ADMIN;
393 break;
394 case ICMP_UNREACH_PROTOCOL:
395 type = ICMP6_PARAM_PROB;
396 code = ICMP6_PARAMPROB_NEXTHEADER;
397 ptr = offsetof(struct ip6_hdr, ip6_nxt);
398 break;
399 case ICMP_UNREACH_NEEDFRAG:
400 type = ICMP6_PACKET_TOO_BIG;
401 code = 0;
402 /*
403 * Make sure we don't overflow adjusting for
404 * translation overhead.
405 * If we do, just work with a lower mtu as is.
406 */
407 if (mtu <= (UINT16_MAX - CLAT46_HDR_EXPANSION_OVERHD)) {
408 mtu += CLAT46_HDR_EXPANSION_OVERHD;
409 }
410 break;
411 default:
412 return -1;
413 }
414 break;
415 case ICMP_TIMXCEED:
416 type = ICMP6_TIME_EXCEEDED;
417 break;
418 case ICMP_PARAMPROB:
419 type = ICMP6_PARAM_PROB;
420 switch (code) {
421 case ICMP_PARAMPROB_ERRATPTR:
422 code = ICMP6_PARAMPROB_HEADER;
423 break;
424 case ICMP_PARAMPROB_LENGTH:
425 code = ICMP6_PARAMPROB_HEADER;
426 break;
427 default:
428 return -1;
429 }
430
431 ptr = icmp4->icmp_pptr;
432 if (ptr == 0 || ptr == PTR_IP(ip_tos)) {
433 ; /* preserve */
434 } else if (ptr == PTR_IP(ip_len) ||
435 ptr == PTR_IP(ip_len) + 1) {
436 ptr = PTR_IP6(ip6_plen);
437 } else if (ptr == PTR_IP(ip_ttl)) {
438 ptr = PTR_IP6(ip6_hlim);
439 } else if (ptr == PTR_IP(ip_p)) {
440 ptr = PTR_IP6(ip6_nxt);
441 } else if (ptr >= PTR_IP(ip_src) &&
442 ptr < PTR_IP(ip_dst)) {
443 ptr = PTR_IP6(ip6_src);
444 } else if (ptr >= PTR_IP(ip_dst) &&
445 ptr < (int32_t)sizeof(struct ip)) {
446 ptr = PTR_IP6(ip6_dst);
447 } else {
448 return -1;
449 }
450 break;
451 default:
452 return -1;
453 }
454 icmp4->icmp_type = type;
455 icmp4->icmp_code = code;
456 icmp4->icmp_nextmtu = htons((uint16_t)mtu);
457
458 if (ptr >= 0) {
459 icmp4->icmp_void = htonl(ptr);
460 }
461 break;
462 }
463
464 return 0;
465 }
466
467 /*
468 * @brief This routine is called to perform address family translation on the
469 * inner IP header (that may come as payload) of an ICMP(v4/v6) error
470 * response.
471 *
472 * @param pbuf Pointer to packet buffer
473 * @param off Points to end of ICMP header
474 * @param tot_len Pointer to total length of the outer IP header
475 * @param off2 Points to end of inner IP header
476 * @param proto2 Inner IP proto field
477 * @param ttl2 Inner IP ttl field
478 * @param tot_len2 Inner IP total length
479 * @param src Pointer to the generic v4/v6 src address
480 * @param dst Pointer to the generic v4/v6 dst address
481 * @param af Old protocol family
482 * @param naf New protocol family
483 *
484 * @return -1 on error and 0 on success
485 */
486 int
nat464_translate_icmp_ip(pbuf_t * pbuf,uint16_t off,uint16_t * tot_len,uint16_t * off2,uint8_t proto2,uint8_t ttl2,uint16_t tot_len2,struct nat464_addr * src,struct nat464_addr * dst,protocol_family_t af,protocol_family_t naf)487 nat464_translate_icmp_ip(pbuf_t *pbuf, uint16_t off, uint16_t *tot_len, uint16_t *off2,
488 uint8_t proto2, uint8_t ttl2, uint16_t tot_len2, struct nat464_addr *src,
489 struct nat464_addr *dst, protocol_family_t af, protocol_family_t naf)
490 {
491 struct ip *__single ip4 = NULL;
492 struct ip6_hdr *__single ip6 = NULL;
493 void *__single hdr = NULL;
494 int hlen = 0, olen = 0;
495 uint64_t ipid_salt = (uint64_t)pbuf_get_packet_buffer_address(pbuf);
496
497 if (af == naf || (af != AF_INET && af != AF_INET6) ||
498 (naf != AF_INET && naf != AF_INET6)) {
499 return -1;
500 }
501
502 /* old header */
503 olen = *off2 - off;
504 /* new header */
505 hlen = naf == PF_INET ? sizeof(*ip4) : sizeof(*ip6);
506
507 /* Modify the pbuf to accommodate the new header */
508 hdr = pbuf_resize_segment(pbuf, off, olen, hlen);
509 if (hdr == NULL) {
510 return -1;
511 }
512
513 /* translate inner ip/ip6 header */
514 switch (naf) {
515 case AF_INET:
516 ip4 = hdr;
517 bzero(ip4, sizeof(*ip4));
518 ip4->ip_v = IPVERSION;
519 ip4->ip_hl = sizeof(*ip4) >> 2;
520 ip4->ip_len = htons((uint16_t)(sizeof(*ip4) + tot_len2 - olen));
521 ip4->ip_id = rfc6864 ? 0 : htons(ip_randomid(ipid_salt));
522 ip4->ip_off = htons(IP_DF);
523 ip4->ip_ttl = ttl2;
524 if (proto2 == IPPROTO_ICMPV6) {
525 ip4->ip_p = IPPROTO_ICMP;
526 } else {
527 ip4->ip_p = proto2;
528 }
529 ip4->ip_src = src->natv4addr;
530 ip4->ip_dst = dst->natv4addr;
531 ip4->ip_sum = pbuf_inet_cksum(pbuf, 0, 0, ip4->ip_hl << 2);
532
533 if (clat_debug) {
534 char buf[MAX_IPv4_STR_LEN];
535 clat_log2((LOG_DEBUG, "%s translated to IPv4 (inner) "
536 "ip_len: %#x ip_p: %d ip_sum: %#x ip_src: %s ip_dst: %s \n",
537 __func__, ntohs(ip4->ip_len), ip4->ip_p, ntohs(ip4->ip_sum),
538 inet_ntop(AF_INET, (void *)&ip4->ip_src, buf, sizeof(buf)),
539 inet_ntop(AF_INET, (void *)&ip4->ip_dst, buf, sizeof(buf))));
540 }
541 break;
542 case AF_INET6:
543 ip6 = hdr;
544 bzero(ip6, sizeof(*ip6));
545 ip6->ip6_vfc = IPV6_VERSION;
546 ip6->ip6_plen = htons((uint16_t)(tot_len2 - olen));
547 if (proto2 == IPPROTO_ICMP) {
548 ip6->ip6_nxt = IPPROTO_ICMPV6;
549 } else {
550 ip6->ip6_nxt = proto2;
551 }
552 if (!ttl2 || ttl2 > IPV6_DEFHLIM) {
553 ip6->ip6_hlim = IPV6_DEFHLIM;
554 } else {
555 ip6->ip6_hlim = ttl2;
556 }
557 ip6->ip6_src = src->natv6addr;
558 ip6->ip6_dst = dst->natv6addr;
559
560 if (clat_debug) {
561 char buf2[MAX_IPv6_STR_LEN];
562 clat_log2((LOG_DEBUG, "%s translated to IPv6 (inner) "
563 "ip6_plen: %#x ip6_nxt: %d ip6_src: %s ip6_dst: %s \n",
564 __func__, ntohs(ip6->ip6_plen), ip6->ip6_nxt,
565 inet_ntop(AF_INET6, (void *)&ip6->ip6_src, buf2, sizeof(buf2)),
566 inet_ntop(AF_INET6, (void *)&ip6->ip6_dst, buf2, sizeof(buf2))));
567 }
568 break;
569 }
570
571 /* adjust payload offset and total packet length */
572 *off2 += hlen - olen;
573 *tot_len += hlen - olen;
574
575 return 0;
576 }
577 /*
578 * @brief The function inserts IPv6 fragmentation header
579 * and populates it with the passed parameters.
580 *
581 * @param pbuf Pointer to the packet buffer
582 * @param ip_id IP identifier (in network byte order)
583 * @param frag_offset Fragment offset (in network byte order)
584 * @param is_last_frag Boolean indicating if the fragment header is for
585 * last fragment or not.
586 *
587 * @return -1 on error and 0 on success.
588 */
589 int
nat464_insert_frag46(pbuf_t * pbuf,uint16_t ip_id_val,uint16_t frag_offset,boolean_t is_last_frag)590 nat464_insert_frag46(pbuf_t *pbuf, uint16_t ip_id_val, uint16_t frag_offset,
591 boolean_t is_last_frag)
592 {
593 struct ip6_frag *p_ip6_frag = NULL;
594 struct ip6_hdr *p_ip6h = NULL;
595
596 /* Insert IPv6 fragmentation header */
597 if (pbuf_resize_segment(pbuf, sizeof(struct ip6_hdr), 0,
598 sizeof(struct ip6_frag)) == NULL) {
599 return -1;
600 }
601
602 p_ip6h = mtod(pbuf->pb_mbuf, struct ip6_hdr *);
603 p_ip6_frag = (struct ip6_frag *)pbuf_contig_segment(pbuf,
604 sizeof(struct ip6_hdr), sizeof(struct ip6_frag));
605
606 if (p_ip6_frag == NULL) {
607 return -1;
608 }
609
610 /* Populate IPv6 fragmentation header */
611 p_ip6_frag->ip6f_nxt = p_ip6h->ip6_nxt;
612 p_ip6_frag->ip6f_reserved = 0;
613 p_ip6_frag->ip6f_offlg = (uint16_t)(frag_offset << 3);
614 if (!is_last_frag) {
615 p_ip6_frag->ip6f_offlg |= 0x1;
616 }
617 p_ip6_frag->ip6f_offlg = htons(p_ip6_frag->ip6f_offlg);
618 p_ip6_frag->ip6f_ident = ip_id_val;
619
620 /* Update IPv6 header */
621 p_ip6h->ip6_nxt = IPPROTO_FRAGMENT;
622 p_ip6h->ip6_plen = htons(ntohs(p_ip6h->ip6_plen) +
623 sizeof(struct ip6_frag));
624
625 return 0;
626 }
627
628 int
nat464_translate_64(pbuf_t * pbuf,int off,uint8_t tos,uint8_t * proto,uint8_t ttl,struct in_addr src_v4,struct in_addr dst_v4,uint64_t tot_len,boolean_t * p_is_first_frag)629 nat464_translate_64(pbuf_t *pbuf, int off, uint8_t tos,
630 uint8_t *proto, uint8_t ttl, struct in_addr src_v4,
631 struct in_addr dst_v4, uint64_t tot_len, boolean_t *p_is_first_frag)
632 {
633 struct ip *ip4;
634 struct ip6_frag *p_frag6 = NULL;
635 struct ip6_frag frag6 = {};
636 boolean_t is_frag = FALSE;
637 uint16_t ip_frag_off = 0;
638
639 /*
640 * ip_input asserts for rcvif to be not NULL
641 * That may not be true for two corner cases
642 * 1. If for some reason a local app sends DNS
643 * AAAA query to local host
644 * 2. If IPv6 stack in kernel internally generates a
645 * message destined for a synthesized IPv6 end-point.
646 */
647 if (pbuf->pb_ifp == NULL) {
648 return NT_DROP;
649 }
650
651 if (*proto == IPPROTO_FRAGMENT) {
652 p_frag6 = (struct ip6_frag *)pbuf_contig_segment(pbuf,
653 sizeof(struct ip6_hdr), sizeof(struct ip6_frag));
654 if (p_frag6 == NULL) {
655 ip6stat.ip6s_clat464_in_64frag_transfail_drop++;
656 return NT_DROP;
657 }
658
659 frag6 = *p_frag6;
660 p_frag6 = NULL;
661 *proto = frag6.ip6f_nxt;
662 off += sizeof(struct ip6_frag);
663 is_frag = TRUE;
664 ip_frag_off = (ntohs(frag6.ip6f_offlg & IP6F_OFF_MASK)) >> 3;
665 if (ip_frag_off != 0) {
666 *p_is_first_frag = FALSE;
667 }
668 }
669
670 ip4 = (struct ip *)pbuf_resize_segment(pbuf, 0, off, sizeof(*ip4));
671 if (ip4 == NULL) {
672 return NT_DROP;
673 }
674 ip4->ip_v = 4;
675 ip4->ip_hl = 5;
676 ip4->ip_tos = tos;
677 ip4->ip_len = htons((uint16_t)(sizeof(*ip4) + (tot_len - off)));
678 ip4->ip_id = 0;
679 ip4->ip_off = 0;
680 ip4->ip_ttl = ttl;
681 ip4->ip_p = *proto;
682 ip4->ip_sum = 0;
683 ip4->ip_src = src_v4;
684 ip4->ip_dst = dst_v4;
685 if (is_frag) {
686 /*
687 * https://tools.ietf.org/html/rfc7915#section-5.1.1
688 * Identification: Copied from the low-order 16 bits in the
689 * Identification field in the Fragment Header.
690 */
691 ip4->ip_id = ntohl(frag6.ip6f_ident) & 0xffff;
692 ip4->ip_id = htons(ip4->ip_id);
693 if (frag6.ip6f_offlg & IP6F_MORE_FRAG) {
694 ip_frag_off |= IP_MF;
695 }
696 ip4->ip_off = htons(ip_frag_off);
697 } else {
698 ip4->ip_off |= htons(IP_DF);
699 }
700
701 /*
702 * Defer calculating ip_sum for ICMPv6 as we do it
703 * later in Protocol translation
704 */
705 if (*proto != IPPROTO_ICMPV6) {
706 ip4->ip_sum = pbuf_inet_cksum(pbuf, 0, 0, ip4->ip_hl << 2);
707 }
708
709 if (clat_debug) {
710 char buf1[MAX_IPv4_STR_LEN], buf2[MAX_IPv4_STR_LEN];
711 clat_log2((LOG_DEBUG, "%s translated to IPv4 ip_len: %#x "
712 "ip_p: %d ip_sum: %#x ip_src: %s ip_dst: %s \n", __func__,
713 ntohs(ip4->ip_len), ip4->ip_p, ntohs(ip4->ip_sum),
714 inet_ntop(AF_INET, (void *)&ip4->ip_src, buf1, sizeof(buf1)),
715 inet_ntop(AF_INET, (void *)&ip4->ip_dst, buf2, sizeof(buf2))));
716 }
717 return NT_NAT64;
718 }
719 /*
720 * @brief The routine translates the IPv4 header to IPv6 header.
721 *
722 * @param pbuf Pointer to the generic packet buffer
723 * @param off Offset to the end of IP header
724 * @param tos Type of service
725 * @param proto Protocol running over IP
726 * @param ttl Time to live
727 * @param src_v6 Source IPv6 address
728 * @param dst_v6 Destination IPv6 address
729 * @param tot_len Total payload length
730 *
731 * @return NT_NAT64 if IP header translation is successful, else error
732 */
733 int
nat464_translate_46(pbuf_t * pbuf,uint16_t off,uint8_t tos,uint8_t proto,uint8_t ttl,struct in6_addr src_v6,struct in6_addr dst_v6,uint16_t tot_len)734 nat464_translate_46(pbuf_t *pbuf, uint16_t off, uint8_t tos,
735 uint8_t proto, uint8_t ttl, struct in6_addr src_v6,
736 struct in6_addr dst_v6, uint16_t tot_len)
737 {
738 struct ip6_hdr *ip6;
739
740 if (pbuf->pb_ifp == NULL) {
741 return NT_DROP;
742 }
743
744 /*
745 * Trim the buffer from head of size equal to to off (which is equal to
746 * the size of IP header and prepend IPv6 header length to the buffer
747 */
748 ip6 = (struct ip6_hdr *)pbuf_resize_segment(pbuf, 0, off, sizeof(*ip6));
749 if (ip6 == NULL) {
750 return NT_DROP;
751 }
752 ip6->ip6_flow = htonl((6 << 28) | (tos << 20));
753 ip6->ip6_plen = htons(tot_len - off);
754 ip6->ip6_nxt = proto;
755 ip6->ip6_hlim = ttl;
756 ip6->ip6_src = src_v6;
757 ip6->ip6_dst = dst_v6;
758
759 if (clat_debug) {
760 char buf1[MAX_IPv6_STR_LEN], buf2[MAX_IPv6_STR_LEN];
761 clat_log2((LOG_DEBUG, "%s translated to IPv6 ip6_plen: %#x "
762 " ip6_nxt: %d ip6_src: %s ip6_dst: %s \n", __func__,
763 ntohs(ip6->ip6_plen), ip6->ip6_nxt,
764 inet_ntop(AF_INET6, (void *)&ip6->ip6_src, buf1, sizeof(buf1)),
765 inet_ntop(AF_INET6, (void *)&ip6->ip6_dst, buf2, sizeof(buf2))));
766 }
767 return NT_NAT64;
768 }
769
770 /* Handle the next protocol checksum */
771 /*
772 * @brief This routine translates the Proto running over IP and updates the checksum
773 * for IP header translation. It also updates pbuf checksum flags and related fields.
774 *
775 * @param pbuf Pointer to protocol buffer
776 * @param nsrc New source address
777 * @param ndst New destination address
778 * @param af Old family
779 * @param naf New family
780 *
781 * @return void
782 */
783 int
nat464_translate_proto(pbuf_t * pbuf,struct nat464_addr * osrc,struct nat464_addr * odst,uint8_t oproto,protocol_family_t af,protocol_family_t naf,int direction,boolean_t only_csum)784 nat464_translate_proto(pbuf_t *pbuf, struct nat464_addr *osrc,
785 struct nat464_addr *odst, uint8_t oproto, protocol_family_t af,
786 protocol_family_t naf, int direction, boolean_t only_csum)
787 {
788 struct ip *iph = NULL;
789 struct ip6_hdr *ip6h = NULL;
790 uint16_t hlen = 0, plen = 0;
791 uint16_t tot_len = 0;
792 void *nsrc = NULL, *ndst = NULL;
793 uint8_t *proto = 0;
794 uint16_t *psum = NULL;
795 boolean_t do_ones_complement = FALSE;
796
797 /* For now these routines only support 464 translations */
798 VERIFY(af != naf);
799 VERIFY(af == PF_INET || af == PF_INET6);
800
801 /*
802 * For now out must be for v4 to v6 translation
803 * and in must be for v6 to v4 translation.
804 */
805 switch (naf) {
806 case PF_INET: {
807 iph = pbuf->pb_data;
808 hlen = (uint16_t)(iph->ip_hl << 2);
809 plen = ntohs(iph->ip_len) - hlen;
810 tot_len = ntohs(iph->ip_len);
811 nsrc = &iph->ip_src;
812 ndst = &iph->ip_dst;
813 proto = &iph->ip_p;
814 break;
815 }
816 case PF_INET6: {
817 ip6h = pbuf->pb_data;
818 hlen = (uint16_t)sizeof(*ip6h);
819 plen = ntohs(ip6h->ip6_plen);
820 tot_len = hlen + plen;
821 nsrc = &ip6h->ip6_src;
822 ndst = &ip6h->ip6_dst;
823 proto = &ip6h->ip6_nxt;
824 break;
825 }
826 default:
827 return NT_DROP; /* We should never come here */
828 }
829
830 if (*proto != oproto) {
831 return NT_DROP;
832 }
833
834 /*
835 * We may want to manipulate csum flags in some cases
836 * and not act on the protocol header as it may not
837 * carry protocol checksums.
838 * For example, fragments other than the first one would
839 * not carry protocol headers.
840 */
841 if (only_csum) {
842 /*
843 * Only translate ICMP proto in the header
844 * and adjust checksums
845 */
846 if (*proto == IPPROTO_ICMP) {
847 if (naf != PF_INET6) {
848 return NT_DROP;
849 }
850
851 *proto = IPPROTO_ICMPV6;
852 } else if (*proto == IPPROTO_ICMPV6) {
853 if (naf != PF_INET) {
854 return NT_DROP;
855 }
856
857 *proto = IPPROTO_ICMP;
858 /* Recalculate IP checksum as proto field has changed */
859 iph->ip_sum = 0;
860 iph->ip_sum = pbuf_inet_cksum(pbuf, 0, 0, hlen);
861 }
862 goto done;
863 }
864
865 switch (*proto) {
866 case IPPROTO_UDP: {
867 struct udphdr *uh = (struct udphdr *)pbuf_contig_segment(pbuf, hlen,
868 sizeof(*uh));
869
870 if (uh == NULL) {
871 return NT_DROP;
872 }
873
874 if (!(*pbuf->pb_csum_flags & (CSUM_UDP | CSUM_PARTIAL)) &&
875 uh->uh_sum == 0 && af == PF_INET && naf == PF_INET6) {
876 uh->uh_sum = pbuf_inet6_cksum(pbuf, IPPROTO_UDP,
877 hlen, ntohs(ip6h->ip6_plen));
878 if (uh->uh_sum == 0) {
879 uh->uh_sum = 0xffff;
880 }
881 goto done;
882 }
883
884 psum = &uh->uh_sum;
885 break;
886 }
887 case IPPROTO_TCP: {
888 struct tcphdr *th = (struct tcphdr *)pbuf_contig_segment(pbuf, hlen,
889 sizeof(*th));
890
891 if (th == NULL) {
892 return NT_DROP;
893 }
894
895 psum = &th->th_sum;
896 break;
897 }
898 }
899
900 /*
901 * Translate the protocol header, update IP header if needed,
902 * calculate checksums and update the checksum flags.
903 */
904 switch (*proto) {
905 case IPPROTO_UDP:
906 /* Fall through */
907 case IPPROTO_TCP:
908 {
909 /*
910 * If it is a locally generated and has CSUM flags set
911 * for TCP and UDP it means we have pseudo header checksum
912 * that has not yet been one's complemented.
913 */
914 if (direction == NT_OUT &&
915 (*pbuf->pb_csum_flags & CSUM_PARTIAL)) {
916 do_ones_complement = TRUE;
917 }
918
919 nat464_addr_cksum_fixup(psum, osrc, (struct nat464_addr *)nsrc,
920 af, naf, (*proto == IPPROTO_UDP) ? 1 : 0, do_ones_complement);
921 nat464_addr_cksum_fixup(psum, odst, (struct nat464_addr *)ndst,
922 af, naf, (*proto == IPPROTO_UDP) ? 1 : 0, do_ones_complement);
923
924 break;
925 }
926 case IPPROTO_ICMP: {
927 if (naf != PF_INET6) { /* allow only v6 as naf for ICMP */
928 return NT_DROP;
929 }
930
931 struct icmp *__single icmph = NULL;
932 struct icmp6_hdr *__single icmp6h = NULL;
933 uint16_t ip2off = 0, hlen2 = 0, tot_len2 = 0;
934
935 icmph = (struct icmp*) pbuf_contig_segment(pbuf, hlen,
936 ICMP_MINLEN);
937 if (icmph == NULL) {
938 return NT_DROP;
939 }
940
941 /* Translate the ICMP header */
942 if (nat464_translate_icmp(PF_INET6, icmph) != 0) {
943 return NT_DROP;
944 }
945
946 *proto = IPPROTO_ICMPV6;
947 icmp6h = (struct icmp6_hdr *__single)(void *)icmph;
948 pbuf_copy_back(pbuf, hlen, sizeof(struct icmp6_hdr),
949 icmp6h, sizeof(*icmp6h));
950
951 /*Translate the inner IP header only for error messages */
952 if (ICMP6_ERRORTYPE(icmp6h->icmp6_type)) {
953 ip2off = (uint16_t)(hlen + sizeof(*icmp6h));
954 struct ip *iph2 = NULL;
955 iph2 = (struct ip*) pbuf_contig_segment(pbuf, ip2off,
956 sizeof(*iph2));
957 if (iph2 == NULL) {
958 return NT_DROP;
959 }
960
961 hlen2 = (uint16_t)(ip2off + (iph2->ip_hl << 2));
962 tot_len2 = ntohs(iph2->ip_len);
963
964 /* Destination in outer IP should be Source in inner IP */
965 VERIFY(IN_ARE_ADDR_EQUAL(&odst->natv4addr, &iph2->ip_src));
966 if (nat464_translate_icmp_ip(pbuf, ip2off, &tot_len,
967 &hlen2, iph2->ip_p, iph2->ip_ttl, tot_len2,
968 (struct nat464_addr *)ndst, (struct nat464_addr *)nsrc,
969 PF_INET, PF_INET6) != 0) {
970 return NT_DROP;
971 }
972 /* Update total length/payload length for outer header */
973 switch (naf) {
974 case PF_INET:
975 iph->ip_len = htons(tot_len);
976 break;
977 case PF_INET6:
978 ip6h->ip6_plen = htons(tot_len - hlen);
979 break;
980 }
981 iph2 = NULL;
982 }
983
984 icmp6h->icmp6_cksum = 0;
985 icmp6h->icmp6_cksum = pbuf_inet6_cksum(pbuf, IPPROTO_ICMPV6, hlen,
986 ntohs(ip6h->ip6_plen));
987
988 clat_log2((LOG_DEBUG, "%s translated to ICMPV6 type: %d "
989 "code: %d checksum: %#x \n", __func__, icmp6h->icmp6_type,
990 icmp6h->icmp6_code, icmp6h->icmp6_cksum));
991
992 icmph = NULL;
993 icmp6h = NULL;
994 break;
995 }
996 case IPPROTO_ICMPV6:
997 { if (naf != PF_INET) { /* allow only v4 as naf for ICMPV6 */
998 return NT_DROP;
999 }
1000
1001 struct icmp6_hdr *__single icmp6h = NULL;
1002 struct icmp *__single icmph = NULL;
1003 uint16_t ip2off = 0, hlen2 = 0, tot_len2 = 0;
1004
1005 icmp6h = (struct icmp6_hdr*) pbuf_contig_segment(pbuf, hlen,
1006 sizeof(*icmp6h));
1007 if (icmp6h == NULL) {
1008 return NT_DROP;
1009 }
1010
1011 /* Translate the ICMP header */
1012 if (nat464_translate_icmp(PF_INET, icmp6h) != 0) {
1013 return NT_DROP;
1014 }
1015
1016 *proto = IPPROTO_ICMP;
1017 icmph = (struct icmp *__single)(void *)icmp6h;
1018 pbuf_copy_back(pbuf, hlen, ICMP_MINLEN,
1019 icmph, sizeof(*icmph));
1020
1021 /*Translate the inner IP header only for error messages */
1022 if (ICMP_ERRORTYPE(icmph->icmp_type)) {
1023 ip2off = hlen + ICMP_MINLEN;
1024 struct ip6_hdr *iph2 = NULL;
1025 iph2 = (struct ip6_hdr*) pbuf_contig_segment(pbuf, ip2off,
1026 sizeof(*iph2));
1027 if (iph2 == NULL) {
1028 return NT_DROP;
1029 }
1030
1031 /* hlen2 points to end of inner IP header from the beginning */
1032 hlen2 = ip2off + sizeof(struct ip6_hdr);
1033 tot_len2 = ntohs(iph2->ip6_plen) + sizeof(struct ip6_hdr);
1034
1035 if (nat464_translate_icmp_ip(pbuf, ip2off, &tot_len,
1036 &hlen2, iph2->ip6_nxt, iph2->ip6_hlim, tot_len2,
1037 (struct nat464_addr *)ndst, (struct nat464_addr *)nsrc,
1038 PF_INET6, PF_INET) != 0) {
1039 return NT_DROP;
1040 }
1041
1042 /* Update total length for outer header */
1043 switch (naf) {
1044 case PF_INET:
1045 iph->ip_len = htons(tot_len);
1046 break;
1047 case PF_INET6:
1048 ip6h->ip6_plen = htons(tot_len - hlen);
1049 break;
1050 }
1051 iph2 = NULL;
1052 }
1053 /* Recalculate IP checksum as some IP fields might have changed */
1054 iph->ip_sum = 0;
1055 iph->ip_sum = pbuf_inet_cksum(pbuf, 0, 0, iph->ip_hl << 2);
1056 icmph->icmp_cksum = 0;
1057 icmph->icmp_cksum = pbuf_inet_cksum(pbuf, 0, hlen,
1058 ntohs(iph->ip_len) - hlen);
1059
1060 clat_log2((LOG_DEBUG, "%s translated to ICMP type: %d "
1061 "code: %d checksum: %#x \n", __func__, icmph->icmp_type,
1062 icmph->icmp_code, icmph->icmp_cksum));
1063
1064 icmp6h = NULL;
1065 icmph = NULL;
1066 break;}
1067
1068 /*
1069 * https://tools.ietf.org/html/rfc7915#section-5.1.1
1070 * If the Next Header field of the Fragment Header is an
1071 * extension header (except ESP, but including the Authentication
1072 * Header (AH)), then the packet SHOULD be dropped and logged.
1073 */
1074 case IPPROTO_HOPOPTS:
1075 case IPPROTO_ROUTING:
1076 case IPPROTO_DSTOPTS:
1077 case IPPROTO_AH:
1078 return NT_DROP;
1079
1080 case IPPROTO_FRAGMENT:
1081 /*
1082 * The fragment header is appended after or removed before
1083 * calling into this routine.
1084 */
1085 VERIFY(FALSE);
1086 case IPPROTO_ESP:
1087 break;
1088
1089 default:
1090 return NT_DROP;
1091 }
1092
1093 done:
1094 /* Update checksum flags and offsets based on direction */
1095 if (direction == NT_OUT) {
1096 if ((*pbuf->pb_csum_flags & (CSUM_DATA_VALID | CSUM_PARTIAL)) ==
1097 (CSUM_DATA_VALID | CSUM_PARTIAL)) {
1098 (pbuf->pb_mbuf)->m_pkthdr.csum_tx_start += CLAT46_HDR_EXPANSION_OVERHD;
1099 (pbuf->pb_mbuf)->m_pkthdr.csum_tx_stuff += CLAT46_HDR_EXPANSION_OVERHD;
1100 }
1101
1102 if (*pbuf->pb_csum_flags & CSUM_TCP) {
1103 *pbuf->pb_csum_flags |= CSUM_TCPIPV6;
1104 }
1105 if (*pbuf->pb_csum_flags & CSUM_UDP) {
1106 *pbuf->pb_csum_flags |= CSUM_UDPIPV6;
1107 }
1108 if (*pbuf->pb_csum_flags & CSUM_FRAGMENT) {
1109 *pbuf->pb_csum_flags |= CSUM_FRAGMENT_IPV6;
1110 }
1111
1112 /* Clear IPv4 checksum flags */
1113 *pbuf->pb_csum_flags &= ~(CSUM_IP | CSUM_IP_FRAGS | CSUM_DELAY_DATA | CSUM_FRAGMENT);
1114 /*
1115 * If the packet requires TCP segmentation due to TSO offload,
1116 * then change the checksum flag to indicate that an IPv6
1117 * TCP segmentation is needed now.
1118 */
1119 if (*pbuf->pb_csum_flags & CSUM_TSO_IPV4) {
1120 *pbuf->pb_csum_flags &= ~CSUM_TSO_IPV4;
1121 *pbuf->pb_csum_flags |= CSUM_TSO_IPV6;
1122 }
1123 } else if (direction == NT_IN) {
1124 /* XXX On input just reset csum flags */
1125 *pbuf->pb_csum_flags = 0; /* Reset all flags for now */
1126 #if 0
1127 /* Update csum flags and offsets for rx */
1128 if (*pbuf->pb_csum_flags & CSUM_PARTIAL) {
1129 (pbuf->pb_mbuf)->m_pkthdr.csum_rx_start -= CLAT46_HDR_EXPANSION_OVERHD;
1130 }
1131 #endif
1132 }
1133 return NT_NAT64;
1134 }
1135
1136 /* Fix the proto checksum for address change */
1137 static void
nat464_addr_cksum_fixup(uint16_t * pc,struct nat464_addr * ao,struct nat464_addr * an,protocol_family_t af,protocol_family_t naf,uint8_t u,boolean_t do_ones_complement)1138 nat464_addr_cksum_fixup(uint16_t *pc, struct nat464_addr *ao, struct nat464_addr *an,
1139 protocol_family_t af, protocol_family_t naf, uint8_t u, boolean_t do_ones_complement)
1140 {
1141 /* Currently we only support v4 to v6 and vice versa */
1142 VERIFY(af != naf);
1143
1144 switch (af) {
1145 case PF_INET:
1146 switch (naf) {
1147 case PF_INET6:
1148 if (do_ones_complement) {
1149 *pc = ~nat464_cksum_fixup(nat464_cksum_fixup(
1150 nat464_cksum_fixup(nat464_cksum_fixup(nat464_cksum_fixup(
1151 nat464_cksum_fixup(nat464_cksum_fixup(nat464_cksum_fixup(~*pc,
1152 ao->nataddr16[0], an->nataddr16[0], u),
1153 ao->nataddr16[1], an->nataddr16[1], u),
1154 0, an->nataddr16[2], u),
1155 0, an->nataddr16[3], u),
1156 0, an->nataddr16[4], u),
1157 0, an->nataddr16[5], u),
1158 0, an->nataddr16[6], u),
1159 0, an->nataddr16[7], u);
1160 } else {
1161 *pc = nat464_cksum_fixup(nat464_cksum_fixup(
1162 nat464_cksum_fixup(nat464_cksum_fixup(nat464_cksum_fixup(
1163 nat464_cksum_fixup(nat464_cksum_fixup(nat464_cksum_fixup(*pc,
1164 ao->nataddr16[0], an->nataddr16[0], u),
1165 ao->nataddr16[1], an->nataddr16[1], u),
1166 0, an->nataddr16[2], u),
1167 0, an->nataddr16[3], u),
1168 0, an->nataddr16[4], u),
1169 0, an->nataddr16[5], u),
1170 0, an->nataddr16[6], u),
1171 0, an->nataddr16[7], u);
1172 }
1173 break;
1174 }
1175 break;
1176 case PF_INET6:
1177 /*
1178 * XXX For NAT464 this only applies to the incoming path.
1179 * The checksum therefore is already ones complemented.
1180 * Therefore we just perform normal fixup.
1181 */
1182 switch (naf) {
1183 case PF_INET:
1184 *pc = nat464_cksum_fixup(nat464_cksum_fixup(
1185 nat464_cksum_fixup(nat464_cksum_fixup(nat464_cksum_fixup(
1186 nat464_cksum_fixup(nat464_cksum_fixup(nat464_cksum_fixup(*pc,
1187 ao->nataddr16[0], an->nataddr16[0], u),
1188 ao->nataddr16[1], an->nataddr16[1], u),
1189 ao->nataddr16[2], 0, u),
1190 ao->nataddr16[3], 0, u),
1191 ao->nataddr16[4], 0, u),
1192 ao->nataddr16[5], 0, u),
1193 ao->nataddr16[6], 0, u),
1194 ao->nataddr16[7], 0, u);
1195 break;
1196 }
1197 break;
1198 }
1199 }
1200
1201 uint16_t
nat464_cksum_fixup(uint16_t cksum,uint16_t old,uint16_t new,uint8_t udp)1202 nat464_cksum_fixup(uint16_t cksum, uint16_t old, uint16_t new, uint8_t udp)
1203 {
1204 uint32_t l;
1205
1206 if (udp && !cksum) {
1207 return 0;
1208 }
1209 l = cksum + old - new;
1210 l = (l >> 16) + (l & 0xffff);
1211 l = l & 0xffff;
1212 if (udp && !l) {
1213 return 0xffff;
1214 }
1215 return (uint16_t)l;
1216 }
1217
1218 /* CLAT46 event handlers */
1219 void
in6_clat46_eventhdlr_callback(struct eventhandler_entry_arg arg0 __unused,in6_clat46_evhdlr_code_t in6_clat46_ev_code,pid_t epid,uuid_t euuid)1220 in6_clat46_eventhdlr_callback(struct eventhandler_entry_arg arg0 __unused,
1221 in6_clat46_evhdlr_code_t in6_clat46_ev_code, pid_t epid, uuid_t euuid)
1222 {
1223 struct kev_msg ev_msg;
1224 struct kev_netevent_clat46_data clat46_event_data;
1225
1226 bzero(&ev_msg, sizeof(ev_msg));
1227 bzero(&clat46_event_data, sizeof(clat46_event_data));
1228
1229 ev_msg.vendor_code = KEV_VENDOR_APPLE;
1230 ev_msg.kev_class = KEV_NETWORK_CLASS;
1231 ev_msg.kev_subclass = KEV_NETEVENT_SUBCLASS;
1232 ev_msg.event_code = KEV_NETEVENT_CLAT46_EVENT;
1233
1234 bzero(&clat46_event_data, sizeof(clat46_event_data));
1235 clat46_event_data.clat46_event_code = in6_clat46_ev_code;
1236 clat46_event_data.epid = epid;
1237 uuid_copy(clat46_event_data.euuid, euuid);
1238
1239 ev_msg.dv[0].data_ptr = &clat46_event_data;
1240 ev_msg.dv[0].data_length = sizeof(clat46_event_data);
1241
1242 kev_post_msg(&ev_msg);
1243 }
1244
1245 struct in6_clat46_event_nwk_wq_entry {
1246 struct nwk_wq_entry nwk_wqe;
1247 struct kev_netevent_clat46_data in6_clat46_ev_arg;
1248 };
1249
1250 static void
in6_clat46_event_callback(struct nwk_wq_entry * nwk_item)1251 in6_clat46_event_callback(struct nwk_wq_entry *nwk_item)
1252 {
1253 struct in6_clat46_event_nwk_wq_entry *p_ev;
1254
1255 p_ev = __container_of(nwk_item,
1256 struct in6_clat46_event_nwk_wq_entry, nwk_wqe);
1257
1258 EVENTHANDLER_INVOKE(&in6_clat46_evhdlr_ctxt, in6_clat46_event,
1259 p_ev->in6_clat46_ev_arg.clat46_event_code, p_ev->in6_clat46_ev_arg.epid,
1260 p_ev->in6_clat46_ev_arg.euuid);
1261
1262 kfree_type(struct in6_clat46_event_nwk_wq_entry, p_ev);
1263 }
1264
1265 void
in6_clat46_event_enqueue_nwk_wq_entry(in6_clat46_evhdlr_code_t in6_clat46_event_code,pid_t epid,uuid_t euuid)1266 in6_clat46_event_enqueue_nwk_wq_entry(in6_clat46_evhdlr_code_t in6_clat46_event_code,
1267 pid_t epid, uuid_t euuid)
1268 {
1269 struct in6_clat46_event_nwk_wq_entry *p_ev = NULL;
1270
1271 p_ev = kalloc_type(struct in6_clat46_event_nwk_wq_entry,
1272 Z_WAITOK | Z_ZERO | Z_NOFAIL);
1273
1274 p_ev->nwk_wqe.func = in6_clat46_event_callback;
1275 p_ev->in6_clat46_ev_arg.clat46_event_code = in6_clat46_event_code;
1276 p_ev->in6_clat46_ev_arg.epid = epid;
1277 uuid_copy(p_ev->in6_clat46_ev_arg.euuid, euuid);
1278
1279 evhlog(debug, "%s: eventhandler enqueuing event of type=in6_clat46_event event_code=%s",
1280 __func__, in6_clat46_evhdlr_code2str(in6_clat46_event_code));
1281
1282 nwk_wq_enqueue(&p_ev->nwk_wqe);
1283 }
1284
1285 extern const char*
in6_clat46_evhdlr_code2str(enum in6_clat46_evhdlr_code_t code)1286 in6_clat46_evhdlr_code2str(enum in6_clat46_evhdlr_code_t code)
1287 {
1288 switch (code) {
1289 #define CLAT46_CODE_TO_STRING(type) case type: return #type;
1290 CLAT46_CODE_TO_STRING(IN6_CLAT46_EVENT_V4_FLOW)
1291 CLAT46_CODE_TO_STRING(IN6_CLAT46_EVENT_V6_ADDR_CONFFAIL)
1292 #undef CLAT46_CODE_TO_STRING
1293 }
1294 return "UNKNOWN_IN6_CLAT46_EVHDLR_CODE";
1295 }
1296