static int tun_set_iff(struct file *file, struct ifreq *ifr)
{
    struct tun_struct *tun;
    struct net_device *dev;
    int err;

    dev = __dev_get_by_name(ifr->ifr_name);
    if (dev) {
        /* Device exist */
        tun = dev->priv;

        if (dev->init != tun_net_init || tun->attached)
            return -EBUSY;

        /* Check permissions */
        if (tun->owner != -1)
            if (current->euid != tun->owner && !capable(CAP_NET_ADMIN))
                return -EPERM;
    } else {
        char *name;

        /* Allocate new device */
        if (!(tun = kmalloc(sizeof(struct tun_struct), GFP_KERNEL)) )
            return -ENOMEM;
        memset(tun, 0, sizeof(struct tun_struct));

        skb_queue_head_init(&tun->readq);
        init_waitqueue_head(&tun->read_wait);

        tun->owner = -1;
        tun->dev.init = tun_net_init;
        tun->dev.priv = tun;

        err = -EINVAL;

        /* Set dev type */
        if (ifr->ifr_flags & IFF_TUN) {
            /* TUN device */
            tun->flags |= TUN_TUN_DEV;
            name = "tun%d";
        } else if (ifr->ifr_flags & IFF_TAP) {
            /* TAP device */
            tun->flags |= TUN_TAP_DEV;
            name = "tap%d";
        } else
            goto failed;

        if (*ifr->ifr_name)
            name = ifr->ifr_name;

        if ((err = dev_alloc_name(&tun->dev, name)) < 0)
            goto failed;
        if ((err = register_netdevice(&tun->dev)))
            goto failed;

        MOD_INC_USE_COUNT;

        tun->name = tun->dev.name;
    }

    DBG(KERN_INFO "%s: tun_set_iff\n", tun->name);

    if (ifr->ifr_flags & IFF_NO_PI)
        tun->flags |= TUN_NO_PI;

    if (ifr->ifr_flags & IFF_ONE_QUEUE)
        tun->flags |= TUN_ONE_QUEUE;

    file->private_data = tun;
    tun->attached = 1;

    strcpy(ifr->ifr_name, tun->name);
    return 0;

failed:
    kfree(tun);
    return err;
}
Exemplo n.º 2
0
static int veth_newlink(struct net *src_net, struct net_device *dev,
			 struct nlattr *tb[], struct nlattr *data[])
{
	int err;
	struct net_device *peer;
	struct veth_priv *priv;
	char ifname[IFNAMSIZ];
	struct nlattr *peer_tb[IFLA_MAX + 1], **tbp;
	struct ifinfomsg *ifmp;
	struct net *net;

	/*
	 * create and register peer first
	 */
	if (data != NULL && data[VETH_INFO_PEER] != NULL) {
		struct nlattr *nla_peer;

		nla_peer = data[VETH_INFO_PEER];
		ifmp = nla_data(nla_peer);
		err = nla_parse(peer_tb, IFLA_MAX,
				nla_data(nla_peer) + sizeof(struct ifinfomsg),
				nla_len(nla_peer) - sizeof(struct ifinfomsg),
				ifla_policy);
		if (err < 0)
			return err;

		err = veth_validate(peer_tb, NULL);
		if (err < 0)
			return err;

		tbp = peer_tb;
	} else {
		ifmp = NULL;
		tbp = tb;
	}

	if (tbp[IFLA_IFNAME])
		nla_strlcpy(ifname, tbp[IFLA_IFNAME], IFNAMSIZ);
	else
		snprintf(ifname, IFNAMSIZ, DRV_NAME "%%d");

	net = rtnl_link_get_net(src_net, tbp);
	if (IS_ERR(net))
		return PTR_ERR(net);

	peer = rtnl_create_link(src_net, net, ifname, &veth_link_ops, tbp);
	if (IS_ERR(peer)) {
		put_net(net);
		return PTR_ERR(peer);
	}

	if (tbp[IFLA_ADDRESS] == NULL)
		random_ether_addr(peer->dev_addr);

	err = register_netdevice(peer);
	put_net(net);
	net = NULL;
	if (err < 0)
		goto err_register_peer;

	netif_carrier_off(peer);

	err = rtnl_configure_link(peer, ifmp);
	if (err < 0)
		goto err_configure_peer;

	/*
	 * register dev last
	 *
	 * note, that since we've registered new device the dev's name
	 * should be re-allocated
	 */

	if (tb[IFLA_ADDRESS] == NULL)
		random_ether_addr(dev->dev_addr);

