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