blob: 2d727751dd846f03aa59fff803197c91d1457f58 [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):
Ashlesh Gawande59f86242017-05-05 09:45:18 -050055 NdnApplication.start(self, "nlsr -f {} > /dev/null 2>&1 &".format(self.confFile))
56 time.sleep(1)
ashuef3490b2015-02-17 11:01:04 -060057
Ashlesh Gawande708fcca2017-06-23 14:04:12 -050058 def createFaces(self):
59 for ip in self.neighbors:
60 self.node.cmd("nfdc face create {}://{} permanent".format(self.faceType, ip))
61
Vince Lehman5d5a5662015-12-02 12:33:12 -060062 @staticmethod
63 def createKey(host, name, outputFile):
64 host.cmd("ndnsec-keygen {} > {}".format(name, outputFile))
65
66 @staticmethod
Ashlesh Gawandea80484e2017-10-17 15:52:23 -050067 def createCertificate(host, signer, keyFile, outputFile):
68 host.cmd("ndnsec-certgen -s {} -r {} > {}".format(signer, keyFile, outputFile))
Vince Lehman5d5a5662015-12-02 12:33:12 -060069
70 @staticmethod
71 def createKeysAndCertificates(net, workDir):
72 securityDir = "{}/security".format(workDir)
73
74 if not os.path.exists(securityDir):
Ashlesh Gawandef5f304b2016-06-16 16:42:41 -050075 os.mkdir(securityDir)
Vince Lehman5d5a5662015-12-02 12:33:12 -060076
77 # Create root certificate
Ashlesh Gawandef6a610b2017-02-21 14:48:08 -060078 rootName = NETWORK
Ashlesh Gawandea80484e2017-10-17 15:52:23 -050079 sh("ndnsec-keygen {}".format(rootName)) # Installs a self-signed cert into the system
80 sh("ndnsec-cert-dump -i {} > {}/root.cert".format(rootName, securityDir, securityDir))
Vince Lehman5d5a5662015-12-02 12:33:12 -060081
82 # Create necessary certificates for each site
83 for host in net.hosts:
84 nodeSecurityFolder = "{}/security".format(host.homeFolder)
85
Ashlesh Gawandef5f304b2016-06-16 16:42:41 -050086 host.cmd("mkdir -p %s" % nodeSecurityFolder)
87
88 # Create temp folders for remote nodes on this machine (localhost) to store site.key file
89 # from RemoteNodes
90 if not os.path.exists(nodeSecurityFolder) and isinstance(host, RemoteMixin) and host.isRemote:
91 os.makedirs(nodeSecurityFolder)
Vince Lehman5d5a5662015-12-02 12:33:12 -060092
93 shutil.copyfile("{}/root.cert".format(securityDir), "{}/root.cert".format(nodeSecurityFolder))
94
95 # Create site certificate
Ashlesh Gawandef6a610b2017-02-21 14:48:08 -060096 siteName = "{}{}-site".format(NETWORK, host.name)
Vince Lehman5d5a5662015-12-02 12:33:12 -060097 siteKeyFile = "{}/site.keys".format(nodeSecurityFolder)
98 siteCertFile = "{}/site.cert".format(nodeSecurityFolder)
99 Nlsr.createKey(host, siteName, siteKeyFile)
100
Ashlesh Gawandef5f304b2016-06-16 16:42:41 -0500101 # Copy siteKeyFile from remote for ndnsec-certgen
102 if isinstance(host, RemoteMixin) and host.isRemote:
103 login = "mininet@{}".format(host.server)
104 src = "{}:{}".format(login, siteKeyFile)
105 dst = siteKeyFile
106 scp(src, dst)
107
Vince Lehman5d5a5662015-12-02 12:33:12 -0600108 # Root key is in root namespace, must sign site key and then install on host
Ashlesh Gawandea80484e2017-10-17 15:52:23 -0500109 sh("ndnsec-certgen -s {} -r {} > {}".format(rootName, siteKeyFile, siteCertFile))
Ashlesh Gawandef5f304b2016-06-16 16:42:41 -0500110
111 # Copy root.cert and site.cert from localhost to remote host
112 if isinstance(host, RemoteMixin) and host.isRemote:
113 login = "mininet@{}".format(host.server)
114 src = "{}/site.cert".format(nodeSecurityFolder)
115 src2 = "{}/root.cert".format(nodeSecurityFolder)
116 dst = "{}:/tmp/".format(login)
117 scp(src, src2, dst)
118 host.cmd("mv /tmp/*.cert {}".format(nodeSecurityFolder))
119
Vince Lehman5d5a5662015-12-02 12:33:12 -0600120 host.cmd("ndnsec-cert-install -f {}".format(siteCertFile))
121
Ashlesh Gawande3bed4832017-02-08 18:17:31 -0600122 # Create and install operator certificate
Vince Lehman5d5a5662015-12-02 12:33:12 -0600123 opName = "{}/%C1.Operator/op".format(siteName)
124 opKeyFile = "{}/op.keys".format(nodeSecurityFolder)
125 opCertFile = "{}/op.cert".format(nodeSecurityFolder)
126 Nlsr.createKey(host, opName, opKeyFile)
Ashlesh Gawandea80484e2017-10-17 15:52:23 -0500127 Nlsr.createCertificate(host, siteName, opKeyFile, opCertFile)
Ashlesh Gawande3bed4832017-02-08 18:17:31 -0600128 host.cmd("ndnsec-cert-install -f {}".format(opCertFile))
Vince Lehman5d5a5662015-12-02 12:33:12 -0600129
Ashlesh Gawande3bed4832017-02-08 18:17:31 -0600130 # Create and install router certificate
Vince Lehman5d5a5662015-12-02 12:33:12 -0600131 routerName = "{}/%C1.Router/cs/{}".format(siteName, host.name)
132 routerKeyFile = "{}/router.keys".format(nodeSecurityFolder)
133 routerCertFile = "{}/router.cert".format(nodeSecurityFolder)
134 Nlsr.createKey(host, routerName, routerKeyFile)
Ashlesh Gawandea80484e2017-10-17 15:52:23 -0500135 Nlsr.createCertificate(host, opName, routerKeyFile, routerCertFile)
Ashlesh Gawande3bed4832017-02-08 18:17:31 -0600136 host.cmd("ndnsec-cert-install -f {}".format(routerCertFile))
Vince Lehman5d5a5662015-12-02 12:33:12 -0600137
ashuef3490b2015-02-17 11:01:04 -0600138class NlsrConfigGenerator:
139
140 ROUTING_LINK_STATE = "ls"
141 ROUTING_HYPERBOLIC = "hr"
142
Ashlesh Gawande708fcca2017-06-23 14:04:12 -0500143 def __init__(self, node, isSecurityEnabled, faceType):
ashuef3490b2015-02-17 11:01:04 -0600144 self.node = node
Vince Lehman5d5a5662015-12-02 12:33:12 -0600145 self.isSecurityEnabled = isSecurityEnabled
Ashlesh Gawande708fcca2017-06-23 14:04:12 -0500146 self.faceType = faceType
Saurab Dulal7a6978e2017-11-29 10:50:09 -0600147 self.infocmd = "infoedit -f nlsr.conf"
ashuef3490b2015-02-17 11:01:04 -0600148
149 parameters = node.nlsrParameters
150
151 self.nFaces = parameters.get("max-faces-per-prefix", 3)
152 self.hyperbolicState = parameters.get("hyperbolic-state", "off")
153 self.hyperRadius = parameters.get("radius", 0.0)
154 self.hyperAngle = parameters.get("angle", 0.0)
Ashlesh Gawandec3ed2b92015-07-01 12:58:08 -0500155 self.logLevel = parameters.get("nlsr-log-level", "DEBUG")
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))
173 self.node.cmd("{} -s general.log-level -v {}".format(self.infocmd, self.logLevel))
174 self.node.cmd("{} -s general.log-dir -v {}/log".format(self.infocmd, self.node.homeFolder))
175 self.node.cmd("{} -s general.seq-dir -v {}/log".format(self.infocmd, self.node.homeFolder))
ashuef3490b2015-02-17 11:01:04 -0600176
Saurab Dulal7a6978e2017-11-29 10:50:09 -0600177 def __editNeighborsSection(self):
ashuef3490b2015-02-17 11:01:04 -0600178
Saurab Dulal7a6978e2017-11-29 10:50:09 -0600179 self.node.cmd("{} -d neighbors.neighbor".format(self.infocmd))
ashuef3490b2015-02-17 11:01:04 -0600180 for intf in self.node.intfList():
181 link = intf.link
182 if link:
183 node1, node2 = link.intf1.node, link.intf2.node
184
185 if node1 == self.node:
186 other = node2
187 ip = other.IP(str(link.intf2))
188 else:
189 other = node1
190 ip = other.IP(str(link.intf1))
191
ashu7b6ba182015-04-17 15:02:37 -0500192 linkCost = intf.params.get("delay", "10ms").replace("ms", "")
ashuef3490b2015-02-17 11:01:04 -0600193
Ashlesh Gawande708fcca2017-06-23 14:04:12 -0500194 # To be used later to create faces
195 self.neighborIPs.append(ip)
196
Saurab Dulal7a6978e2017-11-29 10:50:09 -0600197 self.node.cmd("{} -a neighbors.neighbor \
198 <<<\'name {}{}-site/%C1.Router/cs/{} face-uri {}://{}\n cost {}\'"
199 .format(self.infocmd, NETWORK, other.name, other.name, self.faceType, ip, linkCost))
ashuef3490b2015-02-17 11:01:04 -0600200
Saurab Dulal7a6978e2017-11-29 10:50:09 -0600201 def __editHyperbolicSection(self):
ashuef3490b2015-02-17 11:01:04 -0600202
Saurab Dulal7a6978e2017-11-29 10:50:09 -0600203 self.node.cmd("{} -s hyperbolic.state -v {}".format(self.infocmd, self.hyperbolicState))
204 self.node.cmd("{} -s hyperbolic.radius -v {}".format(self.infocmd, self.hyperRadius))
205 self.node.cmd("{} -s hyperbolic.angle -v {}".format(self.infocmd, self.hyperAngle))
ashuef3490b2015-02-17 11:01:04 -0600206
Saurab Dulal7a6978e2017-11-29 10:50:09 -0600207 def __editFibSection(self):
ashuef3490b2015-02-17 11:01:04 -0600208
Saurab Dulal7a6978e2017-11-29 10:50:09 -0600209 self.node.cmd("{} -s fib.max-faces-per-prefix -v {}".format(self.infocmd, self.nFaces))
ashuef3490b2015-02-17 11:01:04 -0600210
Saurab Dulal7a6978e2017-11-29 10:50:09 -0600211 def __editAdvertisingSection(self):
ashuef3490b2015-02-17 11:01:04 -0600212
Saurab Dulal7a6978e2017-11-29 10:50:09 -0600213 self.node.cmd("{} -d advertising.prefix".format(self.infocmd))
214 self.node.cmd("{} -s advertising.prefix -v {}{}-site/{}"
215 .format(self.infocmd, NETWORK, self.node.name, self.node.name))
ashuef3490b2015-02-17 11:01:04 -0600216
Saurab Dulal7a6978e2017-11-29 10:50:09 -0600217 def __editSecuritySection(self):
ashuef3490b2015-02-17 11:01:04 -0600218
Saurab Dulal7a6978e2017-11-29 10:50:09 -0600219 self.node.cmd("{} -d security.cert-to-publish".format(self.infocmd))
Vince Lehman5d5a5662015-12-02 12:33:12 -0600220 if self.isSecurityEnabled is False:
Saurab Dulal7a6978e2017-11-29 10:50:09 -0600221 self.node.cmd("{} -s security.validator.trust-anchor.type -v any".format(self.infocmd))
222 self.node.cmd("{} -d security.validator.trust-anchor.file-name".format(self.infocmd))
223 self.node.cmd("{} -s security.prefix-update-validator.trust-anchor.type -v any".format(self.infocmd))
224 self.node.cmd("{} -d security.prefix-update-validator.trust-anchor.file-name".format(self.infocmd))
Vince Lehman5d5a5662015-12-02 12:33:12 -0600225 else:
Saurab Dulal7a6978e2017-11-29 10:50:09 -0600226 self.node.cmd("{} -s security.validator.trust-anchor.file-name -v security/root.cert".format(self.infocmd))
227 self.node.cmd("{} -s security.prefix-update-validator.trust-anchor.file-name -v security/site.cert".format(self.infocmd))
228 self.node.cmd("{} -p security.cert-to-publish -v security/site.cert".format(self.infocmd))
229 self.node.cmd("{} -p security.cert-to-publish -v security/op.cert".format(self.infocmd))
230 self.node.cmd("{} -p security.cert-to-publish -v security/router.cert".format(self.infocmd))