4215cb8117b05a5b925b76766e5c126b9656f248
3 char const *cvs_id_ppp
= "$Id: ppp.c,v 1.84 2005-09-16 13:20:39 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
> size
) mru
= size
;
455 if (l
> mru
) l
= mru
;
457 q
= makeppp(buf
, size
, 0, 0, s
, t
, proto
);
461 *(q
+ 1) = ++sess_local
[s
].lcp_ident
;
462 *(uint16_t *)(q
+ 2) = htons(l
);
463 memcpy(q
+ 4, p
, l
- 4);
465 LOG(2, s
, t
, "Unexpected %s code %s\n", pname
, ppp_code(*p
));
466 LOG(3, s
, t
, "%s: send %s\n", pname
, ppp_code(*q
));
467 if (config
->debug
> 3) dumplcp(q
, l
);
469 tunnelsend(buf
, l
+ (q
- buf
), t
);
472 // Process LCP messages
473 void processlcp(sessionidt s
, tunnelidt t
, uint8_t *p
, uint16_t l
)
477 uint32_t magicno
= 0;
482 LOG_HEX(5, "LCP", p
, l
);
485 LOG(1, s
, t
, "Short LCP %d bytes\n", l
);
486 STAT(tunnel_rx_errors
);
490 if ((hl
= ntohs(*(uint16_t *) (p
+ 2))) > l
)
492 LOG(1, s
, t
, "Length mismatch LCP %u/%u\n", hl
, l
);
493 STAT(tunnel_rx_errors
);
498 if (session
[s
].die
) // going down...
501 LOG(*p
== EchoReq
? 4 : 3, s
, t
, "LCP: recv %s\n", ppp_code(*p
));
502 if (config
->debug
> 3) dumplcp(p
, l
);
507 uint8_t *o
= (p
+ 4);
515 if (length
== 0 || type
== 0 || x
< length
) break;
518 case 3: // Authentication-Protocol
520 int proto
= ntohs(*(uint16_t *)(o
+ 2));
523 else if (proto
== PPPCHAP
&& *(o
+ 4) == 5)
533 if (!session
[s
].ip
&& authtype
)
534 sess_local
[s
].lcp_authtype
= authtype
;
536 switch (session
[s
].ppp
.lcp
)
539 initialise_restart_count(s
, lcp
);
540 change_state(s
, lcp
, AckReceived
);
545 LOG(2, s
, t
, "LCP: ConfigAck in state %s? Sending ConfigReq\n", ppp_state(session
[s
].ppp
.lcp
));
546 if (session
[s
].ppp
.lcp
== Opened
)
550 change_state(s
, lcp
, RequestSent
);
558 LOG(2, s
, t
, "LCP: ignoring %s in state %s\n", ppp_code(*p
), ppp_state(session
[s
].ppp
.lcp
));
561 else if (*p
== ConfigReq
)
564 uint8_t *o
= (p
+ 4);
565 uint8_t *response
= 0;
566 static uint8_t asyncmap
[4] = { 0, 0, 0, 0 }; // all zero
567 static uint8_t authproto
[5];
574 if (length
== 0 || type
== 0 || x
< length
) break;
577 case 1: // Maximum-Receive-Unit
578 session
[s
].mru
= ntohs(*(uint16_t *)(o
+ 2));
581 case 2: // Async-Control-Character-Map
582 if (!ntohl(*(uint32_t *)(o
+ 2))) // all bits zero is OK
585 LOG(3, s
, t
, " Remote requesting asyncmap. Rejecting.\n");
586 q
= ppp_conf_nak(s
, b
, sizeof(b
), PPPLCP
, &response
, q
, p
, o
, asyncmap
, sizeof(asyncmap
));
589 case 3: // Authentication-Protocol
591 int proto
= ntohs(*(uint16_t *)(o
+ 2));
592 char proto_name
[] = "0x0000";
597 if (config
->radius_authtypes
& AUTHPAP
)
599 sess_local
[s
].lcp_authtype
= AUTHPAP
;
603 strcpy(proto_name
, "PAP");
605 else if (proto
== PPPCHAP
)
607 if (config
->radius_authtypes
& AUTHCHAP
608 && *(o
+ 4) == 5) // MD5
610 sess_local
[s
].lcp_authtype
= AUTHCHAP
;
614 strcpy(proto_name
, "CHAP");
617 sprintf(proto_name
, "%#4.4x", proto
);
619 LOG(3, s
, t
, " Remote requesting %s authentication. Rejecting.\n", proto_name
);
621 alen
= add_lcp_auth(authproto
, sizeof(authproto
), config
->radius_authprefer
);
622 if (alen
< 2) break; // paranoia
624 q
= ppp_conf_nak(s
, b
, sizeof(b
), PPPLCP
, &response
, q
, p
, o
, authproto
+ 2, alen
- 2);
625 if (q
&& *response
== ConfigNak
&&
626 config
->radius_authtypes
!= config
->radius_authprefer
)
629 alen
= add_lcp_auth(authproto
, sizeof(authproto
), config
->radius_authtypes
& ~config
->radius_authprefer
);
631 q
= ppp_conf_nak(s
, b
, sizeof(b
), PPPLCP
, &response
, q
, p
, o
, authproto
+ 2, alen
- 2);
638 case 5: // Magic-Number
639 magicno
= ntohl(*(uint32_t *)(o
+ 2));
642 case 4: // Quality-Protocol
643 case 7: // Protocol-Field-Compression
644 case 8: // Address-And-Control-Field-Compression
647 default: // Reject any unknown options
648 LOG(3, s
, t
, " Rejecting unknown PPP LCP option %d\n", type
);
649 q
= ppp_conf_rej(s
, b
, sizeof(b
), PPPLCP
, &response
, q
, p
, o
);
657 l
= q
- response
; // LCP packet length
658 *((uint16_t *) (response
+ 2)) = htons(l
); // update header
662 // Send packet back as ConfigAck
663 response
= makeppp(b
, sizeof(b
), p
, l
, s
, t
, PPPLCP
);
664 if (!response
) return;
665 *response
= ConfigAck
;
668 switch (session
[s
].ppp
.lcp
)
671 response
= makeppp(b
, sizeof(b
), p
, 2, s
, t
, PPPLCP
);
672 if (!response
) return;
673 *response
= TerminateAck
;
674 *((uint16_t *) (response
+ 2)) = htons(l
= 4);
678 initialise_restart_count(s
, lcp
);
680 if (*response
== ConfigAck
)
681 change_state(s
, lcp
, AckSent
);
683 change_state(s
, lcp
, RequestSent
);
688 if (*response
== ConfigAck
)
689 change_state(s
, lcp
, AckSent
);
694 if (*response
== ConfigAck
)
705 if (*response
== ConfigAck
)
706 change_state(s
, lcp
, AckSent
);
708 change_state(s
, lcp
, RequestSent
);
713 LOG(2, s
, t
, "LCP: ignoring %s in state %s\n", ppp_code(*p
), ppp_state(session
[s
].ppp
.lcp
));
717 LOG(3, s
, t
, "LCP: send %s\n", ppp_code(*response
));
718 if (config
->debug
> 3) dumplcp(response
, l
);
720 tunnelsend(b
, l
+ (response
- b
), t
);
722 else if (*p
== ConfigNak
|| *p
== ConfigRej
)
725 uint8_t *o
= (p
+ 4);
733 if (length
== 0 || type
== 0 || x
< length
) break;
736 case 1: // Maximum-Receive-Unit
739 sess_local
[s
].ppp_mru
= ntohs(*(uint16_t *)(o
+ 2));
740 LOG(3, s
, t
, " Remote requested MRU of %u\n", sess_local
[s
].ppp_mru
);
744 sess_local
[s
].ppp_mru
= 0;
745 LOG(3, s
, t
, " Remote rejected MRU negotiation\n");
750 case 3: // Authentication-Protocol
756 int proto
= ntohs(*(uint16_t *)(o
+ 2));
759 authtype
= config
->radius_authtypes
& AUTHPAP
;
760 LOG(3, s
, t
, " Remote requested PAP authentication...%sing\n",
761 authtype
? "accept" : "reject");
763 else if (proto
== PPPCHAP
&& *(o
+ 4) == 5)
765 authtype
= config
->radius_authtypes
& AUTHCHAP
;
766 LOG(3, s
, t
, " Remote requested CHAP authentication...%sing\n",
767 authtype
? "accept" : "reject");
771 LOG(3, s
, t
, " Rejecting unsupported authentication %#4x\n",
777 LOG(2, s
, t
, "LCP: remote rejected auth negotiation\n");
778 authtype
= 0; // shutdown
784 LOG(2, s
, t
, "LCP: remote sent %s for type %u?\n", ppp_code(*p
), type
);
793 sessionshutdown(s
, "Unsupported authentication.", 3, 0);
798 sess_local
[s
].lcp_authtype
= authtype
;
800 switch (session
[s
].ppp
.lcp
)
805 uint8_t *response
= makeppp(b
, sizeof(b
), p
, 2, s
, t
, PPPLCP
);
806 if (!response
) return;
807 *response
= TerminateAck
;
808 *((uint16_t *) (response
+ 2)) = htons(l
= 4);
810 LOG(3, s
, t
, "LCP: send %s\n", ppp_code(*response
));
811 if (config
->debug
> 3) dumplcp(response
, l
);
813 tunnelsend(b
, l
+ (response
- b
), t
);
819 initialise_restart_count(s
, lcp
);
824 LOG(2, s
, t
, "LCP: ConfigNak in state %s? Sending ConfigReq\n", ppp_state(session
[s
].ppp
.lcp
));
834 LOG(2, s
, t
, "LCP: ignoring %s in state %s\n", ppp_code(*p
), ppp_state(session
[s
].ppp
.lcp
));
838 else if (*p
== TerminateReq
)
840 *p
= TerminateAck
; // close
841 q
= makeppp(b
, sizeof(b
), p
, l
, s
, t
, PPPLCP
);
844 LOG(3, s
, t
, "LCP: send %s\n", ppp_code(*q
));
845 if (config
->debug
> 3) dumplcp(q
, l
);
847 tunnelsend(b
, l
+ (q
- b
), t
); // send it
848 sessionshutdown(s
, "Remote end closed connection.", 3, 0);
850 else if (*p
== TerminateAck
)
852 sessionshutdown(s
, "Connection closed.", 3, 0);
854 else if (*p
== ProtocolRej
)
861 if (l
> 5 && !(proto
& 1))
868 if (proto
== PPPIPV6CP
)
870 LOG(3, s
, t
, "IPv6 rejected\n");
871 change_state(s
, ipv6cp
, Closed
);
875 LOG(3, s
, t
, "LCP protocol reject: 0x%04X\n", proto
);
878 else if (*p
== EchoReq
)
880 *p
= EchoReply
; // reply
881 *(uint32_t *) (p
+ 4) = htonl(session
[s
].magic
); // our magic number
882 q
= makeppp(b
, sizeof(b
), p
, l
, s
, t
, PPPLCP
);
885 LOG(4, s
, t
, "LCP: send %s\n", ppp_code(*q
));
886 if (config
->debug
> 3) dumplcp(q
, l
);
888 tunnelsend(b
, l
+ (q
- b
), t
); // send it
890 else if (*p
== EchoReply
)
892 // Ignore it, last_packet time is set earlier than this.
894 else if (*p
!= CodeRej
)
896 ppp_code_rej(s
, t
, PPPLCP
, "LCP", p
, l
, b
, sizeof(b
));
900 static void ipcp_open(sessionidt s
, tunnelidt t
)
902 LOG(3, s
, t
, "IPCP: Opened, session is now active\n");
904 change_state(s
, ipcp
, Opened
);
906 if (!session
[s
].walled_garden
)
908 uint16_t r
= radiusnew(s
);
910 radiussend(r
, RADIUSSTART
); // send radius start
913 // start IPv6 if configured and still in passive state
914 if (session
[s
].ppp
.ipv6cp
== Stopped
)
917 change_state(s
, ipv6cp
, RequestSent
);
921 // Process IPCP messages
922 void processipcp(sessionidt s
, tunnelidt t
, uint8_t *p
, uint16_t l
)
930 LOG_HEX(5, "IPCP", p
, l
);
933 LOG(1, s
, t
, "Short IPCP %d bytes\n", l
);
934 STAT(tunnel_rx_errors
);
938 if ((hl
= ntohs(*(uint16_t *) (p
+ 2))) > l
)
940 LOG(1, s
, t
, "Length mismatch IPCP %u/%u\n", hl
, l
);
941 STAT(tunnel_rx_errors
);
946 if (session
[s
].ppp
.phase
< Network
)
948 LOG(2, s
, t
, "IPCP %s ignored in %s phase\n", ppp_code(*p
), ppp_phase(session
[s
].ppp
.phase
));
952 LOG(3, s
, t
, "IPCP: recv %s\n", ppp_code(*p
));
956 switch (session
[s
].ppp
.ipcp
)
959 initialise_restart_count(s
, ipcp
);
960 change_state(s
, ipcp
, AckReceived
);
965 LOG(2, s
, t
, "IPCP: ConfigAck in state %s? Sending ConfigReq\n", ppp_state(session
[s
].ppp
.ipcp
));
967 change_state(s
, ipcp
, RequestSent
);
975 LOG(2, s
, t
, "IPCP: ignoring %s in state %s\n", ppp_code(*p
), ppp_state(session
[s
].ppp
.ipcp
));
978 else if (*p
== ConfigReq
)
980 uint8_t *response
= 0;
990 case 3: // ip address
991 gotip
++; // seen address
992 if (o
[1] != 6 || o
[1] > length
) return;
994 addr
= htonl(session
[s
].ip
);
995 if (memcmp(o
+ 2, &addr
, (sizeof addr
)))
998 q
= ppp_conf_nak(s
, b
, sizeof(b
), PPPIPCP
, &response
, q
, p
, o
, (uint8_t *) &addr
, sizeof(addr
));
999 if (!q
|| (q
!= oq
&& *response
== ConfigRej
))
1001 sessionshutdown(s
, "Can't negotiate IPCP.", 3, 0);
1008 case 129: // primary DNS
1009 if (o
[1] != 6 || o
[1] > length
) return;
1011 addr
= htonl(session
[s
].dns1
);
1012 if (memcmp(o
+ 2, &addr
, (sizeof addr
)))
1014 q
= ppp_conf_nak(s
, b
, sizeof(b
), PPPIPCP
, &response
, q
, p
, o
, (uint8_t *) &addr
, sizeof(addr
));
1020 case 131: // secondary DNS
1021 if (o
[1] != 6 || o
[1] > length
) return;
1023 addr
= htonl(session
[s
].dns1
);
1024 if (memcmp(o
+ 2, &addr
, sizeof(addr
)))
1026 q
= ppp_conf_nak(s
, b
, sizeof(b
), PPPIPCP
, &response
, q
, p
, o
, (uint8_t *) &addr
, sizeof(addr
));
1033 LOG(2, s
, t
, " Rejecting PPP IPCP Option type %d\n", *o
);
1034 q
= ppp_conf_rej(s
, b
, sizeof(b
), PPPIPCP
, &response
, q
, p
, o
);
1044 l
= q
- response
; // IPCP packet length
1045 *((uint16_t *) (response
+ 2)) = htons(l
); // update header
1049 // Send packet back as ConfigAck
1050 response
= makeppp(b
, sizeof(b
), p
, l
, s
, t
, PPPIPCP
);
1051 if (!response
) return;
1052 *response
= ConfigAck
;
1056 LOG(1, s
, t
, "No IP in IPCP request\n");
1057 STAT(tunnel_rx_errors
);
1061 switch (session
[s
].ppp
.ipcp
)
1064 response
= makeppp(b
, sizeof(b
), p
, 2, s
, t
, PPPIPCP
);
1065 if (!response
) return;
1066 *response
= TerminateAck
;
1067 *((uint16_t *) (response
+ 2)) = htons(l
= 4);
1071 initialise_restart_count(s
, ipcp
);
1073 if (*response
== ConfigAck
)
1074 change_state(s
, ipcp
, AckSent
);
1076 change_state(s
, ipcp
, RequestSent
);
1081 if (*response
== ConfigAck
)
1082 change_state(s
, ipcp
, AckSent
);
1087 if (*response
== ConfigAck
)
1093 initialise_restart_count(s
, ipcp
);
1098 if (*response
== ConfigAck
)
1099 change_state(s
, ipcp
, AckSent
);
1101 change_state(s
, ipcp
, RequestSent
);
1106 LOG(2, s
, t
, "IPCP: ignoring %s in state %s\n", ppp_code(*p
), ppp_state(session
[s
].ppp
.ipcp
));
1110 LOG(3, s
, t
, "IPCP: send %s\n", ppp_code(*response
));
1111 tunnelsend(b
, l
+ (response
- b
), t
);
1113 else if (*p
== TerminateReq
)
1116 q
= makeppp(b
, sizeof(b
), p
, l
, s
, t
, PPPIPCP
);
1118 LOG(3, s
, t
, "IPCP: send %s\n", ppp_code(*q
));
1119 tunnelsend(b
, l
+ (q
- b
), t
);
1120 change_state(s
, ipcp
, Stopped
);
1122 else if (*p
!= CodeRej
)
1124 ppp_code_rej(s
, t
, PPPIPCP
, "IPCP", p
, l
, b
, sizeof(b
));
1128 static void ipv6cp_open(sessionidt s
, tunnelidt t
)
1130 LOG(3, s
, t
, "IPV6CP: Opened\n");
1132 change_state(s
, ipv6cp
, Opened
);
1133 if (session
[s
].ipv6prefixlen
)
1134 route6set(s
, session
[s
].ipv6route
, session
[s
].ipv6prefixlen
, 1);
1136 // Send an initial RA (TODO: Should we send these regularly?)
1137 send_ipv6_ra(s
, t
, NULL
);
1140 // Process IPV6CP messages
1141 void processipv6cp(sessionidt s
, tunnelidt t
, uint8_t *p
, uint16_t l
)
1143 uint8_t b
[MAXETHER
];
1147 CSTAT(processipv6cp
);
1149 LOG_HEX(5, "IPV6CP", p
, l
);
1152 LOG(1, s
, t
, "Short IPV6CP %d bytes\n", l
);
1153 STAT(tunnel_rx_errors
);
1157 if ((hl
= ntohs(*(uint16_t *) (p
+ 2))) > l
)
1159 LOG(1, s
, t
, "Length mismatch IPV6CP %u/%u\n", hl
, l
);
1160 STAT(tunnel_rx_errors
);
1165 if (session
[s
].ppp
.phase
< Network
)
1167 LOG(2, s
, t
, "IPV6CP %s ignored in %s phase\n", ppp_code(*p
), ppp_phase(session
[s
].ppp
.phase
));
1171 LOG(3, s
, t
, "IPV6CP: recv %s\n", ppp_code(*p
));
1175 LOG(3, s
, t
, "IPV6CP: no IPv4 address (IPCP in state %s)\n", ppp_state(session
[s
].ppp
.ipcp
));
1176 return; // need IPCP to complete...
1179 if (*p
== ConfigAck
)
1181 switch (session
[s
].ppp
.ipv6cp
)
1184 initialise_restart_count(s
, ipv6cp
);
1185 change_state(s
, ipv6cp
, AckReceived
);
1190 LOG(2, s
, t
, "IPV6CP: ConfigAck in state %s? Sending ConfigReq\n", ppp_state(session
[s
].ppp
.ipv6cp
));
1192 change_state(s
, ipv6cp
, RequestSent
);
1200 LOG(2, s
, t
, "IPV6CP: ignoring %s in state %s\n", ppp_code(*p
), ppp_state(session
[s
].ppp
.ipv6cp
));
1203 else if (*p
== ConfigReq
)
1205 uint8_t *response
= 0;
1215 case 1: // interface identifier
1216 gotip
++; // seen address
1217 if (o
[1] != 10 || o
[1] > length
) return;
1219 *(uint32_t *) ident
= htonl(session
[s
].ip
);
1220 *(uint32_t *) (ident
+ 4) = 0;
1222 if (memcmp(o
+ 2, ident
, sizeof(ident
)))
1224 q
= ppp_conf_nak(s
, b
, sizeof(b
), PPPIPV6CP
, &response
, q
, p
, o
, ident
, sizeof(ident
));
1231 LOG(2, s
, t
, " Rejecting PPP IPV6CP Option type %d\n", *o
);
1232 q
= ppp_conf_rej(s
, b
, sizeof(b
), PPPIPV6CP
, &response
, q
, p
, o
);
1242 l
= q
- response
; // IPV6CP packet length
1243 *((uint16_t *) (response
+ 2)) = htons(l
); // update header
1247 // Send packet back as ConfigAck
1248 response
= makeppp(b
, sizeof(b
), p
, l
, s
, t
, PPPIPV6CP
);
1249 if (!response
) return;
1250 *response
= ConfigAck
;
1254 LOG(1, s
, t
, "No interface identifier in IPV6CP request\n");
1255 STAT(tunnel_rx_errors
);
1259 switch (session
[s
].ppp
.ipv6cp
)
1262 response
= makeppp(b
, sizeof(b
), p
, 2, s
, t
, PPPIPV6CP
);
1263 if (!response
) return;
1264 *response
= TerminateAck
;
1265 *((uint16_t *) (response
+ 2)) = htons(l
= 4);
1269 initialise_restart_count(s
, ipv6cp
);
1271 if (*response
== ConfigAck
)
1272 change_state(s
, ipv6cp
, AckSent
);
1274 change_state(s
, ipv6cp
, RequestSent
);
1279 if (*response
== ConfigAck
)
1280 change_state(s
, ipv6cp
, AckSent
);
1285 if (*response
== ConfigAck
)
1291 initialise_restart_count(s
, ipv6cp
);
1296 if (*response
== ConfigAck
)
1297 change_state(s
, ipv6cp
, AckSent
);
1299 change_state(s
, ipv6cp
, RequestSent
);
1304 LOG(2, s
, t
, "IPV6CP: ignoring %s in state %s\n", ppp_code(*p
), ppp_state(session
[s
].ppp
.ipv6cp
));
1308 LOG(3, s
, t
, "IPV6CP: send %s\n", ppp_code(*response
));
1309 tunnelsend(b
, l
+ (response
- b
), t
);
1311 else if (*p
== TerminateReq
)
1314 q
= makeppp(b
, sizeof(b
), p
, l
, s
, t
, PPPIPV6CP
);
1316 LOG(3, s
, t
, "IPV6CP: send %s\n", ppp_code(*q
));
1317 tunnelsend(b
, l
+ (q
- b
), t
);
1318 change_state(s
, ipv6cp
, Stopped
);
1320 else if (*p
!= CodeRej
)
1322 ppp_code_rej(s
, t
, PPPIPV6CP
, "IPV6CP", p
, l
, b
, sizeof(b
));
1326 // process IP packet received
1328 // This MUST be called with at least 4 byte behind 'p'.
1329 // (i.e. this routine writes to p[-4]).
1330 void processipin(sessionidt s
, tunnelidt t
, uint8_t *p
, uint16_t l
)
1336 LOG_HEX(5, "IP", p
, l
);
1340 LOG(1, s
, t
, "IP packet too short %d\n", l
);
1341 STAT(tunnel_rx_errors
);
1345 ip
= ntohl(*(uint32_t *)(p
+ 12));
1349 LOG(1, s
, t
, "IP packet too long %d\n", l
);
1350 STAT(tunnel_rx_errors
);
1354 if (session
[s
].ppp
.phase
!= Network
|| session
[s
].ppp
.ipcp
!= Opened
)
1357 // no spoof (do sessionbyip to handled statically routed subnets)
1358 if (ip
!= session
[s
].ip
&& sessionbyip(htonl(ip
)) != s
)
1360 LOG(5, s
, t
, "Dropping packet with spoofed IP %s\n", fmtaddr(htonl(ip
), 0));
1364 // run access-list if any
1365 if (session
[s
].filter_in
&& !ip_filter(p
, l
, session
[s
].filter_in
- 1))
1368 // adjust MSS on SYN and SYN,ACK packets with options
1369 if ((ntohs(*(uint16_t *) (p
+ 6)) & 0x1fff) == 0 && p
[9] == IPPROTO_TCP
) // first tcp fragment
1371 int ihl
= (p
[0] & 0xf) * 4; // length of IP header
1372 if (l
>= ihl
+ 20 && (p
[ihl
+ 13] & TCP_FLAG_SYN
) && ((p
[ihl
+ 12] >> 4) > 5))
1373 adjust_tcp_mss(s
, t
, p
, l
, p
+ ihl
);
1376 // Add on the tun header
1378 *(uint32_t *) p
= htonl(PKTIP
);
1381 // Are we throttled and a slave?
1382 if (session
[s
].tbf_in
&& !config
->cluster_iam_master
) {
1383 // Pass it to the master for handling.
1384 master_throttle_packet(session
[s
].tbf_in
, p
, l
);
1388 // Are we throttled and a master??
1389 if (session
[s
].tbf_in
&& config
->cluster_iam_master
) {
1390 // Actually handle the throttled packets.
1391 tbf_queue_packet(session
[s
].tbf_in
, p
, l
);
1396 if (tun_write(p
, l
) < 0)
1398 STAT(tun_tx_errors
);
1399 LOG(0, s
, t
, "Error writing %d bytes to TUN device: %s (tunfd=%d, p=%p)\n",
1400 l
, strerror(errno
), tunfd
, p
);
1408 if (session
[s
].snoop_ip
&& session
[s
].snoop_port
)
1410 // Snooping this session
1411 snoop_send_packet(p
, l
, session
[s
].snoop_ip
, session
[s
].snoop_port
);
1414 increment_counter(&session
[s
].cin
, &session
[s
].cin_wrap
, l
);
1415 session
[s
].cin_delta
+= l
;
1418 sess_local
[s
].cin
+= l
;
1419 sess_local
[s
].pin
++;
1423 STAT(tun_tx_packets
);
1424 INC_STAT(tun_tx_bytes
, l
);
1427 // process IPv6 packet received
1429 // This MUST be called with at least 4 byte behind 'p'.
1430 // (i.e. this routine writes to p[-4]).
1431 void processipv6in(sessionidt s
, tunnelidt t
, uint8_t *p
, uint16_t l
)
1436 CSTAT(processipv6in
);
1438 LOG_HEX(5, "IPv6", p
, l
);
1440 ip
= *(struct in6_addr
*) (p
+ 8);
1441 ipv4
= ntohl(*(uint32_t *)(p
+ 16));
1445 LOG(1, s
, t
, "IP packet too long %d\n", l
);
1446 STAT(tunnel_rx_errors
);
1450 if (session
[s
].ppp
.phase
!= Network
|| session
[s
].ppp
.ipv6cp
!= Opened
)
1454 if (ipv4
!= session
[s
].ip
&& memcmp(&config
->ipv6_prefix
, &ip
, 8) && sessionbyipv6(ip
) != s
)
1456 char str
[INET6_ADDRSTRLEN
];
1457 LOG(5, s
, t
, "Dropping packet with spoofed IP %s\n",
1458 inet_ntop(AF_INET6
, &ip
, str
, INET6_ADDRSTRLEN
));
1462 // Check if it's a Router Solicition message.
1463 if (*(p
+ 6) == 58 && *(p
+ 7) == 255 && *(p
+ 24) == 0xFF && *(p
+ 25) == 2 &&
1464 *(uint32_t *)(p
+ 26) == 0 && *(uint32_t *)(p
+ 30) == 0 &&
1465 *(uint32_t *)(p
+ 34) == 0 &&
1466 *(p
+ 38) == 0 && *(p
+ 39) == 2 && *(p
+ 40) == 133) {
1467 LOG(3, s
, t
, "Got IPv6 RS\n");
1468 send_ipv6_ra(s
, t
, &ip
);
1472 // Add on the tun header
1474 *(uint32_t *) p
= htonl(PKTIPV6
);
1477 // Are we throttled and a slave?
1478 if (session
[s
].tbf_in
&& !config
->cluster_iam_master
) {
1479 // Pass it to the master for handling.
1480 master_throttle_packet(session
[s
].tbf_in
, p
, l
);
1484 // Are we throttled and a master??
1485 if (session
[s
].tbf_in
&& config
->cluster_iam_master
) {
1486 // Actually handle the throttled packets.
1487 tbf_queue_packet(session
[s
].tbf_in
, p
, l
);
1492 if (tun_write(p
, l
) < 0)
1494 STAT(tun_tx_errors
);
1495 LOG(0, s
, t
, "Error writing %d bytes to TUN device: %s (tunfd=%d, p=%p)\n",
1496 l
, strerror(errno
), tunfd
, p
);
1504 if (session
[s
].snoop_ip
&& session
[s
].snoop_port
)
1506 // Snooping this session
1507 snoop_send_packet(p
, l
, session
[s
].snoop_ip
, session
[s
].snoop_port
);
1510 increment_counter(&session
[s
].cin
, &session
[s
].cin_wrap
, l
);
1511 session
[s
].cin_delta
+= l
;
1514 sess_local
[s
].cin
+= l
;
1515 sess_local
[s
].pin
++;
1519 STAT(tun_tx_packets
);
1520 INC_STAT(tun_tx_bytes
, l
);
1524 // Helper routine for the TBF filters.
1525 // Used to send queued data in from the user.
1527 void send_ipin(sessionidt s
, uint8_t *buf
, int len
)
1529 LOG_HEX(5, "IP in throttled", buf
, len
);
1531 if (write(tunfd
, buf
, len
) < 0)
1533 STAT(tun_tx_errors
);
1534 LOG(0, 0, 0, "Error writing %d bytes to TUN device: %s (tunfd=%d, p=%p)\n",
1535 len
, strerror(errno
), tunfd
, buf
);
1543 if (session
[s
].snoop_ip
&& session
[s
].snoop_port
)
1545 // Snooping this session
1546 snoop_send_packet(buf
, len
, session
[s
].snoop_ip
, session
[s
].snoop_port
);
1549 // Increment packet counters
1550 increment_counter(&session
[s
].cin
, &session
[s
].cin_wrap
, len
);
1551 session
[s
].cin_delta
+= len
;
1554 sess_local
[s
].cin
+= len
;
1555 sess_local
[s
].pin
++;
1559 STAT(tun_tx_packets
);
1560 INC_STAT(tun_tx_bytes
, len
- 4);
1564 // Process CCP messages
1565 void processccp(sessionidt s
, tunnelidt t
, uint8_t *p
, uint16_t l
)
1567 uint8_t b
[MAXETHER
];
1572 LOG_HEX(5, "CCP", p
, l
);
1574 if (session
[s
].ppp
.phase
< Network
)
1576 LOG(2, s
, t
, "CCP %s ignored in %s phase\n", ppp_code(*p
), ppp_phase(session
[s
].ppp
.phase
));
1582 LOG(1, s
, t
, "Short CCP packet\n");
1583 STAT(tunnel_rx_errors
);
1586 LOG(3, s
, t
, "CCP: recv %s\n", ppp_code(*p
));
1587 if (*p
== ConfigAck
)
1589 switch (session
[s
].ppp
.ccp
)
1592 initialise_restart_count(s
, ccp
);
1593 change_state(s
, ccp
, AckReceived
);
1598 LOG(2, s
, t
, "CCP: ConfigAck in state %s? Sending ConfigReq\n", ppp_state(session
[s
].ppp
.ccp
));
1600 change_state(s
, ccp
, RequestSent
);
1604 LOG(3, s
, t
, "CCP: Opened\n");
1605 change_state(s
, ccp
, Opened
);
1609 LOG(2, s
, t
, "CCP: ignoring %s in state %s\n", ppp_code(*p
), ppp_state(session
[s
].ppp
.ccp
));
1612 else if (*p
== ConfigReq
)
1614 if (l
< 6) // accept no compression
1616 else // compression requested--reject
1619 q
= makeppp(b
, sizeof(b
), p
, l
, s
, t
, PPPCCP
);
1622 switch (session
[s
].ppp
.ccp
)
1625 q
= makeppp(b
, sizeof(b
), p
, 2, s
, t
, PPPCCP
);
1628 *((uint16_t *) (q
+ 2)) = htons(l
= 4);
1632 initialise_restart_count(s
, ccp
);
1634 if (*q
== ConfigAck
)
1635 change_state(s
, ccp
, AckSent
);
1637 change_state(s
, ccp
, RequestSent
);
1642 if (*q
== ConfigAck
)
1643 change_state(s
, ccp
, AckSent
);
1648 if (*q
== ConfigAck
)
1649 change_state(s
, ccp
, Opened
);
1654 initialise_restart_count(s
, ccp
);
1659 if (*q
== ConfigAck
)
1660 change_state(s
, ccp
, AckSent
);
1662 change_state(s
, ccp
, RequestSent
);
1667 LOG(2, s
, t
, "CCP: ignoring %s in state %s\n", ppp_code(*p
), ppp_state(session
[s
].ppp
.ccp
));
1671 LOG(3, s
, t
, "CCP: send %s\n", ppp_code(*q
));
1672 tunnelsend(b
, l
+ (q
- b
), t
);
1674 else if (*p
== TerminateReq
)
1677 q
= makeppp(b
, sizeof(b
), p
, l
, s
, t
, PPPCCP
);
1679 LOG(3, s
, t
, "CCP: send %s\n", ppp_code(*q
));
1680 tunnelsend(b
, l
+ (q
- b
), t
);
1681 change_state(s
, ccp
, Stopped
);
1683 else if (*p
!= CodeRej
)
1685 ppp_code_rej(s
, t
, PPPCCP
, "CCP", p
, l
, b
, sizeof(b
));
1689 // send a CHAP challenge
1690 void sendchap(sessionidt s
, tunnelidt t
)
1692 uint8_t b
[MAXETHER
];
1701 LOG(1, s
, t
, "No RADIUS to send challenge\n");
1702 STAT(tunnel_tx_errors
);
1706 LOG(1, s
, t
, "Send CHAP challenge\n");
1708 radius
[r
].chap
= 1; // CHAP not PAP
1710 if (radius
[r
].state
!= RADIUSCHAP
)
1713 radius
[r
].state
= RADIUSCHAP
;
1714 radius
[r
].retry
= backoff(radius
[r
].try++);
1715 if (radius
[r
].try > 5)
1717 sessionshutdown(s
, "CHAP timeout.", 3, 0);
1718 STAT(tunnel_tx_errors
);
1721 q
= makeppp(b
, sizeof(b
), 0, 0, s
, t
, PPPCHAP
);
1724 *q
= 1; // challenge
1725 q
[1] = radius
[r
].id
; // ID
1726 q
[4] = 16; // value size (size of challenge)
1727 memcpy(q
+ 5, radius
[r
].auth
, 16); // challenge
1728 strcpy((char *) q
+ 21, hostname
); // our name
1729 *(uint16_t *) (q
+ 2) = htons(strlen(hostname
) + 21); // length
1730 tunnelsend(b
, strlen(hostname
) + 21 + (q
- b
), t
); // send it
1733 // fill in a L2TP message with a PPP frame,
1734 // copies existing PPP message and changes magic number if seen
1735 // returns start of PPP frame
1736 uint8_t *makeppp(uint8_t *b
, int size
, uint8_t *p
, int l
, sessionidt s
, tunnelidt t
, uint16_t mtype
)
1738 if (size
< 12) // Need more space than this!!
1740 static int backtrace_count
= 0;
1741 LOG(0, s
, t
, "makeppp buffer too small for L2TP header (size=%d)\n", size
);
1742 log_backtrace(backtrace_count
, 5)
1746 *(uint16_t *) (b
+ 0) = htons(0x0002); // L2TP with no options
1747 *(uint16_t *) (b
+ 2) = htons(tunnel
[t
].far
); // tunnel
1748 *(uint16_t *) (b
+ 4) = htons(session
[s
].far
); // session
1750 if (mtype
== PPPLCP
|| !(session
[s
].l2tp_flags
& SESSIONACFC
))
1752 *(uint16_t *) b
= htons(0xFF03); // HDLC header
1755 if (mtype
< 0x100 && session
[s
].l2tp_flags
& SESSIONPFC
)
1759 *(uint16_t *) b
= htons(mtype
);
1765 static int backtrace_count
= 0;
1766 LOG(2, s
, t
, "makeppp would overflow buffer (size=%d, header+payload=%d)\n", size
, l
+ 12);
1767 log_backtrace(backtrace_count
, 5)
1777 static int add_lcp_auth(uint8_t *b
, int size
, int authtype
)
1780 if ((authtype
== AUTHCHAP
&& size
< 5) || size
< 4)
1783 *b
++ = 3; // Authentication-Protocol
1784 if (authtype
== AUTHCHAP
)
1786 len
= *b
++ = 5; // length
1787 *(uint16_t *) b
= htons(PPPCHAP
); b
+= 2;
1790 else if (authtype
== AUTHPAP
)
1792 len
= *b
++ = 4; // length
1793 *(uint16_t *) b
= htons(PPPPAP
); b
+= 2;
1797 LOG(0, 0, 0, "add_lcp_auth called with unsupported auth type %d\n", authtype
);
1803 // Send initial LCP ConfigReq for MRU, authentication type and magic no
1804 void sendlcp(sessionidt s
, tunnelidt t
)
1806 uint8_t b
[500], *q
, *l
;
1807 int authtype
= sess_local
[s
].lcp_authtype
;
1809 if (!(q
= makeppp(b
, sizeof(b
), NULL
, 0, s
, t
, PPPLCP
)))
1812 LOG(3, s
, t
, "LCP: send ConfigReq%s%s%s\n",
1813 authtype
? " (" : "",
1814 authtype
? (authtype
== AUTHCHAP
? "CHAP" : "PAP") : "",
1815 authtype
? ")" : "");
1819 *l
++ = ++sess_local
[s
].lcp_ident
; // ID
1821 l
+= 2; //Save space for length
1823 if (sess_local
[s
].ppp_mru
)
1825 *l
++ = 1; *l
++ = 4; // Maximum-Receive-Unit (length 4)
1826 *(uint16_t *) l
= htons(sess_local
[s
].ppp_mru
); l
+= 2;
1830 l
+= add_lcp_auth(l
, sizeof(b
) - (l
- b
), authtype
);
1832 *l
++ = 5; *l
++ = 6; // Magic-Number (length 6)
1833 *(uint32_t *) l
= htonl(session
[s
].magic
);
1836 *(uint16_t *)(q
+ 2) = htons(l
- q
); // Length
1838 LOG_HEX(5, "PPPLCP", q
, l
- q
);
1839 if (config
->debug
> 3) dumplcp(q
, l
- q
);
1841 tunnelsend(b
, (l
- b
), t
);
1844 // Send CCP request for no compression
1845 void sendccp(sessionidt s
, tunnelidt t
)
1849 if (!(q
= makeppp(b
, sizeof(b
), NULL
, 0, s
, t
, PPPCCP
)))
1852 LOG(3, s
, t
, "CCP: send ConfigReq (no compression)\n");
1855 *(q
+ 1) = ++sess_local
[s
].lcp_ident
; // ID
1856 *(uint16_t *)(q
+ 2) = htons(4); // Length
1858 LOG_HEX(5, "PPPCCP", q
, 4);
1859 tunnelsend(b
, (q
- b
) + 4 , t
);