1 #!/bin/sh /etc/rc.common
2 # Copyright (C) 2007-2012 OpenWrt.org
16 BASECONFIGFILE
="/var/etc/dnsmasq.conf"
17 BASEHOSTFILE
="/tmp/hosts/dhcp"
18 TRUSTANCHORSFILE
="/usr/share/dnsmasq/trust-anchors.conf"
19 TIMEVALIDFILE
="/var/state/dnsmasqsec"
20 BASEDHCPSTAMPFILE
="/var/run/dnsmasq"
21 DHCPBOGUSHOSTNAMEFILE
="/usr/share/dnsmasq/dhcpbogushostname.conf"
22 RFC6761FILE
="/usr/share/dnsmasq/rfc6761.conf"
23 DHCPSCRIPT
="/usr/lib/dnsmasq/dhcp-script.sh"
24 DHCPSCRIPT_DEPENDS
="/usr/share/libubox/jshn.sh /usr/bin/jshn /bin/ubus"
28 dnsmasq_ignore_opt
() {
31 if [ -z "$dnsmasq_features" ]; then
32 dnsmasq_features
="$(dnsmasq --version | grep -m1 'Compile time options:' | cut -d: -f2) "
33 [ "${dnsmasq_features#* DHCP }" = "$dnsmasq_features" ] || dnsmasq_has_dhcp
=1
34 [ "${dnsmasq_features#* DHCPv6 }" = "$dnsmasq_features" ] || dnsmasq_has_dhcp6
=1
35 [ "${dnsmasq_features#* DNSSEC }" = "$dnsmasq_features" ] || dnsmasq_has_dnssec
=1
36 [ "${dnsmasq_features#* TFTP }" = "$dnsmasq_features" ] || dnsmasq_has_tftp
=1
37 [ "${dnsmasq_features#* ipset }" = "$dnsmasq_features" ] || dnsmasq_has_ipset
=1
38 [ "${dnsmasq_features#* nftset }" = "$dnsmasq_features" ] || dnsmasq_has_nftset
=1
44 [ -z "$dnsmasq_has_dhcp6" ] ;;
48 [ -z "$dnsmasq_has_dhcp" ] ;;
51 if [ -z "$dnsmasq_has_dnssec" ]; then
52 echo "dnsmasq: \"$opt\" requested, but dnssec support is not available" >&2
58 [ -z "$dnsmasq_has_tftp" ] ;;
60 [ -z "$dnsmasq_has_ipset" ] ;;
62 [ -z "$dnsmasq_has_nftset" ] ;;
70 local opt
="${value%%=*}"
72 if ! dnsmasq_ignore_opt
"$opt"; then
73 echo "$value" >>$CONFIGFILE_TMP
79 local hex
="${2#0x}" # strip optional "0x" prefix
81 if [ -n "${hex//[0-9a-fA-F]/}" ]; then
82 # is invalid hex literal
86 # convert into host id
89 $(((0x$hex >> 16) % 65536)) \
100 while [ -n "$ip" ]; do
102 res
="$(($res * 256))"
103 res
="$(($res + $part))"
104 [ "${ip%.*}" != "$ip" ] && ip
="${ip#*.}" || ip
=
111 local stamp
="${BASEDHCPSTAMPFILE_CFG}.${ifname}.dhcp"
114 [ -s "$stamp" ] && return $
(cat "$stamp")
116 # If interface is down, skip it.
117 # The init script will be called again once the link is up
118 case "$(devstatus "$ifname" | jsonfilter -e @.up)" in
122 udhcpc
-n -q -s /bin
/true
-t 1 -i "$ifname" >&- && rv
=1 || rv
=0
129 pidof dnsmasq
>/dev
/null || \
130 logger
-t dnsmasq
"$@"
136 for file in /etc
/hotplug.d
/dhcp
/* /etc
/hotplug.d
/tftp
/* /etc
/hotplug.d
/neigh
/*; do
137 [ -f "$file" ] && return 0
149 [ -z "$default" ] && default
="0"
150 config_get_bool _loctmp
"$section" "$option" "$default"
151 [ $_loctmp -gt 0 ] && xappend
"$value"
160 config_get _loctmp
"$section" "$option" "$default"
161 [ -z "$_loctmp" ] && return 0
162 xappend
"$switch=$_loctmp"
166 xappend
"--server=$1"
169 append_rev_server
() {
170 xappend
"--rev-server=$1"
174 xappend
"--address=$1"
177 append_connmark_allowlist
() {
178 xappend
"--connmark-allowlist=$1"
182 network_get_device ifname
"$1" || ifname
="$1"
183 xappend
"--interface=$ifname"
186 append_listenaddress
() {
187 xappend
"--listen-address=$1"
190 append_notinterface
() {
191 network_get_device ifname
"$1" || ifname
="$1"
192 xappend
"--except-interface=$ifname"
198 for dirname in $EXTRA_MOUNT ; do
200 "${dirname}/"* |
"${dirname}" )
209 append_extramount
() {
210 ismounted
"$1" || append EXTRA_MOUNT
"$1"
214 append_extramount
"$1"
215 xappend
"--addn-hosts=$1"
218 append_bogusnxdomain
() {
219 xappend
"--bogus-nxdomain=$1"
222 append_pxe_service
() {
223 xappend
"--pxe-service=$1"
226 append_interface_name
() {
227 xappend
"--interface-name=$1,$2"
231 local cfg
="$1" func
="$2" match_cfg
="$3" found_cfg
233 # use entry when no instance entry set, or if it matches
234 config_get found_cfg
"$cfg" "instance"
235 if [ -z "$found_cfg" ] ||
[ "$found_cfg" = "$match_cfg" ]; then
240 dhcp_subscrid_add
() {
243 config_get networkid
"$cfg" networkid
244 [ -n "$networkid" ] ||
return 0
246 config_get subscriberid
"$cfg" subscriberid
247 [ -n "$subscriberid" ] ||
return 0
249 xappend
"--dhcp-subscrid=set:$networkid,$subscriberid"
251 config_get_bool force
"$cfg" force
0
253 dhcp_option_add
"$cfg" "$networkid" "$force"
256 dhcp_remoteid_add
() {
259 config_get networkid
"$cfg" networkid
260 [ -n "$networkid" ] ||
return 0
262 config_get remoteid
"$cfg" remoteid
263 [ -n "$remoteid" ] ||
return 0
265 xappend
"--dhcp-remoteid=set:$networkid,$remoteid"
267 config_get_bool force
"$cfg" force
0
269 dhcp_option_add
"$cfg" "$networkid" "$force"
272 dhcp_circuitid_add
() {
273 # TODO: DHCPV6 does not have circuitid; catch "option6:"
276 config_get networkid
"$cfg" networkid
277 [ -n "$networkid" ] ||
return 0
279 config_get circuitid
"$cfg" circuitid
280 [ -n "$circuitid" ] ||
return 0
282 xappend
"--dhcp-circuitid=set:$networkid,$circuitid"
284 config_get_bool force
"$cfg" force
0
286 dhcp_option_add
"$cfg" "$networkid" "$force"
289 dhcp_userclass_add
() {
292 config_get networkid
"$cfg" networkid
293 [ -n "$networkid" ] ||
return 0
295 config_get userclass
"$cfg" userclass
296 [ -n "$userclass" ] ||
return 0
298 xappend
"--dhcp-userclass=set:$networkid,$userclass"
300 config_get_bool force
"$cfg" force
0
302 dhcp_option_add
"$cfg" "$networkid" "$force"
305 dhcp_vendorclass_add
() {
306 # TODO: DHCPV6 vendor class has stricter definitions; catch? fixup?
309 config_get networkid
"$cfg" networkid
310 [ -n "$networkid" ] ||
return 0
312 config_get vendorclass
"$cfg" vendorclass
313 [ -n "$vendorclass" ] ||
return 0
315 xappend
"--dhcp-vendorclass=set:$networkid,$vendorclass"
317 config_get_bool force
"$cfg" force
0
319 dhcp_option_add
"$cfg" "$networkid" "$force"
325 config_get networkid
"$cfg" networkid
326 [ -n "$networkid" ] ||
return 0
328 config_get match
"$cfg" match
329 [ -n "$match" ] ||
return 0
331 xappend
"--dhcp-match=set:$networkid,$match"
333 config_get_bool force
"$cfg" force
0
335 dhcp_option_add
"$cfg" "$networkid" "$force"
340 local hosttag nametime addrs duids macs tags mtags
342 config_get_bool force
"$cfg" force
0
344 config_get networkid
"$cfg" networkid
345 [ -n "$networkid" ] && dhcp_option_add
"$cfg" "$networkid" "$force"
347 config_get_bool
enable "$cfg" enable 1
348 [ "$enable" = "0" ] && return 0
350 config_get name
"$cfg" name
351 config_get ip
"$cfg" ip
352 config_get hostid
"$cfg" hostid
354 [ -z "$ip" ] && [ -z "$name" ] && [ -z "$hostid" ] && return 0
356 config_get_bool dns
"$cfg" dns
0
357 [ "$dns" = "1" ] && [ -n "$ip" ] && [ -n "$name" ] && {
358 echo "$ip $name${DOMAIN:+.$DOMAIN}" >> $HOSTFILE_TMP
361 config_get mac
"$cfg" mac
362 config_get duid
"$cfg" duid
363 config_get tag
"$cfg" tag
366 mtags
="${mtags}tag:$1,"
368 config_list_foreach
"$cfg" match_tag add_tag
370 if [ -n "$mac" ]; then
371 # --dhcp-host=00:20:e0:3b:13:af,192.168.0.199,lap
372 # many MAC are possible to track a laptop ON/OFF dock
373 for m
in $mac; do append macs
"$m" ","; done
376 if [ $DNSMASQ_DHCP_VER -eq 6 ] && [ -n "$duid" ]; then
377 # --dhcp-host=id:00:03:00:01:12:00:00:01:02:03,[::beef],lap
378 # one (virtual) machine gets one DUID per RFC3315
379 duids
="id:${duid// */}"
382 if [ -z "$macs" ] && [ -z "$duids" ]; then
383 # --dhcp-host=lap,192.168.0.199,[::beef]
384 [ -n "$name" ] ||
return 0
389 if [ -n "$hostid" ]; then
390 hex_to_hostid hostid
"$hostid"
393 if [ -n "$tag" ]; then
394 for t
in $tag; do append tags
"$t" ",set:"; done
397 config_get_bool broadcast
"$cfg" broadcast
0
398 config_get leasetime
"$cfg" leasetime
400 [ "$broadcast" = "0" ] && broadcast
= || broadcast
=",set:needs-broadcast"
402 hosttag
="${networkid:+,set:${networkid}}${tags:+,set:${tags}}$broadcast"
403 nametime
="${name:+,$name}${leasetime:+,$leasetime}"
405 if [ $DNSMASQ_DHCP_VER -eq 6 ]; then
406 addrs
="${ip:+,$ip}${hostid:+,[::$hostid]}"
407 xappend
"--dhcp-host=$mtags$macs${duids:+,$duids}$hosttag$addrs$nametime"
409 xappend
"--dhcp-host=$mtags$macs$hosttag${ip:+,$ip}$nametime"
413 dhcp_this_host_add
() {
417 local routerstub routername ifdashname
418 local lanaddr lanaddr6 lanaddrs6 ulaprefix
420 if [ "$mode" -gt 0 ] ; then
421 ifdashname
="${ifname//./-}"
422 routerstub
="$( md5sum /etc/os-release )"
423 routerstub
="router-${routerstub// */}"
424 routername
="$( uci_get system @system[0] hostname $routerstub )"
426 if [ "$mode" -gt 1 ] ; then
427 if [ "$mode" -gt 2 ] ; then
428 if [ "$mode" -gt 3 ] ; then
429 append_interface_name
"$ifdashname.$routername.$DOMAIN" "$ifname"
432 append_interface_name
"$routername.$DOMAIN" "$ifname"
435 # All IP addresses discovered by dnsmasq will be labeled (except fe80::)
436 append_interface_name
"$routername" "$ifname"
439 # This uses a static host file entry for only limited addresses.
440 # Use dnsmasq option "--expandhosts" to enable FQDN on host files.
441 ulaprefix
="$(uci_get network @globals[0] ula_prefix)"
442 network_get_ipaddr lanaddr
"$net"
443 network_get_ipaddrs6 lanaddrs6
"$net"
445 if [ -n "$lanaddr" ] ; then
446 dhcp_domain_add
"" "$routername" "$lanaddr"
449 if [ -n "$ulaprefix" ] && [ -n "$lanaddrs6" ] ; then
450 for lanaddr6
in $lanaddrs6 ; do
452 "${ulaprefix%%:/*}"*)
453 dhcp_domain_add
"" "$routername" "$lanaddr6"
463 # NOTE: dnsmasq has explicit "option6:" prefix for DHCPv6 so no collisions
468 [ -n "$tag" ] ||
return 0
470 config_get_bool force
"$cfg" force
0
471 [ "$force" = "0" ] && force
=
473 config_get option
"$cfg" dhcp_option
475 xappend
"--dhcp-option${force:+-force}=tag:$tag,$o"
482 config_get networkid
"$cfg" networkid
483 [ -n "$networkid" ] ||
return 0
485 config_get mac
"$cfg" mac
486 [ -n "$mac" ] ||
return 0
488 xappend
"--dhcp-mac=$networkid,$mac"
490 dhcp_option_add
"$cfg" "$networkid"
494 # TODO: BOOTURL is different between DHCPv4 and DHCPv6
497 config_get networkid
"$cfg" networkid
499 config_get filename
"$cfg" filename
500 [ -n "$filename" ] ||
return 0
502 config_get servername
"$cfg" servername
503 config_get serveraddress
"$cfg" serveraddress
505 [ -n "$serveraddress" ] && [ ! -n "$servername" ] && return 0
507 xappend
"--dhcp-boot=${networkid:+net:$networkid,}${filename}${servername:+,$servername}${serveraddress:+,$serveraddress}"
509 config_get_bool force
"$cfg" force
0
511 dhcp_option_add
"$cfg" "$networkid" "$force"
516 local dhcp6range
="::"
520 config_get net
"$cfg" interface
521 [ -n "$net" ] ||
return 0
523 config_get networkid
"$cfg" networkid
524 [ -n "$networkid" ] || networkid
="$net"
526 network_get_device ifname
"$net" ||
return 0
528 [ "$cachelocal" = "0" ] && network_get_dnsserver dnsserver
"$net" && {
529 DNS_SERVERS
="$DNS_SERVERS $dnsserver"
532 append_bool
"$cfg" ignore
"--no-dhcp-interface=$ifname" && {
533 # Many ISP do not have useful names for DHCP customers (your WAN).
534 dhcp_this_host_add
"$net" "$ifname" "$ADD_WAN_FQDN"
538 network_get_subnet subnet
"$net" ||
return 0
539 network_get_protocol proto
"$net" ||
return 0
541 # Do not support non-static interfaces for now
542 [ static
= "$proto" ] ||
return 0
544 # Override interface netmask with dhcp config if applicable
545 config_get netmask
"$cfg" netmask
"${subnet##*/}"
547 #check for an already active dhcp server on the interface, unless 'force' is set
548 config_get_bool force
"$cfg" force
0
549 [ $force -gt 0 ] || dhcp_check
"$ifname" ||
{
551 "found already running DHCP-server on interface '$ifname'" \
552 "refusing to start, use 'option force 1' to override"
556 config_get start
"$cfg" start
100
557 config_get limit
"$cfg" limit
150
558 config_get leasetime
"$cfg" leasetime
12h
559 config_get options
"$cfg" options
560 config_get_bool dynamicdhcp
"$cfg" dynamicdhcp
1
561 config_get_bool dynamicdhcpv4
"$cfg" dynamicdhcpv4
$dynamicdhcp
562 config_get_bool dynamicdhcpv6
"$cfg" dynamicdhcpv6
$dynamicdhcp
564 config_get dhcpv4
"$cfg" dhcpv4
565 config_get dhcpv6
"$cfg" dhcpv6
567 config_get ra
"$cfg" ra
568 config_get ra_management
"$cfg" ra_management
569 config_get ra_preference
"$cfg" ra_preference
570 config_get dns
"$cfg" dns
572 config_list_foreach
"$cfg" "interface_name" append_interface_name
"$ifname"
574 # Put the router host name on this DHCP served interface address(es)
575 dhcp_this_host_add
"$net" "$ifname" "$ADD_LOCAL_FQDN"
577 start
="$( dhcp_calc "$start" )"
580 tags
="${tags}tag:$1,"
582 config_list_foreach
"$cfg" tag add_tag
584 nettag
="${networkid:+set:${networkid},}"
586 if [ "$limit" -gt 0 ] ; then
590 # make sure the DHCP range is not empty
591 if [ "$dhcpv4" != "disabled" ] && ipcalc
"${subnet%%/*}" "$netmask" "$start" "$limit" ; then
592 [ "$dynamicdhcpv4" = "0" ] && END
="static"
594 xappend
"--dhcp-range=$tags$nettag$START,$END,$NETMASK,$leasetime${options:+ $options}"
597 if [ "$dynamicdhcpv6" = "0" ] ; then
598 dhcp6range
="::,static"
600 dhcp6range
="::1000,::ffff"
604 if [ $DNSMASQ_DHCP_VER -eq 6 ] && [ "$ra" = "server" ] ; then
605 # Note: dnsmasq cannot just be a DHCPv6 server (all-in-1)
606 # and let some other machine(s) send RA pointing to it.
608 case $ra_preference in
610 xappend
"--ra-param=$ifname,high,0,7200"
613 xappend
"--ra-param=$ifname,low,0,7200"
616 # Send UNSOLICITED RA at default interval and live for 2 hours.
617 # TODO: convert flexible lease time into route life time (only seconds).
618 xappend
"--ra-param=$ifname,0,7200"
622 if [ "$dhcpv6" = "disabled" ] ; then
627 case $ra_management in
629 # SLACC with DCHP for extended options
630 xappend
"--dhcp-range=$nettag::,constructor:$ifname,ra-stateless,ra-names"
633 # DHCP address and RA only for management redirection
634 xappend
"--dhcp-range=$nettag$dhcp6range,constructor:$ifname,$leasetime"
637 # SLAAC only but dnsmasq attempts to link HOSTNAME, DHCPv4 MAC, and SLAAC
638 xappend
"--dhcp-range=$nettag::,constructor:$ifname,ra-only,ra-names"
641 # SLAAC and full DHCP
642 xappend
"--dhcp-range=$nettag$dhcp6range,constructor:$ifname,slaac,ra-names,$leasetime"
646 if [ -n "$dns" ]; then
648 for d
in $dns; do append dnss
"[$d]" ","; done
653 dhcp_option_append
"option6:dns-server,$dnss" "$networkid"
656 dhcp_option_add
"$cfg" "$networkid" 0
657 dhcp_option_add
"$cfg" "$networkid" 2
660 dhcp_option_append
() {
665 xappend
"--dhcp-option${force:+-force}=${networkid:+$networkid,}$option"
669 # NOTE: dnsmasq has explicit "option6:" prefix for DHCPv6 so no collisions
673 local opt
="dhcp_option"
675 [ "$force" = "0" ] && force
=
676 [ "$force" = "2" ] && opt
="dhcp_option_force"
679 config_get list_len
"$cfg" "${opt}_LENGTH"
681 if [ -n "$list_len" ]; then
682 config_list_foreach
"$cfg" "$opt" dhcp_option_append
"$networkid" "$force"
684 config_get dhcp_option
"$cfg" "$opt"
686 [ -n "$dhcp_option" ] && echo "Warning: the 'option $opt' syntax is deprecated, use 'list $opt'" >&2
689 for option
in $dhcp_option; do
690 dhcp_option_append
"$option" "$networkid" "$force"
697 local ip name names record
699 config_get names
"$cfg" name
"$2"
700 [ -n "$names" ] ||
return 0
702 config_get ip
"$cfg" ip
"$3"
703 [ -n "$ip" ] ||
return 0
705 for name
in $names; do
706 record
="${record:+$record }$name"
709 echo "$ip $record" >> $HOSTFILE_TMP
715 config_get srv
"$cfg" srv
716 [ -n "$srv" ] ||
return 0
718 config_get target
"$cfg" target
719 [ -n "$target" ] ||
return 0
721 config_get port
"$cfg" port
722 [ -n "$port" ] ||
return 0
724 config_get class
"$cfg" class
725 config_get weight
"$cfg" weight
727 local service
="$srv,$target,$port${class:+,$class${weight:+,$weight}}"
729 xappend
"--srv-host=$service"
734 local domain relay pref
736 config_get domain
"$cfg" domain
737 [ -n "$domain" ] ||
return 0
739 config_get relay
"$cfg" relay
740 [ -n "$relay" ] ||
return 0
742 config_get pref
"$cfg" pref
0
744 local service
="$domain,$relay,$pref"
746 xappend
"--mx-host=$service"
753 config_get cname
"$cfg" cname
754 [ -n "$cname" ] ||
return 0
756 config_get target
"$cfg" target
757 [ -n "$target" ] ||
return 0
759 xappend
"--cname=${cname},${target}"
762 dhcp_hostrecord_add
() {
764 local names addresses record val
766 config_get names
"$cfg" name
"$2"
767 if [ -z "$names" ]; then
771 config_get addresses
"$cfg" ip
"$3"
772 if [ -z "$addresses" ]; then
776 for val
in $names $addresses; do
777 record
="${record:+$record,}$val"
780 xappend
"--host-record=$record"
785 local local_addr server_addr interface
787 config_get local_addr
"$cfg" local_addr
788 [ -n "$local_addr" ] ||
return 0
790 config_get server_addr
"$cfg" server_addr
791 [ -n "$server_addr" ] ||
return 0
793 config_get interface
"$cfg" interface
794 if [ -z "$interface" ]; then
795 xappend
"--dhcp-relay=$local_addr,$server_addr"
797 network_get_device ifname
"$interface" ||
return
798 xappend
"--dhcp-relay=$local_addr,$server_addr,$ifname"
802 dnsmasq_ipset_add
() {
804 local ipsets nftsets domains
807 ipsets
="${ipsets:+$ipsets,}$1"
814 [ -n "$fam" ] || fam
=$
(echo "$set" |
sed -nre \
815 's#^.*[^0-9]([46])$|^.*[-_]([46])[-_].*$|^([46])[^0-9].*$#\1\2\3#p')
817 fam
=$
(nft
-t list
set "$table_family" "$table" "$set" 2>&1 |
sed -nre \
818 's#^\t\ttype .*\bipv([46])_addr\b.*$#\1#p')
821 logger
-t dnsmasq
"Cannot infer address family from non-existent nftables set '$set'"
823 nftsets
="${nftsets:+$nftsets,}${fam:+$fam#}$table_family#$table#$set"
828 # leading '/' is expected
829 domains
="$domains/$1"
832 config_get table
"$cfg" table
'fw4'
833 config_get table_family
"$cfg" table_family
'inet'
834 if [ "$table_family" = "ip" ] ; then
836 elif [ "$table_family" = "ip6" ] ; then
839 config_get family
"$cfg" family
842 config_list_foreach
"$cfg" "name" add_ipset
843 config_list_foreach
"$cfg" "name" add_nftset
844 config_list_foreach
"$cfg" "domain" add_domain
846 if [ -z "$ipsets" ] ||
[ -z "$nftsets" ] ||
[ -z "$domains" ]; then
850 xappend
"--ipset=$domains/$ipsets"
851 xappend
"--nftset=$domains/$nftsets"
857 local disabled user_dhcpscript logfacility
858 local resolvfile resolvdir localuse
=0
860 config_get_bool disabled
"$cfg" disabled
0
861 [ "$disabled" -gt 0 ] && return 0
863 # reset list of DOMAINS, DNS servers and EXTRA mounts (for each dnsmasq instance)
867 CONFIGFILE
="${BASECONFIGFILE}.${cfg}"
868 CONFIGFILE_TMP
="${CONFIGFILE}.$$"
869 HOSTFILE
="${BASEHOSTFILE}.${cfg}"
870 HOSTFILE_TMP
="${HOSTFILE}.$$"
871 HOSTFILE_DIR
="$(dirname "$HOSTFILE")"
872 BASEDHCPSTAMPFILE_CFG
="${BASEDHCPSTAMPFILE}.${cfg}"
874 # before we can call xappend
875 umask u
=rwx
,g
=rx
,o
=rx
876 mkdir
-p /var
/run
/dnsmasq
/
877 mkdir
-p $
(dirname $CONFIGFILE)
878 mkdir
-p "$HOSTFILE_DIR"
879 mkdir
-p /var
/lib
/misc
880 chown dnsmasq
:dnsmasq
/var
/run
/dnsmasq
882 echo "# auto-generated config file from /etc/config/dhcp" > $CONFIGFILE_TMP
883 echo "# auto-generated config file from /etc/config/dhcp" > $HOSTFILE_TMP
885 local dnsmasqconffile
="/etc/dnsmasq.${cfg}.conf"
886 if [ ! -r "$dnsmasqconffile" ]; then
887 dnsmasqconffile
=/etc
/dnsmasq.conf
890 # if we did this last, we could override auto-generated config
891 [ -f "${dnsmasqconffile}" ] && {
892 xappend
"--conf-file=${dnsmasqconffile}"
895 $PROG --version |
grep -osqE "^Compile time options:.* DHCPv6( |$)" && DHCPv6CAPABLE
=1 || DHCPv6CAPABLE
=0
898 if [ -x /usr
/sbin
/odhcpd
] && [ -x /etc
/init.d
/odhcpd
] ; then
899 local odhcpd_is_main odhcpd_is_enabled
900 config_get odhcpd_is_main odhcpd maindhcp
0
901 /etc
/init.d
/odhcpd enabled
&& odhcpd_is_enabled
=1 || odhcpd_is_enabled
=0
904 if [ "$odhcpd_is_enabled" -eq 0 ] && [ "$DHCPv6CAPABLE" -eq 1 ] ; then
905 # DHCP V4 and V6 in DNSMASQ
907 elif [ "$odhcpd_is_main" -gt 0 ] ; then
908 # ODHCPD is doing it all
911 # You have ODHCPD but use DNSMASQ for DHCPV4
915 elif [ "$DHCPv6CAPABLE" -eq 1 ] ; then
916 # DHCP V4 and V6 in DNSMASQ
922 # Allow DHCP/DHCPv6 to be handled by ISC DHCPD
923 if [ -x /usr
/sbin
/dhcpd
] ; then
924 if [ -x /etc
/init.d
/dhcpd
] ; then
925 /etc
/init.d
/dhcpd enabled
&& DNSMASQ_DHCP_VER
=0
927 if [ -x /etc
/init.d
/dhcpd6
] && [ "$DNSMASQ_DHCP_VER" -gt 0 ] ; then
928 /etc
/init.d
/dhcpd6 enabled
&& DNSMASQ_DHCP_VER
=4
932 append_bool
"$cfg" authoritative
"--dhcp-authoritative"
933 append_bool
"$cfg" nodaemon
"--no-daemon"
934 append_bool
"$cfg" domainneeded
"--domain-needed"
935 append_bool
"$cfg" filterwin2k
"--filterwin2k"
936 append_bool
"$cfg" nohosts
"--no-hosts"
937 append_bool
"$cfg" nonegcache
"--no-negcache"
938 append_bool
"$cfg" strictorder
"--strict-order"
939 append_bool
"$cfg" logqueries
"--log-queries=extra"
940 append_bool
"$cfg" noresolv
"--no-resolv"
941 append_bool
"$cfg" localise_queries
"--localise-queries"
942 append_bool
"$cfg" readethers
"--read-ethers"
944 local instance_name
="dnsmasq.$cfg"
945 if [ "$cfg" = "$DEFAULT_INSTANCE" ]; then
946 instance_name
="dnsmasq"
948 config_get_bool dbus
"$cfg" "dbus" 0
949 [ $dbus -gt 0 ] && xappend
"--enable-dbus=uk.org.thekelleys.$instance_name"
950 config_get_bool ubus
"$cfg" "ubus" 1
951 [ $ubus -gt 0 ] && xappend
"--enable-ubus=$instance_name"
953 append_bool
"$cfg" expandhosts
"--expand-hosts"
954 config_get tftp_root
"$cfg" "tftp_root"
955 [ -n "$tftp_root" ] && mkdir
-p "$tftp_root" && append_bool
"$cfg" enable_tftp
"--enable-tftp"
956 append_bool
"$cfg" tftp_no_fail
"--tftp-no-fail"
957 append_bool
"$cfg" nonwildcard
"--bind-dynamic" 1
958 append_bool
"$cfg" fqdn
"--dhcp-fqdn"
959 append_bool
"$cfg" proxydnssec
"--proxy-dnssec"
960 append_bool
"$cfg" localservice
"--local-service"
961 append_bool
"$cfg" logdhcp
"--log-dhcp"
962 append_bool
"$cfg" quietdhcp
"--quiet-dhcp"
963 append_bool
"$cfg" sequential_ip
"--dhcp-sequential-ip"
964 append_bool
"$cfg" allservers
"--all-servers"
965 append_bool
"$cfg" noping
"--no-ping"
966 append_bool
"$cfg" rapidcommit
"--dhcp-rapid-commit"
967 append_bool
"$cfg" scriptarp
"--script-arp"
969 append_bool
"$cfg" filter_aaaa
"--filter-AAAA"
970 append_bool
"$cfg" filter_a
"--filter-A"
972 append_parm
"$cfg" logfacility
"--log-facility"
973 config_get logfacility
"$cfg" "logfacility"
974 append_parm
"$cfg" cachesize
"--cache-size"
975 append_parm
"$cfg" dnsforwardmax
"--dns-forward-max"
976 append_parm
"$cfg" port
"--port"
977 append_parm
"$cfg" ednspacket_max
"--edns-packet-max"
978 append_parm
"$cfg" dhcpleasemax
"--dhcp-lease-max"
979 append_parm
"$cfg" "queryport" "--query-port"
980 append_parm
"$cfg" "minport" "--min-port"
981 append_parm
"$cfg" "maxport" "--max-port"
982 append_parm
"$cfg" "domain" "--domain"
983 append_parm
"$cfg" "local" "--local"
984 config_list_foreach
"$cfg" "listen_address" append_listenaddress
985 config_list_foreach
"$cfg" "server" append_server
986 config_list_foreach
"$cfg" "rev_server" append_rev_server
987 config_list_foreach
"$cfg" "address" append_address
989 local connmark_allowlist_enable
990 config_get connmark_allowlist_enable
"$cfg" connmark_allowlist_enable
0
991 [ "$connmark_allowlist_enable" -gt 0 ] && {
992 append_parm
"$cfg" "connmark_allowlist_enable" "--connmark-allowlist-enable"
993 config_list_foreach
"$cfg" "connmark_allowlist" append_connmark_allowlist
997 config_list_foreach
"$cfg" "interface" append_interface
998 config_list_foreach
"$cfg" "notinterface" append_notinterface
1000 config_get_bool ignore_hosts_dir
"$cfg" ignore_hosts_dir
0
1001 if [ "$ignore_hosts_dir" = "1" ]; then
1002 xappend
"--addn-hosts=$HOSTFILE"
1003 append EXTRA_MOUNT
"$HOSTFILE"
1005 xappend
"--addn-hosts=$HOSTFILE_DIR"
1006 append EXTRA_MOUNT
"$HOSTFILE_DIR"
1008 config_list_foreach
"$cfg" "addnhosts" append_addnhosts
1009 config_list_foreach
"$cfg" "bogusnxdomain" append_bogusnxdomain
1010 append_parm
"$cfg" "leasefile" "--dhcp-leasefile" "/tmp/dhcp.leases"
1013 config_get serversfile
"$cfg" "serversfile"
1014 [ -n "$serversfile" ] && {
1015 xappend
"--servers-file=$serversfile"
1016 append EXTRA_MOUNT
"$serversfile"
1019 append_parm
"$cfg" "tftp_root" "--tftp-root"
1020 append_parm
"$cfg" "dhcp_boot" "--dhcp-boot"
1021 append_parm
"$cfg" "local_ttl" "--local-ttl"
1022 append_parm
"$cfg" "max_ttl" "--max-ttl"
1023 append_parm
"$cfg" "min_cache_ttl" "--min-cache-ttl"
1024 append_parm
"$cfg" "max_cache_ttl" "--max-cache-ttl"
1025 append_parm
"$cfg" "pxe_prompt" "--pxe-prompt"
1026 append_parm
"$cfg" "tftp_unique_root" "--tftp-unique-root"
1027 config_list_foreach
"$cfg" "pxe_service" append_pxe_service
1028 config_get DOMAIN
"$cfg" domain
1030 config_get_bool ADD_LOCAL_DOMAIN
"$cfg" add_local_domain
1
1031 config_get_bool ADD_LOCAL_HOSTNAME
"$cfg" add_local_hostname
1
1032 config_get ADD_LOCAL_FQDN
"$cfg" add_local_fqdn
""
1033 config_get ADD_WAN_FQDN
"$cfg" add_wan_fqdn
0
1035 if [ -z "$ADD_LOCAL_FQDN" ] ; then
1036 # maintain support for previous UCI
1037 ADD_LOCAL_FQDN
="$ADD_LOCAL_HOSTNAME"
1040 config_get user_dhcpscript
$cfg dhcpscript
1041 if has_handler ||
[ -n "$user_dhcpscript" ]; then
1042 xappend
"--dhcp-script=$DHCPSCRIPT"
1043 xappend
"--script-arp"
1046 config_get leasefile
$cfg leasefile
"/tmp/dhcp.leases"
1047 [ -n "$leasefile" ] && [ ! -e "$leasefile" ] && touch "$leasefile"
1048 config_get_bool cachelocal
"$cfg" cachelocal
1
1050 config_get_bool noresolv
"$cfg" noresolv
0
1051 if [ "$noresolv" != "1" ]; then
1052 config_get resolvfile
"$cfg" resolvfile
/tmp
/resolv.conf.d
/resolv.conf.auto
1053 [ -n "$resolvfile" ] && [ ! -e "$resolvfile" ] && touch "$resolvfile"
1054 xappend
"--resolv-file=$resolvfile"
1055 [ "$resolvfile" = "/tmp/resolv.conf.d/resolv.conf.auto" ] && localuse
=1
1056 resolvdir
="$(dirname "$resolvfile")"
1058 config_get_bool localuse
"$cfg" localuse
"$localuse"
1060 config_get hostsfile
"$cfg" dhcphostsfile
1061 [ -e "$hostsfile" ] && xappend
"--dhcp-hostsfile=$hostsfile"
1064 config_get_bool rebind
"$cfg" rebind_protection
1
1065 [ $rebind -gt 0 ] && {
1067 "DNS rebinding protection is active," \
1068 "will discard upstream RFC1918 responses!"
1069 xappend
"--stop-dns-rebind"
1071 local rebind_localhost
1072 config_get_bool rebind_localhost
"$cfg" rebind_localhost
0
1073 [ $rebind_localhost -gt 0 ] && {
1074 log_once
"Allowing 127.0.0.0/8 responses"
1075 xappend
"--rebind-localhost-ok"
1078 append_rebind_domain
() {
1079 log_once
"Allowing RFC1918 responses for domain $1"
1080 xappend
"--rebind-domain-ok=$1"
1083 config_list_foreach
"$cfg" rebind_domain append_rebind_domain
1086 config_get_bool dnssec
"$cfg" dnssec
0
1087 [ "$dnssec" -gt 0 ] && {
1088 xappend
"--conf-file=$TRUSTANCHORSFILE"
1090 [ -x /etc
/init.d
/sysntpd
] && {
1091 if /etc
/init.d
/sysntpd enabled ||
[ "$(uci_get system.ntp.enabled)" = "1" ] ; then
1092 [ -f "$TIMEVALIDFILE" ] || xappend
"--dnssec-no-timecheck"
1095 config_get_bool dnsseccheckunsigned
"$cfg" dnsseccheckunsigned
1
1096 [ "$dnsseccheckunsigned" -eq 0 ] && xappend
"--dnssec-check-unsigned=no"
1099 config_get addmac
"$cfg" addmac
0
1100 [ "$addmac" != "0" ] && {
1101 [ "$addmac" = "1" ] && addmac
=
1102 xappend
"--add-mac${addmac:+="$addmac"}"
1105 dhcp_option_add
"$cfg" "" 0
1106 dhcp_option_add
"$cfg" "" 2
1108 xappend
"--dhcp-broadcast=tag:needs-broadcast"
1111 config_get dnsmasqconfdir
"$cfg" confdir
"/tmp/dnsmasq.d"
1112 xappend
"--conf-dir=$dnsmasqconfdir"
1113 dnsmasqconfdir
="${dnsmasqconfdir%%,*}"
1114 [ ! -d "$dnsmasqconfdir" ] && mkdir
-p $dnsmasqconfdir
1115 xappend
"--user=dnsmasq"
1116 xappend
"--group=dnsmasq"
1117 echo >> $CONFIGFILE_TMP
1119 config_get_bool enable_tftp
"$cfg" enable_tftp
0
1120 [ "$enable_tftp" -gt 0 ] && {
1121 config_get tftp_root
"$cfg" tftp_root
1122 append EXTRA_MOUNT
$tftp_root
1125 config_foreach filter_dnsmasq
host dhcp_host_add
"$cfg"
1126 echo >> $CONFIGFILE_TMP
1128 config_get_bool dhcpbogushostname
"$cfg" dhcpbogushostname
1
1129 [ "$dhcpbogushostname" -gt 0 ] && {
1130 xappend
"--dhcp-ignore-names=tag:dhcp_bogus_hostname"
1131 [ -r "$DHCPBOGUSHOSTNAMEFILE" ] && xappend
"--conf-file=$DHCPBOGUSHOSTNAMEFILE"
1134 config_foreach filter_dnsmasq boot dhcp_boot_add
"$cfg"
1135 config_foreach filter_dnsmasq mac dhcp_mac_add
"$cfg"
1136 config_foreach filter_dnsmasq tag dhcp_tag_add
"$cfg"
1137 config_foreach filter_dnsmasq vendorclass dhcp_vendorclass_add
"$cfg"
1138 config_foreach filter_dnsmasq userclass dhcp_userclass_add
"$cfg"
1139 config_foreach filter_dnsmasq circuitid dhcp_circuitid_add
"$cfg"
1140 config_foreach filter_dnsmasq remoteid dhcp_remoteid_add
"$cfg"
1141 config_foreach filter_dnsmasq subscrid dhcp_subscrid_add
"$cfg"
1142 config_foreach filter_dnsmasq match dhcp_match_add
"$cfg"
1143 config_foreach filter_dnsmasq domain dhcp_domain_add
"$cfg"
1144 config_foreach filter_dnsmasq hostrecord dhcp_hostrecord_add
"$cfg"
1145 [ -n "$BOOT" ] || config_foreach filter_dnsmasq relay dhcp_relay_add
"$cfg"
1147 echo >> $CONFIGFILE_TMP
1148 config_foreach filter_dnsmasq srvhost dhcp_srv_add
"$cfg"
1149 config_foreach filter_dnsmasq mxhost dhcp_mx_add
"$cfg"
1150 echo >> $CONFIGFILE_TMP
1152 config_get_bool boguspriv
"$cfg" boguspriv
1
1153 [ "$boguspriv" -gt 0 ] && {
1154 xappend
"--bogus-priv"
1155 [ -r "$RFC6761FILE" ] && xappend
"--conf-file=$RFC6761FILE"
1158 if [ "$DNSMASQ_DHCP_VER" -gt 4 ] ; then
1159 # Enable RA feature for when/if it is constructed,
1160 # and RA is selected per interface pool (RA, DHCP, or both),
1161 # but no one (should) want RA broadcast in syslog
1162 [ -n "$BOOT" ] || config_foreach filter_dnsmasq dhcp dhcp_add
"$cfg"
1163 xappend
"--enable-ra"
1164 xappend
"--quiet-ra"
1165 append_bool
"$cfg" quietdhcp
"--quiet-dhcp6"
1167 elif [ "$DNSMASQ_DHCP_VER" -gt 0 ] ; then
1168 [ -n "$BOOT" ] || config_foreach filter_dnsmasq dhcp dhcp_add
"$cfg"
1172 echo >> $CONFIGFILE_TMP
1173 config_foreach filter_dnsmasq cname dhcp_cname_add
"$cfg"
1174 echo >> $CONFIGFILE_TMP
1176 echo >> $CONFIGFILE_TMP
1177 config_foreach filter_dnsmasq ipset dnsmasq_ipset_add
"$cfg"
1178 echo >> $CONFIGFILE_TMP
1180 mv -f $CONFIGFILE_TMP $CONFIGFILE
1181 mv -f $HOSTFILE_TMP $HOSTFILE
1183 [ "$localuse" -gt 0 ] && {
1184 rm -f /tmp
/resolv.conf
1185 [ $ADD_LOCAL_DOMAIN -eq 1 ] && [ -n "$DOMAIN" ] && {
1186 echo "search $DOMAIN" >> /tmp
/resolv.conf
1188 DNS_SERVERS
="$DNS_SERVERS 127.0.0.1"
1189 [ -e /proc
/sys
/net
/ipv6
] && DNS_SERVERS
="$DNS_SERVERS ::1"
1190 for DNS_SERVER
in $DNS_SERVERS ; do
1191 echo "nameserver $DNS_SERVER" >> /tmp
/resolv.conf
1195 config_list_foreach
"$cfg" addnmount append_extramount
1197 procd_open_instance
$cfg
1198 procd_set_param
command $PROG -C $CONFIGFILE -k -x /var
/run
/dnsmasq
/dnsmasq.
"${cfg}".pid
1199 procd_set_param
file $CONFIGFILE
1200 [ -n "$user_dhcpscript" ] && procd_set_param env USER_DHCPSCRIPT
="$user_dhcpscript"
1201 procd_set_param respawn
1203 local instance_ifc instance_netdev
1204 config_get instance_ifc
"$cfg" interface
1205 [ -n "$instance_ifc" ] && network_get_device instance_netdev
"$instance_ifc" &&
1206 [ -n "$instance_netdev" ] && procd_set_param netdev
$instance_netdev
1208 procd_add_jail dnsmasq ubus log
1209 procd_add_jail_mount
$CONFIGFILE $DHCPBOGUSHOSTNAMEFILE $DHCPSCRIPT $DHCPSCRIPT_DEPENDS
1210 procd_add_jail_mount
$EXTRA_MOUNT $RFC6761FILE $TRUSTANCHORSFILE
1211 procd_add_jail_mount
$dnsmasqconffile $dnsmasqconfdir $resolvdir $user_dhcpscript
1212 procd_add_jail_mount
/etc
/passwd
/etc
/group
/etc
/TZ
/etc
/hosts
/etc
/ethers
1213 procd_add_jail_mount_rw
/var
/run
/dnsmasq
/ $leasefile
1214 case "$logfacility" in */*)
1215 [ ! -e "$logfacility" ] && touch "$logfacility"
1216 procd_add_jail_mount_rw
"$logfacility"
1218 [ -e "$hostsfile" ] && procd_add_jail_mount
$hostsfile
1220 procd_close_instance
1226 local noresolv resolvfile localuse
=0
1228 config_get_bool noresolv
"$cfg" noresolv
0
1229 config_get resolvfile
"$cfg" "resolvfile"
1231 [ "$noresolv" = 0 ] && [ "$resolvfile" = "/tmp/resolv.conf.d/resolv.conf.auto" ] && localuse
=1
1232 config_get_bool localuse
"$cfg" localuse
"$localuse"
1233 [ "$localuse" -gt 0 ] && ln -sf "/tmp/resolv.conf.d/resolv.conf.auto" /tmp
/resolv.conf
1235 rm -f ${BASEDHCPSTAMPFILE}.
${cfg}.
*.dhcp
1238 add_interface_trigger
()
1240 local interface ignore
1242 config_get interface
"$1" interface
1243 config_get_bool ignore
"$1" ignore
0
1245 [ -n "$interface" ] && [ $ignore -eq 0 ] && procd_add_interface_trigger
"interface.*" "$interface" /etc
/init.d
/dnsmasq reload
1250 procd_add_reload_trigger
"dhcp" "system"
1253 config_foreach add_interface_trigger dhcp
1254 config_foreach add_interface_trigger relay
1265 local instance_found
=0
1266 local first_instance
=""
1268 .
/lib
/functions
/network.sh
1273 if [ "$type" = "dnsmasq" ]; then
1274 if [ -n "$instance" ] && [ "$instance" = "$name" ]; then
1277 if [ -z "$DEFAULT_INSTANCE" ]; then
1279 config_get_bool disabled
"$name" disabled
0
1280 if [ "$disabled" -eq 0 ]; then
1281 # First enabled section will be assigned default instance name.
1282 # Unnamed sections get precedence over named sections.
1283 if expr "$cfg" : 'cfg[0-9a-f]*$' >/dev
/null
= "9"; then # See uci_fixup_section.
1284 DEFAULT_INSTANCE
="$name" # Unnamed config section.
1285 elif [ -z "$first_instance" ]; then
1286 first_instance
="$name"
1295 if [ -z "$DEFAULT_INSTANCE" ]; then
1296 DEFAULT_INSTANCE
="$first_instance" # No unnamed config section was found.
1299 if [ -n "$instance" ]; then
1300 [ "$instance_found" -gt 0 ] ||
return
1301 dnsmasq_start
"$instance"
1303 config_foreach dnsmasq_start dnsmasq
1308 rc_procd start_service
"$@"
1309 procd_send_signal dnsmasq
"$@"
1314 local instance_found
=0
1319 if [ "$type" = "dnsmasq" ]; then
1320 if [ -n "$instance" ] && [ "$instance" = "$name" ]; then
1328 if [ -n "$instance" ]; then
1329 [ "$instance_found" -gt 0 ] ||
return
1330 dnsmasq_stop
"$instance"
1332 config_foreach dnsmasq_stop dnsmasq