OpenWrt v19.07.1: adjust config defaults
[openwrt/openwrt.git] / package / network / ipv6 / odhcp6c / files / dhcpv6.script
1 #!/bin/sh
2 [ -z "$2" ] && echo "Error: should be run by odhcpc6c" && exit 1
3 . /lib/functions.sh
4 . /lib/netifd/netifd-proto.sh
5
6 setup_interface () {
7 local device="$1"
8 local prefsig=""
9 local addrsig=""
10
11 # Apply IPv6 / ND configuration
12 HOPLIMIT=$(cat /proc/sys/net/ipv6/conf/$device/hop_limit)
13 [ -n "$RA_HOPLIMIT" -a -n "$HOPLIMIT" ] && [ "$RA_HOPLIMIT" -gt "$HOPLIMIT" ] && echo "$RA_HOPLIMIT" > /proc/sys/net/ipv6/conf/$device/hop_limit
14 [ -n "$RA_MTU" ] && [ "$RA_MTU" -ge 1280 ] && echo "$RA_MTU" > /proc/sys/net/ipv6/conf/$device/mtu 2>/dev/null
15 [ -n "$RA_REACHABLE" ] && [ "$RA_REACHABLE" -gt 0 ] && echo "$RA_REACHABLE" > /proc/sys/net/ipv6/neigh/$device/base_reachable_time_ms
16 [ -n "$RA_RETRANSMIT" ] && [ "$RA_RETRANSMIT" -gt 0 ] && echo "$RA_RETRANSMIT" > /proc/sys/net/ipv6/neigh/$device/retrans_time_ms
17
18 proto_init_update "*" 1
19
20 # Merge RA-DNS
21 for radns in $RA_DNS; do
22 local duplicate=0
23 for dns in $RDNSS; do
24 [ "$radns" = "$dns" ] && duplicate=1
25 done
26 [ "$duplicate" = 0 ] && RDNSS="$RDNSS $radns"
27 done
28
29 for dns in $RDNSS; do
30 proto_add_dns_server "$dns"
31 done
32
33 for radomain in $RA_DOMAINS; do
34 local duplicate=0
35 for domain in $DOMAINS; do
36 [ "$radomain" = "$domain" ] && duplicate=1
37 done
38 [ "$duplicate" = 0 ] && DOMAINS="$DOMAINS $radomain"
39 done
40
41 for domain in $DOMAINS; do
42 proto_add_dns_search "$domain"
43 done
44
45 for prefix in $PREFIXES; do
46 proto_add_ipv6_prefix "$prefix"
47 prefsig="$prefsig ${prefix%%,*}"
48 local entry="${prefix#*/}"
49 entry="${entry#*,}"
50 entry="${entry#*,}"
51 local valid="${entry%%,*}"
52
53 if [ -z "$RA_ADDRESSES" -a -z "$RA_ROUTES" -a \
54 -z "$RA_DNS" -a "$FAKE_ROUTES" = 1 ]; then
55 RA_ROUTES="::/0,$SERVER,$valid,4096"
56 fi
57 done
58
59 for prefix in $USERPREFIX; do
60 proto_add_ipv6_prefix "$prefix"
61 done
62
63 # Merge addresses
64 for entry in $RA_ADDRESSES; do
65 local duplicate=0
66 local addr="${entry%%/*}"
67 for dentry in $ADDRESSES; do
68 local daddr="${dentry%%/*}"
69 [ "$addr" = "$daddr" ] && duplicate=1
70 done
71 [ "$duplicate" = "0" ] && ADDRESSES="$ADDRESSES $entry"
72 done
73
74 for entry in $ADDRESSES; do
75 local addr="${entry%%/*}"
76 entry="${entry#*/}"
77 local mask="${entry%%,*}"
78 entry="${entry#*,}"
79 local preferred="${entry%%,*}"
80 entry="${entry#*,}"
81 local valid="${entry%%,*}"
82
83 proto_add_ipv6_address "$addr" "$mask" "$preferred" "$valid" 1
84 addrsig="$addrsig $addr/$mask"
85
86 if [ -z "$RA_ADDRESSES" -a -z "$RA_ROUTES" -a \
87 -z "$RA_DNS" -a "$FAKE_ROUTES" = 1 ]; then
88 RA_ROUTES="::/0,$SERVER,$valid,4096"
89 fi
90
91 # RFC 7278
92 if [ "$mask" -eq 64 -a -z "$PREFIXES" -a -n "$EXTENDPREFIX" ]; then
93 proto_add_ipv6_prefix "$addr/$mask,$preferred,$valid"
94
95 local raroutes=""
96 for route in $RA_ROUTES; do
97 local prefix="${route%%/*}"
98 local entry="${route#*/}"
99 local pmask="${entry%%,*}"
100 entry="${entry#*,}"
101 local gw="${entry%%,*}"
102
103 [ -z "$gw" -a "$mask" = "$pmask" ] && {
104 case "$addr" in
105 "${prefix%*::}"*) continue;;
106 esac
107 }
108 raroutes="$raroutes $route"
109 done
110 RA_ROUTES="$raroutes"
111 fi
112 done
113
114 for entry in $RA_ROUTES; do
115 local duplicate=$NOSOURCEFILTER
116 local addr="${entry%%/*}"
117 entry="${entry#*/}"
118 local mask="${entry%%,*}"
119 entry="${entry#*,}"
120 local gw="${entry%%,*}"
121 entry="${entry#*,}"
122 local valid="${entry%%,*}"
123 entry="${entry#*,}"
124 local metric="${entry%%,*}"
125
126 for xentry in $RA_ROUTES; do
127 local xprefix="${xentry%%,*}"
128 xentry="${xentry#*,}"
129 local xgw="${xentry%%,*}"
130
131 [ -n "$gw" -a -z "$xgw" -a "$addr/$mask" = "$xprefix" ] && duplicate=1
132 done
133
134 if [ -z "$gw" -o "$duplicate" = 1 ]; then
135 proto_add_ipv6_route "$addr" "$mask" "$gw" "$metric" "$valid"
136 else
137 for prefix in $PREFIXES $ADDRESSES; do
138 local paddr="${prefix%%,*}"
139 proto_add_ipv6_route "$addr" "$mask" "$gw" "$metric" "$valid" "$paddr"
140 done
141 fi
142 done
143
144 proto_add_data
145 [ -n "$CER" ] && json_add_string cer "$CER"
146 [ -n "$PASSTHRU" ] && json_add_string passthru "$PASSTHRU"
147 [ -n "$ZONE" ] && json_add_string zone "$ZONE"
148 proto_close_data
149
150 proto_send_update "$INTERFACE"
151
152 MAPTYPE=""
153 MAPRULE=""
154
155 if [ -n "$MAPE" -a -f /lib/netifd/proto/map.sh ]; then
156 MAPTYPE="map-e"
157 MAPRULE="$MAPE"
158 elif [ -n "$MAPT" -a -f /lib/netifd/proto/map.sh -a -f /proc/net/nat46/control ]; then
159 MAPTYPE="map-t"
160 MAPRULE="$MAPT"
161 elif [ -n "$LW4O6" -a -f /lib/netifd/proto/map.sh ]; then
162 MAPTYPE="lw4o6"
163 MAPRULE="$LW4O6"
164 fi
165
166 [ -n "$ZONE" ] || ZONE=$(fw3 -q network $INTERFACE 2>/dev/null)
167
168 if [ "$IFACE_MAP" != 0 -a -n "$MAPTYPE" -a -n "$MAPRULE" ]; then
169 [ -z "$IFACE_MAP" -o "$IFACE_MAP" = 1 ] && IFACE_MAP=${INTERFACE}_4
170 json_init
171 json_add_string name "$IFACE_MAP"
172 json_add_string ifname "@$INTERFACE"
173 json_add_string proto map
174 json_add_string type "$MAPTYPE"
175 json_add_string _prefsig "$prefsig"
176 [ "$MAPTYPE" = lw4o6 ] && json_add_string _addrsig "$addrsig"
177 json_add_string rule "$MAPRULE"
178 json_add_string tunlink "$INTERFACE"
179 [ -n "$ZONE_MAP" ] || ZONE_MAP=$ZONE
180 [ -n "$ZONE_MAP" ] && json_add_string zone "$ZONE_MAP"
181 [ -n "$ENCAPLIMIT_MAP" ] && json_add_string encaplimit "$ENCAPLIMIT_MAP"
182 [ -n "$IFACE_MAP_DELEGATE" ] && json_add_boolean delegate "$IFACE_MAP_DELEGATE"
183 json_close_object
184 ubus call network add_dynamic "$(json_dump)"
185 elif [ -n "$AFTR" -a "$IFACE_DSLITE" != 0 -a -f /lib/netifd/proto/dslite.sh ]; then
186 [ -z "$IFACE_DSLITE" -o "$IFACE_DSLITE" = 1 ] && IFACE_DSLITE=${INTERFACE}_4
187 json_init
188 json_add_string name "$IFACE_DSLITE"
189 json_add_string ifname "@$INTERFACE"
190 json_add_string proto "dslite"
191 json_add_string peeraddr "$AFTR"
192 json_add_string tunlink "$INTERFACE"
193 [ -n "$ZONE_DSLITE" ] || ZONE_DSLITE=$ZONE
194 [ -n "$ZONE_DSLITE" ] && json_add_string zone "$ZONE_DSLITE"
195 [ -n "$ENCAPLIMIT_DSLITE" ] && json_add_string encaplimit "$ENCAPLIMIT_DSLITE"
196 [ -n "$IFACE_DSLITE_DELEGATE" ] && json_add_boolean delegate "$IFACE_DSLITE_DELEGATE"
197 json_close_object
198 ubus call network add_dynamic "$(json_dump)"
199 elif [ "$IFACE_464XLAT" != 0 -a -f /lib/netifd/proto/464xlat.sh ]; then
200 [ -z "$IFACE_464XLAT" -o "$IFACE_464XLAT" = 1 ] && IFACE_464XLAT=${INTERFACE}_4
201 json_init
202 json_add_string name "$IFACE_464XLAT"
203 json_add_string ifname "@$INTERFACE"
204 json_add_string proto "464xlat"
205 json_add_string tunlink "$INTERFACE"
206 json_add_string _addrsig "$addrsig"
207 [ -n "$ZONE_464XLAT" ] || ZONE_464XLAT=$ZONE
208 [ -n "$ZONE_464XLAT" ] && json_add_string zone "$ZONE_464XLAT"
209 [ -n "$IFACE_464XLAT_DELEGATE" ] && json_add_boolean delegate "$IFACE_464XLAT_DELEGATE"
210 json_close_object
211 ubus call network add_dynamic "$(json_dump)"
212 fi
213
214 # TODO: $SNTP_IP $SIP_IP $SNTP_FQDN $SIP_DOMAIN
215 }
216
217 teardown_interface() {
218 proto_init_update "*" 0
219 proto_send_update "$INTERFACE"
220 }
221
222 case "$2" in
223 bound)
224 teardown_interface "$1"
225 setup_interface "$1"
226 ;;
227 informed|updated|rebound)
228 setup_interface "$1"
229 ;;
230 ra-updated)
231 [ -n "$ADDRESSES$RA_ADDRESSES$PREFIXES$USERPREFIX" ] && setup_interface "$1"
232 ;;
233 started|stopped|unbound)
234 teardown_interface "$1"
235 ;;
236 esac
237
238 # user rules
239 [ -f /etc/odhcp6c.user ] && . /etc/odhcp6c.user "$@"
240
241 exit 0