linux_sll.go 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. AddrType uint16
  51. }
  52. // LayerType returns LayerTypeLinuxSLL.
  53. func (sll *LinuxSLL) LayerType() gopacket.LayerType { return LayerTypeLinuxSLL }
  54. func (sll *LinuxSLL) CanDecode() gopacket.LayerClass {
  55. return LayerTypeLinuxSLL
  56. }
  57. func (sll *LinuxSLL) LinkFlow() gopacket.Flow {
  58. return gopacket.NewFlow(EndpointMAC, sll.Addr, nil)
  59. }
  60. func (sll *LinuxSLL) NextLayerType() gopacket.LayerType {
  61. return sll.EthernetType.LayerType()
  62. }
  63. func (sll *LinuxSLL) DecodeFromBytes(data []byte, df gopacket.DecodeFeedback) error {
  64. if len(data) < 16 {
  65. return errors.New("Linux SLL packet too small")
  66. }
  67. sll.PacketType = LinuxSLLPacketType(binary.BigEndian.Uint16(data[0:2]))
  68. sll.AddrType = binary.BigEndian.Uint16(data[2:4])
  69. sll.AddrLen = binary.BigEndian.Uint16(data[4:6])
  70. sll.Addr = net.HardwareAddr(data[6 : sll.AddrLen+6])
  71. sll.EthernetType = EthernetType(binary.BigEndian.Uint16(data[14:16]))
  72. sll.BaseLayer = BaseLayer{data[:16], data[16:]}
  73. return nil
  74. }
  75. func decodeLinuxSLL(data []byte, p gopacket.PacketBuilder) error {
  76. sll := &LinuxSLL{}
  77. if err := sll.DecodeFromBytes(data, p); err != nil {
  78. return err
  79. }
  80. p.AddLayer(sll)
  81. p.SetLinkLayer(sll)
  82. return p.NextDecoder(sll.EthernetType)
  83. }