ppp_oe.c 33 KB

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