network: also shorten virtual interface names of ppp and 3g/4g connections
[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 }
19
20 proto_directip_setup() {
21         local interface="$1"
22         local chat devpath devname
23
24         local device apn pincode ifname auth username password
25         json_get_vars device apn pincode auth username password
26
27         [ -n "$ctl_device" ] && device=$ctl_device
28
29         [ -e "$device" ] || {
30                 proto_notify_error "$interface" NO_DEVICE
31                 proto_set_available "$interface" 0
32                 return 1
33         }
34
35         devname="$(basename "$device")"
36         devpath="$(readlink -f /sys/class/tty/$devname/device)"
37         ifname="$( ls "$devpath"/../../*/net )"
38
39         [ -n "$ifname" ] || {
40                 proto_notify_error "$interface" NO_IFNAME
41                 proto_set_available "$interface" 0
42                 return 1
43         }
44
45         cardinfo=$(gcom -d "$device" -s /etc/gcom/getcardinfo.gcom)
46         [ -n $(echo "$cardinfo" | grep -q "Sierra Wireless") ] || {
47                 proto_notify_error "$interface" BAD_DEVICE
48                 proto_block_restart "$interface"
49                 return 1
50         }
51
52         if [ -n "$pincode" ]; then
53                 PINCODE="$pincode" gcom -d "$device" -s /etc/gcom/setpin.gcom || {
54                         proto_notify_error "$interface" PIN_FAILED
55                         proto_block_restart "$interface"
56                         return 1
57                 }
58         fi
59         # wait for carrier to avoid firmware stability bugs
60         gcom -d "$device" -s /etc/gcom/getcarrier.gcom || return 1
61
62         local auth_type=0
63         case $auth in
64         pap) auth_type=1;;
65         chap) auth_type=2;;
66         esac
67
68         USE_APN="$apn" USE_USER="$username" USE_PASS="$password" USE_AUTH="$auth_type" \
69                         gcom -d "$device" -s /etc/gcom/directip.gcom || {
70                 proto_notify_error "$interface" CONNECT_FAILED
71                 proto_block_restart "$interface"
72                 return 1
73         }
74
75         logger -p daemon.info -t "directip[$$]" "Connected, starting DHCP"
76         proto_init_update "$ifname" 1
77         proto_send_update "$interface"
78
79         json_init
80         json_add_string name "${interface}_4"
81         json_add_string ifname "@$interface"
82         json_add_string proto "dhcp"
83         ubus call network add_dynamic "$(json_dump)"
84
85         json_init
86         json_add_string name "${interface}_6"
87         json_add_string ifname "@$interface"
88         json_add_string proto "dhcpv6"
89         ubus call network add_dynamic "$(json_dump)"
90
91         return 0
92 }
93
94 proto_directip_teardown() {
95         local interface="$1"
96
97         local device
98         json_get_vars device
99
100         [ -n "$ctl_device" ] && device=$ctl_device
101
102         gcom -d "$device" -s /etc/gcom/directip-stop.gcom || proto_notify_error "$interface" CONNECT_FAILED
103
104         proto_init_update "*" 0
105         proto_send_update "$interface"
106 }
107
108 [ -n "$INCLUDE_ONLY" ] || {
109         add_protocol directip
110 }