	if (tb[IFLA_IFNAME])
		nla_strlcpy(dev->name, tb[IFLA_IFNAME], IFNAMSIZ);
	else
		snprintf(dev->name, IFNAMSIZ, DRV_NAME "%%d");

	if (strchr(dev->name, '%')) {
		err = dev_alloc_name(dev, dev->name);
		if (err < 0)
			goto err_alloc_name;
	}

	err = register_netdevice(dev);
	if (err < 0)
		goto err_register_dev;

	netif_carrier_off(dev);

	/*
	 * tie the deviced together
	 */

	priv = netdev_priv(dev);
	priv->peer = peer;

	priv = netdev_priv(peer);
	priv->peer = dev;
	return 0;

err_register_dev:
	/* nothing to do */
err_alloc_name:
err_configure_peer:
	unregister_netdevice(peer);
	return err;

err_register_peer:
	free_netdev(peer);
	return err;
}
Exemplo n.º 3
0
static int __devinit ieee802154fake_probe(struct platform_device *pdev)
{
	struct net_device *dev;
	struct fakehard_priv *priv;
	struct wpan_phy *phy = wpan_phy_alloc(0);
	int err;

	if (!phy)
		return -ENOMEM;

	dev = alloc_netdev(sizeof(struct fakehard_priv), "hardwpan%d", ieee802154_fake_setup);
	if (!dev) {
		wpan_phy_free(phy);
		return -ENOMEM;
	}

	phy->dev.platform_data = dev;

	memcpy(dev->dev_addr, "\xba\xbe\xca\xfe\xde\xad\xbe\xef",
			dev->addr_len);
	memcpy(dev->perm_addr, dev->dev_addr, dev->addr_len);

	/*
	 * For now we'd like to emulate 2.4 GHz-only device,
	 * both O-QPSK and CSS
	 */
	/* 2.4 GHz O-QPSK 802.15.4-2003 */
	phy->channels_supported[0] |= 0x7FFF800;
	/* 2.4 GHz CSS 802.15.4a-2007 */
	phy->channels_supported[3] |= 0x3fff;

	phy->transmit_power = 0xbf;

	dev->netdev_ops = &fake_ops;
	dev->ml_priv = &fake_mlme;

	priv = netdev_priv(dev);
	priv->phy = phy;

	/*
	 * If the name is a format string the caller wants us to do a
	 * name allocation.
	 */
	if (strchr(dev->name, '%')) {
		err = dev_alloc_name(dev, dev->name);
		if (err < 0)
			goto out;
	}

	wpan_phy_set_dev(phy, &pdev->dev);
	SET_NETDEV_DEV(dev, &phy->dev);

	platform_set_drvdata(pdev, dev);

	err = wpan_phy_register(phy);
	if (err)
		goto out;

	err = register_netdev(dev);
	if (err < 0)
		goto out;

	dev_info(&pdev->dev, "Added ieee802154 HardMAC hardware\n");
	return 0;

out:
	unregister_netdev(dev);
	return err;
}
Exemplo n.º 4
0
int hostapd_mode_init(_adapter *padapter)
{
	unsigned char mac[ETH_ALEN];
	struct hostapd_priv *phostapdpriv;
	struct net_device *pnetdev;
	
	pnetdev = rtw_alloc_etherdev(sizeof(struct hostapd_priv));	
	if (!pnetdev)
	   return -ENOMEM;

	//SET_MODULE_OWNER(pnetdev);
       ether_setup(pnetdev);

	//pnetdev->type = ARPHRD_IEEE80211;
	
	phostapdpriv = rtw_netdev_priv(pnetdev);
	phostapdpriv->pmgnt_netdev = pnetdev;
	phostapdpriv->padapter= padapter;
	padapter->phostapdpriv = phostapdpriv;
	
	//pnetdev->init = NULL;
	
#if (LINUX_VERSION_CODE>=KERNEL_VERSION(2,6,29))

	DBG_871X("register rtl871x_mgnt_netdev_ops to netdev_ops\n");

	pnetdev->netdev_ops = &rtl871x_mgnt_netdev_ops;
	
#else

	pnetdev->open = mgnt_netdev_open;

	pnetdev->stop = mgnt_netdev_close;	
	
	pnetdev->hard_start_xmit = mgnt_xmit_entry;
	
	//pnetdev->set_mac_address = r871x_net_set_mac_address;
	
	//pnetdev->get_stats = r871x_net_get_stats;

	//pnetdev->do_ioctl = r871x_mp_ioctl;
	
#endif

	pnetdev->watchdog_timeo = HZ; /* 1 second timeout */	

	//pnetdev->wireless_handlers = NULL;

#ifdef CONFIG_TCP_CSUM_OFFLOAD_TX
	pnetdev->features |= NETIF_F_IP_CSUM;
#endif	

	
	
	if(dev_alloc_name(pnetdev,"mgnt.wlan%d") < 0)
	{
		DBG_871X("hostapd_mode_init(): dev_alloc_name, fail! \n");		
	}


	//SET_NETDEV_DEV(pnetdev, pintfpriv->udev);


	mac[0]=0x00;
	mac[1]=0xe0;
	mac[2]=0x4c;
	mac[3]=0x87;
	mac[4]=0x11;
	mac[5]=0x12;
				
	_rtw_memcpy(pnetdev->dev_addr, mac, ETH_ALEN);
	

	netif_carrier_off(pnetdev);


	/* Tell the network stack we exist */
	if (register_netdev(pnetdev) != 0)
	{
		DBG_871X("hostapd_mode_init(): register_netdev fail!\n");
		
		if(pnetdev)
      		{	 
			rtw_free_netdev(pnetdev);
      		}
	}
	
	return 0;
	
}
Exemplo n.º 5
0
static int hdlc_fr_pvc(hdlc_device *hdlc, int dlci)
{
	pvc_device **pvc_p = &hdlc->first_pvc;
	pvc_device *pvc;
	int result, create = 1;	/* Create or delete PVC */

	if(!capable(CAP_NET_ADMIN))
		return -EPERM;

	if(dlci<0) {
		dlci = -dlci;
		create = 0;
	}

	if(dlci <= 0 || dlci >= 1024)
		return -EINVAL;	/* Only 10 bits for DLCI, DLCI=0 is reserved */

	if(!mode_is(hdlc, MODE_FR))
		return -EINVAL;	/* Only meaningfull on FR */

	while(*pvc_p) {
		if (netdev_dlci(&(*pvc_p)->netdev) == dlci)
			break;
		pvc_p = &(*pvc_p)->next;
	}

	if (create) {		/* Create PVC */
		if (*pvc_p != NULL)
			return -EEXIST;

		pvc = *pvc_p = kmalloc(sizeof(pvc_device), GFP_KERNEL);
		if (!pvc) {
			printk(KERN_WARNING "%s: Memory squeeze on "
			       "hdlc_fr_pvc()\n", hdlc_to_name(hdlc));
			return -ENOBUFS;
		}
		memset(pvc, 0, sizeof(pvc_device));

		pvc->netdev.hard_start_xmit = pvc_xmit;
		pvc->netdev.get_stats = pvc_get_stats;
		pvc->netdev.open = pvc_open;
		pvc->netdev.stop = pvc_close;
		pvc->netdev.change_mtu = pvc_change_mtu;
		pvc->netdev.mtu = HDLC_MAX_MTU;

		pvc->netdev.type = ARPHRD_DLCI;
		pvc->netdev.hard_header_len = 16;
		pvc->netdev.hard_header = fr_hard_header;
		pvc->netdev.tx_queue_len = 0;
		pvc->netdev.flags = IFF_POINTOPOINT;

		pvc->master = hdlc;
		*(u16*)pvc->netdev.dev_addr = htons(dlci);
		dlci_to_q922(pvc->netdev.broadcast, dlci);
		pvc->netdev.addr_len = 2;
		pvc->netdev.irq = hdlc_to_dev(hdlc)->irq;

		result = dev_alloc_name(&pvc->netdev, "pvc%d");
		if (result < 0) {
			kfree(pvc);
			*pvc_p = NULL;
			return result;
		}

		if (register_netdevice(&pvc->netdev) != 0) {
			kfree(pvc);
			*pvc_p = NULL;
			return -EIO;
		}

		if (!mode_is(hdlc, MODE_SOFT) && hdlc->create_pvc) {
			result = hdlc->create_pvc(pvc);
			if (result) {
				unregister_netdevice(&pvc->netdev);
				kfree(pvc);
				*pvc_p = NULL;
				return result;
			}
		}

		hdlc->lmi.state |= LINK_STATE_CHANGED;
		hdlc->pvc_count++;
		return 0;
	}

	if (*pvc_p == NULL)		/* Delete PVC */
		return -ENOENT;

	pvc = *pvc_p;

	if (pvc->netdev.flags & IFF_UP)
		return -EBUSY;		/* PVC in use */

	if (!mode_is(hdlc, MODE_SOFT) && hdlc->destroy_pvc)
		hdlc->destroy_pvc(pvc);

	hdlc->lmi.state |= LINK_STATE_CHANGED;
	hdlc->pvc_count--;
	*pvc_p = pvc->next;
	unregister_netdevice(&pvc->netdev);
	kfree(pvc);
	return 0;
}
Exemplo n.º 6
0
/**
*  @brief 	WILC_WFI_init_mon_interface
*  @details 	
*  @param[in]   
*  @return 	int : Return 0 on Success
*  @author	mdaftedar
*  @date	12 JUL 2012	
*  @version	1.0
*/
struct net_device * WILC_WFI_init_mon_interface(char *name , struct net_device *real_dev )
	{
	

		WILC_Uint32 ret = WILC_SUCCESS;
		struct WILC_WFI_mon_priv *priv ;

		/*If monitor interface is already initialized, return it*/
		if (wilc_wfi_mon) 
		{
			return wilc_wfi_mon;
		}
#if 0	
		wilc_wfi_mon = alloc_netdev(sizeof(struct WILC_WFI_mon_priv), name, WILC_WFI_mon_setup);
		if (wilc_wfi_mon == NULL)
		{
			PRINT_ER("Failed to allocate netdevice\n");
			goto failed;
		}
	
		//rtnl_lock();
		PRINT_INFO(HOSTAPD_DBG,"Monitor interface name %s\n", wilc_wfi_mon->name);
		
		
		ret = dev_alloc_name(wilc_wfi_mon, wilc_wfi_mon->name);
		if (ret < 0)
					goto failed_mon;
		
		
		priv = netdev_priv(wilc_wfi_mon);
		if(priv== NULL)
		{
		  PRINT_ER("private structure is NULL\n");
		  return WILC_FAIL;
		}	
	
		priv->real_ndev = real_dev;
	
	
		ret = register_netdevice(wilc_wfi_mon);
	
		
		if (ret < 0)
		{
			 PRINT_ER("Failed to register netdevice\n");
			 goto failed_mon;
		}
		
		
		return WILC_SUCCESS;
		   // rtnl_unlock();
	
	failed:
		return ret;
	
	failed_mon:
		   //rtnl_unlock();
		   free_netdev(wilc_wfi_mon);
		   return ret;
#endif

		wilc_wfi_mon = alloc_etherdev(sizeof(struct WILC_WFI_mon_priv));
		if (!wilc_wfi_mon) {
			PRINT_ER("failed to allocate memory\n");
			return NULL;
		
		}
	
		wilc_wfi_mon->type = ARPHRD_IEEE80211_RADIOTAP;
		strncpy(wilc_wfi_mon->name, name, IFNAMSIZ);
		wilc_wfi_mon->name[IFNAMSIZ - 1] = 0;
		wilc_wfi_mon->netdev_ops = &wilc_wfi_netdev_ops;
	
		ret = register_netdevice(wilc_wfi_mon);
		if (ret) {
			PRINT_ER(" register_netdevice failed (%d)\n", ret);
			return NULL;
		}
		priv = netdev_priv(wilc_wfi_mon);
		if(priv== NULL)
		{
		  PRINT_ER("private structure is NULL\n");
		  return NULL;
		}	
	
		priv->real_ndev = real_dev;
	
		return wilc_wfi_mon;
}
Exemplo n.º 7
0
/**
 * @brief This function adds a new interface. It will
 *        allocate, initialize and register the device.
 *
 *  @param handle    A pointer to moal_handle structure
 *  @param bss_index BSS index number
 *  @param bss_type  BSS type
 *
 *  @return          A pointer to the new priv structure
 */
