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_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 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"
119 umbim
$DBG -t $tid -d "$device" disconnect
120 proto_notify_error
"$interface" PIN_FAILED
121 proto_block_restart
"$interface"
127 echo "mbim[$$]" "Checking subscriber"
128 umbim
$DBG -n -t $tid -d $device subscriber ||
{
129 echo "mbim[$$]" "Subscriber init failed"
131 umbim
$DBG -t $tid -d "$device" disconnect
132 proto_notify_error
"$interface" NO_SUBSCRIBER
137 echo "mbim[$$]" "Register with network"
139 umbim
$DBG -n -t $tid -d $device registration
142 0) echo "mbim[$$]" "Registered in home mode"
145 4) if [ "$allow_roaming" = "1" ]; then
146 echo "mbim[$$]" "Registered in roaming mode"
150 5) if [ "$allow_partner" = "1" ]; then
151 echo "mbim[$$]" "Registered in partner mode"
156 if [ $connected -ne 1 ]; then
157 echo "mbim[$$]" "Subscriber registration failed (code $reg_status)"
159 umbim
$DBG -t $tid -d "$device" disconnect
160 proto_notify_error
"$interface" NO_REGISTRATION
164 echo "mbim[$$]" "Attach to network"
165 umbim
$DBG -n -t $tid -d $device attach ||
{
166 echo "mbim[$$]" "Failed to attach to network"
168 umbim
$DBG -t $tid -d "$device" disconnect
169 proto_notify_error
"$interface" ATTACH_FAILED
174 pdptype
=$
(echo "$pdptype" |
awk '{print tolower($0)}')
175 [ "$ipv6" = 0 ] && pdptype
="ipv4"
177 local req_pdptype
="" # Pass "default" PDP type to umbim if unconfigured
178 [ "$pdptype" = "ipv4" -o "$pdptype" = "ipv6" -o "$pdptype" = "ipv4v6" ] && req_pdptype
="$pdptype:"
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"
185 umbim
$DBG -t $tid -d "$device" disconnect
186 proto_notify_error
"$interface" CONNECT_FAILED
191 echo "$connect_state"
192 local iptype
="$(echo "$connect_state" | grep iptype: | awk '{print $4}')"
194 echo "mbim[$$]" "Connected"
196 local zone
="$(fw3 -q network "$interface" 2>/dev/null)"
198 echo "mbim[$$]" "Setting up $ifname"
199 local mbimconfig
="$(umbim $DBG -n -t $tid -d $device config)"
203 proto_init_update
"$ifname" 1
205 json_add_int tid
$tid
207 proto_send_update
"$interface"
209 [ -z "$dhcp" ] && dhcp
="auto"
210 [ -z "$dhcpv6" ] && dhcpv6
="auto"
212 [ "$iptype" != "ipv6" ] && {
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"
220 json_add_array ipaddr
221 for address
in $ipv4address; do
222 json_add_string
"" "$address"
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"
232 [ "$peerdns" = 0 -a "$dhcp" != 1 ] ||
{
234 for server
in $
(_proto_mbim_get_field ipv4dnsserver
"$mbimconfig"); do
235 json_add_string
"" "$server"
240 proto_add_dynamic_defaults
241 [ -n "$zone" ] && json_add_string zone
"$zone"
242 [ -n "$ip4table" ] && json_add_string ip4table
"$ip4table"
244 ubus call network add_dynamic
"$(json_dump)"
247 [ "$iptype" != "ipv4" ] && {
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"
255 json_add_array ip6addr
256 for address
in $ipv6address; do
257 json_add_string
"" "$address"
261 json_add_array ip6prefix
262 for address
in $ipv6address; do
263 json_add_string
"" "$address"
267 json_add_string ip6gw $
(_proto_mbim_get_field ipv6gateway
"$mbimconfig")
269 elif [ "$dhcpv6" != 0 ]; then
270 echo "mbim[$$]" "Starting DHCPv6 on $ifname"
271 json_add_string proto
"dhcpv6"
272 json_add_string extendprefix
1
275 [ "$peerdns" = 0 -a "$dhcpv6" != 1 ] ||
{
277 for server
in $
(_proto_mbim_get_field ipv6dnsserver
"$mbimconfig"); do
278 json_add_string
"" "$server"
283 proto_add_dynamic_defaults
284 [ -n "$zone" ] && json_add_string zone
"$zone"
285 [ -n "$ip6table" ] && json_add_string ip6table
"$ip6table"
287 ubus call network add_dynamic
"$(json_dump)"
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}"
296 mtu
=$
((ipv6mtu
> ipv4mtu ? ipv6mtu
: ipv4mtu
))
298 [ -n "$mtu" -a "$mtu" != 0 ] && {
299 echo Setting MTU of
$ifname to
$mtu
300 /sbin
/ip link
set dev
$ifname mtu
$mtu
310 rm -f "/var/run/mbim.$$."*
313 logger
"mbim bringup failed, retry in 15s"
320 proto_mbim_teardown
() {
324 json_get_vars device tid
326 [ -n "$ctl_device" ] && device
=$ctl_device
328 echo "mbim[$$]" "Stopping network"
329 [ -n "$tid" ] && umbim
$DBG -t $tid -d "$device" disconnect
331 proto_init_update
"*" 0
332 proto_send_update
"$interface"
335 [ -n "$INCLUDE_ONLY" ] || add_protocol mbim