d2411a6e2b1c615c16ffd17bedb72ca74488695b
3 char const *cvs_id_ppp
= "$Id: ppp.c,v 1.85 2005/11/04 14:41:50 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
|| session
[s
].flags
& SESSION_STARTED
))
908 uint16_t r
= radiusnew(s
);
911 radiussend(r
, RADIUSSTART
); // send radius start
913 // don't send further Start records if IPCP is restarted
914 session
[s
].flags
|= SESSION_STARTED
;
915 cluster_send_session(s
);
919 // start IPv6 if configured and still in passive state
920 if (session
[s
].ppp
.ipv6cp
== Stopped
)
923 change_state(s
, ipv6cp
, RequestSent
);
927 // Process IPCP messages
928 void processipcp(sessionidt s
, tunnelidt t
, uint8_t *p
, uint16_t l
)
936 LOG_HEX(5, "IPCP", p
, l
);
939 LOG(1, s
, t
, "Short IPCP %d bytes\n", l
);
940 STAT(tunnel_rx_errors
);
944 if ((hl
= ntohs(*(uint16_t *) (p
+ 2))) > l
)
946 LOG(1, s
, t
, "Length mismatch IPCP %u/%u\n", hl
, l
);
947 STAT(tunnel_rx_errors
);
952 if (session
[s
].ppp
.phase
< Network
)
954 LOG(2, s
, t
, "IPCP %s ignored in %s phase\n", ppp_code(*p
), ppp_phase(session
[s
].ppp
.phase
));
958 LOG(3, s
, t
, "IPCP: recv %s\n", ppp_code(*p
));
962 switch (session
[s
].ppp
.ipcp
)
965 initialise_restart_count(s
, ipcp
);
966 change_state(s
, ipcp
, AckReceived
);
971 LOG(2, s
, t
, "IPCP: ConfigAck in state %s? Sending ConfigReq\n", ppp_state(session
[s
].ppp
.ipcp
));
973 change_state(s
, ipcp
, RequestSent
);
981 LOG(2, s
, t
, "IPCP: ignoring %s in state %s\n", ppp_code(*p
), ppp_state(session
[s
].ppp
.ipcp
));
984 else if (*p
== ConfigReq
)
986 uint8_t *response
= 0;
996 case 3: // ip address
997 gotip
++; // seen address
998 if (o
[1] != 6 || o
[1] > length
) return;
1000 addr
= htonl(session
[s
].ip
);
1001 if (memcmp(o
+ 2, &addr
, (sizeof addr
)))
1004 q
= ppp_conf_nak(s
, b
, sizeof(b
), PPPIPCP
, &response
, q
, p
, o
, (uint8_t *) &addr
, sizeof(addr
));
1005 if (!q
|| (q
!= oq
&& *response
== ConfigRej
))
1007 sessionshutdown(s
, "Can't negotiate IPCP.", 3, 0);
1014 case 129: // primary DNS
1015 if (o
[1] != 6 || o
[1] > length
) return;
1017 addr
= htonl(session
[s
].dns1
);
1018 if (memcmp(o
+ 2, &addr
, (sizeof addr
)))
1020 q
= ppp_conf_nak(s
, b
, sizeof(b
), PPPIPCP
, &response
, q
, p
, o
, (uint8_t *) &addr
, sizeof(addr
));
1026 case 131: // secondary DNS
1027 if (o
[1] != 6 || o
[1] > length
) return;
1029 addr
= htonl(session
[s
].dns1
);
1030 if (memcmp(o
+ 2, &addr
, sizeof(addr
)))
1032 q
= ppp_conf_nak(s
, b
, sizeof(b
), PPPIPCP
, &response
, q
, p
, o
, (uint8_t *) &addr
, sizeof(addr
));
1039 LOG(2, s
, t
, " Rejecting PPP IPCP Option type %d\n", *o
);
1040 q
= ppp_conf_rej(s
, b
, sizeof(b
), PPPIPCP
, &response
, q
, p
, o
);
1050 l
= q
- response
; // IPCP packet length
1051 *((uint16_t *) (response
+ 2)) = htons(l
); // update header
1055 // Send packet back as ConfigAck
1056 response
= makeppp(b
, sizeof(b
), p
, l
, s
, t
, PPPIPCP
);
1057 if (!response
) return;
1058 *response
= ConfigAck
;
1062 LOG(1, s
, t
, "No IP in IPCP request\n");
1063 STAT(tunnel_rx_errors
);
1067 switch (session
[s
].ppp
.ipcp
)
1070 response
= makeppp(b
, sizeof(b
), p
, 2, s
, t
, PPPIPCP
);
1071 if (!response
) return;
1072 *response
= TerminateAck
;
1073 *((uint16_t *) (response
+ 2)) = htons(l
= 4);
1077 initialise_restart_count(s
, ipcp
);
1079 if (*response
== ConfigAck
)
1080 change_state(s
, ipcp
, AckSent
);
1082 change_state(s
, ipcp
, RequestSent
);
1087 if (*response
== ConfigAck
)
1088 change_state(s
, ipcp
, AckSent
);
1093 if (*response
== ConfigAck
)
1099 initialise_restart_count(s
, ipcp
);
1104 if (*response
== ConfigAck
)
1105 change_state(s
, ipcp
, AckSent
);
1107 change_state(s
, ipcp
, RequestSent
);
1112 LOG(2, s
, t
, "IPCP: ignoring %s in state %s\n", ppp_code(*p
), ppp_state(session
[s
].ppp
.ipcp
));
1116 LOG(3, s
, t
, "IPCP: send %s\n", ppp_code(*response
));
1117 tunnelsend(b
, l
+ (response
- b
), t
);
1119 else if (*p
== TerminateReq
)
1122 q
= makeppp(b
, sizeof(b
), p
, l
, s
, t
, PPPIPCP
);
1124 LOG(3, s
, t
, "IPCP: send %s\n", ppp_code(*q
));
1125 tunnelsend(b
, l
+ (q
- b
), t
);
1126 change_state(s
, ipcp
, Stopped
);
1128 else if (*p
!= CodeRej
)
1130 ppp_code_rej(s
, t
, PPPIPCP
, "IPCP", p
, l
, b
, sizeof(b
));
1134 static void ipv6cp_open(sessionidt s
, tunnelidt t
)
1136 LOG(3, s
, t
, "IPV6CP: Opened\n");
1138 change_state(s
, ipv6cp
, Opened
);
1139 if (session
[s
].ipv6prefixlen
)
1140 route6set(s
, session
[s
].ipv6route
, session
[s
].ipv6prefixlen
, 1);
1142 // Send an initial RA (TODO: Should we send these regularly?)
1143 send_ipv6_ra(s
, t
, NULL
);
1146 // Process IPV6CP messages
1147 void processipv6cp(sessionidt s
, tunnelidt t
, uint8_t *p
, uint16_t l
)
1149 uint8_t b
[MAXETHER
];
1153 CSTAT(processipv6cp
);
1155 LOG_HEX(5, "IPV6CP", p
, l
);
1158 LOG(1, s
, t
, "Short IPV6CP %d bytes\n", l
);
1159 STAT(tunnel_rx_errors
);
1163 if ((hl
= ntohs(*(uint16_t *) (p
+ 2))) > l
)
1165 LOG(1, s
, t
, "Length mismatch IPV6CP %u/%u\n", hl
, l
);
1166 STAT(tunnel_rx_errors
);
1171 if (session
[s
].ppp
.phase
< Network
)
1173 LOG(2, s
, t
, "IPV6CP %s ignored in %s phase\n", ppp_code(*p
), ppp_phase(session
[s
].ppp
.phase
));
1177 LOG(3, s
, t
, "IPV6CP: recv %s\n", ppp_code(*p
));
1181 LOG(3, s
, t
, "IPV6CP: no IPv4 address (IPCP in state %s)\n", ppp_state(session
[s
].ppp
.ipcp
));
1182 return; // need IPCP to complete...
1185 if (*p
== ConfigAck
)
1187 switch (session
[s
].ppp
.ipv6cp
)
1190 initialise_restart_count(s
, ipv6cp
);
1191 change_state(s
, ipv6cp
, AckReceived
);
1196 LOG(2, s
, t
, "IPV6CP: ConfigAck in state %s? Sending ConfigReq\n", ppp_state(session
[s
].ppp
.ipv6cp
));
1198 change_state(s
, ipv6cp
, RequestSent
);
1206 LOG(2, s
, t
, "IPV6CP: ignoring %s in state %s\n", ppp_code(*p
), ppp_state(session
[s
].ppp
.ipv6cp
));
1209 else if (*p
== ConfigReq
)
1211 uint8_t *response
= 0;
1221 case 1: // interface identifier
1222 gotip
++; // seen address
1223 if (o
[1] != 10 || o
[1] > length
) return;
1225 *(uint32_t *) ident
= htonl(session
[s
].ip
);
1226 *(uint32_t *) (ident
+ 4) = 0;
1228 if (memcmp(o
+ 2, ident
, sizeof(ident
)))
1230 q
= ppp_conf_nak(s
, b
, sizeof(b
), PPPIPV6CP
, &response
, q
, p
, o
, ident
, sizeof(ident
));
1237 LOG(2, s
, t
, " Rejecting PPP IPV6CP Option type %d\n", *o
);
1238 q
= ppp_conf_rej(s
, b
, sizeof(b
), PPPIPV6CP
, &response
, q
, p
, o
);
1248 l
= q
- response
; // IPV6CP packet length
1249 *((uint16_t *) (response
+ 2)) = htons(l
); // update header
1253 // Send packet back as ConfigAck
1254 response
= makeppp(b
, sizeof(b
), p
, l
, s
, t
, PPPIPV6CP
);
1255 if (!response
) return;
1256 *response
= ConfigAck
;
1260 LOG(1, s
, t
, "No interface identifier in IPV6CP request\n");
1261 STAT(tunnel_rx_errors
);
1265 switch (session
[s
].ppp
.ipv6cp
)
1268 response
= makeppp(b
, sizeof(b
), p
, 2, s
, t
, PPPIPV6CP
);
1269 if (!response
) return;
1270 *response
= TerminateAck
;
1271 *((uint16_t *) (response
+ 2)) = htons(l
= 4);
1275 initialise_restart_count(s
, ipv6cp
);
1277 if (*response
== ConfigAck
)
1278 change_state(s
, ipv6cp
, AckSent
);
1280 change_state(s
, ipv6cp
, RequestSent
);
1285 if (*response
== ConfigAck
)
1286 change_state(s
, ipv6cp
, AckSent
);
1291 if (*response
== ConfigAck
)
1297 initialise_restart_count(s
, ipv6cp
);
1302 if (*response
== ConfigAck
)
1303 change_state(s
, ipv6cp
, AckSent
);
1305 change_state(s
, ipv6cp
, RequestSent
);
1310 LOG(2, s
, t
, "IPV6CP: ignoring %s in state %s\n", ppp_code(*p
), ppp_state(session
[s
].ppp
.ipv6cp
));
1314 LOG(3, s
, t
, "IPV6CP: send %s\n", ppp_code(*response
));
1315 tunnelsend(b
, l
+ (response
- b
), t
);
1317 else if (*p
== TerminateReq
)
1320 q
= makeppp(b
, sizeof(b
), p
, l
, s
, t
, PPPIPV6CP
);
1322 LOG(3, s
, t
, "IPV6CP: send %s\n", ppp_code(*q
));
1323 tunnelsend(b
, l
+ (q
- b
), t
);
1324 change_state(s
, ipv6cp
, Stopped
);
1326 else if (*p
!= CodeRej
)
1328 ppp_code_rej(s
, t
, PPPIPV6CP
, "IPV6CP", p
, l
, b
, sizeof(b
));
1332 // process IP packet received
1334 // This MUST be called with at least 4 byte behind 'p'.
1335 // (i.e. this routine writes to p[-4]).
1336 void processipin(sessionidt s
, tunnelidt t
, uint8_t *p
, uint16_t l
)
1342 LOG_HEX(5, "IP", p
, l
);
1346 LOG(1, s
, t
, "IP packet too short %d\n", l
);
1347 STAT(tunnel_rx_errors
);
1351 ip
= ntohl(*(uint32_t *)(p
+ 12));
1355 LOG(1, s
, t
, "IP packet too long %d\n", l
);
1356 STAT(tunnel_rx_errors
);
1360 if (session
[s
].ppp
.phase
!= Network
|| session
[s
].ppp
.ipcp
!= Opened
)
1363 // no spoof (do sessionbyip to handled statically routed subnets)
1364 if (ip
!= session
[s
].ip
&& sessionbyip(htonl(ip
)) != s
)
1366 LOG(5, s
, t
, "Dropping packet with spoofed IP %s\n", fmtaddr(htonl(ip
), 0));
1370 // run access-list if any
1371 if (session
[s
].filter_in
&& !ip_filter(p
, l
, session
[s
].filter_in
- 1))
1374 // adjust MSS on SYN and SYN,ACK packets with options
1375 if ((ntohs(*(uint16_t *) (p
+ 6)) & 0x1fff) == 0 && p
[9] == IPPROTO_TCP
) // first tcp fragment
1377 int ihl
= (p
[0] & 0xf) * 4; // length of IP header
1378 if (l
>= ihl
+ 20 && (p
[ihl
+ 13] & TCP_FLAG_SYN
) && ((p
[ihl
+ 12] >> 4) > 5))
1379 adjust_tcp_mss(s
, t
, p
, l
, p
+ ihl
);
1382 // Add on the tun header
1384 *(uint32_t *) p
= htonl(PKTIP
);
1387 // Are we throttled and a slave?
1388 if (session
[s
].tbf_in
&& !config
->cluster_iam_master
) {
1389 // Pass it to the master for handling.
1390 master_throttle_packet(session
[s
].tbf_in
, p
, l
);
1394 // Are we throttled and a master??
1395 if (session
[s
].tbf_in
&& config
->cluster_iam_master
) {
1396 // Actually handle the throttled packets.
1397 tbf_queue_packet(session
[s
].tbf_in
, p
, l
);
1402 if (tun_write(p
, l
) < 0)
1404 STAT(tun_tx_errors
);
1405 LOG(0, s
, t
, "Error writing %d bytes to TUN device: %s (tunfd=%d, p=%p)\n",
1406 l
, strerror(errno
), tunfd
, p
);
1414 if (session
[s
].snoop_ip
&& session
[s
].snoop_port
)
1416 // Snooping this session
1417 snoop_send_packet(p
, l
, session
[s
].snoop_ip
, session
[s
].snoop_port
);
1420 increment_counter(&session
[s
].cin
, &session
[s
].cin_wrap
, l
);
1421 session
[s
].cin_delta
+= l
;
1424 sess_local
[s
].cin
+= l
;
1425 sess_local
[s
].pin
++;
1429 STAT(tun_tx_packets
);
1430 INC_STAT(tun_tx_bytes
, l
);
1433 // process IPv6 packet received
1435 // This MUST be called with at least 4 byte behind 'p'.
1436 // (i.e. this routine writes to p[-4]).
1437 void processipv6in(sessionidt s
, tunnelidt t
, uint8_t *p
, uint16_t l
)
1442 CSTAT(processipv6in
);
1444 LOG_HEX(5, "IPv6", p
, l
);
1446 ip
= *(struct in6_addr
*) (p
+ 8);
1447 ipv4
= ntohl(*(uint32_t *)(p
+ 16));
1451 LOG(1, s
, t
, "IP packet too long %d\n", l
);
1452 STAT(tunnel_rx_errors
);
1456 if (session
[s
].ppp
.phase
!= Network
|| session
[s
].ppp
.ipv6cp
!= Opened
)
1460 if (ipv4
!= session
[s
].ip
&& memcmp(&config
->ipv6_prefix
, &ip
, 8) && sessionbyipv6(ip
) != s
)
1462 char str
[INET6_ADDRSTRLEN
];
1463 LOG(5, s
, t
, "Dropping packet with spoofed IP %s\n",
1464 inet_ntop(AF_INET6
, &ip
, str
, INET6_ADDRSTRLEN
));
1468 // Check if it's a Router Solicition message.
1469 if (*(p
+ 6) == 58 && *(p
+ 7) == 255 && *(p
+ 24) == 0xFF && *(p
+ 25) == 2 &&
1470 *(uint32_t *)(p
+ 26) == 0 && *(uint32_t *)(p
+ 30) == 0 &&
1471 *(uint32_t *)(p
+ 34) == 0 &&
1472 *(p
+ 38) == 0 && *(p
+ 39) == 2 && *(p
+ 40) == 133) {
1473 LOG(3, s
, t
, "Got IPv6 RS\n");
1474 send_ipv6_ra(s
, t
, &ip
);
1478 // Add on the tun header
1480 *(uint32_t *) p
= htonl(PKTIPV6
);
1483 // Are we throttled and a slave?
1484 if (session
[s
].tbf_in
&& !config
->cluster_iam_master
) {
1485 // Pass it to the master for handling.
1486 master_throttle_packet(session
[s
].tbf_in
, p
, l
);
1490 // Are we throttled and a master??
1491 if (session
[s
].tbf_in
&& config
->cluster_iam_master
) {
1492 // Actually handle the throttled packets.
1493 tbf_queue_packet(session
[s
].tbf_in
, p
, l
);
1498 if (tun_write(p
, l
) < 0)
1500 STAT(tun_tx_errors
);
1501 LOG(0, s
, t
, "Error writing %d bytes to TUN device: %s (tunfd=%d, p=%p)\n",
1502 l
, strerror(errno
), tunfd
, p
);
1510 if (session
[s
].snoop_ip
&& session
[s
].snoop_port
)
1512 // Snooping this session
1513 snoop_send_packet(p
, l
, session
[s
].snoop_ip
, session
[s
].snoop_port
);
1516 increment_counter(&session
[s
].cin
, &session
[s
].cin_wrap
, l
);
1517 session
[s
].cin_delta
+= l
;
1520 sess_local
[s
].cin
+= l
;
1521 sess_local
[s
].pin
++;
1525 STAT(tun_tx_packets
);
1526 INC_STAT(tun_tx_bytes
, l
);
1530 // Helper routine for the TBF filters.
1531 // Used to send queued data in from the user.
1533 void send_ipin(sessionidt s
, uint8_t *buf
, int len
)
1535 LOG_HEX(5, "IP in throttled", buf
, len
);
1537 if (write(tunfd
, buf
, len
) < 0)
1539 STAT(tun_tx_errors
);
1540 LOG(0, 0, 0, "Error writing %d bytes to TUN device: %s (tunfd=%d, p=%p)\n",
1541 len
, strerror(errno
), tunfd
, buf
);
1549 if (session
[s
].snoop_ip
&& session
[s
].snoop_port
)
1551 // Snooping this session
1552 snoop_send_packet(buf
, len
, session
[s
].snoop_ip
, session
[s
].snoop_port
);
1555 // Increment packet counters
1556 increment_counter(&session
[s
].cin
, &session
[s
].cin_wrap
, len
);
1557 session
[s
].cin_delta
+= len
;
1560 sess_local
[s
].cin
+= len
;
1561 sess_local
[s
].pin
++;
1565 STAT(tun_tx_packets
);
1566 INC_STAT(tun_tx_bytes
, len
- 4);
1570 // Process CCP messages
1571 void processccp(sessionidt s
, tunnelidt t
, uint8_t *p
, uint16_t l
)
1573 uint8_t b
[MAXETHER
];
1578 LOG_HEX(5, "CCP", p
, l
);
1580 if (session
[s
].ppp
.phase
< Network
)
1582 LOG(2, s
, t
, "CCP %s ignored in %s phase\n", ppp_code(*p
), ppp_phase(session
[s
].ppp
.phase
));
1588 LOG(1, s
, t
, "Short CCP packet\n");
1589 STAT(tunnel_rx_errors
);
1592 LOG(3, s
, t
, "CCP: recv %s\n", ppp_code(*p
));
1593 if (*p
== ConfigAck
)
1595 switch (session
[s
].ppp
.ccp
)
1598 initialise_restart_count(s
, ccp
);
1599 change_state(s
, ccp
, AckReceived
);
1604 LOG(2, s
, t
, "CCP: ConfigAck in state %s? Sending ConfigReq\n", ppp_state(session
[s
].ppp
.ccp
));
1606 change_state(s
, ccp
, RequestSent
);
1610 LOG(3, s
, t
, "CCP: Opened\n");
1611 change_state(s
, ccp
, Opened
);
1615 LOG(2, s
, t
, "CCP: ignoring %s in state %s\n", ppp_code(*p
), ppp_state(session
[s
].ppp
.ccp
));
1618 else if (*p
== ConfigReq
)
1620 if (l
< 6) // accept no compression
1622 else // compression requested--reject
1625 q
= makeppp(b
, sizeof(b
), p
, l
, s
, t
, PPPCCP
);
1628 switch (session
[s
].ppp
.ccp
)
1631 q
= makeppp(b
, sizeof(b
), p
, 2, s
, t
, PPPCCP
);
1634 *((uint16_t *) (q
+ 2)) = htons(l
= 4);
1638 initialise_restart_count(s
, ccp
);
1640 if (*q
== ConfigAck
)
1641 change_state(s
, ccp
, AckSent
);
1643 change_state(s
, ccp
, RequestSent
);
1648 if (*q
== ConfigAck
)
1649 change_state(s
, ccp
, AckSent
);
1654 if (*q
== ConfigAck
)
1655 change_state(s
, ccp
, Opened
);
1660 initialise_restart_count(s
, ccp
);
1665 if (*q
== ConfigAck
)
1666 change_state(s
, ccp
, AckSent
);
1668 change_state(s
, ccp
, RequestSent
);
1673 LOG(2, s
, t
, "CCP: ignoring %s in state %s\n", ppp_code(*p
), ppp_state(session
[s
].ppp
.ccp
));
1677 LOG(3, s
, t
, "CCP: send %s\n", ppp_code(*q
));
1678 tunnelsend(b
, l
+ (q
- b
), t
);
1680 else if (*p
== TerminateReq
)
1683 q
= makeppp(b
, sizeof(b
), p
, l
, s
, t
, PPPCCP
);
1685 LOG(3, s
, t
, "CCP: send %s\n", ppp_code(*q
));
1686 tunnelsend(b
, l
+ (q
- b
), t
);
1687 change_state(s
, ccp
, Stopped
);
1689 else if (*p
!= CodeRej
)
1691 ppp_code_rej(s
, t
, PPPCCP
, "CCP", p
, l
, b
, sizeof(b
));
1695 // send a CHAP challenge
1696 void sendchap(sessionidt s
, tunnelidt t
)
1698 uint8_t b
[MAXETHER
];
1707 LOG(1, s
, t
, "No RADIUS to send challenge\n");
1708 STAT(tunnel_tx_errors
);
1712 LOG(1, s
, t
, "Send CHAP challenge\n");
1714 radius
[r
].chap
= 1; // CHAP not PAP
1716 if (radius
[r
].state
!= RADIUSCHAP
)
1719 radius
[r
].state
= RADIUSCHAP
;
1720 radius
[r
].retry
= backoff(radius
[r
].try++);
1721 if (radius
[r
].try > 5)
1723 sessionshutdown(s
, "CHAP timeout.", 3, 0);
1724 STAT(tunnel_tx_errors
);
1727 q
= makeppp(b
, sizeof(b
), 0, 0, s
, t
, PPPCHAP
);
1730 *q
= 1; // challenge
1731 q
[1] = radius
[r
].id
; // ID
1732 q
[4] = 16; // value size (size of challenge)
1733 memcpy(q
+ 5, radius
[r
].auth
, 16); // challenge
1734 strcpy((char *) q
+ 21, hostname
); // our name
1735 *(uint16_t *) (q
+ 2) = htons(strlen(hostname
) + 21); // length
1736 tunnelsend(b
, strlen(hostname
) + 21 + (q
- b
), t
); // send it
1739 // fill in a L2TP message with a PPP frame,
1740 // copies existing PPP message and changes magic number if seen
1741 // returns start of PPP frame
1742 uint8_t *makeppp(uint8_t *b
, int size
, uint8_t *p
, int l
, sessionidt s
, tunnelidt t
, uint16_t mtype
)
1744 if (size
< 12) // Need more space than this!!
1746 static int backtrace_count
= 0;
1747 LOG(0, s
, t
, "makeppp buffer too small for L2TP header (size=%d)\n", size
);
1748 log_backtrace(backtrace_count
, 5)
1752 *(uint16_t *) (b
+ 0) = htons(0x0002); // L2TP with no options
1753 *(uint16_t *) (b
+ 2) = htons(tunnel
[t
].far
); // tunnel
1754 *(uint16_t *) (b
+ 4) = htons(session
[s
].far
); // session
1756 if (mtype
== PPPLCP
|| !(session
[s
].flags
& SESSION_ACFC
))
1758 *(uint16_t *) b
= htons(0xFF03); // HDLC header
1761 if (mtype
< 0x100 && session
[s
].flags
& SESSION_PFC
)
1765 *(uint16_t *) b
= htons(mtype
);
1771 static int backtrace_count
= 0;
1772 LOG(2, s
, t
, "makeppp would overflow buffer (size=%d, header+payload=%d)\n", size
, l
+ 12);
1773 log_backtrace(backtrace_count
, 5)
1783 static int add_lcp_auth(uint8_t *b
, int size
, int authtype
)
1786 if ((authtype
== AUTHCHAP
&& size
< 5) || size
< 4)
1789 *b
++ = 3; // Authentication-Protocol
1790 if (authtype
== AUTHCHAP
)
1792 len
= *b
++ = 5; // length
1793 *(uint16_t *) b
= htons(PPPCHAP
); b
+= 2;
1796 else if (authtype
== AUTHPAP
)
1798 len
= *b
++ = 4; // length
1799 *(uint16_t *) b
= htons(PPPPAP
); b
+= 2;
1803 LOG(0, 0, 0, "add_lcp_auth called with unsupported auth type %d\n", authtype
);
1809 // Send initial LCP ConfigReq for MRU, authentication type and magic no
1810 void sendlcp(sessionidt s
, tunnelidt t
)
1812 uint8_t b
[500], *q
, *l
;
1813 int authtype
= sess_local
[s
].lcp_authtype
;
1815 if (!(q
= makeppp(b
, sizeof(b
), NULL
, 0, s
, t
, PPPLCP
)))
1818 LOG(3, s
, t
, "LCP: send ConfigReq%s%s%s\n",
1819 authtype
? " (" : "",
1820 authtype
? (authtype
== AUTHCHAP
? "CHAP" : "PAP") : "",
1821 authtype
? ")" : "");
1825 *l
++ = ++sess_local
[s
].lcp_ident
; // ID
1827 l
+= 2; //Save space for length
1829 if (sess_local
[s
].ppp_mru
)
1831 *l
++ = 1; *l
++ = 4; // Maximum-Receive-Unit (length 4)
1832 *(uint16_t *) l
= htons(sess_local
[s
].ppp_mru
); l
+= 2;
1836 l
+= add_lcp_auth(l
, sizeof(b
) - (l
- b
), authtype
);
1838 *l
++ = 5; *l
++ = 6; // Magic-Number (length 6)
1839 *(uint32_t *) l
= htonl(session
[s
].magic
);
1842 *(uint16_t *)(q
+ 2) = htons(l
- q
); // Length
1844 LOG_HEX(5, "PPPLCP", q
, l
- q
);
1845 if (config
->debug
> 3) dumplcp(q
, l
- q
);
1847 tunnelsend(b
, (l
- b
), t
);
1848 restart_timer(s
, lcp
);
1851 // Send CCP request for no compression
1852 void sendccp(sessionidt s
, tunnelidt t
)
1856 if (!(q
= makeppp(b
, sizeof(b
), NULL
, 0, s
, t
, PPPCCP
)))
1859 LOG(3, s
, t
, "CCP: send ConfigReq (no compression)\n");
1862 *(q
+ 1) = ++sess_local
[s
].lcp_ident
; // ID
1863 *(uint16_t *)(q
+ 2) = htons(4); // Length
1865 LOG_HEX(5, "PPPCCP", q
, 4);
1866 tunnelsend(b
, (q
- b
) + 4 , t
);
1867 restart_timer(s
, ccp
);