Browse Source

Plan 9 from Bell Labs 2007-08-04

David du Colombier 16 years ago
parent
commit
1f7a598a4c
9 changed files with 258 additions and 124 deletions
  1. 15 13
      dist/replica/_plan9.db
  2. 14 13
      dist/replica/plan9.db
  3. 18 0
      dist/replica/plan9.log
  4. 1 0
      rc/bin/dircp
  5. 0 0
      sys/log/6in4
  6. 20 2
      sys/man/1/tar
  7. 49 19
      sys/man/8/6in4
  8. 2 1
      sys/man/8/mkfs
  9. 139 76
      sys/src/cmd/ip/6in4.c

+ 15 - 13
dist/replica/_plan9.db

@@ -285,10 +285,10 @@
 386/bin/import - 775 sys sys 1179777651 102499
 386/bin/iostats - 775 sys sys 1178568280 98906
 386/bin/ip - 20000000775 sys sys 1016920851 0
-386/bin/ip/6in4 - 775 sys sys 1179777651 88513
+386/bin/ip/6in4 - 775 sys sys 1186112227 89929
 386/bin/ip/dhcpclient - 775 sys sys 1178568281 92992
-386/bin/ip/dhcpd - 775 sys sys 1185420496 149586
-386/bin/ip/dhcpleases - 775 sys sys 1178568281 85069
+386/bin/ip/dhcpd - 775 sys sys 1186112227 151324
+386/bin/ip/dhcpleases - 775 sys sys 1186112228 85106
 386/bin/ip/ftpd - 775 sys sys 1181507267 170562
 386/bin/ip/gping - 775 sys sys 1179372093 182147
 386/bin/ip/hogports - 775 sys sys 1148500655 42914
@@ -303,7 +303,7 @@
 386/bin/ip/httpd/wikipost - 775 sys sys 1178568286 113828
 386/bin/ip/httpfile - 775 sys sys 1184731238 283966
 386/bin/ip/imap4d - 775 sys sys 1179372095 236829
-386/bin/ip/ipconfig - 775 sys sys 1185420496 157540
+386/bin/ip/ipconfig - 775 sys sys 1186112228 159700
 386/bin/ip/linklocal - 775 sys sys 1177530964 42790
 386/bin/ip/ping - 775 sys sys 1184901770 82522
 386/bin/ip/ppp - 775 sys sys 1179372096 219103
@@ -342,10 +342,10 @@
 386/bin/ndb - 20000000775 sys sys 985743147 0
 386/bin/ndb/cs - 775 sys sys 1184529902 149014
 386/bin/ndb/csquery - 775 sys sys 1176488418 61652
-386/bin/ndb/dns - 775 sys sys 1185334135 293921
-386/bin/ndb/dnsdebug - 775 sys sys 1185334136 269375
+386/bin/ndb/dns - 775 sys sys 1186112229 293921
+386/bin/ndb/dnsdebug - 775 sys sys 1186112230 269375
 386/bin/ndb/dnsquery - 775 sys sys 1185334136 64350
-386/bin/ndb/dnstcp - 775 sys sys 1185334137 267228
+386/bin/ndb/dnstcp - 775 sys sys 1186112231 267228
 386/bin/ndb/ipquery - 775 sys sys 1178568296 95524
 386/bin/ndb/mkdb - 775 sys sys 1168402340 64211
 386/bin/ndb/mkhash - 775 sys sys 1178568296 83312
@@ -407,7 +407,7 @@
 386/bin/snap - 775 sys sys 1178939753 316147
 386/bin/snapfs - 775 sys sys 1179372105 390366
 386/bin/sniffer - 775 sys sys 1038443185 99028
-386/bin/snoopy - 775 sys sys 1184529905 190044
+386/bin/snoopy - 775 sys sys 1186112232 190558
 386/bin/sort - 775 sys sys 1178568303 81057
 386/bin/spin - 775 sys sys 1176520498 760856
 386/bin/split - 775 sys sys 1181507270 75811
@@ -5490,7 +5490,7 @@ rc/bin/cpurc.local - 775 sys sys 1176827268 367
 rc/bin/delkey - 775 sys sys 1109429137 643
 rc/bin/dial - 20000000775 sys sys 1059180057 0
 rc/bin/diffy - 775 sys sys 1140694870 277
-rc/bin/dircp - 775 sys sys 1169490692 234
+rc/bin/dircp - 775 sys sys 1186179147 246
 rc/bin/diskparts - 775 sys sys 1178399618 624
 rc/bin/dmaon - 775 sys sys 1178214935 213
 rc/bin/doc2ps - 775 sys sys 1069793831 244
