]> git.ipfire.org Git - people/pmueller/ipfire-2.x.git/blob - src/initscripts/init.d/red
Credits ueberarbeitet.
[people/pmueller/ipfire-2.x.git] / src / initscripts / init.d / red
1 #!/usr/bin/perl
2 #
3 # This file is part of the IPCop Firewall.
4 #
5 # IPCop is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 2 of the License, or
8 # (at your option) any later version.
9 #
10 # IPCop is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with IPCop; if not, write to the Free Software
17 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 #
19 # (c) The SmoothWall Team
20 #
21
22 # Clean up our environment (we're running SUID!)
23 delete @ENV{qw(IFS CDPATH ENV BASH_ENV PATH)};
24 $< = $>;
25
26 use strict;
27 require '/var/ipfire/general-functions.pl';
28
29 my %pppsettings;
30 my %isdnsettings;
31 my %netsettings;
32 my %dhcpsettings;
33 my $iface;
34
35 # read vars back from file.
36 &General::readhash("${General::swroot}/ppp/settings", \%pppsettings);
37 &General::readhash("${General::swroot}/isdn/settings", \%isdnsettings);
38 &General::readhash("${General::swroot}/ethernet/settings", \%netsettings);
39 &General::readhash("${General::swroot}/dhcp/settings", \%dhcpsettings);
40
41 sub dodhcpdial($;$) {
42 my ($iface,$dhcp_name)=@_;
43
44 system ('/sbin/iptables', '-A', 'REDINPUT', '-p', 'tcp', '--source-port', '67',
45 '--destination-port', '68', '-i', $iface, '-j', 'ACCEPT');
46 system ('/sbin/iptables', '-A', 'REDINPUT', '-p', 'udp', '--source-port', '67',
47 '--destination-port', '68', '-i', $iface, '-j', 'ACCEPT');
48
49 foreach ("<${General::swroot}/dhcpc/*.info>") { unlink $1 if ( $_ =~ /^([\/\w.-]+)$/ ); }
50 my @dhcpcommand = ('/sbin/dhcpcd');
51 push(@dhcpcommand, ('-N', '-R', "$iface",'-L', "${General::swroot}/dhcpc"));
52
53 #FIXME the only way actually to set debug use is in pppsetup.cgi and 'RED is modem/isdn' interface
54 if ($pppsettings{'DEBUG'} eq 'on') {
55 push(@dhcpcommand, ('-d'));
56 }
57
58 if ($dhcp_name ne '') { push(@dhcpcommand, ('-h', "$dhcp_name", "&>/dev/null")); }
59 if ($netsettings{'RED_TYPE'} eq 'PPTP') { push(@dhcpcommand, '-G'); }
60
61 if (system (@dhcpcommand)) {
62 &General::log('dhcpcd fail');
63 exit 1;
64 } else {
65 &General::log('dhcpcd success');
66 }
67 }
68
69 sub doupdatesettings {
70 # complete cleanup only if settings were changed or clear is ordered
71 system('/sbin/modprobe', '-r', 'pppoatm');
72 system('/sbin/modprobe', '-r', 'pppoe');
73 system('/bin/killall /usr/bin/br2684ctl 2>/dev/null');
74 system('/sbin/modprobe', '-r', 'br2684');
75 system('/sbin/modprobe', '-r', 'clip');
76
77 if ($pppsettings{'TYPE'} ne 'fritzdsl') {
78 system('/sbin/modprobe', '-r', 'fcdsl', 'fcdsl2', 'fcdslsl', 'fcdslusb', 'fcdslslusb');}
79 sleep 1;
80
81 # remove existing default route (for static address) if it was been changed from setup or web interface SF1175052
82 system ('/sbin/route del default 2>/dev/null');
83
84 # erase in case it was created once with 'persistent' selected but rc.red stop never used : SF1171610
85 unlink ("${General::swroot}/red/iface");
86 }
87
88 # No output should be sent to the webclient
89 open STDIN, '</dev/zero' or die "Can't read from /dev/zero";
90 open STDOUT, '>/dev/null' or die "Can't write to /dev/null";
91
92 if ($ARGV[0] eq 'start') {
93 if (-e "${General::swroot}/red/active" ||
94 -e '/var/run/ppp-ipfire.pid')
95 {
96 &General::log ("ERROR: Can't start RED when it's still active");
97 exit 1;
98 }
99
100 if ( ( ( ($netsettings{'RED_TYPE'} =~ /^(PPPOE|PPTP)$/) && ($netsettings{'CONFIG_TYPE'} =~ /^(2|3|6|7)$/) ) ||
101 ( ( ($pppsettings{'METHOD'} =~ /^(PPPOE|PPPOE_PLUGIN)$/) || ($pppsettings{'PROTOCOL'} eq 'RFC2364') ) &&
102 ($netsettings{'CONFIG_TYPE'} =~ /^(0|1|4|5)$/) ) ) && ($pppsettings{'RECONNECTION'} ne 'manual') ) {
103 system ('/etc/rc.d/init.d/connectioncheck start &');
104 }
105
106 ###
107 ### Red device is ethernet
108 ###
109 if ($netsettings{'CONFIG_TYPE'} =~ /^(2|3|6|7)$/)
110 {
111 if ($netsettings{'RED_DEV'} ne '')
112 {
113 &General::log("Starting RED device $netsettings{'RED_DEV'}.");
114
115 if ( $netsettings{'RED_TYPE'} eq 'DHCP')
116 {
117 if (open(FILE, ">${General::swroot}/red/iface")) { print FILE $netsettings{'RED_DEV'}; close FILE; }
118 dodhcpdial($netsettings{'RED_DEV'},$netsettings{'RED_DHCP_HOSTNAME'});
119 exit 0;
120 }
121 elsif ( ( $netsettings{'RED_TYPE'} eq 'PPTP') && ( $pppsettings{'METHOD'} eq 'DHCP') )
122 {
123 if (open(FILE, ">${General::swroot}/red/device")) { print FILE $netsettings{'RED_DEV'}; close FILE; }
124 unlink ("${General::swroot}/red/iface");
125 dodhcpdial($netsettings{'RED_DEV'},$netsettings{'RED_DHCP_HOSTNAME'});
126 }
127 elsif ( ( $netsettings{'RED_TYPE'} eq 'STATIC') ||
128 ( $netsettings{'RED_TYPE'} eq 'PPTP') && ( $pppsettings{'METHOD'} ne 'DHCP') )
129 {
130 system ("/sbin/ifconfig",
131 $netsettings{'RED_DEV'}, $netsettings{'RED_ADDRESS'},
132 "netmask", $netsettings{'RED_NETMASK'},
133 "broadcast", $netsettings{'RED_BROADCAST'},"up");
134 if ( $netsettings{'RED_TYPE'} eq 'STATIC')
135 {
136 system("/usr/local/bin/setaliases");
137 system("echo $netsettings{'DNS1'} > ${General::swroot}/red/dns1");
138 system("echo $netsettings{'DNS2'} > ${General::swroot}/red/dns2");
139 system("echo $netsettings{'RED_ADDRESS'} > ${General::swroot}/red/local-ipaddress");
140 system("echo $netsettings{'DEFAULT_GATEWAY'} > ${General::swroot}/red/remote-ipaddress");
141 } elsif ( $netsettings{'RED_TYPE'} eq 'PPTP' ) {
142 if (open(FILE, ">${General::swroot}/red/device")) { print FILE $netsettings{'RED_DEV'}; close FILE; }
143 unlink ("${General::swroot}/red/iface");
144 }
145 if ( $netsettings{'DEFAULT_GATEWAY'} ne '' )
146 {
147 system ("/sbin/route","add","default","gw",
148 $netsettings{'DEFAULT_GATEWAY'});
149 }
150 }
151 else
152 {
153 # PPPoE
154 system ("/sbin/ifconfig", $netsettings{'RED_DEV'}, "1.1.1.1",
155 "netmask", "255.255.255.0", "broadcast", "1.1.1.255", "up");
156 }
157
158 if ( $netsettings{'RED_TYPE'} eq 'STATIC')
159 {
160 if (open(FILE, ">${General::swroot}/red/iface")) { print FILE $netsettings{'RED_DEV'}; close FILE; }
161 system ("/bin/touch", "${General::swroot}/red/active");
162 system ("/etc/rc.d/init.d/network red update");
163 exit 0;
164 }
165 }
166 else
167 {
168 &General::log ("ERROR: Can't start RED when RED device not set!");
169 exit 1;
170 }
171 }
172
173
174 if ($pppsettings{'RECONNECTION'} eq 'dialondemand')
175 {
176 system ('/bin/touch', "${General::swroot}/red/dial-on-demand");
177 }
178
179 if ($pppsettings{'VALID'} ne 'yes') {
180 &General::log("Profile has errors.");
181 exit 1;
182 }
183
184 if (-e "${General::swroot}/ppp/updatesettings") {
185 &doupdatesettings;
186 }
187
188 if (( $pppsettings{'METHOD'} eq 'STATIC') && ( $pppsettings{'DNS'} eq 'Manual')) {
189 system("/usr/local/bin/setaliases");
190 if (open(FILE, ">${General::swroot}/red/dns1")) { print FILE $pppsettings{'DNS1'}; close FILE; }
191 if (open(FILE, ">${General::swroot}/red/dns2")) { print FILE $pppsettings{'DNS2'}; close FILE; }
192 if (open(FILE, ">${General::swroot}/red/local-ipaddress")) { print FILE $pppsettings{'IP'}; close FILE; }
193 if (open(FILE, ">${General::swroot}/red/remote-ipaddress")) { print FILE $pppsettings{'GATEWAY'}; close FILE; }
194 }
195 if ($pppsettings{'RECONNECTION'} eq 'dialondemand') {
196 &General::log("Dial-on-Demand waiting to dial $pppsettings{'PROFILENAME'}.");
197 } else {
198 &General::log("Dialling $pppsettings{'PROFILENAME'}.");
199 }
200
201 if ($pppsettings{'TYPE'} eq 'modem') { &domodemdial(); }
202 elsif ($pppsettings{'TYPE'} eq 'serial') { &doserialdial(); }
203 elsif ($pppsettings{'TYPE'} eq 'isdn') { &doisdndial(); }
204 elsif ($pppsettings{'TYPE'} eq 'pppoe') { &dopppoedial(); }
205 elsif ($pppsettings{'TYPE'} eq 'pptp') { &dopptpdial(); }
206 elsif ($pppsettings{'TYPE'} eq 'fritzdsl') { &dofritzdsldial(); }
207
208 if (-e "${General::swroot}/ppp/updatesettings") {
209 # erase update mark only after specific script had run, allowing specific script to treat the update
210 unlink ("${General::swroot}/ppp/updatesettings");
211 }
212 if ( ($pppsettings{'RECONNECTION'} eq 'dialondemand') || ($pppsettings{'METHOD'} eq 'STATIC') ){
213 system ("/etc/rc.d/init.d/network red update");
214 }
215 }
216 elsif ($ARGV[0] eq 'stop')
217 {
218 if (open(IFACE, "${General::swroot}/red/iface")) {
219 $iface = <IFACE>;
220 close IFACE;
221 chomp ($iface);
222 $iface =~ /([a-zA-Z0-9]*)/; $iface = $1;
223 }
224
225 my $device;
226 if (open(FILE, "${General::swroot}/red/device")) {
227 $device = <FILE>;
228 close FILE;
229 chomp ($device);
230 $device =~ /([a-zA-Z0-9]*)/; $device = $1;
231 }
232
233 unlink "${General::swroot}/red/dial-on-demand";
234 unlink "${General::swroot}/red/active";
235 unlink "${General::swroot}/red/local-ipaddress";
236 unlink "${General::swroot}/red/remote-ipaddress";
237 unlink "${General::swroot}/red/dns1";
238 unlink "${General::swroot}/red/dns2";
239 unlink "${General::swroot}/red/resolv.conf";
240 unlink "${General::swroot}/red/device";
241
242 # stay with keepconnected during transitional rc.red stop ordered by connectioncheck
243 if ( ! -e "${General::swroot}/red/redial") {
244 unlink "${General::swroot}/red/keepconnected";
245 }
246 unlink "${General::swroot}/red/redial";
247
248 # Kill PPPD
249 if (open(FILE, "/var/run/ppp-ipfire.pid")) {
250 my $pid = <FILE>;
251 close FILE;
252 chomp ($pid);
253 $pid =~ /(\d*)/; $pid = $1;
254 system ('/bin/kill', $pid);
255 }
256
257 # Bring down Ethernet interfaces & Kill DHCPC daemons
258 if (($netsettings{'CONFIG_TYPE'} =~ /^(2|3|6|7)$/) && ( $netsettings{'RED_TYPE'} eq 'PPPOE') && $iface ) {
259 system ("/sbin/ifconfig", $iface, "down");
260 }
261 if ($device) {
262 system ("/sbin/ifconfig", $device, "down");
263 }
264
265 my $file;
266 while (($file = glob("/var/run/dhcpcd-*.pid") )) {
267 if (open(FILE, $file)) {
268 my $pid = <FILE>;
269 close FILE;
270 chomp ($pid);
271 $pid =~ /(\d*)/; $pid = $1;
272 system ('/bin/kill', $pid);
273 }
274 }
275
276 if (!system ('/bin/ps -ef | /bin/grep -q [a]tmarpd')) {
277 if ($pppsettings{'GATEWAY'} ne '') {
278 system("/usr/sbin/atmarp -d $pppsettings{'GATEWAY'} 2>/dev/null"); }
279 system('/bin/killall /usr/sbin/atmarpd 2>/dev/null');
280 system ('/sbin/ifconfig', 'atm0', 'down');
281 }
282
283 if ($pppsettings{'TYPE'} eq 'isdn') { system('/etc/rc.d/rc.isdn','stop'); }
284 if ($pppsettings{'TYPE'} eq 'fritzdsl') { system ('/etc/rc.d/rc.fritzdsl','stop'); }
285
286 if ( ( $netsettings{'CONFIG_TYPE'} =~ /^(2|3|6|7)$/ && $netsettings{'RED_TYPE'} eq 'STATIC') ||
287 ( $netsettings{'CONFIG_TYPE'} =~ /^(0|1|4|5)$/ && $pppsettings{'PROTOCOL'} eq 'RFC1483' &&
288 $pppsettings{'METHOD'} eq 'STATIC' ) ) {
289 system ("/etc/rc.d/init.d/network red update");
290 }
291 }
292 elsif ($ARGV[0] eq 'clear')
293 {
294 &doupdatesettings();
295 &docleanup();
296 }
297
298 exit 0;
299
300 sub docleanup
301 {
302 if ($pppsettings{'TYPE'} eq 'fritzdsl') { system ('/etc/rc.d/rc.fritzdsl','cleanup'); }
303 }
304
305 sub domodemdial
306 {
307 my @pppcommand = ('/usr/sbin/pppd');
308 my $loginscript = '';
309
310 if ($pppsettings{'COMPORT'} =~ /ttyACM/) {
311 system ('/sbin/rmmod acm');
312 sleep 1;
313 system ('/sbin/modprobe acm');
314 }
315
316 my $device = "/dev/${pppsettings{'COMPORT'}}";
317
318 if ($pppsettings{'DNS'} eq 'Automatic') {
319 push(@pppcommand, ('usepeerdns')); }
320
321 if ($pppsettings{'AUTH'} eq 'pap') {
322 push(@pppcommand, ('-chap'));
323 } elsif ($pppsettings{'AUTH'} eq 'chap') {
324 push(@pppcommand, ('-pap'));
325 } elsif ($pppsettings{'AUTH'} eq 'standard-login-script') {
326 $loginscript = 'standardloginscript';
327 } elsif ($pppsettings{'AUTH'} eq 'demon-login-script') {
328 $loginscript = 'demonloginscript';
329 } else {
330 $loginscript = $pppsettings{'LOGINSCRIPT'};
331 }
332
333 if ($pppsettings{'RECONNECTION'} ne 'persistent') {
334 if ($pppsettings{'TIMEOUT'} != 0)
335 {
336 my $seconds = $pppsettings{'TIMEOUT'} * 60;
337 push (@pppcommand, ('idle', $seconds));
338 }
339 if ($pppsettings{'RECONNECTION'} eq 'dialondemand') {
340 push (@pppcommand, ('demand', 'nopersist'));
341 }
342 push (@pppcommand,
343 ('active-filter',
344 'outbound and not icmp[0] == 3 and not tcp[13] & 4 != 0 ' ));
345 }
346
347 push (@pppcommand, ('novj', 'novjccomp'));
348
349 push (@pppcommand, ('lock', 'modem', 'crtscts', $device,
350 $pppsettings{'DTERATE'}, 'noipdefault',
351 'defaultroute', 'user', $pppsettings{'USERNAME'},
352 'maxfail', $pppsettings{'MAXRETRIES'}, 'connect',
353 '/etc/ppp/dialer'));
354 if ($pppsettings{'DEBUG'} eq 'on') {
355 push(@pppcommand, ('debug'));
356 }
357
358 system @pppcommand;
359 }
360
361 sub doserialdial
362 {
363 my @pppcommand = ('/usr/sbin/pppd');
364 my $loginscript = '';
365
366 if ($pppsettings{'COMPORT'} =~ /ttyACM/) {
367 system ('/sbin/rmmod acm');
368 sleep 1;
369 system ('/sbin/modprobe acm');
370 }
371
372 my $device = "/dev/${pppsettings{'COMPORT'}}";
373
374 if ($pppsettings{'DNS'} eq 'Automatic') {
375 push(@pppcommand, ('usepeerdns')); }
376
377 if ($pppsettings{'AUTH'} eq 'pap') {
378 push(@pppcommand, ('-chap'));
379 } elsif ($pppsettings{'AUTH'} eq 'chap') {
380 push(@pppcommand, ('-pap'));
381 }
382
383 if ($pppsettings{'RECONNECTION'} ne 'persistent') {
384 if ($pppsettings{'TIMEOUT'} != 0)
385 {
386 my $seconds = $pppsettings{'TIMEOUT'} * 60;
387 push (@pppcommand, ('idle', $seconds));
388 }
389 if ($pppsettings{'RECONNECTION'} eq 'dialondemand') {
390 push (@pppcommand, ('demand', 'nopersist'));
391 }
392 push (@pppcommand,
393 ('active-filter',
394 'outbound and not icmp[0] == 3 and not tcp[13] & 4 != 0 ' ));
395 }
396
397 push (@pppcommand, ('novj', 'novjccomp'));
398
399 push (@pppcommand, ('lock', 'modem', 'crtscts', $device,
400 $pppsettings{'DTERATE'}, 'noipdefault',
401 'defaultroute', 'user', $pppsettings{'USERNAME'},
402 'maxfail', $pppsettings{'MAXRETRIES'}, 'connect',
403 '/bin/true'));
404 if ($pppsettings{'DEBUG'} eq 'on') {
405 push(@pppcommand, ('debug'));
406 }
407
408 system @pppcommand;
409 }
410
411 sub doisdndial
412 {
413 my $pppoptions;
414 my $seconds;
415 my $phone;
416
417 if (system ('/etc/rc.d/rc.isdn', 'start')) {
418 &General::log ("ERROR: ISDN module failed to load");
419 exit 1;
420 }
421
422 $seconds = $pppsettings{'TIMEOUT'} * 60;
423 if ($pppsettings{'USEDOV'} eq 'on')
424 {
425 $phone = 'v' . $pppsettings{'TELEPHONE'};
426 }
427 else
428 {
429 $phone = $pppsettings{'TELEPHONE'};
430 };
431
432 if ($pppsettings{'COMPORT'} eq 'isdn2')
433 {
434 system('/usr/sbin/isdnctrl','addif','ippp0');
435 system('/usr/sbin/isdnctrl','addslave','ippp0','ippp1');
436 system('/usr/sbin/isdnctrl','l2_prot','ippp0','hdlc');
437 system('/usr/sbin/isdnctrl','l3_prot','ippp0','trans');
438 system('/usr/sbin/isdnctrl','encap','ippp0','syncppp');
439 system('/usr/sbin/isdnctrl','dialmax','ippp0',$pppsettings{'MAXRETRIES'});
440 system('/usr/sbin/isdnctrl','eaz','ippp0',$isdnsettings{'MSN'});
441 system('/usr/sbin/isdnctrl','addphone','ippp0','out',$phone);
442 system('/usr/sbin/isdnctrl','huptimeout','ippp0',$seconds);
443 system('/usr/sbin/isdnctrl','l2_prot','ippp1','hdlc');
444 system('/usr/sbin/isdnctrl','l3_prot','ippp1','trans');
445 system('/usr/sbin/isdnctrl','encap','ippp1','syncppp');
446 system('/usr/sbin/isdnctrl','dialmax','ippp1',$pppsettings{'MAXRETRIES'});
447 system('/usr/sbin/isdnctrl','eaz','ippp0',$isdnsettings{'MSN'});
448 system('/usr/sbin/isdnctrl','addphone','ippp1','out',$phone);
449 system('/usr/sbin/isdnctrl','huptimeout','ippp1',$seconds);
450 system('/usr/sbin/isdnctrl','dialmode','ippp1','auto');
451
452 my @pppcommand = ('/usr/sbin/ipppd','ms-get-dns','noipdefault','+mp',
453 'defaultroute','user',$pppsettings{'USERNAME'},
454 'name',$pppsettings{'USERNAME'},
455 'active-filter','outbound and not icmp[0] == 3 and not tcp[13] & 4 != 0',
456 'pidfile','/var/run/ppp-ipfire.pid','/dev/ippp0','/dev/ippp1');
457 if ($pppsettings{'DEBUG'} eq 'on') {
458 push(@pppcommand, ('debug'));
459 }
460 if ($pppsettings{'AUTH'} eq 'pap') {
461 push(@pppcommand, ('-chap'));
462 } elsif ($pppsettings{'AUTH'} eq 'chap') {
463 push(@pppcommand, ('-pap'));
464 }
465 system (@pppcommand);
466 }
467 else
468 {
469 system('/usr/sbin/isdnctrl','addif','ippp0');
470 system('/usr/sbin/isdnctrl','l2_prot','ippp0','hdlc');
471 system('/usr/sbin/isdnctrl','l3_prot','ippp0','trans');
472 system('/usr/sbin/isdnctrl','encap','ippp0','syncppp');
473 system('/usr/sbin/isdnctrl','dialmax','ippp0',$pppsettings{'MAXRETRIES'});
474 system('/usr/sbin/isdnctrl','eaz','ippp0',$isdnsettings{'MSN'});
475 system('/usr/sbin/isdnctrl','addphone','ippp0','out',$phone);
476 system('/usr/sbin/isdnctrl','huptimeout','ippp0',$seconds);
477
478 my @pppcommand = ('/usr/sbin/ipppd','ms-get-dns','noipdefault',
479 'defaultroute','user',$pppsettings{'USERNAME'},
480 'name',$pppsettings{'USERNAME'},
481 'active-filter','outbound and not icmp[0] == 3 and not tcp[13] & 4 != 0',
482 'pidfile','/var/run/ppp-ipfire.pid','/dev/ippp0');
483 if ($pppsettings{'DEBUG'} eq 'on') {
484 push(@pppcommand, ('debug'));
485 }
486 if ($pppsettings{'AUTH'} eq 'pap') {
487 push(@pppcommand, ('-chap'));
488 } elsif ($pppsettings{'AUTH'} eq 'chap') {
489 push(@pppcommand, ('-pap'));
490 }
491 system (@pppcommand);
492 }
493
494 sleep 1;
495
496 if ($pppsettings{'RECONNECTION'} eq 'dialondemand') {
497 system('/usr/sbin/isdnctrl','dialmode','ippp0','auto');
498 system('/sbin/ifconfig','ippp0','10.112.112.112','pointopoint','10.112.112.113');
499 system('/sbin/ifconfig','ippp0','-arp','-broadcast');
500 system('/sbin/route','add','default','dev','ippp0');
501 } else {
502 system('/usr/sbin/isdnctrl', 'dial', 'ippp0');
503
504 }
505
506 system('/bin/killall', 'ibod');
507 if ($pppsettings{'COMPORT'} eq 'isdn2') {
508 if ($pppsettings{'USEIBOD'} eq 'on') {
509 system("/usr/sbin/ibod &");
510 } else {
511 system('/usr/sbin/isdnctrl', 'addlink', 'ippp0');
512 }
513 }
514 }
515
516 sub dopppoedial
517 {
518 system("mknod /dev/ppp c 108 0 2>/dev/null");
519 if ($pppsettings{'METHOD'} ne 'PPPOE_PLUGIN') {
520 my @pppcommand = ('/usr/sbin/pppd', 'pty');
521 my @pppoecommand = ('/usr/sbin/pppoe', '-p','/var/run/pppoe.pid','-I',
522 $netsettings{'RED_DEV'}, '-T', '80', '-U', '-m', '1412');
523
524 if ($pppsettings{'SERVICENAME'}) {
525 push(@pppoecommand, ('-S', $pppsettings{'SERVICENAME'})); }
526 if ($pppsettings{'CONCENTRATORNAME'}) {
527 push(@pppoecommand, ('-C', $pppsettings{'CONCENTRATORNAME'})); }
528
529 push(@pppcommand, "@pppoecommand");
530
531 if ($pppsettings{'DNS'} eq 'Automatic') {
532 push(@pppcommand, ('usepeerdns'));
533 }
534
535 if ($pppsettings{'AUTH'} eq 'pap') {
536 push(@pppcommand, ('-chap'));
537 } elsif ($pppsettings{'AUTH'} eq 'chap') {
538 push(@pppcommand, ('-pap'));
539 }
540
541 if ($pppsettings{'RECONNECTION'} ne 'persistent') {
542 if ($pppsettings{'TIMEOUT'} != 0) {
543 my $seconds = $pppsettings{'TIMEOUT'} * 60;
544 push(@pppcommand, ('idle', "$seconds"));
545 }
546 if ($pppsettings{'RECONNECTION'} eq 'dialondemand') {
547 push (@pppcommand, ('demand','nopersist','connect','/bin/true'));
548 }
549 push (@pppcommand,
550 ('active-filter',
551 'outbound and not icmp[0] == 3 and not tcp[13] & 4 != 0 ' ));
552 }
553
554 push(@pppcommand, ('noipdefault', 'default-asyncmap',
555 'defaultroute', 'hide-password', 'local',
556 'mtu', '1492', 'mru', '1492', 'noaccomp', 'noccp',
557 'nobsdcomp', 'nodeflate', 'nopcomp', 'novj', 'novjccomp',
558 'user', $pppsettings{'USERNAME'}, 'lcp-echo-interval', '20',
559 'lcp-echo-failure', '3', 'lcp-max-configure', '50',
560 'maxfail',$pppsettings{'MAXRETRIES'}));
561 if ($pppsettings{'DEBUG'} eq 'on') {
562 push(@pppcommand, ('debug'));
563 }
564
565 system (@pppcommand);
566 } else {
567 # PPPoE plugin
568 system ('/sbin/modprobe pppoe');
569 my @pppcommand = ('/usr/sbin/pppd');
570 push(@pppcommand,'plugin','rp-pppoe.so',"$netsettings{'RED_DEV'}");
571 if ($pppsettings{'DNS'} eq 'Automatic') {
572 push(@pppcommand, ('usepeerdns'));
573 }
574 if ($pppsettings{'AUTH'} eq 'pap') {
575 push(@pppcommand, ('-chap'));
576 } elsif ($pppsettings{'AUTH'} eq 'chap') {
577 push(@pppcommand, ('-pap'));
578 }
579 if ($pppsettings{'RECONNECTION'} ne 'persistent') {
580 if ($pppsettings{'TIMEOUT'} != 0) {
581 my $seconds = $pppsettings{'TIMEOUT'} * 60;
582 push(@pppcommand, ('idle', "$seconds"));
583 }
584 if ($pppsettings{'RECONNECTION'} eq 'dialondemand') {
585 push (@pppcommand, ('demand','nopersist'));
586 }
587 push (@pppcommand,
588 ('active-filter',
589 'outbound and not icmp[0] == 3 and not tcp[13] & 4 != 0 ' ));
590 }
591 push(@pppcommand, ('noipdefault', 'defaultroute', 'hide-password', 'ipcp-accept-local',
592 'ipcp-accept-remote', 'passive', 'noccp','nopcomp', 'novjccomp',
593 'user', $pppsettings{'USERNAME'}, 'lcp-echo-interval', '20',
594 'lcp-echo-failure', '3', 'lcp-max-configure', '50',
595 'maxfail',$pppsettings{'MAXRETRIES'}));
596 if ($pppsettings{'DEBUG'} eq 'on') {
597 push(@pppcommand, ('debug'));
598 }
599
600 system (@pppcommand);
601 }
602 }
603
604 sub dopptpdial
605 {
606 my %pptpdhcpc;
607 my $routerip = $pppsettings{'ROUTERIP'} ? $pppsettings{'ROUTERIP'} : "10.0.0.138";
608 system("mknod /dev/ppp c 108 0 2>/dev/null");
609 if ( $pppsettings{'METHOD'} eq 'DHCP' && open(FILE, "${General::swroot}/red/device")) {
610 my $device = <FILE>;
611 close FILE;
612 chomp ($device);
613 $device =~ /([a-zA-Z0-9]*)/; $device = $1;
614 if (&General::readhash("${General::swroot}/dhcpc/dhcpcd-$device.info", \%pptpdhcpc)) {
615 system("/sbin/route add -host $routerip gw $pptpdhcpc{'GATEWAY'}");
616 } else {
617 system("/sbin/route add -host $routerip dev $device");
618 }
619 }
620
621 my @pppcommand = ('/usr/sbin/pppd', 'pty');
622 my @pptpcommand = ('/usr/sbin/pptp', $routerip, '--nobuffer', '--nolaunchpppd', '--sync');
623 if ($pppsettings{'PHONEBOOK'}) {
624 push (@pptpcommand, ('--phone ', $pppsettings{'PHONEBOOK'}));
625 }
626
627 push(@pppcommand, "@pptpcommand");
628
629 if ($pppsettings{'DNS'} eq 'Automatic') {
630 push(@pppcommand, ('usepeerdns'));
631 }
632 if ($pppsettings{'AUTH'} eq 'pap') {
633 push(@pppcommand, ('-chap'));
634 } elsif ($pppsettings{'AUTH'} eq 'chap') {
635 push(@pppcommand, ('-pap'));
636 }
637
638 if ($pppsettings{'RECONNECTION'} ne 'persistent') {
639 if ($pppsettings{'TIMEOUT'} != 0) {
640 my $seconds = $pppsettings{'TIMEOUT'} * 60;
641 push(@pppcommand, ('idle', "$seconds"));
642 }
643 if ($pppsettings{'RECONNECTION'} eq 'dialondemand') {
644 push (@pppcommand, ('demand','nopersist','connect','/bin/true'));
645 }
646 push (@pppcommand,
647 ('active-filter',
648 'outbound and not icmp[0] == 3 and not tcp[13] & 4 != 0 ' ));
649 }
650
651 push(@pppcommand, ('noipdefault', 'default-asyncmap',
652 'defaultroute', 'hide-password', 'local','noaccomp', 'noccp',
653 'nobsdcomp', 'nodeflate', 'nopcomp', 'novj', 'novjccomp',
654 'user', $pppsettings{'USERNAME'}, 'lcp-echo-interval', '20',
655 'lcp-echo-failure', '3', 'lcp-max-configure', '50',
656 'maxfail',$pppsettings{'MAXRETRIES'},'sync'));
657 if ($pppsettings{'DEBUG'} eq 'on') {
658 push(@pppcommand, ('debug'));
659 }
660
661 system (@pppcommand);
662 }
663
664 sub dofritzdsldial
665 {
666 my $controller;
667
668 if (system ('/etc/rc.d/rc.fritzdsl','start')) {
669 &General::log ("ERROR: Fritz DSL module failed to load");
670 exit 1;
671 }
672
673 # controller number
674 if ($pppsettings{'TYPE'} eq 'fritzdsl') {
675 if ( ! system ('/bin/grep', '1244:2700', '/proc/pci')) {
676 $controller=1; # fcdslsl
677 } elsif (! system('/bin/grep', '1244:2900', '/proc/pci')) {
678 $controller=2; # fcdsl2
679 } elsif (! system('/bin/grep', '1131:5402', '/proc/pci')) {
680 $controller=2; # fdsl
681 } elsif (! system('/bin/grep', 'Vendor=057c ProdID=2300', '/proc/bus/usb/devices')) {
682 $controller=1; # fcdslusb
683 } elsif (! system('/bin/grep', 'Vendor=057c ProdID=3500', '/proc/bus/usb/devices')) {
684 $controller=1; # fcdslslusb
685 }
686 }
687 system("mknod /dev/ppp c 108 0 2>/dev/null");
688 my @pppcommand = ('/usr/sbin/pppd');
689 my @capiplugin;
690
691 if ($pppsettings{'DNS'} eq 'Automatic') {
692 push(@pppcommand, ('usepeerdns'));
693 }
694
695 if ($pppsettings{'RECONNECTION'} ne 'persistent') {
696 if ($pppsettings{'TIMEOUT'} != 0) {
697 my $seconds = $pppsettings{'TIMEOUT'} * 60;
698 push(@pppcommand, ('idle', "$seconds"));
699 }
700 if ($pppsettings{'RECONNECTION'} eq 'dialondemand') {
701 push (@pppcommand, ('demand','nopersist','connect','/bin/true'));
702 }
703 push (@pppcommand,
704 ('active-filter',
705 'outbound and not icmp[0] == 3 and not tcp[13] & 4 != 0 ' ));
706 }
707
708 push(@pppcommand, ('noipdefault', 'defaultroute', 'sync', 'user',
709 $pppsettings{'USERNAME'}, 'ipcp-accept-local', 'ipcp-accept-remote', 'passive',
710 'noaccomp', 'nopcomp', 'noccp', 'novj', 'nobsdcomp',
711 'nodeflate', 'lcp-echo-interval', '20', 'lcp-echo-failure', '3',
712 'lcp-max-configure', '50', 'maxfail', $pppsettings{'MAXRETRIES'}));
713
714 if ($pppsettings{'DEBUG'} eq 'on') {
715 push(@pppcommand, ('debug'));
716 }
717
718 if ($pppsettings {'PROTOCOL'} eq 'RFC1483') {
719 @capiplugin = ('plugin', 'capiplugin.so', 'protocol', 'adslpppoe',
720 'controller', $controller, 'vpi', $pppsettings{'VPI'},'vci',$pppsettings{'VCI'});
721 } else {
722 if ($pppsettings {'ENCAP'} eq '1') {
723 @capiplugin = ('plugin', 'capiplugin.so', 'protocol', 'adslpppoallc',
724 'controller', $controller, 'vpi', $pppsettings{'VPI'},'vci',$pppsettings{'VCI'});
725 } else {
726 @capiplugin = ('plugin', 'capiplugin.so', 'protocol', 'adslpppoa',
727 'controller', $controller,'vpi', $pppsettings{'VPI'},'vci',$pppsettings{'VCI'});
728 }
729 }
730 push(@pppcommand, @capiplugin);
731 push(@pppcommand, '/dev/null');
732
733 system (@pppcommand);
734 }
735
736 sub doatmdial
737 {
738 my $ENCAP;
739 system("mknod /dev/ppp c 108 0 2>/dev/null");
740 if ($pppsettings {'PROTOCOL'} eq 'RFC2364') {
741 system ('/sbin/modprobe pppoatm');
742 my @pppcommand = ('/usr/sbin/pppd');
743 if ($pppsettings{'ENCAP'} eq '0') { $ENCAP='vc-encaps'; } else { $ENCAP='llc-encaps'; }
744 push(@pppcommand,'plugin', 'pppoatm.so',$pppsettings{'VPI'}.".".$pppsettings{'VCI'},"$ENCAP");
745 if ($pppsettings{'DNS'} eq 'Automatic') { push(@pppcommand, ('usepeerdns'));}
746 if ($pppsettings{'AUTH'} eq 'pap') {
747 push(@pppcommand, ('-chap'));
748 } elsif ($pppsettings{'AUTH'} eq 'chap') {
749 push(@pppcommand, ('-pap'));
750 }
751 if ($pppsettings{'RECONNECTION'} ne 'persistent') {
752 if ($pppsettings{'TIMEOUT'} != 0) {
753 my $seconds = $pppsettings{'TIMEOUT'} * 60;
754 push(@pppcommand, ('idle', "$seconds"));
755 }
756 if ($pppsettings{'RECONNECTION'} eq 'dialondemand') {
757 push (@pppcommand, ('demand','nopersist'));
758 }
759 push (@pppcommand,
760 ('active-filter',
761 'outbound and not icmp[0] == 3 and not tcp[13] & 4 != 0 ' ));
762 }
763 push(@pppcommand, ('noipdefault', 'defaultroute', 'user',
764 $pppsettings{'USERNAME'}, 'ipcp-accept-local', 'ipcp-accept-remote', 'passive',
765 'nopcomp', 'noccp', 'novj', 'nobsdcomp',
766 'nodeflate', 'lcp-echo-interval', '20', 'lcp-echo-failure', '3',
767 'lcp-max-configure', '50', 'maxfail', $pppsettings{'MAXRETRIES'}));
768
769 if ($pppsettings{'DEBUG'} eq 'on') {
770 push(@pppcommand, ('debug'));
771 }
772
773 system (@pppcommand);
774 } elsif ($pppsettings {'PROTOCOL'} eq 'RFC1483') {
775 if ($pppsettings {'METHOD'} =~ /^(PPPOE|PPPOE_PLUGIN)$/) {
776 my $itf='0';
777 my $device = "nas$itf";
778 if (open(FILE, ">${General::swroot}/red/device")) { print FILE $device; close FILE; }
779 $netsettings{'RED_DEV'} = $device;
780 if (system ('/bin/ps -ef | /bin/grep -q [b]r2684ctl')) {
781 system ('/sbin/modprobe br2684');
782 system ('/usr/bin/br2684ctl', '-b', '-c', "$itf", '-e', $pppsettings{'ENCAP'}, '-a', "$itf.$pppsettings{'VPI'}.$pppsettings{'VCI'}");
783 sleep 3;
784 }
785 system ('/sbin/ifconfig',"$device",'up');
786 &dopppoedial();
787 } elsif ($pppsettings{'ENCAP'} =~ /^(0|1)$/) {
788 my $itf='0';
789 $iface = "nas$itf";
790 if (open(FILE, ">${General::swroot}/red/iface")) { print FILE $iface; close FILE; }
791 if (system ('/bin/ps -ef | /bin/grep -q [b]r2684ctl')) {
792 system ('/sbin/modprobe br2684');
793 system ('/usr/bin/br2684ctl', '-b', '-c', "$itf", '-e', $pppsettings{'ENCAP'}, '-a', "$itf.$pppsettings{'VPI'}.$pppsettings{'VCI'}");
794 sleep 3;
795 }
796 system ('/sbin/ifconfig',"$iface",'up');
797
798 if ($pppsettings{'METHOD'} eq 'STATIC') {
799 my @staticcommand = ('/sbin/ifconfig');
800 push(@staticcommand, ($iface, $pppsettings{'IP'},'netmask', $pppsettings{'NETMASK'}));
801 if ($pppsettings{'BROADCAST'} ne '') {
802 push(@staticcommand, ('broadcast', $pppsettings{'BROADCAST'}));
803 }
804 system (@staticcommand);
805 system ("/sbin/route","add","default","gw",$pppsettings{'GATEWAY'});
806 system ("/bin/touch", "${General::swroot}/red/active");
807 system ("/etc/rc.d/init.d/network red update");
808 } elsif ($pppsettings {'METHOD'} eq 'DHCP') {
809 dodhcpdial($iface,$pppsettings{'DHCP_HOSTNAME'});
810 }
811 } elsif ($pppsettings{'ENCAP'} =~ /^(2|3)$/) {
812 my $itf='0';
813 $iface = "atm$itf";
814 if (open(FILE, ">${General::swroot}/red/iface")) { print FILE $iface; close FILE; }
815 if (system ('/bin/ps -ef | /bin/grep -q [a]tmarpd')) {
816 if (system ('/usr/sbin/atmarpd -b -l syslog')) {
817 &General::log('atmarpd fail');
818 exit 1;
819 }
820 # it will fail on all attempt after the first because interface still exist
821 system ("/usr/sbin/atmarp -c $iface 2>/dev/null");
822
823 if ($pppsettings{'METHOD'} eq 'STATIC') {
824 my @staticcommand = ('/sbin/ifconfig');
825 push(@staticcommand, ($iface, $pppsettings{'IP'},'netmask', $pppsettings{'NETMASK'}, 'up'));
826 if ($pppsettings{'BROADCAST'} ne '') {
827 push(@staticcommand, ('broadcast', $pppsettings{'BROADCAST'}));
828 }
829 system (@staticcommand);
830 # we have to wait a bit before launching atmarp -s
831 sleep 2;
832 my @atmarp = ('/usr/sbin/atmarp', '-s', $pppsettings{'GATEWAY'}, "$itf.$pppsettings{'VPI'}.$pppsettings{'VCI'}");
833 if ($pppsettings{'ENCAP'} eq '3') {
834 push(@atmarp, 'null' ); # routed ip vc encap
835 }
836 system (@atmarp);
837 system ("/sbin/route", "add", "default", "gw", $pppsettings{'GATEWAY'});
838 system ("/bin/touch", "${General::swroot}/red/active");
839 }
840 }
841 }
842 }
843 }