Пример #1
0
static int af_packet_update_properties ( struct net_device *netdev )
{
	struct af_packet_nic *nic = netdev->priv;
	struct ifreq if_data;
	int ret;

	/* retrieve default MAC address */
	int fd = linux_socket(LINUX_AF_PACKET, LINUX_SOCK_RAW, 0);
	if (fd < 0) {
		DBGC(nic, "af_packet %p cannot create raw socket (%s)\n",
		     nic, linux_strerror(linux_errno));
		return fd;
	}

	/* retrieve host's MAC address */
	memset(&if_data, 0, sizeof(if_data));
	strncpy(if_data.ifr_name, nic->ifname, sizeof(if_data.ifr_name));
	ret = linux_ioctl(fd, LINUX_SIOCGIFHWADDR, &if_data);
	if (ret < 0) {
		DBGC(nic, "af_packet %p cannot get mac addr (%s)\n",
		     nic, linux_strerror(linux_errno));
		linux_close(fd);
		return ret;
	}

	linux_close(fd);
	/* struct sockaddr = { u16 family, u8 pad[14] (equiv. sa_data) }; */
	memcpy(netdev->ll_addr, if_data.ifr_hwaddr.pad, ETH_ALEN);
	return 0;
}
Пример #2
0
/** Open the linux interface */
static int af_packet_nic_open ( struct net_device * netdev )
{
	struct af_packet_nic * nic = netdev->priv;
	struct sockaddr_ll socket_address;
	struct ifreq if_data;
	int ret;

	nic->fd = linux_socket(LINUX_AF_PACKET, LINUX_SOCK_RAW,
			       htons(ETH_P_ALL));
	if (nic->fd < 0) {
		DBGC(nic, "af_packet %p socket(AF_PACKET) = %d (%s)\n",
		     nic, nic->fd, linux_strerror(linux_errno));
		return nic->fd;
	}

	/* resolve ifindex of ifname */
	memset(&if_data, 0, sizeof(if_data));
	strncpy(if_data.ifr_name, nic->ifname, sizeof(if_data.ifr_name));
	ret = linux_ioctl(nic->fd, LINUX_SIOCGIFINDEX, &if_data);
	if (ret < 0) {
		DBGC(nic, "af_packet %p ioctl(SIOCGIFINDEX) = %d (%s)\n",
		     nic, ret, linux_strerror(linux_errno));
		linux_close(nic->fd);
		return ret;
	}

	nic->ifindex = if_data.ifr_ifindex;

	/* bind to interface */
	memset(&socket_address, 0, sizeof(socket_address));
	socket_address.sll_family = LINUX_AF_PACKET;
	socket_address.sll_ifindex = nic->ifindex;
	socket_address.sll_protocol = htons(ETH_P_ALL);
	ret = linux_bind(nic->fd, (void *) &socket_address,
			 sizeof(socket_address));
	if (ret == -1) {
		DBGC(nic, "af_packet %p bind() = %d (%s)\n",
		     nic, ret, linux_strerror(linux_errno));
		linux_close(nic->fd);
		return ret;
	}

	/* Set nonblocking mode to make af_packet_nic_poll() easier */
	ret = linux_fcntl(nic->fd, F_SETFL, O_NONBLOCK);
	if (ret != 0) {
		DBGC(nic, "af_packet %p fcntl(%d, ...) = %d (%s)\n",
		     nic, nic->fd, ret, linux_strerror(linux_errno));
		linux_close(nic->fd);
		return ret;
	}

	return 0;
}
Пример #3
0
/**
 * Open PCI configuration space
 *
 * @v pci		PCI device
 * @v flags		Access mode flags
 * @v where		Address within configuration space
 * @ret fd		File handle, or negative error
 */
