Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 1 | /* |
Ray Milkey | 34c9590 | 2015-04-15 09:47:53 -0700 | [diff] [blame] | 2 | * Copyright 2014-2015 Open Networking Laboratory |
Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 3 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 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 |
Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 7 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 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. |
Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 15 | */ |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 16 | package org.onosproject.proxyarp; |
alshabib | c274c90 | 2014-10-03 14:58:27 -0700 | [diff] [blame] | 17 | |
alshabib | c274c90 | 2014-10-03 14:58:27 -0700 | [diff] [blame] | 18 | import org.apache.felix.scr.annotations.Activate; |
| 19 | import org.apache.felix.scr.annotations.Component; |
| 20 | import org.apache.felix.scr.annotations.Deactivate; |
Pavlin Radoslavov | 93b606b | 2015-02-25 17:28:39 -0800 | [diff] [blame] | 21 | import org.apache.felix.scr.annotations.Modified; |
| 22 | import org.apache.felix.scr.annotations.Property; |
alshabib | c274c90 | 2014-10-03 14:58:27 -0700 | [diff] [blame] | 23 | import org.apache.felix.scr.annotations.Reference; |
| 24 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
Jonathan Hart | 3cfce8e | 2015-01-14 16:43:27 -0800 | [diff] [blame] | 25 | import org.onlab.packet.Ethernet; |
Thomas Vachuska | 6519e6f | 2015-03-11 02:29:31 -0700 | [diff] [blame] | 26 | import org.onosproject.cfg.ComponentConfigService; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 27 | import org.onosproject.core.ApplicationId; |
| 28 | import org.onosproject.core.CoreService; |
Jonathan Hart | 3cfce8e | 2015-01-14 16:43:27 -0800 | [diff] [blame] | 29 | import org.onosproject.net.flow.DefaultTrafficSelector; |
| 30 | import org.onosproject.net.flow.TrafficSelector; |
Ayaka Koshibe | bfbe1d7 | 2015-03-16 16:51:41 -0700 | [diff] [blame] | 31 | import org.onosproject.net.packet.InboundPacket; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 32 | import org.onosproject.net.packet.PacketContext; |
| 33 | import org.onosproject.net.packet.PacketProcessor; |
| 34 | import org.onosproject.net.packet.PacketService; |
| 35 | import org.onosproject.net.proxyarp.ProxyArpService; |
Pavlin Radoslavov | 93b606b | 2015-02-25 17:28:39 -0800 | [diff] [blame] | 36 | import org.osgi.service.component.ComponentContext; |
alshabib | c274c90 | 2014-10-03 14:58:27 -0700 | [diff] [blame] | 37 | import org.slf4j.Logger; |
| 38 | |
Thomas Vachuska | 6519e6f | 2015-03-11 02:29:31 -0700 | [diff] [blame] | 39 | import java.util.Dictionary; |
| 40 | |
Pavlin Radoslavov | 93b606b | 2015-02-25 17:28:39 -0800 | [diff] [blame] | 41 | import static com.google.common.base.Strings.isNullOrEmpty; |
Aaron Kruglikov | 07a923d | 2015-07-03 13:30:57 -0700 | [diff] [blame] | 42 | import static org.onlab.packet.Ethernet.TYPE_ARP; |
| 43 | import static org.onlab.packet.Ethernet.TYPE_IPV6; |
| 44 | import static org.onlab.packet.ICMP6.NEIGHBOR_ADVERTISEMENT; |
| 45 | import static org.onlab.packet.ICMP6.NEIGHBOR_SOLICITATION; |
| 46 | import static org.onlab.packet.IPv6.PROTOCOL_ICMP6; |
| 47 | import static org.onosproject.net.packet.PacketPriority.CONTROL; |
alshabib | 78baaf2 | 2015-02-18 18:55:45 -0800 | [diff] [blame] | 48 | import static org.slf4j.LoggerFactory.getLogger; |
| 49 | |
alshabib | c274c90 | 2014-10-03 14:58:27 -0700 | [diff] [blame] | 50 | /** |
| 51 | * Sample reactive proxy arp application. |
| 52 | */ |
| 53 | @Component(immediate = true) |
| 54 | public class ProxyArp { |
| 55 | |
alshabib | c274c90 | 2014-10-03 14:58:27 -0700 | [diff] [blame] | 56 | private final Logger log = getLogger(getClass()); |
| 57 | |
| 58 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 59 | protected PacketService packetService; |
| 60 | |
| 61 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 62 | protected ProxyArpService proxyArpService; |
| 63 | |
| 64 | private ProxyArpProcessor processor = new ProxyArpProcessor(); |
| 65 | |
alshabib | 92c65ad | 2014-10-08 21:56:05 -0700 | [diff] [blame] | 66 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 67 | protected CoreService coreService; |
| 68 | |
Thomas Vachuska | 6519e6f | 2015-03-11 02:29:31 -0700 | [diff] [blame] | 69 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 70 | protected ComponentConfigService cfgService; |
| 71 | |
alshabib | c274c90 | 2014-10-03 14:58:27 -0700 | [diff] [blame] | 72 | private ApplicationId appId; |
| 73 | |
Pavlin Radoslavov | 93b606b | 2015-02-25 17:28:39 -0800 | [diff] [blame] | 74 | @Property(name = "ipv6NeighborDiscovery", boolValue = false, |
Thomas Vachuska | 6519e6f | 2015-03-11 02:29:31 -0700 | [diff] [blame] | 75 | label = "Enable IPv6 Neighbor Discovery; default is false") |
Ayaka Koshibe | bfbe1d7 | 2015-03-16 16:51:41 -0700 | [diff] [blame] | 76 | protected boolean ipv6NeighborDiscovery = false; |
Pavlin Radoslavov | 93b606b | 2015-02-25 17:28:39 -0800 | [diff] [blame] | 77 | |
alshabib | c274c90 | 2014-10-03 14:58:27 -0700 | [diff] [blame] | 78 | @Activate |
Pavlin Radoslavov | 93b606b | 2015-02-25 17:28:39 -0800 | [diff] [blame] | 79 | public void activate(ComponentContext context) { |
Thomas Vachuska | 6519e6f | 2015-03-11 02:29:31 -0700 | [diff] [blame] | 80 | cfgService.registerProperties(getClass()); |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 81 | appId = coreService.registerApplication("org.onosproject.proxyarp"); |
Pavlin Radoslavov | 93b606b | 2015-02-25 17:28:39 -0800 | [diff] [blame] | 82 | |
Brian O'Connor | 3b78326 | 2015-07-29 17:49:24 -0700 | [diff] [blame] | 83 | packetService.addProcessor(processor, PacketProcessor.director(1)); |
Charles M.C. Chan | e148de8 | 2015-05-06 12:38:21 +0800 | [diff] [blame] | 84 | readComponentConfiguration(context); |
Jonathan Hart | 39ee648 | 2015-08-31 16:00:19 +0200 | [diff] [blame] | 85 | requestPackets(); |
Jonathan Hart | 3cfce8e | 2015-01-14 16:43:27 -0800 | [diff] [blame] | 86 | |
Charles M.C. Chan | e148de8 | 2015-05-06 12:38:21 +0800 | [diff] [blame] | 87 | log.info("Started with Application ID {}", appId.id()); |
| 88 | } |
| 89 | |
| 90 | @Deactivate |
| 91 | public void deactivate() { |
Charles M.C. Chan | e148de8 | 2015-05-06 12:38:21 +0800 | [diff] [blame] | 92 | cfgService.unregisterProperties(getClass(), false); |
Aaron Kruglikov | 07a923d | 2015-07-03 13:30:57 -0700 | [diff] [blame] | 93 | withdrawIntercepts(); |
Charles M.C. Chan | e148de8 | 2015-05-06 12:38:21 +0800 | [diff] [blame] | 94 | packetService.removeProcessor(processor); |
| 95 | processor = null; |
| 96 | log.info("Stopped"); |
| 97 | } |
| 98 | |
| 99 | @Modified |
| 100 | public void modified(ComponentContext context) { |
Charles M.C. Chan | e148de8 | 2015-05-06 12:38:21 +0800 | [diff] [blame] | 101 | readComponentConfiguration(context); |
Jonathan Hart | 39ee648 | 2015-08-31 16:00:19 +0200 | [diff] [blame] | 102 | requestPackets(); |
Charles M.C. Chan | e148de8 | 2015-05-06 12:38:21 +0800 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | /** |
| 106 | * Request packet in via PacketService. |
| 107 | */ |
Jonathan Hart | 39ee648 | 2015-08-31 16:00:19 +0200 | [diff] [blame] | 108 | private void requestPackets() { |
Jonathan Hart | 3cfce8e | 2015-01-14 16:43:27 -0800 | [diff] [blame] | 109 | TrafficSelector.Builder selectorBuilder = |
| 110 | DefaultTrafficSelector.builder(); |
Aaron Kruglikov | 07a923d | 2015-07-03 13:30:57 -0700 | [diff] [blame] | 111 | selectorBuilder.matchEthType(TYPE_ARP); |
Jonathan Hart | 3cfce8e | 2015-01-14 16:43:27 -0800 | [diff] [blame] | 112 | packetService.requestPackets(selectorBuilder.build(), |
Aaron Kruglikov | 07a923d | 2015-07-03 13:30:57 -0700 | [diff] [blame] | 113 | CONTROL, appId); |
Jonathan Hart | 3cfce8e | 2015-01-14 16:43:27 -0800 | [diff] [blame] | 114 | |
Aaron Kruglikov | 07a923d | 2015-07-03 13:30:57 -0700 | [diff] [blame] | 115 | selectorBuilder = DefaultTrafficSelector.builder(); |
| 116 | selectorBuilder.matchEthType(TYPE_IPV6); |
| 117 | selectorBuilder.matchIPProtocol(PROTOCOL_ICMP6); |
| 118 | selectorBuilder.matchIcmpv6Type(NEIGHBOR_SOLICITATION); |
Pavlin Radoslavov | 93b606b | 2015-02-25 17:28:39 -0800 | [diff] [blame] | 119 | if (ipv6NeighborDiscovery) { |
| 120 | // IPv6 Neighbor Solicitation packet. |
Pavlin Radoslavov | 93b606b | 2015-02-25 17:28:39 -0800 | [diff] [blame] | 121 | packetService.requestPackets(selectorBuilder.build(), |
Aaron Kruglikov | 07a923d | 2015-07-03 13:30:57 -0700 | [diff] [blame] | 122 | CONTROL, appId); |
| 123 | } else { |
| 124 | packetService.cancelPackets(selectorBuilder.build(), |
| 125 | CONTROL, appId); |
Pavlin Radoslavov | 93b606b | 2015-02-25 17:28:39 -0800 | [diff] [blame] | 126 | } |
Aaron Kruglikov | 07a923d | 2015-07-03 13:30:57 -0700 | [diff] [blame] | 127 | |
| 128 | // IPv6 Neighbor Advertisement packet. |
| 129 | selectorBuilder = DefaultTrafficSelector.builder(); |
| 130 | selectorBuilder.matchEthType(TYPE_IPV6); |
| 131 | selectorBuilder.matchIPProtocol(PROTOCOL_ICMP6); |
| 132 | selectorBuilder.matchIcmpv6Type(NEIGHBOR_ADVERTISEMENT); |
| 133 | if (ipv6NeighborDiscovery) { |
| 134 | packetService.requestPackets(selectorBuilder.build(), |
| 135 | CONTROL, appId); |
| 136 | } else { |
| 137 | packetService.cancelPackets(selectorBuilder.build(), |
| 138 | CONTROL, appId); |
| 139 | } |
| 140 | |
| 141 | |
| 142 | } |
| 143 | |
| 144 | /** |
| 145 | * Cancel requested packet in via packet service. |
| 146 | */ |
| 147 | private void withdrawIntercepts() { |
| 148 | TrafficSelector.Builder selectorBuilder = |
| 149 | DefaultTrafficSelector.builder(); |
| 150 | selectorBuilder.matchEthType(TYPE_ARP); |
| 151 | packetService.cancelPackets(selectorBuilder.build(), CONTROL, appId); |
| 152 | selectorBuilder = DefaultTrafficSelector.builder(); |
| 153 | selectorBuilder.matchEthType(TYPE_IPV6); |
| 154 | selectorBuilder.matchIPProtocol(PROTOCOL_ICMP6); |
| 155 | selectorBuilder.matchIcmpv6Type(NEIGHBOR_SOLICITATION); |
| 156 | packetService.cancelPackets(selectorBuilder.build(), CONTROL, appId); |
| 157 | selectorBuilder = DefaultTrafficSelector.builder(); |
| 158 | selectorBuilder.matchEthType(TYPE_IPV6); |
| 159 | selectorBuilder.matchIPProtocol(PROTOCOL_ICMP6); |
| 160 | selectorBuilder.matchIcmpv6Type(NEIGHBOR_ADVERTISEMENT); |
| 161 | packetService.cancelPackets(selectorBuilder.build(), CONTROL, appId); |
| 162 | |
Pavlin Radoslavov | 93b606b | 2015-02-25 17:28:39 -0800 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | /** |
| 166 | * Extracts properties from the component configuration context. |
| 167 | * |
| 168 | * @param context the component context |
| 169 | */ |
| 170 | private void readComponentConfiguration(ComponentContext context) { |
| 171 | Dictionary<?, ?> properties = context.getProperties(); |
| 172 | Boolean flag; |
| 173 | |
| 174 | flag = isPropertyEnabled(properties, "ipv6NeighborDiscovery"); |
| 175 | if (flag == null) { |
| 176 | log.info("IPv6 Neighbor Discovery is not configured, " + |
Thomas Vachuska | 6519e6f | 2015-03-11 02:29:31 -0700 | [diff] [blame] | 177 | "using current value of {}", ipv6NeighborDiscovery); |
Pavlin Radoslavov | 93b606b | 2015-02-25 17:28:39 -0800 | [diff] [blame] | 178 | } else { |
| 179 | ipv6NeighborDiscovery = flag; |
| 180 | log.info("Configured. IPv6 Neighbor Discovery is {}", |
| 181 | ipv6NeighborDiscovery ? "enabled" : "disabled"); |
| 182 | } |
| 183 | } |
| 184 | |
| 185 | /** |
| 186 | * Check property name is defined and set to true. |
| 187 | * |
Thomas Vachuska | 6519e6f | 2015-03-11 02:29:31 -0700 | [diff] [blame] | 188 | * @param properties properties to be looked up |
Pavlin Radoslavov | 93b606b | 2015-02-25 17:28:39 -0800 | [diff] [blame] | 189 | * @param propertyName the name of the property to look up |
| 190 | * @return value when the propertyName is defined or return null |
| 191 | */ |
| 192 | private static Boolean isPropertyEnabled(Dictionary<?, ?> properties, |
| 193 | String propertyName) { |
| 194 | Boolean value = null; |
| 195 | try { |
| 196 | String s = (String) properties.get(propertyName); |
| 197 | value = isNullOrEmpty(s) ? null : s.trim().equals("true"); |
| 198 | } catch (ClassCastException e) { |
| 199 | // No propertyName defined. |
| 200 | value = null; |
| 201 | } |
| 202 | return value; |
| 203 | } |
alshabib | c274c90 | 2014-10-03 14:58:27 -0700 | [diff] [blame] | 204 | |
| 205 | /** |
| 206 | * Packet processor responsible for forwarding packets along their paths. |
| 207 | */ |
| 208 | private class ProxyArpProcessor implements PacketProcessor { |
| 209 | |
| 210 | @Override |
| 211 | public void process(PacketContext context) { |
| 212 | // Stop processing if the packet has been handled, since we |
| 213 | // can't do any more to it. |
| 214 | if (context.isHandled()) { |
| 215 | return; |
| 216 | } |
Ayaka Koshibe | bfbe1d7 | 2015-03-16 16:51:41 -0700 | [diff] [blame] | 217 | // If IPv6 NDP is disabled, don't handle IPv6 frames. |
| 218 | InboundPacket pkt = context.inPacket(); |
| 219 | Ethernet ethPkt = pkt.parsed(); |
| 220 | if (ethPkt == null) { |
| 221 | return; |
| 222 | } |
Aaron Kruglikov | 07a923d | 2015-07-03 13:30:57 -0700 | [diff] [blame] | 223 | if (!ipv6NeighborDiscovery && (ethPkt.getEtherType() == TYPE_IPV6)) { |
Ayaka Koshibe | bfbe1d7 | 2015-03-16 16:51:41 -0700 | [diff] [blame] | 224 | return; |
| 225 | } |
Rusty Eddy | 29acad6 | 2015-07-07 19:33:47 -0700 | [diff] [blame] | 226 | |
| 227 | // Do not ARP for multicast packets. Let mfwd handle them. |
| 228 | if (ethPkt.getEtherType() == Ethernet.TYPE_IPV4) { |
| 229 | if (ethPkt.getDestinationMAC().isMulticast()) { |
| 230 | return; |
| 231 | } |
| 232 | } |
| 233 | |
alshabib | c274c90 | 2014-10-03 14:58:27 -0700 | [diff] [blame] | 234 | //handle the arp packet. |
Kunihiro Ishiguro | f1bff50 | 2015-01-30 15:47:06 -0800 | [diff] [blame] | 235 | proxyArpService.handlePacket(context); |
alshabib | c274c90 | 2014-10-03 14:58:27 -0700 | [diff] [blame] | 236 | } |
| 237 | } |
| 238 | } |
| 239 | |
| 240 | |