⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ppp_oe.c

📁 lwip-1.4.0
💻 C
📖 第 1 页 / 共 3 页
字号:
/****************************************************************************** ppp_oe.c - PPP Over Ethernet implementation for lwIP.** Copyright (c) 2006 by Marc Boucher, Services Informatiques (MBSI) inc.** The authors hereby grant permission to use, copy, modify, distribute,* and license this software and its documentation for any purpose, provided* that existing copyright notices are retained in all copies and that this* notice and the following disclaimer are included verbatim in any * distributions. No written agreement, license, or royalty fee is required* for any of the authorized uses.** THIS SOFTWARE IS PROVIDED BY THE CONTRIBUTORS *AS IS* AND ANY EXPRESS OR* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. * IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.******************************************************************************** REVISION HISTORY** 06-01-01 Marc Boucher <marc@mbsi.ca>*   Ported to lwIP.*****************************************************************************//* based on NetBSD: if_pppoe.c,v 1.64 2006/01/31 23:50:15 martin Exp *//*- * Copyright (c) 2002 The NetBSD Foundation, Inc. * All rights reserved. * * This code is derived from software contributed to The NetBSD Foundation * by Martin Husemann <martin@NetBSD.org>. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright *    notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright *    notice, this list of conditions and the following disclaimer in the *    documentation and/or other materials provided with the distribution. * 3. All advertising materials mentioning features or use of this software *    must display the following acknowledgement: *        This product includes software developed by the NetBSD *        Foundation, Inc. and its contributors. * 4. Neither the name of The NetBSD Foundation nor the names of its *    contributors may be used to endorse or promote products derived *    from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */#include "lwip/opt.h"#if PPPOE_SUPPORT /* don't build if not configured for use in lwipopts.h */#include "netif/ppp_oe.h"#include "ppp.h"#include "pppdebug.h"#include "lwip/timers.h"#include "lwip/memp.h"#include <string.h>#include <stdio.h>/* Add a 16 bit unsigned value to a buffer pointed to by PTR */#define PPPOE_ADD_16(PTR, VAL) \    *(PTR)++ = (u8_t)((VAL) / 256);    \    *(PTR)++ = (u8_t)((VAL) % 256)/* Add a complete PPPoE header to the buffer pointed to by PTR */#define PPPOE_ADD_HEADER(PTR, CODE, SESS, LEN)  \    *(PTR)++ = PPPOE_VERTYPE;  \    *(PTR)++ = (CODE);         \    PPPOE_ADD_16(PTR, SESS);   \    PPPOE_ADD_16(PTR, LEN)#define PPPOE_DISC_TIMEOUT (5*1000)  /* base for quick timeout calculation */#define PPPOE_SLOW_RETRY   (60*1000) /* persistent retry interval */#define PPPOE_DISC_MAXPADI  4        /* retry PADI four times (quickly) */#define PPPOE_DISC_MAXPADR  2        /* retry PADR twice */#ifdef PPPOE_SERVER#error "PPPOE_SERVER is not yet supported under lwIP!"/* from if_spppsubr.c */#define IFF_PASSIVE IFF_LINK0 /* wait passively for connection */#endif#ifndef PPPOE_ERRORSTRING_LEN#define PPPOE_ERRORSTRING_LEN     64#endifstatic char pppoe_error_tmp[PPPOE_ERRORSTRING_LEN];/* input routines */static void pppoe_dispatch_disc_pkt(struct netif *, struct pbuf *);/* management routines */static int pppoe_do_disconnect(struct pppoe_softc *);static void pppoe_abort_connect(struct pppoe_softc *);static void pppoe_clear_softc(struct pppoe_softc *, const char *);/* internal timeout handling */static void pppoe_timeout(void *);/* sending actual protocol controll packets */static err_t pppoe_send_padi(struct pppoe_softc *);static err_t pppoe_send_padr(struct pppoe_softc *);#ifdef PPPOE_SERVERstatic err_t pppoe_send_pado(struct pppoe_softc *);static err_t pppoe_send_pads(struct pppoe_softc *);#endifstatic err_t pppoe_send_padt(struct netif *, u_int, const u8_t *);/* internal helper functions */static struct pppoe_softc * pppoe_find_softc_by_session(u_int, struct netif *);static struct pppoe_softc * pppoe_find_softc_by_hunique(u8_t *, size_t, struct netif *);/** linked list of created pppoe interfaces */static struct pppoe_softc *pppoe_softc_list;err_tpppoe_create(struct netif *ethif, int pd, void (*linkStatusCB)(int pd, int up), struct pppoe_softc **scptr){  struct pppoe_softc *sc;  sc = (struct pppoe_softc *)memp_malloc(MEMP_PPPOE_IF);  if (sc == NULL) {    *scptr = NULL;    return ERR_MEM;  }  memset(sc, 0, sizeof(struct pppoe_softc));  /* changed to real address later */  MEMCPY(&sc->sc_dest, ethbroadcast.addr, sizeof(sc->sc_dest));  sc->sc_pd = pd;  sc->sc_linkStatusCB = linkStatusCB;  sc->sc_ethif = ethif;  /* put the new interface at the head of the list */  sc->next = pppoe_softc_list;  pppoe_softc_list = sc;  *scptr = sc;  return ERR_OK;}err_tpppoe_destroy(struct netif *ifp){  struct pppoe_softc *sc, *prev = NULL;  for (sc = pppoe_softc_list; sc != NULL; prev = sc, sc = sc->next) {    if (sc->sc_ethif == ifp) {      break;    }  }  if(!(sc && (sc->sc_ethif == ifp))) {    return ERR_IF;  }  sys_untimeout(pppoe_timeout, sc);  if (prev == NULL) {    /* remove sc from the head of the list */    pppoe_softc_list = sc->next;  } else {    /* remove sc from the list */    prev->next = sc->next;  }#ifdef PPPOE_TODO  if (sc->sc_concentrator_name) {    mem_free(sc->sc_concentrator_name);  }  if (sc->sc_service_name) {    mem_free(sc->sc_service_name);  }#endif /* PPPOE_TODO */  memp_free(MEMP_PPPOE_IF, sc);  return ERR_OK;}/* * Find the interface handling the specified session. * Note: O(number of sessions open), this is a client-side only, mean * and lean implementation, so number of open sessions typically should * be 1. */static struct pppoe_softc *pppoe_find_softc_by_session(u_int session, struct netif *rcvif){  struct pppoe_softc *sc;  if (session == 0) {    return NULL;  }  for (sc = pppoe_softc_list; sc != NULL; sc = sc->next) {    if (sc->sc_state == PPPOE_STATE_SESSION        && sc->sc_session == session) {      if (sc->sc_ethif == rcvif) {        return sc;      } else {        return NULL;      }    }  }  return NULL;}/* Check host unique token passed and return appropriate softc pointer, * or NULL if token is bogus. */static struct pppoe_softc *pppoe_find_softc_by_hunique(u8_t *token, size_t len, struct netif *rcvif){  struct pppoe_softc *sc, *t;  if (pppoe_softc_list == NULL) {    return NULL;  }  if (len != sizeof sc) {    return NULL;  }  MEMCPY(&t, token, len);  for (sc = pppoe_softc_list; sc != NULL; sc = sc->next) {    if (sc == t) {      break;    }  }  if (sc == NULL) {    PPPDEBUG(LOG_DEBUG, ("pppoe: alien host unique tag, no session found\n"));    return NULL;  }  /* should be safe to access *sc now */  if (sc->sc_state < PPPOE_STATE_PADI_SENT || sc->sc_state >= PPPOE_STATE_SESSION) {    printf("%c%c%"U16_F": host unique tag found, but it belongs to a connection in state %d\n",      sc->sc_ethif->name[0], sc->sc_ethif->name[1], sc->sc_ethif->num, sc->sc_state);    return NULL;  }  if (sc->sc_ethif != rcvif) {    printf("%c%c%"U16_F": wrong interface, not accepting host unique\n",      sc->sc_ethif->name[0], sc->sc_ethif->name[1], sc->sc_ethif->num);    return NULL;  }  return sc;}static voidpppoe_linkstatus_up(struct pppoe_softc *sc){  sc->sc_linkStatusCB(sc->sc_pd, 1);}/* analyze and handle a single received packet while not in session state */static voidpppoe_dispatch_disc_pkt(struct netif *netif, struct pbuf *pb){  u16_t tag, len;  u16_t session, plen;  struct pppoe_softc *sc;  const char *err_msg;  char devname[6];  u8_t *ac_cookie;  u16_t ac_cookie_len;#ifdef PPPOE_SERVER  u8_t *hunique;  size_t hunique_len;#endif  struct pppoehdr *ph;  struct pppoetag pt;  int off, err, errortag;  struct eth_hdr *ethhdr;  pb = pppSingleBuf(pb);  strcpy(devname, "pppoe");  /* as long as we don't know which instance */  err_msg = NULL;  errortag = 0;  if (pb->len < sizeof(*ethhdr)) {    goto done;  }  ethhdr = (struct eth_hdr *)pb->payload;  off = sizeof(*ethhdr);  ac_cookie = NULL;  ac_cookie_len = 0;#ifdef PPPOE_SERVER  hunique = NULL;  hunique_len = 0;#endif  session = 0;  if (pb->len - off < PPPOE_HEADERLEN) {    printf("pppoe: packet too short: %d\n", pb->len);    goto done;  }  ph = (struct pppoehdr *) (ethhdr + 1);  if (ph->vertype != PPPOE_VERTYPE) {    printf("pppoe: unknown version/type packet: 0x%x\n", ph->vertype);    goto done;  }  session = ntohs(ph->session);  plen = ntohs(ph->plen);  off += sizeof(*ph);  if (plen + off > pb->len) {    printf("pppoe: packet content does not fit: data available = %d, packet size = %u\n",        pb->len - off, plen);    goto done;  }  if(pb->tot_len == pb->len) {    pb->tot_len = pb->len = (u16_t)off + plen; /* ignore trailing garbage */  }  tag = 0;  len = 0;  sc = NULL;  while (off + sizeof(pt) <= pb->len) {    MEMCPY(&pt, (u8_t*)pb->payload + off, sizeof(pt));    tag = ntohs(pt.tag);    len = ntohs(pt.len);    if (off + sizeof(pt) + len > pb->len) {      printf("pppoe: tag 0x%x len 0x%x is too long\n", tag, len);      goto done;    }    switch (tag) {      case PPPOE_TAG_EOL:        goto breakbreak;      case PPPOE_TAG_SNAME:        break;  /* ignored */      case PPPOE_TAG_ACNAME:        break;  /* ignored */      case PPPOE_TAG_HUNIQUE:        if (sc != NULL) {          break;        }#ifdef PPPOE_SERVER        hunique = (u8_t*)pb->payload + off + sizeof(pt);        hunique_len = len;#endif        sc = pppoe_find_softc_by_hunique((u8_t*)pb->payload + off + sizeof(pt), len, netif);        if (sc != NULL) {          snprintf(devname, sizeof(devname), "%c%c%"U16_F, sc->sc_ethif->name[0], sc->sc_ethif->name[1], sc->sc_ethif->num);        }        break;      case PPPOE_TAG_ACCOOKIE:        if (ac_cookie == NULL) {          ac_cookie = (u8_t*)pb->payload + off + sizeof(pt);          ac_cookie_len = len;        }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -