/
filters.go
104 lines (89 loc) · 2.92 KB
/
filters.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/*
* Copyright 2018-2024 Open Networking Foundation (ONF) and the ONF Contributors
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package packetHandlers
import (
"errors"
"github.com/google/gopacket"
"github.com/google/gopacket/layers"
"net"
)
func IsDhcpPacket(pkt gopacket.Packet) bool {
if layerDHCP := pkt.Layer(layers.LayerTypeDHCPv4); layerDHCP != nil {
return true
}
return false
}
func IsIgmpPacket(pkt gopacket.Packet) bool {
if igmpLayer := pkt.Layer(layers.LayerTypeIGMP); igmpLayer != nil {
return true
}
return false
}
func IsLldpPacket(pkt gopacket.Packet) bool {
if layer := pkt.Layer(layers.LayerTypeLinkLayerDiscovery); layer != nil {
return true
}
return false
}
func IsIcmpPacket(pkt gopacket.Packet) bool {
if layer := pkt.Layer(layers.LayerTypeICMPv6); layer != nil {
return true
}
return false
}
// return true if the packet is coming in the OLT from the DHCP Server
// given that we only check DHCP packets we can use the Operation
// Request are outgoing (toward the server)
// Replies are incoming (toward the OLT)
func IsIncomingPacket(packet gopacket.Packet) bool {
layerDHCP := packet.Layer(layers.LayerTypeDHCPv4)
if dhcp, ok := layerDHCP.(*layers.DHCPv4); ok {
if dhcp.Operation == layers.DHCPOpReply {
return true
}
}
return false
}
// returns the Destination Mac Address contained in the packet
func GetDstMacAddressFromPacket(packet gopacket.Packet) (net.HardwareAddr, error) {
if ethLayer := packet.Layer(layers.LayerTypeEthernet); ethLayer != nil {
eth, _ := ethLayer.(*layers.Ethernet)
if eth.DstMAC != nil {
return eth.DstMAC, nil
}
}
return nil, errors.New("cant-find-mac-address")
}
// returns the Source Mac Address contained in the packet
func GetSrcMacAddressFromPacket(packet gopacket.Packet) (net.HardwareAddr, error) {
if ethLayer := packet.Layer(layers.LayerTypeEthernet); ethLayer != nil {
eth, _ := ethLayer.(*layers.Ethernet)
if eth.DstMAC != nil {
return eth.SrcMAC, nil
}
}
return nil, errors.New("cant-find-mac-address")
}
// returns wether it's an EAPOL or DHCP packet, error if it's none
func GetPktType(pkt gopacket.Packet) (PacketType, error) {
if pkt.Layer(layers.LayerTypeEAP) != nil || pkt.Layer(layers.LayerTypeEAPOL) != nil {
return EAPOL, nil
} else if IsDhcpPacket(pkt) {
return DHCP, nil
} else if IsIgmpPacket(pkt) {
return IGMP, nil
} else if IsIcmpPacket(pkt) {
return ICMP, nil
}
return UNKNOWN, errors.New("unknown-packet-type")
}