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