gtp_linux.go 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. package netlink
  2. import (
  3. "fmt"
  4. "net"
  5. "strings"
  6. "syscall"
  7. "github.com/vishvananda/netlink/nl"
  8. )
  9. type PDP struct {
  10. Version uint32
  11. TID uint64
  12. PeerAddress net.IP
  13. MSAddress net.IP
  14. Flow uint16
  15. NetNSFD uint32
  16. ITEI uint32
  17. OTEI uint32
  18. }
  19. func (pdp *PDP) String() string {
  20. elems := []string{}
  21. elems = append(elems, fmt.Sprintf("Version: %d", pdp.Version))
  22. if pdp.Version == 0 {
  23. elems = append(elems, fmt.Sprintf("TID: %d", pdp.TID))
  24. } else if pdp.Version == 1 {
  25. elems = append(elems, fmt.Sprintf("TEI: %d/%d", pdp.ITEI, pdp.OTEI))
  26. }
  27. elems = append(elems, fmt.Sprintf("MS-Address: %s", pdp.MSAddress))
  28. elems = append(elems, fmt.Sprintf("Peer-Address: %s", pdp.PeerAddress))
  29. return fmt.Sprintf("{%s}", strings.Join(elems, " "))
  30. }
  31. func (p *PDP) parseAttributes(attrs []syscall.NetlinkRouteAttr) error {
  32. for _, a := range attrs {
  33. switch a.Attr.Type {
  34. case nl.GENL_GTP_ATTR_VERSION:
  35. p.Version = native.Uint32(a.Value)
  36. case nl.GENL_GTP_ATTR_TID:
  37. p.TID = native.Uint64(a.Value)
  38. case nl.GENL_GTP_ATTR_PEER_ADDRESS:
  39. p.PeerAddress = net.IP(a.Value)
  40. case nl.GENL_GTP_ATTR_MS_ADDRESS:
  41. p.MSAddress = net.IP(a.Value)
  42. case nl.GENL_GTP_ATTR_FLOW:
  43. p.Flow = native.Uint16(a.Value)
  44. case nl.GENL_GTP_ATTR_NET_NS_FD:
  45. p.NetNSFD = native.Uint32(a.Value)
  46. case nl.GENL_GTP_ATTR_I_TEI:
  47. p.ITEI = native.Uint32(a.Value)
  48. case nl.GENL_GTP_ATTR_O_TEI:
  49. p.OTEI = native.Uint32(a.Value)
  50. }
  51. }
  52. return nil
  53. }
  54. func parsePDP(msgs [][]byte) ([]*PDP, error) {
  55. pdps := make([]*PDP, 0, len(msgs))
  56. for _, m := range msgs {
  57. attrs, err := nl.ParseRouteAttr(m[nl.SizeofGenlmsg:])
  58. if err != nil {
  59. return nil, err
  60. }
  61. pdp := &PDP{}
  62. if err := pdp.parseAttributes(attrs); err != nil {
  63. return nil, err
  64. }
  65. pdps = append(pdps, pdp)
  66. }
  67. return pdps, nil
  68. }
  69. func (h *Handle) GTPPDPList() ([]*PDP, error) {
  70. f, err := h.GenlFamilyGet(nl.GENL_GTP_NAME)
  71. if err != nil {
  72. return nil, err
  73. }
  74. msg := &nl.Genlmsg{
  75. Command: nl.GENL_GTP_CMD_GETPDP,
  76. Version: nl.GENL_GTP_VERSION,
  77. }
  78. req := h.newNetlinkRequest(int(f.ID), syscall.NLM_F_DUMP)
  79. req.AddData(msg)
  80. msgs, err := req.Execute(syscall.NETLINK_GENERIC, 0)
  81. if err != nil {
  82. return nil, err
  83. }
  84. return parsePDP(msgs)
  85. }
  86. func GTPPDPList() ([]*PDP, error) {
  87. return pkgHandle.GTPPDPList()
  88. }
  89. func gtpPDPGet(req *nl.NetlinkRequest) (*PDP, error) {
  90. msgs, err := req.Execute(syscall.NETLINK_GENERIC, 0)
  91. if err != nil {
  92. return nil, err
  93. }
  94. pdps, err := parsePDP(msgs)
  95. if err != nil {
  96. return nil, err
  97. }
  98. if len(pdps) != 1 {
  99. return nil, fmt.Errorf("invalid reqponse for GENL_GTP_CMD_GETPDP")
  100. }
  101. return pdps[0], nil
  102. }
  103. func (h *Handle) GTPPDPByTID(link Link, tid int) (*PDP, error) {
  104. f, err := h.GenlFamilyGet(nl.GENL_GTP_NAME)
  105. if err != nil {
  106. return nil, err
  107. }
  108. msg := &nl.Genlmsg{
  109. Command: nl.GENL_GTP_CMD_GETPDP,
  110. Version: nl.GENL_GTP_VERSION,
  111. }
  112. req := h.newNetlinkRequest(int(f.ID), 0)
  113. req.AddData(msg)
  114. req.AddData(nl.NewRtAttr(nl.GENL_GTP_ATTR_VERSION, nl.Uint32Attr(0)))
  115. req.AddData(nl.NewRtAttr(nl.GENL_GTP_ATTR_LINK, nl.Uint32Attr(uint32(link.Attrs().Index))))
  116. req.AddData(nl.NewRtAttr(nl.GENL_GTP_ATTR_TID, nl.Uint64Attr(uint64(tid))))
  117. return gtpPDPGet(req)
  118. }
  119. func GTPPDPByTID(link Link, tid int) (*PDP, error) {
  120. return pkgHandle.GTPPDPByTID(link, tid)
  121. }
  122. func (h *Handle) GTPPDPByITEI(link Link, itei int) (*PDP, error) {
  123. f, err := h.GenlFamilyGet(nl.GENL_GTP_NAME)
  124. if err != nil {
  125. return nil, err
  126. }
  127. msg := &nl.Genlmsg{
  128. Command: nl.GENL_GTP_CMD_GETPDP,
  129. Version: nl.GENL_GTP_VERSION,
  130. }
  131. req := h.newNetlinkRequest(int(f.ID), 0)
  132. req.AddData(msg)
  133. req.AddData(nl.NewRtAttr(nl.GENL_GTP_ATTR_VERSION, nl.Uint32Attr(1)))
  134. req.AddData(nl.NewRtAttr(nl.GENL_GTP_ATTR_LINK, nl.Uint32Attr(uint32(link.Attrs().Index))))
  135. req.AddData(nl.NewRtAttr(nl.GENL_GTP_ATTR_I_TEI, nl.Uint32Attr(uint32(itei))))
  136. return gtpPDPGet(req)
  137. }
  138. func GTPPDPByITEI(link Link, itei int) (*PDP, error) {
  139. return pkgHandle.GTPPDPByITEI(link, itei)
  140. }
  141. func (h *Handle) GTPPDPByMSAddress(link Link, addr net.IP) (*PDP, error) {
  142. f, err := h.GenlFamilyGet(nl.GENL_GTP_NAME)
  143. if err != nil {
  144. return nil, err
  145. }
  146. msg := &nl.Genlmsg{
  147. Command: nl.GENL_GTP_CMD_GETPDP,
  148. Version: nl.GENL_GTP_VERSION,
  149. }
  150. req := h.newNetlinkRequest(int(f.ID), 0)
  151. req.AddData(msg)
  152. req.AddData(nl.NewRtAttr(nl.GENL_GTP_ATTR_VERSION, nl.Uint32Attr(0)))
  153. req.AddData(nl.NewRtAttr(nl.GENL_GTP_ATTR_LINK, nl.Uint32Attr(uint32(link.Attrs().Index))))
  154. req.AddData(nl.NewRtAttr(nl.GENL_GTP_ATTR_MS_ADDRESS, []byte(addr.To4())))
  155. return gtpPDPGet(req)
  156. }
  157. func GTPPDPByMSAddress(link Link, addr net.IP) (*PDP, error) {
  158. return pkgHandle.GTPPDPByMSAddress(link, addr)
  159. }
  160. func (h *Handle) GTPPDPAdd(link Link, pdp *PDP) error {
  161. f, err := h.GenlFamilyGet(nl.GENL_GTP_NAME)
  162. if err != nil {
  163. return err
  164. }
  165. msg := &nl.Genlmsg{
  166. Command: nl.GENL_GTP_CMD_NEWPDP,
  167. Version: nl.GENL_GTP_VERSION,
  168. }
  169. req := h.newNetlinkRequest(int(f.ID), syscall.NLM_F_EXCL|syscall.NLM_F_ACK)
  170. req.AddData(msg)
  171. req.AddData(nl.NewRtAttr(nl.GENL_GTP_ATTR_VERSION, nl.Uint32Attr(pdp.Version)))
  172. req.AddData(nl.NewRtAttr(nl.GENL_GTP_ATTR_LINK, nl.Uint32Attr(uint32(link.Attrs().Index))))
  173. req.AddData(nl.NewRtAttr(nl.GENL_GTP_ATTR_PEER_ADDRESS, []byte(pdp.PeerAddress.To4())))
  174. req.AddData(nl.NewRtAttr(nl.GENL_GTP_ATTR_MS_ADDRESS, []byte(pdp.MSAddress.To4())))
  175. switch pdp.Version {
  176. case 0:
  177. req.AddData(nl.NewRtAttr(nl.GENL_GTP_ATTR_TID, nl.Uint64Attr(pdp.TID)))
  178. req.AddData(nl.NewRtAttr(nl.GENL_GTP_ATTR_FLOW, nl.Uint16Attr(pdp.Flow)))
  179. case 1:
  180. req.AddData(nl.NewRtAttr(nl.GENL_GTP_ATTR_I_TEI, nl.Uint32Attr(pdp.ITEI)))
  181. req.AddData(nl.NewRtAttr(nl.GENL_GTP_ATTR_O_TEI, nl.Uint32Attr(pdp.OTEI)))
  182. default:
  183. return fmt.Errorf("unsupported GTP version: %d", pdp.Version)
  184. }
  185. _, err = req.Execute(syscall.NETLINK_GENERIC, 0)
  186. return err
  187. }
  188. func GTPPDPAdd(link Link, pdp *PDP) error {
  189. return pkgHandle.GTPPDPAdd(link, pdp)
  190. }
  191. func (h *Handle) GTPPDPDel(link Link, pdp *PDP) error {
  192. f, err := h.GenlFamilyGet(nl.GENL_GTP_NAME)
  193. if err != nil {
  194. return err
  195. }
  196. msg := &nl.Genlmsg{
  197. Command: nl.GENL_GTP_CMD_DELPDP,
  198. Version: nl.GENL_GTP_VERSION,
  199. }
  200. req := h.newNetlinkRequest(int(f.ID), syscall.NLM_F_EXCL|syscall.NLM_F_ACK)
  201. req.AddData(msg)
  202. req.AddData(nl.NewRtAttr(nl.GENL_GTP_ATTR_VERSION, nl.Uint32Attr(pdp.Version)))
  203. req.AddData(nl.NewRtAttr(nl.GENL_GTP_ATTR_LINK, nl.Uint32Attr(uint32(link.Attrs().Index))))
  204. switch pdp.Version {
  205. case 0:
  206. req.AddData(nl.NewRtAttr(nl.GENL_GTP_ATTR_TID, nl.Uint64Attr(pdp.TID)))
  207. case 1:
  208. req.AddData(nl.NewRtAttr(nl.GENL_GTP_ATTR_I_TEI, nl.Uint32Attr(pdp.ITEI)))
  209. default:
  210. return fmt.Errorf("unsupported GTP version: %d", pdp.Version)
  211. }
  212. _, err = req.Execute(syscall.NETLINK_GENERIC, 0)
  213. return err
  214. }
  215. func GTPPDPDel(link Link, pdp *PDP) error {
  216. return pkgHandle.GTPPDPDel(link, pdp)
  217. }