netifd: mbim.sh: only check pin1 for failure
[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 [ -e /proc/sys/net/ipv6 ] && proto_config_add_string ipv6
23 proto_config_add_string dhcp
24 proto_config_add_string dhcpv6
25 proto_config_add_string pdptype
26 proto_config_add_int mtu
27 proto_config_add_defaults
28 }
29
30 _proto_mbim_get_field() {
31 local field="$1"
32 shift
33 local mbimconfig="$@"
34 echo "$mbimconfig" | while read -r line; do
35 variable=${line%%:*}
36 [ "$variable" = "$field" ] || continue;
37 value=${line##* }
38 echo -n "$value "
39 done
40 }
41
42 _proto_mbim_setup() {
43 local interface="$1"
44 local tid=2
45 local ret
46
47 local device apn pincode delay auth username password allow_roaming allow_partner
48 local dhcp dhcpv6 pdptype ip4table ip6table mtu $PROTO_DEFAULT_OPTIONS
49 json_get_vars device apn pincode delay auth username password allow_roaming allow_partner
50 json_get_vars dhcp dhcpv6 pdptype ip4table ip6table mtu $PROTO_DEFAULT_OPTIONS
51
52 [ ! -e /proc/sys/net/ipv6 ] && ipv6=0 || json_get_var ipv6 ipv6
53
54 [ -n "$ctl_device" ] && device=$ctl_device
55
56 [ -n "$device" ] || {
57 echo "mbim[$$]" "No control device specified"
58 proto_notify_error "$interface" NO_DEVICE
59 proto_set_available "$interface" 0
60 return 1
61 }
62 [ -c "$device" ] || {
63 echo "mbim[$$]" "The specified control device does not exist"
64 proto_notify_error "$interface" NO_DEVICE
65 proto_set_available "$interface" 0
66 return 1
67 }
68
69 devname="$(basename "$device")"
70 devpath="$(readlink -f /sys/class/usbmisc/$devname/device/)"
71 ifname="$( ls "$devpath"/net )"
72
73 [ -n "$ifname" ] || {
74 echo "mbim[$$]" "Failed to find matching interface"
75 proto_notify_error "$interface" NO_IFNAME
76 proto_set_available "$interface" 0
77 return 1
78 }
79
80 [ -n "$apn" ] || {
81 echo "mbim[$$]" "No APN specified"
82 proto_notify_error "$interface" NO_APN
83 return 1
84 }
85
86 [ -n "$delay" ] && sleep "$delay"
87
88 echo "mbim[$$]" "Reading capabilities"
89 umbim $DBG -n -d $device caps || {
90 echo "mbim[$$]" "Failed to read modem caps"
91 tid=$((tid + 1))
92 umbim $DBG -t $tid -d "$device" disconnect
93 proto_notify_error "$interface" PIN_FAILED
94 return 1
95 }
96 tid=$((tid + 1))
97
98 [ "$pincode" ] && {
99 echo "mbim[$$]" "Sending pin"
100 umbim $DBG -n -t $tid -d $device unlock "$pincode" || {
101 echo "mbim[$$]" "Unable to verify PIN"
102 tid=$((tid + 1))
103 umbim $DBG -t $tid -d "$device" disconnect
104 proto_notify_error "$interface" PIN_FAILED
105 proto_block_restart "$interface"
106 return 1
107 }
108 }
109 tid=$((tid + 1))
110
111 echo "mbim[$$]" "Checking pin"
112 local pinstate="/var/run/mbim.$$.pinstate"
113 umbim $DBG -n -t $tid -d $device pinstate > "$pinstate" 2>&1 || {
114 local pin=$(awk '$2=="pin:" {print $5}' "$pinstate")
115 # we only need pin1 (the SIM pin) to connect
116 [ "$pin" = "pin1" ] && {
117 echo "mbim[$$]" "PIN required"
118 tid=$((tid + 1))
119 umbim $DBG -t $tid -d "$device" disconnect
120 proto_notify_error "$interface" PIN_FAILED
121 proto_block_restart "$interface"
122 return 1
123 }
124 }
125 tid=$((tid + 1))
126
127 echo "mbim[$$]" "Checking subscriber"
128 umbim $DBG -n -t $tid -d $device subscriber || {
129 echo "mbim[$$]" "Subscriber init failed"
130 tid=$((tid + 1))
131 umbim $DBG -t $tid -d "$device" disconnect
132 proto_notify_error "$interface" NO_SUBSCRIBER
133 return 1
134 }
135 tid=$((tid + 1))
136
137 echo "mbim[$$]" "Register with network"
138 connected=0
139 umbim $DBG -n -t $tid -d $device registration
140 reg_status=$?
141 case $reg_status in
142 0) echo "mbim[$$]" "Registered in home mode"
143 tid=$((tid + 1))
144 connected=1;;
145 4) if [ "$allow_roaming" = "1" ]; then
146 echo "mbim[$$]" "Registered in roaming mode"
147 tid=$((tid + 1))
148 connected=1
149 fi;;
150 5) if [ "$allow_partner" = "1" ]; then
151 echo "mbim[$$]" "Registered in partner mode"
152 tid=$((tid + 1))
153 connected=1
154 fi;;
155 esac
156 if [ $connected -ne 1 ]; then
157 echo "mbim[$$]" "Subscriber registration failed (code $reg_status)"
158 tid=$((tid + 1))
159 umbim $DBG -t $tid -d "$device" disconnect
160 proto_notify_error "$interface" NO_REGISTRATION
161 return 1
162 fi
163
164 echo "mbim[$$]" "Attach to network"
165 umbim $DBG -n -t $tid -d $device attach || {
166 echo "mbim[$$]" "Failed to attach to network"
167 tid=$((tid + 1))
168 umbim $DBG -t $tid -d "$device" disconnect
169 proto_notify_error "$interface" ATTACH_FAILED
170 return 1
171 }
172 tid=$((tid + 1))
173
174 pdptype=$(echo "$pdptype" | awk '{print tolower($0)}')
175 [ "$ipv6" = 0 ] && pdptype="ipv4"
176
177 local req_pdptype="" # Pass "default" PDP type to umbim if unconfigured
178 [ "$pdptype" = "ipv4" -o "$pdptype" = "ipv6" -o "$pdptype" = "ipv4v6" ] && req_pdptype="$pdptype:"
179
180 local connect_state
181 echo "mbim[$$]" "Connect to network"
182 connect_state=$(umbim $DBG -n -t $tid -d $device connect "$req_pdptype$apn" "$auth" "$username" "$password") || {
183 echo "mbim[$$]" "Failed to connect bearer"
184 tid=$((tid + 1))
185 umbim $DBG -t $tid -d "$device" disconnect
186 proto_notify_error "$interface" CONNECT_FAILED
187 return 1
188 }
189 tid=$((tid + 1))
190
191 echo "$connect_state"
192 local iptype="$(echo "$connect_state" | grep iptype: | awk '{print $4}')"
193
194 echo "mbim[$$]" "Connected"
195
196 local zone="$(fw3 -q network "$interface" 2>/dev/null)"
197
198 echo "mbim[$$]" "Setting up $ifname"
199 local mbimconfig="$(umbim $DBG -n -t $tid -d $device config)"
200 echo "$mbimconfig"
201 tid=$((tid + 1))
202
203 proto_init_update "$ifname" 1
204 proto_add_data
205 json_add_int tid $tid
206 proto_close_data
207 proto_send_update "$interface"
208
209 [ -z "$dhcp" ] && dhcp="auto"
210 [ -z "$dhcpv6" ] && dhcpv6="auto"
211
212 [ "$iptype" != "ipv6" ] && {
213 json_init
214 json_add_string name "${interface}_4"
215 json_add_string ifname "@$interface"
216 ipv4address=$(_proto_mbim_get_field ipv4address "$mbimconfig")
217 if [ -n "$ipv4address" -a "$dhcp" != 1 ]; then
218 json_add_string proto "static"
219
220 json_add_array ipaddr
221 for address in $ipv4address; do
222 json_add_string "" "$address"
223 done
224 json_close_array
225
226 json_add_string gateway $(_proto_mbim_get_field ipv4gateway "$mbimconfig")
227 elif [ "$dhcp" != 0 ]; then
228 echo "mbim[$$]" "Starting DHCP on $ifname"
229 json_add_string proto "dhcp"
230 fi
231
232 [ "$peerdns" = 0 -a "$dhcp" != 1 ] || {
233 json_add_array dns
234 for server in $(_proto_mbim_get_field ipv4dnsserver "$mbimconfig"); do
235 json_add_string "" "$server"
236 done
237 json_close_array
238 }
239
240 proto_add_dynamic_defaults
241 [ -n "$zone" ] && json_add_string zone "$zone"
242 [ -n "$ip4table" ] && json_add_string ip4table "$ip4table"
243 json_close_object
244 ubus call network add_dynamic "$(json_dump)"
245 }
246
247 [ "$iptype" != "ipv4" ] && {
248 json_init
249 json_add_string name "${interface}_6"
250 json_add_string ifname "@$interface"
251 ipv6address=$(_proto_mbim_get_field ipv6address "$mbimconfig")
252 if [ -n "$ipv6address" -a "$dhcpv6" != 1 ]; then
253 json_add_string proto "static"
254
255 json_add_array ip6addr
256 for address in $ipv6address; do
257 json_add_string "" "$address"
258 done
259 json_close_array
260
261 json_add_array ip6prefix
262 for address in $ipv6address; do
263 json_add_string "" "$address"
264 done
265 json_close_array
266
267 json_add_string ip6gw $(_proto_mbim_get_field ipv6gateway "$mbimconfig")
268
269 elif [ "$dhcpv6" != 0 ]; then
270 echo "mbim[$$]" "Starting DHCPv6 on $ifname"
271 json_add_string proto "dhcpv6"
272 json_add_string extendprefix 1
273 fi
274
275 [ "$peerdns" = 0 -a "$dhcpv6" != 1 ] || {
276 json_add_array dns
277 for server in $(_proto_mbim_get_field ipv6dnsserver "$mbimconfig"); do
278 json_add_string "" "$server"
279 done
280 json_close_array
281 }
282
283 proto_add_dynamic_defaults
284 [ -n "$zone" ] && json_add_string zone "$zone"
285 [ -n "$ip6table" ] && json_add_string ip6table "$ip6table"
286 json_close_object
287 ubus call network add_dynamic "$(json_dump)"
288 }
289
290 [ -z "$mtu" ] && {
291 local ipv4mtu=$(_proto_mbim_get_field ipv4mtu "$mbimconfig")
292 ipv4mtu="${ipv4mtu:-0}"
293 local ipv6mtu=$(_proto_mbim_get_field ipv6mtu "$mbimconfig")
294 ipv6mtu="${ipv6mtu:-0}"
295
296 mtu=$((ipv6mtu > ipv4mtu ? ipv6mtu : ipv4mtu))
297 }
298 [ -n "$mtu" -a "$mtu" != 0 ] && {
299 echo Setting MTU of $ifname to $mtu
300 /sbin/ip link set dev $ifname mtu $mtu
301 }
302 }
303
304 proto_mbim_setup() {
305 local ret
306
307 _proto_mbim_setup $@
308 ret=$?
309
310 rm -f "/var/run/mbim.$$."*
311
312 [ "$ret" = 0 ] || {
313 logger "mbim bringup failed, retry in 15s"
314 sleep 15
315 }
316
317 return $ret
318 }
319
320 proto_mbim_teardown() {
321 local interface="$1"
322
323 local device tid
324 json_get_vars device tid
325
326 [ -n "$ctl_device" ] && device=$ctl_device
327
328 echo "mbim[$$]" "Stopping network"
329 [ -n "$tid" ] && umbim $DBG -t $tid -d "$device" disconnect
330
331 proto_init_update "*" 0
332 proto_send_update "$interface"
333 }
334
335 [ -n "$INCLUDE_ONLY" ] || add_protocol mbim