1/*
2 * Copyright 2006-2010, Haiku, Inc. All Rights Reserved.
3 * Distributed under the terms of the MIT License.
4 */
5#ifndef NET_STACK_H
6#define NET_STACK_H
7
8
9#include <module.h>
10
11#include <lock.h>
12#include <util/list.h>
13
14
15#define NET_STACK_MODULE_NAME "network/stack/v1"
16
17
18struct net_address_module_info;
19struct net_protocol_module_info;
20
21typedef struct net_buffer net_buffer;
22typedef struct net_device net_device;
23typedef struct net_domain net_domain;
24typedef struct net_socket net_socket;
25
26struct net_timer;
27
28
29typedef struct net_fifo {
30	mutex		lock;
31	sem_id		notify;
32	int32		waiting;
33
34	size_t		max_bytes;
35	size_t		current_bytes;
36
37	struct list	buffers;
38} net_fifo;
39
40typedef void (*net_timer_func)(struct net_timer* timer, void* data);
41
42typedef struct net_timer {
43	struct list_link link;
44	net_timer_func	hook;
45	void*			data;
46	bigtime_t		due;
47	uint32			flags;
48} net_timer;
49
50typedef status_t (*net_deframe_func)(net_device* device, net_buffer* buffer);
51typedef status_t (*net_receive_func)(void* cookie, net_device* device,
52	net_buffer* buffer);
53
54enum {
55	B_DEVICE_GOING_UP = 1,
56	B_DEVICE_GOING_DOWN,
57	B_DEVICE_BEING_REMOVED,
58};
59
60typedef struct net_device_monitor {
61	struct list_link link;
62	void*	cookie;
63
64	status_t (*receive)(struct net_device_monitor* monitor,
65		struct net_buffer* buffer);
66	void (*event)(struct net_device_monitor* monitor, int32 event);
67} net_device_monitor;
68
69typedef struct ancillary_data_header {
70	int		level;
71	int		type;
72	size_t	len;
73} ancillary_data_header;
74
75typedef struct ancillary_data_container ancillary_data_container;
76
77
78#define B_NET_FRAME_TYPE(super, sub)	(((int32)(super) << 16) | (sub))
79	// Use this when registering a device handler, see net/if_types.h for
80	// the possible "super" values. Input values are in host byte order.
81
82
83// sub types
84enum {
85	B_NET_FRAME_TYPE_IPV4				= 0x0001,
86	B_NET_FRAME_TYPE_IPV6				= 0x0002,
87	B_NET_FRAME_TYPE_IPX				= 0x0003,
88	B_NET_FRAME_TYPE_APPLE_TALK			= 0x0004,
89
90	B_NET_FRAME_TYPE_ALL				= 0x0000
91};
92
93
94struct net_stack_module_info {
95	module_info info;
96
97	status_t	(*register_domain)(int family, const char* name,
98					struct net_protocol_module_info* module,
99					struct net_address_module_info* addressModule,
100					net_domain** _domain);
101	status_t	(*unregister_domain)(net_domain* domain);
102	net_domain*	(*get_domain)(int family);
103
104	status_t	(*register_domain_protocols)(int family, int type, int protocol,
105					...);
106	status_t	(*register_domain_datalink_protocols)(int family, int type,
107					...);
108	status_t	(*register_domain_receiving_protocol)(int family, int type,
109					const char* moduleName);
110
111	status_t	(*get_domain_receiving_protocol)(net_domain* domain,
112					uint32 type, struct net_protocol_module_info** _module);
113	status_t	(*put_domain_receiving_protocol)(net_domain* domain,
114					uint32 type);
115
116	// devices
117	status_t	(*register_device_deframer)(net_device* device,
118					net_deframe_func deframeFunc);
119	status_t	(*unregister_device_deframer)(net_device* device);
120
121	status_t	(*register_domain_device_handler)(net_device* device,
122					int32 type, net_domain* domain);
123	status_t	(*register_device_handler)(net_device* device,
124					int32 type, net_receive_func receiveFunc, void* cookie);
125	status_t	(*unregister_device_handler)(net_device* device, int32 type);
126
127	status_t	(*register_device_monitor)(net_device* device,
128					struct net_device_monitor* monitor);
129	status_t	(*unregister_device_monitor)(net_device* device,
130					struct net_device_monitor* monitor);
131
132	status_t	(*device_link_changed)(net_device* device);
133	status_t	(*device_removed)(net_device* device);
134
135	status_t	(*device_enqueue_buffer)(net_device* device,
136					net_buffer* buffer);
137
138	// Utility Functions
139
140	// notification
141	status_t	(*notify_socket)(net_socket* socket, uint8 event, int32 value);
142
143	// checksum
144	uint16		(*checksum)(uint8* buffer, size_t length);
145
146	// fifo
147	status_t	(*init_fifo)(net_fifo* fifo, const char* name, size_t maxBytes);
148	void		(*uninit_fifo)(net_fifo* fifo);
149	status_t	(*fifo_enqueue_buffer)(net_fifo* fifo, net_buffer* buffer);
150	ssize_t		(*fifo_dequeue_buffer)(net_fifo* fifo, uint32 flags,
151					bigtime_t timeout, net_buffer** _buffer);
152	status_t	(*clear_fifo)(net_fifo* fifo);
153	status_t	(*fifo_socket_enqueue_buffer)(net_fifo* fifo,
154					net_socket* socket, uint8 event, net_buffer* buffer);
155
156	// timer
157	void		(*init_timer)(net_timer* timer, net_timer_func hook,
158					void* data);
159	void		(*set_timer)(net_timer* timer, bigtime_t delay);
160	bool		(*cancel_timer)(net_timer* timer);
161	status_t	(*wait_for_timer)(net_timer* timer);
162	bool		(*is_timer_active)(net_timer* timer);
163	bool		(*is_timer_running)(net_timer* timer);
164
165	// syscall restart
166	bool		(*is_syscall)(void);
167	bool		(*is_restarted_syscall)(void);
168	void		(*store_syscall_restart_timeout)(bigtime_t timeout);
169	bigtime_t	(*restore_syscall_restart_timeout)(void);
170
171	// ancillary data
172	ancillary_data_container* (*create_ancillary_data_container)();
173	void		(*delete_ancillary_data_container)(
174					ancillary_data_container* container);
175	status_t	(*add_ancillary_data)(ancillary_data_container* container,
176					const ancillary_data_header* header, const void* data,
177					void (*destructor)(const ancillary_data_header*, void*),
178					void** _allocatedData);
179	status_t	(*remove_ancillary_data)(ancillary_data_container* container,
180					void* data, bool destroy);
181	void*		(*move_ancillary_data)(ancillary_data_container* from,
182					ancillary_data_container* to);
183	void*		(*next_ancillary_data)(ancillary_data_container* container,
184					void* previousData, ancillary_data_header* _header);
185};
186
187
188#endif	// NET_STACK_H
189