81c1c3525f725ae8472095159c3861849848ed34
[oweals/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_string delay
19         proto_config_add_string modes
20         proto_config_add_boolean ipv6
21         proto_config_add_boolean dhcp
22         proto_config_add_int metric
23 }
24
25 proto_qmi_setup() {
26         local interface="$1"
27
28         local device apn auth username password pincode delay modes ipv6 dhcp metric
29         local cid_4 pdh_4 cid_6 pdh_6 ipv4
30         local ip subnet gateway dns1 dns2 ip_6 ip_prefix_length gateway_6 dns1_6 dns2_6
31         json_get_vars device apn auth username password pincode delay modes ipv6 dhcp metric
32
33         ipv4=1
34
35         [ "$ipv6" = 1 ] || ipv6=""
36
37         [ "$metric" = "" ] && metric="0"
38
39         [ -n "$ctl_device" ] && device=$ctl_device
40
41         [ -n "$device" ] || {
42                 echo "No control device specified"
43                 proto_notify_error "$interface" NO_DEVICE
44                 proto_set_available "$interface" 0
45                 return 1
46         }
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         [ -n "$apn" ] || {
80                 echo "No APN specified"
81                 proto_notify_error "$interface" NO_APN
82                 return 1
83         }
84
85         uqmi -s -d "$device" --set-data-format 802.3
86         uqmi -s -d "$device" --wda-set-data-format 802.3
87
88         echo "Waiting for network registration"
89         while uqmi -s -d "$device" --get-serving-system | grep '"searching"' > /dev/null; do
90                 sleep 5;
91         done
92
93         [ -n "$modes" ] && uqmi -s -d "$device" --set-network-modes "$modes"
94
95         echo "Starting network $apn"
96
97         cid_4=`uqmi -s -d "$device" --get-client-id wds`
98         [ $? -ne 0 ] && {
99                 echo "Unable to obtain client ID"
100                 proto_notify_error "$interface" NO_CID
101                 return 1
102         }
103
104         pdh_4=`uqmi -s -d "$device" --set-client-id wds,"$cid_4" \
105                 --start-network "$apn" \
106                 ${auth:+--auth-type $auth} \
107                 ${username:+--username $username} \
108                 ${password:+--password $password} \
109                 --ip-family ipv4`
110         [ $? -ne 0 ] && {
111                 echo "Unable to connect IPv4"
112                 uqmi -s -d "$device" --set-client-id wds,"$cid_4" --release-client-id wds
113                 ipv4=""
114         }
115
116         [ -n "$ipv6" ] && {
117                 cid_6=`uqmi -s -d "$device" --get-client-id wds`
118                 if [ $? = 0 ]; then
119                         pdh_6=`uqmi -s -d "$device" --set-client-id wds,"$cid_6" \
120                                 --start-network "$apn" \
121                                 ${auth:+--auth-type $auth} \
122                                 ${username:+--username $username} \
123                                 ${password:+--password $password} \
124                                 --ip-family ipv6`
125                         [ $? -ne 0 ] && {
126                                 echo "Unable to connect IPv6"
127                                 uqmi -s -d "$device" --set-client-id wds,"$cid_6" --release-client-id wds
128                                 ipv6=""
129                         }
130                 else
131                         echo "Unable to connect IPv6"
132                         ipv6=""
133                 fi
134         }
135
136         [ -z "$ipv4" -a -z "$ipv6" ] && {
137                 echo "Unable to connect"
138                 proto_notify_error "$interface" CALL_FAILED
139                 return 1
140         }
141
142         echo "Setting up $ifname"
143         proto_init_update "$ifname" 1
144         proto_add_data
145         [ -n "$ipv4" ] && {
146                 json_add_string "cid_4" "$cid_4"
147                 json_add_string "pdh_4" "$pdh_4"
148         }
149         [ -n "$ipv6" ] && {
150                 json_add_string "cid_6" "$cid_6"
151                 json_add_string "pdh_6" "$pdh_6"
152         }
153
154         [ -n "$ipv6" ] && {
155                 if [ -z "$dhcp" -o "$dhcp" = 0 ]; then
156                         json_load "$(uqmi -s -d $device --set-client-id wds,$cid_6 --get-current-settings)"
157                         json_select ipv6
158                         json_get_var ip_6 ip
159                         json_get_var gateway_6 gateway
160                         json_get_var dns1_6 dns1
161                         json_get_var dns2_6 dns2
162                         json_get_var ip_prefix_length ip-prefix-length
163
164                         # RFC 7278: Extend an IPv6 /64 Prefix to LAN
165                         proto_add_ipv6_address "$ip_6" "128"
166                         proto_add_ipv6_prefix "${ip_6}/${ip_prefix_length}"
167                         proto_add_ipv6_route "$gateway_6" "128"
168                         proto_add_ipv6_route "::0" 0 "$gateway_6" "" "" "${ip_6}/${ip_prefix_length}"
169                         proto_add_dns_server "$dns1_6"
170                         proto_add_dns_server "$dns2_6"
171                         proto_add_data
172                         json_add_string "cid_6" "$cid_6"
173                         json_add_string "pdh_6" "$pdh_6"
174                 else
175                         json_init
176                         json_add_string name "${interface}_6"
177                         json_add_string ifname "@$interface"
178                         json_add_string proto "dhcpv6"
179                         json_add_int metric "$metric"
180                         # RFC 7278: Extend an IPv6 /64 Prefix to LAN
181                         json_add_string extendprefix 1
182                         json_close_object
183                         ubus call network add_dynamic "$(json_dump)"
184                 fi
185         }
186
187         proto_close_data
188         proto_send_update "$interface"
189
190         [ -n "$ipv4" ] && {
191                 json_init
192                 json_add_string name "${interface}_4"
193                 json_add_string ifname "@$interface"
194                 json_add_string proto "dhcp"
195                 json_add_int metric "$metric"
196                 json_close_object
197                 ubus call network add_dynamic "$(json_dump)"
198         }
199 }
200
201 proto_qmi_teardown() {
202         local interface="$1"
203
204         local device cid_4 pdh_4 cid_6 pdh_6
205         json_get_vars device
206
207         [ -n "$ctl_device" ] && device=$ctl_device
208
209         echo "Stopping network"
210
211         json_load "$(ubus call network.interface.$interface status)"
212         json_select data
213         json_get_vars cid_4 pdh_4 cid_6 pdh_6
214
215         [ -n "$cid_4" ] && {
216                 [ -n "$pdh_4" ] && {
217                         uqmi -s -d "$device" --set-client-id wds,"$cid_4" --stop-network "$pdh_4"
218                         uqmi -s -d "$device" --set-client-id wds,"$cid_4" --release-client-id wds
219                 }
220         }
221         [ -n "$cid_6" ] && {
222                 [ -n "$pdh_6" ] && {
223                         uqmi -s -d "$device" --set-client-id wds,"$cid_6" --stop-network "$pdh_6"
224                         uqmi -s -d "$device" --set-client-id wds,"$cid_6" --release-client-id wds
225                 }
226         }
227
228         proto_init_update "*" 0
229         proto_send_update "$interface"
230 }
231
232 [ -n "$INCLUDE_ONLY" ] || {
233         add_protocol qmi
234 }