static int linux_pci_open ( struct pci_device *pci, int flags,
			    unsigned long where ) {
	char filename[ 22 /* "/proc/bus/pci/xx/xx.x" + NUL */ ];
	int fd;
	int rc;

	/* Construct filename */
	snprintf ( filename, sizeof ( filename ), "/proc/bus/pci/%02x/%02x.%x",
		   PCI_BUS ( pci->busdevfn ), PCI_SLOT ( pci->busdevfn ),
		   PCI_FUNC ( pci->busdevfn ) );

	/* Open file */
	fd = linux_open ( filename, flags );
	if ( fd < 0 ) {
		DBGC ( pci, "PCI could not open %s: %s\n", filename,
		       linux_strerror ( linux_errno ) );
		rc = -ELINUX ( linux_errno );
		goto err_open;
	}

	/* Seek to location */
	if ( linux_lseek ( fd, where, SEEK_SET ) < 0 ) {
		DBGC ( pci, "PCI could not seek to %s offset %#02lx: %s\n",
		       filename, where, linux_strerror ( linux_errno ) );
		rc = -ELINUX ( linux_errno );
		goto err_seek;
	}

	return fd;

 err_seek:
	linux_close ( fd );
 err_open:
	return rc;
}
Пример #4
0
static int linux_probe(struct bladerf_devinfo_list *info_list)
{
    int status = 0;
    struct dirent **matches;
    int num_matches, i;
    struct bladerf *dev;
    struct bladerf_devinfo devinfo;

    num_matches = scandir(BLADERF_DEV_DIR, &matches, device_filter, alphasort);
    if (num_matches > 0) {

        for (i = 0; i < num_matches; i++) {
            status = 0;

            /* Open this specific instance. */
            bladerf_init_devinfo(&devinfo);
            devinfo.instance = str2instance(matches[i]->d_name);

            status = linux_open(&dev, &devinfo);

            if (status < 0) {
                log_error("Failed to open instance=%d\n", devinfo.instance);
            } else {
                /* Since this device was opened by instance, it will have
                 * had it's device info (dev->ident) filled out already */
                bladerf_devinfo_list_add(info_list, &dev->ident);
                linux_close(dev);
            }
        }
    }

    free_dirents(matches, num_matches);

    return (!status &&  num_matches > 0) ? status : BLADERF_ERR_NODEV;
}
Пример #5
0
void
unmap(char *progname)
{
	char buf[1024], *p;
	char rbuf[2048];
	int cc, fd;

	fd = linux_open("/proc/self/maps", 0, 0);

	p = &buf[0];

	while (0 < (cc = linux_read(fd, rbuf, sizeof(rbuf))))
	{
		int i;

		for (i = 0; i < cc; ++i)
		{
			int c = rbuf[i];

			if ('\n' != c)
				*p++ = c;
			else {
				*p = '\0';
				/* When a line from /proc/self/maps shows up as having been
				 * mapped in from this running program, ld.so or libc, unmap it.
				 * This will keep the exec'd program's address space a lot
				 * cleaner.  But even a 32-bit address space can hold 2 copies
				 * of glibc without ill effects, so you don't really have to
				 * munmap() anything other than the program calling ul_exec() */
				if (strstr(buf, progname) || strstr(buf, "libdl") || strstr(buf, "/usr/lib/ld-")
					|| strstr(buf, "/lib64/ld-") || strstr(buf, "libc"))
				{
					char *u;
					char *first, *second;
					unsigned long low, high;

					u = strchr(buf, ' ');
					*u = '\0';

					first = buf;

					second = strchr(first, '-');
					*second = '\0';
					++second;

					low = strtoul(first, NULL, 0x10);
					high = strtoul(second, NULL, 0x10);

					linux_munmap((void *)low, high-low);
				}

				p = &buf[0];
			}
		}
	}

	linux_close(fd);
}
Пример #6
0
void
print_maps(void)
{
	char rbuf[1024];
	int fd, cc;

	fd = linux_open("/proc/self/maps", 0, 0);
	while (0 < (cc = linux_read(fd, rbuf, sizeof(rbuf))))
		linux_write(1, rbuf, cc);
	linux_close(fd);
}
Пример #7
0
/**
 * Read from PCI configuration space
 *
 * @v pci		PCI device
 * @v where		Address within configuration space
 * @v value		Data buffer
 * @v len		Length to read
 * @ret rc		Return status code
 */
