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.
24 #ifdef HAVE_NETPACKET_PACKET_H
25 #include <netpacket/packet.h>
26 #elif defined(HAVE_LINUX_IF_PACKET_H)
27 #include <linux/if_packet.h>
30 #ifdef HAVE_NET_ETHERNET_H
31 #include <net/ethernet.h>
34 #ifdef HAVE_ASM_TYPES_H
35 #include <asm/types.h>
38 #ifdef HAVE_SYS_IOCTL_H
39 #include <sys/ioctl.h>
46 #ifdef HAVE_NET_IF_ARP_H
47 #include <net/if_arp.h>
50 char *xstrdup(const char *s);
58 /* Initialize frame types to RFC 2516 values. Some broken peers apparently
59 use different frame types... sigh... */
61 UINT16_t Eth_PPPOE_Discovery = ETH_PPPOE_DISCOVERY;
62 UINT16_t Eth_PPPOE_Session = ETH_PPPOE_SESSION;
64 /**********************************************************************
67 * packet -- a received PPPoE packet
69 * ethernet packet type (see /usr/include/net/ethertypes.h)
71 * Checks the ethernet packet header to determine its type.
72 * We should only be receveing DISCOVERY and SESSION types if the BPF
73 * is set up correctly. Logs an error if an unexpected type is received.
74 * Note that the ethernet type names come from "pppoe.h" and the packet
75 * packet structure names use the LINUX dialect to maintain consistency
76 * with the rest of this file. See the BSD section of "pppoe.h" for
77 * translations of the data structure names.
78 ***********************************************************************/
80 etherType(PPPoEPacket *packet)
82 UINT16_t type = (UINT16_t) ntohs(packet->ethHdr.h_proto);
83 if (type != Eth_PPPOE_Discovery && type != Eth_PPPOE_Session) {
84 fprintf(stderr, "Invalid ether type 0x%x\n", type);
89 /**********************************************************************
90 *%FUNCTION: openInterface
92 * ifname -- name of interface
93 * type -- Ethernet frame type
94 * hwaddr -- if non-NULL, set to the hardware address
96 * A raw socket for talking to the Ethernet card. Exits on error.
98 * Opens a raw Ethernet socket
99 ***********************************************************************/
101 openInterface(char const *ifname, UINT16_t type, unsigned char *hwaddr)
108 #ifdef HAVE_STRUCT_SOCKADDR_LL
109 struct sockaddr_ll sa;
114 memset(&sa, 0, sizeof(sa));
116 #ifdef HAVE_STRUCT_SOCKADDR_LL
124 if ((fd = socket(domain, stype, htons(type))) < 0) {
125 /* Give a more helpful message for the common error case */
126 if (errno == EPERM) {
127 rp_fatal("Cannot create raw socket -- pppoe must be run as root.");
132 if (setsockopt(fd, SOL_SOCKET, SO_BROADCAST, &optval, sizeof(optval)) < 0) {
133 fatalSys("setsockopt");
136 /* Fill in hardware address */
138 strncpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
139 if (ioctl(fd, SIOCGIFHWADDR, &ifr) < 0) {
140 fatalSys("ioctl(SIOCGIFHWADDR)");
142 memcpy(hwaddr, ifr.ifr_hwaddr.sa_data, ETH_ALEN);
144 if (ifr.ifr_hwaddr.sa_family != ARPHRD_ETHER) {
146 sprintf(buffer, "Interface %.16s is not Ethernet", ifname);
150 if (NOT_UNICAST(hwaddr)) {
153 "Interface %.16s has broadcast/multicast MAC address??",
159 /* Sanity check on MTU */
160 strncpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
161 if (ioctl(fd, SIOCGIFMTU, &ifr) < 0) {
162 fatalSys("ioctl(SIOCGIFMTU)");
164 if (ifr.ifr_mtu < ETH_DATA_LEN) {
165 fprintf(stderr, "Interface %.16s has MTU of %d -- should be %d.\n",
166 ifname, ifr.ifr_mtu, ETH_DATA_LEN);
167 fprintf(stderr, "You may have serious connection problems.\n");
170 #ifdef HAVE_STRUCT_SOCKADDR_LL
171 /* Get interface index */
172 sa.sll_family = AF_PACKET;
173 sa.sll_protocol = htons(type);
175 strncpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
176 if (ioctl(fd, SIOCGIFINDEX, &ifr) < 0) {
177 fatalSys("ioctl(SIOCFIGINDEX): Could not get interface index");
179 sa.sll_ifindex = ifr.ifr_ifindex;
182 strcpy(sa.sa_data, ifname);
185 /* We're only interested in packets on specified interface */
186 if (bind(fd, (struct sockaddr *) &sa, sizeof(sa)) < 0) {
194 /***********************************************************************
195 *%FUNCTION: sendPacket
197 * sock -- socket to send to
198 * pkt -- the packet to transmit
199 * size -- size of packet (in bytes)
201 * 0 on success; -1 on failure
204 ***********************************************************************/
206 sendPacket(PPPoEConnection *conn, int sock, PPPoEPacket *pkt, int size)
208 #if defined(HAVE_STRUCT_SOCKADDR_LL)
209 if (send(sock, pkt, size, 0) < 0) {
210 sysErr("send (sendPacket)");
217 rp_fatal("relay and server not supported on Linux 2.0 kernels");
219 strcpy(sa.sa_data, conn->ifName);
220 if (sendto(sock, pkt, size, 0, &sa, sizeof(sa)) < 0) {
221 sysErr("sendto (sendPacket)");
228 /***********************************************************************
229 *%FUNCTION: receivePacket
231 * sock -- socket to read from
232 * pkt -- place to store the received packet
233 * size -- set to size of packet in bytes
235 * >= 0 if all OK; < 0 if error
238 ***********************************************************************/
240 receivePacket(int sock, PPPoEPacket *pkt, int *size)
242 if ((*size = recv(sock, pkt, sizeof(PPPoEPacket), 0)) < 0) {
243 sysErr("recv (receivePacket)");
249 /**********************************************************************
250 *%FUNCTION: parsePacket
252 * packet -- the PPPoE discovery packet to parse
253 * func -- function called for each tag in the packet
254 * extra -- an opaque data pointer supplied to parsing function
256 * 0 if everything went well; -1 if there was an error
258 * Parses a PPPoE discovery packet, calling "func" for each tag in the packet.
259 * "func" is passed the additional argument "extra".
260 ***********************************************************************/
262 parsePacket(PPPoEPacket *packet, ParseFunc *func, void *extra)
264 UINT16_t len = ntohs(packet->length);
265 unsigned char *curTag;
266 UINT16_t tagType, tagLen;
268 if (PPPOE_VER(packet->vertype) != 1) {
269 fprintf(stderr, "Invalid PPPoE version (%d)\n",
270 PPPOE_VER(packet->vertype));
273 if (PPPOE_TYPE(packet->vertype) != 1) {
274 fprintf(stderr, "Invalid PPPoE type (%d)\n",
275 PPPOE_TYPE(packet->vertype));
279 /* Do some sanity checks on packet */
280 if (len > ETH_JUMBO_LEN - PPPOE_OVERHEAD) { /* 6-byte overhead for PPPoE header */
281 fprintf(stderr, "Invalid PPPoE packet length (%u)\n", len);
285 /* Step through the tags */
286 curTag = packet->payload;
287 while(curTag - packet->payload < len) {
288 /* Alignment is not guaranteed, so do this by hand... */
289 tagType = (curTag[0] << 8) + curTag[1];
290 tagLen = (curTag[2] << 8) + curTag[3];
291 if (tagType == TAG_END_OF_LIST) {
294 if ((curTag - packet->payload) + tagLen + TAG_HDR_SIZE > len) {
295 fprintf(stderr, "Invalid PPPoE tag length (%u)\n", tagLen);
298 func(tagType, tagLen, curTag+TAG_HDR_SIZE, extra);
299 curTag = curTag + TAG_HDR_SIZE + tagLen;
304 /**********************************************************************
305 *%FUNCTION: parseForHostUniq
310 * extra -- user-supplied pointer. This is assumed to be a pointer to int.
314 * If a HostUnique tag is found which matches our PID, sets *extra to 1.
315 ***********************************************************************/
317 parseForHostUniq(UINT16_t type, UINT16_t len, unsigned char *data,
320 int *val = (int *) extra;
321 if (type == TAG_HOST_UNIQ && len == sizeof(pid_t)) {
323 memcpy(&tmp, data, len);
324 if (tmp == getpid()) {
330 /**********************************************************************
331 *%FUNCTION: packetIsForMe
333 * conn -- PPPoE connection info
334 * packet -- a received PPPoE packet
336 * 1 if packet is for this PPPoE daemon; 0 otherwise.
338 * If we are using the Host-Unique tag, verifies that packet contains
339 * our unique identifier.
340 ***********************************************************************/
342 packetIsForMe(PPPoEConnection *conn, PPPoEPacket *packet)
346 /* If packet is not directed to our MAC address, forget it */
347 if (memcmp(packet->ethHdr.h_dest, conn->myEth, ETH_ALEN)) return 0;
349 /* If we're not using the Host-Unique tag, then accept the packet */
350 if (!conn->useHostUniq) return 1;
352 parsePacket(packet, parseForHostUniq, &forMe);
356 /**********************************************************************
357 *%FUNCTION: parsePADOTags
362 * extra -- extra user data. Should point to a PacketCriteria structure
363 * which gets filled in according to selected AC name and service
368 * Picks interesting tags out of a PADO packet
369 ***********************************************************************/
371 parsePADOTags(UINT16_t type, UINT16_t len, unsigned char *data,
374 struct PacketCriteria *pc = (struct PacketCriteria *) extra;
375 PPPoEConnection *conn = pc->conn;
381 printf("Access-Concentrator: %.*s\n", (int) len, data);
382 if (conn->acName && len == strlen(conn->acName) &&
383 !strncmp((char *) data, conn->acName, len)) {
387 case TAG_SERVICE_NAME:
388 pc->seenServiceName = 1;
390 printf(" Service-Name: %.*s\n", (int) len, data);
392 if (conn->serviceName && len == strlen(conn->serviceName) &&
393 !strncmp((char *) data, conn->serviceName, len)) {
394 pc->serviceNameOK = 1;
398 printf("Got a cookie:");
399 /* Print first 20 bytes of cookie */
400 for (i=0; i<len && i < 20; i++) {
401 printf(" %02x", (unsigned) data[i]);
403 if (i < len) printf("...");
405 conn->cookie.type = htons(type);
406 conn->cookie.length = htons(len);
407 memcpy(conn->cookie.payload, data, len);
409 case TAG_RELAY_SESSION_ID:
410 printf("Got a Relay-ID:");
411 /* Print first 20 bytes of relay ID */
412 for (i=0; i<len && i < 20; i++) {
413 printf(" %02x", (unsigned) data[i]);
415 if (i < len) printf("...");
417 conn->relayId.type = htons(type);
418 conn->relayId.length = htons(len);
419 memcpy(conn->relayId.payload, data, len);
421 case TAG_SERVICE_NAME_ERROR:
422 printf("Got a Service-Name-Error tag: %.*s\n", (int) len, data);
424 case TAG_AC_SYSTEM_ERROR:
425 printf("Got a System-Error tag: %.*s\n", (int) len, data);
427 case TAG_GENERIC_ERROR:
428 printf("Got a Generic-Error tag: %.*s\n", (int) len, data);
433 /***********************************************************************
436 * conn -- PPPoEConnection structure
440 * Sends a PADI packet
441 ***********************************************************************/
443 sendPADI(PPPoEConnection *conn)
446 unsigned char *cursor = packet.payload;
447 PPPoETag *svc = (PPPoETag *) (&packet.payload);
448 UINT16_t namelen = 0;
451 if (conn->serviceName) {
452 namelen = (UINT16_t) strlen(conn->serviceName);
454 plen = TAG_HDR_SIZE + namelen;
455 CHECK_ROOM(cursor, packet.payload, plen);
457 /* Set destination to Ethernet broadcast address */
458 memset(packet.ethHdr.h_dest, 0xFF, ETH_ALEN);
459 memcpy(packet.ethHdr.h_source, conn->myEth, ETH_ALEN);
461 packet.ethHdr.h_proto = htons(Eth_PPPOE_Discovery);
462 packet.vertype = PPPOE_VER_TYPE(1, 1);
463 packet.code = CODE_PADI;
466 svc->type = TAG_SERVICE_NAME;
467 svc->length = htons(namelen);
468 CHECK_ROOM(cursor, packet.payload, namelen+TAG_HDR_SIZE);
470 if (conn->serviceName) {
471 memcpy(svc->payload, conn->serviceName, strlen(conn->serviceName));
473 cursor += namelen + TAG_HDR_SIZE;
475 /* If we're using Host-Uniq, copy it over */
476 if (conn->useHostUniq) {
478 pid_t pid = getpid();
479 hostUniq.type = htons(TAG_HOST_UNIQ);
480 hostUniq.length = htons(sizeof(pid));
481 memcpy(hostUniq.payload, &pid, sizeof(pid));
482 CHECK_ROOM(cursor, packet.payload, sizeof(pid) + TAG_HDR_SIZE);
483 memcpy(cursor, &hostUniq, sizeof(pid) + TAG_HDR_SIZE);
484 cursor += sizeof(pid) + TAG_HDR_SIZE;
485 plen += sizeof(pid) + TAG_HDR_SIZE;
488 packet.length = htons(plen);
490 sendPacket(conn, conn->discoverySocket, &packet, (int) (plen + HDR_SIZE));
491 if (conn->debugFile) {
492 dumpPacket(conn->debugFile, &packet, "SENT");
493 fprintf(conn->debugFile, "\n");
494 fflush(conn->debugFile);
498 /**********************************************************************
499 *%FUNCTION: waitForPADO
501 * conn -- PPPoEConnection structure
502 * timeout -- how long to wait (in seconds)
506 * Waits for a PADO packet and copies useful information
507 ***********************************************************************/
509 waitForPADO(PPPoEConnection *conn, int timeout)
517 struct PacketCriteria pc;
519 pc.acNameOK = (conn->acName) ? 0 : 1;
520 pc.serviceNameOK = (conn->serviceName) ? 0 : 1;
522 pc.seenServiceName = 0;
526 if (BPF_BUFFER_IS_EMPTY) {
531 FD_SET(conn->discoverySocket, &readable);
534 r = select(conn->discoverySocket+1, &readable, NULL, NULL, &tv);
535 if (r >= 0 || errno != EINTR) break;
538 perror("select (waitForPADO)");
541 if (r == 0) return; /* Timed out */
545 receivePacket(conn->discoverySocket, &packet, &len);
548 if (ntohs(packet.length) + HDR_SIZE > len) {
549 fprintf(stderr, "Bogus PPPoE length field (%u)\n",
550 (unsigned int) ntohs(packet.length));
555 /* If it's not a Discovery packet, loop again */
556 if (etherType(&packet) != Eth_PPPOE_Discovery) continue;
559 if (conn->debugFile) {
560 dumpPacket(conn->debugFile, &packet, "RCVD");
561 fprintf(conn->debugFile, "\n");
562 fflush(conn->debugFile);
564 /* If it's not for us, loop again */
565 if (!packetIsForMe(conn, &packet)) continue;
567 if (packet.code == CODE_PADO) {
568 if (BROADCAST(packet.ethHdr.h_source)) {
569 fprintf(stderr, "Ignoring PADO packet from broadcast MAC address\n");
572 parsePacket(&packet, parsePADOTags, &pc);
575 if (!pc.seenACName) {
576 fprintf(stderr, "Ignoring PADO packet with no AC-Name tag\n");
579 if (!pc.seenServiceName) {
580 fprintf(stderr, "Ignoring PADO packet with no Service-Name tag\n");
584 printf("--------------------------------------------------\n");
585 if (pc.acNameOK && pc.serviceNameOK) {
586 memcpy(conn->peerEth, packet.ethHdr.h_source, ETH_ALEN);
587 if (conn->printACNames) {
588 printf("AC-Ethernet-Address: %02x:%02x:%02x:%02x:%02x:%02x\n",
589 (unsigned) conn->peerEth[0],
590 (unsigned) conn->peerEth[1],
591 (unsigned) conn->peerEth[2],
592 (unsigned) conn->peerEth[3],
593 (unsigned) conn->peerEth[4],
594 (unsigned) conn->peerEth[5]);
597 conn->discoveryState = STATE_RECEIVED_PADO;
601 } while (conn->discoveryState != STATE_RECEIVED_PADO);
604 /**********************************************************************
605 *%FUNCTION: discovery
607 * conn -- PPPoE connection info structure
611 * Performs the PPPoE discovery phase
612 ***********************************************************************/
614 discovery(PPPoEConnection *conn)
616 int padiAttempts = 0;
617 int timeout = PADI_TIMEOUT;
619 conn->discoverySocket =
620 openInterface(conn->ifName, Eth_PPPOE_Discovery, conn->myEth);
624 if (padiAttempts > MAX_PADI_ATTEMPTS) {
625 fprintf(stderr, "Timeout waiting for PADO packets\n");
626 close(conn->discoverySocket);
627 conn->discoverySocket = -1;
631 conn->discoveryState = STATE_SENT_PADI;
632 waitForPADO(conn, timeout);
633 } while (!conn->numPADOs);
636 int main(int argc, char *argv[])
639 PPPoEConnection *conn;
641 conn = malloc(sizeof(PPPoEConnection));
645 memset(conn, 0, sizeof(PPPoEConnection));
647 while ((opt = getopt(argc, argv, "I:D:VUAS:C:h")) > 0) {
650 conn->serviceName = xstrdup(optarg);
653 conn->acName = xstrdup(optarg);
656 conn->useHostUniq = 1;
659 conn->debugFile = fopen(optarg, "w");
660 if (!conn->debugFile) {
661 fprintf(stderr, "Could not open %s: %s\n",
662 optarg, strerror(errno));
665 fprintf(conn->debugFile, "pppoe-discovery %s\n", RP_VERSION);
668 conn->ifName = xstrdup(optarg);
671 /* this is the default */
683 /* default interface name */
685 conn->ifName = strdup("eth0");
687 conn->discoverySocket = -1;
688 conn->sessionSocket = -1;
689 conn->printACNames = 1;
695 void rp_fatal(char const *str)
697 fprintf(stderr, "%s\n", str);
701 void fatalSys(char const *str)
707 void sysErr(char const *str)
712 char *xstrdup(const char *s)
714 register char *ret = strdup(s);
722 fprintf(stderr, "Usage: pppoe-discovery [options]\n");
723 fprintf(stderr, "\nVersion " RP_VERSION "\n");