RADIUS load test
[l2tpns.git] / ppp.c
diff --git a/ppp.c b/ppp.c
index d48c044..3408d86 100644 (file)
--- a/ppp.c
+++ b/ppp.c
@@ -1,6 +1,6 @@
 // L2TPNS PPP Stuff
 
-char const *cvs_id_ppp = "$Id: ppp.c,v 1.45 2005-03-10 03:31:25 bodea Exp $";
+char const *cvs_id_ppp = "$Id: ppp.c,v 1.61 2005-05-13 09:23:00 bodea Exp $";
 
 #include <stdio.h>
 #include <string.h>
@@ -24,6 +24,7 @@ extern time_t time_now;
 extern configt *config;
 
 static void initccp(tunnelidt t, sessionidt s);
+static uint8_t *add_lcp_auth(uint8_t *b, int size, int authtype);
 
 // Process PAP messages
 void processpap(tunnelidt t, sessionidt s, uint8_t *p, uint16_t l)
@@ -39,14 +40,16 @@ void processpap(tunnelidt t, sessionidt s, uint8_t *p, uint16_t l)
        {
                LOG(1, s, t, "Short PAP %u bytes\n", l);
                STAT(tunnel_rx_errors);
-               return ;
+               sessionshutdown(s, "Short PAP packet.", 3, 0);
+               return;
        }
 
        if ((hl = ntohs(*(uint16_t *) (p + 2))) > l)
        {
                LOG(1, s, t, "Length mismatch PAP %u/%u\n", hl, l);
                STAT(tunnel_rx_errors);
-               return ;
+               sessionshutdown(s, "PAP length mismatch.", 3, 0);
+               return;
        }
        l = hl;
 
@@ -54,7 +57,8 @@ void processpap(tunnelidt t, sessionidt s, uint8_t *p, uint16_t l)
        {
                LOG(1, s, t, "Unexpected PAP code %d\n", *p);
                STAT(tunnel_rx_errors);
-               return ;
+               sessionshutdown(s, "Unexpected PAP code.", 3, 0);
+               return;
        }
 
        {
@@ -74,7 +78,7 @@ void processpap(tunnelidt t, sessionidt s, uint8_t *p, uint16_t l)
                }
                LOG(3, s, t, "PAP login %s/%s\n", user, pass);
        }
-       if (session[s].ip || !session[s].radius)
+       if (session[s].ip || !sess_local[s].radius)
        {
                // respond now, either no RADIUS available or already authenticated
                uint8_t b[MAXCONTROL];
@@ -102,11 +106,12 @@ void processpap(tunnelidt t, sessionidt s, uint8_t *p, uint16_t l)
                }
                LOG(3, s, t, "Fallback response to PAP (%s)\n", (session[s].ip) ? "ACK" : "NAK");
                tunnelsend(b, 5 + (p - b), t); // send it
