+
+/* Unhide an avp.
+ *
+ * This unencodes the AVP using the L2TP CHAP secret and the
+ * previously stored random vector. It replaces the hidden data with
+ * the cleartext data and returns the length of the cleartext data
+ * (including the AVP "header" of 6 bytes).
+ *
+ * Based on code from rp-l2tpd by Roaring Penguin Software Inc.
+ */
+static int unhide_avp(uint8_t *avp, tunnelidt t, sessionidt s, uint16_t length)
+{
+ MD5_CTX ctx;
+ uint8_t *cursor;
+ uint8_t digest[16];
+ uint8_t working_vector[16];
+ uint16_t hidden_length;
+ uint8_t type[2];
+ size_t done, todo;
+ uint8_t *output;
+
+ // Find the AVP type.
+ type[0] = *(avp + 4);
+ type[1] = *(avp + 5);
+
+ // Line up with the hidden data
+ cursor = output = avp + 6;
+
+ // Compute initial pad
+ MD5Init(&ctx);
+ MD5Update(&ctx, type, 2);
+ MD5Update(&ctx, config->l2tpsecret, strlen(config->l2tpsecret));
+ MD5Update(&ctx, session[s].random_vector, session[s].random_vector_length);
+ MD5Final(digest, &ctx);
+
+ // Get hidden length
+ hidden_length = ((uint16_t) (digest[0] ^ cursor[0])) * 256 + (uint16_t) (digest[1] ^ cursor[1]);
+
+ // Keep these for later use
+ working_vector[0] = *cursor;
+ working_vector[1] = *(cursor + 1);
+ cursor += 2;
+
+ if (hidden_length > length - 8)
+ {
+ LOG(1, s, t, "Hidden length %d too long in AVP of length %d\n", (int) hidden_length, (int) length);
+ return 0;
+ }
+
+ /* Decrypt remainder */
+ done = 2;
+ todo = hidden_length;
+ while (todo)
+ {
+ working_vector[done] = *cursor;
+ *output = digest[done] ^ *cursor;
+ ++output;
+ ++cursor;
+ --todo;
+ ++done;
+ if (done == 16 && todo)
+ {
+ // Compute new digest
+ done = 0;
+ MD5Init(&ctx);
+ MD5Update(&ctx, config->l2tpsecret, strlen(config->l2tpsecret));
+ MD5Update(&ctx, &working_vector, 16);
+ MD5Final(digest, &ctx);
+ }
+ }
+
+ return hidden_length + 6;
+}
+
+static int ip_filter_port(ip_filter_portt *p, uint16_t 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(uint8_t op, uint8_t sflags, uint8_t cflags, uint8_t flags)
+{
+ switch (op)
+ {
+ case FILTER_FLAG_OP_ANY:
+ return (flags & sflags) || (~flags & cflags);
+
+ case FILTER_FLAG_OP_ALL:
+ return (flags & sflags) == sflags && (~flags & cflags) == cflags;
+
+ case FILTER_FLAG_OP_EST:
+ return (flags & (TCP_FLAG_ACK|TCP_FLAG_RST)) && (~flags & TCP_FLAG_SYN);
+ }
+
+ return 0;
+}
+
+int ip_filter(uint8_t *buf, int len, uint8_t filter)
+{
+ uint16_t frag_offset;
+ uint8_t proto;
+ in_addr_t src_ip;
+ in_addr_t dst_ip;
+ uint16_t src_port = 0;
+ uint16_t dst_port = 0;
+ uint8_t flags = 0;
+ ip_filter_rulet *rule;
+
+ if (len < 20) // up to end of destination address
+ return 0;
+
+ if ((*buf >> 4) != 4) // IPv4
+ return 0;
+
+ frag_offset = ntohs(*(uint16_t *) (buf + 6)) & 0x1fff;
+ proto = buf[9];
+ src_ip = *(in_addr_t *) (buf + 12);
+ dst_ip = *(in_addr_t *) (buf + 16);
+
+ if (frag_offset == 0 && (proto == IPPROTO_TCP || proto == IPPROTO_UDP))
+ {
+ int l = (buf[0] & 0xf) * 4; // length of IP header
+ if (len < l + 4) // ports
+ return 0;
+
+ src_port = ntohs(*(uint16_t *) (buf + l));
+ dst_port = ntohs(*(uint16_t *) (buf + l + 2));
+ if (proto == IPPROTO_TCP)
+ {
+ if (len < l + 14) // flags
+ return 0;
+
+ flags = buf[l + 13] & 0x3f;
+ }
+ }
+
+ for (rule = ip_filters[filter].rules; rule->action; rule++)
+ {
+ if (rule->proto != IPPROTO_IP && 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 (frag_offset)
+ {
+ if (!rule->frag || rule->action == FILTER_ACTION_DENY)
+ continue;
+ }
+ else
+ {
+ if (rule->frag)
+ 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
+ rule->counter++;
+ return rule->action == FILTER_ACTION_PERMIT;
+ }
+
+ // default deny
+ return 0;
+}