Jayakumar Thazhath | 3ec6aa2 | 2017-09-11 02:00:20 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2017-present Open Networking Foundation |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package org.onosproject.ra; |
| 18 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 19 | import com.google.common.collect.ImmutableMap; |
Jayakumar Thazhath | 3ec6aa2 | 2017-09-11 02:00:20 -0400 | [diff] [blame] | 20 | import org.onlab.packet.EthType; |
| 21 | import org.onlab.packet.Ethernet; |
| 22 | import org.onlab.packet.ICMP6; |
| 23 | import org.onlab.packet.IPv6; |
Jayakumar Thazhath | 3ec6aa2 | 2017-09-11 02:00:20 -0400 | [diff] [blame] | 24 | import org.onlab.packet.Ip6Address; |
Jayakumar Thazhath | e166a4e | 2017-11-15 02:17:04 -0500 | [diff] [blame] | 25 | import org.onlab.packet.IpAddress; |
Jayakumar Thazhath | 3ec6aa2 | 2017-09-11 02:00:20 -0400 | [diff] [blame] | 26 | import org.onlab.packet.MacAddress; |
Jayakumar Thazhath | 3ec6aa2 | 2017-09-11 02:00:20 -0400 | [diff] [blame] | 27 | import org.onlab.packet.ndp.NeighborDiscoveryOptions; |
Jayakumar Thazhath | e166a4e | 2017-11-15 02:17:04 -0500 | [diff] [blame] | 28 | import org.onlab.packet.ndp.RouterAdvertisement; |
Jayakumar Thazhath | 3ec6aa2 | 2017-09-11 02:00:20 -0400 | [diff] [blame] | 29 | import org.onosproject.cfg.ComponentConfigService; |
| 30 | import org.onosproject.core.ApplicationId; |
| 31 | import org.onosproject.core.CoreService; |
Charles Chan | bac954f9 | 2018-04-06 22:29:26 -0700 | [diff] [blame] | 32 | import org.onosproject.mastership.MastershipEvent; |
| 33 | import org.onosproject.mastership.MastershipListener; |
Jayakumar Thazhath | 3ec6aa2 | 2017-09-11 02:00:20 -0400 | [diff] [blame] | 34 | import org.onosproject.mastership.MastershipService; |
Jayakumar Thazhath | e166a4e | 2017-11-15 02:17:04 -0500 | [diff] [blame] | 35 | import org.onosproject.net.ConnectPoint; |
Jayakumar Thazhath | 2848457 | 2017-11-02 00:39:09 -0400 | [diff] [blame] | 36 | import org.onosproject.net.DeviceId; |
Jayakumar Thazhath | 3ec6aa2 | 2017-09-11 02:00:20 -0400 | [diff] [blame] | 37 | import org.onosproject.net.MastershipRole; |
Jayakumar Thazhath | 2848457 | 2017-11-02 00:39:09 -0400 | [diff] [blame] | 38 | import org.onosproject.net.config.ConfigFactory; |
| 39 | import org.onosproject.net.config.NetworkConfigEvent; |
| 40 | import org.onosproject.net.config.NetworkConfigListener; |
| 41 | import org.onosproject.net.config.NetworkConfigRegistry; |
Jayakumar Thazhath | e166a4e | 2017-11-15 02:17:04 -0500 | [diff] [blame] | 42 | import org.onosproject.net.config.basics.InterfaceConfig; |
Jayakumar Thazhath | 2848457 | 2017-11-02 00:39:09 -0400 | [diff] [blame] | 43 | import org.onosproject.net.config.basics.SubjectFactories; |
Jayakumar Thazhath | e166a4e | 2017-11-15 02:17:04 -0500 | [diff] [blame] | 44 | import org.onosproject.net.device.DeviceEvent; |
| 45 | import org.onosproject.net.device.DeviceListener; |
Jayakumar Thazhath | 2848457 | 2017-11-02 00:39:09 -0400 | [diff] [blame] | 46 | import org.onosproject.net.device.DeviceService; |
Jayakumar Thazhath | e166a4e | 2017-11-15 02:17:04 -0500 | [diff] [blame] | 47 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
| 48 | import org.onosproject.net.flow.TrafficTreatment; |
| 49 | import org.onosproject.net.host.InterfaceIpAddress; |
Jayakumar Thazhath | 3ec6aa2 | 2017-09-11 02:00:20 -0400 | [diff] [blame] | 50 | import org.onosproject.net.intf.Interface; |
| 51 | import org.onosproject.net.intf.InterfaceEvent; |
| 52 | import org.onosproject.net.intf.InterfaceListener; |
| 53 | import org.onosproject.net.intf.InterfaceService; |
Jayakumar Thazhath | 3ec6aa2 | 2017-09-11 02:00:20 -0400 | [diff] [blame] | 54 | import org.onosproject.net.packet.DefaultOutboundPacket; |
Jayakumar Thazhath | e166a4e | 2017-11-15 02:17:04 -0500 | [diff] [blame] | 55 | import org.onosproject.net.packet.InboundPacket; |
Jayakumar Thazhath | 3ec6aa2 | 2017-09-11 02:00:20 -0400 | [diff] [blame] | 56 | import org.onosproject.net.packet.OutboundPacket; |
Jayakumar Thazhath | e166a4e | 2017-11-15 02:17:04 -0500 | [diff] [blame] | 57 | import org.onosproject.net.packet.PacketContext; |
| 58 | import org.onosproject.net.packet.PacketProcessor; |
Jayakumar Thazhath | 3ec6aa2 | 2017-09-11 02:00:20 -0400 | [diff] [blame] | 59 | import org.onosproject.net.packet.PacketService; |
Jayakumar Thazhath | 2848457 | 2017-11-02 00:39:09 -0400 | [diff] [blame] | 60 | import org.onosproject.ra.config.RouterAdvertisementDeviceConfig; |
Jayakumar Thazhath | e166a4e | 2017-11-15 02:17:04 -0500 | [diff] [blame] | 61 | import org.osgi.service.component.ComponentContext; |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 62 | import org.osgi.service.component.annotations.Activate; |
| 63 | import org.osgi.service.component.annotations.Component; |
| 64 | import org.osgi.service.component.annotations.Deactivate; |
| 65 | import org.osgi.service.component.annotations.Modified; |
| 66 | import org.osgi.service.component.annotations.Reference; |
| 67 | import org.osgi.service.component.annotations.ReferenceCardinality; |
Jayakumar Thazhath | 3ec6aa2 | 2017-09-11 02:00:20 -0400 | [diff] [blame] | 68 | import org.slf4j.Logger; |
| 69 | import org.slf4j.LoggerFactory; |
Jayakumar Thazhath | 3ec6aa2 | 2017-09-11 02:00:20 -0400 | [diff] [blame] | 70 | |
| 71 | import javax.annotation.concurrent.GuardedBy; |
| 72 | import java.nio.ByteBuffer; |
Jayakumar Thazhath | e166a4e | 2017-11-15 02:17:04 -0500 | [diff] [blame] | 73 | import java.util.AbstractMap; |
| 74 | import java.util.Arrays; |
Jayakumar Thazhath | 3ec6aa2 | 2017-09-11 02:00:20 -0400 | [diff] [blame] | 75 | import java.util.Dictionary; |
| 76 | import java.util.LinkedHashMap; |
| 77 | import java.util.List; |
| 78 | import java.util.Map; |
Jayakumar Thazhath | 3ec6aa2 | 2017-09-11 02:00:20 -0400 | [diff] [blame] | 79 | import java.util.Optional; |
Jayakumar Thazhath | 2848457 | 2017-11-02 00:39:09 -0400 | [diff] [blame] | 80 | import java.util.Set; |
Jayakumar Thazhath | e166a4e | 2017-11-15 02:17:04 -0500 | [diff] [blame] | 81 | import java.util.concurrent.CompletableFuture; |
| 82 | import java.util.concurrent.Executors; |
Jayakumar Thazhath | 3ec6aa2 | 2017-09-11 02:00:20 -0400 | [diff] [blame] | 83 | import java.util.concurrent.ScheduledExecutorService; |
| 84 | import java.util.concurrent.ScheduledFuture; |
Jayakumar Thazhath | 3ec6aa2 | 2017-09-11 02:00:20 -0400 | [diff] [blame] | 85 | import java.util.concurrent.TimeUnit; |
Jayakumar Thazhath | 2848457 | 2017-11-02 00:39:09 -0400 | [diff] [blame] | 86 | import java.util.function.Function; |
Jayakumar Thazhath | 3ec6aa2 | 2017-09-11 02:00:20 -0400 | [diff] [blame] | 87 | import java.util.stream.IntStream; |
| 88 | |
| 89 | import static com.google.common.base.Strings.isNullOrEmpty; |
| 90 | import static org.onlab.util.Tools.get; |
| 91 | import static org.onlab.util.Tools.groupedThreads; |
Ray Milkey | 8e40651 | 2018-10-24 15:56:50 -0700 | [diff] [blame] | 92 | import static org.onosproject.ra.OsgiPropertyConstants.RA_FLAG_MBIT_STATUS; |
| 93 | import static org.onosproject.ra.OsgiPropertyConstants.RA_FLAG_MBIT_STATUS_DEFAULT; |
| 94 | import static org.onosproject.ra.OsgiPropertyConstants.RA_FLAG_OBIT_STATUS; |
| 95 | import static org.onosproject.ra.OsgiPropertyConstants.RA_FLAG_OBIT_STATUS_DEFAULT; |
| 96 | import static org.onosproject.ra.OsgiPropertyConstants.RA_GLOBAL_PREFIX_CONF_STATUS; |
| 97 | import static org.onosproject.ra.OsgiPropertyConstants.RA_GLOBAL_PREFIX_CONF_STATUS_DEFAULT; |
| 98 | import static org.onosproject.ra.OsgiPropertyConstants.RA_OPTION_PREFIX_STATUS; |
| 99 | import static org.onosproject.ra.OsgiPropertyConstants.RA_OPTION_PREFIX_STATUS_DEFAULT; |
| 100 | import static org.onosproject.ra.OsgiPropertyConstants.RA_THREADS_DELAY; |
| 101 | import static org.onosproject.ra.OsgiPropertyConstants.RA_THREADS_DELAY_DEFAULT; |
| 102 | import static org.onosproject.ra.OsgiPropertyConstants.RA_THREADS_POOL; |
| 103 | import static org.onosproject.ra.OsgiPropertyConstants.RA_THREADS_POOL_SIZE_DEFAULT; |
Jayakumar Thazhath | 3ec6aa2 | 2017-09-11 02:00:20 -0400 | [diff] [blame] | 104 | |
| 105 | /** |
| 106 | * Manages IPv6 Router Advertisements. |
| 107 | */ |
Ray Milkey | 8e40651 | 2018-10-24 15:56:50 -0700 | [diff] [blame] | 108 | @Component( |
| 109 | immediate = true, |
| 110 | service = RoutingAdvertisementService.class, |
| 111 | property = { |
| 112 | RA_THREADS_POOL + ":Integer=" + RA_THREADS_POOL_SIZE_DEFAULT, |
| 113 | RA_THREADS_DELAY + ":Integer=" + RA_THREADS_DELAY_DEFAULT, |
| 114 | RA_FLAG_MBIT_STATUS + ":Boolean=" + RA_FLAG_MBIT_STATUS_DEFAULT, |
| 115 | RA_FLAG_OBIT_STATUS + ":Boolean=" + RA_FLAG_OBIT_STATUS_DEFAULT, |
| 116 | RA_OPTION_PREFIX_STATUS + ":Boolean=" + RA_OPTION_PREFIX_STATUS_DEFAULT, |
| 117 | RA_GLOBAL_PREFIX_CONF_STATUS + ":Boolean=" + RA_GLOBAL_PREFIX_CONF_STATUS_DEFAULT |
| 118 | } |
| 119 | ) |
Annce John | 0b4057a | 2017-11-21 01:46:41 -0500 | [diff] [blame] | 120 | public class RouterAdvertisementManager implements RoutingAdvertisementService { |
Jayakumar Thazhath | 3ec6aa2 | 2017-09-11 02:00:20 -0400 | [diff] [blame] | 121 | |
| 122 | private final Logger log = LoggerFactory.getLogger(getClass()); |
Jayakumar Thazhath | 3ec6aa2 | 2017-09-11 02:00:20 -0400 | [diff] [blame] | 123 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 124 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Jayakumar Thazhath | 3ec6aa2 | 2017-09-11 02:00:20 -0400 | [diff] [blame] | 125 | protected CoreService coreService; |
| 126 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 127 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Jayakumar Thazhath | 3ec6aa2 | 2017-09-11 02:00:20 -0400 | [diff] [blame] | 128 | PacketService packetService; |
| 129 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 130 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Jayakumar Thazhath | 3ec6aa2 | 2017-09-11 02:00:20 -0400 | [diff] [blame] | 131 | protected ComponentConfigService componentConfigService; |
| 132 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 133 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Jayakumar Thazhath | 3ec6aa2 | 2017-09-11 02:00:20 -0400 | [diff] [blame] | 134 | public InterfaceService interfaceService; |
| 135 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 136 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Jayakumar Thazhath | 3ec6aa2 | 2017-09-11 02:00:20 -0400 | [diff] [blame] | 137 | public MastershipService mastershipService; |
| 138 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 139 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Jayakumar Thazhath | e166a4e | 2017-11-15 02:17:04 -0500 | [diff] [blame] | 140 | protected NetworkConfigRegistry networkConfigRegistry; |
Jayakumar Thazhath | 2848457 | 2017-11-02 00:39:09 -0400 | [diff] [blame] | 141 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 142 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Jayakumar Thazhath | e166a4e | 2017-11-15 02:17:04 -0500 | [diff] [blame] | 143 | protected DeviceService deviceService; |
Jayakumar Thazhath | 2848457 | 2017-11-02 00:39:09 -0400 | [diff] [blame] | 144 | |
Ray Milkey | 8e40651 | 2018-10-24 15:56:50 -0700 | [diff] [blame] | 145 | /** Thread pool capacity. */ |
| 146 | protected int raPoolSize = RA_THREADS_POOL_SIZE_DEFAULT; |
Jayakumar Thazhath | 3ec6aa2 | 2017-09-11 02:00:20 -0400 | [diff] [blame] | 147 | |
Ray Milkey | 8e40651 | 2018-10-24 15:56:50 -0700 | [diff] [blame] | 148 | /** Thread delay in seconds. */ |
| 149 | protected int raThreadDelay = RA_THREADS_DELAY_DEFAULT; |
Jayakumar Thazhath | 3ec6aa2 | 2017-09-11 02:00:20 -0400 | [diff] [blame] | 150 | |
Ray Milkey | 8e40651 | 2018-10-24 15:56:50 -0700 | [diff] [blame] | 151 | /** Turn M-bit flag on/off. */ |
| 152 | protected boolean raFlagMbitStatus = RA_FLAG_MBIT_STATUS_DEFAULT; |
jayakumarthazhath | 1cbde73 | 2017-10-25 10:01:00 -0400 | [diff] [blame] | 153 | |
Ray Milkey | 8e40651 | 2018-10-24 15:56:50 -0700 | [diff] [blame] | 154 | /** Turn O-bit flag on/off. */ |
| 155 | protected boolean raFlagObitStatus = RA_FLAG_OBIT_STATUS_DEFAULT; |
jayakumarthazhath | 1cbde73 | 2017-10-25 10:01:00 -0400 | [diff] [blame] | 156 | |
Ray Milkey | 8e40651 | 2018-10-24 15:56:50 -0700 | [diff] [blame] | 157 | /** Prefix option support needed or not. */ |
| 158 | protected boolean raOptionPrefixStatus = RA_OPTION_PREFIX_STATUS_DEFAULT; |
jayakumarthazhath | 1cbde73 | 2017-10-25 10:01:00 -0400 | [diff] [blame] | 159 | |
Ray Milkey | 8e40651 | 2018-10-24 15:56:50 -0700 | [diff] [blame] | 160 | /** Global prefix configuration support on/off. */ |
| 161 | protected boolean raGlobalPrefixConfStatus = RA_GLOBAL_PREFIX_CONF_STATUS_DEFAULT; |
Jayakumar Thazhath | 3ec6aa2 | 2017-09-11 02:00:20 -0400 | [diff] [blame] | 162 | |
| 163 | @GuardedBy(value = "this") |
Jayakumar Thazhath | e166a4e | 2017-11-15 02:17:04 -0500 | [diff] [blame] | 164 | private final Map<ConnectPoint, Map.Entry<ScheduledFuture<?>, List<InterfaceIpAddress>>> transmitters = |
| 165 | new LinkedHashMap<>(); |
Jayakumar Thazhath | 3ec6aa2 | 2017-09-11 02:00:20 -0400 | [diff] [blame] | 166 | |
Yuta HIGUCHI | 488a94c | 2018-01-26 17:24:09 -0800 | [diff] [blame] | 167 | // TODO: should consider using concurrent variants |
Jayakumar Thazhath | 2848457 | 2017-11-02 00:39:09 -0400 | [diff] [blame] | 168 | @GuardedBy(value = "this") |
| 169 | private final Map<DeviceId, List<InterfaceIpAddress>> globalPrefixes = new LinkedHashMap<>(); |
| 170 | |
Annce John | 0b4057a | 2017-11-21 01:46:41 -0500 | [diff] [blame] | 171 | @Override |
Yuta HIGUCHI | 488a94c | 2018-01-26 17:24:09 -0800 | [diff] [blame] | 172 | public synchronized ImmutableMap<DeviceId, List<InterfaceIpAddress>> getGlobalPrefixes() { |
Annce John | 0b4057a | 2017-11-21 01:46:41 -0500 | [diff] [blame] | 173 | return ImmutableMap.copyOf(globalPrefixes); |
| 174 | } |
| 175 | |
Yuta HIGUCHI | 488a94c | 2018-01-26 17:24:09 -0800 | [diff] [blame] | 176 | @SuppressWarnings("GuardedBy") |
| 177 | @GuardedBy(value = "this") |
Jayakumar Thazhath | 2848457 | 2017-11-02 00:39:09 -0400 | [diff] [blame] | 178 | private Function<Interface, Map.Entry<ConnectPoint, List<InterfaceIpAddress>>> prefixGenerator = |
| 179 | i -> { |
| 180 | Map.Entry<ConnectPoint, List<InterfaceIpAddress>> prefixEntry; |
Ray Milkey | 8e40651 | 2018-10-24 15:56:50 -0700 | [diff] [blame] | 181 | if (raGlobalPrefixConfStatus && globalPrefixes.containsKey(i.connectPoint().deviceId())) { |
Jayakumar Thazhath | 2848457 | 2017-11-02 00:39:09 -0400 | [diff] [blame] | 182 | prefixEntry = new AbstractMap.SimpleEntry<>(i.connectPoint(), |
| 183 | globalPrefixes.get(i.connectPoint().deviceId())); |
| 184 | } else { |
| 185 | prefixEntry = new AbstractMap.SimpleEntry<>(i.connectPoint(), i.ipAddressesList()); |
| 186 | } |
| 187 | return prefixEntry; |
Jayakumar Thazhath | e166a4e | 2017-11-15 02:17:04 -0500 | [diff] [blame] | 188 | }; |
Jayakumar Thazhath | 2848457 | 2017-11-02 00:39:09 -0400 | [diff] [blame] | 189 | |
| 190 | private ScheduledExecutorService executors = null; |
| 191 | |
Jayakumar Thazhath | 3ec6aa2 | 2017-09-11 02:00:20 -0400 | [diff] [blame] | 192 | private static final String APP_NAME = "org.onosproject.routeradvertisement"; |
| 193 | private ApplicationId appId; |
| 194 | |
Jayakumar Thazhath | 2848457 | 2017-11-02 00:39:09 -0400 | [diff] [blame] | 195 | private final ConfigFactory<DeviceId, RouterAdvertisementDeviceConfig> deviceConfigFactory = |
| 196 | new ConfigFactory<DeviceId, RouterAdvertisementDeviceConfig>( |
| 197 | SubjectFactories.DEVICE_SUBJECT_FACTORY, |
| 198 | RouterAdvertisementDeviceConfig.class, "routeradvertisement") { |
| 199 | @Override |
| 200 | public RouterAdvertisementDeviceConfig createConfig() { |
| 201 | |
| 202 | return new RouterAdvertisementDeviceConfig(); |
| 203 | } |
| 204 | }; |
| 205 | |
Jayakumar Thazhath | 3ec6aa2 | 2017-09-11 02:00:20 -0400 | [diff] [blame] | 206 | // Listener for handling dynamic interface modifications. |
| 207 | private class InternalInterfaceListener implements InterfaceListener { |
| 208 | @Override |
| 209 | public void event(InterfaceEvent event) { |
Jayakumar Thazhath | 3ec6aa2 | 2017-09-11 02:00:20 -0400 | [diff] [blame] | 210 | switch (event.type()) { |
| 211 | case INTERFACE_ADDED: |
Jayakumar Thazhath | e166a4e | 2017-11-15 02:17:04 -0500 | [diff] [blame] | 212 | case INTERFACE_UPDATED: |
| 213 | clearTxWorkers(); |
| 214 | loadGlobalPrefixConfig(); |
| 215 | setupTxWorkers(); |
| 216 | log.info("Configuration updated for {}", event.subject()); |
Jayakumar Thazhath | 3ec6aa2 | 2017-09-11 02:00:20 -0400 | [diff] [blame] | 217 | break; |
| 218 | case INTERFACE_REMOVED: |
Jayakumar Thazhath | e166a4e | 2017-11-15 02:17:04 -0500 | [diff] [blame] | 219 | Interface i = event.subject(); |
Jayakumar Thazhath | 3ec6aa2 | 2017-09-11 02:00:20 -0400 | [diff] [blame] | 220 | if (mastershipService.getLocalRole(i.connectPoint().deviceId()) |
| 221 | == MastershipRole.MASTER) { |
Jayakumar Thazhath | e166a4e | 2017-11-15 02:17:04 -0500 | [diff] [blame] | 222 | deactivateRouterAdvertisement(i.connectPoint()); |
jayakumarthazhath | 1cbde73 | 2017-10-25 10:01:00 -0400 | [diff] [blame] | 223 | } |
Jayakumar Thazhath | 3ec6aa2 | 2017-09-11 02:00:20 -0400 | [diff] [blame] | 224 | break; |
| 225 | default: |
Jayakumar Thazhath | 3ec6aa2 | 2017-09-11 02:00:20 -0400 | [diff] [blame] | 226 | } |
| 227 | } |
| 228 | } |
Jayakumar Thazhath | 2848457 | 2017-11-02 00:39:09 -0400 | [diff] [blame] | 229 | |
Jayakumar Thazhath | 3ec6aa2 | 2017-09-11 02:00:20 -0400 | [diff] [blame] | 230 | private final InterfaceListener interfaceListener = new InternalInterfaceListener(); |
| 231 | |
| 232 | // Enables RA threads on 'connectPoint' with configured IPv6s |
jayakumarthazhath | 1cbde73 | 2017-10-25 10:01:00 -0400 | [diff] [blame] | 233 | private synchronized void activateRouterAdvertisement(ConnectPoint connectPoint, |
| 234 | List<InterfaceIpAddress> addresses) { |
Jayakumar Thazhath | e166a4e | 2017-11-15 02:17:04 -0500 | [diff] [blame] | 235 | RAWorkerThread worker = new RAWorkerThread(connectPoint, addresses, raThreadDelay, null, null); |
Jayakumar Thazhath | 2848457 | 2017-11-02 00:39:09 -0400 | [diff] [blame] | 236 | ScheduledFuture<?> handler = executors.scheduleAtFixedRate(worker, raThreadDelay, |
| 237 | raThreadDelay, TimeUnit.SECONDS); |
Jayakumar Thazhath | e166a4e | 2017-11-15 02:17:04 -0500 | [diff] [blame] | 238 | transmitters.put(connectPoint, new AbstractMap.SimpleEntry<>(handler, addresses)); |
Jayakumar Thazhath | 3ec6aa2 | 2017-09-11 02:00:20 -0400 | [diff] [blame] | 239 | } |
| 240 | |
| 241 | // Disables already activated RA threads on 'connectPoint' |
Jayakumar Thazhath | e166a4e | 2017-11-15 02:17:04 -0500 | [diff] [blame] | 242 | private synchronized List<InterfaceIpAddress> deactivateRouterAdvertisement(ConnectPoint connectPoint) { |
Jayakumar Thazhath | 2848457 | 2017-11-02 00:39:09 -0400 | [diff] [blame] | 243 | if (connectPoint != null) { |
Jayakumar Thazhath | e166a4e | 2017-11-15 02:17:04 -0500 | [diff] [blame] | 244 | Map.Entry<ScheduledFuture<?>, List<InterfaceIpAddress>> details = transmitters.get(connectPoint); |
| 245 | details.getKey().cancel(false); |
Jayakumar Thazhath | 2848457 | 2017-11-02 00:39:09 -0400 | [diff] [blame] | 246 | transmitters.remove(connectPoint); |
Jayakumar Thazhath | e166a4e | 2017-11-15 02:17:04 -0500 | [diff] [blame] | 247 | return details.getValue(); |
Jayakumar Thazhath | 2848457 | 2017-11-02 00:39:09 -0400 | [diff] [blame] | 248 | } |
Jayakumar Thazhath | e166a4e | 2017-11-15 02:17:04 -0500 | [diff] [blame] | 249 | return null; |
Jayakumar Thazhath | 3ec6aa2 | 2017-09-11 02:00:20 -0400 | [diff] [blame] | 250 | } |
| 251 | |
jayakumarthazhath | 1cbde73 | 2017-10-25 10:01:00 -0400 | [diff] [blame] | 252 | private synchronized void setupThreadPool() { |
Jayakumar Thazhath | 3ec6aa2 | 2017-09-11 02:00:20 -0400 | [diff] [blame] | 253 | executors = Executors.newScheduledThreadPool(raPoolSize, |
| 254 | groupedThreads("RouterAdvertisement", "event-%d", log)); |
jayakumarthazhath | 1cbde73 | 2017-10-25 10:01:00 -0400 | [diff] [blame] | 255 | } |
Jayakumar Thazhath | 3ec6aa2 | 2017-09-11 02:00:20 -0400 | [diff] [blame] | 256 | |
jayakumarthazhath | 1cbde73 | 2017-10-25 10:01:00 -0400 | [diff] [blame] | 257 | private synchronized void clearThreadPool() { |
jayakumarthazhath | 1cbde73 | 2017-10-25 10:01:00 -0400 | [diff] [blame] | 258 | executors.shutdown(); |
| 259 | } |
| 260 | |
Jayakumar Thazhath | 2848457 | 2017-11-02 00:39:09 -0400 | [diff] [blame] | 261 | // Start Tx threads for all configured interfaces. |
jayakumarthazhath | 1cbde73 | 2017-10-25 10:01:00 -0400 | [diff] [blame] | 262 | private synchronized void setupTxWorkers() { |
Jayakumar Thazhath | 3ec6aa2 | 2017-09-11 02:00:20 -0400 | [diff] [blame] | 263 | interfaceService.getInterfaces() |
| 264 | .stream() |
| 265 | .filter(i -> mastershipService.getLocalRole(i.connectPoint().deviceId()) |
| 266 | == MastershipRole.MASTER) |
Jayakumar Thazhath | 2848457 | 2017-11-02 00:39:09 -0400 | [diff] [blame] | 267 | .map(prefixGenerator::apply) |
| 268 | .filter(i -> i.getValue() |
Jayakumar Thazhath | 3ec6aa2 | 2017-09-11 02:00:20 -0400 | [diff] [blame] | 269 | .stream() |
| 270 | .anyMatch(ia -> ia.ipAddress().version().equals(IpAddress.Version.INET6))) |
| 271 | .forEach(j -> |
Jayakumar Thazhath | 2848457 | 2017-11-02 00:39:09 -0400 | [diff] [blame] | 272 | activateRouterAdvertisement(j.getKey(), j.getValue()) |
Jayakumar Thazhath | 3ec6aa2 | 2017-09-11 02:00:20 -0400 | [diff] [blame] | 273 | ); |
| 274 | } |
| 275 | |
Jayakumar Thazhath | 2848457 | 2017-11-02 00:39:09 -0400 | [diff] [blame] | 276 | // Clear out Tx threads. |
jayakumarthazhath | 1cbde73 | 2017-10-25 10:01:00 -0400 | [diff] [blame] | 277 | private synchronized void clearTxWorkers() { |
Jayakumar Thazhath | e166a4e | 2017-11-15 02:17:04 -0500 | [diff] [blame] | 278 | transmitters.entrySet().stream().forEach(i -> i.getValue().getKey().cancel(false)); |
Jayakumar Thazhath | 2848457 | 2017-11-02 00:39:09 -0400 | [diff] [blame] | 279 | transmitters.clear(); |
Jayakumar Thazhath | 3ec6aa2 | 2017-09-11 02:00:20 -0400 | [diff] [blame] | 280 | } |
| 281 | |
jayakumarthazhath | 1cbde73 | 2017-10-25 10:01:00 -0400 | [diff] [blame] | 282 | private synchronized void setupPoolAndTxWorkers() { |
| 283 | setupThreadPool(); |
| 284 | setupTxWorkers(); |
| 285 | } |
| 286 | |
jayakumarthazhath | 1cbde73 | 2017-10-25 10:01:00 -0400 | [diff] [blame] | 287 | private synchronized void clearPoolAndTxWorkers() { |
| 288 | clearTxWorkers(); |
| 289 | clearThreadPool(); |
| 290 | } |
| 291 | |
Yuta HIGUCHI | 488a94c | 2018-01-26 17:24:09 -0800 | [diff] [blame] | 292 | @SuppressWarnings("GuardedBy") |
Jayakumar Thazhath | 2848457 | 2017-11-02 00:39:09 -0400 | [diff] [blame] | 293 | // Loading global prefixes for devices from network configuration |
| 294 | private synchronized void loadGlobalPrefixConfig() { |
| 295 | globalPrefixes.clear(); |
| 296 | Set<DeviceId> deviceSubjects = |
| 297 | networkConfigRegistry.getSubjects(DeviceId.class, RouterAdvertisementDeviceConfig.class); |
| 298 | deviceSubjects.forEach(subject -> { |
| 299 | RouterAdvertisementDeviceConfig config = |
| 300 | networkConfigRegistry.getConfig(subject, RouterAdvertisementDeviceConfig.class); |
| 301 | if (config != null) { |
| 302 | List<InterfaceIpAddress> ips = config.prefixes(); |
| 303 | globalPrefixes.put(subject, ips); |
| 304 | } |
| 305 | }); |
| 306 | } |
| 307 | |
Jayakumar Thazhath | e166a4e | 2017-11-15 02:17:04 -0500 | [diff] [blame] | 308 | // Handler for network configuration updates |
Jayakumar Thazhath | 2848457 | 2017-11-02 00:39:09 -0400 | [diff] [blame] | 309 | private class InternalNetworkConfigListener implements NetworkConfigListener { |
| 310 | @Override |
| 311 | public void event(NetworkConfigEvent event) { |
Jayakumar Thazhath | e166a4e | 2017-11-15 02:17:04 -0500 | [diff] [blame] | 312 | if (event.configClass().equals(RouterAdvertisementDeviceConfig.class) |
| 313 | || event.configClass().equals(InterfaceConfig.class)) { |
Jayakumar Thazhath | 2848457 | 2017-11-02 00:39:09 -0400 | [diff] [blame] | 314 | switch (event.type()) { |
| 315 | case CONFIG_ADDED: |
Jayakumar Thazhath | 2848457 | 2017-11-02 00:39:09 -0400 | [diff] [blame] | 316 | case CONFIG_UPDATED: |
Jayakumar Thazhath | 2848457 | 2017-11-02 00:39:09 -0400 | [diff] [blame] | 317 | clearTxWorkers(); |
| 318 | loadGlobalPrefixConfig(); |
| 319 | setupTxWorkers(); |
Jayakumar Thazhath | e166a4e | 2017-11-15 02:17:04 -0500 | [diff] [blame] | 320 | log.info("Configuration updated for {}", event.subject()); |
Jayakumar Thazhath | 2848457 | 2017-11-02 00:39:09 -0400 | [diff] [blame] | 321 | break; |
Jayakumar Thazhath | e166a4e | 2017-11-15 02:17:04 -0500 | [diff] [blame] | 322 | default: |
Jayakumar Thazhath | 2848457 | 2017-11-02 00:39:09 -0400 | [diff] [blame] | 323 | } |
| 324 | } |
| 325 | } |
Jayakumar Thazhath | 2848457 | 2017-11-02 00:39:09 -0400 | [diff] [blame] | 326 | } |
Jayakumar Thazhath | 2848457 | 2017-11-02 00:39:09 -0400 | [diff] [blame] | 327 | |
Jayakumar Thazhath | e166a4e | 2017-11-15 02:17:04 -0500 | [diff] [blame] | 328 | private final InternalNetworkConfigListener networkConfigListener |
| 329 | = new InternalNetworkConfigListener(); |
| 330 | |
| 331 | // Handler for device updates |
| 332 | private class InternalDeviceListener implements DeviceListener { |
Jayakumar Thazhath | e166a4e | 2017-11-15 02:17:04 -0500 | [diff] [blame] | 333 | @Override |
| 334 | public void event(DeviceEvent event) { |
| 335 | switch (event.type()) { |
| 336 | case DEVICE_ADDED: |
| 337 | case PORT_UPDATED: |
| 338 | case PORT_ADDED: |
| 339 | case DEVICE_UPDATED: |
| 340 | case DEVICE_AVAILABILITY_CHANGED: |
| 341 | clearTxWorkers(); |
| 342 | setupTxWorkers(); |
| 343 | log.trace("Processed device event {} on {}", event.type(), event.subject()); |
| 344 | break; |
| 345 | default: |
| 346 | } |
| 347 | } |
| 348 | } |
| 349 | |
Charles Chan | bac954f9 | 2018-04-06 22:29:26 -0700 | [diff] [blame] | 350 | private class InternalMastershipListener implements MastershipListener { |
| 351 | @Override |
| 352 | public void event(MastershipEvent event) { |
| 353 | switch (event.type()) { |
| 354 | case MASTER_CHANGED: |
| 355 | clearTxWorkers(); |
| 356 | setupTxWorkers(); |
| 357 | log.trace("Processed mastership event {} on {}", event.type(), event.subject()); |
| 358 | break; |
| 359 | case BACKUPS_CHANGED: |
| 360 | case SUSPENDED: |
| 361 | default: |
| 362 | break; |
| 363 | } |
| 364 | } |
| 365 | } |
| 366 | |
| 367 | private final InternalDeviceListener internalDeviceListener = new InternalDeviceListener(); |
| 368 | private final InternalMastershipListener internalMastershipListener = new InternalMastershipListener(); |
Jayakumar Thazhath | e166a4e | 2017-11-15 02:17:04 -0500 | [diff] [blame] | 369 | |
| 370 | // Processor for Solicited RA packets |
| 371 | private class InternalPacketProcessor implements PacketProcessor { |
| 372 | |
| 373 | @Override |
| 374 | public void process(PacketContext context) { |
| 375 | if (context.isHandled()) { |
| 376 | return; |
| 377 | } |
| 378 | |
| 379 | // Ensure packet is IPv6 Solicited RA |
| 380 | InboundPacket pkt = context.inPacket(); |
| 381 | Ethernet ethernet = pkt.parsed(); |
| 382 | if ((ethernet == null) || (ethernet.getEtherType() != Ethernet.TYPE_IPV6)) { |
| 383 | return; |
| 384 | } |
| 385 | IPv6 ipv6Packet = (IPv6) ethernet.getPayload(); |
| 386 | if (ipv6Packet.getNextHeader() != IPv6.PROTOCOL_ICMP6) { |
| 387 | return; |
| 388 | } |
| 389 | ICMP6 icmp6Packet = (ICMP6) ipv6Packet.getPayload(); |
| 390 | if (icmp6Packet.getIcmpType() != ICMP6.ROUTER_SOLICITATION) { |
| 391 | return; |
| 392 | } |
| 393 | |
| 394 | // Start solicited-RA handling thread |
| 395 | SolicitedRAWorkerThread sraWorkerThread = new SolicitedRAWorkerThread(pkt); |
| 396 | executors.schedule(sraWorkerThread, 0, TimeUnit.SECONDS); |
| 397 | } |
| 398 | } |
| 399 | |
| 400 | InternalPacketProcessor processor = null; |
Jayakumar Thazhath | 2848457 | 2017-11-02 00:39:09 -0400 | [diff] [blame] | 401 | |
jayakumarthazhath | 1cbde73 | 2017-10-25 10:01:00 -0400 | [diff] [blame] | 402 | @Activate |
| 403 | protected void activate(ComponentContext context) { |
| 404 | // Basic application registrations. |
| 405 | appId = coreService.registerApplication(APP_NAME); |
| 406 | componentConfigService.registerProperties(getClass()); |
| 407 | |
Jayakumar Thazhath | e166a4e | 2017-11-15 02:17:04 -0500 | [diff] [blame] | 408 | // Packet processor for handling Router Solicitations |
| 409 | processor = new InternalPacketProcessor(); |
| 410 | packetService.addProcessor(processor, PacketProcessor.director(3)); |
| 411 | |
Jayakumar Thazhath | 2848457 | 2017-11-02 00:39:09 -0400 | [diff] [blame] | 412 | // Setup global prefix loading components |
| 413 | networkConfigRegistry.addListener(networkConfigListener); |
| 414 | networkConfigRegistry.registerConfigFactory(deviceConfigFactory); |
| 415 | loadGlobalPrefixConfig(); |
jayakumarthazhath | 1cbde73 | 2017-10-25 10:01:00 -0400 | [diff] [blame] | 416 | |
Charles Chan | bac954f9 | 2018-04-06 22:29:26 -0700 | [diff] [blame] | 417 | // Register device and mastership event listener |
Jayakumar Thazhath | e166a4e | 2017-11-15 02:17:04 -0500 | [diff] [blame] | 418 | deviceService.addListener(internalDeviceListener); |
Charles Chan | bac954f9 | 2018-04-06 22:29:26 -0700 | [diff] [blame] | 419 | mastershipService.addListener(internalMastershipListener); |
Jayakumar Thazhath | e166a4e | 2017-11-15 02:17:04 -0500 | [diff] [blame] | 420 | |
jayakumarthazhath | 1cbde73 | 2017-10-25 10:01:00 -0400 | [diff] [blame] | 421 | // Setup pool and worker threads for existing interfaces |
| 422 | setupPoolAndTxWorkers(); |
| 423 | } |
| 424 | |
Jayakumar Thazhath | 2848457 | 2017-11-02 00:39:09 -0400 | [diff] [blame] | 425 | |
jayakumarthazhath | 1cbde73 | 2017-10-25 10:01:00 -0400 | [diff] [blame] | 426 | @Modified |
| 427 | protected void modified(ComponentContext context) { |
| 428 | int newRaPoolSize, newRaThreadDelay; |
| 429 | |
| 430 | // Loading configured properties. |
| 431 | if (context != null) { |
| 432 | Dictionary<?, ?> properties = context.getProperties(); |
| 433 | try { |
| 434 | // Handle change in pool size |
Ray Milkey | 8e40651 | 2018-10-24 15:56:50 -0700 | [diff] [blame] | 435 | String s = get(properties, RA_THREADS_POOL); |
jayakumarthazhath | 1cbde73 | 2017-10-25 10:01:00 -0400 | [diff] [blame] | 436 | newRaPoolSize = isNullOrEmpty(s) ? |
Ray Milkey | 8e40651 | 2018-10-24 15:56:50 -0700 | [diff] [blame] | 437 | RA_THREADS_POOL_SIZE_DEFAULT : Integer.parseInt(s.trim()); |
jayakumarthazhath | 1cbde73 | 2017-10-25 10:01:00 -0400 | [diff] [blame] | 438 | if (newRaPoolSize != raPoolSize) { |
| 439 | raPoolSize = newRaPoolSize; |
| 440 | clearPoolAndTxWorkers(); |
| 441 | setupPoolAndTxWorkers(); |
Jayakumar Thazhath | 2848457 | 2017-11-02 00:39:09 -0400 | [diff] [blame] | 442 | log.info("Thread pool size updated to {}", raPoolSize); |
jayakumarthazhath | 1cbde73 | 2017-10-25 10:01:00 -0400 | [diff] [blame] | 443 | } |
| 444 | |
| 445 | // Handle change in thread delay |
Ray Milkey | 8e40651 | 2018-10-24 15:56:50 -0700 | [diff] [blame] | 446 | s = get(properties, RA_THREADS_DELAY); |
jayakumarthazhath | 1cbde73 | 2017-10-25 10:01:00 -0400 | [diff] [blame] | 447 | newRaThreadDelay = isNullOrEmpty(s) ? |
Ray Milkey | 8e40651 | 2018-10-24 15:56:50 -0700 | [diff] [blame] | 448 | RA_THREADS_DELAY_DEFAULT : Integer.parseInt(s.trim()); |
jayakumarthazhath | 1cbde73 | 2017-10-25 10:01:00 -0400 | [diff] [blame] | 449 | if (newRaThreadDelay != raThreadDelay) { |
| 450 | raThreadDelay = newRaThreadDelay; |
| 451 | clearTxWorkers(); |
| 452 | setupTxWorkers(); |
Jayakumar Thazhath | 2848457 | 2017-11-02 00:39:09 -0400 | [diff] [blame] | 453 | log.info("Thread delay updated to {}", raThreadDelay); |
jayakumarthazhath | 1cbde73 | 2017-10-25 10:01:00 -0400 | [diff] [blame] | 454 | } |
| 455 | |
| 456 | // Handle M-flag changes |
Ray Milkey | 8e40651 | 2018-10-24 15:56:50 -0700 | [diff] [blame] | 457 | s = get(properties, RA_FLAG_MBIT_STATUS); |
Jayakumar Thazhath | 2848457 | 2017-11-02 00:39:09 -0400 | [diff] [blame] | 458 | if (!isNullOrEmpty(s)) { |
| 459 | raFlagMbitStatus = Boolean.parseBoolean(s.trim()); |
| 460 | log.info("RA M-flag set {}", s); |
| 461 | } |
jayakumarthazhath | 1cbde73 | 2017-10-25 10:01:00 -0400 | [diff] [blame] | 462 | |
| 463 | // Handle O-flag changes |
Ray Milkey | 8e40651 | 2018-10-24 15:56:50 -0700 | [diff] [blame] | 464 | s = get(properties, RA_FLAG_OBIT_STATUS); |
Jayakumar Thazhath | 2848457 | 2017-11-02 00:39:09 -0400 | [diff] [blame] | 465 | if (!isNullOrEmpty(s)) { |
| 466 | raFlagObitStatus = Boolean.parseBoolean(s.trim()); |
| 467 | log.info("RA O-flag set {}", s); |
| 468 | } |
jayakumarthazhath | 1cbde73 | 2017-10-25 10:01:00 -0400 | [diff] [blame] | 469 | |
| 470 | // Handle prefix option configuration |
Ray Milkey | 8e40651 | 2018-10-24 15:56:50 -0700 | [diff] [blame] | 471 | s = get(properties, RA_OPTION_PREFIX_STATUS); |
Jayakumar Thazhath | 2848457 | 2017-11-02 00:39:09 -0400 | [diff] [blame] | 472 | if (!isNullOrEmpty(s)) { |
| 473 | raOptionPrefixStatus = Boolean.parseBoolean(s.trim()); |
| 474 | String status = raOptionPrefixStatus ? "enabled" : "disabled"; |
| 475 | log.info("RA prefix option {}", status); |
| 476 | } |
| 477 | |
Ray Milkey | 8e40651 | 2018-10-24 15:56:50 -0700 | [diff] [blame] | 478 | s = get(properties, RA_GLOBAL_PREFIX_CONF_STATUS); |
Jayakumar Thazhath | 2848457 | 2017-11-02 00:39:09 -0400 | [diff] [blame] | 479 | if (!isNullOrEmpty(s)) { |
Ray Milkey | 8e40651 | 2018-10-24 15:56:50 -0700 | [diff] [blame] | 480 | raGlobalPrefixConfStatus = Boolean.parseBoolean(s.trim()); |
Jayakumar Thazhath | 2848457 | 2017-11-02 00:39:09 -0400 | [diff] [blame] | 481 | clearTxWorkers(); |
| 482 | setupTxWorkers(); |
| 483 | String status = raOptionPrefixStatus ? "enabled" : "disabled"; |
| 484 | log.info("RA global configuration file loading {}", status); |
| 485 | } |
jayakumarthazhath | 1cbde73 | 2017-10-25 10:01:00 -0400 | [diff] [blame] | 486 | |
| 487 | } catch (NumberFormatException e) { |
| 488 | log.warn("Component configuration had invalid value, aborting changes loading.", e); |
| 489 | } |
| 490 | } |
| 491 | } |
| 492 | |
| 493 | @Deactivate |
| 494 | protected void deactivate() { |
| 495 | // Unregister resources. |
| 496 | componentConfigService.unregisterProperties(getClass(), false); |
| 497 | interfaceService.removeListener(interfaceListener); |
Jayakumar Thazhath | e166a4e | 2017-11-15 02:17:04 -0500 | [diff] [blame] | 498 | networkConfigRegistry.removeListener(networkConfigListener); |
| 499 | networkConfigRegistry.unregisterConfigFactory(deviceConfigFactory); |
| 500 | packetService.removeProcessor(processor); |
| 501 | deviceService.removeListener(internalDeviceListener); |
Charles Chan | bac954f9 | 2018-04-06 22:29:26 -0700 | [diff] [blame] | 502 | mastershipService.removeListener(internalMastershipListener); |
jayakumarthazhath | 1cbde73 | 2017-10-25 10:01:00 -0400 | [diff] [blame] | 503 | |
| 504 | // Clear pool & threads |
| 505 | clearPoolAndTxWorkers(); |
| 506 | } |
| 507 | |
Jayakumar Thazhath | 3ec6aa2 | 2017-09-11 02:00:20 -0400 | [diff] [blame] | 508 | // Worker thread for actually sending ICMPv6 RA packets. |
| 509 | private class RAWorkerThread implements Runnable { |
| 510 | |
| 511 | ConnectPoint connectPoint; |
| 512 | List<InterfaceIpAddress> ipAddresses; |
| 513 | int retransmitPeriod; |
Jayakumar Thazhath | e166a4e | 2017-11-15 02:17:04 -0500 | [diff] [blame] | 514 | MacAddress solicitHostMac; |
| 515 | byte[] solicitHostAddress; |
Jayakumar Thazhath | 3ec6aa2 | 2017-09-11 02:00:20 -0400 | [diff] [blame] | 516 | |
| 517 | // Various fixed values in RA packet |
| 518 | public static final byte RA_HOP_LIMIT = (byte) 0xff; |
| 519 | public static final short RA_ROUTER_LIFETIME = (short) 1800; |
| 520 | public static final int RA_OPTIONS_BUFFER_SIZE = 500; |
| 521 | public static final int RA_OPTION_MTU_VALUE = 1500; |
| 522 | public static final int RA_OPTION_PREFIX_VALID_LIFETIME = 600; |
| 523 | public static final int RA_OPTION_PREFIX_PREFERRED_LIFETIME = 600; |
| 524 | public static final int RA_RETRANSMIT_CALIBRATION_PERIOD = 1; |
| 525 | |
| 526 | |
Jayakumar Thazhath | e166a4e | 2017-11-15 02:17:04 -0500 | [diff] [blame] | 527 | RAWorkerThread(ConnectPoint connectPoint, List<InterfaceIpAddress> ipAddresses, int period, |
| 528 | MacAddress macAddress, byte[] ipv6Address) { |
Jayakumar Thazhath | 3ec6aa2 | 2017-09-11 02:00:20 -0400 | [diff] [blame] | 529 | this.connectPoint = connectPoint; |
| 530 | this.ipAddresses = ipAddresses; |
| 531 | retransmitPeriod = period; |
Jayakumar Thazhath | e166a4e | 2017-11-15 02:17:04 -0500 | [diff] [blame] | 532 | solicitHostMac = macAddress; |
| 533 | solicitHostAddress = ipv6Address; |
Jayakumar Thazhath | 3ec6aa2 | 2017-09-11 02:00:20 -0400 | [diff] [blame] | 534 | } |
| 535 | |
Yuta HIGUCHI | 488a94c | 2018-01-26 17:24:09 -0800 | [diff] [blame] | 536 | @Override |
Jayakumar Thazhath | 3ec6aa2 | 2017-09-11 02:00:20 -0400 | [diff] [blame] | 537 | public void run() { |
| 538 | // Router Advertisement header filling. Please refer RFC-2461. |
| 539 | RouterAdvertisement ra = new RouterAdvertisement(); |
| 540 | ra.setCurrentHopLimit(RA_HOP_LIMIT); |
jayakumarthazhath | 1cbde73 | 2017-10-25 10:01:00 -0400 | [diff] [blame] | 541 | ra.setMFlag((byte) (raFlagMbitStatus ? 0x01 : 0x00)); |
| 542 | ra.setOFlag((byte) (raFlagObitStatus ? 0x01 : 0x00)); |
Jayakumar Thazhath | 3ec6aa2 | 2017-09-11 02:00:20 -0400 | [diff] [blame] | 543 | ra.setRouterLifetime(RA_ROUTER_LIFETIME); |
| 544 | ra.setReachableTime(0); |
| 545 | ra.setRetransmitTimer(retransmitPeriod + RA_RETRANSMIT_CALIBRATION_PERIOD); |
| 546 | |
| 547 | // Option : Source link-layer address. |
| 548 | byte[] optionBuffer = new byte[RA_OPTIONS_BUFFER_SIZE]; |
| 549 | ByteBuffer option = ByteBuffer.wrap(optionBuffer); |
| 550 | Optional<MacAddress> macAddress = interfaceService.getInterfacesByPort(connectPoint).stream() |
| 551 | .map(Interface::mac).findFirst(); |
| 552 | if (!macAddress.isPresent()) { |
| 553 | log.warn("Unable to retrieve interface {} MAC address. Terminating RA transmission.", connectPoint); |
| 554 | return; |
| 555 | } |
| 556 | option.put(macAddress.get().toBytes()); |
| 557 | ra.addOption(NeighborDiscoveryOptions.TYPE_SOURCE_LL_ADDRESS, |
| 558 | Arrays.copyOfRange(option.array(), 0, option.position())); |
| 559 | |
| 560 | // Option : MTU. |
| 561 | option.rewind(); |
| 562 | option.putShort((short) 0); |
| 563 | option.putInt(RA_OPTION_MTU_VALUE); |
| 564 | ra.addOption(NeighborDiscoveryOptions.TYPE_MTU, |
| 565 | Arrays.copyOfRange(option.array(), 0, option.position())); |
| 566 | |
| 567 | // Option : Prefix information. |
jayakumarthazhath | 1cbde73 | 2017-10-25 10:01:00 -0400 | [diff] [blame] | 568 | if (raOptionPrefixStatus) { |
| 569 | ipAddresses.stream() |
| 570 | .filter(i -> i.ipAddress().version().equals(IpAddress.Version.INET6)) |
| 571 | .forEach(i -> { |
| 572 | option.rewind(); |
| 573 | option.put((byte) i.subnetAddress().prefixLength()); |
| 574 | // Enable "onlink" option only. |
| 575 | option.put((byte) 0x80); |
| 576 | option.putInt(RA_OPTION_PREFIX_VALID_LIFETIME); |
| 577 | option.putInt(RA_OPTION_PREFIX_PREFERRED_LIFETIME); |
| 578 | // Clear reserved fields |
| 579 | option.putInt(0x00000000); |
| 580 | option.put(IpAddress.makeMaskedAddress(i.ipAddress(), |
| 581 | i.subnetAddress().prefixLength()).toOctets()); |
| 582 | ra.addOption(NeighborDiscoveryOptions.TYPE_PREFIX_INFORMATION, |
| 583 | Arrays.copyOfRange(option.array(), 0, option.position())); |
Jayakumar Thazhath | 3ec6aa2 | 2017-09-11 02:00:20 -0400 | [diff] [blame] | 584 | |
jayakumarthazhath | 1cbde73 | 2017-10-25 10:01:00 -0400 | [diff] [blame] | 585 | }); |
| 586 | } |
Jayakumar Thazhath | 3ec6aa2 | 2017-09-11 02:00:20 -0400 | [diff] [blame] | 587 | |
| 588 | // ICMPv6 header filling. |
| 589 | ICMP6 icmpv6 = new ICMP6(); |
| 590 | icmpv6.setIcmpType(ICMP6.ROUTER_ADVERTISEMENT); |
| 591 | icmpv6.setIcmpCode((byte) 0); |
| 592 | icmpv6.setPayload(ra); |
| 593 | |
| 594 | // IPv6 header filling. |
| 595 | byte[] ip6AllNodesAddress = Ip6Address.valueOf("ff02::1").toOctets(); |
| 596 | IPv6 ipv6 = new IPv6(); |
Jayakumar Thazhath | e166a4e | 2017-11-15 02:17:04 -0500 | [diff] [blame] | 597 | ipv6.setDestinationAddress((solicitHostAddress == null) ? ip6AllNodesAddress : solicitHostAddress); |
Jayakumar Thazhath | 3ec6aa2 | 2017-09-11 02:00:20 -0400 | [diff] [blame] | 598 | /* RA packet L2 source address created from port MAC address. |
| 599 | * Note : As per RFC-4861 RAs should be sent from link-local address. |
| 600 | */ |
| 601 | ipv6.setSourceAddress(IPv6.getLinkLocalAddress(macAddress.get().toBytes())); |
| 602 | ipv6.setNextHeader(IPv6.PROTOCOL_ICMP6); |
| 603 | ipv6.setHopLimit(RA_HOP_LIMIT); |
| 604 | ipv6.setTrafficClass((byte) 0xe0); |
| 605 | ipv6.setPayload(icmpv6); |
| 606 | |
| 607 | // Ethernet header filling. |
| 608 | Ethernet ethernet = new Ethernet(); |
| 609 | |
| 610 | /* Ethernet IPv6 multicast address creation. |
| 611 | * Refer : RFC 2624 section 7. |
| 612 | */ |
| 613 | byte[] l2Ipv6MulticastAddress = MacAddress.IPV6_MULTICAST.toBytes(); |
| 614 | IntStream.range(1, 4).forEach(i -> l2Ipv6MulticastAddress[l2Ipv6MulticastAddress.length - i] = |
| 615 | ip6AllNodesAddress[ip6AllNodesAddress.length - i]); |
Jayakumar Thazhath | e166a4e | 2017-11-15 02:17:04 -0500 | [diff] [blame] | 616 | // Provide unicast address for Solicit RA replays |
| 617 | ethernet.setDestinationMACAddress((solicitHostMac == null) ? |
| 618 | MacAddress.valueOf(l2Ipv6MulticastAddress) : solicitHostMac); |
Jayakumar Thazhath | 3ec6aa2 | 2017-09-11 02:00:20 -0400 | [diff] [blame] | 619 | ethernet.setSourceMACAddress(macAddress.get().toBytes()); |
| 620 | ethernet.setEtherType(EthType.EtherType.IPV6.ethType().toShort()); |
| 621 | ethernet.setVlanID(Ethernet.VLAN_UNTAGGED); |
| 622 | ethernet.setPayload(ipv6); |
| 623 | ethernet.setPad(false); |
| 624 | |
| 625 | // Flush out PACKET_OUT. |
| 626 | ByteBuffer stream = ByteBuffer.wrap(ethernet.serialize()); |
| 627 | TrafficTreatment treatment = DefaultTrafficTreatment.builder().setOutput(connectPoint.port()).build(); |
| 628 | OutboundPacket packet = new DefaultOutboundPacket(connectPoint.deviceId(), |
| 629 | treatment, stream); |
| 630 | packetService.emit(packet); |
Jayakumar Thazhath | e166a4e | 2017-11-15 02:17:04 -0500 | [diff] [blame] | 631 | log.trace("Transmitted Unsolicited Router Advertisement on {}", connectPoint); |
Jayakumar Thazhath | 3ec6aa2 | 2017-09-11 02:00:20 -0400 | [diff] [blame] | 632 | } |
| 633 | } |
Jayakumar Thazhath | e166a4e | 2017-11-15 02:17:04 -0500 | [diff] [blame] | 634 | |
| 635 | // Worker thread for processing IPv6 Solicited RA packets. |
| 636 | public class SolicitedRAWorkerThread implements Runnable { |
| 637 | |
| 638 | private InboundPacket packet; |
| 639 | |
| 640 | SolicitedRAWorkerThread(InboundPacket packet) { |
| 641 | this.packet = packet; |
| 642 | } |
| 643 | |
| 644 | @Override |
| 645 | public void run() { |
| 646 | |
| 647 | // TODO : Validate Router Solicitation |
| 648 | |
| 649 | // Pause already running unsolicited RA threads in received connect point |
| 650 | ConnectPoint connectPoint = packet.receivedFrom(); |
| 651 | List<InterfaceIpAddress> addresses = deactivateRouterAdvertisement(connectPoint); |
| 652 | |
| 653 | /* Multicast RA(ie. Unsolicited RA) TX time is not preciously tracked so to make sure that |
| 654 | * Unicast RA(ie. Router Solicitation Response) is TXed before Mulicast RA |
| 655 | * logic adapted here is disable Mulicast RA, TX Unicast RA and then restore Multicast RA. |
| 656 | */ |
| 657 | log.trace("Processing Router Solicitations from {}", connectPoint); |
| 658 | try { |
| 659 | Ethernet ethernet = packet.parsed(); |
| 660 | IPv6 ipv6 = (IPv6) ethernet.getPayload(); |
| 661 | RAWorkerThread worker = new RAWorkerThread(connectPoint, |
| 662 | addresses, raThreadDelay, ethernet.getSourceMAC(), ipv6.getSourceAddress()); |
| 663 | // TODO : Estimate TX time as in RFC 4861, Section 6.2.6 and schedule TX based on it |
| 664 | CompletableFuture<Void> sraHandlerFuture = CompletableFuture.runAsync(worker, executors); |
| 665 | sraHandlerFuture.get(); |
| 666 | } catch (Exception e) { |
| 667 | log.error("Failed to respond to router solicitation. {}", e); |
| 668 | } finally { |
| 669 | activateRouterAdvertisement(connectPoint, addresses); |
| 670 | log.trace("Restored Unsolicited Router Advertisements on {}", connectPoint); |
| 671 | } |
| 672 | } |
| 673 | } |
| 674 | |
Jayakumar Thazhath | 3ec6aa2 | 2017-09-11 02:00:20 -0400 | [diff] [blame] | 675 | } |