/
natpt_trans.c
2886 lines (2354 loc) · 67.8 KB
/
natpt_trans.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* $KAME: natpt_trans.c,v 1.97 2002/04/18 15:28:38 fujisawa Exp $ */
/*
* Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000 and 2001 WIDE Project.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the project nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#ifdef __FreeBSD__
#include "opt_natpt.h"
#endif
#include <sys/param.h>
#include <sys/ctype.h>
#include <sys/libkern.h>
#include <sys/malloc.h>
#include <sys/mbuf.h>
#include <sys/proc.h>
#include <sys/queue.h>
#include <sys/socket.h>
#include <sys/syslog.h>
#include <net/route.h> /* for <netinet6/ip6_var.h> */
#include <netinet/in.h>
#include <netinet/in_systm.h>
#include <netinet/ip.h>
#include <netinet/ip_icmp.h>
#include <netinet/ip_var.h>
#include <netinet/ip6.h>
#include <netinet/icmp6.h>
#include <netinet/tcp.h>
#include <netinet/tcp_fsm.h>
#include <netinet/udp.h>
#include <netinet6/ip6_var.h> /* for ip6_forward() */
#include <netinet6/natpt_defs.h>
#include <netinet6/natpt_log.h>
#include <netinet6/natpt_var.h>
/*
*
*/
/* for backward compatibility */
#define HAVE_IP6_SETPKTADDRS (1)
#ifndef MLD_LISTENER_QUERY
#define MLD_LISTENER_QUERY MLD6_LISTENER_QUERY
#define MLD_LISTENER_REPORT MLD6_LISTENER_REPORT
#define MLD_LISTENER_DONE MLD6_LISTENER_DONE
#undef HAVE_IP6_SETPKTADDRS
#endif
#define FTP_DATA 20
#define FTP_CONTROL 21
#define TFTP 69
#if BYTE_ORDER == BIG_ENDIAN
#define FTP4_PORT 0x504f5254
#define FTP6_LPSV 0x4c505356
#define FTP6_LPRT 0x4c505254
#define FTP6_EPRT 0x45505254
#define FTP6_EPSV 0x45505356
#else
#define FTP4_PORT 0x54524f50
#define FTP6_LPSV 0x5653504c
#define FTP6_LPRT 0x5452504c
#define FTP6_EPRT 0x54525045
#define FTP6_EPSV 0x56535045
#endif
#define FTPMINCMD "CWD\r\n"
#define FTPMINCMDLEN strlen(FTPMINCMD)
#define FTPS_PORT 1
#define FTPS_LPRT 2
#define FTPS_LPSV 3
#define FTPS_EPRT 4
#define FTPS_EPSV 5
struct pseudohdr {
struct in_addr ip_src, ip_dst;
u_int16_t ip_p;
u_int16_t ip_len;
};
struct ftpparam {
u_long cmd;
caddr_t arg; /* argument in mbuf if exist */
caddr_t argend;
struct sockaddr *sa; /* allocated */
};
#define PSEUDOHDRSZ 40 /* sizeof pseudo-header */
struct ulc6 {
struct in6_addr ulc_src, ulc_dst;
u_long ulc_len;
u_char ulc_zero[3];
u_char ulc_pr;
union {
struct icmp6_hdr ih;
struct tcphdr th;
struct udphdr uh;
} ulc_tu;
};
struct ulc4 {
struct in_addr ulc_src, ulc_dst;
u_char ulc_zero;
u_char ulc_pr;
u_short ulc_len;
union {
struct tcphdr th;
struct udphdr uh;
} ulc_tu;
};
extern int udpcksum; /* defined in netinet/udp_usrreq.c */
#ifdef __FreeBSD__
MALLOC_DECLARE(M_NATPT);
#endif
/*
*
*/
/* for fujisawa's convenience */
/* struct mbuf *natpt_translateIPv6To4 */
/* struct mbuf *natpt_translateIPv4To6 */
/* struct mbuf *natpt_translateIPv4To4 */
/* IPv6 -> IPv4 */
struct mbuf *natpt_translateICMPv6To4 __P((struct pcv *, struct pAddr *));
void natpt_icmp6DstUnreach __P((struct pcv *, struct pcv *));
void natpt_icmp6PacketTooBig __P((struct pcv *, struct pcv *));
void natpt_icmp6TimeExceed __P((struct pcv *, struct pcv *));
void natpt_icmp6ParamProb __P((struct pcv *, struct pcv *));
void natpt_icmp6EchoRequest __P((struct pcv *, struct pcv *));
void natpt_icmp6EchoReply __P((struct pcv *, struct pcv *));
void natpt_icmp6MimicPayload __P((struct pcv *, struct pcv *,
struct pAddr *));
void natpt_translatePing4to66 __P((struct pcv *, struct pcv *, int));
struct mbuf *natpt_translateTCPv6To4 __P((struct pcv *, struct pAddr *));
struct mbuf *natpt_translateUDPv6To4 __P((struct pcv *, struct pAddr *));
struct mbuf *natpt_translateTCPUDPv6To4 __P((struct pcv *, struct pAddr *,
struct pcv *));
void natpt_translatePYLD6To4 __P((struct pcv *));
void natpt_watchUDP6 __P((struct pcv *));
/* IPv4 -> IPv6 */
struct mbuf *natpt_translateICMPv4To6 __P((struct pcv *, struct pAddr *));
int natpt_icmp4EchoReply __P((struct pcv *, struct pcv *));
void natpt_icmp4Unreach __P((struct pcv *, struct pcv *,
struct pAddr *));
int natpt_icmp4Echo __P((struct pcv *, struct pcv *));
void natpt_icmp4Timxceed __P((struct pcv *, struct pcv *,
struct pAddr *));
void natpt_icmp4Paramprob __P((struct pcv *, struct pcv *));
int natpt_icmp4MimicPayload __P((struct pcv *, struct pcv *,
struct pAddr *));
struct mbuf *natpt_translateTCPv4To6 __P((struct pcv *, struct pAddr *));
struct mbuf *natpt_translateUDPv4To6 __P((struct pcv *, struct pAddr *));
struct mbuf *natpt_translateUNKNOWNv4To6 __P((struct pcv *, struct pAddr *));
struct mbuf *natpt_translateTCPUDPv4To6 __P((struct pcv *, struct pAddr *,
struct pcv *));
void natpt_translatePYLD4To6 __P((struct pcv *));
void natpt_translateFragment4to66 __P((struct pcv *, struct pAddr *));
void natpt_sendFragmentedTail __P((struct pcv *, struct ip6_hdr *, struct ip6_frag *));
void natpt_ip6_forward __P((struct mbuf *));
/* IPv4 -> IPv4 */
struct mbuf *natpt_translateICMPv4To4 __P((struct pcv *, struct pAddr *));
void natpt_icmp4TimeExceed __P((struct pcv *, struct pAddr *));
struct mbuf *natpt_translateTCPv4To4 __P((struct pcv *, struct pAddr *));
struct mbuf *natpt_translateUDPv4To4 __P((struct pcv *, struct pAddr *));
struct mbuf *natpt_translateTCPUDPv4To4 __P((struct pcv *, struct pAddr *,
struct pcv *));
void natpt_translatePYLD4To4 __P((struct pcv *));
/* FTP translation */
int natpt_translateFTP6CommandTo4 __P((struct pcv *));
int natpt_translateFTP4ReplyTo6 __P((struct pcv *));
struct ftpparam *natpt_parseFTPdialogue __P((caddr_t, caddr_t, struct ftpparam *));
struct sockaddr *natpt_parseLPRT __P((caddr_t, caddr_t, struct sockaddr_in6 *));
struct sockaddr *natpt_parseEPRT __P((caddr_t, caddr_t, struct sockaddr_in6 *));
struct sockaddr *natpt_parsePORT __P((caddr_t, caddr_t, struct sockaddr_in *));
struct sockaddr *natpt_parse227 __P((caddr_t, caddr_t, struct sockaddr_in *));
int natpt_pton6 __P((caddr_t, caddr_t, struct in6_addr *));
int natpt_rewriteMbuf __P((struct mbuf *, char *, int, char *,int));
void natpt_incrementSeq __P((struct tcphdr *, int));
void natpt_decrementAck __P((struct tcphdr *, int));
/* */
int natpt_updateTcpStatus __P((struct pcv *));
int natpt_tcpfsm __P((short state, int, u_char flags));
struct mbuf *natpt_mgethdr __P((int, int));
struct ip6_frag *natpt_composeIPv6Hdr __P((struct pcv *, struct pAddr *,
struct ip6_hdr *));
void natpt_composeIPv4Hdr __P((struct pcv *, struct pAddr *,
struct ip *));
void natpt_adjustMBuf __P((struct mbuf *, struct mbuf *));
void natpt_fixTCPUDP64cksum __P((int, int, struct pcv *, struct pcv *));
void natpt_fixTCPUDP44cksum __P((int, struct pcv *, struct pcv *));
int natpt_fixCksum __P((int, u_char *, int, u_char *, int));
/*
* Translate from IPv6 to IPv4
*/
struct mbuf *
natpt_translateIPv6To4(struct pcv *cv6, struct pAddr *pad)
{
const char *fn = __FUNCTION__;
struct timeval atv;
struct mbuf *m4 = NULL;
if (isDump(D_TRANSLATEIPV6))
natpt_logIp6(LOG_DEBUG, cv6->ip.ip6, "%s():", fn);
microtime(&atv);
cv6->ats->tstamp = atv.tv_sec;
switch (cv6->ip_p) {
case IPPROTO_ICMPV6:
m4 = natpt_translateICMPv6To4(cv6, pad);
break;
case IPPROTO_TCP:
m4 = natpt_translateTCPv6To4(cv6, pad);
break;
case IPPROTO_UDP:
m4 = natpt_translateUDPv6To4(cv6, pad);
break;
}
if (m4)
natpt_adjustMBuf(cv6->m, m4);
return (m4);
}
struct mbuf *
natpt_translateICMPv6To4(struct pcv *cv6, struct pAddr *pad)
{
struct pcv cv4;
struct mbuf *m4;
struct ip *ip4;
struct ip6_hdr *ip6 = mtod(cv6->m, struct ip6_hdr *);
struct icmp *icmp4;
struct icmp6_hdr *icmp6;
caddr_t icmp6end = (caddr_t)ip6 + cv6->m->m_pkthdr.len;
int icmp6len = icmp6end - (caddr_t)cv6->pyld.icmp6;
if ((m4 = natpt_mgethdr(sizeof(struct ip), icmp6len)) == NULL)
return (NULL);
cv4.m = m4;
cv4.ip.ip4 = ip4 = mtod(m4, struct ip *);
cv4.pyld.caddr = (caddr_t)cv4.ip.ip4 + sizeof(struct ip);
cv4.fromto = cv6->fromto;
natpt_composeIPv4Hdr(cv6, pad, ip4);
switch (cv6->pyld.icmp6->icmp6_type) {
case ICMP6_DST_UNREACH:
natpt_icmp6DstUnreach(cv6, &cv4);
natpt_icmp6MimicPayload(cv6, &cv4, pad);
break;
case ICMP6_PACKET_TOO_BIG:
natpt_icmp6PacketTooBig(cv6, &cv4);
break;
case ICMP6_TIME_EXCEEDED:
natpt_icmp6TimeExceed(cv6, &cv4);
natpt_icmp6MimicPayload(cv6, &cv4, pad);
break;
case ICMP6_PARAM_PROB:
natpt_icmp6ParamProb(cv6, &cv4);
break;
case ICMP6_ECHO_REQUEST:
natpt_icmp6EchoRequest(cv6, &cv4);
break;
case ICMP6_ECHO_REPLY:
natpt_icmp6EchoReply(cv6, &cv4);
break;
case MLD_LISTENER_QUERY:
case MLD_LISTENER_REPORT:
case MLD_LISTENER_DONE:
m_freem(m4); /* Single hop message. Silently drop. */
return (NULL);
default:
m_freem(m4); /* Silently drop. */
return (NULL);
}
icmp4 = cv4.pyld.icmp4;
icmp6 = cv6->pyld.icmp6;
icmp4->icmp_id = icmp6->icmp6_id;
icmp4->icmp_seq = icmp6->icmp6_seq;
if ((cv6->fh != NULL)
&& (cv6->pyld.icmp6->icmp6_type != ICMP6_DST_UNREACH)
&& (cv6->pyld.icmp6->icmp6_type != ICMP6_TIME_EXCEEDED)) {
u_short cksum6, cksum4;
struct ulc6 ulc6;
struct icmp6_hdr *icmp6hdr;
struct icmp icmp4hdr;
bzero(&ulc6, sizeof(struct ulc6));
ulc6.ulc_src = cv6->ip.ip6->ip6_src;
ulc6.ulc_dst = cv6->ip.ip6->ip6_dst;
ulc6.ulc_len = htonl(cv6->plen);
ulc6.ulc_pr = cv6->ip_p;
icmp6hdr = (struct icmp6_hdr *)&ulc6.ulc_tu.ih;
bcopy(cv6->pyld.icmp6, icmp6hdr, sizeof(struct icmp6_hdr));
bcopy(cv4.pyld.icmp4, &icmp4hdr, sizeof(struct icmp));
cksum6 = ntohs(icmp6hdr->icmp6_cksum);
icmp6hdr->icmp6_cksum = 0;
icmp4hdr.icmp_cksum = 0;
cksum4 = natpt_fixCksum(cksum6,
(u_char *)&ulc6, PSEUDOHDRSZ + ICMP_MINLEN,
(u_char *)&icmp4hdr, ICMP_MINLEN);
cv4.pyld.icmp4->icmp_cksum = htons(cksum4);
} else {
int hlen;
struct mbuf *m4 = cv4.m;
struct ip *ip4 = cv4.ip.ip4;
struct icmp *icmp4;
#ifdef _IP_VHL
hlen = IP_VHL_HL(ip->ip_vhl) << 2;
#else
hlen = ip4->ip_hl << 2;
#endif
m4->m_data += hlen;
m4->m_len -= hlen;
icmp4 = cv4.pyld.icmp4;
icmp4->icmp_cksum = 0;
icmp4->icmp_cksum = in_cksum(cv4.m, ip4->ip_len - hlen);
m4->m_data -= hlen;
m4->m_len += hlen;
}
return (m4);
}
void
natpt_icmp6DstUnreach(struct pcv *cv6, struct pcv *cv4)
{
struct icmp *icmp4 = cv4->pyld.icmp4;
struct icmp6_hdr *icmp6 = cv6->pyld.icmp6;
icmp4->icmp_type = ICMP_UNREACH;
icmp4->icmp_code = 0;
switch (icmp6->icmp6_code) {
case ICMP6_DST_UNREACH_NOROUTE:
icmp4->icmp_code = ICMP_UNREACH_HOST;
break;
case ICMP6_DST_UNREACH_ADMIN:
icmp4->icmp_code = ICMP_UNREACH_HOST_PROHIB;
break;
case ICMP6_DST_UNREACH_NOTNEIGHBOR:
icmp4->icmp_code = ICMP_UNREACH_SRCFAIL;
break;
case ICMP6_DST_UNREACH_ADDR:
icmp4->icmp_code = ICMP_UNREACH_HOST;
break;
case ICMP6_DST_UNREACH_NOPORT:
icmp4->icmp_code = ICMP_UNREACH_PORT;
break;
}
}
void
natpt_icmp6PacketTooBig(struct pcv *cv6, struct pcv *cv4)
{
struct icmp *icmp4 = cv4->pyld.icmp4;
icmp4->icmp_type = ICMP_UNREACH;
icmp4->icmp_code = ICMP_UNREACH_NEEDFRAG; /* do more */
}
void
natpt_icmp6TimeExceed(struct pcv *cv6, struct pcv *cv4)
{
struct icmp *icmp4 = cv4->pyld.icmp4;
struct icmp6_hdr *icmp6 = cv6->pyld.icmp6;
icmp4->icmp_type = ICMP_TIMXCEED;
icmp4->icmp_code = icmp6->icmp6_code; /* code unchanged */
}
void
natpt_icmp6ParamProb(struct pcv *cv6, struct pcv *cv4)
{
struct icmp *icmp4 = cv4->pyld.icmp4;
struct icmp6_hdr *icmp6 = cv6->pyld.icmp6;
icmp4->icmp_type = ICMP_PARAMPROB; /* do more */
icmp4->icmp_code = 0;
if (icmp6->icmp6_code == ICMP6_PARAMPROB_NEXTHEADER) {
icmp4->icmp_type = ICMP_UNREACH;
icmp4->icmp_code = ICMP_UNREACH_PROTOCOL;
}
}
void
natpt_icmp6EchoRequest(struct pcv *cv6, struct pcv *cv4)
{
struct icmp *icmp4 = cv4->pyld.icmp4;
icmp4->icmp_type = ICMP_ECHO;
icmp4->icmp_code = 0;
{
int dlen;
struct ip *ip4 = cv4->ip.ip4;
struct ip6_hdr *ip6 = cv6->ip.ip6;
caddr_t icmp6off, icmp4off;
caddr_t icmp6end = (caddr_t)ip6 + cv6->m->m_pkthdr.len;
int icmp6len = icmp6end - (caddr_t)cv6->pyld.icmp6;
dlen = icmp6len - sizeof(struct icmp6_hdr);
icmp6off = (caddr_t)(cv6->pyld.icmp6) + sizeof(struct icmp6_hdr);
icmp4off = (caddr_t)(cv4->pyld.icmp4) + ICMP_MINLEN;
bcopy(icmp6off, icmp4off, dlen);
ip4->ip_len = cv4->m->m_len = sizeof(struct ip) + ICMP_MINLEN + dlen;
}
}
void
natpt_icmp6EchoReply(struct pcv *cv6, struct pcv *cv4)
{
struct icmp *icmp4 = cv4->pyld.icmp4;
icmp4->icmp_type = ICMP_ECHOREPLY;
icmp4->icmp_code = 0;
{
int dlen;
struct ip *ip4 = cv4->ip.ip4;
struct ip6_hdr *ip6 = cv6->ip.ip6;
caddr_t icmp6off, icmp4off;
caddr_t icmp6end = (caddr_t)ip6 + cv6->m->m_pkthdr.len;
int icmp6len = icmp6end - (caddr_t)cv6->pyld.icmp6;
dlen = icmp6len - sizeof(struct icmp6_hdr);
icmp6off = (caddr_t)(cv6->pyld.icmp6) + sizeof(struct icmp6_hdr);
icmp4off = (caddr_t)(cv4->pyld.icmp4) + ICMP_MINLEN;
bcopy(icmp6off, icmp4off, dlen);
ip4->ip_len = cv4->m->m_len = sizeof(struct ip) + ICMP_MINLEN + dlen;
}
}
void
natpt_icmp6MimicPayload(struct pcv *cv6, struct pcv *cv4, struct pAddr *pad)
{
int dgramlen;
struct ip *icmpip4, *ip4 = cv4->ip.ip4;
struct ip6_hdr *icmpip6, *ip6 = cv6->ip.ip6;
struct icmp *icmp4;
struct icmp6_hdr *icmp6;
struct udphdr *udp4;
caddr_t ip6end;
caddr_t icmpip6pyld, icmpip4pyld;
ip6end = (caddr_t)(ip6 + 1) + ntohs(ip6->ip6_plen);
icmp6 = cv6->pyld.icmp6;
icmpip6 = (struct ip6_hdr *)((caddr_t)icmp6 + sizeof(struct icmp6_hdr));
icmpip6pyld = natpt_pyldaddr(icmpip6, ip6end, NULL, NULL);
if (icmpip6pyld == NULL)
return ;
icmp4 = cv4->pyld.icmp4;
icmpip4 = (struct ip *)((caddr_t)icmp4 + ICMP_MINLEN);
icmpip4pyld = (caddr_t)icmpip4 + sizeof(struct ip);
dgramlen = (caddr_t)icmp6 + ntohs(ip6->ip6_plen) - icmpip6pyld;
bzero(icmpip4, sizeof(struct ip));
bcopy(icmpip6pyld, icmpip4pyld, dgramlen);
#ifdef _IP_VHL
icmpip4->ip_vhl = IP_MAKE_VHL(IPVERSION, sizeof(struct ip) >> 2);
#else
icmpip4->ip_v = IPVERSION;
icmpip4->ip_hl = sizeof(struct ip) >> 2;
#endif
icmpip4->ip_tos = 0;
icmpip4->ip_len = ntohs(icmpip6->ip6_plen) + sizeof(struct ip);
icmpip4->ip_id = 0;
icmpip4->ip_off = 0;
icmpip4->ip_ttl = icmpip6->ip6_hlim;
icmpip4->ip_p = icmpip6->ip6_nxt;
icmpip4->ip_src = pad->in4dst;
icmpip4->ip_dst = pad->in4src;
ip4->ip_len = sizeof(struct ip) + ICMP_MINLEN + sizeof(struct ip) + dgramlen;
cv4->m->m_pkthdr.len = cv4->m->m_len = ip4->ip_len;
switch (icmp6->icmp6_type) {
case ICMP6_DST_UNREACH:
case ICMP6_TIME_EXCEEDED:
udp4 = (struct udphdr *)icmpip4pyld;
if ((pad->port[1] != 0) || (pad->port[0] != 0)) {
udp4->uh_sport = pad->port[0];
udp4->uh_dport = pad->port[1];
}
break;
}
}
struct mbuf *
natpt_translateTCPv6To4(struct pcv *cv6, struct pAddr *pad)
{
struct pcv cv4;
struct mbuf *m4;
bzero(&cv4, sizeof(struct pcv));
if ((m4 = natpt_translateTCPUDPv6To4(cv6, pad, &cv4)) == NULL)
return (NULL);
cv4.ip_p = IPPROTO_TCP;
natpt_updateTcpStatus(&cv4);
natpt_translatePYLD6To4(&cv4);
natpt_fixTCPUDP64cksum(AF_INET6, IPPROTO_TCP, cv6, &cv4);
return (m4);
}
struct mbuf *
natpt_translateUDPv6To4(struct pcv *cv6, struct pAddr *pad)
{
struct pcv cv4;
struct mbuf *m4;
bzero(&cv4, sizeof(struct pcv));
if ((m4 = natpt_translateTCPUDPv6To4(cv6, pad, &cv4)) == NULL)
return (NULL);
cv4.ip_p = IPPROTO_UDP;
natpt_watchUDP6(&cv4);
if (udpcksum) {
natpt_fixTCPUDP64cksum(AF_INET6, IPPROTO_UDP, cv6, &cv4);
} else {
cv4.pyld.udp->uh_sum = 0;
}
return (m4);
}
struct mbuf *
natpt_translateTCPUDPv6To4(struct pcv *cv6, struct pAddr *pad, struct pcv *cv4)
{
struct mbuf *m4;
struct ip *ip4;
struct ip6_hdr *ip6;
struct tcphdr *th;
static struct pcvaux aux;
static struct ulc6 ulc;
if ((m4 = m_copym(cv6->m, 0, M_COPYALL, M_NOWAIT)) == NULL) {
return (NULL);
}
/*
* There is a case pointing the same data with m4 and cv6->m
* after m_copym, we need to prepare for incremental checksum
* calculation.
*/
bzero(&aux, sizeof(struct pcvaux));
bzero(&ulc, sizeof(struct ulc6));
ulc.ulc_src = cv6->ip.ip6->ip6_src;
ulc.ulc_dst = cv6->ip.ip6->ip6_dst;
ulc.ulc_len = htonl(cv6->plen);
ulc.ulc_pr = cv6->ip_p;
if (cv6->ip_p == IPPROTO_TCP) {
ulc.ulc_tu.th.th_sport = cv6->pyld.tcp6->th_sport;
ulc.ulc_tu.th.th_dport = cv6->pyld.tcp6->th_dport;
aux.cksum6 = ntohs(cv6->pyld.tcp6->th_sum);
} else {
ulc.ulc_tu.uh.uh_sport = cv6->pyld.udp->uh_sport;
ulc.ulc_tu.uh.uh_dport = cv6->pyld.udp->uh_dport;
aux.cksum6 = ntohs(cv6->pyld.udp->uh_sum);
}
aux.ulc6 = &ulc;
cv6->aux = &aux;
/*
* Start translation
*/
{
int diff;
diff = sizeof(struct ip6_hdr) - sizeof(struct ip);
if (cv6->fh) {
diff += sizeof(struct ip6_frag);
}
m4->m_data += diff;
m4->m_len -= diff;
m4->m_pkthdr.len -= diff;
}
cv4->m = m4;
cv4->plen = cv6->plen;
cv4->poff = sizeof(struct ip);
cv4->ip.ip4 = mtod(m4, struct ip *);
cv4->pyld.caddr = (caddr_t)cv4->ip.ip4 + sizeof(struct ip);
cv4->ats = cv6->ats;
cv4->fromto = cv6->fromto;
ip4 = mtod(m4, struct ip *);
ip6 = mtod(cv6->m, struct ip6_hdr *);
natpt_composeIPv4Hdr(cv6, pad, ip4);
th = (struct tcphdr *)(ip4 + 1);
th->th_sport = pad->port[1];
th->th_dport = pad->port[0];
return (m4);
}
void
natpt_translatePYLD6To4(struct pcv *cv4)
{
int delta = 0;
struct tcphdr *th4 = cv4->pyld.tcp4;
struct tcpstate *ts = NULL;
if (htons(cv4->pyld.tcp4->th_dport) == FTP_CONTROL) {
if ((delta = natpt_translateFTP6CommandTo4(cv4)) != 0) {
struct mbuf *mbf = cv4->m;
struct ip *ip4 = cv4->ip.ip4;
ip4->ip_len += delta;
mbf->m_len += delta;
if (mbf->m_flags & M_PKTHDR)
mbf->m_pkthdr.len += delta;
}
if ((cv4->ats == NULL)
|| ((ts = cv4->ats->suit.tcps) == NULL))
return ;
if (ts->delta[0]
&& (cv4->fromto == NATPT_FROM))
natpt_incrementSeq(th4, ts->delta[0]);
if (ts->delta[1]
&& (th4->th_flags & TH_ACK)
&& (cv4->fromto == NATPT_FROM))
natpt_decrementAck(th4, ts->delta[1]);
if ((delta != 0)
&& ((th4->th_seq != ts->seq[0])
|| (th4->th_ack != ts->ack[0])))
{
ts->delta[0] += delta;
ts->seq[0] = th4->th_seq;
ts->ack[0] = th4->th_ack;
}
}
}
void
natpt_watchUDP6(struct pcv *cv4)
{
struct cSlot *cst;
if (cv4->fromto == NATPT_TO)
return ;
if (htons(cv4->pyld.udp->uh_dport) == TFTP) {
MALLOC(cst, struct cSlot *, sizeof(struct cSlot), M_NATPT, M_NOWAIT);
bzero(cst, sizeof(struct cSlot));
cst->proto = NATPT_UDP;
cst->map = NATPT_REDIRECT_PORT;
cst->lifetime = 32;
cst->Local.sa_family = AF_INET;
cst->Local.in4Addr = cv4->ats->remote.in4src;
cst->Local.port[1] = cv4->ats->remote.port[1];
cst->Local.aType = ADDR_SINGLE;
cst->Remote.sa_family = AF_INET6;
cst->Remote.in6Addr = cv4->ats->local.in6src;
cst->Remote.port[1] = cv4->ats->local.port[0];
cst->Remote.aType = ADDR_SINGLE;
natpt_prependRule(cst);
}
}
struct mbuf *
natpt_translateFragment6(struct pcv *cv6, struct pAddr *pad)
{
struct pcv cv4;
struct mbuf *m4;
struct ip *ip4;
struct ip6_hdr *ip6 = mtod(cv6->m, struct ip6_hdr *);
caddr_t frag6end = (caddr_t)ip6 + cv6->m->m_pkthdr.len;
int frag6len = frag6end - cv6->pyld.caddr;
bzero(&cv4, sizeof(struct pcv));
if ((m4 = natpt_mgethdr(sizeof(struct ip), frag6len)) == NULL)
return (NULL);
cv4.m = m4;
cv4.ip.ip4 = ip4 = mtod(m4, struct ip *);
cv4.pyld.caddr = (caddr_t)cv4.ip.ip4 + sizeof(struct ip);
cv4.fromto = cv6->fromto;
natpt_composeIPv4Hdr(cv6, pad, ip4);
bcopy(cv6->pyld.caddr, cv4.pyld.caddr, frag6len);
cv4.m->m_len = ip4->ip_len;
if (m4)
natpt_adjustMBuf(cv6->m, m4);
return (m4);
}
/*
* Translate from IPv4 to IPv6
*/
struct mbuf *
natpt_translateIPv4To6(struct pcv *cv4, struct pAddr *pad)
{
const char *fn = __FUNCTION__;
struct timeval atv;
struct mbuf *m6 = NULL;
if (isDump(D_TRANSLATEIPV4))
natpt_logIp4(LOG_DEBUG, cv4->ip.ip4, "%s():", fn);
microtime(&atv);
cv4->ats->tstamp = atv.tv_sec;
switch (cv4->ip_p) {
case IPPROTO_ICMP:
m6 = natpt_translateICMPv4To6(cv4, pad);
break;
case IPPROTO_TCP:
m6 = natpt_translateTCPv4To6(cv4, pad);
break;
case IPPROTO_UDP:
m6 = natpt_translateUDPv4To6(cv4, pad);
break;
default:
m6 = natpt_translateUNKNOWNv4To6(cv4, pad);
break;
}
if (m6)
m6->m_pkthdr.rcvif = cv4->m->m_pkthdr.rcvif;
return (m6);
}
struct mbuf *
natpt_translateICMPv4To6(struct pcv *cv4, struct pAddr *pad)
{
struct pcv cv6;
struct mbuf *m6;
struct ip *ip4 = mtod(cv4->m, struct ip *);
struct ip6_hdr *ip6;
struct ip6_frag *frag6;
caddr_t icmp4end;
int icmp4len;
int icmp6len = 0;
icmp4end = (caddr_t)ip4 + cv4->m->m_pkthdr.len;
icmp4len = icmp4end - (caddr_t)cv4->pyld.icmp4;
if ((m6 = natpt_mgethdr(sizeof(struct ip6_hdr), icmp4len)) == NULL)
return (NULL);
cv6.m = m6;
cv6.ip.ip6 = ip6 = mtod(m6, struct ip6_hdr *);
cv6.pyld.caddr = (caddr_t)cv6.ip.ip6 + sizeof(struct ip6_hdr);
cv6.fromto = cv4->fromto;
cv6.flags = cv4->flags;
ip6->ip6_nxt = IPPROTO_ICMPV6;
if ((frag6 = natpt_composeIPv6Hdr(cv4, pad, ip6)) != NULL)
cv6.pyld.caddr += sizeof(struct ip6_frag);
ip6->ip6_src.s6_addr32[0] = natpt_prefix.s6_addr32[0];
ip6->ip6_src.s6_addr32[1] = natpt_prefix.s6_addr32[1];
ip6->ip6_src.s6_addr32[2] = natpt_prefix.s6_addr32[2];
ip6->ip6_src.s6_addr32[3] = ip4->ip_src.s_addr;
cv6.pyld.icmp6->icmp6_data32[0] = 0; /* clear unused (maybe) field */
switch (cv4->pyld.icmp4->icmp_type) {
case ICMP_ECHOREPLY:
icmp6len = natpt_icmp4EchoReply(cv4, &cv6);
if (frag6 && needFragment(cv4)) {
cv6.fh = frag6;
natpt_translatePing4to66(cv4, &cv6, icmp6len);
return (NULL);
}
break;
case ICMP_UNREACH:
natpt_icmp4Unreach(cv4, &cv6, pad);
icmp6len = natpt_icmp4MimicPayload(cv4, &cv6, pad);
break;
case ICMP_ECHO:
icmp6len = natpt_icmp4Echo(cv4, &cv6);
if (frag6 && needFragment(cv4)) {
cv6.fh = frag6;
natpt_translatePing4to66(cv4, &cv6, icmp6len);
return (NULL);
}
break;
case ICMP_TIMXCEED:
natpt_icmp4Timxceed(cv4, &cv6, pad);
icmp6len = natpt_icmp4MimicPayload(cv4, &cv6, pad);
break;
case ICMP_PARAMPROB:
natpt_icmp4Paramprob(cv4, &cv6);
break;
case ICMP_REDIRECT:
case ICMP_ROUTERADVERT:
case ICMP_ROUTERSOLICIT:
m_freem(m6); /* Single hop message. Silently drop */
return (NULL);
case ICMP_SOURCEQUENCH:
case ICMP_TSTAMP:
case ICMP_TSTAMPREPLY:
case ICMP_IREQ:
case ICMP_IREQREPLY:
case ICMP_MASKREQ:
case ICMP_MASKREPLY:
m_freem(m6); /* Obsoleted in ICMPv6. Silently drop */
return (NULL);
default:
m_freem(m6); /* Silently drop */
return (NULL);
}
{
int hdrsz = 0;
u_int32_t off = sizeof(struct ip6_hdr);
u_int32_t len = sizeof(struct icmp6_hdr) + icmp6len;
struct icmp6_hdr *icmp6 = cv6.pyld.icmp6;
if (frag6) {
hdrsz += sizeof(struct ip6_frag);
off += sizeof(struct ip6_frag);
}
ip6->ip6_plen = hdrsz + len;
cv6.m->m_pkthdr.len
= cv6.m->m_len
= sizeof(struct ip6_hdr) + ip6->ip6_plen;
icmp6->icmp6_cksum = 0;
icmp6->icmp6_cksum = in6_cksum(cv6.m, IPPROTO_ICMPV6, off, len);
}
HTONS(ip6->ip6_plen);
return (m6);
}
int
natpt_icmp4EchoReply(struct pcv *cv4, struct pcv *cv6)
{
struct icmp6_hdr *icmp6 = cv6->pyld.icmp6;
icmp6->icmp6_type = ICMP6_ECHO_REPLY;
icmp6->icmp6_code = 0;
icmp6->icmp6_id = cv4->pyld.icmp4->icmp_id;
icmp6->icmp6_seq = cv4->pyld.icmp4->icmp_seq;
{
int dlen;
struct ip *ip4 = cv4->ip.ip4;
caddr_t icmp4off, icmp6off;
caddr_t icmp4end = (caddr_t)ip4 + cv4->m->m_pkthdr.len;
int icmp4len = icmp4end - (caddr_t)cv4->pyld.icmp4;
dlen = icmp4len - ICMP_MINLEN;
icmp4off = (caddr_t)(cv4->pyld.icmp4) + ICMP_MINLEN;
icmp6off = (caddr_t)(cv6->pyld.icmp6) + sizeof(struct icmp6_hdr);
bcopy(icmp4off, icmp6off, dlen);
return (dlen);
}
}
void
natpt_icmp4Unreach(struct pcv *cv4, struct pcv *cv6, struct pAddr *pad)
{
struct icmp *icmp4 = cv4->pyld.icmp4;
struct icmp6_hdr *icmp6 = cv6->pyld.icmp6;
icmp6->icmp6_type = ICMP6_DST_UNREACH;
icmp6->icmp6_code = 0;
switch (icmp4->icmp_code) {
case ICMP_UNREACH_NET:
case ICMP_UNREACH_HOST:
icmp6->icmp6_code = ICMP6_DST_UNREACH_NOROUTE;
break;
case ICMP_UNREACH_PROTOCOL:
icmp6->icmp6_type = ICMP6_PARAM_PROB;
icmp6->icmp6_code = ICMP6_PARAMPROB_NEXTHEADER;
icmp6->icmp6_pptr = htonl(6); /* point to the IPv6 Next Header field. */
break;
case ICMP_UNREACH_PORT: