Skip to content

Commit ad11f9b

Browse files
committed
Initial commit
0 parents  commit ad11f9b

File tree

3 files changed

+419
-0
lines changed

3 files changed

+419
-0
lines changed

gen-ula.sh

+131
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
#!/bin/bash
2+
#
3+
# ULA Generator
4+
# Alexandre de Verteuil <[email protected]>
5+
# 2017-06-20
6+
#
7+
# Based on scripts from Shinsuke Suzuki and Holger Zuleger,
8+
# available under the prior_art directory.
9+
#
10+
# Usage: simply run in a bash shell. You will be prompted for your
11+
# physical MAC address. Guessing eth0 is no longer relevant in 2017.
12+
#
13+
# Requirements: wget, ntpq
14+
#
15+
# Improvements over other scripts:
16+
# - Not a CGI script
17+
# - Computes the SHA1 hash of bytes, not their hex representation
18+
# - Better text output
19+
#
20+
# References:
21+
#
22+
# RFC 4193 -- Unique Local IPv6 Unicast Addresses
23+
# https://tools.ietf.org/html/rfc4193
24+
#
25+
# Unique local address
26+
# https://en.wikipedia.org/wiki/Unique_local_address
27+
28+
29+
function die() {
30+
echo
31+
echo "== Error =="
32+
echo "$@"
33+
exit 1
34+
}
35+
36+
read -p "MAC address: " mac
37+
mac=$(echo $mac | tr -d :-)
38+
if [ ${#mac} -ne 12 ]; then
39+
die "MAC address \"${mac}\" is invalid"
40+
fi
41+
42+
echo "For a deterministic calculation, you may enter the ntp clock time."
43+
echo "Leave empty to query an NTP server."
44+
read -p "Clock: "
45+
if [ -z "${clock}" ]; then
46+
clock=$(ntpq -c "rv 0 clock" 0.pool.ntp.org | cut -c7-23)
47+
# Input: "clock=dcf4268b.208dd000 Tue, Jun 20 2017 18:56:11.127"
48+
# Output: "dcf4268b.208dd000"
49+
fi
50+
clock=$(tr -d . <<< "${clock}")
51+
if [ "${#clock}" -ne 16 ]; then
52+
die "Time in NTP format is 64 bits, "\
53+
"or 16 characters in hex representation. "\
54+
"You entered: \"${clock}\"."
55+
56+
if [ ! -r oui.txt ]; then
57+
wget http://standards.ieee.org/regauth/oui/oui.txt
58+
fi
59+
60+
# MAC Vendor check
61+
machexvendor=$(tr a-f A-F <<< "${mac:0:6}")
62+
macvendor=$(grep "^$machexvendor" oui.txt | sed -r "s/.*\t([^\r\n]*).*/\1/")
63+
if [ -z "$macvendor" ]; then
64+
die "MAC address \"${mac}\" is not registered to IEEE. "\
65+
"Please use a REAL MAC address."
66+
fi
67+
68+
# Generate an EUI64 from the MAC address
69+
# as described in RFC 3513
70+
# https://tools.ietf.org/html/rfc3513
71+
72+
first=`echo $mac | cut -c1-1`
73+
second=`echo $mac | cut -c2-2`
74+
macu=`echo $mac | cut -c3-6`
75+
macl=`echo $mac | cut -c7-12`
76+
77+
# reversing u/l bit
78+
case $second in
79+
[13579bdf])
80+
echo "Error"
81+
echo "MAC-address = $mac is a group MAC address"
82+
exit
83+
;;
84+
0)
85+
second_rev=2
86+
;;
87+
2)
88+
second_rev=0
89+
;;
90+
4)
91+
second_rev=6;
92+
;;
93+
6)
94+
second_rev=4;
95+
;;
96+
8)
97+
second_rev=a;
98+
;;
99+
a)
100+
second_rev=8;
101+
;;
102+
c)
103+
second_rev=e;
104+
;;
105+
e)
106+
second_rev=c;
107+
;;
108+
*)
109+
#impossible
110+
die "MAC address \"${mac}\" is registered to the IEEE database, "\
111+
"but the first octet (${first}${second}) is regarded as invalid. "\
112+
"(probably a bug in this script...)"
113+
esac
114+
eui64="${first}${second_rev}${macu}fffe${macl}"
115+
116+
# Convert from hex string representation to bytes before sha1sum
117+
# https://unix.stackexchange.com/a/82766
118+
globalid=$(echo -ne $(sed "s/../\\x&/g" <<< ${date}${eui64}) | sha1sum | cut -c23-32)
119+
ula=$(echo fd${globalid} | sed "s|\(....\)\(....\)\(....\)|\1:\2:\3::/48|")
120+
121+
echo
122+
echo "## Inputs ##"
123+
echo "MAC address = ${mac} (${macvendor})"
124+
echo "NTP time = ${clock}"
125+
echo
126+
echo "## Intermediary values ##"
127+
echo "EUI64 address = ${eui64}"
128+
echo
129+
echo "## Generated ULA ##"
130+
echo "${ula}"
131+
echo

