Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Question: Adding a network interface #21

Open
henrikkorsgaard opened this issue Feb 7, 2019 · 2 comments
Open

Question: Adding a network interface #21

henrikkorsgaard opened this issue Feb 7, 2019 · 2 comments

Comments

@henrikkorsgaard
Copy link

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

@mdlayher
Copy link
Owner

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.

@JohannesWasse
Copy link

JohannesWasse commented Mar 11, 2019

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants