1 /*
2 * Copyright 2006-2013, 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:
Stack()39 static net_stack_module_info* Stack() { return &gNetStackModule; }
Buffer()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();
IsBound() const55 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
LinkProtocol(net_socket * socket)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
~LinkProtocol()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
StartMonitoring(const char * deviceName)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
StopMonitoring(const char * deviceName)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
Bind(const sockaddr * address)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 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
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 memcpy(&socket->address, address, sizeof(struct sockaddr_storage));
186 socket->address.ss_len = sizeof(struct sockaddr_storage);
187
188 return B_OK;
189 }
190
191
192 status_t
Unbind()193 LinkProtocol::Unbind()
194 {
195 MutexLocker locker(fLock);
196
197 if (fBoundToDevice == NULL)
198 return B_BAD_VALUE;
199
200 unregister_device_handler(fBoundToDevice->device, fBoundType);
201 put_device_interface(fBoundToDevice);
202
203 socket->bound_to_device = 0;
204 socket->address.ss_len = 0;
205 return B_OK;
206 }
207
208
209 size_t
MTU()210 LinkProtocol::MTU()
211 {
212 MutexLocker locker(fLock);
213
214 if (!IsBound())
215 return 0;
216
217 return fBoundToDevice->device->mtu;
218 }
219
220
221 status_t
SocketStatus(bool peek) const222 LinkProtocol::SocketStatus(bool peek) const
223 {
224 if (fMonitoredDevice == NULL && !IsBound())
225 return B_DEVICE_NOT_FOUND;
226
227 return LocalDatagramSocket::SocketStatus(peek);
228 }
229
230
231 status_t
_Unregister()232 LinkProtocol::_Unregister()
233 {
234 if (fMonitoredDevice == NULL)
235 return B_BAD_VALUE;
236
237 status_t status = unregister_device_monitor(fMonitoredDevice->device,
238 &fMonitor);
239 put_device_interface(fMonitoredDevice);
240 fMonitoredDevice = NULL;
241
242 return status;
243 }
244
245
246 /*static*/ status_t
_MonitorData(net_device_monitor * monitor,net_buffer * packet)247 LinkProtocol::_MonitorData(net_device_monitor* monitor, net_buffer* packet)
248 {
249 return ((LinkProtocol*)monitor->cookie)->EnqueueClone(packet);
250 }
251
252
253 /*static*/ void
_MonitorEvent(net_device_monitor * monitor,int32 event)254 LinkProtocol::_MonitorEvent(net_device_monitor* monitor, int32 event)
255 {
256 LinkProtocol* protocol = (LinkProtocol*)monitor->cookie;
257
258 if (event == B_DEVICE_GOING_DOWN) {
259 MutexLocker _(protocol->fLock);
260
261 protocol->_Unregister();
262 if (protocol->IsEmpty()) {
263 protocol->WakeAll();
264 notify_socket(protocol->socket, B_SELECT_READ, B_DEVICE_NOT_FOUND);
265 }
266 }
267 }
268
269
270 /*static*/ status_t
_ReceiveData(void * cookie,net_device * device,net_buffer * buffer)271 LinkProtocol::_ReceiveData(void* cookie, net_device* device, net_buffer* buffer)
272 {
273 LinkProtocol* protocol = (LinkProtocol*)cookie;
274
275 return protocol->Enqueue(buffer);
276 }
277
278
279 // #pragma mark -
280
281
282 static bool
user_request_get_device_interface(void * value,struct ifreq & request,net_device_interface * & interface)283 user_request_get_device_interface(void* value, struct ifreq& request,
284 net_device_interface*& interface)
285 {
286 if (user_memcpy(&request, value, IF_NAMESIZE) < B_OK)
287 return false;
288
289 interface = get_device_interface(request.ifr_name);
290 return true;
291 }
292
293
294 // #pragma mark - net_protocol module
295
296
297 static net_protocol*
link_init_protocol(net_socket * socket)298 link_init_protocol(net_socket* socket)
299 {
300 LinkProtocol* protocol = new (std::nothrow) LinkProtocol(socket);
301 if (protocol != NULL && protocol->InitCheck() < B_OK) {
302 delete protocol;
303 return NULL;
304 }
305
306 return protocol;
307 }
308
309
310 static status_t
link_uninit_protocol(net_protocol * protocol)311 link_uninit_protocol(net_protocol* protocol)
312 {
313 delete (LinkProtocol*)protocol;
314 return B_OK;
315 }
316
317
318 static status_t
link_open(net_protocol * protocol)319 link_open(net_protocol* protocol)
320 {
321 return B_OK;
322 }
323
324
325 static status_t
link_close(net_protocol * protocol)326 link_close(net_protocol* protocol)
327 {
328 return B_OK;
329 }
330
331
332 static status_t
link_free(net_protocol * protocol)333 link_free(net_protocol* protocol)
334 {
335 return B_OK;
336 }
337
338
339 static status_t
link_connect(net_protocol * protocol,const struct sockaddr * address)340 link_connect(net_protocol* protocol, const struct sockaddr* address)
341 {
342 return B_NOT_SUPPORTED;
343 }
344
345
346 static status_t
link_accept(net_protocol * protocol,struct net_socket ** _acceptedSocket)347 link_accept(net_protocol* protocol, struct net_socket** _acceptedSocket)
348 {
349 return B_NOT_SUPPORTED;
350 }
351
352
353 static status_t
link_control(net_protocol * _protocol,int level,int option,void * value,size_t * _length)354 link_control(net_protocol* _protocol, int level, int option, void* value,
355 size_t* _length)
356 {
357 LinkProtocol* protocol = (LinkProtocol*)_protocol;
358
359 switch (option) {
360 case SIOCGIFINDEX:
361 {
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;
367
368 if (interface != NULL) {
369 request.ifr_index = interface->device->index;
370 put_device_interface(interface);
371 } else
372 request.ifr_index = 0;
373
374 return user_memcpy(value, &request, sizeof(struct ifreq));
375 }
376 case SIOCGIFNAME:
377 {
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;
382
383 net_device_interface* interface
384 = get_device_interface(request.ifr_index);
385 if (interface == NULL)
386 return B_DEVICE_NOT_FOUND;
387
388 strlcpy(request.ifr_name, interface->device->name, IF_NAMESIZE);
389 put_device_interface(interface);
390
391 return user_memcpy(value, &request, sizeof(struct ifreq));
392 }
393
394 case SIOCGIFCOUNT:
395 {
396 // count number of interfaces
397 struct ifconf config;
398 config.ifc_value = count_device_interfaces();
399
400 return user_memcpy(value, &config, sizeof(struct ifconf));
401 }
402
403 case SIOCGIFCONF:
404 {
405 // retrieve available interfaces
406 struct ifconf config;
407 if (user_memcpy(&config, value, sizeof(struct ifconf)) < B_OK)
408 return B_BAD_ADDRESS;
409
410 status_t result = list_device_interfaces(config.ifc_buf,
411 (size_t*)&config.ifc_len);
412 if (result != B_OK)
413 return result;
414
415 return user_memcpy(value, &config, sizeof(struct ifconf));
416 }
417
418 case SIOCGIFADDR:
419 {
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;
425
426 if (interface == NULL)
427 return B_DEVICE_NOT_FOUND;
428
429 sockaddr_storage address;
430 get_device_interface_address(interface, (sockaddr*)&address);
431 put_device_interface(interface);
432
433 return user_memcpy(&((struct ifreq*)value)->ifr_addr,
434 &address, address.ss_len);
435 }
436
437 case SIOCGIFFLAGS:
438 {
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;
444
445 if (interface == NULL)
446 return B_DEVICE_NOT_FOUND;
447
448 request.ifr_flags = interface->device->flags;
449 put_device_interface(interface);
450
451 return user_memcpy(&((struct ifreq*)value)->ifr_flags,
452 &request.ifr_flags, sizeof(request.ifr_flags));
453 }
454
455 case SIOCGIFMEDIA:
456 {
457 // get media
458 const size_t copylen = offsetof(ifreq, ifr_media) + sizeof(ifreq::ifr_media);
459 if (*_length > 0 && *_length < copylen)
460 return B_BAD_VALUE;
461
462 net_device_interface* interface;
463 struct ifreq request;
464 if (!user_request_get_device_interface(value, request, interface))
465 return B_BAD_ADDRESS;
466 if (interface == NULL)
467 return B_DEVICE_NOT_FOUND;
468
469 request.ifr_media = interface->device->media;
470
471 put_device_interface(interface);
472
473 return user_memcpy(value, &request, copylen);
474 }
475
476 case SIOCSPACKETCAP:
477 {
478 // Only root is allowed to capture packets
479 if (geteuid() != 0)
480 return B_NOT_ALLOWED;
481
482 struct ifreq request;
483 if (user_memcpy(&request, value, IF_NAMESIZE) != B_OK)
484 return B_BAD_ADDRESS;
485
486 return protocol->StartMonitoring(request.ifr_name);
487 }
488
489 case SIOCCPACKETCAP:
490 {
491 struct ifreq request;
492 if (user_memcpy(&request, value, IF_NAMESIZE) != B_OK)
493 return B_BAD_ADDRESS;
494
495 return protocol->StopMonitoring(request.ifr_name);
496 }
497 }
498
499 return gNetDatalinkModule.control(sDomain, option, value, _length);
500 }
501
502
503 static status_t
link_getsockopt(net_protocol * protocol,int level,int option,void * value,int * length)504 link_getsockopt(net_protocol* protocol, int level, int option, void* value,
505 int* length)
506 {
507 if (protocol->next != NULL) {
508 return protocol->next->module->getsockopt(protocol, level, option,
509 value, length);
510 }
511
512 return gNetSocketModule.get_option(protocol->socket, level, option, value,
513 length);
514 }
515
516
517 static status_t
link_setsockopt(net_protocol * protocol,int level,int option,const void * value,int length)518 link_setsockopt(net_protocol* protocol, int level, int option,
519 const void* value, int length)
520 {
521 if (protocol->next != NULL) {
522 return protocol->next->module->setsockopt(protocol, level, option,
523 value, length);
524 }
525
526 return gNetSocketModule.set_option(protocol->socket, level, option,
527 value, length);
528 }
529
530
531 static status_t
link_bind(net_protocol * _protocol,const struct sockaddr * address)532 link_bind(net_protocol* _protocol, const struct sockaddr* address)
533 {
534 LinkProtocol* protocol = (LinkProtocol*)_protocol;
535 return protocol->Bind(address);
536 }
537
538
539 static status_t
link_unbind(net_protocol * _protocol,struct sockaddr * address)540 link_unbind(net_protocol* _protocol, struct sockaddr* address)
541 {
542 LinkProtocol* protocol = (LinkProtocol*)_protocol;
543 return protocol->Unbind();
544 }
545
546
547 static status_t
link_listen(net_protocol * protocol,int count)548 link_listen(net_protocol* protocol, int count)
549 {
550 return B_NOT_SUPPORTED;
551 }
552
553
554 static status_t
link_shutdown(net_protocol * protocol,int direction)555 link_shutdown(net_protocol* protocol, int direction)
556 {
557 return B_NOT_SUPPORTED;
558 }
559
560
561 static status_t
link_send_data(net_protocol * protocol,net_buffer * buffer)562 link_send_data(net_protocol* protocol, net_buffer* buffer)
563 {
564 return gNetDatalinkModule.send_data(protocol, sDomain, buffer);
565 }
566
567
568 static status_t
link_send_routed_data(net_protocol * protocol,struct net_route * route,net_buffer * buffer)569 link_send_routed_data(net_protocol* protocol, struct net_route* route,
570 net_buffer* buffer)
571 {
572 if (buffer->destination->sa_family != buffer->source->sa_family
573 || buffer->destination->sa_family != AF_LINK)
574 return B_BAD_VALUE;
575
576 // The datalink layer will take care of the framing
577
578 return gNetDatalinkModule.send_routed_data(route, buffer);
579 }
580
581
582 static ssize_t
link_send_avail(net_protocol * _protocol)583 link_send_avail(net_protocol* _protocol)
584 {
585 LinkProtocol* protocol = (LinkProtocol*)_protocol;
586 if (!protocol->IsBound())
587 return B_ERROR;
588
589 return protocol->socket->send.buffer_size;
590 }
591
592
593 static status_t
link_read_data(net_protocol * protocol,size_t numBytes,uint32 flags,net_buffer ** _buffer)594 link_read_data(net_protocol* protocol, size_t numBytes, uint32 flags,
595 net_buffer** _buffer)
596 {
597 return ((LinkProtocol*)protocol)->Dequeue(flags, _buffer);
598 }
599
600
601 static ssize_t
link_read_avail(net_protocol * protocol)602 link_read_avail(net_protocol* protocol)
603 {
604 return ((LinkProtocol*)protocol)->AvailableData();
605 }
606
607
608 static struct net_domain*
link_get_domain(net_protocol * protocol)609 link_get_domain(net_protocol* protocol)
610 {
611 return sDomain;
612 }
613
614
615 static size_t
link_get_mtu(net_protocol * _protocol,const struct sockaddr * address)616 link_get_mtu(net_protocol* _protocol, const struct sockaddr* address)
617 {
618 LinkProtocol* protocol = (LinkProtocol*)_protocol;
619 return protocol->MTU();
620 }
621
622
623 static status_t
link_receive_data(net_buffer * buffer)624 link_receive_data(net_buffer* buffer)
625 {
626 // We never receive any data this way
627 return B_ERROR;
628 }
629
630
631 static status_t
link_error_received(net_error error,net_buffer * data)632 link_error_received(net_error error, net_buffer* data)
633 {
634 // We don't do any error processing
635 return B_ERROR;
636 }
637
638
639 static status_t
link_error_reply(net_protocol * protocol,net_buffer * cause,net_error error,net_error_data * errorData)640 link_error_reply(net_protocol* protocol, net_buffer* cause, net_error error,
641 net_error_data* errorData)
642 {
643 // We don't do any error processing
644 return B_ERROR;
645 }
646
647
648 static status_t
link_std_ops(int32 op,...)649 link_std_ops(int32 op, ...)
650 {
651 switch (op) {
652 case B_MODULE_INIT:
653 return register_domain(AF_LINK, "link", NULL, NULL, &sDomain);
654
655 case B_MODULE_UNINIT:
656 unregister_domain(sDomain);
657 return B_OK;
658
659 default:
660 return B_ERROR;
661 }
662 }
663
664
665 // #pragma mark -
666
667
668 void
link_init()669 link_init()
670 {
671 register_domain_protocols(AF_LINK, SOCK_DGRAM, 0, "network/stack/link/v1",
672 NULL);
673
674 // TODO: this should actually be registered for all types (besides local)
675 register_domain_datalink_protocols(AF_LINK, IFT_ETHER,
676 "network/datalink_protocols/ethernet_frame/v1",
677 NULL);
678 }
679
680
681 net_protocol_module_info gLinkModule = {
682 {
683 "network/stack/link/v1",
684 0,
685 link_std_ops
686 },
687 NET_PROTOCOL_ATOMIC_MESSAGES,
688
689 link_init_protocol,
690 link_uninit_protocol,
691 link_open,
692 link_close,
693 link_free,
694 link_connect,
695 link_accept,
696 link_control,
697 link_getsockopt,
698 link_setsockopt,
699 link_bind,
700 link_unbind,
701 link_listen,
702 link_shutdown,
703 link_send_data,
704 link_send_routed_data,
705 link_send_avail,
706 link_read_data,
707 link_read_avail,
708 link_get_domain,
709 link_get_mtu,
710 link_receive_data,
711 NULL, // deliver_data
712 link_error_received,
713 link_error_reply,
714 NULL, // add_ancillary_data()
715 NULL, // process_ancillary_data()
716 NULL, // process_ancillary_data_no_container()
717 NULL, // send_data_no_buffer()
718 NULL // read_data_no_buffer()
719 };
720