ath79: do not build TP-Link tiny images by default
[oweals/openwrt.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         gcom -d "$device" -s /etc/gcom/getcardinfo.gcom | grep -q "Sierra Wireless" || {
48                 proto_notify_error "$interface" BAD_DEVICE
49                 proto_block_restart "$interface"
50                 return 1
51         }
52
53         if [ -n "$pincode" ]; then
54                 PINCODE="$pincode" gcom -d "$device" -s /etc/gcom/setpin.gcom || {
55                         proto_notify_error "$interface" PIN_FAILED
56                         proto_block_restart "$interface"
57                         return 1
58                 }
59         fi
60         # wait for carrier to avoid firmware stability bugs
61         gcom -d "$device" -s /etc/gcom/getcarrier.gcom || return 1
62
63         local auth_type=0
64         case $auth in
65         pap) auth_type=1;;
66         chap) auth_type=2;;
67         esac
68
69         USE_APN="$apn" USE_USER="$username" USE_PASS="$password" USE_AUTH="$auth_type" \
70                         gcom -d "$device" -s /etc/gcom/directip.gcom || {
71                 proto_notify_error "$interface" CONNECT_FAILED
72                 proto_block_restart "$interface"
73                 return 1
74         }
75
76         logger -p daemon.info -t "directip[$$]" "Connected, starting DHCP"
77         proto_init_update "$ifname" 1
78         proto_send_update "$interface"
79
80         json_init
81         json_add_string name "${interface}_4"
82         json_add_string ifname "@$interface"
83         json_add_string proto "dhcp"
84         proto_add_dynamic_defaults
85         ubus call network add_dynamic "$(json_dump)"
86
87         json_init
88         json_add_string name "${interface}_6"
89         json_add_string ifname "@$interface"
90         json_add_string proto "dhcpv6"
91         json_add_string extendprefix 1
92         proto_add_dynamic_defaults
93         ubus call network add_dynamic "$(json_dump)"
94
95         return 0
96 }
97
98 proto_directip_teardown() {
99         local interface="$1"
100
101         local device
102         json_get_vars device
103
104         [ -n "$ctl_device" ] && device=$ctl_device
105
106         gcom -d "$device" -s /etc/gcom/directip-stop.gcom || proto_notify_error "$interface" CONNECT_FAILED
107
108         proto_init_update "*" 0
109         proto_send_update "$interface"
110 }
111
112 [ -n "$INCLUDE_ONLY" ] || {
113         add_protocol directip
114 }