1 #!/bin/sh /etc/rc.common
2 # Copyright (C) 2007 OpenWrt.org
15 while [ -n "$ip" ]; do
18 res
="$(($res + $part))"
19 [ "${ip%.*}" != "$ip" ] && ip
="${ip#*.}" || ip
=
29 config_get_bool _loctmp
"$section" "$option" 0
30 [ $_loctmp -gt 0 ] && append args
"$value"
38 config_get _loctmp
"$section" "$option"
39 [ -z "$_loctmp" ] && return 0
40 append args
"$switch $_loctmp"
48 local ifname
=$
(uci_get_state network
"$1" ifname
"$1")
49 append args
"-i $ifname"
52 append_notinterface
() {
53 local ifname
=$
(uci_get_state network
"$1" ifname
"$1")
54 append args
"-I $ifname"
61 append_bogusnxdomain
() {
67 append_bool
"$cfg" authoritative
"-K"
68 append_bool
"$cfg" nodaemon
"-d"
69 append_bool
"$cfg" domainneeded
"-D"
70 append_bool
"$cfg" filterwin2k
"-f"
71 append_bool
"$cfg" nohosts
"-h"
72 append_bool
"$cfg" nonegcache
"-N"
73 append_bool
"$cfg" strictorder
"-o"
74 append_bool
"$cfg" logqueries
"-q"
75 append_bool
"$cfg" noresolv
"-R"
76 append_bool
"$cfg" localise_queries
"-y"
77 append_bool
"$cfg" readethers
"-Z"
78 append_bool
"$cfg" dbus
"-1"
79 append_bool
"$cfg" boguspriv
"-b"
80 append_bool
"$cfg" expandhosts
"-E"
81 append_bool
"$cfg" enable_tftp
"--enable-tftp"
82 append_bool
"$cfg" nonwildcard
"-z"
84 append_parm
"$cfg" dhcpscript
"-6"
85 append_parm
"$cfg" cachesize
"-c"
86 append_parm
"$cfg" dnsforwardmax
"-0"
87 append_parm
"$cfg" port
"-p"
88 append_parm
"$cfg" ednspacket_max
"-P"
89 append_parm
"$cfg" dhcpleasemax
"-X"
90 append_parm
"$cfg" "queryport" "-Q"
91 append_parm
"$cfg" "domain" "-s"
92 append_parm
"$cfg" "local" "-S"
93 config_list_foreach
"$cfg" "server" append_server
94 config_list_foreach
"$cfg" "interface" append_interface
95 config_list_foreach
"$cfg" "notinterface" append_notinterface
96 config_list_foreach
"$cfg" "addnhosts" append_addnhosts
97 config_list_foreach
"$cfg" "bogusnxdomain" append_bogusnxdomain
98 append_parm
"$cfg" "leasefile" "-l"
99 append_parm
"$cfg" "resolvfile" "-r"
100 append_parm
"$cfg" "tftp_root" "--tftp-root"
101 append_parm
"$cfg" "dhcp_boot" "--dhcp-boot"
103 config_get DOMAIN
"$cfg" domain
105 config_get_bool readethers
"$cfg" readethers
106 [ "$readethers" = "1" -a \
! -e "/etc/ethers" ] && touch /etc
/ethers
108 config_get leasefile
$cfg leasefile
109 [ -n "$leasefile" -a \
! -e "$leasefile" ] && touch "$leasefile"
110 config_get_bool cachelocal
"$cfg" cachelocal
1
112 config_get hostsfile
"$cfg" dhcphostsfile
113 [ -e "$hostsfile" ] && append args
"--dhcp-hostsfile=$hostsfile"
116 config_get_bool rebind
"$cfg" rebind_protection
1
117 [ $rebind -gt 0 ] && {
119 "DNS rebinding protection is active," \
120 "will discard upstream RFC1918 responses!"
121 append args
"--stop-dns-rebind"
123 local rebind_localhost
124 config_get_bool rebind_localhost
"$cfg" rebind_localhost
0
125 [ $rebind_localhost -gt 0 ] && {
126 logger
-t dnsmasq
"Allowing 127.0.0.0/8 responses"
127 append args
"--rebind-localhost-ok"
130 append_rebind_domain
() {
131 logger
-t dnsmasq
"Allowing RFC1918 responses for domain $1"
132 append args
"--rebind-domain-ok=$1"
135 config_list_foreach
"$cfg" rebind_domain append_rebind_domain
139 dhcp_subscrid_add
() {
142 config_get networkid
"$cfg" networkid
143 [ -n "$networkid" ] ||
return 0
145 config_get subscriberid
"$cfg" subscriberid
146 [ -n "$subscriberid" ] ||
return 0
148 append args
"--dhcp-subscrid=$networkid,$subscriberid"
150 dhcp_option_add
"$cfg" "$networkid"
153 dhcp_remoteid_add
() {
156 config_get networkid
"$cfg" networkid
157 [ -n "$networkid" ] ||
return 0
159 config_get remoteid
"$cfg" remoteid
160 [ -n "$remoteid" ] ||
return 0
162 append args
"--dhcp-remoteid=$networkid,$remoteid"
164 dhcp_option_add
"$cfg" "$networkid"
167 dhcp_circuitid_add
() {
170 config_get networkid
"$cfg" networkid
171 [ -n "$networkid" ] ||
return 0
173 config_get circuitid
"$cfg" circuitid
174 [ -n "$circuitid" ] ||
return 0
176 append args
"--dhcp-circuitid=$networkid,$circuitid"
178 dhcp_option_add
"$cfg" "$networkid"
181 dhcp_userclass_add
() {
184 config_get networkid
"$cfg" networkid
185 [ -n "$networkid" ] ||
return 0
187 config_get userclass
"$cfg" userclass
188 [ -n "$userclass" ] ||
return 0
190 append args
"--dhcp-userclass=$networkid,$userclass"
192 dhcp_option_add
"$cfg" "$networkid"
195 dhcp_vendorclass_add
() {
198 config_get networkid
"$cfg" networkid
199 [ -n "$networkid" ] ||
return 0
201 config_get vendorclass
"$cfg" vendorclass
202 [ -n "$vendorclass" ] ||
return 0
204 append args
"--dhcp-vendorclass=$networkid,$vendorclass"
206 dhcp_option_add
"$cfg" "$networkid"
212 config_get networkid
"$cfg" networkid
213 [ -n "$networkid" ] && dhcp_option_add
"$cfg" "$networkid"
215 config_get name
"$cfg" name
216 config_get ip
"$cfg" ip
217 [ -n "$ip" ] ||
[ -n "$name" ] ||
return 0
220 config_get mac
"$cfg" mac
221 for m
in $mac; do append macs
"$m" ","; done
222 [ -n "$macs" ] ||
return 0
224 append args
"--dhcp-host=$macs${networkid:+,net:$networkid}${ip:+,$ip}${name:+,$name}"
230 config_get networkid
"$cfg" networkid
231 [ -n "$networkid" ] ||
return 0
233 config_get mac
"$cfg" mac
234 [ -n "$mac" ] ||
return 0
236 append args
"--dhcp-mac=$networkid,$mac"
238 dhcp_option_add
"$cfg" "$networkid"
244 config_get networkid
"$cfg" networkid
246 config_get filename
"$cfg" filename
247 [ -n "$filename" ] ||
return 0
249 config_get servername
"$cfg" servername
250 [ -n "$servername" ] ||
return 0
252 config_get serveraddress
"$cfg" serveraddress
253 [ -n "$serveraddress" ] ||
return 0
255 append args
"--dhcp-boot=${networkid:+net:$networkid,}$filename,$servername,$serveraddress"
257 dhcp_option_add
"$cfg" "$networkid"
263 config_get net
"$cfg" interface
264 [ -n "$net" ] ||
return 0
266 config_get networkid
"$cfg" networkid
267 [ -n "$networkid" ] || networkid
="$net"
269 config_get ifname
"$net" ifname
270 [ -n "$ifname" ] ||
return 0
272 config_get dnsserver
"$net" dns
273 [ "$cachelocal" = "0" -a -n "$dnsserver" ] && {
274 DNS_SERVERS
="$DNS_SERVERS $dnsserver"
277 append_bool
"$cfg" ignore
"-2 $ifname" && return 0
279 config_get proto
"$net" proto
280 [ static
= "$proto" ] ||
return 0
282 config_get ipaddr
"$net" ipaddr
283 config_get netmask
"$cfg" netmask
284 [ -n "$netmask" ] || config_get netmask
"$net" netmask
286 #check for an already active dhcp server on the interface, unless 'force' is set
287 config_get_bool force
"$cfg" force
0
288 [ $force -gt 0 ] ||
{
289 udhcpc
-n -q -s /bin
/true
-t 1 -i $ifname >&- && {
291 "found already running DHCP-server on interface '$ifname'" \
292 "refusing to start, use 'option force 1' to override"
297 config_get start
"$cfg" start
298 config_get limit
"$cfg" limit
299 config_get leasetime
"$cfg" leasetime
300 config_get options
"$cfg" options
301 config_get_bool dynamicdhcp
"$cfg" dynamicdhcp
1
303 leasetime
="${leasetime:-12h}"
304 start
="$(dhcp_calc "${start:-100}")"
305 limit
="${limit:-150}"
306 [ "$limit" -gt 0 ] && limit
=$
((limit-1
))
307 eval "$(ipcalc.sh $ipaddr $netmask $start $limit)"
308 if [ "$dynamicdhcp" = "0" ]; then END
="static"; fi
309 append args
"--dhcp-range=$networkid,$START,$END,$NETMASK,$leasetime${options:+ $options}"
311 dhcp_option_add
"$cfg" "$networkid"
318 config_get dhcp_option
"$cfg" dhcp_option
319 for o
in $dhcp_option; do
320 append args
"-O $networkid","$o"
329 config_get names
"$cfg" name
"$2"
330 [ -n "$names" ] ||
return 0
332 config_get ip
"$cfg" ip
"$3"
333 [ -n "$ip" ] ||
return 0
335 local oIFS
="$IFS"; IFS
="."; set -- $ip; IFS
="$oIFS"
336 local raddr
="${4:+$4.$3.$2.$1.in-addr.arpa}"
338 for name
in $names; do
341 [ "${fqdn%.*}" == "$fqdn" ] && \
342 fqdn
="$fqdn${DOMAIN:+.$DOMAIN}"
344 append args
"-A /$fqdn/$ip"
347 append args
"--ptr-record=$raddr,$fqdn"
356 config_get srv
"$cfg" srv
357 [ -n "$srv" ] ||
return 0
359 config_get target
"$cfg" target
360 [ -n "$target" ] ||
return 0
362 config_get port
"$cfg" port
364 local service
="$srv,$target"
365 [ -n "$port" ] && service
="$service,$port"
367 append args
"-W $service"
374 config_get cname
"$cfg" cname
375 [ -n "$cname" ] ||
return 0
377 config_get target
"$cfg" target
378 [ -n "$target" ] ||
return 0
380 append args
"--cname=${cname},${target}"
388 config_get lanaddr
"lan" ipaddr
393 config_foreach dnsmasq dnsmasq
394 config_foreach dhcp_host_add
host
395 config_foreach dhcp_boot_add boot
396 config_foreach dhcp_mac_add mac
397 config_foreach dhcp_vendorclass_add vendorclass
398 config_foreach dhcp_userclass_add userclass
399 config_foreach dhcp_circuitid_add circuitid
400 config_foreach dhcp_remoteid_add remoteid
401 config_foreach dhcp_subscrid_add subscrid
402 config_foreach dhcp_domain_add domain
403 config_foreach dhcp_srv_add srvhost
404 config_foreach dhcp_add dhcp
405 config_foreach dhcp_cname_add cname
408 [ -z "$lanaddr" ] ||
{
409 local hostname
="$(uci_get system.@system[0].hostname)"
410 dhcp_domain_add
"" "${hostname:-OpenWrt}" "$lanaddr"
413 service_start
/usr
/sbin
/dnsmasq
$args && {
414 rm -f /tmp
/resolv.conf
415 [ -n "$DOMAIN" ] && echo "search $DOMAIN" >> /tmp
/resolv.conf
416 DNS_SERVERS
="$DNS_SERVERS 127.0.0.1"
417 for DNS_SERVER
in $DNS_SERVERS ; do
418 echo "nameserver $DNS_SERVER" >> /tmp
/resolv.conf
424 service_stop
/usr
/sbin
/dnsmasq
&& {
425 [ -f /tmp
/resolv.conf
] && {
426 rm -f /tmp
/resolv.conf
427 ln -s /tmp
/resolv.conf.auto
/tmp
/resolv.conf