3 char const *cvs_id_ppp
= "$Id: ppp.c,v 1.72 2005/08/12 08:18:46 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
91 uint8_t b
[MAXCONTROL
];
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
);
150 r
= sess_local
[s
].radius
;
153 LOG(1, s
, t
, "Unexpected CHAP message\n");
154 STAT(tunnel_rx_errors
);
160 LOG(1, s
, t
, "Short CHAP %u bytes\n", l
);
161 STAT(tunnel_rx_errors
);
162 sessionshutdown(s
, "Short CHAP packet.", 3, 0);
166 if ((hl
= ntohs(*(uint16_t *) (p
+ 2))) > l
)
168 LOG(1, s
, t
, "Length mismatch CHAP %u/%u\n", hl
, l
);
169 STAT(tunnel_rx_errors
);
170 sessionshutdown(s
, "CHAP length mismatch.", 3, 0);
177 LOG(1, s
, t
, "Unexpected CHAP response code %d\n", *p
);
178 STAT(tunnel_rx_errors
);
179 sessionshutdown(s
, "CHAP length mismatch.", 3, 0);
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));
303 int algo
= *(uint8_t *)(o
+ 4);
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_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_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_rej(s
, buf
, blen
, mtype
, response
, 0, packet
, option
);
423 if (*nak_sent
>= config
->ppp_max_failure
)
424 return ppp_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 // Process LCP messages
448 void processlcp(sessionidt s
, tunnelidt t
, uint8_t *p
, uint16_t l
)
450 uint8_t b
[MAXCONTROL
];
452 uint32_t magicno
= 0;
457 LOG_HEX(5, "LCP", p
, l
);
460 LOG(1, s
, t
, "Short LCP %d bytes\n", l
);
461 STAT(tunnel_rx_errors
);
465 if ((hl
= ntohs(*(uint16_t *) (p
+ 2))) > l
)
467 LOG(1, s
, t
, "Length mismatch LCP %u/%u\n", hl
, l
);
468 STAT(tunnel_rx_errors
);
473 if (session
[s
].die
) // going down...
476 LOG(*p
== EchoReq
? 4 : 3, s
, t
, "LCP: recv %s\n", ppp_code(*p
));
477 if (config
->debug
> 3) dumplcp(p
, l
);
482 uint8_t *o
= (p
+ 4);
490 if (length
== 0 || type
== 0 || x
< length
) break;
493 case 3: // Authentication-Protocol
495 int proto
= ntohs(*(uint16_t *)(o
+ 2));
498 else if (proto
== PPPCHAP
&& *(o
+ 4) == 5)
508 if (!session
[s
].ip
&& authtype
)
509 sess_local
[s
].lcp_authtype
= authtype
;
511 switch (session
[s
].ppp
.lcp
)
514 initialise_restart_count(s
, lcp
);
515 change_state(s
, lcp
, AckReceived
);
520 LOG(2, s
, t
, "LCP: ConfigAck in state %s? Sending ConfigReq\n", ppp_state(session
[s
].ppp
.lcp
));
521 if (session
[s
].ppp
.lcp
== Opened
)
524 sendlcp(s
, t
, sess_local
[s
].lcp_authtype
);
525 change_state(s
, lcp
, RequestSent
);
533 LOG(2, s
, t
, "LCP: ignoring %s in state %s\n", ppp_code(*p
), ppp_state(session
[s
].ppp
.lcp
));
536 else if (*p
== ConfigReq
)
539 uint8_t *o
= (p
+ 4);
540 uint8_t *response
= 0;
541 static uint8_t asyncmap
[4] = { 0, 0, 0, 0 }; // all zero
542 static uint8_t authproto
[5];
549 if (length
== 0 || type
== 0 || x
< length
) break;
552 case 1: // Maximum-Receive-Unit
553 session
[s
].mru
= ntohs(*(uint16_t *)(o
+ 2));
556 case 2: // Async-Control-Character-Map
557 if (!ntohl(*(uint32_t *)(o
+ 2))) // all bits zero is OK
560 LOG(3, s
, t
, " Remote requesting asyncmap. Rejecting.\n");
561 q
= ppp_nak(s
, b
, sizeof(b
), PPPLCP
, &response
, q
, p
, o
, asyncmap
, sizeof(asyncmap
));
564 case 3: // Authentication-Protocol
566 int proto
= ntohs(*(uint16_t *)(o
+ 2));
567 char proto_name
[] = "0x0000";
572 if (config
->radius_authtypes
& AUTHPAP
)
574 sess_local
[s
].lcp_authtype
= AUTHPAP
;
578 strcpy(proto_name
, "PAP");
580 else if (proto
== PPPCHAP
)
582 if (config
->radius_authtypes
& AUTHCHAP
583 && *(o
+ 4) == 5) // MD5
585 sess_local
[s
].lcp_authtype
= AUTHCHAP
;
589 strcpy(proto_name
, "CHAP");
592 sprintf(proto_name
, "%#4.4x", proto
);
594 LOG(3, s
, t
, " Remote requesting %s authentication. Rejecting.\n", proto_name
);
596 alen
= add_lcp_auth(authproto
, sizeof(authproto
), config
->radius_authprefer
);
597 if (alen
< 2) break; // paranoia
599 q
= ppp_nak(s
, b
, sizeof(b
), PPPLCP
, &response
, q
, p
, o
, authproto
+ 2, alen
- 2);
600 if (q
&& *response
== ConfigNak
&&
601 config
->radius_authtypes
!= config
->radius_authprefer
)
604 alen
= add_lcp_auth(authproto
, sizeof(authproto
), config
->radius_authtypes
& ~config
->radius_authprefer
);
606 q
= ppp_nak(s
, b
, sizeof(b
), PPPLCP
, &response
, q
, p
, o
, authproto
+ 2, alen
- 2);
613 case 5: // Magic-Number
614 magicno
= ntohl(*(uint32_t *)(o
+ 2));
617 case 4: // Quality-Protocol
618 case 7: // Protocol-Field-Compression
619 case 8: // Address-And-Control-Field-Compression
622 default: // Reject any unknown options
623 LOG(3, s
, t
, " Rejecting unknown PPP LCP option %d\n", type
);
624 q
= ppp_rej(s
, b
, sizeof(b
), PPPLCP
, &response
, q
, p
, o
);
632 l
= q
- response
; // LCP packet length
633 *((uint16_t *) (response
+ 2)) = htons(l
); // update header
637 // Send packet back as ConfigAck
638 response
= makeppp(b
, sizeof(b
), p
, l
, s
, t
, PPPLCP
);
639 if (!response
) return;
640 *response
= ConfigAck
;
643 switch (session
[s
].ppp
.lcp
)
646 response
= makeppp(b
, sizeof(b
), p
, 2, s
, t
, PPPLCP
);
647 if (!response
) return;
648 *response
= TerminateAck
;
649 *((uint16_t *) (response
+ 2)) = htons(l
= 4);
653 initialise_restart_count(s
, lcp
);
654 sendlcp(s
, t
, sess_local
[s
].lcp_authtype
);
655 if (*response
== ConfigAck
)
656 change_state(s
, lcp
, AckSent
);
658 change_state(s
, lcp
, RequestSent
);
663 if (*response
== ConfigAck
)
664 change_state(s
, lcp
, AckSent
);
669 if (*response
== ConfigAck
)
676 sendlcp(s
, t
, sess_local
[s
].lcp_authtype
);
680 if (*response
== ConfigAck
)
681 change_state(s
, lcp
, AckSent
);
683 change_state(s
, lcp
, RequestSent
);
688 LOG(2, s
, t
, "LCP: ignoring %s in state %s\n", ppp_code(*p
), ppp_state(session
[s
].ppp
.lcp
));
692 LOG(3, s
, t
, "LCP: send %s\n", ppp_code(*response
));
693 if (config
->debug
> 3) dumplcp(response
, l
);
695 tunnelsend(b
, l
+ (response
- b
), t
);
697 else if (*p
== ConfigNak
)
700 uint8_t *o
= (p
+ 4);
708 if (length
== 0 || type
== 0 || x
< length
) break;
711 case 1: // Maximum-Receive-Unit
712 session
[s
].mru
= ntohs(*(uint16_t *)(o
+ 2));
713 LOG(3, s
, t
, " Remote requested MRU of %u\n", session
[s
].mru
);
716 case 3: // Authentication-Protocol
721 int proto
= ntohs(*(uint16_t *)(o
+ 2));
724 authtype
= config
->radius_authtypes
& AUTHPAP
;
725 LOG(3, s
, t
, " Remote requested PAP authentication...%sing\n",
726 authtype
? "accept" : "reject");
728 else if (proto
== PPPCHAP
&& *(o
+ 4) == 5)
730 authtype
= config
->radius_authtypes
& AUTHCHAP
;
731 LOG(3, s
, t
, " Remote requested CHAP authentication...%sing\n",
732 authtype
? "accept" : "reject");
736 LOG(3, s
, t
, " Rejecting unsupported authentication %#4x\n",
744 LOG(2, s
, t
, " Remote NAKed LCP type %u?\n", type
);
753 sessionshutdown(s
, "Unsupported authentication.", 3, 0);
758 sess_local
[s
].lcp_authtype
= authtype
;
760 switch (session
[s
].ppp
.lcp
)
765 uint8_t *response
= makeppp(b
, sizeof(b
), p
, 2, s
, t
, PPPLCP
);
766 if (!response
) return;
767 *response
= TerminateAck
;
768 *((uint16_t *) (response
+ 2)) = htons(l
= 4);
770 LOG(3, s
, t
, "LCP: send %s\n", ppp_code(*response
));
771 if (config
->debug
> 3) dumplcp(response
, l
);
773 tunnelsend(b
, l
+ (response
- b
), t
);
779 initialise_restart_count(s
, lcp
);
780 sendlcp(s
, t
, sess_local
[s
].lcp_authtype
);
784 LOG(2, s
, t
, "LCP: ConfigNak in state %s? Sending ConfigReq\n", ppp_state(session
[s
].ppp
.lcp
));
785 sendlcp(s
, t
, sess_local
[s
].lcp_authtype
);
790 sendlcp(s
, t
, sess_local
[s
].lcp_authtype
);
794 LOG(2, s
, t
, "LCP: ignoring %s in state %s\n", ppp_code(*p
), ppp_state(session
[s
].ppp
.lcp
));
798 else if (*p
== TerminateReq
)
800 *p
= TerminateAck
; // close
801 q
= makeppp(b
, sizeof(b
), p
, l
, s
, t
, PPPLCP
);
804 LOG(3, s
, t
, "LCP: send %s\n", ppp_code(*q
));
805 if (config
->debug
> 3) dumplcp(q
, l
);
807 tunnelsend(b
, l
+ (q
- b
), t
); // send it
808 sessionshutdown(s
, "Remote end closed connection.", 3, 0);
810 else if (*p
== TerminateAck
)
812 sessionshutdown(s
, "Connection closed.", 3, 0);
814 else if (*p
== ProtocolRej
)
816 if (*(uint16_t *) (p
+4) == htons(PPPIPV6CP
))
818 LOG(3, s
, t
, "IPv6 rejected\n");
819 change_state(s
, ipv6cp
, Closed
);
823 LOG(1, s
, t
, "Unexpected LCP protocol reject 0x%X\n",
824 ntohs(*(uint16_t *) (p
+4)));
825 STAT(tunnel_rx_errors
);
828 else if (*p
== EchoReq
)
830 *p
= EchoReply
; // reply
831 *(uint32_t *) (p
+ 4) = htonl(session
[s
].magic
); // our magic number
832 q
= makeppp(b
, sizeof(b
), p
, l
, s
, t
, PPPLCP
);
835 LOG(4, s
, t
, "LCP: send %s\n", ppp_code(*q
));
836 if (config
->debug
> 3) dumplcp(q
, l
);
838 tunnelsend(b
, l
+ (q
- b
), t
); // send it
840 else if (*p
== EchoReply
)
842 // Ignore it, last_packet time is set earlier than this.
847 int mru
= session
[s
].mru
;
851 if (l
> mru
) l
= mru
;
854 q
= makeppp(b
, sizeof(b
), p
, l
, s
, t
, PPPLCP
);
857 LOG(2, s
, t
, "Unexpected LCP code %s\n", ppp_code(code
));
858 LOG(3, s
, t
, "LCP: send %s\n", ppp_code(*q
));
859 if (config
->debug
> 3) dumplcp(q
, l
);
861 tunnelsend(b
, l
+ (q
- b
), t
);
865 static void ipcp_open(sessionidt s
, tunnelidt t
)
867 LOG(3, s
, t
, "IPCP: Opened, session is now active\n");
869 change_state(s
, ipcp
, Opened
);
871 if (!session
[s
].walled_garden
)
873 uint16_t r
= radiusnew(s
);
875 radiussend(r
, RADIUSSTART
); // send radius start
878 // start IPv6 if configured and still in passive state
879 if (session
[s
].ppp
.ipv6cp
== Stopped
)
882 change_state(s
, ipv6cp
, RequestSent
);
886 // Process IPCP messages
887 void processipcp(sessionidt s
, tunnelidt t
, uint8_t *p
, uint16_t l
)
889 uint8_t b
[MAXCONTROL
];
895 LOG_HEX(5, "IPCP", p
, l
);
898 LOG(1, s
, t
, "Short IPCP %d bytes\n", l
);
899 STAT(tunnel_rx_errors
);
903 if ((hl
= ntohs(*(uint16_t *) (p
+ 2))) > l
)
905 LOG(1, s
, t
, "Length mismatch IPCP %u/%u\n", hl
, l
);
906 STAT(tunnel_rx_errors
);
911 if (session
[s
].ppp
.phase
< Network
)
913 LOG(2, s
, t
, "IPCP %s ignored in %s phase\n", ppp_code(*p
), ppp_phase(session
[s
].ppp
.phase
));
917 LOG(3, s
, t
, "IPCP: recv %s\n", ppp_code(*p
));
921 switch (session
[s
].ppp
.ipcp
)
924 initialise_restart_count(s
, ipcp
);
925 change_state(s
, ipcp
, AckReceived
);
930 LOG(2, s
, t
, "IPCP: ConfigAck in state %s? Sending ConfigReq\n", ppp_state(session
[s
].ppp
.ipcp
));
932 change_state(s
, ipcp
, RequestSent
);
940 LOG(2, s
, t
, "IPCP: ignoring %s in state %s\n", ppp_code(*p
), ppp_state(session
[s
].ppp
.ipcp
));
943 else if (*p
== ConfigReq
)
945 uint8_t *response
= 0;
955 case 3: // ip address
956 gotip
++; // seen address
957 if (o
[1] != 6 || o
[1] > length
) return;
959 addr
= htonl(session
[s
].ip
);
960 if (memcmp(o
+ 2, &addr
, (sizeof addr
)))
962 q
= ppp_nak(s
, b
, sizeof(b
), PPPIPCP
, &response
, q
, p
, o
, (uint8_t *) &addr
, sizeof(addr
));
963 if (!q
|| *response
== ConfigRej
)
965 sessionshutdown(s
, "Can't negotiate IPCP.", 3, 0);
972 case 129: // primary DNS
973 if (o
[1] != 6 || o
[1] > length
) return;
975 addr
= htonl(session
[s
].dns1
);
976 if (memcmp(o
+ 2, &addr
, (sizeof addr
)))
978 q
= ppp_nak(s
, b
, sizeof(b
), PPPIPCP
, &response
, q
, p
, o
, (uint8_t *) &addr
, sizeof(addr
));
984 case 131: // secondary DNS
985 if (o
[1] != 6 || o
[1] > length
) return;
987 addr
= htonl(session
[s
].dns1
);
988 if (memcmp(o
+ 2, &addr
, sizeof(addr
)))
990 q
= ppp_nak(s
, b
, sizeof(b
), PPPIPCP
, &response
, q
, p
, o
, (uint8_t *) &addr
, sizeof(addr
));
997 LOG(2, s
, t
, " Rejecting PPP IPCP Option type %d\n", *o
);
998 q
= ppp_rej(s
, b
, sizeof(b
), PPPIPCP
, &response
, q
, p
, o
);
1008 l
= q
- response
; // IPCP packet length
1009 *((uint16_t *) (response
+ 2)) = htons(l
); // update header
1013 // Send packet back as ConfigAck
1014 response
= makeppp(b
, sizeof(b
), p
, l
, s
, t
, PPPIPCP
);
1015 if (!response
) return;
1016 *response
= ConfigAck
;
1020 LOG(1, s
, t
, "No IP in IPCP request\n");
1021 STAT(tunnel_rx_errors
);
1025 switch (session
[s
].ppp
.ipcp
)
1028 response
= makeppp(b
, sizeof(b
), p
, 2, s
, t
, PPPIPCP
);
1029 if (!response
) return;
1030 *response
= TerminateAck
;
1031 *((uint16_t *) (response
+ 2)) = htons(l
= 4);
1035 initialise_restart_count(s
, ipcp
);
1037 if (*response
== ConfigAck
)
1038 change_state(s
, ipcp
, AckSent
);
1040 change_state(s
, ipcp
, RequestSent
);
1045 if (*response
== ConfigAck
)
1046 change_state(s
, ipcp
, AckSent
);
1051 if (*response
== ConfigAck
)
1057 initialise_restart_count(s
, ipcp
);
1062 if (*response
== ConfigAck
)
1063 change_state(s
, ipcp
, AckSent
);
1065 change_state(s
, ipcp
, RequestSent
);
1070 LOG(2, s
, t
, "IPCP: ignoring %s in state %s\n", ppp_code(*p
), ppp_state(session
[s
].ppp
.ipcp
));
1074 LOG(3, s
, t
, "IPCP: send %s\n", ppp_code(*response
));
1075 tunnelsend(b
, l
+ (response
- b
), t
);
1077 else if (*p
== TerminateReq
)
1080 q
= makeppp(b
, sizeof(b
), p
, l
, s
, t
, PPPIPCP
);
1082 LOG(3, s
, t
, "IPCP: send %s\n", ppp_code(*q
));
1083 tunnelsend(b
, l
+ (q
- b
), t
);
1084 change_state(s
, ipcp
, Stopped
);
1089 int mru
= session
[s
].mru
;
1093 if (l
> mru
) l
= mru
;
1096 q
= makeppp(b
, sizeof(b
), p
, l
, s
, t
, PPPIPCP
);
1099 LOG(2, s
, t
, "Unexpected IPCP code %s\n", ppp_code(code
));
1100 LOG(3, s
, t
, "IPCP: send %s\n", ppp_code(*q
));
1101 tunnelsend(b
, l
+ (q
- b
), t
);
1105 static void ipv6cp_open(sessionidt s
, tunnelidt t
)
1107 LOG(3, s
, t
, "IPV6CP: Opened\n");
1109 change_state(s
, ipv6cp
, Opened
);
1110 if (session
[s
].ipv6prefixlen
)
1111 route6set(s
, session
[s
].ipv6route
, session
[s
].ipv6prefixlen
, 1);
1113 // Send an initial RA (TODO: Should we send these regularly?)
1114 send_ipv6_ra(s
, t
, NULL
);
1117 // Process IPV6CP messages
1118 void processipv6cp(sessionidt s
, tunnelidt t
, uint8_t *p
, uint16_t l
)
1120 uint8_t b
[MAXCONTROL
];
1124 CSTAT(processipv6cp
);
1126 LOG_HEX(5, "IPV6CP", p
, l
);
1129 LOG(1, s
, t
, "Short IPV6CP %d bytes\n", l
);
1130 STAT(tunnel_rx_errors
);
1134 if ((hl
= ntohs(*(uint16_t *) (p
+ 2))) > l
)
1136 LOG(1, s
, t
, "Length mismatch IPV6CP %u/%u\n", hl
, l
);
1137 STAT(tunnel_rx_errors
);
1142 if (session
[s
].ppp
.phase
< Network
)
1144 LOG(2, s
, t
, "IPV6CP %s ignored in %s phase\n", ppp_code(*p
), ppp_phase(session
[s
].ppp
.phase
));
1148 LOG(3, s
, t
, "IPV6CP: recv %s\n", ppp_code(*p
));
1150 if (!config
->ipv6_prefix
.s6_addr
[0])
1152 LOG(2, s
, t
, "IPV6CP: %s rejected (not configured)\n", ppp_code(*p
));
1154 q
= makeppp(b
, sizeof(b
), p
, l
, s
, t
, PPPIPV6CP
);
1156 tunnelsend(b
, l
+ (q
- b
), t
);
1162 LOG(3, s
, t
, "IPV6CP: no IPv4 address (IPCP in state %s)\n", ppp_state(session
[s
].ppp
.ipcp
));
1163 return; // need IPCP to complete...
1166 if (*p
== ConfigAck
)
1168 switch (session
[s
].ppp
.ipv6cp
)
1171 initialise_restart_count(s
, ipv6cp
);
1172 change_state(s
, ipv6cp
, AckReceived
);
1177 LOG(2, s
, t
, "IPV6CP: ConfigAck in state %s? Sending ConfigReq\n", ppp_state(session
[s
].ppp
.ipv6cp
));
1179 change_state(s
, ipv6cp
, RequestSent
);
1187 LOG(2, s
, t
, "IPV6CP: ignoring %s in state %s\n", ppp_code(*p
), ppp_state(session
[s
].ppp
.ipv6cp
));
1190 else if (*p
== ConfigReq
)
1192 uint8_t *response
= 0;
1202 case 1: // interface identifier
1203 gotip
++; // seen address
1204 if (o
[1] != 10 || o
[1] > length
) return;
1206 *(uint32_t *) ident
= htonl(session
[s
].ip
);
1207 *(uint32_t *) (ident
+ 4) = 0;
1209 if (memcmp(o
+ 2, ident
, sizeof(ident
)))
1211 q
= ppp_nak(s
, b
, sizeof(b
), PPPIPV6CP
, &response
, q
, p
, o
, ident
, sizeof(ident
));
1218 LOG(2, s
, t
, " Rejecting PPP IPV6CP Option type %d\n", *o
);
1219 q
= ppp_rej(s
, b
, sizeof(b
), PPPIPV6CP
, &response
, q
, p
, o
);
1229 l
= q
- response
; // IPV6CP packet length
1230 *((uint16_t *) (response
+ 2)) = htons(l
); // update header
1234 // Send packet back as ConfigAck
1235 response
= makeppp(b
, sizeof(b
), p
, l
, s
, t
, PPPIPV6CP
);
1236 if (!response
) return;
1237 *response
= ConfigAck
;
1241 LOG(1, s
, t
, "No interface identifier in IPV6CP request\n");
1242 STAT(tunnel_rx_errors
);
1246 switch (session
[s
].ppp
.ipv6cp
)
1249 response
= makeppp(b
, sizeof(b
), p
, 2, s
, t
, PPPIPV6CP
);
1250 if (!response
) return;
1251 *response
= TerminateAck
;
1252 *((uint16_t *) (response
+ 2)) = htons(l
= 4);
1256 initialise_restart_count(s
, ipv6cp
);
1258 if (*response
== ConfigAck
)
1259 change_state(s
, ipv6cp
, AckSent
);
1261 change_state(s
, ipv6cp
, RequestSent
);
1266 if (*response
== ConfigAck
)
1267 change_state(s
, ipv6cp
, AckSent
);
1272 if (*response
== ConfigAck
)
1278 initialise_restart_count(s
, ipv6cp
);
1283 if (*response
== ConfigAck
)
1284 change_state(s
, ipv6cp
, AckSent
);
1286 change_state(s
, ipv6cp
, RequestSent
);
1291 LOG(2, s
, t
, "IPV6CP: ignoring %s in state %s\n", ppp_code(*p
), ppp_state(session
[s
].ppp
.ipv6cp
));
1295 LOG(3, s
, t
, "IPV6CP: send %s\n", ppp_code(*response
));
1296 tunnelsend(b
, l
+ (response
- b
), t
);
1298 else if (*p
== TerminateReq
)
1301 q
= makeppp(b
, sizeof(b
), p
, l
, s
, t
, PPPIPV6CP
);
1303 LOG(3, s
, t
, "IPV6CP: send %s\n", ppp_code(*q
));
1304 tunnelsend(b
, l
+ (q
- b
), t
);
1305 change_state(s
, ipv6cp
, Stopped
);
1310 int mru
= session
[s
].mru
;
1314 if (l
> mru
) l
= mru
;
1317 q
= makeppp(b
, sizeof(b
), p
, l
, s
, t
, PPPIPV6CP
);
1320 LOG(2, s
, t
, "Unexpected IPV6CP code %s\n", ppp_code(code
));
1321 LOG(3, s
, t
, "IPV6CP: send %s\n", ppp_code(*q
));
1322 tunnelsend(b
, l
+ (q
- b
), t
);
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
);
1338 ip
= ntohl(*(uint32_t *)(p
+ 12));
1342 LOG(1, s
, t
, "IP packet too long %d\n", l
);
1343 STAT(tunnel_rx_errors
);
1347 if (session
[s
].ppp
.phase
!= Network
|| session
[s
].ppp
.ipcp
!= Opened
)
1350 // no spoof (do sessionbyip to handled statically routed subnets)
1351 if (ip
!= session
[s
].ip
&& sessionbyip(htonl(ip
)) != s
)
1353 LOG(5, s
, t
, "Dropping packet with spoofed IP %s\n", fmtaddr(htonl(ip
), 0));
1357 // run access-list if any
1358 if (session
[s
].filter_in
&& !ip_filter(p
, l
, session
[s
].filter_in
- 1))
1361 // Add on the tun header
1363 *(uint32_t *) p
= htonl(PKTIP
);
1366 // Are we throttled and a slave?
1367 if (session
[s
].tbf_in
&& !config
->cluster_iam_master
) {
1368 // Pass it to the master for handling.
1369 master_throttle_packet(session
[s
].tbf_in
, p
, l
);
1373 // Are we throttled and a master??
1374 if (session
[s
].tbf_in
&& config
->cluster_iam_master
) {
1375 // Actually handle the throttled packets.
1376 tbf_queue_packet(session
[s
].tbf_in
, p
, l
);
1381 if (tun_write(p
, l
) < 0)
1383 STAT(tun_tx_errors
);
1384 LOG(0, s
, t
, "Error writing %d bytes to TUN device: %s (tunfd=%d, p=%p)\n",
1385 l
, strerror(errno
), tunfd
, p
);
1393 if (session
[s
].snoop_ip
&& session
[s
].snoop_port
)
1395 // Snooping this session
1396 snoop_send_packet(p
, l
, session
[s
].snoop_ip
, session
[s
].snoop_port
);
1399 increment_counter(&session
[s
].cin
, &session
[s
].cin_wrap
, l
);
1400 session
[s
].cin_delta
+= l
;
1403 sess_local
[s
].cin
+= l
;
1404 sess_local
[s
].pin
++;
1408 STAT(tun_tx_packets
);
1409 INC_STAT(tun_tx_bytes
, l
);
1412 // process IPv6 packet received
1414 // This MUST be called with at least 4 byte behind 'p'.
1415 // (i.e. this routine writes to p[-4]).
1416 void processipv6in(sessionidt s
, tunnelidt t
, uint8_t *p
, uint16_t l
)
1421 CSTAT(processipv6in
);
1423 LOG_HEX(5, "IPv6", p
, l
);
1425 ip
= *(struct in6_addr
*) (p
+ 8);
1426 ipv4
= ntohl(*(uint32_t *)(p
+ 16));
1430 LOG(1, s
, t
, "IP packet too long %d\n", l
);
1431 STAT(tunnel_rx_errors
);
1435 if (session
[s
].ppp
.phase
!= Network
|| session
[s
].ppp
.ipv6cp
!= Opened
)
1439 if (ipv4
!= session
[s
].ip
&& memcmp(&config
->ipv6_prefix
, &ip
, 8) && sessionbyipv6(ip
) != s
)
1441 char str
[INET6_ADDRSTRLEN
];
1442 LOG(5, s
, t
, "Dropping packet with spoofed IP %s\n",
1443 inet_ntop(AF_INET6
, &ip
, str
, INET6_ADDRSTRLEN
));
1447 // Check if it's a Router Solicition message.
1448 if (*(p
+ 6) == 58 && *(p
+ 7) == 255 && *(p
+ 24) == 0xFF && *(p
+ 25) == 2 &&
1449 *(uint32_t *)(p
+ 26) == 0 && *(uint32_t *)(p
+ 30) == 0 &&
1450 *(uint32_t *)(p
+ 34) == 0 &&
1451 *(p
+ 38) == 0 && *(p
+ 39) == 2 && *(p
+ 40) == 133) {
1452 LOG(3, s
, t
, "Got IPv6 RS\n");
1453 send_ipv6_ra(s
, t
, &ip
);
1457 // Add on the tun header
1459 *(uint32_t *) p
= htonl(PKTIPV6
);
1462 // Are we throttled and a slave?
1463 if (session
[s
].tbf_in
&& !config
->cluster_iam_master
) {
1464 // Pass it to the master for handling.
1465 master_throttle_packet(session
[s
].tbf_in
, p
, l
);
1469 // Are we throttled and a master??
1470 if (session
[s
].tbf_in
&& config
->cluster_iam_master
) {
1471 // Actually handle the throttled packets.
1472 tbf_queue_packet(session
[s
].tbf_in
, p
, l
);
1477 if (tun_write(p
, l
) < 0)
1479 STAT(tun_tx_errors
);
1480 LOG(0, s
, t
, "Error writing %d bytes to TUN device: %s (tunfd=%d, p=%p)\n",
1481 l
, strerror(errno
), tunfd
, p
);
1489 if (session
[s
].snoop_ip
&& session
[s
].snoop_port
)
1491 // Snooping this session
1492 snoop_send_packet(p
, l
, session
[s
].snoop_ip
, session
[s
].snoop_port
);
1495 increment_counter(&session
[s
].cin
, &session
[s
].cin_wrap
, l
);
1496 session
[s
].cin_delta
+= l
;
1499 sess_local
[s
].cin
+= l
;
1500 sess_local
[s
].pin
++;
1504 STAT(tun_tx_packets
);
1505 INC_STAT(tun_tx_bytes
, l
);
1509 // Helper routine for the TBF filters.
1510 // Used to send queued data in from the user.
1512 void send_ipin(sessionidt s
, uint8_t *buf
, int len
)
1514 LOG_HEX(5, "IP in throttled", buf
, len
);
1516 if (write(tunfd
, buf
, len
) < 0)
1518 STAT(tun_tx_errors
);
1519 LOG(0, 0, 0, "Error writing %d bytes to TUN device: %s (tunfd=%d, p=%p)\n",
1520 len
, strerror(errno
), tunfd
, buf
);
1528 if (session
[s
].snoop_ip
&& session
[s
].snoop_port
)
1530 // Snooping this session
1531 snoop_send_packet(buf
, len
, session
[s
].snoop_ip
, session
[s
].snoop_port
);
1534 // Increment packet counters
1535 increment_counter(&session
[s
].cin
, &session
[s
].cin_wrap
, len
);
1536 session
[s
].cin_delta
+= len
;
1539 sess_local
[s
].cin
+= len
;
1540 sess_local
[s
].pin
++;
1544 STAT(tun_tx_packets
);
1545 INC_STAT(tun_tx_bytes
, len
- 4);
1549 // Process CCP messages
1550 void processccp(sessionidt s
, tunnelidt t
, uint8_t *p
, uint16_t l
)
1552 uint8_t b
[MAXCONTROL
];
1557 LOG_HEX(5, "CCP", p
, l
);
1559 if (session
[s
].ppp
.phase
< Network
)
1561 LOG(2, s
, t
, "CCP %s ignored in %s phase\n", ppp_code(*p
), ppp_phase(session
[s
].ppp
.phase
));
1567 LOG(1, s
, t
, "Short CCP packet\n");
1568 STAT(tunnel_rx_errors
);
1571 LOG(3, s
, t
, "CCP: recv %s\n", ppp_code(*p
));
1572 if (*p
== ConfigAck
)
1574 switch (session
[s
].ppp
.ccp
)
1577 initialise_restart_count(s
, ccp
);
1578 change_state(s
, ccp
, AckReceived
);
1583 LOG(2, s
, t
, "CCP: ConfigAck in state %s? Sending ConfigReq\n", ppp_state(session
[s
].ppp
.ccp
));
1585 change_state(s
, ccp
, RequestSent
);
1589 LOG(3, s
, t
, "CCP: Opened\n");
1590 change_state(s
, ccp
, Opened
);
1594 LOG(2, s
, t
, "CCP: ignoring %s in state %s\n", ppp_code(*p
), ppp_state(session
[s
].ppp
.ccp
));
1597 else if (*p
== ConfigReq
)
1599 if (l
< 6) // accept no compression
1601 else // compression requested--reject
1604 q
= makeppp(b
, sizeof(b
), p
, l
, s
, t
, PPPCCP
);
1607 switch (session
[s
].ppp
.ccp
)
1610 q
= makeppp(b
, sizeof(b
), p
, 2, s
, t
, PPPCCP
);
1613 *((uint16_t *) (q
+ 2)) = htons(l
= 4);
1617 initialise_restart_count(s
, ccp
);
1619 if (*q
== ConfigAck
)
1620 change_state(s
, ccp
, AckSent
);
1622 change_state(s
, ccp
, RequestSent
);
1627 if (*q
== ConfigAck
)
1628 change_state(s
, ccp
, AckSent
);
1633 if (*q
== ConfigAck
)
1634 change_state(s
, ccp
, Opened
);
1639 initialise_restart_count(s
, ccp
);
1644 if (*q
== ConfigAck
)
1645 change_state(s
, ccp
, AckSent
);
1647 change_state(s
, ccp
, RequestSent
);
1652 LOG(2, s
, t
, "CCP: ignoring %s in state %s\n", ppp_code(*p
), ppp_state(session
[s
].ppp
.ccp
));
1656 LOG(3, s
, t
, "CCP: send %s\n", ppp_code(*q
));
1657 tunnelsend(b
, l
+ (q
- b
), t
);
1659 else if (*p
== TerminateReq
)
1662 q
= makeppp(b
, sizeof(b
), p
, l
, s
, t
, PPPCCP
);
1664 LOG(3, s
, t
, "CCP: send %s\n", ppp_code(*q
));
1665 tunnelsend(b
, l
+ (q
- b
), t
);
1666 change_state(s
, ccp
, Stopped
);
1671 int mru
= session
[s
].mru
;
1675 if (l
> mru
) l
= mru
;
1678 q
= makeppp(b
, sizeof(b
), p
, l
, s
, t
, PPPCCP
);
1681 LOG(2, s
, t
, "Unexpected CCP code %s\n", ppp_code(code
));
1682 LOG(3, s
, t
, "CCP: send %s\n", ppp_code(*q
));
1683 tunnelsend(b
, l
+ (q
- b
), t
);
1687 // send a CHAP challenge
1688 void sendchap(sessionidt s
, tunnelidt t
)
1690 uint8_t b
[MAXCONTROL
];
1699 LOG(1, s
, t
, "No RADIUS to send challenge\n");
1700 STAT(tunnel_tx_errors
);
1704 LOG(1, s
, t
, "Send CHAP challenge\n");
1706 radius
[r
].chap
= 1; // CHAP not PAP
1708 if (radius
[r
].state
!= RADIUSCHAP
)
1711 radius
[r
].state
= RADIUSCHAP
;
1712 radius
[r
].retry
= backoff(radius
[r
].try++);
1713 if (radius
[r
].try > 5)
1715 sessionshutdown(s
, "CHAP timeout.", 3, 0);
1716 STAT(tunnel_tx_errors
);
1719 q
= makeppp(b
, sizeof(b
), 0, 0, s
, t
, PPPCHAP
);
1722 *q
= 1; // challenge
1723 q
[1] = radius
[r
].id
; // ID
1724 q
[4] = 16; // value size (size of challenge)
1725 memcpy(q
+ 5, radius
[r
].auth
, 16); // challenge
1726 strcpy((char *) q
+ 21, hostname
); // our name
1727 *(uint16_t *) (q
+ 2) = htons(strlen(hostname
) + 21); // length
1728 tunnelsend(b
, strlen(hostname
) + 21 + (q
- b
), t
); // send it
1731 // fill in a L2TP message with a PPP frame,
1732 // copies existing PPP message and changes magic number if seen
1733 // returns start of PPP frame
1734 uint8_t *makeppp(uint8_t *b
, int size
, uint8_t *p
, int l
, sessionidt s
, tunnelidt t
, uint16_t mtype
)
1736 if (size
< 12) // Need more space than this!!
1738 static int backtrace_count
= 0;
1739 LOG(0, s
, t
, "makeppp buffer too small for L2TP header (size=%d)\n", size
);
1740 log_backtrace(backtrace_count
, 5)
1744 *(uint16_t *) (b
+ 0) = htons(0x0002); // L2TP with no options
1745 *(uint16_t *) (b
+ 2) = htons(tunnel
[t
].far
); // tunnel
1746 *(uint16_t *) (b
+ 4) = htons(session
[s
].far
); // session
1748 if (mtype
== PPPLCP
|| !(session
[s
].l2tp_flags
& SESSIONACFC
))
1750 *(uint16_t *) b
= htons(0xFF03); // HDLC header
1753 if (mtype
< 0x100 && session
[s
].l2tp_flags
& SESSIONPFC
)
1757 *(uint16_t *) b
= htons(mtype
);
1763 static int backtrace_count
= 0;
1764 LOG(2, s
, t
, "makeppp would overflow buffer (size=%d, header+payload=%d)\n", size
, l
+ 12);
1765 log_backtrace(backtrace_count
, 5)
1775 static int add_lcp_auth(uint8_t *b
, int size
, int authtype
)
1778 if ((authtype
== AUTHCHAP
&& size
< 5) || size
< 4)
1781 *b
++ = 3; // Authentication-Protocol
1782 if (authtype
== AUTHCHAP
)
1784 len
= *b
++ = 5; // length
1785 *(uint16_t *) b
= htons(PPPCHAP
); b
+= 2;
1788 else if (authtype
== AUTHPAP
)
1790 len
= *b
++ = 4; // length
1791 *(uint16_t *) b
= htons(PPPPAP
); b
+= 2;
1795 LOG(0, 0, 0, "add_lcp_auth called with unsupported auth type %d\n", authtype
);
1801 // Send initial LCP ConfigReq for MRU, authentication type and magic no
1802 void sendlcp(sessionidt s
, tunnelidt t
, int authtype
)
1804 uint8_t b
[500], *q
, *l
;
1806 if (!(q
= makeppp(b
, sizeof(b
), NULL
, 0, s
, t
, PPPLCP
)))
1809 LOG(3, s
, t
, "LCP: send ConfigReq%s%s%s\n",
1810 authtype
? " (" : "",
1811 authtype
? (authtype
== AUTHCHAP
? "CHAP" : "PAP") : "",
1812 authtype
? ")" : "");
1814 if (!session
[s
].mru
)
1815 session
[s
].mru
= DEFAULT_MRU
;
1819 *l
++ = (time_now
% 255) + 1; // ID
1821 l
+= 2; //Save space for length
1823 *l
++ = 1; *l
++ = 4; // Maximum-Receive-Unit (length 4)
1824 *(uint16_t *) l
= htons(session
[s
].mru
); l
+= 2;
1827 l
+= add_lcp_auth(l
, sizeof(b
) - (l
- b
), authtype
);
1829 *l
++ = 5; *l
++ = 6; // Magic-Number (length 6)
1830 *(uint32_t *) l
= htonl(session
[s
].magic
);
1833 *(uint16_t *)(q
+ 2) = htons(l
- q
); // Length
1835 LOG_HEX(5, "PPPLCP", q
, l
- q
);
1836 if (config
->debug
> 3) dumplcp(q
, l
- q
);
1838 tunnelsend(b
, (l
- b
), t
);
1841 // Send CCP request for no compression
1842 void sendccp(sessionidt s
, tunnelidt t
)
1846 if (!(q
= makeppp(b
, sizeof(b
), NULL
, 0, s
, t
, PPPCCP
)))
1849 LOG(3, s
, t
, "CCP: send ConfigReq (no compression)\n");
1852 *(uint8_t *)(q
+ 1) = (time_now
% 255) + 1; // ID
1853 *(uint16_t *)(q
+ 2) = htons(4); // Length
1855 LOG_HEX(5, "PPPCCP", q
, 4);
1856 tunnelsend(b
, (q
- b
) + 4 , t
);