moal_private *
woal_alloc_virt_interface(moal_handle *handle, t_u8 bss_index, t_u8 bss_type,
#if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 7, 0)
			  const
#endif
			  char *name)
{
	struct net_device *dev = NULL;
	moal_private *priv = NULL;
	ENTER();

#if LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 29)
#ifndef MAX_WMM_QUEUE
#define MAX_WMM_QUEUE   4
#endif
	/* Allocate an Ethernet device */
	dev = alloc_netdev_mq(sizeof(moal_private), name, woal_virt_if_setup,
			      MAX_WMM_QUEUE);
#else
	dev = alloc_netdev(sizeof(moal_private), name, woal_virt_if_setup);
#endif
	if (!dev) {
		PRINTM(MFATAL, "Init virtual ethernet device failed\n");
		goto error;
	}
	/* Allocate device name */
	if ((dev_alloc_name(dev, name) < 0)) {
		PRINTM(MERROR, "Could not allocate device name\n");
		goto error;
	}

	priv = (moal_private *)netdev_priv(dev);
	/* Save the priv to handle */
	handle->priv[bss_index] = priv;

	/* Use the same handle structure */
	priv->phandle = handle;
	priv->netdev = dev;
	priv->bss_index = bss_index;
	priv->bss_type = bss_type;
	priv->bss_role = MLAN_BSS_ROLE_STA;

	INIT_LIST_HEAD(&priv->tcp_sess_queue);
	spin_lock_init(&priv->tcp_sess_lock);

	spin_lock_init(&priv->tx_stat_lock);
	spin_lock_init(&priv->scan_req_lock);
	spin_lock_init(&priv->connect_lock);

#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 24)
	SET_MODULE_OWNER(dev);
#endif

	PRINTM(MCMND, "Alloc virtual interface%s\n", dev->name);

	LEAVE();
	return priv;
error:
	if (dev)
		free_netdev(dev);
	LEAVE();
	return NULL;
}
static int __init init_mac80211_hwsim(void)
{
	int i, err = 0;
	u8 addr[ETH_ALEN];
	struct mac80211_hwsim_data *data;
	struct ieee80211_hw *hw;
	DECLARE_MAC_BUF(mac);

	if (radios < 1 || radios > 65535)
		return -EINVAL;

	hwsim_radio_count = radios;
	hwsim_radios = kcalloc(hwsim_radio_count,
			       sizeof(struct ieee80211_hw *), GFP_KERNEL);
	if (hwsim_radios == NULL)
		return -ENOMEM;

	hwsim_class = class_create(THIS_MODULE, "mac80211_hwsim");
	if (IS_ERR(hwsim_class)) {
		kfree(hwsim_radios);
		return PTR_ERR(hwsim_class);
	}

	memset(addr, 0, ETH_ALEN);
	addr[0] = 0x02;

	for (i = 0; i < hwsim_radio_count; i++) {
		printk(KERN_DEBUG "mac80211_hwsim: Initializing radio %d\n",
		       i);
		hw = ieee80211_alloc_hw(sizeof(*data), &mac80211_hwsim_ops);
		if (hw == NULL) {
			printk(KERN_DEBUG "mac80211_hwsim: ieee80211_alloc_hw "
			       "failed\n");
			err = -ENOMEM;
			goto failed;
		}
		hwsim_radios[i] = hw;

		data = hw->priv;
		data->dev = device_create_drvdata(hwsim_class, NULL, 0, hw,
						"hwsim%d", i);
		if (IS_ERR(data->dev)) {
			printk(KERN_DEBUG
			       "mac80211_hwsim: device_create_drvdata "
			       "failed (%ld)\n", PTR_ERR(data->dev));
			err = -ENOMEM;
			goto failed_drvdata;
		}
		data->dev->driver = &mac80211_hwsim_driver;

		SET_IEEE80211_DEV(hw, data->dev);
		addr[3] = i >> 8;
		addr[4] = i;
		SET_IEEE80211_PERM_ADDR(hw, addr);

		hw->channel_change_time = 1;
		hw->queues = 1;

		memcpy(data->channels, hwsim_channels, sizeof(hwsim_channels));
		memcpy(data->rates, hwsim_rates, sizeof(hwsim_rates));
		data->band.channels = data->channels;
		data->band.n_channels = ARRAY_SIZE(hwsim_channels);
		data->band.bitrates = data->rates;
		data->band.n_bitrates = ARRAY_SIZE(hwsim_rates);
		hw->wiphy->bands[IEEE80211_BAND_2GHZ] = &data->band;

		err = ieee80211_register_hw(hw);
		if (err < 0) {
			printk(KERN_DEBUG "mac80211_hwsim: "
			       "ieee80211_register_hw failed (%d)\n", err);
			goto failed_hw;
		}

		printk(KERN_DEBUG "%s: hwaddr %s registered\n",
		       wiphy_name(hw->wiphy),
		       print_mac(mac, hw->wiphy->perm_addr));

		setup_timer(&data->beacon_timer, mac80211_hwsim_beacon,
			    (unsigned long) hw);
	}

	hwsim_mon = alloc_netdev(0, "hwsim%d", hwsim_mon_setup);
	if (hwsim_mon == NULL)
		goto failed;

	rtnl_lock();

	err = dev_alloc_name(hwsim_mon, hwsim_mon->name);
	if (err < 0)
		goto failed_mon;


	err = register_netdevice(hwsim_mon);
	if (err < 0)
		goto failed_mon;

	rtnl_unlock();

	return 0;

failed_mon:
	rtnl_unlock();
	free_netdev(hwsim_mon);
	mac80211_hwsim_free();
	return err;

failed_hw:
	device_unregister(data->dev);
failed_drvdata:
	ieee80211_free_hw(hw);
	hwsim_radios[i] = NULL;
failed:
	mac80211_hwsim_free();
	return err;
}