20 extern tunnelt
*tunnel
;
21 extern bundlet
*bundle
;
22 extern fragmentationt
*frag
;
23 extern sessiont
*session
;
24 extern radiust
*radius
;
26 extern char hostname
[];
27 extern uint32_t eth_tx
;
28 extern time_t time_now
;
29 extern uint64_t time_now_ms
;
30 extern configt
*config
;
32 static int add_lcp_auth(uint8_t *b
, int size
, int authtype
);
33 static bundleidt
new_bundle(void);
34 static int epdiscmp(epdist
, epdist
);
35 static void setepdis(epdist
*, epdist
);
36 static void ipcp_open(sessionidt s
, tunnelidt t
);
38 static int first_session_in_bundle(sessionidt s
)
41 for (i
= 1; i
< MAXBUNDLE
; i
++)
42 if (bundle
[i
].state
!= BUNDLEFREE
)
43 if (epdiscmp(session
[s
].epdis
,bundle
[i
].epdis
) && !strcmp(session
[s
].user
, bundle
[i
].user
))
48 // Process PAP messages
49 void processpap(sessionidt s
, tunnelidt t
, uint8_t *p
, uint16_t l
)
58 LOG_HEX(5, "PAP", p
, l
);
61 LOG(1, s
, t
, "Short PAP %u bytes\n", l
);
62 STAT(tunnel_rx_errors
);
63 sessionshutdown(s
, "Short PAP packet.", CDN_ADMIN_DISC
, TERM_USER_ERROR
);
67 if ((hl
= ntohs(*(uint16_t *) (p
+ 2))) > l
)
69 LOG(1, s
, t
, "Length mismatch PAP %u/%u\n", hl
, l
);
70 STAT(tunnel_rx_errors
);
71 sessionshutdown(s
, "PAP length mismatch.", CDN_ADMIN_DISC
, TERM_USER_ERROR
);
78 LOG(1, s
, t
, "Unexpected PAP code %d\n", *p
);
79 STAT(tunnel_rx_errors
);
80 sessionshutdown(s
, "Unexpected PAP code.", CDN_ADMIN_DISC
, TERM_USER_ERROR
);
84 if (session
[s
].ppp
.phase
!= Authenticate
)
86 LOG(2, s
, t
, "PAP ignored in %s phase\n", ppp_phase(session
[s
].ppp
.phase
));
93 user
[0] = pass
[0] = 0;
94 if (*b
&& *b
< sizeof(user
))
96 memcpy(user
, b
+ 1, *b
);
99 if (*b
&& *b
< sizeof(pass
))
101 memcpy(pass
, b
+ 1, *b
);
105 LOG(3, s
, t
, "PAP login %s/%s\n", user
, pass
);
109 if ((!config
->disable_lac_func
) && lac_conf_forwardtoremotelns(s
, user
))
111 // Creating a tunnel/session has been started
116 if (session
[s
].ip
|| !(r
= radiusnew(s
)))
118 // respond now, either no RADIUS available or already authenticated
121 uint8_t *p
= makeppp(b
, sizeof(b
), 0, 0, s
, t
, PPPPAP
, 0, 0, 0);
127 *p
= 3; // cant authorise
129 *(uint16_t *) (p
+ 2) = htons(5); // length
130 p
[4] = 0; // no message
131 tunnelsend(b
, 5 + (p
- b
), t
); // send it
135 LOG(3, s
, t
, "Already an IP allocated: %s (%d)\n",
136 fmtaddr(htonl(session
[s
].ip
), 0), session
[s
].ip_pool_index
);
140 LOG(1, s
, t
, "No RADIUS session available to authenticate session...\n");
141 sessionshutdown(s
, "No free RADIUS sessions.", CDN_UNAVAILABLE
, TERM_SERVICE_UNAVAILABLE
);
146 // Run PRE_AUTH plugins
147 struct param_pre_auth packet
= { &tunnel
[t
], &session
[s
], strdup(user
), strdup(pass
), PPPPAP
, 1 };
148 run_plugins(PLUGIN_PRE_AUTH
, &packet
);
149 if (!packet
.continue_auth
)
151 LOG(3, s
, t
, "A plugin rejected PRE_AUTH\n");
152 if (packet
.username
) free(packet
.username
);
153 if (packet
.password
) free(packet
.password
);
157 strncpy(session
[s
].user
, packet
.username
, sizeof(session
[s
].user
) - 1);
158 strncpy(radius
[r
].pass
, packet
.password
, sizeof(radius
[r
].pass
) - 1);
160 free(packet
.username
);
161 free(packet
.password
);
164 LOG(3, s
, t
, "Sending login for %s/%s to RADIUS\n", user
, pass
);
165 if ((session
[s
].mrru
) && (!first_session_in_bundle(s
)))
166 radiussend(r
, RADIUSJUSTAUTH
);
168 radiussend(r
, RADIUSAUTH
);
172 // Process CHAP messages
173 void processchap(sessionidt s
, tunnelidt t
, uint8_t *p
, uint16_t l
)
180 LOG_HEX(5, "CHAP", p
, l
);
184 LOG(1, s
, t
, "Short CHAP %u bytes\n", l
);
185 STAT(tunnel_rx_errors
);
186 sessionshutdown(s
, "Short CHAP packet.", CDN_ADMIN_DISC
, TERM_USER_ERROR
);
190 if ((hl
= ntohs(*(uint16_t *) (p
+ 2))) > l
)
192 LOG(1, s
, t
, "Length mismatch CHAP %u/%u\n", hl
, l
);
193 STAT(tunnel_rx_errors
);
194 sessionshutdown(s
, "CHAP length mismatch.", CDN_ADMIN_DISC
, TERM_USER_ERROR
);
201 LOG(1, s
, t
, "Unexpected CHAP response code %d\n", *p
);
202 STAT(tunnel_rx_errors
);
203 sessionshutdown(s
, "CHAP length mismatch.", CDN_ADMIN_DISC
, TERM_USER_ERROR
);
207 if (session
[s
].ppp
.phase
!= Authenticate
)
209 LOG(2, s
, t
, "CHAP ignored in %s phase\n", ppp_phase(session
[s
].ppp
.phase
));
213 r
= sess_local
[s
].radius
;
216 LOG(3, s
, t
, "Unexpected CHAP message\n");
218 // Some modems (Netgear DM602, possibly others) persist in using CHAP even
219 // after ACKing our ConfigReq for PAP.
220 if (sess_local
[s
].lcp_authtype
== AUTHPAP
&& config
->radius_authtypes
& AUTHCHAP
)
222 sess_local
[s
].lcp_authtype
= AUTHCHAP
;
228 if (p
[1] != radius
[r
].id
)
230 LOG(1, s
, t
, "Wrong CHAP response ID %d (should be %d) (%d)\n", p
[1], radius
[r
].id
, r
);
231 STAT(tunnel_rx_errors
);
232 sessionshutdown(s
, "Unexpected CHAP response ID.", CDN_ADMIN_DISC
, TERM_USER_ERROR
);
236 if (l
< 5 || p
[4] != 16)
238 LOG(1, s
, t
, "Bad CHAP response length %d\n", l
< 5 ? -1 : p
[4]);
239 STAT(tunnel_rx_errors
);
240 sessionshutdown(s
, "Bad CHAP response length.", CDN_ADMIN_DISC
, TERM_USER_ERROR
);
246 if (l
< 16 || l
- 16 >= sizeof(session
[s
].user
))
248 LOG(1, s
, t
, "CHAP user too long %d\n", l
- 16);
249 STAT(tunnel_rx_errors
);
250 sessionshutdown(s
, "CHAP username too long.", CDN_ADMIN_DISC
, TERM_USER_ERROR
);
254 // Run PRE_AUTH plugins
256 struct param_pre_auth packet
= { &tunnel
[t
], &session
[s
], NULL
, NULL
, PPPCHAP
, 1 };
258 packet
.password
= calloc(17, 1);
259 memcpy(packet
.password
, p
, 16);
264 packet
.username
= calloc(l
+ 1, 1);
265 memcpy(packet
.username
, p
, l
);
268 if ((!config
->disable_lac_func
) && lac_conf_forwardtoremotelns(s
, packet
.username
))
270 free(packet
.username
);
271 free(packet
.password
);
272 // Creating a tunnel/session has been started
277 run_plugins(PLUGIN_PRE_AUTH
, &packet
);
278 if (!packet
.continue_auth
)
280 LOG(3, s
, t
, "A plugin rejected PRE_AUTH\n");
281 if (packet
.username
) free(packet
.username
);
282 if (packet
.password
) free(packet
.password
);
286 strncpy(session
[s
].user
, packet
.username
, sizeof(session
[s
].user
) - 1);
287 memcpy(radius
[r
].pass
, packet
.password
, 16);
289 free(packet
.username
);
290 free(packet
.password
);
294 LOG(3, s
, t
, "CHAP login %s\n", session
[s
].user
);
295 if ((session
[s
].mrru
) && (!first_session_in_bundle(s
)))
296 radiussend(r
, RADIUSJUSTAUTH
);
298 radiussend(r
, RADIUSAUTH
);
301 static void dumplcp(uint8_t *p
, int l
)
304 uint8_t *o
= (p
+ 4);
306 LOG_HEX(5, "PPP LCP Packet", p
, l
);
307 LOG(4, 0, 0, "PPP LCP Packet type %d (%s len %d)\n", *p
, ppp_code((int)*p
), ntohs( ((uint16_t *) p
)[1]) );
308 LOG(4, 0, 0, "Length: %d\n", l
);
309 if (*p
!= ConfigReq
&& *p
!= ConfigRej
&& *p
!= ConfigAck
)
318 LOG(4, 0, 0, " Option length is %d...\n", length
);
323 LOG(4, 0, 0, " Option type is 0...\n");
330 case 1: // Maximum-Receive-Unit
332 LOG(4, 0, 0, " %s %d\n", ppp_lcp_option(type
), ntohs(*(uint16_t *)(o
+ 2)));
334 LOG(4, 0, 0, " %s odd length %d\n", ppp_lcp_option(type
), length
);
336 case 2: // Async-Control-Character-Map
339 uint32_t asyncmap
= ntohl(*(uint32_t *)(o
+ 2));
340 LOG(4, 0, 0, " %s %x\n", ppp_lcp_option(type
), asyncmap
);
343 LOG(4, 0, 0, " %s odd length %d\n", ppp_lcp_option(type
), length
);
345 case 3: // Authentication-Protocol
348 int proto
= ntohs(*(uint16_t *)(o
+ 2));
349 LOG(4, 0, 0, " %s 0x%x (%s)\n", ppp_lcp_option(type
), proto
,
350 proto
== PPPPAP
? "PAP" : "UNSUPPORTED");
352 else if (length
== 5)
354 int proto
= ntohs(*(uint16_t *)(o
+ 2));
356 LOG(4, 0, 0, " %s 0x%x 0x%x (%s)\n", ppp_lcp_option(type
), proto
, algo
,
357 (proto
== PPPCHAP
&& algo
== 5) ? "CHAP MD5" : "UNSUPPORTED");
360 LOG(4, 0, 0, " %s odd length %d\n", ppp_lcp_option(type
), length
);
362 case 4: // Quality-Protocol
364 uint32_t qp
= ntohl(*(uint32_t *)(o
+ 2));
365 LOG(4, 0, 0, " %s %x\n", ppp_lcp_option(type
), qp
);
368 case 5: // Magic-Number
371 uint32_t magicno
= ntohl(*(uint32_t *)(o
+ 2));
372 LOG(4, 0, 0, " %s %x\n", ppp_lcp_option(type
), magicno
);
375 LOG(4, 0, 0, " %s odd length %d\n", ppp_lcp_option(type
), length
);
377 case 7: // Protocol-Field-Compression
378 case 8: // Address-And-Control-Field-Compression
379 LOG(4, 0, 0, " %s\n", ppp_lcp_option(type
));
382 LOG(2, 0, 0, " Unknown PPP LCP Option type %d\n", type
);
390 void lcp_open(sessionidt s
, tunnelidt t
)
392 // transition to Authentication or Network phase:
393 session
[s
].ppp
.phase
= sess_local
[s
].lcp_authtype
? Authenticate
: Network
;
395 LOG(3, s
, t
, "LCP: Opened, phase %s\n", ppp_phase(session
[s
].ppp
.phase
));
398 change_state(s
, lcp
, Opened
);
400 if (session
[s
].ppp
.phase
== Authenticate
)
402 if (sess_local
[s
].lcp_authtype
== AUTHCHAP
)
407 if(session
[s
].bundle
== 0 || bundle
[session
[s
].bundle
].num_of_links
== 1)
411 change_state(s
, ipcp
, RequestSent
);
412 // move to passive state for IPv6 (if configured), CCP
413 if (config
->ipv6_prefix
.s6_addr
[0])
414 change_state(s
, ipv6cp
, Stopped
);
416 change_state(s
, ipv6cp
, Closed
);
418 change_state(s
, ccp
, Stopped
);
422 sessionidt first_ses
= bundle
[session
[s
].bundle
].members
[0];
423 LOG(3, s
, t
, "MPPP: Skipping IPCP negotiation for session:%d, first session of bundle is:%d\n",s
,first_ses
);
429 void lcp_restart(sessionidt s
)
431 session
[s
].ppp
.phase
= Establish
;
433 change_state(s
, ipcp
, Dead
);
434 change_state(s
, ipv6cp
, Dead
);
435 change_state(s
, ccp
, Dead
);
438 static uint8_t *ppp_conf_rej(sessionidt s
, uint8_t *buf
, size_t blen
, uint16_t mtype
,
439 uint8_t **response
, uint8_t *queued
, uint8_t *packet
, uint8_t *option
)
441 if (!*response
|| **response
!= ConfigRej
)
443 queued
= *response
= makeppp(buf
, blen
, packet
, 2, s
, session
[s
].tunnel
, mtype
, 0, 0, 0);
451 if ((queued
- buf
+ option
[1]) > blen
)
453 LOG(2, s
, session
[s
].tunnel
, "PPP overflow for ConfigRej (proto %u, option %u).\n", mtype
, *option
);
457 memcpy(queued
, option
, option
[1]);
458 return queued
+ option
[1];
461 static uint8_t *ppp_conf_nak(sessionidt s
, uint8_t *buf
, size_t blen
, uint16_t mtype
,
462 uint8_t **response
, uint8_t *queued
, uint8_t *packet
, uint8_t *option
,
463 uint8_t *value
, size_t vlen
)
468 case PPPLCP
: nak_sent
= &sess_local
[s
].lcp
.nak_sent
; break;
469 case PPPIPCP
: nak_sent
= &sess_local
[s
].ipcp
.nak_sent
; break;
470 case PPPIPV6CP
: nak_sent
= &sess_local
[s
].ipv6cp
.nak_sent
; break;
471 default: return 0; // ?
474 if (*response
&& **response
!= ConfigNak
)
476 if (*nak_sent
< config
->ppp_max_failure
) // reject queued
479 return ppp_conf_rej(s
, buf
, blen
, mtype
, response
, 0, packet
, option
);
484 if (*nak_sent
>= config
->ppp_max_failure
)
485 return ppp_conf_rej(s
, buf
, blen
, mtype
, response
, 0, packet
, option
);
487 queued
= *response
= makeppp(buf
, blen
, packet
, 2, s
, session
[s
].tunnel
, mtype
, 0, 0, 0);
496 if ((queued
- buf
+ vlen
+ 2) > blen
)
498 LOG(2, s
, session
[s
].tunnel
, "PPP overflow for ConfigNak (proto %u, option %u).\n", mtype
, *option
);
503 *queued
++ = vlen
+ 2;
504 memcpy(queued
, value
, vlen
);
505 return queued
+ vlen
;
508 static void ppp_code_rej(sessionidt s
, tunnelidt t
, uint16_t proto
,
509 char *pname
, uint8_t *p
, uint16_t l
, uint8_t *buf
, size_t size
)
512 int mru
= session
[s
].mru
;
513 if (mru
< MINMTU
) mru
= MINMTU
;
514 if (mru
> size
) mru
= size
;
517 if (l
> mru
) l
= mru
;
519 q
= makeppp(buf
, size
, 0, 0, s
, t
, proto
, 0, 0, 0);
523 *(q
+ 1) = ++sess_local
[s
].lcp_ident
;
524 *(uint16_t *)(q
+ 2) = htons(l
);
525 memcpy(q
+ 4, p
, l
- 4);
527 LOG(2, s
, t
, "Unexpected %s code %s\n", pname
, ppp_code(*p
));
528 LOG(3, s
, t
, "%s: send %s\n", pname
, ppp_code(*q
));
529 if (config
->debug
> 3) dumplcp(q
, l
);
531 tunnelsend(buf
, l
+ (q
- buf
), t
);
534 // Process LCP messages
535 void processlcp(sessionidt s
, tunnelidt t
, uint8_t *p
, uint16_t l
)
543 LOG_HEX(5, "LCP", p
, l
);
546 LOG(1, s
, t
, "Short LCP %d bytes\n", l
);
547 STAT(tunnel_rx_errors
);
551 if ((hl
= ntohs(*(uint16_t *) (p
+ 2))) > l
)
553 LOG(1, s
, t
, "Length mismatch LCP %u/%u\n", hl
, l
);
554 STAT(tunnel_rx_errors
);
559 if (session
[s
].die
) // going down...
562 LOG(((*p
== EchoReq
|| *p
== EchoReply
) ? 4 : 3), s
, t
,
563 "LCP: recv %s\n", ppp_code(*p
));
565 if (config
->debug
> 3) dumplcp(p
, l
);
570 uint8_t *o
= (p
+ 4);
578 if (length
== 0 || type
== 0 || x
< length
) break;
581 case 3: // Authentication-Protocol
583 int proto
= ntohs(*(uint16_t *)(o
+ 2));
586 else if (proto
== PPPCHAP
&& *(o
+ 4) == 5)
596 if (!session
[s
].ip
&& authtype
)
597 sess_local
[s
].lcp_authtype
= authtype
;
599 switch (session
[s
].ppp
.lcp
)
602 initialise_restart_count(s
, lcp
);
603 change_state(s
, lcp
, AckReceived
);
608 LOG(2, s
, t
, "LCP: ConfigAck in state %s? Sending ConfigReq\n", ppp_state(session
[s
].ppp
.lcp
));
609 if (session
[s
].ppp
.lcp
== Opened
)
613 change_state(s
, lcp
, RequestSent
);
621 LOG(2, s
, t
, "LCP: ignoring %s in state %s\n", ppp_code(*p
), ppp_state(session
[s
].ppp
.lcp
));
624 else if (*p
== ConfigReq
)
627 uint8_t *o
= (p
+ 4);
628 uint8_t *response
= 0;
629 static uint8_t asyncmap
[4] = { 0, 0, 0, 0 }; // all zero
630 static uint8_t authproto
[5];
638 if (length
== 0 || type
== 0 || x
< length
) break;
641 case 1: // Maximum-Receive-Unit
643 uint16_t mru
= ntohs(*(uint16_t *)(o
+ 2));
646 session
[s
].mru
= mru
;
651 LOG(3, s
, t
, " Remote requesting MRU of %u. Rejecting.\n", mru
);
653 q
= ppp_conf_nak(s
, b
, sizeof(b
), PPPLCP
, &response
, q
, p
, o
, (uint8_t *) &mru
, sizeof(mru
));
657 case 2: // Async-Control-Character-Map
658 if (!ntohl(*(uint32_t *)(o
+ 2))) // all bits zero is OK
661 LOG(3, s
, t
, " Remote requesting asyncmap. Rejecting.\n");
662 q
= ppp_conf_nak(s
, b
, sizeof(b
), PPPLCP
, &response
, q
, p
, o
, asyncmap
, sizeof(asyncmap
));
665 case 3: // Authentication-Protocol
667 int proto
= ntohs(*(uint16_t *)(o
+ 2));
668 char proto_name
[] = "0x0000";
673 if (config
->radius_authtypes
& AUTHPAP
)
675 sess_local
[s
].lcp_authtype
= AUTHPAP
;
679 strcpy(proto_name
, "PAP");
681 else if (proto
== PPPCHAP
)
683 if (config
->radius_authtypes
& AUTHCHAP
684 && *(o
+ 4) == 5) // MD5
686 sess_local
[s
].lcp_authtype
= AUTHCHAP
;
690 strcpy(proto_name
, "CHAP");
693 sprintf(proto_name
, "%#4.4x", proto
);
695 LOG(3, s
, t
, " Remote requesting %s authentication. Rejecting.\n", proto_name
);
697 alen
= add_lcp_auth(authproto
, sizeof(authproto
), config
->radius_authprefer
);
698 if (alen
< 2) break; // paranoia
700 q
= ppp_conf_nak(s
, b
, sizeof(b
), PPPLCP
, &response
, q
, p
, o
, authproto
+ 2, alen
- 2);
701 if (q
&& *response
== ConfigNak
&&
702 config
->radius_authtypes
!= config
->radius_authprefer
)
705 alen
= add_lcp_auth(authproto
, sizeof(authproto
), config
->radius_authtypes
& ~config
->radius_authprefer
);
707 q
= ppp_conf_nak(s
, b
, sizeof(b
), PPPLCP
, &response
, q
, p
, o
, authproto
+ 2, alen
- 2);
714 case 4: // Quality-Protocol
715 case 5: // Magic-Number
716 case 7: // Protocol-Field-Compression
717 case 8: // Address-And-Control-Field-Compression
720 case 17: // Multilink Max-Receive-Reconstructed-Unit
722 uint16_t mrru
= ntohs(*(uint16_t *)(o
+ 2));
723 session
[s
].mrru
= mrru
;
725 LOG(3, s
, t
, " Received PPP LCP option MRRU: %d\n",mrru
);
729 case 18: // Multilink Short Sequence Number Header Format
733 LOG(3, s
, t
, " Received PPP LCP option MSSN format\n");
737 case 19: // Multilink Endpoint Discriminator
739 uint8_t epdis_class
= o
[2];
742 session
[s
].epdis
.addr_class
= epdis_class
;
743 session
[s
].epdis
.length
= length
- 3;
744 if (session
[s
].epdis
.length
> 20)
746 LOG(1, s
, t
, "Error: received EndDis Address Length more than 20: %d\n", session
[s
].epdis
.length
);
747 session
[s
].epdis
.length
= 20;
750 for (addr
= 0; addr
< session
[s
].epdis
.length
; addr
++)
751 session
[s
].epdis
.address
[addr
] = o
[3+addr
];
758 LOG(3, s
, t
, " Received PPP LCP option Multilink EndDis Local Address Class: %d\n",epdis_class
);
761 LOG(3, s
, t
, " Received PPP LCP option Multilink EndDis IP Address Class: %d\n",epdis_class
);
764 LOG(3, s
, t
, " Received PPP LCP option Multilink EndDis IEEE MAC Address Class: %d\n",epdis_class
);
767 LOG(3, s
, t
, " Received PPP LCP option Multilink EndDis PPP Magic No Class: %d\n",epdis_class
);
770 LOG(3, s
, t
, " Received PPP LCP option Multilink EndDis PSND No Class: %d\n",epdis_class
);
773 LOG(3, s
, t
, " Received PPP LCP option Multilink EndDis NULL Class %d\n",epdis_class
);
778 default: // Reject any unknown options
779 LOG(3, s
, t
, " Rejecting unknown PPP LCP option %d\n", type
);
780 q
= ppp_conf_rej(s
, b
, sizeof(b
), PPPLCP
, &response
, q
, p
, o
);
787 cluster_send_session(s
);
791 l
= q
- response
; // LCP packet length
792 *((uint16_t *) (response
+ 2)) = htons(l
); // update header
796 // Send packet back as ConfigAck
797 response
= makeppp(b
, sizeof(b
), p
, l
, s
, t
, PPPLCP
, 0, 0, 0);
798 if (!response
) return;
799 *response
= ConfigAck
;
802 switch (session
[s
].ppp
.lcp
)
805 response
= makeppp(b
, sizeof(b
), p
, 2, s
, t
, PPPLCP
, 0, 0, 0);
806 if (!response
) return;
807 *response
= TerminateAck
;
808 *((uint16_t *) (response
+ 2)) = htons(l
= 4);
812 initialise_restart_count(s
, lcp
);
814 if (*response
== ConfigAck
)
815 change_state(s
, lcp
, AckSent
);
817 change_state(s
, lcp
, RequestSent
);
822 if (*response
== ConfigAck
)
823 change_state(s
, lcp
, AckSent
);
828 if (*response
== ConfigAck
)
839 if (*response
== ConfigAck
)
840 change_state(s
, lcp
, AckSent
);
842 change_state(s
, lcp
, RequestSent
);
847 sessionshutdown(s
, "LCP: ConfigReq in state Closing. This should not happen. Killing session.", CDN_ADMIN_DISC
, TERM_LOST_SERVICE
);
851 LOG(2, s
, t
, "LCP: ignoring %s in state %s\n", ppp_code(*p
), ppp_state(session
[s
].ppp
.lcp
));
855 LOG(3, s
, t
, "LCP: send %s\n", ppp_code(*response
));
856 if (config
->debug
> 3) dumplcp(response
, l
);
858 tunnelsend(b
, l
+ (response
- b
), t
);
860 else if (*p
== ConfigNak
|| *p
== ConfigRej
)
863 uint8_t *o
= (p
+ 4);
871 if (length
== 0 || type
== 0 || x
< length
) break;
874 case 1: // Maximum-Receive-Unit
877 if (length
< 4) break;
878 sess_local
[s
].ppp_mru
= ntohs(*(uint16_t *)(o
+ 2));
879 LOG(3, s
, t
, " Remote requested MRU of %u\n", sess_local
[s
].ppp_mru
);
883 sess_local
[s
].ppp_mru
= 0;
884 LOG(3, s
, t
, " Remote rejected MRU negotiation\n");
889 case 3: // Authentication-Protocol
897 if (length
< 4) break;
898 proto
= ntohs(*(uint16_t *)(o
+ 2));
902 authtype
= config
->radius_authtypes
& AUTHPAP
;
903 LOG(3, s
, t
, " Remote requested PAP authentication...%sing\n",
904 authtype
? "accept" : "reject");
906 else if (proto
== PPPCHAP
&& length
> 4 && *(o
+ 4) == 5)
908 authtype
= config
->radius_authtypes
& AUTHCHAP
;
909 LOG(3, s
, t
, " Remote requested CHAP authentication...%sing\n",
910 authtype
? "accept" : "reject");
914 LOG(3, s
, t
, " Rejecting unsupported authentication %#4x\n",
920 LOG(2, s
, t
, "LCP: remote rejected auth negotiation\n");
921 authtype
= 0; // shutdown
926 case 5: // Magic-Number
927 session
[s
].magic
= 0;
930 if (length
< 6) break;
931 session
[s
].magic
= ntohl(*(uint32_t *)(o
+ 2));
934 if (session
[s
].magic
)
935 LOG(3, s
, t
, " Remote requested magic-no %x\n", session
[s
].magic
);
937 LOG(3, s
, t
, " Remote rejected magic-no\n");
939 cluster_send_session(s
);
942 case 17: // Multilink Max-Receive-Reconstructed-Unit
946 sess_local
[s
].mp_mrru
= ntohs(*(uint16_t *)(o
+ 2));
947 LOG(3, s
, t
, " Remote requested MRRU of %u\n", sess_local
[s
].mp_mrru
);
951 sess_local
[s
].mp_mrru
= 0;
952 LOG(3, s
, t
, " Remote rejected MRRU negotiation\n");
957 case 18: // Multilink Short Sequence Number Header Format
961 sess_local
[s
].mp_mssf
= 0;
962 LOG(3, s
, t
, " Remote requested Naked mssf\n");
966 sess_local
[s
].mp_mssf
= 0;
967 LOG(3, s
, t
, " Remote rejected mssf\n");
972 case 19: // Multilink Endpoint Discriminator
976 LOG(2, s
, t
, " Remote should not configNak Endpoint Dis!\n");
980 sess_local
[s
].mp_epdis
= 0;
981 LOG(3, s
, t
, " Remote rejected Endpoint Discriminator\n");
987 LOG(2, s
, t
, "LCP: remote sent %s for type %u?\n", ppp_code(*p
), type
);
988 sessionshutdown(s
, "Unable to negotiate LCP.", CDN_ADMIN_DISC
, TERM_USER_ERROR
);
997 sessionshutdown(s
, "Unsupported authentication.", CDN_ADMIN_DISC
, TERM_USER_ERROR
);
1002 sess_local
[s
].lcp_authtype
= authtype
;
1004 switch (session
[s
].ppp
.lcp
)
1009 uint8_t *response
= makeppp(b
, sizeof(b
), p
, 2, s
, t
, PPPLCP
, 0, 0, 0);
1010 if (!response
) return;
1011 *response
= TerminateAck
;
1012 *((uint16_t *) (response
+ 2)) = htons(l
= 4);
1014 LOG(3, s
, t
, "LCP: send %s\n", ppp_code(*response
));
1015 if (config
->debug
> 3) dumplcp(response
, l
);
1017 tunnelsend(b
, l
+ (response
- b
), t
);
1023 initialise_restart_count(s
, lcp
);
1028 LOG(2, s
, t
, "LCP: ConfigNak in state %s? Sending ConfigReq\n", ppp_state(session
[s
].ppp
.lcp
));
1038 LOG(2, s
, t
, "LCP: ignoring %s in state %s\n", ppp_code(*p
), ppp_state(session
[s
].ppp
.lcp
));
1042 else if (*p
== TerminateReq
)
1044 switch (session
[s
].ppp
.lcp
)
1057 zero_restart_count(s
, lcp
);
1058 change_state(s
, lcp
, Closing
);
1062 LOG(2, s
, t
, "LCP: ignoring %s in state %s\n", ppp_code(*p
), ppp_state(session
[s
].ppp
.lcp
));
1066 *p
= TerminateAck
; // send ack
1067 q
= makeppp(b
, sizeof(b
), p
, l
, s
, t
, PPPLCP
, 0, 0, 0);
1070 LOG(3, s
, t
, "LCP: send %s\n", ppp_code(*q
));
1071 if (config
->debug
> 3) dumplcp(q
, l
);
1073 tunnelsend(b
, l
+ (q
- b
), t
); // send it
1075 else if (*p
== ProtocolRej
)
1082 if (l
> 5 && !(proto
& 1))
1089 if (proto
== PPPIPV6CP
)
1091 LOG(3, s
, t
, "IPv6 rejected\n");
1092 change_state(s
, ipv6cp
, Closed
);
1096 LOG(3, s
, t
, "LCP protocol reject: 0x%04X\n", proto
);
1099 else if (*p
== EchoReq
)
1101 *p
= EchoReply
; // reply
1102 *(uint32_t *) (p
+ 4) = htonl(session
[s
].magic
); // our magic number
1103 q
= makeppp(b
, sizeof(b
), p
, l
, s
, t
, PPPLCP
, 0, 0, 0);
1106 LOG(4, s
, t
, "LCP: send %s\n", ppp_code(*q
));
1107 if (config
->debug
> 3) dumplcp(q
, l
);
1109 tunnelsend(b
, l
+ (q
- b
), t
); // send it
1111 else if (*p
== EchoReply
)
1113 // Ignore it, last_packet time is set earlier than this.
1115 else if (*p
!= CodeRej
)
1117 ppp_code_rej(s
, t
, PPPLCP
, "LCP", p
, l
, b
, sizeof(b
));
1121 int join_bundle(sessionidt s
)
1123 // Search for a bundle to join
1126 for (i
= 1; i
< MAXBUNDLE
; i
++)
1128 if (bundle
[i
].state
!= BUNDLEFREE
)
1130 if (epdiscmp(session
[s
].epdis
,bundle
[i
].epdis
) && !strcmp(session
[s
].user
, bundle
[i
].user
))
1132 sessionidt first_ses
= bundle
[i
].members
[0];
1133 if (bundle
[i
].mssf
!= session
[s
].mssf
)
1135 // uniformity of sequence number format must be insured
1136 LOG(3, s
, session
[s
].tunnel
, "MPPP: unable to bundle session %d in bundle %d cause of different mssf\n", s
, i
);
1139 session
[s
].bundle
= i
;
1140 session
[s
].ip
= session
[first_ses
].ip
;
1141 session
[s
].dns1
= session
[first_ses
].dns1
;
1142 session
[s
].dns2
= session
[first_ses
].dns2
;
1143 session
[s
].timeout
= session
[first_ses
].timeout
;
1145 if(session
[s
].epdis
.length
> 0)
1146 setepdis(&bundle
[i
].epdis
, session
[s
].epdis
);
1148 strcpy(bundle
[i
].user
, session
[s
].user
);
1149 bundle
[i
].members
[bundle
[i
].num_of_links
] = s
;
1150 bundle
[i
].num_of_links
++;
1151 LOG(3, s
, session
[s
].tunnel
, "MPPP: Bundling additional line in bundle (%d), lines:%d\n",i
,bundle
[i
].num_of_links
);
1157 // No previously created bundle was found for this session, so create a new one
1158 if (!(b
= new_bundle())) return 0;
1160 session
[s
].bundle
= b
;
1161 bundle
[b
].mrru
= session
[s
].mrru
;
1162 bundle
[b
].mssf
= session
[s
].mssf
;
1163 // FIXME !!! to enable l2tpns reading mssf frames receiver_max_seq, sender_max_seq must be introduce
1164 // now session[s].mssf flag indecates that the receiver wish to receive frames in mssf, so max_seq (i.e. recv_max_seq) = 1<<24
1167 bundle[b].max_seq = 1 << 12;
1169 bundle
[b
].max_seq
= 1 << 24;
1170 if(session
[s
].epdis
.length
> 0)
1171 setepdis(&bundle
[b
].epdis
, session
[s
].epdis
);
1173 strcpy(bundle
[b
].user
, session
[s
].user
);
1174 bundle
[b
].members
[0] = s
;
1175 bundle
[b
].timeout
= session
[s
].timeout
;
1176 LOG(3, s
, session
[s
].tunnel
, "MPPP: Created a new bundle (%d)\n", b
);
1180 static int epdiscmp(epdist ep1
, epdist ep2
)
1183 if (ep1
.length
!= ep2
.length
)
1186 if (ep1
.addr_class
!= ep2
.addr_class
)
1189 for (ad
= 0; ad
< ep1
.length
; ad
++)
1190 if (ep1
.address
[ad
] != ep2
.address
[ad
])
1196 static void setepdis(epdist
*ep1
, epdist ep2
)
1199 ep1
->length
= ep2
.length
;
1200 ep1
->addr_class
= ep2
.addr_class
;
1201 for (ad
= 0; ad
< ep2
.length
; ad
++)
1202 ep1
->address
[ad
] = ep2
.address
[ad
];
1205 static bundleidt
new_bundle()
1208 for (i
= 1; i
< MAXBUNDLE
; i
++)
1210 if (bundle
[i
].state
== BUNDLEFREE
)
1212 LOG(4, 0, 0, "MPPP: Assigning bundle ID %d\n", i
);
1213 bundle
[i
].num_of_links
= 1;
1214 bundle
[i
].last_check
= time_now
; // Initialize last_check value
1215 bundle
[i
].state
= BUNDLEOPEN
;
1216 bundle
[i
].current_ses
= -1; // This is to enforce the first session 0 to be used at first
1217 memset(&frag
[i
], 0, sizeof(fragmentationt
));
1218 if (i
> config
->cluster_highest_bundleid
)
1219 config
->cluster_highest_bundleid
= i
;
1223 LOG(0, 0, 0, "MPPP: Can't find a free bundle! There shouldn't be this many in use!\n");
1227 static void ipcp_open(sessionidt s
, tunnelidt t
)
1229 LOG(3, s
, t
, "IPCP: Opened, session is now active\n");
1231 change_state(s
, ipcp
, Opened
);
1233 if (!(session
[s
].walled_garden
|| session
[s
].flags
& SESSION_STARTED
))
1235 uint16_t r
= radiusnew(s
);
1238 radiussend(r
, RADIUSSTART
); // send radius start
1240 // don't send further Start records if IPCP is restarted
1241 session
[s
].flags
|= SESSION_STARTED
;
1242 cluster_send_session(s
);
1246 // start IPv6 if configured and still in passive state
1247 if (session
[s
].ppp
.ipv6cp
== Stopped
)
1250 change_state(s
, ipv6cp
, RequestSent
);
1254 // Process IPCP messages
1255 void processipcp(sessionidt s
, tunnelidt t
, uint8_t *p
, uint16_t l
)
1257 uint8_t b
[MAXETHER
];
1263 LOG_HEX(5, "IPCP", p
, l
);
1266 LOG(1, s
, t
, "Short IPCP %d bytes\n", l
);
1267 STAT(tunnel_rx_errors
);
1271 if ((hl
= ntohs(*(uint16_t *) (p
+ 2))) > l
)
1273 LOG(1, s
, t
, "Length mismatch IPCP %u/%u\n", hl
, l
);
1274 STAT(tunnel_rx_errors
);
1279 if (session
[s
].ppp
.phase
< Network
)
1281 LOG(2, s
, t
, "IPCP %s ignored in %s phase\n", ppp_code(*p
), ppp_phase(session
[s
].ppp
.phase
));
1285 LOG(3, s
, t
, "IPCP: recv %s\n", ppp_code(*p
));
1287 if (*p
== ConfigAck
)
1289 switch (session
[s
].ppp
.ipcp
)
1292 initialise_restart_count(s
, ipcp
);
1293 change_state(s
, ipcp
, AckReceived
);
1298 LOG(2, s
, t
, "IPCP: ConfigAck in state %s? Sending ConfigReq\n", ppp_state(session
[s
].ppp
.ipcp
));
1300 change_state(s
, ipcp
, RequestSent
);
1308 LOG(2, s
, t
, "IPCP: ignoring %s in state %s\n", ppp_code(*p
), ppp_state(session
[s
].ppp
.ipcp
));
1311 else if (*p
== ConfigReq
)
1313 uint8_t *response
= 0;
1321 if (!o
[1] || o
[1] > length
) return;
1325 case 3: // ip address
1326 gotip
++; // seen address
1327 if (o
[1] != 6) return;
1329 addr
= htonl(session
[s
].ip
);
1330 if (memcmp(o
+ 2, &addr
, (sizeof addr
)))
1333 q
= ppp_conf_nak(s
, b
, sizeof(b
), PPPIPCP
, &response
, q
, p
, o
, (uint8_t *) &addr
, sizeof(addr
));
1334 if (!q
|| (q
!= oq
&& *response
== ConfigRej
))
1336 sessionshutdown(s
, "Can't negotiate IPCP.", CDN_ADMIN_DISC
, TERM_USER_ERROR
);
1343 case 129: // primary DNS
1344 if (o
[1] != 6) return;
1346 addr
= htonl(session
[s
].dns1
);
1347 if (memcmp(o
+ 2, &addr
, (sizeof addr
)))
1349 q
= ppp_conf_nak(s
, b
, sizeof(b
), PPPIPCP
, &response
, q
, p
, o
, (uint8_t *) &addr
, sizeof(addr
));
1355 case 131: // secondary DNS
1356 if (o
[1] != 6) return;
1358 addr
= htonl(session
[s
].dns2
);
1359 if (memcmp(o
+ 2, &addr
, sizeof(addr
)))
1361 q
= ppp_conf_nak(s
, b
, sizeof(b
), PPPIPCP
, &response
, q
, p
, o
, (uint8_t *) &addr
, sizeof(addr
));
1368 LOG(2, s
, t
, " Rejecting PPP IPCP Option type %d\n", *o
);
1369 q
= ppp_conf_rej(s
, b
, sizeof(b
), PPPIPCP
, &response
, q
, p
, o
);
1379 l
= q
- response
; // IPCP packet length
1380 *((uint16_t *) (response
+ 2)) = htons(l
); // update header
1384 // Send packet back as ConfigAck
1385 response
= makeppp(b
, sizeof(b
), p
, l
, s
, t
, PPPIPCP
, 0, 0, 0);
1386 if (!response
) return;
1387 *response
= ConfigAck
;
1391 LOG(1, s
, t
, "No IP in IPCP request\n");
1392 STAT(tunnel_rx_errors
);
1396 switch (session
[s
].ppp
.ipcp
)
1399 response
= makeppp(b
, sizeof(b
), p
, 2, s
, t
, PPPIPCP
, 0, 0, 0);
1400 if (!response
) return;
1401 *response
= TerminateAck
;
1402 *((uint16_t *) (response
+ 2)) = htons(l
= 4);
1406 initialise_restart_count(s
, ipcp
);
1408 if (*response
== ConfigAck
)
1409 change_state(s
, ipcp
, AckSent
);
1411 change_state(s
, ipcp
, RequestSent
);
1416 if (*response
== ConfigAck
)
1417 change_state(s
, ipcp
, AckSent
);
1422 if (*response
== ConfigAck
)
1428 initialise_restart_count(s
, ipcp
);
1433 if (*response
== ConfigAck
)
1434 change_state(s
, ipcp
, AckSent
);
1436 change_state(s
, ipcp
, RequestSent
);
1441 LOG(2, s
, t
, "IPCP: ignoring %s in state %s\n", ppp_code(*p
), ppp_state(session
[s
].ppp
.ipcp
));
1445 LOG(3, s
, t
, "IPCP: send %s\n", ppp_code(*response
));
1446 tunnelsend(b
, l
+ (response
- b
), t
);
1448 else if (*p
== TerminateReq
)
1450 switch (session
[s
].ppp
.ipcp
)
1462 zero_restart_count(s
, ipcp
);
1463 change_state(s
, ipcp
, Closing
);
1467 LOG(2, s
, t
, "IPCP: ignoring %s in state %s\n", ppp_code(*p
), ppp_state(session
[s
].ppp
.ipcp
));
1471 *p
= TerminateAck
; // send ack
1472 q
= makeppp(b
, sizeof(b
), p
, l
, s
, t
, PPPIPCP
, 0, 0, 0);
1475 LOG(3, s
, t
, "IPCP: send %s\n", ppp_code(*q
));
1476 tunnelsend(b
, l
+ (q
- b
), t
); // send it
1478 else if (*p
!= CodeRej
)
1480 ppp_code_rej(s
, t
, PPPIPCP
, "IPCP", p
, l
, b
, sizeof(b
));
1484 static void ipv6cp_open(sessionidt s
, tunnelidt t
)
1486 LOG(3, s
, t
, "IPV6CP: Opened\n");
1488 change_state(s
, ipv6cp
, Opened
);
1489 if (session
[s
].ipv6prefixlen
)
1490 route6set(s
, session
[s
].ipv6route
, session
[s
].ipv6prefixlen
, 1);
1492 // Send an initial RA (TODO: Should we send these regularly?)
1493 send_ipv6_ra(s
, t
, NULL
);
1496 // Process IPV6CP messages
1497 void processipv6cp(sessionidt s
, tunnelidt t
, uint8_t *p
, uint16_t l
)
1499 uint8_t b
[MAXETHER
];
1503 CSTAT(processipv6cp
);
1505 LOG_HEX(5, "IPV6CP", p
, l
);
1508 LOG(1, s
, t
, "Short IPV6CP %d bytes\n", l
);
1509 STAT(tunnel_rx_errors
);
1513 if ((hl
= ntohs(*(uint16_t *) (p
+ 2))) > l
)
1515 LOG(1, s
, t
, "Length mismatch IPV6CP %u/%u\n", hl
, l
);
1516 STAT(tunnel_rx_errors
);
1521 if (session
[s
].ppp
.phase
< Network
)
1523 LOG(2, s
, t
, "IPV6CP %s ignored in %s phase\n", ppp_code(*p
), ppp_phase(session
[s
].ppp
.phase
));
1527 LOG(3, s
, t
, "IPV6CP: recv %s\n", ppp_code(*p
));
1531 LOG(3, s
, t
, "IPV6CP: no IPv4 address (IPCP in state %s)\n", ppp_state(session
[s
].ppp
.ipcp
));
1532 return; // need IPCP to complete...
1535 if (*p
== ConfigAck
)
1537 switch (session
[s
].ppp
.ipv6cp
)
1540 initialise_restart_count(s
, ipv6cp
);
1541 change_state(s
, ipv6cp
, AckReceived
);
1546 LOG(2, s
, t
, "IPV6CP: ConfigAck in state %s? Sending ConfigReq\n", ppp_state(session
[s
].ppp
.ipv6cp
));
1548 change_state(s
, ipv6cp
, RequestSent
);
1556 LOG(2, s
, t
, "IPV6CP: ignoring %s in state %s\n", ppp_code(*p
), ppp_state(session
[s
].ppp
.ipv6cp
));
1559 else if (*p
== ConfigReq
)
1561 uint8_t *response
= 0;
1569 if (!o
[1] || o
[1] > length
) return;
1573 case 1: // interface identifier
1574 gotip
++; // seen address
1575 if (o
[1] != 10) return;
1577 ident
[0] = htonl(session
[s
].ip
);
1580 if (memcmp(o
+ 2, ident
, sizeof(ident
)))
1582 q
= ppp_conf_nak(s
, b
, sizeof(b
), PPPIPV6CP
, &response
, q
, p
, o
, (uint8_t *)ident
, sizeof(ident
));
1589 LOG(2, s
, t
, " Rejecting PPP IPV6CP Option type %d\n", *o
);
1590 q
= ppp_conf_rej(s
, b
, sizeof(b
), PPPIPV6CP
, &response
, q
, p
, o
);
1600 l
= q
- response
; // IPV6CP packet length
1601 *((uint16_t *) (response
+ 2)) = htons(l
); // update header
1605 // Send packet back as ConfigAck
1606 response
= makeppp(b
, sizeof(b
), p
, l
, s
, t
, PPPIPV6CP
, 0, 0, 0);
1607 if (!response
) return;
1608 *response
= ConfigAck
;
1612 LOG(1, s
, t
, "No interface identifier in IPV6CP request\n");
1613 STAT(tunnel_rx_errors
);
1617 switch (session
[s
].ppp
.ipv6cp
)
1620 response
= makeppp(b
, sizeof(b
), p
, 2, s
, t
, PPPIPV6CP
, 0, 0, 0);
1621 if (!response
) return;
1622 *response
= TerminateAck
;
1623 *((uint16_t *) (response
+ 2)) = htons(l
= 4);
1627 initialise_restart_count(s
, ipv6cp
);
1629 if (*response
== ConfigAck
)
1630 change_state(s
, ipv6cp
, AckSent
);
1632 change_state(s
, ipv6cp
, RequestSent
);
1637 if (*response
== ConfigAck
)
1638 change_state(s
, ipv6cp
, AckSent
);
1643 if (*response
== ConfigAck
)
1649 initialise_restart_count(s
, ipv6cp
);
1654 if (*response
== ConfigAck
)
1655 change_state(s
, ipv6cp
, AckSent
);
1657 change_state(s
, ipv6cp
, RequestSent
);
1662 LOG(2, s
, t
, "IPV6CP: ignoring %s in state %s\n", ppp_code(*p
), ppp_state(session
[s
].ppp
.ipv6cp
));
1666 LOG(3, s
, t
, "IPV6CP: send %s\n", ppp_code(*response
));
1667 tunnelsend(b
, l
+ (response
- b
), t
);
1669 else if (*p
== TerminateReq
)
1671 switch (session
[s
].ppp
.ipv6cp
)
1683 zero_restart_count(s
, ipv6cp
);
1684 change_state(s
, ipv6cp
, Closing
);
1688 LOG(2, s
, t
, "IPV6CP: ignoring %s in state %s\n", ppp_code(*p
), ppp_state(session
[s
].ppp
.ipv6cp
));
1692 *p
= TerminateAck
; // send ack
1693 q
= makeppp(b
, sizeof(b
), p
, l
, s
, t
, PPPIPV6CP
, 0, 0, 0);
1696 LOG(3, s
, t
, "IPV6CP: send %s\n", ppp_code(*q
));
1697 tunnelsend(b
, l
+ (q
- b
), t
); // send it
1699 else if (*p
!= CodeRej
)
1701 ppp_code_rej(s
, t
, PPPIPV6CP
, "IPV6CP", p
, l
, b
, sizeof(b
));
1705 static void update_sessions_in_stat(sessionidt s
, uint16_t l
)
1707 bundleidt b
= session
[s
].bundle
;
1710 increment_counter(&session
[s
].cin
, &session
[s
].cin_wrap
, l
);
1711 session
[s
].cin_delta
+= l
;
1714 sess_local
[s
].cin
+= l
;
1715 sess_local
[s
].pin
++;
1719 int i
= frag
[b
].re_frame_begin_index
;
1720 int end
= frag
[b
].re_frame_end_index
;
1723 l
= frag
[b
].fragment
[i
].length
;
1724 s
= frag
[b
].fragment
[i
].sid
;
1725 increment_counter(&session
[s
].cin
, &session
[s
].cin_wrap
, l
);
1726 session
[s
].cin_delta
+= l
;
1729 sess_local
[s
].cin
+= l
;
1730 sess_local
[s
].pin
++;
1733 i
= (i
+ 1) & MAXFRAGNUM_MASK
;
1738 // process IP packet received
1740 // This MUST be called with at least 4 byte behind 'p'.
1741 // (i.e. this routine writes to p[-4]).
1742 void processipin(sessionidt s
, tunnelidt t
, uint8_t *p
, uint16_t l
)
1748 LOG_HEX(5, "IP", p
, l
);
1752 LOG(1, s
, t
, "IP packet too short %d\n", l
);
1753 STAT(tunnel_rx_errors
);
1757 ip
= ntohl(*(uint32_t *)(p
+ 12));
1761 LOG(1, s
, t
, "IP packet too long %d\n", l
);
1762 STAT(tunnel_rx_errors
);
1766 if (session
[s
].ppp
.phase
!= Network
|| session
[s
].ppp
.ipcp
!= Opened
)
1769 if (!session
[s
].bundle
|| bundle
[session
[s
].bundle
].num_of_links
< 2) // FIXME:
1771 // no spoof (do sessionbyip to handled statically routed subnets)
1772 if (ip
!= session
[s
].ip
&& sessionbyip(htonl(ip
)) != s
)
1774 LOG(4, s
, t
, "Dropping packet with spoofed IP %s\n", fmtaddr(htonl(ip
), 0));
1779 // run access-list if any
1780 if (session
[s
].filter_in
&& !ip_filter(p
, l
, session
[s
].filter_in
- 1))
1783 // adjust MSS on SYN and SYN,ACK packets with options
1784 if ((ntohs(*(uint16_t *) (p
+ 6)) & 0x1fff) == 0 && p
[9] == IPPROTO_TCP
) // first tcp fragment
1786 int ihl
= (p
[0] & 0xf) * 4; // length of IP header
1787 if (l
>= ihl
+ 20 && (p
[ihl
+ 13] & TCP_FLAG_SYN
) && ((p
[ihl
+ 12] >> 4) > 5))
1788 adjust_tcp_mss(s
, t
, p
, l
, p
+ ihl
);
1791 // Add on the tun header
1793 *(uint32_t *) p
= htonl(PKTIP
);
1796 if (session
[s
].tbf_in
)
1798 // Are we throttling this session?
1799 if (config
->cluster_iam_master
)
1800 tbf_queue_packet(session
[s
].tbf_in
, p
, l
);
1802 master_throttle_packet(session
[s
].tbf_in
, p
, l
);
1807 if (tun_write(p
, l
) < 0)
1809 STAT(tun_tx_errors
);
1810 LOG(0, s
, t
, "Error writing %d bytes to TUN device: %s (tunfd=%d, p=%p)\n",
1811 l
, strerror(errno
), tunfd
, p
);
1819 if (session
[s
].snoop_ip
&& session
[s
].snoop_port
)
1821 // Snooping this session
1822 snoop_send_packet(p
, l
, session
[s
].snoop_ip
, session
[s
].snoop_port
);
1825 update_sessions_in_stat(s
, l
);
1829 STAT(tun_tx_packets
);
1830 INC_STAT(tun_tx_bytes
, l
);
1833 // process Multilink PPP packet received
1834 void processmpin(sessionidt s
, tunnelidt t
, uint8_t *p
, uint16_t l
)
1836 bundleidt b
= session
[s
].bundle
;
1837 bundlet
* this_bundle
= &bundle
[b
];
1838 uint32_t maskSeq
, max_seq
;
1840 uint16_t frag_index
, frag_index_next
, frag_index_prev
;
1841 fragmentationt
*this_fragmentation
= &frag
[b
];
1842 uint8_t begin_frame
= (*p
& MP_BEGIN
);
1843 uint8_t end_frame
= (*p
& MP_END
);
1844 uint32_t seq_num
, seq_num_next
, seq_num_prev
;
1847 uint16_t begin_index
, end_index
;
1849 // Perform length checking
1852 LOG(2, s
, t
, "MPPP: discarding fragment larger than MAXFRAGLEN\n");
1858 LOG(2, s
, t
, "MPPP: Invalid bundle id: 0\n");
1861 // FIXME !! session[s].mssf means that the receiver wants to receive frames in mssf not means the receiver will send frames in mssf
1862 /* if(session[s].mssf)
1864 // Get 12 bit for seq number
1865 seq_num = ntohs((*(uint16_t *) p) & 0xFF0F);
1868 // After this point the pointer should be advanced 2 bytes
1869 LOG(3, s, t, "MPPP: 12 bits, sequence number: %d\n",seq_num);
1873 // Get 24 bit for seq number
1874 seq_num
= ntohl((*(uint32_t *) p
) & 0xFFFFFF00);
1877 // After this point the pointer should be advanced 4 bytes
1878 LOG(4, s
, t
, "MPPP: 24 bits sequence number:%d\n",seq_num
);
1881 max_seq
= this_bundle
->max_seq
;
1882 maskSeq
= max_seq
- 1;
1885 * Expand sequence number to 32 bits, making it as close
1886 * as possible to this_fragmentation->M.
1888 seq_num
|= this_fragmentation
->M
& ~maskSeq
;
1889 if ((int)(this_fragmentation
->M
- seq_num
) > (int)(maskSeq
>> 1))
1891 seq_num
+= maskSeq
+ 1;
1893 else if ((int)(seq_num
- this_fragmentation
->M
) > (int)(maskSeq
>> 1))
1895 seq_num
-= maskSeq
+ 1; /* should never happen */
1898 // calculate this fragment's offset from the begin seq in the bundle
1899 frag_offset
= (int) (seq_num
- this_fragmentation
->start_seq
);
1901 sess_local
[s
].last_seq
= seq_num
;
1903 // calculate the jitter average
1904 uint32_t ljitter
= time_now_ms
- sess_local
[s
].prev_time
;
1907 sess_local
[s
].jitteravg
= (sess_local
[s
].jitteravg
+ ljitter
)>>1;
1908 sess_local
[s
].prev_time
= time_now_ms
;
1913 if (seq_num
< this_fragmentation
->M
)
1916 this_fragmentation
->M
= seq_num
;
1920 Mmin
= sess_local
[(this_bundle
->members
[0])].last_seq
;
1921 for (i
= 1; i
< this_bundle
->num_of_links
; i
++)
1923 uint32_t s_seq
= sess_local
[(this_bundle
->members
[i
])].last_seq
;
1927 this_fragmentation
->M
= Mmin
;
1930 // calculate M offset of the M seq in the bundle
1931 int M_offset
= (int) (Mmin
- this_fragmentation
->start_seq
);
1933 if (M_offset
>= MAXFRAGNUM
)
1935 // There have a long break of the link !!!!!!!!
1936 // M_offset is bigger that the fragmentation buffer size
1937 LOG(3, s
, t
, "MPPP: M_offset out of range, min:%d, begin_seq:%d\n", Mmin
, this_fragmentation
->start_seq
);
1939 // Calculate the new start index, the previous frag are lost
1940 begin_index
= (M_offset
+ this_fragmentation
->start_index
) & MAXFRAGNUM_MASK
;
1942 // Set new Start sequence
1943 this_fragmentation
->start_index
= begin_index
;
1944 this_fragmentation
->start_seq
= Mmin
;
1946 // recalculate the fragment offset from the new begin seq in the bundle
1947 frag_offset
= (int) (seq_num
- Mmin
);
1950 // discard this fragment if the packet comes before the start sequence
1951 if (frag_offset
< 0)
1953 // this packet comes before the next
1954 LOG(3, s
, t
, "MPPP: (COMES BEFORE) the next, seq:%d, begin_seq:%d, size_frag:%d, flags:%02X is LOST\n", seq_num
, this_fragmentation
->start_seq
, l
, flags
);
1958 // discard if frag_offset is bigger that the fragmentation buffer size
1959 if (frag_offset
>= MAXFRAGNUM
)
1961 // frag_offset is bigger that the fragmentation buffer size
1962 LOG(3, s
, t
, "MPPP: Index out of range, seq:%d, begin_seq:%d\n", seq_num
, this_fragmentation
->start_seq
);
1966 //caculate received fragment's index in the fragment array
1967 frag_index
= (frag_offset
+ this_fragmentation
->start_index
) & MAXFRAGNUM_MASK
;
1969 // insert the frame in it's place
1970 fragmentt
*this_frag
= &this_fragmentation
->fragment
[frag_index
];
1972 if (this_frag
->length
> 0)
1973 // This fragment is lost, It was around the buffer and it was never completed the packet.
1974 LOG(3, this_frag
->sid
, this_frag
->tid
, "MPPP: (INSERT) seq_num:%d frag_index:%d flags:%02X is LOST\n",
1975 this_frag
->seq
, frag_index
, this_frag
->flags
);
1977 this_frag
->length
= l
;
1980 this_frag
->flags
= flags
;
1981 this_frag
->seq
= seq_num
;
1982 this_frag
->jitteravg
= sess_local
[s
].jitteravg
;
1983 memcpy(this_frag
->data
, p
, l
);
1985 LOG(4, s
, t
, "MPPP: seq_num:%d frag_index:%d INSERTED flags: %02X\n", seq_num
, frag_index
, flags
);
1988 frag_index_next
= (frag_index
+ 1) & MAXFRAGNUM_MASK
;
1989 //previous frag index
1990 frag_index_prev
= (frag_index
- 1) & MAXFRAGNUM_MASK
;
1992 seq_num_next
= seq_num
+ 1;
1994 seq_num_prev
= seq_num
- 1;
1996 // Clean the buffer and log the lost fragments
1997 if ((frag_index_next
!= this_fragmentation
->start_index
) && this_fragmentation
->fragment
[frag_index_next
].length
)
1999 // check if the next frag is a lost fragment
2000 if (this_fragmentation
->fragment
[frag_index_next
].seq
!= seq_num_next
)
2002 // This fragment is lost, It was around the buffer and it was never completed the packet.
2003 LOG(3, this_fragmentation
->fragment
[frag_index_next
].sid
, this_fragmentation
->fragment
[frag_index_next
].tid
,
2004 "MPPP: (NEXT) seq_num:%d frag_index:%d flags:%02X is LOST\n",
2005 this_fragmentation
->fragment
[frag_index_next
].seq
, frag_index_next
,
2006 this_fragmentation
->fragment
[frag_index_next
].flags
);
2007 // this frag is lost
2008 this_fragmentation
->fragment
[frag_index_next
].length
= 0;
2009 this_fragmentation
->fragment
[frag_index_next
].flags
= 0;
2011 if (begin_frame
&& (!end_frame
)) return; // assembling frame failed
2015 // Clean the buffer and log the lost fragments
2016 if ((frag_index
!= this_fragmentation
->start_index
) && this_fragmentation
->fragment
[frag_index_prev
].length
)
2018 // check if the next frag is a lost fragment
2019 if (this_fragmentation
->fragment
[frag_index_prev
].seq
!= seq_num_prev
)
2021 // This fragment is lost, It was around the buffer and it was never completed the packet.
2022 LOG(3, this_fragmentation
->fragment
[frag_index_prev
].sid
, this_fragmentation
->fragment
[frag_index_prev
].tid
,
2023 "MPPP: (PREV) seq_num:%d frag_index:%d flags:%02X is LOST\n",
2024 this_fragmentation
->fragment
[frag_index_prev
].seq
, frag_index_prev
,
2025 this_fragmentation
->fragment
[frag_index_prev
].flags
);
2027 this_fragmentation
->fragment
[frag_index_prev
].length
= 0;
2028 this_fragmentation
->fragment
[frag_index_prev
].flags
= 0;
2030 if (end_frame
&& (!begin_frame
)) return; // assembling frame failed
2035 begin_index
= this_fragmentation
->start_index
;
2036 uint32_t b_seq
= this_fragmentation
->start_seq
;
2037 // Try to find a Begin sequence from the start_seq sequence to M sequence
2038 while (b_seq
< Mmin
)
2040 if (this_fragmentation
->fragment
[begin_index
].length
)
2042 if (b_seq
== this_fragmentation
->fragment
[begin_index
].seq
)
2044 if (this_fragmentation
->fragment
[begin_index
].flags
& MP_BEGIN
)
2047 // Adjust the new start sequence and start index
2048 this_fragmentation
->start_index
= begin_index
;
2049 this_fragmentation
->start_seq
= b_seq
;
2050 // Begin Sequence found, now try to found the End Sequence to complete the frame
2051 end_index
= begin_index
;
2052 while (b_seq
< Mmin
)
2054 if (this_fragmentation
->fragment
[end_index
].length
)
2056 if (b_seq
== this_fragmentation
->fragment
[end_index
].seq
)
2058 if (this_fragmentation
->fragment
[end_index
].flags
& MP_END
)
2060 // The End sequence was found and the frame is complete
2067 // This fragment is lost, it was never completed the packet.
2068 LOG(3, this_fragmentation
->fragment
[end_index
].sid
, this_fragmentation
->fragment
[end_index
].tid
,
2069 "MPPP: (FIND END) seq_num:%d frag_index:%d flags:%02X is LOST\n",
2070 this_fragmentation
->fragment
[end_index
].seq
, begin_index
,
2071 this_fragmentation
->fragment
[end_index
].flags
);
2072 // this frag is lost
2073 this_fragmentation
->fragment
[end_index
].length
= 0;
2074 this_fragmentation
->fragment
[end_index
].flags
= 0;
2075 // This frame is not complete find the next Begin
2081 // This frame is not complete find the next Begin if exist
2084 end_index
= (end_index
+1) & MAXFRAGNUM_MASK
;
2089 // The End sequence was found and the frame is complete
2092 // find the next Begin
2093 begin_index
= end_index
;
2098 // This fragment is lost, it was never completed the packet.
2099 LOG(3, this_fragmentation
->fragment
[begin_index
].sid
, this_fragmentation
->fragment
[begin_index
].tid
,
2100 "MPPP: (FIND BEGIN) seq_num:%d frag_index:%d flags:%02X is LOST\n",
2101 this_fragmentation
->fragment
[begin_index
].seq
, begin_index
,
2102 this_fragmentation
->fragment
[begin_index
].flags
);
2103 // this frag is lost
2104 this_fragmentation
->fragment
[begin_index
].length
= 0;
2105 this_fragmentation
->fragment
[begin_index
].flags
= 0;
2108 begin_index
= (begin_index
+1) & MAXFRAGNUM_MASK
;
2113 // try to assemble the frame that has the received fragment as a member
2114 // get the beginning of this frame
2115 begin_index
= end_index
= this_fragmentation
->start_index
;
2116 if (this_fragmentation
->fragment
[begin_index
].length
)
2118 if (!(this_fragmentation
->fragment
[begin_index
].flags
& MP_BEGIN
))
2120 LOG(3, this_fragmentation
->fragment
[begin_index
].sid
, this_fragmentation
->fragment
[begin_index
].tid
,
2121 "MPPP: (NOT BEGIN) seq_num:%d frag_index:%d flags:%02X\n",
2122 this_fragmentation
->fragment
[begin_index
].seq
, begin_index
,
2123 this_fragmentation
->fragment
[begin_index
].flags
);
2124 // should occur only after an "M_Offset out of range"
2125 // The start sequence must be a begin sequence
2126 this_fragmentation
->start_index
= (begin_index
+1) & MAXFRAGNUM_MASK
;
2127 this_fragmentation
->start_seq
++;
2128 return; // assembling frame failed
2132 return; // assembling frame failed
2134 // get the end of his frame
2135 while (this_fragmentation
->fragment
[end_index
].length
)
2137 if (this_fragmentation
->fragment
[end_index
].flags
& MP_END
)
2140 end_index
= (end_index
+1) & MAXFRAGNUM_MASK
;
2142 if (end_index
== this_fragmentation
->start_index
)
2143 return; // assembling frame failed
2146 // return if a lost fragment is found
2147 if (!(this_fragmentation
->fragment
[end_index
].length
))
2148 return; // assembling frame failed
2150 // assemble the packet
2151 //assemble frame, process it, reset fragmentation
2152 uint16_t cur_len
= 4; // This is set to 4 to leave 4 bytes for function processipin
2154 LOG(4, s
, t
, "MPPP: processing fragments from %d to %d\n", begin_index
, end_index
);
2155 // Push to the receive buffer
2157 for (i
= begin_index
;; i
= (i
+ 1) & MAXFRAGNUM_MASK
)
2159 this_frag
= &this_fragmentation
->fragment
[i
];
2160 if(cur_len
+ this_frag
->length
> MAXETHER
)
2162 LOG(2, s
, t
, "MPPP: discarding reassembled frames larger than MAXETHER\n");
2166 memcpy(this_fragmentation
->reassembled_frame
+cur_len
, this_frag
->data
, this_frag
->length
);
2167 LOG(5, s
, t
, "MPPP: processing frame at %d, with len %d\n", i
, this_frag
->length
);
2169 cur_len
+= this_frag
->length
;
2172 this_fragmentation
->re_frame_len
= cur_len
;
2173 this_fragmentation
->re_frame_begin_index
= begin_index
;
2174 this_fragmentation
->re_frame_end_index
= end_index
;
2175 // Process the resassembled frame
2176 LOG(5, s
, t
, "MPPP: Process the reassembled frame, len=%d\n",cur_len
);
2177 processmpframe(s
, t
, this_fragmentation
->reassembled_frame
, this_fragmentation
->re_frame_len
, 1);
2182 // Set reassembled frame length to zero after processing it
2183 this_fragmentation
->re_frame_len
= 0;
2184 for (i
= begin_index
;; i
= (i
+ 1) & MAXFRAGNUM_MASK
)
2186 this_fragmentation
->fragment
[i
].length
= 0; // Indicates that this fragment has been consumed
2187 this_fragmentation
->fragment
[i
].flags
= 0;
2192 // Set the new start_index and start_seq
2193 this_fragmentation
->start_index
= (end_index
+ 1) & MAXFRAGNUM_MASK
;
2194 this_fragmentation
->start_seq
= this_fragmentation
->fragment
[end_index
].seq
+ 1;
2195 LOG(4, s
, t
, "MPPP after assembling: start index is = %d, start seq=%d\n", this_fragmentation
->start_index
, this_fragmentation
->start_seq
);
2197 begin_index
= this_fragmentation
->start_index
;
2198 if ((this_fragmentation
->fragment
[begin_index
].length
) &&
2199 (this_fragmentation
->fragment
[begin_index
].seq
!= this_fragmentation
->start_seq
))
2201 LOG(3, this_fragmentation
->fragment
[begin_index
].sid
, this_fragmentation
->fragment
[begin_index
].tid
,
2202 "MPPP: (START) seq_num:%d frag_index:%d flags:%02X is LOST\n",
2203 this_fragmentation
->fragment
[begin_index
].seq
, begin_index
,
2204 this_fragmentation
->fragment
[begin_index
].flags
);
2205 this_fragmentation
->fragment
[begin_index
].length
= 0;
2206 this_fragmentation
->fragment
[begin_index
].flags
= 0;
2209 if (this_fragmentation
->start_seq
<= Mmin
)
2210 // It's possible to find other complete frame or lost frame.
2212 else if ((this_fragmentation
->fragment
[begin_index
].length
) &&
2213 (this_fragmentation
->fragment
[begin_index
].flags
& MP_BEGIN
))
2214 // may be that the next frame is completed
2215 goto assembling_frame
;
2220 // process IPv6 packet received
2222 // This MUST be called with at least 4 byte behind 'p'.
2223 // (i.e. this routine writes to p[-4]).
2224 void processipv6in(sessionidt s
, tunnelidt t
, uint8_t *p
, uint16_t l
)
2229 CSTAT(processipv6in
);
2231 LOG_HEX(5, "IPv6", p
, l
);
2233 ip
= *(struct in6_addr
*) (p
+ 8);
2234 ipv4
= ntohl(*(uint32_t *)(p
+ 16));
2238 LOG(1, s
, t
, "IP packet too long %d\n", l
);
2239 STAT(tunnel_rx_errors
);
2243 if (session
[s
].ppp
.phase
!= Network
|| session
[s
].ppp
.ipv6cp
!= Opened
)
2247 if (ipv4
!= session
[s
].ip
&& memcmp(&config
->ipv6_prefix
, &ip
, 8) && sessionbyipv6(ip
) != s
)
2249 char str
[INET6_ADDRSTRLEN
];
2250 LOG(5, s
, t
, "Dropping packet with spoofed IP %s\n",
2251 inet_ntop(AF_INET6
, &ip
, str
, INET6_ADDRSTRLEN
));
2255 // Check if it's a Router Solicition message.
2256 if (*(p
+ 6) == 58 && *(p
+ 7) == 255 && *(p
+ 24) == 0xFF && *(p
+ 25) == 2 &&
2257 *(uint32_t *)(p
+ 26) == 0 && *(uint32_t *)(p
+ 30) == 0 &&
2258 *(uint32_t *)(p
+ 34) == 0 &&
2259 *(p
+ 38) == 0 && *(p
+ 39) == 2 && *(p
+ 40) == 133) {
2260 LOG(3, s
, t
, "Got IPv6 RS\n");
2261 send_ipv6_ra(s
, t
, &ip
);
2265 // Add on the tun header
2267 *(uint32_t *) p
= htonl(PKTIPV6
);
2270 // Are we throttled and a slave?
2271 if (session
[s
].tbf_in
&& !config
->cluster_iam_master
) {
2272 // Pass it to the master for handling.
2273 master_throttle_packet(session
[s
].tbf_in
, p
, l
);
2277 // Are we throttled and a master??
2278 if (session
[s
].tbf_in
&& config
->cluster_iam_master
) {
2279 // Actually handle the throttled packets.
2280 tbf_queue_packet(session
[s
].tbf_in
, p
, l
);
2285 if (tun_write(p
, l
) < 0)
2287 STAT(tun_tx_errors
);
2288 LOG(0, s
, t
, "Error writing %d bytes to TUN device: %s (tunfd=%d, p=%p)\n",
2289 l
, strerror(errno
), tunfd
, p
);
2297 if (session
[s
].snoop_ip
&& session
[s
].snoop_port
)
2299 // Snooping this session
2300 snoop_send_packet(p
, l
, session
[s
].snoop_ip
, session
[s
].snoop_port
);
2303 update_sessions_in_stat(s
, l
);
2307 STAT(tun_tx_packets
);
2308 INC_STAT(tun_tx_bytes
, l
);
2312 // Helper routine for the TBF filters.
2313 // Used to send queued data in from the user.
2315 void send_ipin(sessionidt s
, uint8_t *buf
, int len
)
2317 LOG_HEX(5, "IP in throttled", buf
, len
);
2319 if (write(tunfd
, buf
, len
) < 0)
2321 STAT(tun_tx_errors
);
2322 LOG(0, 0, 0, "Error writing %d bytes to TUN device: %s (tunfd=%d, p=%p)\n",
2323 len
, strerror(errno
), tunfd
, buf
);
2331 if (session
[s
].snoop_ip
&& session
[s
].snoop_port
)
2333 // Snooping this session
2334 snoop_send_packet(buf
, len
, session
[s
].snoop_ip
, session
[s
].snoop_port
);
2337 // Increment packet counters
2338 increment_counter(&session
[s
].cin
, &session
[s
].cin_wrap
, len
);
2339 session
[s
].cin_delta
+= len
;
2342 sess_local
[s
].cin
+= len
;
2343 sess_local
[s
].pin
++;
2347 STAT(tun_tx_packets
);
2348 INC_STAT(tun_tx_bytes
, len
- 4);
2352 // Process CCP messages
2353 void processccp(sessionidt s
, tunnelidt t
, uint8_t *p
, uint16_t l
)
2355 uint8_t b
[MAXETHER
];
2360 LOG_HEX(5, "CCP", p
, l
);
2362 if (session
[s
].ppp
.phase
< Network
)
2364 LOG(2, s
, t
, "CCP %s ignored in %s phase\n", ppp_code(*p
), ppp_phase(session
[s
].ppp
.phase
));
2370 LOG(1, s
, t
, "Short CCP packet\n");
2371 STAT(tunnel_rx_errors
);
2374 LOG(4, s
, t
, "CCP: recv %s\n", ppp_code(*p
));
2375 if (*p
== ConfigAck
)
2377 switch (session
[s
].ppp
.ccp
)
2380 initialise_restart_count(s
, ccp
);
2381 change_state(s
, ccp
, AckReceived
);
2386 LOG(2, s
, t
, "CCP: ConfigAck in state %s? Sending ConfigReq\n", ppp_state(session
[s
].ppp
.ccp
));
2388 change_state(s
, ccp
, RequestSent
);
2392 LOG(3, s
, t
, "CCP: Opened\n");
2393 change_state(s
, ccp
, Opened
);
2397 LOG(2, s
, t
, "CCP: ignoring %s in state %s\n", ppp_code(*p
), ppp_state(session
[s
].ppp
.ccp
));
2400 else if (*p
== ConfigReq
)
2402 if (l
< 6) // accept no compression
2404 else // compression requested--reject
2407 q
= makeppp(b
, sizeof(b
), p
, l
, s
, t
, PPPCCP
, 0, 0, 0);
2410 switch (session
[s
].ppp
.ccp
)
2413 q
= makeppp(b
, sizeof(b
), p
, 2, s
, t
, PPPCCP
, 0, 0, 0);
2416 *((uint16_t *) (q
+ 2)) = htons(l
= 4);
2420 initialise_restart_count(s
, ccp
);
2422 if (*q
== ConfigAck
)
2423 change_state(s
, ccp
, AckSent
);
2425 change_state(s
, ccp
, RequestSent
);
2430 if (*q
== ConfigAck
)
2431 change_state(s
, ccp
, AckSent
);
2436 if (*q
== ConfigAck
)
2437 change_state(s
, ccp
, Opened
);
2442 initialise_restart_count(s
, ccp
);
2447 if (*q
== ConfigAck
)
2448 change_state(s
, ccp
, AckSent
);
2450 change_state(s
, ccp
, RequestSent
);
2455 LOG(2, s
, t
, "CCP: ignoring %s in state %s\n", ppp_code(*p
), ppp_state(session
[s
].ppp
.ccp
));
2459 LOG(4, s
, t
, "CCP: send %s\n", ppp_code(*q
));
2460 tunnelsend(b
, l
+ (q
- b
), t
);
2462 else if (*p
== TerminateReq
)
2465 q
= makeppp(b
, sizeof(b
), p
, l
, s
, t
, PPPCCP
, 0, 0, 0);
2467 LOG(3, s
, t
, "CCP: send %s\n", ppp_code(*q
));
2468 tunnelsend(b
, l
+ (q
- b
), t
);
2469 change_state(s
, ccp
, Stopped
);
2471 else if (*p
!= CodeRej
)
2473 ppp_code_rej(s
, t
, PPPCCP
, "CCP", p
, l
, b
, sizeof(b
));
2477 // send a CHAP challenge
2478 void sendchap(sessionidt s
, tunnelidt t
)
2480 uint8_t b
[MAXETHER
];
2489 LOG(1, s
, t
, "No RADIUS to send challenge\n");
2490 STAT(tunnel_tx_errors
);
2494 LOG(1, s
, t
, "Send CHAP challenge\n");
2496 radius
[r
].chap
= 1; // CHAP not PAP
2498 if (radius
[r
].state
!= RADIUSCHAP
)
2501 radius
[r
].state
= RADIUSCHAP
;
2502 radius
[r
].retry
= backoff(radius
[r
].try++);
2503 if (radius
[r
].try > 5)
2505 sessionshutdown(s
, "CHAP timeout.", CDN_ADMIN_DISC
, TERM_REAUTHENTICATION_FAILURE
);
2506 STAT(tunnel_tx_errors
);
2509 q
= makeppp(b
, sizeof(b
), 0, 0, s
, t
, PPPCHAP
, 0, 0, 0);
2512 *q
= 1; // challenge
2513 q
[1] = radius
[r
].id
; // ID
2514 q
[4] = 16; // value size (size of challenge)
2515 memcpy(q
+ 5, radius
[r
].auth
, 16); // challenge
2516 strcpy((char *) q
+ 21, hostname
); // our name
2517 *(uint16_t *) (q
+ 2) = htons(strlen(hostname
) + 21); // length
2518 tunnelsend(b
, strlen(hostname
) + 21 + (q
- b
), t
); // send it
2521 // fill in a L2TP message with a PPP frame,
2522 // returns start of PPP frame
2523 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
)
2525 uint16_t hdr
= 0x0002; // L2TP with no options
2526 uint16_t type
= mtype
;
2529 if (t
== TUNNEL_ID_PPPOE
)
2531 return pppoe_makeppp(b
, size
, p
, l
, s
, t
, mtype
, prio
, bid
, mp_bits
);
2534 if (size
< 16) // Need more space than this!!
2536 LOG(0, s
, t
, "makeppp buffer too small for L2TP header (size=%d)\n", size
);
2540 if (prio
) hdr
|= 0x0100; // set priority bit
2542 *(uint16_t *) (b
+ 0) = htons(hdr
);
2543 *(uint16_t *) (b
+ 2) = htons(tunnel
[t
].far
); // tunnel
2544 *(uint16_t *) (b
+ 4) = htons(session
[s
].far
); // session
2547 // Check whether this session is part of multilink
2550 if (bundle
[bid
].num_of_links
> 1)
2551 type
= PPPMP
; // Change PPP message type to the PPPMP
2556 if (type
== PPPLCP
|| !(session
[s
].flags
& SESSION_ACFC
))
2558 *(uint16_t *) b
= htons(0xFF03); // HDLC header
2562 if (type
< 0x100 && session
[s
].flags
& SESSION_PFC
)
2568 *(uint16_t *) b
= htons(type
);
2574 // Set the sequence number and (B)egin (E)nd flags
2575 if (session
[s
].mssf
)
2577 // Set the multilink bits
2578 uint16_t bits_send
= mp_bits
;
2579 *(uint16_t *) b
= htons((bundle
[bid
].seq_num_t
& 0x0FFF)|bits_send
);
2584 *(uint32_t *) b
= htonl(bundle
[bid
].seq_num_t
);
2585 // Set the multilink bits
2590 bundle
[bid
].seq_num_t
++;
2592 // Add the message type if this fragment has the begin bit set
2593 if (mp_bits
& MP_BEGIN
)
2595 //*b++ = mtype; // The next two lines are instead of this
2596 *(uint16_t *) b
= htons(mtype
); // Message type
2601 if ((b
- start
) + l
> size
)
2603 LOG(2, s
, t
, "makeppp would overflow buffer (size=%d, header+payload=%td)\n", size
, (b
- start
) + l
);
2614 // fill in a L2TP message with a PPP frame,
2615 // returns start of PPP frame
2616 //(note: THIS ROUTINE CAN WRITES TO p[-28]).
2617 uint8_t *opt_makeppp(uint8_t *p
, int l
, sessionidt s
, tunnelidt t
, uint16_t mtype
, uint8_t prio
, bundleidt bid
, uint8_t mp_bits
)
2619 uint16_t hdr
= 0x0002; // L2TP with no options
2620 uint16_t type
= mtype
;
2623 if (t
== TUNNEL_ID_PPPOE
)
2625 return opt_pppoe_makeppp(p
, l
, s
, t
, mtype
, prio
, bid
, mp_bits
);
2628 // Check whether this session is part of multilink
2631 if (bundle
[bid
].num_of_links
> 1)
2632 type
= PPPMP
; // Change PPP message type to the PPPMP
2639 // Add the message type if this fragment has the begin bit set
2640 if (mp_bits
& MP_BEGIN
)
2643 *(uint16_t *) b
= htons(mtype
); // Message type
2646 // Set the sequence number and (B)egin (E)nd flags
2647 if (session
[s
].mssf
)
2649 // Set the multilink bits
2650 uint16_t bits_send
= mp_bits
;
2652 *(uint16_t *) b
= htons((bundle
[bid
].seq_num_t
& 0x0FFF)|bits_send
);
2657 *(uint32_t *) b
= htonl(bundle
[bid
].seq_num_t
);
2658 // Set the multilink bits
2662 bundle
[bid
].seq_num_t
++;
2665 if (type
< 0x100 && session
[s
].flags
& SESSION_PFC
)
2673 *(uint16_t *) b
= htons(type
);
2676 if (type
== PPPLCP
|| !(session
[s
].flags
& SESSION_ACFC
))
2679 *(uint16_t *) b
= htons(0xFF03); // HDLC header
2682 if (prio
) hdr
|= 0x0100; // set priority bit
2684 *(uint16_t *) b
= htons(session
[s
].far
); // session
2686 *(uint16_t *) b
= htons(tunnel
[t
].far
); // tunnel
2688 *(uint16_t *) b
= htons(hdr
);
2693 static int add_lcp_auth(uint8_t *b
, int size
, int authtype
)
2696 if ((authtype
== AUTHCHAP
&& size
< 5) || size
< 4)
2699 *b
++ = 3; // Authentication-Protocol
2700 if (authtype
== AUTHCHAP
)
2702 len
= *b
++ = 5; // length
2703 *(uint16_t *) b
= htons(PPPCHAP
); b
+= 2;
2706 else if (authtype
== AUTHPAP
)
2708 len
= *b
++ = 4; // length
2709 *(uint16_t *) b
= htons(PPPPAP
); b
+= 2;
2713 LOG(0, 0, 0, "add_lcp_auth called with unsupported auth type %d\n", authtype
);
2719 // Send LCP ConfigReq for MRU, authentication type and magic no
2720 void sendlcp(sessionidt s
, tunnelidt t
)
2722 uint8_t b
[500], *q
, *l
;
2723 int authtype
= sess_local
[s
].lcp_authtype
;
2725 if (!(q
= makeppp(b
, sizeof(b
), NULL
, 0, s
, t
, PPPLCP
, 0, 0, 0)))
2728 LOG(3, s
, t
, "LCP: send ConfigReq%s%s%s including MP options\n",
2729 authtype
? " (" : "",
2730 authtype
? (authtype
== AUTHCHAP
? "CHAP" : "PAP") : "",
2731 authtype
? ")" : "");
2735 *l
++ = ++sess_local
[s
].lcp_ident
; // ID
2737 l
+= 2; //Save space for length
2739 if (sess_local
[s
].ppp_mru
)
2741 *l
++ = 1; *l
++ = 4; // Maximum-Receive-Unit (length 4)
2742 *(uint16_t *) l
= htons(sess_local
[s
].ppp_mru
); l
+= 2;
2746 l
+= add_lcp_auth(l
, sizeof(b
) - (l
- b
), authtype
);
2748 if (session
[s
].magic
)
2750 *l
++ = 5; *l
++ = 6; // Magic-Number (length 6)
2751 *(uint32_t *) l
= htonl(session
[s
].magic
);
2755 if (sess_local
[s
].mp_mrru
)
2757 *l
++ = 17; *l
++ = 4; // Multilink Max-Receive-Reconstructed-Unit (length 4)
2758 *(uint16_t *) l
= htons(sess_local
[s
].mp_mrru
); l
+= 2;
2761 if (sess_local
[s
].mp_epdis
)
2763 *l
++ = 19; *l
++ = 7; // Multilink Endpoint Discriminator (length 7)
2764 *l
++ = IPADDR
; // Endpoint Discriminator class
2765 *(uint32_t *) l
= htonl(sess_local
[s
].mp_epdis
);
2769 *(uint16_t *)(q
+ 2) = htons(l
- q
); // Length
2771 LOG_HEX(5, "PPPLCP", q
, l
- q
);
2772 if (config
->debug
> 3) dumplcp(q
, l
- q
);
2774 tunnelsend(b
, (l
- b
), t
);
2775 restart_timer(s
, lcp
);
2778 // Send CCP request for no compression
2779 void sendccp(sessionidt s
, tunnelidt t
)
2783 if (!(q
= makeppp(b
, sizeof(b
), NULL
, 0, s
, t
, PPPCCP
, 0, 0, 0)))
2786 LOG(3, s
, t
, "CCP: send ConfigReq (no compression)\n");
2789 *(q
+ 1) = ++sess_local
[s
].lcp_ident
; // ID
2790 *(uint16_t *)(q
+ 2) = htons(4); // Length
2792 LOG_HEX(5, "PPPCCP", q
, 4);
2793 tunnelsend(b
, (q
- b
) + 4 , t
);
2794 restart_timer(s
, ccp
);
2797 // Reject unknown/unconfigured protocols
2798 void protoreject(sessionidt s
, tunnelidt t
, uint8_t *p
, uint16_t l
, uint16_t proto
)
2801 uint8_t buf
[MAXETHER
];
2803 int mru
= session
[s
].mru
;
2804 if (mru
< MINMTU
) mru
= MINMTU
;
2805 if (mru
> sizeof(buf
)) mru
= sizeof(buf
);
2808 if (l
> mru
) l
= mru
;
2810 q
= makeppp(buf
, sizeof(buf
), 0, 0, s
, t
, PPPLCP
, 0, 0, 0);
2814 *(q
+ 1) = ++sess_local
[s
].lcp_ident
;
2815 *(uint16_t *)(q
+ 2) = htons(l
);
2816 *(uint16_t *)(q
+ 4) = htons(proto
);
2817 memcpy(q
+ 6, p
, l
- 6);
2819 if (proto
== PPPIPV6CP
)
2820 LOG(3, s
, t
, "LCP: send ProtocolRej (IPV6CP: not configured)\n");
2822 LOG(2, s
, t
, "LCP: sent ProtocolRej (0x%04X: unsupported)\n", proto
);
2824 tunnelsend(buf
, l
+ (q
- buf
), t
);