Sending IPv6_ra more often.
[l2tpns.git] / cluster.c
index 78de01b..26466be 100644 (file)
--- a/cluster.c
+++ b/cluster.c
@@ -17,6 +17,7 @@
 #include <errno.h>
 #include <libcli.h>
 
 #include <errno.h>
 #include <libcli.h>
 
+#include "dhcp6.h"
 #include "l2tpns.h"
 #include "cluster.h"
 #include "util.h"
 #include "l2tpns.h"
 #include "cluster.h"
 #include "util.h"
@@ -305,10 +306,11 @@ static int _forward_packet(uint8_t *data, int size, in_addr_t addr, int port, in
 // The master just processes the payload as if it had
 // received it off the tun device.
 //(note: THIS ROUTINE WRITES TO pack[-6]).
 // The master just processes the payload as if it had
 // received it off the tun device.
 //(note: THIS ROUTINE WRITES TO pack[-6]).
-int master_forward_packet(uint8_t *data, int size, in_addr_t addr, int port)
+int master_forward_packet(uint8_t *data, int size, in_addr_t addr, uint16_t port, uint16_t indexudp)
 {
        uint8_t *p = data - (3 * sizeof(uint32_t));
        uint8_t *psave = p;
 {
        uint8_t *p = data - (3 * sizeof(uint32_t));
        uint8_t *psave = p;
+       uint32_t indexandport = port | ((indexudp << 16) & 0xFFFF0000);
 
        if (!config->cluster_master_address) // No election has been held yet. Just skip it.
                return -1;
 
        if (!config->cluster_master_address) // No election has been held yet. Just skip it.
                return -1;
@@ -316,7 +318,7 @@ int master_forward_packet(uint8_t *data, int size, in_addr_t addr, int port)
        LOG(4, 0, 0, "Forwarding packet from %s to master (size %d)\n", fmtaddr(addr, 0), size);
 
        STAT(c_forwarded);
        LOG(4, 0, 0, "Forwarding packet from %s to master (size %d)\n", fmtaddr(addr, 0), size);
 
        STAT(c_forwarded);
-       add_type(&p, C_FORWARD, addr, (uint8_t *) &port, sizeof(port)); // ick. should be uint16_t
+       add_type(&p, C_FORWARD, addr, (uint8_t *) &indexandport, sizeof(indexandport));
 
        return peer_send_data(config->cluster_master_address, psave, size + (3 * sizeof(uint32_t)));
 }
 
        return peer_send_data(config->cluster_master_address, psave, size + (3 * sizeof(uint32_t)));
 }
@@ -841,7 +843,7 @@ static int hb_add_type(uint8_t **p, int type, int id)
                        // Failed to compress : Fall through.
                }
                case C_SESSION:
                        // Failed to compress : Fall through.
                }
                case C_SESSION:
-                       add_type(p, C_SESSION, id, (uint8_t *) &session[id], sizeof(sessiont));
+                       add_type(p, C_SESSION, id, (uint8_t *) &session[id], sizeof(sessiont));
                        break;
 
                case C_CBUNDLE: { // Compressed C_BUNDLE
                        break;
 
                case C_CBUNDLE: { // Compressed C_BUNDLE
@@ -882,7 +884,7 @@ static int hb_add_type(uint8_t **p, int type, int id)
                        // Failed to compress : Fall through.
                }
                case C_TUNNEL:
                        // Failed to compress : Fall through.
                }
                case C_TUNNEL:
-                       add_type(p, C_TUNNEL, id, (uint8_t *) &tunnel[id], sizeof(tunnelt));
+                       add_type(p, C_TUNNEL, id, (uint8_t *) &tunnel[id], sizeof(tunnelt));
                        break;
                default:
                        LOG(0, 0, 0, "Found an invalid type in heart queue! (%d)\n", type);
                        break;
                default:
                        LOG(0, 0, 0, "Found an invalid type in heart queue! (%d)\n", type);
@@ -1423,6 +1425,128 @@ struct oldsession {
        char reserved_3[11];
 };
 
        char reserved_3[11];
 };
 