prior_art/gen-ula.sh

+201
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,201 @@
1+
#!/bin/sh
2+
#
3+
# ULA Generator
4+
# SUZUKI, Shinsuke <[email protected]>
5+
# based on Holger Zuleger's script
6+
# @(#) generate-uniq-local-ipv6-unicast-addr.sh
7+
# (c) Sep 2004 Holger Zuleger
8+
#
9+
# Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000, 2001, and 2002 WIDE Project.
10+
# All rights reserved.
11+
#
12+
# Redistribution and use in source and binary forms, with or without
13+
# modification, are permitted provided that the following conditions
14+
# are met:
15+
# 1. Redistributions of source code must retain the above copyright
16+
# notice, this list of conditions and the following disclaimer.
17+
# 2. Redistributions in binary form must reproduce the above copyright
18+
# notice, this list of conditions and the following disclaimer in the
19+
# documentation and/or other materials provided with the distribution.
20+
# 3. Neither the name of the project nor the names of its contributors
21+
# may be used to endorse or promote products derived from this software
22+
# without specific prior written permission.
23+
#
24+
# THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
25+
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26+
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27+
# ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
28+
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29+
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30+
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31+
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32+
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33+
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34+
# SUCH DAMAGE.
35+
#
36+
# $Id: gen-ula.cgi,v 1.11 2013/05/20 22:43:57 suz Exp $
37+
#
38+
39+
40+
#
41+
#site-specific parameters
42+
#
43+
44+
#NTP server name
45+
ntpserv="clk.notemachi.wide.ad.jp"
46+
47+
#ntpdate program
48+
ntpdate=/usr/sbin/ntpdate
49+
50+
#the caller of this CGI script
51+
url="http://www.kame.net/~suz/gen-ula.html"
52+
53+
#list of typical bogus lower 24-bits in MAC address
54+
typical_mac_list="010203 000000 000001 000102 123456"
55+
56+
#SHA hash calculator
57+
sha=/usr/local/bin/sha
58+
59+
#IEEE OUI Database (available at http::/standards.ieee.org/regauth/oui/oui.txt)
60+
ieeeoui=oui.txt
61+
62+
#
63+
# main routine
64+
#
65+
#print-out header
66+
echo Content-type: text/html
67+
echo
68+
69+
cat << EOH
70+
<body>
71+
<head>
72+
<title>
73+
Generated ULA
74+
</title>
75+
</head>
76+
<body>
77+
EOH
78+
79+
#main routine
80+
81+
#1. NTP-date
82+
date=`$ntpdate -dq $ntpserv 2> /dev/null |
83+
sed -n 's/^transmit timestamp: *\([^ ]*\) .*/\1/p' |
84+
tr -d "."`
85+
86+
#2. EUI64
87+
# obtains a MAC address
88+
# sed 's/\(.*\)=\(..:..:..:..:..:..\).*$/\2/' |
89+
mac=`echo $QUERY_STRING | sed 's/%3A/:/g' |
90+
tr '[:lower:]' '[:upper:]' |
91+
sed 's/\(.*\)=\(..:..:..:..:..:..\).*$/\2/'`;
92+
machex=`echo $mac | tr -d ':'`
93+
#length check
94+
len1=`echo $machex | wc -m`
95+
#character check
96+
len2=`echo $machex | tr -d [:xdigit:] | wc -m`
97+
if [ $len1 != 13 -o $len2 != 1 ]; then
98+
echo "<h1>Error</h1>"
99+
echo "<p>MAC-address $mac is invalid"
100+
echo "<p>Please take care of the following points:
101+
<ul>
102+
<li>each octet must be separeted by colon (e.g. 00-01-02-03-04-05 is not permitted)
103+
<li>leading 0 of each octet cannot be omitted (e.g. 3:4:5:4:5:9 is not permitted)
104+
<li>each octet consists of 0-9 and A-F.
105+
</ul></p>"
106+
echo "<a href="$url">go back to the previous page</a></body>"
107+
exit
108+
fi
109+
110+
# MAC Vendor check
111+
machexvendor=`echo $machex | cut -c1-6`
112+
macvendor=`grep "^ $machexvendor" $ieeeoui | cut -c23-`
113+
if [ -z "$macvendor" ]; then
114+
echo "<h1>Error</h1>"
115+
echo "<p>MAC-address $mac"
116+
echo "is not registered to IEEE</p>"
117+
echo "<p>Please use the REAL MAC address</p>"
118+
echo "<a href="$url">go back to the previous page</a></body>"
119+
exit
120+
fi
121+
122+
# booby trap:-)
123+
machexid=`echo $machex | cut -c7-12`
124+
for i in $typical_mac_list; do
125+
if [ $machexid = $i ]; then
126+
echo "<h1>Error</h1>"
127+
echo "<p>MAC-address $mac"
128+
echo "is a typical non-existing MAC address that is frequently used when a user is reluctant to find his/her own MAC address:-)</p>"
129+
echo "<p>Please use the REAL MAC address</p>"
130+
echo "<a href="$url">go back to the previous page</a></body>"
131+
exit
132+
fi
133+
done
134+
135+
# generates EUI64 from the MAC address
136+
first=`echo $machex | cut -c1-1`
137+
second=`echo $machex | cut -c2-2`
138+
macu=`echo $machex | cut -c3-6`
139+
macl=`echo $machex | cut -c7-12`
140+
141+
# reversing u/l bit
142+
case $second in
143+
[13579BDF])
144+
echo "<h1>Error</h1>"
145+
echo "<p>MAC-address = $mac is a group MAC address</p>"
146+
echo "<a href="$url">go back to the previous page</a></body>"
147+
exit
148+
;;
149+
0)
150+
second_rev=2
151+
;;
152+
2)
153+
second_rev=0
154+
;;
155+
4)
156+
second_rev=6;
157+
;;
158+
6)
159+
second_rev=4;
160+
;;
161+
8)
162+
second_rev=a;
163+
;;
164+
A)
165+
second_rev=8;
166+
;;
167+
C)
168+
second_rev=e;
169+
;;
170+
E)
171+
second_rev=c;
172+
;;
173+
*)
174+
#impossible
175+
echo "<h1>Error</h1>"
176+
echo "<p>MAC-address = $mac"
177+
echo "is registered to the IEEE database, but the first octet is regarded as invalid. (probably a bug in this script...) ($first$second)</p>"
178+
echo "<a href="$url">go back to the previous page</a></body>"
179+
exit
180+
esac
181+
eui64="${first}${second_rev}${macu}fffe${macl}"
182+
183+
globalid=`echo $date$eui64 | ${sha} -1 | cut -c23-32`
184+
echo "<center>"
185+
echo "<h1>"
186+
echo "Generated ULA="
187+
echo fd${globalid} | sed "s|\(....\)\(....\)\(....\)|\1:\2:\3::/48|"
188+
echo "</center>"
189+
echo "</h1>"
190+
191+
cat << TAIL
192+
<ul>
193+
<li>MAC address=$mac ($macvendor)
194+
<li>EUI64 address=$eui64
195+
<li>NTP date=$date
196+
</ul>
197+
198+
<a href="$url">go back to the previous page</a>
199+
</body>
200+
TAIL
201+
exit

0 commit comments

Comments
 (0)