2 * Copyright 2006-2013, Haiku, Inc. All Rights Reserved.
3 * Distributed under the terms of the MIT License.
6 * Axel Dörfler, axeld@pinc-software.de
10 //! The net_protocol one talks to when using the AF_LINK protocol
15 #include <net/if_dl.h>
16 #include <net/if_types.h>
20 #include <sys/sockio.h>
22 #include <KernelExport.h>
25 #include <net_datalink.h>
26 #include <net_device.h>
27 #include <ProtocolUtilities.h>
28 #include <util/AutoLock.h>
30 #include "device_interfaces.h"
32 #include "interfaces.h"
33 #include "stack_private.h"
37 class LocalStackBundle
{
39 static net_stack_module_info
* Stack() { return &gNetStackModule
; }
40 static net_buffer_module_info
* Buffer() { return &gNetBufferModule
; }
43 typedef DatagramSocket
<MutexLocking
, LocalStackBundle
> LocalDatagramSocket
;
45 class LinkProtocol
: public net_protocol
, public LocalDatagramSocket
{
47 LinkProtocol(net_socket
* socket
);
48 virtual ~LinkProtocol();
50 status_t
StartMonitoring(const char* deviceName
);
51 status_t
StopMonitoring(const char* deviceName
);
53 status_t
Bind(const sockaddr
* address
);
56 { return fBoundToDevice
!= NULL
; }
61 status_t
SocketStatus(bool peek
) const;
64 status_t
_Unregister();
66 static status_t
_MonitorData(net_device_monitor
* monitor
,
68 static void _MonitorEvent(net_device_monitor
* monitor
,
70 static status_t
_ReceiveData(void* cookie
, net_device
* device
,
74 net_device_monitor fMonitor
;
75 net_device_interface
* fMonitoredDevice
;
76 net_device_interface
* fBoundToDevice
;
81 struct net_domain
* sDomain
;
84 LinkProtocol::LinkProtocol(net_socket
* socket
)
86 LocalDatagramSocket("packet capture", socket
),
87 fMonitoredDevice(NULL
),
90 fMonitor
.cookie
= this;
91 fMonitor
.receive
= _MonitorData
;
92 fMonitor
.event
= _MonitorEvent
;
96 LinkProtocol::~LinkProtocol()
98 if (fMonitoredDevice
!= NULL
) {
99 unregister_device_monitor(fMonitoredDevice
->device
, &fMonitor
);
100 put_device_interface(fMonitoredDevice
);
107 LinkProtocol::StartMonitoring(const char* deviceName
)
109 MutexLocker
locker(fLock
);
111 if (fMonitoredDevice
!= NULL
)
114 net_device_interface
* interface
= get_device_interface(deviceName
);
115 if (interface
== NULL
)
116 return B_DEVICE_NOT_FOUND
;
118 status_t status
= register_device_monitor(interface
->device
, &fMonitor
);
120 put_device_interface(interface
);
124 fMonitoredDevice
= interface
;
130 LinkProtocol::StopMonitoring(const char* deviceName
)
132 MutexLocker
locker(fLock
);
134 if (fMonitoredDevice
== NULL
135 || strcmp(fMonitoredDevice
->device
->name
, deviceName
) != 0)
138 return _Unregister();
143 LinkProtocol::Bind(const sockaddr
* address
)
145 // Only root is allowed to bind to a link layer interface
146 if (address
== NULL
|| geteuid() != 0)
147 return B_NOT_ALLOWED
;
149 MutexLocker
locker(fLock
);
151 if (fMonitoredDevice
!= NULL
)
154 Interface
* interface
= get_interface_for_link(sDomain
, address
);
155 if (interface
== NULL
)
158 net_device_interface
* boundTo
159 = acquire_device_interface(interface
->DeviceInterface());
161 interface
->ReleaseReference();
166 sockaddr_dl
& linkAddress
= *(sockaddr_dl
*)address
;
168 if (linkAddress
.sdl_type
!= 0) {
169 fBoundType
= B_NET_FRAME_TYPE(linkAddress
.sdl_type
,
170 ntohs(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
175 status_t status
= register_device_handler(boundTo
->device
, fBoundType
,
176 &LinkProtocol::_ReceiveData
, this);
182 fBoundToDevice
= boundTo
;
183 socket
->bound_to_device
= boundTo
->device
->index
;
185 memcpy(&socket
->address
, address
, sizeof(struct sockaddr_storage
));
186 socket
->address
.ss_len
= sizeof(struct sockaddr_storage
);
193 LinkProtocol::Unbind()
195 MutexLocker
locker(fLock
);
197 if (fBoundToDevice
== NULL
)
200 unregister_device_handler(fBoundToDevice
->device
, fBoundType
);
201 put_device_interface(fBoundToDevice
);
203 socket
->bound_to_device
= 0;
204 socket
->address
.ss_len
= 0;
212 MutexLocker
locker(fLock
);
217 return fBoundToDevice
->device
->mtu
;
222 LinkProtocol::SocketStatus(bool peek
) const
224 if (fMonitoredDevice
== NULL
&& !IsBound())
225 return B_DEVICE_NOT_FOUND
;
227 return LocalDatagramSocket::SocketStatus(peek
);
232 LinkProtocol::_Unregister()
234 if (fMonitoredDevice
== NULL
)
237 status_t status
= unregister_device_monitor(fMonitoredDevice
->device
,
239 put_device_interface(fMonitoredDevice
);
240 fMonitoredDevice
= NULL
;
247 LinkProtocol::_MonitorData(net_device_monitor
* monitor
, net_buffer
* packet
)
249 return ((LinkProtocol
*)monitor
->cookie
)->EnqueueClone(packet
);
254 LinkProtocol::_MonitorEvent(net_device_monitor
* monitor
, int32 event
)
256 LinkProtocol
* protocol
= (LinkProtocol
*)monitor
->cookie
;
258 if (event
== B_DEVICE_GOING_DOWN
) {
259 MutexLocker
_(protocol
->fLock
);
261 protocol
->_Unregister();
262 if (protocol
->IsEmpty()) {
264 notify_socket(protocol
->socket
, B_SELECT_READ
, B_DEVICE_NOT_FOUND
);
271 LinkProtocol::_ReceiveData(void* cookie
, net_device
* device
, net_buffer
* buffer
)
273 LinkProtocol
* protocol
= (LinkProtocol
*)cookie
;
275 return protocol
->Enqueue(buffer
);
283 user_request_get_device_interface(void* value
, struct ifreq
& request
,
284 net_device_interface
*& interface
)
286 if (user_memcpy(&request
, value
, IF_NAMESIZE
) < B_OK
)
289 interface
= get_device_interface(request
.ifr_name
);
294 // #pragma mark - net_protocol module
298 link_init_protocol(net_socket
* socket
)
300 LinkProtocol
* protocol
= new (std::nothrow
) LinkProtocol(socket
);
301 if (protocol
!= NULL
&& protocol
->InitCheck() < B_OK
) {
311 link_uninit_protocol(net_protocol
* protocol
)
313 delete (LinkProtocol
*)protocol
;
319 link_open(net_protocol
* protocol
)
326 link_close(net_protocol
* protocol
)
333 link_free(net_protocol
* protocol
)
340 link_connect(net_protocol
* protocol
, const struct sockaddr
* address
)
342 return B_NOT_SUPPORTED
;
347 link_accept(net_protocol
* protocol
, struct net_socket
** _acceptedSocket
)
349 return B_NOT_SUPPORTED
;
354 link_control(net_protocol
* _protocol
, int level
, int option
, void* value
,
357 LinkProtocol
* protocol
= (LinkProtocol
*)_protocol
;
362 // get index of interface
363 net_device_interface
* interface
;
364 struct ifreq request
;
365 if (!user_request_get_device_interface(value
, request
, interface
))
366 return B_BAD_ADDRESS
;
368 if (interface
!= NULL
) {
369 request
.ifr_index
= interface
->device
->index
;
370 put_device_interface(interface
);
372 request
.ifr_index
= 0;
374 return user_memcpy(value
, &request
, sizeof(struct ifreq
));
378 // get name of interface via index
379 struct ifreq request
;
380 if (user_memcpy(&request
, value
, sizeof(struct ifreq
)) < B_OK
)
381 return B_BAD_ADDRESS
;
383 net_device_interface
* interface
384 = get_device_interface(request
.ifr_index
);
385 if (interface
== NULL
)
386 return B_DEVICE_NOT_FOUND
;
388 strlcpy(request
.ifr_name
, interface
->device
->name
, IF_NAMESIZE
);
389 put_device_interface(interface
);
391 return user_memcpy(value
, &request
, sizeof(struct ifreq
));
396 // count number of interfaces
397 struct ifconf config
;
398 config
.ifc_value
= count_device_interfaces();
400 return user_memcpy(value
, &config
, sizeof(struct ifconf
));
405 // retrieve available interfaces
406 struct ifconf config
;
407 if (user_memcpy(&config
, value
, sizeof(struct ifconf
)) < B_OK
)
408 return B_BAD_ADDRESS
;
410 status_t result
= list_device_interfaces(config
.ifc_buf
,
411 (size_t*)&config
.ifc_len
);
415 return user_memcpy(value
, &config
, sizeof(struct ifconf
));
420 // get address of interface
421 net_device_interface
* interface
;
422 struct ifreq request
;
423 if (!user_request_get_device_interface(value
, request
, interface
))
424 return B_BAD_ADDRESS
;
426 if (interface
== NULL
)
427 return B_DEVICE_NOT_FOUND
;
429 sockaddr_storage address
;
430 get_device_interface_address(interface
, (sockaddr
*)&address
);
431 put_device_interface(interface
);
433 return user_memcpy(&((struct ifreq
*)value
)->ifr_addr
,
434 &address
, address
.ss_len
);
439 // get flags of interface
440 net_device_interface
* interface
;
441 struct ifreq request
;
442 if (!user_request_get_device_interface(value
, request
, interface
))
443 return B_BAD_ADDRESS
;
445 if (interface
== NULL
)
446 return B_DEVICE_NOT_FOUND
;
448 request
.ifr_flags
= interface
->device
->flags
;
449 put_device_interface(interface
);
451 return user_memcpy(&((struct ifreq
*)value
)->ifr_flags
,
452 &request
.ifr_flags
, sizeof(request
.ifr_flags
));
458 if (*_length
< sizeof(ifmediareq
))
461 net_device_interface
* interface
;
462 struct ifmediareq request
;
463 if (!user_request_get_device_interface(value
, (ifreq
&)request
,
465 return B_BAD_ADDRESS
;
467 if (interface
== NULL
)
468 return B_DEVICE_NOT_FOUND
;
470 if (user_memcpy(&request
, value
, sizeof(ifmediareq
)) != B_OK
) {
471 put_device_interface(interface
);
472 return B_BAD_ADDRESS
;
476 if (interface
->device
->module
->control(interface
->device
,
477 SIOCGIFMEDIA
, &request
,
478 sizeof(struct ifmediareq
)) != B_OK
) {
479 memset(&request
, 0, sizeof(struct ifmediareq
));
480 request
.ifm_active
= request
.ifm_current
481 = interface
->device
->media
;
483 put_device_interface(interface
);
485 return user_memcpy(value
, &request
, sizeof(struct ifmediareq
));
490 // Only root is allowed to capture packets
492 return B_NOT_ALLOWED
;
494 struct ifreq request
;
495 if (user_memcpy(&request
, value
, IF_NAMESIZE
) != B_OK
)
496 return B_BAD_ADDRESS
;
498 return protocol
->StartMonitoring(request
.ifr_name
);
503 struct ifreq request
;
504 if (user_memcpy(&request
, value
, IF_NAMESIZE
) != B_OK
)
505 return B_BAD_ADDRESS
;
507 return protocol
->StopMonitoring(request
.ifr_name
);
511 return gNetDatalinkModule
.control(sDomain
, option
, value
, _length
);
516 link_getsockopt(net_protocol
* protocol
, int level
, int option
, void* value
,
519 if (protocol
->next
!= NULL
) {
520 return protocol
->next
->module
->getsockopt(protocol
, level
, option
,
524 return gNetSocketModule
.get_option(protocol
->socket
, level
, option
, value
,
530 link_setsockopt(net_protocol
* protocol
, int level
, int option
,
531 const void* value
, int length
)
533 if (protocol
->next
!= NULL
) {
534 return protocol
->next
->module
->setsockopt(protocol
, level
, option
,
538 return gNetSocketModule
.set_option(protocol
->socket
, level
, option
,
544 link_bind(net_protocol
* _protocol
, const struct sockaddr
* address
)
546 LinkProtocol
* protocol
= (LinkProtocol
*)_protocol
;
547 return protocol
->Bind(address
);
552 link_unbind(net_protocol
* _protocol
, struct sockaddr
* address
)
554 LinkProtocol
* protocol
= (LinkProtocol
*)_protocol
;
555 return protocol
->Unbind();
560 link_listen(net_protocol
* protocol
, int count
)
562 return B_NOT_SUPPORTED
;
567 link_shutdown(net_protocol
* protocol
, int direction
)
569 return B_NOT_SUPPORTED
;
574 link_send_data(net_protocol
* protocol
, net_buffer
* buffer
)
576 return gNetDatalinkModule
.send_data(protocol
, sDomain
, buffer
);
581 link_send_routed_data(net_protocol
* protocol
, struct net_route
* route
,
584 if (buffer
->destination
->sa_family
!= buffer
->source
->sa_family
585 || buffer
->destination
->sa_family
!= AF_LINK
)
588 // The datalink layer will take care of the framing
590 return gNetDatalinkModule
.send_routed_data(route
, buffer
);
595 link_send_avail(net_protocol
* _protocol
)
597 LinkProtocol
* protocol
= (LinkProtocol
*)_protocol
;
598 if (!protocol
->IsBound())
601 return protocol
->socket
->send
.buffer_size
;
606 link_read_data(net_protocol
* protocol
, size_t numBytes
, uint32 flags
,
607 net_buffer
** _buffer
)
609 return ((LinkProtocol
*)protocol
)->Dequeue(flags
, _buffer
);
614 link_read_avail(net_protocol
* protocol
)
616 return ((LinkProtocol
*)protocol
)->AvailableData();
620 static struct net_domain
*
621 link_get_domain(net_protocol
* protocol
)
628 link_get_mtu(net_protocol
* _protocol
, const struct sockaddr
* address
)
630 LinkProtocol
* protocol
= (LinkProtocol
*)_protocol
;
631 return protocol
->MTU();
636 link_receive_data(net_buffer
* buffer
)
638 // We never receive any data this way
644 link_error_received(net_error error
, net_buffer
* data
)
646 // We don't do any error processing
652 link_error_reply(net_protocol
* protocol
, net_buffer
* cause
, net_error error
,
653 net_error_data
* errorData
)
655 // We don't do any error processing
661 link_std_ops(int32 op
, ...)
665 return register_domain(AF_LINK
, "link", NULL
, NULL
, &sDomain
);
667 case B_MODULE_UNINIT
:
668 unregister_domain(sDomain
);
683 register_domain_protocols(AF_LINK
, SOCK_DGRAM
, 0, "network/stack/link/v1",
686 // TODO: this should actually be registered for all types (besides local)
687 register_domain_datalink_protocols(AF_LINK
, IFT_ETHER
,
688 "network/datalink_protocols/ethernet_frame/v1",
693 net_protocol_module_info gLinkModule
= {
695 "network/stack/link/v1",
699 NET_PROTOCOL_ATOMIC_MESSAGES
,
702 link_uninit_protocol
,
716 link_send_routed_data
,
723 NULL
, // deliver_data
726 NULL
, // add_ancillary_data()
727 NULL
, // process_ancillary_data()
728 NULL
, // process_ancillary_data_no_container()
729 NULL
, // send_data_no_buffer()
730 NULL
// read_data_no_buffer()