set acct-disconnect-cause from result code AVP if no disconnect cause AVP is present
[l2tpns.git] / l2tpns.c
index 519a831..03e780c 100644 (file)
--- a/l2tpns.c
+++ b/l2tpns.c
@@ -4,7 +4,7 @@
 // Copyright (c) 2002 FireBrick (Andrews & Arnold Ltd / Watchfront Ltd) - GPL licenced
 // vim: sw=8 ts=8
 
-char const *cvs_id_l2tpns = "$Id: l2tpns.c,v 1.166 2006-05-16 06:46:37 bodea Exp $";
+char const *cvs_id_l2tpns = "$Id: l2tpns.c,v 1.168 2006-06-22 15:30:29 bodea Exp $";
 
 #include <arpa/inet.h>
 #include <assert.h>
@@ -1075,6 +1075,7 @@ void processmpframe(sessionidt s, tunnelidt t, uint8_t *p, uint16_t l, uint8_t e
                p += 2;
                l -= 2;
        }
+
        if (proto == PPPIP)
        {
                if (session[s].die)
@@ -1082,7 +1083,8 @@ void processmpframe(sessionidt s, tunnelidt t, uint8_t *p, uint16_t l, uint8_t e
                        LOG(4, s, t, "MPPP: Session %u is closing.  Don't process PPP packets\n", s);
                        return;              // closing session, PPP not processed
                }
-               session[s].last_packet = time_now;
+
+               session[s].last_packet = session[s].last_data = time_now;
                processipin(s, t, p, l);
        }
        else if (proto == PPPIPV6 && config->ipv6_prefix.s6_addr[0])
@@ -1093,7 +1095,7 @@ void processmpframe(sessionidt s, tunnelidt t, uint8_t *p, uint16_t l, uint8_t e
                        return;              // closing session, PPP not processed
                }
 
-               session[s].last_packet = time_now;
+               session[s].last_packet = session[s].last_data = time_now;
                processipv6in(s, t, p, l);
        }
        else
@@ -1166,6 +1168,7 @@ static void processipout(uint8_t *buf, int len)
        }
        t = session[s].tunnel;
        sp = &session[s];
+       sp->last_data = time_now;
 
        // DoS prevention: enforce a maximum number of packets per 0.1s for a session
        if (config->max_packets > 0)
@@ -1363,6 +1366,7 @@ static void processipv6out(uint8_t * buf, int len)
        }
        t = session[s].tunnel;
        sp = &session[s];
+       sp->last_data = time_now;
 
        // FIXME: add DoS prevention/filters?
 
@@ -2180,9 +2184,10 @@ void processudp(uint8_t *buf, int len, struct sockaddr_in *addr)
                        int error = 0;
                        char *msg = 0;
 
-                       // default disconnect cause/message on receipt
-                       // of CDN (set to more specific value from
-                       // attribute 46 if present below).
+                       // Default disconnect cause/message on receipt of CDN.  Set to
+                       // more specific value from attribute 1 (result code) or 46
+                       // (disconnect cause) if present below.
+                       int disc_cause_set = 0;
                        int disc_cause = TERM_NAS_REQUEST;
                        char const *disc_reason = "Closed (Received CDN).";
 
@@ -2292,25 +2297,41 @@ void processudp(uint8_t *buf, int len, struct sockaddr_in *addr)
                                case 1:     // result code
                                        {
                                                uint16_t rescode = ntohs(*(uint16_t *) b);
-                                               const char* resdesc = "(unknown)";
+                                               char const *resdesc = "(unknown)";
+                                               char const *errdesc = NULL;
+                                               int cause = 0;
+
                                                if (message == 4)
                                                { /* StopCCN */
                                                        resdesc = l2tp_stopccn_result_code(rescode);
+                                                       cause = TERM_LOST_SERVICE;
                                                }
                                                else if (message == 14)
                                                { /* CDN */
                                                        resdesc = l2tp_cdn_result_code(rescode);
+                                                       if (rescode == 1)
+                                                               cause = TERM_LOST_CARRIER;
+                                                       else
+                                                               cause = TERM_ADMIN_RESET;
                                                }
 
                                                LOG(4, s, t, "   Result Code %u: %s\n", rescode, resdesc);
                                                if (n >= 4)
                                                {
                                                        uint16_t errcode = ntohs(*(uint16_t *)(b + 2));
-                                                       LOG(4, s, t, "   Error Code %u: %s\n", errcode, l2tp_error_code(errcode));
+                                                       errdesc = l2tp_error_code(errcode);
+                                                       LOG(4, s, t, "   Error Code %u: %s\n", errcode, errdesc);
                                                }
                                                if (n > 4)
                                                        LOG(4, s, t, "   Error String: %.*s\n", n-4, b+4);
 
+                                               if (cause && disc_cause_set < mtype) // take cause from attrib 46 in preference
+                                               {
+                                                       disc_cause_set = mtype;
+                                                       disc_reason = errdesc ? errdesc : resdesc;
+                                                       disc_cause = cause;
+                                               }
+
                                                break;
                                        }
                                        break;
