]> git.ozlabs.org Git - ppp.git/blob - pppd/plugins/rp-pppoe/pppoe-discovery.c
5afb037a1c74c74448851f6c75a1218810a912db
[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, IFNAMSIZ);
185     ifr.ifr_name[IFNAMSIZ - 1] = 0;
186     if (ioctl(fd, SIOCGIFINDEX, &ifr) < 0) {
187         fatalSys("ioctl(SIOCFIGINDEX): Could not get interface index");
188     }
189     sa.sll_ifindex = ifr.ifr_ifindex;
190
191 #else
192     strcpy(sa.sa_data, ifname);
193 #endif
194
195     /* We're only interested in packets on specified interface */
196     if (bind(fd, (struct sockaddr *) &sa, sizeof(sa)) < 0) {
197         fatalSys("bind");
198     }
199
200     return fd;
201 }
202
203
204 /***********************************************************************
205 *%FUNCTION: sendPacket
206 *%ARGUMENTS:
207 * sock -- socket to send to
208 * pkt -- the packet to transmit
209 * size -- size of packet (in bytes)
210 *%RETURNS:
211 * 0 on success; -1 on failure
212 *%DESCRIPTION:
213 * Transmits a packet
214 ***********************************************************************/
215 int
216 sendPacket(PPPoEConnection *conn, int sock, PPPoEPacket *pkt, int size)
217 {
218 #if defined(HAVE_STRUCT_SOCKADDR_LL)
219     if (send(sock, pkt, size, 0) < 0) {
220         sysErr("send (sendPacket)");
221         return -1;
222     }
223 #else
224     struct sockaddr sa;
225
226     if (!conn) {
227         rp_fatal("relay and server not supported on Linux 2.0 kernels");
228     }
229     strcpy(sa.sa_data, conn->ifName);
230     if (sendto(sock, pkt, size, 0, &sa, sizeof(sa)) < 0) {
231         sysErr("sendto (sendPacket)");
232         return -1;
233     }
234 #endif
235     return 0;
236 }
237
238 /***********************************************************************
239 *%FUNCTION: receivePacket
240 *%ARGUMENTS:
241 * sock -- socket to read from
242 * pkt -- place to store the received packet
243 * size -- set to size of packet in bytes
244 *%RETURNS:
245 * >= 0 if all OK; < 0 if error
246 *%DESCRIPTION:
247 * Receives a packet
248 ***********************************************************************/
249 int
250 receivePacket(int sock, PPPoEPacket *pkt, int *size)
251 {
252     if ((*size = recv(sock, pkt, sizeof(PPPoEPacket), 0)) < 0) {
253         sysErr("recv (receivePacket)");
254         return -1;
255     }
256     return 0;
257 }
258
259 /**********************************************************************
260 *%FUNCTION: parsePacket
261 *%ARGUMENTS:
262 * packet -- the PPPoE discovery packet to parse
263 * func -- function called for each tag in the packet
264 * extra -- an opaque data pointer supplied to parsing function
265 *%RETURNS:
266 * 0 if everything went well; -1 if there was an error
267 *%DESCRIPTION:
268 * Parses a PPPoE discovery packet, calling "func" for each tag in the packet.
269 * "func" is passed the additional argument "extra".
270 ***********************************************************************/
271 int
272 parsePacket(PPPoEPacket *packet, ParseFunc *func, void *extra)
273 {
274     UINT16_t len = ntohs(packet->length);
275     unsigned char *curTag;
276     UINT16_t tagType, tagLen;
277
278     if (PPPOE_VER(packet->vertype) != 1) {
279         fprintf(stderr, "Invalid PPPoE version (%d)\n",
280                 PPPOE_VER(packet->vertype));
281         return -1;
282     }
283     if (PPPOE_TYPE(packet->vertype) != 1) {
284         fprintf(stderr, "Invalid PPPoE type (%d)\n",
285                 PPPOE_TYPE(packet->vertype));
286         return -1;
287     }
288
289     /* Do some sanity checks on packet */
290     if (len > ETH_JUMBO_LEN - PPPOE_OVERHEAD) { /* 6-byte overhead for PPPoE header */
291         fprintf(stderr, "Invalid PPPoE packet length (%u)\n", len);
292         return -1;
293     }
294
295     /* Step through the tags */
296     curTag = packet->payload;
297     while(curTag - packet->payload < len) {
298         /* Alignment is not guaranteed, so do this by hand... */
299         tagType = (curTag[0] << 8) + curTag[1];
300         tagLen = (curTag[2] << 8) + curTag[3];
301         if (tagType == TAG_END_OF_LIST) {
302             return 0;
303         }
304         if ((curTag - packet->payload) + tagLen + TAG_HDR_SIZE > len) {
305             fprintf(stderr, "Invalid PPPoE tag length (%u)\n", tagLen);
306             return -1;
307         }
308         func(tagType, tagLen, curTag+TAG_HDR_SIZE, extra);
309         curTag = curTag + TAG_HDR_SIZE + tagLen;
310     }
311     return 0;
312 }
313
314 /**********************************************************************
315 *%FUNCTION: parseForHostUniq
316 *%ARGUMENTS:
317 * type -- tag type
318 * len -- tag length
319 * data -- tag data.
320 * extra -- user-supplied pointer.  This is assumed to be a pointer to int.
321 *%RETURNS:
322 * Nothing
323 *%DESCRIPTION:
324 * If a HostUnique tag is found which matches our PID, sets *extra to 1.
325 ***********************************************************************/
326 void
327 parseForHostUniq(UINT16_t type, UINT16_t len, unsigned char *data,
328                  void *extra)
329 {
330     int *val = (int *) extra;
331     if (type == TAG_HOST_UNIQ && len == sizeof(pid_t)) {
332         pid_t tmp;
333         memcpy(&tmp, data, len);
334         if (tmp == getpid()) {
335             *val = 1;
336         }
337     }
338 }
339
340 /**********************************************************************
341 *%FUNCTION: packetIsForMe
342 *%ARGUMENTS:
343 * conn -- PPPoE connection info
344 * packet -- a received PPPoE packet
345 *%RETURNS:
346 * 1 if packet is for this PPPoE daemon; 0 otherwise.
347 *%DESCRIPTION:
348 * If we are using the Host-Unique tag, verifies that packet contains
349 * our unique identifier.
350 ***********************************************************************/
351 int
352 packetIsForMe(PPPoEConnection *conn, PPPoEPacket *packet)
353 {
354     int forMe = 0;
355
356     /* If packet is not directed to our MAC address, forget it */
357     if (memcmp(packet->ethHdr.h_dest, conn->myEth, ETH_ALEN)) return 0;
358
359     /* If we're not using the Host-Unique tag, then accept the packet */
360     if (!conn->useHostUniq) return 1;
361
362     parsePacket(packet, parseForHostUniq, &forMe);
363     return forMe;
364 }
365
366 /**********************************************************************
367 *%FUNCTION: parsePADOTags
368 *%ARGUMENTS:
369 * type -- tag type
370 * len -- tag length
371 * data -- tag data
372 * extra -- extra user data.  Should point to a PacketCriteria structure
373 *          which gets filled in according to selected AC name and service
374 *          name.
375 *%RETURNS:
376 * Nothing
377 *%DESCRIPTION:
378 * Picks interesting tags out of a PADO packet
379 ***********************************************************************/
380 void
381 parsePADOTags(UINT16_t type, UINT16_t len, unsigned char *data,
382               void *extra)
383 {
384     struct PacketCriteria *pc = (struct PacketCriteria *) extra;
385     PPPoEConnection *conn = pc->conn;
386     int i;
387
388     switch(type) {
389     case TAG_AC_NAME:
390         pc->seenACName = 1;
391         if (conn->printACNames) {
392             printf("Access-Concentrator: %.*s\n", (int) len, data);
393         }
394         if (conn->acName && len == strlen(conn->acName) &&
395             !strncmp((char *) data, conn->acName, len)) {
396             pc->acNameOK = 1;
397         }
398         break;
399     case TAG_SERVICE_NAME:
400         pc->seenServiceName = 1;
401         if (conn->printACNames && len > 0) {
402             printf("       Service-Name: %.*s\n", (int) len, data);
403         }
404         if (conn->serviceName && len == strlen(conn->serviceName) &&
405             !strncmp((char *) data, conn->serviceName, len)) {
406             pc->serviceNameOK = 1;
407         }
408         break;
409     case TAG_AC_COOKIE:
410         if (conn->printACNames) {
411             printf("Got a cookie:");
412             /* Print first 20 bytes of cookie */
413             for (i=0; i<len && i < 20; i++) {
414                 printf(" %02x", (unsigned) data[i]);
415             }
416             if (i < len) printf("...");
417             printf("\n");
418         }
419         conn->cookie.type = htons(type);
420         conn->cookie.length = htons(len);
421         memcpy(conn->cookie.payload, data, len);
422         break;
423     case TAG_RELAY_SESSION_ID:
424         if (conn->printACNames) {
425             printf("Got a Relay-ID:");
426             /* Print first 20 bytes of relay ID */
427             for (i=0; i<len && i < 20; i++) {
428                 printf(" %02x", (unsigned) data[i]);
429             }
430             if (i < len) printf("...");
431             printf("\n");
432         }
433         conn->relayId.type = htons(type);
434         conn->relayId.length = htons(len);
435         memcpy(conn->relayId.payload, data, len);
436         break;
437     case TAG_SERVICE_NAME_ERROR:
438         if (conn->printACNames) {
439             printf("Got a Service-Name-Error tag: %.*s\n", (int) len, data);
440         }
441         break;
442     case TAG_AC_SYSTEM_ERROR:
443         if (conn->printACNames) {
444             printf("Got a System-Error tag: %.*s\n", (int) len, data);
445         }
446         break;
447     case TAG_GENERIC_ERROR:
448         if (conn->printACNames) {
449             printf("Got a Generic-Error tag: %.*s\n", (int) len, data);
450         }
451         break;
452     }
453 }
454
455 /***********************************************************************
456 *%FUNCTION: sendPADI
457 *%ARGUMENTS:
458 * conn -- PPPoEConnection structure
459 *%RETURNS:
460 * Nothing
461 *%DESCRIPTION:
462 * Sends a PADI packet
463 ***********************************************************************/
464 void
465 sendPADI(PPPoEConnection *conn)
466 {
467     PPPoEPacket packet;
468     unsigned char *cursor = packet.payload;
469     PPPoETag *svc = (PPPoETag *) (&packet.payload);
470     UINT16_t namelen = 0;
471     UINT16_t plen;
472
473     if (conn->serviceName) {
474         namelen = (UINT16_t) strlen(conn->serviceName);
475     }
476     plen = TAG_HDR_SIZE + namelen;
477     CHECK_ROOM(cursor, packet.payload, plen);
478
479     /* Set destination to Ethernet broadcast address */
480     memset(packet.ethHdr.h_dest, 0xFF, ETH_ALEN);
481     memcpy(packet.ethHdr.h_source, conn->myEth, ETH_ALEN);
482
483     packet.ethHdr.h_proto = htons(Eth_PPPOE_Discovery);
484     packet.vertype = PPPOE_VER_TYPE(1, 1);
485     packet.code = CODE_PADI;
486     packet.session = 0;
487
488     svc->type = TAG_SERVICE_NAME;
489     svc->length = htons(namelen);
490     CHECK_ROOM(cursor, packet.payload, namelen+TAG_HDR_SIZE);
491
492     if (conn->serviceName) {
493         memcpy(svc->payload, conn->serviceName, strlen(conn->serviceName));
494     }
495     cursor += namelen + TAG_HDR_SIZE;
496
497     /* If we're using Host-Uniq, copy it over */
498     if (conn->useHostUniq) {
499         PPPoETag hostUniq;
500         pid_t pid = getpid();
501         hostUniq.type = htons(TAG_HOST_UNIQ);
502         hostUniq.length = htons(sizeof(pid));
503         memcpy(hostUniq.payload, &pid, sizeof(pid));
504         CHECK_ROOM(cursor, packet.payload, sizeof(pid) + TAG_HDR_SIZE);
505         memcpy(cursor, &hostUniq, sizeof(pid) + TAG_HDR_SIZE);
506         cursor += sizeof(pid) + TAG_HDR_SIZE;
507         plen += sizeof(pid) + TAG_HDR_SIZE;
508     }
509
510     packet.length = htons(plen);
511
512     sendPacket(conn, conn->discoverySocket, &packet, (int) (plen + HDR_SIZE));
513     if (conn->debugFile) {
514         dumpPacket(conn->debugFile, &packet, "SENT");
515         fprintf(conn->debugFile, "\n");
516         fflush(conn->debugFile);
517     }
518 }
519
520 /**********************************************************************
521 *%FUNCTION: waitForPADO
522 *%ARGUMENTS:
523 * conn -- PPPoEConnection structure
524 * timeout -- how long to wait (in seconds)
525 *%RETURNS:
526 * Nothing
527 *%DESCRIPTION:
528 * Waits for a PADO packet and copies useful information
529 ***********************************************************************/
530 void
531 waitForPADO(PPPoEConnection *conn, int timeout)
532 {
533     fd_set readable;
534     int r;
535     struct timeval tv;
536     PPPoEPacket packet;
537     int len;
538
539     struct PacketCriteria pc;
540     pc.conn          = conn;
541     pc.acNameOK      = (conn->acName)      ? 0 : 1;
542     pc.serviceNameOK = (conn->serviceName) ? 0 : 1;
543     pc.seenACName    = 0;
544     pc.seenServiceName = 0;
545     conn->error = 0;
546         
547     do {
548         if (BPF_BUFFER_IS_EMPTY) {
549             tv.tv_sec = timeout;
550             tv.tv_usec = 0;
551         
552             FD_ZERO(&readable);
553             FD_SET(conn->discoverySocket, &readable);
554
555             while(1) {
556                 r = select(conn->discoverySocket+1, &readable, NULL, NULL, &tv);
557                 if (r >= 0 || errno != EINTR) break;
558             }
559             if (r < 0) {
560                 perror("select (waitForPADO)");
561                 return;
562             }
563             if (r == 0) return;        /* Timed out */
564         }
565         
566         /* Get the packet */
567         receivePacket(conn->discoverySocket, &packet, &len);
568
569         /* Check length */
570         if (ntohs(packet.length) + HDR_SIZE > len) {
571             fprintf(stderr, "Bogus PPPoE length field (%u)\n",
572                    (unsigned int) ntohs(packet.length));
573             continue;
574         }
575
576 #ifdef USE_BPF
577         /* If it's not a Discovery packet, loop again */
578         if (etherType(&packet) != Eth_PPPOE_Discovery) continue;
579 #endif
580
581         if (conn->debugFile) {
582             dumpPacket(conn->debugFile, &packet, "RCVD");
583             fprintf(conn->debugFile, "\n");
584             fflush(conn->debugFile);
585         }
586         /* If it's not for us, loop again */
587         if (!packetIsForMe(conn, &packet)) continue;
588
589         if (packet.code == CODE_PADO) {
590             if (BROADCAST(packet.ethHdr.h_source)) {
591                 fprintf(stderr, "Ignoring PADO packet from broadcast MAC address\n");
592                 continue;
593             }
594             parsePacket(&packet, parsePADOTags, &pc);
595             if (conn->error)
596                 return;
597             if (!pc.seenACName) {
598                 fprintf(stderr, "Ignoring PADO packet with no AC-Name tag\n");
599                 continue;
600             }
601             if (!pc.seenServiceName) {
602                 fprintf(stderr, "Ignoring PADO packet with no Service-Name tag\n");
603                 continue;
604             }
605             conn->numPADOs++;
606             if (pc.acNameOK && pc.serviceNameOK) {
607                 memcpy(conn->peerEth, packet.ethHdr.h_source, ETH_ALEN);
608                 if (conn->printACNames) {
609                     printf("AC-Ethernet-Address: %02x:%02x:%02x:%02x:%02x:%02x\n",
610                            (unsigned) conn->peerEth[0], 
611                            (unsigned) conn->peerEth[1],
612                            (unsigned) conn->peerEth[2],
613                            (unsigned) conn->peerEth[3],
614                            (unsigned) conn->peerEth[4],
615                            (unsigned) conn->peerEth[5]);
616                     printf("--------------------------------------------------\n");
617                     continue;
618                 }
619                 conn->discoveryState = STATE_RECEIVED_PADO;
620                 break;
621             }
622         }
623     } while (conn->discoveryState != STATE_RECEIVED_PADO);
624 }
625
626 /**********************************************************************
627 *%FUNCTION: discovery
628 *%ARGUMENTS:
629 * conn -- PPPoE connection info structure
630 *%RETURNS:
631 * Nothing
632 *%DESCRIPTION:
633 * Performs the PPPoE discovery phase
634 ***********************************************************************/
635 void
636 discovery(PPPoEConnection *conn)
637 {
638     int padiAttempts = 0;
639     int timeout = conn->discoveryTimeout;
640
641     conn->discoverySocket =
642         openInterface(conn->ifName, Eth_PPPOE_Discovery, conn->myEth);
643
644     do {
645         padiAttempts++;
646         if (padiAttempts > conn->discoveryAttempts) {
647             fprintf(stderr, "Timeout waiting for PADO packets\n");
648             close(conn->discoverySocket);
649             conn->discoverySocket = -1;
650             return;
651         }
652         sendPADI(conn);
653         conn->discoveryState = STATE_SENT_PADI;
654         waitForPADO(conn, timeout);
655     } while (!conn->numPADOs);
656 }
657
658 int main(int argc, char *argv[])
659 {
660     int opt;
661     PPPoEConnection *conn;
662
663     conn = malloc(sizeof(PPPoEConnection));
664     if (!conn)
665         fatalSys("malloc");
666
667     memset(conn, 0, sizeof(PPPoEConnection));
668
669     conn->printACNames = 1;
670     conn->discoveryTimeout = PADI_TIMEOUT;
671     conn->discoveryAttempts = MAX_PADI_ATTEMPTS;
672
673     while ((opt = getopt(argc, argv, "I:D:VUQS:C:t:a:h")) > 0) {
674         switch(opt) {
675         case 'S':
676             conn->serviceName = xstrdup(optarg);
677             break;
678         case 'C':
679             conn->acName = xstrdup(optarg);
680             break;
681         case 't':
682             if (sscanf(optarg, "%d", &conn->discoveryTimeout) != 1) {
683                 fprintf(stderr, "Illegal argument to -t: Should be -t timeout\n");
684                 exit(EXIT_FAILURE);
685             }
686             if (conn->discoveryTimeout < 1) {
687                 conn->discoveryTimeout = 1;
688             }
689             break;
690         case 'a':
691             if (sscanf(optarg, "%d", &conn->discoveryAttempts) != 1) {
692                 fprintf(stderr, "Illegal argument to -a: Should be -a attempts\n");
693                 exit(EXIT_FAILURE);
694             }
695             if (conn->discoveryAttempts < 1) {
696                 conn->discoveryAttempts = 1;
697             }
698             break;
699         case 'U':
700             conn->useHostUniq = 1;
701             break;
702         case 'D':
703             conn->debugFile = fopen(optarg, "w");
704             if (!conn->debugFile) {
705                 fprintf(stderr, "Could not open %s: %s\n",
706                         optarg, strerror(errno));
707                 exit(1);
708             }
709             fprintf(conn->debugFile, "pppoe-discovery %s\n", RP_VERSION);
710             break;
711         case 'I':
712             conn->ifName = xstrdup(optarg);
713             break;
714         case 'Q':
715             conn->printACNames = 0;
716             break;
717         case 'V':
718         case 'h':
719             usage();
720             exit(0);
721         default:
722             usage();
723             exit(1);
724         }
725     }
726
727     /* default interface name */
728     if (!conn->ifName)
729         conn->ifName = strdup("eth0");
730
731     conn->discoverySocket = -1;
732     conn->sessionSocket = -1;
733
734     discovery(conn);
735
736     if (!conn->numPADOs)
737         exit(1);
738     else
739         exit(0);
740 }
741
742 void rp_fatal(char const *str)
743 {
744     fprintf(stderr, "%s\n", str);
745     exit(1);
746 }
747
748 void fatalSys(char const *str)
749 {
750     perror(str);
751     exit(1);
752 }
753
754 void sysErr(char const *str)
755 {
756     rp_fatal(str);
757 }
758
759 char *xstrdup(const char *s)
760 {
761     register char *ret = strdup(s);
762     if (!ret)
763         sysErr("strdup");
764     return ret;
765 }
766
767 void usage(void)
768 {
769     fprintf(stderr, "Usage: pppoe-discovery [options]\n");
770     fprintf(stderr, "Options:\n");
771     fprintf(stderr, "   -I if_name     -- Specify interface (default eth0)\n");
772     fprintf(stderr, "   -D filename    -- Log debugging information in filename.\n");
773     fprintf(stderr,
774             "   -t timeout     -- Initial timeout for discovery packets in seconds\n"
775             "   -a attempts    -- Number of discovery attempts\n"
776             "   -V             -- Print version and exit.\n"
777             "   -Q             -- Quit Mode: Do not print access concentrator names\n"
778             "   -S name        -- Set desired service name.\n"
779             "   -C name        -- Set desired access concentrator name.\n"
780             "   -U             -- Use Host-Unique to allow multiple PPPoE sessions.\n"
781             "   -h             -- Print usage information.\n");
782     fprintf(stderr, "\nVersion " RP_VERSION "\n");
783 }