d2708cd712ff55689dee1a68417a7158075aa265
3 char const *cvs_id_ppp
= "$Id: ppp.c,v 1.71 2005/08/11 06:22:11 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
);
89 if (session
[s
].ip
|| !r
)
91 // respond now, either no RADIUS available or already authenticated
92 uint8_t b
[MAXCONTROL
];
94 uint8_t *p
= makeppp(b
, sizeof(b
), 0, 0, s
, t
, PPPPAP
);
100 *p
= 3; // cant authorise
102 *(uint16_t *) (p
+ 2) = htons(5); // length
103 p
[4] = 0; // no message
104 tunnelsend(b
, 5 + (p
- b
), t
); // send it
108 LOG(3, s
, t
, "Already an IP allocated: %s (%d)\n",
109 fmtaddr(htonl(session
[s
].ip
), 0), session
[s
].ip_pool_index
);
113 LOG(1, s
, t
, "No RADIUS session available to authenticate session...\n");
114 sessionshutdown(s
, "No free RADIUS sessions.", 4, 0);
119 // Run PRE_AUTH plugins
120 struct param_pre_auth packet
= { &tunnel
[t
], &session
[s
], strdup(user
), strdup(pass
), PPPPAP
, 1 };
121 run_plugins(PLUGIN_PRE_AUTH
, &packet
);
122 if (!packet
.continue_auth
)
124 LOG(3, s
, t
, "A plugin rejected PRE_AUTH\n");
125 if (packet
.username
) free(packet
.username
);
126 if (packet
.password
) free(packet
.password
);
130 strncpy(session
[s
].user
, packet
.username
, sizeof(session
[s
].user
) - 1);
131 strncpy(radius
[r
].pass
, packet
.password
, sizeof(radius
[r
].pass
) - 1);
133 free(packet
.username
);
134 free(packet
.password
);
137 LOG(3, s
, t
, "Sending login for %s/%s to RADIUS\n", user
, pass
);
138 radiussend(r
, RADIUSAUTH
);
142 // Process CHAP messages
143 void processchap(sessionidt s
, tunnelidt t
, uint8_t *p
, uint16_t l
)
150 LOG_HEX(5, "CHAP", p
, l
);
151 r
= sess_local
[s
].radius
;
154 LOG(1, s
, t
, "Unexpected CHAP message\n");
155 STAT(tunnel_rx_errors
);
161 LOG(1, s
, t
, "Short CHAP %u bytes\n", l
);
162 STAT(tunnel_rx_errors
);
163 sessionshutdown(s
, "Short CHAP packet.", 3, 0);
167 if ((hl
= ntohs(*(uint16_t *) (p
+ 2))) > l
)
169 LOG(1, s
, t
, "Length mismatch CHAP %u/%u\n", hl
, l
);
170 STAT(tunnel_rx_errors
);
171 sessionshutdown(s
, "CHAP length mismatch.", 3, 0);
178 LOG(1, s
, t
, "Unexpected CHAP response code %d\n", *p
);
179 STAT(tunnel_rx_errors
);
180 sessionshutdown(s
, "CHAP length mismatch.", 3, 0);
184 if (session
[s
].ppp
.phase
!= Authenticate
)
186 LOG(2, s
, t
, "CHAP ignored in %s phase\n", ppp_phase(session
[s
].ppp
.phase
));
190 if (p
[1] != radius
[r
].id
)
192 LOG(1, s
, t
, "Wrong CHAP response ID %d (should be %d) (%d)\n", p
[1], radius
[r
].id
, r
);
193 STAT(tunnel_rx_errors
);
194 sessionshutdown(s
, "Unexpected CHAP response ID.", 3, 0);
198 if (l
< 5 || p
[4] != 16)
200 LOG(1, s
, t
, "Bad CHAP response length %d\n", l
< 5 ? -1 : p
[4]);
201 STAT(tunnel_rx_errors
);
202 sessionshutdown(s
, "Bad CHAP response length.", 3, 0);
208 if (l
< 16 || l
- 16 >= sizeof(session
[s
].user
))
210 LOG(1, s
, t
, "CHAP user too long %d\n", l
- 16);
211 STAT(tunnel_rx_errors
);
212 sessionshutdown(s
, "CHAP username too long.", 3, 0);
216 // Run PRE_AUTH plugins
218 struct param_pre_auth packet
= { &tunnel
[t
], &session
[s
], NULL
, NULL
, PPPCHAP
, 1 };
220 packet
.password
= calloc(17, 1);
221 memcpy(packet
.password
, p
, 16);
226 packet
.username
= calloc(l
+ 1, 1);
227 memcpy(packet
.username
, p
, l
);
229 run_plugins(PLUGIN_PRE_AUTH
, &packet
);
230 if (!packet
.continue_auth
)
232 LOG(3, s
, t
, "A plugin rejected PRE_AUTH\n");
233 if (packet
.username
) free(packet
.username
);
234 if (packet
.password
) free(packet
.password
);
238 strncpy(session
[s
].user
, packet
.username
, sizeof(session
[s
].user
) - 1);
239 memcpy(radius
[r
].pass
, packet
.password
, 16);
241 free(packet
.username
);
242 free(packet
.password
);
246 LOG(3, s
, t
, "CHAP login %s\n", session
[s
].user
);
247 radiussend(r
, RADIUSAUTH
);
250 static void dumplcp(uint8_t *p
, int l
)
253 uint8_t *o
= (p
+ 4);
255 LOG_HEX(5, "PPP LCP Packet", p
, l
);
256 LOG(4, 0, 0, "PPP LCP Packet type %d (%s len %d)\n", *p
, ppp_code((int)*p
), ntohs( ((uint16_t *) p
)[1]) );
257 LOG(4, 0, 0, "Length: %d\n", l
);
258 if (*p
!= ConfigReq
&& *p
!= ConfigRej
&& *p
!= ConfigAck
)
267 LOG(4, 0, 0, " Option length is %d...\n", length
);
272 LOG(4, 0, 0, " Option type is 0...\n");
279 case 1: // Maximum-Receive-Unit
281 LOG(4, 0, 0, " %s %d\n", ppp_lcp_option(type
), ntohs(*(uint16_t *)(o
+ 2)));
283 LOG(4, 0, 0, " %s odd length %d\n", ppp_lcp_option(type
), length
);
285 case 2: // Async-Control-Character-Map
288 uint32_t asyncmap
= ntohl(*(uint32_t *)(o
+ 2));
289 LOG(4, 0, 0, " %s %x\n", ppp_lcp_option(type
), asyncmap
);
292 LOG(4, 0, 0, " %s odd length %d\n", ppp_lcp_option(type
), length
);
294 case 3: // Authentication-Protocol
297 int proto
= ntohs(*(uint16_t *)(o
+ 2));
298 LOG(4, 0, 0, " %s 0x%x (%s)\n", ppp_lcp_option(type
), proto
,
299 proto
== PPPPAP
? "PAP" : "UNSUPPORTED");
301 else if (length
== 5)
303 int proto
= ntohs(*(uint16_t *)(o
+ 2));
304 int algo
= *(uint8_t *)(o
+ 4);
305 LOG(4, 0, 0, " %s 0x%x 0x%x (%s)\n", ppp_lcp_option(type
), proto
, algo
,
306 (proto
== PPPCHAP
&& algo
== 5) ? "CHAP MD5" : "UNSUPPORTED");
309 LOG(4, 0, 0, " %s odd length %d\n", ppp_lcp_option(type
), length
);
311 case 4: // Quality-Protocol
313 uint32_t qp
= ntohl(*(uint32_t *)(o
+ 2));
314 LOG(4, 0, 0, " %s %x\n", ppp_lcp_option(type
), qp
);
317 case 5: // Magic-Number
320 uint32_t magicno
= ntohl(*(uint32_t *)(o
+ 2));
321 LOG(4, 0, 0, " %s %x\n", ppp_lcp_option(type
), magicno
);
324 LOG(4, 0, 0, " %s odd length %d\n", ppp_lcp_option(type
), length
);
326 case 7: // Protocol-Field-Compression
327 case 8: // Address-And-Control-Field-Compression
328 LOG(4, 0, 0, " %s\n", ppp_lcp_option(type
));
331 LOG(2, 0, 0, " Unknown PPP LCP Option type %d\n", type
);
339 void lcp_open(sessionidt s
, tunnelidt t
)
341 // transition to Authentication or Network phase:
342 session
[s
].ppp
.phase
= sess_local
[s
].lcp_authtype
? Authenticate
: Network
;
344 LOG(3, s
, t
, "LCP: Opened, phase %s\n", ppp_phase(session
[s
].ppp
.phase
));
347 change_state(s
, lcp
, Opened
);
349 if (session
[s
].ppp
.phase
== Authenticate
)
351 if (sess_local
[s
].lcp_authtype
== AUTHCHAP
)
358 change_state(s
, ipcp
, RequestSent
);
359 // move to passive state for IPv6 (if configured), CCP
360 if (config
->ipv6_prefix
.s6_addr
[0])
361 change_state(s
, ipv6cp
, Stopped
);
363 change_state(s
, ipv6cp
, Closed
);
365 change_state(s
, ccp
, Stopped
);
369 static void lcp_restart(sessionidt s
)
371 session
[s
].ppp
.phase
= Establish
;
373 change_state(s
, ipcp
, Dead
);
374 change_state(s
, ipv6cp
, Dead
);
375 change_state(s
, ccp
, Dead
);
378 static uint8_t *ppp_rej(sessionidt s
, uint8_t *buf
, size_t blen
, uint16_t mtype
,
379 uint8_t **response
, uint8_t *queued
, uint8_t *packet
, uint8_t *option
)
381 if (!*response
|| **response
!= ConfigRej
)
383 queued
= *response
= makeppp(buf
, blen
, packet
, 2, s
, session
[s
].tunnel
, mtype
);
391 if ((queued
- buf
+ option
[1]) > blen
)
393 LOG(2, s
, session
[s
].tunnel
, "PPP overflow for ConfigRej (proto %u, option %u).\n", mtype
, *option
);
397 memcpy(queued
, option
, option
[1]);
398 return queued
+ option
[1];
401 static uint8_t *ppp_nak(sessionidt s
, uint8_t *buf
, size_t blen
, uint16_t mtype
,
402 uint8_t **response
, uint8_t *queued
, uint8_t *packet
, uint8_t *option
,
403 uint8_t *value
, size_t vlen
)
408 case PPPLCP
: nak_sent
= &sess_local
[s
].lcp
.nak_sent
; break;
409 case PPPIPCP
: nak_sent
= &sess_local
[s
].ipcp
.nak_sent
; break;
410 case PPPIPV6CP
: nak_sent
= &sess_local
[s
].ipv6cp
.nak_sent
; break;
411 default: return 0; // ?
414 if (*response
&& **response
!= ConfigNak
)
416 if (*nak_sent
< config
->ppp_max_failure
) // reject queued
419 return ppp_rej(s
, buf
, blen
, mtype
, response
, 0, packet
, option
);
424 if (*nak_sent
>= config
->ppp_max_failure
)
425 return ppp_rej(s
, buf
, blen
, mtype
, response
, 0, packet
, option
);
427 queued
= *response
= makeppp(buf
, blen
, packet
, 2, s
, session
[s
].tunnel
, mtype
);
436 if ((queued
- buf
+ vlen
+ 2) > blen
)
438 LOG(2, s
, session
[s
].tunnel
, "PPP overflow for ConfigNak (proto %u, option %u).\n", mtype
, *option
);
443 *queued
++ = vlen
+ 2;
444 memcpy(queued
, value
, vlen
);
445 return queued
+ vlen
;
448 // Process LCP messages
449 void processlcp(sessionidt s
, tunnelidt t
, uint8_t *p
, uint16_t l
)
451 uint8_t b
[MAXCONTROL
];
453 uint32_t magicno
= 0;
458 LOG_HEX(5, "LCP", p
, l
);
461 LOG(1, s
, t
, "Short LCP %d bytes\n", l
);
462 STAT(tunnel_rx_errors
);
466 if ((hl
= ntohs(*(uint16_t *) (p
+ 2))) > l
)
468 LOG(1, s
, t
, "Length mismatch LCP %u/%u\n", hl
, l
);
469 STAT(tunnel_rx_errors
);
474 if (session
[s
].die
) // going down...
477 LOG(*p
== EchoReq
? 4 : 3, s
, t
, "LCP: recv %s\n", ppp_code(*p
));
478 if (config
->debug
> 3) dumplcp(p
, l
);
483 uint8_t *o
= (p
+ 4);
491 if (length
== 0 || type
== 0 || x
< length
) break;
494 case 3: // Authentication-Protocol
496 int proto
= ntohs(*(uint16_t *)(o
+ 2));
499 else if (proto
== PPPCHAP
&& *(o
+ 4) == 5)
509 if (!session
[s
].ip
&& authtype
)
510 sess_local
[s
].lcp_authtype
= authtype
;
512 switch (session
[s
].ppp
.lcp
)
515 initialise_restart_count(s
, lcp
);
516 change_state(s
, lcp
, AckReceived
);
521 LOG(2, s
, t
, "LCP: ConfigAck in state %s? Sending ConfigReq\n", ppp_state(session
[s
].ppp
.lcp
));
522 if (session
[s
].ppp
.lcp
== Opened
)
525 sendlcp(s
, t
, sess_local
[s
].lcp_authtype
);
526 change_state(s
, lcp
, RequestSent
);
534 LOG(2, s
, t
, "LCP: ignoring %s in state %s\n", ppp_code(*p
), ppp_state(session
[s
].ppp
.lcp
));
537 else if (*p
== ConfigReq
)
540 uint8_t *o
= (p
+ 4);
541 uint8_t *response
= 0;
542 static uint8_t asyncmap
[4] = { 0, 0, 0, 0 }; // all zero
543 static uint8_t authproto
[5];
550 if (length
== 0 || type
== 0 || x
< length
) break;
553 case 1: // Maximum-Receive-Unit
554 session
[s
].mru
= ntohs(*(uint16_t *)(o
+ 2));
557 case 2: // Async-Control-Character-Map
558 if (!ntohl(*(uint32_t *)(o
+ 2))) // all bits zero is OK
561 LOG(3, s
, t
, " Remote requesting asyncmap. Rejecting.\n");
562 q
= ppp_nak(s
, b
, sizeof(b
), PPPLCP
, &response
, q
, p
, o
, asyncmap
, sizeof(asyncmap
));
565 case 3: // Authentication-Protocol
567 int proto
= ntohs(*(uint16_t *)(o
+ 2));
568 char proto_name
[] = "0x0000";
573 if (config
->radius_authtypes
& AUTHPAP
)
575 sess_local
[s
].lcp_authtype
= AUTHPAP
;
579 strcpy(proto_name
, "PAP");
581 else if (proto
== PPPCHAP
)
583 if (config
->radius_authtypes
& AUTHCHAP
584 && *(o
+ 4) == 5) // MD5
586 sess_local
[s
].lcp_authtype
= AUTHCHAP
;
590 strcpy(proto_name
, "CHAP");
593 sprintf(proto_name
, "%#4.4x", proto
);
595 LOG(3, s
, t
, " Remote requesting %s authentication. Rejecting.\n", proto_name
);
597 alen
= add_lcp_auth(authproto
, sizeof(authproto
), config
->radius_authprefer
);
598 if (alen
< 2) break; // paranoia
600 q
= ppp_nak(s
, b
, sizeof(b
), PPPLCP
, &response
, q
, p
, o
, authproto
+ 2, alen
- 2);
601 if (q
&& *response
== ConfigNak
&&
602 config
->radius_authtypes
!= config
->radius_authprefer
)
605 alen
= add_lcp_auth(authproto
, sizeof(authproto
), config
->radius_authtypes
& ~config
->radius_authprefer
);
607 q
= ppp_nak(s
, b
, sizeof(b
), PPPLCP
, &response
, q
, p
, o
, authproto
+ 2, alen
- 2);
614 case 5: // Magic-Number
615 magicno
= ntohl(*(uint32_t *)(o
+ 2));
618 case 4: // Quality-Protocol
619 case 7: // Protocol-Field-Compression
620 case 8: // Address-And-Control-Field-Compression
623 default: // Reject any unknown options
624 LOG(3, s
, t
, " Rejecting unknown PPP LCP option %d\n", type
);
625 q
= ppp_rej(s
, b
, sizeof(b
), PPPLCP
, &response
, q
, p
, o
);
633 l
= q
- response
; // LCP packet length
634 *((uint16_t *) (response
+ 2)) = htons(l
); // update header
638 // Send packet back as ConfigAck
639 response
= makeppp(b
, sizeof(b
), p
, l
, s
, t
, PPPLCP
);
640 if (!response
) return;
641 *response
= ConfigAck
;
644 switch (session
[s
].ppp
.lcp
)
647 response
= makeppp(b
, sizeof(b
), p
, 2, s
, t
, PPPLCP
);
648 if (!response
) return;
649 *response
= TerminateAck
;
650 *((uint16_t *) (response
+ 2)) = htons(l
= 4);
654 initialise_restart_count(s
, lcp
);
655 sendlcp(s
, t
, sess_local
[s
].lcp_authtype
);
656 if (*response
== ConfigAck
)
657 change_state(s
, lcp
, AckSent
);
659 change_state(s
, lcp
, RequestSent
);
664 if (*response
== ConfigAck
)
665 change_state(s
, lcp
, AckSent
);
670 if (*response
== ConfigAck
)
677 sendlcp(s
, t
, sess_local
[s
].lcp_authtype
);
681 if (*response
== ConfigAck
)
682 change_state(s
, lcp
, AckSent
);
684 change_state(s
, lcp
, RequestSent
);
689 LOG(2, s
, t
, "LCP: ignoring %s in state %s\n", ppp_code(*p
), ppp_state(session
[s
].ppp
.lcp
));
693 LOG(3, s
, t
, "LCP: send %s\n", ppp_code(*response
));
694 if (config
->debug
> 3) dumplcp(response
, l
);
696 tunnelsend(b
, l
+ (response
- b
), t
);
698 else if (*p
== ConfigNak
)
701 uint8_t *o
= (p
+ 4);
709 if (length
== 0 || type
== 0 || x
< length
) break;
712 case 1: // Maximum-Receive-Unit
713 session
[s
].mru
= ntohs(*(uint16_t *)(o
+ 2));
714 LOG(3, s
, t
, " Remote requested MRU of %u\n", session
[s
].mru
);
717 case 3: // Authentication-Protocol
722 int proto
= ntohs(*(uint16_t *)(o
+ 2));
725 authtype
= config
->radius_authtypes
& AUTHPAP
;
726 LOG(3, s
, t
, " Remote requested PAP authentication...%sing\n",
727 authtype
? "accept" : "reject");
729 else if (proto
== PPPCHAP
&& *(o
+ 4) == 5)
731 authtype
= config
->radius_authtypes
& AUTHCHAP
;
732 LOG(3, s
, t
, " Remote requested CHAP authentication...%sing\n",
733 authtype
? "accept" : "reject");
737 LOG(3, s
, t
, " Rejecting unsupported authentication %#4x\n",
745 LOG(2, s
, t
, " Remote NAKed LCP type %u?\n", type
);
754 sessionshutdown(s
, "Unsupported authentication.", 3, 0);
759 sess_local
[s
].lcp_authtype
= authtype
;
761 switch (session
[s
].ppp
.lcp
)
766 uint8_t *response
= makeppp(b
, sizeof(b
), p
, 2, s
, t
, PPPLCP
);
767 if (!response
) return;
768 *response
= TerminateAck
;
769 *((uint16_t *) (response
+ 2)) = htons(l
= 4);
771 LOG(3, s
, t
, "LCP: send %s\n", ppp_code(*response
));
772 if (config
->debug
> 3) dumplcp(response
, l
);
774 tunnelsend(b
, l
+ (response
- b
), t
);
780 initialise_restart_count(s
, lcp
);
781 sendlcp(s
, t
, sess_local
[s
].lcp_authtype
);
785 LOG(2, s
, t
, "LCP: ConfigNak in state %s? Sending ConfigReq\n", ppp_state(session
[s
].ppp
.lcp
));
786 sendlcp(s
, t
, sess_local
[s
].lcp_authtype
);
791 sendlcp(s
, t
, sess_local
[s
].lcp_authtype
);
795 LOG(2, s
, t
, "LCP: ignoring %s in state %s\n", ppp_code(*p
), ppp_state(session
[s
].ppp
.lcp
));
799 else if (*p
== TerminateReq
)
801 *p
= TerminateAck
; // close
802 q
= makeppp(b
, sizeof(b
), p
, l
, s
, t
, PPPLCP
);
805 LOG(3, s
, t
, "LCP: send %s\n", ppp_code(*q
));
806 if (config
->debug
> 3) dumplcp(q
, l
);
808 tunnelsend(b
, l
+ (q
- b
), t
); // send it
809 sessionshutdown(s
, "Remote end closed connection.", 3, 0);
811 else if (*p
== TerminateAck
)
813 sessionshutdown(s
, "Connection closed.", 3, 0);
815 else if (*p
== ProtocolRej
)
817 if (*(uint16_t *) (p
+4) == htons(PPPIPV6CP
))
819 LOG(3, s
, t
, "IPv6 rejected\n");
820 change_state(s
, ipv6cp
, Closed
);
824 LOG(1, s
, t
, "Unexpected LCP protocol reject 0x%X\n",
825 ntohs(*(uint16_t *) (p
+4)));
826 STAT(tunnel_rx_errors
);
829 else if (*p
== EchoReq
)
831 *p
= EchoReply
; // reply
832 *(uint32_t *) (p
+ 4) = htonl(session
[s
].magic
); // our magic number
833 q
= makeppp(b
, sizeof(b
), p
, l
, s
, t
, PPPLCP
);
836 LOG(4, s
, t
, "LCP: send %s\n", ppp_code(*q
));
837 if (config
->debug
> 3) dumplcp(q
, l
);
839 tunnelsend(b
, l
+ (q
- b
), t
); // send it
841 else if (*p
== EchoReply
)
843 // Ignore it, last_packet time is set earlier than this.
848 int mru
= session
[s
].mru
;
852 if (l
> mru
) l
= mru
;
855 q
= makeppp(b
, sizeof(b
), p
, l
, s
, t
, PPPLCP
);
858 LOG(2, s
, t
, "Unexpected LCP code %s\n", ppp_code(code
));
859 LOG(3, s
, t
, "LCP: send %s\n", ppp_code(*q
));
860 if (config
->debug
> 3) dumplcp(q
, l
);
862 tunnelsend(b
, l
+ (q
- b
), t
);
866 static void ipcp_open(sessionidt s
, tunnelidt t
)
868 LOG(3, s
, t
, "IPCP: Opened, session is now active\n");
870 change_state(s
, ipcp
, Opened
);
872 if (!session
[s
].walled_garden
)
874 uint16_t r
= radiusnew(s
);
876 radiussend(r
, RADIUSSTART
); // send radius start
879 // start IPv6 if configured and still in passive state
880 if (session
[s
].ppp
.ipv6cp
== Stopped
)
883 change_state(s
, ipv6cp
, RequestSent
);
887 // Process IPCP messages
888 void processipcp(sessionidt s
, tunnelidt t
, uint8_t *p
, uint16_t l
)
890 uint8_t b
[MAXCONTROL
];
896 LOG_HEX(5, "IPCP", p
, l
);
899 LOG(1, s
, t
, "Short IPCP %d bytes\n", l
);
900 STAT(tunnel_rx_errors
);
904 if ((hl
= ntohs(*(uint16_t *) (p
+ 2))) > l
)
906 LOG(1, s
, t
, "Length mismatch IPCP %u/%u\n", hl
, l
);
907 STAT(tunnel_rx_errors
);
912 if (session
[s
].ppp
.phase
< Network
)
914 LOG(2, s
, t
, "IPCP %s ignored in %s phase\n", ppp_code(*p
), ppp_phase(session
[s
].ppp
.phase
));
918 LOG(3, s
, t
, "IPCP: recv %s\n", ppp_code(*p
));
922 switch (session
[s
].ppp
.ipcp
)
925 initialise_restart_count(s
, ipcp
);
926 change_state(s
, ipcp
, AckReceived
);
931 LOG(2, s
, t
, "IPCP: ConfigAck in state %s? Sending ConfigReq\n", ppp_state(session
[s
].ppp
.ipcp
));
933 change_state(s
, ipcp
, RequestSent
);
941 LOG(2, s
, t
, "IPCP: ignoring %s in state %s\n", ppp_code(*p
), ppp_state(session
[s
].ppp
.ipcp
));
944 else if (*p
== ConfigReq
)
946 uint8_t *response
= 0;
956 case 3: // ip address
957 gotip
++; // seen address
958 if (o
[1] != 6 || o
[1] > length
) return;
960 addr
= htonl(session
[s
].ip
);
961 if (memcmp(o
+ 2, &addr
, (sizeof addr
)))
963 q
= ppp_nak(s
, b
, sizeof(b
), PPPIPCP
, &response
, q
, p
, o
, (uint8_t *) &addr
, sizeof(addr
));
964 if (!q
|| *response
== ConfigRej
)
966 sessionshutdown(s
, "Can't negotiate IPCP.", 3, 0);
973 case 129: // primary DNS
974 if (o
[1] != 6 || o
[1] > length
) return;
976 addr
= htonl(session
[s
].dns1
);
977 if (memcmp(o
+ 2, &addr
, (sizeof addr
)))
979 q
= ppp_nak(s
, b
, sizeof(b
), PPPIPCP
, &response
, q
, p
, o
, (uint8_t *) &addr
, sizeof(addr
));
985 case 131: // secondary DNS
986 if (o
[1] != 6 || o
[1] > length
) return;
988 addr
= htonl(session
[s
].dns1
);
989 if (memcmp(o
+ 2, &addr
, sizeof(addr
)))
991 q
= ppp_nak(s
, b
, sizeof(b
), PPPIPCP
, &response
, q
, p
, o
, (uint8_t *) &addr
, sizeof(addr
));
998 LOG(2, s
, t
, " Rejecting PPP IPCP Option type %d\n", *o
);
999 q
= ppp_rej(s
, b
, sizeof(b
), PPPIPCP
, &response
, q
, p
, o
);
1009 l
= q
- response
; // IPCP packet length
1010 *((uint16_t *) (response
+ 2)) = htons(l
); // update header
1014 // Send packet back as ConfigAck
1015 response
= makeppp(b
, sizeof(b
), p
, l
, s
, t
, PPPIPCP
);
1016 if (!response
) return;
1017 *response
= ConfigAck
;
1021 LOG(1, s
, t
, "No IP in IPCP request\n");
1022 STAT(tunnel_rx_errors
);
1026 switch (session
[s
].ppp
.ipcp
)
1029 response
= makeppp(b
, sizeof(b
), p
, 2, s
, t
, PPPIPCP
);
1030 if (!response
) return;
1031 *response
= TerminateAck
;
1032 *((uint16_t *) (response
+ 2)) = htons(l
= 4);
1036 initialise_restart_count(s
, ipcp
);
1038 if (*response
== ConfigAck
)
1039 change_state(s
, ipcp
, AckSent
);
1041 change_state(s
, ipcp
, RequestSent
);
1046 if (*response
== ConfigAck
)
1047 change_state(s
, ipcp
, AckSent
);
1052 if (*response
== ConfigAck
)
1058 initialise_restart_count(s
, ipcp
);
1063 if (*response
== ConfigAck
)
1064 change_state(s
, ipcp
, AckSent
);
1066 change_state(s
, ipcp
, RequestSent
);
1071 LOG(2, s
, t
, "IPCP: ignoring %s in state %s\n", ppp_code(*p
), ppp_state(session
[s
].ppp
.ipcp
));
1075 LOG(3, s
, t
, "IPCP: send %s\n", ppp_code(*response
));
1076 tunnelsend(b
, l
+ (response
- b
), t
);
1078 else if (*p
== TerminateReq
)
1081 q
= makeppp(b
, sizeof(b
), p
, l
, s
, t
, PPPIPCP
);
1083 LOG(3, s
, t
, "IPCP: send %s\n", ppp_code(*q
));
1084 tunnelsend(b
, l
+ (q
- b
), t
);
1085 change_state(s
, ipcp
, Stopped
);
1090 int mru
= session
[s
].mru
;
1094 if (l
> mru
) l
= mru
;
1097 q
= makeppp(b
, sizeof(b
), p
, l
, s
, t
, PPPIPCP
);
1100 LOG(2, s
, t
, "Unexpected IPCP code %s\n", ppp_code(code
));
1101 LOG(3, s
, t
, "IPCP: send %s\n", ppp_code(*q
));
1102 tunnelsend(b
, l
+ (q
- b
), t
);
1106 static void ipv6cp_open(sessionidt s
, tunnelidt t
)
1108 LOG(3, s
, t
, "IPV6CP: Opened\n");
1110 change_state(s
, ipv6cp
, Opened
);
1111 if (session
[s
].ipv6prefixlen
)
1112 route6set(s
, session
[s
].ipv6route
, session
[s
].ipv6prefixlen
, 1);
1114 // Send an initial RA (TODO: Should we send these regularly?)
1115 send_ipv6_ra(s
, t
, NULL
);
1118 // Process IPV6CP messages
1119 void processipv6cp(sessionidt s
, tunnelidt t
, uint8_t *p
, uint16_t l
)
1121 uint8_t b
[MAXCONTROL
];
1125 CSTAT(processipv6cp
);
1127 LOG_HEX(5, "IPV6CP", p
, l
);
1130 LOG(1, s
, t
, "Short IPV6CP %d bytes\n", l
);
1131 STAT(tunnel_rx_errors
);
1135 if ((hl
= ntohs(*(uint16_t *) (p
+ 2))) > l
)
1137 LOG(1, s
, t
, "Length mismatch IPV6CP %u/%u\n", hl
, l
);
1138 STAT(tunnel_rx_errors
);
1143 if (session
[s
].ppp
.phase
< Network
)
1145 LOG(2, s
, t
, "IPV6CP %s ignored in %s phase\n", ppp_code(*p
), ppp_phase(session
[s
].ppp
.phase
));
1149 LOG(3, s
, t
, "IPV6CP: recv %s\n", ppp_code(*p
));
1151 if (!config
->ipv6_prefix
.s6_addr
[0])
1153 LOG(2, s
, t
, "IPV6CP: %s rejected (not configured)\n", ppp_code(*p
));
1155 q
= makeppp(b
, sizeof(b
), p
, l
, s
, t
, PPPIPV6CP
);
1157 tunnelsend(b
, l
+ (q
- b
), t
);
1163 LOG(3, s
, t
, "IPV6CP: no IPv4 address (IPCP in state %s)\n", ppp_state(session
[s
].ppp
.ipcp
));
1164 return; // need IPCP to complete...
1167 if (*p
== ConfigAck
)
1169 switch (session
[s
].ppp
.ipv6cp
)
1172 initialise_restart_count(s
, ipv6cp
);
1173 change_state(s
, ipv6cp
, AckReceived
);
1178 LOG(2, s
, t
, "IPV6CP: ConfigAck in state %s? Sending ConfigReq\n", ppp_state(session
[s
].ppp
.ipv6cp
));
1180 change_state(s
, ipv6cp
, RequestSent
);
1188 LOG(2, s
, t
, "IPV6CP: ignoring %s in state %s\n", ppp_code(*p
), ppp_state(session
[s
].ppp
.ipv6cp
));
1191 else if (*p
== ConfigReq
)
1193 uint8_t *response
= 0;
1203 case 1: // interface identifier
1204 gotip
++; // seen address
1205 if (o
[1] != 10 || o
[1] > length
) return;
1207 *(uint32_t *) ident
= htonl(session
[s
].ip
);
1208 *(uint32_t *) (ident
+ 4) = 0;
1210 if (memcmp(o
+ 2, ident
, sizeof(ident
)))
1212 q
= ppp_nak(s
, b
, sizeof(b
), PPPIPV6CP
, &response
, q
, p
, o
, ident
, sizeof(ident
));
1219 LOG(2, s
, t
, " Rejecting PPP IPV6CP Option type %d\n", *o
);
1220 q
= ppp_rej(s
, b
, sizeof(b
), PPPIPV6CP
, &response
, q
, p
, o
);
1230 l
= q
- response
; // IPV6CP packet length
1231 *((uint16_t *) (response
+ 2)) = htons(l
); // update header
1235 // Send packet back as ConfigAck
1236 response
= makeppp(b
, sizeof(b
), p
, l
, s
, t
, PPPIPV6CP
);
1237 if (!response
) return;
1238 *response
= ConfigAck
;
1242 LOG(1, s
, t
, "No interface identifier in IPV6CP request\n");
1243 STAT(tunnel_rx_errors
);
1247 switch (session
[s
].ppp
.ipv6cp
)
1250 response
= makeppp(b
, sizeof(b
), p
, 2, s
, t
, PPPIPV6CP
);
1251 if (!response
) return;
1252 *response
= TerminateAck
;
1253 *((uint16_t *) (response
+ 2)) = htons(l
= 4);
1257 initialise_restart_count(s
, ipv6cp
);
1259 if (*response
== ConfigAck
)
1260 change_state(s
, ipv6cp
, AckSent
);
1262 change_state(s
, ipv6cp
, RequestSent
);
1267 if (*response
== ConfigAck
)
1268 change_state(s
, ipv6cp
, AckSent
);
1273 if (*response
== ConfigAck
)
1279 initialise_restart_count(s
, ipv6cp
);
1284 if (*response
== ConfigAck
)
1285 change_state(s
, ipv6cp
, AckSent
);
1287 change_state(s
, ipv6cp
, RequestSent
);
1292 LOG(2, s
, t
, "IPV6CP: ignoring %s in state %s\n", ppp_code(*p
), ppp_state(session
[s
].ppp
.ipv6cp
));
1296 LOG(3, s
, t
, "IPV6CP: send %s\n", ppp_code(*response
));
1297 tunnelsend(b
, l
+ (response
- b
), t
);
1299 else if (*p
== TerminateReq
)
1302 q
= makeppp(b
, sizeof(b
), p
, l
, s
, t
, PPPIPV6CP
);
1304 LOG(3, s
, t
, "IPV6CP: send %s\n", ppp_code(*q
));
1305 tunnelsend(b
, l
+ (q
- b
), t
);
1306 change_state(s
, ipv6cp
, Stopped
);
1311 int mru
= session
[s
].mru
;
1315 if (l
> mru
) l
= mru
;
1318 q
= makeppp(b
, sizeof(b
), p
, l
, s
, t
, PPPIPV6CP
);
1321 LOG(2, s
, t
, "Unexpected IPV6CP code %s\n", ppp_code(code
));
1322 LOG(3, s
, t
, "IPV6CP: send %s\n", ppp_code(*q
));
1323 tunnelsend(b
, l
+ (q
- b
), t
);
1327 // process IP packet received
1329 // This MUST be called with at least 4 byte behind 'p'.
1330 // (i.e. this routine writes to p[-4]).
1331 void processipin(sessionidt s
, tunnelidt t
, uint8_t *p
, uint16_t l
)
1337 LOG_HEX(5, "IP", p
, l
);
1339 ip
= ntohl(*(uint32_t *)(p
+ 12));
1343 LOG(1, s
, t
, "IP packet too long %d\n", l
);
1344 STAT(tunnel_rx_errors
);
1348 if (session
[s
].ppp
.phase
!= Network
|| session
[s
].ppp
.ipcp
!= Opened
)
1351 // no spoof (do sessionbyip to handled statically routed subnets)
1352 if (ip
!= session
[s
].ip
&& sessionbyip(htonl(ip
)) != s
)
1354 LOG(5, s
, t
, "Dropping packet with spoofed IP %s\n", fmtaddr(htonl(ip
), 0));
1358 // run access-list if any
1359 if (session
[s
].filter_in
&& !ip_filter(p
, l
, session
[s
].filter_in
- 1))
1362 // Add on the tun header
1364 *(uint32_t *) p
= htonl(PKTIP
);
1367 // Are we throttled and a slave?
1368 if (session
[s
].tbf_in
&& !config
->cluster_iam_master
) {
1369 // Pass it to the master for handling.
1370 master_throttle_packet(session
[s
].tbf_in
, p
, l
);
1374 // Are we throttled and a master??
1375 if (session
[s
].tbf_in
&& config
->cluster_iam_master
) {
1376 // Actually handle the throttled packets.
1377 tbf_queue_packet(session
[s
].tbf_in
, p
, l
);
1382 if (tun_write(p
, l
) < 0)
1384 STAT(tun_tx_errors
);
1385 LOG(0, s
, t
, "Error writing %d bytes to TUN device: %s (tunfd=%d, p=%p)\n",
1386 l
, strerror(errno
), tunfd
, p
);
1394 if (session
[s
].snoop_ip
&& session
[s
].snoop_port
)
1396 // Snooping this session
1397 snoop_send_packet(p
, l
, session
[s
].snoop_ip
, session
[s
].snoop_port
);
1400 increment_counter(&session
[s
].cin
, &session
[s
].cin_wrap
, l
);
1401 session
[s
].cin_delta
+= l
;
1404 sess_local
[s
].cin
+= l
;
1405 sess_local
[s
].pin
++;
1409 STAT(tun_tx_packets
);
1410 INC_STAT(tun_tx_bytes
, l
);
1413 // process IPv6 packet received
1415 // This MUST be called with at least 4 byte behind 'p'.
1416 // (i.e. this routine writes to p[-4]).
1417 void processipv6in(sessionidt s
, tunnelidt t
, uint8_t *p
, uint16_t l
)
1422 CSTAT(processipv6in
);
1424 LOG_HEX(5, "IPv6", p
, l
);
1426 ip
= *(struct in6_addr
*) (p
+ 8);
1427 ipv4
= ntohl(*(uint32_t *)(p
+ 16));
1431 LOG(1, s
, t
, "IP packet too long %d\n", l
);
1432 STAT(tunnel_rx_errors
);
1436 if (session
[s
].ppp
.phase
!= Network
|| session
[s
].ppp
.ipv6cp
!= Opened
)
1440 if (ipv4
!= session
[s
].ip
&& memcmp(&config
->ipv6_prefix
, &ip
, 8) && sessionbyipv6(ip
) != s
)
1442 char str
[INET6_ADDRSTRLEN
];
1443 LOG(5, s
, t
, "Dropping packet with spoofed IP %s\n",
1444 inet_ntop(AF_INET6
, &ip
, str
, INET6_ADDRSTRLEN
));
1448 // Check if it's a Router Solicition message.
1449 if (*(p
+ 6) == 58 && *(p
+ 7) == 255 && *(p
+ 24) == 0xFF && *(p
+ 25) == 2 &&
1450 *(uint32_t *)(p
+ 26) == 0 && *(uint32_t *)(p
+ 30) == 0 &&
1451 *(uint32_t *)(p
+ 34) == 0 &&
1452 *(p
+ 38) == 0 && *(p
+ 39) == 2 && *(p
+ 40) == 133) {
1453 LOG(3, s
, t
, "Got IPv6 RS\n");
1454 send_ipv6_ra(s
, t
, &ip
);
1458 // Add on the tun header
1460 *(uint32_t *) p
= htonl(PKTIPV6
);
1463 // Are we throttled and a slave?
1464 if (session
[s
].tbf_in
&& !config
->cluster_iam_master
) {
1465 // Pass it to the master for handling.
1466 master_throttle_packet(session
[s
].tbf_in
, p
, l
);
1470 // Are we throttled and a master??
1471 if (session
[s
].tbf_in
&& config
->cluster_iam_master
) {
1472 // Actually handle the throttled packets.
1473 tbf_queue_packet(session
[s
].tbf_in
, p
, l
);
1478 if (tun_write(p
, l
) < 0)
1480 STAT(tun_tx_errors
);
1481 LOG(0, s
, t
, "Error writing %d bytes to TUN device: %s (tunfd=%d, p=%p)\n",
1482 l
, strerror(errno
), tunfd
, p
);
1490 if (session
[s
].snoop_ip
&& session
[s
].snoop_port
)
1492 // Snooping this session
1493 snoop_send_packet(p
, l
, session
[s
].snoop_ip
, session
[s
].snoop_port
);
1496 increment_counter(&session
[s
].cin
, &session
[s
].cin_wrap
, l
);
1497 session
[s
].cin_delta
+= l
;
1500 sess_local
[s
].cin
+= l
;
1501 sess_local
[s
].pin
++;
1505 STAT(tun_tx_packets
);
1506 INC_STAT(tun_tx_bytes
, l
);
1510 // Helper routine for the TBF filters.
1511 // Used to send queued data in from the user.
1513 void send_ipin(sessionidt s
, uint8_t *buf
, int len
)
1515 LOG_HEX(5, "IP in throttled", buf
, len
);
1517 if (write(tunfd
, buf
, len
) < 0)
1519 STAT(tun_tx_errors
);
1520 LOG(0, 0, 0, "Error writing %d bytes to TUN device: %s (tunfd=%d, p=%p)\n",
1521 len
, strerror(errno
), tunfd
, buf
);
1529 if (session
[s
].snoop_ip
&& session
[s
].snoop_port
)
1531 // Snooping this session
1532 snoop_send_packet(buf
, len
, session
[s
].snoop_ip
, session
[s
].snoop_port
);
1535 // Increment packet counters
1536 increment_counter(&session
[s
].cin
, &session
[s
].cin_wrap
, len
);
1537 session
[s
].cin_delta
+= len
;
1540 sess_local
[s
].cin
+= len
;
1541 sess_local
[s
].pin
++;
1545 STAT(tun_tx_packets
);
1546 INC_STAT(tun_tx_bytes
, len
- 4);
1550 // Process CCP messages
1551 void processccp(sessionidt s
, tunnelidt t
, uint8_t *p
, uint16_t l
)
1553 uint8_t b
[MAXCONTROL
];
1558 LOG_HEX(5, "CCP", p
, l
);
1560 if (session
[s
].ppp
.phase
< Network
)
1562 LOG(2, s
, t
, "CCP %s ignored in %s phase\n", ppp_code(*p
), ppp_phase(session
[s
].ppp
.phase
));
1568 LOG(1, s
, t
, "Short CCP packet\n");
1569 STAT(tunnel_rx_errors
);
1572 LOG(3, s
, t
, "CCP: recv %s\n", ppp_code(*p
));
1573 if (*p
== ConfigAck
)
1575 switch (session
[s
].ppp
.ccp
)
1578 initialise_restart_count(s
, ccp
);
1579 change_state(s
, ccp
, AckReceived
);
1584 LOG(2, s
, t
, "CCP: ConfigAck in state %s? Sending ConfigReq\n", ppp_state(session
[s
].ppp
.ccp
));
1586 change_state(s
, ccp
, RequestSent
);
1590 LOG(3, s
, t
, "CCP: Opened\n");
1591 change_state(s
, ccp
, Opened
);
1595 LOG(2, s
, t
, "CCP: ignoring %s in state %s\n", ppp_code(*p
), ppp_state(session
[s
].ppp
.ccp
));
1598 else if (*p
== ConfigReq
)
1600 if (l
< 6) // accept no compression
1602 else // compression requested--reject
1605 q
= makeppp(b
, sizeof(b
), p
, l
, s
, t
, PPPCCP
);
1608 switch (session
[s
].ppp
.ccp
)
1611 q
= makeppp(b
, sizeof(b
), p
, 2, s
, t
, PPPCCP
);
1614 *((uint16_t *) (q
+ 2)) = htons(l
= 4);
1618 initialise_restart_count(s
, ccp
);
1620 if (*q
== ConfigAck
)
1621 change_state(s
, ccp
, AckSent
);
1623 change_state(s
, ccp
, RequestSent
);
1628 if (*q
== ConfigAck
)
1629 change_state(s
, ccp
, AckSent
);
1634 if (*q
== ConfigAck
)
1635 change_state(s
, ccp
, Opened
);
1640 initialise_restart_count(s
, ccp
);
1645 if (*q
== ConfigAck
)
1646 change_state(s
, ccp
, AckSent
);
1648 change_state(s
, ccp
, RequestSent
);
1653 LOG(2, s
, t
, "CCP: ignoring %s in state %s\n", ppp_code(*p
), ppp_state(session
[s
].ppp
.ccp
));
1657 LOG(3, s
, t
, "CCP: send %s\n", ppp_code(*q
));
1658 tunnelsend(b
, l
+ (q
- b
), t
);
1660 else if (*p
== TerminateReq
)
1663 q
= makeppp(b
, sizeof(b
), p
, l
, s
, t
, PPPCCP
);
1665 LOG(3, s
, t
, "CCP: send %s\n", ppp_code(*q
));
1666 tunnelsend(b
, l
+ (q
- b
), t
);
1667 change_state(s
, ccp
, Stopped
);
1672 int mru
= session
[s
].mru
;
1676 if (l
> mru
) l
= mru
;
1679 q
= makeppp(b
, sizeof(b
), p
, l
, s
, t
, PPPCCP
);
1682 LOG(2, s
, t
, "Unexpected CCP code %s\n", ppp_code(code
));
1683 LOG(3, s
, t
, "CCP: send %s\n", ppp_code(*q
));
1684 tunnelsend(b
, l
+ (q
- b
), t
);
1688 // send a CHAP challenge
1689 void sendchap(sessionidt s
, tunnelidt t
)
1691 uint8_t b
[MAXCONTROL
];
1700 LOG(1, s
, t
, "No RADIUS to send challenge\n");
1701 STAT(tunnel_tx_errors
);
1705 LOG(1, s
, t
, "Send CHAP challenge\n");
1707 radius
[r
].chap
= 1; // CHAP not PAP
1709 if (radius
[r
].state
!= RADIUSCHAP
)
1712 radius
[r
].state
= RADIUSCHAP
;
1713 radius
[r
].retry
= backoff(radius
[r
].try++);
1714 if (radius
[r
].try > 5)
1716 sessionshutdown(s
, "CHAP timeout.", 3, 0);
1717 STAT(tunnel_tx_errors
);
1720 q
= makeppp(b
, sizeof(b
), 0, 0, s
, t
, PPPCHAP
);
1723 *q
= 1; // challenge
1724 q
[1] = radius
[r
].id
; // ID
1725 q
[4] = 16; // value size (size of challenge)
1726 memcpy(q
+ 5, radius
[r
].auth
, 16); // challenge
1727 strcpy((char *) q
+ 21, hostname
); // our name
1728 *(uint16_t *) (q
+ 2) = htons(strlen(hostname
) + 21); // length
1729 tunnelsend(b
, strlen(hostname
) + 21 + (q
- b
), t
); // send it
1732 // fill in a L2TP message with a PPP frame,
1733 // copies existing PPP message and changes magic number if seen
1734 // returns start of PPP frame
1735 uint8_t *makeppp(uint8_t *b
, int size
, uint8_t *p
, int l
, sessionidt s
, tunnelidt t
, uint16_t mtype
)
1737 if (size
< 12) // Need more space than this!!
1739 static int backtrace_count
= 0;
1740 LOG(0, s
, t
, "makeppp buffer too small for L2TP header (size=%d)\n", size
);
1741 log_backtrace(backtrace_count
, 5)
1745 *(uint16_t *) (b
+ 0) = htons(0x0002); // L2TP with no options
1746 *(uint16_t *) (b
+ 2) = htons(tunnel
[t
].far
); // tunnel
1747 *(uint16_t *) (b
+ 4) = htons(session
[s
].far
); // session
1749 if (mtype
== PPPLCP
|| !(session
[s
].l2tp_flags
& SESSIONACFC
))
1751 *(uint16_t *) b
= htons(0xFF03); // HDLC header
1754 if (mtype
< 0x100 && session
[s
].l2tp_flags
& SESSIONPFC
)
1758 *(uint16_t *) b
= htons(mtype
);
1764 static int backtrace_count
= 0;
1765 LOG(2, s
, t
, "makeppp would overflow buffer (size=%d, header+payload=%d)\n", size
, l
+ 12);
1766 log_backtrace(backtrace_count
, 5)
1776 static int add_lcp_auth(uint8_t *b
, int size
, int authtype
)
1779 if ((authtype
== AUTHCHAP
&& size
< 5) || size
< 4)
1782 *b
++ = 3; // Authentication-Protocol
1783 if (authtype
== AUTHCHAP
)
1785 len
= *b
++ = 5; // length
1786 *(uint16_t *) b
= htons(PPPCHAP
); b
+= 2;
1789 else if (authtype
== AUTHPAP
)
1791 len
= *b
++ = 4; // length
1792 *(uint16_t *) b
= htons(PPPPAP
); b
+= 2;
1796 LOG(0, 0, 0, "add_lcp_auth called with unsupported auth type %d\n", authtype
);
1802 // Send initial LCP ConfigReq for MRU, authentication type and magic no
1803 void sendlcp(sessionidt s
, tunnelidt t
, int authtype
)
1805 uint8_t b
[500], *q
, *l
;
1807 if (!(q
= makeppp(b
, sizeof(b
), NULL
, 0, s
, t
, PPPLCP
)))
1810 LOG(3, s
, t
, "LCP: send ConfigReq%s%s%s\n",
1811 authtype
? " (" : "",
1812 authtype
? (authtype
== AUTHCHAP
? "CHAP" : "PAP") : "",
1813 authtype
? ")" : "");
1815 if (!session
[s
].mru
)
1816 session
[s
].mru
= DEFAULT_MRU
;
1820 *l
++ = (time_now
% 255) + 1; // ID
1822 l
+= 2; //Save space for length
1824 *l
++ = 1; *l
++ = 4; // Maximum-Receive-Unit (length 4)
1825 *(uint16_t *) l
= htons(session
[s
].mru
); l
+= 2;
1828 l
+= add_lcp_auth(l
, sizeof(b
) - (l
- b
), authtype
);
1830 *l
++ = 5; *l
++ = 6; // Magic-Number (length 6)
1831 *(uint32_t *) l
= htonl(session
[s
].magic
);
1834 *(uint16_t *)(q
+ 2) = htons(l
- q
); // Length
1836 LOG_HEX(5, "PPPLCP", q
, l
- q
);
1837 if (config
->debug
> 3) dumplcp(q
, l
- q
);
1839 tunnelsend(b
, (l
- b
), t
);
1842 // Send CCP request for no compression
1843 void sendccp(sessionidt s
, tunnelidt t
)
1847 if (!(q
= makeppp(b
, sizeof(b
), NULL
, 0, s
, t
, PPPCCP
)))
1850 LOG(3, s
, t
, "CCP: send ConfigReq (no compression)\n");
1853 *(uint8_t *)(q
+ 1) = (time_now
% 255) + 1; // ID
1854 *(uint16_t *)(q
+ 2) = htons(4); // Length
1856 LOG_HEX(5, "PPPCCP", q
, 4);
1857 tunnelsend(b
, (q
- b
) + 4 , t
);