@@ -2502,6 +2523,8 @@ void processudp(uint8_t *buf, int len, struct sockaddr_in *addr)
                                                        "(code=%u, proto=%04X, dir=%u, msg=\"%.*s\")\n",
                                                        code, proto, dir, n - 5, b + 5);
 
+                                               disc_cause_set = mtype;
+
                                                switch (code)
                                                {
                                                case 1: // admin disconnect
@@ -2642,7 +2665,7 @@ void processudp(uint8_t *buf, int len, struct sockaddr_in *addr)
                                                session[s].opened = time_now;
                                                session[s].tunnel = t;
                                                session[s].far = asession;
-                                               session[s].last_packet = time_now;
+                                               session[s].last_packet = session[s].last_data = time_now;
                                                LOG(3, s, t, "New session (%u/%u)\n", tunnel[t].far, session[s].far);
                                                control16(c, 14, s, 1); // assigned session
                                                controladd(c, asession, t); // send the reply
@@ -2803,7 +2826,7 @@ void processudp(uint8_t *buf, int len, struct sockaddr_in *addr)
                                return;              // closing session, PPP not processed
                        }
 
-                       session[s].last_packet = time_now;
+                       session[s].last_packet = session[s].last_data = time_now;
                        if (session[s].walled_garden && !config->cluster_iam_master)
                        {
                                master_forward_packet(buf, len, addr->sin_addr.s_addr, addr->sin_port);
@@ -2820,7 +2843,7 @@ void processudp(uint8_t *buf, int len, struct sockaddr_in *addr)
                                return;              // closing session, PPP not processed
                        }
 
-                       session[s].last_packet = time_now;
+                       session[s].last_packet = session[s].last_data = time_now;
                        if (session[s].walled_garden && !config->cluster_iam_master)
                        {
                                master_forward_packet(buf, len, addr->sin_addr.s_addr, addr->sin_port);
@@ -2837,7 +2860,7 @@ void processudp(uint8_t *buf, int len, struct sockaddr_in *addr)
                                return;              // closing session, PPP not processed
                        }
 
-                       session[s].last_packet = time_now;
+                       session[s].last_packet = session[s].last_data = time_now;
                        if (session[s].walled_garden && !config->cluster_iam_master)
                        {
                                master_forward_packet(buf, len, addr->sin_addr.s_addr, addr->sin_port);
@@ -3195,6 +3218,24 @@ static void regular_cleanups(double period)
                        s_actions++;
                }
 
+               // Drop sessions who have reached session_timeout seconds
+               if (session[s].session_timeout && (time_now - session[s].opened >= session[s].session_timeout))
+               {
+                       sessionshutdown(s, "Session Timeout Reached", CDN_ADMIN_DISC, TERM_SESSION_TIMEOUT);
+                       STAT(session_timeout);
+                       s_actions++;
+                       continue;
+               }
+
+               // Drop sessions who have reached idle_timeout seconds
+               if (session[s].last_data && session[s].idle_timeout && (time_now - session[s].last_data >= session[s].idle_timeout))
+               {
+                       sessionshutdown(s, "Idle Timeout Reached", CDN_ADMIN_DISC, TERM_IDLE_TIMEOUT);
+                       STAT(session_timeout);
+                       s_actions++;
+                       continue;
+               }
+
                // Check for actions requested from the CLI
                if ((a = cli_session_actions[s].action))
                {
@@ -4876,7 +4917,7 @@ int sessionsetup(sessionidt s, tunnelidt t)
        if (session[s].throttle_in || session[s].throttle_out)
                throttle_session(s, session[s].throttle_in, session[s].throttle_out);
 
-       session[s].last_packet = time_now;
+       session[s].last_packet = session[s].last_data = time_now;
 
        LOG(2, s, t, "Login by %s at %s from %s (%s)\n", session[s].user,
                fmtaddr(htonl(session[s].ip), 0),
@@ -5439,7 +5480,7 @@ int cmd_show_hist_idle(struct cli_def *cli, char *command, char **argv, int argc
                if (!session[s].opened)
                        continue;
 
-               idle = time_now - session[s].last_packet;
+               idle = time_now - session[s].last_data;
                idle /= 5 ; // In multiples of 5 seconds.
                if (idle < 0)
                        idle = 0;