3 char const *cvs_id_ppp
= "$Id: ppp.c,v 1.100 2006/04/27 09:53:50 bodea Exp $";
11 #include "constants.h"
17 extern tunnelt
*tunnel
;
18 extern bundlet
*bundle
;
19 extern fragmentationt
*frag
;
20 extern sessiont
*session
;
21 extern radiust
*radius
;
23 extern char hostname
[];
24 extern uint32_t eth_tx
;
25 extern time_t time_now
;
26 extern configt
*config
;
28 static int add_lcp_auth(uint8_t *b
, int size
, int authtype
);
29 static bundleidt
new_bundle(void);
30 static int epdiscmp(epdist
, epdist
);
31 static void setepdis(epdist
*, epdist
);
32 static void ipcp_open(sessionidt s
, tunnelidt t
);
34 // Process PAP messages
35 void processpap(sessionidt s
, tunnelidt t
, uint8_t *p
, uint16_t l
)
44 LOG_HEX(5, "PAP", p
, l
);
47 LOG(1, s
, t
, "Short PAP %u bytes\n", l
);
48 STAT(tunnel_rx_errors
);
49 sessionshutdown(s
, "Short PAP packet.", CDN_ADMIN_DISC
, TERM_USER_ERROR
);
53 if ((hl
= ntohs(*(uint16_t *) (p
+ 2))) > l
)
55 LOG(1, s
, t
, "Length mismatch PAP %u/%u\n", hl
, l
);
56 STAT(tunnel_rx_errors
);
57 sessionshutdown(s
, "PAP length mismatch.", CDN_ADMIN_DISC
, TERM_USER_ERROR
);
64 LOG(1, s
, t
, "Unexpected PAP code %d\n", *p
);
65 STAT(tunnel_rx_errors
);
66 sessionshutdown(s
, "Unexpected PAP code.", CDN_ADMIN_DISC
, TERM_USER_ERROR
);
70 if (session
[s
].ppp
.phase
!= Authenticate
)
72 LOG(2, s
, t
, "PAP ignored in %s phase\n", ppp_phase(session
[s
].ppp
.phase
));
79 user
[0] = pass
[0] = 0;
80 if (*b
&& *b
< sizeof(user
))
82 memcpy(user
, b
+ 1, *b
);
85 if (*b
&& *b
< sizeof(pass
))
87 memcpy(pass
, b
+ 1, *b
);
91 LOG(3, s
, t
, "PAP login %s/%s\n", user
, pass
);
94 if (session
[s
].ip
|| !(r
= radiusnew(s
)))
96 // respond now, either no RADIUS available or already authenticated
99 uint8_t *p
= makeppp(b
, sizeof(b
), 0, 0, s
, t
, PPPPAP
, 0, 0, 0);
105 *p
= 3; // cant authorise
107 *(uint16_t *) (p
+ 2) = htons(5); // length
108 p
[4] = 0; // no message
109 tunnelsend(b
, 5 + (p
- b
), t
); // send it
113 LOG(3, s
, t
, "Already an IP allocated: %s (%d)\n",
114 fmtaddr(htonl(session
[s
].ip
), 0), session
[s
].ip_pool_index
);
118 LOG(1, s
, t
, "No RADIUS session available to authenticate session...\n");
119 sessionshutdown(s
, "No free RADIUS sessions.", CDN_UNAVAILABLE
, TERM_SERVICE_UNAVAILABLE
);
124 // Run PRE_AUTH plugins
125 struct param_pre_auth packet
= { &tunnel
[t
], &session
[s
], strdup(user
), strdup(pass
), PPPPAP
, 1 };
126 run_plugins(PLUGIN_PRE_AUTH
, &packet
);
127 if (!packet
.continue_auth
)
129 LOG(3, s
, t
, "A plugin rejected PRE_AUTH\n");
130 if (packet
.username
) free(packet
.username
);
131 if (packet
.password
) free(packet
.password
);
135 strncpy(session
[s
].user
, packet
.username
, sizeof(session
[s
].user
) - 1);
136 strncpy(radius
[r
].pass
, packet
.password
, sizeof(radius
[r
].pass
) - 1);
138 free(packet
.username
);
139 free(packet
.password
);
142 LOG(3, s
, t
, "Sending login for %s/%s to RADIUS\n", user
, pass
);
143 radiussend(r
, RADIUSAUTH
);
147 // Process CHAP messages
148 void processchap(sessionidt s
, tunnelidt t
, uint8_t *p
, uint16_t l
)
155 LOG_HEX(5, "CHAP", p
, l
);
159 LOG(1, s
, t
, "Short CHAP %u bytes\n", l
);
160 STAT(tunnel_rx_errors
);
161 sessionshutdown(s
, "Short CHAP packet.", CDN_ADMIN_DISC
, TERM_USER_ERROR
);
165 if ((hl
= ntohs(*(uint16_t *) (p
+ 2))) > l
)
167 LOG(1, s
, t
, "Length mismatch CHAP %u/%u\n", hl
, l
);
168 STAT(tunnel_rx_errors
);
169 sessionshutdown(s
, "CHAP length mismatch.", CDN_ADMIN_DISC
, TERM_USER_ERROR
);
176 LOG(1, s
, t
, "Unexpected CHAP response code %d\n", *p
);
177 STAT(tunnel_rx_errors
);
178 sessionshutdown(s
, "CHAP length mismatch.", CDN_ADMIN_DISC
, TERM_USER_ERROR
);
182 r
= sess_local
[s
].radius
;
185 LOG(3, s
, t
, "Unexpected CHAP message\n");
189 if (session
[s
].ppp
.phase
!= Authenticate
)
191 LOG(2, s
, t
, "CHAP ignored in %s phase\n", ppp_phase(session
[s
].ppp
.phase
));
195 if (p
[1] != radius
[r
].id
)
197 LOG(1, s
, t
, "Wrong CHAP response ID %d (should be %d) (%d)\n", p
[1], radius
[r
].id
, r
);
198 STAT(tunnel_rx_errors
);
199 sessionshutdown(s
, "Unexpected CHAP response ID.", CDN_ADMIN_DISC
, TERM_USER_ERROR
);
203 if (l
< 5 || p
[4] != 16)
205 LOG(1, s
, t
, "Bad CHAP response length %d\n", l
< 5 ? -1 : p
[4]);
206 STAT(tunnel_rx_errors
);
207 sessionshutdown(s
, "Bad CHAP response length.", CDN_ADMIN_DISC
, TERM_USER_ERROR
);
213 if (l
< 16 || l
- 16 >= sizeof(session
[s
].user
))
215 LOG(1, s
, t
, "CHAP user too long %d\n", l
- 16);
216 STAT(tunnel_rx_errors
);
217 sessionshutdown(s
, "CHAP username too long.", CDN_ADMIN_DISC
, TERM_USER_ERROR
);
221 // Run PRE_AUTH plugins
223 struct param_pre_auth packet
= { &tunnel
[t
], &session
[s
], NULL
, NULL
, PPPCHAP
, 1 };
225 packet
.password
= calloc(17, 1);
226 memcpy(packet
.password
, p
, 16);
231 packet
.username
= calloc(l
+ 1, 1);
232 memcpy(packet
.username
, p
, l
);
234 run_plugins(PLUGIN_PRE_AUTH
, &packet
);
235 if (!packet
.continue_auth
)
237 LOG(3, s
, t
, "A plugin rejected PRE_AUTH\n");
238 if (packet
.username
) free(packet
.username
);
239 if (packet
.password
) free(packet
.password
);
243 strncpy(session
[s
].user
, packet
.username
, sizeof(session
[s
].user
) - 1);
244 memcpy(radius
[r
].pass
, packet
.password
, 16);
246 free(packet
.username
);
247 free(packet
.password
);
251 LOG(3, s
, t
, "CHAP login %s\n", session
[s
].user
);
252 radiussend(r
, RADIUSAUTH
);
255 static void dumplcp(uint8_t *p
, int l
)
258 uint8_t *o
= (p
+ 4);
260 LOG_HEX(5, "PPP LCP Packet", p
, l
);
261 LOG(4, 0, 0, "PPP LCP Packet type %d (%s len %d)\n", *p
, ppp_code((int)*p
), ntohs( ((uint16_t *) p
)[1]) );
262 LOG(4, 0, 0, "Length: %d\n", l
);
263 if (*p
!= ConfigReq
&& *p
!= ConfigRej
&& *p
!= ConfigAck
)
272 LOG(4, 0, 0, " Option length is %d...\n", length
);
277 LOG(4, 0, 0, " Option type is 0...\n");
284 case 1: // Maximum-Receive-Unit
286 LOG(4, 0, 0, " %s %d\n", ppp_lcp_option(type
), ntohs(*(uint16_t *)(o
+ 2)));
288 LOG(4, 0, 0, " %s odd length %d\n", ppp_lcp_option(type
), length
);
290 case 2: // Async-Control-Character-Map
293 uint32_t asyncmap
= ntohl(*(uint32_t *)(o
+ 2));
294 LOG(4, 0, 0, " %s %x\n", ppp_lcp_option(type
), asyncmap
);
297 LOG(4, 0, 0, " %s odd length %d\n", ppp_lcp_option(type
), length
);
299 case 3: // Authentication-Protocol
302 int proto
= ntohs(*(uint16_t *)(o
+ 2));
303 LOG(4, 0, 0, " %s 0x%x (%s)\n", ppp_lcp_option(type
), proto
,
304 proto
== PPPPAP
? "PAP" : "UNSUPPORTED");
306 else if (length
== 5)
308 int proto
= ntohs(*(uint16_t *)(o
+ 2));
310 LOG(4, 0, 0, " %s 0x%x 0x%x (%s)\n", ppp_lcp_option(type
), proto
, algo
,
311 (proto
== PPPCHAP
&& algo
== 5) ? "CHAP MD5" : "UNSUPPORTED");
314 LOG(4, 0, 0, " %s odd length %d\n", ppp_lcp_option(type
), length
);
316 case 4: // Quality-Protocol
318 uint32_t qp
= ntohl(*(uint32_t *)(o
+ 2));
319 LOG(4, 0, 0, " %s %x\n", ppp_lcp_option(type
), qp
);
322 case 5: // Magic-Number
325 uint32_t magicno
= ntohl(*(uint32_t *)(o
+ 2));
326 LOG(4, 0, 0, " %s %x\n", ppp_lcp_option(type
), magicno
);
329 LOG(4, 0, 0, " %s odd length %d\n", ppp_lcp_option(type
), length
);
331 case 7: // Protocol-Field-Compression
332 case 8: // Address-And-Control-Field-Compression
333 LOG(4, 0, 0, " %s\n", ppp_lcp_option(type
));
336 LOG(2, 0, 0, " Unknown PPP LCP Option type %d\n", type
);
344 void lcp_open(sessionidt s
, tunnelidt t
)
346 // transition to Authentication or Network phase:
347 session
[s
].ppp
.phase
= sess_local
[s
].lcp_authtype
? Authenticate
: Network
;
349 LOG(3, s
, t
, "LCP: Opened, phase %s\n", ppp_phase(session
[s
].ppp
.phase
));
352 change_state(s
, lcp
, Opened
);
354 if (session
[s
].ppp
.phase
== Authenticate
)
356 if (sess_local
[s
].lcp_authtype
== AUTHCHAP
)
361 if (session
[s
].bundle
&& bundle
[session
[s
].bundle
].num_of_links
> 1)
363 sessionidt first_ses
= bundle
[session
[s
].bundle
].members
[0];
364 LOG(3, s
, t
, "MPPP: Skipping IPCP negotiation for session:%d, first session of bundle is:%d\n", s
, first_ses
);
365 session
[s
].ip
= session
[first_ses
].ip
;
366 session
[s
].dns1
= session
[first_ses
].dns1
;
367 session
[s
].dns2
= session
[first_ses
].dns2
;
368 session
[s
].timeout
= session
[first_ses
].timeout
;
375 change_state(s
, ipcp
, RequestSent
);
376 // move to passive state for IPv6 (if configured), CCP
377 if (config
->ipv6_prefix
.s6_addr
[0])
378 change_state(s
, ipv6cp
, Stopped
);
380 change_state(s
, ipv6cp
, Closed
);
382 change_state(s
, ccp
, Stopped
);
387 static void lcp_restart(sessionidt s
)
389 session
[s
].ppp
.phase
= Establish
;
391 change_state(s
, ipcp
, Dead
);
392 change_state(s
, ipv6cp
, Dead
);
393 change_state(s
, ccp
, Dead
);
396 static uint8_t *ppp_conf_rej(sessionidt s
, uint8_t *buf
, size_t blen
, uint16_t mtype
,
397 uint8_t **response
, uint8_t *queued
, uint8_t *packet
, uint8_t *option
)
399 if (!*response
|| **response
!= ConfigRej
)
401 queued
= *response
= makeppp(buf
, blen
, packet
, 2, s
, session
[s
].tunnel
, mtype
, 0, 0, 0);
409 if ((queued
- buf
+ option
[1]) > blen
)
411 LOG(2, s
, session
[s
].tunnel
, "PPP overflow for ConfigRej (proto %u, option %u).\n", mtype
, *option
);
415 memcpy(queued
, option
, option
[1]);
416 return queued
+ option
[1];
419 static uint8_t *ppp_conf_nak(sessionidt s
, uint8_t *buf
, size_t blen
, uint16_t mtype
,
420 uint8_t **response
, uint8_t *queued
, uint8_t *packet
, uint8_t *option
,
421 uint8_t *value
, size_t vlen
)
426 case PPPLCP
: nak_sent
= &sess_local
[s
].lcp
.nak_sent
; break;
427 case PPPIPCP
: nak_sent
= &sess_local
[s
].ipcp
.nak_sent
; break;
428 case PPPIPV6CP
: nak_sent
= &sess_local
[s
].ipv6cp
.nak_sent
; break;
429 default: return 0; // ?
432 if (*response
&& **response
!= ConfigNak
)
434 if (*nak_sent
< config
->ppp_max_failure
) // reject queued
437 return ppp_conf_rej(s
, buf
, blen
, mtype
, response
, 0, packet
, option
);
442 if (*nak_sent
>= config
->ppp_max_failure
)
443 return ppp_conf_rej(s
, buf
, blen
, mtype
, response
, 0, packet
, option
);
445 queued
= *response
= makeppp(buf
, blen
, packet
, 2, s
, session
[s
].tunnel
, mtype
, 0, 0, 0);
454 if ((queued
- buf
+ vlen
+ 2) > blen
)
456 LOG(2, s
, session
[s
].tunnel
, "PPP overflow for ConfigNak (proto %u, option %u).\n", mtype
, *option
);
461 *queued
++ = vlen
+ 2;
462 memcpy(queued
, value
, vlen
);
463 return queued
+ vlen
;
466 static void ppp_code_rej(sessionidt s
, tunnelidt t
, uint16_t proto
,
467 char *pname
, uint8_t *p
, uint16_t l
, uint8_t *buf
, size_t size
)
470 int mru
= session
[s
].mru
;
471 if (mru
< MINMTU
) mru
= MINMTU
;
472 if (mru
> size
) mru
= size
;
475 if (l
> mru
) l
= mru
;
477 q
= makeppp(buf
, size
, 0, 0, s
, t
, proto
, 0, 0, 0);
481 *(q
+ 1) = ++sess_local
[s
].lcp_ident
;
482 *(uint16_t *)(q
+ 2) = htons(l
);
483 memcpy(q
+ 4, p
, l
- 4);
485 LOG(2, s
, t
, "Unexpected %s code %s\n", pname
, ppp_code(*p
));
486 LOG(3, s
, t
, "%s: send %s\n", pname
, ppp_code(*q
));
487 if (config
->debug
> 3) dumplcp(q
, l
);
489 tunnelsend(buf
, l
+ (q
- buf
), t
);
492 // Process LCP messages
493 void processlcp(sessionidt s
, tunnelidt t
, uint8_t *p
, uint16_t l
)
501 LOG_HEX(5, "LCP", p
, l
);
504 LOG(1, s
, t
, "Short LCP %d bytes\n", l
);
505 STAT(tunnel_rx_errors
);
509 if ((hl
= ntohs(*(uint16_t *) (p
+ 2))) > l
)
511 LOG(1, s
, t
, "Length mismatch LCP %u/%u\n", hl
, l
);
512 STAT(tunnel_rx_errors
);
517 if (session
[s
].die
) // going down...
520 LOG((*p
== EchoReq
|| *p
== EchoReply
) ? 4 : 3, s
, t
,
521 "LCP: recv %s\n", ppp_code(*p
));
523 if (config
->debug
> 3) dumplcp(p
, l
);
528 uint8_t *o
= (p
+ 4);
536 if (length
== 0 || type
== 0 || x
< length
) break;
539 case 3: // Authentication-Protocol
541 int proto
= ntohs(*(uint16_t *)(o
+ 2));
544 else if (proto
== PPPCHAP
&& *(o
+ 4) == 5)
554 if (!session
[s
].ip
&& authtype
)
555 sess_local
[s
].lcp_authtype
= authtype
;
557 switch (session
[s
].ppp
.lcp
)
560 initialise_restart_count(s
, lcp
);
561 change_state(s
, lcp
, AckReceived
);
566 LOG(2, s
, t
, "LCP: ConfigAck in state %s? Sending ConfigReq\n", ppp_state(session
[s
].ppp
.lcp
));
567 if (session
[s
].ppp
.lcp
== Opened
)
571 change_state(s
, lcp
, RequestSent
);
579 LOG(2, s
, t
, "LCP: ignoring %s in state %s\n", ppp_code(*p
), ppp_state(session
[s
].ppp
.lcp
));
582 else if (*p
== ConfigReq
)
585 uint8_t *o
= (p
+ 4);
586 uint8_t *response
= 0;
587 static uint8_t asyncmap
[4] = { 0, 0, 0, 0 }; // all zero
588 static uint8_t authproto
[5];
596 if (length
== 0 || type
== 0 || x
< length
) break;
599 case 1: // Maximum-Receive-Unit
601 uint16_t mru
= ntohs(*(uint16_t *)(o
+ 2));
604 session
[s
].mru
= mru
;
609 LOG(3, s
, t
, " Remote requesting MRU of %u. Rejecting.\n", mru
);
611 q
= ppp_conf_nak(s
, b
, sizeof(b
), PPPLCP
, &response
, q
, p
, o
, (uint8_t *) &mru
, sizeof(mru
));
615 case 2: // Async-Control-Character-Map
616 if (!ntohl(*(uint32_t *)(o
+ 2))) // all bits zero is OK
619 LOG(3, s
, t
, " Remote requesting asyncmap. Rejecting.\n");
620 q
= ppp_conf_nak(s
, b
, sizeof(b
), PPPLCP
, &response
, q
, p
, o
, asyncmap
, sizeof(asyncmap
));
623 case 3: // Authentication-Protocol
625 int proto
= ntohs(*(uint16_t *)(o
+ 2));
626 char proto_name
[] = "0x0000";
631 if (config
->radius_authtypes
& AUTHPAP
)
633 sess_local
[s
].lcp_authtype
= AUTHPAP
;
637 strcpy(proto_name
, "PAP");
639 else if (proto
== PPPCHAP
)
641 if (config
->radius_authtypes
& AUTHCHAP
642 && *(o
+ 4) == 5) // MD5
644 sess_local
[s
].lcp_authtype
= AUTHCHAP
;
648 strcpy(proto_name
, "CHAP");
651 sprintf(proto_name
, "%#4.4x", proto
);
653 LOG(3, s
, t
, " Remote requesting %s authentication. Rejecting.\n", proto_name
);
655 alen
= add_lcp_auth(authproto
, sizeof(authproto
), config
->radius_authprefer
);
656 if (alen
< 2) break; // paranoia
658 q
= ppp_conf_nak(s
, b
, sizeof(b
), PPPLCP
, &response
, q
, p
, o
, authproto
+ 2, alen
- 2);
659 if (q
&& *response
== ConfigNak
&&
660 config
->radius_authtypes
!= config
->radius_authprefer
)
663 alen
= add_lcp_auth(authproto
, sizeof(authproto
), config
->radius_authtypes
& ~config
->radius_authprefer
);
665 q
= ppp_conf_nak(s
, b
, sizeof(b
), PPPLCP
, &response
, q
, p
, o
, authproto
+ 2, alen
- 2);
672 case 4: // Quality-Protocol
673 case 5: // Magic-Number
674 case 7: // Protocol-Field-Compression
675 case 8: // Address-And-Control-Field-Compression
678 case 17: // Multilink Max-Receive-Reconstructed-Unit
680 uint16_t mrru
= ntohs(*(uint16_t *)(o
+ 2));
681 session
[s
].mrru
= mrru
;
683 LOG(3, s
, t
, " Received PPP LCP option MRRU: %d\n",mrru
);
687 case 18: // Multilink Short Sequence Number Header Format
691 LOG(3, s
, t
, " Received PPP LCP option MSSN format\n");
695 case 19: // Multilink Endpoint Discriminator
697 uint8_t epdis_class
= o
[2];
700 session
[s
].epdis
.addr_class
= epdis_class
;
701 session
[s
].epdis
.length
= length
- 3;
702 if (session
[s
].epdis
.length
> 20)
704 LOG(1, s
, t
, "Error: received EndDis Address Length more than 20: %d\n", session
[s
].epdis
.length
);
705 session
[s
].epdis
.length
= 20;
708 for (addr
= 0; addr
< session
[s
].epdis
.length
; addr
++)
709 session
[s
].epdis
.address
[addr
] = o
[3+addr
];
716 LOG(3, s
, t
, " Received PPP LCP option Multilink EndDis Local Address Class: %d\n",epdis_class
);
719 LOG(3, s
, t
, " Received PPP LCP option Multilink EndDis IP Address Class: %d\n",epdis_class
);
722 LOG(3, s
, t
, " Received PPP LCP option Multilink EndDis IEEE MAC Address Class: %d\n",epdis_class
);
725 LOG(3, s
, t
, " Received PPP LCP option Multilink EndDis PPP Magic No Class: %d\n",epdis_class
);
728 LOG(3, s
, t
, " Received PPP LCP option Multilink EndDis PSND No Class: %d\n",epdis_class
);
731 LOG(3, s
, t
, " Received PPP LCP option Multilink EndDis NULL Class %d\n",epdis_class
);
736 default: // Reject any unknown options
737 LOG(3, s
, t
, " Rejecting unknown PPP LCP option %d\n", type
);
738 q
= ppp_conf_rej(s
, b
, sizeof(b
), PPPLCP
, &response
, q
, p
, o
);
745 cluster_send_session(s
);
749 l
= q
- response
; // LCP packet length
750 *((uint16_t *) (response
+ 2)) = htons(l
); // update header
754 // Send packet back as ConfigAck
755 response
= makeppp(b
, sizeof(b
), p
, l
, s
, t
, PPPLCP
, 0, 0, 0);
756 if (!response
) return;
757 *response
= ConfigAck
;
760 switch (session
[s
].ppp
.lcp
)
763 response
= makeppp(b
, sizeof(b
), p
, 2, s
, t
, PPPLCP
, 0, 0, 0);
764 if (!response
) return;
765 *response
= TerminateAck
;
766 *((uint16_t *) (response
+ 2)) = htons(l
= 4);
770 initialise_restart_count(s
, lcp
);
772 if (*response
== ConfigAck
)
773 change_state(s
, lcp
, AckSent
);
775 change_state(s
, lcp
, RequestSent
);
780 if (*response
== ConfigAck
)
781 change_state(s
, lcp
, AckSent
);
786 if (*response
== ConfigAck
)
797 if (*response
== ConfigAck
)
798 change_state(s
, lcp
, AckSent
);
800 change_state(s
, lcp
, RequestSent
);
805 LOG(2, s
, t
, "LCP: ignoring %s in state %s\n", ppp_code(*p
), ppp_state(session
[s
].ppp
.lcp
));
809 LOG(3, s
, t
, "LCP: send %s\n", ppp_code(*response
));
810 if (config
->debug
> 3) dumplcp(response
, l
);
812 tunnelsend(b
, l
+ (response
- b
), t
);
814 else if (*p
== ConfigNak
|| *p
== ConfigRej
)
817 uint8_t *o
= (p
+ 4);
825 if (length
== 0 || type
== 0 || x
< length
) break;
828 case 1: // Maximum-Receive-Unit
831 if (length
< 4) break;
832 sess_local
[s
].ppp_mru
= ntohs(*(uint16_t *)(o
+ 2));
833 LOG(3, s
, t
, " Remote requested MRU of %u\n", sess_local
[s
].ppp_mru
);
837 sess_local
[s
].ppp_mru
= 0;
838 LOG(3, s
, t
, " Remote rejected MRU negotiation\n");
843 case 3: // Authentication-Protocol
851 if (length
< 4) break;
852 proto
= ntohs(*(uint16_t *)(o
+ 2));
856 authtype
= config
->radius_authtypes
& AUTHPAP
;
857 LOG(3, s
, t
, " Remote requested PAP authentication...%sing\n",
858 authtype
? "accept" : "reject");
860 else if (proto
== PPPCHAP
&& length
> 4 && *(o
+ 4) == 5)
862 authtype
= config
->radius_authtypes
& AUTHCHAP
;
863 LOG(3, s
, t
, " Remote requested CHAP authentication...%sing\n",
864 authtype
? "accept" : "reject");
868 LOG(3, s
, t
, " Rejecting unsupported authentication %#4x\n",
874 LOG(2, s
, t
, "LCP: remote rejected auth negotiation\n");
875 authtype
= 0; // shutdown
880 case 5: // Magic-Number
881 session
[s
].magic
= 0;
884 if (length
< 6) break;
885 session
[s
].magic
= ntohl(*(uint32_t *)(o
+ 2));
888 if (session
[s
].magic
)
889 LOG(3, s
, t
, " Remote requested magic-no %x\n", session
[s
].magic
);
891 LOG(3, s
, t
, " Remote rejected magic-no\n");
893 cluster_send_session(s
);
896 case 17: // Multilink Max-Receive-Reconstructed-Unit
900 sess_local
[s
].mp_mrru
= ntohs(*(uint16_t *)(o
+ 2));
901 LOG(3, s
, t
, " Remote requested MRRU of %u\n", sess_local
[s
].mp_mrru
);
905 sess_local
[s
].mp_mrru
= 0;
906 LOG(3, s
, t
, " Remote rejected MRRU negotiation\n");
911 case 18: // Multilink Short Sequence Number Header Format
915 sess_local
[s
].mp_mssf
= 0;
916 LOG(3, s
, t
, " Remote requested Naked mssf\n");
920 sess_local
[s
].mp_mssf
= 0;
921 LOG(3, s
, t
, " Remote rejected mssf\n");
926 case 19: // Multilink Endpoint Discriminator
930 LOG(2, s
, t
, " Remote should not configNak Endpoint Dis!\n");
934 sess_local
[s
].mp_epdis
= 0;
935 LOG(3, s
, t
, " Remote rejected Endpoint Discriminator\n");
941 LOG(2, s
, t
, "LCP: remote sent %s for type %u?\n", ppp_code(*p
), type
);
942 sessionshutdown(s
, "Unable to negotiate LCP.", CDN_ADMIN_DISC
, TERM_USER_ERROR
);
951 sessionshutdown(s
, "Unsupported authentication.", CDN_ADMIN_DISC
, TERM_USER_ERROR
);
956 sess_local
[s
].lcp_authtype
= authtype
;
958 switch (session
[s
].ppp
.lcp
)
963 uint8_t *response
= makeppp(b
, sizeof(b
), p
, 2, s
, t
, PPPLCP
, 0, 0, 0);
964 if (!response
) return;
965 *response
= TerminateAck
;
966 *((uint16_t *) (response
+ 2)) = htons(l
= 4);
968 LOG(3, s
, t
, "LCP: send %s\n", ppp_code(*response
));
969 if (config
->debug
> 3) dumplcp(response
, l
);
971 tunnelsend(b
, l
+ (response
- b
), t
);
977 initialise_restart_count(s
, lcp
);
982 LOG(2, s
, t
, "LCP: ConfigNak in state %s? Sending ConfigReq\n", ppp_state(session
[s
].ppp
.lcp
));
992 LOG(2, s
, t
, "LCP: ignoring %s in state %s\n", ppp_code(*p
), ppp_state(session
[s
].ppp
.lcp
));
996 else if (*p
== TerminateReq
)
998 switch (session
[s
].ppp
.lcp
)
1011 zero_restart_count(s
, lcp
);
1012 change_state(s
, lcp
, Closing
);
1016 LOG(2, s
, t
, "LCP: ignoring %s in state %s\n", ppp_code(*p
), ppp_state(session
[s
].ppp
.lcp
));
1020 *p
= TerminateAck
; // send ack
1021 q
= makeppp(b
, sizeof(b
), p
, l
, s
, t
, PPPLCP
, 0, 0, 0);
1024 LOG(3, s
, t
, "LCP: send %s\n", ppp_code(*q
));
1025 if (config
->debug
> 3) dumplcp(q
, l
);
1027 tunnelsend(b
, l
+ (q
- b
), t
); // send it
1029 else if (*p
== ProtocolRej
)
1036 if (l
> 5 && !(proto
& 1))
1043 if (proto
== PPPIPV6CP
)
1045 LOG(3, s
, t
, "IPv6 rejected\n");
1046 change_state(s
, ipv6cp
, Closed
);
1050 LOG(3, s
, t
, "LCP protocol reject: 0x%04X\n", proto
);
1053 else if (*p
== EchoReq
)
1055 *p
= EchoReply
; // reply
1056 *(uint32_t *) (p
+ 4) = htonl(session
[s
].magic
); // our magic number
1057 q
= makeppp(b
, sizeof(b
), p
, l
, s
, t
, PPPLCP
, 0, 0, 0);
1060 LOG(4, s
, t
, "LCP: send %s\n", ppp_code(*q
));
1061 if (config
->debug
> 3) dumplcp(q
, l
);
1063 tunnelsend(b
, l
+ (q
- b
), t
); // send it
1065 else if (*p
== EchoReply
)
1067 // Ignore it, last_packet time is set earlier than this.
1069 else if (*p
!= CodeRej
)
1071 ppp_code_rej(s
, t
, PPPLCP
, "LCP", p
, l
, b
, sizeof(b
));
1075 int join_bundle(sessionidt s
)
1077 // Search for a bundle to join
1080 for (i
= 1; i
< MAXBUNDLE
; i
++)
1082 if (bundle
[i
].state
!= BUNDLEFREE
)
1084 if (epdiscmp(session
[s
].epdis
,bundle
[i
].epdis
) && !strcmp(session
[s
].user
, bundle
[i
].user
))
1086 session
[s
].bundle
= i
;
1087 bundle
[i
].mrru
= session
[s
].mrru
;
1088 bundle
[i
].mssf
= session
[s
].mssf
;
1089 if (session
[s
].epdis
.length
> 0)
1090 setepdis(&bundle
[i
].epdis
, session
[s
].epdis
);
1092 strcpy(bundle
[i
].user
, session
[s
].user
);
1093 bundle
[i
].members
[bundle
[i
].num_of_links
] = s
;
1094 bundle
[i
].num_of_links
++;
1095 LOG(3, s
, session
[s
].tunnel
, "MPPP: Bundling additional line in bundle (%d), lines:%d\n",i
,bundle
[i
].num_of_links
);
1101 // No previously created bundle was found for this session, so create a new one
1102 if (!(b
= new_bundle())) return 0;
1104 session
[s
].bundle
= b
;
1105 bundle
[b
].mrru
= session
[s
].mrru
;
1106 bundle
[b
].mssf
= session
[s
].mssf
;
1107 if (session
[s
].epdis
.length
> 0)
1108 setepdis(&bundle
[b
].epdis
, session
[s
].epdis
);
1110 strcpy(bundle
[b
].user
, session
[s
].user
);
1111 bundle
[b
].members
[0] = s
;
1112 LOG(3, s
, session
[s
].tunnel
, "MPPP: Created a new bundle (%d)\n", b
);
1116 static int epdiscmp(epdist ep1
, epdist ep2
)
1119 if (ep1
.length
!= ep2
.length
)
1122 if (ep1
.addr_class
!= ep2
.addr_class
)
1125 for (ad
= 0; ad
< ep1
.length
; ad
++)
1126 if (ep1
.address
[ad
] != ep2
.address
[ad
])
1132 static void setepdis(epdist
*ep1
, epdist ep2
)
1135 ep1
->length
= ep2
.length
;
1136 ep1
->addr_class
= ep2
.addr_class
;
1137 for (ad
= 0; ad
< ep2
.length
; ad
++)
1138 ep1
->address
[ad
] = ep2
.address
[ad
];
1141 static bundleidt
new_bundle()
1144 for (i
= 1; i
< MAXBUNDLE
; i
++)
1146 if (bundle
[i
].state
== BUNDLEFREE
)
1148 LOG(4, 0, 0, "MPPP: Assigning bundle ID %d\n", i
);
1149 bundle
[i
].num_of_links
= 1;
1150 bundle
[i
].last_check
= time_now
; // Initialize last_check value
1151 bundle
[i
].state
= BUNDLEOPEN
;
1152 bundle
[i
].current_ses
= -1; // This is to enforce the first session 0 to be used at first
1153 if (i
> config
->cluster_highest_bundleid
)
1154 config
->cluster_highest_bundleid
= i
;
1158 LOG(0, 0, 0, "MPPP: Can't find a free bundle! There shouldn't be this many in use!\n");
1162 static void ipcp_open(sessionidt s
, tunnelidt t
)
1164 LOG(3, s
, t
, "IPCP: Opened, session is now active\n");
1166 change_state(s
, ipcp
, Opened
);
1168 if (!(session
[s
].walled_garden
|| session
[s
].flags
& SESSION_STARTED
))
1170 uint16_t r
= radiusnew(s
);
1173 radiussend(r
, RADIUSSTART
); // send radius start
1175 // don't send further Start records if IPCP is restarted
1176 session
[s
].flags
|= SESSION_STARTED
;
1177 cluster_send_session(s
);
1181 // start IPv6 if configured and still in passive state
1182 if (session
[s
].ppp
.ipv6cp
== Stopped
)
1185 change_state(s
, ipv6cp
, RequestSent
);
1189 // Process IPCP messages
1190 void processipcp(sessionidt s
, tunnelidt t
, uint8_t *p
, uint16_t l
)
1192 uint8_t b
[MAXETHER
];
1198 LOG_HEX(5, "IPCP", p
, l
);
1201 LOG(1, s
, t
, "Short IPCP %d bytes\n", l
);
1202 STAT(tunnel_rx_errors
);
1206 if ((hl
= ntohs(*(uint16_t *) (p
+ 2))) > l
)
1208 LOG(1, s
, t
, "Length mismatch IPCP %u/%u\n", hl
, l
);
1209 STAT(tunnel_rx_errors
);
1214 if (session
[s
].ppp
.phase
< Network
)
1216 LOG(2, s
, t
, "IPCP %s ignored in %s phase\n", ppp_code(*p
), ppp_phase(session
[s
].ppp
.phase
));
1220 LOG(3, s
, t
, "IPCP: recv %s\n", ppp_code(*p
));
1222 if (*p
== ConfigAck
)
1224 switch (session
[s
].ppp
.ipcp
)
1227 initialise_restart_count(s
, ipcp
);
1228 change_state(s
, ipcp
, AckReceived
);
1233 LOG(2, s
, t
, "IPCP: ConfigAck in state %s? Sending ConfigReq\n", ppp_state(session
[s
].ppp
.ipcp
));
1235 change_state(s
, ipcp
, RequestSent
);
1243 LOG(2, s
, t
, "IPCP: ignoring %s in state %s\n", ppp_code(*p
), ppp_state(session
[s
].ppp
.ipcp
));
1246 else if (*p
== ConfigReq
)
1248 uint8_t *response
= 0;
1256 if (!o
[1] || o
[1] > length
) return;
1260 case 3: // ip address
1261 gotip
++; // seen address
1262 if (o
[1] != 6) return;
1264 addr
= htonl(session
[s
].ip
);
1265 if (memcmp(o
+ 2, &addr
, (sizeof addr
)))
1268 q
= ppp_conf_nak(s
, b
, sizeof(b
), PPPIPCP
, &response
, q
, p
, o
, (uint8_t *) &addr
, sizeof(addr
));
1269 if (!q
|| (q
!= oq
&& *response
== ConfigRej
))
1271 sessionshutdown(s
, "Can't negotiate IPCP.", CDN_ADMIN_DISC
, TERM_USER_ERROR
);
1278 case 129: // primary DNS
1279 if (o
[1] != 6) return;
1281 addr
= htonl(session
[s
].dns1
);
1282 if (memcmp(o
+ 2, &addr
, (sizeof addr
)))
1284 q
= ppp_conf_nak(s
, b
, sizeof(b
), PPPIPCP
, &response
, q
, p
, o
, (uint8_t *) &addr
, sizeof(addr
));
1290 case 131: // secondary DNS
1291 if (o
[1] != 6) return;
1293 addr
= htonl(session
[s
].dns2
);
1294 if (memcmp(o
+ 2, &addr
, sizeof(addr
)))
1296 q
= ppp_conf_nak(s
, b
, sizeof(b
), PPPIPCP
, &response
, q
, p
, o
, (uint8_t *) &addr
, sizeof(addr
));
1303 LOG(2, s
, t
, " Rejecting PPP IPCP Option type %d\n", *o
);
1304 q
= ppp_conf_rej(s
, b
, sizeof(b
), PPPIPCP
, &response
, q
, p
, o
);
1314 l
= q
- response
; // IPCP packet length
1315 *((uint16_t *) (response
+ 2)) = htons(l
); // update header
1319 // Send packet back as ConfigAck
1320 response
= makeppp(b
, sizeof(b
), p
, l
, s
, t
, PPPIPCP
, 0, 0, 0);
1321 if (!response
) return;
1322 *response
= ConfigAck
;
1326 LOG(1, s
, t
, "No IP in IPCP request\n");
1327 STAT(tunnel_rx_errors
);
1331 switch (session
[s
].ppp
.ipcp
)
1334 response
= makeppp(b
, sizeof(b
), p
, 2, s
, t
, PPPIPCP
, 0, 0, 0);
1335 if (!response
) return;
1336 *response
= TerminateAck
;
1337 *((uint16_t *) (response
+ 2)) = htons(l
= 4);
1341 initialise_restart_count(s
, ipcp
);
1343 if (*response
== ConfigAck
)
1344 change_state(s
, ipcp
, AckSent
);
1346 change_state(s
, ipcp
, RequestSent
);
1351 if (*response
== ConfigAck
)
1352 change_state(s
, ipcp
, AckSent
);
1357 if (*response
== ConfigAck
)
1363 initialise_restart_count(s
, ipcp
);
1368 if (*response
== ConfigAck
)
1369 change_state(s
, ipcp
, AckSent
);
1371 change_state(s
, ipcp
, RequestSent
);
1376 LOG(2, s
, t
, "IPCP: ignoring %s in state %s\n", ppp_code(*p
), ppp_state(session
[s
].ppp
.ipcp
));
1380 LOG(3, s
, t
, "IPCP: send %s\n", ppp_code(*response
));
1381 tunnelsend(b
, l
+ (response
- b
), t
);
1383 else if (*p
== TerminateReq
)
1385 switch (session
[s
].ppp
.ipcp
)
1397 zero_restart_count(s
, ipcp
);
1398 change_state(s
, ipcp
, Closing
);
1402 LOG(2, s
, t
, "IPCP: ignoring %s in state %s\n", ppp_code(*p
), ppp_state(session
[s
].ppp
.ipcp
));
1406 *p
= TerminateAck
; // send ack
1407 q
= makeppp(b
, sizeof(b
), p
, l
, s
, t
, PPPIPCP
, 0, 0, 0);
1410 LOG(3, s
, t
, "IPCP: send %s\n", ppp_code(*q
));
1411 tunnelsend(b
, l
+ (q
- b
), t
); // send it
1413 else if (*p
!= CodeRej
)
1415 ppp_code_rej(s
, t
, PPPIPCP
, "IPCP", p
, l
, b
, sizeof(b
));
1419 static void ipv6cp_open(sessionidt s
, tunnelidt t
)
1421 LOG(3, s
, t
, "IPV6CP: Opened\n");
1423 change_state(s
, ipv6cp
, Opened
);
1424 if (session
[s
].ipv6prefixlen
)
1425 route6set(s
, session
[s
].ipv6route
, session
[s
].ipv6prefixlen
, 1);
1427 // Send an initial RA (TODO: Should we send these regularly?)
1428 send_ipv6_ra(s
, t
, NULL
);
1431 // Process IPV6CP messages
1432 void processipv6cp(sessionidt s
, tunnelidt t
, uint8_t *p
, uint16_t l
)
1434 uint8_t b
[MAXETHER
];
1438 CSTAT(processipv6cp
);
1440 LOG_HEX(5, "IPV6CP", p
, l
);
1443 LOG(1, s
, t
, "Short IPV6CP %d bytes\n", l
);
1444 STAT(tunnel_rx_errors
);
1448 if ((hl
= ntohs(*(uint16_t *) (p
+ 2))) > l
)
1450 LOG(1, s
, t
, "Length mismatch IPV6CP %u/%u\n", hl
, l
);
1451 STAT(tunnel_rx_errors
);
1456 if (session
[s
].ppp
.phase
< Network
)
1458 LOG(2, s
, t
, "IPV6CP %s ignored in %s phase\n", ppp_code(*p
), ppp_phase(session
[s
].ppp
.phase
));
1462 LOG(3, s
, t
, "IPV6CP: recv %s\n", ppp_code(*p
));
1466 LOG(3, s
, t
, "IPV6CP: no IPv4 address (IPCP in state %s)\n", ppp_state(session
[s
].ppp
.ipcp
));
1467 return; // need IPCP to complete...
1470 if (*p
== ConfigAck
)
1472 switch (session
[s
].ppp
.ipv6cp
)
1475 initialise_restart_count(s
, ipv6cp
);
1476 change_state(s
, ipv6cp
, AckReceived
);
1481 LOG(2, s
, t
, "IPV6CP: ConfigAck in state %s? Sending ConfigReq\n", ppp_state(session
[s
].ppp
.ipv6cp
));
1483 change_state(s
, ipv6cp
, RequestSent
);
1491 LOG(2, s
, t
, "IPV6CP: ignoring %s in state %s\n", ppp_code(*p
), ppp_state(session
[s
].ppp
.ipv6cp
));
1494 else if (*p
== ConfigReq
)
1496 uint8_t *response
= 0;
1504 if (!o
[1] || o
[1] > length
) return;
1508 case 1: // interface identifier
1509 gotip
++; // seen address
1510 if (o
[1] != 10) return;
1512 *(uint32_t *) ident
= htonl(session
[s
].ip
);
1513 *(uint32_t *) (ident
+ 4) = 0;
1515 if (memcmp(o
+ 2, ident
, sizeof(ident
)))
1517 q
= ppp_conf_nak(s
, b
, sizeof(b
), PPPIPV6CP
, &response
, q
, p
, o
, ident
, sizeof(ident
));
1524 LOG(2, s
, t
, " Rejecting PPP IPV6CP Option type %d\n", *o
);
1525 q
= ppp_conf_rej(s
, b
, sizeof(b
), PPPIPV6CP
, &response
, q
, p
, o
);
1535 l
= q
- response
; // IPV6CP packet length
1536 *((uint16_t *) (response
+ 2)) = htons(l
); // update header
1540 // Send packet back as ConfigAck
1541 response
= makeppp(b
, sizeof(b
), p
, l
, s
, t
, PPPIPV6CP
, 0, 0, 0);
1542 if (!response
) return;
1543 *response
= ConfigAck
;
1547 LOG(1, s
, t
, "No interface identifier in IPV6CP request\n");
1548 STAT(tunnel_rx_errors
);
1552 switch (session
[s
].ppp
.ipv6cp
)
1555 response
= makeppp(b
, sizeof(b
), p
, 2, s
, t
, PPPIPV6CP
, 0, 0, 0);
1556 if (!response
) return;
1557 *response
= TerminateAck
;
1558 *((uint16_t *) (response
+ 2)) = htons(l
= 4);
1562 initialise_restart_count(s
, ipv6cp
);
1564 if (*response
== ConfigAck
)
1565 change_state(s
, ipv6cp
, AckSent
);
1567 change_state(s
, ipv6cp
, RequestSent
);
1572 if (*response
== ConfigAck
)
1573 change_state(s
, ipv6cp
, AckSent
);
1578 if (*response
== ConfigAck
)
1584 initialise_restart_count(s
, ipv6cp
);
1589 if (*response
== ConfigAck
)
1590 change_state(s
, ipv6cp
, AckSent
);
1592 change_state(s
, ipv6cp
, RequestSent
);
1597 LOG(2, s
, t
, "IPV6CP: ignoring %s in state %s\n", ppp_code(*p
), ppp_state(session
[s
].ppp
.ipv6cp
));
1601 LOG(3, s
, t
, "IPV6CP: send %s\n", ppp_code(*response
));
1602 tunnelsend(b
, l
+ (response
- b
), t
);
1604 else if (*p
== TerminateReq
)
1606 switch (session
[s
].ppp
.ipv6cp
)
1618 zero_restart_count(s
, ipv6cp
);
1619 change_state(s
, ipv6cp
, Closing
);
1623 LOG(2, s
, t
, "IPV6CP: ignoring %s in state %s\n", ppp_code(*p
), ppp_state(session
[s
].ppp
.ipv6cp
));
1627 *p
= TerminateAck
; // send ack
1628 q
= makeppp(b
, sizeof(b
), p
, l
, s
, t
, PPPIPV6CP
, 0, 0, 0);
1631 LOG(3, s
, t
, "IPV6CP: send %s\n", ppp_code(*q
));
1632 tunnelsend(b
, l
+ (q
- b
), t
); // send it
1634 else if (*p
!= CodeRej
)
1636 ppp_code_rej(s
, t
, PPPIPV6CP
, "IPV6CP", p
, l
, b
, sizeof(b
));
1640 // process IP packet received
1642 // This MUST be called with at least 4 byte behind 'p'.
1643 // (i.e. this routine writes to p[-4]).
1644 void processipin(sessionidt s
, tunnelidt t
, uint8_t *p
, uint16_t l
)
1650 LOG_HEX(5, "IP", p
, l
);
1654 LOG(1, s
, t
, "IP packet too short %d\n", l
);
1655 STAT(tunnel_rx_errors
);
1659 ip
= ntohl(*(uint32_t *)(p
+ 12));
1663 LOG(1, s
, t
, "IP packet too long %d\n", l
);
1664 STAT(tunnel_rx_errors
);
1668 if (session
[s
].ppp
.phase
!= Network
|| session
[s
].ppp
.ipcp
!= Opened
)
1671 if (!session
[s
].bundle
|| bundle
[session
[s
].bundle
].num_of_links
< 2) // FIXME:
1673 // no spoof (do sessionbyip to handled statically routed subnets)
1674 if (ip
!= session
[s
].ip
&& sessionbyip(htonl(ip
)) != s
)
1676 LOG(4, s
, t
, "Dropping packet with spoofed IP %s\n", fmtaddr(htonl(ip
), 0));
1681 // run access-list if any
1682 if (session
[s
].filter_in
&& !ip_filter(p
, l
, session
[s
].filter_in
- 1))
1685 // adjust MSS on SYN and SYN,ACK packets with options
1686 if ((ntohs(*(uint16_t *) (p
+ 6)) & 0x1fff) == 0 && p
[9] == IPPROTO_TCP
) // first tcp fragment
1688 int ihl
= (p
[0] & 0xf) * 4; // length of IP header
1689 if (l
>= ihl
+ 20 && (p
[ihl
+ 13] & TCP_FLAG_SYN
) && ((p
[ihl
+ 12] >> 4) > 5))
1690 adjust_tcp_mss(s
, t
, p
, l
, p
+ ihl
);
1693 // Add on the tun header
1695 *(uint32_t *) p
= htonl(PKTIP
);
1698 // Are we throttled and a slave?
1699 if (session
[s
].tbf_in
&& !config
->cluster_iam_master
) {
1700 // Pass it to the master for handling.
1701 master_throttle_packet(session
[s
].tbf_in
, p
, l
);
1705 // Are we throttled and a master??
1706 if (session
[s
].tbf_in
&& config
->cluster_iam_master
) {
1707 // Actually handle the throttled packets.
1708 tbf_queue_packet(session
[s
].tbf_in
, p
, l
);
1713 if (tun_write(p
, l
) < 0)
1715 STAT(tun_tx_errors
);
1716 LOG(0, s
, t
, "Error writing %d bytes to TUN device: %s (tunfd=%d, p=%p)\n",
1717 l
, strerror(errno
), tunfd
, p
);
1725 if (session
[s
].snoop_ip
&& session
[s
].snoop_port
)
1727 // Snooping this session
1728 snoop_send_packet(p
, l
, session
[s
].snoop_ip
, session
[s
].snoop_port
);
1731 increment_counter(&session
[s
].cin
, &session
[s
].cin_wrap
, l
);
1732 session
[s
].cin_delta
+= l
;
1735 sess_local
[s
].cin
+= l
;
1736 sess_local
[s
].pin
++;
1740 STAT(tun_tx_packets
);
1741 INC_STAT(tun_tx_bytes
, l
);
1744 // process Multilink PPP packet received
1745 void processmpin(sessionidt s
, tunnelidt t
, uint8_t *p
, uint16_t l
)
1747 bundleidt b
= session
[s
].bundle
;
1748 uint8_t begin_frame
;
1755 LOG(3, s
, t
, "MPPP: Invalid bundle id: 0\n");
1759 begin_frame
= (*p
& 0x80);
1760 end_frame
= (*p
& 0x40);
1761 if (session
[s
].mssf
)
1763 // Get 12 bit for seq number
1764 uint16_t short_seq_num
= ntohs((*(uint16_t *) p
) & 0xFF0F);
1765 uint16_t short_seq_num2
= short_seq_num
>> 4;
1768 seq_num
= short_seq_num2
;
1769 // After this point the pointer should be advanced 2 bytes
1770 LOG(3, s
, t
, "MPPP: 12 bits, sequence number: %d, short1: %d, short2: %d\n",seq_num
, short_seq_num
, short_seq_num2
);
1774 // Get 24 bit for seq number
1776 seq_num
= ntohl((*(uint32_t *) p
) & 0xFFFFFF00);
1777 seq_num
= seq_num
>> 8;
1780 // After this point the pointer should be advanced 4 bytes
1781 LOG(4, s
, t
, "MPPP: 24 bits sequence number:%d\n",seq_num
);
1784 if (seq_num
- bundle
[b
].offset
< 0)
1786 bundle
[b
].offset
= 0;
1787 bundle
[b
].pending_frag
= 0;
1790 offset
= bundle
[b
].offset
;
1793 // Check for previous non-assembled frames
1795 if (bundle
[b
].pending_frag
)
1797 uint32_t fn
= bundle
[b
].seq_num_m
- offset
;
1799 bundle
[b
].pending_frag
= 0;
1800 // Check for array indexes
1801 if (fn
< 0 || fn
> MAXFRAGNUM
)
1803 LOG(2, s
, t
, "ERROR: Index out of range fn:%d, bundle:%d\n",fn
,b
);
1807 if (seq_num
-offset
< 0 || seq_num
-offset
> MAXFRAGNUM
)
1809 LOG(2, s
, t
, "ERROR: Index out of range fn(last):%d, bundle:%d\n",fn
,b
);
1812 /////////////////////////////////////////////////////
1813 cur_len
= 4; // This is set to 4 to leave 4 bytes for function processipin
1814 for (fn
= bundle
[b
].seq_num_m
- offset
; fn
< seq_num
- offset
; fn
++)
1816 if (!frag
[b
].fragment
[fn
].length
)
1818 LOG(4, s
, t
, "MPPP: Found lost fragment while reassembling frame %d in (%d,%d)\n",fn
, bundle
[b
].seq_num_m
-offset
, seq_num
-offset
);
1823 if (cur_len
+ frag
[b
].fragment
[fn
].length
> MAXETHER
)
1825 LOG(2, s
, t
, "MPPP: ERROR: very long frame after assembling %d\n", frag
[b
].fragment
[fn
].length
+cur_len
);
1830 memcpy(frag
[b
].reassembled_frame
+cur_len
, frag
[b
].fragment
[fn
].data
, frag
[b
].fragment
[fn
].length
);
1831 cur_len
+= frag
[b
].fragment
[fn
].length
;
1832 frag
[b
].fragment
[fn
].length
= 0; // Indicates that this fragment has been consumed
1833 // This is usefull for compression
1834 memset(frag
[b
].fragment
[fn
].data
, 0, sizeof(frag
[b
].fragment
[fn
].data
));
1839 frag
[b
].re_frame_len
= cur_len
;
1840 // Process the resassembled frame
1841 LOG(4, s
, t
, "MPPP: Process the reassembled frame, len=%d\n",cur_len
);
1842 processmpframe(s
, t
, frag
[b
].reassembled_frame
, frag
[b
].re_frame_len
, 1);
1843 // Set reassembled frame length to zero after processing it
1844 frag
[b
].re_frame_len
= 0;
1845 memset(frag
[b
].reassembled_frame
, 0, sizeof(frag
[b
].reassembled_frame
));
1848 //////////////////////////////////////////
1849 bundle
[b
].seq_num_m
= seq_num
;
1852 // Both bits are set
1853 LOG(4, s
, t
, "MPPP: Both bits are set (Begin and End).\n");
1854 processmpframe(s
, t
, p
, l
, 0);
1855 // The maximum number of fragments is 1500
1856 if (seq_num
- bundle
[b
].offset
>= 1400)
1858 bundle
[b
].offset
= seq_num
;
1859 LOG(4, s
, t
, "MPPP: Setting offset to: %d\n",bundle
[b
].offset
);
1864 bundle
[b
].pending_frag
= 1;
1866 LOG(4, s
, t
, "MPPP: Push to receive buffer\n");
1867 // Push to the receive buffer
1868 // Array indexes checking
1869 if (seq_num
-offset
< 0 || seq_num
-offset
>= MAXFRAGNUM
)
1871 LOG(2, s
, t
, "ERROR: Index out of range, push to receive buffer(1) seq:%d, offset:%d, bundle:%d\n",seq_num
,offset
,b
);
1874 // Perform length checking
1877 LOG(2, s
, t
, "MPPP: ERROR: very long fragment length (1)\n");
1880 frag
[b
].fragment
[seq_num
- offset
].length
= l
;
1881 memcpy(frag
[b
].fragment
[seq_num
- offset
].data
, p
, l
);
1886 LOG(4, s
, t
, "MPPP: Push to receive buffer\n");
1887 // Push to the receive buffer
1888 // Array indexes checking
1889 if (seq_num
-offset
< 0 || seq_num
-offset
>= MAXFRAGNUM
)
1891 LOG(2, s
, t
, "ERROR: Index out of range, push to receive buffer(2) seq:%d, offset:%d, bundle:%d\n",seq_num
,offset
,b
);
1894 // Perform length checking
1897 LOG(2, s
, t
, "MPPP: ERROR: very long fragment length (2).\n");
1900 frag
[b
].fragment
[seq_num
- offset
].length
= l
;
1901 memcpy(frag
[b
].fragment
[seq_num
- offset
].data
, p
, l
);
1905 // process IPv6 packet received
1907 // This MUST be called with at least 4 byte behind 'p'.
1908 // (i.e. this routine writes to p[-4]).
1909 void processipv6in(sessionidt s
, tunnelidt t
, uint8_t *p
, uint16_t l
)
1914 CSTAT(processipv6in
);
1916 LOG_HEX(5, "IPv6", p
, l
);
1918 ip
= *(struct in6_addr
*) (p
+ 8);
1919 ipv4
= ntohl(*(uint32_t *)(p
+ 16));
1923 LOG(1, s
, t
, "IP packet too long %d\n", l
);
1924 STAT(tunnel_rx_errors
);
1928 if (session
[s
].ppp
.phase
!= Network
|| session
[s
].ppp
.ipv6cp
!= Opened
)
1932 if (ipv4
!= session
[s
].ip
&& memcmp(&config
->ipv6_prefix
, &ip
, 8) && sessionbyipv6(ip
) != s
)
1934 char str
[INET6_ADDRSTRLEN
];
1935 LOG(5, s
, t
, "Dropping packet with spoofed IP %s\n",
1936 inet_ntop(AF_INET6
, &ip
, str
, INET6_ADDRSTRLEN
));
1940 // Check if it's a Router Solicition message.
1941 if (*(p
+ 6) == 58 && *(p
+ 7) == 255 && *(p
+ 24) == 0xFF && *(p
+ 25) == 2 &&
1942 *(uint32_t *)(p
+ 26) == 0 && *(uint32_t *)(p
+ 30) == 0 &&
1943 *(uint32_t *)(p
+ 34) == 0 &&
1944 *(p
+ 38) == 0 && *(p
+ 39) == 2 && *(p
+ 40) == 133) {
1945 LOG(3, s
, t
, "Got IPv6 RS\n");
1946 send_ipv6_ra(s
, t
, &ip
);
1950 // Add on the tun header
1952 *(uint32_t *) p
= htonl(PKTIPV6
);
1955 // Are we throttled and a slave?
1956 if (session
[s
].tbf_in
&& !config
->cluster_iam_master
) {
1957 // Pass it to the master for handling.
1958 master_throttle_packet(session
[s
].tbf_in
, p
, l
);
1962 // Are we throttled and a master??
1963 if (session
[s
].tbf_in
&& config
->cluster_iam_master
) {
1964 // Actually handle the throttled packets.
1965 tbf_queue_packet(session
[s
].tbf_in
, p
, l
);
1970 if (tun_write(p
, l
) < 0)
1972 STAT(tun_tx_errors
);
1973 LOG(0, s
, t
, "Error writing %d bytes to TUN device: %s (tunfd=%d, p=%p)\n",
1974 l
, strerror(errno
), tunfd
, p
);
1982 if (session
[s
].snoop_ip
&& session
[s
].snoop_port
)
1984 // Snooping this session
1985 snoop_send_packet(p
, l
, session
[s
].snoop_ip
, session
[s
].snoop_port
);
1988 increment_counter(&session
[s
].cin
, &session
[s
].cin_wrap
, l
);
1989 session
[s
].cin_delta
+= l
;
1992 sess_local
[s
].cin
+= l
;
1993 sess_local
[s
].pin
++;
1997 STAT(tun_tx_packets
);
1998 INC_STAT(tun_tx_bytes
, l
);
2002 // Helper routine for the TBF filters.
2003 // Used to send queued data in from the user.
2005 void send_ipin(sessionidt s
, uint8_t *buf
, int len
)
2007 LOG_HEX(5, "IP in throttled", buf
, len
);
2009 if (write(tunfd
, buf
, len
) < 0)
2011 STAT(tun_tx_errors
);
2012 LOG(0, 0, 0, "Error writing %d bytes to TUN device: %s (tunfd=%d, p=%p)\n",
2013 len
, strerror(errno
), tunfd
, buf
);
2021 if (session
[s
].snoop_ip
&& session
[s
].snoop_port
)
2023 // Snooping this session
2024 snoop_send_packet(buf
, len
, session
[s
].snoop_ip
, session
[s
].snoop_port
);
2027 // Increment packet counters
2028 increment_counter(&session
[s
].cin
, &session
[s
].cin_wrap
, len
);
2029 session
[s
].cin_delta
+= len
;
2032 sess_local
[s
].cin
+= len
;
2033 sess_local
[s
].pin
++;
2037 STAT(tun_tx_packets
);
2038 INC_STAT(tun_tx_bytes
, len
- 4);
2042 // Process CCP messages
2043 void processccp(sessionidt s
, tunnelidt t
, uint8_t *p
, uint16_t l
)
2045 uint8_t b
[MAXETHER
];
2050 LOG_HEX(5, "CCP", p
, l
);
2052 if (session
[s
].ppp
.phase
< Network
)
2054 LOG(2, s
, t
, "CCP %s ignored in %s phase\n", ppp_code(*p
), ppp_phase(session
[s
].ppp
.phase
));
2060 LOG(1, s
, t
, "Short CCP packet\n");
2061 STAT(tunnel_rx_errors
);
2064 LOG(4, s
, t
, "CCP: recv %s\n", ppp_code(*p
));
2065 if (*p
== ConfigAck
)
2067 switch (session
[s
].ppp
.ccp
)
2070 initialise_restart_count(s
, ccp
);
2071 change_state(s
, ccp
, AckReceived
);
2076 LOG(2, s
, t
, "CCP: ConfigAck in state %s? Sending ConfigReq\n", ppp_state(session
[s
].ppp
.ccp
));
2078 change_state(s
, ccp
, RequestSent
);
2082 LOG(3, s
, t
, "CCP: Opened\n");
2083 change_state(s
, ccp
, Opened
);
2087 LOG(2, s
, t
, "CCP: ignoring %s in state %s\n", ppp_code(*p
), ppp_state(session
[s
].ppp
.ccp
));
2090 else if (*p
== ConfigReq
)
2092 if (l
< 6) // accept no compression
2094 else // compression requested--reject
2097 q
= makeppp(b
, sizeof(b
), p
, l
, s
, t
, PPPCCP
, 0, 0, 0);
2100 switch (session
[s
].ppp
.ccp
)
2103 q
= makeppp(b
, sizeof(b
), p
, 2, s
, t
, PPPCCP
, 0, 0, 0);
2106 *((uint16_t *) (q
+ 2)) = htons(l
= 4);
2110 initialise_restart_count(s
, ccp
);
2112 if (*q
== ConfigAck
)
2113 change_state(s
, ccp
, AckSent
);
2115 change_state(s
, ccp
, RequestSent
);
2120 if (*q
== ConfigAck
)
2121 change_state(s
, ccp
, AckSent
);
2126 if (*q
== ConfigAck
)
2127 change_state(s
, ccp
, Opened
);
2132 initialise_restart_count(s
, ccp
);
2137 if (*q
== ConfigAck
)
2138 change_state(s
, ccp
, AckSent
);
2140 change_state(s
, ccp
, RequestSent
);
2145 LOG(2, s
, t
, "CCP: ignoring %s in state %s\n", ppp_code(*p
), ppp_state(session
[s
].ppp
.ccp
));
2149 LOG(4, s
, t
, "CCP: send %s\n", ppp_code(*q
));
2150 tunnelsend(b
, l
+ (q
- b
), t
);
2152 else if (*p
== TerminateReq
)
2155 q
= makeppp(b
, sizeof(b
), p
, l
, s
, t
, PPPCCP
, 0, 0, 0);
2157 LOG(3, s
, t
, "CCP: send %s\n", ppp_code(*q
));
2158 tunnelsend(b
, l
+ (q
- b
), t
);
2159 change_state(s
, ccp
, Stopped
);
2161 else if (*p
!= CodeRej
)
2163 ppp_code_rej(s
, t
, PPPCCP
, "CCP", p
, l
, b
, sizeof(b
));
2167 // send a CHAP challenge
2168 void sendchap(sessionidt s
, tunnelidt t
)
2170 uint8_t b
[MAXETHER
];
2179 LOG(1, s
, t
, "No RADIUS to send challenge\n");
2180 STAT(tunnel_tx_errors
);
2184 LOG(1, s
, t
, "Send CHAP challenge\n");
2186 radius
[r
].chap
= 1; // CHAP not PAP
2188 if (radius
[r
].state
!= RADIUSCHAP
)
2191 radius
[r
].state
= RADIUSCHAP
;
2192 radius
[r
].retry
= backoff(radius
[r
].try++);
2193 if (radius
[r
].try > 5)
2195 sessionshutdown(s
, "CHAP timeout.", CDN_ADMIN_DISC
, TERM_REAUTHENTICATION_FAILURE
);
2196 STAT(tunnel_tx_errors
);
2199 q
= makeppp(b
, sizeof(b
), 0, 0, s
, t
, PPPCHAP
, 0, 0, 0);
2202 *q
= 1; // challenge
2203 q
[1] = radius
[r
].id
; // ID
2204 q
[4] = 16; // value size (size of challenge)
2205 memcpy(q
+ 5, radius
[r
].auth
, 16); // challenge
2206 strcpy((char *) q
+ 21, hostname
); // our name
2207 *(uint16_t *) (q
+ 2) = htons(strlen(hostname
) + 21); // length
2208 tunnelsend(b
, strlen(hostname
) + 21 + (q
- b
), t
); // send it
2211 // fill in a L2TP message with a PPP frame,
2212 // returns start of PPP frame
2213 uint8_t *makeppp(uint8_t *b
, int size
, uint8_t *p
, int l
, sessionidt s
, tunnelidt t
, uint16_t mtype
, uint8_t prio
, bundleidt bid
, uint8_t mp_bits
)
2215 uint16_t hdr
= 0x0002; // L2TP with no options
2216 uint16_t type
= mtype
;
2219 if (size
< 16) // Need more space than this!!
2221 LOG(0, s
, t
, "makeppp buffer too small for L2TP header (size=%d)\n", size
);
2225 if (prio
) hdr
|= 0x0100; // set priority bit
2227 *(uint16_t *) (b
+ 0) = htons(hdr
);
2228 *(uint16_t *) (b
+ 2) = htons(tunnel
[t
].far
); // tunnel
2229 *(uint16_t *) (b
+ 4) = htons(session
[s
].far
); // session
2232 // Check whether this session is part of multilink
2235 if (bundle
[bid
].num_of_links
> 1)
2236 type
= PPPMP
; // Change PPP message type to the PPPMP
2241 if (type
== PPPLCP
|| !(session
[s
].flags
& SESSION_ACFC
))
2243 *(uint16_t *) b
= htons(0xFF03); // HDLC header
2247 if (type
< 0x100 && session
[s
].flags
& SESSION_PFC
)
2253 *(uint16_t *) b
= htons(type
);
2259 // Set the sequence number and (B)egin (E)nd flags
2260 if (session
[s
].mssf
)
2262 // Set the multilink bits
2263 uint16_t bits_send
= mp_bits
;
2264 *(uint16_t *) b
= htons((bundle
[bid
].seq_num_t
& 0xFF0F)|bits_send
);
2269 *(uint32_t *) b
= htonl(bundle
[bid
].seq_num_t
);
2270 // Set the multilink bits
2275 bundle
[bid
].seq_num_t
++;
2277 // Add the message type if this fragment has the begin bit set
2278 if (mp_bits
& MP_BEGIN
)
2280 //*b++ = mtype; // The next two lines are instead of this
2281 *(uint16_t *) b
= htons(mtype
); // Message type
2286 if ((b
- start
) + l
> size
)
2288 LOG(2, s
, t
, "makeppp would overflow buffer (size=%d, header+payload=%d)\n", size
, (b
- start
) + l
);
2299 static int add_lcp_auth(uint8_t *b
, int size
, int authtype
)
2302 if ((authtype
== AUTHCHAP
&& size
< 5) || size
< 4)
2305 *b
++ = 3; // Authentication-Protocol
2306 if (authtype
== AUTHCHAP
)
2308 len
= *b
++ = 5; // length
2309 *(uint16_t *) b
= htons(PPPCHAP
); b
+= 2;
2312 else if (authtype
== AUTHPAP
)
2314 len
= *b
++ = 4; // length
2315 *(uint16_t *) b
= htons(PPPPAP
); b
+= 2;
2319 LOG(0, 0, 0, "add_lcp_auth called with unsupported auth type %d\n", authtype
);
2325 // Send LCP ConfigReq for MRU, authentication type and magic no
2326 void sendlcp(sessionidt s
, tunnelidt t
)
2328 uint8_t b
[500], *q
, *l
;
2329 int authtype
= sess_local
[s
].lcp_authtype
;
2331 if (!(q
= makeppp(b
, sizeof(b
), NULL
, 0, s
, t
, PPPLCP
, 0, 0, 0)))
2334 LOG(3, s
, t
, "LCP: send ConfigReq%s%s%s including MP options\n",
2335 authtype
? " (" : "",
2336 authtype
? (authtype
== AUTHCHAP
? "CHAP" : "PAP") : "",
2337 authtype
? ")" : "");
2341 *l
++ = ++sess_local
[s
].lcp_ident
; // ID
2343 l
+= 2; //Save space for length
2345 if (sess_local
[s
].ppp_mru
)
2347 *l
++ = 1; *l
++ = 4; // Maximum-Receive-Unit (length 4)
2348 *(uint16_t *) l
= htons(sess_local
[s
].ppp_mru
); l
+= 2;
2352 l
+= add_lcp_auth(l
, sizeof(b
) - (l
- b
), authtype
);
2354 if (session
[s
].magic
)
2356 *l
++ = 5; *l
++ = 6; // Magic-Number (length 6)
2357 *(uint32_t *) l
= htonl(session
[s
].magic
);
2361 if (sess_local
[s
].mp_mrru
)
2363 *l
++ = 17; *l
++ = 4; // Multilink Max-Receive-Reconstructed-Unit (length 4)
2364 *(uint16_t *) l
= htons(sess_local
[s
].mp_mrru
); l
+= 2;
2367 if (sess_local
[s
].mp_epdis
)
2369 *l
++ = 19; *l
++ = 7; // Multilink Endpoint Discriminator (length 7)
2370 *l
++ = IPADDR
; // Endpoint Discriminator class
2371 *(uint32_t *) l
= htonl(sess_local
[s
].mp_epdis
);
2375 *(uint16_t *)(q
+ 2) = htons(l
- q
); // Length
2377 LOG_HEX(5, "PPPLCP", q
, l
- q
);
2378 if (config
->debug
> 3) dumplcp(q
, l
- q
);
2380 tunnelsend(b
, (l
- b
), t
);
2381 restart_timer(s
, lcp
);
2384 // Send CCP request for no compression
2385 void sendccp(sessionidt s
, tunnelidt t
)
2389 if (!(q
= makeppp(b
, sizeof(b
), NULL
, 0, s
, t
, PPPCCP
, 0, 0, 0)))
2392 LOG(3, s
, t
, "CCP: send ConfigReq (no compression)\n");
2395 *(q
+ 1) = ++sess_local
[s
].lcp_ident
; // ID
2396 *(uint16_t *)(q
+ 2) = htons(4); // Length
2398 LOG_HEX(5, "PPPCCP", q
, 4);
2399 tunnelsend(b
, (q
- b
) + 4 , t
);
2400 restart_timer(s
, ccp
);
2403 // Reject unknown/unconfigured protocols
2404 void protoreject(sessionidt s
, tunnelidt t
, uint8_t *p
, uint16_t l
, uint16_t proto
)
2407 uint8_t buf
[MAXETHER
];
2409 int mru
= session
[s
].mru
;
2410 if (mru
< MINMTU
) mru
= MINMTU
;
2411 if (mru
> sizeof(buf
)) mru
= sizeof(buf
);
2414 if (l
> mru
) l
= mru
;
2416 q
= makeppp(buf
, sizeof(buf
), 0, 0, s
, t
, PPPLCP
, 0, 0, 0);
2420 *(q
+ 1) = ++sess_local
[s
].lcp_ident
;
2421 *(uint16_t *)(q
+ 2) = htons(l
);
2422 *(uint16_t *)(q
+ 4) = htons(proto
);
2423 memcpy(q
+ 6, p
, l
- 6);
2425 if (proto
== PPPIPV6CP
)
2426 LOG(3, s
, t
, "LCP: send ProtocolRej (IPV6CP: not configured)\n");
2428 LOG(2, s
, t
, "LCP: sent ProtocolRej (0x%04X: unsupported)\n", proto
);
2430 tunnelsend(buf
, l
+ (q
- buf
), t
);