wifi icon indicating copy to clipboard operation
wifi copied to clipboard

Question: Adding a network interface

Open henrikkorsgaard opened this issue 6 years ago • 2 comments

Forgive me if this is asking about a trivial thing.

I am trying to add the capability of adding a network interface to a physical device similar to the iw command sudo iw phy phy1 interface add mon type monitor

I've found the neccesary commands (cmdNewInterface) and attributes, but I am having a very hard time constructing a simple example. Would it be possible to point to, show or guide me towards an example?

This is my current code (which outputs "operation not supported" -- while iw works):

func (c *client) CreateNewInterface(PHY int, ifaceType InterfaceType, name string) error {

	var attrs []netlink.Attribute

	attrs = append(attrs, netlink.Attribute{Type: nl80211.AttrWiphy, Data: []byte(strconv.Itoa(PHY))})
	attrs = append(attrs, netlink.Attribute{Type: nl80211.AttrIftype, Data: []byte(strconv.Itoa(int(ifaceType)))})
	attrs = append(attrs, netlink.Attribute{Type: nl80211.AttrIfname, Data: []byte(name)})

	nlattrs, err := netlink.MarshalAttributes(attrs)
	if err != nil {
		return err
	}

	req := genetlink.Message{
		Header: genetlink.Header{
			Command: nl80211.CmdNewInterface,
			Version: c.familyVersion,
		},
		Data: nlattrs,
	}

	flags := netlink.HeaderFlagsRequest | netlink.HeaderFlagsDump | netlink.HeaderFlagsCreate
	msgs, err := c.c.Execute(req, c.familyID, flags)
	if err != nil {
		fmt.Println("this outputs: ***operation not supported***")
		return err
	}

	if err := c.checkMessages(msgs, nl80211.CmdNewInterface); err != nil {
		return err
	}

	return nil
}

Any help or guidance is highly appreciated -- apologies for potential misunderstandings Best, Henrik

henrikkorsgaard avatar Feb 07 '19 12:02 henrikkorsgaard

Hi there, apologies for the delay. I've had most success by using strace on commands like iw, checking the applicable kernel side code, order r setting up an nlmon interface and using Wireshark to inspect the packets.

I apologize, there is no simple solution really. A lot of the stuff here I was able to work out with some trial-and-error.

mdlayher avatar Feb 10 '19 14:02 mdlayher

Hi I had the same problem. If you use the right encoding for the Attributes the code works. It helps to use iw with -- debug option.

func (c *client) CreateNewInterface(PHY int, ifaceType InterfaceType, name string) error {

	var attrs []netlink.Attribute

	attrs = append(attrs, netlink.Attribute{Type: nl80211.AttrWiphy, Data: nlenc.Uint32Bytes(uint32(PHY))})
	attrs = append(attrs, netlink.Attribute{Type: nl80211.AttrIfname, Data: nlenc.Bytes(name)})
	attrs = append(attrs, netlink.Attribute{Type: nl80211.AttrIftype, Data: nlenc.Uint32Bytes(uint32(ifaceType))})

	nlattrs, err := netlink.MarshalAttributes(attrs)
	fmt.Println("Debug", hex.EncodeToString(nlattrs))
	if err != nil {
		return err
	}

	req := genetlink.Message{
		Header: genetlink.Header{
			Command: nl80211.CmdNewInterface,
			Version: c.familyVersion,
		},
		Data: nlattrs,
	}

	flags := netlink.Request | netlink.Acknowledge
	msgs, err := c.c.Execute(req, c.familyID, flags)

	if err != nil {
		return err
	}

	if err := c.checkMessages(msgs, nl80211.CmdNewInterface); err != nil {
		return err
	}

	return nil
}

Hope it helps

JohannesWasse avatar Mar 11 '19 15:03 JohannesWasse