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
= (nbtagfound
*rand()/(RAND_MAX
+1.0));
200 if (idtag
>= nbtagfound
)
201 idtag
= 0; //Sanity checks.
203 strcpy(assignment_id
, ptunnelrlns
[bufidtag
[idtag
]].tunnel_assignment_id
);
207 // Error no tunnel_assignment_id found
211 // Save the 'radius tag tunnels' context on global configuration
212 void lac_save_rad_tag_tunnels(sessionidt s
)
217 for (idtag
= 0; idtag
< MAXTAGTUNNEL
; ++idtag
)
219 if (ptunnelrlns
[idtag
].tunnel_type
== 0)
221 else if (ptunnelrlns
[idtag
].tunnel_type
!= 3) // 3 == L2TP tunnel type
222 LOG(1, s
, session
[s
].tunnel
, "Error, Only L2TP tunnel type supported\n");
223 else if (ptunnelrlns
[idtag
].tunnel_medium_type
!= 1)
224 LOG(1, s
, session
[s
].tunnel
, "Error, Only IP tunnel medium type supported\n");
225 else if (ptunnelrlns
[idtag
].tunnel_server_endpoint
== 0)
226 LOG(1, s
, session
[s
].tunnel
, "Error, Bad IP tunnel server endpoint \n");
227 else if (strlen(ptunnelrlns
[idtag
].tunnel_assignment_id
) <= 0)
228 LOG(1, s
, session
[s
].tunnel
, "Error, No tunnel_assignment_id \n");
230 for (idrlns
= 1; idrlns
< MAXRLNSTUNNEL
; ++idrlns
)
232 if (pconfigrlns
[idrlns
].state
== CONFRLNSFREE
)
234 pconfigrlns
[idrlns
].ip
= ptunnelrlns
[idtag
].tunnel_server_endpoint
;
235 pconfigrlns
[idrlns
].port
= L2TPPORT
; //Default L2TP poart
236 strcpy(pconfigrlns
[idrlns
].l2tp_secret
, ptunnelrlns
[idtag
].tunnel_password
);
237 strcpy(pconfigrlns
[idrlns
].tunnel_assignment_id
, ptunnelrlns
[idtag
].tunnel_assignment_id
);
239 config
->highest_rlnsid
= idrlns
;
241 pconfigrlns
[idrlns
].state
= CONFRLNSSETBYRADIUS
;
245 else if (pconfigrlns
[idrlns
].state
== CONFRLNSSETBYRADIUS
)
247 if ( (pconfigrlns
[idrlns
].ip
== ptunnelrlns
[idtag
].tunnel_server_endpoint
) &&
248 (strcmp(pconfigrlns
[idrlns
].tunnel_assignment_id
, ptunnelrlns
[idtag
].tunnel_assignment_id
) == 0) )
250 LOG(3, s
, session
[s
].tunnel
, "Tunnel IP %s already defined\n", fmtaddr(htonl(pconfigrlns
[idrlns
].ip
), 0));
251 // l2tp_secret may be changed
252 strcpy(pconfigrlns
[idrlns
].l2tp_secret
, ptunnelrlns
[idtag
].tunnel_password
);
253 pconfigrlns
[idrlns
].port
= L2TPPORT
; //Default L2TP poart
255 if (config
->highest_rlnsid
< idrlns
) config
->highest_rlnsid
= idrlns
;
262 if (idrlns
>= MAXRLNSTUNNEL
)
264 LOG(0, s
, session
[s
].tunnel
, "No more Remote LNS Conf Free\n");
270 // Create Remote LNS a Tunnel or Session
271 static int lac_create_tunnelsession(tunnelidt t
, sessionidt s
, confrlnsidt i_conf
, char * puser
)
275 if (main_quit
== QUIT_SHUTDOWN
) return 0;
278 if (!(t
= lac_new_tunnel()))
280 LOG(1, 0, 0, "No more tunnels\n");
281 STAT(tunnel_overflow
);
285 tunnel
[t
].ip
= pconfigrlns
[i_conf
].ip
;
286 tunnel
[t
].port
= pconfigrlns
[i_conf
].port
;
287 tunnel
[t
].window
= 4; // default window
288 tunnel
[t
].isremotelns
= i_conf
;
289 STAT(tunnel_created
);
291 random_data(pconfigrlns
[i_conf
].auth
, sizeof(pconfigrlns
[i_conf
].auth
));
293 LOG(2, 0, t
, "Create New tunnel to REMOTE LNS %s for user %s\n", fmtaddr(htonl(tunnel
[t
].ip
), 0), puser
);
294 lac_send_SCCRQ(t
, pconfigrlns
[i_conf
].auth
, sizeof(pconfigrlns
[i_conf
].auth
));
296 else if (tunnel
[t
].state
== TUNNELOPEN
)
298 if (main_quit
!= QUIT_SHUTDOWN
)
301 /**********************/
302 /** Open New session **/
303 /**********************/
304 sessionidt new_sess
= sessionfree
;
306 sessionfree
= session
[new_sess
].next
;
307 memset(&session
[new_sess
], 0, sizeof(session
[new_sess
]));
309 if (new_sess
> config
->cluster_highest_sessionid
)
310 config
->cluster_highest_sessionid
= new_sess
;
312 session
[new_sess
].opened
= time_now
;
313 session
[new_sess
].tunnel
= t
;
314 session
[new_sess
].last_packet
= session
[s
].last_data
= time_now
;
316 session
[new_sess
].ppp
.phase
= Establish
;
317 session
[new_sess
].ppp
.lcp
= Starting
;
318 session
[s
].ppp
.phase
= Establish
;
320 LOG(2, 0, t
, "Open New session to REMOTE LNS %s for user: %s\n", fmtaddr(htonl(tunnel
[t
].ip
), 0), puser
);
321 // Sent ICRQ Incoming-call-request
322 lac_send_ICRQ(t
, new_sess
);
324 // Set session to forward to another LNS
325 session
[s
].forwardtosession
= new_sess
;
326 session
[new_sess
].forwardtosession
= s
;
327 strncpy(session
[s
].user
, puser
, sizeof(session
[s
].user
) - 1);
328 strncpy(session
[new_sess
].user
, puser
, sizeof(session
[new_sess
].user
) - 1);
330 STAT(session_created
);
334 lac_tunnelshutdown(t
, "Shutting down", 6, 0, 0);
340 LOG(1, 0, t
, "(REMOTE LNS) tunnel is not open\n");
345 // Check if session must be forwarded to another LNS
346 // return 1 if the session must be forwarded (and Creating a tunnel/session has been started)
348 // Note: check from the configuration read on the startup-config (see setforward)
349 int lac_conf_forwardtoremotelns(sessionidt s
, char * puser
)
354 for (i
= 1; i
<= config
->highest_rlnsid
; ++i
)
356 if ( (pconfigrlns
[i
].state
== CONFRLNSSET
) && (NULL
!= strstr(puser
, pconfigrlns
[i
].strmaskuser
)) )
359 for (j
= 0; j
<= config
->cluster_highest_tunnelid
; ++j
)
361 if ((tunnel
[j
].isremotelns
) &&
362 (tunnel
[j
].ip
== pconfigrlns
[i
].ip
) &&
363 (tunnel
[j
].port
== pconfigrlns
[i
].port
) &&
364 (tunnel
[j
].state
!= TUNNELDIE
))
367 if (tunnel
[t
].isremotelns
!= i
)
369 if ( (tunnel
[t
].state
== TUNNELOPEN
) || (tunnel
[t
].state
== TUNNELOPENING
) )
371 LOG(1, 0, t
, "Tunnel Remote LNS ID inconsistency (IP RLNS:%s)\n",
372 fmtaddr(htonl(pconfigrlns
[i
].ip
), 0));
374 tunnel
[t
].isremotelns
= i
;
382 return lac_create_tunnelsession(t
, s
, i
, puser
);
389 // return 1 if the session must be forwarded (and Creating a tunnel/session has been started)
391 // Note: Started from a radius response
392 int lac_rad_forwardtoremotelns(sessionidt s
, char *assignment_id
, char * puser
)
397 for (i
= 1; i
<= config
->highest_rlnsid
; ++i
)
399 if ((pconfigrlns
[i
].state
== CONFRLNSSETBYRADIUS
) &&
400 (strcmp(pconfigrlns
[i
].tunnel_assignment_id
, assignment_id
) == 0))
403 for (j
= 1; j
<= config
->cluster_highest_tunnelid
; ++j
)
405 if ((tunnel
[j
].isremotelns
== i
) &&
406 (tunnel
[j
].ip
== pconfigrlns
[i
].ip
) &&
407 (tunnel
[j
].port
== pconfigrlns
[i
].port
) &&
408 (tunnel
[j
].state
!= TUNNELDIE
))
410 if ( (tunnel
[j
].state
== TUNNELOPEN
) ||
411 (tunnel
[j
].state
== TUNNELOPENING
) )
414 LOG(3, 0, t
, "Tunnel Remote LNS already open(ing) (RLNS IP:%s)\n", fmtaddr(htonl(pconfigrlns
[i
].ip
), 0));
420 return lac_create_tunnelsession(t
, s
, i
, puser
);
427 // Calcul the remote LNS auth
428 void lac_calc_rlns_auth(tunnelidt t
, uint8_t id
, uint8_t *out
)
433 idrlns
= tunnel
[t
].isremotelns
;
436 MD5_Update(&ctx
, &id
, 1);
437 MD5_Update(&ctx
, pconfigrlns
[idrlns
].l2tp_secret
, strlen(pconfigrlns
[idrlns
].l2tp_secret
));
438 MD5_Update(&ctx
, pconfigrlns
[idrlns
].auth
, 16);
439 MD5_Final(out
, &ctx
);
442 // Forward session to LAC or Remote LNS
443 int lac_session_forward(uint8_t *buf
, int len
, sessionidt sess
, uint16_t proto
)
445 uint16_t t
= 0, s
= 0;
446 uint8_t *p
= buf
+ 2; // First word L2TP options
448 s
= session
[sess
].forwardtosession
;
449 if (session
[s
].forwardtosession
!= sess
)
451 LOG(0, sess
, session
[sess
].tunnel
, "Link Session (%u) broken\n", s
);
455 t
= session
[s
].tunnel
;
458 LOG(1, s
, t
, "Session with invalid tunnel ID\n");
462 if ((!tunnel
[t
].isremotelns
) && (!tunnel
[session
[sess
].tunnel
].isremotelns
))
464 LOG(0, sess
, session
[sess
].tunnel
, "Link Tunnel Session (%u) broken\n", s
);
473 *(uint16_t *) p
= htons(tunnel
[t
].far
); // tunnel
475 *(uint16_t *) p
= htons(session
[s
].far
); // session
480 *(uint16_t *) p
= htons(tunnel
[t
].ns
); // sequence
482 *(uint16_t *) p
= htons(tunnel
[t
].nr
); // sequence
486 if ((proto
== PPPIP
) || (proto
== PPPMP
) ||(proto
== PPPIPV6
&& config
->ipv6_prefix
.s6_addr
[0]))
488 session
[sess
].last_packet
= session
[sess
].last_data
= time_now
;
490 increment_counter(&session
[sess
].cin
, &session
[sess
].cin_wrap
, len
);
491 session
[sess
].cin_delta
+= len
;
493 sess_local
[sess
].cin
+= len
;
494 sess_local
[sess
].pin
++;
496 session
[s
].last_data
= time_now
;
498 increment_counter(&session
[s
].cout
, &session
[s
].cout_wrap
, len
); // byte count
499 session
[s
].cout_delta
+= len
;
501 sess_local
[s
].cout
+= len
;
502 sess_local
[s
].pout
++;
505 session
[sess
].last_packet
= time_now
;
507 tunnelsend(buf
, len
, t
); // send it...
512 // Add new Remote LNS from CLI
515 // 1 = New Remote LNS conf ADD
516 // 2 = Remote LNS Conf Updated
517 int lac_addremotelns(char *mask
, char *IP_RemoteLNS
, char *Port_RemoteLNS
, char *SecretRemoteLNS
)
521 for (idrlns
= 1; idrlns
< MAXRLNSTUNNEL
; ++idrlns
)
523 if (pconfigrlns
[idrlns
].state
== CONFRLNSFREE
)
525 snprintf((char *) pconfigrlns
[idrlns
].strmaskuser
, sizeof(pconfigrlns
[idrlns
].strmaskuser
), "%s", mask
);
526 pconfigrlns
[idrlns
].ip
= ntohl(inet_addr(IP_RemoteLNS
));
527 pconfigrlns
[idrlns
].port
= atoi(Port_RemoteLNS
);
528 snprintf((char *) pconfigrlns
[idrlns
].l2tp_secret
, sizeof(pconfigrlns
[idrlns
].l2tp_secret
), "%s", SecretRemoteLNS
);
530 config
->highest_rlnsid
= idrlns
;
532 pconfigrlns
[idrlns
].state
= CONFRLNSSET
;
536 else if ((pconfigrlns
[idrlns
].state
== CONFRLNSSET
) && (strcmp(pconfigrlns
[idrlns
].strmaskuser
, mask
) == 0))
538 if ( (pconfigrlns
[idrlns
].ip
!= ntohl(inet_addr(IP_RemoteLNS
))) ||
539 (pconfigrlns
[idrlns
].port
!= atoi(Port_RemoteLNS
)) ||
540 (strcmp(pconfigrlns
[idrlns
].l2tp_secret
, SecretRemoteLNS
) != 0) )
542 memset(&pconfigrlns
[idrlns
], 0, sizeof(pconfigrlns
[idrlns
]));
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 if (config
->highest_rlnsid
< idrlns
) config
->highest_rlnsid
= idrlns
;
550 pconfigrlns
[idrlns
].state
= CONFRLNSSET
;
551 // Conf Updated, the tunnel must be dropped
559 LOG(0, 0, 0, "No more Remote LNS Conf Free\n");
564 // Cli Show remote LNS defined
565 int lac_cli_show_remotelns(confrlnsidt idrlns
, char *strout
)
567 if (idrlns
> config
->highest_rlnsid
)
572 sprintf(strout
, "%15s %-32s %-32s %11s %7s %10s",
586 strcpy(state
, "Close");
587 for (t
= 0; t
<= config
->cluster_highest_tunnelid
; ++t
)
589 if ((tunnel
[t
].isremotelns
) &&
590 (tunnel
[t
].ip
== pconfigrlns
[idrlns
].ip
) &&
591 (tunnel
[t
].port
== pconfigrlns
[idrlns
].port
) &&
592 (tunnel
[t
].state
!= TUNNELDIE
))
594 if (tunnel
[t
].isremotelns
)
596 if (tunnel
[t
].state
== TUNNELOPENING
)
597 strcpy(state
, "Opening");
598 else if (tunnel
[t
].state
== TUNNELOPEN
)
599 strcpy(state
, "Open");
601 for (s
= 1; s
<= config
->cluster_highest_sessionid
; ++s
)
602 if (session
[s
].tunnel
== t
)
610 sprintf(strout
, "%15s %-32s %-32s %11s %7s %10u",
611 fmtaddr(htonl(pconfigrlns
[idrlns
].ip
), 0),
612 pconfigrlns
[idrlns
].l2tp_secret
,
613 pconfigrlns
[idrlns
].tunnel_assignment_id
,
614 (pconfigrlns
[idrlns
].state
== CONFRLNSSET
?"File":(pconfigrlns
[idrlns
].state
== CONFRLNSSETBYRADIUS
?"Radius":"Free")),