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