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 TRUSTANCHORSFILE="/usr/share/dnsmasq/trust-anchors.conf"
17 TIMEVALIDFILE="/var/state/dnsmasqsec"
18 BASEDHCPSTAMPFILE="/var/run/dnsmasq"
19 DHCPBOGUSHOSTNAMEFILE="/usr/share/dnsmasq/dhcpbogushostname.conf"
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"
128 append_rev_server() {
129 xappend "--rev-server=$1"
133 xappend "--address=$1"
141 network_get_device ifname "$1" || ifname="$1"
142 xappend "--interface=$ifname"
145 append_listenaddress() {
146 xappend "--listen-address=$1"
149 append_notinterface() {
150 network_get_device ifname "$1" || ifname="$1"
151 xappend "--except-interface=$ifname"
155 xappend "--addn-hosts=$1"
158 append_bogusnxdomain() {
159 xappend "--bogus-nxdomain=$1"
162 append_pxe_service() {
163 xappend "--pxe-service=$1"
166 append_interface_name() {
167 xappend "--interface-name=$1,$2"
171 local cfg="$1" func="$2" match_cfg="$3" found_cfg
173 # use entry when no instance entry set, or if it matches
174 config_get found_cfg "$cfg" "instance"
175 if [ -z "$found_cfg" -o "$found_cfg" = "$match_cfg" ]; then
180 dhcp_subscrid_add() {
183 config_get networkid "$cfg" networkid
184 [ -n "$networkid" ] || return 0
186 config_get subscriberid "$cfg" subscriberid
187 [ -n "$subscriberid" ] || return 0
189 xappend "--dhcp-subscrid=$networkid,$subscriberid"
191 config_get_bool force "$cfg" force 0
193 dhcp_option_add "$cfg" "$networkid" "$force"
196 dhcp_remoteid_add() {
199 config_get networkid "$cfg" networkid
200 [ -n "$networkid" ] || return 0
202 config_get remoteid "$cfg" remoteid
203 [ -n "$remoteid" ] || return 0
205 xappend "--dhcp-remoteid=$networkid,$remoteid"
207 config_get_bool force "$cfg" force 0
209 dhcp_option_add "$cfg" "$networkid" "$force"
212 dhcp_circuitid_add() {
213 # TODO: DHCPV6 does not have circuitid; catch "option6:"
216 config_get networkid "$cfg" networkid
217 [ -n "$networkid" ] || return 0
219 config_get circuitid "$cfg" circuitid
220 [ -n "$circuitid" ] || return 0
222 xappend "--dhcp-circuitid=$networkid,$circuitid"
224 config_get_bool force "$cfg" force 0
226 dhcp_option_add "$cfg" "$networkid" "$force"
229 dhcp_userclass_add() {
232 config_get networkid "$cfg" networkid
233 [ -n "$networkid" ] || return 0
235 config_get userclass "$cfg" userclass
236 [ -n "$userclass" ] || return 0
238 xappend "--dhcp-userclass=$networkid,$userclass"
240 config_get_bool force "$cfg" force 0
242 dhcp_option_add "$cfg" "$networkid" "$force"
245 dhcp_vendorclass_add() {
246 # TODO: DHCPV6 vendor class has stricter definitions; catch? fixup?
249 config_get networkid "$cfg" networkid
250 [ -n "$networkid" ] || return 0
252 config_get vendorclass "$cfg" vendorclass
253 [ -n "$vendorclass" ] || return 0
255 xappend "--dhcp-vendorclass=$networkid,$vendorclass"
257 config_get_bool force "$cfg" force 0
259 dhcp_option_add "$cfg" "$networkid" "$force"
265 config_get networkid "$cfg" networkid
266 [ -n "$networkid" ] || return 0
268 config_get match "$cfg" match
269 [ -n "$match" ] || return 0
271 xappend "--dhcp-match=$networkid,$match"
273 config_get_bool force "$cfg" force 0
275 dhcp_option_add "$cfg" "$networkid" "$force"
280 local hosttag nametime addrs duids macs tags
282 config_get_bool force "$cfg" force 0
284 config_get networkid "$cfg" networkid
285 [ -n "$networkid" ] && dhcp_option_add "$cfg" "$networkid" "$force"
287 config_get_bool enable "$cfg" enable 1
288 [ "$enable" = "0" ] && return 0
290 config_get name "$cfg" name
291 config_get ip "$cfg" ip
292 config_get hostid "$cfg" hostid
294 [ -n "$ip" -o -n "$name" -o -n "$hostid" ] || return 0
296 config_get_bool dns "$cfg" dns 0
297 [ "$dns" = "1" -a -n "$ip" -a -n "$name" ] && {
298 echo "$ip $name${DOMAIN:+.$DOMAIN}" >> $HOSTFILE_TMP
301 config_get mac "$cfg" mac
302 config_get duid "$cfg" duid
303 config_get tag "$cfg" tag
305 if [ -n "$mac" ]; then
306 # --dhcp-host=00:20:e0:3b:13:af,192.168.0.199,lap
307 # many MAC are possible to track a laptop ON/OFF dock
308 for m in $mac; do append macs "$m" ","; done
311 if [ $DNSMASQ_DHCP_VER -eq 6 -a -n "$duid" ]; then
312 # --dhcp-host=id:00:03:00:01:12:00:00:01:02:03,[::beef],lap
313 # one (virtual) machine gets one DUID per RFC3315
314 duids="id:${duid// */}"
317 if [ -z "$macs" -a -z "$duids" ]; then
318 # --dhcp-host=lap,192.168.0.199,[::beef]
319 [ -n "$name" ] || return 0
324 if [ -n "$hostid" ]; then
325 hex_to_hostid hostid "$hostid"
328 if [ -n "$tag" ]; then
329 for t in $tag; do append tags "$t" ",set:"; done
332 config_get_bool broadcast "$cfg" broadcast 0
333 config_get leasetime "$cfg" leasetime
335 [ "$broadcast" = "0" ] && broadcast= || broadcast=",set:needs-broadcast"
337 hosttag="${networkid:+,set:${networkid}}${tags:+,set:${tags}}$broadcast"
338 nametime="${name:+,$name}${leasetime:+,$leasetime}"
340 if [ $DNSMASQ_DHCP_VER -eq 6 ]; then
341 addrs="${ip:+,$ip}${hostid:+,[::$hostid]}"
342 xappend "--dhcp-host=$macs${duids:+,$duids}$hosttag$addrs$nametime"
344 xappend "--dhcp-host=$macs$hosttag${ip:+,$ip}$nametime"
348 dhcp_this_host_add() {
352 local routerstub routername ifdashname
353 local lanaddr lanaddr6 lanaddrs6 ulaprefix
355 if [ "$mode" -gt 0 ] ; then
356 ifdashname="${ifname//./-}"
357 routerstub="$( md5sum /etc/os-release )"
358 routerstub="router-${routerstub// */}"
359 routername="$( uci_get system @system[0] hostname $routerstub )"
361 if [ "$mode" -gt 1 ] ; then
362 if [ "$mode" -gt 2 ] ; then
363 if [ "$mode" -gt 3 ] ; then
364 append_interface_name "$ifdashname.$routername.$DOMAIN" "$ifname"
367 append_interface_name "$routername.$DOMAIN" "$ifname"
370 # All IP addresses discovered by dnsmasq will be labeled (except fe80::)
371 append_interface_name "$routername" "$ifname"
374 # This uses a static host file entry for only limited addresses.
375 # Use dnsmasq option "--expandhosts" to enable FQDN on host files.
376 ulaprefix="$(uci_get network @globals[0] ula_prefix)"
377 network_get_ipaddr lanaddr "$net"
378 network_get_ipaddrs6 lanaddrs6 "$net"
380 if [ -n "$lanaddr" ] ; then
381 dhcp_domain_add "" "$routername" "$lanaddr"
384 if [ -n "$ulaprefix" -a -n "$lanaddrs6" ] ; then
385 for lanaddr6 in $lanaddrs6 ; do
387 "${ulaprefix%%:/*}"*)
388 dhcp_domain_add "" "$routername" "$lanaddr6"
398 # NOTE: dnsmasq has explicit "option6:" prefix for DHCPv6 so no collisions
403 [ -n "$tag" ] || return 0
405 config_get_bool force "$cfg" force 0
406 [ "$force" = "0" ] && force=
408 config_get option "$cfg" dhcp_option
410 xappend "--dhcp-option${force:+-force}=tag:$tag,$o"
417 config_get networkid "$cfg" networkid
418 [ -n "$networkid" ] || return 0
420 config_get mac "$cfg" mac
421 [ -n "$mac" ] || return 0
423 xappend "--dhcp-mac=$networkid,$mac"
425 dhcp_option_add "$cfg" "$networkid"
429 # TODO: BOOTURL is different between DHCPv4 and DHCPv6
432 config_get networkid "$cfg" networkid
434 config_get filename "$cfg" filename
435 [ -n "$filename" ] || return 0
437 config_get servername "$cfg" servername
438 config_get serveraddress "$cfg" serveraddress
440 [ -n "$serveraddress" -a ! -n "$servername" ] && return 0
442 xappend "--dhcp-boot=${networkid:+net:$networkid,}${filename}${servername:+,$servername}${serveraddress:+,$serveraddress}"
444 config_get_bool force "$cfg" force 0
446 dhcp_option_add "$cfg" "$networkid" "$force"
452 local dhcp6range="::"
456 config_get net "$cfg" interface
457 [ -n "$net" ] || return 0
459 config_get networkid "$cfg" networkid
460 [ -n "$networkid" ] || networkid="$net"
462 network_get_device ifname "$net" || return 0
464 [ "$cachelocal" = "0" ] && network_get_dnsserver dnsserver "$net" && {
465 DNS_SERVERS="$DNS_SERVERS $dnsserver"
468 append_bool "$cfg" ignore "--no-dhcp-interface=$ifname" && {
469 # Many ISP do not have useful names for DHCP customers (your WAN).
470 dhcp_this_host_add "$net" "$ifname" "$ADD_WAN_FQDN"
474 network_get_subnet subnet "$net" || return 0
475 network_get_protocol proto "$net" || return 0
477 # Do not support non-static interfaces for now
478 [ static = "$proto" ] || return 0
480 # Override interface netmask with dhcp config if applicable
481 config_get netmask "$cfg" netmask "${subnet##*/}"
483 #check for an already active dhcp server on the interface, unless 'force' is set
484 config_get_bool force "$cfg" force 0
485 [ $force -gt 0 ] || dhcp_check "$ifname" || return 0
487 config_get start "$cfg" start 100
488 config_get limit "$cfg" limit 150
489 config_get leasetime "$cfg" leasetime 12h
490 config_get options "$cfg" options
491 config_get_bool dynamicdhcp "$cfg" dynamicdhcp 1
493 config_get dhcpv4 "$cfg" dhcpv4
494 config_get dhcpv6 "$cfg" dhcpv6
496 config_get ra "$cfg" ra
497 config_get ra_management "$cfg" ra_management
498 config_get ra_preference "$cfg" ra_preference
499 config_get dns "$cfg" dns
501 config_list_foreach "$cfg" "interface_name" append_interface_name "$ifname"
503 # Put the router host name on this DHCP served interface address(es)
504 dhcp_this_host_add "$net" "$ifname" "$ADD_LOCAL_FQDN"
506 start="$( dhcp_calc "$start" )"
509 tags="${tags}tag:$1,"
511 config_list_foreach "$cfg" tag add_tag
513 nettag="${networkid:+set:${networkid},}"
515 if [ "$limit" -gt 0 ] ; then
519 eval "$(ipcalc.sh "${subnet%%/*}" $netmask $start $limit)"
521 if [ "$dynamicdhcp" = "0" ] ; then
523 dhcp6range="::,static"
525 dhcp6range="::1000,::ffff"
529 if [ "$dhcpv4" != "disabled" ] ; then
530 xappend "--dhcp-range=$tags$nettag$START,$END,$NETMASK,$leasetime${options:+ $options}"
534 if [ $DNSMASQ_DHCP_VER -eq 6 -a "$ra" = "server" ] ; then
535 # Note: dnsmasq cannot just be a DHCPv6 server (all-in-1)
536 # and let some other machine(s) send RA pointing to it.
538 case $ra_preference in
540 xappend "--ra-param=$ifname,high,0,7200"
543 xappend "--ra-param=$ifname,low,0,7200"
546 # Send UNSOLICITED RA at default interval and live for 2 hours.
547 # TODO: convert flexible lease time into route life time (only seconds).
548 xappend "--ra-param=$ifname,0,7200"
552 if [ "$dhcpv6" = "disabled" ] ; then
557 case $ra_management in
559 # SLACC with DCHP for extended options
560 xappend "--dhcp-range=$nettag::,constructor:$ifname,ra-stateless,ra-names"
563 # DHCP address and RA only for management redirection
564 xappend "--dhcp-range=$nettag$dhcp6range,constructor:$ifname,$leasetime"
567 # SLAAC only but dnsmasq attempts to link HOSTNAME, DHCPv4 MAC, and SLAAC
568 xappend "--dhcp-range=$nettag::,constructor:$ifname,ra-only,ra-names"
571 # SLAAC and full DHCP
572 xappend "--dhcp-range=$nettag$dhcp6range,constructor:$ifname,slaac,ra-names,$leasetime"
576 if [ -n "$dns" ]; then
578 for d in $dns; do append dnss "[$d]" ","; done
583 dhcp_option_append "option6:dns-server,$dnss" "$networkid"
586 dhcp_option_add "$cfg" "$networkid" 0
587 dhcp_option_add "$cfg" "$networkid" 2
590 dhcp_option_append() {
595 xappend "--dhcp-option${force:+-force}=${networkid:+$networkid,}$option"
599 # NOTE: dnsmasq has explicit "option6:" prefix for DHCPv6 so no collisions
603 local opt="dhcp_option"
605 [ "$force" = "0" ] && force=
606 [ "$force" = "2" ] && opt="dhcp_option_force"
609 config_get list_len "$cfg" "${opt}_LENGTH"
611 if [ -n "$list_len" ]; then
612 config_list_foreach "$cfg" "$opt" dhcp_option_append "$networkid" "$force"
614 config_get dhcp_option "$cfg" "$opt"
616 [ -n "$dhcp_option" ] && echo "Warning: the 'option $opt' syntax is deprecated, use 'list $opt'" >&2
619 for option in $dhcp_option; do
620 dhcp_option_append "$option" "$networkid" "$force"
627 local ip name names record
629 config_get names "$cfg" name "$2"
630 [ -n "$names" ] || return 0
632 config_get ip "$cfg" ip "$3"
633 [ -n "$ip" ] || return 0
635 for name in $names; do
636 record="${record:+$record }$name"
639 echo "$ip $record" >> $HOSTFILE_TMP
645 config_get srv "$cfg" srv
646 [ -n "$srv" ] || return 0
648 config_get target "$cfg" target
649 [ -n "$target" ] || return 0
651 config_get port "$cfg" port
652 [ -n "$port" ] || return 0
654 config_get class "$cfg" class
655 config_get weight "$cfg" weight
657 local service="$srv,$target,$port${class:+,$class${weight:+,$weight}}"
659 xappend "--srv-host=$service"
664 local domain relay pref
666 config_get domain "$cfg" domain
667 [ -n "$domain" ] || return 0
669 config_get relay "$cfg" relay
670 [ -n "$relay" ] || return 0
672 config_get pref "$cfg" pref 0
674 local service="$domain,$relay,$pref"
676 xappend "--mx-host=$service"
683 config_get cname "$cfg" cname
684 [ -n "$cname" ] || return 0
686 config_get target "$cfg" target
687 [ -n "$target" ] || return 0
689 xappend "--cname=${cname},${target}"
692 dhcp_hostrecord_add() {
694 local names addresses record val
696 config_get names "$cfg" name "$2"
697 if [ -z "$names" ]; then
701 config_get addresses "$cfg" ip "$3"
702 if [ -z "$addresses" ]; then
706 for val in $names $addresses; do
707 record="${record:+$record,}$val"
710 xappend "--host-record=$record"
715 local local_addr server_addr interface
717 config_get local_addr "$cfg" local_addr
718 [ -n "$local_addr" ] || return 0
720 config_get server_addr "$cfg" server_addr
721 [ -n "$server_addr" ] || return 0
723 config_get interface "$cfg" interface
724 if [ -z "$interface" ]; then
725 xappend "--dhcp-relay=$local_addr,$server_addr"
727 network_get_device ifname "$interface" || return
728 xappend "--dhcp-relay=$local_addr,$server_addr,$ifname"
735 local disabled user_dhcpscript
736 local resolvfile localuse=0
738 config_get_bool disabled "$cfg" disabled 0
739 [ "$disabled" -gt 0 ] && return 0
741 # reset list of DOMAINS and DNS servers (for each dnsmasq instance)
744 CONFIGFILE="${BASECONFIGFILE}.${cfg}"
745 CONFIGFILE_TMP="${CONFIGFILE}.$$"
746 HOSTFILE="${BASEHOSTFILE}.${cfg}"
747 HOSTFILE_TMP="${HOSTFILE}.$$"
748 BASEDHCPSTAMPFILE_CFG="${BASEDHCPSTAMPFILE}.${cfg}"
750 # before we can call xappend
751 mkdir -p /var/run/dnsmasq/
752 mkdir -p $(dirname $CONFIGFILE)
753 mkdir -p $(dirname $HOSTFILE)
754 mkdir -p /var/lib/misc
755 chown dnsmasq:dnsmasq /var/run/dnsmasq
757 echo "# auto-generated config file from /etc/config/dhcp" > $CONFIGFILE_TMP
758 echo "# auto-generated config file from /etc/config/dhcp" > $HOSTFILE_TMP
760 local dnsmasqconffile="/etc/dnsmasq.${cfg}.conf"
761 if [ ! -r "$dnsmasqconffile" ]; then
762 dnsmasqconffile=/etc/dnsmasq.conf
765 # if we did this last, we could override auto-generated config
766 [ -f "${dnsmasqconffile}" ] && {
767 xappend "--conf-file=${dnsmasqconffile}"
770 $PROG --version | grep -osqE "^Compile time options:.* DHCPv6( |$)" && DHCPv6CAPABLE=1 || DHCPv6CAPABLE=0
773 if [ -x /usr/sbin/odhcpd -a -x /etc/init.d/odhcpd ] ; then
774 local odhcpd_is_main odhcpd_is_enabled
775 config_get odhcpd_is_main odhcpd maindhcp 0
776 /etc/init.d/odhcpd enabled && odhcpd_is_enabled=1 || odhcpd_is_enabled=0
779 if [ "$odhcpd_is_enabled" -eq 0 -a "$DHCPv6CAPABLE" -eq 1 ] ; then
780 # DHCP V4 and V6 in DNSMASQ
782 elif [ "$odhcpd_is_main" -gt 0 ] ; then
783 # ODHCPD is doing it all
786 # You have ODHCPD but use DNSMASQ for DHCPV4
790 elif [ "$DHCPv6CAPABLE" -eq 1 ] ; then
791 # DHCP V4 and V6 in DNSMASQ
797 # Allow DHCP/DHCPv6 to be handled by ISC DHCPD
798 if [ -x /usr/sbin/dhcpd ] ; then
799 if [ -x /etc/init.d/dhcpd ] ; then
800 /etc/init.d/dhcpd enabled && DNSMASQ_DHCP_VER=0
802 if [ -x /etc/init.d/dhcpd6 -a "$DNSMASQ_DHCP_VER" -gt 0 ] ; then
803 /etc/init.d/dhcpd6 enabled && DNSMASQ_DHCP_VER=4
807 append_bool "$cfg" authoritative "--dhcp-authoritative"
808 append_bool "$cfg" nodaemon "--no-daemon"
809 append_bool "$cfg" domainneeded "--domain-needed"
810 append_bool "$cfg" filterwin2k "--filterwin2k"
811 append_bool "$cfg" nohosts "--no-hosts"
812 append_bool "$cfg" nonegcache "--no-negcache"
813 append_bool "$cfg" strictorder "--strict-order"
814 append_bool "$cfg" logqueries "--log-queries=extra"
815 append_bool "$cfg" noresolv "--no-resolv"
816 append_bool "$cfg" localise_queries "--localise-queries"
817 append_bool "$cfg" readethers "--read-ethers"
818 append_bool "$cfg" dbus "--enable-dbus"
819 append_bool "$cfg" ubus "--enable-ubus" 1
820 append_bool "$cfg" expandhosts "--expand-hosts"
821 config_get tftp_root "$cfg" "tftp_root"
822 [ -n "$tftp_root" ] && mkdir -p "$tftp_root" && append_bool "$cfg" enable_tftp "--enable-tftp"
823 append_bool "$cfg" tftp_no_fail "--tftp-no-fail"
824 append_bool "$cfg" nonwildcard "--bind-dynamic" 1
825 append_bool "$cfg" fqdn "--dhcp-fqdn"
826 append_bool "$cfg" proxydnssec "--proxy-dnssec"
827 append_bool "$cfg" localservice "--local-service"
828 append_bool "$cfg" logdhcp "--log-dhcp"
829 append_bool "$cfg" quietdhcp "--quiet-dhcp"
830 append_bool "$cfg" sequential_ip "--dhcp-sequential-ip"
831 append_bool "$cfg" allservers "--all-servers"
832 append_bool "$cfg" noping "--no-ping"
834 append_parm "$cfg" logfacility "--log-facility"
836 append_parm "$cfg" cachesize "--cache-size"
837 append_parm "$cfg" dnsforwardmax "--dns-forward-max"
838 append_parm "$cfg" port "--port"
839 append_parm "$cfg" ednspacket_max "--edns-packet-max"
840 append_parm "$cfg" dhcpleasemax "--dhcp-lease-max"
841 append_parm "$cfg" "queryport" "--query-port"
842 append_parm "$cfg" "minport" "--min-port"
843 append_parm "$cfg" "maxport" "--max-port"
844 append_parm "$cfg" "domain" "--domain"
845 append_parm "$cfg" "local" "--server"
846 config_list_foreach "$cfg" "listen_address" append_listenaddress
847 config_list_foreach "$cfg" "server" append_server
848 config_list_foreach "$cfg" "rev_server" append_rev_server
849 config_list_foreach "$cfg" "address" append_address
850 config_list_foreach "$cfg" "ipset" append_ipset
852 config_list_foreach "$cfg" "interface" append_interface
853 config_list_foreach "$cfg" "notinterface" append_notinterface
855 config_list_foreach "$cfg" "addnhosts" append_addnhosts
856 config_list_foreach "$cfg" "bogusnxdomain" append_bogusnxdomain
857 append_parm "$cfg" "leasefile" "--dhcp-leasefile" "/tmp/dhcp.leases"
858 append_parm "$cfg" "serversfile" "--servers-file"
859 append_parm "$cfg" "tftp_root" "--tftp-root"
860 append_parm "$cfg" "dhcp_boot" "--dhcp-boot"
861 append_parm "$cfg" "local_ttl" "--local-ttl"
862 append_parm "$cfg" "pxe_prompt" "--pxe-prompt"
863 config_list_foreach "$cfg" "pxe_service" append_pxe_service
864 config_get DOMAIN "$cfg" domain
866 config_get_bool ADD_LOCAL_DOMAIN "$cfg" add_local_domain 1
867 config_get_bool ADD_LOCAL_HOSTNAME "$cfg" add_local_hostname 1
868 config_get ADD_LOCAL_FQDN "$cfg" add_local_fqdn ""
869 config_get ADD_WAN_FQDN "$cfg" add_wan_fqdn 0
871 if [ -z "$ADD_LOCAL_FQDN" ] ; then
872 # maintain support for previous UCI
873 ADD_LOCAL_FQDN="$ADD_LOCAL_HOSTNAME"
876 config_get user_dhcpscript $cfg dhcpscript
877 if has_handler || [ -n "$user_dhcpscript" ]; then
878 xappend "--dhcp-script=$DHCPSCRIPT"
881 config_get leasefile $cfg leasefile "/tmp/dhcp.leases"
882 [ -n "$leasefile" -a \! -e "$leasefile" ] && touch "$leasefile"
883 config_get_bool cachelocal "$cfg" cachelocal 1
885 config_get_bool noresolv "$cfg" noresolv 0
886 if [ "$noresolv" != "1" ]; then
887 config_get resolvfile "$cfg" resolvfile /tmp/resolv.conf.auto
888 [ -n "$resolvfile" -a ! -e "$resolvfile" ] && touch "$resolvfile"
889 xappend "--resolv-file=$resolvfile"
890 [ "$resolvfile" = "/tmp/resolv.conf.auto" ] && localuse=1
892 config_get_bool localuse "$cfg" localuse "$localuse"
894 config_get hostsfile "$cfg" dhcphostsfile
895 [ -e "$hostsfile" ] && xappend "--dhcp-hostsfile=$hostsfile"
898 config_get_bool rebind "$cfg" rebind_protection 1
899 [ $rebind -gt 0 ] && {
901 "DNS rebinding protection is active," \
902 "will discard upstream RFC1918 responses!"
903 xappend "--stop-dns-rebind"
905 local rebind_localhost
906 config_get_bool rebind_localhost "$cfg" rebind_localhost 0
907 [ $rebind_localhost -gt 0 ] && {
908 log_once "Allowing 127.0.0.0/8 responses"
909 xappend "--rebind-localhost-ok"
912 append_rebind_domain() {
913 log_once "Allowing RFC1918 responses for domain $1"
914 xappend "--rebind-domain-ok=$1"
917 config_list_foreach "$cfg" rebind_domain append_rebind_domain
920 config_get_bool dnssec "$cfg" dnssec 0
921 [ "$dnssec" -gt 0 ] && {
922 xappend "--conf-file=$TRUSTANCHORSFILE"
924 [ -x /etc/init.d/sysntpd ] && {
925 /etc/init.d/sysntpd enabled
926 [ "$?" -ne 0 -o "$(uci_get system.ntp.enabled)" = "1" ] && {
927 [ -f "$TIMEVALIDFILE" ] || xappend "--dnssec-no-timecheck"
930 append_bool "$cfg" dnsseccheckunsigned "--dnssec-check-unsigned"
933 config_get addmac "$cfg" addmac 0
934 [ "$addmac" != "0" ] && {
935 [ "$addmac" = "1" ] && addmac=
936 xappend "--add-mac${addmac:+="$addmac"}"
939 dhcp_option_add "$cfg" "" 0
940 dhcp_option_add "$cfg" "" 2
942 xappend "--dhcp-broadcast=tag:needs-broadcast"
944 xappend "--addn-hosts=$(dirname $HOSTFILE)"
946 config_get dnsmasqconfdir "$cfg" confdir "/tmp/dnsmasq.d"
947 xappend "--conf-dir=$dnsmasqconfdir"
948 dnsmasqconfdir="${dnsmasqconfdir%%,*}"
949 [ ! -d "$dnsmasqconfdir" ] && mkdir -p $dnsmasqconfdir
950 xappend "--user=dnsmasq"
951 xappend "--group=dnsmasq"
952 echo >> $CONFIGFILE_TMP
954 config_get_bool enable_tftp "$cfg" enable_tftp 0
955 [ "$enable_tftp" -gt 0 ] && {
956 config_get tftp_root "$cfg" tftp_root
957 append EXTRA_MOUNT $tftp_root
960 config_foreach filter_dnsmasq host dhcp_host_add "$cfg"
961 echo >> $CONFIGFILE_TMP
963 config_get_bool dhcpbogushostname "$cfg" dhcpbogushostname 1
964 [ "$dhcpbogushostname" -gt 0 ] && {
965 xappend "--dhcp-ignore-names=tag:dhcp_bogus_hostname"
966 [ -r "$DHCPBOGUSHOSTNAMEFILE" ] && xappend "--conf-file=$DHCPBOGUSHOSTNAMEFILE"
969 config_foreach filter_dnsmasq boot dhcp_boot_add "$cfg"
970 config_foreach filter_dnsmasq mac dhcp_mac_add "$cfg"
971 config_foreach filter_dnsmasq tag dhcp_tag_add "$cfg"
972 config_foreach filter_dnsmasq vendorclass dhcp_vendorclass_add "$cfg"
973 config_foreach filter_dnsmasq userclass dhcp_userclass_add "$cfg"
974 config_foreach filter_dnsmasq circuitid dhcp_circuitid_add "$cfg"
975 config_foreach filter_dnsmasq remoteid dhcp_remoteid_add "$cfg"
976 config_foreach filter_dnsmasq subscrid dhcp_subscrid_add "$cfg"
977 config_foreach filter_dnsmasq match dhcp_match_add "$cfg"
978 config_foreach filter_dnsmasq domain dhcp_domain_add "$cfg"
979 config_foreach filter_dnsmasq hostrecord dhcp_hostrecord_add "$cfg"
980 [ -n "$BOOT" ] || config_foreach filter_dnsmasq relay dhcp_relay_add "$cfg"
982 echo >> $CONFIGFILE_TMP
983 config_foreach filter_dnsmasq srvhost dhcp_srv_add "$cfg"
984 config_foreach filter_dnsmasq mxhost dhcp_mx_add "$cfg"
985 echo >> $CONFIGFILE_TMP
987 config_get_bool boguspriv "$cfg" boguspriv 1
988 [ "$boguspriv" -gt 0 ] && {
989 xappend "--bogus-priv"
990 [ -r "$RFC6761FILE" ] && xappend "--conf-file=$RFC6761FILE"
993 if [ "$DNSMASQ_DHCP_VER" -gt 4 ] ; then
994 # Enable RA feature for when/if it is constructed,
995 # and RA is selected per interface pool (RA, DHCP, or both),
996 # but no one (should) want RA broadcast in syslog
997 [ -n "$BOOT" ] || config_foreach filter_dnsmasq dhcp dhcp_add "$cfg"
998 xappend "--enable-ra"
1000 append_bool "$cfg" quietdhcp "--quiet-dhcp6"
1002 elif [ "$DNSMASQ_DHCP_VER" -gt 0 ] ; then
1003 [ -n "$BOOT" ] || config_foreach filter_dnsmasq dhcp dhcp_add "$cfg"
1007 echo >> $CONFIGFILE_TMP
1008 config_foreach filter_dnsmasq cname dhcp_cname_add "$cfg"
1009 echo >> $CONFIGFILE_TMP
1011 echo >> $CONFIGFILE_TMP
1012 mv -f $CONFIGFILE_TMP $CONFIGFILE
1013 mv -f $HOSTFILE_TMP $HOSTFILE
1015 [ "$localuse" -gt 0 ] && {
1016 rm -f /tmp/resolv.conf
1017 [ $ADD_LOCAL_DOMAIN -eq 1 ] && [ -n "$DOMAIN" ] && {
1018 echo "search $DOMAIN" >> /tmp/resolv.conf
1020 DNS_SERVERS="$DNS_SERVERS 127.0.0.1"
1021 for DNS_SERVER in $DNS_SERVERS ; do
1022 echo "nameserver $DNS_SERVER" >> /tmp/resolv.conf
1026 procd_open_instance $cfg
1027 procd_set_param command $PROG -C $CONFIGFILE -k -x /var/run/dnsmasq/dnsmasq."${cfg}".pid
1028 procd_set_param file $CONFIGFILE
1029 [ -n "$user_dhcpscript" ] && procd_set_param env USER_DHCPSCRIPT="$user_dhcpscript"
1030 procd_set_param respawn
1032 procd_add_jail dnsmasq ubus log
1033 procd_add_jail_mount $CONFIGFILE $TRUSTANCHORSFILE $HOSTFILE $RFC6761FILE $DHCPBOGUSHOSTNAMEFILE /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
1034 procd_add_jail_mount_rw /var/run/dnsmasq/ $leasefile
1036 procd_close_instance
1042 local noresolv resolvfile localuse=0
1044 config_get_bool noresolv "$cfg" noresolv 0
1045 config_get resolvfile "$cfg" "resolvfile"
1047 [ "$noresolv" = 0 -a "$resolvfile" = "/tmp/resolv.conf.auto" ] && localuse=1
1048 config_get_bool localuse "$cfg" localuse "$localuse"
1049 [ "$localuse" -gt 0 ] && ln -sf "/tmp/resolv.conf.auto" /tmp/resolv.conf
1051 rm -f ${BASEDHCPSTAMPFILE}.${cfg}.*.dhcp
1054 add_interface_trigger()
1056 local interface ignore
1058 config_get interface "$1" interface
1059 config_get_bool ignore "$1" ignore 0
1061 [ -n "$interface" -a $ignore -eq 0 ] && procd_add_interface_trigger "interface.*" "$interface" /etc/init.d/dnsmasq reload
1066 procd_add_reload_trigger "dhcp" "system"
1069 config_foreach add_interface_trigger dhcp
1070 config_foreach add_interface_trigger relay
1081 local instance_found=0
1083 . /lib/functions/network.sh
1088 if [ "$type" = "dnsmasq" ]; then
1089 if [ -n "$instance" -a "$instance" = "$name" ]; then
1097 if [ -n "$instance" ]; then
1098 [ "$instance_found" -gt 0 ] || return
1099 dnsmasq_start "$instance"
1101 config_foreach dnsmasq_start dnsmasq
1106 rc_procd start_service "$@"
1107 procd_send_signal dnsmasq "$@"
1112 local instance_found=0
1117 if [ "$type" = "dnsmasq" ]; then
1118 if [ -n "$instance" -a "$instance" = "$name" ]; then
1126 if [ -n "$instance" ]; then
1127 [ "$instance_found" -gt 0 ] || return
1128 dnsmasq_stop "$instance"
1130 config_foreach dnsmasq_stop dnsmasq