cacheValue = resolveCacheValue(artificialEntry);
}
if (cacheValue != null) {
- final MapRegisterCacheMetadata mapRegisterValue = cacheValue.getMapRegisterCacheMetadata();
- LOG.debug("Map register message site-ID: {} xTR-ID: {} from cache.", mapRegisterValue.getSiteId(),
- mapRegisterValue.getXtrId());
- mapRegisterCache.refreshEntry(cacheKey);
+ MapRegisterCacheMetadata mapRegisterMeta = cacheValue.getMapRegisterCacheMetadata();
+ LOG.debug("Map register message site-ID: {} xTR-ID: {} from cache.", mapRegisterMeta.getSiteId(),
+ mapRegisterMeta.getXtrId());
+ mapRegisterMeta = refreshEntry(cacheKey);
sendNotificationIfPossible(createMappingKeepAlive(cacheValue));
- if (mapRegisterValue.isWantMapNotify()) {
- sendMapNotifyMsg(inBuffer, sourceAddress, port, mapRegisterValue.getEidLispAddress());
+
+ if (mapRegisterMeta.isWantMapNotify()) {
+ sendMapNotifyMsg(inBuffer, sourceAddress, port, cacheValue);
}
lispSbStats.incrementCacheHits();
} else {
MapRegister mapRegister = MapRegisterSerializer.getInstance().deserialize(inBuffer, sourceAddress);
- if (isAuthenticationSuccessful(mapRegister, inBuffer)) {
+ final MappingAuthkey mappingAuthkey = tryToAuthenticateMessage(mapRegister, inBuffer);
+ if (mappingAuthkey != null) {
AddMappingBuilder addMappingBuilder = new AddMappingBuilder();
addMappingBuilder.setMapRegister(LispNotificationHelper.convertMapRegister(mapRegister));
TransportAddressBuilder transportAddressBuilder = new TransportAddressBuilder();
final MapRegisterCacheValueBuilder cacheValueBldNew = new MapRegisterCacheValueBuilder();
cacheValueBldNew.setPacketData(artificialEntry.getValue());
+ cacheValueBldNew.setAuthKeyValue(mappingAuthkey.getKeyString());
+ cacheValueBldNew.setAuthKeyType(mappingAuthkey.getKeyType());
cacheValueBldNew.setMapRegisterCacheMetadata(cacheMetadataBldNew.build());
mapRegisterCache.addEntry(cacheKey, cacheValueBldNew.build());
}
}
+ private MapRegisterCacheMetadata refreshEntry(final MapRegisterCacheKey cacheKey) {
+ MapRegisterCacheValue mapRegisterCacheValue = mapRegisterCache.refreshEntry(cacheKey);
+ if (mapRegisterCacheValue != null) {
+ mapRegisterCacheValue = refreshAuthKeyIfNecessary(mapRegisterCacheValue);
+ mapRegisterCache.addEntry(cacheKey, mapRegisterCacheValue);
+ return mapRegisterCacheValue.getMapRegisterCacheMetadata();
+ }
+ return null;
+ }
+
+ private MapRegisterCacheValue refreshAuthKeyIfNecessary(MapRegisterCacheValue mapRegisterCacheValue) {
+ if (authenticationKeyDataListener.isAuthKeyRefreshing()) {
+ final boolean shouldAuthKeyRefreshingStop = System.currentTimeMillis() - authenticationKeyDataListener
+ .getAuthKeyRefreshingDate() > CACHE_RECORD_TIMEOUT;
+ if (shouldAuthKeyRefreshingStop) {
+ authenticationKeyDataListener.setAuthKeyRefreshing(false);
+ } else {
+ final MappingAuthkey mappingAuthkey = provideAuthenticateKey(mapRegisterCacheValue
+ .getMapRegisterCacheMetadata().getEidLispAddress());
+
+ final MapRegisterCacheValueBuilder newMapRegisterCacheValueBuilder = new MapRegisterCacheValueBuilder
+ (mapRegisterCacheValue);
+ final MapRegisterCacheMetadataBuilder newMapRegisterCacheMetadataBuilder =
+ new MapRegisterCacheMetadataBuilder(mapRegisterCacheValue.getMapRegisterCacheMetadata());
+
+ newMapRegisterCacheValueBuilder.setAuthKeyValue(mappingAuthkey.getKeyString());
+ newMapRegisterCacheValueBuilder.setAuthKeyType(mappingAuthkey.getKeyType());
+ newMapRegisterCacheValueBuilder.setMapRegisterCacheMetadata(newMapRegisterCacheMetadataBuilder.build());
+ return newMapRegisterCacheValueBuilder.build();
+ }
+ }
+
+ return mapRegisterCacheValue;
+
+ }
+
private MapRegisterCacheValue resolveCacheValue(Map.Entry<MapRegisterCacheKey, byte[]> entry) {
if (entry != null) {
final MapRegisterCacheValue mapRegisterCacheValue = mapRegisterCache.getEntry(entry.getKey());
return mappingKeepAliveBuilder.build();
}
- private void sendMapNotifyMsg(final ByteBuffer inBuffer, final InetAddress inetAddress, int portNumber,
- List<EidLispAddress> eids) {
-
- if (eids == null) {
- LOG.warn("Map-Register Cache: missing EID list when trying to send Map-Notify!");
- return;
- }
-
- final MappingAuthkey authKey = provideAuthenticateKey(eids);
- if (authKey != null) {
- ByteBuffer outBuffer = transformMapRegisterToMapNotify(inBuffer);
- if (authKey.getKeyType() != 0) {
- outBuffer = calculateAndSetNewMAC(outBuffer, authKey.getKeyString());
- }
- outBuffer.position(0);
- lispSbPlugin.handleSerializedLispBuffer(inetAddress, outBuffer, MessageType.MapNotify, portNumber);
- } else {
- LOG.error("Map-Register Cache: authentication succeeded, but can't find auth key for sending Map-Notify");
- }
- }
-
/**
* Returns null if not all of eids have the same value of authentication key
*/
}
+ private void sendMapNotifyMsg(final ByteBuffer inBuffer, final InetAddress inetAddress, int portNumber,
+ MapRegisterCacheValue mapRegisterValue) {
+ if (mapRegisterValue.getAuthKeyType() != null) {
+ ByteBuffer outBuffer = transformMapRegisterToMapNotify(inBuffer);
+ if (mapRegisterValue.getAuthKeyType() != 0) {
+ outBuffer = calculateAndSetNewMAC(outBuffer, mapRegisterValue.getAuthKeyValue());
+ }
+ outBuffer.position(0);
+ lispSbPlugin.handleSerializedLispBuffer(inetAddress, outBuffer, MessageType.MapNotify, portNumber);
+ } else {
+ LOG.error("Map-Register Cache: authentication succeeded, but can't find auth key for sending Map-Notify");
+ }
+ }
+
/**
* Calculates new message authentication code (MAC) for notify message.
*
*
* @param mapRegister
* @param byteBuffer
- * @return
+ * @return Returns authentication key if all of EIDs have the same authentication key or null otherwise
*/
- private boolean isAuthenticationSuccessful(final MapRegister mapRegister, final ByteBuffer byteBuffer) {
+ private MappingAuthkey tryToAuthenticateMessage(final MapRegister mapRegister, final ByteBuffer byteBuffer) {
if (!authenticationEnabled) {
- return true;
+ return null;
}
if (smc == null) {
LOG.debug("Simple map cache wasn't instantieted and set.");
- return false;
+ return null;
}
MappingAuthkey firstAuthKey = null;
if (i == 0) {
firstAuthKey = smc.getAuthenticationKey(mappingRecord.getEid());
if (!LispAuthenticationUtil.validate(mapRegister, byteBuffer, mappingRecord.getEid(), firstAuthKey)) {
- return false;
+ return null;
}
} else {
final Eid eid = mappingRecord.getEid();
if (!firstAuthKey.equals(authKey)) {
LOG.debug("Map register packet contained several eids. Authentication keys for first one and for " +
"{} are different.",LispAddressStringifier.getString(eid));
- return false;
+ return null;
}
}
}
- return true;
+ return firstAuthKey;
}
private void handleMapNotify(ByteBuffer inBuffer, InetAddress sourceAddress, int port) {