+               sessionshutdown(s, "PAP authentication failed.", 3, 0);
        }
        else
        {
                // set up RADIUS request
-               uint16_t r = session[s].radius;
+               uint16_t r = sess_local[s].radius;
 
                // Run PRE_AUTH plugins
                struct param_pre_auth packet = { &tunnel[t], &session[s], strdup(user), strdup(pass), PPPPAP, 1 };
@@ -140,7 +145,7 @@ void processchap(tunnelidt t, sessionidt s, uint8_t *p, uint16_t l)
        CSTAT(processchap);
 
        LOG_HEX(5, "CHAP", p, l);
-       r = session[s].radius;
+       r = sess_local[s].radius;
        if (!r)
        {
                LOG(1, s, t, "Unexpected CHAP message\n");
@@ -152,14 +157,16 @@ void processchap(tunnelidt t, sessionidt s, uint8_t *p, uint16_t l)
        {
                LOG(1, s, t, "Short CHAP %u bytes\n", l);
                STAT(tunnel_rx_errors);
-               return ;
+               sessionshutdown(s, "Short CHAP packet.", 3, 0);
+               return;
        }
 
        if ((hl = ntohs(*(uint16_t *) (p + 2))) > l)
        {
                LOG(1, s, t, "Length mismatch CHAP %u/%u\n", hl, l);
                STAT(tunnel_rx_errors);
-               return ;
+               sessionshutdown(s, "CHAP length mismatch.", 3, 0);
+               return;
        }
        l = hl;
 
@@ -167,20 +174,23 @@ void processchap(tunnelidt t, sessionidt s, uint8_t *p, uint16_t l)
        {
                LOG(1, s, t, "Unexpected CHAP response code %d\n", *p);
                STAT(tunnel_rx_errors);
+               sessionshutdown(s, "CHAP length mismatch.", 3, 0);
                return;
        }
        if (p[1] != radius[r].id)
        {
                LOG(1, s, t, "Wrong CHAP response ID %d (should be %d) (%d)\n", p[1], radius[r].id, r);
                STAT(tunnel_rx_errors);
-               return ;
+               sessionshutdown(s, "Unexpected CHAP response ID.", 3, 0);
+               return;
        }
 
        if (l < 5 || p[4] != 16)
        {
                LOG(1, s, t, "Bad CHAP response length %d\n", l < 5 ? -1 : p[4]);
                STAT(tunnel_rx_errors);
-               return ;
+               sessionshutdown(s, "Bad CHAP response length.", 3, 0);
+               return;
        }
 
        l -= 5;
@@ -189,7 +199,8 @@ void processchap(tunnelidt t, sessionidt s, uint8_t *p, uint16_t l)
        {
                LOG(1, s, t, "CHAP user too long %d\n", l - 16);
                STAT(tunnel_rx_errors);
-               return ;
+               sessionshutdown(s, "CHAP username too long.", 3, 0);
+               return;
        }
 
        // Run PRE_AUTH plugins
@@ -275,8 +286,14 @@ static void dumplcp(uint8_t *p, int l)
                                {
                                        int proto = ntohs(*(uint16_t *)(o + 2));
                                        LOG(4, 0, 0, "   %s 0x%x (%s)\n", lcp_type(type), proto,
-                                               proto == PPPCHAP ? "CHAP" :
-                                               proto == PPPPAP  ? "PAP"  : "UNKNOWN");
+                                               proto == PPPPAP  ? "PAP"  : "UNSUPPORTED");
+                               }
+                               else if (length == 5)
+                               {
+                                       int proto = ntohs(*(uint16_t *)(o + 2));
+                                       int algo = *(uint8_t *)(o + 4);
+                                       LOG(4, 0, 0, "   %s 0x%x 0x%x (%s)\n", lcp_type(type), proto, algo,
+                                               (proto == PPPCHAP && algo == 5) ? "CHAP MD5"  : "UNSUPPORTED");
                                }
                                else
                                        LOG(4, 0, 0, "   %s odd length %d\n", lcp_type(type), length);
@@ -337,7 +354,33 @@ void processlcp(tunnelidt t, sessionidt s, uint8_t *p, uint16_t l)
 
        if (*p == ConfigAck)
        {
-               LOG(3, s, t, "LCP: Discarding ConfigAck\n");
+               int x = l - 4;
+               uint8_t *o = (p + 4);
+
+               LOG(3, s, t, "LCP: ConfigAck (%d bytes)...\n", l);
+               if (config->debug > 3) dumplcp(p, l);
+
+               while (x > 2)
+               {
+                       int type = o[0];
+                       int length = o[1];
+
+                       if (length == 0 || type == 0 || x < length) break;
+                       switch (type)
+                       {
+                               case 3: // Authentication-Protocol
+                                       {
+                                               int proto = ntohs(*(uint16_t *)(o + 2));
+                                               if (proto == PPPCHAP && *(o + 4) == 5)
+                                                       sendchap(t, s);
+                                       }
+
+                                       break;
+                       }
+                       x -= length;
+                       o += length;
+               }
+
                session[s].flags |= SF_LCP_ACKED;
        }
        else if (*p == ConfigReq)
@@ -387,24 +430,35 @@ void processlcp(tunnelidt t, sessionidt s, uint8_t *p, uint16_t l)
                                        *q++ = 6;
                                        memset(q, 0, 4); // asyncmap 0
                                        q += 4;
-                                       *((uint16_t *) (response + 2)) = htons(q - response); // LCP header length
                                        break;
 
                                case 3: // Authentication-Protocol
                                        {
                                                int proto = ntohs(*(uint16_t *)(o + 2));
                                                char proto_name[] = "0x0000";
+                                               uint8_t *a;
+
                                                if (proto == PPPPAP)
-                                                       break;
+                                               {
+                                                       if (config->radius_authtypes & AUTHPAP)
+                                                               break;
 
-                                               if (response && *response != ConfigNak) // rej already queued
-                                                       break;
+                                                       strcpy(proto_name, "PAP");
+                                               }
+                                               else if (proto == PPPCHAP)
+                                               {
+                                                       if (config->radius_authtypes & AUTHCHAP
+                                                           && *(o + 4) == 5) // MD5
+                                                               break;
 
-                                               if (proto == PPPCHAP)
                                                        strcpy(proto_name, "CHAP");
+                                               }
                                                else
                                                        sprintf(proto_name, "%#4.4x", proto);
 
+                                               if (response && *response != ConfigNak) // rej already queued
+                                                       break;
+
                                                LOG(2, s, t, "    Remote requesting %s authentication.  Rejecting.\n", proto_name);
 
                                                if (!response)
@@ -415,16 +469,28 @@ void processlcp(tunnelidt t, sessionidt s, uint8_t *p, uint16_t l)
                                                        q += 4;
                                                }
 
-                                               if ((q - b + length) > sizeof(b))
+                                               a = add_lcp_auth(q, sizeof(b) - (q - b), config->radius_authprefer);
+                                               if (!a)
                                                {
                                                        LOG(2, s, t, "LCP overflow for %s ConfigNak.\n", proto_name);
                                                        break;
                                                }
 
-                                               memcpy(q, o, length);
-                                               *(uint16_t *)(q += 2) = htons(PPPPAP); // NAK -> Use PAP instead
-                                               q += length;
-                                               *((uint16_t *) (response + 2)) = htons(q - response);
+                                               q = a;
+
+                                               if (config->radius_authtypes != config->radius_authprefer)
+                                               {
+                                                       a = add_lcp_auth(q, sizeof(b) - (q - b), config->radius_authtypes & ~config->radius_authprefer);
+                                                       if (!a)
+                                                       {
+                                                               LOG(2, s, t, "LCP overflow for %s ConfigNak.\n", proto_name);
+                                                               break;
+                                                       }
+
+                                                       q = a;
+                                               }
+
+                                               break;
                                        }
                                        break;
 