+struct oldsessionV7 {
+       sessionidt next;                // next session in linked list
+       sessionidt far;                 // far end session ID
+       tunnelidt tunnel;               // near end tunnel ID
+       uint8_t flags;                  // session flags: see SESSION_*
+       struct {
+               uint8_t phase;          // PPP phase
+               uint8_t lcp:4;          //   LCP    state
+               uint8_t ipcp:4;         //   IPCP   state
+               uint8_t ipv6cp:4;       //   IPV6CP state
+               uint8_t ccp:4;          //   CCP    state
+       } ppp;
+       uint16_t mru;                   // maximum receive unit
+       in_addr_t ip;                   // IP of session set by RADIUS response (host byte order).
+       int ip_pool_index;              // index to IP pool
+       uint32_t unique_id;             // unique session id
+       uint32_t magic;                 // ppp magic number
+       uint32_t pin, pout;             // packet counts
+       uint32_t cin, cout;             // byte counts
+       uint32_t cin_wrap, cout_wrap;   // byte counter wrap count (RADIUS accounting giagawords)
+       uint32_t cin_delta, cout_delta; // byte count changes (for dump_session())
+       uint16_t throttle_in;           // upstream throttle rate (kbps)
+       uint16_t throttle_out;          // downstream throttle rate
+       uint8_t filter_in;              // input filter index (to ip_filters[N-1]; 0 if none)
+       uint8_t filter_out;             // output filter index
+       uint16_t snoop_port;            // Interception destination port
+       in_addr_t snoop_ip;             // Interception destination IP
+       clockt opened;                  // when started
+       clockt die;                     // being closed, when to finally free
+       uint32_t session_timeout;       // Maximum session time in seconds
+       uint32_t idle_timeout;          // Maximum idle time in seconds
+       time_t last_packet;             // Last packet from the user (used for idle timeouts)
+       time_t last_data;               // Last data packet to/from the user (used for idle timeouts)
+       in_addr_t dns1, dns2;           // DNS servers
+       routet route[MAXROUTE];         // static routes
+       uint16_t tbf_in;                // filter bucket for throttling in from the user.
+       uint16_t tbf_out;               // filter bucket for throttling out to the user.
+       int random_vector_length;
+       uint8_t random_vector[MAXTEL];
+       char user[MAXUSER];             // user (needed in session for radius stop messages)
+       char called[MAXTEL];            // called number
+       char calling[MAXTEL];           // calling number
+       uint32_t tx_connect_speed;
+       uint32_t rx_connect_speed;
+       clockt timeout;                 // Session timeout
+       uint32_t mrru;                  // Multilink Max-Receive-Reconstructed-Unit
+       epdist epdis;                   // Multilink Endpoint Discriminator
+       bundleidt bundle;               // Multilink Bundle Identifier
+       uint8_t mssf;                   // Multilink Short Sequence Number Header Format
+       uint8_t walled_garden;          // is this session gardened?
+       uint8_t classlen;               // class (needed for radius accounting messages)
+       char class[MAXCLASS];
+       uint8_t ipv6prefixlen;          // IPv6 route prefix length
+       struct in6_addr ipv6route;      // Static IPv6 route
+       sessionidt forwardtosession;    // LNS id_session to forward
+       uint8_t src_hwaddr[ETH_ALEN];   // MAC addr source (for pppoe sessions 6 bytes)
+       char reserved[4];               // Space to expand structure without changing HB_VERSION
+};
+
+struct oldsessionV8 {
+       sessionidt next;                // next session in linked list
+       sessionidt far;                 // far end session ID
+       tunnelidt tunnel;               // near end tunnel ID
+       uint8_t flags;                  // session flags: see SESSION_*
+       struct {
+               uint8_t phase;          // PPP phase
+               uint8_t lcp:4;          //   LCP    state
+               uint8_t ipcp:4;         //   IPCP   state
+               uint8_t ipv6cp:4;       //   IPV6CP state
+               uint8_t ccp:4;          //   CCP    state
+       } ppp;
+       uint16_t mru;                   // maximum receive unit
+       in_addr_t ip;                   // IP of session set by RADIUS response (host byte order).
+       int ip_pool_index;              // index to IP pool
+       uint32_t unique_id;             // unique session id
+       uint32_t magic;                 // ppp magic number
+       uint32_t pin, pout;             // packet counts
+       uint32_t cin, cout;             // byte counts
+       uint32_t cin_wrap, cout_wrap;   // byte counter wrap count (RADIUS accounting giagawords)
+       uint32_t cin_delta, cout_delta; // byte count changes (for dump_session())
+       uint16_t throttle_in;           // upstream throttle rate (kbps)
+       uint16_t throttle_out;          // downstream throttle rate
+       uint8_t filter_in;              // input filter index (to ip_filters[N-1]; 0 if none)
+       uint8_t filter_out;             // output filter index
+       uint16_t snoop_port;            // Interception destination port
+       in_addr_t snoop_ip;             // Interception destination IP
+       clockt opened;                  // when started
+       clockt die;                     // being closed, when to finally free
+       uint32_t session_timeout;       // Maximum session time in seconds
+       uint32_t idle_timeout;          // Maximum idle time in seconds
+       time_t last_packet;             // Last packet from the user (used for idle timeouts)
+       time_t last_data;               // Last data packet to/from the user (used for idle timeouts)
+       in_addr_t dns1, dns2;           // DNS servers
+       routet route[MAXROUTE];         // static routes
+       uint16_t tbf_in;                // filter bucket for throttling in from the user.
+       uint16_t tbf_out;               // filter bucket for throttling out to the user.
+       int random_vector_length;
+       uint8_t random_vector[MAXTEL];
+       char user[MAXUSER];             // user (needed in session for radius stop messages)
+       char called[MAXTEL];            // called number
+       char calling[MAXTEL];           // calling number
+       uint32_t tx_connect_speed;
+       uint32_t rx_connect_speed;
+       clockt timeout;                 // Session timeout
+       uint32_t mrru;                  // Multilink Max-Receive-Reconstructed-Unit
+       epdist epdis;                   // Multilink Endpoint Discriminator
+       bundleidt bundle;               // Multilink Bundle Identifier
+       uint8_t mssf;                   // Multilink Short Sequence Number Header Format
+       uint8_t walled_garden;          // is this session gardened?
+       uint8_t classlen;               // class (needed for radius accounting messages)
+       char class[MAXCLASS];
+       uint8_t ipv6prefixlen;          // IPv6 route prefix length
+       struct in6_addr ipv6route;      // Static IPv6 route
+       sessionidt forwardtosession;    // LNS id_session to forward
+       uint8_t src_hwaddr[ETH_ALEN];   // MAC addr source (for pppoe sessions 6 bytes)
+       uint32_t dhcpv6_prefix_iaid;    // prefix iaid requested by client
+       uint32_t dhcpv6_iana_iaid;              // iaid of iana requested by client
+       struct in6_addr ipv6address;    // Framed Ipv6 address
+       struct dhcp6_opt_clientid dhcpv6_client_id; // Size max (headers + DUID)
+       char reserved[4];               // Space to expand structure without changing HB_VERSION
+};
+
 static uint8_t *convert_session(struct oldsession *old)
 {
        static sessiont new;
 static uint8_t *convert_session(struct oldsession *old)
 {
        static sessiont new;
@@ -1477,8 +1601,8 @@ static uint8_t *convert_session(struct oldsession *old)
        new.snoop_ip = old->snoop_ip;
        new.snoop_port = old->snoop_port;
        new.walled_garden = old->walled_garden;
        new.snoop_ip = old->snoop_ip;
        new.snoop_port = old->snoop_port;
        new.walled_garden = old->walled_garden;
-       new.ipv6prefixlen = old->ipv6prefixlen;
-       new.ipv6route = old->ipv6route;
+       new.route6[0].ipv6prefixlen = old->ipv6prefixlen;
+       new.route6[0].ipv6route = old->ipv6route;
 
        memcpy(new.random_vector, old->random_vector, sizeof(new.random_vector));
        memcpy(new.user, old->user, sizeof(new.user));
 
        memcpy(new.random_vector, old->random_vector, sizeof(new.random_vector));
        memcpy(new.user, old->user, sizeof(new.user));
@@ -1491,6 +1615,152 @@ static uint8_t *convert_session(struct oldsession *old)
        return (uint8_t *) &new;
 }
 
        return (uint8_t *) &new;
 }
 
