3 char const *cvs_id_ppp
= "$Id: ppp.c,v 1.104 2009/12/08 14:49:28 bodea Exp $";
11 #include "constants.h"
17 extern tunnelt
*tunnel
;
18 extern bundlet
*bundle
;
19 extern fragmentationt
*frag
;
20 extern sessiont
*session
;
21 extern radiust
*radius
;
23 extern char hostname
[];
24 extern uint32_t eth_tx
;
25 extern time_t time_now
;
26 extern configt
*config
;
28 static int add_lcp_auth(uint8_t *b
, int size
, int authtype
);
29 static bundleidt
new_bundle(void);
30 static int epdiscmp(epdist
, epdist
);
31 static void setepdis(epdist
*, epdist
);
32 static void ipcp_open(sessionidt s
, tunnelidt t
);
34 static int first_session_in_bundle(sessionidt s
)
37 for (i
= 1; i
< MAXBUNDLE
; i
++)
38 if (bundle
[i
].state
!= BUNDLEFREE
)
39 if (epdiscmp(session
[s
].epdis
,bundle
[i
].epdis
) && !strcmp(session
[s
].user
, bundle
[i
].user
))
44 // Process PAP messages
45 void processpap(sessionidt s
, tunnelidt t
, uint8_t *p
, uint16_t l
)
54 LOG_HEX(5, "PAP", p
, l
);
57 LOG(1, s
, t
, "Short PAP %u bytes\n", l
);
58 STAT(tunnel_rx_errors
);
59 sessionshutdown(s
, "Short PAP packet.", CDN_ADMIN_DISC
, TERM_USER_ERROR
);
63 if ((hl
= ntohs(*(uint16_t *) (p
+ 2))) > l
)
65 LOG(1, s
, t
, "Length mismatch PAP %u/%u\n", hl
, l
);
66 STAT(tunnel_rx_errors
);
67 sessionshutdown(s
, "PAP length mismatch.", CDN_ADMIN_DISC
, TERM_USER_ERROR
);
74 LOG(1, s
, t
, "Unexpected PAP code %d\n", *p
);
75 STAT(tunnel_rx_errors
);
76 sessionshutdown(s
, "Unexpected PAP code.", CDN_ADMIN_DISC
, TERM_USER_ERROR
);
80 if (session
[s
].ppp
.phase
!= Authenticate
)
82 LOG(2, s
, t
, "PAP ignored in %s phase\n", ppp_phase(session
[s
].ppp
.phase
));
89 user
[0] = pass
[0] = 0;
90 if (*b
&& *b
< sizeof(user
))
92 memcpy(user
, b
+ 1, *b
);
95 if (*b
&& *b
< sizeof(pass
))
97 memcpy(pass
, b
+ 1, *b
);
101 LOG(3, s
, t
, "PAP login %s/%s\n", user
, pass
);
104 if (session
[s
].ip
|| !(r
= radiusnew(s
)))
106 // respond now, either no RADIUS available or already authenticated
109 uint8_t *p
= makeppp(b
, sizeof(b
), 0, 0, s
, t
, PPPPAP
, 0, 0, 0);
115 *p
= 3; // cant authorise
117 *(uint16_t *) (p
+ 2) = htons(5); // length
118 p
[4] = 0; // no message
119 tunnelsend(b
, 5 + (p
- b
), t
); // send it
123 LOG(3, s
, t
, "Already an IP allocated: %s (%d)\n",
124 fmtaddr(htonl(session
[s
].ip
), 0), session
[s
].ip_pool_index
);
128 LOG(1, s
, t
, "No RADIUS session available to authenticate session...\n");
129 sessionshutdown(s
, "No free RADIUS sessions.", CDN_UNAVAILABLE
, TERM_SERVICE_UNAVAILABLE
);
134 // Run PRE_AUTH plugins
135 struct param_pre_auth packet
= { &tunnel
[t
], &session
[s
], strdup(user
), strdup(pass
), PPPPAP
, 1 };
136 run_plugins(PLUGIN_PRE_AUTH
, &packet
);
137 if (!packet
.continue_auth
)
139 LOG(3, s
, t
, "A plugin rejected PRE_AUTH\n");
140 if (packet
.username
) free(packet
.username
);
141 if (packet
.password
) free(packet
.password
);
145 strncpy(session
[s
].user
, packet
.username
, sizeof(session
[s
].user
) - 1);
146 strncpy(radius
[r
].pass
, packet
.password
, sizeof(radius
[r
].pass
) - 1);
148 free(packet
.username
);
149 free(packet
.password
);
152 LOG(3, s
, t
, "Sending login for %s/%s to RADIUS\n", user
, pass
);
153 if ((session
[s
].mrru
) && (!first_session_in_bundle(s
)))
154 radiussend(r
, RADIUSJUSTAUTH
);
156 radiussend(r
, RADIUSAUTH
);
160 // Process CHAP messages
161 void processchap(sessionidt s
, tunnelidt t
, uint8_t *p
, uint16_t l
)
168 LOG_HEX(5, "CHAP", p
, l
);
172 LOG(1, s
, t
, "Short CHAP %u bytes\n", l
);
173 STAT(tunnel_rx_errors
);
174 sessionshutdown(s
, "Short CHAP packet.", CDN_ADMIN_DISC
, TERM_USER_ERROR
);
178 if ((hl
= ntohs(*(uint16_t *) (p
+ 2))) > l
)
180 LOG(1, s
, t
, "Length mismatch CHAP %u/%u\n", hl
, l
);
181 STAT(tunnel_rx_errors
);
182 sessionshutdown(s
, "CHAP length mismatch.", CDN_ADMIN_DISC
, TERM_USER_ERROR
);
189 LOG(1, s
, t
, "Unexpected CHAP response code %d\n", *p
);
190 STAT(tunnel_rx_errors
);
191 sessionshutdown(s
, "CHAP length mismatch.", CDN_ADMIN_DISC
, TERM_USER_ERROR
);
195 if (session
[s
].ppp
.phase
!= Authenticate
)
197 LOG(2, s
, t
, "CHAP ignored in %s phase\n", ppp_phase(session
[s
].ppp
.phase
));
201 r
= sess_local
[s
].radius
;
204 LOG(3, s
, t
, "Unexpected CHAP message\n");
206 // Some modems (Netgear DM602, possibly others) persist in using CHAP even
207 // after ACKing our ConfigReq for PAP.
208 if (sess_local
[s
].lcp_authtype
== AUTHPAP
&& config
->radius_authtypes
& AUTHCHAP
)
210 sess_local
[s
].lcp_authtype
= AUTHCHAP
;
216 if (p
[1] != radius
[r
].id
)
218 LOG(1, s
, t
, "Wrong CHAP response ID %d (should be %d) (%d)\n", p
[1], radius
[r
].id
, r
);
219 STAT(tunnel_rx_errors
);
220 sessionshutdown(s
, "Unexpected CHAP response ID.", CDN_ADMIN_DISC
, TERM_USER_ERROR
);
224 if (l
< 5 || p
[4] != 16)
226 LOG(1, s
, t
, "Bad CHAP response length %d\n", l
< 5 ? -1 : p
[4]);
227 STAT(tunnel_rx_errors
);
228 sessionshutdown(s
, "Bad CHAP response length.", CDN_ADMIN_DISC
, TERM_USER_ERROR
);
234 if (l
< 16 || l
- 16 >= sizeof(session
[s
].user
))
236 LOG(1, s
, t
, "CHAP user too long %d\n", l
- 16);
237 STAT(tunnel_rx_errors
);
238 sessionshutdown(s
, "CHAP username too long.", CDN_ADMIN_DISC
, TERM_USER_ERROR
);
242 // Run PRE_AUTH plugins
244 struct param_pre_auth packet
= { &tunnel
[t
], &session
[s
], NULL
, NULL
, PPPCHAP
, 1 };
246 packet
.password
= calloc(17, 1);
247 memcpy(packet
.password
, p
, 16);
252 packet
.username
= calloc(l
+ 1, 1);
253 memcpy(packet
.username
, p
, l
);
255 run_plugins(PLUGIN_PRE_AUTH
, &packet
);
256 if (!packet
.continue_auth
)
258 LOG(3, s
, t
, "A plugin rejected PRE_AUTH\n");
259 if (packet
.username
) free(packet
.username
);
260 if (packet
.password
) free(packet
.password
);
264 strncpy(session
[s
].user
, packet
.username
, sizeof(session
[s
].user
) - 1);
265 memcpy(radius
[r
].pass
, packet
.password
, 16);
267 free(packet
.username
);
268 free(packet
.password
);
272 LOG(3, s
, t
, "CHAP login %s\n", session
[s
].user
);
273 if ((session
[s
].mrru
) && (!first_session_in_bundle(s
)))
274 radiussend(r
, RADIUSJUSTAUTH
);
276 radiussend(r
, RADIUSAUTH
);
279 static void dumplcp(uint8_t *p
, int l
)
282 uint8_t *o
= (p
+ 4);
284 LOG_HEX(5, "PPP LCP Packet", p
, l
);
285 LOG(4, 0, 0, "PPP LCP Packet type %d (%s len %d)\n", *p
, ppp_code((int)*p
), ntohs( ((uint16_t *) p
)[1]) );
286 LOG(4, 0, 0, "Length: %d\n", l
);
287 if (*p
!= ConfigReq
&& *p
!= ConfigRej
&& *p
!= ConfigAck
)
296 LOG(4, 0, 0, " Option length is %d...\n", length
);
301 LOG(4, 0, 0, " Option type is 0...\n");
308 case 1: // Maximum-Receive-Unit
310 LOG(4, 0, 0, " %s %d\n", ppp_lcp_option(type
), ntohs(*(uint16_t *)(o
+ 2)));
312 LOG(4, 0, 0, " %s odd length %d\n", ppp_lcp_option(type
), length
);
314 case 2: // Async-Control-Character-Map
317 uint32_t asyncmap
= ntohl(*(uint32_t *)(o
+ 2));
318 LOG(4, 0, 0, " %s %x\n", ppp_lcp_option(type
), asyncmap
);
321 LOG(4, 0, 0, " %s odd length %d\n", ppp_lcp_option(type
), length
);
323 case 3: // Authentication-Protocol
326 int proto
= ntohs(*(uint16_t *)(o
+ 2));
327 LOG(4, 0, 0, " %s 0x%x (%s)\n", ppp_lcp_option(type
), proto
,
328 proto
== PPPPAP
? "PAP" : "UNSUPPORTED");
330 else if (length
== 5)
332 int proto
= ntohs(*(uint16_t *)(o
+ 2));
334 LOG(4, 0, 0, " %s 0x%x 0x%x (%s)\n", ppp_lcp_option(type
), proto
, algo
,
335 (proto
== PPPCHAP
&& algo
== 5) ? "CHAP MD5" : "UNSUPPORTED");
338 LOG(4, 0, 0, " %s odd length %d\n", ppp_lcp_option(type
), length
);
340 case 4: // Quality-Protocol
342 uint32_t qp
= ntohl(*(uint32_t *)(o
+ 2));
343 LOG(4, 0, 0, " %s %x\n", ppp_lcp_option(type
), qp
);
346 case 5: // Magic-Number
349 uint32_t magicno
= ntohl(*(uint32_t *)(o
+ 2));
350 LOG(4, 0, 0, " %s %x\n", ppp_lcp_option(type
), magicno
);
353 LOG(4, 0, 0, " %s odd length %d\n", ppp_lcp_option(type
), length
);
355 case 7: // Protocol-Field-Compression
356 case 8: // Address-And-Control-Field-Compression
357 LOG(4, 0, 0, " %s\n", ppp_lcp_option(type
));
360 LOG(2, 0, 0, " Unknown PPP LCP Option type %d\n", type
);
368 void lcp_open(sessionidt s
, tunnelidt t
)
370 // transition to Authentication or Network phase:
371 session
[s
].ppp
.phase
= sess_local
[s
].lcp_authtype
? Authenticate
: Network
;
373 LOG(3, s
, t
, "LCP: Opened, phase %s\n", ppp_phase(session
[s
].ppp
.phase
));
376 change_state(s
, lcp
, Opened
);
378 if (session
[s
].ppp
.phase
== Authenticate
)
380 if (sess_local
[s
].lcp_authtype
== AUTHCHAP
)
385 if(session
[s
].bundle
== 0 || bundle
[session
[s
].bundle
].num_of_links
== 1)
389 change_state(s
, ipcp
, RequestSent
);
390 // move to passive state for IPv6 (if configured), CCP
391 if (config
->ipv6_prefix
.s6_addr
[0])
392 change_state(s
, ipv6cp
, Stopped
);
394 change_state(s
, ipv6cp
, Closed
);
396 change_state(s
, ccp
, Stopped
);
400 sessionidt first_ses
= bundle
[session
[s
].bundle
].members
[0];
401 LOG(3, s
, t
, "MPPP: Skipping IPCP negotiation for session:%d, first session of bundle is:%d\n",s
,first_ses
);
407 static void lcp_restart(sessionidt s
)
409 session
[s
].ppp
.phase
= Establish
;
411 change_state(s
, ipcp
, Dead
);
412 change_state(s
, ipv6cp
, Dead
);
413 change_state(s
, ccp
, Dead
);
416 static uint8_t *ppp_conf_rej(sessionidt s
, uint8_t *buf
, size_t blen
, uint16_t mtype
,
417 uint8_t **response
, uint8_t *queued
, uint8_t *packet
, uint8_t *option
)
419 if (!*response
|| **response
!= ConfigRej
)
421 queued
= *response
= makeppp(buf
, blen
, packet
, 2, s
, session
[s
].tunnel
, mtype
, 0, 0, 0);
429 if ((queued
- buf
+ option
[1]) > blen
)
431 LOG(2, s
, session
[s
].tunnel
, "PPP overflow for ConfigRej (proto %u, option %u).\n", mtype
, *option
);
435 memcpy(queued
, option
, option
[1]);
436 return queued
+ option
[1];
439 static uint8_t *ppp_conf_nak(sessionidt s
, uint8_t *buf
, size_t blen
, uint16_t mtype
,
440 uint8_t **response
, uint8_t *queued
, uint8_t *packet
, uint8_t *option
,
441 uint8_t *value
, size_t vlen
)
446 case PPPLCP
: nak_sent
= &sess_local
[s
].lcp
.nak_sent
; break;
447 case PPPIPCP
: nak_sent
= &sess_local
[s
].ipcp
.nak_sent
; break;
448 case PPPIPV6CP
: nak_sent
= &sess_local
[s
].ipv6cp
.nak_sent
; break;
449 default: return 0; // ?
452 if (*response
&& **response
!= ConfigNak
)
454 if (*nak_sent
< config
->ppp_max_failure
) // reject queued
457 return ppp_conf_rej(s
, buf
, blen
, mtype
, response
, 0, packet
, option
);
462 if (*nak_sent
>= config
->ppp_max_failure
)
463 return ppp_conf_rej(s
, buf
, blen
, mtype
, response
, 0, packet
, option
);
465 queued
= *response
= makeppp(buf
, blen
, packet
, 2, s
, session
[s
].tunnel
, mtype
, 0, 0, 0);
474 if ((queued
- buf
+ vlen
+ 2) > blen
)
476 LOG(2, s
, session
[s
].tunnel
, "PPP overflow for ConfigNak (proto %u, option %u).\n", mtype
, *option
);
481 *queued
++ = vlen
+ 2;
482 memcpy(queued
, value
, vlen
);
483 return queued
+ vlen
;
486 static void ppp_code_rej(sessionidt s
, tunnelidt t
, uint16_t proto
,
487 char *pname
, uint8_t *p
, uint16_t l
, uint8_t *buf
, size_t size
)
490 int mru
= session
[s
].mru
;
491 if (mru
< MINMTU
) mru
= MINMTU
;
492 if (mru
> size
) mru
= size
;
495 if (l
> mru
) l
= mru
;
497 q
= makeppp(buf
, size
, 0, 0, s
, t
, proto
, 0, 0, 0);
501 *(q
+ 1) = ++sess_local
[s
].lcp_ident
;
502 *(uint16_t *)(q
+ 2) = htons(l
);
503 memcpy(q
+ 4, p
, l
- 4);
505 LOG(2, s
, t
, "Unexpected %s code %s\n", pname
, ppp_code(*p
));
506 LOG(3, s
, t
, "%s: send %s\n", pname
, ppp_code(*q
));
507 if (config
->debug
> 3) dumplcp(q
, l
);
509 tunnelsend(buf
, l
+ (q
- buf
), t
);
512 // Process LCP messages
513 void processlcp(sessionidt s
, tunnelidt t
, uint8_t *p
, uint16_t l
)
521 LOG_HEX(5, "LCP", p
, l
);
524 LOG(1, s
, t
, "Short LCP %d bytes\n", l
);
525 STAT(tunnel_rx_errors
);
529 if ((hl
= ntohs(*(uint16_t *) (p
+ 2))) > l
)
531 LOG(1, s
, t
, "Length mismatch LCP %u/%u\n", hl
, l
);
532 STAT(tunnel_rx_errors
);
537 if (session
[s
].die
) // going down...
540 LOG((*p
== EchoReq
|| *p
== EchoReply
) ? 4 : 3, s
, t
,
541 "LCP: recv %s\n", ppp_code(*p
));
543 if (config
->debug
> 3) dumplcp(p
, l
);
548 uint8_t *o
= (p
+ 4);
556 if (length
== 0 || type
== 0 || x
< length
) break;
559 case 3: // Authentication-Protocol
561 int proto
= ntohs(*(uint16_t *)(o
+ 2));
564 else if (proto
== PPPCHAP
&& *(o
+ 4) == 5)
574 if (!session
[s
].ip
&& authtype
)
575 sess_local
[s
].lcp_authtype
= authtype
;
577 switch (session
[s
].ppp
.lcp
)
580 initialise_restart_count(s
, lcp
);
581 change_state(s
, lcp
, AckReceived
);
586 LOG(2, s
, t
, "LCP: ConfigAck in state %s? Sending ConfigReq\n", ppp_state(session
[s
].ppp
.lcp
));
587 if (session
[s
].ppp
.lcp
== Opened
)
591 change_state(s
, lcp
, RequestSent
);
599 LOG(2, s
, t
, "LCP: ignoring %s in state %s\n", ppp_code(*p
), ppp_state(session
[s
].ppp
.lcp
));
602 else if (*p
== ConfigReq
)
605 uint8_t *o
= (p
+ 4);
606 uint8_t *response
= 0;
607 static uint8_t asyncmap
[4] = { 0, 0, 0, 0 }; // all zero
608 static uint8_t authproto
[5];
616 if (length
== 0 || type
== 0 || x
< length
) break;
619 case 1: // Maximum-Receive-Unit
621 uint16_t mru
= ntohs(*(uint16_t *)(o
+ 2));
624 session
[s
].mru
= mru
;
629 LOG(3, s
, t
, " Remote requesting MRU of %u. Rejecting.\n", mru
);
631 q
= ppp_conf_nak(s
, b
, sizeof(b
), PPPLCP
, &response
, q
, p
, o
, (uint8_t *) &mru
, sizeof(mru
));
635 case 2: // Async-Control-Character-Map
636 if (!ntohl(*(uint32_t *)(o
+ 2))) // all bits zero is OK
639 LOG(3, s
, t
, " Remote requesting asyncmap. Rejecting.\n");
640 q
= ppp_conf_nak(s
, b
, sizeof(b
), PPPLCP
, &response
, q
, p
, o
, asyncmap
, sizeof(asyncmap
));
643 case 3: // Authentication-Protocol
645 int proto
= ntohs(*(uint16_t *)(o
+ 2));
646 char proto_name
[] = "0x0000";
651 if (config
->radius_authtypes
& AUTHPAP
)
653 sess_local
[s
].lcp_authtype
= AUTHPAP
;
657 strcpy(proto_name
, "PAP");
659 else if (proto
== PPPCHAP
)
661 if (config
->radius_authtypes
& AUTHCHAP
662 && *(o
+ 4) == 5) // MD5
664 sess_local
[s
].lcp_authtype
= AUTHCHAP
;
668 strcpy(proto_name
, "CHAP");
671 sprintf(proto_name
, "%#4.4x", proto
);
673 LOG(3, s
, t
, " Remote requesting %s authentication. Rejecting.\n", proto_name
);
675 alen
= add_lcp_auth(authproto
, sizeof(authproto
), config
->radius_authprefer
);
676 if (alen
< 2) break; // paranoia
678 q
= ppp_conf_nak(s
, b
, sizeof(b
), PPPLCP
, &response
, q
, p
, o
, authproto
+ 2, alen
- 2);
679 if (q
&& *response
== ConfigNak
&&
680 config
->radius_authtypes
!= config
->radius_authprefer
)
683 alen
= add_lcp_auth(authproto
, sizeof(authproto
), config
->radius_authtypes
& ~config
->radius_authprefer
);
685 q
= ppp_conf_nak(s
, b
, sizeof(b
), PPPLCP
, &response
, q
, p
, o
, authproto
+ 2, alen
- 2);
692 case 4: // Quality-Protocol
693 case 5: // Magic-Number
694 case 7: // Protocol-Field-Compression
695 case 8: // Address-And-Control-Field-Compression
698 case 17: // Multilink Max-Receive-Reconstructed-Unit
700 uint16_t mrru
= ntohs(*(uint16_t *)(o
+ 2));
701 session
[s
].mrru
= mrru
;
703 LOG(3, s
, t
, " Received PPP LCP option MRRU: %d\n",mrru
);
707 case 18: // Multilink Short Sequence Number Header Format
711 LOG(3, s
, t
, " Received PPP LCP option MSSN format\n");
715 case 19: // Multilink Endpoint Discriminator
717 uint8_t epdis_class
= o
[2];
720 session
[s
].epdis
.addr_class
= epdis_class
;
721 session
[s
].epdis
.length
= length
- 3;
722 if (session
[s
].epdis
.length
> 20)
724 LOG(1, s
, t
, "Error: received EndDis Address Length more than 20: %d\n", session
[s
].epdis
.length
);
725 session
[s
].epdis
.length
= 20;
728 for (addr
= 0; addr
< session
[s
].epdis
.length
; addr
++)
729 session
[s
].epdis
.address
[addr
] = o
[3+addr
];
736 LOG(3, s
, t
, " Received PPP LCP option Multilink EndDis Local Address Class: %d\n",epdis_class
);
739 LOG(3, s
, t
, " Received PPP LCP option Multilink EndDis IP Address Class: %d\n",epdis_class
);
742 LOG(3, s
, t
, " Received PPP LCP option Multilink EndDis IEEE MAC Address Class: %d\n",epdis_class
);
745 LOG(3, s
, t
, " Received PPP LCP option Multilink EndDis PPP Magic No Class: %d\n",epdis_class
);
748 LOG(3, s
, t
, " Received PPP LCP option Multilink EndDis PSND No Class: %d\n",epdis_class
);
751 LOG(3, s
, t
, " Received PPP LCP option Multilink EndDis NULL Class %d\n",epdis_class
);
756 default: // Reject any unknown options
757 LOG(3, s
, t
, " Rejecting unknown PPP LCP option %d\n", type
);
758 q
= ppp_conf_rej(s
, b
, sizeof(b
), PPPLCP
, &response
, q
, p
, o
);
765 cluster_send_session(s
);
769 l
= q
- response
; // LCP packet length
770 *((uint16_t *) (response
+ 2)) = htons(l
); // update header
774 // Send packet back as ConfigAck
775 response
= makeppp(b
, sizeof(b
), p
, l
, s
, t
, PPPLCP
, 0, 0, 0);
776 if (!response
) return;
777 *response
= ConfigAck
;
780 switch (session
[s
].ppp
.lcp
)
783 response
= makeppp(b
, sizeof(b
), p
, 2, s
, t
, PPPLCP
, 0, 0, 0);
784 if (!response
) return;
785 *response
= TerminateAck
;
786 *((uint16_t *) (response
+ 2)) = htons(l
= 4);
790 initialise_restart_count(s
, lcp
);
792 if (*response
== ConfigAck
)
793 change_state(s
, lcp
, AckSent
);
795 change_state(s
, lcp
, RequestSent
);
800 if (*response
== ConfigAck
)
801 change_state(s
, lcp
, AckSent
);
806 if (*response
== ConfigAck
)
817 if (*response
== ConfigAck
)
818 change_state(s
, lcp
, AckSent
);
820 change_state(s
, lcp
, RequestSent
);
825 LOG(2, s
, t
, "LCP: ignoring %s in state %s\n", ppp_code(*p
), ppp_state(session
[s
].ppp
.lcp
));
829 LOG(3, s
, t
, "LCP: send %s\n", ppp_code(*response
));
830 if (config
->debug
> 3) dumplcp(response
, l
);
832 tunnelsend(b
, l
+ (response
- b
), t
);
834 else if (*p
== ConfigNak
|| *p
== ConfigRej
)
837 uint8_t *o
= (p
+ 4);
845 if (length
== 0 || type
== 0 || x
< length
) break;
848 case 1: // Maximum-Receive-Unit
851 if (length
< 4) break;
852 sess_local
[s
].ppp_mru
= ntohs(*(uint16_t *)(o
+ 2));
853 LOG(3, s
, t
, " Remote requested MRU of %u\n", sess_local
[s
].ppp_mru
);
857 sess_local
[s
].ppp_mru
= 0;
858 LOG(3, s
, t
, " Remote rejected MRU negotiation\n");
863 case 3: // Authentication-Protocol
871 if (length
< 4) break;
872 proto
= ntohs(*(uint16_t *)(o
+ 2));
876 authtype
= config
->radius_authtypes
& AUTHPAP
;
877 LOG(3, s
, t
, " Remote requested PAP authentication...%sing\n",
878 authtype
? "accept" : "reject");
880 else if (proto
== PPPCHAP
&& length
> 4 && *(o
+ 4) == 5)
882 authtype
= config
->radius_authtypes
& AUTHCHAP
;
883 LOG(3, s
, t
, " Remote requested CHAP authentication...%sing\n",
884 authtype
? "accept" : "reject");
888 LOG(3, s
, t
, " Rejecting unsupported authentication %#4x\n",
894 LOG(2, s
, t
, "LCP: remote rejected auth negotiation\n");
895 authtype
= 0; // shutdown
900 case 5: // Magic-Number
901 session
[s
].magic
= 0;
904 if (length
< 6) break;
905 session
[s
].magic
= ntohl(*(uint32_t *)(o
+ 2));
908 if (session
[s
].magic
)
909 LOG(3, s
, t
, " Remote requested magic-no %x\n", session
[s
].magic
);
911 LOG(3, s
, t
, " Remote rejected magic-no\n");
913 cluster_send_session(s
);
916 case 17: // Multilink Max-Receive-Reconstructed-Unit
920 sess_local
[s
].mp_mrru
= ntohs(*(uint16_t *)(o
+ 2));
921 LOG(3, s
, t
, " Remote requested MRRU of %u\n", sess_local
[s
].mp_mrru
);
925 sess_local
[s
].mp_mrru
= 0;
926 LOG(3, s
, t
, " Remote rejected MRRU negotiation\n");
931 case 18: // Multilink Short Sequence Number Header Format
935 sess_local
[s
].mp_mssf
= 0;
936 LOG(3, s
, t
, " Remote requested Naked mssf\n");
940 sess_local
[s
].mp_mssf
= 0;
941 LOG(3, s
, t
, " Remote rejected mssf\n");
946 case 19: // Multilink Endpoint Discriminator
950 LOG(2, s
, t
, " Remote should not configNak Endpoint Dis!\n");
954 sess_local
[s
].mp_epdis
= 0;
955 LOG(3, s
, t
, " Remote rejected Endpoint Discriminator\n");
961 LOG(2, s
, t
, "LCP: remote sent %s for type %u?\n", ppp_code(*p
), type
);
962 sessionshutdown(s
, "Unable to negotiate LCP.", CDN_ADMIN_DISC
, TERM_USER_ERROR
);
971 sessionshutdown(s
, "Unsupported authentication.", CDN_ADMIN_DISC
, TERM_USER_ERROR
);
976 sess_local
[s
].lcp_authtype
= authtype
;
978 switch (session
[s
].ppp
.lcp
)
983 uint8_t *response
= makeppp(b
, sizeof(b
), p
, 2, s
, t
, PPPLCP
, 0, 0, 0);
984 if (!response
) return;
985 *response
= TerminateAck
;
986 *((uint16_t *) (response
+ 2)) = htons(l
= 4);
988 LOG(3, s
, t
, "LCP: send %s\n", ppp_code(*response
));
989 if (config
->debug
> 3) dumplcp(response
, l
);
991 tunnelsend(b
, l
+ (response
- b
), t
);
997 initialise_restart_count(s
, lcp
);
1002 LOG(2, s
, t
, "LCP: ConfigNak in state %s? Sending ConfigReq\n", ppp_state(session
[s
].ppp
.lcp
));
1012 LOG(2, s
, t
, "LCP: ignoring %s in state %s\n", ppp_code(*p
), ppp_state(session
[s
].ppp
.lcp
));
1016 else if (*p
== TerminateReq
)
1018 switch (session
[s
].ppp
.lcp
)
1031 zero_restart_count(s
, lcp
);
1032 change_state(s
, lcp
, Closing
);
1036 LOG(2, s
, t
, "LCP: ignoring %s in state %s\n", ppp_code(*p
), ppp_state(session
[s
].ppp
.lcp
));
1040 *p
= TerminateAck
; // send ack
1041 q
= makeppp(b
, sizeof(b
), p
, l
, s
, t
, PPPLCP
, 0, 0, 0);
1044 LOG(3, s
, t
, "LCP: send %s\n", ppp_code(*q
));
1045 if (config
->debug
> 3) dumplcp(q
, l
);
1047 tunnelsend(b
, l
+ (q
- b
), t
); // send it
1049 else if (*p
== ProtocolRej
)
1056 if (l
> 5 && !(proto
& 1))
1063 if (proto
== PPPIPV6CP
)
1065 LOG(3, s
, t
, "IPv6 rejected\n");
1066 change_state(s
, ipv6cp
, Closed
);
1070 LOG(3, s
, t
, "LCP protocol reject: 0x%04X\n", proto
);
1073 else if (*p
== EchoReq
)
1075 *p
= EchoReply
; // reply
1076 *(uint32_t *) (p
+ 4) = htonl(session
[s
].magic
); // our magic number
1077 q
= makeppp(b
, sizeof(b
), p
, l
, s
, t
, PPPLCP
, 0, 0, 0);
1080 LOG(4, s
, t
, "LCP: send %s\n", ppp_code(*q
));
1081 if (config
->debug
> 3) dumplcp(q
, l
);
1083 tunnelsend(b
, l
+ (q
- b
), t
); // send it
1085 else if (*p
== EchoReply
)
1087 // Ignore it, last_packet time is set earlier than this.
1089 else if (*p
!= CodeRej
)
1091 ppp_code_rej(s
, t
, PPPLCP
, "LCP", p
, l
, b
, sizeof(b
));
1095 int join_bundle(sessionidt s
)
1097 // Search for a bundle to join
1100 for (i
= 1; i
< MAXBUNDLE
; i
++)
1102 if (bundle
[i
].state
!= BUNDLEFREE
)
1104 if (epdiscmp(session
[s
].epdis
,bundle
[i
].epdis
) && !strcmp(session
[s
].user
, bundle
[i
].user
))
1106 sessionidt first_ses
= bundle
[i
].members
[0];
1107 if (bundle
[i
].mssf
!= session
[s
].mssf
)
1109 // uniformity of sequence number format must be insured
1110 LOG(3, s
, session
[s
].tunnel
, "MPPP: unable to bundle session %d in bundle %d cause of different mssf\n", s
, i
);
1113 session
[s
].bundle
= i
;
1114 session
[s
].ip
= session
[first_ses
].ip
;
1115 session
[s
].dns1
= session
[first_ses
].dns1
;
1116 session
[s
].dns2
= session
[first_ses
].dns2
;
1117 session
[s
].timeout
= session
[first_ses
].timeout
;
1119 if(session
[s
].epdis
.length
> 0)
1120 setepdis(&bundle
[i
].epdis
, session
[s
].epdis
);
1122 strcpy(bundle
[i
].user
, session
[s
].user
);
1123 bundle
[i
].members
[bundle
[i
].num_of_links
] = s
;
1124 bundle
[i
].num_of_links
++;
1125 LOG(3, s
, session
[s
].tunnel
, "MPPP: Bundling additional line in bundle (%d), lines:%d\n",i
,bundle
[i
].num_of_links
);
1131 // No previously created bundle was found for this session, so create a new one
1132 if (!(b
= new_bundle())) return 0;
1134 session
[s
].bundle
= b
;
1135 bundle
[b
].mrru
= session
[s
].mrru
;
1136 bundle
[b
].mssf
= session
[s
].mssf
;
1137 // FIXME !!! to enable l2tpns reading mssf frames receiver_max_seq, sender_max_seq must be introduce
1138 // 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
1141 bundle[b].max_seq = 1 << 12;
1143 bundle
[b
].max_seq
= 1 << 24;
1144 if(session
[s
].epdis
.length
> 0)
1145 setepdis(&bundle
[b
].epdis
, session
[s
].epdis
);
1147 strcpy(bundle
[b
].user
, session
[s
].user
);
1148 bundle
[b
].members
[0] = s
;
1149 bundle
[b
].timeout
= session
[s
].timeout
;
1150 LOG(3, s
, session
[s
].tunnel
, "MPPP: Created a new bundle (%d)\n", b
);
1154 static int epdiscmp(epdist ep1
, epdist ep2
)
1157 if (ep1
.length
!= ep2
.length
)
1160 if (ep1
.addr_class
!= ep2
.addr_class
)
1163 for (ad
= 0; ad
< ep1
.length
; ad
++)
1164 if (ep1
.address
[ad
] != ep2
.address
[ad
])
1170 static void setepdis(epdist
*ep1
, epdist ep2
)
1173 ep1
->length
= ep2
.length
;
1174 ep1
->addr_class
= ep2
.addr_class
;
1175 for (ad
= 0; ad
< ep2
.length
; ad
++)
1176 ep1
->address
[ad
] = ep2
.address
[ad
];
1179 static bundleidt
new_bundle()
1182 for (i
= 1; i
< MAXBUNDLE
; i
++)
1184 if (bundle
[i
].state
== BUNDLEFREE
)
1186 LOG(4, 0, 0, "MPPP: Assigning bundle ID %d\n", i
);
1187 bundle
[i
].num_of_links
= 1;
1188 bundle
[i
].last_check
= time_now
; // Initialize last_check value
1189 bundle
[i
].state
= BUNDLEOPEN
;
1190 bundle
[i
].current_ses
= -1; // This is to enforce the first session 0 to be used at first
1191 memset(&frag
[i
], 0, sizeof(fragmentationt
));
1192 if (i
> config
->cluster_highest_bundleid
)
1193 config
->cluster_highest_bundleid
= i
;
1197 LOG(0, 0, 0, "MPPP: Can't find a free bundle! There shouldn't be this many in use!\n");
1201 static void ipcp_open(sessionidt s
, tunnelidt t
)
1203 LOG(3, s
, t
, "IPCP: Opened, session is now active\n");
1205 change_state(s
, ipcp
, Opened
);
1207 if (!(session
[s
].walled_garden
|| session
[s
].flags
& SESSION_STARTED
))
1209 uint16_t r
= radiusnew(s
);
1212 radiussend(r
, RADIUSSTART
); // send radius start
1214 // don't send further Start records if IPCP is restarted
1215 session
[s
].flags
|= SESSION_STARTED
;
1216 cluster_send_session(s
);
1220 // start IPv6 if configured and still in passive state
1221 if (session
[s
].ppp
.ipv6cp
== Stopped
)
1224 change_state(s
, ipv6cp
, RequestSent
);
1228 // Process IPCP messages
1229 void processipcp(sessionidt s
, tunnelidt t
, uint8_t *p
, uint16_t l
)
1231 uint8_t b
[MAXETHER
];
1237 LOG_HEX(5, "IPCP", p
, l
);
1240 LOG(1, s
, t
, "Short IPCP %d bytes\n", l
);
1241 STAT(tunnel_rx_errors
);
1245 if ((hl
= ntohs(*(uint16_t *) (p
+ 2))) > l
)
1247 LOG(1, s
, t
, "Length mismatch IPCP %u/%u\n", hl
, l
);
1248 STAT(tunnel_rx_errors
);
1253 if (session
[s
].ppp
.phase
< Network
)
1255 LOG(2, s
, t
, "IPCP %s ignored in %s phase\n", ppp_code(*p
), ppp_phase(session
[s
].ppp
.phase
));
1259 LOG(3, s
, t
, "IPCP: recv %s\n", ppp_code(*p
));
1261 if (*p
== ConfigAck
)
1263 switch (session
[s
].ppp
.ipcp
)
1266 initialise_restart_count(s
, ipcp
);
1267 change_state(s
, ipcp
, AckReceived
);
1272 LOG(2, s
, t
, "IPCP: ConfigAck in state %s? Sending ConfigReq\n", ppp_state(session
[s
].ppp
.ipcp
));
1274 change_state(s
, ipcp
, RequestSent
);
1282 LOG(2, s
, t
, "IPCP: ignoring %s in state %s\n", ppp_code(*p
), ppp_state(session
[s
].ppp
.ipcp
));
1285 else if (*p
== ConfigReq
)
1287 uint8_t *response
= 0;
1295 if (!o
[1] || o
[1] > length
) return;
1299 case 3: // ip address
1300 gotip
++; // seen address
1301 if (o
[1] != 6) return;
1303 addr
= htonl(session
[s
].ip
);
1304 if (memcmp(o
+ 2, &addr
, (sizeof addr
)))
1307 q
= ppp_conf_nak(s
, b
, sizeof(b
), PPPIPCP
, &response
, q
, p
, o
, (uint8_t *) &addr
, sizeof(addr
));
1308 if (!q
|| (q
!= oq
&& *response
== ConfigRej
))
1310 sessionshutdown(s
, "Can't negotiate IPCP.", CDN_ADMIN_DISC
, TERM_USER_ERROR
);
1317 case 129: // primary DNS
1318 if (o
[1] != 6) return;
1320 addr
= htonl(session
[s
].dns1
);
1321 if (memcmp(o
+ 2, &addr
, (sizeof addr
)))
1323 q
= ppp_conf_nak(s
, b
, sizeof(b
), PPPIPCP
, &response
, q
, p
, o
, (uint8_t *) &addr
, sizeof(addr
));
1329 case 131: // secondary DNS
1330 if (o
[1] != 6) return;
1332 addr
= htonl(session
[s
].dns2
);
1333 if (memcmp(o
+ 2, &addr
, sizeof(addr
)))
1335 q
= ppp_conf_nak(s
, b
, sizeof(b
), PPPIPCP
, &response
, q
, p
, o
, (uint8_t *) &addr
, sizeof(addr
));
1342 LOG(2, s
, t
, " Rejecting PPP IPCP Option type %d\n", *o
);
1343 q
= ppp_conf_rej(s
, b
, sizeof(b
), PPPIPCP
, &response
, q
, p
, o
);
1353 l
= q
- response
; // IPCP packet length
1354 *((uint16_t *) (response
+ 2)) = htons(l
); // update header
1358 // Send packet back as ConfigAck
1359 response
= makeppp(b
, sizeof(b
), p
, l
, s
, t
, PPPIPCP
, 0, 0, 0);
1360 if (!response
) return;
1361 *response
= ConfigAck
;
1365 LOG(1, s
, t
, "No IP in IPCP request\n");
1366 STAT(tunnel_rx_errors
);
1370 switch (session
[s
].ppp
.ipcp
)
1373 response
= makeppp(b
, sizeof(b
), p
, 2, s
, t
, PPPIPCP
, 0, 0, 0);
1374 if (!response
) return;
1375 *response
= TerminateAck
;
1376 *((uint16_t *) (response
+ 2)) = htons(l
= 4);
1380 initialise_restart_count(s
, ipcp
);
1382 if (*response
== ConfigAck
)
1383 change_state(s
, ipcp
, AckSent
);
1385 change_state(s
, ipcp
, RequestSent
);
1390 if (*response
== ConfigAck
)
1391 change_state(s
, ipcp
, AckSent
);
1396 if (*response
== ConfigAck
)
1402 initialise_restart_count(s
, ipcp
);
1407 if (*response
== ConfigAck
)
1408 change_state(s
, ipcp
, AckSent
);
1410 change_state(s
, ipcp
, RequestSent
);
1415 LOG(2, s
, t
, "IPCP: ignoring %s in state %s\n", ppp_code(*p
), ppp_state(session
[s
].ppp
.ipcp
));
1419 LOG(3, s
, t
, "IPCP: send %s\n", ppp_code(*response
));
1420 tunnelsend(b
, l
+ (response
- b
), t
);
1422 else if (*p
== TerminateReq
)
1424 switch (session
[s
].ppp
.ipcp
)
1436 zero_restart_count(s
, ipcp
);
1437 change_state(s
, ipcp
, Closing
);
1441 LOG(2, s
, t
, "IPCP: ignoring %s in state %s\n", ppp_code(*p
), ppp_state(session
[s
].ppp
.ipcp
));
1445 *p
= TerminateAck
; // send ack
1446 q
= makeppp(b
, sizeof(b
), p
, l
, s
, t
, PPPIPCP
, 0, 0, 0);
1449 LOG(3, s
, t
, "IPCP: send %s\n", ppp_code(*q
));
1450 tunnelsend(b
, l
+ (q
- b
), t
); // send it
1452 else if (*p
!= CodeRej
)
1454 ppp_code_rej(s
, t
, PPPIPCP
, "IPCP", p
, l
, b
, sizeof(b
));
1458 static void ipv6cp_open(sessionidt s
, tunnelidt t
)
1460 LOG(3, s
, t
, "IPV6CP: Opened\n");
1462 change_state(s
, ipv6cp
, Opened
);
1463 if (session
[s
].ipv6prefixlen
)
1464 route6set(s
, session
[s
].ipv6route
, session
[s
].ipv6prefixlen
, 1);
1466 // Send an initial RA (TODO: Should we send these regularly?)
1467 send_ipv6_ra(s
, t
, NULL
);
1470 // Process IPV6CP messages
1471 void processipv6cp(sessionidt s
, tunnelidt t
, uint8_t *p
, uint16_t l
)
1473 uint8_t b
[MAXETHER
];
1477 CSTAT(processipv6cp
);
1479 LOG_HEX(5, "IPV6CP", p
, l
);
1482 LOG(1, s
, t
, "Short IPV6CP %d bytes\n", l
);
1483 STAT(tunnel_rx_errors
);
1487 if ((hl
= ntohs(*(uint16_t *) (p
+ 2))) > l
)
1489 LOG(1, s
, t
, "Length mismatch IPV6CP %u/%u\n", hl
, l
);
1490 STAT(tunnel_rx_errors
);
1495 if (session
[s
].ppp
.phase
< Network
)
1497 LOG(2, s
, t
, "IPV6CP %s ignored in %s phase\n", ppp_code(*p
), ppp_phase(session
[s
].ppp
.phase
));
1501 LOG(3, s
, t
, "IPV6CP: recv %s\n", ppp_code(*p
));
1505 LOG(3, s
, t
, "IPV6CP: no IPv4 address (IPCP in state %s)\n", ppp_state(session
[s
].ppp
.ipcp
));
1506 return; // need IPCP to complete...
1509 if (*p
== ConfigAck
)
1511 switch (session
[s
].ppp
.ipv6cp
)
1514 initialise_restart_count(s
, ipv6cp
);
1515 change_state(s
, ipv6cp
, AckReceived
);
1520 LOG(2, s
, t
, "IPV6CP: ConfigAck in state %s? Sending ConfigReq\n", ppp_state(session
[s
].ppp
.ipv6cp
));
1522 change_state(s
, ipv6cp
, RequestSent
);
1530 LOG(2, s
, t
, "IPV6CP: ignoring %s in state %s\n", ppp_code(*p
), ppp_state(session
[s
].ppp
.ipv6cp
));
1533 else if (*p
== ConfigReq
)
1535 uint8_t *response
= 0;
1543 if (!o
[1] || o
[1] > length
) return;
1547 case 1: // interface identifier
1548 gotip
++; // seen address
1549 if (o
[1] != 10) return;
1551 *(uint32_t *) ident
= htonl(session
[s
].ip
);
1552 *(uint32_t *) (ident
+ 4) = 0;
1554 if (memcmp(o
+ 2, ident
, sizeof(ident
)))
1556 q
= ppp_conf_nak(s
, b
, sizeof(b
), PPPIPV6CP
, &response
, q
, p
, o
, ident
, sizeof(ident
));
1563 LOG(2, s
, t
, " Rejecting PPP IPV6CP Option type %d\n", *o
);
1564 q
= ppp_conf_rej(s
, b
, sizeof(b
), PPPIPV6CP
, &response
, q
, p
, o
);
1574 l
= q
- response
; // IPV6CP packet length
1575 *((uint16_t *) (response
+ 2)) = htons(l
); // update header
1579 // Send packet back as ConfigAck
1580 response
= makeppp(b
, sizeof(b
), p
, l
, s
, t
, PPPIPV6CP
, 0, 0, 0);
1581 if (!response
) return;
1582 *response
= ConfigAck
;
1586 LOG(1, s
, t
, "No interface identifier in IPV6CP request\n");
1587 STAT(tunnel_rx_errors
);
1591 switch (session
[s
].ppp
.ipv6cp
)
1594 response
= makeppp(b
, sizeof(b
), p
, 2, s
, t
, PPPIPV6CP
, 0, 0, 0);
1595 if (!response
) return;
1596 *response
= TerminateAck
;
1597 *((uint16_t *) (response
+ 2)) = htons(l
= 4);
1601 initialise_restart_count(s
, ipv6cp
);
1603 if (*response
== ConfigAck
)
1604 change_state(s
, ipv6cp
, AckSent
);
1606 change_state(s
, ipv6cp
, RequestSent
);
1611 if (*response
== ConfigAck
)
1612 change_state(s
, ipv6cp
, AckSent
);
1617 if (*response
== ConfigAck
)
1623 initialise_restart_count(s
, ipv6cp
);
1628 if (*response
== ConfigAck
)
1629 change_state(s
, ipv6cp
, AckSent
);
1631 change_state(s
, ipv6cp
, RequestSent
);
1636 LOG(2, s
, t
, "IPV6CP: ignoring %s in state %s\n", ppp_code(*p
), ppp_state(session
[s
].ppp
.ipv6cp
));
1640 LOG(3, s
, t
, "IPV6CP: send %s\n", ppp_code(*response
));
1641 tunnelsend(b
, l
+ (response
- b
), t
);
1643 else if (*p
== TerminateReq
)
1645 switch (session
[s
].ppp
.ipv6cp
)
1657 zero_restart_count(s
, ipv6cp
);
1658 change_state(s
, ipv6cp
, Closing
);
1662 LOG(2, s
, t
, "IPV6CP: ignoring %s in state %s\n", ppp_code(*p
), ppp_state(session
[s
].ppp
.ipv6cp
));
1666 *p
= TerminateAck
; // send ack
1667 q
= makeppp(b
, sizeof(b
), p
, l
, s
, t
, PPPIPV6CP
, 0, 0, 0);
1670 LOG(3, s
, t
, "IPV6CP: send %s\n", ppp_code(*q
));
1671 tunnelsend(b
, l
+ (q
- b
), t
); // send it
1673 else if (*p
!= CodeRej
)
1675 ppp_code_rej(s
, t
, PPPIPV6CP
, "IPV6CP", p
, l
, b
, sizeof(b
));
1679 static void update_sessions_in_stat(sessionidt s
, uint16_t l
)
1681 bundleidt b
= session
[s
].bundle
;
1684 increment_counter(&session
[s
].cin
, &session
[s
].cin_wrap
, l
);
1685 session
[s
].cin_delta
+= l
;
1688 sess_local
[s
].cin
+= l
;
1689 sess_local
[s
].pin
++;
1693 int i
= frag
[b
].re_frame_begin_index
;
1694 int end
= frag
[b
].re_frame_end_index
;
1697 l
= frag
[b
].fragment
[i
].length
;
1698 s
= frag
[b
].fragment
[i
].sid
;
1699 increment_counter(&session
[s
].cin
, &session
[s
].cin_wrap
, l
);
1700 session
[s
].cin_delta
+= l
;
1703 sess_local
[s
].cin
+= l
;
1704 sess_local
[s
].pin
++;
1707 i
= (i
+ 1) & MAXFRAGNUM_MASK
;
1712 // process IP packet received
1714 // This MUST be called with at least 4 byte behind 'p'.
1715 // (i.e. this routine writes to p[-4]).
1716 void processipin(sessionidt s
, tunnelidt t
, uint8_t *p
, uint16_t l
)
1722 LOG_HEX(5, "IP", p
, l
);
1726 LOG(1, s
, t
, "IP packet too short %d\n", l
);
1727 STAT(tunnel_rx_errors
);
1731 ip
= ntohl(*(uint32_t *)(p
+ 12));
1735 LOG(1, s
, t
, "IP packet too long %d\n", l
);
1736 STAT(tunnel_rx_errors
);
1740 if (session
[s
].ppp
.phase
!= Network
|| session
[s
].ppp
.ipcp
!= Opened
)
1743 if (!session
[s
].bundle
|| bundle
[session
[s
].bundle
].num_of_links
< 2) // FIXME:
1745 // no spoof (do sessionbyip to handled statically routed subnets)
1746 if (ip
!= session
[s
].ip
&& sessionbyip(htonl(ip
)) != s
)
1748 LOG(4, s
, t
, "Dropping packet with spoofed IP %s\n", fmtaddr(htonl(ip
), 0));
1753 // run access-list if any
1754 if (session
[s
].filter_in
&& !ip_filter(p
, l
, session
[s
].filter_in
- 1))
1757 // adjust MSS on SYN and SYN,ACK packets with options
1758 if ((ntohs(*(uint16_t *) (p
+ 6)) & 0x1fff) == 0 && p
[9] == IPPROTO_TCP
) // first tcp fragment
1760 int ihl
= (p
[0] & 0xf) * 4; // length of IP header
1761 if (l
>= ihl
+ 20 && (p
[ihl
+ 13] & TCP_FLAG_SYN
) && ((p
[ihl
+ 12] >> 4) > 5))
1762 adjust_tcp_mss(s
, t
, p
, l
, p
+ ihl
);
1765 // Add on the tun header
1767 *(uint32_t *) p
= htonl(PKTIP
);
1770 if (session
[s
].tbf_in
)
1772 // Are we throttling this session?
1773 if (config
->cluster_iam_master
)
1774 tbf_queue_packet(session
[s
].tbf_in
, p
, l
);
1776 master_throttle_packet(session
[s
].tbf_in
, p
, l
);
1781 if (tun_write(p
, l
) < 0)
1783 STAT(tun_tx_errors
);
1784 LOG(0, s
, t
, "Error writing %d bytes to TUN device: %s (tunfd=%d, p=%p)\n",
1785 l
, strerror(errno
), tunfd
, p
);
1793 if (session
[s
].snoop_ip
&& session
[s
].snoop_port
)
1795 // Snooping this session
1796 snoop_send_packet(p
, l
, session
[s
].snoop_ip
, session
[s
].snoop_port
);
1799 update_sessions_in_stat(s
, l
);
1803 STAT(tun_tx_packets
);
1804 INC_STAT(tun_tx_bytes
, l
);
1807 // process Multilink PPP packet received
1808 void processmpin(sessionidt s
, tunnelidt t
, uint8_t *p
, uint16_t l
)
1810 bundleidt b
= session
[s
].bundle
;
1811 bundlet
* this_bundle
= &bundle
[b
];
1812 uint32_t frag_offset
, M_offset
;
1813 uint16_t frag_index
, M_index
;
1814 fragmentationt
*this_fragmentation
= &frag
[b
];
1815 uint8_t begin_frame
= (*p
& MP_BEGIN
);
1816 uint8_t end_frame
= (*p
& MP_END
);
1819 uint16_t begin_index
, end_index
;
1821 // Perform length checking
1824 LOG(2, s
, t
, "MPPP: discarding fragment larger than MAXFRAGLEN\n");
1830 LOG(2, s
, t
, "MPPP: Invalid bundle id: 0\n");
1833 // FIXME !! session[s].mssf means that the receiver wants to receive frames in mssf not means the receiver will send frames in mssf
1834 /* if(session[s].mssf)
1836 // Get 12 bit for seq number
1837 seq_num = ntohs((*(uint16_t *) p) & 0xFF0F);
1840 // After this point the pointer should be advanced 2 bytes
1841 LOG(3, s, t, "MPPP: 12 bits, sequence number: %d\n",seq_num);
1845 // Get 24 bit for seq number
1846 seq_num
= ntohl((*(uint32_t *) p
) & 0xFFFFFF00);
1849 // After this point the pointer should be advanced 4 bytes
1850 LOG(4, s
, t
, "MPPP: 24 bits sequence number:%d\n",seq_num
);
1853 // calculate this fragment's offset from the begin seq in the bundle
1854 frag_offset
= (seq_num
+ this_bundle
->max_seq
- this_fragmentation
->start_seq
) & (this_bundle
->max_seq
-1);
1856 // discard this fragment if frag_offset is bigger that the fragmentation buffer size
1857 if (frag_offset
>= MAXFRAGNUM
)
1859 LOG(3, s
, t
, "MPPP: Index out of range, received more than MAXFRAGNUM fragment (lost frag) seq:%d, begin_seq:%d, bundle:%d, max:%d\n",seq_num
, this_fragmentation
->start_seq
, b
, this_bundle
->max_seq
);
1864 sess_local
[s
].last_seq
= seq_num
;
1865 if (seq_num
< this_fragmentation
->M
)
1866 this_fragmentation
->M
= seq_num
;
1869 uint32_t i
, min
= sess_local
[(this_bundle
->members
[0])].last_seq
;;
1870 for (i
= 1; i
< this_bundle
->num_of_links
; i
++)
1872 uint32_t s_seq
= sess_local
[(this_bundle
->members
[i
])].last_seq
;
1876 this_fragmentation
->M
= min
;
1879 LOG(4, s
, t
, "MPPP: Setting M to %d\n", this_fragmentation
->M
);
1880 //calculate M's offset from the begin seq in the bundle
1881 M_offset
= (this_fragmentation
->M
+ this_bundle
->max_seq
- this_fragmentation
->start_seq
) & (this_bundle
->max_seq
-1);
1883 //caculate M's index in the fragment array
1884 M_index
= (M_offset
+ this_fragmentation
->start_index
) & MAXFRAGNUM_MASK
;
1886 //caculate received fragment's index in the fragment array
1887 frag_index
= (frag_offset
+ this_fragmentation
->start_index
) & MAXFRAGNUM_MASK
;
1889 //frame with a single fragment
1890 if (begin_frame
&& end_frame
)
1892 // process and reset fragmentation
1893 LOG(4, s
, t
, "MPPP: Both bits are set (Begin and End).\n");
1894 this_fragmentation
->fragment
[frag_index
].length
= l
;
1895 this_fragmentation
->fragment
[frag_index
].sid
= s
;
1896 this_fragmentation
->fragment
[frag_index
].flags
= flags
;
1897 this_fragmentation
->fragment
[frag_index
].seq
= seq_num
;
1898 this_fragmentation
->re_frame_begin_index
= frag_index
;
1899 this_fragmentation
->re_frame_end_index
= frag_index
;
1900 processmpframe(s
, t
, p
, l
, 0);
1901 this_fragmentation
->fragment
[frag_index
].length
= 0;
1902 this_fragmentation
->fragment
[frag_index
].flags
= 0;
1903 end_index
= frag_index
;
1907 // insert the frame in it's place
1908 fragmentt
*this_frag
= &this_fragmentation
->fragment
[frag_index
];
1909 this_frag
->length
= l
;
1911 this_frag
->flags
= flags
;
1912 this_frag
->seq
= seq_num
;
1913 memcpy(this_frag
->data
, p
, l
);
1915 // try to assemble the frame that has the received fragment as a member
1916 // get the beginning of this frame
1917 begin_index
= end_index
= frag_index
;
1918 while (this_fragmentation
->fragment
[begin_index
].length
)
1920 if (this_fragmentation
->fragment
[begin_index
].flags
& MP_BEGIN
)
1922 begin_index
= (begin_index
? (begin_index
-1) : (MAXFRAGNUM
-1));
1925 // return if a lost fragment is found
1926 if (!(this_fragmentation
->fragment
[begin_index
].length
))
1927 return; // assembling frame failed
1928 // get the end of his frame
1929 while (this_fragmentation
->fragment
[end_index
].length
)
1931 if (this_fragmentation
->fragment
[end_index
].flags
& MP_END
)
1933 end_index
= (end_index
+1) & MAXFRAGNUM_MASK
;
1936 // return if a lost fragment is found
1937 if (!(this_fragmentation
->fragment
[end_index
].length
))
1938 return; // assembling frame failed
1940 // assemble the packet
1941 //assemble frame, process it, reset fragmentation
1942 uint16_t cur_len
= 4; // This is set to 4 to leave 4 bytes for function processipin
1945 LOG(4, s
, t
, "MPPP: processing fragments from %d to %d\n", begin_index
, end_index
);
1946 // Push to the receive buffer
1948 for (i
= begin_index
;; i
= (i
+ 1) & MAXFRAGNUM_MASK
)
1950 this_frag
= &this_fragmentation
->fragment
[i
];
1951 if(cur_len
+ this_frag
->length
> MAXETHER
)
1953 LOG(2, s
, t
, "MPPP: discarding reassembled frames larger than MAXETHER\n");
1956 memcpy(this_fragmentation
->reassembled_frame
+cur_len
, this_frag
->data
, this_frag
->length
);
1957 LOG(5, s
, t
, "MPPP: processing frame at %d, with len %d\n", i
, this_frag
->length
);
1958 cur_len
+= this_frag
->length
;
1961 this_fragmentation
->re_frame_len
= cur_len
;
1962 this_fragmentation
->re_frame_begin_index
= begin_index
;
1963 this_fragmentation
->re_frame_end_index
= end_index
;
1964 // Process the resassembled frame
1965 LOG(5, s
, t
, "MPPP: Process the reassembled frame, len=%d\n",cur_len
);
1966 processmpframe(s
, t
, this_fragmentation
->reassembled_frame
, this_fragmentation
->re_frame_len
, 1);
1970 // Set reassembled frame length to zero after processing it
1971 this_fragmentation
->re_frame_len
= 0;
1972 for (i
= begin_index
;; i
= (i
+ 1) & MAXFRAGNUM_MASK
)
1974 this_fragmentation
->fragment
[i
].length
= 0; // Indicates that this fragment has been consumed
1975 this_fragmentation
->fragment
[i
].flags
= 0;
1980 //discard fragments received before the recently assembled frame
1981 begin_index
= this_fragmentation
->start_index
;
1982 this_fragmentation
->start_index
= (end_index
+ 1) & MAXFRAGNUM_MASK
;
1983 this_fragmentation
->start_seq
= (this_fragmentation
->fragment
[end_index
].seq
+ 1) & (this_bundle
->max_seq
-1);
1984 //clear length and flags of the discarded fragments
1985 while (begin_index
!= this_fragmentation
->start_index
)
1987 this_fragmentation
->fragment
[begin_index
].flags
= 0;
1988 this_fragmentation
->fragment
[begin_index
].length
= 0;
1989 begin_index
= (begin_index
+ 1) & MAXFRAGNUM_MASK
;
1992 LOG(4, s
, t
, "MPPP after assembling: M index is =%d, start index is = %d, start seq=%d\n",M_index
, this_fragmentation
->start_index
, this_fragmentation
->start_seq
);
1996 // process IPv6 packet received
1998 // This MUST be called with at least 4 byte behind 'p'.
1999 // (i.e. this routine writes to p[-4]).
2000 void processipv6in(sessionidt s
, tunnelidt t
, uint8_t *p
, uint16_t l
)
2005 CSTAT(processipv6in
);
2007 LOG_HEX(5, "IPv6", p
, l
);
2009 ip
= *(struct in6_addr
*) (p
+ 8);
2010 ipv4
= ntohl(*(uint32_t *)(p
+ 16));
2014 LOG(1, s
, t
, "IP packet too long %d\n", l
);
2015 STAT(tunnel_rx_errors
);
2019 if (session
[s
].ppp
.phase
!= Network
|| session
[s
].ppp
.ipv6cp
!= Opened
)
2023 if (ipv4
!= session
[s
].ip
&& memcmp(&config
->ipv6_prefix
, &ip
, 8) && sessionbyipv6(ip
) != s
)
2025 char str
[INET6_ADDRSTRLEN
];
2026 LOG(5, s
, t
, "Dropping packet with spoofed IP %s\n",
2027 inet_ntop(AF_INET6
, &ip
, str
, INET6_ADDRSTRLEN
));
2031 // Check if it's a Router Solicition message.
2032 if (*(p
+ 6) == 58 && *(p
+ 7) == 255 && *(p
+ 24) == 0xFF && *(p
+ 25) == 2 &&
2033 *(uint32_t *)(p
+ 26) == 0 && *(uint32_t *)(p
+ 30) == 0 &&
2034 *(uint32_t *)(p
+ 34) == 0 &&
2035 *(p
+ 38) == 0 && *(p
+ 39) == 2 && *(p
+ 40) == 133) {
2036 LOG(3, s
, t
, "Got IPv6 RS\n");
2037 send_ipv6_ra(s
, t
, &ip
);
2041 // Add on the tun header
2043 *(uint32_t *) p
= htonl(PKTIPV6
);
2046 // Are we throttled and a slave?
2047 if (session
[s
].tbf_in
&& !config
->cluster_iam_master
) {
2048 // Pass it to the master for handling.
2049 master_throttle_packet(session
[s
].tbf_in
, p
, l
);
2053 // Are we throttled and a master??
2054 if (session
[s
].tbf_in
&& config
->cluster_iam_master
) {
2055 // Actually handle the throttled packets.
2056 tbf_queue_packet(session
[s
].tbf_in
, p
, l
);
2061 if (tun_write(p
, l
) < 0)
2063 STAT(tun_tx_errors
);
2064 LOG(0, s
, t
, "Error writing %d bytes to TUN device: %s (tunfd=%d, p=%p)\n",
2065 l
, strerror(errno
), tunfd
, p
);
2073 if (session
[s
].snoop_ip
&& session
[s
].snoop_port
)
2075 // Snooping this session
2076 snoop_send_packet(p
, l
, session
[s
].snoop_ip
, session
[s
].snoop_port
);
2079 update_sessions_in_stat(s
, l
);
2083 STAT(tun_tx_packets
);
2084 INC_STAT(tun_tx_bytes
, l
);
2088 // Helper routine for the TBF filters.
2089 // Used to send queued data in from the user.
2091 void send_ipin(sessionidt s
, uint8_t *buf
, int len
)
2093 LOG_HEX(5, "IP in throttled", buf
, len
);
2095 if (write(tunfd
, buf
, len
) < 0)
2097 STAT(tun_tx_errors
);
2098 LOG(0, 0, 0, "Error writing %d bytes to TUN device: %s (tunfd=%d, p=%p)\n",
2099 len
, strerror(errno
), tunfd
, buf
);
2107 if (session
[s
].snoop_ip
&& session
[s
].snoop_port
)
2109 // Snooping this session
2110 snoop_send_packet(buf
, len
, session
[s
].snoop_ip
, session
[s
].snoop_port
);
2113 // Increment packet counters
2114 increment_counter(&session
[s
].cin
, &session
[s
].cin_wrap
, len
);
2115 session
[s
].cin_delta
+= len
;
2118 sess_local
[s
].cin
+= len
;
2119 sess_local
[s
].pin
++;
2123 STAT(tun_tx_packets
);
2124 INC_STAT(tun_tx_bytes
, len
- 4);
2128 // Process CCP messages
2129 void processccp(sessionidt s
, tunnelidt t
, uint8_t *p
, uint16_t l
)
2131 uint8_t b
[MAXETHER
];
2136 LOG_HEX(5, "CCP", p
, l
);
2138 if (session
[s
].ppp
.phase
< Network
)
2140 LOG(2, s
, t
, "CCP %s ignored in %s phase\n", ppp_code(*p
), ppp_phase(session
[s
].ppp
.phase
));
2146 LOG(1, s
, t
, "Short CCP packet\n");
2147 STAT(tunnel_rx_errors
);
2150 LOG(4, s
, t
, "CCP: recv %s\n", ppp_code(*p
));
2151 if (*p
== ConfigAck
)
2153 switch (session
[s
].ppp
.ccp
)
2156 initialise_restart_count(s
, ccp
);
2157 change_state(s
, ccp
, AckReceived
);
2162 LOG(2, s
, t
, "CCP: ConfigAck in state %s? Sending ConfigReq\n", ppp_state(session
[s
].ppp
.ccp
));
2164 change_state(s
, ccp
, RequestSent
);
2168 LOG(3, s
, t
, "CCP: Opened\n");
2169 change_state(s
, ccp
, Opened
);
2173 LOG(2, s
, t
, "CCP: ignoring %s in state %s\n", ppp_code(*p
), ppp_state(session
[s
].ppp
.ccp
));
2176 else if (*p
== ConfigReq
)
2178 if (l
< 6) // accept no compression
2180 else // compression requested--reject
2183 q
= makeppp(b
, sizeof(b
), p
, l
, s
, t
, PPPCCP
, 0, 0, 0);
2186 switch (session
[s
].ppp
.ccp
)
2189 q
= makeppp(b
, sizeof(b
), p
, 2, s
, t
, PPPCCP
, 0, 0, 0);
2192 *((uint16_t *) (q
+ 2)) = htons(l
= 4);
2196 initialise_restart_count(s
, ccp
);
2198 if (*q
== ConfigAck
)
2199 change_state(s
, ccp
, AckSent
);
2201 change_state(s
, ccp
, RequestSent
);
2206 if (*q
== ConfigAck
)
2207 change_state(s
, ccp
, AckSent
);
2212 if (*q
== ConfigAck
)
2213 change_state(s
, ccp
, Opened
);
2218 initialise_restart_count(s
, ccp
);
2223 if (*q
== ConfigAck
)
2224 change_state(s
, ccp
, AckSent
);
2226 change_state(s
, ccp
, RequestSent
);
2231 LOG(2, s
, t
, "CCP: ignoring %s in state %s\n", ppp_code(*p
), ppp_state(session
[s
].ppp
.ccp
));
2235 LOG(4, s
, t
, "CCP: send %s\n", ppp_code(*q
));
2236 tunnelsend(b
, l
+ (q
- b
), t
);
2238 else if (*p
== TerminateReq
)
2241 q
= makeppp(b
, sizeof(b
), p
, l
, s
, t
, PPPCCP
, 0, 0, 0);
2243 LOG(3, s
, t
, "CCP: send %s\n", ppp_code(*q
));
2244 tunnelsend(b
, l
+ (q
- b
), t
);
2245 change_state(s
, ccp
, Stopped
);
2247 else if (*p
!= CodeRej
)
2249 ppp_code_rej(s
, t
, PPPCCP
, "CCP", p
, l
, b
, sizeof(b
));
2253 // send a CHAP challenge
2254 void sendchap(sessionidt s
, tunnelidt t
)
2256 uint8_t b
[MAXETHER
];
2265 LOG(1, s
, t
, "No RADIUS to send challenge\n");
2266 STAT(tunnel_tx_errors
);
2270 LOG(1, s
, t
, "Send CHAP challenge\n");
2272 radius
[r
].chap
= 1; // CHAP not PAP
2274 if (radius
[r
].state
!= RADIUSCHAP
)
2277 radius
[r
].state
= RADIUSCHAP
;
2278 radius
[r
].retry
= backoff(radius
[r
].try++);
2279 if (radius
[r
].try > 5)
2281 sessionshutdown(s
, "CHAP timeout.", CDN_ADMIN_DISC
, TERM_REAUTHENTICATION_FAILURE
);
2282 STAT(tunnel_tx_errors
);
2285 q
= makeppp(b
, sizeof(b
), 0, 0, s
, t
, PPPCHAP
, 0, 0, 0);
2288 *q
= 1; // challenge
2289 q
[1] = radius
[r
].id
; // ID
2290 q
[4] = 16; // value size (size of challenge)
2291 memcpy(q
+ 5, radius
[r
].auth
, 16); // challenge
2292 strcpy((char *) q
+ 21, hostname
); // our name
2293 *(uint16_t *) (q
+ 2) = htons(strlen(hostname
) + 21); // length
2294 tunnelsend(b
, strlen(hostname
) + 21 + (q
- b
), t
); // send it
2297 // fill in a L2TP message with a PPP frame,
2298 // returns start of PPP frame
2299 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
)
2301 uint16_t hdr
= 0x0002; // L2TP with no options
2302 uint16_t type
= mtype
;
2305 if (size
< 16) // Need more space than this!!
2307 LOG(0, s
, t
, "makeppp buffer too small for L2TP header (size=%d)\n", size
);
2311 if (prio
) hdr
|= 0x0100; // set priority bit
2313 *(uint16_t *) (b
+ 0) = htons(hdr
);
2314 *(uint16_t *) (b
+ 2) = htons(tunnel
[t
].far
); // tunnel
2315 *(uint16_t *) (b
+ 4) = htons(session
[s
].far
); // session
2318 // Check whether this session is part of multilink
2321 if (bundle
[bid
].num_of_links
> 1)
2322 type
= PPPMP
; // Change PPP message type to the PPPMP
2327 if (type
== PPPLCP
|| !(session
[s
].flags
& SESSION_ACFC
))
2329 *(uint16_t *) b
= htons(0xFF03); // HDLC header
2333 if (type
< 0x100 && session
[s
].flags
& SESSION_PFC
)
2339 *(uint16_t *) b
= htons(type
);
2345 // Set the sequence number and (B)egin (E)nd flags
2346 if (session
[s
].mssf
)
2348 // Set the multilink bits
2349 uint16_t bits_send
= mp_bits
;
2350 *(uint16_t *) b
= htons((bundle
[bid
].seq_num_t
& 0x0FFF)|bits_send
);
2355 *(uint32_t *) b
= htonl(bundle
[bid
].seq_num_t
);
2356 // Set the multilink bits
2361 bundle
[bid
].seq_num_t
++;
2363 // Add the message type if this fragment has the begin bit set
2364 if (mp_bits
& MP_BEGIN
)
2366 //*b++ = mtype; // The next two lines are instead of this
2367 *(uint16_t *) b
= htons(mtype
); // Message type
2372 if ((b
- start
) + l
> size
)
2374 LOG(2, s
, t
, "makeppp would overflow buffer (size=%d, header+payload=%d)\n", size
, (b
- start
) + l
);
2385 static int add_lcp_auth(uint8_t *b
, int size
, int authtype
)
2388 if ((authtype
== AUTHCHAP
&& size
< 5) || size
< 4)
2391 *b
++ = 3; // Authentication-Protocol
2392 if (authtype
== AUTHCHAP
)
2394 len
= *b
++ = 5; // length
2395 *(uint16_t *) b
= htons(PPPCHAP
); b
+= 2;
2398 else if (authtype
== AUTHPAP
)
2400 len
= *b
++ = 4; // length
2401 *(uint16_t *) b
= htons(PPPPAP
); b
+= 2;
2405 LOG(0, 0, 0, "add_lcp_auth called with unsupported auth type %d\n", authtype
);
2411 // Send LCP ConfigReq for MRU, authentication type and magic no
2412 void sendlcp(sessionidt s
, tunnelidt t
)
2414 uint8_t b
[500], *q
, *l
;
2415 int authtype
= sess_local
[s
].lcp_authtype
;
2417 if (!(q
= makeppp(b
, sizeof(b
), NULL
, 0, s
, t
, PPPLCP
, 0, 0, 0)))
2420 LOG(3, s
, t
, "LCP: send ConfigReq%s%s%s including MP options\n",
2421 authtype
? " (" : "",
2422 authtype
? (authtype
== AUTHCHAP
? "CHAP" : "PAP") : "",
2423 authtype
? ")" : "");
2427 *l
++ = ++sess_local
[s
].lcp_ident
; // ID
2429 l
+= 2; //Save space for length
2431 if (sess_local
[s
].ppp_mru
)
2433 *l
++ = 1; *l
++ = 4; // Maximum-Receive-Unit (length 4)
2434 *(uint16_t *) l
= htons(sess_local
[s
].ppp_mru
); l
+= 2;
2438 l
+= add_lcp_auth(l
, sizeof(b
) - (l
- b
), authtype
);
2440 if (session
[s
].magic
)
2442 *l
++ = 5; *l
++ = 6; // Magic-Number (length 6)
2443 *(uint32_t *) l
= htonl(session
[s
].magic
);
2447 if (sess_local
[s
].mp_mrru
)
2449 *l
++ = 17; *l
++ = 4; // Multilink Max-Receive-Reconstructed-Unit (length 4)
2450 *(uint16_t *) l
= htons(sess_local
[s
].mp_mrru
); l
+= 2;
2453 if (sess_local
[s
].mp_epdis
)
2455 *l
++ = 19; *l
++ = 7; // Multilink Endpoint Discriminator (length 7)
2456 *l
++ = IPADDR
; // Endpoint Discriminator class
2457 *(uint32_t *) l
= htonl(sess_local
[s
].mp_epdis
);
2461 *(uint16_t *)(q
+ 2) = htons(l
- q
); // Length
2463 LOG_HEX(5, "PPPLCP", q
, l
- q
);
2464 if (config
->debug
> 3) dumplcp(q
, l
- q
);
2466 tunnelsend(b
, (l
- b
), t
);
2467 restart_timer(s
, lcp
);
2470 // Send CCP request for no compression
2471 void sendccp(sessionidt s
, tunnelidt t
)
2475 if (!(q
= makeppp(b
, sizeof(b
), NULL
, 0, s
, t
, PPPCCP
, 0, 0, 0)))
2478 LOG(3, s
, t
, "CCP: send ConfigReq (no compression)\n");
2481 *(q
+ 1) = ++sess_local
[s
].lcp_ident
; // ID
2482 *(uint16_t *)(q
+ 2) = htons(4); // Length
2484 LOG_HEX(5, "PPPCCP", q
, 4);
2485 tunnelsend(b
, (q
- b
) + 4 , t
);
2486 restart_timer(s
, ccp
);
2489 // Reject unknown/unconfigured protocols
2490 void protoreject(sessionidt s
, tunnelidt t
, uint8_t *p
, uint16_t l
, uint16_t proto
)
2493 uint8_t buf
[MAXETHER
];
2495 int mru
= session
[s
].mru
;
2496 if (mru
< MINMTU
) mru
= MINMTU
;
2497 if (mru
> sizeof(buf
)) mru
= sizeof(buf
);
2500 if (l
> mru
) l
= mru
;
2502 q
= makeppp(buf
, sizeof(buf
), 0, 0, s
, t
, PPPLCP
, 0, 0, 0);
2506 *(q
+ 1) = ++sess_local
[s
].lcp_ident
;
2507 *(uint16_t *)(q
+ 2) = htons(l
);
2508 *(uint16_t *)(q
+ 4) = htons(proto
);
2509 memcpy(q
+ 6, p
, l
- 6);
2511 if (proto
== PPPIPV6CP
)
2512 LOG(3, s
, t
, "LCP: send ProtocolRej (IPV6CP: not configured)\n");
2514 LOG(2, s
, t
, "LCP: sent ProtocolRej (0x%04X: unsupported)\n", proto
);
2516 tunnelsend(buf
, l
+ (q
- buf
), t
);