blob: 0748839f2c13019c4347ebc00804c9ab3cfd6905 [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 Scandolof6f3a7f2019-10-11 11:19:29 -070033// return true if the packet is coming in the OLT from the NNI port
34// it uses the ack to check if the source is the one we assigned to the
35// dhcp server
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -070036func IsIncomingPacket(packet gopacket.Packet) bool {
37 if ipLayer := packet.Layer(layers.LayerTypeIPv4); ipLayer != nil {
38
39 ip, _ := ipLayer.(*layers.IPv4)
40
41 // FIXME find a better way to filter outgoing packets
42 if ip.SrcIP.Equal(net.ParseIP("182.21.0.128")) {
43 return true
44 }
45 }
46 return false
47}
Matteo Scandolof6f3a7f2019-10-11 11:19:29 -070048
49// returns the Desctination Mac Address contained in the packet
50func GetDstMacAddressFromPacket(packet gopacket.Packet) (net.HardwareAddr, error) {
51 if ethLayer := packet.Layer(layers.LayerTypeEthernet); ethLayer != nil {
52 eth, _ := ethLayer.(*layers.Ethernet)
53
54 if eth.DstMAC != nil {
55 return eth.DstMAC, nil
56 }
57 }
58 return nil, errors.New("cant-find-mac-address")
59}