Michiel Berckvens / Mbed 2 deprecated ProjectHTTP

Dependencies:   DS1307 TextLCD mbed

Committer:
Michielber
Date:
Thu Dec 04 10:36:40 2014 +0000
Revision:
0:f615d151a72c
Berckvens Michiel & Basteyns Jonas 4/12/2014

Who changed what in which revision?

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