]> git.ozlabs.org Git - ppp.git/blob - pppd/plugins/rp-pppoe/pppoe-discovery.c
pppoe-discovery: add quiet-mode option
[ppp.git] / pppd / plugins / rp-pppoe / pppoe-discovery.c
1 /*
2  * Perform PPPoE discovery
3  *
4  * Copyright (C) 2000-2001 by Roaring Penguin Software Inc.
5  * Copyright (C) 2004 Marco d'Itri <md@linux.it>
6  *
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.
9  *
10  */
11
12 #include <stdarg.h>
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <unistd.h>
16 #include <errno.h>
17 #include <string.h>
18
19 #include "pppoe.h"
20
21 #ifdef HAVE_UNISTD_H
22 #include <unistd.h>
23 #endif
24
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>
29 #endif
30
31 #ifdef HAVE_NET_ETHERNET_H
32 #include <net/ethernet.h>
33 #endif
34
35 #ifdef HAVE_ASM_TYPES_H
36 #include <asm/types.h>
37 #endif
38
39 #ifdef HAVE_SYS_IOCTL_H
40 #include <sys/ioctl.h>
41 #endif
42
43 #include <errno.h>
44 #include <stdlib.h>
45 #include <string.h>
46
47 #ifdef HAVE_NET_IF_ARP_H
48 #include <net/if_arp.h>
49 #endif
50
51 char *xstrdup(const char *s);
52 void usage(void);
53
54 void die(int status)
55 {
56         exit(status);
57 }
58
59 void error(char *fmt, ...)
60 {
61     va_list pvar;
62     va_start(pvar, fmt);
63     vfprintf(stderr, fmt, pvar);
64     va_end(pvar);
65 }
66
67 /* Initialize frame types to RFC 2516 values.  Some broken peers apparently
68    use different frame types... sigh... */
69
70 UINT16_t Eth_PPPOE_Discovery = ETH_PPPOE_DISCOVERY;
71 UINT16_t Eth_PPPOE_Session   = ETH_PPPOE_SESSION;
72
73 /**********************************************************************
74 *%FUNCTION: etherType
75 *%ARGUMENTS:
76 * packet -- a received PPPoE packet
77 *%RETURNS:
78 * ethernet packet type (see /usr/include/net/ethertypes.h)
79 *%DESCRIPTION:
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 ***********************************************************************/
88 UINT16_t
89 etherType(PPPoEPacket *packet)
90 {
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);
94     }
95     return type;
96 }
97
98 /**********************************************************************
99 *%FUNCTION: openInterface
100 *%ARGUMENTS:
101 * ifname -- name of interface
102 * type -- Ethernet frame type
103 * hwaddr -- if non-NULL, set to the hardware address
104 *%RETURNS:
105 * A raw socket for talking to the Ethernet card.  Exits on error.
106 *%DESCRIPTION:
107 * Opens a raw Ethernet socket
108 ***********************************************************************/
109 int
110 openInterface(char const *ifname, UINT16_t type, unsigned char *hwaddr)
111 {
112     int optval=1;
113     int fd;
114     struct ifreq ifr;
115     int domain, stype;
116
117 #ifdef HAVE_STRUCT_SOCKADDR_LL
118     struct sockaddr_ll sa;
119 #else
120     struct sockaddr sa;
121 #endif
122
123     memset(&sa, 0, sizeof(sa));
124
125 #ifdef HAVE_STRUCT_SOCKADDR_LL
126     domain = PF_PACKET;
127     stype = SOCK_RAW;
128 #else
129     domain = PF_INET;
130     stype = SOCK_PACKET;
131 #endif
132
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.");
137         }
138         fatalSys("socket");
139     }
140
141     if (setsockopt(fd, SOL_SOCKET, SO_BROADCAST, &optval, sizeof(optval)) < 0) {
142         fatalSys("setsockopt");
143     }
144
145     /* Fill in hardware address */
146     if (hwaddr) {
147         strncpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
148         if (ioctl(fd, SIOCGIFHWADDR, &ifr) < 0) {
149             fatalSys("ioctl(SIOCGIFHWADDR)");
150         }
151         memcpy(hwaddr, ifr.ifr_hwaddr.sa_data, ETH_ALEN);
152 #ifdef ARPHRD_ETHER
153         if (ifr.ifr_hwaddr.sa_family != ARPHRD_ETHER) {
154             char buffer[256];
155             sprintf(buffer, "Interface %.16s is not Ethernet", ifname);
156             rp_fatal(buffer);
157         }
158 #endif
159         if (NOT_UNICAST(hwaddr)) {
160             char buffer[256];
161             sprintf(buffer,
162                     "Interface %.16s has broadcast/multicast MAC address??",
163                     ifname);
164             rp_fatal(buffer);
165         }
166     }
167
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)");
172     }
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");
177     }
178
179 #ifdef HAVE_STRUCT_SOCKADDR_LL
180     /* Get interface index */
181     sa.sll_family = AF_PACKET;
182     sa.sll_protocol = htons(type);
183
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");
187     }
188     sa.sll_ifindex = ifr.ifr_ifindex;
189
190 #else
191     strcpy(sa.sa_data, ifname);
192 #endif
193
194     /* We're only interested in packets on specified interface */
195     if (bind(fd, (struct sockaddr *) &sa, sizeof(sa)) < 0) {
196         fatalSys("bind");
197     }
198
199     return fd;
200 }
201
202
203 /***********************************************************************
204 *%FUNCTION: sendPacket
205 *%ARGUMENTS:
206 * sock -- socket to send to
207 * pkt -- the packet to transmit
208 * size -- size of packet (in bytes)
209 *%RETURNS:
210 * 0 on success; -1 on failure
211 *%DESCRIPTION:
212 * Transmits a packet
213 ***********************************************************************/
214 int
215 sendPacket(PPPoEConnection *conn, int sock, PPPoEPacket *pkt, int size)
216 {
217 #if defined(HAVE_STRUCT_SOCKADDR_LL)
218     if (send(sock, pkt, size, 0) < 0) {
219         sysErr("send (sendPacket)");
220         return -1;
221     }
222 #else
223     struct sockaddr sa;
224
225     if (!conn) {
226         rp_fatal("relay and server not supported on Linux 2.0 kernels");
227     }
228     strcpy(sa.sa_data, conn->ifName);
229     if (sendto(sock, pkt, size, 0, &sa, sizeof(sa)) < 0) {
230         sysErr("sendto (sendPacket)");
231         return -1;
232     }
233 #endif
234     return 0;
235 }
236
237 /***********************************************************************
238 *%FUNCTION: receivePacket
239 *%ARGUMENTS:
240 * sock -- socket to read from
241 * pkt -- place to store the received packet
242 * size -- set to size of packet in bytes
243 *%RETURNS:
244 * >= 0 if all OK; < 0 if error
245 *%DESCRIPTION:
246 * Receives a packet
247 ***********************************************************************/
248 int
249 receivePacket(int sock, PPPoEPacket *pkt, int *size)
250 {
251     if ((*size = recv(sock, pkt, sizeof(PPPoEPacket), 0)) < 0) {
252         sysErr("recv (receivePacket)");
253         return -1;
254     }
255     return 0;
256 }
257
258 /**********************************************************************
259 *%FUNCTION: parsePacket
260 *%ARGUMENTS:
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
264 *%RETURNS:
265 * 0 if everything went well; -1 if there was an error
266 *%DESCRIPTION:
267 * Parses a PPPoE discovery packet, calling "func" for each tag in the packet.
268 * "func" is passed the additional argument "extra".
269 ***********************************************************************/
270 int
271 parsePacket(PPPoEPacket *packet, ParseFunc *func, void *extra)
272 {
273     UINT16_t len = ntohs(packet->length);
274     unsigned char *curTag;
275     UINT16_t tagType, tagLen;
276
277     if (PPPOE_VER(packet->vertype) != 1) {
278         fprintf(stderr, "Invalid PPPoE version (%d)\n",
279                 PPPOE_VER(packet->vertype));
280         return -1;
281     }
282     if (PPPOE_TYPE(packet->vertype) != 1) {
283         fprintf(stderr, "Invalid PPPoE type (%d)\n",
284                 PPPOE_TYPE(packet->vertype));
285         return -1;
286     }
287
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);
291         return -1;
292     }
293
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) {
301             return 0;
302         }
303         if ((curTag - packet->payload) + tagLen + TAG_HDR_SIZE > len) {
304             fprintf(stderr, "Invalid PPPoE tag length (%u)\n", tagLen);
305             return -1;
306         }
307         func(tagType, tagLen, curTag+TAG_HDR_SIZE, extra);
308         curTag = curTag + TAG_HDR_SIZE + tagLen;
309     }
310     return 0;
311 }
312
313 /**********************************************************************
314 *%FUNCTION: parseForHostUniq
315 *%ARGUMENTS:
316 * type -- tag type
317 * len -- tag length
318 * data -- tag data.
319 * extra -- user-supplied pointer.  This is assumed to be a pointer to int.
320 *%RETURNS:
321 * Nothing
322 *%DESCRIPTION:
323 * If a HostUnique tag is found which matches our PID, sets *extra to 1.
324 ***********************************************************************/
325 void
326 parseForHostUniq(UINT16_t type, UINT16_t len, unsigned char *data,
327                  void *extra)
328 {
329     int *val = (int *) extra;
330     if (type == TAG_HOST_UNIQ && len == sizeof(pid_t)) {
331         pid_t tmp;
332         memcpy(&tmp, data, len);
333         if (tmp == getpid()) {
334             *val = 1;
335         }
336     }
337 }
338
339 /**********************************************************************
340 *%FUNCTION: packetIsForMe
341 *%ARGUMENTS:
342 * conn -- PPPoE connection info
343 * packet -- a received PPPoE packet
344 *%RETURNS:
345 * 1 if packet is for this PPPoE daemon; 0 otherwise.
346 *%DESCRIPTION:
347 * If we are using the Host-Unique tag, verifies that packet contains
348 * our unique identifier.
349 ***********************************************************************/
350 int
351 packetIsForMe(PPPoEConnection *conn, PPPoEPacket *packet)
352 {
353     int forMe = 0;
354
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;
357
358     /* If we're not using the Host-Unique tag, then accept the packet */
359     if (!conn->useHostUniq) return 1;
360
361     parsePacket(packet, parseForHostUniq, &forMe);
362     return forMe;
363 }
364
365 /**********************************************************************
366 *%FUNCTION: parsePADOTags
367 *%ARGUMENTS:
368 * type -- tag type
369 * len -- tag length
370 * data -- tag data
371 * extra -- extra user data.  Should point to a PacketCriteria structure
372 *          which gets filled in according to selected AC name and service
373 *          name.
374 *%RETURNS:
375 * Nothing
376 *%DESCRIPTION:
377 * Picks interesting tags out of a PADO packet
378 ***********************************************************************/
379 void
380 parsePADOTags(UINT16_t type, UINT16_t len, unsigned char *data,
381               void *extra)
382 {
383     struct PacketCriteria *pc = (struct PacketCriteria *) extra;
384     PPPoEConnection *conn = pc->conn;
385     int i;
386
387     switch(type) {
388     case TAG_AC_NAME:
389         pc->seenACName = 1;
390         if (conn->printACNames) {
391             printf("Access-Concentrator: %.*s\n", (int) len, data);
392         }
393         if (conn->acName && len == strlen(conn->acName) &&
394             !strncmp((char *) data, conn->acName, len)) {
395             pc->acNameOK = 1;
396         }
397         break;
398     case TAG_SERVICE_NAME:
399         pc->seenServiceName = 1;
400         if (conn->printACNames && len > 0) {
401             printf("       Service-Name: %.*s\n", (int) len, data);
402         }
403         if (conn->serviceName && len == strlen(conn->serviceName) &&
404             !strncmp((char *) data, conn->serviceName, len)) {
405             pc->serviceNameOK = 1;
406         }
407         break;
408     case TAG_AC_COOKIE:
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]);
414             }
415             if (i < len) printf("...");
416             printf("\n");
417         }
418         conn->cookie.type = htons(type);
419         conn->cookie.length = htons(len);
420         memcpy(conn->cookie.payload, data, len);
421         break;
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]);
428             }
429             if (i < len) printf("...");
430             printf("\n");
431         }
432         conn->relayId.type = htons(type);
433         conn->relayId.length = htons(len);
434         memcpy(conn->relayId.payload, data, len);
435         break;
436     case TAG_SERVICE_NAME_ERROR:
437         if (conn->printACNames) {
438             printf("Got a Service-Name-Error tag: %.*s\n", (int) len, data);
439         }
440         break;
441     case TAG_AC_SYSTEM_ERROR:
442         if (conn->printACNames) {
443             printf("Got a System-Error tag: %.*s\n", (int) len, data);
444         }
445         break;
446     case TAG_GENERIC_ERROR:
447         if (conn->printACNames) {
448             printf("Got a Generic-Error tag: %.*s\n", (int) len, data);
449         }
450         break;
451     }
452 }
453
454 /***********************************************************************
455 *%FUNCTION: sendPADI
456 *%ARGUMENTS:
457 * conn -- PPPoEConnection structure
458 *%RETURNS:
459 * Nothing
460 *%DESCRIPTION:
461 * Sends a PADI packet
462 ***********************************************************************/
463 void
464 sendPADI(PPPoEConnection *conn)
465 {
466     PPPoEPacket packet;
467     unsigned char *cursor = packet.payload;
468     PPPoETag *svc = (PPPoETag *) (&packet.payload);
469     UINT16_t namelen = 0;
470     UINT16_t plen;
471
472     if (conn->serviceName) {
473         namelen = (UINT16_t) strlen(conn->serviceName);
474     }
475     plen = TAG_HDR_SIZE + namelen;
476     CHECK_ROOM(cursor, packet.payload, plen);
477
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);
481
482     packet.ethHdr.h_proto = htons(Eth_PPPOE_Discovery);
483     packet.vertype = PPPOE_VER_TYPE(1, 1);
484     packet.code = CODE_PADI;
485     packet.session = 0;
486
487     svc->type = TAG_SERVICE_NAME;
488     svc->length = htons(namelen);
489     CHECK_ROOM(cursor, packet.payload, namelen+TAG_HDR_SIZE);
490
491     if (conn->serviceName) {
492         memcpy(svc->payload, conn->serviceName, strlen(conn->serviceName));
493     }
494     cursor += namelen + TAG_HDR_SIZE;
495
496     /* If we're using Host-Uniq, copy it over */
497     if (conn->useHostUniq) {
498         PPPoETag hostUniq;
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;
507     }
508
509     packet.length = htons(plen);
510
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);
516     }
517 }
518
519 /**********************************************************************
520 *%FUNCTION: waitForPADO
521 *%ARGUMENTS:
522 * conn -- PPPoEConnection structure
523 * timeout -- how long to wait (in seconds)
524 *%RETURNS:
525 * Nothing
526 *%DESCRIPTION:
527 * Waits for a PADO packet and copies useful information
528 ***********************************************************************/
529 void
530 waitForPADO(PPPoEConnection *conn, int timeout)
531 {
532     fd_set readable;
533     int r;
534     struct timeval tv;
535     PPPoEPacket packet;
536     int len;
537
538     struct PacketCriteria pc;
539     pc.conn          = conn;
540     pc.acNameOK      = (conn->acName)      ? 0 : 1;
541     pc.serviceNameOK = (conn->serviceName) ? 0 : 1;
542     pc.seenACName    = 0;
543     pc.seenServiceName = 0;
544     conn->error = 0;
545         
546     do {
547         if (BPF_BUFFER_IS_EMPTY) {
548             tv.tv_sec = timeout;
549             tv.tv_usec = 0;
550         
551             FD_ZERO(&readable);
552             FD_SET(conn->discoverySocket, &readable);
553
554             while(1) {
555                 r = select(conn->discoverySocket+1, &readable, NULL, NULL, &tv);
556                 if (r >= 0 || errno != EINTR) break;
557             }
558             if (r < 0) {
559                 perror("select (waitForPADO)");
560                 return;
561             }
562             if (r == 0) return;        /* Timed out */
563         }
564         
565         /* Get the packet */
566         receivePacket(conn->discoverySocket, &packet, &len);
567
568         /* Check length */
569         if (ntohs(packet.length) + HDR_SIZE > len) {
570             fprintf(stderr, "Bogus PPPoE length field (%u)\n",
571                    (unsigned int) ntohs(packet.length));
572             continue;
573         }
574
575 #ifdef USE_BPF
576         /* If it's not a Discovery packet, loop again */
577         if (etherType(&packet) != Eth_PPPOE_Discovery) continue;
578 #endif
579
580         if (conn->debugFile) {
581             dumpPacket(conn->debugFile, &packet, "RCVD");
582             fprintf(conn->debugFile, "\n");
583             fflush(conn->debugFile);
584         }
585         /* If it's not for us, loop again */
586         if (!packetIsForMe(conn, &packet)) continue;
587
588         if (packet.code == CODE_PADO) {
589             if (BROADCAST(packet.ethHdr.h_source)) {
590                 fprintf(stderr, "Ignoring PADO packet from broadcast MAC address\n");
591                 continue;
592             }
593             parsePacket(&packet, parsePADOTags, &pc);
594             if (conn->error)
595                 return;
596             if (!pc.seenACName) {
597                 fprintf(stderr, "Ignoring PADO packet with no AC-Name tag\n");
598                 continue;
599             }
600             if (!pc.seenServiceName) {
601                 fprintf(stderr, "Ignoring PADO packet with no Service-Name tag\n");
602                 continue;
603             }
604             conn->numPADOs++;
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");
616                     continue;
617                 }
618                 conn->discoveryState = STATE_RECEIVED_PADO;
619                 break;
620             }
621         }
622     } while (conn->discoveryState != STATE_RECEIVED_PADO);
623 }
624
625 /**********************************************************************
626 *%FUNCTION: discovery
627 *%ARGUMENTS:
628 * conn -- PPPoE connection info structure
629 *%RETURNS:
630 * Nothing
631 *%DESCRIPTION:
632 * Performs the PPPoE discovery phase
633 ***********************************************************************/
634 void
635 discovery(PPPoEConnection *conn)
636 {
637     int padiAttempts = 0;
638     int timeout = PADI_TIMEOUT;
639
640     conn->discoverySocket =
641         openInterface(conn->ifName, Eth_PPPOE_Discovery, conn->myEth);
642
643     do {
644         padiAttempts++;
645         if (padiAttempts > MAX_PADI_ATTEMPTS) {
646             fprintf(stderr, "Timeout waiting for PADO packets\n");
647             close(conn->discoverySocket);
648             conn->discoverySocket = -1;
649             return;
650         }
651         sendPADI(conn);
652         conn->discoveryState = STATE_SENT_PADI;
653         waitForPADO(conn, timeout);
654     } while (!conn->numPADOs);
655 }
656
657 int main(int argc, char *argv[])
658 {
659     int opt;
660     PPPoEConnection *conn;
661
662     conn = malloc(sizeof(PPPoEConnection));
663     if (!conn)
664         fatalSys("malloc");
665
666     memset(conn, 0, sizeof(PPPoEConnection));
667
668     conn->printACNames = 1;
669
670     while ((opt = getopt(argc, argv, "I:D:VUQS:C:h")) > 0) {
671         switch(opt) {
672         case 'S':
673             conn->serviceName = xstrdup(optarg);
674             break;
675         case 'C':
676             conn->acName = xstrdup(optarg);
677             break;
678         case 'U':
679             conn->useHostUniq = 1;
680             break;
681         case 'D':
682             conn->debugFile = fopen(optarg, "w");
683             if (!conn->debugFile) {
684                 fprintf(stderr, "Could not open %s: %s\n",
685                         optarg, strerror(errno));
686                 exit(1);
687             }
688             fprintf(conn->debugFile, "pppoe-discovery %s\n", RP_VERSION);
689             break;
690         case 'I':
691             conn->ifName = xstrdup(optarg);
692             break;
693         case 'Q':
694             conn->printACNames = 0;
695             break;
696         case 'V':
697         case 'h':
698             usage();
699             exit(0);
700         default:
701             usage();
702             exit(1);
703         }
704     }
705
706     /* default interface name */
707     if (!conn->ifName)
708         conn->ifName = strdup("eth0");
709
710     conn->discoverySocket = -1;
711     conn->sessionSocket = -1;
712
713     discovery(conn);
714
715     if (!conn->numPADOs)
716         exit(1);
717     else
718         exit(0);
719 }
720
721 void rp_fatal(char const *str)
722 {
723     fprintf(stderr, "%s\n", str);
724     exit(1);
725 }
726
727 void fatalSys(char const *str)
728 {
729     perror(str);
730     exit(1);
731 }
732
733 void sysErr(char const *str)
734 {
735     rp_fatal(str);
736 }
737
738 char *xstrdup(const char *s)
739 {
740     register char *ret = strdup(s);
741     if (!ret)
742         sysErr("strdup");
743     return ret;
744 }
745
746 void usage(void)
747 {
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");
752     fprintf(stderr,
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");
760 }