int linux_pci_read ( struct pci_device *pci, unsigned long where,
		     unsigned long *value, size_t len ) {
	uint32_t tmp = 0;
	int fd;
	int check_len;
	int rc;

	/* Return "missing device" in case of error */
	*value = -1UL;

	/* Open configuration space */
	fd = linux_pci_open ( pci, O_RDONLY, where );
	if ( fd < 0 ) {
		rc = fd;
		goto err_open;
	}

	/* Read value */
	check_len = linux_read ( fd, &tmp, len );
	if ( check_len < 0 ) {
		DBGC ( pci, "PCI could not read from " PCI_FMT " %#02lx+%#zx: "
		       "%s\n", PCI_ARGS ( pci ), where, len,
		       linux_strerror ( linux_errno ) );
		rc = -ELINUX ( linux_errno );
		goto err_read;
	}
	if ( ( size_t ) check_len != len ) {
		DBGC ( pci, "PCI read only %#x bytes from " PCI_FMT
		       " %#02lx+%#zx\n", check_len, PCI_ARGS ( pci ),
		       where, len );
		rc = -EIO;
		goto err_read;
	}

	/* Return value */
	*value = le32_to_cpu ( tmp );

	/* Success */
	rc = 0;

 err_read:
	linux_close ( fd );
 err_open:
	return rc;
}
Пример #8
0
/**
 * Write to PCI configuration space
 *
 * @v pci		PCI device
 * @v where		Address within configuration space
 * @v value		Value to write
 * @v len		Length of value
 * @ret rc		Return status code
 */
int linux_pci_write ( struct pci_device *pci, unsigned long where,
		      unsigned long value, size_t len ) {
	uint32_t tmp;
	int fd;
	int check_len;
	int rc;

	/* Open configuration space */
	fd = linux_pci_open ( pci, O_WRONLY, where );
	if ( fd < 0 ) {
		rc = fd;
		goto err_open;
	}

	/* Prepare value for writing */
	tmp = cpu_to_le32 ( value );
	assert ( len <= sizeof ( tmp ) );

	/* Write value */
	check_len = linux_write ( fd, &tmp, len );
	if ( check_len < 0 ) {
		DBGC ( pci, "PCI could not write to " PCI_FMT " %#02lx+%#zx: "
		       "%s\n", PCI_ARGS ( pci ), where, len,
		       linux_strerror ( linux_errno ) );
		rc = -ELINUX ( linux_errno );
		goto err_write;
	}
	if ( ( size_t ) check_len != len ) {
		DBGC ( pci, "PCI wrote only %#x bytes to " PCI_FMT
		       " %#02lx+%#zx\n", check_len, PCI_ARGS ( pci ),
		       where, len );
		rc = -EIO;
		goto err_write;
	}

	/* Success */
	rc = 0;

 err_write:
	linux_close ( fd );
 err_open:
	return rc;
}
Пример #9
0
void
audio_close (AUDIO_OUT *audio_out)
{
#if defined (__linux__)
	linux_close (audio_out) ;
#elif (defined (__MACH__) && defined (__APPLE__))
	macosx_close (audio_out) ;
#elif (defined (sun) && defined (unix))
	solaris_close (audio_out) ;
#elif (defined (_WIN32) || defined (WIN32))
	win32_close (audio_out) ;
#else
	#warning "*** Playing sound not yet supported on this platform."
	#warning "*** Please feel free to submit a patch."
	printf ("Error : Playing sound not yet supported on this platform.\n") ;
	return ;
#endif

	return ;
} /* audio_close */
Пример #10
0
/** Close the packet socket */
static void af_packet_nic_close ( struct net_device *netdev )
{
	struct af_packet_nic * nic = netdev->priv;
	linux_close(nic->fd);
}
Пример #11
0
/**
 * Disable entropy gathering
 *
 */
static void linux_entropy_disable ( void ) {

	/* Close entropy source */
	linux_close ( entropy_fd );
}