+ if (session[s].ppp.phase < Network)
+ {
+ LOG(2, s, t, "IPV6CP %s ignored in %s phase\n", ppp_code(*p), ppp_phase(session[s].ppp.phase));
+ return;
+ }
+
+ LOG(3, s, t, "IPV6CP: recv %s\n", ppp_code(*p));
+
+ if (!session[s].ip)
+ {
+ LOG(3, s, t, "IPV6CP: no IPv4 address (IPCP in state %s)\n", ppp_state(session[s].ppp.ipcp));
+ return; // need IPCP to complete...
+ }
+
+ if (*p == ConfigAck)
+ {
+ switch (session[s].ppp.ipv6cp)
+ {
+ case RequestSent:
+ initialise_restart_count(s, ipv6cp);
+ change_state(s, ipv6cp, AckReceived);
+ break;
+
+ case AckReceived:
+ case Opened:
+ LOG(2, s, t, "IPV6CP: ConfigAck in state %s? Sending ConfigReq\n", ppp_state(session[s].ppp.ipv6cp));
+ sendipv6cp(s, t);
+ change_state(s, ipv6cp, RequestSent);
+ break;
+
+ case AckSent:
+ ipv6cp_open(s, t);
+ break;
+
+ default:
+ LOG(2, s, t, "IPV6CP: ignoring %s in state %s\n", ppp_code(*p), ppp_state(session[s].ppp.ipv6cp));
+ }
+ }
+ else if (*p == ConfigReq)
+ {
+ uint8_t *response = 0;
+ uint8_t *o = p + 4;
+ int length = l - 4;
+ int gotip = 0;
+ uint32_t ident[2];
+
+ while (length > 2)
+ {
+ if (!o[1] || o[1] > length) return;
+
+ switch (*o)
+ {
+ case 1: // interface identifier
+ gotip++; // seen address
+ if (o[1] != 10) return;
+
+ ident[0] = htonl(session[s].ip);
+ ident[1] = 0;
+
+ if (memcmp(o + 2, ident, sizeof(ident)))
+ {
+ q = ppp_conf_nak(s, b, sizeof(b), PPPIPV6CP, &response, q, p, o, (uint8_t *)ident, sizeof(ident));
+ if (!q) return;
+ }
+
+ break;
+
+ default:
+ LOG(2, s, t, " Rejecting PPP IPV6CP Option type %d\n", *o);
+ q = ppp_conf_rej(s, b, sizeof(b), PPPIPV6CP, &response, q, p, o);
+ if (!q) return;
+ }
+
+ length -= o[1];
+ o += o[1];
+ }
+
+ if (response)
+ {
+ l = q - response; // IPV6CP packet length
+ *((uint16_t *) (response + 2)) = htons(l); // update header
+ }
+ else if (gotip)
+ {
+ // Send packet back as ConfigAck
+ response = makeppp(b, sizeof(b), p, l, s, t, PPPIPV6CP, 0, 0, 0);
+ if (!response) return;
+ *response = ConfigAck;
+ }
+ else
+ {
+ LOG(1, s, t, "No interface identifier in IPV6CP request\n");
+ STAT(tunnel_rx_errors);
+ return;
+ }
+
+ switch (session[s].ppp.ipv6cp)
+ {
+ case Closed:
+ response = makeppp(b, sizeof(b), p, 2, s, t, PPPIPV6CP, 0, 0, 0);
+ if (!response) return;
+ *response = TerminateAck;
+ *((uint16_t *) (response + 2)) = htons(l = 4);
+ break;
+
+ case Stopped:
+ initialise_restart_count(s, ipv6cp);
+ sendipv6cp(s, t);
+ if (*response == ConfigAck)
+ change_state(s, ipv6cp, AckSent);
+ else
+ change_state(s, ipv6cp, RequestSent);
+
+ break;
+
+ case RequestSent:
+ if (*response == ConfigAck)
+ change_state(s, ipv6cp, AckSent);
+
+ break;
+
+ case AckReceived:
+ if (*response == ConfigAck)
+ ipv6cp_open(s, t);
+
+ break;
+
+ case Opened:
+ initialise_restart_count(s, ipv6cp);
+ sendipv6cp(s, t);
+ /* fallthrough */
+
+ case AckSent:
+ if (*response == ConfigAck)
+ change_state(s, ipv6cp, AckSent);
+ else
+ change_state(s, ipv6cp, RequestSent);
+
+ break;
+
+ default:
+ LOG(2, s, t, "IPV6CP: ignoring %s in state %s\n", ppp_code(*p), ppp_state(session[s].ppp.ipv6cp));
+ return;
+ }
+
+ LOG(3, s, t, "IPV6CP: send %s\n", ppp_code(*response));
+ tunnelsend(b, l + (response - b), t);
+ }
+ else if (*p == TerminateReq)
+ {
+ switch (session[s].ppp.ipv6cp)
+ {
+ case Closed:
+ case Stopped:
+ case Closing:
+ case Stopping:
+ case RequestSent:
+ case AckReceived:
+ case AckSent:
+ break;
+
+ case Opened:
+ zero_restart_count(s, ipv6cp);
+ change_state(s, ipv6cp, Closing);
+ break;
+
+ default:
+ LOG(2, s, t, "IPV6CP: ignoring %s in state %s\n", ppp_code(*p), ppp_state(session[s].ppp.ipv6cp));
+ return;
+ }
+
+ *p = TerminateAck; // send ack
+ q = makeppp(b, sizeof(b), p, l, s, t, PPPIPV6CP, 0, 0, 0);
+ if (!q) return;
+
+ LOG(3, s, t, "IPV6CP: send %s\n", ppp_code(*q));
+ tunnelsend(b, l + (q - b), t); // send it
+ }
+ else if (*p != CodeRej)
+ {
+ ppp_code_rej(s, t, PPPIPV6CP, "IPV6CP", p, l, b, sizeof(b));
+ }
+}
+
+static void update_sessions_in_stat(sessionidt s, uint16_t l)
+{
+ bundleidt b = session[s].bundle;
+ if (!b)
+ {
+ increment_counter(&session[s].cin, &session[s].cin_wrap, l);
+ session[s].cin_delta += l;
+ session[s].pin++;
+
+ sess_local[s].cin += l;
+ sess_local[s].pin++;
+ }
+ else
+ {
+ int i = frag[b].re_frame_begin_index;
+ int end = frag[b].re_frame_end_index;
+ for (;;)
+ {
+ l = frag[b].fragment[i].length;
+ s = frag[b].fragment[i].sid;
+ increment_counter(&session[s].cin, &session[s].cin_wrap, l);
+ session[s].cin_delta += l;
+ session[s].pin++;
+
+ sess_local[s].cin += l;
+ sess_local[s].pin++;
+ if (i == end)
+ return;
+ i = (i + 1) & MAXFRAGNUM_MASK;
+ }
+ }
+}
+
+// process IP packet received
+//
+// This MUST be called with at least 4 byte behind 'p'.
+// (i.e. this routine writes to p[-4]).
+void processipin(sessionidt s, tunnelidt t, uint8_t *p, uint16_t l)
+{
+ in_addr_t ip;
+
+ CSTAT(processipin);
+
+ LOG_HEX(5, "IP", p, l);
+
+ if (l < 20)
+ {
+ LOG(1, s, t, "IP packet too short %d\n", l);
+ STAT(tunnel_rx_errors);
+ return ;
+ }
+
+ ip = ntohl(*(uint32_t *)(p + 12));
+
+ if (l > MAXETHER)
+ {
+ LOG(1, s, t, "IP packet too long %d\n", l);
+ STAT(tunnel_rx_errors);
+ return ;
+ }
+
+ if (session[s].ppp.phase != Network || session[s].ppp.ipcp != Opened)
+ return;
+
+ if (!session[s].bundle || bundle[session[s].bundle].num_of_links < 2) // FIXME:
+ {
+ // no spoof (do sessionbyip to handled statically routed subnets)
+ if (!config->disable_no_spoof && ip != session[s].ip && sessionbyip(htonl(ip)) != s)
+ {
+ LOG(4, s, t, "Dropping packet with spoofed IP %s\n", fmtaddr(htonl(ip), 0));
+ return;
+ }
+ }
+
+ // run access-list if any
+ if (session[s].filter_in && !ip_filter(p, l, session[s].filter_in - 1))
+ return;
+
+ // adjust MSS on SYN and SYN,ACK packets with options
+ if ((ntohs(*(uint16_t *) (p + 6)) & 0x1fff) == 0 && p[9] == IPPROTO_TCP) // first tcp fragment
+ {
+ int ihl = (p[0] & 0xf) * 4; // length of IP header
+ if (l >= ihl + 20 && (p[ihl + 13] & TCP_FLAG_SYN) && ((p[ihl + 12] >> 4) > 5))
+ adjust_tcp_mss(s, t, p, l, p + ihl);
+ }
+
+ // Add on the tun header
+ p -= 4;
+ *(uint32_t *) p = htonl(PKTIP);
+ l += 4;
+
+ if (session[s].tbf_in)
+ {
+ // Are we throttling this session?
+ if (config->cluster_iam_master)
+ tbf_queue_packet(session[s].tbf_in, p, l);
+ else
+ master_throttle_packet(session[s].tbf_in, p, l);
+ return;
+ }
+
+ // send to ethernet
+ if (tun_write(p, l) < 0)
+ {
+ STAT(tun_tx_errors);
+ LOG(0, s, t, "Error writing %d bytes to TUN device: %s (tunfd=%d, p=%p)\n",
+ l, strerror(errno), tunfd, p);
+
+ return;
+ }
+
+ p += 4;
+ l -= 4;
+
+ if (session[s].snoop_ip && session[s].snoop_port)
+ {
+ // Snooping this session
+ snoop_send_packet(p, l, session[s].snoop_ip, session[s].snoop_port);
+ }
+
+ update_sessions_in_stat(s, l);
+
+ eth_tx += l;
+
+ STAT(tun_tx_packets);
+ INC_STAT(tun_tx_bytes, l);
+}
+
+// process Multilink PPP packet received
+void processmpin(sessionidt s, tunnelidt t, uint8_t *p, uint16_t l)
+{
+ bundleidt b = session[s].bundle;
+ bundlet * this_bundle = &bundle[b];
+ uint32_t maskSeq, max_seq;
+ int frag_offset;
+ uint16_t frag_index, frag_index_next, frag_index_prev;
+ fragmentationt *this_fragmentation = &frag[b];
+ uint8_t begin_frame = (*p & MP_BEGIN);
+ uint8_t end_frame = (*p & MP_END);
+ uint32_t seq_num, seq_num_next, seq_num_prev;
+ uint32_t i;
+ uint8_t flags = *p;
+ uint16_t begin_index, end_index;
+
+ // Perform length checking
+ if(l > MAXFRAGLEN)
+ {
+ LOG(2, s, t, "MPPP: discarding fragment larger than MAXFRAGLEN\n");
+ return;
+ }
+
+ if(!b)
+ {
+ LOG(2, s, t, "MPPP: Invalid bundle id: 0\n");
+ return;
+ }
+ // FIXME !! session[s].mssf means that the receiver wants to receive frames in mssf not means the receiver will send frames in mssf
+ /* if(session[s].mssf)
+ {
+ // Get 12 bit for seq number
+ seq_num = ntohs((*(uint16_t *) p) & 0xFF0F);
+ p += 2;
+ l -= 2;
+ // After this point the pointer should be advanced 2 bytes
+ LOG(3, s, t, "MPPP: 12 bits, sequence number: %d\n",seq_num);
+ }
+ else */
+ {
+ // Get 24 bit for seq number
+ seq_num = ntohl((*(uint32_t *) p) & 0xFFFFFF00);
+ p += 4;
+ l -= 4;
+ // After this point the pointer should be advanced 4 bytes
+ LOG(4, s, t, "MPPP: 24 bits sequence number:%d\n",seq_num);
+ }
+
+ max_seq = this_bundle->max_seq;
+ maskSeq = max_seq - 1;
+
+ /*
+ * Expand sequence number to 32 bits, making it as close
+ * as possible to this_fragmentation->M.
+ */
+ seq_num |= this_fragmentation->M & ~maskSeq;
+ if ((int)(this_fragmentation->M - seq_num) > (int)(maskSeq >> 1))
+ {
+ seq_num += maskSeq + 1;
+ }
+ else if ((int)(seq_num - this_fragmentation->M) > (int)(maskSeq >> 1))
+ {
+ seq_num -= maskSeq + 1; /* should never happen */
+ }
+
+ // calculate this fragment's offset from the begin seq in the bundle
+ frag_offset = (int) (seq_num - this_fragmentation->start_seq);
+
+ sess_local[s].last_seq = seq_num;
+
+ // calculate the jitter average
+ uint32_t ljitter = time_now_ms - sess_local[s].prev_time;
+ if (ljitter > 0)
+ {
+ sess_local[s].jitteravg = (sess_local[s].jitteravg + ljitter)>>1;
+ sess_local[s].prev_time = time_now_ms;
+ }
+
+ uint32_t Mmin;
+
+ if (seq_num < this_fragmentation->M)
+ {
+ Mmin = seq_num;
+ this_fragmentation->M = seq_num;
+ }
+ else