09c3a36103e99f0beeb94f4d86a6ec044fab537c
3 char const *cvs_id_ppp
= "$Id: ppp.c,v 1.76 2005-08-29 03:21:14 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
);
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));
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
)
821 if (l
> 5 && !(proto
& 1))
828 if (proto
== PPPIPV6CP
)
830 LOG(3, s
, t
, "IPv6 rejected\n");
831 change_state(s
, ipv6cp
, Closed
);
835 LOG(3, s
, t
, "LCP protocol reject: 0x%04X\n", proto
);
838 else if (*p
== EchoReq
)
840 *p
= EchoReply
; // reply
841 *(uint32_t *) (p
+ 4) = htonl(session
[s
].magic
); // our magic number
842 q
= makeppp(b
, sizeof(b
), p
, l
, s
, t
, PPPLCP
);
845 LOG(4, s
, t
, "LCP: send %s\n", ppp_code(*q
));
846 if (config
->debug
> 3) dumplcp(q
, l
);
848 tunnelsend(b
, l
+ (q
- b
), t
); // send it
850 else if (*p
== EchoReply
)
852 // Ignore it, last_packet time is set earlier than this.
857 int mru
= session
[s
].mru
;
861 if (l
> mru
) l
= mru
;
864 q
= makeppp(b
, sizeof(b
), p
, l
, s
, t
, PPPLCP
);
867 LOG(2, s
, t
, "Unexpected LCP code %s\n", ppp_code(code
));
868 LOG(3, s
, t
, "LCP: send %s\n", ppp_code(*q
));
869 if (config
->debug
> 3) dumplcp(q
, l
);
871 tunnelsend(b
, l
+ (q
- b
), t
);
875 static void ipcp_open(sessionidt s
, tunnelidt t
)
877 LOG(3, s
, t
, "IPCP: Opened, session is now active\n");
879 change_state(s
, ipcp
, Opened
);
881 if (!session
[s
].walled_garden
)
883 uint16_t r
= radiusnew(s
);
885 radiussend(r
, RADIUSSTART
); // send radius start
888 // start IPv6 if configured and still in passive state
889 if (session
[s
].ppp
.ipv6cp
== Stopped
)
892 change_state(s
, ipv6cp
, RequestSent
);
896 // Process IPCP messages
897 void processipcp(sessionidt s
, tunnelidt t
, uint8_t *p
, uint16_t l
)
899 uint8_t b
[MAXCONTROL
];
905 LOG_HEX(5, "IPCP", p
, l
);
908 LOG(1, s
, t
, "Short IPCP %d bytes\n", l
);
909 STAT(tunnel_rx_errors
);
913 if ((hl
= ntohs(*(uint16_t *) (p
+ 2))) > l
)
915 LOG(1, s
, t
, "Length mismatch IPCP %u/%u\n", hl
, l
);
916 STAT(tunnel_rx_errors
);
921 if (session
[s
].ppp
.phase
< Network
)
923 LOG(2, s
, t
, "IPCP %s ignored in %s phase\n", ppp_code(*p
), ppp_phase(session
[s
].ppp
.phase
));
927 LOG(3, s
, t
, "IPCP: recv %s\n", ppp_code(*p
));
931 switch (session
[s
].ppp
.ipcp
)
934 initialise_restart_count(s
, ipcp
);
935 change_state(s
, ipcp
, AckReceived
);
940 LOG(2, s
, t
, "IPCP: ConfigAck in state %s? Sending ConfigReq\n", ppp_state(session
[s
].ppp
.ipcp
));
942 change_state(s
, ipcp
, RequestSent
);
950 LOG(2, s
, t
, "IPCP: ignoring %s in state %s\n", ppp_code(*p
), ppp_state(session
[s
].ppp
.ipcp
));
953 else if (*p
== ConfigReq
)
955 uint8_t *response
= 0;
965 case 3: // ip address
966 gotip
++; // seen address
967 if (o
[1] != 6 || o
[1] > length
) return;
969 addr
= htonl(session
[s
].ip
);
970 if (memcmp(o
+ 2, &addr
, (sizeof addr
)))
973 q
= ppp_nak(s
, b
, sizeof(b
), PPPIPCP
, &response
, q
, p
, o
, (uint8_t *) &addr
, sizeof(addr
));
974 if (!q
|| (q
!= oq
&& *response
== ConfigRej
))
976 sessionshutdown(s
, "Can't negotiate IPCP.", 3, 0);
983 case 129: // primary DNS
984 if (o
[1] != 6 || o
[1] > length
) return;
986 addr
= htonl(session
[s
].dns1
);
987 if (memcmp(o
+ 2, &addr
, (sizeof addr
)))
989 q
= ppp_nak(s
, b
, sizeof(b
), PPPIPCP
, &response
, q
, p
, o
, (uint8_t *) &addr
, sizeof(addr
));
995 case 131: // secondary DNS
996 if (o
[1] != 6 || o
[1] > length
) return;
998 addr
= htonl(session
[s
].dns1
);
999 if (memcmp(o
+ 2, &addr
, sizeof(addr
)))
1001 q
= ppp_nak(s
, b
, sizeof(b
), PPPIPCP
, &response
, q
, p
, o
, (uint8_t *) &addr
, sizeof(addr
));
1008 LOG(2, s
, t
, " Rejecting PPP IPCP Option type %d\n", *o
);
1009 q
= ppp_rej(s
, b
, sizeof(b
), PPPIPCP
, &response
, q
, p
, o
);
1019 l
= q
- response
; // IPCP packet length
1020 *((uint16_t *) (response
+ 2)) = htons(l
); // update header
1024 // Send packet back as ConfigAck
1025 response
= makeppp(b
, sizeof(b
), p
, l
, s
, t
, PPPIPCP
);
1026 if (!response
) return;
1027 *response
= ConfigAck
;
1031 LOG(1, s
, t
, "No IP in IPCP request\n");
1032 STAT(tunnel_rx_errors
);
1036 switch (session
[s
].ppp
.ipcp
)
1039 response
= makeppp(b
, sizeof(b
), p
, 2, s
, t
, PPPIPCP
);
1040 if (!response
) return;
1041 *response
= TerminateAck
;
1042 *((uint16_t *) (response
+ 2)) = htons(l
= 4);
1046 initialise_restart_count(s
, ipcp
);
1048 if (*response
== ConfigAck
)
1049 change_state(s
, ipcp
, AckSent
);
1051 change_state(s
, ipcp
, RequestSent
);
1056 if (*response
== ConfigAck
)
1057 change_state(s
, ipcp
, AckSent
);
1062 if (*response
== ConfigAck
)
1068 initialise_restart_count(s
, ipcp
);
1073 if (*response
== ConfigAck
)
1074 change_state(s
, ipcp
, AckSent
);
1076 change_state(s
, ipcp
, RequestSent
);
1081 LOG(2, s
, t
, "IPCP: ignoring %s in state %s\n", ppp_code(*p
), ppp_state(session
[s
].ppp
.ipcp
));
1085 LOG(3, s
, t
, "IPCP: send %s\n", ppp_code(*response
));
1086 tunnelsend(b
, l
+ (response
- b
), t
);
1088 else if (*p
== TerminateReq
)
1091 q
= makeppp(b
, sizeof(b
), p
, l
, s
, t
, PPPIPCP
);
1093 LOG(3, s
, t
, "IPCP: send %s\n", ppp_code(*q
));
1094 tunnelsend(b
, l
+ (q
- b
), t
);
1095 change_state(s
, ipcp
, Stopped
);
1100 int mru
= session
[s
].mru
;
1104 if (l
> mru
) l
= mru
;
1107 q
= makeppp(b
, sizeof(b
), p
, l
, s
, t
, PPPIPCP
);
1110 LOG(2, s
, t
, "Unexpected IPCP code %s\n", ppp_code(code
));
1111 LOG(3, s
, t
, "IPCP: send %s\n", ppp_code(*q
));
1112 tunnelsend(b
, l
+ (q
- b
), t
);
1116 static void ipv6cp_open(sessionidt s
, tunnelidt t
)
1118 LOG(3, s
, t
, "IPV6CP: Opened\n");
1120 change_state(s
, ipv6cp
, Opened
);
1121 if (session
[s
].ipv6prefixlen
)
1122 route6set(s
, session
[s
].ipv6route
, session
[s
].ipv6prefixlen
, 1);
1124 // Send an initial RA (TODO: Should we send these regularly?)
1125 send_ipv6_ra(s
, t
, NULL
);
1128 // Process IPV6CP messages
1129 void processipv6cp(sessionidt s
, tunnelidt t
, uint8_t *p
, uint16_t l
)
1131 uint8_t b
[MAXCONTROL
];
1135 CSTAT(processipv6cp
);
1137 LOG_HEX(5, "IPV6CP", p
, l
);
1140 LOG(1, s
, t
, "Short IPV6CP %d bytes\n", l
);
1141 STAT(tunnel_rx_errors
);
1145 if ((hl
= ntohs(*(uint16_t *) (p
+ 2))) > l
)
1147 LOG(1, s
, t
, "Length mismatch IPV6CP %u/%u\n", hl
, l
);
1148 STAT(tunnel_rx_errors
);
1153 if (session
[s
].ppp
.phase
< Network
)
1155 LOG(2, s
, t
, "IPV6CP %s ignored in %s phase\n", ppp_code(*p
), ppp_phase(session
[s
].ppp
.phase
));
1159 LOG(3, s
, t
, "IPV6CP: recv %s\n", ppp_code(*p
));
1161 if (!config
->ipv6_prefix
.s6_addr
[0])
1163 LOG(2, s
, t
, "IPV6CP: %s rejected (not configured)\n", ppp_code(*p
));
1165 q
= makeppp(b
, sizeof(b
), p
, l
, s
, t
, PPPIPV6CP
);
1167 tunnelsend(b
, l
+ (q
- b
), t
);
1173 LOG(3, s
, t
, "IPV6CP: no IPv4 address (IPCP in state %s)\n", ppp_state(session
[s
].ppp
.ipcp
));
1174 return; // need IPCP to complete...
1177 if (*p
== ConfigAck
)
1179 switch (session
[s
].ppp
.ipv6cp
)
1182 initialise_restart_count(s
, ipv6cp
);
1183 change_state(s
, ipv6cp
, AckReceived
);
1188 LOG(2, s
, t
, "IPV6CP: ConfigAck in state %s? Sending ConfigReq\n", ppp_state(session
[s
].ppp
.ipv6cp
));
1190 change_state(s
, ipv6cp
, RequestSent
);
1198 LOG(2, s
, t
, "IPV6CP: ignoring %s in state %s\n", ppp_code(*p
), ppp_state(session
[s
].ppp
.ipv6cp
));
1201 else if (*p
== ConfigReq
)
1203 uint8_t *response
= 0;
1213 case 1: // interface identifier
1214 gotip
++; // seen address
1215 if (o
[1] != 10 || o
[1] > length
) return;
1217 *(uint32_t *) ident
= htonl(session
[s
].ip
);
1218 *(uint32_t *) (ident
+ 4) = 0;
1220 if (memcmp(o
+ 2, ident
, sizeof(ident
)))
1222 q
= ppp_nak(s
, b
, sizeof(b
), PPPIPV6CP
, &response
, q
, p
, o
, ident
, sizeof(ident
));
1229 LOG(2, s
, t
, " Rejecting PPP IPV6CP Option type %d\n", *o
);
1230 q
= ppp_rej(s
, b
, sizeof(b
), PPPIPV6CP
, &response
, q
, p
, o
);
1240 l
= q
- response
; // IPV6CP packet length
1241 *((uint16_t *) (response
+ 2)) = htons(l
); // update header
1245 // Send packet back as ConfigAck
1246 response
= makeppp(b
, sizeof(b
), p
, l
, s
, t
, PPPIPV6CP
);
1247 if (!response
) return;
1248 *response
= ConfigAck
;
1252 LOG(1, s
, t
, "No interface identifier in IPV6CP request\n");
1253 STAT(tunnel_rx_errors
);
1257 switch (session
[s
].ppp
.ipv6cp
)
1260 response
= makeppp(b
, sizeof(b
), p
, 2, s
, t
, PPPIPV6CP
);
1261 if (!response
) return;
1262 *response
= TerminateAck
;
1263 *((uint16_t *) (response
+ 2)) = htons(l
= 4);
1267 initialise_restart_count(s
, ipv6cp
);
1269 if (*response
== ConfigAck
)
1270 change_state(s
, ipv6cp
, AckSent
);
1272 change_state(s
, ipv6cp
, RequestSent
);
1277 if (*response
== ConfigAck
)
1278 change_state(s
, ipv6cp
, AckSent
);
1283 if (*response
== ConfigAck
)
1289 initialise_restart_count(s
, ipv6cp
);
1294 if (*response
== ConfigAck
)
1295 change_state(s
, ipv6cp
, AckSent
);
1297 change_state(s
, ipv6cp
, RequestSent
);
1302 LOG(2, s
, t
, "IPV6CP: ignoring %s in state %s\n", ppp_code(*p
), ppp_state(session
[s
].ppp
.ipv6cp
));
1306 LOG(3, s
, t
, "IPV6CP: send %s\n", ppp_code(*response
));
1307 tunnelsend(b
, l
+ (response
- b
), t
);
1309 else if (*p
== TerminateReq
)
1312 q
= makeppp(b
, sizeof(b
), p
, l
, s
, t
, PPPIPV6CP
);
1314 LOG(3, s
, t
, "IPV6CP: send %s\n", ppp_code(*q
));
1315 tunnelsend(b
, l
+ (q
- b
), t
);
1316 change_state(s
, ipv6cp
, Stopped
);
1321 int mru
= session
[s
].mru
;
1325 if (l
> mru
) l
= mru
;
1328 q
= makeppp(b
, sizeof(b
), p
, l
, s
, t
, PPPIPV6CP
);
1331 LOG(2, s
, t
, "Unexpected IPV6CP code %s\n", ppp_code(code
));
1332 LOG(3, s
, t
, "IPV6CP: send %s\n", ppp_code(*q
));
1333 tunnelsend(b
, l
+ (q
- b
), t
);
1337 // process IP packet received
1339 // This MUST be called with at least 4 byte behind 'p'.
1340 // (i.e. this routine writes to p[-4]).
1341 void processipin(sessionidt s
, tunnelidt t
, uint8_t *p
, uint16_t l
)
1347 LOG_HEX(5, "IP", p
, l
);
1349 ip
= ntohl(*(uint32_t *)(p
+ 12));
1353 LOG(1, s
, t
, "IP packet too long %d\n", l
);
1354 STAT(tunnel_rx_errors
);
1358 if (session
[s
].ppp
.phase
!= Network
|| session
[s
].ppp
.ipcp
!= Opened
)
1361 // no spoof (do sessionbyip to handled statically routed subnets)
1362 if (ip
!= session
[s
].ip
&& sessionbyip(htonl(ip
)) != s
)
1364 LOG(5, s
, t
, "Dropping packet with spoofed IP %s\n", fmtaddr(htonl(ip
), 0));
1368 // run access-list if any
1369 if (session
[s
].filter_in
&& !ip_filter(p
, l
, session
[s
].filter_in
- 1))
1372 // Add on the tun header
1374 *(uint32_t *) p
= htonl(PKTIP
);
1377 // Are we throttled and a slave?
1378 if (session
[s
].tbf_in
&& !config
->cluster_iam_master
) {
1379 // Pass it to the master for handling.
1380 master_throttle_packet(session
[s
].tbf_in
, p
, l
);
1384 // Are we throttled and a master??
1385 if (session
[s
].tbf_in
&& config
->cluster_iam_master
) {
1386 // Actually handle the throttled packets.
1387 tbf_queue_packet(session
[s
].tbf_in
, p
, l
);
1392 if (tun_write(p
, l
) < 0)
1394 STAT(tun_tx_errors
);
1395 LOG(0, s
, t
, "Error writing %d bytes to TUN device: %s (tunfd=%d, p=%p)\n",
1396 l
, strerror(errno
), tunfd
, p
);
1404 if (session
[s
].snoop_ip
&& session
[s
].snoop_port
)
1406 // Snooping this session
1407 snoop_send_packet(p
, l
, session
[s
].snoop_ip
, session
[s
].snoop_port
);
1410 increment_counter(&session
[s
].cin
, &session
[s
].cin_wrap
, l
);
1411 session
[s
].cin_delta
+= l
;
1414 sess_local
[s
].cin
+= l
;
1415 sess_local
[s
].pin
++;
1419 STAT(tun_tx_packets
);
1420 INC_STAT(tun_tx_bytes
, l
);
1423 // process IPv6 packet received
1425 // This MUST be called with at least 4 byte behind 'p'.
1426 // (i.e. this routine writes to p[-4]).
1427 void processipv6in(sessionidt s
, tunnelidt t
, uint8_t *p
, uint16_t l
)
1432 CSTAT(processipv6in
);
1434 LOG_HEX(5, "IPv6", p
, l
);
1436 ip
= *(struct in6_addr
*) (p
+ 8);
1437 ipv4
= ntohl(*(uint32_t *)(p
+ 16));
1441 LOG(1, s
, t
, "IP packet too long %d\n", l
);
1442 STAT(tunnel_rx_errors
);
1446 if (session
[s
].ppp
.phase
!= Network
|| session
[s
].ppp
.ipv6cp
!= Opened
)
1450 if (ipv4
!= session
[s
].ip
&& memcmp(&config
->ipv6_prefix
, &ip
, 8) && sessionbyipv6(ip
) != s
)
1452 char str
[INET6_ADDRSTRLEN
];
1453 LOG(5, s
, t
, "Dropping packet with spoofed IP %s\n",
1454 inet_ntop(AF_INET6
, &ip
, str
, INET6_ADDRSTRLEN
));
1458 // Check if it's a Router Solicition message.
1459 if (*(p
+ 6) == 58 && *(p
+ 7) == 255 && *(p
+ 24) == 0xFF && *(p
+ 25) == 2 &&
1460 *(uint32_t *)(p
+ 26) == 0 && *(uint32_t *)(p
+ 30) == 0 &&
1461 *(uint32_t *)(p
+ 34) == 0 &&
1462 *(p
+ 38) == 0 && *(p
+ 39) == 2 && *(p
+ 40) == 133) {
1463 LOG(3, s
, t
, "Got IPv6 RS\n");
1464 send_ipv6_ra(s
, t
, &ip
);
1468 // Add on the tun header
1470 *(uint32_t *) p
= htonl(PKTIPV6
);
1473 // Are we throttled and a slave?
1474 if (session
[s
].tbf_in
&& !config
->cluster_iam_master
) {
1475 // Pass it to the master for handling.
1476 master_throttle_packet(session
[s
].tbf_in
, p
, l
);
1480 // Are we throttled and a master??
1481 if (session
[s
].tbf_in
&& config
->cluster_iam_master
) {
1482 // Actually handle the throttled packets.
1483 tbf_queue_packet(session
[s
].tbf_in
, p
, l
);
1488 if (tun_write(p
, l
) < 0)
1490 STAT(tun_tx_errors
);
1491 LOG(0, s
, t
, "Error writing %d bytes to TUN device: %s (tunfd=%d, p=%p)\n",
1492 l
, strerror(errno
), tunfd
, p
);
1500 if (session
[s
].snoop_ip
&& session
[s
].snoop_port
)
1502 // Snooping this session
1503 snoop_send_packet(p
, l
, session
[s
].snoop_ip
, session
[s
].snoop_port
);
1506 increment_counter(&session
[s
].cin
, &session
[s
].cin_wrap
, l
);
1507 session
[s
].cin_delta
+= l
;
1510 sess_local
[s
].cin
+= l
;
1511 sess_local
[s
].pin
++;
1515 STAT(tun_tx_packets
);
1516 INC_STAT(tun_tx_bytes
, l
);
1520 // Helper routine for the TBF filters.
1521 // Used to send queued data in from the user.
1523 void send_ipin(sessionidt s
, uint8_t *buf
, int len
)
1525 LOG_HEX(5, "IP in throttled", buf
, len
);
1527 if (write(tunfd
, buf
, len
) < 0)
1529 STAT(tun_tx_errors
);
1530 LOG(0, 0, 0, "Error writing %d bytes to TUN device: %s (tunfd=%d, p=%p)\n",
1531 len
, strerror(errno
), tunfd
, buf
);
1539 if (session
[s
].snoop_ip
&& session
[s
].snoop_port
)
1541 // Snooping this session
1542 snoop_send_packet(buf
, len
, session
[s
].snoop_ip
, session
[s
].snoop_port
);
1545 // Increment packet counters
1546 increment_counter(&session
[s
].cin
, &session
[s
].cin_wrap
, len
);
1547 session
[s
].cin_delta
+= len
;
1550 sess_local
[s
].cin
+= len
;
1551 sess_local
[s
].pin
++;
1555 STAT(tun_tx_packets
);
1556 INC_STAT(tun_tx_bytes
, len
- 4);
1560 // Process CCP messages
1561 void processccp(sessionidt s
, tunnelidt t
, uint8_t *p
, uint16_t l
)
1563 uint8_t b
[MAXCONTROL
];
1568 LOG_HEX(5, "CCP", p
, l
);
1570 if (session
[s
].ppp
.phase
< Network
)
1572 LOG(2, s
, t
, "CCP %s ignored in %s phase\n", ppp_code(*p
), ppp_phase(session
[s
].ppp
.phase
));
1578 LOG(1, s
, t
, "Short CCP packet\n");
1579 STAT(tunnel_rx_errors
);
1582 LOG(3, s
, t
, "CCP: recv %s\n", ppp_code(*p
));
1583 if (*p
== ConfigAck
)
1585 switch (session
[s
].ppp
.ccp
)
1588 initialise_restart_count(s
, ccp
);
1589 change_state(s
, ccp
, AckReceived
);
1594 LOG(2, s
, t
, "CCP: ConfigAck in state %s? Sending ConfigReq\n", ppp_state(session
[s
].ppp
.ccp
));
1596 change_state(s
, ccp
, RequestSent
);
1600 LOG(3, s
, t
, "CCP: Opened\n");
1601 change_state(s
, ccp
, Opened
);
1605 LOG(2, s
, t
, "CCP: ignoring %s in state %s\n", ppp_code(*p
), ppp_state(session
[s
].ppp
.ccp
));
1608 else if (*p
== ConfigReq
)
1610 if (l
< 6) // accept no compression
1612 else // compression requested--reject
1615 q
= makeppp(b
, sizeof(b
), p
, l
, s
, t
, PPPCCP
);
1618 switch (session
[s
].ppp
.ccp
)
1621 q
= makeppp(b
, sizeof(b
), p
, 2, s
, t
, PPPCCP
);
1624 *((uint16_t *) (q
+ 2)) = htons(l
= 4);
1628 initialise_restart_count(s
, ccp
);
1630 if (*q
== ConfigAck
)
1631 change_state(s
, ccp
, AckSent
);
1633 change_state(s
, ccp
, RequestSent
);
1638 if (*q
== ConfigAck
)
1639 change_state(s
, ccp
, AckSent
);
1644 if (*q
== ConfigAck
)
1645 change_state(s
, ccp
, Opened
);
1650 initialise_restart_count(s
, ccp
);
1655 if (*q
== ConfigAck
)
1656 change_state(s
, ccp
, AckSent
);
1658 change_state(s
, ccp
, RequestSent
);
1663 LOG(2, s
, t
, "CCP: ignoring %s in state %s\n", ppp_code(*p
), ppp_state(session
[s
].ppp
.ccp
));
1667 LOG(3, s
, t
, "CCP: send %s\n", ppp_code(*q
));
1668 tunnelsend(b
, l
+ (q
- b
), t
);
1670 else if (*p
== TerminateReq
)
1673 q
= makeppp(b
, sizeof(b
), p
, l
, s
, t
, PPPCCP
);
1675 LOG(3, s
, t
, "CCP: send %s\n", ppp_code(*q
));
1676 tunnelsend(b
, l
+ (q
- b
), t
);
1677 change_state(s
, ccp
, Stopped
);
1682 int mru
= session
[s
].mru
;
1686 if (l
> mru
) l
= mru
;
1689 q
= makeppp(b
, sizeof(b
), p
, l
, s
, t
, PPPCCP
);
1692 LOG(2, s
, t
, "Unexpected CCP code %s\n", ppp_code(code
));
1693 LOG(3, s
, t
, "CCP: send %s\n", ppp_code(*q
));
1694 tunnelsend(b
, l
+ (q
- b
), t
);
1698 // send a CHAP challenge
1699 void sendchap(sessionidt s
, tunnelidt t
)
1701 uint8_t b
[MAXCONTROL
];
1710 LOG(1, s
, t
, "No RADIUS to send challenge\n");
1711 STAT(tunnel_tx_errors
);
1715 LOG(1, s
, t
, "Send CHAP challenge\n");
1717 radius
[r
].chap
= 1; // CHAP not PAP
1719 if (radius
[r
].state
!= RADIUSCHAP
)
1722 radius
[r
].state
= RADIUSCHAP
;
1723 radius
[r
].retry
= backoff(radius
[r
].try++);
1724 if (radius
[r
].try > 5)
1726 sessionshutdown(s
, "CHAP timeout.", 3, 0);
1727 STAT(tunnel_tx_errors
);
1730 q
= makeppp(b
, sizeof(b
), 0, 0, s
, t
, PPPCHAP
);
1733 *q
= 1; // challenge
1734 q
[1] = radius
[r
].id
; // ID
1735 q
[4] = 16; // value size (size of challenge)
1736 memcpy(q
+ 5, radius
[r
].auth
, 16); // challenge
1737 strcpy((char *) q
+ 21, hostname
); // our name
1738 *(uint16_t *) (q
+ 2) = htons(strlen(hostname
) + 21); // length
1739 tunnelsend(b
, strlen(hostname
) + 21 + (q
- b
), t
); // send it
1742 // fill in a L2TP message with a PPP frame,
1743 // copies existing PPP message and changes magic number if seen
1744 // returns start of PPP frame
1745 uint8_t *makeppp(uint8_t *b
, int size
, uint8_t *p
, int l
, sessionidt s
, tunnelidt t
, uint16_t mtype
)
1747 if (size
< 12) // Need more space than this!!
1749 static int backtrace_count
= 0;
1750 LOG(0, s
, t
, "makeppp buffer too small for L2TP header (size=%d)\n", size
);
1751 log_backtrace(backtrace_count
, 5)
1755 *(uint16_t *) (b
+ 0) = htons(0x0002); // L2TP with no options
1756 *(uint16_t *) (b
+ 2) = htons(tunnel
[t
].far
); // tunnel
1757 *(uint16_t *) (b
+ 4) = htons(session
[s
].far
); // session
1759 if (mtype
== PPPLCP
|| !(session
[s
].l2tp_flags
& SESSIONACFC
))
1761 *(uint16_t *) b
= htons(0xFF03); // HDLC header
1764 if (mtype
< 0x100 && session
[s
].l2tp_flags
& SESSIONPFC
)
1768 *(uint16_t *) b
= htons(mtype
);
1774 static int backtrace_count
= 0;
1775 LOG(2, s
, t
, "makeppp would overflow buffer (size=%d, header+payload=%d)\n", size
, l
+ 12);
1776 log_backtrace(backtrace_count
, 5)
1786 static int add_lcp_auth(uint8_t *b
, int size
, int authtype
)
1789 if ((authtype
== AUTHCHAP
&& size
< 5) || size
< 4)
1792 *b
++ = 3; // Authentication-Protocol
1793 if (authtype
== AUTHCHAP
)
1795 len
= *b
++ = 5; // length
1796 *(uint16_t *) b
= htons(PPPCHAP
); b
+= 2;
1799 else if (authtype
== AUTHPAP
)
1801 len
= *b
++ = 4; // length
1802 *(uint16_t *) b
= htons(PPPPAP
); b
+= 2;
1806 LOG(0, 0, 0, "add_lcp_auth called with unsupported auth type %d\n", authtype
);
1812 // Send initial LCP ConfigReq for MRU, authentication type and magic no
1813 void sendlcp(sessionidt s
, tunnelidt t
, int authtype
)
1815 uint8_t b
[500], *q
, *l
;
1817 if (!(q
= makeppp(b
, sizeof(b
), NULL
, 0, s
, t
, PPPLCP
)))
1820 LOG(3, s
, t
, "LCP: send ConfigReq%s%s%s\n",
1821 authtype
? " (" : "",
1822 authtype
? (authtype
== AUTHCHAP
? "CHAP" : "PAP") : "",
1823 authtype
? ")" : "");
1825 if (!session
[s
].mru
)
1826 session
[s
].mru
= DEFAULT_MRU
;
1830 *l
++ = (time_now
% 255) + 1; // ID
1832 l
+= 2; //Save space for length
1834 *l
++ = 1; *l
++ = 4; // Maximum-Receive-Unit (length 4)
1835 *(uint16_t *) l
= htons(session
[s
].mru
); l
+= 2;
1838 l
+= add_lcp_auth(l
, sizeof(b
) - (l
- b
), authtype
);
1840 *l
++ = 5; *l
++ = 6; // Magic-Number (length 6)
1841 *(uint32_t *) l
= htonl(session
[s
].magic
);
1844 *(uint16_t *)(q
+ 2) = htons(l
- q
); // Length
1846 LOG_HEX(5, "PPPLCP", q
, l
- q
);
1847 if (config
->debug
> 3) dumplcp(q
, l
- q
);
1849 tunnelsend(b
, (l
- b
), t
);
1852 // Send CCP request for no compression
1853 void sendccp(sessionidt s
, tunnelidt t
)
1857 if (!(q
= makeppp(b
, sizeof(b
), NULL
, 0, s
, t
, PPPCCP
)))
1860 LOG(3, s
, t
, "CCP: send ConfigReq (no compression)\n");
1863 *(uint8_t *)(q
+ 1) = (time_now
% 255) + 1; // ID
1864 *(uint16_t *)(q
+ 2) = htons(4); // Length
1866 LOG_HEX(5, "PPPCCP", q
, 4);
1867 tunnelsend(b
, (q
- b
) + 4 , t
);