Projet

Général

Profil

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

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

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