blob: ab3c64883dd8743924e1aef2bbcc3df384a69540 [file] [log] [blame]
Vince Lehmanb8b18062015-07-14 13:07:22 -05001# -*- Mode:python; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
2#
Ashlesh Gawandef5f304b2016-06-16 16:42:41 -05003# Copyright (C) 2015-2017, The University of Memphis,
Vince Lehman5d5a5662015-12-02 12:33:12 -06004# Arizona Board of Regents,
5# Regents of the University of California.
Vince Lehmanb8b18062015-07-14 13:07:22 -05006#
7# This file is part of Mini-NDN.
8# See AUTHORS.md for a complete list of Mini-NDN authors and contributors.
9#
10# Mini-NDN is free software: you can redistribute it and/or modify
11# it under the terms of the GNU General Public License as published by
12# the Free Software Foundation, either version 3 of the License, or
13# (at your option) any later version.
14#
15# Mini-NDN is distributed in the hope that it will be useful,
16# but WITHOUT ANY WARRANTY; without even the implied warranty of
17# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18# GNU General Public License for more details.
19#
20# You should have received a copy of the GNU General Public License
21# along with Mini-NDN, e.g., in COPYING.md file.
22# If not, see <http://www.gnu.org/licenses/>.
23
Vince Lehman5d5a5662015-12-02 12:33:12 -060024from mininet.clean import sh
Ashlesh Gawandef5f304b2016-06-16 16:42:41 -050025from mininet.examples.cluster import RemoteMixin
Vince Lehman5d5a5662015-12-02 12:33:12 -060026
Ashlesh Gawande792c6aa2015-07-10 12:18:36 -050027from ndn.ndn_application import NdnApplication
Ashlesh Gawandef5f304b2016-06-16 16:42:41 -050028from ndn.util import ssh, scp
ashuef3490b2015-02-17 11:01:04 -060029
Vince Lehman5d5a5662015-12-02 12:33:12 -060030import shutil
Ashlesh Gawandef5f304b2016-06-16 16:42:41 -050031import os
Vince Lehman5d5a5662015-12-02 12:33:12 -060032import textwrap
Ashlesh Gawandef5f304b2016-06-16 16:42:41 -050033from subprocess import call
Ashlesh Gawande59f86242017-05-05 09:45:18 -050034import time
Vince Lehman5d5a5662015-12-02 12:33:12 -060035
Ashlesh Gawandef6a610b2017-02-21 14:48:08 -060036NETWORK="/ndn/"
37
Ashlesh Gawande792c6aa2015-07-10 12:18:36 -050038class Nlsr(NdnApplication):
Ashlesh Gawande708fcca2017-06-23 14:04:12 -050039 def __init__(self, node, neighbors, faceType):
Ashlesh Gawande792c6aa2015-07-10 12:18:36 -050040 NdnApplication.__init__(self, node)
Ashlesh Gawande708fcca2017-06-23 14:04:12 -050041 self.node = node
42 self.neighbors = neighbors
43 self.faceType = faceType
ashuef3490b2015-02-17 11:01:04 -060044 self.routerName = "/%sC1.Router/cs/%s" % ('%', node.name)
Ashlesh Gawande1b663692015-10-14 16:38:10 -050045 self.confFile = "%s/nlsr.conf" % node.homeFolder
ashuef3490b2015-02-17 11:01:04 -060046
47 # Make directory for log file
Saurab Dulal7a6978e2017-11-29 10:50:09 -060048 self.logDir = "{}/log".format(node.homeFolder)
49 self.node.cmd("mkdir {}".format(self.logDir))
Ashlesh Gawande708fcca2017-06-23 14:04:12 -050050
51 # Create faces in NFD
52 self.createFaces()
ashuef3490b2015-02-17 11:01:04 -060053
ashuef3490b2015-02-17 11:01:04 -060054 def start(self):
dmcoomes73caa522018-01-15 16:33:32 -060055 self.node.cmd("export NDN_LOG=nlsr.*={}".format(self.node.nlsrParameters.get("nlsr-log-level", "DEBUG")))
56 NdnApplication.start(self, "nlsr -f {} > log/nlsr.log 2>&1 &".format(self.confFile))
Ashlesh Gawande59f86242017-05-05 09:45:18 -050057 time.sleep(1)
ashuef3490b2015-02-17 11:01:04 -060058
Ashlesh Gawande708fcca2017-06-23 14:04:12 -050059 def createFaces(self):
60 for ip in self.neighbors:
61 self.node.cmd("nfdc face create {}://{} permanent".format(self.faceType, ip))
62
Vince Lehman5d5a5662015-12-02 12:33:12 -060063 @staticmethod
64 def createKey(host, name, outputFile):
65 host.cmd("ndnsec-keygen {} > {}".format(name, outputFile))
66
67 @staticmethod
Ashlesh Gawandea80484e2017-10-17 15:52:23 -050068 def createCertificate(host, signer, keyFile, outputFile):
69 host.cmd("ndnsec-certgen -s {} -r {} > {}".format(signer, keyFile, outputFile))
Vince Lehman5d5a5662015-12-02 12:33:12 -060070
71 @staticmethod
72 def createKeysAndCertificates(net, workDir):
73 securityDir = "{}/security".format(workDir)
74
75 if not os.path.exists(securityDir):
Ashlesh Gawandef5f304b2016-06-16 16:42:41 -050076 os.mkdir(securityDir)
Vince Lehman5d5a5662015-12-02 12:33:12 -060077
78 # Create root certificate
Ashlesh Gawandef6a610b2017-02-21 14:48:08 -060079 rootName = NETWORK
Ashlesh Gawandea80484e2017-10-17 15:52:23 -050080 sh("ndnsec-keygen {}".format(rootName)) # Installs a self-signed cert into the system
81 sh("ndnsec-cert-dump -i {} > {}/root.cert".format(rootName, securityDir, securityDir))
Vince Lehman5d5a5662015-12-02 12:33:12 -060082
83 # Create necessary certificates for each site
84 for host in net.hosts:
85 nodeSecurityFolder = "{}/security".format(host.homeFolder)
86
Ashlesh Gawandef5f304b2016-06-16 16:42:41 -050087 host.cmd("mkdir -p %s" % nodeSecurityFolder)
88
89 # Create temp folders for remote nodes on this machine (localhost) to store site.key file
90 # from RemoteNodes
91 if not os.path.exists(nodeSecurityFolder) and isinstance(host, RemoteMixin) and host.isRemote:
92 os.makedirs(nodeSecurityFolder)
Vince Lehman5d5a5662015-12-02 12:33:12 -060093
94 shutil.copyfile("{}/root.cert".format(securityDir), "{}/root.cert".format(nodeSecurityFolder))
95
96 # Create site certificate
Ashlesh Gawandef6a610b2017-02-21 14:48:08 -060097 siteName = "{}{}-site".format(NETWORK, host.name)
Vince Lehman5d5a5662015-12-02 12:33:12 -060098 siteKeyFile = "{}/site.keys".format(nodeSecurityFolder)
99 siteCertFile = "{}/site.cert".format(nodeSecurityFolder)
100 Nlsr.createKey(host, siteName, siteKeyFile)
101
Ashlesh Gawandef5f304b2016-06-16 16:42:41 -0500102 # Copy siteKeyFile from remote for ndnsec-certgen
103 if isinstance(host, RemoteMixin) and host.isRemote:
104 login = "mininet@{}".format(host.server)
105 src = "{}:{}".format(login, siteKeyFile)
106 dst = siteKeyFile
107 scp(src, dst)
108
Vince Lehman5d5a5662015-12-02 12:33:12 -0600109 # Root key is in root namespace, must sign site key and then install on host
Ashlesh Gawandea80484e2017-10-17 15:52:23 -0500110 sh("ndnsec-certgen -s {} -r {} > {}".format(rootName, siteKeyFile, siteCertFile))
Ashlesh Gawandef5f304b2016-06-16 16:42:41 -0500111
112 # Copy root.cert and site.cert from localhost to remote host
113 if isinstance(host, RemoteMixin) and host.isRemote:
114 login = "mininet@{}".format(host.server)
115 src = "{}/site.cert".format(nodeSecurityFolder)
116 src2 = "{}/root.cert".format(nodeSecurityFolder)
117 dst = "{}:/tmp/".format(login)
118 scp(src, src2, dst)
119 host.cmd("mv /tmp/*.cert {}".format(nodeSecurityFolder))
120
Vince Lehman5d5a5662015-12-02 12:33:12 -0600121 host.cmd("ndnsec-cert-install -f {}".format(siteCertFile))
122
Ashlesh Gawande3bed4832017-02-08 18:17:31 -0600123 # Create and install operator certificate
Vince Lehman5d5a5662015-12-02 12:33:12 -0600124 opName = "{}/%C1.Operator/op".format(siteName)
125 opKeyFile = "{}/op.keys".format(nodeSecurityFolder)
126 opCertFile = "{}/op.cert".format(nodeSecurityFolder)
127 Nlsr.createKey(host, opName, opKeyFile)
Ashlesh Gawandea80484e2017-10-17 15:52:23 -0500128 Nlsr.createCertificate(host, siteName, opKeyFile, opCertFile)
Ashlesh Gawande3bed4832017-02-08 18:17:31 -0600129 host.cmd("ndnsec-cert-install -f {}".format(opCertFile))
Vince Lehman5d5a5662015-12-02 12:33:12 -0600130
Ashlesh Gawande3bed4832017-02-08 18:17:31 -0600131 # Create and install router certificate
Vince Lehman5d5a5662015-12-02 12:33:12 -0600132 routerName = "{}/%C1.Router/cs/{}".format(siteName, host.name)
133 routerKeyFile = "{}/router.keys".format(nodeSecurityFolder)
134 routerCertFile = "{}/router.cert".format(nodeSecurityFolder)
135 Nlsr.createKey(host, routerName, routerKeyFile)
Ashlesh Gawandea80484e2017-10-17 15:52:23 -0500136 Nlsr.createCertificate(host, opName, routerKeyFile, routerCertFile)
Ashlesh Gawande3bed4832017-02-08 18:17:31 -0600137 host.cmd("ndnsec-cert-install -f {}".format(routerCertFile))
Vince Lehman5d5a5662015-12-02 12:33:12 -0600138
ashuef3490b2015-02-17 11:01:04 -0600139class NlsrConfigGenerator:
140
141 ROUTING_LINK_STATE = "ls"
142 ROUTING_HYPERBOLIC = "hr"
143
Ashlesh Gawande708fcca2017-06-23 14:04:12 -0500144 def __init__(self, node, isSecurityEnabled, faceType):
ashuef3490b2015-02-17 11:01:04 -0600145 self.node = node
Vince Lehman5d5a5662015-12-02 12:33:12 -0600146 self.isSecurityEnabled = isSecurityEnabled
Ashlesh Gawande708fcca2017-06-23 14:04:12 -0500147 self.faceType = faceType
Saurab Dulal7a6978e2017-11-29 10:50:09 -0600148 self.infocmd = "infoedit -f nlsr.conf"
ashuef3490b2015-02-17 11:01:04 -0600149
150 parameters = node.nlsrParameters
151
152 self.nFaces = parameters.get("max-faces-per-prefix", 3)
153 self.hyperbolicState = parameters.get("hyperbolic-state", "off")
154 self.hyperRadius = parameters.get("radius", 0.0)
155 self.hyperAngle = parameters.get("angle", 0.0)
Ashlesh Gawande708fcca2017-06-23 14:04:12 -0500156 self.neighborIPs = []
Saurab Dulal7a6978e2017-11-29 10:50:09 -0600157 self.node.cmd("sudo cp /usr/local/etc/ndn/nlsr.conf.sample nlsr.conf")
ashuef3490b2015-02-17 11:01:04 -0600158
159 def createConfigFile(self):
160
Saurab Dulal7a6978e2017-11-29 10:50:09 -0600161 self.__editGeneralSection()
162 self.__editNeighborsSection()
163 self.__editHyperbolicSection()
164 self.__editFibSection()
165 self.__editAdvertisingSection()
166 self.__editSecuritySection()
ashuef3490b2015-02-17 11:01:04 -0600167
Saurab Dulal7a6978e2017-11-29 10:50:09 -0600168 def __editGeneralSection(self):
ashuef3490b2015-02-17 11:01:04 -0600169
Saurab Dulal7a6978e2017-11-29 10:50:09 -0600170 self.node.cmd("{} -s general.network -v {}".format(self.infocmd, NETWORK))
171 self.node.cmd("{} -s general.site -v /{}-site".format(self.infocmd, self.node.name))
172 self.node.cmd("{} -s general.router -v /%C1.Router/cs/{}".format(self.infocmd, self.node.name))
Saurab Dulal7a6978e2017-11-29 10:50:09 -0600173 self.node.cmd("{} -s general.log-dir -v {}/log".format(self.infocmd, self.node.homeFolder))
174 self.node.cmd("{} -s general.seq-dir -v {}/log".format(self.infocmd, self.node.homeFolder))
ashuef3490b2015-02-17 11:01:04 -0600175
Saurab Dulal7a6978e2017-11-29 10:50:09 -0600176 def __editNeighborsSection(self):
ashuef3490b2015-02-17 11:01:04 -0600177
Saurab Dulal7a6978e2017-11-29 10:50:09 -0600178 self.node.cmd("{} -d neighbors.neighbor".format(self.infocmd))
ashuef3490b2015-02-17 11:01:04 -0600179 for intf in self.node.intfList():
180 link = intf.link
181 if link:
182 node1, node2 = link.intf1.node, link.intf2.node
183
184 if node1 == self.node:
185 other = node2
186 ip = other.IP(str(link.intf2))
187 else:
188 other = node1
189 ip = other.IP(str(link.intf1))
190
ashu7b6ba182015-04-17 15:02:37 -0500191 linkCost = intf.params.get("delay", "10ms").replace("ms", "")
ashuef3490b2015-02-17 11:01:04 -0600192
Ashlesh Gawande708fcca2017-06-23 14:04:12 -0500193 # To be used later to create faces
194 self.neighborIPs.append(ip)
195
Saurab Dulal7a6978e2017-11-29 10:50:09 -0600196 self.node.cmd("{} -a neighbors.neighbor \
Saurab Dulal5f8c7962018-03-09 12:54:38 -0600197 <<<\'name {}{}-site/%C1.Router/cs/{} face-uri {}://{}\n link-cost {}\'"
Saurab Dulal7a6978e2017-11-29 10:50:09 -0600198 .format(self.infocmd, NETWORK, other.name, other.name, self.faceType, ip, linkCost))
ashuef3490b2015-02-17 11:01:04 -0600199
Saurab Dulal7a6978e2017-11-29 10:50:09 -0600200 def __editHyperbolicSection(self):
ashuef3490b2015-02-17 11:01:04 -0600201
Saurab Dulal7a6978e2017-11-29 10:50:09 -0600202 self.node.cmd("{} -s hyperbolic.state -v {}".format(self.infocmd, self.hyperbolicState))
203 self.node.cmd("{} -s hyperbolic.radius -v {}".format(self.infocmd, self.hyperRadius))
204 self.node.cmd("{} -s hyperbolic.angle -v {}".format(self.infocmd, self.hyperAngle))
ashuef3490b2015-02-17 11:01:04 -0600205
Saurab Dulal7a6978e2017-11-29 10:50:09 -0600206 def __editFibSection(self):
ashuef3490b2015-02-17 11:01:04 -0600207
Saurab Dulal7a6978e2017-11-29 10:50:09 -0600208 self.node.cmd("{} -s fib.max-faces-per-prefix -v {}".format(self.infocmd, self.nFaces))
ashuef3490b2015-02-17 11:01:04 -0600209
Saurab Dulal7a6978e2017-11-29 10:50:09 -0600210 def __editAdvertisingSection(self):
ashuef3490b2015-02-17 11:01:04 -0600211
Saurab Dulal7a6978e2017-11-29 10:50:09 -0600212 self.node.cmd("{} -d advertising.prefix".format(self.infocmd))
213 self.node.cmd("{} -s advertising.prefix -v {}{}-site/{}"
214 .format(self.infocmd, NETWORK, self.node.name, self.node.name))
ashuef3490b2015-02-17 11:01:04 -0600215
Saurab Dulal7a6978e2017-11-29 10:50:09 -0600216 def __editSecuritySection(self):
ashuef3490b2015-02-17 11:01:04 -0600217
Saurab Dulal7a6978e2017-11-29 10:50:09 -0600218 self.node.cmd("{} -d security.cert-to-publish".format(self.infocmd))
Vince Lehman5d5a5662015-12-02 12:33:12 -0600219 if self.isSecurityEnabled is False:
Saurab Dulal7a6978e2017-11-29 10:50:09 -0600220 self.node.cmd("{} -s security.validator.trust-anchor.type -v any".format(self.infocmd))
221 self.node.cmd("{} -d security.validator.trust-anchor.file-name".format(self.infocmd))
222 self.node.cmd("{} -s security.prefix-update-validator.trust-anchor.type -v any".format(self.infocmd))
223 self.node.cmd("{} -d security.prefix-update-validator.trust-anchor.file-name".format(self.infocmd))
Vince Lehman5d5a5662015-12-02 12:33:12 -0600224 else:
Saurab Dulal7a6978e2017-11-29 10:50:09 -0600225 self.node.cmd("{} -s security.validator.trust-anchor.file-name -v security/root.cert".format(self.infocmd))
226 self.node.cmd("{} -s security.prefix-update-validator.trust-anchor.file-name -v security/site.cert".format(self.infocmd))
227 self.node.cmd("{} -p security.cert-to-publish -v security/site.cert".format(self.infocmd))
228 self.node.cmd("{} -p security.cert-to-publish -v security/op.cert".format(self.infocmd))
dmcoomes73caa522018-01-15 16:33:32 -0600229 self.node.cmd("{} -p security.cert-to-publish -v security/router.cert".format(self.infocmd))