xref: /haiku/src/add-ons/kernel/network/stack/link.cpp (revision 8b6048a28553498b6c4fb099588da984830ded0f)
1 /*
2  * Copyright 2006-2010, Haiku, Inc. All Rights Reserved.
3  * Distributed under the terms of the MIT License.
4  *
5  * Authors:
6  *		Axel Dörfler, axeld@pinc-software.de
7  */
8 
9 
10 //! The net_protocol one talks to when using the AF_LINK protocol
11 
12 
13 #include "link.h"
14 
15 #include <net/if_dl.h>
16 #include <net/if_types.h>
17 #include <new>
18 #include <stdlib.h>
19 #include <string.h>
20 #include <sys/sockio.h>
21 
22 #include <KernelExport.h>
23 
24 #include <lock.h>
25 #include <net_datalink.h>
26 #include <net_device.h>
27 #include <ProtocolUtilities.h>
28 #include <util/AutoLock.h>
29 
30 #include "device_interfaces.h"
31 #include "domains.h"
32 #include "interfaces.h"
33 #include "stack_private.h"
34 #include "utility.h"
35 
36 
37 class LocalStackBundle {
38 public:
39 	static net_stack_module_info* Stack() { return &gNetStackModule; }
40 	static net_buffer_module_info* Buffer() { return &gNetBufferModule; }
41 };
42 
43 typedef DatagramSocket<MutexLocking, LocalStackBundle> LocalDatagramSocket;
44 
45 class LinkProtocol : public net_protocol, public LocalDatagramSocket {
46 public:
47 								LinkProtocol(net_socket* socket);
48 	virtual						~LinkProtocol();
49 
50 			status_t			StartMonitoring(const char* deviceName);
51 			status_t			StopMonitoring(const char* deviceName);
52 
53 			status_t			Bind(const sockaddr* address);
54 			status_t			Unbind();
55 			bool				IsBound() const
56 									{ return fBoundToDevice != NULL; }
57 
58 			size_t				MTU();
59 
60 protected:
61 			status_t			SocketStatus(bool peek) const;
62 
63 private:
64 			status_t			_Unregister();
65 
66 	static	status_t			_MonitorData(net_device_monitor* monitor,
67 									net_buffer* buffer);
68 	static	void				_MonitorEvent(net_device_monitor* monitor,
69 									int32 event);
70 	static	status_t			_ReceiveData(void* cookie, net_device* device,
71 									net_buffer* buffer);
72 
73 private:
74 			net_device_monitor	fMonitor;
75 			net_device_interface* fMonitoredDevice;
76 			net_device_interface* fBoundToDevice;
77 			uint32				fBoundType;
78 };
79 
80 
81 struct net_domain* sDomain;
82 
83 
84 LinkProtocol::LinkProtocol(net_socket* socket)
85 	:
86 	LocalDatagramSocket("packet capture", socket),
87 	fMonitoredDevice(NULL),
88 	fBoundToDevice(NULL)
89 {
90 	fMonitor.cookie = this;
91 	fMonitor.receive = _MonitorData;
92 	fMonitor.event = _MonitorEvent;
93 }
94 
95 
96 LinkProtocol::~LinkProtocol()
97 {
98 	if (fMonitoredDevice != NULL) {
99 		unregister_device_monitor(fMonitoredDevice->device, &fMonitor);
100 		put_device_interface(fMonitoredDevice);
101 	} else
102 		Unbind();
103 }
104 
105 
106 status_t
107 LinkProtocol::StartMonitoring(const char* deviceName)
108 {
109 	MutexLocker locker(fLock);
110 
111 	if (fMonitoredDevice != NULL)
112 		return B_BUSY;
113 
114 	net_device_interface* interface = get_device_interface(deviceName);
115 	if (interface == NULL)
116 		return B_DEVICE_NOT_FOUND;
117 
118 	status_t status = register_device_monitor(interface->device, &fMonitor);
119 	if (status < B_OK) {
120 		put_device_interface(interface);
121 		return status;
122 	}
123 
124 	fMonitoredDevice = interface;
125 	return B_OK;
126 }
127 
128 
129 status_t
130 LinkProtocol::StopMonitoring(const char* deviceName)
131 {
132 	MutexLocker locker(fLock);
133 
134 	if (fMonitoredDevice == NULL
135 		|| strcmp(fMonitoredDevice->device->name, deviceName) != 0)
136 		return B_BAD_VALUE;
137 
138 	return _Unregister();
139 }
140 
141 
142 status_t
143 LinkProtocol::Bind(const sockaddr* address)
144 {
145 	// Only root is allowed to bind to a link layer interface
146 	if (address == NULL || geteuid() != 0)
147 		return B_NOT_ALLOWED;
148 
149 	MutexLocker locker(fLock);
150 
151 	if (fMonitoredDevice != NULL)
152 		return B_BUSY;
153 
154 	Interface* interface = get_interface_for_link(sDomain, address);
155 	if (interface == NULL)
156 		return B_BAD_VALUE;
157 
158 	net_device_interface* boundTo
159 		= acquire_device_interface(interface->DeviceInterface());
160 
161 	interface->ReleaseReference();
162 
163 	if (boundTo == NULL)
164 		return B_BAD_VALUE;
165 
166 	sockaddr_dl& linkAddress = *(sockaddr_dl*)address;
167 
168 	if (linkAddress.sdl_type != 0) {
169 		fBoundType = B_NET_FRAME_TYPE(linkAddress.sdl_type,
170 			linkAddress.sdl_e_type);
171 		// Bind to the type requested - this is needed in order to
172 		// receive any buffers
173 		// TODO: this could be easily changed by introducing catch all or rule
174 		// based handlers!
175 		status_t status = register_device_handler(boundTo->device, fBoundType,
176 			&LinkProtocol::_ReceiveData, this);
177 		if (status != B_OK)
178 			return status;
179 	} else
180 		fBoundType = 0;
181 
182 	fBoundToDevice = boundTo;
183 	socket->bound_to_device = boundTo->device->index;
184 
185 	return B_OK;
186 }
187 
188 
189 status_t
190 LinkProtocol::Unbind()
191 {
192 	MutexLocker locker(fLock);
193 
194 	if (fBoundToDevice == NULL)
195 		return B_BAD_VALUE;
196 
197 	unregister_device_handler(fBoundToDevice->device, fBoundType);
198 	put_device_interface(fBoundToDevice);
199 
200 	socket->bound_to_device = 0;
201 	return B_OK;
202 }
203 
204 
205 size_t
206 LinkProtocol::MTU()
207 {
208 	MutexLocker locker(fLock);
209 
210 	if (!IsBound())
211 		return 0;
212 
213 	return fBoundToDevice->device->mtu;
214 }
215 
216 
217 status_t
218 LinkProtocol::SocketStatus(bool peek) const
219 {
220 	if (fMonitoredDevice == NULL)
221 		return B_DEVICE_NOT_FOUND;
222 
223 	return LocalDatagramSocket::SocketStatus(peek);
224 }
225 
226 
227 status_t
228 LinkProtocol::_Unregister()
229 {
230 	if (fMonitoredDevice == NULL)
231 		return B_BAD_VALUE;
232 
233 	status_t status = unregister_device_monitor(fMonitoredDevice->device,
234 		&fMonitor);
235 	put_device_interface(fMonitoredDevice);
236 	fMonitoredDevice = NULL;
237 
238 	return status;
239 }
240 
241 
242 /*static*/ status_t
243 LinkProtocol::_MonitorData(net_device_monitor* monitor, net_buffer* packet)
244 {
245 	return ((LinkProtocol*)monitor->cookie)->EnqueueClone(packet);
246 }
247 
248 
249 /*static*/ void
250 LinkProtocol::_MonitorEvent(net_device_monitor* monitor, int32 event)
251 {
252 	LinkProtocol* protocol = (LinkProtocol*)monitor->cookie;
253 
254 	if (event == B_DEVICE_GOING_DOWN) {
255 		MutexLocker _(protocol->fLock);
256 
257 		protocol->_Unregister();
258 		if (protocol->IsEmpty()) {
259 			protocol->WakeAll();
260 			notify_socket(protocol->socket, B_SELECT_READ, B_DEVICE_NOT_FOUND);
261 		}
262 	}
263 }
264 
265 
266 /*static*/ status_t
267 LinkProtocol::_ReceiveData(void* cookie, net_device* device, net_buffer* buffer)
268 {
269 	LinkProtocol* protocol = (LinkProtocol*)cookie;
270 
271 	return protocol->Enqueue(buffer);
272 }
273 
274 
275 //	#pragma mark -
276 
277 
278 static bool
279 user_request_get_device_interface(void* value, struct ifreq& request,
280 	net_device_interface*& interface)
281 {
282 	if (user_memcpy(&request, value, IF_NAMESIZE) < B_OK)
283 		return false;
284 
285 	interface = get_device_interface(request.ifr_name);
286 	return true;
287 }
288 
289 
290 //	#pragma mark - net_protocol module
291 
292 
293 static net_protocol*
294 link_init_protocol(net_socket* socket)
295 {
296 	LinkProtocol* protocol = new (std::nothrow) LinkProtocol(socket);
297 	if (protocol != NULL && protocol->InitCheck() < B_OK) {
298 		delete protocol;
299 		return NULL;
300 	}
301 
302 	return protocol;
303 }
304 
305 
306 static status_t
307 link_uninit_protocol(net_protocol* protocol)
308 {
309 	delete (LinkProtocol*)protocol;
310 	return B_OK;
311 }
312 
313 
314 static status_t
315 link_open(net_protocol* protocol)
316 {
317 	return B_OK;
318 }
319 
320 
321 static status_t
322 link_close(net_protocol* protocol)
323 {
324 	return B_OK;
325 }
326 
327 
328 static status_t
329 link_free(net_protocol* protocol)
330 {
331 	return B_OK;
332 }
333 
334 
335 static status_t
336 link_connect(net_protocol* protocol, const struct sockaddr* address)
337 {
338 	return B_NOT_SUPPORTED;
339 }
340 
341 
342 static status_t
343 link_accept(net_protocol* protocol, struct net_socket** _acceptedSocket)
344 {
345 	return B_NOT_SUPPORTED;
346 }
347 
348 
349 static status_t
350 link_control(net_protocol* _protocol, int level, int option, void* value,
351 	size_t* _length)
352 {
353 	LinkProtocol* protocol = (LinkProtocol*)_protocol;
354 
355 	switch (option) {
356 		case SIOCGIFINDEX:
357 		{
358 			// get index of interface
359 			net_device_interface* interface;
360 			struct ifreq request;
361 			if (!user_request_get_device_interface(value, request, interface))
362 				return B_BAD_ADDRESS;
363 
364 			if (interface != NULL) {
365 				request.ifr_index = interface->device->index;
366 				put_device_interface(interface);
367 			} else
368 				request.ifr_index = 0;
369 
370 			return user_memcpy(value, &request, sizeof(struct ifreq));
371 		}
372 		case SIOCGIFNAME:
373 		{
374 			// get name of interface via index
375 			struct ifreq request;
376 			if (user_memcpy(&request, value, sizeof(struct ifreq)) < B_OK)
377 				return B_BAD_ADDRESS;
378 
379 			net_device_interface* interface
380 				= get_device_interface(request.ifr_index);
381 			if (interface == NULL)
382 				return B_DEVICE_NOT_FOUND;
383 
384 			strlcpy(request.ifr_name, interface->device->name, IF_NAMESIZE);
385 			put_device_interface(interface);
386 
387 			return user_memcpy(value, &request, sizeof(struct ifreq));
388 		}
389 
390 		case SIOCGIFCOUNT:
391 		{
392 			// count number of interfaces
393 			struct ifconf config;
394 			config.ifc_value = count_device_interfaces();
395 
396 			return user_memcpy(value, &config, sizeof(struct ifconf));
397 		}
398 
399 		case SIOCGIFCONF:
400 		{
401 			// retrieve available interfaces
402 			struct ifconf config;
403 			if (user_memcpy(&config, value, sizeof(struct ifconf)) < B_OK)
404 				return B_BAD_ADDRESS;
405 
406 			status_t result = list_device_interfaces(config.ifc_buf,
407 				(size_t*)&config.ifc_len);
408 			if (result != B_OK)
409 				return result;
410 
411 			return user_memcpy(value, &config, sizeof(struct ifconf));
412 		}
413 
414 		case SIOCGIFADDR:
415 		{
416 			// get address of interface
417 			net_device_interface* interface;
418 			struct ifreq request;
419 			if (!user_request_get_device_interface(value, request, interface))
420 				return B_BAD_ADDRESS;
421 
422 			if (interface == NULL)
423 				return B_DEVICE_NOT_FOUND;
424 
425 			sockaddr_storage address;
426 			get_device_interface_address(interface, (sockaddr*)&address);
427 			put_device_interface(interface);
428 
429 			return user_memcpy(&((struct ifreq*)value)->ifr_addr,
430 				&address, address.ss_len);
431 		}
432 
433 		case SIOCGIFFLAGS:
434 		{
435 			// get flags of interface
436 			net_device_interface* interface;
437 			struct ifreq request;
438 			if (!user_request_get_device_interface(value, request, interface))
439 				return B_BAD_ADDRESS;
440 
441 			if (interface == NULL)
442 				return B_DEVICE_NOT_FOUND;
443 
444 			request.ifr_flags = interface->device->flags;
445 			put_device_interface(interface);
446 
447 			return user_memcpy(&((struct ifreq*)value)->ifr_flags,
448 				&request.ifr_flags, sizeof(request.ifr_flags));
449 		}
450 
451 		case SIOCSPACKETCAP:
452 		{
453 			// Only root is allowed to capture packets
454 			if (geteuid() != 0)
455 				return B_NOT_ALLOWED;
456 
457 			struct ifreq request;
458 			if (user_memcpy(&request, value, IF_NAMESIZE) != B_OK)
459 				return B_BAD_ADDRESS;
460 
461 			return protocol->StartMonitoring(request.ifr_name);
462 		}
463 
464 		case SIOCCPACKETCAP:
465 		{
466 			struct ifreq request;
467 			if (user_memcpy(&request, value, IF_NAMESIZE) != B_OK)
468 				return B_BAD_ADDRESS;
469 
470 			return protocol->StopMonitoring(request.ifr_name);
471 		}
472 	}
473 
474 	return gNetDatalinkModule.control(sDomain, option, value, _length);
475 }
476 
477 
478 static status_t
479 link_getsockopt(net_protocol* protocol, int level, int option, void* value,
480 	int* length)
481 {
482 	if (protocol->next != NULL) {
483 		return protocol->next->module->getsockopt(protocol, level, option,
484 			value, length);
485 	}
486 
487 	return gNetSocketModule.get_option(protocol->socket, level, option, value,
488 		length);
489 }
490 
491 
492 static status_t
493 link_setsockopt(net_protocol* protocol, int level, int option,
494 	const void* value, int length)
495 {
496 	if (protocol->next != NULL) {
497 		return protocol->next->module->setsockopt(protocol, level, option,
498 			value, length);
499 	}
500 
501 	return gNetSocketModule.set_option(protocol->socket, level, option,
502 		value, length);
503 }
504 
505 
506 static status_t
507 link_bind(net_protocol* _protocol, const struct sockaddr* address)
508 {
509 	LinkProtocol* protocol = (LinkProtocol*)_protocol;
510 	return protocol->Bind(address);
511 }
512 
513 
514 static status_t
515 link_unbind(net_protocol* _protocol, struct sockaddr* address)
516 {
517 	LinkProtocol* protocol = (LinkProtocol*)_protocol;
518 	return protocol->Unbind();
519 }
520 
521 
522 static status_t
523 link_listen(net_protocol* protocol, int count)
524 {
525 	return B_NOT_SUPPORTED;
526 }
527 
528 
529 static status_t
530 link_shutdown(net_protocol* protocol, int direction)
531 {
532 	return B_NOT_SUPPORTED;
533 }
534 
535 
536 static status_t
537 link_send_data(net_protocol* protocol, net_buffer* buffer)
538 {
539 	return gNetDatalinkModule.send_data(protocol, sDomain, buffer);
540 }
541 
542 
543 static status_t
544 link_send_routed_data(net_protocol* protocol, struct net_route* route,
545 	net_buffer* buffer)
546 {
547 	if (buffer->destination->sa_family != buffer->source->sa_family
548 		|| buffer->destination->sa_family != AF_LINK)
549 		return B_BAD_VALUE;
550 
551 	// The datalink layer will take care of the framing
552 
553 	return gNetDatalinkModule.send_routed_data(route, buffer);
554 }
555 
556 
557 static ssize_t
558 link_send_avail(net_protocol* _protocol)
559 {
560 	LinkProtocol* protocol = (LinkProtocol*)_protocol;
561 	if (!protocol->IsBound())
562 		return B_ERROR;
563 
564 	return protocol->socket->send.buffer_size;
565 }
566 
567 
568 static status_t
569 link_read_data(net_protocol* protocol, size_t numBytes, uint32 flags,
570 	net_buffer** _buffer)
571 {
572 	return ((LinkProtocol*)protocol)->Dequeue(flags, _buffer);
573 }
574 
575 
576 static ssize_t
577 link_read_avail(net_protocol* protocol)
578 {
579 	return ((LinkProtocol*)protocol)->AvailableData();
580 }
581 
582 
583 static struct net_domain*
584 link_get_domain(net_protocol* protocol)
585 {
586 	return sDomain;
587 }
588 
589 
590 static size_t
591 link_get_mtu(net_protocol* _protocol, const struct sockaddr* address)
592 {
593 	LinkProtocol* protocol = (LinkProtocol*)_protocol;
594 	return protocol->MTU();
595 }
596 
597 
598 static status_t
599 link_receive_data(net_buffer* buffer)
600 {
601 	// We never receive any data this way
602 	return B_ERROR;
603 }
604 
605 
606 static status_t
607 link_error_received(net_error error, net_buffer* data)
608 {
609 	// We don't do any error processing
610 	return B_ERROR;
611 }
612 
613 
614 static status_t
615 link_error_reply(net_protocol* protocol, net_buffer* cause, net_error error,
616 	net_error_data* errorData)
617 {
618 	// We don't do any error processing
619 	return B_ERROR;
620 }
621 
622 
623 static status_t
624 link_std_ops(int32 op, ...)
625 {
626 	switch (op) {
627 		case B_MODULE_INIT:
628 			return register_domain(AF_LINK, "link", NULL, NULL, &sDomain);
629 
630 		case B_MODULE_UNINIT:
631 			unregister_domain(sDomain);
632 			return B_OK;
633 
634 		default:
635 			return B_ERROR;
636 	}
637 }
638 
639 
640 //	#pragma mark -
641 
642 
643 void
644 link_init()
645 {
646 	register_domain_protocols(AF_LINK, SOCK_DGRAM, 0, "network/stack/link/v1",
647 		NULL);
648 
649 	// TODO: this should actually be registered for all types (besides local)
650 	register_domain_datalink_protocols(AF_LINK, IFT_ETHER,
651 		"network/datalink_protocols/ethernet_frame/v1",
652 		NULL);
653 }
654 
655 
656 net_protocol_module_info gLinkModule = {
657 	{
658 		"network/stack/link/v1",
659 		0,
660 		link_std_ops
661 	},
662 	NET_PROTOCOL_ATOMIC_MESSAGES,
663 
664 	link_init_protocol,
665 	link_uninit_protocol,
666 	link_open,
667 	link_close,
668 	link_free,
669 	link_connect,
670 	link_accept,
671 	link_control,
672 	link_getsockopt,
673 	link_setsockopt,
674 	link_bind,
675 	link_unbind,
676 	link_listen,
677 	link_shutdown,
678 	link_send_data,
679 	link_send_routed_data,
680 	link_send_avail,
681 	link_read_data,
682 	link_read_avail,
683 	link_get_domain,
684 	link_get_mtu,
685 	link_receive_data,
686 	NULL,		// deliver_data
687 	link_error_received,
688 	link_error_reply,
689 	NULL,		// add_ancillary_data()
690 	NULL,		// process_ancillary_data()
691 	NULL,		// process_ancillary_data_no_container()
692 	NULL,		// send_data_no_buffer()
693 	NULL		// read_data_no_buffer()
694 };
695