1 #!/bin/sh /etc/rc.common
2 # Copyright (C) 2007 OpenWrt.org
12 while [ -n "$ip" ]; do
15 res
="$(($res + $part))"
16 [ "${ip%.*}" != "$ip" ] && ip
="${ip#*.}" || ip
=
26 config_get_bool _loctmp
"$section" "$option" 0
27 [ $_loctmp -gt 0 ] && append args
"$value"
35 config_get _loctmp
"$section" "$option"
36 [ -z "$_loctmp" ] && return 0
37 append args
"$switch $_loctmp"
45 local ifname
=$
(uci_get_state network
"$1" ifname
"$1")
46 append args
"-i $ifname"
49 append_notinterface
() {
50 local ifname
=$
(uci_get_state network
"$1" ifname
"$1")
51 append args
"-I $ifname"
60 append_bool
"$cfg" authoritative
"-K"
61 append_bool
"$cfg" nodaemon
"-d"
62 append_bool
"$cfg" domainneeded
"-D"
63 append_bool
"$cfg" filterwin2k
"-f"
64 append_bool
"$cfg" nohosts
"-h"
65 append_bool
"$cfg" nonegcache
"-N"
66 append_bool
"$cfg" strictorder
"-o"
67 append_bool
"$cfg" logqueries
"-q"
68 append_bool
"$cfg" noresolv
"-R"
69 append_bool
"$cfg" localise_queries
"-y"
70 append_bool
"$cfg" readethers
"-Z"
71 append_bool
"$cfg" dbus
"-1"
72 append_bool
"$cfg" boguspriv
"-b"
73 append_bool
"$cfg" expandhosts
"-E"
74 append_bool
"$cfg" enable_tftp
"--enable-tftp"
75 append_bool
"$cfg" nonwildcard
"-z"
77 append_parm
"$cfg" cachesize
"-c"
78 append_parm
"$cfg" dnsforwardmax
"-0"
79 append_parm
"$cfg" port
"-p"
80 append_parm
"$cfg" ednspacket_max
"-P"
81 append_parm
"$cfg" dhcpleasemax
"-X"
82 append_parm
"$cfg" "queryport" "-Q"
83 append_parm
"$cfg" "domain" "-s"
84 append_parm
"$cfg" "local" "-S"
85 config_list_foreach
"$cfg" "server" append_server
86 config_list_foreach
"$cfg" "interface" append_interface
87 config_list_foreach
"$cfg" "notinterface" append_notinterface
88 config_list_foreach
"$cfg" "addnhosts" append_addnhosts
89 append_parm
"$cfg" "leasefile" "-l"
90 append_parm
"$cfg" "resolvfile" "-r"
91 append_parm
"$cfg" "tftp_root" "--tftp-root"
92 append_parm
"$cfg" "dhcp_boot" "--dhcp-boot"
94 config_get DOMAIN
"$cfg" domain
96 config_get_bool readethers
"$cfg" readethers
97 [ "$readethers" = "1" ] && [ -e "/etc/ethers" ] ||
touch /etc
/ethers
99 config_get leasefile
$cfg leasefile
100 [ -e "$leasefile" ] ||
touch "$leasefile"
101 config_get_bool cachelocal
"$cfg" cachelocal
1
103 config_get hostsfile
"$cfg" dhcphostsfile
104 [ -e "$hostsfile" ] && append args
"--dhcp-hostsfile=$hostsfile"
107 config_get_bool rebind
"$cfg" rebind_protection
1
108 [ $rebind -gt 0 ] && {
110 "DNS rebinding protection is active," \
111 "will discard upstream RFC1918 responses!"
112 append args
"--stop-dns-rebind"
114 local rebind_localhost
115 config_get_bool rebind_localhost
"$cfg" rebind_localhost
0
116 [ $rebind_localhost -gt 0 ] && {
117 logger
-t dnsmasq
"Allowing 127.0.0.0/8 responses"
118 append args
"--rebind-localhost-ok"
121 append_rebind_domain
() {
122 logger
-t dnsmasq
"Allowing RFC1918 responses for domain $1"
123 append args
"--rebind-domain-ok=$1"
126 config_list_foreach
"$cfg" rebind_domain append_rebind_domain
130 dhcp_subscrid_add
() {
133 config_get networkid
"$cfg" networkid
134 [ -n "$networkid" ] ||
return 0
136 config_get subscriberid
"$cfg" subscriberid
137 [ -n "$subscriberid" ] ||
return 0
139 append args
"--dhcp-subscrid=$networkid,$subscriberid"
141 dhcp_option_add
"$cfg" "$networkid"
144 dhcp_remoteid_add
() {
147 config_get networkid
"$cfg" networkid
148 [ -n "$networkid" ] ||
return 0
150 config_get remoteid
"$cfg" remoteid
151 [ -n "$remoteid" ] ||
return 0
153 append args
"--dhcp-remoteid=$networkid,$remoteid"
155 dhcp_option_add
"$cfg" "$networkid"
158 dhcp_circuitid_add
() {
161 config_get networkid
"$cfg" networkid
162 [ -n "$networkid" ] ||
return 0
164 config_get circuitid
"$cfg" circuitid
165 [ -n "$circuitid" ] ||
return 0
167 append args
"--dhcp-circuitid=$networkid,$circuitid"
169 dhcp_option_add
"$cfg" "$networkid"
172 dhcp_userclass_add
() {
175 config_get networkid
"$cfg" networkid
176 [ -n "$networkid" ] ||
return 0
178 config_get userclass
"$cfg" userclass
179 [ -n "$userclass" ] ||
return 0
181 append args
"--dhcp-userclass=$networkid,$userclass"
183 dhcp_option_add
"$cfg" "$networkid"
186 dhcp_vendorclass_add
() {
189 config_get networkid
"$cfg" networkid
190 [ -n "$networkid" ] ||
return 0
192 config_get vendorclass
"$cfg" vendorclass
193 [ -n "$vendorclass" ] ||
return 0
195 append args
"--dhcp-vendorclass=$networkid,$vendorclass"
197 dhcp_option_add
"$cfg" "$networkid"
203 config_get name
"$cfg" name
205 config_get networkid
"$cfg" networkid
206 [ -n "$networkid" ] && dhcp_option_add
"$cfg" "$networkid"
208 config_get ip
"$cfg" ip
209 [ -n "$ip" ] ||
return 0
212 config_get mac
"$cfg" mac
213 for m
in $mac; do append macs
"$m" ","; done
214 [ -n "$macs" ] ||
return 0
216 append args
"--dhcp-host=$macs,${networkid:+net:$networkid,}$ip${name:+,$name}"
222 config_get networkid
"$cfg" networkid
223 [ -n "$networkid" ] ||
return 0
225 config_get mac
"$cfg" mac
226 [ -n "$mac" ] ||
return 0
228 append args
"--dhcp-mac=$networkid,$mac"
230 dhcp_option_add
"$cfg" "$networkid"
236 config_get networkid
"$cfg" networkid
238 config_get filename
"$cfg" filename
239 [ -n "$filename" ] ||
return 0
241 config_get servername
"$cfg" servername
242 [ -n "$servername" ] ||
return 0
244 config_get serveraddress
"$cfg" serveraddress
245 [ -n "$serveraddress" ] ||
return 0
247 append args
"--dhcp-boot=${networkid:+net:$networkid,}$filename,$servername,$serveraddress"
249 dhcp_option_add
"$cfg" "$networkid"
255 config_get net
"$cfg" interface
256 [ -n "$net" ] ||
return 0
258 config_get networkid
"$cfg" networkid
259 [ -n "$networkid" ] || networkid
="$net"
261 config_get ifname
"$net" ifname
262 [ -n "$ifname" ] ||
return 0
264 config_get dnsserver
"$net" dns
265 [ "$cachelocal" = "0" -a -n "$dnsserver" ] && {
266 DNS_SERVERS
="$DNS_SERVERS $dnsserver"
269 append_bool
"$cfg" ignore
"-2 '$ifname'" && return 0
271 config_get proto
"$net" proto
272 [ static
= "$proto" ] ||
return 0
274 config_get ipaddr
"$net" ipaddr
275 config_get netmask
"$cfg" netmask
276 [ -n "$netmask" ] || config_get netmask
"$net" netmask
278 #check for an already active dhcp server on the interface, unless 'force' is set
279 config_get_bool force
"$cfg" force
0
280 [ $force -gt 0 ] ||
{
281 udhcpc
-n -q -s /bin
/true
-t 1 -i $ifname >&- && return 0
284 config_get start
"$cfg" start
285 config_get limit
"$cfg" limit
286 config_get leasetime
"$cfg" leasetime
287 config_get options
"$cfg" options
288 config_get_bool dynamicdhcp
"$cfg" dynamicdhcp
1
290 leasetime
="${leasetime:-12h}"
291 start
="$(dhcp_calc "${start:-100}")"
292 limit
="${limit:-150}"
293 eval "$(ipcalc.sh $ipaddr $netmask $start $limit)"
294 if [ "$dynamicdhcp" = "0" ]; then END
="static"; fi
295 append args
"--dhcp-range=$networkid,$START,$END,$NETMASK,$leasetime${options:+ $options}"
297 dhcp_option_add
"$cfg" "$networkid"
304 config_get dhcp_option
"$cfg" dhcp_option
305 for o
in $dhcp_option; do
306 append args
"-O $networkid","$o"
315 config_get names
"$cfg" name
316 [ -n "$names" ] ||
return 0
318 config_get ip
"$cfg" ip
319 [ -n "$ip" ] ||
return 0
321 local oIFS
="$IFS"; IFS
="."; set -- $ip; IFS
="$oIFS"
322 local raddr
="${4:+$4.$3.$2.$1.in-addr.arpa}"
324 for name
in $names; do
327 [ "${fqdn%.*}" == "$fqdn" ] && \
328 fqdn
="$fqdn${DOMAIN:+.$DOMAIN}"
330 append args
"-A /$fqdn/$ip"
333 append args
"--ptr-record=$raddr,$fqdn"
342 config_get srv
"$cfg" srv
343 [ -n "$srv" ] ||
return 0
345 config_get target
"$cfg" target
346 [ -n "$target" ] ||
return 0
348 config_get port
"$cfg" port
350 local service
="$srv,$target"
351 [ -n "$port" ] && service
="$service,$port"
353 append args
"-W $service"
362 config_foreach dnsmasq dnsmasq
363 config_foreach dhcp_host_add
host
364 config_foreach dhcp_boot_add boot
365 config_foreach dhcp_mac_add mac
366 config_foreach dhcp_vendorclass_add vendorclass
367 config_foreach dhcp_userclass_add userclass
368 config_foreach dhcp_circuitid_add circuitid
369 config_foreach dhcp_remoteid_add remoteid
370 config_foreach dhcp_subscrid_add subscrid
371 config_foreach dhcp_domain_add domain
372 config_foreach dhcp_srv_add srvhost
373 config_foreach dhcp_add dhcp
375 /usr
/sbin
/dnsmasq
$args && {
376 rm -f /tmp
/resolv.conf
377 [ -n "$DOMAIN" ] && echo "search $DOMAIN" >> /tmp
/resolv.conf
378 DNS_SERVERS
="$DNS_SERVERS 127.0.0.1"
379 for DNS_SERVER
in $DNS_SERVERS ; do
380 echo "nameserver $DNS_SERVER" >> /tmp
/resolv.conf
386 [ -f /tmp
/resolv.conf
] && {
387 rm -f /tmp
/resolv.conf
388 ln -s /tmp
/resolv.conf.auto
/tmp
/resolv.conf