Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use raw sockets in Go

Tags:

go

raw-sockets

I'm trying to write a program that receives DHCP discoveries (UDP) and forwards them on to a given IP address using a different source IP address depending on the content of a specific field (GIADDR) in the DHCP packet. I could get working the receiving and sending bit but I'm having an issue with using as IP source address anything that is not a configured IP address on the local machine. I believe that this can only be done using Raw sockets; is that true ? Are there any examples out there on how to do that in Go ? I've spent a couple of days looking around but could not find much.

Cheers, Sal

like image 653
Salvatore Tarallo Avatar asked Aug 25 '13 09:08

Salvatore Tarallo


1 Answers

There are a number of hurdles to jump with what you propose:

Security

In general, being able to set the source IP address for a packet could be a very dangerous thing security wise. Under linux, in order to forge your own raw DHCP packets with custom headers, you will need to run your application as root or from an application with the CAP_NET_RAW capability (see setcap).

Raw Sockets in Go

The standard net library does not provide raw socket capability because it is very specialized and the API may be subject to change as people begin to use it in anger.

The go.net subrepository provides an ipv4 and an ipv6 package, the former of which should suit your needs:

http://godoc.org/code.google.com/p/go.net/ipv4#NewRawConn

Header Spoofing

You will need to use ipv4.RawConn's ReadFrom method to read your source packet. You should then be able to use most of those fields, along with your GIADDR logic, to set up the headers for the WriteTo call. It will probably look something like:

for {
  hdr, payload, _, err := conn.ReadFrom(buf)
  if err != nil { ... }
  hdr.ID = 0
  hdr.Checksum = 0
  hdr.Src = ...
  hdr.Dst = ...
  if err := conn.WriteTo(hdr, payload, nil); err != nil { ... }
}
like image 79
Kyle Lemons Avatar answered Oct 18 '22 19:10

Kyle Lemons