+static sessionidt sessionidtbysessiont(sessiont *s)
+{
+ sessionidt val = s-session;
+ if (s < session || val >= MAXSESSION) return 0;
+ return val;
+}
+
+// actually send a control message for a specific tunnel
+void tunnelsend(uint8_t * buf, uint16_t l, tunnelidt t)
+{
+ struct sockaddr_in addr;
+
+ CSTAT(tunnelsend);
+
+ if (!t)
+ {
+ LOG(0, 0, t, "tunnelsend called with 0 as tunnel id\n");
+ STAT(tunnel_tx_errors);
+ return;
+ }
+
+ if (t == TUNNEL_ID_PPPOE)
+ {
+ pppoe_sess_send(buf, l, t);
+ return;
+ }
+
+ if (!tunnel[t].ip)
+ {
+ LOG(1, 0, t, "Error sending data out tunnel: no remote endpoint (tunnel not set up)\n");
+ STAT(tunnel_tx_errors);
+ return;
+ }
+
+ memset(&addr, 0, sizeof(addr));
+ addr.sin_family = AF_INET;
+ *(uint32_t *) & addr.sin_addr = htonl(tunnel[t].ip);
+ addr.sin_port = htons(tunnel[t].port);
+
+ // sequence expected, if sequence in message
+ if (*buf & 0x08) *(uint16_t *) (buf + ((*buf & 0x40) ? 10 : 8)) = htons(tunnel[t].nr);
+
+ // If this is a control message, deal with retries
+ if (*buf & 0x80)
+ {
+ tunnel[t].last = time_now; // control message sent
+ tunnel[t].retry = backoff(tunnel[t].try); // when to resend
+ if (tunnel[t].try)
+ {
+ STAT(tunnel_retries);
+ LOG(3, 0, t, "Control message resend try %d\n", tunnel[t].try);
+ }
+ }
+
+ if (sendto(udpfd[tunnel[t].indexudp], buf, l, 0, (void *) &addr, sizeof(addr)) < 0)
+ {
+ LOG(0, ntohs((*(uint16_t *) (buf + 6))), t, "Error sending data out tunnel: %s (udpfd=%d, buf=%p, len=%d, dest=%s)\n",
+ strerror(errno), udpfd[tunnel[t].indexudp], buf, l, inet_ntoa(addr.sin_addr));
+ STAT(tunnel_tx_errors);
+ return;
+ }
+
+ LOG_HEX(5, "Send Tunnel Data", buf, l);
+ STAT(tunnel_tx_packets);
+ INC_STAT(tunnel_tx_bytes, l);
+}
+
+//
+// Tiny helper function to write data to
+// the 'tun' device.
+//
+int tun_write(uint8_t * data, int size)
+{
+ return write(tunfd, data, size);
+}
+
+// adjust tcp mss to avoid fragmentation (called only for tcp packets with syn set)
+void adjust_tcp_mss(sessionidt s, tunnelidt t, uint8_t *buf, int len, uint8_t *tcp)
+{
+ int d = (tcp[12] >> 4) * 4;
+ uint8_t *mss = 0;
+ uint8_t *opts;
+ uint8_t *data;
+ uint16_t orig;
+ uint32_t sum;
+
+ if ((tcp[13] & 0x3f) & ~(TCP_FLAG_SYN|TCP_FLAG_ACK)) // only want SYN and SYN,ACK
+ return;
+
+ if (tcp + d > buf + len) // short?
+ return;
+
+ opts = tcp + 20;
+ data = tcp + d;
+
+ while (opts < data)
+ {
+ if (*opts == 2 && opts[1] == 4) // mss option (2), length 4
+ {
+ mss = opts + 2;
+ if (mss + 2 > data) return; // short?
+ break;
+ }
+
+ if (*opts == 0) return; // end of options
+ if (*opts == 1 || !opts[1]) // no op (one byte), or no length (prevent loop)
+ opts++;
+ else
+ opts += opts[1]; // skip over option
+ }
+
+ if (!mss) return; // not found
+ orig = ntohs(*(uint16_t *) mss);
+
+ if (orig <= MSS) return; // mss OK
+
+ LOG(5, s, t, "TCP: %s:%u -> %s:%u SYN%s: adjusted mss from %u to %u\n",
+ fmtaddr(*(in_addr_t *) (buf + 12), 0), ntohs(*(uint16_t *) tcp),
+ fmtaddr(*(in_addr_t *) (buf + 16), 1), ntohs(*(uint16_t *) (tcp + 2)),
+ (tcp[13] & TCP_FLAG_ACK) ? ",ACK" : "", orig, MSS);
+
+ // set mss
+ *(int16_t *) mss = htons(MSS);
+
+ // adjust checksum (see rfc1141)
+ sum = orig + (~MSS & 0xffff);
+ sum += ntohs(*(uint16_t *) (tcp + 16));
+ sum = (sum & 0xffff) + (sum >> 16);
+ *(uint16_t *) (tcp + 16) = htons(sum + (sum >> 16));
+}
+
+void processmpframe(sessionidt s, tunnelidt t, uint8_t *p, uint16_t l, uint8_t extra)
+{
+ uint16_t proto;
+ if (extra) {
+ // Skip the four extra bytes
+ p += 4;
+ l -= 4;
+ }
+
+ if (*p & 1)
+ {
+ proto = *p++;
+ l--;
+ }
+ else
+ {
+ proto = ntohs(*(uint16_t *) p);
+ p += 2;
+ l -= 2;
+ }
+ if (proto == PPPIP)
+ {
+ if (session[s].die)
+ {
+ LOG(4, s, t, "MPPP: Session %d is closing. Don't process PPP packets\n", s);
+ return; // closing session, PPP not processed
+ }
+ session[s].last_packet = session[s].last_data = time_now;
+ processipin(s, t, p, l);
+ }
+ else if (proto == PPPIPV6 && config->ipv6_prefix.s6_addr[0])
+ {
+ if (session[s].die)
+ {
+ LOG(4, s, t, "MPPP: Session %d is closing. Don't process PPP packets\n", s);
+ return; // closing session, PPP not processed
+ }
+
+ session[s].last_packet = session[s].last_data = time_now;
+ processipv6in(s, t, p, l);
+ }
+ else if (proto == PPPIPCP)
+ {
+ session[s].last_packet = session[s].last_data = time_now;
+ processipcp(s, t, p, l);
+ }
+ else if (proto == PPPCCP)
+ {
+ session[s].last_packet = session[s].last_data = time_now;
+ processccp(s, t, p, l);
+ }
+ else
+ {
+ LOG(2, s, t, "MPPP: Unsupported MP protocol 0x%04X received\n",proto);
+ }
+}
+
+static void update_session_out_stat(sessionidt s, sessiont *sp, int len)
+{
+ increment_counter(&sp->cout, &sp->cout_wrap, len); // byte count
+ sp->cout_delta += len;
+ sp->pout++;
+ sp->last_data = time_now;
+
+ sess_local[s].cout += len; // To send to master..
+ sess_local[s].pout++;
+}
+
+// process outgoing (to tunnel) IP
+//
+// (i.e. this routine writes to data[-8]).
+void processipout(uint8_t *buf, int len)
+{
+ sessionidt s;
+ groupidt g;
+ sessiont *sp;
+ tunnelidt t;
+ in_addr_t ip;
+
+ uint8_t *data = buf; // Keep a copy of the originals.
+ int size = len;
+
+ uint8_t fragbuf[MAXETHER + 20];
+
+ CSTAT(processipout);
+
+ if (len < MIN_IP_SIZE)
+ {
+ LOG(1, 0, 0, "Short IP, %d bytes\n", len);
+ STAT(tun_rx_errors);
+ return;
+ }
+ if (len >= MAXETHER)
+ {
+ LOG(1, 0, 0, "Oversize IP packet %d bytes\n", len);
+ STAT(tun_rx_errors);
+ return;
+ }
+
+ // Skip the tun header
+ buf += 4;
+ len -= 4;
+
+ // Got an IP header now
+ if (*(uint8_t *)(buf) >> 4 != 4)
+ {
+ LOG(1, 0, 0, "IP: Don't understand anything except IPv4\n");
+ return;
+ }
+
+ ip = *(uint32_t *)(buf + 16);
+ if ((g = grp_groupbyip(ip)))
+ {
+ s = grp_getnextsession(g, ip);
+ if (!s)
+ {
+ // Is this a packet for a session that doesn't exist?
+ static int rate = 0; // Number of ICMP packets we've sent this second.
+ static int last = 0; // Last time we reset the ICMP packet counter 'rate'.
+
+ if (last != time_now)
+ {
+ last = time_now;
+ rate = 0;
+ }
+
+ if (rate++ < config->icmp_rate) // Only send a max of icmp_rate per second.
+ {
+ LOG(4, 0, 0, "IP: Sending ICMP host unreachable to %s\n", fmtaddr(*(in_addr_t *)(buf + 12), 0));
+ host_unreachable(*(in_addr_t *)(buf + 12), *(uint16_t *)(buf + 4),
+ config->bind_address ? config->bind_address : my_address, buf, len);
+ }
+ return;
+ }
+ }
+ else if (!(s = sessionbyip(ip)))
+ {
+ // Is this a packet for a session that doesn't exist?
+ static int rate = 0; // Number of ICMP packets we've sent this second.
+ static int last = 0; // Last time we reset the ICMP packet counter 'rate'.
+
+ if (last != time_now)
+ {
+ last = time_now;
+ rate = 0;
+ }
+
+ if (rate++ < config->icmp_rate) // Only send a max of icmp_rate per second.
+ {
+ LOG(4, 0, 0, "IP: Sending ICMP host unreachable to %s\n", fmtaddr(*(in_addr_t *)(buf + 12), 0));
+ host_unreachable(*(in_addr_t *)(buf + 12), *(uint16_t *)(buf + 4),
+ config->bind_address ? config->bind_address : my_address, buf, len);
+ }
+ return;
+ }
+
+ t = session[s].tunnel;
+ if (len > session[s].mru || (session[s].mrru && len > session[s].mrru))
+ {
+ LOG(3, s, t, "Packet size more than session MRU\n");
+ return;
+ }
+
+ sp = &session[s];