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