1 /***********************************************************************
5 * Perform PPPoE discovery
7 * Copyright (C) 1999 by Roaring Penguin Software Inc.
9 ***********************************************************************/
11 static char const RCSID[] =
12 "$Id: discovery.c,v 1.6 2008/06/15 04:35:50 paulus Exp $";
16 #include "pppd/pppd.h"
24 #ifdef HAVE_SYS_TIME_H
36 #ifdef USE_LINUX_PACKET
37 #include <sys/ioctl.h>
43 /* Calculate time remaining until *exp, return 0 if now >= *exp */
44 static int time_left(struct timeval *diff, struct timeval *exp)
48 if (gettimeofday(&now, NULL) < 0) {
49 error("gettimeofday: %m");
53 if (now.tv_sec > exp->tv_sec
54 || (now.tv_sec == exp->tv_sec && now.tv_usec >= exp->tv_usec))
57 diff->tv_sec = exp->tv_sec - now.tv_sec;
58 diff->tv_usec = exp->tv_usec - now.tv_usec;
59 if (diff->tv_usec < 0) {
60 diff->tv_usec += 1000000;
67 /**********************************************************************
68 *%FUNCTION: parseForHostUniq
73 * extra -- user-supplied pointer. This is assumed to be a pointer to int.
77 * If a HostUnique tag is found which matches our PID, sets *extra to 1.
78 ***********************************************************************/
80 parseForHostUniq(UINT16_t type, UINT16_t len, unsigned char *data,
83 int *val = (int *) extra;
84 if (type == TAG_HOST_UNIQ && len == sizeof(pid_t)) {
86 memcpy(&tmp, data, len);
87 if (tmp == getpid()) {
93 /**********************************************************************
94 *%FUNCTION: packetIsForMe
96 * conn -- PPPoE connection info
97 * packet -- a received PPPoE packet
99 * 1 if packet is for this PPPoE daemon; 0 otherwise.
101 * If we are using the Host-Unique tag, verifies that packet contains
102 * our unique identifier.
103 ***********************************************************************/
105 packetIsForMe(PPPoEConnection *conn, PPPoEPacket *packet)
109 /* If packet is not directed to our MAC address, forget it */
110 if (memcmp(packet->ethHdr.h_dest, conn->myEth, ETH_ALEN)) return 0;
112 /* If we're not using the Host-Unique tag, then accept the packet */
113 if (!conn->useHostUniq) return 1;
115 parsePacket(packet, parseForHostUniq, &forMe);
119 /**********************************************************************
120 *%FUNCTION: parsePADOTags
125 * extra -- extra user data. Should point to a PacketCriteria structure
126 * which gets filled in according to selected AC name and service
131 * Picks interesting tags out of a PADO packet
132 ***********************************************************************/
134 parsePADOTags(UINT16_t type, UINT16_t len, unsigned char *data,
137 struct PacketCriteria *pc = (struct PacketCriteria *) extra;
138 PPPoEConnection *conn = pc->conn;
145 if (conn->printACNames) {
146 info("Access-Concentrator: %.*s", (int) len, data);
148 if (conn->acName && len == strlen(conn->acName) &&
149 !strncmp((char *) data, conn->acName, len)) {
153 case TAG_SERVICE_NAME:
154 pc->seenServiceName = 1;
155 if (conn->serviceName && len == strlen(conn->serviceName) &&
156 !strncmp((char *) data, conn->serviceName, len)) {
157 pc->serviceNameOK = 1;
161 conn->cookie.type = htons(type);
162 conn->cookie.length = htons(len);
163 memcpy(conn->cookie.payload, data, len);
165 case TAG_RELAY_SESSION_ID:
166 conn->relayId.type = htons(type);
167 conn->relayId.length = htons(len);
168 memcpy(conn->relayId.payload, data, len);
170 case TAG_PPP_MAX_PAYLOAD:
171 if (len == sizeof(mru)) {
172 memcpy(&mru, data, sizeof(mru));
174 if (mru >= ETH_PPPOE_MTU) {
175 if (lcp_allowoptions[0].mru > mru)
176 lcp_allowoptions[0].mru = mru;
177 if (lcp_wantoptions[0].mru > mru)
178 lcp_wantoptions[0].mru = mru;
179 conn->seenMaxPayload = 1;
183 case TAG_SERVICE_NAME_ERROR:
184 error("PADO: Service-Name-Error: %.*s", (int) len, data);
187 case TAG_AC_SYSTEM_ERROR:
188 error("PADO: System-Error: %.*s", (int) len, data);
191 case TAG_GENERIC_ERROR:
192 error("PADO: Generic-Error: %.*s", (int) len, data);
198 /**********************************************************************
199 *%FUNCTION: parsePADSTags
204 * extra -- extra user data (pointer to PPPoEConnection structure)
208 * Picks interesting tags out of a PADS packet
209 ***********************************************************************/
211 parsePADSTags(UINT16_t type, UINT16_t len, unsigned char *data,
214 PPPoEConnection *conn = (PPPoEConnection *) extra;
217 case TAG_SERVICE_NAME:
218 dbglog("PADS: Service-Name: '%.*s'", (int) len, data);
220 case TAG_PPP_MAX_PAYLOAD:
221 if (len == sizeof(mru)) {
222 memcpy(&mru, data, sizeof(mru));
224 if (mru >= ETH_PPPOE_MTU) {
225 if (lcp_allowoptions[0].mru > mru)
226 lcp_allowoptions[0].mru = mru;
227 if (lcp_wantoptions[0].mru > mru)
228 lcp_wantoptions[0].mru = mru;
229 conn->seenMaxPayload = 1;
233 case TAG_SERVICE_NAME_ERROR:
234 error("PADS: Service-Name-Error: %.*s", (int) len, data);
237 case TAG_AC_SYSTEM_ERROR:
238 error("PADS: System-Error: %.*s", (int) len, data);
241 case TAG_GENERIC_ERROR:
242 error("PADS: Generic-Error: %.*s", (int) len, data);
245 case TAG_RELAY_SESSION_ID:
246 conn->relayId.type = htons(type);
247 conn->relayId.length = htons(len);
248 memcpy(conn->relayId.payload, data, len);
253 /***********************************************************************
256 * conn -- PPPoEConnection structure
260 * Sends a PADI packet
261 ***********************************************************************/
263 sendPADI(PPPoEConnection *conn)
266 unsigned char *cursor = packet.payload;
267 PPPoETag *svc = (PPPoETag *) (&packet.payload);
268 UINT16_t namelen = 0;
270 int omit_service_name = 0;
272 if (conn->serviceName) {
273 namelen = (UINT16_t) strlen(conn->serviceName);
274 if (!strcmp(conn->serviceName, "NO-SERVICE-NAME-NON-RFC-COMPLIANT")) {
275 omit_service_name = 1;
279 /* Set destination to Ethernet broadcast address */
280 memset(packet.ethHdr.h_dest, 0xFF, ETH_ALEN);
281 memcpy(packet.ethHdr.h_source, conn->myEth, ETH_ALEN);
283 packet.ethHdr.h_proto = htons(Eth_PPPOE_Discovery);
284 packet.vertype = PPPOE_VER_TYPE(1, 1);
285 packet.code = CODE_PADI;
288 if (!omit_service_name) {
289 plen = TAG_HDR_SIZE + namelen;
290 CHECK_ROOM(cursor, packet.payload, plen);
292 svc->type = TAG_SERVICE_NAME;
293 svc->length = htons(namelen);
295 if (conn->serviceName) {
296 memcpy(svc->payload, conn->serviceName, strlen(conn->serviceName));
298 cursor += namelen + TAG_HDR_SIZE;
303 /* If we're using Host-Uniq, copy it over */
304 if (conn->useHostUniq) {
306 pid_t pid = getpid();
307 hostUniq.type = htons(TAG_HOST_UNIQ);
308 hostUniq.length = htons(sizeof(pid));
309 memcpy(hostUniq.payload, &pid, sizeof(pid));
310 CHECK_ROOM(cursor, packet.payload, sizeof(pid) + TAG_HDR_SIZE);
311 memcpy(cursor, &hostUniq, sizeof(pid) + TAG_HDR_SIZE);
312 cursor += sizeof(pid) + TAG_HDR_SIZE;
313 plen += sizeof(pid) + TAG_HDR_SIZE;
316 /* Add our maximum MTU/MRU */
317 if (MIN(lcp_allowoptions[0].mru, lcp_wantoptions[0].mru) > ETH_PPPOE_MTU) {
319 UINT16_t mru = htons(MIN(lcp_allowoptions[0].mru, lcp_wantoptions[0].mru));
320 maxPayload.type = htons(TAG_PPP_MAX_PAYLOAD);
321 maxPayload.length = htons(sizeof(mru));
322 memcpy(maxPayload.payload, &mru, sizeof(mru));
323 CHECK_ROOM(cursor, packet.payload, sizeof(mru) + TAG_HDR_SIZE);
324 memcpy(cursor, &maxPayload, sizeof(mru) + TAG_HDR_SIZE);
325 cursor += sizeof(mru) + TAG_HDR_SIZE;
326 plen += sizeof(mru) + TAG_HDR_SIZE;
329 packet.length = htons(plen);
331 sendPacket(conn, conn->discoverySocket, &packet, (int) (plen + HDR_SIZE));
334 /**********************************************************************
335 *%FUNCTION: waitForPADO
337 * conn -- PPPoEConnection structure
338 * timeout -- how long to wait (in seconds)
342 * Waits for a PADO packet and copies useful information
343 ***********************************************************************/
345 waitForPADO(PPPoEConnection *conn, int timeout)
350 struct timeval expire_at;
355 struct PacketCriteria pc;
357 pc.acNameOK = (conn->acName) ? 0 : 1;
358 pc.serviceNameOK = (conn->serviceName) ? 0 : 1;
360 pc.seenServiceName = 0;
361 conn->seenMaxPayload = 0;
364 if (gettimeofday(&expire_at, NULL) < 0) {
365 error("gettimeofday (waitForPADO): %m");
368 expire_at.tv_sec += timeout;
371 if (BPF_BUFFER_IS_EMPTY) {
372 if (!time_left(&tv, &expire_at))
373 return; /* Timed out */
376 FD_SET(conn->discoverySocket, &readable);
379 r = select(conn->discoverySocket+1, &readable, NULL, NULL, &tv);
380 if (r >= 0 || errno != EINTR) break;
383 error("select (waitForPADO): %m");
387 return; /* Timed out */
391 receivePacket(conn->discoverySocket, &packet, &len);
394 if (ntohs(packet.length) + HDR_SIZE > len) {
395 error("Bogus PPPoE length field (%u)",
396 (unsigned int) ntohs(packet.length));
401 /* If it's not a Discovery packet, loop again */
402 if (etherType(&packet) != Eth_PPPOE_Discovery) continue;
405 /* If it's not for us, loop again */
406 if (!packetIsForMe(conn, &packet)) continue;
408 if (packet.code == CODE_PADO) {
409 if (NOT_UNICAST(packet.ethHdr.h_source)) {
410 error("Ignoring PADO packet from non-unicast MAC address");
414 && memcmp(packet.ethHdr.h_source, conn->req_peer_mac, ETH_ALEN) != 0) {
415 warn("Ignoring PADO packet from wrong MAC address");
418 if (parsePacket(&packet, parsePADOTags, &pc) < 0)
422 if (!pc.seenACName) {
423 error("Ignoring PADO packet with no AC-Name tag");
426 if (!pc.seenServiceName) {
427 error("Ignoring PADO packet with no Service-Name tag");
431 if (pc.acNameOK && pc.serviceNameOK) {
432 memcpy(conn->peerEth, packet.ethHdr.h_source, ETH_ALEN);
433 conn->discoveryState = STATE_RECEIVED_PADO;
437 } while (conn->discoveryState != STATE_RECEIVED_PADO);
440 /***********************************************************************
443 * conn -- PPPoE connection structur
447 * Sends a PADR packet
448 ***********************************************************************/
450 sendPADR(PPPoEConnection *conn)
453 PPPoETag *svc = (PPPoETag *) packet.payload;
454 unsigned char *cursor = packet.payload;
456 UINT16_t namelen = 0;
459 if (conn->serviceName) {
460 namelen = (UINT16_t) strlen(conn->serviceName);
462 plen = TAG_HDR_SIZE + namelen;
463 CHECK_ROOM(cursor, packet.payload, plen);
465 memcpy(packet.ethHdr.h_dest, conn->peerEth, ETH_ALEN);
466 memcpy(packet.ethHdr.h_source, conn->myEth, ETH_ALEN);
468 packet.ethHdr.h_proto = htons(Eth_PPPOE_Discovery);
469 packet.vertype = PPPOE_VER_TYPE(1, 1);
470 packet.code = CODE_PADR;
473 svc->type = TAG_SERVICE_NAME;
474 svc->length = htons(namelen);
475 if (conn->serviceName) {
476 memcpy(svc->payload, conn->serviceName, namelen);
478 cursor += namelen + TAG_HDR_SIZE;
480 /* If we're using Host-Uniq, copy it over */
481 if (conn->useHostUniq) {
483 pid_t pid = getpid();
484 hostUniq.type = htons(TAG_HOST_UNIQ);
485 hostUniq.length = htons(sizeof(pid));
486 memcpy(hostUniq.payload, &pid, sizeof(pid));
487 CHECK_ROOM(cursor, packet.payload, sizeof(pid)+TAG_HDR_SIZE);
488 memcpy(cursor, &hostUniq, sizeof(pid) + TAG_HDR_SIZE);
489 cursor += sizeof(pid) + TAG_HDR_SIZE;
490 plen += sizeof(pid) + TAG_HDR_SIZE;
493 /* Add our maximum MTU/MRU */
494 if (MIN(lcp_allowoptions[0].mru, lcp_wantoptions[0].mru) > ETH_PPPOE_MTU) {
496 UINT16_t mru = htons(MIN(lcp_allowoptions[0].mru, lcp_wantoptions[0].mru));
497 maxPayload.type = htons(TAG_PPP_MAX_PAYLOAD);
498 maxPayload.length = htons(sizeof(mru));
499 memcpy(maxPayload.payload, &mru, sizeof(mru));
500 CHECK_ROOM(cursor, packet.payload, sizeof(mru) + TAG_HDR_SIZE);
501 memcpy(cursor, &maxPayload, sizeof(mru) + TAG_HDR_SIZE);
502 cursor += sizeof(mru) + TAG_HDR_SIZE;
503 plen += sizeof(mru) + TAG_HDR_SIZE;
506 /* Copy cookie and relay-ID if needed */
507 if (conn->cookie.type) {
508 CHECK_ROOM(cursor, packet.payload,
509 ntohs(conn->cookie.length) + TAG_HDR_SIZE);
510 memcpy(cursor, &conn->cookie, ntohs(conn->cookie.length) + TAG_HDR_SIZE);
511 cursor += ntohs(conn->cookie.length) + TAG_HDR_SIZE;
512 plen += ntohs(conn->cookie.length) + TAG_HDR_SIZE;
515 if (conn->relayId.type) {
516 CHECK_ROOM(cursor, packet.payload,
517 ntohs(conn->relayId.length) + TAG_HDR_SIZE);
518 memcpy(cursor, &conn->relayId, ntohs(conn->relayId.length) + TAG_HDR_SIZE);
519 cursor += ntohs(conn->relayId.length) + TAG_HDR_SIZE;
520 plen += ntohs(conn->relayId.length) + TAG_HDR_SIZE;
523 packet.length = htons(plen);
524 sendPacket(conn, conn->discoverySocket, &packet, (int) (plen + HDR_SIZE));
527 /**********************************************************************
528 *%FUNCTION: waitForPADS
530 * conn -- PPPoE connection info
531 * timeout -- how long to wait (in seconds)
535 * Waits for a PADS packet and copies useful information
536 ***********************************************************************/
538 waitForPADS(PPPoEConnection *conn, int timeout)
543 struct timeval expire_at;
548 if (gettimeofday(&expire_at, NULL) < 0) {
549 error("gettimeofday (waitForPADS): %m");
552 expire_at.tv_sec += timeout;
556 if (BPF_BUFFER_IS_EMPTY) {
557 if (!time_left(&tv, &expire_at))
558 return; /* Timed out */
561 FD_SET(conn->discoverySocket, &readable);
564 r = select(conn->discoverySocket+1, &readable, NULL, NULL, &tv);
565 if (r >= 0 || errno != EINTR) break;
568 error("select (waitForPADS): %m");
572 return; /* Timed out */
576 receivePacket(conn->discoverySocket, &packet, &len);
579 if (ntohs(packet.length) + HDR_SIZE > len) {
580 error("Bogus PPPoE length field (%u)",
581 (unsigned int) ntohs(packet.length));
586 /* If it's not a Discovery packet, loop again */
587 if (etherType(&packet) != Eth_PPPOE_Discovery) continue;
590 /* If it's not from the AC, it's not for me */
591 if (memcmp(packet.ethHdr.h_source, conn->peerEth, ETH_ALEN)) continue;
593 /* If it's not for us, loop again */
594 if (!packetIsForMe(conn, &packet)) continue;
597 if (packet.code == CODE_PADS) {
598 /* Parse for goodies */
599 if (parsePacket(&packet, parsePADSTags, conn) < 0)
603 conn->discoveryState = STATE_SESSION;
606 } while (conn->discoveryState != STATE_SESSION);
608 /* Don't bother with ntohs; we'll just end up converting it back... */
609 conn->session = packet.session;
611 info("PPP session is %d", (int) ntohs(conn->session));
613 /* RFC 2516 says session id MUST NOT be zero or 0xFFFF */
614 if (ntohs(conn->session) == 0 || ntohs(conn->session) == 0xFFFF) {
615 error("Access concentrator used a session value of %x -- the AC is violating RFC 2516", (unsigned int) ntohs(conn->session));
619 /**********************************************************************
620 *%FUNCTION: discovery
622 * conn -- PPPoE connection info structure
626 * Performs the PPPoE discovery phase
627 ***********************************************************************/
629 discovery(PPPoEConnection *conn)
631 int padiAttempts = 0;
632 int padrAttempts = 0;
633 int timeout = conn->discoveryTimeout;
637 if (padiAttempts > MAX_PADI_ATTEMPTS) {
638 warn("Timeout waiting for PADO packets");
639 close(conn->discoverySocket);
640 conn->discoverySocket = -1;
644 conn->discoveryState = STATE_SENT_PADI;
645 waitForPADO(conn, timeout);
648 } while (conn->discoveryState == STATE_SENT_PADI);
650 timeout = conn->discoveryTimeout;
653 if (padrAttempts > MAX_PADI_ATTEMPTS) {
654 warn("Timeout waiting for PADS packets");
655 close(conn->discoverySocket);
656 conn->discoverySocket = -1;
660 conn->discoveryState = STATE_SENT_PADR;
661 waitForPADS(conn, timeout);
663 } while (conn->discoveryState == STATE_SENT_PADR);
665 if (!conn->seenMaxPayload) {
666 /* RFC 4638: MUST limit MTU/MRU to 1492 */
667 if (lcp_allowoptions[0].mru > ETH_PPPOE_MTU)
668 lcp_allowoptions[0].mru = ETH_PPPOE_MTU;
669 if (lcp_wantoptions[0].mru > ETH_PPPOE_MTU)
670 lcp_wantoptions[0].mru = ETH_PPPOE_MTU;
674 conn->discoveryState = STATE_SESSION;