/
ip6_output.c
5179 lines (4737 loc) · 124 KB
/
ip6_output.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: ip6_output.c,v 1.494 2006/12/04 07:30:30 itojun Exp $ */
/*
* Copyright (c) 2002 INRIA. All rights reserved.
*
* Implementation of Multicast Listener Discovery, Version 2.
* Developed by Hitoshi Asaeda, INRIA, August 2002.
*
* 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 INRIA 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 INSTITUTE 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 INSTITUTE 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.
*/
/*
* Copyright (C) 1995, 1996, 1997, and 1998 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.
*/
/*
* Copyright (c) 1982, 1986, 1988, 1990, 1993
* The Regents of the University of California. 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. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Berkeley and its contributors.
* 4. Neither the name of the University 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 REGENTS 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 REGENTS 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.
*
* @(#)ip_output.c 8.3 (Berkeley) 1/21/94
*/
#ifdef __FreeBSD__
#include "opt_ip6fw.h"
#include "opt_inet.h"
#include "opt_inet6.h"
#include "opt_ipsec.h"
#include "opt_mip6.h"
#endif
#ifdef __NetBSD__
#include "opt_inet.h"
#include "opt_ipsec.h"
#include "opt_mip6.h"
#endif
#ifdef __FreeBSD__
#define NPF 0
#else
#include "pf.h"
#endif
#include <sys/param.h>
#include <sys/malloc.h>
#include <sys/mbuf.h>
#include <sys/errno.h>
#include <sys/protosw.h>
#include <sys/socket.h>
#include <sys/socketvar.h>
#include <sys/systm.h>
#include <sys/syslog.h>
#ifdef __FreeBSD__
#include <sys/kernel.h>
#endif
#include <sys/proc.h>
#include <net/if.h>
#include <net/route.h>
#if defined(__NetBSD__) && defined(PFIL_HOOKS) || defined(__FreeBSD__)
#include <net/pfil.h>
#endif
#include <netinet/in.h>
#include <netinet/in_var.h>
#ifdef __OpenBSD__
#include <netinet/in_systm.h>
#include <netinet/ip.h>
#endif
#include <netinet6/in6_var.h>
#include <netinet/ip6.h>
#include <netinet/icmp6.h>
#include <netinet6/ip6_var.h>
#ifdef __FreeBSD__
#include <netinet/in_pcb.h>
#include <netinet6/in6_pcb.h>
#elif defined(__OpenBSD__)
#include <netinet/in_pcb.h>
#else
#include <netinet6/in6_pcb.h>
#endif
#ifdef MLDV2
#include <netinet6/mld6_var.h>
#include <netinet6/in6_msf.h>
#endif
#include <netinet6/nd6.h>
#include <netinet6/ip6protosw.h>
#include <netinet6/scope6_var.h>
#ifdef __FreeBSD__
#include <netinet/tcp_var.h>
#endif
#if NPF > 0
#include <net/pfvar.h>
#endif
#ifdef IPSEC
#ifdef __OpenBSD__
#include <netinet/ip_ah.h>
#include <netinet/ip_esp.h>
#include <netinet/udp.h>
#include <netinet/tcp.h>
#include <net/pfkeyv2.h>
extern u_int8_t get_sa_require __P((struct inpcb *));
extern int ipsec_auth_default_level;
extern int ipsec_esp_trans_default_level;
extern int ipsec_esp_network_default_level;
extern int ipsec_ipcomp_default_level;
#else
#include <netinet6/ipsec.h>
#include <netkey/key.h>
#endif
#endif /* IPSEC */
#ifndef __OpenBSD__
#include "loop.h"
#endif
#if defined(IPV6FIREWALL) || defined(__FreeBSD__)
#include <netinet6/ip6_fw.h>
#endif
#ifdef MIP6
#include <net/mipsock.h>
#include <netinet6/mip6.h>
#include <netinet6/mip6_var.h>
#include "mip.h"
#if NMIP > 0
#include <netinet/ip6mh.h>
#endif /* NMIP > 0*/
#endif /* MIP6 */
#include <net/net_osdep.h>
#if (defined(__NetBSD__) && defined(PFIL_HOOKS)) || defined(__FreeBSD__)
extern struct pfil_head inet6_pfil_hook; /* XXX */
#endif
#ifndef M_IPMOPTS
static MALLOC_DEFINE(M_IPMOPTS, "ip6_moptions", "internet multicast options");
#endif
struct ip6_exthdrs {
struct mbuf *ip6e_ip6;
struct mbuf *ip6e_hbh;
struct mbuf *ip6e_dest1;
struct mbuf *ip6e_rthdr;
#ifdef MIP6
struct mbuf *ip6e_rthdr2;
#endif /* MIP6 */
#if defined(MIP6) && NMIP > 0
struct mbuf *ip6e_hoa;
#endif /* MIP6 && NMIP > 0 */
struct mbuf *ip6e_dest2;
};
static int ip6_pcbopt __P((int, u_char *, int, struct ip6_pktopts **,
int, int));
#ifdef __FreeBSD__
static int ip6_pcbopts __P((struct ip6_pktopts **, struct mbuf *,
struct socket *, struct sockopt *));
static int ip6_getpcbopt __P((struct ip6_pktopts *, int, struct sockopt *));
#else
static int ip6_pcbopts __P((struct ip6_pktopts **, struct mbuf *,
struct socket *));
static int ip6_getpcbopt __P((struct ip6_pktopts *, int, struct mbuf **));
#endif
static int ip6_setpktopt __P((int, u_char *, int, struct ip6_pktopts *, int,
int, int, int));
#ifdef MLDV2
static int in6_getmopt_ifargs(int, struct ifnet **, struct sockaddr_in6 *, u_int32_t);
static int ip6_getmopt_sgaddr(struct mbuf *, int, struct ifnet **, struct sockaddr_storage *,
struct sockaddr_storage *);
#endif
static int ip6_setmoptions __P((int, struct ip6_moptions **, struct mbuf *));
static int ip6_getmoptions __P((int, struct ip6_moptions *, struct mbuf **));
static int ip6_copyexthdr __P((struct mbuf **, caddr_t, int));
static int ip6_insertfraghdr __P((struct mbuf *, struct mbuf *, int,
struct ip6_frag **));
static int ip6_insert_jumboopt __P((struct ip6_exthdrs *, u_int32_t));
static int ip6_splithdr __P((struct mbuf *, struct ip6_exthdrs *));
#ifdef NEW_STRUCT_ROUTE
static int ip6_getpmtu __P((struct route *, struct route *, struct ifnet *,
struct in6_addr *, u_long *, int *));
#else
static int ip6_getpmtu __P((struct route_in6 *, struct route_in6 *, struct ifnet *,
struct in6_addr *, u_long *, int *));
#endif
static int copypktopts __P((struct ip6_pktopts *, struct ip6_pktopts *, int));
#ifdef __NetBSD__
extern struct ifnet loif[NLOOP];
#endif
/*
* IP6 output. The packet in mbuf chain m contains a skeletal IP6
* header (with pri, len, nxt, hlim, src, dst).
* This function may modify ver and hlim only.
* The mbuf chain containing the packet will be freed.
* The mbuf opt, if present, will not be freed.
*
* type of "mtu": rt_rmx.rmx_mtu is u_long, ifnet.ifr_mtu is int, and
* nd_ifinfo.linkmtu is u_int32_t. so we use u_long to hold largest one,
* which is rt_rmx.rmx_mtu.
*/
int
#ifdef __FreeBSD__
ip6_output(m0, opt, ro, flags, im6o, ifpp, inp)
#elif defined(__NetBSD__)
ip6_output(m0, opt, ro, flags, im6o, so, ifpp)
#else
ip6_output(m0, opt, ro, flags, im6o, ifpp)
#endif
struct mbuf *m0;
struct ip6_pktopts *opt;
#ifdef NEW_STRUCT_ROUTE
struct route *ro;
#else
struct route_in6 *ro;
#endif
int flags;
struct ip6_moptions *im6o;
#ifdef __NetBSD__
struct socket *so;
#endif
struct ifnet **ifpp; /* XXX: just for statistics */
#ifdef __FreeBSD__
struct inpcb *inp;
#endif
{
struct ip6_hdr *ip6, *mhip6;
struct ifnet *ifp, *origifp = NULL;
struct mbuf *m = m0;
int hlen, tlen, len, off;
#ifdef NEW_STRUCT_ROUTE
struct route ip6route;
#else
struct route_in6 ip6route;
#endif
struct rtentry *rt = NULL;
struct sockaddr_in6 *dst, src_sa, dst_sa;
struct in6_addr finaldst, src0, dst0;
int error = 0;
struct in6_ifaddr *ia = NULL;
u_long mtu;
int alwaysfrag, dontfrag;
u_int32_t optlen = 0, plen = 0, unfragpartlen = 0;
struct ip6_exthdrs exthdrs;
int clone = 0;
u_int32_t zone;
#ifdef NEW_STRUCT_ROUTE
struct route *ro_pmtu = NULL;
#else
struct route_in6 *ro_pmtu = NULL;
#endif
int hdrsplit = 0;
#ifdef __OpenBSD__
u_int8_t sproto = 0;
#else
int needipsec = 0;
#endif
#ifdef MIP6
struct mip6_bc_internal *mbc;
#if NMIP > 0
struct mip6_bul_internal *mbul = NULL;
#endif /* MIP6 */
#endif /* NMIP > 0 */
#ifdef IPSEC
#ifdef __OpenBSD__
struct m_tag *mtag;
union sockaddr_union sdst;
struct tdb_ident *tdbi;
u_int32_t sspi;
struct inpcb *inp;
struct tdb *tdb;
int s;
inp = NULL; /* XXX */
if (inp && (inp->inp_flags & INP_IPV6) == 0)
panic("ip6_output: IPv4 pcb is passed");
#else
int needipsectun = 0;
#ifndef __NetBSD__
struct socket *so = NULL;
#endif
struct secpolicy *sp = NULL;
#endif
#endif /* IPSEC || FAST_IPSEC */
ip6 = mtod(m, struct ip6_hdr *);
finaldst = ip6->ip6_dst;
#define MAKE_EXTHDR(hp, mp) \
do { \
if (hp) { \
struct ip6_ext *eh = (struct ip6_ext *)(hp); \
error = ip6_copyexthdr((mp), (caddr_t)(hp), \
((eh)->ip6e_len + 1) << 3); \
if (error) \
goto freehdrs; \
} \
} while (/*CONSTCOND*/ 0)
bzero(&exthdrs, sizeof(exthdrs));
if (opt) {
/* Hop-by-Hop options header */
MAKE_EXTHDR(opt->ip6po_hbh, &exthdrs.ip6e_hbh);
/* Destination options header(1st part) */
if (opt->ip6po_rthdr
#ifdef MIP6
|| opt->ip6po_rthdr2
#endif /* MIP6 */
) {
/*
* Destination options header(1st part)
* This only makes sence with a routing header.
* See Section 9.2 of RFC 3542.
* Disabling this part just for MIP6 convenience is
* a bad idea. We need to think carefully about a
* way to make the advanced API coexist with MIP6
* options, which might automatically be inserted in
* the kernel.
*/
MAKE_EXTHDR(opt->ip6po_dest1, &exthdrs.ip6e_dest1);
}
/* Routing header */
MAKE_EXTHDR(opt->ip6po_rthdr, &exthdrs.ip6e_rthdr);
#ifdef MIP6
/* Type 2 Routing header */
MAKE_EXTHDR(opt->ip6po_rthdr2, &exthdrs.ip6e_rthdr2);
#if NMIP > 0
/* Home Address Destination options header */
MAKE_EXTHDR(opt->ip6po_hoa, &exthdrs.ip6e_hoa);
#endif /* NMIP > 0*/
#endif /* MIP6 */
/* Destination options header(2nd part) */
MAKE_EXTHDR(opt->ip6po_dest2, &exthdrs.ip6e_dest2);
}
#ifdef MIP6
/* Find binding cache entry */
/* XXX need policy to determine bid for MCOA*/
mbc = mip6_bce_get(&ip6->ip6_dst, &ip6->ip6_src, NULL, 0);
/*
* If a node has a corresponding binding cache, put a Type 2
* Routing Header to directly deliver the packet. Except, a
* caller didn't specify a Type 2 Routing Header explicitly.
*/
if ((mbc != NULL) && (ip6->ip6_nxt != IPPROTO_MH) &&
(exthdrs.ip6e_rthdr2 == NULL)) {
struct ip6_rthdr2 *rthdr2;
rthdr2 = mip6_create_rthdr2(&mbc->mbc_coa);
if (rthdr2 == NULL)
goto freehdrs;
MAKE_EXTHDR(rthdr2, &exthdrs.ip6e_rthdr2);
free(rthdr2, M_IP6OPT);
}
#endif /* MIP6 */
#if defined(MIP6) && NMIP > 0
/*
* If a correspondent binding update list is found and its
* status is BOUND, a packet is sent directly to the
* destination with a Home Address Option. Except a caller
* didn't specify a Home Address Option explicitly.
*/
mbul = mip6_bul_get(&ip6->ip6_src, &ip6->ip6_dst, 0/* XXX */);
/*
* Route Optimization: appending a HoA option.
*/
if ((mbul != NULL) && (exthdrs.ip6e_hoa == NULL)) {
u_int8_t *hoa_opt;
if (mbul->mbul_state & MIP6_BUL_STATE_NEEDTUNNEL)
goto skip_hoa;
if (ip6->ip6_nxt == IPPROTO_MH) {
#if 0
m_copydata(m, sizeof(struct ip6_hdr),
sizeof(struct ip6_mh), (caddr_t)&mh);
if (mh.ip6mh_type != IP6_MH_TYPE_BU)
#endif /* 0 */
goto skip_hoa;
}
hoa_opt = mip6_create_hoa_opt(&mbul->mbul_coa);
if (hoa_opt == NULL)
goto freehdrs;
MAKE_EXTHDR(hoa_opt, &exthdrs.ip6e_hoa);
free(hoa_opt, M_IP6OPT);
}
skip_hoa:
#endif /* MIP6 && NMIP > 0 */
#ifdef IPSEC
#ifdef __OpenBSD__
/*
* splnet is chosen over spltdb because we are not allowed to
* lower the level, and udp6_output calls us in splnet(). XXX check
*/
s = splnet();
/*
* Check if there was an outgoing SA bound to the flow
* from a transport protocol.
*/
ip6 = mtod(m, struct ip6_hdr *);
/* Do we have any pending SAs to apply ? */
mtag = m_tag_find(m, PACKET_TAG_IPSEC_PENDING_TDB, NULL);
if (mtag != NULL) {
#ifdef DIAGNOSTIC
if (mtag->m_tag_len != sizeof (struct tdb_ident))
panic("ip6_output: tag of length %d (should be %d",
mtag->m_tag_len, sizeof (struct tdb_ident));
#endif
tdbi = (struct tdb_ident *)(mtag + 1);
tdb = gettdb(tdbi->spi, &tdbi->dst, tdbi->proto);
if (tdb == NULL)
error = -EINVAL;
m_tag_delete(m, mtag);
}
else
tdb = ipsp_spd_lookup(m, AF_INET6, sizeof(struct ip6_hdr),
&error, IPSP_DIRECTION_OUT, NULL, inp);
if (tdb == NULL) {
splx(s);
if (error == 0) {
/*
* No IPsec processing required, we'll just send the
* packet out.
*/
sproto = 0;
/* Fall through to routing/multicast handling */
} else {
/*
* -EINVAL is used to indicate that the packet should
* be silently dropped, typically because we've asked
* key management for an SA.
*/
if (error == -EINVAL) /* Should silently drop packet */
error = 0;
goto freehdrs;
}
} else {
/* Loop detection */
for (mtag = m_tag_first(m); mtag != NULL;
mtag = m_tag_next(m, mtag)) {
if (mtag->m_tag_id != PACKET_TAG_IPSEC_OUT_DONE &&
mtag->m_tag_id !=
PACKET_TAG_IPSEC_OUT_CRYPTO_NEEDED)
continue;
tdbi = (struct tdb_ident *)(mtag + 1);
if (tdbi->spi == tdb->tdb_spi &&
tdbi->proto == tdb->tdb_sproto &&
!bcmp(&tdbi->dst, &tdb->tdb_dst,
sizeof(union sockaddr_union))) {
splx(s);
sproto = 0; /* mark as no-IPsec-needed */
goto done_spd;
}
}
/* We need to do IPsec */
bcopy(&tdb->tdb_dst, &sdst, sizeof(sdst));
sspi = tdb->tdb_spi;
sproto = tdb->tdb_sproto;
splx(s);
#if 1 /* XXX */
/* if we have any extension header, we cannot perform IPsec */
if (exthdrs.ip6e_hbh || exthdrs.ip6e_dest1 ||
exthdrs.ip6e_rthdr || exthdrs.ip6e_dest2
#ifdef MIP6
|| exthdrs.ip6e_rthdr2
#if NMIP > 0
|| exthdrs.ip6e_hoa
#endif /* NMIP > 0 */
#endif /* MIP6 */
) {
error = EHOSTUNREACH;
goto freehdrs;
}
#endif
}
/* Fall through to the routing/multicast handling code */
done_spd:
#else
if ((flags & IPV6_FORWARDING) != 0) {
needipsec = 0;
goto skippolicycheck;
}
/* get a security policy for this packet */
if (so == NULL)
sp = ipsec6_getpolicybyaddr(m, IPSEC_DIR_OUTBOUND, 0, &error);
else
sp = ipsec6_getpolicybysock(m, IPSEC_DIR_OUTBOUND, so, &error);
if (sp == NULL) {
ipsec6stat.out_inval++;
goto freehdrs;
}
error = 0;
/* check policy */
switch (sp->policy) {
case IPSEC_POLICY_DISCARD:
/*
* This packet is just discarded.
*/
ipsec6stat.out_polvio++;
goto freehdrs;
case IPSEC_POLICY_BYPASS:
case IPSEC_POLICY_NONE:
/* no need to do IPsec. */
needipsec = 0;
break;
case IPSEC_POLICY_IPSEC:
if (sp->req == NULL) {
/* acquire a policy */
error = key_spdacquire(sp);
goto freehdrs;
}
needipsec = 1;
break;
case IPSEC_POLICY_ENTRUST:
default:
printf("ip6_output: Invalid policy found. %d\n", sp->policy);
}
skippolicycheck:;
#endif /* OpenBSD */
#endif /* IPSEC */
/*
* Calculate the total length of the extension header chain.
* Keep the length of the unfragmentable part for fragmentation.
*/
optlen = 0;
if (exthdrs.ip6e_hbh) optlen += exthdrs.ip6e_hbh->m_len;
if (exthdrs.ip6e_dest1) optlen += exthdrs.ip6e_dest1->m_len;
if (exthdrs.ip6e_rthdr) optlen += exthdrs.ip6e_rthdr->m_len;
#ifdef MIP6
if (exthdrs.ip6e_rthdr2) optlen += exthdrs.ip6e_rthdr2->m_len;
#if NMIP > 0
if (exthdrs.ip6e_hoa) optlen += exthdrs.ip6e_hoa->m_len;
#endif /* NMIP > 0 */
#endif /* MIP6 */
unfragpartlen = optlen + sizeof(struct ip6_hdr);
/* NOTE: we don't add AH/ESP length here. do that later. */
if (exthdrs.ip6e_dest2) optlen += exthdrs.ip6e_dest2->m_len;
/*
* If we need IPsec, or there is at least one extension header,
* separate IP6 header from the payload.
*/
#ifdef __OpenBSD__
if ((sproto || optlen) && !hdrsplit)
#else
if ((needipsec || optlen) && !hdrsplit)
#endif
{
if ((error = ip6_splithdr(m, &exthdrs)) != 0) {
m = NULL;
goto freehdrs;
}
m = exthdrs.ip6e_ip6;
hdrsplit++;
}
/* adjust pointer */
ip6 = mtod(m, struct ip6_hdr *);
/* adjust mbuf packet header length */
m->m_pkthdr.len += optlen;
plen = m->m_pkthdr.len - sizeof(*ip6);
/* If this is a jumbo payload, insert a jumbo payload option. */
if (plen > IPV6_MAXPACKET) {
if (!hdrsplit) {
if ((error = ip6_splithdr(m, &exthdrs)) != 0) {
m = NULL;
goto freehdrs;
}
m = exthdrs.ip6e_ip6;
hdrsplit++;
}
/* adjust pointer */
ip6 = mtod(m, struct ip6_hdr *);
if ((error = ip6_insert_jumboopt(&exthdrs, plen)) != 0)
goto freehdrs;
ip6->ip6_plen = 0;
} else
ip6->ip6_plen = htons(plen);
/*
* Concatenate headers and fill in next header fields.
* Here we have, on "m"
* IPv6 payload
* and we insert headers accordingly. Finally, we should be getting:
* IPv6 hbh dest1 rthdr ah* [esp* dest2 payload]
*
* during the header composing process, "m" points to IPv6 header.
* "mprev" points to an extension header prior to esp.
*/
{
u_char *nexthdrp = &ip6->ip6_nxt;
struct mbuf *mprev = m;
/*
* we treat dest2 specially. this makes IPsec processing
* much easier. the goal here is to make mprev point the
* mbuf prior to dest2.
*
* result: IPv6 dest2 payload
* m and mprev will point to IPv6 header.
*/
if (exthdrs.ip6e_dest2) {
if (!hdrsplit)
panic("assumption failed: hdr not split");
exthdrs.ip6e_dest2->m_next = m->m_next;
m->m_next = exthdrs.ip6e_dest2;
*mtod(exthdrs.ip6e_dest2, u_char *) = ip6->ip6_nxt;
ip6->ip6_nxt = IPPROTO_DSTOPTS;
}
#define MAKE_CHAIN(m, mp, p, i)\
do {\
if (m) {\
if (!hdrsplit) \
panic("assumption failed: hdr not split"); \
*mtod((m), u_char *) = *(p);\
*(p) = (i);\
p = mtod((m), u_char *);\
(m)->m_next = (mp)->m_next;\
(mp)->m_next = (m);\
(mp) = (m);\
}\
} while (/*CONSTCOND*/ 0)
/*
* result: IPv6 hbh dest1 rthdr dest2 payload
* m will point to IPv6 header. mprev will point to the
* extension header prior to dest2 (rthdr in the above case).
*/
MAKE_CHAIN(exthdrs.ip6e_hbh, mprev, nexthdrp, IPPROTO_HOPOPTS);
MAKE_CHAIN(exthdrs.ip6e_dest1, mprev, nexthdrp,
IPPROTO_DSTOPTS);
MAKE_CHAIN(exthdrs.ip6e_rthdr, mprev, nexthdrp,
IPPROTO_ROUTING);
#ifdef MIP6
/* a type 2 routing header for route optimization. */
MAKE_CHAIN(exthdrs.ip6e_rthdr2, mprev, nexthdrp,
IPPROTO_ROUTING);
#if NMIP > 0
/*
* MIP6 homeaddress destination option must reside
* after rthdr and before ah/esp/frag hdr.
* this order is not recommended in the ipv6 spec of course.
* result: IPv6 hbh dest1 rthdr ha dest2 payload.
*/
MAKE_CHAIN(exthdrs.ip6e_hoa, mprev, nexthdrp, IPPROTO_DSTOPTS);
#endif /* NMIP > 0 */
#endif /* MIP6 */
#if defined(IPSEC) && !defined(__OpenBSD__)
if (!needipsec)
goto skip_ipsec2;
/*
* pointers after IPsec headers are not valid any more.
* other pointers need a great care too.
* (IPsec routines should not mangle mbufs prior to AH/ESP)
*/
exthdrs.ip6e_dest2 = NULL;
{
struct ip6_rthdr *rh = NULL;
int segleft_org = 0;
#ifdef MIP6
int segleft2_org = 0;
#endif /* MIP6 */
struct ipsec_output_state state;
if (exthdrs.ip6e_rthdr) {
rh = mtod(exthdrs.ip6e_rthdr,
struct ip6_rthdr *);
segleft_org = rh->ip6r_segleft;
rh->ip6r_segleft = 0;
}
#ifdef MIP6
if (exthdrs.ip6e_rthdr2) {
rh = mtod(exthdrs.ip6e_rthdr2,
struct ip6_rthdr *);
segleft2_org = rh->ip6r_segleft;
rh->ip6r_segleft = 0;
}
#endif /* MIP6 */
bzero(&state, sizeof(state));
state.m = m;
error = ipsec6_output_trans(&state, nexthdrp, mprev,
sp, flags, &needipsectun);
m = state.m;
if (error) {
/*
* mbuf is already reclaimed in
* ipsec6_output_trans.
*/
m = NULL;
switch (error) {
case EHOSTUNREACH:
case ENETUNREACH:
case EMSGSIZE:
case ENOBUFS:
case ENOMEM:
break;
default:
printf("ip6_output (ipsec): error code %d\n", error);
/* FALLTHROUGH */
case ENOENT:
/*
* don't show these error
* codes to the user
*/
error = 0;
break;
}
goto bad;
}
if (exthdrs.ip6e_rthdr) {
/* ah6_output doesn't modify mbuf chain */
rh->ip6r_segleft = segleft_org;
}
#ifdef MIP6
if (exthdrs.ip6e_rthdr2) {
/* ah6_output doesn't modify mbuf chain */
rh->ip6r_segleft = segleft2_org;
}
#endif /* MIP6 */
}
skip_ipsec2:;
#endif
}
#if defined(MIP6) && NMIP > 0
/* Swap HoA and CoA */
if (exthdrs.ip6e_hoa) {
struct ip6_opt_home_address *hoaopt = NULL;
struct in6_addr tmpaddr;
bzero(&tmpaddr, sizeof(tmpaddr));
hoaopt = mip6_search_hoa_in_destopt(mtod(exthdrs.ip6e_hoa, caddr_t));
if (hoaopt == NULL)
goto freehdrs;
if (mip6_ifa_ifwithin6addr(&ip6->ip6_src) == NULL)
goto freehdrs;
ip6 = mtod(m, struct ip6_hdr *);
bcopy(&ip6->ip6_src, &tmpaddr, sizeof(ip6->ip6_src));
bcopy(hoaopt->ip6oh_addr,
&ip6->ip6_src, sizeof(hoaopt->ip6oh_addr));
bcopy(&tmpaddr, hoaopt->ip6oh_addr, sizeof(tmpaddr));
}
#endif /* MIP6 && NMIP > 0 */
/*
* If there is a routing header, replace the destination address field
* with the first hop of the routing header.
*/
{
struct ip6_rthdr *rh;
rh = NULL;
if (exthdrs.ip6e_rthdr)
rh = (struct ip6_rthdr *)(mtod(exthdrs.ip6e_rthdr,
struct ip6_rthdr *));
#ifdef MIP6
else if (exthdrs.ip6e_rthdr2)
rh = (struct ip6_rthdr *)(mtod(exthdrs.ip6e_rthdr2,
struct ip6_rthdr *));
#endif /* MIP6 */
if (rh) {
struct ip6_rthdr0 *rh0;
struct in6_addr *addr;
struct sockaddr_in6 sa;
finaldst = ip6->ip6_dst;
switch (rh->ip6r_type) {
case IPV6_RTHDR_TYPE_0:
#ifdef MIP6
case IPV6_RTHDR_TYPE_2:
#endif /* MIP6 */
rh0 = (struct ip6_rthdr0 *)rh;
addr = (struct in6_addr *)(rh0 + 1);
/*
* construct a sockaddr_in6 form of
* the first hop.
*
* XXX: we may not have enough
* information about its scope zone;
* there is no standard API to pass
* the information from the
* application.
*/
bzero(&sa, sizeof(sa));
sa.sin6_family = AF_INET6;
sa.sin6_len = sizeof(sa);
sa.sin6_addr = addr[0];
if ((error = sa6_embedscope(&sa,
ip6_use_defzone)) != 0) {
goto bad;
}
ip6->ip6_dst = sa.sin6_addr;
bcopy(&addr[1], &addr[0],
sizeof(struct in6_addr)
* (rh0->ip6r0_segleft - 1));
addr[rh0->ip6r0_segleft - 1] = finaldst;
/* XXX */
in6_clearscope(addr + rh0->ip6r0_segleft - 1);
break;
default: /* is it possible? */
error = EINVAL;
goto bad;
}
}
}
/* Source address validation */
if (!(flags & IPV6_UNSPECSRC) &&
IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_src)) {
/*
* XXX: we can probably assume validation in the caller, but
* we explicitly check the address here for safety.
*/
error = EOPNOTSUPP;
ip6stat.ip6s_badscope++;
goto bad;
}
if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_src)) {
error = EOPNOTSUPP;
ip6stat.ip6s_badscope++;
goto bad;
}
ip6stat.ip6s_localout++;
/*
* Route packet.
*/
/* initialize cached route */
if (ro == 0) {
ro = &ip6route;
bzero((caddr_t)ro, sizeof(*ro));
}
ro_pmtu = ro;
if (opt && opt->ip6po_rthdr)
ro = &opt->ip6po_route;
#ifdef MIP6
if (exthdrs.ip6e_rthdr2) {
ro = &ip6route;
bzero((caddr_t)ro, sizeof(*ro));
ro_pmtu = ro;
}
#endif /* MIP6 */
dst = (struct sockaddr_in6 *)&ro->ro_dst;
/*
* if specified, try to fill in the traffic class field.
* do not override if a non-zero value is already set.
* we check the diffserv field and the ecn field separately.
*/
if (opt && opt->ip6po_tclass >= 0) {
int mask = 0;
if ((ip6->ip6_flow & htonl(0xfc << 20)) == 0)
mask |= 0xfc;
if ((ip6->ip6_flow & htonl(0x03 << 20)) == 0)
mask |= 0x03;
if (mask != 0)
ip6->ip6_flow |= htonl((opt->ip6po_tclass & mask) << 20);
}
/* fill in or override the hop limit field, if necessary. */
if (opt && opt->ip6po_hlim != -1)
ip6->ip6_hlim = opt->ip6po_hlim & 0xff;
else if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
if (im6o != NULL)
ip6->ip6_hlim = im6o->im6o_multicast_hlim;
else
ip6->ip6_hlim = ip6_defmcasthlim;
}
#ifdef IPSEC
#ifdef __OpenBSD__
/*
* Check if the packet needs encapsulation.
* ipsp_process_packet will never come back to here.
*/
if (sproto != 0) {
s = splnet();
/*
* XXX what should we do if ip6_hlim == 0 and the
* packet gets tunneled?