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_string dhcp
24 proto_config_add_string dhcpv6
25 proto_config_add_boolean sourcefilter
26 proto_config_add_string pdptype
27 proto_config_add_int mtu
28 proto_config_add_defaults
31 _proto_mbim_get_field
() {
35 echo "$mbimconfig" |
while read -r line
; do
37 [ "$variable" = "$field" ] ||
continue;
48 local device apn pincode delay auth username password allow_roaming allow_partner
49 local dhcp dhcpv6 pdptype ip4table ip6table mtu
$PROTO_DEFAULT_OPTIONS
50 json_get_vars device apn pincode delay auth username password allow_roaming allow_partner
51 json_get_vars dhcp dhcpv6 sourcefilter pdptype ip4table ip6table mtu
$PROTO_DEFAULT_OPTIONS
53 [ ! -e /proc
/sys
/net
/ipv6
] && ipv6
=0 || json_get_var ipv6 ipv6
55 [ -n "$ctl_device" ] && device
=$ctl_device
58 echo "mbim[$$]" "No control device specified"
59 proto_notify_error
"$interface" NO_DEVICE
60 proto_set_available
"$interface" 0
64 echo "mbim[$$]" "The specified control device does not exist"
65 proto_notify_error
"$interface" NO_DEVICE
66 proto_set_available
"$interface" 0
70 devname
="$(basename "$device")"
71 devpath
="$(readlink -f /sys/class/usbmisc/$devname/device/)"
72 ifname
="$( ls "$devpath"/net )"
75 echo "mbim[$$]" "Failed to find matching interface"
76 proto_notify_error
"$interface" NO_IFNAME
77 proto_set_available
"$interface" 0
82 echo "mbim[$$]" "No APN specified"
83 proto_notify_error
"$interface" NO_APN
87 [ -n "$delay" ] && sleep "$delay"
89 echo "mbim[$$]" "Reading capabilities"
90 umbim
$DBG -n -d $device caps ||
{
91 echo "mbim[$$]" "Failed to read modem caps"
93 umbim
$DBG -t $tid -d "$device" disconnect
94 proto_notify_error
"$interface" PIN_FAILED
100 echo "mbim[$$]" "Sending pin"
101 umbim
$DBG -n -t $tid -d $device unlock
"$pincode" ||
{
102 echo "mbim[$$]" "Unable to verify PIN"
104 umbim
$DBG -t $tid -d "$device" disconnect
105 proto_notify_error
"$interface" PIN_FAILED
106 proto_block_restart
"$interface"
112 echo "mbim[$$]" "Checking pin"
113 umbim
$DBG -n -t $tid -d $device pinstate
115 echo "mbim[$$]" "PIN required"
117 umbim
$DBG -t $tid -d "$device" disconnect
118 proto_notify_error
"$interface" PIN_FAILED
119 proto_block_restart
"$interface"
124 echo "mbim[$$]" "Checking subscriber"
125 umbim
$DBG -n -t $tid -d $device subscriber ||
{
126 echo "mbim[$$]" "Subscriber init failed"
128 umbim
$DBG -t $tid -d "$device" disconnect
129 proto_notify_error
"$interface" NO_SUBSCRIBER
134 echo "mbim[$$]" "Register with network"
136 umbim
$DBG -n -t $tid -d $device registration
139 0) echo "mbim[$$]" "Registered in home mode"
142 4) if [ "$allow_roaming" = "1" ]; then
143 echo "mbim[$$]" "Registered in roaming mode"
147 5) if [ "$allow_partner" = "1" ]; then
148 echo "mbim[$$]" "Registered in partner mode"
153 if [ $connected -ne 1 ]; then
154 echo "mbim[$$]" "Subscriber registration failed (code $reg_status)"
156 umbim
$DBG -t $tid -d "$device" disconnect
157 proto_notify_error
"$interface" NO_REGISTRATION
161 echo "mbim[$$]" "Attach to network"
162 umbim
$DBG -n -t $tid -d $device attach ||
{
163 echo "mbim[$$]" "Failed to attach to network"
165 umbim
$DBG -t $tid -d "$device" disconnect
166 proto_notify_error
"$interface" ATTACH_FAILED
171 pdptype
=$
(echo "$pdptype" |
awk '{print tolower($0)}')
172 [ "$ipv6" = 0 ] && pdptype
="ipv4"
174 local req_pdptype
="" # Pass "default" PDP type to umbim if unconfigured
175 [ "$pdptype" = "ipv4" -o "$pdptype" = "ipv6" -o "$pdptype" = "ipv4v6" ] && req_pdptype
="$pdptype:"
178 echo "mbim[$$]" "Connect to network"
179 connect_state
=$
(umbim
$DBG -n -t $tid -d $device connect
"$req_pdptype$apn" "$auth" "$username" "$password") ||
{
180 echo "mbim[$$]" "Failed to connect bearer"
182 umbim
$DBG -t $tid -d "$device" disconnect
183 proto_notify_error
"$interface" CONNECT_FAILED
188 echo "$connect_state"
189 local iptype
="$(echo "$connect_state" | grep iptype: | awk '{print $4}')"
191 echo "mbim[$$]" "Connected"
193 local zone
="$(fw3 -q network "$interface" 2>/dev/null)"
195 echo "mbim[$$]" "Setting up $ifname"
196 local mbimconfig
="$(umbim $DBG -n -t $tid -d $device config)"
200 proto_init_update
"$ifname" 1
201 proto_send_update
"$interface"
203 [ -z "$dhcp" ] && dhcp
="auto"
204 [ -z "$dhcpv6" ] && dhcpv6
="auto"
206 [ "$iptype" != "ipv6" ] && {
208 json_add_string name
"${interface}_4"
209 json_add_string ifname
"@$interface"
210 ipv4address
=$
(_proto_mbim_get_field ipv4address
"$mbimconfig")
211 if [ -n "$ipv4address" -a "$dhcp" != 1 ]; then
212 json_add_string proto
"static"
214 json_add_array ipaddr
215 for address
in $ipv4address; do
216 json_add_string
"" "$address"
220 json_add_string gateway $
(_proto_mbim_get_field ipv4gateway
"$mbimconfig")
221 elif [ "$dhcp" != 0 ]; then
222 echo "mbim[$$]" "Starting DHCP on $ifname"
223 json_add_string proto
"dhcp"
226 [ "$peerdns" = 0 -a "$dhcp" != 1 ] ||
{
228 for server
in $
(_proto_mbim_get_field ipv4dnsserver
"$mbimconfig"); do
229 json_add_string
"" "$server"
234 proto_add_dynamic_defaults
235 [ -n "$zone" ] && json_add_string zone
"$zone"
236 [ -n "$ip4table" ] && json_add_string ip4table
"$ip4table"
238 ubus call network add_dynamic
"$(json_dump)"
241 [ "$iptype" != "ipv4" ] && {
243 json_add_string name
"${interface}_6"
244 json_add_string ifname
"@$interface"
245 ipv6address
=$
(_proto_mbim_get_field ipv6address
"$mbimconfig")
246 if [ -n "$ipv6address" -a "$dhcpv6" != 1 ]; then
247 json_add_string proto
"static"
249 json_add_array ip6addr
250 for address
in $ipv6address; do
251 json_add_string
"" "$address"
255 json_add_array ip6prefix
256 for address
in $ipv6address; do
257 json_add_string
"" "$address"
261 json_add_string ip6gw $
(_proto_mbim_get_field ipv6gateway
"$mbimconfig")
263 elif [ "$dhcpv6" != 0 ]; then
264 echo "mbim[$$]" "Starting DHCPv6 on $ifname"
265 json_add_string proto
"dhcpv6"
266 json_add_string extendprefix
1
267 [ "$sourcefilter" = "0" ] && json_add_boolean sourcefilter
"0"
270 [ "$peerdns" = 0 -a "$dhcpv6" != 1 ] ||
{
272 for server
in $
(_proto_mbim_get_field ipv6dnsserver
"$mbimconfig"); do
273 json_add_string
"" "$server"
278 proto_add_dynamic_defaults
279 [ -n "$zone" ] && json_add_string zone
"$zone"
280 [ -n "$ip6table" ] && json_add_string ip6table
"$ip6table"
282 ubus call network add_dynamic
"$(json_dump)"
286 local ipv4mtu
=$
(_proto_mbim_get_field ipv4mtu
"$mbimconfig")
287 ipv4mtu
="${ipv4mtu:-0}"
288 local ipv6mtu
=$
(_proto_mbim_get_field ipv6mtu
"$mbimconfig")
289 ipv6mtu
="${ipv6mtu:-0}"
291 mtu
=$
((ipv6mtu
> ipv4mtu ? ipv6mtu
: ipv4mtu
))
293 [ -n "$mtu" -a "$mtu" != 0 ] && {
294 echo Setting MTU of
$ifname to
$mtu
295 /sbin
/ip link
set dev
$ifname mtu
$mtu
298 uci_set_state network
$interface tid
"$tid"
308 logger
"mbim bringup failed, retry in 15s"
315 proto_mbim_teardown
() {
320 local tid
=$
(uci_get_state network
$interface tid
)
322 [ -n "$ctl_device" ] && device
=$ctl_device
324 echo "mbim[$$]" "Stopping network"
326 umbim
$DBG -t $tid -d "$device" disconnect
327 uci_revert_state network
$interface tid
330 proto_init_update
"*" 0
331 proto_send_update
"$interface"
334 [ -n "$INCLUDE_ONLY" ] || add_protocol mbim