blob: 649f52e475002339482333c049e3e4fdd140562e [file] [log] [blame]
sanghob35a6192015-04-01 13:05:26 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
sanghob35a6192015-04-01 13:05:26 -07003 *
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 */
16package org.onosproject.segmentrouting;
17
18import org.onlab.packet.ARP;
19import org.onlab.packet.Ethernet;
20import org.onlab.packet.Ip4Address;
21import org.onlab.packet.IpAddress;
Pier Ventre10bd8d12016-11-26 21:05:22 -080022import org.onlab.packet.IpPrefix;
sanghob35a6192015-04-01 13:05:26 -070023import org.onlab.packet.MacAddress;
Charles Chan68aa62d2015-11-09 16:37:23 -080024import org.onlab.packet.VlanId;
Pier Ventre10bd8d12016-11-26 21:05:22 -080025import org.onosproject.incubator.net.neighbour.NeighbourMessageContext;
sanghob35a6192015-04-01 13:05:26 -070026import org.onosproject.net.ConnectPoint;
27import org.onosproject.net.DeviceId;
Pier Ventre10bd8d12016-11-26 21:05:22 -080028import org.onosproject.net.host.HostService;
Charles Chan0b4e6182015-11-03 10:42:14 -080029import org.onosproject.segmentrouting.config.DeviceConfigNotFoundException;
Charles Chan03a73e02016-10-24 14:52:01 -070030import org.onosproject.segmentrouting.config.SegmentRoutingAppConfig;
sanghob35a6192015-04-01 13:05:26 -070031import org.slf4j.Logger;
32import org.slf4j.LoggerFactory;
33
Saurav Das837e0bb2015-10-30 17:45:38 -070034import java.util.Set;
Pier Ventre10bd8d12016-11-26 21:05:22 -080035import java.util.stream.Collectors;
Saurav Das837e0bb2015-10-30 17:45:38 -070036
Pier Ventre10bd8d12016-11-26 21:05:22 -080037import static org.onosproject.incubator.net.neighbour.NeighbourMessageType.REQUEST;
sanghob35a6192015-04-01 13:05:26 -070038
Charles Chane849c192016-01-11 18:28:54 -080039/**
40 * Handler of ARP packets that responses or forwards ARP packets that
41 * are sent to the controller.
42 */
Pier Ventre735b8c82016-12-02 08:16:05 -080043public class ArpHandler extends SegmentRoutingNeighbourHandler {
sanghob35a6192015-04-01 13:05:26 -070044
45 private static Logger log = LoggerFactory.getLogger(ArpHandler.class);
46
sanghob35a6192015-04-01 13:05:26 -070047 /**
48 * Creates an ArpHandler object.
49 *
50 * @param srManager SegmentRoutingManager object
51 */
52 public ArpHandler(SegmentRoutingManager srManager) {
Pier Ventre735b8c82016-12-02 08:16:05 -080053 super(srManager);
sanghob35a6192015-04-01 13:05:26 -070054 }
55
56 /**
57 * Processes incoming ARP packets.
Charles Chan68aa62d2015-11-09 16:37:23 -080058 *
sanghob35a6192015-04-01 13:05:26 -070059 * If it is an ARP request to router itself or known hosts,
60 * then it sends ARP response.
61 * If it is an ARP request to unknown hosts in its own subnet,
62 * then it flood the ARP request to the ports.
63 * If it is an ARP response, then set a flow rule for the host
64 * and forward any IP packets to the host in the packet buffer to the host.
Charles Chan68aa62d2015-11-09 16:37:23 -080065 * <p>
66 * Note: We handles all ARP packet in, even for those ARP packets between
67 * hosts in the same subnet.
68 * For an ARP packet with broadcast destination MAC,
69 * some switches pipelines will send it to the controller due to table miss,
Saurav Das49831642016-02-05 13:15:20 -080070 * other switches will flood the packets directly in the data plane without
Charles Chan68aa62d2015-11-09 16:37:23 -080071 * packet in.
72 * We can deal with both cases.
sanghob35a6192015-04-01 13:05:26 -070073 *
Pier Ventre10bd8d12016-11-26 21:05:22 -080074 * @param pkt incoming ARP packet and context information
75 * @param hostService the host service
sanghob35a6192015-04-01 13:05:26 -070076 */
Pier Ventre10bd8d12016-11-26 21:05:22 -080077 public void processPacketIn(NeighbourMessageContext pkt, HostService hostService) {
Charles Chand2edd472016-10-17 18:03:37 -070078
Charles Chan03a73e02016-10-24 14:52:01 -070079 SegmentRoutingAppConfig appConfig = srManager.cfgService
80 .getConfig(srManager.appId, SegmentRoutingAppConfig.class);
Pier Ventre10bd8d12016-11-26 21:05:22 -080081 if (appConfig != null && appConfig.suppressSubnet().contains(pkt.inPort())) {
Charles Chan03a73e02016-10-24 14:52:01 -070082 // Ignore ARP packets come from suppressed ports
Pier Ventref4b5fce2016-11-28 16:48:06 -080083 pkt.drop();
Charles Chan03a73e02016-10-24 14:52:01 -070084 return;
85 }
86
Pier Ventre10bd8d12016-11-26 21:05:22 -080087 if (!validateArpSpa(pkt)) {
Charles Chan505dfd82016-11-08 16:32:13 -080088 log.debug("Ignore ARP packet discovered on {} with unexpected src protocol address {}.",
Pier Ventre10bd8d12016-11-26 21:05:22 -080089 pkt.inPort(), pkt.sender().getIp4Address());
Pier Ventref4b5fce2016-11-28 16:48:06 -080090 pkt.drop();
Charles Chand2edd472016-10-17 18:03:37 -070091 return;
92 }
93
Pier Ventre10bd8d12016-11-26 21:05:22 -080094 if (pkt.type() == REQUEST) {
95 handleArpRequest(pkt, hostService);
sanghob35a6192015-04-01 13:05:26 -070096 } else {
Pier Ventre10bd8d12016-11-26 21:05:22 -080097 handleArpReply(pkt, hostService);
sanghob35a6192015-04-01 13:05:26 -070098 }
99 }
100
Pier Ventre10bd8d12016-11-26 21:05:22 -0800101 private void handleArpRequest(NeighbourMessageContext pkt, HostService hostService) {
Charles Chan68aa62d2015-11-09 16:37:23 -0800102 // ARP request for router. Send ARP reply.
Pier Ventre10bd8d12016-11-26 21:05:22 -0800103 if (isArpForRouter(pkt)) {
104 MacAddress targetMac = config.getRouterMacForAGatewayIp(pkt.target().getIp4Address());
Pier Ventre735b8c82016-12-02 08:16:05 -0800105 sendResponse(pkt, targetMac, hostService);
Charles Chaneb088e62015-10-15 10:48:13 -0700106 } else {
Charles Chan563a7812017-02-27 15:50:43 -0800107 // NOTE: Ignore ARP packets except those target for the router
108 // We will reconsider enabling this when we have host learning support
109 /*
Pier Ventre10bd8d12016-11-26 21:05:22 -0800110 Set<Host> hosts = hostService.getHostsByIp(pkt.target());
111 if (hosts.size() > 1) {
112 log.warn("More than one host with the same ip {}", pkt.target());
113 }
114 Host targetHost = hosts.stream().findFirst().orElse(null);
Charles Chan68aa62d2015-11-09 16:37:23 -0800115 // ARP request for known hosts. Send proxy ARP reply on behalf of the target.
Charles Chaneb088e62015-10-15 10:48:13 -0700116 if (targetHost != null) {
Pier Ventre10bd8d12016-11-26 21:05:22 -0800117 pkt.forward(targetHost.location());
Charles Chan68aa62d2015-11-09 16:37:23 -0800118 // ARP request for unknown host in the subnet. Flood in the subnet.
119 } else {
Pier Ventre10bd8d12016-11-26 21:05:22 -0800120 flood(pkt);
Charles Chan68aa62d2015-11-09 16:37:23 -0800121 }
Charles Chan563a7812017-02-27 15:50:43 -0800122 */
Charles Chan68aa62d2015-11-09 16:37:23 -0800123 }
124 }
sanghob35a6192015-04-01 13:05:26 -0700125
Pier Ventre10bd8d12016-11-26 21:05:22 -0800126 private void handleArpReply(NeighbourMessageContext pkt, HostService hostService) {
Charles Chan68aa62d2015-11-09 16:37:23 -0800127 // ARP reply for router. Process all pending IP packets.
Pier Ventre10bd8d12016-11-26 21:05:22 -0800128 if (isArpForRouter(pkt)) {
129 Ip4Address hostIpAddress = pkt.sender().getIp4Address();
130 srManager.ipHandler.forwardPackets(pkt.inPort().deviceId(), hostIpAddress);
Charles Chan68aa62d2015-11-09 16:37:23 -0800131 } else {
Charles Chan563a7812017-02-27 15:50:43 -0800132 // NOTE: Ignore ARP packets except those target for the router
133 // We will reconsider enabling this when we have host learning support
134 /*
Pier Ventre10bd8d12016-11-26 21:05:22 -0800135 HostId targetHostId = HostId.hostId(pkt.dstMac(), pkt.vlan());
136 Host targetHost = hostService.getHost(targetHostId);
Charles Chan68aa62d2015-11-09 16:37:23 -0800137 // ARP reply for known hosts. Forward to the host.
138 if (targetHost != null) {
Pier Ventre10bd8d12016-11-26 21:05:22 -0800139 pkt.forward(targetHost.location());
Charles Chan68aa62d2015-11-09 16:37:23 -0800140 // ARP reply for unknown host, Flood in the subnet.
141 } else {
142 // Don't flood to non-edge ports
Charles Chan59cc16d2017-02-02 16:20:42 -0800143 if (pkt.vlan().equals(SegmentRoutingManager.INTERNAL_VLAN)) {
Charles Chan68aa62d2015-11-09 16:37:23 -0800144 return;
145 }
Pier Ventre10bd8d12016-11-26 21:05:22 -0800146 flood(pkt);
Charles Chaneb088e62015-10-15 10:48:13 -0700147 }
Charles Chan563a7812017-02-27 15:50:43 -0800148 */
sanghob35a6192015-04-01 13:05:26 -0700149 }
150 }
151
Charles Chand2edd472016-10-17 18:03:37 -0700152 /**
153 * Check if the source protocol address of an ARP packet belongs to the same
154 * subnet configured on the port it is seen.
155 *
Pier Ventre10bd8d12016-11-26 21:05:22 -0800156 * @param pkt ARP packet and context information
Charles Chand2edd472016-10-17 18:03:37 -0700157 * @return true if the source protocol address belongs to the configured subnet
158 */
Pier Ventre10bd8d12016-11-26 21:05:22 -0800159 private boolean validateArpSpa(NeighbourMessageContext pkt) {
160 Ip4Address spa = pkt.sender().getIp4Address();
161 Set<IpPrefix> subnet = config.getPortSubnets(pkt.inPort().deviceId(), pkt.inPort().port())
162 .stream()
163 .filter(ipPrefix -> ipPrefix.isIp4() && ipPrefix.contains(spa))
164 .collect(Collectors.toSet());
165 return !subnet.isEmpty();
Charles Chand2edd472016-10-17 18:03:37 -0700166 }
167
sanghob35a6192015-04-01 13:05:26 -0700168
Pier Ventre10bd8d12016-11-26 21:05:22 -0800169 private boolean isArpForRouter(NeighbourMessageContext pkt) {
170 Ip4Address targetProtocolAddress = pkt.target().getIp4Address();
171 Set<IpAddress> gatewayIpAddresses = null;
Saurav Das4ce45962015-11-24 23:21:05 -0800172 try {
Pier Ventre10bd8d12016-11-26 21:05:22 -0800173 if (targetProtocolAddress.equals(config.getRouterIpv4(pkt.inPort().deviceId()))) {
sanghob35a6192015-04-01 13:05:26 -0700174 return true;
175 }
Pier Ventre10bd8d12016-11-26 21:05:22 -0800176 gatewayIpAddresses = config.getPortIPs(pkt.inPort().deviceId());
Saurav Das4ce45962015-11-24 23:21:05 -0800177 } catch (DeviceConfigNotFoundException e) {
178 log.warn(e.getMessage() + " Aborting check for router IP in processing arp");
179 }
180 if (gatewayIpAddresses != null &&
181 gatewayIpAddresses.contains(targetProtocolAddress)) {
182 return true;
sanghob35a6192015-04-01 13:05:26 -0700183 }
184 return false;
185 }
186
sanghob35a6192015-04-01 13:05:26 -0700187 /**
188 * Sends an APR request for the target IP address to all ports except in-port.
189 *
190 * @param deviceId Switch device ID
191 * @param targetAddress target IP address for ARP
192 * @param inPort in-port
193 */
194 public void sendArpRequest(DeviceId deviceId, IpAddress targetAddress, ConnectPoint inPort) {
Pier Ventre735b8c82016-12-02 08:16:05 -0800195 byte[] senderMacAddress = new byte[MacAddress.MAC_ADDRESS_LENGTH];
196 byte[] senderIpAddress = new byte[Ip4Address.BYTE_LENGTH];
197 /*
198 * Retrieves device info.
199 */
Pier Luigia905c0c2017-01-29 12:38:48 -0800200 if (!getSenderInfo(senderMacAddress, senderIpAddress, deviceId, targetAddress)) {
201 log.warn("Aborting sendArpRequest, we cannot get all the information needed");
202 return;
203 }
Pier Ventre735b8c82016-12-02 08:16:05 -0800204 /*
205 * Creates the request.
206 */
207 Ethernet arpRequest = ARP.buildArpRequest(
208 senderMacAddress,
209 senderIpAddress,
210 targetAddress.toOctets(),
211 VlanId.NO_VID
Charles Chan68aa62d2015-11-09 16:37:23 -0800212 );
Pier Ventre735b8c82016-12-02 08:16:05 -0800213 flood(arpRequest, inPort, targetAddress);
Pier Ventre10bd8d12016-11-26 21:05:22 -0800214 }
215
sanghob35a6192015-04-01 13:05:26 -0700216}