Vince Lehman | b8b1806 | 2015-07-14 13:07:22 -0500 | [diff] [blame] | 1 | # -*- Mode:python; c-file-style:"gnu"; indent-tabs-mode:nil -*- */ |
| 2 | # |
Ashlesh Gawande | f5f304b | 2016-06-16 16:42:41 -0500 | [diff] [blame] | 3 | # Copyright (C) 2015-2017, The University of Memphis, |
Vince Lehman | 5d5a566 | 2015-12-02 12:33:12 -0600 | [diff] [blame] | 4 | # Arizona Board of Regents, |
| 5 | # Regents of the University of California. |
Vince Lehman | b8b1806 | 2015-07-14 13:07:22 -0500 | [diff] [blame] | 6 | # |
| 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 Lehman | 5d5a566 | 2015-12-02 12:33:12 -0600 | [diff] [blame] | 24 | from mininet.clean import sh |
Ashlesh Gawande | f5f304b | 2016-06-16 16:42:41 -0500 | [diff] [blame] | 25 | from mininet.examples.cluster import RemoteMixin |
Vince Lehman | 5d5a566 | 2015-12-02 12:33:12 -0600 | [diff] [blame] | 26 | |
Ashlesh Gawande | 792c6aa | 2015-07-10 12:18:36 -0500 | [diff] [blame] | 27 | from ndn.ndn_application import NdnApplication |
Ashlesh Gawande | f5f304b | 2016-06-16 16:42:41 -0500 | [diff] [blame] | 28 | from ndn.util import ssh, scp |
ashu | ef3490b | 2015-02-17 11:01:04 -0600 | [diff] [blame] | 29 | |
Vince Lehman | 5d5a566 | 2015-12-02 12:33:12 -0600 | [diff] [blame] | 30 | import shutil |
Ashlesh Gawande | f5f304b | 2016-06-16 16:42:41 -0500 | [diff] [blame] | 31 | import os |
Vince Lehman | 5d5a566 | 2015-12-02 12:33:12 -0600 | [diff] [blame] | 32 | import textwrap |
Ashlesh Gawande | f5f304b | 2016-06-16 16:42:41 -0500 | [diff] [blame] | 33 | from subprocess import call |
Ashlesh Gawande | 59f8624 | 2017-05-05 09:45:18 -0500 | [diff] [blame] | 34 | import time |
Vince Lehman | 5d5a566 | 2015-12-02 12:33:12 -0600 | [diff] [blame] | 35 | |
Ashlesh Gawande | f6a610b | 2017-02-21 14:48:08 -0600 | [diff] [blame] | 36 | NETWORK="/ndn/" |
| 37 | |
Ashlesh Gawande | 792c6aa | 2015-07-10 12:18:36 -0500 | [diff] [blame] | 38 | class Nlsr(NdnApplication): |
Ashlesh Gawande | 708fcca | 2017-06-23 14:04:12 -0500 | [diff] [blame] | 39 | def __init__(self, node, neighbors, faceType): |
Ashlesh Gawande | 792c6aa | 2015-07-10 12:18:36 -0500 | [diff] [blame] | 40 | NdnApplication.__init__(self, node) |
Ashlesh Gawande | 708fcca | 2017-06-23 14:04:12 -0500 | [diff] [blame] | 41 | self.node = node |
| 42 | self.neighbors = neighbors |
| 43 | self.faceType = faceType |
ashu | ef3490b | 2015-02-17 11:01:04 -0600 | [diff] [blame] | 44 | self.routerName = "/%sC1.Router/cs/%s" % ('%', node.name) |
Ashlesh Gawande | 1b66369 | 2015-10-14 16:38:10 -0500 | [diff] [blame] | 45 | self.confFile = "%s/nlsr.conf" % node.homeFolder |
ashu | ef3490b | 2015-02-17 11:01:04 -0600 | [diff] [blame] | 46 | |
| 47 | # Make directory for log file |
Saurab Dulal | 7a6978e | 2017-11-29 10:50:09 -0600 | [diff] [blame] | 48 | self.logDir = "{}/log".format(node.homeFolder) |
| 49 | self.node.cmd("mkdir {}".format(self.logDir)) |
Ashlesh Gawande | 708fcca | 2017-06-23 14:04:12 -0500 | [diff] [blame] | 50 | |
| 51 | # Create faces in NFD |
| 52 | self.createFaces() |
ashu | ef3490b | 2015-02-17 11:01:04 -0600 | [diff] [blame] | 53 | |
ashu | ef3490b | 2015-02-17 11:01:04 -0600 | [diff] [blame] | 54 | def start(self): |
dmcoomes | 73caa52 | 2018-01-15 16:33:32 -0600 | [diff] [blame] | 55 | 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 Gawande | 59f8624 | 2017-05-05 09:45:18 -0500 | [diff] [blame] | 57 | time.sleep(1) |
ashu | ef3490b | 2015-02-17 11:01:04 -0600 | [diff] [blame] | 58 | |
Ashlesh Gawande | 708fcca | 2017-06-23 14:04:12 -0500 | [diff] [blame] | 59 | def createFaces(self): |
| 60 | for ip in self.neighbors: |
| 61 | self.node.cmd("nfdc face create {}://{} permanent".format(self.faceType, ip)) |
| 62 | |
Vince Lehman | 5d5a566 | 2015-12-02 12:33:12 -0600 | [diff] [blame] | 63 | @staticmethod |
| 64 | def createKey(host, name, outputFile): |
| 65 | host.cmd("ndnsec-keygen {} > {}".format(name, outputFile)) |
| 66 | |
| 67 | @staticmethod |
Ashlesh Gawande | a80484e | 2017-10-17 15:52:23 -0500 | [diff] [blame] | 68 | def createCertificate(host, signer, keyFile, outputFile): |
| 69 | host.cmd("ndnsec-certgen -s {} -r {} > {}".format(signer, keyFile, outputFile)) |
Vince Lehman | 5d5a566 | 2015-12-02 12:33:12 -0600 | [diff] [blame] | 70 | |
| 71 | @staticmethod |
| 72 | def createKeysAndCertificates(net, workDir): |
| 73 | securityDir = "{}/security".format(workDir) |
| 74 | |
| 75 | if not os.path.exists(securityDir): |
Ashlesh Gawande | f5f304b | 2016-06-16 16:42:41 -0500 | [diff] [blame] | 76 | os.mkdir(securityDir) |
Vince Lehman | 5d5a566 | 2015-12-02 12:33:12 -0600 | [diff] [blame] | 77 | |
| 78 | # Create root certificate |
Ashlesh Gawande | f6a610b | 2017-02-21 14:48:08 -0600 | [diff] [blame] | 79 | rootName = NETWORK |
Ashlesh Gawande | a80484e | 2017-10-17 15:52:23 -0500 | [diff] [blame] | 80 | 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 Lehman | 5d5a566 | 2015-12-02 12:33:12 -0600 | [diff] [blame] | 82 | |
| 83 | # Create necessary certificates for each site |
| 84 | for host in net.hosts: |
| 85 | nodeSecurityFolder = "{}/security".format(host.homeFolder) |
| 86 | |
Ashlesh Gawande | f5f304b | 2016-06-16 16:42:41 -0500 | [diff] [blame] | 87 | 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 Lehman | 5d5a566 | 2015-12-02 12:33:12 -0600 | [diff] [blame] | 93 | |
| 94 | shutil.copyfile("{}/root.cert".format(securityDir), "{}/root.cert".format(nodeSecurityFolder)) |
| 95 | |
| 96 | # Create site certificate |
Ashlesh Gawande | f6a610b | 2017-02-21 14:48:08 -0600 | [diff] [blame] | 97 | siteName = "{}{}-site".format(NETWORK, host.name) |
Vince Lehman | 5d5a566 | 2015-12-02 12:33:12 -0600 | [diff] [blame] | 98 | siteKeyFile = "{}/site.keys".format(nodeSecurityFolder) |
| 99 | siteCertFile = "{}/site.cert".format(nodeSecurityFolder) |
| 100 | Nlsr.createKey(host, siteName, siteKeyFile) |
| 101 | |
Ashlesh Gawande | f5f304b | 2016-06-16 16:42:41 -0500 | [diff] [blame] | 102 | # 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 Lehman | 5d5a566 | 2015-12-02 12:33:12 -0600 | [diff] [blame] | 109 | # Root key is in root namespace, must sign site key and then install on host |
Ashlesh Gawande | a80484e | 2017-10-17 15:52:23 -0500 | [diff] [blame] | 110 | sh("ndnsec-certgen -s {} -r {} > {}".format(rootName, siteKeyFile, siteCertFile)) |
Ashlesh Gawande | f5f304b | 2016-06-16 16:42:41 -0500 | [diff] [blame] | 111 | |
| 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 Lehman | 5d5a566 | 2015-12-02 12:33:12 -0600 | [diff] [blame] | 121 | host.cmd("ndnsec-cert-install -f {}".format(siteCertFile)) |
| 122 | |
Ashlesh Gawande | 3bed483 | 2017-02-08 18:17:31 -0600 | [diff] [blame] | 123 | # Create and install operator certificate |
Vince Lehman | 5d5a566 | 2015-12-02 12:33:12 -0600 | [diff] [blame] | 124 | 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 Gawande | a80484e | 2017-10-17 15:52:23 -0500 | [diff] [blame] | 128 | Nlsr.createCertificate(host, siteName, opKeyFile, opCertFile) |
Ashlesh Gawande | 3bed483 | 2017-02-08 18:17:31 -0600 | [diff] [blame] | 129 | host.cmd("ndnsec-cert-install -f {}".format(opCertFile)) |
Vince Lehman | 5d5a566 | 2015-12-02 12:33:12 -0600 | [diff] [blame] | 130 | |
Ashlesh Gawande | 3bed483 | 2017-02-08 18:17:31 -0600 | [diff] [blame] | 131 | # Create and install router certificate |
Vince Lehman | 5d5a566 | 2015-12-02 12:33:12 -0600 | [diff] [blame] | 132 | 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 Gawande | a80484e | 2017-10-17 15:52:23 -0500 | [diff] [blame] | 136 | Nlsr.createCertificate(host, opName, routerKeyFile, routerCertFile) |
Ashlesh Gawande | 3bed483 | 2017-02-08 18:17:31 -0600 | [diff] [blame] | 137 | host.cmd("ndnsec-cert-install -f {}".format(routerCertFile)) |
Vince Lehman | 5d5a566 | 2015-12-02 12:33:12 -0600 | [diff] [blame] | 138 | |
ashu | ef3490b | 2015-02-17 11:01:04 -0600 | [diff] [blame] | 139 | class NlsrConfigGenerator: |
| 140 | |
| 141 | ROUTING_LINK_STATE = "ls" |
| 142 | ROUTING_HYPERBOLIC = "hr" |
| 143 | |
Ashlesh Gawande | 708fcca | 2017-06-23 14:04:12 -0500 | [diff] [blame] | 144 | def __init__(self, node, isSecurityEnabled, faceType): |
ashu | ef3490b | 2015-02-17 11:01:04 -0600 | [diff] [blame] | 145 | self.node = node |
Vince Lehman | 5d5a566 | 2015-12-02 12:33:12 -0600 | [diff] [blame] | 146 | self.isSecurityEnabled = isSecurityEnabled |
Ashlesh Gawande | 708fcca | 2017-06-23 14:04:12 -0500 | [diff] [blame] | 147 | self.faceType = faceType |
Saurab Dulal | 7a6978e | 2017-11-29 10:50:09 -0600 | [diff] [blame] | 148 | self.infocmd = "infoedit -f nlsr.conf" |
ashu | ef3490b | 2015-02-17 11:01:04 -0600 | [diff] [blame] | 149 | |
| 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 Gawande | 708fcca | 2017-06-23 14:04:12 -0500 | [diff] [blame] | 156 | self.neighborIPs = [] |
Saurab Dulal | 7a6978e | 2017-11-29 10:50:09 -0600 | [diff] [blame] | 157 | self.node.cmd("sudo cp /usr/local/etc/ndn/nlsr.conf.sample nlsr.conf") |
ashu | ef3490b | 2015-02-17 11:01:04 -0600 | [diff] [blame] | 158 | |
| 159 | def createConfigFile(self): |
| 160 | |
Saurab Dulal | 7a6978e | 2017-11-29 10:50:09 -0600 | [diff] [blame] | 161 | self.__editGeneralSection() |
| 162 | self.__editNeighborsSection() |
| 163 | self.__editHyperbolicSection() |
| 164 | self.__editFibSection() |
| 165 | self.__editAdvertisingSection() |
| 166 | self.__editSecuritySection() |
ashu | ef3490b | 2015-02-17 11:01:04 -0600 | [diff] [blame] | 167 | |
Saurab Dulal | 7a6978e | 2017-11-29 10:50:09 -0600 | [diff] [blame] | 168 | def __editGeneralSection(self): |
ashu | ef3490b | 2015-02-17 11:01:04 -0600 | [diff] [blame] | 169 | |
Saurab Dulal | 7a6978e | 2017-11-29 10:50:09 -0600 | [diff] [blame] | 170 | 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 Dulal | 7a6978e | 2017-11-29 10:50:09 -0600 | [diff] [blame] | 173 | 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)) |
ashu | ef3490b | 2015-02-17 11:01:04 -0600 | [diff] [blame] | 175 | |
Saurab Dulal | 7a6978e | 2017-11-29 10:50:09 -0600 | [diff] [blame] | 176 | def __editNeighborsSection(self): |
ashu | ef3490b | 2015-02-17 11:01:04 -0600 | [diff] [blame] | 177 | |
Saurab Dulal | 7a6978e | 2017-11-29 10:50:09 -0600 | [diff] [blame] | 178 | self.node.cmd("{} -d neighbors.neighbor".format(self.infocmd)) |
ashu | ef3490b | 2015-02-17 11:01:04 -0600 | [diff] [blame] | 179 | 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 | |
ashu | 7b6ba18 | 2015-04-17 15:02:37 -0500 | [diff] [blame] | 191 | linkCost = intf.params.get("delay", "10ms").replace("ms", "") |
ashu | ef3490b | 2015-02-17 11:01:04 -0600 | [diff] [blame] | 192 | |
Ashlesh Gawande | 708fcca | 2017-06-23 14:04:12 -0500 | [diff] [blame] | 193 | # To be used later to create faces |
| 194 | self.neighborIPs.append(ip) |
| 195 | |
Saurab Dulal | 7a6978e | 2017-11-29 10:50:09 -0600 | [diff] [blame] | 196 | self.node.cmd("{} -a neighbors.neighbor \ |
| 197 | <<<\'name {}{}-site/%C1.Router/cs/{} face-uri {}://{}\n cost {}\'" |
| 198 | .format(self.infocmd, NETWORK, other.name, other.name, self.faceType, ip, linkCost)) |
ashu | ef3490b | 2015-02-17 11:01:04 -0600 | [diff] [blame] | 199 | |
Saurab Dulal | 7a6978e | 2017-11-29 10:50:09 -0600 | [diff] [blame] | 200 | def __editHyperbolicSection(self): |
ashu | ef3490b | 2015-02-17 11:01:04 -0600 | [diff] [blame] | 201 | |
Saurab Dulal | 7a6978e | 2017-11-29 10:50:09 -0600 | [diff] [blame] | 202 | 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)) |
ashu | ef3490b | 2015-02-17 11:01:04 -0600 | [diff] [blame] | 205 | |
Saurab Dulal | 7a6978e | 2017-11-29 10:50:09 -0600 | [diff] [blame] | 206 | def __editFibSection(self): |
ashu | ef3490b | 2015-02-17 11:01:04 -0600 | [diff] [blame] | 207 | |
Saurab Dulal | 7a6978e | 2017-11-29 10:50:09 -0600 | [diff] [blame] | 208 | self.node.cmd("{} -s fib.max-faces-per-prefix -v {}".format(self.infocmd, self.nFaces)) |
ashu | ef3490b | 2015-02-17 11:01:04 -0600 | [diff] [blame] | 209 | |
Saurab Dulal | 7a6978e | 2017-11-29 10:50:09 -0600 | [diff] [blame] | 210 | def __editAdvertisingSection(self): |
ashu | ef3490b | 2015-02-17 11:01:04 -0600 | [diff] [blame] | 211 | |
Saurab Dulal | 7a6978e | 2017-11-29 10:50:09 -0600 | [diff] [blame] | 212 | 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)) |
ashu | ef3490b | 2015-02-17 11:01:04 -0600 | [diff] [blame] | 215 | |
Saurab Dulal | 7a6978e | 2017-11-29 10:50:09 -0600 | [diff] [blame] | 216 | def __editSecuritySection(self): |
ashu | ef3490b | 2015-02-17 11:01:04 -0600 | [diff] [blame] | 217 | |
Saurab Dulal | 7a6978e | 2017-11-29 10:50:09 -0600 | [diff] [blame] | 218 | self.node.cmd("{} -d security.cert-to-publish".format(self.infocmd)) |
Vince Lehman | 5d5a566 | 2015-12-02 12:33:12 -0600 | [diff] [blame] | 219 | if self.isSecurityEnabled is False: |
Saurab Dulal | 7a6978e | 2017-11-29 10:50:09 -0600 | [diff] [blame] | 220 | 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 Lehman | 5d5a566 | 2015-12-02 12:33:12 -0600 | [diff] [blame] | 224 | else: |
Saurab Dulal | 7a6978e | 2017-11-29 10:50:09 -0600 | [diff] [blame] | 225 | 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)) |
dmcoomes | 73caa52 | 2018-01-15 16:33:32 -0600 | [diff] [blame] | 229 | self.node.cmd("{} -p security.cert-to-publish -v security/router.cert".format(self.infocmd)) |