blob: d441aeed5864267148ff3b21099b102ac804cf1e [file] [log] [blame]
Thomas Vachuska781d18b2014-10-27 10:31:25 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2014-present Open Networking Laboratory
Thomas Vachuska781d18b2014-10-27 10:31:25 -07003 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07004 * 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 Vachuska781d18b2014-10-27 10:31:25 -07007 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07008 * 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 Vachuska781d18b2014-10-27 10:31:25 -070015 */
Brian O'Connorabafb502014-12-02 22:26:20 -080016package org.onosproject.proxyarp;
alshabibc274c902014-10-03 14:58:27 -070017
alshabibc274c902014-10-03 14:58:27 -070018import org.apache.felix.scr.annotations.Activate;
19import org.apache.felix.scr.annotations.Component;
20import org.apache.felix.scr.annotations.Deactivate;
Pavlin Radoslavov93b606b2015-02-25 17:28:39 -080021import org.apache.felix.scr.annotations.Modified;
22import org.apache.felix.scr.annotations.Property;
alshabibc274c902014-10-03 14:58:27 -070023import org.apache.felix.scr.annotations.Reference;
24import org.apache.felix.scr.annotations.ReferenceCardinality;
Jonathan Hart3cfce8e2015-01-14 16:43:27 -080025import org.onlab.packet.Ethernet;
Brian O'Connorbc865f62015-10-08 21:39:53 -070026import org.onlab.packet.ICMP6;
27import org.onlab.packet.IPv6;
Jian Lid9b5f552016-03-11 18:15:31 -080028import org.onlab.util.Tools;
Thomas Vachuska6519e6f2015-03-11 02:29:31 -070029import org.onosproject.cfg.ComponentConfigService;
Brian O'Connorabafb502014-12-02 22:26:20 -080030import org.onosproject.core.ApplicationId;
31import org.onosproject.core.CoreService;
Jonathan Hart3cfce8e2015-01-14 16:43:27 -080032import org.onosproject.net.flow.DefaultTrafficSelector;
33import org.onosproject.net.flow.TrafficSelector;
Ayaka Koshibebfbe1d72015-03-16 16:51:41 -070034import org.onosproject.net.packet.InboundPacket;
Brian O'Connorabafb502014-12-02 22:26:20 -080035import org.onosproject.net.packet.PacketContext;
36import org.onosproject.net.packet.PacketProcessor;
37import org.onosproject.net.packet.PacketService;
38import org.onosproject.net.proxyarp.ProxyArpService;
Pavlin Radoslavov93b606b2015-02-25 17:28:39 -080039import org.osgi.service.component.ComponentContext;
alshabibc274c902014-10-03 14:58:27 -070040import org.slf4j.Logger;
41
Thomas Vachuska6519e6f2015-03-11 02:29:31 -070042import java.util.Dictionary;
43
Aaron Kruglikov07a923d2015-07-03 13:30:57 -070044import static org.onlab.packet.Ethernet.TYPE_ARP;
45import static org.onlab.packet.Ethernet.TYPE_IPV6;
46import static org.onlab.packet.ICMP6.NEIGHBOR_ADVERTISEMENT;
47import static org.onlab.packet.ICMP6.NEIGHBOR_SOLICITATION;
48import static org.onlab.packet.IPv6.PROTOCOL_ICMP6;
49import static org.onosproject.net.packet.PacketPriority.CONTROL;
alshabib78baaf22015-02-18 18:55:45 -080050import static org.slf4j.LoggerFactory.getLogger;
51
alshabibc274c902014-10-03 14:58:27 -070052/**
53 * Sample reactive proxy arp application.
Jonathan Hartc4f681c2016-09-09 07:14:25 -070054 *
55 * @deprecated in Hummingbird release
alshabibc274c902014-10-03 14:58:27 -070056 */
Jonathan Hartc4f681c2016-09-09 07:14:25 -070057@Deprecated
58@Component(enabled = false)
alshabibc274c902014-10-03 14:58:27 -070059public class ProxyArp {
60
alshabibc274c902014-10-03 14:58:27 -070061 private final Logger log = getLogger(getClass());
62
63 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
64 protected PacketService packetService;
65
66 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
67 protected ProxyArpService proxyArpService;
68
69 private ProxyArpProcessor processor = new ProxyArpProcessor();
70
alshabib92c65ad2014-10-08 21:56:05 -070071 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
72 protected CoreService coreService;
73
Thomas Vachuska6519e6f2015-03-11 02:29:31 -070074 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
75 protected ComponentConfigService cfgService;
76
alshabibc274c902014-10-03 14:58:27 -070077 private ApplicationId appId;
78
Pavlin Radoslavov93b606b2015-02-25 17:28:39 -080079 @Property(name = "ipv6NeighborDiscovery", boolValue = false,
Thomas Vachuska6519e6f2015-03-11 02:29:31 -070080 label = "Enable IPv6 Neighbor Discovery; default is false")
Ayaka Koshibebfbe1d72015-03-16 16:51:41 -070081 protected boolean ipv6NeighborDiscovery = false;
Pavlin Radoslavov93b606b2015-02-25 17:28:39 -080082
alshabibc274c902014-10-03 14:58:27 -070083 @Activate
Pavlin Radoslavov93b606b2015-02-25 17:28:39 -080084 public void activate(ComponentContext context) {
Thomas Vachuska6519e6f2015-03-11 02:29:31 -070085 cfgService.registerProperties(getClass());
Brian O'Connorabafb502014-12-02 22:26:20 -080086 appId = coreService.registerApplication("org.onosproject.proxyarp");
Pavlin Radoslavov93b606b2015-02-25 17:28:39 -080087
Brian O'Connor3b783262015-07-29 17:49:24 -070088 packetService.addProcessor(processor, PacketProcessor.director(1));
Charles M.C. Chane148de82015-05-06 12:38:21 +080089 readComponentConfiguration(context);
Jonathan Hart39ee6482015-08-31 16:00:19 +020090 requestPackets();
Jonathan Hart3cfce8e2015-01-14 16:43:27 -080091
Charles M.C. Chane148de82015-05-06 12:38:21 +080092 log.info("Started with Application ID {}", appId.id());
93 }
94
95 @Deactivate
96 public void deactivate() {
Charles M.C. Chane148de82015-05-06 12:38:21 +080097 cfgService.unregisterProperties(getClass(), false);
Aaron Kruglikov07a923d2015-07-03 13:30:57 -070098 withdrawIntercepts();
Charles M.C. Chane148de82015-05-06 12:38:21 +080099 packetService.removeProcessor(processor);
100 processor = null;
101 log.info("Stopped");
102 }
103
104 @Modified
105 public void modified(ComponentContext context) {
Charles M.C. Chane148de82015-05-06 12:38:21 +0800106 readComponentConfiguration(context);
Jonathan Hart39ee6482015-08-31 16:00:19 +0200107 requestPackets();
Charles M.C. Chane148de82015-05-06 12:38:21 +0800108 }
109
110 /**
111 * Request packet in via PacketService.
112 */
Jonathan Hart39ee6482015-08-31 16:00:19 +0200113 private void requestPackets() {
Jonathan Hart3cfce8e2015-01-14 16:43:27 -0800114 TrafficSelector.Builder selectorBuilder =
115 DefaultTrafficSelector.builder();
Aaron Kruglikov07a923d2015-07-03 13:30:57 -0700116 selectorBuilder.matchEthType(TYPE_ARP);
Jonathan Hart3cfce8e2015-01-14 16:43:27 -0800117 packetService.requestPackets(selectorBuilder.build(),
Aaron Kruglikov07a923d2015-07-03 13:30:57 -0700118 CONTROL, appId);
Jonathan Hart3cfce8e2015-01-14 16:43:27 -0800119
Aaron Kruglikov07a923d2015-07-03 13:30:57 -0700120 selectorBuilder = DefaultTrafficSelector.builder();
121 selectorBuilder.matchEthType(TYPE_IPV6);
122 selectorBuilder.matchIPProtocol(PROTOCOL_ICMP6);
123 selectorBuilder.matchIcmpv6Type(NEIGHBOR_SOLICITATION);
Pavlin Radoslavov93b606b2015-02-25 17:28:39 -0800124 if (ipv6NeighborDiscovery) {
125 // IPv6 Neighbor Solicitation packet.
Pavlin Radoslavov93b606b2015-02-25 17:28:39 -0800126 packetService.requestPackets(selectorBuilder.build(),
Aaron Kruglikov07a923d2015-07-03 13:30:57 -0700127 CONTROL, appId);
128 } else {
129 packetService.cancelPackets(selectorBuilder.build(),
130 CONTROL, appId);
Pavlin Radoslavov93b606b2015-02-25 17:28:39 -0800131 }
Aaron Kruglikov07a923d2015-07-03 13:30:57 -0700132
133 // IPv6 Neighbor Advertisement packet.
134 selectorBuilder = DefaultTrafficSelector.builder();
135 selectorBuilder.matchEthType(TYPE_IPV6);
136 selectorBuilder.matchIPProtocol(PROTOCOL_ICMP6);
137 selectorBuilder.matchIcmpv6Type(NEIGHBOR_ADVERTISEMENT);
138 if (ipv6NeighborDiscovery) {
139 packetService.requestPackets(selectorBuilder.build(),
140 CONTROL, appId);
141 } else {
142 packetService.cancelPackets(selectorBuilder.build(),
143 CONTROL, appId);
144 }
145
146
147 }
148
149 /**
150 * Cancel requested packet in via packet service.
151 */
152 private void withdrawIntercepts() {
153 TrafficSelector.Builder selectorBuilder =
154 DefaultTrafficSelector.builder();
155 selectorBuilder.matchEthType(TYPE_ARP);
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_SOLICITATION);
161 packetService.cancelPackets(selectorBuilder.build(), CONTROL, appId);
162 selectorBuilder = DefaultTrafficSelector.builder();
163 selectorBuilder.matchEthType(TYPE_IPV6);
164 selectorBuilder.matchIPProtocol(PROTOCOL_ICMP6);
165 selectorBuilder.matchIcmpv6Type(NEIGHBOR_ADVERTISEMENT);
166 packetService.cancelPackets(selectorBuilder.build(), CONTROL, appId);
167
Pavlin Radoslavov93b606b2015-02-25 17:28:39 -0800168 }
169
170 /**
171 * Extracts properties from the component configuration context.
172 *
173 * @param context the component context
174 */
175 private void readComponentConfiguration(ComponentContext context) {
176 Dictionary<?, ?> properties = context.getProperties();
177 Boolean flag;
178
Jian Lid9b5f552016-03-11 18:15:31 -0800179 flag = Tools.isPropertyEnabled(properties, "ipv6NeighborDiscovery");
Pavlin Radoslavov93b606b2015-02-25 17:28:39 -0800180 if (flag == null) {
181 log.info("IPv6 Neighbor Discovery is not configured, " +
Thomas Vachuska6519e6f2015-03-11 02:29:31 -0700182 "using current value of {}", ipv6NeighborDiscovery);
Pavlin Radoslavov93b606b2015-02-25 17:28:39 -0800183 } else {
184 ipv6NeighborDiscovery = flag;
185 log.info("Configured. IPv6 Neighbor Discovery is {}",
186 ipv6NeighborDiscovery ? "enabled" : "disabled");
187 }
188 }
189
190 /**
alshabibc274c902014-10-03 14:58:27 -0700191 * Packet processor responsible for forwarding packets along their paths.
192 */
193 private class ProxyArpProcessor implements PacketProcessor {
194
195 @Override
196 public void process(PacketContext context) {
197 // Stop processing if the packet has been handled, since we
198 // can't do any more to it.
199 if (context.isHandled()) {
200 return;
201 }
Brian O'Connorbc865f62015-10-08 21:39:53 -0700202
Ayaka Koshibebfbe1d72015-03-16 16:51:41 -0700203 InboundPacket pkt = context.inPacket();
204 Ethernet ethPkt = pkt.parsed();
205 if (ethPkt == null) {
206 return;
207 }
Brian O'Connorbc865f62015-10-08 21:39:53 -0700208
209 if (ethPkt.getEtherType() == TYPE_ARP) {
210 //handle the arp packet.
211 proxyArpService.handlePacket(context);
212 } else if (ipv6NeighborDiscovery && ethPkt.getEtherType() == TYPE_IPV6) {
213 IPv6 ipv6Pkt = (IPv6) ethPkt.getPayload();
214 if (ipv6Pkt.getNextHeader() == IPv6.PROTOCOL_ICMP6) {
215 ICMP6 icmp6Pkt = (ICMP6) ipv6Pkt.getPayload();
216 if (icmp6Pkt.getIcmpType() == NEIGHBOR_SOLICITATION ||
217 icmp6Pkt.getIcmpType() == NEIGHBOR_ADVERTISEMENT) {
218 // handle ICMPv6 solicitations and advertisements
219 proxyArpService.handlePacket(context);
220 }
221 }
Ayaka Koshibebfbe1d72015-03-16 16:51:41 -0700222 }
Rusty Eddy29acad62015-07-07 19:33:47 -0700223
Brian O'Connorbc865f62015-10-08 21:39:53 -0700224 // FIXME why were we listening to IPv4 frames at all?
Rusty Eddy29acad62015-07-07 19:33:47 -0700225 // Do not ARP for multicast packets. Let mfwd handle them.
226 if (ethPkt.getEtherType() == Ethernet.TYPE_IPV4) {
227 if (ethPkt.getDestinationMAC().isMulticast()) {
228 return;
229 }
230 }
alshabibc274c902014-10-03 14:58:27 -0700231 }
232 }
233}