uqmi: fix option ipv6
[openwrt/staging/jow.git] / package / network / utils / uqmi / files / lib / netifd / proto / qmi.sh
1 #!/bin/sh
2
3 [ -n "$INCLUDE_ONLY" ] || {
4 . /lib/functions.sh
5 . ../netifd-proto.sh
6 init_proto "$@"
7 }
8
9 proto_qmi_init_config() {
10 available=1
11 no_device=1
12 proto_config_add_string "device:device"
13 proto_config_add_string apn
14 proto_config_add_string auth
15 proto_config_add_string username
16 proto_config_add_string password
17 proto_config_add_string pincode
18 proto_config_add_string delay
19 proto_config_add_string modes
20 proto_config_add_boolean ipv6
21 proto_config_add_boolean dhcp
22 }
23
24 proto_qmi_setup() {
25 local interface="$1"
26
27 local device apn auth username password pincode delay modes ipv6 dhcp
28 local cid_4 pdh_4 cid_6 pdh_6 ipv4
29 local ip subnet gateway dns1 dns2 ip_6 ip_prefix_length gateway_6 dns1_6 dns2_6
30 json_get_vars device apn auth username password pincode delay modes ipv6 dhcp
31
32 ipv4=1
33
34 [ "$ipv6" = 1 ] || ipv6=""
35
36 [ -n "$ctl_device" ] && device=$ctl_device
37
38 [ -n "$device" ] || {
39 echo "No control device specified"
40 proto_notify_error "$interface" NO_DEVICE
41 proto_set_available "$interface" 0
42 return 1
43 }
44 [ -c "$device" ] || {
45 echo "The specified control device does not exist"
46 proto_notify_error "$interface" NO_DEVICE
47 proto_set_available "$interface" 0
48 return 1
49 }
50
51 devname="$(basename "$device")"
52 devpath="$(readlink -f /sys/class/usbmisc/$devname/device/)"
53 ifname="$( ls "$devpath"/net )"
54 [ -n "$ifname" ] || {
55 echo "The interface could not be found."
56 proto_notify_error "$interface" NO_IFACE
57 proto_set_available "$interface" 0
58 return 1
59 }
60
61 [ -n "$delay" ] && sleep "$delay"
62
63 while uqmi -s -d "$device" --get-pin-status | grep '"UIM uninitialized"' > /dev/null; do
64 sleep 1;
65 done
66
67 [ -n "$pincode" ] && {
68 uqmi -s -d "$device" --verify-pin1 "$pincode" || {
69 echo "Unable to verify PIN"
70 proto_notify_error "$interface" PIN_FAILED
71 proto_block_restart "$interface"
72 return 1
73 }
74 }
75
76 [ -n "$apn" ] || {
77 echo "No APN specified"
78 proto_notify_error "$interface" NO_APN
79 return 1
80 }
81
82 uqmi -s -d "$device" --set-data-format 802.3
83 uqmi -s -d "$device" --wda-set-data-format 802.3
84
85 echo "Waiting for network registration"
86 while uqmi -s -d "$device" --get-serving-system | grep '"searching"' > /dev/null; do
87 sleep 5;
88 done
89
90 [ -n "$modes" ] && uqmi -s -d "$device" --set-network-modes "$modes"
91
92 echo "Starting network $apn"
93
94 cid_4=`uqmi -s -d "$device" --get-client-id wds`
95 [ $? -ne 0 ] && {
96 echo "Unable to obtain client ID"
97 proto_notify_error "$interface" NO_CID
98 return 1
99 }
100
101 pdh_4=`uqmi -s -d "$device" --set-client-id wds,"$cid_4" \
102 --start-network "$apn" \
103 ${auth:+--auth-type $auth} \
104 ${username:+--username $username} \
105 ${password:+--password $password} \
106 --ip-family ipv4`
107 [ $? -ne 0 ] && {
108 echo "Unable to connect IPv4"
109 uqmi -s -d "$device" --set-client-id wds,"$cid_4" --release-client-id wds
110 ipv4=""
111 }
112
113 [ -n "$ipv6" ] && {
114 cid_6=`uqmi -s -d "$device" --get-client-id wds`
115 if [ $? = 0 ]; then
116 pdh_6=`uqmi -s -d "$device" --set-client-id wds,"$cid_6" \
117 --start-network "$apn" \
118 ${auth:+--auth-type $auth} \
119 ${username:+--username $username} \
120 ${password:+--password $password} \
121 --ip-family ipv6`
122 [ $? -ne 0 ] && {
123 echo "Unable to connect IPv6"
124 uqmi -s -d "$device" --set-client-id wds,"$cid_6" --release-client-id wds
125 ipv6=""
126 }
127 else
128 echo "Unable to connect IPv6"
129 ipv6=""
130 fi
131 }
132
133 [ -z "$ipv4" -a -z "$ipv6" ] && {
134 echo "Unable to connect"
135 proto_notify_error "$interface" CALL_FAILED
136 return 1
137 }
138
139 if [ -z "$dhcp" -o "$dhcp" = 0 ]; then
140 echo "Setting up $ifname"
141 [ -n "$ipv4" ] && {
142 json_load "$(uqmi -s -d $device --set-client-id wds,$cid_4 --get-current-settings)"
143 json_select ipv4
144 json_get_vars ip subnet gateway dns1 dns2
145
146 proto_init_update "$ifname" 1
147 proto_set_keep 1
148 proto_add_ipv4_address "$ip" "$subnet"
149 proto_add_dns_server "$dns1"
150 proto_add_dns_server "$dns2"
151 proto_add_ipv4_route "0.0.0.0" 0 "$gateway"
152 proto_add_data
153 json_add_string "cid_4" "$cid_4"
154 json_add_string "pdh_4" "$pdh_4"
155 proto_close_data
156 proto_send_update "$interface"
157 }
158
159 [ -n "$ipv6" ] && {
160 json_load "$(uqmi -s -d $device --set-client-id wds,$cid_6 --get-current-settings)"
161 json_select ipv6
162 json_get_var ip_6 ip
163 json_get_var gateway_6 gateway
164 json_get_var dns1_6 dns1
165 json_get_var dns2_6 dns2
166 json_get_var ip_prefix_length ip-prefix-length
167
168 proto_init_update "$ifname" 1
169 proto_set_keep 1
170 # RFC 7278: Extend an IPv6 /64 Prefix to LAN
171 proto_add_ipv6_address "$ip_6" "128"
172 proto_add_ipv6_prefix "${ip_6}/${ip_prefix_length}"
173 proto_add_ipv6_route "$gateway_6" "128"
174 proto_add_ipv6_route "::0" 0 "$gateway_6" "" "" "${ip_6}/${ip_prefix_length}"
175 proto_add_dns_server "$dns1_6"
176 proto_add_dns_server "$dns2_6"
177 proto_add_data
178 json_add_string "cid_6" "$cid_6"
179 json_add_string "pdh_6" "$pdh_6"
180 proto_close_data
181 proto_send_update "$interface"
182 }
183 else
184 echo "Starting DHCP on $ifname"
185 proto_init_update "$ifname" 1
186 proto_add_data
187 [ -n "$ipv4" ] && {
188 json_add_string "cid_4" "$cid_4"
189 json_add_string "pdh_4" "$pdh_4"
190 }
191 [ -n "$ipv6" ] && {
192 json_add_string "cid_6" "$cid_6"
193 json_add_string "pdh_6" "$pdh_6"
194 }
195 proto_close_data
196 proto_send_update "$interface"
197
198 [ -n "$ipv4" ] && {
199 json_init
200 json_add_string name "${interface}_4"
201 json_add_string ifname "@$interface"
202 json_add_string proto "dhcp"
203 json_close_object
204 ubus call network add_dynamic "$(json_dump)"
205 }
206
207 [ -n "$ipv6" ] && {
208 json_init
209 json_add_string name "${interface}_6"
210 json_add_string ifname "@$interface"
211 json_add_string proto "dhcpv6"
212 # RFC 7278: Extend an IPv6 /64 Prefix to LAN
213 json_add_string extendprefix 1
214 json_close_object
215 ubus call network add_dynamic "$(json_dump)"
216 }
217 fi
218 }
219
220 proto_qmi_teardown() {
221 local interface="$1"
222
223 local device cid_4 pdh_4 cid_6 pdh_6
224 json_get_vars device
225
226 [ -n "$ctl_device" ] && device=$ctl_device
227
228 echo "Stopping network"
229
230 json_load "$(ubus call network.interface.$interface status)"
231 json_select data
232 json_get_vars cid_4 pdh_4 cid_6 pdh_6
233
234 [ -n "$cid_4" ] && {
235 [ -n "$pdh_4" ] && {
236 uqmi -s -d "$device" --set-client-id wds,"$cid_4" --stop-network "$pdh_4"
237 uqmi -s -d "$device" --set-client-id wds,"$cid_4" --release-client-id wds
238 }
239 }
240 [ -n "$cid_6" ] && {
241 [ -n "$pdh_6" ] && {
242 uqmi -s -d "$device" --set-client-id wds,"$cid_6" --stop-network "$pdh_6"
243 uqmi -s -d "$device" --set-client-id wds,"$cid_6" --release-client-id wds
244 }
245 }
246
247 proto_init_update "*" 0
248 proto_send_update "$interface"
249 }
250
251 [ -n "$INCLUDE_ONLY" ] || {
252 add_protocol qmi
253 }