8 #include <netinet/ip6.h>
11 #include "constants.h"
20 extern tunnelt
*tunnel
;
21 extern bundlet
*bundle
;
22 extern fragmentationt
*frag
;
23 extern sessiont
*session
;
24 extern radiust
*radius
;
26 extern char hostname
[];
27 extern uint32_t eth_tx
;
28 extern time_t time_now
;
29 extern uint64_t time_now_ms
;
30 extern configt
*config
;
32 static int add_lcp_auth(uint8_t *b
, int size
, int authtype
);
33 static bundleidt
new_bundle(void);
34 static int epdiscmp(epdist
, epdist
);
35 static void setepdis(epdist
*, epdist
);
36 static void ipcp_open(sessionidt s
, tunnelidt t
);
38 static int first_session_in_bundle(sessionidt s
)
41 for (i
= 1; i
< MAXBUNDLE
; i
++)
42 if (bundle
[i
].state
!= BUNDLEFREE
)
43 if (epdiscmp(session
[s
].epdis
,bundle
[i
].epdis
) && !strcmp(session
[s
].user
, bundle
[i
].user
))
48 // Process PAP messages
49 void processpap(sessionidt s
, tunnelidt t
, uint8_t *p
, uint16_t l
)
58 LOG_HEX(5, "PAP", p
, l
);
61 LOG(1, s
, t
, "Short PAP %u bytes\n", l
);
62 STAT(tunnel_rx_errors
);
63 sessionshutdown(s
, "Short PAP packet.", CDN_ADMIN_DISC
, TERM_USER_ERROR
);
67 if ((hl
= ntohs(*(uint16_t *) (p
+ 2))) > l
)
69 LOG(1, s
, t
, "Length mismatch PAP %u/%u\n", hl
, l
);
70 STAT(tunnel_rx_errors
);
71 sessionshutdown(s
, "PAP length mismatch.", CDN_ADMIN_DISC
, TERM_USER_ERROR
);
78 LOG(1, s
, t
, "Unexpected PAP code %d\n", *p
);
79 STAT(tunnel_rx_errors
);
80 sessionshutdown(s
, "Unexpected PAP code.", CDN_ADMIN_DISC
, TERM_USER_ERROR
);
84 if (session
[s
].ppp
.phase
!= Authenticate
)
86 LOG(2, s
, t
, "PAP ignored in %s phase\n", ppp_phase(session
[s
].ppp
.phase
));
93 user
[0] = pass
[0] = 0;
94 if (*b
&& *b
< sizeof(user
))
96 memcpy(user
, b
+ 1, *b
);
99 if (*b
&& *b
< sizeof(pass
))
101 memcpy(pass
, b
+ 1, *b
);
105 LOG(3, s
, t
, "PAP login %s/%s\n", user
, pass
);
108 if ((!config
->disable_lac_func
) && lac_conf_forwardtoremotelns(s
, user
))
110 // Creating a tunnel/session has been started
114 if (session
[s
].ip
|| !(r
= radiusnew(s
)))
116 // respond now, either no RADIUS available or already authenticated
119 uint8_t *p
= makeppp(b
, sizeof(b
), 0, 0, s
, t
, PPPPAP
, 0, 0, 0);
125 *p
= 3; // cant authorise
127 *(uint16_t *) (p
+ 2) = htons(5); // length
128 p
[4] = 0; // no message
129 tunnelsend(b
, 5 + (p
- b
), t
); // send it
133 LOG(3, s
, t
, "Already an IP allocated: %s (%d)\n",
134 fmtaddr(htonl(session
[s
].ip
), 0), session
[s
].ip_pool_index
);
138 LOG(1, s
, t
, "No RADIUS session available to authenticate session...\n");
139 sessionshutdown(s
, "No free RADIUS sessions.", CDN_UNAVAILABLE
, TERM_SERVICE_UNAVAILABLE
);
144 // Run PRE_AUTH plugins
145 struct param_pre_auth packet
= { &tunnel
[t
], &session
[s
], strdup(user
), strdup(pass
), PPPPAP
, 1 };
146 run_plugins(PLUGIN_PRE_AUTH
, &packet
);
147 if (!packet
.continue_auth
)
149 LOG(3, s
, t
, "A plugin rejected PRE_AUTH\n");
150 if (packet
.username
) free(packet
.username
);
151 if (packet
.password
) free(packet
.password
);
155 strncpy(session
[s
].user
, packet
.username
, sizeof(session
[s
].user
) - 1);
156 strncpy(radius
[r
].pass
, packet
.password
, sizeof(radius
[r
].pass
) - 1);
158 free(packet
.username
);
159 free(packet
.password
);
162 LOG(3, s
, t
, "Sending login for %s/%s to RADIUS\n", user
, pass
);
163 if ((session
[s
].mrru
) && (!first_session_in_bundle(s
)))
164 radiussend(r
, RADIUSJUSTAUTH
);
166 radiussend(r
, RADIUSAUTH
);
170 // Process CHAP messages
171 void processchap(sessionidt s
, tunnelidt t
, uint8_t *p
, uint16_t l
)
178 LOG_HEX(5, "CHAP", p
, l
);
182 LOG(1, s
, t
, "Short CHAP %u bytes\n", l
);
183 STAT(tunnel_rx_errors
);
184 sessionshutdown(s
, "Short CHAP packet.", CDN_ADMIN_DISC
, TERM_USER_ERROR
);
188 if ((hl
= ntohs(*(uint16_t *) (p
+ 2))) > l
)
190 LOG(1, s
, t
, "Length mismatch CHAP %u/%u\n", hl
, l
);
191 STAT(tunnel_rx_errors
);
192 sessionshutdown(s
, "CHAP length mismatch.", CDN_ADMIN_DISC
, TERM_USER_ERROR
);
199 LOG(1, s
, t
, "Unexpected CHAP response code %d\n", *p
);
200 STAT(tunnel_rx_errors
);
201 sessionshutdown(s
, "CHAP length mismatch.", CDN_ADMIN_DISC
, TERM_USER_ERROR
);
205 if (session
[s
].ppp
.phase
!= Authenticate
)
207 LOG(2, s
, t
, "CHAP ignored in %s phase\n", ppp_phase(session
[s
].ppp
.phase
));
211 r
= sess_local
[s
].radius
;
214 LOG(3, s
, t
, "Unexpected CHAP message\n");
216 // Some modems (Netgear DM602, possibly others) persist in using CHAP even
217 // after ACKing our ConfigReq for PAP.
218 if (sess_local
[s
].lcp_authtype
== AUTHPAP
&& config
->radius_authtypes
& AUTHCHAP
)
220 sess_local
[s
].lcp_authtype
= AUTHCHAP
;
226 if (p
[1] != radius
[r
].id
)
228 LOG(1, s
, t
, "Wrong CHAP response ID %d (should be %d) (%d)\n", p
[1], radius
[r
].id
, r
);
229 STAT(tunnel_rx_errors
);
230 sessionshutdown(s
, "Unexpected CHAP response ID.", CDN_ADMIN_DISC
, TERM_USER_ERROR
);
234 if (l
< 5 || p
[4] != 16)
236 LOG(1, s
, t
, "Bad CHAP response length %d\n", l
< 5 ? -1 : p
[4]);
237 STAT(tunnel_rx_errors
);
238 sessionshutdown(s
, "Bad CHAP response length.", CDN_ADMIN_DISC
, TERM_USER_ERROR
);
244 if (l
< 16 || l
- 16 >= sizeof(session
[s
].user
))
246 LOG(1, s
, t
, "CHAP user too long %d\n", l
- 16);
247 STAT(tunnel_rx_errors
);
248 sessionshutdown(s
, "CHAP username too long.", CDN_ADMIN_DISC
, TERM_USER_ERROR
);
252 // Run PRE_AUTH plugins
254 struct param_pre_auth packet
= { &tunnel
[t
], &session
[s
], NULL
, NULL
, PPPCHAP
, 1 };
256 packet
.password
= calloc(17, 1);
257 memcpy(packet
.password
, p
, 16);
262 packet
.username
= calloc(l
+ 1, 1);
263 memcpy(packet
.username
, p
, l
);
265 if ((!config
->disable_lac_func
) && lac_conf_forwardtoremotelns(s
, packet
.username
))
267 free(packet
.username
);
268 free(packet
.password
);
269 // Creating a tunnel/session has been started
273 run_plugins(PLUGIN_PRE_AUTH
, &packet
);
274 if (!packet
.continue_auth
)
276 LOG(3, s
, t
, "A plugin rejected PRE_AUTH\n");
277 if (packet
.username
) free(packet
.username
);
278 if (packet
.password
) free(packet
.password
);
282 strncpy(session
[s
].user
, packet
.username
, sizeof(session
[s
].user
) - 1);
283 memcpy(radius
[r
].pass
, packet
.password
, 16);
285 free(packet
.username
);
286 free(packet
.password
);
290 LOG(3, s
, t
, "CHAP login %s\n", session
[s
].user
);
291 if ((session
[s
].mrru
) && (!first_session_in_bundle(s
)))
292 radiussend(r
, RADIUSJUSTAUTH
);
294 radiussend(r
, RADIUSAUTH
);
297 static void dumplcp(uint8_t *p
, int l
)
300 uint8_t *o
= (p
+ 4);
302 LOG_HEX(5, "PPP LCP Packet", p
, l
);
303 LOG(4, 0, 0, "PPP LCP Packet type %d (%s len %d)\n", *p
, ppp_code((int)*p
), ntohs( ((uint16_t *) p
)[1]) );
304 LOG(4, 0, 0, "Length: %d\n", l
);
305 if (*p
!= ConfigReq
&& *p
!= ConfigRej
&& *p
!= ConfigAck
)
314 LOG(4, 0, 0, " Option length is %d...\n", length
);
319 LOG(4, 0, 0, " Option type is 0...\n");
326 case 1: // Maximum-Receive-Unit
328 LOG(4, 0, 0, " %s %d\n", ppp_lcp_option(type
), ntohs(*(uint16_t *)(o
+ 2)));
330 LOG(4, 0, 0, " %s odd length %d\n", ppp_lcp_option(type
), length
);
332 case 2: // Async-Control-Character-Map
335 uint32_t asyncmap
= ntohl(*(uint32_t *)(o
+ 2));
336 LOG(4, 0, 0, " %s %x\n", ppp_lcp_option(type
), asyncmap
);
339 LOG(4, 0, 0, " %s odd length %d\n", ppp_lcp_option(type
), length
);
341 case 3: // Authentication-Protocol
344 int proto
= ntohs(*(uint16_t *)(o
+ 2));
345 LOG(4, 0, 0, " %s 0x%x (%s)\n", ppp_lcp_option(type
), proto
,
346 proto
== PPPPAP
? "PAP" : "UNSUPPORTED");
348 else if (length
== 5)
350 int proto
= ntohs(*(uint16_t *)(o
+ 2));
352 LOG(4, 0, 0, " %s 0x%x 0x%x (%s)\n", ppp_lcp_option(type
), proto
, algo
,
353 (proto
== PPPCHAP
&& algo
== 5) ? "CHAP MD5" : "UNSUPPORTED");
356 LOG(4, 0, 0, " %s odd length %d\n", ppp_lcp_option(type
), length
);
358 case 4: // Quality-Protocol
360 uint32_t qp
= ntohl(*(uint32_t *)(o
+ 2));
361 LOG(4, 0, 0, " %s %x\n", ppp_lcp_option(type
), qp
);
364 case 5: // Magic-Number
367 uint32_t magicno
= ntohl(*(uint32_t *)(o
+ 2));
368 LOG(4, 0, 0, " %s %x\n", ppp_lcp_option(type
), magicno
);
371 LOG(4, 0, 0, " %s odd length %d\n", ppp_lcp_option(type
), length
);
373 case 7: // Protocol-Field-Compression
374 case 8: // Address-And-Control-Field-Compression
375 LOG(4, 0, 0, " %s\n", ppp_lcp_option(type
));
378 LOG(2, 0, 0, " Unknown PPP LCP Option type %d\n", type
);
386 void lcp_open(sessionidt s
, tunnelidt t
)
388 // transition to Authentication or Network phase:
389 session
[s
].ppp
.phase
= sess_local
[s
].lcp_authtype
? Authenticate
: Network
;
391 LOG(3, s
, t
, "LCP: Opened, phase %s\n", ppp_phase(session
[s
].ppp
.phase
));
394 change_state(s
, lcp
, Opened
);
396 if (session
[s
].ppp
.phase
== Authenticate
)
398 if (sess_local
[s
].lcp_authtype
== AUTHCHAP
)
403 if(session
[s
].bundle
== 0 || bundle
[session
[s
].bundle
].num_of_links
== 1)
407 change_state(s
, ipcp
, RequestSent
);
408 // move to passive state for IPv6 (if configured), CCP
409 if (config
->ipv6_prefix
.s6_addr
[0])
410 change_state(s
, ipv6cp
, Stopped
);
412 change_state(s
, ipv6cp
, Closed
);
414 change_state(s
, ccp
, Stopped
);
418 sessionidt first_ses
= bundle
[session
[s
].bundle
].members
[0];
419 LOG(3, s
, t
, "MPPP: Skipping IPCP negotiation for session:%d, first session of bundle is:%d\n",s
,first_ses
);
425 void lcp_restart(sessionidt s
)
427 session
[s
].ppp
.phase
= Establish
;
429 change_state(s
, ipcp
, Dead
);
430 change_state(s
, ipv6cp
, Dead
);
431 change_state(s
, ccp
, Dead
);
434 static uint8_t *ppp_conf_rej(sessionidt s
, uint8_t *buf
, size_t blen
, uint16_t mtype
,
435 uint8_t **response
, uint8_t *queued
, uint8_t *packet
, uint8_t *option
)
437 if (!*response
|| **response
!= ConfigRej
)
439 queued
= *response
= makeppp(buf
, blen
, packet
, 2, s
, session
[s
].tunnel
, mtype
, 0, 0, 0);
447 if ((queued
- buf
+ option
[1]) > blen
)
449 LOG(2, s
, session
[s
].tunnel
, "PPP overflow for ConfigRej (proto %u, option %u).\n", mtype
, *option
);
453 memcpy(queued
, option
, option
[1]);
454 return queued
+ option
[1];
457 static uint8_t *ppp_conf_nak(sessionidt s
, uint8_t *buf
, size_t blen
, uint16_t mtype
,
458 uint8_t **response
, uint8_t *queued
, uint8_t *packet
, uint8_t *option
,
459 uint8_t *value
, size_t vlen
)
464 case PPPLCP
: nak_sent
= &sess_local
[s
].lcp
.nak_sent
; break;
465 case PPPIPCP
: nak_sent
= &sess_local
[s
].ipcp
.nak_sent
; break;
466 case PPPIPV6CP
: nak_sent
= &sess_local
[s
].ipv6cp
.nak_sent
; break;
467 default: return 0; // ?
470 if (*response
&& **response
!= ConfigNak
)
472 if (*nak_sent
< config
->ppp_max_failure
) // reject queued
475 return ppp_conf_rej(s
, buf
, blen
, mtype
, response
, 0, packet
, option
);
480 if (*nak_sent
>= config
->ppp_max_failure
)
481 return ppp_conf_rej(s
, buf
, blen
, mtype
, response
, 0, packet
, option
);
483 queued
= *response
= makeppp(buf
, blen
, packet
, 2, s
, session
[s
].tunnel
, mtype
, 0, 0, 0);
492 if ((queued
- buf
+ vlen
+ 2) > blen
)
494 LOG(2, s
, session
[s
].tunnel
, "PPP overflow for ConfigNak (proto %u, option %u).\n", mtype
, *option
);
499 *queued
++ = vlen
+ 2;
500 memcpy(queued
, value
, vlen
);
501 return queued
+ vlen
;
504 static void ppp_code_rej(sessionidt s
, tunnelidt t
, uint16_t proto
,
505 char *pname
, uint8_t *p
, uint16_t l
, uint8_t *buf
, size_t size
)
508 int mru
= session
[s
].mru
;
509 if (mru
< MINMTU
) mru
= MINMTU
;
510 if (mru
> size
) mru
= size
;
513 if (l
> mru
) l
= mru
;
515 q
= makeppp(buf
, size
, 0, 0, s
, t
, proto
, 0, 0, 0);
519 *(q
+ 1) = ++sess_local
[s
].lcp_ident
;
520 *(uint16_t *)(q
+ 2) = htons(l
);
521 memcpy(q
+ 4, p
, l
- 4);
523 LOG(2, s
, t
, "Unexpected %s code %s\n", pname
, ppp_code(*p
));
524 LOG(3, s
, t
, "%s: send %s\n", pname
, ppp_code(*q
));
525 if (config
->debug
> 3) dumplcp(q
, l
);
527 tunnelsend(buf
, l
+ (q
- buf
), t
);
530 // Process LCP messages
531 void processlcp(sessionidt s
, tunnelidt t
, uint8_t *p
, uint16_t l
)
539 LOG_HEX(5, "LCP", p
, l
);
542 LOG(1, s
, t
, "Short LCP %d bytes\n", l
);
543 STAT(tunnel_rx_errors
);
547 if ((hl
= ntohs(*(uint16_t *) (p
+ 2))) > l
)
549 LOG(1, s
, t
, "Length mismatch LCP %u/%u\n", hl
, l
);
550 STAT(tunnel_rx_errors
);
555 if (session
[s
].die
) // going down...
558 LOG(((*p
== EchoReq
|| *p
== EchoReply
) ? 4 : 3), s
, t
,
559 "LCP: recv %s\n", ppp_code(*p
));
561 if (config
->debug
> 3) dumplcp(p
, l
);
566 uint8_t *o
= (p
+ 4);
574 if (length
== 0 || type
== 0 || x
< length
) break;
577 case 3: // Authentication-Protocol
579 int proto
= ntohs(*(uint16_t *)(o
+ 2));
582 else if (proto
== PPPCHAP
&& *(o
+ 4) == 5)
592 if (!session
[s
].ip
&& authtype
)
593 sess_local
[s
].lcp_authtype
= authtype
;
595 switch (session
[s
].ppp
.lcp
)
598 initialise_restart_count(s
, lcp
);
599 change_state(s
, lcp
, AckReceived
);
604 LOG(2, s
, t
, "LCP: ConfigAck in state %s? Sending ConfigReq\n", ppp_state(session
[s
].ppp
.lcp
));
605 if (session
[s
].ppp
.lcp
== Opened
)
609 change_state(s
, lcp
, RequestSent
);
617 LOG(2, s
, t
, "LCP: ignoring %s in state %s\n", ppp_code(*p
), ppp_state(session
[s
].ppp
.lcp
));
620 else if (*p
== ConfigReq
)
623 uint8_t *o
= (p
+ 4);
624 uint8_t *response
= 0;
625 static uint8_t asyncmap
[4] = { 0, 0, 0, 0 }; // all zero
626 static uint8_t authproto
[5];
634 if (length
== 0 || type
== 0 || x
< length
) break;
637 case 1: // Maximum-Receive-Unit
639 uint16_t mru
= ntohs(*(uint16_t *)(o
+ 2));
642 session
[s
].mru
= mru
;
647 LOG(3, s
, t
, " Remote requesting MRU of %u. Rejecting.\n", mru
);
649 q
= ppp_conf_nak(s
, b
, sizeof(b
), PPPLCP
, &response
, q
, p
, o
, (uint8_t *) &mru
, sizeof(mru
));
653 case 2: // Async-Control-Character-Map
654 if (!ntohl(*(uint32_t *)(o
+ 2))) // all bits zero is OK
657 LOG(3, s
, t
, " Remote requesting asyncmap. Rejecting.\n");
658 q
= ppp_conf_nak(s
, b
, sizeof(b
), PPPLCP
, &response
, q
, p
, o
, asyncmap
, sizeof(asyncmap
));
661 case 3: // Authentication-Protocol
663 int proto
= ntohs(*(uint16_t *)(o
+ 2));
664 char proto_name
[] = "0x0000";
669 if (config
->radius_authtypes
& AUTHPAP
)
671 sess_local
[s
].lcp_authtype
= AUTHPAP
;
675 strcpy(proto_name
, "PAP");
677 else if (proto
== PPPCHAP
)
679 if (config
->radius_authtypes
& AUTHCHAP
680 && *(o
+ 4) == 5) // MD5
682 sess_local
[s
].lcp_authtype
= AUTHCHAP
;
686 strcpy(proto_name
, "CHAP");
689 sprintf(proto_name
, "%#4.4x", proto
);
691 LOG(3, s
, t
, " Remote requesting %s authentication. Rejecting.\n", proto_name
);
693 alen
= add_lcp_auth(authproto
, sizeof(authproto
), config
->radius_authprefer
);
694 if (alen
< 2) break; // paranoia
696 q
= ppp_conf_nak(s
, b
, sizeof(b
), PPPLCP
, &response
, q
, p
, o
, authproto
+ 2, alen
- 2);
697 if (q
&& *response
== ConfigNak
&&
698 config
->radius_authtypes
!= config
->radius_authprefer
)
701 alen
= add_lcp_auth(authproto
, sizeof(authproto
), config
->radius_authtypes
& ~config
->radius_authprefer
);
703 q
= ppp_conf_nak(s
, b
, sizeof(b
), PPPLCP
, &response
, q
, p
, o
, authproto
+ 2, alen
- 2);
710 case 4: // Quality-Protocol
711 case 5: // Magic-Number
712 case 7: // Protocol-Field-Compression
713 case 8: // Address-And-Control-Field-Compression
716 case 17: // Multilink Max-Receive-Reconstructed-Unit
718 uint16_t mrru
= ntohs(*(uint16_t *)(o
+ 2));
719 session
[s
].mrru
= mrru
;
721 LOG(3, s
, t
, " Received PPP LCP option MRRU: %d\n",mrru
);
725 case 18: // Multilink Short Sequence Number Header Format
729 LOG(3, s
, t
, " Received PPP LCP option MSSN format\n");
733 case 19: // Multilink Endpoint Discriminator
735 uint8_t epdis_class
= o
[2];
738 session
[s
].epdis
.addr_class
= epdis_class
;
739 session
[s
].epdis
.length
= length
- 3;
740 if (session
[s
].epdis
.length
> 20)
742 LOG(1, s
, t
, "Error: received EndDis Address Length more than 20: %d\n", session
[s
].epdis
.length
);
743 session
[s
].epdis
.length
= 20;
746 for (addr
= 0; addr
< session
[s
].epdis
.length
; addr
++)
747 session
[s
].epdis
.address
[addr
] = o
[3+addr
];
754 LOG(3, s
, t
, " Received PPP LCP option Multilink EndDis Local Address Class: %d\n",epdis_class
);
757 LOG(3, s
, t
, " Received PPP LCP option Multilink EndDis IP Address Class: %d\n",epdis_class
);
760 LOG(3, s
, t
, " Received PPP LCP option Multilink EndDis IEEE MAC Address Class: %d\n",epdis_class
);
763 LOG(3, s
, t
, " Received PPP LCP option Multilink EndDis PPP Magic No Class: %d\n",epdis_class
);
766 LOG(3, s
, t
, " Received PPP LCP option Multilink EndDis PSND No Class: %d\n",epdis_class
);
769 LOG(3, s
, t
, " Received PPP LCP option Multilink EndDis NULL Class %d\n",epdis_class
);
774 default: // Reject any unknown options
775 LOG(3, s
, t
, " Rejecting unknown PPP LCP option %d\n", type
);
776 q
= ppp_conf_rej(s
, b
, sizeof(b
), PPPLCP
, &response
, q
, p
, o
);
783 cluster_send_session(s
);
787 l
= q
- response
; // LCP packet length
788 *((uint16_t *) (response
+ 2)) = htons(l
); // update header
792 // Send packet back as ConfigAck
793 response
= makeppp(b
, sizeof(b
), p
, l
, s
, t
, PPPLCP
, 0, 0, 0);
794 if (!response
) return;
795 *response
= ConfigAck
;
798 switch (session
[s
].ppp
.lcp
)
801 response
= makeppp(b
, sizeof(b
), p
, 2, s
, t
, PPPLCP
, 0, 0, 0);
802 if (!response
) return;
803 *response
= TerminateAck
;
804 *((uint16_t *) (response
+ 2)) = htons(l
= 4);
808 initialise_restart_count(s
, lcp
);
810 if (*response
== ConfigAck
)
811 change_state(s
, lcp
, AckSent
);
813 change_state(s
, lcp
, RequestSent
);
818 if (*response
== ConfigAck
)
819 change_state(s
, lcp
, AckSent
);
824 if (*response
== ConfigAck
)
835 if (*response
== ConfigAck
)
836 change_state(s
, lcp
, AckSent
);
838 change_state(s
, lcp
, RequestSent
);
843 sessionshutdown(s
, "LCP: ConfigReq in state Closing. This should not happen. Killing session.", CDN_ADMIN_DISC
, TERM_LOST_SERVICE
);
847 LOG(2, s
, t
, "LCP: ignoring %s in state %s\n", ppp_code(*p
), ppp_state(session
[s
].ppp
.lcp
));
851 LOG(3, s
, t
, "LCP: send %s\n", ppp_code(*response
));
852 if (config
->debug
> 3) dumplcp(response
, l
);
854 tunnelsend(b
, l
+ (response
- b
), t
);
856 else if (*p
== ConfigNak
|| *p
== ConfigRej
)
859 uint8_t *o
= (p
+ 4);
867 if (length
== 0 || type
== 0 || x
< length
) break;
870 case 1: // Maximum-Receive-Unit
873 if (length
< 4) break;
874 sess_local
[s
].ppp_mru
= ntohs(*(uint16_t *)(o
+ 2));
875 LOG(3, s
, t
, " Remote requested MRU of %u\n", sess_local
[s
].ppp_mru
);
879 sess_local
[s
].ppp_mru
= 0;
880 LOG(3, s
, t
, " Remote rejected MRU negotiation\n");
885 case 3: // Authentication-Protocol
893 if (length
< 4) break;
894 proto
= ntohs(*(uint16_t *)(o
+ 2));
898 authtype
= config
->radius_authtypes
& AUTHPAP
;
899 LOG(3, s
, t
, " Remote requested PAP authentication...%sing\n",
900 authtype
? "accept" : "reject");
902 else if (proto
== PPPCHAP
&& length
> 4 && *(o
+ 4) == 5)
904 authtype
= config
->radius_authtypes
& AUTHCHAP
;
905 LOG(3, s
, t
, " Remote requested CHAP authentication...%sing\n",
906 authtype
? "accept" : "reject");
910 LOG(3, s
, t
, " Rejecting unsupported authentication %#4x\n",
916 LOG(2, s
, t
, "LCP: remote rejected auth negotiation\n");
917 authtype
= 0; // shutdown
922 case 5: // Magic-Number
923 session
[s
].magic
= 0;
926 if (length
< 6) break;
927 session
[s
].magic
= ntohl(*(uint32_t *)(o
+ 2));
930 if (session
[s
].magic
)
931 LOG(3, s
, t
, " Remote requested magic-no %x\n", session
[s
].magic
);
933 LOG(3, s
, t
, " Remote rejected magic-no\n");
935 cluster_send_session(s
);
938 case 17: // Multilink Max-Receive-Reconstructed-Unit
942 sess_local
[s
].mp_mrru
= ntohs(*(uint16_t *)(o
+ 2));
943 LOG(3, s
, t
, " Remote requested MRRU of %u\n", sess_local
[s
].mp_mrru
);
947 sess_local
[s
].mp_mrru
= 0;
948 LOG(3, s
, t
, " Remote rejected MRRU negotiation\n");
953 case 18: // Multilink Short Sequence Number Header Format
957 sess_local
[s
].mp_mssf
= 0;
958 LOG(3, s
, t
, " Remote requested Naked mssf\n");
962 sess_local
[s
].mp_mssf
= 0;
963 LOG(3, s
, t
, " Remote rejected mssf\n");
968 case 19: // Multilink Endpoint Discriminator
972 LOG(2, s
, t
, " Remote should not configNak Endpoint Dis!\n");
976 sess_local
[s
].mp_epdis
= 0;
977 LOG(3, s
, t
, " Remote rejected Endpoint Discriminator\n");
983 LOG(2, s
, t
, "LCP: remote sent %s for type %u?\n", ppp_code(*p
), type
);
984 sessionshutdown(s
, "Unable to negotiate LCP.", CDN_ADMIN_DISC
, TERM_USER_ERROR
);
993 sessionshutdown(s
, "Unsupported authentication.", CDN_ADMIN_DISC
, TERM_USER_ERROR
);
998 sess_local
[s
].lcp_authtype
= authtype
;
1000 switch (session
[s
].ppp
.lcp
)
1005 uint8_t *response
= makeppp(b
, sizeof(b
), p
, 2, s
, t
, PPPLCP
, 0, 0, 0);
1006 if (!response
) return;
1007 *response
= TerminateAck
;
1008 *((uint16_t *) (response
+ 2)) = htons(l
= 4);
1010 LOG(3, s
, t
, "LCP: send %s\n", ppp_code(*response
));
1011 if (config
->debug
> 3) dumplcp(response
, l
);
1013 tunnelsend(b
, l
+ (response
- b
), t
);
1019 initialise_restart_count(s
, lcp
);
1024 LOG(2, s
, t
, "LCP: ConfigNak in state %s? Sending ConfigReq\n", ppp_state(session
[s
].ppp
.lcp
));
1034 LOG(2, s
, t
, "LCP: ignoring %s in state %s\n", ppp_code(*p
), ppp_state(session
[s
].ppp
.lcp
));
1038 else if (*p
== TerminateReq
)
1040 switch (session
[s
].ppp
.lcp
)
1053 zero_restart_count(s
, lcp
);
1054 change_state(s
, lcp
, Closing
);
1058 LOG(2, s
, t
, "LCP: ignoring %s in state %s\n", ppp_code(*p
), ppp_state(session
[s
].ppp
.lcp
));
1062 *p
= TerminateAck
; // send ack
1063 q
= makeppp(b
, sizeof(b
), p
, l
, s
, t
, PPPLCP
, 0, 0, 0);
1066 LOG(3, s
, t
, "LCP: send %s\n", ppp_code(*q
));
1067 if (config
->debug
> 3) dumplcp(q
, l
);
1069 tunnelsend(b
, l
+ (q
- b
), t
); // send it
1071 else if (*p
== ProtocolRej
)
1078 if (l
> 5 && !(proto
& 1))
1085 if (proto
== PPPIPV6CP
)
1087 LOG(3, s
, t
, "IPv6 rejected\n");
1088 change_state(s
, ipv6cp
, Closed
);
1092 LOG(3, s
, t
, "LCP protocol reject: 0x%04X\n", proto
);
1095 else if (*p
== EchoReq
)
1097 *p
= EchoReply
; // reply
1098 *(uint32_t *) (p
+ 4) = htonl(session
[s
].magic
); // our magic number
1099 q
= makeppp(b
, sizeof(b
), p
, l
, s
, t
, PPPLCP
, 0, 0, 0);
1102 LOG(4, s
, t
, "LCP: send %s\n", ppp_code(*q
));
1103 if (config
->debug
> 3) dumplcp(q
, l
);
1105 tunnelsend(b
, l
+ (q
- b
), t
); // send it
1107 else if (*p
== EchoReply
)
1109 // Ignore it, last_packet time is set earlier than this.
1111 else if (*p
!= CodeRej
)
1113 ppp_code_rej(s
, t
, PPPLCP
, "LCP", p
, l
, b
, sizeof(b
));
1117 int join_bundle(sessionidt s
)
1119 // Search for a bundle to join
1122 for (i
= 1; i
< MAXBUNDLE
; i
++)
1124 if (bundle
[i
].state
!= BUNDLEFREE
)
1126 if (epdiscmp(session
[s
].epdis
,bundle
[i
].epdis
) && !strcmp(session
[s
].user
, bundle
[i
].user
))
1128 sessionidt first_ses
= bundle
[i
].members
[0];
1129 if (bundle
[i
].mssf
!= session
[s
].mssf
)
1131 // uniformity of sequence number format must be insured
1132 LOG(3, s
, session
[s
].tunnel
, "MPPP: unable to bundle session %d in bundle %d cause of different mssf\n", s
, i
);
1135 session
[s
].bundle
= i
;
1136 session
[s
].ip
= session
[first_ses
].ip
;
1137 session
[s
].dns1
= session
[first_ses
].dns1
;
1138 session
[s
].dns2
= session
[first_ses
].dns2
;
1139 session
[s
].timeout
= session
[first_ses
].timeout
;
1141 if(session
[s
].epdis
.length
> 0)
1142 setepdis(&bundle
[i
].epdis
, session
[s
].epdis
);
1144 strcpy(bundle
[i
].user
, session
[s
].user
);
1145 bundle
[i
].members
[bundle
[i
].num_of_links
] = s
;
1146 bundle
[i
].num_of_links
++;
1147 LOG(3, s
, session
[s
].tunnel
, "MPPP: Bundling additional line in bundle (%d), lines:%d\n",i
,bundle
[i
].num_of_links
);
1153 // No previously created bundle was found for this session, so create a new one
1154 if (!(b
= new_bundle())) return 0;
1156 session
[s
].bundle
= b
;
1157 bundle
[b
].mrru
= session
[s
].mrru
;
1158 bundle
[b
].mssf
= session
[s
].mssf
;
1159 // FIXME !!! to enable l2tpns reading mssf frames receiver_max_seq, sender_max_seq must be introduce
1160 // 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
1163 bundle[b].max_seq = 1 << 12;
1165 bundle
[b
].max_seq
= 1 << 24;
1166 if(session
[s
].epdis
.length
> 0)
1167 setepdis(&bundle
[b
].epdis
, session
[s
].epdis
);
1169 strcpy(bundle
[b
].user
, session
[s
].user
);
1170 bundle
[b
].members
[0] = s
;
1171 bundle
[b
].timeout
= session
[s
].timeout
;
1172 LOG(3, s
, session
[s
].tunnel
, "MPPP: Created a new bundle (%d)\n", b
);
1176 static int epdiscmp(epdist ep1
, epdist ep2
)
1179 if (ep1
.length
!= ep2
.length
)
1182 if (ep1
.addr_class
!= ep2
.addr_class
)
1185 for (ad
= 0; ad
< ep1
.length
; ad
++)
1186 if (ep1
.address
[ad
] != ep2
.address
[ad
])
1192 static void setepdis(epdist
*ep1
, epdist ep2
)
1195 ep1
->length
= ep2
.length
;
1196 ep1
->addr_class
= ep2
.addr_class
;
1197 for (ad
= 0; ad
< ep2
.length
; ad
++)
1198 ep1
->address
[ad
] = ep2
.address
[ad
];
1201 static bundleidt
new_bundle()
1204 for (i
= 1; i
< MAXBUNDLE
; i
++)
1206 if (bundle
[i
].state
== BUNDLEFREE
)
1208 LOG(4, 0, 0, "MPPP: Assigning bundle ID %d\n", i
);
1209 bundle
[i
].num_of_links
= 1;
1210 bundle
[i
].last_check
= time_now
; // Initialize last_check value
1211 bundle
[i
].state
= BUNDLEOPEN
;
1212 bundle
[i
].current_ses
= -1; // This is to enforce the first session 0 to be used at first
1213 memset(&frag
[i
], 0, sizeof(fragmentationt
));
1214 if (i
> config
->cluster_highest_bundleid
)
1215 config
->cluster_highest_bundleid
= i
;
1219 LOG(0, 0, 0, "MPPP: Can't find a free bundle! There shouldn't be this many in use!\n");
1223 static void ipcp_open(sessionidt s
, tunnelidt t
)
1225 LOG(3, s
, t
, "IPCP: Opened, session is now active\n");
1227 change_state(s
, ipcp
, Opened
);
1229 if (!(session
[s
].walled_garden
|| session
[s
].flags
& SESSION_STARTED
))
1231 uint16_t r
= radiusnew(s
);
1234 radiussend(r
, RADIUSSTART
); // send radius start
1236 // don't send further Start records if IPCP is restarted
1237 session
[s
].flags
|= SESSION_STARTED
;
1238 cluster_send_session(s
);
1242 // start IPv6 if configured and still in passive state
1243 if (session
[s
].ppp
.ipv6cp
== Stopped
)
1246 change_state(s
, ipv6cp
, RequestSent
);
1250 // Process IPCP messages
1251 void processipcp(sessionidt s
, tunnelidt t
, uint8_t *p
, uint16_t l
)
1253 uint8_t b
[MAXETHER
];
1259 LOG_HEX(5, "IPCP", p
, l
);
1262 LOG(1, s
, t
, "Short IPCP %d bytes\n", l
);
1263 STAT(tunnel_rx_errors
);
1267 if ((hl
= ntohs(*(uint16_t *) (p
+ 2))) > l
)
1269 LOG(1, s
, t
, "Length mismatch IPCP %u/%u\n", hl
, l
);
1270 STAT(tunnel_rx_errors
);
1275 if (session
[s
].ppp
.phase
< Network
)
1277 LOG(2, s
, t
, "IPCP %s ignored in %s phase\n", ppp_code(*p
), ppp_phase(session
[s
].ppp
.phase
));
1281 LOG(3, s
, t
, "IPCP: recv %s\n", ppp_code(*p
));
1283 if (*p
== ConfigAck
)
1285 switch (session
[s
].ppp
.ipcp
)
1288 initialise_restart_count(s
, ipcp
);
1289 change_state(s
, ipcp
, AckReceived
);
1294 LOG(2, s
, t
, "IPCP: ConfigAck in state %s? Sending ConfigReq\n", ppp_state(session
[s
].ppp
.ipcp
));
1296 change_state(s
, ipcp
, RequestSent
);
1304 LOG(2, s
, t
, "IPCP: ignoring %s in state %s\n", ppp_code(*p
), ppp_state(session
[s
].ppp
.ipcp
));
1307 else if (*p
== ConfigReq
)
1309 uint8_t *response
= 0;
1317 if (!o
[1] || o
[1] > length
) return;
1321 case 3: // ip address
1322 gotip
++; // seen address
1323 if (o
[1] != 6) return;
1325 addr
= htonl(session
[s
].ip
);
1326 if (memcmp(o
+ 2, &addr
, (sizeof addr
)))
1329 q
= ppp_conf_nak(s
, b
, sizeof(b
), PPPIPCP
, &response
, q
, p
, o
, (uint8_t *) &addr
, sizeof(addr
));
1330 if (!q
|| (q
!= oq
&& *response
== ConfigRej
))
1332 sessionshutdown(s
, "Can't negotiate IPCP.", CDN_ADMIN_DISC
, TERM_USER_ERROR
);
1339 case 129: // primary DNS
1340 if (o
[1] != 6) return;
1342 addr
= htonl(session
[s
].dns1
);
1343 if (memcmp(o
+ 2, &addr
, (sizeof addr
)))
1345 q
= ppp_conf_nak(s
, b
, sizeof(b
), PPPIPCP
, &response
, q
, p
, o
, (uint8_t *) &addr
, sizeof(addr
));
1351 case 131: // secondary DNS
1352 if (o
[1] != 6) return;
1354 addr
= htonl(session
[s
].dns2
);
1355 if (memcmp(o
+ 2, &addr
, sizeof(addr
)))
1357 q
= ppp_conf_nak(s
, b
, sizeof(b
), PPPIPCP
, &response
, q
, p
, o
, (uint8_t *) &addr
, sizeof(addr
));
1364 LOG(2, s
, t
, " Rejecting PPP IPCP Option type %d\n", *o
);
1365 q
= ppp_conf_rej(s
, b
, sizeof(b
), PPPIPCP
, &response
, q
, p
, o
);
1375 l
= q
- response
; // IPCP packet length
1376 *((uint16_t *) (response
+ 2)) = htons(l
); // update header
1380 // Send packet back as ConfigAck
1381 response
= makeppp(b
, sizeof(b
), p
, l
, s
, t
, PPPIPCP
, 0, 0, 0);
1382 if (!response
) return;
1383 *response
= ConfigAck
;
1387 LOG(1, s
, t
, "No IP in IPCP request\n");
1388 STAT(tunnel_rx_errors
);
1392 switch (session
[s
].ppp
.ipcp
)
1395 response
= makeppp(b
, sizeof(b
), p
, 2, s
, t
, PPPIPCP
, 0, 0, 0);
1396 if (!response
) return;
1397 *response
= TerminateAck
;
1398 *((uint16_t *) (response
+ 2)) = htons(l
= 4);
1402 initialise_restart_count(s
, ipcp
);
1404 if (*response
== ConfigAck
)
1405 change_state(s
, ipcp
, AckSent
);
1407 change_state(s
, ipcp
, RequestSent
);
1412 if (*response
== ConfigAck
)
1413 change_state(s
, ipcp
, AckSent
);
1418 if (*response
== ConfigAck
)
1424 initialise_restart_count(s
, ipcp
);
1429 if (*response
== ConfigAck
)
1430 change_state(s
, ipcp
, AckSent
);
1432 change_state(s
, ipcp
, RequestSent
);
1437 LOG(2, s
, t
, "IPCP: ignoring %s in state %s\n", ppp_code(*p
), ppp_state(session
[s
].ppp
.ipcp
));
1441 LOG(3, s
, t
, "IPCP: send %s\n", ppp_code(*response
));
1442 tunnelsend(b
, l
+ (response
- b
), t
);
1444 else if (*p
== TerminateReq
)
1446 switch (session
[s
].ppp
.ipcp
)
1458 zero_restart_count(s
, ipcp
);
1459 change_state(s
, ipcp
, Closing
);
1463 LOG(2, s
, t
, "IPCP: ignoring %s in state %s\n", ppp_code(*p
), ppp_state(session
[s
].ppp
.ipcp
));
1467 *p
= TerminateAck
; // send ack
1468 q
= makeppp(b
, sizeof(b
), p
, l
, s
, t
, PPPIPCP
, 0, 0, 0);
1471 LOG(3, s
, t
, "IPCP: send %s\n", ppp_code(*q
));
1472 tunnelsend(b
, l
+ (q
- b
), t
); // send it
1474 else if (*p
!= CodeRej
)
1476 ppp_code_rej(s
, t
, PPPIPCP
, "IPCP", p
, l
, b
, sizeof(b
));
1480 static void ipv6cp_open(sessionidt s
, tunnelidt t
)
1483 LOG(3, s
, t
, "IPV6CP: Opened\n");
1485 change_state(s
, ipv6cp
, Opened
);
1486 for (i
= 0; i
< MAXROUTE6
&& session
[s
].route6
[i
].ipv6prefixlen
; i
++)
1488 route6set(s
, session
[s
].route6
[i
].ipv6route
, session
[s
].route6
[i
].ipv6prefixlen
, 1);
1491 if (session
[s
].ipv6address
.s6_addr
[0])
1493 // Check if included in prefix
1494 if (sessionbyipv6(session
[s
].ipv6address
) != s
)
1495 route6set(s
, session
[s
].ipv6address
, 128, 1);
1498 // Send an initial RA (TODO: Should we send these regularly?)
1499 send_ipv6_ra(s
, t
, NULL
);
1502 // Process IPV6CP messages
1503 void processipv6cp(sessionidt s
, tunnelidt t
, uint8_t *p
, uint16_t l
)
1505 uint8_t b
[MAXETHER
];
1509 CSTAT(processipv6cp
);
1511 LOG_HEX(5, "IPV6CP", p
, l
);
1514 LOG(1, s
, t
, "Short IPV6CP %d bytes\n", l
);
1515 STAT(tunnel_rx_errors
);
1519 if ((hl
= ntohs(*(uint16_t *) (p
+ 2))) > l
)
1521 LOG(1, s
, t
, "Length mismatch IPV6CP %u/%u\n", hl
, l
);
1522 STAT(tunnel_rx_errors
);
1527 if (session
[s
].ppp
.phase
< Network
)
1529 LOG(2, s
, t
, "IPV6CP %s ignored in %s phase\n", ppp_code(*p
), ppp_phase(session
[s
].ppp
.phase
));
1533 LOG(3, s
, t
, "IPV6CP: recv %s\n", ppp_code(*p
));
1537 LOG(3, s
, t
, "IPV6CP: no IPv4 address (IPCP in state %s)\n", ppp_state(session
[s
].ppp
.ipcp
));
1538 return; // need IPCP to complete...
1541 if (*p
== ConfigAck
)
1543 switch (session
[s
].ppp
.ipv6cp
)
1546 initialise_restart_count(s
, ipv6cp
);
1547 change_state(s
, ipv6cp
, AckReceived
);
1552 LOG(2, s
, t
, "IPV6CP: ConfigAck in state %s? Sending ConfigReq\n", ppp_state(session
[s
].ppp
.ipv6cp
));
1554 change_state(s
, ipv6cp
, RequestSent
);
1562 LOG(2, s
, t
, "IPV6CP: ignoring %s in state %s\n", ppp_code(*p
), ppp_state(session
[s
].ppp
.ipv6cp
));
1565 else if (*p
== ConfigReq
)
1567 uint8_t *response
= 0;
1575 if (!o
[1] || o
[1] > length
) return;
1579 case 1: // interface identifier
1580 gotip
++; // seen address
1581 if (o
[1] != 10) return;
1583 if (session
[s
].ipv6address
.s6_addr
[0])
1585 // LSB 64bits of assigned IPv6 address to user (see radius attribut Framed-IPv6-Address)
1586 memcpy(&ident
[0], &session
[s
].ipv6address
.s6_addr
[8], 8);
1590 ident
[0] = htonl(session
[s
].ip
);
1594 if (memcmp(o
+ 2, ident
, sizeof(ident
)))
1596 q
= ppp_conf_nak(s
, b
, sizeof(b
), PPPIPV6CP
, &response
, q
, p
, o
, (uint8_t *)ident
, sizeof(ident
));
1603 LOG(2, s
, t
, " Rejecting PPP IPV6CP Option type %d\n", *o
);
1604 q
= ppp_conf_rej(s
, b
, sizeof(b
), PPPIPV6CP
, &response
, q
, p
, o
);
1614 l
= q
- response
; // IPV6CP packet length
1615 *((uint16_t *) (response
+ 2)) = htons(l
); // update header
1619 // Send packet back as ConfigAck
1620 response
= makeppp(b
, sizeof(b
), p
, l
, s
, t
, PPPIPV6CP
, 0, 0, 0);
1621 if (!response
) return;
1622 *response
= ConfigAck
;
1626 LOG(1, s
, t
, "No interface identifier in IPV6CP request\n");
1627 STAT(tunnel_rx_errors
);
1631 switch (session
[s
].ppp
.ipv6cp
)
1634 response
= makeppp(b
, sizeof(b
), p
, 2, s
, t
, PPPIPV6CP
, 0, 0, 0);
1635 if (!response
) return;
1636 *response
= TerminateAck
;
1637 *((uint16_t *) (response
+ 2)) = htons(l
= 4);
1641 initialise_restart_count(s
, ipv6cp
);
1643 if (*response
== ConfigAck
)
1644 change_state(s
, ipv6cp
, AckSent
);
1646 change_state(s
, ipv6cp
, RequestSent
);
1651 if (*response
== ConfigAck
)
1652 change_state(s
, ipv6cp
, AckSent
);
1657 if (*response
== ConfigAck
)
1663 initialise_restart_count(s
, ipv6cp
);
1668 if (*response
== ConfigAck
)
1669 change_state(s
, ipv6cp
, AckSent
);
1671 change_state(s
, ipv6cp
, RequestSent
);
1676 LOG(2, s
, t
, "IPV6CP: ignoring %s in state %s\n", ppp_code(*p
), ppp_state(session
[s
].ppp
.ipv6cp
));
1680 LOG(3, s
, t
, "IPV6CP: send %s\n", ppp_code(*response
));
1681 tunnelsend(b
, l
+ (response
- b
), t
);
1683 else if (*p
== TerminateReq
)
1685 switch (session
[s
].ppp
.ipv6cp
)
1697 zero_restart_count(s
, ipv6cp
);
1698 change_state(s
, ipv6cp
, Closing
);
1702 LOG(2, s
, t
, "IPV6CP: ignoring %s in state %s\n", ppp_code(*p
), ppp_state(session
[s
].ppp
.ipv6cp
));
1706 *p
= TerminateAck
; // send ack
1707 q
= makeppp(b
, sizeof(b
), p
, l
, s
, t
, PPPIPV6CP
, 0, 0, 0);
1710 LOG(3, s
, t
, "IPV6CP: send %s\n", ppp_code(*q
));
1711 tunnelsend(b
, l
+ (q
- b
), t
); // send it
1713 else if (*p
!= CodeRej
)
1715 ppp_code_rej(s
, t
, PPPIPV6CP
, "IPV6CP", p
, l
, b
, sizeof(b
));
1719 static void update_sessions_in_stat(sessionidt s
, uint16_t l
)
1721 bundleidt b
= session
[s
].bundle
;
1724 increment_counter(&session
[s
].cin
, &session
[s
].cin_wrap
, l
);
1725 session
[s
].cin_delta
+= l
;
1728 sess_local
[s
].cin
+= l
;
1729 sess_local
[s
].pin
++;
1733 int i
= frag
[b
].re_frame_begin_index
;
1734 int end
= frag
[b
].re_frame_end_index
;
1737 l
= frag
[b
].fragment
[i
].length
;
1738 s
= frag
[b
].fragment
[i
].sid
;
1739 increment_counter(&session
[s
].cin
, &session
[s
].cin_wrap
, l
);
1740 session
[s
].cin_delta
+= l
;
1743 sess_local
[s
].cin
+= l
;
1744 sess_local
[s
].pin
++;
1747 i
= (i
+ 1) & MAXFRAGNUM_MASK
;
1752 // process IP packet received
1754 // This MUST be called with at least 4 byte behind 'p'.
1755 // (i.e. this routine writes to p[-4]).
1756 void processipin(sessionidt s
, tunnelidt t
, uint8_t *p
, uint16_t l
)
1758 in_addr_t ip
, ip_dst
;
1762 LOG_HEX(5, "IP", p
, l
);
1766 LOG(1, s
, t
, "IP packet too short %d\n", l
);
1767 STAT(tunnel_rx_errors
);
1771 ip
= ntohl(*(uint32_t *)(p
+ 12));
1772 ip_dst
= *(uint32_t *)(p
+ 16);
1776 LOG(1, s
, t
, "IP packet too long %d\n", l
);
1777 STAT(tunnel_rx_errors
);
1781 if (session
[s
].ppp
.phase
!= Network
|| session
[s
].ppp
.ipcp
!= Opened
)
1784 if (!session
[s
].bundle
|| bundle
[session
[s
].bundle
].num_of_links
< 2) // FIXME:
1786 // no spoof (do sessionbyip to handled statically routed subnets)
1787 if (!config
->disable_no_spoof
&& ip
!= session
[s
].ip
&& sessionbyip(htonl(ip
)) != s
)
1789 LOG(4, s
, t
, "Dropping packet with spoofed IP %s\n", fmtaddr(htonl(ip
), 0));
1794 // run access-list if any
1795 if (session
[s
].filter_in
&& !ip_filter(p
, l
, session
[s
].filter_in
- 1))
1798 // adjust MSS on SYN and SYN,ACK packets with options
1799 if ((ntohs(*(uint16_t *) (p
+ 6)) & 0x1fff) == 0 && p
[9] == IPPROTO_TCP
) // first tcp fragment
1801 int ihl
= (p
[0] & 0xf) * 4; // length of IP header
1802 if (l
>= ihl
+ 20 && (p
[ihl
+ 13] & TCP_FLAG_SYN
) && ((p
[ihl
+ 12] >> 4) > 5))
1803 adjust_tcp_mss(s
, t
, p
, l
, p
+ ihl
);
1806 // Add on the tun header
1808 *(uint32_t *) p
= htonl(PKTIP
);
1811 if (session
[s
].tbf_in
)
1813 if (!config
->no_throttle_local_IP
|| !sessionbyip(ip_dst
))
1815 // Are we throttling this session?
1816 if (config
->cluster_iam_master
)
1817 tbf_queue_packet(session
[s
].tbf_in
, p
, l
);
1819 master_throttle_packet(session
[s
].tbf_in
, p
, l
);
1825 if (tun_write(p
, l
) < 0)
1827 STAT(tun_tx_errors
);
1828 LOG(0, s
, t
, "Error writing %d bytes to TUN device: %s (tunfd=%d, p=%p)\n",
1829 l
, strerror(errno
), tunfd
, p
);
1837 if (session
[s
].snoop_ip
&& session
[s
].snoop_port
)
1839 // Snooping this session
1840 snoop_send_packet(p
, l
, session
[s
].snoop_ip
, session
[s
].snoop_port
);
1843 update_sessions_in_stat(s
, l
);
1847 STAT(tun_tx_packets
);
1848 INC_STAT(tun_tx_bytes
, l
);
1851 // process Multilink PPP packet received
1852 void processmpin(sessionidt s
, tunnelidt t
, uint8_t *p
, uint16_t l
)
1854 bundleidt b
= session
[s
].bundle
;
1855 bundlet
* this_bundle
= &bundle
[b
];
1856 uint32_t maskSeq
, max_seq
;
1858 uint16_t frag_index
, frag_index_next
, frag_index_prev
;
1859 fragmentationt
*this_fragmentation
= &frag
[b
];
1860 uint8_t begin_frame
= (*p
& MP_BEGIN
);
1861 uint8_t end_frame
= (*p
& MP_END
);
1862 uint32_t seq_num
, seq_num_next
, seq_num_prev
;
1865 uint16_t begin_index
, end_index
;
1867 // Perform length checking
1870 LOG(2, s
, t
, "MPPP: discarding fragment larger than MAXFRAGLEN\n");
1876 LOG(2, s
, t
, "MPPP: Invalid bundle id: 0\n");
1879 // FIXME !! session[s].mssf means that the receiver wants to receive frames in mssf not means the receiver will send frames in mssf
1880 /* if(session[s].mssf)
1882 // Get 12 bit for seq number
1883 seq_num = ntohs((*(uint16_t *) p) & 0xFF0F);
1886 // After this point the pointer should be advanced 2 bytes
1887 LOG(3, s, t, "MPPP: 12 bits, sequence number: %d\n",seq_num);
1891 // Get 24 bit for seq number
1892 seq_num
= ntohl((*(uint32_t *) p
) & 0xFFFFFF00);
1895 // After this point the pointer should be advanced 4 bytes
1896 LOG(4, s
, t
, "MPPP: 24 bits sequence number:%d\n",seq_num
);
1899 max_seq
= this_bundle
->max_seq
;
1900 maskSeq
= max_seq
- 1;
1903 * Expand sequence number to 32 bits, making it as close
1904 * as possible to this_fragmentation->M.
1906 seq_num
|= this_fragmentation
->M
& ~maskSeq
;
1907 if ((int)(this_fragmentation
->M
- seq_num
) > (int)(maskSeq
>> 1))
1909 seq_num
+= maskSeq
+ 1;
1911 else if ((int)(seq_num
- this_fragmentation
->M
) > (int)(maskSeq
>> 1))
1913 seq_num
-= maskSeq
+ 1; /* should never happen */
1916 // calculate this fragment's offset from the begin seq in the bundle
1917 frag_offset
= (int) (seq_num
- this_fragmentation
->start_seq
);
1919 sess_local
[s
].last_seq
= seq_num
;
1921 // calculate the jitter average
1922 uint32_t ljitter
= time_now_ms
- sess_local
[s
].prev_time
;
1925 sess_local
[s
].jitteravg
= (sess_local
[s
].jitteravg
+ ljitter
)>>1;
1926 sess_local
[s
].prev_time
= time_now_ms
;
1931 if (seq_num
< this_fragmentation
->M
)
1934 this_fragmentation
->M
= seq_num
;
1938 Mmin
= sess_local
[(this_bundle
->members
[0])].last_seq
;
1939 for (i
= 1; i
< this_bundle
->num_of_links
; i
++)
1941 uint32_t s_seq
= sess_local
[(this_bundle
->members
[i
])].last_seq
;
1945 this_fragmentation
->M
= Mmin
;
1948 // calculate M offset of the M seq in the bundle
1949 int M_offset
= (int) (Mmin
- this_fragmentation
->start_seq
);
1951 if (M_offset
>= MAXFRAGNUM
)
1953 // There have a long break of the link !!!!!!!!
1954 // M_offset is bigger that the fragmentation buffer size
1955 LOG(3, s
, t
, "MPPP: M_offset out of range, min:%d, begin_seq:%d\n", Mmin
, this_fragmentation
->start_seq
);
1957 // Calculate the new start index, the previous frag are lost
1958 begin_index
= (M_offset
+ this_fragmentation
->start_index
) & MAXFRAGNUM_MASK
;
1960 // Set new Start sequence
1961 this_fragmentation
->start_index
= begin_index
;
1962 this_fragmentation
->start_seq
= Mmin
;
1964 // recalculate the fragment offset from the new begin seq in the bundle
1965 frag_offset
= (int) (seq_num
- Mmin
);
1968 // discard this fragment if the packet comes before the start sequence
1969 if (frag_offset
< 0)
1971 // this packet comes before the next
1972 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
);
1976 // discard if frag_offset is bigger that the fragmentation buffer size
1977 if (frag_offset
>= MAXFRAGNUM
)
1979 // frag_offset is bigger that the fragmentation buffer size
1980 LOG(3, s
, t
, "MPPP: Index out of range, seq:%d, begin_seq:%d\n", seq_num
, this_fragmentation
->start_seq
);
1984 //caculate received fragment's index in the fragment array
1985 frag_index
= (frag_offset
+ this_fragmentation
->start_index
) & MAXFRAGNUM_MASK
;
1987 // insert the frame in it's place
1988 fragmentt
*this_frag
= &this_fragmentation
->fragment
[frag_index
];
1990 if (this_frag
->length
> 0)
1991 // This fragment is lost, It was around the buffer and it was never completed the packet.
1992 LOG(3, this_frag
->sid
, this_frag
->tid
, "MPPP: (INSERT) seq_num:%d frag_index:%d flags:%02X is LOST\n",
1993 this_frag
->seq
, frag_index
, this_frag
->flags
);
1995 this_frag
->length
= l
;
1998 this_frag
->flags
= flags
;
1999 this_frag
->seq
= seq_num
;
2000 this_frag
->jitteravg
= sess_local
[s
].jitteravg
;
2001 memcpy(this_frag
->data
, p
, l
);
2003 LOG(4, s
, t
, "MPPP: seq_num:%d frag_index:%d INSERTED flags: %02X\n", seq_num
, frag_index
, flags
);
2006 frag_index_next
= (frag_index
+ 1) & MAXFRAGNUM_MASK
;
2007 //previous frag index
2008 frag_index_prev
= (frag_index
- 1) & MAXFRAGNUM_MASK
;
2010 seq_num_next
= seq_num
+ 1;
2012 seq_num_prev
= seq_num
- 1;
2014 // Clean the buffer and log the lost fragments
2015 if ((frag_index_next
!= this_fragmentation
->start_index
) && this_fragmentation
->fragment
[frag_index_next
].length
)
2017 // check if the next frag is a lost fragment
2018 if (this_fragmentation
->fragment
[frag_index_next
].seq
!= seq_num_next
)
2020 // This fragment is lost, It was around the buffer and it was never completed the packet.
2021 LOG(3, this_fragmentation
->fragment
[frag_index_next
].sid
, this_fragmentation
->fragment
[frag_index_next
].tid
,
2022 "MPPP: (NEXT) seq_num:%d frag_index:%d flags:%02X is LOST\n",
2023 this_fragmentation
->fragment
[frag_index_next
].seq
, frag_index_next
,
2024 this_fragmentation
->fragment
[frag_index_next
].flags
);
2025 // this frag is lost
2026 this_fragmentation
->fragment
[frag_index_next
].length
= 0;
2027 this_fragmentation
->fragment
[frag_index_next
].flags
= 0;
2029 if (begin_frame
&& (!end_frame
)) return; // assembling frame failed
2033 // Clean the buffer and log the lost fragments
2034 if ((frag_index
!= this_fragmentation
->start_index
) && this_fragmentation
->fragment
[frag_index_prev
].length
)
2036 // check if the next frag is a lost fragment
2037 if (this_fragmentation
->fragment
[frag_index_prev
].seq
!= seq_num_prev
)
2039 // This fragment is lost, It was around the buffer and it was never completed the packet.
2040 LOG(3, this_fragmentation
->fragment
[frag_index_prev
].sid
, this_fragmentation
->fragment
[frag_index_prev
].tid
,
2041 "MPPP: (PREV) seq_num:%d frag_index:%d flags:%02X is LOST\n",
2042 this_fragmentation
->fragment
[frag_index_prev
].seq
, frag_index_prev
,
2043 this_fragmentation
->fragment
[frag_index_prev
].flags
);
2045 this_fragmentation
->fragment
[frag_index_prev
].length
= 0;
2046 this_fragmentation
->fragment
[frag_index_prev
].flags
= 0;
2048 if (end_frame
&& (!begin_frame
)) return; // assembling frame failed
2053 begin_index
= this_fragmentation
->start_index
;
2054 uint32_t b_seq
= this_fragmentation
->start_seq
;
2055 // Try to find a Begin sequence from the start_seq sequence to M sequence
2056 while (b_seq
< Mmin
)
2058 if (this_fragmentation
->fragment
[begin_index
].length
)
2060 if (b_seq
== this_fragmentation
->fragment
[begin_index
].seq
)
2062 if (this_fragmentation
->fragment
[begin_index
].flags
& MP_BEGIN
)
2065 // Adjust the new start sequence and start index
2066 this_fragmentation
->start_index
= begin_index
;
2067 this_fragmentation
->start_seq
= b_seq
;
2068 // Begin Sequence found, now try to found the End Sequence to complete the frame
2069 end_index
= begin_index
;
2070 while (b_seq
< Mmin
)
2072 if (this_fragmentation
->fragment
[end_index
].length
)
2074 if (b_seq
== this_fragmentation
->fragment
[end_index
].seq
)
2076 if (this_fragmentation
->fragment
[end_index
].flags
& MP_END
)
2078 // The End sequence was found and the frame is complete
2085 // This fragment is lost, it was never completed the packet.
2086 LOG(3, this_fragmentation
->fragment
[end_index
].sid
, this_fragmentation
->fragment
[end_index
].tid
,
2087 "MPPP: (FIND END) seq_num:%d frag_index:%d flags:%02X is LOST\n",
2088 this_fragmentation
->fragment
[end_index
].seq
, begin_index
,
2089 this_fragmentation
->fragment
[end_index
].flags
);
2090 // this frag is lost
2091 this_fragmentation
->fragment
[end_index
].length
= 0;
2092 this_fragmentation
->fragment
[end_index
].flags
= 0;
2093 // This frame is not complete find the next Begin
2099 // This frame is not complete find the next Begin if exist
2102 end_index
= (end_index
+1) & MAXFRAGNUM_MASK
;
2107 // The End sequence was found and the frame is complete
2110 // find the next Begin
2111 begin_index
= end_index
;
2116 // This fragment is lost, it was never completed the packet.
2117 LOG(3, this_fragmentation
->fragment
[begin_index
].sid
, this_fragmentation
->fragment
[begin_index
].tid
,
2118 "MPPP: (FIND BEGIN) seq_num:%d frag_index:%d flags:%02X is LOST\n",
2119 this_fragmentation
->fragment
[begin_index
].seq
, begin_index
,
2120 this_fragmentation
->fragment
[begin_index
].flags
);
2121 // this frag is lost
2122 this_fragmentation
->fragment
[begin_index
].length
= 0;
2123 this_fragmentation
->fragment
[begin_index
].flags
= 0;
2126 begin_index
= (begin_index
+1) & MAXFRAGNUM_MASK
;
2131 // try to assemble the frame that has the received fragment as a member
2132 // get the beginning of this frame
2133 begin_index
= end_index
= this_fragmentation
->start_index
;
2134 if (this_fragmentation
->fragment
[begin_index
].length
)
2136 if (!(this_fragmentation
->fragment
[begin_index
].flags
& MP_BEGIN
))
2138 LOG(3, this_fragmentation
->fragment
[begin_index
].sid
, this_fragmentation
->fragment
[begin_index
].tid
,
2139 "MPPP: (NOT BEGIN) seq_num:%d frag_index:%d flags:%02X\n",
2140 this_fragmentation
->fragment
[begin_index
].seq
, begin_index
,
2141 this_fragmentation
->fragment
[begin_index
].flags
);
2142 // should occur only after an "M_Offset out of range"
2143 // The start sequence must be a begin sequence
2144 this_fragmentation
->start_index
= (begin_index
+1) & MAXFRAGNUM_MASK
;
2145 this_fragmentation
->start_seq
++;
2146 return; // assembling frame failed
2150 return; // assembling frame failed
2152 // get the end of his frame
2153 while (this_fragmentation
->fragment
[end_index
].length
)
2155 if (this_fragmentation
->fragment
[end_index
].flags
& MP_END
)
2158 end_index
= (end_index
+1) & MAXFRAGNUM_MASK
;
2160 if (end_index
== this_fragmentation
->start_index
)
2161 return; // assembling frame failed
2164 // return if a lost fragment is found
2165 if (!(this_fragmentation
->fragment
[end_index
].length
))
2166 return; // assembling frame failed
2168 // assemble the packet
2169 //assemble frame, process it, reset fragmentation
2170 uint16_t cur_len
= 4; // This is set to 4 to leave 4 bytes for function processipin
2172 LOG(4, s
, t
, "MPPP: processing fragments from %d to %d\n", begin_index
, end_index
);
2173 // Push to the receive buffer
2175 for (i
= begin_index
;; i
= (i
+ 1) & MAXFRAGNUM_MASK
)
2177 this_frag
= &this_fragmentation
->fragment
[i
];
2178 if(cur_len
+ this_frag
->length
> MAXETHER
)
2180 LOG(2, s
, t
, "MPPP: discarding reassembled frames larger than MAXETHER\n");
2184 memcpy(this_fragmentation
->reassembled_frame
+cur_len
, this_frag
->data
, this_frag
->length
);
2185 LOG(5, s
, t
, "MPPP: processing frame at %d, with len %d\n", i
, this_frag
->length
);
2187 cur_len
+= this_frag
->length
;
2190 this_fragmentation
->re_frame_len
= cur_len
;
2191 this_fragmentation
->re_frame_begin_index
= begin_index
;
2192 this_fragmentation
->re_frame_end_index
= end_index
;
2193 // Process the resassembled frame
2194 LOG(5, s
, t
, "MPPP: Process the reassembled frame, len=%d\n",cur_len
);
2195 processmpframe(s
, t
, this_fragmentation
->reassembled_frame
, this_fragmentation
->re_frame_len
, 1);
2200 // Set reassembled frame length to zero after processing it
2201 this_fragmentation
->re_frame_len
= 0;
2202 for (i
= begin_index
;; i
= (i
+ 1) & MAXFRAGNUM_MASK
)
2204 this_fragmentation
->fragment
[i
].length
= 0; // Indicates that this fragment has been consumed
2205 this_fragmentation
->fragment
[i
].flags
= 0;
2210 // Set the new start_index and start_seq
2211 this_fragmentation
->start_index
= (end_index
+ 1) & MAXFRAGNUM_MASK
;
2212 this_fragmentation
->start_seq
= this_fragmentation
->fragment
[end_index
].seq
+ 1;
2213 LOG(4, s
, t
, "MPPP after assembling: start index is = %d, start seq=%d\n", this_fragmentation
->start_index
, this_fragmentation
->start_seq
);
2215 begin_index
= this_fragmentation
->start_index
;
2216 if ((this_fragmentation
->fragment
[begin_index
].length
) &&
2217 (this_fragmentation
->fragment
[begin_index
].seq
!= this_fragmentation
->start_seq
))
2219 LOG(3, this_fragmentation
->fragment
[begin_index
].sid
, this_fragmentation
->fragment
[begin_index
].tid
,
2220 "MPPP: (START) seq_num:%d frag_index:%d flags:%02X is LOST\n",
2221 this_fragmentation
->fragment
[begin_index
].seq
, begin_index
,
2222 this_fragmentation
->fragment
[begin_index
].flags
);
2223 this_fragmentation
->fragment
[begin_index
].length
= 0;
2224 this_fragmentation
->fragment
[begin_index
].flags
= 0;
2227 if (this_fragmentation
->start_seq
<= Mmin
)
2228 // It's possible to find other complete frame or lost frame.
2230 else if ((this_fragmentation
->fragment
[begin_index
].length
) &&
2231 (this_fragmentation
->fragment
[begin_index
].flags
& MP_BEGIN
))
2232 // may be that the next frame is completed
2233 goto assembling_frame
;
2238 // process IPv6 packet received
2240 // This MUST be called with at least 4 byte behind 'p'.
2241 // (i.e. this routine writes to p[-4]).
2242 void processipv6in(sessionidt s
, tunnelidt t
, uint8_t *p
, uint16_t l
)
2247 CSTAT(processipv6in
);
2249 LOG_HEX(5, "IPv6", p
, l
);
2251 ip
= *(struct in6_addr
*) (p
+ 8);
2252 ipv4
= ntohl(*(uint32_t *)(p
+ 16));
2256 LOG(1, s
, t
, "IP packet too long %d\n", l
);
2257 STAT(tunnel_rx_errors
);
2261 if (session
[s
].ppp
.phase
!= Network
|| session
[s
].ppp
.ipv6cp
!= Opened
)
2265 if (session
[s
].ipv6address
.s6_addr
[0])
2267 if ((sessionbyipv6new(ip
) != s
) &&
2268 (ip
.s6_addr
[0] != 0xFE || ip
.s6_addr
[1] != 0x80 || ip
.s6_addr16
[1] != 0 || ip
.s6_addr16
[2] != 0 || ip
.s6_addr16
[3] != 0))
2270 char str
[INET6_ADDRSTRLEN
];
2271 LOG(5, s
, t
, "Dropping packet with spoofed IP %s\n",
2272 inet_ntop(AF_INET6
, &ip
, str
, INET6_ADDRSTRLEN
));
2276 else if ((ipv4
!= session
[s
].ip
|| memcmp(&config
->ipv6_prefix
, &ip
, 8)) && sessionbyipv6(ip
) != s
)
2278 char str
[INET6_ADDRSTRLEN
];
2279 LOG(5, s
, t
, "Dropping packet with spoofed IP %s\n",
2280 inet_ntop(AF_INET6
, &ip
, str
, INET6_ADDRSTRLEN
));
2284 // Check if it's a Router Solicition message.
2285 if (*(p
+ 6) == 58 && *(p
+ 7) == 255 && *(p
+ 24) == 0xFF && *(p
+ 25) == 2 &&
2286 *(uint32_t *)(p
+ 26) == 0 && *(uint32_t *)(p
+ 30) == 0 &&
2287 *(uint32_t *)(p
+ 34) == 0 &&
2288 *(p
+ 38) == 0 && *(p
+ 39) == 2 && *(p
+ 40) == 133) {
2289 LOG(3, s
, t
, "Got IPv6 RS\n");
2290 send_ipv6_ra(s
, t
, &ip
);
2294 // Check if DhcpV6, IP dst: FF02::1:2, Src Port 0x0222 (546), Dst Port 0x0223 (547)
2295 if (*(p
+ 6) == 17 && *(p
+ 24) == 0xFF && *(p
+ 25) == 2 &&
2296 *(uint32_t *)(p
+ 26) == 0 && *(uint32_t *)(p
+ 30) == 0 &&
2297 *(uint16_t *)(p
+ 34) == 0 && *(p
+ 36) == 0 && *(p
+ 37) == 1 && *(p
+ 38) == 0 && *(p
+ 39) == 2 &&
2298 *(p
+ 40) == 2 && *(p
+ 41) == 0x22 && *(p
+ 42) == 2 && *(p
+ 43) == 0x23)
2300 dhcpv6_process(s
, t
, p
, l
);
2304 // Add on the tun header
2306 *(uint32_t *) p
= htonl(PKTIPV6
);
2309 // Are we throttled and a slave?
2310 if (session
[s
].tbf_in
&& !config
->cluster_iam_master
) {
2311 // Pass it to the master for handling.
2312 master_throttle_packet(session
[s
].tbf_in
, p
, l
);
2316 // Are we throttled and a master??
2317 if (session
[s
].tbf_in
&& config
->cluster_iam_master
) {
2318 // Actually handle the throttled packets.
2319 tbf_queue_packet(session
[s
].tbf_in
, p
, l
);
2324 if (tun_write(p
, l
) < 0)
2326 STAT(tun_tx_errors
);
2327 LOG(0, s
, t
, "Error writing %d bytes to TUN device: %s (tunfd=%d, p=%p)\n",
2328 l
, strerror(errno
), tunfd
, p
);
2336 if (session
[s
].snoop_ip
&& session
[s
].snoop_port
)
2338 // Snooping this session
2339 snoop_send_packet(p
, l
, session
[s
].snoop_ip
, session
[s
].snoop_port
);
2342 update_sessions_in_stat(s
, l
);
2346 STAT(tun_tx_packets
);
2347 INC_STAT(tun_tx_bytes
, l
);
2351 // Helper routine for the TBF filters.
2352 // Used to send queued data in from the user.
2354 void send_ipin(sessionidt s
, uint8_t *buf
, int len
)
2356 LOG_HEX(5, "IP in throttled", buf
, len
);
2358 if (write(tunfd
, buf
, len
) < 0)
2360 STAT(tun_tx_errors
);
2361 LOG(0, 0, 0, "Error writing %d bytes to TUN device: %s (tunfd=%d, p=%p)\n",
2362 len
, strerror(errno
), tunfd
, buf
);
2370 if (session
[s
].snoop_ip
&& session
[s
].snoop_port
)
2372 // Snooping this session
2373 snoop_send_packet(buf
, len
, session
[s
].snoop_ip
, session
[s
].snoop_port
);
2376 // Increment packet counters
2377 increment_counter(&session
[s
].cin
, &session
[s
].cin_wrap
, len
);
2378 session
[s
].cin_delta
+= len
;
2381 sess_local
[s
].cin
+= len
;
2382 sess_local
[s
].pin
++;
2386 STAT(tun_tx_packets
);
2387 INC_STAT(tun_tx_bytes
, len
- 4);
2391 // Process CCP messages
2392 void processccp(sessionidt s
, tunnelidt t
, uint8_t *p
, uint16_t l
)
2394 uint8_t b
[MAXETHER
];
2399 LOG_HEX(5, "CCP", p
, l
);
2401 if (session
[s
].ppp
.phase
< Network
)
2403 LOG(2, s
, t
, "CCP %s ignored in %s phase\n", ppp_code(*p
), ppp_phase(session
[s
].ppp
.phase
));
2409 LOG(1, s
, t
, "Short CCP packet\n");
2410 STAT(tunnel_rx_errors
);
2413 LOG(4, s
, t
, "CCP: recv %s\n", ppp_code(*p
));
2414 if (*p
== ConfigAck
)
2416 switch (session
[s
].ppp
.ccp
)
2419 initialise_restart_count(s
, ccp
);
2420 change_state(s
, ccp
, AckReceived
);
2425 LOG(2, s
, t
, "CCP: ConfigAck in state %s? Sending ConfigReq\n", ppp_state(session
[s
].ppp
.ccp
));
2427 change_state(s
, ccp
, RequestSent
);
2431 LOG(3, s
, t
, "CCP: Opened\n");
2432 change_state(s
, ccp
, Opened
);
2436 LOG(2, s
, t
, "CCP: ignoring %s in state %s\n", ppp_code(*p
), ppp_state(session
[s
].ppp
.ccp
));
2439 else if (*p
== ConfigReq
)
2441 if (l
< 6) // accept no compression
2443 else // compression requested--reject
2446 q
= makeppp(b
, sizeof(b
), p
, l
, s
, t
, PPPCCP
, 0, 0, 0);
2449 switch (session
[s
].ppp
.ccp
)
2452 q
= makeppp(b
, sizeof(b
), p
, 2, s
, t
, PPPCCP
, 0, 0, 0);
2455 *((uint16_t *) (q
+ 2)) = htons(l
= 4);
2459 initialise_restart_count(s
, ccp
);
2461 if (*q
== ConfigAck
)
2462 change_state(s
, ccp
, AckSent
);
2464 change_state(s
, ccp
, RequestSent
);
2469 if (*q
== ConfigAck
)
2470 change_state(s
, ccp
, AckSent
);
2475 if (*q
== ConfigAck
)
2476 change_state(s
, ccp
, Opened
);
2481 initialise_restart_count(s
, ccp
);
2486 if (*q
== ConfigAck
)
2487 change_state(s
, ccp
, AckSent
);
2489 change_state(s
, ccp
, RequestSent
);
2494 LOG(2, s
, t
, "CCP: ignoring %s in state %s\n", ppp_code(*p
), ppp_state(session
[s
].ppp
.ccp
));
2498 LOG(4, s
, t
, "CCP: send %s\n", ppp_code(*q
));
2499 tunnelsend(b
, l
+ (q
- b
), t
);
2501 else if (*p
== TerminateReq
)
2504 q
= makeppp(b
, sizeof(b
), p
, l
, s
, t
, PPPCCP
, 0, 0, 0);
2506 LOG(3, s
, t
, "CCP: send %s\n", ppp_code(*q
));
2507 tunnelsend(b
, l
+ (q
- b
), t
);
2508 change_state(s
, ccp
, Stopped
);
2510 else if (*p
!= CodeRej
)
2512 ppp_code_rej(s
, t
, PPPCCP
, "CCP", p
, l
, b
, sizeof(b
));
2516 // send a CHAP challenge
2517 void sendchap(sessionidt s
, tunnelidt t
)
2519 uint8_t b
[MAXETHER
];
2528 LOG(1, s
, t
, "No RADIUS to send challenge\n");
2529 STAT(tunnel_tx_errors
);
2533 LOG(1, s
, t
, "Send CHAP challenge\n");
2535 radius
[r
].chap
= 1; // CHAP not PAP
2537 if (radius
[r
].state
!= RADIUSCHAP
)
2540 radius
[r
].state
= RADIUSCHAP
;
2541 radius
[r
].retry
= backoff(radius
[r
].try++);
2542 if (radius
[r
].try > 5)
2544 sessionshutdown(s
, "CHAP timeout.", CDN_ADMIN_DISC
, TERM_REAUTHENTICATION_FAILURE
);
2545 STAT(tunnel_tx_errors
);
2548 q
= makeppp(b
, sizeof(b
), 0, 0, s
, t
, PPPCHAP
, 0, 0, 0);
2551 *q
= 1; // challenge
2552 q
[1] = radius
[r
].id
; // ID
2553 q
[4] = 16; // value size (size of challenge)
2554 memcpy(q
+ 5, radius
[r
].auth
, 16); // challenge
2555 strcpy((char *) q
+ 21, config
->multi_n_hostname
[tunnel
[t
].indexudp
][0]?config
->multi_n_hostname
[tunnel
[t
].indexudp
]:hostname
); // our name
2556 *(uint16_t *) (q
+ 2) = htons(strlen(config
->multi_n_hostname
[tunnel
[t
].indexudp
][0]?config
->multi_n_hostname
[tunnel
[t
].indexudp
]:hostname
) + 21); // length
2557 tunnelsend(b
, strlen(config
->multi_n_hostname
[tunnel
[t
].indexudp
][0]?config
->multi_n_hostname
[tunnel
[t
].indexudp
]:hostname
) + 21 + (q
- b
), t
); // send it
2560 // fill in a L2TP message with a PPP frame,
2561 // returns start of PPP frame
2562 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
)
2564 uint16_t hdr
= 0x0002; // L2TP with no options
2565 uint16_t type
= mtype
;
2568 if (t
== TUNNEL_ID_PPPOE
)
2570 return pppoe_makeppp(b
, size
, p
, l
, s
, t
, mtype
, prio
, bid
, mp_bits
);
2573 if (size
< 16) // Need more space than this!!
2575 LOG(0, s
, t
, "makeppp buffer too small for L2TP header (size=%d)\n", size
);
2579 if (prio
) hdr
|= 0x0100; // set priority bit
2581 *(uint16_t *) (b
+ 0) = htons(hdr
);
2582 *(uint16_t *) (b
+ 2) = htons(tunnel
[t
].far
); // tunnel
2583 *(uint16_t *) (b
+ 4) = htons(session
[s
].far
); // session
2586 // Check whether this session is part of multilink
2589 if (bundle
[bid
].num_of_links
> 1)
2590 type
= PPPMP
; // Change PPP message type to the PPPMP
2595 if (type
== PPPLCP
|| !(session
[s
].flags
& SESSION_ACFC
))
2597 *(uint16_t *) b
= htons(0xFF03); // HDLC header
2601 if (type
< 0x100 && session
[s
].flags
& SESSION_PFC
)
2607 *(uint16_t *) b
= htons(type
);
2613 // Set the sequence number and (B)egin (E)nd flags
2614 if (session
[s
].mssf
)
2616 // Set the multilink bits
2617 uint16_t bits_send
= mp_bits
;
2618 *(uint16_t *) b
= htons((bundle
[bid
].seq_num_t
& 0x0FFF)|bits_send
);
2623 *(uint32_t *) b
= htonl(bundle
[bid
].seq_num_t
);
2624 // Set the multilink bits
2629 bundle
[bid
].seq_num_t
++;
2631 // Add the message type if this fragment has the begin bit set
2632 if (mp_bits
& MP_BEGIN
)
2634 //*b++ = mtype; // The next two lines are instead of this
2635 *(uint16_t *) b
= htons(mtype
); // Message type
2640 if ((b
- start
) + l
> size
)
2642 LOG(2, s
, t
, "makeppp would overflow buffer (size=%d, header+payload=%td)\n", size
, (b
- start
) + l
);
2653 // fill in a L2TP message with a PPP frame,
2654 // returns start of PPP frame
2655 //(note: THIS ROUTINE CAN WRITES TO p[-28]).
2656 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
)
2658 uint16_t hdr
= 0x0002; // L2TP with no options
2659 uint16_t type
= mtype
;
2662 if (t
== TUNNEL_ID_PPPOE
)
2664 return opt_pppoe_makeppp(p
, l
, s
, t
, mtype
, prio
, bid
, mp_bits
);
2667 // Check whether this session is part of multilink
2670 if (bundle
[bid
].num_of_links
> 1)
2671 type
= PPPMP
; // Change PPP message type to the PPPMP
2678 // Add the message type if this fragment has the begin bit set
2679 if (mp_bits
& MP_BEGIN
)
2682 *(uint16_t *) b
= htons(mtype
); // Message type
2685 // Set the sequence number and (B)egin (E)nd flags
2686 if (session
[s
].mssf
)
2688 // Set the multilink bits
2689 uint16_t bits_send
= mp_bits
;
2691 *(uint16_t *) b
= htons((bundle
[bid
].seq_num_t
& 0x0FFF)|bits_send
);
2696 *(uint32_t *) b
= htonl(bundle
[bid
].seq_num_t
);
2697 // Set the multilink bits
2701 bundle
[bid
].seq_num_t
++;
2704 if (type
< 0x100 && session
[s
].flags
& SESSION_PFC
)
2712 *(uint16_t *) b
= htons(type
);
2715 if (type
== PPPLCP
|| !(session
[s
].flags
& SESSION_ACFC
))
2718 *(uint16_t *) b
= htons(0xFF03); // HDLC header
2721 if (prio
) hdr
|= 0x0100; // set priority bit
2723 *(uint16_t *) b
= htons(session
[s
].far
); // session
2725 *(uint16_t *) b
= htons(tunnel
[t
].far
); // tunnel
2727 *(uint16_t *) b
= htons(hdr
);
2732 static int add_lcp_auth(uint8_t *b
, int size
, int authtype
)
2735 if ((authtype
== AUTHCHAP
&& size
< 5) || size
< 4)
2738 *b
++ = 3; // Authentication-Protocol
2739 if (authtype
== AUTHCHAP
)
2741 len
= *b
++ = 5; // length
2742 *(uint16_t *) b
= htons(PPPCHAP
); b
+= 2;
2745 else if (authtype
== AUTHPAP
)
2747 len
= *b
++ = 4; // length
2748 *(uint16_t *) b
= htons(PPPPAP
); b
+= 2;
2752 LOG(0, 0, 0, "add_lcp_auth called with unsupported auth type %d\n", authtype
);
2758 // Send LCP ConfigReq for MRU, authentication type and magic no
2759 void sendlcp(sessionidt s
, tunnelidt t
)
2761 uint8_t b
[500], *q
, *l
;
2762 int authtype
= sess_local
[s
].lcp_authtype
;
2764 if (!(q
= makeppp(b
, sizeof(b
), NULL
, 0, s
, t
, PPPLCP
, 0, 0, 0)))
2767 LOG(3, s
, t
, "LCP: send ConfigReq%s%s%s including MP options\n",
2768 authtype
? " (" : "",
2769 authtype
? (authtype
== AUTHCHAP
? "CHAP" : "PAP") : "",
2770 authtype
? ")" : "");
2774 *l
++ = ++sess_local
[s
].lcp_ident
; // ID
2776 l
+= 2; //Save space for length
2778 if (sess_local
[s
].ppp_mru
)
2780 *l
++ = 1; *l
++ = 4; // Maximum-Receive-Unit (length 4)
2781 *(uint16_t *) l
= htons(sess_local
[s
].ppp_mru
); l
+= 2;
2785 l
+= add_lcp_auth(l
, sizeof(b
) - (l
- b
), authtype
);
2787 if (session
[s
].magic
)
2789 *l
++ = 5; *l
++ = 6; // Magic-Number (length 6)
2790 *(uint32_t *) l
= htonl(session
[s
].magic
);
2794 if (sess_local
[s
].mp_mrru
)
2796 *l
++ = 17; *l
++ = 4; // Multilink Max-Receive-Reconstructed-Unit (length 4)
2797 *(uint16_t *) l
= htons(sess_local
[s
].mp_mrru
); l
+= 2;
2800 if (sess_local
[s
].mp_epdis
)
2802 *l
++ = 19; *l
++ = 7; // Multilink Endpoint Discriminator (length 7)
2803 *l
++ = IPADDR
; // Endpoint Discriminator class
2804 *(uint32_t *) l
= htonl(sess_local
[s
].mp_epdis
);
2808 *(uint16_t *)(q
+ 2) = htons(l
- q
); // Length
2810 LOG_HEX(5, "PPPLCP", q
, l
- q
);
2811 if (config
->debug
> 3) dumplcp(q
, l
- q
);
2813 tunnelsend(b
, (l
- b
), t
);
2814 restart_timer(s
, lcp
);
2817 // Send CCP request for no compression
2818 void sendccp(sessionidt s
, tunnelidt t
)
2822 if (!(q
= makeppp(b
, sizeof(b
), NULL
, 0, s
, t
, PPPCCP
, 0, 0, 0)))
2825 LOG(3, s
, t
, "CCP: send ConfigReq (no compression)\n");
2828 *(q
+ 1) = ++sess_local
[s
].lcp_ident
; // ID
2829 *(uint16_t *)(q
+ 2) = htons(4); // Length
2831 LOG_HEX(5, "PPPCCP", q
, 4);
2832 tunnelsend(b
, (q
- b
) + 4 , t
);
2833 restart_timer(s
, ccp
);
2836 // Reject unknown/unconfigured protocols
2837 void protoreject(sessionidt s
, tunnelidt t
, uint8_t *p
, uint16_t l
, uint16_t proto
)
2840 uint8_t buf
[MAXETHER
];
2842 int mru
= session
[s
].mru
;
2843 if (mru
< MINMTU
) mru
= MINMTU
;
2844 if (mru
> sizeof(buf
)) mru
= sizeof(buf
);
2847 if (l
> mru
) l
= mru
;
2849 q
= makeppp(buf
, sizeof(buf
), 0, 0, s
, t
, PPPLCP
, 0, 0, 0);
2853 *(q
+ 1) = ++sess_local
[s
].lcp_ident
;
2854 *(uint16_t *)(q
+ 2) = htons(l
);
2855 *(uint16_t *)(q
+ 4) = htons(proto
);
2856 memcpy(q
+ 6, p
, l
- 6);
2858 if (proto
== PPPIPV6CP
)
2859 LOG(3, s
, t
, "LCP: send ProtocolRej (IPV6CP: not configured)\n");
2861 LOG(2, s
, t
, "LCP: sent ProtocolRej (0x%04X: unsupported)\n", proto
);
2863 tunnelsend(buf
, l
+ (q
- buf
), t
);