Version of http://mbed.org/cookbook/NetServicesTribute with setting set the same for LPC2368

Dependents:   UDPSocketExample 24LCxx_I2CApp WeatherPlatform_pachube HvZServerLib ... more

Committer:
simon
Date:
Tue Nov 23 14:15:36 2010 +0000
Revision:
0:350011bf8be7
Experimental version for testing UDP

Who changed what in which revision?

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