Contains example code to connect the mbed LPC1768 or FRDM-K64F devices to the IBM Internet of Things Cloud service via ethernet.

Dependencies:   C12832 MQTT LM75B MMA7660

Dependents:   MFT_IoT_demo_USB400 IBM_RFID

Committer:
samdanbury
Date:
Wed Aug 20 12:45:14 2014 +0000
Revision:
6:37b6d0d56190
Code completely changed to improve the structure, flow and memory usage of the application

Who changed what in which revision?

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