@@ -7287,6 +7287,7 @@ sys/lib/wiki/werror.html - 664 sys sys 1018580959 1234
 sys/lib/yaccpar - 664 sys sys 1123699615 4588
 sys/lib/yaccpars - 664 sys sys 1123699616 4596
 sys/log - 20000000775 sys sys 1104988294 0
+sys/log/6in4 - 10000000666 sys sys 1186166728 0
 sys/log/aan - 10000000666 sys sys 1019856844 0
 sys/log/auth - 10000000666 sys sys 959261821 0
 sys/log/cs - 10000000666 sys sys 958934039 0
@@ -7453,7 +7454,7 @@ sys/man/1/strip - 664 sys sys 1131293239 523
 sys/man/1/sum - 664 sys sys 984772442 1390
 sys/man/1/syscall - 664 sys sys 1016466457 1439
 sys/man/1/tail - 664 sys sys 1113743327 1413
-sys/man/1/tar - 664 sys sys 1169092135 3577
+sys/man/1/tar - 664 sys sys 1186179393 3836
 sys/man/1/tbl - 664 sys sys 944959674 4308
 sys/man/1/tcs - 664 sys sys 1143131200 2654
 sys/man/1/tee - 664 sys sys 969499886 351
@@ -7770,7 +7771,7 @@ sys/man/7/playlistfs - 664 sys sys 1103794042 3831
 sys/man/7/scat - 664 sys sys 970069855 8904
 sys/man/8 - 20000000775 sys sys 1162240005 0
 sys/man/8/0intro - 664 sys sys 944959679 247
-sys/man/8/6in4 - 664 sys sys 1181070628 1934
+sys/man/8/6in4 - 664 sys sys 1186182195 2330
 sys/man/8/9load - 664 sys sys 1094676489 9170
 sys/man/8/9pcon - 664 sys sys 1145881850 2234
 sys/man/8/INDEX - 664 sys sys 1183260468 2773
@@ -7800,7 +7801,7 @@ sys/man/8/kfscmd - 664 sys sys 1182192559 4514
 sys/man/8/listen - 664 sys sys 1181790503 3658
 sys/man/8/lp - 664 sys sys 954614702 2491
 sys/man/8/mk9660 - 664 sys sys 1104430379 3946
-sys/man/8/mkfs - 664 sys sys 1163208436 3414
+sys/man/8/mkfs - 664 sys sys 1186179498 3427
 sys/man/8/mkpaqfs - 664 sys sys 1181261121 890
 sys/man/8/mksacfs - 664 sys sys 964886843 710
 sys/man/8/mouse - 664 sys sys 971455511 2476
@@ -12464,7 +12465,7 @@ sys/src/cmd/iostats/mkfile - 664 sys sys 1014925727 122
 sys/src/cmd/iostats/statfs.h - 664 sys sys 1140099908 2626
 sys/src/cmd/iostats/statsrv.c - 664 sys sys 1166824341 10773
 sys/src/cmd/ip - 20000000775 sys sys 1128605981 0
-sys/src/cmd/ip/6in4.c - 664 sys sys 1185564832 7472
+sys/src/cmd/ip/6in4.c - 664 sys sys 1186180319 8844
 sys/src/cmd/ip/arp.h - 664 sys sys 944961005 751
 sys/src/cmd/ip/dhcp.h - 664 sys sys 1184696111 3764
 sys/src/cmd/ip/dhcpclient.c - 664 sys sys 1178482836 11969
@@ -15843,3 +15844,4 @@ usr/glenda/lib/profile - 664 glenda glenda 1105128663 890
 usr/glenda/readme.acme - 664 glenda glenda 1019860628 4753
 usr/glenda/readme.rio - 664 glenda glenda 1019860628 6370
 usr/glenda/tmp - 20000000775 glenda glenda 1018802620 0
+386/bin/ip/6in4 - 775 sys sys 1186198157 90962

+ 14 - 13
dist/replica/plan9.db

@@ -285,10 +285,10 @@
 386/bin/import - 775 sys sys 1179777651 102499
 386/bin/iostats - 775 sys sys 1178568280 98906
 386/bin/ip - 20000000775 sys sys 1016920851 0
-386/bin/ip/6in4 - 775 sys sys 1179777651 88513
+386/bin/ip/6in4 - 775 sys sys 1186198157 90962
 386/bin/ip/dhcpclient - 775 sys sys 1178568281 92992
