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