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