2 * Add functionality "LAC" to l2tpns.
3 * Used to forward a ppp session to another "LNS".
15 /* sequence diagram: Client <--> LAC <--> LNS1 <--> LNS2
18 * Client <-------------------> LAC
19 * Challenge (CHAP/PAP)
20 * Client <-------------------> LAC
22 * LAC --------------------> LNS1 (Tunnel Open)
24 * LAC <-------------------- LNS1 (Tunnel Open)
26 * LAC --------------------> LNS1 (Tunnel Open)
28 * LAC <-------------------- LNS1 (Tunnel Open)
30 * LAC --------------------> LNS1 (Session Open)
32 * LAC <-------------------- LNS1 (Session Open)
34 * LAC --------------------> LNS1 (Session Open)
36 * LAC <-------------------- LNS1 (Session Open)
38 * Client <---------------------------------------------> LNS1
39 * Challenge (CHAP/PAP)
40 * Client <---------------------------------------------> LNS1
42 * LNS1 --------------------> LNS2 (Tunnel Open)
44 * LNS1 <-------------------- LNS2 (Tunnel Open)
46 * LNS1 --------------------> LNS2 (Tunnel Open)
48 * LNS1 <-------------------- LNS2 (Tunnel Open)
50 * LNS1 --------------------> LNS2 (Session Open)
52 * LNS1 <-------------------- LNS2 (Session Open)
54 * LNS1 --------------------> LNS2 (Session Open)
56 * LNS1 <-------------------- LNS2 (Session Open)
58 * Client <------------------------------------------------------------------------> LNS2
59 * PAP/CHAP Authentification
60 * Client <------------------------------------------------------------------------> LNS2
62 * Client <------------------------------------------------------------------------> LNS2
68 uint32_t tunnel_medium_type
;
69 in_addr_t tunnel_server_endpoint
; /* IP remote LNS */
70 char tunnel_password
[64]; /* l2tpsecret remote LNS */
71 char tunnel_assignment_id
[256];
74 // Max Radius Tunnels by remote LNS
75 #define MAXTAGTUNNEL 0x20
76 static tunnelrlnst ptunnelrlns
[MAXTAGTUNNEL
];
79 * Possible configrlns states
80 * CONFRLNSFREE -> CONFRLNSSET -> CONFRLNSFREE
84 CONFRLNSFREE
= 0, // Not in use
85 CONFRLNSSET
, // Config Set
86 CONFRLNSSETBYRADIUS
// Config Set
92 int state
; // conf state (tunnelstate enum)
93 in_addr_t ip
; // Ip for far end
94 uint16_t port
; // port for far end
95 hasht auth
; // request authenticator
96 char strmaskuser
[MAXUSER
];
97 char l2tp_secret
[64]; // L2TP shared secret
98 char tunnel_assignment_id
[256];
102 configrlns
*pconfigrlns
= NULL
;
104 // Init data structures
105 void lac_initremotelnsdata()
109 if ( !(pconfigrlns
= shared_malloc(sizeof(pconfigrlns
[0]) * MAXRLNSTUNNEL
)) )
111 LOG(0, 0, 0, "Error doing malloc for tunnels lac: %s\n", strerror(errno
));
115 memset(pconfigrlns
, 0, sizeof(pconfigrlns
[0]) * MAXRLNSTUNNEL
);
117 // Mark all the conf as free.
118 for (i
= 1; i
< MAXRLNSTUNNEL
; i
++)
119 pconfigrlns
[i
].state
= CONFRLNSFREE
; // mark it as not filled in.
121 config
->highest_rlnsid
= 0;
123 lac_reset_rad_tag_tunnel_ctxt();
126 // Reset Radius TAG tunnel context
127 void lac_reset_rad_tag_tunnel_ctxt()
129 memset(ptunnelrlns
, 0, sizeof(ptunnelrlns
[0]) * MAXTAGTUNNEL
);
132 // Add tunnel_type radius TAG tunnel to context
133 void lac_set_rad_tag_tunnel_type(uint8_t tag
, uint32_t tunnel_type
)
135 if (tag
< MAXTAGTUNNEL
)
136 ptunnelrlns
[tag
].tunnel_type
= tunnel_type
;
139 // Add tunnel_medium_type Radius TAG tunnel to context
140 void lac_set_rad_tag_tunnel_medium_type(uint8_t tag
, uint32_t tunnel_medium_type
)
142 if (tag
< MAXTAGTUNNEL
)
143 ptunnelrlns
[tag
].tunnel_medium_type
= tunnel_medium_type
;
146 // Add tunnel_server_endpoint Radius TAG tunnel to context
147 void lac_set_rad_tag_tunnel_serv_endpt(uint8_t tag
, char *tunnel_server_endpoint
)
149 if (tag
< MAXTAGTUNNEL
)
151 ptunnelrlns
[tag
].tunnel_server_endpoint
= ntohl(inet_addr(tunnel_server_endpoint
));
155 // Add tunnel_password Radius TAG tunnel to context
156 void lac_set_rad_tag_tunnel_password(uint8_t tag
, char *tunnel_password
)
158 if ((tag
< MAXTAGTUNNEL
) && (strlen(tunnel_password
) < 64))
160 strcpy(ptunnelrlns
[tag
].tunnel_password
, tunnel_password
);
164 // Add tunnel_assignment_id Radius TAG tunnel to context
165 void lac_set_rad_tag_tunnel_assignment_id(uint8_t tag
, char *tunnel_assignment_id
)
167 if ((tag
< MAXTAGTUNNEL
) && (strlen(tunnel_assignment_id
) < 256))
169 strcpy(ptunnelrlns
[tag
].tunnel_assignment_id
, tunnel_assignment_id
);
173 // Select a tunnel_assignment_id
174 int lac_rad_select_assignment_id(sessionidt s
, char *assignment_id
)
178 int bufidtag
[MAXTAGTUNNEL
];
180 for (idtag
= 0; idtag
< MAXTAGTUNNEL
; ++idtag
)
182 if (ptunnelrlns
[idtag
].tunnel_type
== 0)
184 else if (ptunnelrlns
[idtag
].tunnel_type
!= 3) // 3 == L2TP tunnel type
185 LOG(1, s
, session
[s
].tunnel
, "Error, Only L2TP tunnel type supported\n");
186 else if (ptunnelrlns
[idtag
].tunnel_medium_type
!= 1)
187 LOG(1, s
, session
[s
].tunnel
, "Error, Only IP tunnel medium type supported\n");
188 else if (ptunnelrlns
[idtag
].tunnel_server_endpoint
== 0)
189 LOG(1, s
, session
[s
].tunnel
, "Error, Bad IP tunnel server endpoint \n");
190 else if (strlen(ptunnelrlns
[idtag
].tunnel_assignment_id
) > 0)
192 bufidtag
[nbtagfound
] = idtag
;
199 // random between 0 and nbtagfound-1
200 idtag
= (rand() % nbtagfound
);
202 if (idtag
>= nbtagfound
)
203 idtag
= 0; //Sanity checks.
205 strcpy(assignment_id
, ptunnelrlns
[bufidtag
[idtag
]].tunnel_assignment_id
);
209 // Error no tunnel_assignment_id found
213 // Save the 'radius tag tunnels' context on global configuration
214 void lac_save_rad_tag_tunnels(sessionidt s
)
219 for (idtag
= 0; idtag
< MAXTAGTUNNEL
; ++idtag
)
221 if (ptunnelrlns
[idtag
].tunnel_type
== 0)
223 else if (ptunnelrlns
[idtag
].tunnel_type
!= 3) // 3 == L2TP tunnel type
224 LOG(1, s
, session
[s
].tunnel
, "Error, Only L2TP tunnel type supported\n");
225 else if (ptunnelrlns
[idtag
].tunnel_medium_type
!= 1)
226 LOG(1, s
, session
[s
].tunnel
, "Error, Only IP tunnel medium type supported\n");
227 else if (ptunnelrlns
[idtag
].tunnel_server_endpoint
== 0)
228 LOG(1, s
, session
[s
].tunnel
, "Error, Bad IP tunnel server endpoint \n");
229 else if (strlen(ptunnelrlns
[idtag
].tunnel_assignment_id
) <= 0)
230 LOG(1, s
, session
[s
].tunnel
, "Error, No tunnel_assignment_id \n");
231 else if (ptunnelrlns
[idtag
].tunnel_server_endpoint
== ntohl(config
->bind_address
))
232 LOG(0, s
, session
[s
].tunnel
, "Error, IP Remote LNS == IP local bind address (%s) !!!\n", fmtaddr(config
->bind_address
, 0));
235 for (idrlns
= 1; idrlns
< MAXRLNSTUNNEL
; ++idrlns
)
237 if (pconfigrlns
[idrlns
].state
== CONFRLNSFREE
)
239 pconfigrlns
[idrlns
].ip
= ptunnelrlns
[idtag
].tunnel_server_endpoint
;
240 pconfigrlns
[idrlns
].port
= L2TPPORT
; //Default L2TP port
241 strcpy(pconfigrlns
[idrlns
].l2tp_secret
, ptunnelrlns
[idtag
].tunnel_password
);
242 strcpy(pconfigrlns
[idrlns
].tunnel_assignment_id
, ptunnelrlns
[idtag
].tunnel_assignment_id
);
244 config
->highest_rlnsid
= idrlns
;
246 pconfigrlns
[idrlns
].state
= CONFRLNSSETBYRADIUS
;
250 else if (pconfigrlns
[idrlns
].state
== CONFRLNSSETBYRADIUS
)
252 if ( (pconfigrlns
[idrlns
].ip
== ptunnelrlns
[idtag
].tunnel_server_endpoint
) &&
253 (strcmp(pconfigrlns
[idrlns
].tunnel_assignment_id
, ptunnelrlns
[idtag
].tunnel_assignment_id
) == 0) )
255 // l2tp_secret may be changed
256 strcpy(pconfigrlns
[idrlns
].l2tp_secret
, ptunnelrlns
[idtag
].tunnel_password
);
257 pconfigrlns
[idrlns
].port
= L2TPPORT
; //Default L2TP poart
259 if (config
->highest_rlnsid
< idrlns
) config
->highest_rlnsid
= idrlns
;
266 if (idrlns
>= MAXRLNSTUNNEL
)
268 LOG(0, s
, session
[s
].tunnel
, "No more Remote LNS Conf Free\n");
275 // Create Remote LNS a Tunnel or Session
276 static int lac_create_tunnelsession(tunnelidt t
, sessionidt s
, confrlnsidt i_conf
, char * puser
)
280 if (main_quit
== QUIT_SHUTDOWN
) return 0;
283 if (!(t
= lac_new_tunnel()))
285 LOG(1, 0, 0, "No more tunnels\n");
286 STAT(tunnel_overflow
);
290 tunnel
[t
].ip
= pconfigrlns
[i_conf
].ip
;
291 tunnel
[t
].port
= pconfigrlns
[i_conf
].port
;
292 tunnel
[t
].window
= 4; // default window
293 tunnel
[t
].isremotelns
= i_conf
;
294 STAT(tunnel_created
);
296 random_data(pconfigrlns
[i_conf
].auth
, sizeof(pconfigrlns
[i_conf
].auth
));
298 LOG(2, 0, t
, "Create New tunnel to REMOTE LNS %s for user %s\n", fmtaddr(htonl(tunnel
[t
].ip
), 0), puser
);
299 lac_send_SCCRQ(t
, pconfigrlns
[i_conf
].auth
, sizeof(pconfigrlns
[i_conf
].auth
));
301 else if (tunnel
[t
].state
== TUNNELOPEN
)
303 if (main_quit
!= QUIT_SHUTDOWN
)
306 /**********************/
307 /** Open New session **/
308 /**********************/
309 sessionidt new_sess
= sessionfree
;
311 sessionfree
= session
[new_sess
].next
;
312 memset(&session
[new_sess
], 0, sizeof(session
[new_sess
]));
314 if (new_sess
> config
->cluster_highest_sessionid
)
315 config
->cluster_highest_sessionid
= new_sess
;
317 session
[new_sess
].opened
= time_now
;
318 session
[new_sess
].tunnel
= t
;
319 session
[new_sess
].last_packet
= session
[s
].last_data
= time_now
;
321 session
[new_sess
].ppp
.phase
= Establish
;
322 session
[new_sess
].ppp
.lcp
= Starting
;
323 session
[s
].ppp
.phase
= Establish
;
325 LOG(2, 0, t
, "Open New session to REMOTE LNS %s for user: %s\n", fmtaddr(htonl(tunnel
[t
].ip
), 0), puser
);
326 // Sent ICRQ Incoming-call-request
327 lac_send_ICRQ(t
, new_sess
);
329 // Set session to forward to another LNS
330 session
[s
].forwardtosession
= new_sess
;
331 session
[new_sess
].forwardtosession
= s
;
332 strncpy(session
[s
].user
, puser
, sizeof(session
[s
].user
) - 1);
333 strncpy(session
[new_sess
].user
, puser
, sizeof(session
[new_sess
].user
) - 1);
335 STAT(session_created
);
339 lac_tunnelshutdown(t
, "Shutting down", 6, 0, 0);
345 LOG(1, 0, t
, "(REMOTE LNS) tunnel is not open\n");
350 // Check if session must be forwarded to another LNS
351 // return 1 if the session must be forwarded (and Creating a tunnel/session has been started)
353 // Note: check from the configuration read on the startup-config (see setforward)
354 int lac_conf_forwardtoremotelns(sessionidt s
, char * puser
)
359 for (i
= 1; i
<= config
->highest_rlnsid
; ++i
)
361 if ( (pconfigrlns
[i
].state
== CONFRLNSSET
) && (NULL
!= strstr(puser
, pconfigrlns
[i
].strmaskuser
)) )
364 for (j
= 0; j
<= config
->cluster_highest_tunnelid
; ++j
)
366 if ((tunnel
[j
].isremotelns
) &&
367 (tunnel
[j
].ip
== pconfigrlns
[i
].ip
) &&
368 (tunnel
[j
].port
== pconfigrlns
[i
].port
) &&
369 (tunnel
[j
].state
!= TUNNELDIE
))
372 if (tunnel
[t
].isremotelns
!= i
)
374 if ( (tunnel
[t
].state
== TUNNELOPEN
) || (tunnel
[t
].state
== TUNNELOPENING
) )
376 LOG(1, 0, t
, "Tunnel Remote LNS ID inconsistency (IP RLNS:%s)\n",
377 fmtaddr(htonl(pconfigrlns
[i
].ip
), 0));
379 tunnel
[t
].isremotelns
= i
;
387 return lac_create_tunnelsession(t
, s
, i
, puser
);
394 // return 1 if the session must be forwarded (and Creating a tunnel/session has been started)
396 // Note: Started from a radius response
397 int lac_rad_forwardtoremotelns(sessionidt s
, char *assignment_id
, char * puser
)
402 for (i
= 1; i
<= config
->highest_rlnsid
; ++i
)
404 if ((pconfigrlns
[i
].state
== CONFRLNSSETBYRADIUS
) &&
405 (strcmp(pconfigrlns
[i
].tunnel_assignment_id
, assignment_id
) == 0))
408 for (j
= 1; j
<= config
->cluster_highest_tunnelid
; ++j
)
410 if ((tunnel
[j
].isremotelns
== i
) &&
411 (tunnel
[j
].ip
== pconfigrlns
[i
].ip
) &&
412 (tunnel
[j
].port
== pconfigrlns
[i
].port
) &&
413 (tunnel
[j
].state
!= TUNNELDIE
))
415 if ( (tunnel
[j
].state
== TUNNELOPEN
) ||
416 (tunnel
[j
].state
== TUNNELOPENING
) )
419 LOG(3, 0, t
, "Tunnel Remote LNS already open(ing) (RLNS IP:%s)\n", fmtaddr(htonl(pconfigrlns
[i
].ip
), 0));
425 return lac_create_tunnelsession(t
, s
, i
, puser
);
432 // Calcul the remote LNS auth
433 void lac_calc_rlns_auth(tunnelidt t
, uint8_t id
, uint8_t *out
)
438 idrlns
= tunnel
[t
].isremotelns
;
441 MD5_Update(&ctx
, &id
, 1);
442 MD5_Update(&ctx
, pconfigrlns
[idrlns
].l2tp_secret
, strlen(pconfigrlns
[idrlns
].l2tp_secret
));
443 MD5_Update(&ctx
, pconfigrlns
[idrlns
].auth
, 16);
444 MD5_Final(out
, &ctx
);
447 // Forward session to LAC or Remote LNS
448 int lac_session_forward(uint8_t *buf
, int len
, sessionidt sess
, uint16_t proto
, in_addr_t s_addr
, int sin_port
)
450 uint16_t t
= 0, s
= 0;
451 uint8_t *p
= buf
+ 2; // First word L2TP options
453 s
= session
[sess
].forwardtosession
;
454 if (session
[s
].forwardtosession
!= sess
)
456 LOG(0, sess
, session
[sess
].tunnel
, "Link Session (%u) broken\n", s
);
460 t
= session
[s
].tunnel
;
463 LOG(1, s
, t
, "Session with invalid tunnel ID\n");
467 if ((!tunnel
[t
].isremotelns
) && (!tunnel
[session
[sess
].tunnel
].isremotelns
))
469 LOG(0, sess
, session
[sess
].tunnel
, "Link Tunnel Session (%u) broken\n", s
);
473 if (!config
->cluster_iam_master
)
475 if ( (proto
== PPPIPCP
) || (proto
== PPPLCP
) ||
476 (proto
== PPPPAP
) || (proto
== PPPCHAP
) ||
477 (proto
== PPPIPV6CP
&& config
->ipv6_prefix
.s6_addr
[0]) ||
480 session
[sess
].last_packet
= time_now
;
481 master_forward_packet(buf
, len
, s_addr
, sin_port
);
491 *(uint16_t *) p
= htons(tunnel
[t
].far
); // tunnel
493 *(uint16_t *) p
= htons(session
[s
].far
); // session
498 *(uint16_t *) p
= htons(tunnel
[t
].ns
); // sequence
500 *(uint16_t *) p
= htons(tunnel
[t
].nr
); // sequence
504 if ((proto
== PPPIP
) || (proto
== PPPMP
) ||(proto
== PPPIPV6
&& config
->ipv6_prefix
.s6_addr
[0]))
506 session
[sess
].last_packet
= session
[sess
].last_data
= time_now
;
508 increment_counter(&session
[sess
].cin
, &session
[sess
].cin_wrap
, len
);
509 session
[sess
].cin_delta
+= len
;
511 sess_local
[sess
].cin
+= len
;
512 sess_local
[sess
].pin
++;
514 session
[s
].last_data
= time_now
;
516 increment_counter(&session
[s
].cout
, &session
[s
].cout_wrap
, len
); // byte count
517 session
[s
].cout_delta
+= len
;
519 sess_local
[s
].cout
+= len
;
520 sess_local
[s
].pout
++;
523 session
[sess
].last_packet
= time_now
;
525 tunnelsend(buf
, len
, t
); // send it...
530 // Add new Remote LNS from CLI
533 // 1 = New Remote LNS conf ADD
534 // 2 = Remote LNS Conf Updated
535 int lac_addremotelns(char *mask
, char *IP_RemoteLNS
, char *Port_RemoteLNS
, char *SecretRemoteLNS
)
539 for (idrlns
= 1; idrlns
< MAXRLNSTUNNEL
; ++idrlns
)
541 if (pconfigrlns
[idrlns
].state
== CONFRLNSFREE
)
543 snprintf((char *) pconfigrlns
[idrlns
].strmaskuser
, sizeof(pconfigrlns
[idrlns
].strmaskuser
), "%s", mask
);
544 pconfigrlns
[idrlns
].ip
= ntohl(inet_addr(IP_RemoteLNS
));
545 pconfigrlns
[idrlns
].port
= atoi(Port_RemoteLNS
);
546 snprintf((char *) pconfigrlns
[idrlns
].l2tp_secret
, sizeof(pconfigrlns
[idrlns
].l2tp_secret
), "%s", SecretRemoteLNS
);
548 config
->highest_rlnsid
= idrlns
;
550 pconfigrlns
[idrlns
].state
= CONFRLNSSET
;
554 else if ((pconfigrlns
[idrlns
].state
== CONFRLNSSET
) && (strcmp(pconfigrlns
[idrlns
].strmaskuser
, mask
) == 0))
556 if ( (pconfigrlns
[idrlns
].ip
!= ntohl(inet_addr(IP_RemoteLNS
))) ||
557 (pconfigrlns
[idrlns
].port
!= atoi(Port_RemoteLNS
)) ||
558 (strcmp(pconfigrlns
[idrlns
].l2tp_secret
, SecretRemoteLNS
) != 0) )
560 memset(&pconfigrlns
[idrlns
], 0, sizeof(pconfigrlns
[idrlns
]));
561 snprintf((char *) pconfigrlns
[idrlns
].strmaskuser
, sizeof(pconfigrlns
[idrlns
].strmaskuser
), "%s", mask
);
562 pconfigrlns
[idrlns
].ip
= ntohl(inet_addr(IP_RemoteLNS
));
563 pconfigrlns
[idrlns
].port
= atoi(Port_RemoteLNS
);
564 snprintf((char *) pconfigrlns
[idrlns
].l2tp_secret
, sizeof(pconfigrlns
[idrlns
].l2tp_secret
), "%s", SecretRemoteLNS
);
566 if (config
->highest_rlnsid
< idrlns
) config
->highest_rlnsid
= idrlns
;
568 pconfigrlns
[idrlns
].state
= CONFRLNSSET
;
569 // Conf Updated, the tunnel must be dropped
577 LOG(0, 0, 0, "No more Remote LNS Conf Free\n");
582 // Cli Show remote LNS defined
583 int lac_cli_show_remotelns(confrlnsidt idrlns
, char *strout
)
585 if (idrlns
> config
->highest_rlnsid
)
590 sprintf(strout
, "%15s %3s %-32s %-32s %11s %7s %10s",
600 tunnelidt t
, tfound
= 0;
605 strcpy(state
, "Close");
606 for (t
= 0; t
<= config
->cluster_highest_tunnelid
; ++t
)
608 if ((tunnel
[t
].isremotelns
== idrlns
) &&
609 (tunnel
[t
].ip
== pconfigrlns
[idrlns
].ip
) &&
610 (tunnel
[t
].port
== pconfigrlns
[idrlns
].port
) &&
611 (tunnel
[t
].state
!= TUNNELDIE
))
613 if (tunnel
[t
].state
== TUNNELOPENING
)
614 strcpy(state
, "Opening");
615 else if (tunnel
[t
].state
== TUNNELOPEN
)
616 strcpy(state
, "Open");
618 for (s
= 1; s
<= config
->cluster_highest_sessionid
; ++s
)
619 if (session
[s
].tunnel
== t
)
626 sprintf(strout
, "%15s %3u %-32s %-32s %11s %7s %10u",
627 fmtaddr(htonl(pconfigrlns
[idrlns
].ip
), 0),
629 pconfigrlns
[idrlns
].l2tp_secret
,
630 pconfigrlns
[idrlns
].tunnel_assignment_id
,
631 (pconfigrlns
[idrlns
].state
== CONFRLNSSET
?"File":(pconfigrlns
[idrlns
].state
== CONFRLNSSETBYRADIUS
?"Radius":"Free")),