EthernetNetIf Compatibility.

Dependents:   XBeeWiFi_SPI_example

Fork of NetServicesSource by Donatien Garnier

Committer:
donatien
Date:
Fri Jul 09 14:46:47 2010 +0000
Revision:
4:fd826cad83c0
Parent:
0:632c9925f013
Child:
5:dd63a1e02b1b

        

Who changed what in which revision?

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