Projet

Général

Profil

Télécharger (41,8 ko) Statistiques
| Branche: | Révision:

univnautes-tools / patches / stable / 10 / stf_6rd.diff @ f41bc703

1
diff --git a/sbin/ifconfig/Makefile b/sbin/ifconfig/Makefile
2
index a10d1fb..bbd7b89 100644
3
--- a/sbin/ifconfig/Makefile
4
+++ b/sbin/ifconfig/Makefile
5
@@ -33,6 +33,7 @@ SRCS+=	iffib.c			# non-default FIB support
6
 SRCS+=	ifvlan.c		# SIOC[GS]ETVLAN support
7
 SRCS+=	ifgre.c			# GRE keys etc
8
 SRCS+=	ifgif.c			# GIF reversed header workaround
9
+SRCS+=	ifstf.c			# STF configuration options
10
 
11
 SRCS+=	ifieee80211.c regdomain.c # SIOC[GS]IEEE80211 support
12
 DPADD+=	${LIBBSDXML} ${LIBSBUF}
13
diff --git a/sbin/ifconfig/ifstf.c b/sbin/ifconfig/ifstf.c
14
new file mode 100644
15
index 0000000..8c8b3fb
16
--- /dev/null
17
+++ b/sbin/ifconfig/ifstf.c
18
@@ -0,0 +1,156 @@
19
+/*-
20
+ * Copyright 2013 Ermal Luci
21
+ * All rights reserved.
22
+ *
23
+ * Redistribution and use in source and binary forms, with or without
24
+ * modification, are permitted provided that the following conditions
25
+ * are met:
26
+ * 1. Redistributions of source code must retain the above copyright
27
+ *    notice, this list of conditions and the following disclaimer.
28
+ * 2. Redistributions in binary form must reproduce the above copyright
29
+ *    notice, this list of conditions and the following disclaimer in the
30
+ *    documentation and/or other materials provided with the distribution.
31
+ *
32
+ * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
33
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
34
+ * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
35
+ * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL WASABI SYSTEMS, INC
36
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
37
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
38
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
39
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
40
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
41
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
42
+ * POSSIBILITY OF SUCH DAMAGE.
43
+ */
44
+
45
+#include <sys/param.h>
46
+#include <sys/ioctl.h>
47
+#include <sys/socket.h>
48
+#include <sys/sockio.h>
49
+
50
+#include <stdlib.h>
51
+#include <unistd.h>
52
+
53
+#include <net/ethernet.h>
54
+#include <net/if.h>
55
+#include <net/route.h>
56
+
57
+#include <netinet/in.h>
58
+#include <net/if_stf.h>
59
+#include <arpa/inet.h>
60
+
61
+#include <ctype.h>
62
+#include <stdio.h>
63
+#include <string.h>
64
+#include <stdlib.h>
65
+#include <unistd.h>
66
+#include <err.h>
67
+#include <errno.h>
68
+
69
+#include "ifconfig.h"
70
+
71
+static int
72
+do_cmd(int sock, u_long op, void *arg, size_t argsize, int set)
73
+{
74
+	struct ifdrv ifd;
75
+
76
+	memset(&ifd, 0, sizeof(ifd));
77
+
78
+	strlcpy(ifd.ifd_name, ifr.ifr_name, sizeof(ifd.ifd_name));
79
+	ifd.ifd_cmd = op;
80
+	ifd.ifd_len = argsize;
81
+	ifd.ifd_data = arg;
82
+
83
+	return (ioctl(sock, set ? SIOCSDRVSPEC : SIOCGDRVSPEC, &ifd));
84
+}
85
+
86
+static void
87
+stf_status(int s)
88
+{
89
+	struct stfv4args param;
90
+
91
+	if (do_cmd(s, STF_GV4NET, &param, sizeof(param), 0) < 0)
92
+		return;
93
+
94
+	printf("\tv4net %s/%d\n", inet_ntoa(param.inaddr), param.prefix);
95
+	printf("\tv4br %s\n", inet_ntoa(param.dstv4_addr));
96
+	    
97
+	return;
98
+}
99
+
100
+static void
101
+setstf_br(const char *val, int d, int s, const struct afswtch *afp)
102
+{
103
+	struct stfv4args req;
104
+        struct sockaddr_in sin;
105
+
106
+	memset(&req, 0, sizeof(req));
107
+
108
+        sin.sin_len = sizeof(sin);
109
+	sin.sin_family = AF_INET;
110
+
111
+        if (!inet_aton(val, &sin.sin_addr))
112
+                errx(1, "%s: bad value", val);
113
+
114
+	req.dstv4_addr = sin.sin_addr;
115
+	if (do_cmd(s, STF_SDSTV4, &req, sizeof(req), 1) < 0)
116
+		err(1, "STF_SV4DST %s",  val);
117
+}
118
+
119
+static void
120
+setstf_set(const char *val, int d, int s, const struct afswtch *afp)
121
+{
122
+	struct stfv4args req;
123
+        struct sockaddr_in sin;
124
+	const char *errstr;
125
+	char *p = NULL;
126
+
127
+	memset(&req, 0, sizeof(req));
128
+
129
+        sin.sin_len = sizeof(sin);
130
+	sin.sin_family = AF_INET;
131
+
132
+	p = strrchr(val, '/');
133
+	if (p == NULL)
134
+		errx(2, "Wrong argument given");
135
+
136
+	*p = '\0';
137
+	if (!isdigit(*(p + 1)))
138
+		errstr = "invalid";
139
+	else
140
+		req.prefix = (int)strtonum(p + 1, 0, 32, &errstr);
141
+	if (errstr != NULL) {
142
+		*p = '/';
143
+		errx(1, "%s: bad value (width %s)", val, errstr);
144
+	}
145
+
146
+        if (!inet_aton(val, &sin.sin_addr))
147
+                errx(1, "%s: bad value", val);
148
+
149
+	req.inaddr = sin.sin_addr;
150
+	if (do_cmd(s, STF_SV4NET, &req, sizeof(req), 1) < 0)
151
+		err(1, "STF_SV4NET %s",  val);
152
+}
153
+
154
+static struct cmd stf_cmds[] = {
155
+	DEF_CMD_ARG("stfv4net",		setstf_set),
156
+	DEF_CMD_ARG("stfv4br",		setstf_br),
157
+};
158
+static struct afswtch af_stf = {
159
+	.af_name	= "af_stf",
160
+	.af_af		= AF_UNSPEC,
161
+	.af_other_status = stf_status,
162
+};
163
+
164
+static __constructor void
165
+stf_ctor(void)
166
+{
167
+#define	N(a)	(sizeof(a) / sizeof(a[0]))
168
+	int i;
169
+
170
+	for (i = 0; i < N(stf_cmds);  i++)
171
+		cmd_register(&stf_cmds[i]);
172
+	af_register(&af_stf);
173
+#undef N
174
+}
175
diff --git a/sys/net/if_stf.c b/sys/net/if_stf.c
176
index 20251dc..3d56b2d 100644
177
--- a/sys/net/if_stf.c
178
+++ b/sys/net/if_stf.c
179
@@ -3,6 +3,8 @@
180
 
181
 /*-
182
  * Copyright (C) 2000 WIDE Project.
183
+ * Copyright (c) 2010 Hiroki Sato <hrs@FreeBSD.org>
184
+ * Copyright (c) 2013 Ermal Lu?i <eri@FreeBSD.org>
185
  * All rights reserved.
186
  *
187
  * Redistribution and use in source and binary forms, with or without
188
@@ -31,7 +33,7 @@
189
  */
190
 
191
 /*
192
- * 6to4 interface, based on RFC3056.
193
+ * 6to4 interface, based on RFC3056 + 6rd (RFC5569) support.
194
  *
195
  * 6to4 interface is NOT capable of link-layer (I mean, IPv4) multicasting.
196
  * There is no address mapping defined from IPv6 multicast address to IPv4
197
@@ -60,7 +62,7 @@
198
  * ICMPv6:
199
  * - Redirects cannot be used due to the lack of link-local address.
200
  *
201
- * stf interface does not have, and will not need, a link-local address.  
202
+ * stf interface does not have, and will not need, a link-local address.
203
  * It seems to have no real benefit and does not help the above symptoms much.
204
  * Even if we assign link-locals to interface, we cannot really
205
  * use link-local unicast/multicast on top of 6to4 cloud (since there's no
206
@@ -72,6 +74,12 @@
207
  * http://playground.iijlab.net/i-d/draft-itojun-ipv6-transition-abuse-00.txt
208
  * for details.  The code tries to filter out some of malicious packets.
209
  * Note that there is no way to be 100% secure.
210
+ *
211
+ * 6rd (RFC5569 & RFC5969) extension is enabled when an IPv6 GUA other than
212
+ * 2002::/16 is assigned.  The stf(4) recognizes a 32-bit just after
213
+ * prefixlen as the IPv4 address of the 6rd customer site.  The
214
+ * prefixlen must be shorter than 32.
215
+ *
216
  */
