Leest de waarde van een sensor binnen een maakt deze beschikbaar via internet

Dependencies:   NTPClient_NetServices mbed

Committer:
hendrikvincent
Date:
Mon Dec 02 09:01:23 2013 +0000
Revision:
0:05ccbd4f84f1
eerste programma;

Who changed what in which revision?

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