Ethernet test for ECE 4180 and others to find your IP address and do a simple HTTP GET request over port 80.

Dependencies:   mbed Socket lwip-eth lwip-sys lwip

Committer:
mkersh3
Date:
Thu Apr 04 05:26:09 2013 +0000
Revision:
0:e7ca326e76ee
Ethernet Test for ECE4180 and others to find their IP Address and do a simple HTTP GET request over port 80.

Who changed what in which revision?

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