add filtering
[l2tpns.git] / l2tpns.c
index 54b4667..a1f30a2 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.57 2004/11/27 05:19:53 bodea Exp $";
+char const *cvs_id_l2tpns = "$Id: l2tpns.c,v 1.59 2004/11/28 20:10:04 bodea Exp $";
 
 #include <arpa/inet.h>
 #include <assert.h>
@@ -764,6 +764,10 @@ static void processipout(u8 * buf, int len)
        t = session[s].tunnel;
        sp = &session[s];
 
+       // run access-list if any
+       if (session[s].filter_out && !ip_filter(buf, len, session[s].filter_out - 1))
+               return;
+
        if (sp->tbf_out)
        {
                // Are we throttling this session?
@@ -1002,6 +1006,44 @@ void throttle_session(sessionidt s, int rate_in, int rate_out)
        }
 }
 
+// add/remove filters from session (-1 = no change)
+void filter_session(sessionidt s, int filter_in, int filter_out)
+{
+       if (!session[s].tunnel)
+               return; // No-one home.
+
+       if (!*session[s].user)
+               return; // User not logged in
+
+       // paranoia
+       if (filter_in > MAXFILTER) filter_in = -1;
+       if (filter_out > MAXFILTER) filter_out = -1;
+       if (session[s].filter_in > MAXFILTER) session[s].filter_in = 0;
+       if (session[s].filter_out > MAXFILTER) session[s].filter_out = 0;
+
+       if (filter_in >= 0)
+       {
+               if (session[s].filter_in)
+                       ip_filters[session[s].filter_in - 1].used--;
+
+               if (filter_in > 0)
+                       ip_filters[filter_in - 1].used++;
+
+               session[s].filter_in = filter_in;
+       }
+
+       if (filter_out >= 0)
+       {
+               if (session[s].filter_out)
+                       ip_filters[session[s].filter_out - 1].used--;
+
+               if (filter_out > 0)
+                       ip_filters[filter_out - 1].used++;
+
+               session[s].filter_out = filter_out;
+       }
+}
+
 // start tidy shutdown of session
 void sessionshutdown(sessionidt s, char *reason)
 {
@@ -1081,6 +1123,10 @@ void sessionshutdown(sessionidt s, char *reason)
        if (!session[s].die)
                session[s].die = now() + 150; // Clean up in 15 seconds
 
+       // update filter refcounts
+       if (session[s].filter_in) ip_filters[session[s].filter_in - 1].used--;
+       if (session[s].filter_out) ip_filters[session[s].filter_out - 1].used--;
+
        cluster_send_session(s);
 }
 
@@ -2131,6 +2177,22 @@ static int regular_cleanups(void)
                                send++;
                        }
 
+                       if (a & CLI_SESS_NOFILTER)
+                       {
+                               LOG(2, 0, s, session[s].tunnel, "Un-filtering session by CLI\n");
+                               filter_session(s, 0, 0);
+                               send++;
+                       }
+                       else if (a & CLI_SESS_FILTER)
+                       {
+                               LOG(2, 0, s, session[s].tunnel, "Filtering session by CLI (in=%d, out=%d)\n",
+                                   cli_session_actions[s].filter_in,
+                                   cli_session_actions[s].filter_out);
+
+                               filter_session(s, cli_session_actions[s].filter_in, cli_session_actions[s].filter_out);
+                               send++;
+                       }
+
                        if (send)
                                cluster_send_session(s);
 
@@ -3729,6 +3791,31 @@ int load_session(sessionidt s, sessiont *new)
                }
        }
 
+       // check filters
+       if (new->filter_in && (new->filter_in > MAXFILTER || !ip_filters[new->filter_in - 1].name[0]))
+       {
+               LOG(2, session[s].ip, s, session[s].tunnel, "Dropping invalid input filter %d\n", (int) new->filter_in);
+               new->filter_in = 0;
+       }
+
+       if (new->filter_out && (new->filter_out > MAXFILTER || !ip_filters[new->filter_out - 1].name[0]))
+       {
+               LOG(2, session[s].ip, s, session[s].tunnel, "Dropping invalid output filter %d\n", (int) new->filter_out);
+               new->filter_out = 0;
+       }
+
+       if (new->filter_in != session[s].filter_in)
+       {
+               if (session[s].filter_in) ip_filters[session[s].filter_in - 1].used--;
+               if (new->filter_in)       ip_filters[new->filter_in - 1].used++;
+       }
+
+       if (new->filter_out != session[s].filter_out)
+       {
+               if (session[s].filter_out) ip_filters[session[s].filter_out - 1].used--;
+               if (new->filter_out)       ip_filters[new->filter_out - 1].used++;
+       }
+
        if (new->tunnel && s > config->cluster_highest_sessionid)       // Maintain this in the slave. It's used
                                        // for walking the sessions to forward byte counts to the master.
                config->cluster_highest_sessionid = s;