-386/bin/ip/dhcpd - 775 sys sys 1185420496 149586
-386/bin/ip/dhcpleases - 775 sys sys 1178568281 85069
+386/bin/ip/dhcpd - 775 sys sys 1186112227 151324
+386/bin/ip/dhcpleases - 775 sys sys 1186112228 85106
 386/bin/ip/ftpd - 775 sys sys 1181507267 170562
 386/bin/ip/gping - 775 sys sys 1179372093 182147
 386/bin/ip/hogports - 775 sys sys 1148500655 42914
@@ -303,7 +303,7 @@
 386/bin/ip/httpd/wikipost - 775 sys sys 1178568286 113828
 386/bin/ip/httpfile - 775 sys sys 1184731238 283966
 386/bin/ip/imap4d - 775 sys sys 1179372095 236829
-386/bin/ip/ipconfig - 775 sys sys 1185420496 157540
+386/bin/ip/ipconfig - 775 sys sys 1186112228 159700
 386/bin/ip/linklocal - 775 sys sys 1177530964 42790
 386/bin/ip/ping - 775 sys sys 1184901770 82522
 386/bin/ip/ppp - 775 sys sys 1179372096 219103
@@ -342,10 +342,10 @@
 386/bin/ndb - 20000000775 sys sys 985743147 0
 386/bin/ndb/cs - 775 sys sys 1184529902 149014
 386/bin/ndb/csquery - 775 sys sys 1176488418 61652
-386/bin/ndb/dns - 775 sys sys 1185334135 293921
-386/bin/ndb/dnsdebug - 775 sys sys 1185334136 269375
+386/bin/ndb/dns - 775 sys sys 1186112229 293921
+386/bin/ndb/dnsdebug - 775 sys sys 1186112230 269375
 386/bin/ndb/dnsquery - 775 sys sys 1185334136 64350
-386/bin/ndb/dnstcp - 775 sys sys 1185334137 267228
+386/bin/ndb/dnstcp - 775 sys sys 1186112231 267228
 386/bin/ndb/ipquery - 775 sys sys 1178568296 95524
 386/bin/ndb/mkdb - 775 sys sys 1168402340 64211
 386/bin/ndb/mkhash - 775 sys sys 1178568296 83312
@@ -407,7 +407,7 @@
 386/bin/snap - 775 sys sys 1178939753 316147
 386/bin/snapfs - 775 sys sys 1179372105 390366
 386/bin/sniffer - 775 sys sys 1038443185 99028
-386/bin/snoopy - 775 sys sys 1184529905 190044
+386/bin/snoopy - 775 sys sys 1186112232 190558
 386/bin/sort - 775 sys sys 1178568303 81057
 386/bin/spin - 775 sys sys 1176520498 760856
 386/bin/split - 775 sys sys 1181507270 75811
@@ -5490,7 +5490,7 @@ rc/bin/cpurc.local - 775 sys sys 1176827268 367
 rc/bin/delkey - 775 sys sys 1109429137 643
 rc/bin/dial - 20000000775 sys sys 1059180057 0
 rc/bin/diffy - 775 sys sys 1140694870 277
-rc/bin/dircp - 775 sys sys 1169490692 234
+rc/bin/dircp - 775 sys sys 1186179147 246
 rc/bin/diskparts - 775 sys sys 1178399618 624
 rc/bin/dmaon - 775 sys sys 1178214935 213
 rc/bin/doc2ps - 775 sys sys 1069793831 244
@@ -7287,6 +7287,7 @@ sys/lib/wiki/werror.html - 664 sys sys 1018580959 1234
 sys/lib/yaccpar - 664 sys sys 1123699615 4588
 sys/lib/yaccpars - 664 sys sys 1123699616 4596
 sys/log - 20000000775 sys sys 1104988294 0
+sys/log/6in4 - 10000000666 sys sys 1186166728 0
 sys/log/aan - 10000000666 sys sys 1019856844 0
 sys/log/auth - 10000000666 sys sys 959261821 0
 sys/log/cs - 10000000666 sys sys 958934039 0
@@ -7453,7 +7454,7 @@ sys/man/1/strip - 664 sys sys 1131293239 523
 sys/man/1/sum - 664 sys sys 984772442 1390
 sys/man/1/syscall - 664 sys sys 1016466457 1439
 sys/man/1/tail - 664 sys sys 1113743327 1413
-sys/man/1/tar - 664 sys sys 1169092135 3577
+sys/man/1/tar - 664 sys sys 1186179393 3836
 sys/man/1/tbl - 664 sys sys 944959674 4308
 sys/man/1/tcs - 664 sys sys 1143131200 2654
 sys/man/1/tee - 664 sys sys 969499886 351
