summaryrefslogtreecommitdiff
path: root/user/ppp/32_all_pado-timeout.patch
blob: e1765845695ed688df7e07ac78e5a2188bbfbce1 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
--- ppp-2.4.6/pppd/plugins/rp-pppoe/discovery.c
+++ ppp-2.4.6/pppd/plugins/rp-pppoe/discovery.c
@@ -39,6 +39,7 @@
 #endif
 
 #include <signal.h>
+#include <time.h>
 
 /* Calculate time remaining until *exp, return 0 if now >= *exp */
 static int time_left(struct timeval *diff, struct timeval *exp)
@@ -251,6 +252,80 @@
 }
 
 /***********************************************************************
+*%FUNCTION: recvPacketForMe
+*%ARGUMENTS:
+* packet -- output parameter
+* len -- output parameter length
+* conn -- connection
+* start -- operation startup timestamp
+* timeout -- how long to wait (in seconds)
+*%RETURNS:
+* -1:  error
+*  0:  timed out
+*  1:  packet received
+*%DESCRIPTION:
+* receive and filter junk packets
+***********************************************************************/
+
+static int
+recvPacketForMe(PPPoEPacket *packet, int *len, PPPoEConnection *conn, time_t start, int timeout)
+{
+    fd_set readable;
+    int r;
+    struct timeval tv;
+    time_t now;
+    int time_remain;
+
+    do {
+	time(&now);
+	time_remain = timeout - (int)difftime(now, start);
+	if (time_remain <= 0) return 0;  /* Timed out */
+
+	if (BPF_BUFFER_IS_EMPTY) {
+	    tv.tv_sec = time_remain;
+	    tv.tv_usec = 0;
+
+	    FD_ZERO(&readable);
+	    FD_SET(conn->discoverySocket, &readable);
+
+	    r = select(conn->discoverySocket+1, &readable, NULL, NULL, &tv);
+	    if (r < 0)
+	    {
+		if (errno == EINTR)
+		{
+		    continue;   /* interrupted, so retry */
+		}else
+		{
+		    error("pppoe: recvPacketForMe: select: %m");
+		    return -1;
+		}
+	    }
+
+	    if (r == 0) return 0;	/* Timed out */
+	}
+
+	/* Get the packet */
+	receivePacket(conn->discoverySocket, packet, len);
+
+	/* Check length */
+	if (ntohs(packet->length) + HDR_SIZE > *len) {
+	    error("Bogus PPPoE length field (%u)",
+		   (unsigned int) ntohs(packet->length));
+	    continue;
+	}
+
+#ifdef USE_BPF
+	/* If it's not a Discovery packet, loop again */
+	if (etherType(&packet) != Eth_PPPOE_Discovery) continue;
+#endif
+	/* If it's not for us, loop again */
+	}while ( ! packetIsForMe(conn, packet));
+
+	return 1;
+}
+
+
+/***********************************************************************
 *%FUNCTION: sendPADI
 *%ARGUMENTS:
 * conn -- PPPoEConnection structure
@@ -344,13 +419,12 @@
 void
 waitForPADO(PPPoEConnection *conn, int timeout)
 {
-    fd_set readable;
     int r;
-    struct timeval tv;
     struct timeval expire_at;
 
     PPPoEPacket packet;
     int len;
+    time_t start;
 
     struct PacketCriteria pc;
     pc.conn          = conn;
@@ -367,43 +441,10 @@
     }
     expire_at.tv_sec += timeout;
 
+    time(&start);
     do {
-	if (BPF_BUFFER_IS_EMPTY) {
-	    if (!time_left(&tv, &expire_at))
-		return;		/* Timed out */
-
-	    FD_ZERO(&readable);
-	    FD_SET(conn->discoverySocket, &readable);
-
-	    while(1) {
-		r = select(conn->discoverySocket+1, &readable, NULL, NULL, &tv);
-		if (r >= 0 || errno != EINTR) break;
-	    }
-	    if (r < 0) {
-		error("select (waitForPADO): %m");
-		return;
-	    }
-	    if (r == 0)
-		return;		/* Timed out */
-	}
-
-	/* Get the packet */
-	receivePacket(conn->discoverySocket, &packet, &len);
-
-	/* Check length */
-	if (ntohs(packet.length) + HDR_SIZE > len) {
-	    error("Bogus PPPoE length field (%u)",
-		   (unsigned int) ntohs(packet.length));
-	    continue;
-	}
-
-#ifdef USE_BPF
-	/* If it's not a Discovery packet, loop again */
-	if (etherType(&packet) != Eth_PPPOE_Discovery) continue;
-#endif
-
-	/* If it's not for us, loop again */
-	if (!packetIsForMe(conn, &packet)) continue;
+	r = recvPacketForMe(&packet, &len, conn, start, timeout);
+	if (r<=0) return;  /* Timed out or error */
 
 	if (packet.code == CODE_PADO) {
 	    if (NOT_UNICAST(packet.ethHdr.h_source)) {
@@ -537,13 +578,12 @@
 static void
 waitForPADS(PPPoEConnection *conn, int timeout)
 {
-    fd_set readable;
     int r;
-    struct timeval tv;
     struct timeval expire_at;
 
     PPPoEPacket packet;
     int len;
+    time_t start;
 
     if (gettimeofday(&expire_at, NULL) < 0) {
 	error("gettimeofday (waitForPADS): %m");
@@ -551,48 +591,15 @@
     }
     expire_at.tv_sec += timeout;
 
+    time(&start);
     conn->error = 0;
     do {
-	if (BPF_BUFFER_IS_EMPTY) {
-	    if (!time_left(&tv, &expire_at))
-		return;		/* Timed out */
-
-	    FD_ZERO(&readable);
-	    FD_SET(conn->discoverySocket, &readable);
-
-	    while(1) {
-		r = select(conn->discoverySocket+1, &readable, NULL, NULL, &tv);
-		if (r >= 0 || errno != EINTR) break;
-	    }
-	    if (r < 0) {
-		error("select (waitForPADS): %m");
-		return;
-	    }
-	    if (r == 0)
-		return;		/* Timed out */
-	}
-
-	/* Get the packet */
-	receivePacket(conn->discoverySocket, &packet, &len);
-
-	/* Check length */
-	if (ntohs(packet.length) + HDR_SIZE > len) {
-	    error("Bogus PPPoE length field (%u)",
-		   (unsigned int) ntohs(packet.length));
-	    continue;
-	}
-
-#ifdef USE_BPF
-	/* If it's not a Discovery packet, loop again */
-	if (etherType(&packet) != Eth_PPPOE_Discovery) continue;
-#endif
+	r = recvPacketForMe(&packet, &len, conn, start, timeout);
+	if (r<=0) return;  /* Timed out or error */
 
 	/* If it's not from the AC, it's not for me */
 	if (memcmp(packet.ethHdr.h_source, conn->peerEth, ETH_ALEN)) continue;
 
-	/* If it's not for us, loop again */
-	if (!packetIsForMe(conn, &packet)) continue;
-
 	/* Is it PADS?  */
 	if (packet.code == CODE_PADS) {
 	    /* Parse for goodies */