Hyunsun Moon | 28b358a | 2016-11-28 13:23:05 -0800 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 16 | package org.opencord.cordvtn.impl; |
| 17 | |
Hyunsun Moon | bcf4925 | 2017-02-21 22:28:41 +0900 | [diff] [blame] | 18 | import com.google.common.base.Strings; |
Hyunsun Moon | 28b358a | 2016-11-28 13:23:05 -0800 | [diff] [blame] | 19 | import com.google.common.collect.Lists; |
Hyunsun Moon | 0984cbd | 2016-12-01 17:34:11 -0800 | [diff] [blame] | 20 | import com.google.common.primitives.Bytes; |
Hyunsun Moon | 28b358a | 2016-11-28 13:23:05 -0800 | [diff] [blame] | 21 | import org.apache.felix.scr.annotations.Activate; |
| 22 | import org.apache.felix.scr.annotations.Component; |
| 23 | import org.apache.felix.scr.annotations.Deactivate; |
Hyunsun Moon | bcf4925 | 2017-02-21 22:28:41 +0900 | [diff] [blame] | 24 | import org.apache.felix.scr.annotations.Modified; |
| 25 | import org.apache.felix.scr.annotations.Property; |
Hyunsun Moon | 28b358a | 2016-11-28 13:23:05 -0800 | [diff] [blame] | 26 | import org.apache.felix.scr.annotations.Reference; |
| 27 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 28 | import org.onlab.packet.DHCP; |
| 29 | import org.onlab.packet.DHCPOption; |
| 30 | import org.onlab.packet.DHCPPacketType; |
| 31 | import org.onlab.packet.Ethernet; |
| 32 | import org.onlab.packet.IPv4; |
| 33 | import org.onlab.packet.Ip4Address; |
Hyunsun Moon | 0984cbd | 2016-12-01 17:34:11 -0800 | [diff] [blame] | 34 | import org.onlab.packet.IpPrefix; |
Hyunsun Moon | 28b358a | 2016-11-28 13:23:05 -0800 | [diff] [blame] | 35 | import org.onlab.packet.MacAddress; |
| 36 | import org.onlab.packet.TpPort; |
| 37 | import org.onlab.packet.UDP; |
Hyunsun Moon | bcf4925 | 2017-02-21 22:28:41 +0900 | [diff] [blame] | 38 | import org.onlab.util.Tools; |
| 39 | import org.onosproject.cfg.ComponentConfigService; |
Hyunsun Moon | 28b358a | 2016-11-28 13:23:05 -0800 | [diff] [blame] | 40 | import org.onosproject.core.ApplicationId; |
| 41 | import org.onosproject.core.CoreService; |
| 42 | import org.onosproject.net.ConnectPoint; |
| 43 | import org.onosproject.net.Host; |
| 44 | import org.onosproject.net.HostId; |
Hyunsun Moon | 28b358a | 2016-11-28 13:23:05 -0800 | [diff] [blame] | 45 | import org.onosproject.net.flow.DefaultTrafficSelector; |
| 46 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
| 47 | import org.onosproject.net.flow.TrafficSelector; |
| 48 | import org.onosproject.net.flow.TrafficTreatment; |
| 49 | import org.onosproject.net.host.HostService; |
| 50 | import org.onosproject.net.packet.DefaultOutboundPacket; |
| 51 | import org.onosproject.net.packet.PacketContext; |
| 52 | import org.onosproject.net.packet.PacketPriority; |
| 53 | import org.onosproject.net.packet.PacketProcessor; |
| 54 | import org.onosproject.net.packet.PacketService; |
| 55 | import org.opencord.cordvtn.api.Constants; |
Hyunsun Moon | 187bf53 | 2017-01-19 10:57:40 +0900 | [diff] [blame] | 56 | import org.opencord.cordvtn.api.core.Instance; |
| 57 | import org.opencord.cordvtn.api.core.ServiceNetworkService; |
| 58 | import org.opencord.cordvtn.api.net.NetworkId; |
| 59 | import org.opencord.cordvtn.api.net.ServiceNetwork; |
Hyunsun Moon | bcf4925 | 2017-02-21 22:28:41 +0900 | [diff] [blame] | 60 | import org.osgi.service.component.ComponentContext; |
Hyunsun Moon | 28b358a | 2016-11-28 13:23:05 -0800 | [diff] [blame] | 61 | import org.slf4j.Logger; |
| 62 | |
| 63 | import java.nio.ByteBuffer; |
Hyunsun Moon | 0984cbd | 2016-12-01 17:34:11 -0800 | [diff] [blame] | 64 | import java.util.Arrays; |
Hyunsun Moon | bcf4925 | 2017-02-21 22:28:41 +0900 | [diff] [blame] | 65 | import java.util.Dictionary; |
Hyunsun Moon | 28b358a | 2016-11-28 13:23:05 -0800 | [diff] [blame] | 66 | import java.util.List; |
Hyunsun Moon | 0984cbd | 2016-12-01 17:34:11 -0800 | [diff] [blame] | 67 | import java.util.Objects; |
| 68 | import java.util.Set; |
| 69 | import java.util.stream.Collectors; |
Hyunsun Moon | 28b358a | 2016-11-28 13:23:05 -0800 | [diff] [blame] | 70 | |
Hyunsun Moon | 28b358a | 2016-11-28 13:23:05 -0800 | [diff] [blame] | 71 | import static org.onlab.packet.DHCP.DHCPOptionCode.*; |
| 72 | import static org.onlab.packet.DHCPPacketType.DHCPACK; |
| 73 | import static org.onlab.packet.DHCPPacketType.DHCPOFFER; |
Hyunsun Moon | bcf4925 | 2017-02-21 22:28:41 +0900 | [diff] [blame] | 74 | import static org.opencord.cordvtn.api.Constants.DEFAULT_GATEWAY_MAC_STR; |
Hyunsun Moon | 187bf53 | 2017-01-19 10:57:40 +0900 | [diff] [blame] | 75 | import static org.opencord.cordvtn.api.net.ServiceNetwork.DependencyType.BIDIRECTIONAL; |
| 76 | import static org.opencord.cordvtn.api.net.ServiceNetwork.NetworkType.MANAGEMENT_HOST; |
| 77 | import static org.opencord.cordvtn.api.net.ServiceNetwork.NetworkType.MANAGEMENT_LOCAL; |
Hyunsun Moon | 28b358a | 2016-11-28 13:23:05 -0800 | [diff] [blame] | 78 | import static org.slf4j.LoggerFactory.getLogger; |
| 79 | |
| 80 | /** |
| 81 | * Handles DHCP requests for the virtual instances. |
| 82 | */ |
| 83 | @Component(immediate = true) |
| 84 | public class CordVtnDhcpProxy { |
| 85 | |
| 86 | protected final Logger log = getLogger(getClass()); |
| 87 | |
Hyunsun Moon | bcf4925 | 2017-02-21 22:28:41 +0900 | [diff] [blame] | 88 | private static final String DHCP_SERVER_MAC = "dhcpServerMac"; |
| 89 | |
Hyunsun Moon | 28b358a | 2016-11-28 13:23:05 -0800 | [diff] [blame] | 90 | private static final byte DHCP_OPTION_MTU = (byte) 26; |
Hyunsun Moon | 0984cbd | 2016-12-01 17:34:11 -0800 | [diff] [blame] | 91 | private static final byte DHCP_OPTION_CLASSLESS_STATIC_ROUTE = (byte) 121; |
Hyunsun Moon | bcf4925 | 2017-02-21 22:28:41 +0900 | [diff] [blame] | 92 | |
| 93 | private static final Ip4Address DEFAULT_DNS = Ip4Address.valueOf("8.8.8.8"); |
| 94 | private static final byte DEFAULT_PACKET_TTL = (byte) 127; |
Hyunsun Moon | 28b358a | 2016-11-28 13:23:05 -0800 | [diff] [blame] | 95 | private static final byte[] DHCP_DATA_LEASE_INFINITE = |
| 96 | ByteBuffer.allocate(4).putInt(-1).array(); |
| 97 | private static final byte[] DHCP_DATA_MTU_DEFAULT = |
| 98 | ByteBuffer.allocate(2).putShort((short) 1450).array(); |
| 99 | |
| 100 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 101 | protected CoreService coreService; |
| 102 | |
| 103 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Hyunsun Moon | bcf4925 | 2017-02-21 22:28:41 +0900 | [diff] [blame] | 104 | protected ComponentConfigService configService; |
Hyunsun Moon | 28b358a | 2016-11-28 13:23:05 -0800 | [diff] [blame] | 105 | |
| 106 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 107 | protected PacketService packetService; |
| 108 | |
| 109 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 110 | protected HostService hostService; |
| 111 | |
| 112 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Hyunsun Moon | 187bf53 | 2017-01-19 10:57:40 +0900 | [diff] [blame] | 113 | protected ServiceNetworkService snetService; |
Hyunsun Moon | 28b358a | 2016-11-28 13:23:05 -0800 | [diff] [blame] | 114 | |
Hyunsun Moon | bcf4925 | 2017-02-21 22:28:41 +0900 | [diff] [blame] | 115 | @Property(name = DHCP_SERVER_MAC, value = DEFAULT_GATEWAY_MAC_STR, |
| 116 | label = "Fake MAC address for DHCP server interface") |
| 117 | private String dhcpServerMac = DEFAULT_GATEWAY_MAC_STR; |
| 118 | |
Hyunsun Moon | 28b358a | 2016-11-28 13:23:05 -0800 | [diff] [blame] | 119 | private final PacketProcessor packetProcessor = new InternalPacketProcessor(); |
Hyunsun Moon | 28b358a | 2016-11-28 13:23:05 -0800 | [diff] [blame] | 120 | |
| 121 | private ApplicationId appId; |
Hyunsun Moon | 28b358a | 2016-11-28 13:23:05 -0800 | [diff] [blame] | 122 | |
| 123 | @Activate |
| 124 | protected void activate() { |
| 125 | appId = coreService.registerApplication(Constants.CORDVTN_APP_ID); |
Hyunsun Moon | bcf4925 | 2017-02-21 22:28:41 +0900 | [diff] [blame] | 126 | configService.registerProperties(getClass()); |
Hyunsun Moon | 28b358a | 2016-11-28 13:23:05 -0800 | [diff] [blame] | 127 | packetService.addProcessor(packetProcessor, PacketProcessor.director(0)); |
| 128 | requestPackets(); |
Hyunsun Moon | bcf4925 | 2017-02-21 22:28:41 +0900 | [diff] [blame] | 129 | |
Hyunsun Moon | 28b358a | 2016-11-28 13:23:05 -0800 | [diff] [blame] | 130 | log.info("Started"); |
| 131 | } |
| 132 | |
| 133 | @Deactivate |
| 134 | protected void deactivate() { |
| 135 | packetService.removeProcessor(packetProcessor); |
Hyunsun Moon | bcf4925 | 2017-02-21 22:28:41 +0900 | [diff] [blame] | 136 | configService.unregisterProperties(getClass(), false); |
Hyunsun Moon | 28b358a | 2016-11-28 13:23:05 -0800 | [diff] [blame] | 137 | cancelPackets(); |
Hyunsun Moon | bcf4925 | 2017-02-21 22:28:41 +0900 | [diff] [blame] | 138 | |
Hyunsun Moon | 28b358a | 2016-11-28 13:23:05 -0800 | [diff] [blame] | 139 | log.info("Stopped"); |
| 140 | } |
| 141 | |
Hyunsun Moon | bcf4925 | 2017-02-21 22:28:41 +0900 | [diff] [blame] | 142 | @Modified |
| 143 | protected void modified(ComponentContext context) { |
| 144 | Dictionary<?, ?> properties = context.getProperties(); |
| 145 | String updatedMac; |
| 146 | |
| 147 | updatedMac = Tools.get(properties, DHCP_SERVER_MAC); |
| 148 | if (!Strings.isNullOrEmpty(updatedMac) && !updatedMac.equals(dhcpServerMac)) { |
| 149 | dhcpServerMac = updatedMac; |
| 150 | } |
| 151 | |
| 152 | log.info("Modified"); |
| 153 | } |
| 154 | |
Hyunsun Moon | 28b358a | 2016-11-28 13:23:05 -0800 | [diff] [blame] | 155 | private void requestPackets() { |
| 156 | TrafficSelector selector = DefaultTrafficSelector.builder() |
| 157 | .matchEthType(Ethernet.TYPE_IPV4) |
| 158 | .matchIPProtocol(IPv4.PROTOCOL_UDP) |
| 159 | .matchUdpDst(TpPort.tpPort(UDP.DHCP_SERVER_PORT)) |
| 160 | .matchUdpSrc(TpPort.tpPort(UDP.DHCP_CLIENT_PORT)) |
| 161 | .build(); |
| 162 | packetService.requestPackets(selector, PacketPriority.CONTROL, appId); |
| 163 | } |
| 164 | |
| 165 | private void cancelPackets() { |
| 166 | TrafficSelector selector = DefaultTrafficSelector.builder() |
| 167 | .matchEthType(Ethernet.TYPE_IPV4) |
| 168 | .matchIPProtocol(IPv4.PROTOCOL_UDP) |
| 169 | .matchUdpDst(TpPort.tpPort(UDP.DHCP_SERVER_PORT)) |
| 170 | .matchUdpSrc(TpPort.tpPort(UDP.DHCP_CLIENT_PORT)) |
| 171 | .build(); |
| 172 | packetService.cancelPackets(selector, PacketPriority.CONTROL, appId); |
| 173 | } |
| 174 | |
| 175 | private class InternalPacketProcessor implements PacketProcessor { |
| 176 | |
| 177 | @Override |
| 178 | public void process(PacketContext context) { |
| 179 | if (context.isHandled()) { |
| 180 | return; |
| 181 | } |
| 182 | |
| 183 | Ethernet ethPacket = context.inPacket().parsed(); |
| 184 | if (ethPacket == null || ethPacket.getEtherType() != Ethernet.TYPE_IPV4) { |
| 185 | return; |
| 186 | } |
| 187 | IPv4 ipv4Packet = (IPv4) ethPacket.getPayload(); |
| 188 | if (ipv4Packet.getProtocol() != IPv4.PROTOCOL_UDP) { |
| 189 | return; |
| 190 | } |
| 191 | UDP udpPacket = (UDP) ipv4Packet.getPayload(); |
| 192 | if (udpPacket.getDestinationPort() != UDP.DHCP_SERVER_PORT || |
| 193 | udpPacket.getSourcePort() != UDP.DHCP_CLIENT_PORT) { |
| 194 | return; |
| 195 | } |
| 196 | |
| 197 | DHCP dhcpPacket = (DHCP) udpPacket.getPayload(); |
| 198 | processDhcp(context, dhcpPacket); |
| 199 | } |
| 200 | |
| 201 | private void processDhcp(PacketContext context, DHCP dhcpPacket) { |
| 202 | if (dhcpPacket == null) { |
| 203 | log.trace("DHCP packet without payload received, do nothing"); |
| 204 | return; |
| 205 | } |
| 206 | |
| 207 | DHCPPacketType inPacketType = getPacketType(dhcpPacket); |
| 208 | if (inPacketType == null || dhcpPacket.getClientHardwareAddress() == null) { |
| 209 | log.trace("Malformed DHCP packet received, ignore it"); |
| 210 | return; |
| 211 | } |
| 212 | |
| 213 | MacAddress clientMac = MacAddress.valueOf(dhcpPacket.getClientHardwareAddress()); |
| 214 | Host reqHost = hostService.getHost(HostId.hostId(clientMac)); |
| 215 | if (reqHost == null) { |
| 216 | log.debug("DHCP packet from unknown host, ignore it"); |
| 217 | return; |
| 218 | } |
| 219 | |
| 220 | Instance reqInstance = Instance.of(reqHost); |
| 221 | Ethernet ethPacket = context.inPacket().parsed(); |
| 222 | switch (inPacketType) { |
| 223 | case DHCPDISCOVER: |
| 224 | log.trace("DHCP DISCOVER received from {}", reqHost.id()); |
| 225 | Ethernet discoverReply = buildReply( |
| 226 | ethPacket, |
| 227 | (byte) DHCPOFFER.getValue(), |
| 228 | reqInstance); |
| 229 | sendReply(context, discoverReply); |
| 230 | log.trace("DHCP OFFER({}) is sent to {}", |
| 231 | reqInstance.ipAddress(), reqHost.id()); |
| 232 | break; |
| 233 | case DHCPREQUEST: |
| 234 | log.trace("DHCP REQUEST received from {}", reqHost.id()); |
| 235 | Ethernet requestReply = buildReply( |
| 236 | ethPacket, |
| 237 | (byte) DHCPACK.getValue(), |
| 238 | reqInstance); |
| 239 | sendReply(context, requestReply); |
| 240 | log.trace("DHCP ACK({}) is sent to {}", |
| 241 | reqInstance.ipAddress(), reqHost.id()); |
| 242 | break; |
| 243 | case DHCPRELEASE: |
| 244 | log.trace("DHCP RELEASE received from {}", reqHost.id()); |
| 245 | // do nothing |
| 246 | break; |
| 247 | default: |
| 248 | break; |
| 249 | } |
| 250 | } |
| 251 | |
| 252 | private DHCPPacketType getPacketType(DHCP dhcpPacket) { |
| 253 | DHCPOption optType = dhcpPacket.getOption(OptionCode_MessageType); |
| 254 | if (optType == null) { |
| 255 | log.trace("DHCP packet with no message type, ignore it"); |
| 256 | return null; |
| 257 | } |
| 258 | |
| 259 | DHCPPacketType inPacketType = DHCPPacketType.getType(optType.getData()[0]); |
| 260 | if (inPacketType == null) { |
| 261 | log.trace("DHCP packet with no packet type, ignore it"); |
| 262 | } |
| 263 | return inPacketType; |
| 264 | } |
| 265 | |
| 266 | private Ethernet buildReply(Ethernet ethRequest, byte packetType, |
| 267 | Instance reqInstance) { |
Hyunsun Moon | 187bf53 | 2017-01-19 10:57:40 +0900 | [diff] [blame] | 268 | ServiceNetwork snet = snetService.serviceNetwork(reqInstance.netId()); |
| 269 | Ip4Address serverIp = snet.serviceIp().getIp4Address(); |
Hyunsun Moon | 28b358a | 2016-11-28 13:23:05 -0800 | [diff] [blame] | 270 | |
| 271 | Ethernet ethReply = new Ethernet(); |
| 272 | ethReply.setSourceMACAddress(dhcpServerMac); |
| 273 | ethReply.setDestinationMACAddress(ethRequest.getSourceMAC()); |
| 274 | ethReply.setEtherType(Ethernet.TYPE_IPV4); |
| 275 | |
| 276 | IPv4 ipv4Request = (IPv4) ethRequest.getPayload(); |
| 277 | IPv4 ipv4Reply = new IPv4(); |
| 278 | ipv4Reply.setSourceAddress(serverIp.toInt()); |
| 279 | ipv4Reply.setDestinationAddress(reqInstance.ipAddress().toInt()); |
Hyunsun Moon | bcf4925 | 2017-02-21 22:28:41 +0900 | [diff] [blame] | 280 | ipv4Reply.setTtl(DEFAULT_PACKET_TTL); |
Hyunsun Moon | 28b358a | 2016-11-28 13:23:05 -0800 | [diff] [blame] | 281 | |
| 282 | UDP udpRequest = (UDP) ipv4Request.getPayload(); |
| 283 | UDP udpReply = new UDP(); |
| 284 | udpReply.setSourcePort((byte) UDP.DHCP_SERVER_PORT); |
| 285 | udpReply.setDestinationPort((byte) UDP.DHCP_CLIENT_PORT); |
| 286 | |
| 287 | DHCP dhcpRequest = (DHCP) udpRequest.getPayload(); |
| 288 | DHCP dhcpReply = buildDhcpReply( |
Hyunsun Moon | 187bf53 | 2017-01-19 10:57:40 +0900 | [diff] [blame] | 289 | dhcpRequest, packetType, reqInstance.ipAddress(), snet); |
Hyunsun Moon | 28b358a | 2016-11-28 13:23:05 -0800 | [diff] [blame] | 290 | |
| 291 | udpReply.setPayload(dhcpReply); |
| 292 | ipv4Reply.setPayload(udpReply); |
| 293 | ethReply.setPayload(ipv4Reply); |
| 294 | |
| 295 | return ethReply; |
| 296 | } |
| 297 | |
| 298 | private void sendReply(PacketContext context, Ethernet ethReply) { |
| 299 | if (ethReply == null) { |
| 300 | return; |
| 301 | } |
| 302 | ConnectPoint srcPoint = context.inPacket().receivedFrom(); |
| 303 | TrafficTreatment treatment = DefaultTrafficTreatment |
| 304 | .builder() |
| 305 | .setOutput(srcPoint.port()) |
| 306 | .build(); |
| 307 | |
| 308 | packetService.emit(new DefaultOutboundPacket( |
| 309 | srcPoint.deviceId(), |
| 310 | treatment, |
| 311 | ByteBuffer.wrap(ethReply.serialize()))); |
| 312 | context.block(); |
| 313 | } |
| 314 | |
| 315 | private DHCP buildDhcpReply(DHCP request, byte msgType, Ip4Address yourIp, |
Hyunsun Moon | 187bf53 | 2017-01-19 10:57:40 +0900 | [diff] [blame] | 316 | ServiceNetwork snet) { |
| 317 | Ip4Address serverIp = snet.serviceIp().getIp4Address(); |
| 318 | int subnetPrefixLen = snet.subnet().prefixLength(); |
Hyunsun Moon | 28b358a | 2016-11-28 13:23:05 -0800 | [diff] [blame] | 319 | |
| 320 | DHCP dhcpReply = new DHCP(); |
| 321 | dhcpReply.setOpCode(DHCP.OPCODE_REPLY); |
| 322 | dhcpReply.setHardwareType(DHCP.HWTYPE_ETHERNET); |
| 323 | dhcpReply.setHardwareAddressLength((byte) 6); |
| 324 | dhcpReply.setTransactionId(request.getTransactionId()); |
| 325 | dhcpReply.setFlags(request.getFlags()); |
| 326 | dhcpReply.setYourIPAddress(yourIp.toInt()); |
| 327 | dhcpReply.setServerIPAddress(serverIp.toInt()); |
| 328 | dhcpReply.setClientHardwareAddress(request.getClientHardwareAddress()); |
| 329 | |
| 330 | List<DHCPOption> options = Lists.newArrayList(); |
| 331 | // message type |
| 332 | DHCPOption option = new DHCPOption(); |
| 333 | option.setCode(OptionCode_MessageType.getValue()); |
| 334 | option.setLength((byte) 1); |
| 335 | byte[] optionData = {msgType}; |
| 336 | option.setData(optionData); |
| 337 | options.add(option); |
| 338 | |
| 339 | // server identifier |
| 340 | option = new DHCPOption(); |
| 341 | option.setCode(OptionCode_DHCPServerIp.getValue()); |
| 342 | option.setLength((byte) 4); |
| 343 | option.setData(serverIp.toOctets()); |
| 344 | options.add(option); |
| 345 | |
| 346 | // lease time |
| 347 | option = new DHCPOption(); |
| 348 | option.setCode(OptionCode_LeaseTime.getValue()); |
| 349 | option.setLength((byte) 4); |
| 350 | option.setData(DHCP_DATA_LEASE_INFINITE); |
| 351 | options.add(option); |
| 352 | |
| 353 | // subnet mask |
| 354 | Ip4Address subnetMask = Ip4Address.makeMaskPrefix(subnetPrefixLen); |
| 355 | option = new DHCPOption(); |
| 356 | option.setCode(OptionCode_SubnetMask.getValue()); |
| 357 | option.setLength((byte) 4); |
| 358 | option.setData(subnetMask.toOctets()); |
| 359 | options.add(option); |
| 360 | |
| 361 | // broadcast address |
| 362 | Ip4Address broadcast = Ip4Address.makeMaskedAddress(yourIp, subnetPrefixLen); |
| 363 | option = new DHCPOption(); |
| 364 | option.setCode(OptionCode_BroadcastAddress.getValue()); |
| 365 | option.setLength((byte) 4); |
| 366 | option.setData(broadcast.toOctets()); |
| 367 | options.add(option); |
| 368 | |
Hyunsun Moon | 0984cbd | 2016-12-01 17:34:11 -0800 | [diff] [blame] | 369 | // domain server |
Hyunsun Moon | 28b358a | 2016-11-28 13:23:05 -0800 | [diff] [blame] | 370 | option = new DHCPOption(); |
| 371 | option.setCode(OptionCode_DomainServer.getValue()); |
| 372 | option.setLength((byte) 4); |
| 373 | option.setData(DEFAULT_DNS.toOctets()); |
| 374 | options.add(option); |
| 375 | |
| 376 | // TODO fix MTU value to be configurable |
| 377 | option = new DHCPOption(); |
| 378 | option.setCode(DHCP_OPTION_MTU); |
| 379 | option.setLength((byte) 2); |
| 380 | option.setData(DHCP_DATA_MTU_DEFAULT); |
| 381 | options.add(option); |
| 382 | |
| 383 | // router address |
Hyunsun Moon | 187bf53 | 2017-01-19 10:57:40 +0900 | [diff] [blame] | 384 | if (snet.type() != MANAGEMENT_LOCAL && snet.type() != MANAGEMENT_HOST) { |
Hyunsun Moon | 28b358a | 2016-11-28 13:23:05 -0800 | [diff] [blame] | 385 | option = new DHCPOption(); |
| 386 | option.setCode(OptionCode_RouterAddress.getValue()); |
| 387 | option.setLength((byte) 4); |
| 388 | option.setData(serverIp.toOctets()); |
| 389 | options.add(option); |
| 390 | } |
| 391 | |
Hyunsun Moon | 0984cbd | 2016-12-01 17:34:11 -0800 | [diff] [blame] | 392 | // classless static routes |
Hyunsun Moon | 187bf53 | 2017-01-19 10:57:40 +0900 | [diff] [blame] | 393 | byte[] data = getClasslessStaticRoutesData(snet); |
Hyunsun Moon | 0984cbd | 2016-12-01 17:34:11 -0800 | [diff] [blame] | 394 | if (data.length >= 5) { |
| 395 | option = new DHCPOption(); |
| 396 | option.setCode(DHCP_OPTION_CLASSLESS_STATIC_ROUTE); |
| 397 | option.setLength((byte) data.length); |
| 398 | option.setData(data); |
| 399 | options.add(option); |
| 400 | } |
Hyunsun Moon | 28b358a | 2016-11-28 13:23:05 -0800 | [diff] [blame] | 401 | |
| 402 | // end option |
| 403 | option = new DHCPOption(); |
| 404 | option.setCode(OptionCode_END.getValue()); |
| 405 | option.setLength((byte) 1); |
| 406 | options.add(option); |
| 407 | |
| 408 | dhcpReply.setOptions(options); |
| 409 | return dhcpReply; |
| 410 | } |
Hyunsun Moon | 0984cbd | 2016-12-01 17:34:11 -0800 | [diff] [blame] | 411 | |
Hyunsun Moon | 187bf53 | 2017-01-19 10:57:40 +0900 | [diff] [blame] | 412 | private byte[] getClasslessStaticRoutesData(ServiceNetwork snet) { |
Hyunsun Moon | 0984cbd | 2016-12-01 17:34:11 -0800 | [diff] [blame] | 413 | List<Byte> result = Lists.newArrayList(); |
Hyunsun Moon | 187bf53 | 2017-01-19 10:57:40 +0900 | [diff] [blame] | 414 | List<Byte> router = Bytes.asList(snet.serviceIp().toOctets()); |
Hyunsun Moon | 0984cbd | 2016-12-01 17:34:11 -0800 | [diff] [blame] | 415 | |
| 416 | // static routes for the providers |
Hyunsun Moon | 187bf53 | 2017-01-19 10:57:40 +0900 | [diff] [blame] | 417 | Set<ServiceNetwork> providers = snet.providers().keySet().stream() |
| 418 | .map(provider -> snetService.serviceNetwork(provider)) |
Hyunsun Moon | 0984cbd | 2016-12-01 17:34:11 -0800 | [diff] [blame] | 419 | .filter(Objects::nonNull) |
| 420 | .collect(Collectors.toSet()); |
| 421 | |
Hyunsun Moon | bcf4925 | 2017-02-21 22:28:41 +0900 | [diff] [blame] | 422 | providers.forEach(provider -> { |
Hyunsun Moon | 0984cbd | 2016-12-01 17:34:11 -0800 | [diff] [blame] | 423 | result.add((byte) provider.subnet().prefixLength()); |
| 424 | result.addAll(getSignificantOctets(provider.subnet())); |
| 425 | result.addAll(router); |
| 426 | }); |
| 427 | |
| 428 | // static routes for the bidirectional subscribers |
Hyunsun Moon | 187bf53 | 2017-01-19 10:57:40 +0900 | [diff] [blame] | 429 | Set<ServiceNetwork> subscribers = snetService.serviceNetworks().stream() |
| 430 | .filter(net -> isBidirectionalProvider(net, snet.id())) |
Hyunsun Moon | 0984cbd | 2016-12-01 17:34:11 -0800 | [diff] [blame] | 431 | .collect(Collectors.toSet()); |
| 432 | |
Hyunsun Moon | bcf4925 | 2017-02-21 22:28:41 +0900 | [diff] [blame] | 433 | subscribers.forEach(subscriber -> { |
Hyunsun Moon | 0984cbd | 2016-12-01 17:34:11 -0800 | [diff] [blame] | 434 | result.add((byte) subscriber.subnet().prefixLength()); |
| 435 | result.addAll(getSignificantOctets(subscriber.subnet())); |
| 436 | result.addAll(router); |
| 437 | }); |
| 438 | |
| 439 | return Bytes.toArray(result); |
| 440 | } |
| 441 | |
Hyunsun Moon | 187bf53 | 2017-01-19 10:57:40 +0900 | [diff] [blame] | 442 | private boolean isBidirectionalProvider(ServiceNetwork snet, NetworkId targetNetId) { |
| 443 | return snet.providers().entrySet().stream() |
| 444 | .filter(p -> Objects.equals(p.getKey(), targetNetId)) |
Hyunsun Moon | bcf4925 | 2017-02-21 22:28:41 +0900 | [diff] [blame] | 445 | .anyMatch(p -> p.getValue() == BIDIRECTIONAL); |
Hyunsun Moon | 187bf53 | 2017-01-19 10:57:40 +0900 | [diff] [blame] | 446 | } |
| 447 | |
Hyunsun Moon | 0984cbd | 2016-12-01 17:34:11 -0800 | [diff] [blame] | 448 | private List<Byte> getSignificantOctets(IpPrefix ipPrefix) { |
| 449 | int numOfOctets = ipPrefix.prefixLength() / 8; |
| 450 | if (ipPrefix.prefixLength() % 8 != 0) { |
| 451 | numOfOctets += 1; |
| 452 | } |
| 453 | byte[] result = Arrays.copyOfRange(ipPrefix.address().toOctets(), 0, numOfOctets); |
| 454 | return Bytes.asList(result); |
| 455 | } |
Hyunsun Moon | 28b358a | 2016-11-28 13:23:05 -0800 | [diff] [blame] | 456 | } |
Hyunsun Moon | 28b358a | 2016-11-28 13:23:05 -0800 | [diff] [blame] | 457 | } |