@@ -7770,7 +7771,7 @@ sys/man/7/playlistfs - 664 sys sys 1103794042 3831
 sys/man/7/scat - 664 sys sys 970069855 8904
 sys/man/8 - 20000000775 sys sys 1162240005 0
 sys/man/8/0intro - 664 sys sys 944959679 247
-sys/man/8/6in4 - 664 sys sys 1181070628 1934
+sys/man/8/6in4 - 664 sys sys 1186182195 2330
 sys/man/8/9load - 664 sys sys 1094676489 9170
 sys/man/8/9pcon - 664 sys sys 1145881850 2234
 sys/man/8/INDEX - 664 sys sys 1183260468 2773
@@ -7800,7 +7801,7 @@ sys/man/8/kfscmd - 664 sys sys 1182192559 4514
 sys/man/8/listen - 664 sys sys 1181790503 3658
 sys/man/8/lp - 664 sys sys 954614702 2491
 sys/man/8/mk9660 - 664 sys sys 1104430379 3946
-sys/man/8/mkfs - 664 sys sys 1163208436 3414
+sys/man/8/mkfs - 664 sys sys 1186179498 3427
 sys/man/8/mkpaqfs - 664 sys sys 1181261121 890
 sys/man/8/mksacfs - 664 sys sys 964886843 710
 sys/man/8/mouse - 664 sys sys 971455511 2476
@@ -12464,7 +12465,7 @@ sys/src/cmd/iostats/mkfile - 664 sys sys 1014925727 122
 sys/src/cmd/iostats/statfs.h - 664 sys sys 1140099908 2626
 sys/src/cmd/iostats/statsrv.c - 664 sys sys 1166824341 10773
 sys/src/cmd/ip - 20000000775 sys sys 1128605981 0
-sys/src/cmd/ip/6in4.c - 664 sys sys 1185564832 7472
+sys/src/cmd/ip/6in4.c - 664 sys sys 1186180319 8844
 sys/src/cmd/ip/arp.h - 664 sys sys 944961005 751
 sys/src/cmd/ip/dhcp.h - 664 sys sys 1184696111 3764
 sys/src/cmd/ip/dhcpclient.c - 664 sys sys 1178482836 11969

+ 18 - 0
dist/replica/plan9.log

@@ -49829,3 +49829,21 @@
 1186090204 44 d sys/src/cmd/ip/snoopy/aoe.8 - 664 sys sys 1186084328 0
 1186090204 45 d sys/src/cmd/ip/snoopy/8.out - 775 sys sys 1186084334 0
 1186093804 0 m rc/bin/C - 775 sys sys 1169492077 980
+1186113607 0 c 386/bin/snoopy - 775 sys sys 1186112232 190558
+1186113607 1 c 386/bin/ip/6in4 - 775 sys sys 1186112227 89929
+1186113607 2 c 386/bin/ip/dhcpd - 775 sys sys 1186112227 151324
+1186113607 3 c 386/bin/ip/dhcpleases - 775 sys sys 1186112228 85106
+1186113607 4 c 386/bin/ip/ipconfig - 775 sys sys 1186112228 159700
+1186113607 5 c 386/bin/ndb/dns - 775 sys sys 1186112229 293921
+1186113607 6 c 386/bin/ndb/dnsdebug - 775 sys sys 1186112230 269375
+1186113607 7 c 386/bin/ndb/dnstcp - 775 sys sys 1186112231 267228
+1186119007 0 c sys/man/8/6in4 - 664 sys sys 1186118404 2057
+1186164007 0 c sys/man/8/6in4 - 664 sys sys 1186162945 2213
+1186167605 0 a sys/log/6in4 - 10000000666 sys sys 1186166728 0
+1186178403 0 c sys/man/1/tar - 664 sys sys 1186178227 3672
+1186180204 0 c rc/bin/dircp - 775 sys sys 1186179147 246
+1186180204 1 c sys/man/1/tar - 664 sys sys 1186179393 3836
+1186180204 2 c sys/man/8/mkfs - 664 sys sys 1186179498 3427
+1186182004 0 c sys/src/cmd/ip/6in4.c - 664 sys sys 1186180319 8844
+1186183805 0 c sys/man/8/6in4 - 664 sys sys 1186182195 2330
+1186198203 0 c 386/bin/ip/6in4 - 775 sys sys 1186198157 90962

+ 1 - 0
rc/bin/dircp

