netifd: support DHCP sendopts as list options
[openwrt/openwrt.git] / package / network / config / netifd / files / lib / netifd / proto / dhcp.sh
index 1d3a2091060aac6b89d8d10f915b11b251011a85..afcfba9f49a737dca9f75ca25aa0ad59a60cd171 100755 (executable)
@@ -14,31 +14,37 @@ proto_dhcp_init_config() {
        proto_config_add_boolean 'broadcast:bool'
        proto_config_add_boolean 'release:bool'
        proto_config_add_string 'reqopts:list(string)'
+       proto_config_add_boolean 'defaultreqopts:bool'
        proto_config_add_string iface6rd
-       proto_config_add_string sendopts
+       proto_config_add_array 'sendopts:list(string)'
        proto_config_add_boolean delegate
        proto_config_add_string zone6rd
        proto_config_add_string zone
        proto_config_add_string mtu6rd
        proto_config_add_string customroutes
+       proto_config_add_boolean classlessroute
+}
+
+proto_dhcp_add_sendopts() {
+       [ -n "$1" ] && append "$3" "-x $1"
 }
 
 proto_dhcp_setup() {
        local config="$1"
        local iface="$2"
 
-       local ipaddr hostname clientid vendorid broadcast release reqopts iface6rd sendopts delegate zone6rd zone mtu6rd customroutes
-       json_get_vars ipaddr hostname clientid vendorid broadcast release reqopts iface6rd sendopts delegate zone6rd zone mtu6rd customroutes
+       local ipaddr hostname clientid vendorid broadcast release reqopts defaultreqopts iface6rd sendopts delegate zone6rd zone mtu6rd customroutes classlessroute
+       json_get_vars ipaddr hostname clientid vendorid broadcast release reqopts defaultreqopts iface6rd delegate zone6rd zone mtu6rd customroutes classlessroute
 
        local opt dhcpopts
        for opt in $reqopts; do
                append dhcpopts "-O $opt"
        done
 
-       for opt in $sendopts; do
-               append dhcpopts "-x $opt"
-       done
+       json_for_each_item proto_dhcp_add_sendopts sendopts dhcpopts
 
+       [ -z "$hostname" ] && hostname="$(cat /proc/sys/kernel/hostname)"
+       [ "$defaultreqopts" = 0 ] && defaultreqopts="-o" || defaultreqopts=
        [ "$broadcast" = 1 ] && broadcast="-B" || broadcast=
        [ "$release" = 1 ] && release="-R" || release=
        [ -n "$clientid" ] && clientid="-x 0x3d:${clientid//:/}" || clientid="-C"
@@ -49,6 +55,8 @@ proto_dhcp_setup() {
        [ -n "$mtu6rd" ] && proto_export "MTU6RD=$mtu6rd"
        [ -n "$customroutes" ] && proto_export "CUSTOMROUTES=$customroutes"
        [ "$delegate" = "0" ] && proto_export "IFACE6RD_DELEGATE=0"
+       # Request classless route option (see RFC 3442) by default
+       [ "$classlessroute" = "0" ] || append dhcpopts "-O 121"
 
        proto_export "INTERFACE=$config"
        proto_run_command "$config" udhcpc \
@@ -58,7 +66,7 @@ proto_dhcp_setup() {
                ${ipaddr:+-r $ipaddr} \
                ${hostname:+-x "hostname:$hostname"} \
                ${vendorid:+-V "$vendorid"} \
-               $clientid $broadcast $release $dhcpopts
+               $clientid $defaultreqopts $broadcast $release $dhcpopts
 }
 
 proto_dhcp_renew() {