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