3 char const *cvs_id_ppp
= "$Id: ppp.c,v 1.103 2007-01-25 12:36:48 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 if (session
[s
].ppp
.phase
!= Authenticate
)
184 LOG(2, s
, t
, "CHAP ignored in %s phase\n", ppp_phase(session
[s
].ppp
.phase
));
188 r
= sess_local
[s
].radius
;
191 LOG(3, s
, t
, "Unexpected CHAP message\n");
193 // Some modems (Netgear DM602, possibly others) persist in using CHAP even
194 // after ACKing our ConfigReq for PAP.
195 if (sess_local
[s
].lcp_authtype
== AUTHPAP
&& config
->radius_authtypes
& AUTHCHAP
)
197 sess_local
[s
].lcp_authtype
= AUTHCHAP
;
203 if (p
[1] != radius
[r
].id
)
205 LOG(1, s
, t
, "Wrong CHAP response ID %d (should be %d) (%d)\n", p
[1], radius
[r
].id
, r
);
206 STAT(tunnel_rx_errors
);
207 sessionshutdown(s
, "Unexpected CHAP response ID.", CDN_ADMIN_DISC
, TERM_USER_ERROR
);
211 if (l
< 5 || p
[4] != 16)
213 LOG(1, s
, t
, "Bad CHAP response length %d\n", l
< 5 ? -1 : p
[4]);
214 STAT(tunnel_rx_errors
);
215 sessionshutdown(s
, "Bad CHAP response length.", CDN_ADMIN_DISC
, TERM_USER_ERROR
);
221 if (l
< 16 || l
- 16 >= sizeof(session
[s
].user
))
223 LOG(1, s
, t
, "CHAP user too long %d\n", l
- 16);
224 STAT(tunnel_rx_errors
);
225 sessionshutdown(s
, "CHAP username too long.", CDN_ADMIN_DISC
, TERM_USER_ERROR
);
229 // Run PRE_AUTH plugins
231 struct param_pre_auth packet
= { &tunnel
[t
], &session
[s
], NULL
, NULL
, PPPCHAP
, 1 };
233 packet
.password
= calloc(17, 1);
234 memcpy(packet
.password
, p
, 16);
239 packet
.username
= calloc(l
+ 1, 1);
240 memcpy(packet
.username
, p
, l
);
242 run_plugins(PLUGIN_PRE_AUTH
, &packet
);
243 if (!packet
.continue_auth
)
245 LOG(3, s
, t
, "A plugin rejected PRE_AUTH\n");
246 if (packet
.username
) free(packet
.username
);
247 if (packet
.password
) free(packet
.password
);
251 strncpy(session
[s
].user
, packet
.username
, sizeof(session
[s
].user
) - 1);
252 memcpy(radius
[r
].pass
, packet
.password
, 16);
254 free(packet
.username
);
255 free(packet
.password
);
259 LOG(3, s
, t
, "CHAP login %s\n", session
[s
].user
);
260 radiussend(r
, RADIUSAUTH
);
263 static void dumplcp(uint8_t *p
, int l
)
266 uint8_t *o
= (p
+ 4);
268 LOG_HEX(5, "PPP LCP Packet", p
, l
);
269 LOG(4, 0, 0, "PPP LCP Packet type %d (%s len %d)\n", *p
, ppp_code((int)*p
), ntohs( ((uint16_t *) p
)[1]) );
270 LOG(4, 0, 0, "Length: %d\n", l
);
271 if (*p
!= ConfigReq
&& *p
!= ConfigRej
&& *p
!= ConfigAck
)
280 LOG(4, 0, 0, " Option length is %d...\n", length
);
285 LOG(4, 0, 0, " Option type is 0...\n");
292 case 1: // Maximum-Receive-Unit
294 LOG(4, 0, 0, " %s %d\n", ppp_lcp_option(type
), ntohs(*(uint16_t *)(o
+ 2)));
296 LOG(4, 0, 0, " %s odd length %d\n", ppp_lcp_option(type
), length
);
298 case 2: // Async-Control-Character-Map
301 uint32_t asyncmap
= ntohl(*(uint32_t *)(o
+ 2));
302 LOG(4, 0, 0, " %s %x\n", ppp_lcp_option(type
), asyncmap
);
305 LOG(4, 0, 0, " %s odd length %d\n", ppp_lcp_option(type
), length
);
307 case 3: // Authentication-Protocol
310 int proto
= ntohs(*(uint16_t *)(o
+ 2));
311 LOG(4, 0, 0, " %s 0x%x (%s)\n", ppp_lcp_option(type
), proto
,
312 proto
== PPPPAP
? "PAP" : "UNSUPPORTED");
314 else if (length
== 5)
316 int proto
= ntohs(*(uint16_t *)(o
+ 2));
318 LOG(4, 0, 0, " %s 0x%x 0x%x (%s)\n", ppp_lcp_option(type
), proto
, algo
,
319 (proto
== PPPCHAP
&& algo
== 5) ? "CHAP MD5" : "UNSUPPORTED");
322 LOG(4, 0, 0, " %s odd length %d\n", ppp_lcp_option(type
), length
);
324 case 4: // Quality-Protocol
326 uint32_t qp
= ntohl(*(uint32_t *)(o
+ 2));
327 LOG(4, 0, 0, " %s %x\n", ppp_lcp_option(type
), qp
);
330 case 5: // Magic-Number
333 uint32_t magicno
= ntohl(*(uint32_t *)(o
+ 2));
334 LOG(4, 0, 0, " %s %x\n", ppp_lcp_option(type
), magicno
);
337 LOG(4, 0, 0, " %s odd length %d\n", ppp_lcp_option(type
), length
);
339 case 7: // Protocol-Field-Compression
340 case 8: // Address-And-Control-Field-Compression
341 LOG(4, 0, 0, " %s\n", ppp_lcp_option(type
));
344 LOG(2, 0, 0, " Unknown PPP LCP Option type %d\n", type
);
352 void lcp_open(sessionidt s
, tunnelidt t
)
354 // transition to Authentication or Network phase:
355 session
[s
].ppp
.phase
= sess_local
[s
].lcp_authtype
? Authenticate
: Network
;
357 LOG(3, s
, t
, "LCP: Opened, phase %s\n", ppp_phase(session
[s
].ppp
.phase
));
360 change_state(s
, lcp
, Opened
);
362 if (session
[s
].ppp
.phase
== Authenticate
)
364 if (sess_local
[s
].lcp_authtype
== AUTHCHAP
)
369 if (session
[s
].bundle
&& bundle
[session
[s
].bundle
].num_of_links
> 1)
371 sessionidt first_ses
= bundle
[session
[s
].bundle
].members
[0];
372 LOG(3, s
, t
, "MPPP: Skipping IPCP negotiation for session:%d, first session of bundle is:%d\n", s
, first_ses
);
373 session
[s
].ip
= session
[first_ses
].ip
;
374 session
[s
].dns1
= session
[first_ses
].dns1
;
375 session
[s
].dns2
= session
[first_ses
].dns2
;
376 session
[s
].session_timeout
= session
[first_ses
].session_timeout
;
383 change_state(s
, ipcp
, RequestSent
);
384 // move to passive state for IPv6 (if configured), CCP
385 if (config
->ipv6_prefix
.s6_addr
[0])
386 change_state(s
, ipv6cp
, Stopped
);
388 change_state(s
, ipv6cp
, Closed
);
390 change_state(s
, ccp
, Stopped
);
395 static void lcp_restart(sessionidt s
)
397 session
[s
].ppp
.phase
= Establish
;
399 change_state(s
, ipcp
, Dead
);
400 change_state(s
, ipv6cp
, Dead
);
401 change_state(s
, ccp
, Dead
);
404 static uint8_t *ppp_conf_rej(sessionidt s
, uint8_t *buf
, size_t blen
, uint16_t mtype
,
405 uint8_t **response
, uint8_t *queued
, uint8_t *packet
, uint8_t *option
)
407 if (!*response
|| **response
!= ConfigRej
)
409 queued
= *response
= makeppp(buf
, blen
, packet
, 2, s
, session
[s
].tunnel
, mtype
, 0, 0, 0);
417 if ((queued
- buf
+ option
[1]) > blen
)
419 LOG(2, s
, session
[s
].tunnel
, "PPP overflow for ConfigRej (proto %u, option %u).\n", mtype
, *option
);
423 memcpy(queued
, option
, option
[1]);
424 return queued
+ option
[1];
427 static uint8_t *ppp_conf_nak(sessionidt s
, uint8_t *buf
, size_t blen
, uint16_t mtype
,
428 uint8_t **response
, uint8_t *queued
, uint8_t *packet
, uint8_t *option
,
429 uint8_t *value
, size_t vlen
)
434 case PPPLCP
: nak_sent
= &sess_local
[s
].lcp
.nak_sent
; break;
435 case PPPIPCP
: nak_sent
= &sess_local
[s
].ipcp
.nak_sent
; break;
436 case PPPIPV6CP
: nak_sent
= &sess_local
[s
].ipv6cp
.nak_sent
; break;
437 default: return 0; // ?
440 if (*response
&& **response
!= ConfigNak
)
442 if (*nak_sent
< config
->ppp_max_failure
) // reject queued
445 return ppp_conf_rej(s
, buf
, blen
, mtype
, response
, 0, packet
, option
);
450 if (*nak_sent
>= config
->ppp_max_failure
)
451 return ppp_conf_rej(s
, buf
, blen
, mtype
, response
, 0, packet
, option
);
453 queued
= *response
= makeppp(buf
, blen
, packet
, 2, s
, session
[s
].tunnel
, mtype
, 0, 0, 0);
462 if ((queued
- buf
+ vlen
+ 2) > blen
)
464 LOG(2, s
, session
[s
].tunnel
, "PPP overflow for ConfigNak (proto %u, option %u).\n", mtype
, *option
);
469 *queued
++ = vlen
+ 2;
470 memcpy(queued
, value
, vlen
);
471 return queued
+ vlen
;
474 static void ppp_code_rej(sessionidt s
, tunnelidt t
, uint16_t proto
,
475 char *pname
, uint8_t *p
, uint16_t l
, uint8_t *buf
, size_t size
)
478 int mru
= session
[s
].mru
;
479 if (mru
< MINMTU
) mru
= MINMTU
;
480 if (mru
> size
) mru
= size
;
483 if (l
> mru
) l
= mru
;
485 q
= makeppp(buf
, size
, 0, 0, s
, t
, proto
, 0, 0, 0);
489 *(q
+ 1) = ++sess_local
[s
].lcp_ident
;
490 *(uint16_t *)(q
+ 2) = htons(l
);
491 memcpy(q
+ 4, p
, l
- 4);
493 LOG(2, s
, t
, "Unexpected %s code %s\n", pname
, ppp_code(*p
));
494 LOG(3, s
, t
, "%s: send %s\n", pname
, ppp_code(*q
));
495 if (config
->debug
> 3) dumplcp(q
, l
);
497 tunnelsend(buf
, l
+ (q
- buf
), t
);
500 // Process LCP messages
501 void processlcp(sessionidt s
, tunnelidt t
, uint8_t *p
, uint16_t l
)
509 LOG_HEX(5, "LCP", p
, l
);
512 LOG(1, s
, t
, "Short LCP %d bytes\n", l
);
513 STAT(tunnel_rx_errors
);
517 if ((hl
= ntohs(*(uint16_t *) (p
+ 2))) > l
)
519 LOG(1, s
, t
, "Length mismatch LCP %u/%u\n", hl
, l
);
520 STAT(tunnel_rx_errors
);
525 if (session
[s
].die
) // going down...
528 LOG((*p
== EchoReq
|| *p
== EchoReply
) ? 4 : 3, s
, t
,
529 "LCP: recv %s\n", ppp_code(*p
));
531 if (config
->debug
> 3) dumplcp(p
, l
);
536 uint8_t *o
= (p
+ 4);
544 if (length
== 0 || type
== 0 || x
< length
) break;
547 case 3: // Authentication-Protocol
549 int proto
= ntohs(*(uint16_t *)(o
+ 2));
552 else if (proto
== PPPCHAP
&& *(o
+ 4) == 5)
562 if (!session
[s
].ip
&& authtype
)
563 sess_local
[s
].lcp_authtype
= authtype
;
565 switch (session
[s
].ppp
.lcp
)
568 initialise_restart_count(s
, lcp
);
569 change_state(s
, lcp
, AckReceived
);
574 LOG(2, s
, t
, "LCP: ConfigAck in state %s? Sending ConfigReq\n", ppp_state(session
[s
].ppp
.lcp
));
575 if (session
[s
].ppp
.lcp
== Opened
)
579 change_state(s
, lcp
, RequestSent
);
587 LOG(2, s
, t
, "LCP: ignoring %s in state %s\n", ppp_code(*p
), ppp_state(session
[s
].ppp
.lcp
));
590 else if (*p
== ConfigReq
)
593 uint8_t *o
= (p
+ 4);
594 uint8_t *response
= 0;
595 static uint8_t asyncmap
[4] = { 0, 0, 0, 0 }; // all zero
596 static uint8_t authproto
[5];
604 if (length
== 0 || type
== 0 || x
< length
) break;
607 case 1: // Maximum-Receive-Unit
609 uint16_t mru
= ntohs(*(uint16_t *)(o
+ 2));
612 session
[s
].mru
= mru
;
617 LOG(3, s
, t
, " Remote requesting MRU of %u. Rejecting.\n", mru
);
619 q
= ppp_conf_nak(s
, b
, sizeof(b
), PPPLCP
, &response
, q
, p
, o
, (uint8_t *) &mru
, sizeof(mru
));
623 case 2: // Async-Control-Character-Map
624 if (!ntohl(*(uint32_t *)(o
+ 2))) // all bits zero is OK
627 LOG(3, s
, t
, " Remote requesting asyncmap. Rejecting.\n");
628 q
= ppp_conf_nak(s
, b
, sizeof(b
), PPPLCP
, &response
, q
, p
, o
, asyncmap
, sizeof(asyncmap
));
631 case 3: // Authentication-Protocol
633 int proto
= ntohs(*(uint16_t *)(o
+ 2));
634 char proto_name
[] = "0x0000";
639 if (config
->radius_authtypes
& AUTHPAP
)
641 sess_local
[s
].lcp_authtype
= AUTHPAP
;
645 strcpy(proto_name
, "PAP");
647 else if (proto
== PPPCHAP
)
649 if (config
->radius_authtypes
& AUTHCHAP
650 && *(o
+ 4) == 5) // MD5
652 sess_local
[s
].lcp_authtype
= AUTHCHAP
;
656 strcpy(proto_name
, "CHAP");
659 sprintf(proto_name
, "%#4.4x", proto
);
661 LOG(3, s
, t
, " Remote requesting %s authentication. Rejecting.\n", proto_name
);
663 alen
= add_lcp_auth(authproto
, sizeof(authproto
), config
->radius_authprefer
);
664 if (alen
< 2) break; // paranoia
666 q
= ppp_conf_nak(s
, b
, sizeof(b
), PPPLCP
, &response
, q
, p
, o
, authproto
+ 2, alen
- 2);
667 if (q
&& *response
== ConfigNak
&&
668 config
->radius_authtypes
!= config
->radius_authprefer
)
671 alen
= add_lcp_auth(authproto
, sizeof(authproto
), config
->radius_authtypes
& ~config
->radius_authprefer
);
673 q
= ppp_conf_nak(s
, b
, sizeof(b
), PPPLCP
, &response
, q
, p
, o
, authproto
+ 2, alen
- 2);
680 case 4: // Quality-Protocol
681 case 5: // Magic-Number
682 case 7: // Protocol-Field-Compression
683 case 8: // Address-And-Control-Field-Compression
686 case 17: // Multilink Max-Receive-Reconstructed-Unit
688 uint16_t mrru
= ntohs(*(uint16_t *)(o
+ 2));
689 session
[s
].mrru
= mrru
;
691 LOG(3, s
, t
, " Received PPP LCP option MRRU: %d\n",mrru
);
695 case 18: // Multilink Short Sequence Number Header Format
699 LOG(3, s
, t
, " Received PPP LCP option MSSN format\n");
703 case 19: // Multilink Endpoint Discriminator
705 uint8_t epdis_class
= o
[2];
708 session
[s
].epdis
.addr_class
= epdis_class
;
709 session
[s
].epdis
.length
= length
- 3;
710 if (session
[s
].epdis
.length
> 20)
712 LOG(1, s
, t
, "Error: received EndDis Address Length more than 20: %d\n", session
[s
].epdis
.length
);
713 session
[s
].epdis
.length
= 20;
716 for (addr
= 0; addr
< session
[s
].epdis
.length
; addr
++)
717 session
[s
].epdis
.address
[addr
] = o
[3+addr
];
724 LOG(3, s
, t
, " Received PPP LCP option Multilink EndDis Local Address Class: %d\n",epdis_class
);
727 LOG(3, s
, t
, " Received PPP LCP option Multilink EndDis IP Address Class: %d\n",epdis_class
);
730 LOG(3, s
, t
, " Received PPP LCP option Multilink EndDis IEEE MAC Address Class: %d\n",epdis_class
);
733 LOG(3, s
, t
, " Received PPP LCP option Multilink EndDis PPP Magic No Class: %d\n",epdis_class
);
736 LOG(3, s
, t
, " Received PPP LCP option Multilink EndDis PSND No Class: %d\n",epdis_class
);
739 LOG(3, s
, t
, " Received PPP LCP option Multilink EndDis NULL Class %d\n",epdis_class
);
744 default: // Reject any unknown options
745 LOG(3, s
, t
, " Rejecting unknown PPP LCP option %d\n", type
);
746 q
= ppp_conf_rej(s
, b
, sizeof(b
), PPPLCP
, &response
, q
, p
, o
);
753 cluster_send_session(s
);
757 l
= q
- response
; // LCP packet length
758 *((uint16_t *) (response
+ 2)) = htons(l
); // update header
762 // Send packet back as ConfigAck
763 response
= makeppp(b
, sizeof(b
), p
, l
, s
, t
, PPPLCP
, 0, 0, 0);
764 if (!response
) return;
765 *response
= ConfigAck
;
768 switch (session
[s
].ppp
.lcp
)
771 response
= makeppp(b
, sizeof(b
), p
, 2, s
, t
, PPPLCP
, 0, 0, 0);
772 if (!response
) return;
773 *response
= TerminateAck
;
774 *((uint16_t *) (response
+ 2)) = htons(l
= 4);
778 initialise_restart_count(s
, lcp
);
780 if (*response
== ConfigAck
)
781 change_state(s
, lcp
, AckSent
);
783 change_state(s
, lcp
, RequestSent
);
788 if (*response
== ConfigAck
)
789 change_state(s
, lcp
, AckSent
);
794 if (*response
== ConfigAck
)
805 if (*response
== ConfigAck
)
806 change_state(s
, lcp
, AckSent
);
808 change_state(s
, lcp
, RequestSent
);
813 LOG(2, s
, t
, "LCP: ignoring %s in state %s\n", ppp_code(*p
), ppp_state(session
[s
].ppp
.lcp
));
817 LOG(3, s
, t
, "LCP: send %s\n", ppp_code(*response
));
818 if (config
->debug
> 3) dumplcp(response
, l
);
820 tunnelsend(b
, l
+ (response
- b
), t
);
822 else if (*p
== ConfigNak
|| *p
== ConfigRej
)
825 uint8_t *o
= (p
+ 4);
833 if (length
== 0 || type
== 0 || x
< length
) break;
836 case 1: // Maximum-Receive-Unit
839 if (length
< 4) break;
840 sess_local
[s
].ppp_mru
= ntohs(*(uint16_t *)(o
+ 2));
841 LOG(3, s
, t
, " Remote requested MRU of %u\n", sess_local
[s
].ppp_mru
);
845 sess_local
[s
].ppp_mru
= 0;
846 LOG(3, s
, t
, " Remote rejected MRU negotiation\n");
851 case 3: // Authentication-Protocol
859 if (length
< 4) break;
860 proto
= ntohs(*(uint16_t *)(o
+ 2));
864 authtype
= config
->radius_authtypes
& AUTHPAP
;
865 LOG(3, s
, t
, " Remote requested PAP authentication...%sing\n",
866 authtype
? "accept" : "reject");
868 else if (proto
== PPPCHAP
&& length
> 4 && *(o
+ 4) == 5)
870 authtype
= config
->radius_authtypes
& AUTHCHAP
;
871 LOG(3, s
, t
, " Remote requested CHAP authentication...%sing\n",
872 authtype
? "accept" : "reject");
876 LOG(3, s
, t
, " Rejecting unsupported authentication %#4x\n",
882 LOG(2, s
, t
, "LCP: remote rejected auth negotiation\n");
883 authtype
= 0; // shutdown
888 case 5: // Magic-Number
889 session
[s
].magic
= 0;
892 if (length
< 6) break;
893 session
[s
].magic
= ntohl(*(uint32_t *)(o
+ 2));
896 if (session
[s
].magic
)
897 LOG(3, s
, t
, " Remote requested magic-no %x\n", session
[s
].magic
);
899 LOG(3, s
, t
, " Remote rejected magic-no\n");
901 cluster_send_session(s
);
904 case 17: // Multilink Max-Receive-Reconstructed-Unit
908 sess_local
[s
].mp_mrru
= ntohs(*(uint16_t *)(o
+ 2));
909 LOG(3, s
, t
, " Remote requested MRRU of %u\n", sess_local
[s
].mp_mrru
);
913 sess_local
[s
].mp_mrru
= 0;
914 LOG(3, s
, t
, " Remote rejected MRRU negotiation\n");
919 case 18: // Multilink Short Sequence Number Header Format
923 sess_local
[s
].mp_mssf
= 0;
924 LOG(3, s
, t
, " Remote requested Naked mssf\n");
928 sess_local
[s
].mp_mssf
= 0;
929 LOG(3, s
, t
, " Remote rejected mssf\n");
934 case 19: // Multilink Endpoint Discriminator
938 LOG(2, s
, t
, " Remote should not configNak Endpoint Dis!\n");
942 sess_local
[s
].mp_epdis
= 0;
943 LOG(3, s
, t
, " Remote rejected Endpoint Discriminator\n");
949 LOG(2, s
, t
, "LCP: remote sent %s for type %u?\n", ppp_code(*p
), type
);
950 sessionshutdown(s
, "Unable to negotiate LCP.", CDN_ADMIN_DISC
, TERM_USER_ERROR
);
959 sessionshutdown(s
, "Unsupported authentication.", CDN_ADMIN_DISC
, TERM_USER_ERROR
);
964 sess_local
[s
].lcp_authtype
= authtype
;
966 switch (session
[s
].ppp
.lcp
)
971 uint8_t *response
= makeppp(b
, sizeof(b
), p
, 2, s
, t
, PPPLCP
, 0, 0, 0);
972 if (!response
) return;
973 *response
= TerminateAck
;
974 *((uint16_t *) (response
+ 2)) = htons(l
= 4);
976 LOG(3, s
, t
, "LCP: send %s\n", ppp_code(*response
));
977 if (config
->debug
> 3) dumplcp(response
, l
);
979 tunnelsend(b
, l
+ (response
- b
), t
);
985 initialise_restart_count(s
, lcp
);
990 LOG(2, s
, t
, "LCP: ConfigNak in state %s? Sending ConfigReq\n", ppp_state(session
[s
].ppp
.lcp
));
1000 LOG(2, s
, t
, "LCP: ignoring %s in state %s\n", ppp_code(*p
), ppp_state(session
[s
].ppp
.lcp
));
1004 else if (*p
== TerminateReq
)
1006 switch (session
[s
].ppp
.lcp
)
1019 zero_restart_count(s
, lcp
);
1020 change_state(s
, lcp
, Closing
);
1024 LOG(2, s
, t
, "LCP: ignoring %s in state %s\n", ppp_code(*p
), ppp_state(session
[s
].ppp
.lcp
));
1028 *p
= TerminateAck
; // send ack
1029 q
= makeppp(b
, sizeof(b
), p
, l
, s
, t
, PPPLCP
, 0, 0, 0);
1032 LOG(3, s
, t
, "LCP: send %s\n", ppp_code(*q
));
1033 if (config
->debug
> 3) dumplcp(q
, l
);
1035 tunnelsend(b
, l
+ (q
- b
), t
); // send it
1037 else if (*p
== ProtocolRej
)
1044 if (l
> 5 && !(proto
& 1))
1051 if (proto
== PPPIPV6CP
)
1053 LOG(3, s
, t
, "IPv6 rejected\n");
1054 change_state(s
, ipv6cp
, Closed
);
1058 LOG(3, s
, t
, "LCP protocol reject: 0x%04X\n", proto
);
1061 else if (*p
== EchoReq
)
1063 *p
= EchoReply
; // reply
1064 *(uint32_t *) (p
+ 4) = htonl(session
[s
].magic
); // our magic number
1065 q
= makeppp(b
, sizeof(b
), p
, l
, s
, t
, PPPLCP
, 0, 0, 0);
1068 LOG(4, s
, t
, "LCP: send %s\n", ppp_code(*q
));
1069 if (config
->debug
> 3) dumplcp(q
, l
);
1071 tunnelsend(b
, l
+ (q
- b
), t
); // send it
1073 else if (*p
== EchoReply
)
1075 // Ignore it, last_packet time is set earlier than this.
1077 else if (*p
!= CodeRej
)
1079 ppp_code_rej(s
, t
, PPPLCP
, "LCP", p
, l
, b
, sizeof(b
));
1083 int join_bundle(sessionidt s
)
1085 // Search for a bundle to join
1088 for (i
= 1; i
< MAXBUNDLE
; i
++)
1090 if (bundle
[i
].state
!= BUNDLEFREE
)
1092 if (epdiscmp(session
[s
].epdis
,bundle
[i
].epdis
) && !strcmp(session
[s
].user
, bundle
[i
].user
))
1094 session
[s
].bundle
= i
;
1095 bundle
[i
].mrru
= session
[s
].mrru
;
1096 bundle
[i
].mssf
= session
[s
].mssf
;
1097 if (session
[s
].epdis
.length
> 0)
1098 setepdis(&bundle
[i
].epdis
, session
[s
].epdis
);
1100 strcpy(bundle
[i
].user
, session
[s
].user
);
1101 bundle
[i
].members
[bundle
[i
].num_of_links
] = s
;
1102 bundle
[i
].num_of_links
++;
1103 LOG(3, s
, session
[s
].tunnel
, "MPPP: Bundling additional line in bundle (%d), lines:%d\n",i
,bundle
[i
].num_of_links
);
1109 // No previously created bundle was found for this session, so create a new one
1110 if (!(b
= new_bundle())) return 0;
1112 session
[s
].bundle
= b
;
1113 bundle
[b
].mrru
= session
[s
].mrru
;
1114 bundle
[b
].mssf
= session
[s
].mssf
;
1115 if (session
[s
].epdis
.length
> 0)
1116 setepdis(&bundle
[b
].epdis
, session
[s
].epdis
);
1118 strcpy(bundle
[b
].user
, session
[s
].user
);
1119 bundle
[b
].members
[0] = s
;
1120 LOG(3, s
, session
[s
].tunnel
, "MPPP: Created a new bundle (%d)\n", b
);
1124 static int epdiscmp(epdist ep1
, epdist ep2
)
1127 if (ep1
.length
!= ep2
.length
)
1130 if (ep1
.addr_class
!= ep2
.addr_class
)
1133 for (ad
= 0; ad
< ep1
.length
; ad
++)
1134 if (ep1
.address
[ad
] != ep2
.address
[ad
])
1140 static void setepdis(epdist
*ep1
, epdist ep2
)
1143 ep1
->length
= ep2
.length
;
1144 ep1
->addr_class
= ep2
.addr_class
;
1145 for (ad
= 0; ad
< ep2
.length
; ad
++)
1146 ep1
->address
[ad
] = ep2
.address
[ad
];
1149 static bundleidt
new_bundle()
1152 for (i
= 1; i
< MAXBUNDLE
; i
++)
1154 if (bundle
[i
].state
== BUNDLEFREE
)
1156 LOG(4, 0, 0, "MPPP: Assigning bundle ID %d\n", i
);
1157 bundle
[i
].num_of_links
= 1;
1158 bundle
[i
].last_check
= time_now
; // Initialize last_check value
1159 bundle
[i
].state
= BUNDLEOPEN
;
1160 bundle
[i
].current_ses
= -1; // This is to enforce the first session 0 to be used at first
1161 if (i
> config
->cluster_highest_bundleid
)
1162 config
->cluster_highest_bundleid
= i
;
1166 LOG(0, 0, 0, "MPPP: Can't find a free bundle! There shouldn't be this many in use!\n");
1170 static void ipcp_open(sessionidt s
, tunnelidt t
)
1172 LOG(3, s
, t
, "IPCP: Opened, session is now active\n");
1174 change_state(s
, ipcp
, Opened
);
1176 if (!(session
[s
].walled_garden
|| session
[s
].flags
& SESSION_STARTED
))
1178 uint16_t r
= radiusnew(s
);
1181 radiussend(r
, RADIUSSTART
); // send radius start
1183 // don't send further Start records if IPCP is restarted
1184 session
[s
].flags
|= SESSION_STARTED
;
1185 cluster_send_session(s
);
1189 // start IPv6 if configured and still in passive state
1190 if (session
[s
].ppp
.ipv6cp
== Stopped
)
1193 change_state(s
, ipv6cp
, RequestSent
);
1197 // Process IPCP messages
1198 void processipcp(sessionidt s
, tunnelidt t
, uint8_t *p
, uint16_t l
)
1200 uint8_t b
[MAXETHER
];
1206 LOG_HEX(5, "IPCP", p
, l
);
1209 LOG(1, s
, t
, "Short IPCP %d bytes\n", l
);
1210 STAT(tunnel_rx_errors
);
1214 if ((hl
= ntohs(*(uint16_t *) (p
+ 2))) > l
)
1216 LOG(1, s
, t
, "Length mismatch IPCP %u/%u\n", hl
, l
);
1217 STAT(tunnel_rx_errors
);
1222 if (session
[s
].ppp
.phase
< Network
)
1224 LOG(2, s
, t
, "IPCP %s ignored in %s phase\n", ppp_code(*p
), ppp_phase(session
[s
].ppp
.phase
));
1228 LOG(3, s
, t
, "IPCP: recv %s\n", ppp_code(*p
));
1230 if (*p
== ConfigAck
)
1232 switch (session
[s
].ppp
.ipcp
)
1235 initialise_restart_count(s
, ipcp
);
1236 change_state(s
, ipcp
, AckReceived
);
1241 LOG(2, s
, t
, "IPCP: ConfigAck in state %s? Sending ConfigReq\n", ppp_state(session
[s
].ppp
.ipcp
));
1243 change_state(s
, ipcp
, RequestSent
);
1251 LOG(2, s
, t
, "IPCP: ignoring %s in state %s\n", ppp_code(*p
), ppp_state(session
[s
].ppp
.ipcp
));
1254 else if (*p
== ConfigReq
)
1256 uint8_t *response
= 0;
1264 if (!o
[1] || o
[1] > length
) return;
1268 case 3: // ip address
1269 gotip
++; // seen address
1270 if (o
[1] != 6) return;
1272 addr
= htonl(session
[s
].ip
);
1273 if (memcmp(o
+ 2, &addr
, (sizeof addr
)))
1276 q
= ppp_conf_nak(s
, b
, sizeof(b
), PPPIPCP
, &response
, q
, p
, o
, (uint8_t *) &addr
, sizeof(addr
));
1277 if (!q
|| (q
!= oq
&& *response
== ConfigRej
))
1279 sessionshutdown(s
, "Can't negotiate IPCP.", CDN_ADMIN_DISC
, TERM_USER_ERROR
);
1286 case 129: // primary DNS
1287 if (o
[1] != 6) return;
1289 addr
= htonl(session
[s
].dns1
);
1290 if (memcmp(o
+ 2, &addr
, (sizeof addr
)))
1292 q
= ppp_conf_nak(s
, b
, sizeof(b
), PPPIPCP
, &response
, q
, p
, o
, (uint8_t *) &addr
, sizeof(addr
));
1298 case 131: // secondary DNS
1299 if (o
[1] != 6) return;
1301 addr
= htonl(session
[s
].dns2
);
1302 if (memcmp(o
+ 2, &addr
, sizeof(addr
)))
1304 q
= ppp_conf_nak(s
, b
, sizeof(b
), PPPIPCP
, &response
, q
, p
, o
, (uint8_t *) &addr
, sizeof(addr
));
1311 LOG(2, s
, t
, " Rejecting PPP IPCP Option type %d\n", *o
);
1312 q
= ppp_conf_rej(s
, b
, sizeof(b
), PPPIPCP
, &response
, q
, p
, o
);
1322 l
= q
- response
; // IPCP packet length
1323 *((uint16_t *) (response
+ 2)) = htons(l
); // update header
1327 // Send packet back as ConfigAck
1328 response
= makeppp(b
, sizeof(b
), p
, l
, s
, t
, PPPIPCP
, 0, 0, 0);
1329 if (!response
) return;
1330 *response
= ConfigAck
;
1334 LOG(1, s
, t
, "No IP in IPCP request\n");
1335 STAT(tunnel_rx_errors
);
1339 switch (session
[s
].ppp
.ipcp
)
1342 response
= makeppp(b
, sizeof(b
), p
, 2, s
, t
, PPPIPCP
, 0, 0, 0);
1343 if (!response
) return;
1344 *response
= TerminateAck
;
1345 *((uint16_t *) (response
+ 2)) = htons(l
= 4);
1349 initialise_restart_count(s
, ipcp
);
1351 if (*response
== ConfigAck
)
1352 change_state(s
, ipcp
, AckSent
);
1354 change_state(s
, ipcp
, RequestSent
);
1359 if (*response
== ConfigAck
)
1360 change_state(s
, ipcp
, AckSent
);
1365 if (*response
== ConfigAck
)
1371 initialise_restart_count(s
, ipcp
);
1376 if (*response
== ConfigAck
)
1377 change_state(s
, ipcp
, AckSent
);
1379 change_state(s
, ipcp
, RequestSent
);
1384 LOG(2, s
, t
, "IPCP: ignoring %s in state %s\n", ppp_code(*p
), ppp_state(session
[s
].ppp
.ipcp
));
1388 LOG(3, s
, t
, "IPCP: send %s\n", ppp_code(*response
));
1389 tunnelsend(b
, l
+ (response
- b
), t
);
1391 else if (*p
== TerminateReq
)
1393 switch (session
[s
].ppp
.ipcp
)
1405 zero_restart_count(s
, ipcp
);
1406 change_state(s
, ipcp
, Closing
);
1410 LOG(2, s
, t
, "IPCP: ignoring %s in state %s\n", ppp_code(*p
), ppp_state(session
[s
].ppp
.ipcp
));
1414 *p
= TerminateAck
; // send ack
1415 q
= makeppp(b
, sizeof(b
), p
, l
, s
, t
, PPPIPCP
, 0, 0, 0);
1418 LOG(3, s
, t
, "IPCP: send %s\n", ppp_code(*q
));
1419 tunnelsend(b
, l
+ (q
- b
), t
); // send it
1421 else if (*p
!= CodeRej
)
1423 ppp_code_rej(s
, t
, PPPIPCP
, "IPCP", p
, l
, b
, sizeof(b
));
1427 static void ipv6cp_open(sessionidt s
, tunnelidt t
)
1429 LOG(3, s
, t
, "IPV6CP: Opened\n");
1431 change_state(s
, ipv6cp
, Opened
);
1432 if (session
[s
].ipv6prefixlen
)
1433 route6set(s
, session
[s
].ipv6route
, session
[s
].ipv6prefixlen
, 1);
1435 // Send an initial RA (TODO: Should we send these regularly?)
1436 send_ipv6_ra(s
, t
, NULL
);
1439 // Process IPV6CP messages
1440 void processipv6cp(sessionidt s
, tunnelidt t
, uint8_t *p
, uint16_t l
)
1442 uint8_t b
[MAXETHER
];
1446 CSTAT(processipv6cp
);
1448 LOG_HEX(5, "IPV6CP", p
, l
);
1451 LOG(1, s
, t
, "Short IPV6CP %d bytes\n", l
);
1452 STAT(tunnel_rx_errors
);
1456 if ((hl
= ntohs(*(uint16_t *) (p
+ 2))) > l
)
1458 LOG(1, s
, t
, "Length mismatch IPV6CP %u/%u\n", hl
, l
);
1459 STAT(tunnel_rx_errors
);
1464 if (session
[s
].ppp
.phase
< Network
)
1466 LOG(2, s
, t
, "IPV6CP %s ignored in %s phase\n", ppp_code(*p
), ppp_phase(session
[s
].ppp
.phase
));
1470 LOG(3, s
, t
, "IPV6CP: recv %s\n", ppp_code(*p
));
1474 LOG(3, s
, t
, "IPV6CP: no IPv4 address (IPCP in state %s)\n", ppp_state(session
[s
].ppp
.ipcp
));
1475 return; // need IPCP to complete...
1478 if (*p
== ConfigAck
)
1480 switch (session
[s
].ppp
.ipv6cp
)
1483 initialise_restart_count(s
, ipv6cp
);
1484 change_state(s
, ipv6cp
, AckReceived
);
1489 LOG(2, s
, t
, "IPV6CP: ConfigAck in state %s? Sending ConfigReq\n", ppp_state(session
[s
].ppp
.ipv6cp
));
1491 change_state(s
, ipv6cp
, RequestSent
);
1499 LOG(2, s
, t
, "IPV6CP: ignoring %s in state %s\n", ppp_code(*p
), ppp_state(session
[s
].ppp
.ipv6cp
));
1502 else if (*p
== ConfigReq
)
1504 uint8_t *response
= 0;
1512 if (!o
[1] || o
[1] > length
) return;
1516 case 1: // interface identifier
1517 gotip
++; // seen address
1518 if (o
[1] != 10) return;
1520 *(uint32_t *) ident
= htonl(session
[s
].ip
);
1521 *(uint32_t *) (ident
+ 4) = 0;
1523 if (memcmp(o
+ 2, ident
, sizeof(ident
)))
1525 q
= ppp_conf_nak(s
, b
, sizeof(b
), PPPIPV6CP
, &response
, q
, p
, o
, ident
, sizeof(ident
));
1532 LOG(2, s
, t
, " Rejecting PPP IPV6CP Option type %d\n", *o
);
1533 q
= ppp_conf_rej(s
, b
, sizeof(b
), PPPIPV6CP
, &response
, q
, p
, o
);
1543 l
= q
- response
; // IPV6CP packet length
1544 *((uint16_t *) (response
+ 2)) = htons(l
); // update header
1548 // Send packet back as ConfigAck
1549 response
= makeppp(b
, sizeof(b
), p
, l
, s
, t
, PPPIPV6CP
, 0, 0, 0);
1550 if (!response
) return;
1551 *response
= ConfigAck
;
1555 LOG(1, s
, t
, "No interface identifier in IPV6CP request\n");
1556 STAT(tunnel_rx_errors
);
1560 switch (session
[s
].ppp
.ipv6cp
)
1563 response
= makeppp(b
, sizeof(b
), p
, 2, s
, t
, PPPIPV6CP
, 0, 0, 0);
1564 if (!response
) return;
1565 *response
= TerminateAck
;
1566 *((uint16_t *) (response
+ 2)) = htons(l
= 4);
1570 initialise_restart_count(s
, ipv6cp
);
1572 if (*response
== ConfigAck
)
1573 change_state(s
, ipv6cp
, AckSent
);
1575 change_state(s
, ipv6cp
, RequestSent
);
1580 if (*response
== ConfigAck
)
1581 change_state(s
, ipv6cp
, AckSent
);
1586 if (*response
== ConfigAck
)
1592 initialise_restart_count(s
, ipv6cp
);
1597 if (*response
== ConfigAck
)
1598 change_state(s
, ipv6cp
, AckSent
);
1600 change_state(s
, ipv6cp
, RequestSent
);
1605 LOG(2, s
, t
, "IPV6CP: ignoring %s in state %s\n", ppp_code(*p
), ppp_state(session
[s
].ppp
.ipv6cp
));
1609 LOG(3, s
, t
, "IPV6CP: send %s\n", ppp_code(*response
));
1610 tunnelsend(b
, l
+ (response
- b
), t
);
1612 else if (*p
== TerminateReq
)
1614 switch (session
[s
].ppp
.ipv6cp
)
1626 zero_restart_count(s
, ipv6cp
);
1627 change_state(s
, ipv6cp
, Closing
);
1631 LOG(2, s
, t
, "IPV6CP: ignoring %s in state %s\n", ppp_code(*p
), ppp_state(session
[s
].ppp
.ipv6cp
));
1635 *p
= TerminateAck
; // send ack
1636 q
= makeppp(b
, sizeof(b
), p
, l
, s
, t
, PPPIPV6CP
, 0, 0, 0);
1639 LOG(3, s
, t
, "IPV6CP: send %s\n", ppp_code(*q
));
1640 tunnelsend(b
, l
+ (q
- b
), t
); // send it
1642 else if (*p
!= CodeRej
)
1644 ppp_code_rej(s
, t
, PPPIPV6CP
, "IPV6CP", p
, l
, b
, sizeof(b
));
1648 // process IP packet received
1650 // This MUST be called with at least 4 byte behind 'p'.
1651 // (i.e. this routine writes to p[-4]).
1652 void processipin(sessionidt s
, tunnelidt t
, uint8_t *p
, uint16_t l
)
1658 LOG_HEX(5, "IP", p
, l
);
1662 LOG(1, s
, t
, "IP packet too short %d\n", l
);
1663 STAT(tunnel_rx_errors
);
1667 ip
= ntohl(*(uint32_t *)(p
+ 12));
1671 LOG(1, s
, t
, "IP packet too long %d\n", l
);
1672 STAT(tunnel_rx_errors
);
1676 if (session
[s
].ppp
.phase
!= Network
|| session
[s
].ppp
.ipcp
!= Opened
)
1679 if (!session
[s
].bundle
|| bundle
[session
[s
].bundle
].num_of_links
< 2) // FIXME:
1681 // no spoof (do sessionbyip to handled statically routed subnets)
1682 if (ip
!= session
[s
].ip
&& sessionbyip(htonl(ip
)) != s
)
1684 LOG(4, s
, t
, "Dropping packet with spoofed IP %s\n", fmtaddr(htonl(ip
), 0));
1689 // run access-list if any
1690 if (session
[s
].filter_in
&& !ip_filter(p
, l
, session
[s
].filter_in
- 1))
1693 // adjust MSS on SYN and SYN,ACK packets with options
1694 if ((ntohs(*(uint16_t *) (p
+ 6)) & 0x1fff) == 0 && p
[9] == IPPROTO_TCP
) // first tcp fragment
1696 int ihl
= (p
[0] & 0xf) * 4; // length of IP header
1697 if (l
>= ihl
+ 20 && (p
[ihl
+ 13] & TCP_FLAG_SYN
) && ((p
[ihl
+ 12] >> 4) > 5))
1698 adjust_tcp_mss(s
, t
, p
, l
, p
+ ihl
);
1701 // Add on the tun header
1703 *(uint32_t *) p
= htonl(PKTIP
);
1706 if (session
[s
].tbf_in
)
1708 // Are we throttling this session?
1709 if (config
->cluster_iam_master
)
1710 tbf_queue_packet(session
[s
].tbf_in
, p
, l
);
1712 master_throttle_packet(session
[s
].tbf_in
, p
, l
);
1717 if (tun_write(p
, l
) < 0)
1719 STAT(tun_tx_errors
);
1720 LOG(0, s
, t
, "Error writing %d bytes to TUN device: %s (tunfd=%d, p=%p)\n",
1721 l
, strerror(errno
), tunfd
, p
);
1729 if (session
[s
].snoop_ip
&& session
[s
].snoop_port
)
1731 // Snooping this session
1732 snoop_send_packet(p
, l
, session
[s
].snoop_ip
, session
[s
].snoop_port
);
1735 increment_counter(&session
[s
].cin
, &session
[s
].cin_wrap
, l
);
1736 session
[s
].cin_delta
+= l
;
1739 sess_local
[s
].cin
+= l
;
1740 sess_local
[s
].pin
++;
1744 STAT(tun_tx_packets
);
1745 INC_STAT(tun_tx_bytes
, l
);
1748 // process Multilink PPP packet received
1749 void processmpin(sessionidt s
, tunnelidt t
, uint8_t *p
, uint16_t l
)
1751 bundleidt b
= session
[s
].bundle
;
1752 uint8_t begin_frame
;
1759 LOG(3, s
, t
, "MPPP: Invalid bundle id: 0\n");
1763 begin_frame
= (*p
& 0x80);
1764 end_frame
= (*p
& 0x40);
1765 if (session
[s
].mssf
)
1767 // Get 12 bit for seq number
1768 uint16_t short_seq_num
= ntohs((*(uint16_t *) p
) & 0xFF0F);
1769 uint16_t short_seq_num2
= short_seq_num
>> 4;
1772 seq_num
= short_seq_num2
;
1773 // After this point the pointer should be advanced 2 bytes
1774 LOG(3, s
, t
, "MPPP: 12 bits, sequence number: %d, short1: %d, short2: %d\n",seq_num
, short_seq_num
, short_seq_num2
);
1778 // Get 24 bit for seq number
1780 seq_num
= ntohl((*(uint32_t *) p
) & 0xFFFFFF00);
1781 seq_num
= seq_num
>> 8;
1784 // After this point the pointer should be advanced 4 bytes
1785 LOG(4, s
, t
, "MPPP: 24 bits sequence number:%d\n",seq_num
);
1788 if (seq_num
- bundle
[b
].offset
< 0)
1790 bundle
[b
].offset
= 0;
1791 bundle
[b
].pending_frag
= 0;
1794 offset
= bundle
[b
].offset
;
1797 // Check for previous non-assembled frames
1799 if (bundle
[b
].pending_frag
)
1801 uint32_t fn
= bundle
[b
].seq_num_m
- offset
;
1803 bundle
[b
].pending_frag
= 0;
1804 // Check for array indexes
1805 if (fn
< 0 || fn
> MAXFRAGNUM
)
1807 LOG(2, s
, t
, "ERROR: Index out of range fn:%d, bundle:%d\n",fn
,b
);
1811 if (seq_num
-offset
< 0 || seq_num
-offset
> MAXFRAGNUM
)
1813 LOG(2, s
, t
, "ERROR: Index out of range fn(last):%d, bundle:%d\n",fn
,b
);
1816 /////////////////////////////////////////////////////
1817 cur_len
= 4; // This is set to 4 to leave 4 bytes for function processipin
1818 for (fn
= bundle
[b
].seq_num_m
- offset
; fn
< seq_num
- offset
; fn
++)
1820 if (!frag
[b
].fragment
[fn
].length
)
1822 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
);
1827 if (cur_len
+ frag
[b
].fragment
[fn
].length
> MAXETHER
)
1829 LOG(2, s
, t
, "MPPP: ERROR: very long frame after assembling %d\n", frag
[b
].fragment
[fn
].length
+cur_len
);
1834 memcpy(frag
[b
].reassembled_frame
+cur_len
, frag
[b
].fragment
[fn
].data
, frag
[b
].fragment
[fn
].length
);
1835 cur_len
+= frag
[b
].fragment
[fn
].length
;
1836 frag
[b
].fragment
[fn
].length
= 0; // Indicates that this fragment has been consumed
1837 // This is usefull for compression
1838 memset(frag
[b
].fragment
[fn
].data
, 0, sizeof(frag
[b
].fragment
[fn
].data
));
1843 frag
[b
].re_frame_len
= cur_len
;
1844 // Process the resassembled frame
1845 LOG(4, s
, t
, "MPPP: Process the reassembled frame, len=%d\n",cur_len
);
1846 processmpframe(s
, t
, frag
[b
].reassembled_frame
, frag
[b
].re_frame_len
, 1);
1847 // Set reassembled frame length to zero after processing it
1848 frag
[b
].re_frame_len
= 0;
1849 memset(frag
[b
].reassembled_frame
, 0, sizeof(frag
[b
].reassembled_frame
));
1852 //////////////////////////////////////////
1853 bundle
[b
].seq_num_m
= seq_num
;
1856 // Both bits are set
1857 LOG(4, s
, t
, "MPPP: Both bits are set (Begin and End).\n");
1858 processmpframe(s
, t
, p
, l
, 0);
1859 // The maximum number of fragments is 1500
1860 if (seq_num
- bundle
[b
].offset
>= 1400)
1862 bundle
[b
].offset
= seq_num
;
1863 LOG(4, s
, t
, "MPPP: Setting offset to: %d\n",bundle
[b
].offset
);
1868 bundle
[b
].pending_frag
= 1;
1870 LOG(4, s
, t
, "MPPP: Push to receive buffer\n");
1871 // Push to the receive buffer
1872 // Array indexes checking
1873 if (seq_num
-offset
< 0 || seq_num
-offset
>= MAXFRAGNUM
)
1875 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
);
1878 // Perform length checking
1881 LOG(2, s
, t
, "MPPP: ERROR: very long fragment length (1)\n");
1884 frag
[b
].fragment
[seq_num
- offset
].length
= l
;
1885 memcpy(frag
[b
].fragment
[seq_num
- offset
].data
, p
, l
);
1890 LOG(4, s
, t
, "MPPP: Push to receive buffer\n");
1891 // Push to the receive buffer
1892 // Array indexes checking
1893 if (seq_num
-offset
< 0 || seq_num
-offset
>= MAXFRAGNUM
)
1895 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
);
1898 // Perform length checking
1901 LOG(2, s
, t
, "MPPP: ERROR: very long fragment length (2).\n");
1904 frag
[b
].fragment
[seq_num
- offset
].length
= l
;
1905 memcpy(frag
[b
].fragment
[seq_num
- offset
].data
, p
, l
);
1909 // process IPv6 packet received
1911 // This MUST be called with at least 4 byte behind 'p'.
1912 // (i.e. this routine writes to p[-4]).
1913 void processipv6in(sessionidt s
, tunnelidt t
, uint8_t *p
, uint16_t l
)
1918 CSTAT(processipv6in
);
1920 LOG_HEX(5, "IPv6", p
, l
);
1922 ip
= *(struct in6_addr
*) (p
+ 8);
1923 ipv4
= ntohl(*(uint32_t *)(p
+ 16));
1927 LOG(1, s
, t
, "IP packet too long %d\n", l
);
1928 STAT(tunnel_rx_errors
);
1932 if (session
[s
].ppp
.phase
!= Network
|| session
[s
].ppp
.ipv6cp
!= Opened
)
1936 if (ipv4
!= session
[s
].ip
&& memcmp(&config
->ipv6_prefix
, &ip
, 8) && sessionbyipv6(ip
) != s
)
1938 char str
[INET6_ADDRSTRLEN
];
1939 LOG(5, s
, t
, "Dropping packet with spoofed IP %s\n",
1940 inet_ntop(AF_INET6
, &ip
, str
, INET6_ADDRSTRLEN
));
1944 // Check if it's a Router Solicition message.
1945 if (*(p
+ 6) == 58 && *(p
+ 7) == 255 && *(p
+ 24) == 0xFF && *(p
+ 25) == 2 &&
1946 *(uint32_t *)(p
+ 26) == 0 && *(uint32_t *)(p
+ 30) == 0 &&
1947 *(uint32_t *)(p
+ 34) == 0 &&
1948 *(p
+ 38) == 0 && *(p
+ 39) == 2 && *(p
+ 40) == 133) {
1949 LOG(3, s
, t
, "Got IPv6 RS\n");
1950 send_ipv6_ra(s
, t
, &ip
);
1954 // Add on the tun header
1956 *(uint32_t *) p
= htonl(PKTIPV6
);
1959 // Are we throttled and a slave?
1960 if (session
[s
].tbf_in
&& !config
->cluster_iam_master
) {
1961 // Pass it to the master for handling.
1962 master_throttle_packet(session
[s
].tbf_in
, p
, l
);
1966 // Are we throttled and a master??
1967 if (session
[s
].tbf_in
&& config
->cluster_iam_master
) {
1968 // Actually handle the throttled packets.
1969 tbf_queue_packet(session
[s
].tbf_in
, p
, l
);
1974 if (tun_write(p
, l
) < 0)
1976 STAT(tun_tx_errors
);
1977 LOG(0, s
, t
, "Error writing %d bytes to TUN device: %s (tunfd=%d, p=%p)\n",
1978 l
, strerror(errno
), tunfd
, p
);
1986 if (session
[s
].snoop_ip
&& session
[s
].snoop_port
)
1988 // Snooping this session
1989 snoop_send_packet(p
, l
, session
[s
].snoop_ip
, session
[s
].snoop_port
);
1992 increment_counter(&session
[s
].cin
, &session
[s
].cin_wrap
, l
);
1993 session
[s
].cin_delta
+= l
;
1996 sess_local
[s
].cin
+= l
;
1997 sess_local
[s
].pin
++;
2001 STAT(tun_tx_packets
);
2002 INC_STAT(tun_tx_bytes
, l
);
2006 // Helper routine for the TBF filters.
2007 // Used to send queued data in from the user.
2009 void send_ipin(sessionidt s
, uint8_t *buf
, int len
)
2011 LOG_HEX(5, "IP in throttled", buf
, len
);
2013 if (write(tunfd
, buf
, len
) < 0)
2015 STAT(tun_tx_errors
);
2016 LOG(0, 0, 0, "Error writing %d bytes to TUN device: %s (tunfd=%d, p=%p)\n",
2017 len
, strerror(errno
), tunfd
, buf
);
2025 if (session
[s
].snoop_ip
&& session
[s
].snoop_port
)
2027 // Snooping this session
2028 snoop_send_packet(buf
, len
, session
[s
].snoop_ip
, session
[s
].snoop_port
);
2031 // Increment packet counters
2032 increment_counter(&session
[s
].cin
, &session
[s
].cin_wrap
, len
);
2033 session
[s
].cin_delta
+= len
;
2036 sess_local
[s
].cin
+= len
;
2037 sess_local
[s
].pin
++;
2041 STAT(tun_tx_packets
);
2042 INC_STAT(tun_tx_bytes
, len
- 4);
2046 // Process CCP messages
2047 void processccp(sessionidt s
, tunnelidt t
, uint8_t *p
, uint16_t l
)
2049 uint8_t b
[MAXETHER
];
2054 LOG_HEX(5, "CCP", p
, l
);
2056 if (session
[s
].ppp
.phase
< Network
)
2058 LOG(2, s
, t
, "CCP %s ignored in %s phase\n", ppp_code(*p
), ppp_phase(session
[s
].ppp
.phase
));
2064 LOG(1, s
, t
, "Short CCP packet\n");
2065 STAT(tunnel_rx_errors
);
2068 LOG(4, s
, t
, "CCP: recv %s\n", ppp_code(*p
));
2069 if (*p
== ConfigAck
)
2071 switch (session
[s
].ppp
.ccp
)
2074 initialise_restart_count(s
, ccp
);
2075 change_state(s
, ccp
, AckReceived
);
2080 LOG(2, s
, t
, "CCP: ConfigAck in state %s? Sending ConfigReq\n", ppp_state(session
[s
].ppp
.ccp
));
2082 change_state(s
, ccp
, RequestSent
);
2086 LOG(3, s
, t
, "CCP: Opened\n");
2087 change_state(s
, ccp
, Opened
);
2091 LOG(2, s
, t
, "CCP: ignoring %s in state %s\n", ppp_code(*p
), ppp_state(session
[s
].ppp
.ccp
));
2094 else if (*p
== ConfigReq
)
2096 if (l
< 6) // accept no compression
2098 else // compression requested--reject
2101 q
= makeppp(b
, sizeof(b
), p
, l
, s
, t
, PPPCCP
, 0, 0, 0);
2104 switch (session
[s
].ppp
.ccp
)
2107 q
= makeppp(b
, sizeof(b
), p
, 2, s
, t
, PPPCCP
, 0, 0, 0);
2110 *((uint16_t *) (q
+ 2)) = htons(l
= 4);
2114 initialise_restart_count(s
, ccp
);
2116 if (*q
== ConfigAck
)
2117 change_state(s
, ccp
, AckSent
);
2119 change_state(s
, ccp
, RequestSent
);
2124 if (*q
== ConfigAck
)
2125 change_state(s
, ccp
, AckSent
);
2130 if (*q
== ConfigAck
)
2131 change_state(s
, ccp
, Opened
);
2136 initialise_restart_count(s
, ccp
);
2141 if (*q
== ConfigAck
)
2142 change_state(s
, ccp
, AckSent
);
2144 change_state(s
, ccp
, RequestSent
);
2149 LOG(2, s
, t
, "CCP: ignoring %s in state %s\n", ppp_code(*p
), ppp_state(session
[s
].ppp
.ccp
));
2153 LOG(4, s
, t
, "CCP: send %s\n", ppp_code(*q
));
2154 tunnelsend(b
, l
+ (q
- b
), t
);
2156 else if (*p
== TerminateReq
)
2159 q
= makeppp(b
, sizeof(b
), p
, l
, s
, t
, PPPCCP
, 0, 0, 0);
2161 LOG(3, s
, t
, "CCP: send %s\n", ppp_code(*q
));
2162 tunnelsend(b
, l
+ (q
- b
), t
);
2163 change_state(s
, ccp
, Stopped
);
2165 else if (*p
!= CodeRej
)
2167 ppp_code_rej(s
, t
, PPPCCP
, "CCP", p
, l
, b
, sizeof(b
));
2171 // send a CHAP challenge
2172 void sendchap(sessionidt s
, tunnelidt t
)
2174 uint8_t b
[MAXETHER
];
2183 LOG(1, s
, t
, "No RADIUS to send challenge\n");
2184 STAT(tunnel_tx_errors
);
2188 LOG(1, s
, t
, "Send CHAP challenge\n");
2190 radius
[r
].chap
= 1; // CHAP not PAP
2192 if (radius
[r
].state
!= RADIUSCHAP
)
2195 radius
[r
].state
= RADIUSCHAP
;
2196 radius
[r
].retry
= backoff(radius
[r
].try++);
2197 if (radius
[r
].try > 5)
2199 sessionshutdown(s
, "CHAP timeout.", CDN_ADMIN_DISC
, TERM_REAUTHENTICATION_FAILURE
);
2200 STAT(tunnel_tx_errors
);
2203 q
= makeppp(b
, sizeof(b
), 0, 0, s
, t
, PPPCHAP
, 0, 0, 0);
2206 *q
= 1; // challenge
2207 q
[1] = radius
[r
].id
; // ID
2208 q
[4] = 16; // value size (size of challenge)
2209 memcpy(q
+ 5, radius
[r
].auth
, 16); // challenge
2210 strcpy((char *) q
+ 21, hostname
); // our name
2211 *(uint16_t *) (q
+ 2) = htons(strlen(hostname
) + 21); // length
2212 tunnelsend(b
, strlen(hostname
) + 21 + (q
- b
), t
); // send it
2215 // fill in a L2TP message with a PPP frame,
2216 // returns start of PPP frame
2217 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
)
2219 uint16_t hdr
= 0x0002; // L2TP with no options
2220 uint16_t type
= mtype
;
2223 if (size
< 16) // Need more space than this!!
2225 LOG(0, s
, t
, "makeppp buffer too small for L2TP header (size=%d)\n", size
);
2229 if (prio
) hdr
|= 0x0100; // set priority bit
2231 *(uint16_t *) (b
+ 0) = htons(hdr
);
2232 *(uint16_t *) (b
+ 2) = htons(tunnel
[t
].far
); // tunnel
2233 *(uint16_t *) (b
+ 4) = htons(session
[s
].far
); // session
2236 // Check whether this session is part of multilink
2239 if (bundle
[bid
].num_of_links
> 1)
2240 type
= PPPMP
; // Change PPP message type to the PPPMP
2245 if (type
== PPPLCP
|| !(session
[s
].flags
& SESSION_ACFC
))
2247 *(uint16_t *) b
= htons(0xFF03); // HDLC header
2251 if (type
< 0x100 && session
[s
].flags
& SESSION_PFC
)
2257 *(uint16_t *) b
= htons(type
);
2263 // Set the sequence number and (B)egin (E)nd flags
2264 if (session
[s
].mssf
)
2266 // Set the multilink bits
2267 uint16_t bits_send
= mp_bits
;
2268 *(uint16_t *) b
= htons((bundle
[bid
].seq_num_t
& 0xFF0F)|bits_send
);
2273 *(uint32_t *) b
= htonl(bundle
[bid
].seq_num_t
);
2274 // Set the multilink bits
2279 bundle
[bid
].seq_num_t
++;
2281 // Add the message type if this fragment has the begin bit set
2282 if (mp_bits
& MP_BEGIN
)
2284 //*b++ = mtype; // The next two lines are instead of this
2285 *(uint16_t *) b
= htons(mtype
); // Message type
2290 if ((b
- start
) + l
> size
)
2292 LOG(2, s
, t
, "makeppp would overflow buffer (size=%d, header+payload=%d)\n", size
, (b
- start
) + l
);
2303 static int add_lcp_auth(uint8_t *b
, int size
, int authtype
)
2306 if ((authtype
== AUTHCHAP
&& size
< 5) || size
< 4)
2309 *b
++ = 3; // Authentication-Protocol
2310 if (authtype
== AUTHCHAP
)
2312 len
= *b
++ = 5; // length
2313 *(uint16_t *) b
= htons(PPPCHAP
); b
+= 2;
2316 else if (authtype
== AUTHPAP
)
2318 len
= *b
++ = 4; // length
2319 *(uint16_t *) b
= htons(PPPPAP
); b
+= 2;
2323 LOG(0, 0, 0, "add_lcp_auth called with unsupported auth type %d\n", authtype
);
2329 // Send LCP ConfigReq for MRU, authentication type and magic no
2330 void sendlcp(sessionidt s
, tunnelidt t
)
2332 uint8_t b
[500], *q
, *l
;
2333 int authtype
= sess_local
[s
].lcp_authtype
;
2335 if (!(q
= makeppp(b
, sizeof(b
), NULL
, 0, s
, t
, PPPLCP
, 0, 0, 0)))
2338 LOG(3, s
, t
, "LCP: send ConfigReq%s%s%s including MP options\n",
2339 authtype
? " (" : "",
2340 authtype
? (authtype
== AUTHCHAP
? "CHAP" : "PAP") : "",
2341 authtype
? ")" : "");
2345 *l
++ = ++sess_local
[s
].lcp_ident
; // ID
2347 l
+= 2; //Save space for length
2349 if (sess_local
[s
].ppp_mru
)
2351 *l
++ = 1; *l
++ = 4; // Maximum-Receive-Unit (length 4)
2352 *(uint16_t *) l
= htons(sess_local
[s
].ppp_mru
); l
+= 2;
2356 l
+= add_lcp_auth(l
, sizeof(b
) - (l
- b
), authtype
);
2358 if (session
[s
].magic
)
2360 *l
++ = 5; *l
++ = 6; // Magic-Number (length 6)
2361 *(uint32_t *) l
= htonl(session
[s
].magic
);
2365 if (sess_local
[s
].mp_mrru
)
2367 *l
++ = 17; *l
++ = 4; // Multilink Max-Receive-Reconstructed-Unit (length 4)
2368 *(uint16_t *) l
= htons(sess_local
[s
].mp_mrru
); l
+= 2;
2371 if (sess_local
[s
].mp_epdis
)
2373 *l
++ = 19; *l
++ = 7; // Multilink Endpoint Discriminator (length 7)
2374 *l
++ = IPADDR
; // Endpoint Discriminator class
2375 *(uint32_t *) l
= htonl(sess_local
[s
].mp_epdis
);
2379 *(uint16_t *)(q
+ 2) = htons(l
- q
); // Length
2381 LOG_HEX(5, "PPPLCP", q
, l
- q
);
2382 if (config
->debug
> 3) dumplcp(q
, l
- q
);
2384 tunnelsend(b
, (l
- b
), t
);
2385 restart_timer(s
, lcp
);
2388 // Send CCP request for no compression
2389 void sendccp(sessionidt s
, tunnelidt t
)
2393 if (!(q
= makeppp(b
, sizeof(b
), NULL
, 0, s
, t
, PPPCCP
, 0, 0, 0)))
2396 LOG(3, s
, t
, "CCP: send ConfigReq (no compression)\n");
2399 *(q
+ 1) = ++sess_local
[s
].lcp_ident
; // ID
2400 *(uint16_t *)(q
+ 2) = htons(4); // Length
2402 LOG_HEX(5, "PPPCCP", q
, 4);
2403 tunnelsend(b
, (q
- b
) + 4 , t
);
2404 restart_timer(s
, ccp
);
2407 // Reject unknown/unconfigured protocols
2408 void protoreject(sessionidt s
, tunnelidt t
, uint8_t *p
, uint16_t l
, uint16_t proto
)
2411 uint8_t buf
[MAXETHER
];
2413 int mru
= session
[s
].mru
;
2414 if (mru
< MINMTU
) mru
= MINMTU
;
2415 if (mru
> sizeof(buf
)) mru
= sizeof(buf
);
2418 if (l
> mru
) l
= mru
;
2420 q
= makeppp(buf
, sizeof(buf
), 0, 0, s
, t
, PPPLCP
, 0, 0, 0);
2424 *(q
+ 1) = ++sess_local
[s
].lcp_ident
;
2425 *(uint16_t *)(q
+ 2) = htons(l
);
2426 *(uint16_t *)(q
+ 4) = htons(proto
);
2427 memcpy(q
+ 6, p
, l
- 6);
2429 if (proto
== PPPIPV6CP
)
2430 LOG(3, s
, t
, "LCP: send ProtocolRej (IPV6CP: not configured)\n");
2432 LOG(2, s
, t
, "LCP: sent ProtocolRej (0x%04X: unsupported)\n", proto
);
2434 tunnelsend(buf
, l
+ (q
- buf
), t
);