1 #!/bin/sh /etc/rc.common
2 # Copyright (C) 2007-2012 OpenWrt.org
14 BASECONFIGFILE="/var/etc/dnsmasq.conf"
15 BASEHOSTFILE="/tmp/hosts/dhcp"
16 BASETIMESTAMPFILE="/etc/dnsmasq.time"
17 TRUSTANCHORSFILE="/usr/share/dnsmasq/trust-anchors.conf"
18 TIMEVALIDFILE="/var/state/dnsmasqsec"
19 BASEDHCPSTAMPFILE="/var/run/dnsmasq"
26 echo "${value#--}" >> $CONFIGFILE_TMP
31 local hex="${2#0x}" # strip optional "0x" prefix
33 if [ -n "${hex//[0-9a-fA-F]/}" ]; then
34 # is invalid hex literal
38 # convert into host id
41 $(((0x$hex >> 16) % 65536)) \
52 while [ -n "$ip" ]; do
55 res="$(($res + $part))"
56 [ "${ip%.*}" != "$ip" ] && ip="${ip#*.}" || ip=
63 local stamp="${BASEDHCPSTAMPFILE_CFG}.${ifname}.dhcp"
66 [ -s "$stamp" ] && return $(cat "$stamp")
68 # If there's no carrier yet, skip this interface.
69 # The init script will be called again once the link is up
70 case "$(devstatus "$ifname" | jsonfilter -e @.carrier)" in
74 udhcpc -n -q -s /bin/true -t 1 -i "$ifname" >&- && rv=1 || rv=0
78 "found already running DHCP-server on interface '$ifname'" \
79 "refusing to start, use 'option force 1' to override"
86 pidof dnsmasq >/dev/null || \
87 logger -t dnsmasq "$@"
95 config_get_bool _loctmp "$section" "$option" 0
96 [ $_loctmp -gt 0 ] && xappend "$value"
105 config_get _loctmp "$section" "$option" "$default"
106 [ -z "$_loctmp" ] && return 0
107 xappend "$switch=$_loctmp"
111 xappend "--server=$1"
115 xappend "--address=$1"
123 network_get_device ifname "$1" || return
124 xappend "--interface=$ifname"
127 append_notinterface() {
128 network_get_device ifname "$1" || return
129 xappend "--except-interface=$ifname"
133 xappend "--addn-hosts=$1"
136 append_bogusnxdomain() {
137 xappend "--bogus-nxdomain=$1"
140 append_pxe_service() {
141 xappend "--pxe-service=$1"
145 local cfg="$1" func="$2" match_cfg="$3" found_cfg
147 # use entry when no instance entry set, or if it matches
148 config_get found_cfg "$cfg" "instance"
149 if [ -z "$found_cfg" -o "$found_cfg" = "$match_cfg" ]; then
154 dhcp_subscrid_add() {
157 config_get networkid "$cfg" networkid
158 [ -n "$networkid" ] || return 0
160 config_get subscriberid "$cfg" subscriberid
161 [ -n "$subscriberid" ] || return 0
163 xappend "--dhcp-subscrid=$networkid,$subscriberid"
165 config_get_bool force "$cfg" force 0
167 dhcp_option_add "$cfg" "$networkid" "$force"
170 dhcp_remoteid_add() {
173 config_get networkid "$cfg" networkid
174 [ -n "$networkid" ] || return 0
176 config_get remoteid "$cfg" remoteid
177 [ -n "$remoteid" ] || return 0
179 xappend "--dhcp-remoteid=$networkid,$remoteid"
181 config_get_bool force "$cfg" force 0
183 dhcp_option_add "$cfg" "$networkid" "$force"
186 dhcp_circuitid_add() {
187 # TODO: DHCPV6 does not have circuitid; catch "option6:"
190 config_get networkid "$cfg" networkid
191 [ -n "$networkid" ] || return 0
193 config_get circuitid "$cfg" circuitid
194 [ -n "$circuitid" ] || return 0
196 xappend "--dhcp-circuitid=$networkid,$circuitid"
198 config_get_bool force "$cfg" force 0
200 dhcp_option_add "$cfg" "$networkid" "$force"
203 dhcp_userclass_add() {
206 config_get networkid "$cfg" networkid
207 [ -n "$networkid" ] || return 0
209 config_get userclass "$cfg" userclass
210 [ -n "$userclass" ] || return 0
212 xappend "--dhcp-userclass=$networkid,$userclass"
214 config_get_bool force "$cfg" force 0
216 dhcp_option_add "$cfg" "$networkid" "$force"
219 dhcp_vendorclass_add() {
220 # TODO: DHCPV6 vendor class has stricter definitions; catch? fixup?
223 config_get networkid "$cfg" networkid
224 [ -n "$networkid" ] || return 0
226 config_get vendorclass "$cfg" vendorclass
227 [ -n "$vendorclass" ] || return 0
229 xappend "--dhcp-vendorclass=$networkid,$vendorclass"
231 config_get_bool force "$cfg" force 0
233 dhcp_option_add "$cfg" "$networkid" "$force"
239 config_get networkid "$cfg" networkid
240 [ -n "$networkid" ] || return 0
242 config_get match "$cfg" match
243 [ -n "$match" ] || return 0
245 xappend "--dhcp-match=$networkid,$match"
247 config_get_bool force "$cfg" force 0
249 dhcp_option_add "$cfg" "$networkid" "$force"
254 local hosttag nametime addrs
256 config_get_bool force "$cfg" force 0
258 config_get networkid "$cfg" networkid
259 [ -n "$networkid" ] && dhcp_option_add "$cfg" "$networkid" "$force"
261 config_get_bool enable "$cfg" enable 1
262 [ "$enable" = "0" ] && return 0
264 config_get name "$cfg" name
265 config_get ip "$cfg" ip
266 config_get hostid "$cfg" hostid
268 [ -n "$ip" -o -n "$name" -o -n "$hostid" ] || return 0
270 config_get_bool dns "$cfg" dns 0
271 [ "$dns" = "1" -a -n "$ip" -a -n "$name" ] && {
272 echo "$ip $name${DOMAIN:+.$DOMAIN}" >> $HOSTFILE
275 config_get mac "$cfg" mac
276 config_get duid "$cfg" duid
277 config_get tag "$cfg" tag
279 if [ -n "$mac" ]; then
280 # --dhcp-host=00:20:e0:3b:13:af,192.168.0.199,lap
281 # many MAC are possible to track a laptop ON/OFF dock
283 for m in $mac; do append macs "$m" ","; done
286 if [ $DNSMASQ_DHCP_VER -eq 6 -a -n "$duid" ]; then
287 # --dhcp-host=id:00:03:00:01:12:00:00:01:02:03,[::beef],lap
288 # one (virtual) machine gets one DUID per RFC3315
289 duids="id:${duid// */}"
292 if [ -z "$macs" -a -z "$duids" ]; then
293 # --dhcp-host=lap,192.168.0.199,[::beef]
294 [ -n "$name" ] || return 0
299 if [ -n "$hostid" ]; then
300 hex_to_hostid hostid "$hostid"
303 config_get_bool broadcast "$cfg" broadcast 0
304 config_get leasetime "$cfg" leasetime
306 [ "$broadcast" = "0" ] && broadcast= || broadcast=",set:needs-broadcast"
308 hosttag="${networkid:+,set:${networkid}}${tag:+,set:${tag}}$broadcast"
309 nametime="${name:+,$name}${leasetime:+,$leasetime}"
311 if [ $DNSMASQ_DHCP_VER -eq 6 ]; then
312 addrs="${ip:+,$ip}${hostid:+,[::$hostid]}"
313 xappend "--dhcp-host=$macs${duids:+,$duids}$hosttag$addrs$nametime"
315 xappend "--dhcp-host=$macs$hosttag${ip:+,$ip}$nametime"
319 dhcp_this_host_add() {
323 local routerstub routername ifdashname
324 local lanaddr lanaddr6 lanaddrs6 ulaprefix
326 if [ "$mode" -gt 0 ] ; then
327 ifdashname="${ifname//./-}"
328 routerstub="$( md5sum /etc/os-release )"
329 routerstub="router-${routerstub// */}"
330 routername="$( uci_get system @system[0] hostname $routerstub )"
332 if [ "$mode" -gt 1 ] ; then
333 if [ "$mode" -gt 2 ] ; then
334 if [ "$mode" -gt 3 ] ; then
335 xappend "--interface-name=$ifdashname.$routername.$DOMAIN,$ifname"
338 xappend "--interface-name=$routername.$DOMAIN,$ifname"
341 # All IP addresses discovered by dnsmasq will be labeled (except fe80::)
342 xappend "--interface-name=$routername,$ifname"
345 # This uses a static host file entry for only limited addresses.
346 # Use dnsmasq option "--expandhosts" to enable FQDN on host files.
347 ulaprefix="$(uci_get network @globals[0] ula_prefix)"
348 network_get_ipaddr lanaddr "$net"
349 network_get_ipaddrs6 lanaddrs6 "$net"
351 if [ -n "$lanaddr" ] ; then
352 dhcp_domain_add "" "$routername" "$lanaddr"
355 if [ -n "$ulaprefix" -a -n "$lanaddrs6" ] ; then
356 for lanaddr6 in $lanaddrs6 ; do
358 "${ulaprefix%%:/*}"*)
359 dhcp_domain_add "" "$routername" "$lanaddr6"
369 # NOTE: dnsmasq has explicit "option6:" prefix for DHCPv6 so no collisions
374 [ -n "$tag" ] || return 0
376 config_get_bool force "$cfg" force 0
377 [ "$force" = "0" ] && force=
379 config_get option "$cfg" dhcp_option
381 xappend "--dhcp-option${force:+-force}=tag:$tag,$o"
388 config_get networkid "$cfg" networkid
389 [ -n "$networkid" ] || return 0
391 config_get mac "$cfg" mac
392 [ -n "$mac" ] || return 0
394 xappend "--dhcp-mac=$networkid,$mac"
396 dhcp_option_add "$cfg" "$networkid"
400 # TODO: BOOTURL is different between DHCPv4 and DHCPv6
403 config_get networkid "$cfg" networkid
405 config_get filename "$cfg" filename
406 [ -n "$filename" ] || return 0
408 config_get servername "$cfg" servername
409 config_get serveraddress "$cfg" serveraddress
411 [ -n "$serveraddress" -a ! -n "$servername" ] && return 0
413 xappend "--dhcp-boot=${networkid:+net:$networkid,}${filename}${servername:+,$servername}${serveraddress:+,$serveraddress}"
415 config_get_bool force "$cfg" force 0
417 dhcp_option_add "$cfg" "$networkid" "$force"
423 local dhcp6range="::"
426 config_get net "$cfg" interface
427 [ -n "$net" ] || return 0
429 config_get networkid "$cfg" networkid
430 [ -n "$networkid" ] || networkid="$net"
432 network_get_subnet subnet "$net" || return 0
433 network_get_device ifname "$net" || return 0
434 network_get_protocol proto "$net" || return 0
436 [ "$cachelocal" = "0" ] && network_get_dnsserver dnsserver "$net" && {
437 DNS_SERVERS="$DNS_SERVERS $dnsserver"
440 append_bool "$cfg" ignore "--no-dhcp-interface=$ifname" && {
441 # Many ISP do not have useful names for DHCP customers (your WAN).
442 dhcp_this_host_add "$net" "$ifname" "$ADD_WAN_FQDN"
446 # Do not support non-static interfaces for now
447 [ static = "$proto" ] || return 0
449 # Override interface netmask with dhcp config if applicable
450 config_get netmask "$cfg" netmask "${subnet##*/}"
452 #check for an already active dhcp server on the interface, unless 'force' is set
453 config_get_bool force "$cfg" force 0
454 [ $force -gt 0 ] || dhcp_check "$ifname" || return 0
456 config_get start "$cfg" start 100
457 config_get limit "$cfg" limit 150
458 config_get leasetime "$cfg" leasetime 12h
459 config_get options "$cfg" options
460 config_get_bool dynamicdhcp "$cfg" dynamicdhcp 1
462 config_get dhcpv4 "$cfg" dhcpv4
463 config_get dhcpv6 "$cfg" dhcpv6
465 config_get ra "$cfg" ra
466 config_get ra_management "$cfg" ra_management
467 config_get ra_preference "$cfg" ra_preference
469 # Put the router host name on this DHCP served interface address(es)
470 dhcp_this_host_add "$net" "$ifname" "$ADD_LOCAL_FQDN"
472 start="$( dhcp_calc "$start" )"
473 nettag="${networkid:+set:${networkid},}"
475 if [ "$limit" -gt 0 ] ; then
479 eval "$(ipcalc.sh "${subnet%%/*}" $netmask $start $limit)"
481 if [ "$dynamicdhcp" = "0" ] ; then
483 dhcp6range="::,static"
485 dhcp6range="::1000,::ffff"
489 if [ "$dhcpv4" != "disabled" ] ; then
490 xappend "--dhcp-range=$nettag$START,$END,$NETMASK,$leasetime${options:+ $options}"
494 if [ $DNSMASQ_DHCP_VER -eq 6 -a "$ra" = "server" ] ; then
495 # Note: dnsmasq cannot just be a DHCPv6 server (all-in-1)
496 # and let some other machine(s) send RA pointing to it.
498 case $ra_preference in
500 xappend "--ra-param=$ifname,high,0,7200"
503 xappend "--ra-param=$ifname,low,0,7200"
506 # Send UNSOLICITED RA at default interval and live for 2 hours.
507 # TODO: convert flexible lease time into route life time (only seconds).
508 xappend "--ra-param=$ifname,0,7200"
512 if [ "$dhcpv6" = "disabled" ] ; then
517 case $ra_management in
519 # SLACC with DCHP for extended options
520 xappend "--dhcp-range=$nettag::,constructor:$ifname,ra-stateless,ra-names"
523 # DHCP address and RA only for management redirection
524 xappend "--dhcp-range=$nettag$dhcp6range,constructor:$ifname,$leasetime"
527 # SLAAC only but dnsmasq attempts to link HOSTNAME, DHCPv4 MAC, and SLAAC
528 xappend "--dhcp-range=$nettag::,constructor:$ifname,ra-only,ra-names"
531 # SLAAC and full DHCP
532 xappend "--dhcp-range=$nettag$dhcp6range,constructor:$ifname,slaac,ra-names,$leasetime"
537 dhcp_option_add "$cfg" "$networkid"
541 # NOTE: dnsmasq has explicit "option6:" prefix for DHCPv6 so no collisions
546 [ "$force" = "0" ] && force=
548 config_get dhcp_option "$cfg" dhcp_option
549 for o in $dhcp_option; do
550 xappend "--dhcp-option${force:+-force}=${networkid:+$networkid,}$o"
557 local ip name names record
559 config_get names "$cfg" name "$2"
560 [ -n "$names" ] || return 0
562 config_get ip "$cfg" ip "$3"
563 [ -n "$ip" ] || return 0
565 for name in $names; do
566 record="${record:+$record }$name"
569 echo "$ip $record" >> $HOSTFILE
575 config_get srv "$cfg" srv
576 [ -n "$srv" ] || return 0
578 config_get target "$cfg" target
579 [ -n "$target" ] || return 0
581 config_get port "$cfg" port
582 [ -n "$port" ] || return 0
584 config_get class "$cfg" class
585 config_get weight "$cfg" weight
587 local service="$srv,$target,$port${class:+,$class${weight:+,$weight}}"
589 xappend "--srv-host=$service"
594 local domain relay pref
596 config_get domain "$cfg" domain
597 [ -n "$domain" ] || return 0
599 config_get relay "$cfg" relay
600 [ -n "$relay" ] || return 0
602 config_get pref "$cfg" pref 0
604 local service="$domain,$relay,$pref"
606 xappend "--mx-host=$service"
613 config_get cname "$cfg" cname
614 [ -n "$cname" ] || return 0
616 config_get target "$cfg" target
617 [ -n "$target" ] || return 0
619 xappend "--cname=${cname},${target}"
622 dhcp_hostrecord_add() {
624 local names addresses record val
626 config_get names "$cfg" name "$2"
627 if [ -z "$names" ]; then
631 config_get addresses "$cfg" ip "$3"
632 if [ -z "$addresses" ]; then
636 for val in $names $addresses; do
637 record="${record:+$record,}$val"
640 xappend "--host-record=$record"
645 local local_addr server_addr interface
647 config_get local_addr "$cfg" local_addr
648 [ -n "$local_addr" ] || return 0
650 config_get server_addr "$cfg" server_addr
651 [ -n "$server_addr" ] || return 0
653 config_get interface "$cfg" interface
654 if [ -z "$interface" ]; then
655 xappend "--dhcp-relay=$local_addr,$server_addr"
657 xappend "--dhcp-relay=$local_addr,$server_addr,$interface"
663 local cfg="$1" disabled
665 config_get_bool disabled "$cfg" disabled 0
666 [ "$disabled" -gt 0 ] && return 0
668 # reset list of DOMAINS and DNS servers (for each dnsmasq instance)
671 CONFIGFILE="${BASECONFIGFILE}.${cfg}"
672 CONFIGFILE_TMP="${CONFIGFILE}.$$"
673 HOSTFILE="${BASEHOSTFILE}.${cfg}"
674 TIMESTAMPFILE="${BASETIMESTAMPFILE}.${cfg}"
675 BASEDHCPSTAMPFILE_CFG="${BASEDHCPSTAMPFILE}.${cfg}"
677 # before we can call xappend
678 mkdir -p /var/run/dnsmasq/
679 mkdir -p $(dirname $CONFIGFILE)
680 mkdir -p $(dirname $HOSTFILE)
681 mkdir -p /var/lib/misc
682 chown dnsmasq:dnsmasq /var/run/dnsmasq
684 [ -f "$TIMESTAMPFILE" ] && rm -f "$TIMESTAMPFILE"
686 echo "# auto-generated config file from /etc/config/dhcp" > $CONFIGFILE_TMP
687 echo "# auto-generated config file from /etc/config/dhcp" > $HOSTFILE
689 # if we did this last, we could override auto-generated config
690 [ -f /etc/dnsmasq.conf ] && {
691 xappend "--conf-file=/etc/dnsmasq.conf"
694 $PROG --version | grep -osqE "^Compile time options:.* DHCPv6( |$)" && DHCPv6CAPABLE=1 || DHCPv6CAPABLE=0
697 if [ -x /usr/sbin/odhcpd -a -x /etc/init.d/odhcpd ] ; then
698 local odhcpd_is_main odhcpd_is_enabled
699 config_get odhcpd_is_main odhcpd maindhcp
700 /etc/init.d/odhcpd enabled && odhcpd_is_enabled=1 || odhcpd_is_enabled=0
703 if [ "$odhcpd_is_enabled" -eq 0 -a "$DHCPv6CAPABLE" -eq 1 ] ; then
704 # DHCP V4 and V6 in DNSMASQ
706 elif [ "$odhcpd_is_main" -gt 0 ] ; then
707 # ODHCPD is doing it all
710 # You have ODHCPD but use DNSMASQ for DHCPV4
714 elif [ "$DHCPv6CAPABLE" -eq 1 ] ; then
715 # DHCP V4 and V6 in DNSMASQ
722 append_bool "$cfg" authoritative "--dhcp-authoritative"
723 append_bool "$cfg" nodaemon "--no-daemon"
724 append_bool "$cfg" domainneeded "--domain-needed"
725 append_bool "$cfg" filterwin2k "--filterwin2k"
726 append_bool "$cfg" nohosts "--no-hosts"
727 append_bool "$cfg" nonegcache "--no-negcache"
728 append_bool "$cfg" strictorder "--strict-order"
729 append_bool "$cfg" logqueries "--log-queries=extra"
730 append_bool "$cfg" noresolv "--no-resolv"
731 append_bool "$cfg" localise_queries "--localise-queries"
732 append_bool "$cfg" readethers "--read-ethers"
733 append_bool "$cfg" dbus "--enable-dbus"
734 append_bool "$cfg" boguspriv "--bogus-priv"
735 append_bool "$cfg" expandhosts "--expand-hosts"
736 config_get tftp_root "$cfg" "tftp_root"
737 [ -d "$tftp_root" ] && append_bool "$cfg" enable_tftp "--enable-tftp"
738 append_bool "$cfg" tftp_no_fail "--tftp-no-fail"
739 append_bool "$cfg" nonwildcard "--bind-dynamic"
740 append_bool "$cfg" fqdn "--dhcp-fqdn"
741 append_bool "$cfg" proxydnssec "--proxy-dnssec"
742 append_bool "$cfg" localservice "--local-service"
743 append_bool "$cfg" logdhcp "--log-dhcp"
744 append_bool "$cfg" quietdhcp "--quiet-dhcp"
745 append_bool "$cfg" sequential_ip "--dhcp-sequential-ip"
746 append_bool "$cfg" allservers "--all-servers"
747 append_bool "$cfg" noping "--no-ping"
749 append_parm "$cfg" logfacility "--log-facility"
750 append_parm "$cfg" dhcpscript "--dhcp-script"
751 append_parm "$cfg" cachesize "--cache-size"
752 append_parm "$cfg" dnsforwardmax "--dns-forward-max"
753 append_parm "$cfg" port "--port"
754 append_parm "$cfg" ednspacket_max "--edns-packet-max"
755 append_parm "$cfg" dhcpleasemax "--dhcp-lease-max"
756 append_parm "$cfg" "queryport" "--query-port"
757 append_parm "$cfg" "minport" "--min-port"
758 append_parm "$cfg" "maxport" "--max-port"
759 append_parm "$cfg" "domain" "--domain"
760 append_parm "$cfg" "local" "--server"
761 config_list_foreach "$cfg" "server" append_server
762 config_list_foreach "$cfg" "address" append_address
763 config_list_foreach "$cfg" "ipset" append_ipset
764 config_list_foreach "$cfg" "interface" append_interface
765 config_list_foreach "$cfg" "notinterface" append_notinterface
766 config_list_foreach "$cfg" "addnhosts" append_addnhosts
767 config_list_foreach "$cfg" "bogusnxdomain" append_bogusnxdomain
768 append_parm "$cfg" "leasefile" "--dhcp-leasefile" "/tmp/dhcp.leases"
769 append_parm "$cfg" "resolvfile" "--resolv-file" "/tmp/resolv.conf.auto"
770 append_parm "$cfg" "serversfile" "--servers-file"
771 append_parm "$cfg" "tftp_root" "--tftp-root"
772 append_parm "$cfg" "dhcp_boot" "--dhcp-boot"
773 append_parm "$cfg" "local_ttl" "--local-ttl"
774 append_parm "$cfg" "pxe_prompt" "--pxe-prompt"
775 config_list_foreach "$cfg" "pxe_service" append_pxe_service
776 config_get DOMAIN "$cfg" domain
778 config_get_bool ADD_LOCAL_DOMAIN "$cfg" add_local_domain 1
779 config_get_bool ADD_LOCAL_HOSTNAME "$cfg" add_local_hostname 1
780 config_get ADD_LOCAL_FQDN "$cfg" add_local_fqdn ""
781 config_get ADD_WAN_FQDN "$cfg" add_wan_fqdn 0
783 if [ -z "$ADD_LOCAL_FQDN" ] ; then
784 # maintain support for previous UCI
785 ADD_LOCAL_FQDN="$ADD_LOCAL_HOSTNAME"
788 config_get_bool readethers "$cfg" readethers
789 [ "$readethers" = "1" -a \! -e "/etc/ethers" ] && touch /etc/ethers
791 config_get resolvfile $cfg resolvfile
792 config_get dhcpscript $cfg dhcpscript
794 config_get leasefile $cfg leasefile "/tmp/dhcp.leases"
795 [ -n "$leasefile" -a \! -e "$leasefile" ] && touch "$leasefile"
796 config_get_bool cachelocal "$cfg" cachelocal 1
798 config_get_bool noresolv "$cfg" noresolv 0
799 if [ "$noresolv" != "1" ]; then
800 config_get resolvfile "$cfg" resolvfile "/tmp/resolv.conf.auto"
801 # So jail doesn't complain if file missing
802 [ -n "$resolvfile" -a \! -e "$resolvfile" ] && touch "$resolvfile"
805 config_get hostsfile "$cfg" dhcphostsfile
806 [ -e "$hostsfile" ] && xappend "--dhcp-hostsfile=$hostsfile"
809 config_get_bool rebind "$cfg" rebind_protection 1
810 [ $rebind -gt 0 ] && {
812 "DNS rebinding protection is active," \
813 "will discard upstream RFC1918 responses!"
814 xappend "--stop-dns-rebind"
816 local rebind_localhost
817 config_get_bool rebind_localhost "$cfg" rebind_localhost 0
818 [ $rebind_localhost -gt 0 ] && {
819 log_once "Allowing 127.0.0.0/8 responses"
820 xappend "--rebind-localhost-ok"
823 append_rebind_domain() {
824 log_once "Allowing RFC1918 responses for domain $1"
825 xappend "--rebind-domain-ok=$1"
828 config_list_foreach "$cfg" rebind_domain append_rebind_domain
831 config_get_bool dnssec "$cfg" dnssec 0
832 [ "$dnssec" -gt 0 ] && {
833 xappend "--conf-file=$TRUSTANCHORSFILE"
835 [ -x /etc/init.d/sysntpd ] && {
836 /etc/init.d/sysntpd enabled
837 [ "$?" -ne 0 -o "$(uci_get system.ntp.enabled)" = "1" ] && {
838 [ -f "$TIMEVALIDFILE" ] || xappend "--dnssec-no-timecheck"
841 append_bool "$cfg" dnsseccheckunsigned "--dnssec-check-unsigned"
844 config_get addmac "$cfg" addmac 0
845 [ "$addmac" != "0" ] && {
846 [ "$addmac" = "1" ] && addmac=
847 xappend "--add-mac${addmac:+="$addmac"}"
850 dhcp_option_add "$cfg" "" 0
852 xappend "--dhcp-broadcast=tag:needs-broadcast"
854 xappend "--addn-hosts=$(dirname $HOSTFILE)"
856 config_get dnsmasqconfdir "$cfg" confdir "/tmp/dnsmasq.d"
857 [ ! -d "$dnsmasqconfdir" ] && mkdir -p $dnsmasqconfdir
858 xappend "--conf-dir=$dnsmasqconfdir"
859 xappend "--user=dnsmasq"
860 xappend "--group=dnsmasq"
861 echo >> $CONFIGFILE_TMP
863 config_get_bool enable_tftp "$cfg" enable_tftp 0
864 [ "$enable_tftp" -gt 0 ] && {
865 config_get tftp_root "$cfg" tftp_root
866 append EXTRA_MOUNT $tftp_root
869 config_foreach filter_dnsmasq host dhcp_host_add "$cfg"
870 echo >> $CONFIGFILE_TMP
871 config_foreach filter_dnsmasq boot dhcp_boot_add "$cfg"
872 config_foreach filter_dnsmasq mac dhcp_mac_add "$cfg"
873 config_foreach filter_dnsmasq tag dhcp_tag_add "$cfg"
874 config_foreach filter_dnsmasq vendorclass dhcp_vendorclass_add "$cfg"
875 config_foreach filter_dnsmasq userclass dhcp_userclass_add "$cfg"
876 config_foreach filter_dnsmasq circuitid dhcp_circuitid_add "$cfg"
877 config_foreach filter_dnsmasq remoteid dhcp_remoteid_add "$cfg"
878 config_foreach filter_dnsmasq subscrid dhcp_subscrid_add "$cfg"
879 config_foreach filter_dnsmasq match dhcp_match_add "$cfg"
880 config_foreach filter_dnsmasq domain dhcp_domain_add "$cfg"
881 config_foreach filter_dnsmasq hostrecord dhcp_hostrecord_add "$cfg"
882 config_foreach filter_dnsmasq relay dhcp_relay_add "$cfg"
884 echo >> $CONFIGFILE_TMP
885 config_foreach filter_dnsmasq srvhost dhcp_srv_add "$cfg"
886 config_foreach filter_dnsmasq mxhost dhcp_mx_add "$cfg"
887 echo >> $CONFIGFILE_TMP
890 if [ "$DNSMASQ_DHCP_VER" -gt 4 ] ; then
891 # Enable RA feature for when/if it is constructed,
892 # and RA is selected per interface pool (RA, DHCP, or both),
893 # but no one (should) want RA broadcast in syslog
894 config_foreach dhcp_add dhcp
895 xappend "--enable-ra"
898 elif [ "$DNSMASQ_DHCP_VER" -gt 0 ] ; then
899 config_foreach dhcp_add dhcp
903 echo >> $CONFIGFILE_TMP
904 config_foreach filter_dnsmasq cname dhcp_cname_add "$cfg"
905 echo >> $CONFIGFILE_TMP
907 echo >> $CONFIGFILE_TMP
908 mv -f $CONFIGFILE_TMP $CONFIGFILE
910 [ "$resolvfile" = "/tmp/resolv.conf.auto" ] && {
911 rm -f /tmp/resolv.conf
912 [ $ADD_LOCAL_DOMAIN -eq 1 ] && [ -n "$DOMAIN" ] && {
913 echo "search $DOMAIN" >> /tmp/resolv.conf
915 DNS_SERVERS="$DNS_SERVERS 127.0.0.1"
916 for DNS_SERVER in $DNS_SERVERS ; do
917 echo "nameserver $DNS_SERVER" >> /tmp/resolv.conf
921 procd_open_instance $cfg
922 procd_set_param command $PROG -C $CONFIGFILE -k -x /var/run/dnsmasq/dnsmasq."${cfg}".pid
923 procd_set_param file $CONFIGFILE
924 procd_set_param respawn
926 local dnsmasqconffile="/etc/dnsmasq.${cfg}.conf"
927 if [ ! -r "$dnsmasqconffile" ]; then
928 dnsmasqconffile=/etc/dnsmasq.conf
931 procd_add_jail dnsmasq ubus log
932 procd_add_jail_mount $CONFIGFILE $TRUSTANCHORSFILE $HOSTFILE /etc/passwd /etc/group /etc/TZ /dev/null /dev/urandom $dnsmasqconffile $dnsmasqconfdir $resolvfile $dhcpscript /etc/hosts /etc/ethers $EXTRA_MOUNT
933 procd_add_jail_mount_rw /var/run/dnsmasq/ $leasefile
942 config_get resolvfile "$cfg" "resolvfile"
944 #relink /tmp/resolve.conf only for main instance
945 [ "$resolvfile" = "/tmp/resolv.conf.auto" ] && {
946 [ -f /tmp/resolv.conf ] && {
947 rm -f /tmp/resolv.conf
948 ln -s "$resolvfile" /tmp/resolv.conf
952 rm -f ${BASEDHCPSTAMPFILE}.${cfg}.*.dhcp
957 procd_add_reload_trigger "dhcp"
958 procd_add_raw_trigger "interface.*" 2000 /etc/init.d/dnsmasq reload
969 local instance_found=0
971 [ -n "$BOOT" ] && return
973 . /lib/functions/network.sh
978 if [ "$type" = "dnsmasq" ]; then
979 if [ -n "$instance" -a "$instance" = "$name" ]; then
987 if [ -n "$instance" ]; then
988 [ "$instance_found" -gt 0 ] || return
989 dnsmasq_start "$instance"
991 config_foreach dnsmasq_start dnsmasq
996 rc_procd start_service "$@"
1002 local instance_found=0
1007 if [ "$type" = "dnsmasq" ]; then
1008 if [ -n "$instance" -a "$instance" = "$name" ]; then
1016 if [ -n "$instance" ]; then
1017 [ "$instance_found" -gt 0 ] || return
1018 dnsmasq_stop "$instance"
1020 config_foreach dnsmasq_stop dnsmasq