8 #include <sys/socket.h>
9 #include <linux/rtnetlink.h>
12 #include "constants.h"
21 extern tunnelt
*tunnel
;
22 extern bundlet
*bundle
;
23 extern fragmentationt
*frag
;
24 extern sessiont
*session
;
25 extern radiust
*radius
;
27 extern char hostname
[];
28 extern uint32_t eth_tx
;
29 extern time_t time_now
;
30 extern uint64_t time_now_ms
;
31 extern configt
*config
;
33 static int add_lcp_auth(uint8_t *b
, int size
, int authtype
);
34 static bundleidt
new_bundle(void);
35 static int epdiscmp(epdist
, epdist
);
36 static void setepdis(epdist
*, epdist
);
37 static void ipcp_open(sessionidt s
, tunnelidt t
);
39 static int first_session_in_bundle(sessionidt s
)
42 for (i
= 1; i
< MAXBUNDLE
; i
++)
43 if (bundle
[i
].state
!= BUNDLEFREE
)
44 if (epdiscmp(session
[s
].epdis
,bundle
[i
].epdis
) && !strcmp(session
[s
].user
, bundle
[i
].user
))
49 // Process PAP messages
50 void processpap(sessionidt s
, tunnelidt t
, uint8_t *p
, uint16_t l
)
59 LOG_HEX(5, "PAP", p
, l
);
62 LOG(1, s
, t
, "Short PAP %u bytes\n", l
);
63 STAT(tunnel_rx_errors
);
64 sessionshutdown(s
, "Short PAP packet.", CDN_ADMIN_DISC
, TERM_USER_ERROR
);
68 if ((hl
= ntohs(*(uint16_t *) (p
+ 2))) > l
)
70 LOG(1, s
, t
, "Length mismatch PAP %u/%u\n", hl
, l
);
71 STAT(tunnel_rx_errors
);
72 sessionshutdown(s
, "PAP length mismatch.", CDN_ADMIN_DISC
, TERM_USER_ERROR
);
79 LOG(1, s
, t
, "Unexpected PAP code %d\n", *p
);
80 STAT(tunnel_rx_errors
);
81 sessionshutdown(s
, "Unexpected PAP code.", CDN_ADMIN_DISC
, TERM_USER_ERROR
);
85 if (session
[s
].ppp
.phase
!= Authenticate
)
87 LOG(2, s
, t
, "PAP ignored in %s phase\n", ppp_phase(session
[s
].ppp
.phase
));
94 user
[0] = pass
[0] = 0;
95 if (*b
&& *b
< sizeof(user
))
97 memcpy(user
, b
+ 1, *b
);
100 if (*b
&& *b
< sizeof(pass
))
102 memcpy(pass
, b
+ 1, *b
);
106 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
115 if (session
[s
].ip
|| !(r
= radiusnew(s
)))
117 // respond now, either no RADIUS available or already authenticated
120 uint8_t *p
= makeppp(b
, sizeof(b
), 0, 0, s
, t
, PPPPAP
, 0, 0, 0);
126 *p
= 3; // cant authorise
128 *(uint16_t *) (p
+ 2) = htons(5); // length
129 p
[4] = 0; // no message
130 tunnelsend(b
, 5 + (p
- b
), t
); // send it
134 LOG(3, s
, t
, "Already an IP allocated: %s (%d)\n",
135 fmtaddr(htonl(session
[s
].ip
), 0), session
[s
].ip_pool_index
);
139 LOG(1, s
, t
, "No RADIUS session available to authenticate session...\n");
140 sessionshutdown(s
, "No free RADIUS sessions.", CDN_UNAVAILABLE
, TERM_SERVICE_UNAVAILABLE
);
145 // Run PRE_AUTH plugins
146 struct param_pre_auth packet
= { &tunnel
[t
], &session
[s
], strdup(user
), strdup(pass
), PPPPAP
, 1 };
147 run_plugins(PLUGIN_PRE_AUTH
, &packet
);
148 if (!packet
.continue_auth
)
150 LOG(3, s
, t
, "A plugin rejected PRE_AUTH\n");
151 if (packet
.username
) free(packet
.username
);
152 if (packet
.password
) free(packet
.password
);
156 strncpy(session
[s
].user
, packet
.username
, sizeof(session
[s
].user
) - 1);
157 strncpy(radius
[r
].pass
, packet
.password
, sizeof(radius
[r
].pass
) - 1);
159 free(packet
.username
);
160 free(packet
.password
);
163 LOG(3, s
, t
, "Sending login for %s/%s to RADIUS\n", user
, pass
);
164 if ((session
[s
].mrru
) && (!first_session_in_bundle(s
)))
165 radiussend(r
, RADIUSJUSTAUTH
);
167 radiussend(r
, RADIUSAUTH
);
171 // Process CHAP messages
172 void processchap(sessionidt s
, tunnelidt t
, uint8_t *p
, uint16_t l
)
179 LOG_HEX(5, "CHAP", p
, l
);
183 LOG(1, s
, t
, "Short CHAP %u bytes\n", l
);
184 STAT(tunnel_rx_errors
);
185 sessionshutdown(s
, "Short CHAP packet.", CDN_ADMIN_DISC
, TERM_USER_ERROR
);
189 if ((hl
= ntohs(*(uint16_t *) (p
+ 2))) > l
)
191 LOG(1, s
, t
, "Length mismatch CHAP %u/%u\n", hl
, l
);
192 STAT(tunnel_rx_errors
);
193 sessionshutdown(s
, "CHAP length mismatch.", CDN_ADMIN_DISC
, TERM_USER_ERROR
);
200 LOG(1, s
, t
, "Unexpected CHAP response code %d\n", *p
);
201 STAT(tunnel_rx_errors
);
202 sessionshutdown(s
, "CHAP length mismatch.", CDN_ADMIN_DISC
, TERM_USER_ERROR
);
206 if (session
[s
].ppp
.phase
!= Authenticate
)
208 LOG(2, s
, t
, "CHAP ignored in %s phase\n", ppp_phase(session
[s
].ppp
.phase
));
212 r
= sess_local
[s
].radius
;
215 LOG(3, s
, t
, "Unexpected CHAP message\n");
217 // Some modems (Netgear DM602, possibly others) persist in using CHAP even
218 // after ACKing our ConfigReq for PAP.
219 if (sess_local
[s
].lcp_authtype
== AUTHPAP
&& config
->radius_authtypes
& AUTHCHAP
)
221 sess_local
[s
].lcp_authtype
= AUTHCHAP
;
227 if (p
[1] != radius
[r
].id
)
229 LOG(1, s
, t
, "Wrong CHAP response ID %d (should be %d) (%d)\n", p
[1], radius
[r
].id
, r
);
230 STAT(tunnel_rx_errors
);
231 sessionshutdown(s
, "Unexpected CHAP response ID.", CDN_ADMIN_DISC
, TERM_USER_ERROR
);
235 if (l
< 5 || p
[4] != 16)
237 LOG(1, s
, t
, "Bad CHAP response length %d\n", l
< 5 ? -1 : p
[4]);
238 STAT(tunnel_rx_errors
);
239 sessionshutdown(s
, "Bad CHAP response length.", CDN_ADMIN_DISC
, TERM_USER_ERROR
);
245 if (l
< 16 || l
- 16 >= sizeof(session
[s
].user
))
247 LOG(1, s
, t
, "CHAP user too long %d\n", l
- 16);
248 STAT(tunnel_rx_errors
);
249 sessionshutdown(s
, "CHAP username too long.", CDN_ADMIN_DISC
, TERM_USER_ERROR
);
253 // Run PRE_AUTH plugins
255 struct param_pre_auth packet
= { &tunnel
[t
], &session
[s
], NULL
, NULL
, PPPCHAP
, 1 };
257 packet
.password
= calloc(17, 1);
258 memcpy(packet
.password
, p
, 16);
263 packet
.username
= calloc(l
+ 1, 1);
264 memcpy(packet
.username
, p
, l
);
266 if ((!config
->disable_lac_func
) && lac_conf_forwardtoremotelns(s
, packet
.username
))
268 free(packet
.username
);
269 free(packet
.password
);
270 // Creating a tunnel/session has been started
274 run_plugins(PLUGIN_PRE_AUTH
, &packet
);
275 if (!packet
.continue_auth
)
277 LOG(3, s
, t
, "A plugin rejected PRE_AUTH\n");
278 if (packet
.username
) free(packet
.username
);
279 if (packet
.password
) free(packet
.password
);
283 strncpy(session
[s
].user
, packet
.username
, sizeof(session
[s
].user
) - 1);
284 memcpy(radius
[r
].pass
, packet
.password
, 16);
286 free(packet
.username
);
287 free(packet
.password
);
291 LOG(3, s
, t
, "CHAP login %s\n", session
[s
].user
);
292 if ((session
[s
].mrru
) && (!first_session_in_bundle(s
)))
293 radiussend(r
, RADIUSJUSTAUTH
);
295 radiussend(r
, RADIUSAUTH
);
298 static void dumplcp(uint8_t *p
, int l
)
301 uint8_t *o
= (p
+ 4);
303 LOG_HEX(5, "PPP LCP Packet", p
, l
);
304 LOG(4, 0, 0, "PPP LCP Packet type %d (%s len %d)\n", *p
, ppp_code((int)*p
), ntohs( ((uint16_t *) p
)[1]) );
305 LOG(4, 0, 0, "Length: %d\n", l
);
306 if (*p
!= ConfigReq
&& *p
!= ConfigRej
&& *p
!= ConfigAck
)
315 LOG(4, 0, 0, " Option length is %d...\n", length
);
320 LOG(4, 0, 0, " Option type is 0...\n");
327 case 1: // Maximum-Receive-Unit
329 LOG(4, 0, 0, " %s %d\n", ppp_lcp_option(type
), ntohs(*(uint16_t *)(o
+ 2)));
331 LOG(4, 0, 0, " %s odd length %d\n", ppp_lcp_option(type
), length
);
333 case 2: // Async-Control-Character-Map
336 uint32_t asyncmap
= ntohl(*(uint32_t *)(o
+ 2));
337 LOG(4, 0, 0, " %s %x\n", ppp_lcp_option(type
), asyncmap
);
340 LOG(4, 0, 0, " %s odd length %d\n", ppp_lcp_option(type
), length
);
342 case 3: // Authentication-Protocol
345 int proto
= ntohs(*(uint16_t *)(o
+ 2));
346 LOG(4, 0, 0, " %s 0x%x (%s)\n", ppp_lcp_option(type
), proto
,
347 proto
== PPPPAP
? "PAP" : "UNSUPPORTED");
349 else if (length
== 5)
351 int proto
= ntohs(*(uint16_t *)(o
+ 2));
353 LOG(4, 0, 0, " %s 0x%x 0x%x (%s)\n", ppp_lcp_option(type
), proto
, algo
,
354 (proto
== PPPCHAP
&& algo
== 5) ? "CHAP MD5" : "UNSUPPORTED");
357 LOG(4, 0, 0, " %s odd length %d\n", ppp_lcp_option(type
), length
);
359 case 4: // Quality-Protocol
361 uint32_t qp
= ntohl(*(uint32_t *)(o
+ 2));
362 LOG(4, 0, 0, " %s %x\n", ppp_lcp_option(type
), qp
);
365 case 5: // Magic-Number
368 uint32_t magicno
= ntohl(*(uint32_t *)(o
+ 2));
369 LOG(4, 0, 0, " %s %x\n", ppp_lcp_option(type
), magicno
);
372 LOG(4, 0, 0, " %s odd length %d\n", ppp_lcp_option(type
), length
);
374 case 7: // Protocol-Field-Compression
375 case 8: // Address-And-Control-Field-Compression
376 LOG(4, 0, 0, " %s\n", ppp_lcp_option(type
));
379 LOG(2, 0, 0, " Unknown PPP LCP Option type %d\n", type
);
387 void lcp_open(sessionidt s
, tunnelidt t
)
389 // transition to Authentication or Network phase:
390 session
[s
].ppp
.phase
= sess_local
[s
].lcp_authtype
? Authenticate
: Network
;
392 LOG(3, s
, t
, "LCP: Opened, phase %s\n", ppp_phase(session
[s
].ppp
.phase
));
395 change_state(s
, lcp
, Opened
);
397 if (session
[s
].ppp
.phase
== Authenticate
)
399 if (sess_local
[s
].lcp_authtype
== AUTHCHAP
)
404 if(session
[s
].bundle
== 0 || bundle
[session
[s
].bundle
].num_of_links
== 1)
408 change_state(s
, ipcp
, RequestSent
);
409 // move to passive state for IPv6 (if configured), CCP
410 if (config
->ipv6_prefix
.s6_addr
[0])
411 change_state(s
, ipv6cp
, Stopped
);
413 change_state(s
, ipv6cp
, Closed
);
415 change_state(s
, ccp
, Stopped
);
419 sessionidt first_ses
= bundle
[session
[s
].bundle
].members
[0];
420 LOG(3, s
, t
, "MPPP: Skipping IPCP negotiation for session:%d, first session of bundle is:%d\n",s
,first_ses
);
426 void lcp_restart(sessionidt s
)
428 session
[s
].ppp
.phase
= Establish
;
430 change_state(s
, ipcp
, Dead
);
431 change_state(s
, ipv6cp
, Dead
);
432 change_state(s
, ccp
, Dead
);
435 static uint8_t *ppp_conf_rej(sessionidt s
, uint8_t *buf
, size_t blen
, uint16_t mtype
,
436 uint8_t **response
, uint8_t *queued
, uint8_t *packet
, uint8_t *option
)
438 if (!*response
|| **response
!= ConfigRej
)
440 queued
= *response
= makeppp(buf
, blen
, packet
, 2, s
, session
[s
].tunnel
, mtype
, 0, 0, 0);
448 if ((queued
- buf
+ option
[1]) > blen
)
450 LOG(2, s
, session
[s
].tunnel
, "PPP overflow for ConfigRej (proto %u, option %u).\n", mtype
, *option
);
454 memcpy(queued
, option
, option
[1]);
455 return queued
+ option
[1];
458 static uint8_t *ppp_conf_nak(sessionidt s
, uint8_t *buf
, size_t blen
, uint16_t mtype
,
459 uint8_t **response
, uint8_t *queued
, uint8_t *packet
, uint8_t *option
,
460 uint8_t *value
, size_t vlen
)
465 case PPPLCP
: nak_sent
= &sess_local
[s
].lcp
.nak_sent
; break;
466 case PPPIPCP
: nak_sent
= &sess_local
[s
].ipcp
.nak_sent
; break;
467 case PPPIPV6CP
: nak_sent
= &sess_local
[s
].ipv6cp
.nak_sent
; break;
468 default: return 0; // ?
471 if (*response
&& **response
!= ConfigNak
)
473 if (*nak_sent
< config
->ppp_max_failure
) // reject queued
476 return ppp_conf_rej(s
, buf
, blen
, mtype
, response
, 0, packet
, option
);
481 if (*nak_sent
>= config
->ppp_max_failure
)
482 return ppp_conf_rej(s
, buf
, blen
, mtype
, response
, 0, packet
, option
);
484 queued
= *response
= makeppp(buf
, blen
, packet
, 2, s
, session
[s
].tunnel
, mtype
, 0, 0, 0);
493 if ((queued
- buf
+ vlen
+ 2) > blen
)
495 LOG(2, s
, session
[s
].tunnel
, "PPP overflow for ConfigNak (proto %u, option %u).\n", mtype
, *option
);
500 *queued
++ = vlen
+ 2;
501 memcpy(queued
, value
, vlen
);
502 return queued
+ vlen
;
505 static void ppp_code_rej(sessionidt s
, tunnelidt t
, uint16_t proto
,
506 char *pname
, uint8_t *p
, uint16_t l
, uint8_t *buf
, size_t size
)
509 int mru
= session
[s
].mru
;
510 if (mru
< MINMTU
) mru
= MINMTU
;
511 if (mru
> size
) mru
= size
;
514 if (l
> mru
) l
= mru
;
516 q
= makeppp(buf
, size
, 0, 0, s
, t
, proto
, 0, 0, 0);
520 *(q
+ 1) = ++sess_local
[s
].lcp_ident
;
521 *(uint16_t *)(q
+ 2) = htons(l
);
522 memcpy(q
+ 4, p
, l
- 4);
524 LOG(2, s
, t
, "Unexpected %s code %s\n", pname
, ppp_code(*p
));
525 LOG(3, s
, t
, "%s: send %s\n", pname
, ppp_code(*q
));
526 if (config
->debug
> 3) dumplcp(q
, l
);
528 tunnelsend(buf
, l
+ (q
- buf
), t
);
531 // Process LCP messages
532 void processlcp(sessionidt s
, tunnelidt t
, uint8_t *p
, uint16_t l
)
540 LOG_HEX(5, "LCP", p
, l
);
543 LOG(1, s
, t
, "Short LCP %d bytes\n", l
);
544 STAT(tunnel_rx_errors
);
548 if ((hl
= ntohs(*(uint16_t *) (p
+ 2))) > l
)
550 LOG(1, s
, t
, "Length mismatch LCP %u/%u\n", hl
, l
);
551 STAT(tunnel_rx_errors
);
556 if (session
[s
].die
) // going down...
559 LOG(((*p
== EchoReq
|| *p
== EchoReply
) ? 4 : 3), s
, t
,
560 "LCP: recv %s\n", ppp_code(*p
));
562 if (config
->debug
> 3) dumplcp(p
, l
);
567 uint8_t *o
= (p
+ 4);
575 if (length
== 0 || type
== 0 || x
< length
) break;
578 case 3: // Authentication-Protocol
580 int proto
= ntohs(*(uint16_t *)(o
+ 2));
583 else if (proto
== PPPCHAP
&& *(o
+ 4) == 5)
593 if (!session
[s
].ip
&& authtype
)
594 sess_local
[s
].lcp_authtype
= authtype
;
596 switch (session
[s
].ppp
.lcp
)
599 initialise_restart_count(s
, lcp
);
600 change_state(s
, lcp
, AckReceived
);
605 LOG(2, s
, t
, "LCP: ConfigAck in state %s? Sending ConfigReq\n", ppp_state(session
[s
].ppp
.lcp
));
606 if (session
[s
].ppp
.lcp
== Opened
)
610 change_state(s
, lcp
, RequestSent
);
618 LOG(2, s
, t
, "LCP: ignoring %s in state %s\n", ppp_code(*p
), ppp_state(session
[s
].ppp
.lcp
));
621 else if (*p
== ConfigReq
)
624 uint8_t *o
= (p
+ 4);
625 uint8_t *response
= 0;
626 static uint8_t asyncmap
[4] = { 0, 0, 0, 0 }; // all zero
627 static uint8_t authproto
[5];
635 if (length
== 0 || type
== 0 || x
< length
) break;
638 case 1: // Maximum-Receive-Unit
640 uint16_t mru
= ntohs(*(uint16_t *)(o
+ 2));
643 session
[s
].mru
= mru
;
648 LOG(3, s
, t
, " Remote requesting MRU of %u. Rejecting.\n", mru
);
650 q
= ppp_conf_nak(s
, b
, sizeof(b
), PPPLCP
, &response
, q
, p
, o
, (uint8_t *) &mru
, sizeof(mru
));
654 case 2: // Async-Control-Character-Map
655 if (!ntohl(*(uint32_t *)(o
+ 2))) // all bits zero is OK
658 LOG(3, s
, t
, " Remote requesting asyncmap. Rejecting.\n");
659 q
= ppp_conf_nak(s
, b
, sizeof(b
), PPPLCP
, &response
, q
, p
, o
, asyncmap
, sizeof(asyncmap
));
662 case 3: // Authentication-Protocol
664 int proto
= ntohs(*(uint16_t *)(o
+ 2));
665 char proto_name
[] = "0x0000";
670 if (config
->radius_authtypes
& AUTHPAP
)
672 sess_local
[s
].lcp_authtype
= AUTHPAP
;
676 strcpy(proto_name
, "PAP");
678 else if (proto
== PPPCHAP
)
680 if (config
->radius_authtypes
& AUTHCHAP
681 && *(o
+ 4) == 5) // MD5
683 sess_local
[s
].lcp_authtype
= AUTHCHAP
;
687 strcpy(proto_name
, "CHAP");
690 sprintf(proto_name
, "%#4.4x", proto
);
692 LOG(3, s
, t
, " Remote requesting %s authentication. Rejecting.\n", proto_name
);
694 alen
= add_lcp_auth(authproto
, sizeof(authproto
), config
->radius_authprefer
);
695 if (alen
< 2) break; // paranoia
697 q
= ppp_conf_nak(s
, b
, sizeof(b
), PPPLCP
, &response
, q
, p
, o
, authproto
+ 2, alen
- 2);
698 if (q
&& *response
== ConfigNak
&&
699 config
->radius_authtypes
!= config
->radius_authprefer
)
702 alen
= add_lcp_auth(authproto
, sizeof(authproto
), config
->radius_authtypes
& ~config
->radius_authprefer
);
704 q
= ppp_conf_nak(s
, b
, sizeof(b
), PPPLCP
, &response
, q
, p
, o
, authproto
+ 2, alen
- 2);
711 case 4: // Quality-Protocol
712 case 5: // Magic-Number
713 case 7: // Protocol-Field-Compression
714 case 8: // Address-And-Control-Field-Compression
717 case 17: // Multilink Max-Receive-Reconstructed-Unit
719 uint16_t mrru
= ntohs(*(uint16_t *)(o
+ 2));
720 session
[s
].mrru
= mrru
;
722 LOG(3, s
, t
, " Received PPP LCP option MRRU: %d\n",mrru
);
726 case 18: // Multilink Short Sequence Number Header Format
730 LOG(3, s
, t
, " Received PPP LCP option MSSN format\n");
734 case 19: // Multilink Endpoint Discriminator
736 uint8_t epdis_class
= o
[2];
739 session
[s
].epdis
.addr_class
= epdis_class
;
740 session
[s
].epdis
.length
= length
- 3;
741 if (session
[s
].epdis
.length
> 20)
743 LOG(1, s
, t
, "Error: received EndDis Address Length more than 20: %d\n", session
[s
].epdis
.length
);
744 session
[s
].epdis
.length
= 20;
747 for (addr
= 0; addr
< session
[s
].epdis
.length
; addr
++)
748 session
[s
].epdis
.address
[addr
] = o
[3+addr
];
755 LOG(3, s
, t
, " Received PPP LCP option Multilink EndDis Local Address Class: %d\n",epdis_class
);
758 LOG(3, s
, t
, " Received PPP LCP option Multilink EndDis IP Address Class: %d\n",epdis_class
);
761 LOG(3, s
, t
, " Received PPP LCP option Multilink EndDis IEEE MAC Address Class: %d\n",epdis_class
);
764 LOG(3, s
, t
, " Received PPP LCP option Multilink EndDis PPP Magic No Class: %d\n",epdis_class
);
767 LOG(3, s
, t
, " Received PPP LCP option Multilink EndDis PSND No Class: %d\n",epdis_class
);
770 LOG(3, s
, t
, " Received PPP LCP option Multilink EndDis NULL Class %d\n",epdis_class
);
775 default: // Reject any unknown options
776 LOG(3, s
, t
, " Rejecting unknown PPP LCP option %d\n", type
);
777 q
= ppp_conf_rej(s
, b
, sizeof(b
), PPPLCP
, &response
, q
, p
, o
);
784 cluster_send_session(s
);
788 l
= q
- response
; // LCP packet length
789 *((uint16_t *) (response
+ 2)) = htons(l
); // update header
793 // Send packet back as ConfigAck
794 response
= makeppp(b
, sizeof(b
), p
, l
, s
, t
, PPPLCP
, 0, 0, 0);
795 if (!response
) return;
796 *response
= ConfigAck
;
799 switch (session
[s
].ppp
.lcp
)
802 response
= makeppp(b
, sizeof(b
), p
, 2, s
, t
, PPPLCP
, 0, 0, 0);
803 if (!response
) return;
804 *response
= TerminateAck
;
805 *((uint16_t *) (response
+ 2)) = htons(l
= 4);
809 initialise_restart_count(s
, lcp
);
811 if (*response
== ConfigAck
)
812 change_state(s
, lcp
, AckSent
);
814 change_state(s
, lcp
, RequestSent
);
819 if (*response
== ConfigAck
)
820 change_state(s
, lcp
, AckSent
);
825 if (*response
== ConfigAck
)
836 if (*response
== ConfigAck
)
837 change_state(s
, lcp
, AckSent
);
839 change_state(s
, lcp
, RequestSent
);
844 sessionshutdown(s
, "LCP: ConfigReq in state Closing. This should not happen. Killing session.", CDN_ADMIN_DISC
, TERM_LOST_SERVICE
);
848 LOG(2, s
, t
, "LCP: ignoring %s in state %s\n", ppp_code(*p
), ppp_state(session
[s
].ppp
.lcp
));
852 LOG(3, s
, t
, "LCP: send %s\n", ppp_code(*response
));
853 if (config
->debug
> 3) dumplcp(response
, l
);
855 tunnelsend(b
, l
+ (response
- b
), t
);
857 else if (*p
== ConfigNak
|| *p
== ConfigRej
)
860 uint8_t *o
= (p
+ 4);
868 if (length
== 0 || type
== 0 || x
< length
) break;
871 case 1: // Maximum-Receive-Unit
874 if (length
< 4) break;
875 sess_local
[s
].ppp_mru
= ntohs(*(uint16_t *)(o
+ 2));
876 LOG(3, s
, t
, " Remote requested MRU of %u\n", sess_local
[s
].ppp_mru
);
880 sess_local
[s
].ppp_mru
= 0;
881 LOG(3, s
, t
, " Remote rejected MRU negotiation\n");
886 case 3: // Authentication-Protocol
894 if (length
< 4) break;
895 proto
= ntohs(*(uint16_t *)(o
+ 2));
899 authtype
= config
->radius_authtypes
& AUTHPAP
;
900 LOG(3, s
, t
, " Remote requested PAP authentication...%sing\n",
901 authtype
? "accept" : "reject");
903 else if (proto
== PPPCHAP
&& length
> 4 && *(o
+ 4) == 5)
905 authtype
= config
->radius_authtypes
& AUTHCHAP
;
906 LOG(3, s
, t
, " Remote requested CHAP authentication...%sing\n",
907 authtype
? "accept" : "reject");
911 LOG(3, s
, t
, " Rejecting unsupported authentication %#4x\n",
917 LOG(2, s
, t
, "LCP: remote rejected auth negotiation\n");
918 authtype
= 0; // shutdown
923 case 5: // Magic-Number
924 session
[s
].magic
= 0;
927 if (length
< 6) break;
928 session
[s
].magic
= ntohl(*(uint32_t *)(o
+ 2));
931 if (session
[s
].magic
)
932 LOG(3, s
, t
, " Remote requested magic-no %x\n", session
[s
].magic
);
934 LOG(3, s
, t
, " Remote rejected magic-no\n");
936 cluster_send_session(s
);
939 case 17: // Multilink Max-Receive-Reconstructed-Unit
943 sess_local
[s
].mp_mrru
= ntohs(*(uint16_t *)(o
+ 2));
944 LOG(3, s
, t
, " Remote requested MRRU of %u\n", sess_local
[s
].mp_mrru
);
948 sess_local
[s
].mp_mrru
= 0;
949 LOG(3, s
, t
, " Remote rejected MRRU negotiation\n");
954 case 18: // Multilink Short Sequence Number Header Format
958 sess_local
[s
].mp_mssf
= 0;
959 LOG(3, s
, t
, " Remote requested Naked mssf\n");
963 sess_local
[s
].mp_mssf
= 0;
964 LOG(3, s
, t
, " Remote rejected mssf\n");
969 case 19: // Multilink Endpoint Discriminator
973 LOG(2, s
, t
, " Remote should not configNak Endpoint Dis!\n");
977 sess_local
[s
].mp_epdis
= 0;
978 LOG(3, s
, t
, " Remote rejected Endpoint Discriminator\n");
984 LOG(2, s
, t
, "LCP: remote sent %s for type %u?\n", ppp_code(*p
), type
);
985 sessionshutdown(s
, "Unable to negotiate LCP.", CDN_ADMIN_DISC
, TERM_USER_ERROR
);
994 sessionshutdown(s
, "Unsupported authentication.", CDN_ADMIN_DISC
, TERM_USER_ERROR
);
999 sess_local
[s
].lcp_authtype
= authtype
;
1001 switch (session
[s
].ppp
.lcp
)
1006 uint8_t *response
= makeppp(b
, sizeof(b
), p
, 2, s
, t
, PPPLCP
, 0, 0, 0);
1007 if (!response
) return;
1008 *response
= TerminateAck
;
1009 *((uint16_t *) (response
+ 2)) = htons(l
= 4);
1011 LOG(3, s
, t
, "LCP: send %s\n", ppp_code(*response
));
1012 if (config
->debug
> 3) dumplcp(response
, l
);
1014 tunnelsend(b
, l
+ (response
- b
), t
);
1020 initialise_restart_count(s
, lcp
);
1025 LOG(2, s
, t
, "LCP: ConfigNak in state %s? Sending ConfigReq\n", ppp_state(session
[s
].ppp
.lcp
));
1035 LOG(2, s
, t
, "LCP: ignoring %s in state %s\n", ppp_code(*p
), ppp_state(session
[s
].ppp
.lcp
));
1039 else if (*p
== TerminateReq
)
1041 switch (session
[s
].ppp
.lcp
)
1054 zero_restart_count(s
, lcp
);
1055 change_state(s
, lcp
, Closing
);
1059 LOG(2, s
, t
, "LCP: ignoring %s in state %s\n", ppp_code(*p
), ppp_state(session
[s
].ppp
.lcp
));
1063 *p
= TerminateAck
; // send ack
1064 q
= makeppp(b
, sizeof(b
), p
, l
, s
, t
, PPPLCP
, 0, 0, 0);
1067 LOG(3, s
, t
, "LCP: send %s\n", ppp_code(*q
));
1068 if (config
->debug
> 3) dumplcp(q
, l
);
1070 tunnelsend(b
, l
+ (q
- b
), t
); // send it
1072 else if (*p
== ProtocolRej
)
1079 if (l
> 5 && !(proto
& 1))
1086 if (proto
== PPPIPV6CP
)
1088 LOG(3, s
, t
, "IPv6 rejected\n");
1089 change_state(s
, ipv6cp
, Closed
);
1093 LOG(3, s
, t
, "LCP protocol reject: 0x%04X\n", proto
);
1096 else if (*p
== EchoReq
)
1098 *p
= EchoReply
; // reply
1099 *(uint32_t *) (p
+ 4) = htonl(session
[s
].magic
); // our magic number
1100 q
= makeppp(b
, sizeof(b
), p
, l
, s
, t
, PPPLCP
, 0, 0, 0);
1103 LOG(4, s
, t
, "LCP: send %s\n", ppp_code(*q
));
1104 if (config
->debug
> 3) dumplcp(q
, l
);
1106 tunnelsend(b
, l
+ (q
- b
), t
); // send it
1108 else if (*p
== EchoReply
)
1110 // Ignore it, last_packet time is set earlier than this.
1112 else if (*p
!= CodeRej
)
1114 ppp_code_rej(s
, t
, PPPLCP
, "LCP", p
, l
, b
, sizeof(b
));
1118 int join_bundle(sessionidt s
)
1120 // Search for a bundle to join
1123 for (i
= 1; i
< MAXBUNDLE
; i
++)
1125 if (bundle
[i
].state
!= BUNDLEFREE
)
1127 if (epdiscmp(session
[s
].epdis
,bundle
[i
].epdis
) && !strcmp(session
[s
].user
, bundle
[i
].user
))
1129 sessionidt first_ses
= bundle
[i
].members
[0];
1130 if (bundle
[i
].mssf
!= session
[s
].mssf
)
1132 // uniformity of sequence number format must be insured
1133 LOG(3, s
, session
[s
].tunnel
, "MPPP: unable to bundle session %d in bundle %d cause of different mssf\n", s
, i
);
1136 session
[s
].bundle
= i
;
1137 session
[s
].ip
= session
[first_ses
].ip
;
1138 session
[s
].dns1
= session
[first_ses
].dns1
;
1139 session
[s
].dns2
= session
[first_ses
].dns2
;
1140 session
[s
].timeout
= session
[first_ses
].timeout
;
1142 if(session
[s
].epdis
.length
> 0)
1143 setepdis(&bundle
[i
].epdis
, session
[s
].epdis
);
1145 strcpy(bundle
[i
].user
, session
[s
].user
);
1146 bundle
[i
].members
[bundle
[i
].num_of_links
] = s
;
1147 bundle
[i
].num_of_links
++;
1148 LOG(3, s
, session
[s
].tunnel
, "MPPP: Bundling additional line in bundle (%d), lines:%d\n",i
,bundle
[i
].num_of_links
);
1154 // No previously created bundle was found for this session, so create a new one
1155 if (!(b
= new_bundle())) return 0;
1157 session
[s
].bundle
= b
;
1158 bundle
[b
].mrru
= session
[s
].mrru
;
1159 bundle
[b
].mssf
= session
[s
].mssf
;
1160 // FIXME !!! to enable l2tpns reading mssf frames receiver_max_seq, sender_max_seq must be introduce
1161 // 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
1164 bundle[b].max_seq = 1 << 12;
1166 bundle
[b
].max_seq
= 1 << 24;
1167 if(session
[s
].epdis
.length
> 0)
1168 setepdis(&bundle
[b
].epdis
, session
[s
].epdis
);
1170 strcpy(bundle
[b
].user
, session
[s
].user
);
1171 bundle
[b
].members
[0] = s
;
1172 bundle
[b
].timeout
= session
[s
].timeout
;
1173 LOG(3, s
, session
[s
].tunnel
, "MPPP: Created a new bundle (%d)\n", b
);
1177 static int epdiscmp(epdist ep1
, epdist ep2
)
1180 if (ep1
.length
!= ep2
.length
)
1183 if (ep1
.addr_class
!= ep2
.addr_class
)
1186 for (ad
= 0; ad
< ep1
.length
; ad
++)
1187 if (ep1
.address
[ad
] != ep2
.address
[ad
])
1193 static void setepdis(epdist
*ep1
, epdist ep2
)
1196 ep1
->length
= ep2
.length
;
1197 ep1
->addr_class
= ep2
.addr_class
;
1198 for (ad
= 0; ad
< ep2
.length
; ad
++)
1199 ep1
->address
[ad
] = ep2
.address
[ad
];
1202 static bundleidt
new_bundle()
1205 for (i
= 1; i
< MAXBUNDLE
; i
++)
1207 if (bundle
[i
].state
== BUNDLEFREE
)
1209 LOG(4, 0, 0, "MPPP: Assigning bundle ID %d\n", i
);
1210 bundle
[i
].num_of_links
= 1;
1211 bundle
[i
].last_check
= time_now
; // Initialize last_check value
1212 bundle
[i
].state
= BUNDLEOPEN
;
1213 bundle
[i
].current_ses
= -1; // This is to enforce the first session 0 to be used at first
1214 memset(&frag
[i
], 0, sizeof(fragmentationt
));
1215 if (i
> config
->cluster_highest_bundleid
)
1216 config
->cluster_highest_bundleid
= i
;
1220 LOG(0, 0, 0, "MPPP: Can't find a free bundle! There shouldn't be this many in use!\n");
1224 static void ipcp_open(sessionidt s
, tunnelidt t
)
1226 LOG(3, s
, t
, "IPCP: Opened, session is now active\n");
1228 change_state(s
, ipcp
, Opened
);
1230 if (!(session
[s
].walled_garden
|| session
[s
].flags
& SESSION_STARTED
))
1232 uint16_t r
= radiusnew(s
);
1235 radiussend(r
, RADIUSSTART
); // send radius start
1237 // don't send further Start records if IPCP is restarted
1238 session
[s
].flags
|= SESSION_STARTED
;
1239 cluster_send_session(s
);
1243 // start IPv6 if configured and still in passive state
1244 if (session
[s
].ppp
.ipv6cp
== Stopped
)
1247 change_state(s
, ipv6cp
, RequestSent
);
1251 // Process IPCP messages
1252 void processipcp(sessionidt s
, tunnelidt t
, uint8_t *p
, uint16_t l
)
1254 uint8_t b
[MAXETHER
];
1260 LOG_HEX(5, "IPCP", p
, l
);
1263 LOG(1, s
, t
, "Short IPCP %d bytes\n", l
);
1264 STAT(tunnel_rx_errors
);
1268 if ((hl
= ntohs(*(uint16_t *) (p
+ 2))) > l
)
1270 LOG(1, s
, t
, "Length mismatch IPCP %u/%u\n", hl
, l
);
1271 STAT(tunnel_rx_errors
);
1276 if (session
[s
].ppp
.phase
< Network
)
1278 LOG(2, s
, t
, "IPCP %s ignored in %s phase\n", ppp_code(*p
), ppp_phase(session
[s
].ppp
.phase
));
1282 LOG(3, s
, t
, "IPCP: recv %s\n", ppp_code(*p
));
1284 if (*p
== ConfigAck
)
1286 switch (session
[s
].ppp
.ipcp
)
1289 initialise_restart_count(s
, ipcp
);
1290 change_state(s
, ipcp
, AckReceived
);
1295 LOG(2, s
, t
, "IPCP: ConfigAck in state %s? Sending ConfigReq\n", ppp_state(session
[s
].ppp
.ipcp
));
1297 change_state(s
, ipcp
, RequestSent
);
1305 LOG(2, s
, t
, "IPCP: ignoring %s in state %s\n", ppp_code(*p
), ppp_state(session
[s
].ppp
.ipcp
));
1308 else if (*p
== ConfigReq
)
1310 uint8_t *response
= 0;
1318 if (!o
[1] || o
[1] > length
) return;
1322 case 3: // ip address
1323 gotip
++; // seen address
1324 if (o
[1] != 6) return;
1326 addr
= htonl(session
[s
].ip
);
1327 if (memcmp(o
+ 2, &addr
, (sizeof addr
)))
1330 q
= ppp_conf_nak(s
, b
, sizeof(b
), PPPIPCP
, &response
, q
, p
, o
, (uint8_t *) &addr
, sizeof(addr
));
1331 if (!q
|| (q
!= oq
&& *response
== ConfigRej
))
1333 sessionshutdown(s
, "Can't negotiate IPCP.", CDN_ADMIN_DISC
, TERM_USER_ERROR
);
1340 case 129: // primary DNS
1341 if (o
[1] != 6) return;
1343 addr
= htonl(session
[s
].dns1
);
1344 if (memcmp(o
+ 2, &addr
, (sizeof addr
)))
1346 q
= ppp_conf_nak(s
, b
, sizeof(b
), PPPIPCP
, &response
, q
, p
, o
, (uint8_t *) &addr
, sizeof(addr
));
1352 case 131: // secondary DNS
1353 if (o
[1] != 6) return;
1355 addr
= htonl(session
[s
].dns2
);
1356 if (memcmp(o
+ 2, &addr
, sizeof(addr
)))
1358 q
= ppp_conf_nak(s
, b
, sizeof(b
), PPPIPCP
, &response
, q
, p
, o
, (uint8_t *) &addr
, sizeof(addr
));
1365 LOG(2, s
, t
, " Rejecting PPP IPCP Option type %d\n", *o
);
1366 q
= ppp_conf_rej(s
, b
, sizeof(b
), PPPIPCP
, &response
, q
, p
, o
);
1376 l
= q
- response
; // IPCP packet length
1377 *((uint16_t *) (response
+ 2)) = htons(l
); // update header
1381 // Send packet back as ConfigAck
1382 response
= makeppp(b
, sizeof(b
), p
, l
, s
, t
, PPPIPCP
, 0, 0, 0);
1383 if (!response
) return;
1384 *response
= ConfigAck
;
1388 LOG(1, s
, t
, "No IP in IPCP request\n");
1389 STAT(tunnel_rx_errors
);
1393 switch (session
[s
].ppp
.ipcp
)
1396 response
= makeppp(b
, sizeof(b
), p
, 2, s
, t
, PPPIPCP
, 0, 0, 0);
1397 if (!response
) return;
1398 *response
= TerminateAck
;
1399 *((uint16_t *) (response
+ 2)) = htons(l
= 4);
1403 initialise_restart_count(s
, ipcp
);
1405 if (*response
== ConfigAck
)
1406 change_state(s
, ipcp
, AckSent
);
1408 change_state(s
, ipcp
, RequestSent
);
1413 if (*response
== ConfigAck
)
1414 change_state(s
, ipcp
, AckSent
);
1419 if (*response
== ConfigAck
)
1425 initialise_restart_count(s
, ipcp
);
1430 if (*response
== ConfigAck
)
1431 change_state(s
, ipcp
, AckSent
);
1433 change_state(s
, ipcp
, RequestSent
);
1438 LOG(2, s
, t
, "IPCP: ignoring %s in state %s\n", ppp_code(*p
), ppp_state(session
[s
].ppp
.ipcp
));
1442 LOG(3, s
, t
, "IPCP: send %s\n", ppp_code(*response
));
1443 tunnelsend(b
, l
+ (response
- b
), t
);
1445 else if (*p
== TerminateReq
)
1447 switch (session
[s
].ppp
.ipcp
)
1459 zero_restart_count(s
, ipcp
);
1460 change_state(s
, ipcp
, Closing
);
1464 LOG(2, s
, t
, "IPCP: ignoring %s in state %s\n", ppp_code(*p
), ppp_state(session
[s
].ppp
.ipcp
));
1468 *p
= TerminateAck
; // send ack
1469 q
= makeppp(b
, sizeof(b
), p
, l
, s
, t
, PPPIPCP
, 0, 0, 0);
1472 LOG(3, s
, t
, "IPCP: send %s\n", ppp_code(*q
));
1473 tunnelsend(b
, l
+ (q
- b
), t
); // send it
1475 else if (*p
!= CodeRej
)
1477 ppp_code_rej(s
, t
, PPPIPCP
, "IPCP", p
, l
, b
, sizeof(b
));
1481 static void ipv6cp_open(sessionidt s
, tunnelidt t
)
1483 LOG(3, s
, t
, "IPV6CP: Opened\n");
1485 change_state(s
, ipv6cp
, Opened
);
1486 if (session
[s
].ipv6prefixlen
)
1487 route6set(s
, session
[s
].ipv6route
, session
[s
].ipv6prefixlen
, 1);
1489 // Send an initial RA (TODO: Should we send these regularly?)
1490 send_ipv6_ra(s
, t
, NULL
);
1493 // Process IPV6CP messages
1494 void processipv6cp(sessionidt s
, tunnelidt t
, uint8_t *p
, uint16_t l
)
1496 uint8_t b
[MAXETHER
];
1500 CSTAT(processipv6cp
);
1502 LOG_HEX(5, "IPV6CP", p
, l
);
1505 LOG(1, s
, t
, "Short IPV6CP %d bytes\n", l
);
1506 STAT(tunnel_rx_errors
);
1510 if ((hl
= ntohs(*(uint16_t *) (p
+ 2))) > l
)
1512 LOG(1, s
, t
, "Length mismatch IPV6CP %u/%u\n", hl
, l
);
1513 STAT(tunnel_rx_errors
);
1518 if (session
[s
].ppp
.phase
< Network
)
1520 LOG(2, s
, t
, "IPV6CP %s ignored in %s phase\n", ppp_code(*p
), ppp_phase(session
[s
].ppp
.phase
));
1524 LOG(3, s
, t
, "IPV6CP: recv %s\n", ppp_code(*p
));
1528 LOG(3, s
, t
, "IPV6CP: no IPv4 address (IPCP in state %s)\n", ppp_state(session
[s
].ppp
.ipcp
));
1529 return; // need IPCP to complete...
1532 if (*p
== ConfigAck
)
1534 switch (session
[s
].ppp
.ipv6cp
)
1537 initialise_restart_count(s
, ipv6cp
);
1538 change_state(s
, ipv6cp
, AckReceived
);
1543 LOG(2, s
, t
, "IPV6CP: ConfigAck in state %s? Sending ConfigReq\n", ppp_state(session
[s
].ppp
.ipv6cp
));
1545 change_state(s
, ipv6cp
, RequestSent
);
1553 LOG(2, s
, t
, "IPV6CP: ignoring %s in state %s\n", ppp_code(*p
), ppp_state(session
[s
].ppp
.ipv6cp
));
1556 else if (*p
== ConfigReq
)
1558 uint8_t *response
= 0;
1566 if (!o
[1] || o
[1] > length
) return;
1570 case 1: // interface identifier
1571 gotip
++; // seen address
1572 if (o
[1] != 10) return;
1574 ident
[0] = htonl(session
[s
].ip
);
1577 if (memcmp(o
+ 2, ident
, sizeof(ident
)))
1579 q
= ppp_conf_nak(s
, b
, sizeof(b
), PPPIPV6CP
, &response
, q
, p
, o
, (uint8_t *)ident
, sizeof(ident
));
1586 LOG(2, s
, t
, " Rejecting PPP IPV6CP Option type %d\n", *o
);
1587 q
= ppp_conf_rej(s
, b
, sizeof(b
), PPPIPV6CP
, &response
, q
, p
, o
);
1597 l
= q
- response
; // IPV6CP packet length
1598 *((uint16_t *) (response
+ 2)) = htons(l
); // update header
1602 // Send packet back as ConfigAck
1603 response
= makeppp(b
, sizeof(b
), p
, l
, s
, t
, PPPIPV6CP
, 0, 0, 0);
1604 if (!response
) return;
1605 *response
= ConfigAck
;
1609 LOG(1, s
, t
, "No interface identifier in IPV6CP request\n");
1610 STAT(tunnel_rx_errors
);
1614 switch (session
[s
].ppp
.ipv6cp
)
1617 response
= makeppp(b
, sizeof(b
), p
, 2, s
, t
, PPPIPV6CP
, 0, 0, 0);
1618 if (!response
) return;
1619 *response
= TerminateAck
;
1620 *((uint16_t *) (response
+ 2)) = htons(l
= 4);
1624 initialise_restart_count(s
, ipv6cp
);
1626 if (*response
== ConfigAck
)
1627 change_state(s
, ipv6cp
, AckSent
);
1629 change_state(s
, ipv6cp
, RequestSent
);
1634 if (*response
== ConfigAck
)
1635 change_state(s
, ipv6cp
, AckSent
);
1640 if (*response
== ConfigAck
)
1646 initialise_restart_count(s
, ipv6cp
);
1651 if (*response
== ConfigAck
)
1652 change_state(s
, ipv6cp
, AckSent
);
1654 change_state(s
, ipv6cp
, RequestSent
);
1659 LOG(2, s
, t
, "IPV6CP: ignoring %s in state %s\n", ppp_code(*p
), ppp_state(session
[s
].ppp
.ipv6cp
));
1663 LOG(3, s
, t
, "IPV6CP: send %s\n", ppp_code(*response
));
1664 tunnelsend(b
, l
+ (response
- b
), t
);
1666 else if (*p
== TerminateReq
)
1668 switch (session
[s
].ppp
.ipv6cp
)
1680 zero_restart_count(s
, ipv6cp
);
1681 change_state(s
, ipv6cp
, Closing
);
1685 LOG(2, s
, t
, "IPV6CP: ignoring %s in state %s\n", ppp_code(*p
), ppp_state(session
[s
].ppp
.ipv6cp
));
1689 *p
= TerminateAck
; // send ack
1690 q
= makeppp(b
, sizeof(b
), p
, l
, s
, t
, PPPIPV6CP
, 0, 0, 0);
1693 LOG(3, s
, t
, "IPV6CP: send %s\n", ppp_code(*q
));
1694 tunnelsend(b
, l
+ (q
- b
), t
); // send it
1696 else if (*p
!= CodeRej
)
1698 ppp_code_rej(s
, t
, PPPIPV6CP
, "IPV6CP", p
, l
, b
, sizeof(b
));
1702 static void update_sessions_in_stat(sessionidt s
, uint16_t l
)
1704 bundleidt b
= session
[s
].bundle
;
1707 increment_counter(&session
[s
].cin
, &session
[s
].cin_wrap
, l
);
1708 session
[s
].cin_delta
+= l
;
1711 sess_local
[s
].cin
+= l
;
1712 sess_local
[s
].pin
++;
1716 int i
= frag
[b
].re_frame_begin_index
;
1717 int end
= frag
[b
].re_frame_end_index
;
1720 l
= frag
[b
].fragment
[i
].length
;
1721 s
= frag
[b
].fragment
[i
].sid
;
1722 increment_counter(&session
[s
].cin
, &session
[s
].cin_wrap
, l
);
1723 session
[s
].cin_delta
+= l
;
1726 sess_local
[s
].cin
+= l
;
1727 sess_local
[s
].pin
++;
1730 i
= (i
+ 1) & MAXFRAGNUM_MASK
;
1735 // process IP packet received
1737 // This MUST be called with at least 4 byte behind 'p'.
1738 // (i.e. this routine writes to p[-4]).
1739 void processipin(sessionidt s
, tunnelidt t
, uint8_t *p
, uint16_t l
)
1745 LOG_HEX(5, "IP", p
, l
);
1749 LOG(1, s
, t
, "IP packet too short %d\n", l
);
1750 STAT(tunnel_rx_errors
);
1754 ip
= ntohl(*(uint32_t *)(p
+ 12));
1758 LOG(1, s
, t
, "IP packet too long %d\n", l
);
1759 STAT(tunnel_rx_errors
);
1763 if (session
[s
].ppp
.phase
!= Network
|| session
[s
].ppp
.ipcp
!= Opened
)
1766 if (!session
[s
].bundle
|| bundle
[session
[s
].bundle
].num_of_links
< 2) // FIXME:
1768 // no spoof (do sessionbyip to handled statically routed subnets)
1769 if (!config
->disable_no_spoof
&& ip
!= session
[s
].ip
&& sessionbyip(htonl(ip
)) != s
)
1771 LOG(4, s
, t
, "Dropping packet with spoofed IP %s\n", fmtaddr(htonl(ip
), 0));
1776 // run access-list if any
1777 if (session
[s
].filter_in
&& !ip_filter(p
, l
, session
[s
].filter_in
- 1))
1780 // adjust MSS on SYN and SYN,ACK packets with options
1781 if ((ntohs(*(uint16_t *) (p
+ 6)) & 0x1fff) == 0 && p
[9] == IPPROTO_TCP
) // first tcp fragment
1783 int ihl
= (p
[0] & 0xf) * 4; // length of IP header
1784 if (l
>= ihl
+ 20 && (p
[ihl
+ 13] & TCP_FLAG_SYN
) && ((p
[ihl
+ 12] >> 4) > 5))
1785 adjust_tcp_mss(s
, t
, p
, l
, p
+ ihl
);
1788 // Add on the tun header
1790 *(uint32_t *) p
= htonl(PKTIP
);
1793 if (session
[s
].tbf_in
)
1795 // Are we throttling this session?
1796 if (config
->cluster_iam_master
)
1797 tbf_queue_packet(session
[s
].tbf_in
, p
, l
);
1799 master_throttle_packet(session
[s
].tbf_in
, p
, l
);
1804 if (tun_write(p
, l
) < 0)
1806 STAT(tun_tx_errors
);
1807 LOG(0, s
, t
, "Error writing %d bytes to TUN device: %s (tunfd=%d, p=%p)\n",
1808 l
, strerror(errno
), tunfd
, p
);
1816 if (session
[s
].snoop_ip
&& session
[s
].snoop_port
)
1818 // Snooping this session
1819 snoop_send_packet(p
, l
, session
[s
].snoop_ip
, session
[s
].snoop_port
);
1822 update_sessions_in_stat(s
, l
);
1826 STAT(tun_tx_packets
);
1827 INC_STAT(tun_tx_bytes
, l
);
1830 // process Multilink PPP packet received
1831 void processmpin(sessionidt s
, tunnelidt t
, uint8_t *p
, uint16_t l
)
1833 bundleidt b
= session
[s
].bundle
;
1834 bundlet
* this_bundle
= &bundle
[b
];
1835 uint32_t maskSeq
, max_seq
;
1837 uint16_t frag_index
, frag_index_next
, frag_index_prev
;
1838 fragmentationt
*this_fragmentation
= &frag
[b
];
1839 uint8_t begin_frame
= (*p
& MP_BEGIN
);
1840 uint8_t end_frame
= (*p
& MP_END
);
1841 uint32_t seq_num
, seq_num_next
, seq_num_prev
;
1844 uint16_t begin_index
, end_index
;
1846 // Perform length checking
1849 LOG(2, s
, t
, "MPPP: discarding fragment larger than MAXFRAGLEN\n");
1855 LOG(2, s
, t
, "MPPP: Invalid bundle id: 0\n");
1858 // FIXME !! session[s].mssf means that the receiver wants to receive frames in mssf not means the receiver will send frames in mssf
1859 /* if(session[s].mssf)
1861 // Get 12 bit for seq number
1862 seq_num = ntohs((*(uint16_t *) p) & 0xFF0F);
1865 // After this point the pointer should be advanced 2 bytes
1866 LOG(3, s, t, "MPPP: 12 bits, sequence number: %d\n",seq_num);
1870 // Get 24 bit for seq number
1871 seq_num
= ntohl((*(uint32_t *) p
) & 0xFFFFFF00);
1874 // After this point the pointer should be advanced 4 bytes
1875 LOG(4, s
, t
, "MPPP: 24 bits sequence number:%d\n",seq_num
);
1878 max_seq
= this_bundle
->max_seq
;
1879 maskSeq
= max_seq
- 1;
1882 * Expand sequence number to 32 bits, making it as close
1883 * as possible to this_fragmentation->M.
1885 seq_num
|= this_fragmentation
->M
& ~maskSeq
;
1886 if ((int)(this_fragmentation
->M
- seq_num
) > (int)(maskSeq
>> 1))
1888 seq_num
+= maskSeq
+ 1;
1890 else if ((int)(seq_num
- this_fragmentation
->M
) > (int)(maskSeq
>> 1))
1892 seq_num
-= maskSeq
+ 1; /* should never happen */
1895 // calculate this fragment's offset from the begin seq in the bundle
1896 frag_offset
= (int) (seq_num
- this_fragmentation
->start_seq
);
1898 sess_local
[s
].last_seq
= seq_num
;
1900 // calculate the jitter average
1901 uint32_t ljitter
= time_now_ms
- sess_local
[s
].prev_time
;
1904 sess_local
[s
].jitteravg
= (sess_local
[s
].jitteravg
+ ljitter
)>>1;
1905 sess_local
[s
].prev_time
= time_now_ms
;
1910 if (seq_num
< this_fragmentation
->M
)
1913 this_fragmentation
->M
= seq_num
;
1917 Mmin
= sess_local
[(this_bundle
->members
[0])].last_seq
;
1918 for (i
= 1; i
< this_bundle
->num_of_links
; i
++)
1920 uint32_t s_seq
= sess_local
[(this_bundle
->members
[i
])].last_seq
;
1924 this_fragmentation
->M
= Mmin
;
1927 // calculate M offset of the M seq in the bundle
1928 int M_offset
= (int) (Mmin
- this_fragmentation
->start_seq
);
1930 if (M_offset
>= MAXFRAGNUM
)
1932 // There have a long break of the link !!!!!!!!
1933 // M_offset is bigger that the fragmentation buffer size
1934 LOG(3, s
, t
, "MPPP: M_offset out of range, min:%d, begin_seq:%d\n", Mmin
, this_fragmentation
->start_seq
);
1936 // Calculate the new start index, the previous frag are lost
1937 begin_index
= (M_offset
+ this_fragmentation
->start_index
) & MAXFRAGNUM_MASK
;
1939 // Set new Start sequence
1940 this_fragmentation
->start_index
= begin_index
;
1941 this_fragmentation
->start_seq
= Mmin
;
1943 // recalculate the fragment offset from the new begin seq in the bundle
1944 frag_offset
= (int) (seq_num
- Mmin
);
1947 // discard this fragment if the packet comes before the start sequence
1948 if (frag_offset
< 0)
1950 // this packet comes before the next
1951 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
);
1955 // discard if frag_offset is bigger that the fragmentation buffer size
1956 if (frag_offset
>= MAXFRAGNUM
)
1958 // frag_offset is bigger that the fragmentation buffer size
1959 LOG(3, s
, t
, "MPPP: Index out of range, seq:%d, begin_seq:%d\n", seq_num
, this_fragmentation
->start_seq
);
1963 //caculate received fragment's index in the fragment array
1964 frag_index
= (frag_offset
+ this_fragmentation
->start_index
) & MAXFRAGNUM_MASK
;
1966 // insert the frame in it's place
1967 fragmentt
*this_frag
= &this_fragmentation
->fragment
[frag_index
];
1969 if (this_frag
->length
> 0)
1970 // This fragment is lost, It was around the buffer and it was never completed the packet.
1971 LOG(3, this_frag
->sid
, this_frag
->tid
, "MPPP: (INSERT) seq_num:%d frag_index:%d flags:%02X is LOST\n",
1972 this_frag
->seq
, frag_index
, this_frag
->flags
);
1974 this_frag
->length
= l
;
1977 this_frag
->flags
= flags
;
1978 this_frag
->seq
= seq_num
;
1979 this_frag
->jitteravg
= sess_local
[s
].jitteravg
;
1980 memcpy(this_frag
->data
, p
, l
);
1982 LOG(4, s
, t
, "MPPP: seq_num:%d frag_index:%d INSERTED flags: %02X\n", seq_num
, frag_index
, flags
);
1985 frag_index_next
= (frag_index
+ 1) & MAXFRAGNUM_MASK
;
1986 //previous frag index
1987 frag_index_prev
= (frag_index
- 1) & MAXFRAGNUM_MASK
;
1989 seq_num_next
= seq_num
+ 1;
1991 seq_num_prev
= seq_num
- 1;
1993 // Clean the buffer and log the lost fragments
1994 if ((frag_index_next
!= this_fragmentation
->start_index
) && this_fragmentation
->fragment
[frag_index_next
].length
)
1996 // check if the next frag is a lost fragment
1997 if (this_fragmentation
->fragment
[frag_index_next
].seq
!= seq_num_next
)
1999 // This fragment is lost, It was around the buffer and it was never completed the packet.
2000 LOG(3, this_fragmentation
->fragment
[frag_index_next
].sid
, this_fragmentation
->fragment
[frag_index_next
].tid
,
2001 "MPPP: (NEXT) seq_num:%d frag_index:%d flags:%02X is LOST\n",
2002 this_fragmentation
->fragment
[frag_index_next
].seq
, frag_index_next
,
2003 this_fragmentation
->fragment
[frag_index_next
].flags
);
2004 // this frag is lost
2005 this_fragmentation
->fragment
[frag_index_next
].length
= 0;
2006 this_fragmentation
->fragment
[frag_index_next
].flags
= 0;
2008 if (begin_frame
&& (!end_frame
)) return; // assembling frame failed
2012 // Clean the buffer and log the lost fragments
2013 if ((frag_index
!= this_fragmentation
->start_index
) && this_fragmentation
->fragment
[frag_index_prev
].length
)
2015 // check if the next frag is a lost fragment
2016 if (this_fragmentation
->fragment
[frag_index_prev
].seq
!= seq_num_prev
)
2018 // This fragment is lost, It was around the buffer and it was never completed the packet.
2019 LOG(3, this_fragmentation
->fragment
[frag_index_prev
].sid
, this_fragmentation
->fragment
[frag_index_prev
].tid
,
2020 "MPPP: (PREV) seq_num:%d frag_index:%d flags:%02X is LOST\n",
2021 this_fragmentation
->fragment
[frag_index_prev
].seq
, frag_index_prev
,
2022 this_fragmentation
->fragment
[frag_index_prev
].flags
);
2024 this_fragmentation
->fragment
[frag_index_prev
].length
= 0;
2025 this_fragmentation
->fragment
[frag_index_prev
].flags
= 0;
2027 if (end_frame
&& (!begin_frame
)) return; // assembling frame failed
2032 begin_index
= this_fragmentation
->start_index
;
2033 uint32_t b_seq
= this_fragmentation
->start_seq
;
2034 // Try to find a Begin sequence from the start_seq sequence to M sequence
2035 while (b_seq
< Mmin
)
2037 if (this_fragmentation
->fragment
[begin_index
].length
)
2039 if (b_seq
== this_fragmentation
->fragment
[begin_index
].seq
)
2041 if (this_fragmentation
->fragment
[begin_index
].flags
& MP_BEGIN
)
2044 // Adjust the new start sequence and start index
2045 this_fragmentation
->start_index
= begin_index
;
2046 this_fragmentation
->start_seq
= b_seq
;
2047 // Begin Sequence found, now try to found the End Sequence to complete the frame
2048 end_index
= begin_index
;
2049 while (b_seq
< Mmin
)
2051 if (this_fragmentation
->fragment
[end_index
].length
)
2053 if (b_seq
== this_fragmentation
->fragment
[end_index
].seq
)
2055 if (this_fragmentation
->fragment
[end_index
].flags
& MP_END
)
2057 // The End sequence was found and the frame is complete
2064 // This fragment is lost, it was never completed the packet.
2065 LOG(3, this_fragmentation
->fragment
[end_index
].sid
, this_fragmentation
->fragment
[end_index
].tid
,
2066 "MPPP: (FIND END) seq_num:%d frag_index:%d flags:%02X is LOST\n",
2067 this_fragmentation
->fragment
[end_index
].seq
, begin_index
,
2068 this_fragmentation
->fragment
[end_index
].flags
);
2069 // this frag is lost
2070 this_fragmentation
->fragment
[end_index
].length
= 0;
2071 this_fragmentation
->fragment
[end_index
].flags
= 0;
2072 // This frame is not complete find the next Begin
2078 // This frame is not complete find the next Begin if exist
2081 end_index
= (end_index
+1) & MAXFRAGNUM_MASK
;
2086 // The End sequence was found and the frame is complete
2089 // find the next Begin
2090 begin_index
= end_index
;
2095 // This fragment is lost, it was never completed the packet.
2096 LOG(3, this_fragmentation
->fragment
[begin_index
].sid
, this_fragmentation
->fragment
[begin_index
].tid
,
2097 "MPPP: (FIND BEGIN) seq_num:%d frag_index:%d flags:%02X is LOST\n",
2098 this_fragmentation
->fragment
[begin_index
].seq
, begin_index
,
2099 this_fragmentation
->fragment
[begin_index
].flags
);
2100 // this frag is lost
2101 this_fragmentation
->fragment
[begin_index
].length
= 0;
2102 this_fragmentation
->fragment
[begin_index
].flags
= 0;
2105 begin_index
= (begin_index
+1) & MAXFRAGNUM_MASK
;
2110 // try to assemble the frame that has the received fragment as a member
2111 // get the beginning of this frame
2112 begin_index
= end_index
= this_fragmentation
->start_index
;
2113 if (this_fragmentation
->fragment
[begin_index
].length
)
2115 if (!(this_fragmentation
->fragment
[begin_index
].flags
& MP_BEGIN
))
2117 LOG(3, this_fragmentation
->fragment
[begin_index
].sid
, this_fragmentation
->fragment
[begin_index
].tid
,
2118 "MPPP: (NOT BEGIN) seq_num:%d frag_index:%d flags:%02X\n",
2119 this_fragmentation
->fragment
[begin_index
].seq
, begin_index
,
2120 this_fragmentation
->fragment
[begin_index
].flags
);
2121 // should occur only after an "M_Offset out of range"
2122 // The start sequence must be a begin sequence
2123 this_fragmentation
->start_index
= (begin_index
+1) & MAXFRAGNUM_MASK
;
2124 this_fragmentation
->start_seq
++;
2125 return; // assembling frame failed
2129 return; // assembling frame failed
2131 // get the end of his frame
2132 while (this_fragmentation
->fragment
[end_index
].length
)
2134 if (this_fragmentation
->fragment
[end_index
].flags
& MP_END
)
2137 end_index
= (end_index
+1) & MAXFRAGNUM_MASK
;
2139 if (end_index
== this_fragmentation
->start_index
)
2140 return; // assembling frame failed
2143 // return if a lost fragment is found
2144 if (!(this_fragmentation
->fragment
[end_index
].length
))
2145 return; // assembling frame failed
2147 // assemble the packet
2148 //assemble frame, process it, reset fragmentation
2149 uint16_t cur_len
= 4; // This is set to 4 to leave 4 bytes for function processipin
2151 LOG(4, s
, t
, "MPPP: processing fragments from %d to %d\n", begin_index
, end_index
);
2152 // Push to the receive buffer
2154 for (i
= begin_index
;; i
= (i
+ 1) & MAXFRAGNUM_MASK
)
2156 this_frag
= &this_fragmentation
->fragment
[i
];
2157 if(cur_len
+ this_frag
->length
> MAXETHER
)
2159 LOG(2, s
, t
, "MPPP: discarding reassembled frames larger than MAXETHER\n");
2163 memcpy(this_fragmentation
->reassembled_frame
+cur_len
, this_frag
->data
, this_frag
->length
);
2164 LOG(5, s
, t
, "MPPP: processing frame at %d, with len %d\n", i
, this_frag
->length
);
2166 cur_len
+= this_frag
->length
;
2169 this_fragmentation
->re_frame_len
= cur_len
;
2170 this_fragmentation
->re_frame_begin_index
= begin_index
;
2171 this_fragmentation
->re_frame_end_index
= end_index
;
2172 // Process the resassembled frame
2173 LOG(5, s
, t
, "MPPP: Process the reassembled frame, len=%d\n",cur_len
);
2174 processmpframe(s
, t
, this_fragmentation
->reassembled_frame
, this_fragmentation
->re_frame_len
, 1);
2179 // Set reassembled frame length to zero after processing it
2180 this_fragmentation
->re_frame_len
= 0;
2181 for (i
= begin_index
;; i
= (i
+ 1) & MAXFRAGNUM_MASK
)
2183 this_fragmentation
->fragment
[i
].length
= 0; // Indicates that this fragment has been consumed
2184 this_fragmentation
->fragment
[i
].flags
= 0;
2189 // Set the new start_index and start_seq
2190 this_fragmentation
->start_index
= (end_index
+ 1) & MAXFRAGNUM_MASK
;
2191 this_fragmentation
->start_seq
= this_fragmentation
->fragment
[end_index
].seq
+ 1;
2192 LOG(4, s
, t
, "MPPP after assembling: start index is = %d, start seq=%d\n", this_fragmentation
->start_index
, this_fragmentation
->start_seq
);
2194 begin_index
= this_fragmentation
->start_index
;
2195 if ((this_fragmentation
->fragment
[begin_index
].length
) &&
2196 (this_fragmentation
->fragment
[begin_index
].seq
!= this_fragmentation
->start_seq
))
2198 LOG(3, this_fragmentation
->fragment
[begin_index
].sid
, this_fragmentation
->fragment
[begin_index
].tid
,
2199 "MPPP: (START) seq_num:%d frag_index:%d flags:%02X is LOST\n",
2200 this_fragmentation
->fragment
[begin_index
].seq
, begin_index
,
2201 this_fragmentation
->fragment
[begin_index
].flags
);
2202 this_fragmentation
->fragment
[begin_index
].length
= 0;
2203 this_fragmentation
->fragment
[begin_index
].flags
= 0;
2206 if (this_fragmentation
->start_seq
<= Mmin
)
2207 // It's possible to find other complete frame or lost frame.
2209 else if ((this_fragmentation
->fragment
[begin_index
].length
) &&
2210 (this_fragmentation
->fragment
[begin_index
].flags
& MP_BEGIN
))
2211 // may be that the next frame is completed
2212 goto assembling_frame
;
2217 // process IPv6 packet received
2219 // This MUST be called with at least 4 byte behind 'p'.
2220 // (i.e. this routine writes to p[-4]).
2221 void processipv6in(sessionidt s
, tunnelidt t
, uint8_t *p
, uint16_t l
)
2226 CSTAT(processipv6in
);
2228 LOG_HEX(5, "IPv6", p
, l
);
2230 ip
= *(struct in6_addr
*) (p
+ 8);
2231 ipv4
= ntohl(*(uint32_t *)(p
+ 16));
2235 LOG(1, s
, t
, "IP packet too long %d\n", l
);
2236 STAT(tunnel_rx_errors
);
2240 if (session
[s
].ppp
.phase
!= Network
|| session
[s
].ppp
.ipv6cp
!= Opened
)
2244 if ((ipv4
!= session
[s
].ip
|| memcmp(&config
->ipv6_prefix
, &ip
, 8)) && sessionbyipv6(ip
) != s
)
2246 char str
[INET6_ADDRSTRLEN
];
2247 LOG(5, s
, t
, "Dropping packet with spoofed IP %s\n",
2248 inet_ntop(AF_INET6
, &ip
, str
, INET6_ADDRSTRLEN
));
2252 // Check if it's a Router Solicition message.
2253 if (*(p
+ 6) == 58 && *(p
+ 7) == 255 && *(p
+ 24) == 0xFF && *(p
+ 25) == 2 &&
2254 *(uint32_t *)(p
+ 26) == 0 && *(uint32_t *)(p
+ 30) == 0 &&
2255 *(uint32_t *)(p
+ 34) == 0 &&
2256 *(p
+ 38) == 0 && *(p
+ 39) == 2 && *(p
+ 40) == 133) {
2257 LOG(3, s
, t
, "Got IPv6 RS\n");
2258 send_ipv6_ra(s
, t
, &ip
);
2262 // Add on the tun header
2264 *(uint32_t *) p
= htonl(PKTIPV6
);
2267 // Are we throttled and a slave?
2268 if (session
[s
].tbf_in
&& !config
->cluster_iam_master
) {
2269 // Pass it to the master for handling.
2270 master_throttle_packet(session
[s
].tbf_in
, p
, l
);
2274 // Are we throttled and a master??
2275 if (session
[s
].tbf_in
&& config
->cluster_iam_master
) {
2276 // Actually handle the throttled packets.
2277 tbf_queue_packet(session
[s
].tbf_in
, p
, l
);
2282 if (tun_write(p
, l
) < 0)
2284 STAT(tun_tx_errors
);
2285 LOG(0, s
, t
, "Error writing %d bytes to TUN device: %s (tunfd=%d, p=%p)\n",
2286 l
, strerror(errno
), tunfd
, p
);
2294 if (session
[s
].snoop_ip
&& session
[s
].snoop_port
)
2296 // Snooping this session
2297 snoop_send_packet(p
, l
, session
[s
].snoop_ip
, session
[s
].snoop_port
);
2300 update_sessions_in_stat(s
, l
);
2304 STAT(tun_tx_packets
);
2305 INC_STAT(tun_tx_bytes
, l
);
2309 // Helper routine for the TBF filters.
2310 // Used to send queued data in from the user.
2312 void send_ipin(sessionidt s
, uint8_t *buf
, int len
)
2314 LOG_HEX(5, "IP in throttled", buf
, len
);
2316 if (write(tunfd
, buf
, len
) < 0)
2318 STAT(tun_tx_errors
);
2319 LOG(0, 0, 0, "Error writing %d bytes to TUN device: %s (tunfd=%d, p=%p)\n",
2320 len
, strerror(errno
), tunfd
, buf
);
2328 if (session
[s
].snoop_ip
&& session
[s
].snoop_port
)
2330 // Snooping this session
2331 snoop_send_packet(buf
, len
, session
[s
].snoop_ip
, session
[s
].snoop_port
);
2334 // Increment packet counters
2335 increment_counter(&session
[s
].cin
, &session
[s
].cin_wrap
, len
);
2336 session
[s
].cin_delta
+= len
;
2339 sess_local
[s
].cin
+= len
;
2340 sess_local
[s
].pin
++;
2344 STAT(tun_tx_packets
);
2345 INC_STAT(tun_tx_bytes
, len
- 4);
2349 // Process CCP messages
2350 void processccp(sessionidt s
, tunnelidt t
, uint8_t *p
, uint16_t l
)
2352 uint8_t b
[MAXETHER
];
2357 LOG_HEX(5, "CCP", p
, l
);
2359 if (session
[s
].ppp
.phase
< Network
)
2361 LOG(2, s
, t
, "CCP %s ignored in %s phase\n", ppp_code(*p
), ppp_phase(session
[s
].ppp
.phase
));
2367 LOG(1, s
, t
, "Short CCP packet\n");
2368 STAT(tunnel_rx_errors
);
2371 LOG(4, s
, t
, "CCP: recv %s\n", ppp_code(*p
));
2372 if (*p
== ConfigAck
)
2374 switch (session
[s
].ppp
.ccp
)
2377 initialise_restart_count(s
, ccp
);
2378 change_state(s
, ccp
, AckReceived
);
2383 LOG(2, s
, t
, "CCP: ConfigAck in state %s? Sending ConfigReq\n", ppp_state(session
[s
].ppp
.ccp
));
2385 change_state(s
, ccp
, RequestSent
);
2389 LOG(3, s
, t
, "CCP: Opened\n");
2390 change_state(s
, ccp
, Opened
);
2394 LOG(2, s
, t
, "CCP: ignoring %s in state %s\n", ppp_code(*p
), ppp_state(session
[s
].ppp
.ccp
));
2397 else if (*p
== ConfigReq
)
2399 if (l
< 6) // accept no compression
2401 else // compression requested--reject
2404 q
= makeppp(b
, sizeof(b
), p
, l
, s
, t
, PPPCCP
, 0, 0, 0);
2407 switch (session
[s
].ppp
.ccp
)
2410 q
= makeppp(b
, sizeof(b
), p
, 2, s
, t
, PPPCCP
, 0, 0, 0);
2413 *((uint16_t *) (q
+ 2)) = htons(l
= 4);
2417 initialise_restart_count(s
, ccp
);
2419 if (*q
== ConfigAck
)
2420 change_state(s
, ccp
, AckSent
);
2422 change_state(s
, ccp
, RequestSent
);
2427 if (*q
== ConfigAck
)
2428 change_state(s
, ccp
, AckSent
);
2433 if (*q
== ConfigAck
)
2434 change_state(s
, ccp
, Opened
);
2439 initialise_restart_count(s
, ccp
);
2444 if (*q
== ConfigAck
)
2445 change_state(s
, ccp
, AckSent
);
2447 change_state(s
, ccp
, RequestSent
);
2452 LOG(2, s
, t
, "CCP: ignoring %s in state %s\n", ppp_code(*p
), ppp_state(session
[s
].ppp
.ccp
));
2456 LOG(4, s
, t
, "CCP: send %s\n", ppp_code(*q
));
2457 tunnelsend(b
, l
+ (q
- b
), t
);
2459 else if (*p
== TerminateReq
)
2462 q
= makeppp(b
, sizeof(b
), p
, l
, s
, t
, PPPCCP
, 0, 0, 0);
2464 LOG(3, s
, t
, "CCP: send %s\n", ppp_code(*q
));
2465 tunnelsend(b
, l
+ (q
- b
), t
);
2466 change_state(s
, ccp
, Stopped
);
2468 else if (*p
!= CodeRej
)
2470 ppp_code_rej(s
, t
, PPPCCP
, "CCP", p
, l
, b
, sizeof(b
));
2474 // send a CHAP challenge
2475 void sendchap(sessionidt s
, tunnelidt t
)
2477 uint8_t b
[MAXETHER
];
2486 LOG(1, s
, t
, "No RADIUS to send challenge\n");
2487 STAT(tunnel_tx_errors
);
2491 LOG(1, s
, t
, "Send CHAP challenge\n");
2493 radius
[r
].chap
= 1; // CHAP not PAP
2495 if (radius
[r
].state
!= RADIUSCHAP
)
2498 radius
[r
].state
= RADIUSCHAP
;
2499 radius
[r
].retry
= backoff(radius
[r
].try++);
2500 if (radius
[r
].try > 5)
2502 sessionshutdown(s
, "CHAP timeout.", CDN_ADMIN_DISC
, TERM_REAUTHENTICATION_FAILURE
);
2503 STAT(tunnel_tx_errors
);
2506 q
= makeppp(b
, sizeof(b
), 0, 0, s
, t
, PPPCHAP
, 0, 0, 0);
2509 *q
= 1; // challenge
2510 q
[1] = radius
[r
].id
; // ID
2511 q
[4] = 16; // value size (size of challenge)
2512 memcpy(q
+ 5, radius
[r
].auth
, 16); // challenge
2513 strcpy((char *) q
+ 21, config
->multi_n_hostname
[tunnel
[t
].indexudp
][0]?config
->multi_n_hostname
[tunnel
[t
].indexudp
]:hostname
); // our name
2514 *(uint16_t *) (q
+ 2) = htons(strlen(config
->multi_n_hostname
[tunnel
[t
].indexudp
][0]?config
->multi_n_hostname
[tunnel
[t
].indexudp
]:hostname
) + 21); // length
2515 tunnelsend(b
, strlen(config
->multi_n_hostname
[tunnel
[t
].indexudp
][0]?config
->multi_n_hostname
[tunnel
[t
].indexudp
]:hostname
) + 21 + (q
- b
), t
); // send it
2518 // fill in a L2TP message with a PPP frame,
2519 // returns start of PPP frame
2520 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
)
2522 uint16_t hdr
= 0x0002; // L2TP with no options
2523 uint16_t type
= mtype
;
2526 if (t
== TUNNEL_ID_PPPOE
)
2528 return pppoe_makeppp(b
, size
, p
, l
, s
, t
, mtype
, prio
, bid
, mp_bits
);
2531 if (size
< 16) // Need more space than this!!
2533 LOG(0, s
, t
, "makeppp buffer too small for L2TP header (size=%d)\n", size
);
2537 if (prio
) hdr
|= 0x0100; // set priority bit
2539 *(uint16_t *) (b
+ 0) = htons(hdr
);
2540 *(uint16_t *) (b
+ 2) = htons(tunnel
[t
].far
); // tunnel
2541 *(uint16_t *) (b
+ 4) = htons(session
[s
].far
); // session
2544 // Check whether this session is part of multilink
2547 if (bundle
[bid
].num_of_links
> 1)
2548 type
= PPPMP
; // Change PPP message type to the PPPMP
2553 if (type
== PPPLCP
|| !(session
[s
].flags
& SESSION_ACFC
))
2555 *(uint16_t *) b
= htons(0xFF03); // HDLC header
2559 if (type
< 0x100 && session
[s
].flags
& SESSION_PFC
)
2565 *(uint16_t *) b
= htons(type
);
2571 // Set the sequence number and (B)egin (E)nd flags
2572 if (session
[s
].mssf
)
2574 // Set the multilink bits
2575 uint16_t bits_send
= mp_bits
;
2576 *(uint16_t *) b
= htons((bundle
[bid
].seq_num_t
& 0x0FFF)|bits_send
);
2581 *(uint32_t *) b
= htonl(bundle
[bid
].seq_num_t
);
2582 // Set the multilink bits
2587 bundle
[bid
].seq_num_t
++;
2589 // Add the message type if this fragment has the begin bit set
2590 if (mp_bits
& MP_BEGIN
)
2592 //*b++ = mtype; // The next two lines are instead of this
2593 *(uint16_t *) b
= htons(mtype
); // Message type
2598 if ((b
- start
) + l
> size
)
2600 LOG(2, s
, t
, "makeppp would overflow buffer (size=%d, header+payload=%td)\n", size
, (b
- start
) + l
);
2611 // fill in a L2TP message with a PPP frame,
2612 // returns start of PPP frame
2613 //(note: THIS ROUTINE CAN WRITES TO p[-28]).
2614 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
)
2616 uint16_t hdr
= 0x0002; // L2TP with no options
2617 uint16_t type
= mtype
;
2620 if (t
== TUNNEL_ID_PPPOE
)
2622 return opt_pppoe_makeppp(p
, l
, s
, t
, mtype
, prio
, bid
, mp_bits
);
2625 // Check whether this session is part of multilink
2628 if (bundle
[bid
].num_of_links
> 1)
2629 type
= PPPMP
; // Change PPP message type to the PPPMP
2636 // Add the message type if this fragment has the begin bit set
2637 if (mp_bits
& MP_BEGIN
)
2640 *(uint16_t *) b
= htons(mtype
); // Message type
2643 // Set the sequence number and (B)egin (E)nd flags
2644 if (session
[s
].mssf
)
2646 // Set the multilink bits
2647 uint16_t bits_send
= mp_bits
;
2649 *(uint16_t *) b
= htons((bundle
[bid
].seq_num_t
& 0x0FFF)|bits_send
);
2654 *(uint32_t *) b
= htonl(bundle
[bid
].seq_num_t
);
2655 // Set the multilink bits
2659 bundle
[bid
].seq_num_t
++;
2662 if (type
< 0x100 && session
[s
].flags
& SESSION_PFC
)
2670 *(uint16_t *) b
= htons(type
);
2673 if (type
== PPPLCP
|| !(session
[s
].flags
& SESSION_ACFC
))
2676 *(uint16_t *) b
= htons(0xFF03); // HDLC header
2679 if (prio
) hdr
|= 0x0100; // set priority bit
2681 *(uint16_t *) b
= htons(session
[s
].far
); // session
2683 *(uint16_t *) b
= htons(tunnel
[t
].far
); // tunnel
2685 *(uint16_t *) b
= htons(hdr
);
2690 static int add_lcp_auth(uint8_t *b
, int size
, int authtype
)
2693 if ((authtype
== AUTHCHAP
&& size
< 5) || size
< 4)
2696 *b
++ = 3; // Authentication-Protocol
2697 if (authtype
== AUTHCHAP
)
2699 len
= *b
++ = 5; // length
2700 *(uint16_t *) b
= htons(PPPCHAP
); b
+= 2;
2703 else if (authtype
== AUTHPAP
)
2705 len
= *b
++ = 4; // length
2706 *(uint16_t *) b
= htons(PPPPAP
); b
+= 2;
2710 LOG(0, 0, 0, "add_lcp_auth called with unsupported auth type %d\n", authtype
);
2716 // Send LCP ConfigReq for MRU, authentication type and magic no
2717 void sendlcp(sessionidt s
, tunnelidt t
)
2719 uint8_t b
[500], *q
, *l
;
2720 int authtype
= sess_local
[s
].lcp_authtype
;
2722 if (!(q
= makeppp(b
, sizeof(b
), NULL
, 0, s
, t
, PPPLCP
, 0, 0, 0)))
2725 LOG(3, s
, t
, "LCP: send ConfigReq%s%s%s including MP options\n",
2726 authtype
? " (" : "",
2727 authtype
? (authtype
== AUTHCHAP
? "CHAP" : "PAP") : "",
2728 authtype
? ")" : "");
2732 *l
++ = ++sess_local
[s
].lcp_ident
; // ID
2734 l
+= 2; //Save space for length
2736 if (sess_local
[s
].ppp_mru
)
2738 *l
++ = 1; *l
++ = 4; // Maximum-Receive-Unit (length 4)
2739 *(uint16_t *) l
= htons(sess_local
[s
].ppp_mru
); l
+= 2;
2743 l
+= add_lcp_auth(l
, sizeof(b
) - (l
- b
), authtype
);
2745 if (session
[s
].magic
)
2747 *l
++ = 5; *l
++ = 6; // Magic-Number (length 6)
2748 *(uint32_t *) l
= htonl(session
[s
].magic
);
2752 if (sess_local
[s
].mp_mrru
)
2754 *l
++ = 17; *l
++ = 4; // Multilink Max-Receive-Reconstructed-Unit (length 4)
2755 *(uint16_t *) l
= htons(sess_local
[s
].mp_mrru
); l
+= 2;
2758 if (sess_local
[s
].mp_epdis
)
2760 *l
++ = 19; *l
++ = 7; // Multilink Endpoint Discriminator (length 7)
2761 *l
++ = IPADDR
; // Endpoint Discriminator class
2762 *(uint32_t *) l
= htonl(sess_local
[s
].mp_epdis
);
2766 *(uint16_t *)(q
+ 2) = htons(l
- q
); // Length
2768 LOG_HEX(5, "PPPLCP", q
, l
- q
);
2769 if (config
->debug
> 3) dumplcp(q
, l
- q
);
2771 tunnelsend(b
, (l
- b
), t
);
2772 restart_timer(s
, lcp
);
2775 // Send CCP request for no compression
2776 void sendccp(sessionidt s
, tunnelidt t
)
2780 if (!(q
= makeppp(b
, sizeof(b
), NULL
, 0, s
, t
, PPPCCP
, 0, 0, 0)))
2783 LOG(3, s
, t
, "CCP: send ConfigReq (no compression)\n");
2786 *(q
+ 1) = ++sess_local
[s
].lcp_ident
; // ID
2787 *(uint16_t *)(q
+ 2) = htons(4); // Length
2789 LOG_HEX(5, "PPPCCP", q
, 4);
2790 tunnelsend(b
, (q
- b
) + 4 , t
);
2791 restart_timer(s
, ccp
);
2794 // Reject unknown/unconfigured protocols
2795 void protoreject(sessionidt s
, tunnelidt t
, uint8_t *p
, uint16_t l
, uint16_t proto
)
2798 uint8_t buf
[MAXETHER
];
2800 int mru
= session
[s
].mru
;
2801 if (mru
< MINMTU
) mru
= MINMTU
;
2802 if (mru
> sizeof(buf
)) mru
= sizeof(buf
);
2805 if (l
> mru
) l
= mru
;
2807 q
= makeppp(buf
, sizeof(buf
), 0, 0, s
, t
, PPPLCP
, 0, 0, 0);
2811 *(q
+ 1) = ++sess_local
[s
].lcp_ident
;
2812 *(uint16_t *)(q
+ 2) = htons(l
);
2813 *(uint16_t *)(q
+ 4) = htons(proto
);
2814 memcpy(q
+ 6, p
, l
- 6);
2816 if (proto
== PPPIPV6CP
)
2817 LOG(3, s
, t
, "LCP: send ProtocolRej (IPV6CP: not configured)\n");
2819 LOG(2, s
, t
, "LCP: sent ProtocolRej (0x%04X: unsupported)\n", proto
);
2821 tunnelsend(buf
, l
+ (q
- buf
), t
);