Version of http://mbed.org/cookbook/NetServicesTribute with setting set the same for LPC2368

Dependents:   UDPSocketExample 24LCxx_I2CApp WeatherPlatform_pachube HvZServerLib ... more

Committer:
simon
Date:
Tue Nov 23 14:15:36 2010 +0000
Revision:
0:350011bf8be7
Experimental version for testing UDP

Who changed what in which revision?

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