dnsmasq: add dhcp-script hook for other packages
[openwrt/openwrt.git] / package / network / services / dnsmasq / files / dnsmasq.init
index e6ba7322e3dc63fc2c934e9ab444f359747822f2..62a3169c6787e560b8321b048dd6a2c5a86bb6a0 100644 (file)
@@ -18,6 +18,7 @@ TRUSTANCHORSFILE="/usr/share/dnsmasq/trust-anchors.conf"
 TIMEVALIDFILE="/var/state/dnsmasqsec"
 BASEDHCPSTAMPFILE="/var/run/dnsmasq"
 RFC6761FILE="/usr/share/dnsmasq/rfc6761.conf"
+DHCPSCRIPT="/usr/lib/dnsmasq/dhcp-script.sh"
 
 DNSMASQ_DHCP_VER=4
 
@@ -142,6 +143,10 @@ append_pxe_service() {
        xappend "--pxe-service=$1"
 }
 
+append_interface_name() {
+       xappend "--interface-name=$1,$2"
+}
+
 filter_dnsmasq() {
        local cfg="$1" func="$2" match_cfg="$3" found_cfg
 
@@ -252,7 +257,7 @@ dhcp_match_add() {
 
 dhcp_host_add() {
        local cfg="$1"
-       local hosttag nametime addrs
+       local hosttag nametime addrs duids
 
        config_get_bool force "$cfg" force 0
 
@@ -333,14 +338,14 @@ dhcp_this_host_add() {
                if [ "$mode" -gt 1 ] ; then
                        if [ "$mode" -gt 2 ] ; then
                                if [ "$mode" -gt 3 ] ; then
-                                       xappend "--interface-name=$ifdashname.$routername.$DOMAIN,$ifname"
+                                       append_interface_name "$ifdashname.$routername.$DOMAIN" "$ifname"
                                fi
 
-                               xappend "--interface-name=$routername.$DOMAIN,$ifname"
+                               append_interface_name "$routername.$DOMAIN" "$ifname"
                        fi
 
                        # All IP addresses discovered by dnsmasq will be labeled (except fe80::)
-                       xappend "--interface-name=$routername,$ifname"
+                       append_interface_name "$routername" "$ifname"
 
                else
                        # This uses a static host file entry for only limited addresses.
@@ -466,6 +471,9 @@ dhcp_add() {
        config_get ra "$cfg" ra
        config_get ra_management "$cfg" ra_management
        config_get ra_preference "$cfg" ra_preference
+       config_get dns "$cfg" dns
+
+       config_list_foreach "$cfg" "interface_name" append_interface_name "$ifname"
 
        # Put the router host name on this DHCP served interface address(es)
        dhcp_this_host_add "$net" "$ifname" "$ADD_LOCAL_FQDN"
@@ -533,6 +541,15 @@ dhcp_add() {
                        xappend "--dhcp-range=$nettag$dhcp6range,constructor:$ifname,slaac,ra-names,$leasetime"
                        ;;
                esac
+
+               if [ -n "$dns" ]; then
+                       dnss=""
+                       for d in $dns; do append dnss "[$d]" ","; done
+               else
+                       dnss="[::]"
+               fi
+
+               dhcp_option_append "option6:dns-server,$dnss" "$networkid"
        fi
 
        dhcp_option_add "$cfg" "$networkid"
@@ -680,7 +697,7 @@ dhcp_relay_add() {
 
 dnsmasq_start()
 {
-       local cfg="$1" disabled resolvfile
+       local cfg="$1" disabled resolvfile user_dhcpscript
 
        config_get_bool disabled "$cfg" disabled 0
        [ "$disabled" -gt 0 ] && return 0
@@ -762,7 +779,7 @@ dnsmasq_start()
        append_bool "$cfg" dbus "--enable-dbus"
        append_bool "$cfg" expandhosts "--expand-hosts"
        config_get tftp_root "$cfg" "tftp_root"
-       [ -d "$tftp_root" ] && append_bool "$cfg" enable_tftp "--enable-tftp"
+       [ -n "$tftp_root" ] && mkdir -p "$tftp_root" && append_bool "$cfg" enable_tftp "--enable-tftp"
        append_bool "$cfg" tftp_no_fail "--tftp-no-fail"
        append_bool "$cfg" nonwildcard "--bind-dynamic"
        append_bool "$cfg" fqdn "--dhcp-fqdn"
@@ -775,7 +792,7 @@ dnsmasq_start()
        append_bool "$cfg" noping "--no-ping"
 
        append_parm "$cfg" logfacility "--log-facility"
-       append_parm "$cfg" dhcpscript "--dhcp-script"
+
        append_parm "$cfg" cachesize "--cache-size"
        append_parm "$cfg" dnsforwardmax "--dns-forward-max"
        append_parm "$cfg" port "--port"
@@ -815,7 +832,8 @@ dnsmasq_start()
        config_get_bool readethers "$cfg" readethers
        [ "$readethers" = "1" -a \! -e "/etc/ethers" ] && touch /etc/ethers
 
-       config_get dhcpscript $cfg dhcpscript
+       xappend "--dhcp-script=$DHCPSCRIPT"
+       config_get user_dhcpscript $cfg dhcpscript
 
        config_get leasefile $cfg leasefile "/tmp/dhcp.leases"
        [ -n "$leasefile" -a \! -e "$leasefile" ] && touch "$leasefile"
@@ -955,6 +973,7 @@ dnsmasq_start()
        procd_open_instance $cfg
        procd_set_param command $PROG -C $CONFIGFILE -k -x /var/run/dnsmasq/dnsmasq."${cfg}".pid
        procd_set_param file $CONFIGFILE
+       [ -n "$user_dhcpscript" ] && procd_set_param env USER_DHCPSCRIPT="$user_dhcpscript"
        procd_set_param respawn
 
        local dnsmasqconffile="/etc/dnsmasq.${cfg}.conf"
@@ -963,7 +982,7 @@ dnsmasq_start()
        fi
 
        procd_add_jail dnsmasq ubus log
-       procd_add_jail_mount $CONFIGFILE $TRUSTANCHORSFILE $HOSTFILE $RFC6761FILE /etc/passwd /etc/group /etc/TZ /dev/null /dev/urandom $dnsmasqconffile $dnsmasqconfdir $resolvfile $dhcpscript /etc/hosts /etc/ethers $EXTRA_MOUNT
+       procd_add_jail_mount $CONFIGFILE $TRUSTANCHORSFILE $HOSTFILE $RFC6761FILE /etc/passwd /etc/group /etc/TZ /dev/null /dev/urandom $dnsmasqconffile $dnsmasqconfdir $resolvfile $user_dhcpscript /etc/hosts /etc/ethers /sbin/hotplug-call $EXTRA_MOUNT $DHCPSCRIPT
        procd_add_jail_mount_rw /var/run/dnsmasq/ $leasefile
 
        procd_close_instance