Netservices modded to read fragmented HTTP respsonse/payload from special purpose server - 180 bytes only

Committer:
RodColeman
Date:
Thu Sep 08 10:48:09 2011 +0000
Revision:
0:850eacf3e945
revised fixed length to 178 bytes

Who changed what in which revision?

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