+ else if (*p != CodeRej)
+ {
+ ppp_code_rej(s, t, PPPLCP, "LCP", p, l, b, sizeof(b));
+ }
+}
+
+int join_bundle(sessionidt s)
+{
+ // Search for a bundle to join
+ bundleidt i;
+ bundleidt b;
+ for (i = 1; i < MAXBUNDLE; i++)
+ {
+ if (bundle[i].state != BUNDLEFREE)
+ {
+ if (epdiscmp(session[s].epdis,bundle[i].epdis) && !strcmp(session[s].user, bundle[i].user))
+ {
+ session[s].bundle = i;
+ bundle[i].mrru = session[s].mrru;
+ bundle[i].mssf = session[s].mssf;
+ if (session[s].epdis.length > 0)
+ setepdis(&bundle[i].epdis, session[s].epdis);
+
+ strcpy(bundle[i].user, session[s].user);
+ bundle[i].members[bundle[i].num_of_links] = s;
+ bundle[i].num_of_links++;
+ LOG(3, s, session[s].tunnel, "MPPP: Bundling additional line in bundle (%d), lines:%d\n",i,bundle[i].num_of_links);
+ return i;
+ }
+ }
+ }
+
+ // No previously created bundle was found for this session, so create a new one
+ if (!(b = new_bundle())) return 0;
+
+ session[s].bundle = b;
+ bundle[b].mrru = session[s].mrru;
+ bundle[b].mssf = session[s].mssf;
+ if (session[s].epdis.length > 0)
+ setepdis(&bundle[b].epdis, session[s].epdis);
+
+ strcpy(bundle[b].user, session[s].user);
+ bundle[b].members[0] = s;
+ LOG(3, s, session[s].tunnel, "MPPP: Created a new bundle (%d)\n", b);
+ return b;
+}
+
+static int epdiscmp(epdist ep1, epdist ep2)
+{
+ int ad;
+ if (ep1.length != ep2.length)
+ return 0;
+
+ if (ep1.addr_class != ep2.addr_class)
+ return 0;
+
+ for (ad = 0; ad < ep1.length; ad++)
+ if (ep1.address[ad] != ep2.address[ad])
+ return 0;
+
+ return 1;
+}
+
+static void setepdis(epdist *ep1, epdist ep2)
+{
+ int ad;
+ ep1->length = ep2.length;
+ ep1->addr_class = ep2.addr_class;
+ for (ad = 0; ad < ep2.length; ad++)
+ ep1->address[ad] = ep2.address[ad];
+}
+
+static bundleidt new_bundle()
+{
+ bundleidt i;
+ for (i = 1; i < MAXBUNDLE; i++)
+ {
+ if (bundle[i].state == BUNDLEFREE)
+ {
+ LOG(4, 0, 0, "MPPP: Assigning bundle ID %d\n", i);
+ bundle[i].num_of_links = 1;
+ bundle[i].last_check = time_now; // Initialize last_check value
+ bundle[i].state = BUNDLEOPEN;
+ bundle[i].current_ses = -1; // This is to enforce the first session 0 to be used at first
+ if (i > config->cluster_highest_bundleid)
+ config->cluster_highest_bundleid = i;
+ return i;
+ }
+ }
+ LOG(0, 0, 0, "MPPP: Can't find a free bundle! There shouldn't be this many in use!\n");
+ return 0;
+}
+
+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 || session[s].flags & SESSION_STARTED))
+ {
+ uint16_t r = radiusnew(s);
+ if (r)
+ {
+ radiussend(r, RADIUSSTART); // send radius start
+
+ // don't send further Start records if IPCP is restarted
+ session[s].flags |= SESSION_STARTED;
+ cluster_send_session(s);
+ }
+ }
+
+ // 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 < 4)
+ {
+ 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)
+ {
+ if (!o[1] || o[1] > length) return;
+
+ switch (*o)
+ {
+ case 3: // ip address
+ gotip++; // seen address
+ if (o[1] != 6) return;
+
+ addr = htonl(session[s].ip);
+ if (memcmp(o + 2, &addr, (sizeof addr)))
+ {
+ uint8_t *oq = q;
+ q = ppp_conf_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.", CDN_ADMIN_DISC, TERM_USER_ERROR);
+ return;
+ }
+ }
+
+ break;
+
+ case 129: // primary DNS
+ if (o[1] != 6) return;
+
+ addr = htonl(session[s].dns1);
+ if (memcmp(o + 2, &addr, (sizeof addr)))
+ {
+ q = ppp_conf_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) return;
+
+ addr = htonl(session[s].dns2);
+ if (memcmp(o + 2, &addr, sizeof(addr)))
+ {
+ q = ppp_conf_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_conf_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, 0, 0, 0);
+ if (!response) return;
+ *response = ConfigAck;
+ }
+ else
+ {
+ LOG(1, s, t, "No IP in IPCP request\n");
+ STAT(tunnel_rx_errors);
+ return;
+ }
+
+ switch (session[s].ppp.ipcp)
+ {
+ case Closed:
+ response = makeppp(b, sizeof(b), p, 2, s, t, PPPIPCP, 0, 0, 0);
+ if (!response) return;
+ *response = TerminateAck;
+ *((uint16_t *) (response + 2)) = htons(l = 4);
+ break;
+
+ case Stopped:
+ initialise_restart_count(s, ipcp);
+ sendipcp(s, t);
+ if (*response == ConfigAck)
+ change_state(s, ipcp, AckSent);
+ else
+ change_state(s, ipcp, RequestSent);
+
+ break;
+
+ case RequestSent:
+ if (*response == ConfigAck)
+ change_state(s, ipcp, AckSent);
+
+ break;
+
+ case AckReceived:
+ if (*response == ConfigAck)
+ ipcp_open(s, t);
+
+ break;
+
+ case Opened:
+ initialise_restart_count(s, ipcp);
+ sendipcp(s, t);
+ /* fallthrough */
+
+ case AckSent:
+ if (*response == ConfigAck)
+ change_state(s, ipcp, AckSent);
+ else
+ change_state(s, ipcp, RequestSent);
+
+ break;
+
+ default:
+ LOG(2, s, t, "IPCP: ignoring %s in state %s\n", ppp_code(*p), ppp_state(session[s].ppp.ipcp));
+ return;
+ }
+
+ LOG(3, s, t, "IPCP: send %s\n", ppp_code(*response));
+ tunnelsend(b, l + (response - b), t);
+ }
+ else if (*p == TerminateReq)