The Pedigree Project  0.1
pppol2tp.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2008-2014, Pedigree Developers
3  *
4  * Please see the CONTRIB file in the root of the source tree for a full
5  * list of contributors.
6  *
7  * Permission to use, copy, modify, and distribute this software for any
8  * purpose with or without fee is hereby granted, provided that the above
9  * copyright notice and this permission notice appear in all copies.
10  *
11  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18  */
19 
26 /*
27  * Redistribution and use in source and binary forms, with or without modification,
28  * are permitted provided that the following conditions are met:
29  *
30  * 1. Redistributions of source code must retain the above copyright notice,
31  * this list of conditions and the following disclaimer.
32  * 2. Redistributions in binary form must reproduce the above copyright notice,
33  * this list of conditions and the following disclaimer in the documentation
34  * and/or other materials provided with the distribution.
35  * 3. The name of the author may not be used to endorse or promote products
36  * derived from this software without specific prior written permission.
37  *
38  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
39  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
40  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
41  * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
42  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
43  * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
44  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
45  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
46  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
47  * OF SUCH DAMAGE.
48  *
49  * This file is part of the lwIP TCP/IP stack.
50  *
51  */
52 
53 /*
54  * L2TP Support status:
55  *
56  * Supported:
57  * - L2TPv2 (PPP over L2TP, a.k.a. UDP tunnels)
58  * - LAC
59  *
60  * Not supported:
61  * - LNS (require PPP server support)
62  * - L2TPv3 ethernet pseudowires
63  * - L2TPv3 VLAN pseudowire
64  * - L2TPv3 PPP pseudowires
65  * - L2TPv3 IP encapsulation
66  * - L2TPv3 IP pseudowire
67  * - L2TP tunnel switching - http://tools.ietf.org/html/draft-ietf-l2tpext-tunnel-switching-08
68  * - Multiple tunnels per UDP socket, as well as multiple sessions per tunnel
69  * - Hidden AVPs
70  */
71 
72 #include "netif/ppp/ppp_opts.h"
73 #if PPP_SUPPORT && PPPOL2TP_SUPPORT /* don't build if not configured for use in lwipopts.h */
74 
75 #include "lwip/err.h"
76 #include "lwip/memp.h"
77 #include "lwip/netif.h"
78 #include "lwip/udp.h"
79 #include "lwip/snmp.h"
80 
81 #include "netif/ppp/ppp_impl.h"
82 #include "netif/ppp/lcp.h"
83 #include "netif/ppp/ipcp.h"
84 #include "netif/ppp/pppol2tp.h"
85 #include "netif/ppp/pppcrypt.h"
86 #include "netif/ppp/magic.h"
87 
88 /* Memory pool */
89 LWIP_MEMPOOL_DECLARE(PPPOL2TP_PCB, MEMP_NUM_PPPOL2TP_INTERFACES, sizeof(pppol2tp_pcb), "PPPOL2TP_PCB")
90 
91 /* callbacks called from PPP core */
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); /* Destroy a L2TP control block */
95 static void pppol2tp_connect(ppp_pcb *ppp, void *ctx); /* Be a LAC, connect to a LNS. */
96 static void pppol2tp_disconnect(ppp_pcb *ppp, void *ctx); /* Disconnect */
97 
98  /* Prototypes for procedures local to this file. */
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);
111 
112 /* Callbacks structure for PPP core */
113 static const struct link_callbacks pppol2tp_callbacks = {
114  pppol2tp_connect,
115 #if PPP_SERVER
116  NULL,
117 #endif /* PPP_SERVER */
118  pppol2tp_disconnect,
119  pppol2tp_destroy,
120  pppol2tp_write,
121  pppol2tp_netif_output,
122  NULL,
123  NULL
124 };
125 
126 
127 /* Create a new L2TP session. */
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) {
132  ppp_pcb *ppp;
133  pppol2tp_pcb *l2tp;
134  struct udp_pcb *udp;
135 #if !PPPOL2TP_AUTH_SUPPORT
136  LWIP_UNUSED_ARG(secret);
137  LWIP_UNUSED_ARG(secret_len);
138 #endif /* !PPPOL2TP_AUTH_SUPPORT */
139 
140  if (ipaddr == NULL) {
141  goto ipaddr_check_failed;
142  }
143 
144  l2tp = (pppol2tp_pcb *)LWIP_MEMPOOL_ALLOC(PPPOL2TP_PCB);
145  if (l2tp == NULL) {
146  goto memp_malloc_l2tp_failed;
147  }
148 
149  udp = udp_new_ip_type(IP_GET_TYPE(ipaddr));
150  if (udp == NULL) {
151  goto udp_new_failed;
152  }
153  udp_recv(udp, pppol2tp_input, l2tp);
154 
155  ppp = ppp_new(pppif, &pppol2tp_callbacks, l2tp, link_status_cb, ctx_cb);
156  if (ppp == NULL) {
157  goto ppp_new_failed;
158  }
159 
160  memset(l2tp, 0, sizeof(pppol2tp_pcb));
161  l2tp->phase = PPPOL2TP_STATE_INITIAL;
162  l2tp->ppp = ppp;
163  l2tp->udp = udp;
164  l2tp->netif = netif;
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;
170 #endif /* PPPOL2TP_AUTH_SUPPORT */
171 
172  return ppp;
173 
174 ppp_new_failed:
175  udp_remove(udp);
176 udp_new_failed:
177  LWIP_MEMPOOL_FREE(PPPOL2TP_PCB, l2tp);
178 memp_malloc_l2tp_failed:
179 ipaddr_check_failed:
180  return NULL;
181 }
182 
183 /* Called by PPP core */
184 static err_t pppol2tp_write(ppp_pcb *ppp, void *ctx, struct pbuf *p) {
185  pppol2tp_pcb *l2tp = (pppol2tp_pcb *)ctx;
186  struct pbuf *ph; /* UDP + L2TP header */
187  err_t ret;
188 #if MIB2_STATS
189  u16_t tot_len;
190 #else /* MIB2_STATS */
191  LWIP_UNUSED_ARG(ppp);
192 #endif /* MIB2_STATS */
193 
194  ph = pbuf_alloc(PBUF_TRANSPORT, (u16_t)(PPPOL2TP_OUTPUT_DATA_HEADER_LEN), PBUF_RAM);
195  if(!ph) {
196  LINK_STATS_INC(link.memerr);
197  LINK_STATS_INC(link.proterr);
198  MIB2_STATS_NETIF_INC(ppp->netif, ifoutdiscards);
199  pbuf_free(p);
200  return ERR_MEM;
201  }
202 
203  pbuf_header(ph, -(s16_t)PPPOL2TP_OUTPUT_DATA_HEADER_LEN); /* hide L2TP header */
204  pbuf_cat(ph, p);
205 #if MIB2_STATS
206  tot_len = ph->tot_len;
207 #endif /* MIB2_STATS */
208 
209  ret = pppol2tp_xmit(l2tp, ph);
210  if (ret != ERR_OK) {
211  LINK_STATS_INC(link.err);
212  MIB2_STATS_NETIF_INC(ppp->netif, ifoutdiscards);
213  return ret;
214  }
215 
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);
219  return ERR_OK;
220 }
221 
222 /* Called by PPP core */
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;
225  struct pbuf *pb;
226  u8_t *pl;
227  err_t err;
228 #if MIB2_STATS
229  u16_t tot_len;
230 #else /* MIB2_STATS */
231  LWIP_UNUSED_ARG(ppp);
232 #endif /* MIB2_STATS */
233 
234  /* @todo: try to use pbuf_header() here! */
235  pb = pbuf_alloc(PBUF_TRANSPORT, PPPOL2TP_OUTPUT_DATA_HEADER_LEN + sizeof(protocol), PBUF_RAM);
236  if(!pb) {
237  LINK_STATS_INC(link.memerr);
238  LINK_STATS_INC(link.proterr);
239  MIB2_STATS_NETIF_INC(ppp->netif, ifoutdiscards);
240  return ERR_MEM;
241  }
242 
243  pbuf_header(pb, -(s16_t)PPPOL2TP_OUTPUT_DATA_HEADER_LEN);
244 
245  pl = (u8_t*)pb->payload;
246  PUTSHORT(protocol, pl);
247 
248  pbuf_chain(pb, p);
249 #if MIB2_STATS
250  tot_len = pb->tot_len;
251 #endif /* MIB2_STATS */
252 
253  if( (err = pppol2tp_xmit(l2tp, pb)) != ERR_OK) {
254  LINK_STATS_INC(link.err);
255  MIB2_STATS_NETIF_INC(ppp->netif, ifoutdiscards);
256  return err;
257  }
258 
259  MIB2_STATS_NETIF_ADD(ppp->netif, ifoutoctets, tot_len);
260  MIB2_STATS_NETIF_INC(ppp->netif, ifoutucastpkts);
261  LINK_STATS_INC(link.xmit);
262  return ERR_OK;
263 }
264 
265 /* Destroy a L2TP control block */
266 static err_t pppol2tp_destroy(ppp_pcb *ppp, void *ctx) {
267  pppol2tp_pcb *l2tp = (pppol2tp_pcb *)ctx;
268  LWIP_UNUSED_ARG(ppp);
269 
270  sys_untimeout(pppol2tp_timeout, l2tp);
271  udp_remove(l2tp->udp);
272  LWIP_MEMPOOL_FREE(PPPOL2TP_PCB, l2tp);
273  return ERR_OK;
274 }
275 
276 /* Be a LAC, connect to a LNS. */
277 static void pppol2tp_connect(ppp_pcb *ppp, void *ctx) {
278  err_t err;
279  pppol2tp_pcb *l2tp = (pppol2tp_pcb *)ctx;
280  lcp_options *lcp_wo;
281  lcp_options *lcp_ao;
282 #if PPP_IPV4_SUPPORT && VJ_SUPPORT
283  ipcp_options *ipcp_wo;
284  ipcp_options *ipcp_ao;
285 #endif /* PPP_IPV4_SUPPORT && VJ_SUPPORT */
286 
287  l2tp->tunnel_port = l2tp->remote_port;
288  l2tp->our_ns = 0;
289  l2tp->peer_nr = 0;
290  l2tp->peer_ns = 0;
291  l2tp->source_tunnel_id = 0;
292  l2tp->remote_tunnel_id = 0;
293  l2tp->source_session_id = 0;
294  l2tp->remote_session_id = 0;
295  /* l2tp->*_retried are cleared when used */
296 
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;
302  lcp_wo->passive = 0;
303  lcp_wo->silent = 0;
304 
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;
310 
311 #if PPP_IPV4_SUPPORT && VJ_SUPPORT
312  ipcp_wo = &ppp->ipcp_wantoptions;
313  ipcp_wo->neg_vj = 0;
314  ipcp_wo->old_vj = 0;
315 
316  ipcp_ao = &ppp->ipcp_allowoptions;
317  ipcp_ao->neg_vj = 0;
318  ipcp_ao->old_vj = 0;
319 #endif /* PPP_IPV4_SUPPORT && VJ_SUPPORT */
320 
321  /* Listen to a random source port, we need to do that instead of using udp_connect()
322  * because the L2TP LNS might answer with its own random source port (!= 1701)
323  */
324 #if LWIP_IPV6
325  if (IP_IS_V6_VAL(l2tp->udp->local_ip)) {
326  udp_bind(l2tp->udp, IP6_ADDR_ANY, 0);
327  } else
328 #endif /* LWIP_IPV6 */
329  udp_bind(l2tp->udp, IP_ADDR_ANY, 0);
330 
331 #if PPPOL2TP_AUTH_SUPPORT
332  /* Generate random vector */
333  if (l2tp->secret != NULL) {
334  magic_random_bytes(l2tp->secret_rv, sizeof(l2tp->secret_rv));
335  }
336 #endif /* PPPOL2TP_AUTH_SUPPORT */
337 
338  do {
339  l2tp->remote_tunnel_id = magic();
340  } while(l2tp->remote_tunnel_id == 0);
341  /* save state, in case we fail to send SCCRQ */
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));
346  }
347  sys_timeout(PPPOL2TP_CONTROL_TIMEOUT, pppol2tp_timeout, l2tp);
348 }
349 
350 /* Disconnect */
351 static void pppol2tp_disconnect(ppp_pcb *ppp, void *ctx) {
352  pppol2tp_pcb *l2tp = (pppol2tp_pcb *)ctx;
353 
354  l2tp->our_ns++;
355  pppol2tp_send_stopccn(l2tp, l2tp->our_ns);
356 
357  /* stop any timer, disconnect can be called while initiating is in progress */
358  sys_untimeout(pppol2tp_timeout, l2tp);
359  l2tp->phase = PPPOL2TP_STATE_INITIAL;
360  ppp_link_end(ppp); /* notify upper layers */
361 }
362 
363 /* UDP Callback for incoming IPv4 L2TP frames */
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;
367  u8_t *inp;
368  LWIP_UNUSED_ARG(pcb);
369 
370  /* we can't unbound a UDP pcb, thus we can still receive UDP frames after the link is closed */
371  if (l2tp->phase < PPPOL2TP_STATE_SCCRQ_SENT) {
372  goto free_and_return;
373  }
374 
375  if (!ip_addr_cmp(&l2tp->remote_ip, addr)) {
376  goto free_and_return;
377  }
378 
379  /* discard packet if port mismatch, but only if we received a SCCRP */
380  if (l2tp->phase > PPPOL2TP_STATE_SCCRQ_SENT && l2tp->tunnel_port != port) {
381  goto free_and_return;
382  }
383 
384  /* printf("-----------\nL2TP INPUT, %d\n", p->len); */
385 
386  /* L2TP header */
387  if (p->len < sizeof(hflags) + sizeof(tunnel_id) + sizeof(session_id) ) {
388  goto packet_too_short;
389  }
390 
391  inp = (u8_t*)p->payload;
392  GETSHORT(hflags, inp);
393 
394  if (hflags & PPPOL2TP_HEADERFLAG_CONTROL) {
395  /* check mandatory flags for a control packet */
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;
399  }
400  /* check forbidden flags for a control packet */
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;
404  }
405  } else {
406  /* check mandatory flags for a data packet */
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;
410  }
411  }
412 
413  /* Expected header size */
414  hlen = sizeof(hflags) + sizeof(tunnel_id) + sizeof(session_id);
415  if (hflags & PPPOL2TP_HEADERFLAG_LENGTH) {
416  hlen += sizeof(len);
417  }
418  if (hflags & PPPOL2TP_HEADERFLAG_SEQUENCE) {
419  hlen += sizeof(ns) + sizeof(nr);
420  }
421  if (hflags & PPPOL2TP_HEADERFLAG_OFFSET) {
422  hlen += sizeof(offset);
423  }
424  if (p->len < hlen) {
425  goto packet_too_short;
426  }
427 
428  if (hflags & PPPOL2TP_HEADERFLAG_LENGTH) {
429  GETSHORT(len, inp);
430  if (p->len < len || len < hlen) {
431  goto packet_too_short;
432  }
433  }
434  GETSHORT(tunnel_id, inp);
435  GETSHORT(session_id, inp);
436  if (hflags & PPPOL2TP_HEADERFLAG_SEQUENCE) {
437  GETSHORT(ns, inp);
438  GETSHORT(nr, inp);
439  }
440  if (hflags & PPPOL2TP_HEADERFLAG_OFFSET) {
441  GETSHORT(offset, inp)
442  if (offset > 4096) { /* don't be fooled with large offset which might overflow hlen */
443  PPPDEBUG(LOG_DEBUG, ("pppol2tp: strange packet received, offset=%d\n", offset));
444  goto free_and_return;
445  }
446  hlen += offset;
447  if (p->len < hlen) {
448  goto packet_too_short;
449  }
450  INCPTR(offset, inp);
451  }
452 
453  /* printf("HLEN = %d\n", hlen); */
454 
455  /* skip L2TP header */
456  if (pbuf_header(p, -(s16_t)hlen) != 0) {
457  goto free_and_return;
458  }
459 
460  /* printf("LEN=%d, TUNNEL_ID=%d, SESSION_ID=%d, NS=%d, NR=%d, OFFSET=%d\n", len, tunnel_id, session_id, ns, nr, offset); */
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));
463 
464  /* Control packet */
465  if (hflags & PPPOL2TP_HEADERFLAG_CONTROL) {
466  pppol2tp_dispatch_control_packet(l2tp, port, p, ns, nr);
467  goto free_and_return;
468  }
469 
470  /* Data packet */
471  if(l2tp->phase != PPPOL2TP_STATE_DATA) {
472  goto free_and_return;
473  }
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;
477  }
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;
481  }
482  /*
483  * skip address & flags if necessary
484  *
485  * RFC 2661 does not specify whether the PPP frame in the L2TP payload should
486  * have a HDLC header or not. We handle both cases for compatibility.
487  */
488  if (p->len >= 2) {
489  GETSHORT(hflags, inp);
490  if (hflags == 0xff03) {
491  pbuf_header(p, -(s16_t)2);
492  }
493  }
494  /* Dispatch the packet thereby consuming it. */
495  ppp_input(l2tp->ppp, p);
496  return;
497 
498 packet_too_short:
499  PPPDEBUG(LOG_DEBUG, ("pppol2tp: packet too short: %d\n", p->len));
500 free_and_return:
501  pbuf_free(p);
502 }
503 
504 /* L2TP Control packet entry point */
505 static void pppol2tp_dispatch_control_packet(pppol2tp_pcb *l2tp, u16_t port, struct pbuf *p, u16_t ns, u16_t nr) {
506  u8_t *inp;
507  u16_t avplen, avpflags, vendorid, attributetype, messagetype=0;
508  err_t err;
509 #if PPPOL2TP_AUTH_SUPPORT
510  lwip_md5_context md5_ctx;
511  u8_t md5_hash[16];
512  u8_t challenge_id = 0;
513 #endif /* PPPOL2TP_AUTH_SUPPORT */
514 
515  l2tp->peer_nr = nr;
516  l2tp->peer_ns = ns;
517  /* printf("L2TP CTRL INPUT, ns=%d, nr=%d, len=%d\n", ns, nr, p->len); */
518 
519  /* Handle the special case of the ICCN acknowledge */
520  if (l2tp->phase == PPPOL2TP_STATE_ICCN_SENT && l2tp->peer_nr > l2tp->our_ns) {
521  l2tp->phase = PPPOL2TP_STATE_DATA;
522  }
523 
524  /* ZLB packets */
525  if (p->tot_len == 0) {
526  return;
527  }
528 
529  p = ppp_singlebuf(p);
530  inp = (u8_t*)p->payload;
531  /* Decode AVPs */
532  while (p->len > 0) {
533  if (p->len < sizeof(avpflags) + sizeof(vendorid) + sizeof(attributetype) ) {
534  goto packet_too_short;
535  }
536  GETSHORT(avpflags, inp);
537  avplen = avpflags & PPPOL2TP_AVPHEADERFLAG_LENGTHMASK;
538  /* printf("AVPLEN = %d\n", avplen); */
539  if (p->len < avplen || avplen < sizeof(avpflags) + sizeof(vendorid) + sizeof(attributetype)) {
540  goto packet_too_short;
541  }
542  GETSHORT(vendorid, inp);
543  GETSHORT(attributetype, inp);
544  avplen -= sizeof(avpflags) + sizeof(vendorid) + sizeof(attributetype);
545 
546  /* Message type must be the first AVP */
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"));
550  return;
551  }
552  GETSHORT(messagetype, inp);
553  /* printf("Message type = %d\n", messagetype); */
554  switch(messagetype) {
555  /* Start Control Connection Reply */
556  case PPPOL2TP_MESSAGETYPE_SCCRP:
557  /* Only accept SCCRP packet if we sent a SCCRQ */
558  if (l2tp->phase != PPPOL2TP_STATE_SCCRQ_SENT) {
559  goto send_zlb;
560  }
561  break;
562  /* Incoming Call Reply */
563  case PPPOL2TP_MESSAGETYPE_ICRP:
564  /* Only accept ICRP packet if we sent a IRCQ */
565  if (l2tp->phase != PPPOL2TP_STATE_ICRQ_SENT) {
566  goto send_zlb;
567  }
568  break;
569  /* Stop Control Connection Notification */
570  case PPPOL2TP_MESSAGETYPE_STOPCCN:
571  pppol2tp_send_zlb(l2tp, l2tp->our_ns); /* Ack the StopCCN before we switch to down state */
572  if (l2tp->phase < PPPOL2TP_STATE_DATA) {
573  pppol2tp_abort_connect(l2tp);
574  } else if (l2tp->phase == PPPOL2TP_STATE_DATA) {
575  /* Don't disconnect here, we let the LCP Echo/Reply find the fact
576  * that PPP session is down. Asking the PPP stack to end the session
577  * require strict checking about the PPP phase to prevent endless
578  * disconnection loops.
579  */
580  }
581  return;
582  default:
583  break;
584  }
585  goto nextavp;
586  }
587 
588  /* Skip proprietary L2TP extensions */
589  if (vendorid != 0) {
590  goto skipavp;
591  }
592 
593  switch (messagetype) {
594  /* Start Control Connection Reply */
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"));
600  return;
601  }
602  GETSHORT(l2tp->source_tunnel_id, inp);
603  PPPDEBUG(LOG_DEBUG, ("pppol2tp: Assigned tunnel ID %"U16_F"\n", l2tp->source_tunnel_id));
604  goto nextavp;
605 #if PPPOL2TP_AUTH_SUPPORT
606  case PPPOL2TP_AVPTYPE_CHALLENGE:
607  if (avplen == 0) {
608  PPPDEBUG(LOG_DEBUG, ("pppol2tp: Challenge length check failed\n"));
609  return;
610  }
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);
614  return;
615  }
616  /* Generate hash of ID, secret, challenge */
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;
626  goto skipavp;
627  case PPPOL2TP_AVPTYPE_CHALLENGERESPONSE:
628  if (avplen != PPPOL2TP_AVPTYPE_CHALLENGERESPONSE_SIZE) {
629  PPPDEBUG(LOG_DEBUG, ("pppol2tp: AVP Challenge Response length check failed\n"));
630  return;
631  }
632  /* Generate hash of ID, secret, challenge */
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);
644  return;
645  }
646  goto skipavp;
647 #endif /* PPPOL2TP_AUTH_SUPPORT */
648  default:
649  break;
650  }
651  break;
652  /* Incoming Call Reply */
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"));
658  return;
659  }
660  GETSHORT(l2tp->source_session_id, inp);
661  PPPDEBUG(LOG_DEBUG, ("pppol2tp: Assigned session ID %"U16_F"\n", l2tp->source_session_id));
662  goto nextavp;
663  default:
664  break;
665  }
666  break;
667  default:
668  break;
669  }
670 
671 skipavp:
672  INCPTR(avplen, inp);
673 nextavp:
674  /* printf("AVP Found, vendor=%d, attribute=%d, len=%d\n", vendorid, attributetype, avplen); */
675  /* next AVP */
676  if (pbuf_header(p, -(s16_t)(avplen + sizeof(avpflags) + sizeof(vendorid) + sizeof(attributetype)) ) != 0) {
677  return;
678  }
679  }
680 
681  switch(messagetype) {
682  /* Start Control Connection Reply */
683  case PPPOL2TP_MESSAGETYPE_SCCRP:
684  do {
685  l2tp->remote_session_id = magic();
686  } while(l2tp->remote_session_id == 0);
687  l2tp->tunnel_port = port; /* LNS server might have chosen its own local port */
688  l2tp->icrq_retried = 0;
689  l2tp->phase = PPPOL2TP_STATE_ICRQ_SENT;
690  l2tp->our_ns++;
691  if ((err = pppol2tp_send_scccn(l2tp, l2tp->our_ns)) != 0) {
692  PPPDEBUG(LOG_DEBUG, ("pppol2tp: failed to send SCCCN, error=%d\n", err));
693  }
694  l2tp->our_ns++;
695  if ((err = pppol2tp_send_icrq(l2tp, l2tp->our_ns)) != 0) {
696  PPPDEBUG(LOG_DEBUG, ("pppol2tp: failed to send ICRQ, error=%d\n", err));
697  }
698  sys_untimeout(pppol2tp_timeout, l2tp);
699  sys_timeout(PPPOL2TP_CONTROL_TIMEOUT, pppol2tp_timeout, l2tp);
700  break;
701  /* Incoming Call Reply */
702  case PPPOL2TP_MESSAGETYPE_ICRP:
703  l2tp->iccn_retried = 0;
704  l2tp->phase = PPPOL2TP_STATE_ICCN_SENT;
705  l2tp->our_ns++;
706  ppp_start(l2tp->ppp); /* notify upper layers */
707  if ((err = pppol2tp_send_iccn(l2tp, l2tp->our_ns)) != 0) {
708  PPPDEBUG(LOG_DEBUG, ("pppol2tp: failed to send ICCN, error=%d\n", err));
709  }
710  sys_untimeout(pppol2tp_timeout, l2tp);
711  sys_timeout(PPPOL2TP_CONTROL_TIMEOUT, pppol2tp_timeout, l2tp);
712  break;
713  /* Unhandled packet, send ZLB ACK */
714  default:
715  goto send_zlb;
716  }
717  return;
718 
719 send_zlb:
720  pppol2tp_send_zlb(l2tp, l2tp->our_ns);
721  return;
722 packet_too_short:
723  PPPDEBUG(LOG_DEBUG, ("pppol2tp: packet too short: %d\n", p->len));
724 }
725 
726 /* L2TP Timeout handler */
727 static void pppol2tp_timeout(void *arg) {
728  pppol2tp_pcb *l2tp = (pppol2tp_pcb*)arg;
729  err_t err;
730  u32_t retry_wait;
731 
732  PPPDEBUG(LOG_DEBUG, ("pppol2tp: timeout\n"));
733 
734  switch (l2tp->phase) {
735  case PPPOL2TP_STATE_SCCRQ_SENT:
736  /* backoff wait */
737  if (l2tp->sccrq_retried < 0xff) {
738  l2tp->sccrq_retried++;
739  }
740  if (!l2tp->ppp->settings.persist && l2tp->sccrq_retried >= PPPOL2TP_MAXSCCRQ) {
741  pppol2tp_abort_connect(l2tp);
742  return;
743  }
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));
749  }
750  sys_timeout(retry_wait, pppol2tp_timeout, l2tp);
751  break;
752 
753  case PPPOL2TP_STATE_ICRQ_SENT:
754  l2tp->icrq_retried++;
755  if (l2tp->icrq_retried >= PPPOL2TP_MAXICRQ) {
756  pppol2tp_abort_connect(l2tp);
757  return;
758  }
759  PPPDEBUG(LOG_DEBUG, ("pppol2tp: icrq_retried=%d\n", l2tp->icrq_retried));
760  if (l2tp->peer_nr <= l2tp->our_ns -1) { /* the SCCCN was not acknowledged */
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);
765  break;
766  }
767  }
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));
771  }
772  sys_timeout(PPPOL2TP_CONTROL_TIMEOUT, pppol2tp_timeout, l2tp);
773  break;
774 
775  case PPPOL2TP_STATE_ICCN_SENT:
776  l2tp->iccn_retried++;
777  if (l2tp->iccn_retried >= PPPOL2TP_MAXICCN) {
778  pppol2tp_abort_connect(l2tp);
779  return;
780  }
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));
785  }
786  sys_timeout(PPPOL2TP_CONTROL_TIMEOUT, pppol2tp_timeout, l2tp);
787  break;
788 
789  default:
790  return; /* all done, work in peace */
791  }
792 }
793 
794 /* Connection attempt aborted */
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); /* notify upper layers */
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  /* make room for L2TP header - should not fail */
1123  if (pbuf_header(pb, (s16_t)PPPOL2TP_OUTPUT_DATA_HEADER_LEN) != 0) {
1124  /* bail out */
1125  PPPDEBUG(LOG_ERR, ("pppol2tp: pppol2tp_pcb: could not allocate room for L2TP header\n"));
1126  LINK_STATS_INC(link.lenerr);
1127  pbuf_free(pb);
1128  return ERR_BUF;
1129  }
1130 
1131  p = (u8_t*)pb->payload;
1132  PUTSHORT(PPPOL2TP_HEADERFLAG_DATA_MANDATORY, p);
1133  PUTSHORT(l2tp->source_tunnel_id, p); /* Tunnel Id */
1134  PUTSHORT(l2tp->source_session_id, p); /* Session Id */
1135 
1136  return pppol2tp_udp_send(l2tp, pb);
1137 }
1138 
1139 static err_t pppol2tp_udp_send(pppol2tp_pcb *l2tp, struct pbuf *pb) {
1140  err_t err;
1141  if (l2tp->netif) {
1142  err = udp_sendto_if(l2tp->udp, pb, &l2tp->remote_ip, l2tp->tunnel_port, l2tp->netif);
1143  } else {
1144  err = udp_sendto(l2tp->udp, pb, &l2tp->remote_ip, l2tp->tunnel_port);
1145  }
1146  pbuf_free(pb);
1147  return err;
1148 }
1149 
1150 #endif /* PPP_SUPPORT && PPPOL2TP_SUPPORT */
u16_t tot_len
Definition: pbuf.h:175
u16_t len
Definition: pbuf.h:178
Definition: err.h:86
#define LWIP_MEMPOOL_ALLOC(name)
Definition: memp.h:139
u8_t pbuf_header(struct pbuf *p, s16_t header_size_increment)
Definition: pbuf.c:684
#define LWIP_MEMPOOL_FREE(name, x)
Definition: memp.h:144
Definition: err.h:84
void pbuf_chain(struct pbuf *h, struct pbuf *t)
Definition: pbuf.c:901
#define LWIP_MEMPOOL_DECLARE(name, num, size, desc)
Definition: memp.h:112
Definition: pbuf.h:161
Definition: netif.h:244
void pbuf_cat(struct pbuf *h, struct pbuf *t)
Definition: pbuf.c:859
s8_t err_t
Definition: err.h:76
struct pbuf * pbuf_alloc(pbuf_layer layer, u16_t length, pbuf_type type)
Definition: pbuf.c:267
Definition: pbuf.h:127
#define LWIP_UNUSED_ARG(x)
Definition: arch.h:327
u8_t pbuf_free(struct pbuf *p)
Definition: pbuf.c:734
Definition: err.h:82
void * payload
Definition: pbuf.h:166