blob: 2e013affd6716c8b91fd122517a25ab5939e0ea5 [file] [log] [blame]
Jonathan Hart9bdaaec2016-08-22 13:33:45 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
Jonathan Hart9bdaaec2016-08-22 13:33:45 -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 */
16
Ray Milkeyb65d7842017-08-03 16:28:24 -070017package org.onosproject.net.neighbour.impl;
Jonathan Hart9bdaaec2016-08-22 13:33:45 -070018
Ray Milkeyb65d7842017-08-03 16:28:24 -070019import java.util.Objects;
20
Jonathan Hart9bdaaec2016-08-22 13:33:45 -070021import org.onlab.packet.ARP;
22import org.onlab.packet.Ethernet;
23import org.onlab.packet.ICMP6;
24import org.onlab.packet.IPv6;
25import org.onlab.packet.Ip4Address;
26import org.onlab.packet.Ip6Address;
27import org.onlab.packet.IpAddress;
28import org.onlab.packet.MacAddress;
29import org.onlab.packet.VlanId;
30import org.onlab.packet.ndp.NeighborSolicitation;
Jonathan Hart9bdaaec2016-08-22 13:33:45 -070031import org.onosproject.net.ConnectPoint;
Ray Milkeyb65d7842017-08-03 16:28:24 -070032import org.onosproject.net.intf.Interface;
33import org.onosproject.net.neighbour.NeighbourMessageActions;
34import org.onosproject.net.neighbour.NeighbourMessageContext;
35import org.onosproject.net.neighbour.NeighbourMessageType;
36import org.onosproject.net.neighbour.NeighbourProtocol;
Jonathan Hart584ea2d2016-10-11 10:49:16 +020037
Jonathan Hart9bdaaec2016-08-22 13:33:45 -070038import static com.google.common.base.Preconditions.checkState;
39
40/**
41 * Default implementation of a neighbour message context.
42 */
Ray Milkeyb65d7842017-08-03 16:28:24 -070043
Jonathan Hart9bdaaec2016-08-22 13:33:45 -070044public class DefaultNeighbourMessageContext implements NeighbourMessageContext {
45
46 private final NeighbourProtocol protocol;
47 private final NeighbourMessageType type;
48
49 private final IpAddress target;
50 private final IpAddress sender;
51
52 private final Ethernet eth;
53 private final ConnectPoint inPort;
54
55 private final NeighbourMessageActions actions;
56
57 /**
58 * Creates a new neighbour message context.
59 *
60 * @param actions actions
61 * @param eth ethernet frame
62 * @param inPort incoming port
63 * @param protocol message protocol
64 * @param type message type
65 * @param target target IP address
66 * @param sender sender IP address
67 */
68 DefaultNeighbourMessageContext(NeighbourMessageActions actions,
69 Ethernet eth, ConnectPoint inPort,
70 NeighbourProtocol protocol,
71 NeighbourMessageType type,
72 IpAddress target, IpAddress sender) {
73 this.actions = actions;
74 this.eth = eth;
75 this.inPort = inPort;
76 this.protocol = protocol;
77 this.type = type;
78 this.target = target;
79 this.sender = sender;
80 }
81
82 @Override
83 public ConnectPoint inPort() {
84 return inPort;
85 }
86
87 @Override
88 public Ethernet packet() {
89 return eth;
90 }
91
92 @Override
93 public NeighbourProtocol protocol() {
94 return protocol;
95 }
96
97 @Override
98 public NeighbourMessageType type() {
99 return type;
100 }
101
102 @Override
103 public VlanId vlan() {
104 return VlanId.vlanId(eth.getVlanID());
105 }
106
107 @Override
108 public MacAddress srcMac() {
Jonathan Hartc4f681c2016-09-09 07:14:25 -0700109 return eth.getSourceMAC();
110 }
111
112 @Override
113 public MacAddress dstMac() {
114 return eth.getDestinationMAC();
Jonathan Hart9bdaaec2016-08-22 13:33:45 -0700115 }
116
117 @Override
118 public IpAddress target() {
119 return target;
120 }
121
122 @Override
123 public IpAddress sender() {
124 return sender;
125 }
126
127 @Override
Jonathan Hart2efe0c22016-09-21 12:04:45 -0700128 public void forward(ConnectPoint outPort) {
Jonathan Hart1e393bb2016-09-14 08:51:09 -0700129 actions.forward(this, outPort);
Jonathan Hart9bdaaec2016-08-22 13:33:45 -0700130 }
131
132 @Override
Jonathan Hart2efe0c22016-09-21 12:04:45 -0700133 public void forward(Interface outIntf) {
Jonathan Hart1e393bb2016-09-14 08:51:09 -0700134 actions.forward(this, outIntf);
Jonathan Hart9bdaaec2016-08-22 13:33:45 -0700135 }
136
137 @Override
138 public void reply(MacAddress targetMac) {
139 checkState(type == NeighbourMessageType.REQUEST, "can only reply to requests");
140
141 actions.reply(this, targetMac);
142 }
143
144 @Override
145 public void flood() {
146 actions.flood(this);
147 }
148
149 @Override
150 public void drop() {
151 actions.drop(this);
152 }
153
Jonathan Hart584ea2d2016-10-11 10:49:16 +0200154 @Override
155 public int hashCode() {
156 return Objects.hash(protocol, type, target, sender, eth, inPort);
157 }
158
159 @Override
160 public boolean equals(Object obj) {
161 if (!(obj instanceof DefaultNeighbourMessageContext)) {
162 return false;
163 }
164
165 DefaultNeighbourMessageContext that = (DefaultNeighbourMessageContext) obj;
166
167 return Objects.equals(protocol, that.protocol) &&
168 Objects.equals(type, that.type) &&
169 Objects.equals(target, that.target) &&
170 Objects.equals(sender, that.sender) &&
171 Objects.equals(eth, that.eth) &&
172 Objects.equals(inPort, that.inPort);
173 }
174
Jonathan Hart9bdaaec2016-08-22 13:33:45 -0700175 /**
176 * Attempts to create a MessageContext for the given Ethernet frame. If the
177 * frame is a valid ARP or NDP request or response, a context will be
178 * created.
179 *
180 * @param eth input Ethernet frame
181 * @param inPort in port
Ray Milkeyef794342016-11-09 16:20:29 -0800182 * @param actions actions to take
Jonathan Hart9bdaaec2016-08-22 13:33:45 -0700183 * @return MessageContext if the packet was ARP or NDP, otherwise null
184 */
185 public static NeighbourMessageContext createContext(Ethernet eth,
186 ConnectPoint inPort,
187 NeighbourMessageActions actions) {
188 if (eth.getEtherType() == Ethernet.TYPE_ARP) {
189 return createArpContext(eth, inPort, actions);
190 } else if (eth.getEtherType() == Ethernet.TYPE_IPV6) {
191 return createNdpContext(eth, inPort, actions);
192 }
193
194 return null;
195 }
196
197 /**
198 * Extracts context information from ARP packets.
199 *
200 * @param eth input Ethernet frame that is thought to be ARP
201 * @param inPort in port
Ray Milkeyef794342016-11-09 16:20:29 -0800202 * @param actions actions to take
Jonathan Hart9bdaaec2016-08-22 13:33:45 -0700203 * @return MessageContext object if the packet was a valid ARP packet,
204 * otherwise null
205 */
206 private static NeighbourMessageContext createArpContext(Ethernet eth,
207 ConnectPoint inPort,
208 NeighbourMessageActions actions) {
209 if (eth.getEtherType() != Ethernet.TYPE_ARP) {
210 return null;
211 }
212
213 ARP arp = (ARP) eth.getPayload();
214
215 IpAddress target = Ip4Address.valueOf(arp.getTargetProtocolAddress());
216 IpAddress sender = Ip4Address.valueOf(arp.getSenderProtocolAddress());
217
218 NeighbourMessageType type;
219 if (arp.getOpCode() == ARP.OP_REQUEST) {
220 type = NeighbourMessageType.REQUEST;
221 } else if (arp.getOpCode() == ARP.OP_REPLY) {
222 type = NeighbourMessageType.REPLY;
223 } else {
224 return null;
225 }
226
227 return new DefaultNeighbourMessageContext(actions, eth, inPort,
228 NeighbourProtocol.ARP, type, target, sender);
229 }
230
231 /**
232 * Extracts context information from NDP packets.
233 *
234 * @param eth input Ethernet frame that is thought to be NDP
235 * @param inPort in port
Ray Milkeyef794342016-11-09 16:20:29 -0800236 * @param actions actions to take
Jonathan Hart9bdaaec2016-08-22 13:33:45 -0700237 * @return MessageContext object if the packet was a valid NDP packet,
238 * otherwise null
239 */
240 private static NeighbourMessageContext createNdpContext(Ethernet eth,
241 ConnectPoint inPort,
242 NeighbourMessageActions actions) {
243 if (eth.getEtherType() != Ethernet.TYPE_IPV6) {
244 return null;
245 }
246 IPv6 ipv6 = (IPv6) eth.getPayload();
247
248 if (ipv6.getNextHeader() != IPv6.PROTOCOL_ICMP6) {
249 return null;
250 }
251 ICMP6 icmpv6 = (ICMP6) ipv6.getPayload();
252
253 IpAddress sender = Ip6Address.valueOf(ipv6.getSourceAddress());
Pier Ventre48ca5192016-11-28 16:52:24 -0800254 IpAddress target;
Jonathan Hart9bdaaec2016-08-22 13:33:45 -0700255
256 NeighbourMessageType type;
257 if (icmpv6.getIcmpType() == ICMP6.NEIGHBOR_SOLICITATION) {
258 type = NeighbourMessageType.REQUEST;
259 NeighborSolicitation nsol = (NeighborSolicitation) icmpv6.getPayload();
260 target = Ip6Address.valueOf(nsol.getTargetAddress());
261 } else if (icmpv6.getIcmpType() == ICMP6.NEIGHBOR_ADVERTISEMENT) {
262 type = NeighbourMessageType.REPLY;
Pier Ventre48ca5192016-11-28 16:52:24 -0800263 /*
264 * sender and target are the same in the reply.
265 * We use as target the destination ip.
266 */
267 target = Ip6Address.valueOf(ipv6.getDestinationAddress());
Jonathan Hart9bdaaec2016-08-22 13:33:45 -0700268 } else {
269 return null;
270 }
271
272 return new DefaultNeighbourMessageContext(actions, eth, inPort,
273 NeighbourProtocol.NDP, type, target, sender);
274 }
275
276}