8 #include <sys/socket.h>
9 #include <linux/rtnetlink.h>
12 #include "constants.h"
23 extern tunnelt
*tunnel
;
24 extern bundlet
*bundle
;
25 extern fragmentationt
*frag
;
26 extern sessiont
*session
;
27 extern radiust
*radius
;
29 extern char hostname
[];
30 extern uint32_t eth_tx
;
31 extern time_t time_now
;
32 extern uint64_t time_now_ms
;
33 extern configt
*config
;
35 static int add_lcp_auth(uint8_t *b
, int size
, int authtype
);
36 static bundleidt
new_bundle(void);
37 static int epdiscmp(epdist
, epdist
);
38 static void setepdis(epdist
*, epdist
);
39 static void ipcp_open(sessionidt s
, tunnelidt t
);
41 static int first_session_in_bundle(sessionidt s
)
44 for (i
= 1; i
< MAXBUNDLE
; i
++)
45 if (bundle
[i
].state
!= BUNDLEFREE
)
46 if (epdiscmp(session
[s
].epdis
,bundle
[i
].epdis
) && !strcmp(session
[s
].user
, bundle
[i
].user
))
51 // Process PAP messages
52 void processpap(sessionidt s
, tunnelidt t
, uint8_t *p
, uint16_t l
)
61 LOG_HEX(5, "PAP", p
, l
);
64 LOG(1, s
, t
, "Short PAP %u bytes\n", l
);
65 STAT(tunnel_rx_errors
);
66 sessionshutdown(s
, "Short PAP packet.", CDN_ADMIN_DISC
, TERM_USER_ERROR
);
70 if ((hl
= ntohs(*(uint16_t *) (p
+ 2))) > l
)
72 LOG(1, s
, t
, "Length mismatch PAP %u/%u\n", hl
, l
);
73 STAT(tunnel_rx_errors
);
74 sessionshutdown(s
, "PAP length mismatch.", CDN_ADMIN_DISC
, TERM_USER_ERROR
);
81 LOG(1, s
, t
, "Unexpected PAP code %d\n", *p
);
82 STAT(tunnel_rx_errors
);
83 sessionshutdown(s
, "Unexpected PAP code.", CDN_ADMIN_DISC
, TERM_USER_ERROR
);
87 if (session
[s
].ppp
.phase
!= Authenticate
)
89 LOG(2, s
, t
, "PAP ignored in %s phase\n", ppp_phase(session
[s
].ppp
.phase
));
96 user
[0] = pass
[0] = 0;
97 if (*b
&& *b
< sizeof(user
))
99 memcpy(user
, b
+ 1, *b
);
102 if (*b
&& *b
< sizeof(pass
))
104 memcpy(pass
, b
+ 1, *b
);
108 LOG(3, s
, t
, "PAP login %s/%s\n", user
, pass
);
112 if ((!config
->disable_lac_func
) && lac_conf_forwardtoremotelns(s
, user
))
114 // Creating a tunnel/session has been started
119 if (session
[s
].ip
|| !(r
= radiusnew(s
)))
121 // respond now, either no RADIUS available or already authenticated
124 uint8_t *p
= makeppp(b
, sizeof(b
), 0, 0, s
, t
, PPPPAP
, 0, 0, 0);
130 *p
= 3; // cant authorise
132 *(uint16_t *) (p
+ 2) = htons(5); // length
133 p
[4] = 0; // no message
134 tunnelsend(b
, 5 + (p
- b
), t
); // send it
138 LOG(3, s
, t
, "Already an IP allocated: %s (%d)\n",
139 fmtaddr(htonl(session
[s
].ip
), 0), session
[s
].ip_pool_index
);
143 LOG(1, s
, t
, "No RADIUS session available to authenticate session...\n");
144 sessionshutdown(s
, "No free RADIUS sessions.", CDN_UNAVAILABLE
, TERM_SERVICE_UNAVAILABLE
);
149 // Run PRE_AUTH plugins
150 struct param_pre_auth packet
= { &tunnel
[t
], &session
[s
], strdup(user
), strdup(pass
), PPPPAP
, 1 };
151 run_plugins(PLUGIN_PRE_AUTH
, &packet
);
152 if (!packet
.continue_auth
)
154 LOG(3, s
, t
, "A plugin rejected PRE_AUTH\n");
155 if (packet
.username
) free(packet
.username
);
156 if (packet
.password
) free(packet
.password
);
160 strncpy(session
[s
].user
, packet
.username
, sizeof(session
[s
].user
) - 1);
161 strncpy(radius
[r
].pass
, packet
.password
, sizeof(radius
[r
].pass
) - 1);
163 free(packet
.username
);
164 free(packet
.password
);
167 LOG(3, s
, t
, "Sending login for %s/%s to RADIUS\n", user
, pass
);
168 if ((session
[s
].mrru
) && (!first_session_in_bundle(s
)))
169 radiussend(r
, RADIUSJUSTAUTH
);
171 radiussend(r
, RADIUSAUTH
);
175 // Process CHAP messages
176 void processchap(sessionidt s
, tunnelidt t
, uint8_t *p
, uint16_t l
)
183 LOG_HEX(5, "CHAP", p
, l
);
187 LOG(1, s
, t
, "Short CHAP %u bytes\n", l
);
188 STAT(tunnel_rx_errors
);
189 sessionshutdown(s
, "Short CHAP packet.", CDN_ADMIN_DISC
, TERM_USER_ERROR
);
193 if ((hl
= ntohs(*(uint16_t *) (p
+ 2))) > l
)
195 LOG(1, s
, t
, "Length mismatch CHAP %u/%u\n", hl
, l
);
196 STAT(tunnel_rx_errors
);
197 sessionshutdown(s
, "CHAP length mismatch.", CDN_ADMIN_DISC
, TERM_USER_ERROR
);
204 LOG(1, s
, t
, "Unexpected CHAP response code %d\n", *p
);
205 STAT(tunnel_rx_errors
);
206 sessionshutdown(s
, "CHAP length mismatch.", CDN_ADMIN_DISC
, TERM_USER_ERROR
);
210 if (session
[s
].ppp
.phase
!= Authenticate
)
212 LOG(2, s
, t
, "CHAP ignored in %s phase\n", ppp_phase(session
[s
].ppp
.phase
));
216 r
= sess_local
[s
].radius
;
219 LOG(3, s
, t
, "Unexpected CHAP message\n");
221 // Some modems (Netgear DM602, possibly others) persist in using CHAP even
222 // after ACKing our ConfigReq for PAP.
223 if (sess_local
[s
].lcp_authtype
== AUTHPAP
&& config
->radius_authtypes
& AUTHCHAP
)
225 sess_local
[s
].lcp_authtype
= AUTHCHAP
;
231 if (p
[1] != radius
[r
].id
)
233 LOG(1, s
, t
, "Wrong CHAP response ID %d (should be %d) (%d)\n", p
[1], radius
[r
].id
, r
);
234 STAT(tunnel_rx_errors
);
235 sessionshutdown(s
, "Unexpected CHAP response ID.", CDN_ADMIN_DISC
, TERM_USER_ERROR
);
239 if (l
< 5 || p
[4] != 16)
241 LOG(1, s
, t
, "Bad CHAP response length %d\n", l
< 5 ? -1 : p
[4]);
242 STAT(tunnel_rx_errors
);
243 sessionshutdown(s
, "Bad CHAP response length.", CDN_ADMIN_DISC
, TERM_USER_ERROR
);
249 if (l
< 16 || l
- 16 >= sizeof(session
[s
].user
))
251 LOG(1, s
, t
, "CHAP user too long %d\n", l
- 16);
252 STAT(tunnel_rx_errors
);
253 sessionshutdown(s
, "CHAP username too long.", CDN_ADMIN_DISC
, TERM_USER_ERROR
);
257 // Run PRE_AUTH plugins
259 struct param_pre_auth packet
= { &tunnel
[t
], &session
[s
], NULL
, NULL
, PPPCHAP
, 1 };
261 packet
.password
= calloc(17, 1);
262 memcpy(packet
.password
, p
, 16);
267 packet
.username
= calloc(l
+ 1, 1);
268 memcpy(packet
.username
, p
, l
);
271 if ((!config
->disable_lac_func
) && lac_conf_forwardtoremotelns(s
, packet
.username
))
273 free(packet
.username
);
274 free(packet
.password
);
275 // Creating a tunnel/session has been started
280 run_plugins(PLUGIN_PRE_AUTH
, &packet
);
281 if (!packet
.continue_auth
)
283 LOG(3, s
, t
, "A plugin rejected PRE_AUTH\n");
284 if (packet
.username
) free(packet
.username
);
285 if (packet
.password
) free(packet
.password
);
289 strncpy(session
[s
].user
, packet
.username
, sizeof(session
[s
].user
) - 1);
290 memcpy(radius
[r
].pass
, packet
.password
, 16);
292 free(packet
.username
);
293 free(packet
.password
);
297 LOG(3, s
, t
, "CHAP login %s\n", session
[s
].user
);
298 if ((session
[s
].mrru
) && (!first_session_in_bundle(s
)))
299 radiussend(r
, RADIUSJUSTAUTH
);
301 radiussend(r
, RADIUSAUTH
);
304 static void dumplcp(uint8_t *p
, int l
)
307 uint8_t *o
= (p
+ 4);
309 LOG_HEX(5, "PPP LCP Packet", p
, l
);
310 LOG(4, 0, 0, "PPP LCP Packet type %d (%s len %d)\n", *p
, ppp_code((int)*p
), ntohs( ((uint16_t *) p
)[1]) );
311 LOG(4, 0, 0, "Length: %d\n", l
);
312 if (*p
!= ConfigReq
&& *p
!= ConfigRej
&& *p
!= ConfigAck
)
321 LOG(4, 0, 0, " Option length is %d...\n", length
);
326 LOG(4, 0, 0, " Option type is 0...\n");
333 case 1: // Maximum-Receive-Unit
335 LOG(4, 0, 0, " %s %d\n", ppp_lcp_option(type
), ntohs(*(uint16_t *)(o
+ 2)));
337 LOG(4, 0, 0, " %s odd length %d\n", ppp_lcp_option(type
), length
);
339 case 2: // Async-Control-Character-Map
342 uint32_t asyncmap
= ntohl(*(uint32_t *)(o
+ 2));
343 LOG(4, 0, 0, " %s %x\n", ppp_lcp_option(type
), asyncmap
);
346 LOG(4, 0, 0, " %s odd length %d\n", ppp_lcp_option(type
), length
);
348 case 3: // Authentication-Protocol
351 int proto
= ntohs(*(uint16_t *)(o
+ 2));
352 LOG(4, 0, 0, " %s 0x%x (%s)\n", ppp_lcp_option(type
), proto
,
353 proto
== PPPPAP
? "PAP" : "UNSUPPORTED");
355 else if (length
== 5)
357 int proto
= ntohs(*(uint16_t *)(o
+ 2));
359 LOG(4, 0, 0, " %s 0x%x 0x%x (%s)\n", ppp_lcp_option(type
), proto
, algo
,
360 (proto
== PPPCHAP
&& algo
== 5) ? "CHAP MD5" : "UNSUPPORTED");
363 LOG(4, 0, 0, " %s odd length %d\n", ppp_lcp_option(type
), length
);
365 case 4: // Quality-Protocol
367 uint32_t qp
= ntohl(*(uint32_t *)(o
+ 2));
368 LOG(4, 0, 0, " %s %x\n", ppp_lcp_option(type
), qp
);
371 case 5: // Magic-Number
374 uint32_t magicno
= ntohl(*(uint32_t *)(o
+ 2));
375 LOG(4, 0, 0, " %s %x\n", ppp_lcp_option(type
), magicno
);
378 LOG(4, 0, 0, " %s odd length %d\n", ppp_lcp_option(type
), length
);
380 case 7: // Protocol-Field-Compression
381 case 8: // Address-And-Control-Field-Compression
382 LOG(4, 0, 0, " %s\n", ppp_lcp_option(type
));
385 LOG(2, 0, 0, " Unknown PPP LCP Option type %d\n", type
);
393 void lcp_open(sessionidt s
, tunnelidt t
)
395 // transition to Authentication or Network phase:
396 session
[s
].ppp
.phase
= sess_local
[s
].lcp_authtype
? Authenticate
: Network
;
398 LOG(3, s
, t
, "LCP: Opened, phase %s\n", ppp_phase(session
[s
].ppp
.phase
));
401 change_state(s
, lcp
, Opened
);
403 if (session
[s
].ppp
.phase
== Authenticate
)
405 if (sess_local
[s
].lcp_authtype
== AUTHCHAP
)
410 if(session
[s
].bundle
== 0 || bundle
[session
[s
].bundle
].num_of_links
== 1)
414 change_state(s
, ipcp
, RequestSent
);
415 // move to passive state for IPv6 (if configured), CCP
416 if (config
->ipv6_prefix
.s6_addr
[0])
417 change_state(s
, ipv6cp
, Stopped
);
419 change_state(s
, ipv6cp
, Closed
);
421 change_state(s
, ccp
, Stopped
);
425 sessionidt first_ses
= bundle
[session
[s
].bundle
].members
[0];
426 LOG(3, s
, t
, "MPPP: Skipping IPCP negotiation for session:%d, first session of bundle is:%d\n",s
,first_ses
);
432 void lcp_restart(sessionidt s
)
434 session
[s
].ppp
.phase
= Establish
;
436 change_state(s
, ipcp
, Dead
);
437 change_state(s
, ipv6cp
, Dead
);
438 change_state(s
, ccp
, Dead
);
441 static uint8_t *ppp_conf_rej(sessionidt s
, uint8_t *buf
, size_t blen
, uint16_t mtype
,
442 uint8_t **response
, uint8_t *queued
, uint8_t *packet
, uint8_t *option
)
444 if (!*response
|| **response
!= ConfigRej
)
446 queued
= *response
= makeppp(buf
, blen
, packet
, 2, s
, session
[s
].tunnel
, mtype
, 0, 0, 0);
454 if ((queued
- buf
+ option
[1]) > blen
)
456 LOG(2, s
, session
[s
].tunnel
, "PPP overflow for ConfigRej (proto %u, option %u).\n", mtype
, *option
);
460 memcpy(queued
, option
, option
[1]);
461 return queued
+ option
[1];
464 static uint8_t *ppp_conf_nak(sessionidt s
, uint8_t *buf
, size_t blen
, uint16_t mtype
,
465 uint8_t **response
, uint8_t *queued
, uint8_t *packet
, uint8_t *option
,
466 uint8_t *value
, size_t vlen
)
471 case PPPLCP
: nak_sent
= &sess_local
[s
].lcp
.nak_sent
; break;
472 case PPPIPCP
: nak_sent
= &sess_local
[s
].ipcp
.nak_sent
; break;
473 case PPPIPV6CP
: nak_sent
= &sess_local
[s
].ipv6cp
.nak_sent
; break;
474 default: return 0; // ?
477 if (*response
&& **response
!= ConfigNak
)
479 if (*nak_sent
< config
->ppp_max_failure
) // reject queued
482 return ppp_conf_rej(s
, buf
, blen
, mtype
, response
, 0, packet
, option
);
487 if (*nak_sent
>= config
->ppp_max_failure
)
488 return ppp_conf_rej(s
, buf
, blen
, mtype
, response
, 0, packet
, option
);
490 queued
= *response
= makeppp(buf
, blen
, packet
, 2, s
, session
[s
].tunnel
, mtype
, 0, 0, 0);
499 if ((queued
- buf
+ vlen
+ 2) > blen
)
501 LOG(2, s
, session
[s
].tunnel
, "PPP overflow for ConfigNak (proto %u, option %u).\n", mtype
, *option
);
506 *queued
++ = vlen
+ 2;
507 memcpy(queued
, value
, vlen
);
508 return queued
+ vlen
;
511 static void ppp_code_rej(sessionidt s
, tunnelidt t
, uint16_t proto
,
512 char *pname
, uint8_t *p
, uint16_t l
, uint8_t *buf
, size_t size
)
515 int mru
= session
[s
].mru
;
516 if (mru
< MINMTU
) mru
= MINMTU
;
517 if (mru
> size
) mru
= size
;
520 if (l
> mru
) l
= mru
;
522 q
= makeppp(buf
, size
, 0, 0, s
, t
, proto
, 0, 0, 0);
526 *(q
+ 1) = ++sess_local
[s
].lcp_ident
;
527 *(uint16_t *)(q
+ 2) = htons(l
);
528 memcpy(q
+ 4, p
, l
- 4);
530 LOG(2, s
, t
, "Unexpected %s code %s\n", pname
, ppp_code(*p
));
531 LOG(3, s
, t
, "%s: send %s\n", pname
, ppp_code(*q
));
532 if (config
->debug
> 3) dumplcp(q
, l
);
534 tunnelsend(buf
, l
+ (q
- buf
), t
);
537 // Process LCP messages
538 void processlcp(sessionidt s
, tunnelidt t
, uint8_t *p
, uint16_t l
)
546 LOG_HEX(5, "LCP", p
, l
);
549 LOG(1, s
, t
, "Short LCP %d bytes\n", l
);
550 STAT(tunnel_rx_errors
);
554 if ((hl
= ntohs(*(uint16_t *) (p
+ 2))) > l
)
556 LOG(1, s
, t
, "Length mismatch LCP %u/%u\n", hl
, l
);
557 STAT(tunnel_rx_errors
);
562 if (session
[s
].die
) // going down...
565 LOG(((*p
== EchoReq
|| *p
== EchoReply
) ? 4 : 3), s
, t
,
566 "LCP: recv %s\n", ppp_code(*p
));
568 if (config
->debug
> 3) dumplcp(p
, l
);
573 uint8_t *o
= (p
+ 4);
581 if (length
== 0 || type
== 0 || x
< length
) break;
584 case 3: // Authentication-Protocol
586 int proto
= ntohs(*(uint16_t *)(o
+ 2));
589 else if (proto
== PPPCHAP
&& *(o
+ 4) == 5)
599 if (!session
[s
].ip
&& authtype
)
600 sess_local
[s
].lcp_authtype
= authtype
;
602 switch (session
[s
].ppp
.lcp
)
605 initialise_restart_count(s
, lcp
);
606 change_state(s
, lcp
, AckReceived
);
611 LOG(2, s
, t
, "LCP: ConfigAck in state %s? Sending ConfigReq\n", ppp_state(session
[s
].ppp
.lcp
));
612 if (session
[s
].ppp
.lcp
== Opened
)
616 change_state(s
, lcp
, RequestSent
);
624 LOG(2, s
, t
, "LCP: ignoring %s in state %s\n", ppp_code(*p
), ppp_state(session
[s
].ppp
.lcp
));
627 else if (*p
== ConfigReq
)
630 uint8_t *o
= (p
+ 4);
631 uint8_t *response
= 0;
632 static uint8_t asyncmap
[4] = { 0, 0, 0, 0 }; // all zero
633 static uint8_t authproto
[5];
641 if (length
== 0 || type
== 0 || x
< length
) break;
644 case 1: // Maximum-Receive-Unit
646 uint16_t mru
= ntohs(*(uint16_t *)(o
+ 2));
649 session
[s
].mru
= mru
;
654 LOG(3, s
, t
, " Remote requesting MRU of %u. Rejecting.\n", mru
);
656 q
= ppp_conf_nak(s
, b
, sizeof(b
), PPPLCP
, &response
, q
, p
, o
, (uint8_t *) &mru
, sizeof(mru
));
660 case 2: // Async-Control-Character-Map
661 if (!ntohl(*(uint32_t *)(o
+ 2))) // all bits zero is OK
664 LOG(3, s
, t
, " Remote requesting asyncmap. Rejecting.\n");
665 q
= ppp_conf_nak(s
, b
, sizeof(b
), PPPLCP
, &response
, q
, p
, o
, asyncmap
, sizeof(asyncmap
));
668 case 3: // Authentication-Protocol
670 int proto
= ntohs(*(uint16_t *)(o
+ 2));
671 char proto_name
[] = "0x0000";
676 if (config
->radius_authtypes
& AUTHPAP
)
678 sess_local
[s
].lcp_authtype
= AUTHPAP
;
682 strcpy(proto_name
, "PAP");
684 else if (proto
== PPPCHAP
)
686 if (config
->radius_authtypes
& AUTHCHAP
687 && *(o
+ 4) == 5) // MD5
689 sess_local
[s
].lcp_authtype
= AUTHCHAP
;
693 strcpy(proto_name
, "CHAP");
696 sprintf(proto_name
, "%#4.4x", proto
);
698 LOG(3, s
, t
, " Remote requesting %s authentication. Rejecting.\n", proto_name
);
700 alen
= add_lcp_auth(authproto
, sizeof(authproto
), config
->radius_authprefer
);
701 if (alen
< 2) break; // paranoia
703 q
= ppp_conf_nak(s
, b
, sizeof(b
), PPPLCP
, &response
, q
, p
, o
, authproto
+ 2, alen
- 2);
704 if (q
&& *response
== ConfigNak
&&
705 config
->radius_authtypes
!= config
->radius_authprefer
)
708 alen
= add_lcp_auth(authproto
, sizeof(authproto
), config
->radius_authtypes
& ~config
->radius_authprefer
);
710 q
= ppp_conf_nak(s
, b
, sizeof(b
), PPPLCP
, &response
, q
, p
, o
, authproto
+ 2, alen
- 2);
717 case 4: // Quality-Protocol
718 case 5: // Magic-Number
719 case 7: // Protocol-Field-Compression
720 case 8: // Address-And-Control-Field-Compression
723 case 17: // Multilink Max-Receive-Reconstructed-Unit
725 uint16_t mrru
= ntohs(*(uint16_t *)(o
+ 2));
726 session
[s
].mrru
= mrru
;
728 LOG(3, s
, t
, " Received PPP LCP option MRRU: %d\n",mrru
);
732 case 18: // Multilink Short Sequence Number Header Format
736 LOG(3, s
, t
, " Received PPP LCP option MSSN format\n");
740 case 19: // Multilink Endpoint Discriminator
742 uint8_t epdis_class
= o
[2];
745 session
[s
].epdis
.addr_class
= epdis_class
;
746 session
[s
].epdis
.length
= length
- 3;
747 if (session
[s
].epdis
.length
> 20)
749 LOG(1, s
, t
, "Error: received EndDis Address Length more than 20: %d\n", session
[s
].epdis
.length
);
750 session
[s
].epdis
.length
= 20;
753 for (addr
= 0; addr
< session
[s
].epdis
.length
; addr
++)
754 session
[s
].epdis
.address
[addr
] = o
[3+addr
];
761 LOG(3, s
, t
, " Received PPP LCP option Multilink EndDis Local Address Class: %d\n",epdis_class
);
764 LOG(3, s
, t
, " Received PPP LCP option Multilink EndDis IP Address Class: %d\n",epdis_class
);
767 LOG(3, s
, t
, " Received PPP LCP option Multilink EndDis IEEE MAC Address Class: %d\n",epdis_class
);
770 LOG(3, s
, t
, " Received PPP LCP option Multilink EndDis PPP Magic No Class: %d\n",epdis_class
);
773 LOG(3, s
, t
, " Received PPP LCP option Multilink EndDis PSND No Class: %d\n",epdis_class
);
776 LOG(3, s
, t
, " Received PPP LCP option Multilink EndDis NULL Class %d\n",epdis_class
);
781 default: // Reject any unknown options
782 LOG(3, s
, t
, " Rejecting unknown PPP LCP option %d\n", type
);
783 q
= ppp_conf_rej(s
, b
, sizeof(b
), PPPLCP
, &response
, q
, p
, o
);
790 cluster_send_session(s
);
794 l
= q
- response
; // LCP packet length
795 *((uint16_t *) (response
+ 2)) = htons(l
); // update header
799 // Send packet back as ConfigAck
800 response
= makeppp(b
, sizeof(b
), p
, l
, s
, t
, PPPLCP
, 0, 0, 0);
801 if (!response
) return;
802 *response
= ConfigAck
;
805 switch (session
[s
].ppp
.lcp
)
808 response
= makeppp(b
, sizeof(b
), p
, 2, s
, t
, PPPLCP
, 0, 0, 0);
809 if (!response
) return;
810 *response
= TerminateAck
;
811 *((uint16_t *) (response
+ 2)) = htons(l
= 4);
815 initialise_restart_count(s
, lcp
);
817 if (*response
== ConfigAck
)
818 change_state(s
, lcp
, AckSent
);
820 change_state(s
, lcp
, RequestSent
);
825 if (*response
== ConfigAck
)
826 change_state(s
, lcp
, AckSent
);
831 if (*response
== ConfigAck
)
842 if (*response
== ConfigAck
)
843 change_state(s
, lcp
, AckSent
);
845 change_state(s
, lcp
, RequestSent
);
850 sessionshutdown(s
, "LCP: ConfigReq in state Closing. This should not happen. Killing session.", CDN_ADMIN_DISC
, TERM_LOST_SERVICE
);
854 LOG(2, s
, t
, "LCP: ignoring %s in state %s\n", ppp_code(*p
), ppp_state(session
[s
].ppp
.lcp
));
858 LOG(3, s
, t
, "LCP: send %s\n", ppp_code(*response
));
859 if (config
->debug
> 3) dumplcp(response
, l
);
861 tunnelsend(b
, l
+ (response
- b
), t
);
863 else if (*p
== ConfigNak
|| *p
== ConfigRej
)
866 uint8_t *o
= (p
+ 4);
874 if (length
== 0 || type
== 0 || x
< length
) break;
877 case 1: // Maximum-Receive-Unit
880 if (length
< 4) break;
881 sess_local
[s
].ppp_mru
= ntohs(*(uint16_t *)(o
+ 2));
882 LOG(3, s
, t
, " Remote requested MRU of %u\n", sess_local
[s
].ppp_mru
);
886 sess_local
[s
].ppp_mru
= 0;
887 LOG(3, s
, t
, " Remote rejected MRU negotiation\n");
892 case 3: // Authentication-Protocol
900 if (length
< 4) break;
901 proto
= ntohs(*(uint16_t *)(o
+ 2));
905 authtype
= config
->radius_authtypes
& AUTHPAP
;
906 LOG(3, s
, t
, " Remote requested PAP authentication...%sing\n",
907 authtype
? "accept" : "reject");
909 else if (proto
== PPPCHAP
&& length
> 4 && *(o
+ 4) == 5)
911 authtype
= config
->radius_authtypes
& AUTHCHAP
;
912 LOG(3, s
, t
, " Remote requested CHAP authentication...%sing\n",
913 authtype
? "accept" : "reject");
917 LOG(3, s
, t
, " Rejecting unsupported authentication %#4x\n",
923 LOG(2, s
, t
, "LCP: remote rejected auth negotiation\n");
924 authtype
= 0; // shutdown
929 case 5: // Magic-Number
930 session
[s
].magic
= 0;
933 if (length
< 6) break;
934 session
[s
].magic
= ntohl(*(uint32_t *)(o
+ 2));
937 if (session
[s
].magic
)
938 LOG(3, s
, t
, " Remote requested magic-no %x\n", session
[s
].magic
);
940 LOG(3, s
, t
, " Remote rejected magic-no\n");
942 cluster_send_session(s
);
945 case 17: // Multilink Max-Receive-Reconstructed-Unit
949 sess_local
[s
].mp_mrru
= ntohs(*(uint16_t *)(o
+ 2));
950 LOG(3, s
, t
, " Remote requested MRRU of %u\n", sess_local
[s
].mp_mrru
);
954 sess_local
[s
].mp_mrru
= 0;
955 LOG(3, s
, t
, " Remote rejected MRRU negotiation\n");
960 case 18: // Multilink Short Sequence Number Header Format
964 sess_local
[s
].mp_mssf
= 0;
965 LOG(3, s
, t
, " Remote requested Naked mssf\n");
969 sess_local
[s
].mp_mssf
= 0;
970 LOG(3, s
, t
, " Remote rejected mssf\n");
975 case 19: // Multilink Endpoint Discriminator
979 LOG(2, s
, t
, " Remote should not configNak Endpoint Dis!\n");
983 sess_local
[s
].mp_epdis
= 0;
984 LOG(3, s
, t
, " Remote rejected Endpoint Discriminator\n");
990 LOG(2, s
, t
, "LCP: remote sent %s for type %u?\n", ppp_code(*p
), type
);
991 sessionshutdown(s
, "Unable to negotiate LCP.", CDN_ADMIN_DISC
, TERM_USER_ERROR
);
1000 sessionshutdown(s
, "Unsupported authentication.", CDN_ADMIN_DISC
, TERM_USER_ERROR
);
1005 sess_local
[s
].lcp_authtype
= authtype
;
1007 switch (session
[s
].ppp
.lcp
)
1012 uint8_t *response
= makeppp(b
, sizeof(b
), p
, 2, s
, t
, PPPLCP
, 0, 0, 0);
1013 if (!response
) return;
1014 *response
= TerminateAck
;
1015 *((uint16_t *) (response
+ 2)) = htons(l
= 4);
1017 LOG(3, s
, t
, "LCP: send %s\n", ppp_code(*response
));
1018 if (config
->debug
> 3) dumplcp(response
, l
);
1020 tunnelsend(b
, l
+ (response
- b
), t
);
1026 initialise_restart_count(s
, lcp
);
1031 LOG(2, s
, t
, "LCP: ConfigNak in state %s? Sending ConfigReq\n", ppp_state(session
[s
].ppp
.lcp
));
1041 LOG(2, s
, t
, "LCP: ignoring %s in state %s\n", ppp_code(*p
), ppp_state(session
[s
].ppp
.lcp
));
1045 else if (*p
== TerminateReq
)
1047 switch (session
[s
].ppp
.lcp
)
1060 zero_restart_count(s
, lcp
);
1061 change_state(s
, lcp
, Closing
);
1065 LOG(2, s
, t
, "LCP: ignoring %s in state %s\n", ppp_code(*p
), ppp_state(session
[s
].ppp
.lcp
));
1069 *p
= TerminateAck
; // send ack
1070 q
= makeppp(b
, sizeof(b
), p
, l
, s
, t
, PPPLCP
, 0, 0, 0);
1073 LOG(3, s
, t
, "LCP: send %s\n", ppp_code(*q
));
1074 if (config
->debug
> 3) dumplcp(q
, l
);
1076 tunnelsend(b
, l
+ (q
- b
), t
); // send it
1078 else if (*p
== ProtocolRej
)
1085 if (l
> 5 && !(proto
& 1))
1092 if (proto
== PPPIPV6CP
)
1094 LOG(3, s
, t
, "IPv6 rejected\n");
1095 change_state(s
, ipv6cp
, Closed
);
1099 LOG(3, s
, t
, "LCP protocol reject: 0x%04X\n", proto
);
1102 else if (*p
== EchoReq
)
1104 *p
= EchoReply
; // reply
1105 *(uint32_t *) (p
+ 4) = htonl(session
[s
].magic
); // our magic number
1106 q
= makeppp(b
, sizeof(b
), p
, l
, s
, t
, PPPLCP
, 0, 0, 0);
1109 LOG(4, s
, t
, "LCP: send %s\n", ppp_code(*q
));
1110 if (config
->debug
> 3) dumplcp(q
, l
);
1112 tunnelsend(b
, l
+ (q
- b
), t
); // send it
1114 else if (*p
== EchoReply
)
1116 // Ignore it, last_packet time is set earlier than this.
1118 else if (*p
!= CodeRej
)
1120 ppp_code_rej(s
, t
, PPPLCP
, "LCP", p
, l
, b
, sizeof(b
));
1124 int join_bundle(sessionidt s
)
1126 // Search for a bundle to join
1129 for (i
= 1; i
< MAXBUNDLE
; i
++)
1131 if (bundle
[i
].state
!= BUNDLEFREE
)
1133 if (epdiscmp(session
[s
].epdis
,bundle
[i
].epdis
) && !strcmp(session
[s
].user
, bundle
[i
].user
))
1135 sessionidt first_ses
= bundle
[i
].members
[0];
1136 if (bundle
[i
].mssf
!= session
[s
].mssf
)
1138 // uniformity of sequence number format must be insured
1139 LOG(3, s
, session
[s
].tunnel
, "MPPP: unable to bundle session %d in bundle %d cause of different mssf\n", s
, i
);
1142 session
[s
].bundle
= i
;
1143 session
[s
].ip
= session
[first_ses
].ip
;
1144 session
[s
].dns1
= session
[first_ses
].dns1
;
1145 session
[s
].dns2
= session
[first_ses
].dns2
;
1146 session
[s
].timeout
= session
[first_ses
].timeout
;
1148 if(session
[s
].epdis
.length
> 0)
1149 setepdis(&bundle
[i
].epdis
, session
[s
].epdis
);
1151 strcpy(bundle
[i
].user
, session
[s
].user
);
1152 bundle
[i
].members
[bundle
[i
].num_of_links
] = s
;
1153 bundle
[i
].num_of_links
++;
1154 LOG(3, s
, session
[s
].tunnel
, "MPPP: Bundling additional line in bundle (%d), lines:%d\n",i
,bundle
[i
].num_of_links
);
1160 // No previously created bundle was found for this session, so create a new one
1161 if (!(b
= new_bundle())) return 0;
1163 session
[s
].bundle
= b
;
1164 bundle
[b
].mrru
= session
[s
].mrru
;
1165 bundle
[b
].mssf
= session
[s
].mssf
;
1166 // FIXME !!! to enable l2tpns reading mssf frames receiver_max_seq, sender_max_seq must be introduce
1167 // 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
1170 bundle[b].max_seq = 1 << 12;
1172 bundle
[b
].max_seq
= 1 << 24;
1173 if(session
[s
].epdis
.length
> 0)
1174 setepdis(&bundle
[b
].epdis
, session
[s
].epdis
);
1176 strcpy(bundle
[b
].user
, session
[s
].user
);
1177 bundle
[b
].members
[0] = s
;
1178 bundle
[b
].timeout
= session
[s
].timeout
;
1179 LOG(3, s
, session
[s
].tunnel
, "MPPP: Created a new bundle (%d)\n", b
);
1183 static int epdiscmp(epdist ep1
, epdist ep2
)
1186 if (ep1
.length
!= ep2
.length
)
1189 if (ep1
.addr_class
!= ep2
.addr_class
)
1192 for (ad
= 0; ad
< ep1
.length
; ad
++)
1193 if (ep1
.address
[ad
] != ep2
.address
[ad
])
1199 static void setepdis(epdist
*ep1
, epdist ep2
)
1202 ep1
->length
= ep2
.length
;
1203 ep1
->addr_class
= ep2
.addr_class
;
1204 for (ad
= 0; ad
< ep2
.length
; ad
++)
1205 ep1
->address
[ad
] = ep2
.address
[ad
];
1208 static bundleidt
new_bundle()
1211 for (i
= 1; i
< MAXBUNDLE
; i
++)
1213 if (bundle
[i
].state
== BUNDLEFREE
)
1215 LOG(4, 0, 0, "MPPP: Assigning bundle ID %d\n", i
);
1216 bundle
[i
].num_of_links
= 1;
1217 bundle
[i
].last_check
= time_now
; // Initialize last_check value
1218 bundle
[i
].state
= BUNDLEOPEN
;
1219 bundle
[i
].current_ses
= -1; // This is to enforce the first session 0 to be used at first
1220 memset(&frag
[i
], 0, sizeof(fragmentationt
));
1221 if (i
> config
->cluster_highest_bundleid
)
1222 config
->cluster_highest_bundleid
= i
;
1226 LOG(0, 0, 0, "MPPP: Can't find a free bundle! There shouldn't be this many in use!\n");
1230 static void ipcp_open(sessionidt s
, tunnelidt t
)
1232 LOG(3, s
, t
, "IPCP: Opened, session is now active\n");
1234 change_state(s
, ipcp
, Opened
);
1236 if (!(session
[s
].walled_garden
|| session
[s
].flags
& SESSION_STARTED
))
1238 uint16_t r
= radiusnew(s
);
1241 radiussend(r
, RADIUSSTART
); // send radius start
1243 // don't send further Start records if IPCP is restarted
1244 session
[s
].flags
|= SESSION_STARTED
;
1245 cluster_send_session(s
);
1249 // start IPv6 if configured and still in passive state
1250 if (session
[s
].ppp
.ipv6cp
== Stopped
)
1253 change_state(s
, ipv6cp
, RequestSent
);
1257 // Process IPCP messages
1258 void processipcp(sessionidt s
, tunnelidt t
, uint8_t *p
, uint16_t l
)
1260 uint8_t b
[MAXETHER
];
1266 LOG_HEX(5, "IPCP", p
, l
);
1269 LOG(1, s
, t
, "Short IPCP %d bytes\n", l
);
1270 STAT(tunnel_rx_errors
);
1274 if ((hl
= ntohs(*(uint16_t *) (p
+ 2))) > l
)
1276 LOG(1, s
, t
, "Length mismatch IPCP %u/%u\n", hl
, l
);
1277 STAT(tunnel_rx_errors
);
1282 if (session
[s
].ppp
.phase
< Network
)
1284 LOG(2, s
, t
, "IPCP %s ignored in %s phase\n", ppp_code(*p
), ppp_phase(session
[s
].ppp
.phase
));
1288 LOG(3, s
, t
, "IPCP: recv %s\n", ppp_code(*p
));
1290 if (*p
== ConfigAck
)
1292 switch (session
[s
].ppp
.ipcp
)
1295 initialise_restart_count(s
, ipcp
);
1296 change_state(s
, ipcp
, AckReceived
);
1301 LOG(2, s
, t
, "IPCP: ConfigAck in state %s? Sending ConfigReq\n", ppp_state(session
[s
].ppp
.ipcp
));
1303 change_state(s
, ipcp
, RequestSent
);
1311 LOG(2, s
, t
, "IPCP: ignoring %s in state %s\n", ppp_code(*p
), ppp_state(session
[s
].ppp
.ipcp
));
1314 else if (*p
== ConfigReq
)
1316 uint8_t *response
= 0;
1324 if (!o
[1] || o
[1] > length
) return;
1328 case 3: // ip address
1329 gotip
++; // seen address
1330 if (o
[1] != 6) return;
1332 addr
= htonl(session
[s
].ip
);
1333 if (memcmp(o
+ 2, &addr
, (sizeof addr
)))
1336 q
= ppp_conf_nak(s
, b
, sizeof(b
), PPPIPCP
, &response
, q
, p
, o
, (uint8_t *) &addr
, sizeof(addr
));
1337 if (!q
|| (q
!= oq
&& *response
== ConfigRej
))
1339 sessionshutdown(s
, "Can't negotiate IPCP.", CDN_ADMIN_DISC
, TERM_USER_ERROR
);
1346 case 129: // primary DNS
1347 if (o
[1] != 6) return;
1349 addr
= htonl(session
[s
].dns1
);
1350 if (memcmp(o
+ 2, &addr
, (sizeof addr
)))
1352 q
= ppp_conf_nak(s
, b
, sizeof(b
), PPPIPCP
, &response
, q
, p
, o
, (uint8_t *) &addr
, sizeof(addr
));
1358 case 131: // secondary DNS
1359 if (o
[1] != 6) return;
1361 addr
= htonl(session
[s
].dns2
);
1362 if (memcmp(o
+ 2, &addr
, sizeof(addr
)))
1364 q
= ppp_conf_nak(s
, b
, sizeof(b
), PPPIPCP
, &response
, q
, p
, o
, (uint8_t *) &addr
, sizeof(addr
));
1371 LOG(2, s
, t
, " Rejecting PPP IPCP Option type %d\n", *o
);
1372 q
= ppp_conf_rej(s
, b
, sizeof(b
), PPPIPCP
, &response
, q
, p
, o
);
1382 l
= q
- response
; // IPCP packet length
1383 *((uint16_t *) (response
+ 2)) = htons(l
); // update header
1387 // Send packet back as ConfigAck
1388 response
= makeppp(b
, sizeof(b
), p
, l
, s
, t
, PPPIPCP
, 0, 0, 0);
1389 if (!response
) return;
1390 *response
= ConfigAck
;
1394 LOG(1, s
, t
, "No IP in IPCP request\n");
1395 STAT(tunnel_rx_errors
);
1399 switch (session
[s
].ppp
.ipcp
)
1402 response
= makeppp(b
, sizeof(b
), p
, 2, s
, t
, PPPIPCP
, 0, 0, 0);
1403 if (!response
) return;
1404 *response
= TerminateAck
;
1405 *((uint16_t *) (response
+ 2)) = htons(l
= 4);
1409 initialise_restart_count(s
, ipcp
);
1411 if (*response
== ConfigAck
)
1412 change_state(s
, ipcp
, AckSent
);
1414 change_state(s
, ipcp
, RequestSent
);
1419 if (*response
== ConfigAck
)
1420 change_state(s
, ipcp
, AckSent
);
1425 if (*response
== ConfigAck
)
1431 initialise_restart_count(s
, ipcp
);
1436 if (*response
== ConfigAck
)
1437 change_state(s
, ipcp
, AckSent
);
1439 change_state(s
, ipcp
, RequestSent
);
1444 LOG(2, s
, t
, "IPCP: ignoring %s in state %s\n", ppp_code(*p
), ppp_state(session
[s
].ppp
.ipcp
));
1448 LOG(3, s
, t
, "IPCP: send %s\n", ppp_code(*response
));
1449 tunnelsend(b
, l
+ (response
- b
), t
);
1451 else if (*p
== TerminateReq
)
1453 switch (session
[s
].ppp
.ipcp
)
1465 zero_restart_count(s
, ipcp
);
1466 change_state(s
, ipcp
, Closing
);
1470 LOG(2, s
, t
, "IPCP: ignoring %s in state %s\n", ppp_code(*p
), ppp_state(session
[s
].ppp
.ipcp
));
1474 *p
= TerminateAck
; // send ack
1475 q
= makeppp(b
, sizeof(b
), p
, l
, s
, t
, PPPIPCP
, 0, 0, 0);
1478 LOG(3, s
, t
, "IPCP: send %s\n", ppp_code(*q
));
1479 tunnelsend(b
, l
+ (q
- b
), t
); // send it
1481 else if (*p
!= CodeRej
)
1483 ppp_code_rej(s
, t
, PPPIPCP
, "IPCP", p
, l
, b
, sizeof(b
));
1487 static void ipv6cp_open(sessionidt s
, tunnelidt t
)
1489 LOG(3, s
, t
, "IPV6CP: Opened\n");
1491 change_state(s
, ipv6cp
, Opened
);
1492 if (session
[s
].ipv6prefixlen
)
1493 route6set(s
, session
[s
].ipv6route
, session
[s
].ipv6prefixlen
, 1);
1495 // Send an initial RA (TODO: Should we send these regularly?)
1496 send_ipv6_ra(s
, t
, NULL
);
1499 // Process IPV6CP messages
1500 void processipv6cp(sessionidt s
, tunnelidt t
, uint8_t *p
, uint16_t l
)
1502 uint8_t b
[MAXETHER
];
1506 CSTAT(processipv6cp
);
1508 LOG_HEX(5, "IPV6CP", p
, l
);
1511 LOG(1, s
, t
, "Short IPV6CP %d bytes\n", l
);
1512 STAT(tunnel_rx_errors
);
1516 if ((hl
= ntohs(*(uint16_t *) (p
+ 2))) > l
)
1518 LOG(1, s
, t
, "Length mismatch IPV6CP %u/%u\n", hl
, l
);
1519 STAT(tunnel_rx_errors
);
1524 if (session
[s
].ppp
.phase
< Network
)
1526 LOG(2, s
, t
, "IPV6CP %s ignored in %s phase\n", ppp_code(*p
), ppp_phase(session
[s
].ppp
.phase
));
1530 LOG(3, s
, t
, "IPV6CP: recv %s\n", ppp_code(*p
));
1534 LOG(3, s
, t
, "IPV6CP: no IPv4 address (IPCP in state %s)\n", ppp_state(session
[s
].ppp
.ipcp
));
1535 return; // need IPCP to complete...
1538 if (*p
== ConfigAck
)
1540 switch (session
[s
].ppp
.ipv6cp
)
1543 initialise_restart_count(s
, ipv6cp
);
1544 change_state(s
, ipv6cp
, AckReceived
);
1549 LOG(2, s
, t
, "IPV6CP: ConfigAck in state %s? Sending ConfigReq\n", ppp_state(session
[s
].ppp
.ipv6cp
));
1551 change_state(s
, ipv6cp
, RequestSent
);
1559 LOG(2, s
, t
, "IPV6CP: ignoring %s in state %s\n", ppp_code(*p
), ppp_state(session
[s
].ppp
.ipv6cp
));
1562 else if (*p
== ConfigReq
)
1564 uint8_t *response
= 0;
1572 if (!o
[1] || o
[1] > length
) return;
1576 case 1: // interface identifier
1577 gotip
++; // seen address
1578 if (o
[1] != 10) return;
1580 ident
[0] = htonl(session
[s
].ip
);
1583 if (memcmp(o
+ 2, ident
, sizeof(ident
)))
1585 q
= ppp_conf_nak(s
, b
, sizeof(b
), PPPIPV6CP
, &response
, q
, p
, o
, (uint8_t *)ident
, sizeof(ident
));
1592 LOG(2, s
, t
, " Rejecting PPP IPV6CP Option type %d\n", *o
);
1593 q
= ppp_conf_rej(s
, b
, sizeof(b
), PPPIPV6CP
, &response
, q
, p
, o
);
1603 l
= q
- response
; // IPV6CP packet length
1604 *((uint16_t *) (response
+ 2)) = htons(l
); // update header
1608 // Send packet back as ConfigAck
1609 response
= makeppp(b
, sizeof(b
), p
, l
, s
, t
, PPPIPV6CP
, 0, 0, 0);
1610 if (!response
) return;
1611 *response
= ConfigAck
;
1615 LOG(1, s
, t
, "No interface identifier in IPV6CP request\n");
1616 STAT(tunnel_rx_errors
);
1620 switch (session
[s
].ppp
.ipv6cp
)
1623 response
= makeppp(b
, sizeof(b
), p
, 2, s
, t
, PPPIPV6CP
, 0, 0, 0);
1624 if (!response
) return;
1625 *response
= TerminateAck
;
1626 *((uint16_t *) (response
+ 2)) = htons(l
= 4);
1630 initialise_restart_count(s
, ipv6cp
);
1632 if (*response
== ConfigAck
)
1633 change_state(s
, ipv6cp
, AckSent
);
1635 change_state(s
, ipv6cp
, RequestSent
);
1640 if (*response
== ConfigAck
)
1641 change_state(s
, ipv6cp
, AckSent
);
1646 if (*response
== ConfigAck
)
1652 initialise_restart_count(s
, ipv6cp
);
1657 if (*response
== ConfigAck
)
1658 change_state(s
, ipv6cp
, AckSent
);
1660 change_state(s
, ipv6cp
, RequestSent
);
1665 LOG(2, s
, t
, "IPV6CP: ignoring %s in state %s\n", ppp_code(*p
), ppp_state(session
[s
].ppp
.ipv6cp
));
1669 LOG(3, s
, t
, "IPV6CP: send %s\n", ppp_code(*response
));
1670 tunnelsend(b
, l
+ (response
- b
), t
);
1672 else if (*p
== TerminateReq
)
1674 switch (session
[s
].ppp
.ipv6cp
)
1686 zero_restart_count(s
, ipv6cp
);
1687 change_state(s
, ipv6cp
, Closing
);
1691 LOG(2, s
, t
, "IPV6CP: ignoring %s in state %s\n", ppp_code(*p
), ppp_state(session
[s
].ppp
.ipv6cp
));
1695 *p
= TerminateAck
; // send ack
1696 q
= makeppp(b
, sizeof(b
), p
, l
, s
, t
, PPPIPV6CP
, 0, 0, 0);
1699 LOG(3, s
, t
, "IPV6CP: send %s\n", ppp_code(*q
));
1700 tunnelsend(b
, l
+ (q
- b
), t
); // send it
1702 else if (*p
!= CodeRej
)
1704 ppp_code_rej(s
, t
, PPPIPV6CP
, "IPV6CP", p
, l
, b
, sizeof(b
));
1708 static void update_sessions_in_stat(sessionidt s
, uint16_t l
)
1710 bundleidt b
= session
[s
].bundle
;
1713 increment_counter(&session
[s
].cin
, &session
[s
].cin_wrap
, l
);
1714 session
[s
].cin_delta
+= l
;
1717 sess_local
[s
].cin
+= l
;
1718 sess_local
[s
].pin
++;
1722 int i
= frag
[b
].re_frame_begin_index
;
1723 int end
= frag
[b
].re_frame_end_index
;
1726 l
= frag
[b
].fragment
[i
].length
;
1727 s
= frag
[b
].fragment
[i
].sid
;
1728 increment_counter(&session
[s
].cin
, &session
[s
].cin_wrap
, l
);
1729 session
[s
].cin_delta
+= l
;
1732 sess_local
[s
].cin
+= l
;
1733 sess_local
[s
].pin
++;
1736 i
= (i
+ 1) & MAXFRAGNUM_MASK
;
1741 // process IP packet received
1743 // This MUST be called with at least 4 byte behind 'p'.
1744 // (i.e. this routine writes to p[-4]).
1745 void processipin(sessionidt s
, tunnelidt t
, uint8_t *p
, uint16_t l
)
1751 LOG_HEX(5, "IP", p
, l
);
1755 LOG(1, s
, t
, "IP packet too short %d\n", l
);
1756 STAT(tunnel_rx_errors
);
1760 ip
= ntohl(*(uint32_t *)(p
+ 12));
1764 LOG(1, s
, t
, "IP packet too long %d\n", l
);
1765 STAT(tunnel_rx_errors
);
1769 if (session
[s
].ppp
.phase
!= Network
|| session
[s
].ppp
.ipcp
!= Opened
)
1772 if (!session
[s
].bundle
|| bundle
[session
[s
].bundle
].num_of_links
< 2) // FIXME:
1774 // no spoof (do sessionbyip to handled statically routed subnets)
1775 if (!config
->disable_no_spoof
&& ip
!= session
[s
].ip
&& sessionbyip(htonl(ip
)) != s
)
1777 LOG(4, s
, t
, "Dropping packet with spoofed IP %s\n", fmtaddr(htonl(ip
), 0));
1782 // run access-list if any
1783 if (session
[s
].filter_in
&& !ip_filter(p
, l
, session
[s
].filter_in
- 1))
1786 // adjust MSS on SYN and SYN,ACK packets with options
1787 if ((ntohs(*(uint16_t *) (p
+ 6)) & 0x1fff) == 0 && p
[9] == IPPROTO_TCP
) // first tcp fragment
1789 int ihl
= (p
[0] & 0xf) * 4; // length of IP header
1790 if (l
>= ihl
+ 20 && (p
[ihl
+ 13] & TCP_FLAG_SYN
) && ((p
[ihl
+ 12] >> 4) > 5))
1791 adjust_tcp_mss(s
, t
, p
, l
, p
+ ihl
);
1794 // Add on the tun header
1796 *(uint32_t *) p
= htonl(PKTIP
);
1799 if (session
[s
].tbf_in
)
1801 // Are we throttling this session?
1802 if (config
->cluster_iam_master
)
1803 tbf_queue_packet(session
[s
].tbf_in
, p
, l
);
1805 master_throttle_packet(session
[s
].tbf_in
, p
, l
);
1810 if (tun_write(p
, l
) < 0)
1812 STAT(tun_tx_errors
);
1813 LOG(0, s
, t
, "Error writing %d bytes to TUN device: %s (tunfd=%d, p=%p)\n",
1814 l
, strerror(errno
), tunfd
, p
);
1822 if (session
[s
].snoop_ip
&& session
[s
].snoop_port
)
1824 // Snooping this session
1825 snoop_send_packet(p
, l
, session
[s
].snoop_ip
, session
[s
].snoop_port
);
1828 update_sessions_in_stat(s
, l
);
1832 STAT(tun_tx_packets
);
1833 INC_STAT(tun_tx_bytes
, l
);
1836 // process Multilink PPP packet received
1837 void processmpin(sessionidt s
, tunnelidt t
, uint8_t *p
, uint16_t l
)
1839 bundleidt b
= session
[s
].bundle
;
1840 bundlet
* this_bundle
= &bundle
[b
];
1841 uint32_t maskSeq
, max_seq
;
1843 uint16_t frag_index
, frag_index_next
, frag_index_prev
;
1844 fragmentationt
*this_fragmentation
= &frag
[b
];
1845 uint8_t begin_frame
= (*p
& MP_BEGIN
);
1846 uint8_t end_frame
= (*p
& MP_END
);
1847 uint32_t seq_num
, seq_num_next
, seq_num_prev
;
1850 uint16_t begin_index
, end_index
;
1852 // Perform length checking
1855 LOG(2, s
, t
, "MPPP: discarding fragment larger than MAXFRAGLEN\n");
1861 LOG(2, s
, t
, "MPPP: Invalid bundle id: 0\n");
1864 // FIXME !! session[s].mssf means that the receiver wants to receive frames in mssf not means the receiver will send frames in mssf
1865 /* if(session[s].mssf)
1867 // Get 12 bit for seq number
1868 seq_num = ntohs((*(uint16_t *) p) & 0xFF0F);
1871 // After this point the pointer should be advanced 2 bytes
1872 LOG(3, s, t, "MPPP: 12 bits, sequence number: %d\n",seq_num);
1876 // Get 24 bit for seq number
1877 seq_num
= ntohl((*(uint32_t *) p
) & 0xFFFFFF00);
1880 // After this point the pointer should be advanced 4 bytes
1881 LOG(4, s
, t
, "MPPP: 24 bits sequence number:%d\n",seq_num
);
1884 max_seq
= this_bundle
->max_seq
;
1885 maskSeq
= max_seq
- 1;
1888 * Expand sequence number to 32 bits, making it as close
1889 * as possible to this_fragmentation->M.
1891 seq_num
|= this_fragmentation
->M
& ~maskSeq
;
1892 if ((int)(this_fragmentation
->M
- seq_num
) > (int)(maskSeq
>> 1))
1894 seq_num
+= maskSeq
+ 1;
1896 else if ((int)(seq_num
- this_fragmentation
->M
) > (int)(maskSeq
>> 1))
1898 seq_num
-= maskSeq
+ 1; /* should never happen */
1901 // calculate this fragment's offset from the begin seq in the bundle
1902 frag_offset
= (int) (seq_num
- this_fragmentation
->start_seq
);
1904 sess_local
[s
].last_seq
= seq_num
;
1906 // calculate the jitter average
1907 uint32_t ljitter
= time_now_ms
- sess_local
[s
].prev_time
;
1910 sess_local
[s
].jitteravg
= (sess_local
[s
].jitteravg
+ ljitter
)>>1;
1911 sess_local
[s
].prev_time
= time_now_ms
;
1916 if (seq_num
< this_fragmentation
->M
)
1919 this_fragmentation
->M
= seq_num
;
1923 Mmin
= sess_local
[(this_bundle
->members
[0])].last_seq
;
1924 for (i
= 1; i
< this_bundle
->num_of_links
; i
++)
1926 uint32_t s_seq
= sess_local
[(this_bundle
->members
[i
])].last_seq
;
1930 this_fragmentation
->M
= Mmin
;
1933 // calculate M offset of the M seq in the bundle
1934 int M_offset
= (int) (Mmin
- this_fragmentation
->start_seq
);
1936 if (M_offset
>= MAXFRAGNUM
)
1938 // There have a long break of the link !!!!!!!!
1939 // M_offset is bigger that the fragmentation buffer size
1940 LOG(3, s
, t
, "MPPP: M_offset out of range, min:%d, begin_seq:%d\n", Mmin
, this_fragmentation
->start_seq
);
1942 // Calculate the new start index, the previous frag are lost
1943 begin_index
= (M_offset
+ this_fragmentation
->start_index
) & MAXFRAGNUM_MASK
;
1945 // Set new Start sequence
1946 this_fragmentation
->start_index
= begin_index
;
1947 this_fragmentation
->start_seq
= Mmin
;
1949 // recalculate the fragment offset from the new begin seq in the bundle
1950 frag_offset
= (int) (seq_num
- Mmin
);
1953 // discard this fragment if the packet comes before the start sequence
1954 if (frag_offset
< 0)
1956 // this packet comes before the next
1957 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
);
1961 // discard if frag_offset is bigger that the fragmentation buffer size
1962 if (frag_offset
>= MAXFRAGNUM
)
1964 // frag_offset is bigger that the fragmentation buffer size
1965 LOG(3, s
, t
, "MPPP: Index out of range, seq:%d, begin_seq:%d\n", seq_num
, this_fragmentation
->start_seq
);
1969 //caculate received fragment's index in the fragment array
1970 frag_index
= (frag_offset
+ this_fragmentation
->start_index
) & MAXFRAGNUM_MASK
;
1972 // insert the frame in it's place
1973 fragmentt
*this_frag
= &this_fragmentation
->fragment
[frag_index
];
1975 if (this_frag
->length
> 0)
1976 // This fragment is lost, It was around the buffer and it was never completed the packet.
1977 LOG(3, this_frag
->sid
, this_frag
->tid
, "MPPP: (INSERT) seq_num:%d frag_index:%d flags:%02X is LOST\n",
1978 this_frag
->seq
, frag_index
, this_frag
->flags
);
1980 this_frag
->length
= l
;
1983 this_frag
->flags
= flags
;
1984 this_frag
->seq
= seq_num
;
1985 this_frag
->jitteravg
= sess_local
[s
].jitteravg
;
1986 memcpy(this_frag
->data
, p
, l
);
1988 LOG(4, s
, t
, "MPPP: seq_num:%d frag_index:%d INSERTED flags: %02X\n", seq_num
, frag_index
, flags
);
1991 frag_index_next
= (frag_index
+ 1) & MAXFRAGNUM_MASK
;
1992 //previous frag index
1993 frag_index_prev
= (frag_index
- 1) & MAXFRAGNUM_MASK
;
1995 seq_num_next
= seq_num
+ 1;
1997 seq_num_prev
= seq_num
- 1;
1999 // Clean the buffer and log the lost fragments
2000 if ((frag_index_next
!= this_fragmentation
->start_index
) && this_fragmentation
->fragment
[frag_index_next
].length
)
2002 // check if the next frag is a lost fragment
2003 if (this_fragmentation
->fragment
[frag_index_next
].seq
!= seq_num_next
)
2005 // This fragment is lost, It was around the buffer and it was never completed the packet.
2006 LOG(3, this_fragmentation
->fragment
[frag_index_next
].sid
, this_fragmentation
->fragment
[frag_index_next
].tid
,
2007 "MPPP: (NEXT) seq_num:%d frag_index:%d flags:%02X is LOST\n",
2008 this_fragmentation
->fragment
[frag_index_next
].seq
, frag_index_next
,
2009 this_fragmentation
->fragment
[frag_index_next
].flags
);
2010 // this frag is lost
2011 this_fragmentation
->fragment
[frag_index_next
].length
= 0;
2012 this_fragmentation
->fragment
[frag_index_next
].flags
= 0;
2014 if (begin_frame
&& (!end_frame
)) return; // assembling frame failed
2018 // Clean the buffer and log the lost fragments
2019 if ((frag_index
!= this_fragmentation
->start_index
) && this_fragmentation
->fragment
[frag_index_prev
].length
)
2021 // check if the next frag is a lost fragment
2022 if (this_fragmentation
->fragment
[frag_index_prev
].seq
!= seq_num_prev
)
2024 // This fragment is lost, It was around the buffer and it was never completed the packet.
2025 LOG(3, this_fragmentation
->fragment
[frag_index_prev
].sid
, this_fragmentation
->fragment
[frag_index_prev
].tid
,
2026 "MPPP: (PREV) seq_num:%d frag_index:%d flags:%02X is LOST\n",
2027 this_fragmentation
->fragment
[frag_index_prev
].seq
, frag_index_prev
,
2028 this_fragmentation
->fragment
[frag_index_prev
].flags
);
2030 this_fragmentation
->fragment
[frag_index_prev
].length
= 0;
2031 this_fragmentation
->fragment
[frag_index_prev
].flags
= 0;
2033 if (end_frame
&& (!begin_frame
)) return; // assembling frame failed
2038 begin_index
= this_fragmentation
->start_index
;
2039 uint32_t b_seq
= this_fragmentation
->start_seq
;
2040 // Try to find a Begin sequence from the start_seq sequence to M sequence
2041 while (b_seq
< Mmin
)
2043 if (this_fragmentation
->fragment
[begin_index
].length
)
2045 if (b_seq
== this_fragmentation
->fragment
[begin_index
].seq
)
2047 if (this_fragmentation
->fragment
[begin_index
].flags
& MP_BEGIN
)
2050 // Adjust the new start sequence and start index
2051 this_fragmentation
->start_index
= begin_index
;
2052 this_fragmentation
->start_seq
= b_seq
;
2053 // Begin Sequence found, now try to found the End Sequence to complete the frame
2054 end_index
= begin_index
;
2055 while (b_seq
< Mmin
)
2057 if (this_fragmentation
->fragment
[end_index
].length
)
2059 if (b_seq
== this_fragmentation
->fragment
[end_index
].seq
)
2061 if (this_fragmentation
->fragment
[end_index
].flags
& MP_END
)
2063 // The End sequence was found and the frame is complete
2070 // This fragment is lost, it was never completed the packet.
2071 LOG(3, this_fragmentation
->fragment
[end_index
].sid
, this_fragmentation
->fragment
[end_index
].tid
,
2072 "MPPP: (FIND END) seq_num:%d frag_index:%d flags:%02X is LOST\n",
2073 this_fragmentation
->fragment
[end_index
].seq
, begin_index
,
2074 this_fragmentation
->fragment
[end_index
].flags
);
2075 // this frag is lost
2076 this_fragmentation
->fragment
[end_index
].length
= 0;
2077 this_fragmentation
->fragment
[end_index
].flags
= 0;
2078 // This frame is not complete find the next Begin
2084 // This frame is not complete find the next Begin if exist
2087 end_index
= (end_index
+1) & MAXFRAGNUM_MASK
;
2092 // The End sequence was found and the frame is complete
2095 // find the next Begin
2096 begin_index
= end_index
;
2101 // This fragment is lost, it was never completed the packet.
2102 LOG(3, this_fragmentation
->fragment
[begin_index
].sid
, this_fragmentation
->fragment
[begin_index
].tid
,
2103 "MPPP: (FIND BEGIN) seq_num:%d frag_index:%d flags:%02X is LOST\n",
2104 this_fragmentation
->fragment
[begin_index
].seq
, begin_index
,
2105 this_fragmentation
->fragment
[begin_index
].flags
);
2106 // this frag is lost
2107 this_fragmentation
->fragment
[begin_index
].length
= 0;
2108 this_fragmentation
->fragment
[begin_index
].flags
= 0;
2111 begin_index
= (begin_index
+1) & MAXFRAGNUM_MASK
;
2116 // try to assemble the frame that has the received fragment as a member
2117 // get the beginning of this frame
2118 begin_index
= end_index
= this_fragmentation
->start_index
;
2119 if (this_fragmentation
->fragment
[begin_index
].length
)
2121 if (!(this_fragmentation
->fragment
[begin_index
].flags
& MP_BEGIN
))
2123 LOG(3, this_fragmentation
->fragment
[begin_index
].sid
, this_fragmentation
->fragment
[begin_index
].tid
,
2124 "MPPP: (NOT BEGIN) seq_num:%d frag_index:%d flags:%02X\n",
2125 this_fragmentation
->fragment
[begin_index
].seq
, begin_index
,
2126 this_fragmentation
->fragment
[begin_index
].flags
);
2127 // should occur only after an "M_Offset out of range"
2128 // The start sequence must be a begin sequence
2129 this_fragmentation
->start_index
= (begin_index
+1) & MAXFRAGNUM_MASK
;
2130 this_fragmentation
->start_seq
++;
2131 return; // assembling frame failed
2135 return; // assembling frame failed
2137 // get the end of his frame
2138 while (this_fragmentation
->fragment
[end_index
].length
)
2140 if (this_fragmentation
->fragment
[end_index
].flags
& MP_END
)
2143 end_index
= (end_index
+1) & MAXFRAGNUM_MASK
;
2145 if (end_index
== this_fragmentation
->start_index
)
2146 return; // assembling frame failed
2149 // return if a lost fragment is found
2150 if (!(this_fragmentation
->fragment
[end_index
].length
))
2151 return; // assembling frame failed
2153 // assemble the packet
2154 //assemble frame, process it, reset fragmentation
2155 uint16_t cur_len
= 4; // This is set to 4 to leave 4 bytes for function processipin
2157 LOG(4, s
, t
, "MPPP: processing fragments from %d to %d\n", begin_index
, end_index
);
2158 // Push to the receive buffer
2160 for (i
= begin_index
;; i
= (i
+ 1) & MAXFRAGNUM_MASK
)
2162 this_frag
= &this_fragmentation
->fragment
[i
];
2163 if(cur_len
+ this_frag
->length
> MAXETHER
)
2165 LOG(2, s
, t
, "MPPP: discarding reassembled frames larger than MAXETHER\n");
2169 memcpy(this_fragmentation
->reassembled_frame
+cur_len
, this_frag
->data
, this_frag
->length
);
2170 LOG(5, s
, t
, "MPPP: processing frame at %d, with len %d\n", i
, this_frag
->length
);
2172 cur_len
+= this_frag
->length
;
2175 this_fragmentation
->re_frame_len
= cur_len
;
2176 this_fragmentation
->re_frame_begin_index
= begin_index
;
2177 this_fragmentation
->re_frame_end_index
= end_index
;
2178 // Process the resassembled frame
2179 LOG(5, s
, t
, "MPPP: Process the reassembled frame, len=%d\n",cur_len
);
2180 processmpframe(s
, t
, this_fragmentation
->reassembled_frame
, this_fragmentation
->re_frame_len
, 1);
2185 // Set reassembled frame length to zero after processing it
2186 this_fragmentation
->re_frame_len
= 0;
2187 for (i
= begin_index
;; i
= (i
+ 1) & MAXFRAGNUM_MASK
)
2189 this_fragmentation
->fragment
[i
].length
= 0; // Indicates that this fragment has been consumed
2190 this_fragmentation
->fragment
[i
].flags
= 0;
2195 // Set the new start_index and start_seq
2196 this_fragmentation
->start_index
= (end_index
+ 1) & MAXFRAGNUM_MASK
;
2197 this_fragmentation
->start_seq
= this_fragmentation
->fragment
[end_index
].seq
+ 1;
2198 LOG(4, s
, t
, "MPPP after assembling: start index is = %d, start seq=%d\n", this_fragmentation
->start_index
, this_fragmentation
->start_seq
);
2200 begin_index
= this_fragmentation
->start_index
;
2201 if ((this_fragmentation
->fragment
[begin_index
].length
) &&
2202 (this_fragmentation
->fragment
[begin_index
].seq
!= this_fragmentation
->start_seq
))
2204 LOG(3, this_fragmentation
->fragment
[begin_index
].sid
, this_fragmentation
->fragment
[begin_index
].tid
,
2205 "MPPP: (START) seq_num:%d frag_index:%d flags:%02X is LOST\n",
2206 this_fragmentation
->fragment
[begin_index
].seq
, begin_index
,
2207 this_fragmentation
->fragment
[begin_index
].flags
);
2208 this_fragmentation
->fragment
[begin_index
].length
= 0;
2209 this_fragmentation
->fragment
[begin_index
].flags
= 0;
2212 if (this_fragmentation
->start_seq
<= Mmin
)
2213 // It's possible to find other complete frame or lost frame.
2215 else if ((this_fragmentation
->fragment
[begin_index
].length
) &&
2216 (this_fragmentation
->fragment
[begin_index
].flags
& MP_BEGIN
))
2217 // may be that the next frame is completed
2218 goto assembling_frame
;
2223 // process IPv6 packet received
2225 // This MUST be called with at least 4 byte behind 'p'.
2226 // (i.e. this routine writes to p[-4]).
2227 void processipv6in(sessionidt s
, tunnelidt t
, uint8_t *p
, uint16_t l
)
2232 CSTAT(processipv6in
);
2234 LOG_HEX(5, "IPv6", p
, l
);
2236 ip
= *(struct in6_addr
*) (p
+ 8);
2237 ipv4
= ntohl(*(uint32_t *)(p
+ 16));
2241 LOG(1, s
, t
, "IP packet too long %d\n", l
);
2242 STAT(tunnel_rx_errors
);
2246 if (session
[s
].ppp
.phase
!= Network
|| session
[s
].ppp
.ipv6cp
!= Opened
)
2250 if (ipv4
!= session
[s
].ip
&& memcmp(&config
->ipv6_prefix
, &ip
, 8) && sessionbyipv6(ip
) != s
)
2252 char str
[INET6_ADDRSTRLEN
];
2253 LOG(5, s
, t
, "Dropping packet with spoofed IP %s\n",
2254 inet_ntop(AF_INET6
, &ip
, str
, INET6_ADDRSTRLEN
));
2258 // Check if it's a Router Solicition message.
2259 if (*(p
+ 6) == 58 && *(p
+ 7) == 255 && *(p
+ 24) == 0xFF && *(p
+ 25) == 2 &&
2260 *(uint32_t *)(p
+ 26) == 0 && *(uint32_t *)(p
+ 30) == 0 &&
2261 *(uint32_t *)(p
+ 34) == 0 &&
2262 *(p
+ 38) == 0 && *(p
+ 39) == 2 && *(p
+ 40) == 133) {
2263 LOG(3, s
, t
, "Got IPv6 RS\n");
2264 send_ipv6_ra(s
, t
, &ip
);
2268 // Add on the tun header
2270 *(uint32_t *) p
= htonl(PKTIPV6
);
2273 // Are we throttled and a slave?
2274 if (session
[s
].tbf_in
&& !config
->cluster_iam_master
) {
2275 // Pass it to the master for handling.
2276 master_throttle_packet(session
[s
].tbf_in
, p
, l
);
2280 // Are we throttled and a master??
2281 if (session
[s
].tbf_in
&& config
->cluster_iam_master
) {
2282 // Actually handle the throttled packets.
2283 tbf_queue_packet(session
[s
].tbf_in
, p
, l
);
2288 if (tun_write(p
, l
) < 0)
2290 STAT(tun_tx_errors
);
2291 LOG(0, s
, t
, "Error writing %d bytes to TUN device: %s (tunfd=%d, p=%p)\n",
2292 l
, strerror(errno
), tunfd
, p
);
2300 if (session
[s
].snoop_ip
&& session
[s
].snoop_port
)
2302 // Snooping this session
2303 snoop_send_packet(p
, l
, session
[s
].snoop_ip
, session
[s
].snoop_port
);
2306 update_sessions_in_stat(s
, l
);
2310 STAT(tun_tx_packets
);
2311 INC_STAT(tun_tx_bytes
, l
);
2315 // Helper routine for the TBF filters.
2316 // Used to send queued data in from the user.
2318 void send_ipin(sessionidt s
, uint8_t *buf
, int len
)
2320 LOG_HEX(5, "IP in throttled", buf
, len
);
2322 if (write(tunfd
, buf
, len
) < 0)
2324 STAT(tun_tx_errors
);
2325 LOG(0, 0, 0, "Error writing %d bytes to TUN device: %s (tunfd=%d, p=%p)\n",
2326 len
, strerror(errno
), tunfd
, buf
);
2334 if (session
[s
].snoop_ip
&& session
[s
].snoop_port
)
2336 // Snooping this session
2337 snoop_send_packet(buf
, len
, session
[s
].snoop_ip
, session
[s
].snoop_port
);
2340 // Increment packet counters
2341 increment_counter(&session
[s
].cin
, &session
[s
].cin_wrap
, len
);
2342 session
[s
].cin_delta
+= len
;
2345 sess_local
[s
].cin
+= len
;
2346 sess_local
[s
].pin
++;
2350 STAT(tun_tx_packets
);
2351 INC_STAT(tun_tx_bytes
, len
- 4);
2355 // Process CCP messages
2356 void processccp(sessionidt s
, tunnelidt t
, uint8_t *p
, uint16_t l
)
2358 uint8_t b
[MAXETHER
];
2363 LOG_HEX(5, "CCP", p
, l
);
2365 if (session
[s
].ppp
.phase
< Network
)
2367 LOG(2, s
, t
, "CCP %s ignored in %s phase\n", ppp_code(*p
), ppp_phase(session
[s
].ppp
.phase
));
2373 LOG(1, s
, t
, "Short CCP packet\n");
2374 STAT(tunnel_rx_errors
);
2377 LOG(4, s
, t
, "CCP: recv %s\n", ppp_code(*p
));
2378 if (*p
== ConfigAck
)
2380 switch (session
[s
].ppp
.ccp
)
2383 initialise_restart_count(s
, ccp
);
2384 change_state(s
, ccp
, AckReceived
);
2389 LOG(2, s
, t
, "CCP: ConfigAck in state %s? Sending ConfigReq\n", ppp_state(session
[s
].ppp
.ccp
));
2391 change_state(s
, ccp
, RequestSent
);
2395 LOG(3, s
, t
, "CCP: Opened\n");
2396 change_state(s
, ccp
, Opened
);
2400 LOG(2, s
, t
, "CCP: ignoring %s in state %s\n", ppp_code(*p
), ppp_state(session
[s
].ppp
.ccp
));
2403 else if (*p
== ConfigReq
)
2405 if (l
< 6) // accept no compression
2407 else // compression requested--reject
2410 q
= makeppp(b
, sizeof(b
), p
, l
, s
, t
, PPPCCP
, 0, 0, 0);
2413 switch (session
[s
].ppp
.ccp
)
2416 q
= makeppp(b
, sizeof(b
), p
, 2, s
, t
, PPPCCP
, 0, 0, 0);
2419 *((uint16_t *) (q
+ 2)) = htons(l
= 4);
2423 initialise_restart_count(s
, ccp
);
2425 if (*q
== ConfigAck
)
2426 change_state(s
, ccp
, AckSent
);
2428 change_state(s
, ccp
, RequestSent
);
2433 if (*q
== ConfigAck
)
2434 change_state(s
, ccp
, AckSent
);
2439 if (*q
== ConfigAck
)
2440 change_state(s
, ccp
, Opened
);
2445 initialise_restart_count(s
, ccp
);
2450 if (*q
== ConfigAck
)
2451 change_state(s
, ccp
, AckSent
);
2453 change_state(s
, ccp
, RequestSent
);
2458 LOG(2, s
, t
, "CCP: ignoring %s in state %s\n", ppp_code(*p
), ppp_state(session
[s
].ppp
.ccp
));
2462 LOG(4, s
, t
, "CCP: send %s\n", ppp_code(*q
));
2463 tunnelsend(b
, l
+ (q
- b
), t
);
2465 else if (*p
== TerminateReq
)
2468 q
= makeppp(b
, sizeof(b
), p
, l
, s
, t
, PPPCCP
, 0, 0, 0);
2470 LOG(3, s
, t
, "CCP: send %s\n", ppp_code(*q
));
2471 tunnelsend(b
, l
+ (q
- b
), t
);
2472 change_state(s
, ccp
, Stopped
);
2474 else if (*p
!= CodeRej
)
2476 ppp_code_rej(s
, t
, PPPCCP
, "CCP", p
, l
, b
, sizeof(b
));
2480 // send a CHAP challenge
2481 void sendchap(sessionidt s
, tunnelidt t
)
2483 uint8_t b
[MAXETHER
];
2492 LOG(1, s
, t
, "No RADIUS to send challenge\n");
2493 STAT(tunnel_tx_errors
);
2497 LOG(1, s
, t
, "Send CHAP challenge\n");
2499 radius
[r
].chap
= 1; // CHAP not PAP
2501 if (radius
[r
].state
!= RADIUSCHAP
)
2504 radius
[r
].state
= RADIUSCHAP
;
2505 radius
[r
].retry
= backoff(radius
[r
].try++);
2506 if (radius
[r
].try > 5)
2508 sessionshutdown(s
, "CHAP timeout.", CDN_ADMIN_DISC
, TERM_REAUTHENTICATION_FAILURE
);
2509 STAT(tunnel_tx_errors
);
2512 q
= makeppp(b
, sizeof(b
), 0, 0, s
, t
, PPPCHAP
, 0, 0, 0);
2515 *q
= 1; // challenge
2516 q
[1] = radius
[r
].id
; // ID
2517 q
[4] = 16; // value size (size of challenge)
2518 memcpy(q
+ 5, radius
[r
].auth
, 16); // challenge
2519 strcpy((char *) q
+ 21, hostname
); // our name
2520 *(uint16_t *) (q
+ 2) = htons(strlen(hostname
) + 21); // length
2521 tunnelsend(b
, strlen(hostname
) + 21 + (q
- b
), t
); // send it
2524 // fill in a L2TP message with a PPP frame,
2525 // returns start of PPP frame
2526 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
)
2528 uint16_t hdr
= 0x0002; // L2TP with no options
2529 uint16_t type
= mtype
;
2532 if (t
== TUNNEL_ID_PPPOE
)
2534 return pppoe_makeppp(b
, size
, p
, l
, s
, t
, mtype
, prio
, bid
, mp_bits
);
2537 if (size
< 16) // Need more space than this!!
2539 LOG(0, s
, t
, "makeppp buffer too small for L2TP header (size=%d)\n", size
);
2543 if (prio
) hdr
|= 0x0100; // set priority bit
2545 *(uint16_t *) (b
+ 0) = htons(hdr
);
2546 *(uint16_t *) (b
+ 2) = htons(tunnel
[t
].far
); // tunnel
2547 *(uint16_t *) (b
+ 4) = htons(session
[s
].far
); // session
2550 // Check whether this session is part of multilink
2553 if (bundle
[bid
].num_of_links
> 1)
2554 type
= PPPMP
; // Change PPP message type to the PPPMP
2559 if (type
== PPPLCP
|| !(session
[s
].flags
& SESSION_ACFC
))
2561 *(uint16_t *) b
= htons(0xFF03); // HDLC header
2565 if (type
< 0x100 && session
[s
].flags
& SESSION_PFC
)
2571 *(uint16_t *) b
= htons(type
);
2577 // Set the sequence number and (B)egin (E)nd flags
2578 if (session
[s
].mssf
)
2580 // Set the multilink bits
2581 uint16_t bits_send
= mp_bits
;
2582 *(uint16_t *) b
= htons((bundle
[bid
].seq_num_t
& 0x0FFF)|bits_send
);
2587 *(uint32_t *) b
= htonl(bundle
[bid
].seq_num_t
);
2588 // Set the multilink bits
2593 bundle
[bid
].seq_num_t
++;
2595 // Add the message type if this fragment has the begin bit set
2596 if (mp_bits
& MP_BEGIN
)
2598 //*b++ = mtype; // The next two lines are instead of this
2599 *(uint16_t *) b
= htons(mtype
); // Message type
2604 if ((b
- start
) + l
> size
)
2606 LOG(2, s
, t
, "makeppp would overflow buffer (size=%d, header+payload=%td)\n", size
, (b
- start
) + l
);
2617 // fill in a L2TP message with a PPP frame,
2618 // returns start of PPP frame
2619 //(note: THIS ROUTINE CAN WRITES TO p[-28]).
2620 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
)
2622 uint16_t hdr
= 0x0002; // L2TP with no options
2623 uint16_t type
= mtype
;
2626 if (t
== TUNNEL_ID_PPPOE
)
2628 return opt_pppoe_makeppp(p
, l
, s
, t
, mtype
, prio
, bid
, mp_bits
);
2631 // Check whether this session is part of multilink
2634 if (bundle
[bid
].num_of_links
> 1)
2635 type
= PPPMP
; // Change PPP message type to the PPPMP
2642 // Add the message type if this fragment has the begin bit set
2643 if (mp_bits
& MP_BEGIN
)
2646 *(uint16_t *) b
= htons(mtype
); // Message type
2649 // Set the sequence number and (B)egin (E)nd flags
2650 if (session
[s
].mssf
)
2652 // Set the multilink bits
2653 uint16_t bits_send
= mp_bits
;
2655 *(uint16_t *) b
= htons((bundle
[bid
].seq_num_t
& 0x0FFF)|bits_send
);
2660 *(uint32_t *) b
= htonl(bundle
[bid
].seq_num_t
);
2661 // Set the multilink bits
2665 bundle
[bid
].seq_num_t
++;
2668 if (type
< 0x100 && session
[s
].flags
& SESSION_PFC
)
2676 *(uint16_t *) b
= htons(type
);
2679 if (type
== PPPLCP
|| !(session
[s
].flags
& SESSION_ACFC
))
2682 *(uint16_t *) b
= htons(0xFF03); // HDLC header
2685 if (prio
) hdr
|= 0x0100; // set priority bit
2687 *(uint16_t *) b
= htons(session
[s
].far
); // session
2689 *(uint16_t *) b
= htons(tunnel
[t
].far
); // tunnel
2691 *(uint16_t *) b
= htons(hdr
);
2696 static int add_lcp_auth(uint8_t *b
, int size
, int authtype
)
2699 if ((authtype
== AUTHCHAP
&& size
< 5) || size
< 4)
2702 *b
++ = 3; // Authentication-Protocol
2703 if (authtype
== AUTHCHAP
)
2705 len
= *b
++ = 5; // length
2706 *(uint16_t *) b
= htons(PPPCHAP
); b
+= 2;
2709 else if (authtype
== AUTHPAP
)
2711 len
= *b
++ = 4; // length
2712 *(uint16_t *) b
= htons(PPPPAP
); b
+= 2;
2716 LOG(0, 0, 0, "add_lcp_auth called with unsupported auth type %d\n", authtype
);
2722 // Send LCP ConfigReq for MRU, authentication type and magic no
2723 void sendlcp(sessionidt s
, tunnelidt t
)
2725 uint8_t b
[500], *q
, *l
;
2726 int authtype
= sess_local
[s
].lcp_authtype
;
2728 if (!(q
= makeppp(b
, sizeof(b
), NULL
, 0, s
, t
, PPPLCP
, 0, 0, 0)))
2731 LOG(3, s
, t
, "LCP: send ConfigReq%s%s%s including MP options\n",
2732 authtype
? " (" : "",
2733 authtype
? (authtype
== AUTHCHAP
? "CHAP" : "PAP") : "",
2734 authtype
? ")" : "");
2738 *l
++ = ++sess_local
[s
].lcp_ident
; // ID
2740 l
+= 2; //Save space for length
2742 if (sess_local
[s
].ppp_mru
)
2744 *l
++ = 1; *l
++ = 4; // Maximum-Receive-Unit (length 4)
2745 *(uint16_t *) l
= htons(sess_local
[s
].ppp_mru
); l
+= 2;
2749 l
+= add_lcp_auth(l
, sizeof(b
) - (l
- b
), authtype
);
2751 if (session
[s
].magic
)
2753 *l
++ = 5; *l
++ = 6; // Magic-Number (length 6)
2754 *(uint32_t *) l
= htonl(session
[s
].magic
);
2758 if (sess_local
[s
].mp_mrru
)
2760 *l
++ = 17; *l
++ = 4; // Multilink Max-Receive-Reconstructed-Unit (length 4)
2761 *(uint16_t *) l
= htons(sess_local
[s
].mp_mrru
); l
+= 2;
2764 if (sess_local
[s
].mp_epdis
)
2766 *l
++ = 19; *l
++ = 7; // Multilink Endpoint Discriminator (length 7)
2767 *l
++ = IPADDR
; // Endpoint Discriminator class
2768 *(uint32_t *) l
= htonl(sess_local
[s
].mp_epdis
);
2772 *(uint16_t *)(q
+ 2) = htons(l
- q
); // Length
2774 LOG_HEX(5, "PPPLCP", q
, l
- q
);
2775 if (config
->debug
> 3) dumplcp(q
, l
- q
);
2777 tunnelsend(b
, (l
- b
), t
);
2778 restart_timer(s
, lcp
);
2781 // Send CCP request for no compression
2782 void sendccp(sessionidt s
, tunnelidt t
)
2786 if (!(q
= makeppp(b
, sizeof(b
), NULL
, 0, s
, t
, PPPCCP
, 0, 0, 0)))
2789 LOG(3, s
, t
, "CCP: send ConfigReq (no compression)\n");
2792 *(q
+ 1) = ++sess_local
[s
].lcp_ident
; // ID
2793 *(uint16_t *)(q
+ 2) = htons(4); // Length
2795 LOG_HEX(5, "PPPCCP", q
, 4);
2796 tunnelsend(b
, (q
- b
) + 4 , t
);
2797 restart_timer(s
, ccp
);
2800 // Reject unknown/unconfigured protocols
2801 void protoreject(sessionidt s
, tunnelidt t
, uint8_t *p
, uint16_t l
, uint16_t proto
)
2804 uint8_t buf
[MAXETHER
];
2806 int mru
= session
[s
].mru
;
2807 if (mru
< MINMTU
) mru
= MINMTU
;
2808 if (mru
> sizeof(buf
)) mru
= sizeof(buf
);
2811 if (l
> mru
) l
= mru
;
2813 q
= makeppp(buf
, sizeof(buf
), 0, 0, s
, t
, PPPLCP
, 0, 0, 0);
2817 *(q
+ 1) = ++sess_local
[s
].lcp_ident
;
2818 *(uint16_t *)(q
+ 2) = htons(l
);
2819 *(uint16_t *)(q
+ 4) = htons(proto
);
2820 memcpy(q
+ 6, p
, l
- 6);
2822 if (proto
== PPPIPV6CP
)
2823 LOG(3, s
, t
, "LCP: send ProtocolRej (IPV6CP: not configured)\n");
2825 LOG(2, s
, t
, "LCP: sent ProtocolRej (0x%04X: unsupported)\n", proto
);
2827 tunnelsend(buf
, l
+ (q
- buf
), t
);