]> git.ozlabs.org Git - ppp.git/blob - pppd/plugins/pppoe/pppoe-discovery.c
34b77e8c27217af9924d7f8cbb7c78966c688065
[ppp.git] / pppd / plugins / 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 #include <time.h>
19
20 #include "pppoe.h"
21
22 #ifdef HAVE_UNISTD_H
23 #include <unistd.h>
24 #endif
25
26 #ifdef HAVE_NETPACKET_PACKET_H
27 #include <netpacket/packet.h>
28 #elif defined(HAVE_LINUX_IF_PACKET_H)
29 #include <linux/if_packet.h>
30 #endif
31
32 #ifdef HAVE_ASM_TYPES_H
33 #include <asm/types.h>
34 #endif
35
36 #ifdef HAVE_SYS_IOCTL_H
37 #include <sys/ioctl.h>
38 #endif
39
40 #include <errno.h>
41 #include <stdlib.h>
42 #include <string.h>
43
44 #ifdef HAVE_NET_IF_ARP_H
45 #include <net/if_arp.h>
46 #endif
47
48 char *xstrdup(const char *s);
49 void usage(void);
50
51 void die(int status)
52 {
53         exit(status);
54 }
55
56 void error(char *fmt, ...)
57 {
58     va_list pvar;
59     va_start(pvar, fmt);
60     vfprintf(stderr, fmt, pvar);
61     va_end(pvar);
62 }
63
64 /* Initialize frame types to RFC 2516 values.  Some broken peers apparently
65    use different frame types... sigh... */
66
67 UINT16_t Eth_PPPOE_Discovery = ETH_PPPOE_DISCOVERY;
68 UINT16_t Eth_PPPOE_Session   = ETH_PPPOE_SESSION;
69
70 /**********************************************************************
71 *%FUNCTION: etherType
72 *%ARGUMENTS:
73 * packet -- a received PPPoE packet
74 *%RETURNS:
75 * ethernet packet type (see /usr/include/net/ethertypes.h)
76 *%DESCRIPTION:
77 * Checks the ethernet packet header to determine its type.
78 * We should only be receveing DISCOVERY and SESSION types if the BPF
79 * is set up correctly.  Logs an error if an unexpected type is received.
80 * Note that the ethernet type names come from "pppoe.h" and the packet
81 * packet structure names use the LINUX dialect to maintain consistency
82 * with the rest of this file.  See the BSD section of "pppoe.h" for
83 * translations of the data structure names.
84 ***********************************************************************/
85 UINT16_t
86 etherType(PPPoEPacket *packet)
87 {
88     UINT16_t type = (UINT16_t) ntohs(packet->ethHdr.h_proto);
89     if (type != Eth_PPPOE_Discovery && type != Eth_PPPOE_Session) {
90         fprintf(stderr, "Invalid ether type 0x%x\n", type);
91     }
92     return type;
93 }
94
95 /**********************************************************************
96 *%FUNCTION: openInterface
97 *%ARGUMENTS:
98 * ifname -- name of interface
99 * type -- Ethernet frame type
100 * hwaddr -- if non-NULL, set to the hardware address
101 *%RETURNS:
102 * A raw socket for talking to the Ethernet card.  Exits on error.
103 *%DESCRIPTION:
104 * Opens a raw Ethernet socket
105 ***********************************************************************/
106 int
107 openInterface(char const *ifname, UINT16_t type, unsigned char *hwaddr)
108 {
109     int optval=1;
110     int fd;
111     struct ifreq ifr;
112     int domain, stype;
113
114 #ifdef HAVE_STRUCT_SOCKADDR_LL
115     struct sockaddr_ll sa;
116 #else
117     struct sockaddr sa;
118 #endif
119
120     memset(&sa, 0, sizeof(sa));
121
122 #ifdef HAVE_STRUCT_SOCKADDR_LL
123     domain = PF_PACKET;
124     stype = SOCK_RAW;
125 #else
126     domain = PF_INET;
127     stype = SOCK_PACKET;
128 #endif
129
130     if ((fd = socket(domain, stype, htons(type))) < 0) {
131         /* Give a more helpful message for the common error case */
132         if (errno == EPERM) {
133             rp_fatal("Cannot create raw socket -- pppoe must be run as root.");
134         }
135         fatalSys("socket");
136     }
137
138     if (setsockopt(fd, SOL_SOCKET, SO_BROADCAST, &optval, sizeof(optval)) < 0) {
139         fatalSys("setsockopt");
140     }
141
142     /* Fill in hardware address */
143     if (hwaddr) {
144         strncpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
145         if (ioctl(fd, SIOCGIFHWADDR, &ifr) < 0) {
146             fatalSys("ioctl(SIOCGIFHWADDR)");
147         }
148         memcpy(hwaddr, ifr.ifr_hwaddr.sa_data, ETH_ALEN);
149 #ifdef ARPHRD_ETHER
150         if (ifr.ifr_hwaddr.sa_family != ARPHRD_ETHER) {
151             char buffer[256];
152             sprintf(buffer, "Interface %.16s is not Ethernet", ifname);
153             rp_fatal(buffer);
154         }
155 #endif
156         if (NOT_UNICAST(hwaddr)) {
157             char buffer[256];
158             sprintf(buffer,
159                     "Interface %.16s has broadcast/multicast MAC address??",
160                     ifname);
161             rp_fatal(buffer);
162         }
163     }
164
165     /* Sanity check on MTU */
166     strncpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
167     if (ioctl(fd, SIOCGIFMTU, &ifr) < 0) {
168         fatalSys("ioctl(SIOCGIFMTU)");
169     }
170     if (ifr.ifr_mtu < ETH_DATA_LEN) {
171         fprintf(stderr, "Interface %.16s has MTU of %d -- should be %d.\n",
172               ifname, ifr.ifr_mtu, ETH_DATA_LEN);
173         fprintf(stderr, "You may have serious connection problems.\n");
174     }
175
176 #ifdef HAVE_STRUCT_SOCKADDR_LL
177     /* Get interface index */
178     sa.sll_family = AF_PACKET;
179     sa.sll_protocol = htons(type);
180
181     strncpy(ifr.ifr_name, ifname, IFNAMSIZ);
182     ifr.ifr_name[IFNAMSIZ - 1] = 0;
183     if (ioctl(fd, SIOCGIFINDEX, &ifr) < 0) {
184         fatalSys("ioctl(SIOCFIGINDEX): Could not get interface index");
185     }
186     sa.sll_ifindex = ifr.ifr_ifindex;
187
188 #else
189     strcpy(sa.sa_data, ifname);
190 #endif
191
192     /* We're only interested in packets on specified interface */
193     if (bind(fd, (struct sockaddr *) &sa, sizeof(sa)) < 0) {
194         fatalSys("bind");
195     }
196
197     return fd;
198 }
199
200
201 /***********************************************************************
202 *%FUNCTION: sendPacket
203 *%ARGUMENTS:
204 * sock -- socket to send to
205 * pkt -- the packet to transmit
206 * size -- size of packet (in bytes)
207 *%RETURNS:
208 * 0 on success; -1 on failure
209 *%DESCRIPTION:
210 * Transmits a packet
211 ***********************************************************************/
212 int
213 sendPacket(PPPoEConnection *conn, int sock, PPPoEPacket *pkt, int size)
214 {
215 #if defined(HAVE_STRUCT_SOCKADDR_LL)
216     if (send(sock, pkt, size, 0) < 0) {
217         sysErr("send (sendPacket)");
218         return -1;
219     }
220 #else
221     struct sockaddr sa;
222
223     if (!conn) {
224         rp_fatal("relay and server not supported on Linux 2.0 kernels");
225     }
226     strcpy(sa.sa_data, conn->ifName);
227     if (sendto(sock, pkt, size, 0, &sa, sizeof(sa)) < 0) {
228         sysErr("sendto (sendPacket)");
229         return -1;
230     }
231 #endif
232     return 0;
233 }
234
235 /***********************************************************************
236 *%FUNCTION: receivePacket
237 *%ARGUMENTS:
238 * sock -- socket to read from
239 * pkt -- place to store the received packet
240 * size -- set to size of packet in bytes
241 *%RETURNS:
242 * >= 0 if all OK; < 0 if error
243 *%DESCRIPTION:
244 * Receives a packet
245 ***********************************************************************/
246 int
247 receivePacket(int sock, PPPoEPacket *pkt, int *size)
248 {
249     if ((*size = recv(sock, pkt, sizeof(PPPoEPacket), 0)) < 0) {
250         sysErr("recv (receivePacket)");
251         return -1;
252     }
253     return 0;
254 }
255
256 /**********************************************************************
257 *%FUNCTION: parsePacket
258 *%ARGUMENTS:
259 * packet -- the PPPoE discovery packet to parse
260 * func -- function called for each tag in the packet
261 * extra -- an opaque data pointer supplied to parsing function
262 *%RETURNS:
263 * 0 if everything went well; -1 if there was an error
264 *%DESCRIPTION:
265 * Parses a PPPoE discovery packet, calling "func" for each tag in the packet.
266 * "func" is passed the additional argument "extra".
267 ***********************************************************************/
268 int
269 parsePacket(PPPoEPacket *packet, ParseFunc *func, void *extra)
270 {
271     UINT16_t len = ntohs(packet->length);
272     unsigned char *curTag;
273     UINT16_t tagType, tagLen;
274
275     if (PPPOE_VER(packet->vertype) != 1) {
276         fprintf(stderr, "Invalid PPPoE version (%d)\n",
277                 PPPOE_VER(packet->vertype));
278         return -1;
279     }
280     if (PPPOE_TYPE(packet->vertype) != 1) {
281         fprintf(stderr, "Invalid PPPoE type (%d)\n",
282                 PPPOE_TYPE(packet->vertype));
283         return -1;
284     }
285
286     /* Do some sanity checks on packet */
287     if (len > ETH_JUMBO_LEN - PPPOE_OVERHEAD) { /* 6-byte overhead for PPPoE header */
288         fprintf(stderr, "Invalid PPPoE packet length (%u)\n", len);
289         return -1;
290     }
291
292     /* Step through the tags */
293     curTag = packet->payload;
294     while(curTag - packet->payload < len) {
295         /* Alignment is not guaranteed, so do this by hand... */
296         tagType = (curTag[0] << 8) + curTag[1];
297         tagLen = (curTag[2] << 8) + curTag[3];
298         if (tagType == TAG_END_OF_LIST) {
299             return 0;
300         }
301         if ((curTag - packet->payload) + tagLen + TAG_HDR_SIZE > len) {
302             fprintf(stderr, "Invalid PPPoE tag length (%u)\n", tagLen);
303             return -1;
304         }
305         func(tagType, tagLen, curTag+TAG_HDR_SIZE, extra);
306         curTag = curTag + TAG_HDR_SIZE + tagLen;
307     }
308     return 0;
309 }
310
311 /**********************************************************************
312 *%FUNCTION: parseForHostUniq
313 *%ARGUMENTS:
314 * type -- tag type
315 * len -- tag length
316 * data -- tag data.
317 * extra -- user-supplied pointer.  This is assumed to be a pointer to int.
318 *%RETURNS:
319 * Nothing
320 *%DESCRIPTION:
321 * If a HostUnique tag is found which matches our PID, sets *extra to 1.
322 ***********************************************************************/
323 void
324 parseForHostUniq(UINT16_t type, UINT16_t len, unsigned char *data,
325                  void *extra)
326 {
327     PPPoETag *tag = extra;
328
329     if (type == TAG_HOST_UNIQ && len == ntohs(tag->length))
330         tag->length = memcmp(data, tag->payload, len);
331 }
332
333 /**********************************************************************
334 *%FUNCTION: packetIsForMe
335 *%ARGUMENTS:
336 * conn -- PPPoE connection info
337 * packet -- a received PPPoE packet
338 *%RETURNS:
339 * 1 if packet is for this PPPoE daemon; 0 otherwise.
340 *%DESCRIPTION:
341 * If we are using the Host-Unique tag, verifies that packet contains
342 * our unique identifier.
343 ***********************************************************************/
344 int
345 packetIsForMe(PPPoEConnection *conn, PPPoEPacket *packet)
346 {
347     PPPoETag hostUniq = conn->hostUniq;
348
349     /* If packet is not directed to our MAC address, forget it */
350     if (memcmp(packet->ethHdr.h_dest, conn->myEth, ETH_ALEN)) return 0;
351
352     /* If we're not using the Host-Unique tag, then accept the packet */
353     if (!conn->hostUniq.length) return 1;
354
355     parsePacket(packet, parseForHostUniq, &hostUniq);
356     return !hostUniq.length;
357 }
358
359 /**********************************************************************
360 *%FUNCTION: parsePADOTags
361 *%ARGUMENTS:
362 * type -- tag type
363 * len -- tag length
364 * data -- tag data
365 * extra -- extra user data.  Should point to a PacketCriteria structure
366 *          which gets filled in according to selected AC name and service
367 *          name.
368 *%RETURNS:
369 * Nothing
370 *%DESCRIPTION:
371 * Picks interesting tags out of a PADO packet
372 ***********************************************************************/
373 void
374 parsePADOTags(UINT16_t type, UINT16_t len, unsigned char *data,
375               void *extra)
376 {
377     struct PacketCriteria *pc = (struct PacketCriteria *) extra;
378     PPPoEConnection *conn = pc->conn;
379     int i;
380
381     switch(type) {
382     case TAG_AC_NAME:
383         pc->seenACName = 1;
384         if (conn->printACNames) {
385             printf("Access-Concentrator: %.*s\n", (int) len, data);
386         }
387         if (conn->acName && len == strlen(conn->acName) &&
388             !strncmp((char *) data, conn->acName, len)) {
389             pc->acNameOK = 1;
390         }
391         break;
392     case TAG_SERVICE_NAME:
393         pc->seenServiceName = 1;
394         if (conn->printACNames && len > 0) {
395             printf("       Service-Name: %.*s\n", (int) len, data);
396         }
397         if (conn->serviceName && len == strlen(conn->serviceName) &&
398             !strncmp((char *) data, conn->serviceName, len)) {
399             pc->serviceNameOK = 1;
400         }
401         break;
402     case TAG_AC_COOKIE:
403         if (conn->printACNames) {
404             printf("Got a cookie:");
405             /* Print first 20 bytes of cookie */
406             for (i=0; i<len && i < 20; i++) {
407                 printf(" %02x", (unsigned) data[i]);
408             }
409             if (i < len) printf("...");
410             printf("\n");
411         }
412         conn->cookie.type = htons(type);
413         conn->cookie.length = htons(len);
414         memcpy(conn->cookie.payload, data, len);
415         break;
416     case TAG_RELAY_SESSION_ID:
417         if (conn->printACNames) {
418             printf("Got a Relay-ID:");
419             /* Print first 20 bytes of relay ID */
420             for (i=0; i<len && i < 20; i++) {
421                 printf(" %02x", (unsigned) data[i]);
422             }
423             if (i < len) printf("...");
424             printf("\n");
425         }
426         conn->relayId.type = htons(type);
427         conn->relayId.length = htons(len);
428         memcpy(conn->relayId.payload, data, len);
429         break;
430     case TAG_SERVICE_NAME_ERROR:
431         if (conn->printACNames) {
432             printf("Got a Service-Name-Error tag: %.*s\n", (int) len, data);
433         }
434         break;
435     case TAG_AC_SYSTEM_ERROR:
436         if (conn->printACNames) {
437             printf("Got a System-Error tag: %.*s\n", (int) len, data);
438         }
439         break;
440     case TAG_GENERIC_ERROR:
441         if (conn->printACNames) {
442             printf("Got a Generic-Error tag: %.*s\n", (int) len, data);
443         }
444         break;
445     }
446 }
447
448 /***********************************************************************
449 *%FUNCTION: sendPADI
450 *%ARGUMENTS:
451 * conn -- PPPoEConnection structure
452 *%RETURNS:
453 * Nothing
454 *%DESCRIPTION:
455 * Sends a PADI packet
456 ***********************************************************************/
457 void
458 sendPADI(PPPoEConnection *conn)
459 {
460     PPPoEPacket packet;
461     unsigned char *cursor = packet.payload;
462     PPPoETag *svc = (PPPoETag *) (&packet.payload);
463     UINT16_t namelen = 0;
464     UINT16_t plen;
465
466     if (conn->serviceName) {
467         namelen = (UINT16_t) strlen(conn->serviceName);
468     }
469     plen = TAG_HDR_SIZE + namelen;
470     CHECK_ROOM(cursor, packet.payload, plen);
471
472     /* Set destination to Ethernet broadcast address */
473     memset(packet.ethHdr.h_dest, 0xFF, ETH_ALEN);
474     memcpy(packet.ethHdr.h_source, conn->myEth, ETH_ALEN);
475
476     packet.ethHdr.h_proto = htons(Eth_PPPOE_Discovery);
477     packet.vertype = PPPOE_VER_TYPE(1, 1);
478     packet.code = CODE_PADI;
479     packet.session = 0;
480
481     svc->type = TAG_SERVICE_NAME;
482     svc->length = htons(namelen);
483     CHECK_ROOM(cursor, packet.payload, namelen+TAG_HDR_SIZE);
484
485     if (conn->serviceName) {
486         memcpy(svc->payload, conn->serviceName, strlen(conn->serviceName));
487     }
488     cursor += namelen + TAG_HDR_SIZE;
489
490     /* If we're using Host-Uniq, copy it over */
491     if (conn->hostUniq.length) {
492         int len = ntohs(conn->hostUniq.length);
493         CHECK_ROOM(cursor, packet.payload, len + TAG_HDR_SIZE);
494         memcpy(cursor, &conn->hostUniq, len + TAG_HDR_SIZE);
495         cursor += len + TAG_HDR_SIZE;
496         plen += len + TAG_HDR_SIZE;
497     }
498
499     packet.length = htons(plen);
500
501     sendPacket(conn, conn->discoverySocket, &packet, (int) (plen + HDR_SIZE));
502     if (conn->debugFile) {
503         dumpPacket(conn->debugFile, &packet, "SENT");
504         fprintf(conn->debugFile, "\n");
505         fflush(conn->debugFile);
506     }
507 }
508
509 /**********************************************************************
510 *%FUNCTION: waitForPADO
511 *%ARGUMENTS:
512 * conn -- PPPoEConnection structure
513 * timeout -- how long to wait (in seconds)
514 *%RETURNS:
515 * Nothing
516 *%DESCRIPTION:
517 * Waits for a PADO packet and copies useful information
518 ***********************************************************************/
519 void
520 waitForPADO(PPPoEConnection *conn, int timeout)
521 {
522     fd_set readable;
523     int r;
524     struct timeval tv;
525     PPPoEPacket packet;
526     int len;
527
528     struct PacketCriteria pc;
529     pc.conn          = conn;
530     pc.acNameOK      = (conn->acName)      ? 0 : 1;
531     pc.serviceNameOK = (conn->serviceName) ? 0 : 1;
532     pc.seenACName    = 0;
533     pc.seenServiceName = 0;
534     conn->error = 0;
535         
536     do {
537         if (BPF_BUFFER_IS_EMPTY) {
538             tv.tv_sec = timeout;
539             tv.tv_usec = 0;
540         
541             FD_ZERO(&readable);
542             FD_SET(conn->discoverySocket, &readable);
543
544             while(1) {
545                 r = select(conn->discoverySocket+1, &readable, NULL, NULL, &tv);
546                 if (r >= 0 || errno != EINTR) break;
547             }
548             if (r < 0) {
549                 perror("select (waitForPADO)");
550                 return;
551             }
552             if (r == 0) return;        /* Timed out */
553         }
554         
555         /* Get the packet */
556         receivePacket(conn->discoverySocket, &packet, &len);
557
558         /* Check length */
559         if (ntohs(packet.length) + HDR_SIZE > len) {
560             fprintf(stderr, "Bogus PPPoE length field (%u)\n",
561                    (unsigned int) ntohs(packet.length));
562             continue;
563         }
564
565 #ifdef USE_BPF
566         /* If it's not a Discovery packet, loop again */
567         if (etherType(&packet) != Eth_PPPOE_Discovery) continue;
568 #endif
569
570         if (conn->debugFile) {
571             dumpPacket(conn->debugFile, &packet, "RCVD");
572             fprintf(conn->debugFile, "\n");
573             fflush(conn->debugFile);
574         }
575         /* If it's not for us, loop again */
576         if (!packetIsForMe(conn, &packet)) continue;
577
578         if (packet.code == CODE_PADO) {
579             if (BROADCAST(packet.ethHdr.h_source)) {
580                 fprintf(stderr, "Ignoring PADO packet from broadcast MAC address\n");
581                 continue;
582             }
583             parsePacket(&packet, parsePADOTags, &pc);
584             if (conn->error)
585                 return;
586             if (!pc.seenACName) {
587                 fprintf(stderr, "Ignoring PADO packet with no AC-Name tag\n");
588                 continue;
589             }
590             if (!pc.seenServiceName) {
591                 fprintf(stderr, "Ignoring PADO packet with no Service-Name tag\n");
592                 continue;
593             }
594             conn->numPADOs++;
595             if (pc.acNameOK && pc.serviceNameOK) {
596                 memcpy(conn->peerEth, packet.ethHdr.h_source, ETH_ALEN);
597                 if (conn->printACNames) {
598                     printf("AC-Ethernet-Address: %02x:%02x:%02x:%02x:%02x:%02x\n",
599                            (unsigned) conn->peerEth[0], 
600                            (unsigned) conn->peerEth[1],
601                            (unsigned) conn->peerEth[2],
602                            (unsigned) conn->peerEth[3],
603                            (unsigned) conn->peerEth[4],
604                            (unsigned) conn->peerEth[5]);
605                     printf("--------------------------------------------------\n");
606                     continue;
607                 }
608                 conn->discoveryState = STATE_RECEIVED_PADO;
609                 break;
610             }
611         }
612     } while (conn->discoveryState != STATE_RECEIVED_PADO);
613 }
614
615 /**********************************************************************
616 *%FUNCTION: discovery
617 *%ARGUMENTS:
618 * conn -- PPPoE connection info structure
619 *%RETURNS:
620 * Nothing
621 *%DESCRIPTION:
622 * Performs the PPPoE discovery phase
623 ***********************************************************************/
624 void
625 discovery(PPPoEConnection *conn)
626 {
627     int padiAttempts = 0;
628     int timeout = conn->discoveryTimeout;
629
630     conn->discoverySocket =
631         openInterface(conn->ifName, Eth_PPPOE_Discovery, conn->myEth);
632
633     do {
634         padiAttempts++;
635         if (padiAttempts > conn->discoveryAttempts) {
636             fprintf(stderr, "Timeout waiting for PADO packets\n");
637             close(conn->discoverySocket);
638             conn->discoverySocket = -1;
639             return;
640         }
641         sendPADI(conn);
642         conn->discoveryState = STATE_SENT_PADI;
643         waitForPADO(conn, timeout);
644     } while (!conn->numPADOs);
645 }
646
647 int main(int argc, char *argv[])
648 {
649     int opt;
650     PPPoEConnection *conn;
651
652     conn = malloc(sizeof(PPPoEConnection));
653     if (!conn)
654         fatalSys("malloc");
655
656     memset(conn, 0, sizeof(PPPoEConnection));
657
658     conn->printACNames = 1;
659     conn->discoveryTimeout = PADI_TIMEOUT;
660     conn->discoveryAttempts = MAX_PADI_ATTEMPTS;
661
662     while ((opt = getopt(argc, argv, "I:D:VUQS:C:W:t:a:h")) > 0) {
663         switch(opt) {
664         case 'S':
665             conn->serviceName = xstrdup(optarg);
666             break;
667         case 'C':
668             conn->acName = xstrdup(optarg);
669             break;
670         case 't':
671             if (sscanf(optarg, "%d", &conn->discoveryTimeout) != 1) {
672                 fprintf(stderr, "Illegal argument to -t: Should be -t timeout\n");
673                 exit(EXIT_FAILURE);
674             }
675             if (conn->discoveryTimeout < 1) {
676                 conn->discoveryTimeout = 1;
677             }
678             break;
679         case 'a':
680             if (sscanf(optarg, "%d", &conn->discoveryAttempts) != 1) {
681                 fprintf(stderr, "Illegal argument to -a: Should be -a attempts\n");
682                 exit(EXIT_FAILURE);
683             }
684             if (conn->discoveryAttempts < 1) {
685                 conn->discoveryAttempts = 1;
686             }
687             break;
688         case 'U':
689             if(conn->hostUniq.length) {
690                 fprintf(stderr, "-U and -W are mutually exclusive\n");
691                 exit(EXIT_FAILURE);
692             } else {
693                 pid_t pid = getpid();
694                 conn->hostUniq.type = htons(TAG_HOST_UNIQ);
695                 conn->hostUniq.length = htons(sizeof(pid));
696                 memcpy(conn->hostUniq.payload, &pid, sizeof(pid));
697             }
698             break;
699         case 'W':
700             if(conn->hostUniq.length) {
701                 fprintf(stderr, "-U and -W are mutually exclusive\n");
702                 exit(EXIT_FAILURE);
703             }
704             if (!parseHostUniq(optarg, &conn->hostUniq)) {
705                 fprintf(stderr, "Invalid host-uniq argument: %s\n", optarg);
706                 exit(EXIT_FAILURE);
707             }
708             break;
709         case 'D':
710             conn->debugFile = fopen(optarg, "w");
711             if (!conn->debugFile) {
712                 fprintf(stderr, "Could not open %s: %s\n",
713                         optarg, strerror(errno));
714                 exit(1);
715             }
716             fprintf(conn->debugFile, "pppoe-discovery from pppd %s\n", VERSION);
717             break;
718         case 'I':
719             conn->ifName = xstrdup(optarg);
720             break;
721         case 'Q':
722             conn->printACNames = 0;
723             break;
724         case 'V':
725         case 'h':
726             usage();
727             exit(0);
728         default:
729             usage();
730             exit(1);
731         }
732     }
733
734     /* default interface name */
735     if (!conn->ifName)
736         conn->ifName = strdup("eth0");
737
738     conn->discoverySocket = -1;
739     conn->sessionSocket = -1;
740
741     discovery(conn);
742
743     if (!conn->numPADOs)
744         exit(1);
745     else
746         exit(0);
747 }
748
749 void rp_fatal(char const *str)
750 {
751     fprintf(stderr, "%s\n", str);
752     exit(1);
753 }
754
755 void fatalSys(char const *str)
756 {
757     perror(str);
758     exit(1);
759 }
760
761 void sysErr(char const *str)
762 {
763     rp_fatal(str);
764 }
765
766 char *xstrdup(const char *s)
767 {
768     register char *ret = strdup(s);
769     if (!ret)
770         sysErr("strdup");
771     return ret;
772 }
773
774 void usage(void)
775 {
776     fprintf(stderr, "Usage: pppoe-discovery [options]\n");
777     fprintf(stderr, "Options:\n");
778     fprintf(stderr, "   -I if_name     -- Specify interface (default eth0)\n");
779     fprintf(stderr, "   -D filename    -- Log debugging information in filename.\n");
780     fprintf(stderr,
781             "   -t timeout     -- Initial timeout for discovery packets in seconds\n"
782             "   -a attempts    -- Number of discovery attempts\n"
783             "   -V             -- Print version and exit.\n"
784             "   -Q             -- Quit Mode: Do not print access concentrator names\n"
785             "   -S name        -- Set desired service name.\n"
786             "   -C name        -- Set desired access concentrator name.\n"
787             "   -U             -- Use Host-Unique to allow multiple PPPoE sessions.\n"
788             "   -W hexvalue    -- Set the Host-Unique to the supplied hex string.\n"
789             "   -h             -- Print usage information.\n");
790     fprintf(stderr, "\npppoe-discovery from pppd " VERSION "\n");
791 }