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 (get_time(&now) < 0) {
49 error("get_time: %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 PPPoETag *tag = extra;
85 if (type == TAG_HOST_UNIQ && len == ntohs(tag->length))
86 tag->length = memcmp(data, tag->payload, len);
89 /**********************************************************************
90 *%FUNCTION: packetIsForMe
92 * conn -- PPPoE connection info
93 * packet -- a received PPPoE packet
95 * 1 if packet is for this PPPoE daemon; 0 otherwise.
97 * If we are using the Host-Unique tag, verifies that packet contains
98 * our unique identifier.
99 ***********************************************************************/
101 packetIsForMe(PPPoEConnection *conn, PPPoEPacket *packet)
103 PPPoETag hostUniq = conn->hostUniq;
105 /* If packet is not directed to our MAC address, forget it */
106 if (memcmp(packet->ethHdr.h_dest, conn->myEth, ETH_ALEN)) return 0;
108 /* If we're not using the Host-Unique tag, then accept the packet */
109 if (!conn->hostUniq.length) return 1;
111 parsePacket(packet, parseForHostUniq, &hostUniq);
112 return !hostUniq.length;
115 /**********************************************************************
116 *%FUNCTION: parsePADOTags
121 * extra -- extra user data. Should point to a PacketCriteria structure
122 * which gets filled in according to selected AC name and service
127 * Picks interesting tags out of a PADO packet
128 ***********************************************************************/
130 parsePADOTags(UINT16_t type, UINT16_t len, unsigned char *data,
133 struct PacketCriteria *pc = (struct PacketCriteria *) extra;
134 PPPoEConnection *conn = pc->conn;
141 if (conn->printACNames) {
142 info("Access-Concentrator: %.*s", (int) len, data);
144 if (conn->acName && len == strlen(conn->acName) &&
145 !strncmp((char *) data, conn->acName, len)) {
149 case TAG_SERVICE_NAME:
150 pc->seenServiceName = 1;
151 if (conn->serviceName && len == strlen(conn->serviceName) &&
152 !strncmp((char *) data, conn->serviceName, len)) {
153 pc->serviceNameOK = 1;
157 conn->cookie.type = htons(type);
158 conn->cookie.length = htons(len);
159 memcpy(conn->cookie.payload, data, len);
161 case TAG_RELAY_SESSION_ID:
162 conn->relayId.type = htons(type);
163 conn->relayId.length = htons(len);
164 memcpy(conn->relayId.payload, data, len);
166 case TAG_PPP_MAX_PAYLOAD:
167 if (len == sizeof(mru)) {
168 memcpy(&mru, data, sizeof(mru));
170 if (mru >= ETH_PPPOE_MTU) {
171 if (lcp_allowoptions[0].mru > mru)
172 lcp_allowoptions[0].mru = mru;
173 if (lcp_wantoptions[0].mru > mru)
174 lcp_wantoptions[0].mru = mru;
175 conn->seenMaxPayload = 1;
179 case TAG_SERVICE_NAME_ERROR:
180 error("PADO: Service-Name-Error: %.*s", (int) len, data);
183 case TAG_AC_SYSTEM_ERROR:
184 error("PADO: System-Error: %.*s", (int) len, data);
187 case TAG_GENERIC_ERROR:
188 error("PADO: Generic-Error: %.*s", (int) len, data);
194 /**********************************************************************
195 *%FUNCTION: parsePADSTags
200 * extra -- extra user data (pointer to PPPoEConnection structure)
204 * Picks interesting tags out of a PADS packet
205 ***********************************************************************/
207 parsePADSTags(UINT16_t type, UINT16_t len, unsigned char *data,
210 PPPoEConnection *conn = (PPPoEConnection *) extra;
213 case TAG_SERVICE_NAME:
214 dbglog("PADS: Service-Name: '%.*s'", (int) len, data);
216 case TAG_PPP_MAX_PAYLOAD:
217 if (len == sizeof(mru)) {
218 memcpy(&mru, data, sizeof(mru));
220 if (mru >= ETH_PPPOE_MTU) {
221 if (lcp_allowoptions[0].mru > mru)
222 lcp_allowoptions[0].mru = mru;
223 if (lcp_wantoptions[0].mru > mru)
224 lcp_wantoptions[0].mru = mru;
225 conn->seenMaxPayload = 1;
229 case TAG_SERVICE_NAME_ERROR:
230 error("PADS: Service-Name-Error: %.*s", (int) len, data);
233 case TAG_AC_SYSTEM_ERROR:
234 error("PADS: System-Error: %.*s", (int) len, data);
237 case TAG_GENERIC_ERROR:
238 error("PADS: Generic-Error: %.*s", (int) len, data);
241 case TAG_RELAY_SESSION_ID:
242 conn->relayId.type = htons(type);
243 conn->relayId.length = htons(len);
244 memcpy(conn->relayId.payload, data, len);
249 /***********************************************************************
252 * conn -- PPPoEConnection structure
256 * Sends a PADI packet
257 ***********************************************************************/
259 sendPADI(PPPoEConnection *conn)
262 unsigned char *cursor = packet.payload;
263 PPPoETag *svc = (PPPoETag *) (&packet.payload);
264 UINT16_t namelen = 0;
266 int omit_service_name = 0;
268 if (conn->serviceName) {
269 namelen = (UINT16_t) strlen(conn->serviceName);
270 if (!strcmp(conn->serviceName, "NO-SERVICE-NAME-NON-RFC-COMPLIANT")) {
271 omit_service_name = 1;
275 /* Set destination to Ethernet broadcast address */
276 memset(packet.ethHdr.h_dest, 0xFF, ETH_ALEN);
277 memcpy(packet.ethHdr.h_source, conn->myEth, ETH_ALEN);
279 packet.ethHdr.h_proto = htons(Eth_PPPOE_Discovery);
280 packet.vertype = PPPOE_VER_TYPE(1, 1);
281 packet.code = CODE_PADI;
284 if (!omit_service_name) {
285 plen = TAG_HDR_SIZE + namelen;
286 CHECK_ROOM(cursor, packet.payload, plen);
288 svc->type = TAG_SERVICE_NAME;
289 svc->length = htons(namelen);
291 if (conn->serviceName) {
292 memcpy(svc->payload, conn->serviceName, strlen(conn->serviceName));
294 cursor += namelen + TAG_HDR_SIZE;
299 /* If we're using Host-Uniq, copy it over */
300 if (conn->hostUniq.length) {
301 int len = ntohs(conn->hostUniq.length);
302 CHECK_ROOM(cursor, packet.payload, len + TAG_HDR_SIZE);
303 memcpy(cursor, &conn->hostUniq, len + TAG_HDR_SIZE);
304 cursor += len + TAG_HDR_SIZE;
305 plen += len + TAG_HDR_SIZE;
308 /* Add our maximum MTU/MRU */
309 if (MIN(lcp_allowoptions[0].mru, lcp_wantoptions[0].mru) > ETH_PPPOE_MTU) {
311 UINT16_t mru = htons(MIN(lcp_allowoptions[0].mru, lcp_wantoptions[0].mru));
312 maxPayload.type = htons(TAG_PPP_MAX_PAYLOAD);
313 maxPayload.length = htons(sizeof(mru));
314 memcpy(maxPayload.payload, &mru, sizeof(mru));
315 CHECK_ROOM(cursor, packet.payload, sizeof(mru) + TAG_HDR_SIZE);
316 memcpy(cursor, &maxPayload, sizeof(mru) + TAG_HDR_SIZE);
317 cursor += sizeof(mru) + TAG_HDR_SIZE;
318 plen += sizeof(mru) + TAG_HDR_SIZE;
321 packet.length = htons(plen);
323 sendPacket(conn, conn->discoverySocket, &packet, (int) (plen + HDR_SIZE));
326 /**********************************************************************
327 *%FUNCTION: waitForPADO
329 * conn -- PPPoEConnection structure
330 * timeout -- how long to wait (in seconds)
334 * Waits for a PADO packet and copies useful information
335 ***********************************************************************/
337 waitForPADO(PPPoEConnection *conn, int timeout)
342 struct timeval expire_at;
347 struct PacketCriteria pc;
349 pc.acNameOK = (conn->acName) ? 0 : 1;
350 pc.serviceNameOK = (conn->serviceName) ? 0 : 1;
352 pc.seenServiceName = 0;
353 conn->seenMaxPayload = 0;
356 if (get_time(&expire_at) < 0) {
357 error("get_time (waitForPADO): %m");
360 expire_at.tv_sec += timeout;
363 if (BPF_BUFFER_IS_EMPTY) {
364 if (!time_left(&tv, &expire_at))
365 return; /* Timed out */
368 FD_SET(conn->discoverySocket, &readable);
371 r = select(conn->discoverySocket+1, &readable, NULL, NULL, &tv);
372 if (r >= 0 || errno != EINTR) break;
375 error("select (waitForPADO): %m");
379 return; /* Timed out */
383 receivePacket(conn->discoverySocket, &packet, &len);
386 if (ntohs(packet.length) + HDR_SIZE > len) {
387 error("Bogus PPPoE length field (%u)",
388 (unsigned int) ntohs(packet.length));
393 /* If it's not a Discovery packet, loop again */
394 if (etherType(&packet) != Eth_PPPOE_Discovery) continue;
397 /* If it's not for us, loop again */
398 if (!packetIsForMe(conn, &packet)) continue;
400 if (packet.code == CODE_PADO) {
401 if (NOT_UNICAST(packet.ethHdr.h_source)) {
402 error("Ignoring PADO packet from non-unicast MAC address");
406 && memcmp(packet.ethHdr.h_source, conn->req_peer_mac, ETH_ALEN) != 0) {
407 warn("Ignoring PADO packet from wrong MAC address");
410 if (parsePacket(&packet, parsePADOTags, &pc) < 0)
414 if (!pc.seenACName) {
415 error("Ignoring PADO packet with no AC-Name tag");
418 if (!pc.seenServiceName) {
419 error("Ignoring PADO packet with no Service-Name tag");
423 if (pc.acNameOK && pc.serviceNameOK) {
424 memcpy(conn->peerEth, packet.ethHdr.h_source, ETH_ALEN);
425 conn->discoveryState = STATE_RECEIVED_PADO;
429 } while (conn->discoveryState != STATE_RECEIVED_PADO);
432 /***********************************************************************
435 * conn -- PPPoE connection structur
439 * Sends a PADR packet
440 ***********************************************************************/
442 sendPADR(PPPoEConnection *conn)
445 PPPoETag *svc = (PPPoETag *) packet.payload;
446 unsigned char *cursor = packet.payload;
448 UINT16_t namelen = 0;
451 if (conn->serviceName) {
452 namelen = (UINT16_t) strlen(conn->serviceName);
454 plen = TAG_HDR_SIZE + namelen;
455 CHECK_ROOM(cursor, packet.payload, plen);
457 memcpy(packet.ethHdr.h_dest, conn->peerEth, ETH_ALEN);
458 memcpy(packet.ethHdr.h_source, conn->myEth, ETH_ALEN);
460 packet.ethHdr.h_proto = htons(Eth_PPPOE_Discovery);
461 packet.vertype = PPPOE_VER_TYPE(1, 1);
462 packet.code = CODE_PADR;
465 svc->type = TAG_SERVICE_NAME;
466 svc->length = htons(namelen);
467 if (conn->serviceName) {
468 memcpy(svc->payload, conn->serviceName, namelen);
470 cursor += namelen + TAG_HDR_SIZE;
472 /* If we're using Host-Uniq, copy it over */
473 if (conn->hostUniq.length) {
474 int len = ntohs(conn->hostUniq.length);
475 CHECK_ROOM(cursor, packet.payload, len+TAG_HDR_SIZE);
476 memcpy(cursor, &conn->hostUniq, len + TAG_HDR_SIZE);
477 cursor += len + TAG_HDR_SIZE;
478 plen += len + TAG_HDR_SIZE;
481 /* Add our maximum MTU/MRU */
482 if (MIN(lcp_allowoptions[0].mru, lcp_wantoptions[0].mru) > ETH_PPPOE_MTU) {
484 UINT16_t mru = htons(MIN(lcp_allowoptions[0].mru, lcp_wantoptions[0].mru));
485 maxPayload.type = htons(TAG_PPP_MAX_PAYLOAD);
486 maxPayload.length = htons(sizeof(mru));
487 memcpy(maxPayload.payload, &mru, sizeof(mru));
488 CHECK_ROOM(cursor, packet.payload, sizeof(mru) + TAG_HDR_SIZE);
489 memcpy(cursor, &maxPayload, sizeof(mru) + TAG_HDR_SIZE);
490 cursor += sizeof(mru) + TAG_HDR_SIZE;
491 plen += sizeof(mru) + TAG_HDR_SIZE;
494 /* Copy cookie and relay-ID if needed */
495 if (conn->cookie.type) {
496 CHECK_ROOM(cursor, packet.payload,
497 ntohs(conn->cookie.length) + TAG_HDR_SIZE);
498 memcpy(cursor, &conn->cookie, ntohs(conn->cookie.length) + TAG_HDR_SIZE);
499 cursor += ntohs(conn->cookie.length) + TAG_HDR_SIZE;
500 plen += ntohs(conn->cookie.length) + TAG_HDR_SIZE;
503 if (conn->relayId.type) {
504 CHECK_ROOM(cursor, packet.payload,
505 ntohs(conn->relayId.length) + TAG_HDR_SIZE);
506 memcpy(cursor, &conn->relayId, ntohs(conn->relayId.length) + TAG_HDR_SIZE);
507 cursor += ntohs(conn->relayId.length) + TAG_HDR_SIZE;
508 plen += ntohs(conn->relayId.length) + TAG_HDR_SIZE;
511 packet.length = htons(plen);
512 sendPacket(conn, conn->discoverySocket, &packet, (int) (plen + HDR_SIZE));
515 /**********************************************************************
516 *%FUNCTION: waitForPADS
518 * conn -- PPPoE connection info
519 * timeout -- how long to wait (in seconds)
523 * Waits for a PADS packet and copies useful information
524 ***********************************************************************/
526 waitForPADS(PPPoEConnection *conn, int timeout)
531 struct timeval expire_at;
536 if (get_time(&expire_at) < 0) {
537 error("get_time (waitForPADS): %m");
540 expire_at.tv_sec += timeout;
544 if (BPF_BUFFER_IS_EMPTY) {
545 if (!time_left(&tv, &expire_at))
546 return; /* Timed out */
549 FD_SET(conn->discoverySocket, &readable);
552 r = select(conn->discoverySocket+1, &readable, NULL, NULL, &tv);
553 if (r >= 0 || errno != EINTR) break;
556 error("select (waitForPADS): %m");
560 return; /* Timed out */
564 receivePacket(conn->discoverySocket, &packet, &len);
567 if (ntohs(packet.length) + HDR_SIZE > len) {
568 error("Bogus PPPoE length field (%u)",
569 (unsigned int) ntohs(packet.length));
574 /* If it's not a Discovery packet, loop again */
575 if (etherType(&packet) != Eth_PPPOE_Discovery) continue;
578 /* If it's not from the AC, it's not for me */
579 if (memcmp(packet.ethHdr.h_source, conn->peerEth, ETH_ALEN)) continue;
581 /* If it's not for us, loop again */
582 if (!packetIsForMe(conn, &packet)) continue;
585 if (packet.code == CODE_PADS) {
586 /* Parse for goodies */
587 if (parsePacket(&packet, parsePADSTags, conn) < 0)
591 conn->discoveryState = STATE_SESSION;
594 } while (conn->discoveryState != STATE_SESSION);
596 /* Don't bother with ntohs; we'll just end up converting it back... */
597 conn->session = packet.session;
599 info("PPP session is %d", (int) ntohs(conn->session));
601 /* RFC 2516 says session id MUST NOT be zero or 0xFFFF */
602 if (ntohs(conn->session) == 0 || ntohs(conn->session) == 0xFFFF) {
603 error("Access concentrator used a session value of %x -- the AC is violating RFC 2516", (unsigned int) ntohs(conn->session));
607 /**********************************************************************
608 *%FUNCTION: discovery
610 * conn -- PPPoE connection info structure
614 * Performs the PPPoE discovery phase
615 ***********************************************************************/
617 discovery(PPPoEConnection *conn)
619 int padiAttempts = 0;
620 int padrAttempts = 0;
621 int timeout = conn->discoveryTimeout;
625 if (padiAttempts > conn->discoveryAttempts) {
626 warn("Timeout waiting for PADO packets");
627 close(conn->discoverySocket);
628 conn->discoverySocket = -1;
632 conn->discoveryState = STATE_SENT_PADI;
633 waitForPADO(conn, timeout);
636 } while (conn->discoveryState == STATE_SENT_PADI);
638 timeout = conn->discoveryTimeout;
641 if (padrAttempts > conn->discoveryAttempts) {
642 warn("Timeout waiting for PADS packets");
643 close(conn->discoverySocket);
644 conn->discoverySocket = -1;
648 conn->discoveryState = STATE_SENT_PADR;
649 waitForPADS(conn, timeout);
651 } while (conn->discoveryState == STATE_SENT_PADR);
653 if (!conn->seenMaxPayload) {
654 /* RFC 4638: MUST limit MTU/MRU to 1492 */
655 if (lcp_allowoptions[0].mru > ETH_PPPOE_MTU)
656 lcp_allowoptions[0].mru = ETH_PPPOE_MTU;
657 if (lcp_wantoptions[0].mru > ETH_PPPOE_MTU)
658 lcp_wantoptions[0].mru = ETH_PPPOE_MTU;
662 conn->discoveryState = STATE_SESSION;