]> git.ipfire.org Git - people/ms/network.git/blob - src/functions/functions.isdn
hostapd: Enable WMM by default.
[people/ms/network.git] / src / functions / functions.isdn
1 #!/bin/bash
2 ###############################################################################
3 # #
4 # IPFire.org - A linux based firewall #
5 # Copyright (C) 2011 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 function isdn_config_dir() {
23 local device=${1}
24 local dir="${RUN_DIR}/isdn/${device}"
25
26 [ -d "${dir}" ] || mkdir -p ${dir}
27
28 echo "${dir}"
29 }
30
31 function isdn_create_device() {
32 local device=${1}
33
34 if device_exists ${device}; then
35 return ${EXIT_OK}
36 fi
37
38 log INFO "Creating ISDN interface ${device}."
39 isdnctrl addif ${device} >/dev/null
40 }
41
42 function isdn_remove_device() {
43 local device=${1}
44
45 if ! device_exists ${device}; then
46 return ${EXIT_OK}
47 fi
48
49 log INFO "Removing ISDN interface ${device}."
50 isdnctrl delif ${device} >/dev/null
51 }
52
53 function isdn_add_slave() {
54 local device=${1}
55 local slave=${2}
56
57 assert device_exists ${device}
58 assert isset slave
59
60 log INFO "Creating ISDN slave interface ${slave} for device ${device}."
61 isdnctrl addslave ${device} ${slave}
62 local ret=$?
63
64 case "${ret}" in
65 0)
66 return ${EXIT_OK}
67 ;;
68 255)
69 log ERROR "Could not create slave device for ${device}."
70 ;;
71 esac
72
73 return ${EXIT_ERROR}
74 }
75
76 function isdn_rem_slave() {
77 local device=${1}
78 local slave=${2}
79
80 assert device_exists ${device}
81 assert isset slave
82
83 log INFO "Removing ISDN slave interface ${slave}."
84 isdnctrl delslave ${device} ${slave} >/dev/null
85 }
86
87 function isdn_addlink() {
88 local device=${1}
89
90 assert device_exists ${device}
91
92 log INFO "Adding link to ISDN interface ${device}."
93 isdnctrl addlink ${device} >/dev/null
94 }
95
96 function isdn_get_encap() {
97 local device=${1}
98
99 assert device_exists ${device}
100
101 isdnctrl encap ${device} | awk '{ print $NF }'
102 }
103
104 function isdn_set_encap() {
105 local device=${1}
106 local encap=${2}
107
108 assert device_exists ${device}
109 assert isset encap
110
111 case "${encap}" in
112 syncppp) ;;
113 *)
114 log ERROR "Cannot set unknown encapsulation: ${encap}"
115 return ${EXIT_ERROR}
116 ;;
117 esac
118
119 isdnctrl encap ${device} ${encap} >/dev/null
120 }
121
122 function isdn_get_l2proto() {
123 local device=${1}
124
125 assert device_exists ${device}
126
127 isdnctrl l2_prot ${device} | awk '{ print $NF }'
128 }
129
130 function isdn_set_l2proto() {
131 local device=${1}
132 local proto=${2}
133
134 assert device_exists ${device}
135 assert isset proto
136
137 case "${proto}" in
138 hdlc) ;;
139 *)
140 log ERROR "Cannot set unknown l2 proto: ${proto}"
141 return ${EXIT_ERROR}
142 ;;
143 esac
144
145 isdnctrl l2_prot ${device} ${proto} >/dev/null
146 }
147
148 function isdn_get_l3proto() {
149 local device=${1}
150
151 assert device_exists ${device}
152
153 isdnctrl l3_prot ${device} | awk '{ print $NF }'
154 }
155
156 function isdn_set_l3proto() {
157 local device=${1}
158 local proto=${2}
159
160 assert device_exists ${device}
161 assert isset proto
162
163 case "${proto}" in
164 trans) ;;
165 *)
166 log ERROR "Cannot set unknown l3 proto: ${proto}"
167 return ${EXIT_ERROR}
168 ;;
169 esac
170
171 isdnctrl l3_prot ${device} ${proto} >/dev/null
172 }
173
174 function isdn_set_dialmax() {
175 local device=${1}
176 local dialmax=${2}
177
178 assert device_exists ${device}
179 assert [ ${dialmax} -gt 0 ]
180
181 isdnctrl dialmax ${device} ${dialmax} >/dev/null
182 }
183
184 function isdn_set_eaz() {
185 local device=${1}
186 local eaz=${2}
187
188 assert device_exists ${device}
189 assert isset eaz
190
191 isdnctrl eaz ${device} ${eaz} >/dev/null
192 }
193
194 function isdn_set_dialmode() {
195 local device=${1}
196 local mode=${2}
197
198 assert device_exists ${device}
199
200 case "${proto}" in
201 auto) ;;
202 *)
203 log ERROR "Cannot set unknown dialmode: ${mode}"
204 return ${EXIT_ERROR}
205 ;;
206 esac
207
208 isdnctrl dialmode ${device} ${mode} >/dev/null
209 }
210
211 function isdn_set_huptimeout() {
212 local device=${1}
213 local timeout=${2}
214
215 assert device_exists ${device}
216 assert isinteger timeout
217
218 isdnctrl huptimeout ${device} ${mode} >/dev/null
219 }
220
221 function isdn_addphone() {
222 local device=${1}
223 local type=${2}
224 local number=${3}
225
226 assert device_exists ${device}
227 assert isoneof type in out
228 assert isset number
229
230 isdnctrl addphone ${device} ${type} ${number} >/dev/null
231 }
232
233 function isdn_dial() {
234 local device=${1}
235 shift
236
237 assert device_exists ${device}
238
239 local mode="persistent"
240 local channels="auto"
241 local ipppd_args
242
243 while [ $# -gt 0 ]; do
244 case "${1}" in
245 --mode=*)
246 mode=${1#--mode=}
247 ;;
248 --channels=*)
249 channels=${1#--channels=}
250 ;;
251 *)
252 ipppd_args="${ipppd_args} ${1}"
253 ;;
254 esac
255 shift
256 done
257
258 assert isset channels
259 assert isset mode
260 assert isoneof channels 1 2 auto
261
262 # Start ippp daemon.
263 ipppd_start ${ipppd_args}
264
265 case "${mode}" in
266 dialondemand)
267 isdn_set_dialmode ${device} auto
268 ;;
269 persistent)
270 case "${channels}" in
271 auto)
272 ibod_start ${device}
273 ;;
274 1)
275 # Do nothing.
276 ;;
277 2)
278 isdn_addlink ${device}
279 ;;
280 esac
281
282 # Establish the connection immediately.
283 isdnctrl dial ${device} >/dev/null
284 ;;
285 *)
286 log ERROR "Unknown dial mode given: ${mode}."
287 return ${EXIT_ERROR}
288 ;;
289 esac
290 }
291
292 function isdn_hangup() {
293 local device=${1}
294
295 assert device_exists ${device}
296
297 # Bring isdn device down.
298 ip link set ${device} down
299
300 # Kill ippp daemon.
301 ipppd_stop ${device}
302 }
303
304 function ipppd_start() {
305 local device=${1}
306 shift
307
308 assert device_exists ${device}
309
310 ipppd_write_config ${device} $@
311
312 # Actually run the service.
313 service_start "ipppd@${device}.service"
314 }
315
316 function ipppd_write_config() {
317 local device=${1}
318 shift
319
320 local value
321 local auth="chap"
322 local user
323 local mppe="on"
324 local mtu="1500"
325 local proxyarp="on"
326 local local_address
327 local remote_address
328 local netmask
329 local dns_servers
330
331 # mode tells us if we are running in server or
332 # client mode. The collection of variables to
333 # be set depends on this.
334 local mode="client"
335
336 while [ $# -gt 0 ]; do
337 case "${1}" in
338 --mode=*)
339 mode=${1#--mode=}
340 ;;
341 --auth=*)
342 auth=${1#--auth=}
343 ;;
344 --user=*)
345 user=${1#--user=}
346 ;;
347 --mppe=*)
348 mppe=${1#--mppe=}
349 ;;
350 --mtu=*)
351 mtu=${1#--mtu=}
352 ;;
353 --proxyarp=*)
354 value=${1#--proxyarp=}
355 if enabled value; then
356 proxyarp="on"
357 else
358 proxyarp="off"
359 fi
360 ;;
361 --netmask=*)
362 netmask=${1#--netmask=}
363 ;;
364 --local-address=*)
365 local_address=${1#--local-address=}
366 ;;
367 --remote-address=*)
368 remote_address=${1#--remote-address=}
369 ;;
370 --dns-server=*)
371 value=${1#--dns-server=}
372 # XXX check if this is actually an IP address
373
374 dns_servers="${dns_servers} ${value}"
375 ;;
376 *)
377 log WARN "Unknown argument given: ${1}"
378 ;;
379 esac
380 shift
381 done
382
383 # Check if all common variables are correctly set.
384 assert isset mtu
385 assert isinteger mtu
386
387 case "${mode}" in
388 client)
389 # Check if all variables for client mode are set.
390 assert isset auth
391 assert isset user
392 ;;
393 server)
394 assert isset local_address
395 assert isset remote_address
396 assert isset netmask
397 ;;
398 *)
399 log CRITICAL "Invalid mode given: ${mode}"
400 exit ${EXIT_ERROR}
401 ;;
402 esac
403
404 # Make a configuration file.
405 local config="$(isdn_config_dir ${device})/config"
406
407 cat >${config} <<EOF
408 ### Common section
409
410 # XXX find a solution for this
411 /dev/${device}
412
413 # Never change the default route
414 -defaultroute
415
416 # Link properties
417 mru ${mtu}
418 mtu ${mtu}
419
420 lock
421 noipdefault
422
423 # Disable compression
424 -vj -vjccomp
425
426 debug
427
428 EOF
429
430 case "${mode}" in
431 client)
432 cat >>${config} <<EOF
433 ### Client section
434
435 # Authentication
436 user ${user}
437 +${auth}
438
439 # Get remote DNS servers
440 ms-get-dns
441
442 EOF
443 ;;
444
445
446 server)
447 cat >>${config} <<EOF
448 ### Server section
449
450 ${local_address}:${remote_address}
451 netmask ${netmask}
452
453 # Add DNS servers
454 $(for value in ${dns_servers}; do echo "ms-dns ${value}"; done)
455
456 EOF
457 ;;
458 esac
459 }
460
461 function ipppd_stop() {
462 local device=${1}
463
464 # Stop service.
465 service_stop "ipppd@${device}.service"
466
467 # Remove configuration file.
468 rm -f $(isdn_config_dir ${device})/config
469 }
470
471 function ibod_start() {
472 local device=${1}
473
474 assert device_exists ${device}
475
476 # Create ibod configuration file first.
477 ibod_write_config $@
478
479 # Start the daemon.
480 log INFO "Starting ibod service on device ${device}."
481 service_start "ibod@${device}.service"
482 }
483
484 function ibod_config_file() {
485 assert [ -n "$@" ]
486
487 echo "$(isdn_config_dir $@)/ibod.cf"
488 }
489
490 function ibod_write_config() {
491 local device=${1}
492
493 log DEBUG "Writing ibod.cf for device ${device}."
494
495 # Create path to configuration file.
496 local config=$(ibod_config_file ${device})
497
498 # Empty configuration file.
499 : > ${config}
500
501 # Set the device to watch.
502 echo "DEVICE ${device}" >> ${config}
503
504 # We could set some more options here to
505 # configure when ibod is bringing the second
506 # channel up and down. I guess that is not
507 # required at the moment.
508 # See man ibod.cf.
509 }
510
511 function ibod_stop() {
512 local device=${1}
513
514 log INFO "Stopping ibod on device ${device}..."
515 service_stop "ibod@${device}.service"
516
517 # Remove ibod configuration file.
518 rm -f $(ibod_config_file ${device})
519 }