Yi Tseng | 5130129 | 2017-07-28 13:02:59 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2017-present Open Networking Foundation |
| 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 | */ |
| 17 | |
| 18 | package org.onosproject.dhcprelay; |
| 19 | |
Yi Tseng | 525ff40 | 2017-10-23 19:39:39 -0700 | [diff] [blame] | 20 | import com.google.common.collect.HashMultimap; |
Yi Tseng | 919b2df | 2017-09-07 16:22:51 -0700 | [diff] [blame] | 21 | import com.google.common.collect.Lists; |
Yi Tseng | 525ff40 | 2017-10-23 19:39:39 -0700 | [diff] [blame] | 22 | import com.google.common.collect.Multimap; |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 23 | import org.apache.felix.scr.annotations.Activate; |
| 24 | import org.apache.felix.scr.annotations.Deactivate; |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 25 | import com.google.common.collect.Sets; |
| 26 | import com.google.common.collect.ImmutableSet; |
Yi Tseng | 5130129 | 2017-07-28 13:02:59 -0700 | [diff] [blame] | 27 | import org.apache.felix.scr.annotations.Component; |
| 28 | import org.apache.felix.scr.annotations.Property; |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 29 | import org.apache.felix.scr.annotations.Reference; |
| 30 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
Yi Tseng | 5130129 | 2017-07-28 13:02:59 -0700 | [diff] [blame] | 31 | import org.apache.felix.scr.annotations.Service; |
| 32 | import org.onlab.packet.BasePacket; |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 33 | import org.onlab.packet.DHCP6; |
| 34 | import org.onlab.packet.IPv6; |
| 35 | import org.onlab.packet.Ethernet; |
| 36 | import org.onlab.packet.Ip6Address; |
Yi Tseng | 5130129 | 2017-07-28 13:02:59 -0700 | [diff] [blame] | 37 | import org.onlab.packet.IpAddress; |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 38 | import org.onlab.packet.IpPrefix; |
Yi Tseng | 5130129 | 2017-07-28 13:02:59 -0700 | [diff] [blame] | 39 | import org.onlab.packet.MacAddress; |
Yi Tseng | 525ff40 | 2017-10-23 19:39:39 -0700 | [diff] [blame] | 40 | import org.onlab.packet.TpPort; |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 41 | import org.onlab.packet.UDP; |
Yi Tseng | 5130129 | 2017-07-28 13:02:59 -0700 | [diff] [blame] | 42 | import org.onlab.packet.VlanId; |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 43 | import org.onlab.packet.dhcp.Dhcp6RelayOption; |
| 44 | import org.onlab.packet.dhcp.Dhcp6InterfaceIdOption; |
| 45 | import org.onlab.packet.dhcp.Dhcp6Option; |
| 46 | import org.onlab.packet.dhcp.Dhcp6IaNaOption; |
| 47 | import org.onlab.packet.dhcp.Dhcp6IaTaOption; |
| 48 | import org.onlab.packet.dhcp.Dhcp6IaPdOption; |
| 49 | import org.onlab.packet.dhcp.Dhcp6IaAddressOption; |
| 50 | import org.onlab.packet.dhcp.Dhcp6IaPrefixOption; |
| 51 | import org.onlab.util.HexString; |
Yi Tseng | 525ff40 | 2017-10-23 19:39:39 -0700 | [diff] [blame] | 52 | import org.onosproject.core.ApplicationId; |
| 53 | import org.onosproject.core.CoreService; |
Yi Tseng | 5130129 | 2017-07-28 13:02:59 -0700 | [diff] [blame] | 54 | import org.onosproject.dhcprelay.api.DhcpHandler; |
Yi Tseng | 919b2df | 2017-09-07 16:22:51 -0700 | [diff] [blame] | 55 | import org.onosproject.dhcprelay.api.DhcpServerInfo; |
Yi Tseng | 525ff40 | 2017-10-23 19:39:39 -0700 | [diff] [blame] | 56 | import org.onosproject.dhcprelay.config.IgnoreDhcpConfig; |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 57 | import org.onosproject.dhcprelay.store.DhcpRelayStore; |
Yi Tseng | 525ff40 | 2017-10-23 19:39:39 -0700 | [diff] [blame] | 58 | import org.onosproject.net.Device; |
| 59 | import org.onosproject.net.DeviceId; |
| 60 | import org.onosproject.net.behaviour.Pipeliner; |
| 61 | import org.onosproject.net.device.DeviceService; |
| 62 | import org.onosproject.net.flow.DefaultTrafficSelector; |
| 63 | import org.onosproject.net.flow.TrafficSelector; |
| 64 | import org.onosproject.net.flowobjective.DefaultForwardingObjective; |
| 65 | import org.onosproject.net.flowobjective.FlowObjectiveService; |
| 66 | import org.onosproject.net.flowobjective.ForwardingObjective; |
| 67 | import org.onosproject.net.flowobjective.Objective; |
| 68 | import org.onosproject.net.flowobjective.ObjectiveContext; |
| 69 | import org.onosproject.net.flowobjective.ObjectiveError; |
Yi Tseng | aa417a6 | 2017-09-08 17:22:51 -0700 | [diff] [blame] | 70 | import org.onosproject.net.host.HostProvider; |
| 71 | import org.onosproject.net.host.HostProviderRegistry; |
| 72 | import org.onosproject.net.host.HostProviderService; |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 73 | import org.onosproject.net.host.HostService; |
| 74 | import org.onosproject.net.host.DefaultHostDescription; |
| 75 | import org.onosproject.net.host.HostDescription; |
| 76 | import org.onosproject.net.host.InterfaceIpAddress; |
| 77 | import org.onosproject.net.host.HostListener; |
| 78 | import org.onosproject.net.host.HostEvent; |
| 79 | import org.onosproject.net.intf.Interface; |
| 80 | import org.onosproject.net.intf.InterfaceService; |
Yi Tseng | 525ff40 | 2017-10-23 19:39:39 -0700 | [diff] [blame] | 81 | import org.onosproject.net.packet.PacketPriority; |
Yi Tseng | aa417a6 | 2017-09-08 17:22:51 -0700 | [diff] [blame] | 82 | import org.onosproject.net.provider.ProviderId; |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 83 | import org.onosproject.routeservice.Route; |
| 84 | import org.onosproject.routeservice.RouteStore; |
Yi Tseng | 483ac6f | 2017-08-02 15:03:31 -0700 | [diff] [blame] | 85 | import org.onosproject.dhcprelay.config.DhcpServerConfig; |
Yi Tseng | 5130129 | 2017-07-28 13:02:59 -0700 | [diff] [blame] | 86 | import org.onosproject.net.ConnectPoint; |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 87 | import org.onosproject.net.Host; |
| 88 | import org.onosproject.net.HostId; |
| 89 | import org.onosproject.net.HostLocation; |
| 90 | import org.onosproject.net.packet.DefaultOutboundPacket; |
| 91 | import org.onosproject.net.packet.OutboundPacket; |
Yi Tseng | 5130129 | 2017-07-28 13:02:59 -0700 | [diff] [blame] | 92 | import org.onosproject.net.packet.PacketContext; |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 93 | import org.onosproject.net.packet.PacketService; |
| 94 | import org.slf4j.Logger; |
| 95 | import org.slf4j.LoggerFactory; |
| 96 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
| 97 | import org.onosproject.net.flow.TrafficTreatment; |
Yi Tseng | 5130129 | 2017-07-28 13:02:59 -0700 | [diff] [blame] | 98 | |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 99 | |
| 100 | import java.nio.ByteBuffer; |
| 101 | import java.util.List; |
Yi Tseng | 483ac6f | 2017-08-02 15:03:31 -0700 | [diff] [blame] | 102 | import java.util.Collection; |
Yi Tseng | 5130129 | 2017-07-28 13:02:59 -0700 | [diff] [blame] | 103 | import java.util.Optional; |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 104 | import java.util.Set; |
| 105 | import java.util.ArrayList; |
Yi Tseng | 525ff40 | 2017-10-23 19:39:39 -0700 | [diff] [blame] | 106 | import java.util.concurrent.atomic.AtomicInteger; |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 107 | |
| 108 | |
| 109 | import static com.google.common.base.Preconditions.checkNotNull; |
| 110 | import static com.google.common.base.Preconditions.checkState; |
Yi Tseng | 525ff40 | 2017-10-23 19:39:39 -0700 | [diff] [blame] | 111 | import static org.onosproject.net.flowobjective.Objective.Operation.ADD; |
| 112 | import static org.onosproject.net.flowobjective.Objective.Operation.REMOVE; |
Yi Tseng | 5130129 | 2017-07-28 13:02:59 -0700 | [diff] [blame] | 113 | |
| 114 | @Component |
| 115 | @Service |
| 116 | @Property(name = "version", value = "6") |
Yi Tseng | aa417a6 | 2017-09-08 17:22:51 -0700 | [diff] [blame] | 117 | public class Dhcp6HandlerImpl implements DhcpHandler, HostProvider { |
Charles Chan | d988c28 | 2017-09-12 17:09:32 -0700 | [diff] [blame] | 118 | public static final String DHCP_V6_RELAY_APP = "org.onosproject.Dhcp6HandlerImpl"; |
Saurav Das | 7c6dec1 | 2017-09-13 14:35:56 -0700 | [diff] [blame] | 119 | public static final ProviderId PROVIDER_ID = new ProviderId("dhcp6", DHCP_V6_RELAY_APP); |
Yi Tseng | 525ff40 | 2017-10-23 19:39:39 -0700 | [diff] [blame] | 120 | private static final int IGNORE_CONTROL_PRIORITY = PacketPriority.CONTROL.priorityValue() + 1000; |
| 121 | |
| 122 | private static final TrafficSelector CLIENT_SERVER_SELECTOR = DefaultTrafficSelector.builder() |
| 123 | .matchEthType(Ethernet.TYPE_IPV6) |
| 124 | .matchIPProtocol(IPv6.PROTOCOL_UDP) |
| 125 | .matchIPv6Src(IpPrefix.IPV6_LINK_LOCAL_PREFIX) |
| 126 | .matchIPv6Dst(Ip6Address.ALL_DHCP_RELAY_AGENTS_AND_SERVERS.toIpPrefix()) |
| 127 | .matchUdpSrc(TpPort.tpPort(UDP.DHCP_V6_CLIENT_PORT)) |
| 128 | .matchUdpDst(TpPort.tpPort(UDP.DHCP_V6_SERVER_PORT)) |
| 129 | .build(); |
| 130 | private static final TrafficSelector SERVER_RELAY_SELECTOR = DefaultTrafficSelector.builder() |
| 131 | .matchEthType(Ethernet.TYPE_IPV6) |
| 132 | .matchIPProtocol(IPv6.PROTOCOL_UDP) |
| 133 | .matchUdpSrc(TpPort.tpPort(UDP.DHCP_V6_SERVER_PORT)) |
| 134 | .matchUdpDst(TpPort.tpPort(UDP.DHCP_V6_SERVER_PORT)) |
| 135 | .build(); |
| 136 | static final Set<TrafficSelector> DHCP_SELECTORS = ImmutableSet.of( |
| 137 | CLIENT_SERVER_SELECTOR, |
| 138 | SERVER_RELAY_SELECTOR |
| 139 | ); |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 140 | private static Logger log = LoggerFactory.getLogger(Dhcp6HandlerImpl.class); |
Yi Tseng | 5130129 | 2017-07-28 13:02:59 -0700 | [diff] [blame] | 141 | |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 142 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 143 | protected DhcpRelayStore dhcpRelayStore; |
Yi Tseng | 5130129 | 2017-07-28 13:02:59 -0700 | [diff] [blame] | 144 | |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 145 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 146 | protected PacketService packetService; |
| 147 | |
| 148 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 149 | protected RouteStore routeStore; |
| 150 | |
| 151 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 152 | protected InterfaceService interfaceService; |
| 153 | |
| 154 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 155 | protected HostService hostService; |
| 156 | |
Yi Tseng | aa417a6 | 2017-09-08 17:22:51 -0700 | [diff] [blame] | 157 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 158 | protected HostProviderRegistry providerRegistry; |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 159 | |
Yi Tseng | 525ff40 | 2017-10-23 19:39:39 -0700 | [diff] [blame] | 160 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 161 | protected CoreService coreService; |
| 162 | |
| 163 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 164 | protected DeviceService deviceService; |
| 165 | |
| 166 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 167 | protected FlowObjectiveService flowObjectiveService; |
| 168 | |
Yi Tseng | aa417a6 | 2017-09-08 17:22:51 -0700 | [diff] [blame] | 169 | protected HostProviderService providerService; |
Yi Tseng | 525ff40 | 2017-10-23 19:39:39 -0700 | [diff] [blame] | 170 | protected ApplicationId appId; |
| 171 | protected Multimap<DeviceId, VlanId> ignoredVlans = HashMultimap.create(); |
| 172 | private InternalHostListener hostListener = new InternalHostListener(); |
| 173 | |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 174 | private Ip6Address dhcpServerIp = null; |
| 175 | // dhcp server may be connected directly to the SDN network or |
| 176 | // via an external gateway. When connected directly, the dhcpConnectPoint, dhcpConnectMac, |
| 177 | // and dhcpConnectVlan refer to the server. When connected via the gateway, they refer |
| 178 | // to the gateway. |
| 179 | private ConnectPoint dhcpServerConnectPoint = null; |
| 180 | private MacAddress dhcpConnectMac = null; |
| 181 | private VlanId dhcpConnectVlan = null; |
| 182 | private Ip6Address dhcpGatewayIp = null; |
| 183 | private Ip6Address relayAgentIpFromCfg = null; |
| 184 | |
| 185 | private Ip6Address indirectDhcpServerIp = null; |
| 186 | private ConnectPoint indirectDhcpServerConnectPoint = null; |
| 187 | private MacAddress indirectDhcpConnectMac = null; |
| 188 | private VlanId indirectDhcpConnectVlan = null; |
| 189 | private Ip6Address indirectDhcpGatewayIp = null; |
| 190 | private Ip6Address indirectRelayAgentIpFromCfg = null; |
| 191 | |
Yi Tseng | 919b2df | 2017-09-07 16:22:51 -0700 | [diff] [blame] | 192 | private List<DhcpServerInfo> defaultServerInfoList = Lists.newArrayList(); |
| 193 | private List<DhcpServerInfo> indirectServerInfoList = Lists.newArrayList(); |
| 194 | |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 195 | |
| 196 | // CLIENT message types |
| 197 | public static final Set<Byte> MSG_TYPE_FROM_CLIENT = |
| 198 | ImmutableSet.of(DHCP6.MsgType.SOLICIT.value(), |
| 199 | DHCP6.MsgType.REQUEST.value(), |
| 200 | DHCP6.MsgType.REBIND.value(), |
| 201 | DHCP6.MsgType.RENEW.value(), |
| 202 | DHCP6.MsgType.RELEASE.value(), |
| 203 | DHCP6.MsgType.DECLINE.value(), |
| 204 | DHCP6.MsgType.CONFIRM.value(), |
| 205 | DHCP6.MsgType.RELAY_FORW.value()); |
| 206 | // SERVER message types |
| 207 | public static final Set<Byte> MSG_TYPE_FROM_SERVER = |
| 208 | ImmutableSet.of(DHCP6.MsgType.RELAY_REPL.value()); |
| 209 | |
| 210 | @Activate |
| 211 | protected void activate() { |
Yi Tseng | 525ff40 | 2017-10-23 19:39:39 -0700 | [diff] [blame] | 212 | appId = coreService.registerApplication(DHCP_V6_RELAY_APP); |
Yi Tseng | aa417a6 | 2017-09-08 17:22:51 -0700 | [diff] [blame] | 213 | providerService = providerRegistry.register(this); |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 214 | hostService.addListener(hostListener); |
Yi Tseng | 5130129 | 2017-07-28 13:02:59 -0700 | [diff] [blame] | 215 | } |
| 216 | |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 217 | @Deactivate |
| 218 | protected void deactivate() { |
Yi Tseng | aa417a6 | 2017-09-08 17:22:51 -0700 | [diff] [blame] | 219 | providerRegistry.unregister(this); |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 220 | hostService.removeListener(hostListener); |
Yi Tseng | 919b2df | 2017-09-07 16:22:51 -0700 | [diff] [blame] | 221 | defaultServerInfoList.forEach(this::stopMonitoringIps); |
| 222 | defaultServerInfoList.clear(); |
| 223 | indirectServerInfoList.forEach(this::stopMonitoringIps); |
| 224 | indirectServerInfoList.clear(); |
| 225 | } |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 226 | |
Yi Tseng | 919b2df | 2017-09-07 16:22:51 -0700 | [diff] [blame] | 227 | private void stopMonitoringIps(DhcpServerInfo serverInfo) { |
| 228 | serverInfo.getDhcpGatewayIp6().ifPresent(gatewayIp -> { |
| 229 | hostService.stopMonitoringIp(gatewayIp); |
| 230 | }); |
| 231 | serverInfo.getDhcpServerIp6().ifPresent(serverIp -> { |
| 232 | hostService.stopMonitoringIp(serverIp); |
| 233 | }); |
Yi Tseng | 5130129 | 2017-07-28 13:02:59 -0700 | [diff] [blame] | 234 | } |
| 235 | |
Yi Tseng | 5130129 | 2017-07-28 13:02:59 -0700 | [diff] [blame] | 236 | @Override |
Yi Tseng | 919b2df | 2017-09-07 16:22:51 -0700 | [diff] [blame] | 237 | public List<DhcpServerInfo> getDefaultDhcpServerInfoList() { |
| 238 | return defaultServerInfoList; |
Yi Tseng | 5130129 | 2017-07-28 13:02:59 -0700 | [diff] [blame] | 239 | } |
| 240 | |
| 241 | @Override |
Yi Tseng | 919b2df | 2017-09-07 16:22:51 -0700 | [diff] [blame] | 242 | public List<DhcpServerInfo> getIndirectDhcpServerInfoList() { |
| 243 | return indirectServerInfoList; |
Yi Tseng | 5130129 | 2017-07-28 13:02:59 -0700 | [diff] [blame] | 244 | } |
| 245 | |
| 246 | @Override |
Yi Tseng | 525ff40 | 2017-10-23 19:39:39 -0700 | [diff] [blame] | 247 | public void updateIgnoreVlanConfig(IgnoreDhcpConfig config) { |
| 248 | if (config == null) { |
| 249 | ignoredVlans.forEach(((deviceId, vlanId) -> { |
| 250 | processIgnoreVlanRule(deviceId, vlanId, REMOVE); |
| 251 | })); |
| 252 | return; |
| 253 | } |
| 254 | config.ignoredVlans().forEach((deviceId, vlanId) -> { |
| 255 | if (ignoredVlans.get(deviceId).contains(vlanId)) { |
| 256 | // don't need to process if it already ignored |
| 257 | return; |
| 258 | } |
| 259 | processIgnoreVlanRule(deviceId, vlanId, ADD); |
| 260 | }); |
| 261 | |
| 262 | ignoredVlans.forEach((deviceId, vlanId) -> { |
| 263 | if (!config.ignoredVlans().get(deviceId).contains(vlanId)) { |
| 264 | // not contains in new config, remove it |
| 265 | processIgnoreVlanRule(deviceId, vlanId, REMOVE); |
| 266 | } |
| 267 | }); |
| 268 | } |
| 269 | |
| 270 | @Override |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 271 | public void processDhcpPacket(PacketContext context, BasePacket payload) { |
| 272 | checkNotNull(payload, "DHCP6 payload can't be null"); |
| 273 | checkState(payload instanceof DHCP6, "Payload is not a DHCP6"); |
| 274 | DHCP6 dhcp6Payload = (DHCP6) payload; |
| 275 | Ethernet receivedPacket = context.inPacket().parsed(); |
| 276 | |
| 277 | if (!configured()) { |
| 278 | log.warn("Missing DHCP6 relay server config. Abort packet processing"); |
| 279 | log.warn("dhcp6 payload {}", dhcp6Payload); |
| 280 | |
| 281 | return; |
| 282 | } |
| 283 | |
| 284 | byte msgType = dhcp6Payload.getMsgType(); |
| 285 | log.warn("msgType is {}", msgType); |
| 286 | |
| 287 | ConnectPoint inPort = context.inPacket().receivedFrom(); |
| 288 | if (inPort == null) { |
| 289 | log.warn("incommin ConnectPoint is null"); |
| 290 | } |
| 291 | Set<Interface> receivingInterfaces = interfaceService.getInterfacesByPort(inPort); |
| 292 | //ignore the packets if dhcp client interface is not configured on onos. |
| 293 | if (receivingInterfaces.isEmpty()) { |
| 294 | log.warn("Virtual interface is not configured on {}", inPort); |
| 295 | return; |
| 296 | } |
| 297 | |
| 298 | |
| 299 | if (MSG_TYPE_FROM_CLIENT.contains(msgType)) { |
| 300 | |
| 301 | InternalPacket ethernetClientPacket = |
| 302 | processDhcp6PacketFromClient(context, receivedPacket, receivingInterfaces); |
| 303 | if (ethernetClientPacket != null) { |
| 304 | forwardPacket(ethernetClientPacket); |
| 305 | } |
| 306 | |
| 307 | } else if (MSG_TYPE_FROM_SERVER.contains(msgType)) { |
| 308 | log.warn("calling processDhcp6PacketFromServer with RELAY_REPL", msgType); |
| 309 | InternalPacket ethernetPacketReply = |
| 310 | processDhcp6PacketFromServer(context, receivedPacket, receivingInterfaces); |
| 311 | if (ethernetPacketReply != null) { |
| 312 | forwardPacket(ethernetPacketReply); |
| 313 | } |
| 314 | } else { |
| 315 | log.warn("Not so fast, packet type {} not supported yet", msgType); |
| 316 | } |
| 317 | } |
| 318 | |
| 319 | |
| 320 | /** |
| 321 | * Checks if this app has been configured. |
| 322 | * |
| 323 | * @return true if all information we need have been initialized |
| 324 | */ |
| 325 | public boolean configured() { |
Yi Tseng | 919b2df | 2017-09-07 16:22:51 -0700 | [diff] [blame] | 326 | return !defaultServerInfoList.isEmpty(); |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 327 | } |
| 328 | |
Yi Tseng | aa417a6 | 2017-09-08 17:22:51 -0700 | [diff] [blame] | 329 | @Override |
| 330 | public ProviderId id() { |
Charles Chan | d988c28 | 2017-09-12 17:09:32 -0700 | [diff] [blame] | 331 | return PROVIDER_ID; |
Yi Tseng | aa417a6 | 2017-09-08 17:22:51 -0700 | [diff] [blame] | 332 | } |
| 333 | |
| 334 | @Override |
| 335 | public void triggerProbe(Host host) { |
| 336 | // Do nothing here |
| 337 | } |
| 338 | |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 339 | // the new class the contains Ethernet packet and destination port, kind of like adding |
| 340 | // internal header to the packet |
| 341 | private class InternalPacket { |
| 342 | Ethernet packet; |
| 343 | ConnectPoint destLocation; |
| 344 | public InternalPacket(Ethernet newPacket, ConnectPoint newLocation) { |
| 345 | packet = newPacket; |
| 346 | destLocation = newLocation; |
| 347 | } |
| 348 | void setLocation(ConnectPoint newLocation) { |
| 349 | destLocation = newLocation; |
| 350 | } |
| 351 | } |
| 352 | |
| 353 | //forward the packet to ConnectPoint where the DHCP server is attached. |
| 354 | private void forwardPacket(InternalPacket packet) { |
| 355 | //send Packetout to dhcp server connectpoint. |
| 356 | if (packet.destLocation != null) { |
| 357 | TrafficTreatment t = DefaultTrafficTreatment.builder() |
| 358 | .setOutput(packet.destLocation.port()).build(); |
| 359 | OutboundPacket o = new DefaultOutboundPacket( |
| 360 | packet.destLocation.deviceId(), t, ByteBuffer.wrap(packet.packet.serialize())); |
| 361 | if (log.isTraceEnabled()) { |
| 362 | log.trace("Relaying packet to destination {}", packet.destLocation); |
| 363 | } |
| 364 | packetService.emit(o); |
| 365 | } // if |
| 366 | } |
| 367 | |
| 368 | /** |
| 369 | * Check if the host is directly connected to the network or not. |
| 370 | * |
| 371 | * @param dhcp6Payload the dhcp6 payload |
| 372 | * @return true if the host is directly connected to the network; false otherwise |
| 373 | */ |
| 374 | private boolean directlyConnected(DHCP6 dhcp6Payload) { |
| 375 | log.debug("directlyConnected enters"); |
| 376 | |
| 377 | if (dhcp6Payload.getMsgType() != DHCP6.MsgType.RELAY_FORW.value() && |
| 378 | dhcp6Payload.getMsgType() != DHCP6.MsgType.RELAY_REPL.value()) { |
| 379 | log.debug("directlyConnected true. MsgType {}", dhcp6Payload.getMsgType()); |
| 380 | |
| 381 | return true; |
| 382 | } |
| 383 | |
| 384 | // Regardless of relay-forward or relay-replay, check if we see another relay message |
| 385 | DHCP6 dhcp6Payload2 = dhcp6PacketFromRelayPacket(dhcp6Payload); |
| 386 | if (dhcp6Payload2 != null) { |
| 387 | if (dhcp6Payload.getMsgType() == DHCP6.MsgType.RELAY_FORW.value()) { |
| 388 | log.debug("directlyConnected false. 1st realy-foward, 2nd MsgType {}", dhcp6Payload2.getMsgType()); |
| 389 | return false; |
| 390 | } else { |
| 391 | // relay-reply |
| 392 | if (dhcp6Payload2.getMsgType() != DHCP6.MsgType.RELAY_REPL.value()) { |
| 393 | log.debug("directlyConnected true. 2nd MsgType {}", dhcp6Payload2.getMsgType()); |
| 394 | return true; // must be directly connected |
| 395 | } else { |
| 396 | log.debug("directlyConnected false. 1st relay-reply, 2nd relay-reply MsgType {}", |
| 397 | dhcp6Payload2.getMsgType()); |
| 398 | return false; // must be indirectly connected |
| 399 | } |
| 400 | } |
| 401 | } else { |
| 402 | log.warn("directlyConnected true."); |
| 403 | return true; |
| 404 | } |
| 405 | } |
| 406 | |
| 407 | /** |
| 408 | * extract DHCP6 payload from dhcp6 relay message within relay-forwrd/reply. |
| 409 | * |
| 410 | * @param dhcp6 dhcp6 relay-reply or relay-foward |
| 411 | * @return dhcp6Packet dhcp6 packet extracted from relay-message |
| 412 | */ |
| 413 | private DHCP6 dhcp6PacketFromRelayPacket(DHCP6 dhcp6) { |
| 414 | log.debug("dhcp6PacketFromRelayPacket enters. dhcp6 {}", dhcp6); |
| 415 | |
| 416 | // extract the relay message if exist |
| 417 | DHCP6 dhcp6Payload = dhcp6.getOptions().stream() |
| 418 | .filter(opt -> opt instanceof Dhcp6RelayOption) |
| 419 | .map(BasePacket::getPayload) |
| 420 | .map(pld -> (DHCP6) pld) |
| 421 | .findFirst() |
| 422 | .orElse(null); |
| 423 | |
| 424 | |
| 425 | if (dhcp6Payload == null) { |
| 426 | // Can't find dhcp payload |
| 427 | log.debug("Can't find dhcp6 payload from relay message"); |
| 428 | } else { |
| 429 | log.debug("dhcp6 payload found from relay message {}", dhcp6Payload); |
| 430 | } |
| 431 | |
| 432 | return dhcp6Payload; |
| 433 | } |
| 434 | |
| 435 | /** |
| 436 | * find the leaf DHCP6 packet from multi-level relay packet. |
| 437 | * |
| 438 | * @param relayPacket dhcp6 relay packet |
| 439 | * @return leafPacket non-relay dhcp6 packet |
| 440 | */ |
| 441 | private DHCP6 getDhcp6Leaf(DHCP6 relayPacket) { |
| 442 | DHCP6 dhcp6Parent = relayPacket; |
| 443 | DHCP6 dhcp6Child = null; |
| 444 | |
| 445 | log.debug("getDhcp6Leaf entered."); |
| 446 | while (dhcp6Parent != null) { |
| 447 | dhcp6Child = dhcp6PacketFromRelayPacket(dhcp6Parent); |
| 448 | |
| 449 | if (dhcp6Child != null) { |
| 450 | if (dhcp6Child.getMsgType() != DHCP6.MsgType.RELAY_FORW.value() && |
| 451 | dhcp6Child.getMsgType() != DHCP6.MsgType.RELAY_REPL.value()) { |
| 452 | log.debug("leaf dhcp6 packet found."); |
| 453 | break; |
| 454 | } else { |
| 455 | // found another relay |
| 456 | // go for another loop |
| 457 | dhcp6Parent = dhcp6Child; |
| 458 | } |
| 459 | } else { |
| 460 | log.warn("malformed pkt! Expected dhcp6 within relay pkt, but no dhcp6 leaf found."); |
| 461 | break; |
| 462 | } |
| 463 | } |
| 464 | return dhcp6Child; |
| 465 | } |
| 466 | |
| 467 | /** |
| 468 | * check if DHCP6 relay-reply is reply. |
| 469 | * |
| 470 | * @param relayPacket dhcp6 relay-reply |
| 471 | * @return boolean relay-reply contains ack |
| 472 | */ |
| 473 | private boolean isDhcp6Reply(DHCP6 relayPacket) { |
| 474 | log.debug("isDhcp6Reply entered."); |
| 475 | |
| 476 | DHCP6 leafDhcp6 = getDhcp6Leaf(relayPacket); |
| 477 | |
| 478 | if (leafDhcp6 != null) { |
| 479 | if (leafDhcp6.getMsgType() == DHCP6.MsgType.REPLY.value()) { |
| 480 | log.debug("isDhcp6Reply true."); |
| 481 | return true; // must be directly connected |
| 482 | } else { |
| 483 | log.debug("isDhcp6Reply false. leaf dhcp6 is not replay. MsgType {}", leafDhcp6.getMsgType()); |
| 484 | } |
| 485 | } else { |
| 486 | log.debug("isDhcp6Reply false. Expected dhcp6 within relay pkt but not found."); |
| 487 | } |
| 488 | log.debug("isDhcp6Reply false."); |
| 489 | return false; |
| 490 | } |
| 491 | |
| 492 | /** |
| 493 | * check if DHCP6 is release or relay-forward contains release. |
| 494 | * |
| 495 | * @param dhcp6Payload dhcp6 packet |
| 496 | * @return boolean dhcp6 contains release |
| 497 | */ |
| 498 | private boolean isDhcp6Release(DHCP6 dhcp6Payload) { |
| 499 | |
| 500 | log.debug("isDhcp6Release entered."); |
| 501 | |
| 502 | if (dhcp6Payload.getMsgType() == DHCP6.MsgType.RELEASE.value()) { |
| 503 | log.debug("isDhcp6Release true."); |
| 504 | return true; // must be directly connected |
| 505 | } else { |
| 506 | DHCP6 dhcp6Leaf = getDhcp6Leaf(dhcp6Payload); |
| 507 | if (dhcp6Leaf != null) { |
| 508 | if (dhcp6Leaf.getMsgType() == DHCP6.MsgType.RELEASE.value()) { |
| 509 | log.debug("isDhcp6Release true. indirectlry connected"); |
| 510 | return true; |
| 511 | } else { |
| 512 | log.debug("leaf dhcp6 is not release. MsgType {}", dhcp6Leaf.getMsgType()); |
| 513 | return false; |
| 514 | } |
| 515 | } else { |
| 516 | log.debug("isDhcp6Release false. dhcp6 is niether relay nor release."); |
| 517 | return false; |
| 518 | } |
| 519 | } |
| 520 | } |
| 521 | |
| 522 | /** |
| 523 | * extract from dhcp6 packet client ipv6 address of given by dhcp server. |
| 524 | * |
| 525 | * @param dhcp6 the dhcp6 packet |
| 526 | * @return Ip6Address Ip6Address given by dhcp server, or null if not exists |
| 527 | */ |
| 528 | private Ip6Address extractIpAddress(DHCP6 dhcp6) { |
| 529 | Ip6Address ip = null; |
| 530 | |
| 531 | log.debug("extractIpAddress enters dhcp6 {}.", dhcp6); |
| 532 | // Extract IPv6 address from IA NA ot IA TA option |
| 533 | Optional<Dhcp6IaNaOption> iaNaOption = dhcp6.getOptions() |
| 534 | .stream() |
| 535 | .filter(opt -> opt instanceof Dhcp6IaNaOption) |
| 536 | .map(opt -> (Dhcp6IaNaOption) opt) |
| 537 | .findFirst(); |
| 538 | Optional<Dhcp6IaTaOption> iaTaOption = dhcp6.getOptions() |
| 539 | .stream() |
| 540 | .filter(opt -> opt instanceof Dhcp6IaTaOption) |
| 541 | .map(opt -> (Dhcp6IaTaOption) opt) |
| 542 | .findFirst(); |
| 543 | Optional<Dhcp6IaAddressOption> iaAddressOption; |
| 544 | if (iaNaOption.isPresent()) { |
| 545 | log.debug("Found IPv6 address from iaNaOption {}", iaNaOption); |
| 546 | |
| 547 | iaAddressOption = iaNaOption.get().getOptions().stream() |
| 548 | .filter(opt -> opt instanceof Dhcp6IaAddressOption) |
| 549 | .map(opt -> (Dhcp6IaAddressOption) opt) |
| 550 | .findFirst(); |
| 551 | } else if (iaTaOption.isPresent()) { |
| 552 | log.debug("Found IPv6 address from iaTaOption {}", iaTaOption); |
| 553 | |
| 554 | iaAddressOption = iaTaOption.get().getOptions().stream() |
| 555 | .filter(opt -> opt instanceof Dhcp6IaAddressOption) |
| 556 | .map(opt -> (Dhcp6IaAddressOption) opt) |
| 557 | .findFirst(); |
| 558 | } else { |
| 559 | iaAddressOption = Optional.empty(); |
| 560 | } |
| 561 | if (iaAddressOption.isPresent()) { |
| 562 | ip = iaAddressOption.get().getIp6Address(); |
| 563 | log.debug("Found IPv6 address from iaAddressOption {}", iaAddressOption); |
| 564 | |
| 565 | |
| 566 | } else { |
| 567 | log.debug("Can't find IPv6 address from DHCPv6 {}", dhcp6); |
| 568 | } |
| 569 | |
| 570 | return ip; |
| 571 | } |
| 572 | /** |
| 573 | * extract from dhcp6 packet Prefix prefix provided by dhcp server. |
| 574 | * |
| 575 | * @param dhcp6 the dhcp6 payload |
| 576 | * @return IpPrefix Prefix Delegation prefix, or null if not exists. |
| 577 | */ |
| 578 | private IpPrefix extractPrefix(DHCP6 dhcp6) { |
| 579 | log.warn("extractPrefix enters {}", dhcp6); |
| 580 | |
| 581 | // extract prefix |
| 582 | IpPrefix prefixPrefix = null; |
| 583 | |
| 584 | Ip6Address prefixAddress = null; |
| 585 | |
| 586 | // Extract IPv6 prefix from IA PD option |
| 587 | Optional<Dhcp6IaPdOption> iaPdOption = dhcp6.getOptions() |
| 588 | .stream() |
| 589 | .filter(opt -> opt instanceof Dhcp6IaPdOption) |
| 590 | .map(opt -> (Dhcp6IaPdOption) opt) |
| 591 | .findFirst(); |
| 592 | |
| 593 | Optional<Dhcp6IaPrefixOption> iaPrefixOption; |
| 594 | if (iaPdOption.isPresent()) { |
| 595 | log.warn("IA_PD option found {}", iaPdOption); |
| 596 | |
| 597 | iaPrefixOption = iaPdOption.get().getOptions().stream() |
| 598 | .filter(opt -> opt instanceof Dhcp6IaPrefixOption) |
| 599 | .map(opt -> (Dhcp6IaPrefixOption) opt) |
| 600 | .findFirst(); |
| 601 | } else { |
| 602 | log.warn("IA_PD option NOT found"); |
| 603 | |
| 604 | iaPrefixOption = Optional.empty(); |
| 605 | } |
| 606 | if (iaPrefixOption.isPresent()) { |
| 607 | log.warn("IAPrefix Option within IA_PD option found {}", iaPrefixOption); |
| 608 | |
| 609 | prefixAddress = iaPrefixOption.get().getIp6Prefix(); |
| 610 | int prefixLen = (int) iaPrefixOption.get().getPrefixLength(); |
| 611 | log.warn("Prefix length is {} bits", prefixLen); |
| 612 | prefixPrefix = IpPrefix.valueOf(prefixAddress, prefixLen); |
| 613 | |
| 614 | } else { |
| 615 | log.warn("Can't find IPv6 prefix from DHCPv6 {}", dhcp6); |
| 616 | } |
| 617 | |
| 618 | return prefixPrefix; |
| 619 | } |
| 620 | |
| 621 | /** |
| 622 | * remove host or route. |
| 623 | * |
| 624 | * @param directConnFlag flag to show that packet is from directly connected client |
| 625 | * @param dhcp6Packet the dhcp6 payload |
| 626 | * @param clientPacket client's ethernet packet |
| 627 | * @param clientIpv6 client's Ipv6 packet |
Kalhee Kim | 0c0cb0b | 2017-09-15 17:43:27 +0000 | [diff] [blame] | 628 | * @param clientInterface client interfaces |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 629 | */ |
| 630 | private void removeHostOrRoute(boolean directConnFlag, DHCP6 dhcp6Packet, |
| 631 | Ethernet clientPacket, IPv6 clientIpv6, |
Kalhee Kim | 0c0cb0b | 2017-09-15 17:43:27 +0000 | [diff] [blame] | 632 | Interface clientInterface) { |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 633 | log.debug("extractPrefix enters {}", dhcp6Packet); |
Kalhee Kim | 121ba92 | 2017-11-01 17:56:44 +0000 | [diff] [blame^] | 634 | VlanId vlanId = clientInterface.vlan(); |
| 635 | MacAddress clientMac = clientPacket.getSourceMAC(); |
| 636 | log.debug("client mac {} client vlan {}", HexString.toHexString(clientMac.toBytes(), ":"), vlanId); |
| 637 | |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 638 | // add host or route |
| 639 | if (isDhcp6Release(dhcp6Packet)) { |
| 640 | IpAddress ip = null; |
| 641 | if (directConnFlag) { |
| 642 | // Add to host store if it is connected to network directly |
| 643 | ip = extractIpAddress(dhcp6Packet); |
| 644 | if (ip != null) { |
Kalhee Kim | 121ba92 | 2017-11-01 17:56:44 +0000 | [diff] [blame^] | 645 | |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 646 | HostId hostId = HostId.hostId(clientMac, vlanId); |
| 647 | log.debug("remove Host {} ip for directly connected.", hostId.toString()); |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 648 | // Remove host's ip of when dhcp release msg is received |
Yi Tseng | aa417a6 | 2017-09-08 17:22:51 -0700 | [diff] [blame] | 649 | providerService.removeIpFromHost(hostId, ip); |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 650 | } else { |
| 651 | log.debug("ipAddress not found. Do not add Host for directly connected."); |
| 652 | } |
| 653 | } else { |
| 654 | // Remove from route store if it is not connected to network directly |
Kalhee Kim | 121ba92 | 2017-11-01 17:56:44 +0000 | [diff] [blame^] | 655 | // pick out the first link-local ip address |
| 656 | IpAddress nextHopIp = getFirstIpByHost(clientMac, vlanId); |
| 657 | if (nextHopIp == null) { |
| 658 | log.warn("Can't find link-local IP address of gateway mac {} vlanId {}", |
| 659 | clientMac, vlanId); |
| 660 | return; |
| 661 | } |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 662 | |
| 663 | DHCP6 leafDhcp = getDhcp6Leaf(dhcp6Packet); |
| 664 | ip = extractIpAddress(leafDhcp); |
| 665 | if (ip == null) { |
| 666 | log.debug("ip is null"); |
| 667 | } else { |
| 668 | Route routeForIP = new Route(Route.Source.STATIC, ip.toIpPrefix(), nextHopIp); |
| 669 | log.debug("removing route of 128 address for indirectly connected."); |
| 670 | log.debug("128 ip {}, nexthop {}", HexString.toHexString(ip.toOctets(), ":"), |
| 671 | HexString.toHexString(nextHopIp.toOctets(), ":")); |
| 672 | routeStore.removeRoute(routeForIP); |
| 673 | } |
| 674 | |
| 675 | IpPrefix ipPrefix = extractPrefix(leafDhcp); |
| 676 | if (ipPrefix == null) { |
| 677 | log.debug("ipPrefix is null "); |
| 678 | } else { |
| 679 | Route routeForPrefix = new Route(Route.Source.STATIC, ipPrefix, nextHopIp); |
| 680 | log.debug("removing route of PD for indirectly connected."); |
| 681 | log.debug("pd ip {}, nexthop {}", HexString.toHexString(ipPrefix.address().toOctets(), ":"), |
| 682 | HexString.toHexString(nextHopIp.toOctets(), ":")); |
| 683 | |
| 684 | routeStore.removeRoute(routeForPrefix); |
| 685 | } |
| 686 | } |
| 687 | } |
| 688 | } |
| 689 | |
| 690 | /** |
| 691 | * add host or route. |
| 692 | * |
| 693 | * @param directConnFlag flag to show that packet is from directly connected client |
| 694 | * @param dhcp6Relay the dhcp6 payload |
| 695 | * @param embeddedDhcp6 client's ethernet packetthe dhcp6 payload within relay |
| 696 | * @param clientMac client macAddress |
Kalhee Kim | 0c0cb0b | 2017-09-15 17:43:27 +0000 | [diff] [blame] | 697 | * @param clientInterface client interface |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 698 | */ |
| 699 | private void addHostOrRoute(boolean directConnFlag, DHCP6 dhcp6Relay, |
| 700 | DHCP6 embeddedDhcp6, |
| 701 | MacAddress clientMac, |
Kalhee Kim | 0c0cb0b | 2017-09-15 17:43:27 +0000 | [diff] [blame] | 702 | Interface clientInterface) { |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 703 | log.debug("addHostOrRoute entered."); |
Kalhee Kim | 121ba92 | 2017-11-01 17:56:44 +0000 | [diff] [blame^] | 704 | VlanId vlanId = clientInterface.vlan(); |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 705 | // add host or route |
| 706 | if (isDhcp6Reply(dhcp6Relay)) { |
| 707 | IpAddress ip = null; |
| 708 | if (directConnFlag) { |
| 709 | // Add to host store if it connect to network directly |
| 710 | ip = extractIpAddress(embeddedDhcp6); |
| 711 | if (ip != null) { |
| 712 | Set<IpAddress> ips = Sets.newHashSet(ip); |
Yi Tseng | aa417a6 | 2017-09-08 17:22:51 -0700 | [diff] [blame] | 713 | |
| 714 | // FIXME: we should use vlan id from original packet (solicit, request) |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 715 | HostId hostId = HostId.hostId(clientMac, vlanId); |
Yi Tseng | aa417a6 | 2017-09-08 17:22:51 -0700 | [diff] [blame] | 716 | Host host = hostService.getHost(hostId); |
Kalhee Kim | 0c0cb0b | 2017-09-15 17:43:27 +0000 | [diff] [blame] | 717 | HostLocation hostLocation = new HostLocation(clientInterface.connectPoint(), |
Yi Tseng | aa417a6 | 2017-09-08 17:22:51 -0700 | [diff] [blame] | 718 | System.currentTimeMillis()); |
| 719 | Set<HostLocation> hostLocations = Sets.newHashSet(hostLocation); |
| 720 | |
| 721 | if (host != null) { |
| 722 | // Dual homing support: |
| 723 | // if host exists, use old locations and new location |
| 724 | hostLocations.addAll(host.locations()); |
| 725 | } |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 726 | HostDescription desc = new DefaultHostDescription(clientMac, vlanId, |
Yi Tseng | aa417a6 | 2017-09-08 17:22:51 -0700 | [diff] [blame] | 727 | hostLocations, ips, |
| 728 | false); |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 729 | log.debug("adding Host for directly connected."); |
| 730 | log.debug("client mac {} client vlan {} hostlocation {}", |
| 731 | HexString.toHexString(clientMac.toBytes(), ":"), |
| 732 | vlanId, hostLocation.toString()); |
| 733 | |
| 734 | // Replace the ip when dhcp server give the host new ip address |
Yi Tseng | aa417a6 | 2017-09-08 17:22:51 -0700 | [diff] [blame] | 735 | providerService.hostDetected(hostId, desc, false); |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 736 | } else { |
| 737 | log.warn("ipAddress not found. Do not add Host for directly connected."); |
| 738 | } |
| 739 | } else { |
| 740 | // Add to route store if it does not connect to network directly |
Kalhee Kim | 121ba92 | 2017-11-01 17:56:44 +0000 | [diff] [blame^] | 741 | // pick out the first link-local ip address |
| 742 | IpAddress nextHopIp = getFirstIpByHost(clientMac, vlanId); |
| 743 | if (nextHopIp == null) { |
| 744 | log.warn("Can't find link-local IP address of gateway mac {} vlanId {}", |
| 745 | clientMac, vlanId); |
| 746 | return; |
| 747 | } |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 748 | |
| 749 | DHCP6 leafDhcp = getDhcp6Leaf(embeddedDhcp6); |
| 750 | ip = extractIpAddress(leafDhcp); |
| 751 | if (ip == null) { |
| 752 | log.warn("ip is null"); |
| 753 | } else { |
| 754 | Route routeForIP = new Route(Route.Source.STATIC, ip.toIpPrefix(), nextHopIp); |
| 755 | log.warn("adding Route of 128 address for indirectly connected."); |
| 756 | routeStore.updateRoute(routeForIP); |
| 757 | } |
| 758 | |
| 759 | IpPrefix ipPrefix = extractPrefix(leafDhcp); |
| 760 | if (ipPrefix == null) { |
| 761 | log.warn("ipPrefix is null "); |
| 762 | } else { |
| 763 | Route routeForPrefix = new Route(Route.Source.STATIC, ipPrefix, nextHopIp); |
| 764 | log.warn("adding Route of PD for indirectly connected."); |
| 765 | routeStore.updateRoute(routeForPrefix); |
| 766 | } |
| 767 | } |
| 768 | } |
| 769 | } |
| 770 | |
| 771 | /** |
| 772 | * |
| 773 | * build the DHCP6 solicit/request packet with gatewayip. |
| 774 | * |
| 775 | * @param context packet context |
| 776 | * @param clientPacket client ethernet packet |
| 777 | * @param clientInterfaces set of client side interfaces |
| 778 | */ |
| 779 | private InternalPacket processDhcp6PacketFromClient(PacketContext context, |
| 780 | Ethernet clientPacket, Set<Interface> clientInterfaces) { |
Kalhee Kim | 45b2418 | 2017-10-18 18:30:23 +0000 | [diff] [blame] | 781 | Ip6Address relayAgentIp = getRelayAgentIPv6Address(clientInterfaces); |
| 782 | MacAddress relayAgentMac = clientInterfaces.iterator().next().mac(); |
| 783 | if (relayAgentIp == null || relayAgentMac == null) { |
| 784 | log.warn("Missing DHCP relay agent interface Ipv6 addr config for " |
| 785 | + "packet from client on port: {}. Aborting packet processing", |
| 786 | clientInterfaces.iterator().next().connectPoint()); |
| 787 | return null; |
| 788 | } |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 789 | |
Kalhee Kim | 45b2418 | 2017-10-18 18:30:23 +0000 | [diff] [blame] | 790 | // get dhcp6 header. |
Kalhee Kim | 45b2418 | 2017-10-18 18:30:23 +0000 | [diff] [blame] | 791 | IPv6 clientIpv6 = (IPv6) clientPacket.getPayload(); |
| 792 | UDP clientUdp = (UDP) clientIpv6.getPayload(); |
| 793 | DHCP6 clientDhcp6 = (DHCP6) clientUdp.getPayload(); |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 794 | |
Kalhee Kim | 45b2418 | 2017-10-18 18:30:23 +0000 | [diff] [blame] | 795 | boolean directConnFlag = directlyConnected(clientDhcp6); |
| 796 | Interface serverInterface = directConnFlag ? getServerInterface() : getIndirectServerInterface(); |
| 797 | if (serverInterface == null) { |
| 798 | log.warn("Can't get {} server interface, ignore", directConnFlag ? "direct" : "indirect"); |
| 799 | return null; |
| 800 | } |
| 801 | Ip6Address ipFacingServer = getFirstIpFromInterface(serverInterface); |
| 802 | MacAddress macFacingServer = serverInterface.mac(); |
| 803 | if (ipFacingServer == null || macFacingServer == null) { |
| 804 | log.warn("No IP v6 address for server Interface {}", serverInterface); |
| 805 | return null; |
| 806 | } |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 807 | |
Kalhee Kim | 45b2418 | 2017-10-18 18:30:23 +0000 | [diff] [blame] | 808 | Ethernet etherReply = clientPacket.duplicate(); |
| 809 | etherReply.setSourceMACAddress(macFacingServer); |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 810 | |
Kalhee Kim | 45b2418 | 2017-10-18 18:30:23 +0000 | [diff] [blame] | 811 | if ((directConnFlag && this.dhcpConnectMac == null) || |
| 812 | !directConnFlag && this.indirectDhcpConnectMac == null && this.dhcpConnectMac == null) { |
| 813 | log.warn("Packet received from {} connected client.", directConnFlag ? "directly" : "indirectly"); |
Charles Chan | 6fa3b4f | 2017-10-30 13:51:22 -0700 | [diff] [blame] | 814 | log.warn("DHCP6 {} not yet resolved .. Aborting DHCP packet processing from client on port: {}", |
Kalhee Kim | 45b2418 | 2017-10-18 18:30:23 +0000 | [diff] [blame] | 815 | (this.dhcpGatewayIp == null) ? "server IP " + this.dhcpServerIp |
| 816 | : "gateway IP " + this.dhcpGatewayIp, |
| 817 | clientInterfaces.iterator().next().connectPoint()); |
Kalhee Kim | 45b2418 | 2017-10-18 18:30:23 +0000 | [diff] [blame] | 818 | return null; |
| 819 | } |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 820 | |
Kalhee Kim | 45b2418 | 2017-10-18 18:30:23 +0000 | [diff] [blame] | 821 | if (this.dhcpServerConnectPoint == null) { |
| 822 | log.warn("DHCP6 server connection point direct {} directConn {} indirectConn {} is not set up yet", |
| 823 | directConnFlag, this.dhcpServerConnectPoint, this.indirectDhcpServerConnectPoint); |
Kalhee Kim | 121ba92 | 2017-11-01 17:56:44 +0000 | [diff] [blame^] | 824 | return null; |
| 825 | } |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 826 | |
Kalhee Kim | 121ba92 | 2017-11-01 17:56:44 +0000 | [diff] [blame^] | 827 | etherReply.setDestinationMACAddress(this.dhcpConnectMac); |
| 828 | etherReply.setVlanID(this.dhcpConnectVlan.toShort()); |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 829 | |
Kalhee Kim | 121ba92 | 2017-11-01 17:56:44 +0000 | [diff] [blame^] | 830 | IPv6 ipv6Packet = (IPv6) etherReply.getPayload(); |
| 831 | byte[] peerAddress = clientIpv6.getSourceAddress(); |
| 832 | ipv6Packet.setSourceAddress(ipFacingServer.toOctets()); |
| 833 | ipv6Packet.setDestinationAddress(this.dhcpServerIp.toOctets()); |
Charles Chan | a990ce9 | 2017-10-30 10:22:50 -0700 | [diff] [blame] | 834 | |
Kalhee Kim | 121ba92 | 2017-11-01 17:56:44 +0000 | [diff] [blame^] | 835 | UDP udpPacket = (UDP) ipv6Packet.getPayload(); |
| 836 | udpPacket.setSourcePort(UDP.DHCP_V6_SERVER_PORT); |
| 837 | DHCP6 dhcp6Packet = (DHCP6) udpPacket.getPayload(); |
| 838 | byte[] dhcp6PacketByte = dhcp6Packet.serialize(); |
Charles Chan | a990ce9 | 2017-10-30 10:22:50 -0700 | [diff] [blame] | 839 | |
Kalhee Kim | 121ba92 | 2017-11-01 17:56:44 +0000 | [diff] [blame^] | 840 | // notify onos and quagga to release PD |
| 841 | //releasePD(dhcp6Packet); |
| 842 | ConnectPoint clientConnectionPoint = context.inPacket().receivedFrom(); |
| 843 | VlanId vlanIdInUse = VlanId.vlanId(clientPacket.getVlanID()); |
| 844 | Interface clientInterface = interfaceService.getInterfacesByPort(clientConnectionPoint) |
| 845 | .stream().filter(iface -> interfaceContainsVlan(iface, vlanIdInUse)) |
| 846 | .findFirst().orElse(null); |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 847 | |
Kalhee Kim | 121ba92 | 2017-11-01 17:56:44 +0000 | [diff] [blame^] | 848 | removeHostOrRoute(directConnFlag, dhcp6Packet, clientPacket, clientIpv6, clientInterface); |
Kalhee Kim | 45b2418 | 2017-10-18 18:30:23 +0000 | [diff] [blame] | 849 | |
Kalhee Kim | 121ba92 | 2017-11-01 17:56:44 +0000 | [diff] [blame^] | 850 | DHCP6 dhcp6Relay = new DHCP6(); |
| 851 | dhcp6Relay.setMsgType(DHCP6.MsgType.RELAY_FORW.value()); |
| 852 | // link address: server uses the address to identify the link on which the client |
| 853 | // is located. |
| 854 | if (directConnFlag) { |
| 855 | dhcp6Relay.setLinkAddress(relayAgentIp.toOctets()); |
| 856 | log.debug("direct connection: relayAgentIp obtained dynamically {}", |
| 857 | HexString.toHexString(relayAgentIp.toOctets(), ":")); |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 858 | |
Kalhee Kim | 121ba92 | 2017-11-01 17:56:44 +0000 | [diff] [blame^] | 859 | } else { |
| 860 | if (this.indirectDhcpServerIp == null) { |
| 861 | log.warn("indirect DhcpServerIp not available, use default DhcpServerIp {}", |
Kalhee Kim | d21029f | 2017-09-26 20:21:53 +0000 | [diff] [blame] | 862 | HexString.toHexString(this.dhcpServerIp.toOctets())); |
| 863 | } else { |
| 864 | // Indirect case, replace destination to indirect dhcp server if exist |
| 865 | // Check if mac is obtained for valid server ip |
| 866 | if (this.indirectDhcpConnectMac == null) { |
| 867 | log.warn("DHCP6 {} not yet resolved .. Aborting DHCP " |
| 868 | + "packet processing from client on port: {}", |
| 869 | (this.indirectDhcpGatewayIp == null) ? "server IP " + this.indirectDhcpServerIp |
| 870 | : "gateway IP " + this.indirectDhcpGatewayIp, |
| 871 | clientInterfaces.iterator().next().connectPoint()); |
| 872 | return null; |
| 873 | } |
| 874 | etherReply.setDestinationMACAddress(this.indirectDhcpConnectMac); |
| 875 | etherReply.setVlanID(this.indirectDhcpConnectVlan.toShort()); |
| 876 | ipv6Packet.setDestinationAddress(this.indirectDhcpServerIp.toOctets()); |
| 877 | |
| 878 | } |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 879 | if (this.indirectRelayAgentIpFromCfg == null) { |
| 880 | dhcp6Relay.setLinkAddress(relayAgentIp.toOctets()); |
Kalhee Kim | 0c0cb0b | 2017-09-15 17:43:27 +0000 | [diff] [blame] | 881 | log.warn("indirect connection: relayAgentIp NOT availale from config file! Use dynamic. {}", |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 882 | HexString.toHexString(relayAgentIp.toOctets(), ":")); |
Kalhee Kim | 121ba92 | 2017-11-01 17:56:44 +0000 | [diff] [blame^] | 883 | } else { |
| 884 | dhcp6Relay.setLinkAddress(this.indirectRelayAgentIpFromCfg.toOctets()); |
| 885 | log.debug("indirect connection: relayAgentIp from config file is available! {}", |
| 886 | HexString.toHexString(this.indirectRelayAgentIpFromCfg.toOctets(), ":")); |
| 887 | } |
| 888 | } |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 889 | |
Kalhee Kim | 121ba92 | 2017-11-01 17:56:44 +0000 | [diff] [blame^] | 890 | // peer address: address of the client or relay agent from which |
| 891 | // the message to be relayed was received. |
| 892 | dhcp6Relay.setPeerAddress(peerAddress); |
| 893 | List<Dhcp6Option> options = new ArrayList<Dhcp6Option>(); |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 894 | |
Kalhee Kim | 121ba92 | 2017-11-01 17:56:44 +0000 | [diff] [blame^] | 895 | // directly connected case, hop count is zero |
| 896 | // otherwise, hop count + 1 |
| 897 | if (directConnFlag) { |
| 898 | dhcp6Relay.setHopCount((byte) 0); |
| 899 | } else { |
| 900 | dhcp6Relay.setHopCount((byte) (dhcp6Packet.getHopCount() + 1)); |
| 901 | } |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 902 | |
Kalhee Kim | 121ba92 | 2017-11-01 17:56:44 +0000 | [diff] [blame^] | 903 | // create relay message option |
| 904 | Dhcp6Option relayMessage = new Dhcp6Option(); |
| 905 | relayMessage.setCode(DHCP6.OptionCode.RELAY_MSG.value()); |
| 906 | relayMessage.setLength((short) dhcp6PacketByte.length); |
| 907 | relayMessage.setData(dhcp6PacketByte); |
| 908 | options.add(relayMessage); |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 909 | |
Kalhee Kim | 121ba92 | 2017-11-01 17:56:44 +0000 | [diff] [blame^] | 910 | // create interfaceId option |
| 911 | String inPortString = "-" + context.inPacket().receivedFrom().toString() + ":"; |
| 912 | Dhcp6Option interfaceId = new Dhcp6Option(); |
| 913 | interfaceId.setCode(DHCP6.OptionCode.INTERFACE_ID.value()); |
| 914 | byte[] clientSoureMacBytes = clientPacket.getSourceMACAddress(); |
| 915 | byte[] inPortStringBytes = inPortString.getBytes(); |
| 916 | byte[] vlanIdBytes = new byte[2]; |
| 917 | vlanIdBytes[0] = (byte) (clientPacket.getVlanID() & 0xff); |
| 918 | vlanIdBytes[1] = (byte) ((clientPacket.getVlanID() >> 8) & 0xff); |
| 919 | byte[] interfaceIdBytes = new byte[clientSoureMacBytes.length + |
| 920 | inPortStringBytes.length + vlanIdBytes.length]; |
| 921 | log.debug("Length: interfaceIdBytes {} clientSoureMacBytes {} inPortStringBytes {} vlan {}", |
| 922 | interfaceIdBytes.length, clientSoureMacBytes.length, inPortStringBytes.length, |
| 923 | vlanIdBytes.length); |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 924 | |
Kalhee Kim | 121ba92 | 2017-11-01 17:56:44 +0000 | [diff] [blame^] | 925 | System.arraycopy(clientSoureMacBytes, 0, interfaceIdBytes, 0, clientSoureMacBytes.length); |
| 926 | System.arraycopy(inPortStringBytes, 0, interfaceIdBytes, clientSoureMacBytes.length, inPortStringBytes.length); |
| 927 | System.arraycopy(vlanIdBytes, 0, interfaceIdBytes, clientSoureMacBytes.length + inPortStringBytes.length, |
| 928 | vlanIdBytes.length); |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 929 | |
Kalhee Kim | 121ba92 | 2017-11-01 17:56:44 +0000 | [diff] [blame^] | 930 | interfaceId.setData(interfaceIdBytes); |
| 931 | interfaceId.setLength((short) interfaceIdBytes.length); |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 932 | |
Kalhee Kim | 121ba92 | 2017-11-01 17:56:44 +0000 | [diff] [blame^] | 933 | options.add(interfaceId); |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 934 | |
Kalhee Kim | 121ba92 | 2017-11-01 17:56:44 +0000 | [diff] [blame^] | 935 | log.debug("interfaceId write srcMac {} portString {}", |
| 936 | HexString.toHexString(clientSoureMacBytes, ":"), inPortString); |
| 937 | dhcp6Relay.setOptions(options); |
| 938 | udpPacket.setPayload(dhcp6Relay); |
| 939 | udpPacket.resetChecksum(); |
| 940 | ipv6Packet.setPayload(udpPacket); |
| 941 | ipv6Packet.setHopLimit((byte) 64); |
| 942 | etherReply.setPayload(ipv6Packet); |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 943 | |
Kalhee Kim | 121ba92 | 2017-11-01 17:56:44 +0000 | [diff] [blame^] | 944 | if (directConnFlag) { |
| 945 | return new InternalPacket(etherReply, this.dhcpServerConnectPoint); |
| 946 | } else { |
| 947 | if (this.indirectDhcpServerIp == null) { |
| 948 | return new InternalPacket(etherReply, this.dhcpServerConnectPoint); |
| 949 | } else { |
| 950 | return new InternalPacket(etherReply, this.indirectDhcpServerConnectPoint); |
| 951 | } |
| 952 | } |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 953 | } |
| 954 | |
| 955 | /** |
| 956 | * |
| 957 | * process the DHCP6 relay-reply packet from dhcp server. |
| 958 | * |
| 959 | * @param context packet context |
| 960 | * @param receivedPacket server ethernet packet |
| 961 | * @param recevingInterfaces set of server side interfaces |
| 962 | */ |
| 963 | private InternalPacket processDhcp6PacketFromServer(PacketContext context, |
| 964 | Ethernet receivedPacket, Set<Interface> recevingInterfaces) { |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 965 | // get dhcp6 header. |
Ray Milkey | f0c4761 | 2017-09-28 11:29:38 -0700 | [diff] [blame] | 966 | Ethernet etherReply = receivedPacket.duplicate(); |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 967 | IPv6 ipv6Packet = (IPv6) etherReply.getPayload(); |
| 968 | UDP udpPacket = (UDP) ipv6Packet.getPayload(); |
| 969 | DHCP6 dhcp6Relay = (DHCP6) udpPacket.getPayload(); |
| 970 | |
| 971 | Boolean directConnFlag = directlyConnected(dhcp6Relay); |
Kalhee Kim | d21029f | 2017-09-26 20:21:53 +0000 | [diff] [blame] | 972 | ConnectPoint inPort = context.inPacket().receivedFrom(); |
| 973 | if ((directConnFlag || (!directConnFlag && this.indirectDhcpServerIp == null)) |
| 974 | && !inPort.equals(this.dhcpServerConnectPoint)) { |
| 975 | log.warn("Receiving port {} is not the same as server connect point {} for direct or indirect-null", |
| 976 | inPort, this.dhcpServerConnectPoint); |
| 977 | return null; |
| 978 | } |
| 979 | |
| 980 | if (!directConnFlag && this.indirectDhcpServerIp != null && |
| 981 | !inPort.equals(this.indirectDhcpServerConnectPoint)) { |
| 982 | log.warn("Receiving port {} is not the same as server connect point {} for indirect", |
| 983 | inPort, this.indirectDhcpServerConnectPoint); |
| 984 | return null; |
| 985 | } |
| 986 | |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 987 | |
| 988 | Dhcp6InterfaceIdOption interfaceIdOption = dhcp6Relay.getOptions().stream() |
| 989 | .filter(opt -> opt instanceof Dhcp6InterfaceIdOption) |
| 990 | .map(opt -> (Dhcp6InterfaceIdOption) opt) |
| 991 | .findFirst() |
| 992 | .orElse(null); |
| 993 | |
| 994 | if (interfaceIdOption == null) { |
| 995 | log.warn("Interface Id option is not present, abort packet..."); |
| 996 | return null; |
| 997 | } |
| 998 | |
| 999 | MacAddress peerMac = interfaceIdOption.getMacAddress(); |
| 1000 | String clientConnectionPointStr = new String(interfaceIdOption.getInPort()); |
| 1001 | |
| 1002 | ConnectPoint clientConnectionPoint = ConnectPoint.deviceConnectPoint(clientConnectionPointStr); |
Kalhee Kim | 0c0cb0b | 2017-09-15 17:43:27 +0000 | [diff] [blame] | 1003 | VlanId vlanIdInUse = VlanId.vlanId(interfaceIdOption.getVlanId()); |
| 1004 | Interface clientInterface = interfaceService.getInterfacesByPort(clientConnectionPoint) |
| 1005 | .stream() |
| 1006 | .filter(iface -> interfaceContainsVlan(iface, vlanIdInUse)) |
| 1007 | .findFirst() |
| 1008 | .orElse(null); |
| 1009 | if (clientInterface == null) { |
| 1010 | log.warn("Cannot get client interface for from packet, abort... vlan {}", vlanIdInUse.toString()); |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 1011 | return null; |
| 1012 | } |
Kalhee Kim | 0c0cb0b | 2017-09-15 17:43:27 +0000 | [diff] [blame] | 1013 | MacAddress relayAgentMac = clientInterface.mac(); |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 1014 | if (relayAgentMac == null) { |
| 1015 | log.warn("Can not get interface mac, abort packet.."); |
| 1016 | return null; |
| 1017 | } |
| 1018 | etherReply.setSourceMACAddress(relayAgentMac); |
| 1019 | |
| 1020 | // find destMac |
| 1021 | MacAddress clientMac = null; |
Yi Tseng | aa417a6 | 2017-09-08 17:22:51 -0700 | [diff] [blame] | 1022 | Ip6Address peerAddress = Ip6Address.valueOf(dhcp6Relay.getPeerAddress()); |
| 1023 | Set<Host> clients = hostService.getHostsByIp(peerAddress); |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 1024 | if (clients.isEmpty()) { |
| 1025 | log.warn("There's no host found for this address {}", |
| 1026 | HexString.toHexString(dhcp6Relay.getPeerAddress(), ":")); |
| 1027 | log.warn("Let's look up interfaceId {}", HexString.toHexString(peerMac.toBytes(), ":")); |
| 1028 | clientMac = peerMac; |
| 1029 | } else { |
| 1030 | clientMac = clients.iterator().next().mac(); |
| 1031 | if (clientMac == null) { |
| 1032 | log.warn("No client mac address found, abort packet..."); |
| 1033 | return null; |
| 1034 | } |
| 1035 | log.warn("Client mac address found from getHostByIp"); |
| 1036 | |
| 1037 | } |
| 1038 | etherReply.setDestinationMACAddress(clientMac); |
| 1039 | |
| 1040 | // ip header |
| 1041 | ipv6Packet.setSourceAddress(dhcp6Relay.getLinkAddress()); |
| 1042 | ipv6Packet.setDestinationAddress(dhcp6Relay.getPeerAddress()); |
| 1043 | // udp header |
| 1044 | udpPacket.setSourcePort(UDP.DHCP_V6_SERVER_PORT); |
| 1045 | if (directConnFlag) { |
| 1046 | udpPacket.setDestinationPort(UDP.DHCP_V6_CLIENT_PORT); |
| 1047 | } else { |
| 1048 | udpPacket.setDestinationPort(UDP.DHCP_V6_SERVER_PORT); |
| 1049 | } |
| 1050 | |
| 1051 | DHCP6 embeddedDhcp6 = dhcp6Relay.getOptions().stream() |
| 1052 | .filter(opt -> opt instanceof Dhcp6RelayOption) |
| 1053 | .map(BasePacket::getPayload) |
| 1054 | .map(pld -> (DHCP6) pld) |
| 1055 | .findFirst() |
| 1056 | .orElse(null); |
| 1057 | |
| 1058 | |
| 1059 | // add host or route |
Kalhee Kim | 0c0cb0b | 2017-09-15 17:43:27 +0000 | [diff] [blame] | 1060 | addHostOrRoute(directConnFlag, dhcp6Relay, embeddedDhcp6, clientMac, clientInterface); |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 1061 | |
| 1062 | udpPacket.setPayload(embeddedDhcp6); |
| 1063 | udpPacket.resetChecksum(); |
| 1064 | ipv6Packet.setPayload(udpPacket); |
| 1065 | etherReply.setPayload(ipv6Packet); |
| 1066 | |
| 1067 | return new InternalPacket(etherReply, clientConnectionPoint); |
| 1068 | } |
| 1069 | |
Yi Tseng | 919b2df | 2017-09-07 16:22:51 -0700 | [diff] [blame] | 1070 | // Returns the first v6 interface ip out of a set of interfaces or null. |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 1071 | // Checks all interfaces, and ignores v6 interface ips |
| 1072 | private Ip6Address getRelayAgentIPv6Address(Set<Interface> intfs) { |
| 1073 | for (Interface intf : intfs) { |
| 1074 | for (InterfaceIpAddress ip : intf.ipAddressesList()) { |
| 1075 | Ip6Address relayAgentIp = ip.ipAddress().getIp6Address(); |
| 1076 | if (relayAgentIp != null) { |
| 1077 | return relayAgentIp; |
| 1078 | } |
| 1079 | } |
| 1080 | } |
| 1081 | return null; |
Yi Tseng | 5130129 | 2017-07-28 13:02:59 -0700 | [diff] [blame] | 1082 | } |
Yi Tseng | 483ac6f | 2017-08-02 15:03:31 -0700 | [diff] [blame] | 1083 | |
| 1084 | @Override |
| 1085 | public void setDefaultDhcpServerConfigs(Collection<DhcpServerConfig> configs) { |
Yi Tseng | 919b2df | 2017-09-07 16:22:51 -0700 | [diff] [blame] | 1086 | setDhcpServerConfigs(configs, defaultServerInfoList); |
| 1087 | reloadServerSettings(); |
| 1088 | } |
| 1089 | |
| 1090 | @Override |
| 1091 | public void setIndirectDhcpServerConfigs(Collection<DhcpServerConfig> configs) { |
| 1092 | setDhcpServerConfigs(configs, indirectServerInfoList); |
| 1093 | reloadServerSettings(); |
| 1094 | } |
| 1095 | |
| 1096 | public void setDhcpServerConfigs(Collection<DhcpServerConfig> configs, List<DhcpServerInfo> serverInfoList) { |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 1097 | if (configs.size() == 0) { |
| 1098 | // no config to update |
| 1099 | return; |
| 1100 | } |
| 1101 | |
| 1102 | // TODO: currently we pick up first DHCP server config. |
| 1103 | // Will use other server configs in the future for HA. |
| 1104 | DhcpServerConfig serverConfig = configs.iterator().next(); |
Yi Tseng | 919b2df | 2017-09-07 16:22:51 -0700 | [diff] [blame] | 1105 | |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 1106 | if (!serverConfig.getDhcpServerIp6().isPresent()) { |
Yi Tseng | 919b2df | 2017-09-07 16:22:51 -0700 | [diff] [blame] | 1107 | // not a DHCPv6 config |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 1108 | return; |
| 1109 | } |
| 1110 | |
Yi Tseng | 919b2df | 2017-09-07 16:22:51 -0700 | [diff] [blame] | 1111 | if (!serverInfoList.isEmpty()) { |
| 1112 | // remove old server info |
| 1113 | DhcpServerInfo oldServerInfo = serverInfoList.remove(0); |
| 1114 | |
| 1115 | // stop monitoring gateway or server |
| 1116 | oldServerInfo.getDhcpGatewayIp6().ifPresent(gatewayIp -> { |
| 1117 | hostService.stopMonitoringIp(gatewayIp); |
| 1118 | }); |
| 1119 | oldServerInfo.getDhcpServerIp6().ifPresent(serverIp -> { |
| 1120 | hostService.stopMonitoringIp(serverIp); |
Yi Tseng | 525ff40 | 2017-10-23 19:39:39 -0700 | [diff] [blame] | 1121 | cancelDhcpPacket(serverIp); |
Yi Tseng | 919b2df | 2017-09-07 16:22:51 -0700 | [diff] [blame] | 1122 | }); |
| 1123 | } |
| 1124 | |
| 1125 | // Create new server info according to the config |
| 1126 | DhcpServerInfo newServerInfo = new DhcpServerInfo(serverConfig, |
| 1127 | DhcpServerInfo.Version.DHCP_V6); |
| 1128 | checkState(newServerInfo.getDhcpServerConnectPoint().isPresent(), |
| 1129 | "Connect point not exists"); |
| 1130 | checkState(newServerInfo.getDhcpServerIp6().isPresent(), |
| 1131 | "IP of DHCP server not exists"); |
| 1132 | |
| 1133 | log.debug("DHCP server connect point: {}", newServerInfo.getDhcpServerConnectPoint().orElse(null)); |
| 1134 | log.debug("DHCP server IP: {}", newServerInfo.getDhcpServerIp6().orElse(null)); |
| 1135 | |
Yi Tseng | 525ff40 | 2017-10-23 19:39:39 -0700 | [diff] [blame] | 1136 | Ip6Address serverIp = newServerInfo.getDhcpServerIp6().get(); |
| 1137 | Ip6Address ipToProbe; |
Yi Tseng | 919b2df | 2017-09-07 16:22:51 -0700 | [diff] [blame] | 1138 | if (newServerInfo.getDhcpGatewayIp6().isPresent()) { |
| 1139 | ipToProbe = newServerInfo.getDhcpGatewayIp6().get(); |
| 1140 | } else { |
| 1141 | ipToProbe = newServerInfo.getDhcpServerIp6().orElse(null); |
| 1142 | } |
| 1143 | String hostToProbe = newServerInfo.getDhcpGatewayIp6() |
| 1144 | .map(ip -> "gateway").orElse("server"); |
| 1145 | |
| 1146 | log.debug("Probing to resolve {} IP {}", hostToProbe, ipToProbe); |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 1147 | hostService.startMonitoringIp(ipToProbe); |
| 1148 | |
| 1149 | Set<Host> hosts = hostService.getHostsByIp(ipToProbe); |
| 1150 | if (!hosts.isEmpty()) { |
| 1151 | Host host = hosts.iterator().next(); |
Yi Tseng | 919b2df | 2017-09-07 16:22:51 -0700 | [diff] [blame] | 1152 | newServerInfo.setDhcpConnectVlan(host.vlan()); |
| 1153 | newServerInfo.setDhcpConnectMac(host.mac()); |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 1154 | } |
Yi Tseng | 919b2df | 2017-09-07 16:22:51 -0700 | [diff] [blame] | 1155 | // Add new server info |
| 1156 | serverInfoList.add(0, newServerInfo); |
Yi Tseng | 483ac6f | 2017-08-02 15:03:31 -0700 | [diff] [blame] | 1157 | |
Yi Tseng | 919b2df | 2017-09-07 16:22:51 -0700 | [diff] [blame] | 1158 | // Remove duplicated server info |
| 1159 | Set<DhcpServerInfo> nonDupServerInfoList = Sets.newLinkedHashSet(); |
| 1160 | nonDupServerInfoList.addAll(serverInfoList); |
| 1161 | serverInfoList.clear(); |
| 1162 | serverInfoList.addAll(nonDupServerInfoList); |
Yi Tseng | 525ff40 | 2017-10-23 19:39:39 -0700 | [diff] [blame] | 1163 | requestDhcpPacket(serverIp); |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 1164 | } |
| 1165 | |
| 1166 | class InternalHostListener implements HostListener { |
| 1167 | @Override |
| 1168 | public void event(HostEvent event) { |
| 1169 | switch (event.type()) { |
| 1170 | case HOST_ADDED: |
| 1171 | case HOST_UPDATED: |
| 1172 | hostUpdated(event.subject()); |
| 1173 | break; |
| 1174 | case HOST_REMOVED: |
| 1175 | hostRemoved(event.subject()); |
| 1176 | break; |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 1177 | default: |
| 1178 | break; |
| 1179 | } |
| 1180 | } |
| 1181 | } |
| 1182 | |
| 1183 | /** |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 1184 | * Handle host updated. |
| 1185 | * If the host is DHCP server or gateway, update connect mac and vlan. |
| 1186 | * |
| 1187 | * @param host the host |
| 1188 | */ |
| 1189 | private void hostUpdated(Host host) { |
Yi Tseng | 525ff40 | 2017-10-23 19:39:39 -0700 | [diff] [blame] | 1190 | hostUpdated(host, defaultServerInfoList); |
| 1191 | hostUpdated(host, indirectServerInfoList); |
Yi Tseng | 919b2df | 2017-09-07 16:22:51 -0700 | [diff] [blame] | 1192 | reloadServerSettings(); |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 1193 | } |
| 1194 | |
Yi Tseng | 525ff40 | 2017-10-23 19:39:39 -0700 | [diff] [blame] | 1195 | private void hostUpdated(Host host, List<DhcpServerInfo> serverInfoList) { |
| 1196 | DhcpServerInfo serverInfo; |
| 1197 | Ip6Address targetIp; |
| 1198 | if (!serverInfoList.isEmpty()) { |
| 1199 | serverInfo = serverInfoList.get(0); |
| 1200 | Ip6Address serverIp = serverInfo.getDhcpServerIp6().orElse(null); |
| 1201 | targetIp = serverInfo.getDhcpGatewayIp6().orElse(null); |
| 1202 | |
| 1203 | if (targetIp == null) { |
| 1204 | targetIp = serverIp; |
| 1205 | } |
| 1206 | |
| 1207 | if (targetIp != null) { |
| 1208 | if (host.ipAddresses().contains(targetIp)) { |
| 1209 | serverInfo.setDhcpConnectMac(host.mac()); |
| 1210 | serverInfo.setDhcpConnectVlan(host.vlan()); |
| 1211 | requestDhcpPacket(serverIp); |
| 1212 | } |
| 1213 | } |
| 1214 | } |
| 1215 | } |
| 1216 | |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 1217 | /** |
| 1218 | * Handle host removed. |
| 1219 | * If the host is DHCP server or gateway, unset connect mac and vlan. |
| 1220 | * |
| 1221 | * @param host the host |
| 1222 | */ |
| 1223 | private void hostRemoved(Host host) { |
Yi Tseng | 525ff40 | 2017-10-23 19:39:39 -0700 | [diff] [blame] | 1224 | hostRemoved(host, defaultServerInfoList); |
| 1225 | hostRemoved(host, indirectServerInfoList); |
Yi Tseng | 919b2df | 2017-09-07 16:22:51 -0700 | [diff] [blame] | 1226 | reloadServerSettings(); |
| 1227 | } |
| 1228 | |
Yi Tseng | 525ff40 | 2017-10-23 19:39:39 -0700 | [diff] [blame] | 1229 | private void hostRemoved(Host host, List<DhcpServerInfo> serverInfoList) { |
| 1230 | DhcpServerInfo serverInfo; |
| 1231 | Ip6Address targetIp; |
| 1232 | |
| 1233 | if (!serverInfoList.isEmpty()) { |
| 1234 | serverInfo = serverInfoList.get(0); |
| 1235 | Ip6Address serverIp = serverInfo.getDhcpServerIp6().orElse(null); |
| 1236 | targetIp = serverInfo.getDhcpGatewayIp6().orElse(null); |
| 1237 | |
| 1238 | if (targetIp == null) { |
| 1239 | targetIp = serverIp; |
| 1240 | } |
| 1241 | |
| 1242 | if (targetIp != null) { |
| 1243 | if (host.ipAddresses().contains(targetIp)) { |
| 1244 | serverInfo.setDhcpConnectVlan(null); |
| 1245 | serverInfo.setDhcpConnectMac(null); |
| 1246 | cancelDhcpPacket(serverIp); |
| 1247 | } |
| 1248 | } |
| 1249 | } |
| 1250 | } |
| 1251 | |
Yi Tseng | 919b2df | 2017-09-07 16:22:51 -0700 | [diff] [blame] | 1252 | private void reloadServerSettings() { |
| 1253 | DhcpServerInfo serverInfo; |
| 1254 | if (!defaultServerInfoList.isEmpty()) { |
| 1255 | serverInfo = defaultServerInfoList.get(0); |
| 1256 | this.dhcpConnectMac = serverInfo.getDhcpConnectMac().orElse(null); |
| 1257 | this.dhcpGatewayIp = serverInfo.getDhcpGatewayIp6().orElse(null); |
| 1258 | this.dhcpServerIp = serverInfo.getDhcpServerIp6().orElse(null); |
| 1259 | this.dhcpServerConnectPoint = serverInfo.getDhcpServerConnectPoint().orElse(null); |
| 1260 | this.dhcpConnectVlan = serverInfo.getDhcpConnectVlan().orElse(null); |
| 1261 | } |
| 1262 | |
| 1263 | if (!indirectServerInfoList.isEmpty()) { |
| 1264 | serverInfo = indirectServerInfoList.get(0); |
| 1265 | this.indirectDhcpConnectMac = serverInfo.getDhcpConnectMac().orElse(null); |
| 1266 | this.indirectDhcpGatewayIp = serverInfo.getDhcpGatewayIp6().orElse(null); |
| 1267 | this.indirectDhcpServerIp = serverInfo.getDhcpServerIp6().orElse(null); |
| 1268 | this.indirectDhcpServerConnectPoint = serverInfo.getDhcpServerConnectPoint().orElse(null); |
| 1269 | this.indirectDhcpConnectVlan = serverInfo.getDhcpConnectVlan().orElse(null); |
| 1270 | this.indirectRelayAgentIpFromCfg = serverInfo.getRelayAgentIp6().orElse(null); |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 1271 | } |
Yi Tseng | 483ac6f | 2017-08-02 15:03:31 -0700 | [diff] [blame] | 1272 | } |
Kalhee Kim | 45b2418 | 2017-10-18 18:30:23 +0000 | [diff] [blame] | 1273 | |
| 1274 | /** |
| 1275 | * Returns the first interface ip from interface. |
| 1276 | * |
| 1277 | * @param iface interface of one connect point |
| 1278 | * @return the first interface IP; null if not exists an IP address in |
| 1279 | * these interfaces |
| 1280 | */ |
| 1281 | private Ip6Address getFirstIpFromInterface(Interface iface) { |
| 1282 | checkNotNull(iface, "Interface can't be null"); |
| 1283 | return iface.ipAddressesList().stream() |
| 1284 | .map(InterfaceIpAddress::ipAddress) |
| 1285 | .filter(IpAddress::isIp6) |
| 1286 | .map(IpAddress::getIp6Address) |
| 1287 | .findFirst() |
| 1288 | .orElse(null); |
| 1289 | } |
| 1290 | |
| 1291 | /** |
| 1292 | * Gets Interface facing to the server for default host. |
| 1293 | * |
| 1294 | * @return the Interface facing to the server; null if not found |
| 1295 | */ |
| 1296 | private Interface getServerInterface() { |
| 1297 | if (dhcpServerConnectPoint == null || dhcpConnectVlan == null) { |
| 1298 | return null; |
| 1299 | } |
| 1300 | return interfaceService.getInterfacesByPort(dhcpServerConnectPoint) |
| 1301 | .stream() |
| 1302 | .filter(iface -> interfaceContainsVlan(iface, dhcpConnectVlan)) |
| 1303 | .findFirst() |
| 1304 | .orElse(null); |
| 1305 | } |
| 1306 | |
| 1307 | /** |
| 1308 | * Gets Interface facing to the server for indirect hosts. |
| 1309 | * Use default server Interface if indirect server not configured. |
| 1310 | * |
| 1311 | * @return the Interface facing to the server; null if not found |
| 1312 | */ |
| 1313 | private Interface getIndirectServerInterface() { |
| 1314 | if (indirectDhcpServerConnectPoint == null || indirectDhcpConnectVlan == null) { |
| 1315 | return getServerInterface(); |
| 1316 | } |
| 1317 | return interfaceService.getInterfacesByPort(indirectDhcpServerConnectPoint) |
| 1318 | .stream() |
| 1319 | .filter(iface -> interfaceContainsVlan(iface, indirectDhcpConnectVlan)) |
| 1320 | .findFirst() |
| 1321 | .orElse(null); |
| 1322 | } |
| 1323 | |
Kalhee Kim | 0c0cb0b | 2017-09-15 17:43:27 +0000 | [diff] [blame] | 1324 | /** |
| 1325 | * Determind if an Interface contains a vlan id. |
| 1326 | * |
| 1327 | * @param iface the Interface |
| 1328 | * @param vlanId the vlan id |
| 1329 | * @return true if the Interface contains the vlan id |
| 1330 | */ |
| 1331 | private boolean interfaceContainsVlan(Interface iface, VlanId vlanId) { |
| 1332 | if (vlanId.equals(VlanId.NONE)) { |
| 1333 | // untagged packet, check if vlan untagged or vlan native is not NONE |
| 1334 | return !iface.vlanUntagged().equals(VlanId.NONE) || |
| 1335 | !iface.vlanNative().equals(VlanId.NONE); |
| 1336 | } |
| 1337 | // tagged packet, check if the interface contains the vlan |
| 1338 | return iface.vlanTagged().contains(vlanId); |
| 1339 | } |
| 1340 | |
Yi Tseng | 525ff40 | 2017-10-23 19:39:39 -0700 | [diff] [blame] | 1341 | private void requestDhcpPacket(Ip6Address serverIp) { |
| 1342 | requestServerDhcpPacket(serverIp); |
| 1343 | requestClientDhcpPacket(serverIp); |
| 1344 | } |
| 1345 | |
| 1346 | private void cancelDhcpPacket(Ip6Address serverIp) { |
| 1347 | cancelServerDhcpPacket(serverIp); |
| 1348 | cancelClientDhcpPacket(serverIp); |
| 1349 | } |
| 1350 | |
| 1351 | private void cancelServerDhcpPacket(Ip6Address serverIp) { |
| 1352 | TrafficSelector serverSelector = |
| 1353 | DefaultTrafficSelector.builder(SERVER_RELAY_SELECTOR) |
| 1354 | .matchIPv6Src(serverIp.toIpPrefix()) |
| 1355 | .build(); |
| 1356 | packetService.cancelPackets(serverSelector, |
| 1357 | PacketPriority.CONTROL, |
| 1358 | appId); |
| 1359 | } |
| 1360 | |
| 1361 | private void requestServerDhcpPacket(Ip6Address serverIp) { |
| 1362 | TrafficSelector serverSelector = |
| 1363 | DefaultTrafficSelector.builder(SERVER_RELAY_SELECTOR) |
| 1364 | .matchIPv6Src(serverIp.toIpPrefix()) |
| 1365 | .build(); |
| 1366 | packetService.requestPackets(serverSelector, |
| 1367 | PacketPriority.CONTROL, |
| 1368 | appId); |
| 1369 | } |
| 1370 | |
| 1371 | private void cancelClientDhcpPacket(Ip6Address serverIp) { |
| 1372 | // Packet comes from relay |
| 1373 | TrafficSelector indirectClientSelector = |
| 1374 | DefaultTrafficSelector.builder(SERVER_RELAY_SELECTOR) |
| 1375 | .matchIPv6Dst(serverIp.toIpPrefix()) |
| 1376 | .build(); |
| 1377 | packetService.cancelPackets(indirectClientSelector, |
| 1378 | PacketPriority.CONTROL, |
| 1379 | appId); |
Yi Tseng | 41dde70 | 2017-11-02 15:21:10 -0700 | [diff] [blame] | 1380 | indirectClientSelector = |
| 1381 | DefaultTrafficSelector.builder(SERVER_RELAY_SELECTOR) |
| 1382 | .matchIPv6Dst(Ip6Address.ALL_DHCP_RELAY_AGENTS_AND_SERVERS.toIpPrefix()) |
| 1383 | .build(); |
| 1384 | packetService.cancelPackets(indirectClientSelector, |
| 1385 | PacketPriority.CONTROL, |
| 1386 | appId); |
| 1387 | indirectClientSelector = |
| 1388 | DefaultTrafficSelector.builder(SERVER_RELAY_SELECTOR) |
| 1389 | .matchIPv6Dst(Ip6Address.ALL_DHCP_SERVERS.toIpPrefix()) |
| 1390 | .build(); |
| 1391 | packetService.cancelPackets(indirectClientSelector, |
| 1392 | PacketPriority.CONTROL, |
| 1393 | appId); |
Yi Tseng | 525ff40 | 2017-10-23 19:39:39 -0700 | [diff] [blame] | 1394 | |
| 1395 | // Packet comes from client |
| 1396 | packetService.cancelPackets(CLIENT_SERVER_SELECTOR, |
| 1397 | PacketPriority.CONTROL, |
| 1398 | appId); |
| 1399 | } |
| 1400 | |
| 1401 | private void requestClientDhcpPacket(Ip6Address serverIp) { |
| 1402 | // Packet comes from relay |
| 1403 | TrafficSelector indirectClientSelector = |
| 1404 | DefaultTrafficSelector.builder(SERVER_RELAY_SELECTOR) |
| 1405 | .matchIPv6Dst(serverIp.toIpPrefix()) |
| 1406 | .build(); |
| 1407 | packetService.requestPackets(indirectClientSelector, |
| 1408 | PacketPriority.CONTROL, |
| 1409 | appId); |
Yi Tseng | 41dde70 | 2017-11-02 15:21:10 -0700 | [diff] [blame] | 1410 | indirectClientSelector = |
| 1411 | DefaultTrafficSelector.builder(SERVER_RELAY_SELECTOR) |
| 1412 | .matchIPv6Dst(Ip6Address.ALL_DHCP_RELAY_AGENTS_AND_SERVERS.toIpPrefix()) |
| 1413 | .build(); |
| 1414 | packetService.requestPackets(indirectClientSelector, |
| 1415 | PacketPriority.CONTROL, |
| 1416 | appId); |
| 1417 | indirectClientSelector = |
| 1418 | DefaultTrafficSelector.builder(SERVER_RELAY_SELECTOR) |
| 1419 | .matchIPv6Dst(Ip6Address.ALL_DHCP_SERVERS.toIpPrefix()) |
| 1420 | .build(); |
| 1421 | packetService.requestPackets(indirectClientSelector, |
| 1422 | PacketPriority.CONTROL, |
| 1423 | appId); |
Yi Tseng | 525ff40 | 2017-10-23 19:39:39 -0700 | [diff] [blame] | 1424 | |
| 1425 | // Packet comes from client |
| 1426 | packetService.requestPackets(CLIENT_SERVER_SELECTOR, |
| 1427 | PacketPriority.CONTROL, |
| 1428 | appId); |
| 1429 | } |
| 1430 | |
| 1431 | /** |
| 1432 | * Process the ignore rules. |
| 1433 | * |
| 1434 | * @param deviceId the device id |
| 1435 | * @param vlanId the vlan to be ignored |
| 1436 | * @param op the operation, ADD to install; REMOVE to uninstall rules |
| 1437 | */ |
| 1438 | private void processIgnoreVlanRule(DeviceId deviceId, VlanId vlanId, Objective.Operation op) { |
| 1439 | TrafficTreatment dropTreatment = DefaultTrafficTreatment.builder().wipeDeferred().build(); |
| 1440 | AtomicInteger installedCount = new AtomicInteger(DHCP_SELECTORS.size()); |
| 1441 | DHCP_SELECTORS.forEach(trafficSelector -> { |
| 1442 | TrafficSelector selector = DefaultTrafficSelector.builder(trafficSelector) |
| 1443 | .matchVlanId(vlanId) |
| 1444 | .build(); |
| 1445 | |
| 1446 | ForwardingObjective.Builder builder = DefaultForwardingObjective.builder() |
| 1447 | .withFlag(ForwardingObjective.Flag.VERSATILE) |
| 1448 | .withSelector(selector) |
| 1449 | .withPriority(IGNORE_CONTROL_PRIORITY) |
| 1450 | .withTreatment(dropTreatment) |
| 1451 | .fromApp(appId); |
| 1452 | |
| 1453 | |
| 1454 | ObjectiveContext objectiveContext = new ObjectiveContext() { |
| 1455 | @Override |
| 1456 | public void onSuccess(Objective objective) { |
| 1457 | log.info("Ignore rule {} (Vlan id {}, device {}, selector {})", |
| 1458 | op, vlanId, deviceId, selector); |
| 1459 | int countDown = installedCount.decrementAndGet(); |
| 1460 | if (countDown != 0) { |
| 1461 | return; |
| 1462 | } |
| 1463 | switch (op) { |
| 1464 | case ADD: |
| 1465 | ignoredVlans.put(deviceId, vlanId); |
| 1466 | break; |
| 1467 | case REMOVE: |
| 1468 | ignoredVlans.remove(deviceId, vlanId); |
| 1469 | break; |
| 1470 | default: |
| 1471 | log.warn("Unsupported objective operation {}", op); |
| 1472 | break; |
| 1473 | } |
| 1474 | } |
| 1475 | |
| 1476 | @Override |
| 1477 | public void onError(Objective objective, ObjectiveError error) { |
| 1478 | log.warn("Can't {} ignore rule (vlan id {}, selector {}, device {}) due to {}", |
| 1479 | op, vlanId, selector, deviceId, error); |
| 1480 | } |
| 1481 | }; |
| 1482 | |
| 1483 | ForwardingObjective fwd; |
| 1484 | switch (op) { |
| 1485 | case ADD: |
| 1486 | fwd = builder.add(objectiveContext); |
| 1487 | break; |
| 1488 | case REMOVE: |
| 1489 | fwd = builder.remove(objectiveContext); |
| 1490 | break; |
| 1491 | default: |
| 1492 | log.warn("Unsupported objective operation {}", op); |
| 1493 | return; |
| 1494 | } |
| 1495 | |
| 1496 | Device device = deviceService.getDevice(deviceId); |
| 1497 | if (device == null || !device.is(Pipeliner.class)) { |
| 1498 | log.warn("Device {} is not available now, wait until device is available", deviceId); |
| 1499 | return; |
| 1500 | } |
| 1501 | flowObjectiveService.apply(deviceId, fwd); |
| 1502 | }); |
| 1503 | } |
Kalhee Kim | 121ba92 | 2017-11-01 17:56:44 +0000 | [diff] [blame^] | 1504 | |
| 1505 | /** |
| 1506 | * Find first ipaddress for a given Host info i.e. mac and vlan. |
| 1507 | * |
| 1508 | * @param clientMac client mac |
| 1509 | * @param vlanId packet's vlan |
| 1510 | * @return next-hop link-local ipaddress for a given host |
| 1511 | */ |
| 1512 | private IpAddress getFirstIpByHost(MacAddress clientMac, VlanId vlanId) { |
| 1513 | IpAddress nextHopIp; |
| 1514 | // pick out the first link-local ip address |
| 1515 | HostId gwHostId = HostId.hostId(clientMac, vlanId); |
| 1516 | Host gwHost = hostService.getHost(gwHostId); |
| 1517 | if (gwHost == null) { |
| 1518 | log.warn("Can't find gateway host for hostId {}", gwHostId); |
| 1519 | return null; |
| 1520 | } |
| 1521 | nextHopIp = gwHost.ipAddresses() |
| 1522 | .stream() |
| 1523 | .filter(IpAddress::isIp6) |
| 1524 | .filter(ip6 -> ip6.isLinkLocal()) |
| 1525 | .map(IpAddress::getIp6Address) |
| 1526 | .findFirst() |
| 1527 | .orElse(null); |
| 1528 | return nextHopIp; |
| 1529 | } |
Yi Tseng | 5130129 | 2017-07-28 13:02:59 -0700 | [diff] [blame] | 1530 | } |