Projet

Général

Profil

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

univnautes-tools / patches / stable / 10 / stf_6rd.diff @ 4ab3b90b

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