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