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