]> git.ipfire.org Git - people/ms/network.git/blob - src/hooks/zones/isdn-server
Use autotools.
[people/ms/network.git] / src / hooks / zones / isdn-server
1 #!/bin/bash
2 ###############################################################################
3 # #
4 # IPFire.org - A linux based firewall #
5 # Copyright (C) 2010 Michael Tremer & Christian Schmidt #
6 # #
7 # This program is free software: you can redistribute it and/or modify #
8 # it under the terms of the GNU General Public License as published by #
9 # the Free Software Foundation, either version 3 of the License, or #
10 # (at your option) any later version. #
11 # #
12 # This program is distributed in the hope that it will be useful, #
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of #
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
15 # GNU General Public License for more details. #
16 # #
17 # You should have received a copy of the GNU General Public License #
18 # along with this program. If not, see <http://www.gnu.org/licenses/>. #
19 # #
20 ###############################################################################
21
22 . /usr/lib/network/header-zone
23
24 HOOK_SETTINGS="HOOK LOCAL_ADDRESS REMOTE_ADDRESS MSN MTU MRU"
25 HOOK_SETTINGS="${HOOK_SETTINGS} L2PROTO L3PROTO ENCAP"
26
27 # The peer address of the ISDN server.
28 LOCAL_ADDRESS=
29 REMOTE_ADDRESS=
30
31 AUTH=
32 ENCAP="syncppp"
33 L2PROTO="hdlc"
34 L3PROTO="trans"
35 LINKNAME="$(uuid)"
36 MSN=
37 MTU=1500
38 TIMEOUT=10
39
40 MODE="persistent"
41
42 function hook_check() {
43 assert isset LOCAL_ADDRESS
44 assert isset REMOTE_ADDRESS
45
46 assert isset LINKNAME
47 assert isset TIMEOUT
48
49 assert isinteger MSN
50 assert isinteger TIMEOUT
51
52 isset AUTH && assert isoneof AUTH ${ISDN_ALLOWED_AUTHS}
53 }
54
55 function hook_parse_cmdline() {
56 local value
57
58 while [ $# -gt 0 ]; do
59 case "$1" in
60 --local-address=*)
61 LOCAL_ADDRESS=${1#--local-address=}
62 ;;
63 --remote-address=*)
64 REMOTE_ADDRESS=${1#--remote-address=}
65 ;;
66 --subnet=*)
67 SUBNET=${1#--subnet=}
68 ;;
69 --linkname=*)
70 LINKNAME=${1#--name=}
71 ;;
72 --mtu=*)
73 MTU=${1#--mtu=}
74 ;;
75 --defaultroute=*)
76 value=${1#--defaultroute=}
77 if enabled value; then
78 DEFAULTROUTE=1
79 else
80 DEFAULTROUTE=0
81 fi
82 ;;
83 --dns=*)
84 value=${1#--dns=}
85 if enabled value; then
86 PEERDNS=1
87 else
88 PEERDNS=0
89 fi
90 ;;
91 --auth=*)
92 AUTH=${1#--auth=}
93 ;;
94 --device=*)
95 DEVICE=${1#--device=}
96 ;;
97 --msn=*)
98 MSN=${1#--msn=}
99 ;;
100 --timeout=*)
101 TIMEOUT=${1#--timeout=}
102 ;;
103 --phone=*)
104 PHONE="${PHONE} ${1#--phone=}"
105 ;;
106 *)
107 echo "Unknown option: $1" >&2
108 exit ${EXIT_ERROR}
109 ;;
110 esac
111 shift
112 done
113 }
114
115 function hook_up() {
116 local zone=${1}
117 shift
118
119 assert isset zone
120
121 zone_config_read ${zone}
122
123 assert [ -e "/dev/${DEVICE}" ]
124
125 # Creating necessary files
126 # XXX must be PPP_RUN
127 [ -d "${RED_RUN}/${LINKNAME}" ] || mkdir -p ${RED_RUN}/${LINKNAME}
128
129 # Create device node.
130 isdn_create_device ${zone}
131
132 # Apply configuration to the ISDN stack.
133 isdn_set_l2proto ${zone} ${L2PROTO}
134 isdn_set_l3proto ${zone} ${L3PROTO}
135 isdn_set_encap ${zone} ${ENCAP}
136
137 isdn_set_eaz ${zone} ${MSN}
138 isdn_set_huptimeout ${zone} $(( ${TIMEOUT} * 60 ))
139
140 # Set our ip address.
141 ip_address_add ${zone} ${LOCAL_ADDRESS}
142 device_set_up ${zone}
143
144 # Start ipppd in server mode and make it listening for
145 # incoming connections:
146 local options
147
148 # Get a list of all DNS servers.
149 local dns_server
150 for dns_server in ${dns_servers}; do
151 options="${options} --dns-server=${dns_server}"
152 done
153
154 # Convert netmask.
155 local prefix=$(ip_get_prefix ${LOCAL_ADDRESS})
156 local netmask=$(ipv4_prefix2netmask ${prefix})
157
158 # Split prefix from LOCAL_ADDRESS.
159 local local_address=$(ip_split_prefix ${LOCAL_ADDRESS})
160
161 ipppd_start ${zone} \
162 --mode="server" \
163 --local-address="${local_address}" \
164 --remote-address="${REMOTE_ADDRESS}" \
165 --netmask="${netmask}" \
166 --mtu=${MTU} \
167 ${options}
168
169 exit ${EXIT_OK}
170 }
171
172 function hook_down() {
173 local zone=${1}
174 shift
175
176 # Kill ipppd service.
177 ipppd_stop ${zone}
178
179 # Bring down ISDN interface.
180 device_set_down ${zone}
181
182 # Remove ISDN device.
183 isdn_remove_device ${zone}
184
185 exit ${EXIT_OK}
186 }
187
188 function hook_status() {
189 local zone=${1}
190 assert isset zone
191
192 cli_device_headline ${zone}
193
194 zone_config_read ${zone}
195
196 cli_headline " Configuration:"
197 printf "${DEVICE_PRINT_LINE1}" "User:" "${USER}"
198 printf "${DEVICE_PRINT_LINE1}" "Secret:" "<hidden>"
199 echo
200 printf "${DEVICE_PRINT_LINE1}" "MTU:" "${MTU}"
201 printf "${DEVICE_PRINT_LINE1}" "Use default route?" "$(enabled DEFAULTROUTE && echo "enabled" || echo "disabled")"
202 printf "${DEVICE_PRINT_LINE1}" "Use peer DNS?" "$(enabled PEERDNS && echo "enabled" || echo "disabled")"
203 echo
204
205 if device_exists ${zone}; then
206 cli_headline " ISDN information:"
207 printf "${DEVICE_PRINT_LINE1}" "L2 protocol:" "$(isdn_get_l2proto ${zone})"
208 printf "${DEVICE_PRINT_LINE1}" "L3 protocol:" "$(isdn_get_l3proto ${zone})"
209 printf "${DEVICE_PRINT_LINE1}" "Encapsulation:" "$(isdn_get_encap ${zone})"
210 echo
211 fi
212
213 # Exit if zone is down
214 if ! zone_is_up ${zone}; then
215 echo # Empty line
216 exit ${EXIT_ERROR}
217 fi
218
219 # XXX display time since connection started
220
221 cli_headline " Point-to-Point-over-Ethernet protocol:"
222 echo " IP-Address : $(routing_db_get ${zone} local-ip-address)"
223 echo " Gateway : $(routing_db_get ${zone} remote-ip-address)"
224 echo " DNS-Server : $(routing_db_get ${zone} dns)"
225 echo
226 echo " MAC-Remote : $(routing_db_get ${zone} remote-address)"
227 echo
228 echo " MTU : $(device_get_mtu ${zone})"
229 echo # Empty line
230 exit ${EXIT_OK}
231 }