dnsmasq: full: disable ipset support by default
[openwrt/staging/mkresin.git] / package / base-files / files / lib / functions / system.sh
1 # Copyright (C) 2006-2013 OpenWrt.org
2
3 . /lib/functions.sh
4 . /usr/share/libubox/jshn.sh
5
6 get_mac_binary() {
7 local path="$1"
8 local offset="$2"
9
10 if ! [ -e "$path" ]; then
11 echo "get_mac_binary: file $path not found!" >&2
12 return
13 fi
14
15 hexdump -v -n 6 -s $offset -e '5/1 "%02x:" 1/1 "%02x"' $path 2>/dev/null
16 }
17
18 get_mac_label_dt() {
19 local basepath="/proc/device-tree"
20 local macdevice="$(cat "$basepath/aliases/label-mac-device" 2>/dev/null)"
21 local macaddr
22
23 [ -n "$macdevice" ] || return
24
25 macaddr=$(get_mac_binary "$basepath/$macdevice/mac-address" 0 2>/dev/null)
26 [ -n "$macaddr" ] || macaddr=$(get_mac_binary "$basepath/$macdevice/local-mac-address" 0 2>/dev/null)
27
28 echo $macaddr
29 }
30
31 get_mac_label_json() {
32 local cfg="/etc/board.json"
33 local macaddr
34
35 [ -s "$cfg" ] || return
36
37 json_init
38 json_load "$(cat $cfg)"
39 if json_is_a system object; then
40 json_select system
41 json_get_var macaddr label_macaddr
42 json_select ..
43 fi
44
45 echo $macaddr
46 }
47
48 get_mac_label() {
49 local macaddr=$(get_mac_label_dt)
50
51 [ -n "$macaddr" ] || macaddr=$(get_mac_label_json)
52
53 echo $macaddr
54 }
55
56 find_mtd_chardev() {
57 local INDEX=$(find_mtd_index "$1")
58 local PREFIX=/dev/mtd
59
60 [ -d /dev/mtd ] && PREFIX=/dev/mtd/
61 echo "${INDEX:+$PREFIX$INDEX}"
62 }
63
64 mtd_get_mac_ascii() {
65 local mtdname="$1"
66 local key="$2"
67 local part
68 local mac_dirty
69
70 part=$(find_mtd_part "$mtdname")
71 if [ -z "$part" ]; then
72 echo "mtd_get_mac_ascii: partition $mtdname not found!" >&2
73 return
74 fi
75
76 mac_dirty=$(strings "$part" | sed -n 's/^'"$key"'=//p')
77
78 # "canonicalize" mac
79 [ -n "$mac_dirty" ] && macaddr_canonicalize "$mac_dirty"
80 }
81
82 mtd_get_mac_text() {
83 local mtdname=$1
84 local offset=$(($2))
85 local part
86 local mac_dirty
87
88 part=$(find_mtd_part "$mtdname")
89 if [ -z "$part" ]; then
90 echo "mtd_get_mac_text: partition $mtdname not found!" >&2
91 return
92 fi
93
94 if [ -z "$offset" ]; then
95 echo "mtd_get_mac_text: offset missing!" >&2
96 return
97 fi
98
99 mac_dirty=$(dd if="$part" bs=1 skip="$offset" count=17 2>/dev/null)
100
101 # "canonicalize" mac
102 [ -n "$mac_dirty" ] && macaddr_canonicalize "$mac_dirty"
103 }
104
105 mtd_get_mac_binary() {
106 local mtdname="$1"
107 local offset="$2"
108 local part
109
110 part=$(find_mtd_part "$mtdname")
111 get_mac_binary "$part" "$offset"
112 }
113
114 mtd_get_mac_binary_ubi() {
115 local mtdname="$1"
116 local offset="$2"
117
118 . /lib/upgrade/nand.sh
119
120 local ubidev=$(nand_find_ubi $CI_UBIPART)
121 local part=$(nand_find_volume $ubidev $1)
122
123 get_mac_binary "/dev/$part" "$offset"
124 }
125
126 mtd_get_part_size() {
127 local part_name=$1
128 local first dev size erasesize name
129 while read dev size erasesize name; do
130 name=${name#'"'}; name=${name%'"'}
131 if [ "$name" = "$part_name" ]; then
132 echo $((0x$size))
133 break
134 fi
135 done < /proc/mtd
136 }
137
138 mmc_get_mac_binary() {
139 local part_name="$1"
140 local offset="$2"
141 local part
142
143 part=$(find_mmc_part "$part_name")
144 get_mac_binary "$part" "$offset"
145 }
146
147 macaddr_add() {
148 local mac=$1
149 local val=$2
150 local oui=${mac%:*:*:*}
151 local nic=${mac#*:*:*:}
152
153 nic=$(printf "%06x" $((0x${nic//:/} + val & 0xffffff)) | sed 's/^\(.\{2\}\)\(.\{2\}\)\(.\{2\}\)/\1:\2:\3/')
154 echo $oui:$nic
155 }
156
157 macaddr_geteui() {
158 local mac=$1
159 local sep=$2
160
161 echo ${mac:9:2}$sep${mac:12:2}$sep${mac:15:2}
162 }
163
164 macaddr_setbit() {
165 local mac=$1
166 local bit=${2:-0}
167
168 [ $bit -gt 0 -a $bit -le 48 ] || return
169
170 printf "%012x" $(( 0x${mac//:/} | 2**(48-bit) )) | sed -e 's/\(.\{2\}\)/\1:/g' -e 's/:$//'
171 }
172
173 macaddr_unsetbit() {
174 local mac=$1
175 local bit=${2:-0}
176
177 [ $bit -gt 0 -a $bit -le 48 ] || return
178
179 printf "%012x" $(( 0x${mac//:/} & ~(2**(48-bit)) )) | sed -e 's/\(.\{2\}\)/\1:/g' -e 's/:$//'
180 }
181
182 macaddr_setbit_la() {
183 macaddr_setbit $1 7
184 }
185
186 macaddr_unsetbit_mc() {
187 local mac=$1
188
189 printf "%02x:%s" $((0x${mac%%:*} & ~0x01)) ${mac#*:}
190 }
191
192 macaddr_random() {
193 local randsrc=$(get_mac_binary /dev/urandom 0)
194
195 echo "$(macaddr_unsetbit_mc "$(macaddr_setbit_la "${randsrc}")")"
196 }
197
198 macaddr_2bin() {
199 local mac=$1
200
201 echo -ne \\x${mac//:/\\x}
202 }
203
204 macaddr_canonicalize() {
205 local mac="$1"
206 local canon=""
207
208 mac=$(echo -n $mac | tr -d \")
209 [ ${#mac} -gt 17 ] && return
210 [ -n "${mac//[a-fA-F0-9\.: -]/}" ] && return
211
212 for octet in ${mac//[\.:-]/ }; do
213 case "${#octet}" in
214 1)
215 octet="0${octet}"
216 ;;
217 2)
218 ;;
219 4)
220 octet="${octet:0:2} ${octet:2:2}"
221 ;;
222 12)
223 octet="${octet:0:2} ${octet:2:2} ${octet:4:2} ${octet:6:2} ${octet:8:2} ${octet:10:2}"
224 ;;
225 *)
226 return
227 ;;
228 esac
229 canon=${canon}${canon:+ }${octet}
230 done
231
232 [ ${#canon} -ne 17 ] && return
233
234 printf "%02x:%02x:%02x:%02x:%02x:%02x" 0x${canon// / 0x} 2>/dev/null
235 }