]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/Common/ethernet/lwIP_130/src/netif/ppp/ppp_oe.c
Add FreeRTOS-Plus directory.
[freertos] / FreeRTOS / Demo / Common / ethernet / lwIP_130 / src / netif / ppp / ppp_oe.c
1 /*****************************************************************************\r
2 * ppp_oe.c - PPP Over Ethernet implementation for lwIP.\r
3 *\r
4 * Copyright (c) 2006 by Marc Boucher, Services Informatiques (MBSI) inc.\r
5 *\r
6 * The authors hereby grant permission to use, copy, modify, distribute,\r
7 * and license this software and its documentation for any purpose, provided\r
8 * that existing copyright notices are retained in all copies and that this\r
9 * notice and the following disclaimer are included verbatim in any \r
10 * distributions. No written agreement, license, or royalty fee is required\r
11 * for any of the authorized uses.\r
12 *\r
13 * THIS SOFTWARE IS PROVIDED BY THE CONTRIBUTORS *AS IS* AND ANY EXPRESS OR\r
14 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES\r
15 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. \r
16 * IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,\r
17 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT\r
18 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,\r
19 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY\r
20 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r
21 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF\r
22 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r
23 *\r
24 ******************************************************************************\r
25 * REVISION HISTORY\r
26 *\r
27 * 06-01-01 Marc Boucher <marc@mbsi.ca>\r
28 *   Ported to lwIP.\r
29 *****************************************************************************/\r
30 \r
31 \r
32 \r
33 /* based on NetBSD: if_pppoe.c,v 1.64 2006/01/31 23:50:15 martin Exp */\r
34 \r
35 /*-\r
36  * Copyright (c) 2002 The NetBSD Foundation, Inc.\r
37  * All rights reserved.\r
38  *\r
39  * This code is derived from software contributed to The NetBSD Foundation\r
40  * by Martin Husemann <martin@NetBSD.org>.\r
41  *\r
42  * Redistribution and use in source and binary forms, with or without\r
43  * modification, are permitted provided that the following conditions\r
44  * are met:\r
45  * 1. Redistributions of source code must retain the above copyright\r
46  *    notice, this list of conditions and the following disclaimer.\r
47  * 2. Redistributions in binary form must reproduce the above copyright\r
48  *    notice, this list of conditions and the following disclaimer in the\r
49  *    documentation and/or other materials provided with the distribution.\r
50  * 3. All advertising materials mentioning features or use of this software\r
51  *    must display the following acknowledgement:\r
52  *        This product includes software developed by the NetBSD\r
53  *        Foundation, Inc. and its contributors.\r
54  * 4. Neither the name of The NetBSD Foundation nor the names of its\r
55  *    contributors may be used to endorse or promote products derived\r
56  *    from this software without specific prior written permission.\r
57  *\r
58  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS\r
59  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED\r
60  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR\r
61  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS\r
62  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR\r
63  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF\r
64  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS\r
65  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN\r
66  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)\r
67  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE\r
68  * POSSIBILITY OF SUCH DAMAGE.\r
69  */\r
70 \r
71 #include "lwip/opt.h"\r
72 \r
73 #if PPPOE_SUPPORT /* don't build if not configured for use in lwipopts.h */\r
74 \r
75 #include "ppp.h"\r
76 #include "pppdebug.h"\r
77 \r
78 #include "lwip/sys.h"\r
79 \r
80 #include "netif/ppp_oe.h"\r
81 #include "netif/etharp.h"\r
82 \r
83 #include <string.h>\r
84 #include <stdio.h>\r
85 \r
86 /** @todo Replace this part with a simple list like other lwIP lists */\r
87 #ifndef _SYS_QUEUE_H_\r
88 #define _SYS_QUEUE_H_\r
89 \r
90 /*\r
91  * A list is headed by a single forward pointer (or an array of forward\r
92  * pointers for a hash table header). The elements are doubly linked\r
93  * so that an arbitrary element can be removed without a need to\r
94  * traverse the list. New elements can be added to the list before\r
95  * or after an existing element or at the head of the list. A list\r
96  * may only be traversed in the forward direction.\r
97  *\r
98  * For details on the use of these macros, see the queue(3) manual page.\r
99  */\r
100 \r
101 /*\r
102  * List declarations.\r
103  */\r
104 #define  LIST_HEAD(name, type)                                                 \\r
105 struct name {                                                                  \\r
106   struct type *lh_first;  /* first element */                                  \\r
107 }\r
108 \r
109 #define  LIST_HEAD_INITIALIZER(head)                                           \\r
110   { NULL }\r
111 \r
112 #define  LIST_ENTRY(type)                                                      \\r
113 struct {                                                                       \\r
114   struct type *le_next;  /* next element */                                    \\r
115   struct type **le_prev; /* address of previous next element */                \\r
116 }\r
117 \r
118 /*\r
119  * List functions.\r
120  */\r
121 \r
122 #define  LIST_EMPTY(head)  ((head)->lh_first == NULL)\r
123 \r
124 #define  LIST_FIRST(head)  ((head)->lh_first)\r
125 \r
126 #define  LIST_FOREACH(var, head, field)                                        \\r
127   for ((var) = LIST_FIRST((head));                                             \\r
128       (var);                                                                   \\r
129       (var) = LIST_NEXT((var), field))\r
130 \r
131 #define  LIST_INIT(head) do {                                                  \\r
132   LIST_FIRST((head)) = NULL;                                                   \\r
133 } while (0)\r
134 \r
135 #define  LIST_INSERT_AFTER(listelm, elm, field) do {                           \\r
136   if ((LIST_NEXT((elm), field) = LIST_NEXT((listelm), field)) != NULL)         \\r
137     LIST_NEXT((listelm), field)->field.le_prev =                               \\r
138         &LIST_NEXT((elm), field);                                              \\r
139   LIST_NEXT((listelm), field) = (elm);                                         \\r
140   (elm)->field.le_prev = &LIST_NEXT((listelm), field);                         \\r
141 } while (0)\r
142 \r
143 #define  LIST_INSERT_BEFORE(listelm, elm, field) do {                          \\r
144   (elm)->field.le_prev = (listelm)->field.le_prev;                             \\r
145   LIST_NEXT((elm), field) = (listelm);                                         \\r
146   *(listelm)->field.le_prev = (elm);                                           \\r
147   (listelm)->field.le_prev = &LIST_NEXT((elm), field);                         \\r
148 } while (0)\r
149 \r
150 #define  LIST_INSERT_HEAD(head, elm, field) do {                               \\r
151   if ((LIST_NEXT((elm), field) = LIST_FIRST((head))) != NULL)                  \\r
152     LIST_FIRST((head))->field.le_prev = &LIST_NEXT((elm), field);              \\r
153   LIST_FIRST((head)) = (elm);                                                  \\r
154   (elm)->field.le_prev = &LIST_FIRST((head));                                  \\r
155 } while (0)\r
156 \r
157 #define  LIST_NEXT(elm, field)  ((elm)->field.le_next)\r
158 \r
159 #define  LIST_REMOVE(elm, field) do {                                          \\r
160   if (LIST_NEXT((elm), field) != NULL)                                         \\r
161     LIST_NEXT((elm), field)->field.le_prev =                                   \\r
162         (elm)->field.le_prev;                                                  \\r
163   *(elm)->field.le_prev = LIST_NEXT((elm), field);                             \\r
164 } while (0)\r
165 \r
166 #endif /* !_SYS_QUEUE_H_ */\r
167 \r
168 \r
169 /* Add a 16 bit unsigned value to a buffer pointed to by PTR */\r
170 #define PPPOE_ADD_16(PTR, VAL) \\r
171     *(PTR)++ = (VAL) / 256;    \\r
172     *(PTR)++ = (VAL) % 256\r
173 \r
174 /* Add a complete PPPoE header to the buffer pointed to by PTR */\r
175 #define PPPOE_ADD_HEADER(PTR, CODE, SESS, LEN)  \\r
176     *(PTR)++ = PPPOE_VERTYPE;  \\r
177     *(PTR)++ = (CODE);         \\r
178     PPPOE_ADD_16(PTR, SESS);   \\r
179     PPPOE_ADD_16(PTR, LEN)\r
180 \r
181 #define PPPOE_DISC_TIMEOUT (5*1000)  /* base for quick timeout calculation */\r
182 #define PPPOE_SLOW_RETRY   (60*1000) /* persistent retry interval */\r
183 #define PPPOE_DISC_MAXPADI  4        /* retry PADI four times (quickly) */\r
184 #define PPPOE_DISC_MAXPADR  2        /* retry PADR twice */\r
185 \r
186 #ifdef PPPOE_SERVER\r
187 /* from if_spppsubr.c */\r
188 #define IFF_PASSIVE IFF_LINK0 /* wait passively for connection */\r
189 #endif\r
190 \r
191 struct pppoe_softc {\r
192   LIST_ENTRY(pppoe_softc) sc_list;\r
193   struct netif *sc_ethif;      /* ethernet interface we are using */\r
194   int sc_pd;                   /* ppp unit number */\r
195   void (*sc_linkStatusCB)(int pd, int up);\r
196 \r
197   int sc_state;                /* discovery phase or session connected */\r
198   struct eth_addr sc_dest;     /* hardware address of concentrator */\r
199   u16_t sc_session;            /* PPPoE session id */\r
200 \r
201   char *sc_service_name;       /* if != NULL: requested name of service */\r
202   char *sc_concentrator_name;  /* if != NULL: requested concentrator id */\r
203   u8_t *sc_ac_cookie;          /* content of AC cookie we must echo back */\r
204   size_t sc_ac_cookie_len;     /* length of cookie data */\r
205 #ifdef PPPOE_SERVER\r
206   u8_t *sc_hunique;            /* content of host unique we must echo back */\r
207   size_t sc_hunique_len;       /* length of host unique */\r
208 #endif\r
209   int sc_padi_retried;         /* number of PADI retries already done */\r
210   int sc_padr_retried;         /* number of PADR retries already done */\r
211 };\r
212 \r
213 /* input routines */\r
214 static void pppoe_dispatch_disc_pkt(struct netif *, struct pbuf *);\r
215 \r
216 /* management routines */\r
217 static int pppoe_do_disconnect(struct pppoe_softc *);\r
218 static void pppoe_abort_connect(struct pppoe_softc *);\r
219 static void pppoe_clear_softc(struct pppoe_softc *, const char *);\r
220 \r
221 /* internal timeout handling */\r
222 static void pppoe_timeout(void *);\r
223 \r
224 /* sending actual protocol controll packets */\r
225 static err_t pppoe_send_padi(struct pppoe_softc *);\r
226 static err_t pppoe_send_padr(struct pppoe_softc *);\r
227 #ifdef PPPOE_SERVER\r
228 static err_t pppoe_send_pado(struct pppoe_softc *);\r
229 static err_t pppoe_send_pads(struct pppoe_softc *);\r
230 #endif\r
231 static err_t pppoe_send_padt(struct netif *, u_int, const u8_t *);\r
232 \r
233 /* internal helper functions */\r
234 static struct pppoe_softc * pppoe_find_softc_by_session(u_int, struct netif *);\r
235 static struct pppoe_softc * pppoe_find_softc_by_hunique(u8_t *, size_t, struct netif *);\r
236 \r
237 static LIST_HEAD(pppoe_softc_head, pppoe_softc) pppoe_softc_list;\r
238 \r
239 int pppoe_hdrlen;\r
240 \r
241 void\r
242 pppoe_init(void)\r
243 {\r
244   pppoe_hdrlen = sizeof(struct eth_hdr) + PPPOE_HEADERLEN;\r
245   LIST_INIT(&pppoe_softc_list);\r
246 }\r
247 \r
248 err_t\r
249 pppoe_create(struct netif *ethif, int pd, void (*linkStatusCB)(int pd, int up), struct pppoe_softc **scptr)\r
250 {\r
251   struct pppoe_softc *sc;\r
252 \r
253   sc = mem_malloc(sizeof(struct pppoe_softc));\r
254   if(!sc) {\r
255     *scptr = NULL;\r
256     return ERR_MEM;\r
257   }\r
258   memset(sc, 0, sizeof(struct pppoe_softc));\r
259 \r
260   /* changed to real address later */\r
261   MEMCPY(&sc->sc_dest, ethbroadcast.addr, sizeof(sc->sc_dest));\r
262 \r
263   sc->sc_pd = pd;\r
264   sc->sc_linkStatusCB = linkStatusCB;\r
265   sc->sc_ethif = ethif;\r
266 \r
267   LIST_INSERT_HEAD(&pppoe_softc_list, sc, sc_list);\r
268 \r
269   *scptr = sc;\r
270 \r
271   return ERR_OK;\r
272 }\r
273 \r
274 err_t\r
275 pppoe_destroy(struct netif *ifp)\r
276 {\r
277   struct pppoe_softc * sc;\r
278 \r
279   LIST_FOREACH(sc, &pppoe_softc_list, sc_list) {\r
280     if (sc->sc_ethif == ifp) {\r
281       break;\r
282     }\r
283   }\r
284 \r
285   if(!(sc && (sc->sc_ethif == ifp))) {\r
286     return ERR_IF;\r
287   }\r
288 \r
289   tcpip_untimeout(pppoe_timeout, sc);\r
290   LIST_REMOVE(sc, sc_list);\r
291 \r
292   if (sc->sc_concentrator_name) {\r
293     mem_free(sc->sc_concentrator_name);\r
294   }\r
295   if (sc->sc_service_name) {\r
296     mem_free(sc->sc_service_name);\r
297   }\r
298   if (sc->sc_ac_cookie) {\r
299     mem_free(sc->sc_ac_cookie);\r
300   }\r
301   mem_free(sc);\r
302 \r
303   return ERR_OK;\r
304 }\r
305 \r
306 /*\r
307  * Find the interface handling the specified session.\r
308  * Note: O(number of sessions open), this is a client-side only, mean\r
309  * and lean implementation, so number of open sessions typically should\r
310  * be 1.\r
311  */\r
312 static struct pppoe_softc *\r
313 pppoe_find_softc_by_session(u_int session, struct netif *rcvif)\r
314 {\r
315   struct pppoe_softc *sc;\r
316 \r
317   if (session == 0) {\r
318     return NULL;\r
319   }\r
320 \r
321   LIST_FOREACH(sc, &pppoe_softc_list, sc_list) {\r
322     if (sc->sc_state == PPPOE_STATE_SESSION\r
323         && sc->sc_session == session) {\r
324       if (sc->sc_ethif == rcvif) {\r
325         return sc;\r
326       } else {\r
327         return NULL;\r
328       }\r
329     }\r
330   }\r
331   return NULL;\r
332 }\r
333 \r
334 /* Check host unique token passed and return appropriate softc pointer,\r
335  * or NULL if token is bogus. */\r
336 static struct pppoe_softc *\r
337 pppoe_find_softc_by_hunique(u8_t *token, size_t len, struct netif *rcvif)\r
338 {\r
339   struct pppoe_softc *sc, *t;\r
340 \r
341   if (LIST_EMPTY(&pppoe_softc_list)) {\r
342     return NULL;\r
343   }\r
344 \r
345   if (len != sizeof sc) {\r
346     return NULL;\r
347   }\r
348   MEMCPY(&t, token, len);\r
349 \r
350   LIST_FOREACH(sc, &pppoe_softc_list, sc_list) {\r
351     if (sc == t) {\r
352       break;\r
353     }\r
354   }\r
355 \r
356   if (sc == NULL) {\r
357     PPPDEBUG((LOG_DEBUG, "pppoe: alien host unique tag, no session found\n"));\r
358     return NULL;\r
359   }\r
360 \r
361   /* should be safe to access *sc now */\r
362   if (sc->sc_state < PPPOE_STATE_PADI_SENT || sc->sc_state >= PPPOE_STATE_SESSION) {\r
363     printf("%c%c%"U16_F": host unique tag found, but it belongs to a connection in state %d\n",\r
364       sc->sc_ethif->name[0], sc->sc_ethif->name[1], sc->sc_ethif->num, sc->sc_state);\r
365     return NULL;\r
366   }\r
367   if (sc->sc_ethif != rcvif) {\r
368     printf("%c%c%"U16_F": wrong interface, not accepting host unique\n",\r
369       sc->sc_ethif->name[0], sc->sc_ethif->name[1], sc->sc_ethif->num);\r
370     return NULL;\r
371   }\r
372   return sc;\r
373 }\r
374 \r
375 static void\r
376 pppoe_linkstatus_up(void *arg)\r
377 {\r
378   struct pppoe_softc *sc = (struct pppoe_softc*)arg;\r
379 \r
380   sc->sc_linkStatusCB(sc->sc_pd, 1);\r
381 }\r
382 \r
383 /* analyze and handle a single received packet while not in session state */\r
384 static void\r
385 pppoe_dispatch_disc_pkt(struct netif *netif, struct pbuf *pb)\r
386 {\r
387   u16_t tag, len;\r
388   u16_t session, plen;\r
389   struct pppoe_softc *sc;\r
390   const char *err_msg;\r
391   char devname[6];\r
392   char *error;\r
393   u8_t *ac_cookie;\r
394   size_t ac_cookie_len;\r
395 #ifdef PPPOE_SERVER\r
396   u8_t *hunique;\r
397   size_t hunique_len;\r
398 #endif\r
399   struct pppoehdr *ph;\r
400   struct pppoetag pt;\r
401   int off = 0, err, errortag;\r
402   struct eth_hdr *ethhdr;\r
403 \r
404   pb = pppSingleBuf(pb);\r
405 \r
406   strcpy(devname, "pppoe");  /* as long as we don't know which instance */\r
407   err_msg = NULL;\r
408   errortag = 0;\r
409   if (pb->len < sizeof(*ethhdr)) {\r
410     goto done;\r
411   }\r
412   ethhdr = (struct eth_hdr *)pb->payload;\r
413   off += sizeof(*ethhdr);\r
414 \r
415   ac_cookie = NULL;\r
416   ac_cookie_len = 0;\r
417 #ifdef PPPOE_SERVER\r
418   hunique = NULL;\r
419   hunique_len = 0;\r
420 #endif\r
421   session = 0;\r
422   if (pb->len - off <= PPPOE_HEADERLEN) {\r
423     printf("pppoe: packet too short: %d\n", pb->len);\r
424     goto done;\r
425   }\r
426 \r
427   ph = (struct pppoehdr *) (ethhdr + 1);\r
428   if (ph->vertype != PPPOE_VERTYPE) {\r
429     printf("pppoe: unknown version/type packet: 0x%x\n", ph->vertype);\r
430     goto done;\r
431   }\r
432   session = ntohs(ph->session);\r
433   plen = ntohs(ph->plen);\r
434   off += sizeof(*ph);\r
435 \r
436   if (plen + off > pb->len) {\r
437     printf("pppoe: packet content does not fit: data available = %d, packet size = %u\n",\r
438         pb->len - off, plen);\r
439     goto done;\r
440   }\r
441   if(pb->tot_len == pb->len) {\r
442     pb->tot_len = pb->len = off + plen; /* ignore trailing garbage */\r
443   }\r
444   tag = 0;\r
445   len = 0;\r
446   sc = NULL;\r
447   while (off + sizeof(pt) <= pb->len) {\r
448     MEMCPY(&pt, (u8_t*)pb->payload + off, sizeof(pt));\r
449     tag = ntohs(pt.tag);\r
450     len = ntohs(pt.len);\r
451     if (off + sizeof(pt) + len > pb->len) {\r
452       printf("pppoe: tag 0x%x len 0x%x is too long\n", tag, len);\r
453       goto done;\r
454     }\r
455     switch (tag) {\r
456       case PPPOE_TAG_EOL:\r
457         goto breakbreak;\r
458       case PPPOE_TAG_SNAME:\r
459         break;  /* ignored */\r
460       case PPPOE_TAG_ACNAME:\r
461         break;  /* ignored */\r
462       case PPPOE_TAG_HUNIQUE:\r
463         if (sc != NULL) {\r
464           break;\r
465         }\r
466 #ifdef PPPOE_SERVER\r
467         hunique = (u8_t*)pb->payload + off + sizeof(pt);\r
468         hunique_len = len;\r
469 #endif\r
470         sc = pppoe_find_softc_by_hunique((u8_t*)pb->payload + off + sizeof(pt), len, netif);\r
471         if (sc != NULL) {\r
472           snprintf(devname, sizeof(devname), "%c%c%"U16_F, sc->sc_ethif->name[0], sc->sc_ethif->name[1], sc->sc_ethif->num);\r
473         }\r
474         break;\r
475       case PPPOE_TAG_ACCOOKIE:\r
476         if (ac_cookie == NULL) {\r
477           ac_cookie = (u8_t*)pb->payload + off + sizeof(pt);\r
478           ac_cookie_len = len;\r
479         }\r
480         break;\r
481       case PPPOE_TAG_SNAME_ERR:\r
482         err_msg = "SERVICE NAME ERROR";\r
483         errortag = 1;\r
484         break;\r
485       case PPPOE_TAG_ACSYS_ERR:\r
486         err_msg = "AC SYSTEM ERROR";\r
487         errortag = 1;\r
488         break;\r
489       case PPPOE_TAG_GENERIC_ERR:\r
490         err_msg = "GENERIC ERROR";\r
491         errortag = 1;\r
492         break;\r
493     }\r
494     if (err_msg) {\r
495       error = NULL;\r
496       if (errortag && len) {\r
497         error = mem_malloc(len+1);\r
498         if (error) {\r
499           strncpy(error, (char*)pb->payload + off + sizeof(pt), len);\r
500           error[len-1] = '\0';\r
501         }\r
502       }\r
503       if (error) {\r
504         printf("%s: %s: %s\n", devname, err_msg, error);\r
505         mem_free(error);\r
506       } else {\r
507         printf("%s: %s\n", devname, err_msg);\r
508       }\r
509       if (errortag) {\r
510         goto done;\r
511       }\r
512     }\r
513     off += sizeof(pt) + len;\r
514   }\r
515 \r
516 breakbreak:;\r
517   switch (ph->code) {\r
518     case PPPOE_CODE_PADI:\r
519 #ifdef PPPOE_SERVER\r
520       /*\r
521        * got service name, concentrator name, and/or host unique.\r
522        * ignore if we have no interfaces with IFF_PASSIVE|IFF_UP.\r
523        */\r
524       if (LIST_EMPTY(&pppoe_softc_list)) {\r
525         goto done;\r
526       }\r
527       LIST_FOREACH(sc, &pppoe_softc_list, sc_list) {\r
528         if (!(sc->sc_sppp.pp_if.if_flags & IFF_UP)) {\r
529           continue;\r
530         }\r
531         if (!(sc->sc_sppp.pp_if.if_flags & IFF_PASSIVE)) {\r
532           continue;\r
533         }\r
534         if (sc->sc_state == PPPOE_STATE_INITIAL) {\r
535           break;\r
536         }\r
537       }\r
538       if (sc == NULL) {\r
539         /* printf("pppoe: free passive interface is not found\n"); */\r
540         goto done;\r
541       }\r
542       if (hunique) {\r
543         if (sc->sc_hunique) {\r
544           mem_free(sc->sc_hunique);\r
545         }\r
546         sc->sc_hunique = mem_malloc(hunique_len);\r
547         if (sc->sc_hunique == NULL) {\r
548           goto done;\r
549         }\r
550         sc->sc_hunique_len = hunique_len;\r
551         MEMCPY(sc->sc_hunique, hunique, hunique_len);\r
552       }\r
553       MEMCPY(&sc->sc_dest, eh->ether_shost, sizeof sc->sc_dest);\r
554       sc->sc_state = PPPOE_STATE_PADO_SENT;\r
555       pppoe_send_pado(sc);\r
556       break;\r
557   #endif /* PPPOE_SERVER */\r
558     case PPPOE_CODE_PADR:\r
559   #ifdef PPPOE_SERVER\r
560       /*\r
561        * get sc from ac_cookie if IFF_PASSIVE\r
562        */\r
563       if (ac_cookie == NULL) {\r
564         /* be quiet if there is not a single pppoe instance */\r
565         printf("pppoe: received PADR but not includes ac_cookie\n");\r
566         goto done;\r
567       }\r
568       sc = pppoe_find_softc_by_hunique(ac_cookie, ac_cookie_len, netif);\r
569       if (sc == NULL) {\r
570         /* be quiet if there is not a single pppoe instance */\r
571         if (!LIST_EMPTY(&pppoe_softc_list)) {\r
572           printf("pppoe: received PADR but could not find request for it\n");\r
573         }\r
574         goto done;\r
575       }\r
576       if (sc->sc_state != PPPOE_STATE_PADO_SENT) {\r
577         printf("%c%c%"U16_F": received unexpected PADR\n", sc->sc_ethif->name[0], sc->sc_ethif->name[1], sc->sc_ethif->num);\r
578         goto done;\r
579       }\r
580       if (hunique) {\r
581         if (sc->sc_hunique) {\r
582           mem_free(sc->sc_hunique);\r
583         }\r
584         sc->sc_hunique = mem_malloc(hunique_len);\r
585         if (sc->sc_hunique == NULL) {\r
586           goto done;\r
587         }\r
588         sc->sc_hunique_len = hunique_len;\r
589         MEMCPY(sc->sc_hunique, hunique, hunique_len);\r
590       }\r
591       pppoe_send_pads(sc);\r
592       sc->sc_state = PPPOE_STATE_SESSION;\r
593       tcpip_timeout (100, pppoe_linkstatus_up, sc); /* notify upper layers */\r
594       break;\r
595   #else\r
596       /* ignore, we are no access concentrator */\r
597       goto done;\r
598   #endif /* PPPOE_SERVER */\r
599     case PPPOE_CODE_PADO:\r
600       if (sc == NULL) {\r
601         /* be quiet if there is not a single pppoe instance */\r
602         if (!LIST_EMPTY(&pppoe_softc_list)) {\r
603           printf("pppoe: received PADO but could not find request for it\n");\r
604         }\r
605         goto done;\r
606       }\r
607       if (sc->sc_state != PPPOE_STATE_PADI_SENT) {\r
608         printf("%c%c%"U16_F": received unexpected PADO\n", sc->sc_ethif->name[0], sc->sc_ethif->name[1], sc->sc_ethif->num);\r
609         goto done;\r
610       }\r
611       if (ac_cookie) {\r
612         if (sc->sc_ac_cookie) {\r
613           mem_free(sc->sc_ac_cookie);\r
614         }\r
615         sc->sc_ac_cookie = mem_malloc(ac_cookie_len);\r
616         if (sc->sc_ac_cookie == NULL) {\r
617           goto done;\r
618         }\r
619         sc->sc_ac_cookie_len = ac_cookie_len;\r
620         MEMCPY(sc->sc_ac_cookie, ac_cookie, ac_cookie_len);\r
621       }\r
622       MEMCPY(&sc->sc_dest, ethhdr->src.addr, sizeof(sc->sc_dest.addr));\r
623       tcpip_untimeout(pppoe_timeout, sc);\r
624       sc->sc_padr_retried = 0;\r
625       sc->sc_state = PPPOE_STATE_PADR_SENT;\r
626       if ((err = pppoe_send_padr(sc)) != 0) {\r
627         PPPDEBUG((LOG_DEBUG, "pppoe: %c%c%"U16_F": failed to send PADR, error=%d\n", sc->sc_ethif->name[0], sc->sc_ethif->name[1], sc->sc_ethif->num, err));\r
628       }\r
629       tcpip_timeout(PPPOE_DISC_TIMEOUT * (1 + sc->sc_padr_retried), pppoe_timeout, sc);\r
630       break;\r
631     case PPPOE_CODE_PADS:\r
632       if (sc == NULL) {\r
633         goto done;\r
634       }\r
635       sc->sc_session = session;\r
636       tcpip_untimeout(pppoe_timeout, sc);\r
637       PPPDEBUG((LOG_DEBUG, "pppoe: %c%c%"U16_F": session 0x%x connected\n", sc->sc_ethif->name[0], sc->sc_ethif->name[1], sc->sc_ethif->num, session));\r
638       sc->sc_state = PPPOE_STATE_SESSION;\r
639       tcpip_timeout (100, pppoe_linkstatus_up, sc); /* notify upper layers */\r
640       break;\r
641     case PPPOE_CODE_PADT:\r
642       if (sc == NULL) {\r
643         goto done;\r
644       }\r
645       pppoe_clear_softc(sc, "received PADT");\r
646       break;\r
647     default:\r
648       if(sc) {\r
649         printf("%c%c%"U16_F": unknown code (0x%04x) session = 0x%04x\n",\r
650             sc->sc_ethif->name[0], sc->sc_ethif->name[1], sc->sc_ethif->num,\r
651             ph->code, session);\r
652       } else {\r
653         printf("pppoe: unknown code (0x%04x) session = 0x%04x\n", ph->code, session);\r
654       }\r
655       break;\r
656   }\r
657 \r
658 done:\r
659   pbuf_free(pb);\r
660   return;\r
661 }\r
662 \r
663 void\r
664 pppoe_disc_input(struct netif *netif, struct pbuf *p)\r
665 {\r
666   /* avoid error messages if there is not a single pppoe instance */\r
667   if (!LIST_EMPTY(&pppoe_softc_list)) {\r
668     pppoe_dispatch_disc_pkt(netif, p);\r
669   } else {\r
670     pbuf_free(p);\r
671   }\r
672 }\r
673 \r
674 void\r
675 pppoe_data_input(struct netif *netif, struct pbuf *pb)\r
676 {\r
677   u16_t session, plen;\r
678   struct pppoe_softc *sc;\r
679   struct pppoehdr *ph;\r
680 #ifdef PPPOE_TERM_UNKNOWN_SESSIONS\r
681   u8_t shost[ETHER_ADDR_LEN];\r
682 #endif\r
683 \r
684 #ifdef PPPOE_TERM_UNKNOWN_SESSIONS\r
685   MEMCPY(shost, ((struct eth_hdr *)pb->payload)->src.addr, sizeof(shost));\r
686 #endif\r
687   if (pbuf_header(pb, -(int)sizeof(struct eth_hdr)) != 0) {\r
688     /* bail out */\r
689     PPPDEBUG((LOG_ERR, "pppoe_data_input: pbuf_header failed\n"));\r
690     LINK_STATS_INC(link.lenerr);\r
691     goto drop;\r
692   } \r
693 \r
694   pb = pppSingleBuf (pb);\r
695 \r
696   if (pb->len <= PPPOE_HEADERLEN) {\r
697     printf("pppoe (data): dropping too short packet: %d bytes\n", pb->len);\r
698     goto drop;\r
699   }\r
700 \r
701   if (pb->len < sizeof(*ph)) {\r
702     printf("pppoe_data_input: could not get PPPoE header\n");\r
703     goto drop;\r
704   }\r
705   ph = (struct pppoehdr *)pb->payload;\r
706 \r
707   if (ph->vertype != PPPOE_VERTYPE) {\r
708     printf("pppoe (data): unknown version/type packet: 0x%x\n", ph->vertype);\r
709     goto drop;\r
710   }\r
711   if (ph->code != 0) {\r
712     goto drop;\r
713   }\r
714 \r
715   session = ntohs(ph->session);\r
716   sc = pppoe_find_softc_by_session(session, netif);\r
717   if (sc == NULL) {\r
718 #ifdef PPPOE_TERM_UNKNOWN_SESSIONS\r
719     printf("pppoe: input for unknown session 0x%x, sending PADT\n", session);\r
720     pppoe_send_padt(netif, session, shost);\r
721 #endif\r
722     goto drop;\r
723   }\r
724 \r
725   plen = ntohs(ph->plen);\r
726 \r
727   if (pbuf_header(pb, -(int)(PPPOE_HEADERLEN)) != 0) {\r
728     /* bail out */\r
729     PPPDEBUG((LOG_ERR, "pppoe_data_input: pbuf_header PPPOE_HEADERLEN failed\n"));\r
730     LINK_STATS_INC(link.lenerr);\r
731     goto drop;\r
732   } \r
733 \r
734   PPPDEBUG((LOG_DEBUG, "pppoe_data_input: %c%c%"U16_F": pkthdr.len=%d, pppoe.len=%d\n",\r
735         sc->sc_ethif->name[0], sc->sc_ethif->name[1], sc->sc_ethif->num,\r
736         pb->len, plen));\r
737 \r
738   if (pb->len < plen) {\r
739     goto drop;\r
740   }\r
741 \r
742   pppInProcOverEthernet(sc->sc_pd, pb);\r
743 \r
744   return;\r
745 \r
746 drop:\r
747   pbuf_free(pb);\r
748 }\r
749 \r
750 static err_t\r
751 pppoe_output(struct pppoe_softc *sc, struct pbuf *pb)\r
752 {\r
753   struct eth_hdr *ethhdr;\r
754   u16_t etype;\r
755   err_t res;\r
756 \r
757   if (!sc->sc_ethif) {\r
758     pbuf_free(pb);\r
759     return ERR_IF;\r
760   }\r
761 \r
762   ethhdr = (struct eth_hdr *)pb->payload;\r
763   etype = sc->sc_state == PPPOE_STATE_SESSION ? ETHTYPE_PPPOE : ETHTYPE_PPPOEDISC;\r
764   ethhdr->type = htons(etype);\r
765   MEMCPY(ethhdr->dest.addr, sc->sc_dest.addr, sizeof(ethhdr->dest.addr));\r
766   MEMCPY(ethhdr->src.addr, ((struct eth_addr *)sc->sc_ethif->hwaddr)->addr, sizeof(ethhdr->src.addr));\r
767 \r
768   PPPDEBUG((LOG_DEBUG, "pppoe: %c%c%"U16_F" (%x) state=%d, session=0x%x output -> %02"X16_F":%02"X16_F":%02"X16_F":%02"X16_F":%02"X16_F":%02"X16_F", len=%d\n",\r
769       sc->sc_ethif->name[0], sc->sc_ethif->name[1], sc->sc_ethif->num, etype,\r
770       sc->sc_state, sc->sc_session,\r
771       sc->sc_dest.addr[0], sc->sc_dest.addr[1], sc->sc_dest.addr[2], sc->sc_dest.addr[3], sc->sc_dest.addr[4], sc->sc_dest.addr[5],\r
772       pb->tot_len));\r
773 \r
774   res = sc->sc_ethif->linkoutput(sc->sc_ethif, pb);\r
775 \r
776   pbuf_free(pb);\r
777 \r
778   return res;\r
779 }\r
780 \r
781 static err_t\r
782 pppoe_send_padi(struct pppoe_softc *sc)\r
783 {\r
784   struct pbuf *pb;\r
785   u8_t *p;\r
786   int len, l1 = 0, l2 = 0; /* XXX: gcc */\r
787 \r
788   if (sc->sc_state >PPPOE_STATE_PADI_SENT) {\r
789     PPPDEBUG((LOG_ERR, "ERROR: pppoe_send_padi in state %d", sc->sc_state));\r
790   }\r
791 \r
792   /* calculate length of frame (excluding ethernet header + pppoe header) */\r
793   len = 2 + 2 + 2 + 2 + sizeof sc;  /* service name tag is required, host unique is send too */\r
794   if (sc->sc_service_name != NULL) {\r
795     l1 = strlen(sc->sc_service_name);\r
796     len += l1;\r
797   }\r
798   if (sc->sc_concentrator_name != NULL) {\r
799     l2 = strlen(sc->sc_concentrator_name);\r
800     len += 2 + 2 + l2;\r
801   }\r
802 \r
803   /* allocate a buffer */\r
804   pb = pbuf_alloc(PBUF_LINK, sizeof(struct eth_hdr) + PPPOE_HEADERLEN + len, PBUF_RAM);\r
805   if (!pb) {\r
806     return ERR_MEM;\r
807   }\r
808 \r
809   p = (u8_t*)pb->payload + sizeof (struct eth_hdr);\r
810   /* fill in pkt */\r
811   PPPOE_ADD_HEADER(p, PPPOE_CODE_PADI, 0, len);\r
812   PPPOE_ADD_16(p, PPPOE_TAG_SNAME);\r
813   if (sc->sc_service_name != NULL) {\r
814     PPPOE_ADD_16(p, l1);\r
815     MEMCPY(p, sc->sc_service_name, l1);\r
816     p += l1;\r
817   } else {\r
818     PPPOE_ADD_16(p, 0);\r
819   }\r
820   if (sc->sc_concentrator_name != NULL) {\r
821     PPPOE_ADD_16(p, PPPOE_TAG_ACNAME);\r
822     PPPOE_ADD_16(p, l2);\r
823     MEMCPY(p, sc->sc_concentrator_name, l2);\r
824     p += l2;\r
825   }\r
826   PPPOE_ADD_16(p, PPPOE_TAG_HUNIQUE);\r
827   PPPOE_ADD_16(p, sizeof(sc));\r
828   MEMCPY(p, &sc, sizeof sc);\r
829 \r
830   /* send pkt */\r
831   return pppoe_output(sc, pb);\r
832 }\r
833 \r
834 static void\r
835 pppoe_timeout(void *arg)\r
836 {\r
837   int retry_wait, err;\r
838   struct pppoe_softc *sc = (struct pppoe_softc*)arg;\r
839 \r
840   PPPDEBUG((LOG_DEBUG, "pppoe: %c%c%"U16_F": timeout\n", sc->sc_ethif->name[0], sc->sc_ethif->name[1], sc->sc_ethif->num));\r
841 \r
842   switch (sc->sc_state) {\r
843     case PPPOE_STATE_PADI_SENT:\r
844       /*\r
845        * We have two basic ways of retrying:\r
846        *  - Quick retry mode: try a few times in short sequence\r
847        *  - Slow retry mode: we already had a connection successfully\r
848        *    established and will try infinitely (without user\r
849        *    intervention)\r
850        * We only enter slow retry mode if IFF_LINK1 (aka autodial)\r
851        * is not set.\r
852        */\r
853 \r
854       /* initialize for quick retry mode */\r
855       retry_wait = PPPOE_DISC_TIMEOUT * (1 + sc->sc_padi_retried);\r
856 \r
857       sc->sc_padi_retried++;\r
858       if (sc->sc_padi_retried >= PPPOE_DISC_MAXPADI) {\r
859 #if 0\r
860         if ((sc->sc_sppp.pp_if.if_flags & IFF_LINK1) == 0) {\r
861           /* slow retry mode */\r
862           retry_wait = PPPOE_SLOW_RETRY;\r
863         } else\r
864 #endif\r
865         {\r
866           pppoe_abort_connect(sc);\r
867           return;\r
868         }\r
869       }\r
870       if ((err = pppoe_send_padi(sc)) != 0) {\r
871         sc->sc_padi_retried--;\r
872         PPPDEBUG((LOG_DEBUG, "pppoe: %c%c%"U16_F": failed to transmit PADI, error=%d\n", sc->sc_ethif->name[0], sc->sc_ethif->name[1], sc->sc_ethif->num, err));\r
873       }\r
874       tcpip_timeout(retry_wait, pppoe_timeout, sc);\r
875       break;\r
876 \r
877     case PPPOE_STATE_PADR_SENT:\r
878       sc->sc_padr_retried++;\r
879       if (sc->sc_padr_retried >= PPPOE_DISC_MAXPADR) {\r
880         MEMCPY(&sc->sc_dest, ethbroadcast.addr, sizeof(sc->sc_dest));\r
881         sc->sc_state = PPPOE_STATE_PADI_SENT;\r
882         sc->sc_padr_retried = 0;\r
883         if ((err = pppoe_send_padi(sc)) != 0) {\r
884           PPPDEBUG((LOG_DEBUG, "pppoe: %c%c%"U16_F": failed to send PADI, error=%d\n", sc->sc_ethif->name[0], sc->sc_ethif->name[1], sc->sc_ethif->num, err));\r
885         }\r
886         tcpip_timeout(PPPOE_DISC_TIMEOUT * (1 + sc->sc_padi_retried), pppoe_timeout, sc);\r
887         return;\r
888       }\r
889       if ((err = pppoe_send_padr(sc)) != 0) {\r
890         sc->sc_padr_retried--;\r
891         PPPDEBUG((LOG_DEBUG, "pppoe: %c%c%"U16_F": failed to send PADR, error=%d\n", sc->sc_ethif->name[0], sc->sc_ethif->name[1], sc->sc_ethif->num, err));\r
892       }\r
893       tcpip_timeout(PPPOE_DISC_TIMEOUT * (1 + sc->sc_padr_retried), pppoe_timeout, sc);\r
894       break;\r
895     case PPPOE_STATE_CLOSING:\r
896       pppoe_do_disconnect(sc);\r
897       break;\r
898     default:\r
899       return;  /* all done, work in peace */\r
900   }\r
901 }\r
902 \r
903 /* Start a connection (i.e. initiate discovery phase) */\r
904 int\r
905 pppoe_connect(struct pppoe_softc *sc)\r
906 {\r
907   int err;\r
908 \r
909   if (sc->sc_state != PPPOE_STATE_INITIAL) {\r
910     return EBUSY;\r
911   }\r
912 \r
913 #ifdef PPPOE_SERVER\r
914   /* wait PADI if IFF_PASSIVE */\r
915   if ((sc->sc_sppp.pp_if.if_flags & IFF_PASSIVE)) {\r
916     return 0;\r
917   }\r
918 #endif\r
919   /* save state, in case we fail to send PADI */\r
920   sc->sc_state = PPPOE_STATE_PADI_SENT;\r
921   sc->sc_padr_retried = 0;\r
922   err = pppoe_send_padi(sc);\r
923   PPPDEBUG((LOG_DEBUG, "pppoe: %c%c%"U16_F": failed to send PADI, error=%d\n", sc->sc_ethif->name[0], sc->sc_ethif->name[1], sc->sc_ethif->num, err));\r
924   tcpip_timeout(PPPOE_DISC_TIMEOUT, pppoe_timeout, sc);\r
925   return err;\r
926 }\r
927 \r
928 /* disconnect */\r
929 void\r
930 pppoe_disconnect(struct pppoe_softc *sc)\r
931 {\r
932   if (sc->sc_state < PPPOE_STATE_SESSION) {\r
933     return;\r
934   }\r
935   /*\r
936    * Do not call pppoe_disconnect here, the upper layer state\r
937    * machine gets confused by this. We must return from this\r
938    * function and defer disconnecting to the timeout handler.\r
939    */\r
940   sc->sc_state = PPPOE_STATE_CLOSING;\r
941   tcpip_timeout(20, pppoe_timeout, sc);\r
942 }\r
943 \r
944 static int\r
945 pppoe_do_disconnect(struct pppoe_softc *sc)\r
946 {\r
947   int err;\r
948 \r
949   if (sc->sc_state < PPPOE_STATE_SESSION) {\r
950     err = EBUSY;\r
951   } else {\r
952     PPPDEBUG((LOG_DEBUG, "pppoe: %c%c%"U16_F": disconnecting\n", sc->sc_ethif->name[0], sc->sc_ethif->name[1], sc->sc_ethif->num));\r
953     err = pppoe_send_padt(sc->sc_ethif, sc->sc_session, (const u8_t *)&sc->sc_dest);\r
954   }\r
955 \r
956   /* cleanup softc */\r
957   sc->sc_state = PPPOE_STATE_INITIAL;\r
958   MEMCPY(&sc->sc_dest, ethbroadcast.addr, sizeof(sc->sc_dest));\r
959   if (sc->sc_ac_cookie) {\r
960     mem_free(sc->sc_ac_cookie);\r
961     sc->sc_ac_cookie = NULL;\r
962   }\r
963   sc->sc_ac_cookie_len = 0;\r
964 #ifdef PPPOE_SERVER\r
965   if (sc->sc_hunique) {\r
966     mem_free(sc->sc_hunique);\r
967     sc->sc_hunique = NULL;\r
968   }\r
969   sc->sc_hunique_len = 0;\r
970 #endif\r
971   sc->sc_session = 0;\r
972 \r
973   sc->sc_linkStatusCB(sc->sc_pd, 0); /* notify upper layers */\r
974 \r
975   return err;\r
976 }\r
977 \r
978 /* Connection attempt aborted */\r
979 static void\r
980 pppoe_abort_connect(struct pppoe_softc *sc)\r
981 {\r
982   printf("%c%c%"U16_F": could not establish connection\n", sc->sc_ethif->name[0], sc->sc_ethif->name[1], sc->sc_ethif->num);\r
983   sc->sc_state = PPPOE_STATE_CLOSING;\r
984 \r
985   sc->sc_linkStatusCB(sc->sc_pd, 0); /* notify upper layers */\r
986 \r
987   /* clear connection state */\r
988   MEMCPY(&sc->sc_dest, ethbroadcast.addr, sizeof(sc->sc_dest));\r
989   sc->sc_state = PPPOE_STATE_INITIAL;\r
990 }\r
991 \r
992 /* Send a PADR packet */\r
993 static err_t\r
994 pppoe_send_padr(struct pppoe_softc *sc)\r
995 {\r
996   struct pbuf *pb;\r
997   u8_t *p;\r
998   size_t len, l1 = 0; /* XXX: gcc */\r
999 \r
1000   if (sc->sc_state != PPPOE_STATE_PADR_SENT) {\r
1001     return ERR_CONN;\r
1002   }\r
1003 \r
1004   len = 2 + 2 + 2 + 2 + sizeof(sc);    /* service name, host unique */\r
1005   if (sc->sc_service_name != NULL) {    /* service name tag maybe empty */\r
1006     l1 = strlen(sc->sc_service_name);\r
1007     len += l1;\r
1008   }\r
1009   if (sc->sc_ac_cookie_len > 0) {\r
1010     len += 2 + 2 + sc->sc_ac_cookie_len;  /* AC cookie */\r
1011   }\r
1012   pb = pbuf_alloc(PBUF_LINK, sizeof(struct eth_hdr) + PPPOE_HEADERLEN + len, PBUF_RAM);\r
1013   if (!pb) {\r
1014     return ERR_MEM;\r
1015   }\r
1016   p = (u8_t*)pb->payload + sizeof (struct eth_hdr);\r
1017   PPPOE_ADD_HEADER(p, PPPOE_CODE_PADR, 0, len);\r
1018   PPPOE_ADD_16(p, PPPOE_TAG_SNAME);\r
1019   if (sc->sc_service_name != NULL) {\r
1020     PPPOE_ADD_16(p, l1);\r
1021     MEMCPY(p, sc->sc_service_name, l1);\r
1022     p += l1;\r
1023   } else {\r
1024     PPPOE_ADD_16(p, 0);\r
1025   }\r
1026   if (sc->sc_ac_cookie_len > 0) {\r
1027     PPPOE_ADD_16(p, PPPOE_TAG_ACCOOKIE);\r
1028     PPPOE_ADD_16(p, sc->sc_ac_cookie_len);\r
1029     MEMCPY(p, sc->sc_ac_cookie, sc->sc_ac_cookie_len);\r
1030     p += sc->sc_ac_cookie_len;\r
1031   }\r
1032   PPPOE_ADD_16(p, PPPOE_TAG_HUNIQUE);\r
1033   PPPOE_ADD_16(p, sizeof(sc));\r
1034   MEMCPY(p, &sc, sizeof sc);\r
1035 \r
1036   return pppoe_output(sc, pb);\r
1037 }\r
1038 \r
1039 /* send a PADT packet */\r
1040 static err_t\r
1041 pppoe_send_padt(struct netif *outgoing_if, u_int session, const u8_t *dest)\r
1042 {\r
1043   struct pbuf *pb;\r
1044   struct eth_hdr *ethhdr;\r
1045   err_t res;\r
1046   u8_t *p;\r
1047 \r
1048   pb = pbuf_alloc(PBUF_LINK, sizeof(struct eth_hdr) + PPPOE_HEADERLEN, PBUF_RAM);\r
1049   if (!pb) {\r
1050     return ERR_MEM;\r
1051   }\r
1052 \r
1053   ethhdr = (struct eth_hdr *)pb->payload;\r
1054   ethhdr->type = htons(ETHTYPE_PPPOEDISC);\r
1055   MEMCPY(ethhdr->dest.addr, dest, sizeof(ethhdr->dest.addr));\r
1056   MEMCPY(ethhdr->src.addr, ((struct eth_addr *)outgoing_if->hwaddr)->addr, sizeof(ethhdr->src.addr));\r
1057 \r
1058   p = (u8_t*)(ethhdr + 1);\r
1059   PPPOE_ADD_HEADER(p, PPPOE_CODE_PADT, session, 0);\r
1060 \r
1061   res = outgoing_if->linkoutput(outgoing_if, pb);\r
1062 \r
1063   pbuf_free(pb);\r
1064 \r
1065   return res;\r
1066 }\r
1067 \r
1068 #ifdef PPPOE_SERVER\r
1069 static err_t\r
1070 pppoe_send_pado(struct pppoe_softc *sc)\r
1071 {\r
1072   struct pbuf *pb;\r
1073   u8_t *p;\r
1074   size_t len;\r
1075 \r
1076   if (sc->sc_state != PPPOE_STATE_PADO_SENT) {\r
1077     return ERR_CONN;\r
1078   }\r
1079 \r
1080   /* calc length */\r
1081   len = 0;\r
1082   /* include ac_cookie */\r
1083   len += 2 + 2 + sizeof(sc);\r
1084   /* include hunique */\r
1085   len += 2 + 2 + sc->sc_hunique_len;\r
1086   pb = pbuf_alloc(PBUF_LINK, sizeof(struct eth_hdr) + PPPOE_HEADERLEN + len, PBUF_RAM);\r
1087   if (!pb) {\r
1088     return ERR_MEM;\r
1089   }\r
1090   p = (u8_t*)pb->payload + sizeof (struct eth_hdr);\r
1091   PPPOE_ADD_HEADER(p, PPPOE_CODE_PADO, 0, len);\r
1092   PPPOE_ADD_16(p, PPPOE_TAG_ACCOOKIE);\r
1093   PPPOE_ADD_16(p, sizeof(sc));\r
1094   MEMCPY(p, &sc, sizeof(sc));\r
1095   p += sizeof(sc);\r
1096   PPPOE_ADD_16(p, PPPOE_TAG_HUNIQUE);\r
1097   PPPOE_ADD_16(p, sc->sc_hunique_len);\r
1098   MEMCPY(p, sc->sc_hunique, sc->sc_hunique_len);\r
1099   return pppoe_output(sc, pb);\r
1100 }\r
1101 \r
1102 static err_t\r
1103 pppoe_send_pads(struct pppoe_softc *sc)\r
1104 {\r
1105   struct pbuf *pb;\r
1106   u8_t *p;\r
1107   size_t len, l1 = 0;  /* XXX: gcc */\r
1108 \r
1109   if (sc->sc_state != PPPOE_STATE_PADO_SENT) {\r
1110     return ERR_CONN;\r
1111   }\r
1112 \r
1113   sc->sc_session = mono_time.tv_sec % 0xff + 1;\r
1114   /* calc length */\r
1115   len = 0;\r
1116   /* include hunique */\r
1117   len += 2 + 2 + 2 + 2 + sc->sc_hunique_len;  /* service name, host unique*/\r
1118   if (sc->sc_service_name != NULL) {    /* service name tag maybe empty */\r
1119     l1 = strlen(sc->sc_service_name);\r
1120     len += l1;\r
1121   }\r
1122   pb = pbuf_alloc(PBUF_LINK, sizeof(struct eth_hdr) + PPPOE_HEADERLEN + len, PBUF_RAM);\r
1123   if (!pb) {\r
1124     return ERR_MEM;\r
1125   }\r
1126   p = (u8_t*)pb->payload + sizeof (struct eth_hdr);\r
1127   PPPOE_ADD_HEADER(p, PPPOE_CODE_PADS, sc->sc_session, len);\r
1128   PPPOE_ADD_16(p, PPPOE_TAG_SNAME);\r
1129   if (sc->sc_service_name != NULL) {\r
1130     PPPOE_ADD_16(p, l1);\r
1131     MEMCPY(p, sc->sc_service_name, l1);\r
1132     p += l1;\r
1133   } else {\r
1134     PPPOE_ADD_16(p, 0);\r
1135   }\r
1136   PPPOE_ADD_16(p, PPPOE_TAG_HUNIQUE);\r
1137   PPPOE_ADD_16(p, sc->sc_hunique_len);\r
1138   MEMCPY(p, sc->sc_hunique, sc->sc_hunique_len);\r
1139   return pppoe_output(sc, pb);\r
1140 }\r
1141 #endif\r
1142 \r
1143 err_t\r
1144 pppoe_xmit(struct pppoe_softc *sc, struct pbuf *pb)\r
1145 {\r
1146   u8_t *p;\r
1147   size_t len;\r
1148 \r
1149   /* are we ready to process data yet? */\r
1150   if (sc->sc_state < PPPOE_STATE_SESSION) {\r
1151     /*sppp_flush(&sc->sc_sppp.pp_if);*/\r
1152     pbuf_free(pb);\r
1153     return ERR_CONN;\r
1154   }\r
1155 \r
1156   len = pb->tot_len;\r
1157 \r
1158   /* make room for Ethernet header - should not fail */\r
1159   if (pbuf_header(pb, sizeof(struct eth_hdr) + PPPOE_HEADERLEN) != 0) {\r
1160     /* bail out */\r
1161     PPPDEBUG((LOG_ERR, "pppoe: %c%c%"U16_F": pppoe_xmit: could not allocate room for header\n", sc->sc_ethif->name[0], sc->sc_ethif->name[1], sc->sc_ethif->num));\r
1162     LINK_STATS_INC(link.lenerr);\r
1163     pbuf_free(pb);\r
1164     return ERR_BUF;\r
1165   } \r
1166 \r
1167   p = (u8_t*)pb->payload + sizeof(struct eth_hdr);\r
1168   PPPOE_ADD_HEADER(p, 0, sc->sc_session, len);\r
1169 \r
1170   return pppoe_output(sc, pb);\r
1171 }\r
1172 \r
1173 #if 0 /*def PFIL_HOOKS*/\r
1174 static int\r
1175 pppoe_ifattach_hook(void *arg, struct pbuf **mp, struct netif *ifp, int dir)\r
1176 {\r
1177   struct pppoe_softc *sc;\r
1178   int s;\r
1179 \r
1180   if (mp != (struct pbuf **)PFIL_IFNET_DETACH) {\r
1181     return 0;\r
1182   }\r
1183 \r
1184   LIST_FOREACH(sc, &pppoe_softc_list, sc_list) {\r
1185     if (sc->sc_ethif != ifp) {\r
1186       continue;\r
1187     }\r
1188     if (sc->sc_sppp.pp_if.if_flags & IFF_UP) {\r
1189       sc->sc_sppp.pp_if.if_flags &= ~(IFF_UP|IFF_RUNNING);\r
1190       printf("%c%c%"U16_F": ethernet interface detached, going down\n",\r
1191           sc->sc_ethif->name[0], sc->sc_ethif->name[1], sc->sc_ethif->num);\r
1192     }\r
1193     sc->sc_ethif = NULL;\r
1194     pppoe_clear_softc(sc, "ethernet interface detached");\r
1195   }\r
1196 \r
1197   return 0;\r
1198 }\r
1199 #endif\r
1200 \r
1201 static void\r
1202 pppoe_clear_softc(struct pppoe_softc *sc, const char *message)\r
1203 {\r
1204   LWIP_UNUSED_ARG(message);\r
1205 \r
1206   /* stop timer */\r
1207   tcpip_untimeout(pppoe_timeout, sc);\r
1208   PPPDEBUG((LOG_DEBUG, "pppoe: %c%c%"U16_F": session 0x%x terminated, %s\n", sc->sc_ethif->name[0], sc->sc_ethif->name[1], sc->sc_ethif->num, sc->sc_session, message));\r
1209 \r
1210   /* fix our state */\r
1211   sc->sc_state = PPPOE_STATE_INITIAL;\r
1212 \r
1213   /* notify upper layers */\r
1214   sc->sc_linkStatusCB(sc->sc_pd, 0);\r
1215 \r
1216   /* clean up softc */\r
1217   MEMCPY(&sc->sc_dest, ethbroadcast.addr, sizeof(sc->sc_dest));\r
1218   if (sc->sc_ac_cookie) {\r
1219     mem_free(sc->sc_ac_cookie);\r
1220     sc->sc_ac_cookie = NULL;\r
1221   }\r
1222   sc->sc_ac_cookie_len = 0;\r
1223   sc->sc_session = 0;\r
1224 }\r
1225 \r
1226 #endif /* PPPOE_SUPPORT */\r
1227 \r