NetTribute library with debug turned on in FShandler Donatien Garner -> Segundo Equipo -> this version

Committer:
hexley
Date:
Fri Nov 19 01:54:45 2010 +0000
Revision:
0:281d6ff68967

        

Who changed what in which revision?

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