summaryrefslogtreecommitdiffstats
path: root/src/lib/connections/mobile_ppp
blob: 2466ad74aac91710ac9f103f31058b8449c8f68d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# Contributed by Robbie Smith <zoqaeski@gmail.com>
# Based on Thomas Bächler’s <thomas@archlinux.org> pppoe script
# Also see <https://wiki.archlinux.org/index.php/3G_and_GPRS_modems_with_pppd> for more information.

: ${PPPD:=pppd}
: ${InterfaceRoot=dev/}

quote_word() {
    set -- "${@//\\/\\\\}"
    printf '"%s"\n' "${@//\"/\\\"}"
}

mobile_ppp_up() {
    local cfg
    local chat

    mkdir -p "$STATE_DIR/mobile_ppp.${Interface}.${Profile}/"
    chmod 700 "$STATE_DIR/mobile_ppp.${Interface}.${Profile}/"
    cfg="$STATE_DIR/mobile_ppp.${Interface}.${Profile}/options"
    chat="$STATE_DIR/mobile_ppp.${Interface}.${Profile}/modem.chat"
    echo "linkname $(quote_word "${Profile}")" > "${cfg}"
    chmod 600 "${cfg}"

    cat >> "${cfg}" << EOF
${Interface}
921600
lock
crtscts
modem
passive
novj
holdoff 10
maxfail 5
EOF

    # Debug pppd output separately from netctl
    if is_yes "${PPPDebug:-yes}"; then
        echo "debug" >> "${cfg}"
    fi

    # Sets up route
    if is_yes "${DefaultRoute:-yes}"; then
        echo "defaultroute" >> "${cfg}"
    else
        echo "nodefaultroute" >> "${cfg}"
    fi
    if is_yes "${UsePeerDNS:-yes}"; then
        echo "usepeerdns" >> "${cfg}"
    fi

    # Writes username and password
    echo "noauth" >> "${cfg}"
    echo "hide-password" >> ${cfg}
    [[ -n ${User} ]] && echo "user $(quote_word "${User}")" >> "${cfg}"
    [[ -n ${Password} ]] && echo "password $(quote_word "${Password}")" >> "${cfg}"

    #echo "'OK' @/etc/ppp/chatscripts/pin" >> "${chat}"
    if [ -n "${Pin}" ]; then
        PinStr="'OK' 'AT+CPIN=\"${Pin}\"'"
    else
        PinStr="'OK' 'AT'"
    fi
    report_debug echo $PinStr

    # Mode can be one of 3Gpref, 3Gonly, GPRSpref, GPRSonly, None
    # Only works for Huawei modems
    #echo "'OK' @/etc/ppp/chatscripts/mode" >> "${chat}"
    case "${Mode}" in
        3Gonly)
            ModeStr="'OK' 'AT\^SYSCFG=14,2,3fffffff,0,1'"
            ;;
        3Gpref)
            ModeStr="'OK' 'AT\^SYSCFG=2,2,3fffffff,0,1'"
            ;;
        GPRSonly)
            ModeStr="'OK' 'AT\^SYSCFG=13,1,3fffffff,0,0'"
            ;;
        GPRSpref)
            ModeStr="'OK' 'AT\^SYSCFG=2,1,3fffffff,0,0'"
            ;;
        *)
            ModeStr="'OK' 'AT'"
            ;;
    esac

    # Now that we’ve got the ppp configuration set up, write the chat script
    cat >> "${chat}" << EOF
ECHO ON
ABORT 'BUSY'
ABORT 'NO CARRIER'
ABORT 'VOICE'
ABORT 'NO DIALTONE'
ABORT 'NO DIAL TONE'
ABORT 'NO ANSWER'
ABORT 'DELAYED'
ABORT   '\nRINGING\r\n\r\nRINGING\r'
REPORT CONNECT
TIMEOUT 6
'' 'ATQ0'
'OK-AT-OK' 'ATZ'
TIMEOUT 3
'OK' 'AT+CFUN=1'
${PinStr}
'OK\d-AT-OK' 'ATI'
'OK' 'ATZ'
'OK' 'ATQ0 V1 E1 S0=0 &C1 &D2 +FCLASS=0'
${ModeStr}
'OK-AT-OK' 'AT+CGDCONT=1,"IP","${AccessPointName}"'
'OK' 'ATDT*99#'
TIMEOUT 30
CONNECT ''
EOF

    # Add the chat script line to the configuration
    echo "connect \"/usr/sbin/chat -v -t15 -f ${chat}\"" >> "${cfg}"

    if ! $PPPD file "${cfg}"; then
        rmdir "$STATE_DIR/mobile_ppp.${Interface}.${Profile}/"
        report_error "Couldn't make pppd connection."
        return 1
    fi
}

mobile_ppp_down() {
    local cfg chat pidfile pid
    cfg="$STATE_DIR/mobile_ppp.${Interface}.${Profile}/options"
    chat="$STATE_DIR/mobile_ppp.${Interface}.${Profile}/modem.chat"
    pidfile="/var/run/ppp-${Profile}.pid"

    if [[ -e $pidfile ]]; then
        read pid < "$pidfile"
        [[ "$pid" ]] && kill "$pid"
    fi

    rm "${cfg}" "${chat}"
    rmdir "$STATE_DIR/mobile_ppp.${Interface}.${Profile}/"
}


# vim: ft=sh ts=4 et sw=4: