]> git.ozlabs.org Git - ppp.git/blobdiff - pppd/plugins/rp-pppoe/discovery.c
pppoe: Custom host-uniq tag
[ppp.git] / pppd / plugins / rp-pppoe / discovery.c
index a856490f96fb7ba887ae2e8dda16a91eae358769..16a59f77f09b0761951684d8868c277913066abd 100644 (file)
@@ -40,6 +40,30 @@ static char const RCSID[] =
 
 #include <signal.h>
 
+/* Calculate time remaining until *exp, return 0 if now >= *exp */
+static int time_left(struct timeval *diff, struct timeval *exp)
+{
+    struct timeval now;
+
+    if (gettimeofday(&now, NULL) < 0) {
+       error("gettimeofday: %m");
+       return 0;
+    }
+
+    if (now.tv_sec > exp->tv_sec
+       || (now.tv_sec == exp->tv_sec && now.tv_usec >= exp->tv_usec))
+       return 0;
+
+    diff->tv_sec = exp->tv_sec - now.tv_sec;
+    diff->tv_usec = exp->tv_usec - now.tv_usec;
+    if (diff->tv_usec < 0) {
+       diff->tv_usec += 1000000;
+       --diff->tv_sec;
+    }
+
+    return 1;
+}
+
 /**********************************************************************
 *%FUNCTION: parseForHostUniq
 *%ARGUMENTS:
@@ -56,14 +80,10 @@ static void
 parseForHostUniq(UINT16_t type, UINT16_t len, unsigned char *data,
                 void *extra)
 {
-    int *val = (int *) extra;
-    if (type == TAG_HOST_UNIQ && len == sizeof(pid_t)) {
-       pid_t tmp;
-       memcpy(&tmp, data, len);
-       if (tmp == getpid()) {
-           *val = 1;
-       }
-    }
+    PPPoETag *tag = extra;
+
+    if (type == TAG_HOST_UNIQ && len == ntohs(tag->length))
+       tag->length = memcmp(data, tag->payload, len);
 }
 
 /**********************************************************************
@@ -80,16 +100,16 @@ parseForHostUniq(UINT16_t type, UINT16_t len, unsigned char *data,
 static int
 packetIsForMe(PPPoEConnection *conn, PPPoEPacket *packet)
 {
-    int forMe = 0;
+    PPPoETag hostUniq = conn->hostUniq;
 
     /* If packet is not directed to our MAC address, forget it */
     if (memcmp(packet->ethHdr.h_dest, conn->myEth, ETH_ALEN)) return 0;
 
     /* If we're not using the Host-Unique tag, then accept the packet */
-    if (!conn->useHostUniq) return 1;
+    if (!conn->hostUniq.length) return 1;
 
-    parsePacket(packet, parseForHostUniq, &forMe);
-    return forMe;
+    parsePacket(packet, parseForHostUniq, &hostUniq);
+    return !hostUniq.length;
 }
 
 /**********************************************************************
@@ -277,16 +297,12 @@ sendPADI(PPPoEConnection *conn)
     }
 
     /* If we're using Host-Uniq, copy it over */
