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
)
1741 in_addr_t ip
, ip_dst
;
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));
1755 ip_dst
= *(uint32_t *)(p
+ 16);
1759 LOG(1, s
, t
, "IP packet too long %d\n", l
);
1760 STAT(tunnel_rx_errors
);
1764 if (session
[s
].ppp
.phase
!= Network
|| session
[s
].ppp
.ipcp
!= Opened
)
1767 if (!session
[s
].bundle
|| bundle
[session
[s
].bundle
].num_of_links
< 2) // FIXME:
1769 // no spoof (do sessionbyip to handled statically routed subnets)
1770 if (!config
->disable_no_spoof
&& ip
!= session
[s
].ip
&& sessionbyip(htonl(ip
)) != s
)
1772 LOG(4, s
, t
, "Dropping packet with spoofed IP %s\n", fmtaddr(htonl(ip
), 0));
1777 // run access-list if any
1778 if (session
[s
].filter_in
&& !ip_filter(p
, l
, session
[s
].filter_in
- 1))
1781 // adjust MSS on SYN and SYN,ACK packets with options
1782 if ((ntohs(*(uint16_t *) (p
+ 6)) & 0x1fff) == 0 && p
[9] == IPPROTO_TCP
) // first tcp fragment
1784 int ihl
= (p
[0] & 0xf) * 4; // length of IP header
1785 if (l
>= ihl
+ 20 && (p
[ihl
+ 13] & TCP_FLAG_SYN
) && ((p
[ihl
+ 12] >> 4) > 5))
1786 adjust_tcp_mss(s
, t
, p
, l
, p
+ ihl
);
1789 // Add on the tun header
1791 *(uint32_t *) p
= htonl(PKTIP
);
1794 if (session
[s
].tbf_in
)
1796 if (!config
->no_throttle_local_IP
|| !sessionbyip(ip_dst
))
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
);
1808 if (tun_write(p
, l
) < 0)
1810 STAT(tun_tx_errors
);
1811 LOG(0, s
, t
, "Error writing %d bytes to TUN device: %s (tunfd=%d, p=%p)\n",
1812 l
, strerror(errno
), tunfd
, p
);
1820 if (session
[s
].snoop_ip
&& session
[s
].snoop_port
)
1822 // Snooping this session
1823 snoop_send_packet(p
, l
, session
[s
].snoop_ip
, session
[s
].snoop_port
);
1826 update_sessions_in_stat(s
, l
);
1830 STAT(tun_tx_packets
);
1831 INC_STAT(tun_tx_bytes
, l
);
1834 // process Multilink PPP packet received
1835 void processmpin(sessionidt s
, tunnelidt t
, uint8_t *p
, uint16_t l
)
1837 bundleidt b
= session
[s
].bundle
;
1838 bundlet
* this_bundle
= &bundle
[b
];
1839 uint32_t maskSeq
, max_seq
;
1841 uint16_t frag_index
, frag_index_next
, frag_index_prev
;
1842 fragmentationt
*this_fragmentation
= &frag
[b
];
1843 uint8_t begin_frame
= (*p
& MP_BEGIN
);
1844 uint8_t end_frame
= (*p
& MP_END
);
1845 uint32_t seq_num
, seq_num_next
, seq_num_prev
;
1848 uint16_t begin_index
, end_index
;
1850 // Perform length checking
1853 LOG(2, s
, t
, "MPPP: discarding fragment larger than MAXFRAGLEN\n");
1859 LOG(2, s
, t
, "MPPP: Invalid bundle id: 0\n");
1862 // FIXME !! session[s].mssf means that the receiver wants to receive frames in mssf not means the receiver will send frames in mssf
1863 /* if(session[s].mssf)
1865 // Get 12 bit for seq number
1866 seq_num = ntohs((*(uint16_t *) p) & 0xFF0F);
1869 // After this point the pointer should be advanced 2 bytes
1870 LOG(3, s, t, "MPPP: 12 bits, sequence number: %d\n",seq_num);
1874 // Get 24 bit for seq number
1875 seq_num
= ntohl((*(uint32_t *) p
) & 0xFFFFFF00);
1878 // After this point the pointer should be advanced 4 bytes
1879 LOG(4, s
, t
, "MPPP: 24 bits sequence number:%d\n",seq_num
);
1882 max_seq
= this_bundle
->max_seq
;
1883 maskSeq
= max_seq
- 1;
1886 * Expand sequence number to 32 bits, making it as close
1887 * as possible to this_fragmentation->M.
1889 seq_num
|= this_fragmentation
->M
& ~maskSeq
;
1890 if ((int)(this_fragmentation
->M
- seq_num
) > (int)(maskSeq
>> 1))
1892 seq_num
+= maskSeq
+ 1;
1894 else if ((int)(seq_num
- this_fragmentation
->M
) > (int)(maskSeq
>> 1))
1896 seq_num
-= maskSeq
+ 1; /* should never happen */
1899 // calculate this fragment's offset from the begin seq in the bundle
1900 frag_offset
= (int) (seq_num
- this_fragmentation
->start_seq
);
1902 sess_local
[s
].last_seq
= seq_num
;
1904 // calculate the jitter average
1905 uint32_t ljitter
= time_now_ms
- sess_local
[s
].prev_time
;
1908 sess_local
[s
].jitteravg
= (sess_local
[s
].jitteravg
+ ljitter
)>>1;
1909 sess_local
[s
].prev_time
= time_now_ms
;
1914 if (seq_num
< this_fragmentation
->M
)
1917 this_fragmentation
->M
= seq_num
;
1921 Mmin
= sess_local
[(this_bundle
->members
[0])].last_seq
;
1922 for (i
= 1; i
< this_bundle
->num_of_links
; i
++)
1924 uint32_t s_seq
= sess_local
[(this_bundle
->members
[i
])].last_seq
;
1928 this_fragmentation
->M
= Mmin
;
1931 // calculate M offset of the M seq in the bundle
1932 int M_offset
= (int) (Mmin
- this_fragmentation
->start_seq
);
1934 if (M_offset
>= MAXFRAGNUM
)
1936 // There have a long break of the link !!!!!!!!
1937 // M_offset is bigger that the fragmentation buffer size
1938 LOG(3, s
, t
, "MPPP: M_offset out of range, min:%d, begin_seq:%d\n", Mmin
, this_fragmentation
->start_seq
);
1940 // Calculate the new start index, the previous frag are lost
1941 begin_index
= (M_offset
+ this_fragmentation
->start_index
) & MAXFRAGNUM_MASK
;
1943 // Set new Start sequence
1944 this_fragmentation
->start_index
= begin_index
;
1945 this_fragmentation
->start_seq
= Mmin
;
1947 // recalculate the fragment offset from the new begin seq in the bundle
1948 frag_offset
= (int) (seq_num
- Mmin
);
1951 // discard this fragment if the packet comes before the start sequence
1952 if (frag_offset
< 0)
1954 // this packet comes before the next
1955 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
);
1959 // discard if frag_offset is bigger that the fragmentation buffer size
1960 if (frag_offset
>= MAXFRAGNUM
)
1962 // frag_offset is bigger that the fragmentation buffer size
1963 LOG(3, s
, t
, "MPPP: Index out of range, seq:%d, begin_seq:%d\n", seq_num
, this_fragmentation
->start_seq
);
1967 //caculate received fragment's index in the fragment array
1968 frag_index
= (frag_offset
+ this_fragmentation
->start_index
) & MAXFRAGNUM_MASK
;
1970 // insert the frame in it's place
1971 fragmentt
*this_frag
= &this_fragmentation
->fragment
[frag_index
];
1973 if (this_frag
->length
> 0)
1974 // This fragment is lost, It was around the buffer and it was never completed the packet.
1975 LOG(3, this_frag
->sid
, this_frag
->tid
, "MPPP: (INSERT) seq_num:%d frag_index:%d flags:%02X is LOST\n",
1976 this_frag
->seq
, frag_index
, this_frag
->flags
);
1978 this_frag
->length
= l
;
1981 this_frag
->flags
= flags
;
1982 this_frag
->seq
= seq_num
;
1983 this_frag
->jitteravg
= sess_local
[s
].jitteravg
;
1984 memcpy(this_frag
->data
, p
, l
);
1986 LOG(4, s
, t
, "MPPP: seq_num:%d frag_index:%d INSERTED flags: %02X\n", seq_num
, frag_index
, flags
);
1989 frag_index_next
= (frag_index
+ 1) & MAXFRAGNUM_MASK
;
1990 //previous frag index
1991 frag_index_prev
= (frag_index
- 1) & MAXFRAGNUM_MASK
;
1993 seq_num_next
= seq_num
+ 1;
1995 seq_num_prev
= seq_num
- 1;
1997 // Clean the buffer and log the lost fragments
1998 if ((frag_index_next
!= this_fragmentation
->start_index
) && this_fragmentation
->fragment
[frag_index_next
].length
)
2000 // check if the next frag is a lost fragment
2001 if (this_fragmentation
->fragment
[frag_index_next
].seq
!= seq_num_next
)
2003 // This fragment is lost, It was around the buffer and it was never completed the packet.
2004 LOG(3, this_fragmentation
->fragment
[frag_index_next
].sid
, this_fragmentation
->fragment
[frag_index_next
].tid
,
2005 "MPPP: (NEXT) seq_num:%d frag_index:%d flags:%02X is LOST\n",
2006 this_fragmentation
->fragment
[frag_index_next
].seq
, frag_index_next
,
2007 this_fragmentation
->fragment
[frag_index_next
].flags
);
2008 // this frag is lost
2009 this_fragmentation
->fragment
[frag_index_next
].length
= 0;
2010 this_fragmentation
->fragment
[frag_index_next
].flags
= 0;
2012 if (begin_frame
&& (!end_frame
)) return; // assembling frame failed
2016 // Clean the buffer and log the lost fragments
2017 if ((frag_index
!= this_fragmentation
->start_index
) && this_fragmentation
->fragment
[frag_index_prev
].length
)
2019 // check if the next frag is a lost fragment
2020 if (this_fragmentation
->fragment
[frag_index_prev
].seq
!= seq_num_prev
)
2022 // This fragment is lost, It was around the buffer and it was never completed the packet.
2023 LOG(3, this_fragmentation
->fragment
[frag_index_prev
].sid
, this_fragmentation
->fragment
[frag_index_prev
].tid
,
2024 "MPPP: (PREV) seq_num:%d frag_index:%d flags:%02X is LOST\n",
2025 this_fragmentation
->fragment
[frag_index_prev
].seq
, frag_index_prev
,
2026 this_fragmentation
->fragment
[frag_index_prev
].flags
);
2028 this_fragmentation
->fragment
[frag_index_prev
].length
= 0;
2029 this_fragmentation
->fragment
[frag_index_prev
].flags
= 0;
2031 if (end_frame
&& (!begin_frame
)) return; // assembling frame failed
2036 begin_index
= this_fragmentation
->start_index
;
2037 uint32_t b_seq
= this_fragmentation
->start_seq
;
2038 // Try to find a Begin sequence from the start_seq sequence to M sequence
2039 while (b_seq
< Mmin
)
2041 if (this_fragmentation
->fragment
[begin_index
].length
)
2043 if (b_seq
== this_fragmentation
->fragment
[begin_index
].seq
)
2045 if (this_fragmentation
->fragment
[begin_index
].flags
& MP_BEGIN
)
2048 // Adjust the new start sequence and start index
2049 this_fragmentation
->start_index
= begin_index
;
2050 this_fragmentation
->start_seq
= b_seq
;
2051 // Begin Sequence found, now try to found the End Sequence to complete the frame
2052 end_index
= begin_index
;
2053 while (b_seq
< Mmin
)
2055 if (this_fragmentation
->fragment
[end_index
].length
)
2057 if (b_seq
== this_fragmentation
->fragment
[end_index
].seq
)
2059 if (this_fragmentation
->fragment
[end_index
].flags
& MP_END
)
2061 // The End sequence was found and the frame is complete
2068 // This fragment is lost, it was never completed the packet.
2069 LOG(3, this_fragmentation
->fragment
[end_index
].sid
, this_fragmentation
->fragment
[end_index
].tid
,
2070 "MPPP: (FIND END) seq_num:%d frag_index:%d flags:%02X is LOST\n",
2071 this_fragmentation
->fragment
[end_index
].seq
, begin_index
,
2072 this_fragmentation
->fragment
[end_index
].flags
);
2073 // this frag is lost
2074 this_fragmentation
->fragment
[end_index
].length
= 0;
2075 this_fragmentation
->fragment
[end_index
].flags
= 0;
2076 // This frame is not complete find the next Begin
2082 // This frame is not complete find the next Begin if exist
2085 end_index
= (end_index
+1) & MAXFRAGNUM_MASK
;
2090 // The End sequence was found and the frame is complete
2093 // find the next Begin
2094 begin_index
= end_index
;
2099 // This fragment is lost, it was never completed the packet.
2100 LOG(3, this_fragmentation
->fragment
[begin_index
].sid
, this_fragmentation
->fragment
[begin_index
].tid
,
2101 "MPPP: (FIND BEGIN) seq_num:%d frag_index:%d flags:%02X is LOST\n",
2102 this_fragmentation
->fragment
[begin_index
].seq
, begin_index
,
2103 this_fragmentation
->fragment
[begin_index
].flags
);
2104 // this frag is lost
2105 this_fragmentation
->fragment
[begin_index
].length
= 0;
2106 this_fragmentation
->fragment
[begin_index
].flags
= 0;
2109 begin_index
= (begin_index
+1) & MAXFRAGNUM_MASK
;
2114 // try to assemble the frame that has the received fragment as a member
2115 // get the beginning of this frame
2116 begin_index
= end_index
= this_fragmentation
->start_index
;
2117 if (this_fragmentation
->fragment
[begin_index
].length
)
2119 if (!(this_fragmentation
->fragment
[begin_index
].flags
& MP_BEGIN
))
2121 LOG(3, this_fragmentation
->fragment
[begin_index
].sid
, this_fragmentation
->fragment
[begin_index
].tid
,
2122 "MPPP: (NOT BEGIN) seq_num:%d frag_index:%d flags:%02X\n",
2123 this_fragmentation
->fragment
[begin_index
].seq
, begin_index
,
2124 this_fragmentation
->fragment
[begin_index
].flags
);
2125 // should occur only after an "M_Offset out of range"
2126 // The start sequence must be a begin sequence
2127 this_fragmentation
->start_index
= (begin_index
+1) & MAXFRAGNUM_MASK
;
2128 this_fragmentation
->start_seq
++;
2129 return; // assembling frame failed
2133 return; // assembling frame failed
2135 // get the end of his frame
2136 while (this_fragmentation
->fragment
[end_index
].length
)
2138 if (this_fragmentation
->fragment
[end_index
].flags
& MP_END
)
2141 end_index
= (end_index
+1) & MAXFRAGNUM_MASK
;
2143 if (end_index
== this_fragmentation
->start_index
)
2144 return; // assembling frame failed
2147 // return if a lost fragment is found
2148 if (!(this_fragmentation
->fragment
[end_index
].length
))
2149 return; // assembling frame failed
2151 // assemble the packet
2152 //assemble frame, process it, reset fragmentation
2153 uint16_t cur_len
= 4; // This is set to 4 to leave 4 bytes for function processipin
2155 LOG(4, s
, t
, "MPPP: processing fragments from %d to %d\n", begin_index
, end_index
);
2156 // Push to the receive buffer
2158 for (i
= begin_index
;; i
= (i
+ 1) & MAXFRAGNUM_MASK
)
2160 this_frag
= &this_fragmentation
->fragment
[i
];
2161 if(cur_len
+ this_frag
->length
> MAXETHER
)
2163 LOG(2, s
, t
, "MPPP: discarding reassembled frames larger than MAXETHER\n");
2167 memcpy(this_fragmentation
->reassembled_frame
+cur_len
, this_frag
->data
, this_frag
->length
);
2168 LOG(5, s
, t
, "MPPP: processing frame at %d, with len %d\n", i
, this_frag
->length
);
2170 cur_len
+= this_frag
->length
;
2173 this_fragmentation
->re_frame_len
= cur_len
;
2174 this_fragmentation
->re_frame_begin_index
= begin_index
;
2175 this_fragmentation
->re_frame_end_index
= end_index
;
2176 // Process the resassembled frame
2177 LOG(5, s
, t
, "MPPP: Process the reassembled frame, len=%d\n",cur_len
);
2178 processmpframe(s
, t
, this_fragmentation
->reassembled_frame
, this_fragmentation
->re_frame_len
, 1);
2183 // Set reassembled frame length to zero after processing it
2184 this_fragmentation
->re_frame_len
= 0;
2185 for (i
= begin_index
;; i
= (i
+ 1) & MAXFRAGNUM_MASK
)
2187 this_fragmentation
->fragment
[i
].length
= 0; // Indicates that this fragment has been consumed
2188 this_fragmentation
->fragment
[i
].flags
= 0;
2193 // Set the new start_index and start_seq
2194 this_fragmentation
->start_index
= (end_index
+ 1) & MAXFRAGNUM_MASK
;
2195 this_fragmentation
->start_seq
= this_fragmentation
->fragment
[end_index
].seq
+ 1;
2196 LOG(4, s
, t
, "MPPP after assembling: start index is = %d, start seq=%d\n", this_fragmentation
->start_index
, this_fragmentation
->start_seq
);
2198 begin_index
= this_fragmentation
->start_index
;
2199 if ((this_fragmentation
->fragment
[begin_index
].length
) &&
2200 (this_fragmentation
->fragment
[begin_index
].seq
!= this_fragmentation
->start_seq
))
2202 LOG(3, this_fragmentation
->fragment
[begin_index
].sid
, this_fragmentation
->fragment
[begin_index
].tid
,
2203 "MPPP: (START) seq_num:%d frag_index:%d flags:%02X is LOST\n",
2204 this_fragmentation
->fragment
[begin_index
].seq
, begin_index
,
2205 this_fragmentation
->fragment
[begin_index
].flags
);
2206 this_fragmentation
->fragment
[begin_index
].length
= 0;
2207 this_fragmentation
->fragment
[begin_index
].flags
= 0;
2210 if (this_fragmentation
->start_seq
<= Mmin
)
2211 // It's possible to find other complete frame or lost frame.
2213 else if ((this_fragmentation
->fragment
[begin_index
].length
) &&
2214 (this_fragmentation
->fragment
[begin_index
].flags
& MP_BEGIN
))
2215 // may be that the next frame is completed
2216 goto assembling_frame
;
2221 // process IPv6 packet received
2223 // This MUST be called with at least 4 byte behind 'p'.
2224 // (i.e. this routine writes to p[-4]).
2225 void processipv6in(sessionidt s
, tunnelidt t
, uint8_t *p
, uint16_t l
)
2230 CSTAT(processipv6in
);
2232 LOG_HEX(5, "IPv6", p
, l
);
2234 ip
= *(struct in6_addr
*) (p
+ 8);
2235 ipv4
= ntohl(*(uint32_t *)(p
+ 16));
2239 LOG(1, s
, t
, "IP packet too long %d\n", l
);
2240 STAT(tunnel_rx_errors
);
2244 if (session
[s
].ppp
.phase
!= Network
|| session
[s
].ppp
.ipv6cp
!= Opened
)
2248 if ((ipv4
!= session
[s
].ip
|| memcmp(&config
->ipv6_prefix
, &ip
, 8)) && sessionbyipv6(ip
) != s
)
2250 char str
[INET6_ADDRSTRLEN
];
2251 LOG(5, s
, t
, "Dropping packet with spoofed IP %s\n",
2252 inet_ntop(AF_INET6
, &ip
, str
, INET6_ADDRSTRLEN
));
2256 // Check if it's a Router Solicition message.
2257 if (*(p
+ 6) == 58 && *(p
+ 7) == 255 && *(p
+ 24) == 0xFF && *(p
+ 25) == 2 &&
2258 *(uint32_t *)(p
+ 26) == 0 && *(uint32_t *)(p
+ 30) == 0 &&
2259 *(uint32_t *)(p
+ 34) == 0 &&
2260 *(p
+ 38) == 0 && *(p
+ 39) == 2 && *(p
+ 40) == 133) {
2261 LOG(3, s
, t
, "Got IPv6 RS\n");
2262 send_ipv6_ra(s
, t
, &ip
);
2266 // Add on the tun header
2268 *(uint32_t *) p
= htonl(PKTIPV6
);
2271 // Are we throttled and a slave?
2272 if (session
[s
].tbf_in
&& !config
->cluster_iam_master
) {
2273 // Pass it to the master for handling.
2274 master_throttle_packet(session
[s
].tbf_in
, p
, l
);
2278 // Are we throttled and a master??
2279 if (session
[s
].tbf_in
&& config
->cluster_iam_master
) {
2280 // Actually handle the throttled packets.
2281 tbf_queue_packet(session
[s
].tbf_in
, p
, l
);
2286 if (tun_write(p
, l
) < 0)
2288 STAT(tun_tx_errors
);
2289 LOG(0, s
, t
, "Error writing %d bytes to TUN device: %s (tunfd=%d, p=%p)\n",
2290 l
, strerror(errno
), tunfd
, p
);
2298 if (session
[s
].snoop_ip
&& session
[s
].snoop_port
)
2300 // Snooping this session
2301 snoop_send_packet(p
, l
, session
[s
].snoop_ip
, session
[s
].snoop_port
);
2304 update_sessions_in_stat(s
, l
);
2308 STAT(tun_tx_packets
);
2309 INC_STAT(tun_tx_bytes
, l
);
2313 // Helper routine for the TBF filters.
2314 // Used to send queued data in from the user.
2316 void send_ipin(sessionidt s
, uint8_t *buf
, int len
)
2318 LOG_HEX(5, "IP in throttled", buf
, len
);
2320 if (write(tunfd
, buf
, len
) < 0)
2322 STAT(tun_tx_errors
);
2323 LOG(0, 0, 0, "Error writing %d bytes to TUN device: %s (tunfd=%d, p=%p)\n",
2324 len
, strerror(errno
), tunfd
, buf
);
2332 if (session
[s
].snoop_ip
&& session
[s
].snoop_port
)
2334 // Snooping this session
2335 snoop_send_packet(buf
, len
, session
[s
].snoop_ip
, session
[s
].snoop_port
);
2338 // Increment packet counters
2339 increment_counter(&session
[s
].cin
, &session
[s
].cin_wrap
, len
);
2340 session
[s
].cin_delta
+= len
;
2343 sess_local
[s
].cin
+= len
;
2344 sess_local
[s
].pin
++;
2348 STAT(tun_tx_packets
);
2349 INC_STAT(tun_tx_bytes
, len
- 4);
2353 // Process CCP messages
2354 void processccp(sessionidt s
, tunnelidt t
, uint8_t *p
, uint16_t l
)
2356 uint8_t b
[MAXETHER
];
2361 LOG_HEX(5, "CCP", p
, l
);
2363 if (session
[s
].ppp
.phase
< Network
)
2365 LOG(2, s
, t
, "CCP %s ignored in %s phase\n", ppp_code(*p
), ppp_phase(session
[s
].ppp
.phase
));
2371 LOG(1, s
, t
, "Short CCP packet\n");
2372 STAT(tunnel_rx_errors
);
2375 LOG(4, s
, t
, "CCP: recv %s\n", ppp_code(*p
));
2376 if (*p
== ConfigAck
)
2378 switch (session
[s
].ppp
.ccp
)
2381 initialise_restart_count(s
, ccp
);
2382 change_state(s
, ccp
, AckReceived
);
2387 LOG(2, s
, t
, "CCP: ConfigAck in state %s? Sending ConfigReq\n", ppp_state(session
[s
].ppp
.ccp
));
2389 change_state(s
, ccp
, RequestSent
);
2393 LOG(3, s
, t
, "CCP: Opened\n");
2394 change_state(s
, ccp
, Opened
);
2398 LOG(2, s
, t
, "CCP: ignoring %s in state %s\n", ppp_code(*p
), ppp_state(session
[s
].ppp
.ccp
));
2401 else if (*p
== ConfigReq
)
2403 if (l
< 6) // accept no compression
2405 else // compression requested--reject
2408 q
= makeppp(b
, sizeof(b
), p
, l
, s
, t
, PPPCCP
, 0, 0, 0);
2411 switch (session
[s
].ppp
.ccp
)
2414 q
= makeppp(b
, sizeof(b
), p
, 2, s
, t
, PPPCCP
, 0, 0, 0);
2417 *((uint16_t *) (q
+ 2)) = htons(l
= 4);
2421 initialise_restart_count(s
, ccp
);
2423 if (*q
== ConfigAck
)
2424 change_state(s
, ccp
, AckSent
);
2426 change_state(s
, ccp
, RequestSent
);
2431 if (*q
== ConfigAck
)
2432 change_state(s
, ccp
, AckSent
);
2437 if (*q
== ConfigAck
)
2438 change_state(s
, ccp
, Opened
);
2443 initialise_restart_count(s
, ccp
);
2448 if (*q
== ConfigAck
)
2449 change_state(s
, ccp
, AckSent
);
2451 change_state(s
, ccp
, RequestSent
);
2456 LOG(2, s
, t
, "CCP: ignoring %s in state %s\n", ppp_code(*p
), ppp_state(session
[s
].ppp
.ccp
));
2460 LOG(4, s
, t
, "CCP: send %s\n", ppp_code(*q
));
2461 tunnelsend(b
, l
+ (q
- b
), t
);
2463 else if (*p
== TerminateReq
)
2466 q
= makeppp(b
, sizeof(b
), p
, l
, s
, t
, PPPCCP
, 0, 0, 0);
2468 LOG(3, s
, t
, "CCP: send %s\n", ppp_code(*q
));
2469 tunnelsend(b
, l
+ (q
- b
), t
);
2470 change_state(s
, ccp
, Stopped
);
2472 else if (*p
!= CodeRej
)
2474 ppp_code_rej(s
, t
, PPPCCP
, "CCP", p
, l
, b
, sizeof(b
));
2478 // send a CHAP challenge
2479 void sendchap(sessionidt s
, tunnelidt t
)
2481 uint8_t b
[MAXETHER
];
2490 LOG(1, s
, t
, "No RADIUS to send challenge\n");
2491 STAT(tunnel_tx_errors
);
2495 LOG(1, s
, t
, "Send CHAP challenge\n");
2497 radius
[r
].chap
= 1; // CHAP not PAP
2499 if (radius
[r
].state
!= RADIUSCHAP
)
2502 radius
[r
].state
= RADIUSCHAP
;
2503 radius
[r
].retry
= backoff(radius
[r
].try++);
2504 if (radius
[r
].try > 5)
2506 sessionshutdown(s
, "CHAP timeout.", CDN_ADMIN_DISC
, TERM_REAUTHENTICATION_FAILURE
);
2507 STAT(tunnel_tx_errors
);
2510 q
= makeppp(b
, sizeof(b
), 0, 0, s
, t
, PPPCHAP
, 0, 0, 0);
2513 *q
= 1; // challenge
2514 q
[1] = radius
[r
].id
; // ID
2515 q
[4] = 16; // value size (size of challenge)
2516 memcpy(q
+ 5, radius
[r
].auth
, 16); // challenge
2517 strcpy((char *) q
+ 21, config
->multi_n_hostname
[tunnel
[t
].indexudp
][0]?config
->multi_n_hostname
[tunnel
[t
].indexudp
]:hostname
); // our name
2518 *(uint16_t *) (q
+ 2) = htons(strlen(config
->multi_n_hostname
[tunnel
[t
].indexudp
][0]?config
->multi_n_hostname
[tunnel
[t
].indexudp
]:hostname
) + 21); // length
2519 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
2522 // fill in a L2TP message with a PPP frame,
2523 // returns start of PPP frame
2524 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
)
2526 uint16_t hdr
= 0x0002; // L2TP with no options
2527 uint16_t type
= mtype
;
2530 if (t
== TUNNEL_ID_PPPOE
)
2532 return pppoe_makeppp(b
, size
, p
, l
, s
, t
, mtype
, prio
, bid
, mp_bits
);
2535 if (size
< 16) // Need more space than this!!
2537 LOG(0, s
, t
, "makeppp buffer too small for L2TP header (size=%d)\n", size
);
2541 if (prio
) hdr
|= 0x0100; // set priority bit
2543 *(uint16_t *) (b
+ 0) = htons(hdr
);
2544 *(uint16_t *) (b
+ 2) = htons(tunnel
[t
].far
); // tunnel
2545 *(uint16_t *) (b
+ 4) = htons(session
[s
].far
); // session
2548 // Check whether this session is part of multilink
2551 if (bundle
[bid
].num_of_links
> 1)
2552 type
= PPPMP
; // Change PPP message type to the PPPMP
2557 if (type
== PPPLCP
|| !(session
[s
].flags
& SESSION_ACFC
))
2559 *(uint16_t *) b
= htons(0xFF03); // HDLC header
2563 if (type
< 0x100 && session
[s
].flags
& SESSION_PFC
)
2569 *(uint16_t *) b
= htons(type
);
2575 // Set the sequence number and (B)egin (E)nd flags
2576 if (session
[s
].mssf
)
2578 // Set the multilink bits
2579 uint16_t bits_send
= mp_bits
;
2580 *(uint16_t *) b
= htons((bundle
[bid
].seq_num_t
& 0x0FFF)|bits_send
);
2585 *(uint32_t *) b
= htonl(bundle
[bid
].seq_num_t
);
2586 // Set the multilink bits
2591 bundle
[bid
].seq_num_t
++;
2593 // Add the message type if this fragment has the begin bit set
2594 if (mp_bits
& MP_BEGIN
)
2596 //*b++ = mtype; // The next two lines are instead of this
2597 *(uint16_t *) b
= htons(mtype
); // Message type
2602 if ((b
- start
) + l
> size
)
2604 LOG(2, s
, t
, "makeppp would overflow buffer (size=%d, header+payload=%td)\n", size
, (b
- start
) + l
);
2615 // fill in a L2TP message with a PPP frame,
2616 // returns start of PPP frame
2617 //(note: THIS ROUTINE CAN WRITES TO p[-28]).
2618 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
)
2620 uint16_t hdr
= 0x0002; // L2TP with no options
2621 uint16_t type
= mtype
;
2624 if (t
== TUNNEL_ID_PPPOE
)
2626 return opt_pppoe_makeppp(p
, l
, s
, t
, mtype
, prio
, bid
, mp_bits
);
2629 // Check whether this session is part of multilink
2632 if (bundle
[bid
].num_of_links
> 1)
2633 type
= PPPMP
; // Change PPP message type to the PPPMP
2640 // Add the message type if this fragment has the begin bit set
2641 if (mp_bits
& MP_BEGIN
)
2644 *(uint16_t *) b
= htons(mtype
); // Message type
2647 // Set the sequence number and (B)egin (E)nd flags
2648 if (session
[s
].mssf
)
2650 // Set the multilink bits
2651 uint16_t bits_send
= mp_bits
;
2653 *(uint16_t *) b
= htons((bundle
[bid
].seq_num_t
& 0x0FFF)|bits_send
);
2658 *(uint32_t *) b
= htonl(bundle
[bid
].seq_num_t
);
2659 // Set the multilink bits
2663 bundle
[bid
].seq_num_t
++;
2666 if (type
< 0x100 && session
[s
].flags
& SESSION_PFC
)
2674 *(uint16_t *) b
= htons(type
);
2677 if (type
== PPPLCP
|| !(session
[s
].flags
& SESSION_ACFC
))
2680 *(uint16_t *) b
= htons(0xFF03); // HDLC header
2683 if (prio
) hdr
|= 0x0100; // set priority bit
2685 *(uint16_t *) b
= htons(session
[s
].far
); // session
2687 *(uint16_t *) b
= htons(tunnel
[t
].far
); // tunnel
2689 *(uint16_t *) b
= htons(hdr
);
2694 static int add_lcp_auth(uint8_t *b
, int size
, int authtype
)
2697 if ((authtype
== AUTHCHAP
&& size
< 5) || size
< 4)
2700 *b
++ = 3; // Authentication-Protocol
2701 if (authtype
== AUTHCHAP
)
2703 len
= *b
++ = 5; // length
2704 *(uint16_t *) b
= htons(PPPCHAP
); b
+= 2;
2707 else if (authtype
== AUTHPAP
)
2709 len
= *b
++ = 4; // length
2710 *(uint16_t *) b
= htons(PPPPAP
); b
+= 2;
2714 LOG(0, 0, 0, "add_lcp_auth called with unsupported auth type %d\n", authtype
);
2720 // Send LCP ConfigReq for MRU, authentication type and magic no
2721 void sendlcp(sessionidt s
, tunnelidt t
)
2723 uint8_t b
[500], *q
, *l
;
2724 int authtype
= sess_local
[s
].lcp_authtype
;
2726 if (!(q
= makeppp(b
, sizeof(b
), NULL
, 0, s
, t
, PPPLCP
, 0, 0, 0)))
2729 LOG(3, s
, t
, "LCP: send ConfigReq%s%s%s including MP options\n",
2730 authtype
? " (" : "",
2731 authtype
? (authtype
== AUTHCHAP
? "CHAP" : "PAP") : "",
2732 authtype
? ")" : "");
2736 *l
++ = ++sess_local
[s
].lcp_ident
; // ID
2738 l
+= 2; //Save space for length
2740 if (sess_local
[s
].ppp_mru
)
2742 *l
++ = 1; *l
++ = 4; // Maximum-Receive-Unit (length 4)
2743 *(uint16_t *) l
= htons(sess_local
[s
].ppp_mru
); l
+= 2;
2747 l
+= add_lcp_auth(l
, sizeof(b
) - (l
- b
), authtype
);
2749 if (session
[s
].magic
)
2751 *l
++ = 5; *l
++ = 6; // Magic-Number (length 6)
2752 *(uint32_t *) l
= htonl(session
[s
].magic
);
2756 if (sess_local
[s
].mp_mrru
)
2758 *l
++ = 17; *l
++ = 4; // Multilink Max-Receive-Reconstructed-Unit (length 4)
2759 *(uint16_t *) l
= htons(sess_local
[s
].mp_mrru
); l
+= 2;
2762 if (sess_local
[s
].mp_epdis
)
2764 *l
++ = 19; *l
++ = 7; // Multilink Endpoint Discriminator (length 7)
2765 *l
++ = IPADDR
; // Endpoint Discriminator class
2766 *(uint32_t *) l
= htonl(sess_local
[s
].mp_epdis
);
2770 *(uint16_t *)(q
+ 2) = htons(l
- q
); // Length
2772 LOG_HEX(5, "PPPLCP", q
, l
- q
);
2773 if (config
->debug
> 3) dumplcp(q
, l
- q
);
2775 tunnelsend(b
, (l
- b
), t
);
2776 restart_timer(s
, lcp
);
2779 // Send CCP request for no compression
2780 void sendccp(sessionidt s
, tunnelidt t
)
2784 if (!(q
= makeppp(b
, sizeof(b
), NULL
, 0, s
, t
, PPPCCP
, 0, 0, 0)))
2787 LOG(3, s
, t
, "CCP: send ConfigReq (no compression)\n");
2790 *(q
+ 1) = ++sess_local
[s
].lcp_ident
; // ID
2791 *(uint16_t *)(q
+ 2) = htons(4); // Length
2793 LOG_HEX(5, "PPPCCP", q
, 4);
2794 tunnelsend(b
, (q
- b
) + 4 , t
);
2795 restart_timer(s
, ccp
);
2798 // Reject unknown/unconfigured protocols
2799 void protoreject(sessionidt s
, tunnelidt t
, uint8_t *p
, uint16_t l
, uint16_t proto
)
2802 uint8_t buf
[MAXETHER
];
2804 int mru
= session
[s
].mru
;
2805 if (mru
< MINMTU
) mru
= MINMTU
;
2806 if (mru
> sizeof(buf
)) mru
= sizeof(buf
);
2809 if (l
> mru
) l
= mru
;
2811 q
= makeppp(buf
, sizeof(buf
), 0, 0, s
, t
, PPPLCP
, 0, 0, 0);
2815 *(q
+ 1) = ++sess_local
[s
].lcp_ident
;
2816 *(uint16_t *)(q
+ 2) = htons(l
);
2817 *(uint16_t *)(q
+ 4) = htons(proto
);
2818 memcpy(q
+ 6, p
, l
- 6);
2820 if (proto
== PPPIPV6CP
)
2821 LOG(3, s
, t
, "LCP: send ProtocolRej (IPV6CP: not configured)\n");
2823 LOG(2, s
, t
, "LCP: sent ProtocolRej (0x%04X: unsupported)\n", proto
);
2825 tunnelsend(buf
, l
+ (q
- buf
), t
);