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