Onenet

Dependents:   K64F_eCompass_OneNET_JW

Committer:
robert_jw
Date:
Mon Jun 20 01:40:20 2016 +0000
Revision:
0:b2805b6888dc
ADS

Who changed what in which revision?

UserRevisionLine numberNew contents of line
robert_jw 0:b2805b6888dc 1 /*****************************************************************************
robert_jw 0:b2805b6888dc 2 * ppp.c - Network Point to Point Protocol program file.
robert_jw 0:b2805b6888dc 3 *
robert_jw 0:b2805b6888dc 4 * Copyright (c) 2003 by Marc Boucher, Services Informatiques (MBSI) inc.
robert_jw 0:b2805b6888dc 5 * portions Copyright (c) 1997 by Global Election Systems Inc.
robert_jw 0:b2805b6888dc 6 *
robert_jw 0:b2805b6888dc 7 * The authors hereby grant permission to use, copy, modify, distribute,
robert_jw 0:b2805b6888dc 8 * and license this software and its documentation for any purpose, provided
robert_jw 0:b2805b6888dc 9 * that existing copyright notices are retained in all copies and that this
robert_jw 0:b2805b6888dc 10 * notice and the following disclaimer are included verbatim in any
robert_jw 0:b2805b6888dc 11 * distributions. No written agreement, license, or royalty fee is required
robert_jw 0:b2805b6888dc 12 * for any of the authorized uses.
robert_jw 0:b2805b6888dc 13 *
robert_jw 0:b2805b6888dc 14 * THIS SOFTWARE IS PROVIDED BY THE CONTRIBUTORS *AS IS* AND ANY EXPRESS OR
robert_jw 0:b2805b6888dc 15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
robert_jw 0:b2805b6888dc 16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
robert_jw 0:b2805b6888dc 17 * IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
robert_jw 0:b2805b6888dc 18 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
robert_jw 0:b2805b6888dc 19 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
robert_jw 0:b2805b6888dc 20 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
robert_jw 0:b2805b6888dc 21 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
robert_jw 0:b2805b6888dc 22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
robert_jw 0:b2805b6888dc 23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
robert_jw 0:b2805b6888dc 24 *
robert_jw 0:b2805b6888dc 25 ******************************************************************************
robert_jw 0:b2805b6888dc 26 * REVISION HISTORY
robert_jw 0:b2805b6888dc 27 *
robert_jw 0:b2805b6888dc 28 * 03-01-01 Marc Boucher <marc@mbsi.ca>
robert_jw 0:b2805b6888dc 29 * Ported to lwIP.
robert_jw 0:b2805b6888dc 30 * 97-11-05 Guy Lancaster <lancasterg@acm.org>, Global Election Systems Inc.
robert_jw 0:b2805b6888dc 31 * Original.
robert_jw 0:b2805b6888dc 32 *****************************************************************************/
robert_jw 0:b2805b6888dc 33
robert_jw 0:b2805b6888dc 34 /*
robert_jw 0:b2805b6888dc 35 * ppp_defs.h - PPP definitions.
robert_jw 0:b2805b6888dc 36 *
robert_jw 0:b2805b6888dc 37 * if_pppvar.h - private structures and declarations for PPP.
robert_jw 0:b2805b6888dc 38 *
robert_jw 0:b2805b6888dc 39 * Copyright (c) 1994 The Australian National University.
robert_jw 0:b2805b6888dc 40 * All rights reserved.
robert_jw 0:b2805b6888dc 41 *
robert_jw 0:b2805b6888dc 42 * Permission to use, copy, modify, and distribute this software and its
robert_jw 0:b2805b6888dc 43 * documentation is hereby granted, provided that the above copyright
robert_jw 0:b2805b6888dc 44 * notice appears in all copies. This software is provided without any
robert_jw 0:b2805b6888dc 45 * warranty, express or implied. The Australian National University
robert_jw 0:b2805b6888dc 46 * makes no representations about the suitability of this software for
robert_jw 0:b2805b6888dc 47 * any purpose.
robert_jw 0:b2805b6888dc 48 *
robert_jw 0:b2805b6888dc 49 * IN NO EVENT SHALL THE AUSTRALIAN NATIONAL UNIVERSITY BE LIABLE TO ANY
robert_jw 0:b2805b6888dc 50 * PARTY FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
robert_jw 0:b2805b6888dc 51 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
robert_jw 0:b2805b6888dc 52 * THE AUSTRALIAN NATIONAL UNIVERSITY HAVE BEEN ADVISED OF THE POSSIBILITY
robert_jw 0:b2805b6888dc 53 * OF SUCH DAMAGE.
robert_jw 0:b2805b6888dc 54 *
robert_jw 0:b2805b6888dc 55 * THE AUSTRALIAN NATIONAL UNIVERSITY SPECIFICALLY DISCLAIMS ANY WARRANTIES,
robert_jw 0:b2805b6888dc 56 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
robert_jw 0:b2805b6888dc 57 * AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
robert_jw 0:b2805b6888dc 58 * ON AN "AS IS" BASIS, AND THE AUSTRALIAN NATIONAL UNIVERSITY HAS NO
robert_jw 0:b2805b6888dc 59 * OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS,
robert_jw 0:b2805b6888dc 60 * OR MODIFICATIONS.
robert_jw 0:b2805b6888dc 61 */
robert_jw 0:b2805b6888dc 62
robert_jw 0:b2805b6888dc 63 /*
robert_jw 0:b2805b6888dc 64 * if_ppp.h - Point-to-Point Protocol definitions.
robert_jw 0:b2805b6888dc 65 *
robert_jw 0:b2805b6888dc 66 * Copyright (c) 1989 Carnegie Mellon University.
robert_jw 0:b2805b6888dc 67 * All rights reserved.
robert_jw 0:b2805b6888dc 68 *
robert_jw 0:b2805b6888dc 69 * Redistribution and use in source and binary forms are permitted
robert_jw 0:b2805b6888dc 70 * provided that the above copyright notice and this paragraph are
robert_jw 0:b2805b6888dc 71 * duplicated in all such forms and that any documentation,
robert_jw 0:b2805b6888dc 72 * advertising materials, and other materials related to such
robert_jw 0:b2805b6888dc 73 * distribution and use acknowledge that the software was developed
robert_jw 0:b2805b6888dc 74 * by Carnegie Mellon University. The name of the
robert_jw 0:b2805b6888dc 75 * University may not be used to endorse or promote products derived
robert_jw 0:b2805b6888dc 76 * from this software without specific prior written permission.
robert_jw 0:b2805b6888dc 77 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
robert_jw 0:b2805b6888dc 78 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
robert_jw 0:b2805b6888dc 79 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
robert_jw 0:b2805b6888dc 80 */
robert_jw 0:b2805b6888dc 81
robert_jw 0:b2805b6888dc 82 #include "lwip/opt.h"
robert_jw 0:b2805b6888dc 83
robert_jw 0:b2805b6888dc 84 #if PPP_SUPPORT /* don't build if not configured for use in lwipopts.h */
robert_jw 0:b2805b6888dc 85
robert_jw 0:b2805b6888dc 86 #include "lwip/ip.h" /* for ip_input() */
robert_jw 0:b2805b6888dc 87
robert_jw 0:b2805b6888dc 88 #include "ppp.h"
robert_jw 0:b2805b6888dc 89 #include "pppdebug.h"
robert_jw 0:b2805b6888dc 90
robert_jw 0:b2805b6888dc 91 #include "randm.h"
robert_jw 0:b2805b6888dc 92 #include "fsm.h"
robert_jw 0:b2805b6888dc 93 #if PAP_SUPPORT
robert_jw 0:b2805b6888dc 94 #include "pap.h"
robert_jw 0:b2805b6888dc 95 #endif /* PAP_SUPPORT */
robert_jw 0:b2805b6888dc 96 #if CHAP_SUPPORT
robert_jw 0:b2805b6888dc 97 #include "chap.h"
robert_jw 0:b2805b6888dc 98 #endif /* CHAP_SUPPORT */
robert_jw 0:b2805b6888dc 99 #include "ipcp.h"
robert_jw 0:b2805b6888dc 100 #include "lcp.h"
robert_jw 0:b2805b6888dc 101 #include "magic.h"
robert_jw 0:b2805b6888dc 102 #include "auth.h"
robert_jw 0:b2805b6888dc 103 #if VJ_SUPPORT
robert_jw 0:b2805b6888dc 104 #include "vj.h"
robert_jw 0:b2805b6888dc 105 #endif /* VJ_SUPPORT */
robert_jw 0:b2805b6888dc 106 #if PPPOE_SUPPORT
robert_jw 0:b2805b6888dc 107 #include "netif/ppp_oe.h"
robert_jw 0:b2805b6888dc 108 #endif /* PPPOE_SUPPORT */
robert_jw 0:b2805b6888dc 109
robert_jw 0:b2805b6888dc 110 #include "lwip/tcpip.h"
robert_jw 0:b2805b6888dc 111 #include "lwip/api.h"
robert_jw 0:b2805b6888dc 112 #include "lwip/snmp.h"
robert_jw 0:b2805b6888dc 113
robert_jw 0:b2805b6888dc 114 #include <string.h>
robert_jw 0:b2805b6888dc 115
robert_jw 0:b2805b6888dc 116 /*************************/
robert_jw 0:b2805b6888dc 117 /*** LOCAL DEFINITIONS ***/
robert_jw 0:b2805b6888dc 118 /*************************/
robert_jw 0:b2805b6888dc 119
robert_jw 0:b2805b6888dc 120 /** PPP_INPROC_MULTITHREADED==1 call pppInput using tcpip_callback().
robert_jw 0:b2805b6888dc 121 * Set this to 0 if pppInProc is called inside tcpip_thread or with NO_SYS==1.
robert_jw 0:b2805b6888dc 122 * Default is 1 for NO_SYS==0 (multithreaded) and 0 for NO_SYS==1 (single-threaded).
robert_jw 0:b2805b6888dc 123 */
robert_jw 0:b2805b6888dc 124 #ifndef PPP_INPROC_MULTITHREADED
robert_jw 0:b2805b6888dc 125 #define PPP_INPROC_MULTITHREADED (NO_SYS==0)
robert_jw 0:b2805b6888dc 126 #endif
robert_jw 0:b2805b6888dc 127
robert_jw 0:b2805b6888dc 128 /** PPP_INPROC_OWNTHREAD==1: start a dedicated RX thread per PPP session.
robert_jw 0:b2805b6888dc 129 * Default is 0: call pppos_input() for received raw characters, charcater
robert_jw 0:b2805b6888dc 130 * reception is up to the port */
robert_jw 0:b2805b6888dc 131 #ifndef PPP_INPROC_OWNTHREAD
robert_jw 0:b2805b6888dc 132 #define PPP_INPROC_OWNTHREAD PPP_INPROC_MULTITHREADED
robert_jw 0:b2805b6888dc 133 #endif
robert_jw 0:b2805b6888dc 134
robert_jw 0:b2805b6888dc 135 #if PPP_INPROC_OWNTHREAD && !PPP_INPROC_MULTITHREADED
robert_jw 0:b2805b6888dc 136 #error "PPP_INPROC_OWNTHREAD needs PPP_INPROC_MULTITHREADED==1"
robert_jw 0:b2805b6888dc 137 #endif
robert_jw 0:b2805b6888dc 138
robert_jw 0:b2805b6888dc 139 /*
robert_jw 0:b2805b6888dc 140 * The basic PPP frame.
robert_jw 0:b2805b6888dc 141 */
robert_jw 0:b2805b6888dc 142 #define PPP_ADDRESS(p) (((u_char *)(p))[0])
robert_jw 0:b2805b6888dc 143 #define PPP_CONTROL(p) (((u_char *)(p))[1])
robert_jw 0:b2805b6888dc 144 #define PPP_PROTOCOL(p) ((((u_char *)(p))[2] << 8) + ((u_char *)(p))[3])
robert_jw 0:b2805b6888dc 145
robert_jw 0:b2805b6888dc 146 /* PPP packet parser states. Current state indicates operation yet to be
robert_jw 0:b2805b6888dc 147 * completed. */
robert_jw 0:b2805b6888dc 148 typedef enum {
robert_jw 0:b2805b6888dc 149 PDIDLE = 0, /* Idle state - waiting. */
robert_jw 0:b2805b6888dc 150 PDSTART, /* Process start flag. */
robert_jw 0:b2805b6888dc 151 PDADDRESS, /* Process address field. */
robert_jw 0:b2805b6888dc 152 PDCONTROL, /* Process control field. */
robert_jw 0:b2805b6888dc 153 PDPROTOCOL1, /* Process protocol field 1. */
robert_jw 0:b2805b6888dc 154 PDPROTOCOL2, /* Process protocol field 2. */
robert_jw 0:b2805b6888dc 155 PDDATA /* Process data byte. */
robert_jw 0:b2805b6888dc 156 } PPPDevStates;
robert_jw 0:b2805b6888dc 157
robert_jw 0:b2805b6888dc 158 #define ESCAPE_P(accm, c) ((accm)[(c) >> 3] & pppACCMMask[c & 0x07])
robert_jw 0:b2805b6888dc 159
robert_jw 0:b2805b6888dc 160 /************************/
robert_jw 0:b2805b6888dc 161 /*** LOCAL DATA TYPES ***/
robert_jw 0:b2805b6888dc 162 /************************/
robert_jw 0:b2805b6888dc 163
robert_jw 0:b2805b6888dc 164 /** RX buffer size: this may be configured smaller! */
robert_jw 0:b2805b6888dc 165 #ifndef PPPOS_RX_BUFSIZE
robert_jw 0:b2805b6888dc 166 #define PPPOS_RX_BUFSIZE (PPP_MRU + PPP_HDRLEN)
robert_jw 0:b2805b6888dc 167 #endif
robert_jw 0:b2805b6888dc 168
robert_jw 0:b2805b6888dc 169 typedef struct PPPControlRx_s {
robert_jw 0:b2805b6888dc 170 /** unit number / ppp descriptor */
robert_jw 0:b2805b6888dc 171 int pd;
robert_jw 0:b2805b6888dc 172 /** the rx file descriptor */
robert_jw 0:b2805b6888dc 173 sio_fd_t fd;
robert_jw 0:b2805b6888dc 174 /** receive buffer - encoded data is stored here */
robert_jw 0:b2805b6888dc 175 u_char rxbuf[PPPOS_RX_BUFSIZE];
robert_jw 0:b2805b6888dc 176
robert_jw 0:b2805b6888dc 177 /* The input packet. */
robert_jw 0:b2805b6888dc 178 struct pbuf *inHead, *inTail;
robert_jw 0:b2805b6888dc 179
robert_jw 0:b2805b6888dc 180 #if PPPOS_SUPPORT
robert_jw 0:b2805b6888dc 181 u16_t inProtocol; /* The input protocol code. */
robert_jw 0:b2805b6888dc 182 u16_t inFCS; /* Input Frame Check Sequence value. */
robert_jw 0:b2805b6888dc 183 #endif /* PPPOS_SUPPORT */
robert_jw 0:b2805b6888dc 184 PPPDevStates inState; /* The input process state. */
robert_jw 0:b2805b6888dc 185 char inEscaped; /* Escape next character. */
robert_jw 0:b2805b6888dc 186 ext_accm inACCM; /* Async-Ctl-Char-Map for input. */
robert_jw 0:b2805b6888dc 187 } PPPControlRx;
robert_jw 0:b2805b6888dc 188
robert_jw 0:b2805b6888dc 189 /*
robert_jw 0:b2805b6888dc 190 * PPP interface control block.
robert_jw 0:b2805b6888dc 191 */
robert_jw 0:b2805b6888dc 192 typedef struct PPPControl_s {
robert_jw 0:b2805b6888dc 193 PPPControlRx rx;
robert_jw 0:b2805b6888dc 194 char openFlag; /* True when in use. */
robert_jw 0:b2805b6888dc 195 #if PPPOE_SUPPORT
robert_jw 0:b2805b6888dc 196 struct netif *ethif;
robert_jw 0:b2805b6888dc 197 struct pppoe_softc *pppoe_sc;
robert_jw 0:b2805b6888dc 198 #endif /* PPPOE_SUPPORT */
robert_jw 0:b2805b6888dc 199 int if_up; /* True when the interface is up. */
robert_jw 0:b2805b6888dc 200 int errCode; /* Code indicating why interface is down. */
robert_jw 0:b2805b6888dc 201 #if PPPOS_SUPPORT
robert_jw 0:b2805b6888dc 202 sio_fd_t fd; /* File device ID of port. */
robert_jw 0:b2805b6888dc 203 #endif /* PPPOS_SUPPORT */
robert_jw 0:b2805b6888dc 204 u16_t mtu; /* Peer's mru */
robert_jw 0:b2805b6888dc 205 int pcomp; /* Does peer accept protocol compression? */
robert_jw 0:b2805b6888dc 206 int accomp; /* Does peer accept addr/ctl compression? */
robert_jw 0:b2805b6888dc 207 u_long lastXMit; /* Time of last transmission. */
robert_jw 0:b2805b6888dc 208 ext_accm outACCM; /* Async-Ctl-Char-Map for output. */
robert_jw 0:b2805b6888dc 209 #if PPPOS_SUPPORT && VJ_SUPPORT
robert_jw 0:b2805b6888dc 210 int vjEnabled; /* Flag indicating VJ compression enabled. */
robert_jw 0:b2805b6888dc 211 struct vjcompress vjComp; /* Van Jacobson compression header. */
robert_jw 0:b2805b6888dc 212 #endif /* PPPOS_SUPPORT && VJ_SUPPORT */
robert_jw 0:b2805b6888dc 213
robert_jw 0:b2805b6888dc 214 struct netif netif;
robert_jw 0:b2805b6888dc 215
robert_jw 0:b2805b6888dc 216 struct ppp_addrs addrs;
robert_jw 0:b2805b6888dc 217
robert_jw 0:b2805b6888dc 218 void (*linkStatusCB)(void *ctx, int errCode, void *arg);
robert_jw 0:b2805b6888dc 219 void *linkStatusCtx;
robert_jw 0:b2805b6888dc 220
robert_jw 0:b2805b6888dc 221 } PPPControl;
robert_jw 0:b2805b6888dc 222
robert_jw 0:b2805b6888dc 223
robert_jw 0:b2805b6888dc 224 /*
robert_jw 0:b2805b6888dc 225 * Ioctl definitions.
robert_jw 0:b2805b6888dc 226 */
robert_jw 0:b2805b6888dc 227
robert_jw 0:b2805b6888dc 228 struct npioctl {
robert_jw 0:b2805b6888dc 229 int protocol; /* PPP procotol, e.g. PPP_IP */
robert_jw 0:b2805b6888dc 230 enum NPmode mode;
robert_jw 0:b2805b6888dc 231 };
robert_jw 0:b2805b6888dc 232
robert_jw 0:b2805b6888dc 233
robert_jw 0:b2805b6888dc 234
robert_jw 0:b2805b6888dc 235 /***********************************/
robert_jw 0:b2805b6888dc 236 /*** LOCAL FUNCTION DECLARATIONS ***/
robert_jw 0:b2805b6888dc 237 /***********************************/
robert_jw 0:b2805b6888dc 238 #if PPPOS_SUPPORT
robert_jw 0:b2805b6888dc 239 #if PPP_INPROC_OWNTHREAD
robert_jw 0:b2805b6888dc 240 static void pppInputThread(void *arg);
robert_jw 0:b2805b6888dc 241 #endif /* PPP_INPROC_OWNTHREAD */
robert_jw 0:b2805b6888dc 242 static void pppDrop(PPPControlRx *pcrx);
robert_jw 0:b2805b6888dc 243 static void pppInProc(PPPControlRx *pcrx, u_char *s, int l);
robert_jw 0:b2805b6888dc 244 #endif /* PPPOS_SUPPORT */
robert_jw 0:b2805b6888dc 245
robert_jw 0:b2805b6888dc 246
robert_jw 0:b2805b6888dc 247 /******************************/
robert_jw 0:b2805b6888dc 248 /*** PUBLIC DATA STRUCTURES ***/
robert_jw 0:b2805b6888dc 249 /******************************/
robert_jw 0:b2805b6888dc 250 u_long subnetMask;
robert_jw 0:b2805b6888dc 251
robert_jw 0:b2805b6888dc 252 static PPPControl pppControl[NUM_PPP] __attribute((section("AHBSRAM1"))); /* The PPP interface control blocks. */
robert_jw 0:b2805b6888dc 253
robert_jw 0:b2805b6888dc 254 sys_mbox_t pppMbox; //Used to signal PPP thread that a PPP session begins
robert_jw 0:b2805b6888dc 255
robert_jw 0:b2805b6888dc 256 /*
robert_jw 0:b2805b6888dc 257 * PPP Data Link Layer "protocol" table.
robert_jw 0:b2805b6888dc 258 * One entry per supported protocol.
robert_jw 0:b2805b6888dc 259 * The last entry must be NULL.
robert_jw 0:b2805b6888dc 260 */
robert_jw 0:b2805b6888dc 261 struct protent *ppp_protocols[] = {
robert_jw 0:b2805b6888dc 262 &lcp_protent,
robert_jw 0:b2805b6888dc 263 #if PAP_SUPPORT
robert_jw 0:b2805b6888dc 264 &pap_protent,
robert_jw 0:b2805b6888dc 265 #endif /* PAP_SUPPORT */
robert_jw 0:b2805b6888dc 266 #if CHAP_SUPPORT
robert_jw 0:b2805b6888dc 267 &chap_protent,
robert_jw 0:b2805b6888dc 268 #endif /* CHAP_SUPPORT */
robert_jw 0:b2805b6888dc 269 #if CBCP_SUPPORT
robert_jw 0:b2805b6888dc 270 &cbcp_protent,
robert_jw 0:b2805b6888dc 271 #endif /* CBCP_SUPPORT */
robert_jw 0:b2805b6888dc 272 &ipcp_protent,
robert_jw 0:b2805b6888dc 273 #if CCP_SUPPORT
robert_jw 0:b2805b6888dc 274 &ccp_protent,
robert_jw 0:b2805b6888dc 275 #endif /* CCP_SUPPORT */
robert_jw 0:b2805b6888dc 276 NULL
robert_jw 0:b2805b6888dc 277 };
robert_jw 0:b2805b6888dc 278
robert_jw 0:b2805b6888dc 279
robert_jw 0:b2805b6888dc 280 /*
robert_jw 0:b2805b6888dc 281 * Buffers for outgoing packets. This must be accessed only from the appropriate
robert_jw 0:b2805b6888dc 282 * PPP task so that it doesn't need to be protected to avoid collisions.
robert_jw 0:b2805b6888dc 283 */
robert_jw 0:b2805b6888dc 284 u_char outpacket_buf[NUM_PPP][PPP_MRU+PPP_HDRLEN] __attribute((section("AHBSRAM1")));
robert_jw 0:b2805b6888dc 285
robert_jw 0:b2805b6888dc 286
robert_jw 0:b2805b6888dc 287 /*****************************/
robert_jw 0:b2805b6888dc 288 /*** LOCAL DATA STRUCTURES ***/
robert_jw 0:b2805b6888dc 289 /*****************************/
robert_jw 0:b2805b6888dc 290
robert_jw 0:b2805b6888dc 291 #if PPPOS_SUPPORT
robert_jw 0:b2805b6888dc 292 /*
robert_jw 0:b2805b6888dc 293 * FCS lookup table as calculated by genfcstab.
robert_jw 0:b2805b6888dc 294 * @todo: smaller, slower implementation for lower memory footprint?
robert_jw 0:b2805b6888dc 295 */
robert_jw 0:b2805b6888dc 296 static const u_short fcstab[256] = {
robert_jw 0:b2805b6888dc 297 0x0000, 0x1189, 0x2312, 0x329b, 0x4624, 0x57ad, 0x6536, 0x74bf,
robert_jw 0:b2805b6888dc 298 0x8c48, 0x9dc1, 0xaf5a, 0xbed3, 0xca6c, 0xdbe5, 0xe97e, 0xf8f7,
robert_jw 0:b2805b6888dc 299 0x1081, 0x0108, 0x3393, 0x221a, 0x56a5, 0x472c, 0x75b7, 0x643e,
robert_jw 0:b2805b6888dc 300 0x9cc9, 0x8d40, 0xbfdb, 0xae52, 0xdaed, 0xcb64, 0xf9ff, 0xe876,
robert_jw 0:b2805b6888dc 301 0x2102, 0x308b, 0x0210, 0x1399, 0x6726, 0x76af, 0x4434, 0x55bd,
robert_jw 0:b2805b6888dc 302 0xad4a, 0xbcc3, 0x8e58, 0x9fd1, 0xeb6e, 0xfae7, 0xc87c, 0xd9f5,
robert_jw 0:b2805b6888dc 303 0x3183, 0x200a, 0x1291, 0x0318, 0x77a7, 0x662e, 0x54b5, 0x453c,
robert_jw 0:b2805b6888dc 304 0xbdcb, 0xac42, 0x9ed9, 0x8f50, 0xfbef, 0xea66, 0xd8fd, 0xc974,
robert_jw 0:b2805b6888dc 305 0x4204, 0x538d, 0x6116, 0x709f, 0x0420, 0x15a9, 0x2732, 0x36bb,
robert_jw 0:b2805b6888dc 306 0xce4c, 0xdfc5, 0xed5e, 0xfcd7, 0x8868, 0x99e1, 0xab7a, 0xbaf3,
robert_jw 0:b2805b6888dc 307 0x5285, 0x430c, 0x7197, 0x601e, 0x14a1, 0x0528, 0x37b3, 0x263a,
robert_jw 0:b2805b6888dc 308 0xdecd, 0xcf44, 0xfddf, 0xec56, 0x98e9, 0x8960, 0xbbfb, 0xaa72,
robert_jw 0:b2805b6888dc 309 0x6306, 0x728f, 0x4014, 0x519d, 0x2522, 0x34ab, 0x0630, 0x17b9,
robert_jw 0:b2805b6888dc 310 0xef4e, 0xfec7, 0xcc5c, 0xddd5, 0xa96a, 0xb8e3, 0x8a78, 0x9bf1,
robert_jw 0:b2805b6888dc 311 0x7387, 0x620e, 0x5095, 0x411c, 0x35a3, 0x242a, 0x16b1, 0x0738,
robert_jw 0:b2805b6888dc 312 0xffcf, 0xee46, 0xdcdd, 0xcd54, 0xb9eb, 0xa862, 0x9af9, 0x8b70,
robert_jw 0:b2805b6888dc 313 0x8408, 0x9581, 0xa71a, 0xb693, 0xc22c, 0xd3a5, 0xe13e, 0xf0b7,
robert_jw 0:b2805b6888dc 314 0x0840, 0x19c9, 0x2b52, 0x3adb, 0x4e64, 0x5fed, 0x6d76, 0x7cff,
robert_jw 0:b2805b6888dc 315 0x9489, 0x8500, 0xb79b, 0xa612, 0xd2ad, 0xc324, 0xf1bf, 0xe036,
robert_jw 0:b2805b6888dc 316 0x18c1, 0x0948, 0x3bd3, 0x2a5a, 0x5ee5, 0x4f6c, 0x7df7, 0x6c7e,
robert_jw 0:b2805b6888dc 317 0xa50a, 0xb483, 0x8618, 0x9791, 0xe32e, 0xf2a7, 0xc03c, 0xd1b5,
robert_jw 0:b2805b6888dc 318 0x2942, 0x38cb, 0x0a50, 0x1bd9, 0x6f66, 0x7eef, 0x4c74, 0x5dfd,
robert_jw 0:b2805b6888dc 319 0xb58b, 0xa402, 0x9699, 0x8710, 0xf3af, 0xe226, 0xd0bd, 0xc134,
robert_jw 0:b2805b6888dc 320 0x39c3, 0x284a, 0x1ad1, 0x0b58, 0x7fe7, 0x6e6e, 0x5cf5, 0x4d7c,
robert_jw 0:b2805b6888dc 321 0xc60c, 0xd785, 0xe51e, 0xf497, 0x8028, 0x91a1, 0xa33a, 0xb2b3,
robert_jw 0:b2805b6888dc 322 0x4a44, 0x5bcd, 0x6956, 0x78df, 0x0c60, 0x1de9, 0x2f72, 0x3efb,
robert_jw 0:b2805b6888dc 323 0xd68d, 0xc704, 0xf59f, 0xe416, 0x90a9, 0x8120, 0xb3bb, 0xa232,
robert_jw 0:b2805b6888dc 324 0x5ac5, 0x4b4c, 0x79d7, 0x685e, 0x1ce1, 0x0d68, 0x3ff3, 0x2e7a,
robert_jw 0:b2805b6888dc 325 0xe70e, 0xf687, 0xc41c, 0xd595, 0xa12a, 0xb0a3, 0x8238, 0x93b1,
robert_jw 0:b2805b6888dc 326 0x6b46, 0x7acf, 0x4854, 0x59dd, 0x2d62, 0x3ceb, 0x0e70, 0x1ff9,
robert_jw 0:b2805b6888dc 327 0xf78f, 0xe606, 0xd49d, 0xc514, 0xb1ab, 0xa022, 0x92b9, 0x8330,
robert_jw 0:b2805b6888dc 328 0x7bc7, 0x6a4e, 0x58d5, 0x495c, 0x3de3, 0x2c6a, 0x1ef1, 0x0f78
robert_jw 0:b2805b6888dc 329 };
robert_jw 0:b2805b6888dc 330
robert_jw 0:b2805b6888dc 331 /* PPP's Asynchronous-Control-Character-Map. The mask array is used
robert_jw 0:b2805b6888dc 332 * to select the specific bit for a character. */
robert_jw 0:b2805b6888dc 333 static u_char pppACCMMask[] = {
robert_jw 0:b2805b6888dc 334 0x01,
robert_jw 0:b2805b6888dc 335 0x02,
robert_jw 0:b2805b6888dc 336 0x04,
robert_jw 0:b2805b6888dc 337 0x08,
robert_jw 0:b2805b6888dc 338 0x10,
robert_jw 0:b2805b6888dc 339 0x20,
robert_jw 0:b2805b6888dc 340 0x40,
robert_jw 0:b2805b6888dc 341 0x80
robert_jw 0:b2805b6888dc 342 };
robert_jw 0:b2805b6888dc 343
robert_jw 0:b2805b6888dc 344 /** Wake up the task blocked in reading from serial line (if any) */
robert_jw 0:b2805b6888dc 345 static void
robert_jw 0:b2805b6888dc 346 pppRecvWakeup(int pd)
robert_jw 0:b2805b6888dc 347 {
robert_jw 0:b2805b6888dc 348 PPPDEBUG(LOG_DEBUG, ("pppRecvWakeup: unit %d\n", pd));
robert_jw 0:b2805b6888dc 349 if (pppControl[pd].openFlag != 0) {
robert_jw 0:b2805b6888dc 350 sio_read_abort(pppControl[pd].fd);
robert_jw 0:b2805b6888dc 351 }
robert_jw 0:b2805b6888dc 352 }
robert_jw 0:b2805b6888dc 353 #endif /* PPPOS_SUPPORT */
robert_jw 0:b2805b6888dc 354
robert_jw 0:b2805b6888dc 355 void
robert_jw 0:b2805b6888dc 356 pppLinkTerminated(int pd)
robert_jw 0:b2805b6888dc 357 {
robert_jw 0:b2805b6888dc 358 PPPDEBUG(LOG_DEBUG, ("pppLinkTerminated: unit %d\n", pd));
robert_jw 0:b2805b6888dc 359
robert_jw 0:b2805b6888dc 360 #if PPPOE_SUPPORT
robert_jw 0:b2805b6888dc 361 if (pppControl[pd].ethif) {
robert_jw 0:b2805b6888dc 362 pppoe_disconnect(pppControl[pd].pppoe_sc);
robert_jw 0:b2805b6888dc 363 } else
robert_jw 0:b2805b6888dc 364 #endif /* PPPOE_SUPPORT */
robert_jw 0:b2805b6888dc 365 {
robert_jw 0:b2805b6888dc 366 #if PPPOS_SUPPORT
robert_jw 0:b2805b6888dc 367 PPPControl* pc;
robert_jw 0:b2805b6888dc 368 pppRecvWakeup(pd);
robert_jw 0:b2805b6888dc 369 pc = &pppControl[pd];
robert_jw 0:b2805b6888dc 370
robert_jw 0:b2805b6888dc 371 PPPDEBUG(LOG_DEBUG, ("pppLinkTerminated: unit %d: linkStatusCB=%p errCode=%d\n", pd, pc->linkStatusCB, pc->errCode));
robert_jw 0:b2805b6888dc 372 if (pc->linkStatusCB) {
robert_jw 0:b2805b6888dc 373 pc->linkStatusCB(pc->linkStatusCtx, pc->errCode ? pc->errCode : PPPERR_PROTOCOL, NULL);
robert_jw 0:b2805b6888dc 374 }
robert_jw 0:b2805b6888dc 375
robert_jw 0:b2805b6888dc 376 pc->openFlag = 0;/**/
robert_jw 0:b2805b6888dc 377 #endif /* PPPOS_SUPPORT */
robert_jw 0:b2805b6888dc 378 }
robert_jw 0:b2805b6888dc 379 PPPDEBUG(LOG_DEBUG, ("pppLinkTerminated: finished.\n"));
robert_jw 0:b2805b6888dc 380 }
robert_jw 0:b2805b6888dc 381
robert_jw 0:b2805b6888dc 382 void
robert_jw 0:b2805b6888dc 383 pppLinkDown(int pd)
robert_jw 0:b2805b6888dc 384 {
robert_jw 0:b2805b6888dc 385 PPPDEBUG(LOG_DEBUG, ("pppLinkDown: unit %d\n", pd));
robert_jw 0:b2805b6888dc 386
robert_jw 0:b2805b6888dc 387 #if PPPOE_SUPPORT
robert_jw 0:b2805b6888dc 388 if (pppControl[pd].ethif) {
robert_jw 0:b2805b6888dc 389 pppoe_disconnect(pppControl[pd].pppoe_sc);
robert_jw 0:b2805b6888dc 390 } else
robert_jw 0:b2805b6888dc 391 #endif /* PPPOE_SUPPORT */
robert_jw 0:b2805b6888dc 392 {
robert_jw 0:b2805b6888dc 393 #if PPPOS_SUPPORT
robert_jw 0:b2805b6888dc 394 pppRecvWakeup(pd);
robert_jw 0:b2805b6888dc 395 #endif /* PPPOS_SUPPORT */
robert_jw 0:b2805b6888dc 396 }
robert_jw 0:b2805b6888dc 397 }
robert_jw 0:b2805b6888dc 398
robert_jw 0:b2805b6888dc 399 /** Initiate LCP open request */
robert_jw 0:b2805b6888dc 400 static void
robert_jw 0:b2805b6888dc 401 pppStart(int pd)
robert_jw 0:b2805b6888dc 402 {
robert_jw 0:b2805b6888dc 403 PPPDEBUG(LOG_DEBUG, ("pppStart: unit %d\n", pd));
robert_jw 0:b2805b6888dc 404 lcp_lowerup(pd);
robert_jw 0:b2805b6888dc 405 lcp_open(pd); /* Start protocol */
robert_jw 0:b2805b6888dc 406 PPPDEBUG(LOG_DEBUG, ("pppStart: finished\n"));
robert_jw 0:b2805b6888dc 407 }
robert_jw 0:b2805b6888dc 408
robert_jw 0:b2805b6888dc 409 /** LCP close request */
robert_jw 0:b2805b6888dc 410 static void
robert_jw 0:b2805b6888dc 411 pppStop(int pd)
robert_jw 0:b2805b6888dc 412 {
robert_jw 0:b2805b6888dc 413 PPPDEBUG(LOG_DEBUG, ("pppStop: unit %d\n", pd));
robert_jw 0:b2805b6888dc 414 lcp_close(pd, "User request");
robert_jw 0:b2805b6888dc 415 }
robert_jw 0:b2805b6888dc 416
robert_jw 0:b2805b6888dc 417 /** Called when carrier/link is lost */
robert_jw 0:b2805b6888dc 418 static void
robert_jw 0:b2805b6888dc 419 pppHup(int pd)
robert_jw 0:b2805b6888dc 420 {
robert_jw 0:b2805b6888dc 421 PPPDEBUG(LOG_DEBUG, ("pppHupCB: unit %d\n", pd));
robert_jw 0:b2805b6888dc 422 lcp_lowerdown(pd);
robert_jw 0:b2805b6888dc 423 link_terminated(pd);
robert_jw 0:b2805b6888dc 424 }
robert_jw 0:b2805b6888dc 425
robert_jw 0:b2805b6888dc 426 /***********************************/
robert_jw 0:b2805b6888dc 427 /*** PUBLIC FUNCTION DEFINITIONS ***/
robert_jw 0:b2805b6888dc 428 /***********************************/
robert_jw 0:b2805b6888dc 429 /* Initialize the PPP subsystem. */
robert_jw 0:b2805b6888dc 430
robert_jw 0:b2805b6888dc 431 struct ppp_settings ppp_settings;
robert_jw 0:b2805b6888dc 432
robert_jw 0:b2805b6888dc 433 void
robert_jw 0:b2805b6888dc 434 pppInit(void)
robert_jw 0:b2805b6888dc 435 {
robert_jw 0:b2805b6888dc 436 struct protent *protp;
robert_jw 0:b2805b6888dc 437 int i, j;
robert_jw 0:b2805b6888dc 438
robert_jw 0:b2805b6888dc 439 memset(&ppp_settings, 0, sizeof(ppp_settings));
robert_jw 0:b2805b6888dc 440 ppp_settings.usepeerdns = 1;
robert_jw 0:b2805b6888dc 441 pppSetAuth(PPPAUTHTYPE_NONE, NULL, NULL);
robert_jw 0:b2805b6888dc 442
robert_jw 0:b2805b6888dc 443 magicInit();
robert_jw 0:b2805b6888dc 444
robert_jw 0:b2805b6888dc 445 subnetMask = PP_HTONL(0xffffff00UL);
robert_jw 0:b2805b6888dc 446
robert_jw 0:b2805b6888dc 447 for (i = 0; i < NUM_PPP; i++) {
robert_jw 0:b2805b6888dc 448 /* Initialize each protocol to the standard option set. */
robert_jw 0:b2805b6888dc 449 for (j = 0; (protp = ppp_protocols[j]) != NULL; ++j) {
robert_jw 0:b2805b6888dc 450 (*protp->init)(i);
robert_jw 0:b2805b6888dc 451 }
robert_jw 0:b2805b6888dc 452 }
robert_jw 0:b2805b6888dc 453
robert_jw 0:b2805b6888dc 454 sys_mbox_new(&pppMbox, 1);
robert_jw 0:b2805b6888dc 455 sys_thread_new(PPP_THREAD_NAME, pppInputThread, (void*)NULL, PPP_THREAD_STACKSIZE, PPP_THREAD_PRIO); //Create PPP thread here
robert_jw 0:b2805b6888dc 456 }
robert_jw 0:b2805b6888dc 457
robert_jw 0:b2805b6888dc 458 void
robert_jw 0:b2805b6888dc 459 pppSetAuth(enum pppAuthType authType, const char *user, const char *passwd)
robert_jw 0:b2805b6888dc 460 {
robert_jw 0:b2805b6888dc 461 switch(authType) {
robert_jw 0:b2805b6888dc 462 case PPPAUTHTYPE_NONE:
robert_jw 0:b2805b6888dc 463 default:
robert_jw 0:b2805b6888dc 464 #ifdef LWIP_PPP_STRICT_PAP_REJECT
robert_jw 0:b2805b6888dc 465 ppp_settings.refuse_pap = 1;
robert_jw 0:b2805b6888dc 466 #else /* LWIP_PPP_STRICT_PAP_REJECT */
robert_jw 0:b2805b6888dc 467 /* some providers request pap and accept an empty login/pw */
robert_jw 0:b2805b6888dc 468 ppp_settings.refuse_pap = 0;
robert_jw 0:b2805b6888dc 469 #endif /* LWIP_PPP_STRICT_PAP_REJECT */
robert_jw 0:b2805b6888dc 470 ppp_settings.refuse_chap = 1;
robert_jw 0:b2805b6888dc 471 break;
robert_jw 0:b2805b6888dc 472
robert_jw 0:b2805b6888dc 473 case PPPAUTHTYPE_ANY:
robert_jw 0:b2805b6888dc 474 /* Warning: Using PPPAUTHTYPE_ANY might have security consequences.
robert_jw 0:b2805b6888dc 475 * RFC 1994 says:
robert_jw 0:b2805b6888dc 476 *
robert_jw 0:b2805b6888dc 477 * In practice, within or associated with each PPP server, there is a
robert_jw 0:b2805b6888dc 478 * database which associates "user" names with authentication
robert_jw 0:b2805b6888dc 479 * information ("secrets"). It is not anticipated that a particular
robert_jw 0:b2805b6888dc 480 * named user would be authenticated by multiple methods. This would
robert_jw 0:b2805b6888dc 481 * make the user vulnerable to attacks which negotiate the least secure
robert_jw 0:b2805b6888dc 482 * method from among a set (such as PAP rather than CHAP). If the same
robert_jw 0:b2805b6888dc 483 * secret was used, PAP would reveal the secret to be used later with
robert_jw 0:b2805b6888dc 484 * CHAP.
robert_jw 0:b2805b6888dc 485 *
robert_jw 0:b2805b6888dc 486 * Instead, for each user name there should be an indication of exactly
robert_jw 0:b2805b6888dc 487 * one method used to authenticate that user name. If a user needs to
robert_jw 0:b2805b6888dc 488 * make use of different authentication methods under different
robert_jw 0:b2805b6888dc 489 * circumstances, then distinct user names SHOULD be employed, each of
robert_jw 0:b2805b6888dc 490 * which identifies exactly one authentication method.
robert_jw 0:b2805b6888dc 491 *
robert_jw 0:b2805b6888dc 492 */
robert_jw 0:b2805b6888dc 493 ppp_settings.refuse_pap = 0;
robert_jw 0:b2805b6888dc 494 ppp_settings.refuse_chap = 0;
robert_jw 0:b2805b6888dc 495 break;
robert_jw 0:b2805b6888dc 496
robert_jw 0:b2805b6888dc 497 case PPPAUTHTYPE_PAP:
robert_jw 0:b2805b6888dc 498 ppp_settings.refuse_pap = 0;
robert_jw 0:b2805b6888dc 499 ppp_settings.refuse_chap = 1;
robert_jw 0:b2805b6888dc 500 break;
robert_jw 0:b2805b6888dc 501
robert_jw 0:b2805b6888dc 502 case PPPAUTHTYPE_CHAP:
robert_jw 0:b2805b6888dc 503 ppp_settings.refuse_pap = 1;
robert_jw 0:b2805b6888dc 504 ppp_settings.refuse_chap = 0;
robert_jw 0:b2805b6888dc 505 break;
robert_jw 0:b2805b6888dc 506 }
robert_jw 0:b2805b6888dc 507
robert_jw 0:b2805b6888dc 508 if(user) {
robert_jw 0:b2805b6888dc 509 strncpy(ppp_settings.user, user, sizeof(ppp_settings.user)-1);
robert_jw 0:b2805b6888dc 510 ppp_settings.user[sizeof(ppp_settings.user)-1] = '\0';
robert_jw 0:b2805b6888dc 511 } else {
robert_jw 0:b2805b6888dc 512 ppp_settings.user[0] = '\0';
robert_jw 0:b2805b6888dc 513 }
robert_jw 0:b2805b6888dc 514
robert_jw 0:b2805b6888dc 515 if(passwd) {
robert_jw 0:b2805b6888dc 516 strncpy(ppp_settings.passwd, passwd, sizeof(ppp_settings.passwd)-1);
robert_jw 0:b2805b6888dc 517 ppp_settings.passwd[sizeof(ppp_settings.passwd)-1] = '\0';
robert_jw 0:b2805b6888dc 518 } else {
robert_jw 0:b2805b6888dc 519 ppp_settings.passwd[0] = '\0';
robert_jw 0:b2805b6888dc 520 }
robert_jw 0:b2805b6888dc 521 }
robert_jw 0:b2805b6888dc 522
robert_jw 0:b2805b6888dc 523 #if PPPOS_SUPPORT
robert_jw 0:b2805b6888dc 524 /** Open a new PPP connection using the given I/O device.
robert_jw 0:b2805b6888dc 525 * This initializes the PPP control block but does not
robert_jw 0:b2805b6888dc 526 * attempt to negotiate the LCP session. If this port
robert_jw 0:b2805b6888dc 527 * connects to a modem, the modem connection must be
robert_jw 0:b2805b6888dc 528 * established before calling this.
robert_jw 0:b2805b6888dc 529 * Return a new PPP connection descriptor on success or
robert_jw 0:b2805b6888dc 530 * an error code (negative) on failure.
robert_jw 0:b2805b6888dc 531 *
robert_jw 0:b2805b6888dc 532 * pppOpen() is directly defined to this function.
robert_jw 0:b2805b6888dc 533 */
robert_jw 0:b2805b6888dc 534 int
robert_jw 0:b2805b6888dc 535 pppOverSerialOpen(sio_fd_t fd, void (*linkStatusCB)(void *ctx, int errCode, void *arg), void *linkStatusCtx)
robert_jw 0:b2805b6888dc 536 {
robert_jw 0:b2805b6888dc 537 PPPControl *pc;
robert_jw 0:b2805b6888dc 538 int pd;
robert_jw 0:b2805b6888dc 539
robert_jw 0:b2805b6888dc 540 if (linkStatusCB == NULL) {
robert_jw 0:b2805b6888dc 541 /* PPP is single-threaded: without a callback,
robert_jw 0:b2805b6888dc 542 * there is no way to know when the link is up. */
robert_jw 0:b2805b6888dc 543 return PPPERR_PARAM;
robert_jw 0:b2805b6888dc 544 }
robert_jw 0:b2805b6888dc 545
robert_jw 0:b2805b6888dc 546 /* Find a free PPP session descriptor. */
robert_jw 0:b2805b6888dc 547 for (pd = 0; pd < NUM_PPP && pppControl[pd].openFlag != 0; pd++);
robert_jw 0:b2805b6888dc 548
robert_jw 0:b2805b6888dc 549 if (pd >= NUM_PPP) {
robert_jw 0:b2805b6888dc 550 pd = PPPERR_OPEN;
robert_jw 0:b2805b6888dc 551 } else {
robert_jw 0:b2805b6888dc 552 pc = &pppControl[pd];
robert_jw 0:b2805b6888dc 553 /* @todo: is this correct or do I overwrite something? */
robert_jw 0:b2805b6888dc 554 memset(pc, 0, sizeof(PPPControl));
robert_jw 0:b2805b6888dc 555 pc->rx.pd = pd;
robert_jw 0:b2805b6888dc 556 pc->rx.fd = fd;
robert_jw 0:b2805b6888dc 557
robert_jw 0:b2805b6888dc 558 pc->openFlag = 1;
robert_jw 0:b2805b6888dc 559 pc->fd = fd;
robert_jw 0:b2805b6888dc 560
robert_jw 0:b2805b6888dc 561 #if VJ_SUPPORT
robert_jw 0:b2805b6888dc 562 vj_compress_init(&pc->vjComp);
robert_jw 0:b2805b6888dc 563 #endif /* VJ_SUPPORT */
robert_jw 0:b2805b6888dc 564
robert_jw 0:b2805b6888dc 565 /*
robert_jw 0:b2805b6888dc 566 * Default the in and out accm so that escape and flag characters
robert_jw 0:b2805b6888dc 567 * are always escaped.
robert_jw 0:b2805b6888dc 568 */
robert_jw 0:b2805b6888dc 569 pc->rx.inACCM[15] = 0x60; /* no need to protect since RX is not running */
robert_jw 0:b2805b6888dc 570 pc->outACCM[15] = 0x60;
robert_jw 0:b2805b6888dc 571
robert_jw 0:b2805b6888dc 572 pc->linkStatusCB = linkStatusCB;
robert_jw 0:b2805b6888dc 573 pc->linkStatusCtx = linkStatusCtx;
robert_jw 0:b2805b6888dc 574
robert_jw 0:b2805b6888dc 575 /*
robert_jw 0:b2805b6888dc 576 * Start the connection and handle incoming events (packet or timeout).
robert_jw 0:b2805b6888dc 577 */
robert_jw 0:b2805b6888dc 578 PPPDEBUG(LOG_INFO, ("pppOverSerialOpen: unit %d: Connecting\n", pd));
robert_jw 0:b2805b6888dc 579 pppStart(pd);
robert_jw 0:b2805b6888dc 580 #if PPP_INPROC_OWNTHREAD
robert_jw 0:b2805b6888dc 581 sys_mbox_post(&pppMbox, (void*)&pc->rx);
robert_jw 0:b2805b6888dc 582 #endif
robert_jw 0:b2805b6888dc 583 }
robert_jw 0:b2805b6888dc 584
robert_jw 0:b2805b6888dc 585 return pd;
robert_jw 0:b2805b6888dc 586 }
robert_jw 0:b2805b6888dc 587 #endif /* PPPOS_SUPPORT */
robert_jw 0:b2805b6888dc 588
robert_jw 0:b2805b6888dc 589 #if PPPOE_SUPPORT
robert_jw 0:b2805b6888dc 590 static void pppOverEthernetLinkStatusCB(int pd, int up);
robert_jw 0:b2805b6888dc 591
robert_jw 0:b2805b6888dc 592 void
robert_jw 0:b2805b6888dc 593 pppOverEthernetClose(int pd)
robert_jw 0:b2805b6888dc 594 {
robert_jw 0:b2805b6888dc 595 PPPControl* pc = &pppControl[pd];
robert_jw 0:b2805b6888dc 596
robert_jw 0:b2805b6888dc 597 /* *TJL* There's no lcp_deinit */
robert_jw 0:b2805b6888dc 598 lcp_close(pd, NULL);
robert_jw 0:b2805b6888dc 599
robert_jw 0:b2805b6888dc 600 pppoe_destroy(&pc->netif);
robert_jw 0:b2805b6888dc 601 }
robert_jw 0:b2805b6888dc 602
robert_jw 0:b2805b6888dc 603 int pppOverEthernetOpen(struct netif *ethif, const char *service_name, const char *concentrator_name, void (*linkStatusCB)(void *ctx, int errCode, void *arg), void *linkStatusCtx)
robert_jw 0:b2805b6888dc 604 {
robert_jw 0:b2805b6888dc 605 PPPControl *pc;
robert_jw 0:b2805b6888dc 606 int pd;
robert_jw 0:b2805b6888dc 607
robert_jw 0:b2805b6888dc 608 LWIP_UNUSED_ARG(service_name);
robert_jw 0:b2805b6888dc 609 LWIP_UNUSED_ARG(concentrator_name);
robert_jw 0:b2805b6888dc 610
robert_jw 0:b2805b6888dc 611 if (linkStatusCB == NULL) {
robert_jw 0:b2805b6888dc 612 /* PPP is single-threaded: without a callback,
robert_jw 0:b2805b6888dc 613 * there is no way to know when the link is up. */
robert_jw 0:b2805b6888dc 614 return PPPERR_PARAM;
robert_jw 0:b2805b6888dc 615 }
robert_jw 0:b2805b6888dc 616
robert_jw 0:b2805b6888dc 617 /* Find a free PPP session descriptor. Critical region? */
robert_jw 0:b2805b6888dc 618 for (pd = 0; pd < NUM_PPP && pppControl[pd].openFlag != 0; pd++);
robert_jw 0:b2805b6888dc 619 if (pd >= NUM_PPP) {
robert_jw 0:b2805b6888dc 620 pd = PPPERR_OPEN;
robert_jw 0:b2805b6888dc 621 } else {
robert_jw 0:b2805b6888dc 622 pc = &pppControl[pd];
robert_jw 0:b2805b6888dc 623 memset(pc, 0, sizeof(PPPControl));
robert_jw 0:b2805b6888dc 624 pc->openFlag = 1;
robert_jw 0:b2805b6888dc 625 pc->ethif = ethif;
robert_jw 0:b2805b6888dc 626
robert_jw 0:b2805b6888dc 627 pc->linkStatusCB = linkStatusCB;
robert_jw 0:b2805b6888dc 628 pc->linkStatusCtx = linkStatusCtx;
robert_jw 0:b2805b6888dc 629
robert_jw 0:b2805b6888dc 630 lcp_wantoptions[pd].mru = PPPOE_MAXMTU;
robert_jw 0:b2805b6888dc 631 lcp_wantoptions[pd].neg_asyncmap = 0;
robert_jw 0:b2805b6888dc 632 lcp_wantoptions[pd].neg_pcompression = 0;
robert_jw 0:b2805b6888dc 633 lcp_wantoptions[pd].neg_accompression = 0;
robert_jw 0:b2805b6888dc 634
robert_jw 0:b2805b6888dc 635 lcp_allowoptions[pd].mru = PPPOE_MAXMTU;
robert_jw 0:b2805b6888dc 636 lcp_allowoptions[pd].neg_asyncmap = 0;
robert_jw 0:b2805b6888dc 637 lcp_allowoptions[pd].neg_pcompression = 0;
robert_jw 0:b2805b6888dc 638 lcp_allowoptions[pd].neg_accompression = 0;
robert_jw 0:b2805b6888dc 639
robert_jw 0:b2805b6888dc 640 if(pppoe_create(ethif, pd, pppOverEthernetLinkStatusCB, &pc->pppoe_sc) != ERR_OK) {
robert_jw 0:b2805b6888dc 641 pc->openFlag = 0;
robert_jw 0:b2805b6888dc 642 return PPPERR_OPEN;
robert_jw 0:b2805b6888dc 643 }
robert_jw 0:b2805b6888dc 644
robert_jw 0:b2805b6888dc 645 pppoe_connect(pc->pppoe_sc);
robert_jw 0:b2805b6888dc 646 }
robert_jw 0:b2805b6888dc 647
robert_jw 0:b2805b6888dc 648 return pd;
robert_jw 0:b2805b6888dc 649 }
robert_jw 0:b2805b6888dc 650 #endif /* PPPOE_SUPPORT */
robert_jw 0:b2805b6888dc 651
robert_jw 0:b2805b6888dc 652
robert_jw 0:b2805b6888dc 653 /* Close a PPP connection and release the descriptor.
robert_jw 0:b2805b6888dc 654 * Any outstanding packets in the queues are dropped.
robert_jw 0:b2805b6888dc 655 * Return 0 on success, an error code on failure. */
robert_jw 0:b2805b6888dc 656 int
robert_jw 0:b2805b6888dc 657 pppClose(int pd)
robert_jw 0:b2805b6888dc 658 {
robert_jw 0:b2805b6888dc 659 PPPControl *pc = &pppControl[pd];
robert_jw 0:b2805b6888dc 660 int st = 0;
robert_jw 0:b2805b6888dc 661
robert_jw 0:b2805b6888dc 662 PPPDEBUG(LOG_DEBUG, ("pppClose() called\n"));
robert_jw 0:b2805b6888dc 663
robert_jw 0:b2805b6888dc 664 /* Disconnect */
robert_jw 0:b2805b6888dc 665 #if PPPOE_SUPPORT
robert_jw 0:b2805b6888dc 666 if(pc->ethif) {
robert_jw 0:b2805b6888dc 667 PPPDEBUG(LOG_DEBUG, ("pppClose: unit %d kill_link -> pppStop\n", pd));
robert_jw 0:b2805b6888dc 668 pc->errCode = PPPERR_USER;
robert_jw 0:b2805b6888dc 669 /* This will leave us at PHASE_DEAD. */
robert_jw 0:b2805b6888dc 670 pppStop(pd);
robert_jw 0:b2805b6888dc 671 } else
robert_jw 0:b2805b6888dc 672 #endif /* PPPOE_SUPPORT */
robert_jw 0:b2805b6888dc 673 {
robert_jw 0:b2805b6888dc 674 #if PPPOS_SUPPORT
robert_jw 0:b2805b6888dc 675 PPPDEBUG(LOG_DEBUG, ("pppClose: unit %d kill_link -> pppStop\n", pd));
robert_jw 0:b2805b6888dc 676 pc->errCode = PPPERR_USER;
robert_jw 0:b2805b6888dc 677 /* This will leave us at PHASE_DEAD. */
robert_jw 0:b2805b6888dc 678 pppStop(pd);
robert_jw 0:b2805b6888dc 679 pppRecvWakeup(pd);
robert_jw 0:b2805b6888dc 680 #endif /* PPPOS_SUPPORT */
robert_jw 0:b2805b6888dc 681 }
robert_jw 0:b2805b6888dc 682
robert_jw 0:b2805b6888dc 683 return st;
robert_jw 0:b2805b6888dc 684 }
robert_jw 0:b2805b6888dc 685
robert_jw 0:b2805b6888dc 686 /* This function is called when carrier is lost on the PPP channel. */
robert_jw 0:b2805b6888dc 687 void
robert_jw 0:b2805b6888dc 688 pppSigHUP(int pd)
robert_jw 0:b2805b6888dc 689 {
robert_jw 0:b2805b6888dc 690 PPPDEBUG(LOG_DEBUG, ("pppSigHUP: unit %d sig_hup -> pppHupCB\n", pd));
robert_jw 0:b2805b6888dc 691 pppHup(pd);
robert_jw 0:b2805b6888dc 692 }
robert_jw 0:b2805b6888dc 693
robert_jw 0:b2805b6888dc 694 #if PPPOS_SUPPORT
robert_jw 0:b2805b6888dc 695 static void
robert_jw 0:b2805b6888dc 696 nPut(PPPControl *pc, struct pbuf *nb)
robert_jw 0:b2805b6888dc 697 {
robert_jw 0:b2805b6888dc 698 struct pbuf *b;
robert_jw 0:b2805b6888dc 699 int c;
robert_jw 0:b2805b6888dc 700
robert_jw 0:b2805b6888dc 701 for(b = nb; b != NULL; b = b->next) {
robert_jw 0:b2805b6888dc 702 if((c = sio_write(pc->fd, b->payload, b->len)) != b->len) {
robert_jw 0:b2805b6888dc 703 PPPDEBUG(LOG_WARNING,
robert_jw 0:b2805b6888dc 704 ("PPP nPut: incomplete sio_write(fd:%"SZT_F", len:%d, c: 0x%"X8_F") c = %d\n", (size_t)pc->fd, b->len, c, c));
robert_jw 0:b2805b6888dc 705 LINK_STATS_INC(link.err);
robert_jw 0:b2805b6888dc 706 pc->lastXMit = 0; /* prepend PPP_FLAG to next packet */
robert_jw 0:b2805b6888dc 707 snmp_inc_ifoutdiscards(&pc->netif);
robert_jw 0:b2805b6888dc 708 pbuf_free(nb);
robert_jw 0:b2805b6888dc 709 return;
robert_jw 0:b2805b6888dc 710 }
robert_jw 0:b2805b6888dc 711 }
robert_jw 0:b2805b6888dc 712
robert_jw 0:b2805b6888dc 713 snmp_add_ifoutoctets(&pc->netif, nb->tot_len);
robert_jw 0:b2805b6888dc 714 snmp_inc_ifoutucastpkts(&pc->netif);
robert_jw 0:b2805b6888dc 715 pbuf_free(nb);
robert_jw 0:b2805b6888dc 716 LINK_STATS_INC(link.xmit);
robert_jw 0:b2805b6888dc 717 }
robert_jw 0:b2805b6888dc 718
robert_jw 0:b2805b6888dc 719 /*
robert_jw 0:b2805b6888dc 720 * pppAppend - append given character to end of given pbuf. If outACCM
robert_jw 0:b2805b6888dc 721 * is not NULL and the character needs to be escaped, do so.
robert_jw 0:b2805b6888dc 722 * If pbuf is full, append another.
robert_jw 0:b2805b6888dc 723 * Return the current pbuf.
robert_jw 0:b2805b6888dc 724 */
robert_jw 0:b2805b6888dc 725 static struct pbuf *
robert_jw 0:b2805b6888dc 726 pppAppend(u_char c, struct pbuf *nb, ext_accm *outACCM)
robert_jw 0:b2805b6888dc 727 {
robert_jw 0:b2805b6888dc 728 struct pbuf *tb = nb;
robert_jw 0:b2805b6888dc 729
robert_jw 0:b2805b6888dc 730 /* Make sure there is room for the character and an escape code.
robert_jw 0:b2805b6888dc 731 * Sure we don't quite fill the buffer if the character doesn't
robert_jw 0:b2805b6888dc 732 * get escaped but is one character worth complicating this? */
robert_jw 0:b2805b6888dc 733 /* Note: We assume no packet header. */
robert_jw 0:b2805b6888dc 734 if (nb && (PBUF_POOL_BUFSIZE - nb->len) < 2) {
robert_jw 0:b2805b6888dc 735 tb = pbuf_alloc(PBUF_RAW, 0, PBUF_POOL);
robert_jw 0:b2805b6888dc 736 if (tb) {
robert_jw 0:b2805b6888dc 737 nb->next = tb;
robert_jw 0:b2805b6888dc 738 } else {
robert_jw 0:b2805b6888dc 739 LINK_STATS_INC(link.memerr);
robert_jw 0:b2805b6888dc 740 }
robert_jw 0:b2805b6888dc 741 nb = tb;
robert_jw 0:b2805b6888dc 742 }
robert_jw 0:b2805b6888dc 743
robert_jw 0:b2805b6888dc 744 if (nb) {
robert_jw 0:b2805b6888dc 745 if (outACCM && ESCAPE_P(*outACCM, c)) {
robert_jw 0:b2805b6888dc 746 *((u_char*)nb->payload + nb->len++) = PPP_ESCAPE;
robert_jw 0:b2805b6888dc 747 *((u_char*)nb->payload + nb->len++) = c ^ PPP_TRANS;
robert_jw 0:b2805b6888dc 748 } else {
robert_jw 0:b2805b6888dc 749 *((u_char*)nb->payload + nb->len++) = c;
robert_jw 0:b2805b6888dc 750 }
robert_jw 0:b2805b6888dc 751 }
robert_jw 0:b2805b6888dc 752
robert_jw 0:b2805b6888dc 753 return tb;
robert_jw 0:b2805b6888dc 754 }
robert_jw 0:b2805b6888dc 755 #endif /* PPPOS_SUPPORT */
robert_jw 0:b2805b6888dc 756
robert_jw 0:b2805b6888dc 757 #if PPPOE_SUPPORT
robert_jw 0:b2805b6888dc 758 static err_t
robert_jw 0:b2805b6888dc 759 pppifOutputOverEthernet(int pd, struct pbuf *p)
robert_jw 0:b2805b6888dc 760 {
robert_jw 0:b2805b6888dc 761 PPPControl *pc = &pppControl[pd];
robert_jw 0:b2805b6888dc 762 struct pbuf *pb;
robert_jw 0:b2805b6888dc 763 u_short protocol = PPP_IP;
robert_jw 0:b2805b6888dc 764 int i=0;
robert_jw 0:b2805b6888dc 765 u16_t tot_len;
robert_jw 0:b2805b6888dc 766
robert_jw 0:b2805b6888dc 767 /* @todo: try to use pbuf_header() here! */
robert_jw 0:b2805b6888dc 768 pb = pbuf_alloc(PBUF_LINK, PPPOE_HDRLEN + sizeof(protocol), PBUF_RAM);
robert_jw 0:b2805b6888dc 769 if(!pb) {
robert_jw 0:b2805b6888dc 770 LINK_STATS_INC(link.memerr);
robert_jw 0:b2805b6888dc 771 LINK_STATS_INC(link.proterr);
robert_jw 0:b2805b6888dc 772 snmp_inc_ifoutdiscards(&pc->netif);
robert_jw 0:b2805b6888dc 773 return ERR_MEM;
robert_jw 0:b2805b6888dc 774 }
robert_jw 0:b2805b6888dc 775
robert_jw 0:b2805b6888dc 776 pbuf_header(pb, -(s16_t)PPPOE_HDRLEN);
robert_jw 0:b2805b6888dc 777
robert_jw 0:b2805b6888dc 778 pc->lastXMit = sys_jiffies();
robert_jw 0:b2805b6888dc 779
robert_jw 0:b2805b6888dc 780 if (!pc->pcomp || protocol > 0xFF) {
robert_jw 0:b2805b6888dc 781 *((u_char*)pb->payload + i++) = (protocol >> 8) & 0xFF;
robert_jw 0:b2805b6888dc 782 }
robert_jw 0:b2805b6888dc 783 *((u_char*)pb->payload + i) = protocol & 0xFF;
robert_jw 0:b2805b6888dc 784
robert_jw 0:b2805b6888dc 785 pbuf_chain(pb, p);
robert_jw 0:b2805b6888dc 786 tot_len = pb->tot_len;
robert_jw 0:b2805b6888dc 787
robert_jw 0:b2805b6888dc 788 if(pppoe_xmit(pc->pppoe_sc, pb) != ERR_OK) {
robert_jw 0:b2805b6888dc 789 LINK_STATS_INC(link.err);
robert_jw 0:b2805b6888dc 790 snmp_inc_ifoutdiscards(&pc->netif);
robert_jw 0:b2805b6888dc 791 return PPPERR_DEVICE;
robert_jw 0:b2805b6888dc 792 }
robert_jw 0:b2805b6888dc 793
robert_jw 0:b2805b6888dc 794 snmp_add_ifoutoctets(&pc->netif, tot_len);
robert_jw 0:b2805b6888dc 795 snmp_inc_ifoutucastpkts(&pc->netif);
robert_jw 0:b2805b6888dc 796 LINK_STATS_INC(link.xmit);
robert_jw 0:b2805b6888dc 797 return ERR_OK;
robert_jw 0:b2805b6888dc 798 }
robert_jw 0:b2805b6888dc 799 #endif /* PPPOE_SUPPORT */
robert_jw 0:b2805b6888dc 800
robert_jw 0:b2805b6888dc 801 /* Send a packet on the given connection. */
robert_jw 0:b2805b6888dc 802 static err_t
robert_jw 0:b2805b6888dc 803 pppifOutput(struct netif *netif, struct pbuf *pb, ip_addr_t *ipaddr)
robert_jw 0:b2805b6888dc 804 {
robert_jw 0:b2805b6888dc 805 int pd = (int)(size_t)netif->state;
robert_jw 0:b2805b6888dc 806 PPPControl *pc = &pppControl[pd];
robert_jw 0:b2805b6888dc 807 #if PPPOS_SUPPORT
robert_jw 0:b2805b6888dc 808 u_short protocol = PPP_IP;
robert_jw 0:b2805b6888dc 809 u_int fcsOut = PPP_INITFCS;
robert_jw 0:b2805b6888dc 810 struct pbuf *headMB = NULL, *tailMB = NULL, *p;
robert_jw 0:b2805b6888dc 811 u_char c;
robert_jw 0:b2805b6888dc 812 #endif /* PPPOS_SUPPORT */
robert_jw 0:b2805b6888dc 813
robert_jw 0:b2805b6888dc 814 LWIP_UNUSED_ARG(ipaddr);
robert_jw 0:b2805b6888dc 815
robert_jw 0:b2805b6888dc 816 /* Validate parameters. */
robert_jw 0:b2805b6888dc 817 /* We let any protocol value go through - it can't hurt us
robert_jw 0:b2805b6888dc 818 * and the peer will just drop it if it's not accepting it. */
robert_jw 0:b2805b6888dc 819 if (pd < 0 || pd >= NUM_PPP || !pc->openFlag || !pb) {
robert_jw 0:b2805b6888dc 820 PPPDEBUG(LOG_WARNING, ("pppifOutput[%d]: bad parms prot=%d pb=%p\n",
robert_jw 0:b2805b6888dc 821 pd, PPP_IP, pb));
robert_jw 0:b2805b6888dc 822 LINK_STATS_INC(link.opterr);
robert_jw 0:b2805b6888dc 823 LINK_STATS_INC(link.drop);
robert_jw 0:b2805b6888dc 824 snmp_inc_ifoutdiscards(netif);
robert_jw 0:b2805b6888dc 825 return ERR_ARG;
robert_jw 0:b2805b6888dc 826 }
robert_jw 0:b2805b6888dc 827
robert_jw 0:b2805b6888dc 828 /* Check that the link is up. */
robert_jw 0:b2805b6888dc 829 if (lcp_phase[pd] == PHASE_DEAD) {
robert_jw 0:b2805b6888dc 830 PPPDEBUG(LOG_ERR, ("pppifOutput[%d]: link not up\n", pd));
robert_jw 0:b2805b6888dc 831 LINK_STATS_INC(link.rterr);
robert_jw 0:b2805b6888dc 832 LINK_STATS_INC(link.drop);
robert_jw 0:b2805b6888dc 833 snmp_inc_ifoutdiscards(netif);
robert_jw 0:b2805b6888dc 834 return ERR_RTE;
robert_jw 0:b2805b6888dc 835 }
robert_jw 0:b2805b6888dc 836
robert_jw 0:b2805b6888dc 837 #if PPPOE_SUPPORT
robert_jw 0:b2805b6888dc 838 if(pc->ethif) {
robert_jw 0:b2805b6888dc 839 return pppifOutputOverEthernet(pd, pb);
robert_jw 0:b2805b6888dc 840 }
robert_jw 0:b2805b6888dc 841 #endif /* PPPOE_SUPPORT */
robert_jw 0:b2805b6888dc 842
robert_jw 0:b2805b6888dc 843 #if PPPOS_SUPPORT
robert_jw 0:b2805b6888dc 844 /* Grab an output buffer. */
robert_jw 0:b2805b6888dc 845 headMB = pbuf_alloc(PBUF_RAW, 0, PBUF_POOL);
robert_jw 0:b2805b6888dc 846 if (headMB == NULL) {
robert_jw 0:b2805b6888dc 847 PPPDEBUG(LOG_WARNING, ("pppifOutput[%d]: first alloc fail\n", pd));
robert_jw 0:b2805b6888dc 848 LINK_STATS_INC(link.memerr);
robert_jw 0:b2805b6888dc 849 LINK_STATS_INC(link.drop);
robert_jw 0:b2805b6888dc 850 snmp_inc_ifoutdiscards(netif);
robert_jw 0:b2805b6888dc 851 return ERR_MEM;
robert_jw 0:b2805b6888dc 852 }
robert_jw 0:b2805b6888dc 853
robert_jw 0:b2805b6888dc 854 #if VJ_SUPPORT
robert_jw 0:b2805b6888dc 855 /*
robert_jw 0:b2805b6888dc 856 * Attempt Van Jacobson header compression if VJ is configured and
robert_jw 0:b2805b6888dc 857 * this is an IP packet.
robert_jw 0:b2805b6888dc 858 */
robert_jw 0:b2805b6888dc 859 if (protocol == PPP_IP && pc->vjEnabled) {
robert_jw 0:b2805b6888dc 860 switch (vj_compress_tcp(&pc->vjComp, pb)) {
robert_jw 0:b2805b6888dc 861 case TYPE_IP:
robert_jw 0:b2805b6888dc 862 /* No change...
robert_jw 0:b2805b6888dc 863 protocol = PPP_IP_PROTOCOL; */
robert_jw 0:b2805b6888dc 864 break;
robert_jw 0:b2805b6888dc 865 case TYPE_COMPRESSED_TCP:
robert_jw 0:b2805b6888dc 866 protocol = PPP_VJC_COMP;
robert_jw 0:b2805b6888dc 867 break;
robert_jw 0:b2805b6888dc 868 case TYPE_UNCOMPRESSED_TCP:
robert_jw 0:b2805b6888dc 869 protocol = PPP_VJC_UNCOMP;
robert_jw 0:b2805b6888dc 870 break;
robert_jw 0:b2805b6888dc 871 default:
robert_jw 0:b2805b6888dc 872 PPPDEBUG(LOG_WARNING, ("pppifOutput[%d]: bad IP packet\n", pd));
robert_jw 0:b2805b6888dc 873 LINK_STATS_INC(link.proterr);
robert_jw 0:b2805b6888dc 874 LINK_STATS_INC(link.drop);
robert_jw 0:b2805b6888dc 875 snmp_inc_ifoutdiscards(netif);
robert_jw 0:b2805b6888dc 876 pbuf_free(headMB);
robert_jw 0:b2805b6888dc 877 return ERR_VAL;
robert_jw 0:b2805b6888dc 878 }
robert_jw 0:b2805b6888dc 879 }
robert_jw 0:b2805b6888dc 880 #endif /* VJ_SUPPORT */
robert_jw 0:b2805b6888dc 881
robert_jw 0:b2805b6888dc 882 tailMB = headMB;
robert_jw 0:b2805b6888dc 883
robert_jw 0:b2805b6888dc 884 /* Build the PPP header. */
robert_jw 0:b2805b6888dc 885 if ((sys_jiffies() - pc->lastXMit) >= PPP_MAXIDLEFLAG) {
robert_jw 0:b2805b6888dc 886 tailMB = pppAppend(PPP_FLAG, tailMB, NULL);
robert_jw 0:b2805b6888dc 887 }
robert_jw 0:b2805b6888dc 888
robert_jw 0:b2805b6888dc 889 pc->lastXMit = sys_jiffies();
robert_jw 0:b2805b6888dc 890 if (!pc->accomp) {
robert_jw 0:b2805b6888dc 891 fcsOut = PPP_FCS(fcsOut, PPP_ALLSTATIONS);
robert_jw 0:b2805b6888dc 892 tailMB = pppAppend(PPP_ALLSTATIONS, tailMB, &pc->outACCM);
robert_jw 0:b2805b6888dc 893 fcsOut = PPP_FCS(fcsOut, PPP_UI);
robert_jw 0:b2805b6888dc 894 tailMB = pppAppend(PPP_UI, tailMB, &pc->outACCM);
robert_jw 0:b2805b6888dc 895 }
robert_jw 0:b2805b6888dc 896 if (!pc->pcomp || protocol > 0xFF) {
robert_jw 0:b2805b6888dc 897 c = (protocol >> 8) & 0xFF;
robert_jw 0:b2805b6888dc 898 fcsOut = PPP_FCS(fcsOut, c);
robert_jw 0:b2805b6888dc 899 tailMB = pppAppend(c, tailMB, &pc->outACCM);
robert_jw 0:b2805b6888dc 900 }
robert_jw 0:b2805b6888dc 901 c = protocol & 0xFF;
robert_jw 0:b2805b6888dc 902 fcsOut = PPP_FCS(fcsOut, c);
robert_jw 0:b2805b6888dc 903 tailMB = pppAppend(c, tailMB, &pc->outACCM);
robert_jw 0:b2805b6888dc 904
robert_jw 0:b2805b6888dc 905 /* Load packet. */
robert_jw 0:b2805b6888dc 906 for(p = pb; p; p = p->next) {
robert_jw 0:b2805b6888dc 907 int n;
robert_jw 0:b2805b6888dc 908 u_char *sPtr;
robert_jw 0:b2805b6888dc 909
robert_jw 0:b2805b6888dc 910 sPtr = (u_char*)p->payload;
robert_jw 0:b2805b6888dc 911 n = p->len;
robert_jw 0:b2805b6888dc 912 while (n-- > 0) {
robert_jw 0:b2805b6888dc 913 c = *sPtr++;
robert_jw 0:b2805b6888dc 914
robert_jw 0:b2805b6888dc 915 /* Update FCS before checking for special characters. */
robert_jw 0:b2805b6888dc 916 fcsOut = PPP_FCS(fcsOut, c);
robert_jw 0:b2805b6888dc 917
robert_jw 0:b2805b6888dc 918 /* Copy to output buffer escaping special characters. */
robert_jw 0:b2805b6888dc 919 tailMB = pppAppend(c, tailMB, &pc->outACCM);
robert_jw 0:b2805b6888dc 920 }
robert_jw 0:b2805b6888dc 921 }
robert_jw 0:b2805b6888dc 922
robert_jw 0:b2805b6888dc 923 /* Add FCS and trailing flag. */
robert_jw 0:b2805b6888dc 924 c = ~fcsOut & 0xFF;
robert_jw 0:b2805b6888dc 925 tailMB = pppAppend(c, tailMB, &pc->outACCM);
robert_jw 0:b2805b6888dc 926 c = (~fcsOut >> 8) & 0xFF;
robert_jw 0:b2805b6888dc 927 tailMB = pppAppend(c, tailMB, &pc->outACCM);
robert_jw 0:b2805b6888dc 928 tailMB = pppAppend(PPP_FLAG, tailMB, NULL);
robert_jw 0:b2805b6888dc 929
robert_jw 0:b2805b6888dc 930 /* If we failed to complete the packet, throw it away. */
robert_jw 0:b2805b6888dc 931 if (!tailMB) {
robert_jw 0:b2805b6888dc 932 PPPDEBUG(LOG_WARNING,
robert_jw 0:b2805b6888dc 933 ("pppifOutput[%d]: Alloc err - dropping proto=%d\n",
robert_jw 0:b2805b6888dc 934 pd, protocol));
robert_jw 0:b2805b6888dc 935 pbuf_free(headMB);
robert_jw 0:b2805b6888dc 936 LINK_STATS_INC(link.memerr);
robert_jw 0:b2805b6888dc 937 LINK_STATS_INC(link.drop);
robert_jw 0:b2805b6888dc 938 snmp_inc_ifoutdiscards(netif);
robert_jw 0:b2805b6888dc 939 return ERR_MEM;
robert_jw 0:b2805b6888dc 940 }
robert_jw 0:b2805b6888dc 941
robert_jw 0:b2805b6888dc 942 /* Send it. */
robert_jw 0:b2805b6888dc 943 PPPDEBUG(LOG_INFO, ("pppifOutput[%d]: proto=0x%"X16_F"\n", pd, protocol));
robert_jw 0:b2805b6888dc 944
robert_jw 0:b2805b6888dc 945 nPut(pc, headMB);
robert_jw 0:b2805b6888dc 946 #endif /* PPPOS_SUPPORT */
robert_jw 0:b2805b6888dc 947
robert_jw 0:b2805b6888dc 948 return ERR_OK;
robert_jw 0:b2805b6888dc 949 }
robert_jw 0:b2805b6888dc 950
robert_jw 0:b2805b6888dc 951 /* Get and set parameters for the given connection.
robert_jw 0:b2805b6888dc 952 * Return 0 on success, an error code on failure. */
robert_jw 0:b2805b6888dc 953 int
robert_jw 0:b2805b6888dc 954 pppIOCtl(int pd, int cmd, void *arg)
robert_jw 0:b2805b6888dc 955 {
robert_jw 0:b2805b6888dc 956 PPPControl *pc = &pppControl[pd];
robert_jw 0:b2805b6888dc 957 int st = 0;
robert_jw 0:b2805b6888dc 958
robert_jw 0:b2805b6888dc 959 if (pd < 0 || pd >= NUM_PPP) {
robert_jw 0:b2805b6888dc 960 st = PPPERR_PARAM;
robert_jw 0:b2805b6888dc 961 } else {
robert_jw 0:b2805b6888dc 962 switch(cmd) {
robert_jw 0:b2805b6888dc 963 case PPPCTLG_UPSTATUS: /* Get the PPP up status. */
robert_jw 0:b2805b6888dc 964 if (arg) {
robert_jw 0:b2805b6888dc 965 *(int *)arg = (int)(pc->if_up);
robert_jw 0:b2805b6888dc 966 } else {
robert_jw 0:b2805b6888dc 967 st = PPPERR_PARAM;
robert_jw 0:b2805b6888dc 968 }
robert_jw 0:b2805b6888dc 969 break;
robert_jw 0:b2805b6888dc 970 case PPPCTLS_ERRCODE: /* Set the PPP error code. */
robert_jw 0:b2805b6888dc 971 if (arg) {
robert_jw 0:b2805b6888dc 972 pc->errCode = *(int *)arg;
robert_jw 0:b2805b6888dc 973 } else {
robert_jw 0:b2805b6888dc 974 st = PPPERR_PARAM;
robert_jw 0:b2805b6888dc 975 }
robert_jw 0:b2805b6888dc 976 break;
robert_jw 0:b2805b6888dc 977 case PPPCTLG_ERRCODE: /* Get the PPP error code. */
robert_jw 0:b2805b6888dc 978 if (arg) {
robert_jw 0:b2805b6888dc 979 *(int *)arg = (int)(pc->errCode);
robert_jw 0:b2805b6888dc 980 } else {
robert_jw 0:b2805b6888dc 981 st = PPPERR_PARAM;
robert_jw 0:b2805b6888dc 982 }
robert_jw 0:b2805b6888dc 983 break;
robert_jw 0:b2805b6888dc 984 #if PPPOS_SUPPORT
robert_jw 0:b2805b6888dc 985 case PPPCTLG_FD: /* Get the fd associated with the ppp */
robert_jw 0:b2805b6888dc 986 if (arg) {
robert_jw 0:b2805b6888dc 987 *(sio_fd_t *)arg = pc->fd;
robert_jw 0:b2805b6888dc 988 } else {
robert_jw 0:b2805b6888dc 989 st = PPPERR_PARAM;
robert_jw 0:b2805b6888dc 990 }
robert_jw 0:b2805b6888dc 991 break;
robert_jw 0:b2805b6888dc 992 #endif /* PPPOS_SUPPORT */
robert_jw 0:b2805b6888dc 993 default:
robert_jw 0:b2805b6888dc 994 st = PPPERR_PARAM;
robert_jw 0:b2805b6888dc 995 break;
robert_jw 0:b2805b6888dc 996 }
robert_jw 0:b2805b6888dc 997 }
robert_jw 0:b2805b6888dc 998
robert_jw 0:b2805b6888dc 999 return st;
robert_jw 0:b2805b6888dc 1000 }
robert_jw 0:b2805b6888dc 1001
robert_jw 0:b2805b6888dc 1002 /*
robert_jw 0:b2805b6888dc 1003 * Return the Maximum Transmission Unit for the given PPP connection.
robert_jw 0:b2805b6888dc 1004 */
robert_jw 0:b2805b6888dc 1005 u_short
robert_jw 0:b2805b6888dc 1006 pppMTU(int pd)
robert_jw 0:b2805b6888dc 1007 {
robert_jw 0:b2805b6888dc 1008 PPPControl *pc = &pppControl[pd];
robert_jw 0:b2805b6888dc 1009 u_short st;
robert_jw 0:b2805b6888dc 1010
robert_jw 0:b2805b6888dc 1011 /* Validate parameters. */
robert_jw 0:b2805b6888dc 1012 if (pd < 0 || pd >= NUM_PPP || !pc->openFlag) {
robert_jw 0:b2805b6888dc 1013 st = 0;
robert_jw 0:b2805b6888dc 1014 } else {
robert_jw 0:b2805b6888dc 1015 st = pc->mtu;
robert_jw 0:b2805b6888dc 1016 }
robert_jw 0:b2805b6888dc 1017
robert_jw 0:b2805b6888dc 1018 return st;
robert_jw 0:b2805b6888dc 1019 }
robert_jw 0:b2805b6888dc 1020
robert_jw 0:b2805b6888dc 1021 #if PPPOE_SUPPORT
robert_jw 0:b2805b6888dc 1022 int
robert_jw 0:b2805b6888dc 1023 pppWriteOverEthernet(int pd, const u_char *s, int n)
robert_jw 0:b2805b6888dc 1024 {
robert_jw 0:b2805b6888dc 1025 PPPControl *pc = &pppControl[pd];
robert_jw 0:b2805b6888dc 1026 struct pbuf *pb;
robert_jw 0:b2805b6888dc 1027
robert_jw 0:b2805b6888dc 1028 /* skip address & flags */
robert_jw 0:b2805b6888dc 1029 s += 2;
robert_jw 0:b2805b6888dc 1030 n -= 2;
robert_jw 0:b2805b6888dc 1031
robert_jw 0:b2805b6888dc 1032 LWIP_ASSERT("PPPOE_HDRLEN + n <= 0xffff", PPPOE_HDRLEN + n <= 0xffff);
robert_jw 0:b2805b6888dc 1033 pb = pbuf_alloc(PBUF_LINK, (u16_t)(PPPOE_HDRLEN + n), PBUF_RAM);
robert_jw 0:b2805b6888dc 1034 if(!pb) {
robert_jw 0:b2805b6888dc 1035 LINK_STATS_INC(link.memerr);
robert_jw 0:b2805b6888dc 1036 LINK_STATS_INC(link.proterr);
robert_jw 0:b2805b6888dc 1037 snmp_inc_ifoutdiscards(&pc->netif);
robert_jw 0:b2805b6888dc 1038 return PPPERR_ALLOC;
robert_jw 0:b2805b6888dc 1039 }
robert_jw 0:b2805b6888dc 1040
robert_jw 0:b2805b6888dc 1041 pbuf_header(pb, -(s16_t)PPPOE_HDRLEN);
robert_jw 0:b2805b6888dc 1042
robert_jw 0:b2805b6888dc 1043 pc->lastXMit = sys_jiffies();
robert_jw 0:b2805b6888dc 1044
robert_jw 0:b2805b6888dc 1045 MEMCPY(pb->payload, s, n);
robert_jw 0:b2805b6888dc 1046
robert_jw 0:b2805b6888dc 1047 if(pppoe_xmit(pc->pppoe_sc, pb) != ERR_OK) {
robert_jw 0:b2805b6888dc 1048 LINK_STATS_INC(link.err);
robert_jw 0:b2805b6888dc 1049 snmp_inc_ifoutdiscards(&pc->netif);
robert_jw 0:b2805b6888dc 1050 return PPPERR_DEVICE;
robert_jw 0:b2805b6888dc 1051 }
robert_jw 0:b2805b6888dc 1052
robert_jw 0:b2805b6888dc 1053 snmp_add_ifoutoctets(&pc->netif, (u16_t)n);
robert_jw 0:b2805b6888dc 1054 snmp_inc_ifoutucastpkts(&pc->netif);
robert_jw 0:b2805b6888dc 1055 LINK_STATS_INC(link.xmit);
robert_jw 0:b2805b6888dc 1056 return PPPERR_NONE;
robert_jw 0:b2805b6888dc 1057 }
robert_jw 0:b2805b6888dc 1058 #endif /* PPPOE_SUPPORT */
robert_jw 0:b2805b6888dc 1059
robert_jw 0:b2805b6888dc 1060 /*
robert_jw 0:b2805b6888dc 1061 * Write n characters to a ppp link.
robert_jw 0:b2805b6888dc 1062 * RETURN: >= 0 Number of characters written
robert_jw 0:b2805b6888dc 1063 * -1 Failed to write to device
robert_jw 0:b2805b6888dc 1064 */
robert_jw 0:b2805b6888dc 1065 int
robert_jw 0:b2805b6888dc 1066 pppWrite(int pd, const u_char *s, int n)
robert_jw 0:b2805b6888dc 1067 {
robert_jw 0:b2805b6888dc 1068 PPPControl *pc = &pppControl[pd];
robert_jw 0:b2805b6888dc 1069 #if PPPOS_SUPPORT
robert_jw 0:b2805b6888dc 1070 u_char c;
robert_jw 0:b2805b6888dc 1071 u_int fcsOut;
robert_jw 0:b2805b6888dc 1072 struct pbuf *headMB, *tailMB;
robert_jw 0:b2805b6888dc 1073 #endif /* PPPOS_SUPPORT */
robert_jw 0:b2805b6888dc 1074
robert_jw 0:b2805b6888dc 1075 #if PPPOE_SUPPORT
robert_jw 0:b2805b6888dc 1076 if(pc->ethif) {
robert_jw 0:b2805b6888dc 1077 return pppWriteOverEthernet(pd, s, n);
robert_jw 0:b2805b6888dc 1078 }
robert_jw 0:b2805b6888dc 1079 #endif /* PPPOE_SUPPORT */
robert_jw 0:b2805b6888dc 1080
robert_jw 0:b2805b6888dc 1081 #if PPPOS_SUPPORT
robert_jw 0:b2805b6888dc 1082 headMB = pbuf_alloc(PBUF_RAW, 0, PBUF_POOL);
robert_jw 0:b2805b6888dc 1083 if (headMB == NULL) {
robert_jw 0:b2805b6888dc 1084 LINK_STATS_INC(link.memerr);
robert_jw 0:b2805b6888dc 1085 LINK_STATS_INC(link.proterr);
robert_jw 0:b2805b6888dc 1086 snmp_inc_ifoutdiscards(&pc->netif);
robert_jw 0:b2805b6888dc 1087 return PPPERR_ALLOC;
robert_jw 0:b2805b6888dc 1088 }
robert_jw 0:b2805b6888dc 1089
robert_jw 0:b2805b6888dc 1090 tailMB = headMB;
robert_jw 0:b2805b6888dc 1091
robert_jw 0:b2805b6888dc 1092 /* If the link has been idle, we'll send a fresh flag character to
robert_jw 0:b2805b6888dc 1093 * flush any noise. */
robert_jw 0:b2805b6888dc 1094 if ((sys_jiffies() - pc->lastXMit) >= PPP_MAXIDLEFLAG) {
robert_jw 0:b2805b6888dc 1095 tailMB = pppAppend(PPP_FLAG, tailMB, NULL);
robert_jw 0:b2805b6888dc 1096 }
robert_jw 0:b2805b6888dc 1097 pc->lastXMit = sys_jiffies();
robert_jw 0:b2805b6888dc 1098
robert_jw 0:b2805b6888dc 1099 fcsOut = PPP_INITFCS;
robert_jw 0:b2805b6888dc 1100 /* Load output buffer. */
robert_jw 0:b2805b6888dc 1101 while (n-- > 0) {
robert_jw 0:b2805b6888dc 1102 c = *s++;
robert_jw 0:b2805b6888dc 1103
robert_jw 0:b2805b6888dc 1104 /* Update FCS before checking for special characters. */
robert_jw 0:b2805b6888dc 1105 fcsOut = PPP_FCS(fcsOut, c);
robert_jw 0:b2805b6888dc 1106
robert_jw 0:b2805b6888dc 1107 /* Copy to output buffer escaping special characters. */
robert_jw 0:b2805b6888dc 1108 tailMB = pppAppend(c, tailMB, &pc->outACCM);
robert_jw 0:b2805b6888dc 1109 }
robert_jw 0:b2805b6888dc 1110
robert_jw 0:b2805b6888dc 1111 /* Add FCS and trailing flag. */
robert_jw 0:b2805b6888dc 1112 c = ~fcsOut & 0xFF;
robert_jw 0:b2805b6888dc 1113 tailMB = pppAppend(c, tailMB, &pc->outACCM);
robert_jw 0:b2805b6888dc 1114 c = (~fcsOut >> 8) & 0xFF;
robert_jw 0:b2805b6888dc 1115 tailMB = pppAppend(c, tailMB, &pc->outACCM);
robert_jw 0:b2805b6888dc 1116 tailMB = pppAppend(PPP_FLAG, tailMB, NULL);
robert_jw 0:b2805b6888dc 1117
robert_jw 0:b2805b6888dc 1118 /* If we failed to complete the packet, throw it away.
robert_jw 0:b2805b6888dc 1119 * Otherwise send it. */
robert_jw 0:b2805b6888dc 1120 if (!tailMB) {
robert_jw 0:b2805b6888dc 1121 PPPDEBUG(LOG_WARNING,
robert_jw 0:b2805b6888dc 1122 ("pppWrite[%d]: Alloc err - dropping pbuf len=%d\n", pd, headMB->len));
robert_jw 0:b2805b6888dc 1123 /*"pppWrite[%d]: Alloc err - dropping %d:%.*H", pd, headMB->len, LWIP_MIN(headMB->len * 2, 40), headMB->payload)); */
robert_jw 0:b2805b6888dc 1124 pbuf_free(headMB);
robert_jw 0:b2805b6888dc 1125 LINK_STATS_INC(link.memerr);
robert_jw 0:b2805b6888dc 1126 LINK_STATS_INC(link.proterr);
robert_jw 0:b2805b6888dc 1127 snmp_inc_ifoutdiscards(&pc->netif);
robert_jw 0:b2805b6888dc 1128 return PPPERR_ALLOC;
robert_jw 0:b2805b6888dc 1129 }
robert_jw 0:b2805b6888dc 1130
robert_jw 0:b2805b6888dc 1131 PPPDEBUG(LOG_INFO, ("pppWrite[%d]: len=%d\n", pd, headMB->len));
robert_jw 0:b2805b6888dc 1132 /* "pppWrite[%d]: %d:%.*H", pd, headMB->len, LWIP_MIN(headMB->len * 2, 40), headMB->payload)); */
robert_jw 0:b2805b6888dc 1133 nPut(pc, headMB);
robert_jw 0:b2805b6888dc 1134 #endif /* PPPOS_SUPPORT */
robert_jw 0:b2805b6888dc 1135
robert_jw 0:b2805b6888dc 1136 return PPPERR_NONE;
robert_jw 0:b2805b6888dc 1137 }
robert_jw 0:b2805b6888dc 1138
robert_jw 0:b2805b6888dc 1139 /*
robert_jw 0:b2805b6888dc 1140 * ppp_send_config - configure the transmit characteristics of
robert_jw 0:b2805b6888dc 1141 * the ppp interface.
robert_jw 0:b2805b6888dc 1142 */
robert_jw 0:b2805b6888dc 1143 void
robert_jw 0:b2805b6888dc 1144 ppp_send_config( int unit, u16_t mtu, u32_t asyncmap, int pcomp, int accomp)
robert_jw 0:b2805b6888dc 1145 {
robert_jw 0:b2805b6888dc 1146 PPPControl *pc = &pppControl[unit];
robert_jw 0:b2805b6888dc 1147 int i;
robert_jw 0:b2805b6888dc 1148
robert_jw 0:b2805b6888dc 1149 pc->mtu = mtu;
robert_jw 0:b2805b6888dc 1150 pc->pcomp = pcomp;
robert_jw 0:b2805b6888dc 1151 pc->accomp = accomp;
robert_jw 0:b2805b6888dc 1152
robert_jw 0:b2805b6888dc 1153 /* Load the ACCM bits for the 32 control codes. */
robert_jw 0:b2805b6888dc 1154 for (i = 0; i < 32/8; i++) {
robert_jw 0:b2805b6888dc 1155 pc->outACCM[i] = (u_char)((asyncmap >> (8 * i)) & 0xFF);
robert_jw 0:b2805b6888dc 1156 }
robert_jw 0:b2805b6888dc 1157 PPPDEBUG(LOG_INFO, ("ppp_send_config[%d]: outACCM=%X %X %X %X\n",
robert_jw 0:b2805b6888dc 1158 unit,
robert_jw 0:b2805b6888dc 1159 pc->outACCM[0], pc->outACCM[1], pc->outACCM[2], pc->outACCM[3]));
robert_jw 0:b2805b6888dc 1160 }
robert_jw 0:b2805b6888dc 1161
robert_jw 0:b2805b6888dc 1162
robert_jw 0:b2805b6888dc 1163 /*
robert_jw 0:b2805b6888dc 1164 * ppp_set_xaccm - set the extended transmit ACCM for the interface.
robert_jw 0:b2805b6888dc 1165 */
robert_jw 0:b2805b6888dc 1166 void
robert_jw 0:b2805b6888dc 1167 ppp_set_xaccm(int unit, ext_accm *accm)
robert_jw 0:b2805b6888dc 1168 {
robert_jw 0:b2805b6888dc 1169 SMEMCPY(pppControl[unit].outACCM, accm, sizeof(ext_accm));
robert_jw 0:b2805b6888dc 1170 PPPDEBUG(LOG_INFO, ("ppp_set_xaccm[%d]: outACCM=%X %X %X %X\n",
robert_jw 0:b2805b6888dc 1171 unit,
robert_jw 0:b2805b6888dc 1172 pppControl[unit].outACCM[0],
robert_jw 0:b2805b6888dc 1173 pppControl[unit].outACCM[1],
robert_jw 0:b2805b6888dc 1174 pppControl[unit].outACCM[2],
robert_jw 0:b2805b6888dc 1175 pppControl[unit].outACCM[3]));
robert_jw 0:b2805b6888dc 1176 }
robert_jw 0:b2805b6888dc 1177
robert_jw 0:b2805b6888dc 1178
robert_jw 0:b2805b6888dc 1179 /*
robert_jw 0:b2805b6888dc 1180 * ppp_recv_config - configure the receive-side characteristics of
robert_jw 0:b2805b6888dc 1181 * the ppp interface.
robert_jw 0:b2805b6888dc 1182 */
robert_jw 0:b2805b6888dc 1183 void
robert_jw 0:b2805b6888dc 1184 ppp_recv_config( int unit, int mru, u32_t asyncmap, int pcomp, int accomp)
robert_jw 0:b2805b6888dc 1185 {
robert_jw 0:b2805b6888dc 1186 PPPControl *pc = &pppControl[unit];
robert_jw 0:b2805b6888dc 1187 int i;
robert_jw 0:b2805b6888dc 1188 SYS_ARCH_DECL_PROTECT(lev);
robert_jw 0:b2805b6888dc 1189
robert_jw 0:b2805b6888dc 1190 LWIP_UNUSED_ARG(accomp);
robert_jw 0:b2805b6888dc 1191 LWIP_UNUSED_ARG(pcomp);
robert_jw 0:b2805b6888dc 1192 LWIP_UNUSED_ARG(mru);
robert_jw 0:b2805b6888dc 1193
robert_jw 0:b2805b6888dc 1194 /* Load the ACCM bits for the 32 control codes. */
robert_jw 0:b2805b6888dc 1195 SYS_ARCH_PROTECT(lev);
robert_jw 0:b2805b6888dc 1196 for (i = 0; i < 32 / 8; i++) {
robert_jw 0:b2805b6888dc 1197 /* @todo: does this work? ext_accm has been modified from pppd! */
robert_jw 0:b2805b6888dc 1198 pc->rx.inACCM[i] = (u_char)(asyncmap >> (i * 8));
robert_jw 0:b2805b6888dc 1199 }
robert_jw 0:b2805b6888dc 1200 SYS_ARCH_UNPROTECT(lev);
robert_jw 0:b2805b6888dc 1201 PPPDEBUG(LOG_INFO, ("ppp_recv_config[%d]: inACCM=%X %X %X %X\n",
robert_jw 0:b2805b6888dc 1202 unit,
robert_jw 0:b2805b6888dc 1203 pc->rx.inACCM[0], pc->rx.inACCM[1], pc->rx.inACCM[2], pc->rx.inACCM[3]));
robert_jw 0:b2805b6888dc 1204 }
robert_jw 0:b2805b6888dc 1205
robert_jw 0:b2805b6888dc 1206 #if 0
robert_jw 0:b2805b6888dc 1207 /*
robert_jw 0:b2805b6888dc 1208 * ccp_test - ask kernel whether a given compression method
robert_jw 0:b2805b6888dc 1209 * is acceptable for use. Returns 1 if the method and parameters
robert_jw 0:b2805b6888dc 1210 * are OK, 0 if the method is known but the parameters are not OK
robert_jw 0:b2805b6888dc 1211 * (e.g. code size should be reduced), or -1 if the method is unknown.
robert_jw 0:b2805b6888dc 1212 */
robert_jw 0:b2805b6888dc 1213 int
robert_jw 0:b2805b6888dc 1214 ccp_test( int unit, int opt_len, int for_transmit, u_char *opt_ptr)
robert_jw 0:b2805b6888dc 1215 {
robert_jw 0:b2805b6888dc 1216 return 0; /* XXX Currently no compression. */
robert_jw 0:b2805b6888dc 1217 }
robert_jw 0:b2805b6888dc 1218
robert_jw 0:b2805b6888dc 1219 /*
robert_jw 0:b2805b6888dc 1220 * ccp_flags_set - inform kernel about the current state of CCP.
robert_jw 0:b2805b6888dc 1221 */
robert_jw 0:b2805b6888dc 1222 void
robert_jw 0:b2805b6888dc 1223 ccp_flags_set(int unit, int isopen, int isup)
robert_jw 0:b2805b6888dc 1224 {
robert_jw 0:b2805b6888dc 1225 /* XXX */
robert_jw 0:b2805b6888dc 1226 }
robert_jw 0:b2805b6888dc 1227
robert_jw 0:b2805b6888dc 1228 /*
robert_jw 0:b2805b6888dc 1229 * ccp_fatal_error - returns 1 if decompression was disabled as a
robert_jw 0:b2805b6888dc 1230 * result of an error detected after decompression of a packet,
robert_jw 0:b2805b6888dc 1231 * 0 otherwise. This is necessary because of patent nonsense.
robert_jw 0:b2805b6888dc 1232 */
robert_jw 0:b2805b6888dc 1233 int
robert_jw 0:b2805b6888dc 1234 ccp_fatal_error(int unit)
robert_jw 0:b2805b6888dc 1235 {
robert_jw 0:b2805b6888dc 1236 /* XXX */
robert_jw 0:b2805b6888dc 1237 return 0;
robert_jw 0:b2805b6888dc 1238 }
robert_jw 0:b2805b6888dc 1239 #endif
robert_jw 0:b2805b6888dc 1240
robert_jw 0:b2805b6888dc 1241 /*
robert_jw 0:b2805b6888dc 1242 * get_idle_time - return how long the link has been idle.
robert_jw 0:b2805b6888dc 1243 */
robert_jw 0:b2805b6888dc 1244 int
robert_jw 0:b2805b6888dc 1245 get_idle_time(int u, struct ppp_idle *ip)
robert_jw 0:b2805b6888dc 1246 {
robert_jw 0:b2805b6888dc 1247 /* XXX */
robert_jw 0:b2805b6888dc 1248 LWIP_UNUSED_ARG(u);
robert_jw 0:b2805b6888dc 1249 LWIP_UNUSED_ARG(ip);
robert_jw 0:b2805b6888dc 1250
robert_jw 0:b2805b6888dc 1251 return 0;
robert_jw 0:b2805b6888dc 1252 }
robert_jw 0:b2805b6888dc 1253
robert_jw 0:b2805b6888dc 1254
robert_jw 0:b2805b6888dc 1255 /*
robert_jw 0:b2805b6888dc 1256 * Return user specified netmask, modified by any mask we might determine
robert_jw 0:b2805b6888dc 1257 * for address `addr' (in network byte order).
robert_jw 0:b2805b6888dc 1258 * Here we scan through the system's list of interfaces, looking for
robert_jw 0:b2805b6888dc 1259 * any non-point-to-point interfaces which might appear to be on the same
robert_jw 0:b2805b6888dc 1260 * network as `addr'. If we find any, we OR in their netmask to the
robert_jw 0:b2805b6888dc 1261 * user-specified netmask.
robert_jw 0:b2805b6888dc 1262 */
robert_jw 0:b2805b6888dc 1263 u32_t
robert_jw 0:b2805b6888dc 1264 GetMask(u32_t addr)
robert_jw 0:b2805b6888dc 1265 {
robert_jw 0:b2805b6888dc 1266 u32_t mask, nmask;
robert_jw 0:b2805b6888dc 1267
robert_jw 0:b2805b6888dc 1268 htonl(addr);
robert_jw 0:b2805b6888dc 1269 if (IP_CLASSA(addr)) { /* determine network mask for address class */
robert_jw 0:b2805b6888dc 1270 nmask = IP_CLASSA_NET;
robert_jw 0:b2805b6888dc 1271 } else if (IP_CLASSB(addr)) {
robert_jw 0:b2805b6888dc 1272 nmask = IP_CLASSB_NET;
robert_jw 0:b2805b6888dc 1273 } else {
robert_jw 0:b2805b6888dc 1274 nmask = IP_CLASSC_NET;
robert_jw 0:b2805b6888dc 1275 }
robert_jw 0:b2805b6888dc 1276
robert_jw 0:b2805b6888dc 1277 /* class D nets are disallowed by bad_ip_adrs */
robert_jw 0:b2805b6888dc 1278 mask = subnetMask | htonl(nmask);
robert_jw 0:b2805b6888dc 1279
robert_jw 0:b2805b6888dc 1280 /* XXX
robert_jw 0:b2805b6888dc 1281 * Scan through the system's network interfaces.
robert_jw 0:b2805b6888dc 1282 * Get each netmask and OR them into our mask.
robert_jw 0:b2805b6888dc 1283 */
robert_jw 0:b2805b6888dc 1284
robert_jw 0:b2805b6888dc 1285 return mask;
robert_jw 0:b2805b6888dc 1286 }
robert_jw 0:b2805b6888dc 1287
robert_jw 0:b2805b6888dc 1288 /*
robert_jw 0:b2805b6888dc 1289 * sifvjcomp - config tcp header compression
robert_jw 0:b2805b6888dc 1290 */
robert_jw 0:b2805b6888dc 1291 int
robert_jw 0:b2805b6888dc 1292 sifvjcomp(int pd, int vjcomp, u8_t cidcomp, u8_t maxcid)
robert_jw 0:b2805b6888dc 1293 {
robert_jw 0:b2805b6888dc 1294 #if PPPOS_SUPPORT && VJ_SUPPORT
robert_jw 0:b2805b6888dc 1295 PPPControl *pc = &pppControl[pd];
robert_jw 0:b2805b6888dc 1296
robert_jw 0:b2805b6888dc 1297 pc->vjEnabled = vjcomp;
robert_jw 0:b2805b6888dc 1298 pc->vjComp.compressSlot = cidcomp;
robert_jw 0:b2805b6888dc 1299 pc->vjComp.maxSlotIndex = maxcid;
robert_jw 0:b2805b6888dc 1300 PPPDEBUG(LOG_INFO, ("sifvjcomp: VJ compress enable=%d slot=%d max slot=%d\n",
robert_jw 0:b2805b6888dc 1301 vjcomp, cidcomp, maxcid));
robert_jw 0:b2805b6888dc 1302 #else /* PPPOS_SUPPORT && VJ_SUPPORT */
robert_jw 0:b2805b6888dc 1303 LWIP_UNUSED_ARG(pd);
robert_jw 0:b2805b6888dc 1304 LWIP_UNUSED_ARG(vjcomp);
robert_jw 0:b2805b6888dc 1305 LWIP_UNUSED_ARG(cidcomp);
robert_jw 0:b2805b6888dc 1306 LWIP_UNUSED_ARG(maxcid);
robert_jw 0:b2805b6888dc 1307 #endif /* PPPOS_SUPPORT && VJ_SUPPORT */
robert_jw 0:b2805b6888dc 1308
robert_jw 0:b2805b6888dc 1309 return 0;
robert_jw 0:b2805b6888dc 1310 }
robert_jw 0:b2805b6888dc 1311
robert_jw 0:b2805b6888dc 1312 /*
robert_jw 0:b2805b6888dc 1313 * pppifNetifInit - netif init callback
robert_jw 0:b2805b6888dc 1314 */
robert_jw 0:b2805b6888dc 1315 static err_t
robert_jw 0:b2805b6888dc 1316 pppifNetifInit(struct netif *netif)
robert_jw 0:b2805b6888dc 1317 {
robert_jw 0:b2805b6888dc 1318 netif->name[0] = 'p';
robert_jw 0:b2805b6888dc 1319 netif->name[1] = 'p';
robert_jw 0:b2805b6888dc 1320 netif->output = pppifOutput;
robert_jw 0:b2805b6888dc 1321 netif->mtu = pppMTU((int)(size_t)netif->state);
robert_jw 0:b2805b6888dc 1322 netif->flags = NETIF_FLAG_POINTTOPOINT | NETIF_FLAG_LINK_UP;
robert_jw 0:b2805b6888dc 1323 #if LWIP_NETIF_HOSTNAME
robert_jw 0:b2805b6888dc 1324 /* @todo: Initialize interface hostname */
robert_jw 0:b2805b6888dc 1325 /* netif_set_hostname(netif, "lwip"); */
robert_jw 0:b2805b6888dc 1326 #endif /* LWIP_NETIF_HOSTNAME */
robert_jw 0:b2805b6888dc 1327 return ERR_OK;
robert_jw 0:b2805b6888dc 1328 }
robert_jw 0:b2805b6888dc 1329
robert_jw 0:b2805b6888dc 1330
robert_jw 0:b2805b6888dc 1331 /*
robert_jw 0:b2805b6888dc 1332 * sifup - Config the interface up and enable IP packets to pass.
robert_jw 0:b2805b6888dc 1333 */
robert_jw 0:b2805b6888dc 1334 int
robert_jw 0:b2805b6888dc 1335 sifup(int pd)
robert_jw 0:b2805b6888dc 1336 {
robert_jw 0:b2805b6888dc 1337 PPPControl *pc = &pppControl[pd];
robert_jw 0:b2805b6888dc 1338 int st = 1;
robert_jw 0:b2805b6888dc 1339
robert_jw 0:b2805b6888dc 1340 if (pd < 0 || pd >= NUM_PPP || !pc->openFlag) {
robert_jw 0:b2805b6888dc 1341 st = 0;
robert_jw 0:b2805b6888dc 1342 PPPDEBUG(LOG_WARNING, ("sifup[%d]: bad parms\n", pd));
robert_jw 0:b2805b6888dc 1343 } else {
robert_jw 0:b2805b6888dc 1344 netif_remove(&pc->netif);
robert_jw 0:b2805b6888dc 1345 if (netif_add(&pc->netif, &pc->addrs.our_ipaddr, &pc->addrs.netmask,
robert_jw 0:b2805b6888dc 1346 &pc->addrs.his_ipaddr, (void *)(size_t)pd, pppifNetifInit, ip_input)) {
robert_jw 0:b2805b6888dc 1347 netif_set_up(&pc->netif);
robert_jw 0:b2805b6888dc 1348 pc->if_up = 1;
robert_jw 0:b2805b6888dc 1349 pc->errCode = PPPERR_NONE;
robert_jw 0:b2805b6888dc 1350
robert_jw 0:b2805b6888dc 1351 PPPDEBUG(LOG_DEBUG, ("sifup: unit %d: linkStatusCB=%p errCode=%d\n", pd, pc->linkStatusCB, pc->errCode));
robert_jw 0:b2805b6888dc 1352 if (pc->linkStatusCB) {
robert_jw 0:b2805b6888dc 1353 pc->linkStatusCB(pc->linkStatusCtx, pc->errCode, &pc->addrs);
robert_jw 0:b2805b6888dc 1354 }
robert_jw 0:b2805b6888dc 1355 } else {
robert_jw 0:b2805b6888dc 1356 st = 0;
robert_jw 0:b2805b6888dc 1357 PPPDEBUG(LOG_ERR, ("sifup[%d]: netif_add failed\n", pd));
robert_jw 0:b2805b6888dc 1358 }
robert_jw 0:b2805b6888dc 1359 }
robert_jw 0:b2805b6888dc 1360
robert_jw 0:b2805b6888dc 1361 return st;
robert_jw 0:b2805b6888dc 1362 }
robert_jw 0:b2805b6888dc 1363
robert_jw 0:b2805b6888dc 1364 /*
robert_jw 0:b2805b6888dc 1365 * sifnpmode - Set the mode for handling packets for a given NP.
robert_jw 0:b2805b6888dc 1366 */
robert_jw 0:b2805b6888dc 1367 int
robert_jw 0:b2805b6888dc 1368 sifnpmode(int u, int proto, enum NPmode mode)
robert_jw 0:b2805b6888dc 1369 {
robert_jw 0:b2805b6888dc 1370 LWIP_UNUSED_ARG(u);
robert_jw 0:b2805b6888dc 1371 LWIP_UNUSED_ARG(proto);
robert_jw 0:b2805b6888dc 1372 LWIP_UNUSED_ARG(mode);
robert_jw 0:b2805b6888dc 1373 return 0;
robert_jw 0:b2805b6888dc 1374 }
robert_jw 0:b2805b6888dc 1375
robert_jw 0:b2805b6888dc 1376 /*
robert_jw 0:b2805b6888dc 1377 * sifdown - Config the interface down and disable IP.
robert_jw 0:b2805b6888dc 1378 */
robert_jw 0:b2805b6888dc 1379 int
robert_jw 0:b2805b6888dc 1380 sifdown(int pd)
robert_jw 0:b2805b6888dc 1381 {
robert_jw 0:b2805b6888dc 1382 PPPControl *pc = &pppControl[pd];
robert_jw 0:b2805b6888dc 1383 int st = 1;
robert_jw 0:b2805b6888dc 1384
robert_jw 0:b2805b6888dc 1385 if (pd < 0 || pd >= NUM_PPP || !pc->openFlag) {
robert_jw 0:b2805b6888dc 1386 st = 0;
robert_jw 0:b2805b6888dc 1387 PPPDEBUG(LOG_WARNING, ("sifdown[%d]: bad parms\n", pd));
robert_jw 0:b2805b6888dc 1388 } else {
robert_jw 0:b2805b6888dc 1389 pc->if_up = 0;
robert_jw 0:b2805b6888dc 1390 /* make sure the netif status callback is called */
robert_jw 0:b2805b6888dc 1391 netif_set_down(&pc->netif);
robert_jw 0:b2805b6888dc 1392 netif_remove(&pc->netif);
robert_jw 0:b2805b6888dc 1393 PPPDEBUG(LOG_DEBUG, ("sifdown: unit %d: linkStatusCB=%p errCode=%d\n", pd, pc->linkStatusCB, pc->errCode));
robert_jw 0:b2805b6888dc 1394 if (pc->linkStatusCB) {
robert_jw 0:b2805b6888dc 1395 pc->linkStatusCB(pc->linkStatusCtx, PPPERR_CONNECT, NULL);
robert_jw 0:b2805b6888dc 1396 }
robert_jw 0:b2805b6888dc 1397 }
robert_jw 0:b2805b6888dc 1398 return st;
robert_jw 0:b2805b6888dc 1399 }
robert_jw 0:b2805b6888dc 1400
robert_jw 0:b2805b6888dc 1401 /**
robert_jw 0:b2805b6888dc 1402 * sifaddr - Config the interface IP addresses and netmask.
robert_jw 0:b2805b6888dc 1403 * @param pd Interface unit ???
robert_jw 0:b2805b6888dc 1404 * @param o Our IP address ???
robert_jw 0:b2805b6888dc 1405 * @param h His IP address ???
robert_jw 0:b2805b6888dc 1406 * @param m IP subnet mask ???
robert_jw 0:b2805b6888dc 1407 * @param ns1 Primary DNS
robert_jw 0:b2805b6888dc 1408 * @param ns2 Secondary DNS
robert_jw 0:b2805b6888dc 1409 */
robert_jw 0:b2805b6888dc 1410 int
robert_jw 0:b2805b6888dc 1411 sifaddr( int pd, u32_t o, u32_t h, u32_t m, u32_t ns1, u32_t ns2)
robert_jw 0:b2805b6888dc 1412 {
robert_jw 0:b2805b6888dc 1413 PPPControl *pc = &pppControl[pd];
robert_jw 0:b2805b6888dc 1414 int st = 1;
robert_jw 0:b2805b6888dc 1415
robert_jw 0:b2805b6888dc 1416 if (pd < 0 || pd >= NUM_PPP || !pc->openFlag) {
robert_jw 0:b2805b6888dc 1417 st = 0;
robert_jw 0:b2805b6888dc 1418 PPPDEBUG(LOG_WARNING, ("sifup[%d]: bad parms\n", pd));
robert_jw 0:b2805b6888dc 1419 } else {
robert_jw 0:b2805b6888dc 1420 SMEMCPY(&pc->addrs.our_ipaddr, &o, sizeof(o));
robert_jw 0:b2805b6888dc 1421 SMEMCPY(&pc->addrs.his_ipaddr, &h, sizeof(h));
robert_jw 0:b2805b6888dc 1422 SMEMCPY(&pc->addrs.netmask, &m, sizeof(m));
robert_jw 0:b2805b6888dc 1423 SMEMCPY(&pc->addrs.dns1, &ns1, sizeof(ns1));
robert_jw 0:b2805b6888dc 1424 SMEMCPY(&pc->addrs.dns2, &ns2, sizeof(ns2));
robert_jw 0:b2805b6888dc 1425 }
robert_jw 0:b2805b6888dc 1426 return st;
robert_jw 0:b2805b6888dc 1427 }
robert_jw 0:b2805b6888dc 1428
robert_jw 0:b2805b6888dc 1429 /**
robert_jw 0:b2805b6888dc 1430 * cifaddr - Clear the interface IP addresses, and delete routes
robert_jw 0:b2805b6888dc 1431 * through the interface if possible.
robert_jw 0:b2805b6888dc 1432 * @param pd Interface unit ???
robert_jw 0:b2805b6888dc 1433 * @param o Our IP address ???
robert_jw 0:b2805b6888dc 1434 * @param h IP broadcast address ???
robert_jw 0:b2805b6888dc 1435 */
robert_jw 0:b2805b6888dc 1436 int
robert_jw 0:b2805b6888dc 1437 cifaddr( int pd, u32_t o, u32_t h)
robert_jw 0:b2805b6888dc 1438 {
robert_jw 0:b2805b6888dc 1439 PPPControl *pc = &pppControl[pd];
robert_jw 0:b2805b6888dc 1440 int st = 1;
robert_jw 0:b2805b6888dc 1441
robert_jw 0:b2805b6888dc 1442 LWIP_UNUSED_ARG(o);
robert_jw 0:b2805b6888dc 1443 LWIP_UNUSED_ARG(h);
robert_jw 0:b2805b6888dc 1444 if (pd < 0 || pd >= NUM_PPP || !pc->openFlag) {
robert_jw 0:b2805b6888dc 1445 st = 0;
robert_jw 0:b2805b6888dc 1446 PPPDEBUG(LOG_WARNING, ("sifup[%d]: bad parms\n", pd));
robert_jw 0:b2805b6888dc 1447 } else {
robert_jw 0:b2805b6888dc 1448 IP4_ADDR(&pc->addrs.our_ipaddr, 0,0,0,0);
robert_jw 0:b2805b6888dc 1449 IP4_ADDR(&pc->addrs.his_ipaddr, 0,0,0,0);
robert_jw 0:b2805b6888dc 1450 IP4_ADDR(&pc->addrs.netmask, 255,255,255,0);
robert_jw 0:b2805b6888dc 1451 IP4_ADDR(&pc->addrs.dns1, 0,0,0,0);
robert_jw 0:b2805b6888dc 1452 IP4_ADDR(&pc->addrs.dns2, 0,0,0,0);
robert_jw 0:b2805b6888dc 1453 }
robert_jw 0:b2805b6888dc 1454 return st;
robert_jw 0:b2805b6888dc 1455 }
robert_jw 0:b2805b6888dc 1456
robert_jw 0:b2805b6888dc 1457 /*
robert_jw 0:b2805b6888dc 1458 * sifdefaultroute - assign a default route through the address given.
robert_jw 0:b2805b6888dc 1459 */
robert_jw 0:b2805b6888dc 1460 int
robert_jw 0:b2805b6888dc 1461 sifdefaultroute(int pd, u32_t l, u32_t g)
robert_jw 0:b2805b6888dc 1462 {
robert_jw 0:b2805b6888dc 1463 PPPControl *pc = &pppControl[pd];
robert_jw 0:b2805b6888dc 1464 int st = 1;
robert_jw 0:b2805b6888dc 1465
robert_jw 0:b2805b6888dc 1466 LWIP_UNUSED_ARG(l);
robert_jw 0:b2805b6888dc 1467 LWIP_UNUSED_ARG(g);
robert_jw 0:b2805b6888dc 1468
robert_jw 0:b2805b6888dc 1469 if (pd < 0 || pd >= NUM_PPP || !pc->openFlag) {
robert_jw 0:b2805b6888dc 1470 st = 0;
robert_jw 0:b2805b6888dc 1471 PPPDEBUG(LOG_WARNING, ("sifup[%d]: bad parms\n", pd));
robert_jw 0:b2805b6888dc 1472 } else {
robert_jw 0:b2805b6888dc 1473 netif_set_default(&pc->netif);
robert_jw 0:b2805b6888dc 1474 }
robert_jw 0:b2805b6888dc 1475
robert_jw 0:b2805b6888dc 1476 /* TODO: check how PPP handled the netMask, previously not set by ipSetDefault */
robert_jw 0:b2805b6888dc 1477
robert_jw 0:b2805b6888dc 1478 return st;
robert_jw 0:b2805b6888dc 1479 }
robert_jw 0:b2805b6888dc 1480
robert_jw 0:b2805b6888dc 1481 /*
robert_jw 0:b2805b6888dc 1482 * cifdefaultroute - delete a default route through the address given.
robert_jw 0:b2805b6888dc 1483 */
robert_jw 0:b2805b6888dc 1484 int
robert_jw 0:b2805b6888dc 1485 cifdefaultroute(int pd, u32_t l, u32_t g)
robert_jw 0:b2805b6888dc 1486 {
robert_jw 0:b2805b6888dc 1487 PPPControl *pc = &pppControl[pd];
robert_jw 0:b2805b6888dc 1488 int st = 1;
robert_jw 0:b2805b6888dc 1489
robert_jw 0:b2805b6888dc 1490 LWIP_UNUSED_ARG(l);
robert_jw 0:b2805b6888dc 1491 LWIP_UNUSED_ARG(g);
robert_jw 0:b2805b6888dc 1492
robert_jw 0:b2805b6888dc 1493 if (pd < 0 || pd >= NUM_PPP || !pc->openFlag) {
robert_jw 0:b2805b6888dc 1494 st = 0;
robert_jw 0:b2805b6888dc 1495 PPPDEBUG(LOG_WARNING, ("sifup[%d]: bad parms\n", pd));
robert_jw 0:b2805b6888dc 1496 } else {
robert_jw 0:b2805b6888dc 1497 netif_set_default(NULL);
robert_jw 0:b2805b6888dc 1498 }
robert_jw 0:b2805b6888dc 1499
robert_jw 0:b2805b6888dc 1500 return st;
robert_jw 0:b2805b6888dc 1501 }
robert_jw 0:b2805b6888dc 1502
robert_jw 0:b2805b6888dc 1503 /**********************************/
robert_jw 0:b2805b6888dc 1504 /*** LOCAL FUNCTION DEFINITIONS ***/
robert_jw 0:b2805b6888dc 1505 /**********************************/
robert_jw 0:b2805b6888dc 1506
robert_jw 0:b2805b6888dc 1507 #if PPPOS_SUPPORT && PPP_INPROC_OWNTHREAD
robert_jw 0:b2805b6888dc 1508 /* The main PPP process function. This implements the state machine according
robert_jw 0:b2805b6888dc 1509 * to section 4 of RFC 1661: The Point-To-Point Protocol. */
robert_jw 0:b2805b6888dc 1510 static void
robert_jw 0:b2805b6888dc 1511 pppInputThread(void *arg)
robert_jw 0:b2805b6888dc 1512 {
robert_jw 0:b2805b6888dc 1513 int count;
robert_jw 0:b2805b6888dc 1514 PPPControlRx *pcrx = arg;
robert_jw 0:b2805b6888dc 1515
robert_jw 0:b2805b6888dc 1516 do
robert_jw 0:b2805b6888dc 1517 {
robert_jw 0:b2805b6888dc 1518 sys_arch_mbox_fetch(&pppMbox, (void**)&pcrx, 0); //Wait indefinitely
robert_jw 0:b2805b6888dc 1519
robert_jw 0:b2805b6888dc 1520 while (lcp_phase[pcrx->pd] != PHASE_DEAD) {
robert_jw 0:b2805b6888dc 1521 count = sio_read(pcrx->fd, pcrx->rxbuf, PPPOS_RX_BUFSIZE);
robert_jw 0:b2805b6888dc 1522 if(count > 0) {
robert_jw 0:b2805b6888dc 1523 pppInProc(pcrx, pcrx->rxbuf, count);
robert_jw 0:b2805b6888dc 1524 } else {
robert_jw 0:b2805b6888dc 1525 /* nothing received, give other tasks a chance to run */
robert_jw 0:b2805b6888dc 1526 sys_msleep(1);
robert_jw 0:b2805b6888dc 1527 }
robert_jw 0:b2805b6888dc 1528 }
robert_jw 0:b2805b6888dc 1529 } while(1); //Never terminates
robert_jw 0:b2805b6888dc 1530 }
robert_jw 0:b2805b6888dc 1531 #endif /* PPPOS_SUPPORT && PPP_INPROC_OWNTHREAD */
robert_jw 0:b2805b6888dc 1532
robert_jw 0:b2805b6888dc 1533 #if PPPOE_SUPPORT
robert_jw 0:b2805b6888dc 1534
robert_jw 0:b2805b6888dc 1535 void
robert_jw 0:b2805b6888dc 1536 pppOverEthernetInitFailed(int pd)
robert_jw 0:b2805b6888dc 1537 {
robert_jw 0:b2805b6888dc 1538 PPPControl* pc;
robert_jw 0:b2805b6888dc 1539
robert_jw 0:b2805b6888dc 1540 pppHup(pd);
robert_jw 0:b2805b6888dc 1541 pppStop(pd);
robert_jw 0:b2805b6888dc 1542
robert_jw 0:b2805b6888dc 1543 pc = &pppControl[pd];
robert_jw 0:b2805b6888dc 1544 pppoe_destroy(&pc->netif);
robert_jw 0:b2805b6888dc 1545 pc->openFlag = 0;
robert_jw 0:b2805b6888dc 1546
robert_jw 0:b2805b6888dc 1547 if(pc->linkStatusCB) {
robert_jw 0:b2805b6888dc 1548 pc->linkStatusCB(pc->linkStatusCtx, pc->errCode ? pc->errCode : PPPERR_PROTOCOL, NULL);
robert_jw 0:b2805b6888dc 1549 }
robert_jw 0:b2805b6888dc 1550 }
robert_jw 0:b2805b6888dc 1551
robert_jw 0:b2805b6888dc 1552 static void
robert_jw 0:b2805b6888dc 1553 pppOverEthernetLinkStatusCB(int pd, int up)
robert_jw 0:b2805b6888dc 1554 {
robert_jw 0:b2805b6888dc 1555 if(up) {
robert_jw 0:b2805b6888dc 1556 PPPDEBUG(LOG_INFO, ("pppOverEthernetLinkStatusCB: unit %d: Connecting\n", pd));
robert_jw 0:b2805b6888dc 1557 pppStart(pd);
robert_jw 0:b2805b6888dc 1558 } else {
robert_jw 0:b2805b6888dc 1559 pppOverEthernetInitFailed(pd);
robert_jw 0:b2805b6888dc 1560 }
robert_jw 0:b2805b6888dc 1561 }
robert_jw 0:b2805b6888dc 1562 #endif /* PPPOE_SUPPORT */
robert_jw 0:b2805b6888dc 1563
robert_jw 0:b2805b6888dc 1564 struct pbuf *
robert_jw 0:b2805b6888dc 1565 pppSingleBuf(struct pbuf *p)
robert_jw 0:b2805b6888dc 1566 {
robert_jw 0:b2805b6888dc 1567 struct pbuf *q, *b;
robert_jw 0:b2805b6888dc 1568 u_char *pl;
robert_jw 0:b2805b6888dc 1569
robert_jw 0:b2805b6888dc 1570 if(p->tot_len == p->len) {
robert_jw 0:b2805b6888dc 1571 return p;
robert_jw 0:b2805b6888dc 1572 }
robert_jw 0:b2805b6888dc 1573
robert_jw 0:b2805b6888dc 1574 q = pbuf_alloc(PBUF_RAW, p->tot_len, PBUF_RAM);
robert_jw 0:b2805b6888dc 1575 if(!q) {
robert_jw 0:b2805b6888dc 1576 PPPDEBUG(LOG_ERR,
robert_jw 0:b2805b6888dc 1577 ("pppSingleBuf: unable to alloc new buf (%d)\n", p->tot_len));
robert_jw 0:b2805b6888dc 1578 return p; /* live dangerously */
robert_jw 0:b2805b6888dc 1579 }
robert_jw 0:b2805b6888dc 1580
robert_jw 0:b2805b6888dc 1581 for(b = p, pl = q->payload; b != NULL; b = b->next) {
robert_jw 0:b2805b6888dc 1582 MEMCPY(pl, b->payload, b->len);
robert_jw 0:b2805b6888dc 1583 pl += b->len;
robert_jw 0:b2805b6888dc 1584 }
robert_jw 0:b2805b6888dc 1585
robert_jw 0:b2805b6888dc 1586 pbuf_free(p);
robert_jw 0:b2805b6888dc 1587
robert_jw 0:b2805b6888dc 1588 return q;
robert_jw 0:b2805b6888dc 1589 }
robert_jw 0:b2805b6888dc 1590
robert_jw 0:b2805b6888dc 1591 struct pppInputHeader {
robert_jw 0:b2805b6888dc 1592 int unit;
robert_jw 0:b2805b6888dc 1593 u16_t proto;
robert_jw 0:b2805b6888dc 1594 };
robert_jw 0:b2805b6888dc 1595
robert_jw 0:b2805b6888dc 1596 /*
robert_jw 0:b2805b6888dc 1597 * Pass the processed input packet to the appropriate handler.
robert_jw 0:b2805b6888dc 1598 * This function and all handlers run in the context of the tcpip_thread
robert_jw 0:b2805b6888dc 1599 */
robert_jw 0:b2805b6888dc 1600 static void
robert_jw 0:b2805b6888dc 1601 pppInput(void *arg)
robert_jw 0:b2805b6888dc 1602 {
robert_jw 0:b2805b6888dc 1603 struct pbuf *nb = (struct pbuf *)arg;
robert_jw 0:b2805b6888dc 1604 u16_t protocol;
robert_jw 0:b2805b6888dc 1605 int pd;
robert_jw 0:b2805b6888dc 1606
robert_jw 0:b2805b6888dc 1607 pd = ((struct pppInputHeader *)nb->payload)->unit;
robert_jw 0:b2805b6888dc 1608 protocol = ((struct pppInputHeader *)nb->payload)->proto;
robert_jw 0:b2805b6888dc 1609
robert_jw 0:b2805b6888dc 1610 if(pbuf_header(nb, -(int)sizeof(struct pppInputHeader))) {
robert_jw 0:b2805b6888dc 1611 LWIP_ASSERT("pbuf_header failed\n", 0);
robert_jw 0:b2805b6888dc 1612 goto drop;
robert_jw 0:b2805b6888dc 1613 }
robert_jw 0:b2805b6888dc 1614
robert_jw 0:b2805b6888dc 1615 LINK_STATS_INC(link.recv);
robert_jw 0:b2805b6888dc 1616 snmp_inc_ifinucastpkts(&pppControl[pd].netif);
robert_jw 0:b2805b6888dc 1617 snmp_add_ifinoctets(&pppControl[pd].netif, nb->tot_len);
robert_jw 0:b2805b6888dc 1618
robert_jw 0:b2805b6888dc 1619 /*
robert_jw 0:b2805b6888dc 1620 * Toss all non-LCP packets unless LCP is OPEN.
robert_jw 0:b2805b6888dc 1621 * Until we get past the authentication phase, toss all packets
robert_jw 0:b2805b6888dc 1622 * except LCP, LQR and authentication packets.
robert_jw 0:b2805b6888dc 1623 */
robert_jw 0:b2805b6888dc 1624 if((lcp_phase[pd] <= PHASE_AUTHENTICATE) && (protocol != PPP_LCP)) {
robert_jw 0:b2805b6888dc 1625 if(!((protocol == PPP_LQR) || (protocol == PPP_PAP) || (protocol == PPP_CHAP)) ||
robert_jw 0:b2805b6888dc 1626 (lcp_phase[pd] != PHASE_AUTHENTICATE)) {
robert_jw 0:b2805b6888dc 1627 PPPDEBUG(LOG_INFO, ("pppInput: discarding proto 0x%"X16_F" in phase %d\n", protocol, lcp_phase[pd]));
robert_jw 0:b2805b6888dc 1628 goto drop;
robert_jw 0:b2805b6888dc 1629 }
robert_jw 0:b2805b6888dc 1630 }
robert_jw 0:b2805b6888dc 1631
robert_jw 0:b2805b6888dc 1632 switch(protocol) {
robert_jw 0:b2805b6888dc 1633 case PPP_VJC_COMP: /* VJ compressed TCP */
robert_jw 0:b2805b6888dc 1634 #if PPPOS_SUPPORT && VJ_SUPPORT
robert_jw 0:b2805b6888dc 1635 PPPDEBUG(LOG_INFO, ("pppInput[%d]: vj_comp in pbuf len=%d\n", pd, nb->len));
robert_jw 0:b2805b6888dc 1636 /*
robert_jw 0:b2805b6888dc 1637 * Clip off the VJ header and prepend the rebuilt TCP/IP header and
robert_jw 0:b2805b6888dc 1638 * pass the result to IP.
robert_jw 0:b2805b6888dc 1639 */
robert_jw 0:b2805b6888dc 1640 if ((vj_uncompress_tcp(&nb, &pppControl[pd].vjComp) >= 0) && (pppControl[pd].netif.input)) {
robert_jw 0:b2805b6888dc 1641 pppControl[pd].netif.input(nb, &pppControl[pd].netif);
robert_jw 0:b2805b6888dc 1642 return;
robert_jw 0:b2805b6888dc 1643 }
robert_jw 0:b2805b6888dc 1644 /* Something's wrong so drop it. */
robert_jw 0:b2805b6888dc 1645 PPPDEBUG(LOG_WARNING, ("pppInput[%d]: Dropping VJ compressed\n", pd));
robert_jw 0:b2805b6888dc 1646 #else /* PPPOS_SUPPORT && VJ_SUPPORT */
robert_jw 0:b2805b6888dc 1647 /* No handler for this protocol so drop the packet. */
robert_jw 0:b2805b6888dc 1648 PPPDEBUG(LOG_INFO, ("pppInput[%d]: drop VJ Comp in %d:%s\n", pd, nb->len, nb->payload));
robert_jw 0:b2805b6888dc 1649 #endif /* PPPOS_SUPPORT && VJ_SUPPORT */
robert_jw 0:b2805b6888dc 1650 break;
robert_jw 0:b2805b6888dc 1651
robert_jw 0:b2805b6888dc 1652 case PPP_VJC_UNCOMP: /* VJ uncompressed TCP */
robert_jw 0:b2805b6888dc 1653 #if PPPOS_SUPPORT && VJ_SUPPORT
robert_jw 0:b2805b6888dc 1654 PPPDEBUG(LOG_INFO, ("pppInput[%d]: vj_un in pbuf len=%d\n", pd, nb->len));
robert_jw 0:b2805b6888dc 1655 /*
robert_jw 0:b2805b6888dc 1656 * Process the TCP/IP header for VJ header compression and then pass
robert_jw 0:b2805b6888dc 1657 * the packet to IP.
robert_jw 0:b2805b6888dc 1658 */
robert_jw 0:b2805b6888dc 1659 if ((vj_uncompress_uncomp(nb, &pppControl[pd].vjComp) >= 0) && pppControl[pd].netif.input) {
robert_jw 0:b2805b6888dc 1660 pppControl[pd].netif.input(nb, &pppControl[pd].netif);
robert_jw 0:b2805b6888dc 1661 return;
robert_jw 0:b2805b6888dc 1662 }
robert_jw 0:b2805b6888dc 1663 /* Something's wrong so drop it. */
robert_jw 0:b2805b6888dc 1664 PPPDEBUG(LOG_WARNING, ("pppInput[%d]: Dropping VJ uncompressed\n", pd));
robert_jw 0:b2805b6888dc 1665 #else /* PPPOS_SUPPORT && VJ_SUPPORT */
robert_jw 0:b2805b6888dc 1666 /* No handler for this protocol so drop the packet. */
robert_jw 0:b2805b6888dc 1667 PPPDEBUG(LOG_INFO,
robert_jw 0:b2805b6888dc 1668 ("pppInput[%d]: drop VJ UnComp in %d:.*H\n",
robert_jw 0:b2805b6888dc 1669 pd, nb->len, LWIP_MIN(nb->len * 2, 40), nb->payload));
robert_jw 0:b2805b6888dc 1670 #endif /* PPPOS_SUPPORT && VJ_SUPPORT */
robert_jw 0:b2805b6888dc 1671 break;
robert_jw 0:b2805b6888dc 1672
robert_jw 0:b2805b6888dc 1673 case PPP_IP: /* Internet Protocol */
robert_jw 0:b2805b6888dc 1674 PPPDEBUG(LOG_INFO, ("pppInput[%d]: ip in pbuf len=%d\n", pd, nb->len));
robert_jw 0:b2805b6888dc 1675 if (pppControl[pd].netif.input) {
robert_jw 0:b2805b6888dc 1676 pppControl[pd].netif.input(nb, &pppControl[pd].netif);
robert_jw 0:b2805b6888dc 1677 return;
robert_jw 0:b2805b6888dc 1678 }
robert_jw 0:b2805b6888dc 1679 break;
robert_jw 0:b2805b6888dc 1680
robert_jw 0:b2805b6888dc 1681 default: {
robert_jw 0:b2805b6888dc 1682 struct protent *protp;
robert_jw 0:b2805b6888dc 1683 int i;
robert_jw 0:b2805b6888dc 1684
robert_jw 0:b2805b6888dc 1685 /*
robert_jw 0:b2805b6888dc 1686 * Upcall the proper protocol input routine.
robert_jw 0:b2805b6888dc 1687 */
robert_jw 0:b2805b6888dc 1688 for (i = 0; (protp = ppp_protocols[i]) != NULL; ++i) {
robert_jw 0:b2805b6888dc 1689 if (protp->protocol == protocol && protp->enabled_flag) {
robert_jw 0:b2805b6888dc 1690 PPPDEBUG(LOG_INFO, ("pppInput[%d]: %s len=%d\n", pd, protp->name, nb->len));
robert_jw 0:b2805b6888dc 1691 nb = pppSingleBuf(nb);
robert_jw 0:b2805b6888dc 1692 (*protp->input)(pd, nb->payload, nb->len);
robert_jw 0:b2805b6888dc 1693 PPPDEBUG(LOG_DETAIL, ("pppInput[%d]: packet processed\n", pd));
robert_jw 0:b2805b6888dc 1694 goto out;
robert_jw 0:b2805b6888dc 1695 }
robert_jw 0:b2805b6888dc 1696 }
robert_jw 0:b2805b6888dc 1697
robert_jw 0:b2805b6888dc 1698 /* No handler for this protocol so reject the packet. */
robert_jw 0:b2805b6888dc 1699 PPPDEBUG(LOG_INFO, ("pppInput[%d]: rejecting unsupported proto 0x%"X16_F" len=%d\n", pd, protocol, nb->len));
robert_jw 0:b2805b6888dc 1700 if (pbuf_header(nb, sizeof(protocol))) {
robert_jw 0:b2805b6888dc 1701 LWIP_ASSERT("pbuf_header failed\n", 0);
robert_jw 0:b2805b6888dc 1702 goto drop;
robert_jw 0:b2805b6888dc 1703 }
robert_jw 0:b2805b6888dc 1704 #if BYTE_ORDER == LITTLE_ENDIAN
robert_jw 0:b2805b6888dc 1705 protocol = htons(protocol);
robert_jw 0:b2805b6888dc 1706 #endif /* BYTE_ORDER == LITTLE_ENDIAN */
robert_jw 0:b2805b6888dc 1707 SMEMCPY(nb->payload, &protocol, sizeof(protocol));
robert_jw 0:b2805b6888dc 1708 lcp_sprotrej(pd, nb->payload, nb->len);
robert_jw 0:b2805b6888dc 1709 }
robert_jw 0:b2805b6888dc 1710 break;
robert_jw 0:b2805b6888dc 1711 }
robert_jw 0:b2805b6888dc 1712
robert_jw 0:b2805b6888dc 1713 drop:
robert_jw 0:b2805b6888dc 1714 LINK_STATS_INC(link.drop);
robert_jw 0:b2805b6888dc 1715 snmp_inc_ifindiscards(&pppControl[pd].netif);
robert_jw 0:b2805b6888dc 1716
robert_jw 0:b2805b6888dc 1717 out:
robert_jw 0:b2805b6888dc 1718 pbuf_free(nb);
robert_jw 0:b2805b6888dc 1719 return;
robert_jw 0:b2805b6888dc 1720 }
robert_jw 0:b2805b6888dc 1721
robert_jw 0:b2805b6888dc 1722 #if PPPOS_SUPPORT
robert_jw 0:b2805b6888dc 1723 /*
robert_jw 0:b2805b6888dc 1724 * Drop the input packet.
robert_jw 0:b2805b6888dc 1725 */
robert_jw 0:b2805b6888dc 1726 static void
robert_jw 0:b2805b6888dc 1727 pppDrop(PPPControlRx *pcrx)
robert_jw 0:b2805b6888dc 1728 {
robert_jw 0:b2805b6888dc 1729 if (pcrx->inHead != NULL) {
robert_jw 0:b2805b6888dc 1730 #if 0
robert_jw 0:b2805b6888dc 1731 PPPDEBUG(LOG_INFO, ("pppDrop: %d:%.*H\n", pcrx->inHead->len, min(60, pcrx->inHead->len * 2), pcrx->inHead->payload));
robert_jw 0:b2805b6888dc 1732 #endif
robert_jw 0:b2805b6888dc 1733 PPPDEBUG(LOG_INFO, ("pppDrop: pbuf len=%d, addr %p\n", pcrx->inHead->len, (void*)pcrx->inHead));
robert_jw 0:b2805b6888dc 1734 if (pcrx->inTail && (pcrx->inTail != pcrx->inHead)) {
robert_jw 0:b2805b6888dc 1735 pbuf_free(pcrx->inTail);
robert_jw 0:b2805b6888dc 1736 }
robert_jw 0:b2805b6888dc 1737 pbuf_free(pcrx->inHead);
robert_jw 0:b2805b6888dc 1738 pcrx->inHead = NULL;
robert_jw 0:b2805b6888dc 1739 pcrx->inTail = NULL;
robert_jw 0:b2805b6888dc 1740 }
robert_jw 0:b2805b6888dc 1741 #if VJ_SUPPORT
robert_jw 0:b2805b6888dc 1742 vj_uncompress_err(&pppControl[pcrx->pd].vjComp);
robert_jw 0:b2805b6888dc 1743 #endif /* VJ_SUPPORT */
robert_jw 0:b2805b6888dc 1744
robert_jw 0:b2805b6888dc 1745 LINK_STATS_INC(link.drop);
robert_jw 0:b2805b6888dc 1746 snmp_inc_ifindiscards(&pppControl[pcrx->pd].netif);
robert_jw 0:b2805b6888dc 1747 }
robert_jw 0:b2805b6888dc 1748
robert_jw 0:b2805b6888dc 1749 /** Pass received raw characters to PPPoS to be decoded. This function is
robert_jw 0:b2805b6888dc 1750 * thread-safe and can be called from a dedicated RX-thread or from a main-loop.
robert_jw 0:b2805b6888dc 1751 *
robert_jw 0:b2805b6888dc 1752 * @param pd PPP descriptor index, returned by pppOpen()
robert_jw 0:b2805b6888dc 1753 * @param data received data
robert_jw 0:b2805b6888dc 1754 * @param len length of received data
robert_jw 0:b2805b6888dc 1755 */
robert_jw 0:b2805b6888dc 1756 void
robert_jw 0:b2805b6888dc 1757 pppos_input(int pd, u_char* data, int len)
robert_jw 0:b2805b6888dc 1758 {
robert_jw 0:b2805b6888dc 1759 pppInProc(&pppControl[pd].rx, data, len);
robert_jw 0:b2805b6888dc 1760 }
robert_jw 0:b2805b6888dc 1761
robert_jw 0:b2805b6888dc 1762 /**
robert_jw 0:b2805b6888dc 1763 * Process a received octet string.
robert_jw 0:b2805b6888dc 1764 */
robert_jw 0:b2805b6888dc 1765 static void
robert_jw 0:b2805b6888dc 1766 pppInProc(PPPControlRx *pcrx, u_char *s, int l)
robert_jw 0:b2805b6888dc 1767 {
robert_jw 0:b2805b6888dc 1768 struct pbuf *nextNBuf;
robert_jw 0:b2805b6888dc 1769 u_char curChar;
robert_jw 0:b2805b6888dc 1770 u_char escaped;
robert_jw 0:b2805b6888dc 1771 SYS_ARCH_DECL_PROTECT(lev);
robert_jw 0:b2805b6888dc 1772
robert_jw 0:b2805b6888dc 1773 PPPDEBUG(LOG_DEBUG, ("pppInProc[%d]: got %d bytes\n", pcrx->pd, l));
robert_jw 0:b2805b6888dc 1774 while (l-- > 0) {
robert_jw 0:b2805b6888dc 1775 curChar = *s++;
robert_jw 0:b2805b6888dc 1776
robert_jw 0:b2805b6888dc 1777 SYS_ARCH_PROTECT(lev);
robert_jw 0:b2805b6888dc 1778 escaped = ESCAPE_P(pcrx->inACCM, curChar);
robert_jw 0:b2805b6888dc 1779 SYS_ARCH_UNPROTECT(lev);
robert_jw 0:b2805b6888dc 1780 /* Handle special characters. */
robert_jw 0:b2805b6888dc 1781 if (escaped) {
robert_jw 0:b2805b6888dc 1782 /* Check for escape sequences. */
robert_jw 0:b2805b6888dc 1783 /* XXX Note that this does not handle an escaped 0x5d character which
robert_jw 0:b2805b6888dc 1784 * would appear as an escape character. Since this is an ASCII ']'
robert_jw 0:b2805b6888dc 1785 * and there is no reason that I know of to escape it, I won't complicate
robert_jw 0:b2805b6888dc 1786 * the code to handle this case. GLL */
robert_jw 0:b2805b6888dc 1787 if (curChar == PPP_ESCAPE) {
robert_jw 0:b2805b6888dc 1788 pcrx->inEscaped = 1;
robert_jw 0:b2805b6888dc 1789 /* Check for the flag character. */
robert_jw 0:b2805b6888dc 1790 } else if (curChar == PPP_FLAG) {
robert_jw 0:b2805b6888dc 1791 /* If this is just an extra flag character, ignore it. */
robert_jw 0:b2805b6888dc 1792 if (pcrx->inState <= PDADDRESS) {
robert_jw 0:b2805b6888dc 1793 /* ignore it */;
robert_jw 0:b2805b6888dc 1794 /* If we haven't received the packet header, drop what has come in. */
robert_jw 0:b2805b6888dc 1795 } else if (pcrx->inState < PDDATA) {
robert_jw 0:b2805b6888dc 1796 PPPDEBUG(LOG_WARNING,
robert_jw 0:b2805b6888dc 1797 ("pppInProc[%d]: Dropping incomplete packet %d\n",
robert_jw 0:b2805b6888dc 1798 pcrx->pd, pcrx->inState));
robert_jw 0:b2805b6888dc 1799 LINK_STATS_INC(link.lenerr);
robert_jw 0:b2805b6888dc 1800 pppDrop(pcrx);
robert_jw 0:b2805b6888dc 1801 /* If the fcs is invalid, drop the packet. */
robert_jw 0:b2805b6888dc 1802 } else if (pcrx->inFCS != PPP_GOODFCS) {
robert_jw 0:b2805b6888dc 1803 PPPDEBUG(LOG_INFO,
robert_jw 0:b2805b6888dc 1804 ("pppInProc[%d]: Dropping bad fcs 0x%"X16_F" proto=0x%"X16_F"\n",
robert_jw 0:b2805b6888dc 1805 pcrx->pd, pcrx->inFCS, pcrx->inProtocol));
robert_jw 0:b2805b6888dc 1806 /* Note: If you get lots of these, check for UART frame errors or try different baud rate */
robert_jw 0:b2805b6888dc 1807 LINK_STATS_INC(link.chkerr);
robert_jw 0:b2805b6888dc 1808 pppDrop(pcrx);
robert_jw 0:b2805b6888dc 1809 /* Otherwise it's a good packet so pass it on. */
robert_jw 0:b2805b6888dc 1810 } else {
robert_jw 0:b2805b6888dc 1811 struct pbuf *inp;
robert_jw 0:b2805b6888dc 1812 /* Trim off the checksum. */
robert_jw 0:b2805b6888dc 1813 if(pcrx->inTail->len >= 2) {
robert_jw 0:b2805b6888dc 1814 pcrx->inTail->len -= 2;
robert_jw 0:b2805b6888dc 1815
robert_jw 0:b2805b6888dc 1816 pcrx->inTail->tot_len = pcrx->inTail->len;
robert_jw 0:b2805b6888dc 1817 if (pcrx->inTail != pcrx->inHead) {
robert_jw 0:b2805b6888dc 1818 pbuf_cat(pcrx->inHead, pcrx->inTail);
robert_jw 0:b2805b6888dc 1819 }
robert_jw 0:b2805b6888dc 1820 } else {
robert_jw 0:b2805b6888dc 1821 pcrx->inTail->tot_len = pcrx->inTail->len;
robert_jw 0:b2805b6888dc 1822 if (pcrx->inTail != pcrx->inHead) {
robert_jw 0:b2805b6888dc 1823 pbuf_cat(pcrx->inHead, pcrx->inTail);
robert_jw 0:b2805b6888dc 1824 }
robert_jw 0:b2805b6888dc 1825
robert_jw 0:b2805b6888dc 1826 pbuf_realloc(pcrx->inHead, pcrx->inHead->tot_len - 2);
robert_jw 0:b2805b6888dc 1827 }
robert_jw 0:b2805b6888dc 1828
robert_jw 0:b2805b6888dc 1829 /* Dispatch the packet thereby consuming it. */
robert_jw 0:b2805b6888dc 1830 inp = pcrx->inHead;
robert_jw 0:b2805b6888dc 1831 /* Packet consumed, release our references. */
robert_jw 0:b2805b6888dc 1832 pcrx->inHead = NULL;
robert_jw 0:b2805b6888dc 1833 pcrx->inTail = NULL;
robert_jw 0:b2805b6888dc 1834 #if PPP_INPROC_MULTITHREADED
robert_jw 0:b2805b6888dc 1835 if(tcpip_callback_with_block(pppInput, inp, 0) != ERR_OK) {
robert_jw 0:b2805b6888dc 1836 PPPDEBUG(LOG_ERR, ("pppInProc[%d]: tcpip_callback() failed, dropping packet\n", pcrx->pd));
robert_jw 0:b2805b6888dc 1837 pbuf_free(inp);
robert_jw 0:b2805b6888dc 1838 LINK_STATS_INC(link.drop);
robert_jw 0:b2805b6888dc 1839 snmp_inc_ifindiscards(&pppControl[pcrx->pd].netif);
robert_jw 0:b2805b6888dc 1840 }
robert_jw 0:b2805b6888dc 1841 #else /* PPP_INPROC_MULTITHREADED */
robert_jw 0:b2805b6888dc 1842 pppInput(inp);
robert_jw 0:b2805b6888dc 1843 #endif /* PPP_INPROC_MULTITHREADED */
robert_jw 0:b2805b6888dc 1844 }
robert_jw 0:b2805b6888dc 1845
robert_jw 0:b2805b6888dc 1846 /* Prepare for a new packet. */
robert_jw 0:b2805b6888dc 1847 pcrx->inFCS = PPP_INITFCS;
robert_jw 0:b2805b6888dc 1848 pcrx->inState = PDADDRESS;
robert_jw 0:b2805b6888dc 1849 pcrx->inEscaped = 0;
robert_jw 0:b2805b6888dc 1850 /* Other characters are usually control characters that may have
robert_jw 0:b2805b6888dc 1851 * been inserted by the physical layer so here we just drop them. */
robert_jw 0:b2805b6888dc 1852 } else {
robert_jw 0:b2805b6888dc 1853 PPPDEBUG(LOG_WARNING,
robert_jw 0:b2805b6888dc 1854 ("pppInProc[%d]: Dropping ACCM char <%d>\n", pcrx->pd, curChar));
robert_jw 0:b2805b6888dc 1855 }
robert_jw 0:b2805b6888dc 1856 /* Process other characters. */
robert_jw 0:b2805b6888dc 1857 } else {
robert_jw 0:b2805b6888dc 1858 /* Unencode escaped characters. */
robert_jw 0:b2805b6888dc 1859 if (pcrx->inEscaped) {
robert_jw 0:b2805b6888dc 1860 pcrx->inEscaped = 0;
robert_jw 0:b2805b6888dc 1861 curChar ^= PPP_TRANS;
robert_jw 0:b2805b6888dc 1862 }
robert_jw 0:b2805b6888dc 1863
robert_jw 0:b2805b6888dc 1864 /* Process character relative to current state. */
robert_jw 0:b2805b6888dc 1865 switch(pcrx->inState) {
robert_jw 0:b2805b6888dc 1866 case PDIDLE: /* Idle state - waiting. */
robert_jw 0:b2805b6888dc 1867 /* Drop the character if it's not 0xff
robert_jw 0:b2805b6888dc 1868 * we would have processed a flag character above. */
robert_jw 0:b2805b6888dc 1869 if (curChar != PPP_ALLSTATIONS) {
robert_jw 0:b2805b6888dc 1870 break;
robert_jw 0:b2805b6888dc 1871 }
robert_jw 0:b2805b6888dc 1872
robert_jw 0:b2805b6888dc 1873 /* Fall through */
robert_jw 0:b2805b6888dc 1874 case PDSTART: /* Process start flag. */
robert_jw 0:b2805b6888dc 1875 /* Prepare for a new packet. */
robert_jw 0:b2805b6888dc 1876 pcrx->inFCS = PPP_INITFCS;
robert_jw 0:b2805b6888dc 1877
robert_jw 0:b2805b6888dc 1878 /* Fall through */
robert_jw 0:b2805b6888dc 1879 case PDADDRESS: /* Process address field. */
robert_jw 0:b2805b6888dc 1880 if (curChar == PPP_ALLSTATIONS) {
robert_jw 0:b2805b6888dc 1881 pcrx->inState = PDCONTROL;
robert_jw 0:b2805b6888dc 1882 break;
robert_jw 0:b2805b6888dc 1883 }
robert_jw 0:b2805b6888dc 1884 /* Else assume compressed address and control fields so
robert_jw 0:b2805b6888dc 1885 * fall through to get the protocol... */
robert_jw 0:b2805b6888dc 1886 case PDCONTROL: /* Process control field. */
robert_jw 0:b2805b6888dc 1887 /* If we don't get a valid control code, restart. */
robert_jw 0:b2805b6888dc 1888 if (curChar == PPP_UI) {
robert_jw 0:b2805b6888dc 1889 pcrx->inState = PDPROTOCOL1;
robert_jw 0:b2805b6888dc 1890 break;
robert_jw 0:b2805b6888dc 1891 }
robert_jw 0:b2805b6888dc 1892 #if 0
robert_jw 0:b2805b6888dc 1893 else {
robert_jw 0:b2805b6888dc 1894 PPPDEBUG(LOG_WARNING,
robert_jw 0:b2805b6888dc 1895 ("pppInProc[%d]: Invalid control <%d>\n", pcrx->pd, curChar));
robert_jw 0:b2805b6888dc 1896 pcrx->inState = PDSTART;
robert_jw 0:b2805b6888dc 1897 }
robert_jw 0:b2805b6888dc 1898 #endif
robert_jw 0:b2805b6888dc 1899 case PDPROTOCOL1: /* Process protocol field 1. */
robert_jw 0:b2805b6888dc 1900 /* If the lower bit is set, this is the end of the protocol
robert_jw 0:b2805b6888dc 1901 * field. */
robert_jw 0:b2805b6888dc 1902 if (curChar & 1) {
robert_jw 0:b2805b6888dc 1903 pcrx->inProtocol = curChar;
robert_jw 0:b2805b6888dc 1904 pcrx->inState = PDDATA;
robert_jw 0:b2805b6888dc 1905 } else {
robert_jw 0:b2805b6888dc 1906 pcrx->inProtocol = (u_int)curChar << 8;
robert_jw 0:b2805b6888dc 1907 pcrx->inState = PDPROTOCOL2;
robert_jw 0:b2805b6888dc 1908 }
robert_jw 0:b2805b6888dc 1909 break;
robert_jw 0:b2805b6888dc 1910 case PDPROTOCOL2: /* Process protocol field 2. */
robert_jw 0:b2805b6888dc 1911 pcrx->inProtocol |= curChar;
robert_jw 0:b2805b6888dc 1912 pcrx->inState = PDDATA;
robert_jw 0:b2805b6888dc 1913 break;
robert_jw 0:b2805b6888dc 1914 case PDDATA: /* Process data byte. */
robert_jw 0:b2805b6888dc 1915 /* Make space to receive processed data. */
robert_jw 0:b2805b6888dc 1916 if (pcrx->inTail == NULL || pcrx->inTail->len == PBUF_POOL_BUFSIZE) {
robert_jw 0:b2805b6888dc 1917 if (pcrx->inTail != NULL) {
robert_jw 0:b2805b6888dc 1918 pcrx->inTail->tot_len = pcrx->inTail->len;
robert_jw 0:b2805b6888dc 1919 if (pcrx->inTail != pcrx->inHead) {
robert_jw 0:b2805b6888dc 1920 pbuf_cat(pcrx->inHead, pcrx->inTail);
robert_jw 0:b2805b6888dc 1921 /* give up the inTail reference now */
robert_jw 0:b2805b6888dc 1922 pcrx->inTail = NULL;
robert_jw 0:b2805b6888dc 1923 }
robert_jw 0:b2805b6888dc 1924 }
robert_jw 0:b2805b6888dc 1925 /* If we haven't started a packet, we need a packet header. */
robert_jw 0:b2805b6888dc 1926 nextNBuf = pbuf_alloc(PBUF_RAW, 0, PBUF_POOL);
robert_jw 0:b2805b6888dc 1927 if (nextNBuf == NULL) {
robert_jw 0:b2805b6888dc 1928 /* No free buffers. Drop the input packet and let the
robert_jw 0:b2805b6888dc 1929 * higher layers deal with it. Continue processing
robert_jw 0:b2805b6888dc 1930 * the received pbuf chain in case a new packet starts. */
robert_jw 0:b2805b6888dc 1931 PPPDEBUG(LOG_ERR, ("pppInProc[%d]: NO FREE MBUFS!\n", pcrx->pd));
robert_jw 0:b2805b6888dc 1932 LINK_STATS_INC(link.memerr);
robert_jw 0:b2805b6888dc 1933 pppDrop(pcrx);
robert_jw 0:b2805b6888dc 1934 pcrx->inState = PDSTART; /* Wait for flag sequence. */
robert_jw 0:b2805b6888dc 1935 break;
robert_jw 0:b2805b6888dc 1936 }
robert_jw 0:b2805b6888dc 1937 if (pcrx->inHead == NULL) {
robert_jw 0:b2805b6888dc 1938 struct pppInputHeader *pih = nextNBuf->payload;
robert_jw 0:b2805b6888dc 1939
robert_jw 0:b2805b6888dc 1940 pih->unit = pcrx->pd;
robert_jw 0:b2805b6888dc 1941 pih->proto = pcrx->inProtocol;
robert_jw 0:b2805b6888dc 1942
robert_jw 0:b2805b6888dc 1943 nextNBuf->len += sizeof(*pih);
robert_jw 0:b2805b6888dc 1944
robert_jw 0:b2805b6888dc 1945 pcrx->inHead = nextNBuf;
robert_jw 0:b2805b6888dc 1946 }
robert_jw 0:b2805b6888dc 1947 pcrx->inTail = nextNBuf;
robert_jw 0:b2805b6888dc 1948 }
robert_jw 0:b2805b6888dc 1949 /* Load character into buffer. */
robert_jw 0:b2805b6888dc 1950 ((u_char*)pcrx->inTail->payload)[pcrx->inTail->len++] = curChar;
robert_jw 0:b2805b6888dc 1951 break;
robert_jw 0:b2805b6888dc 1952 }
robert_jw 0:b2805b6888dc 1953
robert_jw 0:b2805b6888dc 1954 /* update the frame check sequence number. */
robert_jw 0:b2805b6888dc 1955 pcrx->inFCS = PPP_FCS(pcrx->inFCS, curChar);
robert_jw 0:b2805b6888dc 1956 }
robert_jw 0:b2805b6888dc 1957 } /* while (l-- > 0), all bytes processed */
robert_jw 0:b2805b6888dc 1958
robert_jw 0:b2805b6888dc 1959 avRandomize();
robert_jw 0:b2805b6888dc 1960 }
robert_jw 0:b2805b6888dc 1961 #endif /* PPPOS_SUPPORT */
robert_jw 0:b2805b6888dc 1962
robert_jw 0:b2805b6888dc 1963 #if PPPOE_SUPPORT
robert_jw 0:b2805b6888dc 1964 void
robert_jw 0:b2805b6888dc 1965 pppInProcOverEthernet(int pd, struct pbuf *pb)
robert_jw 0:b2805b6888dc 1966 {
robert_jw 0:b2805b6888dc 1967 struct pppInputHeader *pih;
robert_jw 0:b2805b6888dc 1968 u16_t inProtocol;
robert_jw 0:b2805b6888dc 1969
robert_jw 0:b2805b6888dc 1970 if(pb->len < sizeof(inProtocol)) {
robert_jw 0:b2805b6888dc 1971 PPPDEBUG(LOG_ERR, ("pppInProcOverEthernet: too small for protocol field\n"));
robert_jw 0:b2805b6888dc 1972 goto drop;
robert_jw 0:b2805b6888dc 1973 }
robert_jw 0:b2805b6888dc 1974
robert_jw 0:b2805b6888dc 1975 inProtocol = (((u8_t *)pb->payload)[0] << 8) | ((u8_t*)pb->payload)[1];
robert_jw 0:b2805b6888dc 1976
robert_jw 0:b2805b6888dc 1977 /* make room for pppInputHeader - should not fail */
robert_jw 0:b2805b6888dc 1978 if (pbuf_header(pb, sizeof(*pih) - sizeof(inProtocol)) != 0) {
robert_jw 0:b2805b6888dc 1979 PPPDEBUG(LOG_ERR, ("pppInProcOverEthernet: could not allocate room for header\n"));
robert_jw 0:b2805b6888dc 1980 goto drop;
robert_jw 0:b2805b6888dc 1981 }
robert_jw 0:b2805b6888dc 1982
robert_jw 0:b2805b6888dc 1983 pih = pb->payload;
robert_jw 0:b2805b6888dc 1984
robert_jw 0:b2805b6888dc 1985 pih->unit = pd;
robert_jw 0:b2805b6888dc 1986 pih->proto = inProtocol;
robert_jw 0:b2805b6888dc 1987
robert_jw 0:b2805b6888dc 1988 /* Dispatch the packet thereby consuming it. */
robert_jw 0:b2805b6888dc 1989 pppInput(pb);
robert_jw 0:b2805b6888dc 1990 return;
robert_jw 0:b2805b6888dc 1991
robert_jw 0:b2805b6888dc 1992 drop:
robert_jw 0:b2805b6888dc 1993 LINK_STATS_INC(link.drop);
robert_jw 0:b2805b6888dc 1994 snmp_inc_ifindiscards(&pppControl[pd].netif);
robert_jw 0:b2805b6888dc 1995 pbuf_free(pb);
robert_jw 0:b2805b6888dc 1996 return;
robert_jw 0:b2805b6888dc 1997 }
robert_jw 0:b2805b6888dc 1998 #endif /* PPPOE_SUPPORT */
robert_jw 0:b2805b6888dc 1999
robert_jw 0:b2805b6888dc 2000 #if LWIP_NETIF_STATUS_CALLBACK
robert_jw 0:b2805b6888dc 2001 /** Set the status callback of a PPP's netif
robert_jw 0:b2805b6888dc 2002 *
robert_jw 0:b2805b6888dc 2003 * @param pd The PPP descriptor returned by pppOpen()
robert_jw 0:b2805b6888dc 2004 * @param status_callback pointer to the status callback function
robert_jw 0:b2805b6888dc 2005 *
robert_jw 0:b2805b6888dc 2006 * @see netif_set_status_callback
robert_jw 0:b2805b6888dc 2007 */
robert_jw 0:b2805b6888dc 2008 void
robert_jw 0:b2805b6888dc 2009 ppp_set_netif_statuscallback(int pd, netif_status_callback_fn status_callback)
robert_jw 0:b2805b6888dc 2010 {
robert_jw 0:b2805b6888dc 2011 netif_set_status_callback(&pppControl[pd].netif, status_callback);
robert_jw 0:b2805b6888dc 2012 }
robert_jw 0:b2805b6888dc 2013 #endif /* LWIP_NETIF_STATUS_CALLBACK */
robert_jw 0:b2805b6888dc 2014
robert_jw 0:b2805b6888dc 2015 #if LWIP_NETIF_LINK_CALLBACK
robert_jw 0:b2805b6888dc 2016 /** Set the link callback of a PPP's netif
robert_jw 0:b2805b6888dc 2017 *
robert_jw 0:b2805b6888dc 2018 * @param pd The PPP descriptor returned by pppOpen()
robert_jw 0:b2805b6888dc 2019 * @param link_callback pointer to the link callback function
robert_jw 0:b2805b6888dc 2020 *
robert_jw 0:b2805b6888dc 2021 * @see netif_set_link_callback
robert_jw 0:b2805b6888dc 2022 */
robert_jw 0:b2805b6888dc 2023 void
robert_jw 0:b2805b6888dc 2024 ppp_set_netif_linkcallback(int pd, netif_status_callback_fn link_callback)
robert_jw 0:b2805b6888dc 2025 {
robert_jw 0:b2805b6888dc 2026 netif_set_link_callback(&pppControl[pd].netif, link_callback);
robert_jw 0:b2805b6888dc 2027 }
robert_jw 0:b2805b6888dc 2028 #endif /* LWIP_NETIF_LINK_CALLBACK */
robert_jw 0:b2805b6888dc 2029
robert_jw 0:b2805b6888dc 2030 #endif /* PPP_SUPPORT */