@@ -4297,3 +4384,107 @@ static int unhide_avp(u8 *avp, tunnelidt t, sessionidt s, u16 length)
        return hidden_length + 6;
 }
 
+static int ip_filter_port(ip_filter_portt *p, portt port)
+{
+       switch (p->op)
+       {
+       case FILTER_PORT_OP_EQ:    return port == p->port;
+       case FILTER_PORT_OP_NEQ:   return port != p->port;
+       case FILTER_PORT_OP_GT:    return port > p->port;
+       case FILTER_PORT_OP_LT:    return port < p->port;
+       case FILTER_PORT_OP_RANGE: return port >= p->port && port <= p->port2;
+       }
+
+       return 0;
+}
+
+static int ip_filter_flag(u8 op, u8 sflags, u8 cflags, u8 flags)
+{
+       switch (op)
+       {
+       /*
+        * NOTE: "match-any +A +B -C -D" is interpreted as "match if
+        * either A or B is set *and* C or D is clear".  While "or" is
+        * possibly more correct, the way "established" is currently
+        * implemented depends on this behaviour.
+        */
+       case FILTER_FLAG_OP_ANY:
+               return (flags & sflags) && !(flags & cflags);
+
+       case FILTER_FLAG_OP_ALL:
+               return (flags & sflags) == sflags && (~flags & cflags) == cflags;
+       }
+
+       return 0;
+}
+
+int ip_filter(u8 *buf, int len, u8 filter)
+{
+       u8 proto;
+       ipt src_ip;
+       ipt dst_ip;
+       portt src_port = 0;
+       portt dst_port = 0;
+       u8 flags = 0;
+       ip_filter_rulet *rule;
+
+       if (len < 20) // up to end of destination address
+               return 0;
+
+       if (*buf >> 4) // IPv4
+               return 0;
+
+       proto = buf[9];
+       src_ip = *(u32 *) (buf + 12);
+       dst_ip = *(u32 *) (buf + 16);
+
+       if (proto == IPPROTO_TCP || proto == IPPROTO_UDP)
+       {
+               int l = buf[0] & 0xf;
+               if (len < l + 4) // ports
+                       return 0;
+
+               src_port = ntohs(*(u16 *) (buf + l));
+               dst_port = ntohs(*(u16 *) (buf + l + 2));
+               if (proto == IPPROTO_TCP)
+               {
+                       if (len < l + 15) // flags
+                               return 0;
+
+                       flags = buf[l + 14] & 0x3f;
+               }
+       }
+
+       for (rule = ip_filters[filter].rules; rule->action; rule++)
+       {
+               if (proto && proto != rule->proto)
+                       continue;
+
+               if (rule->src_wild != INADDR_BROADCAST &&
+                   (src_ip & ~rule->src_wild) != (rule->src_ip & ~rule->src_wild))
+                       continue;
+
+               if (rule->dst_wild != INADDR_BROADCAST &&
+                   (dst_ip & ~rule->dst_wild) != (rule->dst_ip & ~rule->dst_wild))
+                       continue;
+
+               if (proto == IPPROTO_TCP || proto == IPPROTO_UDP)
+               {
+                       if (rule->src_ports.op && !ip_filter_port(&rule->src_ports, src_port))
+                               continue;
+
+                       if (rule->dst_ports.op && !ip_filter_port(&rule->dst_ports, dst_port))
+                               continue;
+
+                       if (proto == IPPROTO_TCP && rule->tcp_flag_op &&
+                           !ip_filter_flag(rule->tcp_flag_op, rule->tcp_sflags, rule->tcp_cflags, flags))
+                               continue;
+               }
+
+               // matched
+               return rule->action == FILTER_ACTION_PERMIT;
+       }
+
+       // default deny
+       return 0;
+}