Project:
IPFire
Code Location:
git://git.ipfire.org/network.gitmaster
/
functions.isdn
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
#!/bin/bash ############################################################################### # # # IPFire.org - A linux based firewall # # Copyright (C) 2011 Michael Tremer & Christian Schmidt # # # # This program is free software: you can redistribute it and/or modify # # it under the terms of the GNU General Public License as published by # # the Free Software Foundation, either version 3 of the License, or # # (at your option) any later version. # # # # This program is distributed in the hope that it will be useful, # # but WITHOUT ANY WARRANTY; without even the implied warranty of # # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # # GNU General Public License for more details. # # # # You should have received a copy of the GNU General Public License # # along with this program. If not, see <http://www.gnu.org/licenses/>. # # # ############################################################################### function isdn_config_dir() { local device=${1} local dir="${RUN_DIR}/isdn/${device}" [ -d "${dir}" ] || mkdir -p ${dir} echo "${dir}" } function isdn_create_device() { local device=${1} if device_exists ${device}; then return ${EXIT_OK} fi log INFO "Creating ISDN interface ${device}." isdnctrl addif ${device} >/dev/null } function isdn_remove_device() { local device=${1} if ! device_exists ${device}; then return ${EXIT_OK} fi log INFO "Removing ISDN interface ${device}." isdnctrl delif ${device} >/dev/null } function isdn_add_slave() { local device=${1} local slave=${2} assert device_exists ${device} assert isset slave log INFO "Creating ISDN slave interface ${slave} for device ${device}." isdnctrl addslave ${device} ${slave} local ret=$? case "${ret}" in 0) return ${EXIT_OK} ;; 255) log ERROR "Could not create slave device for ${device}." ;; esac return ${EXIT_ERROR} } function isdn_rem_slave() { local device=${1} local slave=${2} assert device_exists ${device} assert isset slave log INFO "Removing ISDN slave interface ${slave}." isdnctrl delslave ${device} ${slave} >/dev/null } function isdn_addlink() { local device=${1} assert device_exists ${device} log INFO "Adding link to ISDN interface ${device}." isdnctrl addlink ${device} >/dev/null } function isdn_get_encap() { local device=${1} assert device_exists ${device} isdnctrl encap ${device} | awk '{ print $NF }' } function isdn_set_encap() { local device=${1} local encap=${2} assert device_exists ${device} assert isset encap case "${encap}" in syncppp) ;; *) log ERROR "Cannot set unknown encapsulation: ${encap}" return ${EXIT_ERROR} ;; esac isdnctrl encap ${device} ${encap} >/dev/null } function isdn_get_l2proto() { local device=${1} assert device_exists ${device} isdnctrl l2_prot ${device} | awk '{ print $NF }' } function isdn_set_l2proto() { local device=${1} local proto=${2} assert device_exists ${device} assert isset proto case "${proto}" in hdlc) ;; *) log ERROR "Cannot set unknown l2 proto: ${proto}" return ${EXIT_ERROR} ;; esac isdnctrl l2_prot ${device} ${proto} >/dev/null } function isdn_get_l3proto() { local device=${1} assert device_exists ${device} isdnctrl l3_prot ${device} | awk '{ print $NF }' } function isdn_set_l3proto() { local device=${1} local proto=${2} assert device_exists ${device} assert isset proto case "${proto}" in trans) ;; *) log ERROR "Cannot set unknown l3 proto: ${proto}" return ${EXIT_ERROR} ;; esac isdnctrl l3_prot ${device} ${proto} >/dev/null } function isdn_set_dialmax() { local device=${1} local dialmax=${2} assert device_exists ${device} assert [ ${dialmax} -gt 0 ] isdnctrl dialmax ${device} ${dialmax} >/dev/null } function isdn_set_eaz() { local device=${1} local eaz=${2} assert device_exists ${device} assert isset eaz isdnctrl eaz ${device} ${eaz} >/dev/null } function isdn_set_dialmode() { local device=${1} local mode=${2} assert device_exists ${device} case "${proto}" in auto) ;; *) log ERROR "Cannot set unknown dialmode: ${mode}" return ${EXIT_ERROR} ;; esac isdnctrl dialmode ${device} ${mode} >/dev/null } function isdn_set_huptimeout() { local device=${1} local timeout=${2} assert device_exists ${device} assert isinteger timeout isdnctrl huptimeout ${device} ${mode} >/dev/null } function isdn_addphone() { local device=${1} local type=${2} local number=${3} assert device_exists ${device} assert isoneof type in out assert isset number isdnctrl addphone ${device} ${type} ${number} >/dev/null } function isdn_dial() { local device=${1} shift assert device_exists ${device} local mode="persistent" local channels="auto" local ipppd_args while [ $# -gt 0 ]; do case "${1}" in --mode=*) mode=${1#--mode=} ;; --channels=*) channels=${1#--channels=} ;; *) ipppd_args="${ipppd_args} ${1}" ;; esac shift done assert isset channels assert isset mode assert isoneof channels 1 2 auto # Start ippp daemon. ipppd_start ${ipppd_args} case "${mode}" in dialondemand) isdn_set_dialmode ${device} auto ;; persistent) case "${channels}" in auto) ibod_start ${device} ;; 1) # Do nothing. ;; 2) isdn_addlink ${device} ;; esac # Establish the connection immediately. isdnctrl dial ${device} >/dev/null ;; *) log ERROR "Unknown dial mode given: ${mode}." return ${EXIT_ERROR} ;; esac } function isdn_hangup() { local device=${1} assert device_exists ${device} # Bring isdn device down. ip link set ${device} down # Kill ippp daemon. ipppd_stop ${device} } function ipppd_start() { local device=${1} shift assert device_exists ${device} ipppd_write_config ${device} $@ # Actually run the service. service_start "ipppd@${device}.service" } function ipppd_write_config() { local device=${1} shift local value local auth="chap" local user local mppe="on" local mtu="1500" local proxyarp="on" local local_address local remote_address local netmask local dns_servers # mode tells us if we are running in server or # client mode. The collection of variables to # be set depends on this. local mode="client" while [ $# -gt 0 ]; do case "${1}" in --mode=*) mode=${1#--mode=} ;; --auth=*) auth=${1#--auth=} ;; --user=*) user=${1#--user=} ;; --mppe=*) mppe=${1#--mppe=} ;; --mtu=*) mtu=${1#--mtu=} ;; --proxyarp=*) value=${1#--proxyarp=} if enabled value; then proxyarp="on" else proxyarp="off" fi ;; --netmask=*) netmask=${1#--netmask=} ;; --local-address=*) local_address=${1#--local-address=} ;; --remote-address=*) remote_address=${1#--remote-address=} ;; --dns-server=*) value=${1#--dns-server=} # XXX check if this is actually an IP address dns_servers="${dns_servers} ${value}" ;; *) log WARN "Unknown argument given: ${1}" ;; esac shift done # Check if all common variables are correctly set. assert isset mtu assert isinteger mtu case "${mode}" in client) # Check if all variables for client mode are set. assert isset auth assert isset user ;; server) assert isset local_address assert isset remote_address assert isset netmask ;; *) log CRITICAL "Invalid mode given: ${mode}" exit ${EXIT_ERROR} ;; esac # Make a configuration file. local config="$(isdn_config_dir ${device})/config" cat >${config} <<EOF ### Common section # XXX find a solution for this /dev/${device} # Never change the default route -defaultroute # Link properties mru ${mtu} mtu ${mtu} lock noipdefault # Disable compression -vj -vjccomp debug EOF case "${mode}" in client) cat >>${config} <<EOF ### Client section # Authentication user ${user} +${auth} # Get remote DNS servers ms-get-dns EOF ;; server) cat >>${config} <<EOF ### Server section ${local_address}:${remote_address} netmask ${netmask} # Add DNS servers $(for value in ${dns_servers}; do echo "ms-dns ${value}"; done) EOF ;; esac } function ipppd_stop() { local device=${1} # Stop service. service_stop "ipppd@${device}.service" # Remove configuration file. rm -f $(isdn_config_dir ${device})/config } function ibod_start() { local device=${1} assert device_exists ${device} # Create ibod configuration file first. ibod_write_config $@ # Start the daemon. log INFO "Starting ibod service on device ${device}." service_start "ibod@${device}.service" } function ibod_config_file() { assert [ -n "$@" ] echo "$(isdn_config_dir $@)/ibod.cf" } function ibod_write_config() { local device=${1} log DEBUG "Writing ibod.cf for device ${device}." # Create path to configuration file. local config=$(ibod_config_file ${device}) # Empty configuration file. : > ${config} # Set the device to watch. echo "DEVICE ${device}" >> ${config} # We could set some more options here to # configure when ibod is bringing the second # channel up and down. I guess that is not # required at the moment. # See man ibod.cf. } function ibod_stop() { local device=${1} log INFO "Stopping ibod on device ${device}..." service_stop "ibod@${device}.service" # Remove ibod configuration file. rm -f $(ibod_config_file ${device}) }
