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