Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [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 core |
| 18 | |
| 19 | import ( |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 20 | "github.com/google/gopacket" |
| 21 | "github.com/google/gopacket/layers" |
| 22 | "github.com/google/gopacket/pcap" |
Keita NISHIMOTO | b841749 | 2018-10-19 17:37:38 +0900 | [diff] [blame^] | 23 | "errors" |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 24 | "log" |
| 25 | "net" |
| 26 | ) |
| 27 | |
| 28 | func RecvWorker(io *Ioinfo, handler *pcap.Handle, r chan Packet) { |
| 29 | log.Printf("recvWorker runs. handler: %v", *handler) |
| 30 | packetSource := gopacket.NewPacketSource(handler, handler.LinkType()) |
| 31 | for packet := range packetSource.Packets() { |
| 32 | log.Printf("recv packet from IF: %v \n", *handler) |
| 33 | //log.Println(packet.Dump()) |
| 34 | pkt := Packet{} |
| 35 | pkt.Info = io |
| 36 | pkt.Pkt = packet |
| 37 | r <- pkt |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | func SendUni(handle *pcap.Handle, packet gopacket.Packet) { |
Keita NISHIMOTO | b841749 | 2018-10-19 17:37:38 +0900 | [diff] [blame^] | 42 | err := handle.WritePacketData(packet.Data()) |
| 43 | if err != nil { |
| 44 | log.Printf("Error in send packet to UNI-IF: %v e:%s\n", *handle, err) |
| 45 | } |
| 46 | log.Printf("Successfully send packet to UNI-IF: %v \n", *handle) |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 47 | //log.Println(packet.Dump()) |
| 48 | } |
| 49 | |
| 50 | func SendNni(handle *pcap.Handle, packet gopacket.Packet) { |
Keita NISHIMOTO | b841749 | 2018-10-19 17:37:38 +0900 | [diff] [blame^] | 51 | err := handle.WritePacketData(packet.Data()) |
| 52 | if err != nil{ |
| 53 | log.Printf("Error in send packet to NNI e:%s\n", err) |
| 54 | } |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 55 | log.Printf("send packet to NNI-IF: %v \n", *handle) |
| 56 | //log.Println(packet.Dump()) |
| 57 | } |
| 58 | |
| 59 | func PopVLAN(pkt gopacket.Packet) (gopacket.Packet, uint16, error) { |
| 60 | if layer := getDot1QLayer(pkt); layer != nil { |
| 61 | if eth := getEthernetLayer(pkt); eth != nil { |
| 62 | ethernetLayer := &layers.Ethernet{ |
| 63 | SrcMAC: eth.SrcMAC, |
| 64 | DstMAC: eth.DstMAC, |
| 65 | EthernetType: layer.Type, |
| 66 | } |
| 67 | buffer := gopacket.NewSerializeBuffer() |
| 68 | gopacket.SerializeLayers(buffer, gopacket.SerializeOptions{}, |
| 69 | ethernetLayer, |
| 70 | gopacket.Payload(layer.Payload), |
| 71 | ) |
| 72 | retpkt := gopacket.NewPacket( |
| 73 | buffer.Bytes(), |
| 74 | layers.LayerTypeEthernet, |
| 75 | gopacket.Default, |
| 76 | ) |
| 77 | vid := uint16(4095 & layer.VLANIdentifier) |
| 78 | log.Printf("Pop the 802.1Q header (VID: %d)", vid) |
| 79 | return retpkt, vid, nil |
| 80 | } |
| 81 | } |
Keita NISHIMOTO | c864da2 | 2018-10-15 22:41:42 +0900 | [diff] [blame] | 82 | return pkt, 0, nil |
| 83 | //return nil, 0, errors.New("failed to pop vlan") |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | func PushVLAN(pkt gopacket.Packet, vid uint16) (gopacket.Packet, error) { |
| 87 | if eth := getEthernetLayer(pkt); eth != nil { |
| 88 | ethernetLayer := &layers.Ethernet{ |
| 89 | SrcMAC: eth.SrcMAC, |
| 90 | DstMAC: eth.DstMAC, |
| 91 | EthernetType: 0x8100, |
| 92 | } |
| 93 | dot1qLayer := &layers.Dot1Q{ |
| 94 | Type: eth.EthernetType, |
| 95 | VLANIdentifier: uint16(vid), |
| 96 | } |
| 97 | |
| 98 | buffer := gopacket.NewSerializeBuffer() |
| 99 | gopacket.SerializeLayers( |
| 100 | buffer, |
| 101 | gopacket.SerializeOptions{ |
| 102 | FixLengths: false, |
| 103 | }, |
| 104 | ethernetLayer, |
| 105 | dot1qLayer, |
| 106 | gopacket.Payload(eth.Payload), |
| 107 | ) |
| 108 | ret := gopacket.NewPacket( |
| 109 | buffer.Bytes(), |
| 110 | layers.LayerTypeEthernet, |
| 111 | gopacket.Default, |
| 112 | ) |
| 113 | log.Printf("Push the 802.1Q header (VID: %d)", vid) |
| 114 | return ret, nil |
| 115 | } |
| 116 | return nil, errors.New("failed to push vlan") |
| 117 | } |
| 118 | |
| 119 | func getEthernetLayer(pkt gopacket.Packet) *layers.Ethernet { |
| 120 | eth := &layers.Ethernet{} |
| 121 | if ethLayer := pkt.Layer(layers.LayerTypeEthernet); ethLayer != nil { |
| 122 | eth, _ = ethLayer.(*layers.Ethernet) |
| 123 | } |
| 124 | return eth |
| 125 | } |
| 126 | func getDot1QLayer(pkt gopacket.Packet) (dot1q *layers.Dot1Q) { |
| 127 | if dot1qLayer := pkt.Layer(layers.LayerTypeDot1Q); dot1qLayer != nil { |
| 128 | dot1q = dot1qLayer.(*layers.Dot1Q) |
| 129 | } |
| 130 | return dot1q |
| 131 | } |
| 132 | |
| 133 | func getMacAddress(ifName string) net.HardwareAddr { |
| 134 | var err error |
| 135 | var netIf *net.Interface |
| 136 | var hwAddr net.HardwareAddr |
| 137 | if netIf, err = net.InterfaceByName(ifName); err == nil { |
| 138 | hwAddr = netIf.HardwareAddr |
| 139 | } |
| 140 | return hwAddr |
| 141 | } |