dnsmasq: Add match section support
[openwrt/staging/lynxis/omap.git] / package / network / services / dnsmasq / files / dnsmasq.init
index ecf83622be83f729064319e30add0f1022619141..69c375c395728f8b0721fa53a38595772d61716f 100644 (file)
@@ -1,7 +1,7 @@
 #!/bin/sh /etc/rc.common
 # Copyright (C) 2007-2012 OpenWrt.org
 
-START=60
+START=19
 
 USE_PROCD=1
 PROG=/usr/sbin/dnsmasq
@@ -13,14 +13,35 @@ ADD_LOCAL_DOMAIN=1
 ADD_LOCAL_HOSTNAME=1
 
 CONFIGFILE="/var/etc/dnsmasq.conf"
+CONFIGFILE_TMP="/var/etc/dnsmasq.conf.$$"
 HOSTFILE="/tmp/hosts/dhcp"
 TRUSTANCHORSFILE="/usr/share/dnsmasq/trust-anchors.conf"
 TIMESTAMPFILE="/etc/dnsmasq.time"
+TIMEVALIDFILE="/var/state/dnsmasqsec"
 
 xappend() {
        local value="$1"
 
-       echo "${value#--}" >> $CONFIGFILE
+       echo "${value#--}" >> $CONFIGFILE_TMP
+}
+
+hex_to_hostid() {
+        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
+
+        # convert into host id
+        export "$var=$(
+                printf "%0x:%0x"  \
+                        $(((0x$hex >> 16) % 65536)) \
+                        $(( 0x$hex        % 256))
+        )"
+
+        return 0
 }
 
 dhcp_calc() {
@@ -43,6 +64,12 @@ dhcp_check() {
 
        [ -s "$stamp" ] && return $(cat "$stamp")
 
+       # If there's no carrier yet, skip this interface.
+       # The init script will be called again once the link is up
+       case "$(devstatus "$ifname" | jsonfilter -e @.carrier)" in
+               false) return 1;;
+       esac
+
        udhcpc -n -q -s /bin/true -t 1 -i "$ifname" >&- && rv=1 || rv=0
 
        [ $rv -eq 1 ] && \
@@ -92,12 +119,12 @@ append_ipset() {
 }
 
 append_interface() {
-       local ifname=$(uci_get_state network "$1" ifname "$1")
+       network_get_device ifname "$1" || return
        xappend "--interface=$ifname"
 }
 
 append_notinterface() {
-       local ifname=$(uci_get_state network "$1" ifname "$1")
+       network_get_device ifname "$1" || return
        xappend "--except-interface=$ifname"
 }
 
@@ -216,7 +243,12 @@ dnsmasq() {
        [ "$dnssec" -gt 0 ] && {
                xappend "--conf-file=$TRUSTANCHORSFILE"
                xappend "--dnssec"
-               xappend "--dnssec-timestamp=$TIMESTAMPFILE"
+               [ -x /etc/init.d/sysntpd ] && {
+                       /etc/init.d/sysntpd enabled
+                       [ "$?" -ne 0 -o "$(uci_get system.ntp.enabled)" = "1" ] && {
+                               [ -f "$TIMEVALIDFILE" ] || xappend "--dnssec-no-timecheck"
+                       }
+               }
                append_bool "$cfg" dnsseccheckunsigned "--dnssec-check-unsigned"
        }
 
@@ -230,7 +262,7 @@ dnsmasq() {
        xappend "--user=dnsmasq"
        xappend "--group=dnsmasq"
 
-       echo >> $CONFIGFILE
+       echo >> $CONFIGFILE_TMP
 
        config_get_bool enable_tftp "$cfg" enable_tftp 0
        [ "$enable_tftp" -gt 0 ] && {
@@ -319,6 +351,22 @@ dhcp_vendorclass_add() {
        dhcp_option_add "$cfg" "$networkid" "$force"
 }
 
+dhcp_match_add() {
+       local cfg="$1"
+
+       config_get networkid "$cfg" networkid
+       [ -n "$networkid" ] || return 0
+
+       config_get match "$cfg" match
+       [ -n "$match" ] || return 0
+
+       xappend "--dhcp-match=$networkid,$match"
+
+       config_get_bool force "$cfg" force 0
+
+       dhcp_option_add "$cfg" "$networkid" "$force"
+}
+
 dhcp_host_add() {
        local cfg="$1"
 
@@ -353,12 +401,19 @@ dhcp_host_add() {
 
        config_get tag "$cfg" tag
 
+       if [ "$DHCPv6CAPABLE" -eq 1 ]; then
+               config_get hostid "$cfg" hostid
+               if [ -n "$hostid" ]; then
+                       hex_to_hostid hostid "$hostid"
+               fi
+       fi
+
        config_get_bool broadcast "$cfg" broadcast 0
        [ "$broadcast" = "0" ] && broadcast=
 
        config_get leasetime "$cfg" leasetime
 
-       xappend "--dhcp-host=$macs${networkid:+,net:$networkid}${broadcast:+,set:needs-broadcast}${tag:+,set:$tag}${ip:+,$ip}${name:+,$name}${leasetime:+,$leasetime}"
+       xappend "--dhcp-host=$macs${networkid:+,net:$networkid}${broadcast:+,set:needs-broadcast}${tag:+,set:$tag}${ip:+,$ip${hostid:+,[::$hostid]}}${name:+,$name}${leasetime:+,$leasetime}"
 }
 
 dhcp_tag_add() {
@@ -583,14 +638,18 @@ dhcp_relay_add() {
 service_triggers()
 {
        procd_add_reload_trigger "dhcp"
+       procd_add_raw_trigger "interface.*" 2000 /etc/init.d/dnsmasq reload
 }
 
-boot() {
-       # Will be launched through hotplug
-       return 0
+boot()
+{
+       BOOT=1
+       start "$@"
 }
 
 start_service() {
+       [ -n "$BOOT" ] && return
+
        include /lib/functions
 
        config_load dhcp
@@ -600,13 +659,11 @@ start_service() {
        mkdir -p $(dirname $CONFIGFILE)
        mkdir -p /var/lib/misc
        touch /tmp/dhcp.leases
+       chown dnsmasq:dnsmasq /var/run/dnsmasq
 
-       if [ ! -f "$TIMESTAMPFILE" ]; then
-               touch "$TIMESTAMPFILE"
-               chown dnsmasq.dnsmasq "$TIMESTAMPFILE"
-       fi
+       [ -f "$TIMESTAMPFILE" ] && rm -f "$TIMESTAMPFILE"
 
-       echo "# auto-generated config file from /etc/config/dhcp" > $CONFIGFILE
+       echo "# auto-generated config file from /etc/config/dhcp" > $CONFIGFILE_TMP
        echo "# auto-generated config file from /etc/config/dhcp" > $HOSTFILE
 
        # if we did this last, we could override auto-generated config
@@ -614,10 +671,12 @@ start_service() {
                xappend "--conf-file=/etc/dnsmasq.conf"
        }
 
+       $PROG --version | grep -osqE "^Compile time options:.* DHCPv6( |$)" && DHCPv6CAPABLE=1 || DHCPv6CAPABLE=0
+
        args=""
        config_foreach dnsmasq dnsmasq
        config_foreach dhcp_host_add host
-       echo >> $CONFIGFILE
+       echo >> $CONFIGFILE_TMP
        config_foreach dhcp_boot_add boot
        config_foreach dhcp_mac_add mac
        config_foreach dhcp_tag_add tag
@@ -626,6 +685,7 @@ start_service() {
        config_foreach dhcp_circuitid_add circuitid
        config_foreach dhcp_remoteid_add remoteid
        config_foreach dhcp_subscrid_add subscrid
+       config_foreach dhcp_match_add match
        config_foreach dhcp_domain_add domain
        config_foreach dhcp_hostrecord_add hostrecord
        config_foreach dhcp_relay_add relay
@@ -651,19 +711,20 @@ start_service() {
                }
        }
 
-       echo >> $CONFIGFILE
+       echo >> $CONFIGFILE_TMP
        config_foreach dhcp_srv_add srvhost
        config_foreach dhcp_mx_add mxhost
-       echo >> $CONFIGFILE
+       echo >> $CONFIGFILE_TMP
 
        config_get odhcpd_is_active odhcpd maindhcp
        if [ "$odhcpd_is_active" != "1" ]; then
                config_foreach dhcp_add dhcp
        fi
 
-       echo >> $CONFIGFILE
+       echo >> $CONFIGFILE_TMP
        config_foreach dhcp_cname_add cname
-       echo >> $CONFIGFILE
+       echo >> $CONFIGFILE_TMP
+       mv -f $CONFIGFILE_TMP $CONFIGFILE
 
        rm -f /tmp/resolv.conf
        [ $ADD_LOCAL_DOMAIN -eq 1 ] && [ -n "$DOMAIN" ] && {
@@ -681,7 +742,7 @@ start_service() {
 
        procd_add_jail dnsmasq ubus log
        procd_add_jail_mount $CONFIGFILE $TRUSTANCHORSFILE $HOSTFILE /etc/passwd /etc/group /etc/TZ /dev/null /dev/urandom /etc/dnsmasq.conf /tmp/dnsmasq.d /tmp/resolv.conf.auto /etc/hosts /etc/ethers $EXTRA_MOUNT
-       procd_add_jail_mount_rw /var/run/dnsmasq/ /tmp/dhcp.leases $TIMESTAMPFILE
+       procd_add_jail_mount_rw /var/run/dnsmasq/ /tmp/dhcp.leases
 
        procd_close_instance
 }