@@ -6,4 +6,5 @@ case 2
 	@{cd $1 && tar cf /fd/1 .} | @{cd $2 && tar xTf /fd/0}
 case *
 	echo usage: dircp from to >[1=2]
+	exit usage
 }

+ 0 - 0
sys/log/6in4


+ 20 - 2
sys/man/1/tar

@@ -1,12 +1,16 @@
 .TH TAR 1
 .SH NAME
-tar \- archiver
+tar, dircp \- archiver
 .SH SYNOPSIS
 .B tar
 .I key
 [
 .I file ...
 ]
+.PP
+.B dircp
+.I fromdir
+.I todir
 .SH DESCRIPTION
 .PP
 .I Tar
@@ -146,6 +150,8 @@ flag is unnecessary (but allowed) when using the
 and
 .B x
 verbs on archives with recognized extensions.
+.br
+.ne 6
 .SH EXAMPLES
 .I Tar
 can be used to copy hierarchies thus:
@@ -153,12 +159,18 @@ can be used to copy hierarchies thus:
 .EX
 @{cd fromdir && tar cp .} | @{cd todir && tar xT}
 .EE
+.PP
+.I Dircp
+does this.
 .SH SOURCE
 .B /sys/src/cmd/tar.c
+.br
+.B /rc/bin/dircp
 .SH SEE ALSO
 .IR ar (1),
 .IR bundle (1),
-.IR tapefs (4)
+.IR tapefs (4),
+.IR mkfs (8)
 .SH BUGS
 There is no way to ask for any but the last
 occurrence of a file.
@@ -174,3 +186,9 @@ The
 .B r
 key (append)
 cannot be used on compressed archives.
+.PP
+.IR Tar ,
+thus
+.IR dircp ,
+doesn't record Plan-9-specific metadata
+such as append-only and exclusive-open permission bits, so they aren't copied.

+ 49 - 19
sys/man/8/6in4

@@ -18,6 +18,7 @@
 .SH DESCRIPTION
 .I 6in4
 sets up and maintains a tunnel of IPv6 traffic through an IPv4 connection.
+.PP
 .I Local6
 and
 .I mask
@@ -26,23 +27,15 @@ define the IPv6 address and subnet of the near end of the tunnel
 defaults to
 .L /128
 for a single-host
-tunnel),
-.I remote4
-is the IPv4 address of the far end of the tunnel
-(must be given explicitly for a configured tunnel, or
-defaults to the anycast address 192.88.99.1 for
-.IR 6to4 ),
-and
-.I remote6
-is the IPv6 address of the far end of the tunnel
-(used as the point-to-point destination for routing, and
-defaults to a link-local address constructed from
-.IR remote4 ).
+tunnel).
 If
 .I local6
-is missing,
+is missing or
+.LR - ,
 it defaults to
-.BI 2002: aabb : ccdd ::1/48 ,
+.IP
+.BI 2002: aabb : ccdd ::1/48
+.PP
 where
 .IR aa ,
 .IR bb ,
@@ -54,24 +47,53 @@ are the hexadecimal equivalents of the bytes
 .I d
 in this host's primary IPv4 address.
 .PP
+.I Remote4
+is the IPv4 address of the far end of the tunnel
+(must be given explicitly for a configured tunnel, or
+defaults to the anycast address 192.88.99.1 for
+.IR 6to4 ).
+.PP
+.I Remote6
+is the IPv6 address of the far end of the tunnel
+(used as the point-to-point destination for routing, and
+defaults to a link-local address constructed from
+.IR remote4 ).
+.PP
 .I 6in4
 forks a pair of background processes to copy packets to and from
 the tunnel.
 .PP
-Supply
+Options are:
+.TF -x
+.PD 0
+.TP
+.B -a
+permit any remote IPv4 address as the far end of a tunnel.
+This is likely to be useful for the server side of a tunnel.
+.TP
 .B -g
-to use the tunnel as the default route for global IPv6 addresses.
+use the tunnel as the default route for global IPv6 addresses
+.TP
 .B -x
-uses the network mounted at
+use the network mounted at
 .I netmtpt
 instead of
 .LR /net .
+.PD
 .SH EXAMPLES
 If your primary IPv4 address is public,
 you can start a 6to4 tunnel simply with
 .IP
 .EX
-6in4 -g
+ip/6in4 -g
+.EE
+.PP
+Similarly, you can start a server for
+.I 6to4
+tunnels with
+.IP
+.EX
+ip/6in4 -ag
 .EE
 .PP
 If you use a tunnel broker at address
@@ -83,11 +105,19 @@ subnet with address
 you can start the tunnel with
 .IP
 .EX
