[package] mac-to-devinfo updates and cleanups (#5488)
[openwrt/svn-archive/archive.git] / net / mac-to-devinfo / files / mac-to-devinfo
1 #!/bin/sh
2
3 UCIBIN=uci
4 UCIFUNC=/usr/lib/mac-to-devinfo/devinfo-functions.sh
5 OVERRIDETABLE=mactodevinfo
6 DEVDB=macdevdb
7 UCICONFIG=/etc/config
8
9 OUIURL=http://standards.ieee.org/regauth/oui/oui.txt
10 IABURL=http://standards.ieee.org/regauth/oui/iab.txt
11
12 OUIDIR=/var/cache/mac-to-devinfo
13
14 OUIFILE=$OUIDIR/oui.txt
15 IABFILE=$OUIDIR/iab.txt
16
17 UCIDEVDB=/usr/share/mac-to-devinfo
18 UCISTATE="$(mktemp -t)"
19
20 . $UCIFUNC
21
22
23 if [ "$1" == "-x" ]; then
24 PORT="$2"
25 if [ -z "$PORT" ]; then
26 echo "-x without the port"
27 echo "Usage: $0 [-x port] MAC-address"
28 exit 1
29 fi
30 shift
31 shift
32 fi
33
34 MAC="$1"
35
36 if [ -z "$MAC" ]; then
37 echo "Unknown MAC address | Unknown | unknown | unknown | unknown"
38 exit 1
39 fi
40
41 MAC=$(echo $MAC | sed -e 's/^\([0-9]\):/0\1:/g' | sed -e 's/:\([0-9]\)$/:0\1/g' | sed -e 's/:\([0-9]\):/:0\1:/g')
42
43 parse_override() {
44 local cfg="$1"
45 config_get name "$cfg" name
46 config_get maclow "$cfg" maclow
47 config_get machigh "$cfg" machigh
48 config_get vendor "$cfg" vendor
49 config_get devtype "$cfg" devtype
50 config_get model "$cfg" model
51 config_get ouiowneroverride "$cfg" ouiowner
52 LOW=$(echo $maclow | sed -e 's/://g')
53 HIGH=$(echo $machigh | sed -e 's/://g')
54 if [ -z "$LOW" ] || [ -z "$HIGH" ] || [ -n "$(echo $LOW | grep -E -v '^[0-9A-Fa-f]{12}$')" ] || [ -n "$(echo $HIGH | grep -E -v '^[0-9A-Fa-f]{12}$')" ]; then
55 echo "Invalid mac range for $name: $ouiowneroverride $vendor $devtype $model : $LOW-$HIGH" >&2
56 return 1
57 fi
58
59 LOWMSB=$(expr substr "$LOW" 1 6)
60 LOWLSB=$(expr substr "$LOW" 7 6)
61 HIGHMSB=$(expr substr "$HIGH" 1 6)
62 HIGHLSB=$(expr substr "$HIGH" 7 6)
63
64 if [ -z "$LOWMSB" ] || [ -z "$LOWLSB" ] || [ -z "$HIGHMSB" ] || [ -z "$HIGHLSB" ]; then
65 echo "Error splitting database address strings"
66 return 1
67 fi
68
69 MACMSB=$(expr substr "$MACNUM" 1 6)
70 MACLSB=$(expr substr "$MACNUM" 7 6)
71
72 if [ -z "$MACMSB" ] || [ -z "$MACLSB" ]; then
73 echo "Error spliiting mac address string"
74 return 1
75 fi
76
77 if [ -n "$ouiowneroverride" ]; then
78 OUIOWNER="$ouiowneroverride"
79 fi
80 if [ $((0x$MACMSB)) -ge $((0x$LOWMSB)) ] && [ $((0x$MACMSB)) -le $((0x$HIGHMSB)) ]; then
81 if [ $((0x$MACLSB)) -ge $((0x$LOWLSB)) ] && [ $((0x$MACLSB)) -le $((0x$HIGHLSB)) ]; then
82 if [ "$PORT" == "5060" ]; then
83 if [ "$vendor" == "Vertical Communications" ]; then
84 devtype="VoIP Phone"
85 model="IP200x"
86 fi
87 fi
88 if [ "$PORT" == "6060" ]; then
89 if [ "$vendor" == "Vertical Communications" ]; then
90 devtype="VoIP PBX"
91 model="Xcelerator IP"
92 fi
93 fi
94 if [ -n "$OUTSTRING" ]; then
95 echo "ERROR: We already have found information '$OUTSTRING'" >&2
96 echo -n "and now we have: " >&2
97 echo "$MAC | $OUIOWNER | $vendor | $devtype | $model" >&2
98 return 1
99 else
100 echo "$MAC | $OUIOWNER | $vendor | $devtype | $model"
101 return 0
102 fi
103 fi
104 fi
105 return 0
106 }
107
108 if [ ! -r "$OUIFILE" ] || [ ! -r "$IABFILE" ]; then
109 echo "Missing IEEE Registration Authority Database. Please run prep-devinfo"
110 exit 1
111 fi
112
113 OUI="$(echo $MAC | cut -f1-3 -d: | sed -e 's/:/-/g' | tr abcdef ABCDEF )"
114
115 OUIOWNER="$(cat $OUIFILE | grep $OUI | tr -s '\t' | cut -f2- -d"$(printf '\t')" )"
116
117 if [ -z "$OUIOWNER" ] || [ "$OUIOWNER" = "IEEE REGISTRATION AUTHORITY" ]; then
118 if [ -n "$(cat $IABFILE | grep $OUI)" ]; then
119 LOWRANGE="$(echo $MAC | cut -f4-6 -d: | sed -e 's/://g' | tr abcdef ABCDEF )"
120 for range in $(cat $IABFILE |grep -E '[0-9A-F]{6}\-[0-9A-F]{6}' |cut -f1 -d"$(printf '\t')" ); do
121 LOW="$(echo $range | cut -f1 -d- )"
122 HIGH="$(echo $range | cut -f2 -d- )"
123 if [ -z "$LOW" ] || [ -z "$HIGH" ]; then
124 echo "Error parsing IAB table"
125 exit 1
126
127 fi
128 LOWMSB=$(expr substr "$LOW" 1 6)
129 LOWLSB=$(expr substr "$LOW" 7 6)
130 HIGHMSB=$(expr substr "$HIGH" 1 6)
131 HIGHLSB=$(expr substr "$HIGH" 7 6)
132
133 if [ -z "$LOWMSB" ] || [ -z "$LOWLSB" ] || [ -z "$HIGHMSB" ] || [ -z "$HIGHLSB" ]; then
134 echo "Error splitting registration authority IAB address strings"
135 exit 1
136 fi
137
138 MACMSB=$(expr substr "$LOWRANGE" 1 6)
139 MACLSB=$(expr substr "$LOWRANGE" 7 6)
140
141 if [ -z "$MACMSB" ] || [ -z "$MACLSB" ]; then
142 echo "Error splitting mac address string"
143 exit 1
144 fi
145 if [ $((0x$MACMSB)) -ge $((0x$LOWMSB)) ] && [ $((0x$MACMSB)) -le $((0x$HIGHMSB)) ]; then
146 if [ $((0x$MACLSB)) -ge $((0x$LOWLSB)) ] && [ $((0x$MACLSB)) -le $((0x$HIGHLSB)) ]; then
147 OUIOWNER="$(cat $IABFILE | grep "$LOW-$HIGH" | tr -s '\t' | cut -f3- -d"$(printf '\t')" )"
148 break;
149 fi
150 fi
151 done
152 fi
153 fi
154
155 if [ -z "$OUIOWNER" ]; then
156 OUIOWNER="Unknown Organization"
157 fi
158
159 MACNUM="$(echo $MAC |sed -e 's/://g' | tr ABCDEF abcdef )"
160
161 UCI="$UCIBIN -c $UCIDEVDB"
162 config_load $DEVDB
163 DEVSTRING=$(config_foreach parse_override mactodevinfo)
164
165 SAVEDEVSTRING="$DEVSTRING"
166
167 UCI="$UCIBIN -c $UCICONFIG"
168 config_load $OVERRIDETABLE
169 DEVSTRING=$(config_foreach parse_override mactodevinfo)
170
171 if [ -z "$DEVSTRING" ]; then
172 if [ -z "$SAVEDEVSTRING" ]; then
173 echo "$MAC | $OUIOWNER | unknown | unknown | unknown"
174 else
175 echo "$SAVEDEVSTRING"
176 fi
177 else
178 echo "$DEVSTRING"
179 fi
180
181 rm -f $UCISTATE