netifd: always send DHCPv4 hostname
[openwrt/openwrt.git] / package / network / config / netifd / files / lib / netifd / proto / dhcp.sh
1 #!/bin/sh
2
3 . /lib/functions.sh
4 . ../netifd-proto.sh
5 init_proto "$@"
6
7 proto_dhcp_init_config() {
8 renew_handler=1
9
10 proto_config_add_string 'ipaddr:ipaddr'
11 proto_config_add_string 'hostname:hostname'
12 proto_config_add_string clientid
13 proto_config_add_string vendorid
14 proto_config_add_boolean 'broadcast:bool'
15 proto_config_add_boolean 'release:bool'
16 proto_config_add_string 'reqopts:list(string)'
17 proto_config_add_string iface6rd
18 proto_config_add_string sendopts
19 proto_config_add_boolean delegate
20 proto_config_add_string zone6rd
21 proto_config_add_string zone
22 proto_config_add_string mtu6rd
23 proto_config_add_string customroutes
24 proto_config_add_boolean classlessroute
25 }
26
27 proto_dhcp_setup() {
28 local config="$1"
29 local iface="$2"
30
31 local ipaddr hostname clientid vendorid broadcast release reqopts iface6rd sendopts delegate zone6rd zone mtu6rd customroutes classlessroute
32 json_get_vars ipaddr hostname clientid vendorid broadcast release reqopts iface6rd sendopts delegate zone6rd zone mtu6rd customroutes classlessroute
33
34 local opt dhcpopts
35 for opt in $reqopts; do
36 append dhcpopts "-O $opt"
37 done
38
39 for opt in $sendopts; do
40 append dhcpopts "-x $opt"
41 done
42
43 [ -z "$hostname" ] && hostname="$(cat /proc/sys/kernel/hostname)"
44 [ "$broadcast" = 1 ] && broadcast="-B" || broadcast=
45 [ "$release" = 1 ] && release="-R" || release=
46 [ -n "$clientid" ] && clientid="-x 0x3d:${clientid//:/}" || clientid="-C"
47 [ -n "$iface6rd" ] && proto_export "IFACE6RD=$iface6rd"
48 [ "$iface6rd" != 0 -a -f /lib/netifd/proto/6rd.sh ] && append dhcpopts "-O 212"
49 [ -n "$zone6rd" ] && proto_export "ZONE6RD=$zone6rd"
50 [ -n "$zone" ] && proto_export "ZONE=$zone"
51 [ -n "$mtu6rd" ] && proto_export "MTU6RD=$mtu6rd"
52 [ -n "$customroutes" ] && proto_export "CUSTOMROUTES=$customroutes"
53 [ "$delegate" = "0" ] && proto_export "IFACE6RD_DELEGATE=0"
54 # Request classless route option (see RFC 3442) by default
55 [ "$classlessroute" = "0" ] || append dhcpopts "-O 121"
56
57 proto_export "INTERFACE=$config"
58 proto_run_command "$config" udhcpc \
59 -p /var/run/udhcpc-$iface.pid \
60 -s /lib/netifd/dhcp.script \
61 -f -t 0 -i "$iface" \
62 ${ipaddr:+-r $ipaddr} \
63 ${hostname:+-x "hostname:$hostname"} \
64 ${vendorid:+-V "$vendorid"} \
65 $clientid $broadcast $release $dhcpopts
66 }
67
68 proto_dhcp_renew() {
69 local interface="$1"
70 # SIGUSR1 forces udhcpc to renew its lease
71 local sigusr1="$(kill -l SIGUSR1)"
72 [ -n "$sigusr1" ] && proto_kill_command "$interface" $sigusr1
73 }
74
75 proto_dhcp_teardown() {
76 local interface="$1"
77 proto_kill_command "$interface"
78 }
79
80 add_protocol dhcp