STM32F769IDiscovery  1.00
uDANTE Audio Networking with STM32F7 DISCO board
pppol2tp.c
Go to the documentation of this file.
1 
7 /*
8  * Redistribution and use in source and binary forms, with or without modification,
9  * are permitted provided that the following conditions are met:
10  *
11  * 1. Redistributions of source code must retain the above copyright notice,
12  * this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright notice,
14  * this list of conditions and the following disclaimer in the documentation
15  * and/or other materials provided with the distribution.
16  * 3. The name of the author may not be used to endorse or promote products
17  * derived from this software without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
20  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
22  * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
24  * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
27  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
28  * OF SUCH DAMAGE.
29  *
30  * This file is part of the lwIP TCP/IP stack.
31  *
32  */
33 
34 /*
35  * L2TP Support status:
36  *
37  * Supported:
38  * - L2TPv2 (PPP over L2TP, a.k.a. UDP tunnels)
39  * - LAC
40  *
41  * Not supported:
42  * - LNS (require PPP server support)
43  * - L2TPv3 ethernet pseudowires
44  * - L2TPv3 VLAN pseudowire
45  * - L2TPv3 PPP pseudowires
46  * - L2TPv3 IP encapsulation
47  * - L2TPv3 IP pseudowire
48  * - L2TP tunnel switching - http://tools.ietf.org/html/draft-ietf-l2tpext-tunnel-switching-08
49  * - Multiple tunnels per UDP socket, as well as multiple sessions per tunnel
50  * - Hidden AVPs
51  */
52 
53 #include "lwip/opt.h"
54 #if PPP_SUPPORT && PPPOL2TP_SUPPORT /* don't build if not configured for use in lwipopts.h */
55 
56 #include "lwip/err.h"
57 #include "lwip/memp.h"
58 #include "lwip/netif.h"
59 #include "lwip/udp.h"
60 #include "lwip/snmp.h"
61 
62 #include "netif/ppp/ppp_impl.h"
63 #include "netif/ppp/lcp.h"
64 #include "netif/ppp/ipcp.h"
65 #include "netif/ppp/pppol2tp.h"
66 
67 #include "netif/ppp/magic.h"
68 
69 #if PPPOL2TP_AUTH_SUPPORT
70 #if LWIP_INCLUDED_POLARSSL_MD5
71 #include "netif/ppp/polarssl/md5.h"
72 #else
73 #include "polarssl/md5.h"
74 #endif
75 #endif /* PPPOL2TP_AUTH_SUPPORT */
76 
77 /* callbacks called from PPP core */
78 static err_t pppol2tp_write(ppp_pcb *ppp, void *ctx, struct pbuf *p);
79 static err_t pppol2tp_netif_output(ppp_pcb *ppp, void *ctx, struct pbuf *p, u_short protocol);
80 static err_t pppol2tp_destroy(ppp_pcb *ppp, void *ctx); /* Destroy a L2TP control block */
81 static err_t pppol2tp_connect(ppp_pcb *ppp, void *ctx); /* Be a LAC, connect to a LNS. */
82 static void pppol2tp_disconnect(ppp_pcb *ppp, void *ctx); /* Disconnect */
83 
84  /* Prototypes for procedures local to this file. */
85 static void pppol2tp_input(void *arg, struct udp_pcb *pcb, struct pbuf *p, const ip_addr_t *addr, u16_t port);
86 static void pppol2tp_dispatch_control_packet(pppol2tp_pcb *l2tp, u16_t port, struct pbuf *p, u16_t ns, u16_t nr);
87 static void pppol2tp_timeout(void *arg);
88 static void pppol2tp_abort_connect(pppol2tp_pcb *l2tp);
89 static void pppol2tp_clear(pppol2tp_pcb *l2tp);
90 static err_t pppol2tp_send_sccrq(pppol2tp_pcb *l2tp);
91 static err_t pppol2tp_send_scccn(pppol2tp_pcb *l2tp, u16_t ns);
92 static err_t pppol2tp_send_icrq(pppol2tp_pcb *l2tp, u16_t ns);
93 static err_t pppol2tp_send_iccn(pppol2tp_pcb *l2tp, u16_t ns);
94 static err_t pppol2tp_send_zlb(pppol2tp_pcb *l2tp, u16_t ns);
95 static err_t pppol2tp_send_stopccn(pppol2tp_pcb *l2tp, u16_t ns);
96 static err_t pppol2tp_xmit(pppol2tp_pcb *l2tp, struct pbuf *pb);
97 static err_t pppol2tp_udp_send(pppol2tp_pcb *l2tp, struct pbuf *pb);
98 
99 /* Callbacks structure for PPP core */
100 static const struct link_callbacks pppol2tp_callbacks = {
101  pppol2tp_connect,
102 #if PPP_SERVER
103  NULL,
104 #endif /* PPP_SERVER */
105  pppol2tp_disconnect,
106  pppol2tp_destroy,
107  pppol2tp_write,
108  pppol2tp_netif_output,
109  NULL,
110  NULL
111 };
112 
113 
114 /* Create a new L2TP session. */
115 ppp_pcb *pppol2tp_create(struct netif *pppif,
116  struct netif *netif, ip_addr_t *ipaddr, u16_t port,
117  const u8_t *secret, u8_t secret_len,
118  ppp_link_status_cb_fn link_status_cb, void *ctx_cb) {
119  ppp_pcb *ppp;
120  pppol2tp_pcb *l2tp;
121  struct udp_pcb *udp;
122 
123  if (ipaddr == NULL) {
124  goto ipaddr_check_failed;
125  }
126 
127  l2tp = (pppol2tp_pcb *)memp_malloc(MEMP_PPPOL2TP_PCB);
128  if (l2tp == NULL) {
129  goto memp_malloc_l2tp_failed;
130  }
131 
132 #if LWIP_IPV6
133  if (IP_IS_V6_VAL(*ipaddr)) {
134  udp = udp_new_ip6();
135  } else
136 #endif /* LWIP_IPV6 */
137  udp = udp_new();
138  if (udp == NULL) {
139  goto udp_new_failed;
140  }
141  udp_recv(udp, pppol2tp_input, l2tp);
142 
143  ppp = ppp_new(pppif, &pppol2tp_callbacks, l2tp, link_status_cb, ctx_cb);
144  if (ppp == NULL) {
145  goto ppp_new_failed;
146  }
147 
148  memset(l2tp, 0, sizeof(pppol2tp_pcb));
149  l2tp->phase = PPPOL2TP_STATE_INITIAL;
150  l2tp->ppp = ppp;
151  l2tp->udp = udp;
152  l2tp->netif = netif;
153  ip_addr_copy(l2tp->remote_ip, *ipaddr);
154  l2tp->remote_port = port;
155 #if PPPOL2TP_AUTH_SUPPORT
156  l2tp->secret = secret;
157  l2tp->secret_len = secret_len;
158 #endif /* PPPOL2TP_AUTH_SUPPORT */
159 
160  return ppp;
161 
162 ppp_new_failed:
163  udp_remove(udp);
164 udp_new_failed:
165  memp_free(MEMP_PPPOL2TP_PCB, l2tp);
166 memp_malloc_l2tp_failed:
167 ipaddr_check_failed:
168  return NULL;
169 }
170 
171 /* Called by PPP core */
172 static err_t pppol2tp_write(ppp_pcb *ppp, void *ctx, struct pbuf *p) {
173  pppol2tp_pcb *l2tp = (pppol2tp_pcb *)ctx;
174  struct pbuf *ph; /* UDP + L2TP header */
175  err_t ret;
176 #if MIB2_STATS
177  u16_t tot_len;
178 #else /* MIB2_STATS */
179  LWIP_UNUSED_ARG(ppp);
180 #endif /* MIB2_STATS */
181 
182  ph = pbuf_alloc(PBUF_TRANSPORT, (u16_t)(PPPOL2TP_OUTPUT_DATA_HEADER_LEN), PBUF_RAM);
183  if(!ph) {
184  LINK_STATS_INC(link.memerr);
185  LINK_STATS_INC(link.proterr);
186  MIB2_STATS_NETIF_INC(ppp->netif, ifoutdiscards);
187  pbuf_free(p);
188  return ERR_MEM;
189  }
190 
191  pbuf_header(ph, -(s16_t)PPPOL2TP_OUTPUT_DATA_HEADER_LEN); /* hide L2TP header */
192  pbuf_cat(ph, p);
193 #if MIB2_STATS
194  tot_len = ph->tot_len;
195 #endif /* MIB2_STATS */
196 
197  ret = pppol2tp_xmit(l2tp, ph);
198  if (ret != ERR_OK) {
199  LINK_STATS_INC(link.err);
200  MIB2_STATS_NETIF_INC(ppp->netif, ifoutdiscards);
201  return ret;
202  }
203 
204  MIB2_STATS_NETIF_ADD(ppp->netif, ifoutoctets, (u16_t)tot_len);
205  MIB2_STATS_NETIF_INC(ppp->netif, ifoutucastpkts);
206  LINK_STATS_INC(link.xmit);
207  return ERR_OK;
208 }
209 
210 /* Called by PPP core */
211 static err_t pppol2tp_netif_output(ppp_pcb *ppp, void *ctx, struct pbuf *p, u_short protocol) {
212  pppol2tp_pcb *l2tp = (pppol2tp_pcb *)ctx;
213  struct pbuf *pb;
214  u8_t *pl;
215  err_t err;
216 #if MIB2_STATS
217  u16_t tot_len;
218 #else /* MIB2_STATS */
219  LWIP_UNUSED_ARG(ppp);
220 #endif /* MIB2_STATS */
221 
222  /* @todo: try to use pbuf_header() here! */
223  pb = pbuf_alloc(PBUF_TRANSPORT, PPPOL2TP_OUTPUT_DATA_HEADER_LEN + sizeof(protocol), PBUF_RAM);
224  if(!pb) {
225  LINK_STATS_INC(link.memerr);
226  LINK_STATS_INC(link.proterr);
227  MIB2_STATS_NETIF_INC(ppp->netif, ifoutdiscards);
228  return ERR_MEM;
229  }
230 
231  pbuf_header(pb, -(s16_t)PPPOL2TP_OUTPUT_DATA_HEADER_LEN);
232 
233  pl = (u8_t*)pb->payload;
234  PUTSHORT(protocol, pl);
235 
236  pbuf_chain(pb, p);
237 #if MIB2_STATS
238  tot_len = pb->tot_len;
239 #endif /* MIB2_STATS */
240 
241  if( (err = pppol2tp_xmit(l2tp, pb)) != ERR_OK) {
242  LINK_STATS_INC(link.err);
243  MIB2_STATS_NETIF_INC(ppp->netif, ifoutdiscards);
244  return err;
245  }
246 
247  MIB2_STATS_NETIF_ADD(ppp->netif, ifoutoctets, tot_len);
248  MIB2_STATS_NETIF_INC(ppp->netif, ifoutucastpkts);
249  LINK_STATS_INC(link.xmit);
250  return ERR_OK;
251 }
252 
253 /* Destroy a L2TP control block */
254 static err_t pppol2tp_destroy(ppp_pcb *ppp, void *ctx) {
255  pppol2tp_pcb *l2tp = (pppol2tp_pcb *)ctx;
256  LWIP_UNUSED_ARG(ppp);
257 
258  sys_untimeout(pppol2tp_timeout, l2tp);
259  if (l2tp->udp != NULL) {
260  udp_remove(l2tp->udp);
261  }
262  memp_free(MEMP_PPPOL2TP_PCB, l2tp);
263  return ERR_OK;
264 }
265 
266 /* Be a LAC, connect to a LNS. */
267 static err_t pppol2tp_connect(ppp_pcb *ppp, void *ctx) {
268  err_t err;
269  pppol2tp_pcb *l2tp = (pppol2tp_pcb *)ctx;
270  lcp_options *lcp_wo;
271  lcp_options *lcp_ao;
272 #if PPP_IPV4_SUPPORT && VJ_SUPPORT
273  ipcp_options *ipcp_wo;
274  ipcp_options *ipcp_ao;
275 #endif /* PPP_IPV4_SUPPORT && VJ_SUPPORT */
276 
277  if (l2tp->phase != PPPOL2TP_STATE_INITIAL) {
278  return ERR_VAL;
279  }
280 
281  pppol2tp_clear(l2tp);
282 
283  ppp_clear(ppp);
284 
285  lcp_wo = &ppp->lcp_wantoptions;
286  lcp_wo->mru = PPPOL2TP_DEFMRU;
287  lcp_wo->neg_asyncmap = 0;
288  lcp_wo->neg_pcompression = 0;
289  lcp_wo->neg_accompression = 0;
290 
291  lcp_ao = &ppp->lcp_allowoptions;
292  lcp_ao->mru = PPPOL2TP_DEFMRU;
293  lcp_ao->neg_asyncmap = 0;
294  lcp_ao->neg_pcompression = 0;
295  lcp_ao->neg_accompression = 0;
296 
297 #if PPP_IPV4_SUPPORT && VJ_SUPPORT
298  ipcp_wo = &ppp->ipcp_wantoptions;
299  ipcp_wo->neg_vj = 0;
300  ipcp_wo->old_vj = 0;
301 
302  ipcp_ao = &ppp->ipcp_allowoptions;
303  ipcp_ao->neg_vj = 0;
304  ipcp_ao->old_vj = 0;
305 #endif /* PPP_IPV4_SUPPORT && VJ_SUPPORT */
306 
307  /* Listen to a random source port, we need to do that instead of using udp_connect()
308  * because the L2TP LNS might answer with its own random source port (!= 1701)
309  */
310 #if LWIP_IPV6
311  if (PCB_ISIPV6(l2tp->udp)) {
312  udp_bind(l2tp->udp, IP6_ADDR_ANY, 0);
313  } else
314 #endif /* LWIP_IPV6 */
315  udp_bind(l2tp->udp, IP_ADDR_ANY, 0);
316 
317 #if PPPOL2TP_AUTH_SUPPORT
318  /* Generate random vector */
319  if (l2tp->secret != NULL) {
320  magic_random_bytes(l2tp->secret_rv, sizeof(l2tp->secret_rv));
321  }
322 #endif /* PPPOL2TP_AUTH_SUPPORT */
323 
324  do {
325  l2tp->remote_tunnel_id = magic();
326  } while(l2tp->remote_tunnel_id == 0);
327  /* save state, in case we fail to send SCCRQ */
328  l2tp->sccrq_retried = 0;
329  l2tp->phase = PPPOL2TP_STATE_SCCRQ_SENT;
330  if ((err = pppol2tp_send_sccrq(l2tp)) != 0) {
331  PPPDEBUG(LOG_DEBUG, ("pppol2tp: failed to send SCCRQ, error=%d\n", err));
332  }
333  sys_timeout(PPPOL2TP_CONTROL_TIMEOUT, pppol2tp_timeout, l2tp);
334  return err;
335 }
336 
337 /* Disconnect */
338 static void pppol2tp_disconnect(ppp_pcb *ppp, void *ctx) {
339  pppol2tp_pcb *l2tp = (pppol2tp_pcb *)ctx;
340 
341  if (l2tp->phase < PPPOL2TP_STATE_DATA) {
342  return;
343  }
344 
345  l2tp->our_ns++;
346  pppol2tp_send_stopccn(l2tp, l2tp->our_ns);
347 
348  pppol2tp_clear(l2tp);
349  ppp_link_end(ppp); /* notify upper layers */
350 }
351 
352 /* UDP Callback for incoming IPv4 L2TP frames */
353 static void pppol2tp_input(void *arg, struct udp_pcb *pcb, struct pbuf *p, const ip_addr_t *addr, u16_t port) {
354  pppol2tp_pcb *l2tp = (pppol2tp_pcb*)arg;
355  u16_t hflags, hlen, len=0, tunnel_id=0, session_id=0, ns=0, nr=0, offset=0;
356  u8_t *inp;
357  LWIP_UNUSED_ARG(pcb);
358 
359  if (l2tp->phase < PPPOL2TP_STATE_SCCRQ_SENT) {
360  goto free_and_return;
361  }
362 
363  if (!ip_addr_cmp(&l2tp->remote_ip, addr)) {
364  goto free_and_return;
365  }
366 
367  /* discard packet if port mismatch, but only if we received a SCCRP */
368  if (l2tp->phase > PPPOL2TP_STATE_SCCRQ_SENT && l2tp->tunnel_port != port) {
369  goto free_and_return;
370  }
371 
372  /* printf("-----------\nL2TP INPUT, %d\n", p->len); */
373 
374  /* L2TP header */
375  if (p->len < sizeof(hflags) + sizeof(tunnel_id) + sizeof(session_id) ) {
376  goto packet_too_short;
377  }
378 
379  inp = (u8_t*)p->payload;
380  GETSHORT(hflags, inp);
381 
382  if (hflags & PPPOL2TP_HEADERFLAG_CONTROL) {
383  /* check mandatory flags for a control packet */
384  if ( (hflags & PPPOL2TP_HEADERFLAG_CONTROL_MANDATORY) != PPPOL2TP_HEADERFLAG_CONTROL_MANDATORY ) {
385  PPPDEBUG(LOG_DEBUG, ("pppol2tp: mandatory header flags for control packet not set\n"));
386  goto free_and_return;
387  }
388  /* check forbidden flags for a control packet */
389  if (hflags & PPPOL2TP_HEADERFLAG_CONTROL_FORBIDDEN) {
390  PPPDEBUG(LOG_DEBUG, ("pppol2tp: forbidden header flags for control packet found\n"));
391  goto free_and_return;
392  }
393  } else {
394  /* check mandatory flags for a data packet */
395  if ( (hflags & PPPOL2TP_HEADERFLAG_DATA_MANDATORY) != PPPOL2TP_HEADERFLAG_DATA_MANDATORY) {
396  PPPDEBUG(LOG_DEBUG, ("pppol2tp: mandatory header flags for data packet not set\n"));
397  goto free_and_return;
398  }
399  }
400 
401  /* Expected header size */
402  hlen = sizeof(hflags) + sizeof(tunnel_id) + sizeof(session_id);
403  if (hflags & PPPOL2TP_HEADERFLAG_LENGTH) {
404  hlen += sizeof(len);
405  }
406  if (hflags & PPPOL2TP_HEADERFLAG_SEQUENCE) {
407  hlen += sizeof(ns) + sizeof(nr);
408  }
409  if (hflags & PPPOL2TP_HEADERFLAG_OFFSET) {
410  hlen += sizeof(offset);
411  }
412  if (p->len < hlen) {
413  goto packet_too_short;
414  }
415 
416  if (hflags & PPPOL2TP_HEADERFLAG_LENGTH) {
417  GETSHORT(len, inp);
418  if (p->len < len || len < hlen) {
419  goto packet_too_short;
420  }
421  }
422  GETSHORT(tunnel_id, inp);
423  GETSHORT(session_id, inp);
424  if (hflags & PPPOL2TP_HEADERFLAG_SEQUENCE) {
425  GETSHORT(ns, inp);
426  GETSHORT(nr, inp);
427  }
428  if (hflags & PPPOL2TP_HEADERFLAG_OFFSET) {
429  GETSHORT(offset, inp)
430  if (offset > 4096) { /* don't be fooled with large offset which might overflow hlen */
431  PPPDEBUG(LOG_DEBUG, ("pppol2tp: strange packet received, offset=%d\n", offset));
432  goto free_and_return;
433  }
434  hlen += offset;
435  if (p->len < hlen) {
436  goto packet_too_short;
437  }
438  INCPTR(offset, inp);
439  }
440 
441  /* printf("HLEN = %d\n", hlen); */
442 
443  /* skip L2TP header */
444  if (pbuf_header(p, -(s16_t)hlen) != 0) {
445  goto free_and_return;
446  }
447 
448  /* printf("LEN=%d, TUNNEL_ID=%d, SESSION_ID=%d, NS=%d, NR=%d, OFFSET=%d\n", len, tunnel_id, session_id, ns, nr, offset); */
449  PPPDEBUG(LOG_DEBUG, ("pppol2tp: input packet, len=%"U16_F", tunnel=%"U16_F", session=%"U16_F", ns=%"U16_F", nr=%"U16_F"\n",
450  len, tunnel_id, session_id, ns, nr));
451 
452  /* Control packet */
453  if (hflags & PPPOL2TP_HEADERFLAG_CONTROL) {
454  pppol2tp_dispatch_control_packet(l2tp, port, p, ns, nr);
455  goto free_and_return;
456  }
457 
458  /* Data packet */
459  if(l2tp->phase != PPPOL2TP_STATE_DATA) {
460  goto free_and_return;
461  }
462  if(tunnel_id != l2tp->remote_tunnel_id) {
463  PPPDEBUG(LOG_DEBUG, ("pppol2tp: tunnel ID mismatch, assigned=%d, received=%d\n", l2tp->remote_tunnel_id, tunnel_id));
464  goto free_and_return;
465  }
466  if(session_id != l2tp->remote_session_id) {
467  PPPDEBUG(LOG_DEBUG, ("pppol2tp: session ID mismatch, assigned=%d, received=%d\n", l2tp->remote_session_id, session_id));
468  goto free_and_return;
469  }
470  /*
471  * skip address & flags if necessary
472  *
473  * RFC 2661 does not specify whether the PPP frame in the L2TP payload should
474  * have a HDLC header or not. We handle both cases for compatibility.
475  */
476  if (p->len >= 2) {
477  GETSHORT(hflags, inp);
478  if (hflags == 0xff03) {
479  pbuf_header(p, -(s16_t)2);
480  }
481  }
482  /* Dispatch the packet thereby consuming it. */
483  ppp_input(l2tp->ppp, p);
484  return;
485 
486 packet_too_short:
487  PPPDEBUG(LOG_DEBUG, ("pppol2tp: packet too short: %d\n", p->len));
488 free_and_return:
489  pbuf_free(p);
490 }
491 
492 /* L2TP Control packet entry point */
493 static void pppol2tp_dispatch_control_packet(pppol2tp_pcb *l2tp, u16_t port, struct pbuf *p, u16_t ns, u16_t nr) {
494  u8_t *inp;
495  u16_t avplen, avpflags, vendorid, attributetype, messagetype=0;
496  err_t err;
497 #if PPPOL2TP_AUTH_SUPPORT
498  md5_context md5_ctx;
499  u8_t md5_hash[16];
500  u8_t challenge_id = 0;
501 #endif /* PPPOL2TP_AUTH_SUPPORT */
502 
503  l2tp->peer_nr = nr;
504  l2tp->peer_ns = ns;
505  /* printf("L2TP CTRL INPUT, ns=%d, nr=%d, len=%d\n", ns, nr, p->len); */
506 
507  /* Handle the special case of the ICCN acknowledge */
508  if (l2tp->phase == PPPOL2TP_STATE_ICCN_SENT && l2tp->peer_nr > l2tp->our_ns) {
509  l2tp->phase = PPPOL2TP_STATE_DATA;
510  }
511 
512  /* ZLB packets */
513  if (p->tot_len == 0) {
514  return;
515  }
516 
517  p = ppp_singlebuf(p);
518  inp = (u8_t*)p->payload;
519  /* Decode AVPs */
520  while (p->len > 0) {
521  if (p->len < sizeof(avpflags) + sizeof(vendorid) + sizeof(attributetype) ) {
522  goto packet_too_short;
523  }
524  GETSHORT(avpflags, inp);
525  avplen = avpflags & PPPOL2TP_AVPHEADERFLAG_LENGTHMASK;
526  /* printf("AVPLEN = %d\n", avplen); */
527  if (p->len < avplen || avplen < sizeof(avpflags) + sizeof(vendorid) + sizeof(attributetype)) {
528  goto packet_too_short;
529  }
530  GETSHORT(vendorid, inp);
531  GETSHORT(attributetype, inp);
532  avplen -= sizeof(avpflags) + sizeof(vendorid) + sizeof(attributetype);
533 
534  /* Message type must be the first AVP */
535  if (messagetype == 0) {
536  if (attributetype != 0 || vendorid != 0 || avplen != sizeof(messagetype) ) {
537  PPPDEBUG(LOG_DEBUG, ("pppol2tp: message type must be the first AVP\n"));
538  return;
539  }
540  GETSHORT(messagetype, inp);
541  /* printf("Message type = %d\n", messagetype); */
542  switch(messagetype) {
543  /* Start Control Connection Reply */
544  case PPPOL2TP_MESSAGETYPE_SCCRP:
545  /* Only accept SCCRP packet if we sent a SCCRQ */
546  if (l2tp->phase != PPPOL2TP_STATE_SCCRQ_SENT) {
547  goto send_zlb;
548  }
549  break;
550  /* Incoming Call Reply */
551  case PPPOL2TP_MESSAGETYPE_ICRP:
552  /* Only accept ICRP packet if we sent a IRCQ */
553  if (l2tp->phase != PPPOL2TP_STATE_ICRQ_SENT) {
554  goto send_zlb;
555  }
556  break;
557  /* Stop Control Connection Notification */
558  case PPPOL2TP_MESSAGETYPE_STOPCCN:
559  pppol2tp_send_zlb(l2tp, l2tp->our_ns); /* Ack the StopCCN before we switch to down state */
560  if (l2tp->phase < PPPOL2TP_STATE_DATA) {
561  pppol2tp_abort_connect(l2tp);
562  } else if (l2tp->phase == PPPOL2TP_STATE_DATA) {
563  /* Don't disconnect here, we let the LCP Echo/Reply find the fact
564  * that PPP session is down. Asking the PPP stack to end the session
565  * require strict checking about the PPP phase to prevent endless
566  * disconnection loops.
567  */
568  }
569  return;
570  default:
571  break;
572  }
573  goto nextavp;
574  }
575 
576  /* Skip proprietary L2TP extensions */
577  if (vendorid != 0) {
578  goto skipavp;
579  }
580 
581  switch (messagetype) {
582  /* Start Control Connection Reply */
583  case PPPOL2TP_MESSAGETYPE_SCCRP:
584  switch (attributetype) {
585  case PPPOL2TP_AVPTYPE_TUNNELID:
586  if (avplen != sizeof(l2tp->source_tunnel_id) ) {
587  PPPDEBUG(LOG_DEBUG, ("pppol2tp: AVP Assign tunnel ID length check failed\n"));
588  return;
589  }
590  GETSHORT(l2tp->source_tunnel_id, inp);
591  PPPDEBUG(LOG_DEBUG, ("pppol2tp: Assigned tunnel ID %"U16_F"\n", l2tp->source_tunnel_id));
592  goto nextavp;
593 #if PPPOL2TP_AUTH_SUPPORT
594  case PPPOL2TP_AVPTYPE_CHALLENGE:
595  if (avplen == 0) {
596  PPPDEBUG(LOG_DEBUG, ("pppol2tp: Challenge length check failed\n"));
597  return;
598  }
599  if (l2tp->secret == NULL) {
600  PPPDEBUG(LOG_DEBUG, ("pppol2tp: Received challenge from peer and no secret key available\n"));
601  pppol2tp_abort_connect(l2tp);
602  return;
603  }
604  /* Generate hash of ID, secret, challenge */
605  md5_starts(&md5_ctx);
606  challenge_id = PPPOL2TP_MESSAGETYPE_SCCCN;
607  md5_update(&md5_ctx, &challenge_id, 1);
608  md5_update(&md5_ctx, l2tp->secret, l2tp->secret_len);
609  md5_update(&md5_ctx, inp, avplen);
610  md5_finish(&md5_ctx, l2tp->challenge_hash);
611  l2tp->send_challenge = 1;
612  goto skipavp;
613  case PPPOL2TP_AVPTYPE_CHALLENGERESPONSE:
614  if (avplen != PPPOL2TP_AVPTYPE_CHALLENGERESPONSE_SIZE) {
615  PPPDEBUG(LOG_DEBUG, ("pppol2tp: AVP Challenge Response length check failed\n"));
616  return;
617  }
618  /* Generate hash of ID, secret, challenge */
619  md5_starts(&md5_ctx);
620  challenge_id = PPPOL2TP_MESSAGETYPE_SCCRP;
621  md5_update(&md5_ctx, &challenge_id, 1);
622  md5_update(&md5_ctx, l2tp->secret, l2tp->secret_len);
623  md5_update(&md5_ctx, l2tp->secret_rv, sizeof(l2tp->secret_rv));
624  md5_finish(&md5_ctx, md5_hash);
625  if ( memcmp(inp, md5_hash, sizeof(md5_hash)) ) {
626  PPPDEBUG(LOG_DEBUG, ("pppol2tp: Received challenge response from peer and secret key do not match\n"));
627  pppol2tp_abort_connect(l2tp);
628  return;
629  }
630  goto skipavp;
631 #endif /* PPPOL2TP_AUTH_SUPPORT */
632  default:
633  break;
634  }
635  break;
636  /* Incoming Call Reply */
637  case PPPOL2TP_MESSAGETYPE_ICRP:
638  switch (attributetype) {
639  case PPPOL2TP_AVPTYPE_SESSIONID:
640  if (avplen != sizeof(l2tp->source_session_id) ) {
641  PPPDEBUG(LOG_DEBUG, ("pppol2tp: AVP Assign session ID length check failed\n"));
642  return;
643  }
644  GETSHORT(l2tp->source_session_id, inp);
645  PPPDEBUG(LOG_DEBUG, ("pppol2tp: Assigned session ID %"U16_F"\n", l2tp->source_session_id));
646  goto nextavp;
647  default:
648  break;
649  }
650  break;
651  default:
652  break;
653  }
654 
655 skipavp:
656  INCPTR(avplen, inp);
657 nextavp:
658  /* printf("AVP Found, vendor=%d, attribute=%d, len=%d\n", vendorid, attributetype, avplen); */
659  /* next AVP */
660  if (pbuf_header(p, -(s16_t)(avplen + sizeof(avpflags) + sizeof(vendorid) + sizeof(attributetype)) ) != 0) {
661  return;
662  }
663  }
664 
665  switch(messagetype) {
666  /* Start Control Connection Reply */
667  case PPPOL2TP_MESSAGETYPE_SCCRP:
668  do {
669  l2tp->remote_session_id = magic();
670  } while(l2tp->remote_session_id == 0);
671  l2tp->tunnel_port = port; /* LNS server might have chosen its own local port */
672  l2tp->icrq_retried = 0;
673  l2tp->phase = PPPOL2TP_STATE_ICRQ_SENT;
674  l2tp->our_ns++;
675  if ((err = pppol2tp_send_scccn(l2tp, l2tp->our_ns)) != 0) {
676  PPPDEBUG(LOG_DEBUG, ("pppol2tp: failed to send SCCCN, error=%d\n", err));
677  }
678  l2tp->our_ns++;
679  if ((err = pppol2tp_send_icrq(l2tp, l2tp->our_ns)) != 0) {
680  PPPDEBUG(LOG_DEBUG, ("pppol2tp: failed to send ICRQ, error=%d\n", err));
681  }
682  sys_untimeout(pppol2tp_timeout, l2tp);
683  sys_timeout(PPPOL2TP_CONTROL_TIMEOUT, pppol2tp_timeout, l2tp);
684  break;
685  /* Incoming Call Reply */
686  case PPPOL2TP_MESSAGETYPE_ICRP:
687  l2tp->iccn_retried = 0;
688  l2tp->phase = PPPOL2TP_STATE_ICCN_SENT;
689  l2tp->our_ns++;
690  ppp_start(l2tp->ppp); /* notify upper layers */
691  if ((err = pppol2tp_send_iccn(l2tp, l2tp->our_ns)) != 0) {
692  PPPDEBUG(LOG_DEBUG, ("pppol2tp: failed to send ICCN, error=%d\n", err));
693  }
694  sys_untimeout(pppol2tp_timeout, l2tp);
695  sys_timeout(PPPOL2TP_CONTROL_TIMEOUT, pppol2tp_timeout, l2tp);
696  break;
697  /* Unhandled packet, send ZLB ACK */
698  default:
699  goto send_zlb;
700  }
701  return;
702 
703 send_zlb:
704  pppol2tp_send_zlb(l2tp, l2tp->our_ns);
705  return;
706 packet_too_short:
707  PPPDEBUG(LOG_DEBUG, ("pppol2tp: packet too short: %d\n", p->len));
708 }
709 
710 /* L2TP Timeout handler */
711 static void pppol2tp_timeout(void *arg) {
712  pppol2tp_pcb *l2tp = (pppol2tp_pcb*)arg;
713  err_t err;
714  u32_t retry_wait;
715 
716  PPPDEBUG(LOG_DEBUG, ("pppol2tp: timeout\n"));
717 
718  switch (l2tp->phase) {
719  case PPPOL2TP_STATE_SCCRQ_SENT:
720  /* backoff wait */
721  if (l2tp->sccrq_retried < 0xff) {
722  l2tp->sccrq_retried++;
723  }
724  if (!l2tp->ppp->settings.persist && l2tp->sccrq_retried >= PPPOL2TP_MAXSCCRQ) {
725  pppol2tp_abort_connect(l2tp);
726  return;
727  }
728  retry_wait = LWIP_MIN(PPPOL2TP_CONTROL_TIMEOUT * l2tp->sccrq_retried, PPPOL2TP_SLOW_RETRY);
729  PPPDEBUG(LOG_DEBUG, ("pppol2tp: sccrq_retried=%d\n", l2tp->sccrq_retried));
730  if ((err = pppol2tp_send_sccrq(l2tp)) != 0) {
731  l2tp->sccrq_retried--;
732  PPPDEBUG(LOG_DEBUG, ("pppol2tp: failed to send SCCRQ, error=%d\n", err));
733  }
734  sys_timeout(retry_wait, pppol2tp_timeout, l2tp);
735  break;
736 
737  case PPPOL2TP_STATE_ICRQ_SENT:
738  l2tp->icrq_retried++;
739  if (l2tp->icrq_retried >= PPPOL2TP_MAXICRQ) {
740  pppol2tp_abort_connect(l2tp);
741  return;
742  }
743  PPPDEBUG(LOG_DEBUG, ("pppol2tp: icrq_retried=%d\n", l2tp->icrq_retried));
744  if (l2tp->peer_nr <= l2tp->our_ns -1) { /* the SCCCN was not acknowledged */
745  if ((err = pppol2tp_send_scccn(l2tp, l2tp->our_ns -1)) != 0) {
746  l2tp->icrq_retried--;
747  PPPDEBUG(LOG_DEBUG, ("pppol2tp: failed to send SCCCN, error=%d\n", err));
748  sys_timeout(PPPOL2TP_CONTROL_TIMEOUT, pppol2tp_timeout, l2tp);
749  break;
750  }
751  }
752  if ((err = pppol2tp_send_icrq(l2tp, l2tp->our_ns)) != 0) {
753  l2tp->icrq_retried--;
754  PPPDEBUG(LOG_DEBUG, ("pppol2tp: failed to send ICRQ, error=%d\n", err));
755  }
756  sys_timeout(PPPOL2TP_CONTROL_TIMEOUT, pppol2tp_timeout, l2tp);
757  break;
758 
759  case PPPOL2TP_STATE_ICCN_SENT:
760  l2tp->iccn_retried++;
761  if (l2tp->iccn_retried >= PPPOL2TP_MAXICCN) {
762  pppol2tp_abort_connect(l2tp);
763  return;
764  }
765  PPPDEBUG(LOG_DEBUG, ("pppol2tp: iccn_retried=%d\n", l2tp->iccn_retried));
766  if ((err = pppol2tp_send_iccn(l2tp, l2tp->our_ns)) != 0) {
767  l2tp->iccn_retried--;
768  PPPDEBUG(LOG_DEBUG, ("pppol2tp: failed to send ICCN, error=%d\n", err));
769  }
770  sys_timeout(PPPOL2TP_CONTROL_TIMEOUT, pppol2tp_timeout, l2tp);
771  break;
772 
773  default:
774  return; /* all done, work in peace */
775  }
776 }
777 
778 /* Connection attempt aborted */
779 static void pppol2tp_abort_connect(pppol2tp_pcb *l2tp) {
780  PPPDEBUG(LOG_DEBUG, ("pppol2tp: could not establish connection\n"));
781  pppol2tp_clear(l2tp);
782  ppp_link_failed(l2tp->ppp); /* notify upper layers */
783 }
784 
785 /* Reset L2TP control block to its initial state */
786 static void pppol2tp_clear(pppol2tp_pcb *l2tp) {
787  /* stop any timer */
788  sys_untimeout(pppol2tp_timeout, l2tp);
789  l2tp->phase = PPPOL2TP_STATE_INITIAL;
790  l2tp->tunnel_port = l2tp->remote_port;
791  l2tp->our_ns = 0;
792  l2tp->peer_nr = 0;
793  l2tp->peer_ns = 0;
794  l2tp->source_tunnel_id = 0;
795  l2tp->remote_tunnel_id = 0;
796  l2tp->source_session_id = 0;
797  l2tp->remote_session_id = 0;
798  /* l2tp->*_retried are cleared when used */
799 }
800 
801 /* Initiate a new tunnel */
802 static err_t pppol2tp_send_sccrq(pppol2tp_pcb *l2tp) {
803  struct pbuf *pb;
804  u8_t *p;
805  u16_t len;
806 
807  /* calculate UDP packet length */
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);
812  }
813 #endif /* PPPOL2TP_AUTH_SUPPORT */
814 
815  /* allocate a buffer */
816  pb = pbuf_alloc(PBUF_TRANSPORT, len, PBUF_RAM);
817  if (pb == NULL) {
818  return ERR_MEM;
819  }
820  LWIP_ASSERT("pb->tot_len == pb->len", pb->tot_len == pb->len);
821 
822  p = (u8_t*)pb->payload;
823  /* fill in pkt */
824  /* L2TP control header */
825  PUTSHORT(PPPOL2TP_HEADERFLAG_CONTROL_MANDATORY, p);
826  PUTSHORT(len, p); /* Length */
827  PUTSHORT(0, p); /* Tunnel Id */
828  PUTSHORT(0, p); /* Session Id */
829  PUTSHORT(0, p); /* NS Sequence number - to peer */
830  PUTSHORT(0, p); /* NR Sequence number - expected for peer */
831 
832  /* AVP - Message type */
833  PUTSHORT(PPPOL2TP_AVPHEADERFLAG_MANDATORY + 8, p); /* Mandatory flag + len field */
834  PUTSHORT(0, p); /* Vendor ID */
835  PUTSHORT(PPPOL2TP_AVPTYPE_MESSAGE, p); /* Attribute type: Message Type */
836  PUTSHORT(PPPOL2TP_MESSAGETYPE_SCCRQ, p); /* Attribute value: Message type: SCCRQ */
837 
838  /* AVP - L2TP Version */
839  PUTSHORT(PPPOL2TP_AVPHEADERFLAG_MANDATORY + 8, p); /* Mandatory flag + len field */
840  PUTSHORT(0, p); /* Vendor ID */
841  PUTSHORT(PPPOL2TP_AVPTYPE_VERSION, p); /* Attribute type: Version */
842  PUTSHORT(PPPOL2TP_VERSION, p); /* Attribute value: L2TP Version */
843 
844  /* AVP - Framing capabilities */
845  PUTSHORT(PPPOL2TP_AVPHEADERFLAG_MANDATORY + 10, p); /* Mandatory flag + len field */
846  PUTSHORT(0, p); /* Vendor ID */
847  PUTSHORT(PPPOL2TP_AVPTYPE_FRAMINGCAPABILITIES, p); /* Attribute type: Framing capabilities */
848  PUTLONG(PPPOL2TP_FRAMINGCAPABILITIES, p); /* Attribute value: Framing capabilities */
849 
850  /* AVP - Bearer capabilities */
851  PUTSHORT(PPPOL2TP_AVPHEADERFLAG_MANDATORY + 10, p); /* Mandatory flag + len field */
852  PUTSHORT(0, p); /* Vendor ID */
853  PUTSHORT(PPPOL2TP_AVPTYPE_BEARERCAPABILITIES, p); /* Attribute type: Bearer capabilities */
854  PUTLONG(PPPOL2TP_BEARERCAPABILITIES, p); /* Attribute value: Bearer capabilities */
855 
856  /* AVP - Host name */
857  PUTSHORT(PPPOL2TP_AVPHEADERFLAG_MANDATORY + 6+sizeof(PPPOL2TP_HOSTNAME)-1, p); /* Mandatory flag + len field */
858  PUTSHORT(0, p); /* Vendor ID */
859  PUTSHORT(PPPOL2TP_AVPTYPE_HOSTNAME, p); /* Attribute type: Hostname */
860  MEMCPY(p, PPPOL2TP_HOSTNAME, sizeof(PPPOL2TP_HOSTNAME)-1); /* Attribute value: Hostname */
861  INCPTR(sizeof(PPPOL2TP_HOSTNAME)-1, p);
862 
863  /* AVP - Vendor name */
864  PUTSHORT(6+sizeof(PPPOL2TP_VENDORNAME)-1, p); /* len field */
865  PUTSHORT(0, p); /* Vendor ID */
866  PUTSHORT(PPPOL2TP_AVPTYPE_VENDORNAME, p); /* Attribute type: Vendor name */
867  MEMCPY(p, PPPOL2TP_VENDORNAME, sizeof(PPPOL2TP_VENDORNAME)-1); /* Attribute value: Vendor name */
868  INCPTR(sizeof(PPPOL2TP_VENDORNAME)-1, p);
869 
870  /* AVP - Assign tunnel ID */
871  PUTSHORT(PPPOL2TP_AVPHEADERFLAG_MANDATORY + 8, p); /* Mandatory flag + len field */
872  PUTSHORT(0, p); /* Vendor ID */
873  PUTSHORT(PPPOL2TP_AVPTYPE_TUNNELID, p); /* Attribute type: Tunnel ID */
874  PUTSHORT(l2tp->remote_tunnel_id, p); /* Attribute value: Tunnel ID */
875 
876  /* AVP - Receive window size */
877  PUTSHORT(PPPOL2TP_AVPHEADERFLAG_MANDATORY + 8, p); /* Mandatory flag + len field */
878  PUTSHORT(0, p); /* Vendor ID */
879  PUTSHORT(PPPOL2TP_AVPTYPE_RECEIVEWINDOWSIZE, p); /* Attribute type: Receive window size */
880  PUTSHORT(PPPOL2TP_RECEIVEWINDOWSIZE, p); /* Attribute value: Receive window size */
881 
882 #if PPPOL2TP_AUTH_SUPPORT
883  /* AVP - Challenge */
884  if (l2tp->secret != NULL) {
885  PUTSHORT(PPPOL2TP_AVPHEADERFLAG_MANDATORY + 6 + sizeof(l2tp->secret_rv), p); /* Mandatory flag + len field */
886  PUTSHORT(0, p); /* Vendor ID */
887  PUTSHORT(PPPOL2TP_AVPTYPE_CHALLENGE, p); /* Attribute type: Challenge */
888  MEMCPY(p, l2tp->secret_rv, sizeof(l2tp->secret_rv)); /* Attribute value: Random vector */
889  INCPTR(sizeof(l2tp->secret_rv), p);
890  }
891 #endif /* PPPOL2TP_AUTH_SUPPORT */
892 
893  return pppol2tp_udp_send(l2tp, pb);
894 }
895 
896 /* Complete tunnel establishment */
897 static err_t pppol2tp_send_scccn(pppol2tp_pcb *l2tp, u16_t ns) {
898  struct pbuf *pb;
899  u8_t *p;
900  u16_t len;
901 
902  /* calculate UDP packet length */
903  len = 12 +8;
904 #if PPPOL2TP_AUTH_SUPPORT
905  if (l2tp->send_challenge) {
906  len += 6 + sizeof(l2tp->challenge_hash);
907  }
908 #endif /* PPPOL2TP_AUTH_SUPPORT */
909 
910  /* allocate a buffer */
911  pb = pbuf_alloc(PBUF_TRANSPORT, len, PBUF_RAM);
912  if (pb == NULL) {
913  return ERR_MEM;
914  }
915  LWIP_ASSERT("pb->tot_len == pb->len", pb->tot_len == pb->len);
916 
917  p = (u8_t*)pb->payload;
918  /* fill in pkt */
919  /* L2TP control header */
920  PUTSHORT(PPPOL2TP_HEADERFLAG_CONTROL_MANDATORY, p);
921  PUTSHORT(len, p); /* Length */
922  PUTSHORT(l2tp->source_tunnel_id, p); /* Tunnel Id */
923  PUTSHORT(0, p); /* Session Id */
924  PUTSHORT(ns, p); /* NS Sequence number - to peer */
925  PUTSHORT(l2tp->peer_ns+1, p); /* NR Sequence number - expected for peer */
926 
927  /* AVP - Message type */
928  PUTSHORT(PPPOL2TP_AVPHEADERFLAG_MANDATORY + 8, p); /* Mandatory flag + len field */
929  PUTSHORT(0, p); /* Vendor ID */
930  PUTSHORT(PPPOL2TP_AVPTYPE_MESSAGE, p); /* Attribute type: Message Type */
931  PUTSHORT(PPPOL2TP_MESSAGETYPE_SCCCN, p); /* Attribute value: Message type: SCCCN */
932 
933 #if PPPOL2TP_AUTH_SUPPORT
934  /* AVP - Challenge response */
935  if (l2tp->send_challenge) {
936  PUTSHORT(PPPOL2TP_AVPHEADERFLAG_MANDATORY + 6 + sizeof(l2tp->challenge_hash), p); /* Mandatory flag + len field */
937  PUTSHORT(0, p); /* Vendor ID */
938  PUTSHORT(PPPOL2TP_AVPTYPE_CHALLENGERESPONSE, p); /* Attribute type: Challenge response */
939  MEMCPY(p, l2tp->challenge_hash, sizeof(l2tp->challenge_hash)); /* Attribute value: Computed challenge */
940  INCPTR(sizeof(l2tp->challenge_hash), p);
941  }
942 #endif /* PPPOL2TP_AUTH_SUPPORT */
943 
944  return pppol2tp_udp_send(l2tp, pb);
945 }
946 
947 /* Initiate a new session */
948 static err_t pppol2tp_send_icrq(pppol2tp_pcb *l2tp, u16_t ns) {
949  struct pbuf *pb;
950  u8_t *p;
951  u16_t len;
952  u32_t serialnumber;
953 
954  /* calculate UDP packet length */
955  len = 12 +8 +8 +10;
956 
957  /* allocate a buffer */
958  pb = pbuf_alloc(PBUF_TRANSPORT, len, PBUF_RAM);
959  if (pb == NULL) {
960  return ERR_MEM;
961  }
962  LWIP_ASSERT("pb->tot_len == pb->len", pb->tot_len == pb->len);
963 
964  p = (u8_t*)pb->payload;
965  /* fill in pkt */
966  /* L2TP control header */
967  PUTSHORT(PPPOL2TP_HEADERFLAG_CONTROL_MANDATORY, p);
968  PUTSHORT(len, p); /* Length */
969  PUTSHORT(l2tp->source_tunnel_id, p); /* Tunnel Id */
970  PUTSHORT(0, p); /* Session Id */
971  PUTSHORT(ns, p); /* NS Sequence number - to peer */
972  PUTSHORT(l2tp->peer_ns+1, p); /* NR Sequence number - expected for peer */
973 
974  /* AVP - Message type */
975  PUTSHORT(PPPOL2TP_AVPHEADERFLAG_MANDATORY + 8, p); /* Mandatory flag + len field */
976  PUTSHORT(0, p); /* Vendor ID */
977  PUTSHORT(PPPOL2TP_AVPTYPE_MESSAGE, p); /* Attribute type: Message Type */
978  PUTSHORT(PPPOL2TP_MESSAGETYPE_ICRQ, p); /* Attribute value: Message type: ICRQ */
979 
980  /* AVP - Assign session ID */
981  PUTSHORT(PPPOL2TP_AVPHEADERFLAG_MANDATORY + 8, p); /* Mandatory flag + len field */
982  PUTSHORT(0, p); /* Vendor ID */
983  PUTSHORT(PPPOL2TP_AVPTYPE_SESSIONID, p); /* Attribute type: Session ID */
984  PUTSHORT(l2tp->remote_session_id, p); /* Attribute value: Session ID */
985 
986  /* AVP - Call Serial Number */
987  PUTSHORT(PPPOL2TP_AVPHEADERFLAG_MANDATORY + 10, p); /* Mandatory flag + len field */
988  PUTSHORT(0, p); /* Vendor ID */
989  PUTSHORT(PPPOL2TP_AVPTYPE_CALLSERIALNUMBER, p); /* Attribute type: Serial number */
990  serialnumber = magic();
991  PUTLONG(serialnumber, p); /* Attribute value: Serial number */
992 
993  return pppol2tp_udp_send(l2tp, pb);
994 }
995 
996 /* Complete tunnel establishment */
997 static err_t pppol2tp_send_iccn(pppol2tp_pcb *l2tp, u16_t ns) {
998  struct pbuf *pb;
999  u8_t *p;
1000  u16_t len;
1001 
1002  /* calculate UDP packet length */
1003  len = 12 +8 +10 +10;
1004 
1005  /* allocate a buffer */
1006  pb = pbuf_alloc(PBUF_TRANSPORT, len, PBUF_RAM);
1007  if (pb == NULL) {
1008  return ERR_MEM;
1009  }
1010  LWIP_ASSERT("pb->tot_len == pb->len", pb->tot_len == pb->len);
1011 
1012  p = (u8_t*)pb->payload;
1013  /* fill in pkt */
1014  /* L2TP control header */
1015  PUTSHORT(PPPOL2TP_HEADERFLAG_CONTROL_MANDATORY, p);
1016  PUTSHORT(len, p); /* Length */
1017  PUTSHORT(l2tp->source_tunnel_id, p); /* Tunnel Id */
1018  PUTSHORT(l2tp->source_session_id, p); /* Session Id */
1019  PUTSHORT(ns, p); /* NS Sequence number - to peer */
1020  PUTSHORT(l2tp->peer_ns+1, p); /* NR Sequence number - expected for peer */
1021 
1022  /* AVP - Message type */
1023  PUTSHORT(PPPOL2TP_AVPHEADERFLAG_MANDATORY + 8, p); /* Mandatory flag + len field */
1024  PUTSHORT(0, p); /* Vendor ID */
1025  PUTSHORT(PPPOL2TP_AVPTYPE_MESSAGE, p); /* Attribute type: Message Type */
1026  PUTSHORT(PPPOL2TP_MESSAGETYPE_ICCN, p); /* Attribute value: Message type: ICCN */
1027 
1028  /* AVP - Framing type */
1029  PUTSHORT(PPPOL2TP_AVPHEADERFLAG_MANDATORY + 10, p); /* Mandatory flag + len field */
1030  PUTSHORT(0, p); /* Vendor ID */
1031  PUTSHORT(PPPOL2TP_AVPTYPE_FRAMINGTYPE, p); /* Attribute type: Framing type */
1032  PUTLONG(PPPOL2TP_FRAMINGTYPE, p); /* Attribute value: Framing type */
1033 
1034  /* AVP - TX Connect speed */
1035  PUTSHORT(PPPOL2TP_AVPHEADERFLAG_MANDATORY + 10, p); /* Mandatory flag + len field */
1036  PUTSHORT(0, p); /* Vendor ID */
1037  PUTSHORT(PPPOL2TP_AVPTYPE_TXCONNECTSPEED, p); /* Attribute type: TX Connect speed */
1038  PUTLONG(PPPOL2TP_TXCONNECTSPEED, p); /* Attribute value: TX Connect speed */
1039 
1040  return pppol2tp_udp_send(l2tp, pb);
1041 }
1042 
1043 /* Send a ZLB ACK packet */
1044 static err_t pppol2tp_send_zlb(pppol2tp_pcb *l2tp, u16_t ns) {
1045  struct pbuf *pb;
1046  u8_t *p;
1047  u16_t len;
1048 
1049  /* calculate UDP packet length */
1050  len = 12;
1051 
1052  /* allocate a buffer */
1053  pb = pbuf_alloc(PBUF_TRANSPORT, len, PBUF_RAM);
1054  if (pb == NULL) {
1055  return ERR_MEM;
1056  }
1057  LWIP_ASSERT("pb->tot_len == pb->len", pb->tot_len == pb->len);
1058 
1059  p = (u8_t*)pb->payload;
1060  /* fill in pkt */
1061  /* L2TP control header */
1062  PUTSHORT(PPPOL2TP_HEADERFLAG_CONTROL_MANDATORY, p);
1063  PUTSHORT(len, p); /* Length */
1064  PUTSHORT(l2tp->source_tunnel_id, p); /* Tunnel Id */
1065  PUTSHORT(0, p); /* Session Id */
1066  PUTSHORT(ns, p); /* NS Sequence number - to peer */
1067  PUTSHORT(l2tp->peer_ns+1, p); /* NR Sequence number - expected for peer */
1068 
1069  return pppol2tp_udp_send(l2tp, pb);
1070 }
1071 
1072 /* Send a StopCCN packet */
1073 static err_t pppol2tp_send_stopccn(pppol2tp_pcb *l2tp, u16_t ns) {
1074  struct pbuf *pb;
1075  u8_t *p;
1076  u16_t len;
1077 
1078  /* calculate UDP packet length */
1079  len = 12 +8 +8 +8;
1080 
1081  /* allocate a buffer */
1082  pb = pbuf_alloc(PBUF_TRANSPORT, len, PBUF_RAM);
1083  if (pb == NULL) {
1084  return ERR_MEM;
1085  }
1086  LWIP_ASSERT("pb->tot_len == pb->len", pb->tot_len == pb->len);
1087 
1088  p = (u8_t*)pb->payload;
1089  /* fill in pkt */
1090  /* L2TP control header */
1091  PUTSHORT(PPPOL2TP_HEADERFLAG_CONTROL_MANDATORY, p);
1092  PUTSHORT(len, p); /* Length */
1093  PUTSHORT(l2tp->source_tunnel_id, p); /* Tunnel Id */
1094  PUTSHORT(0, p); /* Session Id */
1095  PUTSHORT(ns, p); /* NS Sequence number - to peer */
1096  PUTSHORT(l2tp->peer_ns+1, p); /* NR Sequence number - expected for peer */
1097 
1098  /* AVP - Message type */
1099  PUTSHORT(PPPOL2TP_AVPHEADERFLAG_MANDATORY + 8, p); /* Mandatory flag + len field */
1100  PUTSHORT(0, p); /* Vendor ID */
1101  PUTSHORT(PPPOL2TP_AVPTYPE_MESSAGE, p); /* Attribute type: Message Type */
1102  PUTSHORT(PPPOL2TP_MESSAGETYPE_STOPCCN, p); /* Attribute value: Message type: StopCCN */
1103 
1104  /* AVP - Assign tunnel ID */
1105  PUTSHORT(PPPOL2TP_AVPHEADERFLAG_MANDATORY + 8, p); /* Mandatory flag + len field */
1106  PUTSHORT(0, p); /* Vendor ID */
1107  PUTSHORT(PPPOL2TP_AVPTYPE_TUNNELID, p); /* Attribute type: Tunnel ID */
1108  PUTSHORT(l2tp->remote_tunnel_id, p); /* Attribute value: Tunnel ID */
1109 
1110  /* AVP - Result code */
1111  PUTSHORT(PPPOL2TP_AVPHEADERFLAG_MANDATORY + 8, p); /* Mandatory flag + len field */
1112  PUTSHORT(0, p); /* Vendor ID */
1113  PUTSHORT(PPPOL2TP_AVPTYPE_RESULTCODE, p); /* Attribute type: Result code */
1114  PUTSHORT(PPPOL2TP_RESULTCODE, p); /* Attribute value: Result code */
1115 
1116  return pppol2tp_udp_send(l2tp, pb);
1117 }
1118 
1119 static err_t pppol2tp_xmit(pppol2tp_pcb *l2tp, struct pbuf *pb) {
1120  u8_t *p;
1121 
1122  /* are we ready to process data yet? */
1123  if (l2tp->phase < PPPOL2TP_STATE_DATA) {
1124  pbuf_free(pb);
1125  return ERR_CONN;
1126  }
1127 
1128  /* make room for L2TP header - should not fail */
1129  if (pbuf_header(pb, (s16_t)PPPOL2TP_OUTPUT_DATA_HEADER_LEN) != 0) {
1130  /* bail out */
1131  PPPDEBUG(LOG_ERR, ("pppol2tp: pppol2tp_pcb: could not allocate room for L2TP header\n"));
1132  LINK_STATS_INC(link.lenerr);
1133  pbuf_free(pb);
1134  return ERR_BUF;
1135  }
1136 
1137  p = (u8_t*)pb->payload;
1138  PUTSHORT(PPPOL2TP_HEADERFLAG_DATA_MANDATORY, p);
1139  PUTSHORT(l2tp->source_tunnel_id, p); /* Tunnel Id */
1140  PUTSHORT(l2tp->source_session_id, p); /* Session Id */
1141 
1142  return pppol2tp_udp_send(l2tp, pb);
1143 }
1144 
1145 static err_t pppol2tp_udp_send(pppol2tp_pcb *l2tp, struct pbuf *pb) {
1146  err_t err;
1147  if (l2tp->netif) {
1148  err = udp_sendto_if(l2tp->udp, pb, &l2tp->remote_ip, l2tp->tunnel_port, l2tp->netif);
1149  } else {
1150  err = udp_sendto(l2tp->udp, pb, &l2tp->remote_ip, l2tp->tunnel_port);
1151  }
1152  pbuf_free(pb);
1153  return err;
1154 }
1155 
1156 #endif /* PPP_SUPPORT && PPPOL2TP_SUPPORT */
1157 
#define ERR_CONN
Definition: err.h:64
#define U16_F
Definition: cc.h:48
signed short s16_t
Definition: cc.h:41
u8_t pbuf_header(struct pbuf *p, s16_t header_size_increment)
Definition: pbuf.c:603
#define ERR_VAL
Definition: err.h:58
#define MEMCPY(dst, src, len)
Definition: opt.h:84
if(LCD_Lock==DISABLE)
Definition: lcd_log.c:249
void memp_free(memp_t type, void *mem)
Definition: memp.c:399
u16_t len
Definition: pbuf.h:125
struct pbuf * pbuf_alloc(pbuf_layer layer, u16_t length, pbuf_type type)
Definition: pbuf.c:199
Definition: pbuf.h:77
#define MIB2_STATS_NETIF_ADD(n, x, val)
Definition: snmp.h:122
#define LWIP_MIN(x, y)
Definition: def.h:50
#define NULL
Definition: usbd_def.h:53
u8_t pbuf_free(struct pbuf *p)
Definition: pbuf.c:652
#define PCB_ISIPV6(pcb)
Definition: ip.h:89
#define ip_addr_copy(dest, src)
Definition: ip_addr.h:203
unsigned long u32_t
Definition: cc.h:42
void sys_timeout(u32_t msecs, sys_timeout_handler handler, void *arg)
void pbuf_chain(struct pbuf *h, struct pbuf *t)
Definition: pbuf.c:820
#define ERR_OK
Definition: err.h:52
u16_t tot_len
Definition: pbuf.h:122
#define IP_IS_V6_VAL(ipaddr)
Definition: ip_addr.h:196
Definition: pbuf.h:108
s8_t err_t
Definition: err.h:47
#define LINK_STATS_INC(x)
Definition: stats.h:318
#define ip_addr_cmp(addr1, addr2)
Definition: ip_addr.h:214
#define LWIP_ASSERT(message, assertion)
Definition: debug.h:70
Definition: netif.h:182
void pbuf_cat(struct pbuf *h, struct pbuf *t)
Definition: pbuf.c:779
unsigned char u8_t
Definition: cc.h:38
ip6_addr_t ip_addr_t
Definition: ip_addr.h:194
#define MIB2_STATS_NETIF_INC(n, x)
Definition: snmp.h:121
void * memp_malloc(memp_t type)
Definition: memp.c:303
#define ERR_MEM
Definition: err.h:53
#define LWIP_UNUSED_ARG(x)
Definition: arch.h:89
void sys_untimeout(sys_timeout_handler handler, void *arg)
unsigned short u16_t
Definition: cc.h:40
void * payload
Definition: pbuf.h:113
#define ERR_BUF
Definition: err.h:54