1 #!/bin/sh /etc/rc.common
2 # Copyright (C) 2007-2012 OpenWrt.org
15 CONFIGFILE="/var/etc/dnsmasq.conf"
16 HOSTFILE="/tmp/hosts/dhcp"
17 TRUSTANCHORSFILE="/usr/share/dnsmasq/trust-anchors.conf"
22 echo "${value#--}" >> $CONFIGFILE
29 while [ -n "$ip" ]; do
32 res="$(($res + $part))"
33 [ "${ip%.*}" != "$ip" ] && ip="${ip#*.}" || ip=
40 local stamp="/var/run/dnsmasq.$ifname.dhcp"
43 [ -s "$stamp" ] && return $(cat "$stamp")
45 udhcpc -n -q -s /bin/true -t 1 -i "$ifname" >&- && rv=1 || rv=0
49 "found already running DHCP-server on interface '$ifname'" \
50 "refusing to start, use 'option force 1' to override"
57 pidof dnsmasq >/dev/null || \
58 logger -t dnsmasq "$@"
66 config_get_bool _loctmp "$section" "$option" 0
67 [ $_loctmp -gt 0 ] && xappend "$value"
75 config_get _loctmp "$section" "$option"
76 [ -z "$_loctmp" ] && return 0
77 xappend "$switch=$_loctmp"
85 xappend "--address=$1"
93 local ifname=$(uci_get_state network "$1" ifname "$1")
94 xappend "--interface=$ifname"
97 append_notinterface() {
98 local ifname=$(uci_get_state network "$1" ifname "$1")
99 xappend "--except-interface=$ifname"
103 xappend "--addn-hosts=$1"
106 append_bogusnxdomain() {
107 xappend "--bogus-nxdomain=$1"
112 append_bool "$cfg" authoritative "--dhcp-authoritative"
113 append_bool "$cfg" nodaemon "--no-daemon"
114 append_bool "$cfg" domainneeded "--domain-needed"
115 append_bool "$cfg" filterwin2k "--filterwin2k"
116 append_bool "$cfg" nohosts "--no-hosts"
117 append_bool "$cfg" nonegcache "--no-negcache"
118 append_bool "$cfg" strictorder "--strict-order"
119 append_bool "$cfg" logqueries "--log-queries"
120 append_bool "$cfg" noresolv "--no-resolv"
121 append_bool "$cfg" localise_queries "--localise-queries"
122 append_bool "$cfg" readethers "--read-ethers"
123 append_bool "$cfg" dbus "--enable-dbus"
124 append_bool "$cfg" boguspriv "--bogus-priv"
125 append_bool "$cfg" expandhosts "--expand-hosts"
126 append_bool "$cfg" enable_tftp "--enable-tftp"
127 append_bool "$cfg" nonwildcard "--bind-interfaces"
128 append_bool "$cfg" fqdn "--dhcp-fqdn"
129 append_bool "$cfg" proxydnssec "--proxy-dnssec"
130 append_bool "$cfg" localservice "--local-service"
132 append_parm "$cfg" dhcpscript "--dhcp-script"
133 append_parm "$cfg" cachesize "--cache-size"
134 append_parm "$cfg" dnsforwardmax "--dns-forward-max"
135 append_parm "$cfg" port "--port"
136 append_parm "$cfg" ednspacket_max "--edns-packet-max"
137 append_parm "$cfg" dhcpleasemax "--dhcp-lease-max"
138 append_parm "$cfg" "queryport" "--query-port"
139 append_parm "$cfg" "domain" "--domain"
140 append_parm "$cfg" "local" "--server"
141 config_list_foreach "$cfg" "server" append_server
142 config_list_foreach "$cfg" "address" append_address
143 config_list_foreach "$cfg" "ipset" append_ipset
144 config_list_foreach "$cfg" "interface" append_interface
145 config_list_foreach "$cfg" "notinterface" append_notinterface
146 config_list_foreach "$cfg" "addnhosts" append_addnhosts
147 config_list_foreach "$cfg" "bogusnxdomain" append_bogusnxdomain
148 append_parm "$cfg" "leasefile" "--dhcp-leasefile"
149 append_parm "$cfg" "resolvfile" "--resolv-file"
150 append_parm "$cfg" "tftp_root" "--tftp-root"
151 append_parm "$cfg" "dhcp_boot" "--dhcp-boot"
152 append_parm "$cfg" "local_ttl" "--local-ttl"
154 config_get DOMAIN "$cfg" domain
156 config_get_bool ADD_LOCAL_DOMAIN "$cfg" add_local_domain 1
157 config_get_bool ADD_LOCAL_HOSTNAME "$cfg" add_local_hostname 1
159 config_get_bool readethers "$cfg" readethers
160 [ "$readethers" = "1" -a \! -e "/etc/ethers" ] && touch /etc/ethers
162 config_get leasefile $cfg leasefile
163 [ -n "$leasefile" -a \! -e "$leasefile" ] && touch "$leasefile"
164 config_get_bool cachelocal "$cfg" cachelocal 1
166 config_get hostsfile "$cfg" dhcphostsfile
167 [ -e "$hostsfile" ] && xappend "--dhcp-hostsfile=$hostsfile"
169 mkdir -p /tmp/hosts /tmp/dnsmasq.d
170 xappend "--addn-hosts=/tmp/hosts"
171 xappend "--conf-dir=/tmp/dnsmasq.d"
174 config_get_bool rebind "$cfg" rebind_protection 1
175 [ $rebind -gt 0 ] && {
177 "DNS rebinding protection is active," \
178 "will discard upstream RFC1918 responses!"
179 xappend "--stop-dns-rebind"
181 local rebind_localhost
182 config_get_bool rebind_localhost "$cfg" rebind_localhost 0
183 [ $rebind_localhost -gt 0 ] && {
184 log_once "Allowing 127.0.0.0/8 responses"
185 xappend "--rebind-localhost-ok"
188 append_rebind_domain() {
189 log_once "Allowing RFC1918 responses for domain $1"
190 xappend "--rebind-domain-ok=$1"
193 config_list_foreach "$cfg" rebind_domain append_rebind_domain
196 config_get_bool dnssec "$cfg" dnssec 0
197 [ "$dnssec" -gt 0 ] && {
198 xappend "--conf-file=$TRUSTANCHORSFILE"
200 append_bool "$cfg" dnsseccheckunsigned "--dnssec-check-unsigned"
203 dhcp_option_add "$cfg" "" 0
205 xappend "--dhcp-broadcast=tag:needs-broadcast"
210 dhcp_subscrid_add() {
213 config_get networkid "$cfg" networkid
214 [ -n "$networkid" ] || return 0
216 config_get subscriberid "$cfg" subscriberid
217 [ -n "$subscriberid" ] || return 0
219 xappend "--dhcp-subscrid=$networkid,$subscriberid"
221 config_get_bool force "$cfg" force 0
223 dhcp_option_add "$cfg" "$networkid" "$force"
226 dhcp_remoteid_add() {
229 config_get networkid "$cfg" networkid
230 [ -n "$networkid" ] || return 0
232 config_get remoteid "$cfg" remoteid
233 [ -n "$remoteid" ] || return 0
235 xappend "--dhcp-remoteid=$networkid,$remoteid"
237 config_get_bool force "$cfg" force 0
239 dhcp_option_add "$cfg" "$networkid" "$force"
242 dhcp_circuitid_add() {
245 config_get networkid "$cfg" networkid
246 [ -n "$networkid" ] || return 0
248 config_get circuitid "$cfg" circuitid
249 [ -n "$circuitid" ] || return 0
251 xappend "--dhcp-circuitid=$networkid,$circuitid"
253 config_get_bool force "$cfg" force 0
255 dhcp_option_add "$cfg" "$networkid" "$force"
258 dhcp_userclass_add() {
261 config_get networkid "$cfg" networkid
262 [ -n "$networkid" ] || return 0
264 config_get userclass "$cfg" userclass
265 [ -n "$userclass" ] || return 0
267 xappend "--dhcp-userclass=$networkid,$userclass"
269 config_get_bool force "$cfg" force 0
271 dhcp_option_add "$cfg" "$networkid" "$force"
274 dhcp_vendorclass_add() {
277 config_get networkid "$cfg" networkid
278 [ -n "$networkid" ] || return 0
280 config_get vendorclass "$cfg" vendorclass
281 [ -n "$vendorclass" ] || return 0
283 xappend "--dhcp-vendorclass=$networkid,$vendorclass"
285 config_get_bool force "$cfg" force 0
287 dhcp_option_add "$cfg" "$networkid" "$force"
293 config_get_bool force "$cfg" force 0
295 config_get networkid "$cfg" networkid
296 [ -n "$networkid" ] && dhcp_option_add "$cfg" "$networkid" "$force"
298 config_get name "$cfg" name
299 config_get ip "$cfg" ip
300 [ -n "$ip" -o -n "$name" ] || return 0
302 config_get_bool dns "$cfg" dns 0
303 [ "$dns" = "1" -a -n "$ip" -a -n "$name" ] && {
304 echo "$ip $name${DOMAIN:+.$DOMAIN}" >> $HOSTFILE
307 config_get mac "$cfg" mac
308 if [ -n "$mac" ]; then
309 # --dhcp-host=00:20:e0:3b:13:af,192.168.0.199,lap
311 for m in $mac; do append macs "$m" ","; done
313 # --dhcp-host=lap,192.168.0.199
314 [ -n "$name" ] || return 0
319 config_get tag "$cfg" tag
321 config_get_bool broadcast "$cfg" broadcast 0
322 [ "$broadcast" = "0" ] && broadcast=
324 xappend "--dhcp-host=$macs${networkid:+,net:$networkid}${broadcast:+,set:needs-broadcast}${tag:+,set:$tag}${ip:+,$ip}${name:+,$name}"
332 [ -n "$tag" ] || return 0
334 config_get_bool force "$cfg" force 0
335 [ "$force" = "0" ] && force=
337 config_get option "$cfg" dhcp_option
339 xappend "--dhcp-option${force:+-force}=tag:$tag,$o"
346 config_get networkid "$cfg" networkid
347 [ -n "$networkid" ] || return 0
349 config_get mac "$cfg" mac
350 [ -n "$mac" ] || return 0
352 xappend "--dhcp-mac=$networkid,$mac"
354 dhcp_option_add "$cfg" "$networkid"
360 config_get networkid "$cfg" networkid
362 config_get filename "$cfg" filename
363 [ -n "$filename" ] || return 0
365 config_get servername "$cfg" servername
366 [ -n "$servername" ] || return 0
368 config_get serveraddress "$cfg" serveraddress
369 [ -n "$serveraddress" ] || return 0
371 xappend "--dhcp-boot=${networkid:+net:$networkid,}$filename,$servername,$serveraddress"
373 config_get_bool force "$cfg" force 0
375 dhcp_option_add "$cfg" "$networkid" "$force"
381 config_get net "$cfg" interface
382 [ -n "$net" ] || return 0
384 config_get dhcpv4 "$cfg" dhcpv4
385 [ "$dhcpv4" != "disabled" ] || return 0
387 config_get networkid "$cfg" networkid
388 [ -n "$networkid" ] || networkid="$net"
390 network_get_subnet subnet "$net" || return 0
391 network_get_device ifname "$net" || return 0
392 network_get_protocol proto "$net" || return 0
394 [ "$cachelocal" = "0" ] && network_get_dnsserver dnsserver "$net" && {
395 DNS_SERVERS="$DNS_SERVERS $dnsserver"
398 append_bool "$cfg" ignore "--no-dhcp-interface=$ifname" && return 0
400 # Do not support non-static interfaces for now
401 [ static = "$proto" ] || return 0
403 # Override interface netmask with dhcp config if applicable
404 config_get netmask "$cfg" netmask "${subnet##*/}"
406 #check for an already active dhcp server on the interface, unless 'force' is set
407 config_get_bool force "$cfg" force 0
408 [ $force -gt 0 ] || dhcp_check "$ifname" || return 0
410 config_get start "$cfg" start
411 config_get limit "$cfg" limit
412 config_get leasetime "$cfg" leasetime
413 config_get options "$cfg" options
414 config_get_bool dynamicdhcp "$cfg" dynamicdhcp 1
416 leasetime="${leasetime:-12h}"
417 start="$(dhcp_calc "${start:-100}")"
418 limit="${limit:-150}"
419 [ "$limit" -gt 0 ] && limit=$((limit-1))
420 eval "$(ipcalc.sh "${subnet%%/*}" $netmask $start $limit)"
421 if [ "$dynamicdhcp" = "0" ]; then END="static"; fi
422 xappend "--dhcp-range=$networkid,$START,$END,$NETMASK,$leasetime${options:+ $options}"
424 dhcp_option_add "$cfg" "$networkid"
432 [ "$force" = "0" ] && force=
434 config_get dhcp_option "$cfg" dhcp_option
435 for o in $dhcp_option; do
436 xappend "--dhcp-option${force:+-force}=${networkid:+$networkid,}$o"
443 local ip name names record
445 config_get names "$cfg" name "$2"
446 [ -n "$names" ] || return 0
448 config_get ip "$cfg" ip "$3"
449 [ -n "$ip" ] || return 0
451 for name in $names; do
452 record="${record:+$record }$name"
455 echo "$ip $record" >> $HOSTFILE
461 config_get srv "$cfg" srv
462 [ -n "$srv" ] || return 0
464 config_get target "$cfg" target
465 [ -n "$target" ] || return 0
467 config_get port "$cfg" port
468 [ -n "$port" ] || return 0
470 config_get class "$cfg" class
471 config_get weight "$cfg" weight
473 local service="$srv,$target,$port${class:+,$class${weight:+,$weight}}"
475 xappend "--srv-host=$service"
480 local domain relay pref
482 config_get domain "$cfg" domain
483 [ -n "$domain" ] || return 0
485 config_get relay "$cfg" relay
486 [ -n "$relay" ] || return 0
488 config_get pref "$cfg" pref 0
490 local service="$domain,$relay,$pref"
492 xappend "--mx-host=$service"
499 config_get cname "$cfg" cname
500 [ -n "$cname" ] || return 0
502 config_get target "$cfg" target
503 [ -n "$target" ] || return 0
505 xappend "--cname=${cname},${target}"
508 dhcp_hostrecord_add() {
510 local names addresses record val
512 config_get names "$cfg" name "$2"
513 if [ -z "$names" ]; then
517 config_get addresses "$cfg" ip "$3"
518 if [ -z "$addresses" ]; then
522 for val in $names $addresses; do
523 record="${record:+$record,}$val"
526 xappend "--host-record=$record"
531 procd_add_reload_trigger "dhcp"
535 # Will be launched through hotplug
540 include /lib/functions
545 procd_set_param command $PROG -C $CONFIGFILE -k
546 procd_set_param file $CONFIGFILE
547 procd_set_param respawn
550 # before we can call xappend
551 mkdir -p $(dirname $CONFIGFILE)
553 echo "# auto-generated config file from /etc/config/dhcp" > $CONFIGFILE
554 echo "# auto-generated config file from /etc/config/dhcp" > $HOSTFILE
556 # if we did this last, we could override auto-generated config
557 [ -f /etc/dnsmasq.conf ] && {
558 xappend "--conf-file=/etc/dnsmasq.conf"
562 config_foreach dnsmasq dnsmasq
563 config_foreach dhcp_host_add host
565 config_foreach dhcp_boot_add boot
566 config_foreach dhcp_mac_add mac
567 config_foreach dhcp_tag_add tag
568 config_foreach dhcp_vendorclass_add vendorclass
569 config_foreach dhcp_userclass_add userclass
570 config_foreach dhcp_circuitid_add circuitid
571 config_foreach dhcp_remoteid_add remoteid
572 config_foreach dhcp_subscrid_add subscrid
573 config_foreach dhcp_domain_add domain
574 config_foreach dhcp_hostrecord_add hostrecord
578 [ $ADD_LOCAL_HOSTNAME -eq 1 ] && network_get_ipaddr lanaddr "lan" && {
579 local hostname="$(uci_get system @system[0] hostname OpenWrt)"
580 dhcp_domain_add "" "$hostname" "$lanaddr"
584 config_foreach dhcp_srv_add srvhost
585 config_foreach dhcp_mx_add mxhost
588 config_get odhcpd_is_active odhcpd maindhcp
589 if [ "$odhcpd_is_active" != "1" ]; then
590 config_foreach dhcp_add dhcp
594 config_foreach dhcp_cname_add cname
597 rm -f /tmp/resolv.conf
598 [ $ADD_LOCAL_DOMAIN -eq 1 ] && [ -n "$DOMAIN" ] && {
599 echo "search $DOMAIN" >> /tmp/resolv.conf
601 DNS_SERVERS="$DNS_SERVERS 127.0.0.1"
602 for DNS_SERVER in $DNS_SERVERS ; do
603 echo "nameserver $DNS_SERVER" >> /tmp/resolv.conf
608 rc_procd start_service "$@"
613 [ -f /tmp/resolv.conf ] && {
614 rm -f /tmp/resolv.conf
615 ln -s /tmp/resolv.conf.auto /tmp/resolv.conf
617 rm -f /var/run/dnsmasq.*.dhcp