000b42398c45281647428854d7a4c1dd11a66dcd
[openwrt/openwrt.git] / package / network / ipv6 / odhcp6c / files / dhcpv6.sh
1 #!/bin/sh
2
3 . /lib/functions.sh
4 . ../netifd-proto.sh
5 init_proto "$@"
6
7 proto_dhcpv6_init_config() {
8 renew_handler=1
9
10 proto_config_add_string 'reqaddress:or("try","force","none")'
11 proto_config_add_string 'reqprefix:or("auto","no",range(0, 64))'
12 proto_config_add_string clientid
13 proto_config_add_string 'reqopts:list(uinteger)'
14 proto_config_add_string 'defaultreqopts:bool'
15 proto_config_add_string 'noslaaconly:bool'
16 proto_config_add_string 'forceprefix:bool'
17 proto_config_add_string 'extendprefix:bool'
18 proto_config_add_string 'norelease:bool'
19 proto_config_add_array 'ip6prefix:list(ip6addr)'
20 proto_config_add_string iface_dslite
21 proto_config_add_string zone_dslite
22 proto_config_add_string encaplimit_dslite
23 proto_config_add_string iface_map
24 proto_config_add_string zone_map
25 proto_config_add_string encaplimit_map
26 proto_config_add_string iface_464xlat
27 proto_config_add_string zone_464xlat
28 proto_config_add_string zone
29 proto_config_add_string 'ifaceid:ip6addr'
30 proto_config_add_string "userclass"
31 proto_config_add_string "vendorclass"
32 proto_config_add_array "sendopts:list(string)"
33 proto_config_add_boolean delegate
34 proto_config_add_int "soltimeout"
35 proto_config_add_boolean fakeroutes
36 proto_config_add_boolean sourcefilter
37 proto_config_add_boolean keep_ra_dnslifetime
38 proto_config_add_int "ra_holdoff"
39 }
40
41 proto_dhcpv6_add_prefix() {
42 append "$3" "$1"
43 }
44
45 proto_dhcpv6_add_sendopts() {
46 [ -n "$1" ] && append "$3" "-x$1"
47 }
48
49 proto_dhcpv6_setup() {
50 local config="$1"
51 local iface="$2"
52
53 local reqaddress reqprefix clientid reqopts defaultreqopts noslaaconly forceprefix extendprefix norelease ip6prefix ip6prefixes iface_dslite iface_map iface_464xlat ifaceid userclass vendorclass sendopts delegate zone_dslite zone_map zone_464xlat zone encaplimit_dslite encaplimit_map soltimeout fakeroutes sourcefilter keep_ra_dnslifetime ra_holdoff
54 json_get_vars reqaddress reqprefix clientid reqopts defaultreqopts noslaaconly forceprefix extendprefix norelease iface_dslite iface_map iface_464xlat ifaceid userclass vendorclass delegate zone_dslite zone_map zone_464xlat zone encaplimit_dslite encaplimit_map soltimeout fakeroutes sourcefilter keep_ra_dnslifetime ra_holdoff
55 json_for_each_item proto_dhcpv6_add_prefix ip6prefix ip6prefixes
56
57 # Configure
58 local opts=""
59 [ -n "$reqaddress" ] && append opts "-N$reqaddress"
60
61 [ -z "$reqprefix" -o "$reqprefix" = "auto" ] && reqprefix=0
62 [ "$reqprefix" != "no" ] && append opts "-P$reqprefix"
63
64 [ -n "$clientid" ] && append opts "-c$clientid"
65
66 [ "$defaultreqopts" = "0" ] && append opts "-R"
67
68 [ "$noslaaconly" = "1" ] && append opts "-S"
69
70 [ "$forceprefix" = "1" ] && append opts "-F"
71
72 [ "$norelease" = "1" ] && append opts "-k"
73
74 [ -n "$ifaceid" ] && append opts "-i$ifaceid"
75
76 [ -n "$vendorclass" ] && append opts "-V$vendorclass"
77
78 [ -n "$userclass" ] && append opts "-u$userclass"
79
80 [ "$keep_ra_dnslifetime" = "1" ] && append opts "-L"
81
82 [ -n "$ra_holdoff" ] && append opts "-m$ra_holdoff"
83
84 local opt
85 for opt in $reqopts; do
86 append opts "-r$opt"
87 done
88
89 json_for_each_item proto_dhcpv6_add_sendopts sendopts opts
90
91 append opts "-t${soltimeout:-120}"
92
93 [ -n "$ip6prefixes" ] && proto_export "USERPREFIX=$ip6prefixes"
94 [ -n "$iface_dslite" ] && proto_export "IFACE_DSLITE=$iface_dslite"
95 [ -n "$iface_map" ] && proto_export "IFACE_MAP=$iface_map"
96 [ -n "$iface_464xlat" ] && proto_export "IFACE_464XLAT=$iface_464xlat"
97 [ "$delegate" = "0" ] && proto_export "IFACE_DSLITE_DELEGATE=0"
98 [ "$delegate" = "0" ] && proto_export "IFACE_MAP_DELEGATE=0"
99 [ -n "$zone_dslite" ] && proto_export "ZONE_DSLITE=$zone_dslite"
100 [ -n "$zone_map" ] && proto_export "ZONE_MAP=$zone_map"
101 [ -n "$zone_464xlat" ] && proto_export "ZONE_464XLAT=$zone_464xlat"
102 [ -n "$zone" ] && proto_export "ZONE=$zone"
103 [ -n "$encaplimit_dslite" ] && proto_export "ENCAPLIMIT_DSLITE=$encaplimit_dslite"
104 [ -n "$encaplimit_map" ] && proto_export "ENCAPLIMIT_MAP=$encaplimit_map"
105 [ "$fakeroutes" != "0" ] && proto_export "FAKE_ROUTES=1"
106 [ "$sourcefilter" = "0" ] && proto_export "NOSOURCEFILTER=1"
107 [ "$extendprefix" = "1" ] && proto_export "EXTENDPREFIX=1"
108
109 proto_export "INTERFACE=$config"
110 proto_run_command "$config" odhcp6c \
111 -s /lib/netifd/dhcpv6.script \
112 $opts $iface
113 }
114
115 proto_dhcpv6_renew() {
116 local interface="$1"
117 # SIGUSR1 forces odhcp6c to renew its lease
118 local sigusr1="$(kill -l SIGUSR1)"
119 [ -n "$sigusr1" ] && proto_kill_command "$interface" $sigusr1
120 }
121
122 proto_dhcpv6_teardown() {
123 local interface="$1"
124 proto_kill_command "$interface"
125 }
126
127 add_protocol dhcpv6
128