18 extern tunnelt
*tunnel
;
19 extern bundlet
*bundle
;
20 extern fragmentationt
*frag
;
21 extern sessiont
*session
;
22 extern radiust
*radius
;
24 extern char hostname
[];
25 extern uint32_t eth_tx
;
26 extern time_t time_now
;
27 extern uint64_t time_now_ms
;
28 extern configt
*config
;
30 static int add_lcp_auth(uint8_t *b
, int size
, int authtype
);
31 static bundleidt
new_bundle(void);
32 static int epdiscmp(epdist
, epdist
);
33 static void setepdis(epdist
*, epdist
);
34 static void ipcp_open(sessionidt s
, tunnelidt t
);
36 static int first_session_in_bundle(sessionidt s
)
39 for (i
= 1; i
< MAXBUNDLE
; i
++)
40 if (bundle
[i
].state
!= BUNDLEFREE
)
41 if (epdiscmp(session
[s
].epdis
,bundle
[i
].epdis
) && !strcmp(session
[s
].user
, bundle
[i
].user
))
46 // Process PAP messages
47 void processpap(sessionidt s
, tunnelidt t
, uint8_t *p
, uint16_t l
)
56 LOG_HEX(5, "PAP", p
, l
);
59 LOG(1, s
, t
, "Short PAP %u bytes\n", l
);
60 STAT(tunnel_rx_errors
);
61 sessionshutdown(s
, "Short PAP packet.", CDN_ADMIN_DISC
, TERM_USER_ERROR
);
65 if ((hl
= ntohs(*(uint16_t *) (p
+ 2))) > l
)
67 LOG(1, s
, t
, "Length mismatch PAP %u/%u\n", hl
, l
);
68 STAT(tunnel_rx_errors
);
69 sessionshutdown(s
, "PAP length mismatch.", CDN_ADMIN_DISC
, TERM_USER_ERROR
);
76 LOG(1, s
, t
, "Unexpected PAP code %d\n", *p
);
77 STAT(tunnel_rx_errors
);
78 sessionshutdown(s
, "Unexpected PAP code.", CDN_ADMIN_DISC
, TERM_USER_ERROR
);
82 if (session
[s
].ppp
.phase
!= Authenticate
)
84 LOG(2, s
, t
, "PAP ignored in %s phase\n", ppp_phase(session
[s
].ppp
.phase
));
91 user
[0] = pass
[0] = 0;
92 if (*b
&& *b
< sizeof(user
))
94 memcpy(user
, b
+ 1, *b
);
97 if (*b
&& *b
< sizeof(pass
))
99 memcpy(pass
, b
+ 1, *b
);
103 LOG(3, s
, t
, "PAP login %s/%s\n", user
, pass
);
106 if ((!config
->disable_lac_func
) && lac_conf_forwardtoremotelns(s
, user
))
108 // Creating a tunnel/session has been started
112 if (session
[s
].ip
|| !(r
= radiusnew(s
)))
114 // respond now, either no RADIUS available or already authenticated
117 uint8_t *p
= makeppp(b
, sizeof(b
), 0, 0, s
, t
, PPPPAP
, 0, 0, 0);
123 *p
= 3; // cant authorise
125 *(uint16_t *) (p
+ 2) = htons(5); // length
126 p
[4] = 0; // no message
127 tunnelsend(b
, 5 + (p
- b
), t
); // send it
131 LOG(3, s
, t
, "Already an IP allocated: %s (%d)\n",
132 fmtaddr(htonl(session
[s
].ip
), 0), session
[s
].ip_pool_index
);
136 LOG(1, s
, t
, "No RADIUS session available to authenticate session...\n");
137 sessionshutdown(s
, "No free RADIUS sessions.", CDN_UNAVAILABLE
, TERM_SERVICE_UNAVAILABLE
);
142 // Run PRE_AUTH plugins
143 struct param_pre_auth packet
= { &tunnel
[t
], &session
[s
], strdup(user
), strdup(pass
), PPPPAP
, 1 };
144 run_plugins(PLUGIN_PRE_AUTH
, &packet
);
145 if (!packet
.continue_auth
)
147 LOG(3, s
, t
, "A plugin rejected PRE_AUTH\n");
148 if (packet
.username
) free(packet
.username
);
149 if (packet
.password
) free(packet
.password
);
153 strncpy(session
[s
].user
, packet
.username
, sizeof(session
[s
].user
) - 1);
154 strncpy(radius
[r
].pass
, packet
.password
, sizeof(radius
[r
].pass
) - 1);
156 free(packet
.username
);
157 free(packet
.password
);
160 LOG(3, s
, t
, "Sending login for %s/%s to RADIUS\n", user
, pass
);
161 if ((session
[s
].mrru
) && (!first_session_in_bundle(s
)))
162 radiussend(r
, RADIUSJUSTAUTH
);
164 radiussend(r
, RADIUSAUTH
);
168 // Process CHAP messages
169 void processchap(sessionidt s
, tunnelidt t
, uint8_t *p
, uint16_t l
)
176 LOG_HEX(5, "CHAP", p
, l
);
180 LOG(1, s
, t
, "Short CHAP %u bytes\n", l
);
181 STAT(tunnel_rx_errors
);
182 sessionshutdown(s
, "Short CHAP packet.", CDN_ADMIN_DISC
, TERM_USER_ERROR
);
186 if ((hl
= ntohs(*(uint16_t *) (p
+ 2))) > l
)
188 LOG(1, s
, t
, "Length mismatch CHAP %u/%u\n", hl
, l
);
189 STAT(tunnel_rx_errors
);
190 sessionshutdown(s
, "CHAP length mismatch.", CDN_ADMIN_DISC
, TERM_USER_ERROR
);
197 LOG(1, s
, t
, "Unexpected CHAP response code %d\n", *p
);
198 STAT(tunnel_rx_errors
);
199 sessionshutdown(s
, "CHAP length mismatch.", CDN_ADMIN_DISC
, TERM_USER_ERROR
);
203 if (session
[s
].ppp
.phase
!= Authenticate
)
205 LOG(2, s
, t
, "CHAP ignored in %s phase\n", ppp_phase(session
[s
].ppp
.phase
));
209 r
= sess_local
[s
].radius
;
212 LOG(3, s
, t
, "Unexpected CHAP message\n");
214 // Some modems (Netgear DM602, possibly others) persist in using CHAP even
215 // after ACKing our ConfigReq for PAP.
216 if (sess_local
[s
].lcp_authtype
== AUTHPAP
&& config
->radius_authtypes
& AUTHCHAP
)
218 sess_local
[s
].lcp_authtype
= AUTHCHAP
;
224 if (p
[1] != radius
[r
].id
)
226 LOG(1, s
, t
, "Wrong CHAP response ID %d (should be %d) (%d)\n", p
[1], radius
[r
].id
, r
);
227 STAT(tunnel_rx_errors
);
228 sessionshutdown(s
, "Unexpected CHAP response ID.", CDN_ADMIN_DISC
, TERM_USER_ERROR
);
232 if (l
< 5 || p
[4] != 16)
234 LOG(1, s
, t
, "Bad CHAP response length %d\n", l
< 5 ? -1 : p
[4]);
235 STAT(tunnel_rx_errors
);
236 sessionshutdown(s
, "Bad CHAP response length.", CDN_ADMIN_DISC
, TERM_USER_ERROR
);
242 if (l
< 16 || l
- 16 >= sizeof(session
[s
].user
))
244 LOG(1, s
, t
, "CHAP user too long %d\n", l
- 16);
245 STAT(tunnel_rx_errors
);
246 sessionshutdown(s
, "CHAP username too long.", CDN_ADMIN_DISC
, TERM_USER_ERROR
);
250 // Run PRE_AUTH plugins
252 struct param_pre_auth packet
= { &tunnel
[t
], &session
[s
], NULL
, NULL
, PPPCHAP
, 1 };
254 packet
.password
= calloc(17, 1);
255 memcpy(packet
.password
, p
, 16);
260 packet
.username
= calloc(l
+ 1, 1);
261 memcpy(packet
.username
, p
, l
);
263 if ((!config
->disable_lac_func
) && lac_conf_forwardtoremotelns(s
, packet
.username
))
265 free(packet
.username
);
266 free(packet
.password
);
267 // Creating a tunnel/session has been started
271 run_plugins(PLUGIN_PRE_AUTH
, &packet
);
272 if (!packet
.continue_auth
)
274 LOG(3, s
, t
, "A plugin rejected PRE_AUTH\n");
275 if (packet
.username
) free(packet
.username
);
276 if (packet
.password
) free(packet
.password
);
280 strncpy(session
[s
].user
, packet
.username
, sizeof(session
[s
].user
) - 1);
281 memcpy(radius
[r
].pass
, packet
.password
, 16);
283 free(packet
.username
);
284 free(packet
.password
);
288 LOG(3, s
, t
, "CHAP login %s\n", session
[s
].user
);
289 if ((session
[s
].mrru
) && (!first_session_in_bundle(s
)))
290 radiussend(r
, RADIUSJUSTAUTH
);
292 radiussend(r
, RADIUSAUTH
);
295 static void dumplcp(uint8_t *p
, int l
)
298 uint8_t *o
= (p
+ 4);
300 LOG_HEX(5, "PPP LCP Packet", p
, l
);
301 LOG(4, 0, 0, "PPP LCP Packet type %d (%s len %d)\n", *p
, ppp_code((int)*p
), ntohs( ((uint16_t *) p
)[1]) );
302 LOG(4, 0, 0, "Length: %d\n", l
);
303 if (*p
!= ConfigReq
&& *p
!= ConfigRej
&& *p
!= ConfigAck
)
312 LOG(4, 0, 0, " Option length is %d...\n", length
);
317 LOG(4, 0, 0, " Option type is 0...\n");
324 case 1: // Maximum-Receive-Unit
326 LOG(4, 0, 0, " %s %d\n", ppp_lcp_option(type
), ntohs(*(uint16_t *)(o
+ 2)));
328 LOG(4, 0, 0, " %s odd length %d\n", ppp_lcp_option(type
), length
);
330 case 2: // Async-Control-Character-Map
333 uint32_t asyncmap
= ntohl(*(uint32_t *)(o
+ 2));
334 LOG(4, 0, 0, " %s %x\n", ppp_lcp_option(type
), asyncmap
);
337 LOG(4, 0, 0, " %s odd length %d\n", ppp_lcp_option(type
), length
);
339 case 3: // Authentication-Protocol
342 int proto
= ntohs(*(uint16_t *)(o
+ 2));
343 LOG(4, 0, 0, " %s 0x%x (%s)\n", ppp_lcp_option(type
), proto
,
344 proto
== PPPPAP
? "PAP" : "UNSUPPORTED");
346 else if (length
== 5)
348 int proto
= ntohs(*(uint16_t *)(o
+ 2));
350 LOG(4, 0, 0, " %s 0x%x 0x%x (%s)\n", ppp_lcp_option(type
), proto
, algo
,
351 (proto
== PPPCHAP
&& algo
== 5) ? "CHAP MD5" : "UNSUPPORTED");
354 LOG(4, 0, 0, " %s odd length %d\n", ppp_lcp_option(type
), length
);
356 case 4: // Quality-Protocol
358 uint32_t qp
= ntohl(*(uint32_t *)(o
+ 2));
359 LOG(4, 0, 0, " %s %x\n", ppp_lcp_option(type
), qp
);
362 case 5: // Magic-Number
365 uint32_t magicno
= ntohl(*(uint32_t *)(o
+ 2));
366 LOG(4, 0, 0, " %s %x\n", ppp_lcp_option(type
), magicno
);
369 LOG(4, 0, 0, " %s odd length %d\n", ppp_lcp_option(type
), length
);
371 case 7: // Protocol-Field-Compression
372 case 8: // Address-And-Control-Field-Compression
373 LOG(4, 0, 0, " %s\n", ppp_lcp_option(type
));
376 LOG(2, 0, 0, " Unknown PPP LCP Option type %d\n", type
);
384 void lcp_open(sessionidt s
, tunnelidt t
)
386 // transition to Authentication or Network phase:
387 session
[s
].ppp
.phase
= sess_local
[s
].lcp_authtype
? Authenticate
: Network
;
389 LOG(3, s
, t
, "LCP: Opened, phase %s\n", ppp_phase(session
[s
].ppp
.phase
));
392 change_state(s
, lcp
, Opened
);
394 if (session
[s
].ppp
.phase
== Authenticate
)
396 if (sess_local
[s
].lcp_authtype
== AUTHCHAP
)
401 if(session
[s
].bundle
== 0 || bundle
[session
[s
].bundle
].num_of_links
== 1)
405 change_state(s
, ipcp
, RequestSent
);
406 // move to passive state for IPv6 (if configured), CCP
407 if (config
->ipv6_prefix
.s6_addr
[0])
408 change_state(s
, ipv6cp
, Stopped
);
410 change_state(s
, ipv6cp
, Closed
);
412 change_state(s
, ccp
, Stopped
);
416 sessionidt first_ses
= bundle
[session
[s
].bundle
].members
[0];
417 LOG(3, s
, t
, "MPPP: Skipping IPCP negotiation for session:%d, first session of bundle is:%d\n",s
,first_ses
);
423 void lcp_restart(sessionidt s
)
425 session
[s
].ppp
.phase
= Establish
;
427 change_state(s
, ipcp
, Dead
);
428 change_state(s
, ipv6cp
, Dead
);
429 change_state(s
, ccp
, Dead
);
432 static uint8_t *ppp_conf_rej(sessionidt s
, uint8_t *buf
, size_t blen
, uint16_t mtype
,
433 uint8_t **response
, uint8_t *queued
, uint8_t *packet
, uint8_t *option
)
435 if (!*response
|| **response
!= ConfigRej
)
437 queued
= *response
= makeppp(buf
, blen
, packet
, 2, s
, session
[s
].tunnel
, mtype
, 0, 0, 0);
445 if ((queued
- buf
+ option
[1]) > blen
)
447 LOG(2, s
, session
[s
].tunnel
, "PPP overflow for ConfigRej (proto %u, option %u).\n", mtype
, *option
);
451 memcpy(queued
, option
, option
[1]);
452 return queued
+ option
[1];
455 static uint8_t *ppp_conf_nak(sessionidt s
, uint8_t *buf
, size_t blen
, uint16_t mtype
,
456 uint8_t **response
, uint8_t *queued
, uint8_t *packet
, uint8_t *option
,
457 uint8_t *value
, size_t vlen
)
462 case PPPLCP
: nak_sent
= &sess_local
[s
].lcp
.nak_sent
; break;
463 case PPPIPCP
: nak_sent
= &sess_local
[s
].ipcp
.nak_sent
; break;
464 case PPPIPV6CP
: nak_sent
= &sess_local
[s
].ipv6cp
.nak_sent
; break;
465 default: return 0; // ?
468 if (*response
&& **response
!= ConfigNak
)
470 if (*nak_sent
< config
->ppp_max_failure
) // reject queued
473 return ppp_conf_rej(s
, buf
, blen
, mtype
, response
, 0, packet
, option
);
478 if (*nak_sent
>= config
->ppp_max_failure
)
479 return ppp_conf_rej(s
, buf
, blen
, mtype
, response
, 0, packet
, option
);
481 queued
= *response
= makeppp(buf
, blen
, packet
, 2, s
, session
[s
].tunnel
, mtype
, 0, 0, 0);
490 if ((queued
- buf
+ vlen
+ 2) > blen
)
492 LOG(2, s
, session
[s
].tunnel
, "PPP overflow for ConfigNak (proto %u, option %u).\n", mtype
, *option
);
497 *queued
++ = vlen
+ 2;
498 memcpy(queued
, value
, vlen
);
499 return queued
+ vlen
;
502 static void ppp_code_rej(sessionidt s
, tunnelidt t
, uint16_t proto
,
503 char *pname
, uint8_t *p
, uint16_t l
, uint8_t *buf
, size_t size
)
506 int mru
= session
[s
].mru
;
507 if (mru
< MINMTU
) mru
= MINMTU
;
508 if (mru
> size
) mru
= size
;
511 if (l
> mru
) l
= mru
;
513 q
= makeppp(buf
, size
, 0, 0, s
, t
, proto
, 0, 0, 0);
517 *(q
+ 1) = ++sess_local
[s
].lcp_ident
;
518 *(uint16_t *)(q
+ 2) = htons(l
);
519 memcpy(q
+ 4, p
, l
- 4);
521 LOG(2, s
, t
, "Unexpected %s code %s\n", pname
, ppp_code(*p
));
522 LOG(3, s
, t
, "%s: send %s\n", pname
, ppp_code(*q
));
523 if (config
->debug
> 3) dumplcp(q
, l
);
525 tunnelsend(buf
, l
+ (q
- buf
), t
);
528 // Process LCP messages
529 void processlcp(sessionidt s
, tunnelidt t
, uint8_t *p
, uint16_t l
)
537 LOG_HEX(5, "LCP", p
, l
);
540 LOG(1, s
, t
, "Short LCP %d bytes\n", l
);
541 STAT(tunnel_rx_errors
);
545 if ((hl
= ntohs(*(uint16_t *) (p
+ 2))) > l
)
547 LOG(1, s
, t
, "Length mismatch LCP %u/%u\n", hl
, l
);
548 STAT(tunnel_rx_errors
);
553 if (session
[s
].die
) // going down...
556 LOG(((*p
== EchoReq
|| *p
== EchoReply
) ? 4 : 3), s
, t
,
557 "LCP: recv %s\n", ppp_code(*p
));
559 if (config
->debug
> 3) dumplcp(p
, l
);
564 uint8_t *o
= (p
+ 4);
572 if (length
== 0 || type
== 0 || x
< length
) break;
575 case 3: // Authentication-Protocol
577 int proto
= ntohs(*(uint16_t *)(o
+ 2));
580 else if (proto
== PPPCHAP
&& *(o
+ 4) == 5)
590 if (!session
[s
].ip
&& authtype
)
591 sess_local
[s
].lcp_authtype
= authtype
;
593 switch (session
[s
].ppp
.lcp
)
596 initialise_restart_count(s
, lcp
);
597 change_state(s
, lcp
, AckReceived
);
602 LOG(2, s
, t
, "LCP: ConfigAck in state %s? Sending ConfigReq\n", ppp_state(session
[s
].ppp
.lcp
));
603 if (session
[s
].ppp
.lcp
== Opened
)
607 change_state(s
, lcp
, RequestSent
);
615 LOG(2, s
, t
, "LCP: ignoring %s in state %s\n", ppp_code(*p
), ppp_state(session
[s
].ppp
.lcp
));
618 else if (*p
== ConfigReq
)
621 uint8_t *o
= (p
+ 4);
622 uint8_t *response
= 0;
623 static uint8_t asyncmap
[4] = { 0, 0, 0, 0 }; // all zero
624 static uint8_t authproto
[5];
632 if (length
== 0 || type
== 0 || x
< length
) break;
635 case 1: // Maximum-Receive-Unit
637 uint16_t mru
= ntohs(*(uint16_t *)(o
+ 2));
640 session
[s
].mru
= mru
;
645 LOG(3, s
, t
, " Remote requesting MRU of %u. Rejecting.\n", mru
);
647 q
= ppp_conf_nak(s
, b
, sizeof(b
), PPPLCP
, &response
, q
, p
, o
, (uint8_t *) &mru
, sizeof(mru
));
651 case 2: // Async-Control-Character-Map
652 if (!ntohl(*(uint32_t *)(o
+ 2))) // all bits zero is OK
655 LOG(3, s
, t
, " Remote requesting asyncmap. Rejecting.\n");
656 q
= ppp_conf_nak(s
, b
, sizeof(b
), PPPLCP
, &response
, q
, p
, o
, asyncmap
, sizeof(asyncmap
));
659 case 3: // Authentication-Protocol
661 int proto
= ntohs(*(uint16_t *)(o
+ 2));
662 char proto_name
[] = "0x0000";
667 if (config
->radius_authtypes
& AUTHPAP
)
669 sess_local
[s
].lcp_authtype
= AUTHPAP
;
673 strcpy(proto_name
, "PAP");
675 else if (proto
== PPPCHAP
)
677 if (config
->radius_authtypes
& AUTHCHAP
678 && *(o
+ 4) == 5) // MD5
680 sess_local
[s
].lcp_authtype
= AUTHCHAP
;
684 strcpy(proto_name
, "CHAP");
687 sprintf(proto_name
, "%#4.4x", proto
);
689 LOG(3, s
, t
, " Remote requesting %s authentication. Rejecting.\n", proto_name
);
691 alen
= add_lcp_auth(authproto
, sizeof(authproto
), config
->radius_authprefer
);
692 if (alen
< 2) break; // paranoia
694 q
= ppp_conf_nak(s
, b
, sizeof(b
), PPPLCP
, &response
, q
, p
, o
, authproto
+ 2, alen
- 2);
695 if (q
&& *response
== ConfigNak
&&
696 config
->radius_authtypes
!= config
->radius_authprefer
)
699 alen
= add_lcp_auth(authproto
, sizeof(authproto
), config
->radius_authtypes
& ~config
->radius_authprefer
);
701 q
= ppp_conf_nak(s
, b
, sizeof(b
), PPPLCP
, &response
, q
, p
, o
, authproto
+ 2, alen
- 2);
708 case 4: // Quality-Protocol
709 case 5: // Magic-Number
710 case 7: // Protocol-Field-Compression
711 case 8: // Address-And-Control-Field-Compression
714 case 17: // Multilink Max-Receive-Reconstructed-Unit
716 uint16_t mrru
= ntohs(*(uint16_t *)(o
+ 2));
717 session
[s
].mrru
= mrru
;
719 LOG(3, s
, t
, " Received PPP LCP option MRRU: %d\n",mrru
);
723 case 18: // Multilink Short Sequence Number Header Format
727 LOG(3, s
, t
, " Received PPP LCP option MSSN format\n");
731 case 19: // Multilink Endpoint Discriminator
733 uint8_t epdis_class
= o
[2];
736 session
[s
].epdis
.addr_class
= epdis_class
;
737 session
[s
].epdis
.length
= length
- 3;
738 if (session
[s
].epdis
.length
> 20)
740 LOG(1, s
, t
, "Error: received EndDis Address Length more than 20: %d\n", session
[s
].epdis
.length
);
741 session
[s
].epdis
.length
= 20;
744 for (addr
= 0; addr
< session
[s
].epdis
.length
; addr
++)
745 session
[s
].epdis
.address
[addr
] = o
[3+addr
];
752 LOG(3, s
, t
, " Received PPP LCP option Multilink EndDis Local Address Class: %d\n",epdis_class
);
755 LOG(3, s
, t
, " Received PPP LCP option Multilink EndDis IP Address Class: %d\n",epdis_class
);
758 LOG(3, s
, t
, " Received PPP LCP option Multilink EndDis IEEE MAC Address Class: %d\n",epdis_class
);
761 LOG(3, s
, t
, " Received PPP LCP option Multilink EndDis PPP Magic No Class: %d\n",epdis_class
);
764 LOG(3, s
, t
, " Received PPP LCP option Multilink EndDis PSND No Class: %d\n",epdis_class
);
767 LOG(3, s
, t
, " Received PPP LCP option Multilink EndDis NULL Class %d\n",epdis_class
);
772 default: // Reject any unknown options
773 LOG(3, s
, t
, " Rejecting unknown PPP LCP option %d\n", type
);
774 q
= ppp_conf_rej(s
, b
, sizeof(b
), PPPLCP
, &response
, q
, p
, o
);
781 cluster_send_session(s
);
785 l
= q
- response
; // LCP packet length
786 *((uint16_t *) (response
+ 2)) = htons(l
); // update header
790 // Send packet back as ConfigAck
791 response
= makeppp(b
, sizeof(b
), p
, l
, s
, t
, PPPLCP
, 0, 0, 0);
792 if (!response
) return;
793 *response
= ConfigAck
;
796 switch (session
[s
].ppp
.lcp
)
799 response
= makeppp(b
, sizeof(b
), p
, 2, s
, t
, PPPLCP
, 0, 0, 0);
800 if (!response
) return;
801 *response
= TerminateAck
;
802 *((uint16_t *) (response
+ 2)) = htons(l
= 4);
806 initialise_restart_count(s
, lcp
);
808 if (*response
== ConfigAck
)
809 change_state(s
, lcp
, AckSent
);
811 change_state(s
, lcp
, RequestSent
);
816 if (*response
== ConfigAck
)
817 change_state(s
, lcp
, AckSent
);
822 if (*response
== ConfigAck
)
833 if (*response
== ConfigAck
)
834 change_state(s
, lcp
, AckSent
);
836 change_state(s
, lcp
, RequestSent
);
841 sessionshutdown(s
, "LCP: ConfigReq in state Closing. This should not happen. Killing session.", CDN_ADMIN_DISC
, TERM_LOST_SERVICE
);
845 LOG(2, s
, t
, "LCP: ignoring %s in state %s\n", ppp_code(*p
), ppp_state(session
[s
].ppp
.lcp
));
849 LOG(3, s
, t
, "LCP: send %s\n", ppp_code(*response
));
850 if (config
->debug
> 3) dumplcp(response
, l
);
852 tunnelsend(b
, l
+ (response
- b
), t
);
854 else if (*p
== ConfigNak
|| *p
== ConfigRej
)
857 uint8_t *o
= (p
+ 4);
865 if (length
== 0 || type
== 0 || x
< length
) break;
868 case 1: // Maximum-Receive-Unit
871 if (length
< 4) break;
872 sess_local
[s
].ppp_mru
= ntohs(*(uint16_t *)(o
+ 2));
873 LOG(3, s
, t
, " Remote requested MRU of %u\n", sess_local
[s
].ppp_mru
);
877 sess_local
[s
].ppp_mru
= 0;
878 LOG(3, s
, t
, " Remote rejected MRU negotiation\n");
883 case 3: // Authentication-Protocol
891 if (length
< 4) break;
892 proto
= ntohs(*(uint16_t *)(o
+ 2));
896 authtype
= config
->radius_authtypes
& AUTHPAP
;
897 LOG(3, s
, t
, " Remote requested PAP authentication...%sing\n",
898 authtype
? "accept" : "reject");
900 else if (proto
== PPPCHAP
&& length
> 4 && *(o
+ 4) == 5)
902 authtype
= config
->radius_authtypes
& AUTHCHAP
;
903 LOG(3, s
, t
, " Remote requested CHAP authentication...%sing\n",
904 authtype
? "accept" : "reject");
908 LOG(3, s
, t
, " Rejecting unsupported authentication %#4x\n",
914 LOG(2, s
, t
, "LCP: remote rejected auth negotiation\n");
915 authtype
= 0; // shutdown
920 case 5: // Magic-Number
921 session
[s
].magic
= 0;
924 if (length
< 6) break;
925 session
[s
].magic
= ntohl(*(uint32_t *)(o
+ 2));
928 if (session
[s
].magic
)
929 LOG(3, s
, t
, " Remote requested magic-no %x\n", session
[s
].magic
);
931 LOG(3, s
, t
, " Remote rejected magic-no\n");
933 cluster_send_session(s
);
936 case 17: // Multilink Max-Receive-Reconstructed-Unit
940 sess_local
[s
].mp_mrru
= ntohs(*(uint16_t *)(o
+ 2));
941 LOG(3, s
, t
, " Remote requested MRRU of %u\n", sess_local
[s
].mp_mrru
);
945 sess_local
[s
].mp_mrru
= 0;
946 LOG(3, s
, t
, " Remote rejected MRRU negotiation\n");
951 case 18: // Multilink Short Sequence Number Header Format
955 sess_local
[s
].mp_mssf
= 0;
956 LOG(3, s
, t
, " Remote requested Naked mssf\n");
960 sess_local
[s
].mp_mssf
= 0;
961 LOG(3, s
, t
, " Remote rejected mssf\n");
966 case 19: // Multilink Endpoint Discriminator
970 LOG(2, s
, t
, " Remote should not configNak Endpoint Dis!\n");
974 sess_local
[s
].mp_epdis
= 0;
975 LOG(3, s
, t
, " Remote rejected Endpoint Discriminator\n");
981 LOG(2, s
, t
, "LCP: remote sent %s for type %u?\n", ppp_code(*p
), type
);
982 sessionshutdown(s
, "Unable to negotiate LCP.", CDN_ADMIN_DISC
, TERM_USER_ERROR
);
991 sessionshutdown(s
, "Unsupported authentication.", CDN_ADMIN_DISC
, TERM_USER_ERROR
);
996 sess_local
[s
].lcp_authtype
= authtype
;
998 switch (session
[s
].ppp
.lcp
)
1003 uint8_t *response
= makeppp(b
, sizeof(b
), p
, 2, s
, t
, PPPLCP
, 0, 0, 0);
1004 if (!response
) return;
1005 *response
= TerminateAck
;
1006 *((uint16_t *) (response
+ 2)) = htons(l
= 4);
1008 LOG(3, s
, t
, "LCP: send %s\n", ppp_code(*response
));
1009 if (config
->debug
> 3) dumplcp(response
, l
);
1011 tunnelsend(b
, l
+ (response
- b
), t
);
1017 initialise_restart_count(s
, lcp
);
1022 LOG(2, s
, t
, "LCP: ConfigNak in state %s? Sending ConfigReq\n", ppp_state(session
[s
].ppp
.lcp
));
1032 LOG(2, s
, t
, "LCP: ignoring %s in state %s\n", ppp_code(*p
), ppp_state(session
[s
].ppp
.lcp
));
1036 else if (*p
== TerminateReq
)
1038 switch (session
[s
].ppp
.lcp
)
1051 zero_restart_count(s
, lcp
);
1052 change_state(s
, lcp
, Closing
);
1056 LOG(2, s
, t
, "LCP: ignoring %s in state %s\n", ppp_code(*p
), ppp_state(session
[s
].ppp
.lcp
));
1060 *p
= TerminateAck
; // send ack
1061 q
= makeppp(b
, sizeof(b
), p
, l
, s
, t
, PPPLCP
, 0, 0, 0);
1064 LOG(3, s
, t
, "LCP: send %s\n", ppp_code(*q
));
1065 if (config
->debug
> 3) dumplcp(q
, l
);
1067 tunnelsend(b
, l
+ (q
- b
), t
); // send it
1069 else if (*p
== ProtocolRej
)
1076 if (l
> 5 && !(proto
& 1))
1083 if (proto
== PPPIPV6CP
)
1085 LOG(3, s
, t
, "IPv6 rejected\n");
1086 change_state(s
, ipv6cp
, Closed
);
1090 LOG(3, s
, t
, "LCP protocol reject: 0x%04X\n", proto
);
1093 else if (*p
== EchoReq
)
1095 *p
= EchoReply
; // reply
1096 *(uint32_t *) (p
+ 4) = htonl(session
[s
].magic
); // our magic number
1097 q
= makeppp(b
, sizeof(b
), p
, l
, s
, t
, PPPLCP
, 0, 0, 0);
1100 LOG(4, s
, t
, "LCP: send %s\n", ppp_code(*q
));
1101 if (config
->debug
> 3) dumplcp(q
, l
);
1103 tunnelsend(b
, l
+ (q
- b
), t
); // send it
1105 else if (*p
== EchoReply
)
1107 // Ignore it, last_packet time is set earlier than this.
1109 else if (*p
!= CodeRej
)
1111 ppp_code_rej(s
, t
, PPPLCP
, "LCP", p
, l
, b
, sizeof(b
));
1115 int join_bundle(sessionidt s
)
1117 // Search for a bundle to join
1120 for (i
= 1; i
< MAXBUNDLE
; i
++)
1122 if (bundle
[i
].state
!= BUNDLEFREE
)
1124 if (epdiscmp(session
[s
].epdis
,bundle
[i
].epdis
) && !strcmp(session
[s
].user
, bundle
[i
].user
))
1126 sessionidt first_ses
= bundle
[i
].members
[0];
1127 if (bundle
[i
].mssf
!= session
[s
].mssf
)
1129 // uniformity of sequence number format must be insured
1130 LOG(3, s
, session
[s
].tunnel
, "MPPP: unable to bundle session %d in bundle %d cause of different mssf\n", s
, i
);
1133 session
[s
].bundle
= i
;
1134 session
[s
].ip
= session
[first_ses
].ip
;
1135 session
[s
].dns1
= session
[first_ses
].dns1
;
1136 session
[s
].dns2
= session
[first_ses
].dns2
;
1137 session
[s
].timeout
= session
[first_ses
].timeout
;
1139 if(session
[s
].epdis
.length
> 0)
1140 setepdis(&bundle
[i
].epdis
, session
[s
].epdis
);
1142 strcpy(bundle
[i
].user
, session
[s
].user
);
1143 bundle
[i
].members
[bundle
[i
].num_of_links
] = s
;
1144 bundle
[i
].num_of_links
++;
1145 LOG(3, s
, session
[s
].tunnel
, "MPPP: Bundling additional line in bundle (%d), lines:%d\n",i
,bundle
[i
].num_of_links
);
1151 // No previously created bundle was found for this session, so create a new one
1152 if (!(b
= new_bundle())) return 0;
1154 session
[s
].bundle
= b
;
1155 bundle
[b
].mrru
= session
[s
].mrru
;
1156 bundle
[b
].mssf
= session
[s
].mssf
;
1157 // FIXME !!! to enable l2tpns reading mssf frames receiver_max_seq, sender_max_seq must be introduce
1158 // 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
1161 bundle[b].max_seq = 1 << 12;
1163 bundle
[b
].max_seq
= 1 << 24;
1164 if(session
[s
].epdis
.length
> 0)
1165 setepdis(&bundle
[b
].epdis
, session
[s
].epdis
);
1167 strcpy(bundle
[b
].user
, session
[s
].user
);
1168 bundle
[b
].members
[0] = s
;
1169 bundle
[b
].timeout
= session
[s
].timeout
;
1170 LOG(3, s
, session
[s
].tunnel
, "MPPP: Created a new bundle (%d)\n", b
);
1174 static int epdiscmp(epdist ep1
, epdist ep2
)
1177 if (ep1
.length
!= ep2
.length
)
1180 if (ep1
.addr_class
!= ep2
.addr_class
)
1183 for (ad
= 0; ad
< ep1
.length
; ad
++)
1184 if (ep1
.address
[ad
] != ep2
.address
[ad
])
1190 static void setepdis(epdist
*ep1
, epdist ep2
)
1193 ep1
->length
= ep2
.length
;
1194 ep1
->addr_class
= ep2
.addr_class
;
1195 for (ad
= 0; ad
< ep2
.length
; ad
++)
1196 ep1
->address
[ad
] = ep2
.address
[ad
];
1199 static bundleidt
new_bundle()
1202 for (i
= 1; i
< MAXBUNDLE
; i
++)
1204 if (bundle
[i
].state
== BUNDLEFREE
)
1206 LOG(4, 0, 0, "MPPP: Assigning bundle ID %d\n", i
);
1207 bundle
[i
].num_of_links
= 1;
1208 bundle
[i
].last_check
= time_now
; // Initialize last_check value
1209 bundle
[i
].state
= BUNDLEOPEN
;
1210 bundle
[i
].current_ses
= -1; // This is to enforce the first session 0 to be used at first
1211 memset(&frag
[i
], 0, sizeof(fragmentationt
));
1212 if (i
> config
->cluster_highest_bundleid
)
1213 config
->cluster_highest_bundleid
= i
;
1217 LOG(0, 0, 0, "MPPP: Can't find a free bundle! There shouldn't be this many in use!\n");
1221 static void ipcp_open(sessionidt s
, tunnelidt t
)
1223 LOG(3, s
, t
, "IPCP: Opened, session is now active\n");
1225 change_state(s
, ipcp
, Opened
);
1227 if (!(session
[s
].walled_garden
|| session
[s
].flags
& SESSION_STARTED
))
1229 uint16_t r
= radiusnew(s
);
1232 radiussend(r
, RADIUSSTART
); // send radius start
1234 // don't send further Start records if IPCP is restarted
1235 session
[s
].flags
|= SESSION_STARTED
;
1236 cluster_send_session(s
);
1240 // start IPv6 if configured and still in passive state
1241 if (session
[s
].ppp
.ipv6cp
== Stopped
)
1244 change_state(s
, ipv6cp
, RequestSent
);
1248 // Process IPCP messages
1249 void processipcp(sessionidt s
, tunnelidt t
, uint8_t *p
, uint16_t l
)
1251 uint8_t b
[MAXETHER
];
1257 LOG_HEX(5, "IPCP", p
, l
);
1260 LOG(1, s
, t
, "Short IPCP %d bytes\n", l
);
1261 STAT(tunnel_rx_errors
);
1265 if ((hl
= ntohs(*(uint16_t *) (p
+ 2))) > l
)
1267 LOG(1, s
, t
, "Length mismatch IPCP %u/%u\n", hl
, l
);
1268 STAT(tunnel_rx_errors
);
1273 if (session
[s
].ppp
.phase
< Network
)
1275 LOG(2, s
, t
, "IPCP %s ignored in %s phase\n", ppp_code(*p
), ppp_phase(session
[s
].ppp
.phase
));
1279 LOG(3, s
, t
, "IPCP: recv %s\n", ppp_code(*p
));
1281 if (*p
== ConfigAck
)
1283 switch (session
[s
].ppp
.ipcp
)
1286 initialise_restart_count(s
, ipcp
);
1287 change_state(s
, ipcp
, AckReceived
);
1292 LOG(2, s
, t
, "IPCP: ConfigAck in state %s? Sending ConfigReq\n", ppp_state(session
[s
].ppp
.ipcp
));
1294 change_state(s
, ipcp
, RequestSent
);
1302 LOG(2, s
, t
, "IPCP: ignoring %s in state %s\n", ppp_code(*p
), ppp_state(session
[s
].ppp
.ipcp
));
1305 else if (*p
== ConfigReq
)
1307 uint8_t *response
= 0;
1315 if (!o
[1] || o
[1] > length
) return;
1319 case 3: // ip address
1320 gotip
++; // seen address
1321 if (o
[1] != 6) return;
1323 addr
= htonl(session
[s
].ip
);
1324 if (memcmp(o
+ 2, &addr
, (sizeof addr
)))
1327 q
= ppp_conf_nak(s
, b
, sizeof(b
), PPPIPCP
, &response
, q
, p
, o
, (uint8_t *) &addr
, sizeof(addr
));
1328 if (!q
|| (q
!= oq
&& *response
== ConfigRej
))
1330 sessionshutdown(s
, "Can't negotiate IPCP.", CDN_ADMIN_DISC
, TERM_USER_ERROR
);
1337 case 129: // primary DNS
1338 if (o
[1] != 6) return;
1340 addr
= htonl(session
[s
].dns1
);
1341 if (memcmp(o
+ 2, &addr
, (sizeof addr
)))
1343 q
= ppp_conf_nak(s
, b
, sizeof(b
), PPPIPCP
, &response
, q
, p
, o
, (uint8_t *) &addr
, sizeof(addr
));
1349 case 131: // secondary DNS
1350 if (o
[1] != 6) return;
1352 addr
= htonl(session
[s
].dns2
);
1353 if (memcmp(o
+ 2, &addr
, sizeof(addr
)))
1355 q
= ppp_conf_nak(s
, b
, sizeof(b
), PPPIPCP
, &response
, q
, p
, o
, (uint8_t *) &addr
, sizeof(addr
));
1362 LOG(2, s
, t
, " Rejecting PPP IPCP Option type %d\n", *o
);
1363 q
= ppp_conf_rej(s
, b
, sizeof(b
), PPPIPCP
, &response
, q
, p
, o
);
1373 l
= q
- response
; // IPCP packet length
1374 *((uint16_t *) (response
+ 2)) = htons(l
); // update header
1378 // Send packet back as ConfigAck
1379 response
= makeppp(b
, sizeof(b
), p
, l
, s
, t
, PPPIPCP
, 0, 0, 0);
1380 if (!response
) return;
1381 *response
= ConfigAck
;
1385 LOG(1, s
, t
, "No IP in IPCP request\n");
1386 STAT(tunnel_rx_errors
);
1390 switch (session
[s
].ppp
.ipcp
)
1393 response
= makeppp(b
, sizeof(b
), p
, 2, s
, t
, PPPIPCP
, 0, 0, 0);
1394 if (!response
) return;
1395 *response
= TerminateAck
;
1396 *((uint16_t *) (response
+ 2)) = htons(l
= 4);
1400 initialise_restart_count(s
, ipcp
);
1402 if (*response
== ConfigAck
)
1403 change_state(s
, ipcp
, AckSent
);
1405 change_state(s
, ipcp
, RequestSent
);
1410 if (*response
== ConfigAck
)
1411 change_state(s
, ipcp
, AckSent
);
1416 if (*response
== ConfigAck
)
1422 initialise_restart_count(s
, ipcp
);
1427 if (*response
== ConfigAck
)
1428 change_state(s
, ipcp
, AckSent
);
1430 change_state(s
, ipcp
, RequestSent
);
1435 LOG(2, s
, t
, "IPCP: ignoring %s in state %s\n", ppp_code(*p
), ppp_state(session
[s
].ppp
.ipcp
));
1439 LOG(3, s
, t
, "IPCP: send %s\n", ppp_code(*response
));
1440 tunnelsend(b
, l
+ (response
- b
), t
);
1442 else if (*p
== TerminateReq
)
1444 switch (session
[s
].ppp
.ipcp
)
1456 zero_restart_count(s
, ipcp
);
1457 change_state(s
, ipcp
, Closing
);
1461 LOG(2, s
, t
, "IPCP: ignoring %s in state %s\n", ppp_code(*p
), ppp_state(session
[s
].ppp
.ipcp
));
1465 *p
= TerminateAck
; // send ack
1466 q
= makeppp(b
, sizeof(b
), p
, l
, s
, t
, PPPIPCP
, 0, 0, 0);
1469 LOG(3, s
, t
, "IPCP: send %s\n", ppp_code(*q
));
1470 tunnelsend(b
, l
+ (q
- b
), t
); // send it
1472 else if (*p
!= CodeRej
)
1474 ppp_code_rej(s
, t
, PPPIPCP
, "IPCP", p
, l
, b
, sizeof(b
));
1478 static void ipv6cp_open(sessionidt s
, tunnelidt t
)
1480 LOG(3, s
, t
, "IPV6CP: Opened\n");
1482 change_state(s
, ipv6cp
, Opened
);
1483 if (session
[s
].ipv6prefixlen
)
1484 route6set(s
, session
[s
].ipv6route
, session
[s
].ipv6prefixlen
, 1);
1486 // Send an initial RA (TODO: Should we send these regularly?)
1487 send_ipv6_ra(s
, t
, NULL
);
1490 // Process IPV6CP messages
1491 void processipv6cp(sessionidt s
, tunnelidt t
, uint8_t *p
, uint16_t l
)
1493 uint8_t b
[MAXETHER
];
1497 CSTAT(processipv6cp
);
1499 LOG_HEX(5, "IPV6CP", p
, l
);
1502 LOG(1, s
, t
, "Short IPV6CP %d bytes\n", l
);
1503 STAT(tunnel_rx_errors
);
1507 if ((hl
= ntohs(*(uint16_t *) (p
+ 2))) > l
)
1509 LOG(1, s
, t
, "Length mismatch IPV6CP %u/%u\n", hl
, l
);
1510 STAT(tunnel_rx_errors
);
1515 if (session
[s
].ppp
.phase
< Network
)
1517 LOG(2, s
, t
, "IPV6CP %s ignored in %s phase\n", ppp_code(*p
), ppp_phase(session
[s
].ppp
.phase
));
1521 LOG(3, s
, t
, "IPV6CP: recv %s\n", ppp_code(*p
));
1525 LOG(3, s
, t
, "IPV6CP: no IPv4 address (IPCP in state %s)\n", ppp_state(session
[s
].ppp
.ipcp
));
1526 return; // need IPCP to complete...
1529 if (*p
== ConfigAck
)
1531 switch (session
[s
].ppp
.ipv6cp
)
1534 initialise_restart_count(s
, ipv6cp
);
1535 change_state(s
, ipv6cp
, AckReceived
);
1540 LOG(2, s
, t
, "IPV6CP: ConfigAck in state %s? Sending ConfigReq\n", ppp_state(session
[s
].ppp
.ipv6cp
));
1542 change_state(s
, ipv6cp
, RequestSent
);
1550 LOG(2, s
, t
, "IPV6CP: ignoring %s in state %s\n", ppp_code(*p
), ppp_state(session
[s
].ppp
.ipv6cp
));
1553 else if (*p
== ConfigReq
)
1555 uint8_t *response
= 0;
1563 if (!o
[1] || o
[1] > length
) return;
1567 case 1: // interface identifier
1568 gotip
++; // seen address
1569 if (o
[1] != 10) return;
1571 ident
[0] = htonl(session
[s
].ip
);
1574 if (memcmp(o
+ 2, ident
, sizeof(ident
)))
1576 q
= ppp_conf_nak(s
, b
, sizeof(b
), PPPIPV6CP
, &response
, q
, p
, o
, (uint8_t *)ident
, sizeof(ident
));
1583 LOG(2, s
, t
, " Rejecting PPP IPV6CP Option type %d\n", *o
);
1584 q
= ppp_conf_rej(s
, b
, sizeof(b
), PPPIPV6CP
, &response
, q
, p
, o
);
1594 l
= q
- response
; // IPV6CP packet length
1595 *((uint16_t *) (response
+ 2)) = htons(l
); // update header
1599 // Send packet back as ConfigAck
1600 response
= makeppp(b
, sizeof(b
), p
, l
, s
, t
, PPPIPV6CP
, 0, 0, 0);
1601 if (!response
) return;
1602 *response
= ConfigAck
;
1606 LOG(1, s
, t
, "No interface identifier in IPV6CP request\n");
1607 STAT(tunnel_rx_errors
);
1611 switch (session
[s
].ppp
.ipv6cp
)
1614 response
= makeppp(b
, sizeof(b
), p
, 2, s
, t
, PPPIPV6CP
, 0, 0, 0);
1615 if (!response
) return;
1616 *response
= TerminateAck
;
1617 *((uint16_t *) (response
+ 2)) = htons(l
= 4);
1621 initialise_restart_count(s
, ipv6cp
);
1623 if (*response
== ConfigAck
)
1624 change_state(s
, ipv6cp
, AckSent
);
1626 change_state(s
, ipv6cp
, RequestSent
);
1631 if (*response
== ConfigAck
)
1632 change_state(s
, ipv6cp
, AckSent
);
1637 if (*response
== ConfigAck
)
1643 initialise_restart_count(s
, ipv6cp
);
1648 if (*response
== ConfigAck
)
1649 change_state(s
, ipv6cp
, AckSent
);
1651 change_state(s
, ipv6cp
, RequestSent
);
1656 LOG(2, s
, t
, "IPV6CP: ignoring %s in state %s\n", ppp_code(*p
), ppp_state(session
[s
].ppp
.ipv6cp
));
1660 LOG(3, s
, t
, "IPV6CP: send %s\n", ppp_code(*response
));
1661 tunnelsend(b
, l
+ (response
- b
), t
);
1663 else if (*p
== TerminateReq
)
1665 switch (session
[s
].ppp
.ipv6cp
)
1677 zero_restart_count(s
, ipv6cp
);
1678 change_state(s
, ipv6cp
, Closing
);
1682 LOG(2, s
, t
, "IPV6CP: ignoring %s in state %s\n", ppp_code(*p
), ppp_state(session
[s
].ppp
.ipv6cp
));
1686 *p
= TerminateAck
; // send ack
1687 q
= makeppp(b
, sizeof(b
), p
, l
, s
, t
, PPPIPV6CP
, 0, 0, 0);
1690 LOG(3, s
, t
, "IPV6CP: send %s\n", ppp_code(*q
));
1691 tunnelsend(b
, l
+ (q
- b
), t
); // send it
1693 else if (*p
!= CodeRej
)
1695 ppp_code_rej(s
, t
, PPPIPV6CP
, "IPV6CP", p
, l
, b
, sizeof(b
));
1699 static void update_sessions_in_stat(sessionidt s
, uint16_t l
)
1701 bundleidt b
= session
[s
].bundle
;
1704 increment_counter(&session
[s
].cin
, &session
[s
].cin_wrap
, l
);
1705 session
[s
].cin_delta
+= l
;
1708 sess_local
[s
].cin
+= l
;
1709 sess_local
[s
].pin
++;
1713 int i
= frag
[b
].re_frame_begin_index
;
1714 int end
= frag
[b
].re_frame_end_index
;
1717 l
= frag
[b
].fragment
[i
].length
;
1718 s
= frag
[b
].fragment
[i
].sid
;
1719 increment_counter(&session
[s
].cin
, &session
[s
].cin_wrap
, l
);
1720 session
[s
].cin_delta
+= l
;
1723 sess_local
[s
].cin
+= l
;
1724 sess_local
[s
].pin
++;
1727 i
= (i
+ 1) & MAXFRAGNUM_MASK
;
1732 // process IP packet received
1734 // This MUST be called with at least 4 byte behind 'p'.
1735 // (i.e. this routine writes to p[-4]).
1736 void processipin(sessionidt s
, tunnelidt t
, uint8_t *p
, uint16_t l
)
1742 LOG_HEX(5, "IP", p
, l
);
1746 LOG(1, s
, t
, "IP packet too short %d\n", l
);
1747 STAT(tunnel_rx_errors
);
1751 ip
= ntohl(*(uint32_t *)(p
+ 12));
1755 LOG(1, s
, t
, "IP packet too long %d\n", l
);
1756 STAT(tunnel_rx_errors
);
1760 if (session
[s
].ppp
.phase
!= Network
|| session
[s
].ppp
.ipcp
!= Opened
)
1763 if (!session
[s
].bundle
|| bundle
[session
[s
].bundle
].num_of_links
< 2) // FIXME:
1765 // no spoof (do sessionbyip to handled statically routed subnets)
1766 if (!config
->disable_no_spoof
&& ip
!= session
[s
].ip
&& sessionbyip(htonl(ip
)) != s
)
1768 LOG(4, s
, t
, "Dropping packet with spoofed IP %s\n", fmtaddr(htonl(ip
), 0));
1773 // run access-list if any
1774 if (session
[s
].filter_in
&& !ip_filter(p
, l
, session
[s
].filter_in
- 1))
1777 // adjust MSS on SYN and SYN,ACK packets with options
1778 if ((ntohs(*(uint16_t *) (p
+ 6)) & 0x1fff) == 0 && p
[9] == IPPROTO_TCP
) // first tcp fragment
1780 int ihl
= (p
[0] & 0xf) * 4; // length of IP header
1781 if (l
>= ihl
+ 20 && (p
[ihl
+ 13] & TCP_FLAG_SYN
) && ((p
[ihl
+ 12] >> 4) > 5))
1782 adjust_tcp_mss(s
, t
, p
, l
, p
+ ihl
);
1785 // Add on the tun header
1787 *(uint32_t *) p
= htonl(PKTIP
);
1790 if (session
[s
].tbf_in
)
1792 // Are we throttling this session?
1793 if (config
->cluster_iam_master
)
1794 tbf_queue_packet(session
[s
].tbf_in
, p
, l
);
1796 master_throttle_packet(session
[s
].tbf_in
, p
, l
);
1801 if (tun_write(p
, l
) < 0)
1803 STAT(tun_tx_errors
);
1804 LOG(0, s
, t
, "Error writing %d bytes to TUN device: %s (tunfd=%d, p=%p)\n",
1805 l
, strerror(errno
), tunfd
, p
);
1813 if (session
[s
].snoop_ip
&& session
[s
].snoop_port
)
1815 // Snooping this session
1816 snoop_send_packet(p
, l
, session
[s
].snoop_ip
, session
[s
].snoop_port
);
1819 update_sessions_in_stat(s
, l
);
1823 STAT(tun_tx_packets
);
1824 INC_STAT(tun_tx_bytes
, l
);
1827 // process Multilink PPP packet received
1828 void processmpin(sessionidt s
, tunnelidt t
, uint8_t *p
, uint16_t l
)
1830 bundleidt b
= session
[s
].bundle
;
1831 bundlet
* this_bundle
= &bundle
[b
];
1832 uint32_t maskSeq
, max_seq
;
1834 uint16_t frag_index
, frag_index_next
, frag_index_prev
;
1835 fragmentationt
*this_fragmentation
= &frag
[b
];
1836 uint8_t begin_frame
= (*p
& MP_BEGIN
);
1837 uint8_t end_frame
= (*p
& MP_END
);
1838 uint32_t seq_num
, seq_num_next
, seq_num_prev
;
1841 uint16_t begin_index
, end_index
;
1843 // Perform length checking
1846 LOG(2, s
, t
, "MPPP: discarding fragment larger than MAXFRAGLEN\n");
1852 LOG(2, s
, t
, "MPPP: Invalid bundle id: 0\n");
1855 // FIXME !! session[s].mssf means that the receiver wants to receive frames in mssf not means the receiver will send frames in mssf
1856 /* if(session[s].mssf)
1858 // Get 12 bit for seq number
1859 seq_num = ntohs((*(uint16_t *) p) & 0xFF0F);
1862 // After this point the pointer should be advanced 2 bytes
1863 LOG(3, s, t, "MPPP: 12 bits, sequence number: %d\n",seq_num);
1867 // Get 24 bit for seq number
1868 seq_num
= ntohl((*(uint32_t *) p
) & 0xFFFFFF00);
1871 // After this point the pointer should be advanced 4 bytes
1872 LOG(4, s
, t
, "MPPP: 24 bits sequence number:%d\n",seq_num
);
1875 max_seq
= this_bundle
->max_seq
;
1876 maskSeq
= max_seq
- 1;
1879 * Expand sequence number to 32 bits, making it as close
1880 * as possible to this_fragmentation->M.
1882 seq_num
|= this_fragmentation
->M
& ~maskSeq
;
1883 if ((int)(this_fragmentation
->M
- seq_num
) > (int)(maskSeq
>> 1))
1885 seq_num
+= maskSeq
+ 1;
1887 else if ((int)(seq_num
- this_fragmentation
->M
) > (int)(maskSeq
>> 1))
1889 seq_num
-= maskSeq
+ 1; /* should never happen */
1892 // calculate this fragment's offset from the begin seq in the bundle
1893 frag_offset
= (int) (seq_num
- this_fragmentation
->start_seq
);
1895 sess_local
[s
].last_seq
= seq_num
;
1897 // calculate the jitter average
1898 uint32_t ljitter
= time_now_ms
- sess_local
[s
].prev_time
;
1901 sess_local
[s
].jitteravg
= (sess_local
[s
].jitteravg
+ ljitter
)>>1;
1902 sess_local
[s
].prev_time
= time_now_ms
;
1907 if (seq_num
< this_fragmentation
->M
)
1910 this_fragmentation
->M
= seq_num
;
1914 Mmin
= sess_local
[(this_bundle
->members
[0])].last_seq
;
1915 for (i
= 1; i
< this_bundle
->num_of_links
; i
++)
1917 uint32_t s_seq
= sess_local
[(this_bundle
->members
[i
])].last_seq
;
1921 this_fragmentation
->M
= Mmin
;
1924 // calculate M offset of the M seq in the bundle
1925 int M_offset
= (int) (Mmin
- this_fragmentation
->start_seq
);
1927 if (M_offset
>= MAXFRAGNUM
)
1929 // There have a long break of the link !!!!!!!!
1930 // M_offset is bigger that the fragmentation buffer size
1931 LOG(3, s
, t
, "MPPP: M_offset out of range, min:%d, begin_seq:%d\n", Mmin
, this_fragmentation
->start_seq
);
1933 // Calculate the new start index, the previous frag are lost
1934 begin_index
= (M_offset
+ this_fragmentation
->start_index
) & MAXFRAGNUM_MASK
;
1936 // Set new Start sequence
1937 this_fragmentation
->start_index
= begin_index
;
1938 this_fragmentation
->start_seq
= Mmin
;
1940 // recalculate the fragment offset from the new begin seq in the bundle
1941 frag_offset
= (int) (seq_num
- Mmin
);
1944 // discard this fragment if the packet comes before the start sequence
1945 if (frag_offset
< 0)
1947 // this packet comes before the next
1948 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
);
1952 // discard if frag_offset is bigger that the fragmentation buffer size
1953 if (frag_offset
>= MAXFRAGNUM
)
1955 // frag_offset is bigger that the fragmentation buffer size
1956 LOG(3, s
, t
, "MPPP: Index out of range, seq:%d, begin_seq:%d\n", seq_num
, this_fragmentation
->start_seq
);
1960 //caculate received fragment's index in the fragment array
1961 frag_index
= (frag_offset
+ this_fragmentation
->start_index
) & MAXFRAGNUM_MASK
;
1963 // insert the frame in it's place
1964 fragmentt
*this_frag
= &this_fragmentation
->fragment
[frag_index
];
1966 if (this_frag
->length
> 0)
1967 // This fragment is lost, It was around the buffer and it was never completed the packet.
1968 LOG(3, this_frag
->sid
, this_frag
->tid
, "MPPP: (INSERT) seq_num:%d frag_index:%d flags:%02X is LOST\n",
1969 this_frag
->seq
, frag_index
, this_frag
->flags
);
1971 this_frag
->length
= l
;
1974 this_frag
->flags
= flags
;
1975 this_frag
->seq
= seq_num
;
1976 this_frag
->jitteravg
= sess_local
[s
].jitteravg
;
1977 memcpy(this_frag
->data
, p
, l
);
1979 LOG(4, s
, t
, "MPPP: seq_num:%d frag_index:%d INSERTED flags: %02X\n", seq_num
, frag_index
, flags
);
1982 frag_index_next
= (frag_index
+ 1) & MAXFRAGNUM_MASK
;
1983 //previous frag index
1984 frag_index_prev
= (frag_index
- 1) & MAXFRAGNUM_MASK
;
1986 seq_num_next
= seq_num
+ 1;
1988 seq_num_prev
= seq_num
- 1;
1990 // Clean the buffer and log the lost fragments
1991 if ((frag_index_next
!= this_fragmentation
->start_index
) && this_fragmentation
->fragment
[frag_index_next
].length
)
1993 // check if the next frag is a lost fragment
1994 if (this_fragmentation
->fragment
[frag_index_next
].seq
!= seq_num_next
)
1996 // This fragment is lost, It was around the buffer and it was never completed the packet.
1997 LOG(3, this_fragmentation
->fragment
[frag_index_next
].sid
, this_fragmentation
->fragment
[frag_index_next
].tid
,
1998 "MPPP: (NEXT) seq_num:%d frag_index:%d flags:%02X is LOST\n",
1999 this_fragmentation
->fragment
[frag_index_next
].seq
, frag_index_next
,
2000 this_fragmentation
->fragment
[frag_index_next
].flags
);
2001 // this frag is lost
2002 this_fragmentation
->fragment
[frag_index_next
].length
= 0;
2003 this_fragmentation
->fragment
[frag_index_next
].flags
= 0;
2005 if (begin_frame
&& (!end_frame
)) return; // assembling frame failed
2009 // Clean the buffer and log the lost fragments
2010 if ((frag_index
!= this_fragmentation
->start_index
) && this_fragmentation
->fragment
[frag_index_prev
].length
)
2012 // check if the next frag is a lost fragment
2013 if (this_fragmentation
->fragment
[frag_index_prev
].seq
!= seq_num_prev
)
2015 // This fragment is lost, It was around the buffer and it was never completed the packet.
2016 LOG(3, this_fragmentation
->fragment
[frag_index_prev
].sid
, this_fragmentation
->fragment
[frag_index_prev
].tid
,
2017 "MPPP: (PREV) seq_num:%d frag_index:%d flags:%02X is LOST\n",
2018 this_fragmentation
->fragment
[frag_index_prev
].seq
, frag_index_prev
,
2019 this_fragmentation
->fragment
[frag_index_prev
].flags
);
2021 this_fragmentation
->fragment
[frag_index_prev
].length
= 0;
2022 this_fragmentation
->fragment
[frag_index_prev
].flags
= 0;
2024 if (end_frame
&& (!begin_frame
)) return; // assembling frame failed
2029 begin_index
= this_fragmentation
->start_index
;
2030 uint32_t b_seq
= this_fragmentation
->start_seq
;
2031 // Try to find a Begin sequence from the start_seq sequence to M sequence
2032 while (b_seq
< Mmin
)
2034 if (this_fragmentation
->fragment
[begin_index
].length
)
2036 if (b_seq
== this_fragmentation
->fragment
[begin_index
].seq
)
2038 if (this_fragmentation
->fragment
[begin_index
].flags
& MP_BEGIN
)
2041 // Adjust the new start sequence and start index
2042 this_fragmentation
->start_index
= begin_index
;
2043 this_fragmentation
->start_seq
= b_seq
;
2044 // Begin Sequence found, now try to found the End Sequence to complete the frame
2045 end_index
= begin_index
;
2046 while (b_seq
< Mmin
)
2048 if (this_fragmentation
->fragment
[end_index
].length
)
2050 if (b_seq
== this_fragmentation
->fragment
[end_index
].seq
)
2052 if (this_fragmentation
->fragment
[end_index
].flags
& MP_END
)
2054 // The End sequence was found and the frame is complete
2061 // This fragment is lost, it was never completed the packet.
2062 LOG(3, this_fragmentation
->fragment
[end_index
].sid
, this_fragmentation
->fragment
[end_index
].tid
,
2063 "MPPP: (FIND END) seq_num:%d frag_index:%d flags:%02X is LOST\n",
2064 this_fragmentation
->fragment
[end_index
].seq
, begin_index
,
2065 this_fragmentation
->fragment
[end_index
].flags
);
2066 // this frag is lost
2067 this_fragmentation
->fragment
[end_index
].length
= 0;
2068 this_fragmentation
->fragment
[end_index
].flags
= 0;
2069 // This frame is not complete find the next Begin
2075 // This frame is not complete find the next Begin if exist
2078 end_index
= (end_index
+1) & MAXFRAGNUM_MASK
;
2083 // The End sequence was found and the frame is complete
2086 // find the next Begin
2087 begin_index
= end_index
;
2092 // This fragment is lost, it was never completed the packet.
2093 LOG(3, this_fragmentation
->fragment
[begin_index
].sid
, this_fragmentation
->fragment
[begin_index
].tid
,
2094 "MPPP: (FIND BEGIN) seq_num:%d frag_index:%d flags:%02X is LOST\n",
2095 this_fragmentation
->fragment
[begin_index
].seq
, begin_index
,
2096 this_fragmentation
->fragment
[begin_index
].flags
);
2097 // this frag is lost
2098 this_fragmentation
->fragment
[begin_index
].length
= 0;
2099 this_fragmentation
->fragment
[begin_index
].flags
= 0;
2102 begin_index
= (begin_index
+1) & MAXFRAGNUM_MASK
;
2107 // try to assemble the frame that has the received fragment as a member
2108 // get the beginning of this frame
2109 begin_index
= end_index
= this_fragmentation
->start_index
;
2110 if (this_fragmentation
->fragment
[begin_index
].length
)
2112 if (!(this_fragmentation
->fragment
[begin_index
].flags
& MP_BEGIN
))
2114 LOG(3, this_fragmentation
->fragment
[begin_index
].sid
, this_fragmentation
->fragment
[begin_index
].tid
,
2115 "MPPP: (NOT BEGIN) seq_num:%d frag_index:%d flags:%02X\n",
2116 this_fragmentation
->fragment
[begin_index
].seq
, begin_index
,
2117 this_fragmentation
->fragment
[begin_index
].flags
);
2118 // should occur only after an "M_Offset out of range"
2119 // The start sequence must be a begin sequence
2120 this_fragmentation
->start_index
= (begin_index
+1) & MAXFRAGNUM_MASK
;
2121 this_fragmentation
->start_seq
++;
2122 return; // assembling frame failed
2126 return; // assembling frame failed
2128 // get the end of his frame
2129 while (this_fragmentation
->fragment
[end_index
].length
)
2131 if (this_fragmentation
->fragment
[end_index
].flags
& MP_END
)
2134 end_index
= (end_index
+1) & MAXFRAGNUM_MASK
;
2136 if (end_index
== this_fragmentation
->start_index
)
2137 return; // assembling frame failed
2140 // return if a lost fragment is found
2141 if (!(this_fragmentation
->fragment
[end_index
].length
))
2142 return; // assembling frame failed
2144 // assemble the packet
2145 //assemble frame, process it, reset fragmentation
2146 uint16_t cur_len
= 4; // This is set to 4 to leave 4 bytes for function processipin
2148 LOG(4, s
, t
, "MPPP: processing fragments from %d to %d\n", begin_index
, end_index
);
2149 // Push to the receive buffer
2151 for (i
= begin_index
;; i
= (i
+ 1) & MAXFRAGNUM_MASK
)
2153 this_frag
= &this_fragmentation
->fragment
[i
];
2154 if(cur_len
+ this_frag
->length
> MAXETHER
)
2156 LOG(2, s
, t
, "MPPP: discarding reassembled frames larger than MAXETHER\n");
2160 memcpy(this_fragmentation
->reassembled_frame
+cur_len
, this_frag
->data
, this_frag
->length
);
2161 LOG(5, s
, t
, "MPPP: processing frame at %d, with len %d\n", i
, this_frag
->length
);
2163 cur_len
+= this_frag
->length
;
2166 this_fragmentation
->re_frame_len
= cur_len
;
2167 this_fragmentation
->re_frame_begin_index
= begin_index
;
2168 this_fragmentation
->re_frame_end_index
= end_index
;
2169 // Process the resassembled frame
2170 LOG(5, s
, t
, "MPPP: Process the reassembled frame, len=%d\n",cur_len
);
2171 processmpframe(s
, t
, this_fragmentation
->reassembled_frame
, this_fragmentation
->re_frame_len
, 1);
2176 // Set reassembled frame length to zero after processing it
2177 this_fragmentation
->re_frame_len
= 0;
2178 for (i
= begin_index
;; i
= (i
+ 1) & MAXFRAGNUM_MASK
)
2180 this_fragmentation
->fragment
[i
].length
= 0; // Indicates that this fragment has been consumed
2181 this_fragmentation
->fragment
[i
].flags
= 0;
2186 // Set the new start_index and start_seq
2187 this_fragmentation
->start_index
= (end_index
+ 1) & MAXFRAGNUM_MASK
;
2188 this_fragmentation
->start_seq
= this_fragmentation
->fragment
[end_index
].seq
+ 1;
2189 LOG(4, s
, t
, "MPPP after assembling: start index is = %d, start seq=%d\n", this_fragmentation
->start_index
, this_fragmentation
->start_seq
);
2191 begin_index
= this_fragmentation
->start_index
;
2192 if ((this_fragmentation
->fragment
[begin_index
].length
) &&
2193 (this_fragmentation
->fragment
[begin_index
].seq
!= this_fragmentation
->start_seq
))
2195 LOG(3, this_fragmentation
->fragment
[begin_index
].sid
, this_fragmentation
->fragment
[begin_index
].tid
,
2196 "MPPP: (START) seq_num:%d frag_index:%d flags:%02X is LOST\n",
2197 this_fragmentation
->fragment
[begin_index
].seq
, begin_index
,
2198 this_fragmentation
->fragment
[begin_index
].flags
);
2199 this_fragmentation
->fragment
[begin_index
].length
= 0;
2200 this_fragmentation
->fragment
[begin_index
].flags
= 0;
2203 if (this_fragmentation
->start_seq
<= Mmin
)
2204 // It's possible to find other complete frame or lost frame.
2206 else if ((this_fragmentation
->fragment
[begin_index
].length
) &&
2207 (this_fragmentation
->fragment
[begin_index
].flags
& MP_BEGIN
))
2208 // may be that the next frame is completed
2209 goto assembling_frame
;
2214 // process IPv6 packet received
2216 // This MUST be called with at least 4 byte behind 'p'.
2217 // (i.e. this routine writes to p[-4]).
2218 void processipv6in(sessionidt s
, tunnelidt t
, uint8_t *p
, uint16_t l
)
2223 CSTAT(processipv6in
);
2225 LOG_HEX(5, "IPv6", p
, l
);
2227 ip
= *(struct in6_addr
*) (p
+ 8);
2228 ipv4
= ntohl(*(uint32_t *)(p
+ 16));
2232 LOG(1, s
, t
, "IP packet too long %d\n", l
);
2233 STAT(tunnel_rx_errors
);
2237 if (session
[s
].ppp
.phase
!= Network
|| session
[s
].ppp
.ipv6cp
!= Opened
)
2241 if ((ipv4
!= session
[s
].ip
|| memcmp(&config
->ipv6_prefix
, &ip
, 8)) && sessionbyipv6(ip
) != s
)
2243 char str
[INET6_ADDRSTRLEN
];
2244 LOG(5, s
, t
, "Dropping packet with spoofed IP %s\n",
2245 inet_ntop(AF_INET6
, &ip
, str
, INET6_ADDRSTRLEN
));
2249 // Check if it's a Router Solicition message.
2250 if (*(p
+ 6) == 58 && *(p
+ 7) == 255 && *(p
+ 24) == 0xFF && *(p
+ 25) == 2 &&
2251 *(uint32_t *)(p
+ 26) == 0 && *(uint32_t *)(p
+ 30) == 0 &&
2252 *(uint32_t *)(p
+ 34) == 0 &&
2253 *(p
+ 38) == 0 && *(p
+ 39) == 2 && *(p
+ 40) == 133) {
2254 LOG(3, s
, t
, "Got IPv6 RS\n");
2255 send_ipv6_ra(s
, t
, &ip
);
2259 // Add on the tun header
2261 *(uint32_t *) p
= htonl(PKTIPV6
);
2264 // Are we throttled and a slave?
2265 if (session
[s
].tbf_in
&& !config
->cluster_iam_master
) {
2266 // Pass it to the master for handling.
2267 master_throttle_packet(session
[s
].tbf_in
, p
, l
);
2271 // Are we throttled and a master??
2272 if (session
[s
].tbf_in
&& config
->cluster_iam_master
) {
2273 // Actually handle the throttled packets.
2274 tbf_queue_packet(session
[s
].tbf_in
, p
, l
);
2279 if (tun_write(p
, l
) < 0)
2281 STAT(tun_tx_errors
);
2282 LOG(0, s
, t
, "Error writing %d bytes to TUN device: %s (tunfd=%d, p=%p)\n",
2283 l
, strerror(errno
), tunfd
, p
);
2291 if (session
[s
].snoop_ip
&& session
[s
].snoop_port
)
2293 // Snooping this session
2294 snoop_send_packet(p
, l
, session
[s
].snoop_ip
, session
[s
].snoop_port
);
2297 update_sessions_in_stat(s
, l
);
2301 STAT(tun_tx_packets
);
2302 INC_STAT(tun_tx_bytes
, l
);
2306 // Helper routine for the TBF filters.
2307 // Used to send queued data in from the user.
2309 void send_ipin(sessionidt s
, uint8_t *buf
, int len
)
2311 LOG_HEX(5, "IP in throttled", buf
, len
);
2313 if (write(tunfd
, buf
, len
) < 0)
2315 STAT(tun_tx_errors
);
2316 LOG(0, 0, 0, "Error writing %d bytes to TUN device: %s (tunfd=%d, p=%p)\n",
2317 len
, strerror(errno
), tunfd
, buf
);
2325 if (session
[s
].snoop_ip
&& session
[s
].snoop_port
)
2327 // Snooping this session
2328 snoop_send_packet(buf
, len
, session
[s
].snoop_ip
, session
[s
].snoop_port
);
2331 // Increment packet counters
2332 increment_counter(&session
[s
].cin
, &session
[s
].cin_wrap
, len
);
2333 session
[s
].cin_delta
+= len
;
2336 sess_local
[s
].cin
+= len
;
2337 sess_local
[s
].pin
++;
2341 STAT(tun_tx_packets
);
2342 INC_STAT(tun_tx_bytes
, len
- 4);
2346 // Process CCP messages
2347 void processccp(sessionidt s
, tunnelidt t
, uint8_t *p
, uint16_t l
)
2349 uint8_t b
[MAXETHER
];
2354 LOG_HEX(5, "CCP", p
, l
);
2356 if (session
[s
].ppp
.phase
< Network
)
2358 LOG(2, s
, t
, "CCP %s ignored in %s phase\n", ppp_code(*p
), ppp_phase(session
[s
].ppp
.phase
));
2364 LOG(1, s
, t
, "Short CCP packet\n");
2365 STAT(tunnel_rx_errors
);
2368 LOG(4, s
, t
, "CCP: recv %s\n", ppp_code(*p
));
2369 if (*p
== ConfigAck
)
2371 switch (session
[s
].ppp
.ccp
)
2374 initialise_restart_count(s
, ccp
);
2375 change_state(s
, ccp
, AckReceived
);
2380 LOG(2, s
, t
, "CCP: ConfigAck in state %s? Sending ConfigReq\n", ppp_state(session
[s
].ppp
.ccp
));
2382 change_state(s
, ccp
, RequestSent
);
2386 LOG(3, s
, t
, "CCP: Opened\n");
2387 change_state(s
, ccp
, Opened
);
2391 LOG(2, s
, t
, "CCP: ignoring %s in state %s\n", ppp_code(*p
), ppp_state(session
[s
].ppp
.ccp
));
2394 else if (*p
== ConfigReq
)
2396 if (l
< 6) // accept no compression
2398 else // compression requested--reject
2401 q
= makeppp(b
, sizeof(b
), p
, l
, s
, t
, PPPCCP
, 0, 0, 0);
2404 switch (session
[s
].ppp
.ccp
)
2407 q
= makeppp(b
, sizeof(b
), p
, 2, s
, t
, PPPCCP
, 0, 0, 0);
2410 *((uint16_t *) (q
+ 2)) = htons(l
= 4);
2414 initialise_restart_count(s
, ccp
);
2416 if (*q
== ConfigAck
)
2417 change_state(s
, ccp
, AckSent
);
2419 change_state(s
, ccp
, RequestSent
);
2424 if (*q
== ConfigAck
)
2425 change_state(s
, ccp
, AckSent
);
2430 if (*q
== ConfigAck
)
2431 change_state(s
, ccp
, Opened
);
2436 initialise_restart_count(s
, ccp
);
2441 if (*q
== ConfigAck
)
2442 change_state(s
, ccp
, AckSent
);
2444 change_state(s
, ccp
, RequestSent
);
2449 LOG(2, s
, t
, "CCP: ignoring %s in state %s\n", ppp_code(*p
), ppp_state(session
[s
].ppp
.ccp
));
2453 LOG(4, s
, t
, "CCP: send %s\n", ppp_code(*q
));
2454 tunnelsend(b
, l
+ (q
- b
), t
);
2456 else if (*p
== TerminateReq
)
2459 q
= makeppp(b
, sizeof(b
), p
, l
, s
, t
, PPPCCP
, 0, 0, 0);
2461 LOG(3, s
, t
, "CCP: send %s\n", ppp_code(*q
));
2462 tunnelsend(b
, l
+ (q
- b
), t
);
2463 change_state(s
, ccp
, Stopped
);
2465 else if (*p
!= CodeRej
)
2467 ppp_code_rej(s
, t
, PPPCCP
, "CCP", p
, l
, b
, sizeof(b
));
2471 // send a CHAP challenge
2472 void sendchap(sessionidt s
, tunnelidt t
)
2474 uint8_t b
[MAXETHER
];
2483 LOG(1, s
, t
, "No RADIUS to send challenge\n");
2484 STAT(tunnel_tx_errors
);
2488 LOG(1, s
, t
, "Send CHAP challenge\n");
2490 radius
[r
].chap
= 1; // CHAP not PAP
2492 if (radius
[r
].state
!= RADIUSCHAP
)
2495 radius
[r
].state
= RADIUSCHAP
;
2496 radius
[r
].retry
= backoff(radius
[r
].try++);
2497 if (radius
[r
].try > 5)
2499 sessionshutdown(s
, "CHAP timeout.", CDN_ADMIN_DISC
, TERM_REAUTHENTICATION_FAILURE
);
2500 STAT(tunnel_tx_errors
);
2503 q
= makeppp(b
, sizeof(b
), 0, 0, s
, t
, PPPCHAP
, 0, 0, 0);
2506 *q
= 1; // challenge
2507 q
[1] = radius
[r
].id
; // ID
2508 q
[4] = 16; // value size (size of challenge)
2509 memcpy(q
+ 5, radius
[r
].auth
, 16); // challenge
2510 strcpy((char *) q
+ 21, hostname
); // our name
2511 *(uint16_t *) (q
+ 2) = htons(strlen(hostname
) + 21); // length
2512 tunnelsend(b
, strlen(hostname
) + 21 + (q
- b
), t
); // send it
2515 // fill in a L2TP message with a PPP frame,
2516 // returns start of PPP frame
2517 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
)
2519 uint16_t hdr
= 0x0002; // L2TP with no options
2520 uint16_t type
= mtype
;
2523 if (t
== TUNNEL_ID_PPPOE
)
2525 return pppoe_makeppp(b
, size
, p
, l
, s
, t
, mtype
, prio
, bid
, mp_bits
);
2528 if (size
< 16) // Need more space than this!!
2530 LOG(0, s
, t
, "makeppp buffer too small for L2TP header (size=%d)\n", size
);
2534 if (prio
) hdr
|= 0x0100; // set priority bit
2536 *(uint16_t *) (b
+ 0) = htons(hdr
);
2537 *(uint16_t *) (b
+ 2) = htons(tunnel
[t
].far
); // tunnel
2538 *(uint16_t *) (b
+ 4) = htons(session
[s
].far
); // session
2541 // Check whether this session is part of multilink
2544 if (bundle
[bid
].num_of_links
> 1)
2545 type
= PPPMP
; // Change PPP message type to the PPPMP
2550 if (type
== PPPLCP
|| !(session
[s
].flags
& SESSION_ACFC
))
2552 *(uint16_t *) b
= htons(0xFF03); // HDLC header
2556 if (type
< 0x100 && session
[s
].flags
& SESSION_PFC
)
2562 *(uint16_t *) b
= htons(type
);
2568 // Set the sequence number and (B)egin (E)nd flags
2569 if (session
[s
].mssf
)
2571 // Set the multilink bits
2572 uint16_t bits_send
= mp_bits
;
2573 *(uint16_t *) b
= htons((bundle
[bid
].seq_num_t
& 0x0FFF)|bits_send
);
2578 *(uint32_t *) b
= htonl(bundle
[bid
].seq_num_t
);
2579 // Set the multilink bits
2584 bundle
[bid
].seq_num_t
++;
2586 // Add the message type if this fragment has the begin bit set
2587 if (mp_bits
& MP_BEGIN
)
2589 //*b++ = mtype; // The next two lines are instead of this
2590 *(uint16_t *) b
= htons(mtype
); // Message type
2595 if ((b
- start
) + l
> size
)
2597 LOG(2, s
, t
, "makeppp would overflow buffer (size=%d, header+payload=%td)\n", size
, (b
- start
) + l
);
2608 // fill in a L2TP message with a PPP frame,
2609 // returns start of PPP frame
2610 //(note: THIS ROUTINE CAN WRITES TO p[-28]).
2611 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
)
2613 uint16_t hdr
= 0x0002; // L2TP with no options
2614 uint16_t type
= mtype
;
2617 if (t
== TUNNEL_ID_PPPOE
)
2619 return opt_pppoe_makeppp(p
, l
, s
, t
, mtype
, prio
, bid
, mp_bits
);
2622 // Check whether this session is part of multilink
2625 if (bundle
[bid
].num_of_links
> 1)
2626 type
= PPPMP
; // Change PPP message type to the PPPMP
2633 // Add the message type if this fragment has the begin bit set
2634 if (mp_bits
& MP_BEGIN
)
2637 *(uint16_t *) b
= htons(mtype
); // Message type
2640 // Set the sequence number and (B)egin (E)nd flags
2641 if (session
[s
].mssf
)
2643 // Set the multilink bits
2644 uint16_t bits_send
= mp_bits
;
2646 *(uint16_t *) b
= htons((bundle
[bid
].seq_num_t
& 0x0FFF)|bits_send
);
2651 *(uint32_t *) b
= htonl(bundle
[bid
].seq_num_t
);
2652 // Set the multilink bits
2656 bundle
[bid
].seq_num_t
++;
2659 if (type
< 0x100 && session
[s
].flags
& SESSION_PFC
)
2667 *(uint16_t *) b
= htons(type
);
2670 if (type
== PPPLCP
|| !(session
[s
].flags
& SESSION_ACFC
))
2673 *(uint16_t *) b
= htons(0xFF03); // HDLC header
2676 if (prio
) hdr
|= 0x0100; // set priority bit
2678 *(uint16_t *) b
= htons(session
[s
].far
); // session
2680 *(uint16_t *) b
= htons(tunnel
[t
].far
); // tunnel
2682 *(uint16_t *) b
= htons(hdr
);
2687 static int add_lcp_auth(uint8_t *b
, int size
, int authtype
)
2690 if ((authtype
== AUTHCHAP
&& size
< 5) || size
< 4)
2693 *b
++ = 3; // Authentication-Protocol
2694 if (authtype
== AUTHCHAP
)
2696 len
= *b
++ = 5; // length
2697 *(uint16_t *) b
= htons(PPPCHAP
); b
+= 2;
2700 else if (authtype
== AUTHPAP
)
2702 len
= *b
++ = 4; // length
2703 *(uint16_t *) b
= htons(PPPPAP
); b
+= 2;
2707 LOG(0, 0, 0, "add_lcp_auth called with unsupported auth type %d\n", authtype
);
2713 // Send LCP ConfigReq for MRU, authentication type and magic no
2714 void sendlcp(sessionidt s
, tunnelidt t
)
2716 uint8_t b
[500], *q
, *l
;
2717 int authtype
= sess_local
[s
].lcp_authtype
;
2719 if (!(q
= makeppp(b
, sizeof(b
), NULL
, 0, s
, t
, PPPLCP
, 0, 0, 0)))
2722 LOG(3, s
, t
, "LCP: send ConfigReq%s%s%s including MP options\n",
2723 authtype
? " (" : "",
2724 authtype
? (authtype
== AUTHCHAP
? "CHAP" : "PAP") : "",
2725 authtype
? ")" : "");
2729 *l
++ = ++sess_local
[s
].lcp_ident
; // ID
2731 l
+= 2; //Save space for length
2733 if (sess_local
[s
].ppp_mru
)
2735 *l
++ = 1; *l
++ = 4; // Maximum-Receive-Unit (length 4)
2736 *(uint16_t *) l
= htons(sess_local
[s
].ppp_mru
); l
+= 2;
2740 l
+= add_lcp_auth(l
, sizeof(b
) - (l
- b
), authtype
);
2742 if (session
[s
].magic
)
2744 *l
++ = 5; *l
++ = 6; // Magic-Number (length 6)
2745 *(uint32_t *) l
= htonl(session
[s
].magic
);
2749 if (sess_local
[s
].mp_mrru
)
2751 *l
++ = 17; *l
++ = 4; // Multilink Max-Receive-Reconstructed-Unit (length 4)
2752 *(uint16_t *) l
= htons(sess_local
[s
].mp_mrru
); l
+= 2;
2755 if (sess_local
[s
].mp_epdis
)
2757 *l
++ = 19; *l
++ = 7; // Multilink Endpoint Discriminator (length 7)
2758 *l
++ = IPADDR
; // Endpoint Discriminator class
2759 *(uint32_t *) l
= htonl(sess_local
[s
].mp_epdis
);
2763 *(uint16_t *)(q
+ 2) = htons(l
- q
); // Length
2765 LOG_HEX(5, "PPPLCP", q
, l
- q
);
2766 if (config
->debug
> 3) dumplcp(q
, l
- q
);
2768 tunnelsend(b
, (l
- b
), t
);
2769 restart_timer(s
, lcp
);
2772 // Send CCP request for no compression
2773 void sendccp(sessionidt s
, tunnelidt t
)
2777 if (!(q
= makeppp(b
, sizeof(b
), NULL
, 0, s
, t
, PPPCCP
, 0, 0, 0)))
2780 LOG(3, s
, t
, "CCP: send ConfigReq (no compression)\n");
2783 *(q
+ 1) = ++sess_local
[s
].lcp_ident
; // ID
2784 *(uint16_t *)(q
+ 2) = htons(4); // Length
2786 LOG_HEX(5, "PPPCCP", q
, 4);
2787 tunnelsend(b
, (q
- b
) + 4 , t
);
2788 restart_timer(s
, ccp
);
2791 // Reject unknown/unconfigured protocols
2792 void protoreject(sessionidt s
, tunnelidt t
, uint8_t *p
, uint16_t l
, uint16_t proto
)
2795 uint8_t buf
[MAXETHER
];
2797 int mru
= session
[s
].mru
;
2798 if (mru
< MINMTU
) mru
= MINMTU
;
2799 if (mru
> sizeof(buf
)) mru
= sizeof(buf
);
2802 if (l
> mru
) l
= mru
;
2804 q
= makeppp(buf
, sizeof(buf
), 0, 0, s
, t
, PPPLCP
, 0, 0, 0);
2808 *(q
+ 1) = ++sess_local
[s
].lcp_ident
;
2809 *(uint16_t *)(q
+ 2) = htons(l
);
2810 *(uint16_t *)(q
+ 4) = htons(proto
);
2811 memcpy(q
+ 6, p
, l
- 6);
2813 if (proto
== PPPIPV6CP
)
2814 LOG(3, s
, t
, "LCP: send ProtocolRej (IPV6CP: not configured)\n");
2816 LOG(2, s
, t
, "LCP: sent ProtocolRej (0x%04X: unsupported)\n", proto
);
2818 tunnelsend(buf
, l
+ (q
- buf
), t
);