Michael Fischler / Mbed 2 deprecated Contest_Winner

Dependencies:   mbed

Committer:
mafischl
Date:
Thu Oct 13 17:02:29 2011 +0000
Revision:
0:d9266031f832

        

Who changed what in which revision?

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