Dependencies:   FatFileSystem mbed WeatherMeters SDFileSystem

Committer:
dcoban
Date:
Tue Apr 03 18:43:13 2012 +0000
Revision:
0:1a61c61d0845

        

Who changed what in which revision?

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