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

📄 ppp_oe.c

📁 最新的lwip 1.3.0版本在ucos平台上的移植
💻 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 "ppp.h"#include "pppdebug.h"#include "lwip/sys.h"#include "netif/ppp_oe.h"#include "netif/etharp.h"#include <string.h>#include <stdio.h>/** @todo Replace this part with a simple list like other lwIP lists */#ifndef _SYS_QUEUE_H_#define _SYS_QUEUE_H_/* * A list is headed by a single forward pointer (or an array of forward * pointers for a hash table header). The elements are doubly linked * so that an arbitrary element can be removed without a need to * traverse the list. New elements can be added to the list before * or after an existing element or at the head of the list. A list * may only be traversed in the forward direction. * * For details on the use of these macros, see the queue(3) manual page. *//* * List declarations. */#define  LIST_HEAD(name, type)                                                 \struct name {                                                                  \  struct type *lh_first;  /* first element */                                  \}#define  LIST_HEAD_INITIALIZER(head)                                           \  { NULL }#define  LIST_ENTRY(type)                                                      \struct {                                                                       \  struct type *le_next;  /* next element */                                    \  struct type **le_prev; /* address of previous next element */                \}/* * List functions. */#define  LIST_EMPTY(head)  ((head)->lh_first == NULL)#define  LIST_FIRST(head)  ((head)->lh_first)#define  LIST_FOREACH(var, head, field)                                        \  for ((var) = LIST_FIRST((head));                                             \      (var);                                                                   \      (var) = LIST_NEXT((var), field))#define  LIST_INIT(head) do {                                                  \  LIST_FIRST((head)) = NULL;                                                   \} while (0)#define  LIST_INSERT_AFTER(listelm, elm, field) do {                           \  if ((LIST_NEXT((elm), field) = LIST_NEXT((listelm), field)) != NULL)         \    LIST_NEXT((listelm), field)->field.le_prev =                               \        &LIST_NEXT((elm), field);                                              \  LIST_NEXT((listelm), field) = (elm);                                         \  (elm)->field.le_prev = &LIST_NEXT((listelm), field);                         \} while (0)#define  LIST_INSERT_BEFORE(listelm, elm, field) do {                          \  (elm)->field.le_prev = (listelm)->field.le_prev;                             \  LIST_NEXT((elm), field) = (listelm);                                         \  *(listelm)->field.le_prev = (elm);                                           \  (listelm)->field.le_prev = &LIST_NEXT((elm), field);                         \} while (0)#define  LIST_INSERT_HEAD(head, elm, field) do {                               \  if ((LIST_NEXT((elm), field) = LIST_FIRST((head))) != NULL)                  \    LIST_FIRST((head))->field.le_prev = &LIST_NEXT((elm), field);              \  LIST_FIRST((head)) = (elm);                                                  \  (elm)->field.le_prev = &LIST_FIRST((head));                                  \} while (0)#define  LIST_NEXT(elm, field)  ((elm)->field.le_next)#define  LIST_REMOVE(elm, field) do {                                          \  if (LIST_NEXT((elm), field) != NULL)                                         \    LIST_NEXT((elm), field)->field.le_prev =                                   \        (elm)->field.le_prev;                                                  \  *(elm)->field.le_prev = LIST_NEXT((elm), field);                             \} while (0)#endif /* !_SYS_QUEUE_H_ *//* Add a 16 bit unsigned value to a buffer pointed to by PTR */#define PPPOE_ADD_16(PTR, VAL) \    *(PTR)++ = (VAL) / 256;    \    *(PTR)++ = (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/* from if_spppsubr.c */#define IFF_PASSIVE IFF_LINK0 /* wait passively for connection */#endifstruct pppoe_softc {  LIST_ENTRY(pppoe_softc) sc_list;  struct netif *sc_ethif;      /* ethernet interface we are using */  int sc_pd;                   /* ppp unit number */  void (*sc_linkStatusCB)(int pd, int up);  int sc_state;                /* discovery phase or session connected */  struct eth_addr sc_dest;     /* hardware address of concentrator */  u16_t sc_session;            /* PPPoE session id */  char *sc_service_name;       /* if != NULL: requested name of service */  char *sc_concentrator_name;  /* if != NULL: requested concentrator id */  u8_t *sc_ac_cookie;          /* content of AC cookie we must echo back */  size_t sc_ac_cookie_len;     /* length of cookie data */#ifdef PPPOE_SERVER  u8_t *sc_hunique;            /* content of host unique we must echo back */  size_t sc_hunique_len;       /* length of host unique */#endif  int sc_padi_retried;         /* number of PADI retries already done */  int sc_padr_retried;         /* number of PADR retries already done */};/* 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 *);static LIST_HEAD(pppoe_softc_head, pppoe_softc) pppoe_softc_list;int pppoe_hdrlen;voidpppoe_init(void){  pppoe_hdrlen = sizeof(struct eth_hdr) + PPPOE_HEADERLEN;  LIST_INIT(&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 = mem_malloc(sizeof(struct pppoe_softc));  if(!sc) {    *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;  LIST_INSERT_HEAD(&pppoe_softc_list, sc, sc_list);  *scptr = sc;  return ERR_OK;}err_tpppoe_destroy(struct netif *ifp){  struct pppoe_softc * sc;  LIST_FOREACH(sc, &pppoe_softc_list, sc_list) {    if (sc->sc_ethif == ifp) {      break;    }  }  if(!(sc && (sc->sc_ethif == ifp))) {    return ERR_IF;  }  tcpip_untimeout(pppoe_timeout, sc);  LIST_REMOVE(sc, sc_list);  if (sc->sc_concentrator_name) {    mem_free(sc->sc_concentrator_name);  }  if (sc->sc_service_name) {    mem_free(sc->sc_service_name);  }  if (sc->sc_ac_cookie) {    mem_free(sc->sc_ac_cookie);  }  mem_free(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;  }  LIST_FOREACH(sc, &pppoe_softc_list, sc_list) {    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 (LIST_EMPTY(&pppoe_softc_list)) {    return NULL;  }  if (len != sizeof sc) {    return NULL;  }  MEMCPY(&t, token, len);  LIST_FOREACH(sc, &pppoe_softc_list, sc_list) {    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(void *arg){  struct pppoe_softc *sc = (struct pppoe_softc*)arg;  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];  char *error;  u8_t *ac_cookie;  size_t ac_cookie_len;#ifdef PPPOE_SERVER  u8_t *hunique;  size_t hunique_len;#endif  struct pppoehdr *ph;  struct pppoetag pt;  int off = 0, 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;

⌨️ 快捷键说明

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