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 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 863 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042
|
/***********************************************************************
*
* pppoe.c
*
* Implementation of user-space PPPoE redirector for Linux.
*
* Copyright (C) 2000-2015 by Roaring Penguin Software Inc.
* Copyright (C) 2018-2021 Dianne Skoll
*
* This program may be distributed according to the terms of the GNU
* General Public License, version 2 or (at your option) any later version.
*
* LIC: GPL
*
***********************************************************************/
#define _GNU_SOURCE 1
#include "pppoe.h"
#ifdef HAVE_SYSLOG_H
#include <syslog.h>
#endif
#ifdef HAVE_GETOPT_H
#include <getopt.h>
#endif
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#ifdef HAVE_SYS_TIME_H
#include <sys/time.h>
#endif
#ifdef HAVE_SYS_UIO_H
#include <sys/uio.h>
#endif
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#ifdef USE_LINUX_PACKET
#include <sys/ioctl.h>
#include <fcntl.h>
#endif
#include <signal.h>
#ifdef HAVE_N_HDLC
#ifndef N_HDLC
#include <pty.h>
#endif
#endif
/* Default interface if no -I option given */
#define DEFAULT_IF "eth0"
/* Global variables -- options */
int optInactivityTimeout = 0; /* Inactivity timeout */
int optClampMSS = 0; /* Clamp MSS to this value */
int optSkipSession = 0; /* Perform discovery, print session info
and exit */
int optFloodDiscovery = 0; /* Flood server with discovery requests.
USED FOR STRESS-TESTING ONLY. DO NOT
USE THE -F OPTION AGAINST A REAL ISP */
#ifdef AUTO_IFUP
/* for interface activation, based on stripped down source source of ifconfig*/
#include <linux/if.h>
/*#include <sys/socket.h> */
#include <sys/types.h>
#include <sys/socket.h>
#include "config.h"
int skfd = -1; /* generic raw socket desc. */
int sockets_open(int family)
{
int sfd = -1;
sfd = socket(AF_INET, SOCK_DGRAM, 0);
return sfd;
}
/* Like strncpy but make sure the resulting string is always 0 terminated.
* Ripped from util.c (net-tools package) */
char *safe_strncpy(char *dst, const char *src, size_t size)
{
dst[size-1] = '\0';
return strncpy(dst,src,size-1);
}
/* Set a certain interface flag. Ripped from ifconfig.c */
static int set_flag(char *ifname, short flag)
{
struct ifreq ifr;
safe_strncpy(ifr.ifr_name, ifname, IFNAMSIZ);
if (ioctl(skfd, SIOCGIFFLAGS, &ifr) < 0) {
fprintf(stderr, "%s: unknown interface: %s\n",
ifname, strerror(errno));
return (-1);
}
safe_strncpy(ifr.ifr_name, ifname, IFNAMSIZ);
ifr.ifr_flags |= flag;
if (ioctl(skfd, SIOCSIFFLAGS, &ifr) < 0) {
perror("SIOCSIFFLAGS");
return -1;
}
return (0);
}
#endif
PPPoEConnection *Connection = NULL; /* Must be global -- used
in signal handler */
/***********************************************************************
*%FUNCTION: sendSessionPacket
*%ARGUMENTS:
* conn -- PPPoE connection
* packet -- the packet to send
* len -- length of data to send
*%RETURNS:
* Nothing
*�SCRIPTION:
* Transmits a session packet to the peer.
***********************************************************************/
void
sendSessionPacket(PPPoEConnection *conn, PPPoEPacket *packet, int len)
{
packet->length = htons(len);
if (optClampMSS) {
clampMSS(packet, "outgoing", optClampMSS);
}
if (sendPacket(conn, conn->sessionSocket, packet, len + HDR_SIZE) < 0) {
if (errno == ENOBUFS) {
/* No buffer space is a transient error */
return;
}
exit(EXIT_FAILURE);
}
#ifdef DEBUGGING_ENABLED
if (conn->debugFile) {
dumpPacket(conn->debugFile, packet, "SENT");
fprintf(conn->debugFile, "\n");
fflush(conn->debugFile);
}
#endif
}
#ifdef USE_BPF
/**********************************************************************
*%FUNCTION: sessionDiscoveryPacket
*%ARGUMENTS:
* packet -- the discovery packet that was received
*%RETURNS:
* Nothing
*�SCRIPTION:
* We got a discovery packet during the session stage. This most likely
* means a PADT.
*
* The BSD version uses a single socket for both discovery and session
* packets. When a packet comes in over the wire once we are in
* session mode, either syncReadFromEth() or asyncReadFromEth() will
* have already read the packet and determined it to be a discovery
* packet before passing it here.
***********************************************************************/
static void
sessionDiscoveryPacket(PPPoEPacket *packet)
{
/* Sanity check */
if (packet->code != CODE_PADT) {
return;
}
/* It's a PADT, all right. Is it for us? */
if (packet->session != Connection->session) {
/* Nope, ignore it */
return;
}
if (memcmp(packet->ethHdr.h_dest, Connection->myEth, ETH_ALEN)) {
return;
}
if (memcmp(packet->ethHdr.h_source, Connection->peerEth, ETH_ALEN)) {
return;
}
syslog(LOG_INFO,
"Session %d terminated -- received PADT from peer",
(int) ntohs(packet->session));
parsePacket(packet, parseLogErrs, NULL);
sendPADT(Connection, "Received PADT from peer");
exit(EXIT_SUCCESS);
}
#else
/**********************************************************************
*%FUNCTION: sessionDiscoveryPacket
*%ARGUMENTS:
* conn -- PPPoE connection
*%RETURNS:
* Nothing
*�SCRIPTION:
* We got a discovery packet during the session stage. This most likely
* means a PADT.
***********************************************************************/
static void
sessionDiscoveryPacket(PPPoEConnection *conn)
{
PPPoEPacket packet;
int len;
if (receivePacket(conn->discoverySocket, &packet, &len) < 0) {
return;
}
/* Check length */
if (ntohs(packet.length) + HDR_SIZE > len) {
syslog(LOG_ERR, "Bogus PPPoE length field (%u)",
(unsigned int) ntohs(packet.length));
return;
}
/* Is it for our session? */
if (packet.session != conn->session) {
/* Nope, ignore it */
return;
}
/* Is it for our Ethernet interface? */
if (memcmp(packet.ethHdr.h_dest, conn->myEth, ETH_ALEN)) {
/* Nope, ignore it */
return;
}
/* Is it from our peer's Ethernet interface? */
if (memcmp(packet.ethHdr.h_source, conn->peerEth, ETH_ALEN)) {
/* Nope, ignore it */
return;
}
if (packet.code != CODE_PADT) {
/* Not PADT; ignore it */
return;
}
#ifdef DEBUGGING_ENABLED
if (conn->debugFile) {
dumpPacket(conn->debugFile, &packet, "RCVD");
fprintf(conn->debugFile, "\n");
fflush(conn->debugFile);
}
#endif
syslog(LOG_INFO,
"Session %d terminated -- received PADT from peer",
(int) ntohs(packet.session));
parsePacket(&packet, parseLogErrs, NULL);
sendPADT(conn, "Received PADT from peer");
exit(EXIT_SUCCESS);
}
#endif /* USE_BPF */
/**********************************************************************
*%FUNCTION: session
*%ARGUMENTS:
* conn -- PPPoE connection info
*%RETURNS:
* Nothing
*�SCRIPTION:
* Handles the "session" phase of PPPoE
***********************************************************************/
void
session(PPPoEConnection *conn)
{
fd_set readable;
PPPoEPacket packet;
struct timeval tv;
struct timeval *tvp = NULL;
int maxFD = 0;
int r;
/* Drop privileges */
dropPrivs();
/* Prepare for select() */
if (conn->sessionSocket > maxFD) maxFD = conn->sessionSocket;
if (conn->discoverySocket > maxFD) maxFD = conn->discoverySocket;
maxFD++;
/* Fill in the constant fields of the packet to save time */
memcpy(packet.ethHdr.h_dest, conn->peerEth, ETH_ALEN);
memcpy(packet.ethHdr.h_source, conn->myEth, ETH_ALEN);
packet.ethHdr.h_proto = htons(Eth_PPPOE_Session);
packet.vertype = PPPOE_VER_TYPE(1, 1);
packet.code = CODE_SESS;
packet.session = conn->session;
initPPP();
#ifdef USE_BPF
/* check for buffered session data */
while (BPF_BUFFER_HAS_DATA) {
if (conn->synchronous) {
syncReadFromEth(conn, conn->sessionSocket, optClampMSS);
} else {
asyncReadFromEth(conn, conn->sessionSocket, optClampMSS);
}
}
#endif
for (;;) {
if (optInactivityTimeout > 0) {
tv.tv_sec = optInactivityTimeout;
tv.tv_usec = 0;
tvp = &tv;
}
FD_ZERO(&readable);
FD_SET(0, &readable); /* ppp packets come from stdin */
if (conn->discoverySocket >= 0) {
FD_SET(conn->discoverySocket, &readable);
}
FD_SET(conn->sessionSocket, &readable);
while(1) {
r = select(maxFD, &readable, NULL, NULL, tvp);
if (r >= 0 || errno != EINTR) break;
}
if (r < 0) {
fatalSys("select (session)");
}
if (r == 0) { /* Inactivity timeout */
syslog(LOG_ERR, "Inactivity timeout... something wicked happened on session %d",
(int) ntohs(conn->session));
sendPADT(conn, "RP-PPPoE: Inactivity timeout");
exit(EXIT_FAILURE);
}
/* Handle ready sockets */
if (FD_ISSET(0, &readable)) {
if (conn->synchronous) {
syncReadFromPPP(conn, &packet);
} else {
asyncReadFromPPP(conn, &packet);
}
}
if (FD_ISSET(conn->sessionSocket, &readable)) {
do {
if (conn->synchronous) {
syncReadFromEth(conn, conn->sessionSocket, optClampMSS);
} else {
asyncReadFromEth(conn, conn->sessionSocket, optClampMSS);
}
} while (BPF_BUFFER_HAS_DATA);
}
#ifndef USE_BPF
/* BSD uses a single socket, see *syncReadFromEth() */
/* for calls to sessionDiscoveryPacket() */
if (conn->discoverySocket >= 0) {
if (FD_ISSET(conn->discoverySocket, &readable)) {
sessionDiscoveryPacket(conn);
}
}
#endif
}
}
/***********************************************************************
*%FUNCTION: sigPADT
*%ARGUMENTS:
* src -- signal received
*%RETURNS:
* Nothing
*�SCRIPTION:
* If an established session exists send PADT to terminate from session
* from our end
***********************************************************************/
static void
sigPADT(int src)
{
syslog(LOG_DEBUG,"Received signal %d on session %d.",
(int)src, (int) ntohs(Connection->session));
sendPADTf(Connection, "RP-PPPoE: Received signal %d", src);
exit(EXIT_SUCCESS);
}
/**********************************************************************
*%FUNCTION: usage
*%ARGUMENTS:
* argv0 -- program name
*%RETURNS:
* Nothing
*�SCRIPTION:
* Prints usage information and exits.
***********************************************************************/
void
usage(char const *argv0)
{
fprintf(stderr, "Usage: %s [options]\n", argv0);
fprintf(stderr, "Options:\n");
#ifdef USE_BPF
fprintf(stderr, " -I if_name -- Specify interface (REQUIRED)\n");
#else
fprintf(stderr, " -I if_name -- Specify interface (default %s.)\n",
DEFAULT_IF);
#endif
#ifdef DEBUGGING_ENABLED
fprintf(stderr, " -D filename -- Log debugging information in filename.\n");
#endif
fprintf(stderr,
" -T timeout -- Specify inactivity timeout in seconds.\n"
" -t timeout -- Initial timeout for discovery packets in seconds\n"
" -V -- Print version and exit.\n"
" -A -- Print access concentrator names and exit.\n"
" -S name -- Set desired service name.\n"
" -C name -- Set desired access concentrator name.\n"
" -U -- Use Host-Unique to allow multiple PPPoE sessions.\n"
" -W value -- Use Host-Unique set to 'value' specifically.\n"
" -s -- Use synchronous PPP encapsulation.\n"
" -m MSS -- Clamp incoming and outgoing MSS options.\n"
" -p pidfile -- Write process-ID to pidfile.\n"
" -e sess:mac -- Skip discovery phase; use existing session.\n"
" -n -- Do not open discovery socket.\n"
" -k -- Kill a session with PADT (requires -e)\n"
" -d -- Perform discovery, print session info and exit.\n"
" -f disc:sess -- Set Ethernet frame types (hex).\n"
" -H XX:XX:XX:XX:XX:XX -- Force Hardware Address (hex).\n"
" -h -- Print usage information.\n\n"
"RP-PPPoE Version %s, Copyright (C) 2001-2018 Roaring Penguin Software Inc.\n"
" %*s Copyright (C) 2018-2021 Dianne Skoll\n"
"RP-PPPoE comes with ABSOLUTELY NO WARRANTY.\n"
"This is free software, and you are welcome to redistribute it under the terms\n"
"of the GNU General Public License, version 2 or any later version.\n"
"https://dianne.skoll.ca/projects/rp-pppoe/\n", RP_VERSION, (int) strlen(RP_VERSION), "");
exit(EXIT_SUCCESS);
}
/**********************************************************************
*%FUNCTION: main
*%ARGUMENTS:
* argc, argv -- count and values of command-line arguments
*%RETURNS:
* Nothing
*�SCRIPTION:
* Main program
***********************************************************************/
int
main(int argc, char *argv[])
{
int opt;
int n;
unsigned int m[6]; /* MAC address in -e option */
unsigned int s; /* Temporary to hold session */
FILE *pidfile;
unsigned int discoveryType, sessionType;
char const *options;
PPPoEConnection conn;
#ifdef HAVE_N_HDLC
int disc = N_HDLC;
long flags;
#endif
if (getuid() != geteuid() ||
getgid() != getegid()) {
IsSetID = 1;
}
/* Initialize connection info */
memset(&conn, 0, sizeof(conn));
conn.discoverySocket = -1;
conn.sessionSocket = -1;
conn.discoveryTimeout = PADI_TIMEOUT;
/* For signal handler */
Connection = &conn;
/* Initialize syslog */
openlog("pppoe", LOG_PID, LOG_DAEMON);
#ifdef DEBUGGING_ENABLED
options = "I:VAT:D:hS:C:UW:sm:np:e:kdf:F:t:H:";
#else
options = "I:VAT:hS:C:UW:sm:np:e:kdf:F:t:H:";
#endif
while((opt = getopt(argc, argv, options)) != -1) {
switch(opt) {
case 't':
if (sscanf(optarg, "%d", &conn.discoveryTimeout) != 1) {
fprintf(stderr, "Illegal argument to -t: Should be -t timeout\n");
exit(EXIT_FAILURE);
}
if (conn.discoveryTimeout < 1) {
conn.discoveryTimeout = 1;
}
break;
case 'F':
if (sscanf(optarg, "%d", &optFloodDiscovery) != 1) {
fprintf(stderr, "Illegal argument to -F: Should be -F numFloods\n");
exit(EXIT_FAILURE);
}
if (optFloodDiscovery < 1) optFloodDiscovery = 1;
fprintf(stderr,
"WARNING: DISCOVERY FLOOD IS MEANT FOR STRESS-TESTING\n"
"A PPPOE SERVER WHICH YOU OWN. DO NOT USE IT AGAINST\n"
"A REAL ISP. YOU HAVE 5 SECONDS TO ABORT.\n");
sleep(5);
break;
case 'f':
if (sscanf(optarg, "%x:%x", &discoveryType, &sessionType) != 2) {
fprintf(stderr, "Illegal argument to -f: Should be disc:sess in hex\n");
exit(EXIT_FAILURE);
}
Eth_PPPOE_Discovery = (UINT16_t) discoveryType;
Eth_PPPOE_Session = (UINT16_t) sessionType;
break;
case 'H':
if (sscanf(optarg, "%2x:%2x:%2x:%2x:%2x:%2x",
&m[0], &m[1], &m[2], &m[3], &m[4], &m[5])!= 6) {
fprintf(stderr,
"Illegal argument to -H: Should be"
"XX:XX:XX:XX:XX:XX in hex\n");
exit(EXIT_FAILURE);
}
for (n=0; n<6; n++) {
conn.myEth[n] = (unsigned char) m[n];
}
break;
case 'd':
optSkipSession = 1;
break;
case 'k':
conn.killSession = 1;
break;
case 'n':
/* Do not even open a discovery socket -- used when invoked
by pppoe-server */
conn.noDiscoverySocket = 1;
break;
case 'e':
/* Existing session: "sess:xx:yy:zz:aa:bb:cc" where "sess" is
session-ID, and xx:yy:zz:aa:bb:cc is MAC-address of peer */
n = sscanf(optarg, "%u:%2x:%2x:%2x:%2x:%2x:%2x",
&s, &m[0], &m[1], &m[2], &m[3], &m[4], &m[5]);
if (n != 7) {
fprintf(stderr, "Illegal argument to -e: Should be sess:xx:yy:zz:aa:bb:cc\n");
exit(EXIT_FAILURE);
}
/* Copy MAC address of peer */
for (n=0; n<6; n++) {
conn.peerEth[n] = (unsigned char) m[n];
}
/* Convert session */
conn.session = htons(s);
/* Skip discovery phase! */
conn.skipDiscovery = 1;
break;
case 'p':
switchToRealID();
pidfile = fopen(optarg, "w");
if (pidfile) {
fprintf(pidfile, "%lu\n", (unsigned long) getpid());
fclose(pidfile);
}
switchToEffectiveID();
break;
case 'S':
SET_STRING(conn.serviceName, optarg);
break;
case 'C':
SET_STRING(conn.acName, optarg);
break;
case 's':
conn.synchronous = 1;
break;
case 'U':
if (conn.hostUniq) {
fprintf(stderr, "-U and -W are mutually-exclusive and may only be used once.\n");
exit(EXIT_FAILURE);
}
/* Allows for a 64-bit PID */
conn.hostUniq = malloc(17);
if (!conn.hostUniq) {
fprintf(stderr, "Out of memory.\n");
exit(EXIT_FAILURE);
}
sprintf(conn.hostUniq, "%lx", (unsigned long) getpid());
break;
case 'W':
if (conn.hostUniq) {
fprintf(stderr, "-U and -W are mutually-exclusive and may only be used once.\n");
exit(EXIT_FAILURE);
}
SET_STRING(conn.hostUniq, optarg);
break;
#ifdef DEBUGGING_ENABLED
case 'D':
switchToRealID();
conn.debugFile = fopen(optarg, "w");
switchToEffectiveID();
if (!conn.debugFile) {
fprintf(stderr, "Could not open %s: %s\n",
optarg, strerror(errno));
exit(EXIT_FAILURE);
}
fprintf(conn.debugFile, "rp-pppoe-%s\n", RP_VERSION);
fflush(conn.debugFile);
break;
#endif
case 'T':
optInactivityTimeout = (int) strtol(optarg, NULL, 10);
if (optInactivityTimeout < 0) {
optInactivityTimeout = 0;
}
break;
case 'm':
optClampMSS = (int) strtol(optarg, NULL, 10);
if (optClampMSS < 536) {
fprintf(stderr, "-m: %d is too low (min 536)\n", optClampMSS);
exit(EXIT_FAILURE);
}
if (optClampMSS > 1452) {
fprintf(stderr, "-m: %d is too high (max 1452)\n", optClampMSS);
exit(EXIT_FAILURE);
}
break;
case 'I':
SET_STRING(conn.ifName, optarg);
break;
case 'V':
printf("RP-PPPoE Version %s\n", RP_VERSION);
exit(EXIT_SUCCESS);
case 'A':
conn.printACNames = 1;
break;
case 'h':
usage(argv[0]);
break;
default:
usage(argv[0]);
}
}
/* Pick a default interface name */
if (!conn.ifName) {
#ifdef USE_BPF
fprintf(stderr, "No interface specified (-I option)\n");
exit(EXIT_FAILURE);
#else
SET_STRING(conn.ifName, DEFAULT_IF);
#endif
}
#ifdef AUTO_IFUP
/* Create a channel to the NET kernel. */
if ((skfd = sockets_open(0)) < 0) {
perror("socket");
exit(1);
}
set_flag(conn.ifName, (IFF_UP | IFF_RUNNING));
#endif
if (!conn.printACNames) {
#ifdef HAVE_N_HDLC
if (conn.synchronous) {
if (ioctl(0, TIOCSETD, &disc) < 0) {
printErr("Unable to set line discipline to N_HDLC. Make sure your kernel supports the N_HDLC line discipline, or do not use the SYNCHRONOUS option. Quitting.");
exit(EXIT_FAILURE);
} else {
syslog(LOG_INFO,
"Changed pty line discipline to N_HDLC for synchronous mode");
}
/* There is a bug in Linux's select which returns a descriptor
* as readable if N_HDLC line discipline is on, even if
* it isn't really readable. This return happens only when
* select() times out. To avoid blocking forever in read(),
* make descriptor 0 non-blocking */
flags = fcntl(0, F_GETFL);
if (flags < 0) fatalSys("fcntl(F_GETFL)");
if (fcntl(0, F_SETFL, (long) flags | O_NONBLOCK) < 0) {
fatalSys("fcntl(F_SETFL)");
}
}
#endif
}
if (optFloodDiscovery) {
for (n=0; n < optFloodDiscovery; n++) {
if (conn.printACNames) {
fprintf(stderr, "Sending discovery flood %d\n", n+1);
}
conn.discoverySocket =
openInterface(conn.ifName, Eth_PPPOE_Discovery, conn.myEth, NULL);
discovery(&conn);
conn.discoveryState = STATE_SENT_PADI;
close(conn.discoverySocket);
}
exit(EXIT_SUCCESS);
}
/* Open session socket before discovery phase, to avoid losing session */
/* packets sent by peer just after PADS packet (noted on some Cisco */
/* server equipment). */
/* Opening this socket just before waitForPADS in the discovery() */
/* function would be more appropriate, but it would mess-up the code */
if (!optSkipSession)
conn.sessionSocket = openInterface(conn.ifName, Eth_PPPOE_Session, conn.myEth, NULL);
/* Skip discovery and don't open discovery socket? */
if (conn.skipDiscovery && conn.noDiscoverySocket) {
conn.discoveryState = STATE_SESSION;
} else {
conn.discoverySocket =
openInterface(conn.ifName, Eth_PPPOE_Discovery, conn.myEth, NULL);
discovery(&conn);
}
if (optSkipSession) {
printf("%u:x:x:x:x:x:x\n",
ntohs(conn.session),
conn.peerEth[0],
conn.peerEth[1],
conn.peerEth[2],
conn.peerEth[3],
conn.peerEth[4],
conn.peerEth[5]);
exit(EXIT_SUCCESS);
}
/* Set signal handlers: send PADT on HUP, TERM and INT */
signal(SIGTERM, sigPADT);
signal(SIGINT, sigPADT);
signal(SIGHUP, sigPADT);
session(&conn);
return 0;
}
/**********************************************************************
*%FUNCTION: fatalSys
*%ARGUMENTS:
* str -- error message
*%RETURNS:
* Nothing
*�SCRIPTION:
* Prints a message plus the errno value to stderr and syslog and exits.
***********************************************************************/
void
fatalSys(char const *str)
{
char buf[1024];
sprintf(buf, "%.256s: Session %d: %.256s",
str, (int) ntohs(Connection->session), strerror(errno));
printErr(buf);
sendPADTf(Connection, "RP-PPPoE: System call error: %s",
strerror(errno));
exit(EXIT_FAILURE);
}
/**********************************************************************
*%FUNCTION: sysErr
*%ARGUMENTS:
* str -- error message
*%RETURNS:
* Nothing
*�SCRIPTION:
* Prints a message plus the errno value to syslog.
***********************************************************************/
void
sysErr(char const *str)
{
char buf[1024];
sprintf(buf, "%.256s: %.256s", str, strerror(errno));
printErr(buf);
}
/**********************************************************************
*%FUNCTION: rp_fatal
*%ARGUMENTS:
* str -- error message
*%RETURNS:
* Nothing
*�SCRIPTION:
* Prints a message to stderr and syslog and exits.
***********************************************************************/
void
rp_fatal(char const *str)
{
printErr(str);
sendPADTf(Connection, "RP-PPPoE: Session %d: %.256s",
(int) ntohs(Connection->session), str);
exit(EXIT_FAILURE);
}
/**********************************************************************
*%FUNCTION: asyncReadFromEth
*%ARGUMENTS:
* conn -- PPPoE connection info
* sock -- Ethernet socket
* clampMss -- if non-zero, do MSS-clamping
*%RETURNS:
* Nothing
*�SCRIPTION:
* Reads a packet from the Ethernet interface and sends it to async PPP
* device.
***********************************************************************/
void
asyncReadFromEth(PPPoEConnection *conn, int sock, int clampMss)
{
PPPoEPacket packet;
int len;
int plen;
int i;
unsigned char pppBuf[4096];
unsigned char *ptr = pppBuf;
unsigned char c;
UINT16_t fcs;
unsigned char header[2] = {FRAME_ADDR, FRAME_CTRL};
unsigned char tail[2];
#ifdef USE_BPF
int type;
#endif
if (receivePacket(sock, &packet, &len) < 0) {
return;
}
/* Check length */
if (ntohs(packet.length) + HDR_SIZE > len) {
syslog(LOG_ERR, "Bogus PPPoE length field (%u)",
(unsigned int) ntohs(packet.length));
return;
}
#ifdef DEBUGGING_ENABLED
if (conn->debugFile) {
dumpPacket(conn->debugFile, &packet, "RCVD");
fprintf(conn->debugFile, "\n");
fflush(conn->debugFile);
}
#endif
#ifdef USE_BPF
/* Make sure this is a session packet before processing further */
type = etherType(&packet);
if (type == Eth_PPPOE_Discovery) {
sessionDiscoveryPacket(&packet);
} else if (type != Eth_PPPOE_Session) {
return;
}
#endif
/* Sanity check */
if (packet.code != CODE_SESS) {
syslog(LOG_ERR, "Unexpected packet code %d", (int) packet.code);
return;
}
if (PPPOE_VER(packet.vertype) != 1) {
syslog(LOG_ERR, "Unexpected packet version %d", PPPOE_VER(packet.vertype));
return;
}
if (PPPOE_TYPE(packet.vertype) != 1) {
syslog(LOG_ERR, "Unexpected packet type %d", PPPOE_TYPE(packet.vertype));
return;
}
if (memcmp(packet.ethHdr.h_dest, conn->myEth, ETH_ALEN)) {
return;
}
if (memcmp(packet.ethHdr.h_source, conn->peerEth, ETH_ALEN)) {
/* Not for us -- must be another session. This is not an error,
so don't log anything. */
return;
}
if (packet.session != conn->session) {
/* Not for us -- must be another session. This is not an error,
so don't log anything. */
return;
}
plen = ntohs(packet.length);
if (plen + HDR_SIZE > len) {
syslog(LOG_ERR, "Bogus length field in session packet %d (%d)",
(int) plen, (int) len);
return;
}
/* Clamp MSS */
if (clampMss) {
clampMSS(&packet, "incoming", clampMss);
}
/* Compute FCS */
fcs = pppFCS16(PPPINITFCS16, header, 2);
fcs = pppFCS16(fcs, packet.payload, plen) ^ 0xffff;
tail[0] = fcs & 0x00ff;
tail[1] = (fcs >> 8) & 0x00ff;
/* Build a buffer to send to PPP */
*ptr++ = FRAME_FLAG;
*ptr++ = FRAME_ADDR;
*ptr++ = FRAME_ESC;
*ptr++ = FRAME_CTRL ^ FRAME_ENC;
for (i=0; i<plen; i++) {
c = packet.payload[i];
if (c == FRAME_FLAG || c == FRAME_ADDR || c == FRAME_ESC || c < 0x20) {
*ptr++ = FRAME_ESC;
*ptr++ = c ^ FRAME_ENC;
} else {
*ptr++ = c;
}
}
for (i=0; i<2; i++) {
c = tail[i];
if (c == FRAME_FLAG || c == FRAME_ADDR || c == FRAME_ESC || c < 0x20) {
*ptr++ = FRAME_ESC;
*ptr++ = c ^ FRAME_ENC;
} else {
*ptr++ = c;
}
}
*ptr++ = FRAME_FLAG;
/* Ship it out */
if (write(1, pppBuf, (ptr-pppBuf)) < 0) {
fatalSys("asyncReadFromEth: write");
}
}
/**********************************************************************
*%FUNCTION: syncReadFromEth
*%ARGUMENTS:
* conn -- PPPoE connection info
* sock -- Ethernet socket
* clampMss -- if true, clamp MSS.
*%RETURNS:
* Nothing
*�SCRIPTION:
* Reads a packet from the Ethernet interface and sends it to sync PPP
* device.
***********************************************************************/
void
syncReadFromEth(PPPoEConnection *conn, int sock, int clampMss)
{
PPPoEPacket packet;
int len;
int plen;
struct iovec vec[2];
unsigned char dummy[2];
#ifdef USE_BPF
int type;
#endif
if (receivePacket(sock, &packet, &len) < 0) {
return;
}
/* Check length */
if (ntohs(packet.length) + HDR_SIZE > len) {
syslog(LOG_ERR, "Bogus PPPoE length field (%u)",
(unsigned int) ntohs(packet.length));
return;
}
#ifdef DEBUGGING_ENABLED
if (conn->debugFile) {
dumpPacket(conn->debugFile, &packet, "RCVD");
fprintf(conn->debugFile, "\n");
fflush(conn->debugFile);
}
#endif
#ifdef USE_BPF
/* Make sure this is a session packet before processing further */
type = etherType(&packet);
if (type == Eth_PPPOE_Discovery) {
sessionDiscoveryPacket(&packet);
} else if (type != Eth_PPPOE_Session) {
return;
}
#endif
/* Sanity check */
if (packet.code != CODE_SESS) {
syslog(LOG_ERR, "Unexpected packet code %d", (int) packet.code);
return;
}
if (PPPOE_VER(packet.vertype) != 1) {
syslog(LOG_ERR, "Unexpected packet version %d", PPPOE_VER(packet.vertype));
return;
}
if (PPPOE_TYPE(packet.vertype) != 1) {
syslog(LOG_ERR, "Unexpected packet type %d", PPPOE_TYPE(packet.vertype));
return;
}
if (memcmp(packet.ethHdr.h_dest, conn->myEth, ETH_ALEN)) {
/* Not for us -- must be another session. This is not an error,
so don't log anything. */
return;
}
if (memcmp(packet.ethHdr.h_source, conn->peerEth, ETH_ALEN)) {
/* Not for us -- must be another session. This is not an error,
so don't log anything. */
return;
}
if (packet.session != conn->session) {
/* Not for us -- must be another session. This is not an error,
so don't log anything. */
return;
}
plen = ntohs(packet.length);
if (plen + HDR_SIZE > len) {
syslog(LOG_ERR, "Bogus length field in session packet %d (%d)",
(int) plen, (int) len);
return;
}
/* Clamp MSS */
if (clampMss) {
clampMSS(&packet, "incoming", clampMss);
}
/* Ship it out */
vec[0].iov_base = (void *) dummy;
dummy[0] = FRAME_ADDR;
dummy[1] = FRAME_CTRL;
vec[0].iov_len = 2;
vec[1].iov_base = (void *) packet.payload;
vec[1].iov_len = plen;
if (writev(1, vec, 2) < 0) {
fatalSys("syncReadFromEth: write");
}
}
|