blob: bcc726af97ed2b8f44db2944ee5f2d32840e83e5 [file] [log] [blame]
Jian Li451cea32016-10-04 15:27:50 +09001/*
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 */
16package org.onosproject.lisp.ctl;
17
Jian Li24f6cc02016-11-01 16:38:40 +090018import org.onosproject.lisp.msg.protocols.DefaultLispMapNotify.DefaultNotifyBuilder;
19import org.onosproject.lisp.msg.protocols.DefaultLispMapRegister.DefaultRegisterBuilder;
20import org.onosproject.lisp.msg.protocols.LispEidRecord;
Jian Liafe2d3f2016-11-01 02:49:07 +090021import org.onosproject.lisp.msg.protocols.LispMapNotify;
Jian Li24f6cc02016-11-01 16:38:40 +090022import org.onosproject.lisp.msg.protocols.LispMapNotify.NotifyBuilder;
23import org.onosproject.lisp.msg.protocols.LispMapRegister;
24import org.onosproject.lisp.msg.protocols.LispMapRegister.RegisterBuilder;
Jian Li451cea32016-10-04 15:27:50 +090025import org.onosproject.lisp.msg.protocols.LispMessage;
Jian Li24f6cc02016-11-01 16:38:40 +090026import org.slf4j.Logger;
27import org.slf4j.LoggerFactory;
28
29import java.net.InetSocketAddress;
30import java.util.Arrays;
31
32import static org.onosproject.lisp.msg.authentication.LispAuthenticationKeyEnum.valueOf;
Jian Li451cea32016-10-04 15:27:50 +090033
34/**
35 * LISP map server class.
36 * Handles map-register message and acknowledges with map-notify message.
37 */
Jian Li6322a362016-10-31 00:57:19 +090038public class LispMapServer {
39
Jian Lid1a109e2016-11-12 09:00:42 +090040 private static final int MAP_NOTIFY_PORT = 4342;
Jian Li24f6cc02016-11-01 16:38:40 +090041
42 // TODO: need to be configurable
43 private static final String AUTH_KEY = "onos";
44
Jian Li24f6cc02016-11-01 16:38:40 +090045 // TODO: need to be configurable
46 private static final short AUTH_METHOD = 1;
47
48 private static final Logger log = LoggerFactory.getLogger(LispMapServer.class);
49
Jian Li24f6cc02016-11-01 16:38:40 +090050 private LispEidRlocMap mapInfo;
51
52 public LispMapServer() {
Jian Li24f6cc02016-11-01 16:38:40 +090053 mapInfo = LispEidRlocMap.getInstance();
54 }
55
56 /**
57 * Handles map-register message and replies with map-notify message.
58 *
59 * @param message map-register message
60 * @return map-notify message
61 */
Jian Liafe2d3f2016-11-01 02:49:07 +090062 public LispMapNotify processMapRegister(LispMessage message) {
Jian Li24f6cc02016-11-01 16:38:40 +090063
64 LispMapRegister register = (LispMapRegister) message;
65
Jian Lid1a109e2016-11-12 09:00:42 +090066 if (!checkMapRegisterAuthData(register)) {
Jian Li24f6cc02016-11-01 16:38:40 +090067 log.warn("Unmatched authentication data of Map-Register");
68 return null;
69 }
70
Jian Li24f6cc02016-11-01 16:38:40 +090071 NotifyBuilder notifyBuilder = new DefaultNotifyBuilder();
72 notifyBuilder.withKeyId(AUTH_METHOD);
Jian Lid1a109e2016-11-12 09:00:42 +090073 notifyBuilder.withAuthDataLength(valueOf(AUTH_METHOD).getHashLength());
74 notifyBuilder.withAuthKey(AUTH_KEY);
Jian Li24f6cc02016-11-01 16:38:40 +090075 notifyBuilder.withNonce(register.getNonce());
76 notifyBuilder.withMapRecords(register.getMapRecords());
77
78 LispMapNotify notify = notifyBuilder.build();
79
80 InetSocketAddress address =
Jian Lid1a109e2016-11-12 09:00:42 +090081 new InetSocketAddress(register.getSender().getAddress(), MAP_NOTIFY_PORT);
Jian Li24f6cc02016-11-01 16:38:40 +090082 notify.configSender(address);
83
84 register.getMapRecords().forEach(record -> {
85 LispEidRecord eidRecord =
86 new LispEidRecord(record.getMaskLength(), record.getEidPrefixAfi());
87 mapInfo.insertMapRecord(eidRecord, record);
88 });
89
90 return notify;
91 }
92
93 /**
Jian Li51aaca12016-11-11 01:56:15 +090094 * Checks the integrity of the received map-register message by calculating
95 * authentication data from received map-register message.
Jian Li24f6cc02016-11-01 16:38:40 +090096 *
Jian Li51aaca12016-11-11 01:56:15 +090097 * @param register map-register message
Jian Li24f6cc02016-11-01 16:38:40 +090098 * @return evaluation result
99 */
Jian Lid1a109e2016-11-12 09:00:42 +0900100 private boolean checkMapRegisterAuthData(LispMapRegister register) {
Jian Li24f6cc02016-11-01 16:38:40 +0900101 RegisterBuilder registerBuilder = new DefaultRegisterBuilder();
102 registerBuilder.withKeyId(register.getKeyId());
Jian Lid1a109e2016-11-12 09:00:42 +0900103 registerBuilder.withAuthKey(AUTH_KEY);
Jian Li24f6cc02016-11-01 16:38:40 +0900104 registerBuilder.withNonce(register.getNonce());
105 registerBuilder.withIsProxyMapReply(register.isProxyMapReply());
106 registerBuilder.withIsWantMapNotify(register.isWantMapNotify());
107 registerBuilder.withMapRecords(register.getMapRecords());
Jian Lid1a109e2016-11-12 09:00:42 +0900108 LispMapRegister authRegister = registerBuilder.build();
Jian Li24f6cc02016-11-01 16:38:40 +0900109
Jian Lid1a109e2016-11-12 09:00:42 +0900110 return Arrays.equals(authRegister.getAuthData(), register.getAuthData());
Jian Li451cea32016-10-04 15:27:50 +0900111 }
112}