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