+ change_state(s, ipv6cp, Closed);
+ }
+ else
+ {
+ LOG(3, s, t, "LCP protocol reject: 0x%04X\n", proto);
+ }
+ }
+ else if (*p == EchoReq)
+ {
+ *p = EchoReply; // reply
+ *(uint32_t *) (p + 4) = htonl(session[s].magic); // our magic number
+ q = makeppp(b, sizeof(b), p, l, s, t, PPPLCP);
+ if (!q) return;
+
+ LOG(4, s, t, "LCP: send %s\n", ppp_code(*q));
+ if (config->debug > 3) dumplcp(q, l);
+
+ tunnelsend(b, l + (q - b), t); // send it
+ }
+ else if (*p == EchoReply)
+ {
+ // Ignore it, last_packet time is set earlier than this.
+ }
+ else if (*p != CodeRej)
+ {
+ int code = *p;
+ int mru = session[s].mru;
+ if (!mru)
+ mru = DEFAULT_MRU;
+
+ if (l > mru) l = mru;
+
+ *p = CodeRej;
+ q = makeppp(b, sizeof(b), p, l, s, t, PPPLCP);
+ if (!q) return;
+
+ LOG(2, s, t, "Unexpected LCP code %s\n", ppp_code(code));
+ LOG(3, s, t, "LCP: send %s\n", ppp_code(*q));
+ if (config->debug > 3) dumplcp(q, l);
+
+ tunnelsend(b, l + (q - b), t);
+ }
+}
+
+static void ipcp_open(sessionidt s, tunnelidt t)
+{
+ LOG(3, s, t, "IPCP: Opened, session is now active\n");
+
+ change_state(s, ipcp, Opened);
+
+ if (!session[s].walled_garden)
+ {
+ uint16_t r = radiusnew(s);
+ if (r)
+ radiussend(r, RADIUSSTART); // send radius start
+ }
+
+ // start IPv6 if configured and still in passive state
+ if (session[s].ppp.ipv6cp == Stopped)
+ {
+ sendipv6cp(s, t);
+ change_state(s, ipv6cp, RequestSent);
+ }
+}
+
+// Process IPCP messages
+void processipcp(sessionidt s, tunnelidt t, uint8_t *p, uint16_t l)
+{
+ uint8_t b[MAXETHER];
+ uint8_t *q = 0;
+ uint16_t hl;
+
+ CSTAT(processipcp);
+
+ LOG_HEX(5, "IPCP", p, l);
+ if (l < 5)
+ {
+ LOG(1, s, t, "Short IPCP %d bytes\n", l);
+ STAT(tunnel_rx_errors);
+ return ;
+ }
+
+ if ((hl = ntohs(*(uint16_t *) (p + 2))) > l)
+ {
+ LOG(1, s, t, "Length mismatch IPCP %u/%u\n", hl, l);
+ STAT(tunnel_rx_errors);
+ return ;
+ }
+ l = hl;
+
+ if (session[s].ppp.phase < Network)
+ {
+ LOG(2, s, t, "IPCP %s ignored in %s phase\n", ppp_code(*p), ppp_phase(session[s].ppp.phase));
+ return;
+ }
+
+ LOG(3, s, t, "IPCP: recv %s\n", ppp_code(*p));
+
+ if (*p == ConfigAck)
+ {
+ switch (session[s].ppp.ipcp)
+ {
+ case RequestSent:
+ initialise_restart_count(s, ipcp);
+ change_state(s, ipcp, AckReceived);
+ break;
+
+ case AckReceived:
+ case Opened:
+ LOG(2, s, t, "IPCP: ConfigAck in state %s? Sending ConfigReq\n", ppp_state(session[s].ppp.ipcp));
+ sendipcp(s, t);
+ change_state(s, ipcp, RequestSent);
+ break;
+
+ case AckSent:
+ ipcp_open(s, t);
+ break;
+
+ default:
+ LOG(2, s, t, "IPCP: ignoring %s in state %s\n", ppp_code(*p), ppp_state(session[s].ppp.ipcp));
+ }
+ }
+ else if (*p == ConfigReq)
+ {
+ uint8_t *response = 0;
+ uint8_t *o = p + 4;
+ int length = l - 4;
+ int gotip = 0;
+ in_addr_t addr;
+
+ while (length > 2)
+ {
+ switch (*o)
+ {
+ case 3: // ip address
+ gotip++; // seen address
+ if (o[1] != 6 || o[1] > length) return;
+
+ addr = htonl(session[s].ip);
+ if (memcmp(o + 2, &addr, (sizeof addr)))
+ {
+ uint8_t *oq = q;
+ q = ppp_nak(s, b, sizeof(b), PPPIPCP, &response, q, p, o, (uint8_t *) &addr, sizeof(addr));
+ if (!q || (q != oq && *response == ConfigRej))
+ {
+ sessionshutdown(s, "Can't negotiate IPCP.", 3, 0);
+ return;
+ }
+ }
+
+ break;
+
+ case 129: // primary DNS
+ if (o[1] != 6 || o[1] > length) return;
+
+ addr = htonl(session[s].dns1);
+ if (memcmp(o + 2, &addr, (sizeof addr)))
+ {
+ q = ppp_nak(s, b, sizeof(b), PPPIPCP, &response, q, p, o, (uint8_t *) &addr, sizeof(addr));
+ if (!q) return;
+ }
+
+ break;
+
+ case 131: // secondary DNS
+ if (o[1] != 6 || o[1] > length) return;
+
+ addr = htonl(session[s].dns1);
+ if (memcmp(o + 2, &addr, sizeof(addr)))
+ {
+ q = ppp_nak(s, b, sizeof(b), PPPIPCP, &response, q, p, o, (uint8_t *) &addr, sizeof(addr));
+ if (!q) return;
+ }
+
+ break;
+
+ default:
+ LOG(2, s, t, " Rejecting PPP IPCP Option type %d\n", *o);
+ q = ppp_rej(s, b, sizeof(b), PPPIPCP, &response, q, p, o);
+ if (!q) return;
+ }
+
+ length -= o[1];
+ o += o[1];
+ }
+
+ if (response)
+ {
+ l = q - response; // IPCP 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, PPPIPCP);
+ if (!response) return;
+ *response = ConfigAck;