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 | */ |
Yi Tseng | 5130129 | 2017-07-28 13:02:59 -0700 | [diff] [blame] | 17 | package org.onosproject.dhcprelay; |
| 18 | |
Yi Tseng | 525ff40 | 2017-10-23 19:39:39 -0700 | [diff] [blame] | 19 | import com.google.common.collect.HashMultimap; |
Yi Tseng | 919b2df | 2017-09-07 16:22:51 -0700 | [diff] [blame] | 20 | import com.google.common.collect.Lists; |
Yi Tseng | 525ff40 | 2017-10-23 19:39:39 -0700 | [diff] [blame] | 21 | import com.google.common.collect.Multimap; |
Charles Chan | 70fdd49 | 2018-03-07 17:36:06 -0800 | [diff] [blame] | 22 | import com.google.common.collect.Multimaps; |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 23 | import com.google.common.collect.Sets; |
| 24 | import com.google.common.collect.ImmutableSet; |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 25 | import org.osgi.service.component.annotations.Activate; |
| 26 | import org.osgi.service.component.annotations.Component; |
| 27 | import org.osgi.service.component.annotations.Deactivate; |
| 28 | import org.osgi.service.component.annotations.Modified; |
| 29 | import org.osgi.service.component.annotations.Reference; |
| 30 | import org.osgi.service.component.annotations.ReferenceCardinality; |
Yi Tseng | 5130129 | 2017-07-28 13:02:59 -0700 | [diff] [blame] | 31 | import org.onlab.packet.BasePacket; |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 32 | import org.onlab.packet.DHCP6; |
| 33 | import org.onlab.packet.IPv6; |
| 34 | import org.onlab.packet.Ethernet; |
| 35 | import org.onlab.packet.Ip6Address; |
Yi Tseng | 5130129 | 2017-07-28 13:02:59 -0700 | [diff] [blame] | 36 | import org.onlab.packet.IpAddress; |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 37 | import org.onlab.packet.IpPrefix; |
Yi Tseng | 5130129 | 2017-07-28 13:02:59 -0700 | [diff] [blame] | 38 | import org.onlab.packet.MacAddress; |
Yi Tseng | 525ff40 | 2017-10-23 19:39:39 -0700 | [diff] [blame] | 39 | import org.onlab.packet.TpPort; |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 40 | import org.onlab.packet.UDP; |
Yi Tseng | 5130129 | 2017-07-28 13:02:59 -0700 | [diff] [blame] | 41 | import org.onlab.packet.VlanId; |
Lior Assouline | a21c0ca | 2018-01-28 16:18:48 -0800 | [diff] [blame] | 42 | import org.onlab.packet.dhcp.Dhcp6ClientDataOption; |
| 43 | import org.onlab.packet.dhcp.Dhcp6LeaseQueryOption; |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 44 | import org.onlab.packet.dhcp.Dhcp6RelayOption; |
| 45 | import org.onlab.packet.dhcp.Dhcp6InterfaceIdOption; |
Lior Assouline | a21c0ca | 2018-01-28 16:18:48 -0800 | [diff] [blame] | 46 | import org.onlab.packet.dhcp.Dhcp6Option; |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 47 | import org.onlab.packet.dhcp.Dhcp6IaNaOption; |
| 48 | import org.onlab.packet.dhcp.Dhcp6IaTaOption; |
| 49 | import org.onlab.packet.dhcp.Dhcp6IaPdOption; |
| 50 | import org.onlab.packet.dhcp.Dhcp6IaAddressOption; |
| 51 | import org.onlab.packet.dhcp.Dhcp6IaPrefixOption; |
Kalhee Kim | ea4b6c2 | 2017-11-09 14:38:37 +0000 | [diff] [blame] | 52 | import org.onlab.packet.dhcp.Dhcp6ClientIdOption; |
| 53 | import org.onlab.packet.dhcp.Dhcp6Duid; |
Kalhee Kim | 495c9b2 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 54 | import org.onlab.packet.DHCP6.MsgType; |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 55 | import org.onlab.util.HexString; |
Taras Lemkin | 96a0d34 | 2018-03-26 14:52:58 +0000 | [diff] [blame] | 56 | import org.onlab.util.Tools; |
| 57 | import org.onosproject.cfg.ComponentConfigService; |
Yi Tseng | 525ff40 | 2017-10-23 19:39:39 -0700 | [diff] [blame] | 58 | import org.onosproject.core.ApplicationId; |
| 59 | import org.onosproject.core.CoreService; |
Yi Tseng | 5130129 | 2017-07-28 13:02:59 -0700 | [diff] [blame] | 60 | import org.onosproject.dhcprelay.api.DhcpHandler; |
Yi Tseng | 919b2df | 2017-09-07 16:22:51 -0700 | [diff] [blame] | 61 | import org.onosproject.dhcprelay.api.DhcpServerInfo; |
Yi Tseng | 525ff40 | 2017-10-23 19:39:39 -0700 | [diff] [blame] | 62 | import org.onosproject.dhcprelay.config.IgnoreDhcpConfig; |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 63 | import org.onosproject.dhcprelay.store.DhcpRelayStore; |
Kalhee Kim | ea4b6c2 | 2017-11-09 14:38:37 +0000 | [diff] [blame] | 64 | import org.onosproject.dhcprelay.store.DhcpRecord; |
Kalhee Kim | ba36606 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 65 | import org.onosproject.dhcprelay.store.DhcpFpmPrefixStore; |
Kalhee Kim | d94ceea | 2017-11-29 19:03:02 +0000 | [diff] [blame] | 66 | import org.onosproject.dhcprelay.store.DhcpRelayCountersStore; |
Yi Tseng | 525ff40 | 2017-10-23 19:39:39 -0700 | [diff] [blame] | 67 | import org.onosproject.net.Device; |
| 68 | import org.onosproject.net.DeviceId; |
Kalhee Kim | ba36606 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 69 | import org.onosproject.routing.fpm.api.FpmRecord; |
Yi Tseng | 525ff40 | 2017-10-23 19:39:39 -0700 | [diff] [blame] | 70 | import org.onosproject.net.behaviour.Pipeliner; |
| 71 | import org.onosproject.net.device.DeviceService; |
| 72 | import org.onosproject.net.flow.DefaultTrafficSelector; |
| 73 | import org.onosproject.net.flow.TrafficSelector; |
| 74 | import org.onosproject.net.flowobjective.DefaultForwardingObjective; |
| 75 | import org.onosproject.net.flowobjective.FlowObjectiveService; |
| 76 | import org.onosproject.net.flowobjective.ForwardingObjective; |
| 77 | import org.onosproject.net.flowobjective.Objective; |
| 78 | import org.onosproject.net.flowobjective.ObjectiveContext; |
| 79 | import org.onosproject.net.flowobjective.ObjectiveError; |
Yi Tseng | aa417a6 | 2017-09-08 17:22:51 -0700 | [diff] [blame] | 80 | import org.onosproject.net.host.HostProvider; |
| 81 | import org.onosproject.net.host.HostProviderRegistry; |
| 82 | import org.onosproject.net.host.HostProviderService; |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 83 | import org.onosproject.net.host.HostService; |
| 84 | import org.onosproject.net.host.DefaultHostDescription; |
| 85 | import org.onosproject.net.host.HostDescription; |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 86 | import org.onosproject.net.host.HostListener; |
| 87 | import org.onosproject.net.host.HostEvent; |
| 88 | import org.onosproject.net.intf.Interface; |
| 89 | import org.onosproject.net.intf.InterfaceService; |
Yi Tseng | 525ff40 | 2017-10-23 19:39:39 -0700 | [diff] [blame] | 90 | import org.onosproject.net.packet.PacketPriority; |
Yi Tseng | aa417a6 | 2017-09-08 17:22:51 -0700 | [diff] [blame] | 91 | import org.onosproject.net.provider.ProviderId; |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 92 | import org.onosproject.routeservice.Route; |
| 93 | import org.onosproject.routeservice.RouteStore; |
Yi Tseng | 483ac6f | 2017-08-02 15:03:31 -0700 | [diff] [blame] | 94 | import org.onosproject.dhcprelay.config.DhcpServerConfig; |
Yi Tseng | 5130129 | 2017-07-28 13:02:59 -0700 | [diff] [blame] | 95 | import org.onosproject.net.ConnectPoint; |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 96 | import org.onosproject.net.Host; |
| 97 | import org.onosproject.net.HostId; |
| 98 | import org.onosproject.net.HostLocation; |
| 99 | import org.onosproject.net.packet.DefaultOutboundPacket; |
| 100 | import org.onosproject.net.packet.OutboundPacket; |
Yi Tseng | 5130129 | 2017-07-28 13:02:59 -0700 | [diff] [blame] | 101 | import org.onosproject.net.packet.PacketContext; |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 102 | import org.onosproject.net.packet.PacketService; |
Taras Lemkin | 96a0d34 | 2018-03-26 14:52:58 +0000 | [diff] [blame] | 103 | import org.osgi.service.component.ComponentContext; |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 104 | import org.slf4j.Logger; |
| 105 | import org.slf4j.LoggerFactory; |
| 106 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
| 107 | import org.onosproject.net.flow.TrafficTreatment; |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 108 | import java.nio.ByteBuffer; |
Taras Lemkin | 96a0d34 | 2018-03-26 14:52:58 +0000 | [diff] [blame] | 109 | import java.util.ArrayList; |
Yi Tseng | 483ac6f | 2017-08-02 15:03:31 -0700 | [diff] [blame] | 110 | import java.util.Collection; |
Taras Lemkin | 96a0d34 | 2018-03-26 14:52:58 +0000 | [diff] [blame] | 111 | import java.util.Dictionary; |
| 112 | import java.util.List; |
Yi Tseng | 5130129 | 2017-07-28 13:02:59 -0700 | [diff] [blame] | 113 | import java.util.Optional; |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 114 | import java.util.Set; |
Charles Chan | 909cff8 | 2018-03-05 13:14:02 -0800 | [diff] [blame] | 115 | import java.util.concurrent.CopyOnWriteArrayList; |
Jordan Halterman | 6328db7 | 2018-04-10 13:34:50 -0400 | [diff] [blame] | 116 | import java.util.concurrent.Executor; |
Yi Tseng | 525ff40 | 2017-10-23 19:39:39 -0700 | [diff] [blame] | 117 | import java.util.concurrent.atomic.AtomicInteger; |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 118 | import static com.google.common.base.Preconditions.checkNotNull; |
| 119 | import static com.google.common.base.Preconditions.checkState; |
Jordan Halterman | 6328db7 | 2018-04-10 13:34:50 -0400 | [diff] [blame] | 120 | import static java.util.concurrent.Executors.newSingleThreadExecutor; |
| 121 | import static org.onlab.util.Tools.groupedThreads; |
Ray Milkey | 687c00c | 2018-10-31 10:18:41 -0700 | [diff] [blame] | 122 | import static org.onosproject.dhcprelay.OsgiPropertyConstants.LEARN_ROUTE_FROM_LEASE_QUERY; |
| 123 | import static org.onosproject.dhcprelay.OsgiPropertyConstants.LEARN_ROUTE_FROM_LEASE_QUERY_DEFAULT; |
Yi Tseng | 525ff40 | 2017-10-23 19:39:39 -0700 | [diff] [blame] | 124 | import static org.onosproject.net.flowobjective.Objective.Operation.ADD; |
| 125 | import static org.onosproject.net.flowobjective.Objective.Operation.REMOVE; |
Kalhee Kim | d94ceea | 2017-11-29 19:03:02 +0000 | [diff] [blame] | 126 | import java.util.concurrent.Semaphore; |
Yi Tseng | 5130129 | 2017-07-28 13:02:59 -0700 | [diff] [blame] | 127 | |
Ray Milkey | 687c00c | 2018-10-31 10:18:41 -0700 | [diff] [blame] | 128 | @Component( |
| 129 | service = { DhcpHandler.class, HostProvider.class }, |
| 130 | property = { |
Ray Milkey | 5504bd2 | 2019-03-22 16:24:38 -0700 | [diff] [blame] | 131 | "_version:Integer = 6", |
Ray Milkey | 687c00c | 2018-10-31 10:18:41 -0700 | [diff] [blame] | 132 | LEARN_ROUTE_FROM_LEASE_QUERY + ":Boolean=" + LEARN_ROUTE_FROM_LEASE_QUERY_DEFAULT |
| 133 | } |
| 134 | ) |
| 135 | |
Yi Tseng | aa417a6 | 2017-09-08 17:22:51 -0700 | [diff] [blame] | 136 | public class Dhcp6HandlerImpl implements DhcpHandler, HostProvider { |
Charles Chan | d988c28 | 2017-09-12 17:09:32 -0700 | [diff] [blame] | 137 | public static final String DHCP_V6_RELAY_APP = "org.onosproject.Dhcp6HandlerImpl"; |
Saurav Das | 7c6dec1 | 2017-09-13 14:35:56 -0700 | [diff] [blame] | 138 | 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] | 139 | private static final int IGNORE_CONTROL_PRIORITY = PacketPriority.CONTROL.priorityValue() + 1000; |
Kalhee Kim | d94ceea | 2017-11-29 19:03:02 +0000 | [diff] [blame] | 140 | private String gCount = "global"; |
Yi Tseng | 525ff40 | 2017-10-23 19:39:39 -0700 | [diff] [blame] | 141 | private static final TrafficSelector CLIENT_SERVER_SELECTOR = DefaultTrafficSelector.builder() |
| 142 | .matchEthType(Ethernet.TYPE_IPV6) |
| 143 | .matchIPProtocol(IPv6.PROTOCOL_UDP) |
| 144 | .matchIPv6Src(IpPrefix.IPV6_LINK_LOCAL_PREFIX) |
| 145 | .matchIPv6Dst(Ip6Address.ALL_DHCP_RELAY_AGENTS_AND_SERVERS.toIpPrefix()) |
| 146 | .matchUdpSrc(TpPort.tpPort(UDP.DHCP_V6_CLIENT_PORT)) |
| 147 | .matchUdpDst(TpPort.tpPort(UDP.DHCP_V6_SERVER_PORT)) |
| 148 | .build(); |
| 149 | private static final TrafficSelector SERVER_RELAY_SELECTOR = DefaultTrafficSelector.builder() |
| 150 | .matchEthType(Ethernet.TYPE_IPV6) |
| 151 | .matchIPProtocol(IPv6.PROTOCOL_UDP) |
| 152 | .matchUdpSrc(TpPort.tpPort(UDP.DHCP_V6_SERVER_PORT)) |
| 153 | .matchUdpDst(TpPort.tpPort(UDP.DHCP_V6_SERVER_PORT)) |
| 154 | .build(); |
Lior Assouline | a21c0ca | 2018-01-28 16:18:48 -0800 | [diff] [blame] | 155 | // lease query reply is from server to client (no relay in between) - so we need to |
| 156 | // catch that scenario also .. |
| 157 | private static final TrafficSelector LEASE_QUERY_RESPONSE_SELECTOR = DefaultTrafficSelector.builder() |
| 158 | .matchEthType(Ethernet.TYPE_IPV6) |
| 159 | .matchIPProtocol(IPv6.PROTOCOL_UDP) |
| 160 | .matchUdpSrc(TpPort.tpPort(UDP.DHCP_V6_SERVER_PORT)) |
| 161 | .matchUdpDst(TpPort.tpPort(UDP.DHCP_V6_CLIENT_PORT)) |
| 162 | .build(); |
Yi Tseng | 525ff40 | 2017-10-23 19:39:39 -0700 | [diff] [blame] | 163 | static final Set<TrafficSelector> DHCP_SELECTORS = ImmutableSet.of( |
| 164 | CLIENT_SERVER_SELECTOR, |
Lior Assouline | a21c0ca | 2018-01-28 16:18:48 -0800 | [diff] [blame] | 165 | SERVER_RELAY_SELECTOR, |
| 166 | LEASE_QUERY_RESPONSE_SELECTOR |
Yi Tseng | 525ff40 | 2017-10-23 19:39:39 -0700 | [diff] [blame] | 167 | ); |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 168 | private static Logger log = LoggerFactory.getLogger(Dhcp6HandlerImpl.class); |
Yi Tseng | 5130129 | 2017-07-28 13:02:59 -0700 | [diff] [blame] | 169 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 170 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 171 | protected DhcpRelayStore dhcpRelayStore; |
Yi Tseng | 5130129 | 2017-07-28 13:02:59 -0700 | [diff] [blame] | 172 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 173 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Kalhee Kim | d94ceea | 2017-11-29 19:03:02 +0000 | [diff] [blame] | 174 | protected DhcpRelayCountersStore dhcpRelayCountersStore; |
| 175 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 176 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 177 | protected PacketService packetService; |
| 178 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 179 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 180 | protected RouteStore routeStore; |
| 181 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 182 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 183 | protected InterfaceService interfaceService; |
| 184 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 185 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 186 | protected HostService hostService; |
| 187 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 188 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Yi Tseng | aa417a6 | 2017-09-08 17:22:51 -0700 | [diff] [blame] | 189 | protected HostProviderRegistry providerRegistry; |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 190 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 191 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Yi Tseng | 525ff40 | 2017-10-23 19:39:39 -0700 | [diff] [blame] | 192 | protected CoreService coreService; |
| 193 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 194 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Kalhee Kim | ba36606 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 195 | protected DhcpFpmPrefixStore dhcpFpmPrefixStore; |
| 196 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 197 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Yi Tseng | 525ff40 | 2017-10-23 19:39:39 -0700 | [diff] [blame] | 198 | protected DeviceService deviceService; |
| 199 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 200 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Yi Tseng | 525ff40 | 2017-10-23 19:39:39 -0700 | [diff] [blame] | 201 | protected FlowObjectiveService flowObjectiveService; |
| 202 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 203 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Taras Lemkin | 96a0d34 | 2018-03-26 14:52:58 +0000 | [diff] [blame] | 204 | protected ComponentConfigService cfgService; |
| 205 | |
Ray Milkey | 687c00c | 2018-10-31 10:18:41 -0700 | [diff] [blame] | 206 | /** Enable learning routing information from LQ. */ |
| 207 | private Boolean learnRouteFromLeasequery = LEARN_ROUTE_FROM_LEASE_QUERY_DEFAULT; |
Taras Lemkin | 96a0d34 | 2018-03-26 14:52:58 +0000 | [diff] [blame] | 208 | |
Yi Tseng | aa417a6 | 2017-09-08 17:22:51 -0700 | [diff] [blame] | 209 | protected HostProviderService providerService; |
Yi Tseng | 525ff40 | 2017-10-23 19:39:39 -0700 | [diff] [blame] | 210 | protected ApplicationId appId; |
Charles Chan | 70fdd49 | 2018-03-07 17:36:06 -0800 | [diff] [blame] | 211 | protected Multimap<DeviceId, VlanId> ignoredVlans = Multimaps.synchronizedMultimap(HashMultimap.create()); |
Yi Tseng | 525ff40 | 2017-10-23 19:39:39 -0700 | [diff] [blame] | 212 | private InternalHostListener hostListener = new InternalHostListener(); |
Kalhee Kim | ba36606 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 213 | private Boolean dhcpFpmEnabled = false; |
Charles Chan | 909cff8 | 2018-03-05 13:14:02 -0800 | [diff] [blame] | 214 | private List<DhcpServerInfo> defaultServerInfoList = new CopyOnWriteArrayList<>(); |
| 215 | private List<DhcpServerInfo> indirectServerInfoList = new CopyOnWriteArrayList<>(); |
Jordan Halterman | 6328db7 | 2018-04-10 13:34:50 -0400 | [diff] [blame] | 216 | |
| 217 | private Executor hostEventExecutor = newSingleThreadExecutor( |
| 218 | groupedThreads("dhcp6-event-host", "%d", log)); |
| 219 | |
Kalhee Kim | 495c9b2 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 220 | private class IpAddressInfo { |
| 221 | Ip6Address ip6Address; |
| 222 | long prefTime; |
| 223 | } |
| 224 | private class PdPrefixInfo { |
| 225 | IpPrefix pdPrefix; |
| 226 | long prefTime; |
| 227 | } |
| 228 | protected int dhcp6PollInterval = 24 * 3600; // 24 hr period |
| 229 | |
Kalhee Kim | d94ceea | 2017-11-29 19:03:02 +0000 | [diff] [blame] | 230 | // max 1 thread |
| 231 | static Semaphore recordSemaphore = new Semaphore(1); |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 232 | |
| 233 | // CLIENT message types |
| 234 | public static final Set<Byte> MSG_TYPE_FROM_CLIENT = |
| 235 | ImmutableSet.of(DHCP6.MsgType.SOLICIT.value(), |
| 236 | DHCP6.MsgType.REQUEST.value(), |
| 237 | DHCP6.MsgType.REBIND.value(), |
| 238 | DHCP6.MsgType.RENEW.value(), |
| 239 | DHCP6.MsgType.RELEASE.value(), |
| 240 | DHCP6.MsgType.DECLINE.value(), |
| 241 | DHCP6.MsgType.CONFIRM.value(), |
Lior Assouline | a21c0ca | 2018-01-28 16:18:48 -0800 | [diff] [blame] | 242 | DHCP6.MsgType.RELAY_FORW.value(), |
| 243 | DHCP6.MsgType.LEASEQUERY.value()); |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 244 | // SERVER message types |
| 245 | public static final Set<Byte> MSG_TYPE_FROM_SERVER = |
Lior Assouline | a21c0ca | 2018-01-28 16:18:48 -0800 | [diff] [blame] | 246 | ImmutableSet.of(DHCP6.MsgType.RELAY_REPL.value(), |
| 247 | DHCP6.MsgType.LEASEQUERY_REPLY.value()); |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 248 | |
| 249 | @Activate |
Taras Lemkin | 96a0d34 | 2018-03-26 14:52:58 +0000 | [diff] [blame] | 250 | protected void activate(ComponentContext context) { |
| 251 | cfgService.registerProperties(getClass()); |
| 252 | modified(context); |
Yi Tseng | 525ff40 | 2017-10-23 19:39:39 -0700 | [diff] [blame] | 253 | appId = coreService.registerApplication(DHCP_V6_RELAY_APP); |
Yi Tseng | aa417a6 | 2017-09-08 17:22:51 -0700 | [diff] [blame] | 254 | providerService = providerRegistry.register(this); |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 255 | hostService.addListener(hostListener); |
Yi Tseng | 5130129 | 2017-07-28 13:02:59 -0700 | [diff] [blame] | 256 | } |
| 257 | |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 258 | @Deactivate |
| 259 | protected void deactivate() { |
Taras Lemkin | 96a0d34 | 2018-03-26 14:52:58 +0000 | [diff] [blame] | 260 | cfgService.unregisterProperties(getClass(), false); |
Yi Tseng | aa417a6 | 2017-09-08 17:22:51 -0700 | [diff] [blame] | 261 | providerRegistry.unregister(this); |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 262 | hostService.removeListener(hostListener); |
Yi Tseng | 919b2df | 2017-09-07 16:22:51 -0700 | [diff] [blame] | 263 | defaultServerInfoList.forEach(this::stopMonitoringIps); |
Charles Chan | fc1c22e | 2018-07-19 09:52:01 -0700 | [diff] [blame] | 264 | defaultServerInfoList.forEach(info -> info.getDhcpServerIp6().ifPresent(this::cancelDhcpPacket)); |
Yi Tseng | 919b2df | 2017-09-07 16:22:51 -0700 | [diff] [blame] | 265 | defaultServerInfoList.clear(); |
| 266 | indirectServerInfoList.forEach(this::stopMonitoringIps); |
Charles Chan | fc1c22e | 2018-07-19 09:52:01 -0700 | [diff] [blame] | 267 | indirectServerInfoList.forEach(info -> info.getDhcpServerIp6().ifPresent(this::cancelDhcpPacket)); |
Yi Tseng | 919b2df | 2017-09-07 16:22:51 -0700 | [diff] [blame] | 268 | indirectServerInfoList.clear(); |
| 269 | } |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 270 | |
Taras Lemkin | 96a0d34 | 2018-03-26 14:52:58 +0000 | [diff] [blame] | 271 | @Modified |
| 272 | protected void modified(ComponentContext context) { |
| 273 | Dictionary<?, ?> properties = context.getProperties(); |
| 274 | Boolean flag; |
Ray Milkey | 687c00c | 2018-10-31 10:18:41 -0700 | [diff] [blame] | 275 | flag = Tools.isPropertyEnabled(properties, LEARN_ROUTE_FROM_LEASE_QUERY); |
Taras Lemkin | 96a0d34 | 2018-03-26 14:52:58 +0000 | [diff] [blame] | 276 | if (flag != null) { |
| 277 | learnRouteFromLeasequery = flag; |
| 278 | log.info("Learning routes from DHCP leasequery is {}", |
| 279 | learnRouteFromLeasequery ? "enabled" : "disabled"); |
| 280 | } |
| 281 | } |
| 282 | |
Yi Tseng | 919b2df | 2017-09-07 16:22:51 -0700 | [diff] [blame] | 283 | private void stopMonitoringIps(DhcpServerInfo serverInfo) { |
| 284 | serverInfo.getDhcpGatewayIp6().ifPresent(gatewayIp -> { |
| 285 | hostService.stopMonitoringIp(gatewayIp); |
| 286 | }); |
| 287 | serverInfo.getDhcpServerIp6().ifPresent(serverIp -> { |
| 288 | hostService.stopMonitoringIp(serverIp); |
| 289 | }); |
Yi Tseng | 5130129 | 2017-07-28 13:02:59 -0700 | [diff] [blame] | 290 | } |
| 291 | |
Yi Tseng | 5130129 | 2017-07-28 13:02:59 -0700 | [diff] [blame] | 292 | @Override |
Yi Tseng | 919b2df | 2017-09-07 16:22:51 -0700 | [diff] [blame] | 293 | public List<DhcpServerInfo> getDefaultDhcpServerInfoList() { |
| 294 | return defaultServerInfoList; |
Yi Tseng | 5130129 | 2017-07-28 13:02:59 -0700 | [diff] [blame] | 295 | } |
| 296 | |
| 297 | @Override |
Yi Tseng | 919b2df | 2017-09-07 16:22:51 -0700 | [diff] [blame] | 298 | public List<DhcpServerInfo> getIndirectDhcpServerInfoList() { |
| 299 | return indirectServerInfoList; |
Yi Tseng | 5130129 | 2017-07-28 13:02:59 -0700 | [diff] [blame] | 300 | } |
| 301 | |
| 302 | @Override |
Yi Tseng | 525ff40 | 2017-10-23 19:39:39 -0700 | [diff] [blame] | 303 | public void updateIgnoreVlanConfig(IgnoreDhcpConfig config) { |
| 304 | if (config == null) { |
| 305 | ignoredVlans.forEach(((deviceId, vlanId) -> { |
| 306 | processIgnoreVlanRule(deviceId, vlanId, REMOVE); |
| 307 | })); |
| 308 | return; |
| 309 | } |
| 310 | config.ignoredVlans().forEach((deviceId, vlanId) -> { |
| 311 | if (ignoredVlans.get(deviceId).contains(vlanId)) { |
| 312 | // don't need to process if it already ignored |
| 313 | return; |
| 314 | } |
| 315 | processIgnoreVlanRule(deviceId, vlanId, ADD); |
| 316 | }); |
Yi Tseng | 525ff40 | 2017-10-23 19:39:39 -0700 | [diff] [blame] | 317 | ignoredVlans.forEach((deviceId, vlanId) -> { |
| 318 | if (!config.ignoredVlans().get(deviceId).contains(vlanId)) { |
| 319 | // not contains in new config, remove it |
| 320 | processIgnoreVlanRule(deviceId, vlanId, REMOVE); |
| 321 | } |
| 322 | }); |
| 323 | } |
| 324 | |
| 325 | @Override |
Saurav Das | b805f1a | 2017-12-13 16:19:35 -0800 | [diff] [blame] | 326 | public void removeIgnoreVlanState(IgnoreDhcpConfig config) { |
| 327 | if (config == null) { |
| 328 | ignoredVlans.clear(); |
| 329 | return; |
| 330 | } |
| 331 | config.ignoredVlans().forEach((deviceId, vlanId) -> { |
| 332 | ignoredVlans.remove(deviceId, vlanId); |
| 333 | }); |
| 334 | } |
| 335 | |
Lior Assouline | a21c0ca | 2018-01-28 16:18:48 -0800 | [diff] [blame] | 336 | public DhcpRecord getDhcpRelayRecordFor(Ip6Address clientAddress) { |
| 337 | |
Taras Lemkin | 96a0d34 | 2018-03-26 14:52:58 +0000 | [diff] [blame] | 338 | Collection<DhcpRecord> records = dhcpRelayStore.getDhcpRecords(); |
Lior Assouline | a21c0ca | 2018-01-28 16:18:48 -0800 | [diff] [blame] | 339 | DhcpRecord dr = null; |
| 340 | for (DhcpRecord e:records) { |
| 341 | if (e.ip6Address().isPresent()) { |
| 342 | if (e.ip6Address().get().equals(clientAddress)) { |
| 343 | dr = e; |
| 344 | break; |
| 345 | } |
| 346 | } |
| 347 | } |
| 348 | return dr; |
| 349 | } |
| 350 | |
| 351 | public MacAddress findNextHopMacForIp6FromRelayStore(Ip6Address clientAddress, |
| 352 | MacAddress clientMacAddress, VlanId vlanID) { |
| 353 | |
| 354 | DhcpRecord dr = getDhcpRelayRecordFor(clientAddress); |
| 355 | |
| 356 | if (dr != null) { |
| 357 | Optional<MacAddress> nextHopTempMac = dr.nextHopTemp(); |
| 358 | if (nextHopTempMac.isPresent()) { |
| 359 | log.info("findNextHopForIp6FromRelayStore " + clientAddress + " got mac " + nextHopTempMac.toString()); |
| 360 | return nextHopTempMac.get(); |
| 361 | } |
| 362 | } else { |
| 363 | log.warn("findNextHopMacForIp6FromRelayStore could NOT find next hop for " + clientAddress); |
| 364 | return null; |
| 365 | } |
| 366 | return null; |
| 367 | } |
| 368 | |
| 369 | public Ip6Address findNextHopIp6FromRelayStore(Ip6Address clientAddress) { |
| 370 | |
| 371 | DhcpRecord dr = getDhcpRelayRecordFor(clientAddress); |
| 372 | if (dr != null) { |
| 373 | Optional<MacAddress> nextHopMac = dr.nextHop(); |
| 374 | if (nextHopMac.isPresent()) { |
| 375 | // find the local ip6 from the host store |
| 376 | HostId gwHostId = HostId.hostId(nextHopMac.get(), dr.vlanId()); |
| 377 | Host gwHost = hostService.getHost(gwHostId); |
| 378 | if (gwHost == null) { |
| 379 | log.warn("Can't find next hop host ID {}", gwHostId); |
| 380 | return null; |
| 381 | } |
| 382 | Ip6Address nextHopIp = gwHost.ipAddresses() |
| 383 | .stream() |
| 384 | .filter(IpAddress::isIp6) |
| 385 | .filter(IpAddress::isLinkLocal) |
| 386 | .map(IpAddress::getIp6Address) |
| 387 | .findFirst() |
| 388 | .orElse(null); |
| 389 | |
| 390 | log.info("findNextHopIp6FromRelayStore " + clientAddress + " got mac " + |
| 391 | nextHopMac.toString() + " ip6 " + nextHopIp); |
| 392 | return nextHopIp; |
| 393 | } |
| 394 | } else { |
| 395 | log.warn("findNextHopIp6FromRelayStore could NOT find next hop for " + clientAddress); |
| 396 | return null; |
| 397 | } |
| 398 | return null; |
| 399 | } |
| 400 | |
| 401 | private void setPotentialNextHopForIp6InRelayStore(Ip6Address clientAddress, |
| 402 | VlanId vlanId, MacAddress nh) { |
| 403 | DhcpRecord dr = getDhcpRelayRecordFor(clientAddress); |
| 404 | if (dr != null) { |
| 405 | dr.nextHopTemp(nh); |
| 406 | log.debug("LQ6 potential NH mac " + nh.toString() + " UPDATED in RelayRecord client " + clientAddress); |
| 407 | } else { |
| 408 | log.warn("LQ6 potential NH mac" + nh.toString() + |
| 409 | " NOT FOUND in RelayRecord for client - LQ rejected" + clientAddress); |
| 410 | } |
| 411 | } |
| 412 | |
| 413 | public void handleLeaseQuery6ReplyMsg(PacketContext context, DHCP6 dhcp6Payload) { |
| 414 | ConnectPoint inPort = context.inPacket().receivedFrom(); |
| 415 | log.info("Got LQV6-REPLY on port {}", inPort); |
| 416 | List<Dhcp6Option> lopt = dhcp6Payload.getOptions(); |
| 417 | log.info("Options list: {}", lopt); |
| 418 | // find out if this lease is known is |
| 419 | Dhcp6ClientDataOption clientDataOption = dhcp6Payload.getOptions() |
| 420 | .stream() |
| 421 | .filter(opt -> opt instanceof Dhcp6ClientDataOption) |
| 422 | .map(pld -> (Dhcp6ClientDataOption) pld) |
| 423 | .findFirst() |
| 424 | .orElse(null); |
| 425 | |
| 426 | if (clientDataOption == null) { |
| 427 | log.warn("clientDataOption option is not present, " + |
| 428 | "lease is UNKNOWN - not adding any new route..."); |
| 429 | } else { |
| 430 | Dhcp6IaAddressOption aiAddressOption = clientDataOption.getOptions() |
| 431 | .stream() |
| 432 | .filter(opt -> opt instanceof Dhcp6IaAddressOption) |
| 433 | .map(pld -> (Dhcp6IaAddressOption) pld) |
| 434 | .findFirst() |
| 435 | .orElse(null); |
| 436 | |
| 437 | Dhcp6ClientIdOption clientIdOption = clientDataOption.getOptions() |
| 438 | .stream() |
| 439 | .filter(opt -> opt instanceof Dhcp6ClientIdOption) |
| 440 | .map(pld -> (Dhcp6ClientIdOption) pld) |
| 441 | .findFirst() |
| 442 | .orElse(null); |
| 443 | |
| 444 | if (aiAddressOption == null) { |
| 445 | log.warn("clientDataOption from DHCP server does not " + |
| 446 | "contains Dhcp6IaAddressOption for the client - giving up..."); |
| 447 | } else { |
| 448 | Ip6Address clientAddress = aiAddressOption.getIp6Address(); |
| 449 | MacAddress clientMacAddress = MacAddress.valueOf(clientIdOption.getDuid().getLinkLayerAddress()); |
| 450 | Ethernet packet = context.inPacket().parsed(); |
| 451 | VlanId vlanId = VlanId.vlanId(packet.getVlanID()); |
| 452 | MacAddress potentialNextHopMac = |
| 453 | findNextHopMacForIp6FromRelayStore(clientAddress, clientMacAddress, vlanId); |
| 454 | |
| 455 | if (potentialNextHopMac == null) { |
| 456 | log.warn("Can't find next hop host mac for client {} mac:{}/{}", |
| 457 | clientAddress, clientMacAddress, vlanId); |
| 458 | return; |
| 459 | } else { |
| 460 | log.info("Next hop mac for {}/{}/{} is {}", clientAddress, |
| 461 | clientMacAddress, vlanId, potentialNextHopMac.toString()); |
| 462 | } |
| 463 | // search the next hop in the hosts store |
| 464 | HostId gwHostId = HostId.hostId(potentialNextHopMac, vlanId); |
| 465 | Host gwHost = hostService.getHost(gwHostId); |
| 466 | if (gwHost == null) { |
| 467 | log.warn("Can't find next hop host ID {}", gwHostId); |
| 468 | return; |
| 469 | } |
| 470 | Ip6Address nextHopIp = gwHost.ipAddresses() |
| 471 | .stream() |
| 472 | .filter(IpAddress::isIp6) |
| 473 | .filter(IpAddress::isLinkLocal) |
| 474 | .map(IpAddress::getIp6Address) |
| 475 | .findFirst() |
| 476 | .orElse(null); |
| 477 | if (nextHopIp == null) { |
| 478 | log.warn("Can't find IP6 address of next hop {}", gwHost); |
| 479 | return; |
| 480 | } |
| 481 | log.info("client " + clientAddress + " is known !"); |
Kalhee Kim | 54a97c9 | 2018-04-02 21:23:46 +0000 | [diff] [blame] | 482 | Route routeForIP6 = new Route(Route.Source.DHCP, clientAddress.toIpPrefix(), nextHopIp); |
Lior Assouline | a21c0ca | 2018-01-28 16:18:48 -0800 | [diff] [blame] | 483 | log.debug("updating route of Client for indirectly connected."); |
| 484 | log.debug("client ip: " + clientAddress + ", next hop ip6: " + nextHopIp); |
| 485 | routeStore.updateRoute(routeForIP6); |
| 486 | } |
| 487 | } |
| 488 | } |
| 489 | |
Saurav Das | b805f1a | 2017-12-13 16:19:35 -0800 | [diff] [blame] | 490 | @Override |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 491 | public void processDhcpPacket(PacketContext context, BasePacket payload) { |
| 492 | checkNotNull(payload, "DHCP6 payload can't be null"); |
| 493 | checkState(payload instanceof DHCP6, "Payload is not a DHCP6"); |
| 494 | DHCP6 dhcp6Payload = (DHCP6) payload; |
| 495 | Ethernet receivedPacket = context.inPacket().parsed(); |
| 496 | |
| 497 | if (!configured()) { |
Lior Assouline | a21c0ca | 2018-01-28 16:18:48 -0800 | [diff] [blame] | 498 | log.warn("Missing DHCP6 relay server config. " + |
| 499 | "Abort packet processing dhcp6 payload {}", dhcp6Payload); |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 500 | return; |
| 501 | } |
Kalhee Kim | 495c9b2 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 502 | byte msgTypeVal = dhcp6Payload.getMsgType(); |
| 503 | MsgType msgType = DHCP6.MsgType.getType(msgTypeVal); |
| 504 | log.debug("msgType is {}", msgType); |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 505 | |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 506 | ConnectPoint inPort = context.inPacket().receivedFrom(); |
Jonathan Hart | 8ca2bc0 | 2017-11-30 18:23:42 -0800 | [diff] [blame] | 507 | |
Kalhee Kim | 495c9b2 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 508 | if (inPort == null) { |
Taras Lemkin | 96a0d34 | 2018-03-26 14:52:58 +0000 | [diff] [blame] | 509 | log.warn("incoming ConnectPoint is null"); |
Kalhee Kim | 495c9b2 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 510 | } |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 511 | Set<Interface> receivingInterfaces = interfaceService.getInterfacesByPort(inPort); |
| 512 | //ignore the packets if dhcp client interface is not configured on onos. |
| 513 | if (receivingInterfaces.isEmpty()) { |
| 514 | log.warn("Virtual interface is not configured on {}", inPort); |
| 515 | return; |
| 516 | } |
| 517 | |
Lior Assouline | a21c0ca | 2018-01-28 16:18:48 -0800 | [diff] [blame] | 518 | if (msgTypeVal == DHCP6.MsgType.LEASEQUERY.value()) { |
Taras Lemkin | 96a0d34 | 2018-03-26 14:52:58 +0000 | [diff] [blame] | 519 | List<InternalPacket> ethernetClientPackets = |
| 520 | learnRouteFromLeasequery ? |
| 521 | processLQ6PacketFromClient(context, receivedPacket, receivingInterfaces, dhcp6Payload) : |
| 522 | processDhcp6ForwardOnly(context, receivedPacket, receivingInterfaces, dhcp6Payload); |
| 523 | for (InternalPacket internalPacket : ethernetClientPackets) { |
Kalhee Kim | 495c9b2 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 524 | forwardPacket(internalPacket); |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 525 | } |
Taras Lemkin | 96a0d34 | 2018-03-26 14:52:58 +0000 | [diff] [blame] | 526 | } else if (msgTypeVal == DHCP6.MsgType.LEASEQUERY_REPLY.value() && learnRouteFromLeasequery) { |
Lior Assouline | a21c0ca | 2018-01-28 16:18:48 -0800 | [diff] [blame] | 527 | IPv6 clientIpv6 = (IPv6) receivedPacket.getPayload(); |
| 528 | UDP clientUdp = (UDP) clientIpv6.getPayload(); |
| 529 | DHCP6 clientDhcp6 = (DHCP6) clientUdp.getPayload(); |
Taras Lemkin | 96a0d34 | 2018-03-26 14:52:58 +0000 | [diff] [blame] | 530 | Interface serverInterface = Dhcp6HandlerUtil.directlyConnected(clientDhcp6) ? |
Lior Assouline | a21c0ca | 2018-01-28 16:18:48 -0800 | [diff] [blame] | 531 | getServerInterface() : getIndirectServerInterface(); |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 532 | InternalPacket ethernetPacketReply = |
Taras Lemkin | 96a0d34 | 2018-03-26 14:52:58 +0000 | [diff] [blame] | 533 | Dhcp6HandlerUtil.processLQ6PacketFromServer( |
Lior Assouline | a21c0ca | 2018-01-28 16:18:48 -0800 | [diff] [blame] | 534 | defaultServerInfoList, indirectServerInfoList, |
| 535 | serverInterface, interfaceService, |
| 536 | hostService, |
| 537 | context, receivedPacket, receivingInterfaces); |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 538 | if (ethernetPacketReply != null) { |
| 539 | forwardPacket(ethernetPacketReply); |
| 540 | } |
Lior Assouline | a21c0ca | 2018-01-28 16:18:48 -0800 | [diff] [blame] | 541 | handleLeaseQuery6ReplyMsg(context, dhcp6Payload); |
Taras Lemkin | 96a0d34 | 2018-03-26 14:52:58 +0000 | [diff] [blame] | 542 | } else if (MSG_TYPE_FROM_CLIENT.contains(msgTypeVal)) { |
| 543 | List<InternalPacket> ethernetClientPacket = |
| 544 | processDhcp6PacketFromClient(context, receivedPacket, receivingInterfaces); |
| 545 | for (InternalPacket internalPacket : ethernetClientPacket) { |
| 546 | forwardPacket(internalPacket); |
Lior Assouline | a21c0ca | 2018-01-28 16:18:48 -0800 | [diff] [blame] | 547 | } |
Taras Lemkin | 96a0d34 | 2018-03-26 14:52:58 +0000 | [diff] [blame] | 548 | } else if (MSG_TYPE_FROM_SERVER.contains(msgTypeVal)) { |
| 549 | log.debug("calling processDhcp6PacketFromServer with RELAY_REPL {}, {}", receivedPacket, dhcp6Payload); |
| 550 | InternalPacket ethernetPacketReply = |
| 551 | processDhcp6PacketFromServer(context, receivedPacket, receivingInterfaces); |
| 552 | if (ethernetPacketReply != null) { |
| 553 | forwardPacket(ethernetPacketReply); |
| 554 | } |
| 555 | } else { |
| 556 | log.warn("Not so fast, packet type {} not supported yet", msgTypeVal); |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 557 | } |
| 558 | } |
| 559 | |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 560 | /** |
| 561 | * Checks if this app has been configured. |
| 562 | * |
| 563 | * @return true if all information we need have been initialized |
| 564 | */ |
| 565 | public boolean configured() { |
Yi Tseng | 919b2df | 2017-09-07 16:22:51 -0700 | [diff] [blame] | 566 | return !defaultServerInfoList.isEmpty(); |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 567 | } |
| 568 | |
Yi Tseng | aa417a6 | 2017-09-08 17:22:51 -0700 | [diff] [blame] | 569 | @Override |
| 570 | public ProviderId id() { |
Charles Chan | d988c28 | 2017-09-12 17:09:32 -0700 | [diff] [blame] | 571 | return PROVIDER_ID; |
Yi Tseng | aa417a6 | 2017-09-08 17:22:51 -0700 | [diff] [blame] | 572 | } |
| 573 | |
| 574 | @Override |
| 575 | public void triggerProbe(Host host) { |
| 576 | // Do nothing here |
| 577 | } |
| 578 | |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 579 | //forward the packet to ConnectPoint where the DHCP server is attached. |
| 580 | private void forwardPacket(InternalPacket packet) { |
| 581 | //send Packetout to dhcp server connectpoint. |
Taras Lemkin | 96a0d34 | 2018-03-26 14:52:58 +0000 | [diff] [blame] | 582 | if (packet.getDestLocation() != null) { |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 583 | TrafficTreatment t = DefaultTrafficTreatment.builder() |
Taras Lemkin | 96a0d34 | 2018-03-26 14:52:58 +0000 | [diff] [blame] | 584 | .setOutput(packet.getDestLocation().port()).build(); |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 585 | OutboundPacket o = new DefaultOutboundPacket( |
Taras Lemkin | 96a0d34 | 2018-03-26 14:52:58 +0000 | [diff] [blame] | 586 | packet.getDestLocation().deviceId(), t, ByteBuffer.wrap(packet.getPacket().serialize())); |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 587 | packetService.emit(o); |
Taras Lemkin | 96a0d34 | 2018-03-26 14:52:58 +0000 | [diff] [blame] | 588 | if (log.isTraceEnabled()) { |
| 589 | IPv6 ip6 = (IPv6) packet.getPacket().getPayload(); |
| 590 | UDP udp = (UDP) ip6.getPayload(); |
| 591 | DHCP6 dhcp6 = (DHCP6) udp.getPayload(); |
| 592 | log.trace("Relaying packet to destination {} eth: {} dhcp: {}", |
| 593 | packet.getDestLocation(), packet.getPacket(), dhcp6); |
| 594 | } |
| 595 | |
| 596 | } |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 597 | } |
| 598 | |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 599 | /** |
| 600 | * extract from dhcp6 packet client ipv6 address of given by dhcp server. |
| 601 | * |
| 602 | * @param dhcp6 the dhcp6 packet |
Kalhee Kim | 495c9b2 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 603 | * @return IpAddressInfo IpAddressInfo given by dhcp server, or null if not exists |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 604 | */ |
Kalhee Kim | 495c9b2 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 605 | private IpAddressInfo extractIpAddress(DHCP6 dhcp6) { |
| 606 | IpAddressInfo ipInfo = new IpAddressInfo(); |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 607 | |
| 608 | log.debug("extractIpAddress enters dhcp6 {}.", dhcp6); |
| 609 | // Extract IPv6 address from IA NA ot IA TA option |
| 610 | Optional<Dhcp6IaNaOption> iaNaOption = dhcp6.getOptions() |
| 611 | .stream() |
| 612 | .filter(opt -> opt instanceof Dhcp6IaNaOption) |
| 613 | .map(opt -> (Dhcp6IaNaOption) opt) |
| 614 | .findFirst(); |
| 615 | Optional<Dhcp6IaTaOption> iaTaOption = dhcp6.getOptions() |
| 616 | .stream() |
| 617 | .filter(opt -> opt instanceof Dhcp6IaTaOption) |
| 618 | .map(opt -> (Dhcp6IaTaOption) opt) |
| 619 | .findFirst(); |
| 620 | Optional<Dhcp6IaAddressOption> iaAddressOption; |
| 621 | if (iaNaOption.isPresent()) { |
| 622 | log.debug("Found IPv6 address from iaNaOption {}", iaNaOption); |
| 623 | |
| 624 | iaAddressOption = iaNaOption.get().getOptions().stream() |
| 625 | .filter(opt -> opt instanceof Dhcp6IaAddressOption) |
| 626 | .map(opt -> (Dhcp6IaAddressOption) opt) |
| 627 | .findFirst(); |
| 628 | } else if (iaTaOption.isPresent()) { |
| 629 | log.debug("Found IPv6 address from iaTaOption {}", iaTaOption); |
| 630 | |
| 631 | iaAddressOption = iaTaOption.get().getOptions().stream() |
| 632 | .filter(opt -> opt instanceof Dhcp6IaAddressOption) |
| 633 | .map(opt -> (Dhcp6IaAddressOption) opt) |
| 634 | .findFirst(); |
| 635 | } else { |
Kalhee Kim | 495c9b2 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 636 | log.info("No IPv6 address found from iaTaOption {}", iaTaOption); |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 637 | iaAddressOption = Optional.empty(); |
| 638 | } |
| 639 | if (iaAddressOption.isPresent()) { |
Kalhee Kim | 495c9b2 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 640 | ipInfo.ip6Address = iaAddressOption.get().getIp6Address(); |
| 641 | ipInfo.prefTime = iaAddressOption.get().getPreferredLifetime(); |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 642 | log.debug("Found IPv6 address from iaAddressOption {}", iaAddressOption); |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 643 | } else { |
| 644 | log.debug("Can't find IPv6 address from DHCPv6 {}", dhcp6); |
Kalhee Kim | 495c9b2 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 645 | return null; |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 646 | } |
Kalhee Kim | 495c9b2 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 647 | return ipInfo; |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 648 | } |
Lior Assouline | a21c0ca | 2018-01-28 16:18:48 -0800 | [diff] [blame] | 649 | |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 650 | /** |
| 651 | * extract from dhcp6 packet Prefix prefix provided by dhcp server. |
| 652 | * |
| 653 | * @param dhcp6 the dhcp6 payload |
| 654 | * @return IpPrefix Prefix Delegation prefix, or null if not exists. |
| 655 | */ |
Kalhee Kim | 495c9b2 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 656 | private PdPrefixInfo extractPrefix(DHCP6 dhcp6) { |
| 657 | log.debug("extractPrefix enters {}", dhcp6); |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 658 | |
| 659 | // extract prefix |
Kalhee Kim | 495c9b2 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 660 | PdPrefixInfo pdPrefixInfo = new PdPrefixInfo(); |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 661 | |
| 662 | Ip6Address prefixAddress = null; |
| 663 | |
| 664 | // Extract IPv6 prefix from IA PD option |
| 665 | Optional<Dhcp6IaPdOption> iaPdOption = dhcp6.getOptions() |
| 666 | .stream() |
| 667 | .filter(opt -> opt instanceof Dhcp6IaPdOption) |
| 668 | .map(opt -> (Dhcp6IaPdOption) opt) |
| 669 | .findFirst(); |
| 670 | |
| 671 | Optional<Dhcp6IaPrefixOption> iaPrefixOption; |
| 672 | if (iaPdOption.isPresent()) { |
Kalhee Kim | 495c9b2 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 673 | log.debug("IA_PD option found {}", iaPdOption); |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 674 | |
| 675 | iaPrefixOption = iaPdOption.get().getOptions().stream() |
| 676 | .filter(opt -> opt instanceof Dhcp6IaPrefixOption) |
| 677 | .map(opt -> (Dhcp6IaPrefixOption) opt) |
| 678 | .findFirst(); |
| 679 | } else { |
Kalhee Kim | 495c9b2 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 680 | log.debug("IA_PD option NOT found"); |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 681 | |
| 682 | iaPrefixOption = Optional.empty(); |
| 683 | } |
| 684 | if (iaPrefixOption.isPresent()) { |
Kalhee Kim | 495c9b2 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 685 | log.debug("IAPrefix Option within IA_PD option found {}", iaPrefixOption); |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 686 | |
| 687 | prefixAddress = iaPrefixOption.get().getIp6Prefix(); |
| 688 | int prefixLen = (int) iaPrefixOption.get().getPrefixLength(); |
Kalhee Kim | 495c9b2 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 689 | log.debug("Prefix length is {} bits", prefixLen); |
| 690 | pdPrefixInfo.pdPrefix = IpPrefix.valueOf(prefixAddress, prefixLen); |
| 691 | pdPrefixInfo.prefTime = iaPrefixOption.get().getPreferredLifetime(); |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 692 | } else { |
Kalhee Kim | 495c9b2 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 693 | log.debug("Can't find IPv6 prefix from DHCPv6 {}", dhcp6); |
| 694 | return null; |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 695 | } |
Kalhee Kim | 495c9b2 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 696 | return pdPrefixInfo; |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 697 | } |
| 698 | |
| 699 | /** |
Kalhee Kim | 495c9b2 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 700 | * remove host or route and update dhcp relay record attributes. |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 701 | * |
| 702 | * @param directConnFlag flag to show that packet is from directly connected client |
Kalhee Kim | 495c9b2 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 703 | * @param location client side connect point |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 704 | * @param dhcp6Packet the dhcp6 payload |
| 705 | * @param clientPacket client's ethernet packet |
| 706 | * @param clientIpv6 client's Ipv6 packet |
Kalhee Kim | 0c0cb0b | 2017-09-15 17:43:27 +0000 | [diff] [blame] | 707 | * @param clientInterface client interfaces |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 708 | */ |
Kalhee Kim | ea4b6c2 | 2017-11-09 14:38:37 +0000 | [diff] [blame] | 709 | private void removeHostOrRoute(boolean directConnFlag, ConnectPoint location, |
| 710 | DHCP6 dhcp6Packet, |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 711 | Ethernet clientPacket, IPv6 clientIpv6, |
Kalhee Kim | 0c0cb0b | 2017-09-15 17:43:27 +0000 | [diff] [blame] | 712 | Interface clientInterface) { |
Kalhee Kim | 495c9b2 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 713 | log.debug("removeHostOrRoute enters {}", dhcp6Packet); |
Kalhee Kim | 121ba92 | 2017-11-01 17:56:44 +0000 | [diff] [blame] | 714 | VlanId vlanId = clientInterface.vlan(); |
Kalhee Kim | 495c9b2 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 715 | MacAddress srcMac = clientPacket.getSourceMAC(); // could be gw or host |
Kalhee Kim | ea4b6c2 | 2017-11-09 14:38:37 +0000 | [diff] [blame] | 716 | MacAddress leafClientMac; |
| 717 | byte leafMsgType; |
Kalhee Kim | 495c9b2 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 718 | log.debug("client mac {} client vlan {}", HexString.toHexString(srcMac.toBytes(), ":"), vlanId); |
Kalhee Kim | ea4b6c2 | 2017-11-09 14:38:37 +0000 | [diff] [blame] | 719 | |
Charles Chan | 24a96ff | 2018-08-13 10:32:03 -0700 | [diff] [blame] | 720 | Dhcp6ClientIdOption clientIdOption = Dhcp6HandlerUtil.extractClientId(directConnFlag, dhcp6Packet); |
Kalhee Kim | ea4b6c2 | 2017-11-09 14:38:37 +0000 | [diff] [blame] | 721 | if (clientIdOption != null) { |
Kalhee Kim | ea4b6c2 | 2017-11-09 14:38:37 +0000 | [diff] [blame] | 722 | if ((clientIdOption.getDuid().getDuidType() == Dhcp6Duid.DuidType.DUID_LLT) || |
| 723 | (clientIdOption.getDuid().getDuidType() == Dhcp6Duid.DuidType.DUID_LL)) { |
| 724 | leafClientMac = MacAddress.valueOf(clientIdOption.getDuid().getLinkLayerAddress()); |
| 725 | } else { |
| 726 | log.warn("Link-Layer Address not supported in CLIENTID option. No DhcpRelay Record created."); |
Charles Chan | b87495c | 2019-02-01 11:31:50 -0800 | [diff] [blame] | 727 | //dhcpRelayCountersStore.incrementCounter(gCount, DhcpRelayCounters.NO_LINKLOCAL_FAIL); |
Kalhee Kim | ea4b6c2 | 2017-11-09 14:38:37 +0000 | [diff] [blame] | 728 | return; |
| 729 | } |
Kalhee Kim | ea4b6c2 | 2017-11-09 14:38:37 +0000 | [diff] [blame] | 730 | } else { |
Kalhee Kim | 495c9b2 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 731 | log.warn("CLIENTID option NOT found. Don't create DhcpRelay Record."); |
Charles Chan | b87495c | 2019-02-01 11:31:50 -0800 | [diff] [blame] | 732 | //dhcpRelayCountersStore.incrementCounter(gCount, DhcpRelayCounters.NO_CLIENTID_FAIL); |
Kalhee Kim | ea4b6c2 | 2017-11-09 14:38:37 +0000 | [diff] [blame] | 733 | return; |
| 734 | } |
| 735 | |
Kalhee Kim | 495c9b2 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 736 | HostId leafHostId = HostId.hostId(leafClientMac, vlanId); |
| 737 | DhcpRecord record = dhcpRelayStore.getDhcpRecord(leafHostId).orElse(null); |
Kalhee Kim | ea4b6c2 | 2017-11-09 14:38:37 +0000 | [diff] [blame] | 738 | if (record == null) { |
Kalhee Kim | 495c9b2 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 739 | record = new DhcpRecord(leafHostId); |
Kalhee Kim | d94ceea | 2017-11-29 19:03:02 +0000 | [diff] [blame] | 740 | } else { |
| 741 | record = record.clone(); |
Kalhee Kim | ea4b6c2 | 2017-11-09 14:38:37 +0000 | [diff] [blame] | 742 | } |
Kalhee Kim | ea4b6c2 | 2017-11-09 14:38:37 +0000 | [diff] [blame] | 743 | |
Taras Lemkin | 96a0d34 | 2018-03-26 14:52:58 +0000 | [diff] [blame] | 744 | Boolean isMsgRelease = Dhcp6HandlerUtil.isDhcp6Release(dhcp6Packet); |
Kalhee Kim | 495c9b2 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 745 | IpAddressInfo ipInfo; |
| 746 | PdPrefixInfo pdInfo = null; |
Kalhee Kim | ea4b6c2 | 2017-11-09 14:38:37 +0000 | [diff] [blame] | 747 | if (directConnFlag) { |
Kalhee Kim | 495c9b2 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 748 | // Add to host store if it is connected to network directly |
| 749 | ipInfo = extractIpAddress(dhcp6Packet); |
| 750 | if (ipInfo != null) { |
| 751 | if (isMsgRelease) { |
| 752 | HostId hostId = HostId.hostId(srcMac, vlanId); |
| 753 | log.debug("remove Host {} ip for directly connected.", hostId.toString()); |
| 754 | providerService.removeIpFromHost(hostId, ipInfo.ip6Address); |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 755 | } |
| 756 | } else { |
Kalhee Kim | 495c9b2 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 757 | log.debug("ipAddress not found. Do not remove Host {} for directly connected.", |
| 758 | HostId.hostId(srcMac, vlanId).toString()); |
Kalhee Kim | ea4b6c2 | 2017-11-09 14:38:37 +0000 | [diff] [blame] | 759 | } |
Kalhee Kim | 495c9b2 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 760 | leafMsgType = dhcp6Packet.getMsgType(); |
Kalhee Kim | ea4b6c2 | 2017-11-09 14:38:37 +0000 | [diff] [blame] | 761 | } else { |
Kalhee Kim | 495c9b2 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 762 | // Remove from route store if it is not connected to network directly |
Kalhee Kim | ea4b6c2 | 2017-11-09 14:38:37 +0000 | [diff] [blame] | 763 | // pick out the first link-local ip address |
Kalhee Kim | 495c9b2 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 764 | IpAddress nextHopIp = getFirstIpByHost(directConnFlag, srcMac, vlanId); |
Kalhee Kim | ea4b6c2 | 2017-11-09 14:38:37 +0000 | [diff] [blame] | 765 | if (nextHopIp == null) { |
Kalhee Kim | 495c9b2 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 766 | log.warn("Can't find link-local IP address of gateway mac {} vlanId {}", srcMac, vlanId); |
Charles Chan | b87495c | 2019-02-01 11:31:50 -0800 | [diff] [blame] | 767 | //dhcpRelayCountersStore.incrementCounter(gCount, DhcpRelayCounters.NO_LINKLOCAL_GW); |
Kalhee Kim | ea4b6c2 | 2017-11-09 14:38:37 +0000 | [diff] [blame] | 768 | return; |
| 769 | } |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 770 | |
Taras Lemkin | 96a0d34 | 2018-03-26 14:52:58 +0000 | [diff] [blame] | 771 | DHCP6 leafDhcp = Dhcp6HandlerUtil.getDhcp6Leaf(dhcp6Packet); |
Kalhee Kim | 495c9b2 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 772 | ipInfo = extractIpAddress(leafDhcp); |
| 773 | if (ipInfo == null) { |
| 774 | log.debug("ip is null"); |
Kalhee Kim | ea4b6c2 | 2017-11-09 14:38:37 +0000 | [diff] [blame] | 775 | } else { |
Kalhee Kim | 495c9b2 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 776 | if (isMsgRelease) { |
Kalhee Kim | 54a97c9 | 2018-04-02 21:23:46 +0000 | [diff] [blame] | 777 | Route routeForIP = new Route(Route.Source.DHCP, ipInfo.ip6Address.toIpPrefix(), nextHopIp); |
Kalhee Kim | 495c9b2 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 778 | log.debug("removing route of 128 address for indirectly connected."); |
| 779 | log.debug("128 ip {}, nexthop {}", |
| 780 | HexString.toHexString(ipInfo.ip6Address.toOctets(), ":"), |
| 781 | HexString.toHexString(nextHopIp.toOctets(), ":")); |
| 782 | routeStore.removeRoute(routeForIP); |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 783 | } |
Kalhee Kim | ea4b6c2 | 2017-11-09 14:38:37 +0000 | [diff] [blame] | 784 | } |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 785 | |
Kalhee Kim | 495c9b2 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 786 | pdInfo = extractPrefix(leafDhcp); |
| 787 | if (pdInfo == null) { |
| 788 | log.debug("ipPrefix is null "); |
Kalhee Kim | ea4b6c2 | 2017-11-09 14:38:37 +0000 | [diff] [blame] | 789 | } else { |
Kalhee Kim | 495c9b2 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 790 | if (isMsgRelease) { |
Kalhee Kim | 54a97c9 | 2018-04-02 21:23:46 +0000 | [diff] [blame] | 791 | Route routeForPrefix = new Route(Route.Source.DHCP, pdInfo.pdPrefix, nextHopIp); |
Kalhee Kim | 495c9b2 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 792 | log.debug("removing route of PD for indirectly connected."); |
| 793 | log.debug("pd ip {}, nexthop {}", |
| 794 | HexString.toHexString(pdInfo.pdPrefix.address().toOctets(), ":"), |
| 795 | HexString.toHexString(nextHopIp.toOctets(), ":")); |
| 796 | |
| 797 | routeStore.removeRoute(routeForPrefix); |
Kalhee Kim | ba36606 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 798 | if (this.dhcpFpmEnabled) { |
Kalhee Kim | 495c9b2 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 799 | dhcpFpmPrefixStore.removeFpmRecord(pdInfo.pdPrefix); |
Kalhee Kim | ba36606 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 800 | } |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 801 | } |
| 802 | } |
Kalhee Kim | ea4b6c2 | 2017-11-09 14:38:37 +0000 | [diff] [blame] | 803 | leafMsgType = leafDhcp.getMsgType(); |
Kalhee Kim | d94ceea | 2017-11-29 19:03:02 +0000 | [diff] [blame] | 804 | } |
Kalhee Kim | ea4b6c2 | 2017-11-09 14:38:37 +0000 | [diff] [blame] | 805 | |
Kalhee Kim | 495c9b2 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 806 | if (isMsgRelease) { |
| 807 | log.debug("DHCP6 RELEASE msg."); |
| 808 | if (record != null) { |
| 809 | if (ipInfo != null) { |
| 810 | log.debug("DhcpRelay Record ip6Address is set to null."); |
| 811 | record.ip6Address(null); |
| 812 | } |
| 813 | if (pdInfo != null) { |
| 814 | log.debug("DhcpRelay Record pdPrefix is set to null."); |
| 815 | } |
| 816 | |
| 817 | if (!record.ip6Address().isPresent() && !record.pdPrefix().isPresent()) { |
| 818 | log.warn("IP6 address and IP6 PD both are null. Remove record."); |
| 819 | // do not remove a record. Let timer task handler it. |
| 820 | //dhcpRelayStore.removeDhcpRecord(HostId.hostId(leafClientMac, vlanId)); |
| 821 | } |
| 822 | } |
| 823 | } |
| 824 | |
Ray Milkey | ffe1a33 | 2018-01-24 10:41:14 -0800 | [diff] [blame] | 825 | if (record != null) { |
Taras Lemkin | 96a0d34 | 2018-03-26 14:52:58 +0000 | [diff] [blame] | 826 | record.getV6Counters().incrementCounter(Dhcp6HandlerUtil.getMsgTypeStr(leafMsgType)); |
Ray Milkey | ffe1a33 | 2018-01-24 10:41:14 -0800 | [diff] [blame] | 827 | record.addLocation(new HostLocation(location, System.currentTimeMillis())); |
| 828 | record.ip6Status(DHCP6.MsgType.getType(leafMsgType)); |
| 829 | record.setDirectlyConnected(directConnFlag); |
| 830 | if (!directConnFlag) { |
| 831 | // Update gateway mac address if the host is not directly connected |
| 832 | record.nextHop(srcMac); |
| 833 | } |
| 834 | record.updateLastSeen(); |
Kalhee Kim | 495c9b2 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 835 | } |
Kalhee Kim | 495c9b2 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 836 | dhcpRelayStore.updateDhcpRecord(leafHostId, record); |
Charles Chan | b87495c | 2019-02-01 11:31:50 -0800 | [diff] [blame] | 837 | /* |
Kalhee Kim | d94ceea | 2017-11-29 19:03:02 +0000 | [diff] [blame] | 838 | // TODO Use AtomicInteger for the counters |
| 839 | try { |
| 840 | recordSemaphore.acquire(); |
| 841 | try { |
Taras Lemkin | 96a0d34 | 2018-03-26 14:52:58 +0000 | [diff] [blame] | 842 | dhcpRelayCountersStore.incrementCounter(gCount, Dhcp6HandlerUtil.getMsgTypeStr(leafMsgType)); |
Kalhee Kim | d94ceea | 2017-11-29 19:03:02 +0000 | [diff] [blame] | 843 | } finally { |
| 844 | // calling release() after a successful acquire() |
| 845 | recordSemaphore.release(); |
| 846 | } |
| 847 | } catch (InterruptedException e) { |
Ray Milkey | ba547f9 | 2018-02-01 15:22:31 -0800 | [diff] [blame] | 848 | Thread.currentThread().interrupt(); |
Kalhee Kim | d94ceea | 2017-11-29 19:03:02 +0000 | [diff] [blame] | 849 | } |
Charles Chan | b87495c | 2019-02-01 11:31:50 -0800 | [diff] [blame] | 850 | */ |
Kalhee Kim | 495c9b2 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 851 | } |
| 852 | |
| 853 | /** |
| 854 | * add host or route and update dhcp relay record. |
| 855 | * |
| 856 | * @param directConnFlag flag to show that packet is from directly connected client |
| 857 | * @param location client side connect point |
| 858 | * @param dhcp6Relay the dhcp6 payload |
| 859 | * @param embeddedDhcp6 the dhcp6 payload within relay |
| 860 | * @param srcMac client gw/host macAddress |
| 861 | * @param clientInterface client interface |
pier | 86231ef | 2019-08-23 10:45:40 -0700 | [diff] [blame] | 862 | * @param vlanIdInUse vlanid encoded in the interface id Option |
Kalhee Kim | 495c9b2 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 863 | */ |
| 864 | private void addHostOrRoute(boolean directConnFlag, ConnectPoint location, DHCP6 dhcp6Relay, |
pier | 86231ef | 2019-08-23 10:45:40 -0700 | [diff] [blame] | 865 | DHCP6 embeddedDhcp6, MacAddress srcMac, Interface clientInterface, |
| 866 | VlanId vlanIdInUse) { |
Kalhee Kim | 495c9b2 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 867 | log.debug("addHostOrRoute entered."); |
pier | 86231ef | 2019-08-23 10:45:40 -0700 | [diff] [blame] | 868 | VlanId vlanId; |
| 869 | if (clientInterface.vlanTagged().isEmpty()) { |
| 870 | vlanId = clientInterface.vlan(); |
| 871 | } else { |
| 872 | // might be multiple vlan in same interface |
| 873 | vlanId = vlanIdInUse; |
| 874 | } |
| 875 | if (vlanId == null) { |
| 876 | vlanId = VlanId.NONE; |
| 877 | } |
| 878 | |
Taras Lemkin | 96a0d34 | 2018-03-26 14:52:58 +0000 | [diff] [blame] | 879 | Boolean isMsgReply = Dhcp6HandlerUtil.isDhcp6Reply(dhcp6Relay); |
Kalhee Kim | 495c9b2 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 880 | MacAddress leafClientMac; |
| 881 | Byte leafMsgType; |
| 882 | |
Charles Chan | 24a96ff | 2018-08-13 10:32:03 -0700 | [diff] [blame] | 883 | Dhcp6ClientIdOption clientIdOption = Dhcp6HandlerUtil.extractClientId(directConnFlag, embeddedDhcp6); |
Kalhee Kim | ea4b6c2 | 2017-11-09 14:38:37 +0000 | [diff] [blame] | 884 | if (clientIdOption != null) { |
| 885 | log.debug("CLIENTID option found {}", clientIdOption); |
| 886 | if ((clientIdOption.getDuid().getDuidType() == Dhcp6Duid.DuidType.DUID_LLT) || |
| 887 | (clientIdOption.getDuid().getDuidType() == Dhcp6Duid.DuidType.DUID_LL)) { |
| 888 | leafClientMac = MacAddress.valueOf(clientIdOption.getDuid().getLinkLayerAddress()); |
| 889 | } else { |
| 890 | log.warn("Link-Layer Address not supported in CLIENTID option. No DhcpRelay Record created."); |
Charles Chan | b87495c | 2019-02-01 11:31:50 -0800 | [diff] [blame] | 891 | //dhcpRelayCountersStore.incrementCounter(gCount, DhcpRelayCounters.NO_LINKLOCAL_FAIL); |
Kalhee Kim | ea4b6c2 | 2017-11-09 14:38:37 +0000 | [diff] [blame] | 892 | return; |
| 893 | } |
Kalhee Kim | ea4b6c2 | 2017-11-09 14:38:37 +0000 | [diff] [blame] | 894 | } else { |
| 895 | log.warn("CLIENTID option NOT found. No DhcpRelay Record created."); |
Charles Chan | b87495c | 2019-02-01 11:31:50 -0800 | [diff] [blame] | 896 | //dhcpRelayCountersStore.incrementCounter(gCount, DhcpRelayCounters.NO_CLIENTID_FAIL); |
Kalhee Kim | ea4b6c2 | 2017-11-09 14:38:37 +0000 | [diff] [blame] | 897 | return; |
| 898 | } |
Kalhee Kim | 495c9b2 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 899 | HostId leafHostId = HostId.hostId(leafClientMac, vlanId); |
| 900 | DhcpRecord record = dhcpRelayStore.getDhcpRecord(leafHostId).orElse(null); |
Kalhee Kim | ea4b6c2 | 2017-11-09 14:38:37 +0000 | [diff] [blame] | 901 | if (record == null) { |
| 902 | record = new DhcpRecord(HostId.hostId(leafClientMac, vlanId)); |
Kalhee Kim | d94ceea | 2017-11-29 19:03:02 +0000 | [diff] [blame] | 903 | } else { |
| 904 | record = record.clone(); |
Kalhee Kim | ea4b6c2 | 2017-11-09 14:38:37 +0000 | [diff] [blame] | 905 | } |
Kalhee Kim | 495c9b2 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 906 | |
| 907 | IpAddressInfo ipInfo; |
| 908 | PdPrefixInfo pdInfo = null; |
| 909 | if (directConnFlag) { |
| 910 | // Add to host store if it connect to network directly |
| 911 | ipInfo = extractIpAddress(embeddedDhcp6); |
| 912 | if (ipInfo != null) { |
| 913 | if (isMsgReply) { |
| 914 | Set<IpAddress> ips = Sets.newHashSet(ipInfo.ip6Address); |
| 915 | HostId hostId = HostId.hostId(srcMac, vlanId); |
| 916 | Host host = hostService.getHost(hostId); |
| 917 | HostLocation hostLocation = new HostLocation(clientInterface.connectPoint(), |
| 918 | System.currentTimeMillis()); |
| 919 | Set<HostLocation> hostLocations = Sets.newHashSet(hostLocation); |
| 920 | if (host != null) { |
| 921 | // Dual homing support: |
| 922 | // if host exists, use old locations and new location |
| 923 | hostLocations.addAll(host.locations()); |
| 924 | } |
| 925 | HostDescription desc = new DefaultHostDescription(srcMac, vlanId, hostLocations, ips, |
| 926 | false); |
| 927 | log.debug("adding Host for directly connected."); |
| 928 | log.debug("client mac {} client vlan {} hostlocation {}", |
| 929 | HexString.toHexString(srcMac.toBytes(), ":"), vlanId, hostLocation.toString()); |
| 930 | // Replace the ip when dhcp server give the host new ip address |
| 931 | providerService.hostDetected(hostId, desc, false); |
| 932 | } |
| 933 | } else { |
| 934 | log.warn("ipAddress not found. Do not add Host {} for directly connected.", |
| 935 | HostId.hostId(srcMac, vlanId).toString()); |
| 936 | } |
| 937 | leafMsgType = embeddedDhcp6.getMsgType(); |
| 938 | } else { |
| 939 | // Add to route store if it does not connect to network directly |
| 940 | // pick out the first link-local ip address |
| 941 | IpAddress nextHopIp = getFirstIpByHost(directConnFlag, srcMac, vlanId); |
| 942 | if (nextHopIp == null) { |
| 943 | log.warn("Can't find link-local IP address of gateway mac {} vlanId {}", srcMac, vlanId); |
Charles Chan | b87495c | 2019-02-01 11:31:50 -0800 | [diff] [blame] | 944 | //dhcpRelayCountersStore.incrementCounter(gCount, DhcpRelayCounters.NO_LINKLOCAL_GW); |
Kalhee Kim | 495c9b2 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 945 | return; |
| 946 | } |
| 947 | |
Taras Lemkin | 96a0d34 | 2018-03-26 14:52:58 +0000 | [diff] [blame] | 948 | DHCP6 leafDhcp = Dhcp6HandlerUtil.getDhcp6Leaf(embeddedDhcp6); |
Kalhee Kim | 495c9b2 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 949 | ipInfo = extractIpAddress(leafDhcp); |
| 950 | if (ipInfo == null) { |
| 951 | log.debug("ip is null"); |
| 952 | } else { |
| 953 | if (isMsgReply) { |
Kalhee Kim | 54a97c9 | 2018-04-02 21:23:46 +0000 | [diff] [blame] | 954 | Route routeForIP = new Route(Route.Source.DHCP, ipInfo.ip6Address.toIpPrefix(), nextHopIp); |
Kalhee Kim | 495c9b2 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 955 | log.debug("adding Route of 128 address for indirectly connected."); |
Daniel Ginsburg | 83b7645 | 2018-06-09 01:43:59 +0300 | [diff] [blame] | 956 | routeStore.replaceRoute(routeForIP); |
Kalhee Kim | 495c9b2 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 957 | } |
| 958 | } |
| 959 | |
| 960 | pdInfo = extractPrefix(leafDhcp); |
| 961 | if (pdInfo == null) { |
| 962 | log.debug("ipPrefix is null "); |
| 963 | } else { |
| 964 | if (isMsgReply) { |
Kalhee Kim | 54a97c9 | 2018-04-02 21:23:46 +0000 | [diff] [blame] | 965 | Route routeForPrefix = new Route(Route.Source.DHCP, pdInfo.pdPrefix, nextHopIp); |
Kalhee Kim | 495c9b2 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 966 | log.debug("adding Route of PD for indirectly connected."); |
Daniel Ginsburg | 83b7645 | 2018-06-09 01:43:59 +0300 | [diff] [blame] | 967 | routeStore.replaceRoute(routeForPrefix); |
Kalhee Kim | 495c9b2 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 968 | if (this.dhcpFpmEnabled) { |
| 969 | FpmRecord fpmRecord = new FpmRecord(pdInfo.pdPrefix, nextHopIp, FpmRecord.Type.DHCP_RELAY); |
| 970 | dhcpFpmPrefixStore.addFpmRecord(pdInfo.pdPrefix, fpmRecord); |
| 971 | } |
| 972 | } |
| 973 | } |
| 974 | leafMsgType = leafDhcp.getMsgType(); |
| 975 | } |
| 976 | if (leafMsgType == DHCP6.MsgType.RELEASE.value() || |
| 977 | (leafMsgType == DHCP6.MsgType.REPLY.value()) && ipInfo == null) { |
| 978 | log.warn("DHCP6 RELEASE/REPLY(null ip) from Server. MsgType {}", leafMsgType); |
| 979 | //return; |
| 980 | } |
| 981 | |
Kalhee Kim | ea4b6c2 | 2017-11-09 14:38:37 +0000 | [diff] [blame] | 982 | record.addLocation(new HostLocation(location, System.currentTimeMillis())); |
Kalhee Kim | 2f07c60 | 2017-11-15 18:57:53 +0000 | [diff] [blame] | 983 | |
Kalhee Kim | ea4b6c2 | 2017-11-09 14:38:37 +0000 | [diff] [blame] | 984 | if (leafMsgType == DHCP6.MsgType.REPLY.value()) { |
Kalhee Kim | 495c9b2 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 985 | if (ipInfo != null) { |
Kalhee Kim | ea4b6c2 | 2017-11-09 14:38:37 +0000 | [diff] [blame] | 986 | log.debug("IP6 address is being stored into dhcp-relay store."); |
Lior Assouline | a21c0ca | 2018-01-28 16:18:48 -0800 | [diff] [blame] | 987 | log.debug("Client IP6 address {}", HexString.toHexString(ipInfo.ip6Address.toOctets(), ":")); |
Kalhee Kim | 495c9b2 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 988 | record.ip6Address(ipInfo.ip6Address); |
Kalhee Kim | 2f07c60 | 2017-11-15 18:57:53 +0000 | [diff] [blame] | 989 | record.updateAddrPrefTime(ipInfo.prefTime); |
| 990 | record.updateLastIp6Update(); |
Lior Assouline | a21c0ca | 2018-01-28 16:18:48 -0800 | [diff] [blame] | 991 | } else { |
Kalhee Kim | ea4b6c2 | 2017-11-09 14:38:37 +0000 | [diff] [blame] | 992 | log.debug("IP6 address is not returned from server. Maybe only PD is returned."); |
| 993 | } |
Kalhee Kim | 495c9b2 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 994 | if (pdInfo != null) { |
| 995 | log.debug("IP6 PD address {}", |
| 996 | HexString.toHexString(pdInfo.pdPrefix.address().toOctets(), ":")); |
Kalhee Kim | 2f07c60 | 2017-11-15 18:57:53 +0000 | [diff] [blame] | 997 | record.pdPrefix(pdInfo.pdPrefix); |
| 998 | record.updatePdPrefTime(pdInfo.prefTime); |
| 999 | record.updateLastPdUpdate(); |
Kalhee Kim | ea4b6c2 | 2017-11-09 14:38:37 +0000 | [diff] [blame] | 1000 | } else { |
| 1001 | log.debug("IP6 PD address is not returned from server. Maybe only IPAddress is returned."); |
| 1002 | } |
| 1003 | } |
Kalhee Kim | 2f07c60 | 2017-11-15 18:57:53 +0000 | [diff] [blame] | 1004 | |
Taras Lemkin | 96a0d34 | 2018-03-26 14:52:58 +0000 | [diff] [blame] | 1005 | record.getV6Counters().incrementCounter(Dhcp6HandlerUtil.getMsgTypeStr(leafMsgType)); |
Kalhee Kim | 495c9b2 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 1006 | record.ip6Status(DHCP6.MsgType.getType(leafMsgType)); |
Kalhee Kim | ea4b6c2 | 2017-11-09 14:38:37 +0000 | [diff] [blame] | 1007 | record.setDirectlyConnected(directConnFlag); |
| 1008 | record.updateLastSeen(); |
Kalhee Kim | 495c9b2 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 1009 | dhcpRelayStore.updateDhcpRecord(leafHostId, record); |
Charles Chan | b87495c | 2019-02-01 11:31:50 -0800 | [diff] [blame] | 1010 | /* |
Kalhee Kim | d94ceea | 2017-11-29 19:03:02 +0000 | [diff] [blame] | 1011 | // TODO Use AtomicInteger for the counters |
| 1012 | try { |
| 1013 | recordSemaphore.acquire(); |
| 1014 | try { |
Taras Lemkin | 96a0d34 | 2018-03-26 14:52:58 +0000 | [diff] [blame] | 1015 | dhcpRelayCountersStore.incrementCounter(gCount, Dhcp6HandlerUtil.getMsgTypeStr(leafMsgType)); |
Kalhee Kim | d94ceea | 2017-11-29 19:03:02 +0000 | [diff] [blame] | 1016 | } finally { |
| 1017 | // calling release() after a successful acquire() |
| 1018 | recordSemaphore.release(); |
| 1019 | } |
| 1020 | } catch (InterruptedException e) { |
Ray Milkey | ba547f9 | 2018-02-01 15:22:31 -0800 | [diff] [blame] | 1021 | Thread.currentThread().interrupt(); |
Kalhee Kim | d94ceea | 2017-11-29 19:03:02 +0000 | [diff] [blame] | 1022 | } |
Charles Chan | b87495c | 2019-02-01 11:31:50 -0800 | [diff] [blame] | 1023 | */ |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 1024 | } |
| 1025 | |
Taras Lemkin | 96a0d34 | 2018-03-26 14:52:58 +0000 | [diff] [blame] | 1026 | private List<InternalPacket> processDhcp6ForwardOnly(PacketContext context, |
| 1027 | Ethernet clientPacket, |
| 1028 | Set<Interface> clientInterfaces, |
| 1029 | DHCP6 dhcpPacket) { |
| 1030 | ConnectPoint inPort = context.inPacket().receivedFrom(); |
| 1031 | log.trace("Got DHCPv6 on port {}", inPort); |
| 1032 | boolean directConnFlag = Dhcp6HandlerUtil.directlyConnected(dhcpPacket); |
| 1033 | |
| 1034 | List<InternalPacket> internalPackets = new ArrayList<>(); |
| 1035 | List<DhcpServerInfo> serverInfoList = findValidServerInfo(directConnFlag); |
| 1036 | |
| 1037 | for (DhcpServerInfo dhcpServer : serverInfoList) { |
Charles Chan | 24a96ff | 2018-08-13 10:32:03 -0700 | [diff] [blame] | 1038 | Interface serverInterface = getServerInterface(dhcpServer); |
| 1039 | if (serverInterface == null) { |
| 1040 | log.warn("Can't get server interface, ignore"); |
| 1041 | continue; |
| 1042 | } |
| 1043 | |
Taras Lemkin | 96a0d34 | 2018-03-26 14:52:58 +0000 | [diff] [blame] | 1044 | Ethernet newPacket = Dhcp6HandlerUtil.buildDhcp6PacketFromClient(context, |
Charles Chan | 24a96ff | 2018-08-13 10:32:03 -0700 | [diff] [blame] | 1045 | clientPacket, clientInterfaces, dhcpServer, serverInterface); |
Taras Lemkin | 96a0d34 | 2018-03-26 14:52:58 +0000 | [diff] [blame] | 1046 | log.trace("Built packet for server {} : {}", dhcpServer, newPacket); |
| 1047 | internalPackets.add(InternalPacket.internalPacket(newPacket, |
| 1048 | dhcpServer.getDhcpServerConnectPoint().get())); |
| 1049 | } |
| 1050 | |
| 1051 | return internalPackets; |
| 1052 | } |
| 1053 | |
Lior Assouline | a21c0ca | 2018-01-28 16:18:48 -0800 | [diff] [blame] | 1054 | private List<InternalPacket> processLQ6PacketFromClient(PacketContext context, |
| 1055 | Ethernet clientPacket, |
| 1056 | Set<Interface> clientInterfaces, |
| 1057 | DHCP6 dhcp6Payload) { |
| 1058 | ConnectPoint inPort = context.inPacket().receivedFrom(); |
| 1059 | log.info("Got LQ-REQUEST V6 on port {}", inPort); |
| 1060 | List<Dhcp6Option> lopt = dhcp6Payload.getOptions(); |
| 1061 | log.info("Options list: {}", lopt); |
| 1062 | Dhcp6LeaseQueryOption lqoption = dhcp6Payload.getOptions() |
| 1063 | .stream() |
| 1064 | .filter(opt -> opt instanceof Dhcp6LeaseQueryOption) |
| 1065 | .map(pld -> (Dhcp6LeaseQueryOption) pld) |
| 1066 | .findFirst() |
| 1067 | .orElse(null); |
| 1068 | |
| 1069 | if (lqoption == null) { |
| 1070 | // Can't find dhcp payload |
| 1071 | log.warn("Can't find dhcp6 lease query message - aborting"); |
| 1072 | return null; |
| 1073 | } else { |
| 1074 | log.info("dhcp6 lqv6 options found: {}", lqoption); |
| 1075 | } |
| 1076 | log.warn("LQv6 for " + lqoption.linkAddress.toString() + " comes from " + inPort.toString()); |
| 1077 | Ethernet packet = context.inPacket().parsed(); |
| 1078 | Ip6Address clientAddress = lqoption.linkAddress; |
| 1079 | IPv6 ipv6Packet = (IPv6) packet.getPayload(); |
| 1080 | Ip6Address nextHopIp = findNextHopIp6FromRelayStore(clientAddress); |
| 1081 | |
| 1082 | // 1. only if there is a route to remove - remove it |
| 1083 | if (nextHopIp != null) { |
Kalhee Kim | 54a97c9 | 2018-04-02 21:23:46 +0000 | [diff] [blame] | 1084 | Route routeForIP6 = new Route(Route.Source.DHCP, clientAddress.toIpPrefix(), nextHopIp); |
Lior Assouline | a21c0ca | 2018-01-28 16:18:48 -0800 | [diff] [blame] | 1085 | log.debug("Removing route of Client " + clientAddress + |
| 1086 | " for indirectly connected - next hop ip6 " + nextHopIp); |
| 1087 | routeStore.removeRoute(routeForIP6); |
| 1088 | } |
| 1089 | |
| 1090 | // 2. note the potential NH this packet came from in case it's a known lease |
| 1091 | // this NH will then be used to build the route |
| 1092 | MacAddress potentialNH = packet.getSourceMAC(); |
| 1093 | VlanId vlanId = VlanId.vlanId(packet.getVlanID()); |
| 1094 | setPotentialNextHopForIp6InRelayStore(clientAddress, vlanId, potentialNH); |
Lior Assouline | a21c0ca | 2018-01-28 16:18:48 -0800 | [diff] [blame] | 1095 | // 3. route this LQ6 to all relevant servers |
| 1096 | IPv6 clientIpv6 = (IPv6) clientPacket.getPayload(); |
| 1097 | UDP clientUdp = (UDP) clientIpv6.getPayload(); |
| 1098 | DHCP6 clientDhcp6 = (DHCP6) clientUdp.getPayload(); |
| 1099 | |
Taras Lemkin | 96a0d34 | 2018-03-26 14:52:58 +0000 | [diff] [blame] | 1100 | boolean directConnFlag = Dhcp6HandlerUtil.directlyConnected(clientDhcp6); |
Harshada Chaundkar | f0780fe | 2019-05-06 20:16:13 +0000 | [diff] [blame] | 1101 | boolean serverFound = false; |
Lior Assouline | a21c0ca | 2018-01-28 16:18:48 -0800 | [diff] [blame] | 1102 | List<InternalPacket> internalPackets = new ArrayList<>(); |
| 1103 | List<DhcpServerInfo> serverInfoList = findValidServerInfo(directConnFlag); |
| 1104 | List<DhcpServerInfo> copyServerInfoList = new ArrayList<DhcpServerInfo>(serverInfoList); |
| 1105 | |
| 1106 | for (DhcpServerInfo serverInfo : copyServerInfoList) { |
Taras Lemkin | 96a0d34 | 2018-03-26 14:52:58 +0000 | [diff] [blame] | 1107 | if (!Dhcp6HandlerUtil.checkDhcpServerConnPt(directConnFlag, serverInfo)) { |
Lior Assouline | a21c0ca | 2018-01-28 16:18:48 -0800 | [diff] [blame] | 1108 | log.warn("Can't get server connect point, ignore"); |
| 1109 | continue; |
| 1110 | } |
| 1111 | DhcpServerInfo newServerInfo = getHostInfoForServerInfo(serverInfo, serverInfoList); |
| 1112 | if (newServerInfo == null) { |
Harshada Chaundkar | f0780fe | 2019-05-06 20:16:13 +0000 | [diff] [blame] | 1113 | log.debug("Can't get server interface with host info resolved, ignore serverInfo {} serverInfoList {}", |
| 1114 | serverInfo, serverInfoList); |
Lior Assouline | a21c0ca | 2018-01-28 16:18:48 -0800 | [diff] [blame] | 1115 | continue; |
| 1116 | } |
Lior Assouline | a21c0ca | 2018-01-28 16:18:48 -0800 | [diff] [blame] | 1117 | Interface serverInterface = getServerInterface(newServerInfo); |
| 1118 | if (serverInterface == null) { |
Harshada Chaundkar | f0780fe | 2019-05-06 20:16:13 +0000 | [diff] [blame] | 1119 | log.debug("Can't get server interface, ignore for serverInfo {}, serverInfoList {}", |
| 1120 | serverInfo, serverInfoList); |
Lior Assouline | a21c0ca | 2018-01-28 16:18:48 -0800 | [diff] [blame] | 1121 | continue; |
| 1122 | } |
Harshada Chaundkar | f0780fe | 2019-05-06 20:16:13 +0000 | [diff] [blame] | 1123 | |
| 1124 | serverFound = true; |
| 1125 | log.debug("Server Info Found {}", serverInfo.getDhcpConnectMac()); |
Lior Assouline | a21c0ca | 2018-01-28 16:18:48 -0800 | [diff] [blame] | 1126 | Ethernet etherRouted = (Ethernet) clientPacket.clone(); |
| 1127 | MacAddress macFacingServer = serverInterface.mac(); |
| 1128 | if (macFacingServer == null) { |
| 1129 | log.warn("No MAC address for server Interface {}", serverInterface); |
| 1130 | return null; |
| 1131 | } |
| 1132 | etherRouted.setSourceMACAddress(macFacingServer); |
| 1133 | etherRouted.setDestinationMACAddress(newServerInfo.getDhcpConnectMac().get()); |
| 1134 | InternalPacket internalPacket = |
Taras Lemkin | 96a0d34 | 2018-03-26 14:52:58 +0000 | [diff] [blame] | 1135 | InternalPacket.internalPacket(etherRouted, |
Lior Assouline | a21c0ca | 2018-01-28 16:18:48 -0800 | [diff] [blame] | 1136 | serverInfo.getDhcpServerConnectPoint().get()); |
| 1137 | internalPackets.add(internalPacket); |
| 1138 | log.debug("Sending LQ to DHCP server {}", newServerInfo.getDhcpServerIp6()); |
| 1139 | } |
Harshada Chaundkar | f0780fe | 2019-05-06 20:16:13 +0000 | [diff] [blame] | 1140 | if (!serverFound) { |
| 1141 | log.warn("ProcessDhcp6PacketFromClient No Server Found"); |
| 1142 | } |
Lior Assouline | a21c0ca | 2018-01-28 16:18:48 -0800 | [diff] [blame] | 1143 | log.debug("num of client packets to send is{}", internalPackets.size()); |
| 1144 | |
| 1145 | return internalPackets; |
| 1146 | } |
| 1147 | |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 1148 | /** |
Kalhee Kim | 495c9b2 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 1149 | * build the DHCP6 solicit/request packet with gatewayip. |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 1150 | * |
| 1151 | * @param context packet context |
| 1152 | * @param clientPacket client ethernet packet |
| 1153 | * @param clientInterfaces set of client side interfaces |
| 1154 | */ |
Kalhee Kim | 495c9b2 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 1155 | private List<InternalPacket> processDhcp6PacketFromClient(PacketContext context, |
| 1156 | Ethernet clientPacket, |
| 1157 | Set<Interface> clientInterfaces) { |
Yi Tseng | 3bd57ac | 2017-11-29 14:39:18 -0800 | [diff] [blame] | 1158 | ConnectPoint receivedFrom = context.inPacket().receivedFrom(); |
Taras Lemkin | 96a0d34 | 2018-03-26 14:52:58 +0000 | [diff] [blame] | 1159 | Ip6Address relayAgentIp = Dhcp6HandlerUtil.getRelayAgentIPv6Address(clientInterfaces); |
Yi Tseng | 3bd57ac | 2017-11-29 14:39:18 -0800 | [diff] [blame] | 1160 | MacAddress relayAgentMac = clientInterfaces.iterator().next().mac(); |
| 1161 | if (relayAgentIp == null || relayAgentMac == null) { |
| 1162 | log.warn("Missing DHCP relay agent interface Ipv6 addr config for " |
Kalhee Kim | 495c9b2 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 1163 | + "packet from client on port: {}. Aborting packet processing", |
| 1164 | clientInterfaces.iterator().next().connectPoint()); |
Charles Chan | b87495c | 2019-02-01 11:31:50 -0800 | [diff] [blame] | 1165 | //dhcpRelayCountersStore.incrementCounter(gCount, DhcpRelayCounters.NO_CLIENT_INTF_MAC); |
Charles Chan | 239492c | 2018-01-22 13:27:28 -0800 | [diff] [blame] | 1166 | return Lists.newArrayList(); |
Kalhee Kim | 121ba92 | 2017-11-01 17:56:44 +0000 | [diff] [blame] | 1167 | } |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 1168 | |
Kalhee Kim | 495c9b2 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 1169 | IPv6 clientIpv6 = (IPv6) clientPacket.getPayload(); |
| 1170 | UDP clientUdp = (UDP) clientIpv6.getPayload(); |
| 1171 | DHCP6 clientDhcp6 = (DHCP6) clientUdp.getPayload(); |
| 1172 | |
Taras Lemkin | 96a0d34 | 2018-03-26 14:52:58 +0000 | [diff] [blame] | 1173 | boolean directConnFlag = Dhcp6HandlerUtil.directlyConnected(clientDhcp6); |
Charles Chan | a990ce9 | 2017-10-30 10:22:50 -0700 | [diff] [blame] | 1174 | |
Kalhee Kim | 121ba92 | 2017-11-01 17:56:44 +0000 | [diff] [blame] | 1175 | ConnectPoint clientConnectionPoint = context.inPacket().receivedFrom(); |
| 1176 | VlanId vlanIdInUse = VlanId.vlanId(clientPacket.getVlanID()); |
| 1177 | Interface clientInterface = interfaceService.getInterfacesByPort(clientConnectionPoint) |
Taras Lemkin | 96a0d34 | 2018-03-26 14:52:58 +0000 | [diff] [blame] | 1178 | .stream().filter(iface -> Dhcp6HandlerUtil.interfaceContainsVlan(iface, vlanIdInUse)) |
Kalhee Kim | 495c9b2 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 1179 | .findFirst() |
| 1180 | .orElse(null); |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 1181 | |
Kalhee Kim | 495c9b2 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 1182 | List<InternalPacket> internalPackets = new ArrayList<>(); |
| 1183 | List<DhcpServerInfo> serverInfoList = findValidServerInfo(directConnFlag); |
| 1184 | List<DhcpServerInfo> copyServerInfoList = new ArrayList<DhcpServerInfo>(serverInfoList); |
Kalhee Kim | 45b2418 | 2017-10-18 18:30:23 +0000 | [diff] [blame] | 1185 | |
Kalhee Kim | 495c9b2 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 1186 | for (DhcpServerInfo serverInfo : copyServerInfoList) { |
Taras Lemkin | 96a0d34 | 2018-03-26 14:52:58 +0000 | [diff] [blame] | 1187 | if (!Dhcp6HandlerUtil.checkDhcpServerConnPt(directConnFlag, serverInfo)) { |
Kalhee Kim | 495c9b2 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 1188 | log.warn("Can't get server connect point, ignore"); |
| 1189 | continue; |
| 1190 | } |
| 1191 | DhcpServerInfo newServerInfo = getHostInfoForServerInfo(serverInfo, serverInfoList); |
| 1192 | if (newServerInfo == null) { |
| 1193 | log.warn("Can't get server interface with host info resolved, ignore"); |
| 1194 | continue; |
| 1195 | } |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 1196 | |
Kalhee Kim | 495c9b2 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 1197 | Interface serverInterface = getServerInterface(newServerInfo); |
| 1198 | if (serverInterface == null) { |
| 1199 | log.warn("Can't get server interface, ignore"); |
| 1200 | continue; |
| 1201 | } |
Kalhee Kim | ea4b6c2 | 2017-11-09 14:38:37 +0000 | [diff] [blame] | 1202 | |
Taras Lemkin | 96a0d34 | 2018-03-26 14:52:58 +0000 | [diff] [blame] | 1203 | Ethernet etherReply = Dhcp6HandlerUtil.buildDhcp6PacketFromClient(context, clientPacket, |
Kalhee Kim | 495c9b2 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 1204 | clientInterfaces, newServerInfo, serverInterface); |
| 1205 | removeHostOrRoute(directConnFlag, clientConnectionPoint, clientDhcp6, clientPacket, |
| 1206 | clientIpv6, clientInterface); |
Kalhee Kim | ea4b6c2 | 2017-11-09 14:38:37 +0000 | [diff] [blame] | 1207 | |
Taras Lemkin | 96a0d34 | 2018-03-26 14:52:58 +0000 | [diff] [blame] | 1208 | InternalPacket internalPacket = InternalPacket.internalPacket(etherReply, |
Kalhee Kim | 495c9b2 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 1209 | serverInfo.getDhcpServerConnectPoint().get()); |
| 1210 | internalPackets.add(internalPacket); |
| 1211 | } |
| 1212 | log.debug("num of client packets to send is{}", internalPackets.size()); |
Kalhee Kim | ea4b6c2 | 2017-11-09 14:38:37 +0000 | [diff] [blame] | 1213 | |
Kalhee Kim | 495c9b2 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 1214 | return internalPackets; |
| 1215 | } |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 1216 | |
| 1217 | /** |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 1218 | * process the DHCP6 relay-reply packet from dhcp server. |
| 1219 | * |
| 1220 | * @param context packet context |
| 1221 | * @param receivedPacket server ethernet packet |
| 1222 | * @param recevingInterfaces set of server side interfaces |
Kalhee Kim | 495c9b2 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 1223 | * @return internalPacket toward client |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 1224 | */ |
| 1225 | private InternalPacket processDhcp6PacketFromServer(PacketContext context, |
| 1226 | Ethernet receivedPacket, Set<Interface> recevingInterfaces) { |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 1227 | // get dhcp6 header. |
Ray Milkey | f0c4761 | 2017-09-28 11:29:38 -0700 | [diff] [blame] | 1228 | Ethernet etherReply = receivedPacket.duplicate(); |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 1229 | IPv6 ipv6Packet = (IPv6) etherReply.getPayload(); |
| 1230 | UDP udpPacket = (UDP) ipv6Packet.getPayload(); |
| 1231 | DHCP6 dhcp6Relay = (DHCP6) udpPacket.getPayload(); |
Taras Lemkin | 96a0d34 | 2018-03-26 14:52:58 +0000 | [diff] [blame] | 1232 | Boolean directConnFlag = Dhcp6HandlerUtil.directlyConnected(dhcp6Relay); |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 1233 | |
Kalhee Kim | 495c9b2 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 1234 | DHCP6 embeddedDhcp6 = dhcp6Relay.getOptions().stream() |
| 1235 | .filter(opt -> opt instanceof Dhcp6RelayOption) |
| 1236 | .map(BasePacket::getPayload) |
| 1237 | .map(pld -> (DHCP6) pld) |
| 1238 | .findFirst() |
| 1239 | .orElse(null); |
Kalhee Kim | d21029f | 2017-09-26 20:21:53 +0000 | [diff] [blame] | 1240 | ConnectPoint inPort = context.inPacket().receivedFrom(); |
Kalhee Kim | 495c9b2 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 1241 | DhcpServerInfo foundServerInfo = findServerInfoFromServer(directConnFlag, inPort); |
Kalhee Kim | d21029f | 2017-09-26 20:21:53 +0000 | [diff] [blame] | 1242 | |
Kalhee Kim | 495c9b2 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 1243 | if (foundServerInfo == null) { |
jayakumarthazhath | ed5f05d | 2018-09-25 15:56:51 -0400 | [diff] [blame] | 1244 | log.warn("Cannot find server info for {} server, inPort {}", |
| 1245 | directConnFlag ? "direct" : "indirect", inPort); |
Charles Chan | b87495c | 2019-02-01 11:31:50 -0800 | [diff] [blame] | 1246 | //dhcpRelayCountersStore.incrementCounter(gCount, DhcpRelayCounters.NO_SERVER_INFO); |
Kalhee Kim | d21029f | 2017-09-26 20:21:53 +0000 | [diff] [blame] | 1247 | return null; |
Kalhee Kim | 495c9b2 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 1248 | } else { |
Taras Lemkin | 96a0d34 | 2018-03-26 14:52:58 +0000 | [diff] [blame] | 1249 | if (Dhcp6HandlerUtil.isServerIpEmpty(foundServerInfo)) { |
Kalhee Kim | 495c9b2 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 1250 | log.warn("Cannot find server info's ipaddress"); |
Charles Chan | b87495c | 2019-02-01 11:31:50 -0800 | [diff] [blame] | 1251 | //dhcpRelayCountersStore.incrementCounter(gCount, DhcpRelayCounters.NO_SERVER_IP6ADDR); |
Kalhee Kim | 495c9b2 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 1252 | return null; |
| 1253 | } |
Kalhee Kim | d21029f | 2017-09-26 20:21:53 +0000 | [diff] [blame] | 1254 | } |
| 1255 | |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 1256 | Dhcp6InterfaceIdOption interfaceIdOption = dhcp6Relay.getOptions().stream() |
| 1257 | .filter(opt -> opt instanceof Dhcp6InterfaceIdOption) |
| 1258 | .map(opt -> (Dhcp6InterfaceIdOption) opt) |
| 1259 | .findFirst() |
| 1260 | .orElse(null); |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 1261 | if (interfaceIdOption == null) { |
| 1262 | log.warn("Interface Id option is not present, abort packet..."); |
Charles Chan | b87495c | 2019-02-01 11:31:50 -0800 | [diff] [blame] | 1263 | //dhcpRelayCountersStore.incrementCounter(gCount, DhcpRelayCounters.OPTION_MISSING_FAIL); |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 1264 | return null; |
| 1265 | } |
| 1266 | |
| 1267 | MacAddress peerMac = interfaceIdOption.getMacAddress(); |
| 1268 | String clientConnectionPointStr = new String(interfaceIdOption.getInPort()); |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 1269 | ConnectPoint clientConnectionPoint = ConnectPoint.deviceConnectPoint(clientConnectionPointStr); |
Kalhee Kim | 0c0cb0b | 2017-09-15 17:43:27 +0000 | [diff] [blame] | 1270 | VlanId vlanIdInUse = VlanId.vlanId(interfaceIdOption.getVlanId()); |
Harshada Chaundkar | f0780fe | 2019-05-06 20:16:13 +0000 | [diff] [blame] | 1271 | |
| 1272 | log.debug("processDhcp6PacketFromServer Interface Id Mac {}, port{}, vlan {}", |
| 1273 | peerMac, clientConnectionPointStr, vlanIdInUse); |
Kalhee Kim | 0c0cb0b | 2017-09-15 17:43:27 +0000 | [diff] [blame] | 1274 | Interface clientInterface = interfaceService.getInterfacesByPort(clientConnectionPoint) |
Taras Lemkin | 96a0d34 | 2018-03-26 14:52:58 +0000 | [diff] [blame] | 1275 | .stream().filter(iface -> Dhcp6HandlerUtil.interfaceContainsVlan(iface, vlanIdInUse)) |
Kalhee Kim | 495c9b2 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 1276 | .findFirst().orElse(null); |
Kalhee Kim | 0c0cb0b | 2017-09-15 17:43:27 +0000 | [diff] [blame] | 1277 | if (clientInterface == null) { |
| 1278 | log.warn("Cannot get client interface for from packet, abort... vlan {}", vlanIdInUse.toString()); |
Charles Chan | b87495c | 2019-02-01 11:31:50 -0800 | [diff] [blame] | 1279 | //dhcpRelayCountersStore.incrementCounter(gCount, DhcpRelayCounters.NO_MATCHING_INTF); |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 1280 | return null; |
| 1281 | } |
Kalhee Kim | 44fbda1 | 2018-04-03 21:08:18 +0000 | [diff] [blame] | 1282 | etherReply.setVlanID(vlanIdInUse.toShort()); |
| 1283 | |
Kalhee Kim | 0c0cb0b | 2017-09-15 17:43:27 +0000 | [diff] [blame] | 1284 | MacAddress relayAgentMac = clientInterface.mac(); |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 1285 | if (relayAgentMac == null) { |
Yi Tseng | 68ef26b | 2017-12-18 17:10:00 -0800 | [diff] [blame] | 1286 | log.warn("Can not get client interface mac, abort packet.."); |
Charles Chan | b87495c | 2019-02-01 11:31:50 -0800 | [diff] [blame] | 1287 | //dhcpRelayCountersStore.incrementCounter(gCount, DhcpRelayCounters.NO_CLIENT_INTF_MAC); |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 1288 | return null; |
| 1289 | } |
| 1290 | etherReply.setSourceMACAddress(relayAgentMac); |
| 1291 | |
| 1292 | // find destMac |
Yi Tseng | 68ef26b | 2017-12-18 17:10:00 -0800 | [diff] [blame] | 1293 | MacAddress clientMac; |
Yi Tseng | aa417a6 | 2017-09-08 17:22:51 -0700 | [diff] [blame] | 1294 | Ip6Address peerAddress = Ip6Address.valueOf(dhcp6Relay.getPeerAddress()); |
| 1295 | Set<Host> clients = hostService.getHostsByIp(peerAddress); |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 1296 | if (clients.isEmpty()) { |
Yi Tseng | 68ef26b | 2017-12-18 17:10:00 -0800 | [diff] [blame] | 1297 | log.trace("There's no host found for this address {}", |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 1298 | HexString.toHexString(dhcp6Relay.getPeerAddress(), ":")); |
Yi Tseng | 68ef26b | 2017-12-18 17:10:00 -0800 | [diff] [blame] | 1299 | log.trace("Let's look up interfaceId {}", HexString.toHexString(peerMac.toBytes(), ":")); |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 1300 | clientMac = peerMac; |
| 1301 | } else { |
| 1302 | clientMac = clients.iterator().next().mac(); |
| 1303 | if (clientMac == null) { |
| 1304 | log.warn("No client mac address found, abort packet..."); |
Charles Chan | b87495c | 2019-02-01 11:31:50 -0800 | [diff] [blame] | 1305 | //dhcpRelayCountersStore.incrementCounter(gCount, DhcpRelayCounters.NO_CLIENT_INTF_MAC); |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 1306 | return null; |
| 1307 | } |
Yi Tseng | 68ef26b | 2017-12-18 17:10:00 -0800 | [diff] [blame] | 1308 | log.trace("Client mac address found from getHostByIp"); |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 1309 | } |
| 1310 | etherReply.setDestinationMACAddress(clientMac); |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 1311 | // ip header |
| 1312 | ipv6Packet.setSourceAddress(dhcp6Relay.getLinkAddress()); |
| 1313 | ipv6Packet.setDestinationAddress(dhcp6Relay.getPeerAddress()); |
| 1314 | // udp header |
| 1315 | udpPacket.setSourcePort(UDP.DHCP_V6_SERVER_PORT); |
| 1316 | if (directConnFlag) { |
| 1317 | udpPacket.setDestinationPort(UDP.DHCP_V6_CLIENT_PORT); |
| 1318 | } else { |
| 1319 | udpPacket.setDestinationPort(UDP.DHCP_V6_SERVER_PORT); |
| 1320 | } |
Lior Assouline | a21c0ca | 2018-01-28 16:18:48 -0800 | [diff] [blame] | 1321 | |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 1322 | |
Taras Lemkin | 96a0d34 | 2018-03-26 14:52:58 +0000 | [diff] [blame] | 1323 | boolean hostOrRouteAllowed = learnRouteFromLeasequery || |
| 1324 | Dhcp6HandlerUtil.getDhcp6LeafMessageType(dhcp6Relay) != MsgType.LEASEQUERY_REPLY; |
| 1325 | log.debug("Can add host or route: {}", hostOrRouteAllowed); |
| 1326 | |
| 1327 | if (hostOrRouteAllowed) { |
| 1328 | // add host or route |
| 1329 | addHostOrRoute(directConnFlag, clientConnectionPoint, dhcp6Relay, embeddedDhcp6, |
pier | 86231ef | 2019-08-23 10:45:40 -0700 | [diff] [blame] | 1330 | clientMac, clientInterface, vlanIdInUse); |
Taras Lemkin | 96a0d34 | 2018-03-26 14:52:58 +0000 | [diff] [blame] | 1331 | } |
| 1332 | |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 1333 | udpPacket.setPayload(embeddedDhcp6); |
| 1334 | udpPacket.resetChecksum(); |
| 1335 | ipv6Packet.setPayload(udpPacket); |
| 1336 | etherReply.setPayload(ipv6Packet); |
Taras Lemkin | 96a0d34 | 2018-03-26 14:52:58 +0000 | [diff] [blame] | 1337 | return InternalPacket.internalPacket(etherReply, clientConnectionPoint); |
Lior Assouline | a21c0ca | 2018-01-28 16:18:48 -0800 | [diff] [blame] | 1338 | } |
Yi Tseng | 483ac6f | 2017-08-02 15:03:31 -0700 | [diff] [blame] | 1339 | |
| 1340 | @Override |
Kalhee Kim | ba36606 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 1341 | public void setDhcpFpmEnabled(Boolean enabled) { |
| 1342 | dhcpFpmEnabled = enabled; |
| 1343 | } |
| 1344 | |
| 1345 | @Override |
Yi Tseng | 483ac6f | 2017-08-02 15:03:31 -0700 | [diff] [blame] | 1346 | public void setDefaultDhcpServerConfigs(Collection<DhcpServerConfig> configs) { |
Kalhee Kim | 495c9b2 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 1347 | log.debug("setDefaultDhcpServerConfigs is called."); |
Yi Tseng | 919b2df | 2017-09-07 16:22:51 -0700 | [diff] [blame] | 1348 | setDhcpServerConfigs(configs, defaultServerInfoList); |
Yi Tseng | 919b2df | 2017-09-07 16:22:51 -0700 | [diff] [blame] | 1349 | } |
| 1350 | |
| 1351 | @Override |
| 1352 | public void setIndirectDhcpServerConfigs(Collection<DhcpServerConfig> configs) { |
Kalhee Kim | 495c9b2 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 1353 | log.debug("setIndirectDhcpServerConfigs is called."); |
Yi Tseng | 919b2df | 2017-09-07 16:22:51 -0700 | [diff] [blame] | 1354 | setDhcpServerConfigs(configs, indirectServerInfoList); |
Yi Tseng | 919b2df | 2017-09-07 16:22:51 -0700 | [diff] [blame] | 1355 | } |
| 1356 | |
| 1357 | public void setDhcpServerConfigs(Collection<DhcpServerConfig> configs, List<DhcpServerInfo> serverInfoList) { |
Kalhee Kim | 495c9b2 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 1358 | log.debug("config size {}.", configs.size()); |
| 1359 | |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 1360 | if (configs.size() == 0) { |
| 1361 | // no config to update |
| 1362 | return; |
| 1363 | } |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 1364 | // TODO: currently we pick up first DHCP server config. |
| 1365 | // Will use other server configs in the future for HA. |
Kalhee Kim | 495c9b2 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 1366 | Boolean isConfigValid = false; |
| 1367 | for (DhcpServerConfig serverConfig : configs) { |
| 1368 | if (serverConfig.getDhcpServerIp6().isPresent()) { |
| 1369 | isConfigValid = true; |
| 1370 | break; |
| 1371 | } |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 1372 | } |
Kalhee Kim | 495c9b2 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 1373 | if (!isConfigValid) { |
| 1374 | log.warn("No IP V6 server address found."); |
| 1375 | return; // No IP V6 address found |
| 1376 | } |
| 1377 | for (DhcpServerInfo oldServerInfo : serverInfoList) { |
Yi Tseng | 919b2df | 2017-09-07 16:22:51 -0700 | [diff] [blame] | 1378 | // stop monitoring gateway or server |
| 1379 | oldServerInfo.getDhcpGatewayIp6().ifPresent(gatewayIp -> { |
| 1380 | hostService.stopMonitoringIp(gatewayIp); |
| 1381 | }); |
| 1382 | oldServerInfo.getDhcpServerIp6().ifPresent(serverIp -> { |
| 1383 | hostService.stopMonitoringIp(serverIp); |
Yi Tseng | 525ff40 | 2017-10-23 19:39:39 -0700 | [diff] [blame] | 1384 | cancelDhcpPacket(serverIp); |
Yi Tseng | 919b2df | 2017-09-07 16:22:51 -0700 | [diff] [blame] | 1385 | }); |
| 1386 | } |
Kalhee Kim | 495c9b2 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 1387 | serverInfoList.clear(); |
| 1388 | for (DhcpServerConfig serverConfig : configs) { |
| 1389 | // Create new server info according to the config |
| 1390 | DhcpServerInfo newServerInfo = new DhcpServerInfo(serverConfig, |
| 1391 | DhcpServerInfo.Version.DHCP_V6); |
| 1392 | checkState(newServerInfo.getDhcpServerConnectPoint().isPresent(), |
| 1393 | "Connect point not exists"); |
| 1394 | checkState(newServerInfo.getDhcpServerIp6().isPresent(), |
| 1395 | "IP of DHCP server not exists"); |
Yi Tseng | 919b2df | 2017-09-07 16:22:51 -0700 | [diff] [blame] | 1396 | |
Kalhee Kim | 495c9b2 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 1397 | log.debug("DHCP server connect point: {}", newServerInfo.getDhcpServerConnectPoint().orElse(null)); |
| 1398 | log.debug("DHCP server IP: {}", newServerInfo.getDhcpServerIp6().orElse(null)); |
Yi Tseng | 919b2df | 2017-09-07 16:22:51 -0700 | [diff] [blame] | 1399 | |
Kalhee Kim | 495c9b2 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 1400 | Ip6Address serverIp = newServerInfo.getDhcpServerIp6().get(); |
| 1401 | Ip6Address ipToProbe; |
| 1402 | if (newServerInfo.getDhcpGatewayIp6().isPresent()) { |
| 1403 | ipToProbe = newServerInfo.getDhcpGatewayIp6().get(); |
| 1404 | } else { |
| 1405 | ipToProbe = newServerInfo.getDhcpServerIp6().orElse(null); |
| 1406 | } |
| 1407 | String hostToProbe = newServerInfo.getDhcpGatewayIp6() |
| 1408 | .map(ip -> "gateway").orElse("server"); |
Yi Tseng | 919b2df | 2017-09-07 16:22:51 -0700 | [diff] [blame] | 1409 | |
Kalhee Kim | 495c9b2 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 1410 | log.warn("Probing to resolve {} IP {}", hostToProbe, ipToProbe); |
| 1411 | hostService.startMonitoringIp(ipToProbe); |
| 1412 | |
| 1413 | Set<Host> hosts = hostService.getHostsByIp(ipToProbe); |
| 1414 | if (!hosts.isEmpty()) { |
| 1415 | Host host = hosts.iterator().next(); |
| 1416 | newServerInfo.setDhcpConnectVlan(host.vlan()); |
| 1417 | newServerInfo.setDhcpConnectMac(host.mac()); |
| 1418 | log.warn("Host found host {}", host); |
| 1419 | |
| 1420 | } else { |
| 1421 | log.warn("No host found host ip {}", ipToProbe); |
| 1422 | } |
| 1423 | // Add new server info |
| 1424 | synchronized (this) { |
| 1425 | serverInfoList.add(newServerInfo); |
| 1426 | } |
| 1427 | if (!hosts.isEmpty()) { |
| 1428 | requestDhcpPacket(serverIp); |
| 1429 | } |
Yi Tseng | 919b2df | 2017-09-07 16:22:51 -0700 | [diff] [blame] | 1430 | } |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 1431 | } |
| 1432 | |
| 1433 | class InternalHostListener implements HostListener { |
| 1434 | @Override |
| 1435 | public void event(HostEvent event) { |
| 1436 | switch (event.type()) { |
| 1437 | case HOST_ADDED: |
| 1438 | case HOST_UPDATED: |
Charles Chan | 24a96ff | 2018-08-13 10:32:03 -0700 | [diff] [blame] | 1439 | case HOST_MOVED: |
Jordan Halterman | 6328db7 | 2018-04-10 13:34:50 -0400 | [diff] [blame] | 1440 | log.trace("Scheduled host event {}", event); |
| 1441 | hostEventExecutor.execute(() -> hostUpdated(event.subject())); |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 1442 | break; |
| 1443 | case HOST_REMOVED: |
Jordan Halterman | 6328db7 | 2018-04-10 13:34:50 -0400 | [diff] [blame] | 1444 | log.trace("Scheduled host event {}", event); |
| 1445 | hostEventExecutor.execute(() -> hostRemoved(event.subject())); |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 1446 | break; |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 1447 | default: |
| 1448 | break; |
| 1449 | } |
| 1450 | } |
| 1451 | } |
| 1452 | |
| 1453 | /** |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 1454 | * Handle host updated. |
| 1455 | * If the host is DHCP server or gateway, update connect mac and vlan. |
| 1456 | * |
| 1457 | * @param host the host |
| 1458 | */ |
| 1459 | private void hostUpdated(Host host) { |
Yi Tseng | 525ff40 | 2017-10-23 19:39:39 -0700 | [diff] [blame] | 1460 | hostUpdated(host, defaultServerInfoList); |
| 1461 | hostUpdated(host, indirectServerInfoList); |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 1462 | } |
| 1463 | |
Yi Tseng | 525ff40 | 2017-10-23 19:39:39 -0700 | [diff] [blame] | 1464 | private void hostUpdated(Host host, List<DhcpServerInfo> serverInfoList) { |
jjosep004c | 05f55ab | 2018-08-21 09:01:10 -0400 | [diff] [blame] | 1465 | serverInfoList.stream().forEach(serverInfo -> { |
Yi Tseng | 525ff40 | 2017-10-23 19:39:39 -0700 | [diff] [blame] | 1466 | Ip6Address serverIp = serverInfo.getDhcpServerIp6().orElse(null); |
jjosep004c | 05f55ab | 2018-08-21 09:01:10 -0400 | [diff] [blame] | 1467 | Ip6Address targetIp = serverInfo.getDhcpGatewayIp6().orElse(null); |
Yi Tseng | 525ff40 | 2017-10-23 19:39:39 -0700 | [diff] [blame] | 1468 | |
| 1469 | if (targetIp == null) { |
| 1470 | targetIp = serverIp; |
| 1471 | } |
Yi Tseng | 525ff40 | 2017-10-23 19:39:39 -0700 | [diff] [blame] | 1472 | if (targetIp != null) { |
| 1473 | if (host.ipAddresses().contains(targetIp)) { |
| 1474 | serverInfo.setDhcpConnectMac(host.mac()); |
| 1475 | serverInfo.setDhcpConnectVlan(host.vlan()); |
| 1476 | requestDhcpPacket(serverIp); |
| 1477 | } |
| 1478 | } |
jjosep004c | 05f55ab | 2018-08-21 09:01:10 -0400 | [diff] [blame] | 1479 | }); |
Yi Tseng | 525ff40 | 2017-10-23 19:39:39 -0700 | [diff] [blame] | 1480 | } |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 1481 | /** |
| 1482 | * Handle host removed. |
| 1483 | * If the host is DHCP server or gateway, unset connect mac and vlan. |
| 1484 | * |
| 1485 | * @param host the host |
| 1486 | */ |
| 1487 | private void hostRemoved(Host host) { |
Yi Tseng | 525ff40 | 2017-10-23 19:39:39 -0700 | [diff] [blame] | 1488 | hostRemoved(host, defaultServerInfoList); |
| 1489 | hostRemoved(host, indirectServerInfoList); |
Yi Tseng | 919b2df | 2017-09-07 16:22:51 -0700 | [diff] [blame] | 1490 | } |
| 1491 | |
Yi Tseng | 525ff40 | 2017-10-23 19:39:39 -0700 | [diff] [blame] | 1492 | private void hostRemoved(Host host, List<DhcpServerInfo> serverInfoList) { |
jjosep004c | 05f55ab | 2018-08-21 09:01:10 -0400 | [diff] [blame] | 1493 | serverInfoList.stream().forEach(serverInfo -> { |
Yi Tseng | 525ff40 | 2017-10-23 19:39:39 -0700 | [diff] [blame] | 1494 | Ip6Address serverIp = serverInfo.getDhcpServerIp6().orElse(null); |
jjosep004c | 05f55ab | 2018-08-21 09:01:10 -0400 | [diff] [blame] | 1495 | Ip6Address targetIp = serverInfo.getDhcpGatewayIp6().orElse(null); |
Yi Tseng | 525ff40 | 2017-10-23 19:39:39 -0700 | [diff] [blame] | 1496 | |
| 1497 | if (targetIp == null) { |
| 1498 | targetIp = serverIp; |
| 1499 | } |
Yi Tseng | 525ff40 | 2017-10-23 19:39:39 -0700 | [diff] [blame] | 1500 | if (targetIp != null) { |
| 1501 | if (host.ipAddresses().contains(targetIp)) { |
| 1502 | serverInfo.setDhcpConnectVlan(null); |
| 1503 | serverInfo.setDhcpConnectMac(null); |
| 1504 | cancelDhcpPacket(serverIp); |
| 1505 | } |
| 1506 | } |
jjosep004c | 05f55ab | 2018-08-21 09:01:10 -0400 | [diff] [blame] | 1507 | }); |
Yi Tseng | 525ff40 | 2017-10-23 19:39:39 -0700 | [diff] [blame] | 1508 | } |
| 1509 | |
Lior Assouline | a21c0ca | 2018-01-28 16:18:48 -0800 | [diff] [blame] | 1510 | /** |
| 1511 | * Gets Interface facing to the server for default host. |
| 1512 | * |
| 1513 | * @return the Interface facing to the server; null if not found |
| 1514 | */ |
| 1515 | private Interface getServerInterface() { |
| 1516 | DhcpServerInfo serverInfo; |
| 1517 | ConnectPoint dhcpServerConnectPoint; |
| 1518 | VlanId dhcpConnectVlan; |
| 1519 | |
| 1520 | if (!defaultServerInfoList.isEmpty()) { |
| 1521 | serverInfo = defaultServerInfoList.get(0); |
| 1522 | dhcpServerConnectPoint = serverInfo.getDhcpServerConnectPoint().orElse(null); |
| 1523 | dhcpConnectVlan = serverInfo.getDhcpConnectVlan().orElse(null); |
| 1524 | } else { |
| 1525 | return null; |
| 1526 | } |
| 1527 | if (dhcpServerConnectPoint == null || dhcpConnectVlan == null) { |
| 1528 | log.info("Default DHCP server {} not resolve yet", serverInfo.getDhcpGatewayIp6()); |
| 1529 | return null; |
| 1530 | } |
| 1531 | return interfaceService.getInterfacesByPort(dhcpServerConnectPoint) |
| 1532 | .stream() |
Taras Lemkin | 96a0d34 | 2018-03-26 14:52:58 +0000 | [diff] [blame] | 1533 | .filter(iface -> Dhcp6HandlerUtil.interfaceContainsVlan(iface, dhcpConnectVlan)) |
Lior Assouline | a21c0ca | 2018-01-28 16:18:48 -0800 | [diff] [blame] | 1534 | .findFirst() |
| 1535 | .orElse(null); |
| 1536 | } |
| 1537 | |
| 1538 | /** |
| 1539 | * Gets Interface facing to the server for indirect hosts. |
| 1540 | * Use default server Interface if indirect server not configured. |
| 1541 | * |
| 1542 | * @return the Interface facing to the server; null if not found |
| 1543 | */ |
| 1544 | private Interface getIndirectServerInterface() { |
| 1545 | DhcpServerInfo serverInfo; |
| 1546 | |
| 1547 | ConnectPoint indirectDhcpServerConnectPoint; |
| 1548 | VlanId indirectDhcpConnectVlan; |
| 1549 | |
| 1550 | if (!indirectServerInfoList.isEmpty()) { |
| 1551 | serverInfo = indirectServerInfoList.get(0); |
| 1552 | indirectDhcpServerConnectPoint = serverInfo.getDhcpServerConnectPoint().orElse(null); |
| 1553 | indirectDhcpConnectVlan = serverInfo.getDhcpConnectVlan().orElse(null); |
| 1554 | } else { |
| 1555 | return getServerInterface(); |
| 1556 | } |
| 1557 | if (indirectDhcpServerConnectPoint == null || indirectDhcpConnectVlan == null) { |
| 1558 | log.info("Indirect DHCP server {} not resolve yet", serverInfo.getDhcpGatewayIp6()); |
| 1559 | return null; |
| 1560 | } |
| 1561 | return interfaceService.getInterfacesByPort(indirectDhcpServerConnectPoint) |
| 1562 | .stream() |
Taras Lemkin | 96a0d34 | 2018-03-26 14:52:58 +0000 | [diff] [blame] | 1563 | .filter(iface -> Dhcp6HandlerUtil.interfaceContainsVlan(iface, indirectDhcpConnectVlan)) |
Lior Assouline | a21c0ca | 2018-01-28 16:18:48 -0800 | [diff] [blame] | 1564 | .findFirst() |
| 1565 | .orElse(null); |
| 1566 | } |
| 1567 | |
Kalhee Kim | 45b2418 | 2017-10-18 18:30:23 +0000 | [diff] [blame] | 1568 | /** |
Kalhee Kim | 495c9b2 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 1569 | * Checks if serverInfo's host info (mac and vlan) is filled in; if not, fills in. |
| 1570 | * |
| 1571 | * @param serverInfo server information |
| 1572 | * @return newServerInfo if host info can be either found or filled in. |
| 1573 | */ |
| 1574 | private DhcpServerInfo getHostInfoForServerInfo(DhcpServerInfo serverInfo, List<DhcpServerInfo> sererInfoList) { |
| 1575 | DhcpServerInfo newServerInfo = null; |
| 1576 | MacAddress dhcpServerConnectMac = serverInfo.getDhcpConnectMac().orElse(null); |
| 1577 | VlanId dhcpConnectVlan = serverInfo.getDhcpConnectVlan().orElse(null); |
| 1578 | ConnectPoint dhcpServerConnectPoint = serverInfo.getDhcpServerConnectPoint().orElse(null); |
| 1579 | |
| 1580 | if (dhcpServerConnectMac != null && dhcpConnectVlan != null) { |
| 1581 | newServerInfo = serverInfo; |
Charles Chan | 750c999 | 2018-02-26 10:44:49 -0800 | [diff] [blame] | 1582 | log.debug("DHCP server {} host info found. ConnectPt{} Mac {} vlan {}", serverInfo.getDhcpServerIp6(), |
Kalhee Kim | 495c9b2 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 1583 | dhcpServerConnectPoint, dhcpServerConnectMac, dhcpConnectVlan); |
| 1584 | } else { |
Harshada Chaundkar | f0780fe | 2019-05-06 20:16:13 +0000 | [diff] [blame] | 1585 | log.debug("DHCP server {} not resolve yet connectPt {} mac {} vlan {}", serverInfo.getDhcpServerIp6(), |
Kalhee Kim | 495c9b2 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 1586 | dhcpServerConnectPoint, dhcpServerConnectMac, dhcpConnectVlan); |
| 1587 | |
| 1588 | Ip6Address ipToProbe; |
| 1589 | if (serverInfo.getDhcpGatewayIp6().isPresent()) { |
| 1590 | ipToProbe = serverInfo.getDhcpGatewayIp6().get(); |
| 1591 | } else { |
| 1592 | ipToProbe = serverInfo.getDhcpServerIp6().orElse(null); |
| 1593 | } |
| 1594 | String hostToProbe = serverInfo.getDhcpGatewayIp6() |
| 1595 | .map(ip -> "gateway").orElse("server"); |
| 1596 | |
Harshada Chaundkar | f0780fe | 2019-05-06 20:16:13 +0000 | [diff] [blame] | 1597 | log.debug("Dynamically probing to resolve {} IP {}", hostToProbe, ipToProbe); |
Kalhee Kim | 495c9b2 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 1598 | hostService.startMonitoringIp(ipToProbe); |
| 1599 | |
| 1600 | Set<Host> hosts = hostService.getHostsByIp(ipToProbe); |
| 1601 | if (!hosts.isEmpty()) { |
| 1602 | int serverInfoIndex = sererInfoList.indexOf(serverInfo); |
| 1603 | Host host = hosts.iterator().next(); |
| 1604 | serverInfo.setDhcpConnectVlan(host.vlan()); |
| 1605 | serverInfo.setDhcpConnectMac(host.mac()); |
| 1606 | // replace the serverInfo in the list |
| 1607 | sererInfoList.set(serverInfoIndex, serverInfo); |
| 1608 | newServerInfo = serverInfo; |
| 1609 | log.warn("Dynamically host found host {}", host); |
| 1610 | } else { |
Harshada Chaundkar | f0780fe | 2019-05-06 20:16:13 +0000 | [diff] [blame] | 1611 | log.debug("No host found host ip {} dynamically", ipToProbe); |
Kalhee Kim | 495c9b2 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 1612 | } |
| 1613 | } |
| 1614 | return newServerInfo; |
| 1615 | } |
| 1616 | |
| 1617 | /** |
Kalhee Kim | 45b2418 | 2017-10-18 18:30:23 +0000 | [diff] [blame] | 1618 | * Gets Interface facing to the server for default host. |
| 1619 | * |
Kalhee Kim | 495c9b2 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 1620 | * @param serverInfo server information |
Kalhee Kim | 45b2418 | 2017-10-18 18:30:23 +0000 | [diff] [blame] | 1621 | * @return the Interface facing to the server; null if not found |
| 1622 | */ |
Kalhee Kim | 495c9b2 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 1623 | private Interface getServerInterface(DhcpServerInfo serverInfo) { |
| 1624 | Interface serverInterface = null; |
Yi Tseng | 25bfe37 | 2017-11-03 16:27:32 -0700 | [diff] [blame] | 1625 | |
Kalhee Kim | 495c9b2 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 1626 | ConnectPoint dhcpServerConnectPoint = serverInfo.getDhcpServerConnectPoint().orElse(null); |
| 1627 | VlanId dhcpConnectVlan = serverInfo.getDhcpConnectVlan().orElse(null); |
| 1628 | |
| 1629 | if (dhcpServerConnectPoint != null && dhcpConnectVlan != null) { |
| 1630 | serverInterface = interfaceService.getInterfacesByPort(dhcpServerConnectPoint) |
| 1631 | .stream() |
Taras Lemkin | 96a0d34 | 2018-03-26 14:52:58 +0000 | [diff] [blame] | 1632 | .filter(iface -> Dhcp6HandlerUtil.interfaceContainsVlan(iface, dhcpConnectVlan)) |
Kalhee Kim | 495c9b2 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 1633 | .findFirst() |
| 1634 | .orElse(null); |
Yi Tseng | 25bfe37 | 2017-11-03 16:27:32 -0700 | [diff] [blame] | 1635 | } else { |
Kalhee Kim | 495c9b2 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 1636 | log.warn("DHCP server {} not resolve yet connectPoint {} vlan {}", serverInfo.getDhcpServerIp6(), |
| 1637 | dhcpServerConnectPoint, dhcpConnectVlan); |
Kalhee Kim | 45b2418 | 2017-10-18 18:30:23 +0000 | [diff] [blame] | 1638 | } |
Kalhee Kim | 495c9b2 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 1639 | |
| 1640 | return serverInterface; |
Kalhee Kim | 45b2418 | 2017-10-18 18:30:23 +0000 | [diff] [blame] | 1641 | } |
| 1642 | |
Yi Tseng | 525ff40 | 2017-10-23 19:39:39 -0700 | [diff] [blame] | 1643 | private void requestDhcpPacket(Ip6Address serverIp) { |
| 1644 | requestServerDhcpPacket(serverIp); |
| 1645 | requestClientDhcpPacket(serverIp); |
Lior Assouline | a21c0ca | 2018-01-28 16:18:48 -0800 | [diff] [blame] | 1646 | requestServerLQPacket(serverIp); |
Yi Tseng | 525ff40 | 2017-10-23 19:39:39 -0700 | [diff] [blame] | 1647 | } |
| 1648 | |
| 1649 | private void cancelDhcpPacket(Ip6Address serverIp) { |
| 1650 | cancelServerDhcpPacket(serverIp); |
| 1651 | cancelClientDhcpPacket(serverIp); |
Lior Assouline | a21c0ca | 2018-01-28 16:18:48 -0800 | [diff] [blame] | 1652 | cancelServerLQPacket(serverIp); |
Yi Tseng | 525ff40 | 2017-10-23 19:39:39 -0700 | [diff] [blame] | 1653 | } |
| 1654 | |
| 1655 | private void cancelServerDhcpPacket(Ip6Address serverIp) { |
| 1656 | TrafficSelector serverSelector = |
| 1657 | DefaultTrafficSelector.builder(SERVER_RELAY_SELECTOR) |
| 1658 | .matchIPv6Src(serverIp.toIpPrefix()) |
| 1659 | .build(); |
| 1660 | packetService.cancelPackets(serverSelector, |
| 1661 | PacketPriority.CONTROL, |
| 1662 | appId); |
| 1663 | } |
| 1664 | |
Lior Assouline | a21c0ca | 2018-01-28 16:18:48 -0800 | [diff] [blame] | 1665 | private void cancelServerLQPacket(Ip6Address serverIp) { |
| 1666 | TrafficSelector serverSelector = |
| 1667 | DefaultTrafficSelector.builder(LEASE_QUERY_RESPONSE_SELECTOR) |
| 1668 | .matchIPv6Src(serverIp.toIpPrefix()) |
| 1669 | .build(); |
| 1670 | packetService.cancelPackets(serverSelector, |
| 1671 | PacketPriority.CONTROL, |
| 1672 | appId); |
| 1673 | } |
| 1674 | |
Yi Tseng | 525ff40 | 2017-10-23 19:39:39 -0700 | [diff] [blame] | 1675 | private void requestServerDhcpPacket(Ip6Address serverIp) { |
| 1676 | TrafficSelector serverSelector = |
| 1677 | DefaultTrafficSelector.builder(SERVER_RELAY_SELECTOR) |
| 1678 | .matchIPv6Src(serverIp.toIpPrefix()) |
| 1679 | .build(); |
| 1680 | packetService.requestPackets(serverSelector, |
| 1681 | PacketPriority.CONTROL, |
| 1682 | appId); |
| 1683 | } |
| 1684 | |
Lior Assouline | a21c0ca | 2018-01-28 16:18:48 -0800 | [diff] [blame] | 1685 | private void requestServerLQPacket(Ip6Address serverIp) { |
| 1686 | TrafficSelector serverSelector = |
| 1687 | DefaultTrafficSelector.builder(LEASE_QUERY_RESPONSE_SELECTOR) |
| 1688 | .matchIPv6Src(serverIp.toIpPrefix()) |
| 1689 | .build(); |
| 1690 | packetService.requestPackets(serverSelector, |
| 1691 | PacketPriority.CONTROL, |
| 1692 | appId); |
| 1693 | } |
| 1694 | |
Yi Tseng | 525ff40 | 2017-10-23 19:39:39 -0700 | [diff] [blame] | 1695 | private void cancelClientDhcpPacket(Ip6Address serverIp) { |
| 1696 | // Packet comes from relay |
| 1697 | TrafficSelector indirectClientSelector = |
| 1698 | DefaultTrafficSelector.builder(SERVER_RELAY_SELECTOR) |
| 1699 | .matchIPv6Dst(serverIp.toIpPrefix()) |
| 1700 | .build(); |
| 1701 | packetService.cancelPackets(indirectClientSelector, |
| 1702 | PacketPriority.CONTROL, |
| 1703 | appId); |
Yi Tseng | 41dde70 | 2017-11-02 15:21:10 -0700 | [diff] [blame] | 1704 | indirectClientSelector = |
| 1705 | DefaultTrafficSelector.builder(SERVER_RELAY_SELECTOR) |
| 1706 | .matchIPv6Dst(Ip6Address.ALL_DHCP_RELAY_AGENTS_AND_SERVERS.toIpPrefix()) |
| 1707 | .build(); |
| 1708 | packetService.cancelPackets(indirectClientSelector, |
| 1709 | PacketPriority.CONTROL, |
| 1710 | appId); |
| 1711 | indirectClientSelector = |
| 1712 | DefaultTrafficSelector.builder(SERVER_RELAY_SELECTOR) |
| 1713 | .matchIPv6Dst(Ip6Address.ALL_DHCP_SERVERS.toIpPrefix()) |
| 1714 | .build(); |
| 1715 | packetService.cancelPackets(indirectClientSelector, |
| 1716 | PacketPriority.CONTROL, |
| 1717 | appId); |
Yi Tseng | 525ff40 | 2017-10-23 19:39:39 -0700 | [diff] [blame] | 1718 | |
| 1719 | // Packet comes from client |
| 1720 | packetService.cancelPackets(CLIENT_SERVER_SELECTOR, |
| 1721 | PacketPriority.CONTROL, |
| 1722 | appId); |
| 1723 | } |
| 1724 | |
| 1725 | private void requestClientDhcpPacket(Ip6Address serverIp) { |
| 1726 | // Packet comes from relay |
| 1727 | TrafficSelector indirectClientSelector = |
| 1728 | DefaultTrafficSelector.builder(SERVER_RELAY_SELECTOR) |
| 1729 | .matchIPv6Dst(serverIp.toIpPrefix()) |
| 1730 | .build(); |
| 1731 | packetService.requestPackets(indirectClientSelector, |
| 1732 | PacketPriority.CONTROL, |
| 1733 | appId); |
Yi Tseng | 41dde70 | 2017-11-02 15:21:10 -0700 | [diff] [blame] | 1734 | indirectClientSelector = |
| 1735 | DefaultTrafficSelector.builder(SERVER_RELAY_SELECTOR) |
| 1736 | .matchIPv6Dst(Ip6Address.ALL_DHCP_RELAY_AGENTS_AND_SERVERS.toIpPrefix()) |
| 1737 | .build(); |
| 1738 | packetService.requestPackets(indirectClientSelector, |
| 1739 | PacketPriority.CONTROL, |
| 1740 | appId); |
| 1741 | indirectClientSelector = |
| 1742 | DefaultTrafficSelector.builder(SERVER_RELAY_SELECTOR) |
| 1743 | .matchIPv6Dst(Ip6Address.ALL_DHCP_SERVERS.toIpPrefix()) |
| 1744 | .build(); |
| 1745 | packetService.requestPackets(indirectClientSelector, |
| 1746 | PacketPriority.CONTROL, |
| 1747 | appId); |
Yi Tseng | 525ff40 | 2017-10-23 19:39:39 -0700 | [diff] [blame] | 1748 | |
| 1749 | // Packet comes from client |
| 1750 | packetService.requestPackets(CLIENT_SERVER_SELECTOR, |
| 1751 | PacketPriority.CONTROL, |
| 1752 | appId); |
| 1753 | } |
| 1754 | |
| 1755 | /** |
| 1756 | * Process the ignore rules. |
| 1757 | * |
| 1758 | * @param deviceId the device id |
| 1759 | * @param vlanId the vlan to be ignored |
| 1760 | * @param op the operation, ADD to install; REMOVE to uninstall rules |
| 1761 | */ |
| 1762 | private void processIgnoreVlanRule(DeviceId deviceId, VlanId vlanId, Objective.Operation op) { |
Yi Tseng | 525ff40 | 2017-10-23 19:39:39 -0700 | [diff] [blame] | 1763 | AtomicInteger installedCount = new AtomicInteger(DHCP_SELECTORS.size()); |
| 1764 | DHCP_SELECTORS.forEach(trafficSelector -> { |
| 1765 | TrafficSelector selector = DefaultTrafficSelector.builder(trafficSelector) |
| 1766 | .matchVlanId(vlanId) |
| 1767 | .build(); |
| 1768 | |
| 1769 | ForwardingObjective.Builder builder = DefaultForwardingObjective.builder() |
| 1770 | .withFlag(ForwardingObjective.Flag.VERSATILE) |
| 1771 | .withSelector(selector) |
| 1772 | .withPriority(IGNORE_CONTROL_PRIORITY) |
Yi Tseng | dbabeed | 2017-11-29 10:49:20 -0800 | [diff] [blame] | 1773 | .withTreatment(DefaultTrafficTreatment.emptyTreatment()) |
Yi Tseng | 525ff40 | 2017-10-23 19:39:39 -0700 | [diff] [blame] | 1774 | .fromApp(appId); |
| 1775 | |
| 1776 | |
| 1777 | ObjectiveContext objectiveContext = new ObjectiveContext() { |
| 1778 | @Override |
| 1779 | public void onSuccess(Objective objective) { |
| 1780 | log.info("Ignore rule {} (Vlan id {}, device {}, selector {})", |
| 1781 | op, vlanId, deviceId, selector); |
| 1782 | int countDown = installedCount.decrementAndGet(); |
| 1783 | if (countDown != 0) { |
| 1784 | return; |
| 1785 | } |
| 1786 | switch (op) { |
| 1787 | case ADD: |
| 1788 | ignoredVlans.put(deviceId, vlanId); |
| 1789 | break; |
| 1790 | case REMOVE: |
| 1791 | ignoredVlans.remove(deviceId, vlanId); |
| 1792 | break; |
| 1793 | default: |
| 1794 | log.warn("Unsupported objective operation {}", op); |
| 1795 | break; |
| 1796 | } |
| 1797 | } |
| 1798 | |
| 1799 | @Override |
| 1800 | public void onError(Objective objective, ObjectiveError error) { |
| 1801 | log.warn("Can't {} ignore rule (vlan id {}, selector {}, device {}) due to {}", |
| 1802 | op, vlanId, selector, deviceId, error); |
| 1803 | } |
| 1804 | }; |
| 1805 | |
| 1806 | ForwardingObjective fwd; |
| 1807 | switch (op) { |
| 1808 | case ADD: |
| 1809 | fwd = builder.add(objectiveContext); |
| 1810 | break; |
| 1811 | case REMOVE: |
| 1812 | fwd = builder.remove(objectiveContext); |
| 1813 | break; |
| 1814 | default: |
| 1815 | log.warn("Unsupported objective operation {}", op); |
| 1816 | return; |
| 1817 | } |
| 1818 | |
| 1819 | Device device = deviceService.getDevice(deviceId); |
| 1820 | if (device == null || !device.is(Pipeliner.class)) { |
| 1821 | log.warn("Device {} is not available now, wait until device is available", deviceId); |
| 1822 | return; |
| 1823 | } |
| 1824 | flowObjectiveService.apply(deviceId, fwd); |
| 1825 | }); |
| 1826 | } |
Lior Assouline | a21c0ca | 2018-01-28 16:18:48 -0800 | [diff] [blame] | 1827 | |
Kalhee Kim | 121ba92 | 2017-11-01 17:56:44 +0000 | [diff] [blame] | 1828 | /** |
| 1829 | * Find first ipaddress for a given Host info i.e. mac and vlan. |
| 1830 | * |
| 1831 | * @param clientMac client mac |
| 1832 | * @param vlanId packet's vlan |
| 1833 | * @return next-hop link-local ipaddress for a given host |
| 1834 | */ |
Kalhee Kim | 495c9b2 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 1835 | private IpAddress getFirstIpByHost(Boolean directConnFlag, MacAddress clientMac, VlanId vlanId) { |
Kalhee Kim | 121ba92 | 2017-11-01 17:56:44 +0000 | [diff] [blame] | 1836 | IpAddress nextHopIp; |
| 1837 | // pick out the first link-local ip address |
| 1838 | HostId gwHostId = HostId.hostId(clientMac, vlanId); |
| 1839 | Host gwHost = hostService.getHost(gwHostId); |
| 1840 | if (gwHost == null) { |
| 1841 | log.warn("Can't find gateway host for hostId {}", gwHostId); |
| 1842 | return null; |
| 1843 | } |
Kalhee Kim | 495c9b2 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 1844 | if (directConnFlag) { |
| 1845 | nextHopIp = gwHost.ipAddresses() |
| 1846 | .stream() |
| 1847 | .filter(IpAddress::isIp6) |
| 1848 | .map(IpAddress::getIp6Address) |
| 1849 | .findFirst() |
| 1850 | .orElse(null); |
| 1851 | } else { |
| 1852 | nextHopIp = gwHost.ipAddresses() |
| 1853 | .stream() |
| 1854 | .filter(IpAddress::isIp6) |
| 1855 | .filter(ip6 -> ip6.isLinkLocal()) |
| 1856 | .map(IpAddress::getIp6Address) |
| 1857 | .findFirst() |
| 1858 | .orElse(null); |
| 1859 | } |
Kalhee Kim | 121ba92 | 2017-11-01 17:56:44 +0000 | [diff] [blame] | 1860 | return nextHopIp; |
| 1861 | } |
Kalhee Kim | ea4b6c2 | 2017-11-09 14:38:37 +0000 | [diff] [blame] | 1862 | |
Kalhee Kim | 495c9b2 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 1863 | private List<DhcpServerInfo> findValidServerInfo(boolean directConnFlag) { |
| 1864 | List<DhcpServerInfo> validServerInfo; |
| 1865 | |
| 1866 | if (directConnFlag || indirectServerInfoList.isEmpty()) { |
| 1867 | validServerInfo = new ArrayList<DhcpServerInfo>(defaultServerInfoList); |
| 1868 | } else { |
| 1869 | validServerInfo = new ArrayList<DhcpServerInfo>(indirectServerInfoList); |
| 1870 | } |
| 1871 | return validServerInfo; |
| 1872 | } |
| 1873 | |
| 1874 | private DhcpServerInfo findServerInfoFromServer(boolean directConnFlag, ConnectPoint inPort) { |
| 1875 | List<DhcpServerInfo> validServerInfoList = findValidServerInfo(directConnFlag); |
| 1876 | DhcpServerInfo foundServerInfo = null; |
| 1877 | for (DhcpServerInfo serverInfo : validServerInfoList) { |
| 1878 | if (inPort.equals(serverInfo.getDhcpServerConnectPoint().get())) { |
| 1879 | foundServerInfo = serverInfo; |
Charles Chan | 750c999 | 2018-02-26 10:44:49 -0800 | [diff] [blame] | 1880 | log.debug("ServerInfo found for Rcv port {} Server Connect Point {} for {}", |
Kalhee Kim | 495c9b2 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 1881 | inPort, serverInfo.getDhcpServerConnectPoint(), directConnFlag ? "direct" : "indirect"); |
| 1882 | break; |
Kalhee Kim | 495c9b2 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 1883 | } |
| 1884 | } |
| 1885 | return foundServerInfo; |
| 1886 | } |
Lior Assouline | a21c0ca | 2018-01-28 16:18:48 -0800 | [diff] [blame] | 1887 | |
Kalhee Kim | 495c9b2 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 1888 | /** |
| 1889 | * Set the dhcp6 lease expiry poll interval value. |
| 1890 | * |
| 1891 | * @param val poll interval value in seconds |
| 1892 | */ |
Kalhee Kim | 2f07c60 | 2017-11-15 18:57:53 +0000 | [diff] [blame] | 1893 | @Override |
Kalhee Kim | 495c9b2 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 1894 | public void setDhcp6PollInterval(int val) { |
| 1895 | dhcp6PollInterval = val; |
| 1896 | } |
| 1897 | |
Kalhee Kim | 2f07c60 | 2017-11-15 18:57:53 +0000 | [diff] [blame] | 1898 | /** |
| 1899 | * get the dhcp6 lease expiry poll interval value. |
| 1900 | * This is a private function |
| 1901 | * @return poll interval value in seconds |
| 1902 | */ |
| 1903 | private int getDhcp6PollInterval() { |
| 1904 | return dhcp6PollInterval; |
| 1905 | } |
| 1906 | |
| 1907 | /** |
| 1908 | * Find lease-expired ipaddresses and pd prefixes. |
| 1909 | * Removing host/route/fpm entries. |
| 1910 | */ |
| 1911 | public void timeTick() { |
| 1912 | long currentTime = System.currentTimeMillis(); |
| 1913 | Collection<DhcpRecord> records = dhcpRelayStore.getDhcpRecords(); |
| 1914 | |
| 1915 | log.debug("timeTick called currenttime {} records num {} ", currentTime, records.size()); |
| 1916 | |
| 1917 | records.forEach(record -> { |
| 1918 | boolean addrOrPdRemoved = false; |
| 1919 | DHCP6.MsgType ip6Status = record.ip6Status().orElse(null); |
| 1920 | if (ip6Status == null) { |
| 1921 | log.debug("record is not valid v6 record."); |
| 1922 | return; |
| 1923 | } |
| 1924 | |
| 1925 | if ((currentTime - record.getLastIp6Update()) > |
| 1926 | ((record.addrPrefTime() + getDhcp6PollInterval() / 2) * 1000)) { |
| 1927 | // remove ipaddress from host/route table |
| 1928 | IpAddress ip = record.ip6Address().orElse(null); |
| 1929 | if (ip != null) { |
| 1930 | if (record.directlyConnected()) { |
| 1931 | providerService.removeIpFromHost(HostId.hostId(record.macAddress(), |
| 1932 | record.vlanId()), ip); |
| 1933 | } else { |
| 1934 | MacAddress gwMac = record.nextHop().orElse(null); |
| 1935 | if (gwMac == null) { |
| 1936 | log.warn("Can't find gateway mac address from record {} for ip6Addr", record); |
| 1937 | return; |
| 1938 | } |
| 1939 | IpAddress nextHopIp = getFirstIpByHost(record.directlyConnected(), |
| 1940 | gwMac, |
| 1941 | record.vlanId()); |
Kalhee Kim | 54a97c9 | 2018-04-02 21:23:46 +0000 | [diff] [blame] | 1942 | Route route = new Route(Route.Source.DHCP, ip.toIpPrefix(), nextHopIp); |
Kalhee Kim | 2f07c60 | 2017-11-15 18:57:53 +0000 | [diff] [blame] | 1943 | routeStore.removeRoute(route); |
| 1944 | } |
| 1945 | record.updateAddrPrefTime(0); |
| 1946 | record.ip6Address(null); |
| 1947 | addrOrPdRemoved = true; |
| 1948 | dhcpRelayStore.updateDhcpRecord(HostId.hostId(record.macAddress(), |
| 1949 | record.vlanId()), record); |
| 1950 | log.warn("IP6 address is set to null. delta {} lastUpdate {} addrPrefTime {}", |
| 1951 | (currentTime - record.getLastIp6Update()), record.getLastIp6Update(), |
| 1952 | record.addrPrefTime()); |
| 1953 | } |
| 1954 | } |
| 1955 | if ((currentTime - record.getLastPdUpdate()) > |
| 1956 | ((record.pdPrefTime() + getDhcp6PollInterval() / 2) * 1000)) { |
| 1957 | // remove PD from route/fpm table |
| 1958 | IpPrefix pdIpPrefix = record.pdPrefix().orElse(null); |
| 1959 | if (pdIpPrefix != null) { |
| 1960 | if (record.directlyConnected()) { |
| 1961 | providerService.removeIpFromHost(HostId.hostId(record.macAddress(), record.vlanId()), |
| 1962 | pdIpPrefix.address().getIp6Address()); |
| 1963 | } else { |
| 1964 | MacAddress gwMac = record.nextHop().orElse(null); |
| 1965 | if (gwMac == null) { |
| 1966 | log.warn("Can't find gateway mac address from record {} for PD prefix", record); |
| 1967 | return; |
| 1968 | } |
| 1969 | IpAddress nextHopIp = getFirstIpByHost(record.directlyConnected(), |
| 1970 | gwMac, |
| 1971 | record.vlanId()); |
Kalhee Kim | 54a97c9 | 2018-04-02 21:23:46 +0000 | [diff] [blame] | 1972 | Route route = new Route(Route.Source.DHCP, pdIpPrefix, nextHopIp); |
Kalhee Kim | 2f07c60 | 2017-11-15 18:57:53 +0000 | [diff] [blame] | 1973 | routeStore.removeRoute(route); |
| 1974 | if (this.dhcpFpmEnabled) { |
| 1975 | dhcpFpmPrefixStore.removeFpmRecord(pdIpPrefix); |
| 1976 | } |
| 1977 | } |
| 1978 | record.updatePdPrefTime(0); |
| 1979 | record.pdPrefix(null); |
| 1980 | addrOrPdRemoved = true; |
| 1981 | dhcpRelayStore.updateDhcpRecord(HostId.hostId(record.macAddress(), |
| 1982 | record.vlanId()), record); |
| 1983 | log.warn("PD prefix is set to null.delta {} pdPrefTime {}", |
| 1984 | (currentTime - record.getLastPdUpdate()), record.pdPrefTime()); |
| 1985 | } |
| 1986 | } |
| 1987 | if (addrOrPdRemoved && |
| 1988 | !record.ip6Address().isPresent() && !record.pdPrefix().isPresent()) { |
| 1989 | log.warn("ip6Status {} IP6 address and IP6 PD both are null. Remove record.", ip6Status); |
| 1990 | dhcpRelayStore.removeDhcpRecord(HostId.hostId(record.macAddress(), record.vlanId())); |
| 1991 | } |
| 1992 | } |
| 1993 | ); |
| 1994 | } |
| 1995 | } |