SquashFS hinzugefuegt damit wir auf OpenVZ-Maschinen bauen koennen.
[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")); }
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 if ($pppsettings{'METHOD'} ne 'PPPOE_PLUGIN') {
519 my @pppcommand = ('/usr/sbin/pppd', 'pty');
520 my @pppoecommand = ('/usr/sbin/pppoe', '-p','/var/run/pppoe.pid','-I',
521 $netsettings{'RED_DEV'}, '-T', '80', '-U', '-m', '1412');
522
523 if ($pppsettings{'SERVICENAME'}) {
524 push(@pppoecommand, ('-S', $pppsettings{'SERVICENAME'})); }
525 if ($pppsettings{'CONCENTRATORNAME'}) {
526 push(@pppoecommand, ('-C', $pppsettings{'CONCENTRATORNAME'})); }
527
528 push(@pppcommand, "@pppoecommand");
529
530 if ($pppsettings{'DNS'} eq 'Automatic') {
531 push(@pppcommand, ('usepeerdns'));
532 }
533
534 if ($pppsettings{'AUTH'} eq 'pap') {
535 push(@pppcommand, ('-chap'));
536 } elsif ($pppsettings{'AUTH'} eq 'chap') {
537 push(@pppcommand, ('-pap'));
538 }
539
540 if ($pppsettings{'RECONNECTION'} ne 'persistent') {
541 if ($pppsettings{'TIMEOUT'} != 0) {
542 my $seconds = $pppsettings{'TIMEOUT'} * 60;
543 push(@pppcommand, ('idle', "$seconds"));
544 }
545 if ($pppsettings{'RECONNECTION'} eq 'dialondemand') {
546 push (@pppcommand, ('demand','nopersist','connect','/bin/true'));
547 }
548 push (@pppcommand,
549 ('active-filter',
550 'outbound and not icmp[0] == 3 and not tcp[13] & 4 != 0 ' ));
551 }
552
553 push(@pppcommand, ('noipdefault', 'default-asyncmap',
554 'defaultroute', 'hide-password', 'local',
555 'mtu', '1492', 'mru', '1492', 'noaccomp', 'noccp',
556 'nobsdcomp', 'nodeflate', 'nopcomp', 'novj', 'novjccomp',
557 'user', $pppsettings{'USERNAME'}, 'lcp-echo-interval', '20',
558 'lcp-echo-failure', '3', 'lcp-max-configure', '50',
559 'maxfail',$pppsettings{'MAXRETRIES'}));
560 if ($pppsettings{'DEBUG'} eq 'on') {
561 push(@pppcommand, ('debug'));
562 }
563
564 system (@pppcommand);
565 } else {
566 # PPPoE plugin
567 system ('/sbin/modprobe pppoe');
568 my @pppcommand = ('/usr/sbin/pppd');
569 push(@pppcommand,'plugin','rp-pppoe.so',"$netsettings{'RED_DEV'}");
570 if ($pppsettings{'DNS'} eq 'Automatic') {
571 push(@pppcommand, ('usepeerdns'));
572 }
573 if ($pppsettings{'AUTH'} eq 'pap') {
574 push(@pppcommand, ('-chap'));
575 } elsif ($pppsettings{'AUTH'} eq 'chap') {
576 push(@pppcommand, ('-pap'));
577 }
578 if ($pppsettings{'RECONNECTION'} ne 'persistent') {
579 if ($pppsettings{'TIMEOUT'} != 0) {
580 my $seconds = $pppsettings{'TIMEOUT'} * 60;
581 push(@pppcommand, ('idle', "$seconds"));
582 }
583 if ($pppsettings{'RECONNECTION'} eq 'dialondemand') {
584 push (@pppcommand, ('demand','nopersist'));
585 }
586 push (@pppcommand,
587 ('active-filter',
588 'outbound and not icmp[0] == 3 and not tcp[13] & 4 != 0 ' ));
589 }
590 push(@pppcommand, ('noipdefault', 'defaultroute', 'hide-password', 'ipcp-accept-local',
591 'ipcp-accept-remote', 'passive', 'noccp','nopcomp', 'novjccomp',
592 'user', $pppsettings{'USERNAME'}, 'lcp-echo-interval', '20',
593 'lcp-echo-failure', '3', 'lcp-max-configure', '50',
594 'maxfail',$pppsettings{'MAXRETRIES'}));
595 if ($pppsettings{'DEBUG'} eq 'on') {
596 push(@pppcommand, ('debug'));
597 }
598
599 system (@pppcommand);
600 }
601 }
602
603 sub dopptpdial
604 {
605 my %pptpdhcpc;
606 my $routerip = $pppsettings{'ROUTERIP'} ? $pppsettings{'ROUTERIP'} : "10.0.0.138";
607 if ( $pppsettings{'METHOD'} eq 'DHCP' && open(FILE, "${General::swroot}/red/device")) {
608 my $device = <FILE>;
609 close FILE;
610 chomp ($device);
611 $device =~ /([a-zA-Z0-9]*)/; $device = $1;
612 if (&General::readhash("${General::swroot}/dhcpc/dhcpcd-$device.info", \%pptpdhcpc)) {
613 system("/sbin/route add -host $routerip gw $pptpdhcpc{'GATEWAY'}");
614 } else {
615 system("/sbin/route add -host $routerip dev $device");
616 }
617 }
618
619 my @pppcommand = ('/usr/sbin/pppd', 'pty');
620 my @pptpcommand = ('/usr/sbin/pptp', $routerip, '--nobuffer', '--nolaunchpppd', '--sync');
621 if ($pppsettings{'PHONEBOOK'}) {
622 push (@pptpcommand, ('--phone ', $pppsettings{'PHONEBOOK'}));
623 }
624
625 push(@pppcommand, "@pptpcommand");
626
627 if ($pppsettings{'DNS'} eq 'Automatic') {
628 push(@pppcommand, ('usepeerdns'));
629 }
630 if ($pppsettings{'AUTH'} eq 'pap') {
631 push(@pppcommand, ('-chap'));
632 } elsif ($pppsettings{'AUTH'} eq 'chap') {
633 push(@pppcommand, ('-pap'));
634 }
635
636 if ($pppsettings{'RECONNECTION'} ne 'persistent') {
637 if ($pppsettings{'TIMEOUT'} != 0) {
638 my $seconds = $pppsettings{'TIMEOUT'} * 60;
639 push(@pppcommand, ('idle', "$seconds"));
640 }
641 if ($pppsettings{'RECONNECTION'} eq 'dialondemand') {
642 push (@pppcommand, ('demand','nopersist','connect','/bin/true'));
643 }
644 push (@pppcommand,
645 ('active-filter',
646 'outbound and not icmp[0] == 3 and not tcp[13] & 4 != 0 ' ));
647 }
648
649 push(@pppcommand, ('noipdefault', 'default-asyncmap',
650 'defaultroute', 'hide-password', 'local','noaccomp', 'noccp',
651 'nobsdcomp', 'nodeflate', 'nopcomp', 'novj', 'novjccomp',
652 'user', $pppsettings{'USERNAME'}, 'lcp-echo-interval', '20',
653 'lcp-echo-failure', '3', 'lcp-max-configure', '50',
654 'maxfail',$pppsettings{'MAXRETRIES'},'sync'));
655 if ($pppsettings{'DEBUG'} eq 'on') {
656 push(@pppcommand, ('debug'));
657 }
658
659 system (@pppcommand);
660 }
661
662 sub dofritzdsldial
663 {
664 my $controller;
665
666 if (system ('/etc/rc.d/rc.fritzdsl','start')) {
667 &General::log ("ERROR: Fritz DSL module failed to load");
668 exit 1;
669 }
670
671 # controller number
672 if ($pppsettings{'TYPE'} eq 'fritzdsl') {
673 if ( ! system ('/bin/grep', '1244:2700', '/proc/pci')) {
674 $controller=1; # fcdslsl
675 } elsif (! system('/bin/grep', '1244:2900', '/proc/pci')) {
676 $controller=2; # fcdsl2
677 } elsif (! system('/bin/grep', '1131:5402', '/proc/pci')) {
678 $controller=2; # fdsl
679 } elsif (! system('/bin/grep', 'Vendor=057c ProdID=2300', '/proc/bus/usb/devices')) {
680 $controller=1; # fcdslusb
681 } elsif (! system('/bin/grep', 'Vendor=057c ProdID=3500', '/proc/bus/usb/devices')) {
682 $controller=1; # fcdslslusb
683 }
684 }
685 my @pppcommand = ('/usr/sbin/pppd');
686 my @capiplugin;
687
688 if ($pppsettings{'DNS'} eq 'Automatic') {
689 push(@pppcommand, ('usepeerdns'));
690 }
691
692 if ($pppsettings{'RECONNECTION'} ne 'persistent') {
693 if ($pppsettings{'TIMEOUT'} != 0) {
694 my $seconds = $pppsettings{'TIMEOUT'} * 60;
695 push(@pppcommand, ('idle', "$seconds"));
696 }
697 if ($pppsettings{'RECONNECTION'} eq 'dialondemand') {
698 push (@pppcommand, ('demand','nopersist','connect','/bin/true'));
699 }
700 push (@pppcommand,
701 ('active-filter',
702 'outbound and not icmp[0] == 3 and not tcp[13] & 4 != 0 ' ));
703 }
704
705 push(@pppcommand, ('noipdefault', 'defaultroute', 'sync', 'user',
706 $pppsettings{'USERNAME'}, 'ipcp-accept-local', 'ipcp-accept-remote', 'passive',
707 'noaccomp', 'nopcomp', 'noccp', 'novj', 'nobsdcomp',
708 'nodeflate', 'lcp-echo-interval', '20', 'lcp-echo-failure', '3',
709 'lcp-max-configure', '50', 'maxfail', $pppsettings{'MAXRETRIES'}));
710
711 if ($pppsettings{'DEBUG'} eq 'on') {
712 push(@pppcommand, ('debug'));
713 }
714
715 if ($pppsettings {'PROTOCOL'} eq 'RFC1483') {
716 @capiplugin = ('plugin', 'capiplugin.so', 'protocol', 'adslpppoe',
717 'controller', $controller, 'vpi', $pppsettings{'VPI'},'vci',$pppsettings{'VCI'});
718 } else {
719 if ($pppsettings {'ENCAP'} eq '1') {
720 @capiplugin = ('plugin', 'capiplugin.so', 'protocol', 'adslpppoallc',
721 'controller', $controller, 'vpi', $pppsettings{'VPI'},'vci',$pppsettings{'VCI'});
722 } else {
723 @capiplugin = ('plugin', 'capiplugin.so', 'protocol', 'adslpppoa',
724 'controller', $controller,'vpi', $pppsettings{'VPI'},'vci',$pppsettings{'VCI'});
725 }
726 }
727 push(@pppcommand, @capiplugin);
728 push(@pppcommand, '/dev/null');
729
730 system (@pppcommand);
731 }
732
733 sub doatmdial
734 {
735 my $ENCAP;
736 if ($pppsettings {'PROTOCOL'} eq 'RFC2364') {
737 system ('/sbin/modprobe pppoatm');
738 my @pppcommand = ('/usr/sbin/pppd');
739 if ($pppsettings{'ENCAP'} eq '0') { $ENCAP='vc-encaps'; } else { $ENCAP='llc-encaps'; }
740 push(@pppcommand,'plugin', 'pppoatm.so',$pppsettings{'VPI'}.".".$pppsettings{'VCI'},"$ENCAP");
741 if ($pppsettings{'DNS'} eq 'Automatic') { push(@pppcommand, ('usepeerdns'));}
742 if ($pppsettings{'AUTH'} eq 'pap') {
743 push(@pppcommand, ('-chap'));
744 } elsif ($pppsettings{'AUTH'} eq 'chap') {
745 push(@pppcommand, ('-pap'));
746 }
747 if ($pppsettings{'RECONNECTION'} ne 'persistent') {
748 if ($pppsettings{'TIMEOUT'} != 0) {
749 my $seconds = $pppsettings{'TIMEOUT'} * 60;
750 push(@pppcommand, ('idle', "$seconds"));
751 }
752 if ($pppsettings{'RECONNECTION'} eq 'dialondemand') {
753 push (@pppcommand, ('demand','nopersist'));
754 }
755 push (@pppcommand,
756 ('active-filter',
757 'outbound and not icmp[0] == 3 and not tcp[13] & 4 != 0 ' ));
758 }
759 push(@pppcommand, ('noipdefault', 'defaultroute', 'user',
760 $pppsettings{'USERNAME'}, 'ipcp-accept-local', 'ipcp-accept-remote', 'passive',
761 'nopcomp', 'noccp', 'novj', 'nobsdcomp',
762 'nodeflate', 'lcp-echo-interval', '20', 'lcp-echo-failure', '3',
763 'lcp-max-configure', '50', 'maxfail', $pppsettings{'MAXRETRIES'}));
764
765 if ($pppsettings{'DEBUG'} eq 'on') {
766 push(@pppcommand, ('debug'));
767 }
768
769 system (@pppcommand);
770 } elsif ($pppsettings {'PROTOCOL'} eq 'RFC1483') {
771 if ($pppsettings {'METHOD'} =~ /^(PPPOE|PPPOE_PLUGIN)$/) {
772 my $itf='0';
773 my $device = "nas$itf";
774 if (open(FILE, ">${General::swroot}/red/device")) { print FILE $device; close FILE; }
775 $netsettings{'RED_DEV'} = $device;
776 if (system ('/bin/ps -ef | /bin/grep -q [b]r2684ctl')) {
777 system ('/sbin/modprobe br2684');
778 system ('/usr/bin/br2684ctl', '-b', '-c', "$itf", '-e', $pppsettings{'ENCAP'}, '-a', "$itf.$pppsettings{'VPI'}.$pppsettings{'VCI'}");
779 sleep 3;
780 }
781 system ('/sbin/ifconfig',"$device",'up');
782 &dopppoedial();
783 } elsif ($pppsettings{'ENCAP'} =~ /^(0|1)$/) {
784 my $itf='0';
785 $iface = "nas$itf";
786 if (open(FILE, ">${General::swroot}/red/iface")) { print FILE $iface; close FILE; }
787 if (system ('/bin/ps -ef | /bin/grep -q [b]r2684ctl')) {
788 system ('/sbin/modprobe br2684');
789 system ('/usr/bin/br2684ctl', '-b', '-c', "$itf", '-e', $pppsettings{'ENCAP'}, '-a', "$itf.$pppsettings{'VPI'}.$pppsettings{'VCI'}");
790 sleep 3;
791 }
792 system ('/sbin/ifconfig',"$iface",'up');
793
794 if ($pppsettings{'METHOD'} eq 'STATIC') {
795 my @staticcommand = ('/sbin/ifconfig');
796 push(@staticcommand, ($iface, $pppsettings{'IP'},'netmask', $pppsettings{'NETMASK'}));
797 if ($pppsettings{'BROADCAST'} ne '') {
798 push(@staticcommand, ('broadcast', $pppsettings{'BROADCAST'}));
799 }
800 system (@staticcommand);
801 system ("/sbin/route","add","default","gw",$pppsettings{'GATEWAY'});
802 system ("/bin/touch", "${General::swroot}/red/active");
803 system ("/etc/rc.d/init.d/network red update");
804 } elsif ($pppsettings {'METHOD'} eq 'DHCP') {
805 dodhcpdial($iface,$pppsettings{'DHCP_HOSTNAME'});
806 }
807 } elsif ($pppsettings{'ENCAP'} =~ /^(2|3)$/) {
808 my $itf='0';
809 $iface = "atm$itf";
810 if (open(FILE, ">${General::swroot}/red/iface")) { print FILE $iface; close FILE; }
811 if (system ('/bin/ps -ef | /bin/grep -q [a]tmarpd')) {
812 if (system ('/usr/sbin/atmarpd -b -l syslog')) {
813 &General::log('atmarpd fail');
814 exit 1;
815 }
816 # it will fail on all attempt after the first because interface still exist
817 system ("/usr/sbin/atmarp -c $iface 2>/dev/null");
818
819 if ($pppsettings{'METHOD'} eq 'STATIC') {
820 my @staticcommand = ('/sbin/ifconfig');
821 push(@staticcommand, ($iface, $pppsettings{'IP'},'netmask', $pppsettings{'NETMASK'}, 'up'));
822 if ($pppsettings{'BROADCAST'} ne '') {
823 push(@staticcommand, ('broadcast', $pppsettings{'BROADCAST'}));
824 }
825 system (@staticcommand);
826 # we have to wait a bit before launching atmarp -s
827 sleep 2;
828 my @atmarp = ('/usr/sbin/atmarp', '-s', $pppsettings{'GATEWAY'}, "$itf.$pppsettings{'VPI'}.$pppsettings{'VCI'}");
829 if ($pppsettings{'ENCAP'} eq '3') {
830 push(@atmarp, 'null' ); # routed ip vc encap
831 }
832 system (@atmarp);
833 system ("/sbin/route", "add", "default", "gw", $pppsettings{'GATEWAY'});
834 system ("/bin/touch", "${General::swroot}/red/active");
835 }
836 }
837 }
838 }
839 }