umbim: handle MTU configuration
[openwrt/openwrt.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 [ -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
28 }
29
30 _proto_mbim_get_field() {
31 local field="$1"
32 shift
33 local mbimconfig="$@"
34 echo "$mbimconfig" | while read -r line; do
35 variable=${line%%:*}
36 [ "$variable" = "$field" ] || continue;
37 value=${line##* }
38 echo -n "$value "
39 done
40 }
41
42 _proto_mbim_setup() {
43 local interface="$1"
44 local tid=2
45 local ret
46
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
51
52 [ ! -e /proc/sys/net/ipv6 ] && ipv6=0 || json_get_var ipv6 ipv6
53
54 [ -n "$ctl_device" ] && device=$ctl_device
55
56 [ -n "$device" ] || {
57 echo "mbim[$$]" "No control device specified"
58 proto_notify_error "$interface" NO_DEVICE
59 proto_set_available "$interface" 0
60 return 1
61 }
62 [ -c "$device" ] || {
63 echo "mbim[$$]" "The specified control device does not exist"
64 proto_notify_error "$interface" NO_DEVICE
65 proto_set_available "$interface" 0
66 return 1
67 }
68
69 devname="$(basename "$device")"
70 devpath="$(readlink -f /sys/class/usbmisc/$devname/device/)"
71 ifname="$( ls "$devpath"/net )"
72
73 [ -n "$ifname" ] || {
74 echo "mbim[$$]" "Failed to find matching interface"
75 proto_notify_error "$interface" NO_IFNAME
76 proto_set_available "$interface" 0
77 return 1
78 }
79
80 [ -n "$apn" ] || {
81 echo "mbim[$$]" "No APN specified"
82 proto_notify_error "$interface" NO_APN
83 return 1
84 }
85
86 [ -n "$delay" ] && sleep "$delay"
87
88 echo "mbim[$$]" "Reading capabilities"
89 umbim $DBG -n -d $device caps || {
90 echo "mbim[$$]" "Failed to read modem caps"
91 tid=$((tid + 1))
92 umbim $DBG -t $tid -d "$device" disconnect
93 proto_notify_error "$interface" PIN_FAILED
94 return 1
95 }
96 tid=$((tid + 1))
97
98 [ "$pincode" ] && {
99 echo "mbim[$$]" "Sending pin"
100 umbim $DBG -n -t $tid -d $device unlock "$pincode" || {
101 echo "mbim[$$]" "Unable to verify PIN"
102 tid=$((tid + 1))
103 umbim $DBG -t $tid -d "$device" disconnect
104 proto_notify_error "$interface" PIN_FAILED
105 proto_block_restart "$interface"
106 return 1
107 }
108 }
109 tid=$((tid + 1))
110
111 echo "mbim[$$]" "Checking pin"
112 umbim $DBG -n -t $tid -d $device pinstate
113 [ $? -eq 2 ] && {
114 echo "mbim[$$]" "PIN required"
115 tid=$((tid + 1))
116 umbim $DBG -t $tid -d "$device" disconnect
117 proto_notify_error "$interface" PIN_FAILED
118 proto_block_restart "$interface"
119 return 1
120 }
121 tid=$((tid + 1))
122
123 echo "mbim[$$]" "Checking subscriber"
124 umbim $DBG -n -t $tid -d $device subscriber || {
125 echo "mbim[$$]" "Subscriber init failed"
126 tid=$((tid + 1))
127 umbim $DBG -t $tid -d "$device" disconnect
128 proto_notify_error "$interface" NO_SUBSCRIBER
129 return 1
130 }
131 tid=$((tid + 1))
132
133 echo "mbim[$$]" "Register with network"
134 connected=0
135 umbim $DBG -n -t $tid -d $device registration
136 reg_status=$?
137 case $reg_status in
138 0) echo "mbim[$$]" "Registered in home mode"
139 tid=$((tid + 1))
140 connected=1;;
141 4) if [ "$allow_roaming" = "1" ]; then
142 echo "mbim[$$]" "Registered in roaming mode"
143 tid=$((tid + 1))
144 connected=1
145 fi;;
146 5) if [ "$allow_partner" = "1" ]; then
147 echo "mbim[$$]" "Registered in partner mode"
148 tid=$((tid + 1))
149 connected=1
150 fi;;
151 esac
152 if [ $connected -ne 1 ]; then
153 echo "mbim[$$]" "Subscriber registration failed (code $reg_status)"
154 tid=$((tid + 1))
155 umbim $DBG -t $tid -d "$device" disconnect
156 proto_notify_error "$interface" NO_REGISTRATION
157 return 1
158 fi
159
160 echo "mbim[$$]" "Attach to network"
161 umbim $DBG -n -t $tid -d $device attach || {
162 echo "mbim[$$]" "Failed to attach to network"
163 tid=$((tid + 1))
164 umbim $DBG -t $tid -d "$device" disconnect
165 proto_notify_error "$interface" ATTACH_FAILED
166 return 1
167 }
168 tid=$((tid + 1))
169
170 pdptype=$(echo "$pdptype" | awk '{print tolower($0)}')
171 [ "$ipv6" = 0 ] && pdptype="ipv4"
172
173 local req_pdptype="" # Pass "default" PDP type to umbim if unconfigured
174 [ "$pdptype" = "ipv4" -o "$pdptype" = "ipv6" -o "$pdptype" = "ipv4v6" ] && req_pdptype="$pdptype:"
175
176 local connect_state
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"
180 tid=$((tid + 1))
181 umbim $DBG -t $tid -d "$device" disconnect
182 proto_notify_error "$interface" CONNECT_FAILED
183 return 1
184 }
185 tid=$((tid + 1))
186
187 echo "$connect_state"
188 local iptype="$(echo "$connect_state" | grep iptype: | awk '{print $4}')"
189
190 echo "mbim[$$]" "Connected"
191
192 local zone="$(fw3 -q network "$interface" 2>/dev/null)"
193
194 echo "mbim[$$]" "Setting up $ifname"
195 local mbimconfig="$(umbim $DBG -n -t $tid -d $device config)"
196 echo "$mbimconfig"
197 tid=$((tid + 1))
198
199 proto_init_update "$ifname" 1
200 proto_send_update "$interface"
201
202 [ -z "$dhcp" ] && dhcp=1
203 [ -z "$dhcpv6" ] && dhcpv6=1
204
205 [ "$iptype" != "ipv6" ] && {
206 ipv4address=$(_proto_mbim_get_field ipv4address "$mbimconfig")
207 if [ -n "$ipv4address" ]; then
208 json_init
209 json_add_string name "${interface}_4"
210 json_add_string ifname "@$interface"
211 json_add_string proto "static"
212
213 json_add_array ipaddr
214 for address in $ipv4address; do
215 json_add_string "" "$address"
216 done
217 json_close_array
218
219 json_add_string gateway $(_proto_mbim_get_field ipv4gateway "$mbimconfig")
220
221 [ "$peerdns" = 0 ] || {
222 json_add_array dns
223 for server in $(_proto_mbim_get_field ipv4dnsserver "$mbimconfig"); do
224 json_add_string "" "$server"
225 done
226 json_close_array
227 }
228
229 proto_add_dynamic_defaults
230 [ -n "$zone" ] && json_add_string zone "$zone"
231 [ -n "$ip4table" ] && json_add_string ip4table "$ip4table"
232 json_close_object
233 ubus call network add_dynamic "$(json_dump)"
234 elif [ "$dhcp" != 0 ]; then
235 echo "mbim[$$]" "Starting DHCP on $ifname"
236 json_init
237 json_add_string name "${interface}_4"
238 json_add_string ifname "@$interface"
239 json_add_string proto "dhcp"
240 proto_add_dynamic_defaults
241 [ -n "$zone" ] && json_add_string zone "$zone"
242 [ -n "$ip4table" ] && json_add_string ip4table "$ip4table"
243 json_close_object
244 ubus call network add_dynamic "$(json_dump)"
245 fi
246 }
247
248 [ "$iptype" != "ipv4" ] && {
249 ipv6address=$(_proto_mbim_get_field ipv6address "$mbimconfig")
250 if [ -n "$ipv6address" ]; then
251 json_init
252 json_add_string name "${interface}_6"
253 json_add_string ifname "@$interface"
254 json_add_string proto "static"
255
256 json_add_array ip6addr
257 for address in $ipv6address; do
258 json_add_string "" "$address"
259 done
260 json_close_array
261
262 json_add_array ip6prefix
263 for address in $ipv6address; do
264 json_add_string "" "$address"
265 done
266 json_close_array
267
268 json_add_string ip6gw $(_proto_mbim_get_field ipv6gateway "$mbimconfig")
269
270 [ "$peerdns" = 0 ] || {
271 json_add_array dns
272 for server in $(_proto_mbim_get_field ipv6dnsserver "$mbimconfig"); do
273 json_add_string "" "$server"
274 done
275 json_close_array
276 }
277
278 proto_add_dynamic_defaults
279 [ -n "$zone" ] && json_add_string zone "$zone"
280 [ -n "$ip6table" ] && json_add_string ip6table "$ip6table"
281 json_close_object
282 ubus call network add_dynamic "$(json_dump)"
283 elif [ "$dhcpv6" != 0 ]; then
284 echo "mbim[$$]" "Starting DHCPv6 on $ifname"
285 json_init
286 json_add_string name "${interface}_6"
287 json_add_string ifname "@$interface"
288 json_add_string proto "dhcpv6"
289 json_add_string extendprefix 1
290 proto_add_dynamic_defaults
291 [ -n "$zone" ] && json_add_string zone "$zone"
292 [ -n "$ip6table" ] && json_add_string ip6table "$ip6table"
293 json_close_object
294 ubus call network add_dynamic "$(json_dump)"
295 fi
296 }
297
298 [ -z "$mtu" ] && {
299 local ipv4mtu=$(_proto_mbim_get_field ipv4mtu "$mbimconfig")
300 ipv4mtu="${ipv4mtu:-0}"
301 local ipv6mtu=$(_proto_mbim_get_field ipv6mtu "$mbimconfig")
302 ipv6mtu="${ipv6mtu:-0}"
303
304 mtu=$((ipv6mtu > ipv4mtu ? ipv6mtu : ipv4mtu))
305 }
306 [ -n "$mtu" -a "$mtu" != 0 ] && {
307 echo Setting MTU of $ifname to $mtu
308 /sbin/ip link set dev $ifname mtu $mtu
309 }
310
311 uci_set_state network $interface tid "$tid"
312 }
313
314 proto_mbim_setup() {
315 local ret
316
317 _proto_mbim_setup $@
318 ret=$?
319
320 [ "$ret" = 0 ] || {
321 logger "mbim bringup failed, retry in 15s"
322 sleep 15
323 }
324
325 return $ret
326 }
327
328 proto_mbim_teardown() {
329 local interface="$1"
330
331 local device
332 json_get_vars device
333 local tid=$(uci_get_state network $interface tid)
334
335 [ -n "$ctl_device" ] && device=$ctl_device
336
337 echo "mbim[$$]" "Stopping network"
338 [ -n "$tid" ] && {
339 umbim $DBG -t $tid -d "$device" disconnect
340 uci_revert_state network $interface tid
341 }
342
343 proto_init_update "*" 0
344 proto_send_update "$interface"
345 }
346
347 [ -n "$INCLUDE_ONLY" ] || add_protocol mbim