217
 
218
 #include "opt_inet.h"
219
@@ -92,13 +100,14 @@
220
 #include <machine/cpu.h>
221
 
222
 #include <sys/malloc.h>
223
+#include <sys/priv.h>
224
 
225
 #include <net/if.h>
226
+#include <net/if_var.h>
227
 #include <net/if_clone.h>
228
 #include <net/route.h>
229
 #include <net/netisr.h>
230
 #include <net/if_types.h>
231
-#include <net/if_stf.h>
232
 #include <net/vnet.h>
233
 
234
 #include <netinet/in.h>
235
@@ -106,6 +115,7 @@
236
 #include <netinet/ip.h>
237
 #include <netinet/ip_var.h>
238
 #include <netinet/in_var.h>
239
+#include <net/if_stf.h>
240
 
241
 #include <netinet/ip6.h>
242
 #include <netinet6/ip6_var.h>
243
@@ -120,20 +130,48 @@
244
 
245
 #include <security/mac/mac_framework.h>
246
 
247
+#define	STF_DEBUG 1
248
+#if	STF_DEBUG > 3
249
+#define	ip_sprintf(buf, a)						\
250
+	sprintf(buf, "%u.%u.%u.%u",					\
251
+		(ntohl((a)->s_addr)>>24)&0xFF,				\
252
+		(ntohl((a)->s_addr)>>16)&0xFF,				\
253
+		(ntohl((a)->s_addr)>>8)&0xFF,				\
254
+		(ntohl((a)->s_addr))&0xFF);
255
+#endif
256
+
257
+#if STF_DEBUG
258
+#define	DEBUG_PRINTF(a, ...)						\
259
+	do {								\
260
+		if (V_stf_debug >= a)                                   \
261
+		printf(__VA_ARGS__);					\
262
+	} while (0)
263
+#else
264
+#define DEBUG_PRINTF(a, ...)
265
+#endif
266
+
267
 SYSCTL_DECL(_net_link);
268
 static SYSCTL_NODE(_net_link, IFT_STF, stf, CTLFLAG_RW, 0, "6to4 Interface");
269
 
270
-static int stf_route_cache = 1;
271
-SYSCTL_INT(_net_link_stf, OID_AUTO, route_cache, CTLFLAG_RW,
272
-    &stf_route_cache, 0, "Caching of IPv4 routes for 6to4 Output");
273
+static	VNET_DEFINE(int, stf_route_cache) = 0;
274
+#define	V_stf_route_cache     VNET(stf_route_cache)
275
+SYSCTL_VNET_INT(_net_link_stf, OID_AUTO, route_cache, CTLFLAG_RW,
276
+	&VNET_NAME(stf_route_cache), 0,
277
+	"Enable caching of IPv4 routes for 6to4 output.");
278
+
279
+#if STF_DEBUG
280
+static VNET_DEFINE(int, stf_debug) = 0;
281
+#define	V_stf_debug   VNET(stf_debug)
282
+SYSCTL_VNET_INT(_net_link_stf, OID_AUTO, stf_debug, CTLFLAG_RW,
283
+	&VNET_NAME(stf_debug), 0,
284
+	"Enable displaying verbose debug message of stf interfaces");
285
+#endif
286
 
287
 static int stf_permit_rfc1918 = 0;
288
 TUNABLE_INT("net.link.stf.permit_rfc1918", &stf_permit_rfc1918);
289
 SYSCTL_INT(_net_link_stf, OID_AUTO, permit_rfc1918, CTLFLAG_RW | CTLFLAG_TUN,
290
     &stf_permit_rfc1918, 0, "Permit the use of private IPv4 addresses");
291
 
292
-#define STFUNIT		0
293
-
294
 #define IN6_IS_ADDR_6TO4(x)	(ntohs((x)->s6_addr16[0]) == 0x2002)
295
 
296
 /*
297
@@ -149,19 +187,31 @@ struct stf_softc {
298
 		struct route_in6 __sc_ro6; /* just for safety */
299
 	} __sc_ro46;
300
 #define sc_ro	__sc_ro46.__sc_ro4
301
-	struct mtx	sc_ro_mtx;
302
+	struct mtx	sc_mtx;
303
 	u_int	sc_fibnum;
304
 	const struct encaptab *encap_cookie;
305
+	u_int   sc_flags;
306
+	u_int   v4prefixlen;
307
+	in_addr_t inaddr;
308
+	in_addr_t dstv4_addr;
309
+	LIST_ENTRY(stf_softc) stf_list;
310
 };
311
 #define STF2IFP(sc)	((sc)->sc_ifp)
312
 
313
 static const char stfname[] = "stf";
314
 
315
-/*
316
- * Note that mutable fields in the softc are not currently locked.
317
- * We do lock sc_ro in stf_output though.
318
- */
319
+static struct mtx stf_mtx;
320
 static MALLOC_DEFINE(M_STF, stfname, "6to4 Tunnel Interface");
321
+static VNET_DEFINE(LIST_HEAD(, stf_softc), stf_softc_list);
322
+#define	V_stf_softc_list      VNET(stf_softc_list)
323
+
324
+#define	STF_LOCK_INIT(sc)     mtx_init(&(sc)->sc_mtx, "stf softc",    \
325
+	NULL, MTX_DEF);
326
+#define	STF_LOCK_DESTROY(sc)  mtx_destroy(&(sc)->sc_mtx)
327
+#define	STF_LOCK(sc)          mtx_lock(&(sc)->sc_mtx)
328
+#define	STF_UNLOCK(sc)                mtx_unlock(&(sc)->sc_mtx)
329
+#define	STF_LOCK_ASSERT(sc)   mtx_assert(&(sc)->sc_mtx, MA_OWNED)
330
+
331
 static const int ip_stf_ttl = 40;
332
 
333
 extern  struct domain inetdomain;
334
@@ -176,8 +226,6 @@ struct protosw in_stf_protosw = {
335
 	.pr_usrreqs =		&rip_usrreqs
336
 };
337
 
338
-static char *stfnames[] = {"stf0", "stf", "6to4", NULL};
339
-
340
 static int stfmodevent(module_t, int, void *);
341
 static int stf_encapcheck(const struct mbuf *, int, int, void *);
342
 static struct in6_ifaddr *stf_getsrcifa6(struct ifnet *);