+static uint8_t *convert_sessionV7(struct oldsessionV7 *old)
+{
+       static sessiont new;
+       int i;
+
+       memset(&new, 0, sizeof(new));
+
+       new.next = old->next;
+       new.far = old->far;
+       new.tunnel = old->tunnel;
+       new.flags = old->flags;
+       new.ppp.phase = old->ppp.phase;
+       new.ppp.lcp = old->ppp.lcp;
+       new.ppp.ipcp = old->ppp.ipcp;
+       new.ppp.ipv6cp = old->ppp.ipv6cp;
+       new.ppp.ccp = old->ppp.ccp;
+       new.mru = old->mru;
+       new.ip = old->ip;
+       new.ip_pool_index = old->ip_pool_index;
+       new.unique_id = old->unique_id;
+       new.magic = old->magic;
+       new.pin = old->pin;
+       new.pout = old->pout;
+       new.cin = old->cin;
+       new.cout = old->cout;
+       new.cin_wrap = old->cin_wrap;
+       new.cout_wrap = old->cout_wrap;
+       new.cin_delta = old->cin_delta;
+       new.cout_delta = old->cout_delta;
+       new.throttle_in = old->throttle_in;
+       new.throttle_out = old->throttle_out;
+       new.filter_in = old->filter_in;
+       new.filter_out = old->filter_out;
+       new.snoop_port = old->snoop_port;
+       new.snoop_ip = old->snoop_ip;
+       new.opened = old->opened;
+       new.die = old->die;
+       new.session_timeout = old->session_timeout;
+       new.idle_timeout = old->idle_timeout;
+       new.last_packet = old->last_packet;
+       new.last_data = old->last_data;
+       new.dns1 = old->dns1;
+       new.dns2 = old->dns2;
+       for (i = 0; i < MAXROUTE; i++)
+               memcpy(&new.route[i], &old->route[i], sizeof(new.route[i]));
+       new.tbf_in = old->tbf_in;
+       new.tbf_out = old->tbf_out;
+       new.random_vector_length = old->random_vector_length;
+       memcpy(new.random_vector, old->random_vector, sizeof(new.random_vector));
+       memcpy(new.user, old->user, sizeof(new.user));
+       memcpy(new.called, old->called, sizeof(new.called));
+       memcpy(new.calling, old->calling, sizeof(new.calling));
+       new.tx_connect_speed = old->tx_connect_speed;
+       new.rx_connect_speed = old->rx_connect_speed;
+       new.timeout = old->timeout;
+       new.mrru = old->mrru;
+       new.epdis = old->epdis;
+       new.bundle = old->bundle;
+       new.mssf = old->mssf;
+       new.walled_garden = old->walled_garden;
+       new.classlen = old->classlen;
+       memcpy(new.class, old->class, sizeof(new.class));
+       new.route6[0].ipv6prefixlen = old->ipv6prefixlen;
+       new.route6[0].ipv6route = old->ipv6route;
+
+       new.forwardtosession = old->forwardtosession;
+       memcpy(new.src_hwaddr, old->src_hwaddr, sizeof(new.src_hwaddr));
+
+       return (uint8_t *) &new;
+}
+
+static uint8_t *convert_sessionV8(struct oldsessionV8 *old)
+{
+       static sessiont new;
+       int i;
+
+       memset(&new, 0, sizeof(new));
+
+       new.next = old->next;
+       new.far = old->far;
+       new.tunnel = old->tunnel;
+       new.flags = old->flags;
+       new.ppp.phase = old->ppp.phase;
+       new.ppp.lcp = old->ppp.lcp;
+       new.ppp.ipcp = old->ppp.ipcp;
+       new.ppp.ipv6cp = old->ppp.ipv6cp;
+       new.ppp.ccp = old->ppp.ccp;
+       new.mru = old->mru;
+       new.ip = old->ip;
+       new.ip_pool_index = old->ip_pool_index;
+       new.unique_id = old->unique_id;
+       new.magic = old->magic;
+       new.pin = old->pin;
+       new.pout = old->pout;
+       new.cin = old->cin;
+       new.cout = old->cout;
+       new.cin_wrap = old->cin_wrap;
+       new.cout_wrap = old->cout_wrap;
+       new.cin_delta = old->cin_delta;
+       new.cout_delta = old->cout_delta;
+       new.throttle_in = old->throttle_in;
+       new.throttle_out = old->throttle_out;
+       new.filter_in = old->filter_in;
+       new.filter_out = old->filter_out;
+       new.snoop_port = old->snoop_port;
+       new.snoop_ip = old->snoop_ip;
+       new.opened = old->opened;
+       new.die = old->die;
+       new.session_timeout = old->session_timeout;
+       new.idle_timeout = old->idle_timeout;
+       new.last_packet = old->last_packet;
+       new.last_data = old->last_data;
+       new.dns1 = old->dns1;
+       new.dns2 = old->dns2;
+       for (i = 0; i < MAXROUTE; i++)
+               memcpy(&new.route[i], &old->route[i], sizeof(new.route[i]));
+       new.tbf_in = old->tbf_in;
+       new.tbf_out = old->tbf_out;
+       new.random_vector_length = old->random_vector_length;
+       memcpy(new.random_vector, old->random_vector, sizeof(new.random_vector));
+       memcpy(new.user, old->user, sizeof(new.user));
+       memcpy(new.called, old->called, sizeof(new.called));
+       memcpy(new.calling, old->calling, sizeof(new.calling));
+       new.tx_connect_speed = old->tx_connect_speed;
+       new.rx_connect_speed = old->rx_connect_speed;
+       new.timeout = old->timeout;
+       new.mrru = old->mrru;
+       new.epdis = old->epdis;
+       new.bundle = old->bundle;
+       new.mssf = old->mssf;
+       new.walled_garden = old->walled_garden;
+       new.classlen = old->classlen;
+       memcpy(new.class, old->class, sizeof(new.class));
+       new.route6[0].ipv6prefixlen = old->ipv6prefixlen;
+       new.route6[0].ipv6route = old->ipv6route;
+
+       new.forwardtosession = old->forwardtosession;
+       memcpy(new.src_hwaddr, old->src_hwaddr, sizeof(new.src_hwaddr));
+       new.dhcpv6_prefix_iaid = old->dhcpv6_prefix_iaid;
+       new.dhcpv6_iana_iaid = old->dhcpv6_iana_iaid;
+       new.ipv6address = old->ipv6address;
+       new.dhcpv6_client_id = old->dhcpv6_client_id;
+
+       return (uint8_t *) &new;
+}
+
 //
 // Process a heartbeat..
 //
 //
 // Process a heartbeat..
 //