-6in4 -g 2001:1122:3344:5566::/64 5.6.7.8
+ip/6in4 -g 2001:1122:3344:5566::/64 5.6.7.8
 .EE
 .SH FILES
+.TF /net/ipmux
+.PD 0
+.TP
 .B /net/ipmux
+access to IPv6-in-IPv4 packets
+.TP
+.B /net/ipifc
+packet interface to IPv6 network
 .SH SEE ALSO
+.IR bridge (3),
 .I ipmux
 in
 .IR ip (3)

+ 2 - 1
sys/man/8/mkfs

@@ -187,4 +187,5 @@ disk/mkext -u -d /n/newfs < arch
 .SH "SEE ALSO"
 .IR prep (8),
 .IR kfscmd (8),
-.IR sd (3)
+.IR sd (3),
+.IR tar (1)

+ 139 - 76
sys/src/cmd/ip/6in4.c

@@ -1,5 +1,6 @@
 /*
- * 6in4 - tunnel client for automatic 6to4 or configured v6-in-v4 tunnels
+ * 6in4 - tunnel client for automatic 6to4 or configured v6-in-v4 tunnels.
+ *	see rfc3056.
  */
 
 #include <u.h>
@@ -7,12 +8,30 @@
 #include <ip.h>
 
 enum {
-	IP_IPV6PROTO	= 41,
+	IP_IPV6PROTO	= 41,		/* IPv4 protocol number for IPv6 */
 	V6to4pfx	= 0x2002,
 };
 
+typedef struct Iphdr Iphdr;
+struct Iphdr
+{
+	uchar	vihl;		/* Version and header length */
+	uchar	tos;		/* Type of service */
+	uchar	length[2];	/* packet length */
+	uchar	id[2];		/* Identification */
+	uchar	frag[2];	/* Fragment information */
+	uchar	ttl;		/* Time to live */
+	uchar	proto;		/* Protocol */
+	uchar	cksum[2];	/* Header checksum */
+	uchar	src[4];		/* Ip source (uchar ordering unimportant) */
+	uchar	dst[4];		/* Ip destination (uchar ordering unimportant) */
+};
+
+#define STFHDR sizeof(Iphdr)
+
 int anysender;
 int gateway;
+int debug;
 
 uchar local6[IPaddrlen];
 uchar remote6[IPaddrlen];
@@ -39,45 +58,30 @@ usage(void)
 	exits("Usage");
 }
 
-void
-main(int argc, char **argv)
+static char *
+defv6addr(void)
 {
-	int n, tunnel, ifc, cfd;
-	char *p, *cl, *ir, *loc6;
-	char buf[128], path[64];
-
-	fmtinstall('I', eipfmt);
-	fmtinstall('V', eipfmt);
-	fmtinstall('M', eipfmt);
+	uchar *ipv4 = &myip[IPaddrlen - IPv4addrlen];
 
-	ARGBEGIN {
-	case 'a':
-		anysender++;
-		break;
-	case 'g':
-		gateway++;
-		break;
-	case 'x':
-		net = EARGF(usage());
-		break;
-	default:
-		usage();
-	} ARGEND
+	return smprint("%ux:%2.2x%2.2x:%2.2x%2.2x::1/48", V6to4pfx,
+		ipv4[0], ipv4[1], ipv4[2], ipv4[3]);
+}
 
