2 * Add functionality "LAC" to l2tpns.
3 * Used to forward a ppp session to another "LNS".
14 /* sequence diagram: Client <--> LAC <--> LNS1 <--> LNS2
17 * Client <-------------------> LAC
18 * Challenge (CHAP/PAP)
19 * Client <-------------------> LAC
21 * LAC --------------------> LNS1 (Tunnel Open)
23 * LAC <-------------------- LNS1 (Tunnel Open)
25 * LAC --------------------> LNS1 (Tunnel Open)
27 * LAC <-------------------- LNS1 (Tunnel Open)
29 * LAC --------------------> LNS1 (Session Open)
31 * LAC <-------------------- LNS1 (Session Open)
33 * LAC --------------------> LNS1 (Session Open)
35 * LAC <-------------------- LNS1 (Session Open)
37 * Client <---------------------------------------------> LNS1
38 * Challenge (CHAP/PAP)
39 * Client <---------------------------------------------> LNS1
41 * LNS1 --------------------> LNS2 (Tunnel Open)
43 * LNS1 <-------------------- LNS2 (Tunnel Open)
45 * LNS1 --------------------> LNS2 (Tunnel Open)
47 * LNS1 <-------------------- LNS2 (Tunnel Open)
49 * LNS1 --------------------> LNS2 (Session Open)
51 * LNS1 <-------------------- LNS2 (Session Open)
53 * LNS1 --------------------> LNS2 (Session Open)
55 * LNS1 <-------------------- LNS2 (Session Open)
57 * Client <------------------------------------------------------------------------> LNS2
58 * PAP/CHAP Authentification
59 * Client <------------------------------------------------------------------------> LNS2
61 * Client <------------------------------------------------------------------------> LNS2
67 uint32_t tunnel_medium_type
;
68 in_addr_t tunnel_server_endpoint
; /* IP remote LNS */
69 char tunnel_password
[64]; /* l2tpsecret remote LNS */
70 char tunnel_assignment_id
[256];
73 // Max Radius Tunnels by remote LNS
74 #define MAXTAGTUNNEL 0x20
75 static tunnelrlnst ptunnelrlns
[MAXTAGTUNNEL
];
78 * Possible configrlns states
79 * CONFRLNSFREE -> CONFRLNSSET -> CONFRLNSFREE
83 CONFRLNSFREE
= 0, // Not in use
84 CONFRLNSSET
, // Config Set
85 CONFRLNSSETBYRADIUS
// Config Set
91 int state
; // conf state (tunnelstate enum)
92 in_addr_t ip
; // Ip for far end
93 uint16_t port
; // port for far end
94 hasht auth
; // request authenticator
95 char strmaskuser
[MAXUSER
];
96 char l2tp_secret
[64]; // L2TP shared secret
97 char tunnel_assignment_id
[256];
101 configrlns
*pconfigrlns
= NULL
;
103 // Init data structures
104 void lac_initremotelnsdata()
108 if ( !(pconfigrlns
= shared_malloc(sizeof(pconfigrlns
[0]) * MAXRLNSTUNNEL
)) )
110 LOG(0, 0, 0, "Error doing malloc for tunnels lac: %s\n", strerror(errno
));
114 memset(pconfigrlns
, 0, sizeof(pconfigrlns
[0]) * MAXRLNSTUNNEL
);
116 // Mark all the conf as free.
117 for (i
= 1; i
< MAXRLNSTUNNEL
; i
++)
118 pconfigrlns
[i
].state
= CONFRLNSFREE
; // mark it as not filled in.
120 config
->highest_rlnsid
= 0;
122 lac_reset_rad_tag_tunnel_ctxt();
125 // Reset Radius TAG tunnel context
126 void lac_reset_rad_tag_tunnel_ctxt()
128 memset(ptunnelrlns
, 0, sizeof(ptunnelrlns
[0]) * MAXTAGTUNNEL
);
131 // Add tunnel_type radius TAG tunnel to context
132 void lac_set_rad_tag_tunnel_type(uint8_t tag
, uint32_t tunnel_type
)
134 if (tag
< MAXTAGTUNNEL
)
135 ptunnelrlns
[tag
].tunnel_type
= tunnel_type
;
138 // Add tunnel_medium_type Radius TAG tunnel to context
139 void lac_set_rad_tag_tunnel_medium_type(uint8_t tag
, uint32_t tunnel_medium_type
)
141 if (tag
< MAXTAGTUNNEL
)
142 ptunnelrlns
[tag
].tunnel_medium_type
= tunnel_medium_type
;
145 // Add tunnel_server_endpoint Radius TAG tunnel to context
146 void lac_set_rad_tag_tunnel_serv_endpt(uint8_t tag
, char *tunnel_server_endpoint
)
148 if (tag
< MAXTAGTUNNEL
)
150 ptunnelrlns
[tag
].tunnel_server_endpoint
= ntohl(inet_addr(tunnel_server_endpoint
));
154 // Add tunnel_password Radius TAG tunnel to context
155 void lac_set_rad_tag_tunnel_password(uint8_t tag
, char *tunnel_password
)
157 if ((tag
< MAXTAGTUNNEL
) && (strlen(tunnel_password
) < 64))
159 strcpy(ptunnelrlns
[tag
].tunnel_password
, tunnel_password
);
163 // Add tunnel_assignment_id Radius TAG tunnel to context
164 void lac_set_rad_tag_tunnel_assignment_id(uint8_t tag
, char *tunnel_assignment_id
)
166 if ((tag
< MAXTAGTUNNEL
) && (strlen(tunnel_assignment_id
) < 256))
168 strcpy(ptunnelrlns
[tag
].tunnel_assignment_id
, tunnel_assignment_id
);
172 // Select a tunnel_assignment_id
173 int lac_rad_select_assignment_id(sessionidt s
, char *assignment_id
)
177 int bufidtag
[MAXTAGTUNNEL
];
179 for (idtag
= 0; idtag
< MAXTAGTUNNEL
; ++idtag
)
181 if (ptunnelrlns
[idtag
].tunnel_type
== 0)
183 else if (ptunnelrlns
[idtag
].tunnel_type
!= 3) // 3 == L2TP tunnel type
184 LOG(1, s
, session
[s
].tunnel
, "Error, Only L2TP tunnel type supported\n");
185 else if (ptunnelrlns
[idtag
].tunnel_medium_type
!= 1)
186 LOG(1, s
, session
[s
].tunnel
, "Error, Only IP tunnel medium type supported\n");
187 else if (ptunnelrlns
[idtag
].tunnel_server_endpoint
== 0)
188 LOG(1, s
, session
[s
].tunnel
, "Error, Bad IP tunnel server endpoint \n");
189 else if (strlen(ptunnelrlns
[idtag
].tunnel_assignment_id
) > 0)
191 bufidtag
[nbtagfound
] = idtag
;
198 // random between 0 and nbtagfound-1
199 idtag
= (rand() % nbtagfound
);
201 if (idtag
>= nbtagfound
)
202 idtag
= 0; //Sanity checks.
204 strcpy(assignment_id
, ptunnelrlns
[bufidtag
[idtag
]].tunnel_assignment_id
);
208 // Error no tunnel_assignment_id found
212 // Save the 'radius tag tunnels' context on global configuration
213 void lac_save_rad_tag_tunnels(sessionidt s
)
218 for (idtag
= 0; idtag
< MAXTAGTUNNEL
; ++idtag
)
220 if (ptunnelrlns
[idtag
].tunnel_type
== 0)
222 else if (ptunnelrlns
[idtag
].tunnel_type
!= 3) // 3 == L2TP tunnel type
223 LOG(1, s
, session
[s
].tunnel
, "Error, Only L2TP tunnel type supported\n");
224 else if (ptunnelrlns
[idtag
].tunnel_medium_type
!= 1)
225 LOG(1, s
, session
[s
].tunnel
, "Error, Only IP tunnel medium type supported\n");
226 else if (ptunnelrlns
[idtag
].tunnel_server_endpoint
== 0)
227 LOG(1, s
, session
[s
].tunnel
, "Error, Bad IP tunnel server endpoint \n");
228 else if (strlen(ptunnelrlns
[idtag
].tunnel_assignment_id
) <= 0)
229 LOG(1, s
, session
[s
].tunnel
, "Error, No tunnel_assignment_id \n");
230 else if (ptunnelrlns
[idtag
].tunnel_server_endpoint
== ntohl(config
->bind_address
))
231 LOG(0, s
, session
[s
].tunnel
, "Error, IP Remote LNS == IP local bind address (%s) !!!\n", fmtaddr(config
->bind_address
, 0));
234 for (idrlns
= 1; idrlns
< MAXRLNSTUNNEL
; ++idrlns
)
236 if (pconfigrlns
[idrlns
].state
== CONFRLNSFREE
)
238 pconfigrlns
[idrlns
].ip
= ptunnelrlns
[idtag
].tunnel_server_endpoint
;
239 pconfigrlns
[idrlns
].port
= L2TPPORT
; //Default L2TP poart
240 strcpy(pconfigrlns
[idrlns
].l2tp_secret
, ptunnelrlns
[idtag
].tunnel_password
);
241 strcpy(pconfigrlns
[idrlns
].tunnel_assignment_id
, ptunnelrlns
[idtag
].tunnel_assignment_id
);
243 config
->highest_rlnsid
= idrlns
;
245 pconfigrlns
[idrlns
].state
= CONFRLNSSETBYRADIUS
;
249 else if (pconfigrlns
[idrlns
].state
== CONFRLNSSETBYRADIUS
)
251 if ( (pconfigrlns
[idrlns
].ip
== ptunnelrlns
[idtag
].tunnel_server_endpoint
) &&
252 (strcmp(pconfigrlns
[idrlns
].tunnel_assignment_id
, ptunnelrlns
[idtag
].tunnel_assignment_id
) == 0) )
254 // l2tp_secret may be changed
255 strcpy(pconfigrlns
[idrlns
].l2tp_secret
, ptunnelrlns
[idtag
].tunnel_password
);
256 pconfigrlns
[idrlns
].port
= L2TPPORT
; //Default L2TP poart
258 if (config
->highest_rlnsid
< idrlns
) config
->highest_rlnsid
= idrlns
;
265 if (idrlns
>= MAXRLNSTUNNEL
)
267 LOG(0, s
, session
[s
].tunnel
, "No more Remote LNS Conf Free\n");
274 // Create Remote LNS a Tunnel or Session
275 static int lac_create_tunnelsession(tunnelidt t
, sessionidt s
, confrlnsidt i_conf
, char * puser
)
279 if (main_quit
== QUIT_SHUTDOWN
) return 0;
282 if (!(t
= lac_new_tunnel()))
284 LOG(1, 0, 0, "No more tunnels\n");
285 STAT(tunnel_overflow
);
289 tunnel
[t
].ip
= pconfigrlns
[i_conf
].ip
;
290 tunnel
[t
].port
= pconfigrlns
[i_conf
].port
;
291 tunnel
[t
].window
= 4; // default window
292 tunnel
[t
].isremotelns
= i_conf
;
293 STAT(tunnel_created
);
295 random_data(pconfigrlns
[i_conf
].auth
, sizeof(pconfigrlns
[i_conf
].auth
));
297 LOG(2, 0, t
, "Create New tunnel to REMOTE LNS %s for user %s\n", fmtaddr(htonl(tunnel
[t
].ip
), 0), puser
);
298 lac_send_SCCRQ(t
, pconfigrlns
[i_conf
].auth
, sizeof(pconfigrlns
[i_conf
].auth
));
300 else if (tunnel
[t
].state
== TUNNELOPEN
)
302 if (main_quit
!= QUIT_SHUTDOWN
)
305 /**********************/
306 /** Open New session **/
307 /**********************/
308 sessionidt new_sess
= sessionfree
;
310 sessionfree
= session
[new_sess
].next
;
311 memset(&session
[new_sess
], 0, sizeof(session
[new_sess
]));
313 if (new_sess
> config
->cluster_highest_sessionid
)
314 config
->cluster_highest_sessionid
= new_sess
;
316 session
[new_sess
].opened
= time_now
;
317 session
[new_sess
].tunnel
= t
;
318 session
[new_sess
].last_packet
= session
[s
].last_data
= time_now
;
320 session
[new_sess
].ppp
.phase
= Establish
;
321 session
[new_sess
].ppp
.lcp
= Starting
;
322 session
[s
].ppp
.phase
= Establish
;
324 LOG(2, 0, t
, "Open New session to REMOTE LNS %s for user: %s\n", fmtaddr(htonl(tunnel
[t
].ip
), 0), puser
);
325 // Sent ICRQ Incoming-call-request
326 lac_send_ICRQ(t
, new_sess
);
328 // Set session to forward to another LNS
329 session
[s
].forwardtosession
= new_sess
;
330 session
[new_sess
].forwardtosession
= s
;
331 strncpy(session
[s
].user
, puser
, sizeof(session
[s
].user
) - 1);
332 strncpy(session
[new_sess
].user
, puser
, sizeof(session
[new_sess
].user
) - 1);
334 STAT(session_created
);
338 lac_tunnelshutdown(t
, "Shutting down", 6, 0, 0);
344 LOG(1, 0, t
, "(REMOTE LNS) tunnel is not open\n");
349 // Check if session must be forwarded to another LNS
350 // return 1 if the session must be forwarded (and Creating a tunnel/session has been started)
352 // Note: check from the configuration read on the startup-config (see setforward)
353 int lac_conf_forwardtoremotelns(sessionidt s
, char * puser
)
358 for (i
= 1; i
<= config
->highest_rlnsid
; ++i
)
360 if ( (pconfigrlns
[i
].state
== CONFRLNSSET
) && (NULL
!= strstr(puser
, pconfigrlns
[i
].strmaskuser
)) )
363 for (j
= 0; j
<= config
->cluster_highest_tunnelid
; ++j
)
365 if ((tunnel
[j
].isremotelns
) &&
366 (tunnel
[j
].ip
== pconfigrlns
[i
].ip
) &&
367 (tunnel
[j
].port
== pconfigrlns
[i
].port
) &&
368 (tunnel
[j
].state
!= TUNNELDIE
))
371 if (tunnel
[t
].isremotelns
!= i
)
373 if ( (tunnel
[t
].state
== TUNNELOPEN
) || (tunnel
[t
].state
== TUNNELOPENING
) )
375 LOG(1, 0, t
, "Tunnel Remote LNS ID inconsistency (IP RLNS:%s)\n",
376 fmtaddr(htonl(pconfigrlns
[i
].ip
), 0));
378 tunnel
[t
].isremotelns
= i
;
386 return lac_create_tunnelsession(t
, s
, i
, puser
);
393 // return 1 if the session must be forwarded (and Creating a tunnel/session has been started)
395 // Note: Started from a radius response
396 int lac_rad_forwardtoremotelns(sessionidt s
, char *assignment_id
, char * puser
)
401 for (i
= 1; i
<= config
->highest_rlnsid
; ++i
)
403 if ((pconfigrlns
[i
].state
== CONFRLNSSETBYRADIUS
) &&
404 (strcmp(pconfigrlns
[i
].tunnel_assignment_id
, assignment_id
) == 0))
407 for (j
= 1; j
<= config
->cluster_highest_tunnelid
; ++j
)
409 if ((tunnel
[j
].isremotelns
== i
) &&
410 (tunnel
[j
].ip
== pconfigrlns
[i
].ip
) &&
411 (tunnel
[j
].port
== pconfigrlns
[i
].port
) &&
412 (tunnel
[j
].state
!= TUNNELDIE
))
414 if ( (tunnel
[j
].state
== TUNNELOPEN
) ||
415 (tunnel
[j
].state
== TUNNELOPENING
) )
418 LOG(3, 0, t
, "Tunnel Remote LNS already open(ing) (RLNS IP:%s)\n", fmtaddr(htonl(pconfigrlns
[i
].ip
), 0));
424 return lac_create_tunnelsession(t
, s
, i
, puser
);
431 // Calcul the remote LNS auth
432 void lac_calc_rlns_auth(tunnelidt t
, uint8_t id
, uint8_t *out
)
437 idrlns
= tunnel
[t
].isremotelns
;
440 MD5_Update(&ctx
, &id
, 1);
441 MD5_Update(&ctx
, pconfigrlns
[idrlns
].l2tp_secret
, strlen(pconfigrlns
[idrlns
].l2tp_secret
));
442 MD5_Update(&ctx
, pconfigrlns
[idrlns
].auth
, 16);
443 MD5_Final(out
, &ctx
);
446 // Forward session to LAC or Remote LNS
447 int lac_session_forward(uint8_t *buf
, int len
, sessionidt sess
, uint16_t proto
)
449 uint16_t t
= 0, s
= 0;
450 uint8_t *p
= buf
+ 2; // First word L2TP options
452 s
= session
[sess
].forwardtosession
;
453 if (session
[s
].forwardtosession
!= sess
)
455 LOG(0, sess
, session
[sess
].tunnel
, "Link Session (%u) broken\n", s
);
459 t
= session
[s
].tunnel
;
462 LOG(1, s
, t
, "Session with invalid tunnel ID\n");
466 if ((!tunnel
[t
].isremotelns
) && (!tunnel
[session
[sess
].tunnel
].isremotelns
))
468 LOG(0, sess
, session
[sess
].tunnel
, "Link Tunnel Session (%u) broken\n", s
);
477 *(uint16_t *) p
= htons(tunnel
[t
].far
); // tunnel
479 *(uint16_t *) p
= htons(session
[s
].far
); // session
484 *(uint16_t *) p
= htons(tunnel
[t
].ns
); // sequence
486 *(uint16_t *) p
= htons(tunnel
[t
].nr
); // sequence
490 if ((proto
== PPPIP
) || (proto
== PPPMP
) ||(proto
== PPPIPV6
&& config
->ipv6_prefix
.s6_addr
[0]))
492 session
[sess
].last_packet
= session
[sess
].last_data
= time_now
;
494 increment_counter(&session
[sess
].cin
, &session
[sess
].cin_wrap
, len
);
495 session
[sess
].cin_delta
+= len
;
497 sess_local
[sess
].cin
+= len
;
498 sess_local
[sess
].pin
++;
500 session
[s
].last_data
= time_now
;
502 increment_counter(&session
[s
].cout
, &session
[s
].cout_wrap
, len
); // byte count
503 session
[s
].cout_delta
+= len
;
505 sess_local
[s
].cout
+= len
;
506 sess_local
[s
].pout
++;
509 session
[sess
].last_packet
= time_now
;
511 tunnelsend(buf
, len
, t
); // send it...
516 // Add new Remote LNS from CLI
519 // 1 = New Remote LNS conf ADD
520 // 2 = Remote LNS Conf Updated
521 int lac_addremotelns(char *mask
, char *IP_RemoteLNS
, char *Port_RemoteLNS
, char *SecretRemoteLNS
)
525 for (idrlns
= 1; idrlns
< MAXRLNSTUNNEL
; ++idrlns
)
527 if (pconfigrlns
[idrlns
].state
== CONFRLNSFREE
)
529 snprintf((char *) pconfigrlns
[idrlns
].strmaskuser
, sizeof(pconfigrlns
[idrlns
].strmaskuser
), "%s", mask
);
530 pconfigrlns
[idrlns
].ip
= ntohl(inet_addr(IP_RemoteLNS
));
531 pconfigrlns
[idrlns
].port
= atoi(Port_RemoteLNS
);
532 snprintf((char *) pconfigrlns
[idrlns
].l2tp_secret
, sizeof(pconfigrlns
[idrlns
].l2tp_secret
), "%s", SecretRemoteLNS
);
534 config
->highest_rlnsid
= idrlns
;
536 pconfigrlns
[idrlns
].state
= CONFRLNSSET
;
540 else if ((pconfigrlns
[idrlns
].state
== CONFRLNSSET
) && (strcmp(pconfigrlns
[idrlns
].strmaskuser
, mask
) == 0))
542 if ( (pconfigrlns
[idrlns
].ip
!= ntohl(inet_addr(IP_RemoteLNS
))) ||
543 (pconfigrlns
[idrlns
].port
!= atoi(Port_RemoteLNS
)) ||
544 (strcmp(pconfigrlns
[idrlns
].l2tp_secret
, SecretRemoteLNS
) != 0) )
546 memset(&pconfigrlns
[idrlns
], 0, sizeof(pconfigrlns
[idrlns
]));
547 snprintf((char *) pconfigrlns
[idrlns
].strmaskuser
, sizeof(pconfigrlns
[idrlns
].strmaskuser
), "%s", mask
);
548 pconfigrlns
[idrlns
].ip
= ntohl(inet_addr(IP_RemoteLNS
));
549 pconfigrlns
[idrlns
].port
= atoi(Port_RemoteLNS
);
550 snprintf((char *) pconfigrlns
[idrlns
].l2tp_secret
, sizeof(pconfigrlns
[idrlns
].l2tp_secret
), "%s", SecretRemoteLNS
);
552 if (config
->highest_rlnsid
< idrlns
) config
->highest_rlnsid
= idrlns
;
554 pconfigrlns
[idrlns
].state
= CONFRLNSSET
;
555 // Conf Updated, the tunnel must be dropped
563 LOG(0, 0, 0, "No more Remote LNS Conf Free\n");
568 // Cli Show remote LNS defined
569 int lac_cli_show_remotelns(confrlnsidt idrlns
, char *strout
)
571 if (idrlns
> config
->highest_rlnsid
)
576 sprintf(strout
, "%15s %3s %-32s %-32s %11s %7s %10s",
586 tunnelidt t
, tfound
= 0;
591 strcpy(state
, "Close");
592 for (t
= 0; t
<= config
->cluster_highest_tunnelid
; ++t
)
594 if ((tunnel
[t
].isremotelns
== idrlns
) &&
595 (tunnel
[t
].ip
== pconfigrlns
[idrlns
].ip
) &&
596 (tunnel
[t
].port
== pconfigrlns
[idrlns
].port
) &&
597 (tunnel
[t
].state
!= TUNNELDIE
))
599 if (tunnel
[t
].state
== TUNNELOPENING
)
600 strcpy(state
, "Opening");
601 else if (tunnel
[t
].state
== TUNNELOPEN
)
602 strcpy(state
, "Open");
604 for (s
= 1; s
<= config
->cluster_highest_sessionid
; ++s
)
605 if (session
[s
].tunnel
== t
)
612 sprintf(strout
, "%15s %3u %-32s %-32s %11s %7s %10u",
613 fmtaddr(htonl(pconfigrlns
[idrlns
].ip
), 0),
615 pconfigrlns
[idrlns
].l2tp_secret
,
616 pconfigrlns
[idrlns
].tunnel_assignment_id
,
617 (pconfigrlns
[idrlns
].state
== CONFRLNSSET
?"File":(pconfigrlns
[idrlns
].state
== CONFRLNSSETBYRADIUS
?"Radius":"Free")),