244d59a4fe7561b2eadaff7c4432839e779fefe0
3 char const *cvs_id_ppp
= "$Id: ppp.c,v 1.92 2006/01/19 21:00:24 bodea Exp $";
11 #include "constants.h"
17 extern tunnelt
*tunnel
;
18 extern sessiont
*session
;
19 extern radiust
*radius
;
21 extern char hostname
[];
22 extern uint32_t eth_tx
;
23 extern time_t time_now
;
24 extern configt
*config
;
26 static int add_lcp_auth(uint8_t *b
, int size
, int authtype
);
28 // Process PAP messages
29 void processpap(sessionidt s
, tunnelidt t
, uint8_t *p
, uint16_t l
)
38 LOG_HEX(5, "PAP", p
, l
);
41 LOG(1, s
, t
, "Short PAP %u bytes\n", l
);
42 STAT(tunnel_rx_errors
);
43 sessionshutdown(s
, "Short PAP packet.", 3, 0);
47 if ((hl
= ntohs(*(uint16_t *) (p
+ 2))) > l
)
49 LOG(1, s
, t
, "Length mismatch PAP %u/%u\n", hl
, l
);
50 STAT(tunnel_rx_errors
);
51 sessionshutdown(s
, "PAP length mismatch.", 3, 0);
58 LOG(1, s
, t
, "Unexpected PAP code %d\n", *p
);
59 STAT(tunnel_rx_errors
);
60 sessionshutdown(s
, "Unexpected PAP code.", 3, 0);
64 if (session
[s
].ppp
.phase
!= Authenticate
)
66 LOG(2, s
, t
, "PAP ignored in %s phase\n", ppp_phase(session
[s
].ppp
.phase
));
73 user
[0] = pass
[0] = 0;
74 if (*b
&& *b
< sizeof(user
))
76 memcpy(user
, b
+ 1, *b
);
79 if (*b
&& *b
< sizeof(pass
))
81 memcpy(pass
, b
+ 1, *b
);
85 LOG(3, s
, t
, "PAP login %s/%s\n", user
, pass
);
88 if (session
[s
].ip
|| !(r
= radiusnew(s
)))
90 // respond now, either no RADIUS available or already authenticated
93 uint8_t *p
= makeppp(b
, sizeof(b
), 0, 0, s
, t
, PPPPAP
);
99 *p
= 3; // cant authorise
101 *(uint16_t *) (p
+ 2) = htons(5); // length
102 p
[4] = 0; // no message
103 tunnelsend(b
, 5 + (p
- b
), t
); // send it
107 LOG(3, s
, t
, "Already an IP allocated: %s (%d)\n",
108 fmtaddr(htonl(session
[s
].ip
), 0), session
[s
].ip_pool_index
);
112 LOG(1, s
, t
, "No RADIUS session available to authenticate session...\n");
113 sessionshutdown(s
, "No free RADIUS sessions.", 4, 0);
118 // Run PRE_AUTH plugins
119 struct param_pre_auth packet
= { &tunnel
[t
], &session
[s
], strdup(user
), strdup(pass
), PPPPAP
, 1 };
120 run_plugins(PLUGIN_PRE_AUTH
, &packet
);
121 if (!packet
.continue_auth
)
123 LOG(3, s
, t
, "A plugin rejected PRE_AUTH\n");
124 if (packet
.username
) free(packet
.username
);
125 if (packet
.password
) free(packet
.password
);
129 strncpy(session
[s
].user
, packet
.username
, sizeof(session
[s
].user
) - 1);
130 strncpy(radius
[r
].pass
, packet
.password
, sizeof(radius
[r
].pass
) - 1);
132 free(packet
.username
);
133 free(packet
.password
);
136 LOG(3, s
, t
, "Sending login for %s/%s to RADIUS\n", user
, pass
);
137 radiussend(r
, RADIUSAUTH
);
141 // Process CHAP messages
142 void processchap(sessionidt s
, tunnelidt t
, uint8_t *p
, uint16_t l
)
149 LOG_HEX(5, "CHAP", p
, l
);
153 LOG(1, s
, t
, "Short CHAP %u bytes\n", l
);
154 STAT(tunnel_rx_errors
);
155 sessionshutdown(s
, "Short CHAP packet.", 3, 0);
159 if ((hl
= ntohs(*(uint16_t *) (p
+ 2))) > l
)
161 LOG(1, s
, t
, "Length mismatch CHAP %u/%u\n", hl
, l
);
162 STAT(tunnel_rx_errors
);
163 sessionshutdown(s
, "CHAP length mismatch.", 3, 0);
170 LOG(1, s
, t
, "Unexpected CHAP response code %d\n", *p
);
171 STAT(tunnel_rx_errors
);
172 sessionshutdown(s
, "CHAP length mismatch.", 3, 0);
176 r
= sess_local
[s
].radius
;
179 LOG(3, s
, t
, "Unexpected CHAP message\n");
183 if (session
[s
].ppp
.phase
!= Authenticate
)
185 LOG(2, s
, t
, "CHAP ignored in %s phase\n", ppp_phase(session
[s
].ppp
.phase
));
189 if (p
[1] != radius
[r
].id
)
191 LOG(1, s
, t
, "Wrong CHAP response ID %d (should be %d) (%d)\n", p
[1], radius
[r
].id
, r
);
192 STAT(tunnel_rx_errors
);
193 sessionshutdown(s
, "Unexpected CHAP response ID.", 3, 0);
197 if (l
< 5 || p
[4] != 16)
199 LOG(1, s
, t
, "Bad CHAP response length %d\n", l
< 5 ? -1 : p
[4]);
200 STAT(tunnel_rx_errors
);
201 sessionshutdown(s
, "Bad CHAP response length.", 3, 0);
207 if (l
< 16 || l
- 16 >= sizeof(session
[s
].user
))
209 LOG(1, s
, t
, "CHAP user too long %d\n", l
- 16);
210 STAT(tunnel_rx_errors
);
211 sessionshutdown(s
, "CHAP username too long.", 3, 0);
215 // Run PRE_AUTH plugins
217 struct param_pre_auth packet
= { &tunnel
[t
], &session
[s
], NULL
, NULL
, PPPCHAP
, 1 };
219 packet
.password
= calloc(17, 1);
220 memcpy(packet
.password
, p
, 16);
225 packet
.username
= calloc(l
+ 1, 1);
226 memcpy(packet
.username
, p
, l
);
228 run_plugins(PLUGIN_PRE_AUTH
, &packet
);
229 if (!packet
.continue_auth
)
231 LOG(3, s
, t
, "A plugin rejected PRE_AUTH\n");
232 if (packet
.username
) free(packet
.username
);
233 if (packet
.password
) free(packet
.password
);
237 strncpy(session
[s
].user
, packet
.username
, sizeof(session
[s
].user
) - 1);
238 memcpy(radius
[r
].pass
, packet
.password
, 16);
240 free(packet
.username
);
241 free(packet
.password
);
245 LOG(3, s
, t
, "CHAP login %s\n", session
[s
].user
);
246 radiussend(r
, RADIUSAUTH
);
249 static void dumplcp(uint8_t *p
, int l
)
252 uint8_t *o
= (p
+ 4);
254 LOG_HEX(5, "PPP LCP Packet", p
, l
);
255 LOG(4, 0, 0, "PPP LCP Packet type %d (%s len %d)\n", *p
, ppp_code((int)*p
), ntohs( ((uint16_t *) p
)[1]) );
256 LOG(4, 0, 0, "Length: %d\n", l
);
257 if (*p
!= ConfigReq
&& *p
!= ConfigRej
&& *p
!= ConfigAck
)
266 LOG(4, 0, 0, " Option length is %d...\n", length
);
271 LOG(4, 0, 0, " Option type is 0...\n");
278 case 1: // Maximum-Receive-Unit
280 LOG(4, 0, 0, " %s %d\n", ppp_lcp_option(type
), ntohs(*(uint16_t *)(o
+ 2)));
282 LOG(4, 0, 0, " %s odd length %d\n", ppp_lcp_option(type
), length
);
284 case 2: // Async-Control-Character-Map
287 uint32_t asyncmap
= ntohl(*(uint32_t *)(o
+ 2));
288 LOG(4, 0, 0, " %s %x\n", ppp_lcp_option(type
), asyncmap
);
291 LOG(4, 0, 0, " %s odd length %d\n", ppp_lcp_option(type
), length
);
293 case 3: // Authentication-Protocol
296 int proto
= ntohs(*(uint16_t *)(o
+ 2));
297 LOG(4, 0, 0, " %s 0x%x (%s)\n", ppp_lcp_option(type
), proto
,
298 proto
== PPPPAP
? "PAP" : "UNSUPPORTED");
300 else if (length
== 5)
302 int proto
= ntohs(*(uint16_t *)(o
+ 2));
304 LOG(4, 0, 0, " %s 0x%x 0x%x (%s)\n", ppp_lcp_option(type
), proto
, algo
,
305 (proto
== PPPCHAP
&& algo
== 5) ? "CHAP MD5" : "UNSUPPORTED");
308 LOG(4, 0, 0, " %s odd length %d\n", ppp_lcp_option(type
), length
);
310 case 4: // Quality-Protocol
312 uint32_t qp
= ntohl(*(uint32_t *)(o
+ 2));
313 LOG(4, 0, 0, " %s %x\n", ppp_lcp_option(type
), qp
);
316 case 5: // Magic-Number
319 uint32_t magicno
= ntohl(*(uint32_t *)(o
+ 2));
320 LOG(4, 0, 0, " %s %x\n", ppp_lcp_option(type
), magicno
);
323 LOG(4, 0, 0, " %s odd length %d\n", ppp_lcp_option(type
), length
);
325 case 7: // Protocol-Field-Compression
326 case 8: // Address-And-Control-Field-Compression
327 LOG(4, 0, 0, " %s\n", ppp_lcp_option(type
));
330 LOG(2, 0, 0, " Unknown PPP LCP Option type %d\n", type
);
338 void lcp_open(sessionidt s
, tunnelidt t
)
340 // transition to Authentication or Network phase:
341 session
[s
].ppp
.phase
= sess_local
[s
].lcp_authtype
? Authenticate
: Network
;
343 LOG(3, s
, t
, "LCP: Opened, phase %s\n", ppp_phase(session
[s
].ppp
.phase
));
346 change_state(s
, lcp
, Opened
);
348 if (session
[s
].ppp
.phase
== Authenticate
)
350 if (sess_local
[s
].lcp_authtype
== AUTHCHAP
)
357 change_state(s
, ipcp
, RequestSent
);
358 // move to passive state for IPv6 (if configured), CCP
359 if (config
->ipv6_prefix
.s6_addr
[0])
360 change_state(s
, ipv6cp
, Stopped
);
362 change_state(s
, ipv6cp
, Closed
);
364 change_state(s
, ccp
, Stopped
);
368 static void lcp_restart(sessionidt s
)
370 session
[s
].ppp
.phase
= Establish
;
372 change_state(s
, ipcp
, Dead
);
373 change_state(s
, ipv6cp
, Dead
);
374 change_state(s
, ccp
, Dead
);
377 static uint8_t *ppp_conf_rej(sessionidt s
, uint8_t *buf
, size_t blen
, uint16_t mtype
,
378 uint8_t **response
, uint8_t *queued
, uint8_t *packet
, uint8_t *option
)
380 if (!*response
|| **response
!= ConfigRej
)
382 queued
= *response
= makeppp(buf
, blen
, packet
, 2, s
, session
[s
].tunnel
, mtype
);
390 if ((queued
- buf
+ option
[1]) > blen
)
392 LOG(2, s
, session
[s
].tunnel
, "PPP overflow for ConfigRej (proto %u, option %u).\n", mtype
, *option
);
396 memcpy(queued
, option
, option
[1]);
397 return queued
+ option
[1];
400 static uint8_t *ppp_conf_nak(sessionidt s
, uint8_t *buf
, size_t blen
, uint16_t mtype
,
401 uint8_t **response
, uint8_t *queued
, uint8_t *packet
, uint8_t *option
,
402 uint8_t *value
, size_t vlen
)
407 case PPPLCP
: nak_sent
= &sess_local
[s
].lcp
.nak_sent
; break;
408 case PPPIPCP
: nak_sent
= &sess_local
[s
].ipcp
.nak_sent
; break;
409 case PPPIPV6CP
: nak_sent
= &sess_local
[s
].ipv6cp
.nak_sent
; break;
410 default: return 0; // ?
413 if (*response
&& **response
!= ConfigNak
)
415 if (*nak_sent
< config
->ppp_max_failure
) // reject queued
418 return ppp_conf_rej(s
, buf
, blen
, mtype
, response
, 0, packet
, option
);
423 if (*nak_sent
>= config
->ppp_max_failure
)
424 return ppp_conf_rej(s
, buf
, blen
, mtype
, response
, 0, packet
, option
);
426 queued
= *response
= makeppp(buf
, blen
, packet
, 2, s
, session
[s
].tunnel
, mtype
);
435 if ((queued
- buf
+ vlen
+ 2) > blen
)
437 LOG(2, s
, session
[s
].tunnel
, "PPP overflow for ConfigNak (proto %u, option %u).\n", mtype
, *option
);
442 *queued
++ = vlen
+ 2;
443 memcpy(queued
, value
, vlen
);
444 return queued
+ vlen
;
447 static void ppp_code_rej(sessionidt s
, tunnelidt t
, uint16_t proto
,
448 char *pname
, uint8_t *p
, uint16_t l
, uint8_t *buf
, size_t size
)
451 int mru
= session
[s
].mru
;
452 if (mru
< MINMTU
) mru
= MINMTU
;
453 if (mru
> size
) mru
= size
;
456 if (l
> mru
) l
= mru
;
458 q
= makeppp(buf
, size
, 0, 0, s
, t
, proto
);
462 *(q
+ 1) = ++sess_local
[s
].lcp_ident
;
463 *(uint16_t *)(q
+ 2) = htons(l
);
464 memcpy(q
+ 4, p
, l
- 4);
466 LOG(2, s
, t
, "Unexpected %s code %s\n", pname
, ppp_code(*p
));
467 LOG(3, s
, t
, "%s: send %s\n", pname
, ppp_code(*q
));
468 if (config
->debug
> 3) dumplcp(q
, l
);
470 tunnelsend(buf
, l
+ (q
- buf
), t
);
473 // Process LCP messages
474 void processlcp(sessionidt s
, tunnelidt t
, uint8_t *p
, uint16_t l
)
478 uint32_t magicno
= 0;
483 LOG_HEX(5, "LCP", p
, l
);
486 LOG(1, s
, t
, "Short LCP %d bytes\n", l
);
487 STAT(tunnel_rx_errors
);
491 if ((hl
= ntohs(*(uint16_t *) (p
+ 2))) > l
)
493 LOG(1, s
, t
, "Length mismatch LCP %u/%u\n", hl
, l
);
494 STAT(tunnel_rx_errors
);
499 if (session
[s
].die
) // going down...
502 LOG((*p
== EchoReq
|| *p
== EchoReply
) ? 4 : 3, s
, t
,
503 "LCP: recv %s\n", ppp_code(*p
));
505 if (config
->debug
> 3) dumplcp(p
, l
);
510 uint8_t *o
= (p
+ 4);
518 if (length
== 0 || type
== 0 || x
< length
) break;
521 case 3: // Authentication-Protocol
523 int proto
= ntohs(*(uint16_t *)(o
+ 2));
526 else if (proto
== PPPCHAP
&& *(o
+ 4) == 5)
536 if (!session
[s
].ip
&& authtype
)
537 sess_local
[s
].lcp_authtype
= authtype
;
539 switch (session
[s
].ppp
.lcp
)
542 initialise_restart_count(s
, lcp
);
543 change_state(s
, lcp
, AckReceived
);
548 LOG(2, s
, t
, "LCP: ConfigAck in state %s? Sending ConfigReq\n", ppp_state(session
[s
].ppp
.lcp
));
549 if (session
[s
].ppp
.lcp
== Opened
)
553 change_state(s
, lcp
, RequestSent
);
561 LOG(2, s
, t
, "LCP: ignoring %s in state %s\n", ppp_code(*p
), ppp_state(session
[s
].ppp
.lcp
));
564 else if (*p
== ConfigReq
)
567 uint8_t *o
= (p
+ 4);
568 uint8_t *response
= 0;
569 static uint8_t asyncmap
[4] = { 0, 0, 0, 0 }; // all zero
570 static uint8_t authproto
[5];
577 if (length
== 0 || type
== 0 || x
< length
) break;
580 case 1: // Maximum-Receive-Unit
582 uint16_t mru
= ntohs(*(uint16_t *)(o
+ 2));
585 session
[s
].mru
= mru
;
589 LOG(3, s
, t
, " Remote requesting MRU of %u. Rejecting.\n", mru
);
591 q
= ppp_conf_nak(s
, b
, sizeof(b
), PPPLCP
, &response
, q
, p
, o
, (uint8_t *) &mru
, sizeof(mru
));
595 case 2: // Async-Control-Character-Map
596 if (!ntohl(*(uint32_t *)(o
+ 2))) // all bits zero is OK
599 LOG(3, s
, t
, " Remote requesting asyncmap. Rejecting.\n");
600 q
= ppp_conf_nak(s
, b
, sizeof(b
), PPPLCP
, &response
, q
, p
, o
, asyncmap
, sizeof(asyncmap
));
603 case 3: // Authentication-Protocol
605 int proto
= ntohs(*(uint16_t *)(o
+ 2));
606 char proto_name
[] = "0x0000";
611 if (config
->radius_authtypes
& AUTHPAP
)
613 sess_local
[s
].lcp_authtype
= AUTHPAP
;
617 strcpy(proto_name
, "PAP");
619 else if (proto
== PPPCHAP
)
621 if (config
->radius_authtypes
& AUTHCHAP
622 && *(o
+ 4) == 5) // MD5
624 sess_local
[s
].lcp_authtype
= AUTHCHAP
;
628 strcpy(proto_name
, "CHAP");
631 sprintf(proto_name
, "%#4.4x", proto
);
633 LOG(3, s
, t
, " Remote requesting %s authentication. Rejecting.\n", proto_name
);
635 alen
= add_lcp_auth(authproto
, sizeof(authproto
), config
->radius_authprefer
);
636 if (alen
< 2) break; // paranoia
638 q
= ppp_conf_nak(s
, b
, sizeof(b
), PPPLCP
, &response
, q
, p
, o
, authproto
+ 2, alen
- 2);
639 if (q
&& *response
== ConfigNak
&&
640 config
->radius_authtypes
!= config
->radius_authprefer
)
643 alen
= add_lcp_auth(authproto
, sizeof(authproto
), config
->radius_authtypes
& ~config
->radius_authprefer
);
645 q
= ppp_conf_nak(s
, b
, sizeof(b
), PPPLCP
, &response
, q
, p
, o
, authproto
+ 2, alen
- 2);
652 case 5: // Magic-Number
653 magicno
= ntohl(*(uint32_t *)(o
+ 2));
656 case 4: // Quality-Protocol
657 case 7: // Protocol-Field-Compression
658 case 8: // Address-And-Control-Field-Compression
661 default: // Reject any unknown options
662 LOG(3, s
, t
, " Rejecting unknown PPP LCP option %d\n", type
);
663 q
= ppp_conf_rej(s
, b
, sizeof(b
), PPPLCP
, &response
, q
, p
, o
);
671 l
= q
- response
; // LCP packet length
672 *((uint16_t *) (response
+ 2)) = htons(l
); // update header
676 // Send packet back as ConfigAck
677 response
= makeppp(b
, sizeof(b
), p
, l
, s
, t
, PPPLCP
);
678 if (!response
) return;
679 *response
= ConfigAck
;
682 switch (session
[s
].ppp
.lcp
)
685 response
= makeppp(b
, sizeof(b
), p
, 2, s
, t
, PPPLCP
);
686 if (!response
) return;
687 *response
= TerminateAck
;
688 *((uint16_t *) (response
+ 2)) = htons(l
= 4);
692 initialise_restart_count(s
, lcp
);
694 if (*response
== ConfigAck
)
695 change_state(s
, lcp
, AckSent
);
697 change_state(s
, lcp
, RequestSent
);
702 if (*response
== ConfigAck
)
703 change_state(s
, lcp
, AckSent
);
708 if (*response
== ConfigAck
)
719 if (*response
== ConfigAck
)
720 change_state(s
, lcp
, AckSent
);
722 change_state(s
, lcp
, RequestSent
);
727 LOG(2, s
, t
, "LCP: ignoring %s in state %s\n", ppp_code(*p
), ppp_state(session
[s
].ppp
.lcp
));
731 LOG(3, s
, t
, "LCP: send %s\n", ppp_code(*response
));
732 if (config
->debug
> 3) dumplcp(response
, l
);
734 tunnelsend(b
, l
+ (response
- b
), t
);
736 else if (*p
== ConfigNak
|| *p
== ConfigRej
)
739 uint8_t *o
= (p
+ 4);
747 if (length
== 0 || type
== 0 || x
< length
) break;
750 case 1: // Maximum-Receive-Unit
753 sess_local
[s
].ppp_mru
= ntohs(*(uint16_t *)(o
+ 2));
754 LOG(3, s
, t
, " Remote requested MRU of %u\n", sess_local
[s
].ppp_mru
);
758 sess_local
[s
].ppp_mru
= 0;
759 LOG(3, s
, t
, " Remote rejected MRU negotiation\n");
764 case 3: // Authentication-Protocol
770 int proto
= ntohs(*(uint16_t *)(o
+ 2));
773 authtype
= config
->radius_authtypes
& AUTHPAP
;
774 LOG(3, s
, t
, " Remote requested PAP authentication...%sing\n",
775 authtype
? "accept" : "reject");
777 else if (proto
== PPPCHAP
&& *(o
+ 4) == 5)
779 authtype
= config
->radius_authtypes
& AUTHCHAP
;
780 LOG(3, s
, t
, " Remote requested CHAP authentication...%sing\n",
781 authtype
? "accept" : "reject");
785 LOG(3, s
, t
, " Rejecting unsupported authentication %#4x\n",
791 LOG(2, s
, t
, "LCP: remote rejected auth negotiation\n");
792 authtype
= 0; // shutdown
797 case 5: // Magic-Number
800 session
[s
].magic
= ntohl(*(uint32_t *)(o
+ 2));
801 LOG(3, s
, t
, " Remote requested magic-no %x\n", session
[s
].magic
);
802 if (!session
[s
].magic
) session
[s
].magic
= time_now
; // Netgear DG814 sends zero??
805 // ConfigRej: fallthrough
808 LOG(2, s
, t
, "LCP: remote sent %s for type %u?\n", ppp_code(*p
), type
);
809 sessionshutdown(s
, "Unable to negotiate LCP.", 3, 0);
818 sessionshutdown(s
, "Unsupported authentication.", 3, 0);
823 sess_local
[s
].lcp_authtype
= authtype
;
825 switch (session
[s
].ppp
.lcp
)
830 uint8_t *response
= makeppp(b
, sizeof(b
), p
, 2, s
, t
, PPPLCP
);
831 if (!response
) return;
832 *response
= TerminateAck
;
833 *((uint16_t *) (response
+ 2)) = htons(l
= 4);
835 LOG(3, s
, t
, "LCP: send %s\n", ppp_code(*response
));
836 if (config
->debug
> 3) dumplcp(response
, l
);
838 tunnelsend(b
, l
+ (response
- b
), t
);
844 initialise_restart_count(s
, lcp
);
849 LOG(2, s
, t
, "LCP: ConfigNak in state %s? Sending ConfigReq\n", ppp_state(session
[s
].ppp
.lcp
));
859 LOG(2, s
, t
, "LCP: ignoring %s in state %s\n", ppp_code(*p
), ppp_state(session
[s
].ppp
.lcp
));
863 else if (*p
== TerminateReq
)
865 *p
= TerminateAck
; // close
866 q
= makeppp(b
, sizeof(b
), p
, l
, s
, t
, PPPLCP
);
869 LOG(3, s
, t
, "LCP: send %s\n", ppp_code(*q
));
870 if (config
->debug
> 3) dumplcp(q
, l
);
872 tunnelsend(b
, l
+ (q
- b
), t
); // send it
873 sessionshutdown(s
, "Remote end closed connection.", 3, 0);
875 else if (*p
== TerminateAck
)
877 sessionshutdown(s
, "Connection closed.", 3, 0);
879 else if (*p
== ProtocolRej
)
886 if (l
> 5 && !(proto
& 1))
893 if (proto
== PPPIPV6CP
)
895 LOG(3, s
, t
, "IPv6 rejected\n");
896 change_state(s
, ipv6cp
, Closed
);
900 LOG(3, s
, t
, "LCP protocol reject: 0x%04X\n", proto
);
903 else if (*p
== EchoReq
)
905 *p
= EchoReply
; // reply
906 *(uint32_t *) (p
+ 4) = htonl(session
[s
].magic
); // our magic number
907 q
= makeppp(b
, sizeof(b
), p
, l
, s
, t
, PPPLCP
);
910 LOG(4, s
, t
, "LCP: send %s\n", ppp_code(*q
));
911 if (config
->debug
> 3) dumplcp(q
, l
);
913 tunnelsend(b
, l
+ (q
- b
), t
); // send it
915 else if (*p
== EchoReply
)
917 // Ignore it, last_packet time is set earlier than this.
919 else if (*p
!= CodeRej
)
921 ppp_code_rej(s
, t
, PPPLCP
, "LCP", p
, l
, b
, sizeof(b
));
925 static void ipcp_open(sessionidt s
, tunnelidt t
)
927 LOG(3, s
, t
, "IPCP: Opened, session is now active\n");
929 change_state(s
, ipcp
, Opened
);
931 if (!(session
[s
].walled_garden
|| session
[s
].flags
& SESSION_STARTED
))
933 uint16_t r
= radiusnew(s
);
936 radiussend(r
, RADIUSSTART
); // send radius start
938 // don't send further Start records if IPCP is restarted
939 session
[s
].flags
|= SESSION_STARTED
;
940 cluster_send_session(s
);
944 // start IPv6 if configured and still in passive state
945 if (session
[s
].ppp
.ipv6cp
== Stopped
)
948 change_state(s
, ipv6cp
, RequestSent
);
952 // Process IPCP messages
953 void processipcp(sessionidt s
, tunnelidt t
, uint8_t *p
, uint16_t l
)
961 LOG_HEX(5, "IPCP", p
, l
);
964 LOG(1, s
, t
, "Short IPCP %d bytes\n", l
);
965 STAT(tunnel_rx_errors
);
969 if ((hl
= ntohs(*(uint16_t *) (p
+ 2))) > l
)
971 LOG(1, s
, t
, "Length mismatch IPCP %u/%u\n", hl
, l
);
972 STAT(tunnel_rx_errors
);
977 if (session
[s
].ppp
.phase
< Network
)
979 LOG(2, s
, t
, "IPCP %s ignored in %s phase\n", ppp_code(*p
), ppp_phase(session
[s
].ppp
.phase
));
983 LOG(3, s
, t
, "IPCP: recv %s\n", ppp_code(*p
));
987 switch (session
[s
].ppp
.ipcp
)
990 initialise_restart_count(s
, ipcp
);
991 change_state(s
, ipcp
, AckReceived
);
996 LOG(2, s
, t
, "IPCP: ConfigAck in state %s? Sending ConfigReq\n", ppp_state(session
[s
].ppp
.ipcp
));
998 change_state(s
, ipcp
, RequestSent
);
1006 LOG(2, s
, t
, "IPCP: ignoring %s in state %s\n", ppp_code(*p
), ppp_state(session
[s
].ppp
.ipcp
));
1009 else if (*p
== ConfigReq
)
1011 uint8_t *response
= 0;
1021 case 3: // ip address
1022 gotip
++; // seen address
1023 if (o
[1] != 6 || o
[1] > length
) return;
1025 addr
= htonl(session
[s
].ip
);
1026 if (memcmp(o
+ 2, &addr
, (sizeof addr
)))
1029 q
= ppp_conf_nak(s
, b
, sizeof(b
), PPPIPCP
, &response
, q
, p
, o
, (uint8_t *) &addr
, sizeof(addr
));
1030 if (!q
|| (q
!= oq
&& *response
== ConfigRej
))
1032 sessionshutdown(s
, "Can't negotiate IPCP.", 3, 0);
1039 case 129: // primary DNS
1040 if (o
[1] != 6 || o
[1] > length
) return;
1042 addr
= htonl(session
[s
].dns1
);
1043 if (memcmp(o
+ 2, &addr
, (sizeof addr
)))
1045 q
= ppp_conf_nak(s
, b
, sizeof(b
), PPPIPCP
, &response
, q
, p
, o
, (uint8_t *) &addr
, sizeof(addr
));
1051 case 131: // secondary DNS
1052 if (o
[1] != 6 || o
[1] > length
) return;
1054 addr
= htonl(session
[s
].dns2
);
1055 if (memcmp(o
+ 2, &addr
, sizeof(addr
)))
1057 q
= ppp_conf_nak(s
, b
, sizeof(b
), PPPIPCP
, &response
, q
, p
, o
, (uint8_t *) &addr
, sizeof(addr
));
1064 LOG(2, s
, t
, " Rejecting PPP IPCP Option type %d\n", *o
);
1065 q
= ppp_conf_rej(s
, b
, sizeof(b
), PPPIPCP
, &response
, q
, p
, o
);
1075 l
= q
- response
; // IPCP packet length
1076 *((uint16_t *) (response
+ 2)) = htons(l
); // update header
1080 // Send packet back as ConfigAck
1081 response
= makeppp(b
, sizeof(b
), p
, l
, s
, t
, PPPIPCP
);
1082 if (!response
) return;
1083 *response
= ConfigAck
;
1087 LOG(1, s
, t
, "No IP in IPCP request\n");
1088 STAT(tunnel_rx_errors
);
1092 switch (session
[s
].ppp
.ipcp
)
1095 response
= makeppp(b
, sizeof(b
), p
, 2, s
, t
, PPPIPCP
);
1096 if (!response
) return;
1097 *response
= TerminateAck
;
1098 *((uint16_t *) (response
+ 2)) = htons(l
= 4);
1102 initialise_restart_count(s
, ipcp
);
1104 if (*response
== ConfigAck
)
1105 change_state(s
, ipcp
, AckSent
);
1107 change_state(s
, ipcp
, RequestSent
);
1112 if (*response
== ConfigAck
)
1113 change_state(s
, ipcp
, AckSent
);
1118 if (*response
== ConfigAck
)
1124 initialise_restart_count(s
, ipcp
);
1129 if (*response
== ConfigAck
)
1130 change_state(s
, ipcp
, AckSent
);
1132 change_state(s
, ipcp
, RequestSent
);
1137 LOG(2, s
, t
, "IPCP: ignoring %s in state %s\n", ppp_code(*p
), ppp_state(session
[s
].ppp
.ipcp
));
1141 LOG(3, s
, t
, "IPCP: send %s\n", ppp_code(*response
));
1142 tunnelsend(b
, l
+ (response
- b
), t
);
1144 else if (*p
== TerminateReq
)
1147 q
= makeppp(b
, sizeof(b
), p
, l
, s
, t
, PPPIPCP
);
1149 LOG(3, s
, t
, "IPCP: send %s\n", ppp_code(*q
));
1150 tunnelsend(b
, l
+ (q
- b
), t
);
1151 change_state(s
, ipcp
, Stopped
);
1153 else if (*p
!= CodeRej
)
1155 ppp_code_rej(s
, t
, PPPIPCP
, "IPCP", p
, l
, b
, sizeof(b
));
1159 static void ipv6cp_open(sessionidt s
, tunnelidt t
)
1161 LOG(3, s
, t
, "IPV6CP: Opened\n");
1163 change_state(s
, ipv6cp
, Opened
);
1164 if (session
[s
].ipv6prefixlen
)
1165 route6set(s
, session
[s
].ipv6route
, session
[s
].ipv6prefixlen
, 1);
1167 // Send an initial RA (TODO: Should we send these regularly?)
1168 send_ipv6_ra(s
, t
, NULL
);
1171 // Process IPV6CP messages
1172 void processipv6cp(sessionidt s
, tunnelidt t
, uint8_t *p
, uint16_t l
)
1174 uint8_t b
[MAXETHER
];
1178 CSTAT(processipv6cp
);
1180 LOG_HEX(5, "IPV6CP", p
, l
);
1183 LOG(1, s
, t
, "Short IPV6CP %d bytes\n", l
);
1184 STAT(tunnel_rx_errors
);
1188 if ((hl
= ntohs(*(uint16_t *) (p
+ 2))) > l
)
1190 LOG(1, s
, t
, "Length mismatch IPV6CP %u/%u\n", hl
, l
);
1191 STAT(tunnel_rx_errors
);
1196 if (session
[s
].ppp
.phase
< Network
)
1198 LOG(2, s
, t
, "IPV6CP %s ignored in %s phase\n", ppp_code(*p
), ppp_phase(session
[s
].ppp
.phase
));
1202 LOG(3, s
, t
, "IPV6CP: recv %s\n", ppp_code(*p
));
1206 LOG(3, s
, t
, "IPV6CP: no IPv4 address (IPCP in state %s)\n", ppp_state(session
[s
].ppp
.ipcp
));
1207 return; // need IPCP to complete...
1210 if (*p
== ConfigAck
)
1212 switch (session
[s
].ppp
.ipv6cp
)
1215 initialise_restart_count(s
, ipv6cp
);
1216 change_state(s
, ipv6cp
, AckReceived
);
1221 LOG(2, s
, t
, "IPV6CP: ConfigAck in state %s? Sending ConfigReq\n", ppp_state(session
[s
].ppp
.ipv6cp
));
1223 change_state(s
, ipv6cp
, RequestSent
);
1231 LOG(2, s
, t
, "IPV6CP: ignoring %s in state %s\n", ppp_code(*p
), ppp_state(session
[s
].ppp
.ipv6cp
));
1234 else if (*p
== ConfigReq
)
1236 uint8_t *response
= 0;
1246 case 1: // interface identifier
1247 gotip
++; // seen address
1248 if (o
[1] != 10 || o
[1] > length
) return;
1250 *(uint32_t *) ident
= htonl(session
[s
].ip
);
1251 *(uint32_t *) (ident
+ 4) = 0;
1253 if (memcmp(o
+ 2, ident
, sizeof(ident
)))
1255 q
= ppp_conf_nak(s
, b
, sizeof(b
), PPPIPV6CP
, &response
, q
, p
, o
, ident
, sizeof(ident
));
1262 LOG(2, s
, t
, " Rejecting PPP IPV6CP Option type %d\n", *o
);
1263 q
= ppp_conf_rej(s
, b
, sizeof(b
), PPPIPV6CP
, &response
, q
, p
, o
);
1273 l
= q
- response
; // IPV6CP packet length
1274 *((uint16_t *) (response
+ 2)) = htons(l
); // update header
1278 // Send packet back as ConfigAck
1279 response
= makeppp(b
, sizeof(b
), p
, l
, s
, t
, PPPIPV6CP
);
1280 if (!response
) return;
1281 *response
= ConfigAck
;
1285 LOG(1, s
, t
, "No interface identifier in IPV6CP request\n");
1286 STAT(tunnel_rx_errors
);
1290 switch (session
[s
].ppp
.ipv6cp
)
1293 response
= makeppp(b
, sizeof(b
), p
, 2, s
, t
, PPPIPV6CP
);
1294 if (!response
) return;
1295 *response
= TerminateAck
;
1296 *((uint16_t *) (response
+ 2)) = htons(l
= 4);
1300 initialise_restart_count(s
, ipv6cp
);
1302 if (*response
== ConfigAck
)
1303 change_state(s
, ipv6cp
, AckSent
);
1305 change_state(s
, ipv6cp
, RequestSent
);
1310 if (*response
== ConfigAck
)
1311 change_state(s
, ipv6cp
, AckSent
);
1316 if (*response
== ConfigAck
)
1322 initialise_restart_count(s
, ipv6cp
);
1327 if (*response
== ConfigAck
)
1328 change_state(s
, ipv6cp
, AckSent
);
1330 change_state(s
, ipv6cp
, RequestSent
);
1335 LOG(2, s
, t
, "IPV6CP: ignoring %s in state %s\n", ppp_code(*p
), ppp_state(session
[s
].ppp
.ipv6cp
));
1339 LOG(3, s
, t
, "IPV6CP: send %s\n", ppp_code(*response
));
1340 tunnelsend(b
, l
+ (response
- b
), t
);
1342 else if (*p
== TerminateReq
)
1345 q
= makeppp(b
, sizeof(b
), p
, l
, s
, t
, PPPIPV6CP
);
1347 LOG(3, s
, t
, "IPV6CP: send %s\n", ppp_code(*q
));
1348 tunnelsend(b
, l
+ (q
- b
), t
);
1349 change_state(s
, ipv6cp
, Stopped
);
1351 else if (*p
!= CodeRej
)
1353 ppp_code_rej(s
, t
, PPPIPV6CP
, "IPV6CP", p
, l
, b
, sizeof(b
));
1357 // process IP packet received
1359 // This MUST be called with at least 4 byte behind 'p'.
1360 // (i.e. this routine writes to p[-4]).
1361 void processipin(sessionidt s
, tunnelidt t
, uint8_t *p
, uint16_t l
)
1367 LOG_HEX(5, "IP", p
, l
);
1371 LOG(1, s
, t
, "IP packet too short %d\n", l
);
1372 STAT(tunnel_rx_errors
);
1376 ip
= ntohl(*(uint32_t *)(p
+ 12));
1380 LOG(1, s
, t
, "IP packet too long %d\n", l
);
1381 STAT(tunnel_rx_errors
);
1385 if (session
[s
].ppp
.phase
!= Network
|| session
[s
].ppp
.ipcp
!= Opened
)
1388 // no spoof (do sessionbyip to handled statically routed subnets)
1389 if (ip
!= session
[s
].ip
&& sessionbyip(htonl(ip
)) != s
)
1391 LOG(5, s
, t
, "Dropping packet with spoofed IP %s\n", fmtaddr(htonl(ip
), 0));
1395 // run access-list if any
1396 if (session
[s
].filter_in
&& !ip_filter(p
, l
, session
[s
].filter_in
- 1))
1399 // adjust MSS on SYN and SYN,ACK packets with options
1400 if ((ntohs(*(uint16_t *) (p
+ 6)) & 0x1fff) == 0 && p
[9] == IPPROTO_TCP
) // first tcp fragment
1402 int ihl
= (p
[0] & 0xf) * 4; // length of IP header
1403 if (l
>= ihl
+ 20 && (p
[ihl
+ 13] & TCP_FLAG_SYN
) && ((p
[ihl
+ 12] >> 4) > 5))
1404 adjust_tcp_mss(s
, t
, p
, l
, p
+ ihl
);
1407 // Add on the tun header
1409 *(uint32_t *) p
= htonl(PKTIP
);
1412 // Are we throttled and a slave?
1413 if (session
[s
].tbf_in
&& !config
->cluster_iam_master
) {
1414 // Pass it to the master for handling.
1415 master_throttle_packet(session
[s
].tbf_in
, p
, l
);
1419 // Are we throttled and a master??
1420 if (session
[s
].tbf_in
&& config
->cluster_iam_master
) {
1421 // Actually handle the throttled packets.
1422 tbf_queue_packet(session
[s
].tbf_in
, p
, l
);
1427 if (tun_write(p
, l
) < 0)
1429 STAT(tun_tx_errors
);
1430 LOG(0, s
, t
, "Error writing %d bytes to TUN device: %s (tunfd=%d, p=%p)\n",
1431 l
, strerror(errno
), tunfd
, p
);
1439 if (session
[s
].snoop_ip
&& session
[s
].snoop_port
)
1441 // Snooping this session
1442 snoop_send_packet(p
, l
, session
[s
].snoop_ip
, session
[s
].snoop_port
);
1445 increment_counter(&session
[s
].cin
, &session
[s
].cin_wrap
, l
);
1446 session
[s
].cin_delta
+= l
;
1449 sess_local
[s
].cin
+= l
;
1450 sess_local
[s
].pin
++;
1454 STAT(tun_tx_packets
);
1455 INC_STAT(tun_tx_bytes
, l
);
1458 // process IPv6 packet received
1460 // This MUST be called with at least 4 byte behind 'p'.
1461 // (i.e. this routine writes to p[-4]).
1462 void processipv6in(sessionidt s
, tunnelidt t
, uint8_t *p
, uint16_t l
)
1467 CSTAT(processipv6in
);
1469 LOG_HEX(5, "IPv6", p
, l
);
1471 ip
= *(struct in6_addr
*) (p
+ 8);
1472 ipv4
= ntohl(*(uint32_t *)(p
+ 16));
1476 LOG(1, s
, t
, "IP packet too long %d\n", l
);
1477 STAT(tunnel_rx_errors
);
1481 if (session
[s
].ppp
.phase
!= Network
|| session
[s
].ppp
.ipv6cp
!= Opened
)
1485 if (ipv4
!= session
[s
].ip
&& memcmp(&config
->ipv6_prefix
, &ip
, 8) && sessionbyipv6(ip
) != s
)
1487 char str
[INET6_ADDRSTRLEN
];
1488 LOG(5, s
, t
, "Dropping packet with spoofed IP %s\n",
1489 inet_ntop(AF_INET6
, &ip
, str
, INET6_ADDRSTRLEN
));
1493 // Check if it's a Router Solicition message.
1494 if (*(p
+ 6) == 58 && *(p
+ 7) == 255 && *(p
+ 24) == 0xFF && *(p
+ 25) == 2 &&
1495 *(uint32_t *)(p
+ 26) == 0 && *(uint32_t *)(p
+ 30) == 0 &&
1496 *(uint32_t *)(p
+ 34) == 0 &&
1497 *(p
+ 38) == 0 && *(p
+ 39) == 2 && *(p
+ 40) == 133) {
1498 LOG(3, s
, t
, "Got IPv6 RS\n");
1499 send_ipv6_ra(s
, t
, &ip
);
1503 // Add on the tun header
1505 *(uint32_t *) p
= htonl(PKTIPV6
);
1508 // Are we throttled and a slave?
1509 if (session
[s
].tbf_in
&& !config
->cluster_iam_master
) {
1510 // Pass it to the master for handling.
1511 master_throttle_packet(session
[s
].tbf_in
, p
, l
);
1515 // Are we throttled and a master??
1516 if (session
[s
].tbf_in
&& config
->cluster_iam_master
) {
1517 // Actually handle the throttled packets.
1518 tbf_queue_packet(session
[s
].tbf_in
, p
, l
);
1523 if (tun_write(p
, l
) < 0)
1525 STAT(tun_tx_errors
);
1526 LOG(0, s
, t
, "Error writing %d bytes to TUN device: %s (tunfd=%d, p=%p)\n",
1527 l
, strerror(errno
), tunfd
, p
);
1535 if (session
[s
].snoop_ip
&& session
[s
].snoop_port
)
1537 // Snooping this session
1538 snoop_send_packet(p
, l
, session
[s
].snoop_ip
, session
[s
].snoop_port
);
1541 increment_counter(&session
[s
].cin
, &session
[s
].cin_wrap
, l
);
1542 session
[s
].cin_delta
+= l
;
1545 sess_local
[s
].cin
+= l
;
1546 sess_local
[s
].pin
++;
1550 STAT(tun_tx_packets
);
1551 INC_STAT(tun_tx_bytes
, l
);
1555 // Helper routine for the TBF filters.
1556 // Used to send queued data in from the user.
1558 void send_ipin(sessionidt s
, uint8_t *buf
, int len
)
1560 LOG_HEX(5, "IP in throttled", buf
, len
);
1562 if (write(tunfd
, buf
, len
) < 0)
1564 STAT(tun_tx_errors
);
1565 LOG(0, 0, 0, "Error writing %d bytes to TUN device: %s (tunfd=%d, p=%p)\n",
1566 len
, strerror(errno
), tunfd
, buf
);
1574 if (session
[s
].snoop_ip
&& session
[s
].snoop_port
)
1576 // Snooping this session
1577 snoop_send_packet(buf
, len
, session
[s
].snoop_ip
, session
[s
].snoop_port
);
1580 // Increment packet counters
1581 increment_counter(&session
[s
].cin
, &session
[s
].cin_wrap
, len
);
1582 session
[s
].cin_delta
+= len
;
1585 sess_local
[s
].cin
+= len
;
1586 sess_local
[s
].pin
++;
1590 STAT(tun_tx_packets
);
1591 INC_STAT(tun_tx_bytes
, len
- 4);
1595 // Process CCP messages
1596 void processccp(sessionidt s
, tunnelidt t
, uint8_t *p
, uint16_t l
)
1598 uint8_t b
[MAXETHER
];
1603 LOG_HEX(5, "CCP", p
, l
);
1605 if (session
[s
].ppp
.phase
< Network
)
1607 LOG(2, s
, t
, "CCP %s ignored in %s phase\n", ppp_code(*p
), ppp_phase(session
[s
].ppp
.phase
));
1613 LOG(1, s
, t
, "Short CCP packet\n");
1614 STAT(tunnel_rx_errors
);
1617 LOG(3, s
, t
, "CCP: recv %s\n", ppp_code(*p
));
1618 if (*p
== ConfigAck
)
1620 switch (session
[s
].ppp
.ccp
)
1623 initialise_restart_count(s
, ccp
);
1624 change_state(s
, ccp
, AckReceived
);
1629 LOG(2, s
, t
, "CCP: ConfigAck in state %s? Sending ConfigReq\n", ppp_state(session
[s
].ppp
.ccp
));
1631 change_state(s
, ccp
, RequestSent
);
1635 LOG(3, s
, t
, "CCP: Opened\n");
1636 change_state(s
, ccp
, Opened
);
1640 LOG(2, s
, t
, "CCP: ignoring %s in state %s\n", ppp_code(*p
), ppp_state(session
[s
].ppp
.ccp
));
1643 else if (*p
== ConfigReq
)
1645 if (l
< 6) // accept no compression
1647 else // compression requested--reject
1650 q
= makeppp(b
, sizeof(b
), p
, l
, s
, t
, PPPCCP
);
1653 switch (session
[s
].ppp
.ccp
)
1656 q
= makeppp(b
, sizeof(b
), p
, 2, s
, t
, PPPCCP
);
1659 *((uint16_t *) (q
+ 2)) = htons(l
= 4);
1663 initialise_restart_count(s
, ccp
);
1665 if (*q
== ConfigAck
)
1666 change_state(s
, ccp
, AckSent
);
1668 change_state(s
, ccp
, RequestSent
);
1673 if (*q
== ConfigAck
)
1674 change_state(s
, ccp
, AckSent
);
1679 if (*q
== ConfigAck
)
1680 change_state(s
, ccp
, Opened
);
1685 initialise_restart_count(s
, ccp
);
1690 if (*q
== ConfigAck
)
1691 change_state(s
, ccp
, AckSent
);
1693 change_state(s
, ccp
, RequestSent
);
1698 LOG(2, s
, t
, "CCP: ignoring %s in state %s\n", ppp_code(*p
), ppp_state(session
[s
].ppp
.ccp
));
1702 LOG(3, s
, t
, "CCP: send %s\n", ppp_code(*q
));
1703 tunnelsend(b
, l
+ (q
- b
), t
);
1705 else if (*p
== TerminateReq
)
1708 q
= makeppp(b
, sizeof(b
), p
, l
, s
, t
, PPPCCP
);
1710 LOG(3, s
, t
, "CCP: send %s\n", ppp_code(*q
));
1711 tunnelsend(b
, l
+ (q
- b
), t
);
1712 change_state(s
, ccp
, Stopped
);
1714 else if (*p
!= CodeRej
)
1716 ppp_code_rej(s
, t
, PPPCCP
, "CCP", p
, l
, b
, sizeof(b
));
1720 // send a CHAP challenge
1721 void sendchap(sessionidt s
, tunnelidt t
)
1723 uint8_t b
[MAXETHER
];
1732 LOG(1, s
, t
, "No RADIUS to send challenge\n");
1733 STAT(tunnel_tx_errors
);
1737 LOG(1, s
, t
, "Send CHAP challenge\n");
1739 radius
[r
].chap
= 1; // CHAP not PAP
1741 if (radius
[r
].state
!= RADIUSCHAP
)
1744 radius
[r
].state
= RADIUSCHAP
;
1745 radius
[r
].retry
= backoff(radius
[r
].try++);
1746 if (radius
[r
].try > 5)
1748 sessionshutdown(s
, "CHAP timeout.", 3, 0);
1749 STAT(tunnel_tx_errors
);
1752 q
= makeppp(b
, sizeof(b
), 0, 0, s
, t
, PPPCHAP
);
1755 *q
= 1; // challenge
1756 q
[1] = radius
[r
].id
; // ID
1757 q
[4] = 16; // value size (size of challenge)
1758 memcpy(q
+ 5, radius
[r
].auth
, 16); // challenge
1759 strcpy((char *) q
+ 21, hostname
); // our name
1760 *(uint16_t *) (q
+ 2) = htons(strlen(hostname
) + 21); // length
1761 tunnelsend(b
, strlen(hostname
) + 21 + (q
- b
), t
); // send it
1764 // fill in a L2TP message with a PPP frame,
1765 // copies existing PPP message and changes magic number if seen
1766 // returns start of PPP frame
1767 uint8_t *makeppp(uint8_t *b
, int size
, uint8_t *p
, int l
, sessionidt s
, tunnelidt t
, uint16_t mtype
)
1769 if (size
< 12) // Need more space than this!!
1771 LOG(0, s
, t
, "makeppp buffer too small for L2TP header (size=%d)\n", size
);
1775 *(uint16_t *) (b
+ 0) = htons(0x0002); // L2TP with no options
1776 *(uint16_t *) (b
+ 2) = htons(tunnel
[t
].far
); // tunnel
1777 *(uint16_t *) (b
+ 4) = htons(session
[s
].far
); // session
1779 if (mtype
== PPPLCP
|| !(session
[s
].flags
& SESSION_ACFC
))
1781 *(uint16_t *) b
= htons(0xFF03); // HDLC header
1784 if (mtype
< 0x100 && session
[s
].flags
& SESSION_PFC
)
1788 *(uint16_t *) b
= htons(mtype
);
1794 LOG(2, s
, t
, "makeppp would overflow buffer (size=%d, header+payload=%d)\n", size
, l
+ 12);
1804 static int add_lcp_auth(uint8_t *b
, int size
, int authtype
)
1807 if ((authtype
== AUTHCHAP
&& size
< 5) || size
< 4)
1810 *b
++ = 3; // Authentication-Protocol
1811 if (authtype
== AUTHCHAP
)
1813 len
= *b
++ = 5; // length
1814 *(uint16_t *) b
= htons(PPPCHAP
); b
+= 2;
1817 else if (authtype
== AUTHPAP
)
1819 len
= *b
++ = 4; // length
1820 *(uint16_t *) b
= htons(PPPPAP
); b
+= 2;
1824 LOG(0, 0, 0, "add_lcp_auth called with unsupported auth type %d\n", authtype
);
1830 // Send initial LCP ConfigReq for MRU, authentication type and magic no
1831 void sendlcp(sessionidt s
, tunnelidt t
)
1833 uint8_t b
[500], *q
, *l
;
1834 int authtype
= sess_local
[s
].lcp_authtype
;
1836 if (!(q
= makeppp(b
, sizeof(b
), NULL
, 0, s
, t
, PPPLCP
)))
1839 LOG(3, s
, t
, "LCP: send ConfigReq%s%s%s\n",
1840 authtype
? " (" : "",
1841 authtype
? (authtype
== AUTHCHAP
? "CHAP" : "PAP") : "",
1842 authtype
? ")" : "");
1846 *l
++ = ++sess_local
[s
].lcp_ident
; // ID
1848 l
+= 2; //Save space for length
1850 if (sess_local
[s
].ppp_mru
)
1852 *l
++ = 1; *l
++ = 4; // Maximum-Receive-Unit (length 4)
1853 *(uint16_t *) l
= htons(sess_local
[s
].ppp_mru
); l
+= 2;
1857 l
+= add_lcp_auth(l
, sizeof(b
) - (l
- b
), authtype
);
1859 *l
++ = 5; *l
++ = 6; // Magic-Number (length 6)
1860 *(uint32_t *) l
= htonl(session
[s
].magic
);
1863 *(uint16_t *)(q
+ 2) = htons(l
- q
); // Length
1865 LOG_HEX(5, "PPPLCP", q
, l
- q
);
1866 if (config
->debug
> 3) dumplcp(q
, l
- q
);
1868 tunnelsend(b
, (l
- b
), t
);
1869 restart_timer(s
, lcp
);
1872 // Send CCP request for no compression
1873 void sendccp(sessionidt s
, tunnelidt t
)
1877 if (!(q
= makeppp(b
, sizeof(b
), NULL
, 0, s
, t
, PPPCCP
)))
1880 LOG(3, s
, t
, "CCP: send ConfigReq (no compression)\n");
1883 *(q
+ 1) = ++sess_local
[s
].lcp_ident
; // ID
1884 *(uint16_t *)(q
+ 2) = htons(4); // Length
1886 LOG_HEX(5, "PPPCCP", q
, 4);
1887 tunnelsend(b
, (q
- b
) + 4 , t
);
1888 restart_timer(s
, ccp
);
1891 // Reject unknown/unconfigured protocols
1892 void protoreject(sessionidt s
, tunnelidt t
, uint8_t *p
, uint16_t l
, uint16_t proto
)
1895 uint8_t buf
[MAXETHER
];
1897 int mru
= session
[s
].mru
;
1898 if (mru
< MINMTU
) mru
= MINMTU
;
1899 if (mru
> sizeof(buf
)) mru
= sizeof(buf
);
1902 if (l
> mru
) l
= mru
;
1904 q
= makeppp(buf
, sizeof(buf
), 0, 0, s
, t
, PPPLCP
);
1908 *(q
+ 1) = ++sess_local
[s
].lcp_ident
;
1909 *(uint16_t *)(q
+ 2) = htons(l
);
1910 *(uint16_t *)(q
+ 4) = htons(proto
);
1911 memcpy(q
+ 6, p
, l
- 6);
1913 if (proto
== PPPIPV6CP
)
1914 LOG(3, s
, t
, "LCP: send ProtocolRej (IPV6CP: not configured)\n");
1916 LOG(2, s
, t
, "LCP: sent ProtocolRej (0x%04X: unsupported)\n", proto
);
1918 tunnelsend(buf
, l
+ (q
- buf
), t
);