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"
20 RFC6761FILE="/usr/share/dnsmasq/rfc6761.conf"
21 DHCPSCRIPT="/usr/lib/dnsmasq/dhcp-script.sh"
28 echo "${value#--}" >> $CONFIGFILE_TMP
33 local hex="${2#0x}" # strip optional "0x" prefix
35 if [ -n "${hex//[0-9a-fA-F]/}" ]; then
36 # is invalid hex literal
40 # convert into host id
43 $(((0x$hex >> 16) % 65536)) \
54 while [ -n "$ip" ]; do
57 res="$(($res + $part))"
58 [ "${ip%.*}" != "$ip" ] && ip="${ip#*.}" || ip=
65 local stamp="${BASEDHCPSTAMPFILE_CFG}.${ifname}.dhcp"
68 [ -s "$stamp" ] && return $(cat "$stamp")
70 # If there's no carrier yet, skip this interface.
71 # The init script will be called again once the link is up
72 case "$(devstatus "$ifname" | jsonfilter -e @.carrier)" in
76 udhcpc -n -q -s /bin/true -t 1 -i "$ifname" >&- && rv=1 || rv=0
80 "found already running DHCP-server on interface '$ifname'" \
81 "refusing to start, use 'option force 1' to override"
88 pidof dnsmasq >/dev/null || \
89 logger -t dnsmasq "$@"
95 for file in /etc/hotplug.d/dhcp/* /etc/hotplug.d/tftp/* /etc/hotplug.d/neigh/*; do
96 [ -f "$file" ] && return 0
108 [ -z "$default" ] && default="0"
109 config_get_bool _loctmp "$section" "$option" "$default"
110 [ $_loctmp -gt 0 ] && xappend "$value"
119 config_get _loctmp "$section" "$option" "$default"
120 [ -z "$_loctmp" ] && return 0
121 xappend "$switch=$_loctmp"
125 xappend "--server=$1"
129 xappend "--address=$1"
137 network_get_device ifname "$1" || return
138 xappend "--interface=$ifname"
141 append_notinterface() {
142 network_get_device ifname "$1" || return
143 xappend "--except-interface=$ifname"
147 xappend "--addn-hosts=$1"
150 append_bogusnxdomain() {
151 xappend "--bogus-nxdomain=$1"
154 append_pxe_service() {
155 xappend "--pxe-service=$1"
158 append_interface_name() {
159 xappend "--interface-name=$1,$2"
163 local cfg="$1" func="$2" match_cfg="$3" found_cfg
165 # use entry when no instance entry set, or if it matches
166 config_get found_cfg "$cfg" "instance"
167 if [ -z "$found_cfg" -o "$found_cfg" = "$match_cfg" ]; then
172 dhcp_subscrid_add() {
175 config_get networkid "$cfg" networkid
176 [ -n "$networkid" ] || return 0
178 config_get subscriberid "$cfg" subscriberid
179 [ -n "$subscriberid" ] || return 0
181 xappend "--dhcp-subscrid=$networkid,$subscriberid"
183 config_get_bool force "$cfg" force 0
185 dhcp_option_add "$cfg" "$networkid" "$force"
188 dhcp_remoteid_add() {
191 config_get networkid "$cfg" networkid
192 [ -n "$networkid" ] || return 0
194 config_get remoteid "$cfg" remoteid
195 [ -n "$remoteid" ] || return 0
197 xappend "--dhcp-remoteid=$networkid,$remoteid"
199 config_get_bool force "$cfg" force 0
201 dhcp_option_add "$cfg" "$networkid" "$force"
204 dhcp_circuitid_add() {
205 # TODO: DHCPV6 does not have circuitid; catch "option6:"
208 config_get networkid "$cfg" networkid
209 [ -n "$networkid" ] || return 0
211 config_get circuitid "$cfg" circuitid
212 [ -n "$circuitid" ] || return 0
214 xappend "--dhcp-circuitid=$networkid,$circuitid"
216 config_get_bool force "$cfg" force 0
218 dhcp_option_add "$cfg" "$networkid" "$force"
221 dhcp_userclass_add() {
224 config_get networkid "$cfg" networkid
225 [ -n "$networkid" ] || return 0
227 config_get userclass "$cfg" userclass
228 [ -n "$userclass" ] || return 0
230 xappend "--dhcp-userclass=$networkid,$userclass"
232 config_get_bool force "$cfg" force 0
234 dhcp_option_add "$cfg" "$networkid" "$force"
237 dhcp_vendorclass_add() {
238 # TODO: DHCPV6 vendor class has stricter definitions; catch? fixup?
241 config_get networkid "$cfg" networkid
242 [ -n "$networkid" ] || return 0
244 config_get vendorclass "$cfg" vendorclass
245 [ -n "$vendorclass" ] || return 0
247 xappend "--dhcp-vendorclass=$networkid,$vendorclass"
249 config_get_bool force "$cfg" force 0
251 dhcp_option_add "$cfg" "$networkid" "$force"
257 config_get networkid "$cfg" networkid
258 [ -n "$networkid" ] || return 0
260 config_get match "$cfg" match
261 [ -n "$match" ] || return 0
263 xappend "--dhcp-match=$networkid,$match"
265 config_get_bool force "$cfg" force 0
267 dhcp_option_add "$cfg" "$networkid" "$force"
272 local hosttag nametime addrs duids
274 config_get_bool force "$cfg" force 0
276 config_get networkid "$cfg" networkid
277 [ -n "$networkid" ] && dhcp_option_add "$cfg" "$networkid" "$force"
279 config_get_bool enable "$cfg" enable 1
280 [ "$enable" = "0" ] && return 0
282 config_get name "$cfg" name
283 config_get ip "$cfg" ip
284 config_get hostid "$cfg" hostid
286 [ -n "$ip" -o -n "$name" -o -n "$hostid" ] || return 0
288 config_get_bool dns "$cfg" dns 0
289 [ "$dns" = "1" -a -n "$ip" -a -n "$name" ] && {
290 echo "$ip $name${DOMAIN:+.$DOMAIN}" >> $HOSTFILE
293 config_get mac "$cfg" mac
294 config_get duid "$cfg" duid
295 config_get tag "$cfg" tag
297 if [ -n "$mac" ]; then
298 # --dhcp-host=00:20:e0:3b:13:af,192.168.0.199,lap
299 # many MAC are possible to track a laptop ON/OFF dock
301 for m in $mac; do append macs "$m" ","; done
304 if [ $DNSMASQ_DHCP_VER -eq 6 -a -n "$duid" ]; then
305 # --dhcp-host=id:00:03:00:01:12:00:00:01:02:03,[::beef],lap
306 # one (virtual) machine gets one DUID per RFC3315
307 duids="id:${duid// */}"
310 if [ -z "$macs" -a -z "$duids" ]; then
311 # --dhcp-host=lap,192.168.0.199,[::beef]
312 [ -n "$name" ] || return 0
317 if [ -n "$hostid" ]; then
318 hex_to_hostid hostid "$hostid"
321 config_get_bool broadcast "$cfg" broadcast 0
322 config_get leasetime "$cfg" leasetime
324 [ "$broadcast" = "0" ] && broadcast= || broadcast=",set:needs-broadcast"
326 hosttag="${networkid:+,set:${networkid}}${tag:+,set:${tag}}$broadcast"
327 nametime="${name:+,$name}${leasetime:+,$leasetime}"
329 if [ $DNSMASQ_DHCP_VER -eq 6 ]; then
330 addrs="${ip:+,$ip}${hostid:+,[::$hostid]}"
331 xappend "--dhcp-host=$macs${duids:+,$duids}$hosttag$addrs$nametime"
333 xappend "--dhcp-host=$macs$hosttag${ip:+,$ip}$nametime"
337 dhcp_this_host_add() {
341 local routerstub routername ifdashname
342 local lanaddr lanaddr6 lanaddrs6 ulaprefix
344 if [ "$mode" -gt 0 ] ; then
345 ifdashname="${ifname//./-}"
346 routerstub="$( md5sum /etc/os-release )"
347 routerstub="router-${routerstub// */}"
348 routername="$( uci_get system @system[0] hostname $routerstub )"
350 if [ "$mode" -gt 1 ] ; then
351 if [ "$mode" -gt 2 ] ; then
352 if [ "$mode" -gt 3 ] ; then
353 append_interface_name "$ifdashname.$routername.$DOMAIN" "$ifname"
356 append_interface_name "$routername.$DOMAIN" "$ifname"
359 # All IP addresses discovered by dnsmasq will be labeled (except fe80::)
360 append_interface_name "$routername" "$ifname"
363 # This uses a static host file entry for only limited addresses.
364 # Use dnsmasq option "--expandhosts" to enable FQDN on host files.
365 ulaprefix="$(uci_get network @globals[0] ula_prefix)"
366 network_get_ipaddr lanaddr "$net"
367 network_get_ipaddrs6 lanaddrs6 "$net"
369 if [ -n "$lanaddr" ] ; then
370 dhcp_domain_add "" "$routername" "$lanaddr"
373 if [ -n "$ulaprefix" -a -n "$lanaddrs6" ] ; then
374 for lanaddr6 in $lanaddrs6 ; do
376 "${ulaprefix%%:/*}"*)
377 dhcp_domain_add "" "$routername" "$lanaddr6"
387 # NOTE: dnsmasq has explicit "option6:" prefix for DHCPv6 so no collisions
392 [ -n "$tag" ] || return 0
394 config_get_bool force "$cfg" force 0
395 [ "$force" = "0" ] && force=
397 config_get option "$cfg" dhcp_option
399 xappend "--dhcp-option${force:+-force}=tag:$tag,$o"
406 config_get networkid "$cfg" networkid
407 [ -n "$networkid" ] || return 0
409 config_get mac "$cfg" mac
410 [ -n "$mac" ] || return 0
412 xappend "--dhcp-mac=$networkid,$mac"
414 dhcp_option_add "$cfg" "$networkid"
418 # TODO: BOOTURL is different between DHCPv4 and DHCPv6
421 config_get networkid "$cfg" networkid
423 config_get filename "$cfg" filename
424 [ -n "$filename" ] || return 0
426 config_get servername "$cfg" servername
427 config_get serveraddress "$cfg" serveraddress
429 [ -n "$serveraddress" -a ! -n "$servername" ] && return 0
431 xappend "--dhcp-boot=${networkid:+net:$networkid,}${filename}${servername:+,$servername}${serveraddress:+,$serveraddress}"
433 config_get_bool force "$cfg" force 0
435 dhcp_option_add "$cfg" "$networkid" "$force"
441 local dhcp6range="::"
444 config_get net "$cfg" interface
445 [ -n "$net" ] || return 0
447 config_get networkid "$cfg" networkid
448 [ -n "$networkid" ] || networkid="$net"
450 network_get_subnet subnet "$net" || return 0
451 network_get_device ifname "$net" || return 0
452 network_get_protocol proto "$net" || return 0
454 [ "$cachelocal" = "0" ] && network_get_dnsserver dnsserver "$net" && {
455 DNS_SERVERS="$DNS_SERVERS $dnsserver"
458 append_bool "$cfg" ignore "--no-dhcp-interface=$ifname" && {
459 # Many ISP do not have useful names for DHCP customers (your WAN).
460 dhcp_this_host_add "$net" "$ifname" "$ADD_WAN_FQDN"
464 # Do not support non-static interfaces for now
465 [ static = "$proto" ] || return 0
467 # Override interface netmask with dhcp config if applicable
468 config_get netmask "$cfg" netmask "${subnet##*/}"
470 #check for an already active dhcp server on the interface, unless 'force' is set
471 config_get_bool force "$cfg" force 0
472 [ $force -gt 0 ] || dhcp_check "$ifname" || return 0
474 config_get start "$cfg" start 100
475 config_get limit "$cfg" limit 150
476 config_get leasetime "$cfg" leasetime 12h
477 config_get options "$cfg" options
478 config_get_bool dynamicdhcp "$cfg" dynamicdhcp 1
480 config_get dhcpv4 "$cfg" dhcpv4
481 config_get dhcpv6 "$cfg" dhcpv6
483 config_get ra "$cfg" ra
484 config_get ra_management "$cfg" ra_management
485 config_get ra_preference "$cfg" ra_preference
486 config_get dns "$cfg" dns
488 config_list_foreach "$cfg" "interface_name" append_interface_name "$ifname"
490 # Put the router host name on this DHCP served interface address(es)
491 dhcp_this_host_add "$net" "$ifname" "$ADD_LOCAL_FQDN"
493 start="$( dhcp_calc "$start" )"
494 nettag="${networkid:+set:${networkid},}"
496 if [ "$limit" -gt 0 ] ; then
500 eval "$(ipcalc.sh "${subnet%%/*}" $netmask $start $limit)"
502 if [ "$dynamicdhcp" = "0" ] ; then
504 dhcp6range="::,static"
506 dhcp6range="::1000,::ffff"
510 if [ "$dhcpv4" != "disabled" ] ; then
511 xappend "--dhcp-range=$nettag$START,$END,$NETMASK,$leasetime${options:+ $options}"
515 if [ $DNSMASQ_DHCP_VER -eq 6 -a "$ra" = "server" ] ; then
516 # Note: dnsmasq cannot just be a DHCPv6 server (all-in-1)
517 # and let some other machine(s) send RA pointing to it.
519 case $ra_preference in
521 xappend "--ra-param=$ifname,high,0,7200"
524 xappend "--ra-param=$ifname,low,0,7200"
527 # Send UNSOLICITED RA at default interval and live for 2 hours.
528 # TODO: convert flexible lease time into route life time (only seconds).
529 xappend "--ra-param=$ifname,0,7200"
533 if [ "$dhcpv6" = "disabled" ] ; then
538 case $ra_management in
540 # SLACC with DCHP for extended options
541 xappend "--dhcp-range=$nettag::,constructor:$ifname,ra-stateless,ra-names"
544 # DHCP address and RA only for management redirection
545 xappend "--dhcp-range=$nettag$dhcp6range,constructor:$ifname,$leasetime"
548 # SLAAC only but dnsmasq attempts to link HOSTNAME, DHCPv4 MAC, and SLAAC
549 xappend "--dhcp-range=$nettag::,constructor:$ifname,ra-only,ra-names"
552 # SLAAC and full DHCP
553 xappend "--dhcp-range=$nettag$dhcp6range,constructor:$ifname,slaac,ra-names,$leasetime"
557 if [ -n "$dns" ]; then
559 for d in $dns; do append dnss "[$d]" ","; done
564 dhcp_option_append "option6:dns-server,$dnss" "$networkid"
567 dhcp_option_add "$cfg" "$networkid"
570 dhcp_option_append() {
575 xappend "--dhcp-option${force:+-force}=${networkid:+$networkid,}$option"
579 # NOTE: dnsmasq has explicit "option6:" prefix for DHCPv6 so no collisions
584 [ "$force" = "0" ] && force=
587 config_get list_len "$cfg" dhcp_option_LENGTH
589 if [ -n "$list_len" ]; then
590 config_list_foreach "$cfg" dhcp_option dhcp_option_append "$networkid" "$force"
592 config_get dhcp_option "$cfg" dhcp_option
594 [ -n "$dhcp_option" ] && echo "Warning: the 'option dhcp_option' syntax is deprecated, use 'list dhcp_option'" >&2
597 for option in $dhcp_option; do
598 dhcp_option_append "$option" "$networkid" "$force"
605 local ip name names record
607 config_get names "$cfg" name "$2"
608 [ -n "$names" ] || return 0
610 config_get ip "$cfg" ip "$3"
611 [ -n "$ip" ] || return 0
613 for name in $names; do
614 record="${record:+$record }$name"
617 echo "$ip $record" >> $HOSTFILE
623 config_get srv "$cfg" srv
624 [ -n "$srv" ] || return 0
626 config_get target "$cfg" target
627 [ -n "$target" ] || return 0
629 config_get port "$cfg" port
630 [ -n "$port" ] || return 0
632 config_get class "$cfg" class
633 config_get weight "$cfg" weight
635 local service="$srv,$target,$port${class:+,$class${weight:+,$weight}}"
637 xappend "--srv-host=$service"
642 local domain relay pref
644 config_get domain "$cfg" domain
645 [ -n "$domain" ] || return 0
647 config_get relay "$cfg" relay
648 [ -n "$relay" ] || return 0
650 config_get pref "$cfg" pref 0
652 local service="$domain,$relay,$pref"
654 xappend "--mx-host=$service"
661 config_get cname "$cfg" cname
662 [ -n "$cname" ] || return 0
664 config_get target "$cfg" target
665 [ -n "$target" ] || return 0
667 xappend "--cname=${cname},${target}"
670 dhcp_hostrecord_add() {
672 local names addresses record val
674 config_get names "$cfg" name "$2"
675 if [ -z "$names" ]; then
679 config_get addresses "$cfg" ip "$3"
680 if [ -z "$addresses" ]; then
684 for val in $names $addresses; do
685 record="${record:+$record,}$val"
688 xappend "--host-record=$record"
693 local local_addr server_addr interface
695 config_get local_addr "$cfg" local_addr
696 [ -n "$local_addr" ] || return 0
698 config_get server_addr "$cfg" server_addr
699 [ -n "$server_addr" ] || return 0
701 config_get interface "$cfg" interface
702 if [ -z "$interface" ]; then
703 xappend "--dhcp-relay=$local_addr,$server_addr"
705 network_get_device ifname "$interface" || return
706 xappend "--dhcp-relay=$local_addr,$server_addr,$ifname"
713 local port notinterfaces
715 config_get port "$cfg" port "53"
716 [ $port = "53" ] || return 1
718 config_get notinterfaces "$cfg" notinterface ""
719 [ -n $notinterfaces ] && list_contains $notinterfaces "loopback" || return 1
721 # dnsmasq instance is designated to listen on 127.0.0.1#53.
727 local cfg="$1" disabled resolvfile user_dhcpscript
729 config_get_bool disabled "$cfg" disabled 0
730 [ "$disabled" -gt 0 ] && return 0
732 # reset list of DOMAINS and DNS servers (for each dnsmasq instance)
735 CONFIGFILE="${BASECONFIGFILE}.${cfg}"
736 CONFIGFILE_TMP="${CONFIGFILE}.$$"
737 HOSTFILE="${BASEHOSTFILE}.${cfg}"
738 TIMESTAMPFILE="${BASETIMESTAMPFILE}.${cfg}"
739 BASEDHCPSTAMPFILE_CFG="${BASEDHCPSTAMPFILE}.${cfg}"
741 # before we can call xappend
742 mkdir -p /var/run/dnsmasq/
743 mkdir -p $(dirname $CONFIGFILE)
744 mkdir -p $(dirname $HOSTFILE)
745 mkdir -p /var/lib/misc
746 chown dnsmasq:dnsmasq /var/run/dnsmasq
748 [ -f "$TIMESTAMPFILE" ] && rm -f "$TIMESTAMPFILE"
750 echo "# auto-generated config file from /etc/config/dhcp" > $CONFIGFILE_TMP
751 echo "# auto-generated config file from /etc/config/dhcp" > $HOSTFILE
753 # if we did this last, we could override auto-generated config
754 [ -f /etc/dnsmasq.conf ] && {
755 xappend "--conf-file=/etc/dnsmasq.conf"
758 $PROG --version | grep -osqE "^Compile time options:.* DHCPv6( |$)" && DHCPv6CAPABLE=1 || DHCPv6CAPABLE=0
761 if [ -x /usr/sbin/odhcpd -a -x /etc/init.d/odhcpd ] ; then
762 local odhcpd_is_main odhcpd_is_enabled
763 config_get odhcpd_is_main odhcpd maindhcp 0
764 /etc/init.d/odhcpd enabled && odhcpd_is_enabled=1 || odhcpd_is_enabled=0
767 if [ "$odhcpd_is_enabled" -eq 0 -a "$DHCPv6CAPABLE" -eq 1 ] ; then
768 # DHCP V4 and V6 in DNSMASQ
770 elif [ "$odhcpd_is_main" -gt 0 ] ; then
771 # ODHCPD is doing it all
774 # You have ODHCPD but use DNSMASQ for DHCPV4
778 elif [ "$DHCPv6CAPABLE" -eq 1 ] ; then
779 # DHCP V4 and V6 in DNSMASQ
785 # Allow DHCP/DHCPv6 to be handled by ISC DHCPD
786 if [ -x /usr/sbin/dhcpd ] ; then
787 if [ -x /etc/init.d/dhcpd ] ; then
788 /etc/init.d/dhcpd enabled && DNSMASQ_DHCP_VER=0
790 if [ -x /etc/init.d/dhcpd6 -a "$DNSMASQ_DHCP_VER" -gt 0 ] ; then
791 /etc/init.d/dhcpd6 enabled && DNSMASQ_DHCP_VER=4
795 append_bool "$cfg" authoritative "--dhcp-authoritative"
796 append_bool "$cfg" nodaemon "--no-daemon"
797 append_bool "$cfg" domainneeded "--domain-needed"
798 append_bool "$cfg" filterwin2k "--filterwin2k"
799 append_bool "$cfg" nohosts "--no-hosts"
800 append_bool "$cfg" nonegcache "--no-negcache"
801 append_bool "$cfg" strictorder "--strict-order"
802 append_bool "$cfg" logqueries "--log-queries=extra"
803 append_bool "$cfg" noresolv "--no-resolv"
804 append_bool "$cfg" localise_queries "--localise-queries"
805 append_bool "$cfg" readethers "--read-ethers"
806 append_bool "$cfg" dbus "--enable-dbus"
807 append_bool "$cfg" expandhosts "--expand-hosts"
808 config_get tftp_root "$cfg" "tftp_root"
809 [ -n "$tftp_root" ] && mkdir -p "$tftp_root" && append_bool "$cfg" enable_tftp "--enable-tftp"
810 append_bool "$cfg" tftp_no_fail "--tftp-no-fail"
811 append_bool "$cfg" nonwildcard "--bind-dynamic" 1
812 append_bool "$cfg" fqdn "--dhcp-fqdn"
813 append_bool "$cfg" proxydnssec "--proxy-dnssec"
814 append_bool "$cfg" localservice "--local-service"
815 append_bool "$cfg" logdhcp "--log-dhcp"
816 append_bool "$cfg" quietdhcp "--quiet-dhcp"
817 append_bool "$cfg" sequential_ip "--dhcp-sequential-ip"
818 append_bool "$cfg" allservers "--all-servers"
819 append_bool "$cfg" noping "--no-ping"
821 append_parm "$cfg" logfacility "--log-facility"
823 append_parm "$cfg" cachesize "--cache-size"
824 append_parm "$cfg" dnsforwardmax "--dns-forward-max"
825 append_parm "$cfg" port "--port"
826 append_parm "$cfg" ednspacket_max "--edns-packet-max"
827 append_parm "$cfg" dhcpleasemax "--dhcp-lease-max"
828 append_parm "$cfg" "queryport" "--query-port"
829 append_parm "$cfg" "minport" "--min-port"
830 append_parm "$cfg" "maxport" "--max-port"
831 append_parm "$cfg" "domain" "--domain"
832 append_parm "$cfg" "local" "--server"
833 config_list_foreach "$cfg" "server" append_server
834 config_list_foreach "$cfg" "address" append_address
835 config_list_foreach "$cfg" "ipset" append_ipset
836 config_list_foreach "$cfg" "interface" append_interface
837 config_list_foreach "$cfg" "notinterface" append_notinterface
838 config_list_foreach "$cfg" "addnhosts" append_addnhosts
839 config_list_foreach "$cfg" "bogusnxdomain" append_bogusnxdomain
840 append_parm "$cfg" "leasefile" "--dhcp-leasefile" "/tmp/dhcp.leases"
841 append_parm "$cfg" "serversfile" "--servers-file"
842 append_parm "$cfg" "tftp_root" "--tftp-root"
843 append_parm "$cfg" "dhcp_boot" "--dhcp-boot"
844 append_parm "$cfg" "local_ttl" "--local-ttl"
845 append_parm "$cfg" "pxe_prompt" "--pxe-prompt"
846 config_list_foreach "$cfg" "pxe_service" append_pxe_service
847 config_get DOMAIN "$cfg" domain
849 config_get_bool ADD_LOCAL_DOMAIN "$cfg" add_local_domain 1
850 config_get_bool ADD_LOCAL_HOSTNAME "$cfg" add_local_hostname 1
851 config_get ADD_LOCAL_FQDN "$cfg" add_local_fqdn ""
852 config_get ADD_WAN_FQDN "$cfg" add_wan_fqdn 0
854 if [ -z "$ADD_LOCAL_FQDN" ] ; then
855 # maintain support for previous UCI
856 ADD_LOCAL_FQDN="$ADD_LOCAL_HOSTNAME"
859 config_get_bool readethers "$cfg" readethers
860 [ "$readethers" = "1" -a \! -e "/etc/ethers" ] && touch /etc/ethers
862 config_get user_dhcpscript $cfg dhcpscript
863 if has_handler || [ -n "$user_dhcpscript" ]; then
864 xappend "--dhcp-script=$DHCPSCRIPT"
867 config_get leasefile $cfg leasefile "/tmp/dhcp.leases"
868 [ -n "$leasefile" -a \! -e "$leasefile" ] && touch "$leasefile"
869 config_get_bool cachelocal "$cfg" cachelocal 1
871 config_get resolvfile "$cfg" resolvfile "/tmp/resolv.conf.auto"
872 xappend "--resolv-file=$resolvfile"
873 # So jail doesn't complain if file missing
874 [ \! -e "$resolvfile" ] && touch "$resolvfile"
876 config_get hostsfile "$cfg" dhcphostsfile
877 [ -e "$hostsfile" ] && xappend "--dhcp-hostsfile=$hostsfile"
880 config_get_bool rebind "$cfg" rebind_protection 1
881 [ $rebind -gt 0 ] && {
883 "DNS rebinding protection is active," \
884 "will discard upstream RFC1918 responses!"
885 xappend "--stop-dns-rebind"
887 local rebind_localhost
888 config_get_bool rebind_localhost "$cfg" rebind_localhost 0
889 [ $rebind_localhost -gt 0 ] && {
890 log_once "Allowing 127.0.0.0/8 responses"
891 xappend "--rebind-localhost-ok"
894 append_rebind_domain() {
895 log_once "Allowing RFC1918 responses for domain $1"
896 xappend "--rebind-domain-ok=$1"
899 config_list_foreach "$cfg" rebind_domain append_rebind_domain
902 config_get_bool dnssec "$cfg" dnssec 0
903 [ "$dnssec" -gt 0 ] && {
904 xappend "--conf-file=$TRUSTANCHORSFILE"
906 [ -x /etc/init.d/sysntpd ] && {
907 /etc/init.d/sysntpd enabled
908 [ "$?" -ne 0 -o "$(uci_get system.ntp.enabled)" = "1" ] && {
909 [ -f "$TIMEVALIDFILE" ] || xappend "--dnssec-no-timecheck"
912 append_bool "$cfg" dnsseccheckunsigned "--dnssec-check-unsigned"
915 config_get addmac "$cfg" addmac 0
916 [ "$addmac" != "0" ] && {
917 [ "$addmac" = "1" ] && addmac=
918 xappend "--add-mac${addmac:+="$addmac"}"
921 dhcp_option_add "$cfg" "" 0
923 xappend "--dhcp-broadcast=tag:needs-broadcast"
925 xappend "--addn-hosts=$(dirname $HOSTFILE)"
927 config_get dnsmasqconfdir "$cfg" confdir "/tmp/dnsmasq.d"
928 [ ! -d "$dnsmasqconfdir" ] && mkdir -p $dnsmasqconfdir
929 xappend "--conf-dir=$dnsmasqconfdir"
930 xappend "--user=dnsmasq"
931 xappend "--group=dnsmasq"
932 echo >> $CONFIGFILE_TMP
934 config_get_bool enable_tftp "$cfg" enable_tftp 0
935 [ "$enable_tftp" -gt 0 ] && {
936 config_get tftp_root "$cfg" tftp_root
937 append EXTRA_MOUNT $tftp_root
940 config_foreach filter_dnsmasq host dhcp_host_add "$cfg"
941 echo >> $CONFIGFILE_TMP
942 config_foreach filter_dnsmasq boot dhcp_boot_add "$cfg"
943 config_foreach filter_dnsmasq mac dhcp_mac_add "$cfg"
944 config_foreach filter_dnsmasq tag dhcp_tag_add "$cfg"
945 config_foreach filter_dnsmasq vendorclass dhcp_vendorclass_add "$cfg"
946 config_foreach filter_dnsmasq userclass dhcp_userclass_add "$cfg"
947 config_foreach filter_dnsmasq circuitid dhcp_circuitid_add "$cfg"
948 config_foreach filter_dnsmasq remoteid dhcp_remoteid_add "$cfg"
949 config_foreach filter_dnsmasq subscrid dhcp_subscrid_add "$cfg"
950 config_foreach filter_dnsmasq match dhcp_match_add "$cfg"
951 config_foreach filter_dnsmasq domain dhcp_domain_add "$cfg"
952 config_foreach filter_dnsmasq hostrecord dhcp_hostrecord_add "$cfg"
953 config_foreach filter_dnsmasq relay dhcp_relay_add "$cfg"
955 echo >> $CONFIGFILE_TMP
956 config_foreach filter_dnsmasq srvhost dhcp_srv_add "$cfg"
957 config_foreach filter_dnsmasq mxhost dhcp_mx_add "$cfg"
958 echo >> $CONFIGFILE_TMP
960 config_get_bool boguspriv "$cfg" boguspriv 1
961 [ "$boguspriv" -gt 0 ] && {
962 xappend "--bogus-priv"
963 [ -r "$RFC6761FILE" ] && xappend "--conf-file=$RFC6761FILE"
966 if [ "$DNSMASQ_DHCP_VER" -gt 4 ] ; then
967 # Enable RA feature for when/if it is constructed,
968 # and RA is selected per interface pool (RA, DHCP, or both),
969 # but no one (should) want RA broadcast in syslog
970 config_foreach filter_dnsmasq dhcp dhcp_add "$cfg"
971 xappend "--enable-ra"
973 append_bool "$cfg" quietdhcp "--quiet-dhcp6"
975 elif [ "$DNSMASQ_DHCP_VER" -gt 0 ] ; then
976 config_foreach filter_dnsmasq dhcp dhcp_add "$cfg"
980 echo >> $CONFIGFILE_TMP
981 config_foreach filter_dnsmasq cname dhcp_cname_add "$cfg"
982 echo >> $CONFIGFILE_TMP
984 echo >> $CONFIGFILE_TMP
985 mv -f $CONFIGFILE_TMP $CONFIGFILE
988 procd_open_instance $cfg
989 procd_set_param command $PROG -C $CONFIGFILE -k -x /var/run/dnsmasq/dnsmasq."${cfg}".pid
990 procd_set_param file $CONFIGFILE
991 [ -n "$user_dhcpscript" ] && procd_set_param env USER_DHCPSCRIPT="$user_dhcpscript"
992 procd_set_param respawn
994 local dnsmasqconffile="/etc/dnsmasq.${cfg}.conf"
995 if [ ! -r "$dnsmasqconffile" ]; then
996 dnsmasqconffile=/etc/dnsmasq.conf
999 procd_add_jail dnsmasq ubus log
1000 procd_add_jail_mount $CONFIGFILE $TRUSTANCHORSFILE $HOSTFILE $RFC6761FILE /etc/passwd /etc/group /etc/TZ /dev/null /dev/urandom $dnsmasqconffile $dnsmasqconfdir $resolvfile $user_dhcpscript /etc/hosts /etc/ethers /sbin/hotplug-call $EXTRA_MOUNT $DHCPSCRIPT
1001 procd_add_jail_mount_rw /var/run/dnsmasq/ $leasefile
1003 procd_close_instance
1006 # write /tmp/resolve.conf only for main instance
1007 dnsmasq_ismain $cfg && {
1008 rm -f /tmp/resolv.conf
1009 [ $ADD_LOCAL_DOMAIN -eq 1 ] && [ -n "$DOMAIN" ] && {
1010 echo "search $DOMAIN" >> /tmp/resolv.conf
1012 DNS_SERVERS="$DNS_SERVERS 127.0.0.1"
1013 for DNS_SERVER in $DNS_SERVERS ; do
1014 echo "nameserver $DNS_SERVER" >> /tmp/resolv.conf
1023 #relink /tmp/resolve.conf only for main instance
1024 dnsmasq_ismain $cfg && {
1025 [ -f /tmp/resolv.conf ] && rm -f /tmp/resolv.conf
1026 ln -s /tmp/resolv.conf.auto /tmp/resolv.conf
1029 rm -f ${BASEDHCPSTAMPFILE}.${cfg}.*.dhcp
1034 procd_add_reload_trigger "dhcp"
1035 procd_add_raw_trigger "interface.*" 2000 /etc/init.d/dnsmasq reload
1046 local instance_found=0
1048 [ -n "$BOOT" ] && return
1050 . /lib/functions/network.sh
1055 if [ "$type" = "dnsmasq" ]; then
1056 if [ -n "$instance" -a "$instance" = "$name" ]; then
1064 if [ -n "$instance" ]; then
1065 [ "$instance_found" -gt 0 ] || return
1066 dnsmasq_start "$instance"
1068 config_foreach dnsmasq_start dnsmasq
1073 rc_procd start_service "$@"
1079 local instance_found=0
1084 if [ "$type" = "dnsmasq" ]; then
1085 if [ -n "$instance" -a "$instance" = "$name" ]; then
1093 if [ -n "$instance" ]; then
1094 [ "$instance_found" -gt 0 ] || return
1095 dnsmasq_stop "$instance"
1097 config_foreach dnsmasq_stop dnsmasq