-	if (myipaddr(myip, net) < 0)
-		sysfatal("can't find my ipv4 address on %s", net);
+/* process non-option arguments */
+static void
+procargs(int argc, char **argv)
+{
+	char *p, *loc6;
 
 	if (argc < 1)
-		loc6 = smprint("%ux:%2.2x%2.2x:%2.2x%2.2x::1/48",
-			V6to4pfx,
-			myip[IPaddrlen - IPv4addrlen],
-			myip[IPaddrlen - IPv4addrlen + 1],
-			myip[IPaddrlen - IPv4addrlen + 2],
-			myip[IPaddrlen - IPv4addrlen + 3]);
-	else {
-		loc6 = argv[0];
+		loc6 = defv6addr();
+	else if (strcmp(argv[0], "-") == 0) {
+		loc6 = defv6addr();
 		argv++;
 		argc--;
+	} else {
+		loc6 = *argv++;
+		argc--;
 	}
 
 	/* local v6 address (mask defaults to /128) */
@@ -91,30 +95,31 @@ main(int argc, char **argv)
 	if (isv4(local6))
 		usage();
 	if (argc >= 1 && argv[0][0] == '/') {
-		parseipmask(localmask, argv[0]);
-		argv++;
+		parseipmask(localmask, *argv++);
 		argc--;
 	}
+	if (debug)
+		fprint(2, "local6 %I %M\n", local6, localmask);
 
 	/* remote v4 address (defaults to anycast 6to4) */
 	if (argc >= 1) {
-		parseip(remote4, argv[0]);
+		parseip(remote4, *argv++);
+		argc--;
 		if (!isv4(remote4))
 			usage();
-		argv++;
-		argc--;
 	} else {
 		v4tov6(remote4, anycast6to4);
 		anysender++;
 	}
+	if (debug)
+		fprint(2, "remote4 %I\n", remote4);
 
 	/* remote v6 address (defaults to link-local w/ v4 as interface part) */
 	if (argc >= 1) {
-		parseip(remote6, argv[0]);
+		parseip(remote6, *argv++);
+		argc--;
 		if (isv4(remote4))
 			usage();
-		argv++;
-		argc--;
 	} else {
 		remote6[0] = 0xFE;		/* link local */
 		remote6[1] = 0x80;
@@ -125,87 +130,135 @@ main(int argc, char **argv)
 		usage();
 
 	maskip(local6, localmask, localnet);
+	if (debug)
+		fprint(2, "localnet %I remote6 %I\n", localnet, remote6);
+}
+
+static void
+setup(int *v6net, int *tunp)
+{
+	int n, cfd;
+	char *p, *cl, *ir;
+	char buf[128], path[64];
 
 	/*
-	 * open IPv6-in-IPv4 tunnel
+	 * gain access to IPv6-in-IPv4 packets
 	 */
-	p = seprint(buf, buf + sizeof buf, "ipmux!proto=%2.2x;dst=%V",
-		IP_IPV6PROTO, myip + IPv4off);
+	p = seprint(buf, buf + sizeof buf, "%s/ipmux!proto=%2.2x",
+		net, IP_IPV6PROTO);
+	if (1)
+		seprint(p, buf + sizeof buf, ";dst=%V", myip + IPv4off);
 	if (!anysender)
 		seprint(p, buf + sizeof buf, ";src=%V", remote4 + IPv4off);
-	tunnel = dial(buf, 0, 0, 0);
-	if (tunnel < 0)
-		sysfatal("can't make 6in4 tunnel with dial str %s: %r", buf);
+	*tunp = dial(buf, 0, 0, 0);
+	if (*tunp < 0)
+		sysfatal("can't access ipv6-in-ipv4 with dial str %s: %r", buf);
+	if (debug)
+		fprint(2, "dialed %s for v6-in-v4 access\n", buf);
 
 	/*
 	 * open local IPv6 interface (as a packet interface)
 	 */
+
 	cl = smprint("%s/ipifc/clone", net);
 	cfd = open(cl, ORDWR);			/* allocate a conversation */
-	free(cl);
 	n = 0;
 	if (cfd < 0 || (n = read(cfd, buf, sizeof buf - 1)) <= 0)
-		sysfatal("can't make packet interface: %r");
+		sysfatal("can't make packet interface %s: %r", cl);
+	if (debug)
+		fprint(2, "cloned %s as local v6 interface\n", cl);
+	free(cl);
 	buf[n] = 0;
 
 	snprint(path, sizeof path, "%s/ipifc/%s/data", net, buf);
-	ifc = open(path, ORDWR);
-	if (ifc < 0 || fprint(cfd, "bind pkt") < 0)
+	*v6net = open(path, ORDWR);
+	if (*v6net < 0 || fprint(cfd, "bind pkt") < 0)
 		sysfatal("can't bind packet interface: %r");
 	/* 1280 is MTU, apparently from rfc2460 */
 	if (fprint(cfd, "add %I /128 %I 1280", local6, remote6) <= 0)
 		sysfatal("can't set local ipv6 address: %r");
 	close(cfd);
+	if (debug)
+		fprint(2, "opened & bound %s as local v6 interface\n", path);
 
 	if (gateway) {
 		/* route global addresses through the tunnel to remote6 */
 		ir = smprint("%s/iproute", net);
 		cfd = open(ir, OWRITE);
+		if (cfd >= 0 && debug)
+			fprint(2, "injected 2000::/3 %I into %s\n", remote6, ir);
 		free(ir);
 		if (cfd < 0 || fprint(cfd, "add 2000:: /3 %I", remote6) <= 0)
 			sysfatal("can't set default global route: %r");
 	}
+}
 
+static void
+runtunnel(int v6net, int tunnel)
+{
 	/* run the tunnel copying in the background */
 	switch (rfork(RFPROC|RFNOWAIT|RFMEM|RFNOTEG)) {
+	case -1:
+		sysfatal("rfork");
 	default:
 		exits(nil);
 	case 0:
 		break;
-	case -1:
-		sysfatal("rfork");
 	}
 
 	switch (rfork(RFPROC|RFNOWAIT|RFMEM)) {
+	case -1:
+		sysfatal("rfork");
 	default:
-		tunnel2ip(tunnel, ifc);
+		tunnel2ip(tunnel, v6net);
 		break;
 	case 0:
-		ip2tunnel(ifc, tunnel);
+		ip2tunnel(v6net, tunnel);
 		break;
-	case -1:
-		sysfatal("rfork");
 	}
 	exits("tunnel gone");
 }
 
-typedef struct Iphdr Iphdr;
-struct Iphdr
+void
+main(int argc, char **argv)
 {
-	uchar	vihl;		/* Version and header length */
-	uchar	tos;		/* Type of service */
-	uchar	length[2];	/* packet length */
-	uchar	id[2];		/* Identification */
-	uchar	frag[2];	/* Fragment information */
-	uchar	ttl;		/* Time to live */
-	uchar	proto;		/* Protocol */
-	uchar	cksum[2];	/* Header checksum */
-	uchar	src[4];		/* Ip source (uchar ordering unimportant) */
-	uchar	dst[4];		/* Ip destination (uchar ordering unimportant) */
-};
+	int tunnel, v6net;
 
-#define STFHDR (sizeof(Iphdr))
+	fmtinstall('I', eipfmt);
+	fmtinstall('V', eipfmt);
+	fmtinstall('M', eipfmt);
+
+	ARGBEGIN {
+	case 'a':
+		anysender++;
+		break;
+	case 'd':
+		debug++;
+		break;
+	case 'g':
+		gateway++;
+		break;
+	case 'x':
+		net = EARGF(usage());
+		break;
+	default:
+		usage();
+	} ARGEND
+
+	if (myipaddr(myip, net) < 0)
+		sysfatal("can't find my ipv4 address on %s", net);
+	if (!isv4(myip))
+		sysfatal("my ip, %I, is not a v4 address", myip);
+
+	procargs(argc, argv);
+	setup(&v6net, &tunnel);
+	runtunnel(v6net, tunnel);
+}
 
+/*
+ * encapsulate v6 packets from the packet interface in v4 ones
+ * and send them into the tunnel.
+ */
 static void
 ip2tunnel(int in, int out)
 {
@@ -214,6 +267,7 @@ ip2tunnel(int in, int out)
 	Iphdr *op;
 	Ip6hdr *ip;
 
+	/* populate v4 header */
 	op = (Iphdr*)buf;
 	op->vihl = 0x45;		/* v4, hdr is 5 longs? */
 	memcpy(op->src, myip + IPv4off, sizeof op->src);
@@ -257,6 +311,10 @@ ip2tunnel(int in, int out)
 	}
 }
 
