50913e7fa0461e2732583dd1499dffaba6de5964
[openwrt/openwrt.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 umbim $DBG -n -t $tid -d $device pinstate
113 [ $? -eq 2 ] && {
114 echo "mbim[$$]" "PIN required"
115 tid=$((tid + 1))
116 umbim $DBG -t $tid -d "$device" disconnect
117 proto_notify_error "$interface" PIN_FAILED
118 proto_block_restart "$interface"
119 return 1
120 }
121 tid=$((tid + 1))
122
123 echo "mbim[$$]" "Checking subscriber"
124 umbim $DBG -n -t $tid -d $device subscriber || {
125 echo "mbim[$$]" "Subscriber init failed"
126 tid=$((tid + 1))
127 umbim $DBG -t $tid -d "$device" disconnect
128 proto_notify_error "$interface" NO_SUBSCRIBER
129 return 1
130 }
131 tid=$((tid + 1))
132
133 echo "mbim[$$]" "Register with network"
134 connected=0
135 umbim $DBG -n -t $tid -d $device registration
136 reg_status=$?
137 case $reg_status in
138 0) echo "mbim[$$]" "Registered in home mode"
139 tid=$((tid + 1))
140 connected=1;;
141 4) if [ "$allow_roaming" = "1" ]; then
142 echo "mbim[$$]" "Registered in roaming mode"
143 tid=$((tid + 1))
144 connected=1
145 fi;;
146 5) if [ "$allow_partner" = "1" ]; then
147 echo "mbim[$$]" "Registered in partner mode"
148 tid=$((tid + 1))
149 connected=1
150 fi;;
151 esac
152 if [ $connected -ne 1 ]; then
153 echo "mbim[$$]" "Subscriber registration failed (code $reg_status)"
154 tid=$((tid + 1))
155 umbim $DBG -t $tid -d "$device" disconnect
156 proto_notify_error "$interface" NO_REGISTRATION
157 return 1
158 fi
159
160 echo "mbim[$$]" "Attach to network"
161 umbim $DBG -n -t $tid -d $device attach || {
162 echo "mbim[$$]" "Failed to attach to network"
163 tid=$((tid + 1))
164 umbim $DBG -t $tid -d "$device" disconnect
165 proto_notify_error "$interface" ATTACH_FAILED
166 return 1
167 }
168 tid=$((tid + 1))
169
170 pdptype=$(echo "$pdptype" | awk '{print tolower($0)}')
171 [ "$ipv6" = 0 ] && pdptype="ipv4"
172
173 local req_pdptype="" # Pass "default" PDP type to umbim if unconfigured
174 [ "$pdptype" = "ipv4" -o "$pdptype" = "ipv6" -o "$pdptype" = "ipv4v6" ] && req_pdptype="$pdptype:"
175
176 local connect_state
177 echo "mbim[$$]" "Connect to network"
178 connect_state=$(umbim $DBG -n -t $tid -d $device connect "$req_pdptype$apn" "$auth" "$username" "$password") || {
179 echo "mbim[$$]" "Failed to connect bearer"
180 tid=$((tid + 1))
181 umbim $DBG -t $tid -d "$device" disconnect
182 proto_notify_error "$interface" CONNECT_FAILED
183 return 1
184 }
185 tid=$((tid + 1))
186
187 echo "$connect_state"
188 local iptype="$(echo "$connect_state" | grep iptype: | awk '{print $4}')"
189
190 echo "mbim[$$]" "Connected"
191
192 local zone="$(fw3 -q network "$interface" 2>/dev/null)"
193
194 echo "mbim[$$]" "Setting up $ifname"
195 local mbimconfig="$(umbim $DBG -n -t $tid -d $device config)"
196 echo "$mbimconfig"
197 tid=$((tid + 1))
198
199 proto_init_update "$ifname" 1
200 proto_send_update "$interface"
201
202 [ -z "$dhcp" ] && dhcp="auto"
203 [ -z "$dhcpv6" ] && dhcpv6="auto"
204
205 [ "$iptype" != "ipv6" ] && {
206 json_init
207 json_add_string name "${interface}_4"
208 json_add_string ifname "@$interface"
209 ipv4address=$(_proto_mbim_get_field ipv4address "$mbimconfig")
210 if [ -n "$ipv4address" -a "$dhcp" != 1 ]; then
211 json_add_string proto "static"
212
213 json_add_array ipaddr
214 for address in $ipv4address; do
215 json_add_string "" "$address"
216 done
217 json_close_array
218
219 json_add_string gateway $(_proto_mbim_get_field ipv4gateway "$mbimconfig")
220 elif [ "$dhcp" != 0 ]; then
221 echo "mbim[$$]" "Starting DHCP on $ifname"
222 json_add_string proto "dhcp"
223 fi
224
225 [ "$peerdns" = 0 -a "$dhcp" != 1 ] || {
226 json_add_array dns
227 for server in $(_proto_mbim_get_field ipv4dnsserver "$mbimconfig"); do
228 json_add_string "" "$server"
229 done
230 json_close_array
231 }
232
233 proto_add_dynamic_defaults
234 [ -n "$zone" ] && json_add_string zone "$zone"
235 [ -n "$ip4table" ] && json_add_string ip4table "$ip4table"
236 json_close_object
237 ubus call network add_dynamic "$(json_dump)"
238 }
239
240 [ "$iptype" != "ipv4" ] && {
241 json_init
242 json_add_string name "${interface}_6"
243 json_add_string ifname "@$interface"
244 ipv6address=$(_proto_mbim_get_field ipv6address "$mbimconfig")
245 if [ -n "$ipv6address" -a "$dhcpv6" != 1 ]; then
246 json_add_string proto "static"
247
248 json_add_array ip6addr
249 for address in $ipv6address; do
250 json_add_string "" "$address"
251 done
252 json_close_array
253
254 json_add_array ip6prefix
255 for address in $ipv6address; do
256 json_add_string "" "$address"
257 done
258 json_close_array
259
260 json_add_string ip6gw $(_proto_mbim_get_field ipv6gateway "$mbimconfig")
261
262 elif [ "$dhcpv6" != 0 ]; then
263 echo "mbim[$$]" "Starting DHCPv6 on $ifname"
264 json_add_string proto "dhcpv6"
265 json_add_string extendprefix 1
266 fi
267
268 [ "$peerdns" = 0 -a "$dhcpv6" != 1 ] || {
269 json_add_array dns
270 for server in $(_proto_mbim_get_field ipv6dnsserver "$mbimconfig"); do
271 json_add_string "" "$server"
272 done
273 json_close_array
274 }
275
276 proto_add_dynamic_defaults
277 [ -n "$zone" ] && json_add_string zone "$zone"
278 [ -n "$ip6table" ] && json_add_string ip6table "$ip6table"
279 json_close_object
280 ubus call network add_dynamic "$(json_dump)"
281 }
282
283 [ -z "$mtu" ] && {
284 local ipv4mtu=$(_proto_mbim_get_field ipv4mtu "$mbimconfig")
285 ipv4mtu="${ipv4mtu:-0}"
286 local ipv6mtu=$(_proto_mbim_get_field ipv6mtu "$mbimconfig")
287 ipv6mtu="${ipv6mtu:-0}"
288
289 mtu=$((ipv6mtu > ipv4mtu ? ipv6mtu : ipv4mtu))
290 }
291 [ -n "$mtu" -a "$mtu" != 0 ] && {
292 echo Setting MTU of $ifname to $mtu
293 /sbin/ip link set dev $ifname mtu $mtu
294 }
295
296 uci_set_state network $interface tid "$tid"
297 }
298
299 proto_mbim_setup() {
300 local ret
301
302 _proto_mbim_setup $@
303 ret=$?
304
305 [ "$ret" = 0 ] || {
306 logger "mbim bringup failed, retry in 15s"
307 sleep 15
308 }
309
310 return $ret
311 }
312
313 proto_mbim_teardown() {
314 local interface="$1"
315
316 local device
317 json_get_vars device
318 local tid=$(uci_get_state network $interface tid)
319
320 [ -n "$ctl_device" ] && device=$ctl_device
321
322 echo "mbim[$$]" "Stopping network"
323 [ -n "$tid" ] && {
324 umbim $DBG -t $tid -d "$device" disconnect
325 uci_revert_state network $interface tid
326 }
327
328 proto_init_update "*" 0
329 proto_send_update "$interface"
330 }
331
332 [ -n "$INCLUDE_ONLY" ] || add_protocol mbim