Index: uspace/lib/nic/Makefile
===================================================================
--- uspace/lib/nic/Makefile	(revision 00d7e1bfe06501df61aed156666d5597397d910a)
+++ uspace/lib/nic/Makefile	(revision 00d7e1bfe06501df61aed156666d5597397d910a)
@@ -0,0 +1,41 @@
+#
+# Copyright (c) 2011 Radim Vansa
+# All rights reserved.
+#
+# Redistribution and use in source and binary forms, with or without
+# modification, are permitted provided that the following conditions
+# are met:
+#
+# - Redistributions of source code must retain the above copyright
+#   notice, this list of conditions and the following disclaimer.
+# - Redistributions in binary form must reproduce the above copyright
+#   notice, this list of conditions and the following disclaimer in the
+#   documentation and/or other materials provided with the distribution.
+# - The name of the author may not be used to endorse or promote products
+#   derived from this software without specific prior written permission.
+#
+# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+# OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+# IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+# NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+#
+
+USPACE_PREFIX = ../..
+LIBRARY = libnic
+LIBS = $(LIBDRV_PREFIX)/libdrv.a $(LIBNET_PREFIX)/libnet.a
+EXTRA_CFLAGS += -DLIBNIC_INTERNAL -Iinclude -I$(LIBDRV_PREFIX)/include -I$(LIBNET_PREFIX)/include
+
+SOURCES = \
+	src/nic_driver.c \
+	src/nic_addr_db.c \
+	src/nic_rx_control.c \
+	src/nic_wol_virtues.c \
+	src/nic_impl.c
+
+include $(USPACE_PREFIX)/Makefile.common
Index: uspace/lib/nic/include/nic.h
===================================================================
--- uspace/lib/nic/include/nic.h	(revision 00d7e1bfe06501df61aed156666d5597397d910a)
+++ uspace/lib/nic/include/nic.h	(revision 00d7e1bfe06501df61aed156666d5597397d910a)
@@ -0,0 +1,283 @@
+/*
+ * Copyright (c) 2011 Radim Vansa
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * - Redistributions of source code must retain the above copyright
+ *   notice, this list of conditions and the following disclaimer.
+ * - Redistributions in binary form must reproduce the above copyright
+ *   notice, this list of conditions and the following disclaimer in the
+ *   documentation and/or other materials provided with the distribution.
+ * - The name of the author may not be used to endorse or promote products
+ *   derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+/**
+ * @addtogroup libnic
+ * @{
+ */
+/**
+ * @file
+ * @brief Public header exposing NICF to drivers
+ */
+
+#ifndef __NIC_H__
+#define __NIC_H__
+
+#include <adt/list.h>
+#include <ddf/driver.h>
+#include <device/hw_res_parsed.h>
+#include <net/packet.h>
+#include <ops/nic.h>
+
+struct nic;
+typedef struct nic nic_t;
+
+/**
+ * Single WOL virtue descriptor.
+ */
+typedef struct nic_wol_virtue {
+	link_t item;
+	nic_wv_id_t id;
+	nic_wv_type_t type;
+	const void *data;
+	size_t length;
+	struct nic_wol_virtue *next;
+} nic_wol_virtue_t;
+
+/**
+ * Simple structure for sending the allocated frames (packets) in a list.
+ */
+typedef struct {
+	link_t link;
+	packet_t *packet;
+} nic_frame_t;
+
+typedef list_t nic_frame_list_t;
+
+/**
+ * Handler for writing packet data to the NIC device.
+ * The function is responsible for releasing the packet.
+ * It does not return anything, if some error is detected the function just
+ * silently fails (logging on debug level is suggested).
+ *
+ * @param nic_data
+ * @param packet	Pointer to the packet to be sent
+ */
+typedef void (*write_packet_handler)(nic_t *, packet_t *);
+/**
+ * The handler for transitions between driver states.
+ * If the handler returns negative error code, the transition between
+ * states is canceled (the state is not changed).
+ *
+ * @param nic_data	NICF main structure
+ *
+ * @return EOK	If everything is all right.
+ * @return negative error code on error.
+ */
+typedef int (*state_change_handler)(nic_t *);
+/**
+ * Handler for unicast filtering mode change.
+ *
+ * @param nic_data		NICF main structure
+ * @param new_mode		The mode that should be set up
+ * @param address_list	Addresses as argument to the mode
+ * @param address_count	Number of addresses in the list
+ *
+ * @return EOK		If the mode is set up
+ * @return ENOTSUP	If this mode is not supported
+ */
+typedef int (*unicast_mode_change_handler)(nic_t *,
+	nic_unicast_mode_t, const nic_address_t *, size_t);
+/**
+ * Handler for multicast filtering mode change.
+ *
+ * @param nic_data		NICF main structure
+ * @param new_mode		The mode that should be set up
+ * @param address_list	Addresses as argument to the mode
+ * @param address_count	Number of addresses in the list
+ *
+ * @return EOK		If the mode is set up
+ * @return ENOTSUP	If this mode is not supported
+ */
+typedef int (*multicast_mode_change_handler)(nic_t *,
+	nic_multicast_mode_t, const nic_address_t *, size_t);
+/**
+ * Handler for broadcast filtering mode change.
+ *
+ * @param nic_data	NICF main structure
+ * @param new_mode	The mode that should be set up
+ *
+ * @return EOK		If the mode is set up
+ * @return ENOTSUP	If this mode is not supported
+ */
+typedef int (*broadcast_mode_change_handler)(nic_t *, nic_broadcast_mode_t);
+/**
+ * Handler for blocked sources list change.
+ *
+ * @param nic_data		NICF main structure
+ * @param address_list	Addresses as argument to the mode
+ * @param address_count	Number of addresses in the list
+ */
+typedef void (*blocked_sources_change_handler)(nic_t *,
+	const nic_address_t *, size_t);
+/**
+ * Handler for VLAN filtering mask change.
+ * @param nic_data		NICF main structure
+ * @param vlan_mask		The new mask | NULL for disabling vlan filter
+ */
+typedef void (*vlan_mask_change_handler)(nic_t *, const nic_vlan_mask_t *);
+/**
+ * Handler called when a WOL virtue is added.
+ * If the maximum of accepted WOL virtues changes due to adding this virtue
+ * you should update the vector wol_virtues.caps_max.
+ * The driver is allowed to store pointer to the virtue data until
+ * on_wol_virtue_remove on these data is called (although probably this is
+ * not a good practice).
+ *
+ * @param nic_data	NICF main structure
+ * @param virtue	Structure with virtue description
+ *
+ * @return EOK		If the filter can be used. Software emulation of the
+ * 					filter is activated unless the emulate is set to 0.
+ * @return ENOTSUP	If this filter cannot work on this NIC (e.g. the NIC
+ * 					cannot run in promiscuous node or the limit of WOL
+ * 					packets' specifications was reached).
+ * @return ELIMIT	If this filter must implemented in HW but currently the
+ * 					limit of these HW filters was reached.
+ */
+typedef int (*wol_virtue_add_handler)(nic_t *, const nic_wol_virtue_t *);
+/**
+ * Handler called when a WOL virtue is removed.
+ * If the maximum of accepted WOL virtues changes due to removing this
+ * virtue you should update the vector wol_virtues.caps_max.
+ *
+ * @param nic_data	NICF main structure
+ * @param virtue		Structure with virtue description
+ */
+typedef void (*wol_virtue_remove_handler)(nic_t *, const nic_wol_virtue_t *);
+/**
+ * Handler for poll mode change.
+ *
+ * @param nic_data	NICF main structure
+ * @param mode		Mode to be set up
+ * @param period	New period of polling (with NIC_POLL_PERIODIC)
+ *
+ * @return EOK		If the mode was fully setup
+ * @return ENOTSUP	If NICF should do the periodic polling
+ * @return EINVAL	If this mode cannot be set up under no circumstances
+ */
+typedef int (*poll_mode_change_handler)(nic_t *,
+	nic_poll_mode_t, const struct timeval *);
+/**
+ * Event handler called when the NIC should poll its buffers for a new frame
+ * (in NIC_POLL_PERIODIC or NIC_POLL_ON_DEMAND) modes.
+ *
+ * @param nic_data	NICF main structure
+ */
+typedef void (*poll_request_handler)(nic_t *);
+
+/* nic_t allocation and deallocation */
+extern nic_t *nic_create_and_bind(ddf_dev_t *);
+extern void nic_unbind_and_destroy(ddf_dev_t *);
+
+/* Functions called in the main function */
+extern int nic_driver_init(const char *);
+extern void nic_driver_implement(driver_ops_t *, ddf_dev_ops_t *,
+	nic_iface_t *);
+
+/* Functions called in add_device */
+extern int nic_connect_to_services(nic_t *);
+extern int nic_register_as_ddf_fun(nic_t *, ddf_dev_ops_t *);
+extern int nic_get_resources(nic_t *, hw_res_list_parsed_t *);
+extern void nic_set_specific(nic_t *, void *);
+extern void nic_set_write_packet_handler(nic_t *, write_packet_handler);
+extern void nic_set_state_change_handlers(nic_t *,
+	state_change_handler, state_change_handler, state_change_handler);
+extern void nic_set_filtering_change_handlers(nic_t *,
+	unicast_mode_change_handler, multicast_mode_change_handler,
+	broadcast_mode_change_handler, blocked_sources_change_handler,
+	vlan_mask_change_handler);
+extern void nic_set_wol_virtue_change_handlers(nic_t *,
+	wol_virtue_add_handler, wol_virtue_remove_handler);
+extern void nic_set_poll_handlers(nic_t *,
+	poll_mode_change_handler, poll_request_handler);
+
+/* Functions called in device_added */
+extern int nic_ready(nic_t *);
+
+/* General driver functions */
+extern ddf_dev_t *nic_get_ddf_dev(nic_t *);
+extern ddf_fun_t *nic_get_ddf_fun(nic_t *);
+extern nic_t *nic_get_from_ddf_dev(ddf_dev_t *);
+extern nic_t *nic_get_from_ddf_fun(ddf_fun_t *);
+extern void *nic_get_specific(nic_t *);
+extern nic_device_state_t nic_query_state(nic_t *);
+extern void nic_set_tx_busy(nic_t *, int);
+extern int nic_report_address(nic_t *, const nic_address_t *);
+extern int nic_report_poll_mode(nic_t *, nic_poll_mode_t, struct timeval *);
+extern void nic_query_address(nic_t *, nic_address_t *);
+extern void nic_received_packet(nic_t *, packet_t *);
+extern void nic_received_noneth_packet(nic_t *, packet_t *);
+extern void nic_received_frame(nic_t *, nic_frame_t *);
+extern void nic_received_frame_list(nic_t *, nic_frame_list_t *);
+extern void nic_disable_interrupt(nic_t *, int);
+extern void nic_enable_interrupt(nic_t *, int);
+extern nic_poll_mode_t nic_query_poll_mode(nic_t *, struct timeval *);
+
+/* Statistics updates */
+extern void nic_report_send_ok(nic_t *, size_t, size_t);
+extern void nic_report_send_error(nic_t *, nic_send_error_cause_t, unsigned);
+extern void nic_report_receive_error(nic_t *, nic_receive_error_cause_t,
+    unsigned);
+extern void nic_report_collisions(nic_t *, unsigned);
+
+/* Packet / frame / frame list allocation and deallocation */
+extern packet_t *nic_alloc_packet(nic_t *, size_t);
+extern void nic_release_packet(nic_t *, packet_t *);
+extern nic_frame_t *nic_alloc_frame(nic_t *, size_t);
+extern nic_frame_list_t *nic_alloc_frame_list(void);
+extern void nic_frame_list_append(nic_frame_list_t *, nic_frame_t *);
+extern void nic_release_frame(nic_t *, nic_frame_t *);
+
+/* RXC query and report functions */
+extern void nic_report_hw_filtering(nic_t *, int, int, int);
+extern void nic_query_unicast(const nic_t *,
+	nic_unicast_mode_t *, size_t, nic_address_t *, size_t *);
+extern void nic_query_multicast(const nic_t *,
+	nic_multicast_mode_t *, size_t, nic_address_t *, size_t *);
+extern void nic_query_broadcast(const nic_t *, nic_broadcast_mode_t *);
+extern void nic_query_blocked_sources(const nic_t *,
+	size_t, nic_address_t *, size_t *);
+extern int nic_query_vlan_mask(const nic_t *, nic_vlan_mask_t *);
+extern int nic_query_wol_max_caps(const nic_t *, nic_wv_type_t);
+extern void nic_set_wol_max_caps(nic_t *, nic_wv_type_t, int);
+extern uint64_t nic_mcast_hash(const nic_address_t *, size_t);
+extern uint64_t nic_query_mcast_hash(nic_t *);
+
+/* Software period functions */
+extern void nic_sw_period_start(nic_t *);
+extern void nic_sw_period_stop(nic_t *);
+
+/* Packet DMA lock */
+extern void * nic_dma_lock_packet(packet_t * packet);
+extern void nic_dma_unlock_packet(packet_t * packet);
+
+#endif // __NIC_H__
+
+/** @}
+ */
Index: uspace/lib/nic/include/nic_addr_db.h
===================================================================
--- uspace/lib/nic/include/nic_addr_db.h	(revision 00d7e1bfe06501df61aed156666d5597397d910a)
+++ uspace/lib/nic/include/nic_addr_db.h	(revision 00d7e1bfe06501df61aed156666d5597397d910a)
@@ -0,0 +1,88 @@
+/*
+ * Copyright (c) 2011 Radim Vansa
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * - Redistributions of source code must retain the above copyright
+ *   notice, this list of conditions and the following disclaimer.
+ * - Redistributions in binary form must reproduce the above copyright
+ *   notice, this list of conditions and the following disclaimer in the
+ *   documentation and/or other materials provided with the distribution.
+ * - The name of the author may not be used to endorse or promote products
+ *   derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+/**
+ * @addtogroup libnic
+ * @{
+ */
+/**
+ * @file
+ * @brief Generic hash-set based database of addresses
+ */
+
+#ifndef __NIC_ADDR_DB_H__
+#define __NIC_ADDR_DB_H__
+
+#ifndef LIBNIC_INTERNAL
+#error "This is internal libnic header, please don't include it"
+#endif
+
+#include <adt/hash_set.h>
+
+/**
+ * Initial size of DB's hash set
+ */
+#define NIC_ADDR_DB_INIT_SIZE 	8
+/**
+ * Maximal length of addresses in the DB (in bytes).
+ */
+#define NIC_ADDR_MAX_LENGTH		16
+
+/**
+ * Fibril-safe database of addresses implemented using hash set.
+ */
+typedef struct nic_addr_db {
+	hash_set_t set;
+	size_t addr_len;
+} nic_addr_db_t;
+
+/**
+ * Helper structure for keeping the address in the hash set.
+ */
+typedef struct nic_addr_entry {
+	link_t item;
+	size_t addr_len;
+	uint8_t addr[NIC_ADDR_MAX_LENGTH];
+} nic_addr_entry_t;
+
+extern int nic_addr_db_init(nic_addr_db_t *db, size_t addr_len);
+extern void nic_addr_db_clear(nic_addr_db_t *db);
+extern void nic_addr_db_destroy(nic_addr_db_t *db);
+extern size_t nic_addr_db_count(const nic_addr_db_t *db);
+extern int nic_addr_db_insert(nic_addr_db_t *db, const uint8_t *addr);
+extern int nic_addr_db_remove(nic_addr_db_t *db, const uint8_t *addr);
+extern void nic_addr_db_remove_selected(nic_addr_db_t *db,
+	int (*func)(const uint8_t *, void *), void *arg);
+extern int nic_addr_db_contains(const nic_addr_db_t *db, const uint8_t *addr);
+extern void nic_addr_db_foreach(const nic_addr_db_t *db,
+	void (*func)(const uint8_t *, void *), void *arg);
+
+#endif
+
+/** @}
+ */
Index: uspace/lib/nic/include/nic_driver.h
===================================================================
--- uspace/lib/nic/include/nic_driver.h	(revision 00d7e1bfe06501df61aed156666d5597397d910a)
+++ uspace/lib/nic/include/nic_driver.h	(revision 00d7e1bfe06501df61aed156666d5597397d910a)
@@ -0,0 +1,223 @@
+/*
+ * Copyright (c) 2011 Radim Vansa
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * - Redistributions of source code must retain the above copyright
+ *   notice, this list of conditions and the following disclaimer.
+ * - Redistributions in binary form must reproduce the above copyright
+ *   notice, this list of conditions and the following disclaimer in the
+ *   documentation and/or other materials provided with the distribution.
+ * - The name of the author may not be used to endorse or promote products
+ *   derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+/**
+ * @addtogroup libnic
+ * @{
+ */
+/**
+ * @file
+ * @brief Internal NICF structures
+ */
+
+#ifndef __NIC_DRIVER_H__
+#define __NIC_DRIVER_H__
+
+#ifndef LIBNIC_INTERNAL
+#error "This is internal libnic's header, please do not include it"
+#endif
+
+#include <fibril_synch.h>
+#include <net/device.h>
+#include <async.h>
+
+#include "nic.h"
+#include "nic_rx_control.h"
+#include "nic_wol_virtues.h"
+
+#define DEVICE_CATEGORY_NIC "nic"
+
+struct sw_poll_info {
+	fid_t fibril;
+	volatile int run;
+	volatile int running;
+};
+
+struct nic {
+	/**
+	 * Device from device manager's point of view.
+	 * The value must be set within the add_device handler
+	 * (in nic_create_and_bind) and must not be changed.
+	 */
+	ddf_dev_t *dev;
+	/**
+	 * Device's NIC function.
+	 * The value must be set within the add_device handler
+	 * (in nic_driver_register_as_ddf_function) and must not be changed.
+	 */
+	ddf_fun_t *fun;
+	/** Identifier for higher network stack layers */
+	nic_device_id_t device_id;
+	/** Current state of the device */
+	nic_device_state_t state;
+	/** Transmiter is busy - messages are dropped */
+	int tx_busy;
+	/** Device's MAC address */
+	nic_address_t mac;
+	/** Device's default MAC address (assigned the first time, used in STOP) */
+	nic_address_t default_mac;
+	/** Session to SERVICE_NETWORKING */
+	async_sess_t *net_session;
+	/** Session to SERVICE_ETHERNET or SERVICE_NILDUMMY */
+	async_sess_t *nil_session;
+	/** Phone to APIC or i8259 */
+	async_sess_t *irc_session;
+	/** Current polling mode of the NIC */
+	nic_poll_mode_t poll_mode;
+	/** Polling period (applicable when poll_mode == NIC_POLL_PERIODIC) */
+	struct timeval poll_period;
+	/** Current polling mode of the NIC */
+	nic_poll_mode_t default_poll_mode;
+	/** Polling period (applicable when default_poll_mode == NIC_POLL_PERIODIC) */
+	struct timeval default_poll_period;
+	/** Software period fibrill information */
+	struct sw_poll_info sw_poll_info;
+	/**
+	 * Lock on everything but statistics, rx control and wol virtues. This lock
+	 * cannot be used if filters_lock or stats_lock is already held - you must
+	 * acquire main_lock first (otherwise deadlock could happen).
+	 */
+	fibril_rwlock_t main_lock;
+	/** Device statistics */
+	nic_device_stats_t stats;
+	/**
+	 * Lock for statistics. You must not hold any other lock from nic_t except
+	 * the main_lock at the same moment. If both this lock and main_lock should
+	 * be locked, the main_lock must be locked as the first.
+	 */
+	fibril_rwlock_t stats_lock;
+	/** Receive control configuration */
+	nic_rxc_t rx_control;
+	/**
+	 * Lock for receive control. You must not hold any other lock from nic_t
+	 * except the main_lock at the same moment. If both this lock and main_lock
+	 * should be locked, the main_lock must be locked as the first.
+	 */
+	fibril_rwlock_t rxc_lock;
+	/** WOL virtues configuration */
+	nic_wol_virtues_t wol_virtues;
+	/**
+	 * Lock for WOL virtues. You must not hold any other lock from nic_t
+	 * except the main_lock at the same moment. If both this lock and main_lock
+	 * should be locked, the main_lock must be locked as the first.
+	 */
+	fibril_rwlock_t wv_lock;
+	/**
+	 * Function really sending the data. This MUST be filled in if the 
+	 * nic_send_message_impl function is used for sending messages (filled 
+	 * as send_message member of the nic_iface_t structure).
+	 * Called with the main_lock locked for reading.
+	 */
+	write_packet_handler write_packet;
+	/**
+	 * Event handler called when device goes to the ACTIVE state.
+	 * The implementation is optional.
+	 * Called with the main_lock locked for writing.
+	 */
+	state_change_handler on_activating;
+	/**
+	 * Event handler called when device goes to the DOWN state.
+	 * The implementation is optional.
+	 * Called with the main_lock locked for writing.
+	 */
+	state_change_handler on_going_down;
+	/**
+	 * Event handler called when device goes to the STOPPED state.
+	 * The implementation is optional.
+	 * Called with the main_lock locked for writing.
+	 */
+	state_change_handler on_stopping;
+	/**
+	 * Event handler called when the unicast receive mode is changed.
+	 * The implementation is optional. Called with rxc_lock locked for writing.
+	 */
+	unicast_mode_change_handler on_unicast_mode_change;
+	/**
+	 * Event handler called when the multicast receive mode is changed.
+	 * The implementation is optional. Called with rxc_lock locked for writing.
+	 */
+	multicast_mode_change_handler on_multicast_mode_change;
+	/**
+	 * Event handler called when the broadcast receive mode is changed.
+	 * The implementation is optional. Called with rxc_lock locked for writing.
+	 */
+	broadcast_mode_change_handler on_broadcast_mode_change;
+	/**
+	 * Event handler called when the blocked sources set is changed.
+	 * The implementation is optional. Called with rxc_lock locked for writing.
+	 */
+	blocked_sources_change_handler on_blocked_sources_change;
+	/**
+	 * Event handler called when the VLAN mask is changed.
+	 * The implementation is optional. Called with rxc_lock locked for writing.
+	 */
+	vlan_mask_change_handler on_vlan_mask_change;
+	/**
+	 * Event handler called when a new WOL virtue is added.
+	 * The implementation is optional.
+	 * Called with filters_lock locked for writing.
+	 */
+	wol_virtue_add_handler on_wol_virtue_add;
+	/**
+	 * Event handler called when a WOL virtue is removed.
+	 * The implementation is optional.
+	 * Called with filters_lock locked for writing.
+	 */
+	wol_virtue_remove_handler on_wol_virtue_remove;
+	/**
+	 * Event handler called when the polling mode is changed.
+	 * The implementation is optional.
+	 * Called with main_lock locked for writing.
+	 */
+	poll_mode_change_handler on_poll_mode_change;
+	/**
+	 * Event handler called when the NIC should poll its buffers for a new frame
+	 * (in NIC_POLL_PERIODIC or NIC_POLL_ON_DEMAND) modes.
+	 * Called with the main_lock locked for reading.
+	 * The implementation is optional.
+	 */
+	poll_request_handler on_poll_request;
+	/** Data specific for particular driver */
+	void *specific;
+};
+
+/**
+ * Structure keeping global data
+ */
+typedef struct nic_globals {
+	list_t frame_list_cache;
+	size_t frame_list_cache_size;
+	list_t frame_cache;
+	size_t frame_cache_size;
+	fibril_mutex_t lock;
+} nic_globals_t;
+
+#endif
+
+/** @}
+ */
Index: uspace/lib/nic/include/nic_impl.h
===================================================================
--- uspace/lib/nic/include/nic_impl.h	(revision 00d7e1bfe06501df61aed156666d5597397d910a)
+++ uspace/lib/nic/include/nic_impl.h	(revision 00d7e1bfe06501df61aed156666d5597397d910a)
@@ -0,0 +1,95 @@
+/*
+ * Copyright (c) 2011 Radim Vansa
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * - Redistributions of source code must retain the above copyright
+ *   notice, this list of conditions and the following disclaimer.
+ * - Redistributions in binary form must reproduce the above copyright
+ *   notice, this list of conditions and the following disclaimer in the
+ *   documentation and/or other materials provided with the distribution.
+ * - The name of the author may not be used to endorse or promote products
+ *   derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+/**
+ * @addtogroup libnic
+ * @{
+ */
+/**
+ * @file
+ * @brief Prototypes of default DDF NIC interface methods implementations
+ */
+
+#ifndef NIC_IMPL_H__
+#define NIC_IMPL_H__
+
+#include <assert.h>
+#include <net/device.h>
+#include <ddf/driver.h>
+#include <nil_remote.h>
+
+/* Inclusion of this file is not prohibited, because drivers could want to
+ * inject some adaptation layer between the DDF call and NICF implementation */
+
+extern int nic_get_address_impl(ddf_fun_t *dev_fun, nic_address_t *address);
+extern int nic_send_message_impl(ddf_fun_t *dev_fun, packet_id_t packet_id);
+extern int nic_connect_to_nil_impl(ddf_fun_t *dev_fun, services_t nil_service,
+	int device_id);
+extern int nic_get_state_impl(ddf_fun_t *dev_fun, nic_device_state_t *state);
+extern int nic_set_state_impl(ddf_fun_t *dev_fun, nic_device_state_t state);
+extern int nic_get_stats_impl(ddf_fun_t *dev_fun, nic_device_stats_t *stats);
+extern int nic_unicast_get_mode_impl(ddf_fun_t *dev_fun,
+	nic_unicast_mode_t *, size_t, nic_address_t *, size_t *);
+extern int nic_unicast_set_mode_impl(ddf_fun_t *dev_fun,
+	nic_unicast_mode_t, const nic_address_t *, size_t);
+extern int nic_multicast_get_mode_impl(ddf_fun_t *dev_fun,
+	nic_multicast_mode_t *, size_t, nic_address_t *, size_t *);
+extern int nic_multicast_set_mode_impl(ddf_fun_t *dev_fun,
+	nic_multicast_mode_t, const nic_address_t *, size_t);
+extern int nic_broadcast_get_mode_impl(ddf_fun_t *, nic_broadcast_mode_t *);
+extern int nic_broadcast_set_mode_impl(ddf_fun_t *, nic_broadcast_mode_t);
+extern int nic_blocked_sources_get_impl(ddf_fun_t *,
+	size_t, nic_address_t *, size_t *);
+extern int nic_blocked_sources_set_impl(ddf_fun_t *, const nic_address_t *, size_t);
+extern int nic_vlan_get_mask_impl(ddf_fun_t *, nic_vlan_mask_t *);
+extern int nic_vlan_set_mask_impl(ddf_fun_t *, const nic_vlan_mask_t *);
+extern int nic_wol_virtue_add_impl(ddf_fun_t *dev_fun, nic_wv_type_t type,
+	const void *data, size_t length, nic_wv_id_t *new_id);
+extern int nic_wol_virtue_remove_impl(ddf_fun_t *dev_fun, nic_wv_id_t id);
+extern int nic_wol_virtue_probe_impl(ddf_fun_t *dev_fun, nic_wv_id_t id,
+	nic_wv_type_t *type, size_t max_length, void *data, size_t *length);
+extern int nic_wol_virtue_list_impl(ddf_fun_t *dev_fun, nic_wv_type_t type,
+	size_t max_count, nic_wv_id_t *id_list, size_t *id_count);
+extern int nic_wol_virtue_get_caps_impl(ddf_fun_t *, nic_wv_type_t, int *);
+extern int nic_poll_get_mode_impl(ddf_fun_t *,
+	nic_poll_mode_t *, struct timeval *);
+extern int nic_poll_set_mode_impl(ddf_fun_t *,
+	nic_poll_mode_t, const struct timeval *);
+extern int nic_poll_now_impl(ddf_fun_t *);
+
+extern void nic_default_handler_impl(ddf_fun_t *dev_fun,
+	ipc_callid_t callid, ipc_call_t *call);
+extern int nic_open_impl(ddf_fun_t *fun);
+extern void nic_close_impl(ddf_fun_t *fun);
+
+extern void nic_device_added_impl(ddf_dev_t *dev);
+
+#endif
+
+/** @}
+ */
Index: uspace/lib/nic/include/nic_rx_control.h
===================================================================
--- uspace/lib/nic/include/nic_rx_control.h	(revision 00d7e1bfe06501df61aed156666d5597397d910a)
+++ uspace/lib/nic/include/nic_rx_control.h	(revision 00d7e1bfe06501df61aed156666d5597397d910a)
@@ -0,0 +1,149 @@
+/*
+ * Copyright (c) 2011 Radim Vansa
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * - Redistributions of source code must retain the above copyright
+ *   notice, this list of conditions and the following disclaimer.
+ * - Redistributions in binary form must reproduce the above copyright
+ *   notice, this list of conditions and the following disclaimer in the
+ *   documentation and/or other materials provided with the distribution.
+ * - The name of the author may not be used to endorse or promote products
+ *   derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+/**
+ * @addtogroup libnic
+ * @{
+ */
+/**
+ * @file
+ * @brief Incoming packets (frames) filtering control structures
+ */
+
+#ifndef __NIC_FILTERS_H__
+#define __NIC_FILTERS_H__
+
+#ifndef LIBNIC_INTERNAL
+#error "This is internal libnic's header, please do not include it"
+#endif
+
+#include <adt/hash_table.h>
+#include <fibril_synch.h>
+#include <net/device.h>
+#include <net/packet_header.h>
+
+#include "nic_addr_db.h"
+
+/**
+ * General structure describing receive control.
+ * The structure is not synchronized inside, the nic_driver should provide
+ * a synchronized facade.
+ */
+typedef struct nic_rxc {
+	/**
+	 * Allowed unicast destination MAC addresses
+	 */
+	nic_addr_db_t unicast_addrs;
+	/**
+	 * Allowed unicast destination MAC addresses
+	 */
+	nic_addr_db_t multicast_addrs;
+	/**
+	 * Single flag if any source is blocked
+	 */
+	int block_sources;
+	/**
+	 * Blocked source MAC addresses
+	 */
+	nic_addr_db_t blocked_sources;
+	/**
+	 * Selected mode for unicast frames
+	 */
+	nic_unicast_mode_t unicast_mode;
+	/**
+	 * Selected mode for multicast frames
+	 */
+	nic_multicast_mode_t multicast_mode;
+	/**
+	 * Selected mode for broadcast frames
+	 */
+	nic_broadcast_mode_t broadcast_mode;
+	/**
+	 * Mask for VLAN tags. This vector must be at least 512 bytes long.
+	 */
+	nic_vlan_mask_t *vlan_mask;
+	/**
+	 * If true, the NIC is receiving only unicast frames which we really want to
+	 * receive (the filtering is perfect).
+	 */
+	int unicast_exact;
+	/**
+	 * If true, the NIC is receiving only multicast frames which we really want
+	 * to receive (the filtering is perfect).
+	 */
+	int multicast_exact;
+	/**
+	 * If true, the NIC is receiving only frames with VLAN tags which we really
+	 * want to receive (the filtering is perfect).
+	 */
+	int vlan_exact;
+} nic_rxc_t;
+
+#define VLAN_TPID_UPPER 0x81
+#define VLAN_TPID_LOWER 0x00
+
+typedef struct vlan_header {
+	uint8_t tpid_upper;
+	uint8_t tpid_lower;
+	uint8_t vid_upper;
+	uint8_t vid_lower;
+} __attribute__ ((packed)) vlan_header_t;
+
+extern int nic_rxc_init(nic_rxc_t *rxc);
+extern int nic_rxc_clear(nic_rxc_t *rxc);
+extern int nic_rxc_set_addr(nic_rxc_t *rxc,
+	const nic_address_t *prev_addr, const nic_address_t *curr_addr);
+extern int nic_rxc_check(const nic_rxc_t *rxc,
+	const packet_t *packet, nic_frame_type_t *frame_type);
+extern void nic_rxc_hw_filtering(nic_rxc_t *rxc,
+	int unicast_exact, int multicast_exact, int vlan_exact);
+extern uint64_t nic_rxc_mcast_hash(const nic_address_t *list, size_t count);
+extern uint64_t nic_rxc_multicast_get_hash(const nic_rxc_t *rxc);
+extern void nic_rxc_unicast_get_mode(const nic_rxc_t *, nic_unicast_mode_t *,
+	size_t max_count, nic_address_t *address_list, size_t *address_count);
+extern int nic_rxc_unicast_set_mode(nic_rxc_t *rxc, nic_unicast_mode_t mode,
+	const nic_address_t *address_list, size_t address_count);
+extern void nic_rxc_multicast_get_mode(const nic_rxc_t *,
+	nic_multicast_mode_t *, size_t, nic_address_t *, size_t *);
+extern int nic_rxc_multicast_set_mode(nic_rxc_t *, nic_multicast_mode_t mode,
+	const nic_address_t *address_list, size_t address_count);
+extern void nic_rxc_broadcast_get_mode(const nic_rxc_t *,
+	nic_broadcast_mode_t *mode);
+extern int nic_rxc_broadcast_set_mode(nic_rxc_t *,
+	nic_broadcast_mode_t mode);
+extern void nic_rxc_blocked_sources_get(const nic_rxc_t *,
+	size_t max_count, nic_address_t *address_list, size_t *address_count);
+extern int nic_rxc_blocked_sources_set(nic_rxc_t *,
+	const nic_address_t *address_list, size_t address_count);
+extern int nic_rxc_vlan_get_mask(const nic_rxc_t *rxc, nic_vlan_mask_t *mask);
+extern int nic_rxc_vlan_set_mask(nic_rxc_t *rxc, const nic_vlan_mask_t *mask);
+
+#endif
+
+/** @}
+ */
Index: uspace/lib/nic/include/nic_wol_virtues.h
===================================================================
--- uspace/lib/nic/include/nic_wol_virtues.h	(revision 00d7e1bfe06501df61aed156666d5597397d910a)
+++ uspace/lib/nic/include/nic_wol_virtues.h	(revision 00d7e1bfe06501df61aed156666d5597397d910a)
@@ -0,0 +1,90 @@
+/*
+ * Copyright (c) 2011 Radim Vansa
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * - Redistributions of source code must retain the above copyright
+ *   notice, this list of conditions and the following disclaimer.
+ * - Redistributions in binary form must reproduce the above copyright
+ *   notice, this list of conditions and the following disclaimer in the
+ *   documentation and/or other materials provided with the distribution.
+ * - The name of the author may not be used to endorse or promote products
+ *   derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+/**
+ * @addtogroup libnic
+ * @{
+ */
+/**
+ * @file
+ * @brief Wake-on-LAN support
+ */
+
+#ifndef __NIC_WOL_VIRTUES_H__
+#define __NIC_WOL_VIRTUES_H__
+
+#ifndef LIBNIC_INTERNAL
+#error "This is internal libnic's header, please do not include it"
+#endif
+
+#include <net/device.h>
+#include <adt/hash_table.h>
+#include "nic.h"
+
+typedef struct nic_wol_virtues {
+	/**
+	 * Operations for table
+	 */
+	hash_table_operations_t table_operations;
+	/**
+	 * WOL virtues hashed by their ID's.
+	 */
+	hash_table_t table;
+	/**
+	 * WOL virtues in lists by their type
+	 */
+	nic_wol_virtue_t *lists[NIC_WV_MAX];
+	/**
+	 * Number of virtues in the wv_types list
+	 */
+	size_t lists_sizes[NIC_WV_MAX];
+	/**
+	 * Counter for the ID's
+	 */
+	nic_wv_id_t next_id;
+	/**
+	 * Maximum capabilities
+	 */
+	int caps_max[NIC_WV_MAX];
+} nic_wol_virtues_t;
+
+extern int nic_wol_virtues_init(nic_wol_virtues_t *);
+extern void nic_wol_virtues_clear(nic_wol_virtues_t *);
+extern int nic_wol_virtues_verify(nic_wv_type_t, const void *, size_t);
+extern int nic_wol_virtues_list(const nic_wol_virtues_t *, nic_wv_type_t type,
+	size_t max_count, nic_wv_id_t *id_list, size_t *id_count);
+extern int nic_wol_virtues_add(nic_wol_virtues_t *, nic_wol_virtue_t *);
+extern nic_wol_virtue_t *nic_wol_virtues_remove(nic_wol_virtues_t *,
+	nic_wv_id_t);
+extern const nic_wol_virtue_t *nic_wol_virtues_find(const nic_wol_virtues_t *,
+	nic_wv_id_t);
+
+#endif
+
+/** @}
+ */
Index: uspace/lib/nic/src/nic_addr_db.c
===================================================================
--- uspace/lib/nic/src/nic_addr_db.c	(revision 00d7e1bfe06501df61aed156666d5597397d910a)
+++ uspace/lib/nic/src/nic_addr_db.c	(revision 00d7e1bfe06501df61aed156666d5597397d910a)
@@ -0,0 +1,277 @@
+/*
+ * Copyright (c) 2011 Radim Vansa
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * - Redistributions of source code must retain the above copyright
+ *   notice, this list of conditions and the following disclaimer.
+ * - Redistributions in binary form must reproduce the above copyright
+ *   notice, this list of conditions and the following disclaimer in the
+ *   documentation and/or other materials provided with the distribution.
+ * - The name of the author may not be used to endorse or promote products
+ *   derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+/**
+ * @addtogroup libnic
+ * @{
+ */
+/**
+ * @file
+ * @brief Generic hash-set based database of addresses
+ */
+#include <assert.h>
+#include <stdlib.h>
+#include <bool.h>
+#include <errno.h>
+#include <mem.h>
+
+#include "nic_addr_db.h"
+
+/**
+ * Hash set helper function
+ */
+static int nic_addr_equals(const link_t *item1, const link_t *item2)
+{
+	assert(item1 && item2);
+	size_t addr_len = ((const nic_addr_entry_t *) item1)->addr_len;
+
+	assert(addr_len	== ((const nic_addr_entry_t *) item2)->addr_len);
+
+	size_t i;
+	for (i = 0; i < addr_len; ++i) {
+		if (((nic_addr_entry_t *) item1)->addr[i] !=
+			((nic_addr_entry_t *) item2)->addr[i])
+			return false;
+	}
+	return true;
+}
+
+/**
+ * Hash set helper function
+ */
+static unsigned long nic_addr_hash(const link_t *item)
+{
+	assert(item);
+	const nic_addr_entry_t *entry = (const nic_addr_entry_t *) item;
+	unsigned long hash = 0;
+
+	size_t i;
+	for (i = 0; i < entry->addr_len; ++i) {
+		hash = (hash << 8) ^ (hash >> 24) ^ entry->addr[i];
+	}
+	return hash;
+}
+
+/**
+ * Helper wrapper
+ */
+static void nic_addr_destroy(link_t *item, void *unused)
+{
+	free(item);
+}
+
+/**
+ * Initialize the database
+ *
+ * @param[in,out]	db			Uninitialized storage
+ * @param[in]		addr_len	Size of addresses in the db
+ *
+ * @return EOK		If successfully initialized
+ * @return EINVAL	If the address length is too big
+ * @return ENOMEM	If there was not enough memory to initialize the storage
+ */
+int nic_addr_db_init(nic_addr_db_t *db, size_t addr_len)
+{
+	assert(db);
+	if (addr_len > NIC_ADDR_MAX_LENGTH) {
+		return EINVAL;
+	}
+	if (!hash_set_init(&db->set, nic_addr_hash, nic_addr_equals,
+		NIC_ADDR_DB_INIT_SIZE)) {
+		return ENOMEM;
+	}
+	db->addr_len = addr_len;
+	return EOK;
+}
+
+/**
+ * Remove all records from the DB
+ *
+ * @param db
+ */
+void nic_addr_db_clear(nic_addr_db_t *db)
+{
+	assert(db);
+	hash_set_clear(&db->set, nic_addr_destroy, NULL);
+}
+
+/**
+ * Free the memory used by db, including all records...
+ *
+ * @param	db
+ */
+void nic_addr_db_destroy(nic_addr_db_t *db)
+{
+	assert(db);
+	hash_set_apply(&db->set, nic_addr_destroy, NULL);
+	hash_set_destroy(&db->set);
+}
+
+/**
+ * Get number of addresses in the db
+ *
+ * @param	db
+ *
+ * @return Number of adresses
+ */
+size_t nic_addr_db_count(const nic_addr_db_t *db)
+{
+	assert(db);
+	return hash_set_count(&db->set);
+}
+
+/**
+ * Insert an address to the db
+ *
+ * @param	db
+ * @param	addr 	Inserted address. Length is implicitly concluded from the
+ * 					db's address length.
+ *
+ * @return EOK		If the address was inserted
+ * @return ENOMEM	If there was not enough memory
+ * @return EEXIST	If this adress already is in the db
+ */
+int nic_addr_db_insert(nic_addr_db_t *db, const uint8_t *addr)
+{
+	assert(db && addr);
+	nic_addr_entry_t *entry = malloc(sizeof (nic_addr_entry_t));
+	if (entry == NULL) {
+		return ENOMEM;
+	}
+	entry->addr_len = db->addr_len;
+	memcpy(entry->addr, addr, db->addr_len);
+
+	return hash_set_insert(&db->set, &entry->item) ? EOK : EEXIST;
+}
+
+/**
+ * Remove the address from the db
+ *
+ * @param	db
+ * @param	addr	Removed address.
+ *
+ * @return EOK		If the address was removed
+ * @return ENOENT	If there is no address
+ */
+int nic_addr_db_remove(nic_addr_db_t *db, const uint8_t *addr)
+{
+	assert(db && addr);
+	nic_addr_entry_t entry;
+	entry.addr_len = db->addr_len;
+	memcpy(entry.addr, addr, db->addr_len);
+
+	link_t *removed = hash_set_remove(&db->set, &entry.item);
+	free(removed);
+	return removed != NULL ? EOK : ENOENT;
+}
+
+/**
+ * Test if the address is contained in the db
+ *
+ * @param	db
+ * @param	addr	Tested addresss
+ *
+ * @return true if the address is in the db, false otherwise
+ */
+int nic_addr_db_contains(const nic_addr_db_t *db, const uint8_t *addr)
+{
+	assert(db && addr);
+	nic_addr_entry_t entry;
+	entry.addr_len = db->addr_len;
+	memcpy(entry.addr, addr, db->addr_len);
+
+	return hash_set_contains(&db->set, &entry.item);
+}
+
+/**
+ * Helper structure for nic_addr_db_foreach
+ */
+typedef struct {
+	void (*func)(const uint8_t *, void *);
+	void *arg;
+} nic_addr_db_fe_arg_t;
+
+/**
+ * Helper function for nic_addr_db_foreach
+ */
+static void nic_addr_db_fe_helper(link_t *item, void *arg) {
+	nic_addr_db_fe_arg_t *hs = (nic_addr_db_fe_arg_t *) arg;
+	hs->func(((nic_addr_entry_t *) item)->addr, hs->arg);
+}
+
+/**
+ * Executes a user-defined function on all addresses in the DB. The function
+ * must not change the addresses.
+ *
+ * @param	db
+ * @param	func	User-defined function
+ * @param	arg		Custom argument passed to the function
+ */
+void nic_addr_db_foreach(const nic_addr_db_t *db,
+	void (*func)(const uint8_t *, void *), void *arg)
+{
+	nic_addr_db_fe_arg_t hs = { .func = func, .arg = arg };
+	hash_set_apply((hash_set_t *) &db->set, nic_addr_db_fe_helper, &hs);
+}
+
+/**
+ * Helper structure for nic_addr_db_remove_selected
+ */
+typedef struct {
+	int (*func)(const uint8_t *, void *);
+	void *arg;
+} nic_addr_db_rs_arg_t;
+
+/**
+ * Helper function for nic_addr_db_foreach
+ */
+static int nic_addr_db_rs_helper(link_t *item, void *arg) {
+	nic_addr_db_rs_arg_t *hs = (nic_addr_db_rs_arg_t *) arg;
+	int retval = hs->func(((nic_addr_entry_t *) item)->addr, hs->arg);
+	if (retval) {
+		free(item);
+	}
+	return retval;
+}
+
+/**
+ * Removes all addresses for which the function returns non-zero.
+ *
+ * @param	db
+ * @param	func	User-defined function
+ * @param	arg		Custom argument passed to the function
+ */
+void nic_addr_db_remove_selected(nic_addr_db_t *db,
+	int (*func)(const uint8_t *, void *), void *arg)
+{
+	nic_addr_db_rs_arg_t hs = { .func = func, .arg = arg };
+	hash_set_remove_selected(&db->set, nic_addr_db_rs_helper, &hs);
+}
+
+/** @}
+ */
Index: uspace/lib/nic/src/nic_driver.c
===================================================================
--- uspace/lib/nic/src/nic_driver.c	(revision 00d7e1bfe06501df61aed156666d5597397d910a)
+++ uspace/lib/nic/src/nic_driver.c	(revision 00d7e1bfe06501df61aed156666d5597397d910a)
@@ -0,0 +1,1368 @@
+/*
+ * Copyright (c) 2011 Radim Vansa
+ * Copyright (c) 2011 Jiri Michalec
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * - Redistributions of source code must retain the above copyright
+ *   notice, this list of conditions and the following disclaimer.
+ * - Redistributions in binary form must reproduce the above copyright
+ *   notice, this list of conditions and the following disclaimer in the
+ *   documentation and/or other materials provided with the distribution.
+ * - The name of the author may not be used to endorse or promote products
+ *   derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+/**
+ * @addtogroup libnic
+ * @{
+ */
+/**
+ * @file
+ * @brief Internal implementation of general NIC operations
+ */
+
+#include <assert.h>
+#include <fibril_synch.h>
+#include <ns.h>
+#include <stdio.h>
+#include <str_error.h>
+#include <ipc/services.h>
+#include <ipc/ns.h>
+#include <ipc/irc.h>
+#include <sysinfo.h>
+
+#include <devman.h>
+#include <ddf/interrupt.h>
+#include <net_interface.h>
+#include <ops/nic.h>
+#include <packet_client.h>
+#include <packet_remote.h>
+#include <net/packet_header.h>
+#include <errno.h>
+
+#include "nic_driver.h"
+#include "nic_impl.h"
+
+#define NIC_GLOBALS_MAX_CACHE_SIZE 16
+
+nic_globals_t nic_globals;
+
+/**
+ * Initializes libraries required for NIC framework - logger, packet manager
+ *
+ * @param name	Name of the device/driver (used in logging)
+ */
+int nic_driver_init(const char *name)
+{
+	list_initialize(&nic_globals.frame_list_cache);
+	nic_globals.frame_list_cache_size = 0;
+	list_initialize(&nic_globals.frame_cache);
+	nic_globals.frame_cache_size = 0;
+	fibril_mutex_initialize(&nic_globals.lock);
+	
+	char buffer[256];
+	snprintf(buffer, 256, "drv/" DEVICE_CATEGORY_NIC "/%s", name);
+	
+	/* Initialize packet manager */
+	return pm_init();
+}
+
+/**
+ * Fill in the default implementations for device options and NIC interface.
+ *
+ * @param driver_ops
+ * @param dev_ops
+ * @param iface
+ */
+void nic_driver_implement(driver_ops_t *driver_ops, ddf_dev_ops_t *dev_ops,
+    nic_iface_t *iface)
+{
+	if (driver_ops) {
+		if (!driver_ops->device_added)
+			driver_ops->device_added = nic_device_added_impl;
+	}
+
+	if (dev_ops) {
+		if (!dev_ops->open)
+			dev_ops->open = nic_open_impl;
+		if (!dev_ops->close)
+			dev_ops->close = nic_close_impl;
+		if (!dev_ops->interfaces[NIC_DEV_IFACE])
+			dev_ops->interfaces[NIC_DEV_IFACE] = iface;
+		if (!dev_ops->default_handler)
+			dev_ops->default_handler = nic_default_handler_impl;
+	}
+
+	if (iface) {
+		if (!iface->get_state)
+			iface->get_state = nic_get_state_impl;
+		if (!iface->set_state)
+			iface->set_state = nic_set_state_impl;
+		if (!iface->send_message)
+			iface->send_message = nic_send_message_impl;
+		if (!iface->connect_to_nil)
+			iface->connect_to_nil = nic_connect_to_nil_impl;
+		if (!iface->get_address)
+			iface->get_address = nic_get_address_impl;
+		if (!iface->get_stats)
+			iface->get_stats = nic_get_stats_impl;
+		if (!iface->unicast_get_mode)
+			iface->unicast_get_mode = nic_unicast_get_mode_impl;
+		if (!iface->unicast_set_mode)
+			iface->unicast_set_mode = nic_unicast_set_mode_impl;
+		if (!iface->multicast_get_mode)
+			iface->multicast_get_mode = nic_multicast_get_mode_impl;
+		if (!iface->multicast_set_mode)
+			iface->multicast_set_mode = nic_multicast_set_mode_impl;
+		if (!iface->broadcast_get_mode)
+			iface->broadcast_get_mode = nic_broadcast_get_mode_impl;
+		if (!iface->broadcast_set_mode)
+			iface->broadcast_set_mode = nic_broadcast_set_mode_impl;
+		if (!iface->blocked_sources_get)
+			iface->blocked_sources_get = nic_blocked_sources_get_impl;
+		if (!iface->blocked_sources_set)
+			iface->blocked_sources_set = nic_blocked_sources_set_impl;
+		if (!iface->vlan_get_mask)
+			iface->vlan_get_mask = nic_vlan_get_mask_impl;
+		if (!iface->vlan_set_mask)
+			iface->vlan_set_mask = nic_vlan_set_mask_impl;
+		if (!iface->wol_virtue_add)
+			iface->wol_virtue_add = nic_wol_virtue_add_impl;
+		if (!iface->wol_virtue_remove)
+			iface->wol_virtue_remove = nic_wol_virtue_remove_impl;
+		if (!iface->wol_virtue_probe)
+			iface->wol_virtue_probe = nic_wol_virtue_probe_impl;
+		if (!iface->wol_virtue_list)
+			iface->wol_virtue_list = nic_wol_virtue_list_impl;
+		if (!iface->wol_virtue_get_caps)
+			iface->wol_virtue_get_caps = nic_wol_virtue_get_caps_impl;
+		if (!iface->poll_get_mode)
+			iface->poll_get_mode = nic_poll_get_mode_impl;
+		if (!iface->poll_set_mode)
+			iface->poll_set_mode = nic_poll_set_mode_impl;
+		if (!iface->poll_now)
+			iface->poll_now = nic_poll_now_impl;
+	}
+}
+
+/**
+ * Setup write packet handler. This MUST be called in the add_device handler
+ * if the nic_send_message_impl function is used for sending messages (filled
+ * as send_message member of the nic_iface_t structure). The function must not
+ * be called anywhere else.
+ *
+ * @param nic_data
+ * @param wpfunc		Function handling the write_packet request
+ */
+void nic_set_write_packet_handler(nic_t *nic_data, write_packet_handler wpfunc)
+{
+	nic_data->write_packet = wpfunc;
+}
+
+/**
+ * Setup event handlers for transitions between driver states.
+ * This function can be called only in the add_device handler.
+ *
+ * @param on_activating	Called when device is going to the ACTIVE state.
+ * @param on_going_down Called when device is going to the DOWN state.
+ * @param on_stopping	Called when device is going to the STOP state.
+ */
+void nic_set_state_change_handlers(nic_t *nic_data,
+	state_change_handler on_activating, state_change_handler on_going_down,
+	state_change_handler on_stopping)
+{
+	nic_data->on_activating = on_activating;
+	nic_data->on_going_down = on_going_down;
+	nic_data->on_stopping = on_stopping;
+}
+
+/**
+ * Setup event handlers for changing the filtering modes.
+ * This function can be called only in the add_device handler.
+ *
+ * @param nic_data
+ * @param on_unicast_mode_change
+ * @param on_multicast_mode_change
+ * @param on_broadcast_mode_change
+ * @param on_blocked_sources_change
+ * @param on_vlan_mask_change
+ */
+void nic_set_filtering_change_handlers(nic_t *nic_data,
+	unicast_mode_change_handler on_unicast_mode_change,
+	multicast_mode_change_handler on_multicast_mode_change,
+	broadcast_mode_change_handler on_broadcast_mode_change,
+	blocked_sources_change_handler on_blocked_sources_change,
+	vlan_mask_change_handler on_vlan_mask_change)
+{
+	nic_data->on_unicast_mode_change = on_unicast_mode_change;
+	nic_data->on_multicast_mode_change = on_multicast_mode_change;
+	nic_data->on_broadcast_mode_change = on_broadcast_mode_change;
+	nic_data->on_blocked_sources_change = on_blocked_sources_change;
+	nic_data->on_vlan_mask_change = on_vlan_mask_change;
+}
+
+/**
+ * Setup filters for WOL virtues add and removal.
+ * This function can be called only in the add_device handler. Both handlers
+ * must be set or none of them.
+ *
+ * @param on_wv_add		Called when a virtue is added
+ * @param on_wv_remove	Called when a virtue is removed
+ */
+void nic_set_wol_virtue_change_handlers(nic_t *nic_data,
+	wol_virtue_add_handler on_wv_add, wol_virtue_remove_handler on_wv_remove)
+{
+	assert(on_wv_add != NULL && on_wv_remove != NULL);
+	nic_data->on_wol_virtue_add = on_wv_add;
+	nic_data->on_wol_virtue_remove = on_wv_remove;
+}
+
+/**
+ * Setup poll handlers.
+ * This function can be called only in the add_device handler.
+ *
+ * @param on_poll_mode_change	Called when the mode is about to be changed
+ * @param on_poll_request		Called when poll request is triggered
+ */
+void nic_set_poll_handlers(nic_t *nic_data,
+	poll_mode_change_handler on_poll_mode_change, poll_request_handler on_poll_req)
+{
+	nic_data->on_poll_mode_change = on_poll_mode_change;
+	nic_data->on_poll_request = on_poll_req;
+}
+
+/**
+ * Connect to the parent's driver and get HW resources list in parsed format.
+ * Note: this function should be called only from add_device handler, therefore
+ * we don't need to use locks.
+ *
+ * @param		nic_data
+ * @param[out]	resources	Parsed lists of resources.
+ *
+ * @return EOK or negative error code
+ */
+int nic_get_resources(nic_t *nic_data, hw_res_list_parsed_t *resources)
+{
+	ddf_dev_t *dev = nic_data->dev;
+	
+	/* Connect to the parent's driver. */
+	dev->parent_sess = devman_parent_device_connect(EXCHANGE_SERIALIZE,
+		dev->handle, IPC_FLAG_BLOCKING);
+	if (dev->parent_sess == NULL)
+		return EPARTY;
+	
+	return hw_res_get_list_parsed(nic_data->dev->parent_sess, resources, 0);
+}
+
+/**
+ * Just a wrapper over the packet_get_1_remote function
+ */
+packet_t *nic_alloc_packet(nic_t *nic_data, size_t data_size)
+{
+	return packet_get_1_remote(nic_data->net_session, data_size);
+}
+
+
+/**
+ * Just a wrapper over the pq_release_remote function
+ */
+void nic_release_packet(nic_t *nic_data, packet_t *packet)
+{
+	pq_release_remote(nic_data->net_session, packet_get_id(packet));
+}
+
+/** Allocate frame and packet
+ *
+ *  @param nic_data 	The NIC driver data
+ *  @param packet_size	Size of packet
+ *  @param offload_size	Size of packet offload
+ *  @return pointer to allocated frame if success, NULL otherwise
+ */
+nic_frame_t *nic_alloc_frame(nic_t *nic_data, size_t packet_size)
+{
+	nic_frame_t *frame;
+	fibril_mutex_lock(&nic_globals.lock);
+	if (nic_globals.frame_cache_size > 0) {
+		link_t *first = list_first(&nic_globals.frame_cache);
+		list_remove(first);
+		nic_globals.frame_cache_size--;
+		frame = list_get_instance(first, nic_frame_t, link);
+		fibril_mutex_unlock(&nic_globals.lock);
+	} else {
+		fibril_mutex_unlock(&nic_globals.lock);
+		frame = malloc(sizeof(nic_frame_t));
+		if (!frame)
+			return NULL;
+		
+		link_initialize(&frame->link);
+	}
+
+	packet_t *packet = nic_alloc_packet(nic_data, packet_size);
+	if (!packet) {
+		free(frame);
+		return NULL;
+	}
+
+	frame->packet = packet;
+	return frame;
+}
+
+/** Release frame
+ *
+ * @param nic_data	The driver data
+ * @param frame		The frame to release
+ */
+void nic_release_frame(nic_t *nic_data, nic_frame_t *frame)
+{
+	if (!frame)
+		return;
+	if (frame->packet != NULL) {
+		nic_release_packet(nic_data, frame->packet);
+	}
+	fibril_mutex_lock(&nic_globals.lock);
+	if (nic_globals.frame_cache_size >= NIC_GLOBALS_MAX_CACHE_SIZE) {
+		fibril_mutex_unlock(&nic_globals.lock);
+		free(frame);
+	} else {
+		list_prepend(&frame->link, &nic_globals.frame_cache);
+		nic_globals.frame_cache_size++;
+		fibril_mutex_unlock(&nic_globals.lock);
+	}
+}
+
+/**
+ * Allocate a new frame list
+ *
+ * @return New frame list or NULL on error.
+ */
+nic_frame_list_t *nic_alloc_frame_list(void)
+{
+	nic_frame_list_t *frames;
+	fibril_mutex_lock(&nic_globals.lock);
+	
+	if (nic_globals.frame_list_cache_size > 0) {
+		frames =
+		    list_get_instance(list_first(&nic_globals.frame_list_cache),
+		    nic_frame_list_t, head);
+		list_remove(&frames->head);
+		list_initialize(frames);
+		nic_globals.frame_list_cache_size--;
+		fibril_mutex_unlock(&nic_globals.lock);
+	} else {
+		fibril_mutex_unlock(&nic_globals.lock);
+		
+		frames = malloc(sizeof (nic_frame_list_t));
+		if (frames != NULL)
+			list_initialize(frames);
+	}
+	
+	return frames;
+}
+
+static void nic_driver_release_frame_list(nic_frame_list_t *frames)
+{
+	if (!frames)
+		return;
+	fibril_mutex_lock(&nic_globals.lock);
+	if (nic_globals.frame_list_cache_size >= NIC_GLOBALS_MAX_CACHE_SIZE) {
+		fibril_mutex_unlock(&nic_globals.lock);
+		free(frames);
+	} else {
+		list_prepend(&frames->head, &nic_globals.frame_list_cache);
+		nic_globals.frame_list_cache_size++;
+		fibril_mutex_unlock(&nic_globals.lock);
+	}
+}
+
+/**
+ * Append a frame to the frame list
+ *
+ * @param frames	Frame list
+ * @param frame		Appended frame
+ */
+void nic_frame_list_append(nic_frame_list_t *frames,
+	nic_frame_t *frame)
+{
+	assert(frame != NULL && frames != NULL);
+	list_append(&frame->link, frames);
+}
+
+
+/**
+ * Enable interrupts for this driver.
+ *
+ * @param nic_data
+ * @param irq			The IRQ number for this device
+ */
+void nic_enable_interrupt(nic_t *nic_data, int irq)
+{
+	async_exch_t *exch = async_exchange_begin(nic_data->irc_session);
+	async_msg_1(exch, IRC_ENABLE_INTERRUPT, irq);
+	async_exchange_end(exch);
+}
+
+/**
+ * Disable interrupts for this driver.
+ *
+ * @param nic_data
+ * @param irq			The IRQ number for this device
+ */
+void nic_disable_interrupt(nic_t *nic_data, int irq)
+{
+	async_exch_t *exch = async_exchange_begin(nic_data->irc_session);
+	async_msg_1(exch, IRC_CLEAR_INTERRUPT, irq);
+	async_exchange_end(exch);
+}
+
+/** Get the polling mode information from the device 
+ *
+ *	The main lock should be locked, otherwise the inconsistency between
+ *	mode and period can occure.
+ *
+ *  @param nic_data The controller data
+ *  @param period [out] The the period. Valid only if mode == NIC_POLL_PERIODIC
+ *  @return Current polling mode of the controller
+ */
+nic_poll_mode_t nic_query_poll_mode(nic_t *nic_data, struct timeval *period)
+{
+	if (period)
+		*period = nic_data->poll_period;
+	return nic_data->poll_mode;
+};
+
+/**
+ * Connect to the NET and IRQ services. This function should be called only from
+ * the add_device handler, thus no locking is required.
+ *
+ * @param nic_data
+ *
+ * @return EOK		If connection was successful.
+ * @return EINVAL	If the IRC service cannot be determined.
+ * @return EREFUSED	If NET or IRC service cannot be connected.
+ */
+int nic_connect_to_services(nic_t *nic_data)
+{
+	/* NET service */
+	nic_data->net_session = service_connect_blocking(EXCHANGE_SERIALIZE,
+		SERVICE_NETWORKING, 0, 0);
+	if (nic_data->net_session == NULL)
+		return errno;
+	
+	/* IRC service */
+	sysarg_t apic;
+	sysarg_t i8259;
+	services_t irc_service = -1;
+	if (((sysinfo_get_value("apic", &apic) == EOK) && (apic)) ||
+	    ((sysinfo_get_value("i8259", &i8259) == EOK) && (i8259)))
+		irc_service = SERVICE_IRC;
+	else
+		return EINVAL;
+	
+	nic_data->irc_session = service_connect_blocking(EXCHANGE_SERIALIZE,
+		irc_service, 0, 0);
+	if (nic_data->irc_session == NULL)
+		return errno;
+	
+	return EOK;
+}
+
+/** Notify the NET service that the device is ready
+ *
+ * @param nic NICF structure
+ *
+ * @return EOK on success
+ *
+ */
+int nic_ready(nic_t *nic)
+{
+	fibril_rwlock_read_lock(&nic->main_lock);
+	
+	async_sess_t *session = nic->net_session;
+	devman_handle_t handle = nic->dev->handle;
+	
+	fibril_rwlock_read_unlock(&nic->main_lock);
+	
+	if (session == NULL)
+		return EINVAL;
+	
+	return net_driver_ready(session, handle);
+}
+
+/** Inform the NICF about poll mode
+ *
+ *  @param nic_data The controller data
+ *  @param mode
+ *  @param period [out] The the period. Valid only if mode == NIC_POLL_PERIODIC
+ *  @return EOK
+ *  @return EINVAL
+ */
+int nic_report_poll_mode(nic_t *nic_data, nic_poll_mode_t mode,
+	struct timeval *period)
+{
+	int rc = EOK;
+	fibril_rwlock_write_lock(&nic_data->main_lock);
+	nic_data->poll_mode = mode;
+	nic_data->default_poll_mode = mode;
+	if (mode == NIC_POLL_PERIODIC) {
+		if (period) {
+			memcpy(&nic_data->default_poll_period, period, sizeof (struct timeval));
+			memcpy(&nic_data->poll_period, period, sizeof (struct timeval));
+		} else {
+			rc = EINVAL;
+		}
+	}
+	fibril_rwlock_write_unlock(&nic_data->main_lock);
+	return rc;
+}
+
+/** Inform the NICF about device's MAC adress.
+ *
+ * @return EOK On success
+ *
+ */
+int nic_report_address(nic_t *nic_data, const nic_address_t *address)
+{
+	assert(nic_data);
+	
+	if (address->address[0] & 1)
+		return EINVAL;
+	
+	fibril_rwlock_write_lock(&nic_data->main_lock);
+	
+	/* Notify NIL layer (and uppper) if bound - not in add_device */
+	if (nic_data->nil_session != NULL) {
+		int rc = nil_addr_changed_msg(nic_data->nil_session,
+		    nic_data->device_id, address);
+		if (rc != EOK) {
+			fibril_rwlock_write_unlock(&nic_data->main_lock);
+			return rc;
+		}
+	}
+	
+	fibril_rwlock_write_lock(&nic_data->rxc_lock);
+	
+	/*
+	 * The initial address (all zeroes) shouldn't be
+	 * there and we will ignore that error -- in next
+	 * calls this should not happen.
+	 */
+	int rc = nic_rxc_set_addr(&nic_data->rx_control,
+	    &nic_data->mac, address);
+	
+	/* For the first time also record the default MAC */
+	if (MAC_IS_ZERO(nic_data->default_mac.address)) {
+		assert(MAC_IS_ZERO(nic_data->mac.address));
+		memcpy(&nic_data->default_mac, address, sizeof(nic_address_t));
+	}
+	
+	fibril_rwlock_write_unlock(&nic_data->rxc_lock);
+	
+	if ((rc != EOK) && (rc != ENOENT)) {
+		fibril_rwlock_write_unlock(&nic_data->main_lock);
+		return rc;
+	}
+	
+	memcpy(&nic_data->mac, address, sizeof(nic_address_t));
+	
+	fibril_rwlock_write_unlock(&nic_data->main_lock);
+	
+	return EOK;
+}
+
+/**
+ * Used to obtain devices MAC address.
+ *
+ * The main lock should be locked, otherwise the inconsistent address
+ * can be returend.
+ *
+ * @param nic_data The controller data
+ * @param address The output for address.
+ */
+void nic_query_address(nic_t *nic_data, nic_address_t *addr) {
+	if (!addr)
+		return;
+	if (!nic_data)
+		memset(addr, 0, sizeof(nic_address_t));
+
+	memcpy(addr, &nic_data->mac, sizeof(nic_address_t));
+};
+
+/**
+ * The busy flag can be set to 1 only in the write_packet handler, to 0 it can
+ * be set anywhere.
+ *
+ * @param nic_data
+ * @param busy
+ */
+void nic_set_tx_busy(nic_t *nic_data, int busy)
+{
+	/*
+	 * When the function is called in write_packet handler the main lock is
+	 * locked so no race can happen.
+	 * Otherwise, when it is unexpectedly set to 0 (even with main lock held
+	 * by other fibril) it cannot crash anything.
+	 */
+	nic_data->tx_busy = busy;
+}
+
+/**
+ * Provided for correct naming conventions.
+ * The packet is checked by filters and then sent up to the NIL layer or
+ * discarded, the frame is released.
+ *
+ * @param nic_data
+ * @param frame		The frame containing received packet
+ */
+void nic_received_frame(nic_t *nic_data, nic_frame_t *frame)
+{
+	nic_received_packet(nic_data, frame->packet);
+	frame->packet = NULL;
+	nic_release_frame(nic_data, frame);
+}
+
+/**
+ * This is the function that the driver should call when it receives a packet.
+ * The packet is checked by filters and then sent up to the NIL layer or
+ * discarded.
+ *
+ * @param nic_data
+ * @param packet		The received packet
+ */
+void nic_received_packet(nic_t *nic_data, packet_t *packet)
+{
+	/* Note: this function must not lock main lock, because loopback driver
+	 * 		 calls it inside write_packet handler (with locked main lock) */
+	packet_id_t pid = packet_get_id(packet);
+	
+	fibril_rwlock_read_lock(&nic_data->rxc_lock);
+	nic_frame_type_t frame_type;
+	int check = nic_rxc_check(&nic_data->rx_control, packet, &frame_type);
+	fibril_rwlock_read_unlock(&nic_data->rxc_lock);
+	/* Update statistics */
+	fibril_rwlock_write_lock(&nic_data->stats_lock);
+	/* Both sending message up and releasing packet are atomic IPC calls */
+	if (nic_data->state == NIC_STATE_ACTIVE && check) {
+		nic_data->stats.receive_packets++;
+		nic_data->stats.receive_bytes += packet_get_data_length(packet);
+		switch (frame_type) {
+		case NIC_FRAME_MULTICAST:
+			nic_data->stats.receive_multicast++;
+			break;
+		case NIC_FRAME_BROADCAST:
+			nic_data->stats.receive_broadcast++;
+			break;
+		default:
+			break;
+		}
+		fibril_rwlock_write_unlock(&nic_data->stats_lock);
+		nil_received_msg(nic_data->nil_session, pid, nic_data->device_id);
+	} else {
+		switch (frame_type) {
+		case NIC_FRAME_UNICAST:
+			nic_data->stats.receive_filtered_unicast++;
+			break;
+		case NIC_FRAME_MULTICAST:
+			nic_data->stats.receive_filtered_multicast++;
+			break;
+		case NIC_FRAME_BROADCAST:
+			nic_data->stats.receive_filtered_broadcast++;
+			break;
+		}
+		fibril_rwlock_write_unlock(&nic_data->stats_lock);
+		nic_release_packet(nic_data, packet);
+	}
+}
+
+/**
+ * This function is to be used only in the loopback driver. It's workaround
+ * for the situation when the packet does not contain ethernet address.
+ * The filtering is therefore not applied here.
+ *
+ * @param nic_data
+ * @param packet
+ */
+void nic_received_noneth_packet(nic_t *nic_data, packet_t *packet)
+{
+	fibril_rwlock_write_lock(&nic_data->stats_lock);
+	nic_data->stats.receive_packets++;
+	nic_data->stats.receive_bytes += packet_get_data_length(packet);
+	fibril_rwlock_write_unlock(&nic_data->stats_lock);
+
+	nil_received_msg(nic_data->nil_session, packet_get_id(packet),
+		nic_data->device_id);
+}
+
+/**
+ * Some NICs can receive multiple packets during single interrupt. These can
+ * send them in whole list of frames (actually nic_frame_t structures), then
+ * the list is deallocated and each packet is passed to the
+ * nic_received_packet function.
+ *
+ * @param nic_data
+ * @param frames		List of received frames
+ */
+void nic_received_frame_list(nic_t *nic_data, nic_frame_list_t *frames)
+{
+	if (frames == NULL)
+		return;
+	while (!list_empty(frames)) {
+		nic_frame_t *frame =
+			list_get_instance(list_first(frames), nic_frame_t, link);
+
+		list_remove(&frame->link);
+		nic_received_packet(nic_data, frame->packet);
+		frame->packet = NULL;
+		nic_release_frame(nic_data, frame);
+	}
+	nic_driver_release_frame_list(frames);
+}
+
+/** Allocate and initialize the driver data.
+ *
+ * @return Allocated structure or NULL.
+ *
+ */
+static nic_t *nic_create(void)
+{
+	nic_t *nic_data = malloc(sizeof(nic_t));
+	if (nic_data == NULL)
+		return NULL;
+	
+	/* Force zero to all uninitialized fields (e.g. added in future) */
+	bzero(nic_data, sizeof(nic_t));
+	if (nic_rxc_init(&nic_data->rx_control) != EOK) {
+		free(nic_data);
+		return NULL;
+	}
+	
+	if (nic_wol_virtues_init(&nic_data->wol_virtues) != EOK) {
+		free(nic_data);
+		return NULL;
+	}
+	
+	nic_data->dev = NULL;
+	nic_data->fun = NULL;
+	nic_data->device_id = NIC_DEVICE_INVALID_ID;
+	nic_data->state = NIC_STATE_STOPPED;
+	nic_data->net_session = NULL;
+	nic_data->nil_session = NULL;
+	nic_data->irc_session = NULL;
+	nic_data->poll_mode = NIC_POLL_IMMEDIATE;
+	nic_data->default_poll_mode = NIC_POLL_IMMEDIATE;
+	nic_data->write_packet = NULL;
+	nic_data->on_activating = NULL;
+	nic_data->on_going_down = NULL;
+	nic_data->on_stopping = NULL;
+	nic_data->specific = NULL;
+	
+	fibril_rwlock_initialize(&nic_data->main_lock);
+	fibril_rwlock_initialize(&nic_data->stats_lock);
+	fibril_rwlock_initialize(&nic_data->rxc_lock);
+	fibril_rwlock_initialize(&nic_data->wv_lock);
+	
+	bzero(&nic_data->mac, sizeof(nic_address_t));
+	bzero(&nic_data->default_mac, sizeof(nic_address_t));
+	bzero(&nic_data->stats, sizeof(nic_device_stats_t));
+	
+	return nic_data;
+}
+
+/** Create NIC structure for the device and bind it to dev_fun_t
+ *
+ * The pointer to the created and initialized NIC structure will
+ * be stored in device->nic_data.
+ *
+ * @param device The NIC device structure
+ *
+ * @return Pointer to created nic_t structure or NULL
+ *
+ */
+nic_t *nic_create_and_bind(ddf_dev_t *device)
+{
+	assert(device);
+	assert(!device->driver_data);
+	
+	nic_t *nic_data = nic_create();
+	if (!nic_data)
+		return NULL;
+	
+	nic_data->dev = device;
+	device->driver_data = nic_data;
+	
+	return nic_data;
+}
+
+/**
+ * Hangs up the phones in the structure, deallocates specific data and then
+ * the structure itself.
+ *
+ * @param data
+ */
+static void nic_destroy(nic_t *nic_data) {
+	if (nic_data->net_session != NULL) {
+		async_hangup(nic_data->net_session);
+	}
+
+	if (nic_data->nil_session != NULL) {
+		async_hangup(nic_data->nil_session);
+	}
+
+	free(nic_data->specific);
+	free(nic_data);
+}
+
+/**
+ * Unbind and destroy nic_t stored in ddf_dev_t.nic_data.
+ * The ddf_dev_t.nic_data will be set to NULL, specific driver data will be
+ * destroyed.
+ *
+ * @param device The NIC device structure
+ */
+void nic_unbind_and_destroy(ddf_dev_t *device){
+	if (!device)
+		return;
+	if (!device->driver_data)
+		return;
+
+	nic_destroy((nic_t *) device->driver_data);
+	device->driver_data = NULL;
+	return;
+}
+
+/**
+ * Creates an exposed DDF function for the device, named "port0".
+ * Device options are set as this function's options. The function is bound
+ * (see ddf_fun_bind) and then registered to the DEVICE_CATEGORY_NIC class.
+ * Note: this function should be called only from add_device handler, therefore
+ * we don't need to use locks.
+ *
+ * @param nic_data	The NIC structure
+ * @param ops		Device options for the DDF function.
+ */
+int nic_register_as_ddf_fun(nic_t *nic_data, ddf_dev_ops_t *ops)
+{
+	int rc;
+	assert(nic_data);
+
+	nic_data->fun = ddf_fun_create(nic_data->dev, fun_exposed, "port0");
+	if (nic_data->fun == NULL)
+		return ENOMEM;
+	
+	nic_data->fun->ops = ops;
+	nic_data->fun->driver_data = nic_data;
+
+	rc = ddf_fun_bind(nic_data->fun);
+	if (rc != EOK) {
+		ddf_fun_destroy(nic_data->fun);
+		return rc;
+	}
+
+	rc = ddf_fun_add_to_category(nic_data->fun, DEVICE_CATEGORY_NIC);
+	if (rc != EOK) {
+		ddf_fun_destroy(nic_data->fun);
+		return rc;
+	}
+	
+	return EOK;
+}
+
+/**
+ * Set information about current HW filtering.
+ *  1 ...	Only those frames we want to receive are passed through HW
+ *  0 ...	The HW filtering is imperfect
+ * -1 ...	Don't change the setting
+ * Can be called only from the on_*_change handler.
+ *
+ * @param	nic_data
+ * @param	unicast_exact	Unicast frames
+ * @param	mcast_exact		Multicast frames
+ * @param	vlan_exact		VLAN tags
+ */
+void nic_report_hw_filtering(nic_t *nic_data,
+	int unicast_exact, int multicast_exact, int vlan_exact)
+{
+	nic_rxc_hw_filtering(&nic_data->rx_control,
+		unicast_exact, multicast_exact, vlan_exact);
+}
+
+/**
+ * Computes hash for the address list based on standard multicast address
+ * hashing.
+ *
+ * @param address_list
+ * @param count
+ *
+ * @return Multicast hash
+ *
+ * @see multicast_hash
+ */
+uint64_t nic_mcast_hash(const nic_address_t *list, size_t count)
+{
+	return nic_rxc_mcast_hash(list, count);
+}
+
+/**
+ * Computes hash for multicast addresses currently set up in the RX multicast
+ * filtering. For promiscuous mode returns all ones, for blocking all zeroes.
+ * Can be called only from the state change handlers (on_activating,
+ * on_going_down and on_stopping).
+ *
+ * @param nic_data
+ *
+ * @return Multicast hash
+ *
+ * @see multicast_hash
+ */
+uint64_t nic_query_mcast_hash(nic_t *nic_data)
+{
+	fibril_rwlock_read_lock(&nic_data->rxc_lock);
+	uint64_t hash = nic_rxc_multicast_get_hash(&nic_data->rx_control);
+	fibril_rwlock_read_unlock(&nic_data->rxc_lock);
+	return hash;
+}
+
+/**
+ * Queries the current mode of unicast frames receiving.
+ * Can be called only from the on_*_change handler.
+ *
+ * @param nic_data
+ * @param mode			The new unicast mode
+ * @param max_count		Max number of addresses that can be written into the
+ * 						address_list.
+ * @param address_list	List of MAC addresses or NULL.
+ * @param address_count Number of addresses in the list
+ */
+void nic_query_unicast(const nic_t *nic_data,
+	nic_unicast_mode_t *mode,
+	size_t max_count, nic_address_t *address_list, size_t *address_count)
+{
+	assert(mode != NULL);
+	nic_rxc_unicast_get_mode(&nic_data->rx_control, mode,
+		max_count, address_list, address_count);
+}
+
+/**
+ * Queries the current mode of multicast frames receiving.
+ * Can be called only from the on_*_change handler.
+ *
+ * @param nic_data
+ * @param mode			The current multicast mode
+ * @param max_count		Max number of addresses that can be written into the
+ * 						address_list.
+ * @param address_list	List of MAC addresses or NULL.
+ * @param address_count Number of addresses in the list
+ */
+void nic_query_multicast(const nic_t *nic_data,
+	nic_multicast_mode_t *mode,
+	size_t max_count, nic_address_t *address_list, size_t *address_count)
+{
+	assert(mode != NULL);
+	nic_rxc_multicast_get_mode(&nic_data->rx_control, mode,
+		max_count, address_list, address_count);
+}
+
+/**
+ * Queries the current mode of broadcast frames receiving.
+ * Can be called only from the on_*_change handler.
+ *
+ * @param nic_data
+ * @param mode			The new broadcast mode
+ */
+void nic_query_broadcast(const nic_t *nic_data,
+	nic_broadcast_mode_t *mode)
+{
+	assert(mode != NULL);
+	nic_rxc_broadcast_get_mode(&nic_data->rx_control, mode);
+}
+
+/**
+ * Queries the current blocked source addresses.
+ * Can be called only from the on_*_change handler.
+ *
+ * @param nic_data
+ * @param max_count		Max number of addresses that can be written into the
+ * 						address_list.
+ * @param address_list	List of MAC addresses or NULL.
+ * @param address_count Number of addresses in the list
+ */
+void nic_query_blocked_sources(const nic_t *nic_data,
+	size_t max_count, nic_address_t *address_list, size_t *address_count)
+{
+	nic_rxc_blocked_sources_get(&nic_data->rx_control,
+		max_count, address_list, address_count);
+}
+
+/**
+ * Query mask used for filtering according to the VLAN tags.
+ * Can be called only from the on_*_change handler.
+ *
+ * @param nic_data
+ * @param mask		Must be 512 bytes long
+ *
+ * @return EOK
+ * @return ENOENT
+ */
+int nic_query_vlan_mask(const nic_t *nic_data, nic_vlan_mask_t *mask)
+{
+	assert(mask);
+	return nic_rxc_vlan_get_mask(&nic_data->rx_control, mask);
+}
+
+/**
+ * Query maximum number of WOL virtues of specified type allowed on the device.
+ * Can be called only from add_device and on_wol_virtue_* handlers.
+ *
+ * @param nic_data
+ * @param type		The type of the WOL virtues
+ *
+ * @return	Maximal number of allowed virtues of this type. -1 means this type
+ * 			is not supported at all.
+ */
+int nic_query_wol_max_caps(const nic_t *nic_data, nic_wv_type_t type)
+{
+	return nic_data->wol_virtues.caps_max[type];
+}
+
+/**
+ * Sets maximum number of WOL virtues of specified type allowed on the device.
+ * Can be called only from add_device and on_wol_virtue_* handlers.
+ *
+ * @param nic_data
+ * @param type		The type of the WOL virtues
+ * @param count		Maximal number of allowed virtues of this type. -1 means
+ * 					this type is not supported at all.
+ */
+void nic_set_wol_max_caps(nic_t *nic_data, nic_wv_type_t type, int count)
+{
+	nic_data->wol_virtues.caps_max[type] = count;
+}
+
+/**
+ * @param nic_data
+ * @return The driver-specific structure for this NIC.
+ */
+void *nic_get_specific(nic_t *nic_data)
+{
+	return nic_data->specific;
+}
+
+/**
+ * @param nic_data
+ * @param specific The driver-specific structure for this NIC.
+ */
+void nic_set_specific(nic_t *nic_data, void *specific)
+{
+	nic_data->specific = specific;
+}
+
+/**
+ * You can call the function only from one of the state change handlers.
+ * @param	nic_data
+ * @return	Current state of the NIC, prior to the actually executed change
+ */
+nic_device_state_t nic_query_state(nic_t *nic_data)
+{
+	return nic_data->state;
+}
+
+/**
+ * @param nic_data
+ * @return DDF device associated with this NIC.
+ */
+ddf_dev_t *nic_get_ddf_dev(nic_t *nic_data)
+{
+	return nic_data->dev;
+}
+
+/**
+ * @param nic_data
+ * @return DDF function associated with this NIC.
+ */
+ddf_fun_t *nic_get_ddf_fun(nic_t *nic_data)
+{
+	return nic_data->fun;
+}
+
+/** 
+ * @param dev DDF device associated with NIC
+ * @return The associated NIC structure
+ */
+nic_t *nic_get_from_ddf_dev(ddf_dev_t *dev)
+{
+	return (nic_t *) dev->driver_data;
+};
+
+/** 
+ * @param dev DDF function associated with NIC
+ * @return The associated NIC structure
+ */
+nic_t *nic_get_from_ddf_fun(ddf_fun_t *fun)
+{
+	return (nic_t *) fun->driver_data;
+};
+
+/**
+ * Raises the send_packets and send_bytes in device statistics.
+ *
+ * @param nic_data
+ * @param packets	Number of received packets
+ * @param bytes		Number of received bytes
+ */
+void nic_report_send_ok(nic_t *nic_data, size_t packets, size_t bytes)
+{
+	fibril_rwlock_write_lock(&nic_data->stats_lock);
+	nic_data->stats.send_packets += packets;
+	nic_data->stats.send_bytes += bytes;
+	fibril_rwlock_write_unlock(&nic_data->stats_lock);
+}
+
+/**
+ * Raises total error counter (send_errors) and the concrete send error counter
+ * determined by the cause argument.
+ *
+ * @param nic_data
+ * @param cause		The concrete error cause.
+ */
+void nic_report_send_error(nic_t *nic_data, nic_send_error_cause_t cause, 
+    unsigned count)
+{
+	if (count == 0)
+		return;
+	
+	fibril_rwlock_write_lock(&nic_data->stats_lock);
+	nic_data->stats.send_errors += count;
+	switch (cause) {
+	case NIC_SEC_BUFFER_FULL:
+		nic_data->stats.send_dropped += count;
+		break;
+	case NIC_SEC_ABORTED:
+		nic_data->stats.send_aborted_errors += count;
+		break;
+	case NIC_SEC_CARRIER_LOST:
+		nic_data->stats.send_carrier_errors += count;
+		break;
+	case NIC_SEC_FIFO_OVERRUN:
+		nic_data->stats.send_fifo_errors += count;
+		break;
+	case NIC_SEC_HEARTBEAT:
+		nic_data->stats.send_heartbeat_errors += count;
+		break;
+	case NIC_SEC_WINDOW_ERROR:
+		nic_data->stats.send_window_errors += count;
+		break;
+	case NIC_SEC_OTHER:
+		break;
+	}
+	fibril_rwlock_write_unlock(&nic_data->stats_lock);
+}
+
+/**
+ * Raises total error counter (receive_errors) and the concrete receive error
+ * counter determined by the cause argument.
+ *
+ * @param nic_data
+ * @param cause		The concrete error cause
+ */
+void nic_report_receive_error(nic_t *nic_data,
+	nic_receive_error_cause_t cause, unsigned count)
+{
+	fibril_rwlock_write_lock(&nic_data->stats_lock);
+	nic_data->stats.receive_errors += count;
+	switch (cause) {
+	case NIC_REC_BUFFER_FULL:
+		nic_data->stats.receive_dropped += count;
+		break;
+	case NIC_REC_LENGTH:
+		nic_data->stats.receive_length_errors += count;
+		break;
+	case NIC_REC_BUFFER_OVERFLOW:
+		nic_data->stats.receive_dropped += count;
+		break;
+	case NIC_REC_CRC:
+		nic_data->stats.receive_crc_errors += count;
+		break;
+	case NIC_REC_FRAME_ALIGNMENT:
+		nic_data->stats.receive_frame_errors += count;
+		break;
+	case NIC_REC_FIFO_OVERRUN:
+		nic_data->stats.receive_fifo_errors += count;
+		break;
+	case NIC_REC_MISSED:
+		nic_data->stats.receive_missed_errors += count;
+		break;
+	case NIC_REC_OTHER:
+		break;
+	}
+	fibril_rwlock_write_unlock(&nic_data->stats_lock);
+}
+
+/**
+ * Raises the collisions counter in device statistics.
+ */
+void nic_report_collisions(nic_t *nic_data, unsigned count)
+{
+	fibril_rwlock_write_lock(&nic_data->stats_lock);
+	nic_data->stats.collisions += count;
+	fibril_rwlock_write_unlock(&nic_data->stats_lock);
+}
+
+/** Just wrapper for checking nonzero time interval 
+ *
+ *  @oaram t The interval to check
+ *  @returns Zero if the t is nonzero interval
+ *  @returns Nonzero if t is zero interval
+ */
+static int timeval_nonpositive(struct timeval t) {
+	return (t.tv_sec <= 0) && (t.tv_usec <= 0);
+}
+
+/** Main function of software period fibrill
+ *
+ *  Just calls poll() in the nic->poll_period period
+ *
+ *  @param  data The NIC structure pointer
+ *
+ *  @return 0, never reached
+ */
+static int period_fibril_fun(void *data)
+{
+	nic_t *nic = data;
+	struct sw_poll_info *info = &nic->sw_poll_info;
+	while (true) {
+		fibril_rwlock_read_lock(&nic->main_lock);
+		int run = info->run;
+		int running = info->running;
+		struct timeval remaining = nic->poll_period;
+		fibril_rwlock_read_unlock(&nic->main_lock);
+
+		if (!running) {
+			remaining.tv_sec = 5;
+			remaining.tv_usec = 0;
+		}
+
+		/* Wait the period (keep attention to overflows) */
+		while (!timeval_nonpositive(remaining)) {
+			suseconds_t wait = 0;
+			if (remaining.tv_sec > 0) {
+				time_t wait_sec = remaining.tv_sec;
+				/* wait maximaly 5 seconds to get reasonable reaction time
+				 * when period is reset
+				 */
+				if (wait_sec > 5)
+					wait_sec = 5;
+
+				wait = (suseconds_t) wait_sec * 1000000;
+
+				remaining.tv_sec -= wait_sec;
+			} else {
+				wait = remaining.tv_usec;
+
+				if (wait > 5 * 1000000) {
+					wait = 5 * 1000000;
+				}
+
+				remaining.tv_usec -= wait;
+			}
+			async_usleep(wait);
+			
+			/* Check if the period was not reset */
+			if (info->run != run)
+				break;
+		}
+		
+		/* Provide polling if the period finished */
+		fibril_rwlock_read_lock(&nic->main_lock);
+		if (info->running && info->run == run) {
+			nic->on_poll_request(nic);
+		}
+		fibril_rwlock_read_unlock(&nic->main_lock);
+	}
+	return 0;
+}
+
+/** Starts software periodic polling
+ *
+ *  Reset to new period if the original period was running
+ *
+ *  @param nic_data Nic data structure
+ */
+void nic_sw_period_start(nic_t *nic_data)
+{
+	/* Create the fibril if it is not crated */
+	if (nic_data->sw_poll_info.fibril == 0) {
+		nic_data->sw_poll_info.fibril = fibril_create(period_fibril_fun, 
+		    nic_data);
+		nic_data->sw_poll_info.running = 0;
+		nic_data->sw_poll_info.run = 0;
+
+		/* Start fibril */
+		fibril_add_ready(nic_data->sw_poll_info.fibril);
+	}
+
+	/* Inform fibril about running with new period */
+	nic_data->sw_poll_info.run = (nic_data->sw_poll_info.run + 1) % 100;
+	nic_data->sw_poll_info.running = 1;
+}
+
+/** Stops software periodic polling
+ *
+ *  @param nic_data Nic data structure
+ */
+void nic_sw_period_stop(nic_t *nic_data)
+{
+	nic_data->sw_poll_info.running = 0;
+}
+
+// FIXME: Later
+#if 0
+
+/** Lock packet for DMA usage
+ *
+ * @param packet
+ * @return physical address of packet
+ */
+void *nic_dma_lock_packet(packet_t *packet)
+{
+	void *phys_addr;
+	size_t locked;
+	int rc = dma_lock(packet, &phys_addr, 1, &locked);
+	if (rc != EOK)
+		return NULL;
+	
+	assert(locked == 1);
+	return phys_addr;
+}
+
+/** Unlock packet after DMA usage
+ *
+ * @param packet
+ */
+void nic_dma_unlock_packet(packet_t *packet)
+{
+	size_t unlocked;
+	int rc = dma_unlock(packet, 1, &unlocked);
+	if (rc != EOK)
+		return;
+	
+	assert(unlocked == 1);
+}
+
+#endif
+
+/** @}
+ */
Index: uspace/lib/nic/src/nic_impl.c
===================================================================
--- uspace/lib/nic/src/nic_impl.c	(revision 00d7e1bfe06501df61aed156666d5597397d910a)
+++ uspace/lib/nic/src/nic_impl.c	(revision 00d7e1bfe06501df61aed156666d5597397d910a)
@@ -0,0 +1,878 @@
+/*
+ * Copyright (c) 2011 Radim Vansa
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * - Redistributions of source code must retain the above copyright
+ *   notice, this list of conditions and the following disclaimer.
+ * - Redistributions in binary form must reproduce the above copyright
+ *   notice, this list of conditions and the following disclaimer in the
+ *   documentation and/or other materials provided with the distribution.
+ * - The name of the author may not be used to endorse or promote products
+ *   derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+/**
+ * @addtogroup libnic
+ * @{
+ */
+/**
+ * @file
+ * @brief Default DDF NIC interface methods implementations
+ */
+
+#include <str_error.h>
+#include <ipc/services.h>
+#include <ns.h>
+#include <packet_client.h>
+#include <packet_remote.h>
+#include "nic_driver.h"
+#include "nic_impl.h"
+
+/**
+ * Default implementation of the set_state method. Trivial.
+ *
+ * @param		fun
+ * @param[out]	state
+ *
+ * @return EOK always.
+ */
+int nic_get_state_impl(ddf_fun_t *fun, nic_device_state_t *state)
+{
+	nic_t *nic_data = (nic_t *) fun->driver_data;
+	fibril_rwlock_read_lock(&nic_data->main_lock);
+	*state = nic_data->state;
+	fibril_rwlock_read_unlock(&nic_data->main_lock);
+	return EOK;
+}
+
+/**
+ * Default implementation of the set_state method. Changes the internal
+ * driver's state, calls the appropriate callback and notifies the NIL service
+ * about this change.
+ *
+ * @param	fun
+ * @param	state	The new device's state
+ *
+ * @return EOK		If the state was changed
+ * @return EINVAL	If the state cannot be changed
+ */
+int nic_set_state_impl(ddf_fun_t *fun, nic_device_state_t state)
+{
+	if (state >= NIC_STATE_MAX) {
+		return EINVAL;
+	}
+
+	nic_t *nic_data = (nic_t *) fun->driver_data;
+
+	fibril_rwlock_write_lock(&nic_data->main_lock);
+	if (nic_data->state == state) {
+		/* No change, nothing to do */
+		fibril_rwlock_write_unlock(&nic_data->main_lock);
+		return EOK;
+	}
+	if (state == NIC_STATE_ACTIVE) {
+		if (nic_data->nil_session == NULL || nic_data->net_session == NULL
+		    || nic_data->device_id < 0) {
+			fibril_rwlock_write_unlock(&nic_data->main_lock);
+			return EINVAL;
+		}
+	}
+
+	state_change_handler event_handler = NULL;
+	switch (state) {
+	case NIC_STATE_STOPPED:
+		event_handler = nic_data->on_stopping;
+		break;
+	case NIC_STATE_DOWN:
+		event_handler = nic_data->on_going_down;
+		break;
+	case NIC_STATE_ACTIVE:
+		event_handler = nic_data->on_activating;
+		break;
+	default:
+		break;
+	}
+	if (event_handler != NULL) {
+		int rc = event_handler(nic_data);
+		if (rc != EOK) {
+			fibril_rwlock_write_unlock(&nic_data->main_lock);
+			return EINVAL;
+		}
+	}
+
+	if (state == NIC_STATE_STOPPED) {
+		/* Notify upper layers that we are reseting the MAC */
+		int rc = nil_addr_changed_msg(nic_data->nil_session,
+			nic_data->device_id, &nic_data->default_mac);
+		nic_data->poll_mode = nic_data->default_poll_mode;
+		memcpy(&nic_data->poll_period, &nic_data->default_poll_period,
+			sizeof (struct timeval));
+		if (rc != EOK) {
+			/* We have already ran the on stopped handler, even if we
+			 * terminated the state change we would end up in undefined state.
+			 * Therefore we just log the problem. */
+		}
+
+		fibril_rwlock_write_lock(&nic_data->stats_lock);
+		bzero(&nic_data->stats, sizeof (nic_device_stats_t));
+		fibril_rwlock_write_unlock(&nic_data->stats_lock);
+
+		fibril_rwlock_write_lock(&nic_data->rxc_lock);
+		nic_rxc_clear(&nic_data->rx_control);
+		/* Reinsert device's default MAC */
+		nic_rxc_set_addr(&nic_data->rx_control, NULL,
+			&nic_data->default_mac);
+		fibril_rwlock_write_unlock(&nic_data->rxc_lock);
+		memcpy(&nic_data->mac, &nic_data->default_mac, sizeof (nic_address_t));
+
+		fibril_rwlock_write_lock(&nic_data->wv_lock);
+		nic_wol_virtues_clear(&nic_data->wol_virtues);
+		fibril_rwlock_write_unlock(&nic_data->wv_lock);
+
+		/* Ensure stopping period of NIC_POLL_SOFTWARE_PERIODIC */
+		nic_sw_period_stop(nic_data);
+	}
+
+	nic_data->state = state;
+
+	nil_device_state_msg(nic_data->nil_session, nic_data->device_id, state);
+
+	fibril_rwlock_write_unlock(&nic_data->main_lock);
+
+	return EOK;
+}
+
+/**
+ * Default implementation of the send_message method.
+ * Send messages to the network.
+ *
+ * @param	fun
+ * @param	packet_id	ID of the first packet in a queue of sent packets
+ *
+ * @return EOK		If the message was sent
+ * @return EBUSY	If the device is not in state when the packet can be set.
+ * @return EINVAL	If the packet ID is invalid
+ */
+int nic_send_message_impl(ddf_fun_t *fun, packet_id_t packet_id)
+{
+	nic_t *nic_data = (nic_t *) fun->driver_data;
+	packet_t *packet, *next;
+
+	fibril_rwlock_read_lock(&nic_data->main_lock);
+	if (nic_data->state != NIC_STATE_ACTIVE || nic_data->tx_busy) {
+		fibril_rwlock_read_unlock(&nic_data->main_lock);
+		pq_release_remote(nic_data->net_session, packet_id);
+		return EBUSY;
+	}
+
+	int rc = packet_translate_remote(nic_data->net_session, &packet, packet_id);
+
+	if (rc != EOK) {
+		fibril_rwlock_read_unlock(&nic_data->main_lock);
+		return EINVAL;
+	}
+
+	/*
+	 * Process the packet queue. Each sent packet must be detached from the
+	 * queue and destroyed. This is why the cycle differs from loopback's
+	 * cycle, where the packets are immediately used in upper layers and
+	 * therefore they must not be destroyed (released).
+	 */
+	assert(nic_data->write_packet != NULL);
+	do {
+		next = pq_detach(packet);
+		nic_data->write_packet(nic_data, packet);
+		packet = next;
+	} while (packet);
+	fibril_rwlock_read_unlock(&nic_data->main_lock);
+	return EOK;
+}
+
+/**
+ * Default implementation of the connect_to_nil method.
+ * Connects the driver to the NIL service.
+ *
+ * @param	fun
+ * @param	nil_service	ID of the server implementing the NIL service
+ * @param	device_id	ID of the device as used in higher layers
+ *
+ * @return EOK		If the services were bound
+ * @return 			Negative error code from service_connect_blocking
+ */
+int nic_connect_to_nil_impl(ddf_fun_t *fun, services_t nil_service,
+    nic_device_id_t device_id)
+{
+	nic_t *nic_data = (nic_t *) fun->driver_data;
+	fibril_rwlock_write_lock(&nic_data->main_lock);
+	
+	nic_data->device_id = device_id;
+	
+	nic_data->nil_session = service_connect_blocking(EXCHANGE_SERIALIZE,
+	    nil_service, 0, 0);
+	if (nic_data->nil_session != NULL) {
+		fibril_rwlock_write_unlock(&nic_data->main_lock);
+		return EOK;
+	}
+	
+	fibril_rwlock_write_unlock(&nic_data->main_lock);
+	return EHANGUP;
+}
+
+/**
+ * Default implementation of the get_address method.
+ * Retrieves the NIC's physical address.
+ *
+ * @param	fun
+ * @param	address	Pointer to the structure where the address will be stored.
+ *
+ * @return EOK		If the services were bound
+ * @return ELIMIT	If the buffer is too short
+ */
+int nic_get_address_impl(ddf_fun_t *fun, nic_address_t *address)
+{
+	assert(address);
+	nic_t *nic_data = (nic_t *) fun->driver_data;
+	fibril_rwlock_read_lock(&nic_data->main_lock);
+	memcpy(address, &nic_data->mac, sizeof (nic_address_t));
+	fibril_rwlock_read_unlock(&nic_data->main_lock);
+	return EOK;
+}
+
+/**
+ * Default implementation of the get_stats method. Copies the statistics from
+ * the drivers data to supplied buffer.
+ *
+ * @param		fun
+ * @param[out]	stats	The buffer for statistics
+ *
+ * @return EOK (cannot fail)
+ */
+int nic_get_stats_impl(ddf_fun_t *fun, nic_device_stats_t *stats)
+{
+	nic_t *nic_data = (nic_t *) fun->driver_data;
+	assert (stats != NULL);
+	fibril_rwlock_read_lock(&nic_data->stats_lock);
+	memcpy(stats, &nic_data->stats, sizeof (nic_device_stats_t));
+	fibril_rwlock_read_unlock(&nic_data->stats_lock);
+	return EOK;
+}
+
+/**
+ * Default implementation of unicast_get_mode method.
+ *
+ * @param		fun
+ * @param[out]	mode		Current operation mode
+ * @param[in]	max_count	Max number of addresses that can be written into the
+ * 							buffer (addr_list).
+ * @param[out]	addr_list	Buffer for addresses
+ * @param[out]	addr_count	Number of addresses written into the list
+ *
+ * @return EOK
+ */
+int nic_unicast_get_mode_impl(ddf_fun_t *fun, nic_unicast_mode_t *mode,
+	size_t max_count, nic_address_t *addr_list, size_t *addr_count)
+{
+	nic_t *nic_data = (nic_t *) fun->driver_data;
+	fibril_rwlock_read_lock(&nic_data->rxc_lock);
+	nic_rxc_unicast_get_mode(&nic_data->rx_control, mode, max_count,
+		addr_list, addr_count);
+	fibril_rwlock_read_unlock(&nic_data->rxc_lock);
+	return EOK;
+}
+
+/**
+ * Default implementation of unicast_set_mode method.
+ *
+ * @param		fun
+ * @param[in]	mode		New operation mode
+ * @param[in]	addr_list	List of unicast addresses
+ * @param[in]	addr_count	Number of addresses in the list
+ *
+ * @return EOK
+ * @return EINVAL
+ * @return ENOTSUP
+ * @return ENOMEM
+ */
+int nic_unicast_set_mode_impl(ddf_fun_t *fun,
+	nic_unicast_mode_t mode, const nic_address_t *addr_list, size_t addr_count)
+{
+	assert((addr_count == 0 && addr_list == NULL)
+		|| (addr_count != 0 && addr_list != NULL));
+	size_t i;
+	for (i = 0; i < addr_count; ++i) {
+		if (addr_list[i].address[0] & 1)
+			return EINVAL;
+	}
+
+	nic_t *nic_data = (nic_t *) fun->driver_data;
+	fibril_rwlock_write_lock(&nic_data->rxc_lock);
+	int rc = ENOTSUP;
+	if (nic_data->on_unicast_mode_change) {
+		rc = nic_data->on_unicast_mode_change(nic_data,
+			mode, addr_list, addr_count);
+	}
+	if (rc == EOK) {
+		rc = nic_rxc_unicast_set_mode(&nic_data->rx_control, mode,
+			addr_list, addr_count);
+		/* After changing the mode the addr db gets cleared, therefore we have
+		 * to reinsert also the physical address of NIC.
+		 */
+		nic_rxc_set_addr(&nic_data->rx_control, NULL, &nic_data->mac);
+	}
+	fibril_rwlock_write_unlock(&nic_data->rxc_lock);
+	return rc;
+}
+
+
+/**
+ * Default implementation of multicast_get_mode method.
+ *
+ * @param		fun
+ * @param[out]	mode		Current operation mode
+ * @param[in]	max_count	Max number of addresses that can be written into the
+ * 							buffer (addr_list).
+ * @param[out]	addr_list	Buffer for addresses
+ * @param[out]	addr_count	Number of addresses written into the list
+ *
+ * @return EOK
+ */
+int nic_multicast_get_mode_impl(ddf_fun_t *fun, nic_multicast_mode_t *mode,
+	size_t max_count, nic_address_t *addr_list, size_t *addr_count)
+{
+	nic_t *nic_data = (nic_t *) fun->driver_data;
+	fibril_rwlock_read_lock(&nic_data->rxc_lock);
+	nic_rxc_multicast_get_mode(&nic_data->rx_control, mode, max_count,
+		addr_list, addr_count);
+	fibril_rwlock_read_unlock(&nic_data->rxc_lock);
+	return EOK;
+}
+
+/**
+ * Default implementation of multicast_set_mode method.
+ *
+ * @param		fun
+ * @param[in]	mode		New operation mode
+ * @param[in]	addr_list	List of multicast addresses
+ * @param[in]	addr_count	Number of addresses in the list
+ *
+ * @return EOK
+ * @return EINVAL
+ * @return ENOTSUP
+ * @return ENOMEM
+ */
+int nic_multicast_set_mode_impl(ddf_fun_t *fun,	nic_multicast_mode_t mode,
+	const nic_address_t *addr_list, size_t addr_count)
+{
+	assert((addr_count == 0 && addr_list == NULL)
+		|| (addr_count != 0 && addr_list != NULL));
+	size_t i;
+	for (i = 0; i < addr_count; ++i) {
+		if (!(addr_list[i].address[0] & 1))
+			return EINVAL;
+	}
+
+	nic_t *nic_data = (nic_t *) fun->dev->driver_data;
+	fibril_rwlock_write_lock(&nic_data->rxc_lock);
+	int rc = ENOTSUP;
+	if (nic_data->on_multicast_mode_change) {
+		rc = nic_data->on_multicast_mode_change(nic_data, mode, addr_list, addr_count);
+	}
+	if (rc == EOK) {
+		rc = nic_rxc_multicast_set_mode(&nic_data->rx_control, mode,
+			addr_list, addr_count);
+	}
+	fibril_rwlock_write_unlock(&nic_data->rxc_lock);
+	return rc;
+}
+
+/**
+ * Default implementation of broadcast_get_mode method.
+ *
+ * @param		fun
+ * @param[out]	mode		Current operation mode
+ *
+ * @return EOK
+ */
+int nic_broadcast_get_mode_impl(ddf_fun_t *fun, nic_broadcast_mode_t *mode)
+{
+	nic_t *nic_data = (nic_t *) fun->driver_data;
+	fibril_rwlock_read_lock(&nic_data->rxc_lock);
+	nic_rxc_broadcast_get_mode(&nic_data->rx_control, mode);
+	fibril_rwlock_read_unlock(&nic_data->rxc_lock);
+	return EOK;
+}
+
+/**
+ * Default implementation of broadcast_set_mode method.
+ *
+ * @param		fun
+ * @param[in]	mode		New operation mode
+ *
+ * @return EOK
+ * @return EINVAL
+ * @return ENOTSUP
+ * @return ENOMEM
+ */
+int nic_broadcast_set_mode_impl(ddf_fun_t *fun, nic_broadcast_mode_t mode)
+{
+	nic_t *nic_data = (nic_t *) fun->driver_data;
+	fibril_rwlock_write_lock(&nic_data->rxc_lock);
+	int rc = ENOTSUP;
+	if (nic_data->on_broadcast_mode_change) {
+		rc = nic_data->on_broadcast_mode_change(nic_data, mode);
+	}
+	if (rc == EOK) {
+		rc = nic_rxc_broadcast_set_mode(&nic_data->rx_control, mode);
+	}
+	fibril_rwlock_write_unlock(&nic_data->rxc_lock);
+	return rc;
+}
+
+/**
+ * Default implementation of blocked_sources_get method.
+ *
+ * @param		fun
+ * @param[in]	max_count	Max number of addresses that can be written into the
+ * 							buffer (addr_list).
+ * @param[out]	addr_list	Buffer for addresses
+ * @param[out]	addr_count	Number of addresses written into the list
+ *
+ * @return EOK
+ */
+int nic_blocked_sources_get_impl(ddf_fun_t *fun,
+	size_t max_count, nic_address_t *addr_list, size_t *addr_count)
+{
+	nic_t *nic_data = (nic_t *) fun->driver_data;
+	fibril_rwlock_read_lock(&nic_data->rxc_lock);
+	nic_rxc_blocked_sources_get(&nic_data->rx_control,
+		max_count, addr_list, addr_count);
+	fibril_rwlock_read_unlock(&nic_data->rxc_lock);
+	return EOK;
+}
+
+/**
+ * Default implementation of blocked_sources_set method.
+ *
+ * @param		fun
+ * @param[in]	addr_list	List of blocked addresses
+ * @param[in]	addr_count	Number of addresses in the list
+ *
+ * @return EOK
+ * @return EINVAL
+ * @return ENOTSUP
+ * @return ENOMEM
+ */
+int nic_blocked_sources_set_impl(ddf_fun_t *fun,
+	const nic_address_t *addr_list, size_t addr_count)
+{
+	nic_t *nic_data = (nic_t *) fun->driver_data;
+	fibril_rwlock_write_lock(&nic_data->rxc_lock);
+	if (nic_data->on_blocked_sources_change) {
+		nic_data->on_blocked_sources_change(nic_data, addr_list, addr_count);
+	}
+	int rc = nic_rxc_blocked_sources_set(&nic_data->rx_control,
+		addr_list, addr_count);
+	fibril_rwlock_write_unlock(&nic_data->rxc_lock);
+	return rc;
+}
+
+/**
+ * Default implementation of vlan_get_mask method.
+ *
+ * @param		fun
+ * @param[out]	mask	Current VLAN mask
+ *
+ * @return EOK
+ * @return ENOENT	If the mask is not set
+ */
+int nic_vlan_get_mask_impl(ddf_fun_t *fun, nic_vlan_mask_t *mask)
+{
+	nic_t *nic_data = (nic_t *) fun->driver_data;
+	fibril_rwlock_read_lock(&nic_data->rxc_lock);
+	int rc = nic_rxc_vlan_get_mask(&nic_data->rx_control, mask);
+	fibril_rwlock_read_unlock(&nic_data->rxc_lock);
+	return rc;
+}
+
+/**
+ * Default implementation of vlan_set_mask method.
+ *
+ * @param		fun
+ * @param[in]	mask	The new VLAN mask
+ *
+ * @return EOK
+ * @return ENOMEM
+ */
+int nic_vlan_set_mask_impl(ddf_fun_t *fun, const nic_vlan_mask_t *mask)
+{
+	nic_t *nic_data = (nic_t *) fun->driver_data;
+	fibril_rwlock_write_lock(&nic_data->rxc_lock);
+	int rc = nic_rxc_vlan_set_mask(&nic_data->rx_control, mask);
+	if (rc == EOK && nic_data->on_vlan_mask_change) {
+		nic_data->on_vlan_mask_change(nic_data, mask);
+	}
+	fibril_rwlock_write_unlock(&nic_data->rxc_lock);
+	return rc;
+}
+
+/**
+ * Default implementation of the wol_virtue_add method.
+ * Create a new WOL virtue.
+ *
+ * @param[in]	fun
+ * @param[in]	type		Type of the virtue
+ * @param[in]	data		Data required for this virtue (depends on type)
+ * @param[in]	length		Length of the data
+ * @param[out]	filter		Identifier of the new virtue
+ *
+ * @return EOK		If the operation was successfully completed
+ * @return EINVAL	If virtue type is not supported or the data are invalid
+ * @return ELIMIT	If the driver does not allow to create more virtues
+ * @return ENOMEM	If there was not enough memory to complete the operation
+ */
+int nic_wol_virtue_add_impl(ddf_fun_t *fun, nic_wv_type_t type,
+	const void *data, size_t length, nic_wv_id_t *new_id)
+{
+	nic_t *nic_data = (nic_t *) fun->driver_data;
+	if (nic_data->on_wol_virtue_add == NULL
+		|| nic_data->on_wol_virtue_remove == NULL) {
+		return ENOTSUP;
+	}
+	if (type == NIC_WV_NONE || type >= NIC_WV_MAX) {
+		return EINVAL;
+	}
+	if (nic_wol_virtues_verify(type, data, length) != EOK) {
+		return EINVAL;
+	}
+	nic_wol_virtue_t *virtue = malloc(sizeof (nic_wol_virtue_t));
+	if (virtue == NULL) {
+		return ENOMEM;
+	}
+	bzero(virtue, sizeof (nic_wol_virtue_t));
+	if (length != 0) {
+		virtue->data = malloc(length);
+		if (virtue->data == NULL) {
+			free(virtue);
+			return ENOMEM;
+		}
+		memcpy((void *) virtue->data, data, length);
+	}
+	virtue->type = type;
+	virtue->length = length;
+
+	fibril_rwlock_write_lock(&nic_data->wv_lock);
+	/* Check if we haven't reached the maximum */
+	if (nic_data->wol_virtues.caps_max[type] < 0) {
+		fibril_rwlock_write_unlock(&nic_data->wv_lock);
+		return EINVAL;
+	}
+	if ((int) nic_data->wol_virtues.lists_sizes[type] >=
+		nic_data->wol_virtues.caps_max[type]) {
+		fibril_rwlock_write_unlock(&nic_data->wv_lock);
+		return ELIMIT;
+	}
+	/* Call the user-defined add callback */
+	int rc = nic_data->on_wol_virtue_add(nic_data, virtue);
+	if (rc != EOK) {
+		free(virtue->data);
+		free(virtue);
+		fibril_rwlock_write_unlock(&nic_data->wv_lock);
+		return rc;
+	}
+	rc = nic_wol_virtues_add(&nic_data->wol_virtues, virtue);
+	if (rc != EOK) {
+		/* If the adding fails, call user-defined remove callback */
+		nic_data->on_wol_virtue_remove(nic_data, virtue);
+		fibril_rwlock_write_unlock(&nic_data->wv_lock);
+		free(virtue->data);
+		free(virtue);
+		return rc;
+	} else {
+		*new_id = virtue->id;
+		fibril_rwlock_write_unlock(&nic_data->wv_lock);
+	}
+	return EOK;
+}
+
+/**
+ * Default implementation of the wol_virtue_remove method.
+ * Destroys the WOL virtue.
+ *
+ * @param[in] fun
+ * @param[in] id	WOL virtue identification
+ *
+ * @return EOK		If the operation was successfully completed
+ * @return ENOTSUP	If the function is not supported by the driver or device
+ * @return ENOENT	If the virtue identifier is not valid.
+ */
+int nic_wol_virtue_remove_impl(ddf_fun_t *fun, nic_wv_id_t id)
+{
+	nic_t *nic_data = (nic_t *) fun->driver_data;
+	if (nic_data->on_wol_virtue_add == NULL
+		|| nic_data->on_wol_virtue_remove == NULL) {
+		return ENOTSUP;
+	}
+	fibril_rwlock_write_lock(&nic_data->wv_lock);
+	nic_wol_virtue_t *virtue =
+		nic_wol_virtues_remove(&nic_data->wol_virtues, id);
+	if (virtue == NULL) {
+		fibril_rwlock_write_unlock(&nic_data->wv_lock);
+		return ENOENT;
+	}
+	/* The event handler is called after the filter was removed */
+	nic_data->on_wol_virtue_remove(nic_data, virtue);
+	fibril_rwlock_write_unlock(&nic_data->wv_lock);
+	free(virtue->data);
+	free(virtue);
+	return EOK;
+}
+
+/**
+ * Default implementation of the wol_virtue_probe method.
+ * Queries the type and data of the virtue.
+ *
+ * @param[in]	fun
+ * @param[in]	id		Virtue identifier
+ * @param[out]	type		Type of the virtue. Can be NULL.
+ * @param[out]	data		Data used when the virtue was created. Can be NULL.
+ * @param[out]	length		Length of the data. Can be NULL.
+ *
+ * @return EOK		If the operation was successfully completed
+ * @return ENOENT	If the virtue identifier is not valid.
+ * @return ENOMEM	If there was not enough memory to complete the operation
+ */
+int nic_wol_virtue_probe_impl(ddf_fun_t *fun, nic_wv_id_t id,
+	nic_wv_type_t *type, size_t max_length, void *data, size_t *length)
+{
+	nic_t *nic_data = (nic_t *) fun->driver_data;
+	fibril_rwlock_read_lock(&nic_data->wv_lock);
+	const nic_wol_virtue_t *virtue =
+			nic_wol_virtues_find(&nic_data->wol_virtues, id);
+	if (virtue == NULL) {
+		*type = NIC_WV_NONE;
+		*length = 0;
+		fibril_rwlock_read_unlock(&nic_data->wv_lock);
+		return ENOENT;
+	} else {
+		*type = virtue->type;
+		if (max_length > virtue->length) {
+			max_length = virtue->length;
+		}
+		memcpy(data, virtue->data, max_length);
+		*length = virtue->length;
+		fibril_rwlock_read_unlock(&nic_data->wv_lock);
+		return EOK;
+	}
+}
+
+/**
+ * Default implementation of the wol_virtue_list method.
+ * List filters of the specified type. If NIC_WV_NONE is the type, it lists all
+ * filters.
+ *
+ * @param[in]	fun
+ * @param[in]	type	Type of the virtues
+ * @param[out]	virtues	Vector of virtue ID's.
+ * @param[out]	count	Length of the data. Can be NULL.
+ *
+ * @return EOK		If the operation was successfully completed
+ * @return ENOENT	If the filter identification is not valid.
+ * @return ENOMEM	If there was not enough memory to complete the operation
+ */
+int nic_wol_virtue_list_impl(ddf_fun_t *fun, nic_wv_type_t type,
+	size_t max_count, nic_wv_id_t *id_list, size_t *id_count)
+{
+	nic_t *nic_data = (nic_t *) fun->driver_data;
+	fibril_rwlock_read_lock(&nic_data->wv_lock);
+	int rc = nic_wol_virtues_list(&nic_data->wol_virtues, type,
+		max_count, id_list, id_count);
+	fibril_rwlock_read_unlock(&nic_data->wv_lock);
+	return rc;
+}
+
+/**
+ * Default implementation of the wol_virtue_get_caps method.
+ * Queries for the current capabilities for some type of filter.
+ *
+ * @param[in]	fun
+ * @param[in]	type	Type of the virtues
+ * @param[out]	count	Number of virtues of this type that can be currently set
+ *
+ * @return EOK		If the operation was successfully completed
+  */
+int nic_wol_virtue_get_caps_impl(ddf_fun_t *fun, nic_wv_type_t type, int *count)
+{
+	nic_t *nic_data = (nic_t *) fun->driver_data;
+	fibril_rwlock_read_lock(&nic_data->wv_lock);
+	*count = nic_data->wol_virtues.caps_max[type]
+	    - (int) nic_data->wol_virtues.lists_sizes[type];
+	fibril_rwlock_read_unlock(&nic_data->wv_lock);
+	return EOK;
+}
+
+/**
+ * Default implementation of the poll_get_mode method.
+ * Queries the current interrupt/poll mode of the NIC
+ *
+ * @param[in]	fun
+ * @param[out]	mode		Current poll mode
+ * @param[out]	period		Period used in periodic polling. Can be NULL.
+ *
+ * @return EOK		If the operation was successfully completed
+ * @return ENOTSUP	This function is not supported.
+ * @return EPARTY	Error in communication protocol
+ */
+int nic_poll_get_mode_impl(ddf_fun_t *fun,
+	nic_poll_mode_t *mode, struct timeval *period)
+{
+	nic_t *nic_data = (nic_t *) fun->driver_data;
+	fibril_rwlock_read_lock(&nic_data->main_lock);
+	*mode = nic_data->poll_mode;
+	memcpy(period, &nic_data->poll_period, sizeof (struct timeval));
+	fibril_rwlock_read_unlock(&nic_data->main_lock);
+	return EOK;
+}
+
+/**
+ * Default implementation of the poll_set_mode_impl method.
+ * Sets the interrupt/poll mode of the NIC.
+ *
+ * @param[in]	fun
+ * @param[in]	mode		The new poll mode
+ * @param[in]	period		Period used in periodic polling. Can be NULL.
+ *
+ * @return EOK		If the operation was successfully completed
+ * @return ENOTSUP	This operation is not supported.
+ * @return EPARTY	Error in communication protocol
+ */
+int nic_poll_set_mode_impl(ddf_fun_t *fun,
+	nic_poll_mode_t mode, const struct timeval *period)
+{
+	nic_t *nic_data = (nic_t *) fun->driver_data;
+	/* If the driver does not implement the poll mode change handler it cannot
+	 * switch off interrupts and this is not supported. */
+	if (nic_data->on_poll_mode_change == NULL)
+		return ENOTSUP;
+
+	if ((mode == NIC_POLL_ON_DEMAND) && nic_data->on_poll_request == NULL)
+		return ENOTSUP;
+
+	if (mode == NIC_POLL_PERIODIC || mode == NIC_POLL_SOFTWARE_PERIODIC) {
+		if (period == NULL)
+			return EINVAL;
+		if (period->tv_sec == 0 && period->tv_usec == 0)
+			return EINVAL;
+		if (period->tv_sec < 0 || period->tv_usec < 0)
+			return EINVAL;
+	}
+	fibril_rwlock_write_lock(&nic_data->main_lock);
+	int rc = nic_data->on_poll_mode_change(nic_data, mode, period);
+	assert(rc == EOK || rc == ENOTSUP || rc == EINVAL);
+	if (rc == ENOTSUP && (nic_data->on_poll_request != NULL) && 
+	    (mode == NIC_POLL_PERIODIC || mode == NIC_POLL_SOFTWARE_PERIODIC) ) {
+
+		rc = nic_data->on_poll_mode_change(nic_data, NIC_POLL_ON_DEMAND, NULL);
+		assert(rc == EOK || rc == ENOTSUP);
+		if (rc == EOK) 
+			nic_sw_period_start(nic_data);
+	}
+	if (rc == EOK) {
+		nic_data->poll_mode = mode;
+		if (period)
+			nic_data->poll_period = *period;
+	}
+	fibril_rwlock_write_unlock(&nic_data->main_lock);
+	return rc;
+}
+
+/**
+ * Default implementation of the poll_now method.
+ * Wrapper for the actual poll implementation.
+ *
+ * @param[in]	fun
+ *
+ * @return EOK		If the NIC was polled
+ * @return ENOTSUP	If the function is not supported
+ * @return EINVAL	If the NIC is not in state where it allows on demand polling
+ */
+int nic_poll_now_impl(ddf_fun_t *fun) {
+	nic_t *nic_data = (nic_t *) fun->driver_data;
+	fibril_rwlock_read_lock(&nic_data->main_lock);
+	if (nic_data->poll_mode != NIC_POLL_ON_DEMAND) {
+		fibril_rwlock_read_unlock(&nic_data->main_lock);
+		return EINVAL;
+	}
+	if (nic_data->on_poll_request != NULL) {
+		nic_data->on_poll_request(nic_data);
+		fibril_rwlock_read_unlock(&nic_data->main_lock);
+		return EOK;
+	} else {
+		fibril_rwlock_read_unlock(&nic_data->main_lock);
+		return ENOTSUP;
+	}
+}
+
+/** Default implementation of the device_added method
+ *
+ * Just calls nic_ready.
+ *
+ * @param dev
+ *
+ */
+void nic_device_added_impl(ddf_dev_t *dev)
+{
+	nic_ready((nic_t *) dev->driver_data);
+}
+
+/**
+ * Default handler for unknown methods (outside of the NIC interface).
+ * Logs a warning message and returns ENOTSUP to the caller.
+ *
+ * @param fun		The DDF function where the method should be called.
+ * @param callid	IPC call identifier
+ * @param call		IPC call data
+ */
+void nic_default_handler_impl(ddf_fun_t *fun, ipc_callid_t callid,
+    ipc_call_t *call)
+{
+	async_answer_0(callid, ENOTSUP);
+}
+
+/**
+ * Default (empty) OPEN function implementation.
+ *
+ * @param fun	The DDF function
+ *
+ * @return EOK always.
+ */
+int nic_open_impl(ddf_fun_t *fun)
+{
+	return EOK;
+}
+
+/**
+ * Default (empty) OPEN function implementation.
+ *
+ * @param fun	The DDF function
+ */
+void nic_close_impl(ddf_fun_t *fun)
+{
+}
+
+/** @}
+ */
Index: uspace/lib/nic/src/nic_rx_control.c
===================================================================
--- uspace/lib/nic/src/nic_rx_control.c	(revision 00d7e1bfe06501df61aed156666d5597397d910a)
+++ uspace/lib/nic/src/nic_rx_control.c	(revision 00d7e1bfe06501df61aed156666d5597397d910a)
@@ -0,0 +1,539 @@
+/*
+ * Copyright (c) 2011 Radim Vansa
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * - Redistributions of source code must retain the above copyright
+ *   notice, this list of conditions and the following disclaimer.
+ * - Redistributions in binary form must reproduce the above copyright
+ *   notice, this list of conditions and the following disclaimer in the
+ *   documentation and/or other materials provided with the distribution.
+ * - The name of the author may not be used to endorse or promote products
+ *   derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+/**
+ * @addtogroup libnic
+ * @{
+ */
+/**
+ * @file
+ * @brief Incoming packets (frames) filtering functions
+ */
+
+#include <assert.h>
+#include <stdlib.h>
+#include <bool.h>
+#include <errno.h>
+#include <net/device.h>
+#include <net_checksum.h>
+#include <packet_client.h>
+#include "nic_rx_control.h"
+
+/**
+ * Initializes the receive control structure
+ *
+ * @param rxc
+ *
+ * @return EOK		On success
+ * @return ENOMEM	On not enough memory
+ * @return EINVAL	Internal error, should not happen
+ */
+int nic_rxc_init(nic_rxc_t *rxc)
+{
+	bzero(rxc, sizeof (nic_rxc_t));
+	int rc;
+	rc = nic_addr_db_init(&rxc->blocked_sources, ETH_ADDR);
+	if (rc != EOK) {
+		return rc;
+	}
+	rc = nic_addr_db_init(&rxc->unicast_addrs, ETH_ADDR);
+	if (rc != EOK) {
+		return rc;
+	}
+	rc = nic_addr_db_init(&rxc->multicast_addrs, ETH_ADDR);
+	if (rc != EOK) {
+		return rc;
+	}
+	rxc->block_sources = false;
+	rxc->unicast_mode = NIC_UNICAST_DEFAULT;
+	rxc->multicast_mode = NIC_MULTICAST_BLOCKED;
+	rxc->broadcast_mode = NIC_BROADCAST_ACCEPTED;
+
+	/* Default NIC behavior */
+	rxc->unicast_exact = true;
+	rxc->multicast_exact = false;
+	rxc->vlan_exact = true;
+	return EOK;
+}
+
+/** Reinitialize the structure.
+ *
+ * @param filters
+ */
+int nic_rxc_clear(nic_rxc_t *rxc)
+{
+	nic_addr_db_destroy(&rxc->unicast_addrs);
+	nic_addr_db_destroy(&rxc->multicast_addrs);
+	nic_addr_db_destroy(&rxc->blocked_sources);
+	return nic_rxc_init(rxc);
+}
+
+/** Set the NIC's address that should be used as the default address during
+ * the checks.
+ *
+ * @param rxc
+ * @param prev_addr Previously used default address. Can be NULL
+ *                  if this is the first call after filters' initialization.
+ * @param curr_addr The new default address.
+ *
+ * @return EOK On success
+ *
+ */
+int nic_rxc_set_addr(nic_rxc_t *rxc, const nic_address_t *prev_addr,
+    const nic_address_t *curr_addr)
+{
+	if (prev_addr != NULL) {
+		int rc = nic_addr_db_remove(&rxc->unicast_addrs,
+		    (const uint8_t *) &prev_addr->address);
+		if (rc != EOK)
+			return rc;
+	}
+	
+	return nic_addr_db_insert(&rxc->unicast_addrs,
+	    (const uint8_t *) &curr_addr->address);
+}
+
+/* Helper structure */
+typedef struct {
+	size_t max_count;
+	nic_address_t *address_list;
+	size_t address_count;
+} nic_rxc_add_addr_t;
+
+/** Helper function */
+static void nic_rxc_add_addr(const uint8_t *addr, void *arg)
+{
+	nic_rxc_add_addr_t *hs = (nic_rxc_add_addr_t *) arg;
+	if (hs->address_count < hs->max_count && hs->address_list != NULL) {
+		memcpy(&hs->address_list[hs->address_count].address, addr, ETH_ADDR);
+	}
+	hs->address_count++;
+}
+
+/**
+ * Queries the current mode of unicast frames receiving.
+ *
+ * @param rxc
+ * @param mode			The new unicast mode
+ * @param max_count		Max number of addresses that can be written into the
+ * 						address_list.
+ * @param address_list	List of MAC addresses or NULL.
+ * @param address_count Number of accepted addresses (can be > max_count)
+ */
+void nic_rxc_unicast_get_mode(const nic_rxc_t *rxc, nic_unicast_mode_t *mode,
+	size_t max_count, nic_address_t *address_list, size_t *address_count)
+{
+	*mode = rxc->unicast_mode;
+	if (rxc->unicast_mode == NIC_UNICAST_LIST) {
+		nic_rxc_add_addr_t hs = {
+			.max_count = max_count,
+			.address_list = address_list,
+			.address_count = 0
+		};
+		nic_addr_db_foreach(&rxc->unicast_addrs, nic_rxc_add_addr, &hs);
+		if (address_count) {
+			*address_count = hs.address_count;
+		}
+	}
+}
+
+/**
+ * Sets the current mode of unicast frames receiving.
+ *
+ * @param rxc
+ * @param mode			The current unicast mode
+ * @param address_list	List of MAC addresses or NULL.
+ * @param address_count Number of addresses in the list
+ *
+ * @return EOK		On success
+ * @return EINVAL	If any of the MAC addresses is not a unicast address.
+ * @return ENOMEM	If there was not enough memory
+ */
+int nic_rxc_unicast_set_mode(nic_rxc_t *rxc, nic_unicast_mode_t mode,
+	const nic_address_t *address_list, size_t address_count)
+{
+	if (mode == NIC_UNICAST_LIST && address_list == NULL) {
+		return EINVAL;
+	} else if (mode != NIC_UNICAST_LIST && address_list != NULL) {
+		return EINVAL;
+	}
+
+	if (rxc->unicast_mode == NIC_UNICAST_LIST) {
+		nic_addr_db_clear(&rxc->unicast_addrs);
+	}
+	rxc->unicast_mode = mode;
+	size_t i;
+	for (i = 0; i < address_count; ++i) {
+		int rc = nic_addr_db_insert(&rxc->unicast_addrs,
+			(const uint8_t *) &address_list[i].address);
+		if (rc == ENOMEM) {
+			return ENOMEM;
+		}
+	}
+	return EOK;
+}
+
+/**
+ * Queries the current mode of multicast frames receiving.
+ *
+ * @param rxc
+ * @param mode			The current multicast mode
+ * @param max_count		Max number of addresses that can be written into the
+ * 						address_list.
+ * @param address_list	List of MAC addresses or NULL.
+ * @param address_count Number of accepted addresses (can be > max_count)
+ */
+void nic_rxc_multicast_get_mode(const nic_rxc_t *rxc,
+	nic_multicast_mode_t *mode, size_t max_count, nic_address_t *address_list,
+	size_t *address_count)
+{
+	*mode = rxc->multicast_mode;
+	if (rxc->multicast_mode == NIC_MULTICAST_LIST) {
+		nic_rxc_add_addr_t hs = {
+			.max_count = max_count,
+			.address_list = address_list,
+			.address_count = 0
+		};
+		nic_addr_db_foreach(&rxc->multicast_addrs, nic_rxc_add_addr, &hs);
+		if (address_count) {
+			*address_count = hs.address_count;
+		}
+	}
+}
+
+/**
+ * Sets the current mode of multicast frames receiving.
+ *
+ * @param rxc
+ * @param mode			The new multicast mode
+ * @param address_list	List of MAC addresses or NULL.
+ * @param address_count Number of addresses in the list
+ *
+ * @return EOK		On success
+ * @return EINVAL	If any of the MAC addresses is not a multicast address.
+ * @return ENOMEM	If there was not enough memory
+ */
+int nic_rxc_multicast_set_mode(nic_rxc_t *rxc, nic_multicast_mode_t mode,
+	const nic_address_t *address_list, size_t address_count)
+{
+	if (mode == NIC_MULTICAST_LIST && address_list == NULL)
+		return EINVAL;
+	else if (mode != NIC_MULTICAST_LIST && address_list != NULL)
+		return EINVAL;
+	
+	if (rxc->multicast_mode == NIC_MULTICAST_LIST)
+		nic_addr_db_clear(&rxc->multicast_addrs);
+	
+	rxc->multicast_mode = mode;
+	size_t i;
+	for (i = 0; i < address_count; ++i) {
+		int rc = nic_addr_db_insert(&rxc->multicast_addrs,
+			(const uint8_t *)&address_list[i].address);
+		if (rc == ENOMEM) {
+			return ENOMEM;
+		}
+	}
+	return EOK;
+}
+
+/**
+ * Queries the current mode of broadcast frames receiving.
+ *
+ * @param rxc
+ * @param mode			The new broadcast mode
+ */
+void nic_rxc_broadcast_get_mode(const nic_rxc_t *rxc, nic_broadcast_mode_t *mode)
+{
+	*mode = rxc->broadcast_mode;
+}
+
+/**
+ * Sets the current mode of broadcast frames receiving.
+ *
+ * @param rxc
+ * @param mode			The new broadcast mode
+ *
+ * @return EOK		On success
+ */
+int nic_rxc_broadcast_set_mode(nic_rxc_t *rxc, nic_broadcast_mode_t mode)
+{
+	rxc->broadcast_mode = mode;
+	return EOK;
+}
+
+/**
+ * Queries the current blocked source addresses.
+ *
+ * @param rxc
+ * @param max_count		Max number of addresses that can be written into the
+ * 						address_list.
+ * @param address_list	List of MAC addresses or NULL.
+ * @param address_count Number of blocked addresses (can be > max_count)
+ */
+void nic_rxc_blocked_sources_get(const nic_rxc_t *rxc,
+	size_t max_count, nic_address_t *address_list, size_t *address_count)
+{
+	nic_rxc_add_addr_t hs = {
+		.max_count = max_count,
+		.address_list = address_list,
+		.address_count = 0
+	};
+	nic_addr_db_foreach(&rxc->blocked_sources, nic_rxc_add_addr, &hs);
+	if (address_count) {
+		*address_count = hs.address_count;
+	}
+}
+
+/**
+ * Clears the currently blocked addresses and sets the addresses contained in
+ * the list as the set of blocked source addresses (no frame with this source
+ * address will be received). Duplicated addresses are ignored.
+ *
+ * @param rxc
+ * @param address_list	List of the blocked addresses. Can be NULL.
+ * @param address_count Number of addresses in the list
+ *
+ * @return EOK		On success
+ * @return ENOMEM	If there was not enough memory
+ */
+int nic_rxc_blocked_sources_set(nic_rxc_t *rxc,
+	const nic_address_t *address_list, size_t address_count)
+{
+	assert((address_count == 0 && address_list == NULL)
+		|| (address_count != 0 && address_list != NULL));
+
+	nic_addr_db_clear(&rxc->blocked_sources);
+	rxc->block_sources = (address_count != 0);
+	size_t i;
+	for (i = 0; i < address_count; ++i) {
+		int rc = nic_addr_db_insert(&rxc->blocked_sources,
+			(const uint8_t *) &address_list[i].address);
+		if (rc == ENOMEM) {
+			return ENOMEM;
+		}
+	}
+	return EOK;
+}
+
+/**
+ * Query mask used for filtering according to the VLAN tags.
+ *
+ * @param rxc
+ * @param mask		Must be 512 bytes long
+ *
+ * @return EOK
+ * @return ENOENT
+ */
+int nic_rxc_vlan_get_mask(const nic_rxc_t *rxc, nic_vlan_mask_t *mask)
+{
+	if (rxc->vlan_mask == NULL) {
+		return ENOENT;
+	}
+	memcpy(mask, rxc->vlan_mask, sizeof (nic_vlan_mask_t));
+	return EOK;
+}
+
+/**
+ * Set mask for filtering according to the VLAN tags.
+ *
+ * @param rxc
+ * @param mask		Must be 512 bytes long
+ *
+ * @return EOK
+ * @return ENOMEM
+ */
+int nic_rxc_vlan_set_mask(nic_rxc_t *rxc, const nic_vlan_mask_t *mask)
+{
+	if (mask == NULL) {
+		if (rxc->vlan_mask) {
+			free(rxc->vlan_mask);
+		}
+		rxc->vlan_mask = NULL;
+		return EOK;
+	}
+	if (!rxc->vlan_mask) {
+		rxc->vlan_mask = malloc(sizeof (nic_vlan_mask_t));
+		if (rxc->vlan_mask == NULL) {
+			return ENOMEM;
+		}
+	}
+	memcpy(rxc->vlan_mask, mask, sizeof (nic_vlan_mask_t));
+	return EOK;
+}
+
+
+/**
+ * Check if the frame passes through the receive control.
+ *
+ * @param rxc
+ * @param packet	The probed frame
+ *
+ * @return True if the frame passes, false if it does not
+ */
+int nic_rxc_check(const nic_rxc_t *rxc, const packet_t *packet,
+	nic_frame_type_t *frame_type)
+{
+	assert(frame_type != NULL);
+	uint8_t *dest_addr = (uint8_t *) packet + packet->data_start;
+	uint8_t *src_addr = dest_addr + ETH_ADDR;
+
+	if (dest_addr[0] & 1) {
+		/* Multicast or broadcast */
+		if (*(uint32_t *) dest_addr == 0xFFFFFFFF &&
+		    *(uint16_t *) (dest_addr + 4) == 0xFFFF) {
+			/* It is broadcast */
+			*frame_type = NIC_FRAME_BROADCAST;
+			if (rxc->broadcast_mode == NIC_BROADCAST_BLOCKED)
+				return false;
+		} else {
+			*frame_type = NIC_FRAME_MULTICAST;
+			/* In promiscuous mode the multicast_exact should be set to true */
+			if (!rxc->multicast_exact) {
+				if (rxc->multicast_mode == NIC_MULTICAST_BLOCKED)
+					return false;
+				else {
+					if (!nic_addr_db_contains(&rxc->multicast_addrs,
+					    dest_addr))
+						return false;
+				}
+			}
+		}
+	} else {
+		/* Unicast */
+		*frame_type = NIC_FRAME_UNICAST;
+		/* In promiscuous mode the unicast_exact should be set to true */
+		if (!rxc->unicast_exact) {
+			if (rxc->unicast_mode == NIC_UNICAST_BLOCKED)
+				return false;
+			else {
+				if (!nic_addr_db_contains(&rxc->unicast_addrs,
+				    dest_addr))
+					return false;
+			}
+		}
+	}
+	/* Blocked source addresses */
+	if (rxc->block_sources) {
+		if (nic_addr_db_contains(&rxc->blocked_sources, src_addr))
+			return false;
+	}
+	/* VLAN filtering */
+	if (!rxc->vlan_exact && rxc->vlan_mask != NULL) {
+		vlan_header_t *vlan_header = (vlan_header_t *)
+			((uint8_t *) packet + packet->data_start + 2 * ETH_ADDR);
+		if (vlan_header->tpid_upper == VLAN_TPID_UPPER &&
+			vlan_header->tpid_lower == VLAN_TPID_LOWER) {
+			int index = ((int) (vlan_header->vid_upper & 0xF) << 5) |
+			    (vlan_header->vid_lower >> 3);
+			if (!(rxc->vlan_mask->bitmap[index] &
+			    (1 << (vlan_header->vid_lower & 0x7))))
+				return false;
+		}
+	}
+	
+	return true;
+}
+
+/**
+ * Set information about current HW filtering.
+ *  1 ...	Only those frames we want to receive are passed through HW
+ *  0 ...	The HW filtering is imperfect
+ * -1 ...	Don't change the setting
+ * This function should be called only from the mode change event handler.
+ *
+ * @param	rxc
+ * @param	unicast_exact	Unicast frames
+ * @param	mcast_exact		Multicast frames
+ * @param	vlan_exact		VLAN tags
+ */
+void nic_rxc_hw_filtering(nic_rxc_t *rxc,
+	int unicast_exact, int multicast_exact, int vlan_exact)
+{
+	if (unicast_exact >= 0)
+		rxc->unicast_exact = unicast_exact;
+	if (multicast_exact >= 0)
+		rxc->multicast_exact = multicast_exact;
+	if (vlan_exact >= 0)
+		rxc->vlan_exact = vlan_exact;
+}
+
+/**
+ * Computes hash for the address list based on standard multicast address
+ * hashing.
+ *
+ * @param address_list
+ * @param count
+ *
+ * @return Multicast hash
+ *
+ * @see multicast_hash
+ */
+uint64_t nic_rxc_mcast_hash(const nic_address_t *address_list, size_t count)
+{
+	size_t i;
+	uint64_t hash = 0;
+	for (i = 0; i < count; ++i) {
+		hash |= multicast_hash(address_list[i].address);
+	}
+	return hash;
+}
+
+static void nic_rxc_hash_addr(const uint8_t *address, void *arg)
+{
+	*((uint64_t *) arg) |= multicast_hash(address);
+}
+
+/**
+ * Computes hash for multicast addresses currently set up in the RX multicast
+ * filtering. For promiscuous mode returns all ones, for blocking all zeroes.
+ * Can be called only from the on_*_change handler.
+ *
+ * @param rxc
+ *
+ * @return Multicast hash
+ *
+ * @see multicast_hash
+ */
+uint64_t nic_rxc_multicast_get_hash(const nic_rxc_t *rxc)
+{
+	switch (rxc->multicast_mode) {
+	case NIC_MULTICAST_UNKNOWN:
+	case NIC_MULTICAST_BLOCKED:
+		return 0;
+	case NIC_MULTICAST_LIST:
+		break;
+	case NIC_MULTICAST_PROMISC:
+		return ~ (uint64_t) 0;
+	}
+	uint64_t hash;
+	nic_addr_db_foreach(&rxc->multicast_addrs, nic_rxc_hash_addr, &hash);
+	return hash;
+}
+
+/** @}
+ */
Index: uspace/lib/nic/src/nic_wol_virtues.c
===================================================================
--- uspace/lib/nic/src/nic_wol_virtues.c	(revision 00d7e1bfe06501df61aed156666d5597397d910a)
+++ uspace/lib/nic/src/nic_wol_virtues.c	(revision 00d7e1bfe06501df61aed156666d5597397d910a)
@@ -0,0 +1,296 @@
+/*
+ * Copyright (c) 2011 Radim Vansa
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * - Redistributions of source code must retain the above copyright
+ *   notice, this list of conditions and the following disclaimer.
+ * - Redistributions in binary form must reproduce the above copyright
+ *   notice, this list of conditions and the following disclaimer in the
+ *   documentation and/or other materials provided with the distribution.
+ * - The name of the author may not be used to endorse or promote products
+ *   derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+/**
+ * @addtogroup libnic
+ * @{
+ */
+/**
+ * @file
+ * @brief Wake-on-LAN support
+ */
+
+#include "nic_wol_virtues.h"
+#include <assert.h>
+
+#define NIC_WV_HASH_COUNT 32
+
+/**
+ * Hash table helper function
+ */
+static int nic_wv_compare(unsigned long key[], hash_count_t keys,
+	link_t *item)
+{
+	nic_wol_virtue_t *virtue = (nic_wol_virtue_t *) item;
+	return (virtue->id == (nic_wv_id_t) key[0]);
+}
+
+/**
+ * Hash table helper function
+ */
+static void nic_wv_rc(link_t *item)
+{
+}
+
+/**
+ * Hash table helper function
+ */
+static hash_index_t nic_wv_hash(unsigned long keys[])
+{
+	return keys[0] % NIC_WV_HASH_COUNT;
+}
+
+/**
+ * Initializes the WOL virtues structure
+ *
+ * @param wvs
+ *
+ * @return EOK		On success
+ * @return ENOMEM	On not enough memory
+ */
+int nic_wol_virtues_init(nic_wol_virtues_t *wvs)
+{
+	bzero(wvs, sizeof (nic_wol_virtues_t));
+	wvs->table_operations.compare = nic_wv_compare;
+	wvs->table_operations.hash = nic_wv_hash;
+	wvs->table_operations.remove_callback = nic_wv_rc;
+	if (!hash_table_create(&wvs->table, NIC_WV_HASH_COUNT, 1,
+		&wvs->table_operations)) {
+		return ENOMEM;
+	}
+	size_t i;
+	for (i = 0; i < NIC_WV_MAX; ++i) {
+		wvs->caps_max[i] = -1;
+	}
+	wvs->next_id = 0;
+	return EOK;
+}
+
+/**
+ * Reinitializes the structure, destroying all virtues. The next_id is not
+ * changed (some apps could still hold the filter IDs).
+ *
+ * @param wvs
+ */
+void nic_wol_virtues_clear(nic_wol_virtues_t *wvs)
+{
+	hash_table_clear(&wvs->table);
+	nic_wv_type_t type;
+	for (type = NIC_WV_NONE; type < NIC_WV_MAX; ++type) {
+		nic_wol_virtue_t *virtue = wvs->lists[type];
+		while (virtue != NULL) {
+			nic_wol_virtue_t *next = virtue->next;
+			free(virtue->data);
+			free(virtue);
+			virtue = next;
+		}
+		wvs->lists_sizes[type] = 0;
+	}
+}
+
+/**
+ * Verifies that the arguments for the WOL virtues are correct.
+ *
+ * @param type		Type of the virtue
+ * @param data		Data argument for the virtue
+ * @param length	Length of the data
+ *
+ * @return EOK		The arguments are correct
+ * @return EINVAL	The arguments are incorrect
+ * @return ENOTSUP	This type is unknown
+ */
+int nic_wol_virtues_verify(nic_wv_type_t type, const void *data, size_t length)
+{
+	switch (type) {
+	case NIC_WV_ARP_REQUEST:
+	case NIC_WV_BROADCAST:
+	case NIC_WV_LINK_CHANGE:
+		return EOK;
+	case NIC_WV_DESTINATION:
+		return length == sizeof (nic_address_t) ? EOK : EINVAL;
+	case NIC_WV_DIRECTED_IPV4:
+		return length == sizeof (nic_wv_ipv4_data_t) ? EOK : EINVAL;
+	case NIC_WV_DIRECTED_IPV6:
+		return length == sizeof (nic_wv_ipv6_data_t) ? EOK : EINVAL;
+	case NIC_WV_FULL_MATCH:
+		return length % 2 == 0 ? EOK : EINVAL;
+	case NIC_WV_MAGIC_PACKET:
+		return data == NULL || length == sizeof (nic_wv_magic_packet_data_t) ?
+			EOK : EINVAL;
+	default:
+		return ENOTSUP;
+	}
+}
+
+/**
+ * Adds the virtue to the list of known virtues, activating it.
+ *
+ * @param wvs
+ * @param virtue	The virtue structure
+ *
+ * @return EOK		On success
+ * @return ENOTSUP	If the virtue type is not supported
+ * @return EINVAL	If the virtue type is a single-filter and there's already
+ * 					a virtue of this type defined, or there is something wrong
+ * 					with the data
+ * @return ENOMEM	Not enough memory to activate the virtue
+ */
+int nic_wol_virtues_add(nic_wol_virtues_t *wvs, nic_wol_virtue_t *virtue)
+{
+	if (!nic_wv_is_multi(virtue->type) &&
+		wvs->lists[virtue->type] != NULL) {
+		return EINVAL;
+	}
+	do {
+		virtue->id = wvs->next_id++;
+	} while (NULL !=
+		hash_table_find(&wvs->table, (unsigned long *) &virtue->id));
+	hash_table_insert(&wvs->table,
+		(unsigned long *) &virtue->id, &virtue->item);
+	virtue->next = wvs->lists[virtue->type];
+	wvs->lists[virtue->type] = virtue;
+	wvs->lists_sizes[virtue->type]++;
+	return EOK;
+}
+
+/**
+ * Removes the virtue from the list of virtues, but NOT deallocating the
+ * nic_wol_virtue structure.
+ *
+ * @param wvs
+ * @param id	Identifier of the removed virtue
+ *
+ * @return Removed virtue structure or NULL if not found.
+ */
+nic_wol_virtue_t *nic_wol_virtues_remove(nic_wol_virtues_t *wvs, nic_wv_id_t id)
+{
+	nic_wol_virtue_t *virtue = (nic_wol_virtue_t *)
+		hash_table_find(&wvs->table, (unsigned long *) &id);
+	if (virtue == NULL) {
+		return NULL;
+	}
+
+	/* Remove from filter_table */
+	hash_table_remove(&wvs->table, (unsigned long *) &id, 1);
+
+	/* Remove from filter_types */
+	assert(wvs->lists[virtue->type] != NULL);
+	if (wvs->lists[virtue->type] == virtue) {
+		wvs->lists[virtue->type] = virtue->next;
+	} else {
+		nic_wol_virtue_t *wv = wvs->lists[virtue->type];
+		while (wv->next != virtue) {
+			wv = wv->next;
+			assert(wv != NULL);
+		}
+		wv->next = virtue->next;
+	}
+	wvs->lists_sizes[virtue->type]--;
+
+	virtue->next = NULL;
+	return virtue;
+}
+
+
+/**
+ * Searches the filters table for a filter with specified ID
+ *
+ * @param wvs
+ * @param id	Identifier of the searched virtue
+ *
+ * @return Requested filter or NULL if not found.
+ */
+const nic_wol_virtue_t *nic_wol_virtues_find(const nic_wol_virtues_t *wvs,
+	nic_wv_id_t id)
+{
+	/*
+	 * The hash_table_find cannot be const, because it would require the
+	 * returned link to be const as well. But in this case, when we're returning
+	 * constant virtue the retyping is correct.
+	 */
+	link_t *virtue = hash_table_find(
+		&((nic_wol_virtues_t *) wvs)->table, (unsigned long *) &id);
+	return (const nic_wol_virtue_t *) virtue;
+}
+
+/**
+ * Fill identifiers of current wol virtues of the specified type into the list.
+ * If the type is set to NIC_WV_NONE, all virtues are used.
+ *
+ * @param		wvs
+ * @param[in]	type		Type of the virtues or NIC_WV_NONE
+ * @param[out]	id_list		The new vector of filter IDs. Can be NULL.
+ * @param[out]	count		Number of IDs in the filter_list. Can be NULL.
+ *
+ * @return EOK		If it completes successfully
+ * @return EINVAL	If the filter type is invalid
+ */
+int nic_wol_virtues_list(const nic_wol_virtues_t *wvs, nic_wv_type_t type,
+	size_t max_count, nic_wv_id_t *id_list, size_t *id_count)
+{
+	size_t count = 0;
+	if (type == NIC_WV_NONE) {
+		size_t i;
+		for (i = NIC_WV_NONE; i < NIC_WV_MAX; ++i) {
+			if (id_list != NULL) {
+				nic_wol_virtue_t *virtue = wvs->lists[i];
+				while (virtue != NULL) {
+					if (count < max_count) {
+						id_list[count] = virtue->id;
+					}
+					++count;
+					virtue = virtue->next;
+				}
+			} else {
+				count += wvs->lists_sizes[i];
+			}
+		}
+	} else if (type >= NIC_WV_MAX) {
+		return EINVAL;
+	} else {
+		if (id_list != NULL) {
+			nic_wol_virtue_t *virtue = wvs->lists[type];
+			while (virtue != NULL) {
+				if (count < max_count) {
+					id_list[count] = virtue->id;
+				}
+				++count;
+				virtue = virtue->next;
+			}
+		} else {
+			count = wvs->lists_sizes[type];
+		}
+	}
+	if (id_count != NULL) {
+		*id_count = count;
+	}
+	return EOK;
+}
+
+/** @}
+ */
