blob: afb2c319cbffa3a810f5a23ef29ffd8b5c419ba1 [file] [log] [blame]
Harshada Chaundkardcd1b142019-03-25 17:27:44 -04001/*
2 * Copyright 2019-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 */
16package org.onosproject.net.packet.packetfilter;
17
18import org.onlab.packet.BasePacket;
19import org.onlab.packet.Ethernet;
20import org.onlab.packet.IPv6;
21import org.onlab.packet.Ip6Address;
22import org.onlab.packet.DHCP6;
23import org.onlab.packet.UDP;
24import org.onlab.packet.dhcp.Dhcp6RelayOption;
25import org.onosproject.net.packet.PacketContext;
26import org.onosproject.net.packet.PacketInClassifier;
27import org.slf4j.Logger;
28
29import java.util.Arrays;
30
31import static org.slf4j.LoggerFactory.getLogger;
32
33public class Dhcp6IndirectPacketClassifier implements PacketInClassifier {
34 private final Logger log = getLogger(getClass());
35
36 @Override
37 public boolean match(PacketContext packet) {
38
39 Ethernet eth = packet.inPacket().parsed();
40
41 if (eth.getEtherType() == Ethernet.TYPE_IPV6) {
42 IPv6 ipv6Packet = (IPv6) eth.getPayload();
43
44 if (ipv6Packet.getNextHeader() == IPv6.PROTOCOL_UDP) {
45 UDP udpPacket = (UDP) ipv6Packet.getPayload();
46 //Indirectly connected host
47 if (udpPacket.getDestinationPort() == UDP.DHCP_V6_SERVER_PORT &&
48 udpPacket.getSourcePort() == UDP.DHCP_V6_SERVER_PORT &&
49 Arrays.equals(ipv6Packet.getDestinationAddress(),
50 Ip6Address.valueOf("ff02::1:2").toOctets())) {
51 DHCP6 relayMessage = (DHCP6) udpPacket.getPayload();
52 DHCP6 dhcp6 = (DHCP6) relayMessage.getOptions().stream()
53 .filter(opt -> opt instanceof Dhcp6RelayOption)
54 .map(BasePacket::getPayload)
55 .map(pld -> (DHCP6) pld)
56 .findFirst()
57 .orElse(null);
58
59 if (dhcp6.getMsgType() == DHCP6.MsgType.SOLICIT.value()) {
60 return true;
61 }
62 }
63 }
64 }
65 return false;
66 }
67}