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