CIRA fix.

Ylian Saint-Hilaire committed Oct 8, 2020 at 14:25 UTC 8fd7327aea73b9ba13dc05eb5ead2da68b9a58b2
4 files changed +47 -25
amt/amt-wsman-comm.js
+9 -3
@@ -348,7 +348,7 @@ var CreateWsmanComm = function (host, port, user, pass, tls, tlsoptions, transpo
348 if (isNaN(s)) s = 500;
349 if (s == 401 && ++(obj.authcounter) < 3) {
350 obj.challengeParams = obj.parseDigest(header['www-authenticate']); // Set the digest parameters, after this, the socket will close and we will auto-retry
351 - if (obj.transportServer == null) { obj.socket.end(); }
351 + if (obj.transportServer == null) { obj.socket.end(); } else { obj.socket.close(); }
352 } else {
353 var r = obj.pendingAjaxCall.shift();
354 if (r == null || r.length < 1) { console.log("pendingAjaxCall error, " + r); return; }
@@ -364,7 +364,10 @@ var CreateWsmanComm = function (host, port, user, pass, tls, tlsoptions, transpo
364 obj.xxOnSocketClosed = function () {
365 //obj.Debug("xxOnSocketClosed");
366 obj.socketState = 0;
367 - if ((obj.transportServer == null) && (obj.socket != null)) { obj.socket.destroy(); obj.socket = null; }
367 + if (obj.socket != null) {
368 + if (obj.transportServer == null) { obj.socket.destroy(); } else { obj.socket.close(); }
369 + obj.socket = null;
370 + }
371 if (obj.pendingAjaxCall.length > 0) {
372 var r = obj.pendingAjaxCall.shift(), retry = r[5];
373 setTimeout(function () { obj.PerformAjaxExNodeJS2(r[0], r[1], r[2], r[3], r[4], --retry) }, 500); // Wait half a second and try again
@@ -372,7 +375,10 @@ var CreateWsmanComm = function (host, port, user, pass, tls, tlsoptions, transpo
375 }
376
377 obj.xxOnSocketTimeout = function () {
375 - if ((obj.transportServer == null) && (obj.socket != null)) { obj.socket.destroy(); obj.socket = null; }
378 + if (obj.socket != null) {
379 + if (obj.transportServer == null) { obj.socket.destroy(); } else { obj.socket.close(); }
380 + obj.socket = null;
381 + }
382 }
383
384 // NODE.js specific private method
amtmanager.js
+3 -3
@@ -44,7 +44,7 @@ module.exports.CreateAmtManager = function(parent) {
44 // React to nodes connecting and disconnecting
45 if (event.action == 'nodeconnect') {
46 if ((event.conn & 14) != 0) { // connectType: Bitmask, 1 = MeshAgent, 2 = Intel AMT CIRA, 4 = Intel AMT local, 8 = Intel AMT Relay, 16 = MQTT
47 - if ((event.conn & 2) == 0) return // Debug: Only look at CIRA connections *****************************
47 + //if ((event.conn & 2) == 0) return // Debug: Only look at CIRA connections *****************************
48
49 // We have an OOB connection to Intel AMT, update our information
50 var dev = obj.amtDevices[event.nodeid];
@@ -265,7 +265,7 @@ module.exports.CreateAmtManager = function(parent) {
265 }
266
267 function attemptLocalConnectResponse(stack, name, responses, status) {
268 - console.log('attemptLocalConnectResponse', status);
268 + //console.log('attemptLocalConnectResponse', status);
269
270 // Release active connection to this host.
271 delete obj.activeLocalConnections[stack.wsman.comm.host];
@@ -285,7 +285,7 @@ module.exports.CreateAmtManager = function(parent) {
285 dev.aquired.user = stack.wsman.comm.user;
286 dev.aquired.pass = stack.wsman.comm.pass;
287 dev.aquired.lastContact = Date.now();
288 - dev.aquired.tls = stack.wsman.comm.xtls;
288 + if (dev.conntype == 1) { dev.aquired.tls = stack.wsman.comm.xtls; } // Only set the TLS state if on local mode. When using CIRA, this is auto-detected.
289 if (stack.wsman.comm.xtls == 1) { dev.aquired.hash = stack.wsman.comm.xtlsCertificate.fingerprint.split(':').join('').toLowerCase(); } else { delete dev.aquired.hash; }
290 //console.log(dev.nodeid, dev.name, dev.host, dev.aquired);
291 UpdateDevice(dev);
apfserver.js
+1
@@ -479,6 +479,7 @@ module.exports.CreateApfServer = function (parent, db, args) {
479 if (cirachannel.state > 0) {
480 cirachannel.state = 0;
481 if (cirachannel.onStateChange) { cirachannel.onStateChange(cirachannel, cirachannel.state); }
482 + SendChannelClose(cirachannel.socket, cirachannel.amtchannelid);
483 delete socket.tag.channels[RecipientChannel];
484 }
485 return 5;
mpsserver.js
+34 -19
@@ -236,7 +236,7 @@ module.exports.CreateMpsServer = function (parent, db, args, certificates) {
236 socket.on('timeout', () => { ciraTimeoutCount++; parent.debug('mps', "CIRA timeout, disconnecting."); try { socket.end(); } catch (e) { } });
237
238 socket.addListener('data', function (data) {
239 - if (args.mpsdebug) { var buf = Buffer.from(data, 'binary'); console.log("MPS <-- (" + buf.length + "):" + buf.toString('hex')); } // Print out received bytes
239 + if (args.mpsdebug) { var buf = Buffer.from(data, 'binary'); console.log("MPS --> (" + buf.length + "):" + buf.toString('hex')); } // Print out received bytes
240 socket.tag.accumulator += data;
241
242 // Detect if this is an HTTPS request, if it is, return a simple answer and disconnect. This is useful for debugging access to the MPS port.
@@ -393,13 +393,13 @@ module.exports.CreateMpsServer = function (parent, db, args, certificates) {
393 switch (cmd) {
394 case APFProtocol.KEEPALIVE_REQUEST: {
395 if (len < 5) return 0;
396 - parent.debug('mpscmd', 'KEEPALIVE_REQUEST');
396 + parent.debug('mpscmd', '--> KEEPALIVE_REQUEST');
397 SendKeepAliveReply(socket, common.ReadInt(data, 1));
398 return 5;
399 }
400 case APFProtocol.KEEPALIVE_REPLY: {
401 if (len < 5) return 0;
402 - parent.debug('mpscmd', 'KEEPALIVE_REPLY');
402 + parent.debug('mpscmd', '--> KEEPALIVE_REPLY');
403 return 5;
404 }
405 case APFProtocol.PROTOCOLVERSION: {
@@ -408,7 +408,7 @@ module.exports.CreateMpsServer = function (parent, db, args, certificates) {
408 socket.tag.MajorVersion = common.ReadInt(data, 1);
409 socket.tag.MinorVersion = common.ReadInt(data, 5);
410 socket.tag.SystemId = guidToStr(common.rstr2hex(data.substring(13, 29))).toLowerCase();
411 - parent.debug('mpscmd', 'PROTOCOLVERSION', socket.tag.MajorVersion, socket.tag.MinorVersion, socket.tag.SystemId);
411 + parent.debug('mpscmd', '--> PROTOCOLVERSION', socket.tag.MajorVersion, socket.tag.MinorVersion, socket.tag.SystemId);
412 return 93;
413 }
414 case APFProtocol.USERAUTH_REQUEST: {
@@ -426,7 +426,7 @@ module.exports.CreateMpsServer = function (parent, db, args, certificates) {
426 password = data.substring(18 + usernameLen + serviceNameLen + methodNameLen, 18 + usernameLen + serviceNameLen + methodNameLen + passwordLen);
427 }
428 //console.log('MPS:USERAUTH_REQUEST user=' + username + ', service=' + serviceName + ', method=' + methodName + ', password=' + password);
429 - parent.debug('mpscmd', 'USERAUTH_REQUEST user=' + username + ', service=' + serviceName + ', method=' + methodName + ', password=' + password);
429 + parent.debug('mpscmd', '--> USERAUTH_REQUEST user=' + username + ', service=' + serviceName + ', method=' + methodName + ', password=' + password);
430
431 // Check the CIRA password
432 if ((args.mpspass != null) && (password != args.mpspass)) { incorrectPasswordCount++; parent.debug('mps', 'Incorrect password', username, password); SendUserAuthFail(socket); return -1; }
@@ -553,7 +553,7 @@ module.exports.CreateMpsServer = function (parent, db, args, certificates) {
553 var xserviceNameLen = common.ReadInt(data, 1);
554 if (len < 5 + xserviceNameLen) return 0;
555 var xserviceName = data.substring(5, 5 + xserviceNameLen);
556 - parent.debug('mpscmd', 'SERVICE_REQUEST', xserviceName);
556 + parent.debug('mpscmd', '--> SERVICE_REQUEST', xserviceName);
557 if (xserviceName == "pfwd@amt.intel.com") { SendServiceAccept(socket, "pfwd@amt.intel.com"); }
558 if (xserviceName == "auth@amt.intel.com") { SendServiceAccept(socket, "auth@amt.intel.com"); }
559 return 5 + xserviceNameLen;
@@ -570,7 +570,7 @@ module.exports.CreateMpsServer = function (parent, db, args, certificates) {
570 if (len < 14 + requestLen + addrLen) return 0;
571 var addr = data.substring(10 + requestLen, 10 + requestLen + addrLen);
572 var port = common.ReadInt(data, 10 + requestLen + addrLen);
573 - parent.debug('mpscmd', 'GLOBAL_REQUEST', request, addr + ':' + port);
573 + parent.debug('mpscmd', '--> GLOBAL_REQUEST', request, addr + ':' + port);
574 ChangeHostname(socket, addr, socket.tag.SystemId);
575 if (socket.tag.boundPorts.indexOf(port) == -1) { socket.tag.boundPorts.push(port); }
576 SendTcpForwardSuccessReply(socket, port);
@@ -582,7 +582,7 @@ module.exports.CreateMpsServer = function (parent, db, args, certificates) {
582 if (len < 14 + requestLen + addrLen) return 0;
583 var addr = data.substring(10 + requestLen, 10 + requestLen + addrLen);
584 var port = common.ReadInt(data, 10 + requestLen + addrLen);
585 - parent.debug('mpscmd', 'GLOBAL_REQUEST', request, addr + ':' + port);
585 + parent.debug('mpscmd', '--> GLOBAL_REQUEST', request, addr + ':' + port);
586 var portindex = socket.tag.boundPorts.indexOf(port);
587 if (portindex >= 0) { socket.tag.boundPorts.splice(portindex, 1); }
588 SendTcpForwardCancelReply(socket);
@@ -600,7 +600,7 @@ module.exports.CreateMpsServer = function (parent, db, args, certificates) {
600 var oport = common.ReadInt(data, 18 + requestLen + addrLen + oaddrLen);
601 var datalen = common.ReadInt(data, 22 + requestLen + addrLen + oaddrLen);
602 if (len < 26 + requestLen + addrLen + oaddrLen + datalen) return 0;
603 - parent.debug('mpscmd', 'GLOBAL_REQUEST', request, addr + ':' + port, oaddr + ':' + oport, datalen);
603 + parent.debug('mpscmd', '--> GLOBAL_REQUEST', request, addr + ':' + port, oaddr + ':' + oport, datalen);
604 // TODO
605 return 26 + requestLen + addrLen + oaddrLen + datalen;
606 }
@@ -630,7 +630,7 @@ module.exports.CreateMpsServer = function (parent, db, args, certificates) {
630 var SourcePort = common.ReadInt(data, 29 + ChannelTypeLength + TargetLen + SourceLen);
631
632 channelOpenCount++;
633 - parent.debug('mpscmd', 'CHANNEL_OPEN', ChannelType, SenderChannel, WindowSize, Target + ':' + TargetPort, Source + ':' + SourcePort);
633 + parent.debug('mpscmd', '--> CHANNEL_OPEN', ChannelType, SenderChannel, WindowSize, Target + ':' + TargetPort, Source + ':' + SourcePort);
634
635 // Check if we understand this channel type
636 //if (ChannelType.toLowerCase() == "direct-tcpip")
@@ -661,7 +661,7 @@ module.exports.CreateMpsServer = function (parent, db, args, certificates) {
661 cirachannel.amtchannelid = SenderChannel;
662 cirachannel.sendcredits = cirachannel.amtCiraWindow = WindowSize;
663 channelOpenConfirmCount++;
664 - parent.debug('mpscmd', 'CHANNEL_OPEN_CONFIRMATION', RecipientChannel, SenderChannel, WindowSize);
664 + parent.debug('mpscmd', '--> CHANNEL_OPEN_CONFIRMATION', RecipientChannel, SenderChannel, WindowSize);
665 if (cirachannel.closing == 1) {
666 // Close this channel
667 SendChannelClose(cirachannel.socket, cirachannel.amtchannelid);
@@ -693,7 +693,7 @@ module.exports.CreateMpsServer = function (parent, db, args, certificates) {
693 var RecipientChannel = common.ReadInt(data, 1);
694 var ReasonCode = common.ReadInt(data, 5);
695 channelOpenFailCount++;
696 - parent.debug('mpscmd', 'CHANNEL_OPEN_FAILURE', RecipientChannel, ReasonCode);
696 + parent.debug('mpscmd', '--> CHANNEL_OPEN_FAILURE', RecipientChannel, ReasonCode);
697 var cirachannel = socket.tag.channels[RecipientChannel];
698 if (cirachannel == null) { console.log("MPS Error in CHANNEL_OPEN_FAILURE: Unable to find channelid " + RecipientChannel); return 17; }
699 if (cirachannel.state > 0) {
@@ -708,13 +708,14 @@ module.exports.CreateMpsServer = function (parent, db, args, certificates) {
708 if (len < 5) return 0;
709 var RecipientChannel = common.ReadInt(data, 1);
710 channelCloseCount++;
711 - parent.debug('mpscmd', 'CHANNEL_CLOSE', RecipientChannel);
711 + parent.debug('mpscmd', '--> CHANNEL_CLOSE', RecipientChannel);
712 var cirachannel = socket.tag.channels[RecipientChannel];
713 if (cirachannel == null) { console.log("MPS Error in CHANNEL_CLOSE: Unable to find channelid " + RecipientChannel); return 5; }
714 socket.tag.activetunnels--;
715 if (cirachannel.state > 0) {
716 cirachannel.state = 0;
717 if (cirachannel.onStateChange) { cirachannel.onStateChange(cirachannel, cirachannel.state); }
718 + SendChannelClose(cirachannel.socket, cirachannel.amtchannelid);
719 delete socket.tag.channels[RecipientChannel];
720 }
721 return 5;
@@ -727,7 +728,7 @@ module.exports.CreateMpsServer = function (parent, db, args, certificates) {
728 var cirachannel = socket.tag.channels[RecipientChannel];
729 if (cirachannel == null) { console.log("MPS Error in CHANNEL_WINDOW_ADJUST: Unable to find channelid " + RecipientChannel); return 9; }
730 cirachannel.sendcredits += ByteToAdd;
730 - parent.debug('mpscmd', 'CHANNEL_WINDOW_ADJUST', RecipientChannel, ByteToAdd, cirachannel.sendcredits);
731 + parent.debug('mpscmd', '--> CHANNEL_WINDOW_ADJUST', RecipientChannel, ByteToAdd, cirachannel.sendcredits);
732 if (cirachannel.state == 2 && cirachannel.sendBuffer != null) {
733 // Compute how much data we can send
734 if (cirachannel.sendBuffer.length <= cirachannel.sendcredits) {
@@ -751,7 +752,7 @@ module.exports.CreateMpsServer = function (parent, db, args, certificates) {
752 var RecipientChannel = common.ReadInt(data, 1);
753 var LengthOfData = common.ReadInt(data, 5);
754 if (len < (9 + LengthOfData)) return 0;
754 - parent.debug('mpscmddata', 'CHANNEL_DATA', RecipientChannel, LengthOfData);
755 + parent.debug('mpscmddata', '--> CHANNEL_DATA', RecipientChannel, LengthOfData);
756 var cirachannel = socket.tag.channels[RecipientChannel];
757 if (cirachannel == null) { console.log("MPS Error in CHANNEL_DATA: Unable to find channelid " + RecipientChannel); return 9 + LengthOfData; }
758 cirachannel.amtpendingcredits += LengthOfData;
@@ -767,14 +768,14 @@ module.exports.CreateMpsServer = function (parent, db, args, certificates) {
768 if (len < 7) return 0;
769 var ReasonCode = common.ReadInt(data, 1);
770 disconnectCommandCount++;
770 - parent.debug('mpscmd', 'DISCONNECT', ReasonCode);
771 + parent.debug('mpscmd', '--> DISCONNECT', ReasonCode);
772 try { delete obj.ciraConnections[socket.tag.nodeid]; } catch (e) { }
773 obj.parent.ClearConnectivityState(socket.tag.meshid, socket.tag.nodeid, 2);
774 return 7;
775 }
776 default:
777 {
777 - parent.debug('mpscmd', 'Unknown CIRA command: ' + cmd);
778 + parent.debug('mpscmd', '--> Unknown CIRA command: ' + cmd);
779 return -1;
780 }
781 }
@@ -789,6 +790,7 @@ module.exports.CreateMpsServer = function (parent, db, args, certificates) {
790
791 socket.addListener('error', function () {
792 socketErrorCount++;
793 + parent.debug('mps', 'CIRA connection error');
794 //console.log("MPS Error: " + socket.remoteAddress);
795 });
796
@@ -802,33 +804,40 @@ module.exports.CreateMpsServer = function (parent, db, args, certificates) {
804 };
805
806 function SendServiceAccept(socket, service) {
807 + parent.debug('mpscmd', '<-- SERVICE_ACCEPT', service);
808 Write(socket, String.fromCharCode(APFProtocol.SERVICE_ACCEPT) + common.IntToStr(service.length) + service);
809 }
810
811 function SendTcpForwardSuccessReply(socket, port) {
812 + parent.debug('mpscmd', '<-- REQUEST_SUCCESS', port);
813 Write(socket, String.fromCharCode(APFProtocol.REQUEST_SUCCESS) + common.IntToStr(port));
814 }
815
816 function SendTcpForwardCancelReply(socket) {
817 + parent.debug('mpscmd', '<-- REQUEST_SUCCESS');
818 Write(socket, String.fromCharCode(APFProtocol.REQUEST_SUCCESS));
819 }
820
821 /*
822 function SendKeepAliveRequest(socket, cookie) {
823 + parent.debug('mpscmd', '<-- KEEPALIVE_REQUEST', cookie);
824 Write(socket, String.fromCharCode(APFProtocol.KEEPALIVE_REQUEST) + common.IntToStr(cookie));
825 }
826 */
827
828 function SendKeepAliveReply(socket, cookie) {
829 + parent.debug('mpscmd', '<-- KEEPALIVE_REPLY', cookie);
830 Write(socket, String.fromCharCode(APFProtocol.KEEPALIVE_REPLY) + common.IntToStr(cookie));
831 }
832
833 function SendChannelOpenFailure(socket, senderChannel, reasonCode) {
834 + parent.debug('mpscmd', '<-- CHANNEL_OPEN_FAILURE', senderChannel, reasonCode);
835 Write(socket, String.fromCharCode(APFProtocol.CHANNEL_OPEN_FAILURE) + common.IntToStr(senderChannel) + common.IntToStr(reasonCode) + common.IntToStr(0) + common.IntToStr(0));
836 }
837
838 /*
839 function SendChannelOpenConfirmation(socket, recipientChannelId, senderChannelId, initialWindowSize) {
840 + parent.debug('mpscmd', '<-- CHANNEL_OPEN_CONFIRMATION', recipientChannelId, senderChannelId, initialWindowSize);
841 Write(socket, String.fromCharCode(APFProtocol.CHANNEL_OPEN_CONFIRMATION) + common.IntToStr(recipientChannelId) + common.IntToStr(senderChannelId) + common.IntToStr(initialWindowSize) + common.IntToStr(-1));
842 }
843 */
@@ -836,33 +845,39 @@ module.exports.CreateMpsServer = function (parent, db, args, certificates) {
845 function SendChannelOpen(socket, direct, channelid, windowsize, target, targetport, source, sourceport) {
846 var connectionType = ((direct == true) ? 'direct-tcpip' : 'forwarded-tcpip');
847 if ((target == null) || (target == null)) target = ''; // TODO: Reports of target being undefined that causes target.length to fail. This is a hack.
848 + parent.debug('mpscmd', '<-- CHANNEL_OPEN', connectionType, channelid, windowsize, target + ':' + targetport, source + ':' + sourceport);
849 Write(socket, String.fromCharCode(APFProtocol.CHANNEL_OPEN) + common.IntToStr(connectionType.length) + connectionType + common.IntToStr(channelid) + common.IntToStr(windowsize) + common.IntToStr(-1) + common.IntToStr(target.length) + target + common.IntToStr(targetport) + common.IntToStr(source.length) + source + common.IntToStr(sourceport));
850 }
851
852 function SendChannelClose(socket, channelid) {
853 + parent.debug('mpscmd', '<-- CHANNEL_CLOSE', channelid);
854 Write(socket, String.fromCharCode(APFProtocol.CHANNEL_CLOSE) + common.IntToStr(channelid));
855 }
856
857 function SendChannelData(socket, channelid, data) {
858 + parent.debug('mpscmddata', '<-- CHANNEL_DATA', channelid, data.length);
859 Write(socket, String.fromCharCode(APFProtocol.CHANNEL_DATA) + common.IntToStr(channelid) + common.IntToStr(data.length) + data);
860 }
861
862 function SendChannelWindowAdjust(socket, channelid, bytestoadd) {
851 - parent.debug('mpscmd', 'SendChannelWindowAdjust', channelid, bytestoadd);
863 + parent.debug('mpscmd', '<-- CHANNEL_WINDOW_ADJUST', channelid, bytestoadd);
864 Write(socket, String.fromCharCode(APFProtocol.CHANNEL_WINDOW_ADJUST) + common.IntToStr(channelid) + common.IntToStr(bytestoadd));
865 }
866
867 /*
868 function SendDisconnect(socket, reasonCode) {
869 + parent.debug('mpscmd', '<-- DISCONNECT', reasonCode);
870 Write(socket, String.fromCharCode(APFProtocol.DISCONNECT) + common.IntToStr(reasonCode) + common.ShortToStr(0));
871 }
872 */
873
874 function SendUserAuthFail(socket) {
875 + parent.debug('mpscmd', '<-- USERAUTH_FAILURE');
876 Write(socket, String.fromCharCode(APFProtocol.USERAUTH_FAILURE) + common.IntToStr(8) + 'password' + common.ShortToStr(0));
877 }
878
879 function SendUserAuthSuccess(socket) {
880 + parent.debug('mpscmd', '<-- USERAUTH_SUCCESS');
881 Write(socket, String.fromCharCode(APFProtocol.USERAUTH_SUCCESS));
882 }
883
@@ -870,7 +885,7 @@ module.exports.CreateMpsServer = function (parent, db, args, certificates) {
885 if (args.mpsdebug) {
886 // Print out sent bytes
887 var buf = Buffer.from(data, 'binary');
873 - console.log('MPS --> (' + buf.length + '):' + buf.toString('hex'));
888 + console.log('MPS <-- (' + buf.length + '):' + buf.toString('hex'));
889 socket.write(buf);
890 } else {
891 socket.write(Buffer.from(data, 'binary'));