0f6bba9d6272695baa993cf2d2062b3bd9e603e7
3 char const *cvs_id_ppp
= "$Id: ppp.c,v 1.94 2006-01-19 21:31:25 bodea Exp $";
11 #include "constants.h"
17 extern tunnelt
*tunnel
;
18 extern sessiont
*session
;
19 extern radiust
*radius
;
21 extern char hostname
[];
22 extern uint32_t eth_tx
;
23 extern time_t time_now
;
24 extern configt
*config
;
26 static int add_lcp_auth(uint8_t *b
, int size
, int authtype
);
28 // Process PAP messages
29 void processpap(sessionidt s
, tunnelidt t
, uint8_t *p
, uint16_t l
)
38 LOG_HEX(5, "PAP", p
, l
);
41 LOG(1, s
, t
, "Short PAP %u bytes\n", l
);
42 STAT(tunnel_rx_errors
);
43 sessionshutdown(s
, "Short PAP packet.", 3, 0);
47 if ((hl
= ntohs(*(uint16_t *) (p
+ 2))) > l
)
49 LOG(1, s
, t
, "Length mismatch PAP %u/%u\n", hl
, l
);
50 STAT(tunnel_rx_errors
);
51 sessionshutdown(s
, "PAP length mismatch.", 3, 0);
58 LOG(1, s
, t
, "Unexpected PAP code %d\n", *p
);
59 STAT(tunnel_rx_errors
);
60 sessionshutdown(s
, "Unexpected PAP code.", 3, 0);
64 if (session
[s
].ppp
.phase
!= Authenticate
)
66 LOG(2, s
, t
, "PAP ignored in %s phase\n", ppp_phase(session
[s
].ppp
.phase
));
73 user
[0] = pass
[0] = 0;
74 if (*b
&& *b
< sizeof(user
))
76 memcpy(user
, b
+ 1, *b
);
79 if (*b
&& *b
< sizeof(pass
))
81 memcpy(pass
, b
+ 1, *b
);
85 LOG(3, s
, t
, "PAP login %s/%s\n", user
, pass
);
88 if (session
[s
].ip
|| !(r
= radiusnew(s
)))
90 // respond now, either no RADIUS available or already authenticated
93 uint8_t *p
= makeppp(b
, sizeof(b
), 0, 0, s
, t
, PPPPAP
);
99 *p
= 3; // cant authorise
101 *(uint16_t *) (p
+ 2) = htons(5); // length
102 p
[4] = 0; // no message
103 tunnelsend(b
, 5 + (p
- b
), t
); // send it
107 LOG(3, s
, t
, "Already an IP allocated: %s (%d)\n",
108 fmtaddr(htonl(session
[s
].ip
), 0), session
[s
].ip_pool_index
);
112 LOG(1, s
, t
, "No RADIUS session available to authenticate session...\n");
113 sessionshutdown(s
, "No free RADIUS sessions.", 4, 0);
118 // Run PRE_AUTH plugins
119 struct param_pre_auth packet
= { &tunnel
[t
], &session
[s
], strdup(user
), strdup(pass
), PPPPAP
, 1 };
120 run_plugins(PLUGIN_PRE_AUTH
, &packet
);
121 if (!packet
.continue_auth
)
123 LOG(3, s
, t
, "A plugin rejected PRE_AUTH\n");
124 if (packet
.username
) free(packet
.username
);
125 if (packet
.password
) free(packet
.password
);
129 strncpy(session
[s
].user
, packet
.username
, sizeof(session
[s
].user
) - 1);
130 strncpy(radius
[r
].pass
, packet
.password
, sizeof(radius
[r
].pass
) - 1);
132 free(packet
.username
);
133 free(packet
.password
);
136 LOG(3, s
, t
, "Sending login for %s/%s to RADIUS\n", user
, pass
);
137 radiussend(r
, RADIUSAUTH
);
141 // Process CHAP messages
142 void processchap(sessionidt s
, tunnelidt t
, uint8_t *p
, uint16_t l
)
149 LOG_HEX(5, "CHAP", p
, l
);
153 LOG(1, s
, t
, "Short CHAP %u bytes\n", l
);
154 STAT(tunnel_rx_errors
);
155 sessionshutdown(s
, "Short CHAP packet.", 3, 0);
159 if ((hl
= ntohs(*(uint16_t *) (p
+ 2))) > l
)
161 LOG(1, s
, t
, "Length mismatch CHAP %u/%u\n", hl
, l
);
162 STAT(tunnel_rx_errors
);
163 sessionshutdown(s
, "CHAP length mismatch.", 3, 0);
170 LOG(1, s
, t
, "Unexpected CHAP response code %d\n", *p
);
171 STAT(tunnel_rx_errors
);
172 sessionshutdown(s
, "CHAP length mismatch.", 3, 0);
176 r
= sess_local
[s
].radius
;
179 LOG(3, s
, t
, "Unexpected CHAP message\n");
183 if (session
[s
].ppp
.phase
!= Authenticate
)
185 LOG(2, s
, t
, "CHAP ignored in %s phase\n", ppp_phase(session
[s
].ppp
.phase
));
189 if (p
[1] != radius
[r
].id
)
191 LOG(1, s
, t
, "Wrong CHAP response ID %d (should be %d) (%d)\n", p
[1], radius
[r
].id
, r
);
192 STAT(tunnel_rx_errors
);
193 sessionshutdown(s
, "Unexpected CHAP response ID.", 3, 0);
197 if (l
< 5 || p
[4] != 16)
199 LOG(1, s
, t
, "Bad CHAP response length %d\n", l
< 5 ? -1 : p
[4]);
200 STAT(tunnel_rx_errors
);
201 sessionshutdown(s
, "Bad CHAP response length.", 3, 0);
207 if (l
< 16 || l
- 16 >= sizeof(session
[s
].user
))
209 LOG(1, s
, t
, "CHAP user too long %d\n", l
- 16);
210 STAT(tunnel_rx_errors
);
211 sessionshutdown(s
, "CHAP username too long.", 3, 0);
215 // Run PRE_AUTH plugins
217 struct param_pre_auth packet
= { &tunnel
[t
], &session
[s
], NULL
, NULL
, PPPCHAP
, 1 };
219 packet
.password
= calloc(17, 1);
220 memcpy(packet
.password
, p
, 16);
225 packet
.username
= calloc(l
+ 1, 1);
226 memcpy(packet
.username
, p
, l
);
228 run_plugins(PLUGIN_PRE_AUTH
, &packet
);
229 if (!packet
.continue_auth
)
231 LOG(3, s
, t
, "A plugin rejected PRE_AUTH\n");
232 if (packet
.username
) free(packet
.username
);
233 if (packet
.password
) free(packet
.password
);
237 strncpy(session
[s
].user
, packet
.username
, sizeof(session
[s
].user
) - 1);
238 memcpy(radius
[r
].pass
, packet
.password
, 16);
240 free(packet
.username
);
241 free(packet
.password
);
245 LOG(3, s
, t
, "CHAP login %s\n", session
[s
].user
);
246 radiussend(r
, RADIUSAUTH
);
249 static void dumplcp(uint8_t *p
, int l
)
252 uint8_t *o
= (p
+ 4);
254 LOG_HEX(5, "PPP LCP Packet", p
, l
);
255 LOG(4, 0, 0, "PPP LCP Packet type %d (%s len %d)\n", *p
, ppp_code((int)*p
), ntohs( ((uint16_t *) p
)[1]) );
256 LOG(4, 0, 0, "Length: %d\n", l
);
257 if (*p
!= ConfigReq
&& *p
!= ConfigRej
&& *p
!= ConfigAck
)
266 LOG(4, 0, 0, " Option length is %d...\n", length
);
271 LOG(4, 0, 0, " Option type is 0...\n");
278 case 1: // Maximum-Receive-Unit
280 LOG(4, 0, 0, " %s %d\n", ppp_lcp_option(type
), ntohs(*(uint16_t *)(o
+ 2)));
282 LOG(4, 0, 0, " %s odd length %d\n", ppp_lcp_option(type
), length
);
284 case 2: // Async-Control-Character-Map
287 uint32_t asyncmap
= ntohl(*(uint32_t *)(o
+ 2));
288 LOG(4, 0, 0, " %s %x\n", ppp_lcp_option(type
), asyncmap
);
291 LOG(4, 0, 0, " %s odd length %d\n", ppp_lcp_option(type
), length
);
293 case 3: // Authentication-Protocol
296 int proto
= ntohs(*(uint16_t *)(o
+ 2));
297 LOG(4, 0, 0, " %s 0x%x (%s)\n", ppp_lcp_option(type
), proto
,
298 proto
== PPPPAP
? "PAP" : "UNSUPPORTED");
300 else if (length
== 5)
302 int proto
= ntohs(*(uint16_t *)(o
+ 2));
304 LOG(4, 0, 0, " %s 0x%x 0x%x (%s)\n", ppp_lcp_option(type
), proto
, algo
,
305 (proto
== PPPCHAP
&& algo
== 5) ? "CHAP MD5" : "UNSUPPORTED");
308 LOG(4, 0, 0, " %s odd length %d\n", ppp_lcp_option(type
), length
);
310 case 4: // Quality-Protocol
312 uint32_t qp
= ntohl(*(uint32_t *)(o
+ 2));
313 LOG(4, 0, 0, " %s %x\n", ppp_lcp_option(type
), qp
);
316 case 5: // Magic-Number
319 uint32_t magicno
= ntohl(*(uint32_t *)(o
+ 2));
320 LOG(4, 0, 0, " %s %x\n", ppp_lcp_option(type
), magicno
);
323 LOG(4, 0, 0, " %s odd length %d\n", ppp_lcp_option(type
), length
);
325 case 7: // Protocol-Field-Compression
326 case 8: // Address-And-Control-Field-Compression
327 LOG(4, 0, 0, " %s\n", ppp_lcp_option(type
));
330 LOG(2, 0, 0, " Unknown PPP LCP Option type %d\n", type
);
338 void lcp_open(sessionidt s
, tunnelidt t
)
340 // transition to Authentication or Network phase:
341 session
[s
].ppp
.phase
= sess_local
[s
].lcp_authtype
? Authenticate
: Network
;
343 LOG(3, s
, t
, "LCP: Opened, phase %s\n", ppp_phase(session
[s
].ppp
.phase
));
346 change_state(s
, lcp
, Opened
);
348 if (session
[s
].ppp
.phase
== Authenticate
)
350 if (sess_local
[s
].lcp_authtype
== AUTHCHAP
)
357 change_state(s
, ipcp
, RequestSent
);
358 // move to passive state for IPv6 (if configured), CCP
359 if (config
->ipv6_prefix
.s6_addr
[0])
360 change_state(s
, ipv6cp
, Stopped
);
362 change_state(s
, ipv6cp
, Closed
);
364 change_state(s
, ccp
, Stopped
);
368 static void lcp_restart(sessionidt s
)
370 session
[s
].ppp
.phase
= Establish
;
372 change_state(s
, ipcp
, Dead
);
373 change_state(s
, ipv6cp
, Dead
);
374 change_state(s
, ccp
, Dead
);
377 static uint8_t *ppp_conf_rej(sessionidt s
, uint8_t *buf
, size_t blen
, uint16_t mtype
,
378 uint8_t **response
, uint8_t *queued
, uint8_t *packet
, uint8_t *option
)
380 if (!*response
|| **response
!= ConfigRej
)
382 queued
= *response
= makeppp(buf
, blen
, packet
, 2, s
, session
[s
].tunnel
, mtype
);
390 if ((queued
- buf
+ option
[1]) > blen
)
392 LOG(2, s
, session
[s
].tunnel
, "PPP overflow for ConfigRej (proto %u, option %u).\n", mtype
, *option
);
396 memcpy(queued
, option
, option
[1]);
397 return queued
+ option
[1];
400 static uint8_t *ppp_conf_nak(sessionidt s
, uint8_t *buf
, size_t blen
, uint16_t mtype
,
401 uint8_t **response
, uint8_t *queued
, uint8_t *packet
, uint8_t *option
,
402 uint8_t *value
, size_t vlen
)
407 case PPPLCP
: nak_sent
= &sess_local
[s
].lcp
.nak_sent
; break;
408 case PPPIPCP
: nak_sent
= &sess_local
[s
].ipcp
.nak_sent
; break;
409 case PPPIPV6CP
: nak_sent
= &sess_local
[s
].ipv6cp
.nak_sent
; break;
410 default: return 0; // ?
413 if (*response
&& **response
!= ConfigNak
)
415 if (*nak_sent
< config
->ppp_max_failure
) // reject queued
418 return ppp_conf_rej(s
, buf
, blen
, mtype
, response
, 0, packet
, option
);
423 if (*nak_sent
>= config
->ppp_max_failure
)
424 return ppp_conf_rej(s
, buf
, blen
, mtype
, response
, 0, packet
, option
);
426 queued
= *response
= makeppp(buf
, blen
, packet
, 2, s
, session
[s
].tunnel
, mtype
);
435 if ((queued
- buf
+ vlen
+ 2) > blen
)
437 LOG(2, s
, session
[s
].tunnel
, "PPP overflow for ConfigNak (proto %u, option %u).\n", mtype
, *option
);
442 *queued
++ = vlen
+ 2;
443 memcpy(queued
, value
, vlen
);
444 return queued
+ vlen
;
447 static void ppp_code_rej(sessionidt s
, tunnelidt t
, uint16_t proto
,
448 char *pname
, uint8_t *p
, uint16_t l
, uint8_t *buf
, size_t size
)
451 int mru
= session
[s
].mru
;
452 if (mru
< MINMTU
) mru
= MINMTU
;
453 if (mru
> size
) mru
= size
;
456 if (l
> mru
) l
= mru
;
458 q
= makeppp(buf
, size
, 0, 0, s
, t
, proto
);
462 *(q
+ 1) = ++sess_local
[s
].lcp_ident
;
463 *(uint16_t *)(q
+ 2) = htons(l
);
464 memcpy(q
+ 4, p
, l
- 4);
466 LOG(2, s
, t
, "Unexpected %s code %s\n", pname
, ppp_code(*p
));
467 LOG(3, s
, t
, "%s: send %s\n", pname
, ppp_code(*q
));
468 if (config
->debug
> 3) dumplcp(q
, l
);
470 tunnelsend(buf
, l
+ (q
- buf
), t
);
473 // Process LCP messages
474 void processlcp(sessionidt s
, tunnelidt t
, uint8_t *p
, uint16_t l
)
482 LOG_HEX(5, "LCP", p
, l
);
485 LOG(1, s
, t
, "Short LCP %d bytes\n", l
);
486 STAT(tunnel_rx_errors
);
490 if ((hl
= ntohs(*(uint16_t *) (p
+ 2))) > l
)
492 LOG(1, s
, t
, "Length mismatch LCP %u/%u\n", hl
, l
);
493 STAT(tunnel_rx_errors
);
498 if (session
[s
].die
) // going down...
501 LOG((*p
== EchoReq
|| *p
== EchoReply
) ? 4 : 3, s
, t
,
502 "LCP: recv %s\n", ppp_code(*p
));
504 if (config
->debug
> 3) dumplcp(p
, l
);
509 uint8_t *o
= (p
+ 4);
517 if (length
== 0 || type
== 0 || x
< length
) break;
520 case 3: // Authentication-Protocol
522 int proto
= ntohs(*(uint16_t *)(o
+ 2));
525 else if (proto
== PPPCHAP
&& *(o
+ 4) == 5)
535 if (!session
[s
].ip
&& authtype
)
536 sess_local
[s
].lcp_authtype
= authtype
;
538 switch (session
[s
].ppp
.lcp
)
541 initialise_restart_count(s
, lcp
);
542 change_state(s
, lcp
, AckReceived
);
547 LOG(2, s
, t
, "LCP: ConfigAck in state %s? Sending ConfigReq\n", ppp_state(session
[s
].ppp
.lcp
));
548 if (session
[s
].ppp
.lcp
== Opened
)
552 change_state(s
, lcp
, RequestSent
);
560 LOG(2, s
, t
, "LCP: ignoring %s in state %s\n", ppp_code(*p
), ppp_state(session
[s
].ppp
.lcp
));
563 else if (*p
== ConfigReq
)
566 uint8_t *o
= (p
+ 4);
567 uint8_t *response
= 0;
568 static uint8_t asyncmap
[4] = { 0, 0, 0, 0 }; // all zero
569 static uint8_t authproto
[5];
576 if (length
== 0 || type
== 0 || x
< length
) break;
579 case 1: // Maximum-Receive-Unit
581 uint16_t mru
= ntohs(*(uint16_t *)(o
+ 2));
584 session
[s
].mru
= mru
;
585 cluster_send_session(s
);
589 LOG(3, s
, t
, " Remote requesting MRU of %u. Rejecting.\n", mru
);
591 q
= ppp_conf_nak(s
, b
, sizeof(b
), PPPLCP
, &response
, q
, p
, o
, (uint8_t *) &mru
, sizeof(mru
));
595 case 2: // Async-Control-Character-Map
596 if (!ntohl(*(uint32_t *)(o
+ 2))) // all bits zero is OK
599 LOG(3, s
, t
, " Remote requesting asyncmap. Rejecting.\n");
600 q
= ppp_conf_nak(s
, b
, sizeof(b
), PPPLCP
, &response
, q
, p
, o
, asyncmap
, sizeof(asyncmap
));
603 case 3: // Authentication-Protocol
605 int proto
= ntohs(*(uint16_t *)(o
+ 2));
606 char proto_name
[] = "0x0000";
611 if (config
->radius_authtypes
& AUTHPAP
)
613 sess_local
[s
].lcp_authtype
= AUTHPAP
;
617 strcpy(proto_name
, "PAP");
619 else if (proto
== PPPCHAP
)
621 if (config
->radius_authtypes
& AUTHCHAP
622 && *(o
+ 4) == 5) // MD5
624 sess_local
[s
].lcp_authtype
= AUTHCHAP
;
628 strcpy(proto_name
, "CHAP");
631 sprintf(proto_name
, "%#4.4x", proto
);
633 LOG(3, s
, t
, " Remote requesting %s authentication. Rejecting.\n", proto_name
);
635 alen
= add_lcp_auth(authproto
, sizeof(authproto
), config
->radius_authprefer
);
636 if (alen
< 2) break; // paranoia
638 q
= ppp_conf_nak(s
, b
, sizeof(b
), PPPLCP
, &response
, q
, p
, o
, authproto
+ 2, alen
- 2);
639 if (q
&& *response
== ConfigNak
&&
640 config
->radius_authtypes
!= config
->radius_authprefer
)
643 alen
= add_lcp_auth(authproto
, sizeof(authproto
), config
->radius_authtypes
& ~config
->radius_authprefer
);
645 q
= ppp_conf_nak(s
, b
, sizeof(b
), PPPLCP
, &response
, q
, p
, o
, authproto
+ 2, alen
- 2);
652 case 4: // Quality-Protocol
653 case 5: // Magic-Number
654 case 7: // Protocol-Field-Compression
655 case 8: // Address-And-Control-Field-Compression
658 default: // Reject any unknown options
659 LOG(3, s
, t
, " Rejecting unknown PPP LCP option %d\n", type
);
660 q
= ppp_conf_rej(s
, b
, sizeof(b
), PPPLCP
, &response
, q
, p
, o
);
668 l
= q
- response
; // LCP packet length
669 *((uint16_t *) (response
+ 2)) = htons(l
); // update header
673 // Send packet back as ConfigAck
674 response
= makeppp(b
, sizeof(b
), p
, l
, s
, t
, PPPLCP
);
675 if (!response
) return;
676 *response
= ConfigAck
;
679 switch (session
[s
].ppp
.lcp
)
682 response
= makeppp(b
, sizeof(b
), p
, 2, s
, t
, PPPLCP
);
683 if (!response
) return;
684 *response
= TerminateAck
;
685 *((uint16_t *) (response
+ 2)) = htons(l
= 4);
689 initialise_restart_count(s
, lcp
);
691 if (*response
== ConfigAck
)
692 change_state(s
, lcp
, AckSent
);
694 change_state(s
, lcp
, RequestSent
);
699 if (*response
== ConfigAck
)
700 change_state(s
, lcp
, AckSent
);
705 if (*response
== ConfigAck
)
716 if (*response
== ConfigAck
)
717 change_state(s
, lcp
, AckSent
);
719 change_state(s
, lcp
, RequestSent
);
724 LOG(2, s
, t
, "LCP: ignoring %s in state %s\n", ppp_code(*p
), ppp_state(session
[s
].ppp
.lcp
));
728 LOG(3, s
, t
, "LCP: send %s\n", ppp_code(*response
));
729 if (config
->debug
> 3) dumplcp(response
, l
);
731 tunnelsend(b
, l
+ (response
- b
), t
);
733 else if (*p
== ConfigNak
|| *p
== ConfigRej
)
736 uint8_t *o
= (p
+ 4);
744 if (length
== 0 || type
== 0 || x
< length
) break;
747 case 1: // Maximum-Receive-Unit
750 sess_local
[s
].ppp_mru
= ntohs(*(uint16_t *)(o
+ 2));
751 LOG(3, s
, t
, " Remote requested MRU of %u\n", sess_local
[s
].ppp_mru
);
755 sess_local
[s
].ppp_mru
= 0;
756 LOG(3, s
, t
, " Remote rejected MRU negotiation\n");
761 case 3: // Authentication-Protocol
767 int proto
= ntohs(*(uint16_t *)(o
+ 2));
770 authtype
= config
->radius_authtypes
& AUTHPAP
;
771 LOG(3, s
, t
, " Remote requested PAP authentication...%sing\n",
772 authtype
? "accept" : "reject");
774 else if (proto
== PPPCHAP
&& *(o
+ 4) == 5)
776 authtype
= config
->radius_authtypes
& AUTHCHAP
;
777 LOG(3, s
, t
, " Remote requested CHAP authentication...%sing\n",
778 authtype
? "accept" : "reject");
782 LOG(3, s
, t
, " Rejecting unsupported authentication %#4x\n",
788 LOG(2, s
, t
, "LCP: remote rejected auth negotiation\n");
789 authtype
= 0; // shutdown
794 case 5: // Magic-Number
797 session
[s
].magic
= ntohl(*(uint32_t *)(o
+ 2));
798 LOG(3, s
, t
, " Remote requested magic-no %x\n", session
[s
].magic
);
799 if (!session
[s
].magic
) session
[s
].magic
= time_now
; // Netgear DG814 sends zero??
800 cluster_send_session(s
);
803 // ConfigRej: fallthrough
806 LOG(2, s
, t
, "LCP: remote sent %s for type %u?\n", ppp_code(*p
), type
);
807 sessionshutdown(s
, "Unable to negotiate LCP.", 3, 0);
816 sessionshutdown(s
, "Unsupported authentication.", 3, 0);
821 sess_local
[s
].lcp_authtype
= authtype
;
823 switch (session
[s
].ppp
.lcp
)
828 uint8_t *response
= makeppp(b
, sizeof(b
), p
, 2, s
, t
, PPPLCP
);
829 if (!response
) return;
830 *response
= TerminateAck
;
831 *((uint16_t *) (response
+ 2)) = htons(l
= 4);
833 LOG(3, s
, t
, "LCP: send %s\n", ppp_code(*response
));
834 if (config
->debug
> 3) dumplcp(response
, l
);
836 tunnelsend(b
, l
+ (response
- b
), t
);
842 initialise_restart_count(s
, lcp
);
847 LOG(2, s
, t
, "LCP: ConfigNak in state %s? Sending ConfigReq\n", ppp_state(session
[s
].ppp
.lcp
));
857 LOG(2, s
, t
, "LCP: ignoring %s in state %s\n", ppp_code(*p
), ppp_state(session
[s
].ppp
.lcp
));
861 else if (*p
== TerminateReq
)
863 *p
= TerminateAck
; // close
864 q
= makeppp(b
, sizeof(b
), p
, l
, s
, t
, PPPLCP
);
867 LOG(3, s
, t
, "LCP: send %s\n", ppp_code(*q
));
868 if (config
->debug
> 3) dumplcp(q
, l
);
870 tunnelsend(b
, l
+ (q
- b
), t
); // send it
871 sessionshutdown(s
, "Remote end closed connection.", 3, 0);
873 else if (*p
== TerminateAck
)
875 sessionshutdown(s
, "Connection closed.", 3, 0);
877 else if (*p
== ProtocolRej
)
884 if (l
> 5 && !(proto
& 1))
891 if (proto
== PPPIPV6CP
)
893 LOG(3, s
, t
, "IPv6 rejected\n");
894 change_state(s
, ipv6cp
, Closed
);
898 LOG(3, s
, t
, "LCP protocol reject: 0x%04X\n", proto
);
901 else if (*p
== EchoReq
)
903 *p
= EchoReply
; // reply
904 *(uint32_t *) (p
+ 4) = htonl(session
[s
].magic
); // our magic number
905 q
= makeppp(b
, sizeof(b
), p
, l
, s
, t
, PPPLCP
);
908 LOG(4, s
, t
, "LCP: send %s\n", ppp_code(*q
));
909 if (config
->debug
> 3) dumplcp(q
, l
);
911 tunnelsend(b
, l
+ (q
- b
), t
); // send it
913 else if (*p
== EchoReply
)
915 // Ignore it, last_packet time is set earlier than this.
917 else if (*p
!= CodeRej
)
919 ppp_code_rej(s
, t
, PPPLCP
, "LCP", p
, l
, b
, sizeof(b
));
923 static void ipcp_open(sessionidt s
, tunnelidt t
)
925 LOG(3, s
, t
, "IPCP: Opened, session is now active\n");
927 change_state(s
, ipcp
, Opened
);
929 if (!(session
[s
].walled_garden
|| session
[s
].flags
& SESSION_STARTED
))
931 uint16_t r
= radiusnew(s
);
934 radiussend(r
, RADIUSSTART
); // send radius start
936 // don't send further Start records if IPCP is restarted
937 session
[s
].flags
|= SESSION_STARTED
;
938 cluster_send_session(s
);
942 // start IPv6 if configured and still in passive state
943 if (session
[s
].ppp
.ipv6cp
== Stopped
)
946 change_state(s
, ipv6cp
, RequestSent
);
950 // Process IPCP messages
951 void processipcp(sessionidt s
, tunnelidt t
, uint8_t *p
, uint16_t l
)
959 LOG_HEX(5, "IPCP", p
, l
);
962 LOG(1, s
, t
, "Short IPCP %d bytes\n", l
);
963 STAT(tunnel_rx_errors
);
967 if ((hl
= ntohs(*(uint16_t *) (p
+ 2))) > l
)
969 LOG(1, s
, t
, "Length mismatch IPCP %u/%u\n", hl
, l
);
970 STAT(tunnel_rx_errors
);
975 if (session
[s
].ppp
.phase
< Network
)
977 LOG(2, s
, t
, "IPCP %s ignored in %s phase\n", ppp_code(*p
), ppp_phase(session
[s
].ppp
.phase
));
981 LOG(3, s
, t
, "IPCP: recv %s\n", ppp_code(*p
));
985 switch (session
[s
].ppp
.ipcp
)
988 initialise_restart_count(s
, ipcp
);
989 change_state(s
, ipcp
, AckReceived
);
994 LOG(2, s
, t
, "IPCP: ConfigAck in state %s? Sending ConfigReq\n", ppp_state(session
[s
].ppp
.ipcp
));
996 change_state(s
, ipcp
, RequestSent
);
1004 LOG(2, s
, t
, "IPCP: ignoring %s in state %s\n", ppp_code(*p
), ppp_state(session
[s
].ppp
.ipcp
));
1007 else if (*p
== ConfigReq
)
1009 uint8_t *response
= 0;
1019 case 3: // ip address
1020 gotip
++; // seen address
1021 if (o
[1] != 6 || o
[1] > length
) return;
1023 addr
= htonl(session
[s
].ip
);
1024 if (memcmp(o
+ 2, &addr
, (sizeof addr
)))
1027 q
= ppp_conf_nak(s
, b
, sizeof(b
), PPPIPCP
, &response
, q
, p
, o
, (uint8_t *) &addr
, sizeof(addr
));
1028 if (!q
|| (q
!= oq
&& *response
== ConfigRej
))
1030 sessionshutdown(s
, "Can't negotiate IPCP.", 3, 0);
1037 case 129: // primary DNS
1038 if (o
[1] != 6 || o
[1] > length
) return;
1040 addr
= htonl(session
[s
].dns1
);
1041 if (memcmp(o
+ 2, &addr
, (sizeof addr
)))
1043 q
= ppp_conf_nak(s
, b
, sizeof(b
), PPPIPCP
, &response
, q
, p
, o
, (uint8_t *) &addr
, sizeof(addr
));
1049 case 131: // secondary DNS
1050 if (o
[1] != 6 || o
[1] > length
) return;
1052 addr
= htonl(session
[s
].dns2
);
1053 if (memcmp(o
+ 2, &addr
, sizeof(addr
)))
1055 q
= ppp_conf_nak(s
, b
, sizeof(b
), PPPIPCP
, &response
, q
, p
, o
, (uint8_t *) &addr
, sizeof(addr
));
1062 LOG(2, s
, t
, " Rejecting PPP IPCP Option type %d\n", *o
);
1063 q
= ppp_conf_rej(s
, b
, sizeof(b
), PPPIPCP
, &response
, q
, p
, o
);
1073 l
= q
- response
; // IPCP packet length
1074 *((uint16_t *) (response
+ 2)) = htons(l
); // update header
1078 // Send packet back as ConfigAck
1079 response
= makeppp(b
, sizeof(b
), p
, l
, s
, t
, PPPIPCP
);
1080 if (!response
) return;
1081 *response
= ConfigAck
;
1085 LOG(1, s
, t
, "No IP in IPCP request\n");
1086 STAT(tunnel_rx_errors
);
1090 switch (session
[s
].ppp
.ipcp
)
1093 response
= makeppp(b
, sizeof(b
), p
, 2, s
, t
, PPPIPCP
);
1094 if (!response
) return;
1095 *response
= TerminateAck
;
1096 *((uint16_t *) (response
+ 2)) = htons(l
= 4);
1100 initialise_restart_count(s
, ipcp
);
1102 if (*response
== ConfigAck
)
1103 change_state(s
, ipcp
, AckSent
);
1105 change_state(s
, ipcp
, RequestSent
);
1110 if (*response
== ConfigAck
)
1111 change_state(s
, ipcp
, AckSent
);
1116 if (*response
== ConfigAck
)
1122 initialise_restart_count(s
, ipcp
);
1127 if (*response
== ConfigAck
)
1128 change_state(s
, ipcp
, AckSent
);
1130 change_state(s
, ipcp
, RequestSent
);
1135 LOG(2, s
, t
, "IPCP: ignoring %s in state %s\n", ppp_code(*p
), ppp_state(session
[s
].ppp
.ipcp
));
1139 LOG(3, s
, t
, "IPCP: send %s\n", ppp_code(*response
));
1140 tunnelsend(b
, l
+ (response
- b
), t
);
1142 else if (*p
== TerminateReq
)
1145 q
= makeppp(b
, sizeof(b
), p
, l
, s
, t
, PPPIPCP
);
1147 LOG(3, s
, t
, "IPCP: send %s\n", ppp_code(*q
));
1148 tunnelsend(b
, l
+ (q
- b
), t
);
1149 change_state(s
, ipcp
, Stopped
);
1151 else if (*p
!= CodeRej
)
1153 ppp_code_rej(s
, t
, PPPIPCP
, "IPCP", p
, l
, b
, sizeof(b
));
1157 static void ipv6cp_open(sessionidt s
, tunnelidt t
)
1159 LOG(3, s
, t
, "IPV6CP: Opened\n");
1161 change_state(s
, ipv6cp
, Opened
);
1162 if (session
[s
].ipv6prefixlen
)
1163 route6set(s
, session
[s
].ipv6route
, session
[s
].ipv6prefixlen
, 1);
1165 // Send an initial RA (TODO: Should we send these regularly?)
1166 send_ipv6_ra(s
, t
, NULL
);
1169 // Process IPV6CP messages
1170 void processipv6cp(sessionidt s
, tunnelidt t
, uint8_t *p
, uint16_t l
)
1172 uint8_t b
[MAXETHER
];
1176 CSTAT(processipv6cp
);
1178 LOG_HEX(5, "IPV6CP", p
, l
);
1181 LOG(1, s
, t
, "Short IPV6CP %d bytes\n", l
);
1182 STAT(tunnel_rx_errors
);
1186 if ((hl
= ntohs(*(uint16_t *) (p
+ 2))) > l
)
1188 LOG(1, s
, t
, "Length mismatch IPV6CP %u/%u\n", hl
, l
);
1189 STAT(tunnel_rx_errors
);
1194 if (session
[s
].ppp
.phase
< Network
)
1196 LOG(2, s
, t
, "IPV6CP %s ignored in %s phase\n", ppp_code(*p
), ppp_phase(session
[s
].ppp
.phase
));
1200 LOG(3, s
, t
, "IPV6CP: recv %s\n", ppp_code(*p
));
1204 LOG(3, s
, t
, "IPV6CP: no IPv4 address (IPCP in state %s)\n", ppp_state(session
[s
].ppp
.ipcp
));
1205 return; // need IPCP to complete...
1208 if (*p
== ConfigAck
)
1210 switch (session
[s
].ppp
.ipv6cp
)
1213 initialise_restart_count(s
, ipv6cp
);
1214 change_state(s
, ipv6cp
, AckReceived
);
1219 LOG(2, s
, t
, "IPV6CP: ConfigAck in state %s? Sending ConfigReq\n", ppp_state(session
[s
].ppp
.ipv6cp
));
1221 change_state(s
, ipv6cp
, RequestSent
);
1229 LOG(2, s
, t
, "IPV6CP: ignoring %s in state %s\n", ppp_code(*p
), ppp_state(session
[s
].ppp
.ipv6cp
));
1232 else if (*p
== ConfigReq
)
1234 uint8_t *response
= 0;
1244 case 1: // interface identifier
1245 gotip
++; // seen address
1246 if (o
[1] != 10 || o
[1] > length
) return;
1248 *(uint32_t *) ident
= htonl(session
[s
].ip
);
1249 *(uint32_t *) (ident
+ 4) = 0;
1251 if (memcmp(o
+ 2, ident
, sizeof(ident
)))
1253 q
= ppp_conf_nak(s
, b
, sizeof(b
), PPPIPV6CP
, &response
, q
, p
, o
, ident
, sizeof(ident
));
1260 LOG(2, s
, t
, " Rejecting PPP IPV6CP Option type %d\n", *o
);
1261 q
= ppp_conf_rej(s
, b
, sizeof(b
), PPPIPV6CP
, &response
, q
, p
, o
);
1271 l
= q
- response
; // IPV6CP packet length
1272 *((uint16_t *) (response
+ 2)) = htons(l
); // update header
1276 // Send packet back as ConfigAck
1277 response
= makeppp(b
, sizeof(b
), p
, l
, s
, t
, PPPIPV6CP
);
1278 if (!response
) return;
1279 *response
= ConfigAck
;
1283 LOG(1, s
, t
, "No interface identifier in IPV6CP request\n");
1284 STAT(tunnel_rx_errors
);
1288 switch (session
[s
].ppp
.ipv6cp
)
1291 response
= makeppp(b
, sizeof(b
), p
, 2, s
, t
, PPPIPV6CP
);
1292 if (!response
) return;
1293 *response
= TerminateAck
;
1294 *((uint16_t *) (response
+ 2)) = htons(l
= 4);
1298 initialise_restart_count(s
, ipv6cp
);
1300 if (*response
== ConfigAck
)
1301 change_state(s
, ipv6cp
, AckSent
);
1303 change_state(s
, ipv6cp
, RequestSent
);
1308 if (*response
== ConfigAck
)
1309 change_state(s
, ipv6cp
, AckSent
);
1314 if (*response
== ConfigAck
)
1320 initialise_restart_count(s
, ipv6cp
);
1325 if (*response
== ConfigAck
)
1326 change_state(s
, ipv6cp
, AckSent
);
1328 change_state(s
, ipv6cp
, RequestSent
);
1333 LOG(2, s
, t
, "IPV6CP: ignoring %s in state %s\n", ppp_code(*p
), ppp_state(session
[s
].ppp
.ipv6cp
));
1337 LOG(3, s
, t
, "IPV6CP: send %s\n", ppp_code(*response
));
1338 tunnelsend(b
, l
+ (response
- b
), t
);
1340 else if (*p
== TerminateReq
)
1343 q
= makeppp(b
, sizeof(b
), p
, l
, s
, t
, PPPIPV6CP
);
1345 LOG(3, s
, t
, "IPV6CP: send %s\n", ppp_code(*q
));
1346 tunnelsend(b
, l
+ (q
- b
), t
);
1347 change_state(s
, ipv6cp
, Stopped
);
1349 else if (*p
!= CodeRej
)
1351 ppp_code_rej(s
, t
, PPPIPV6CP
, "IPV6CP", p
, l
, b
, sizeof(b
));
1355 // process IP packet received
1357 // This MUST be called with at least 4 byte behind 'p'.
1358 // (i.e. this routine writes to p[-4]).
1359 void processipin(sessionidt s
, tunnelidt t
, uint8_t *p
, uint16_t l
)
1365 LOG_HEX(5, "IP", p
, l
);
1369 LOG(1, s
, t
, "IP packet too short %d\n", l
);
1370 STAT(tunnel_rx_errors
);
1374 ip
= ntohl(*(uint32_t *)(p
+ 12));
1378 LOG(1, s
, t
, "IP packet too long %d\n", l
);
1379 STAT(tunnel_rx_errors
);
1383 if (session
[s
].ppp
.phase
!= Network
|| session
[s
].ppp
.ipcp
!= Opened
)
1386 // no spoof (do sessionbyip to handled statically routed subnets)
1387 if (ip
!= session
[s
].ip
&& sessionbyip(htonl(ip
)) != s
)
1389 LOG(5, s
, t
, "Dropping packet with spoofed IP %s\n", fmtaddr(htonl(ip
), 0));
1393 // run access-list if any
1394 if (session
[s
].filter_in
&& !ip_filter(p
, l
, session
[s
].filter_in
- 1))
1397 // adjust MSS on SYN and SYN,ACK packets with options
1398 if ((ntohs(*(uint16_t *) (p
+ 6)) & 0x1fff) == 0 && p
[9] == IPPROTO_TCP
) // first tcp fragment
1400 int ihl
= (p
[0] & 0xf) * 4; // length of IP header
1401 if (l
>= ihl
+ 20 && (p
[ihl
+ 13] & TCP_FLAG_SYN
) && ((p
[ihl
+ 12] >> 4) > 5))
1402 adjust_tcp_mss(s
, t
, p
, l
, p
+ ihl
);
1405 // Add on the tun header
1407 *(uint32_t *) p
= htonl(PKTIP
);
1410 // Are we throttled and a slave?
1411 if (session
[s
].tbf_in
&& !config
->cluster_iam_master
) {
1412 // Pass it to the master for handling.
1413 master_throttle_packet(session
[s
].tbf_in
, p
, l
);
1417 // Are we throttled and a master??
1418 if (session
[s
].tbf_in
&& config
->cluster_iam_master
) {
1419 // Actually handle the throttled packets.
1420 tbf_queue_packet(session
[s
].tbf_in
, p
, l
);
1425 if (tun_write(p
, l
) < 0)
1427 STAT(tun_tx_errors
);
1428 LOG(0, s
, t
, "Error writing %d bytes to TUN device: %s (tunfd=%d, p=%p)\n",
1429 l
, strerror(errno
), tunfd
, p
);
1437 if (session
[s
].snoop_ip
&& session
[s
].snoop_port
)
1439 // Snooping this session
1440 snoop_send_packet(p
, l
, session
[s
].snoop_ip
, session
[s
].snoop_port
);
1443 increment_counter(&session
[s
].cin
, &session
[s
].cin_wrap
, l
);
1444 session
[s
].cin_delta
+= l
;
1447 sess_local
[s
].cin
+= l
;
1448 sess_local
[s
].pin
++;
1452 STAT(tun_tx_packets
);
1453 INC_STAT(tun_tx_bytes
, l
);
1456 // process IPv6 packet received
1458 // This MUST be called with at least 4 byte behind 'p'.
1459 // (i.e. this routine writes to p[-4]).
1460 void processipv6in(sessionidt s
, tunnelidt t
, uint8_t *p
, uint16_t l
)
1465 CSTAT(processipv6in
);
1467 LOG_HEX(5, "IPv6", p
, l
);
1469 ip
= *(struct in6_addr
*) (p
+ 8);
1470 ipv4
= ntohl(*(uint32_t *)(p
+ 16));
1474 LOG(1, s
, t
, "IP packet too long %d\n", l
);
1475 STAT(tunnel_rx_errors
);
1479 if (session
[s
].ppp
.phase
!= Network
|| session
[s
].ppp
.ipv6cp
!= Opened
)
1483 if (ipv4
!= session
[s
].ip
&& memcmp(&config
->ipv6_prefix
, &ip
, 8) && sessionbyipv6(ip
) != s
)
1485 char str
[INET6_ADDRSTRLEN
];
1486 LOG(5, s
, t
, "Dropping packet with spoofed IP %s\n",
1487 inet_ntop(AF_INET6
, &ip
, str
, INET6_ADDRSTRLEN
));
1491 // Check if it's a Router Solicition message.
1492 if (*(p
+ 6) == 58 && *(p
+ 7) == 255 && *(p
+ 24) == 0xFF && *(p
+ 25) == 2 &&
1493 *(uint32_t *)(p
+ 26) == 0 && *(uint32_t *)(p
+ 30) == 0 &&
1494 *(uint32_t *)(p
+ 34) == 0 &&
1495 *(p
+ 38) == 0 && *(p
+ 39) == 2 && *(p
+ 40) == 133) {
1496 LOG(3, s
, t
, "Got IPv6 RS\n");
1497 send_ipv6_ra(s
, t
, &ip
);
1501 // Add on the tun header
1503 *(uint32_t *) p
= htonl(PKTIPV6
);
1506 // Are we throttled and a slave?
1507 if (session
[s
].tbf_in
&& !config
->cluster_iam_master
) {
1508 // Pass it to the master for handling.
1509 master_throttle_packet(session
[s
].tbf_in
, p
, l
);
1513 // Are we throttled and a master??
1514 if (session
[s
].tbf_in
&& config
->cluster_iam_master
) {
1515 // Actually handle the throttled packets.
1516 tbf_queue_packet(session
[s
].tbf_in
, p
, l
);
1521 if (tun_write(p
, l
) < 0)
1523 STAT(tun_tx_errors
);
1524 LOG(0, s
, t
, "Error writing %d bytes to TUN device: %s (tunfd=%d, p=%p)\n",
1525 l
, strerror(errno
), tunfd
, p
);
1533 if (session
[s
].snoop_ip
&& session
[s
].snoop_port
)
1535 // Snooping this session
1536 snoop_send_packet(p
, l
, session
[s
].snoop_ip
, session
[s
].snoop_port
);
1539 increment_counter(&session
[s
].cin
, &session
[s
].cin_wrap
, l
);
1540 session
[s
].cin_delta
+= l
;
1543 sess_local
[s
].cin
+= l
;
1544 sess_local
[s
].pin
++;
1548 STAT(tun_tx_packets
);
1549 INC_STAT(tun_tx_bytes
, l
);
1553 // Helper routine for the TBF filters.
1554 // Used to send queued data in from the user.
1556 void send_ipin(sessionidt s
, uint8_t *buf
, int len
)
1558 LOG_HEX(5, "IP in throttled", buf
, len
);
1560 if (write(tunfd
, buf
, len
) < 0)
1562 STAT(tun_tx_errors
);
1563 LOG(0, 0, 0, "Error writing %d bytes to TUN device: %s (tunfd=%d, p=%p)\n",
1564 len
, strerror(errno
), tunfd
, buf
);
1572 if (session
[s
].snoop_ip
&& session
[s
].snoop_port
)
1574 // Snooping this session
1575 snoop_send_packet(buf
, len
, session
[s
].snoop_ip
, session
[s
].snoop_port
);
1578 // Increment packet counters
1579 increment_counter(&session
[s
].cin
, &session
[s
].cin_wrap
, len
);
1580 session
[s
].cin_delta
+= len
;
1583 sess_local
[s
].cin
+= len
;
1584 sess_local
[s
].pin
++;
1588 STAT(tun_tx_packets
);
1589 INC_STAT(tun_tx_bytes
, len
- 4);
1593 // Process CCP messages
1594 void processccp(sessionidt s
, tunnelidt t
, uint8_t *p
, uint16_t l
)
1596 uint8_t b
[MAXETHER
];
1601 LOG_HEX(5, "CCP", p
, l
);
1603 if (session
[s
].ppp
.phase
< Network
)
1605 LOG(2, s
, t
, "CCP %s ignored in %s phase\n", ppp_code(*p
), ppp_phase(session
[s
].ppp
.phase
));
1611 LOG(1, s
, t
, "Short CCP packet\n");
1612 STAT(tunnel_rx_errors
);
1615 LOG(3, s
, t
, "CCP: recv %s\n", ppp_code(*p
));
1616 if (*p
== ConfigAck
)
1618 switch (session
[s
].ppp
.ccp
)
1621 initialise_restart_count(s
, ccp
);
1622 change_state(s
, ccp
, AckReceived
);
1627 LOG(2, s
, t
, "CCP: ConfigAck in state %s? Sending ConfigReq\n", ppp_state(session
[s
].ppp
.ccp
));
1629 change_state(s
, ccp
, RequestSent
);
1633 LOG(3, s
, t
, "CCP: Opened\n");
1634 change_state(s
, ccp
, Opened
);
1638 LOG(2, s
, t
, "CCP: ignoring %s in state %s\n", ppp_code(*p
), ppp_state(session
[s
].ppp
.ccp
));
1641 else if (*p
== ConfigReq
)
1643 if (l
< 6) // accept no compression
1645 else // compression requested--reject
1648 q
= makeppp(b
, sizeof(b
), p
, l
, s
, t
, PPPCCP
);
1651 switch (session
[s
].ppp
.ccp
)
1654 q
= makeppp(b
, sizeof(b
), p
, 2, s
, t
, PPPCCP
);
1657 *((uint16_t *) (q
+ 2)) = htons(l
= 4);
1661 initialise_restart_count(s
, ccp
);
1663 if (*q
== ConfigAck
)
1664 change_state(s
, ccp
, AckSent
);
1666 change_state(s
, ccp
, RequestSent
);
1671 if (*q
== ConfigAck
)
1672 change_state(s
, ccp
, AckSent
);
1677 if (*q
== ConfigAck
)
1678 change_state(s
, ccp
, Opened
);
1683 initialise_restart_count(s
, ccp
);
1688 if (*q
== ConfigAck
)
1689 change_state(s
, ccp
, AckSent
);
1691 change_state(s
, ccp
, RequestSent
);
1696 LOG(2, s
, t
, "CCP: ignoring %s in state %s\n", ppp_code(*p
), ppp_state(session
[s
].ppp
.ccp
));
1700 LOG(3, s
, t
, "CCP: send %s\n", ppp_code(*q
));
1701 tunnelsend(b
, l
+ (q
- b
), t
);
1703 else if (*p
== TerminateReq
)
1706 q
= makeppp(b
, sizeof(b
), p
, l
, s
, t
, PPPCCP
);
1708 LOG(3, s
, t
, "CCP: send %s\n", ppp_code(*q
));
1709 tunnelsend(b
, l
+ (q
- b
), t
);
1710 change_state(s
, ccp
, Stopped
);
1712 else if (*p
!= CodeRej
)
1714 ppp_code_rej(s
, t
, PPPCCP
, "CCP", p
, l
, b
, sizeof(b
));
1718 // send a CHAP challenge
1719 void sendchap(sessionidt s
, tunnelidt t
)
1721 uint8_t b
[MAXETHER
];
1730 LOG(1, s
, t
, "No RADIUS to send challenge\n");
1731 STAT(tunnel_tx_errors
);
1735 LOG(1, s
, t
, "Send CHAP challenge\n");
1737 radius
[r
].chap
= 1; // CHAP not PAP
1739 if (radius
[r
].state
!= RADIUSCHAP
)
1742 radius
[r
].state
= RADIUSCHAP
;
1743 radius
[r
].retry
= backoff(radius
[r
].try++);
1744 if (radius
[r
].try > 5)
1746 sessionshutdown(s
, "CHAP timeout.", 3, 0);
1747 STAT(tunnel_tx_errors
);
1750 q
= makeppp(b
, sizeof(b
), 0, 0, s
, t
, PPPCHAP
);
1753 *q
= 1; // challenge
1754 q
[1] = radius
[r
].id
; // ID
1755 q
[4] = 16; // value size (size of challenge)
1756 memcpy(q
+ 5, radius
[r
].auth
, 16); // challenge
1757 strcpy((char *) q
+ 21, hostname
); // our name
1758 *(uint16_t *) (q
+ 2) = htons(strlen(hostname
) + 21); // length
1759 tunnelsend(b
, strlen(hostname
) + 21 + (q
- b
), t
); // send it
1762 // fill in a L2TP message with a PPP frame,
1763 // copies existing PPP message and changes magic number if seen
1764 // returns start of PPP frame
1765 uint8_t *makeppp(uint8_t *b
, int size
, uint8_t *p
, int l
, sessionidt s
, tunnelidt t
, uint16_t mtype
)
1767 if (size
< 12) // Need more space than this!!
1769 LOG(0, s
, t
, "makeppp buffer too small for L2TP header (size=%d)\n", size
);
1773 *(uint16_t *) (b
+ 0) = htons(0x0002); // L2TP with no options
1774 *(uint16_t *) (b
+ 2) = htons(tunnel
[t
].far
); // tunnel
1775 *(uint16_t *) (b
+ 4) = htons(session
[s
].far
); // session
1777 if (mtype
== PPPLCP
|| !(session
[s
].flags
& SESSION_ACFC
))
1779 *(uint16_t *) b
= htons(0xFF03); // HDLC header
1782 if (mtype
< 0x100 && session
[s
].flags
& SESSION_PFC
)
1786 *(uint16_t *) b
= htons(mtype
);
1792 LOG(2, s
, t
, "makeppp would overflow buffer (size=%d, header+payload=%d)\n", size
, l
+ 12);
1802 static int add_lcp_auth(uint8_t *b
, int size
, int authtype
)
1805 if ((authtype
== AUTHCHAP
&& size
< 5) || size
< 4)
1808 *b
++ = 3; // Authentication-Protocol
1809 if (authtype
== AUTHCHAP
)
1811 len
= *b
++ = 5; // length
1812 *(uint16_t *) b
= htons(PPPCHAP
); b
+= 2;
1815 else if (authtype
== AUTHPAP
)
1817 len
= *b
++ = 4; // length
1818 *(uint16_t *) b
= htons(PPPPAP
); b
+= 2;
1822 LOG(0, 0, 0, "add_lcp_auth called with unsupported auth type %d\n", authtype
);
1828 // Send initial LCP ConfigReq for MRU, authentication type and magic no
1829 void sendlcp(sessionidt s
, tunnelidt t
)
1831 uint8_t b
[500], *q
, *l
;
1832 int authtype
= sess_local
[s
].lcp_authtype
;
1834 if (!(q
= makeppp(b
, sizeof(b
), NULL
, 0, s
, t
, PPPLCP
)))
1837 LOG(3, s
, t
, "LCP: send ConfigReq%s%s%s\n",
1838 authtype
? " (" : "",
1839 authtype
? (authtype
== AUTHCHAP
? "CHAP" : "PAP") : "",
1840 authtype
? ")" : "");
1844 *l
++ = ++sess_local
[s
].lcp_ident
; // ID
1846 l
+= 2; //Save space for length
1848 if (sess_local
[s
].ppp_mru
)
1850 *l
++ = 1; *l
++ = 4; // Maximum-Receive-Unit (length 4)
1851 *(uint16_t *) l
= htons(sess_local
[s
].ppp_mru
); l
+= 2;
1855 l
+= add_lcp_auth(l
, sizeof(b
) - (l
- b
), authtype
);
1857 *l
++ = 5; *l
++ = 6; // Magic-Number (length 6)
1858 *(uint32_t *) l
= htonl(session
[s
].magic
);
1861 *(uint16_t *)(q
+ 2) = htons(l
- q
); // Length
1863 LOG_HEX(5, "PPPLCP", q
, l
- q
);
1864 if (config
->debug
> 3) dumplcp(q
, l
- q
);
1866 tunnelsend(b
, (l
- b
), t
);
1867 restart_timer(s
, lcp
);
1870 // Send CCP request for no compression
1871 void sendccp(sessionidt s
, tunnelidt t
)
1875 if (!(q
= makeppp(b
, sizeof(b
), NULL
, 0, s
, t
, PPPCCP
)))
1878 LOG(3, s
, t
, "CCP: send ConfigReq (no compression)\n");
1881 *(q
+ 1) = ++sess_local
[s
].lcp_ident
; // ID
1882 *(uint16_t *)(q
+ 2) = htons(4); // Length
1884 LOG_HEX(5, "PPPCCP", q
, 4);
1885 tunnelsend(b
, (q
- b
) + 4 , t
);
1886 restart_timer(s
, ccp
);
1889 // Reject unknown/unconfigured protocols
1890 void protoreject(sessionidt s
, tunnelidt t
, uint8_t *p
, uint16_t l
, uint16_t proto
)
1893 uint8_t buf
[MAXETHER
];
1895 int mru
= session
[s
].mru
;
1896 if (mru
< MINMTU
) mru
= MINMTU
;
1897 if (mru
> sizeof(buf
)) mru
= sizeof(buf
);
1900 if (l
> mru
) l
= mru
;
1902 q
= makeppp(buf
, sizeof(buf
), 0, 0, s
, t
, PPPLCP
);
1906 *(q
+ 1) = ++sess_local
[s
].lcp_ident
;
1907 *(uint16_t *)(q
+ 2) = htons(l
);
1908 *(uint16_t *)(q
+ 4) = htons(proto
);
1909 memcpy(q
+ 6, p
, l
- 6);
1911 if (proto
== PPPIPV6CP
)
1912 LOG(3, s
, t
, "LCP: send ProtocolRej (IPV6CP: not configured)\n");
1914 LOG(2, s
, t
, "LCP: sent ProtocolRej (0x%04X: unsupported)\n", proto
);
1916 tunnelsend(buf
, l
+ (q
- buf
), t
);