Fresh pull from upstream
[librecmc/librecmc.git] / package / network / services / dnsmasq / files / dnsmasq.init
index eebf5c76c89479f4a9653059110432a4faa748d4..87332a9081da87c7d69bcad6f93e9e9b23978cb7 100644 (file)
@@ -8,6 +8,7 @@ PROG=/usr/sbin/dnsmasq
 
 ADD_LOCAL_DOMAIN=1
 ADD_LOCAL_HOSTNAME=1
+ADD_WAN_HOSTNAME=0
 
 BASECONFIGFILE="/var/etc/dnsmasq.conf"
 BASEHOSTFILE="/tmp/hosts/dhcp"
@@ -23,22 +24,22 @@ xappend() {
 }
 
 hex_to_hostid() {
-        local var="$1"
-        local hex="${2#0x}"     # strip optional "0x" prefix
+       local var="$1"
+       local hex="${2#0x}" # strip optional "0x" prefix
 
-        if [ -n "${hex//[0-9a-fA-F]/}" ]; then
-                # is invalid hex literal
-                return 1
-        fi
+       if [ -n "${hex//[0-9a-fA-F]/}" ]; then
+               # is invalid hex literal
+               return 1
+       fi
 
-        # convert into host id
-        export "$var=$(
-                printf "%0x:%0x"  \
-                        $(((0x$hex >> 16) % 65536)) \
-                        $(( 0x$hex        % 65536))
-        )"
+       # convert into host id
+       export "$var=$(
+               printf "%0x:%0x" \
+               $(((0x$hex >> 16) % 65536)) \
+               $(( 0x$hex        % 65536))
+               )"
 
-        return 0
+       return 0
 }
 
 dhcp_calc() {
@@ -278,6 +279,7 @@ dhcp_host_add() {
        config_get tag "$cfg" tag
 
        if [ "$DHCPv6CAPABLE" -eq 1 ]; then
+               config_get duid "$cfg" duid
                config_get hostid "$cfg" hostid
                if [ -n "$hostid" ]; then
                        hex_to_hostid hostid "$hostid"
@@ -289,7 +291,27 @@ dhcp_host_add() {
 
        config_get leasetime "$cfg" leasetime
 
-       xappend "--dhcp-host=$macs${networkid:+,net:$networkid}${broadcast:+,set:needs-broadcast}${tag:+,set:$tag}${ip:+,$ip${hostid:+,[::$hostid]}}${name:+,$name}${leasetime:+,$leasetime}"
+       xappend "--dhcp-host=$macs${duid:+,id:$duid}${networkid:+,net:$networkid}${broadcast:+,set:needs-broadcast}${tag:+,set:$tag}${ip:+,$ip${hostid:+,[::$hostid]}}${name:+,$name}${leasetime:+,$leasetime}"
+}
+
+dhcp_this_host_add() {
+       # TODO: case-in do/not short-host or FQDN; UCI already intended ...
+       local ifname="$1"
+       local do_enable="$2"
+       local routerstub routername ifdashname
+
+
+       if [ "$do_enable" -gt 0 ] ; then
+               # All IP addresses discovered by dnsmasq will be labeled robustly (except fe80::)
+               ifdashname="${ifname//./-}"
+               routerstub="$( md5sum /etc/os-release )"
+               routerstub="router-${routerstub// */}"
+               routername="$( uci_get system @system[0] hostname $routerstub )"
+
+               xappend "--interface-name=$ifdashname.$routername.$DOMAIN,$ifname"
+               xappend "--interface-name=$routername.$DOMAIN,$ifname"
+               xappend "--interface-name=$routername,$ifname"
+       fi
 }
 
 dhcp_tag_add() {
@@ -362,7 +384,11 @@ dhcp_add() {
                DNS_SERVERS="$DNS_SERVERS $dnsserver"
        }
 
-       append_bool "$cfg" ignore "--no-dhcp-interface=$ifname" && return 0
+       append_bool "$cfg" ignore "--no-dhcp-interface=$ifname" && {
+               # Many ISP do not have useful names for DHCP customers (your WAN).
+               dhcp_this_host_add $ifname $ADD_WAN_HOSTNAME
+               return 0
+       }
 
        # Do not support non-static interfaces for now
        [ static = "$proto" ] || return 0
@@ -380,6 +406,9 @@ dhcp_add() {
        config_get options "$cfg" options
        config_get_bool dynamicdhcp "$cfg" dynamicdhcp 1
 
+       # Put the router host name on this DHCP served interface address(es)
+       dhcp_this_host_add $ifname $ADD_LOCAL_HOSTNAME
+
        leasetime="${leasetime:-12h}"
        start="$(dhcp_calc "${start:-100}")"
        limit="${limit:-150}"
@@ -573,6 +602,7 @@ dnsmasq_start()
        append_bool "$cfg" allservers "--all-servers"
        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"
@@ -603,6 +633,7 @@ dnsmasq_start()
 
        config_get_bool ADD_LOCAL_DOMAIN "$cfg" add_local_domain 1
        config_get_bool ADD_LOCAL_HOSTNAME "$cfg" add_local_hostname 1
+       config_get_bool ADD_WAN_HOSTNAME "$cfg" add_wan_hostname 0
 
        config_get_bool readethers "$cfg" readethers
        [ "$readethers" = "1" -a \! -e "/etc/ethers" ] && touch /etc/ethers
@@ -670,7 +701,7 @@ dnsmasq_start()
 
        xappend "--dhcp-broadcast=tag:needs-broadcast"
 
-       xappend "--addn-hosts=$HOSTFILE"
+       xappend "--addn-hosts=$(dirname $HOSTFILE)"
 
        config_get dnsmasqconfdir "$cfg" confdir "/tmp/dnsmasq.d"
        [ ! -d "$dnsmasqconfdir" ] && mkdir -p $dnsmasqconfdir
@@ -700,27 +731,6 @@ dnsmasq_start()
        config_foreach filter_dnsmasq hostrecord dhcp_hostrecord_add "$cfg"
        config_foreach filter_dnsmasq relay dhcp_relay_add "$cfg"
 
-       # add own hostname
-       [ $ADD_LOCAL_HOSTNAME -eq 1 ] && {
-               local lanaddr lanaddr6
-               local ulaprefix="$(uci_get network @globals[0] ula_prefix)"
-               local hostname="$(uci_get system @system[0] hostname Lede)"
-
-               network_get_ipaddr lanaddr "lan" && {
-                       dhcp_domain_add "" "$hostname" "$lanaddr"
-               }
-
-               [ -n "$ulaprefix" ] && network_get_ipaddrs6 lanaddr6 "lan" && {
-                       for lanaddr6 in $lanaddr6; do
-                               case "$lanaddr6" in
-                                       "${ulaprefix%%:/*}"*)
-                                               dhcp_domain_add "" "$hostname" "$lanaddr6"
-                                       ;;
-                               esac
-                       done
-               }
-       }
-
        echo >> $CONFIGFILE_TMP
        config_foreach filter_dnsmasq srvhost dhcp_srv_add "$cfg"
        config_foreach filter_dnsmasq mxhost dhcp_mx_add "$cfg"
@@ -785,7 +795,7 @@ dnsmasq_stop()
 
 service_triggers()
 {
-       procd_add_reload_trigger "dhcp" "system"
+       procd_add_reload_trigger "dhcp"
        procd_add_raw_trigger "interface.*" 2000 /etc/init.d/dnsmasq reload
 }
 
@@ -825,7 +835,6 @@ start_service() {
 
 reload_service() {
        rc_procd start_service "$@"
-       killall -HUP dnsmasq
        return 0
 }