15 extern tunnelt
*tunnel
;
16 extern bundlet
*bundle
;
17 extern fragmentationt
*frag
;
18 extern sessiont
*session
;
19 extern radiust
*radius
;
21 extern char hostname
[];
22 extern uint32_t eth_tx
;
23 extern time_t time_now
;
24 extern uint64_t time_now_ms
;
25 extern configt
*config
;
27 static int add_lcp_auth(uint8_t *b
, int size
, int authtype
);
28 static bundleidt
new_bundle(void);
29 static int epdiscmp(epdist
, epdist
);
30 static void setepdis(epdist
*, epdist
);
31 static void ipcp_open(sessionidt s
, tunnelidt t
);
33 static int first_session_in_bundle(sessionidt s
)
36 for (i
= 1; i
< MAXBUNDLE
; i
++)
37 if (bundle
[i
].state
!= BUNDLEFREE
)
38 if (epdiscmp(session
[s
].epdis
,bundle
[i
].epdis
) && !strcmp(session
[s
].user
, bundle
[i
].user
))
43 // Process PAP messages
44 void processpap(sessionidt s
, tunnelidt t
, uint8_t *p
, uint16_t l
)
53 LOG_HEX(5, "PAP", p
, l
);
56 LOG(1, s
, t
, "Short PAP %u bytes\n", l
);
57 STAT(tunnel_rx_errors
);
58 sessionshutdown(s
, "Short PAP packet.", CDN_ADMIN_DISC
, TERM_USER_ERROR
);
62 if ((hl
= ntohs(*(uint16_t *) (p
+ 2))) > l
)
64 LOG(1, s
, t
, "Length mismatch PAP %u/%u\n", hl
, l
);
65 STAT(tunnel_rx_errors
);
66 sessionshutdown(s
, "PAP length mismatch.", CDN_ADMIN_DISC
, TERM_USER_ERROR
);
73 LOG(1, s
, t
, "Unexpected PAP code %d\n", *p
);
74 STAT(tunnel_rx_errors
);
75 sessionshutdown(s
, "Unexpected PAP code.", CDN_ADMIN_DISC
, TERM_USER_ERROR
);
79 if (session
[s
].ppp
.phase
!= Authenticate
)
81 LOG(2, s
, t
, "PAP ignored in %s phase\n", ppp_phase(session
[s
].ppp
.phase
));
88 user
[0] = pass
[0] = 0;
89 if (*b
&& *b
< sizeof(user
))
91 memcpy(user
, b
+ 1, *b
);
94 if (*b
&& *b
< sizeof(pass
))
96 memcpy(pass
, b
+ 1, *b
);
100 LOG(3, s
, t
, "PAP login %s/%s\n", user
, pass
);
103 if (session
[s
].ip
|| !(r
= radiusnew(s
)))
105 // respond now, either no RADIUS available or already authenticated
108 uint8_t *p
= makeppp(b
, sizeof(b
), 0, 0, s
, t
, PPPPAP
, 0, 0, 0);
114 *p
= 3; // cant authorise
116 *(uint16_t *) (p
+ 2) = htons(5); // length
117 p
[4] = 0; // no message
118 tunnelsend(b
, 5 + (p
- b
), t
); // send it
122 LOG(3, s
, t
, "Already an IP allocated: %s (%d)\n",
123 fmtaddr(htonl(session
[s
].ip
), 0), session
[s
].ip_pool_index
);
127 LOG(1, s
, t
, "No RADIUS session available to authenticate session...\n");
128 sessionshutdown(s
, "No free RADIUS sessions.", CDN_UNAVAILABLE
, TERM_SERVICE_UNAVAILABLE
);
133 // Run PRE_AUTH plugins
134 struct param_pre_auth packet
= { &tunnel
[t
], &session
[s
], strdup(user
), strdup(pass
), PPPPAP
, 1 };
135 run_plugins(PLUGIN_PRE_AUTH
, &packet
);
136 if (!packet
.continue_auth
)
138 LOG(3, s
, t
, "A plugin rejected PRE_AUTH\n");
139 if (packet
.username
) free(packet
.username
);
140 if (packet
.password
) free(packet
.password
);
144 strncpy(session
[s
].user
, packet
.username
, sizeof(session
[s
].user
) - 1);
145 strncpy(radius
[r
].pass
, packet
.password
, sizeof(radius
[r
].pass
) - 1);
147 free(packet
.username
);
148 free(packet
.password
);
151 LOG(3, s
, t
, "Sending login for %s/%s to RADIUS\n", user
, pass
);
152 if ((session
[s
].mrru
) && (!first_session_in_bundle(s
)))
153 radiussend(r
, RADIUSJUSTAUTH
);
155 radiussend(r
, RADIUSAUTH
);
159 // Process CHAP messages
160 void processchap(sessionidt s
, tunnelidt t
, uint8_t *p
, uint16_t l
)
167 LOG_HEX(5, "CHAP", p
, l
);
171 LOG(1, s
, t
, "Short CHAP %u bytes\n", l
);
172 STAT(tunnel_rx_errors
);
173 sessionshutdown(s
, "Short CHAP packet.", CDN_ADMIN_DISC
, TERM_USER_ERROR
);
177 if ((hl
= ntohs(*(uint16_t *) (p
+ 2))) > l
)
179 LOG(1, s
, t
, "Length mismatch CHAP %u/%u\n", hl
, l
);
180 STAT(tunnel_rx_errors
);
181 sessionshutdown(s
, "CHAP length mismatch.", CDN_ADMIN_DISC
, TERM_USER_ERROR
);
188 LOG(1, s
, t
, "Unexpected CHAP response code %d\n", *p
);
189 STAT(tunnel_rx_errors
);
190 sessionshutdown(s
, "CHAP length mismatch.", CDN_ADMIN_DISC
, TERM_USER_ERROR
);
194 if (session
[s
].ppp
.phase
!= Authenticate
)
196 LOG(2, s
, t
, "CHAP ignored in %s phase\n", ppp_phase(session
[s
].ppp
.phase
));
200 r
= sess_local
[s
].radius
;
203 LOG(3, s
, t
, "Unexpected CHAP message\n");
205 // Some modems (Netgear DM602, possibly others) persist in using CHAP even
206 // after ACKing our ConfigReq for PAP.
207 if (sess_local
[s
].lcp_authtype
== AUTHPAP
&& config
->radius_authtypes
& AUTHCHAP
)
209 sess_local
[s
].lcp_authtype
= AUTHCHAP
;
215 if (p
[1] != radius
[r
].id
)
217 LOG(1, s
, t
, "Wrong CHAP response ID %d (should be %d) (%d)\n", p
[1], radius
[r
].id
, r
);
218 STAT(tunnel_rx_errors
);
219 sessionshutdown(s
, "Unexpected CHAP response ID.", CDN_ADMIN_DISC
, TERM_USER_ERROR
);
223 if (l
< 5 || p
[4] != 16)
225 LOG(1, s
, t
, "Bad CHAP response length %d\n", l
< 5 ? -1 : p
[4]);
226 STAT(tunnel_rx_errors
);
227 sessionshutdown(s
, "Bad CHAP response length.", CDN_ADMIN_DISC
, TERM_USER_ERROR
);
233 if (l
< 16 || l
- 16 >= sizeof(session
[s
].user
))
235 LOG(1, s
, t
, "CHAP user too long %d\n", l
- 16);
236 STAT(tunnel_rx_errors
);
237 sessionshutdown(s
, "CHAP username too long.", CDN_ADMIN_DISC
, TERM_USER_ERROR
);
241 // Run PRE_AUTH plugins
243 struct param_pre_auth packet
= { &tunnel
[t
], &session
[s
], NULL
, NULL
, PPPCHAP
, 1 };
245 packet
.password
= calloc(17, 1);
246 memcpy(packet
.password
, p
, 16);
251 packet
.username
= calloc(l
+ 1, 1);
252 memcpy(packet
.username
, p
, l
);
254 run_plugins(PLUGIN_PRE_AUTH
, &packet
);
255 if (!packet
.continue_auth
)
257 LOG(3, s
, t
, "A plugin rejected PRE_AUTH\n");
258 if (packet
.username
) free(packet
.username
);
259 if (packet
.password
) free(packet
.password
);
263 strncpy(session
[s
].user
, packet
.username
, sizeof(session
[s
].user
) - 1);
264 memcpy(radius
[r
].pass
, packet
.password
, 16);
266 free(packet
.username
);
267 free(packet
.password
);
271 LOG(3, s
, t
, "CHAP login %s\n", session
[s
].user
);
272 if ((session
[s
].mrru
) && (!first_session_in_bundle(s
)))
273 radiussend(r
, RADIUSJUSTAUTH
);
275 radiussend(r
, RADIUSAUTH
);
278 static void dumplcp(uint8_t *p
, int l
)
281 uint8_t *o
= (p
+ 4);
283 LOG_HEX(5, "PPP LCP Packet", p
, l
);
284 LOG(4, 0, 0, "PPP LCP Packet type %d (%s len %d)\n", *p
, ppp_code((int)*p
), ntohs( ((uint16_t *) p
)[1]) );
285 LOG(4, 0, 0, "Length: %d\n", l
);
286 if (*p
!= ConfigReq
&& *p
!= ConfigRej
&& *p
!= ConfigAck
)
295 LOG(4, 0, 0, " Option length is %d...\n", length
);
300 LOG(4, 0, 0, " Option type is 0...\n");
307 case 1: // Maximum-Receive-Unit
309 LOG(4, 0, 0, " %s %d\n", ppp_lcp_option(type
), ntohs(*(uint16_t *)(o
+ 2)));
311 LOG(4, 0, 0, " %s odd length %d\n", ppp_lcp_option(type
), length
);
313 case 2: // Async-Control-Character-Map
316 uint32_t asyncmap
= ntohl(*(uint32_t *)(o
+ 2));
317 LOG(4, 0, 0, " %s %x\n", ppp_lcp_option(type
), asyncmap
);
320 LOG(4, 0, 0, " %s odd length %d\n", ppp_lcp_option(type
), length
);
322 case 3: // Authentication-Protocol
325 int proto
= ntohs(*(uint16_t *)(o
+ 2));
326 LOG(4, 0, 0, " %s 0x%x (%s)\n", ppp_lcp_option(type
), proto
,
327 proto
== PPPPAP
? "PAP" : "UNSUPPORTED");
329 else if (length
== 5)
331 int proto
= ntohs(*(uint16_t *)(o
+ 2));
333 LOG(4, 0, 0, " %s 0x%x 0x%x (%s)\n", ppp_lcp_option(type
), proto
, algo
,
334 (proto
== PPPCHAP
&& algo
== 5) ? "CHAP MD5" : "UNSUPPORTED");
337 LOG(4, 0, 0, " %s odd length %d\n", ppp_lcp_option(type
), length
);
339 case 4: // Quality-Protocol
341 uint32_t qp
= ntohl(*(uint32_t *)(o
+ 2));
342 LOG(4, 0, 0, " %s %x\n", ppp_lcp_option(type
), qp
);
345 case 5: // Magic-Number
348 uint32_t magicno
= ntohl(*(uint32_t *)(o
+ 2));
349 LOG(4, 0, 0, " %s %x\n", ppp_lcp_option(type
), magicno
);
352 LOG(4, 0, 0, " %s odd length %d\n", ppp_lcp_option(type
), length
);
354 case 7: // Protocol-Field-Compression
355 case 8: // Address-And-Control-Field-Compression
356 LOG(4, 0, 0, " %s\n", ppp_lcp_option(type
));
359 LOG(2, 0, 0, " Unknown PPP LCP Option type %d\n", type
);
367 void lcp_open(sessionidt s
, tunnelidt t
)
369 // transition to Authentication or Network phase:
370 session
[s
].ppp
.phase
= sess_local
[s
].lcp_authtype
? Authenticate
: Network
;
372 LOG(3, s
, t
, "LCP: Opened, phase %s\n", ppp_phase(session
[s
].ppp
.phase
));
375 change_state(s
, lcp
, Opened
);
377 if (session
[s
].ppp
.phase
== Authenticate
)
379 if (sess_local
[s
].lcp_authtype
== AUTHCHAP
)
384 if(session
[s
].bundle
== 0 || bundle
[session
[s
].bundle
].num_of_links
== 1)
388 change_state(s
, ipcp
, RequestSent
);
389 // move to passive state for IPv6 (if configured), CCP
390 if (config
->ipv6_prefix
.s6_addr
[0])
391 change_state(s
, ipv6cp
, Stopped
);
393 change_state(s
, ipv6cp
, Closed
);
395 change_state(s
, ccp
, Stopped
);
399 sessionidt first_ses
= bundle
[session
[s
].bundle
].members
[0];
400 LOG(3, s
, t
, "MPPP: Skipping IPCP negotiation for session:%d, first session of bundle is:%d\n",s
,first_ses
);
406 static void lcp_restart(sessionidt s
)
408 session
[s
].ppp
.phase
= Establish
;
410 change_state(s
, ipcp
, Dead
);
411 change_state(s
, ipv6cp
, Dead
);
412 change_state(s
, ccp
, Dead
);
415 static uint8_t *ppp_conf_rej(sessionidt s
, uint8_t *buf
, size_t blen
, uint16_t mtype
,
416 uint8_t **response
, uint8_t *queued
, uint8_t *packet
, uint8_t *option
)
418 if (!*response
|| **response
!= ConfigRej
)
420 queued
= *response
= makeppp(buf
, blen
, packet
, 2, s
, session
[s
].tunnel
, mtype
, 0, 0, 0);
428 if ((queued
- buf
+ option
[1]) > blen
)
430 LOG(2, s
, session
[s
].tunnel
, "PPP overflow for ConfigRej (proto %u, option %u).\n", mtype
, *option
);
434 memcpy(queued
, option
, option
[1]);
435 return queued
+ option
[1];
438 static uint8_t *ppp_conf_nak(sessionidt s
, uint8_t *buf
, size_t blen
, uint16_t mtype
,
439 uint8_t **response
, uint8_t *queued
, uint8_t *packet
, uint8_t *option
,
440 uint8_t *value
, size_t vlen
)
445 case PPPLCP
: nak_sent
= &sess_local
[s
].lcp
.nak_sent
; break;
446 case PPPIPCP
: nak_sent
= &sess_local
[s
].ipcp
.nak_sent
; break;
447 case PPPIPV6CP
: nak_sent
= &sess_local
[s
].ipv6cp
.nak_sent
; break;
448 default: return 0; // ?
451 if (*response
&& **response
!= ConfigNak
)
453 if (*nak_sent
< config
->ppp_max_failure
) // reject queued
456 return ppp_conf_rej(s
, buf
, blen
, mtype
, response
, 0, packet
, option
);
461 if (*nak_sent
>= config
->ppp_max_failure
)
462 return ppp_conf_rej(s
, buf
, blen
, mtype
, response
, 0, packet
, option
);
464 queued
= *response
= makeppp(buf
, blen
, packet
, 2, s
, session
[s
].tunnel
, mtype
, 0, 0, 0);
473 if ((queued
- buf
+ vlen
+ 2) > blen
)
475 LOG(2, s
, session
[s
].tunnel
, "PPP overflow for ConfigNak (proto %u, option %u).\n", mtype
, *option
);
480 *queued
++ = vlen
+ 2;
481 memcpy(queued
, value
, vlen
);
482 return queued
+ vlen
;
485 static void ppp_code_rej(sessionidt s
, tunnelidt t
, uint16_t proto
,
486 char *pname
, uint8_t *p
, uint16_t l
, uint8_t *buf
, size_t size
)
489 int mru
= session
[s
].mru
;
490 if (mru
< MINMTU
) mru
= MINMTU
;
491 if (mru
> size
) mru
= size
;
494 if (l
> mru
) l
= mru
;
496 q
= makeppp(buf
, size
, 0, 0, s
, t
, proto
, 0, 0, 0);
500 *(q
+ 1) = ++sess_local
[s
].lcp_ident
;
501 *(uint16_t *)(q
+ 2) = htons(l
);
502 memcpy(q
+ 4, p
, l
- 4);
504 LOG(2, s
, t
, "Unexpected %s code %s\n", pname
, ppp_code(*p
));
505 LOG(3, s
, t
, "%s: send %s\n", pname
, ppp_code(*q
));
506 if (config
->debug
> 3) dumplcp(q
, l
);
508 tunnelsend(buf
, l
+ (q
- buf
), t
);
511 // Process LCP messages
512 void processlcp(sessionidt s
, tunnelidt t
, uint8_t *p
, uint16_t l
)
520 LOG_HEX(5, "LCP", p
, l
);
523 LOG(1, s
, t
, "Short LCP %d bytes\n", l
);
524 STAT(tunnel_rx_errors
);
528 if ((hl
= ntohs(*(uint16_t *) (p
+ 2))) > l
)
530 LOG(1, s
, t
, "Length mismatch LCP %u/%u\n", hl
, l
);
531 STAT(tunnel_rx_errors
);
536 if (session
[s
].die
) // going down...
539 LOG((*p
== EchoReq
|| *p
== EchoReply
) ? 4 : 3, s
, t
,
540 "LCP: recv %s\n", ppp_code(*p
));
542 if (config
->debug
> 3) dumplcp(p
, l
);
547 uint8_t *o
= (p
+ 4);
555 if (length
== 0 || type
== 0 || x
< length
) break;
558 case 3: // Authentication-Protocol
560 int proto
= ntohs(*(uint16_t *)(o
+ 2));
563 else if (proto
== PPPCHAP
&& *(o
+ 4) == 5)
573 if (!session
[s
].ip
&& authtype
)
574 sess_local
[s
].lcp_authtype
= authtype
;
576 switch (session
[s
].ppp
.lcp
)
579 initialise_restart_count(s
, lcp
);
580 change_state(s
, lcp
, AckReceived
);
585 LOG(2, s
, t
, "LCP: ConfigAck in state %s? Sending ConfigReq\n", ppp_state(session
[s
].ppp
.lcp
));
586 if (session
[s
].ppp
.lcp
== Opened
)
590 change_state(s
, lcp
, RequestSent
);
598 LOG(2, s
, t
, "LCP: ignoring %s in state %s\n", ppp_code(*p
), ppp_state(session
[s
].ppp
.lcp
));
601 else if (*p
== ConfigReq
)
604 uint8_t *o
= (p
+ 4);
605 uint8_t *response
= 0;
606 static uint8_t asyncmap
[4] = { 0, 0, 0, 0 }; // all zero
607 static uint8_t authproto
[5];
615 if (length
== 0 || type
== 0 || x
< length
) break;
618 case 1: // Maximum-Receive-Unit
620 uint16_t mru
= ntohs(*(uint16_t *)(o
+ 2));
623 session
[s
].mru
= mru
;
628 LOG(3, s
, t
, " Remote requesting MRU of %u. Rejecting.\n", mru
);
630 q
= ppp_conf_nak(s
, b
, sizeof(b
), PPPLCP
, &response
, q
, p
, o
, (uint8_t *) &mru
, sizeof(mru
));
634 case 2: // Async-Control-Character-Map
635 if (!ntohl(*(uint32_t *)(o
+ 2))) // all bits zero is OK
638 LOG(3, s
, t
, " Remote requesting asyncmap. Rejecting.\n");
639 q
= ppp_conf_nak(s
, b
, sizeof(b
), PPPLCP
, &response
, q
, p
, o
, asyncmap
, sizeof(asyncmap
));
642 case 3: // Authentication-Protocol
644 int proto
= ntohs(*(uint16_t *)(o
+ 2));
645 char proto_name
[] = "0x0000";
650 if (config
->radius_authtypes
& AUTHPAP
)
652 sess_local
[s
].lcp_authtype
= AUTHPAP
;
656 strcpy(proto_name
, "PAP");
658 else if (proto
== PPPCHAP
)
660 if (config
->radius_authtypes
& AUTHCHAP
661 && *(o
+ 4) == 5) // MD5
663 sess_local
[s
].lcp_authtype
= AUTHCHAP
;
667 strcpy(proto_name
, "CHAP");
670 sprintf(proto_name
, "%#4.4x", proto
);
672 LOG(3, s
, t
, " Remote requesting %s authentication. Rejecting.\n", proto_name
);
674 alen
= add_lcp_auth(authproto
, sizeof(authproto
), config
->radius_authprefer
);
675 if (alen
< 2) break; // paranoia
677 q
= ppp_conf_nak(s
, b
, sizeof(b
), PPPLCP
, &response
, q
, p
, o
, authproto
+ 2, alen
- 2);
678 if (q
&& *response
== ConfigNak
&&
679 config
->radius_authtypes
!= config
->radius_authprefer
)
682 alen
= add_lcp_auth(authproto
, sizeof(authproto
), config
->radius_authtypes
& ~config
->radius_authprefer
);
684 q
= ppp_conf_nak(s
, b
, sizeof(b
), PPPLCP
, &response
, q
, p
, o
, authproto
+ 2, alen
- 2);
691 case 4: // Quality-Protocol
692 case 5: // Magic-Number
693 case 7: // Protocol-Field-Compression
694 case 8: // Address-And-Control-Field-Compression
697 case 17: // Multilink Max-Receive-Reconstructed-Unit
699 uint16_t mrru
= ntohs(*(uint16_t *)(o
+ 2));
700 session
[s
].mrru
= mrru
;
702 LOG(3, s
, t
, " Received PPP LCP option MRRU: %d\n",mrru
);
706 case 18: // Multilink Short Sequence Number Header Format
710 LOG(3, s
, t
, " Received PPP LCP option MSSN format\n");
714 case 19: // Multilink Endpoint Discriminator
716 uint8_t epdis_class
= o
[2];
719 session
[s
].epdis
.addr_class
= epdis_class
;
720 session
[s
].epdis
.length
= length
- 3;
721 if (session
[s
].epdis
.length
> 20)
723 LOG(1, s
, t
, "Error: received EndDis Address Length more than 20: %d\n", session
[s
].epdis
.length
);
724 session
[s
].epdis
.length
= 20;
727 for (addr
= 0; addr
< session
[s
].epdis
.length
; addr
++)
728 session
[s
].epdis
.address
[addr
] = o
[3+addr
];
735 LOG(3, s
, t
, " Received PPP LCP option Multilink EndDis Local Address Class: %d\n",epdis_class
);
738 LOG(3, s
, t
, " Received PPP LCP option Multilink EndDis IP Address Class: %d\n",epdis_class
);
741 LOG(3, s
, t
, " Received PPP LCP option Multilink EndDis IEEE MAC Address Class: %d\n",epdis_class
);
744 LOG(3, s
, t
, " Received PPP LCP option Multilink EndDis PPP Magic No Class: %d\n",epdis_class
);
747 LOG(3, s
, t
, " Received PPP LCP option Multilink EndDis PSND No Class: %d\n",epdis_class
);
750 LOG(3, s
, t
, " Received PPP LCP option Multilink EndDis NULL Class %d\n",epdis_class
);
755 default: // Reject any unknown options
756 LOG(3, s
, t
, " Rejecting unknown PPP LCP option %d\n", type
);
757 q
= ppp_conf_rej(s
, b
, sizeof(b
), PPPLCP
, &response
, q
, p
, o
);
764 cluster_send_session(s
);
768 l
= q
- response
; // LCP packet length
769 *((uint16_t *) (response
+ 2)) = htons(l
); // update header
773 // Send packet back as ConfigAck
774 response
= makeppp(b
, sizeof(b
), p
, l
, s
, t
, PPPLCP
, 0, 0, 0);
775 if (!response
) return;
776 *response
= ConfigAck
;
779 switch (session
[s
].ppp
.lcp
)
782 response
= makeppp(b
, sizeof(b
), p
, 2, s
, t
, PPPLCP
, 0, 0, 0);
783 if (!response
) return;
784 *response
= TerminateAck
;
785 *((uint16_t *) (response
+ 2)) = htons(l
= 4);
789 initialise_restart_count(s
, lcp
);
791 if (*response
== ConfigAck
)
792 change_state(s
, lcp
, AckSent
);
794 change_state(s
, lcp
, RequestSent
);
799 if (*response
== ConfigAck
)
800 change_state(s
, lcp
, AckSent
);
805 if (*response
== ConfigAck
)
816 if (*response
== ConfigAck
)
817 change_state(s
, lcp
, AckSent
);
819 change_state(s
, lcp
, RequestSent
);
824 sessionshutdown(s
, "LCP: ConfigReq in state Closing. This should not happen. Killing session.", CDN_ADMIN_DISC
, TERM_LOST_SERVICE
);
828 LOG(2, s
, t
, "LCP: ignoring %s in state %s\n", ppp_code(*p
), ppp_state(session
[s
].ppp
.lcp
));
832 LOG(3, s
, t
, "LCP: send %s\n", ppp_code(*response
));
833 if (config
->debug
> 3) dumplcp(response
, l
);
835 tunnelsend(b
, l
+ (response
- b
), t
);
837 else if (*p
== ConfigNak
|| *p
== ConfigRej
)
840 uint8_t *o
= (p
+ 4);
848 if (length
== 0 || type
== 0 || x
< length
) break;
851 case 1: // Maximum-Receive-Unit
854 if (length
< 4) break;
855 sess_local
[s
].ppp_mru
= ntohs(*(uint16_t *)(o
+ 2));
856 LOG(3, s
, t
, " Remote requested MRU of %u\n", sess_local
[s
].ppp_mru
);
860 sess_local
[s
].ppp_mru
= 0;
861 LOG(3, s
, t
, " Remote rejected MRU negotiation\n");
866 case 3: // Authentication-Protocol
874 if (length
< 4) break;
875 proto
= ntohs(*(uint16_t *)(o
+ 2));
879 authtype
= config
->radius_authtypes
& AUTHPAP
;
880 LOG(3, s
, t
, " Remote requested PAP authentication...%sing\n",
881 authtype
? "accept" : "reject");
883 else if (proto
== PPPCHAP
&& length
> 4 && *(o
+ 4) == 5)
885 authtype
= config
->radius_authtypes
& AUTHCHAP
;
886 LOG(3, s
, t
, " Remote requested CHAP authentication...%sing\n",
887 authtype
? "accept" : "reject");
891 LOG(3, s
, t
, " Rejecting unsupported authentication %#4x\n",
897 LOG(2, s
, t
, "LCP: remote rejected auth negotiation\n");
898 authtype
= 0; // shutdown
903 case 5: // Magic-Number
904 session
[s
].magic
= 0;
907 if (length
< 6) break;
908 session
[s
].magic
= ntohl(*(uint32_t *)(o
+ 2));
911 if (session
[s
].magic
)
912 LOG(3, s
, t
, " Remote requested magic-no %x\n", session
[s
].magic
);
914 LOG(3, s
, t
, " Remote rejected magic-no\n");
916 cluster_send_session(s
);
919 case 17: // Multilink Max-Receive-Reconstructed-Unit
923 sess_local
[s
].mp_mrru
= ntohs(*(uint16_t *)(o
+ 2));
924 LOG(3, s
, t
, " Remote requested MRRU of %u\n", sess_local
[s
].mp_mrru
);
928 sess_local
[s
].mp_mrru
= 0;
929 LOG(3, s
, t
, " Remote rejected MRRU negotiation\n");
934 case 18: // Multilink Short Sequence Number Header Format
938 sess_local
[s
].mp_mssf
= 0;
939 LOG(3, s
, t
, " Remote requested Naked mssf\n");
943 sess_local
[s
].mp_mssf
= 0;
944 LOG(3, s
, t
, " Remote rejected mssf\n");
949 case 19: // Multilink Endpoint Discriminator
953 LOG(2, s
, t
, " Remote should not configNak Endpoint Dis!\n");
957 sess_local
[s
].mp_epdis
= 0;
958 LOG(3, s
, t
, " Remote rejected Endpoint Discriminator\n");
964 LOG(2, s
, t
, "LCP: remote sent %s for type %u?\n", ppp_code(*p
), type
);
965 sessionshutdown(s
, "Unable to negotiate LCP.", CDN_ADMIN_DISC
, TERM_USER_ERROR
);
974 sessionshutdown(s
, "Unsupported authentication.", CDN_ADMIN_DISC
, TERM_USER_ERROR
);
979 sess_local
[s
].lcp_authtype
= authtype
;
981 switch (session
[s
].ppp
.lcp
)
986 uint8_t *response
= makeppp(b
, sizeof(b
), p
, 2, s
, t
, PPPLCP
, 0, 0, 0);
987 if (!response
) return;
988 *response
= TerminateAck
;
989 *((uint16_t *) (response
+ 2)) = htons(l
= 4);
991 LOG(3, s
, t
, "LCP: send %s\n", ppp_code(*response
));
992 if (config
->debug
> 3) dumplcp(response
, l
);
994 tunnelsend(b
, l
+ (response
- b
), t
);
1000 initialise_restart_count(s
, lcp
);
1005 LOG(2, s
, t
, "LCP: ConfigNak in state %s? Sending ConfigReq\n", ppp_state(session
[s
].ppp
.lcp
));
1015 LOG(2, s
, t
, "LCP: ignoring %s in state %s\n", ppp_code(*p
), ppp_state(session
[s
].ppp
.lcp
));
1019 else if (*p
== TerminateReq
)
1021 switch (session
[s
].ppp
.lcp
)
1034 zero_restart_count(s
, lcp
);
1035 change_state(s
, lcp
, Closing
);
1039 LOG(2, s
, t
, "LCP: ignoring %s in state %s\n", ppp_code(*p
), ppp_state(session
[s
].ppp
.lcp
));
1043 *p
= TerminateAck
; // send ack
1044 q
= makeppp(b
, sizeof(b
), p
, l
, s
, t
, PPPLCP
, 0, 0, 0);
1047 LOG(3, s
, t
, "LCP: send %s\n", ppp_code(*q
));
1048 if (config
->debug
> 3) dumplcp(q
, l
);
1050 tunnelsend(b
, l
+ (q
- b
), t
); // send it
1052 else if (*p
== ProtocolRej
)
1059 if (l
> 5 && !(proto
& 1))
1066 if (proto
== PPPIPV6CP
)
1068 LOG(3, s
, t
, "IPv6 rejected\n");
1069 change_state(s
, ipv6cp
, Closed
);
1073 LOG(3, s
, t
, "LCP protocol reject: 0x%04X\n", proto
);
1076 else if (*p
== EchoReq
)
1078 *p
= EchoReply
; // reply
1079 *(uint32_t *) (p
+ 4) = htonl(session
[s
].magic
); // our magic number
1080 q
= makeppp(b
, sizeof(b
), p
, l
, s
, t
, PPPLCP
, 0, 0, 0);
1083 LOG(4, s
, t
, "LCP: send %s\n", ppp_code(*q
));
1084 if (config
->debug
> 3) dumplcp(q
, l
);
1086 tunnelsend(b
, l
+ (q
- b
), t
); // send it
1088 else if (*p
== EchoReply
)
1090 // Ignore it, last_packet time is set earlier than this.
1092 else if (*p
!= CodeRej
)
1094 ppp_code_rej(s
, t
, PPPLCP
, "LCP", p
, l
, b
, sizeof(b
));
1098 int join_bundle(sessionidt s
)
1100 // Search for a bundle to join
1103 for (i
= 1; i
< MAXBUNDLE
; i
++)
1105 if (bundle
[i
].state
!= BUNDLEFREE
)
1107 if (epdiscmp(session
[s
].epdis
,bundle
[i
].epdis
) && !strcmp(session
[s
].user
, bundle
[i
].user
))
1109 sessionidt first_ses
= bundle
[i
].members
[0];
1110 if (bundle
[i
].mssf
!= session
[s
].mssf
)
1112 // uniformity of sequence number format must be insured
1113 LOG(3, s
, session
[s
].tunnel
, "MPPP: unable to bundle session %d in bundle %d cause of different mssf\n", s
, i
);
1116 session
[s
].bundle
= i
;
1117 session
[s
].ip
= session
[first_ses
].ip
;
1118 session
[s
].dns1
= session
[first_ses
].dns1
;
1119 session
[s
].dns2
= session
[first_ses
].dns2
;
1120 session
[s
].timeout
= session
[first_ses
].timeout
;
1122 if(session
[s
].epdis
.length
> 0)
1123 setepdis(&bundle
[i
].epdis
, session
[s
].epdis
);
1125 strcpy(bundle
[i
].user
, session
[s
].user
);
1126 bundle
[i
].members
[bundle
[i
].num_of_links
] = s
;
1127 bundle
[i
].num_of_links
++;
1128 LOG(3, s
, session
[s
].tunnel
, "MPPP: Bundling additional line in bundle (%d), lines:%d\n",i
,bundle
[i
].num_of_links
);
1134 // No previously created bundle was found for this session, so create a new one
1135 if (!(b
= new_bundle())) return 0;
1137 session
[s
].bundle
= b
;
1138 bundle
[b
].mrru
= session
[s
].mrru
;
1139 bundle
[b
].mssf
= session
[s
].mssf
;
1140 // FIXME !!! to enable l2tpns reading mssf frames receiver_max_seq, sender_max_seq must be introduce
1141 // 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
1144 bundle[b].max_seq = 1 << 12;
1146 bundle
[b
].max_seq
= 1 << 24;
1147 if(session
[s
].epdis
.length
> 0)
1148 setepdis(&bundle
[b
].epdis
, session
[s
].epdis
);
1150 strcpy(bundle
[b
].user
, session
[s
].user
);
1151 bundle
[b
].members
[0] = s
;
1152 bundle
[b
].timeout
= session
[s
].timeout
;
1153 LOG(3, s
, session
[s
].tunnel
, "MPPP: Created a new bundle (%d)\n", b
);
1157 static int epdiscmp(epdist ep1
, epdist ep2
)
1160 if (ep1
.length
!= ep2
.length
)
1163 if (ep1
.addr_class
!= ep2
.addr_class
)
1166 for (ad
= 0; ad
< ep1
.length
; ad
++)
1167 if (ep1
.address
[ad
] != ep2
.address
[ad
])
1173 static void setepdis(epdist
*ep1
, epdist ep2
)
1176 ep1
->length
= ep2
.length
;
1177 ep1
->addr_class
= ep2
.addr_class
;
1178 for (ad
= 0; ad
< ep2
.length
; ad
++)
1179 ep1
->address
[ad
] = ep2
.address
[ad
];
1182 static bundleidt
new_bundle()
1185 for (i
= 1; i
< MAXBUNDLE
; i
++)
1187 if (bundle
[i
].state
== BUNDLEFREE
)
1189 LOG(4, 0, 0, "MPPP: Assigning bundle ID %d\n", i
);
1190 bundle
[i
].num_of_links
= 1;
1191 bundle
[i
].last_check
= time_now
; // Initialize last_check value
1192 bundle
[i
].state
= BUNDLEOPEN
;
1193 bundle
[i
].current_ses
= -1; // This is to enforce the first session 0 to be used at first
1194 memset(&frag
[i
], 0, sizeof(fragmentationt
));
1195 if (i
> config
->cluster_highest_bundleid
)
1196 config
->cluster_highest_bundleid
= i
;
1200 LOG(0, 0, 0, "MPPP: Can't find a free bundle! There shouldn't be this many in use!\n");
1204 static void ipcp_open(sessionidt s
, tunnelidt t
)
1206 LOG(3, s
, t
, "IPCP: Opened, session is now active\n");
1208 change_state(s
, ipcp
, Opened
);
1210 if (!(session
[s
].walled_garden
|| session
[s
].flags
& SESSION_STARTED
))
1212 uint16_t r
= radiusnew(s
);
1215 radiussend(r
, RADIUSSTART
); // send radius start
1217 // don't send further Start records if IPCP is restarted
1218 session
[s
].flags
|= SESSION_STARTED
;
1219 cluster_send_session(s
);
1223 // start IPv6 if configured and still in passive state
1224 if (session
[s
].ppp
.ipv6cp
== Stopped
)
1227 change_state(s
, ipv6cp
, RequestSent
);
1231 // Process IPCP messages
1232 void processipcp(sessionidt s
, tunnelidt t
, uint8_t *p
, uint16_t l
)
1234 uint8_t b
[MAXETHER
];
1240 LOG_HEX(5, "IPCP", p
, l
);
1243 LOG(1, s
, t
, "Short IPCP %d bytes\n", l
);
1244 STAT(tunnel_rx_errors
);
1248 if ((hl
= ntohs(*(uint16_t *) (p
+ 2))) > l
)
1250 LOG(1, s
, t
, "Length mismatch IPCP %u/%u\n", hl
, l
);
1251 STAT(tunnel_rx_errors
);
1256 if (session
[s
].ppp
.phase
< Network
)
1258 LOG(2, s
, t
, "IPCP %s ignored in %s phase\n", ppp_code(*p
), ppp_phase(session
[s
].ppp
.phase
));
1262 LOG(3, s
, t
, "IPCP: recv %s\n", ppp_code(*p
));
1264 if (*p
== ConfigAck
)
1266 switch (session
[s
].ppp
.ipcp
)
1269 initialise_restart_count(s
, ipcp
);
1270 change_state(s
, ipcp
, AckReceived
);
1275 LOG(2, s
, t
, "IPCP: ConfigAck in state %s? Sending ConfigReq\n", ppp_state(session
[s
].ppp
.ipcp
));
1277 change_state(s
, ipcp
, RequestSent
);
1285 LOG(2, s
, t
, "IPCP: ignoring %s in state %s\n", ppp_code(*p
), ppp_state(session
[s
].ppp
.ipcp
));
1288 else if (*p
== ConfigReq
)
1290 uint8_t *response
= 0;
1298 if (!o
[1] || o
[1] > length
) return;
1302 case 3: // ip address
1303 gotip
++; // seen address
1304 if (o
[1] != 6) return;
1306 addr
= htonl(session
[s
].ip
);
1307 if (memcmp(o
+ 2, &addr
, (sizeof addr
)))
1310 q
= ppp_conf_nak(s
, b
, sizeof(b
), PPPIPCP
, &response
, q
, p
, o
, (uint8_t *) &addr
, sizeof(addr
));
1311 if (!q
|| (q
!= oq
&& *response
== ConfigRej
))
1313 sessionshutdown(s
, "Can't negotiate IPCP.", CDN_ADMIN_DISC
, TERM_USER_ERROR
);
1320 case 129: // primary DNS
1321 if (o
[1] != 6) return;
1323 addr
= htonl(session
[s
].dns1
);
1324 if (memcmp(o
+ 2, &addr
, (sizeof addr
)))
1326 q
= ppp_conf_nak(s
, b
, sizeof(b
), PPPIPCP
, &response
, q
, p
, o
, (uint8_t *) &addr
, sizeof(addr
));
1332 case 131: // secondary DNS
1333 if (o
[1] != 6) return;
1335 addr
= htonl(session
[s
].dns2
);
1336 if (memcmp(o
+ 2, &addr
, sizeof(addr
)))
1338 q
= ppp_conf_nak(s
, b
, sizeof(b
), PPPIPCP
, &response
, q
, p
, o
, (uint8_t *) &addr
, sizeof(addr
));
1345 LOG(2, s
, t
, " Rejecting PPP IPCP Option type %d\n", *o
);
1346 q
= ppp_conf_rej(s
, b
, sizeof(b
), PPPIPCP
, &response
, q
, p
, o
);
1356 l
= q
- response
; // IPCP packet length
1357 *((uint16_t *) (response
+ 2)) = htons(l
); // update header
1361 // Send packet back as ConfigAck
1362 response
= makeppp(b
, sizeof(b
), p
, l
, s
, t
, PPPIPCP
, 0, 0, 0);
1363 if (!response
) return;
1364 *response
= ConfigAck
;
1368 LOG(1, s
, t
, "No IP in IPCP request\n");
1369 STAT(tunnel_rx_errors
);
1373 switch (session
[s
].ppp
.ipcp
)
1376 response
= makeppp(b
, sizeof(b
), p
, 2, s
, t
, PPPIPCP
, 0, 0, 0);
1377 if (!response
) return;
1378 *response
= TerminateAck
;
1379 *((uint16_t *) (response
+ 2)) = htons(l
= 4);
1383 initialise_restart_count(s
, ipcp
);
1385 if (*response
== ConfigAck
)
1386 change_state(s
, ipcp
, AckSent
);
1388 change_state(s
, ipcp
, RequestSent
);
1393 if (*response
== ConfigAck
)
1394 change_state(s
, ipcp
, AckSent
);
1399 if (*response
== ConfigAck
)
1405 initialise_restart_count(s
, ipcp
);
1410 if (*response
== ConfigAck
)
1411 change_state(s
, ipcp
, AckSent
);
1413 change_state(s
, ipcp
, RequestSent
);
1418 LOG(2, s
, t
, "IPCP: ignoring %s in state %s\n", ppp_code(*p
), ppp_state(session
[s
].ppp
.ipcp
));
1422 LOG(3, s
, t
, "IPCP: send %s\n", ppp_code(*response
));
1423 tunnelsend(b
, l
+ (response
- b
), t
);
1425 else if (*p
== TerminateReq
)
1427 switch (session
[s
].ppp
.ipcp
)
1439 zero_restart_count(s
, ipcp
);
1440 change_state(s
, ipcp
, Closing
);
1444 LOG(2, s
, t
, "IPCP: ignoring %s in state %s\n", ppp_code(*p
), ppp_state(session
[s
].ppp
.ipcp
));
1448 *p
= TerminateAck
; // send ack
1449 q
= makeppp(b
, sizeof(b
), p
, l
, s
, t
, PPPIPCP
, 0, 0, 0);
1452 LOG(3, s
, t
, "IPCP: send %s\n", ppp_code(*q
));
1453 tunnelsend(b
, l
+ (q
- b
), t
); // send it
1455 else if (*p
!= CodeRej
)
1457 ppp_code_rej(s
, t
, PPPIPCP
, "IPCP", p
, l
, b
, sizeof(b
));
1461 static void ipv6cp_open(sessionidt s
, tunnelidt t
)
1463 LOG(3, s
, t
, "IPV6CP: Opened\n");
1465 change_state(s
, ipv6cp
, Opened
);
1466 if (session
[s
].ipv6prefixlen
)
1467 route6set(s
, session
[s
].ipv6route
, session
[s
].ipv6prefixlen
, 1);
1469 // Send an initial RA (TODO: Should we send these regularly?)
1470 send_ipv6_ra(s
, t
, NULL
);
1473 // Process IPV6CP messages
1474 void processipv6cp(sessionidt s
, tunnelidt t
, uint8_t *p
, uint16_t l
)
1476 uint8_t b
[MAXETHER
];
1480 CSTAT(processipv6cp
);
1482 LOG_HEX(5, "IPV6CP", p
, l
);
1485 LOG(1, s
, t
, "Short IPV6CP %d bytes\n", l
);
1486 STAT(tunnel_rx_errors
);
1490 if ((hl
= ntohs(*(uint16_t *) (p
+ 2))) > l
)
1492 LOG(1, s
, t
, "Length mismatch IPV6CP %u/%u\n", hl
, l
);
1493 STAT(tunnel_rx_errors
);
1498 if (session
[s
].ppp
.phase
< Network
)
1500 LOG(2, s
, t
, "IPV6CP %s ignored in %s phase\n", ppp_code(*p
), ppp_phase(session
[s
].ppp
.phase
));
1504 LOG(3, s
, t
, "IPV6CP: recv %s\n", ppp_code(*p
));
1508 LOG(3, s
, t
, "IPV6CP: no IPv4 address (IPCP in state %s)\n", ppp_state(session
[s
].ppp
.ipcp
));
1509 return; // need IPCP to complete...
1512 if (*p
== ConfigAck
)
1514 switch (session
[s
].ppp
.ipv6cp
)
1517 initialise_restart_count(s
, ipv6cp
);
1518 change_state(s
, ipv6cp
, AckReceived
);
1523 LOG(2, s
, t
, "IPV6CP: ConfigAck in state %s? Sending ConfigReq\n", ppp_state(session
[s
].ppp
.ipv6cp
));
1525 change_state(s
, ipv6cp
, RequestSent
);
1533 LOG(2, s
, t
, "IPV6CP: ignoring %s in state %s\n", ppp_code(*p
), ppp_state(session
[s
].ppp
.ipv6cp
));
1536 else if (*p
== ConfigReq
)
1538 uint8_t *response
= 0;
1546 if (!o
[1] || o
[1] > length
) return;
1550 case 1: // interface identifier
1551 gotip
++; // seen address
1552 if (o
[1] != 10) return;
1554 ident
[0] = htonl(session
[s
].ip
);
1557 if (memcmp(o
+ 2, ident
, sizeof(ident
)))
1559 q
= ppp_conf_nak(s
, b
, sizeof(b
), PPPIPV6CP
, &response
, q
, p
, o
, (uint8_t *)ident
, sizeof(ident
));
1566 LOG(2, s
, t
, " Rejecting PPP IPV6CP Option type %d\n", *o
);
1567 q
= ppp_conf_rej(s
, b
, sizeof(b
), PPPIPV6CP
, &response
, q
, p
, o
);
1577 l
= q
- response
; // IPV6CP packet length
1578 *((uint16_t *) (response
+ 2)) = htons(l
); // update header
1582 // Send packet back as ConfigAck
1583 response
= makeppp(b
, sizeof(b
), p
, l
, s
, t
, PPPIPV6CP
, 0, 0, 0);
1584 if (!response
) return;
1585 *response
= ConfigAck
;
1589 LOG(1, s
, t
, "No interface identifier in IPV6CP request\n");
1590 STAT(tunnel_rx_errors
);
1594 switch (session
[s
].ppp
.ipv6cp
)
1597 response
= makeppp(b
, sizeof(b
), p
, 2, s
, t
, PPPIPV6CP
, 0, 0, 0);
1598 if (!response
) return;
1599 *response
= TerminateAck
;
1600 *((uint16_t *) (response
+ 2)) = htons(l
= 4);
1604 initialise_restart_count(s
, ipv6cp
);
1606 if (*response
== ConfigAck
)
1607 change_state(s
, ipv6cp
, AckSent
);
1609 change_state(s
, ipv6cp
, RequestSent
);
1614 if (*response
== ConfigAck
)
1615 change_state(s
, ipv6cp
, AckSent
);
1620 if (*response
== ConfigAck
)
1626 initialise_restart_count(s
, ipv6cp
);
1631 if (*response
== ConfigAck
)
1632 change_state(s
, ipv6cp
, AckSent
);
1634 change_state(s
, ipv6cp
, RequestSent
);
1639 LOG(2, s
, t
, "IPV6CP: ignoring %s in state %s\n", ppp_code(*p
), ppp_state(session
[s
].ppp
.ipv6cp
));
1643 LOG(3, s
, t
, "IPV6CP: send %s\n", ppp_code(*response
));
1644 tunnelsend(b
, l
+ (response
- b
), t
);
1646 else if (*p
== TerminateReq
)
1648 switch (session
[s
].ppp
.ipv6cp
)
1660 zero_restart_count(s
, ipv6cp
);
1661 change_state(s
, ipv6cp
, Closing
);
1665 LOG(2, s
, t
, "IPV6CP: ignoring %s in state %s\n", ppp_code(*p
), ppp_state(session
[s
].ppp
.ipv6cp
));
1669 *p
= TerminateAck
; // send ack
1670 q
= makeppp(b
, sizeof(b
), p
, l
, s
, t
, PPPIPV6CP
, 0, 0, 0);
1673 LOG(3, s
, t
, "IPV6CP: send %s\n", ppp_code(*q
));
1674 tunnelsend(b
, l
+ (q
- b
), t
); // send it
1676 else if (*p
!= CodeRej
)
1678 ppp_code_rej(s
, t
, PPPIPV6CP
, "IPV6CP", p
, l
, b
, sizeof(b
));
1682 static void update_sessions_in_stat(sessionidt s
, uint16_t l
)
1684 bundleidt b
= session
[s
].bundle
;
1687 increment_counter(&session
[s
].cin
, &session
[s
].cin_wrap
, l
);
1688 session
[s
].cin_delta
+= l
;
1691 sess_local
[s
].cin
+= l
;
1692 sess_local
[s
].pin
++;
1696 int i
= frag
[b
].re_frame_begin_index
;
1697 int end
= frag
[b
].re_frame_end_index
;
1700 l
= frag
[b
].fragment
[i
].length
;
1701 s
= frag
[b
].fragment
[i
].sid
;
1702 increment_counter(&session
[s
].cin
, &session
[s
].cin_wrap
, l
);
1703 session
[s
].cin_delta
+= l
;
1706 sess_local
[s
].cin
+= l
;
1707 sess_local
[s
].pin
++;
1710 i
= (i
+ 1) & MAXFRAGNUM_MASK
;
1715 // process IP packet received
1717 // This MUST be called with at least 4 byte behind 'p'.
1718 // (i.e. this routine writes to p[-4]).
1719 void processipin(sessionidt s
, tunnelidt t
, uint8_t *p
, uint16_t l
)
1725 LOG_HEX(5, "IP", p
, l
);
1729 LOG(1, s
, t
, "IP packet too short %d\n", l
);
1730 STAT(tunnel_rx_errors
);
1734 ip
= ntohl(*(uint32_t *)(p
+ 12));
1738 LOG(1, s
, t
, "IP packet too long %d\n", l
);
1739 STAT(tunnel_rx_errors
);
1743 if (session
[s
].ppp
.phase
!= Network
|| session
[s
].ppp
.ipcp
!= Opened
)
1746 if (!session
[s
].bundle
|| bundle
[session
[s
].bundle
].num_of_links
< 2) // FIXME:
1748 // no spoof (do sessionbyip to handled statically routed subnets)
1749 if (ip
!= session
[s
].ip
&& sessionbyip(htonl(ip
)) != s
)
1751 LOG(4, s
, t
, "Dropping packet with spoofed IP %s\n", fmtaddr(htonl(ip
), 0));
1756 // run access-list if any
1757 if (session
[s
].filter_in
&& !ip_filter(p
, l
, session
[s
].filter_in
- 1))
1760 // adjust MSS on SYN and SYN,ACK packets with options
1761 if ((ntohs(*(uint16_t *) (p
+ 6)) & 0x1fff) == 0 && p
[9] == IPPROTO_TCP
) // first tcp fragment
1763 int ihl
= (p
[0] & 0xf) * 4; // length of IP header
1764 if (l
>= ihl
+ 20 && (p
[ihl
+ 13] & TCP_FLAG_SYN
) && ((p
[ihl
+ 12] >> 4) > 5))
1765 adjust_tcp_mss(s
, t
, p
, l
, p
+ ihl
);
1768 // Add on the tun header
1770 *(uint32_t *) p
= htonl(PKTIP
);
1773 if (session
[s
].tbf_in
)
1775 // Are we throttling this session?
1776 if (config
->cluster_iam_master
)
1777 tbf_queue_packet(session
[s
].tbf_in
, p
, l
);
1779 master_throttle_packet(session
[s
].tbf_in
, p
, l
);
1784 if (tun_write(p
, l
) < 0)
1786 STAT(tun_tx_errors
);
1787 LOG(0, s
, t
, "Error writing %d bytes to TUN device: %s (tunfd=%d, p=%p)\n",
1788 l
, strerror(errno
), tunfd
, p
);
1796 if (session
[s
].snoop_ip
&& session
[s
].snoop_port
)
1798 // Snooping this session
1799 snoop_send_packet(p
, l
, session
[s
].snoop_ip
, session
[s
].snoop_port
);
1802 update_sessions_in_stat(s
, l
);
1806 STAT(tun_tx_packets
);
1807 INC_STAT(tun_tx_bytes
, l
);
1810 // process Multilink PPP packet received
1811 void processmpin(sessionidt s
, tunnelidt t
, uint8_t *p
, uint16_t l
)
1813 bundleidt b
= session
[s
].bundle
;
1814 bundlet
* this_bundle
= &bundle
[b
];
1815 uint32_t maskSeq
, max_seq
;
1817 uint16_t frag_index
, frag_index_next
, frag_index_prev
;
1818 fragmentationt
*this_fragmentation
= &frag
[b
];
1819 uint8_t begin_frame
= (*p
& MP_BEGIN
);
1820 uint8_t end_frame
= (*p
& MP_END
);
1821 uint32_t seq_num
, seq_num_next
, seq_num_prev
;
1824 uint16_t begin_index
, end_index
;
1826 // Perform length checking
1829 LOG(2, s
, t
, "MPPP: discarding fragment larger than MAXFRAGLEN\n");
1835 LOG(2, s
, t
, "MPPP: Invalid bundle id: 0\n");
1838 // FIXME !! session[s].mssf means that the receiver wants to receive frames in mssf not means the receiver will send frames in mssf
1839 /* if(session[s].mssf)
1841 // Get 12 bit for seq number
1842 seq_num = ntohs((*(uint16_t *) p) & 0xFF0F);
1845 // After this point the pointer should be advanced 2 bytes
1846 LOG(3, s, t, "MPPP: 12 bits, sequence number: %d\n",seq_num);
1850 // Get 24 bit for seq number
1851 seq_num
= ntohl((*(uint32_t *) p
) & 0xFFFFFF00);
1854 // After this point the pointer should be advanced 4 bytes
1855 LOG(4, s
, t
, "MPPP: 24 bits sequence number:%d\n",seq_num
);
1858 max_seq
= this_bundle
->max_seq
;
1859 maskSeq
= max_seq
- 1;
1862 * Expand sequence number to 32 bits, making it as close
1863 * as possible to this_fragmentation->M.
1865 seq_num
|= this_fragmentation
->M
& ~maskSeq
;
1866 if ((int)(this_fragmentation
->M
- seq_num
) > (int)(maskSeq
>> 1))
1868 seq_num
+= maskSeq
+ 1;
1870 else if ((int)(seq_num
- this_fragmentation
->M
) > (int)(maskSeq
>> 1))
1872 seq_num
-= maskSeq
+ 1; /* should never happen */
1875 // calculate this fragment's offset from the begin seq in the bundle
1876 frag_offset
= (int) (seq_num
- this_fragmentation
->start_seq
);
1878 sess_local
[s
].last_seq
= seq_num
;
1880 // calculate the jitter average
1881 uint32_t ljitter
= time_now_ms
- sess_local
[s
].prev_time
;
1882 sess_local
[s
].jitteravg
= (sess_local
[s
].jitteravg
+ ljitter
)>>1;
1883 sess_local
[s
].prev_time
= time_now_ms
;
1887 if (seq_num
< this_fragmentation
->M
)
1890 this_fragmentation
->M
= seq_num
;
1894 Mmin
= sess_local
[(this_bundle
->members
[0])].last_seq
;
1895 for (i
= 1; i
< this_bundle
->num_of_links
; i
++)
1897 uint32_t s_seq
= sess_local
[(this_bundle
->members
[i
])].last_seq
;
1901 this_fragmentation
->M
= Mmin
;
1904 // calculate M offset of the M seq in the bundle
1905 int M_offset
= (int) (Mmin
- this_fragmentation
->start_seq
);
1907 if (M_offset
>= MAXFRAGNUM
)
1909 // There have a long break of the link !!!!!!!!
1910 // M_offset is bigger that the fragmentation buffer size
1911 LOG(3, s
, t
, "MPPP: M_offset out of range, min:%d, begin_seq:%d, jitteravg:%d\n", Mmin
, this_fragmentation
->start_seq
, sess_local
[s
].jitteravg
);
1913 // Calculate the new start index, the previous frag are lost
1914 begin_index
= (M_offset
+ this_fragmentation
->start_index
) & MAXFRAGNUM_MASK
;
1916 // Set new Start sequence
1917 this_fragmentation
->start_index
= begin_index
;
1918 this_fragmentation
->start_seq
= Mmin
;
1920 // recalculate the fragment offset from the new begin seq in the bundle
1921 frag_offset
= (int) (seq_num
- Mmin
);
1924 // discard this fragment if the packet comes before the start sequence
1925 if (frag_offset
< 0)
1927 // this packet comes before the next
1928 LOG(3, s
, t
, "MPPP: packet comes before the next, seq:%d, begin_seq:%d, size frag:%d\n", seq_num
, this_fragmentation
->start_seq
, l
);
1932 // discard if frag_offset is bigger that the fragmentation buffer size
1933 if (frag_offset
>= MAXFRAGNUM
)
1935 // frag_offset is bigger that the fragmentation buffer size
1936 LOG(3, s
, t
, "MPPP: Index out of range, seq:%d, begin_seq:%d, jitteravg:%d\n", seq_num
, this_fragmentation
->start_seq
, sess_local
[s
].jitteravg
);
1940 //caculate received fragment's index in the fragment array
1941 frag_index
= (frag_offset
+ this_fragmentation
->start_index
) & MAXFRAGNUM_MASK
;
1943 // insert the frame in it's place
1944 fragmentt
*this_frag
= &this_fragmentation
->fragment
[frag_index
];
1946 if (this_frag
->length
> 0)
1947 // This fragment is lost, It was around the buffer and it was never completed the packet.
1948 LOG(3, this_frag
->sid
, this_frag
->tid
, "MPPP: (INSERT) seq_num:%d frag_index:%d flags:%d jitteravg:%d is LOST\n",
1949 this_frag
->seq
, frag_index
, this_frag
->flags
, this_frag
->jitteravg
);
1951 this_frag
->length
= l
;
1954 this_frag
->flags
= flags
;
1955 this_frag
->seq
= seq_num
;
1956 this_frag
->jitteravg
= sess_local
[s
].jitteravg
;
1957 memcpy(this_frag
->data
, p
, l
);
1959 LOG(4, s
, t
, "MPPP: seq_num:%d frag_index:%d INSERTED flags: %02X\n", seq_num
, frag_index
, flags
);
1962 frag_index_next
= (frag_index
+ 1) & MAXFRAGNUM_MASK
;
1963 //previous frag index
1964 frag_index_prev
= (frag_index
- 1) & MAXFRAGNUM_MASK
;
1966 seq_num_next
= seq_num
+ 1;
1968 seq_num_prev
= seq_num
- 1;
1970 // Clean the buffer and log the lost fragments
1971 if ((frag_index_next
!= this_fragmentation
->start_index
) && this_fragmentation
->fragment
[frag_index_next
].length
)
1973 // check if the next frag is a lost fragment
1974 if (this_fragmentation
->fragment
[frag_index_next
].seq
!= seq_num_next
)
1976 // This fragment is lost, It was around the buffer and it was never completed the packet.
1977 LOG(3, this_fragmentation
->fragment
[frag_index_next
].sid
, this_fragmentation
->fragment
[frag_index_next
].tid
,
1978 "MPPP: (NEXT) seq_num:%d frag_index:%d flags:%d jitteravg:%d is LOST\n",
1979 this_fragmentation
->fragment
[frag_index_next
].seq
, frag_index_next
,
1980 this_fragmentation
->fragment
[frag_index_next
].flags
, this_fragmentation
->fragment
[frag_index_next
].jitteravg
);
1981 // this frag is lost
1982 this_fragmentation
->fragment
[frag_index_next
].length
= 0;
1983 this_fragmentation
->fragment
[frag_index_next
].flags
= 0;
1985 if (begin_frame
&& (!end_frame
)) return; // assembling frame failed
1989 // Clean the buffer and log the lost fragments
1990 if ((frag_index
!= this_fragmentation
->start_index
) && this_fragmentation
->fragment
[frag_index_prev
].length
)
1992 // check if the next frag is a lost fragment
1993 if (this_fragmentation
->fragment
[frag_index_prev
].seq
!= seq_num_prev
)
1995 // This fragment is lost, It was around the buffer and it was never completed the packet.
1996 LOG(3, this_fragmentation
->fragment
[frag_index_prev
].sid
, this_fragmentation
->fragment
[frag_index_prev
].tid
,
1997 "MPPP: (PREV) seq_num:%d frag_index:%d flags:%d jitteravg:%d is LOST\n",
1998 this_fragmentation
->fragment
[frag_index_prev
].seq
, frag_index_prev
,
1999 this_fragmentation
->fragment
[frag_index_prev
].flags
, this_fragmentation
->fragment
[frag_index_prev
].jitteravg
);
2001 this_fragmentation
->fragment
[frag_index_prev
].length
= 0;
2002 this_fragmentation
->fragment
[frag_index_prev
].flags
= 0;
2004 if (end_frame
&& (!begin_frame
)) return; // assembling frame failed
2009 begin_index
= this_fragmentation
->start_index
;
2010 uint32_t b_seq
= this_fragmentation
->start_seq
;
2011 // Try to find a Begin sequence from the start_seq sequence to M sequence
2012 while (b_seq
< Mmin
)
2014 if (this_fragmentation
->fragment
[begin_index
].length
)
2016 if (b_seq
== this_fragmentation
->fragment
[begin_index
].seq
)
2018 if (this_fragmentation
->fragment
[begin_index
].flags
& MP_BEGIN
)
2021 // Adjust the new start sequence and start index
2022 this_fragmentation
->start_index
= begin_index
;
2023 this_fragmentation
->start_seq
= b_seq
;
2024 // Begin Sequence found, now try to found the End Sequence to complete the frame
2025 end_index
= begin_index
;
2026 while (b_seq
< Mmin
)
2028 if (this_fragmentation
->fragment
[end_index
].length
)
2030 if (b_seq
== this_fragmentation
->fragment
[end_index
].seq
)
2032 if (this_fragmentation
->fragment
[end_index
].flags
& MP_END
)
2034 // The End sequence was found and the frame is complete
2041 // This fragment is lost, it was never completed the packet.
2042 LOG(3, this_fragmentation
->fragment
[end_index
].sid
, this_fragmentation
->fragment
[end_index
].tid
,
2043 "MPPP: (FIND END) seq_num:%d frag_index:%d flags:%d jitteravg:%d is LOST\n",
2044 this_fragmentation
->fragment
[end_index
].seq
, begin_index
,
2045 this_fragmentation
->fragment
[end_index
].flags
, this_fragmentation
->fragment
[end_index
].jitteravg
);
2046 // this frag is lost
2047 this_fragmentation
->fragment
[end_index
].length
= 0;
2048 this_fragmentation
->fragment
[end_index
].flags
= 0;
2049 // This frame is not complete find the next Begin
2055 // This frame is not complete find the next Begin if exist
2058 end_index
= (end_index
+1) & MAXFRAGNUM_MASK
;
2063 // The End sequence was found and the frame is complete
2066 // find the next Begin
2067 begin_index
= end_index
;
2072 // This fragment is lost, it was never completed the packet.
2073 LOG(3, this_fragmentation
->fragment
[begin_index
].sid
, this_fragmentation
->fragment
[begin_index
].tid
,
2074 "MPPP: (FIND BEGIN) seq_num:%d frag_index:%d flags:%d jitteravg:%d is LOST\n",
2075 this_fragmentation
->fragment
[begin_index
].seq
, begin_index
,
2076 this_fragmentation
->fragment
[begin_index
].flags
, this_fragmentation
->fragment
[begin_index
].jitteravg
);
2077 // this frag is lost
2078 this_fragmentation
->fragment
[begin_index
].length
= 0;
2079 this_fragmentation
->fragment
[begin_index
].flags
= 0;
2082 begin_index
= (begin_index
+1) & MAXFRAGNUM_MASK
;
2087 // try to assemble the frame that has the received fragment as a member
2088 // get the beginning of this frame
2089 begin_index
= end_index
= this_fragmentation
->start_index
;
2090 if (this_fragmentation
->fragment
[begin_index
].length
)
2092 if (!(this_fragmentation
->fragment
[begin_index
].flags
& MP_BEGIN
))
2094 // should occur only after an "M_Offset out of range"
2095 // The start sequence must be a begin sequence
2096 this_fragmentation
->start_index
= (begin_index
+1) & MAXFRAGNUM_MASK
;
2097 this_fragmentation
->start_seq
++;
2098 return; // assembling frame failed
2102 return; // assembling frame failed
2104 // get the end of his frame
2105 while (this_fragmentation
->fragment
[end_index
].length
)
2107 if (this_fragmentation
->fragment
[end_index
].flags
& MP_END
)
2110 end_index
= (end_index
+1) & MAXFRAGNUM_MASK
;
2112 if (end_index
== this_fragmentation
->start_index
)
2113 return; // assembling frame failed
2116 // return if a lost fragment is found
2117 if (!(this_fragmentation
->fragment
[end_index
].length
))
2118 return; // assembling frame failed
2120 // assemble the packet
2121 //assemble frame, process it, reset fragmentation
2122 uint16_t cur_len
= 4; // This is set to 4 to leave 4 bytes for function processipin
2124 LOG(4, s
, t
, "MPPP: processing fragments from %d to %d\n", begin_index
, end_index
);
2125 // Push to the receive buffer
2127 for (i
= begin_index
;; i
= (i
+ 1) & MAXFRAGNUM_MASK
)
2129 this_frag
= &this_fragmentation
->fragment
[i
];
2130 if(cur_len
+ this_frag
->length
> MAXETHER
)
2132 LOG(2, s
, t
, "MPPP: discarding reassembled frames larger than MAXETHER\n");
2136 memcpy(this_fragmentation
->reassembled_frame
+cur_len
, this_frag
->data
, this_frag
->length
);
2137 LOG(5, s
, t
, "MPPP: processing frame at %d, with len %d\n", i
, this_frag
->length
);
2139 cur_len
+= this_frag
->length
;
2142 this_fragmentation
->re_frame_len
= cur_len
;
2143 this_fragmentation
->re_frame_begin_index
= begin_index
;
2144 this_fragmentation
->re_frame_end_index
= end_index
;
2145 // Process the resassembled frame
2146 LOG(5, s
, t
, "MPPP: Process the reassembled frame, len=%d\n",cur_len
);
2147 processmpframe(s
, t
, this_fragmentation
->reassembled_frame
, this_fragmentation
->re_frame_len
, 1);
2152 // Set reassembled frame length to zero after processing it
2153 this_fragmentation
->re_frame_len
= 0;
2154 for (i
= begin_index
;; i
= (i
+ 1) & MAXFRAGNUM_MASK
)
2156 this_fragmentation
->fragment
[i
].length
= 0; // Indicates that this fragment has been consumed
2157 this_fragmentation
->fragment
[i
].flags
= 0;
2162 // Set the new start_index and start_seq
2163 this_fragmentation
->start_index
= (end_index
+ 1) & MAXFRAGNUM_MASK
;
2164 this_fragmentation
->start_seq
= this_fragmentation
->fragment
[end_index
].seq
+ 1;
2165 LOG(4, s
, t
, "MPPP after assembling: start index is = %d, start seq=%d\n", this_fragmentation
->start_index
, this_fragmentation
->start_seq
);
2167 begin_index
= this_fragmentation
->start_index
;
2168 if ((this_fragmentation
->fragment
[begin_index
].length
) &&
2169 (this_fragmentation
->fragment
[begin_index
].seq
!= this_fragmentation
->start_seq
))
2171 LOG(3, this_fragmentation
->fragment
[begin_index
].sid
, this_fragmentation
->fragment
[begin_index
].tid
,
2172 "MPPP: (START) seq_num:%d frag_index:%d flags:%d jitteravg:%d is LOST\n",
2173 this_fragmentation
->fragment
[begin_index
].seq
, begin_index
,
2174 this_fragmentation
->fragment
[begin_index
].flags
, this_fragmentation
->fragment
[begin_index
].jitteravg
);
2175 this_fragmentation
->fragment
[begin_index
].length
= 0;
2176 this_fragmentation
->fragment
[begin_index
].flags
= 0;
2179 if (this_fragmentation
->start_seq
<= Mmin
)
2180 // It's possible to find other complete frame or lost frame.
2182 else if ((this_fragmentation
->fragment
[begin_index
].length
) &&
2183 (this_fragmentation
->fragment
[begin_index
].flags
& MP_BEGIN
))
2184 // may be that the next frame is completed
2185 goto assembling_frame
;
2190 // process IPv6 packet received
2192 // This MUST be called with at least 4 byte behind 'p'.
2193 // (i.e. this routine writes to p[-4]).
2194 void processipv6in(sessionidt s
, tunnelidt t
, uint8_t *p
, uint16_t l
)
2199 CSTAT(processipv6in
);
2201 LOG_HEX(5, "IPv6", p
, l
);
2203 ip
= *(struct in6_addr
*) (p
+ 8);
2204 ipv4
= ntohl(*(uint32_t *)(p
+ 16));
2208 LOG(1, s
, t
, "IP packet too long %d\n", l
);
2209 STAT(tunnel_rx_errors
);
2213 if (session
[s
].ppp
.phase
!= Network
|| session
[s
].ppp
.ipv6cp
!= Opened
)
2217 if (ipv4
!= session
[s
].ip
&& memcmp(&config
->ipv6_prefix
, &ip
, 8) && sessionbyipv6(ip
) != s
)
2219 char str
[INET6_ADDRSTRLEN
];
2220 LOG(5, s
, t
, "Dropping packet with spoofed IP %s\n",
2221 inet_ntop(AF_INET6
, &ip
, str
, INET6_ADDRSTRLEN
));
2225 // Check if it's a Router Solicition message.
2226 if (*(p
+ 6) == 58 && *(p
+ 7) == 255 && *(p
+ 24) == 0xFF && *(p
+ 25) == 2 &&
2227 *(uint32_t *)(p
+ 26) == 0 && *(uint32_t *)(p
+ 30) == 0 &&
2228 *(uint32_t *)(p
+ 34) == 0 &&
2229 *(p
+ 38) == 0 && *(p
+ 39) == 2 && *(p
+ 40) == 133) {
2230 LOG(3, s
, t
, "Got IPv6 RS\n");
2231 send_ipv6_ra(s
, t
, &ip
);
2235 // Add on the tun header
2237 *(uint32_t *) p
= htonl(PKTIPV6
);
2240 // Are we throttled and a slave?
2241 if (session
[s
].tbf_in
&& !config
->cluster_iam_master
) {
2242 // Pass it to the master for handling.
2243 master_throttle_packet(session
[s
].tbf_in
, p
, l
);
2247 // Are we throttled and a master??
2248 if (session
[s
].tbf_in
&& config
->cluster_iam_master
) {
2249 // Actually handle the throttled packets.
2250 tbf_queue_packet(session
[s
].tbf_in
, p
, l
);
2255 if (tun_write(p
, l
) < 0)
2257 STAT(tun_tx_errors
);
2258 LOG(0, s
, t
, "Error writing %d bytes to TUN device: %s (tunfd=%d, p=%p)\n",
2259 l
, strerror(errno
), tunfd
, p
);
2267 if (session
[s
].snoop_ip
&& session
[s
].snoop_port
)
2269 // Snooping this session
2270 snoop_send_packet(p
, l
, session
[s
].snoop_ip
, session
[s
].snoop_port
);
2273 update_sessions_in_stat(s
, l
);
2277 STAT(tun_tx_packets
);
2278 INC_STAT(tun_tx_bytes
, l
);
2282 // Helper routine for the TBF filters.
2283 // Used to send queued data in from the user.
2285 void send_ipin(sessionidt s
, uint8_t *buf
, int len
)
2287 LOG_HEX(5, "IP in throttled", buf
, len
);
2289 if (write(tunfd
, buf
, len
) < 0)
2291 STAT(tun_tx_errors
);
2292 LOG(0, 0, 0, "Error writing %d bytes to TUN device: %s (tunfd=%d, p=%p)\n",
2293 len
, strerror(errno
), tunfd
, buf
);
2301 if (session
[s
].snoop_ip
&& session
[s
].snoop_port
)
2303 // Snooping this session
2304 snoop_send_packet(buf
, len
, session
[s
].snoop_ip
, session
[s
].snoop_port
);
2307 // Increment packet counters
2308 increment_counter(&session
[s
].cin
, &session
[s
].cin_wrap
, len
);
2309 session
[s
].cin_delta
+= len
;
2312 sess_local
[s
].cin
+= len
;
2313 sess_local
[s
].pin
++;
2317 STAT(tun_tx_packets
);
2318 INC_STAT(tun_tx_bytes
, len
- 4);
2322 // Process CCP messages
2323 void processccp(sessionidt s
, tunnelidt t
, uint8_t *p
, uint16_t l
)
2325 uint8_t b
[MAXETHER
];
2330 LOG_HEX(5, "CCP", p
, l
);
2332 if (session
[s
].ppp
.phase
< Network
)
2334 LOG(2, s
, t
, "CCP %s ignored in %s phase\n", ppp_code(*p
), ppp_phase(session
[s
].ppp
.phase
));
2340 LOG(1, s
, t
, "Short CCP packet\n");
2341 STAT(tunnel_rx_errors
);
2344 LOG(4, s
, t
, "CCP: recv %s\n", ppp_code(*p
));
2345 if (*p
== ConfigAck
)
2347 switch (session
[s
].ppp
.ccp
)
2350 initialise_restart_count(s
, ccp
);
2351 change_state(s
, ccp
, AckReceived
);
2356 LOG(2, s
, t
, "CCP: ConfigAck in state %s? Sending ConfigReq\n", ppp_state(session
[s
].ppp
.ccp
));
2358 change_state(s
, ccp
, RequestSent
);
2362 LOG(3, s
, t
, "CCP: Opened\n");
2363 change_state(s
, ccp
, Opened
);
2367 LOG(2, s
, t
, "CCP: ignoring %s in state %s\n", ppp_code(*p
), ppp_state(session
[s
].ppp
.ccp
));
2370 else if (*p
== ConfigReq
)
2372 if (l
< 6) // accept no compression
2374 else // compression requested--reject
2377 q
= makeppp(b
, sizeof(b
), p
, l
, s
, t
, PPPCCP
, 0, 0, 0);
2380 switch (session
[s
].ppp
.ccp
)
2383 q
= makeppp(b
, sizeof(b
), p
, 2, s
, t
, PPPCCP
, 0, 0, 0);
2386 *((uint16_t *) (q
+ 2)) = htons(l
= 4);
2390 initialise_restart_count(s
, ccp
);
2392 if (*q
== ConfigAck
)
2393 change_state(s
, ccp
, AckSent
);
2395 change_state(s
, ccp
, RequestSent
);
2400 if (*q
== ConfigAck
)
2401 change_state(s
, ccp
, AckSent
);
2406 if (*q
== ConfigAck
)
2407 change_state(s
, ccp
, Opened
);
2412 initialise_restart_count(s
, ccp
);
2417 if (*q
== ConfigAck
)
2418 change_state(s
, ccp
, AckSent
);
2420 change_state(s
, ccp
, RequestSent
);
2425 LOG(2, s
, t
, "CCP: ignoring %s in state %s\n", ppp_code(*p
), ppp_state(session
[s
].ppp
.ccp
));
2429 LOG(4, s
, t
, "CCP: send %s\n", ppp_code(*q
));
2430 tunnelsend(b
, l
+ (q
- b
), t
);
2432 else if (*p
== TerminateReq
)
2435 q
= makeppp(b
, sizeof(b
), p
, l
, s
, t
, PPPCCP
, 0, 0, 0);
2437 LOG(3, s
, t
, "CCP: send %s\n", ppp_code(*q
));
2438 tunnelsend(b
, l
+ (q
- b
), t
);
2439 change_state(s
, ccp
, Stopped
);
2441 else if (*p
!= CodeRej
)
2443 ppp_code_rej(s
, t
, PPPCCP
, "CCP", p
, l
, b
, sizeof(b
));
2447 // send a CHAP challenge
2448 void sendchap(sessionidt s
, tunnelidt t
)
2450 uint8_t b
[MAXETHER
];
2459 LOG(1, s
, t
, "No RADIUS to send challenge\n");
2460 STAT(tunnel_tx_errors
);
2464 LOG(1, s
, t
, "Send CHAP challenge\n");
2466 radius
[r
].chap
= 1; // CHAP not PAP
2468 if (radius
[r
].state
!= RADIUSCHAP
)
2471 radius
[r
].state
= RADIUSCHAP
;
2472 radius
[r
].retry
= backoff(radius
[r
].try++);
2473 if (radius
[r
].try > 5)
2475 sessionshutdown(s
, "CHAP timeout.", CDN_ADMIN_DISC
, TERM_REAUTHENTICATION_FAILURE
);
2476 STAT(tunnel_tx_errors
);
2479 q
= makeppp(b
, sizeof(b
), 0, 0, s
, t
, PPPCHAP
, 0, 0, 0);
2482 *q
= 1; // challenge
2483 q
[1] = radius
[r
].id
; // ID
2484 q
[4] = 16; // value size (size of challenge)
2485 memcpy(q
+ 5, radius
[r
].auth
, 16); // challenge
2486 strcpy((char *) q
+ 21, hostname
); // our name
2487 *(uint16_t *) (q
+ 2) = htons(strlen(hostname
) + 21); // length
2488 tunnelsend(b
, strlen(hostname
) + 21 + (q
- b
), t
); // send it
2491 // fill in a L2TP message with a PPP frame,
2492 // returns start of PPP frame
2493 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
)
2495 uint16_t hdr
= 0x0002; // L2TP with no options
2496 uint16_t type
= mtype
;
2499 if (size
< 16) // Need more space than this!!
2501 LOG(0, s
, t
, "makeppp buffer too small for L2TP header (size=%d)\n", size
);
2505 if (prio
) hdr
|= 0x0100; // set priority bit
2507 *(uint16_t *) (b
+ 0) = htons(hdr
);
2508 *(uint16_t *) (b
+ 2) = htons(tunnel
[t
].far
); // tunnel
2509 *(uint16_t *) (b
+ 4) = htons(session
[s
].far
); // session
2512 // Check whether this session is part of multilink
2515 if (bundle
[bid
].num_of_links
> 1)
2516 type
= PPPMP
; // Change PPP message type to the PPPMP
2521 if (type
== PPPLCP
|| !(session
[s
].flags
& SESSION_ACFC
))
2523 *(uint16_t *) b
= htons(0xFF03); // HDLC header
2527 if (type
< 0x100 && session
[s
].flags
& SESSION_PFC
)
2533 *(uint16_t *) b
= htons(type
);
2539 // Set the sequence number and (B)egin (E)nd flags
2540 if (session
[s
].mssf
)
2542 // Set the multilink bits
2543 uint16_t bits_send
= mp_bits
;
2544 *(uint16_t *) b
= htons((bundle
[bid
].seq_num_t
& 0x0FFF)|bits_send
);
2549 *(uint32_t *) b
= htonl(bundle
[bid
].seq_num_t
);
2550 // Set the multilink bits
2555 bundle
[bid
].seq_num_t
++;
2557 // Add the message type if this fragment has the begin bit set
2558 if (mp_bits
& MP_BEGIN
)
2560 //*b++ = mtype; // The next two lines are instead of this
2561 *(uint16_t *) b
= htons(mtype
); // Message type
2566 if ((b
- start
) + l
> size
)
2568 LOG(2, s
, t
, "makeppp would overflow buffer (size=%d, header+payload=%td)\n", size
, (b
- start
) + l
);
2579 static int add_lcp_auth(uint8_t *b
, int size
, int authtype
)
2582 if ((authtype
== AUTHCHAP
&& size
< 5) || size
< 4)
2585 *b
++ = 3; // Authentication-Protocol
2586 if (authtype
== AUTHCHAP
)
2588 len
= *b
++ = 5; // length
2589 *(uint16_t *) b
= htons(PPPCHAP
); b
+= 2;
2592 else if (authtype
== AUTHPAP
)
2594 len
= *b
++ = 4; // length
2595 *(uint16_t *) b
= htons(PPPPAP
); b
+= 2;
2599 LOG(0, 0, 0, "add_lcp_auth called with unsupported auth type %d\n", authtype
);
2605 // Send LCP ConfigReq for MRU, authentication type and magic no
2606 void sendlcp(sessionidt s
, tunnelidt t
)
2608 uint8_t b
[500], *q
, *l
;
2609 int authtype
= sess_local
[s
].lcp_authtype
;
2611 if (!(q
= makeppp(b
, sizeof(b
), NULL
, 0, s
, t
, PPPLCP
, 0, 0, 0)))
2614 LOG(3, s
, t
, "LCP: send ConfigReq%s%s%s including MP options\n",
2615 authtype
? " (" : "",
2616 authtype
? (authtype
== AUTHCHAP
? "CHAP" : "PAP") : "",
2617 authtype
? ")" : "");
2621 *l
++ = ++sess_local
[s
].lcp_ident
; // ID
2623 l
+= 2; //Save space for length
2625 if (sess_local
[s
].ppp_mru
)
2627 *l
++ = 1; *l
++ = 4; // Maximum-Receive-Unit (length 4)
2628 *(uint16_t *) l
= htons(sess_local
[s
].ppp_mru
); l
+= 2;
2632 l
+= add_lcp_auth(l
, sizeof(b
) - (l
- b
), authtype
);
2634 if (session
[s
].magic
)
2636 *l
++ = 5; *l
++ = 6; // Magic-Number (length 6)
2637 *(uint32_t *) l
= htonl(session
[s
].magic
);
2641 if (sess_local
[s
].mp_mrru
)
2643 *l
++ = 17; *l
++ = 4; // Multilink Max-Receive-Reconstructed-Unit (length 4)
2644 *(uint16_t *) l
= htons(sess_local
[s
].mp_mrru
); l
+= 2;
2647 if (sess_local
[s
].mp_epdis
)
2649 *l
++ = 19; *l
++ = 7; // Multilink Endpoint Discriminator (length 7)
2650 *l
++ = IPADDR
; // Endpoint Discriminator class
2651 *(uint32_t *) l
= htonl(sess_local
[s
].mp_epdis
);
2655 *(uint16_t *)(q
+ 2) = htons(l
- q
); // Length
2657 LOG_HEX(5, "PPPLCP", q
, l
- q
);
2658 if (config
->debug
> 3) dumplcp(q
, l
- q
);
2660 tunnelsend(b
, (l
- b
), t
);
2661 restart_timer(s
, lcp
);
2664 // Send CCP request for no compression
2665 void sendccp(sessionidt s
, tunnelidt t
)
2669 if (!(q
= makeppp(b
, sizeof(b
), NULL
, 0, s
, t
, PPPCCP
, 0, 0, 0)))
2672 LOG(3, s
, t
, "CCP: send ConfigReq (no compression)\n");
2675 *(q
+ 1) = ++sess_local
[s
].lcp_ident
; // ID
2676 *(uint16_t *)(q
+ 2) = htons(4); // Length
2678 LOG_HEX(5, "PPPCCP", q
, 4);
2679 tunnelsend(b
, (q
- b
) + 4 , t
);
2680 restart_timer(s
, ccp
);
2683 // Reject unknown/unconfigured protocols
2684 void protoreject(sessionidt s
, tunnelidt t
, uint8_t *p
, uint16_t l
, uint16_t proto
)
2687 uint8_t buf
[MAXETHER
];
2689 int mru
= session
[s
].mru
;
2690 if (mru
< MINMTU
) mru
= MINMTU
;
2691 if (mru
> sizeof(buf
)) mru
= sizeof(buf
);
2694 if (l
> mru
) l
= mru
;
2696 q
= makeppp(buf
, sizeof(buf
), 0, 0, s
, t
, PPPLCP
, 0, 0, 0);
2700 *(q
+ 1) = ++sess_local
[s
].lcp_ident
;
2701 *(uint16_t *)(q
+ 2) = htons(l
);
2702 *(uint16_t *)(q
+ 4) = htons(proto
);
2703 memcpy(q
+ 6, p
, l
- 6);
2705 if (proto
== PPPIPV6CP
)
2706 LOG(3, s
, t
, "LCP: send ProtocolRej (IPV6CP: not configured)\n");
2708 LOG(2, s
, t
, "LCP: sent ProtocolRej (0x%04X: unsupported)\n", proto
);
2710 tunnelsend(buf
, l
+ (q
- buf
), t
);