343
@@ -191,66 +239,42 @@ static int stf_checkaddr6(struct stf_softc *, struct in6_addr *,
344
 static void stf_rtrequest(int, struct rtentry *, struct rt_addrinfo *);
345
 static int stf_ioctl(struct ifnet *, u_long, caddr_t);
346
 
347
-static int stf_clone_match(struct if_clone *, const char *);
348
-static int stf_clone_create(struct if_clone *, char *, size_t, caddr_t);
349
-static int stf_clone_destroy(struct if_clone *, struct ifnet *);
350
-static struct if_clone *stf_cloner;
351
+#define	STF_GETIN4_USE_CACHE  1
352
+static struct sockaddr_in *stf_getin4addr(struct stf_softc *, struct sockaddr_in *,
353
+	struct ifaddr *, int);
354
+static struct sockaddr_in *stf_getin4addr_in6(struct stf_softc *, struct sockaddr_in *,
355
+	struct ifaddr *, const struct in6_addr *);
356
+static struct sockaddr_in *stf_getin4addr_sin6(struct stf_softc *, struct sockaddr_in *,
357
+	struct ifaddr *, struct sockaddr_in6 *);
358
+static int stf_clone_create(struct if_clone *, int, caddr_t);
359
+static void stf_clone_destroy(struct ifnet *);
360
 
361
-static int
362
-stf_clone_match(struct if_clone *ifc, const char *name)
363
-{
364
-	int i;
365
-
366
-	for(i = 0; stfnames[i] != NULL; i++) {
367
-		if (strcmp(stfnames[i], name) == 0)
368
-			return (1);
369
-	}
370
-
371
-	return (0);
372
-}
373
+static struct if_clone *stf_cloner;
374
 
375
 static int
376
-stf_clone_create(struct if_clone *ifc, char *name, size_t len, caddr_t params)
377
+stf_clone_create(struct if_clone *ifc, int unit, caddr_t params)
378
 {
379
-	int err, unit;
380
 	struct stf_softc *sc;
381
 	struct ifnet *ifp;
382
 
383
-	/*
384
-	 * We can only have one unit, but since unit allocation is
385
-	 * already locked, we use it to keep from allocating extra
386
-	 * interfaces.
387
-	 */
388
-	unit = STFUNIT;
389
-	err = ifc_alloc_unit(ifc, &unit);
390
-	if (err != 0)
391
-		return (err);
392
-
393
 	sc = malloc(sizeof(struct stf_softc), M_STF, M_WAITOK | M_ZERO);
394
+	sc->sc_fibnum = curthread->td_proc->p_fibnum;
395
 	ifp = STF2IFP(sc) = if_alloc(IFT_STF);
396
-	if (ifp == NULL) {
397
+	if (sc->sc_ifp == NULL) {
398
 		free(sc, M_STF);
399
-		ifc_free_unit(ifc, unit);
400
-		return (ENOSPC);
401
+		return (ENOMEM);
402
 	}
403
+	STF_LOCK_INIT(sc);
404
 	ifp->if_softc = sc;
405
-	sc->sc_fibnum = curthread->td_proc->p_fibnum;
406
 
407
-	/*
408
-	 * Set the name manually rather then using if_initname because
409
-	 * we don't conform to the default naming convention for interfaces.
410
-	 */
411
-	strlcpy(ifp->if_xname, name, IFNAMSIZ);
412
-	ifp->if_dname = stfname;
413
-	ifp->if_dunit = IF_DUNIT_NONE;
414
+	if_initname(ifp, stfname, unit);
415
 
416
-	mtx_init(&(sc)->sc_ro_mtx, "stf ro", NULL, MTX_DEF);
417
 	sc->encap_cookie = encap_attach_func(AF_INET, IPPROTO_IPV6,
418
 	    stf_encapcheck, &in_stf_protosw, sc);
419
 	if (sc->encap_cookie == NULL) {
420
 		if_printf(ifp, "attach failed\n");
421
+		if_free(ifp);
422
 		free(sc, M_STF);
423
-		ifc_free_unit(ifc, unit);
424
 		return (ENOMEM);
425
 	}
426
 
427
@@ -260,42 +284,56 @@ stf_clone_create(struct if_clone *ifc, char *name, size_t len, caddr_t params)
428
 	ifp->if_snd.ifq_maxlen = ifqmaxlen;
429
 	if_attach(ifp);
430
 	bpfattach(ifp, DLT_NULL, sizeof(u_int32_t));
431
+
432
+	mtx_lock(&stf_mtx);
433
+	LIST_INSERT_HEAD(&V_stf_softc_list, sc, stf_list);
434
+	mtx_unlock(&stf_mtx);
435
+
436
 	return (0);
437
 }
438
 
439
-static int
440
-stf_clone_destroy(struct if_clone *ifc, struct ifnet *ifp)
441
+static void
442
+stf_clone_destroy(struct ifnet *ifp)
443
 {
444
 	struct stf_softc *sc = ifp->if_softc;
445
 	int err;
446
 
447
+	mtx_lock(&stf_mtx);
448
+	LIST_REMOVE(sc, stf_list);
449
+	mtx_unlock(&stf_mtx);
450
+
451
 	err = encap_detach(sc->encap_cookie);
452
 	KASSERT(err == 0, ("Unexpected error detaching encap_cookie"));
453
-	mtx_destroy(&(sc)->sc_ro_mtx);
454
 	bpfdetach(ifp);
455
 	if_detach(ifp);
456
 	if_free(ifp);
457
 
458
+	STF_LOCK_DESTROY(sc);
459
 	free(sc, M_STF);
460
-	ifc_free_unit(ifc, STFUNIT);
461
+}
462
 
463
-	return (0);
464
+static void
465
+vnet_stf_init(const void *unused __unused)
466
+{
467
+
468
+	LIST_INIT(&V_stf_softc_list);
469
 }
470
+VNET_SYSINIT(vnet_stf_init, SI_SUB_PSEUDO, SI_ORDER_MIDDLE, vnet_stf_init,
471
+	NULL);
472
 
473
 static int
474
-stfmodevent(mod, type, data)
475
-	module_t mod;
476
-	int type;
477
-	void *data;
478
+stfmodevent(module_t mod, int type, void *data)
479
 {
480
 
481
 	switch (type) {
482
 	case MOD_LOAD:
483
-		stf_cloner = if_clone_advanced(stfname, 0, stf_clone_match,
484
-		    stf_clone_create, stf_clone_destroy);
485
+		mtx_init(&stf_mtx, "stf_mtx", NULL, MTX_DEF);
486
+		stf_cloner = if_clone_simple(stfname,
487
+		    stf_clone_create, stf_clone_destroy, 0);
488
 		break;
489
 	case MOD_UNLOAD:
490
 		if_clone_detach(stf_cloner);
491
+		mtx_destroy(&stf_mtx);
492
 		break;
493
 	default:
494
 		return (EOPNOTSUPP);
495
@@ -311,28 +349,31 @@ static moduledata_t stf_mod = {
496
 };
497
 
498
 DECLARE_MODULE(if_stf, stf_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
499
+MODULE_VERSION(if_stf, 1);
500
 
501
 static int
502
-stf_encapcheck(m, off, proto, arg)
503
-	const struct mbuf *m;
504
-	int off;
505
-	int proto;
506
-	void *arg;
507
+stf_encapcheck(const struct mbuf *m, int off, int proto, void *arg)
508
 {
509
 	struct ip ip;
510
 	struct in6_ifaddr *ia6;
511
+	struct sockaddr_in ia6_in4addr;
512
+	struct sockaddr_in ia6_in4mask;
513
+	struct sockaddr_in *sin;
514
 	struct stf_softc *sc;
515
-	struct in_addr a, b, mask;
516
+	struct ifnet *ifp;
517
+	int ret = 0;
518
 
519
+	DEBUG_PRINTF(1, "%s: enter\n", __func__);
520
 	sc = (struct stf_softc *)arg;
521
 	if (sc == NULL)
522
 		return 0;
523
+	ifp = STF2IFP(sc);
524
 
525
-	if ((STF2IFP(sc)->if_flags & IFF_UP) == 0)
526
+	if ((ifp->if_flags & IFF_UP) == 0)
527
 		return 0;
528
 
529
 	/* IFF_LINK0 means "no decapsulation" */
530
-	if ((STF2IFP(sc)->if_flags & IFF_LINK0) != 0)
531
+	if ((ifp->if_flags & IFF_LINK0) != 0)
532
 		return 0;
533
 
534
 	if (proto != IPPROTO_IPV6)
535
@@ -344,72 +385,156 @@ stf_encapcheck(m, off, proto, arg)
536
 	if (ip.ip_v != 4)
537
 		return 0;
538
 
539
-	ia6 = stf_getsrcifa6(STF2IFP(sc));
540
+	/* Lookup an ia6 whose IPv4 addr encoded in the IPv6 addr is valid. */
541
+	ia6 = stf_getsrcifa6(ifp);
542
 	if (ia6 == NULL)
543
 		return 0;
544
+	sin = stf_getin4addr(sc, &ia6_in4addr, &ia6->ia_ifa, STF_GETIN4_USE_CACHE);
545
+	if (sin == NULL)
546
+		return (0);
547
+
548
+#if STF_DEBUG > 3
549
+	{
550
+		char buf[INET6_ADDRSTRLEN + 1];
551
+		memset(&buf, 0, sizeof(buf));
552
+
553
+		ip6_sprintf(buf, &satosin6(ia6->ia_ifa.ifa_addr)->sin6_addr);
554
+		DEBUG_PRINTF(1, "%s: ia6->ia_ifa.ifa_addr = %s\n", __func__, buf);
555
+		ip6_sprintf(buf, &ia6->ia_addr.sin6_addr);
556
+		DEBUG_PRINTF(1, "%s: ia6->ia_addr = %s\n", __func__, buf);
557
+		ip6_sprintf(buf, &satosin6(ia6->ia_ifa.ifa_netmask)->sin6_addr);
558
+		DEBUG_PRINTF(1, "%s: ia6->ia_ifa.ifa_netmask = %s\n", __func__, buf);
559
+		ip6_sprintf(buf, &ia6->ia_prefixmask.sin6_addr);
560
+		DEBUG_PRINTF(1, "%s: ia6->ia_prefixmask = %s\n", __func__, buf);
561
+
562
+		ip_sprintf(buf, &ia6_in4addr.sin_addr);
563
+		DEBUG_PRINTF(1, "%s: ia6_in4addr.sin_addr = %s\n", __func__, buf);
564
+		ip_sprintf(buf, &ip.ip_src);
565
+		DEBUG_PRINTF(1, "%s: ip.ip_src = %s\n", __func__, buf);
566
+		ip_sprintf(buf, &ip.ip_dst);
567
+		DEBUG_PRINTF(1, "%s: ip.ip_dst = %s\n", __func__, buf);
568
+	}
569
+#endif
570
 
571
 	/*
572
 	 * check if IPv4 dst matches the IPv4 address derived from the
573
 	 * local 6to4 address.
574
 	 * success on: dst = 10.1.1.1, ia6->ia_addr = 2002:0a01:0101:...
575
 	 */
576
-	if (bcmp(GET_V4(&ia6->ia_addr.sin6_addr), &ip.ip_dst,
577
-	    sizeof(ip.ip_dst)) != 0) {
578
-		ifa_free(&ia6->ia_ifa);
579
-		return 0;
580
+	DEBUG_PRINTF(1, "%s: check1: ia6_in4addr.sin_addr == ip.ip_dst?\n", __func__);
581
+	if (ia6_in4addr.sin_addr.s_addr != ip.ip_dst.s_addr) {
582
+		DEBUG_PRINTF(1, "%s: check1: false.  Ignore this packet.\n", __func__);
583
+		goto freeit;
584
 	}
585
 
586
-	/*
587
-	 * check if IPv4 src matches the IPv4 address derived from the
588
-	 * local 6to4 address masked by prefixmask.
589
-	 * success on: src = 10.1.1.1, ia6->ia_addr = 2002:0a00:.../24
590
-	 * fail on: src = 10.1.1.1, ia6->ia_addr = 2002:0b00:.../24
591
-	 */
592
-	bzero(&a, sizeof(a));
593
-	bcopy(GET_V4(&ia6->ia_addr.sin6_addr), &a, sizeof(a));
594
-	bcopy(GET_V4(&ia6->ia_prefixmask.sin6_addr), &mask, sizeof(mask));
595
-	ifa_free(&ia6->ia_ifa);
596
-	a.s_addr &= mask.s_addr;
597
-	b = ip.ip_src;
598
-	b.s_addr &= mask.s_addr;
599
-	if (a.s_addr != b.s_addr)
600
-		return 0;
601
+	DEBUG_PRINTF(1, "%s: check2: ia6->ia_addr is 2002::/16?\n", __func__);
602
+	if (IN6_IS_ADDR_6TO4(&ia6->ia_addr.sin6_addr)) {
603
+		/* 6to4 (RFC 3056) */
604
+		/*
605
+		 * check if IPv4 src matches the IPv4 address derived
606
+		 * from the local 6to4 address masked by prefixmask.
607
+		 * success on: src = 10.1.1.1, ia6->ia_addr = 2002:0a00:.../24
608
+		 * fail on: src = 10.1.1.1, ia6->ia_addr = 2002:0b00:.../24
609
+		 */
610
+		DEBUG_PRINTF(1, "%s: check2: true.\n", __func__);
611
+
612
+		memcpy(&ia6_in4mask.sin_addr,
613
+		GET_V4(&ia6->ia_prefixmask.sin6_addr),
614
+		sizeof(ia6_in4mask));
615
+#if STF_DEBUG > 3
616
+		{
617
+			char buf[INET6_ADDRSTRLEN + 1];
618
+			memset(&buf, 0, sizeof(buf));
619
+
620
+			ip_sprintf(buf, &ia6_in4addr.sin_addr);
621
+			DEBUG_PRINTF(1, "%s: ia6->ia_addr = %s\n",
622
+				__func__, buf);
623
+			ip_sprintf(buf, &ip.ip_src);
624
+			DEBUG_PRINTF(1, "%s: ip.ip_src = %s\n",
625
+				__func__, buf);
626
+			ip_sprintf(buf, &ia6_in4mask.sin_addr);
627
+			DEBUG_PRINTF(1, "%s: ia6->ia_prefixmask = %s\n",
628
+				__func__, buf);
629
+
630
+			DEBUG_PRINTF(1, "%s: check3: ia6_in4addr.sin_addr & mask == ip.ip_src & mask\n",
631
+				__func__);
632
+		}
633
+#endif
634
+		  
635
+		if ((ia6_in4addr.sin_addr.s_addr & ia6_in4mask.sin_addr.s_addr) !=
636
+		    (ip.ip_src.s_addr & ia6_in4mask.sin_addr.s_addr)) {
637
+			DEBUG_PRINTF(1, "%s: check3: false.  Ignore this packet.\n",
638
+				__func__);
639
+			goto freeit;
640
+		}
641
+	} else {
642
+		/* 6rd (RFC 5569) */
643
+		DEBUG_PRINTF(1, "%s: check2: false.  6rd.\n", __func__);
644
+		/*
645
+		 * No restriction on the src address in the case of
646
+		 * 6rd because the stf(4) interface always has a
647
+		 * prefix which covers whole of IPv4 src address
648
+		 * range.  So, stf_output() will catch all of
649
+		 * 6rd-capsuled IPv4 traffic with suspicious inner dst
650
+		 * IPv4 address (i.e. the IPv6 destination address is
651
+		 * one the admin does not like to route to outside),
652
+		 * and then it discard them silently.
653
+		 */
654
+	}
655
+	DEBUG_PRINTF(1, "%s: all clear!\n", __func__);
656
 
657
 	/* stf interface makes single side match only */
658
-	return 32;
659
+	ret = 32;
660
+freeit:
661
+	ifa_free(&ia6->ia_ifa);
662
+
663
+	return (ret);
664
 }
665
 
666
 static struct in6_ifaddr *
667
-stf_getsrcifa6(ifp)
668
-	struct ifnet *ifp;
669
+stf_getsrcifa6(struct ifnet *ifp)
670
 {
671
-	struct ifaddr *ia;
672
+	struct ifaddr *ifa;
673
 	struct in_ifaddr *ia4;
674
-	struct sockaddr_in6 *sin6;
675
-	struct in_addr in;
676
+	struct sockaddr_in *sin;
677
+	struct sockaddr_in in4;
678
 
679
 	if_addr_rlock(ifp);
680
-	TAILQ_FOREACH(ia, &ifp->if_addrhead, ifa_link) {
681
-		if (ia->ifa_addr->sa_family != AF_INET6)
682
+	TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
683
+		if (ifa->ifa_addr->sa_family != AF_INET6)
684
 			continue;
685
-		sin6 = (struct sockaddr_in6 *)ia->ifa_addr;
686
-		if (!IN6_IS_ADDR_6TO4(&sin6->sin6_addr))
687
+
688
+		if ((sin = stf_getin4addr(ifp->if_softc, &in4, ifa,
689
+		    STF_GETIN4_USE_CACHE)) == NULL)
690
 			continue;
691
 
692
-		bcopy(GET_V4(&sin6->sin6_addr), &in, sizeof(in));
693
-		LIST_FOREACH(ia4, INADDR_HASH(in.s_addr), ia_hash)
694
-			if (ia4->ia_addr.sin_addr.s_addr == in.s_addr)
695
+		LIST_FOREACH(ia4, INADDR_HASH(sin->sin_addr.s_addr), ia_hash)
696
+			if (ia4->ia_addr.sin_addr.s_addr == sin->sin_addr.s_addr)
697
 				break;
698
 		if (ia4 == NULL)
699
 			continue;
700
 
701
-		ifa_ref(ia);
702
+#if STF_DEBUG > 3
703
+	{
704
+		char buf[INET6_ADDRSTRLEN + 1];
705
+		memset(&buf, 0, sizeof(buf));
706
+
707
+		ip6_sprintf(buf, &((struct sockaddr_in6 *)ifa->ifa_addr)->sin6_addr);
708
+		DEBUG_PRINTF(1, "%s: ifa->ifa_addr->sin6_addr = %s\n",
709
+			__func__, buf);
710
+		ip_sprintf(buf, &ia4->ia_addr.sin_addr);
711
+		DEBUG_PRINTF(1, "%s: ia4->ia_addr.sin_addr = %s\n",
712
+			__func__, buf);
713
+	}
714
+#endif
715
+
716
+		ifa_ref(ifa);
717
 		if_addr_runlock(ifp);
718
-		return (struct in6_ifaddr *)ia;
719
+		return (ifatoia6(ifa));
720
 	}
721
 	if_addr_runlock(ifp);
722
 
723
-	return NULL;
724
+	return (NULL);
725
 }
726
 
727
 static int
728
@@ -419,8 +544,8 @@ stf_output(struct ifnet *ifp, struct mbuf *m, const struct sockaddr *dst,
729
 	struct stf_softc *sc;
730
 	const struct sockaddr_in6 *dst6;
731
 	struct route *cached_route;
732
-	struct in_addr in4;
733
-	const void *ptr;
734
+	struct sockaddr_in *sin;
735
+	struct sockaddr_in in4;
736
 	struct sockaddr_in *dst4;
737
 	u_int8_t tos;
738
 	struct ip *ip;
739
@@ -472,20 +597,32 @@ stf_output(struct ifnet *ifp, struct mbuf *m, const struct sockaddr *dst,
740
 	/*
741
 	 * Pickup the right outer dst addr from the list of candidates.
742
 	 * ip6_dst has priority as it may be able to give us shorter IPv4 hops.
743
+	 *   ip6_dst: destination addr in the packet header.
744
+	 *   dst6: destination addr specified in function argument.
745
 	 */
746
-	ptr = NULL;
747
-	if (IN6_IS_ADDR_6TO4(&ip6->ip6_dst))
748
-		ptr = GET_V4(&ip6->ip6_dst);
749
-	else if (IN6_IS_ADDR_6TO4(&dst6->sin6_addr))
750
-		ptr = GET_V4(&dst6->sin6_addr);
751
+	DEBUG_PRINTF(1, "%s: dst addr selection\n", __func__);
752
+	if (sc->dstv4_addr != INADDR_ANY)
753
+		in4.sin_addr.s_addr = sc->dstv4_addr;
754
 	else {
755
-		ifa_free(&ia6->ia_ifa);
756
-		m_freem(m);
757
-		ifp->if_oerrors++;
758
-		return ENETUNREACH;
759
+		sin = stf_getin4addr_in6(sc, &in4, &ia6->ia_ifa, &ip6->ip6_dst);
760
+		if (sin == NULL)
761
+			sin = stf_getin4addr_in6(sc, &in4, &ia6->ia_ifa, &dst6->sin6_addr);
762
+		if (sin == NULL) {
763
+			ifa_free(&ia6->ia_ifa);
764
+			m_freem(m);
765
+			ifp->if_oerrors++;
766
+			return ENETUNREACH;
767
+		}
768
 	}
769
-	bcopy(ptr, &in4, sizeof(in4));
770
+#if STF_DEBUG > 3
771
+	{
772
+		char buf[INET6_ADDRSTRLEN + 1];
773
+		memset(&buf, 0, sizeof(buf));
774
 
775
+		ip_sprintf(buf, &in4.sin_addr);
776
+		DEBUG_PRINTF(1, "%s: ip_dst = %s\n", __func__, buf);
777
+	}
778
+#endif
779
 	if (bpf_peers_present(ifp->if_bpf)) {
780
 		/*
781
 		 * We need to prepend the address family as
782
@@ -509,11 +646,26 @@ stf_output(struct ifnet *ifp, struct mbuf *m, const struct sockaddr *dst,
783
 	ip = mtod(m, struct ip *);
784
 
785
 	bzero(ip, sizeof(*ip));
786
+	bcopy(&in4.sin_addr, &ip->ip_dst, sizeof(ip->ip_dst));
787
 
788
-	bcopy(GET_V4(&((struct sockaddr_in6 *)&ia6->ia_addr)->sin6_addr),
789
-	    &ip->ip_src, sizeof(ip->ip_src));
790
+	sin = stf_getin4addr_sin6(sc, &in4, &ia6->ia_ifa, &ia6->ia_addr);
791
+	if (sin == NULL) {
792
+		ifa_free(&ia6->ia_ifa);
793
+		m_freem(m);
794
+		ifp->if_oerrors++;
795
+		return ENETUNREACH;
796
+	}
797
+	bcopy(&in4.sin_addr, &ip->ip_src, sizeof(ip->ip_src));
798
+#if STF_DEBUG > 3
799
+	{
800
+		char buf[INET6_ADDRSTRLEN + 1];
801
+		memset(&buf, 0, sizeof(buf));
802
+
803
+		ip_sprintf(buf, &ip->ip_src);
804
+		DEBUG_PRINTF(1, "%s: ip_src = %s\n", __func__, buf);
805
+	}
806
+#endif
807
 	ifa_free(&ia6->ia_ifa);
808
-	bcopy(&in4, &ip->ip_dst, sizeof(ip->ip_dst));
809
 	ip->ip_p = IPPROTO_IPV6;
810
 	ip->ip_ttl = ip_stf_ttl;
811
 	ip->ip_len = htons(m->m_pkthdr.len);
812
@@ -522,7 +674,7 @@ stf_output(struct ifnet *ifp, struct mbuf *m, const struct sockaddr *dst,
813
 	else
814
 		ip_ecn_ingress(ECN_NOCARE, &ip->ip_tos, &tos);
815
 
816
-	if (!stf_route_cache) {
817
+	if (!V_stf_route_cache) {
818
 		cached_route = NULL;
819
 		goto sendit;
820
 	}
821
@@ -530,7 +682,7 @@ stf_output(struct ifnet *ifp, struct mbuf *m, const struct sockaddr *dst,
822
 	/*
823
 	 * Do we have a cached route?
824
 	 */
825
-	mtx_lock(&(sc)->sc_ro_mtx);
826
+	STF_LOCK(sc);
827
 	dst4 = (struct sockaddr_in *)&sc->sc_ro.ro_dst;
828
 	if (dst4->sin_family != AF_INET ||
829
 	    bcmp(&dst4->sin_addr, &ip->ip_dst, sizeof(ip->ip_dst)) != 0) {
830
@@ -548,8 +700,15 @@ stf_output(struct ifnet *ifp, struct mbuf *m, const struct sockaddr *dst,
831
 		rtalloc_fib(&sc->sc_ro, sc->sc_fibnum);
832
 		if (sc->sc_ro.ro_rt == NULL) {
833
 			m_freem(m);
834
-			mtx_unlock(&(sc)->sc_ro_mtx);
835
 			ifp->if_oerrors++;
836
+			STF_UNLOCK(sc);
837
+			return ENETUNREACH;
838
+		}
839
+		if (sc->sc_ro.ro_rt->rt_ifp == ifp) {
840
+			/* infinite loop detection */
841
+			m_free(m);
842
+			ifp->if_oerrors++;
843
+			STF_UNLOCK(sc);
844
 			return ENETUNREACH;
845
 		}
846
 	}
847
@@ -558,35 +717,33 @@ stf_output(struct ifnet *ifp, struct mbuf *m, const struct sockaddr *dst,
848
 sendit:
849
 	M_SETFIB(m, sc->sc_fibnum);
850
 	ifp->if_opackets++;
851
+	DEBUG_PRINTF(1, "%s: ip_output dispatch.\n", __func__);
852
 	error = ip_output(m, NULL, cached_route, 0, NULL, NULL);
853
 
854
 	if (cached_route != NULL)
855
-		mtx_unlock(&(sc)->sc_ro_mtx);
856
-	return error;
857
+		STF_UNLOCK(sc);
858
+
859
+	return (error);
860
 }
861
 
862
 static int
863
-isrfc1918addr(in)
864
-	struct in_addr *in;
865
+isrfc1918addr(struct in_addr *in)
866
 {
867
 	/*
868
 	 * returns 1 if private address range:
869
 	 * 10.0.0.0/8 172.16.0.0/12 192.168.0.0/16
870
 	 */
871
 	if (stf_permit_rfc1918 == 0 && (
872
-	    (ntohl(in->s_addr) & 0xff000000) >> 24 == 10 ||
873
-	    (ntohl(in->s_addr) & 0xfff00000) >> 16 == 172 * 256 + 16 ||
874
-	    (ntohl(in->s_addr) & 0xffff0000) >> 16 == 192 * 256 + 168))
875
+	    (ntohl(in->s_addr) & 0xff000000) == 10 << 24 ||
876
+	    (ntohl(in->s_addr) & 0xfff00000) == (172 * 256 + 16) << 16 ||
877
+	    (ntohl(in->s_addr) & 0xffff0000) == (192 * 256 + 168) << 16 ))
878
 		return 1;
879
 
880
 	return 0;
881
 }
882
 
883
 static int
884
-stf_checkaddr4(sc, in, inifp)
885
-	struct stf_softc *sc;
886
-	struct in_addr *in;
887
-	struct ifnet *inifp;	/* incoming interface */
888
+stf_checkaddr4(struct stf_softc *sc, struct in_addr *in, struct ifnet *inifp)
889
 {
890
 	struct in_ifaddr *ia4;
891
 
892
@@ -602,13 +759,6 @@ stf_checkaddr4(sc, in, inifp)
893
 	}
894
 
895
 	/*
896
-	 * reject packets with private address range.
897
-	 * (requirement from RFC3056 section 2 1st paragraph)
898
-	 */
899
-	if (isrfc1918addr(in))
900
-		return -1;
901
-
902
-	/*
903
 	 * reject packets with broadcast
904
 	 */
905
 	IN_IFADDR_RLOCK();
906
@@ -631,7 +781,7 @@ stf_checkaddr4(sc, in, inifp)
907
 
908
 		bzero(&sin, sizeof(sin));
909
 		sin.sin_family = AF_INET;
910
-		sin.sin_len = sizeof(struct sockaddr_in);
911
+		sin.sin_len = sizeof(sin);
912
 		sin.sin_addr = *in;
913
 		rt = rtalloc1_fib((struct sockaddr *)&sin, 0,
914
 		    0UL, sc->sc_fibnum);
915
@@ -652,10 +802,7 @@ stf_checkaddr4(sc, in, inifp)
916
 }
917
 
918
 static int
919
-stf_checkaddr6(sc, in6, inifp)
920
-	struct stf_softc *sc;
921
-	struct in6_addr *in6;
922
-	struct ifnet *inifp;	/* incoming interface */
923
+stf_checkaddr6(struct stf_softc *sc, struct in6_addr *in6, struct ifnet *inifp)
924
 {
925
 	/*
926
 	 * check 6to4 addresses
927
@@ -679,9 +826,7 @@ stf_checkaddr6(sc, in6, inifp)
928
 }
929
 
930
 void
931
-in_stf_input(m, off)
932
-	struct mbuf *m;
933
-	int off;
934
+in_stf_input(struct mbuf *m, int off)
935
 {
936
 	int proto;
937
 	struct stf_softc *sc;
938
@@ -689,6 +834,7 @@ in_stf_input(m, off)
939
 	struct ip6_hdr *ip6;
940
 	u_int8_t otos, itos;
941
 	struct ifnet *ifp;
942
+	struct route_in6 rin6;
943
 
944
 	proto = mtod(m, struct ip *)->ip_p;
945
 
946
@@ -712,6 +858,17 @@ in_stf_input(m, off)
947
 	mac_ifnet_create_mbuf(ifp, m);
948
 #endif
949
 
950
+#if STF_DEBUG > 3
951
+	{
952
+		char buf[INET6_ADDRSTRLEN + 1];
953
+		memset(&buf, 0, sizeof(buf));
954
+
955
+		ip_sprintf(buf, &ip->ip_dst);
956
+		DEBUG_PRINTF(1, "%s: ip->ip_dst = %s\n", __func__, buf);
957
+		ip_sprintf(buf, &ip->ip_src);
958
+		DEBUG_PRINTF(1, "%s: ip->ip_src = %s\n", __func__, buf);
959
+	}
960
+#endif
961
 	/*
962
 	 * perform sanity check against outer src/dst.
963
 	 * for source, perform ingress filter as well.
964
@@ -732,6 +889,17 @@ in_stf_input(m, off)
965
 	}
966
 	ip6 = mtod(m, struct ip6_hdr *);
967
 
968
+#if STF_DEBUG > 3
969
+	{
970
+		char buf[INET6_ADDRSTRLEN + 1];
971
+		memset(&buf, 0, sizeof(buf));
972
+
973
+		ip6_sprintf(buf, &ip6->ip6_dst);
974
+		DEBUG_PRINTF(1, "%s: ip6->ip6_dst = %s\n", __func__, buf);
975
+		ip6_sprintf(buf, &ip6->ip6_src);
976
+		DEBUG_PRINTF(1, "%s: ip6->ip6_src = %s\n", __func__, buf);
977
+	}
978
+#endif
979
 	/*
980
 	 * perform sanity check against inner src/dst.
981
 	 * for source, perform ingress filter as well.
982
@@ -742,6 +910,41 @@ in_stf_input(m, off)
983
 		return;
984
 	}
985
 
986
+	/*
987
+	 * reject packets with private address range.
988
+	 * (requirement from RFC3056 section 2 1st paragraph)
989
+	 */
990
+	if ((IN6_IS_ADDR_6TO4(&ip6->ip6_src) && isrfc1918addr(&ip->ip_src)) ||
991
+	    (IN6_IS_ADDR_6TO4(&ip6->ip6_dst) && isrfc1918addr(&ip->ip_dst))) {
992
+		m_freem(m);
993
+		return;
994
+	}
995
+
996
+	/*
997
+	 * Ignore if the destination is the same stf interface because
998
+	 * all of valid IPv6 outgoing traffic should go interfaces
999
+	 * except for it.
1000
+	 */
1001
+	memset(&rin6, 0, sizeof(rin6));
1002
+	rin6.ro_dst.sin6_len = sizeof(rin6.ro_dst);
1003
+	rin6.ro_dst.sin6_family = AF_INET6;
1004
+	memcpy(&rin6.ro_dst.sin6_addr, &ip6->ip6_dst,
1005
+	       sizeof(rin6.ro_dst.sin6_addr));
1006
+	rtalloc((struct route *)&rin6);
1007
+	if (rin6.ro_rt == NULL) {
1008
+		DEBUG_PRINTF(1, "%s: no IPv6 dst.  Ignored.\n", __func__);
1009
+		m_free(m);
1010
+		return;
1011
+	}
1012
+	if ((rin6.ro_rt->rt_ifp == ifp) &&
1013
+	    (!IN6_ARE_ADDR_EQUAL(&ip6->ip6_src, &rin6.ro_dst.sin6_addr))) {
1014
+		DEBUG_PRINTF(1, "%s: IPv6 dst is the same stf.  Ignored.\n", __func__);
1015
+		RTFREE(rin6.ro_rt);
1016
+		m_free(m);
1017
+		return;
1018
+	}
1019
+	RTFREE(rin6.ro_rt);
1020
+
1021
 	itos = (ntohl(ip6->ip6_flow) >> 20) & 0xff;
1022
 	if ((ifp->if_flags & IFF_LINK1) != 0)
1023
 		ip_ecn_egress(ECN_ALLOWED, &otos, &itos);
1024
@@ -751,7 +954,7 @@ in_stf_input(m, off)
1025
 	ip6->ip6_flow |= htonl((u_int32_t)itos << 20);
1026
 
1027
 	m->m_pkthdr.rcvif = ifp;
1028
-	
1029
+
1030
 	if (bpf_peers_present(ifp->if_bpf)) {
1031
 		/*
1032
 		 * We need to prepend the address family as
1033
@@ -764,6 +967,7 @@ in_stf_input(m, off)
1034
 		bpf_mtap2(ifp->if_bpf, &af, sizeof(af), m);
1035
 	}
1036
 
1037
+	DEBUG_PRINTF(1, "%s: netisr_dispatch(NETISR_IPV6)\n", __func__);
1038
 	/*
1039
 	 * Put the packet to the network layer input queue according to the
1040
 	 * specified address family.
1041
@@ -778,46 +982,356 @@ in_stf_input(m, off)
1042
 
1043
 /* ARGSUSED */
1044
 static void
1045
-stf_rtrequest(cmd, rt, info)
1046
-	int cmd;
1047
-	struct rtentry *rt;
1048
-	struct rt_addrinfo *info;
1049
+stf_rtrequest(int cmd, struct rtentry *rt, struct rt_addrinfo *info)
1050
 {
1051
+
1052
 	RT_LOCK_ASSERT(rt);
1053
 	rt->rt_mtu = rt->rt_ifp->if_mtu;
1054
 }
1055
 
1056
+static struct sockaddr_in *
1057
+stf_getin4addr_in6(struct stf_softc *sc, struct sockaddr_in *sin,
1058
+      struct ifaddr *ifa,
1059
+      const struct in6_addr *in6)
1060
+{
1061
+      struct sockaddr_in6 sin6;
1062
+
1063
+      DEBUG_PRINTF(1, "%s: enter.\n", __func__);
1064
+      if (ifa == NULL || in6 == NULL)
1065
+              return NULL;
1066
+
1067
+      memset(&sin6, 0, sizeof(sin6));
1068
+      memcpy(&sin6.sin6_addr, in6, sizeof(sin6.sin6_addr));
1069
+      sin6.sin6_len = sizeof(sin6);
1070
+      sin6.sin6_family = AF_INET6;
1071
+
1072
+      return(stf_getin4addr_sin6(sc, sin, ifa, &sin6));
1073
+}
1074
+
1075
+static struct sockaddr_in *
1076
+stf_getin4addr_sin6(struct stf_softc *sc, struct sockaddr_in *sin,
1077
+      struct ifaddr *ifa,
1078
+      struct sockaddr_in6 *sin6)
1079
+{
1080
+      struct in6_ifaddr ia6;
1081
+      int i;
1082
+
1083
+      DEBUG_PRINTF(1, "%s: enter.\n", __func__);
1084
+      if (ifa == NULL || sin6 == NULL)
1085
+      return NULL;
1086
+
1087
+      memset(&ia6, 0, sizeof(ia6));
1088
+      memcpy(&ia6, ifatoia6(ifa), sizeof(ia6));
1089
+
1090
+      /*
1091
+      * Use prefixmask information from ifa, and
1092
+      * address information from sin6.
1093
+      */
1094
+      ia6.ia_addr.sin6_family = AF_INET6;
1095
+      ia6.ia_ifa.ifa_addr = (struct sockaddr *)&ia6.ia_addr;
1096
+      ia6.ia_ifa.ifa_dstaddr = NULL;
1097
+      ia6.ia_ifa.ifa_netmask = (struct sockaddr *)&ia6.ia_prefixmask;
1098
+
1099
+#if STF_DEBUG > 3
1100
+      {
1101
+              char buf[INET6_ADDRSTRLEN 1];
1102
+              memset(&buf, 0, sizeof(buf));
1103
+
1104
+              ip6_sprintf(buf, &sin6->sin6_addr);
1105
+              DEBUG_PRINTF(1, "%s: sin6->sin6_addr = %s\n", __func__, buf);
1106
+              ip6_sprintf(buf, &ia6.ia_addr.sin6_addr);
1107
+              DEBUG_PRINTF(1, "%s: ia6.ia_addr.sin6_addr = %s\n", __func__, buf);
1108
+              ip6_sprintf(buf, &ia6.ia_prefixmask.sin6_addr);
1109
+              DEBUG_PRINTF(1, "%s: ia6.ia_prefixmask.sin6_addr = %s\n", __func__, buf);
1110
+      }
1111
+#endif
1112
+
1113
+      /*
1114
+      * When (src addr & src mask) != (dst (sin6) addr & src mask),
1115
+      * the dst is not in the 6rd domain.  The IPv4 address must
1116
+      * not be used.
1117
+      */
1118
+      for (i = 0; i < sizeof(ia6.ia_addr.sin6_addr); i++) {
1119
+              if ((((u_char *)&ia6.ia_addr.sin6_addr)[i] &
1120
+                  ((u_char *)&ia6.ia_prefixmask.sin6_addr)[i])
1121
+                  !=
1122
+                  (((u_char *)&sin6->sin6_addr)[i] &
1123
+                  ((u_char *)&ia6.ia_prefixmask.sin6_addr)[i]))
1124
+              return NULL;
1125
+      }
1126
+
1127
+      /* After the mask check, overwrite ia6.ia_addr with sin6. */
1128
+      memcpy(&ia6.ia_addr, sin6, sizeof(ia6.ia_addr));
1129
+      return(stf_getin4addr(sc, sin, (struct ifaddr *)&ia6, 0));
1130
+}
1131
+
1132
+static struct sockaddr_in *
1133
+stf_getin4addr(struct stf_softc *sc, struct sockaddr_in *sin,
1134
+      struct ifaddr *ifa,
1135
+      int flags)
1136
+{
1137
+      struct in_addr *in;
1138
+      struct sockaddr_in6 *sin6;
1139
+      struct in6_ifaddr *ia6;
1140
+
1141
+      DEBUG_PRINTF(1, "%s: enter.\n", __func__);
1142
+      if (ifa == NULL ||
1143
+          ifa->ifa_addr == NULL ||
1144
+          ifa->ifa_addr->sa_family != AF_INET6)
1145
+      return NULL;
1146
+
1147
+      sin6 = satosin6(ifa->ifa_addr);
1148
+      ia6 = ifatoia6(ifa);
1149
+
1150
+      if ((flags & STF_GETIN4_USE_CACHE) &&
1151
+          (ifa->ifa_dstaddr != NULL) &&
1152
+          (ifa->ifa_dstaddr->sa_family == AF_INET)) {
1153
+              /*
1154
+               * XXX: ifa_dstaddr is used as a cache of the
1155
+               * extracted IPv4 address.
1156
+               */
1157
+              memcpy(sin, satosin(ifa->ifa_dstaddr), sizeof(*sin));
1158
+
1159
+#if STF_DEBUG > 3
1160
+              {
1161
+                      char tmpbuf[INET6_ADDRSTRLEN 1];
1162
+                      memset(&tmpbuf, 0, INET6_ADDRSTRLEN);
1163
+
1164
+                      ip_sprintf(tmpbuf, &sin->sin_addr);
1165
+                      DEBUG_PRINTF(1, "%s: cached address was used = %s\n", __func__, tmpbuf);
1166
+              }
1167
+#endif
1168
+              return (sin);
1169
+      }
1170
+      memset(sin, 0, sizeof(*sin));
1171
+      in = &sin->sin_addr;
1172
+
1173
+#if STF_DEBUG > 3
1174
+      {
1175
+              char tmpbuf[INET6_ADDRSTRLEN 1];
1176
+              memset(&tmpbuf, 0, INET6_ADDRSTRLEN);
1177
+
1178
+              ip6_sprintf(tmpbuf, &sin6->sin6_addr);
1179
+              DEBUG_PRINTF(1, "%s: sin6->sin6_addr = %s\n", __func__, tmpbuf);
1180
+      }
1181
+#endif
1182
+
1183
+      if (IN6_IS_ADDR_6TO4(&sin6->sin6_addr)) {
1184
+              /* 6to4 (RFC 3056) */
1185
+              bcopy(GET_V4(&sin6->sin6_addr), in, sizeof(*in));
1186
+              if (isrfc1918addr(in))
1187
+                      return NULL;
1188
+      } else {
1189
+              /* 6rd (RFC 5569) */
1190
+              struct in6_addr buf;
1191
+              u_char *p = (u_char *)&buf;
1192
+              u_char *q = (u_char *)&in->s_addr;
1193
+              u_int residue = 0, v4residue = 0;
1194
+              u_char mask, v4mask = 0;
1195
+              int i;
1196
+              u_int plen, loop;
1197
+
1198
+              /*
1199
+               * 6rd-relays IPv6 prefix is located at a 32-bit just
1200
+               * after the prefix edge.
1201
+               */
1202
+              plen = in6_mask2len(&satosin6(ifa->ifa_netmask)->sin6_addr, NULL);
1203
+              if (64 < plen) {
1204
+                      DEBUG_PRINTF(1, "prefixlen is %d\n", plen);
1205
+                      return NULL;
1206
+              }
1207
+
1208
+              memcpy(&buf, &sin6->sin6_addr, sizeof(buf));
1209
+              if (sc->v4prefixlen == 0 || sc->v4prefixlen == 32)
1210
+                      loop = 4; /* Normal 6rd operation */
1211
+              else {
1212
+                      loop = sc->v4prefixlen / 8;
1213
+                      v4residue = sc->v4prefixlen % 8;
1214
+              }
1215
+
1216
+              p += plen / 8;
1217
+              residue = plen % 8;
1218
+              mask = ~((u_char)(-1) >> residue);
1219
+              if (v4residue) {
1220
+                      loop++;
1221
+                      v4mask = ((u_char)(-1) << v4residue);
1222
+              }
1223
+              /*
1224
+               * The p points head of the IPv4 address part in
1225
+               * bytes.  The residue is a bit-shift factor when
1226
+               * prefixlen is not a multiple of 8.
1227
+               */
1228
+              DEBUG_PRINTF(2, "residue = %d\n", residue);
1229
+              for (i = loop; i >= 0; i--) {
1230
+                      if (residue) {
1231
+                              DEBUG_PRINTF(2, "p[%d] << residue = %d-%x/%x\n",
1232
+                                      i, p[i], p[i], p[i] >> residue);
1233
+                              p[i] = (p[i] >> residue);
1234
+                              DEBUG_PRINTF(2, "p[%d] = %d/%x - p[%d 1] = %d-%x/%x\n",
1235
+                                      i, p[i], p[i], i, p[i - 1], p[i - 1], p[i - 1] << (8 - residue));
1236
+                              p[i] |= (p[i - 1] << (8 - residue));
1237
+                      }
1238
+                      q[i - 1] = p[i];
1239
+                      DEBUG_PRINTF(2, "FINAL q[%d] - p[%d] %d/%x\n",
1240
+                              i, i, q[i - 1], q[i]);
1241
+              }
1242
+              if (v4residue)
1243
+              q[i + 1] &= v4mask;
1244
+
1245
+              if (sc->v4prefixlen)
1246
+              in->s_addr |= (sc->inaddr & ((uint32_t)(-1) >> sc->v4prefixlen));
1247
+      }
1248
+
1249
+#if STF_DEBUG > 3
1250
+      {
1251
+              char tmpbuf[INET6_ADDRSTRLEN 1];
1252
+              memset(&tmpbuf, 0, INET_ADDRSTRLEN);
1253
+
1254
+              ip_sprintf(tmpbuf, in);
1255
+              DEBUG_PRINTF(1, "%s: in->in_addr = %s\n", __func__, tmpbuf);
1256
+              DEBUG_PRINTF(1, "%s: leave\n", __func__);
1257
+      }
1258
+#endif
1259
+
1260
+      if (flags & STF_GETIN4_USE_CACHE) {
1261
+              DEBUG_PRINTF(1, "%s: try to access ifa->ifa_dstaddr.\n", __func__);
1262
+              ifa->ifa_dstaddr = (struct sockaddr *)&ia6->ia_dstaddr;
1263
+              DEBUG_PRINTF(1, "%s: try to memset 0 to ia_dstaddr.\n", __func__);
1264
+                      memset(&ia6->ia_dstaddr, 0, sizeof(ia6->ia_dstaddr));
1265
+              DEBUG_PRINTF(1, "%s: try to memcpy ifa->ifa_dstaddr.\n", __func__);
1266
+                      memcpy((struct sockaddr_in *)ifa->ifa_dstaddr,
1267
+                      sin, sizeof(struct sockaddr_in));
1268
+              DEBUG_PRINTF(1, "%s: try to set sa_family.\n", __func__);
1269
+              ifa->ifa_dstaddr->sa_family = AF_INET;
1270
+              DEBUG_PRINTF(1, "%s: in->in_addr is stored in ifa_dstaddr.\n",
1271
+                      __func__);
1272
+      }
1273
+
1274
+      return (sin);
1275
+}
1276
+
1277
+
1278
 static int
1279
-stf_ioctl(ifp, cmd, data)
1280
-	struct ifnet *ifp;
1281
-	u_long cmd;
1282
-	caddr_t data;
1283
+stf_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
1284
 {
1285
+	struct stf_softc *sc, *sc_cur;
1286
 	struct ifaddr *ifa;
1287
 	struct ifreq *ifr;
1288
-	struct sockaddr_in6 *sin6;
1289
-	struct in_addr addr;
1290
+	struct sockaddr_in in4;
1291
+	struct stfv4args args;
1292
+	struct in6_ifaddr *ia6;
1293
+	struct ifdrv *ifd;
1294
 	int error, mtu;
1295
 
1296
 	error = 0;
1297
+	sc_cur = ifp->if_softc;
1298
+
1299
 	switch (cmd) {
1300
+	case SIOCSDRVSPEC:
1301
+		ifd = (struct ifdrv *) data;
1302
+		error = priv_check(curthread, PRIV_NET_ADDIFADDR);
1303
+		if (error)
1304
+			break;
1305
+		if (ifd->ifd_cmd == STF_SV4NET) {
1306
+			if (ifd->ifd_len != sizeof(args)) {
1307
+				error = EINVAL;
1308
+				break;
1309
+			}
1310
+			mtx_lock(&stf_mtx);
1311
+			LIST_FOREACH(sc, &V_stf_softc_list, stf_list) {
1312
+				if (sc == sc_cur)
1313
+					continue;
1314
+				if (sc->inaddr == 0 || sc->v4prefixlen == 0)
1315
+					continue;
1316
+
1317
+				if ((ntohl(sc->inaddr) & ((uint32_t)(-1) << sc_cur->v4prefixlen)) == ntohl(sc_cur->inaddr)) {
1318
+					error = EEXIST;
1319
+					mtx_unlock(&stf_mtx);
1320
+					return (error);
1321
+				}
1322
+				if ((ntohl(sc_cur->inaddr) & ((uint32_t)(-1) << sc->v4prefixlen)) == ntohl(sc->inaddr)) {
1323
+					error = EEXIST;
1324
+					mtx_unlock(&stf_mtx);
1325
+					return (error);
1326
+				}
1327
+			}
1328
+			mtx_unlock(&stf_mtx);
1329
+			bzero(&args, sizeof args);
1330
+			error = copyin(ifd->ifd_data, &args, ifd->ifd_len); 
1331
+			if (error)
1332
+				break;
1333
+
1334
+			sc_cur->inaddr = ntohl(args.inaddr.s_addr);
1335
+			sc_cur->inaddr &= ((uint32_t)(-1) << args.prefix);
1336
+			sc_cur->inaddr = htonl(sc_cur->inaddr);
1337
+			sc_cur->v4prefixlen = args.prefix;
1338
+			if (sc_cur->v4prefixlen == 32)
1339
+				sc_cur->v4prefixlen = 0;
1340
+		} else if (ifd->ifd_cmd == STF_SDSTV4) {
1341
+			if (ifd->ifd_len != sizeof(args)) {
1342
+				error = EINVAL;
1343
+				break;
1344
+			}
1345
+			bzero(&args, sizeof args);
1346
+			error = copyin(ifd->ifd_data, &args, ifd->ifd_len); 
1347
+			if (error)
1348
+				break;
1349
+			sc_cur->dstv4_addr = args.dstv4_addr.s_addr;
1350
+		} else
1351
+			error = EINVAL;
1352
+		break;
1353
+	case SIOCGDRVSPEC:
1354
+		ifd = (struct ifdrv *) data;
1355
+		if (ifd->ifd_len != sizeof(args)) {
1356
+			error = EINVAL;
1357
+			break;
1358
+		}
1359
+		if (ifd->ifd_cmd != STF_GV4NET) {
1360
+			error = EINVAL;
1361
+			break;
1362
+		}
1363
+		bzero(&args, sizeof args);
1364
+		args.inaddr.s_addr = sc_cur->inaddr;
1365
+		args.dstv4_addr.s_addr = sc_cur->dstv4_addr;
1366
+		args.prefix = sc_cur->v4prefixlen;
1367
+		error = copyout(&args, ifd->ifd_data, ifd->ifd_len); 
1368
+
1369
+		break;
1370
 	case SIOCSIFADDR:
1371
 		ifa = (struct ifaddr *)data;
1372
 		if (ifa == NULL || ifa->ifa_addr->sa_family != AF_INET6) {
1373
 			error = EAFNOSUPPORT;
1374
 			break;
1375
 		}
1376
-		sin6 = (struct sockaddr_in6 *)ifa->ifa_addr;
1377
-		if (!IN6_IS_ADDR_6TO4(&sin6->sin6_addr)) {
1378
+		if (stf_getin4addr(sc_cur, &in4, ifa, 0) == NULL) {
1379
 			error = EINVAL;
1380
 			break;
1381
 		}
1382
-		bcopy(GET_V4(&sin6->sin6_addr), &addr, sizeof(addr));
1383
-		if (isrfc1918addr(&addr)) {
1384
-			error = EINVAL;
1385
-			break;
1386
+		/*
1387
+		 * Sanity check: if more than two interfaces have IFF_UP, do
1388
+		 * if_down() for all of them except for the specified one.
1389
+		 */
1390
+		mtx_lock(&stf_mtx);
1391
+		LIST_FOREACH(sc, &V_stf_softc_list, stf_list) {
1392
+			if (sc == sc_cur)
1393
+				continue;
1394
+			if ((ia6 = stf_getsrcifa6(sc->sc_ifp)) == NULL)
1395
+				continue;
1396
+			if (IN6_ARE_ADDR_EQUAL(&ia6->ia_addr.sin6_addr, &ifatoia6(ifa)->ia_addr.sin6_addr)) {
1397
+				error = EEXIST;
1398
+				ifa_free(&ia6->ia_ifa);
1399
+				break;
1400
+			}
1401
+			ifa_free(&ia6->ia_ifa);
1402
 		}
1403
-
1404
+		mtx_unlock(&stf_mtx);
1405
+  
1406
+		/*
1407
+		 * XXX: ifa_dstaddr is used as a cache of the
1408
+		 * extracted IPv4 address.
1409
+		 */
1410
+		if (ifa->ifa_dstaddr != NULL)
1411
+			ifa->ifa_dstaddr->sa_family = AF_UNSPEC;
1412
 		ifa->ifa_rtrequest = stf_rtrequest;
1413
 		ifp->if_flags |= IFF_UP;
1414
 		break;
1415
@@ -849,4 +1363,5 @@ stf_ioctl(ifp, cmd, data)
1416
 	}
1417
 
1418
 	return error;
1419
+
1420
 }
1421
diff --git a/sys/net/if_stf.h b/sys/net/if_stf.h
1422
index cbaf670..e6ff29e 100644
1423
--- a/sys/net/if_stf.h
1424
+++ b/sys/net/if_stf.h
1425
@@ -33,6 +33,15 @@
1426
 #ifndef _NET_IF_STF_H_
1427
 #define _NET_IF_STF_H_
1428
 
1429
+struct stfv4args {
1430
+	struct in_addr inaddr;
1431
+	struct in_addr dstv4_addr;
1432
+	int prefix;
1433
+};
1434
+
1435
+#define	STF_SV4NET	1
1436
+#define	STF_GV4NET	2
1437
+#define	STF_SDSTV4	3
1438
 void in_stf_input(struct mbuf *, int);
1439
 
1440
 #endif /* _NET_IF_STF_H_ */
(60-60/64)