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