uqmi: add support of using device symlinks.
[openwrt/openwrt.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
82 echo "Waiting for network registration"
83 while uqmi -s -d "$device" --get-serving-system | grep '"searching"' > /dev/null; do
84 sleep 5;
85 done
86
87 [ -n "$modes" ] && uqmi -s -d "$device" --set-network-modes "$modes"
88
89 echo "Starting network $interface"
90
91 pdptype=`echo "$pdptype" | awk '{print tolower($0)}'`
92 [ "$pdptype" = "ip" -o "$pdptype" = "ipv6" -o "$pdptype" = "ipv4v6" ] || pdptype="ip"
93
94 if [ "$pdptype" = "ip" ]; then
95 [ -z "$autoconnect" ] && autoconnect=1
96 [ "$autoconnect" = 0 ] && autoconnect=""
97 else
98 [ "$autoconnect" = 1 ] || autoconnect=""
99 fi
100
101 [ "$pdptype" = "ip" -o "$pdptype" = "ipv4v6" ] && {
102 cid_4=`uqmi -s -d "$device" --get-client-id wds`
103 [ $? -ne 0 ] && {
104 echo "Unable to obtain client ID"
105 proto_notify_error "$interface" NO_CID
106 return 1
107 }
108
109 uqmi -s -d "$device" --set-client-id wds,"$cid_4" --set-ip-family ipv4 > /dev/null
110
111 # try to clear previous autoconnect state
112 uqmi -s -d "$device" --set-client-id wds,"$cid_4" \
113 --stop-network 0xffffffff \
114 --autoconnect > /dev/null
115
116 pdh_4=`uqmi -s -d "$device" --set-client-id wds,"$cid_4" \
117 --start-network \
118 ${apn:+--apn $apn} \
119 ${profile:+--profile $profile} \
120 ${auth:+--auth-type $auth} \
121 ${username:+--username $username} \
122 ${password:+--password $password} \
123 ${autoconnect:+--autoconnect}`
124 [ $? -ne 0 ] && {
125 echo "Unable to connect IPv4"
126 uqmi -s -d "$device" --set-client-id wds,"$cid_4" --release-client-id wds
127 proto_notify_error "$interface" CALL_FAILED
128 return 1
129 }
130 }
131
132 [ "$pdptype" = "ipv6" -o "$pdptype" = "ipv4v6" ] && {
133 cid_6=`uqmi -s -d "$device" --get-client-id wds`
134 [ $? -ne 0 ] && {
135 echo "Unable to obtain client ID"
136 proto_notify_error "$interface" NO_CID
137 return 1
138 }
139
140 uqmi -s -d "$device" --set-client-id wds,"$cid_6" --set-ip-family ipv6 > /dev/null
141
142 # try to clear previous autoconnect state
143 uqmi -s -d "$device" --set-client-id wds,"$cid_6" \
144 --stop-network 0xffffffff \
145 --autoconnect > /dev/null
146
147 pdh_6=`uqmi -s -d "$device" --set-client-id wds,"$cid_6" \
148 --start-network \
149 ${apn:+--apn $apn} \
150 ${profile:+--profile $profile} \
151 ${auth:+--auth-type $auth} \
152 ${username:+--username $username} \
153 ${password:+--password $password} \
154 ${autoconnect:+--autoconnect}`
155 [ $? -ne 0 ] && {
156 echo "Unable to connect IPv6"
157 uqmi -s -d "$device" --set-client-id wds,"$cid_6" --release-client-id wds
158 proto_notify_error "$interface" CALL_FAILED
159 return 1
160 }
161 }
162
163 echo "Setting up $ifname"
164 proto_init_update "$ifname" 1
165 proto_set_keep 1
166 proto_add_data
167 [ -n "$pdh_4" ] && {
168 json_add_string "cid_4" "$cid_4"
169 json_add_string "pdh_4" "$pdh_4"
170 }
171 [ -n "$pdh_6" ] && {
172 json_add_string "cid_6" "$cid_6"
173 json_add_string "pdh_6" "$pdh_6"
174 }
175 proto_close_data
176 proto_send_update "$interface"
177 [ -n "$pdh_6" ] && {
178 if [ -z "$dhcpv6" -o "$dhcpv6" = 0 ]; then
179 json_load "$(uqmi -s -d $device --set-client-id wds,$cid_6 --get-current-settings)"
180 json_select ipv6
181 json_get_var ip_6 ip
182 json_get_var gateway_6 gateway
183 json_get_var dns1_6 dns1
184 json_get_var dns2_6 dns2
185 json_get_var ip_prefix_length ip-prefix-length
186
187 proto_init_update "$ifname" 1
188 proto_set_keep 1
189 proto_add_ipv6_address "$ip_6" "128"
190 proto_add_ipv6_prefix "${ip_6}/${ip_prefix_length}"
191 proto_add_ipv6_route "$gateway_6" "128"
192 [ "$defaultroute" = 0 ] || proto_add_ipv6_route "::0" 0 "$gateway_6" "" "" "${ip_6}/${ip_prefix_length}"
193 [ "$peerdns" = 0 ] || {
194 proto_add_dns_server "$dns1_6"
195 proto_add_dns_server "$dns2_6"
196 }
197 proto_send_update "$interface"
198 else
199 json_init
200 json_add_string name "${interface}_6"
201 json_add_string ifname "@$interface"
202 json_add_string proto "dhcpv6"
203 proto_add_dynamic_defaults
204 # RFC 7278: Extend an IPv6 /64 Prefix to LAN
205 json_add_string extendprefix 1
206 json_close_object
207 ubus call network add_dynamic "$(json_dump)"
208 fi
209 }
210
211 [ -n "$pdh_4" ] && {
212 json_init
213 json_add_string name "${interface}_4"
214 json_add_string ifname "@$interface"
215 json_add_string proto "dhcp"
216 proto_add_dynamic_defaults
217 json_close_object
218 ubus call network add_dynamic "$(json_dump)"
219 }
220 }
221
222 qmi_wds_stop() {
223 local cid="$1"
224 local pdh="$2"
225
226 [ -n "$cid" ] || return
227
228 uqmi -s -d "$device" --set-client-id wds,"$cid" \
229 --stop-network 0xffffffff \
230 --autoconnect > /dev/null
231
232 [ -n "$pdh" ] && uqmi -s -d "$device" --set-client-id wds,"$cid" --stop-network "$pdh"
233 uqmi -s -d "$device" --set-client-id wds,"$cid" --release-client-id wds
234 }
235
236 proto_qmi_teardown() {
237 local interface="$1"
238
239 local device cid_4 pdh_4 cid_6 pdh_6
240 json_get_vars device
241
242 [ -n "$ctl_device" ] && device=$ctl_device
243
244 echo "Stopping network $interface"
245
246 json_load "$(ubus call network.interface.$interface status)"
247 json_select data
248 json_get_vars cid_4 pdh_4 cid_6 pdh_6
249
250 qmi_wds_stop "$cid_4" "$pdh_4"
251 qmi_wds_stop "$cid_6" "$pdh_6"
252
253 proto_init_update "*" 0
254 proto_send_update "$interface"
255 }
256
257 [ -n "$INCLUDE_ONLY" ] || {
258 add_protocol qmi
259 }