3 [ -n "$INCLUDE_ONLY" ] ||
{
10 proto_mbim_init_config
() {
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_boolean dhcp
24 proto_config_add_boolean dhcpv6
25 proto_config_add_string pdptype
26 proto_config_add_int mtu
27 proto_config_add_defaults
30 _proto_mbim_get_field
() {
34 echo "$mbimconfig" |
while read -r line
; do
36 [ "$variable" = "$field" ] ||
continue;
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
52 [ ! -e /proc
/sys
/net
/ipv6
] && ipv6
=0 || json_get_var ipv6 ipv6
54 [ -n "$ctl_device" ] && device
=$ctl_device
57 echo "mbim[$$]" "No control device specified"
58 proto_notify_error
"$interface" NO_DEVICE
59 proto_set_available
"$interface" 0
63 echo "mbim[$$]" "The specified control device does not exist"
64 proto_notify_error
"$interface" NO_DEVICE
65 proto_set_available
"$interface" 0
69 devname
="$(basename "$device")"
70 devpath
="$(readlink -f /sys/class/usbmisc/$devname/device/)"
71 ifname
="$( ls "$devpath"/net )"
74 echo "mbim[$$]" "Failed to find matching interface"
75 proto_notify_error
"$interface" NO_IFNAME
76 proto_set_available
"$interface" 0
81 echo "mbim[$$]" "No APN specified"
82 proto_notify_error
"$interface" NO_APN
86 [ -n "$delay" ] && sleep "$delay"
88 echo "mbim[$$]" "Reading capabilities"
89 umbim
$DBG -n -d $device caps ||
{
90 echo "mbim[$$]" "Failed to read modem caps"
92 umbim
$DBG -t $tid -d "$device" disconnect
93 proto_notify_error
"$interface" PIN_FAILED
99 echo "mbim[$$]" "Sending pin"
100 umbim
$DBG -n -t $tid -d $device unlock
"$pincode" ||
{
101 echo "mbim[$$]" "Unable to verify PIN"
103 umbim
$DBG -t $tid -d "$device" disconnect
104 proto_notify_error
"$interface" PIN_FAILED
105 proto_block_restart
"$interface"
111 echo "mbim[$$]" "Checking pin"
112 umbim
$DBG -n -t $tid -d $device pinstate
114 echo "mbim[$$]" "PIN required"
116 umbim
$DBG -t $tid -d "$device" disconnect
117 proto_notify_error
"$interface" PIN_FAILED
118 proto_block_restart
"$interface"
123 echo "mbim[$$]" "Checking subscriber"
124 umbim
$DBG -n -t $tid -d $device subscriber ||
{
125 echo "mbim[$$]" "Subscriber init failed"
127 umbim
$DBG -t $tid -d "$device" disconnect
128 proto_notify_error
"$interface" NO_SUBSCRIBER
133 echo "mbim[$$]" "Register with network"
135 umbim
$DBG -n -t $tid -d $device registration
138 0) echo "mbim[$$]" "Registered in home mode"
141 4) if [ "$allow_roaming" = "1" ]; then
142 echo "mbim[$$]" "Registered in roaming mode"
146 5) if [ "$allow_partner" = "1" ]; then
147 echo "mbim[$$]" "Registered in partner mode"
152 if [ $connected -ne 1 ]; then
153 echo "mbim[$$]" "Subscriber registration failed (code $reg_status)"
155 umbim
$DBG -t $tid -d "$device" disconnect
156 proto_notify_error
"$interface" NO_REGISTRATION
160 echo "mbim[$$]" "Attach to network"
161 umbim
$DBG -n -t $tid -d $device attach ||
{
162 echo "mbim[$$]" "Failed to attach to network"
164 umbim
$DBG -t $tid -d "$device" disconnect
165 proto_notify_error
"$interface" ATTACH_FAILED
170 pdptype
=$
(echo "$pdptype" |
awk '{print tolower($0)}')
171 [ "$ipv6" = 0 ] && pdptype
="ipv4"
173 local req_pdptype
="" # Pass "default" PDP type to umbim if unconfigured
174 [ "$pdptype" = "ipv4" -o "$pdptype" = "ipv6" -o "$pdptype" = "ipv4v6" ] && req_pdptype
="$pdptype:"
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"
181 umbim
$DBG -t $tid -d "$device" disconnect
182 proto_notify_error
"$interface" CONNECT_FAILED
187 echo "$connect_state"
188 local iptype
="$(echo "$connect_state" | grep iptype: | awk '{print $4}')"
190 echo "mbim[$$]" "Connected"
192 local zone
="$(fw3 -q network "$interface" 2>/dev/null)"
194 echo "mbim[$$]" "Setting up $ifname"
195 local mbimconfig
="$(umbim $DBG -n -t $tid -d $device config)"
199 proto_init_update
"$ifname" 1
200 proto_send_update
"$interface"
202 [ -z "$dhcp" ] && dhcp
=1
203 [ -z "$dhcpv6" ] && dhcpv6
=1
205 [ "$iptype" != "ipv6" ] && {
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" ]; then
211 json_add_string proto
"static"
213 json_add_array ipaddr
214 for address
in $ipv4address; do
215 json_add_string
"" "$address"
219 json_add_string gateway $
(_proto_mbim_get_field ipv4gateway
"$mbimconfig")
221 [ "$peerdns" = 0 ] ||
{
223 for server
in $
(_proto_mbim_get_field ipv4dnsserver
"$mbimconfig"); do
224 json_add_string
"" "$server"
228 elif [ "$dhcp" != 0 ]; then
229 echo "mbim[$$]" "Starting DHCP on $ifname"
230 json_add_string proto
"dhcp"
232 proto_add_dynamic_defaults
233 [ -n "$zone" ] && json_add_string zone
"$zone"
234 [ -n "$ip4table" ] && json_add_string ip4table
"$ip4table"
236 ubus call network add_dynamic
"$(json_dump)"
239 [ "$iptype" != "ipv4" ] && {
241 json_add_string name
"${interface}_6"
242 json_add_string ifname
"@$interface"
243 ipv6address
=$
(_proto_mbim_get_field ipv6address
"$mbimconfig")
244 if [ -n "$ipv6address" ]; then
245 json_add_string proto
"static"
247 json_add_array ip6addr
248 for address
in $ipv6address; do
249 json_add_string
"" "$address"
253 json_add_array ip6prefix
254 for address
in $ipv6address; do
255 json_add_string
"" "$address"
259 json_add_string ip6gw $
(_proto_mbim_get_field ipv6gateway
"$mbimconfig")
261 [ "$peerdns" = 0 ] ||
{
263 for server
in $
(_proto_mbim_get_field ipv6dnsserver
"$mbimconfig"); do
264 json_add_string
"" "$server"
268 elif [ "$dhcpv6" != 0 ]; then
269 echo "mbim[$$]" "Starting DHCPv6 on $ifname"
270 json_add_string proto
"dhcpv6"
271 json_add_string extendprefix
1
273 proto_add_dynamic_defaults
274 [ -n "$zone" ] && json_add_string zone
"$zone"
275 [ -n "$ip6table" ] && json_add_string ip6table
"$ip6table"
277 ubus call network add_dynamic
"$(json_dump)"
281 local ipv4mtu
=$
(_proto_mbim_get_field ipv4mtu
"$mbimconfig")
282 ipv4mtu
="${ipv4mtu:-0}"
283 local ipv6mtu
=$
(_proto_mbim_get_field ipv6mtu
"$mbimconfig")
284 ipv6mtu
="${ipv6mtu:-0}"
286 mtu
=$
((ipv6mtu
> ipv4mtu ? ipv6mtu
: ipv4mtu
))
288 [ -n "$mtu" -a "$mtu" != 0 ] && {
289 echo Setting MTU of
$ifname to
$mtu
290 /sbin
/ip link
set dev
$ifname mtu
$mtu
293 uci_set_state network
$interface tid
"$tid"
303 logger
"mbim bringup failed, retry in 15s"
310 proto_mbim_teardown
() {
315 local tid
=$
(uci_get_state network
$interface tid
)
317 [ -n "$ctl_device" ] && device
=$ctl_device
319 echo "mbim[$$]" "Stopping network"
321 umbim
$DBG -t $tid -d "$device" disconnect
322 uci_revert_state network
$interface tid
325 proto_init_update
"*" 0
326 proto_send_update
"$interface"
329 [ -n "$INCLUDE_ONLY" ] || add_protocol mbim