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