blob: c46f87d696b31a1005083ccc4232009f294d3894 [file] [log] [blame]
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -07001/*
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
17package packetHandlers
18
19import (
Matteo Scandolof6f3a7f2019-10-11 11:19:29 -070020 "errors"
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -070021 "github.com/google/gopacket"
22 "github.com/google/gopacket/layers"
23 "net"
24)
25
26func IsDhcpPacket(pkt gopacket.Packet) bool {
27 if layerDHCP := pkt.Layer(layers.LayerTypeDHCPv4); layerDHCP != nil {
28 return true
29 }
30 return false
31}
32
Matteo Scandolo618a6582020-09-09 12:21:29 -070033func IsIgmpPacket(pkt gopacket.Packet) bool {
34 if igmpLayer := pkt.Layer(layers.LayerTypeIGMP); igmpLayer != nil {
35 return true
36 }
37 return false
38}
39
Matteo Scandolo73c488d2019-11-01 14:44:22 -070040func IsLldpPacket(pkt gopacket.Packet) bool {
41 if layer := pkt.Layer(layers.LayerTypeLinkLayerDiscovery); layer != nil {
42 return true
43 }
44 return false
45}
46
Matteo Scandolof6f3a7f2019-10-11 11:19:29 -070047// return true if the packet is coming in the OLT from the NNI port
48// it uses the ack to check if the source is the one we assigned to the
49// dhcp server
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -070050func IsIncomingPacket(packet gopacket.Packet) bool {
51 if ipLayer := packet.Layer(layers.LayerTypeIPv4); ipLayer != nil {
52
53 ip, _ := ipLayer.(*layers.IPv4)
54
55 // FIXME find a better way to filter outgoing packets
Matteo Scandolo40e067f2019-10-16 16:59:41 -070056 if ip.SrcIP.Equal(net.ParseIP("192.168.254.1")) {
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -070057 return true
58 }
59 }
60 return false
61}
Matteo Scandolof6f3a7f2019-10-11 11:19:29 -070062
Matteo Scandolo40e067f2019-10-16 16:59:41 -070063// returns the Destination Mac Address contained in the packet
Matteo Scandolof6f3a7f2019-10-11 11:19:29 -070064func GetDstMacAddressFromPacket(packet gopacket.Packet) (net.HardwareAddr, error) {
65 if ethLayer := packet.Layer(layers.LayerTypeEthernet); ethLayer != nil {
66 eth, _ := ethLayer.(*layers.Ethernet)
67
68 if eth.DstMAC != nil {
69 return eth.DstMAC, nil
70 }
71 }
72 return nil, errors.New("cant-find-mac-address")
73}
Matteo Scandolo40e067f2019-10-16 16:59:41 -070074
75// returns the Source Mac Address contained in the packet
76func GetSrcMacAddressFromPacket(packet gopacket.Packet) (net.HardwareAddr, error) {
77 if ethLayer := packet.Layer(layers.LayerTypeEthernet); ethLayer != nil {
78 eth, _ := ethLayer.(*layers.Ethernet)
79
80 if eth.DstMAC != nil {
81 return eth.SrcMAC, nil
82 }
83 }
84 return nil, errors.New("cant-find-mac-address")
85}
86
87// returns wether it's an EAPOL or DHCP packet, error if it's none
Matteo Scandolo618a6582020-09-09 12:21:29 -070088func GetPktType(pkt gopacket.Packet) (PacketType, error) {
Matteo Scandolo40e067f2019-10-16 16:59:41 -070089 if pkt.Layer(layers.LayerTypeEAP) != nil || pkt.Layer(layers.LayerTypeEAPOL) != nil {
90 return EAPOL, nil
91 } else if IsDhcpPacket(pkt) {
92 return DHCP, nil
Matteo Scandolo618a6582020-09-09 12:21:29 -070093 } else if IsIgmpPacket(pkt) {
94 return IGMP, nil
Matteo Scandolo40e067f2019-10-16 16:59:41 -070095 }
Matteo Scandolo618a6582020-09-09 12:21:29 -070096 return UNKNOWN, errors.New("unknown-packet-type")
Matteo Scandolo40e067f2019-10-16 16:59:41 -070097}