dhcpv6: add missing dollar sign in dhcpv6 script (FS#874)
[openwrt/staging/blogic.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 fi
85 done
86
87 for entry in $RA_ROUTES; do
88 local duplicate=$NOSOURCEFILTER
89 local addr="${entry%%/*}"
90 entry="${entry#*/}"
91 local mask="${entry%%,*}"
92 entry="${entry#*,}"
93 local gw="${entry%%,*}"
94 entry="${entry#*,}"
95 local valid="${entry%%,*}"
96 entry="${entry#*,}"
97 local metric="${entry%%,*}"
98
99 for xentry in $RA_ROUTES; do
100 local xprefix="${xentry%%,*}"
101 xentry="${xentry#*,}"
102 local xgw="${xentry%%,*}"
103
104 [ -n "$gw" -a -z "$xgw" -a "$addr/$mask" = "$xprefix" ] && duplicate=1
105 done
106
107 if [ -z "$gw" -o "$duplicate" = 1 ]; then
108 proto_add_ipv6_route "$addr" "$mask" "$gw" "$metric" "$valid"
109 else
110 for prefix in $PREFIXES $ADDRESSES; do
111 local paddr="${prefix%%,*}"
112 proto_add_ipv6_route "$addr" "$mask" "$gw" "$metric" "$valid" "$paddr"
113 done
114 fi
115 done
116
117 proto_add_data
118 [ -n "$CER" ] && json_add_string cer "$CER"
119 [ -n "$PASSTHRU" ] && json_add_string passthru "$PASSTHRU"
120 [ -n "$ZONE" ] && json_add_string zone "$ZONE"
121 proto_close_data
122
123 proto_send_update "$INTERFACE"
124
125 MAPTYPE=""
126 MAPRULE=""
127
128 if [ -n "$MAPE" -a -f /lib/netifd/proto/map.sh ]; then
129 MAPTYPE="map-e"
130 MAPRULE="$MAPE"
131 elif [ -n "$MAPT" -a -f /lib/netifd/proto/map.sh -a -f /proc/net/nat46/control ]; then
132 MAPTYPE="map-t"
133 MAPRULE="$MAPT"
134 elif [ -n "$LW4O6" -a -f /lib/netifd/proto/map.sh ]; then
135 MAPTYPE="lw4o6"
136 MAPRULE="$LW4O6"
137 fi
138
139 [ -n "$ZONE" ] || ZONE=$(fw3 -q network $INTERFACE 2>/dev/null)
140
141 if [ "$IFACE_MAP" != 0 -a -n "$MAPTYPE" -a -n "$MAPRULE" ]; then
142 [ -z "$IFACE_MAP" -o "$IFACE_MAP" = 1 ] && IFACE_MAP=${INTERFACE}_4
143 json_init
144 json_add_string name "$IFACE_MAP"
145 json_add_string ifname "@$INTERFACE"
146 json_add_string proto map
147 json_add_string type "$MAPTYPE"
148 json_add_string _prefsig "$prefsig"
149 [ "$MAPTYPE" = lw4o6 ] && json_add_string _addrsig "$addrsig"
150 json_add_string rule "$MAPRULE"
151 json_add_string tunlink "$INTERFACE"
152 [ -n "$ZONE_MAP" ] || ZONE_MAP=$ZONE
153 [ -n "$ZONE_MAP" ] && json_add_string zone "$ZONE_MAP"
154 [ -n "$IFACE_MAP_DELEGATE" ] && json_add_boolean delegate "$IFACE_MAP_DELEGATE"
155 json_close_object
156 ubus call network add_dynamic "$(json_dump)"
157 elif [ -n "$AFTR" -a "$IFACE_DSLITE" != 0 -a -f /lib/netifd/proto/dslite.sh ]; then
158 [ -z "$IFACE_DSLITE" -o "$IFACE_DSLITE" = 1 ] && IFACE_DSLITE=${INTERFACE}_4
159 json_init
160 json_add_string name "$IFACE_DSLITE"
161 json_add_string ifname "@$INTERFACE"
162 json_add_string proto "dslite"
163 json_add_string peeraddr "$AFTR"
164 json_add_string tunlink "$INTERFACE"
165 [ -n "$ZONE_DSLITE" ] || ZONE_DSLITE=$ZONE
166 [ -n "$ZONE_DSLITE" ] && json_add_string zone "$ZONE_DSLITE"
167 [ -n "$IFACE_DSLITE_DELEGATE" ] && json_add_boolean delegate "$IFACE_DSLITE_DELEGATE"
168 json_close_object
169 ubus call network add_dynamic "$(json_dump)"
170 elif [ "$IFACE_464XLAT" != 0 -a -f /lib/netifd/proto/464xlat.sh ]; then
171 [ -z "$IFACE_464XLAT" -o "$IFACE_464XLAT" = 1 ] && IFACE_464XLAT=${INTERFACE}_4
172 json_init
173 json_add_string name "$IFACE_464XLAT"
174 json_add_string ifname "@$INTERFACE"
175 json_add_string proto "464xlat"
176 json_add_string tunlink "$INTERFACE"
177 json_add_string _addrsig "$addrsig"
178 [ -n "$ZONE_464XLAT" ] || ZONE_464XLAT=$ZONE
179 [ -n "$ZONE_464XLAT" ] && json_add_string zone "$ZONE_464XLAT"
180 [ -n "$IFACE_464XLAT_DELEGATE" ] && json_add_boolean delegate "$IFACE_464XLAT_DELEGATE"
181 json_close_object
182 ubus call network add_dynamic "$(json_dump)"
183 fi
184
185 # Apply IPv6 / ND configuration
186 HOPLIMIT=$(cat /proc/sys/net/ipv6/conf/$device/hop_limit)
187 [ -n "$RA_HOPLIMIT" -a -n "$HOPLIMIT" ] && [ "$RA_HOPLIMIT" -gt "$HOPLIMIT" ] && echo "$RA_HOPLIMIT" > /proc/sys/net/ipv6/conf/$device/hop_limit
188 [ -n "$RA_MTU" ] && [ "$RA_MTU" -ge 1280 ] && echo "$RA_MTU" > /proc/sys/net/ipv6/conf/$device/mtu 2>/dev/null
189 [ -n "$RA_REACHABLE" ] && [ "$RA_REACHABLE" -gt 0 ] && echo "$RA_REACHABLE" > /proc/sys/net/ipv6/neigh/$device/base_reachable_time_ms
190 [ -n "$RA_RETRANSMIT" ] && [ "$RA_RETRANSMIT" -gt 0 ] && echo "$RA_RETRANSMIT" > /proc/sys/net/ipv6/neigh/$device/retrans_time_ms
191
192 # TODO: $SNTP_IP $SIP_IP $SNTP_FQDN $SIP_DOMAIN
193 }
194
195 teardown_interface() {
196 proto_init_update "*" 0
197 proto_send_update "$INTERFACE"
198 }
199
200 case "$2" in
201 bound)
202 teardown_interface "$1"
203 setup_interface "$1"
204 ;;
205 informed|updated|rebound)
206 setup_interface "$1"
207 ;;
208 ra-updated)
209 [ -n "$ADDRESSES$RA_ADDRESSES$PREFIXES$USERPREFIX" ] && setup_interface "$1"
210 ;;
211 started|stopped|unbound)
212 teardown_interface "$1"
213 ;;
214 esac
215
216 # user rules
217 [ -f /etc/odhcp6c.user ] && . /etc/odhcp6c.user "$@"
218
219 exit 0