I have a problem getting this to work. Server only recieves half of the data being sent. Whats wrong

Dependencies:   mbed

Committer:
tax
Date:
Tue Mar 29 13:20:15 2011 +0000
Revision:
0:66300c77c6e9

        

Who changed what in which revision?

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