Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018-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 | */ |
| 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 | 73c488d | 2019-11-01 14:44:22 -0700 | [diff] [blame^] | 33 | func IsLldpPacket(pkt gopacket.Packet) bool { |
| 34 | if layer := pkt.Layer(layers.LayerTypeLinkLayerDiscovery); layer != nil { |
| 35 | return true |
| 36 | } |
| 37 | return false |
| 38 | } |
| 39 | |
Matteo Scandolo | f6f3a7f | 2019-10-11 11:19:29 -0700 | [diff] [blame] | 40 | // return true if the packet is coming in the OLT from the NNI port |
| 41 | // it uses the ack to check if the source is the one we assigned to the |
| 42 | // dhcp server |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 43 | func IsIncomingPacket(packet gopacket.Packet) bool { |
| 44 | if ipLayer := packet.Layer(layers.LayerTypeIPv4); ipLayer != nil { |
| 45 | |
| 46 | ip, _ := ipLayer.(*layers.IPv4) |
| 47 | |
| 48 | // FIXME find a better way to filter outgoing packets |
Matteo Scandolo | 40e067f | 2019-10-16 16:59:41 -0700 | [diff] [blame] | 49 | if ip.SrcIP.Equal(net.ParseIP("192.168.254.1")) { |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 50 | return true |
| 51 | } |
| 52 | } |
| 53 | return false |
| 54 | } |
Matteo Scandolo | f6f3a7f | 2019-10-11 11:19:29 -0700 | [diff] [blame] | 55 | |
Matteo Scandolo | 40e067f | 2019-10-16 16:59:41 -0700 | [diff] [blame] | 56 | // returns the Destination Mac Address contained in the packet |
Matteo Scandolo | f6f3a7f | 2019-10-11 11:19:29 -0700 | [diff] [blame] | 57 | func GetDstMacAddressFromPacket(packet gopacket.Packet) (net.HardwareAddr, error) { |
| 58 | if ethLayer := packet.Layer(layers.LayerTypeEthernet); ethLayer != nil { |
| 59 | eth, _ := ethLayer.(*layers.Ethernet) |
| 60 | |
| 61 | if eth.DstMAC != nil { |
| 62 | return eth.DstMAC, nil |
| 63 | } |
| 64 | } |
| 65 | return nil, errors.New("cant-find-mac-address") |
| 66 | } |
Matteo Scandolo | 40e067f | 2019-10-16 16:59:41 -0700 | [diff] [blame] | 67 | |
| 68 | // returns the Source Mac Address contained in the packet |
| 69 | func GetSrcMacAddressFromPacket(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.SrcMAC, nil |
| 75 | } |
| 76 | } |
| 77 | return nil, errors.New("cant-find-mac-address") |
| 78 | } |
| 79 | |
| 80 | // returns wether it's an EAPOL or DHCP packet, error if it's none |
| 81 | func IsEapolOrDhcp(pkt gopacket.Packet) (PacketType, error) { |
| 82 | if pkt.Layer(layers.LayerTypeEAP) != nil || pkt.Layer(layers.LayerTypeEAPOL) != nil { |
| 83 | return EAPOL, nil |
| 84 | } else if IsDhcpPacket(pkt) { |
| 85 | return DHCP, nil |
| 86 | } |
| 87 | return UNKNOWN, errors.New("packet-is-neither-eapol-or-dhcp") |
| 88 | } |