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_defaults
29 _proto_mbim_get_field
() {
33 echo "$mbimconfig" |
while read -r line
; do
35 [ "$variable" = "$field" ] ||
continue;
46 local device apn pincode delay auth username password allow_roaming allow_partner
47 local dhcp dhcpv6 pdptype ip4table ip6table
$PROTO_DEFAULT_OPTIONS
48 json_get_vars device apn pincode delay auth username password allow_roaming allow_partner
49 json_get_vars dhcp dhcpv6 pdptype ip4table ip6table
$PROTO_DEFAULT_OPTIONS
51 [ ! -e /proc
/sys
/net
/ipv6
] && ipv6
=0 || json_get_var ipv6 ipv6
53 [ -n "$ctl_device" ] && device
=$ctl_device
56 echo "mbim[$$]" "No control device specified"
57 proto_notify_error
"$interface" NO_DEVICE
58 proto_set_available
"$interface" 0
62 echo "mbim[$$]" "The specified control device does not exist"
63 proto_notify_error
"$interface" NO_DEVICE
64 proto_set_available
"$interface" 0
68 devname
="$(basename "$device")"
69 devpath
="$(readlink -f /sys/class/usbmisc/$devname/device/)"
70 ifname
="$( ls "$devpath"/net )"
73 echo "mbim[$$]" "Failed to find matching interface"
74 proto_notify_error
"$interface" NO_IFNAME
75 proto_set_available
"$interface" 0
80 echo "mbim[$$]" "No APN specified"
81 proto_notify_error
"$interface" NO_APN
85 [ -n "$delay" ] && sleep "$delay"
87 echo "mbim[$$]" "Reading capabilities"
88 umbim
$DBG -n -d $device caps ||
{
89 echo "mbim[$$]" "Failed to read modem caps"
91 umbim
$DBG -t $tid -d "$device" disconnect
92 proto_notify_error
"$interface" PIN_FAILED
98 echo "mbim[$$]" "Sending pin"
99 umbim
$DBG -n -t $tid -d $device unlock
"$pincode" ||
{
100 echo "mbim[$$]" "Unable to verify PIN"
102 umbim
$DBG -t $tid -d "$device" disconnect
103 proto_notify_error
"$interface" PIN_FAILED
104 proto_block_restart
"$interface"
110 echo "mbim[$$]" "Checking pin"
111 umbim
$DBG -n -t $tid -d $device pinstate
113 echo "mbim[$$]" "PIN required"
115 umbim
$DBG -t $tid -d "$device" disconnect
116 proto_notify_error
"$interface" PIN_FAILED
117 proto_block_restart
"$interface"
122 echo "mbim[$$]" "Checking subscriber"
123 umbim
$DBG -n -t $tid -d $device subscriber ||
{
124 echo "mbim[$$]" "Subscriber init failed"
126 umbim
$DBG -t $tid -d "$device" disconnect
127 proto_notify_error
"$interface" NO_SUBSCRIBER
132 echo "mbim[$$]" "Register with network"
134 umbim
$DBG -n -t $tid -d $device registration
137 0) echo "mbim[$$]" "Registered in home mode"
140 4) if [ "$allow_roaming" = "1" ]; then
141 echo "mbim[$$]" "Registered in roaming mode"
145 5) if [ "$allow_partner" = "1" ]; then
146 echo "mbim[$$]" "Registered in partner mode"
151 if [ $connected -ne 1 ]; then
152 echo "mbim[$$]" "Subscriber registration failed (code $reg_status)"
154 umbim
$DBG -t $tid -d "$device" disconnect
155 proto_notify_error
"$interface" NO_REGISTRATION
159 echo "mbim[$$]" "Attach to network"
160 umbim
$DBG -n -t $tid -d $device attach ||
{
161 echo "mbim[$$]" "Failed to attach to network"
163 umbim
$DBG -t $tid -d "$device" disconnect
164 proto_notify_error
"$interface" ATTACH_FAILED
169 pdptype
=$
(echo "$pdptype" |
awk '{print tolower($0)}')
170 [ "$ipv6" = 0 ] && pdptype
="ipv4"
172 local req_pdptype
="" # Pass "default" PDP type to umbim if unconfigured
173 [ "$pdptype" = "ipv4" -o "$pdptype" = "ipv6" -o "$pdptype" = "ipv4v6" ] && req_pdptype
="$pdptype:"
176 echo "mbim[$$]" "Connect to network"
177 connect_state
=$
(umbim
$DBG -n -t $tid -d $device connect
"$req_pdptype$apn" "$auth" "$username" "$password") ||
{
178 echo "mbim[$$]" "Failed to connect bearer"
180 umbim
$DBG -t $tid -d "$device" disconnect
181 proto_notify_error
"$interface" CONNECT_FAILED
186 echo "$connect_state"
187 local iptype
="$(echo "$connect_state" | grep iptype: | awk '{print $4}')"
189 echo "mbim[$$]" "Connected"
191 local zone
="$(fw3 -q network "$interface" 2>/dev/null)"
193 echo "mbim[$$]" "Setting up $ifname"
194 local mbimconfig
="$(umbim $DBG -n -t $tid -d $device config)"
198 proto_init_update
"$ifname" 1
199 proto_send_update
"$interface"
201 [ -z "$dhcp" ] && dhcp
=1
202 [ -z "$dhcpv6" ] && dhcpv6
=1
204 [ "$iptype" != "ipv6" ] && {
205 ipv4address
=$
(_proto_mbim_get_field ipv4address
"$mbimconfig")
206 if [ -n "$ipv4address" ]; then
208 json_add_string name
"${interface}_4"
209 json_add_string ifname
"@$interface"
210 json_add_string proto
"static"
212 json_add_array ipaddr
213 for address
in $ipv4address; do
214 json_add_string
"" "$address"
218 json_add_string gateway $
(_proto_mbim_get_field ipv4gateway
"$mbimconfig")
220 [ "$peerdns" = 0 ] ||
{
222 for server
in $
(_proto_mbim_get_field ipv4dnsserver
"$mbimconfig"); do
223 json_add_string
"" "$server"
228 proto_add_dynamic_defaults
229 [ -n "$zone" ] && json_add_string zone
"$zone"
230 [ -n "$ip4table" ] && json_add_string ip4table
"$ip4table"
232 ubus call network add_dynamic
"$(json_dump)"
233 elif [ "$dhcp" != 0 ]; then
234 echo "mbim[$$]" "Starting DHCP on $ifname"
236 json_add_string name
"${interface}_4"
237 json_add_string ifname
"@$interface"
238 json_add_string proto
"dhcp"
239 proto_add_dynamic_defaults
240 [ -n "$zone" ] && json_add_string zone
"$zone"
241 [ -n "$ip4table" ] && json_add_string ip4table
"$ip4table"
243 ubus call network add_dynamic
"$(json_dump)"
247 [ "$iptype" != "ipv4" ] && {
248 ipv6address
=$
(_proto_mbim_get_field ipv6address
"$mbimconfig")
249 if [ -n "$ipv6address" ]; then
251 json_add_string name
"${interface}_6"
252 json_add_string ifname
"@$interface"
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 [ "$peerdns" = 0 ] ||
{
271 for server
in $
(_proto_mbim_get_field ipv6dnsserver
"$mbimconfig"); do
272 json_add_string
"" "$server"
277 proto_add_dynamic_defaults
278 [ -n "$zone" ] && json_add_string zone
"$zone"
279 [ -n "$ip6table" ] && json_add_string ip6table
"$ip6table"
281 ubus call network add_dynamic
"$(json_dump)"
282 elif [ "$dhcpv6" != 0 ]; then
283 echo "mbim[$$]" "Starting DHCPv6 on $ifname"
285 json_add_string name
"${interface}_6"
286 json_add_string ifname
"@$interface"
287 json_add_string proto
"dhcpv6"
288 json_add_string extendprefix
1
289 proto_add_dynamic_defaults
290 [ -n "$zone" ] && json_add_string zone
"$zone"
291 [ -n "$ip6table" ] && json_add_string ip6table
"$ip6table"
293 ubus call network add_dynamic
"$(json_dump)"
297 uci_set_state network
$interface tid
"$tid"
307 logger
"mbim bringup failed, retry in 15s"
314 proto_mbim_teardown
() {
319 local tid
=$
(uci_get_state network
$interface tid
)
321 [ -n "$ctl_device" ] && device
=$ctl_device
323 echo "mbim[$$]" "Stopping network"
325 umbim
$DBG -t $tid -d "$device" disconnect
326 uci_revert_state network
$interface tid
329 proto_init_update
"*" 0
330 proto_send_update
"$interface"
333 [ -n "$INCLUDE_ONLY" ] || add_protocol mbim