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"
712 local cfg="$1" disabled resolvfile user_dhcpscript
714 config_get_bool disabled "$cfg" disabled 0
715 [ "$disabled" -gt 0 ] && return 0
717 # reset list of DOMAINS and DNS servers (for each dnsmasq instance)
720 CONFIGFILE="${BASECONFIGFILE}.${cfg}"
721 CONFIGFILE_TMP="${CONFIGFILE}.$$"
722 HOSTFILE="${BASEHOSTFILE}.${cfg}"
723 TIMESTAMPFILE="${BASETIMESTAMPFILE}.${cfg}"
724 BASEDHCPSTAMPFILE_CFG="${BASEDHCPSTAMPFILE}.${cfg}"
726 # before we can call xappend
727 mkdir -p /var/run/dnsmasq/
728 mkdir -p $(dirname $CONFIGFILE)
729 mkdir -p $(dirname $HOSTFILE)
730 mkdir -p /var/lib/misc
731 chown dnsmasq:dnsmasq /var/run/dnsmasq
733 [ -f "$TIMESTAMPFILE" ] && rm -f "$TIMESTAMPFILE"
735 echo "# auto-generated config file from /etc/config/dhcp" > $CONFIGFILE_TMP
736 echo "# auto-generated config file from /etc/config/dhcp" > $HOSTFILE
738 # if we did this last, we could override auto-generated config
739 [ -f /etc/dnsmasq.conf ] && {
740 xappend "--conf-file=/etc/dnsmasq.conf"
743 $PROG --version | grep -osqE "^Compile time options:.* DHCPv6( |$)" && DHCPv6CAPABLE=1 || DHCPv6CAPABLE=0
746 if [ -x /usr/sbin/odhcpd -a -x /etc/init.d/odhcpd ] ; then
747 local odhcpd_is_main odhcpd_is_enabled
748 config_get odhcpd_is_main odhcpd maindhcp 0
749 /etc/init.d/odhcpd enabled && odhcpd_is_enabled=1 || odhcpd_is_enabled=0
752 if [ "$odhcpd_is_enabled" -eq 0 -a "$DHCPv6CAPABLE" -eq 1 ] ; then
753 # DHCP V4 and V6 in DNSMASQ
755 elif [ "$odhcpd_is_main" -gt 0 ] ; then
756 # ODHCPD is doing it all
759 # You have ODHCPD but use DNSMASQ for DHCPV4
763 elif [ "$DHCPv6CAPABLE" -eq 1 ] ; then
764 # DHCP V4 and V6 in DNSMASQ
770 # Allow DHCP/DHCPv6 to be handled by ISC DHCPD
771 if [ -x /usr/sbin/dhcpd ] ; then
772 if [ -x /etc/init.d/dhcpd ] ; then
773 /etc/init.d/dhcpd enabled && DNSMASQ_DHCP_VER=0
775 if [ -x /etc/init.d/dhcpd6 -a "$DNSMASQ_DHCP_VER" -gt 0 ] ; then
776 /etc/init.d/dhcpd6 enabled && DNSMASQ_DHCP_VER=4
780 append_bool "$cfg" authoritative "--dhcp-authoritative"
781 append_bool "$cfg" nodaemon "--no-daemon"
782 append_bool "$cfg" domainneeded "--domain-needed"
783 append_bool "$cfg" filterwin2k "--filterwin2k"
784 append_bool "$cfg" nohosts "--no-hosts"
785 append_bool "$cfg" nonegcache "--no-negcache"
786 append_bool "$cfg" strictorder "--strict-order"
787 append_bool "$cfg" logqueries "--log-queries=extra"
788 append_bool "$cfg" noresolv "--no-resolv"
789 append_bool "$cfg" localise_queries "--localise-queries"
790 append_bool "$cfg" readethers "--read-ethers"
791 append_bool "$cfg" dbus "--enable-dbus"
792 append_bool "$cfg" expandhosts "--expand-hosts"
793 config_get tftp_root "$cfg" "tftp_root"
794 [ -n "$tftp_root" ] && mkdir -p "$tftp_root" && append_bool "$cfg" enable_tftp "--enable-tftp"
795 append_bool "$cfg" tftp_no_fail "--tftp-no-fail"
796 append_bool "$cfg" nonwildcard "--bind-dynamic" 1
797 append_bool "$cfg" fqdn "--dhcp-fqdn"
798 append_bool "$cfg" proxydnssec "--proxy-dnssec"
799 append_bool "$cfg" localservice "--local-service"
800 append_bool "$cfg" logdhcp "--log-dhcp"
801 append_bool "$cfg" quietdhcp "--quiet-dhcp"
802 append_bool "$cfg" sequential_ip "--dhcp-sequential-ip"
803 append_bool "$cfg" allservers "--all-servers"
804 append_bool "$cfg" noping "--no-ping"
806 append_parm "$cfg" logfacility "--log-facility"
808 append_parm "$cfg" cachesize "--cache-size"
809 append_parm "$cfg" dnsforwardmax "--dns-forward-max"
810 append_parm "$cfg" port "--port"
811 append_parm "$cfg" ednspacket_max "--edns-packet-max"
812 append_parm "$cfg" dhcpleasemax "--dhcp-lease-max"
813 append_parm "$cfg" "queryport" "--query-port"
814 append_parm "$cfg" "minport" "--min-port"
815 append_parm "$cfg" "maxport" "--max-port"
816 append_parm "$cfg" "domain" "--domain"
817 append_parm "$cfg" "local" "--server"
818 config_list_foreach "$cfg" "server" append_server
819 config_list_foreach "$cfg" "address" append_address
820 config_list_foreach "$cfg" "ipset" append_ipset
821 config_list_foreach "$cfg" "interface" append_interface
822 config_list_foreach "$cfg" "notinterface" append_notinterface
823 config_list_foreach "$cfg" "addnhosts" append_addnhosts
824 config_list_foreach "$cfg" "bogusnxdomain" append_bogusnxdomain
825 append_parm "$cfg" "leasefile" "--dhcp-leasefile" "/tmp/dhcp.leases"
826 append_parm "$cfg" "serversfile" "--servers-file"
827 append_parm "$cfg" "tftp_root" "--tftp-root"
828 append_parm "$cfg" "dhcp_boot" "--dhcp-boot"
829 append_parm "$cfg" "local_ttl" "--local-ttl"
830 append_parm "$cfg" "pxe_prompt" "--pxe-prompt"
831 config_list_foreach "$cfg" "pxe_service" append_pxe_service
832 config_get DOMAIN "$cfg" domain
834 config_get_bool ADD_LOCAL_DOMAIN "$cfg" add_local_domain 1
835 config_get_bool ADD_LOCAL_HOSTNAME "$cfg" add_local_hostname 1
836 config_get ADD_LOCAL_FQDN "$cfg" add_local_fqdn ""
837 config_get ADD_WAN_FQDN "$cfg" add_wan_fqdn 0
839 if [ -z "$ADD_LOCAL_FQDN" ] ; then
840 # maintain support for previous UCI
841 ADD_LOCAL_FQDN="$ADD_LOCAL_HOSTNAME"
844 config_get_bool readethers "$cfg" readethers
845 [ "$readethers" = "1" -a \! -e "/etc/ethers" ] && touch /etc/ethers
847 config_get user_dhcpscript $cfg dhcpscript
848 if has_handler || [ -n "$user_dhcpscript" ]; then
849 xappend "--dhcp-script=$DHCPSCRIPT"
852 config_get leasefile $cfg leasefile "/tmp/dhcp.leases"
853 [ -n "$leasefile" -a \! -e "$leasefile" ] && touch "$leasefile"
854 config_get_bool cachelocal "$cfg" cachelocal 1
856 config_get_bool noresolv "$cfg" noresolv 0
857 if [ "$noresolv" != "1" ]; then
858 config_get resolvfile "$cfg" resolvfile "/tmp/resolv.conf.auto"
859 # So jail doesn't complain if file missing
860 [ -n "$resolvfile" -a \! -e "$resolvfile" ] && touch "$resolvfile"
863 [ -n "$resolvfile" ] && xappend "--resolv-file=$resolvfile"
865 config_get hostsfile "$cfg" dhcphostsfile
866 [ -e "$hostsfile" ] && xappend "--dhcp-hostsfile=$hostsfile"
869 config_get_bool rebind "$cfg" rebind_protection 1
870 [ $rebind -gt 0 ] && {
872 "DNS rebinding protection is active," \
873 "will discard upstream RFC1918 responses!"
874 xappend "--stop-dns-rebind"
876 local rebind_localhost
877 config_get_bool rebind_localhost "$cfg" rebind_localhost 0
878 [ $rebind_localhost -gt 0 ] && {
879 log_once "Allowing 127.0.0.0/8 responses"
880 xappend "--rebind-localhost-ok"
883 append_rebind_domain() {
884 log_once "Allowing RFC1918 responses for domain $1"
885 xappend "--rebind-domain-ok=$1"
888 config_list_foreach "$cfg" rebind_domain append_rebind_domain
891 config_get_bool dnssec "$cfg" dnssec 0
892 [ "$dnssec" -gt 0 ] && {
893 xappend "--conf-file=$TRUSTANCHORSFILE"
895 [ -x /etc/init.d/sysntpd ] && {
896 /etc/init.d/sysntpd enabled
897 [ "$?" -ne 0 -o "$(uci_get system.ntp.enabled)" = "1" ] && {
898 [ -f "$TIMEVALIDFILE" ] || xappend "--dnssec-no-timecheck"
901 append_bool "$cfg" dnsseccheckunsigned "--dnssec-check-unsigned"
904 config_get addmac "$cfg" addmac 0
905 [ "$addmac" != "0" ] && {
906 [ "$addmac" = "1" ] && addmac=
907 xappend "--add-mac${addmac:+="$addmac"}"
910 dhcp_option_add "$cfg" "" 0
912 xappend "--dhcp-broadcast=tag:needs-broadcast"
914 xappend "--addn-hosts=$(dirname $HOSTFILE)"
916 config_get dnsmasqconfdir "$cfg" confdir "/tmp/dnsmasq.d"
917 [ ! -d "$dnsmasqconfdir" ] && mkdir -p $dnsmasqconfdir
918 xappend "--conf-dir=$dnsmasqconfdir"
919 xappend "--user=dnsmasq"
920 xappend "--group=dnsmasq"
921 echo >> $CONFIGFILE_TMP
923 config_get_bool enable_tftp "$cfg" enable_tftp 0
924 [ "$enable_tftp" -gt 0 ] && {
925 config_get tftp_root "$cfg" tftp_root
926 append EXTRA_MOUNT $tftp_root
929 config_foreach filter_dnsmasq host dhcp_host_add "$cfg"
930 echo >> $CONFIGFILE_TMP
931 config_foreach filter_dnsmasq boot dhcp_boot_add "$cfg"
932 config_foreach filter_dnsmasq mac dhcp_mac_add "$cfg"
933 config_foreach filter_dnsmasq tag dhcp_tag_add "$cfg"
934 config_foreach filter_dnsmasq vendorclass dhcp_vendorclass_add "$cfg"
935 config_foreach filter_dnsmasq userclass dhcp_userclass_add "$cfg"
936 config_foreach filter_dnsmasq circuitid dhcp_circuitid_add "$cfg"
937 config_foreach filter_dnsmasq remoteid dhcp_remoteid_add "$cfg"
938 config_foreach filter_dnsmasq subscrid dhcp_subscrid_add "$cfg"
939 config_foreach filter_dnsmasq match dhcp_match_add "$cfg"
940 config_foreach filter_dnsmasq domain dhcp_domain_add "$cfg"
941 config_foreach filter_dnsmasq hostrecord dhcp_hostrecord_add "$cfg"
942 config_foreach filter_dnsmasq relay dhcp_relay_add "$cfg"
944 echo >> $CONFIGFILE_TMP
945 config_foreach filter_dnsmasq srvhost dhcp_srv_add "$cfg"
946 config_foreach filter_dnsmasq mxhost dhcp_mx_add "$cfg"
947 echo >> $CONFIGFILE_TMP
949 config_get_bool boguspriv "$cfg" boguspriv 1
950 [ "$boguspriv" -gt 0 ] && {
951 xappend "--bogus-priv"
952 [ -r "$RFC6761FILE" ] && xappend "--conf-file=$RFC6761FILE"
955 if [ "$DNSMASQ_DHCP_VER" -gt 4 ] ; then
956 # Enable RA feature for when/if it is constructed,
957 # and RA is selected per interface pool (RA, DHCP, or both),
958 # but no one (should) want RA broadcast in syslog
959 config_foreach filter_dnsmasq dhcp dhcp_add "$cfg"
960 xappend "--enable-ra"
962 append_bool "$cfg" quietdhcp "--quiet-dhcp6"
964 elif [ "$DNSMASQ_DHCP_VER" -gt 0 ] ; then
965 config_foreach filter_dnsmasq dhcp dhcp_add "$cfg"
969 echo >> $CONFIGFILE_TMP
970 config_foreach filter_dnsmasq cname dhcp_cname_add "$cfg"
971 echo >> $CONFIGFILE_TMP
973 echo >> $CONFIGFILE_TMP
974 mv -f $CONFIGFILE_TMP $CONFIGFILE
976 [ "$resolvfile" = "/tmp/resolv.conf.auto" ] && {
977 rm -f /tmp/resolv.conf
978 [ $ADD_LOCAL_DOMAIN -eq 1 ] && [ -n "$DOMAIN" ] && {
979 echo "search $DOMAIN" >> /tmp/resolv.conf
981 DNS_SERVERS="$DNS_SERVERS 127.0.0.1"
982 for DNS_SERVER in $DNS_SERVERS ; do
983 echo "nameserver $DNS_SERVER" >> /tmp/resolv.conf
987 procd_open_instance $cfg
988 procd_set_param command $PROG -C $CONFIGFILE -k -x /var/run/dnsmasq/dnsmasq."${cfg}".pid
989 procd_set_param file $CONFIGFILE
990 [ -n "$user_dhcpscript" ] && procd_set_param env USER_DHCPSCRIPT="$user_dhcpscript"
991 procd_set_param respawn
993 local dnsmasqconffile="/etc/dnsmasq.${cfg}.conf"
994 if [ ! -r "$dnsmasqconffile" ]; then
995 dnsmasqconffile=/etc/dnsmasq.conf
998 procd_add_jail dnsmasq ubus log
999 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
1000 procd_add_jail_mount_rw /var/run/dnsmasq/ $leasefile
1002 procd_close_instance
1009 config_get resolvfile "$cfg" "resolvfile"
1011 #relink /tmp/resolve.conf only for main instance
1012 [ "$resolvfile" = "/tmp/resolv.conf.auto" ] && {
1013 [ -f /tmp/resolv.conf ] && {
1014 rm -f /tmp/resolv.conf
1015 ln -s "$resolvfile" /tmp/resolv.conf
1019 rm -f ${BASEDHCPSTAMPFILE}.${cfg}.*.dhcp
1024 procd_add_reload_trigger "dhcp"
1025 procd_add_raw_trigger "interface.*" 2000 /etc/init.d/dnsmasq reload
1036 local instance_found=0
1038 [ -n "$BOOT" ] && return
1040 . /lib/functions/network.sh
1045 if [ "$type" = "dnsmasq" ]; then
1046 if [ -n "$instance" -a "$instance" = "$name" ]; then
1054 if [ -n "$instance" ]; then
1055 [ "$instance_found" -gt 0 ] || return
1056 dnsmasq_start "$instance"
1058 config_foreach dnsmasq_start dnsmasq
1063 rc_procd start_service "$@"
1069 local instance_found=0
1074 if [ "$type" = "dnsmasq" ]; then
1075 if [ -n "$instance" -a "$instance" = "$name" ]; then
1083 if [ -n "$instance" ]; then
1084 [ "$instance_found" -gt 0 ] || return
1085 dnsmasq_stop "$instance"
1087 config_foreach dnsmasq_stop dnsmasq