umbim: fail connect step immediately
[openwrt/staging/jow.git] / package / network / utils / umbim / files / lib / netifd / proto / mbim.sh
1 #!/bin/sh
2
3 [ -n "$INCLUDE_ONLY" ] || {
4 . /lib/functions.sh
5 . ../netifd-proto.sh
6 init_proto "$@"
7 }
8 #DBG=-v
9
10 proto_mbim_init_config() {
11 available=1
12 no_device=1
13 proto_config_add_string "device:device"
14 proto_config_add_string apn
15 proto_config_add_string pincode
16 proto_config_add_string delay
17 proto_config_add_boolean allow_roaming
18 proto_config_add_boolean allow_partner
19 proto_config_add_string auth
20 proto_config_add_string username
21 proto_config_add_string password
22 proto_config_add_boolean dhcp
23 proto_config_add_string pdptype
24 proto_config_add_defaults
25 }
26
27 _proto_mbim_setup() {
28 local interface="$1"
29 local tid=2
30 local ret
31
32 local device apn pincode delay allow_roaming allow_partner dhcp pdptype $PROTO_DEFAULT_OPTIONS
33 json_get_vars device apn pincode delay auth username password allow_roaming allow_partner dhcp pdptype $PROTO_DEFAULT_OPTIONS
34
35 [ -n "$ctl_device" ] && device=$ctl_device
36
37 [ -n "$device" ] || {
38 echo "mbim[$$]" "No control device specified"
39 proto_notify_error "$interface" NO_DEVICE
40 proto_set_available "$interface" 0
41 return 1
42 }
43 [ -c "$device" ] || {
44 echo "mbim[$$]" "The specified control device does not exist"
45 proto_notify_error "$interface" NO_DEVICE
46 proto_set_available "$interface" 0
47 return 1
48 }
49
50 devname="$(basename "$device")"
51 devpath="$(readlink -f /sys/class/usbmisc/$devname/device/)"
52 ifname="$( ls "$devpath"/net )"
53
54 [ -n "$ifname" ] || {
55 echo "mbim[$$]" "Failed to find matching interface"
56 proto_notify_error "$interface" NO_IFNAME
57 proto_set_available "$interface" 0
58 return 1
59 }
60
61 [ -n "$apn" ] || {
62 echo "mbim[$$]" "No APN specified"
63 proto_notify_error "$interface" NO_APN
64 return 1
65 }
66
67 [ -n "$delay" ] && sleep "$delay"
68
69 echo "mbim[$$]" "Reading capabilities"
70 umbim $DBG -n -d $device caps || {
71 echo "mbim[$$]" "Failed to read modem caps"
72 tid=$((tid + 1))
73 umbim $DBG -t $tid -d "$device" disconnect
74 proto_notify_error "$interface" PIN_FAILED
75 return 1
76 }
77 tid=$((tid + 1))
78
79 [ "$pincode" ] && {
80 echo "mbim[$$]" "Sending pin"
81 umbim $DBG -n -t $tid -d $device unlock "$pincode" || {
82 echo "mbim[$$]" "Unable to verify PIN"
83 tid=$((tid + 1))
84 umbim $DBG -t $tid -d "$device" disconnect
85 proto_notify_error "$interface" PIN_FAILED
86 proto_block_restart "$interface"
87 return 1
88 }
89 }
90 tid=$((tid + 1))
91
92 echo "mbim[$$]" "Checking pin"
93 umbim $DBG -n -t $tid -d $device pinstate
94 [ $? -eq 2 ] && {
95 echo "mbim[$$]" "PIN required"
96 tid=$((tid + 1))
97 umbim $DBG -t $tid -d "$device" disconnect
98 proto_notify_error "$interface" PIN_FAILED
99 proto_block_restart "$interface"
100 return 1
101 }
102 tid=$((tid + 1))
103
104 echo "mbim[$$]" "Checking subscriber"
105 umbim $DBG -n -t $tid -d $device subscriber || {
106 echo "mbim[$$]" "Subscriber init failed"
107 tid=$((tid + 1))
108 umbim $DBG -t $tid -d "$device" disconnect
109 proto_notify_error "$interface" NO_SUBSCRIBER
110 return 1
111 }
112 tid=$((tid + 1))
113
114 echo "mbim[$$]" "Register with network"
115 connected=0
116 umbim $DBG -n -t $tid -d $device registration
117 reg_status=$?
118 case $reg_status in
119 0) echo "mbim[$$]" "Registered in home mode"
120 tid=$((tid + 1))
121 connected=1;;
122 4) if [ "$allow_roaming" = "1" ]; then
123 echo "mbim[$$]" "Registered in roaming mode"
124 tid=$((tid + 1))
125 connected=1
126 fi;;
127 5) if [ "$allow_partner" = "1" ]; then
128 echo "mbim[$$]" "Registered in partner mode"
129 tid=$((tid + 1))
130 connected=1
131 fi;;
132 esac
133 if [ $connected -ne 1 ]; then
134 echo "mbim[$$]" "Subscriber registration failed (code $reg_status)"
135 tid=$((tid + 1))
136 umbim $DBG -t $tid -d "$device" disconnect
137 proto_notify_error "$interface" NO_REGISTRATION
138 return 1
139 fi
140
141 echo "mbim[$$]" "Attach to network"
142 umbim $DBG -n -t $tid -d $device attach || {
143 echo "mbim[$$]" "Failed to attach to network"
144 tid=$((tid + 1))
145 umbim $DBG -t $tid -d "$device" disconnect
146 proto_notify_error "$interface" ATTACH_FAILED
147 return 1
148 }
149 tid=$((tid + 1))
150
151 pdptype=$(echo "$pdptype" | awk '{print tolower($0)}')
152 [ "$pdptype" = "ipv4" -o "$pdptype" = "ipv6" -o "$pdptype" = "ipv4v6" ] || pdptype="ipv4v6"
153
154 echo "mbim[$$]" "Connect to network"
155 umbim $DBG -n -t $tid -d $device connect "$pdptype:$apn" "$auth" "$username" "$password" || {
156 echo "mbim[$$]" "Failed to connect bearer"
157 tid=$((tid + 1))
158 umbim $DBG -t $tid -d "$device" disconnect
159 proto_notify_error "$interface" CONNECT_FAILED
160 return 1
161 }
162 tid=$((tid + 1))
163
164 echo "mbim[$$]" "Connected"
165
166 if [ "$dhcp" = 0 ]; then
167 echo "mbim[$$]" "Setting up $ifname"
168 eval $(umbim $DBG -n -t $tid -d $device config | sed 's/: /=/g')
169 tid=$((tid + 1))
170
171 proto_init_update "$ifname" 1
172 proto_send_update "$interface"
173
174 [ "$pdptype" = "ipv4" -o "$pdptype" = "ipv4v6" ] && {
175 json_init
176 json_add_string name "${interface}_4"
177 json_add_string ifname "@$interface"
178 json_add_string proto "static"
179 json_add_array ipaddr
180 json_add_string "" "$ipv4address"
181 json_close_array
182 json_add_string gateway "$ipv4gateway"
183 json_add_array dns
184 json_add_string "" "$ipv4dnsserver"
185 json_close_array
186 proto_add_dynamic_defaults
187 json_close_object
188 ubus call network add_dynamic "$(json_dump)"
189 }
190
191 [ "$pdptype" = "ipv6" -o "$pdptype" = "ipv4v6" ] && {
192 json_init
193 json_add_string name "${interface}_6"
194 json_add_string ifname "@$interface"
195 json_add_string proto "static"
196 json_add_array ip6addr
197 json_add_string "" "$ipv6address"
198 json_close_array
199 json_add_string ip6gw "$ipv6gateway"
200 json_add_array dns
201 json_add_string "" "$ipv6dnsserver"
202 json_close_array
203 proto_add_dynamic_defaults
204 json_close_object
205 ubus call network add_dynamic "$(json_dump)"
206 }
207 else
208 echo "mbim[$$]" "Starting DHCP on $ifname"
209 proto_init_update "$ifname" 1
210 proto_send_update "$interface"
211
212 [ "$pdptype" = "ipv4" -o "$pdptype" = "ipv4v6" ] && {
213 json_init
214 json_add_string name "${interface}_4"
215 json_add_string ifname "@$interface"
216 json_add_string proto "dhcp"
217 proto_add_dynamic_defaults
218 json_close_object
219 ubus call network add_dynamic "$(json_dump)"
220 }
221
222 [ "$pdptype" = "ipv6" -o "$pdptype" = "ipv4v6" ] && {
223 json_init
224 json_add_string name "${interface}_6"
225 json_add_string ifname "@$interface"
226 json_add_string proto "dhcpv6"
227 json_add_string extendprefix 1
228 proto_add_dynamic_defaults
229 json_close_object
230 ubus call network add_dynamic "$(json_dump)"
231 }
232 fi
233
234 uci_set_state network $interface tid "$tid"
235 }
236
237 proto_mbim_setup() {
238 local ret
239
240 _proto_mbim_setup $@
241 ret=$?
242
243 [ "$ret" = 0 ] || {
244 logger "mbim bringup failed, retry in 15s"
245 sleep 15
246 }
247
248 return $ret
249 }
250
251 proto_mbim_teardown() {
252 local interface="$1"
253
254 local device
255 json_get_vars device
256 local tid=$(uci_get_state network $interface tid)
257
258 [ -n "$ctl_device" ] && device=$ctl_device
259
260 echo "mbim[$$]" "Stopping network"
261 [ -n "$tid" ] && {
262 umbim $DBG -t $tid -d "$device" disconnect
263 uci_revert_state network $interface tid
264 }
265
266 proto_init_update "*" 0
267 proto_send_update "$interface"
268 }
269
270 [ -n "$INCLUDE_ONLY" ] || add_protocol mbim