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