Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 1 | /* |
Joey Armstrong | 2c03936 | 2024-02-04 18:51:52 -0500 | [diff] [blame^] | 2 | * Copyright 2018-2024 Open Networking Foundation (ONF) and the ONF Contributors |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 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 | */ |
| 16 | |
| 17 | package packetHandlers |
| 18 | |
| 19 | import ( |
Matteo Scandolo | f6f3a7f | 2019-10-11 11:19:29 -0700 | [diff] [blame] | 20 | "errors" |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 21 | "github.com/google/gopacket" |
| 22 | "github.com/google/gopacket/layers" |
| 23 | "net" |
| 24 | ) |
| 25 | |
| 26 | func IsDhcpPacket(pkt gopacket.Packet) bool { |
| 27 | if layerDHCP := pkt.Layer(layers.LayerTypeDHCPv4); layerDHCP != nil { |
| 28 | return true |
| 29 | } |
| 30 | return false |
| 31 | } |
| 32 | |
Matteo Scandolo | 618a658 | 2020-09-09 12:21:29 -0700 | [diff] [blame] | 33 | func IsIgmpPacket(pkt gopacket.Packet) bool { |
| 34 | if igmpLayer := pkt.Layer(layers.LayerTypeIGMP); igmpLayer != nil { |
| 35 | return true |
| 36 | } |
| 37 | return false |
| 38 | } |
| 39 | |
Matteo Scandolo | 73c488d | 2019-11-01 14:44:22 -0700 | [diff] [blame] | 40 | func IsLldpPacket(pkt gopacket.Packet) bool { |
| 41 | if layer := pkt.Layer(layers.LayerTypeLinkLayerDiscovery); layer != nil { |
| 42 | return true |
| 43 | } |
| 44 | return false |
| 45 | } |
| 46 | |
Matteo Scandolo | ec170af | 2021-10-07 11:53:22 -0700 | [diff] [blame] | 47 | func IsIcmpPacket(pkt gopacket.Packet) bool { |
| 48 | if layer := pkt.Layer(layers.LayerTypeICMPv6); layer != nil { |
| 49 | return true |
| 50 | } |
| 51 | return false |
| 52 | } |
| 53 | |
Matteo Scandolo | 90d08f6 | 2020-10-29 12:06:55 -0700 | [diff] [blame] | 54 | // return true if the packet is coming in the OLT from the DHCP Server |
| 55 | // given that we only check DHCP packets we can use the Operation |
| 56 | // Request are outgoing (toward the server) |
| 57 | // Replies are incoming (toward the OLT) |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 58 | func IsIncomingPacket(packet gopacket.Packet) bool { |
Matteo Scandolo | 90d08f6 | 2020-10-29 12:06:55 -0700 | [diff] [blame] | 59 | layerDHCP := packet.Layer(layers.LayerTypeDHCPv4) |
| 60 | if dhcp, ok := layerDHCP.(*layers.DHCPv4); ok { |
| 61 | if dhcp.Operation == layers.DHCPOpReply { |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 62 | return true |
| 63 | } |
| 64 | } |
| 65 | return false |
| 66 | } |
Matteo Scandolo | f6f3a7f | 2019-10-11 11:19:29 -0700 | [diff] [blame] | 67 | |
Matteo Scandolo | 40e067f | 2019-10-16 16:59:41 -0700 | [diff] [blame] | 68 | // returns the Destination Mac Address contained in the packet |
Matteo Scandolo | f6f3a7f | 2019-10-11 11:19:29 -0700 | [diff] [blame] | 69 | func GetDstMacAddressFromPacket(packet gopacket.Packet) (net.HardwareAddr, error) { |
| 70 | if ethLayer := packet.Layer(layers.LayerTypeEthernet); ethLayer != nil { |
| 71 | eth, _ := ethLayer.(*layers.Ethernet) |
| 72 | |
| 73 | if eth.DstMAC != nil { |
| 74 | return eth.DstMAC, nil |
| 75 | } |
| 76 | } |
| 77 | return nil, errors.New("cant-find-mac-address") |
| 78 | } |
Matteo Scandolo | 40e067f | 2019-10-16 16:59:41 -0700 | [diff] [blame] | 79 | |
| 80 | // returns the Source Mac Address contained in the packet |
| 81 | func GetSrcMacAddressFromPacket(packet gopacket.Packet) (net.HardwareAddr, error) { |
| 82 | if ethLayer := packet.Layer(layers.LayerTypeEthernet); ethLayer != nil { |
| 83 | eth, _ := ethLayer.(*layers.Ethernet) |
| 84 | |
| 85 | if eth.DstMAC != nil { |
| 86 | return eth.SrcMAC, nil |
| 87 | } |
| 88 | } |
| 89 | return nil, errors.New("cant-find-mac-address") |
| 90 | } |
| 91 | |
| 92 | // returns wether it's an EAPOL or DHCP packet, error if it's none |
Matteo Scandolo | 618a658 | 2020-09-09 12:21:29 -0700 | [diff] [blame] | 93 | func GetPktType(pkt gopacket.Packet) (PacketType, error) { |
Matteo Scandolo | 40e067f | 2019-10-16 16:59:41 -0700 | [diff] [blame] | 94 | if pkt.Layer(layers.LayerTypeEAP) != nil || pkt.Layer(layers.LayerTypeEAPOL) != nil { |
| 95 | return EAPOL, nil |
| 96 | } else if IsDhcpPacket(pkt) { |
| 97 | return DHCP, nil |
Matteo Scandolo | 618a658 | 2020-09-09 12:21:29 -0700 | [diff] [blame] | 98 | } else if IsIgmpPacket(pkt) { |
| 99 | return IGMP, nil |
Matteo Scandolo | ec170af | 2021-10-07 11:53:22 -0700 | [diff] [blame] | 100 | } else if IsIcmpPacket(pkt) { |
| 101 | return ICMP, nil |
Matteo Scandolo | 40e067f | 2019-10-16 16:59:41 -0700 | [diff] [blame] | 102 | } |
Matteo Scandolo | 618a658 | 2020-09-09 12:21:29 -0700 | [diff] [blame] | 103 | return UNKNOWN, errors.New("unknown-packet-type") |
Matteo Scandolo | 40e067f | 2019-10-16 16:59:41 -0700 | [diff] [blame] | 104 | } |