+/*
+ * decapsulate v6 packets from v4 ones from the tunnel
+ * and forward them to the packet interface
+ */
 static void
 tunnel2ip(int in, int out)
 {
@@ -276,7 +334,7 @@ tunnel2ip(int in, int out)
 			break;
 		}
 
-		/* if not IPv4 nor IP protocol IPv6, drop it */
+		/* if not IPv4 nor IPv4 protocol IPv6, drop it */
 		if ((ip->vihl&0xF0) != 0x40 || ip->proto != IP_IPV6PROTO)
 			continue;
 
@@ -290,8 +348,10 @@ tunnel2ip(int in, int out)
 		op = (Ip6hdr*)(buf + IPaddrlen + STFHDR);
 		n -= STFHDR;
 
-		/* don't relay: just accept packets for local host/subnet */
-		/* (this blocks link-local and multicast addresses as well) */
+		/*
+		 * don't relay: just accept packets for local host/subnet
+		 * (this blocks link-local and multicast addresses as well)
+		 */
 		maskip(op->dst, localmask, a);
 		if (!equivip6(a, localnet)) {
 			syslog(0, "6in4", "ingress filtered %I -> %I",
@@ -300,7 +360,10 @@ tunnel2ip(int in, int out)
 		}
 
 		/* pass V6 packet to the interface */
-		write(out, op, n);
+		if (write(out, op, n) != n) {
+			syslog(0, "6in4", "error writing to packet interface (%r), giving up");
+			break;
+		}
 	}
 }