@@ -1503,18 +1773,11 @@ static int cluster_process_heartbeat(uint8_t *data, int size, int more, uint8_t
        int i, type;
        int hb_ver = more;
 
        int i, type;
        int hb_ver = more;
 
-#ifdef LAC
-#if HB_VERSION != 7
+#if HB_VERSION != 9
 # error "need to update cluster_process_heartbeat()"
 # error "need to update cluster_process_heartbeat()"
-#endif
-#else
-#if HB_VERSION != 6
-# error "need to update cluster_process_heartbeat()"
-#endif
 #endif
 
 #endif
 
-
-       // we handle versions 5 through 7
+       // we handle versions 5 through 8
        if (hb_ver < 5 || hb_ver > HB_VERSION) {
                LOG(0, 0, 0, "Received a heartbeat version that I don't support (%d)!\n", hb_ver);
                return -1; // Ignore it??
        if (hb_ver < 5 || hb_ver > HB_VERSION) {
                LOG(0, 0, 0, "Received a heartbeat version that I don't support (%d)!\n", hb_ver);
                return -1; // Ignore it??
@@ -1687,10 +1950,23 @@ static int cluster_process_heartbeat(uint8_t *data, int size, int more, uint8_t
                                        break;
                                }
 
                                        break;
                                }
 
-                               if (size != sizeof(sessiont) ) { // Ouch! Very very bad!
-                                       LOG(0, 0, 0, "DANGER: Received a CSESSION that didn't decompress correctly!\n");
-                                               // Now what? Should exit! No-longer up to date!
-                                       break;
+                               if (size != sizeof(sessiont)) { // Ouch! Very very bad!
+                                       if ((hb_ver < HB_VERSION) && (size < sizeof(sessiont)))
+                                       {
+                                               if ((hb_ver == 7) && (size == sizeof(struct oldsessionV7)))
+                                                       cluster_recv_session(more, convert_sessionV7((struct oldsessionV7 *) c));
+                                               else if (size == sizeof(struct oldsessionV8))
+                                                       cluster_recv_session(more, convert_sessionV8((struct oldsessionV8 *) c));
+                                               else
+                                                       LOG(0, 0, 0, "DANGER: Received a CSESSION version=%d that didn't decompress correctly!\n", hb_ver);
+                                               break;
+                                       }
+                                       else
+                                       {
+                                               LOG(0, 0, 0, "DANGER: Received a CSESSION that didn't decompress correctly!\n");
+                                                       // Now what? Should exit! No-longer up to date!
+                                               break;
+                                       }
                                }
 
                                cluster_recv_session(more, c);
                                }
 
                                cluster_recv_session(more, c);
@@ -1726,12 +2002,8 @@ static int cluster_process_heartbeat(uint8_t *data, int size, int more, uint8_t
                                size = rle_decompress((uint8_t **) &p, s, c, sizeof(c));
                                s -= (p - orig_p);
 
                                size = rle_decompress((uint8_t **) &p, s, c, sizeof(c));
                                s -= (p - orig_p);
 
-#ifdef LAC
                                if ( ((hb_ver >= HB_VERSION) && (size != sizeof(tunnelt))) ||
                                         ((hb_ver < HB_VERSION) && (size > sizeof(tunnelt))) )
                                if ( ((hb_ver >= HB_VERSION) && (size != sizeof(tunnelt))) ||
                                         ((hb_ver < HB_VERSION) && (size > sizeof(tunnelt))) )
-#else
-                               if (size != sizeof(tunnelt) )
-#endif
                                { // Ouch! Very very bad!
                                        LOG(0, 0, 0, "DANGER: Received a CTUNNEL that didn't decompress correctly!\n");
                                                // Now what? Should exit! No-longer up to date!
                                { // Ouch! Very very bad!
                                        LOG(0, 0, 0, "DANGER: Received a CTUNNEL that didn't decompress correctly!\n");
                                                // Now what? Should exit! No-longer up to date!
@@ -1854,9 +2126,11 @@ int processcluster(uint8_t *data, int size, in_addr_t addr)
                else
                {
                        struct sockaddr_in a;
                else
                {
                        struct sockaddr_in a;
+                       uint16_t indexudp;
                        a.sin_addr.s_addr = more;
 
                        a.sin_addr.s_addr = more;
 
-                       a.sin_port = *(int *) p;
+                       a.sin_port = (*(int *) p) & 0xFFFF;
+                       indexudp = ((*(int *) p) >> 16) & 0xFFFF;
                        s -= sizeof(int);
                        p += sizeof(int);
 
                        s -= sizeof(int);
                        p += sizeof(int);
 
@@ -1871,7 +2145,7 @@ int processcluster(uint8_t *data, int size, in_addr_t addr)
                                processdae(p, s, &a, sizeof(a), &local);
                        }
                        else
                                processdae(p, s, &a, sizeof(a), &local);
                        }
                        else
-                               processudp(p, s, &a);
+                               processudp(p, s, &a, indexudp);
 
                        return 0;
                }
 
                        return 0;
                }
@@ -1962,7 +2236,7 @@ shortpacket:
 
 //====================================================================================================
 
 
 //====================================================================================================
 
-int cmd_show_cluster(struct cli_def *cli, char *command, char **argv, int argc)
+int cmd_show_cluster(struct cli_def *cli, const char *command, char **argv, int argc)
 {
        int i;
 
 {
        int i;