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 printf("Access-Concentrator: %.*s\n", (int) len, data);
391 if (conn->acName && len == strlen(conn->acName) &&
392 !strncmp((char *) data, conn->acName, len)) {
396 case TAG_SERVICE_NAME:
397 pc->seenServiceName = 1;
399 printf(" Service-Name: %.*s\n", (int) len, data);
401 if (conn->serviceName && len == strlen(conn->serviceName) &&
402 !strncmp((char *) data, conn->serviceName, len)) {
403 pc->serviceNameOK = 1;
407 printf("Got a cookie:");
408 /* Print first 20 bytes of cookie */
409 for (i=0; i<len && i < 20; i++) {
410 printf(" %02x", (unsigned) data[i]);
412 if (i < len) printf("...");
414 conn->cookie.type = htons(type);
415 conn->cookie.length = htons(len);
416 memcpy(conn->cookie.payload, data, len);
418 case TAG_RELAY_SESSION_ID:
419 printf("Got a Relay-ID:");
420 /* Print first 20 bytes of relay ID */
421 for (i=0; i<len && i < 20; i++) {
422 printf(" %02x", (unsigned) data[i]);
424 if (i < len) printf("...");
426 conn->relayId.type = htons(type);
427 conn->relayId.length = htons(len);
428 memcpy(conn->relayId.payload, data, len);
430 case TAG_SERVICE_NAME_ERROR:
431 printf("Got a Service-Name-Error tag: %.*s\n", (int) len, data);
433 case TAG_AC_SYSTEM_ERROR:
434 printf("Got a System-Error tag: %.*s\n", (int) len, data);
436 case TAG_GENERIC_ERROR:
437 printf("Got a Generic-Error tag: %.*s\n", (int) len, data);
442 /***********************************************************************
445 * conn -- PPPoEConnection structure
449 * Sends a PADI packet
450 ***********************************************************************/
452 sendPADI(PPPoEConnection *conn)
455 unsigned char *cursor = packet.payload;
456 PPPoETag *svc = (PPPoETag *) (&packet.payload);
457 UINT16_t namelen = 0;
460 if (conn->serviceName) {
461 namelen = (UINT16_t) strlen(conn->serviceName);
463 plen = TAG_HDR_SIZE + namelen;
464 CHECK_ROOM(cursor, packet.payload, plen);
466 /* Set destination to Ethernet broadcast address */
467 memset(packet.ethHdr.h_dest, 0xFF, ETH_ALEN);
468 memcpy(packet.ethHdr.h_source, conn->myEth, ETH_ALEN);
470 packet.ethHdr.h_proto = htons(Eth_PPPOE_Discovery);
471 packet.vertype = PPPOE_VER_TYPE(1, 1);
472 packet.code = CODE_PADI;
475 svc->type = TAG_SERVICE_NAME;
476 svc->length = htons(namelen);
477 CHECK_ROOM(cursor, packet.payload, namelen+TAG_HDR_SIZE);
479 if (conn->serviceName) {
480 memcpy(svc->payload, conn->serviceName, strlen(conn->serviceName));
482 cursor += namelen + TAG_HDR_SIZE;
484 /* If we're using Host-Uniq, copy it over */
485 if (conn->useHostUniq) {
487 pid_t pid = getpid();
488 hostUniq.type = htons(TAG_HOST_UNIQ);
489 hostUniq.length = htons(sizeof(pid));
490 memcpy(hostUniq.payload, &pid, sizeof(pid));
491 CHECK_ROOM(cursor, packet.payload, sizeof(pid) + TAG_HDR_SIZE);
492 memcpy(cursor, &hostUniq, sizeof(pid) + TAG_HDR_SIZE);
493 cursor += sizeof(pid) + TAG_HDR_SIZE;
494 plen += sizeof(pid) + TAG_HDR_SIZE;
497 packet.length = htons(plen);
499 sendPacket(conn, conn->discoverySocket, &packet, (int) (plen + HDR_SIZE));
500 if (conn->debugFile) {
501 dumpPacket(conn->debugFile, &packet, "SENT");
502 fprintf(conn->debugFile, "\n");
503 fflush(conn->debugFile);
507 /**********************************************************************
508 *%FUNCTION: waitForPADO
510 * conn -- PPPoEConnection structure
511 * timeout -- how long to wait (in seconds)
515 * Waits for a PADO packet and copies useful information
516 ***********************************************************************/
518 waitForPADO(PPPoEConnection *conn, int timeout)
526 struct PacketCriteria pc;
528 pc.acNameOK = (conn->acName) ? 0 : 1;
529 pc.serviceNameOK = (conn->serviceName) ? 0 : 1;
531 pc.seenServiceName = 0;
535 if (BPF_BUFFER_IS_EMPTY) {
540 FD_SET(conn->discoverySocket, &readable);
543 r = select(conn->discoverySocket+1, &readable, NULL, NULL, &tv);
544 if (r >= 0 || errno != EINTR) break;
547 perror("select (waitForPADO)");
550 if (r == 0) return; /* Timed out */
554 receivePacket(conn->discoverySocket, &packet, &len);
557 if (ntohs(packet.length) + HDR_SIZE > len) {
558 fprintf(stderr, "Bogus PPPoE length field (%u)\n",
559 (unsigned int) ntohs(packet.length));
564 /* If it's not a Discovery packet, loop again */
565 if (etherType(&packet) != Eth_PPPOE_Discovery) continue;
568 if (conn->debugFile) {
569 dumpPacket(conn->debugFile, &packet, "RCVD");
570 fprintf(conn->debugFile, "\n");
571 fflush(conn->debugFile);
573 /* If it's not for us, loop again */
574 if (!packetIsForMe(conn, &packet)) continue;
576 if (packet.code == CODE_PADO) {
577 if (BROADCAST(packet.ethHdr.h_source)) {
578 fprintf(stderr, "Ignoring PADO packet from broadcast MAC address\n");
581 parsePacket(&packet, parsePADOTags, &pc);
584 if (!pc.seenACName) {
585 fprintf(stderr, "Ignoring PADO packet with no AC-Name tag\n");
588 if (!pc.seenServiceName) {
589 fprintf(stderr, "Ignoring PADO packet with no Service-Name tag\n");
593 printf("--------------------------------------------------\n");
594 if (pc.acNameOK && pc.serviceNameOK) {
595 memcpy(conn->peerEth, packet.ethHdr.h_source, ETH_ALEN);
596 if (conn->printACNames) {
597 printf("AC-Ethernet-Address: %02x:%02x:%02x:%02x:%02x:%02x\n",
598 (unsigned) conn->peerEth[0],
599 (unsigned) conn->peerEth[1],
600 (unsigned) conn->peerEth[2],
601 (unsigned) conn->peerEth[3],
602 (unsigned) conn->peerEth[4],
603 (unsigned) conn->peerEth[5]);
606 conn->discoveryState = STATE_RECEIVED_PADO;
610 } while (conn->discoveryState != STATE_RECEIVED_PADO);
613 /**********************************************************************
614 *%FUNCTION: discovery
616 * conn -- PPPoE connection info structure
620 * Performs the PPPoE discovery phase
621 ***********************************************************************/
623 discovery(PPPoEConnection *conn)
625 int padiAttempts = 0;
626 int timeout = PADI_TIMEOUT;
628 conn->discoverySocket =
629 openInterface(conn->ifName, Eth_PPPOE_Discovery, conn->myEth);
633 if (padiAttempts > MAX_PADI_ATTEMPTS) {
634 fprintf(stderr, "Timeout waiting for PADO packets\n");
635 close(conn->discoverySocket);
636 conn->discoverySocket = -1;
640 conn->discoveryState = STATE_SENT_PADI;
641 waitForPADO(conn, timeout);
642 } while (!conn->numPADOs);
645 int main(int argc, char *argv[])
648 PPPoEConnection *conn;
650 conn = malloc(sizeof(PPPoEConnection));
654 memset(conn, 0, sizeof(PPPoEConnection));
656 while ((opt = getopt(argc, argv, "I:D:VUAS:C:h")) > 0) {
659 conn->serviceName = xstrdup(optarg);
662 conn->acName = xstrdup(optarg);
665 conn->useHostUniq = 1;
668 conn->debugFile = fopen(optarg, "w");
669 if (!conn->debugFile) {
670 fprintf(stderr, "Could not open %s: %s\n",
671 optarg, strerror(errno));
674 fprintf(conn->debugFile, "pppoe-discovery %s\n", RP_VERSION);
677 conn->ifName = xstrdup(optarg);
680 /* this is the default */
692 /* default interface name */
694 conn->ifName = strdup("eth0");
696 conn->discoverySocket = -1;
697 conn->sessionSocket = -1;
698 conn->printACNames = 1;
704 void rp_fatal(char const *str)
706 fprintf(stderr, "%s\n", str);
710 void fatalSys(char const *str)
716 void sysErr(char const *str)
721 char *xstrdup(const char *s)
723 register char *ret = strdup(s);
731 fprintf(stderr, "Usage: pppoe-discovery [options]\n");
732 fprintf(stderr, "\nVersion " RP_VERSION "\n");