68946a3197e0d767e43ba6f44709e5bf32d68229
[openwrt/staging/chunkeey.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 domain in $DOMAINS; do
24 proto_add_dns_search "$domain"
25 done
26
27 for prefix in $PREFIXES; do
28 proto_add_ipv6_prefix "$prefix"
29 done
30
31 [ -n "$USERPREFIX" ] && proto_add_ipv6_prefix "$USERPREFIX"
32
33 # Merge addresses
34 for entry in $RA_ADDRESSES; do
35 local duplicate=0
36 local addr="${entry%%/*}"
37 for dentry in $ADDRESSES; do
38 local daddr="${dentry%%/*}"
39 [ "$addr" = "$daddr" ] && duplicate=1
40 done
41 [ "$duplicate" = "0" ] && ADDRESSES="$ADDRESSES $entry"
42 done
43
44 for entry in $ADDRESSES; do
45 local addr="${entry%%/*}"
46 entry="${entry#*/}"
47 local mask="${entry%%,*}"
48 entry="${entry#*,}"
49 local preferred="${entry%%,*}"
50 entry="${entry#*,}"
51 local valid="${entry%%,*}"
52
53 proto_add_ipv6_address "$addr" "$mask" "$preferred" "$valid" 1
54 done
55
56 for entry in $RA_ROUTES; do
57 local addr="${entry%%/*}"
58 entry="${entry#*/}"
59 local mask="${entry%%,*}"
60 entry="${entry#*,}"
61 local gw="${entry%%,*}"
62 entry="${entry#*,}"
63 local valid="${entry%%,*}"
64 entry="${entry#*,}"
65 local metric="${entry%%,*}"
66
67 if [ -z "$SOURCE_ROUTING" -o -z "$gw" ]; then
68 proto_add_ipv6_route "$addr" "$mask" "$gw" "$metric" "$valid"
69 else
70 proto_add_ipv6_route "$addr" "$mask" "$gw" "$metric" "$valid" "::/128"
71 for prefix in $PREFIXES $ADDRESSES; do
72 local paddr="${prefix%%,*}"
73 proto_add_ipv6_route "$addr" "$mask" "$gw" "$metric" "$valid" "$paddr"
74 done
75 fi
76 done
77
78 proto_add_data
79 [ -n "$CER" ] && json_add_string cer "$CER"
80 [ -n "$PASSTHRU" ] && json_add_string passthru "$PASSTHRU"
81 [ -n "$ZONE" ] && json_add_string zone "$ZONE"
82 proto_close_data
83
84 proto_send_update "$INTERFACE"
85
86 MAPTYPE=""
87 MAPRULE=""
88
89 if [ -n "$MAPE" -a -f /lib/netifd/proto/map.sh ]; then
90 MAPTYPE="map-e"
91 MAPRULE="$MAPE"
92 elif [ -n "$MAPT" -a -f /lib/netifd/proto/map.sh -a -f /proc/net/nat46/control ]; then
93 MAPTYPE="map-t"
94 MAPRULE="$MAPT"
95 elif [ -n "$LW4O6" -a -f /lib/netifd/proto/map.sh ]; then
96 MAPTYPE="lw4o6"
97 MAPRULE="$LW4O6"
98 fi
99
100 [ -n "$ZONE" ] || ZONE=$(fw3 -q network $INTERFACE)
101
102 if [ "$IFACE_MAP" != 0 -a -n "$MAPTYPE" -a -n "$MAPRULE" ]; then
103 [ -z "$IFACE_MAP" -o "$IFACE_MAP" = 1 ] && IFACE_MAP=${INTERFACE}_map
104 json_init
105 json_add_string name "$IFACE_MAP"
106 json_add_string ifname "@$INTERFACE"
107 json_add_string proto map
108 json_add_string type "$MAPTYPE"
109 json_add_string rule "$MAPRULE"
110 json_add_string tunlink "$INTERFACE"
111 [ -n "$ZONE_MAP" ] || ZONE_MAP=$ZONE
112 [ -n "$ZONE_MAP" ] && json_add_string zone "$ZONE_MAP"
113 [ -n "$IFACE_MAP_DELEGATE" ] && json_add_boolean delegate "$IFACE_MAP_DELEGATE"
114 json_close_object
115 ubus call network add_dynamic "$(json_dump)"
116 elif [ -n "$AFTR_IP " -a "$IFACE_DSLITE" != 0 -a -f /lib/netifd/proto/dslite.sh ]; then
117 [ -z "$IFACE_DSLITE" -o "$IFACE_DSLITE" = 1 ] && IFACE_DSLITE=${INTERFACE}_dslite
118 json_init
119 json_add_string name "$IFACE_DSLITE"
120 json_add_string ifname "@$INTERFACE"
121 json_add_string proto "dslite"
122 json_add_string peeraddr "$AFTR_IP"
123 json_add_string tunlink "$INTERFACE"
124 [ -n "$ZONE_DSLITE" ] || ZONE_DSLITE=$ZONE
125 [ -n "$ZONE_DSLITE" ] && json_add_string zone "$ZONE_DSLITE"
126 [ -n "$IFACE_DSLITE_DELEGATE" ] && json_add_boolean delegate "$IFACE_DSLITE_DELEGATE"
127 json_close_object
128 ubus call network add_dynamic "$(json_dump)"
129 fi
130
131 # TODO: $SNTP_IP $SIP_IP $SNTP_FQDN $SIP_DOMAIN
132 }
133
134 teardown_interface() {
135 proto_init_update "*" 0
136 proto_send_update "$INTERFACE"
137 }
138
139 case "$2" in
140 bound)
141 teardown_interface "$1"
142 setup_interface "$1"
143 ;;
144 informed|updated|rebound|ra-updated)
145 setup_interface "$1"
146 ;;
147 started|stopped|unbound)
148 teardown_interface "$1"
149 ;;
150 esac
151
152 # user rules
153 [ -f /etc/odhcp6c.user ] && . /etc/odhcp6c.user
154
155 exit 0