Marco Zecchini
/
Example_RTOS
Rtos API example
Embed:
(wiki syntax)
Show/hide line numbers
lwip_pppol2tp.c
Go to the documentation of this file.
00001 /** 00002 * @file 00003 * Network Point to Point Protocol over Layer 2 Tunneling Protocol program file. 00004 * 00005 */ 00006 00007 /* 00008 * Redistribution and use in source and binary forms, with or without modification, 00009 * are permitted provided that the following conditions are met: 00010 * 00011 * 1. Redistributions of source code must retain the above copyright notice, 00012 * this list of conditions and the following disclaimer. 00013 * 2. Redistributions in binary form must reproduce the above copyright notice, 00014 * this list of conditions and the following disclaimer in the documentation 00015 * and/or other materials provided with the distribution. 00016 * 3. The name of the author may not be used to endorse or promote products 00017 * derived from this software without specific prior written permission. 00018 * 00019 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED 00020 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 00021 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT 00022 * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 00023 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT 00024 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 00025 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 00026 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 00027 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY 00028 * OF SUCH DAMAGE. 00029 * 00030 * This file is part of the lwIP TCP/IP stack. 00031 * 00032 */ 00033 00034 /* 00035 * L2TP Support status: 00036 * 00037 * Supported: 00038 * - L2TPv2 (PPP over L2TP, a.k.a. UDP tunnels) 00039 * - LAC 00040 * 00041 * Not supported: 00042 * - LNS (require PPP server support) 00043 * - L2TPv3 ethernet pseudowires 00044 * - L2TPv3 VLAN pseudowire 00045 * - L2TPv3 PPP pseudowires 00046 * - L2TPv3 IP encapsulation 00047 * - L2TPv3 IP pseudowire 00048 * - L2TP tunnel switching - http://tools.ietf.org/html/draft-ietf-l2tpext-tunnel-switching-08 00049 * - Multiple tunnels per UDP socket, as well as multiple sessions per tunnel 00050 * - Hidden AVPs 00051 */ 00052 00053 #include "netif/ppp/ppp_opts.h" 00054 #if PPP_SUPPORT && PPPOL2TP_SUPPORT /* don't build if not configured for use in lwipopts.h */ 00055 00056 #include "lwip/err.h" 00057 #include "lwip/memp.h" 00058 #include "lwip/netif.h" 00059 #include "lwip/udp.h" 00060 #include "lwip/snmp.h" 00061 00062 #include "netif/ppp/ppp_impl.h" 00063 #include "netif/ppp/lcp.h" 00064 #include "netif/ppp/ipcp.h" 00065 #include "netif/ppp/pppol2tp.h" 00066 #include "netif/ppp/pppcrypt.h" 00067 #include "netif/ppp/magic.h" 00068 00069 /* Memory pool */ 00070 LWIP_MEMPOOL_DECLARE(PPPOL2TP_PCB, MEMP_NUM_PPPOL2TP_INTERFACES, sizeof(pppol2tp_pcb), "PPPOL2TP_PCB") 00071 00072 /* callbacks called from PPP core */ 00073 static err_t pppol2tp_write(ppp_pcb *ppp, void *ctx, struct pbuf *p); 00074 static err_t pppol2tp_netif_output(ppp_pcb *ppp, void *ctx, struct pbuf *p, u_short protocol); 00075 static err_t pppol2tp_destroy(ppp_pcb *ppp, void *ctx); /* Destroy a L2TP control block */ 00076 static void pppol2tp_connect(ppp_pcb *ppp, void *ctx); /* Be a LAC, connect to a LNS. */ 00077 static void pppol2tp_disconnect(ppp_pcb *ppp, void *ctx); /* Disconnect */ 00078 00079 /* Prototypes for procedures local to this file. */ 00080 static void pppol2tp_input(void *arg, struct udp_pcb *pcb, struct pbuf *p, const ip_addr_t *addr, u16_t port); 00081 static void pppol2tp_dispatch_control_packet(pppol2tp_pcb *l2tp, u16_t port, struct pbuf *p, u16_t ns, u16_t nr); 00082 static void pppol2tp_timeout(void *arg); 00083 static void pppol2tp_abort_connect(pppol2tp_pcb *l2tp); 00084 static err_t pppol2tp_send_sccrq(pppol2tp_pcb *l2tp); 00085 static err_t pppol2tp_send_scccn(pppol2tp_pcb *l2tp, u16_t ns); 00086 static err_t pppol2tp_send_icrq(pppol2tp_pcb *l2tp, u16_t ns); 00087 static err_t pppol2tp_send_iccn(pppol2tp_pcb *l2tp, u16_t ns); 00088 static err_t pppol2tp_send_zlb(pppol2tp_pcb *l2tp, u16_t ns); 00089 static err_t pppol2tp_send_stopccn(pppol2tp_pcb *l2tp, u16_t ns); 00090 static err_t pppol2tp_xmit(pppol2tp_pcb *l2tp, struct pbuf *pb); 00091 static err_t pppol2tp_udp_send(pppol2tp_pcb *l2tp, struct pbuf *pb); 00092 00093 /* Callbacks structure for PPP core */ 00094 static const struct link_callbacks pppol2tp_callbacks = { 00095 pppol2tp_connect, 00096 #if PPP_SERVER 00097 NULL, 00098 #endif /* PPP_SERVER */ 00099 pppol2tp_disconnect, 00100 pppol2tp_destroy, 00101 pppol2tp_write, 00102 pppol2tp_netif_output, 00103 NULL, 00104 NULL 00105 }; 00106 00107 00108 /* Create a new L2TP session. */ 00109 ppp_pcb *pppol2tp_create(struct netif *pppif, 00110 struct netif *netif, const ip_addr_t *ipaddr, u16_t port, 00111 const u8_t *secret, u8_t secret_len, 00112 ppp_link_status_cb_fn link_status_cb, void *ctx_cb) { 00113 ppp_pcb *ppp; 00114 pppol2tp_pcb *l2tp; 00115 struct udp_pcb *udp; 00116 #if !PPPOL2TP_AUTH_SUPPORT 00117 LWIP_UNUSED_ARG(secret); 00118 LWIP_UNUSED_ARG(secret_len); 00119 #endif /* !PPPOL2TP_AUTH_SUPPORT */ 00120 00121 if (ipaddr == NULL) { 00122 goto ipaddr_check_failed; 00123 } 00124 00125 l2tp = (pppol2tp_pcb *)LWIP_MEMPOOL_ALLOC(PPPOL2TP_PCB); 00126 if (l2tp == NULL) { 00127 goto memp_malloc_l2tp_failed; 00128 } 00129 00130 udp = udp_new_ip_type(IP_GET_TYPE(ipaddr)); 00131 if (udp == NULL) { 00132 goto udp_new_failed; 00133 } 00134 udp_recv(udp, pppol2tp_input, l2tp); 00135 00136 ppp = ppp_new(pppif, &pppol2tp_callbacks, l2tp, link_status_cb, ctx_cb); 00137 if (ppp == NULL) { 00138 goto ppp_new_failed; 00139 } 00140 00141 memset(l2tp, 0, sizeof(pppol2tp_pcb)); 00142 l2tp->phase = PPPOL2TP_STATE_INITIAL; 00143 l2tp->ppp = ppp; 00144 l2tp->udp = udp; 00145 l2tp->netif = netif; 00146 ip_addr_copy(l2tp->remote_ip, *ipaddr); 00147 l2tp->remote_port = port; 00148 #if PPPOL2TP_AUTH_SUPPORT 00149 l2tp->secret = secret; 00150 l2tp->secret_len = secret_len; 00151 #endif /* PPPOL2TP_AUTH_SUPPORT */ 00152 00153 return ppp; 00154 00155 ppp_new_failed: 00156 udp_remove(udp); 00157 udp_new_failed: 00158 LWIP_MEMPOOL_FREE(PPPOL2TP_PCB, l2tp); 00159 memp_malloc_l2tp_failed: 00160 ipaddr_check_failed: 00161 return NULL; 00162 } 00163 00164 /* Called by PPP core */ 00165 static err_t pppol2tp_write(ppp_pcb *ppp, void *ctx, struct pbuf *p) { 00166 pppol2tp_pcb *l2tp = (pppol2tp_pcb *)ctx; 00167 struct pbuf *ph; /* UDP + L2TP header */ 00168 err_t ret; 00169 #if MIB2_STATS 00170 u16_t tot_len; 00171 #else /* MIB2_STATS */ 00172 LWIP_UNUSED_ARG(ppp); 00173 #endif /* MIB2_STATS */ 00174 00175 ph = pbuf_alloc(PBUF_TRANSPORT, (u16_t)(PPPOL2TP_OUTPUT_DATA_HEADER_LEN), PBUF_RAM); 00176 if(!ph) { 00177 LINK_STATS_INC(link.memerr); 00178 LINK_STATS_INC(link.proterr); 00179 MIB2_STATS_NETIF_INC(ppp->netif, ifoutdiscards); 00180 pbuf_free(p); 00181 return ERR_MEM; 00182 } 00183 00184 pbuf_header(ph, -(s16_t)PPPOL2TP_OUTPUT_DATA_HEADER_LEN); /* hide L2TP header */ 00185 pbuf_cat(ph, p); 00186 #if MIB2_STATS 00187 tot_len = ph->tot_len; 00188 #endif /* MIB2_STATS */ 00189 00190 ret = pppol2tp_xmit(l2tp, ph); 00191 if (ret != ERR_OK) { 00192 LINK_STATS_INC(link.err); 00193 MIB2_STATS_NETIF_INC(ppp->netif, ifoutdiscards); 00194 return ret; 00195 } 00196 00197 MIB2_STATS_NETIF_ADD(ppp->netif, ifoutoctets, (u16_t)tot_len); 00198 MIB2_STATS_NETIF_INC(ppp->netif, ifoutucastpkts); 00199 LINK_STATS_INC(link.xmit); 00200 return ERR_OK; 00201 } 00202 00203 /* Called by PPP core */ 00204 static err_t pppol2tp_netif_output(ppp_pcb *ppp, void *ctx, struct pbuf *p, u_short protocol) { 00205 pppol2tp_pcb *l2tp = (pppol2tp_pcb *)ctx; 00206 struct pbuf *pb; 00207 u8_t *pl; 00208 err_t err; 00209 #if MIB2_STATS 00210 u16_t tot_len; 00211 #else /* MIB2_STATS */ 00212 LWIP_UNUSED_ARG(ppp); 00213 #endif /* MIB2_STATS */ 00214 00215 /* @todo: try to use pbuf_header() here! */ 00216 pb = pbuf_alloc(PBUF_TRANSPORT, PPPOL2TP_OUTPUT_DATA_HEADER_LEN + sizeof(protocol), PBUF_RAM); 00217 if(!pb) { 00218 LINK_STATS_INC(link.memerr); 00219 LINK_STATS_INC(link.proterr); 00220 MIB2_STATS_NETIF_INC(ppp->netif, ifoutdiscards); 00221 return ERR_MEM; 00222 } 00223 00224 pbuf_header(pb, -(s16_t)PPPOL2TP_OUTPUT_DATA_HEADER_LEN); 00225 00226 pl = (u8_t*)pb->payload; 00227 PUTSHORT(protocol, pl); 00228 00229 pbuf_chain(pb, p); 00230 #if MIB2_STATS 00231 tot_len = pb->tot_len; 00232 #endif /* MIB2_STATS */ 00233 00234 if( (err = pppol2tp_xmit(l2tp, pb)) != ERR_OK) { 00235 LINK_STATS_INC(link.err); 00236 MIB2_STATS_NETIF_INC(ppp->netif, ifoutdiscards); 00237 return err; 00238 } 00239 00240 MIB2_STATS_NETIF_ADD(ppp->netif, ifoutoctets, tot_len); 00241 MIB2_STATS_NETIF_INC(ppp->netif, ifoutucastpkts); 00242 LINK_STATS_INC(link.xmit); 00243 return ERR_OK; 00244 } 00245 00246 /* Destroy a L2TP control block */ 00247 static err_t pppol2tp_destroy(ppp_pcb *ppp, void *ctx) { 00248 pppol2tp_pcb *l2tp = (pppol2tp_pcb *)ctx; 00249 LWIP_UNUSED_ARG(ppp); 00250 00251 sys_untimeout(pppol2tp_timeout, l2tp); 00252 udp_remove(l2tp->udp); 00253 LWIP_MEMPOOL_FREE(PPPOL2TP_PCB, l2tp); 00254 return ERR_OK; 00255 } 00256 00257 /* Be a LAC, connect to a LNS. */ 00258 static void pppol2tp_connect(ppp_pcb *ppp, void *ctx) { 00259 err_t err; 00260 pppol2tp_pcb *l2tp = (pppol2tp_pcb *)ctx; 00261 lcp_options *lcp_wo; 00262 lcp_options *lcp_ao; 00263 #if PPP_IPV4_SUPPORT && VJ_SUPPORT 00264 ipcp_options *ipcp_wo; 00265 ipcp_options *ipcp_ao; 00266 #endif /* PPP_IPV4_SUPPORT && VJ_SUPPORT */ 00267 00268 l2tp->tunnel_port = l2tp->remote_port; 00269 l2tp->our_ns = 0; 00270 l2tp->peer_nr = 0; 00271 l2tp->peer_ns = 0; 00272 l2tp->source_tunnel_id = 0; 00273 l2tp->remote_tunnel_id = 0; 00274 l2tp->source_session_id = 0; 00275 l2tp->remote_session_id = 0; 00276 /* l2tp->*_retried are cleared when used */ 00277 00278 lcp_wo = &ppp->lcp_wantoptions; 00279 lcp_wo->mru = PPPOL2TP_DEFMRU; 00280 lcp_wo->neg_asyncmap = 0; 00281 lcp_wo->neg_pcompression = 0; 00282 lcp_wo->neg_accompression = 0; 00283 lcp_wo->passive = 0; 00284 lcp_wo->silent = 0; 00285 00286 lcp_ao = &ppp->lcp_allowoptions; 00287 lcp_ao->mru = PPPOL2TP_DEFMRU; 00288 lcp_ao->neg_asyncmap = 0; 00289 lcp_ao->neg_pcompression = 0; 00290 lcp_ao->neg_accompression = 0; 00291 00292 #if PPP_IPV4_SUPPORT && VJ_SUPPORT 00293 ipcp_wo = &ppp->ipcp_wantoptions; 00294 ipcp_wo->neg_vj = 0; 00295 ipcp_wo->old_vj = 0; 00296 00297 ipcp_ao = &ppp->ipcp_allowoptions; 00298 ipcp_ao->neg_vj = 0; 00299 ipcp_ao->old_vj = 0; 00300 #endif /* PPP_IPV4_SUPPORT && VJ_SUPPORT */ 00301 00302 /* Listen to a random source port, we need to do that instead of using udp_connect() 00303 * because the L2TP LNS might answer with its own random source port (!= 1701) 00304 */ 00305 #if LWIP_IPV6 00306 if (IP_IS_V6_VAL(l2tp->udp->local_ip)) { 00307 udp_bind(l2tp->udp, IP6_ADDR_ANY, 0); 00308 } else 00309 #endif /* LWIP_IPV6 */ 00310 udp_bind(l2tp->udp, IP_ADDR_ANY, 0); 00311 00312 #if PPPOL2TP_AUTH_SUPPORT 00313 /* Generate random vector */ 00314 if (l2tp->secret != NULL) { 00315 magic_random_bytes(l2tp->secret_rv, sizeof(l2tp->secret_rv)); 00316 } 00317 #endif /* PPPOL2TP_AUTH_SUPPORT */ 00318 00319 do { 00320 l2tp->remote_tunnel_id = magic(); 00321 } while(l2tp->remote_tunnel_id == 0); 00322 /* save state, in case we fail to send SCCRQ */ 00323 l2tp->sccrq_retried = 0; 00324 l2tp->phase = PPPOL2TP_STATE_SCCRQ_SENT; 00325 if ((err = pppol2tp_send_sccrq(l2tp)) != 0) { 00326 PPPDEBUG(LOG_DEBUG, ("pppol2tp: failed to send SCCRQ, error=%d\n", err)); 00327 } 00328 sys_timeout(PPPOL2TP_CONTROL_TIMEOUT, pppol2tp_timeout, l2tp); 00329 } 00330 00331 /* Disconnect */ 00332 static void pppol2tp_disconnect(ppp_pcb *ppp, void *ctx) { 00333 pppol2tp_pcb *l2tp = (pppol2tp_pcb *)ctx; 00334 00335 l2tp->our_ns++; 00336 pppol2tp_send_stopccn(l2tp, l2tp->our_ns); 00337 00338 /* stop any timer, disconnect can be called while initiating is in progress */ 00339 sys_untimeout(pppol2tp_timeout, l2tp); 00340 l2tp->phase = PPPOL2TP_STATE_INITIAL; 00341 ppp_link_end(ppp); /* notify upper layers */ 00342 } 00343 00344 /* UDP Callback for incoming IPv4 L2TP frames */ 00345 static void pppol2tp_input(void *arg, struct udp_pcb *pcb, struct pbuf *p, const ip_addr_t *addr, u16_t port) { 00346 pppol2tp_pcb *l2tp = (pppol2tp_pcb*)arg; 00347 u16_t hflags, hlen, len=0, tunnel_id=0, session_id=0, ns=0, nr=0, offset=0; 00348 u8_t *inp; 00349 LWIP_UNUSED_ARG(pcb); 00350 00351 /* we can't unbound a UDP pcb, thus we can still receive UDP frames after the link is closed */ 00352 if (l2tp->phase < PPPOL2TP_STATE_SCCRQ_SENT) { 00353 goto free_and_return; 00354 } 00355 00356 if (!ip_addr_cmp(&l2tp->remote_ip, addr)) { 00357 goto free_and_return; 00358 } 00359 00360 /* discard packet if port mismatch, but only if we received a SCCRP */ 00361 if (l2tp->phase > PPPOL2TP_STATE_SCCRQ_SENT && l2tp->tunnel_port != port) { 00362 goto free_and_return; 00363 } 00364 00365 /* printf("-----------\nL2TP INPUT, %d\n", p->len); */ 00366 00367 /* L2TP header */ 00368 if (p->len < sizeof(hflags) + sizeof(tunnel_id) + sizeof(session_id) ) { 00369 goto packet_too_short; 00370 } 00371 00372 inp = (u8_t*)p->payload; 00373 GETSHORT(hflags, inp); 00374 00375 if (hflags & PPPOL2TP_HEADERFLAG_CONTROL) { 00376 /* check mandatory flags for a control packet */ 00377 if ( (hflags & PPPOL2TP_HEADERFLAG_CONTROL_MANDATORY) != PPPOL2TP_HEADERFLAG_CONTROL_MANDATORY ) { 00378 PPPDEBUG(LOG_DEBUG, ("pppol2tp: mandatory header flags for control packet not set\n")); 00379 goto free_and_return; 00380 } 00381 /* check forbidden flags for a control packet */ 00382 if (hflags & PPPOL2TP_HEADERFLAG_CONTROL_FORBIDDEN) { 00383 PPPDEBUG(LOG_DEBUG, ("pppol2tp: forbidden header flags for control packet found\n")); 00384 goto free_and_return; 00385 } 00386 } else { 00387 /* check mandatory flags for a data packet */ 00388 if ( (hflags & PPPOL2TP_HEADERFLAG_DATA_MANDATORY) != PPPOL2TP_HEADERFLAG_DATA_MANDATORY) { 00389 PPPDEBUG(LOG_DEBUG, ("pppol2tp: mandatory header flags for data packet not set\n")); 00390 goto free_and_return; 00391 } 00392 } 00393 00394 /* Expected header size */ 00395 hlen = sizeof(hflags) + sizeof(tunnel_id) + sizeof(session_id); 00396 if (hflags & PPPOL2TP_HEADERFLAG_LENGTH) { 00397 hlen += sizeof(len); 00398 } 00399 if (hflags & PPPOL2TP_HEADERFLAG_SEQUENCE) { 00400 hlen += sizeof(ns) + sizeof(nr); 00401 } 00402 if (hflags & PPPOL2TP_HEADERFLAG_OFFSET) { 00403 hlen += sizeof(offset); 00404 } 00405 if (p->len < hlen) { 00406 goto packet_too_short; 00407 } 00408 00409 if (hflags & PPPOL2TP_HEADERFLAG_LENGTH) { 00410 GETSHORT(len, inp); 00411 if (p->len < len || len < hlen) { 00412 goto packet_too_short; 00413 } 00414 } 00415 GETSHORT(tunnel_id, inp); 00416 GETSHORT(session_id, inp); 00417 if (hflags & PPPOL2TP_HEADERFLAG_SEQUENCE) { 00418 GETSHORT(ns, inp); 00419 GETSHORT(nr, inp); 00420 } 00421 if (hflags & PPPOL2TP_HEADERFLAG_OFFSET) { 00422 GETSHORT(offset, inp) 00423 if (offset > 4096) { /* don't be fooled with large offset which might overflow hlen */ 00424 PPPDEBUG(LOG_DEBUG, ("pppol2tp: strange packet received, offset=%d\n", offset)); 00425 goto free_and_return; 00426 } 00427 hlen += offset; 00428 if (p->len < hlen) { 00429 goto packet_too_short; 00430 } 00431 INCPTR(offset, inp); 00432 } 00433 00434 /* printf("HLEN = %d\n", hlen); */ 00435 00436 /* skip L2TP header */ 00437 if (pbuf_header(p, -(s16_t)hlen) != 0) { 00438 goto free_and_return; 00439 } 00440 00441 /* printf("LEN=%d, TUNNEL_ID=%d, SESSION_ID=%d, NS=%d, NR=%d, OFFSET=%d\n", len, tunnel_id, session_id, ns, nr, offset); */ 00442 PPPDEBUG(LOG_DEBUG, ("pppol2tp: input packet, len=%"U16_F", tunnel=%"U16_F", session=%"U16_F", ns=%"U16_F", nr=%"U16_F"\n", 00443 len, tunnel_id, session_id, ns, nr)); 00444 00445 /* Control packet */ 00446 if (hflags & PPPOL2TP_HEADERFLAG_CONTROL) { 00447 pppol2tp_dispatch_control_packet(l2tp, port, p, ns, nr); 00448 goto free_and_return; 00449 } 00450 00451 /* Data packet */ 00452 if(l2tp->phase != PPPOL2TP_STATE_DATA) { 00453 goto free_and_return; 00454 } 00455 if(tunnel_id != l2tp->remote_tunnel_id) { 00456 PPPDEBUG(LOG_DEBUG, ("pppol2tp: tunnel ID mismatch, assigned=%d, received=%d\n", l2tp->remote_tunnel_id, tunnel_id)); 00457 goto free_and_return; 00458 } 00459 if(session_id != l2tp->remote_session_id) { 00460 PPPDEBUG(LOG_DEBUG, ("pppol2tp: session ID mismatch, assigned=%d, received=%d\n", l2tp->remote_session_id, session_id)); 00461 goto free_and_return; 00462 } 00463 /* 00464 * skip address & flags if necessary 00465 * 00466 * RFC 2661 does not specify whether the PPP frame in the L2TP payload should 00467 * have a HDLC header or not. We handle both cases for compatibility. 00468 */ 00469 if (p->len >= 2) { 00470 GETSHORT(hflags, inp); 00471 if (hflags == 0xff03) { 00472 pbuf_header(p, -(s16_t)2); 00473 } 00474 } 00475 /* Dispatch the packet thereby consuming it. */ 00476 ppp_input(l2tp->ppp, p); 00477 return; 00478 00479 packet_too_short: 00480 PPPDEBUG(LOG_DEBUG, ("pppol2tp: packet too short: %d\n", p->len)); 00481 free_and_return: 00482 pbuf_free(p); 00483 } 00484 00485 /* L2TP Control packet entry point */ 00486 static void pppol2tp_dispatch_control_packet(pppol2tp_pcb *l2tp, u16_t port, struct pbuf *p, u16_t ns, u16_t nr) { 00487 u8_t *inp; 00488 u16_t avplen, avpflags, vendorid, attributetype, messagetype=0; 00489 err_t err; 00490 #if PPPOL2TP_AUTH_SUPPORT 00491 lwip_md5_context md5_ctx; 00492 u8_t md5_hash[16]; 00493 u8_t challenge_id = 0; 00494 #endif /* PPPOL2TP_AUTH_SUPPORT */ 00495 00496 l2tp->peer_nr = nr; 00497 l2tp->peer_ns = ns; 00498 /* printf("L2TP CTRL INPUT, ns=%d, nr=%d, len=%d\n", ns, nr, p->len); */ 00499 00500 /* Handle the special case of the ICCN acknowledge */ 00501 if (l2tp->phase == PPPOL2TP_STATE_ICCN_SENT && l2tp->peer_nr > l2tp->our_ns) { 00502 l2tp->phase = PPPOL2TP_STATE_DATA; 00503 } 00504 00505 /* ZLB packets */ 00506 if (p->tot_len == 0) { 00507 return; 00508 } 00509 00510 p = ppp_singlebuf(p); 00511 inp = (u8_t*)p->payload; 00512 /* Decode AVPs */ 00513 while (p->len > 0) { 00514 if (p->len < sizeof(avpflags) + sizeof(vendorid) + sizeof(attributetype) ) { 00515 goto packet_too_short; 00516 } 00517 GETSHORT(avpflags, inp); 00518 avplen = avpflags & PPPOL2TP_AVPHEADERFLAG_LENGTHMASK; 00519 /* printf("AVPLEN = %d\n", avplen); */ 00520 if (p->len < avplen || avplen < sizeof(avpflags) + sizeof(vendorid) + sizeof(attributetype)) { 00521 goto packet_too_short; 00522 } 00523 GETSHORT(vendorid, inp); 00524 GETSHORT(attributetype, inp); 00525 avplen -= sizeof(avpflags) + sizeof(vendorid) + sizeof(attributetype); 00526 00527 /* Message type must be the first AVP */ 00528 if (messagetype == 0) { 00529 if (attributetype != 0 || vendorid != 0 || avplen != sizeof(messagetype) ) { 00530 PPPDEBUG(LOG_DEBUG, ("pppol2tp: message type must be the first AVP\n")); 00531 return; 00532 } 00533 GETSHORT(messagetype, inp); 00534 /* printf("Message type = %d\n", messagetype); */ 00535 switch(messagetype) { 00536 /* Start Control Connection Reply */ 00537 case PPPOL2TP_MESSAGETYPE_SCCRP: 00538 /* Only accept SCCRP packet if we sent a SCCRQ */ 00539 if (l2tp->phase != PPPOL2TP_STATE_SCCRQ_SENT) { 00540 goto send_zlb; 00541 } 00542 break; 00543 /* Incoming Call Reply */ 00544 case PPPOL2TP_MESSAGETYPE_ICRP: 00545 /* Only accept ICRP packet if we sent a IRCQ */ 00546 if (l2tp->phase != PPPOL2TP_STATE_ICRQ_SENT) { 00547 goto send_zlb; 00548 } 00549 break; 00550 /* Stop Control Connection Notification */ 00551 case PPPOL2TP_MESSAGETYPE_STOPCCN: 00552 pppol2tp_send_zlb(l2tp, l2tp->our_ns); /* Ack the StopCCN before we switch to down state */ 00553 if (l2tp->phase < PPPOL2TP_STATE_DATA) { 00554 pppol2tp_abort_connect(l2tp); 00555 } else if (l2tp->phase == PPPOL2TP_STATE_DATA) { 00556 /* Don't disconnect here, we let the LCP Echo/Reply find the fact 00557 * that PPP session is down. Asking the PPP stack to end the session 00558 * require strict checking about the PPP phase to prevent endless 00559 * disconnection loops. 00560 */ 00561 } 00562 return; 00563 default: 00564 break; 00565 } 00566 goto nextavp; 00567 } 00568 00569 /* Skip proprietary L2TP extensions */ 00570 if (vendorid != 0) { 00571 goto skipavp; 00572 } 00573 00574 switch (messagetype) { 00575 /* Start Control Connection Reply */ 00576 case PPPOL2TP_MESSAGETYPE_SCCRP: 00577 switch (attributetype) { 00578 case PPPOL2TP_AVPTYPE_TUNNELID: 00579 if (avplen != sizeof(l2tp->source_tunnel_id) ) { 00580 PPPDEBUG(LOG_DEBUG, ("pppol2tp: AVP Assign tunnel ID length check failed\n")); 00581 return; 00582 } 00583 GETSHORT(l2tp->source_tunnel_id, inp); 00584 PPPDEBUG(LOG_DEBUG, ("pppol2tp: Assigned tunnel ID %"U16_F"\n", l2tp->source_tunnel_id)); 00585 goto nextavp; 00586 #if PPPOL2TP_AUTH_SUPPORT 00587 case PPPOL2TP_AVPTYPE_CHALLENGE: 00588 if (avplen == 0) { 00589 PPPDEBUG(LOG_DEBUG, ("pppol2tp: Challenge length check failed\n")); 00590 return; 00591 } 00592 if (l2tp->secret == NULL) { 00593 PPPDEBUG(LOG_DEBUG, ("pppol2tp: Received challenge from peer and no secret key available\n")); 00594 pppol2tp_abort_connect(l2tp); 00595 return; 00596 } 00597 /* Generate hash of ID, secret, challenge */ 00598 lwip_md5_init(&md5_ctx); 00599 lwip_md5_starts(&md5_ctx); 00600 challenge_id = PPPOL2TP_MESSAGETYPE_SCCCN; 00601 lwip_md5_update(&md5_ctx, &challenge_id, 1); 00602 lwip_md5_update(&md5_ctx, l2tp->secret, l2tp->secret_len); 00603 lwip_md5_update(&md5_ctx, inp, avplen); 00604 lwip_md5_finish(&md5_ctx, l2tp->challenge_hash); 00605 lwip_md5_free(&md5_ctx); 00606 l2tp->send_challenge = 1; 00607 goto skipavp; 00608 case PPPOL2TP_AVPTYPE_CHALLENGERESPONSE: 00609 if (avplen != PPPOL2TP_AVPTYPE_CHALLENGERESPONSE_SIZE) { 00610 PPPDEBUG(LOG_DEBUG, ("pppol2tp: AVP Challenge Response length check failed\n")); 00611 return; 00612 } 00613 /* Generate hash of ID, secret, challenge */ 00614 lwip_md5_init(&md5_ctx); 00615 lwip_md5_starts(&md5_ctx); 00616 challenge_id = PPPOL2TP_MESSAGETYPE_SCCRP; 00617 lwip_md5_update(&md5_ctx, &challenge_id, 1); 00618 lwip_md5_update(&md5_ctx, l2tp->secret, l2tp->secret_len); 00619 lwip_md5_update(&md5_ctx, l2tp->secret_rv, sizeof(l2tp->secret_rv)); 00620 lwip_md5_finish(&md5_ctx, md5_hash); 00621 lwip_md5_free(&md5_ctx); 00622 if ( memcmp(inp, md5_hash, sizeof(md5_hash)) ) { 00623 PPPDEBUG(LOG_DEBUG, ("pppol2tp: Received challenge response from peer and secret key do not match\n")); 00624 pppol2tp_abort_connect(l2tp); 00625 return; 00626 } 00627 goto skipavp; 00628 #endif /* PPPOL2TP_AUTH_SUPPORT */ 00629 default: 00630 break; 00631 } 00632 break; 00633 /* Incoming Call Reply */ 00634 case PPPOL2TP_MESSAGETYPE_ICRP: 00635 switch (attributetype) { 00636 case PPPOL2TP_AVPTYPE_SESSIONID: 00637 if (avplen != sizeof(l2tp->source_session_id) ) { 00638 PPPDEBUG(LOG_DEBUG, ("pppol2tp: AVP Assign session ID length check failed\n")); 00639 return; 00640 } 00641 GETSHORT(l2tp->source_session_id, inp); 00642 PPPDEBUG(LOG_DEBUG, ("pppol2tp: Assigned session ID %"U16_F"\n", l2tp->source_session_id)); 00643 goto nextavp; 00644 default: 00645 break; 00646 } 00647 break; 00648 default: 00649 break; 00650 } 00651 00652 skipavp: 00653 INCPTR(avplen, inp); 00654 nextavp: 00655 /* printf("AVP Found, vendor=%d, attribute=%d, len=%d\n", vendorid, attributetype, avplen); */ 00656 /* next AVP */ 00657 if (pbuf_header(p, -(s16_t)(avplen + sizeof(avpflags) + sizeof(vendorid) + sizeof(attributetype)) ) != 0) { 00658 return; 00659 } 00660 } 00661 00662 switch(messagetype) { 00663 /* Start Control Connection Reply */ 00664 case PPPOL2TP_MESSAGETYPE_SCCRP: 00665 do { 00666 l2tp->remote_session_id = magic(); 00667 } while(l2tp->remote_session_id == 0); 00668 l2tp->tunnel_port = port; /* LNS server might have chosen its own local port */ 00669 l2tp->icrq_retried = 0; 00670 l2tp->phase = PPPOL2TP_STATE_ICRQ_SENT; 00671 l2tp->our_ns++; 00672 if ((err = pppol2tp_send_scccn(l2tp, l2tp->our_ns)) != 0) { 00673 PPPDEBUG(LOG_DEBUG, ("pppol2tp: failed to send SCCCN, error=%d\n", err)); 00674 } 00675 l2tp->our_ns++; 00676 if ((err = pppol2tp_send_icrq(l2tp, l2tp->our_ns)) != 0) { 00677 PPPDEBUG(LOG_DEBUG, ("pppol2tp: failed to send ICRQ, error=%d\n", err)); 00678 } 00679 sys_untimeout(pppol2tp_timeout, l2tp); 00680 sys_timeout(PPPOL2TP_CONTROL_TIMEOUT, pppol2tp_timeout, l2tp); 00681 break; 00682 /* Incoming Call Reply */ 00683 case PPPOL2TP_MESSAGETYPE_ICRP: 00684 l2tp->iccn_retried = 0; 00685 l2tp->phase = PPPOL2TP_STATE_ICCN_SENT; 00686 l2tp->our_ns++; 00687 ppp_start(l2tp->ppp); /* notify upper layers */ 00688 if ((err = pppol2tp_send_iccn(l2tp, l2tp->our_ns)) != 0) { 00689 PPPDEBUG(LOG_DEBUG, ("pppol2tp: failed to send ICCN, error=%d\n", err)); 00690 } 00691 sys_untimeout(pppol2tp_timeout, l2tp); 00692 sys_timeout(PPPOL2TP_CONTROL_TIMEOUT, pppol2tp_timeout, l2tp); 00693 break; 00694 /* Unhandled packet, send ZLB ACK */ 00695 default: 00696 goto send_zlb; 00697 } 00698 return; 00699 00700 send_zlb: 00701 pppol2tp_send_zlb(l2tp, l2tp->our_ns); 00702 return; 00703 packet_too_short: 00704 PPPDEBUG(LOG_DEBUG, ("pppol2tp: packet too short: %d\n", p->len)); 00705 } 00706 00707 /* L2TP Timeout handler */ 00708 static void pppol2tp_timeout(void *arg) { 00709 pppol2tp_pcb *l2tp = (pppol2tp_pcb*)arg; 00710 err_t err; 00711 u32_t retry_wait; 00712 00713 PPPDEBUG(LOG_DEBUG, ("pppol2tp: timeout\n")); 00714 00715 switch (l2tp->phase) { 00716 case PPPOL2TP_STATE_SCCRQ_SENT: 00717 /* backoff wait */ 00718 if (l2tp->sccrq_retried < 0xff) { 00719 l2tp->sccrq_retried++; 00720 } 00721 if (!l2tp->ppp->settings.persist && l2tp->sccrq_retried >= PPPOL2TP_MAXSCCRQ) { 00722 pppol2tp_abort_connect(l2tp); 00723 return; 00724 } 00725 retry_wait = LWIP_MIN(PPPOL2TP_CONTROL_TIMEOUT * l2tp->sccrq_retried, PPPOL2TP_SLOW_RETRY); 00726 PPPDEBUG(LOG_DEBUG, ("pppol2tp: sccrq_retried=%d\n", l2tp->sccrq_retried)); 00727 if ((err = pppol2tp_send_sccrq(l2tp)) != 0) { 00728 l2tp->sccrq_retried--; 00729 PPPDEBUG(LOG_DEBUG, ("pppol2tp: failed to send SCCRQ, error=%d\n", err)); 00730 } 00731 sys_timeout(retry_wait, pppol2tp_timeout, l2tp); 00732 break; 00733 00734 case PPPOL2TP_STATE_ICRQ_SENT: 00735 l2tp->icrq_retried++; 00736 if (l2tp->icrq_retried >= PPPOL2TP_MAXICRQ) { 00737 pppol2tp_abort_connect(l2tp); 00738 return; 00739 } 00740 PPPDEBUG(LOG_DEBUG, ("pppol2tp: icrq_retried=%d\n", l2tp->icrq_retried)); 00741 if (l2tp->peer_nr <= l2tp->our_ns -1) { /* the SCCCN was not acknowledged */ 00742 if ((err = pppol2tp_send_scccn(l2tp, l2tp->our_ns -1)) != 0) { 00743 l2tp->icrq_retried--; 00744 PPPDEBUG(LOG_DEBUG, ("pppol2tp: failed to send SCCCN, error=%d\n", err)); 00745 sys_timeout(PPPOL2TP_CONTROL_TIMEOUT, pppol2tp_timeout, l2tp); 00746 break; 00747 } 00748 } 00749 if ((err = pppol2tp_send_icrq(l2tp, l2tp->our_ns)) != 0) { 00750 l2tp->icrq_retried--; 00751 PPPDEBUG(LOG_DEBUG, ("pppol2tp: failed to send ICRQ, error=%d\n", err)); 00752 } 00753 sys_timeout(PPPOL2TP_CONTROL_TIMEOUT, pppol2tp_timeout, l2tp); 00754 break; 00755 00756 case PPPOL2TP_STATE_ICCN_SENT: 00757 l2tp->iccn_retried++; 00758 if (l2tp->iccn_retried >= PPPOL2TP_MAXICCN) { 00759 pppol2tp_abort_connect(l2tp); 00760 return; 00761 } 00762 PPPDEBUG(LOG_DEBUG, ("pppol2tp: iccn_retried=%d\n", l2tp->iccn_retried)); 00763 if ((err = pppol2tp_send_iccn(l2tp, l2tp->our_ns)) != 0) { 00764 l2tp->iccn_retried--; 00765 PPPDEBUG(LOG_DEBUG, ("pppol2tp: failed to send ICCN, error=%d\n", err)); 00766 } 00767 sys_timeout(PPPOL2TP_CONTROL_TIMEOUT, pppol2tp_timeout, l2tp); 00768 break; 00769 00770 default: 00771 return; /* all done, work in peace */ 00772 } 00773 } 00774 00775 /* Connection attempt aborted */ 00776 static void pppol2tp_abort_connect(pppol2tp_pcb *l2tp) { 00777 PPPDEBUG(LOG_DEBUG, ("pppol2tp: could not establish connection\n")); 00778 l2tp->phase = PPPOL2TP_STATE_INITIAL; 00779 ppp_link_failed(l2tp->ppp); /* notify upper layers */ 00780 } 00781 00782 /* Initiate a new tunnel */ 00783 static err_t pppol2tp_send_sccrq(pppol2tp_pcb *l2tp) { 00784 struct pbuf *pb; 00785 u8_t *p; 00786 u16_t len; 00787 00788 /* calculate UDP packet length */ 00789 len = 12 +8 +8 +10 +10 +6+sizeof(PPPOL2TP_HOSTNAME)-1 +6+sizeof(PPPOL2TP_VENDORNAME)-1 +8 +8; 00790 #if PPPOL2TP_AUTH_SUPPORT 00791 if (l2tp->secret != NULL) { 00792 len += 6 + sizeof(l2tp->secret_rv); 00793 } 00794 #endif /* PPPOL2TP_AUTH_SUPPORT */ 00795 00796 /* allocate a buffer */ 00797 pb = pbuf_alloc(PBUF_TRANSPORT, len, PBUF_RAM); 00798 if (pb == NULL) { 00799 return ERR_MEM; 00800 } 00801 LWIP_ASSERT("pb->tot_len == pb->len", pb->tot_len == pb->len); 00802 00803 p = (u8_t*)pb->payload; 00804 /* fill in pkt */ 00805 /* L2TP control header */ 00806 PUTSHORT(PPPOL2TP_HEADERFLAG_CONTROL_MANDATORY, p); 00807 PUTSHORT(len, p); /* Length */ 00808 PUTSHORT(0, p); /* Tunnel Id */ 00809 PUTSHORT(0, p); /* Session Id */ 00810 PUTSHORT(0, p); /* NS Sequence number - to peer */ 00811 PUTSHORT(0, p); /* NR Sequence number - expected for peer */ 00812 00813 /* AVP - Message type */ 00814 PUTSHORT(PPPOL2TP_AVPHEADERFLAG_MANDATORY + 8, p); /* Mandatory flag + len field */ 00815 PUTSHORT(0, p); /* Vendor ID */ 00816 PUTSHORT(PPPOL2TP_AVPTYPE_MESSAGE, p); /* Attribute type: Message Type */ 00817 PUTSHORT(PPPOL2TP_MESSAGETYPE_SCCRQ, p); /* Attribute value: Message type: SCCRQ */ 00818 00819 /* AVP - L2TP Version */ 00820 PUTSHORT(PPPOL2TP_AVPHEADERFLAG_MANDATORY + 8, p); /* Mandatory flag + len field */ 00821 PUTSHORT(0, p); /* Vendor ID */ 00822 PUTSHORT(PPPOL2TP_AVPTYPE_VERSION, p); /* Attribute type: Version */ 00823 PUTSHORT(PPPOL2TP_VERSION, p); /* Attribute value: L2TP Version */ 00824 00825 /* AVP - Framing capabilities */ 00826 PUTSHORT(PPPOL2TP_AVPHEADERFLAG_MANDATORY + 10, p); /* Mandatory flag + len field */ 00827 PUTSHORT(0, p); /* Vendor ID */ 00828 PUTSHORT(PPPOL2TP_AVPTYPE_FRAMINGCAPABILITIES, p); /* Attribute type: Framing capabilities */ 00829 PUTLONG(PPPOL2TP_FRAMINGCAPABILITIES, p); /* Attribute value: Framing capabilities */ 00830 00831 /* AVP - Bearer capabilities */ 00832 PUTSHORT(PPPOL2TP_AVPHEADERFLAG_MANDATORY + 10, p); /* Mandatory flag + len field */ 00833 PUTSHORT(0, p); /* Vendor ID */ 00834 PUTSHORT(PPPOL2TP_AVPTYPE_BEARERCAPABILITIES, p); /* Attribute type: Bearer capabilities */ 00835 PUTLONG(PPPOL2TP_BEARERCAPABILITIES, p); /* Attribute value: Bearer capabilities */ 00836 00837 /* AVP - Host name */ 00838 PUTSHORT(PPPOL2TP_AVPHEADERFLAG_MANDATORY + 6+sizeof(PPPOL2TP_HOSTNAME)-1, p); /* Mandatory flag + len field */ 00839 PUTSHORT(0, p); /* Vendor ID */ 00840 PUTSHORT(PPPOL2TP_AVPTYPE_HOSTNAME, p); /* Attribute type: Hostname */ 00841 MEMCPY(p, PPPOL2TP_HOSTNAME, sizeof(PPPOL2TP_HOSTNAME)-1); /* Attribute value: Hostname */ 00842 INCPTR(sizeof(PPPOL2TP_HOSTNAME)-1, p); 00843 00844 /* AVP - Vendor name */ 00845 PUTSHORT(6+sizeof(PPPOL2TP_VENDORNAME)-1, p); /* len field */ 00846 PUTSHORT(0, p); /* Vendor ID */ 00847 PUTSHORT(PPPOL2TP_AVPTYPE_VENDORNAME, p); /* Attribute type: Vendor name */ 00848 MEMCPY(p, PPPOL2TP_VENDORNAME, sizeof(PPPOL2TP_VENDORNAME)-1); /* Attribute value: Vendor name */ 00849 INCPTR(sizeof(PPPOL2TP_VENDORNAME)-1, p); 00850 00851 /* AVP - Assign tunnel ID */ 00852 PUTSHORT(PPPOL2TP_AVPHEADERFLAG_MANDATORY + 8, p); /* Mandatory flag + len field */ 00853 PUTSHORT(0, p); /* Vendor ID */ 00854 PUTSHORT(PPPOL2TP_AVPTYPE_TUNNELID, p); /* Attribute type: Tunnel ID */ 00855 PUTSHORT(l2tp->remote_tunnel_id, p); /* Attribute value: Tunnel ID */ 00856 00857 /* AVP - Receive window size */ 00858 PUTSHORT(PPPOL2TP_AVPHEADERFLAG_MANDATORY + 8, p); /* Mandatory flag + len field */ 00859 PUTSHORT(0, p); /* Vendor ID */ 00860 PUTSHORT(PPPOL2TP_AVPTYPE_RECEIVEWINDOWSIZE, p); /* Attribute type: Receive window size */ 00861 PUTSHORT(PPPOL2TP_RECEIVEWINDOWSIZE, p); /* Attribute value: Receive window size */ 00862 00863 #if PPPOL2TP_AUTH_SUPPORT 00864 /* AVP - Challenge */ 00865 if (l2tp->secret != NULL) { 00866 PUTSHORT(PPPOL2TP_AVPHEADERFLAG_MANDATORY + 6 + sizeof(l2tp->secret_rv), p); /* Mandatory flag + len field */ 00867 PUTSHORT(0, p); /* Vendor ID */ 00868 PUTSHORT(PPPOL2TP_AVPTYPE_CHALLENGE, p); /* Attribute type: Challenge */ 00869 MEMCPY(p, l2tp->secret_rv, sizeof(l2tp->secret_rv)); /* Attribute value: Random vector */ 00870 INCPTR(sizeof(l2tp->secret_rv), p); 00871 } 00872 #endif /* PPPOL2TP_AUTH_SUPPORT */ 00873 00874 return pppol2tp_udp_send(l2tp, pb); 00875 } 00876 00877 /* Complete tunnel establishment */ 00878 static err_t pppol2tp_send_scccn(pppol2tp_pcb *l2tp, u16_t ns) { 00879 struct pbuf *pb; 00880 u8_t *p; 00881 u16_t len; 00882 00883 /* calculate UDP packet length */ 00884 len = 12 +8; 00885 #if PPPOL2TP_AUTH_SUPPORT 00886 if (l2tp->send_challenge) { 00887 len += 6 + sizeof(l2tp->challenge_hash); 00888 } 00889 #endif /* PPPOL2TP_AUTH_SUPPORT */ 00890 00891 /* allocate a buffer */ 00892 pb = pbuf_alloc(PBUF_TRANSPORT, len, PBUF_RAM); 00893 if (pb == NULL) { 00894 return ERR_MEM; 00895 } 00896 LWIP_ASSERT("pb->tot_len == pb->len", pb->tot_len == pb->len); 00897 00898 p = (u8_t*)pb->payload; 00899 /* fill in pkt */ 00900 /* L2TP control header */ 00901 PUTSHORT(PPPOL2TP_HEADERFLAG_CONTROL_MANDATORY, p); 00902 PUTSHORT(len, p); /* Length */ 00903 PUTSHORT(l2tp->source_tunnel_id, p); /* Tunnel Id */ 00904 PUTSHORT(0, p); /* Session Id */ 00905 PUTSHORT(ns, p); /* NS Sequence number - to peer */ 00906 PUTSHORT(l2tp->peer_ns+1, p); /* NR Sequence number - expected for peer */ 00907 00908 /* AVP - Message type */ 00909 PUTSHORT(PPPOL2TP_AVPHEADERFLAG_MANDATORY + 8, p); /* Mandatory flag + len field */ 00910 PUTSHORT(0, p); /* Vendor ID */ 00911 PUTSHORT(PPPOL2TP_AVPTYPE_MESSAGE, p); /* Attribute type: Message Type */ 00912 PUTSHORT(PPPOL2TP_MESSAGETYPE_SCCCN, p); /* Attribute value: Message type: SCCCN */ 00913 00914 #if PPPOL2TP_AUTH_SUPPORT 00915 /* AVP - Challenge response */ 00916 if (l2tp->send_challenge) { 00917 PUTSHORT(PPPOL2TP_AVPHEADERFLAG_MANDATORY + 6 + sizeof(l2tp->challenge_hash), p); /* Mandatory flag + len field */ 00918 PUTSHORT(0, p); /* Vendor ID */ 00919 PUTSHORT(PPPOL2TP_AVPTYPE_CHALLENGERESPONSE, p); /* Attribute type: Challenge response */ 00920 MEMCPY(p, l2tp->challenge_hash, sizeof(l2tp->challenge_hash)); /* Attribute value: Computed challenge */ 00921 INCPTR(sizeof(l2tp->challenge_hash), p); 00922 } 00923 #endif /* PPPOL2TP_AUTH_SUPPORT */ 00924 00925 return pppol2tp_udp_send(l2tp, pb); 00926 } 00927 00928 /* Initiate a new session */ 00929 static err_t pppol2tp_send_icrq(pppol2tp_pcb *l2tp, u16_t ns) { 00930 struct pbuf *pb; 00931 u8_t *p; 00932 u16_t len; 00933 u32_t serialnumber; 00934 00935 /* calculate UDP packet length */ 00936 len = 12 +8 +8 +10; 00937 00938 /* allocate a buffer */ 00939 pb = pbuf_alloc(PBUF_TRANSPORT, len, PBUF_RAM); 00940 if (pb == NULL) { 00941 return ERR_MEM; 00942 } 00943 LWIP_ASSERT("pb->tot_len == pb->len", pb->tot_len == pb->len); 00944 00945 p = (u8_t*)pb->payload; 00946 /* fill in pkt */ 00947 /* L2TP control header */ 00948 PUTSHORT(PPPOL2TP_HEADERFLAG_CONTROL_MANDATORY, p); 00949 PUTSHORT(len, p); /* Length */ 00950 PUTSHORT(l2tp->source_tunnel_id, p); /* Tunnel Id */ 00951 PUTSHORT(0, p); /* Session Id */ 00952 PUTSHORT(ns, p); /* NS Sequence number - to peer */ 00953 PUTSHORT(l2tp->peer_ns+1, p); /* NR Sequence number - expected for peer */ 00954 00955 /* AVP - Message type */ 00956 PUTSHORT(PPPOL2TP_AVPHEADERFLAG_MANDATORY + 8, p); /* Mandatory flag + len field */ 00957 PUTSHORT(0, p); /* Vendor ID */ 00958 PUTSHORT(PPPOL2TP_AVPTYPE_MESSAGE, p); /* Attribute type: Message Type */ 00959 PUTSHORT(PPPOL2TP_MESSAGETYPE_ICRQ, p); /* Attribute value: Message type: ICRQ */ 00960 00961 /* AVP - Assign session ID */ 00962 PUTSHORT(PPPOL2TP_AVPHEADERFLAG_MANDATORY + 8, p); /* Mandatory flag + len field */ 00963 PUTSHORT(0, p); /* Vendor ID */ 00964 PUTSHORT(PPPOL2TP_AVPTYPE_SESSIONID, p); /* Attribute type: Session ID */ 00965 PUTSHORT(l2tp->remote_session_id, p); /* Attribute value: Session ID */ 00966 00967 /* AVP - Call Serial Number */ 00968 PUTSHORT(PPPOL2TP_AVPHEADERFLAG_MANDATORY + 10, p); /* Mandatory flag + len field */ 00969 PUTSHORT(0, p); /* Vendor ID */ 00970 PUTSHORT(PPPOL2TP_AVPTYPE_CALLSERIALNUMBER, p); /* Attribute type: Serial number */ 00971 serialnumber = magic(); 00972 PUTLONG(serialnumber, p); /* Attribute value: Serial number */ 00973 00974 return pppol2tp_udp_send(l2tp, pb); 00975 } 00976 00977 /* Complete tunnel establishment */ 00978 static err_t pppol2tp_send_iccn(pppol2tp_pcb *l2tp, u16_t ns) { 00979 struct pbuf *pb; 00980 u8_t *p; 00981 u16_t len; 00982 00983 /* calculate UDP packet length */ 00984 len = 12 +8 +10 +10; 00985 00986 /* allocate a buffer */ 00987 pb = pbuf_alloc(PBUF_TRANSPORT, len, PBUF_RAM); 00988 if (pb == NULL) { 00989 return ERR_MEM; 00990 } 00991 LWIP_ASSERT("pb->tot_len == pb->len", pb->tot_len == pb->len); 00992 00993 p = (u8_t*)pb->payload; 00994 /* fill in pkt */ 00995 /* L2TP control header */ 00996 PUTSHORT(PPPOL2TP_HEADERFLAG_CONTROL_MANDATORY, p); 00997 PUTSHORT(len, p); /* Length */ 00998 PUTSHORT(l2tp->source_tunnel_id, p); /* Tunnel Id */ 00999 PUTSHORT(l2tp->source_session_id, p); /* Session Id */ 01000 PUTSHORT(ns, p); /* NS Sequence number - to peer */ 01001 PUTSHORT(l2tp->peer_ns+1, p); /* NR Sequence number - expected for peer */ 01002 01003 /* AVP - Message type */ 01004 PUTSHORT(PPPOL2TP_AVPHEADERFLAG_MANDATORY + 8, p); /* Mandatory flag + len field */ 01005 PUTSHORT(0, p); /* Vendor ID */ 01006 PUTSHORT(PPPOL2TP_AVPTYPE_MESSAGE, p); /* Attribute type: Message Type */ 01007 PUTSHORT(PPPOL2TP_MESSAGETYPE_ICCN, p); /* Attribute value: Message type: ICCN */ 01008 01009 /* AVP - Framing type */ 01010 PUTSHORT(PPPOL2TP_AVPHEADERFLAG_MANDATORY + 10, p); /* Mandatory flag + len field */ 01011 PUTSHORT(0, p); /* Vendor ID */ 01012 PUTSHORT(PPPOL2TP_AVPTYPE_FRAMINGTYPE, p); /* Attribute type: Framing type */ 01013 PUTLONG(PPPOL2TP_FRAMINGTYPE, p); /* Attribute value: Framing type */ 01014 01015 /* AVP - TX Connect speed */ 01016 PUTSHORT(PPPOL2TP_AVPHEADERFLAG_MANDATORY + 10, p); /* Mandatory flag + len field */ 01017 PUTSHORT(0, p); /* Vendor ID */ 01018 PUTSHORT(PPPOL2TP_AVPTYPE_TXCONNECTSPEED, p); /* Attribute type: TX Connect speed */ 01019 PUTLONG(PPPOL2TP_TXCONNECTSPEED, p); /* Attribute value: TX Connect speed */ 01020 01021 return pppol2tp_udp_send(l2tp, pb); 01022 } 01023 01024 /* Send a ZLB ACK packet */ 01025 static err_t pppol2tp_send_zlb(pppol2tp_pcb *l2tp, u16_t ns) { 01026 struct pbuf *pb; 01027 u8_t *p; 01028 u16_t len; 01029 01030 /* calculate UDP packet length */ 01031 len = 12; 01032 01033 /* allocate a buffer */ 01034 pb = pbuf_alloc(PBUF_TRANSPORT, len, PBUF_RAM); 01035 if (pb == NULL) { 01036 return ERR_MEM; 01037 } 01038 LWIP_ASSERT("pb->tot_len == pb->len", pb->tot_len == pb->len); 01039 01040 p = (u8_t*)pb->payload; 01041 /* fill in pkt */ 01042 /* L2TP control header */ 01043 PUTSHORT(PPPOL2TP_HEADERFLAG_CONTROL_MANDATORY, p); 01044 PUTSHORT(len, p); /* Length */ 01045 PUTSHORT(l2tp->source_tunnel_id, p); /* Tunnel Id */ 01046 PUTSHORT(0, p); /* Session Id */ 01047 PUTSHORT(ns, p); /* NS Sequence number - to peer */ 01048 PUTSHORT(l2tp->peer_ns+1, p); /* NR Sequence number - expected for peer */ 01049 01050 return pppol2tp_udp_send(l2tp, pb); 01051 } 01052 01053 /* Send a StopCCN packet */ 01054 static err_t pppol2tp_send_stopccn(pppol2tp_pcb *l2tp, u16_t ns) { 01055 struct pbuf *pb; 01056 u8_t *p; 01057 u16_t len; 01058 01059 /* calculate UDP packet length */ 01060 len = 12 +8 +8 +8; 01061 01062 /* allocate a buffer */ 01063 pb = pbuf_alloc(PBUF_TRANSPORT, len, PBUF_RAM); 01064 if (pb == NULL) { 01065 return ERR_MEM; 01066 } 01067 LWIP_ASSERT("pb->tot_len == pb->len", pb->tot_len == pb->len); 01068 01069 p = (u8_t*)pb->payload; 01070 /* fill in pkt */ 01071 /* L2TP control header */ 01072 PUTSHORT(PPPOL2TP_HEADERFLAG_CONTROL_MANDATORY, p); 01073 PUTSHORT(len, p); /* Length */ 01074 PUTSHORT(l2tp->source_tunnel_id, p); /* Tunnel Id */ 01075 PUTSHORT(0, p); /* Session Id */ 01076 PUTSHORT(ns, p); /* NS Sequence number - to peer */ 01077 PUTSHORT(l2tp->peer_ns+1, p); /* NR Sequence number - expected for peer */ 01078 01079 /* AVP - Message type */ 01080 PUTSHORT(PPPOL2TP_AVPHEADERFLAG_MANDATORY + 8, p); /* Mandatory flag + len field */ 01081 PUTSHORT(0, p); /* Vendor ID */ 01082 PUTSHORT(PPPOL2TP_AVPTYPE_MESSAGE, p); /* Attribute type: Message Type */ 01083 PUTSHORT(PPPOL2TP_MESSAGETYPE_STOPCCN, p); /* Attribute value: Message type: StopCCN */ 01084 01085 /* AVP - Assign tunnel ID */ 01086 PUTSHORT(PPPOL2TP_AVPHEADERFLAG_MANDATORY + 8, p); /* Mandatory flag + len field */ 01087 PUTSHORT(0, p); /* Vendor ID */ 01088 PUTSHORT(PPPOL2TP_AVPTYPE_TUNNELID, p); /* Attribute type: Tunnel ID */ 01089 PUTSHORT(l2tp->remote_tunnel_id, p); /* Attribute value: Tunnel ID */ 01090 01091 /* AVP - Result code */ 01092 PUTSHORT(PPPOL2TP_AVPHEADERFLAG_MANDATORY + 8, p); /* Mandatory flag + len field */ 01093 PUTSHORT(0, p); /* Vendor ID */ 01094 PUTSHORT(PPPOL2TP_AVPTYPE_RESULTCODE, p); /* Attribute type: Result code */ 01095 PUTSHORT(PPPOL2TP_RESULTCODE, p); /* Attribute value: Result code */ 01096 01097 return pppol2tp_udp_send(l2tp, pb); 01098 } 01099 01100 static err_t pppol2tp_xmit(pppol2tp_pcb *l2tp, struct pbuf *pb) { 01101 u8_t *p; 01102 01103 /* make room for L2TP header - should not fail */ 01104 if (pbuf_header(pb, (s16_t)PPPOL2TP_OUTPUT_DATA_HEADER_LEN) != 0) { 01105 /* bail out */ 01106 PPPDEBUG(LOG_ERR, ("pppol2tp: pppol2tp_pcb: could not allocate room for L2TP header\n")); 01107 LINK_STATS_INC(link.lenerr); 01108 pbuf_free(pb); 01109 return ERR_BUF; 01110 } 01111 01112 p = (u8_t*)pb->payload; 01113 PUTSHORT(PPPOL2TP_HEADERFLAG_DATA_MANDATORY, p); 01114 PUTSHORT(l2tp->source_tunnel_id, p); /* Tunnel Id */ 01115 PUTSHORT(l2tp->source_session_id, p); /* Session Id */ 01116 01117 return pppol2tp_udp_send(l2tp, pb); 01118 } 01119 01120 static err_t pppol2tp_udp_send(pppol2tp_pcb *l2tp, struct pbuf *pb) { 01121 err_t err; 01122 if (l2tp->netif) { 01123 err = udp_sendto_if(l2tp->udp, pb, &l2tp->remote_ip, l2tp->tunnel_port, l2tp->netif); 01124 } else { 01125 err = udp_sendto(l2tp->udp, pb, &l2tp->remote_ip, l2tp->tunnel_port); 01126 } 01127 pbuf_free(pb); 01128 return err; 01129 } 01130 01131 #endif /* PPP_SUPPORT && PPPOL2TP_SUPPORT */
Generated on Sun Jul 17 2022 08:25:25 by 1.7.2