blob: b47e1ed3b51eb2818b88ed6b3f8690312579ca26 [file] [log] [blame]
Yi Tseng51301292017-07-28 13:02:59 -07001/*
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 Tseng51301292017-07-28 13:02:59 -070017package org.onosproject.dhcprelay;
18
Yi Tseng525ff402017-10-23 19:39:39 -070019import com.google.common.collect.HashMultimap;
Yi Tseng919b2df2017-09-07 16:22:51 -070020import com.google.common.collect.Lists;
Yi Tseng525ff402017-10-23 19:39:39 -070021import com.google.common.collect.Multimap;
Charles Chan70fdd492018-03-07 17:36:06 -080022import com.google.common.collect.Multimaps;
Kalhee Kim45fede42017-09-05 19:05:06 +000023import com.google.common.collect.Sets;
24import com.google.common.collect.ImmutableSet;
Taras Lemkin96a0d342018-03-26 14:52:58 +000025import org.apache.felix.scr.annotations.Activate;
Yi Tseng51301292017-07-28 13:02:59 -070026import org.apache.felix.scr.annotations.Component;
Taras Lemkin96a0d342018-03-26 14:52:58 +000027import org.apache.felix.scr.annotations.Deactivate;
28import org.apache.felix.scr.annotations.Modified;
Yi Tseng51301292017-07-28 13:02:59 -070029import org.apache.felix.scr.annotations.Property;
Kalhee Kim45fede42017-09-05 19:05:06 +000030import org.apache.felix.scr.annotations.Reference;
31import org.apache.felix.scr.annotations.ReferenceCardinality;
Yi Tseng51301292017-07-28 13:02:59 -070032import org.apache.felix.scr.annotations.Service;
33import org.onlab.packet.BasePacket;
Kalhee Kim45fede42017-09-05 19:05:06 +000034import org.onlab.packet.DHCP6;
35import org.onlab.packet.IPv6;
36import org.onlab.packet.Ethernet;
37import org.onlab.packet.Ip6Address;
Yi Tseng51301292017-07-28 13:02:59 -070038import org.onlab.packet.IpAddress;
Kalhee Kim45fede42017-09-05 19:05:06 +000039import org.onlab.packet.IpPrefix;
Yi Tseng51301292017-07-28 13:02:59 -070040import org.onlab.packet.MacAddress;
Yi Tseng525ff402017-10-23 19:39:39 -070041import org.onlab.packet.TpPort;
Kalhee Kim45fede42017-09-05 19:05:06 +000042import org.onlab.packet.UDP;
Yi Tseng51301292017-07-28 13:02:59 -070043import org.onlab.packet.VlanId;
Lior Assoulinea21c0ca2018-01-28 16:18:48 -080044import org.onlab.packet.dhcp.Dhcp6ClientDataOption;
45import org.onlab.packet.dhcp.Dhcp6LeaseQueryOption;
Kalhee Kim45fede42017-09-05 19:05:06 +000046import org.onlab.packet.dhcp.Dhcp6RelayOption;
47import org.onlab.packet.dhcp.Dhcp6InterfaceIdOption;
Lior Assoulinea21c0ca2018-01-28 16:18:48 -080048import org.onlab.packet.dhcp.Dhcp6Option;
Kalhee Kim45fede42017-09-05 19:05:06 +000049import org.onlab.packet.dhcp.Dhcp6IaNaOption;
50import org.onlab.packet.dhcp.Dhcp6IaTaOption;
51import org.onlab.packet.dhcp.Dhcp6IaPdOption;
52import org.onlab.packet.dhcp.Dhcp6IaAddressOption;
53import org.onlab.packet.dhcp.Dhcp6IaPrefixOption;
Kalhee Kimea4b6c22017-11-09 14:38:37 +000054import org.onlab.packet.dhcp.Dhcp6ClientIdOption;
55import org.onlab.packet.dhcp.Dhcp6Duid;
Kalhee Kim495c9b22017-11-07 16:32:09 +000056import org.onlab.packet.DHCP6.MsgType;
Kalhee Kim45fede42017-09-05 19:05:06 +000057import org.onlab.util.HexString;
Taras Lemkin96a0d342018-03-26 14:52:58 +000058import org.onlab.util.Tools;
59import org.onosproject.cfg.ComponentConfigService;
Yi Tseng525ff402017-10-23 19:39:39 -070060import org.onosproject.core.ApplicationId;
61import org.onosproject.core.CoreService;
Yi Tseng51301292017-07-28 13:02:59 -070062import org.onosproject.dhcprelay.api.DhcpHandler;
Yi Tseng919b2df2017-09-07 16:22:51 -070063import org.onosproject.dhcprelay.api.DhcpServerInfo;
Yi Tseng525ff402017-10-23 19:39:39 -070064import org.onosproject.dhcprelay.config.IgnoreDhcpConfig;
Kalhee Kim45fede42017-09-05 19:05:06 +000065import org.onosproject.dhcprelay.store.DhcpRelayStore;
Kalhee Kimea4b6c22017-11-09 14:38:37 +000066import org.onosproject.dhcprelay.store.DhcpRecord;
Kalhee Kimba366062017-11-07 16:32:09 +000067import org.onosproject.dhcprelay.store.DhcpFpmPrefixStore;
Kalhee Kimd94ceea2017-11-29 19:03:02 +000068import org.onosproject.dhcprelay.store.DhcpRelayCounters;
69import org.onosproject.dhcprelay.store.DhcpRelayCountersStore;
Yi Tseng525ff402017-10-23 19:39:39 -070070import org.onosproject.net.Device;
71import org.onosproject.net.DeviceId;
Kalhee Kimba366062017-11-07 16:32:09 +000072import org.onosproject.routing.fpm.api.FpmRecord;
Yi Tseng525ff402017-10-23 19:39:39 -070073import org.onosproject.net.behaviour.Pipeliner;
74import org.onosproject.net.device.DeviceService;
75import org.onosproject.net.flow.DefaultTrafficSelector;
76import org.onosproject.net.flow.TrafficSelector;
77import org.onosproject.net.flowobjective.DefaultForwardingObjective;
78import org.onosproject.net.flowobjective.FlowObjectiveService;
79import org.onosproject.net.flowobjective.ForwardingObjective;
80import org.onosproject.net.flowobjective.Objective;
81import org.onosproject.net.flowobjective.ObjectiveContext;
82import org.onosproject.net.flowobjective.ObjectiveError;
Yi Tsengaa417a62017-09-08 17:22:51 -070083import org.onosproject.net.host.HostProvider;
84import org.onosproject.net.host.HostProviderRegistry;
85import org.onosproject.net.host.HostProviderService;
Kalhee Kim45fede42017-09-05 19:05:06 +000086import org.onosproject.net.host.HostService;
87import org.onosproject.net.host.DefaultHostDescription;
88import org.onosproject.net.host.HostDescription;
Kalhee Kim45fede42017-09-05 19:05:06 +000089import org.onosproject.net.host.HostListener;
90import org.onosproject.net.host.HostEvent;
91import org.onosproject.net.intf.Interface;
92import org.onosproject.net.intf.InterfaceService;
Yi Tseng525ff402017-10-23 19:39:39 -070093import org.onosproject.net.packet.PacketPriority;
Yi Tsengaa417a62017-09-08 17:22:51 -070094import org.onosproject.net.provider.ProviderId;
Kalhee Kim45fede42017-09-05 19:05:06 +000095import org.onosproject.routeservice.Route;
96import org.onosproject.routeservice.RouteStore;
Yi Tseng483ac6f2017-08-02 15:03:31 -070097import org.onosproject.dhcprelay.config.DhcpServerConfig;
Yi Tseng51301292017-07-28 13:02:59 -070098import org.onosproject.net.ConnectPoint;
Kalhee Kim45fede42017-09-05 19:05:06 +000099import org.onosproject.net.Host;
100import org.onosproject.net.HostId;
101import org.onosproject.net.HostLocation;
102import org.onosproject.net.packet.DefaultOutboundPacket;
103import org.onosproject.net.packet.OutboundPacket;
Yi Tseng51301292017-07-28 13:02:59 -0700104import org.onosproject.net.packet.PacketContext;
Kalhee Kim45fede42017-09-05 19:05:06 +0000105import org.onosproject.net.packet.PacketService;
Taras Lemkin96a0d342018-03-26 14:52:58 +0000106import org.osgi.service.component.ComponentContext;
Kalhee Kim45fede42017-09-05 19:05:06 +0000107import org.slf4j.Logger;
108import org.slf4j.LoggerFactory;
109import org.onosproject.net.flow.DefaultTrafficTreatment;
110import org.onosproject.net.flow.TrafficTreatment;
Kalhee Kim45fede42017-09-05 19:05:06 +0000111import java.nio.ByteBuffer;
Taras Lemkin96a0d342018-03-26 14:52:58 +0000112import java.util.ArrayList;
Yi Tseng483ac6f2017-08-02 15:03:31 -0700113import java.util.Collection;
Taras Lemkin96a0d342018-03-26 14:52:58 +0000114import java.util.Dictionary;
115import java.util.List;
Yi Tseng51301292017-07-28 13:02:59 -0700116import java.util.Optional;
Kalhee Kim45fede42017-09-05 19:05:06 +0000117import java.util.Set;
Charles Chan909cff82018-03-05 13:14:02 -0800118import java.util.concurrent.CopyOnWriteArrayList;
Jordan Halterman6328db72018-04-10 13:34:50 -0400119import java.util.concurrent.Executor;
Yi Tseng525ff402017-10-23 19:39:39 -0700120import java.util.concurrent.atomic.AtomicInteger;
Kalhee Kim45fede42017-09-05 19:05:06 +0000121import static com.google.common.base.Preconditions.checkNotNull;
122import static com.google.common.base.Preconditions.checkState;
Jordan Halterman6328db72018-04-10 13:34:50 -0400123import static java.util.concurrent.Executors.newSingleThreadExecutor;
124import static org.onlab.util.Tools.groupedThreads;
Yi Tseng525ff402017-10-23 19:39:39 -0700125import static org.onosproject.net.flowobjective.Objective.Operation.ADD;
126import static org.onosproject.net.flowobjective.Objective.Operation.REMOVE;
Kalhee Kimd94ceea2017-11-29 19:03:02 +0000127import java.util.concurrent.Semaphore;
Yi Tseng51301292017-07-28 13:02:59 -0700128
Lior Assoulinea21c0ca2018-01-28 16:18:48 -0800129
Yi Tseng51301292017-07-28 13:02:59 -0700130@Component
131@Service
132@Property(name = "version", value = "6")
Yi Tsengaa417a62017-09-08 17:22:51 -0700133public class Dhcp6HandlerImpl implements DhcpHandler, HostProvider {
Charles Chand988c282017-09-12 17:09:32 -0700134 public static final String DHCP_V6_RELAY_APP = "org.onosproject.Dhcp6HandlerImpl";
Saurav Das7c6dec12017-09-13 14:35:56 -0700135 public static final ProviderId PROVIDER_ID = new ProviderId("dhcp6", DHCP_V6_RELAY_APP);
Yi Tseng525ff402017-10-23 19:39:39 -0700136 private static final int IGNORE_CONTROL_PRIORITY = PacketPriority.CONTROL.priorityValue() + 1000;
Kalhee Kimd94ceea2017-11-29 19:03:02 +0000137 private String gCount = "global";
Taras Lemkin96a0d342018-03-26 14:52:58 +0000138 private static final String LQ_ROUTE_PROPERTY_NAME = "learnRouteFromLeasequery";
Yi Tseng525ff402017-10-23 19:39:39 -0700139 private static final TrafficSelector CLIENT_SERVER_SELECTOR = DefaultTrafficSelector.builder()
140 .matchEthType(Ethernet.TYPE_IPV6)
141 .matchIPProtocol(IPv6.PROTOCOL_UDP)
142 .matchIPv6Src(IpPrefix.IPV6_LINK_LOCAL_PREFIX)
143 .matchIPv6Dst(Ip6Address.ALL_DHCP_RELAY_AGENTS_AND_SERVERS.toIpPrefix())
144 .matchUdpSrc(TpPort.tpPort(UDP.DHCP_V6_CLIENT_PORT))
145 .matchUdpDst(TpPort.tpPort(UDP.DHCP_V6_SERVER_PORT))
146 .build();
147 private static final TrafficSelector SERVER_RELAY_SELECTOR = DefaultTrafficSelector.builder()
148 .matchEthType(Ethernet.TYPE_IPV6)
149 .matchIPProtocol(IPv6.PROTOCOL_UDP)
150 .matchUdpSrc(TpPort.tpPort(UDP.DHCP_V6_SERVER_PORT))
151 .matchUdpDst(TpPort.tpPort(UDP.DHCP_V6_SERVER_PORT))
152 .build();
Lior Assoulinea21c0ca2018-01-28 16:18:48 -0800153 // lease query reply is from server to client (no relay in between) - so we need to
154 // catch that scenario also ..
155 private static final TrafficSelector LEASE_QUERY_RESPONSE_SELECTOR = DefaultTrafficSelector.builder()
156 .matchEthType(Ethernet.TYPE_IPV6)
157 .matchIPProtocol(IPv6.PROTOCOL_UDP)
158 .matchUdpSrc(TpPort.tpPort(UDP.DHCP_V6_SERVER_PORT))
159 .matchUdpDst(TpPort.tpPort(UDP.DHCP_V6_CLIENT_PORT))
160 .build();
Yi Tseng525ff402017-10-23 19:39:39 -0700161 static final Set<TrafficSelector> DHCP_SELECTORS = ImmutableSet.of(
162 CLIENT_SERVER_SELECTOR,
Lior Assoulinea21c0ca2018-01-28 16:18:48 -0800163 SERVER_RELAY_SELECTOR,
164 LEASE_QUERY_RESPONSE_SELECTOR
Yi Tseng525ff402017-10-23 19:39:39 -0700165 );
Kalhee Kim45fede42017-09-05 19:05:06 +0000166 private static Logger log = LoggerFactory.getLogger(Dhcp6HandlerImpl.class);
Yi Tseng51301292017-07-28 13:02:59 -0700167
Kalhee Kim45fede42017-09-05 19:05:06 +0000168 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
169 protected DhcpRelayStore dhcpRelayStore;
Yi Tseng51301292017-07-28 13:02:59 -0700170
Kalhee Kim45fede42017-09-05 19:05:06 +0000171 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Kalhee Kimd94ceea2017-11-29 19:03:02 +0000172 protected DhcpRelayCountersStore dhcpRelayCountersStore;
173
174 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Kalhee Kim45fede42017-09-05 19:05:06 +0000175 protected PacketService packetService;
176
177 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Kalhee Kim45fede42017-09-05 19:05:06 +0000178 protected RouteStore routeStore;
179
180 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
181 protected InterfaceService interfaceService;
182
183 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
184 protected HostService hostService;
185
Yi Tsengaa417a62017-09-08 17:22:51 -0700186 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
187 protected HostProviderRegistry providerRegistry;
Kalhee Kim45fede42017-09-05 19:05:06 +0000188
Yi Tseng525ff402017-10-23 19:39:39 -0700189 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
190 protected CoreService coreService;
191
192 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Kalhee Kimba366062017-11-07 16:32:09 +0000193 protected DhcpFpmPrefixStore dhcpFpmPrefixStore;
194
195 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Yi Tseng525ff402017-10-23 19:39:39 -0700196 protected DeviceService deviceService;
197
198 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
199 protected FlowObjectiveService flowObjectiveService;
200
Taras Lemkin96a0d342018-03-26 14:52:58 +0000201 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
202 protected ComponentConfigService cfgService;
203
204 @Property(name = Dhcp6HandlerImpl.LQ_ROUTE_PROPERTY_NAME, boolValue = false,
205 label = "Enable learning routing information from LQ")
206 private Boolean learnRouteFromLeasequery = Boolean.TRUE;
207
Yi Tsengaa417a62017-09-08 17:22:51 -0700208 protected HostProviderService providerService;
Yi Tseng525ff402017-10-23 19:39:39 -0700209 protected ApplicationId appId;
Charles Chan70fdd492018-03-07 17:36:06 -0800210 protected Multimap<DeviceId, VlanId> ignoredVlans = Multimaps.synchronizedMultimap(HashMultimap.create());
Yi Tseng525ff402017-10-23 19:39:39 -0700211 private InternalHostListener hostListener = new InternalHostListener();
Kalhee Kimba366062017-11-07 16:32:09 +0000212 private Boolean dhcpFpmEnabled = false;
Charles Chan909cff82018-03-05 13:14:02 -0800213 private List<DhcpServerInfo> defaultServerInfoList = new CopyOnWriteArrayList<>();
214 private List<DhcpServerInfo> indirectServerInfoList = new CopyOnWriteArrayList<>();
Jordan Halterman6328db72018-04-10 13:34:50 -0400215
216 private Executor hostEventExecutor = newSingleThreadExecutor(
217 groupedThreads("dhcp6-event-host", "%d", log));
218
Kalhee Kim495c9b22017-11-07 16:32:09 +0000219 private class IpAddressInfo {
220 Ip6Address ip6Address;
221 long prefTime;
222 }
223 private class PdPrefixInfo {
224 IpPrefix pdPrefix;
225 long prefTime;
226 }
227 protected int dhcp6PollInterval = 24 * 3600; // 24 hr period
228
Kalhee Kimd94ceea2017-11-29 19:03:02 +0000229 // max 1 thread
230 static Semaphore recordSemaphore = new Semaphore(1);
Kalhee Kim45fede42017-09-05 19:05:06 +0000231
232 // CLIENT message types
233 public static final Set<Byte> MSG_TYPE_FROM_CLIENT =
234 ImmutableSet.of(DHCP6.MsgType.SOLICIT.value(),
235 DHCP6.MsgType.REQUEST.value(),
236 DHCP6.MsgType.REBIND.value(),
237 DHCP6.MsgType.RENEW.value(),
238 DHCP6.MsgType.RELEASE.value(),
239 DHCP6.MsgType.DECLINE.value(),
240 DHCP6.MsgType.CONFIRM.value(),
Lior Assoulinea21c0ca2018-01-28 16:18:48 -0800241 DHCP6.MsgType.RELAY_FORW.value(),
242 DHCP6.MsgType.LEASEQUERY.value());
Kalhee Kim45fede42017-09-05 19:05:06 +0000243 // SERVER message types
244 public static final Set<Byte> MSG_TYPE_FROM_SERVER =
Lior Assoulinea21c0ca2018-01-28 16:18:48 -0800245 ImmutableSet.of(DHCP6.MsgType.RELAY_REPL.value(),
246 DHCP6.MsgType.LEASEQUERY_REPLY.value());
Kalhee Kim45fede42017-09-05 19:05:06 +0000247
248 @Activate
Taras Lemkin96a0d342018-03-26 14:52:58 +0000249 protected void activate(ComponentContext context) {
250 cfgService.registerProperties(getClass());
251 modified(context);
Yi Tseng525ff402017-10-23 19:39:39 -0700252 appId = coreService.registerApplication(DHCP_V6_RELAY_APP);
Yi Tsengaa417a62017-09-08 17:22:51 -0700253 providerService = providerRegistry.register(this);
Kalhee Kim45fede42017-09-05 19:05:06 +0000254 hostService.addListener(hostListener);
Yi Tseng51301292017-07-28 13:02:59 -0700255 }
256
Kalhee Kim45fede42017-09-05 19:05:06 +0000257 @Deactivate
258 protected void deactivate() {
Taras Lemkin96a0d342018-03-26 14:52:58 +0000259 cfgService.unregisterProperties(getClass(), false);
Yi Tsengaa417a62017-09-08 17:22:51 -0700260 providerRegistry.unregister(this);
Kalhee Kim45fede42017-09-05 19:05:06 +0000261 hostService.removeListener(hostListener);
Yi Tseng919b2df2017-09-07 16:22:51 -0700262 defaultServerInfoList.forEach(this::stopMonitoringIps);
263 defaultServerInfoList.clear();
264 indirectServerInfoList.forEach(this::stopMonitoringIps);
265 indirectServerInfoList.clear();
266 }
Kalhee Kim45fede42017-09-05 19:05:06 +0000267
Taras Lemkin96a0d342018-03-26 14:52:58 +0000268 @Modified
269 protected void modified(ComponentContext context) {
270 Dictionary<?, ?> properties = context.getProperties();
271 Boolean flag;
272 flag = Tools.isPropertyEnabled(properties, Dhcp6HandlerImpl.LQ_ROUTE_PROPERTY_NAME);
273 if (flag != null) {
274 learnRouteFromLeasequery = flag;
275 log.info("Learning routes from DHCP leasequery is {}",
276 learnRouteFromLeasequery ? "enabled" : "disabled");
277 }
278 }
279
Yi Tseng919b2df2017-09-07 16:22:51 -0700280 private void stopMonitoringIps(DhcpServerInfo serverInfo) {
281 serverInfo.getDhcpGatewayIp6().ifPresent(gatewayIp -> {
282 hostService.stopMonitoringIp(gatewayIp);
283 });
284 serverInfo.getDhcpServerIp6().ifPresent(serverIp -> {
285 hostService.stopMonitoringIp(serverIp);
286 });
Yi Tseng51301292017-07-28 13:02:59 -0700287 }
288
Yi Tseng51301292017-07-28 13:02:59 -0700289 @Override
Yi Tseng919b2df2017-09-07 16:22:51 -0700290 public List<DhcpServerInfo> getDefaultDhcpServerInfoList() {
291 return defaultServerInfoList;
Yi Tseng51301292017-07-28 13:02:59 -0700292 }
293
294 @Override
Yi Tseng919b2df2017-09-07 16:22:51 -0700295 public List<DhcpServerInfo> getIndirectDhcpServerInfoList() {
296 return indirectServerInfoList;
Yi Tseng51301292017-07-28 13:02:59 -0700297 }
298
299 @Override
Yi Tseng525ff402017-10-23 19:39:39 -0700300 public void updateIgnoreVlanConfig(IgnoreDhcpConfig config) {
301 if (config == null) {
302 ignoredVlans.forEach(((deviceId, vlanId) -> {
303 processIgnoreVlanRule(deviceId, vlanId, REMOVE);
304 }));
305 return;
306 }
307 config.ignoredVlans().forEach((deviceId, vlanId) -> {
308 if (ignoredVlans.get(deviceId).contains(vlanId)) {
309 // don't need to process if it already ignored
310 return;
311 }
312 processIgnoreVlanRule(deviceId, vlanId, ADD);
313 });
Yi Tseng525ff402017-10-23 19:39:39 -0700314 ignoredVlans.forEach((deviceId, vlanId) -> {
315 if (!config.ignoredVlans().get(deviceId).contains(vlanId)) {
316 // not contains in new config, remove it
317 processIgnoreVlanRule(deviceId, vlanId, REMOVE);
318 }
319 });
320 }
321
322 @Override
Saurav Dasb805f1a2017-12-13 16:19:35 -0800323 public void removeIgnoreVlanState(IgnoreDhcpConfig config) {
324 if (config == null) {
325 ignoredVlans.clear();
326 return;
327 }
328 config.ignoredVlans().forEach((deviceId, vlanId) -> {
329 ignoredVlans.remove(deviceId, vlanId);
330 });
331 }
332
Lior Assoulinea21c0ca2018-01-28 16:18:48 -0800333 public DhcpRecord getDhcpRelayRecordFor(Ip6Address clientAddress) {
334
Taras Lemkin96a0d342018-03-26 14:52:58 +0000335 Collection<DhcpRecord> records = dhcpRelayStore.getDhcpRecords();
Lior Assoulinea21c0ca2018-01-28 16:18:48 -0800336 DhcpRecord dr = null;
337 for (DhcpRecord e:records) {
338 if (e.ip6Address().isPresent()) {
339 if (e.ip6Address().get().equals(clientAddress)) {
340 dr = e;
341 break;
342 }
343 }
344 }
345 return dr;
346 }
347
348 public MacAddress findNextHopMacForIp6FromRelayStore(Ip6Address clientAddress,
349 MacAddress clientMacAddress, VlanId vlanID) {
350
351 DhcpRecord dr = getDhcpRelayRecordFor(clientAddress);
352
353 if (dr != null) {
354 Optional<MacAddress> nextHopTempMac = dr.nextHopTemp();
355 if (nextHopTempMac.isPresent()) {
356 log.info("findNextHopForIp6FromRelayStore " + clientAddress + " got mac " + nextHopTempMac.toString());
357 return nextHopTempMac.get();
358 }
359 } else {
360 log.warn("findNextHopMacForIp6FromRelayStore could NOT find next hop for " + clientAddress);
361 return null;
362 }
363 return null;
364 }
365
366 public Ip6Address findNextHopIp6FromRelayStore(Ip6Address clientAddress) {
367
368 DhcpRecord dr = getDhcpRelayRecordFor(clientAddress);
369 if (dr != null) {
370 Optional<MacAddress> nextHopMac = dr.nextHop();
371 if (nextHopMac.isPresent()) {
372 // find the local ip6 from the host store
373 HostId gwHostId = HostId.hostId(nextHopMac.get(), dr.vlanId());
374 Host gwHost = hostService.getHost(gwHostId);
375 if (gwHost == null) {
376 log.warn("Can't find next hop host ID {}", gwHostId);
377 return null;
378 }
379 Ip6Address nextHopIp = gwHost.ipAddresses()
380 .stream()
381 .filter(IpAddress::isIp6)
382 .filter(IpAddress::isLinkLocal)
383 .map(IpAddress::getIp6Address)
384 .findFirst()
385 .orElse(null);
386
387 log.info("findNextHopIp6FromRelayStore " + clientAddress + " got mac " +
388 nextHopMac.toString() + " ip6 " + nextHopIp);
389 return nextHopIp;
390 }
391 } else {
392 log.warn("findNextHopIp6FromRelayStore could NOT find next hop for " + clientAddress);
393 return null;
394 }
395 return null;
396 }
397
398 private void setPotentialNextHopForIp6InRelayStore(Ip6Address clientAddress,
399 VlanId vlanId, MacAddress nh) {
400 DhcpRecord dr = getDhcpRelayRecordFor(clientAddress);
401 if (dr != null) {
402 dr.nextHopTemp(nh);
403 log.debug("LQ6 potential NH mac " + nh.toString() + " UPDATED in RelayRecord client " + clientAddress);
404 } else {
405 log.warn("LQ6 potential NH mac" + nh.toString() +
406 " NOT FOUND in RelayRecord for client - LQ rejected" + clientAddress);
407 }
408 }
409
410 public void handleLeaseQuery6ReplyMsg(PacketContext context, DHCP6 dhcp6Payload) {
411 ConnectPoint inPort = context.inPacket().receivedFrom();
412 log.info("Got LQV6-REPLY on port {}", inPort);
413 List<Dhcp6Option> lopt = dhcp6Payload.getOptions();
414 log.info("Options list: {}", lopt);
415 // find out if this lease is known is
416 Dhcp6ClientDataOption clientDataOption = dhcp6Payload.getOptions()
417 .stream()
418 .filter(opt -> opt instanceof Dhcp6ClientDataOption)
419 .map(pld -> (Dhcp6ClientDataOption) pld)
420 .findFirst()
421 .orElse(null);
422
423 if (clientDataOption == null) {
424 log.warn("clientDataOption option is not present, " +
425 "lease is UNKNOWN - not adding any new route...");
426 } else {
427 Dhcp6IaAddressOption aiAddressOption = clientDataOption.getOptions()
428 .stream()
429 .filter(opt -> opt instanceof Dhcp6IaAddressOption)
430 .map(pld -> (Dhcp6IaAddressOption) pld)
431 .findFirst()
432 .orElse(null);
433
434 Dhcp6ClientIdOption clientIdOption = clientDataOption.getOptions()
435 .stream()
436 .filter(opt -> opt instanceof Dhcp6ClientIdOption)
437 .map(pld -> (Dhcp6ClientIdOption) pld)
438 .findFirst()
439 .orElse(null);
440
441 if (aiAddressOption == null) {
442 log.warn("clientDataOption from DHCP server does not " +
443 "contains Dhcp6IaAddressOption for the client - giving up...");
444 } else {
445 Ip6Address clientAddress = aiAddressOption.getIp6Address();
446 MacAddress clientMacAddress = MacAddress.valueOf(clientIdOption.getDuid().getLinkLayerAddress());
447 Ethernet packet = context.inPacket().parsed();
448 VlanId vlanId = VlanId.vlanId(packet.getVlanID());
449 MacAddress potentialNextHopMac =
450 findNextHopMacForIp6FromRelayStore(clientAddress, clientMacAddress, vlanId);
451
452 if (potentialNextHopMac == null) {
453 log.warn("Can't find next hop host mac for client {} mac:{}/{}",
454 clientAddress, clientMacAddress, vlanId);
455 return;
456 } else {
457 log.info("Next hop mac for {}/{}/{} is {}", clientAddress,
458 clientMacAddress, vlanId, potentialNextHopMac.toString());
459 }
460 // search the next hop in the hosts store
461 HostId gwHostId = HostId.hostId(potentialNextHopMac, vlanId);
462 Host gwHost = hostService.getHost(gwHostId);
463 if (gwHost == null) {
464 log.warn("Can't find next hop host ID {}", gwHostId);
465 return;
466 }
467 Ip6Address nextHopIp = gwHost.ipAddresses()
468 .stream()
469 .filter(IpAddress::isIp6)
470 .filter(IpAddress::isLinkLocal)
471 .map(IpAddress::getIp6Address)
472 .findFirst()
473 .orElse(null);
474 if (nextHopIp == null) {
475 log.warn("Can't find IP6 address of next hop {}", gwHost);
476 return;
477 }
478 log.info("client " + clientAddress + " is known !");
Kalhee Kim54a97c92018-04-02 21:23:46 +0000479 Route routeForIP6 = new Route(Route.Source.DHCP, clientAddress.toIpPrefix(), nextHopIp);
Lior Assoulinea21c0ca2018-01-28 16:18:48 -0800480 log.debug("updating route of Client for indirectly connected.");
481 log.debug("client ip: " + clientAddress + ", next hop ip6: " + nextHopIp);
482 routeStore.updateRoute(routeForIP6);
483 }
484 }
485 }
486
Saurav Dasb805f1a2017-12-13 16:19:35 -0800487 @Override
Kalhee Kim45fede42017-09-05 19:05:06 +0000488 public void processDhcpPacket(PacketContext context, BasePacket payload) {
489 checkNotNull(payload, "DHCP6 payload can't be null");
490 checkState(payload instanceof DHCP6, "Payload is not a DHCP6");
491 DHCP6 dhcp6Payload = (DHCP6) payload;
492 Ethernet receivedPacket = context.inPacket().parsed();
493
494 if (!configured()) {
Lior Assoulinea21c0ca2018-01-28 16:18:48 -0800495 log.warn("Missing DHCP6 relay server config. " +
496 "Abort packet processing dhcp6 payload {}", dhcp6Payload);
Kalhee Kim45fede42017-09-05 19:05:06 +0000497 return;
498 }
Kalhee Kim495c9b22017-11-07 16:32:09 +0000499 byte msgTypeVal = dhcp6Payload.getMsgType();
500 MsgType msgType = DHCP6.MsgType.getType(msgTypeVal);
501 log.debug("msgType is {}", msgType);
Kalhee Kim45fede42017-09-05 19:05:06 +0000502
Kalhee Kim45fede42017-09-05 19:05:06 +0000503 ConnectPoint inPort = context.inPacket().receivedFrom();
Jonathan Hart8ca2bc02017-11-30 18:23:42 -0800504
Kalhee Kim495c9b22017-11-07 16:32:09 +0000505 if (inPort == null) {
Taras Lemkin96a0d342018-03-26 14:52:58 +0000506 log.warn("incoming ConnectPoint is null");
Kalhee Kim495c9b22017-11-07 16:32:09 +0000507 }
Kalhee Kim45fede42017-09-05 19:05:06 +0000508 Set<Interface> receivingInterfaces = interfaceService.getInterfacesByPort(inPort);
509 //ignore the packets if dhcp client interface is not configured on onos.
510 if (receivingInterfaces.isEmpty()) {
511 log.warn("Virtual interface is not configured on {}", inPort);
512 return;
513 }
514
Lior Assoulinea21c0ca2018-01-28 16:18:48 -0800515 if (msgTypeVal == DHCP6.MsgType.LEASEQUERY.value()) {
Taras Lemkin96a0d342018-03-26 14:52:58 +0000516 List<InternalPacket> ethernetClientPackets =
517 learnRouteFromLeasequery ?
518 processLQ6PacketFromClient(context, receivedPacket, receivingInterfaces, dhcp6Payload) :
519 processDhcp6ForwardOnly(context, receivedPacket, receivingInterfaces, dhcp6Payload);
520 for (InternalPacket internalPacket : ethernetClientPackets) {
Kalhee Kim495c9b22017-11-07 16:32:09 +0000521 forwardPacket(internalPacket);
Kalhee Kim45fede42017-09-05 19:05:06 +0000522 }
Taras Lemkin96a0d342018-03-26 14:52:58 +0000523 } else if (msgTypeVal == DHCP6.MsgType.LEASEQUERY_REPLY.value() && learnRouteFromLeasequery) {
Lior Assoulinea21c0ca2018-01-28 16:18:48 -0800524 IPv6 clientIpv6 = (IPv6) receivedPacket.getPayload();
525 UDP clientUdp = (UDP) clientIpv6.getPayload();
526 DHCP6 clientDhcp6 = (DHCP6) clientUdp.getPayload();
Taras Lemkin96a0d342018-03-26 14:52:58 +0000527 Interface serverInterface = Dhcp6HandlerUtil.directlyConnected(clientDhcp6) ?
Lior Assoulinea21c0ca2018-01-28 16:18:48 -0800528 getServerInterface() : getIndirectServerInterface();
Kalhee Kim45fede42017-09-05 19:05:06 +0000529 InternalPacket ethernetPacketReply =
Taras Lemkin96a0d342018-03-26 14:52:58 +0000530 Dhcp6HandlerUtil.processLQ6PacketFromServer(
Lior Assoulinea21c0ca2018-01-28 16:18:48 -0800531 defaultServerInfoList, indirectServerInfoList,
532 serverInterface, interfaceService,
533 hostService,
534 context, receivedPacket, receivingInterfaces);
Kalhee Kim45fede42017-09-05 19:05:06 +0000535 if (ethernetPacketReply != null) {
536 forwardPacket(ethernetPacketReply);
537 }
Lior Assoulinea21c0ca2018-01-28 16:18:48 -0800538 handleLeaseQuery6ReplyMsg(context, dhcp6Payload);
Taras Lemkin96a0d342018-03-26 14:52:58 +0000539 } else if (MSG_TYPE_FROM_CLIENT.contains(msgTypeVal)) {
540 List<InternalPacket> ethernetClientPacket =
541 processDhcp6PacketFromClient(context, receivedPacket, receivingInterfaces);
542 for (InternalPacket internalPacket : ethernetClientPacket) {
543 forwardPacket(internalPacket);
Lior Assoulinea21c0ca2018-01-28 16:18:48 -0800544 }
Taras Lemkin96a0d342018-03-26 14:52:58 +0000545 } else if (MSG_TYPE_FROM_SERVER.contains(msgTypeVal)) {
546 log.debug("calling processDhcp6PacketFromServer with RELAY_REPL {}, {}", receivedPacket, dhcp6Payload);
547 InternalPacket ethernetPacketReply =
548 processDhcp6PacketFromServer(context, receivedPacket, receivingInterfaces);
549 if (ethernetPacketReply != null) {
550 forwardPacket(ethernetPacketReply);
551 }
552 } else {
553 log.warn("Not so fast, packet type {} not supported yet", msgTypeVal);
Kalhee Kim45fede42017-09-05 19:05:06 +0000554 }
555 }
556
Kalhee Kim45fede42017-09-05 19:05:06 +0000557 /**
558 * Checks if this app has been configured.
559 *
560 * @return true if all information we need have been initialized
561 */
562 public boolean configured() {
Yi Tseng919b2df2017-09-07 16:22:51 -0700563 return !defaultServerInfoList.isEmpty();
Kalhee Kim45fede42017-09-05 19:05:06 +0000564 }
565
Yi Tsengaa417a62017-09-08 17:22:51 -0700566 @Override
567 public ProviderId id() {
Charles Chand988c282017-09-12 17:09:32 -0700568 return PROVIDER_ID;
Yi Tsengaa417a62017-09-08 17:22:51 -0700569 }
570
571 @Override
572 public void triggerProbe(Host host) {
573 // Do nothing here
574 }
575
Kalhee Kim45fede42017-09-05 19:05:06 +0000576 //forward the packet to ConnectPoint where the DHCP server is attached.
577 private void forwardPacket(InternalPacket packet) {
578 //send Packetout to dhcp server connectpoint.
Taras Lemkin96a0d342018-03-26 14:52:58 +0000579 if (packet.getDestLocation() != null) {
Kalhee Kim45fede42017-09-05 19:05:06 +0000580 TrafficTreatment t = DefaultTrafficTreatment.builder()
Taras Lemkin96a0d342018-03-26 14:52:58 +0000581 .setOutput(packet.getDestLocation().port()).build();
Kalhee Kim45fede42017-09-05 19:05:06 +0000582 OutboundPacket o = new DefaultOutboundPacket(
Taras Lemkin96a0d342018-03-26 14:52:58 +0000583 packet.getDestLocation().deviceId(), t, ByteBuffer.wrap(packet.getPacket().serialize()));
Kalhee Kim45fede42017-09-05 19:05:06 +0000584 packetService.emit(o);
Taras Lemkin96a0d342018-03-26 14:52:58 +0000585 if (log.isTraceEnabled()) {
586 IPv6 ip6 = (IPv6) packet.getPacket().getPayload();
587 UDP udp = (UDP) ip6.getPayload();
588 DHCP6 dhcp6 = (DHCP6) udp.getPayload();
589 log.trace("Relaying packet to destination {} eth: {} dhcp: {}",
590 packet.getDestLocation(), packet.getPacket(), dhcp6);
591 }
592
593 }
Kalhee Kim45fede42017-09-05 19:05:06 +0000594 }
595
Kalhee Kim45fede42017-09-05 19:05:06 +0000596 /**
597 * extract from dhcp6 packet client ipv6 address of given by dhcp server.
598 *
599 * @param dhcp6 the dhcp6 packet
Kalhee Kim495c9b22017-11-07 16:32:09 +0000600 * @return IpAddressInfo IpAddressInfo given by dhcp server, or null if not exists
Kalhee Kim45fede42017-09-05 19:05:06 +0000601 */
Kalhee Kim495c9b22017-11-07 16:32:09 +0000602 private IpAddressInfo extractIpAddress(DHCP6 dhcp6) {
603 IpAddressInfo ipInfo = new IpAddressInfo();
Kalhee Kim45fede42017-09-05 19:05:06 +0000604
605 log.debug("extractIpAddress enters dhcp6 {}.", dhcp6);
606 // Extract IPv6 address from IA NA ot IA TA option
607 Optional<Dhcp6IaNaOption> iaNaOption = dhcp6.getOptions()
608 .stream()
609 .filter(opt -> opt instanceof Dhcp6IaNaOption)
610 .map(opt -> (Dhcp6IaNaOption) opt)
611 .findFirst();
612 Optional<Dhcp6IaTaOption> iaTaOption = dhcp6.getOptions()
613 .stream()
614 .filter(opt -> opt instanceof Dhcp6IaTaOption)
615 .map(opt -> (Dhcp6IaTaOption) opt)
616 .findFirst();
617 Optional<Dhcp6IaAddressOption> iaAddressOption;
618 if (iaNaOption.isPresent()) {
619 log.debug("Found IPv6 address from iaNaOption {}", iaNaOption);
620
621 iaAddressOption = iaNaOption.get().getOptions().stream()
622 .filter(opt -> opt instanceof Dhcp6IaAddressOption)
623 .map(opt -> (Dhcp6IaAddressOption) opt)
624 .findFirst();
625 } else if (iaTaOption.isPresent()) {
626 log.debug("Found IPv6 address from iaTaOption {}", iaTaOption);
627
628 iaAddressOption = iaTaOption.get().getOptions().stream()
629 .filter(opt -> opt instanceof Dhcp6IaAddressOption)
630 .map(opt -> (Dhcp6IaAddressOption) opt)
631 .findFirst();
632 } else {
Kalhee Kim495c9b22017-11-07 16:32:09 +0000633 log.info("No IPv6 address found from iaTaOption {}", iaTaOption);
Kalhee Kim45fede42017-09-05 19:05:06 +0000634 iaAddressOption = Optional.empty();
635 }
636 if (iaAddressOption.isPresent()) {
Kalhee Kim495c9b22017-11-07 16:32:09 +0000637 ipInfo.ip6Address = iaAddressOption.get().getIp6Address();
638 ipInfo.prefTime = iaAddressOption.get().getPreferredLifetime();
Kalhee Kim45fede42017-09-05 19:05:06 +0000639 log.debug("Found IPv6 address from iaAddressOption {}", iaAddressOption);
Kalhee Kim45fede42017-09-05 19:05:06 +0000640 } else {
641 log.debug("Can't find IPv6 address from DHCPv6 {}", dhcp6);
Kalhee Kim495c9b22017-11-07 16:32:09 +0000642 return null;
Kalhee Kim45fede42017-09-05 19:05:06 +0000643 }
Kalhee Kim495c9b22017-11-07 16:32:09 +0000644 return ipInfo;
Kalhee Kim45fede42017-09-05 19:05:06 +0000645 }
Lior Assoulinea21c0ca2018-01-28 16:18:48 -0800646
Kalhee Kim45fede42017-09-05 19:05:06 +0000647 /**
648 * extract from dhcp6 packet Prefix prefix provided by dhcp server.
649 *
650 * @param dhcp6 the dhcp6 payload
651 * @return IpPrefix Prefix Delegation prefix, or null if not exists.
652 */
Kalhee Kim495c9b22017-11-07 16:32:09 +0000653 private PdPrefixInfo extractPrefix(DHCP6 dhcp6) {
654 log.debug("extractPrefix enters {}", dhcp6);
Kalhee Kim45fede42017-09-05 19:05:06 +0000655
656 // extract prefix
Kalhee Kim495c9b22017-11-07 16:32:09 +0000657 PdPrefixInfo pdPrefixInfo = new PdPrefixInfo();
Kalhee Kim45fede42017-09-05 19:05:06 +0000658
659 Ip6Address prefixAddress = null;
660
661 // Extract IPv6 prefix from IA PD option
662 Optional<Dhcp6IaPdOption> iaPdOption = dhcp6.getOptions()
663 .stream()
664 .filter(opt -> opt instanceof Dhcp6IaPdOption)
665 .map(opt -> (Dhcp6IaPdOption) opt)
666 .findFirst();
667
668 Optional<Dhcp6IaPrefixOption> iaPrefixOption;
669 if (iaPdOption.isPresent()) {
Kalhee Kim495c9b22017-11-07 16:32:09 +0000670 log.debug("IA_PD option found {}", iaPdOption);
Kalhee Kim45fede42017-09-05 19:05:06 +0000671
672 iaPrefixOption = iaPdOption.get().getOptions().stream()
673 .filter(opt -> opt instanceof Dhcp6IaPrefixOption)
674 .map(opt -> (Dhcp6IaPrefixOption) opt)
675 .findFirst();
676 } else {
Kalhee Kim495c9b22017-11-07 16:32:09 +0000677 log.debug("IA_PD option NOT found");
Kalhee Kim45fede42017-09-05 19:05:06 +0000678
679 iaPrefixOption = Optional.empty();
680 }
681 if (iaPrefixOption.isPresent()) {
Kalhee Kim495c9b22017-11-07 16:32:09 +0000682 log.debug("IAPrefix Option within IA_PD option found {}", iaPrefixOption);
Kalhee Kim45fede42017-09-05 19:05:06 +0000683
684 prefixAddress = iaPrefixOption.get().getIp6Prefix();
685 int prefixLen = (int) iaPrefixOption.get().getPrefixLength();
Kalhee Kim495c9b22017-11-07 16:32:09 +0000686 log.debug("Prefix length is {} bits", prefixLen);
687 pdPrefixInfo.pdPrefix = IpPrefix.valueOf(prefixAddress, prefixLen);
688 pdPrefixInfo.prefTime = iaPrefixOption.get().getPreferredLifetime();
Kalhee Kim45fede42017-09-05 19:05:06 +0000689 } else {
Kalhee Kim495c9b22017-11-07 16:32:09 +0000690 log.debug("Can't find IPv6 prefix from DHCPv6 {}", dhcp6);
691 return null;
Kalhee Kim45fede42017-09-05 19:05:06 +0000692 }
Kalhee Kim495c9b22017-11-07 16:32:09 +0000693 return pdPrefixInfo;
Kalhee Kim45fede42017-09-05 19:05:06 +0000694 }
695
696 /**
Kalhee Kimea4b6c22017-11-09 14:38:37 +0000697 * extract from dhcp6 packet ClientIdOption.
698 *
699 * @param directConnFlag directly connected host
700 * @param dhcp6Payload the dhcp6 payload
701 * @return Dhcp6ClientIdOption clientIdOption, or null if not exists.
702 */
703 private Dhcp6ClientIdOption extractClinedId(Boolean directConnFlag, DHCP6 dhcp6Payload) {
704 Dhcp6ClientIdOption clientIdOption;
705
706 if (directConnFlag) {
707 clientIdOption = dhcp6Payload.getOptions()
708 .stream()
709 .filter(opt -> opt instanceof Dhcp6ClientIdOption)
710 .map(opt -> (Dhcp6ClientIdOption) opt)
711 .findFirst()
712 .orElse(null);
713 } else {
Taras Lemkin96a0d342018-03-26 14:52:58 +0000714 DHCP6 leafDhcp = Dhcp6HandlerUtil.getDhcp6Leaf(dhcp6Payload);
Kalhee Kimea4b6c22017-11-09 14:38:37 +0000715 clientIdOption = leafDhcp.getOptions()
716 .stream()
717 .filter(opt -> opt instanceof Dhcp6ClientIdOption)
718 .map(opt -> (Dhcp6ClientIdOption) opt)
719 .findFirst()
720 .orElse(null);
721 }
722
723 return clientIdOption;
724 }
Lior Assoulinea21c0ca2018-01-28 16:18:48 -0800725
Kalhee Kimea4b6c22017-11-09 14:38:37 +0000726 /**
Kalhee Kim495c9b22017-11-07 16:32:09 +0000727 * remove host or route and update dhcp relay record attributes.
Kalhee Kim45fede42017-09-05 19:05:06 +0000728 *
729 * @param directConnFlag flag to show that packet is from directly connected client
Kalhee Kim495c9b22017-11-07 16:32:09 +0000730 * @param location client side connect point
Kalhee Kim45fede42017-09-05 19:05:06 +0000731 * @param dhcp6Packet the dhcp6 payload
732 * @param clientPacket client's ethernet packet
733 * @param clientIpv6 client's Ipv6 packet
Kalhee Kim0c0cb0b2017-09-15 17:43:27 +0000734 * @param clientInterface client interfaces
Kalhee Kim45fede42017-09-05 19:05:06 +0000735 */
Kalhee Kimea4b6c22017-11-09 14:38:37 +0000736 private void removeHostOrRoute(boolean directConnFlag, ConnectPoint location,
737 DHCP6 dhcp6Packet,
Kalhee Kim45fede42017-09-05 19:05:06 +0000738 Ethernet clientPacket, IPv6 clientIpv6,
Kalhee Kim0c0cb0b2017-09-15 17:43:27 +0000739 Interface clientInterface) {
Kalhee Kim495c9b22017-11-07 16:32:09 +0000740 log.debug("removeHostOrRoute enters {}", dhcp6Packet);
Kalhee Kim121ba922017-11-01 17:56:44 +0000741 VlanId vlanId = clientInterface.vlan();
Kalhee Kim495c9b22017-11-07 16:32:09 +0000742 MacAddress srcMac = clientPacket.getSourceMAC(); // could be gw or host
Kalhee Kimea4b6c22017-11-09 14:38:37 +0000743 MacAddress leafClientMac;
744 byte leafMsgType;
Kalhee Kim495c9b22017-11-07 16:32:09 +0000745 log.debug("client mac {} client vlan {}", HexString.toHexString(srcMac.toBytes(), ":"), vlanId);
Kalhee Kimea4b6c22017-11-09 14:38:37 +0000746
747 Dhcp6ClientIdOption clientIdOption = extractClinedId(directConnFlag, dhcp6Packet);
748 if (clientIdOption != null) {
Kalhee Kimea4b6c22017-11-09 14:38:37 +0000749 if ((clientIdOption.getDuid().getDuidType() == Dhcp6Duid.DuidType.DUID_LLT) ||
750 (clientIdOption.getDuid().getDuidType() == Dhcp6Duid.DuidType.DUID_LL)) {
751 leafClientMac = MacAddress.valueOf(clientIdOption.getDuid().getLinkLayerAddress());
752 } else {
753 log.warn("Link-Layer Address not supported in CLIENTID option. No DhcpRelay Record created.");
Kalhee Kimd94ceea2017-11-29 19:03:02 +0000754 dhcpRelayCountersStore.incrementCounter(gCount, DhcpRelayCounters.NO_LINKLOCAL_FAIL);
Kalhee Kimea4b6c22017-11-09 14:38:37 +0000755 return;
756 }
Kalhee Kimea4b6c22017-11-09 14:38:37 +0000757 } else {
Kalhee Kim495c9b22017-11-07 16:32:09 +0000758 log.warn("CLIENTID option NOT found. Don't create DhcpRelay Record.");
Kalhee Kimd94ceea2017-11-29 19:03:02 +0000759 dhcpRelayCountersStore.incrementCounter(gCount, DhcpRelayCounters.NO_CLIENTID_FAIL);
Kalhee Kimea4b6c22017-11-09 14:38:37 +0000760 return;
761 }
762
Kalhee Kim495c9b22017-11-07 16:32:09 +0000763 HostId leafHostId = HostId.hostId(leafClientMac, vlanId);
764 DhcpRecord record = dhcpRelayStore.getDhcpRecord(leafHostId).orElse(null);
Kalhee Kimea4b6c22017-11-09 14:38:37 +0000765 if (record == null) {
Kalhee Kim495c9b22017-11-07 16:32:09 +0000766 record = new DhcpRecord(leafHostId);
Kalhee Kimd94ceea2017-11-29 19:03:02 +0000767 } else {
768 record = record.clone();
Kalhee Kimea4b6c22017-11-09 14:38:37 +0000769 }
Kalhee Kimea4b6c22017-11-09 14:38:37 +0000770
Taras Lemkin96a0d342018-03-26 14:52:58 +0000771 Boolean isMsgRelease = Dhcp6HandlerUtil.isDhcp6Release(dhcp6Packet);
Kalhee Kim495c9b22017-11-07 16:32:09 +0000772 IpAddressInfo ipInfo;
773 PdPrefixInfo pdInfo = null;
Kalhee Kimea4b6c22017-11-09 14:38:37 +0000774 if (directConnFlag) {
Kalhee Kim495c9b22017-11-07 16:32:09 +0000775 // Add to host store if it is connected to network directly
776 ipInfo = extractIpAddress(dhcp6Packet);
777 if (ipInfo != null) {
778 if (isMsgRelease) {
779 HostId hostId = HostId.hostId(srcMac, vlanId);
780 log.debug("remove Host {} ip for directly connected.", hostId.toString());
781 providerService.removeIpFromHost(hostId, ipInfo.ip6Address);
Kalhee Kim45fede42017-09-05 19:05:06 +0000782 }
783 } else {
Kalhee Kim495c9b22017-11-07 16:32:09 +0000784 log.debug("ipAddress not found. Do not remove Host {} for directly connected.",
785 HostId.hostId(srcMac, vlanId).toString());
Kalhee Kimea4b6c22017-11-09 14:38:37 +0000786 }
Kalhee Kim495c9b22017-11-07 16:32:09 +0000787 leafMsgType = dhcp6Packet.getMsgType();
Kalhee Kimea4b6c22017-11-09 14:38:37 +0000788 } else {
Kalhee Kim495c9b22017-11-07 16:32:09 +0000789 // Remove from route store if it is not connected to network directly
Kalhee Kimea4b6c22017-11-09 14:38:37 +0000790 // pick out the first link-local ip address
Kalhee Kim495c9b22017-11-07 16:32:09 +0000791 IpAddress nextHopIp = getFirstIpByHost(directConnFlag, srcMac, vlanId);
Kalhee Kimea4b6c22017-11-09 14:38:37 +0000792 if (nextHopIp == null) {
Kalhee Kim495c9b22017-11-07 16:32:09 +0000793 log.warn("Can't find link-local IP address of gateway mac {} vlanId {}", srcMac, vlanId);
Kalhee Kimd94ceea2017-11-29 19:03:02 +0000794 dhcpRelayCountersStore.incrementCounter(gCount, DhcpRelayCounters.NO_LINKLOCAL_GW);
Kalhee Kimea4b6c22017-11-09 14:38:37 +0000795 return;
796 }
Kalhee Kim45fede42017-09-05 19:05:06 +0000797
Taras Lemkin96a0d342018-03-26 14:52:58 +0000798 DHCP6 leafDhcp = Dhcp6HandlerUtil.getDhcp6Leaf(dhcp6Packet);
Kalhee Kim495c9b22017-11-07 16:32:09 +0000799 ipInfo = extractIpAddress(leafDhcp);
800 if (ipInfo == null) {
801 log.debug("ip is null");
Kalhee Kimea4b6c22017-11-09 14:38:37 +0000802 } else {
Kalhee Kim495c9b22017-11-07 16:32:09 +0000803 if (isMsgRelease) {
Kalhee Kim54a97c92018-04-02 21:23:46 +0000804 Route routeForIP = new Route(Route.Source.DHCP, ipInfo.ip6Address.toIpPrefix(), nextHopIp);
Kalhee Kim495c9b22017-11-07 16:32:09 +0000805 log.debug("removing route of 128 address for indirectly connected.");
806 log.debug("128 ip {}, nexthop {}",
807 HexString.toHexString(ipInfo.ip6Address.toOctets(), ":"),
808 HexString.toHexString(nextHopIp.toOctets(), ":"));
809 routeStore.removeRoute(routeForIP);
Kalhee Kim45fede42017-09-05 19:05:06 +0000810 }
Kalhee Kimea4b6c22017-11-09 14:38:37 +0000811 }
Kalhee Kim45fede42017-09-05 19:05:06 +0000812
Kalhee Kim495c9b22017-11-07 16:32:09 +0000813 pdInfo = extractPrefix(leafDhcp);
814 if (pdInfo == null) {
815 log.debug("ipPrefix is null ");
Kalhee Kimea4b6c22017-11-09 14:38:37 +0000816 } else {
Kalhee Kim495c9b22017-11-07 16:32:09 +0000817 if (isMsgRelease) {
Kalhee Kim54a97c92018-04-02 21:23:46 +0000818 Route routeForPrefix = new Route(Route.Source.DHCP, pdInfo.pdPrefix, nextHopIp);
Kalhee Kim495c9b22017-11-07 16:32:09 +0000819 log.debug("removing route of PD for indirectly connected.");
820 log.debug("pd ip {}, nexthop {}",
821 HexString.toHexString(pdInfo.pdPrefix.address().toOctets(), ":"),
822 HexString.toHexString(nextHopIp.toOctets(), ":"));
823
824 routeStore.removeRoute(routeForPrefix);
Kalhee Kimba366062017-11-07 16:32:09 +0000825 if (this.dhcpFpmEnabled) {
Kalhee Kim495c9b22017-11-07 16:32:09 +0000826 dhcpFpmPrefixStore.removeFpmRecord(pdInfo.pdPrefix);
Kalhee Kimba366062017-11-07 16:32:09 +0000827 }
Kalhee Kim45fede42017-09-05 19:05:06 +0000828 }
829 }
Kalhee Kimea4b6c22017-11-09 14:38:37 +0000830 leafMsgType = leafDhcp.getMsgType();
Kalhee Kimd94ceea2017-11-29 19:03:02 +0000831 }
Kalhee Kimea4b6c22017-11-09 14:38:37 +0000832
Kalhee Kim495c9b22017-11-07 16:32:09 +0000833 if (isMsgRelease) {
834 log.debug("DHCP6 RELEASE msg.");
835 if (record != null) {
836 if (ipInfo != null) {
837 log.debug("DhcpRelay Record ip6Address is set to null.");
838 record.ip6Address(null);
839 }
840 if (pdInfo != null) {
841 log.debug("DhcpRelay Record pdPrefix is set to null.");
842 }
843
844 if (!record.ip6Address().isPresent() && !record.pdPrefix().isPresent()) {
845 log.warn("IP6 address and IP6 PD both are null. Remove record.");
846 // do not remove a record. Let timer task handler it.
847 //dhcpRelayStore.removeDhcpRecord(HostId.hostId(leafClientMac, vlanId));
848 }
849 }
850 }
851
Ray Milkeyffe1a332018-01-24 10:41:14 -0800852 if (record != null) {
Taras Lemkin96a0d342018-03-26 14:52:58 +0000853 record.getV6Counters().incrementCounter(Dhcp6HandlerUtil.getMsgTypeStr(leafMsgType));
Ray Milkeyffe1a332018-01-24 10:41:14 -0800854 record.addLocation(new HostLocation(location, System.currentTimeMillis()));
855 record.ip6Status(DHCP6.MsgType.getType(leafMsgType));
856 record.setDirectlyConnected(directConnFlag);
857 if (!directConnFlag) {
858 // Update gateway mac address if the host is not directly connected
859 record.nextHop(srcMac);
860 }
861 record.updateLastSeen();
Kalhee Kim495c9b22017-11-07 16:32:09 +0000862 }
Kalhee Kim495c9b22017-11-07 16:32:09 +0000863 dhcpRelayStore.updateDhcpRecord(leafHostId, record);
Kalhee Kimd94ceea2017-11-29 19:03:02 +0000864 // TODO Use AtomicInteger for the counters
865 try {
866 recordSemaphore.acquire();
867 try {
Taras Lemkin96a0d342018-03-26 14:52:58 +0000868 dhcpRelayCountersStore.incrementCounter(gCount, Dhcp6HandlerUtil.getMsgTypeStr(leafMsgType));
Kalhee Kimd94ceea2017-11-29 19:03:02 +0000869 } finally {
870 // calling release() after a successful acquire()
871 recordSemaphore.release();
872 }
873 } catch (InterruptedException e) {
Ray Milkeyba547f92018-02-01 15:22:31 -0800874 Thread.currentThread().interrupt();
Kalhee Kimd94ceea2017-11-29 19:03:02 +0000875 }
Kalhee Kim495c9b22017-11-07 16:32:09 +0000876 }
877
878 /**
879 * add host or route and update dhcp relay record.
880 *
881 * @param directConnFlag flag to show that packet is from directly connected client
882 * @param location client side connect point
883 * @param dhcp6Relay the dhcp6 payload
884 * @param embeddedDhcp6 the dhcp6 payload within relay
885 * @param srcMac client gw/host macAddress
886 * @param clientInterface client interface
887 */
888 private void addHostOrRoute(boolean directConnFlag, ConnectPoint location, DHCP6 dhcp6Relay,
889 DHCP6 embeddedDhcp6, MacAddress srcMac, Interface clientInterface) {
890 log.debug("addHostOrRoute entered.");
891 VlanId vlanId = clientInterface.vlan();
Taras Lemkin96a0d342018-03-26 14:52:58 +0000892 Boolean isMsgReply = Dhcp6HandlerUtil.isDhcp6Reply(dhcp6Relay);
Kalhee Kim495c9b22017-11-07 16:32:09 +0000893 MacAddress leafClientMac;
894 Byte leafMsgType;
895
Kalhee Kimea4b6c22017-11-09 14:38:37 +0000896 Dhcp6ClientIdOption clientIdOption = extractClinedId(directConnFlag, embeddedDhcp6);
897 if (clientIdOption != null) {
898 log.debug("CLIENTID option found {}", clientIdOption);
899 if ((clientIdOption.getDuid().getDuidType() == Dhcp6Duid.DuidType.DUID_LLT) ||
900 (clientIdOption.getDuid().getDuidType() == Dhcp6Duid.DuidType.DUID_LL)) {
901 leafClientMac = MacAddress.valueOf(clientIdOption.getDuid().getLinkLayerAddress());
902 } else {
903 log.warn("Link-Layer Address not supported in CLIENTID option. No DhcpRelay Record created.");
Kalhee Kimd94ceea2017-11-29 19:03:02 +0000904 dhcpRelayCountersStore.incrementCounter(gCount, DhcpRelayCounters.NO_LINKLOCAL_FAIL);
Kalhee Kimea4b6c22017-11-09 14:38:37 +0000905 return;
906 }
Kalhee Kimea4b6c22017-11-09 14:38:37 +0000907 } else {
908 log.warn("CLIENTID option NOT found. No DhcpRelay Record created.");
Kalhee Kimd94ceea2017-11-29 19:03:02 +0000909 dhcpRelayCountersStore.incrementCounter(gCount, DhcpRelayCounters.NO_CLIENTID_FAIL);
Kalhee Kimea4b6c22017-11-09 14:38:37 +0000910 return;
911 }
Kalhee Kim495c9b22017-11-07 16:32:09 +0000912 HostId leafHostId = HostId.hostId(leafClientMac, vlanId);
913 DhcpRecord record = dhcpRelayStore.getDhcpRecord(leafHostId).orElse(null);
Kalhee Kimea4b6c22017-11-09 14:38:37 +0000914 if (record == null) {
915 record = new DhcpRecord(HostId.hostId(leafClientMac, vlanId));
Kalhee Kimd94ceea2017-11-29 19:03:02 +0000916 } else {
917 record = record.clone();
Kalhee Kimea4b6c22017-11-09 14:38:37 +0000918 }
Kalhee Kim495c9b22017-11-07 16:32:09 +0000919
920 IpAddressInfo ipInfo;
921 PdPrefixInfo pdInfo = null;
922 if (directConnFlag) {
923 // Add to host store if it connect to network directly
924 ipInfo = extractIpAddress(embeddedDhcp6);
925 if (ipInfo != null) {
926 if (isMsgReply) {
927 Set<IpAddress> ips = Sets.newHashSet(ipInfo.ip6Address);
928 HostId hostId = HostId.hostId(srcMac, vlanId);
929 Host host = hostService.getHost(hostId);
930 HostLocation hostLocation = new HostLocation(clientInterface.connectPoint(),
931 System.currentTimeMillis());
932 Set<HostLocation> hostLocations = Sets.newHashSet(hostLocation);
933 if (host != null) {
934 // Dual homing support:
935 // if host exists, use old locations and new location
936 hostLocations.addAll(host.locations());
937 }
938 HostDescription desc = new DefaultHostDescription(srcMac, vlanId, hostLocations, ips,
939 false);
940 log.debug("adding Host for directly connected.");
941 log.debug("client mac {} client vlan {} hostlocation {}",
942 HexString.toHexString(srcMac.toBytes(), ":"), vlanId, hostLocation.toString());
943 // Replace the ip when dhcp server give the host new ip address
944 providerService.hostDetected(hostId, desc, false);
945 }
946 } else {
947 log.warn("ipAddress not found. Do not add Host {} for directly connected.",
948 HostId.hostId(srcMac, vlanId).toString());
949 }
950 leafMsgType = embeddedDhcp6.getMsgType();
951 } else {
952 // Add to route store if it does not connect to network directly
953 // pick out the first link-local ip address
954 IpAddress nextHopIp = getFirstIpByHost(directConnFlag, srcMac, vlanId);
955 if (nextHopIp == null) {
956 log.warn("Can't find link-local IP address of gateway mac {} vlanId {}", srcMac, vlanId);
Kalhee Kimd94ceea2017-11-29 19:03:02 +0000957 dhcpRelayCountersStore.incrementCounter(gCount, DhcpRelayCounters.NO_LINKLOCAL_GW);
Kalhee Kim495c9b22017-11-07 16:32:09 +0000958 return;
959 }
960
Taras Lemkin96a0d342018-03-26 14:52:58 +0000961 DHCP6 leafDhcp = Dhcp6HandlerUtil.getDhcp6Leaf(embeddedDhcp6);
Kalhee Kim495c9b22017-11-07 16:32:09 +0000962 ipInfo = extractIpAddress(leafDhcp);
963 if (ipInfo == null) {
964 log.debug("ip is null");
965 } else {
966 if (isMsgReply) {
Kalhee Kim54a97c92018-04-02 21:23:46 +0000967 Route routeForIP = new Route(Route.Source.DHCP, ipInfo.ip6Address.toIpPrefix(), nextHopIp);
Kalhee Kim495c9b22017-11-07 16:32:09 +0000968 log.debug("adding Route of 128 address for indirectly connected.");
Daniel Ginsburg83b76452018-06-09 01:43:59 +0300969 routeStore.replaceRoute(routeForIP);
Kalhee Kim495c9b22017-11-07 16:32:09 +0000970 }
971 }
972
973 pdInfo = extractPrefix(leafDhcp);
974 if (pdInfo == null) {
975 log.debug("ipPrefix is null ");
976 } else {
977 if (isMsgReply) {
Kalhee Kim54a97c92018-04-02 21:23:46 +0000978 Route routeForPrefix = new Route(Route.Source.DHCP, pdInfo.pdPrefix, nextHopIp);
Kalhee Kim495c9b22017-11-07 16:32:09 +0000979 log.debug("adding Route of PD for indirectly connected.");
Daniel Ginsburg83b76452018-06-09 01:43:59 +0300980 routeStore.replaceRoute(routeForPrefix);
Kalhee Kim495c9b22017-11-07 16:32:09 +0000981 if (this.dhcpFpmEnabled) {
982 FpmRecord fpmRecord = new FpmRecord(pdInfo.pdPrefix, nextHopIp, FpmRecord.Type.DHCP_RELAY);
983 dhcpFpmPrefixStore.addFpmRecord(pdInfo.pdPrefix, fpmRecord);
984 }
985 }
986 }
987 leafMsgType = leafDhcp.getMsgType();
988 }
989 if (leafMsgType == DHCP6.MsgType.RELEASE.value() ||
990 (leafMsgType == DHCP6.MsgType.REPLY.value()) && ipInfo == null) {
991 log.warn("DHCP6 RELEASE/REPLY(null ip) from Server. MsgType {}", leafMsgType);
992 //return;
993 }
994
Kalhee Kimea4b6c22017-11-09 14:38:37 +0000995 record.addLocation(new HostLocation(location, System.currentTimeMillis()));
Kalhee Kim2f07c602017-11-15 18:57:53 +0000996
Kalhee Kimea4b6c22017-11-09 14:38:37 +0000997 if (leafMsgType == DHCP6.MsgType.REPLY.value()) {
Kalhee Kim495c9b22017-11-07 16:32:09 +0000998 if (ipInfo != null) {
Kalhee Kimea4b6c22017-11-09 14:38:37 +0000999 log.debug("IP6 address is being stored into dhcp-relay store.");
Lior Assoulinea21c0ca2018-01-28 16:18:48 -08001000 log.debug("Client IP6 address {}", HexString.toHexString(ipInfo.ip6Address.toOctets(), ":"));
Kalhee Kim495c9b22017-11-07 16:32:09 +00001001 record.ip6Address(ipInfo.ip6Address);
Kalhee Kim2f07c602017-11-15 18:57:53 +00001002 record.updateAddrPrefTime(ipInfo.prefTime);
1003 record.updateLastIp6Update();
Lior Assoulinea21c0ca2018-01-28 16:18:48 -08001004 } else {
Kalhee Kimea4b6c22017-11-09 14:38:37 +00001005 log.debug("IP6 address is not returned from server. Maybe only PD is returned.");
1006 }
Kalhee Kim495c9b22017-11-07 16:32:09 +00001007 if (pdInfo != null) {
1008 log.debug("IP6 PD address {}",
1009 HexString.toHexString(pdInfo.pdPrefix.address().toOctets(), ":"));
Kalhee Kim2f07c602017-11-15 18:57:53 +00001010 record.pdPrefix(pdInfo.pdPrefix);
1011 record.updatePdPrefTime(pdInfo.prefTime);
1012 record.updateLastPdUpdate();
Kalhee Kimea4b6c22017-11-09 14:38:37 +00001013 } else {
1014 log.debug("IP6 PD address is not returned from server. Maybe only IPAddress is returned.");
1015 }
1016 }
Kalhee Kim2f07c602017-11-15 18:57:53 +00001017
Taras Lemkin96a0d342018-03-26 14:52:58 +00001018 record.getV6Counters().incrementCounter(Dhcp6HandlerUtil.getMsgTypeStr(leafMsgType));
Kalhee Kim495c9b22017-11-07 16:32:09 +00001019 record.ip6Status(DHCP6.MsgType.getType(leafMsgType));
Kalhee Kimea4b6c22017-11-09 14:38:37 +00001020 record.setDirectlyConnected(directConnFlag);
1021 record.updateLastSeen();
Kalhee Kim495c9b22017-11-07 16:32:09 +00001022 dhcpRelayStore.updateDhcpRecord(leafHostId, record);
Kalhee Kimd94ceea2017-11-29 19:03:02 +00001023 // TODO Use AtomicInteger for the counters
1024 try {
1025 recordSemaphore.acquire();
1026 try {
Taras Lemkin96a0d342018-03-26 14:52:58 +00001027 dhcpRelayCountersStore.incrementCounter(gCount, Dhcp6HandlerUtil.getMsgTypeStr(leafMsgType));
Kalhee Kimd94ceea2017-11-29 19:03:02 +00001028 } finally {
1029 // calling release() after a successful acquire()
1030 recordSemaphore.release();
1031 }
1032 } catch (InterruptedException e) {
Ray Milkeyba547f92018-02-01 15:22:31 -08001033 Thread.currentThread().interrupt();
Kalhee Kimd94ceea2017-11-29 19:03:02 +00001034 }
Kalhee Kim45fede42017-09-05 19:05:06 +00001035 }
1036
Taras Lemkin96a0d342018-03-26 14:52:58 +00001037 private List<InternalPacket> processDhcp6ForwardOnly(PacketContext context,
1038 Ethernet clientPacket,
1039 Set<Interface> clientInterfaces,
1040 DHCP6 dhcpPacket) {
1041 ConnectPoint inPort = context.inPacket().receivedFrom();
1042 log.trace("Got DHCPv6 on port {}", inPort);
1043 boolean directConnFlag = Dhcp6HandlerUtil.directlyConnected(dhcpPacket);
1044
1045 List<InternalPacket> internalPackets = new ArrayList<>();
1046 List<DhcpServerInfo> serverInfoList = findValidServerInfo(directConnFlag);
1047
1048 for (DhcpServerInfo dhcpServer : serverInfoList) {
1049 Ethernet newPacket = Dhcp6HandlerUtil.buildDhcp6PacketFromClient(context,
1050 clientPacket, clientInterfaces, dhcpServer, getServerInterface(dhcpServer));
1051 log.trace("Built packet for server {} : {}", dhcpServer, newPacket);
1052 internalPackets.add(InternalPacket.internalPacket(newPacket,
1053 dhcpServer.getDhcpServerConnectPoint().get()));
1054 }
1055
1056 return internalPackets;
1057 }
1058
Lior Assoulinea21c0ca2018-01-28 16:18:48 -08001059 private List<InternalPacket> processLQ6PacketFromClient(PacketContext context,
1060 Ethernet clientPacket,
1061 Set<Interface> clientInterfaces,
1062 DHCP6 dhcp6Payload) {
1063 ConnectPoint inPort = context.inPacket().receivedFrom();
1064 log.info("Got LQ-REQUEST V6 on port {}", inPort);
1065 List<Dhcp6Option> lopt = dhcp6Payload.getOptions();
1066 log.info("Options list: {}", lopt);
1067 Dhcp6LeaseQueryOption lqoption = dhcp6Payload.getOptions()
1068 .stream()
1069 .filter(opt -> opt instanceof Dhcp6LeaseQueryOption)
1070 .map(pld -> (Dhcp6LeaseQueryOption) pld)
1071 .findFirst()
1072 .orElse(null);
1073
1074 if (lqoption == null) {
1075 // Can't find dhcp payload
1076 log.warn("Can't find dhcp6 lease query message - aborting");
1077 return null;
1078 } else {
1079 log.info("dhcp6 lqv6 options found: {}", lqoption);
1080 }
1081 log.warn("LQv6 for " + lqoption.linkAddress.toString() + " comes from " + inPort.toString());
1082 Ethernet packet = context.inPacket().parsed();
1083 Ip6Address clientAddress = lqoption.linkAddress;
1084 IPv6 ipv6Packet = (IPv6) packet.getPayload();
1085 Ip6Address nextHopIp = findNextHopIp6FromRelayStore(clientAddress);
1086
1087 // 1. only if there is a route to remove - remove it
1088 if (nextHopIp != null) {
Kalhee Kim54a97c92018-04-02 21:23:46 +00001089 Route routeForIP6 = new Route(Route.Source.DHCP, clientAddress.toIpPrefix(), nextHopIp);
Lior Assoulinea21c0ca2018-01-28 16:18:48 -08001090 log.debug("Removing route of Client " + clientAddress +
1091 " for indirectly connected - next hop ip6 " + nextHopIp);
1092 routeStore.removeRoute(routeForIP6);
1093 }
1094
1095 // 2. note the potential NH this packet came from in case it's a known lease
1096 // this NH will then be used to build the route
1097 MacAddress potentialNH = packet.getSourceMAC();
1098 VlanId vlanId = VlanId.vlanId(packet.getVlanID());
1099 setPotentialNextHopForIp6InRelayStore(clientAddress, vlanId, potentialNH);
Lior Assoulinea21c0ca2018-01-28 16:18:48 -08001100 // 3. route this LQ6 to all relevant servers
1101 IPv6 clientIpv6 = (IPv6) clientPacket.getPayload();
1102 UDP clientUdp = (UDP) clientIpv6.getPayload();
1103 DHCP6 clientDhcp6 = (DHCP6) clientUdp.getPayload();
1104
Taras Lemkin96a0d342018-03-26 14:52:58 +00001105 boolean directConnFlag = Dhcp6HandlerUtil.directlyConnected(clientDhcp6);
Lior Assoulinea21c0ca2018-01-28 16:18:48 -08001106 List<InternalPacket> internalPackets = new ArrayList<>();
1107 List<DhcpServerInfo> serverInfoList = findValidServerInfo(directConnFlag);
1108 List<DhcpServerInfo> copyServerInfoList = new ArrayList<DhcpServerInfo>(serverInfoList);
1109
1110 for (DhcpServerInfo serverInfo : copyServerInfoList) {
Taras Lemkin96a0d342018-03-26 14:52:58 +00001111 if (!Dhcp6HandlerUtil.checkDhcpServerConnPt(directConnFlag, serverInfo)) {
Lior Assoulinea21c0ca2018-01-28 16:18:48 -08001112 log.warn("Can't get server connect point, ignore");
1113 continue;
1114 }
1115 DhcpServerInfo newServerInfo = getHostInfoForServerInfo(serverInfo, serverInfoList);
1116 if (newServerInfo == null) {
1117 log.warn("Can't get server interface with host info resolved, ignore");
1118 continue;
1119 }
Lior Assoulinea21c0ca2018-01-28 16:18:48 -08001120 Interface serverInterface = getServerInterface(newServerInfo);
1121 if (serverInterface == null) {
1122 log.warn("Can't get server interface, ignore");
1123 continue;
1124 }
Lior Assoulinea21c0ca2018-01-28 16:18:48 -08001125 Ethernet etherRouted = (Ethernet) clientPacket.clone();
1126 MacAddress macFacingServer = serverInterface.mac();
1127 if (macFacingServer == null) {
1128 log.warn("No MAC address for server Interface {}", serverInterface);
1129 return null;
1130 }
1131 etherRouted.setSourceMACAddress(macFacingServer);
1132 etherRouted.setDestinationMACAddress(newServerInfo.getDhcpConnectMac().get());
1133 InternalPacket internalPacket =
Taras Lemkin96a0d342018-03-26 14:52:58 +00001134 InternalPacket.internalPacket(etherRouted,
Lior Assoulinea21c0ca2018-01-28 16:18:48 -08001135 serverInfo.getDhcpServerConnectPoint().get());
1136 internalPackets.add(internalPacket);
1137 log.debug("Sending LQ to DHCP server {}", newServerInfo.getDhcpServerIp6());
1138 }
1139 log.debug("num of client packets to send is{}", internalPackets.size());
1140
1141 return internalPackets;
1142 }
1143
Kalhee Kim45fede42017-09-05 19:05:06 +00001144 /**
Kalhee Kim495c9b22017-11-07 16:32:09 +00001145 * build the DHCP6 solicit/request packet with gatewayip.
Kalhee Kim45fede42017-09-05 19:05:06 +00001146 *
1147 * @param context packet context
1148 * @param clientPacket client ethernet packet
1149 * @param clientInterfaces set of client side interfaces
1150 */
Kalhee Kim495c9b22017-11-07 16:32:09 +00001151 private List<InternalPacket> processDhcp6PacketFromClient(PacketContext context,
1152 Ethernet clientPacket,
1153 Set<Interface> clientInterfaces) {
Yi Tseng3bd57ac2017-11-29 14:39:18 -08001154 ConnectPoint receivedFrom = context.inPacket().receivedFrom();
Taras Lemkin96a0d342018-03-26 14:52:58 +00001155 Ip6Address relayAgentIp = Dhcp6HandlerUtil.getRelayAgentIPv6Address(clientInterfaces);
Yi Tseng3bd57ac2017-11-29 14:39:18 -08001156 MacAddress relayAgentMac = clientInterfaces.iterator().next().mac();
1157 if (relayAgentIp == null || relayAgentMac == null) {
1158 log.warn("Missing DHCP relay agent interface Ipv6 addr config for "
Kalhee Kim495c9b22017-11-07 16:32:09 +00001159 + "packet from client on port: {}. Aborting packet processing",
1160 clientInterfaces.iterator().next().connectPoint());
Kalhee Kimd94ceea2017-11-29 19:03:02 +00001161 dhcpRelayCountersStore.incrementCounter(gCount, DhcpRelayCounters.NO_CLIENT_INTF_MAC);
Charles Chan239492c2018-01-22 13:27:28 -08001162 return Lists.newArrayList();
Kalhee Kim121ba922017-11-01 17:56:44 +00001163 }
Kalhee Kim45fede42017-09-05 19:05:06 +00001164
Kalhee Kim495c9b22017-11-07 16:32:09 +00001165 IPv6 clientIpv6 = (IPv6) clientPacket.getPayload();
1166 UDP clientUdp = (UDP) clientIpv6.getPayload();
1167 DHCP6 clientDhcp6 = (DHCP6) clientUdp.getPayload();
1168
Taras Lemkin96a0d342018-03-26 14:52:58 +00001169 boolean directConnFlag = Dhcp6HandlerUtil.directlyConnected(clientDhcp6);
Charles Chana990ce92017-10-30 10:22:50 -07001170
Kalhee Kim121ba922017-11-01 17:56:44 +00001171 ConnectPoint clientConnectionPoint = context.inPacket().receivedFrom();
1172 VlanId vlanIdInUse = VlanId.vlanId(clientPacket.getVlanID());
1173 Interface clientInterface = interfaceService.getInterfacesByPort(clientConnectionPoint)
Taras Lemkin96a0d342018-03-26 14:52:58 +00001174 .stream().filter(iface -> Dhcp6HandlerUtil.interfaceContainsVlan(iface, vlanIdInUse))
Kalhee Kim495c9b22017-11-07 16:32:09 +00001175 .findFirst()
1176 .orElse(null);
Kalhee Kim45fede42017-09-05 19:05:06 +00001177
Kalhee Kim495c9b22017-11-07 16:32:09 +00001178 List<InternalPacket> internalPackets = new ArrayList<>();
1179 List<DhcpServerInfo> serverInfoList = findValidServerInfo(directConnFlag);
1180 List<DhcpServerInfo> copyServerInfoList = new ArrayList<DhcpServerInfo>(serverInfoList);
Kalhee Kim45b24182017-10-18 18:30:23 +00001181
Kalhee Kim495c9b22017-11-07 16:32:09 +00001182 for (DhcpServerInfo serverInfo : copyServerInfoList) {
Taras Lemkin96a0d342018-03-26 14:52:58 +00001183 if (!Dhcp6HandlerUtil.checkDhcpServerConnPt(directConnFlag, serverInfo)) {
Kalhee Kim495c9b22017-11-07 16:32:09 +00001184 log.warn("Can't get server connect point, ignore");
1185 continue;
1186 }
1187 DhcpServerInfo newServerInfo = getHostInfoForServerInfo(serverInfo, serverInfoList);
1188 if (newServerInfo == null) {
1189 log.warn("Can't get server interface with host info resolved, ignore");
1190 continue;
1191 }
Kalhee Kim45fede42017-09-05 19:05:06 +00001192
Kalhee Kim495c9b22017-11-07 16:32:09 +00001193 Interface serverInterface = getServerInterface(newServerInfo);
1194 if (serverInterface == null) {
1195 log.warn("Can't get server interface, ignore");
1196 continue;
1197 }
Kalhee Kimea4b6c22017-11-09 14:38:37 +00001198
Taras Lemkin96a0d342018-03-26 14:52:58 +00001199 Ethernet etherReply = Dhcp6HandlerUtil.buildDhcp6PacketFromClient(context, clientPacket,
Kalhee Kim495c9b22017-11-07 16:32:09 +00001200 clientInterfaces, newServerInfo, serverInterface);
1201 removeHostOrRoute(directConnFlag, clientConnectionPoint, clientDhcp6, clientPacket,
1202 clientIpv6, clientInterface);
Kalhee Kimea4b6c22017-11-09 14:38:37 +00001203
Taras Lemkin96a0d342018-03-26 14:52:58 +00001204 InternalPacket internalPacket = InternalPacket.internalPacket(etherReply,
Kalhee Kim495c9b22017-11-07 16:32:09 +00001205 serverInfo.getDhcpServerConnectPoint().get());
1206 internalPackets.add(internalPacket);
1207 }
1208 log.debug("num of client packets to send is{}", internalPackets.size());
Kalhee Kimea4b6c22017-11-09 14:38:37 +00001209
Kalhee Kim495c9b22017-11-07 16:32:09 +00001210 return internalPackets;
1211 }
Kalhee Kim45fede42017-09-05 19:05:06 +00001212
1213 /**
Kalhee Kim45fede42017-09-05 19:05:06 +00001214 * process the DHCP6 relay-reply packet from dhcp server.
1215 *
1216 * @param context packet context
1217 * @param receivedPacket server ethernet packet
1218 * @param recevingInterfaces set of server side interfaces
Kalhee Kim495c9b22017-11-07 16:32:09 +00001219 * @return internalPacket toward client
Kalhee Kim45fede42017-09-05 19:05:06 +00001220 */
1221 private InternalPacket processDhcp6PacketFromServer(PacketContext context,
1222 Ethernet receivedPacket, Set<Interface> recevingInterfaces) {
Kalhee Kim45fede42017-09-05 19:05:06 +00001223 // get dhcp6 header.
Ray Milkeyf0c47612017-09-28 11:29:38 -07001224 Ethernet etherReply = receivedPacket.duplicate();
Kalhee Kim45fede42017-09-05 19:05:06 +00001225 IPv6 ipv6Packet = (IPv6) etherReply.getPayload();
1226 UDP udpPacket = (UDP) ipv6Packet.getPayload();
1227 DHCP6 dhcp6Relay = (DHCP6) udpPacket.getPayload();
Taras Lemkin96a0d342018-03-26 14:52:58 +00001228 Boolean directConnFlag = Dhcp6HandlerUtil.directlyConnected(dhcp6Relay);
Kalhee Kim45fede42017-09-05 19:05:06 +00001229
Kalhee Kim495c9b22017-11-07 16:32:09 +00001230 DHCP6 embeddedDhcp6 = dhcp6Relay.getOptions().stream()
1231 .filter(opt -> opt instanceof Dhcp6RelayOption)
1232 .map(BasePacket::getPayload)
1233 .map(pld -> (DHCP6) pld)
1234 .findFirst()
1235 .orElse(null);
Kalhee Kimd21029f2017-09-26 20:21:53 +00001236 ConnectPoint inPort = context.inPacket().receivedFrom();
Kalhee Kim495c9b22017-11-07 16:32:09 +00001237 DhcpServerInfo foundServerInfo = findServerInfoFromServer(directConnFlag, inPort);
Kalhee Kimd21029f2017-09-26 20:21:53 +00001238
Kalhee Kim495c9b22017-11-07 16:32:09 +00001239 if (foundServerInfo == null) {
1240 log.warn("Cannot find server info");
Kalhee Kimd94ceea2017-11-29 19:03:02 +00001241 dhcpRelayCountersStore.incrementCounter(gCount, DhcpRelayCounters.NO_SERVER_INFO);
Kalhee Kimd21029f2017-09-26 20:21:53 +00001242 return null;
Kalhee Kim495c9b22017-11-07 16:32:09 +00001243 } else {
Taras Lemkin96a0d342018-03-26 14:52:58 +00001244 if (Dhcp6HandlerUtil.isServerIpEmpty(foundServerInfo)) {
Kalhee Kim495c9b22017-11-07 16:32:09 +00001245 log.warn("Cannot find server info's ipaddress");
Kalhee Kimd94ceea2017-11-29 19:03:02 +00001246 dhcpRelayCountersStore.incrementCounter(gCount, DhcpRelayCounters.NO_SERVER_IP6ADDR);
Kalhee Kim495c9b22017-11-07 16:32:09 +00001247 return null;
1248 }
Kalhee Kimd21029f2017-09-26 20:21:53 +00001249 }
1250
Kalhee Kim45fede42017-09-05 19:05:06 +00001251 Dhcp6InterfaceIdOption interfaceIdOption = dhcp6Relay.getOptions().stream()
1252 .filter(opt -> opt instanceof Dhcp6InterfaceIdOption)
1253 .map(opt -> (Dhcp6InterfaceIdOption) opt)
1254 .findFirst()
1255 .orElse(null);
Kalhee Kim45fede42017-09-05 19:05:06 +00001256 if (interfaceIdOption == null) {
1257 log.warn("Interface Id option is not present, abort packet...");
Kalhee Kimd94ceea2017-11-29 19:03:02 +00001258 dhcpRelayCountersStore.incrementCounter(gCount, DhcpRelayCounters.OPTION_MISSING_FAIL);
Kalhee Kim45fede42017-09-05 19:05:06 +00001259 return null;
1260 }
1261
1262 MacAddress peerMac = interfaceIdOption.getMacAddress();
1263 String clientConnectionPointStr = new String(interfaceIdOption.getInPort());
Kalhee Kim45fede42017-09-05 19:05:06 +00001264 ConnectPoint clientConnectionPoint = ConnectPoint.deviceConnectPoint(clientConnectionPointStr);
Kalhee Kim0c0cb0b2017-09-15 17:43:27 +00001265 VlanId vlanIdInUse = VlanId.vlanId(interfaceIdOption.getVlanId());
1266 Interface clientInterface = interfaceService.getInterfacesByPort(clientConnectionPoint)
Taras Lemkin96a0d342018-03-26 14:52:58 +00001267 .stream().filter(iface -> Dhcp6HandlerUtil.interfaceContainsVlan(iface, vlanIdInUse))
Kalhee Kim495c9b22017-11-07 16:32:09 +00001268 .findFirst().orElse(null);
Kalhee Kim0c0cb0b2017-09-15 17:43:27 +00001269 if (clientInterface == null) {
1270 log.warn("Cannot get client interface for from packet, abort... vlan {}", vlanIdInUse.toString());
Kalhee Kimd94ceea2017-11-29 19:03:02 +00001271 dhcpRelayCountersStore.incrementCounter(gCount, DhcpRelayCounters.NO_MATCHING_INTF);
Kalhee Kim45fede42017-09-05 19:05:06 +00001272 return null;
1273 }
Kalhee Kim44fbda12018-04-03 21:08:18 +00001274 etherReply.setVlanID(vlanIdInUse.toShort());
1275
Kalhee Kim0c0cb0b2017-09-15 17:43:27 +00001276 MacAddress relayAgentMac = clientInterface.mac();
Kalhee Kim45fede42017-09-05 19:05:06 +00001277 if (relayAgentMac == null) {
Yi Tseng68ef26b2017-12-18 17:10:00 -08001278 log.warn("Can not get client interface mac, abort packet..");
Kalhee Kimd94ceea2017-11-29 19:03:02 +00001279 dhcpRelayCountersStore.incrementCounter(gCount, DhcpRelayCounters.NO_CLIENT_INTF_MAC);
Kalhee Kim45fede42017-09-05 19:05:06 +00001280 return null;
1281 }
1282 etherReply.setSourceMACAddress(relayAgentMac);
1283
1284 // find destMac
Yi Tseng68ef26b2017-12-18 17:10:00 -08001285 MacAddress clientMac;
Yi Tsengaa417a62017-09-08 17:22:51 -07001286 Ip6Address peerAddress = Ip6Address.valueOf(dhcp6Relay.getPeerAddress());
1287 Set<Host> clients = hostService.getHostsByIp(peerAddress);
Kalhee Kim45fede42017-09-05 19:05:06 +00001288 if (clients.isEmpty()) {
Yi Tseng68ef26b2017-12-18 17:10:00 -08001289 log.trace("There's no host found for this address {}",
Kalhee Kim45fede42017-09-05 19:05:06 +00001290 HexString.toHexString(dhcp6Relay.getPeerAddress(), ":"));
Yi Tseng68ef26b2017-12-18 17:10:00 -08001291 log.trace("Let's look up interfaceId {}", HexString.toHexString(peerMac.toBytes(), ":"));
Kalhee Kim45fede42017-09-05 19:05:06 +00001292 clientMac = peerMac;
1293 } else {
1294 clientMac = clients.iterator().next().mac();
1295 if (clientMac == null) {
1296 log.warn("No client mac address found, abort packet...");
Kalhee Kimd94ceea2017-11-29 19:03:02 +00001297 dhcpRelayCountersStore.incrementCounter(gCount, DhcpRelayCounters.NO_CLIENT_INTF_MAC);
Kalhee Kim45fede42017-09-05 19:05:06 +00001298 return null;
1299 }
Yi Tseng68ef26b2017-12-18 17:10:00 -08001300 log.trace("Client mac address found from getHostByIp");
Kalhee Kim45fede42017-09-05 19:05:06 +00001301 }
1302 etherReply.setDestinationMACAddress(clientMac);
Kalhee Kim45fede42017-09-05 19:05:06 +00001303 // ip header
1304 ipv6Packet.setSourceAddress(dhcp6Relay.getLinkAddress());
1305 ipv6Packet.setDestinationAddress(dhcp6Relay.getPeerAddress());
1306 // udp header
1307 udpPacket.setSourcePort(UDP.DHCP_V6_SERVER_PORT);
1308 if (directConnFlag) {
1309 udpPacket.setDestinationPort(UDP.DHCP_V6_CLIENT_PORT);
1310 } else {
1311 udpPacket.setDestinationPort(UDP.DHCP_V6_SERVER_PORT);
1312 }
Lior Assoulinea21c0ca2018-01-28 16:18:48 -08001313
Kalhee Kim45fede42017-09-05 19:05:06 +00001314
Taras Lemkin96a0d342018-03-26 14:52:58 +00001315 boolean hostOrRouteAllowed = learnRouteFromLeasequery ||
1316 Dhcp6HandlerUtil.getDhcp6LeafMessageType(dhcp6Relay) != MsgType.LEASEQUERY_REPLY;
1317 log.debug("Can add host or route: {}", hostOrRouteAllowed);
1318
1319 if (hostOrRouteAllowed) {
1320 // add host or route
1321 addHostOrRoute(directConnFlag, clientConnectionPoint, dhcp6Relay, embeddedDhcp6,
1322 clientMac, clientInterface);
1323 }
1324
Kalhee Kim45fede42017-09-05 19:05:06 +00001325 udpPacket.setPayload(embeddedDhcp6);
1326 udpPacket.resetChecksum();
1327 ipv6Packet.setPayload(udpPacket);
1328 etherReply.setPayload(ipv6Packet);
Taras Lemkin96a0d342018-03-26 14:52:58 +00001329 return InternalPacket.internalPacket(etherReply, clientConnectionPoint);
Lior Assoulinea21c0ca2018-01-28 16:18:48 -08001330 }
Yi Tseng483ac6f2017-08-02 15:03:31 -07001331
1332 @Override
Kalhee Kimba366062017-11-07 16:32:09 +00001333 public void setDhcpFpmEnabled(Boolean enabled) {
1334 dhcpFpmEnabled = enabled;
1335 }
1336
1337 @Override
Yi Tseng483ac6f2017-08-02 15:03:31 -07001338 public void setDefaultDhcpServerConfigs(Collection<DhcpServerConfig> configs) {
Kalhee Kim495c9b22017-11-07 16:32:09 +00001339 log.debug("setDefaultDhcpServerConfigs is called.");
Yi Tseng919b2df2017-09-07 16:22:51 -07001340 setDhcpServerConfigs(configs, defaultServerInfoList);
Yi Tseng919b2df2017-09-07 16:22:51 -07001341 }
1342
1343 @Override
1344 public void setIndirectDhcpServerConfigs(Collection<DhcpServerConfig> configs) {
Kalhee Kim495c9b22017-11-07 16:32:09 +00001345 log.debug("setIndirectDhcpServerConfigs is called.");
Yi Tseng919b2df2017-09-07 16:22:51 -07001346 setDhcpServerConfigs(configs, indirectServerInfoList);
Yi Tseng919b2df2017-09-07 16:22:51 -07001347 }
1348
1349 public void setDhcpServerConfigs(Collection<DhcpServerConfig> configs, List<DhcpServerInfo> serverInfoList) {
Kalhee Kim495c9b22017-11-07 16:32:09 +00001350 log.debug("config size {}.", configs.size());
1351
Kalhee Kim45fede42017-09-05 19:05:06 +00001352 if (configs.size() == 0) {
1353 // no config to update
1354 return;
1355 }
Kalhee Kim45fede42017-09-05 19:05:06 +00001356 // TODO: currently we pick up first DHCP server config.
1357 // Will use other server configs in the future for HA.
Kalhee Kim495c9b22017-11-07 16:32:09 +00001358 Boolean isConfigValid = false;
1359 for (DhcpServerConfig serverConfig : configs) {
1360 if (serverConfig.getDhcpServerIp6().isPresent()) {
1361 isConfigValid = true;
1362 break;
1363 }
Kalhee Kim45fede42017-09-05 19:05:06 +00001364 }
Kalhee Kim495c9b22017-11-07 16:32:09 +00001365 if (!isConfigValid) {
1366 log.warn("No IP V6 server address found.");
1367 return; // No IP V6 address found
1368 }
1369 for (DhcpServerInfo oldServerInfo : serverInfoList) {
Yi Tseng919b2df2017-09-07 16:22:51 -07001370 // stop monitoring gateway or server
1371 oldServerInfo.getDhcpGatewayIp6().ifPresent(gatewayIp -> {
1372 hostService.stopMonitoringIp(gatewayIp);
1373 });
1374 oldServerInfo.getDhcpServerIp6().ifPresent(serverIp -> {
1375 hostService.stopMonitoringIp(serverIp);
Yi Tseng525ff402017-10-23 19:39:39 -07001376 cancelDhcpPacket(serverIp);
Yi Tseng919b2df2017-09-07 16:22:51 -07001377 });
1378 }
Kalhee Kim495c9b22017-11-07 16:32:09 +00001379 serverInfoList.clear();
1380 for (DhcpServerConfig serverConfig : configs) {
1381 // Create new server info according to the config
1382 DhcpServerInfo newServerInfo = new DhcpServerInfo(serverConfig,
1383 DhcpServerInfo.Version.DHCP_V6);
1384 checkState(newServerInfo.getDhcpServerConnectPoint().isPresent(),
1385 "Connect point not exists");
1386 checkState(newServerInfo.getDhcpServerIp6().isPresent(),
1387 "IP of DHCP server not exists");
Yi Tseng919b2df2017-09-07 16:22:51 -07001388
Kalhee Kim495c9b22017-11-07 16:32:09 +00001389 log.debug("DHCP server connect point: {}", newServerInfo.getDhcpServerConnectPoint().orElse(null));
1390 log.debug("DHCP server IP: {}", newServerInfo.getDhcpServerIp6().orElse(null));
Yi Tseng919b2df2017-09-07 16:22:51 -07001391
Kalhee Kim495c9b22017-11-07 16:32:09 +00001392 Ip6Address serverIp = newServerInfo.getDhcpServerIp6().get();
1393 Ip6Address ipToProbe;
1394 if (newServerInfo.getDhcpGatewayIp6().isPresent()) {
1395 ipToProbe = newServerInfo.getDhcpGatewayIp6().get();
1396 } else {
1397 ipToProbe = newServerInfo.getDhcpServerIp6().orElse(null);
1398 }
1399 String hostToProbe = newServerInfo.getDhcpGatewayIp6()
1400 .map(ip -> "gateway").orElse("server");
Yi Tseng919b2df2017-09-07 16:22:51 -07001401
Kalhee Kim495c9b22017-11-07 16:32:09 +00001402 log.warn("Probing to resolve {} IP {}", hostToProbe, ipToProbe);
1403 hostService.startMonitoringIp(ipToProbe);
1404
1405 Set<Host> hosts = hostService.getHostsByIp(ipToProbe);
1406 if (!hosts.isEmpty()) {
1407 Host host = hosts.iterator().next();
1408 newServerInfo.setDhcpConnectVlan(host.vlan());
1409 newServerInfo.setDhcpConnectMac(host.mac());
1410 log.warn("Host found host {}", host);
1411
1412 } else {
1413 log.warn("No host found host ip {}", ipToProbe);
1414 }
1415 // Add new server info
1416 synchronized (this) {
1417 serverInfoList.add(newServerInfo);
1418 }
1419 if (!hosts.isEmpty()) {
1420 requestDhcpPacket(serverIp);
1421 }
Yi Tseng919b2df2017-09-07 16:22:51 -07001422 }
Kalhee Kim45fede42017-09-05 19:05:06 +00001423 }
1424
1425 class InternalHostListener implements HostListener {
1426 @Override
1427 public void event(HostEvent event) {
1428 switch (event.type()) {
1429 case HOST_ADDED:
1430 case HOST_UPDATED:
Jordan Halterman6328db72018-04-10 13:34:50 -04001431 log.trace("Scheduled host event {}", event);
1432 hostEventExecutor.execute(() -> hostUpdated(event.subject()));
Kalhee Kim45fede42017-09-05 19:05:06 +00001433 break;
1434 case HOST_REMOVED:
Jordan Halterman6328db72018-04-10 13:34:50 -04001435 log.trace("Scheduled host event {}", event);
1436 hostEventExecutor.execute(() -> hostRemoved(event.subject()));
Kalhee Kim45fede42017-09-05 19:05:06 +00001437 break;
Kalhee Kim45fede42017-09-05 19:05:06 +00001438 default:
1439 break;
1440 }
1441 }
1442 }
1443
1444 /**
Kalhee Kim45fede42017-09-05 19:05:06 +00001445 * Handle host updated.
1446 * If the host is DHCP server or gateway, update connect mac and vlan.
1447 *
1448 * @param host the host
1449 */
1450 private void hostUpdated(Host host) {
Yi Tseng525ff402017-10-23 19:39:39 -07001451 hostUpdated(host, defaultServerInfoList);
1452 hostUpdated(host, indirectServerInfoList);
Kalhee Kim45fede42017-09-05 19:05:06 +00001453 }
1454
Yi Tseng525ff402017-10-23 19:39:39 -07001455 private void hostUpdated(Host host, List<DhcpServerInfo> serverInfoList) {
1456 DhcpServerInfo serverInfo;
1457 Ip6Address targetIp;
1458 if (!serverInfoList.isEmpty()) {
1459 serverInfo = serverInfoList.get(0);
1460 Ip6Address serverIp = serverInfo.getDhcpServerIp6().orElse(null);
1461 targetIp = serverInfo.getDhcpGatewayIp6().orElse(null);
1462
1463 if (targetIp == null) {
1464 targetIp = serverIp;
1465 }
Yi Tseng525ff402017-10-23 19:39:39 -07001466 if (targetIp != null) {
1467 if (host.ipAddresses().contains(targetIp)) {
1468 serverInfo.setDhcpConnectMac(host.mac());
1469 serverInfo.setDhcpConnectVlan(host.vlan());
1470 requestDhcpPacket(serverIp);
1471 }
1472 }
1473 }
1474 }
Kalhee Kim45fede42017-09-05 19:05:06 +00001475 /**
1476 * Handle host removed.
1477 * If the host is DHCP server or gateway, unset connect mac and vlan.
1478 *
1479 * @param host the host
1480 */
1481 private void hostRemoved(Host host) {
Yi Tseng525ff402017-10-23 19:39:39 -07001482 hostRemoved(host, defaultServerInfoList);
1483 hostRemoved(host, indirectServerInfoList);
Yi Tseng919b2df2017-09-07 16:22:51 -07001484 }
1485
Yi Tseng525ff402017-10-23 19:39:39 -07001486 private void hostRemoved(Host host, List<DhcpServerInfo> serverInfoList) {
1487 DhcpServerInfo serverInfo;
1488 Ip6Address targetIp;
1489
1490 if (!serverInfoList.isEmpty()) {
1491 serverInfo = serverInfoList.get(0);
1492 Ip6Address serverIp = serverInfo.getDhcpServerIp6().orElse(null);
1493 targetIp = serverInfo.getDhcpGatewayIp6().orElse(null);
1494
1495 if (targetIp == null) {
1496 targetIp = serverIp;
1497 }
Yi Tseng525ff402017-10-23 19:39:39 -07001498 if (targetIp != null) {
1499 if (host.ipAddresses().contains(targetIp)) {
1500 serverInfo.setDhcpConnectVlan(null);
1501 serverInfo.setDhcpConnectMac(null);
1502 cancelDhcpPacket(serverIp);
1503 }
1504 }
1505 }
1506 }
1507
Lior Assoulinea21c0ca2018-01-28 16:18:48 -08001508 /**
1509 * Gets Interface facing to the server for default host.
1510 *
1511 * @return the Interface facing to the server; null if not found
1512 */
1513 private Interface getServerInterface() {
1514 DhcpServerInfo serverInfo;
1515 ConnectPoint dhcpServerConnectPoint;
1516 VlanId dhcpConnectVlan;
1517
1518 if (!defaultServerInfoList.isEmpty()) {
1519 serverInfo = defaultServerInfoList.get(0);
1520 dhcpServerConnectPoint = serverInfo.getDhcpServerConnectPoint().orElse(null);
1521 dhcpConnectVlan = serverInfo.getDhcpConnectVlan().orElse(null);
1522 } else {
1523 return null;
1524 }
1525 if (dhcpServerConnectPoint == null || dhcpConnectVlan == null) {
1526 log.info("Default DHCP server {} not resolve yet", serverInfo.getDhcpGatewayIp6());
1527 return null;
1528 }
1529 return interfaceService.getInterfacesByPort(dhcpServerConnectPoint)
1530 .stream()
Taras Lemkin96a0d342018-03-26 14:52:58 +00001531 .filter(iface -> Dhcp6HandlerUtil.interfaceContainsVlan(iface, dhcpConnectVlan))
Lior Assoulinea21c0ca2018-01-28 16:18:48 -08001532 .findFirst()
1533 .orElse(null);
1534 }
1535
1536 /**
1537 * Gets Interface facing to the server for indirect hosts.
1538 * Use default server Interface if indirect server not configured.
1539 *
1540 * @return the Interface facing to the server; null if not found
1541 */
1542 private Interface getIndirectServerInterface() {
1543 DhcpServerInfo serverInfo;
1544
1545 ConnectPoint indirectDhcpServerConnectPoint;
1546 VlanId indirectDhcpConnectVlan;
1547
1548 if (!indirectServerInfoList.isEmpty()) {
1549 serverInfo = indirectServerInfoList.get(0);
1550 indirectDhcpServerConnectPoint = serverInfo.getDhcpServerConnectPoint().orElse(null);
1551 indirectDhcpConnectVlan = serverInfo.getDhcpConnectVlan().orElse(null);
1552 } else {
1553 return getServerInterface();
1554 }
1555 if (indirectDhcpServerConnectPoint == null || indirectDhcpConnectVlan == null) {
1556 log.info("Indirect DHCP server {} not resolve yet", serverInfo.getDhcpGatewayIp6());
1557 return null;
1558 }
1559 return interfaceService.getInterfacesByPort(indirectDhcpServerConnectPoint)
1560 .stream()
Taras Lemkin96a0d342018-03-26 14:52:58 +00001561 .filter(iface -> Dhcp6HandlerUtil.interfaceContainsVlan(iface, indirectDhcpConnectVlan))
Lior Assoulinea21c0ca2018-01-28 16:18:48 -08001562 .findFirst()
1563 .orElse(null);
1564 }
1565
Kalhee Kim45b24182017-10-18 18:30:23 +00001566 /**
Kalhee Kim495c9b22017-11-07 16:32:09 +00001567 * Checks if serverInfo's host info (mac and vlan) is filled in; if not, fills in.
1568 *
1569 * @param serverInfo server information
1570 * @return newServerInfo if host info can be either found or filled in.
1571 */
1572 private DhcpServerInfo getHostInfoForServerInfo(DhcpServerInfo serverInfo, List<DhcpServerInfo> sererInfoList) {
1573 DhcpServerInfo newServerInfo = null;
1574 MacAddress dhcpServerConnectMac = serverInfo.getDhcpConnectMac().orElse(null);
1575 VlanId dhcpConnectVlan = serverInfo.getDhcpConnectVlan().orElse(null);
1576 ConnectPoint dhcpServerConnectPoint = serverInfo.getDhcpServerConnectPoint().orElse(null);
1577
1578 if (dhcpServerConnectMac != null && dhcpConnectVlan != null) {
1579 newServerInfo = serverInfo;
Charles Chan750c9992018-02-26 10:44:49 -08001580 log.debug("DHCP server {} host info found. ConnectPt{} Mac {} vlan {}", serverInfo.getDhcpServerIp6(),
Kalhee Kim495c9b22017-11-07 16:32:09 +00001581 dhcpServerConnectPoint, dhcpServerConnectMac, dhcpConnectVlan);
1582 } else {
1583 log.warn("DHCP server {} not resolve yet connectPt {} mac {} vlan {}", serverInfo.getDhcpServerIp6(),
1584 dhcpServerConnectPoint, dhcpServerConnectMac, dhcpConnectVlan);
1585
1586 Ip6Address ipToProbe;
1587 if (serverInfo.getDhcpGatewayIp6().isPresent()) {
1588 ipToProbe = serverInfo.getDhcpGatewayIp6().get();
1589 } else {
1590 ipToProbe = serverInfo.getDhcpServerIp6().orElse(null);
1591 }
1592 String hostToProbe = serverInfo.getDhcpGatewayIp6()
1593 .map(ip -> "gateway").orElse("server");
1594
1595 log.info("Dynamically probing to resolve {} IP {}", hostToProbe, ipToProbe);
1596 hostService.startMonitoringIp(ipToProbe);
1597
1598 Set<Host> hosts = hostService.getHostsByIp(ipToProbe);
1599 if (!hosts.isEmpty()) {
1600 int serverInfoIndex = sererInfoList.indexOf(serverInfo);
1601 Host host = hosts.iterator().next();
1602 serverInfo.setDhcpConnectVlan(host.vlan());
1603 serverInfo.setDhcpConnectMac(host.mac());
1604 // replace the serverInfo in the list
1605 sererInfoList.set(serverInfoIndex, serverInfo);
1606 newServerInfo = serverInfo;
1607 log.warn("Dynamically host found host {}", host);
1608 } else {
1609 log.warn("No host found host ip {} dynamically", ipToProbe);
1610 }
1611 }
1612 return newServerInfo;
1613 }
1614
1615 /**
Kalhee Kim45b24182017-10-18 18:30:23 +00001616 * Gets Interface facing to the server for default host.
1617 *
Kalhee Kim495c9b22017-11-07 16:32:09 +00001618 * @param serverInfo server information
Kalhee Kim45b24182017-10-18 18:30:23 +00001619 * @return the Interface facing to the server; null if not found
1620 */
Kalhee Kim495c9b22017-11-07 16:32:09 +00001621 private Interface getServerInterface(DhcpServerInfo serverInfo) {
1622 Interface serverInterface = null;
Yi Tseng25bfe372017-11-03 16:27:32 -07001623
Kalhee Kim495c9b22017-11-07 16:32:09 +00001624 ConnectPoint dhcpServerConnectPoint = serverInfo.getDhcpServerConnectPoint().orElse(null);
1625 VlanId dhcpConnectVlan = serverInfo.getDhcpConnectVlan().orElse(null);
1626
1627 if (dhcpServerConnectPoint != null && dhcpConnectVlan != null) {
1628 serverInterface = interfaceService.getInterfacesByPort(dhcpServerConnectPoint)
1629 .stream()
Taras Lemkin96a0d342018-03-26 14:52:58 +00001630 .filter(iface -> Dhcp6HandlerUtil.interfaceContainsVlan(iface, dhcpConnectVlan))
Kalhee Kim495c9b22017-11-07 16:32:09 +00001631 .findFirst()
1632 .orElse(null);
Yi Tseng25bfe372017-11-03 16:27:32 -07001633 } else {
Kalhee Kim495c9b22017-11-07 16:32:09 +00001634 log.warn("DHCP server {} not resolve yet connectPoint {} vlan {}", serverInfo.getDhcpServerIp6(),
1635 dhcpServerConnectPoint, dhcpConnectVlan);
Kalhee Kim45b24182017-10-18 18:30:23 +00001636 }
Kalhee Kim495c9b22017-11-07 16:32:09 +00001637
1638 return serverInterface;
Kalhee Kim45b24182017-10-18 18:30:23 +00001639 }
1640
Yi Tseng525ff402017-10-23 19:39:39 -07001641 private void requestDhcpPacket(Ip6Address serverIp) {
1642 requestServerDhcpPacket(serverIp);
1643 requestClientDhcpPacket(serverIp);
Lior Assoulinea21c0ca2018-01-28 16:18:48 -08001644 requestServerLQPacket(serverIp);
Yi Tseng525ff402017-10-23 19:39:39 -07001645 }
1646
1647 private void cancelDhcpPacket(Ip6Address serverIp) {
1648 cancelServerDhcpPacket(serverIp);
1649 cancelClientDhcpPacket(serverIp);
Lior Assoulinea21c0ca2018-01-28 16:18:48 -08001650 cancelServerLQPacket(serverIp);
Yi Tseng525ff402017-10-23 19:39:39 -07001651 }
1652
1653 private void cancelServerDhcpPacket(Ip6Address serverIp) {
1654 TrafficSelector serverSelector =
1655 DefaultTrafficSelector.builder(SERVER_RELAY_SELECTOR)
1656 .matchIPv6Src(serverIp.toIpPrefix())
1657 .build();
1658 packetService.cancelPackets(serverSelector,
1659 PacketPriority.CONTROL,
1660 appId);
1661 }
1662
Lior Assoulinea21c0ca2018-01-28 16:18:48 -08001663 private void cancelServerLQPacket(Ip6Address serverIp) {
1664 TrafficSelector serverSelector =
1665 DefaultTrafficSelector.builder(LEASE_QUERY_RESPONSE_SELECTOR)
1666 .matchIPv6Src(serverIp.toIpPrefix())
1667 .build();
1668 packetService.cancelPackets(serverSelector,
1669 PacketPriority.CONTROL,
1670 appId);
1671 }
1672
Yi Tseng525ff402017-10-23 19:39:39 -07001673 private void requestServerDhcpPacket(Ip6Address serverIp) {
1674 TrafficSelector serverSelector =
1675 DefaultTrafficSelector.builder(SERVER_RELAY_SELECTOR)
1676 .matchIPv6Src(serverIp.toIpPrefix())
1677 .build();
1678 packetService.requestPackets(serverSelector,
1679 PacketPriority.CONTROL,
1680 appId);
1681 }
1682
Lior Assoulinea21c0ca2018-01-28 16:18:48 -08001683 private void requestServerLQPacket(Ip6Address serverIp) {
1684 TrafficSelector serverSelector =
1685 DefaultTrafficSelector.builder(LEASE_QUERY_RESPONSE_SELECTOR)
1686 .matchIPv6Src(serverIp.toIpPrefix())
1687 .build();
1688 packetService.requestPackets(serverSelector,
1689 PacketPriority.CONTROL,
1690 appId);
1691 }
1692
Yi Tseng525ff402017-10-23 19:39:39 -07001693 private void cancelClientDhcpPacket(Ip6Address serverIp) {
1694 // Packet comes from relay
1695 TrafficSelector indirectClientSelector =
1696 DefaultTrafficSelector.builder(SERVER_RELAY_SELECTOR)
1697 .matchIPv6Dst(serverIp.toIpPrefix())
1698 .build();
1699 packetService.cancelPackets(indirectClientSelector,
1700 PacketPriority.CONTROL,
1701 appId);
Yi Tseng41dde702017-11-02 15:21:10 -07001702 indirectClientSelector =
1703 DefaultTrafficSelector.builder(SERVER_RELAY_SELECTOR)
1704 .matchIPv6Dst(Ip6Address.ALL_DHCP_RELAY_AGENTS_AND_SERVERS.toIpPrefix())
1705 .build();
1706 packetService.cancelPackets(indirectClientSelector,
1707 PacketPriority.CONTROL,
1708 appId);
1709 indirectClientSelector =
1710 DefaultTrafficSelector.builder(SERVER_RELAY_SELECTOR)
1711 .matchIPv6Dst(Ip6Address.ALL_DHCP_SERVERS.toIpPrefix())
1712 .build();
1713 packetService.cancelPackets(indirectClientSelector,
1714 PacketPriority.CONTROL,
1715 appId);
Yi Tseng525ff402017-10-23 19:39:39 -07001716
1717 // Packet comes from client
1718 packetService.cancelPackets(CLIENT_SERVER_SELECTOR,
1719 PacketPriority.CONTROL,
1720 appId);
1721 }
1722
1723 private void requestClientDhcpPacket(Ip6Address serverIp) {
1724 // Packet comes from relay
1725 TrafficSelector indirectClientSelector =
1726 DefaultTrafficSelector.builder(SERVER_RELAY_SELECTOR)
1727 .matchIPv6Dst(serverIp.toIpPrefix())
1728 .build();
1729 packetService.requestPackets(indirectClientSelector,
1730 PacketPriority.CONTROL,
1731 appId);
Yi Tseng41dde702017-11-02 15:21:10 -07001732 indirectClientSelector =
1733 DefaultTrafficSelector.builder(SERVER_RELAY_SELECTOR)
1734 .matchIPv6Dst(Ip6Address.ALL_DHCP_RELAY_AGENTS_AND_SERVERS.toIpPrefix())
1735 .build();
1736 packetService.requestPackets(indirectClientSelector,
1737 PacketPriority.CONTROL,
1738 appId);
1739 indirectClientSelector =
1740 DefaultTrafficSelector.builder(SERVER_RELAY_SELECTOR)
1741 .matchIPv6Dst(Ip6Address.ALL_DHCP_SERVERS.toIpPrefix())
1742 .build();
1743 packetService.requestPackets(indirectClientSelector,
1744 PacketPriority.CONTROL,
1745 appId);
Yi Tseng525ff402017-10-23 19:39:39 -07001746
1747 // Packet comes from client
1748 packetService.requestPackets(CLIENT_SERVER_SELECTOR,
1749 PacketPriority.CONTROL,
1750 appId);
1751 }
1752
1753 /**
1754 * Process the ignore rules.
1755 *
1756 * @param deviceId the device id
1757 * @param vlanId the vlan to be ignored
1758 * @param op the operation, ADD to install; REMOVE to uninstall rules
1759 */
1760 private void processIgnoreVlanRule(DeviceId deviceId, VlanId vlanId, Objective.Operation op) {
Yi Tseng525ff402017-10-23 19:39:39 -07001761 AtomicInteger installedCount = new AtomicInteger(DHCP_SELECTORS.size());
1762 DHCP_SELECTORS.forEach(trafficSelector -> {
1763 TrafficSelector selector = DefaultTrafficSelector.builder(trafficSelector)
1764 .matchVlanId(vlanId)
1765 .build();
1766
1767 ForwardingObjective.Builder builder = DefaultForwardingObjective.builder()
1768 .withFlag(ForwardingObjective.Flag.VERSATILE)
1769 .withSelector(selector)
1770 .withPriority(IGNORE_CONTROL_PRIORITY)
Yi Tsengdbabeed2017-11-29 10:49:20 -08001771 .withTreatment(DefaultTrafficTreatment.emptyTreatment())
Yi Tseng525ff402017-10-23 19:39:39 -07001772 .fromApp(appId);
1773
1774
1775 ObjectiveContext objectiveContext = new ObjectiveContext() {
1776 @Override
1777 public void onSuccess(Objective objective) {
1778 log.info("Ignore rule {} (Vlan id {}, device {}, selector {})",
1779 op, vlanId, deviceId, selector);
1780 int countDown = installedCount.decrementAndGet();
1781 if (countDown != 0) {
1782 return;
1783 }
1784 switch (op) {
1785 case ADD:
1786 ignoredVlans.put(deviceId, vlanId);
1787 break;
1788 case REMOVE:
1789 ignoredVlans.remove(deviceId, vlanId);
1790 break;
1791 default:
1792 log.warn("Unsupported objective operation {}", op);
1793 break;
1794 }
1795 }
1796
1797 @Override
1798 public void onError(Objective objective, ObjectiveError error) {
1799 log.warn("Can't {} ignore rule (vlan id {}, selector {}, device {}) due to {}",
1800 op, vlanId, selector, deviceId, error);
1801 }
1802 };
1803
1804 ForwardingObjective fwd;
1805 switch (op) {
1806 case ADD:
1807 fwd = builder.add(objectiveContext);
1808 break;
1809 case REMOVE:
1810 fwd = builder.remove(objectiveContext);
1811 break;
1812 default:
1813 log.warn("Unsupported objective operation {}", op);
1814 return;
1815 }
1816
1817 Device device = deviceService.getDevice(deviceId);
1818 if (device == null || !device.is(Pipeliner.class)) {
1819 log.warn("Device {} is not available now, wait until device is available", deviceId);
1820 return;
1821 }
1822 flowObjectiveService.apply(deviceId, fwd);
1823 });
1824 }
Lior Assoulinea21c0ca2018-01-28 16:18:48 -08001825
Kalhee Kim121ba922017-11-01 17:56:44 +00001826 /**
1827 * Find first ipaddress for a given Host info i.e. mac and vlan.
1828 *
1829 * @param clientMac client mac
1830 * @param vlanId packet's vlan
1831 * @return next-hop link-local ipaddress for a given host
1832 */
Kalhee Kim495c9b22017-11-07 16:32:09 +00001833 private IpAddress getFirstIpByHost(Boolean directConnFlag, MacAddress clientMac, VlanId vlanId) {
Kalhee Kim121ba922017-11-01 17:56:44 +00001834 IpAddress nextHopIp;
1835 // pick out the first link-local ip address
1836 HostId gwHostId = HostId.hostId(clientMac, vlanId);
1837 Host gwHost = hostService.getHost(gwHostId);
1838 if (gwHost == null) {
1839 log.warn("Can't find gateway host for hostId {}", gwHostId);
1840 return null;
1841 }
Kalhee Kim495c9b22017-11-07 16:32:09 +00001842 if (directConnFlag) {
1843 nextHopIp = gwHost.ipAddresses()
1844 .stream()
1845 .filter(IpAddress::isIp6)
1846 .map(IpAddress::getIp6Address)
1847 .findFirst()
1848 .orElse(null);
1849 } else {
1850 nextHopIp = gwHost.ipAddresses()
1851 .stream()
1852 .filter(IpAddress::isIp6)
1853 .filter(ip6 -> ip6.isLinkLocal())
1854 .map(IpAddress::getIp6Address)
1855 .findFirst()
1856 .orElse(null);
1857 }
Kalhee Kim121ba922017-11-01 17:56:44 +00001858 return nextHopIp;
1859 }
Kalhee Kimea4b6c22017-11-09 14:38:37 +00001860
Kalhee Kim495c9b22017-11-07 16:32:09 +00001861 private List<DhcpServerInfo> findValidServerInfo(boolean directConnFlag) {
1862 List<DhcpServerInfo> validServerInfo;
1863
1864 if (directConnFlag || indirectServerInfoList.isEmpty()) {
1865 validServerInfo = new ArrayList<DhcpServerInfo>(defaultServerInfoList);
1866 } else {
1867 validServerInfo = new ArrayList<DhcpServerInfo>(indirectServerInfoList);
1868 }
1869 return validServerInfo;
1870 }
1871
1872 private DhcpServerInfo findServerInfoFromServer(boolean directConnFlag, ConnectPoint inPort) {
1873 List<DhcpServerInfo> validServerInfoList = findValidServerInfo(directConnFlag);
1874 DhcpServerInfo foundServerInfo = null;
1875 for (DhcpServerInfo serverInfo : validServerInfoList) {
1876 if (inPort.equals(serverInfo.getDhcpServerConnectPoint().get())) {
1877 foundServerInfo = serverInfo;
Charles Chan750c9992018-02-26 10:44:49 -08001878 log.debug("ServerInfo found for Rcv port {} Server Connect Point {} for {}",
Kalhee Kim495c9b22017-11-07 16:32:09 +00001879 inPort, serverInfo.getDhcpServerConnectPoint(), directConnFlag ? "direct" : "indirect");
1880 break;
1881 } else {
1882 log.warn("Rcv port {} not the same as Server Connect Point {} for {}",
1883 inPort, serverInfo.getDhcpServerConnectPoint(), directConnFlag ? "direct" : "indirect");
1884 }
1885 }
1886 return foundServerInfo;
1887 }
Lior Assoulinea21c0ca2018-01-28 16:18:48 -08001888
Kalhee Kim495c9b22017-11-07 16:32:09 +00001889 /**
1890 * Set the dhcp6 lease expiry poll interval value.
1891 *
1892 * @param val poll interval value in seconds
1893 */
Kalhee Kim2f07c602017-11-15 18:57:53 +00001894 @Override
Kalhee Kim495c9b22017-11-07 16:32:09 +00001895 public void setDhcp6PollInterval(int val) {
1896 dhcp6PollInterval = val;
1897 }
1898
Kalhee Kim2f07c602017-11-15 18:57:53 +00001899 /**
1900 * get the dhcp6 lease expiry poll interval value.
1901 * This is a private function
1902 * @return poll interval value in seconds
1903 */
1904 private int getDhcp6PollInterval() {
1905 return dhcp6PollInterval;
1906 }
1907
1908 /**
1909 * Find lease-expired ipaddresses and pd prefixes.
1910 * Removing host/route/fpm entries.
1911 */
1912 public void timeTick() {
1913 long currentTime = System.currentTimeMillis();
1914 Collection<DhcpRecord> records = dhcpRelayStore.getDhcpRecords();
1915
1916 log.debug("timeTick called currenttime {} records num {} ", currentTime, records.size());
1917
1918 records.forEach(record -> {
1919 boolean addrOrPdRemoved = false;
1920 DHCP6.MsgType ip6Status = record.ip6Status().orElse(null);
1921 if (ip6Status == null) {
1922 log.debug("record is not valid v6 record.");
1923 return;
1924 }
1925
1926 if ((currentTime - record.getLastIp6Update()) >
1927 ((record.addrPrefTime() + getDhcp6PollInterval() / 2) * 1000)) {
1928 // remove ipaddress from host/route table
1929 IpAddress ip = record.ip6Address().orElse(null);
1930 if (ip != null) {
1931 if (record.directlyConnected()) {
1932 providerService.removeIpFromHost(HostId.hostId(record.macAddress(),
1933 record.vlanId()), ip);
1934 } else {
1935 MacAddress gwMac = record.nextHop().orElse(null);
1936 if (gwMac == null) {
1937 log.warn("Can't find gateway mac address from record {} for ip6Addr", record);
1938 return;
1939 }
1940 IpAddress nextHopIp = getFirstIpByHost(record.directlyConnected(),
1941 gwMac,
1942 record.vlanId());
Kalhee Kim54a97c92018-04-02 21:23:46 +00001943 Route route = new Route(Route.Source.DHCP, ip.toIpPrefix(), nextHopIp);
Kalhee Kim2f07c602017-11-15 18:57:53 +00001944 routeStore.removeRoute(route);
1945 }
1946 record.updateAddrPrefTime(0);
1947 record.ip6Address(null);
1948 addrOrPdRemoved = true;
1949 dhcpRelayStore.updateDhcpRecord(HostId.hostId(record.macAddress(),
1950 record.vlanId()), record);
1951 log.warn("IP6 address is set to null. delta {} lastUpdate {} addrPrefTime {}",
1952 (currentTime - record.getLastIp6Update()), record.getLastIp6Update(),
1953 record.addrPrefTime());
1954 }
1955 }
1956 if ((currentTime - record.getLastPdUpdate()) >
1957 ((record.pdPrefTime() + getDhcp6PollInterval() / 2) * 1000)) {
1958 // remove PD from route/fpm table
1959 IpPrefix pdIpPrefix = record.pdPrefix().orElse(null);
1960 if (pdIpPrefix != null) {
1961 if (record.directlyConnected()) {
1962 providerService.removeIpFromHost(HostId.hostId(record.macAddress(), record.vlanId()),
1963 pdIpPrefix.address().getIp6Address());
1964 } else {
1965 MacAddress gwMac = record.nextHop().orElse(null);
1966 if (gwMac == null) {
1967 log.warn("Can't find gateway mac address from record {} for PD prefix", record);
1968 return;
1969 }
1970 IpAddress nextHopIp = getFirstIpByHost(record.directlyConnected(),
1971 gwMac,
1972 record.vlanId());
Kalhee Kim54a97c92018-04-02 21:23:46 +00001973 Route route = new Route(Route.Source.DHCP, pdIpPrefix, nextHopIp);
Kalhee Kim2f07c602017-11-15 18:57:53 +00001974 routeStore.removeRoute(route);
1975 if (this.dhcpFpmEnabled) {
1976 dhcpFpmPrefixStore.removeFpmRecord(pdIpPrefix);
1977 }
1978 }
1979 record.updatePdPrefTime(0);
1980 record.pdPrefix(null);
1981 addrOrPdRemoved = true;
1982 dhcpRelayStore.updateDhcpRecord(HostId.hostId(record.macAddress(),
1983 record.vlanId()), record);
1984 log.warn("PD prefix is set to null.delta {} pdPrefTime {}",
1985 (currentTime - record.getLastPdUpdate()), record.pdPrefTime());
1986 }
1987 }
1988 if (addrOrPdRemoved &&
1989 !record.ip6Address().isPresent() && !record.pdPrefix().isPresent()) {
1990 log.warn("ip6Status {} IP6 address and IP6 PD both are null. Remove record.", ip6Status);
1991 dhcpRelayStore.removeDhcpRecord(HostId.hostId(record.macAddress(), record.vlanId()));
1992 }
1993 }
1994 );
1995 }
1996}