Projet

Général

Profil

Télécharger (42 ko) Statistiques
| Branche: | Révision:

univnautes-tools / patches / stable / 10 / stf_6rd.diff @ 2e75006f

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..532750b 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,66 @@ 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
+#define	STF_GETIN4_USE_CACHE  1
349
+static struct sockaddr_in *stf_getin4addr(struct stf_softc *, struct sockaddr_in *,
350
+	struct ifaddr *, int);
351
+static struct sockaddr_in *stf_getin4addr_in6(struct stf_softc *, struct sockaddr_in *,
352
+	struct ifaddr *, const struct in6_addr *);
353
+static struct sockaddr_in *stf_getin4addr_sin6(struct stf_softc *, struct sockaddr_in *,
354
+	struct ifaddr *, struct sockaddr_in6 *);
355
 static int stf_clone_create(struct if_clone *, char *, size_t, caddr_t);
356
 static int stf_clone_destroy(struct if_clone *, struct ifnet *);
357
+static int stf_clone_match(struct if_clone *, const char *);
358
+
359
 static struct if_clone *stf_cloner;
360
 
361
 static int
362
 stf_clone_match(struct if_clone *ifc, const char *name)
363
 {
364
-	int i;
365
+	const char *cp;
366
 
367
-	for(i = 0; stfnames[i] != NULL; i++) {
368
-		if (strcmp(stfnames[i], name) == 0)
369
-			return (1);
370
-	}
371
+	if (strncmp(stfname, name, strlen(stfname)) != 0)
372
+		return (0);
373
 
374
-	return (0);
375
+	for (cp = name + 3; *cp != '\0'; cp++) {
376
+		if (*cp < '0' || *cp > '9')
377
+			return (0);
378
+	}
379
+	return (1);
380
 }
381
 
382
 static int
383
 stf_clone_create(struct if_clone *ifc, char *name, size_t len, caddr_t params)