@@ -455,31 +521,97 @@ void processlcp(tunnelidt t, sessionidt s, uint8_t *p, uint16_t l)
 
                                        memcpy(q, o, length);
                                        q += length;
-                                       *((uint16_t *) (response + 2)) = htons(q - response); // LCP header length
                        }
                        x -= length;
                        o += length;
                }
 
-               if (!response)
+               if (response)
                {
-                       // Send back a ConfigAck
-                       q = response = makeppp(b, sizeof(b), p, l, t, s, PPPLCP);
-                       if (!q) return;
-                       *q = ConfigAck;
+                       l = q - response; // LCP packet length
+                       *((uint16_t *) (response + 2)) = htons(l); // update header
+               }
+               else
+               {
+                       // Send packet back as ConfigAck
+                       response = makeppp(b, sizeof(b), p, l, t, s, PPPLCP);
+                       if (!response) return;
+                       *response = ConfigAck;
                }
 
                LOG(3, s, t, "Sending %s\n", ppp_lcp_type(*response));
-               tunnelsend(b, l + (q - b), t);
+               tunnelsend(b, l + response - b, t);
 
                if (!(session[s].flags & SF_LCP_ACKED))
-                       initlcp(t, s);
+                       sendlcp(t, s, config->radius_authprefer);
        }
        else if (*p == ConfigNak)
        {
-               LOG(1, s, t, "Remote end sent a ConfigNak.  Ignoring\n");
+               int x = l - 4;
+               uint8_t *o = (p + 4);
+               int authtype = -1;
+
+               LOG(3, s, t, "LCP: ConfigNak (%d bytes)...\n", l);
                if (config->debug > 3) dumplcp(p, l);
-               return ;
+
+               while (x > 2)
+               {
+                       int type = o[0];
+                       int length = o[1];
+
+                       if (length == 0 || type == 0 || x < length) break;
+                       switch (type)
+                       {
+                               case 1: // Maximum-Receive-Unit
+                                       session[s].mru = ntohs(*(uint16_t *)(o + 2));
+                                       LOG(3, s, t, "    Remote requested MRU of %u\n", session[s].mru);
+                                       break;
+
+                               case 3: // Authentication-Protocol
+                                       if (authtype > 0)
+                                               break;
+
+                                       {
+                                               int proto = ntohs(*(uint16_t *)(o + 2));
+                                               if (proto == PPPPAP)
+                                               {
+                                                       authtype = config->radius_authtypes & AUTHPAP;
+                                                       LOG(3, s, t, "    Remote requested PAP authentication...%sing\n",
+                                                               authtype ? "accept" : "reject");
+                                               }
+                                               else if (proto == PPPCHAP && *(o + 4) == 5)
+                                               {
+                                                       authtype = config->radius_authtypes & AUTHCHAP;
+                                                       LOG(3, s, t, "    Remote requested CHAP authentication...%sing\n",
+                                                               authtype ? "accept" : "reject");
+                                               }
+                                               else
+                                               {
+                                                       LOG(3, s, t, "    Rejecting unsupported authentication %#4x\n",
+                                                               proto);
+                                               }
+                                       }
+
+                                       break;
+
+                               default:
+                                       LOG(2, s, t, "    Remote NAKed LCP type %u?\n", type);
+                                       break;
+                       }
+                       x -= length;
+                       o += length;
+               }
+
+               if (!authtype)
+               {
+                       sessionshutdown(s, "Unsupported authentication.", 3, 0);
+                       return;
+               }
+
+               if (authtype == -1)
+                       authtype = config->radius_authprefer;
+
+               sendlcp(t, s, authtype);
        }
        else if (*p == TerminateReq)
        {
@@ -488,11 +620,11 @@ void processlcp(tunnelidt t, sessionidt s, uint8_t *p, uint16_t l)
                q = makeppp(b, sizeof(b),  p, l, t, s, PPPLCP);
                if (!q) return;
                tunnelsend(b, l + (q - b), t); // send it
-               sessionshutdown(s, "Remote end closed connection.");
+               sessionshutdown(s, "Remote end closed connection.", 3, 0);
        }
        else if (*p == TerminateAck)
        {
-               sessionshutdown(s, "Connection closed.");
+               sessionshutdown(s, "Connection closed.", 3, 0);
        }
        else if (*p == ProtocolRej)
        {
@@ -538,7 +670,6 @@ void processlcp(tunnelidt t, sessionidt s, uint8_t *p, uint16_t l)
        {
                LOG(1, s, t, "Unexpected LCP code %d\n", *p);
                STAT(tunnel_rx_errors);
-               return ;
        }
 }
 
