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");
232 for (idrlns
= 1; idrlns
< MAXRLNSTUNNEL
; ++idrlns
)
234 if (pconfigrlns
[idrlns
].state
== CONFRLNSFREE
)
236 pconfigrlns
[idrlns
].ip
= ptunnelrlns
[idtag
].tunnel_server_endpoint
;
237 pconfigrlns
[idrlns
].port
= L2TPPORT
; //Default L2TP poart
238 strcpy(pconfigrlns
[idrlns
].l2tp_secret
, ptunnelrlns
[idtag
].tunnel_password
);
239 strcpy(pconfigrlns
[idrlns
].tunnel_assignment_id
, ptunnelrlns
[idtag
].tunnel_assignment_id
);
241 config
->highest_rlnsid
= idrlns
;
243 pconfigrlns
[idrlns
].state
= CONFRLNSSETBYRADIUS
;
247 else if (pconfigrlns
[idrlns
].state
== CONFRLNSSETBYRADIUS
)
249 if ( (pconfigrlns
[idrlns
].ip
== ptunnelrlns
[idtag
].tunnel_server_endpoint
) &&
250 (strcmp(pconfigrlns
[idrlns
].tunnel_assignment_id
, ptunnelrlns
[idtag
].tunnel_assignment_id
) == 0) )
252 // l2tp_secret may be changed
253 strcpy(pconfigrlns
[idrlns
].l2tp_secret
, ptunnelrlns
[idtag
].tunnel_password
);
254 pconfigrlns
[idrlns
].port
= L2TPPORT
; //Default L2TP poart
256 if (config
->highest_rlnsid
< idrlns
) config
->highest_rlnsid
= idrlns
;
263 if (idrlns
>= MAXRLNSTUNNEL
)
265 LOG(0, s
, session
[s
].tunnel
, "No more Remote LNS Conf Free\n");
272 // Create Remote LNS a Tunnel or Session
273 static int lac_create_tunnelsession(tunnelidt t
, sessionidt s
, confrlnsidt i_conf
, char * puser
)
277 if (main_quit
== QUIT_SHUTDOWN
) return 0;
280 if (!(t
= lac_new_tunnel()))
282 LOG(1, 0, 0, "No more tunnels\n");
283 STAT(tunnel_overflow
);
287 tunnel
[t
].ip
= pconfigrlns
[i_conf
].ip
;
288 tunnel
[t
].port
= pconfigrlns
[i_conf
].port
;
289 tunnel
[t
].window
= 4; // default window
290 tunnel
[t
].isremotelns
= i_conf
;
291 STAT(tunnel_created
);
293 random_data(pconfigrlns
[i_conf
].auth
, sizeof(pconfigrlns
[i_conf
].auth
));
295 LOG(2, 0, t
, "Create New tunnel to REMOTE LNS %s for user %s\n", fmtaddr(htonl(tunnel
[t
].ip
), 0), puser
);
296 lac_send_SCCRQ(t
, pconfigrlns
[i_conf
].auth
, sizeof(pconfigrlns
[i_conf
].auth
));
298 else if (tunnel
[t
].state
== TUNNELOPEN
)
300 if (main_quit
!= QUIT_SHUTDOWN
)
303 /**********************/
304 /** Open New session **/
305 /**********************/
306 sessionidt new_sess
= sessionfree
;
308 sessionfree
= session
[new_sess
].next
;
309 memset(&session
[new_sess
], 0, sizeof(session
[new_sess
]));
311 if (new_sess
> config
->cluster_highest_sessionid
)
312 config
->cluster_highest_sessionid
= new_sess
;
314 session
[new_sess
].opened
= time_now
;
315 session
[new_sess
].tunnel
= t
;
316 session
[new_sess
].last_packet
= session
[s
].last_data
= time_now
;
318 session
[new_sess
].ppp
.phase
= Establish
;
319 session
[new_sess
].ppp
.lcp
= Starting
;
320 session
[s
].ppp
.phase
= Establish
;
322 LOG(2, 0, t
, "Open New session to REMOTE LNS %s for user: %s\n", fmtaddr(htonl(tunnel
[t
].ip
), 0), puser
);
323 // Sent ICRQ Incoming-call-request
324 lac_send_ICRQ(t
, new_sess
);
326 // Set session to forward to another LNS
327 session
[s
].forwardtosession
= new_sess
;
328 session
[new_sess
].forwardtosession
= s
;
329 strncpy(session
[s
].user
, puser
, sizeof(session
[s
].user
) - 1);
330 strncpy(session
[new_sess
].user
, puser
, sizeof(session
[new_sess
].user
) - 1);
332 STAT(session_created
);
336 lac_tunnelshutdown(t
, "Shutting down", 6, 0, 0);
342 LOG(1, 0, t
, "(REMOTE LNS) tunnel is not open\n");
347 // Check if session must be forwarded to another LNS
348 // return 1 if the session must be forwarded (and Creating a tunnel/session has been started)
350 // Note: check from the configuration read on the startup-config (see setforward)
351 int lac_conf_forwardtoremotelns(sessionidt s
, char * puser
)
356 for (i
= 1; i
<= config
->highest_rlnsid
; ++i
)
358 if ( (pconfigrlns
[i
].state
== CONFRLNSSET
) && (NULL
!= strstr(puser
, pconfigrlns
[i
].strmaskuser
)) )
361 for (j
= 0; j
<= config
->cluster_highest_tunnelid
; ++j
)
363 if ((tunnel
[j
].isremotelns
) &&
364 (tunnel
[j
].ip
== pconfigrlns
[i
].ip
) &&
365 (tunnel
[j
].port
== pconfigrlns
[i
].port
) &&
366 (tunnel
[j
].state
!= TUNNELDIE
))
369 if (tunnel
[t
].isremotelns
!= i
)
371 if ( (tunnel
[t
].state
== TUNNELOPEN
) || (tunnel
[t
].state
== TUNNELOPENING
) )
373 LOG(1, 0, t
, "Tunnel Remote LNS ID inconsistency (IP RLNS:%s)\n",
374 fmtaddr(htonl(pconfigrlns
[i
].ip
), 0));
376 tunnel
[t
].isremotelns
= i
;
384 return lac_create_tunnelsession(t
, s
, i
, puser
);
391 // return 1 if the session must be forwarded (and Creating a tunnel/session has been started)
393 // Note: Started from a radius response
394 int lac_rad_forwardtoremotelns(sessionidt s
, char *assignment_id
, char * puser
)
399 for (i
= 1; i
<= config
->highest_rlnsid
; ++i
)
401 if ((pconfigrlns
[i
].state
== CONFRLNSSETBYRADIUS
) &&
402 (strcmp(pconfigrlns
[i
].tunnel_assignment_id
, assignment_id
) == 0))
405 for (j
= 1; j
<= config
->cluster_highest_tunnelid
; ++j
)
407 if ((tunnel
[j
].isremotelns
== i
) &&
408 (tunnel
[j
].ip
== pconfigrlns
[i
].ip
) &&
409 (tunnel
[j
].port
== pconfigrlns
[i
].port
) &&
410 (tunnel
[j
].state
!= TUNNELDIE
))
412 if ( (tunnel
[j
].state
== TUNNELOPEN
) ||
413 (tunnel
[j
].state
== TUNNELOPENING
) )
416 LOG(3, 0, t
, "Tunnel Remote LNS already open(ing) (RLNS IP:%s)\n", fmtaddr(htonl(pconfigrlns
[i
].ip
), 0));
422 return lac_create_tunnelsession(t
, s
, i
, puser
);
429 // Calcul the remote LNS auth
430 void lac_calc_rlns_auth(tunnelidt t
, uint8_t id
, uint8_t *out
)
435 idrlns
= tunnel
[t
].isremotelns
;
438 MD5_Update(&ctx
, &id
, 1);
439 MD5_Update(&ctx
, pconfigrlns
[idrlns
].l2tp_secret
, strlen(pconfigrlns
[idrlns
].l2tp_secret
));
440 MD5_Update(&ctx
, pconfigrlns
[idrlns
].auth
, 16);
441 MD5_Final(out
, &ctx
);
444 // Forward session to LAC or Remote LNS
445 int lac_session_forward(uint8_t *buf
, int len
, sessionidt sess
, uint16_t proto
)
447 uint16_t t
= 0, s
= 0;
448 uint8_t *p
= buf
+ 2; // First word L2TP options
450 s
= session
[sess
].forwardtosession
;
451 if (session
[s
].forwardtosession
!= sess
)
453 LOG(0, sess
, session
[sess
].tunnel
, "Link Session (%u) broken\n", s
);
457 t
= session
[s
].tunnel
;
460 LOG(1, s
, t
, "Session with invalid tunnel ID\n");
464 if ((!tunnel
[t
].isremotelns
) && (!tunnel
[session
[sess
].tunnel
].isremotelns
))
466 LOG(0, sess
, session
[sess
].tunnel
, "Link Tunnel Session (%u) broken\n", s
);
475 *(uint16_t *) p
= htons(tunnel
[t
].far
); // tunnel
477 *(uint16_t *) p
= htons(session
[s
].far
); // session
482 *(uint16_t *) p
= htons(tunnel
[t
].ns
); // sequence
484 *(uint16_t *) p
= htons(tunnel
[t
].nr
); // sequence
488 if ((proto
== PPPIP
) || (proto
== PPPMP
) ||(proto
== PPPIPV6
&& config
->ipv6_prefix
.s6_addr
[0]))
490 session
[sess
].last_packet
= session
[sess
].last_data
= time_now
;
492 increment_counter(&session
[sess
].cin
, &session
[sess
].cin_wrap
, len
);
493 session
[sess
].cin_delta
+= len
;
495 sess_local
[sess
].cin
+= len
;
496 sess_local
[sess
].pin
++;
498 session
[s
].last_data
= time_now
;
500 increment_counter(&session
[s
].cout
, &session
[s
].cout_wrap
, len
); // byte count
501 session
[s
].cout_delta
+= len
;
503 sess_local
[s
].cout
+= len
;
504 sess_local
[s
].pout
++;
507 session
[sess
].last_packet
= time_now
;
509 tunnelsend(buf
, len
, t
); // send it...
514 // Add new Remote LNS from CLI
517 // 1 = New Remote LNS conf ADD
518 // 2 = Remote LNS Conf Updated
519 int lac_addremotelns(char *mask
, char *IP_RemoteLNS
, char *Port_RemoteLNS
, char *SecretRemoteLNS
)
523 for (idrlns
= 1; idrlns
< MAXRLNSTUNNEL
; ++idrlns
)
525 if (pconfigrlns
[idrlns
].state
== CONFRLNSFREE
)
527 snprintf((char *) pconfigrlns
[idrlns
].strmaskuser
, sizeof(pconfigrlns
[idrlns
].strmaskuser
), "%s", mask
);
528 pconfigrlns
[idrlns
].ip
= ntohl(inet_addr(IP_RemoteLNS
));
529 pconfigrlns
[idrlns
].port
= atoi(Port_RemoteLNS
);
530 snprintf((char *) pconfigrlns
[idrlns
].l2tp_secret
, sizeof(pconfigrlns
[idrlns
].l2tp_secret
), "%s", SecretRemoteLNS
);
532 config
->highest_rlnsid
= idrlns
;
534 pconfigrlns
[idrlns
].state
= CONFRLNSSET
;
538 else if ((pconfigrlns
[idrlns
].state
== CONFRLNSSET
) && (strcmp(pconfigrlns
[idrlns
].strmaskuser
, mask
) == 0))
540 if ( (pconfigrlns
[idrlns
].ip
!= ntohl(inet_addr(IP_RemoteLNS
))) ||
541 (pconfigrlns
[idrlns
].port
!= atoi(Port_RemoteLNS
)) ||
542 (strcmp(pconfigrlns
[idrlns
].l2tp_secret
, SecretRemoteLNS
) != 0) )
544 memset(&pconfigrlns
[idrlns
], 0, sizeof(pconfigrlns
[idrlns
]));
545 snprintf((char *) pconfigrlns
[idrlns
].strmaskuser
, sizeof(pconfigrlns
[idrlns
].strmaskuser
), "%s", mask
);
546 pconfigrlns
[idrlns
].ip
= ntohl(inet_addr(IP_RemoteLNS
));
547 pconfigrlns
[idrlns
].port
= atoi(Port_RemoteLNS
);
548 snprintf((char *) pconfigrlns
[idrlns
].l2tp_secret
, sizeof(pconfigrlns
[idrlns
].l2tp_secret
), "%s", SecretRemoteLNS
);
550 if (config
->highest_rlnsid
< idrlns
) config
->highest_rlnsid
= idrlns
;
552 pconfigrlns
[idrlns
].state
= CONFRLNSSET
;
553 // Conf Updated, the tunnel must be dropped
561 LOG(0, 0, 0, "No more Remote LNS Conf Free\n");
566 // Cli Show remote LNS defined
567 int lac_cli_show_remotelns(confrlnsidt idrlns
, char *strout
)
569 if (idrlns
> config
->highest_rlnsid
)
574 sprintf(strout
, "%15s %3s %-32s %-32s %11s %7s %10s",
584 tunnelidt t
, tfound
= 0;
589 strcpy(state
, "Close");
590 for (t
= 0; t
<= config
->cluster_highest_tunnelid
; ++t
)
592 if ((tunnel
[t
].isremotelns
== idrlns
) &&
593 (tunnel
[t
].ip
== pconfigrlns
[idrlns
].ip
) &&
594 (tunnel
[t
].port
== pconfigrlns
[idrlns
].port
) &&
595 (tunnel
[t
].state
!= TUNNELDIE
))
597 if (tunnel
[t
].state
== TUNNELOPENING
)
598 strcpy(state
, "Opening");
599 else if (tunnel
[t
].state
== TUNNELOPEN
)
600 strcpy(state
, "Open");
602 for (s
= 1; s
<= config
->cluster_highest_sessionid
; ++s
)
603 if (session
[s
].tunnel
== t
)
610 sprintf(strout
, "%15s %3u %-32s %-32s %11s %7s %10u",
611 fmtaddr(htonl(pconfigrlns
[idrlns
].ip
), 0),
613 pconfigrlns
[idrlns
].l2tp_secret
,
614 pconfigrlns
[idrlns
].tunnel_assignment_id
,
615 (pconfigrlns
[idrlns
].state
== CONFRLNSSET
?"File":(pconfigrlns
[idrlns
].state
== CONFRLNSSETBYRADIUS
?"Radius":"Free")),