b860f1ef2719ebf92dd7603ee3c4a30f63e44a8a
[openwrt/openwrt.git] / package / network / utils / comgt / files / directip.sh
1 #!/bin/sh
2
3 . /lib/functions.sh
4 . ../netifd-proto.sh
5 init_proto "$@"
6
7 proto_directip_init_config() {
8 available=1
9 no_device=1
10 proto_config_add_string "device:device"
11 proto_config_add_string "apn"
12 proto_config_add_string "pincode"
13 proto_config_add_string "auth"
14 proto_config_add_string "username"
15 proto_config_add_string "password"
16 }
17
18 proto_directip_setup() {
19 local interface="$1"
20 local chat devpath devname
21
22 local device apn pincode ifname auth username password
23 json_get_vars device apn pincode auth username password
24
25 [ -e "$device" ] || {
26 proto_notify_error "$interface" NO_DEVICE
27 proto_set_available "$interface" 0
28 return 1
29 }
30
31 devname="$(basename "$device")"
32 devpath="$(readlink -f /sys/class/tty/$devname/device)"
33 ifname="$( ls "$devpath"/../../*/net )"
34
35 [ -n "$ifname" ] || {
36 proto_notify_error "$interface" NO_IFNAME
37 proto_set_available "$interface" 0
38 return 1
39 }
40
41 cardinfo=$(gcom -d "$device" -s /etc/gcom/getcardinfo.gcom)
42 [ -n $(echo "$cardinfo" | grep -q "Sierra Wireless") ] || {
43 proto_notify_error "$interface" BAD_DEVICE
44 proto_block_restart "$interface"
45 return 1
46 }
47
48 if [ -n "$pincode" ]; then
49 PINCODE="$pincode" gcom -d "$device" -s /etc/gcom/setpin.gcom || {
50 proto_notify_error "$interface" PIN_FAILED
51 proto_block_restart "$interface"
52 return 1
53 }
54 fi
55 # wait for carrier to avoid firmware stability bugs
56 gcom -d "$device" -s /etc/gcom/getcarrier.gcom || return 1
57
58 local auth_type=0
59 case $auth in
60 pap) auth_type=1;;
61 chap) auth_type=2;;
62 esac
63
64 USE_APN="$apn" USE_USER="$username" USE_PASS="$password" USE_AUTH="$auth_type" \
65 gcom -d "$device" -s /etc/gcom/directip.gcom || {
66 proto_notify_error "$interface" CONNECT_FAILED
67 proto_block_restart "$interface"
68 return 1
69 }
70
71 logger -p daemon.info -t "directip[$$]" "Connected, starting DHCP"
72 proto_init_update "$ifname" 1
73 proto_send_update "$interface"
74
75 json_init
76 json_add_string name "${interface}_dhcp"
77 json_add_string ifname "@$interface"
78 json_add_string proto "dhcp"
79 ubus call network add_dynamic "$(json_dump)"
80
81 json_init
82 json_add_string name "${interface}_dhcpv6"
83 json_add_string ifname "@$interface"
84 json_add_string proto "dhcpv6"
85 ubus call network add_dynamic "$(json_dump)"
86
87 return 0
88 }
89
90 proto_directip_teardown() {
91 local interface="$1"
92
93 local device
94 json_get_vars device
95
96 gcom -d "$device" -s /etc/gcom/directip-stop.gcom || proto_notify_error "$interface" CONNECT_FAILED
97
98 proto_init_update "*" 0
99 proto_send_update "$interface"
100 }
101
102 add_protocol directip