blob: c0d6bc4fdca84e528ebd964735abadb86f25493b [file] [log] [blame]
akmhoque3d06e792014-05-27 16:23:20 -05001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Ashlesh Gawande7e3f6d72019-01-25 13:13:43 -06003 * Copyright (c) 2014-2019, The University of Memphis,
Vince Lehmanc2e51f62015-01-20 15:03:11 -06004 * Regents of the University of California,
5 * Arizona Board of Regents.
akmhoque3d06e792014-05-27 16:23:20 -05006 *
7 * This file is part of NLSR (Named-data Link State Routing).
8 * See AUTHORS.md for complete list of NLSR authors and contributors.
9 *
10 * NLSR is free software: you can redistribute it and/or modify it under the terms
11 * of the GNU General Public License as published by the Free Software Foundation,
12 * either version 3 of the License, or (at your option) any later version.
13 *
14 * NLSR is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
15 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
16 * PURPOSE. See the GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along with
19 * NLSR, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
akmhoque3d06e792014-05-27 16:23:20 -050020 **/
Vince Lehmanc2e51f62015-01-20 15:03:11 -060021
dmcoomes9f936662017-03-02 10:33:09 -060022#ifndef NLSR_NLSR_HPP
23#define NLSR_NLSR_HPP
akmhoque298385a2014-02-13 14:13:09 -060024
Vince Lehmanc2acdcb2015-04-29 11:14:35 -050025#include "adjacency-list.hpp"
Vince Lehman0a7da612014-10-29 14:39:29 -050026#include "common.hpp"
akmhoque53353462014-04-22 08:43:45 -050027#include "conf-parameter.hpp"
akmhoque31d1d4b2014-05-05 22:08:14 -050028#include "hello-protocol.hpp"
Vince Lehmanc2acdcb2015-04-29 11:14:35 -050029#include "lsdb.hpp"
30#include "name-prefix-list.hpp"
Vince Lehman7b616582014-10-17 16:25:39 -050031#include "test-access-control.hpp"
laqinfan35731852017-08-08 06:17:39 -050032#include "publisher/dataset-interest-handler.hpp"
Vince Lehmanc2acdcb2015-04-29 11:14:35 -050033#include "route/fib.hpp"
34#include "route/name-prefix-table.hpp"
35#include "route/routing-table.hpp"
36#include "security/certificate-store.hpp"
37#include "update/prefix-update-processor.hpp"
Nick Gordon4d2c6c02017-01-20 13:18:46 -060038#include "update/nfd-rib-command-processor.hpp"
Vince Lehmanc2acdcb2015-04-29 11:14:35 -050039#include "utility/name-helper.hpp"
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -060040#include "stats-collector.hpp"
Yingdi Yu20e3a6e2014-05-26 23:16:10 -070041
Nick Gordond0a7df32017-05-30 16:44:34 -050042#include <boost/cstdint.hpp>
Laqin Fan54a43f02017-03-08 12:31:30 -060043#include <stdexcept>
Nick Gordond0a7df32017-05-30 16:44:34 -050044#include <boost/throw_exception.hpp>
Laqin Fan54a43f02017-03-08 12:31:30 -060045
46#include <ndn-cxx/face.hpp>
47#include <ndn-cxx/security/key-chain.hpp>
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -050048#include <ndn-cxx/security/validator-config.hpp>
49#include <ndn-cxx/security/v2/certificate-fetcher-direct-fetch.hpp>
50#include <ndn-cxx/security/signing-helpers.hpp>
51#include <ndn-cxx/security/signing-info.hpp>
Laqin Fan54a43f02017-03-08 12:31:30 -060052#include <ndn-cxx/util/scheduler.hpp>
53#include <ndn-cxx/mgmt/nfd/face-event-notification.hpp>
54#include <ndn-cxx/mgmt/nfd/face-monitor.hpp>
55#include <ndn-cxx/mgmt/dispatcher.hpp>
56#include <ndn-cxx/mgmt/nfd/face-status.hpp>
57#include <ndn-cxx/data.hpp>
58#include <ndn-cxx/encoding/block.hpp>
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -050059#include <ndn-cxx/encoding/nfd-constants.hpp>
60#include <ndn-cxx/mgmt/nfd/control-parameters.hpp>
61#include <ndn-cxx/mgmt/nfd/control-response.hpp>
Laqin Fan54a43f02017-03-08 12:31:30 -060062
akmhoque53353462014-04-22 08:43:45 -050063namespace nlsr {
64
Yingdi Yu20e3a6e2014-05-26 23:16:10 -070065static ndn::Name DEFAULT_BROADCAST_PREFIX("/ndn/broadcast");
66
akmhoque53353462014-04-22 08:43:45 -050067class Nlsr
68{
Nick Gordond5c1a372016-10-31 13:56:23 -050069public:
70 using FetchDatasetCallback = std::function<void(const std::vector<ndn::nfd::FaceStatus>&)>;
71 using FetchDatasetTimeoutCallback = std::function<void(uint32_t, const std::string&)>;
Nick Gordon9461afb2017-04-25 15:54:50 -050072
akmhoquefdbddb12014-05-02 18:35:19 -050073 class Error : public std::runtime_error
74 {
75 public:
76 explicit
77 Error(const std::string& what)
78 : std::runtime_error(what)
79 {
80 }
81 };
82
Ashlesh Gawande85998a12017-12-07 22:22:13 -060083 Nlsr(ndn::Face& face, ndn::KeyChain& keyChain, ConfParameter& confParam);
akmhoque298385a2014-02-13 14:13:09 -060084
akmhoque53353462014-04-22 08:43:45 -050085 void
86 registrationFailed(const ndn::Name& name);
87
88 void
akmhoque157b0a42014-05-13 00:26:37 -050089 onRegistrationSuccess(const ndn::Name& name);
90
91 void
akmhoque31d1d4b2014-05-05 22:08:14 -050092 setInfoInterestFilter();
93
94 void
95 setLsaInterestFilter();
akmhoque53353462014-04-22 08:43:45 -050096
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -050097 /*! \brief Add top level prefixes for Dispatcher
98 *
99 * All dispatcher-related sub-prefixes *must* be registered before sub-prefixes
100 * must be added before adding top
101 */
102 void
103 addDispatcherTopPrefix(const ndn::Name& topPrefix);
104
akmhoque53353462014-04-22 08:43:45 -0500105 Fib&
106 getFib()
107 {
108 return m_fib;
109 }
akmhoque298385a2014-02-13 14:13:09 -0600110
akmhoque53353462014-04-22 08:43:45 -0500111 void
112 initialize();
akmhoque1fd8c1e2014-02-19 19:41:49 -0600113
Nick Gordond5c1a372016-10-31 13:56:23 -0500114 /*! \brief Initializes neighbors' Faces using information from NFD.
115 * \sa Nlsr::initialize()
116 * \sa Nlsr::processFaceDataset()
117 *
118 * This function serves as the entry-point for initializing the
119 * neighbors listed in nlsr.conf during Nlsr::initialize(). NLSR
120 * will attempt to fetch a dataset of Faces from NFD, and configure
121 * each of its neighbors using information from that dataset. The
122 * explicit callbacks allow for better testability.
123 */
124 void
125 initializeFaces(const FetchDatasetCallback& onFetchSuccess,
126 const FetchDatasetTimeoutCallback& onFetchFailure);
127
128 void
129 onFaceDatasetFetchTimeout(uint32_t code,
130 const std::string& reason,
131 uint32_t nRetriesSoFar);
132
133 /*! \brief Consumes a Face StatusDataset to configure NLSR neighbors.
134 * \sa Nlsr::initializeFaces
135 * \param faces A Face Dataset that should conform to FaceMgmt specifications.
136 *
137 * This function processes a Face StatusDataset that should conform
138 * to the FaceMgmt specifications listed
139 * [here](https://redmine.named-data.net/projects/nfd/wiki/FaceMgmt#Face-Dataset).
140 * Any newly configured neighbors will have prefixes registered with NFD
141 * and be sent Hello Interests as well.
142 */
143 void
144 processFaceDataset(const std::vector<ndn::nfd::FaceStatus>& faces);
145
146 /*! \brief Registers NLSR-specific prefixes for a neighbor (Adjacent)
147 * \sa Nlsr::initializeFaces
148 * \param adj A reference to the neighbor to register prefixes for
149 * \param timeout The amount of time to give NFD to respond to *each* registration request.
150 *
151 * Registers the prefixes in NFD that NLSR needs to route with a
152 * neighbor. The timeout given is how long to set the timeout for
153 * *each* registration request that is made.
154 */
155 void
156 registerAdjacencyPrefixes(const Adjacent& adj,
157 const ndn::time::milliseconds& timeout);
158
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500159 /*! \brief Add a certificate NLSR claims to be authoritative for to the certificate store.
160 *
161 * \sa CertificateStore
162 */
163 void
164 loadCertToPublish(const ndn::security::v2::Certificate& certificate);
165
Muktadir Chowdhuryc3ea26f2018-01-05 21:40:59 +0000166 /*! \brief Callback when SegmentFetcher retrieves a segment.
167 */
168 void
169 afterFetcherSignalEmitted(const ndn::Data& lsaSegment);
170
171 /*! \brief Retrieves the chain of certificates from Validator's cache and
172 * store them in Nlsr's own CertificateStore.
173 * \param keyName Name of the first key in the certificate chain.
174 */
175 void
176 publishCertFromCache(const ndn::Name& keyName);
177
178 void
akmhoque443ad812014-07-29 10:26:56 -0500179 initializeKey();
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700180
Nick Gordond0a7df32017-05-30 16:44:34 -0500181 /*! \brief Find a certificate
182 *
183 * Find a certificate that NLSR has. First it checks against the
184 * certificates this NLSR claims to be authoritative for, usually
185 * something like this specific router's certificate, and then
186 * checks the cache of certficates it has already fetched. If none
187 * can be found, it will return an empty pointer.
188 */
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500189 const ndn::security::v2::Certificate*
190 getCertificate(const ndn::Name& certificateKeyName)
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700191 {
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500192 const ndn::security::v2::Certificate* cert =
193 m_certStore.find(certificateKeyName);
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700194
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500195 return cert;
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700196 }
197
akmhoquee1765152014-06-30 11:32:01 -0500198 void
akmhoquec04e7272014-07-02 11:00:14 -0500199 setStrategies();
akmhoque157b0a42014-05-13 00:26:37 -0500200
alvy297f4162015-03-03 17:15:33 -0600201PUBLIC_WITH_TESTS_ELSE_PRIVATE:
alvy297f4162015-03-03 17:15:33 -0600202
Vince Lehmanc2acdcb2015-04-29 11:14:35 -0500203 security::CertificateStore&
204 getCertificateStore()
205 {
206 return m_certStore;
207 }
208
akmhoque393d4ff2014-07-16 14:27:03 -0500209private:
Nick Gordond0a7df32017-05-30 16:44:34 -0500210 /*! \brief Registers the prefix that NLSR will use for key/certificate interests.
211 */
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700212 void
213 registerKeyPrefix();
214
Nick Gordond0a7df32017-05-30 16:44:34 -0500215 /*! \brief Registers the prefix that NLSR will consider to be the machine-local, secure prefix.
216 */
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700217 void
alvy297f4162015-03-03 17:15:33 -0600218 registerLocalhostPrefix();
219
Ashlesh Gawandecba0ae22018-03-27 17:57:56 -0500220 /*! \brief Registers the <router-prefix>/nlsr so that NLSR can respond to status requests from remote routers.
221 */
222 void
223 registerRouterPrefix();
224
Nick Gordond0a7df32017-05-30 16:44:34 -0500225 /*! \brief Attempts to satisfy an Interest for a certificate, and send it back.
226 */
alvy297f4162015-03-03 17:15:33 -0600227 void
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700228 onKeyInterest(const ndn::Name& name, const ndn::Interest& interest);
229
Nick Gordond0a7df32017-05-30 16:44:34 -0500230 /*! \brief Do nothing.
231 */
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700232 void
233 onKeyPrefixRegSuccess(const ndn::Name& name);
234
Nick Gordond0a7df32017-05-30 16:44:34 -0500235 /*! \brief Do nothing.
236 */
akmhoquee1765152014-06-30 11:32:01 -0500237 void
akmhoquec04e7272014-07-02 11:00:14 -0500238 onFaceEventNotification(const ndn::nfd::FaceEventNotification& faceEventNotification);
akmhoquee1765152014-06-30 11:32:01 -0500239
Vince Lehman7b616582014-10-17 16:25:39 -0500240 void
Nick Gordond5c1a372016-10-31 13:56:23 -0500241 scheduleDatasetFetch();
242
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500243 /*! \brief Enables NextHopFaceId indication in NFD for incoming data packet.
244 *
245 * After enabling, when NFD gets a data packet, it will put the incoming face id
246 * of the data in NextHopFaceId field of the packet. The NextHopFaceId will be used
247 * by DirectFetcher to fetch the certificates needed to validate the data packet.
248 * \sa https://redmine.named-data.net/projects/nfd/wiki/NDNLPv2#Consumer-Controlled-Forwarding
249 */
250 void
251 enableIncomingFaceIdIndication();
252
253 void
254 onFaceIdIndicationSuccess(const ndn::nfd::ControlParameters& cp);
255
256 void
257 onFaceIdIndicationFailure(const ndn::nfd::ControlResponse& cr);
258
alvy297f4162015-03-03 17:15:33 -0600259public:
260 static const ndn::Name LOCALHOST_PREFIX;
261
akmhoque157b0a42014-05-13 00:26:37 -0500262private:
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600263 ndn::Face& m_face;
264 ndn::Scheduler m_scheduler;
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500265 ndn::security::v2::KeyChain& m_keyChain;
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600266 ConfParameter& m_confParam;
267 AdjacencyList& m_adjacencyList;
268 NamePrefixList& m_namePrefixList;
269 bool m_isDaemonProcess;
270 ndn::security::ValidatorConfig& m_validator;
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700271
Vince Lehmanf7eec4f2015-05-08 19:02:31 -0500272PUBLIC_WITH_TESTS_ELSE_PRIVATE:
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600273 Fib m_fib;
274 RoutingTable m_routingTable;
275 NamePrefixTable m_namePrefixTable;
276 Lsdb m_lsdb;
Vince Lehmanf7eec4f2015-05-08 19:02:31 -0500277
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600278private:
279 ndn::util::signal::ScopedConnection m_afterSegmentValidatedConnection;
280
281PUBLIC_WITH_TESTS_ELSE_PRIVATE:
282 ndn::mgmt::Dispatcher m_dispatcher;
283 DatasetInterestHandler m_datasetHandler;
284 HelloProtocol m_helloProtocol;
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500285
Vince Lehman7b616582014-10-17 16:25:39 -0500286private:
Nick Gordond0a7df32017-05-30 16:44:34 -0500287 /*! \brief Where NLSR stores certificates it claims to be
288 * authoritative for. Usually the router certificate.
289 */
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600290 security::CertificateStore& m_certStore;
Nick Gordond5c1a372016-10-31 13:56:23 -0500291
Nick Gordond5c1a372016-10-31 13:56:23 -0500292 ndn::nfd::Controller m_controller;
293 ndn::nfd::Controller m_faceDatasetController;
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600294
295PUBLIC_WITH_TESTS_ELSE_PRIVATE:
Joao Pereira97473d42015-07-03 16:57:27 -0400296 ndn::security::SigningInfo m_signingInfo;
alvy297f4162015-03-03 17:15:33 -0600297 update::PrefixUpdateProcessor m_prefixUpdateProcessor;
Nick Gordon4d2c6c02017-01-20 13:18:46 -0600298 update::NfdRibCommandProcessor m_nfdRibCommandProcessor;
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600299
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -0600300 StatsCollector m_statsCollector;
akmhoquee1765152014-06-30 11:32:01 -0500301
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600302private:
akmhoque060d3022014-08-12 13:35:06 -0500303 ndn::nfd::FaceMonitor m_faceMonitor;
Vince Lehman7b616582014-10-17 16:25:39 -0500304
Nick Gordond5c1a372016-10-31 13:56:23 -0500305 friend class NlsrRunner;
akmhoque53353462014-04-22 08:43:45 -0500306};
akmhoque298385a2014-02-13 14:13:09 -0600307
Nick Gordonfad8e252016-08-11 14:21:38 -0500308} // namespace nlsr
akmhoqueb1710aa2014-02-19 17:13:36 -0600309
dmcoomes9f936662017-03-02 10:33:09 -0600310#endif // NLSR_NLSR_HPP