@@ -588,7 +719,7 @@ void processipcp(tunnelidt t, sessionidt s, uint8_t *p, uint16_t l)
        if (*p == ConfigAck)
        {
                // happy with our IPCP
-               uint16_t r = session[s].radius;
+               uint16_t r = sess_local[s].radius;
                if ((!r || radius[r].state == RADIUSIPCP) && !session[s].walled_garden)
                {
                        if (!r)
@@ -1076,11 +1207,11 @@ void processccp(tunnelidt t, sessionidt s, uint8_t *p, uint16_t l)
        tunnelsend(b, l + (q - b), t); // send it
 }
 
-// send a CHAP PP packet
+// send a CHAP challenge
 void sendchap(tunnelidt t, sessionidt s)
 {
        uint8_t b[MAXCONTROL];
-       uint16_t r = session[s].radius;
+       uint16_t r = sess_local[s].radius;
        uint8_t *q;
 
        CSTAT(sendchap);
@@ -1094,8 +1225,6 @@ void sendchap(tunnelidt t, sessionidt s)
 
        LOG(1, s, t, "Send CHAP challenge\n");
 
-       // new challenge
-       random_data(radius[r].auth, sizeof(radius[r].auth));
        radius[r].chap = 1;             // CHAP not PAP
        radius[r].id++;
        if (radius[r].state != RADIUSCHAP)
@@ -1105,7 +1234,7 @@ void sendchap(tunnelidt t, sessionidt s)
        radius[r].retry = backoff(radius[r].try++);
        if (radius[r].try > 5)
        {
-               sessionshutdown(s, "Timeout CHAP");
+               sessionshutdown(s, "CHAP timeout.", 3, 0);
                STAT(tunnel_tx_errors);
                return ;
        }
@@ -1114,7 +1243,7 @@ void sendchap(tunnelidt t, sessionidt s)
 
        *q = 1;                                 // challenge
        q[1] = radius[r].id;                    // ID
-       q[4] = 16;                              // length
+       q[4] = 16;                              // value size (size of challenge)
        memcpy(q + 5, radius[r].auth, 16);      // challenge
        strcpy(q + 21, hostname);               // our name
        *(uint16_t *) (q + 2) = htons(strlen(hostname) + 21); // length
@@ -1165,27 +1294,64 @@ uint8_t *makeppp(uint8_t *b, int size, uint8_t *p, int l, tunnelidt t, sessionid
        return b;
 }
 
-// Send initial LCP ConfigReq for PAP, set magic no.
-void initlcp(tunnelidt t, sessionidt s)
+static uint8_t *add_lcp_auth(uint8_t *b, int size, int authtype)
 {
-       char b[500], *q;
+       if ((authtype == AUTHCHAP && size < 5) || size < 4)
+               return 0;
+
+       *b++ = 3; // Authentication-Protocol
+       if (authtype == AUTHCHAP)
+       {
+               *b++ = 5; // length
+               *(uint16_t *) b = htons(PPPCHAP); b += 2;
+               *b++ = 5; // MD5
+       }
+       else if (authtype == AUTHPAP)
+       {
+               *b++ = 4; // length
+               *(uint16_t *) b = htons(PPPPAP); b += 2;
+       }
+       else
+       {
+               LOG(0, 0, 0, "add_lcp_auth called with unsupported auth type %d\n", authtype);
+       }
+
+       return b;
+}
+
+// Send initial LCP ConfigReq for MRU, authentication type and magic no
+void sendlcp(tunnelidt t, sessionidt s, int authtype)
+{
+       char b[500], *q, *l;
 
        if (!(q = makeppp(b, sizeof(b), NULL, 0, t, s, PPPLCP)))
                return;
 
-       LOG(4, s, t, "Sending LCP ConfigReq for PAP\n");
-       *q = ConfigReq;
-       *(uint8_t *)(q + 1) = (time_now % 255) + 1; // ID
-       *(uint16_t *)(q + 2) = htons(14); // Length
-       *(uint8_t *)(q + 4) = 5;
-       *(uint8_t *)(q + 5) = 6;
-       *(uint32_t *)(q + 6) = htonl(session[s].magic);
-       *(uint8_t *)(q + 10) = 3;
-       *(uint8_t *)(q + 11) = 4;
-       *(uint16_t *)(q + 12) = htons(PPPPAP); // PAP
-
-       LOG_HEX(5, "PPPLCP", q, 14);
-       tunnelsend(b, (q - b) + 14, t);
+       LOG(4, s, t, "Sending LCP ConfigReq for %s\n",
+           authtype == AUTHCHAP ? "CHAP" : "PAP");
+
+       if (!session[s].mru)
+               session[s].mru = DEFAULT_MRU;
+
+       l = q;
+       *l++ = ConfigReq;
+       *l++ = (time_now % 255) + 1; // ID
+
+       l += 2; //Save space for length
+
+       *l++ = 1; *l++ = 4; // Maximum-Receive-Unit (length 4)
+       *(uint16_t *) l = htons(session[s].mru); l += 2;
+
+       l = add_lcp_auth(l, sizeof(b) - (l - b), authtype);
+
+       *l++ = 5; *l++ = 6; // Magic-Number (length 6)
+       *(uint32_t *) l = htonl(session[s].magic);
+       l += 4;
+
+       *(uint16_t *)(q + 2) = htons(l - q); // Length
+
+       LOG_HEX(5, "PPPLCP", q, l - q);
+       tunnelsend(b, (l - b), t);
 }
 
 // Send CCP request for no compression