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