384
 {
385
-	int err, unit;
386
 	struct stf_softc *sc;
387
 	struct ifnet *ifp;
388
+	int unit, error;
389
 
390
-	/*
391
-	 * We can only have one unit, but since unit allocation is
392
-	 * already locked, we use it to keep from allocating extra
393
-	 * interfaces.
394
-	 */
395
-	unit = STFUNIT;
396
-	err = ifc_alloc_unit(ifc, &unit);
397
-	if (err != 0)
398
-		return (err);
399
+	error = ifc_name2unit(name, &unit);
400
+	if (error != 0)
401
+		return (error);
402
+
403
+	error = ifc_alloc_unit(ifc, &unit);
404
+	if (error != 0)
405
+		return (error);
406
 
407
 	sc = malloc(sizeof(struct stf_softc), M_STF, M_WAITOK | M_ZERO);
408
+	sc->sc_fibnum = curthread->td_proc->p_fibnum;
409
 	ifp = STF2IFP(sc) = if_alloc(IFT_STF);
410
-	if (ifp == NULL) {
411
+	if (sc->sc_ifp == NULL) {
412
 		free(sc, M_STF);
413
-		ifc_free_unit(ifc, unit);
414
-		return (ENOSPC);
415
+		return (ENOMEM);
416
 	}
417
+	STF_LOCK_INIT(sc);
418
 	ifp->if_softc = sc;
419
-	sc->sc_fibnum = curthread->td_proc->p_fibnum;
420
 
421
-	/*
422
-	 * Set the name manually rather then using if_initname because
423
-	 * we don't conform to the default naming convention for interfaces.
424
-	 */
425
-	strlcpy(ifp->if_xname, name, IFNAMSIZ);
426
-	ifp->if_dname = stfname;
427
-	ifp->if_dunit = IF_DUNIT_NONE;
428
+	if_initname(ifp, name, unit);
429
 
430
-	mtx_init(&(sc)->sc_ro_mtx, "stf ro", NULL, MTX_DEF);
431
 	sc->encap_cookie = encap_attach_func(AF_INET, IPPROTO_IPV6,
432
 	    stf_encapcheck, &in_stf_protosw, sc);
433
 	if (sc->encap_cookie == NULL) {
434
 		if_printf(ifp, "attach failed\n");
435
 		free(sc, M_STF);
436
-		ifc_free_unit(ifc, unit);
437
 		return (ENOMEM);
438
 	}
439
 
440
@@ -260,6 +308,11 @@ stf_clone_create(struct if_clone *ifc, char *name, size_t len, caddr_t params)
441
 	ifp->if_snd.ifq_maxlen = ifqmaxlen;
442
 	if_attach(ifp);
443
 	bpfattach(ifp, DLT_NULL, sizeof(u_int32_t));
444
+
445
+	mtx_lock(&stf_mtx);
446
+	LIST_INSERT_HEAD(&V_stf_softc_list, sc, stf_list);
447
+	mtx_unlock(&stf_mtx);
448
+
449
 	return (0);
450
 }
451
 
452
@@ -267,35 +320,48 @@ static int
453
 stf_clone_destroy(struct if_clone *ifc, struct ifnet *ifp)
454
 {
455
 	struct stf_softc *sc = ifp->if_softc;
456
+	int unit = ifp->if_dunit;
457
 	int err;
458
 
459
+	mtx_lock(&stf_mtx);
460
+	LIST_REMOVE(sc, stf_list);
461
+	mtx_unlock(&stf_mtx);
462
+
463
 	err = encap_detach(sc->encap_cookie);
464
 	KASSERT(err == 0, ("Unexpected error detaching encap_cookie"));
465
-	mtx_destroy(&(sc)->sc_ro_mtx);
466
 	bpfdetach(ifp);
467
 	if_detach(ifp);
468
 	if_free(ifp);
469
 
470
+	STF_LOCK_DESTROY(sc);
471
 	free(sc, M_STF);
472
-	ifc_free_unit(ifc, STFUNIT);
473
+	ifc_free_unit(ifc, unit);
474
 
475
 	return (0);
476
 }
477
 
478
+static void
479
+vnet_stf_init(const void *unused __unused)
480
+{
481
+
482
+	LIST_INIT(&V_stf_softc_list);
483
+}
484
+VNET_SYSINIT(vnet_stf_init, SI_SUB_PSEUDO, SI_ORDER_MIDDLE, vnet_stf_init,
485
+	NULL);
486
+
487
 static int
488
-stfmodevent(mod, type, data)
489
-	module_t mod;
490
-	int type;
491
-	void *data;
492
+stfmodevent(module_t mod, int type, void *data)
493
 {
494
 
495
 	switch (type) {
496
 	case MOD_LOAD:
497
+		mtx_init(&stf_mtx, "stf_mtx", NULL, MTX_DEF);
498
 		stf_cloner = if_clone_advanced(stfname, 0, stf_clone_match,
499
 		    stf_clone_create, stf_clone_destroy);
500
 		break;
501
 	case MOD_UNLOAD:
502
 		if_clone_detach(stf_cloner);
503
+		mtx_destroy(&stf_mtx);
504
 		break;
505
 	default:
506
 		return (EOPNOTSUPP);
507
@@ -311,28 +377,31 @@ static moduledata_t stf_mod = {
508
 };
509
 
510
 DECLARE_MODULE(if_stf, stf_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
511
+MODULE_VERSION(if_stf, 1);
512
 
513
 static int
514
-stf_encapcheck(m, off, proto, arg)
515
-	const struct mbuf *m;
516
-	int off;
517
-	int proto;
518
-	void *arg;
519
+stf_encapcheck(const struct mbuf *m, int off, int proto, void *arg)
520
 {
521
 	struct ip ip;
522
 	struct in6_ifaddr *ia6;
523
+	struct sockaddr_in ia6_in4addr;
524
+	struct sockaddr_in ia6_in4mask;
525
+	struct sockaddr_in *sin;
526
 	struct stf_softc *sc;
527
-	struct in_addr a, b, mask;
528
+	struct ifnet *ifp;
529
+	int ret = 0;
530
 
531
+	DEBUG_PRINTF(1, "%s: enter\n", __func__);
532
 	sc = (struct stf_softc *)arg;
533
 	if (sc == NULL)
534
 		return 0;
535
+	ifp = STF2IFP(sc);
536
 
537
-	if ((STF2IFP(sc)->if_flags & IFF_UP) == 0)
538
+	if ((ifp->if_flags & IFF_UP) == 0)
539
 		return 0;
540
 
541
 	/* IFF_LINK0 means "no decapsulation" */
542
-	if ((STF2IFP(sc)->if_flags & IFF_LINK0) != 0)
543
+	if ((ifp->if_flags & IFF_LINK0) != 0)
544
 		return 0;
545
 
546
 	if (proto != IPPROTO_IPV6)
547
@@ -344,72 +413,156 @@ stf_encapcheck(m, off, proto, arg)
548
 	if (ip.ip_v != 4)
549
 		return 0;
550
 
551
-	ia6 = stf_getsrcifa6(STF2IFP(sc));
552
+	/* Lookup an ia6 whose IPv4 addr encoded in the IPv6 addr is valid. */
553
+	ia6 = stf_getsrcifa6(ifp);
554
 	if (ia6 == NULL)
555
 		return 0;
556
+	sin = stf_getin4addr(sc, &ia6_in4addr, &ia6->ia_ifa, STF_GETIN4_USE_CACHE);
557
+	if (sin == NULL)
558
+		return (0);
559
+
560
+#if STF_DEBUG > 3
561
+	{
562
+		char buf[INET6_ADDRSTRLEN + 1];
563
+		memset(&buf, 0, sizeof(buf));
564
+
565
+		ip6_sprintf(buf, &satosin6(ia6->ia_ifa.ifa_addr)->sin6_addr);
566
+		DEBUG_PRINTF(1, "%s: ia6->ia_ifa.ifa_addr = %s\n", __func__, buf);
567
+		ip6_sprintf(buf, &ia6->ia_addr.sin6_addr);
568
+		DEBUG_PRINTF(1, "%s: ia6->ia_addr = %s\n", __func__, buf);
569
+		ip6_sprintf(buf, &satosin6(ia6->ia_ifa.ifa_netmask)->sin6_addr);
570
+		DEBUG_PRINTF(1, "%s: ia6->ia_ifa.ifa_netmask = %s\n", __func__, buf);
571
+		ip6_sprintf(buf, &ia6->ia_prefixmask.sin6_addr);
572
+		DEBUG_PRINTF(1, "%s: ia6->ia_prefixmask = %s\n", __func__, buf);
573
+
574
+		ip_sprintf(buf, &ia6_in4addr.sin_addr);
575
+		DEBUG_PRINTF(1, "%s: ia6_in4addr.sin_addr = %s\n", __func__, buf);
576
+		ip_sprintf(buf, &ip.ip_src);
577
+		DEBUG_PRINTF(1, "%s: ip.ip_src = %s\n", __func__, buf);
578
+		ip_sprintf(buf, &ip.ip_dst);
579
+		DEBUG_PRINTF(1, "%s: ip.ip_dst = %s\n", __func__, buf);
580
+	}
581
+#endif
582
 
583
 	/*
584
 	 * check if IPv4 dst matches the IPv4 address derived from the
585
 	 * local 6to4 address.
586
 	 * success on: dst = 10.1.1.1, ia6->ia_addr = 2002:0a01:0101:...
587
 	 */
588
-	if (bcmp(GET_V4(&ia6->ia_addr.sin6_addr), &ip.ip_dst,
589
-	    sizeof(ip.ip_dst)) != 0) {
590
-		ifa_free(&ia6->ia_ifa);
591
-		return 0;
592
+	DEBUG_PRINTF(1, "%s: check1: ia6_in4addr.sin_addr == ip.ip_dst?\n", __func__);
593
+	if (ia6_in4addr.sin_addr.s_addr != ip.ip_dst.s_addr) {
594
+		DEBUG_PRINTF(1, "%s: check1: false.  Ignore this packet.\n", __func__);
595
+		goto freeit;
596
 	}
597
 
598
-	/*
599
-	 * check if IPv4 src matches the IPv4 address derived from the
600
-	 * local 6to4 address masked by prefixmask.
601
-	 * success on: src = 10.1.1.1, ia6->ia_addr = 2002:0a00:.../24
602
-	 * fail on: src = 10.1.1.1, ia6->ia_addr = 2002:0b00:.../24
603
-	 */
604
-	bzero(&a, sizeof(a));
605
-	bcopy(GET_V4(&ia6->ia_addr.sin6_addr), &a, sizeof(a));
606
-	bcopy(GET_V4(&ia6->ia_prefixmask.sin6_addr), &mask, sizeof(mask));
607
-	ifa_free(&ia6->ia_ifa);
608
-	a.s_addr &= mask.s_addr;
609
-	b = ip.ip_src;
610
-	b.s_addr &= mask.s_addr;
611
-	if (a.s_addr != b.s_addr)
612
-		return 0;
613
+	DEBUG_PRINTF(1, "%s: check2: ia6->ia_addr is 2002::/16?\n", __func__);
614
+	if (IN6_IS_ADDR_6TO4(&ia6->ia_addr.sin6_addr)) {
615
+		/* 6to4 (RFC 3056) */
616
+		/*
617
+		 * check if IPv4 src matches the IPv4 address derived
618
+		 * from the local 6to4 address masked by prefixmask.
619
+		 * success on: src = 10.1.1.1, ia6->ia_addr = 2002:0a00:.../24
620
+		 * fail on: src = 10.1.1.1, ia6->ia_addr = 2002:0b00:.../24
621
+		 */
622
+		DEBUG_PRINTF(1, "%s: check2: true.\n", __func__);
623
+
624
+		memcpy(&ia6_in4mask.sin_addr,
625
+		GET_V4(&ia6->ia_prefixmask.sin6_addr),
626
+		sizeof(ia6_in4mask));
627
+#if STF_DEBUG > 3
628
+		{
629
+			char buf[INET6_ADDRSTRLEN + 1];
630
+			memset(&buf, 0, sizeof(buf));
631
+
632
+			ip_sprintf(buf, &ia6_in4addr.sin_addr);
633
+			DEBUG_PRINTF(1, "%s: ia6->ia_addr = %s\n",
634
+				__func__, buf);
635
+			ip_sprintf(buf, &ip.ip_src);
636
+			DEBUG_PRINTF(1, "%s: ip.ip_src = %s\n",
637
+				__func__, buf);
638
+			ip_sprintf(buf, &ia6_in4mask.sin_addr);
639
+			DEBUG_PRINTF(1, "%s: ia6->ia_prefixmask = %s\n",
640
+				__func__, buf);
641
+
642
+			DEBUG_PRINTF(1, "%s: check3: ia6_in4addr.sin_addr & mask == ip.ip_src & mask\n",
643
+				__func__);
644
+		}
645
+#endif
646
+		  
647
+		if ((ia6_in4addr.sin_addr.s_addr & ia6_in4mask.sin_addr.s_addr) !=
648
+		    (ip.ip_src.s_addr & ia6_in4mask.sin_addr.s_addr)) {
649
+			DEBUG_PRINTF(1, "%s: check3: false.  Ignore this packet.\n",
650
+				__func__);
651
+			goto freeit;
652
+		}
653
+	} else {
654
+		/* 6rd (RFC 5569) */
655
+		DEBUG_PRINTF(1, "%s: check2: false.  6rd.\n", __func__);
656
+		/*
657
+		 * No restriction on the src address in the case of
658
+		 * 6rd because the stf(4) interface always has a
659
+		 * prefix which covers whole of IPv4 src address
660
+		 * range.  So, stf_output() will catch all of
661
+		 * 6rd-capsuled IPv4 traffic with suspicious inner dst
662
+		 * IPv4 address (i.e. the IPv6 destination address is
663
+		 * one the admin does not like to route to outside),
664
+		 * and then it discard them silently.
665
+		 */
666
+	}
667
+	DEBUG_PRINTF(1, "%s: all clear!\n", __func__);
668
 
669
 	/* stf interface makes single side match only */
670
-	return 32;
671
+	ret = 32;
672
+freeit:
673
+	ifa_free(&ia6->ia_ifa);
674
+
675
+	return (ret);
676
 }
677
 
678
 static struct in6_ifaddr *
679
-stf_getsrcifa6(ifp)
680
-	struct ifnet *ifp;
681
+stf_getsrcifa6(struct ifnet *ifp)
682
 {
683
-	struct ifaddr *ia;
684
+	struct ifaddr *ifa;
685
 	struct in_ifaddr *ia4;
686
-	struct sockaddr_in6 *sin6;
687
-	struct in_addr in;
688
+	struct sockaddr_in *sin;
689
+	struct sockaddr_in in4;
690
 
691
 	if_addr_rlock(ifp);
692
-	TAILQ_FOREACH(ia, &ifp->if_addrhead, ifa_link) {
693
-		if (ia->ifa_addr->sa_family != AF_INET6)
694
+	TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
695
+		if (ifa->ifa_addr->sa_family != AF_INET6)
696
 			continue;
697
-		sin6 = (struct sockaddr_in6 *)ia->ifa_addr;
698
-		if (!IN6_IS_ADDR_6TO4(&sin6->sin6_addr))
699
+
700
+		if ((sin = stf_getin4addr(ifp->if_softc, &in4, ifa,
701
+		    STF_GETIN4_USE_CACHE)) == NULL)
702
 			continue;
703
 
704
-		bcopy(GET_V4(&sin6->sin6_addr), &in, sizeof(in));
705
-		LIST_FOREACH(ia4, INADDR_HASH(in.s_addr), ia_hash)
706
-			if (ia4->ia_addr.sin_addr.s_addr == in.s_addr)
707
+		LIST_FOREACH(ia4, INADDR_HASH(sin->sin_addr.s_addr), ia_hash)
708
+			if (ia4->ia_addr.sin_addr.s_addr == sin->sin_addr.s_addr)
709
 				break;
710
 		if (ia4 == NULL)
711
 			continue;
712
 
713
-		ifa_ref(ia);
714
+#if STF_DEBUG > 3
715
+	{
716
+		char buf[INET6_ADDRSTRLEN + 1];
717
+		memset(&buf, 0, sizeof(buf));
718
+
719
+		ip6_sprintf(buf, &((struct sockaddr_in6 *)ifa->ifa_addr)->sin6_addr);
720
+		DEBUG_PRINTF(1, "%s: ifa->ifa_addr->sin6_addr = %s\n",
721
+			__func__, buf);
722
+		ip_sprintf(buf, &ia4->ia_addr.sin_addr);
723
+		DEBUG_PRINTF(1, "%s: ia4->ia_addr.sin_addr = %s\n",
724
+			__func__, buf);
725
+	}
726
+#endif
727
+
728
+		ifa_ref(ifa);
729
 		if_addr_runlock(ifp);
730
-		return (struct in6_ifaddr *)ia;
731
+		return (ifatoia6(ifa));
732
 	}
733
 	if_addr_runlock(ifp);
734
 
735
-	return NULL;
736
+	return (NULL);
737
 }
738
 
739
 static int
740
@@ -419,8 +572,8 @@ stf_output(struct ifnet *ifp, struct mbuf *m, const struct sockaddr *dst,
741
 	struct stf_softc *sc;
742
 	const struct sockaddr_in6 *dst6;
743
 	struct route *cached_route;
744
-	struct in_addr in4;
745
-	const void *ptr;
746
+	struct sockaddr_in *sin;
747
+	struct sockaddr_in in4;
748
 	struct sockaddr_in *dst4;
749
 	u_int8_t tos;
750
 	struct ip *ip;
751
@@ -472,20 +625,32 @@ stf_output(struct ifnet *ifp, struct mbuf *m, const struct sockaddr *dst,
752
 	/*
753
 	 * Pickup the right outer dst addr from the list of candidates.
754
 	 * ip6_dst has priority as it may be able to give us shorter IPv4 hops.
755
+	 *   ip6_dst: destination addr in the packet header.
756
+	 *   dst6: destination addr specified in function argument.
757
 	 */
758
-	ptr = NULL;
759
-	if (IN6_IS_ADDR_6TO4(&ip6->ip6_dst))
760
-		ptr = GET_V4(&ip6->ip6_dst);
761
-	else if (IN6_IS_ADDR_6TO4(&dst6->sin6_addr))
762
-		ptr = GET_V4(&dst6->sin6_addr);
763
+	DEBUG_PRINTF(1, "%s: dst addr selection\n", __func__);
764
+	if (sc->dstv4_addr != INADDR_ANY)
765
+		in4.sin_addr.s_addr = sc->dstv4_addr;
766
 	else {
767
-		ifa_free(&ia6->ia_ifa);
768
-		m_freem(m);
769
-		ifp->if_oerrors++;
770
-		return ENETUNREACH;
771
+		sin = stf_getin4addr_in6(sc, &in4, &ia6->ia_ifa, &ip6->ip6_dst);
772
+		if (sin == NULL)
773
+			sin = stf_getin4addr_in6(sc, &in4, &ia6->ia_ifa, &dst6->sin6_addr);
774
+		if (sin == NULL) {
775
+			ifa_free(&ia6->ia_ifa);
776
+			m_freem(m);
777
+			ifp->if_oerrors++;
778
+			return ENETUNREACH;
779
+		}
780
 	}
781
-	bcopy(ptr, &in4, sizeof(in4));
782
+#if STF_DEBUG > 3
783
+	{
784
+		char buf[INET6_ADDRSTRLEN + 1];
785
+		memset(&buf, 0, sizeof(buf));
786
 
787
+		ip_sprintf(buf, &in4.sin_addr);
788
+		DEBUG_PRINTF(1, "%s: ip_dst = %s\n", __func__, buf);
789
+	}
790
+#endif
791
 	if (bpf_peers_present(ifp->if_bpf)) {
792
 		/*
793
 		 * We need to prepend the address family as
794
@@ -509,11 +674,26 @@ stf_output(struct ifnet *ifp, struct mbuf *m, const struct sockaddr *dst,
795
 	ip = mtod(m, struct ip *);
796
 
797
 	bzero(ip, sizeof(*ip));
798
+	bcopy(&in4.sin_addr, &ip->ip_dst, sizeof(ip->ip_dst));
799
 
800
-	bcopy(GET_V4(&((struct sockaddr_in6 *)&ia6->ia_addr)->sin6_addr),
801
-	    &ip->ip_src, sizeof(ip->ip_src));
802
+	sin = stf_getin4addr_sin6(sc, &in4, &ia6->ia_ifa, &ia6->ia_addr);
803
+	if (sin == NULL) {
804
+		ifa_free(&ia6->ia_ifa);
805
+		m_freem(m);
806
+		ifp->if_oerrors++;
807
+		return ENETUNREACH;
808
+	}
809
+	bcopy(&in4.sin_addr, &ip->ip_src, sizeof(ip->ip_src));
810
+#if STF_DEBUG > 3
811
+	{
812
+		char buf[INET6_ADDRSTRLEN + 1];
813
+		memset(&buf, 0, sizeof(buf));
814
+
815
+		ip_sprintf(buf, &ip->ip_src);
816
+		DEBUG_PRINTF(1, "%s: ip_src = %s\n", __func__, buf);
817
+	}
818
+#endif
819
 	ifa_free(&ia6->ia_ifa);
820
-	bcopy(&in4, &ip->ip_dst, sizeof(ip->ip_dst));
821
 	ip->ip_p = IPPROTO_IPV6;
822
 	ip->ip_ttl = ip_stf_ttl;
823
 	ip->ip_len = htons(m->m_pkthdr.len);
824
@@ -522,7 +702,7 @@ stf_output(struct ifnet *ifp, struct mbuf *m, const struct sockaddr *dst,
825
 	else
826
 		ip_ecn_ingress(ECN_NOCARE, &ip->ip_tos, &tos);
827
 
828
-	if (!stf_route_cache) {
829
+	if (!V_stf_route_cache) {
830
 		cached_route = NULL;
831
 		goto sendit;
832
 	}
833
@@ -530,7 +710,7 @@ stf_output(struct ifnet *ifp, struct mbuf *m, const struct sockaddr *dst,
834
 	/*
835
 	 * Do we have a cached route?
836
 	 */
837
-	mtx_lock(&(sc)->sc_ro_mtx);
838
+	STF_LOCK(sc);
839
 	dst4 = (struct sockaddr_in *)&sc->sc_ro.ro_dst;
840
 	if (dst4->sin_family != AF_INET ||
841
 	    bcmp(&dst4->sin_addr, &ip->ip_dst, sizeof(ip->ip_dst)) != 0) {
842
@@ -548,8 +728,15 @@ stf_output(struct ifnet *ifp, struct mbuf *m, const struct sockaddr *dst,
843
 		rtalloc_fib(&sc->sc_ro, sc->sc_fibnum);
844
 		if (sc->sc_ro.ro_rt == NULL) {
845
 			m_freem(m);
846
-			mtx_unlock(&(sc)->sc_ro_mtx);
847
 			ifp->if_oerrors++;
848
+			STF_UNLOCK(sc);
849
+			return ENETUNREACH;
850
+		}
851
+		if (sc->sc_ro.ro_rt->rt_ifp == ifp) {
852
+			/* infinite loop detection */
853
+			m_free(m);
854
+			ifp->if_oerrors++;
855
+			STF_UNLOCK(sc);
856
 			return ENETUNREACH;
857
 		}
858
 	}
859
@@ -558,35 +745,33 @@ stf_output(struct ifnet *ifp, struct mbuf *m, const struct sockaddr *dst,
860
 sendit:
861
 	M_SETFIB(m, sc->sc_fibnum);
862
 	ifp->if_opackets++;
863
+	DEBUG_PRINTF(1, "%s: ip_output dispatch.\n", __func__);
864
 	error = ip_output(m, NULL, cached_route, 0, NULL, NULL);
865
 
866
 	if (cached_route != NULL)
867
-		mtx_unlock(&(sc)->sc_ro_mtx);
868
-	return error;
869
+		STF_UNLOCK(sc);
870
+
871
+	return (error);
872
 }
873
 
874
 static int
875
-isrfc1918addr(in)
876
-	struct in_addr *in;
877
+isrfc1918addr(struct in_addr *in)
878
 {
879
 	/*
880
 	 * returns 1 if private address range:
881
 	 * 10.0.0.0/8 172.16.0.0/12 192.168.0.0/16
882
 	 */
883
 	if (stf_permit_rfc1918 == 0 && (
884
-	    (ntohl(in->s_addr) & 0xff000000) >> 24 == 10 ||
885
-	    (ntohl(in->s_addr) & 0xfff00000) >> 16 == 172 * 256 + 16 ||
886
-	    (ntohl(in->s_addr) & 0xffff0000) >> 16 == 192 * 256 + 168))
887
+	    (ntohl(in->s_addr) & 0xff000000) == 10 << 24 ||
888
+	    (ntohl(in->s_addr) & 0xfff00000) == (172 * 256 + 16) << 16 ||
889
+	    (ntohl(in->s_addr) & 0xffff0000) == (192 * 256 + 168) << 16 ))
890
 		return 1;
891
 
892
 	return 0;
893
 }
894
 
895
 static int
896
-stf_checkaddr4(sc, in, inifp)
897
-	struct stf_softc *sc;
898
-	struct in_addr *in;
899
-	struct ifnet *inifp;	/* incoming interface */
900
+stf_checkaddr4(struct stf_softc *sc, struct in_addr *in, struct ifnet *inifp)
901
 {
902
 	struct in_ifaddr *ia4;
903
 
904
@@ -602,13 +787,6 @@ stf_checkaddr4(sc, in, inifp)
905
 	}
906
 
907
 	/*
908
-	 * reject packets with private address range.
909
-	 * (requirement from RFC3056 section 2 1st paragraph)
910
-	 */
911
-	if (isrfc1918addr(in))
912
-		return -1;
913
-
914
-	/*
915
 	 * reject packets with broadcast
916
 	 */
917
 	IN_IFADDR_RLOCK();
918
@@ -631,7 +809,7 @@ stf_checkaddr4(sc, in, inifp)
919
 
920
 		bzero(&sin, sizeof(sin));
921
 		sin.sin_family = AF_INET;
922
-		sin.sin_len = sizeof(struct sockaddr_in);
923
+		sin.sin_len = sizeof(sin);
924
 		sin.sin_addr = *in;
925
 		rt = rtalloc1_fib((struct sockaddr *)&sin, 0,
926
 		    0UL, sc->sc_fibnum);
927
@@ -652,10 +830,7 @@ stf_checkaddr4(sc, in, inifp)
928
 }
929
 
930
 static int
931
-stf_checkaddr6(sc, in6, inifp)
932
-	struct stf_softc *sc;
933
-	struct in6_addr *in6;
934
-	struct ifnet *inifp;	/* incoming interface */
935
+stf_checkaddr6(struct stf_softc *sc, struct in6_addr *in6, struct ifnet *inifp)
936
 {
937
 	/*
938
 	 * check 6to4 addresses
939
@@ -679,9 +854,7 @@ stf_checkaddr6(sc, in6, inifp)
940
 }
941
 
942
 void
943
-in_stf_input(m, off)
944
-	struct mbuf *m;
945
-	int off;
946
+in_stf_input(struct mbuf *m, int off)
947
 {
948
 	int proto;
949
 	struct stf_softc *sc;
950
@@ -689,6 +862,7 @@ in_stf_input(m, off)
951
 	struct ip6_hdr *ip6;
952
 	u_int8_t otos, itos;
953
 	struct ifnet *ifp;
954
+	struct route_in6 rin6;
955
 
956
 	proto = mtod(m, struct ip *)->ip_p;
957
 
958
@@ -712,6 +886,17 @@ in_stf_input(m, off)
959
 	mac_ifnet_create_mbuf(ifp, m);
960
 #endif
961
 
962
+#if STF_DEBUG > 3
963
+	{
964
+		char buf[INET6_ADDRSTRLEN + 1];
965
+		memset(&buf, 0, sizeof(buf));
966
+
967
+		ip_sprintf(buf, &ip->ip_dst);
968
+		DEBUG_PRINTF(1, "%s: ip->ip_dst = %s\n", __func__, buf);
969
+		ip_sprintf(buf, &ip->ip_src);
970
+		DEBUG_PRINTF(1, "%s: ip->ip_src = %s\n", __func__, buf);
971
+	}
972
+#endif
973
 	/*
974
 	 * perform sanity check against outer src/dst.
975
 	 * for source, perform ingress filter as well.
976
@@ -732,6 +917,17 @@ in_stf_input(m, off)
977
 	}
978
 	ip6 = mtod(m, struct ip6_hdr *);
979
 
980
+#if STF_DEBUG > 3
981
+	{
982
+		char buf[INET6_ADDRSTRLEN + 1];
983
+		memset(&buf, 0, sizeof(buf));
984
+
985
+		ip6_sprintf(buf, &ip6->ip6_dst);
986
+		DEBUG_PRINTF(1, "%s: ip6->ip6_dst = %s\n", __func__, buf);
987
+		ip6_sprintf(buf, &ip6->ip6_src);
988
+		DEBUG_PRINTF(1, "%s: ip6->ip6_src = %s\n", __func__, buf);
989
+	}
990
+#endif
991
 	/*
992
 	 * perform sanity check against inner src/dst.
993
 	 * for source, perform ingress filter as well.
994
@@ -742,6 +938,41 @@ in_stf_input(m, off)
995
 		return;
996
 	}
997
 
998
+	/*
999
+	 * reject packets with private address range.
1000
+	 * (requirement from RFC3056 section 2 1st paragraph)
1001
+	 */
1002
+	if ((IN6_IS_ADDR_6TO4(&ip6->ip6_src) && isrfc1918addr(&ip->ip_src)) ||
1003
+	    (IN6_IS_ADDR_6TO4(&ip6->ip6_dst) && isrfc1918addr(&ip->ip_dst))) {
1004
+		m_freem(m);
1005
+		return;
1006
+	}
1007
+
1008
+	/*
1009
+	 * Ignore if the destination is the same stf interface because
1010
+	 * all of valid IPv6 outgoing traffic should go interfaces
1011
+	 * except for it.
1012
+	 */
1013
+	memset(&rin6, 0, sizeof(rin6));
1014
+	rin6.ro_dst.sin6_len = sizeof(rin6.ro_dst);
1015
+	rin6.ro_dst.sin6_family = AF_INET6;
1016
+	memcpy(&rin6.ro_dst.sin6_addr, &ip6->ip6_dst,
1017
+	       sizeof(rin6.ro_dst.sin6_addr));
1018
+	rtalloc((struct route *)&rin6);
1019
+	if (rin6.ro_rt == NULL) {
1020
+		DEBUG_PRINTF(1, "%s: no IPv6 dst.  Ignored.\n", __func__);
1021
+		m_free(m);
1022
+		return;
1023
+	}
1024
+	if ((rin6.ro_rt->rt_ifp == ifp) &&
1025
+	    (!IN6_ARE_ADDR_EQUAL(&ip6->ip6_src, &rin6.ro_dst.sin6_addr))) {
1026
+		DEBUG_PRINTF(1, "%s: IPv6 dst is the same stf.  Ignored.\n", __func__);
1027
+		RTFREE(rin6.ro_rt);
1028
+		m_free(m);
1029
+		return;
1030
+	}
1031
+	RTFREE(rin6.ro_rt);
1032
+
1033
 	itos = (ntohl(ip6->ip6_flow) >> 20) & 0xff;
1034
 	if ((ifp->if_flags & IFF_LINK1) != 0)
1035
 		ip_ecn_egress(ECN_ALLOWED, &otos, &itos);
1036
@@ -751,7 +982,7 @@ in_stf_input(m, off)
1037
 	ip6->ip6_flow |= htonl((u_int32_t)itos << 20);
1038
 
1039
 	m->m_pkthdr.rcvif = ifp;
1040
-	
1041
+
1042
 	if (bpf_peers_present(ifp->if_bpf)) {
1043
 		/*
1044
 		 * We need to prepend the address family as
1045
@@ -764,6 +995,7 @@ in_stf_input(m, off)
1046
 		bpf_mtap2(ifp->if_bpf, &af, sizeof(af), m);
1047
 	}
1048
 
1049
+	DEBUG_PRINTF(1, "%s: netisr_dispatch(NETISR_IPV6)\n", __func__);
1050
 	/*
1051
 	 * Put the packet to the network layer input queue according to the
1052
 	 * specified address family.
1053
@@ -778,46 +1010,356 @@ in_stf_input(m, off)
1054
 
1055
 /* ARGSUSED */
1056
 static void
1057
-stf_rtrequest(cmd, rt, info)
1058
-	int cmd;
1059
-	struct rtentry *rt;
1060
-	struct rt_addrinfo *info;
1061
+stf_rtrequest(int cmd, struct rtentry *rt, struct rt_addrinfo *info)
1062
 {
1063
+
1064
 	RT_LOCK_ASSERT(rt);
1065
 	rt->rt_mtu = rt->rt_ifp->if_mtu;
1066
 }
1067
 
1068
+static struct sockaddr_in *
1069
+stf_getin4addr_in6(struct stf_softc *sc, struct sockaddr_in *sin,
1070
+      struct ifaddr *ifa,
1071
+      const struct in6_addr *in6)
1072
+{
1073
+      struct sockaddr_in6 sin6;
1074
+
1075
+      DEBUG_PRINTF(1, "%s: enter.\n", __func__);
1076
+      if (ifa == NULL || in6 == NULL)
1077
+              return NULL;
1078
+
1079
+      memset(&sin6, 0, sizeof(sin6));
1080
+      memcpy(&sin6.sin6_addr, in6, sizeof(sin6.sin6_addr));
1081
+      sin6.sin6_len = sizeof(sin6);
1082
+      sin6.sin6_family = AF_INET6;
1083
+
1084
+      return(stf_getin4addr_sin6(sc, sin, ifa, &sin6));
1085
+}
1086
+
1087
+static struct sockaddr_in *
1088
+stf_getin4addr_sin6(struct stf_softc *sc, struct sockaddr_in *sin,
1089
+      struct ifaddr *ifa,
1090
+      struct sockaddr_in6 *sin6)
1091
+{
1092
+      struct in6_ifaddr ia6;
1093
+      int i;
1094
+
1095
+      DEBUG_PRINTF(1, "%s: enter.\n", __func__);
1096
+      if (ifa == NULL || sin6 == NULL)
1097
+      return NULL;
1098
+
1099
+      memset(&ia6, 0, sizeof(ia6));
1100
+      memcpy(&ia6, ifatoia6(ifa), sizeof(ia6));
1101
+
1102
+      /*
1103
+      * Use prefixmask information from ifa, and
1104
+      * address information from sin6.
1105
+      */
1106
+      ia6.ia_addr.sin6_family = AF_INET6;
1107
+      ia6.ia_ifa.ifa_addr = (struct sockaddr *)&ia6.ia_addr;
1108
+      ia6.ia_ifa.ifa_dstaddr = NULL;
1109
+      ia6.ia_ifa.ifa_netmask = (struct sockaddr *)&ia6.ia_prefixmask;
1110
+
1111
+#if STF_DEBUG > 3
1112
+      {
1113
+              char buf[INET6_ADDRSTRLEN 1];
1114
+              memset(&buf, 0, sizeof(buf));
1115
+
1116
+              ip6_sprintf(buf, &sin6->sin6_addr);
1117
+              DEBUG_PRINTF(1, "%s: sin6->sin6_addr = %s\n", __func__, buf);
1118
+              ip6_sprintf(buf, &ia6.ia_addr.sin6_addr);
1119
+              DEBUG_PRINTF(1, "%s: ia6.ia_addr.sin6_addr = %s\n", __func__, buf);
1120
+              ip6_sprintf(buf, &ia6.ia_prefixmask.sin6_addr);
1121
+              DEBUG_PRINTF(1, "%s: ia6.ia_prefixmask.sin6_addr = %s\n", __func__, buf);
1122
+      }
1123
+#endif
1124
+
1125
+      /*
1126
+      * When (src addr & src mask) != (dst (sin6) addr & src mask),
1127
+      * the dst is not in the 6rd domain.  The IPv4 address must
1128
+      * not be used.
1129
+      */
1130
+      for (i = 0; i < sizeof(ia6.ia_addr.sin6_addr); i++) {
1131
+              if ((((u_char *)&ia6.ia_addr.sin6_addr)[i] &
1132
+                  ((u_char *)&ia6.ia_prefixmask.sin6_addr)[i])
1133
+                  !=
1134
+                  (((u_char *)&sin6->sin6_addr)[i] &
1135
+                  ((u_char *)&ia6.ia_prefixmask.sin6_addr)[i]))
1136
+              return NULL;
1137
+      }
1138
+
1139
+      /* After the mask check, overwrite ia6.ia_addr with sin6. */
1140
+      memcpy(&ia6.ia_addr, sin6, sizeof(ia6.ia_addr));
1141
+      return(stf_getin4addr(sc, sin, (struct ifaddr *)&ia6, 0));
1142
+}
1143
+
1144
+static struct sockaddr_in *
1145
+stf_getin4addr(struct stf_softc *sc, struct sockaddr_in *sin,
1146
+      struct ifaddr *ifa,
1147
+      int flags)
1148
+{
1149
+      struct in_addr *in;
1150
+      struct sockaddr_in6 *sin6;
1151
+      struct in6_ifaddr *ia6;
1152
+
1153
+      DEBUG_PRINTF(1, "%s: enter.\n", __func__);
1154
+      if (ifa == NULL ||
1155
+          ifa->ifa_addr == NULL ||
1156
+          ifa->ifa_addr->sa_family != AF_INET6)
1157
+      return NULL;
1158
+
1159
+      sin6 = satosin6(ifa->ifa_addr);
1160
+      ia6 = ifatoia6(ifa);
1161
+
1162
+      if ((flags & STF_GETIN4_USE_CACHE) &&
1163
+          (ifa->ifa_dstaddr != NULL) &&
1164
+          (ifa->ifa_dstaddr->sa_family == AF_INET)) {
1165
+              /*
1166
+               * XXX: ifa_dstaddr is used as a cache of the
1167
+               * extracted IPv4 address.
1168
+               */
1169
+              memcpy(sin, satosin(ifa->ifa_dstaddr), sizeof(*sin));
1170
+
1171
+#if STF_DEBUG > 3
1172
+              {
1173
+                      char tmpbuf[INET6_ADDRSTRLEN 1];
1174
+                      memset(&tmpbuf, 0, INET6_ADDRSTRLEN);
1175
+
1176
+                      ip_sprintf(tmpbuf, &sin->sin_addr);
1177
+                      DEBUG_PRINTF(1, "%s: cached address was used = %s\n", __func__, tmpbuf);
1178
+              }
1179
+#endif
1180
+              return (sin);
1181
+      }
1182
+      memset(sin, 0, sizeof(*sin));
1183
+      in = &sin->sin_addr;
1184
+
1185
+#if STF_DEBUG > 3
1186
+      {
1187
+              char tmpbuf[INET6_ADDRSTRLEN 1];
1188
+              memset(&tmpbuf, 0, INET6_ADDRSTRLEN);
1189
+
1190
+              ip6_sprintf(tmpbuf, &sin6->sin6_addr);
1191
+              DEBUG_PRINTF(1, "%s: sin6->sin6_addr = %s\n", __func__, tmpbuf);
1192
+      }
1193
+#endif
1194
+
1195
+      if (IN6_IS_ADDR_6TO4(&sin6->sin6_addr)) {
1196
+              /* 6to4 (RFC 3056) */
1197
+              bcopy(GET_V4(&sin6->sin6_addr), in, sizeof(*in));
1198
+              if (isrfc1918addr(in))
1199
+                      return NULL;
1200
+      } else {
1201
+              /* 6rd (RFC 5569) */
1202
+              struct in6_addr buf;
1203
+              u_char *p = (u_char *)&buf;
1204
+              u_char *q = (u_char *)&in->s_addr;
1205
+              u_int residue = 0, v4residue = 0;
1206
+              u_char mask, v4mask = 0;
1207
+              int i;
1208
+              u_int plen, loop;
1209
+
1210
+              /*
1211
+               * 6rd-relays IPv6 prefix is located at a 32-bit just
1212
+               * after the prefix edge.
1213
+               */
1214
+              plen = in6_mask2len(&satosin6(ifa->ifa_netmask)->sin6_addr, NULL);
1215
+              if (64 < plen) {
1216
+                      DEBUG_PRINTF(1, "prefixlen is %d\n", plen);
1217
+                      return NULL;
1218
+              }
1219
+
1220
+              memcpy(&buf, &sin6->sin6_addr, sizeof(buf));
1221
+              if (sc->v4prefixlen == 0 || sc->v4prefixlen == 32)
1222
+                      loop = 4; /* Normal 6rd operation */
1223
+              else {
1224
+                      loop = sc->v4prefixlen / 8;
1225
+                      v4residue = sc->v4prefixlen % 8;
1226
+              }
1227
+
1228
+              p += plen / 8;
1229
+              residue = plen % 8;
1230
+              mask = ~((u_char)(-1) >> residue);
1231
+              if (v4residue) {
1232
+                      loop++;
1233
+                      v4mask = ((u_char)(-1) << v4residue);
1234
+              }
1235
+              /*
1236
+               * The p points head of the IPv4 address part in
1237
+               * bytes.  The residue is a bit-shift factor when
1238
+               * prefixlen is not a multiple of 8.
1239
+               */
1240
+              DEBUG_PRINTF(2, "residue = %d\n", residue);
1241
+              for (i = loop; i >= 0; i--) {
1242
+                      if (residue) {
1243
+                              DEBUG_PRINTF(2, "p[%d] << residue = %d-%x/%x\n",
1244
+                                      i, p[i], p[i], p[i] >> residue);
1245
+                              p[i] = (p[i] >> residue);
1246
+                              DEBUG_PRINTF(2, "p[%d] = %d/%x - p[%d 1] = %d-%x/%x\n",
1247
+                                      i, p[i], p[i], i, p[i - 1], p[i - 1], p[i - 1] << (8 - residue));
1248
+                              p[i] |= (p[i - 1] << (8 - residue));
1249
+                      }
1250
+                      q[i - 1] = p[i];
1251
+                      DEBUG_PRINTF(2, "FINAL q[%d] - p[%d] %d/%x\n",
1252
+                              i, i, q[i - 1], q[i]);
1253
+              }
1254
+              if (v4residue)
1255
+              q[i + 1] &= v4mask;
1256
+
1257
+              if (sc->v4prefixlen)
1258
+              in->s_addr |= (sc->inaddr & ((uint32_t)(-1) >> sc->v4prefixlen));
1259
+      }
1260
+
1261
+#if STF_DEBUG > 3
1262
+      {
1263
+              char tmpbuf[INET6_ADDRSTRLEN 1];
1264
+              memset(&tmpbuf, 0, INET_ADDRSTRLEN);
1265
+
1266
+              ip_sprintf(tmpbuf, in);
1267
+              DEBUG_PRINTF(1, "%s: in->in_addr = %s\n", __func__, tmpbuf);
1268
+              DEBUG_PRINTF(1, "%s: leave\n", __func__);
1269
+      }
1270
+#endif
1271
+
1272
+      if (flags & STF_GETIN4_USE_CACHE) {
1273
+              DEBUG_PRINTF(1, "%s: try to access ifa->ifa_dstaddr.\n", __func__);
1274
+              ifa->ifa_dstaddr = (struct sockaddr *)&ia6->ia_dstaddr;
1275
+              DEBUG_PRINTF(1, "%s: try to memset 0 to ia_dstaddr.\n", __func__);
1276
+                      memset(&ia6->ia_dstaddr, 0, sizeof(ia6->ia_dstaddr));
1277
+              DEBUG_PRINTF(1, "%s: try to memcpy ifa->ifa_dstaddr.\n", __func__);
1278
+                      memcpy((struct sockaddr_in *)ifa->ifa_dstaddr,
1279
+                      sin, sizeof(struct sockaddr_in));
1280
+              DEBUG_PRINTF(1, "%s: try to set sa_family.\n", __func__);
1281
+              ifa->ifa_dstaddr->sa_family = AF_INET;
1282
+              DEBUG_PRINTF(1, "%s: in->in_addr is stored in ifa_dstaddr.\n",
1283
+                      __func__);
1284
+      }
1285
+
1286
+      return (sin);
1287
+}
1288
+
1289
+
1290
 static int
1291
-stf_ioctl(ifp, cmd, data)
1292
-	struct ifnet *ifp;
1293
-	u_long cmd;
1294
-	caddr_t data;
1295
+stf_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
1296
 {
1297
+	struct stf_softc *sc, *sc_cur;
1298
 	struct ifaddr *ifa;
1299
 	struct ifreq *ifr;
1300
-	struct sockaddr_in6 *sin6;
1301
-	struct in_addr addr;
1302
+	struct sockaddr_in in4;
1303
+	struct stfv4args args;
1304
+	struct in6_ifaddr *ia6;
1305
+	struct ifdrv *ifd;
1306
 	int error, mtu;
1307
 
1308
 	error = 0;
1309
+	sc_cur = ifp->if_softc;
1310
+
1311
 	switch (cmd) {
1312
+	case SIOCSDRVSPEC:
1313
+		ifd = (struct ifdrv *) data;
1314
+		error = priv_check(curthread, PRIV_NET_ADDIFADDR);
1315
+		if (error)
1316
+			break;
1317
+		if (ifd->ifd_cmd == STF_SV4NET) {
1318
+			if (ifd->ifd_len != sizeof(args)) {
1319
+				error = EINVAL;
1320
+				break;
1321
+			}
1322
+			mtx_lock(&stf_mtx);
1323
+			LIST_FOREACH(sc, &V_stf_softc_list, stf_list) {
1324
+				if (sc == sc_cur)
1325
+					continue;
1326
+				if (sc->inaddr == 0 || sc->v4prefixlen == 0)
1327
+					continue;
1328
+
1329
+				if ((ntohl(sc->inaddr) & ((uint32_t)(-1) << sc_cur->v4prefixlen)) == ntohl(sc_cur->inaddr)) {
1330
+					error = EEXIST;
1331
+					mtx_unlock(&stf_mtx);
1332
+					return (error);
1333
+				}
1334
+				if ((ntohl(sc_cur->inaddr) & ((uint32_t)(-1) << sc->v4prefixlen)) == ntohl(sc->inaddr)) {
1335
+					error = EEXIST;
1336
+					mtx_unlock(&stf_mtx);
1337
+					return (error);
1338
+				}
1339
+			}
1340
+			mtx_unlock(&stf_mtx);
1341
+			bzero(&args, sizeof args);
1342
+			error = copyin(ifd->ifd_data, &args, ifd->ifd_len); 
1343
+			if (error)
1344
+				break;
1345
+
1346
+			sc_cur->inaddr = ntohl(args.inaddr.s_addr);
1347
+			sc_cur->inaddr &= ((uint32_t)(-1) << args.prefix);
1348
+			sc_cur->inaddr = htonl(sc_cur->inaddr);
1349
+			sc_cur->v4prefixlen = args.prefix;
1350
+			if (sc_cur->v4prefixlen == 32)
1351
+				sc_cur->v4prefixlen = 0;
1352
+		} else if (ifd->ifd_cmd == STF_SDSTV4) {
1353
+			if (ifd->ifd_len != sizeof(args)) {
1354
+				error = EINVAL;
1355
+				break;
1356
+			}
1357
+			bzero(&args, sizeof args);
1358
+			error = copyin(ifd->ifd_data, &args, ifd->ifd_len); 
1359
+			if (error)
1360
+				break;
1361
+			sc_cur->dstv4_addr = args.dstv4_addr.s_addr;
1362
+		} else
1363
+			error = EINVAL;
1364
+		break;
1365
+	case SIOCGDRVSPEC:
1366
+		ifd = (struct ifdrv *) data;
1367
+		if (ifd->ifd_len != sizeof(args)) {
1368
+			error = EINVAL;
1369
+			break;
1370
+		}
1371
+		if (ifd->ifd_cmd != STF_GV4NET) {
1372
+			error = EINVAL;
1373
+			break;
1374
+		}
1375
+		bzero(&args, sizeof args);
1376
+		args.inaddr.s_addr = sc_cur->inaddr;
1377
+		args.dstv4_addr.s_addr = sc_cur->dstv4_addr;
1378
+		args.prefix = sc_cur->v4prefixlen;
1379
+		error = copyout(&args, ifd->ifd_data, ifd->ifd_len); 
1380
+
1381
+		break;
1382
 	case SIOCSIFADDR:
1383
 		ifa = (struct ifaddr *)data;
1384
 		if (ifa == NULL || ifa->ifa_addr->sa_family != AF_INET6) {
1385
 			error = EAFNOSUPPORT;
1386
 			break;
1387
 		}
1388
-		sin6 = (struct sockaddr_in6 *)ifa->ifa_addr;
1389
-		if (!IN6_IS_ADDR_6TO4(&sin6->sin6_addr)) {
1390
+		if (stf_getin4addr(sc_cur, &in4, ifa, 0) == NULL) {
1391
 			error = EINVAL;
1392
 			break;
1393
 		}
1394
-		bcopy(GET_V4(&sin6->sin6_addr), &addr, sizeof(addr));
1395
-		if (isrfc1918addr(&addr)) {
1396
-			error = EINVAL;
1397
-			break;
1398
+		/*
1399
+		 * Sanity check: if more than two interfaces have IFF_UP, do
1400
+		 * if_down() for all of them except for the specified one.
1401
+		 */
1402
+		mtx_lock(&stf_mtx);
1403
+		LIST_FOREACH(sc, &V_stf_softc_list, stf_list) {
1404
+			if (sc == sc_cur)
1405
+				continue;
1406
+			if ((ia6 = stf_getsrcifa6(sc->sc_ifp)) == NULL)
1407
+				continue;
1408
+			if (IN6_ARE_ADDR_EQUAL(&ia6->ia_addr.sin6_addr, &ifatoia6(ifa)->ia_addr.sin6_addr)) {
1409
+				error = EEXIST;
1410
+				ifa_free(&ia6->ia_ifa);
1411
+				break;
1412
+			}
1413
+			ifa_free(&ia6->ia_ifa);
1414
 		}
1415
-
1416
+		mtx_unlock(&stf_mtx);
1417
+  
1418
+		/*
1419
+		 * XXX: ifa_dstaddr is used as a cache of the
1420
+		 * extracted IPv4 address.
1421
+		 */
1422
+		if (ifa->ifa_dstaddr != NULL)
1423
+			ifa->ifa_dstaddr->sa_family = AF_UNSPEC;
1424
 		ifa->ifa_rtrequest = stf_rtrequest;
1425
 		ifp->if_flags |= IFF_UP;
1426
 		break;
1427
@@ -849,4 +1391,5 @@ stf_ioctl(ifp, cmd, data)
1428
 	}
1429
 
1430
 	return error;
1431
+
1432
 }
1433
diff --git a/sys/net/if_stf.h b/sys/net/if_stf.h
1434
index cbaf670..e6ff29e 100644
1435
--- a/sys/net/if_stf.h
1436
+++ b/sys/net/if_stf.h
1437
@@ -33,6 +33,15 @@
1438
 #ifndef _NET_IF_STF_H_
1439
 #define _NET_IF_STF_H_
1440
 
1441
+struct stfv4args {
1442
+	struct in_addr inaddr;
1443
+	struct in_addr dstv4_addr;
1444
+	int prefix;
1445
+};
1446
+
1447
+#define	STF_SV4NET	1
1448
+#define	STF_GV4NET	2
1449
+#define	STF_SDSTV4	3
1450
 void in_stf_input(struct mbuf *, int);
1451
 
1452
 #endif /* _NET_IF_STF_H_ */
(60-60/64)