+
+/* Unhide an avp.
+ *
+ * This unencodes the AVP using the L2TP secret and the previously
+ * stored random vector. It overwrites the hidden data with the
+ * unhidden AVP subformat.
+ */
+static void unhide_value(uint8_t *value, size_t len, uint16_t type, uint8_t *vector, size_t vec_len)
+{
+ MD5_CTX ctx;
+ uint8_t digest[16];
+ uint8_t *last;
+ size_t d = 0;
+ uint16_t m = htons(type);
+
+ // Compute initial pad
+ MD5_Init(&ctx);
+ MD5_Update(&ctx, (unsigned char *) &m, 2);
+ MD5_Update(&ctx, config->l2tp_secret, strlen(config->l2tp_secret));
+ MD5_Update(&ctx, vector, vec_len);
+ MD5_Final(digest, &ctx);
+
+ // pointer to last decoded 16 octets
+ last = value;
+
+ while (len > 0)
+ {
+ // calculate a new pad based on the last decoded block
+ if (d >= sizeof(digest))
+ {
+ MD5_Init(&ctx);
+ MD5_Update(&ctx, config->l2tp_secret, strlen(config->l2tp_secret));
+ MD5_Update(&ctx, last, sizeof(digest));
+ MD5_Final(digest, &ctx);
+
+ d = 0;
+ last = value;
+ }
+
+ *value++ ^= digest[d++];
+ len--;
+ }
+}
+
+int find_filter(char const *name, size_t len)
+{
+ int free = -1;
+ int i;
+
+ for (i = 0; i < MAXFILTER; i++)
+ {
+ if (!*ip_filters[i].name)
+ {
+ if (free < 0)
+ free = i;
+
+ continue;
+ }
+
+ if (strlen(ip_filters[i].name) != len)
+ continue;
+
+ if (!strncmp(ip_filters[i].name, name, len))
+ return i;
+ }
+
+ return free;
+}
+
+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)
+ {
+ // layer 4 deny rules are skipped
+ if (rule->action == FILTER_ACTION_DENY &&
+ (rule->src_ports.op || rule->dst_ports.op || rule->tcp_flag_op))
+ 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;
+}
+
+tunnelidt lac_new_tunnel()
+{
+ return new_tunnel();
+}
+
+void lac_tunnelclear(tunnelidt t)
+{
+ tunnelclear(t);
+}
+
+void lac_send_SCCRQ(tunnelidt t, uint8_t * auth, unsigned int auth_len)
+{
+ uint16_t version = 0x0100; // protocol version
+
+ tunnel[t].state = TUNNELOPENING;
+
+ // Sent SCCRQ - Start Control Connection Request
+ controlt *c = controlnew(1); // sending SCCRQ
+ controls(c, 7, config->multi_n_hostname[tunnel[t].indexudp][0]?config->multi_n_hostname[tunnel[t].indexudp]:hostname, 1); // host name
+ controls(c, 8, Vendor_name, 1); // Vendor name
+ control16(c, 2, version, 1); // protocol version
+ control32(c, 3, 3, 1); // framing Capabilities
+ control16(c, 9, t, 1); // assigned tunnel
+ controlb(c, 11, (uint8_t *) auth, auth_len, 1); // CHAP Challenge
+ LOG(3, 0, t, "Sent SCCRQ to REMOTE LNS\n");
+ controladd(c, 0, t); // send
+}
+
+void lac_send_ICRQ(tunnelidt t, sessionidt s)
+{
+ // Sent ICRQ Incoming-call-request
+ controlt *c = controlnew(10); // ICRQ
+
+ control16(c, 14, s, 1); // assigned sesion
+ call_serial_number++;
+ control32(c, 15, call_serial_number, 1); // call serial number
+ LOG(3, s, t, "Sent ICRQ to REMOTE LNS (far ID %u)\n", tunnel[t].far);
+ controladd(c, 0, t); // send
+}
+
+void lac_tunnelshutdown(tunnelidt t, char *reason, int result, int error, char *msg)
+{
+ tunnelshutdown(t, reason, result, error, msg);
+}
+