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 # make sure the DHCP range is not empty
587 if [ "$dhcpv4" != "disabled" ] && ipcalc
"${subnet%%/*}" "$netmask" "$start" "$limit" ; then
588 [ "$dynamicdhcpv4" = "0" ] && END
="static"
590 xappend
"--dhcp-range=$tags$nettag$START,$END,$NETMASK,$leasetime${options:+ $options}"
593 if [ "$dynamicdhcpv6" = "0" ] ; then
594 dhcp6range
="::,static"
596 dhcp6range
="::1000,::ffff"
600 if [ $DNSMASQ_DHCP_VER -eq 6 ] && [ "$ra" = "server" ] ; then
601 # Note: dnsmasq cannot just be a DHCPv6 server (all-in-1)
602 # and let some other machine(s) send RA pointing to it.
604 case $ra_preference in
606 xappend
"--ra-param=$ifname,high,0,7200"
609 xappend
"--ra-param=$ifname,low,0,7200"
612 # Send UNSOLICITED RA at default interval and live for 2 hours.
613 # TODO: convert flexible lease time into route life time (only seconds).
614 xappend
"--ra-param=$ifname,0,7200"
618 if [ "$dhcpv6" = "disabled" ] ; then
623 case $ra_management in
625 # SLACC with DCHP for extended options
626 xappend
"--dhcp-range=$nettag::,constructor:$ifname,ra-stateless,ra-names"
629 # DHCP address and RA only for management redirection
630 xappend
"--dhcp-range=$nettag$dhcp6range,constructor:$ifname,$leasetime"
633 # SLAAC only but dnsmasq attempts to link HOSTNAME, DHCPv4 MAC, and SLAAC
634 xappend
"--dhcp-range=$nettag::,constructor:$ifname,ra-only,ra-names"
637 # SLAAC and full DHCP
638 xappend
"--dhcp-range=$nettag$dhcp6range,constructor:$ifname,slaac,ra-names,$leasetime"
642 if [ -n "$dns" ]; then
644 for d
in $dns; do append dnss
"[$d]" ","; done
649 dhcp_option_append
"option6:dns-server,$dnss" "$networkid"
652 dhcp_option_add
"$cfg" "$networkid" 0
653 dhcp_option_add
"$cfg" "$networkid" 2
656 dhcp_option_append
() {
661 xappend
"--dhcp-option${force:+-force}=${networkid:+$networkid,}$option"
665 # NOTE: dnsmasq has explicit "option6:" prefix for DHCPv6 so no collisions
669 local opt
="dhcp_option"
671 [ "$force" = "0" ] && force
=
672 [ "$force" = "2" ] && opt
="dhcp_option_force"
675 config_get list_len
"$cfg" "${opt}_LENGTH"
677 if [ -n "$list_len" ]; then
678 config_list_foreach
"$cfg" "$opt" dhcp_option_append
"$networkid" "$force"
680 config_get dhcp_option
"$cfg" "$opt"
682 [ -n "$dhcp_option" ] && echo "Warning: the 'option $opt' syntax is deprecated, use 'list $opt'" >&2
685 for option
in $dhcp_option; do
686 dhcp_option_append
"$option" "$networkid" "$force"
693 local ip name names record
695 config_get names
"$cfg" name
"$2"
696 [ -n "$names" ] ||
return 0
698 config_get ip
"$cfg" ip
"$3"
699 [ -n "$ip" ] ||
return 0
701 for name
in $names; do
702 record
="${record:+$record }$name"
705 echo "$ip $record" >> $HOSTFILE_TMP
711 config_get srv
"$cfg" srv
712 [ -n "$srv" ] ||
return 0
714 config_get target
"$cfg" target
715 [ -n "$target" ] ||
return 0
717 config_get port
"$cfg" port
718 [ -n "$port" ] ||
return 0
720 config_get class
"$cfg" class
721 config_get weight
"$cfg" weight
723 local service
="$srv,$target,$port${class:+,$class${weight:+,$weight}}"
725 xappend
"--srv-host=$service"
730 local domain relay pref
732 config_get domain
"$cfg" domain
733 [ -n "$domain" ] ||
return 0
735 config_get relay
"$cfg" relay
736 [ -n "$relay" ] ||
return 0
738 config_get pref
"$cfg" pref
0
740 local service
="$domain,$relay,$pref"
742 xappend
"--mx-host=$service"
749 config_get cname
"$cfg" cname
750 [ -n "$cname" ] ||
return 0
752 config_get target
"$cfg" target
753 [ -n "$target" ] ||
return 0
755 xappend
"--cname=${cname},${target}"
758 dhcp_hostrecord_add
() {
760 local names addresses record val
762 config_get names
"$cfg" name
"$2"
763 if [ -z "$names" ]; then
767 config_get addresses
"$cfg" ip
"$3"
768 if [ -z "$addresses" ]; then
772 for val
in $names $addresses; do
773 record
="${record:+$record,}$val"
776 xappend
"--host-record=$record"
781 local local_addr server_addr interface
783 config_get local_addr
"$cfg" local_addr
784 [ -n "$local_addr" ] ||
return 0
786 config_get server_addr
"$cfg" server_addr
787 [ -n "$server_addr" ] ||
return 0
789 config_get interface
"$cfg" interface
790 if [ -z "$interface" ]; then
791 xappend
"--dhcp-relay=$local_addr,$server_addr"
793 network_get_device ifname
"$interface" ||
return
794 xappend
"--dhcp-relay=$local_addr,$server_addr,$ifname"
798 dnsmasq_ipset_add
() {
800 local ipsets nftsets domains
803 ipsets
="${ipsets:+$ipsets,}$1"
810 [ -n "$fam" ] || fam
=$
(echo "$set" |
sed -nre \
811 's#^.*[^0-9]([46])$|^.*[-_]([46])[-_].*$|^([46])[^0-9].*$#\1\2\3#p')
813 fam
=$
(nft
-t list
set "$table_family" "$table" "$set" 2>&1 |
sed -nre \
814 's#^\t\ttype .*\bipv([46])_addr\b.*$#\1#p')
817 logger
-t dnsmasq
"Cannot infer address family from non-existent nftables set '$set'"
819 nftsets
="${nftsets:+$nftsets,}${fam:+$fam#}$table_family#$table#$set"
824 # leading '/' is expected
825 domains
="$domains/$1"
828 config_get table
"$cfg" table
'fw4'
829 config_get table_family
"$cfg" table_family
'inet'
830 if [ "$table_family" = "ip" ] ; then
832 elif [ "$table_family" = "ip6" ] ; then
835 config_get family
"$cfg" family
838 config_list_foreach
"$cfg" "name" add_ipset
839 config_list_foreach
"$cfg" "name" add_nftset
840 config_list_foreach
"$cfg" "domain" add_domain
842 if [ -z "$ipsets" ] ||
[ -z "$nftsets" ] ||
[ -z "$domains" ]; then
846 xappend
"--ipset=$domains/$ipsets"
847 xappend
"--nftset=$domains/$nftsets"
853 local disabled user_dhcpscript logfacility
854 local resolvfile resolvdir localuse
=0
856 config_get_bool disabled
"$cfg" disabled
0
857 [ "$disabled" -gt 0 ] && return 0
859 # reset list of DOMAINS, DNS servers and EXTRA mounts (for each dnsmasq instance)
863 CONFIGFILE
="${BASECONFIGFILE}.${cfg}"
864 CONFIGFILE_TMP
="${CONFIGFILE}.$$"
865 HOSTFILE
="${BASEHOSTFILE}.${cfg}"
866 HOSTFILE_TMP
="${HOSTFILE}.$$"
867 HOSTFILE_DIR
="$(dirname "$HOSTFILE")"
868 BASEDHCPSTAMPFILE_CFG
="${BASEDHCPSTAMPFILE}.${cfg}"
870 # before we can call xappend
871 umask u
=rwx
,g
=rx
,o
=rx
872 mkdir
-p /var
/run
/dnsmasq
/
873 mkdir
-p $
(dirname $CONFIGFILE)
874 mkdir
-p "$HOSTFILE_DIR"
875 mkdir
-p /var
/lib
/misc
876 chown dnsmasq
:dnsmasq
/var
/run
/dnsmasq
878 echo "# auto-generated config file from /etc/config/dhcp" > $CONFIGFILE_TMP
879 echo "# auto-generated config file from /etc/config/dhcp" > $HOSTFILE_TMP
881 local dnsmasqconffile
="/etc/dnsmasq.${cfg}.conf"
882 if [ ! -r "$dnsmasqconffile" ]; then
883 dnsmasqconffile
=/etc
/dnsmasq.conf
886 # if we did this last, we could override auto-generated config
887 [ -f "${dnsmasqconffile}" ] && {
888 xappend
"--conf-file=${dnsmasqconffile}"
891 $PROG --version |
grep -osqE "^Compile time options:.* DHCPv6( |$)" && DHCPv6CAPABLE
=1 || DHCPv6CAPABLE
=0
894 if [ -x /usr
/sbin
/odhcpd
] && [ -x /etc
/init.d
/odhcpd
] ; then
895 local odhcpd_is_main odhcpd_is_enabled
896 config_get odhcpd_is_main odhcpd maindhcp
0
897 /etc
/init.d
/odhcpd enabled
&& odhcpd_is_enabled
=1 || odhcpd_is_enabled
=0
900 if [ "$odhcpd_is_enabled" -eq 0 ] && [ "$DHCPv6CAPABLE" -eq 1 ] ; then
901 # DHCP V4 and V6 in DNSMASQ
903 elif [ "$odhcpd_is_main" -gt 0 ] ; then
904 # ODHCPD is doing it all
907 # You have ODHCPD but use DNSMASQ for DHCPV4
911 elif [ "$DHCPv6CAPABLE" -eq 1 ] ; then
912 # DHCP V4 and V6 in DNSMASQ
918 # Allow DHCP/DHCPv6 to be handled by ISC DHCPD
919 if [ -x /usr
/sbin
/dhcpd
] ; then
920 if [ -x /etc
/init.d
/dhcpd
] ; then
921 /etc
/init.d
/dhcpd enabled
&& DNSMASQ_DHCP_VER
=0
923 if [ -x /etc
/init.d
/dhcpd6
] && [ "$DNSMASQ_DHCP_VER" -gt 0 ] ; then
924 /etc
/init.d
/dhcpd6 enabled
&& DNSMASQ_DHCP_VER
=4
928 append_bool
"$cfg" authoritative
"--dhcp-authoritative"
929 append_bool
"$cfg" nodaemon
"--no-daemon"
930 append_bool
"$cfg" domainneeded
"--domain-needed"
931 append_bool
"$cfg" filterwin2k
"--filterwin2k"
932 append_bool
"$cfg" nohosts
"--no-hosts"
933 append_bool
"$cfg" nonegcache
"--no-negcache"
934 append_bool
"$cfg" strictorder
"--strict-order"
935 append_bool
"$cfg" logqueries
"--log-queries=extra"
936 append_bool
"$cfg" noresolv
"--no-resolv"
937 append_bool
"$cfg" localise_queries
"--localise-queries"
938 append_bool
"$cfg" readethers
"--read-ethers"
940 local instance_name
="dnsmasq.$cfg"
941 if [ "$cfg" = "$DEFAULT_INSTANCE" ]; then
942 instance_name
="dnsmasq"
944 config_get_bool dbus
"$cfg" "dbus" 0
945 [ $dbus -gt 0 ] && xappend
"--enable-dbus=uk.org.thekelleys.$instance_name"
946 config_get_bool ubus
"$cfg" "ubus" 1
947 [ $ubus -gt 0 ] && xappend
"--enable-ubus=$instance_name"
949 append_bool
"$cfg" expandhosts
"--expand-hosts"
950 config_get tftp_root
"$cfg" "tftp_root"
951 [ -n "$tftp_root" ] && mkdir
-p "$tftp_root" && append_bool
"$cfg" enable_tftp
"--enable-tftp"
952 append_bool
"$cfg" tftp_no_fail
"--tftp-no-fail"
953 append_bool
"$cfg" nonwildcard
"--bind-dynamic" 1
954 append_bool
"$cfg" fqdn
"--dhcp-fqdn"
955 append_bool
"$cfg" proxydnssec
"--proxy-dnssec"
956 append_bool
"$cfg" localservice
"--local-service"
957 append_bool
"$cfg" logdhcp
"--log-dhcp"
958 append_bool
"$cfg" quietdhcp
"--quiet-dhcp"
959 append_bool
"$cfg" sequential_ip
"--dhcp-sequential-ip"
960 append_bool
"$cfg" allservers
"--all-servers"
961 append_bool
"$cfg" noping
"--no-ping"
962 append_bool
"$cfg" rapidcommit
"--dhcp-rapid-commit"
963 append_bool
"$cfg" scriptarp
"--script-arp"
965 append_bool
"$cfg" filter_aaaa
"--filter-AAAA"
966 append_bool
"$cfg" filter_a
"--filter-A"
968 append_parm
"$cfg" logfacility
"--log-facility"
969 config_get logfacility
"$cfg" "logfacility"
970 append_parm
"$cfg" cachesize
"--cache-size"
971 append_parm
"$cfg" dnsforwardmax
"--dns-forward-max"
972 append_parm
"$cfg" port
"--port"
973 append_parm
"$cfg" ednspacket_max
"--edns-packet-max"
974 append_parm
"$cfg" dhcpleasemax
"--dhcp-lease-max"
975 append_parm
"$cfg" "queryport" "--query-port"
976 append_parm
"$cfg" "minport" "--min-port"
977 append_parm
"$cfg" "maxport" "--max-port"
978 append_parm
"$cfg" "domain" "--domain"
979 append_parm
"$cfg" "local" "--local"
980 config_list_foreach
"$cfg" "listen_address" append_listenaddress
981 config_list_foreach
"$cfg" "server" append_server
982 config_list_foreach
"$cfg" "rev_server" append_rev_server
983 config_list_foreach
"$cfg" "address" append_address
985 local connmark_allowlist_enable
986 config_get connmark_allowlist_enable
"$cfg" connmark_allowlist_enable
0
987 [ "$connmark_allowlist_enable" -gt 0 ] && {
988 append_parm
"$cfg" "connmark_allowlist_enable" "--connmark-allowlist-enable"
989 config_list_foreach
"$cfg" "connmark_allowlist" append_connmark_allowlist
993 config_list_foreach
"$cfg" "interface" append_interface
994 config_list_foreach
"$cfg" "notinterface" append_notinterface
996 config_get_bool ignore_hosts_dir
"$cfg" ignore_hosts_dir
0
997 if [ "$ignore_hosts_dir" = "1" ]; then
998 xappend
"--addn-hosts=$HOSTFILE"
999 append EXTRA_MOUNT
"$HOSTFILE"
1001 xappend
"--addn-hosts=$HOSTFILE_DIR"
1002 append EXTRA_MOUNT
"$HOSTFILE_DIR"
1004 config_list_foreach
"$cfg" "addnhosts" append_addnhosts
1005 config_list_foreach
"$cfg" "bogusnxdomain" append_bogusnxdomain
1006 append_parm
"$cfg" "leasefile" "--dhcp-leasefile" "/tmp/dhcp.leases"
1009 config_get serversfile
"$cfg" "serversfile"
1010 [ -n "$serversfile" ] && {
1011 xappend
"--servers-file=$serversfile"
1012 append EXTRA_MOUNT
"$serversfile"
1015 append_parm
"$cfg" "tftp_root" "--tftp-root"
1016 append_parm
"$cfg" "dhcp_boot" "--dhcp-boot"
1017 append_parm
"$cfg" "local_ttl" "--local-ttl"
1018 append_parm
"$cfg" "max_ttl" "--max-ttl"
1019 append_parm
"$cfg" "min_cache_ttl" "--min-cache-ttl"
1020 append_parm
"$cfg" "max_cache_ttl" "--max-cache-ttl"
1021 append_parm
"$cfg" "pxe_prompt" "--pxe-prompt"
1022 append_parm
"$cfg" "tftp_unique_root" "--tftp-unique-root"
1023 config_list_foreach
"$cfg" "pxe_service" append_pxe_service
1024 config_get DOMAIN
"$cfg" domain
1026 config_get_bool ADD_LOCAL_DOMAIN
"$cfg" add_local_domain
1
1027 config_get_bool ADD_LOCAL_HOSTNAME
"$cfg" add_local_hostname
1
1028 config_get ADD_LOCAL_FQDN
"$cfg" add_local_fqdn
""
1029 config_get ADD_WAN_FQDN
"$cfg" add_wan_fqdn
0
1031 if [ -z "$ADD_LOCAL_FQDN" ] ; then
1032 # maintain support for previous UCI
1033 ADD_LOCAL_FQDN
="$ADD_LOCAL_HOSTNAME"
1036 config_get user_dhcpscript
$cfg dhcpscript
1037 if has_handler ||
[ -n "$user_dhcpscript" ]; then
1038 xappend
"--dhcp-script=$DHCPSCRIPT"
1039 xappend
"--script-arp"
1042 config_get leasefile
$cfg leasefile
"/tmp/dhcp.leases"
1043 [ -n "$leasefile" ] && [ ! -e "$leasefile" ] && touch "$leasefile"
1044 config_get_bool cachelocal
"$cfg" cachelocal
1
1046 config_get_bool noresolv
"$cfg" noresolv
0
1047 if [ "$noresolv" != "1" ]; then
1048 config_get resolvfile
"$cfg" resolvfile
/tmp
/resolv.conf.d
/resolv.conf.auto
1049 [ -n "$resolvfile" ] && [ ! -e "$resolvfile" ] && touch "$resolvfile"
1050 xappend
"--resolv-file=$resolvfile"
1051 [ "$resolvfile" = "/tmp/resolv.conf.d/resolv.conf.auto" ] && localuse
=1
1052 resolvdir
="$(dirname "$resolvfile")"
1054 config_get_bool localuse
"$cfg" localuse
"$localuse"
1056 config_get hostsfile
"$cfg" dhcphostsfile
1057 [ -e "$hostsfile" ] && xappend
"--dhcp-hostsfile=$hostsfile"
1060 config_get_bool rebind
"$cfg" rebind_protection
1
1061 [ $rebind -gt 0 ] && {
1063 "DNS rebinding protection is active," \
1064 "will discard upstream RFC1918 responses!"
1065 xappend
"--stop-dns-rebind"
1067 local rebind_localhost
1068 config_get_bool rebind_localhost
"$cfg" rebind_localhost
0
1069 [ $rebind_localhost -gt 0 ] && {
1070 log_once
"Allowing 127.0.0.0/8 responses"
1071 xappend
"--rebind-localhost-ok"
1074 append_rebind_domain
() {
1075 log_once
"Allowing RFC1918 responses for domain $1"
1076 xappend
"--rebind-domain-ok=$1"
1079 config_list_foreach
"$cfg" rebind_domain append_rebind_domain
1082 config_get_bool dnssec
"$cfg" dnssec
0
1083 [ "$dnssec" -gt 0 ] && {
1084 xappend
"--conf-file=$TRUSTANCHORSFILE"
1086 [ -x /etc
/init.d
/sysntpd
] && {
1087 if /etc
/init.d
/sysntpd enabled ||
[ "$(uci_get system.ntp.enabled)" = "1" ] ; then
1088 [ -f "$TIMEVALIDFILE" ] || xappend
"--dnssec-no-timecheck"
1091 config_get_bool dnsseccheckunsigned
"$cfg" dnsseccheckunsigned
1
1092 [ "$dnsseccheckunsigned" -eq 0 ] && xappend
"--dnssec-check-unsigned=no"
1095 config_get addmac
"$cfg" addmac
0
1096 [ "$addmac" != "0" ] && {
1097 [ "$addmac" = "1" ] && addmac
=
1098 xappend
"--add-mac${addmac:+="$addmac"}"
1101 dhcp_option_add
"$cfg" "" 0
1102 dhcp_option_add
"$cfg" "" 2
1104 xappend
"--dhcp-broadcast=tag:needs-broadcast"
1107 config_get dnsmasqconfdir
"$cfg" confdir
"/tmp/dnsmasq.d"
1108 xappend
"--conf-dir=$dnsmasqconfdir"
1109 dnsmasqconfdir
="${dnsmasqconfdir%%,*}"
1110 [ ! -d "$dnsmasqconfdir" ] && mkdir
-p $dnsmasqconfdir
1111 xappend
"--user=dnsmasq"
1112 xappend
"--group=dnsmasq"
1113 echo >> $CONFIGFILE_TMP
1115 config_get_bool enable_tftp
"$cfg" enable_tftp
0
1116 [ "$enable_tftp" -gt 0 ] && {
1117 config_get tftp_root
"$cfg" tftp_root
1118 append EXTRA_MOUNT
$tftp_root
1121 config_foreach filter_dnsmasq
host dhcp_host_add
"$cfg"
1122 echo >> $CONFIGFILE_TMP
1124 config_get_bool dhcpbogushostname
"$cfg" dhcpbogushostname
1
1125 [ "$dhcpbogushostname" -gt 0 ] && {
1126 xappend
"--dhcp-ignore-names=tag:dhcp_bogus_hostname"
1127 [ -r "$DHCPBOGUSHOSTNAMEFILE" ] && xappend
"--conf-file=$DHCPBOGUSHOSTNAMEFILE"
1130 config_foreach filter_dnsmasq boot dhcp_boot_add
"$cfg"
1131 config_foreach filter_dnsmasq mac dhcp_mac_add
"$cfg"
1132 config_foreach filter_dnsmasq tag dhcp_tag_add
"$cfg"
1133 config_foreach filter_dnsmasq vendorclass dhcp_vendorclass_add
"$cfg"
1134 config_foreach filter_dnsmasq userclass dhcp_userclass_add
"$cfg"
1135 config_foreach filter_dnsmasq circuitid dhcp_circuitid_add
"$cfg"
1136 config_foreach filter_dnsmasq remoteid dhcp_remoteid_add
"$cfg"
1137 config_foreach filter_dnsmasq subscrid dhcp_subscrid_add
"$cfg"
1138 config_foreach filter_dnsmasq match dhcp_match_add
"$cfg"
1139 config_foreach filter_dnsmasq domain dhcp_domain_add
"$cfg"
1140 config_foreach filter_dnsmasq hostrecord dhcp_hostrecord_add
"$cfg"
1141 [ -n "$BOOT" ] || config_foreach filter_dnsmasq relay dhcp_relay_add
"$cfg"
1143 echo >> $CONFIGFILE_TMP
1144 config_foreach filter_dnsmasq srvhost dhcp_srv_add
"$cfg"
1145 config_foreach filter_dnsmasq mxhost dhcp_mx_add
"$cfg"
1146 echo >> $CONFIGFILE_TMP
1148 config_get_bool boguspriv
"$cfg" boguspriv
1
1149 [ "$boguspriv" -gt 0 ] && {
1150 xappend
"--bogus-priv"
1151 [ -r "$RFC6761FILE" ] && xappend
"--conf-file=$RFC6761FILE"
1154 if [ "$DNSMASQ_DHCP_VER" -gt 4 ] ; then
1155 # Enable RA feature for when/if it is constructed,
1156 # and RA is selected per interface pool (RA, DHCP, or both),
1157 # but no one (should) want RA broadcast in syslog
1158 [ -n "$BOOT" ] || config_foreach filter_dnsmasq dhcp dhcp_add
"$cfg"
1159 xappend
"--enable-ra"
1160 xappend
"--quiet-ra"
1161 append_bool
"$cfg" quietdhcp
"--quiet-dhcp6"
1163 elif [ "$DNSMASQ_DHCP_VER" -gt 0 ] ; then
1164 [ -n "$BOOT" ] || config_foreach filter_dnsmasq dhcp dhcp_add
"$cfg"
1168 echo >> $CONFIGFILE_TMP
1169 config_foreach filter_dnsmasq cname dhcp_cname_add
"$cfg"
1170 echo >> $CONFIGFILE_TMP
1172 echo >> $CONFIGFILE_TMP
1173 config_foreach filter_dnsmasq ipset dnsmasq_ipset_add
"$cfg"
1174 echo >> $CONFIGFILE_TMP
1176 mv -f $CONFIGFILE_TMP $CONFIGFILE
1177 mv -f $HOSTFILE_TMP $HOSTFILE
1179 [ "$localuse" -gt 0 ] && {
1180 rm -f /tmp
/resolv.conf
1181 [ $ADD_LOCAL_DOMAIN -eq 1 ] && [ -n "$DOMAIN" ] && {
1182 echo "search $DOMAIN" >> /tmp
/resolv.conf
1184 DNS_SERVERS
="$DNS_SERVERS 127.0.0.1"
1185 [ -e /proc
/sys
/net
/ipv6
] && DNS_SERVERS
="$DNS_SERVERS ::1"
1186 for DNS_SERVER
in $DNS_SERVERS ; do
1187 echo "nameserver $DNS_SERVER" >> /tmp
/resolv.conf
1191 config_list_foreach
"$cfg" addnmount append_extramount
1193 procd_open_instance
$cfg
1194 procd_set_param
command $PROG -C $CONFIGFILE -k -x /var
/run
/dnsmasq
/dnsmasq.
"${cfg}".pid
1195 procd_set_param
file $CONFIGFILE
1196 [ -n "$user_dhcpscript" ] && procd_set_param env USER_DHCPSCRIPT
="$user_dhcpscript"
1197 procd_set_param respawn
1199 local instance_ifc instance_netdev
1200 config_get instance_ifc
"$cfg" interface
1201 [ -n "$instance_ifc" ] && network_get_device instance_netdev
"$instance_ifc" &&
1202 [ -n "$instance_netdev" ] && procd_set_param netdev
$instance_netdev
1204 procd_add_jail dnsmasq ubus log
1205 procd_add_jail_mount
$CONFIGFILE $DHCPBOGUSHOSTNAMEFILE $DHCPSCRIPT $DHCPSCRIPT_DEPENDS
1206 procd_add_jail_mount
$EXTRA_MOUNT $RFC6761FILE $TRUSTANCHORSFILE
1207 procd_add_jail_mount
$dnsmasqconffile $dnsmasqconfdir $resolvdir $user_dhcpscript
1208 procd_add_jail_mount
/etc
/passwd
/etc
/group
/etc
/TZ
/etc
/hosts
/etc
/ethers
1209 procd_add_jail_mount_rw
/var
/run
/dnsmasq
/ $leasefile
1210 case "$logfacility" in */*)
1211 [ ! -e "$logfacility" ] && touch "$logfacility"
1212 procd_add_jail_mount_rw
"$logfacility"
1214 [ -e "$hostsfile" ] && procd_add_jail_mount
$hostsfile
1216 procd_close_instance
1222 local noresolv resolvfile localuse
=0
1224 config_get_bool noresolv
"$cfg" noresolv
0
1225 config_get resolvfile
"$cfg" "resolvfile"
1227 [ "$noresolv" = 0 ] && [ "$resolvfile" = "/tmp/resolv.conf.d/resolv.conf.auto" ] && localuse
=1
1228 config_get_bool localuse
"$cfg" localuse
"$localuse"
1229 [ "$localuse" -gt 0 ] && ln -sf "/tmp/resolv.conf.d/resolv.conf.auto" /tmp
/resolv.conf
1231 rm -f ${BASEDHCPSTAMPFILE}.
${cfg}.
*.dhcp
1234 add_interface_trigger
()
1236 local interface ignore
1238 config_get interface
"$1" interface
1239 config_get_bool ignore
"$1" ignore
0
1241 [ -n "$interface" ] && [ $ignore -eq 0 ] && procd_add_interface_trigger
"interface.*" "$interface" /etc
/init.d
/dnsmasq reload
1246 procd_add_reload_trigger
"dhcp" "system"
1249 config_foreach add_interface_trigger dhcp
1250 config_foreach add_interface_trigger relay
1261 local instance_found
=0
1262 local first_instance
=""
1264 .
/lib
/functions
/network.sh
1269 if [ "$type" = "dnsmasq" ]; then
1270 if [ -n "$instance" ] && [ "$instance" = "$name" ]; then
1273 if [ -z "$DEFAULT_INSTANCE" ]; then
1275 config_get_bool disabled
"$name" disabled
0
1276 if [ "$disabled" -eq 0 ]; then
1277 # First enabled section will be assigned default instance name.
1278 # Unnamed sections get precedence over named sections.
1279 if expr "$cfg" : 'cfg[0-9a-f]*$' >/dev
/null
= "9"; then # See uci_fixup_section.
1280 DEFAULT_INSTANCE
="$name" # Unnamed config section.
1281 elif [ -z "$first_instance" ]; then
1282 first_instance
="$name"
1291 if [ -z "$DEFAULT_INSTANCE" ]; then
1292 DEFAULT_INSTANCE
="$first_instance" # No unnamed config section was found.
1295 if [ -n "$instance" ]; then
1296 [ "$instance_found" -gt 0 ] ||
return
1297 dnsmasq_start
"$instance"
1299 config_foreach dnsmasq_start dnsmasq
1304 rc_procd start_service
"$@"
1305 procd_send_signal dnsmasq
"$@"
1310 local instance_found
=0
1315 if [ "$type" = "dnsmasq" ]; then
1316 if [ -n "$instance" ] && [ "$instance" = "$name" ]; then
1324 if [ -n "$instance" ]; then
1325 [ "$instance_found" -gt 0 ] ||
return
1326 dnsmasq_stop
"$instance"
1328 config_foreach dnsmasq_stop dnsmasq