Web server based weather station using Sparkfun Weather Meters.

Dependencies:   FatFileSystem mbed WeatherMeters SDFileSystem

Committer:
AdamGreen
Date:
Sat Feb 25 03:28:05 2012 +0000
Revision:
1:c7958aa34fa1
Parent:
0:616601bde9fb
Use published libraries where possible.

Who changed what in which revision?

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