linux_sll.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. // Copyright 2012 Google, Inc. All rights reserved.
  2. //
  3. // Use of this source code is governed by a BSD-style license
  4. // that can be found in the LICENSE file in the root of the source
  5. // tree.
  6. package layers
  7. import (
  8. "encoding/binary"
  9. "errors"
  10. "fmt"
  11. "net"
  12. "github.com/google/gopacket"
  13. )
  14. type LinuxSLLPacketType uint16
  15. const (
  16. LinuxSLLPacketTypeHost LinuxSLLPacketType = 0 // To us
  17. LinuxSLLPacketTypeBroadcast LinuxSLLPacketType = 1 // To all
  18. LinuxSLLPacketTypeMulticast LinuxSLLPacketType = 2 // To group
  19. LinuxSLLPacketTypeOtherhost LinuxSLLPacketType = 3 // To someone else
  20. LinuxSLLPacketTypeOutgoing LinuxSLLPacketType = 4 // Outgoing of any type
  21. // These ones are invisible by user level
  22. LinuxSLLPacketTypeLoopback LinuxSLLPacketType = 5 // MC/BRD frame looped back
  23. LinuxSLLPacketTypeFastroute LinuxSLLPacketType = 6 // Fastrouted frame
  24. )
  25. func (l LinuxSLLPacketType) String() string {
  26. switch l {
  27. case LinuxSLLPacketTypeHost:
  28. return "host"
  29. case LinuxSLLPacketTypeBroadcast:
  30. return "broadcast"
  31. case LinuxSLLPacketTypeMulticast:
  32. return "multicast"
  33. case LinuxSLLPacketTypeOtherhost:
  34. return "otherhost"
  35. case LinuxSLLPacketTypeOutgoing:
  36. return "outgoing"
  37. case LinuxSLLPacketTypeLoopback:
  38. return "loopback"
  39. case LinuxSLLPacketTypeFastroute:
  40. return "fastroute"
  41. }
  42. return fmt.Sprintf("Unknown(%d)", int(l))
  43. }
  44. type LinuxSLL struct {
  45. BaseLayer
  46. PacketType LinuxSLLPacketType
  47. AddrLen uint16
  48. Addr net.HardwareAddr
  49. EthernetType EthernetType
  50. }
  51. // LayerType returns LayerTypeLinuxSLL.
  52. func (sll *LinuxSLL) LayerType() gopacket.LayerType { return LayerTypeLinuxSLL }
  53. func (sll *LinuxSLL) CanDecode() gopacket.LayerClass {
  54. return LayerTypeLinuxSLL
  55. }
  56. func (sll *LinuxSLL) LinkFlow() gopacket.Flow {
  57. return gopacket.NewFlow(EndpointMAC, sll.Addr, nil)
  58. }
  59. func (sll *LinuxSLL) NextLayerType() gopacket.LayerType {
  60. return sll.EthernetType.LayerType()
  61. }
  62. func (sll *LinuxSLL) DecodeFromBytes(data []byte, df gopacket.DecodeFeedback) error {
  63. if len(data) < 16 {
  64. return errors.New("Linux SLL packet too small")
  65. }
  66. sll.PacketType = LinuxSLLPacketType(binary.BigEndian.Uint16(data[0:2]))
  67. sll.AddrLen = binary.BigEndian.Uint16(data[4:6])
  68. sll.Addr = net.HardwareAddr(data[6 : sll.AddrLen+6])
  69. sll.EthernetType = EthernetType(binary.BigEndian.Uint16(data[14:16]))
  70. sll.BaseLayer = BaseLayer{data[:16], data[16:]}
  71. return nil
  72. }
  73. func decodeLinuxSLL(data []byte, p gopacket.PacketBuilder) error {
  74. sll := &LinuxSLL{}
  75. if err := sll.DecodeFromBytes(data, p); err != nil {
  76. return err
  77. }
  78. p.AddLayer(sll)
  79. p.SetLinkLayer(sll)
  80. return p.NextDecoder(sll.EthernetType)
  81. }