Jian Li | 451cea3 | 2016-10-04 15:27:50 +0900 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016-present Open Networking Laboratory |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | package org.onosproject.lisp.ctl; |
| 17 | |
Jian Li | 6ef1b3f | 2016-11-12 18:16:06 +0900 | [diff] [blame] | 18 | import org.onlab.packet.IpAddress; |
Jian Li | 712ec05 | 2016-11-22 03:23:54 +0900 | [diff] [blame] | 19 | import org.onosproject.lisp.msg.authentication.LispAuthenticationConfig; |
Jian Li | 6ef1b3f | 2016-11-12 18:16:06 +0900 | [diff] [blame] | 20 | import org.onosproject.lisp.msg.protocols.DefaultLispInfoReply; |
Jian Li | 24f6cc0 | 2016-11-01 16:38:40 +0900 | [diff] [blame] | 21 | import org.onosproject.lisp.msg.protocols.DefaultLispMapNotify.DefaultNotifyBuilder; |
| 22 | import org.onosproject.lisp.msg.protocols.DefaultLispMapRegister.DefaultRegisterBuilder; |
| 23 | import org.onosproject.lisp.msg.protocols.LispEidRecord; |
Jian Li | 6ef1b3f | 2016-11-12 18:16:06 +0900 | [diff] [blame] | 24 | import org.onosproject.lisp.msg.protocols.LispInfoReply; |
| 25 | import org.onosproject.lisp.msg.protocols.LispInfoReply.InfoReplyBuilder; |
| 26 | import org.onosproject.lisp.msg.protocols.LispInfoRequest; |
| 27 | import org.onosproject.lisp.msg.protocols.LispInfoRequest.InfoRequestBuilder; |
Jian Li | afe2d3f | 2016-11-01 02:49:07 +0900 | [diff] [blame] | 28 | import org.onosproject.lisp.msg.protocols.LispMapNotify; |
Jian Li | 24f6cc0 | 2016-11-01 16:38:40 +0900 | [diff] [blame] | 29 | import org.onosproject.lisp.msg.protocols.LispMapNotify.NotifyBuilder; |
| 30 | import org.onosproject.lisp.msg.protocols.LispMapRegister; |
| 31 | import org.onosproject.lisp.msg.protocols.LispMapRegister.RegisterBuilder; |
Jian Li | 451cea3 | 2016-10-04 15:27:50 +0900 | [diff] [blame] | 32 | import org.onosproject.lisp.msg.protocols.LispMessage; |
Jian Li | 6ef1b3f | 2016-11-12 18:16:06 +0900 | [diff] [blame] | 33 | import org.onosproject.lisp.msg.types.LispAfiAddress; |
| 34 | import org.onosproject.lisp.msg.types.LispIpv4Address; |
| 35 | import org.onosproject.lisp.msg.types.LispIpv6Address; |
| 36 | import org.onosproject.lisp.msg.types.LispNoAddress; |
Jian Li | 24f6cc0 | 2016-11-01 16:38:40 +0900 | [diff] [blame] | 37 | import org.slf4j.Logger; |
| 38 | import org.slf4j.LoggerFactory; |
| 39 | |
Jian Li | 6ef1b3f | 2016-11-12 18:16:06 +0900 | [diff] [blame] | 40 | import java.net.InetAddress; |
Jian Li | 24f6cc0 | 2016-11-01 16:38:40 +0900 | [diff] [blame] | 41 | import java.net.InetSocketAddress; |
Jian Li | 6ef1b3f | 2016-11-12 18:16:06 +0900 | [diff] [blame] | 42 | import java.net.UnknownHostException; |
Jian Li | 24f6cc0 | 2016-11-01 16:38:40 +0900 | [diff] [blame] | 43 | import java.util.Arrays; |
| 44 | |
| 45 | import static org.onosproject.lisp.msg.authentication.LispAuthenticationKeyEnum.valueOf; |
Jian Li | 6ef1b3f | 2016-11-12 18:16:06 +0900 | [diff] [blame] | 46 | import static org.onosproject.lisp.msg.protocols.DefaultLispInfoRequest.DefaultInfoRequestBuilder; |
| 47 | import static org.onosproject.lisp.msg.types.LispNatLcafAddress.NatAddressBuilder; |
Jian Li | 451cea3 | 2016-10-04 15:27:50 +0900 | [diff] [blame] | 48 | |
| 49 | /** |
| 50 | * LISP map server class. |
| 51 | * Handles map-register message and acknowledges with map-notify message. |
| 52 | */ |
Jian Li | 712ec05 | 2016-11-22 03:23:54 +0900 | [diff] [blame] | 53 | public final class LispMapServer { |
Jian Li | 6322a36 | 2016-10-31 00:57:19 +0900 | [diff] [blame] | 54 | |
Jian Li | d1a109e | 2016-11-12 09:00:42 +0900 | [diff] [blame] | 55 | private static final int MAP_NOTIFY_PORT = 4342; |
Jian Li | 6ef1b3f | 2016-11-12 18:16:06 +0900 | [diff] [blame] | 56 | private static final int INFO_REPLY_PORT = 4342; |
Jian Li | 24f6cc0 | 2016-11-01 16:38:40 +0900 | [diff] [blame] | 57 | |
Jian Li | 24f6cc0 | 2016-11-01 16:38:40 +0900 | [diff] [blame] | 58 | private static final Logger log = LoggerFactory.getLogger(LispMapServer.class); |
| 59 | |
Jian Li | 29986d8 | 2016-12-01 03:25:12 +0900 | [diff] [blame^] | 60 | private LispMappingDatabase mapDb = LispMappingDatabase.getInstance(); |
Jian Li | 712ec05 | 2016-11-22 03:23:54 +0900 | [diff] [blame] | 61 | private LispAuthenticationConfig authConfig = LispAuthenticationConfig.getInstance(); |
Jian Li | 24f6cc0 | 2016-11-01 16:38:40 +0900 | [diff] [blame] | 62 | |
Jian Li | 712ec05 | 2016-11-22 03:23:54 +0900 | [diff] [blame] | 63 | // non-instantiable (except for our Singleton) |
| 64 | private LispMapServer() { |
Jian Li | 24f6cc0 | 2016-11-01 16:38:40 +0900 | [diff] [blame] | 65 | } |
| 66 | |
Jian Li | 29986d8 | 2016-12-01 03:25:12 +0900 | [diff] [blame^] | 67 | public static LispMapServer getInstance() { |
| 68 | return SingletonHelper.INSTANCE; |
| 69 | } |
| 70 | |
Jian Li | 24f6cc0 | 2016-11-01 16:38:40 +0900 | [diff] [blame] | 71 | /** |
| 72 | * Handles map-register message and replies with map-notify message. |
| 73 | * |
| 74 | * @param message map-register message |
| 75 | * @return map-notify message |
| 76 | */ |
Jian Li | afe2d3f | 2016-11-01 02:49:07 +0900 | [diff] [blame] | 77 | public LispMapNotify processMapRegister(LispMessage message) { |
Jian Li | 24f6cc0 | 2016-11-01 16:38:40 +0900 | [diff] [blame] | 78 | |
| 79 | LispMapRegister register = (LispMapRegister) message; |
| 80 | |
Jian Li | d1a109e | 2016-11-12 09:00:42 +0900 | [diff] [blame] | 81 | if (!checkMapRegisterAuthData(register)) { |
Jian Li | 24f6cc0 | 2016-11-01 16:38:40 +0900 | [diff] [blame] | 82 | log.warn("Unmatched authentication data of Map-Register"); |
| 83 | return null; |
| 84 | } |
| 85 | |
Jian Li | 29986d8 | 2016-12-01 03:25:12 +0900 | [diff] [blame^] | 86 | register.getMapRecords().forEach(mapRecord -> { |
| 87 | LispEidRecord eidRecord = |
| 88 | new LispEidRecord(mapRecord.getMaskLength(), |
| 89 | mapRecord.getEidPrefixAfi()); |
| 90 | |
| 91 | mapDb.putMapRecord(eidRecord, mapRecord); |
| 92 | }); |
| 93 | |
Jian Li | 2c8a2a4 | 2016-11-24 02:51:03 +0900 | [diff] [blame] | 94 | // we only acknowledge back to ETR when want-map-notify bit is set to true |
| 95 | // otherwise, we do not acknowledge back to ETR |
| 96 | if (register.isWantMapNotify()) { |
| 97 | NotifyBuilder notifyBuilder = new DefaultNotifyBuilder(); |
| 98 | notifyBuilder.withKeyId(authConfig.lispAuthKeyId()); |
| 99 | notifyBuilder.withAuthDataLength(valueOf(authConfig.lispAuthKeyId()).getHashLength()); |
| 100 | notifyBuilder.withAuthKey(authConfig.lispAuthKey()); |
| 101 | notifyBuilder.withNonce(register.getNonce()); |
| 102 | notifyBuilder.withMapRecords(register.getMapRecords()); |
Jian Li | 24f6cc0 | 2016-11-01 16:38:40 +0900 | [diff] [blame] | 103 | |
Jian Li | 2c8a2a4 | 2016-11-24 02:51:03 +0900 | [diff] [blame] | 104 | LispMapNotify notify = notifyBuilder.build(); |
Jian Li | 24f6cc0 | 2016-11-01 16:38:40 +0900 | [diff] [blame] | 105 | |
Jian Li | 2c8a2a4 | 2016-11-24 02:51:03 +0900 | [diff] [blame] | 106 | InetSocketAddress address = |
| 107 | new InetSocketAddress(register.getSender().getAddress(), MAP_NOTIFY_PORT); |
| 108 | notify.configSender(address); |
Jian Li | 24f6cc0 | 2016-11-01 16:38:40 +0900 | [diff] [blame] | 109 | |
Jian Li | 2c8a2a4 | 2016-11-24 02:51:03 +0900 | [diff] [blame] | 110 | return notify; |
| 111 | } |
| 112 | |
| 113 | return null; |
Jian Li | 24f6cc0 | 2016-11-01 16:38:40 +0900 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | /** |
Jian Li | 6ef1b3f | 2016-11-12 18:16:06 +0900 | [diff] [blame] | 117 | * Handles info-request message and replies with info-reply message. |
| 118 | * |
| 119 | * @param message info-request message |
| 120 | * @return info-reply message |
| 121 | */ |
| 122 | public LispInfoReply processInfoRequest(LispMessage message) { |
| 123 | LispInfoRequest request = (LispInfoRequest) message; |
| 124 | |
| 125 | if (!checkInfoRequestAuthData(request)) { |
| 126 | log.warn("Unmatched authentication data of Info-Request"); |
| 127 | return null; |
| 128 | } |
| 129 | |
| 130 | NatAddressBuilder natBuilder = new NatAddressBuilder(); |
| 131 | try { |
| 132 | LispAfiAddress msAddress = |
| 133 | new LispIpv4Address(IpAddress.valueOf(InetAddress.getLocalHost())); |
| 134 | natBuilder.withMsRlocAddress(msAddress); |
| 135 | natBuilder.withMsUdpPortNumber((short) INFO_REPLY_PORT); |
| 136 | |
| 137 | // try to extract global ETR RLOC address from info-request |
| 138 | IpAddress globalRlocIp = IpAddress.valueOf(request.getSender().getAddress()); |
| 139 | LispAfiAddress globalRlocAddress; |
| 140 | if (globalRlocIp.isIp4()) { |
| 141 | globalRlocAddress = new LispIpv4Address(globalRlocIp); |
| 142 | } else { |
| 143 | globalRlocAddress = new LispIpv6Address(globalRlocIp); |
| 144 | } |
| 145 | natBuilder.withGlobalEtrRlocAddress(globalRlocAddress); |
| 146 | natBuilder.withEtrUdpPortNumber((short) request.getSender().getPort()); |
| 147 | natBuilder.withPrivateEtrRlocAddress(new LispNoAddress()); |
| 148 | |
| 149 | // TODO: need to specify RTR addresses |
| 150 | |
| 151 | } catch (UnknownHostException e) { |
| 152 | log.warn("Fails during formulate NAT address", e); |
| 153 | } |
| 154 | |
| 155 | InfoReplyBuilder replyBuilder = new DefaultLispInfoReply.DefaultInfoReplyBuilder(); |
| 156 | replyBuilder.withKeyId(request.getKeyId()); |
Jian Li | 712ec05 | 2016-11-22 03:23:54 +0900 | [diff] [blame] | 157 | replyBuilder.withAuthDataLength(valueOf(authConfig.lispAuthKeyId()).getHashLength()); |
| 158 | replyBuilder.withAuthKey(authConfig.lispAuthKey()); |
Jian Li | 6ef1b3f | 2016-11-12 18:16:06 +0900 | [diff] [blame] | 159 | replyBuilder.withNonce(request.getNonce()); |
| 160 | replyBuilder.withEidPrefix(request.getPrefix()); |
| 161 | replyBuilder.withMaskLength(request.getMaskLength()); |
| 162 | replyBuilder.withTtl(request.getTtl()); |
| 163 | replyBuilder.withNatLcafAddress(natBuilder.build()); |
| 164 | replyBuilder.withIsInfoReply(true); |
| 165 | |
| 166 | LispInfoReply reply = replyBuilder.build(); |
| 167 | reply.configSender(request.getSender()); |
| 168 | |
| 169 | return reply; |
| 170 | } |
| 171 | |
| 172 | /** |
Jian Li | 51aaca1 | 2016-11-11 01:56:15 +0900 | [diff] [blame] | 173 | * Checks the integrity of the received map-register message by calculating |
| 174 | * authentication data from received map-register message. |
Jian Li | 24f6cc0 | 2016-11-01 16:38:40 +0900 | [diff] [blame] | 175 | * |
Jian Li | 51aaca1 | 2016-11-11 01:56:15 +0900 | [diff] [blame] | 176 | * @param register map-register message |
Jian Li | 24f6cc0 | 2016-11-01 16:38:40 +0900 | [diff] [blame] | 177 | * @return evaluation result |
| 178 | */ |
Jian Li | d1a109e | 2016-11-12 09:00:42 +0900 | [diff] [blame] | 179 | private boolean checkMapRegisterAuthData(LispMapRegister register) { |
Jian Li | 24f6cc0 | 2016-11-01 16:38:40 +0900 | [diff] [blame] | 180 | RegisterBuilder registerBuilder = new DefaultRegisterBuilder(); |
| 181 | registerBuilder.withKeyId(register.getKeyId()); |
Jian Li | 712ec05 | 2016-11-22 03:23:54 +0900 | [diff] [blame] | 182 | registerBuilder.withAuthKey(authConfig.lispAuthKey()); |
Jian Li | 24f6cc0 | 2016-11-01 16:38:40 +0900 | [diff] [blame] | 183 | registerBuilder.withNonce(register.getNonce()); |
| 184 | registerBuilder.withIsProxyMapReply(register.isProxyMapReply()); |
| 185 | registerBuilder.withIsWantMapNotify(register.isWantMapNotify()); |
| 186 | registerBuilder.withMapRecords(register.getMapRecords()); |
Jian Li | 6ef1b3f | 2016-11-12 18:16:06 +0900 | [diff] [blame] | 187 | |
Jian Li | d1a109e | 2016-11-12 09:00:42 +0900 | [diff] [blame] | 188 | LispMapRegister authRegister = registerBuilder.build(); |
Jian Li | 24f6cc0 | 2016-11-01 16:38:40 +0900 | [diff] [blame] | 189 | |
Jian Li | d1a109e | 2016-11-12 09:00:42 +0900 | [diff] [blame] | 190 | return Arrays.equals(authRegister.getAuthData(), register.getAuthData()); |
Jian Li | 451cea3 | 2016-10-04 15:27:50 +0900 | [diff] [blame] | 191 | } |
Jian Li | 6ef1b3f | 2016-11-12 18:16:06 +0900 | [diff] [blame] | 192 | |
| 193 | /** |
| 194 | * Checks the integrity of the received info-request message by calculating |
| 195 | * authentication data from received info-request message. |
| 196 | * |
| 197 | * @param request info-request message |
| 198 | * @return evaluation result |
| 199 | */ |
| 200 | private boolean checkInfoRequestAuthData(LispInfoRequest request) { |
| 201 | InfoRequestBuilder requestBuilder = new DefaultInfoRequestBuilder(); |
| 202 | requestBuilder.withKeyId(request.getKeyId()); |
Jian Li | 712ec05 | 2016-11-22 03:23:54 +0900 | [diff] [blame] | 203 | requestBuilder.withAuthKey(authConfig.lispAuthKey()); |
Jian Li | 6ef1b3f | 2016-11-12 18:16:06 +0900 | [diff] [blame] | 204 | requestBuilder.withNonce(request.getNonce()); |
| 205 | requestBuilder.withTtl(request.getTtl()); |
| 206 | requestBuilder.withEidPrefix(request.getPrefix()); |
| 207 | requestBuilder.withIsInfoReply(request.isInfoReply()); |
| 208 | requestBuilder.withMaskLength(request.getMaskLength()); |
| 209 | |
yoonseon | 980cd7c | 2016-11-18 14:18:46 -0800 | [diff] [blame] | 210 | LispInfoRequest authRequest = requestBuilder.build(); |
Jian Li | 6ef1b3f | 2016-11-12 18:16:06 +0900 | [diff] [blame] | 211 | |
| 212 | return Arrays.equals(authRequest.getAuthData(), request.getAuthData()); |
| 213 | } |
Jian Li | 712ec05 | 2016-11-22 03:23:54 +0900 | [diff] [blame] | 214 | |
| 215 | /** |
| 216 | * Prevents object instantiation from external. |
| 217 | */ |
Jian Li | 29986d8 | 2016-12-01 03:25:12 +0900 | [diff] [blame^] | 218 | private static final class SingletonHelper { |
| 219 | private static final String ILLEGAL_ACCESS_MSG = "Should not instantiate this class."; |
Jian Li | 712ec05 | 2016-11-22 03:23:54 +0900 | [diff] [blame] | 220 | private static final LispMapServer INSTANCE = new LispMapServer(); |
Jian Li | 29986d8 | 2016-12-01 03:25:12 +0900 | [diff] [blame^] | 221 | |
| 222 | private SingletonHelper() { |
| 223 | throw new IllegalAccessError(ILLEGAL_ACCESS_MSG); |
| 224 | } |
Jian Li | 712ec05 | 2016-11-22 03:23:54 +0900 | [diff] [blame] | 225 | } |
Jian Li | 451cea3 | 2016-10-04 15:27:50 +0900 | [diff] [blame] | 226 | } |