2 * Perform PPPoE discovery
4 * Copyright (C) 2000-2001 by Roaring Penguin Software Inc.
5 * Copyright (C) 2004 Marco d'Itri <md@linux.it>
7 * This program may be distributed according to the terms of the GNU
8 * General Public License, version 2 or (at your option) any later version.
25 #ifdef HAVE_NETPACKET_PACKET_H
26 #include <netpacket/packet.h>
27 #elif defined(HAVE_LINUX_IF_PACKET_H)
28 #include <linux/if_packet.h>
31 #ifdef HAVE_NET_ETHERNET_H
32 #include <net/ethernet.h>
35 #ifdef HAVE_ASM_TYPES_H
36 #include <asm/types.h>
39 #ifdef HAVE_SYS_IOCTL_H
40 #include <sys/ioctl.h>
47 #ifdef HAVE_NET_IF_ARP_H
48 #include <net/if_arp.h>
51 char *xstrdup(const char *s);
59 void error(char *fmt, ...)
63 vfprintf(stderr, fmt, pvar);
67 /* Initialize frame types to RFC 2516 values. Some broken peers apparently
68 use different frame types... sigh... */
70 UINT16_t Eth_PPPOE_Discovery = ETH_PPPOE_DISCOVERY;
71 UINT16_t Eth_PPPOE_Session = ETH_PPPOE_SESSION;
73 /**********************************************************************
76 * packet -- a received PPPoE packet
78 * ethernet packet type (see /usr/include/net/ethertypes.h)
80 * Checks the ethernet packet header to determine its type.
81 * We should only be receveing DISCOVERY and SESSION types if the BPF
82 * is set up correctly. Logs an error if an unexpected type is received.
83 * Note that the ethernet type names come from "pppoe.h" and the packet
84 * packet structure names use the LINUX dialect to maintain consistency
85 * with the rest of this file. See the BSD section of "pppoe.h" for
86 * translations of the data structure names.
87 ***********************************************************************/
89 etherType(PPPoEPacket *packet)
91 UINT16_t type = (UINT16_t) ntohs(packet->ethHdr.h_proto);
92 if (type != Eth_PPPOE_Discovery && type != Eth_PPPOE_Session) {
93 fprintf(stderr, "Invalid ether type 0x%x\n", type);
98 /**********************************************************************
99 *%FUNCTION: openInterface
101 * ifname -- name of interface
102 * type -- Ethernet frame type
103 * hwaddr -- if non-NULL, set to the hardware address
105 * A raw socket for talking to the Ethernet card. Exits on error.
107 * Opens a raw Ethernet socket
108 ***********************************************************************/
110 openInterface(char const *ifname, UINT16_t type, unsigned char *hwaddr)
117 #ifdef HAVE_STRUCT_SOCKADDR_LL
118 struct sockaddr_ll sa;
123 memset(&sa, 0, sizeof(sa));
125 #ifdef HAVE_STRUCT_SOCKADDR_LL
133 if ((fd = socket(domain, stype, htons(type))) < 0) {
134 /* Give a more helpful message for the common error case */
135 if (errno == EPERM) {
136 rp_fatal("Cannot create raw socket -- pppoe must be run as root.");
141 if (setsockopt(fd, SOL_SOCKET, SO_BROADCAST, &optval, sizeof(optval)) < 0) {
142 fatalSys("setsockopt");
145 /* Fill in hardware address */
147 strncpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
148 if (ioctl(fd, SIOCGIFHWADDR, &ifr) < 0) {
149 fatalSys("ioctl(SIOCGIFHWADDR)");
151 memcpy(hwaddr, ifr.ifr_hwaddr.sa_data, ETH_ALEN);
153 if (ifr.ifr_hwaddr.sa_family != ARPHRD_ETHER) {
155 sprintf(buffer, "Interface %.16s is not Ethernet", ifname);
159 if (NOT_UNICAST(hwaddr)) {
162 "Interface %.16s has broadcast/multicast MAC address??",
168 /* Sanity check on MTU */
169 strncpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
170 if (ioctl(fd, SIOCGIFMTU, &ifr) < 0) {
171 fatalSys("ioctl(SIOCGIFMTU)");
173 if (ifr.ifr_mtu < ETH_DATA_LEN) {
174 fprintf(stderr, "Interface %.16s has MTU of %d -- should be %d.\n",
175 ifname, ifr.ifr_mtu, ETH_DATA_LEN);
176 fprintf(stderr, "You may have serious connection problems.\n");
179 #ifdef HAVE_STRUCT_SOCKADDR_LL
180 /* Get interface index */
181 sa.sll_family = AF_PACKET;
182 sa.sll_protocol = htons(type);
184 strncpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
185 if (ioctl(fd, SIOCGIFINDEX, &ifr) < 0) {
186 fatalSys("ioctl(SIOCFIGINDEX): Could not get interface index");
188 sa.sll_ifindex = ifr.ifr_ifindex;
191 strcpy(sa.sa_data, ifname);
194 /* We're only interested in packets on specified interface */
195 if (bind(fd, (struct sockaddr *) &sa, sizeof(sa)) < 0) {
203 /***********************************************************************
204 *%FUNCTION: sendPacket
206 * sock -- socket to send to
207 * pkt -- the packet to transmit
208 * size -- size of packet (in bytes)
210 * 0 on success; -1 on failure
213 ***********************************************************************/
215 sendPacket(PPPoEConnection *conn, int sock, PPPoEPacket *pkt, int size)
217 #if defined(HAVE_STRUCT_SOCKADDR_LL)
218 if (send(sock, pkt, size, 0) < 0) {
219 sysErr("send (sendPacket)");
226 rp_fatal("relay and server not supported on Linux 2.0 kernels");
228 strcpy(sa.sa_data, conn->ifName);
229 if (sendto(sock, pkt, size, 0, &sa, sizeof(sa)) < 0) {
230 sysErr("sendto (sendPacket)");
237 /***********************************************************************
238 *%FUNCTION: receivePacket
240 * sock -- socket to read from
241 * pkt -- place to store the received packet
242 * size -- set to size of packet in bytes
244 * >= 0 if all OK; < 0 if error
247 ***********************************************************************/
249 receivePacket(int sock, PPPoEPacket *pkt, int *size)
251 if ((*size = recv(sock, pkt, sizeof(PPPoEPacket), 0)) < 0) {
252 sysErr("recv (receivePacket)");
258 /**********************************************************************
259 *%FUNCTION: parsePacket
261 * packet -- the PPPoE discovery packet to parse
262 * func -- function called for each tag in the packet
263 * extra -- an opaque data pointer supplied to parsing function
265 * 0 if everything went well; -1 if there was an error
267 * Parses a PPPoE discovery packet, calling "func" for each tag in the packet.
268 * "func" is passed the additional argument "extra".
269 ***********************************************************************/
271 parsePacket(PPPoEPacket *packet, ParseFunc *func, void *extra)
273 UINT16_t len = ntohs(packet->length);
274 unsigned char *curTag;
275 UINT16_t tagType, tagLen;
277 if (PPPOE_VER(packet->vertype) != 1) {
278 fprintf(stderr, "Invalid PPPoE version (%d)\n",
279 PPPOE_VER(packet->vertype));
282 if (PPPOE_TYPE(packet->vertype) != 1) {
283 fprintf(stderr, "Invalid PPPoE type (%d)\n",
284 PPPOE_TYPE(packet->vertype));
288 /* Do some sanity checks on packet */
289 if (len > ETH_JUMBO_LEN - PPPOE_OVERHEAD) { /* 6-byte overhead for PPPoE header */
290 fprintf(stderr, "Invalid PPPoE packet length (%u)\n", len);
294 /* Step through the tags */
295 curTag = packet->payload;
296 while(curTag - packet->payload < len) {
297 /* Alignment is not guaranteed, so do this by hand... */
298 tagType = (curTag[0] << 8) + curTag[1];
299 tagLen = (curTag[2] << 8) + curTag[3];
300 if (tagType == TAG_END_OF_LIST) {
303 if ((curTag - packet->payload) + tagLen + TAG_HDR_SIZE > len) {
304 fprintf(stderr, "Invalid PPPoE tag length (%u)\n", tagLen);
307 func(tagType, tagLen, curTag+TAG_HDR_SIZE, extra);
308 curTag = curTag + TAG_HDR_SIZE + tagLen;
313 /**********************************************************************
314 *%FUNCTION: parseForHostUniq
319 * extra -- user-supplied pointer. This is assumed to be a pointer to int.
323 * If a HostUnique tag is found which matches our PID, sets *extra to 1.
324 ***********************************************************************/
326 parseForHostUniq(UINT16_t type, UINT16_t len, unsigned char *data,
329 int *val = (int *) extra;
330 if (type == TAG_HOST_UNIQ && len == sizeof(pid_t)) {
332 memcpy(&tmp, data, len);
333 if (tmp == getpid()) {
339 /**********************************************************************
340 *%FUNCTION: packetIsForMe
342 * conn -- PPPoE connection info
343 * packet -- a received PPPoE packet
345 * 1 if packet is for this PPPoE daemon; 0 otherwise.
347 * If we are using the Host-Unique tag, verifies that packet contains
348 * our unique identifier.
349 ***********************************************************************/
351 packetIsForMe(PPPoEConnection *conn, PPPoEPacket *packet)
355 /* If packet is not directed to our MAC address, forget it */
356 if (memcmp(packet->ethHdr.h_dest, conn->myEth, ETH_ALEN)) return 0;
358 /* If we're not using the Host-Unique tag, then accept the packet */
359 if (!conn->useHostUniq) return 1;
361 parsePacket(packet, parseForHostUniq, &forMe);
365 /**********************************************************************
366 *%FUNCTION: parsePADOTags
371 * extra -- extra user data. Should point to a PacketCriteria structure
372 * which gets filled in according to selected AC name and service
377 * Picks interesting tags out of a PADO packet
378 ***********************************************************************/
380 parsePADOTags(UINT16_t type, UINT16_t len, unsigned char *data,
383 struct PacketCriteria *pc = (struct PacketCriteria *) extra;
384 PPPoEConnection *conn = pc->conn;
390 if (conn->printACNames) {
391 printf("Access-Concentrator: %.*s\n", (int) len, data);
393 if (conn->acName && len == strlen(conn->acName) &&
394 !strncmp((char *) data, conn->acName, len)) {
398 case TAG_SERVICE_NAME:
399 pc->seenServiceName = 1;
400 if (conn->printACNames && len > 0) {
401 printf(" Service-Name: %.*s\n", (int) len, data);
403 if (conn->serviceName && len == strlen(conn->serviceName) &&
404 !strncmp((char *) data, conn->serviceName, len)) {
405 pc->serviceNameOK = 1;
409 if (conn->printACNames) {
410 printf("Got a cookie:");
411 /* Print first 20 bytes of cookie */
412 for (i=0; i<len && i < 20; i++) {
413 printf(" %02x", (unsigned) data[i]);
415 if (i < len) printf("...");
418 conn->cookie.type = htons(type);
419 conn->cookie.length = htons(len);
420 memcpy(conn->cookie.payload, data, len);
422 case TAG_RELAY_SESSION_ID:
423 if (conn->printACNames) {
424 printf("Got a Relay-ID:");
425 /* Print first 20 bytes of relay ID */
426 for (i=0; i<len && i < 20; i++) {
427 printf(" %02x", (unsigned) data[i]);
429 if (i < len) printf("...");
432 conn->relayId.type = htons(type);
433 conn->relayId.length = htons(len);
434 memcpy(conn->relayId.payload, data, len);
436 case TAG_SERVICE_NAME_ERROR:
437 if (conn->printACNames) {
438 printf("Got a Service-Name-Error tag: %.*s\n", (int) len, data);
441 case TAG_AC_SYSTEM_ERROR:
442 if (conn->printACNames) {
443 printf("Got a System-Error tag: %.*s\n", (int) len, data);
446 case TAG_GENERIC_ERROR:
447 if (conn->printACNames) {
448 printf("Got a Generic-Error tag: %.*s\n", (int) len, data);
454 /***********************************************************************
457 * conn -- PPPoEConnection structure
461 * Sends a PADI packet
462 ***********************************************************************/
464 sendPADI(PPPoEConnection *conn)
467 unsigned char *cursor = packet.payload;
468 PPPoETag *svc = (PPPoETag *) (&packet.payload);
469 UINT16_t namelen = 0;
472 if (conn->serviceName) {
473 namelen = (UINT16_t) strlen(conn->serviceName);
475 plen = TAG_HDR_SIZE + namelen;
476 CHECK_ROOM(cursor, packet.payload, plen);
478 /* Set destination to Ethernet broadcast address */
479 memset(packet.ethHdr.h_dest, 0xFF, ETH_ALEN);
480 memcpy(packet.ethHdr.h_source, conn->myEth, ETH_ALEN);
482 packet.ethHdr.h_proto = htons(Eth_PPPOE_Discovery);
483 packet.vertype = PPPOE_VER_TYPE(1, 1);
484 packet.code = CODE_PADI;
487 svc->type = TAG_SERVICE_NAME;
488 svc->length = htons(namelen);
489 CHECK_ROOM(cursor, packet.payload, namelen+TAG_HDR_SIZE);
491 if (conn->serviceName) {
492 memcpy(svc->payload, conn->serviceName, strlen(conn->serviceName));
494 cursor += namelen + TAG_HDR_SIZE;
496 /* If we're using Host-Uniq, copy it over */
497 if (conn->useHostUniq) {
499 pid_t pid = getpid();
500 hostUniq.type = htons(TAG_HOST_UNIQ);
501 hostUniq.length = htons(sizeof(pid));
502 memcpy(hostUniq.payload, &pid, sizeof(pid));
503 CHECK_ROOM(cursor, packet.payload, sizeof(pid) + TAG_HDR_SIZE);
504 memcpy(cursor, &hostUniq, sizeof(pid) + TAG_HDR_SIZE);
505 cursor += sizeof(pid) + TAG_HDR_SIZE;
506 plen += sizeof(pid) + TAG_HDR_SIZE;
509 packet.length = htons(plen);
511 sendPacket(conn, conn->discoverySocket, &packet, (int) (plen + HDR_SIZE));
512 if (conn->debugFile) {
513 dumpPacket(conn->debugFile, &packet, "SENT");
514 fprintf(conn->debugFile, "\n");
515 fflush(conn->debugFile);
519 /**********************************************************************
520 *%FUNCTION: waitForPADO
522 * conn -- PPPoEConnection structure
523 * timeout -- how long to wait (in seconds)
527 * Waits for a PADO packet and copies useful information
528 ***********************************************************************/
530 waitForPADO(PPPoEConnection *conn, int timeout)
538 struct PacketCriteria pc;
540 pc.acNameOK = (conn->acName) ? 0 : 1;
541 pc.serviceNameOK = (conn->serviceName) ? 0 : 1;
543 pc.seenServiceName = 0;
547 if (BPF_BUFFER_IS_EMPTY) {
552 FD_SET(conn->discoverySocket, &readable);
555 r = select(conn->discoverySocket+1, &readable, NULL, NULL, &tv);
556 if (r >= 0 || errno != EINTR) break;
559 perror("select (waitForPADO)");
562 if (r == 0) return; /* Timed out */
566 receivePacket(conn->discoverySocket, &packet, &len);
569 if (ntohs(packet.length) + HDR_SIZE > len) {
570 fprintf(stderr, "Bogus PPPoE length field (%u)\n",
571 (unsigned int) ntohs(packet.length));
576 /* If it's not a Discovery packet, loop again */
577 if (etherType(&packet) != Eth_PPPOE_Discovery) continue;
580 if (conn->debugFile) {
581 dumpPacket(conn->debugFile, &packet, "RCVD");
582 fprintf(conn->debugFile, "\n");
583 fflush(conn->debugFile);
585 /* If it's not for us, loop again */
586 if (!packetIsForMe(conn, &packet)) continue;
588 if (packet.code == CODE_PADO) {
589 if (BROADCAST(packet.ethHdr.h_source)) {
590 fprintf(stderr, "Ignoring PADO packet from broadcast MAC address\n");
593 parsePacket(&packet, parsePADOTags, &pc);
596 if (!pc.seenACName) {
597 fprintf(stderr, "Ignoring PADO packet with no AC-Name tag\n");
600 if (!pc.seenServiceName) {
601 fprintf(stderr, "Ignoring PADO packet with no Service-Name tag\n");
605 if (pc.acNameOK && pc.serviceNameOK) {
606 memcpy(conn->peerEth, packet.ethHdr.h_source, ETH_ALEN);
607 if (conn->printACNames) {
608 printf("AC-Ethernet-Address: %02x:%02x:%02x:%02x:%02x:%02x\n",
609 (unsigned) conn->peerEth[0],
610 (unsigned) conn->peerEth[1],
611 (unsigned) conn->peerEth[2],
612 (unsigned) conn->peerEth[3],
613 (unsigned) conn->peerEth[4],
614 (unsigned) conn->peerEth[5]);
615 printf("--------------------------------------------------\n");
618 conn->discoveryState = STATE_RECEIVED_PADO;
622 } while (conn->discoveryState != STATE_RECEIVED_PADO);
625 /**********************************************************************
626 *%FUNCTION: discovery
628 * conn -- PPPoE connection info structure
632 * Performs the PPPoE discovery phase
633 ***********************************************************************/
635 discovery(PPPoEConnection *conn)
637 int padiAttempts = 0;
638 int timeout = PADI_TIMEOUT;
640 conn->discoverySocket =
641 openInterface(conn->ifName, Eth_PPPOE_Discovery, conn->myEth);
645 if (padiAttempts > MAX_PADI_ATTEMPTS) {
646 fprintf(stderr, "Timeout waiting for PADO packets\n");
647 close(conn->discoverySocket);
648 conn->discoverySocket = -1;
652 conn->discoveryState = STATE_SENT_PADI;
653 waitForPADO(conn, timeout);
654 } while (!conn->numPADOs);
657 int main(int argc, char *argv[])
660 PPPoEConnection *conn;
662 conn = malloc(sizeof(PPPoEConnection));
666 memset(conn, 0, sizeof(PPPoEConnection));
668 conn->printACNames = 1;
670 while ((opt = getopt(argc, argv, "I:D:VUQS:C:h")) > 0) {
673 conn->serviceName = xstrdup(optarg);
676 conn->acName = xstrdup(optarg);
679 conn->useHostUniq = 1;
682 conn->debugFile = fopen(optarg, "w");
683 if (!conn->debugFile) {
684 fprintf(stderr, "Could not open %s: %s\n",
685 optarg, strerror(errno));
688 fprintf(conn->debugFile, "pppoe-discovery %s\n", RP_VERSION);
691 conn->ifName = xstrdup(optarg);
694 conn->printACNames = 0;
706 /* default interface name */
708 conn->ifName = strdup("eth0");
710 conn->discoverySocket = -1;
711 conn->sessionSocket = -1;
721 void rp_fatal(char const *str)
723 fprintf(stderr, "%s\n", str);
727 void fatalSys(char const *str)
733 void sysErr(char const *str)
738 char *xstrdup(const char *s)
740 register char *ret = strdup(s);
748 fprintf(stderr, "Usage: pppoe-discovery [options]\n");
749 fprintf(stderr, "Options:\n");
750 fprintf(stderr, " -I if_name -- Specify interface (default eth0)\n");
751 fprintf(stderr, " -D filename -- Log debugging information in filename.\n");
753 " -V -- Print version and exit.\n"
754 " -Q -- Quit Mode: Do not print access concentrator names\n"
755 " -S name -- Set desired service name.\n"
756 " -C name -- Set desired access concentrator name.\n"
757 " -U -- Use Host-Unique to allow multiple PPPoE sessions.\n"
758 " -h -- Print usage information.\n");
759 fprintf(stderr, "\nVersion " RP_VERSION "\n");