72 #include "netif/ppp/ppp_opts.h" 73 #if PPP_SUPPORT && PPPOL2TP_SUPPORT 81 #include "netif/ppp/ppp_impl.h" 82 #include "netif/ppp/lcp.h" 83 #include "netif/ppp/ipcp.h" 85 #include "netif/ppp/pppcrypt.h" 86 #include "netif/ppp/magic.h" 89 LWIP_MEMPOOL_DECLARE(PPPOL2TP_PCB, MEMP_NUM_PPPOL2TP_INTERFACES,
sizeof(pppol2tp_pcb),
"PPPOL2TP_PCB")
92 static
err_t pppol2tp_write(ppp_pcb *ppp,
void *ctx, struct
pbuf *p);
93 static
err_t pppol2tp_netif_output(ppp_pcb *ppp,
void *ctx, struct
pbuf *p, u_short protocol);
94 static
err_t pppol2tp_destroy(ppp_pcb *ppp,
void *ctx);
95 static
void pppol2tp_connect(ppp_pcb *ppp,
void *ctx);
96 static
void pppol2tp_disconnect(ppp_pcb *ppp,
void *ctx);
99 static
void pppol2tp_input(
void *arg, struct udp_pcb *pcb, struct
pbuf *p, const ip_addr_t *addr, u16_t port);
100 static
void pppol2tp_dispatch_control_packet(pppol2tp_pcb *l2tp, u16_t port, struct
pbuf *p, u16_t ns, u16_t nr);
101 static
void pppol2tp_timeout(
void *arg);
102 static
void pppol2tp_abort_connect(pppol2tp_pcb *l2tp);
103 static
err_t pppol2tp_send_sccrq(pppol2tp_pcb *l2tp);
104 static
err_t pppol2tp_send_scccn(pppol2tp_pcb *l2tp, u16_t ns);
105 static
err_t pppol2tp_send_icrq(pppol2tp_pcb *l2tp, u16_t ns);
106 static
err_t pppol2tp_send_iccn(pppol2tp_pcb *l2tp, u16_t ns);
107 static
err_t pppol2tp_send_zlb(pppol2tp_pcb *l2tp, u16_t ns);
108 static
err_t pppol2tp_send_stopccn(pppol2tp_pcb *l2tp, u16_t ns);
109 static
err_t pppol2tp_xmit(pppol2tp_pcb *l2tp, struct
pbuf *pb);
110 static
err_t pppol2tp_udp_send(pppol2tp_pcb *l2tp, struct
pbuf *pb);
113 static const struct link_callbacks pppol2tp_callbacks = {
121 pppol2tp_netif_output,
128 ppp_pcb *pppol2tp_create(
struct netif *pppif,
129 struct netif *
netif,
const ip_addr_t *ipaddr, u16_t port,
130 const u8_t *secret, u8_t secret_len,
131 ppp_link_status_cb_fn link_status_cb,
void *ctx_cb) {
135 #if !PPPOL2TP_AUTH_SUPPORT 140 if (ipaddr == NULL) {
141 goto ipaddr_check_failed;
146 goto memp_malloc_l2tp_failed;
149 udp = udp_new_ip_type(IP_GET_TYPE(ipaddr));
153 udp_recv(udp, pppol2tp_input, l2tp);
155 ppp = ppp_new(pppif, &pppol2tp_callbacks, l2tp, link_status_cb, ctx_cb);
160 memset(l2tp, 0,
sizeof(pppol2tp_pcb));
161 l2tp->phase = PPPOL2TP_STATE_INITIAL;
165 ip_addr_copy(l2tp->remote_ip, *ipaddr);
166 l2tp->remote_port = port;
167 #if PPPOL2TP_AUTH_SUPPORT 168 l2tp->secret = secret;
169 l2tp->secret_len = secret_len;
178 memp_malloc_l2tp_failed:
184 static err_t pppol2tp_write(ppp_pcb *ppp,
void *ctx,
struct pbuf *p) {
185 pppol2tp_pcb *l2tp = (pppol2tp_pcb *)ctx;
196 LINK_STATS_INC(link.memerr);
197 LINK_STATS_INC(link.proterr);
198 MIB2_STATS_NETIF_INC(ppp->netif, ifoutdiscards);
203 pbuf_header(ph, -(s16_t)PPPOL2TP_OUTPUT_DATA_HEADER_LEN);
209 ret = pppol2tp_xmit(l2tp, ph);
211 LINK_STATS_INC(link.err);
212 MIB2_STATS_NETIF_INC(ppp->netif, ifoutdiscards);
216 MIB2_STATS_NETIF_ADD(ppp->netif, ifoutoctets, (u16_t)tot_len);
217 MIB2_STATS_NETIF_INC(ppp->netif, ifoutucastpkts);
218 LINK_STATS_INC(link.xmit);
223 static err_t pppol2tp_netif_output(ppp_pcb *ppp,
void *ctx,
struct pbuf *p, u_short protocol) {
224 pppol2tp_pcb *l2tp = (pppol2tp_pcb *)ctx;
237 LINK_STATS_INC(link.memerr);
238 LINK_STATS_INC(link.proterr);
239 MIB2_STATS_NETIF_INC(ppp->netif, ifoutdiscards);
243 pbuf_header(pb, -(s16_t)PPPOL2TP_OUTPUT_DATA_HEADER_LEN);
246 PUTSHORT(protocol, pl);
253 if( (err = pppol2tp_xmit(l2tp, pb)) !=
ERR_OK) {
254 LINK_STATS_INC(link.err);
255 MIB2_STATS_NETIF_INC(ppp->netif, ifoutdiscards);
259 MIB2_STATS_NETIF_ADD(ppp->netif, ifoutoctets, tot_len);
260 MIB2_STATS_NETIF_INC(ppp->netif, ifoutucastpkts);
261 LINK_STATS_INC(link.xmit);
266 static err_t pppol2tp_destroy(ppp_pcb *ppp,
void *ctx) {
267 pppol2tp_pcb *l2tp = (pppol2tp_pcb *)ctx;
270 sys_untimeout(pppol2tp_timeout, l2tp);
271 udp_remove(l2tp->udp);
277 static void pppol2tp_connect(ppp_pcb *ppp,
void *ctx) {
279 pppol2tp_pcb *l2tp = (pppol2tp_pcb *)ctx;
282 #if PPP_IPV4_SUPPORT && VJ_SUPPORT 283 ipcp_options *ipcp_wo;
284 ipcp_options *ipcp_ao;
287 l2tp->tunnel_port = l2tp->remote_port;
291 l2tp->source_tunnel_id = 0;
292 l2tp->remote_tunnel_id = 0;
293 l2tp->source_session_id = 0;
294 l2tp->remote_session_id = 0;
297 lcp_wo = &ppp->lcp_wantoptions;
298 lcp_wo->mru = PPPOL2TP_DEFMRU;
299 lcp_wo->neg_asyncmap = 0;
300 lcp_wo->neg_pcompression = 0;
301 lcp_wo->neg_accompression = 0;
305 lcp_ao = &ppp->lcp_allowoptions;
306 lcp_ao->mru = PPPOL2TP_DEFMRU;
307 lcp_ao->neg_asyncmap = 0;
308 lcp_ao->neg_pcompression = 0;
309 lcp_ao->neg_accompression = 0;
311 #if PPP_IPV4_SUPPORT && VJ_SUPPORT 312 ipcp_wo = &ppp->ipcp_wantoptions;
316 ipcp_ao = &ppp->ipcp_allowoptions;
325 if (IP_IS_V6_VAL(l2tp->udp->local_ip)) {
326 udp_bind(l2tp->udp, IP6_ADDR_ANY, 0);
329 udp_bind(l2tp->udp, IP_ADDR_ANY, 0);
331 #if PPPOL2TP_AUTH_SUPPORT 333 if (l2tp->secret != NULL) {
334 magic_random_bytes(l2tp->secret_rv,
sizeof(l2tp->secret_rv));
339 l2tp->remote_tunnel_id = magic();
340 }
while(l2tp->remote_tunnel_id == 0);
342 l2tp->sccrq_retried = 0;
343 l2tp->phase = PPPOL2TP_STATE_SCCRQ_SENT;
344 if ((err = pppol2tp_send_sccrq(l2tp)) != 0) {
345 PPPDEBUG(LOG_DEBUG, (
"pppol2tp: failed to send SCCRQ, error=%d\n", err));
347 sys_timeout(PPPOL2TP_CONTROL_TIMEOUT, pppol2tp_timeout, l2tp);
351 static void pppol2tp_disconnect(ppp_pcb *ppp,
void *ctx) {
352 pppol2tp_pcb *l2tp = (pppol2tp_pcb *)ctx;
355 pppol2tp_send_stopccn(l2tp, l2tp->our_ns);
358 sys_untimeout(pppol2tp_timeout, l2tp);
359 l2tp->phase = PPPOL2TP_STATE_INITIAL;
364 static void pppol2tp_input(
void *arg,
struct udp_pcb *pcb,
struct pbuf *p,
const ip_addr_t *addr, u16_t port) {
365 pppol2tp_pcb *l2tp = (pppol2tp_pcb*)arg;
366 u16_t hflags, hlen,
len=0, tunnel_id=0, session_id=0, ns=0, nr=0, offset=0;
371 if (l2tp->phase < PPPOL2TP_STATE_SCCRQ_SENT) {
372 goto free_and_return;
375 if (!ip_addr_cmp(&l2tp->remote_ip, addr)) {
376 goto free_and_return;
380 if (l2tp->phase > PPPOL2TP_STATE_SCCRQ_SENT && l2tp->tunnel_port != port) {
381 goto free_and_return;
387 if (p->
len <
sizeof(hflags) +
sizeof(tunnel_id) +
sizeof(session_id) ) {
388 goto packet_too_short;
392 GETSHORT(hflags, inp);
394 if (hflags & PPPOL2TP_HEADERFLAG_CONTROL) {
396 if ( (hflags & PPPOL2TP_HEADERFLAG_CONTROL_MANDATORY) != PPPOL2TP_HEADERFLAG_CONTROL_MANDATORY ) {
397 PPPDEBUG(LOG_DEBUG, (
"pppol2tp: mandatory header flags for control packet not set\n"));
398 goto free_and_return;
401 if (hflags & PPPOL2TP_HEADERFLAG_CONTROL_FORBIDDEN) {
402 PPPDEBUG(LOG_DEBUG, (
"pppol2tp: forbidden header flags for control packet found\n"));
403 goto free_and_return;
407 if ( (hflags & PPPOL2TP_HEADERFLAG_DATA_MANDATORY) != PPPOL2TP_HEADERFLAG_DATA_MANDATORY) {
408 PPPDEBUG(LOG_DEBUG, (
"pppol2tp: mandatory header flags for data packet not set\n"));
409 goto free_and_return;
414 hlen =
sizeof(hflags) +
sizeof(tunnel_id) +
sizeof(session_id);
415 if (hflags & PPPOL2TP_HEADERFLAG_LENGTH) {
418 if (hflags & PPPOL2TP_HEADERFLAG_SEQUENCE) {
419 hlen +=
sizeof(ns) +
sizeof(nr);
421 if (hflags & PPPOL2TP_HEADERFLAG_OFFSET) {
422 hlen +=
sizeof(offset);
425 goto packet_too_short;
428 if (hflags & PPPOL2TP_HEADERFLAG_LENGTH) {
430 if (p->
len < len || len < hlen) {
431 goto packet_too_short;
434 GETSHORT(tunnel_id, inp);
435 GETSHORT(session_id, inp);
436 if (hflags & PPPOL2TP_HEADERFLAG_SEQUENCE) {
440 if (hflags & PPPOL2TP_HEADERFLAG_OFFSET) {
441 GETSHORT(offset, inp)
443 PPPDEBUG(LOG_DEBUG, (
"pppol2tp: strange packet received, offset=%d\n", offset));
444 goto free_and_return;
448 goto packet_too_short;
457 goto free_and_return;
461 PPPDEBUG(LOG_DEBUG, (
"pppol2tp: input packet, len=%"U16_F
", tunnel=%"U16_F
", session=%"U16_F
", ns=%"U16_F
", nr=%"U16_F
"\n",
462 len, tunnel_id, session_id, ns, nr));
465 if (hflags & PPPOL2TP_HEADERFLAG_CONTROL) {
466 pppol2tp_dispatch_control_packet(l2tp, port, p, ns, nr);
467 goto free_and_return;
471 if(l2tp->phase != PPPOL2TP_STATE_DATA) {
472 goto free_and_return;
474 if(tunnel_id != l2tp->remote_tunnel_id) {
475 PPPDEBUG(LOG_DEBUG, (
"pppol2tp: tunnel ID mismatch, assigned=%d, received=%d\n", l2tp->remote_tunnel_id, tunnel_id));
476 goto free_and_return;
478 if(session_id != l2tp->remote_session_id) {
479 PPPDEBUG(LOG_DEBUG, (
"pppol2tp: session ID mismatch, assigned=%d, received=%d\n", l2tp->remote_session_id, session_id));
480 goto free_and_return;
489 GETSHORT(hflags, inp);
490 if (hflags == 0xff03) {
495 ppp_input(l2tp->ppp, p);
499 PPPDEBUG(LOG_DEBUG, (
"pppol2tp: packet too short: %d\n", p->
len));
505 static void pppol2tp_dispatch_control_packet(pppol2tp_pcb *l2tp, u16_t port,
struct pbuf *p, u16_t ns, u16_t nr) {
507 u16_t avplen, avpflags, vendorid, attributetype, messagetype=0;
509 #if PPPOL2TP_AUTH_SUPPORT 510 lwip_md5_context md5_ctx;
512 u8_t challenge_id = 0;
520 if (l2tp->phase == PPPOL2TP_STATE_ICCN_SENT && l2tp->peer_nr > l2tp->our_ns) {
521 l2tp->phase = PPPOL2TP_STATE_DATA;
529 p = ppp_singlebuf(p);
533 if (p->
len <
sizeof(avpflags) +
sizeof(vendorid) +
sizeof(attributetype) ) {
534 goto packet_too_short;
536 GETSHORT(avpflags, inp);
537 avplen = avpflags & PPPOL2TP_AVPHEADERFLAG_LENGTHMASK;
539 if (p->
len < avplen || avplen <
sizeof(avpflags) +
sizeof(vendorid) +
sizeof(attributetype)) {
540 goto packet_too_short;
542 GETSHORT(vendorid, inp);
543 GETSHORT(attributetype, inp);
544 avplen -=
sizeof(avpflags) +
sizeof(vendorid) +
sizeof(attributetype);
547 if (messagetype == 0) {
548 if (attributetype != 0 || vendorid != 0 || avplen !=
sizeof(messagetype) ) {
549 PPPDEBUG(LOG_DEBUG, (
"pppol2tp: message type must be the first AVP\n"));
552 GETSHORT(messagetype, inp);
554 switch(messagetype) {
556 case PPPOL2TP_MESSAGETYPE_SCCRP:
558 if (l2tp->phase != PPPOL2TP_STATE_SCCRQ_SENT) {
563 case PPPOL2TP_MESSAGETYPE_ICRP:
565 if (l2tp->phase != PPPOL2TP_STATE_ICRQ_SENT) {
570 case PPPOL2TP_MESSAGETYPE_STOPCCN:
571 pppol2tp_send_zlb(l2tp, l2tp->our_ns);
572 if (l2tp->phase < PPPOL2TP_STATE_DATA) {
573 pppol2tp_abort_connect(l2tp);
574 }
else if (l2tp->phase == PPPOL2TP_STATE_DATA) {
593 switch (messagetype) {
595 case PPPOL2TP_MESSAGETYPE_SCCRP:
596 switch (attributetype) {
597 case PPPOL2TP_AVPTYPE_TUNNELID:
598 if (avplen !=
sizeof(l2tp->source_tunnel_id) ) {
599 PPPDEBUG(LOG_DEBUG, (
"pppol2tp: AVP Assign tunnel ID length check failed\n"));
602 GETSHORT(l2tp->source_tunnel_id, inp);
603 PPPDEBUG(LOG_DEBUG, (
"pppol2tp: Assigned tunnel ID %"U16_F
"\n", l2tp->source_tunnel_id));
605 #if PPPOL2TP_AUTH_SUPPORT 606 case PPPOL2TP_AVPTYPE_CHALLENGE:
608 PPPDEBUG(LOG_DEBUG, (
"pppol2tp: Challenge length check failed\n"));
611 if (l2tp->secret == NULL) {
612 PPPDEBUG(LOG_DEBUG, (
"pppol2tp: Received challenge from peer and no secret key available\n"));
613 pppol2tp_abort_connect(l2tp);
617 lwip_md5_init(&md5_ctx);
618 lwip_md5_starts(&md5_ctx);
619 challenge_id = PPPOL2TP_MESSAGETYPE_SCCCN;
620 lwip_md5_update(&md5_ctx, &challenge_id, 1);
621 lwip_md5_update(&md5_ctx, l2tp->secret, l2tp->secret_len);
622 lwip_md5_update(&md5_ctx, inp, avplen);
623 lwip_md5_finish(&md5_ctx, l2tp->challenge_hash);
624 lwip_md5_free(&md5_ctx);
625 l2tp->send_challenge = 1;
627 case PPPOL2TP_AVPTYPE_CHALLENGERESPONSE:
628 if (avplen != PPPOL2TP_AVPTYPE_CHALLENGERESPONSE_SIZE) {
629 PPPDEBUG(LOG_DEBUG, (
"pppol2tp: AVP Challenge Response length check failed\n"));
633 lwip_md5_init(&md5_ctx);
634 lwip_md5_starts(&md5_ctx);
635 challenge_id = PPPOL2TP_MESSAGETYPE_SCCRP;
636 lwip_md5_update(&md5_ctx, &challenge_id, 1);
637 lwip_md5_update(&md5_ctx, l2tp->secret, l2tp->secret_len);
638 lwip_md5_update(&md5_ctx, l2tp->secret_rv,
sizeof(l2tp->secret_rv));
639 lwip_md5_finish(&md5_ctx, md5_hash);
640 lwip_md5_free(&md5_ctx);
641 if ( memcmp(inp, md5_hash,
sizeof(md5_hash)) ) {
642 PPPDEBUG(LOG_DEBUG, (
"pppol2tp: Received challenge response from peer and secret key do not match\n"));
643 pppol2tp_abort_connect(l2tp);
653 case PPPOL2TP_MESSAGETYPE_ICRP:
654 switch (attributetype) {
655 case PPPOL2TP_AVPTYPE_SESSIONID:
656 if (avplen !=
sizeof(l2tp->source_session_id) ) {
657 PPPDEBUG(LOG_DEBUG, (
"pppol2tp: AVP Assign session ID length check failed\n"));
660 GETSHORT(l2tp->source_session_id, inp);
661 PPPDEBUG(LOG_DEBUG, (
"pppol2tp: Assigned session ID %"U16_F
"\n", l2tp->source_session_id));
676 if (
pbuf_header(p, -(s16_t)(avplen +
sizeof(avpflags) +
sizeof(vendorid) +
sizeof(attributetype)) ) != 0) {
681 switch(messagetype) {
683 case PPPOL2TP_MESSAGETYPE_SCCRP:
685 l2tp->remote_session_id = magic();
686 }
while(l2tp->remote_session_id == 0);
687 l2tp->tunnel_port = port;
688 l2tp->icrq_retried = 0;
689 l2tp->phase = PPPOL2TP_STATE_ICRQ_SENT;
691 if ((err = pppol2tp_send_scccn(l2tp, l2tp->our_ns)) != 0) {
692 PPPDEBUG(LOG_DEBUG, (
"pppol2tp: failed to send SCCCN, error=%d\n", err));
695 if ((err = pppol2tp_send_icrq(l2tp, l2tp->our_ns)) != 0) {
696 PPPDEBUG(LOG_DEBUG, (
"pppol2tp: failed to send ICRQ, error=%d\n", err));
698 sys_untimeout(pppol2tp_timeout, l2tp);
699 sys_timeout(PPPOL2TP_CONTROL_TIMEOUT, pppol2tp_timeout, l2tp);
702 case PPPOL2TP_MESSAGETYPE_ICRP:
703 l2tp->iccn_retried = 0;
704 l2tp->phase = PPPOL2TP_STATE_ICCN_SENT;
706 ppp_start(l2tp->ppp);
707 if ((err = pppol2tp_send_iccn(l2tp, l2tp->our_ns)) != 0) {
708 PPPDEBUG(LOG_DEBUG, (
"pppol2tp: failed to send ICCN, error=%d\n", err));
710 sys_untimeout(pppol2tp_timeout, l2tp);
711 sys_timeout(PPPOL2TP_CONTROL_TIMEOUT, pppol2tp_timeout, l2tp);
720 pppol2tp_send_zlb(l2tp, l2tp->our_ns);
723 PPPDEBUG(LOG_DEBUG, (
"pppol2tp: packet too short: %d\n", p->
len));
727 static void pppol2tp_timeout(
void *arg) {
728 pppol2tp_pcb *l2tp = (pppol2tp_pcb*)arg;
732 PPPDEBUG(LOG_DEBUG, (
"pppol2tp: timeout\n"));
734 switch (l2tp->phase) {
735 case PPPOL2TP_STATE_SCCRQ_SENT:
737 if (l2tp->sccrq_retried < 0xff) {
738 l2tp->sccrq_retried++;
740 if (!l2tp->ppp->settings.persist && l2tp->sccrq_retried >= PPPOL2TP_MAXSCCRQ) {
741 pppol2tp_abort_connect(l2tp);
744 retry_wait = LWIP_MIN(PPPOL2TP_CONTROL_TIMEOUT * l2tp->sccrq_retried, PPPOL2TP_SLOW_RETRY);
745 PPPDEBUG(LOG_DEBUG, (
"pppol2tp: sccrq_retried=%d\n", l2tp->sccrq_retried));
746 if ((err = pppol2tp_send_sccrq(l2tp)) != 0) {
747 l2tp->sccrq_retried--;
748 PPPDEBUG(LOG_DEBUG, (
"pppol2tp: failed to send SCCRQ, error=%d\n", err));
750 sys_timeout(retry_wait, pppol2tp_timeout, l2tp);
753 case PPPOL2TP_STATE_ICRQ_SENT:
754 l2tp->icrq_retried++;
755 if (l2tp->icrq_retried >= PPPOL2TP_MAXICRQ) {
756 pppol2tp_abort_connect(l2tp);
759 PPPDEBUG(LOG_DEBUG, (
"pppol2tp: icrq_retried=%d\n", l2tp->icrq_retried));
760 if (l2tp->peer_nr <= l2tp->our_ns -1) {
761 if ((err = pppol2tp_send_scccn(l2tp, l2tp->our_ns -1)) != 0) {
762 l2tp->icrq_retried--;
763 PPPDEBUG(LOG_DEBUG, (
"pppol2tp: failed to send SCCCN, error=%d\n", err));
764 sys_timeout(PPPOL2TP_CONTROL_TIMEOUT, pppol2tp_timeout, l2tp);
768 if ((err = pppol2tp_send_icrq(l2tp, l2tp->our_ns)) != 0) {
769 l2tp->icrq_retried--;
770 PPPDEBUG(LOG_DEBUG, (
"pppol2tp: failed to send ICRQ, error=%d\n", err));
772 sys_timeout(PPPOL2TP_CONTROL_TIMEOUT, pppol2tp_timeout, l2tp);
775 case PPPOL2TP_STATE_ICCN_SENT:
776 l2tp->iccn_retried++;
777 if (l2tp->iccn_retried >= PPPOL2TP_MAXICCN) {
778 pppol2tp_abort_connect(l2tp);
781 PPPDEBUG(LOG_DEBUG, (
"pppol2tp: iccn_retried=%d\n", l2tp->iccn_retried));
782 if ((err = pppol2tp_send_iccn(l2tp, l2tp->our_ns)) != 0) {
783 l2tp->iccn_retried--;
784 PPPDEBUG(LOG_DEBUG, (
"pppol2tp: failed to send ICCN, error=%d\n", err));
786 sys_timeout(PPPOL2TP_CONTROL_TIMEOUT, pppol2tp_timeout, l2tp);
795 static void pppol2tp_abort_connect(pppol2tp_pcb *l2tp) {
796 PPPDEBUG(LOG_DEBUG, (
"pppol2tp: could not establish connection\n"));
797 l2tp->phase = PPPOL2TP_STATE_INITIAL;
798 ppp_link_failed(l2tp->ppp);
802 static err_t pppol2tp_send_sccrq(pppol2tp_pcb *l2tp) {
808 len = 12 +8 +8 +10 +10 +6+
sizeof(PPPOL2TP_HOSTNAME)-1 +6+
sizeof(PPPOL2TP_VENDORNAME)-1 +8 +8;
809 #if PPPOL2TP_AUTH_SUPPORT 810 if (l2tp->secret != NULL) {
811 len += 6 +
sizeof(l2tp->secret_rv);
820 LWIP_ASSERT(
"pb->tot_len == pb->len", pb->
tot_len == pb->
len);
825 PUTSHORT(PPPOL2TP_HEADERFLAG_CONTROL_MANDATORY, p);
833 PUTSHORT(PPPOL2TP_AVPHEADERFLAG_MANDATORY + 8, p);
835 PUTSHORT(PPPOL2TP_AVPTYPE_MESSAGE, p);
836 PUTSHORT(PPPOL2TP_MESSAGETYPE_SCCRQ, p);
839 PUTSHORT(PPPOL2TP_AVPHEADERFLAG_MANDATORY + 8, p);
841 PUTSHORT(PPPOL2TP_AVPTYPE_VERSION, p);
842 PUTSHORT(PPPOL2TP_VERSION, p);
845 PUTSHORT(PPPOL2TP_AVPHEADERFLAG_MANDATORY + 10, p);
847 PUTSHORT(PPPOL2TP_AVPTYPE_FRAMINGCAPABILITIES, p);
848 PUTLONG(PPPOL2TP_FRAMINGCAPABILITIES, p);
851 PUTSHORT(PPPOL2TP_AVPHEADERFLAG_MANDATORY + 10, p);
853 PUTSHORT(PPPOL2TP_AVPTYPE_BEARERCAPABILITIES, p);
854 PUTLONG(PPPOL2TP_BEARERCAPABILITIES, p);
857 PUTSHORT(PPPOL2TP_AVPHEADERFLAG_MANDATORY + 6+
sizeof(PPPOL2TP_HOSTNAME)-1, p);
859 PUTSHORT(PPPOL2TP_AVPTYPE_HOSTNAME, p);
860 MEMCPY(p, PPPOL2TP_HOSTNAME,
sizeof(PPPOL2TP_HOSTNAME)-1);
861 INCPTR(
sizeof(PPPOL2TP_HOSTNAME)-1, p);
864 PUTSHORT(6+
sizeof(PPPOL2TP_VENDORNAME)-1, p);
866 PUTSHORT(PPPOL2TP_AVPTYPE_VENDORNAME, p);
867 MEMCPY(p, PPPOL2TP_VENDORNAME,
sizeof(PPPOL2TP_VENDORNAME)-1);
868 INCPTR(
sizeof(PPPOL2TP_VENDORNAME)-1, p);
871 PUTSHORT(PPPOL2TP_AVPHEADERFLAG_MANDATORY + 8, p);
873 PUTSHORT(PPPOL2TP_AVPTYPE_TUNNELID, p);
874 PUTSHORT(l2tp->remote_tunnel_id, p);
877 PUTSHORT(PPPOL2TP_AVPHEADERFLAG_MANDATORY + 8, p);
879 PUTSHORT(PPPOL2TP_AVPTYPE_RECEIVEWINDOWSIZE, p);
880 PUTSHORT(PPPOL2TP_RECEIVEWINDOWSIZE, p);
882 #if PPPOL2TP_AUTH_SUPPORT 884 if (l2tp->secret != NULL) {
885 PUTSHORT(PPPOL2TP_AVPHEADERFLAG_MANDATORY + 6 +
sizeof(l2tp->secret_rv), p);
887 PUTSHORT(PPPOL2TP_AVPTYPE_CHALLENGE, p);
888 MEMCPY(p, l2tp->secret_rv,
sizeof(l2tp->secret_rv));
889 INCPTR(
sizeof(l2tp->secret_rv), p);
893 return pppol2tp_udp_send(l2tp, pb);
897 static err_t pppol2tp_send_scccn(pppol2tp_pcb *l2tp, u16_t ns) {
904 #if PPPOL2TP_AUTH_SUPPORT 905 if (l2tp->send_challenge) {
906 len += 6 +
sizeof(l2tp->challenge_hash);
915 LWIP_ASSERT(
"pb->tot_len == pb->len", pb->
tot_len == pb->
len);
920 PUTSHORT(PPPOL2TP_HEADERFLAG_CONTROL_MANDATORY, p);
922 PUTSHORT(l2tp->source_tunnel_id, p);
925 PUTSHORT(l2tp->peer_ns+1, p);
928 PUTSHORT(PPPOL2TP_AVPHEADERFLAG_MANDATORY + 8, p);
930 PUTSHORT(PPPOL2TP_AVPTYPE_MESSAGE, p);
931 PUTSHORT(PPPOL2TP_MESSAGETYPE_SCCCN, p);
933 #if PPPOL2TP_AUTH_SUPPORT 935 if (l2tp->send_challenge) {
936 PUTSHORT(PPPOL2TP_AVPHEADERFLAG_MANDATORY + 6 +
sizeof(l2tp->challenge_hash), p);
938 PUTSHORT(PPPOL2TP_AVPTYPE_CHALLENGERESPONSE, p);
939 MEMCPY(p, l2tp->challenge_hash,
sizeof(l2tp->challenge_hash));
940 INCPTR(
sizeof(l2tp->challenge_hash), p);
944 return pppol2tp_udp_send(l2tp, pb);
948 static err_t pppol2tp_send_icrq(pppol2tp_pcb *l2tp, u16_t ns) {
962 LWIP_ASSERT(
"pb->tot_len == pb->len", pb->
tot_len == pb->
len);
967 PUTSHORT(PPPOL2TP_HEADERFLAG_CONTROL_MANDATORY, p);
969 PUTSHORT(l2tp->source_tunnel_id, p);
972 PUTSHORT(l2tp->peer_ns+1, p);
975 PUTSHORT(PPPOL2TP_AVPHEADERFLAG_MANDATORY + 8, p);
977 PUTSHORT(PPPOL2TP_AVPTYPE_MESSAGE, p);
978 PUTSHORT(PPPOL2TP_MESSAGETYPE_ICRQ, p);
981 PUTSHORT(PPPOL2TP_AVPHEADERFLAG_MANDATORY + 8, p);
983 PUTSHORT(PPPOL2TP_AVPTYPE_SESSIONID, p);
984 PUTSHORT(l2tp->remote_session_id, p);
987 PUTSHORT(PPPOL2TP_AVPHEADERFLAG_MANDATORY + 10, p);
989 PUTSHORT(PPPOL2TP_AVPTYPE_CALLSERIALNUMBER, p);
990 serialnumber = magic();
991 PUTLONG(serialnumber, p);
993 return pppol2tp_udp_send(l2tp, pb);
997 static err_t pppol2tp_send_iccn(pppol2tp_pcb *l2tp, u16_t ns) {
1003 len = 12 +8 +10 +10;
1010 LWIP_ASSERT(
"pb->tot_len == pb->len", pb->
tot_len == pb->
len);
1015 PUTSHORT(PPPOL2TP_HEADERFLAG_CONTROL_MANDATORY, p);
1017 PUTSHORT(l2tp->source_tunnel_id, p);
1018 PUTSHORT(l2tp->source_session_id, p);
1020 PUTSHORT(l2tp->peer_ns+1, p);
1023 PUTSHORT(PPPOL2TP_AVPHEADERFLAG_MANDATORY + 8, p);
1025 PUTSHORT(PPPOL2TP_AVPTYPE_MESSAGE, p);
1026 PUTSHORT(PPPOL2TP_MESSAGETYPE_ICCN, p);
1029 PUTSHORT(PPPOL2TP_AVPHEADERFLAG_MANDATORY + 10, p);
1031 PUTSHORT(PPPOL2TP_AVPTYPE_FRAMINGTYPE, p);
1032 PUTLONG(PPPOL2TP_FRAMINGTYPE, p);
1035 PUTSHORT(PPPOL2TP_AVPHEADERFLAG_MANDATORY + 10, p);
1037 PUTSHORT(PPPOL2TP_AVPTYPE_TXCONNECTSPEED, p);
1038 PUTLONG(PPPOL2TP_TXCONNECTSPEED, p);
1040 return pppol2tp_udp_send(l2tp, pb);
1044 static err_t pppol2tp_send_zlb(pppol2tp_pcb *l2tp, u16_t ns) {
1057 LWIP_ASSERT(
"pb->tot_len == pb->len", pb->
tot_len == pb->
len);
1062 PUTSHORT(PPPOL2TP_HEADERFLAG_CONTROL_MANDATORY, p);
1064 PUTSHORT(l2tp->source_tunnel_id, p);
1067 PUTSHORT(l2tp->peer_ns+1, p);
1069 return pppol2tp_udp_send(l2tp, pb);
1073 static err_t pppol2tp_send_stopccn(pppol2tp_pcb *l2tp, u16_t ns) {
1086 LWIP_ASSERT(
"pb->tot_len == pb->len", pb->
tot_len == pb->
len);
1091 PUTSHORT(PPPOL2TP_HEADERFLAG_CONTROL_MANDATORY, p);
1093 PUTSHORT(l2tp->source_tunnel_id, p);
1096 PUTSHORT(l2tp->peer_ns+1, p);
1099 PUTSHORT(PPPOL2TP_AVPHEADERFLAG_MANDATORY + 8, p);
1101 PUTSHORT(PPPOL2TP_AVPTYPE_MESSAGE, p);
1102 PUTSHORT(PPPOL2TP_MESSAGETYPE_STOPCCN, p);
1105 PUTSHORT(PPPOL2TP_AVPHEADERFLAG_MANDATORY + 8, p);
1107 PUTSHORT(PPPOL2TP_AVPTYPE_TUNNELID, p);
1108 PUTSHORT(l2tp->remote_tunnel_id, p);
1111 PUTSHORT(PPPOL2TP_AVPHEADERFLAG_MANDATORY + 8, p);
1113 PUTSHORT(PPPOL2TP_AVPTYPE_RESULTCODE, p);
1114 PUTSHORT(PPPOL2TP_RESULTCODE, p);
1116 return pppol2tp_udp_send(l2tp, pb);
1119 static err_t pppol2tp_xmit(pppol2tp_pcb *l2tp,
struct pbuf *pb) {
1123 if (
pbuf_header(pb, (s16_t)PPPOL2TP_OUTPUT_DATA_HEADER_LEN) != 0) {
1125 PPPDEBUG(LOG_ERR, (
"pppol2tp: pppol2tp_pcb: could not allocate room for L2TP header\n"));
1126 LINK_STATS_INC(link.lenerr);
1132 PUTSHORT(PPPOL2TP_HEADERFLAG_DATA_MANDATORY, p);
1133 PUTSHORT(l2tp->source_tunnel_id, p);
1134 PUTSHORT(l2tp->source_session_id, p);
1136 return pppol2tp_udp_send(l2tp, pb);
1139 static err_t pppol2tp_udp_send(pppol2tp_pcb *l2tp,
struct pbuf *pb) {
1142 err = udp_sendto_if(l2tp->udp, pb, &l2tp->remote_ip, l2tp->tunnel_port, l2tp->netif);
1144 err = udp_sendto(l2tp->udp, pb, &l2tp->remote_ip, l2tp->tunnel_port);
#define LWIP_MEMPOOL_ALLOC(name)
u8_t pbuf_header(struct pbuf *p, s16_t header_size_increment)
#define LWIP_MEMPOOL_FREE(name, x)
void pbuf_chain(struct pbuf *h, struct pbuf *t)
#define LWIP_MEMPOOL_DECLARE(name, num, size, desc)
void pbuf_cat(struct pbuf *h, struct pbuf *t)
struct pbuf * pbuf_alloc(pbuf_layer layer, u16_t length, pbuf_type type)
#define LWIP_UNUSED_ARG(x)
u8_t pbuf_free(struct pbuf *p)