-    if (conn->useHostUniq) {
-       PPPoETag hostUniq;
-       pid_t pid = getpid();
-       hostUniq.type = htons(TAG_HOST_UNIQ);
-       hostUniq.length = htons(sizeof(pid));
-       memcpy(hostUniq.payload, &pid, sizeof(pid));
-       CHECK_ROOM(cursor, packet.payload, sizeof(pid) + TAG_HDR_SIZE);
-       memcpy(cursor, &hostUniq, sizeof(pid) + TAG_HDR_SIZE);
-       cursor += sizeof(pid) + TAG_HDR_SIZE;
-       plen += sizeof(pid) + TAG_HDR_SIZE;
+    if (conn->hostUniq.length) {
+       int len = ntohs(conn->hostUniq.length);
+       CHECK_ROOM(cursor, packet.payload, len + TAG_HDR_SIZE);
+       memcpy(cursor, &conn->hostUniq, len + TAG_HDR_SIZE);
+       cursor += len + TAG_HDR_SIZE;
+       plen += len + TAG_HDR_SIZE;
     }
 
     /* Add our maximum MTU/MRU */
@@ -323,6 +339,8 @@ waitForPADO(PPPoEConnection *conn, int timeout)
     fd_set readable;
     int r;
     struct timeval tv;
+    struct timeval expire_at;
+
     PPPoEPacket packet;
     int len;
 
@@ -335,10 +353,16 @@ waitForPADO(PPPoEConnection *conn, int timeout)
     conn->seenMaxPayload = 0;
     conn->error = 0;
 
+    if (gettimeofday(&expire_at, NULL) < 0) {
+       error("gettimeofday (waitForPADO): %m");
+       return;
+    }
+    expire_at.tv_sec += timeout;
+
     do {
        if (BPF_BUFFER_IS_EMPTY) {
-           tv.tv_sec = timeout;
-           tv.tv_usec = 0;
+           if (!time_left(&tv, &expire_at))
+               return;         /* Timed out */
 
            FD_ZERO(&readable);
            FD_SET(conn->discoverySocket, &readable);
@@ -351,7 +375,8 @@ waitForPADO(PPPoEConnection *conn, int timeout)
                error("select (waitForPADO): %m");
                return;
            }
-           if (r == 0) return;        /* Timed out */
+           if (r == 0)
+               return;         /* Timed out */
        }
 
        /* Get the packet */
@@ -445,16 +470,12 @@ sendPADR(PPPoEConnection *conn)
     cursor += namelen + TAG_HDR_SIZE;
 
     /* If we're using Host-Uniq, copy it over */
-    if (conn->useHostUniq) {
-       PPPoETag hostUniq;
-       pid_t pid = getpid();
-       hostUniq.type = htons(TAG_HOST_UNIQ);
-       hostUniq.length = htons(sizeof(pid));
-       memcpy(hostUniq.payload, &pid, sizeof(pid));
-       CHECK_ROOM(cursor, packet.payload, sizeof(pid)+TAG_HDR_SIZE);
-       memcpy(cursor, &hostUniq, sizeof(pid) + TAG_HDR_SIZE);
-       cursor += sizeof(pid) + TAG_HDR_SIZE;
-       plen += sizeof(pid) + TAG_HDR_SIZE;
+    if (conn->hostUniq.length) {
+       int len = ntohs(conn->hostUniq.length);
+       CHECK_ROOM(cursor, packet.payload, len+TAG_HDR_SIZE);
+       memcpy(cursor, &conn->hostUniq, len + TAG_HDR_SIZE);
+       cursor += len + TAG_HDR_SIZE;
+       plen += len + TAG_HDR_SIZE;
     }
 
     /* Add our maximum MTU/MRU */
@@ -507,14 +528,22 @@ waitForPADS(PPPoEConnection *conn, int timeout)
     fd_set readable;
     int r;
     struct timeval tv;
+    struct timeval expire_at;
+
     PPPoEPacket packet;
     int len;
 
+    if (gettimeofday(&expire_at, NULL) < 0) {
+       error("gettimeofday (waitForPADS): %m");
+       return;
+    }
+    expire_at.tv_sec += timeout;
+
     conn->error = 0;
     do {
        if (BPF_BUFFER_IS_EMPTY) {
-           tv.tv_sec = timeout;
-           tv.tv_usec = 0;
+           if (!time_left(&tv, &expire_at))
+               return;         /* Timed out */
 
            FD_ZERO(&readable);
            FD_SET(conn->discoverySocket, &readable);
@@ -527,7 +556,8 @@ waitForPADS(PPPoEConnection *conn, int timeout)
                error("select (waitForPADS): %m");
                return;
            }
-           if (r == 0) return;
+           if (r == 0)
+               return;         /* Timed out */
        }
 
        /* Get the packet */
@@ -590,9 +620,6 @@ discovery(PPPoEConnection *conn)
     int padrAttempts = 0;
     int timeout = conn->discoveryTimeout;
 
-    conn->discoverySocket =
-       openInterface(conn->ifName, Eth_PPPOE_Discovery, conn->myEth);
-
     do {
        padiAttempts++;
        if (padiAttempts > MAX_PADI_ATTEMPTS) {