/
mip6.c
1984 lines (1743 loc) · 49 KB
/
mip6.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
/* $Id: mip6.c,v 1.228 2005/11/13 18:59:16 t-momose Exp $ */
/*
* Copyright (C) 2004 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_ipsec.h"
#include "opt_inet.h"
#include "opt_inet6.h"
#include "opt_mip6.h"
#endif
#ifdef __NetBSD__
#include "opt_ipsec.h"
#include "opt_inet.h"
#include "opt_mip6.h"
#endif
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/kernel.h>
#if defined(__FreeBSD__) || defined(__APPLE__)
#include <sys/malloc.h>
#endif
#include <sys/mbuf.h>
#include <sys/socket.h>
#include <sys/syslog.h>
#if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__APPLE__)
#include <sys/sysctl.h>
#endif
#include <net/if.h>
#include <net/if_types.h>
#include <net/route.h>
#include <net/mipsock.h>
#include <net/net_osdep.h>
#include <netinet/in.h>
#include <netinet/ip6.h>
#include <netinet/ip_encap.h>
#include <netinet/icmp6.h>
#include <netinet/ip6mh.h>
#include <netinet6/in6_var.h>
#include <netinet6/ip6_var.h>
#include <netinet6/scope6_var.h>
#include <netinet6/nd6.h>
#include <netinet6/mip6.h>
#include <netinet6/mip6_var.h>
#include <net/if_mip.h>
#if defined(IPSEC) && !defined(__OpenBSD__)
#include <netinet6/ipsec.h>
#include <netkey/key.h>
#include <netkey/keydb.h>
#endif /* IPSEC && !__OpenBSD__ */
#ifdef __OpenBSD__
#include <netinet/ip_ipsp.h>
#endif
#ifdef __APPLE__
#include <machine/spl.h>
#endif /* __APPLE__ */
#include "mip.h"
#ifndef MIP6_BC_HASH_SIZE
#define MIP6_BC_HASH_SIZE 35 /* XXX */
#endif
/* XXX I'm not sure the effectivity of this hash function */
#define MIP6_IN6ADDR_HASH(addr) \
((addr)->s6_addr32[0] ^ (addr)->s6_addr32[1] ^ \
(addr)->s6_addr32[2] ^ (addr)->s6_addr32[3])
#define MIP6_BC_HASH_ID(addr1, addr2) \
((MIP6_IN6ADDR_HASH(addr1) ) \
% MIP6_BC_HASH_SIZE)
struct mip6_bc_internal *mip6_bc_hash[MIP6_BC_HASH_SIZE];
struct mip6_bc_list mip6_bc_list = LIST_HEAD_INITIALIZER(mip6_bc_list);
struct mip6stat mip6stat;
u_int8_t mip6_nodetype = MIP6_NODETYPE_NONE;
/* sysctl parameters. */
int mip6ctl_use_ipsec = 1;
#ifdef MIP6_DEBUG
int mip6ctl_debug = 1;
#else
int mip6ctl_debug = 0;
#endif
int mip6ctl_rr_hint_ppslim = 10;
int mip6ctl_use_migrate = 0;
extern struct ip6protosw mip6_tunnel_protosw;
#if NMIP > 0
static int mip6_rr_hint_pps_count = 0;
static struct timeval mip6_rr_hint_ppslim_last;
static struct mip6_bul_internal *mip6_bul_create(const struct in6_addr *,
const struct in6_addr *, const struct in6_addr *, u_int16_t, u_int8_t,
struct mip_softc *, u_int16_t);
int mip6_bul_encapcheck(const struct mbuf *, int, int, void *arg);
static void mip6_bul_update_ipsecdb(struct mip6_bul_internal *);
#endif /* NMIP > 0 */
int mip6_rev_encapcheck(const struct mbuf *, int, int, void *arg);
static struct mip6_bc_internal *mip6_bce_new_entry(struct in6_addr *,
struct in6_addr *, struct in6_addr *, struct ifaddr *, u_int16_t,
u_int16_t);
static void mip6_bc_list_insert(struct mip6_bc_internal *);
static void mip6_bc_list_remove(struct mip6_bc_internal *);
static void mip6_bce_update_ipsecdb(struct mip6_bc_internal *);
#if NMIP > 0
static int mip6_rr_hint_ratelimit(const struct in6_addr *,
const struct in6_addr *);
#endif /* NMIP > 0 */
static int mhdefaultlen[] = {
sizeof(struct ip6_mh_binding_request),
sizeof(struct ip6_mh_home_test_init),
sizeof(struct ip6_mh_careof_test_init),
sizeof(struct ip6_mh_home_test),
sizeof(struct ip6_mh_careof_test),
sizeof(struct ip6_mh_binding_update),
sizeof(struct ip6_mh_binding_ack),
sizeof(struct ip6_mh_binding_error)
};
/*
* sysctl knobs.
*/
#if defined(__OpenBSD__)
int
mip6_sysctl(name, namelen, oldp, oldlenp, newp, newlen)
int *name;
u_int namelen;
void *oldp;
size_t *oldlenp;
void *newp;
size_t newlen;
{
/* All sysctl names at this level are terminal. */
if (namelen != 1)
return ENOTDIR;
switch (name[0]) {
case MIP6CTL_DEBUG:
return sysctl_int(oldp, oldlenp, newp, newlen, &mip6ctl_debug);
case MIP6CTL_USE_IPSEC:
return sysctl_int(oldp, oldlenp, newp, newlen,
&mip6ctl_use_ipsec);
case MIP6CTL_RR_HINT_PPSLIM:
return sysctl_int(oldp, oldlenp, newp, newlen,
&mip6ctl_rr_hint_ppslim);
case MIP6CTL_USE_MIGRATE:
return sysctl_int(oldp, oldlenp, newp, newlen,
&mip6ctl_use_migrate);
default:
return (EOPNOTSUPP);
}
/* NOTREACHED */
}
#endif /* __OpenBSD__ */
#ifdef __NetBSD__
/*
* sysctl for MIP6
*/
SYSCTL_SETUP(sysctl_net_inet6_mip6_setup, "sysctl net.inet6.mip6 subtree setup")
{
sysctl_createv(clog, 0, NULL, NULL,
CTLFLAG_PERMANENT,
CTLTYPE_NODE, "net", NULL,
NULL, 0, NULL, 0,
CTL_NET, CTL_EOL);
sysctl_createv(clog, 0, NULL, NULL,
CTLFLAG_PERMANENT,
CTLTYPE_NODE, "inet6",
SYSCTL_DESCR("PF_INET6 related settings"),
NULL, 0, NULL, 0,
CTL_NET, PF_INET6, CTL_EOL);
sysctl_createv(clog, 0, NULL, NULL,
CTLFLAG_PERMANENT,
CTLTYPE_NODE, "mip6",
SYSCTL_DESCR("MIPv6 related settings"),
NULL, 0, NULL, 0,
CTL_NET, PF_INET6, IPPROTO_MH, CTL_EOL);
sysctl_createv(clog, 0, NULL, NULL,
CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
CTLTYPE_INT, "debug",
SYSCTL_DESCR("Enable mip6 debug output"),
NULL, 0, &mip6ctl_debug, 0,
CTL_NET, PF_INET6, IPPROTO_MH,
MIP6CTL_DEBUG, CTL_EOL);
sysctl_createv(clog, 0, NULL, NULL,
CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
CTLTYPE_INT, "use_ipsec",
SYSCTL_DESCR("Enable ipsec of mip6"),
NULL, 0, &mip6ctl_use_ipsec, 0,
CTL_NET, PF_INET6, IPPROTO_MH,
MIP6CTL_USE_IPSEC, CTL_EOL);
sysctl_createv(clog, 0, NULL, NULL,
CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
CTLTYPE_INT, "rr_hint_ppslimit",
SYSCTL_DESCR("Maximum RR hints sent per second"),
NULL, 0, &mip6ctl_rr_hint_ppslim, 0,
CTL_NET, PF_INET6, IPPROTO_MH,
MIP6CTL_RR_HINT_PPSLIM, CTL_EOL);
sysctl_createv(clog, 0, NULL, NULL,
CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
CTLTYPE_INT, "use_migrate",
SYSCTL_DESCR("Enable SADB_X_MIGRATE"),
NULL, 0, &mip6ctl_use_migrate, 0,
CTL_NET, PF_INET6, IPPROTO_MH,
MIP6CTL_RR_HINT_PPSLIM, CTL_EOL);
}
#endif /* __NetBSD__ */
#if defined(__FreeBSD__) || defined(__APPLE__)
SYSCTL_DECL(_net_inet6_mip6);
SYSCTL_INT(_net_inet6_mip6, MIP6CTL_DEBUG, debug, CTLFLAG_RW,
&mip6ctl_debug, 0, "");
SYSCTL_INT(_net_inet6_mip6, MIP6CTL_USE_IPSEC, use_ipsec, CTLFLAG_RW,
&mip6ctl_use_ipsec, 0, "");
SYSCTL_INT(_net_inet6_mip6, MIP6CTL_RR_HINT_PPSLIM, rr_hint_ppslimit,
CTLFLAG_RW, &mip6ctl_rr_hint_ppslim, 0, "");
SYSCTL_INT(_net_inet6_mip6, MIP6CTL_USE_MIGRATE, use_migrate, CTLFLAG_RW,
&mip6ctl_use_migrate, 0, "");
#endif /* __FreeBSD__ || __APPLE__*/
/*
* Mobility Header processing.
*/
int
#ifndef __APPLE__
mip6_input(mp, offp, proto)
struct mbuf **mp;
int *offp, proto;
#else
mip6_input(mp, offp)
struct mbuf **mp;
int *offp;
#endif
{
struct mbuf *m = *mp;
struct ip6_hdr *ip6;
struct ip6_mh *mh;
int off = *offp, mhlen;
int sum;
struct in6_addr src, dst, hoa, rt;
int presence = 0;
mip6stat.mip6s_mh++;
ip6 = mtod(m, struct ip6_hdr *);
/* validation of the length of the header */
#ifndef PULLDOWN_TEST
IP6_EXTHDR_CHECK(m, off, sizeof(*mh), IPPROTO_DONE);
mh = (struct ip6_mh *)(mtod(m, caddr_t) + off);
#else
IP6_EXTHDR_GET(mh, struct ip6_mh *, m, off, sizeof(*mh));
if (mh == NULL)
return (IPPROTO_DONE);
#endif
if (mh->ip6mh_proto != IPPROTO_NONE) {
mip6log((LOG_INFO,
"mip6_input:%d: Payload Proto %d.\n",
__LINE__, mh->ip6mh_proto));
/* 9.2 discard and SHOULD send ICMP Parameter Problem */
mip6stat.mip6s_payloadproto++;
/* If either a home address option or a routing header
* option is present, the source address of icmp6
* error messages must be carefully choosen. If the
* home registration is not yet completed, the home
* address cannot be used for the source
* address. Instead, the care-of address must be used
* to send the icmp6 error messages.
*/
if (mip6_get_ip6hdrinfo(m, &src, &dst, &hoa, &rt, 0, &presence)==0) {
struct m_tag *mtag = NULL;
if (presence & RTHDR_PRESENT) {
bcopy(&rt, &ip6->ip6_dst, sizeof(struct in6_addr));
}
mtag = ip6_findaux(m);
if (mtag) {
bcopy(&rt,
&((struct ip6aux *)(mtag + 1))->ip6a_dstia6->ia_addr.sin6_addr,
sizeof(struct in6_addr));
}
}
icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_HEADER,
(caddr_t)&mh->ip6mh_proto - (caddr_t)ip6);
return (IPPROTO_DONE);
}
/*
* Section 9.2 If length is invalid, issue ICMP param prob,
* code0.
*/
mhlen = (mh->ip6mh_len + 1) << 3;
if (mhlen < IP6OPT_MINLEN ||
(mh->ip6mh_type < sizeof(mhdefaultlen) / sizeof(int)
&& mhlen < mhdefaultlen[mh->ip6mh_type])) {
mip6log((LOG_INFO, "%s:%d: Mobility Header Length %d.\n",
__FILE__, __LINE__, mhlen));
/* 9.2 discard and SHOULD send ICMP Parameter Problem XXX */
ip6stat.ip6s_toosmall++;
/* If either a home address option or a routing header
* option is present, the source address of icmp6
* error messages must be carefully choosen. If the
* home registration is not yet completed, the home
* address cannot be used for the source
* address. Instead, the care-of address must be used
* to send the icmp6 error messages.
*/
if (mip6_get_ip6hdrinfo(m, &src, &dst, &hoa, &rt, 0, &presence)==0) {
struct m_tag *mtag = NULL;
if (presence & RTHDR_PRESENT)
bcopy(&rt, &ip6->ip6_dst, sizeof(struct in6_addr));
mtag = ip6_findaux(m);
if (mtag) {
bcopy(&rt,
&((struct ip6aux *)(mtag + 1))->ip6a_dstia6->ia_addr.sin6_addr,
sizeof(struct in6_addr));
}
}
icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_HEADER,
(caddr_t)&mh->ip6mh_len - (caddr_t)ip6);
return (IPPROTO_DONE);
}
/*
* calculate the checksum
*/
#ifndef PULLDOWN_TEST
IP6_EXTHDR_CHECK(m, off, mhlen, IPPROTO_DONE);
mh = (struct ip6_mh *)(mtod(m, caddr_t) + off);
#else
IP6_EXTHDR_GET(mh, struct ip6_mh *, m, off, mhlen);
if (mh == NULL)
return (IPPROTO_DONE);
#endif
if ((sum = in6_cksum(m, IPPROTO_MH, off, mhlen)) != 0) {
mip6log((LOG_ERR,
"mip6_input:%d: Mobility Header checksum error"
"(type = %d, sum = %x) from %s\n",
__LINE__,
mh->ip6mh_type, sum, ip6_sprintf(&ip6->ip6_src)));
m_freem(m);
mip6stat.mip6s_checksum++;
return (IPPROTO_DONE);
}
switch (mh->ip6mh_type) {
#if NMIP > 0
case IP6_MH_TYPE_BACK:
{
struct mip6_bul_internal *mbul;
/*
* find the binding update entry related to this
* binding ack message. if the bue is for home
* registration, make sure the packet is protected by
* IPsec.
*/
mbul = mip6_bul_get(&ip6->ip6_src, &ip6->ip6_dst, 0 /* XXX */);
if (mbul != NULL
&& (mbul->mbul_flags & IP6_MH_BU_HOME) != 0
&& mip6ctl_use_ipsec) {
#ifndef __OpenBSD__
if (((m->m_flags & M_DECRYPTED) == 0)
&& (m->m_flags & M_AUTHIPHDR) == 0)
#else /* !__OpenBSD__ */
if ((m->m_flags & M_AUTH) == 0)
#endif /* __OpenBSD__ */
{
mip6log((LOG_ERR,
"mip6_input: an unprotected binding "
"update from %s\n",
ip6_sprintf(&ip6->ip6_src)));
m_freem(m);
mip6stat.mip6s_unprotected++;
return (IPPROTO_DONE);
}
}
break;
}
#endif /* NMIP > 0 */
case IP6_MH_TYPE_BU:
/*
* XXX if this BU is home registration, check if the
* packet is protected by IPsec.
*/
break;
}
/* deliver the packet using Raw IPv6 interface. */
return (rip6_input(mp, offp
#ifndef __APPLE__
,proto
#endif
));
}
int
mip6_tunnel_input(mp, offp, proto)
struct mbuf **mp;
int *offp, proto;
{
struct mbuf *m = *mp;
#if NMIP > 0
int off, nxt;
struct icmp6_hdr icmp6;
struct in6_addr src, dst;
struct mip6_bul_internal *bul, *cnbul;
int presence;
#endif /* NMIP > 0 */
#ifndef __FreeBSD__
int s;
#endif
m_adj(m, *offp);
switch (proto) {
case IPPROTO_IPV6:
if (m->m_len < sizeof(struct ip6_hdr)) {
m = m_pullup(m, sizeof(struct ip6_hdr));
if (!m)
return (IPPROTO_DONE);
}
#if NMIP > 0
if ((off = ip6_lasthdr(m, 0, IPPROTO_IPV6, &nxt)) == -1) {
return (IPPROTO_DONE);
}
/* a mobile node must not start the RR procedure
when receiving tunneled MH messages. */
if (nxt == IPPROTO_MH)
goto dontstartrr;
/* a mobile node must not start the RR procedure
when receiving ICMPv6 error messages. */
if (nxt == IPPROTO_ICMPV6) {
m_copydata(m, off, sizeof(struct icmp6_hdr),
(caddr_t)&icmp6);
if (icmp6.icmp6_type < ICMP6_ECHO_REQUEST)
goto dontstartrr;
}
if (mip6_get_ip6hdrinfo(m, &src, &dst, NULL, NULL, 1 /* logical */, &presence) < 0) {
mip6log((LOG_ERR, "mip6_tunnel_input: "
"failded to get logical source and destination "
"addresses.\n"));
return (IPPROTO_DONE);
}
bul = mip6_bul_get_home_agent(&dst);
if (bul == NULL)
goto dontstartrr;
cnbul = mip6_bul_get(&dst, &src, 0);
if (cnbul != NULL)
goto dontstartrr;
mip6_notify_rr_hint(&dst, &src);
dontstartrr:
#endif /* NMIP > 0 */
mip6stat.mip6s_revtunnel++;
#ifdef __NetBSD__
s = splnet();
#elif defined(__OpenBSD__) || defined(__APPLE__)
s = splimp();
#endif
#ifdef __FreeBSD__
if (!IF_HANDOFF(&ip6intrq, m, NULL))
goto bad;
#else
if (IF_QFULL(&ip6intrq)) {
IF_DROP(&ip6intrq); /* update statistics */
splx(s);
goto bad;
}
IF_ENQUEUE(&ip6intrq, m);
#endif
#ifndef __FreeBSD__
splx(s);
#endif
break;
default:
mip6log((LOG_ERR, "protocol %d not supported.\n", proto));
goto bad;
}
return (IPPROTO_DONE);
bad:
m_freem(m);
return (IPPROTO_DONE);
}
struct mip6_bc_internal *
mip6_bce_get(hoa, cnaddr, coa, bid)
struct in6_addr *hoa;
struct in6_addr *cnaddr;
struct in6_addr *coa;
u_int16_t bid;
{
struct mip6_bc_internal *mbc;
int hash = MIP6_BC_HASH_ID(hoa, cnaddr);
for (mbc = mip6_bc_hash[hash];
mbc && mbc->mbc_hash_cache == hash;
mbc = LIST_NEXT(mbc, mbc_entry)) {
#ifdef MIP6_MCOA
if (bid && (mbc->mbc_bid != bid))
continue;
if (coa && !IN6_ARE_ADDR_EQUAL(&mbc->mbc_coa, coa))
continue;
#endif /* MIP6_COA */
if (IN6_ARE_ADDR_EQUAL(&mbc->mbc_hoa, hoa) &&
(cnaddr ? IN6_ARE_ADDR_EQUAL(&mbc->mbc_cnaddr, cnaddr) : 1))
break;
}
return (mbc ? (mbc->mbc_hash_cache == hash ? mbc : NULL) : NULL);
}
static struct mip6_bc_internal *
mip6_bce_new_entry(cnaddr, hoa, coa, ifa, flags, bid)
struct in6_addr *cnaddr, *hoa, *coa;
struct ifaddr *ifa;
u_int16_t flags, bid;
{
struct mip6_bc_internal *mbc = NULL;
MALLOC(mbc, struct mip6_bc_internal *,
sizeof(struct mip6_bc_internal), M_TEMP, M_NOWAIT);
if (mbc == NULL)
return (NULL);
bzero(mbc, sizeof(*mbc));
mbc->mbc_cnaddr = *cnaddr;
mbc->mbc_hoa = *hoa;
mbc->mbc_coa = *coa;
mbc->mbc_ifaddr = ifa;
mbc->mbc_flags = flags;
#ifdef MIP6_MCOA
mbc->mbc_bid = bid;
#endif /* MIP6_MCOA */
return (mbc);
}
static void
mip6_bc_list_insert(mbc)
struct mip6_bc_internal *mbc;
{
int hash = MIP6_BC_HASH_ID(&mbc->mbc_hoa, &mbc->mbc_cnaddr);
if (mip6_bc_hash[hash] != NULL)
LIST_INSERT_BEFORE(mip6_bc_hash[hash], mbc, mbc_entry);
else
LIST_INSERT_HEAD(&mip6_bc_list, mbc, mbc_entry);
mip6_bc_hash[hash] = mbc;
mbc->mbc_hash_cache = hash;
}
int
mip6_bce_update(cnaddr, hoa, coa, flags, bid)
struct sockaddr_in6 *cnaddr, *hoa, *coa;
u_int16_t flags, bid;
{
int s;
int error = 0;
struct mip6_bc_internal *bce = NULL;
struct ifaddr *ifa;
/* Non IPv6 address is not support (only for MIP6) */
if ((cnaddr->sin6_family != AF_INET6) ||
(hoa->sin6_family != AF_INET6) ||
(coa->sin6_family != AF_INET6))
return (EAFNOSUPPORT); /* XXX ? */
#if defined(__NetBSD__) || defined(__OpenBSD__)
s = splsoftnet();
#else
s = splnet();
#endif
bce = mip6_bce_get((struct in6_addr *)(&hoa->sin6_addr),
(struct in6_addr *)(&cnaddr->sin6_addr), NULL, bid);
if (bce) {
bce->mbc_coa = coa->sin6_addr;
goto bc_update_ipsecdb;
}
/* there is no existing bc entry. create a new one. */
ifa = ifa_ifwithaddr((struct sockaddr *)cnaddr);
bce = mip6_bce_new_entry(&cnaddr->sin6_addr, &hoa->sin6_addr,
&coa->sin6_addr, ifa, flags, bid);
if (bce == NULL) {
error = ENOMEM;
goto done;
}
mip6_bc_list_insert(bce);
/* a home agent creates a proxy ND entry for a mobile node. */
if (MIP6_IS_HA && bce != NULL &&
(flags & IP6_MH_BU_HOME) != 0) {
bce->mbc_encap = encap_attach_func(AF_INET6, IPPROTO_IPV6,
mip6_rev_encapcheck,
(void *)&mip6_tunnel_protosw, bce);
if (bce->mbc_encap == NULL) {
mip6log((LOG_ERR, "mip6_bce_update: "
"attaching an encaptab on a home agent "
"failed.\n"));
error = EIO; /* XXX ? */
goto done;
}
error = mip6_bc_proxy_control(&hoa->sin6_addr,
&cnaddr->sin6_addr, RTM_ADD);
if (error) {
mip6log((LOG_ERR, "mip6_bce_update: "
"adding a proxy ND entry failed.\n"));
goto done;
}
}
bc_update_ipsecdb:
if (MIP6_IS_HA && bce != NULL &&
(flags & IP6_MH_BU_HOME) != 0) {
#if defined(IPSEC) && !defined(__OpenBSD__)
mip6_bce_update_ipsecdb(bce);
#endif /* IPSEC && !__OpenBSD__ */
}
done:
splx(s);
return (error);
}
void
mip6_bc_list_remove(mbc)
struct mip6_bc_internal *mbc;
{
int hash = MIP6_BC_HASH_ID(&mbc->mbc_hoa, &mbc->mbc_cnaddr);
if (mip6_bc_hash[hash] == mbc) {
struct mip6_bc_internal *next = LIST_NEXT(mbc, mbc_entry);
if (next != NULL &&
hash == MIP6_BC_HASH_ID(&next->mbc_hoa, &mbc->mbc_cnaddr)) {
mip6_bc_hash[hash] = next;
} else {
mip6_bc_hash[hash] = NULL;
}
}
LIST_REMOVE(mbc, mbc_entry);
}
int
mip6_bce_remove_addr(cnaddr, hoa, coa, flags, bid)
struct sockaddr_in6 *cnaddr, *hoa, *coa;
u_int16_t flags, bid;
{
struct mip6_bc_internal *mbc;
mbc = mip6_bce_get(&hoa->sin6_addr, &cnaddr->sin6_addr, NULL, bid);
if (!mbc)
return (0);
mbc->mbc_coa = coa->sin6_addr;
return (mip6_bce_remove_bc(mbc));
}
int
mip6_bce_remove_bc(mbc)
struct mip6_bc_internal *mbc;
{
int s;
int error = 0;
#if 0
mbc->mbc_refcnt--;
if (mbc->mbc_flags & IP6_MH_BU_CLONED) {
if (mbc->mbc_refcnt > 1)
return (0);
} else {
if (mbc->mbc_refcnt > 0)
return (0);
}
#endif
#if defined(__NetBSD__) || defined(__OpenBSD__)
s = splsoftnet();
#else
s = splnet();
#endif
mip6_bc_list_remove(mbc);
if (MIP6_IS_HA && (mbc->mbc_flags & IP6_MH_BU_HOME)) {
#if 0
if (MIP6_IS_BC_DAD_WAIT(mbc)) {
mip6_dad_stop(mbc);
} else {
#endif
error = mip6_bc_proxy_control(&mbc->mbc_hoa,
&mbc->mbc_cnaddr, RTM_DELETE);
error = encap_detach(mbc->mbc_encap);
#if 0
}
#endif
#ifdef IPSEC
mip6_bce_update_ipsecdb(mbc);
#endif /* IPSEC */
}
FREE(mbc, M_TEMP);
splx(s);
return (error);
}
static void
mip6_bce_update_ipsecdb(bce)
struct mip6_bc_internal *bce;
{
#ifndef __APPLE__
#ifdef IPSEC
/* racoon2 guys want us to update ipsecdb. (2004.10.8) */
struct sockaddr_in6 hoa_sa, coa_sa, haaddr_sa;
/* update the ipsecdb. */
bzero(&hoa_sa, sizeof(struct sockaddr_in6));
hoa_sa.sin6_len = sizeof(struct sockaddr_in6);
hoa_sa.sin6_family = AF_INET6;
hoa_sa.sin6_addr = bce->mbc_hoa;
bzero(&coa_sa, sizeof(struct sockaddr_in6));
coa_sa.sin6_len = sizeof(struct sockaddr_in6);
coa_sa.sin6_family = AF_INET6;
coa_sa.sin6_addr = bce->mbc_coa;
bzero(&haaddr_sa, sizeof(struct sockaddr_in6));
haaddr_sa.sin6_len = sizeof(struct sockaddr_in6);
haaddr_sa.sin6_family = AF_INET6;
haaddr_sa.sin6_addr = bce->mbc_cnaddr;
key_mip6_update_home_agent_ipsecdb(&hoa_sa, NULL, &coa_sa,
&haaddr_sa);
#endif /* IPSEC */
#endif /* __APPLE__ */
}
void
mip6_bce_remove_all (void)
{
struct mip6_bc_internal *mbc, *mbcn = NULL;
int s;
#if defined(__NetBSD__) || defined(__OpenBSD__)
s = splsoftnet();
#else
s = splnet();
#endif
for (mbc = LIST_FIRST(&mip6_bc_list); mbc; mbc = mbcn) {
mbcn = LIST_NEXT(mbc, mbc_entry);
mip6_bc_list_remove(mbc);
}
splx(s);
}
/* Preparing a type2 routing header for outgoing packets */
/* The caller must free the returned buffer */
struct ip6_rthdr2 *
mip6_create_rthdr2(coa)
struct in6_addr *coa;
{
struct ip6_rthdr2 *rthdr2;
size_t len;
/*
* Mobile IPv6 uses type 2 routing header for route
* optimization. if the packet has a type 1 routing header
* already, we must add a type 2 routing header after the type
* 1 routing header.
*/
len = sizeof(struct ip6_rthdr2) + sizeof(struct in6_addr);
MALLOC(rthdr2, struct ip6_rthdr2 *, len, M_IP6OPT, M_NOWAIT);
if (rthdr2 == NULL) {
return (NULL);
}
bzero(rthdr2, len);
/* rthdr2->ip6r2_nxt = will be filled later in ip6_output */
rthdr2->ip6r2_len = 2;
rthdr2->ip6r2_type = 2;
rthdr2->ip6r2_segleft = 1;
rthdr2->ip6r2_reserved = 0;
bcopy((caddr_t)coa, (caddr_t)(rthdr2 + 1), sizeof(struct in6_addr));
mip6stat.mip6s_orthdr2++;
return (rthdr2);
}
#if NMIP > 0
struct in6_ifaddr *
mip6_ifa_ifwithin6addr(in6)
const struct in6_addr *in6;
{
struct sockaddr_in6 sin6;
/*
* in6 must have a scope information embedded in it,
* before being passed to this function.
*/
bzero(&sin6, sizeof(struct sockaddr_in6));
sin6.sin6_len = sizeof(struct sockaddr_in6);
sin6.sin6_family = AF_INET6;
sin6.sin6_addr = *in6;
#ifndef __APPLE__
if (sa6_recoverscope(&sin6) != 0)
return (NULL);
#else
/* XXXX */
/* should support scope6.c */
if (IN6_IS_SCOPE_LINKLOCAL(&sin6.sin6_addr)
/*||IN6_IS_ADDR_MC_INTFACELOCAL(&sin6.sin6_addr)*/) {
sin6.sin6_scope_id = ntohs(sin6.sin6_addr.s6_addr16[1]);
sin6.sin6_addr.s6_addr16[1] = 0;
}
#endif
return ((struct in6_ifaddr *)ifa_ifwithaddr((struct sockaddr *)&sin6));
}
static struct mip6_bul_internal *
mip6_bul_create(peeraddr, hoa, coa, flags, state, sc, bid)
const struct in6_addr *peeraddr, *hoa, *coa;
u_int16_t flags;
u_int8_t state;
struct mip_softc *sc;
u_int16_t bid;
{
struct mip6_bul_internal *mbul;
MALLOC(mbul, struct mip6_bul_internal *,
sizeof(struct mip6_bul_internal), M_TEMP, M_NOWAIT);
if (mbul == NULL) {
return (NULL);
}
bzero(mbul, sizeof(*mbul));
mbul->mbul_peeraddr = *peeraddr;
mbul->mbul_hoa = *hoa;
mbul->mbul_coa = *coa;
mbul->mbul_mip = sc;
mbul->mbul_flags = flags;
mbul->mbul_state = state;
#ifdef MIP6_MCOA
mbul->mbul_bid = bid;
#endif /* MIP6_MCOA */
return (mbul);
}
int
mip6_bul_add(peeraddr, hoa, coa, hoa_ifindex, flags, state, bid)
const struct in6_addr *peeraddr, *hoa, *coa;
u_short hoa_ifindex;
u_int16_t flags;
u_int8_t state;
u_int16_t bid;
{
int error = 0;
struct in6_ifaddr *ia6_hoa;
struct mip6_bul_internal *mbul;
#if 0
#ifdef __FreeBSD__
mipsc = (struct mip_softc *)ifnet_byindex(hoa_ifindex);
#else
mipsc = (struct mip_softc *)ifindex2ifnet[hoa_ifindex];
#endif
if (mipsc == NULL)
return (-1);
ia6_hoa = mip6_ifa_ifwithin6addr(hoa);
#endif
ia6_hoa = mip6_ifa_ifwithin6addr(hoa);
if (ia6_hoa == NULL)
return (-1);
if (IN6_IS_ADDR_MULTICAST(peeraddr) ||
IN6_IS_ADDR_UNSPECIFIED(peeraddr))
return (EINVAL);
if (IN6_IS_ADDR_MULTICAST(coa) ||
IN6_IS_ADDR_UNSPECIFIED(coa))
return (EINVAL);
/*
* If the correspondent entry exists, the entry is removed
* first. Then, the requested bul will be added right after
* this deletion.
*/
mbul = mip6_bul_get(hoa, peeraddr, bid);
if (mbul)
mip6_bul_remove(mbul);
/* binding update list is created here */
mbul = mip6_bul_create(peeraddr, hoa, coa, flags, state,
(struct mip_softc *)ia6_hoa->ia_ifp, bid);
if (mbul == NULL)
return (-1);
LIST_INSERT_HEAD(MBUL_LIST(ia6_hoa), mbul, mbul_entry);
/*
* tunnel setup only when the requested bul is for home
* registration. (and not for the basic NEMO protocol)
*/
if ((mbul->mbul_flags & IP6_MH_BU_HOME) &&
(mbul->mbul_flags & IP6_MH_BU_ROUTER) == 0) {
mbul->mbul_encap = encap_attach_func(AF_INET6, IPPROTO_IPV6,
mip6_bul_encapcheck,
(void *)&mip6_tunnel_protosw, mbul);
if (error) {
mip6log((LOG_ERR, "tunnel move failed.\n"));
/* XXX notifiy to upper XXX */
mip6_bul_remove(mbul);
return (error);
}
#ifdef IPSEC
mip6_bul_update_ipsecdb(mbul);
#endif
}
return (error);
}
void
mip6_bul_remove(mbul)
struct mip6_bul_internal *mbul;
{
int s, error;
#if defined(__NetBSD__) || defined(__OpenBSD__)
s = splsoftnet();
#else
s = splnet();
#endif
LIST_REMOVE(mbul, mbul_entry);
if ((mbul->mbul_flags & IP6_MH_BU_HOME) &&
(mbul->mbul_flags & IP6_MH_BU_ROUTER) == 0) {
/* close encaptab */
error = encap_detach(mbul->mbul_encap);
mbul->mbul_encap = NULL;
if (error) {
mip6log((LOG_ERR, "mip6_bul_remove: "