umbim: detect actual connection IP type
[openwrt/staging/jow.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 proto_config_add_boolean dhcp
23 proto_config_add_string pdptype
24 proto_config_add_defaults
25 }
26
27 _proto_mbim_setup() {
28 local interface="$1"
29 local tid=2
30 local ret
31
32 local device apn pincode delay allow_roaming allow_partner dhcp pdptype $PROTO_DEFAULT_OPTIONS
33 json_get_vars device apn pincode delay auth username password allow_roaming allow_partner dhcp pdptype $PROTO_DEFAULT_OPTIONS
34
35 [ -n "$ctl_device" ] && device=$ctl_device
36
37 [ -n "$device" ] || {
38 echo "mbim[$$]" "No control device specified"
39 proto_notify_error "$interface" NO_DEVICE
40 proto_set_available "$interface" 0
41 return 1
42 }
43 [ -c "$device" ] || {
44 echo "mbim[$$]" "The specified control device does not exist"
45 proto_notify_error "$interface" NO_DEVICE
46 proto_set_available "$interface" 0
47 return 1
48 }
49
50 devname="$(basename "$device")"
51 devpath="$(readlink -f /sys/class/usbmisc/$devname/device/)"
52 ifname="$( ls "$devpath"/net )"
53
54 [ -n "$ifname" ] || {
55 echo "mbim[$$]" "Failed to find matching interface"
56 proto_notify_error "$interface" NO_IFNAME
57 proto_set_available "$interface" 0
58 return 1
59 }
60
61 [ -n "$apn" ] || {
62 echo "mbim[$$]" "No APN specified"
63 proto_notify_error "$interface" NO_APN
64 return 1
65 }
66
67 [ -n "$delay" ] && sleep "$delay"
68
69 echo "mbim[$$]" "Reading capabilities"
70 umbim $DBG -n -d $device caps || {
71 echo "mbim[$$]" "Failed to read modem caps"
72 tid=$((tid + 1))
73 umbim $DBG -t $tid -d "$device" disconnect
74 proto_notify_error "$interface" PIN_FAILED
75 return 1
76 }
77 tid=$((tid + 1))
78
79 [ "$pincode" ] && {
80 echo "mbim[$$]" "Sending pin"
81 umbim $DBG -n -t $tid -d $device unlock "$pincode" || {
82 echo "mbim[$$]" "Unable to verify PIN"
83 tid=$((tid + 1))
84 umbim $DBG -t $tid -d "$device" disconnect
85 proto_notify_error "$interface" PIN_FAILED
86 proto_block_restart "$interface"
87 return 1
88 }
89 }
90 tid=$((tid + 1))
91
92 echo "mbim[$$]" "Checking pin"
93 umbim $DBG -n -t $tid -d $device pinstate
94 [ $? -eq 2 ] && {
95 echo "mbim[$$]" "PIN required"
96 tid=$((tid + 1))
97 umbim $DBG -t $tid -d "$device" disconnect
98 proto_notify_error "$interface" PIN_FAILED
99 proto_block_restart "$interface"
100 return 1
101 }
102 tid=$((tid + 1))
103
104 echo "mbim[$$]" "Checking subscriber"
105 umbim $DBG -n -t $tid -d $device subscriber || {
106 echo "mbim[$$]" "Subscriber init failed"
107 tid=$((tid + 1))
108 umbim $DBG -t $tid -d "$device" disconnect
109 proto_notify_error "$interface" NO_SUBSCRIBER
110 return 1
111 }
112 tid=$((tid + 1))
113
114 echo "mbim[$$]" "Register with network"
115 connected=0
116 umbim $DBG -n -t $tid -d $device registration
117 reg_status=$?
118 case $reg_status in
119 0) echo "mbim[$$]" "Registered in home mode"
120 tid=$((tid + 1))
121 connected=1;;
122 4) if [ "$allow_roaming" = "1" ]; then
123 echo "mbim[$$]" "Registered in roaming mode"
124 tid=$((tid + 1))
125 connected=1
126 fi;;
127 5) if [ "$allow_partner" = "1" ]; then
128 echo "mbim[$$]" "Registered in partner mode"
129 tid=$((tid + 1))
130 connected=1
131 fi;;
132 esac
133 if [ $connected -ne 1 ]; then
134 echo "mbim[$$]" "Subscriber registration failed (code $reg_status)"
135 tid=$((tid + 1))
136 umbim $DBG -t $tid -d "$device" disconnect
137 proto_notify_error "$interface" NO_REGISTRATION
138 return 1
139 fi
140
141 echo "mbim[$$]" "Attach to network"
142 umbim $DBG -n -t $tid -d $device attach || {
143 echo "mbim[$$]" "Failed to attach to network"
144 tid=$((tid + 1))
145 umbim $DBG -t $tid -d "$device" disconnect
146 proto_notify_error "$interface" ATTACH_FAILED
147 return 1
148 }
149 tid=$((tid + 1))
150
151 pdptype=$(echo "$pdptype" | awk '{print tolower($0)}')
152
153 local req_pdptype="" # Pass "default" PDP type to umbim if unconfigured
154 [ "$pdptype" = "ipv4" -o "$pdptype" = "ipv6" -o "$pdptype" = "ipv4v6" ] && req_pdptype="$pdptype:"
155
156 local connect_state
157 echo "mbim[$$]" "Connect to network"
158 connect_state=$(umbim $DBG -n -t $tid -d $device connect "$req_pdptype$apn" "$auth" "$username" "$password") || {
159 echo "mbim[$$]" "Failed to connect bearer"
160 tid=$((tid + 1))
161 umbim $DBG -t $tid -d "$device" disconnect
162 proto_notify_error "$interface" CONNECT_FAILED
163 return 1
164 }
165 tid=$((tid + 1))
166
167 echo "$connect_state"
168 local iptype="$(echo "$connect_state" | grep iptype: | awk '{print $4}')"
169
170 echo "mbim[$$]" "Connected"
171
172 if [ "$dhcp" = 0 ]; then
173 echo "mbim[$$]" "Setting up $ifname"
174 eval $(umbim $DBG -n -t $tid -d $device config | sed 's/: /=/g')
175 tid=$((tid + 1))
176
177 proto_init_update "$ifname" 1
178 proto_send_update "$interface"
179
180 [ "$iptype" != "ipv6" ] && {
181 json_init
182 json_add_string name "${interface}_4"
183 json_add_string ifname "@$interface"
184 json_add_string proto "static"
185 json_add_array ipaddr
186 json_add_string "" "$ipv4address"
187 json_close_array
188 json_add_string gateway "$ipv4gateway"
189 json_add_array dns
190 json_add_string "" "$ipv4dnsserver"
191 json_close_array
192 proto_add_dynamic_defaults
193 json_close_object
194 ubus call network add_dynamic "$(json_dump)"
195 }
196
197 [ "$iptype" != "ipv4" ] && {
198 json_init
199 json_add_string name "${interface}_6"
200 json_add_string ifname "@$interface"
201 json_add_string proto "static"
202 json_add_array ip6addr
203 json_add_string "" "$ipv6address"
204 json_close_array
205 json_add_string ip6gw "$ipv6gateway"
206 json_add_array dns
207 json_add_string "" "$ipv6dnsserver"
208 json_close_array
209 proto_add_dynamic_defaults
210 json_close_object
211 ubus call network add_dynamic "$(json_dump)"
212 }
213 else
214 echo "mbim[$$]" "Starting DHCP on $ifname"
215 proto_init_update "$ifname" 1
216 proto_send_update "$interface"
217
218 [ "$pdptype" = "ipv4" -o "$pdptype" = "ipv4v6" ] && {
219 json_init
220 json_add_string name "${interface}_4"
221 json_add_string ifname "@$interface"
222 json_add_string proto "dhcp"
223 proto_add_dynamic_defaults
224 json_close_object
225 ubus call network add_dynamic "$(json_dump)"
226 }
227
228 [ "$pdptype" = "ipv6" -o "$pdptype" = "ipv4v6" ] && {
229 json_init
230 json_add_string name "${interface}_6"
231 json_add_string ifname "@$interface"
232 json_add_string proto "dhcpv6"
233 json_add_string extendprefix 1
234 proto_add_dynamic_defaults
235 json_close_object
236 ubus call network add_dynamic "$(json_dump)"
237 }
238 fi
239
240 uci_set_state network $interface tid "$tid"
241 }
242
243 proto_mbim_setup() {
244 local ret
245
246 _proto_mbim_setup $@
247 ret=$?
248
249 [ "$ret" = 0 ] || {
250 logger "mbim bringup failed, retry in 15s"
251 sleep 15
252 }
253
254 return $ret
255 }
256
257 proto_mbim_teardown() {
258 local interface="$1"
259
260 local device
261 json_get_vars device
262 local tid=$(uci_get_state network $interface tid)
263
264 [ -n "$ctl_device" ] && device=$ctl_device
265
266 echo "mbim[$$]" "Stopping network"
267 [ -n "$tid" ] && {
268 umbim $DBG -t $tid -d "$device" disconnect
269 uci_revert_state network $interface tid
270 }
271
272 proto_init_update "*" 0
273 proto_send_update "$interface"
274 }
275
276 [ -n "$INCLUDE_ONLY" ] || add_protocol mbim