1c22d69bfSAxel Dörfler /* 24ae58630SAxel Dörfler * Copyright 2006-2011, Haiku, Inc. All Rights Reserved. 3c22d69bfSAxel Dörfler * Distributed under the terms of the MIT License. 4c22d69bfSAxel Dörfler * 5c22d69bfSAxel Dörfler * Authors: 6c22d69bfSAxel Dörfler * Axel Dörfler, axeld@pinc-software.de 7c22d69bfSAxel Dörfler */ 8c22d69bfSAxel Dörfler 9c22d69bfSAxel Dörfler 10c22d69bfSAxel Dörfler #include "interfaces.h" 11c22d69bfSAxel Dörfler 12c22d69bfSAxel Dörfler #include <net/if_dl.h> 13c22d69bfSAxel Dörfler #include <new> 14a1deb55eSHugo Santos #include <stdio.h> 15c22d69bfSAxel Dörfler #include <stdlib.h> 16c22d69bfSAxel Dörfler #include <string.h> 1761729d93SAxel Dörfler #include <sys/sockio.h> 1861729d93SAxel Dörfler 1961729d93SAxel Dörfler #include <KernelExport.h> 2061729d93SAxel Dörfler 2161729d93SAxel Dörfler #include <net_device.h> 22b0cf274aSAxel Dörfler #include <NetUtilities.h> 2361729d93SAxel Dörfler 2461729d93SAxel Dörfler #include "device_interfaces.h" 2561729d93SAxel Dörfler #include "domains.h" 2661729d93SAxel Dörfler #include "stack_private.h" 2761729d93SAxel Dörfler #include "utility.h" 28c22d69bfSAxel Dörfler 29c22d69bfSAxel Dörfler 3061729d93SAxel Dörfler //#define TRACE_INTERFACES 31c22d69bfSAxel Dörfler #ifdef TRACE_INTERFACES 3261729d93SAxel Dörfler # define TRACE(x...) dprintf(STACK_DEBUG_PREFIX x) 33c22d69bfSAxel Dörfler #else 3461729d93SAxel Dörfler # define TRACE(x...) ; 35c22d69bfSAxel Dörfler #endif 36c22d69bfSAxel Dörfler 3761729d93SAxel Dörfler 3861729d93SAxel Dörfler struct AddressHashDefinition { 3961729d93SAxel Dörfler typedef const sockaddr* KeyType; 4061729d93SAxel Dörfler typedef InterfaceAddress ValueType; 4161729d93SAxel Dörfler 4261729d93SAxel Dörfler AddressHashDefinition() 4361729d93SAxel Dörfler { 4461729d93SAxel Dörfler } 4561729d93SAxel Dörfler 4661729d93SAxel Dörfler size_t HashKey(const KeyType& key) const 4761729d93SAxel Dörfler { 4861729d93SAxel Dörfler net_domain* domain = get_domain(key->sa_family); 4961729d93SAxel Dörfler if (domain == NULL) 5061729d93SAxel Dörfler return 0; 5161729d93SAxel Dörfler 5261729d93SAxel Dörfler return domain->address_module->hash_address(key, false); 5361729d93SAxel Dörfler } 5461729d93SAxel Dörfler 5561729d93SAxel Dörfler size_t Hash(InterfaceAddress* address) const 5661729d93SAxel Dörfler { 574ae58630SAxel Dörfler return address->domain->address_module->hash_address(address->local, 584ae58630SAxel Dörfler false); 5961729d93SAxel Dörfler } 6061729d93SAxel Dörfler 6161729d93SAxel Dörfler bool Compare(const KeyType& key, InterfaceAddress* address) const 6261729d93SAxel Dörfler { 6361729d93SAxel Dörfler if (address->local == NULL) 6461729d93SAxel Dörfler return key->sa_family == AF_UNSPEC; 6561729d93SAxel Dörfler 6661729d93SAxel Dörfler if (address->local->sa_family != key->sa_family) 6761729d93SAxel Dörfler return false; 6861729d93SAxel Dörfler 6961729d93SAxel Dörfler return address->domain->address_module->equal_addresses(key, 7061729d93SAxel Dörfler address->local); 7161729d93SAxel Dörfler } 7261729d93SAxel Dörfler 7361729d93SAxel Dörfler InterfaceAddress*& GetLink(InterfaceAddress* address) const 7461729d93SAxel Dörfler { 7561729d93SAxel Dörfler return address->HashTableLink(); 7661729d93SAxel Dörfler } 7761729d93SAxel Dörfler }; 7861729d93SAxel Dörfler 794ae58630SAxel Dörfler typedef BOpenHashTable<AddressHashDefinition, true, false> AddressTable; 80a3ec278aSAxel Dörfler 81c22d69bfSAxel Dörfler 828a6337d0SStefano Ceccherini static recursive_lock sLock; 8361729d93SAxel Dörfler static InterfaceList sInterfaces; 8461729d93SAxel Dörfler static mutex sHashLock; 8561729d93SAxel Dörfler static AddressTable sAddressTable; 86c22d69bfSAxel Dörfler static uint32 sInterfaceIndex; 879261b129SHugo Santos 889261b129SHugo Santos 89ec4fb0f4SAxel Dörfler #if 0 90ec4fb0f4SAxel Dörfler //! For debugging purposes only 91ec4fb0f4SAxel Dörfler void 92ec4fb0f4SAxel Dörfler dump_interface_refs(void) 93ec4fb0f4SAxel Dörfler { 948a6337d0SStefano Ceccherini RecursiveLocker locker(sLock); 95ec4fb0f4SAxel Dörfler 96ec4fb0f4SAxel Dörfler InterfaceList::Iterator iterator = sInterfaces.GetIterator(); 97ec4fb0f4SAxel Dörfler while (Interface* interface = iterator.Next()) { 98ec4fb0f4SAxel Dörfler dprintf("%p: %s, %ld\n", interface, interface->name, 99ec4fb0f4SAxel Dörfler interface->CountReferences()); 100ec4fb0f4SAxel Dörfler } 101ec4fb0f4SAxel Dörfler } 102ec4fb0f4SAxel Dörfler #endif 103ec4fb0f4SAxel Dörfler 104ec4fb0f4SAxel Dörfler 105a3ec278aSAxel Dörfler #if ENABLE_DEBUGGER_COMMANDS 106a3ec278aSAxel Dörfler 107a3ec278aSAxel Dörfler 108a3ec278aSAxel Dörfler static int 109a3ec278aSAxel Dörfler dump_interface(int argc, char** argv) 110a3ec278aSAxel Dörfler { 111a3ec278aSAxel Dörfler if (argc != 2) { 11211eccb54SAxel Dörfler kprintf("usage: %s [name|address]\n", argv[0]); 113a3ec278aSAxel Dörfler return 0; 114a3ec278aSAxel Dörfler } 115a3ec278aSAxel Dörfler 11611eccb54SAxel Dörfler Interface* interface = NULL; 11711eccb54SAxel Dörfler 11811eccb54SAxel Dörfler InterfaceList::Iterator iterator = sInterfaces.GetIterator(); 11911eccb54SAxel Dörfler while ((interface = iterator.Next()) != NULL) { 12011eccb54SAxel Dörfler if (!strcmp(argv[1], interface->name)) 12111eccb54SAxel Dörfler break; 12211eccb54SAxel Dörfler } 12311eccb54SAxel Dörfler 12411eccb54SAxel Dörfler if (interface == NULL) 12511eccb54SAxel Dörfler interface = (Interface*)parse_expression(argv[1]); 12611eccb54SAxel Dörfler 12761729d93SAxel Dörfler interface->Dump(); 128a3ec278aSAxel Dörfler 129a3ec278aSAxel Dörfler return 0; 130a3ec278aSAxel Dörfler } 131a3ec278aSAxel Dörfler 132a3ec278aSAxel Dörfler 133a3ec278aSAxel Dörfler static int 13461729d93SAxel Dörfler dump_interfaces(int argc, char** argv) 13561729d93SAxel Dörfler { 13661729d93SAxel Dörfler InterfaceList::Iterator iterator = sInterfaces.GetIterator(); 13761729d93SAxel Dörfler while (Interface* interface = iterator.Next()) { 13861729d93SAxel Dörfler kprintf("%p %s\n", interface, interface->name); 13961729d93SAxel Dörfler } 14061729d93SAxel Dörfler return 0; 14161729d93SAxel Dörfler } 14261729d93SAxel Dörfler 14361729d93SAxel Dörfler 14461729d93SAxel Dörfler static int 14561729d93SAxel Dörfler dump_local(int argc, char** argv) 14661729d93SAxel Dörfler { 14761729d93SAxel Dörfler AddressTable::Iterator iterator = sAddressTable.GetIterator(); 14861729d93SAxel Dörfler size_t i = 0; 14961729d93SAxel Dörfler while (InterfaceAddress* address = iterator.Next()) { 15061729d93SAxel Dörfler address->Dump(++i); 1516d796a84SAlex Smith dprintf(" hash: %" B_PRIu32 "\n", 15261729d93SAxel Dörfler address->domain->address_module->hash_address(address->local, 15361729d93SAxel Dörfler false)); 15461729d93SAxel Dörfler } 15561729d93SAxel Dörfler return 0; 15661729d93SAxel Dörfler } 15761729d93SAxel Dörfler 15861729d93SAxel Dörfler 15961729d93SAxel Dörfler static int 16061729d93SAxel Dörfler dump_route(int argc, char** argv) 161a3ec278aSAxel Dörfler { 162a3ec278aSAxel Dörfler if (argc != 2) { 163a3ec278aSAxel Dörfler kprintf("usage: %s [address]\n", argv[0]); 164a3ec278aSAxel Dörfler return 0; 165a3ec278aSAxel Dörfler } 166a3ec278aSAxel Dörfler 16761729d93SAxel Dörfler net_route* route = (net_route*)parse_expression(argv[1]); 16861729d93SAxel Dörfler kprintf("destination: %p\n", route->destination); 16961729d93SAxel Dörfler kprintf("mask: %p\n", route->mask); 17061729d93SAxel Dörfler kprintf("gateway: %p\n", route->gateway); 17161729d93SAxel Dörfler kprintf("flags: %" B_PRIx32 "\n", route->flags); 17261729d93SAxel Dörfler kprintf("mtu: %" B_PRIu32 "\n", route->mtu); 17361729d93SAxel Dörfler kprintf("interface address: %p\n", route->interface_address); 174a3ec278aSAxel Dörfler 17561729d93SAxel Dörfler if (route->interface_address != NULL) { 17661729d93SAxel Dörfler ((InterfaceAddress*)route->interface_address)->Dump(); 177a3ec278aSAxel Dörfler } 178a3ec278aSAxel Dörfler 179a3ec278aSAxel Dörfler return 0; 180a3ec278aSAxel Dörfler } 181a3ec278aSAxel Dörfler 182a3ec278aSAxel Dörfler 183a3ec278aSAxel Dörfler #endif // ENABLE_DEBUGGER_COMMANDS 184a3ec278aSAxel Dörfler 185a3ec278aSAxel Dörfler 18661729d93SAxel Dörfler InterfaceAddress::InterfaceAddress() 187c22d69bfSAxel Dörfler { 18861729d93SAxel Dörfler _Init(NULL, NULL); 18961729d93SAxel Dörfler } 190c22d69bfSAxel Dörfler 19161729d93SAxel Dörfler 19261729d93SAxel Dörfler InterfaceAddress::InterfaceAddress(net_interface* netInterface, 19361729d93SAxel Dörfler net_domain* netDomain) 19461729d93SAxel Dörfler { 19561729d93SAxel Dörfler _Init(netInterface, netDomain); 19661729d93SAxel Dörfler } 19761729d93SAxel Dörfler 19861729d93SAxel Dörfler 19961729d93SAxel Dörfler InterfaceAddress::~InterfaceAddress() 20061729d93SAxel Dörfler { 2012b1c0755SAxel Dörfler TRACE("InterfaceAddress %p: destructor\n", this); 2022b1c0755SAxel Dörfler 2032b1c0755SAxel Dörfler if (interface != NULL && (flags & IFAF_DIRECT_ADDRESS) == 0) 2042b1c0755SAxel Dörfler ((Interface*)interface)->ReleaseReference(); 20561729d93SAxel Dörfler } 20661729d93SAxel Dörfler 20761729d93SAxel Dörfler 20861729d93SAxel Dörfler status_t 20961729d93SAxel Dörfler InterfaceAddress::SetTo(const ifaliasreq& request) 21061729d93SAxel Dörfler { 21161729d93SAxel Dörfler status_t status = SetLocal((const sockaddr*)&request.ifra_addr); 21261729d93SAxel Dörfler if (status == B_OK) 21361729d93SAxel Dörfler status = SetDestination((const sockaddr*)&request.ifra_broadaddr); 21461729d93SAxel Dörfler if (status == B_OK) 21561729d93SAxel Dörfler status = SetMask((const sockaddr*)&request.ifra_mask); 21661729d93SAxel Dörfler 21761729d93SAxel Dörfler return status; 21861729d93SAxel Dörfler } 21961729d93SAxel Dörfler 22061729d93SAxel Dörfler 22161729d93SAxel Dörfler status_t 22261729d93SAxel Dörfler InterfaceAddress::SetLocal(const sockaddr* to) 22361729d93SAxel Dörfler { 22461729d93SAxel Dörfler return Set(&local, to); 22561729d93SAxel Dörfler } 22661729d93SAxel Dörfler 22761729d93SAxel Dörfler 22861729d93SAxel Dörfler status_t 22961729d93SAxel Dörfler InterfaceAddress::SetDestination(const sockaddr* to) 23061729d93SAxel Dörfler { 23161729d93SAxel Dörfler return Set(&destination, to); 23261729d93SAxel Dörfler } 23361729d93SAxel Dörfler 23461729d93SAxel Dörfler 23561729d93SAxel Dörfler status_t 23661729d93SAxel Dörfler InterfaceAddress::SetMask(const sockaddr* to) 23761729d93SAxel Dörfler { 23861729d93SAxel Dörfler return Set(&mask, to); 23961729d93SAxel Dörfler } 24061729d93SAxel Dörfler 24161729d93SAxel Dörfler 24261729d93SAxel Dörfler sockaddr** 24361729d93SAxel Dörfler InterfaceAddress::AddressFor(int32 option) 24461729d93SAxel Dörfler { 24561729d93SAxel Dörfler switch (option) { 24661729d93SAxel Dörfler case SIOCSIFADDR: 24761729d93SAxel Dörfler case SIOCGIFADDR: 2482b1c0755SAxel Dörfler case SIOCDIFADDR: 24961729d93SAxel Dörfler return &local; 25061729d93SAxel Dörfler 25161729d93SAxel Dörfler case SIOCSIFNETMASK: 25261729d93SAxel Dörfler case SIOCGIFNETMASK: 25361729d93SAxel Dörfler return &mask; 25461729d93SAxel Dörfler 25561729d93SAxel Dörfler case SIOCSIFBRDADDR: 25661729d93SAxel Dörfler case SIOCSIFDSTADDR: 25761729d93SAxel Dörfler case SIOCGIFBRDADDR: 25861729d93SAxel Dörfler case SIOCGIFDSTADDR: 25961729d93SAxel Dörfler return &destination; 26061729d93SAxel Dörfler 26161729d93SAxel Dörfler default: 26261729d93SAxel Dörfler return NULL; 26361729d93SAxel Dörfler } 26461729d93SAxel Dörfler } 26561729d93SAxel Dörfler 26661729d93SAxel Dörfler 2672b1c0755SAxel Dörfler /*! Adds the default routes that every interface address needs, ie. the local 2682b1c0755SAxel Dörfler host route, and one for the subnet (if set). 2692b1c0755SAxel Dörfler */ 2702b1c0755SAxel Dörfler void 2712b1c0755SAxel Dörfler InterfaceAddress::AddDefaultRoutes(int32 option) 2722b1c0755SAxel Dörfler { 2732b1c0755SAxel Dörfler net_route route; 2742b1c0755SAxel Dörfler route.destination = local; 2752b1c0755SAxel Dörfler route.gateway = NULL; 2762b1c0755SAxel Dörfler route.interface_address = this; 2772b1c0755SAxel Dörfler 2782b1c0755SAxel Dörfler if (mask != NULL && (option == SIOCSIFNETMASK || option == SIOCSIFADDR)) { 2792b1c0755SAxel Dörfler route.mask = mask; 2802b1c0755SAxel Dörfler route.flags = 0; 2812b1c0755SAxel Dörfler add_route(domain, &route); 2822b1c0755SAxel Dörfler } 2832b1c0755SAxel Dörfler 2842b1c0755SAxel Dörfler if (option == SIOCSIFADDR) { 2852b1c0755SAxel Dörfler route.mask = NULL; 2862b1c0755SAxel Dörfler route.flags = RTF_LOCAL | RTF_HOST; 2872b1c0755SAxel Dörfler add_route(domain, &route); 2882b1c0755SAxel Dörfler } 2892b1c0755SAxel Dörfler } 2902b1c0755SAxel Dörfler 2912b1c0755SAxel Dörfler 2922b1c0755SAxel Dörfler /*! Removes the default routes as set by AddDefaultRoutes() again. */ 2932b1c0755SAxel Dörfler void 2942b1c0755SAxel Dörfler InterfaceAddress::RemoveDefaultRoutes(int32 option) 2952b1c0755SAxel Dörfler { 2962b1c0755SAxel Dörfler net_route route; 2972b1c0755SAxel Dörfler route.destination = local; 2982b1c0755SAxel Dörfler route.gateway = NULL; 2992b1c0755SAxel Dörfler route.interface_address = this; 3002b1c0755SAxel Dörfler 3012b1c0755SAxel Dörfler if (mask != NULL && (option == SIOCSIFNETMASK || option == SIOCSIFADDR)) { 3022b1c0755SAxel Dörfler route.mask = mask; 3032b1c0755SAxel Dörfler route.flags = 0; 3042b1c0755SAxel Dörfler remove_route(domain, &route); 3052b1c0755SAxel Dörfler } 3062b1c0755SAxel Dörfler 3072b1c0755SAxel Dörfler if (option == SIOCSIFADDR) { 3082b1c0755SAxel Dörfler route.mask = NULL; 3092b1c0755SAxel Dörfler route.flags = RTF_LOCAL | RTF_HOST; 3102b1c0755SAxel Dörfler remove_route(domain, &route); 3112b1c0755SAxel Dörfler } 3122b1c0755SAxel Dörfler } 3132b1c0755SAxel Dörfler 3142b1c0755SAxel Dörfler 3154ae58630SAxel Dörfler bool 3164ae58630SAxel Dörfler InterfaceAddress::LocalIsDefined() const 3174ae58630SAxel Dörfler { 3184ae58630SAxel Dörfler return local != NULL && local->sa_family != AF_UNSPEC; 3194ae58630SAxel Dörfler } 3204ae58630SAxel Dörfler 3214ae58630SAxel Dörfler 32261729d93SAxel Dörfler #if ENABLE_DEBUGGER_COMMANDS 32361729d93SAxel Dörfler 32461729d93SAxel Dörfler 32561729d93SAxel Dörfler void 32661729d93SAxel Dörfler InterfaceAddress::Dump(size_t index, bool hideInterface) 32761729d93SAxel Dörfler { 32861729d93SAxel Dörfler if (index) 32961729d93SAxel Dörfler kprintf("%2zu. ", index); 33061729d93SAxel Dörfler else 33161729d93SAxel Dörfler kprintf(" "); 33261729d93SAxel Dörfler 33361729d93SAxel Dörfler if (!hideInterface) { 33461729d93SAxel Dörfler kprintf("interface: %p (%s)\n ", interface, 33561729d93SAxel Dörfler interface != NULL ? interface->name : "-"); 33661729d93SAxel Dörfler } 33761729d93SAxel Dörfler 33861729d93SAxel Dörfler kprintf("domain: %p (family %u)\n", domain, 33961729d93SAxel Dörfler domain != NULL ? domain->family : AF_UNSPEC); 34061729d93SAxel Dörfler 34161729d93SAxel Dörfler char buffer[64]; 34261729d93SAxel Dörfler if (local != NULL && domain != NULL) { 34361729d93SAxel Dörfler domain->address_module->print_address_buffer(local, buffer, 34461729d93SAxel Dörfler sizeof(buffer), false); 34561729d93SAxel Dörfler } else 34661729d93SAxel Dörfler strcpy(buffer, "-"); 34761729d93SAxel Dörfler kprintf(" local: %s\n", buffer); 34861729d93SAxel Dörfler 34961729d93SAxel Dörfler if (mask != NULL && domain != NULL) { 35061729d93SAxel Dörfler domain->address_module->print_address_buffer(mask, buffer, 35161729d93SAxel Dörfler sizeof(buffer), false); 35261729d93SAxel Dörfler } else 35361729d93SAxel Dörfler strcpy(buffer, "-"); 35461729d93SAxel Dörfler kprintf(" mask: %s\n", buffer); 35561729d93SAxel Dörfler 35661729d93SAxel Dörfler if (destination != NULL && domain != NULL) { 35761729d93SAxel Dörfler domain->address_module->print_address_buffer(destination, buffer, 35861729d93SAxel Dörfler sizeof(buffer), false); 35961729d93SAxel Dörfler } else 36061729d93SAxel Dörfler strcpy(buffer, "-"); 36161729d93SAxel Dörfler kprintf(" destination: %s\n", buffer); 3624af124a1SAxel Dörfler 3634af124a1SAxel Dörfler kprintf(" ref count: %" B_PRId32 "\n", CountReferences()); 36461729d93SAxel Dörfler } 36561729d93SAxel Dörfler 36661729d93SAxel Dörfler 36761729d93SAxel Dörfler #endif // ENABLE_DEBUGGER_COMMANDS 36861729d93SAxel Dörfler 36961729d93SAxel Dörfler 37061729d93SAxel Dörfler /*static*/ status_t 37161729d93SAxel Dörfler InterfaceAddress::Set(sockaddr** _address, const sockaddr* to) 37261729d93SAxel Dörfler { 37361729d93SAxel Dörfler sockaddr* address = *_address; 37461729d93SAxel Dörfler 37561729d93SAxel Dörfler if (to == NULL || to->sa_family == AF_UNSPEC) { 37661729d93SAxel Dörfler // Clear address 37761729d93SAxel Dörfler free(address); 37861729d93SAxel Dörfler *_address = NULL; 37961729d93SAxel Dörfler return B_OK; 38061729d93SAxel Dörfler } 38161729d93SAxel Dörfler 38261729d93SAxel Dörfler // Set address 38361729d93SAxel Dörfler 38461729d93SAxel Dörfler size_t size = max_c(to->sa_len, sizeof(sockaddr)); 38561729d93SAxel Dörfler if (size > sizeof(sockaddr_storage)) 38661729d93SAxel Dörfler size = sizeof(sockaddr_storage); 38761729d93SAxel Dörfler 38861729d93SAxel Dörfler address = Prepare(_address, size); 38961729d93SAxel Dörfler if (address == NULL) 39061729d93SAxel Dörfler return B_NO_MEMORY; 39161729d93SAxel Dörfler 39261729d93SAxel Dörfler memcpy(address, to, size); 39361729d93SAxel Dörfler address->sa_len = size; 39461729d93SAxel Dörfler 39561729d93SAxel Dörfler return B_OK; 39661729d93SAxel Dörfler } 39761729d93SAxel Dörfler 39861729d93SAxel Dörfler 3995ebd9548SAxel Dörfler /*! Makes sure that the sockaddr object pointed to by \a _address is large 4005ebd9548SAxel Dörfler enough to hold \a size bytes. 4015ebd9548SAxel Dörfler \a _address may point to NULL when calling this method. 4025ebd9548SAxel Dörfler */ 40361729d93SAxel Dörfler /*static*/ sockaddr* 40461729d93SAxel Dörfler InterfaceAddress::Prepare(sockaddr** _address, size_t size) 40561729d93SAxel Dörfler { 40661729d93SAxel Dörfler size = max_c(size, sizeof(sockaddr)); 40761729d93SAxel Dörfler if (size > sizeof(sockaddr_storage)) 40861729d93SAxel Dörfler size = sizeof(sockaddr_storage); 40961729d93SAxel Dörfler 41061729d93SAxel Dörfler sockaddr* address = *_address; 41161729d93SAxel Dörfler 41261729d93SAxel Dörfler if (address == NULL || size > address->sa_len) { 413*466081d0SPhil Costin sockaddr* resized = (sockaddr*)realloc(address, size); 414*466081d0SPhil Costin 415*466081d0SPhil Costin if (resized == NULL) { 416*466081d0SPhil Costin free(address); 41761729d93SAxel Dörfler return NULL; 41861729d93SAxel Dörfler } 41961729d93SAxel Dörfler 420*466081d0SPhil Costin address = resized; 421*466081d0SPhil Costin } 422*466081d0SPhil Costin 42361729d93SAxel Dörfler address->sa_len = size; 42461729d93SAxel Dörfler 42561729d93SAxel Dörfler *_address = address; 42661729d93SAxel Dörfler return address; 42761729d93SAxel Dörfler } 42861729d93SAxel Dörfler 42961729d93SAxel Dörfler 43061729d93SAxel Dörfler void 43161729d93SAxel Dörfler InterfaceAddress::_Init(net_interface* netInterface, net_domain* netDomain) 43261729d93SAxel Dörfler { 4332b1c0755SAxel Dörfler TRACE("InterfaceAddress %p: init interface %p, domain %p\n", this, 4342b1c0755SAxel Dörfler netInterface, netDomain); 4352b1c0755SAxel Dörfler 43661729d93SAxel Dörfler interface = netInterface; 43761729d93SAxel Dörfler domain = netDomain; 43861729d93SAxel Dörfler local = NULL; 43961729d93SAxel Dörfler destination = NULL; 44061729d93SAxel Dörfler mask = NULL; 4412b1c0755SAxel Dörfler flags = 0; 4422b1c0755SAxel Dörfler 4432b1c0755SAxel Dörfler if (interface != NULL) 4442b1c0755SAxel Dörfler ((Interface*)interface)->AcquireReference(); 44561729d93SAxel Dörfler } 44661729d93SAxel Dörfler 44761729d93SAxel Dörfler 44861729d93SAxel Dörfler // #pragma mark - 44961729d93SAxel Dörfler 45061729d93SAxel Dörfler 45161729d93SAxel Dörfler Interface::Interface(const char* interfaceName, 45261729d93SAxel Dörfler net_device_interface* deviceInterface) 453ff8c8dfcSStefano Ceccherini : 454ff8c8dfcSStefano Ceccherini fBusy(false) 45561729d93SAxel Dörfler { 45661729d93SAxel Dörfler TRACE("Interface %p: new \"%s\", device interface %p\n", this, 45761729d93SAxel Dörfler interfaceName, deviceInterface); 45861729d93SAxel Dörfler 459b0cf274aSAxel Dörfler int written = strlcpy(name, interfaceName, IF_NAMESIZE); 460b0cf274aSAxel Dörfler memset(name + written, 0, IF_NAMESIZE - written); 461b0cf274aSAxel Dörfler // Clear remaining space 462b0cf274aSAxel Dörfler 46361729d93SAxel Dörfler device = deviceInterface->device; 46461729d93SAxel Dörfler 46561729d93SAxel Dörfler index = ++sInterfaceIndex; 46661729d93SAxel Dörfler flags = 0; 46761729d93SAxel Dörfler type = 0; 46861729d93SAxel Dörfler mtu = deviceInterface->device->mtu; 46961729d93SAxel Dörfler metric = 0; 47061729d93SAxel Dörfler 47161729d93SAxel Dörfler fDeviceInterface = acquire_device_interface(deviceInterface); 47261729d93SAxel Dörfler 47361729d93SAxel Dörfler recursive_lock_init(&fLock, name); 47461729d93SAxel Dörfler 47561729d93SAxel Dörfler // Grab a reference to the networking stack, to make sure it won't be 47661729d93SAxel Dörfler // unloaded as long as an interface exists 47761729d93SAxel Dörfler module_info* module; 47861729d93SAxel Dörfler get_module(gNetStackInterfaceModule.info.name, &module); 47961729d93SAxel Dörfler } 48061729d93SAxel Dörfler 48161729d93SAxel Dörfler 48261729d93SAxel Dörfler Interface::~Interface() 48361729d93SAxel Dörfler { 4842b1c0755SAxel Dörfler TRACE("Interface %p: destructor\n", this); 4852b1c0755SAxel Dörfler 4862b1c0755SAxel Dörfler put_device_interface(fDeviceInterface); 4872b1c0755SAxel Dörfler 4882b1c0755SAxel Dörfler // Uninitialize the domain datalink protocols 4892b1c0755SAxel Dörfler 4902b1c0755SAxel Dörfler DatalinkTable::Iterator iterator = fDatalinkTable.GetIterator(); 4912b1c0755SAxel Dörfler while (domain_datalink* datalink = iterator.Next()) { 4922b1c0755SAxel Dörfler put_domain_datalink_protocols(this, datalink->domain); 4932b1c0755SAxel Dörfler } 4942b1c0755SAxel Dörfler 4952b1c0755SAxel Dörfler // Free domain datalink objects 4962b1c0755SAxel Dörfler 4972b1c0755SAxel Dörfler domain_datalink* next = fDatalinkTable.Clear(true); 4982b1c0755SAxel Dörfler while (next != NULL) { 4992b1c0755SAxel Dörfler domain_datalink* datalink = next; 5002b1c0755SAxel Dörfler next = next->hash_link; 5012b1c0755SAxel Dörfler 5022b1c0755SAxel Dörfler delete datalink; 5032b1c0755SAxel Dörfler } 5042b1c0755SAxel Dörfler 50561729d93SAxel Dörfler recursive_lock_destroy(&fLock); 50661729d93SAxel Dörfler 50761729d93SAxel Dörfler // Release reference of the stack - at this point, our stack may be unloaded 50861729d93SAxel Dörfler // if no other interfaces or sockets are left 50961729d93SAxel Dörfler put_module(gNetStackInterfaceModule.info.name); 51061729d93SAxel Dörfler } 51161729d93SAxel Dörfler 51261729d93SAxel Dörfler 51361729d93SAxel Dörfler /*! Returns a reference to the first InterfaceAddress that is from the same 51461729d93SAxel Dörfler as the specified \a family. 51561729d93SAxel Dörfler */ 51661729d93SAxel Dörfler InterfaceAddress* 51761729d93SAxel Dörfler Interface::FirstForFamily(int family) 51861729d93SAxel Dörfler { 51961729d93SAxel Dörfler RecursiveLocker locker(fLock); 52061729d93SAxel Dörfler 52161729d93SAxel Dörfler InterfaceAddress* address = _FirstForFamily(family); 52261729d93SAxel Dörfler if (address != NULL) { 52361729d93SAxel Dörfler address->AcquireReference(); 52461729d93SAxel Dörfler return address; 52561729d93SAxel Dörfler } 52661729d93SAxel Dörfler 52761729d93SAxel Dörfler return NULL; 52861729d93SAxel Dörfler } 52961729d93SAxel Dörfler 53061729d93SAxel Dörfler 53161729d93SAxel Dörfler /*! Returns a reference to the first unconfigured address of this interface 53261729d93SAxel Dörfler for the specified \a family. 53361729d93SAxel Dörfler */ 53461729d93SAxel Dörfler InterfaceAddress* 53561729d93SAxel Dörfler Interface::FirstUnconfiguredForFamily(int family) 53661729d93SAxel Dörfler { 53761729d93SAxel Dörfler RecursiveLocker locker(fLock); 53861729d93SAxel Dörfler 53961729d93SAxel Dörfler AddressList::Iterator iterator = fAddresses.GetIterator(); 54061729d93SAxel Dörfler while (InterfaceAddress* address = iterator.Next()) { 54161729d93SAxel Dörfler if (address->domain->family == family 54261729d93SAxel Dörfler && (address->local == NULL 54361729d93SAxel Dörfler // TODO: this has to be solved differently!! 54461729d93SAxel Dörfler || (flags & IFF_CONFIGURING) != 0)) { 54561729d93SAxel Dörfler address->AcquireReference(); 54661729d93SAxel Dörfler return address; 54761729d93SAxel Dörfler } 54861729d93SAxel Dörfler } 54961729d93SAxel Dörfler 55061729d93SAxel Dörfler return NULL; 55161729d93SAxel Dörfler } 55261729d93SAxel Dörfler 55361729d93SAxel Dörfler 55461729d93SAxel Dörfler /*! Returns a reference to the InterfaceAddress that has the specified 55561729d93SAxel Dörfler \a destination address. 55661729d93SAxel Dörfler */ 55761729d93SAxel Dörfler InterfaceAddress* 55861729d93SAxel Dörfler Interface::AddressForDestination(net_domain* domain, 55961729d93SAxel Dörfler const sockaddr* destination) 56061729d93SAxel Dörfler { 56161729d93SAxel Dörfler RecursiveLocker locker(fLock); 56261729d93SAxel Dörfler 56361729d93SAxel Dörfler if ((device->flags & IFF_BROADCAST) == 0) { 56461729d93SAxel Dörfler // The device does not support broadcasting 56561729d93SAxel Dörfler return NULL; 56661729d93SAxel Dörfler } 56761729d93SAxel Dörfler 56861729d93SAxel Dörfler AddressList::Iterator iterator = fAddresses.GetIterator(); 56961729d93SAxel Dörfler while (InterfaceAddress* address = iterator.Next()) { 57061729d93SAxel Dörfler if (address->domain == domain 57161729d93SAxel Dörfler && address->destination != NULL 57261729d93SAxel Dörfler && domain->address_module->equal_addresses(address->destination, 57361729d93SAxel Dörfler destination)) { 57461729d93SAxel Dörfler address->AcquireReference(); 57561729d93SAxel Dörfler return address; 57661729d93SAxel Dörfler } 57761729d93SAxel Dörfler } 57861729d93SAxel Dörfler 57961729d93SAxel Dörfler return NULL; 58061729d93SAxel Dörfler } 58161729d93SAxel Dörfler 58261729d93SAxel Dörfler 583910ffb32SAxel Dörfler /*! Returns a reference to the InterfaceAddress that has the specified 584910ffb32SAxel Dörfler \a local address. 585910ffb32SAxel Dörfler */ 586910ffb32SAxel Dörfler InterfaceAddress* 587910ffb32SAxel Dörfler Interface::AddressForLocal(net_domain* domain, const sockaddr* local) 588910ffb32SAxel Dörfler { 589910ffb32SAxel Dörfler RecursiveLocker locker(fLock); 590910ffb32SAxel Dörfler 591910ffb32SAxel Dörfler AddressList::Iterator iterator = fAddresses.GetIterator(); 592910ffb32SAxel Dörfler while (InterfaceAddress* address = iterator.Next()) { 593910ffb32SAxel Dörfler if (address->domain == domain 594910ffb32SAxel Dörfler && address->local != NULL 595910ffb32SAxel Dörfler && domain->address_module->equal_addresses(address->local, local)) { 596910ffb32SAxel Dörfler address->AcquireReference(); 597910ffb32SAxel Dörfler return address; 598910ffb32SAxel Dörfler } 599910ffb32SAxel Dörfler } 600910ffb32SAxel Dörfler 601910ffb32SAxel Dörfler return NULL; 602910ffb32SAxel Dörfler } 603910ffb32SAxel Dörfler 604910ffb32SAxel Dörfler 60561729d93SAxel Dörfler status_t 60661729d93SAxel Dörfler Interface::AddAddress(InterfaceAddress* address) 60761729d93SAxel Dörfler { 60861729d93SAxel Dörfler net_domain* domain = address->domain; 60961729d93SAxel Dörfler if (domain == NULL) 61061729d93SAxel Dörfler return B_BAD_VALUE; 61161729d93SAxel Dörfler 61261729d93SAxel Dörfler RecursiveLocker locker(fLock); 61361729d93SAxel Dörfler fAddresses.Add(address); 61461729d93SAxel Dörfler locker.Unlock(); 61561729d93SAxel Dörfler 6164ae58630SAxel Dörfler if (address->LocalIsDefined()) { 61761729d93SAxel Dörfler MutexLocker hashLocker(sHashLock); 61861729d93SAxel Dörfler sAddressTable.Insert(address); 6194ae58630SAxel Dörfler } 62061729d93SAxel Dörfler return B_OK; 62161729d93SAxel Dörfler } 62261729d93SAxel Dörfler 62361729d93SAxel Dörfler 62461729d93SAxel Dörfler void 62561729d93SAxel Dörfler Interface::RemoveAddress(InterfaceAddress* address) 62661729d93SAxel Dörfler { 62761729d93SAxel Dörfler net_domain* domain = address->domain; 62861729d93SAxel Dörfler if (domain == NULL) 62961729d93SAxel Dörfler return; 63061729d93SAxel Dörfler 63161729d93SAxel Dörfler RecursiveLocker locker(fLock); 63261729d93SAxel Dörfler 63361729d93SAxel Dörfler fAddresses.Remove(address); 63461729d93SAxel Dörfler address->GetDoublyLinkedListLink()->next = NULL; 63561729d93SAxel Dörfler 63661729d93SAxel Dörfler locker.Unlock(); 63761729d93SAxel Dörfler 6384ae58630SAxel Dörfler if (address->LocalIsDefined()) { 63961729d93SAxel Dörfler MutexLocker hashLocker(sHashLock); 64061729d93SAxel Dörfler sAddressTable.Remove(address); 64161729d93SAxel Dörfler } 6424ae58630SAxel Dörfler } 64361729d93SAxel Dörfler 64461729d93SAxel Dörfler 64561729d93SAxel Dörfler bool 64661729d93SAxel Dörfler Interface::GetNextAddress(InterfaceAddress** _address) 64761729d93SAxel Dörfler { 64861729d93SAxel Dörfler RecursiveLocker locker(fLock); 64961729d93SAxel Dörfler 65061729d93SAxel Dörfler InterfaceAddress* address = *_address; 65161729d93SAxel Dörfler if (address == NULL) { 65261729d93SAxel Dörfler // get first address 65361729d93SAxel Dörfler address = fAddresses.First(); 65461729d93SAxel Dörfler } else { 65561729d93SAxel Dörfler // get next, if possible 65661729d93SAxel Dörfler InterfaceAddress* next = fAddresses.GetNext(address); 65761729d93SAxel Dörfler address->ReleaseReference(); 65861729d93SAxel Dörfler address = next; 65961729d93SAxel Dörfler } 66061729d93SAxel Dörfler 66161729d93SAxel Dörfler *_address = address; 66261729d93SAxel Dörfler 66361729d93SAxel Dörfler if (address == NULL) 66461729d93SAxel Dörfler return false; 66561729d93SAxel Dörfler 66661729d93SAxel Dörfler address->AcquireReference(); 66761729d93SAxel Dörfler return true; 66861729d93SAxel Dörfler } 66961729d93SAxel Dörfler 67061729d93SAxel Dörfler 6719d771afbSAxel Dörfler InterfaceAddress* 6729d771afbSAxel Dörfler Interface::AddressAt(size_t index) 6739d771afbSAxel Dörfler { 6749d771afbSAxel Dörfler RecursiveLocker locker(fLock); 6759d771afbSAxel Dörfler 6769d771afbSAxel Dörfler AddressList::Iterator iterator = fAddresses.GetIterator(); 6779d771afbSAxel Dörfler size_t i = 0; 6789d771afbSAxel Dörfler 6799d771afbSAxel Dörfler while (InterfaceAddress* address = iterator.Next()) { 6809d771afbSAxel Dörfler if (i++ == index) { 6819d771afbSAxel Dörfler address->AcquireReference(); 6829d771afbSAxel Dörfler return address; 6839d771afbSAxel Dörfler } 6849d771afbSAxel Dörfler } 6859d771afbSAxel Dörfler 6869d771afbSAxel Dörfler return NULL; 6879d771afbSAxel Dörfler } 6889d771afbSAxel Dörfler 6899d771afbSAxel Dörfler 6902c12b8a3SAxel Dörfler int32 6912c12b8a3SAxel Dörfler Interface::IndexOfAddress(InterfaceAddress* address) 6922c12b8a3SAxel Dörfler { 693b0766602SAxel Dörfler if (address == NULL) 694b0766602SAxel Dörfler return -1; 695b0766602SAxel Dörfler 6962c12b8a3SAxel Dörfler RecursiveLocker locker(fLock); 6972c12b8a3SAxel Dörfler 6982c12b8a3SAxel Dörfler AddressList::Iterator iterator = fAddresses.GetIterator(); 6992c12b8a3SAxel Dörfler int32 index = 0; 7002c12b8a3SAxel Dörfler 7012c12b8a3SAxel Dörfler while (iterator.HasNext()) { 7022c12b8a3SAxel Dörfler if (address == iterator.Next()) 7032c12b8a3SAxel Dörfler return index; 7042c12b8a3SAxel Dörfler 7052c12b8a3SAxel Dörfler index++; 7062c12b8a3SAxel Dörfler } 7072c12b8a3SAxel Dörfler 7082c12b8a3SAxel Dörfler return -1; 7092c12b8a3SAxel Dörfler } 7102c12b8a3SAxel Dörfler 7112c12b8a3SAxel Dörfler 7129d771afbSAxel Dörfler size_t 7139d771afbSAxel Dörfler Interface::CountAddresses() 7149d771afbSAxel Dörfler { 7159d771afbSAxel Dörfler RecursiveLocker locker(fLock); 7169d771afbSAxel Dörfler return fAddresses.Count(); 7179d771afbSAxel Dörfler } 7189d771afbSAxel Dörfler 7199d771afbSAxel Dörfler 7202b1c0755SAxel Dörfler void 7212b1c0755SAxel Dörfler Interface::RemoveAddresses() 7222b1c0755SAxel Dörfler { 7232b1c0755SAxel Dörfler RecursiveLocker locker(fLock); 7242b1c0755SAxel Dörfler 7252b1c0755SAxel Dörfler while (InterfaceAddress* address = fAddresses.RemoveHead()) { 726b0766602SAxel Dörfler locker.Unlock(); 727b0766602SAxel Dörfler 728b0766602SAxel Dörfler if (address->LocalIsDefined()) { 729b0766602SAxel Dörfler MutexLocker hashLocker(sHashLock); 730b0766602SAxel Dörfler sAddressTable.Remove(address); 731b0766602SAxel Dörfler } 7325ebd9548SAxel Dörfler address->ReleaseReference(); 733b0766602SAxel Dörfler 734b0766602SAxel Dörfler locker.Lock(); 7352b1c0755SAxel Dörfler } 7362b1c0755SAxel Dörfler } 7372b1c0755SAxel Dörfler 7382b1c0755SAxel Dörfler 7399d771afbSAxel Dörfler /*! This is called in order to call the correct methods of the datalink 7409d771afbSAxel Dörfler protocols, ie. it will translate address changes to 7419d771afbSAxel Dörfler net_datalink_protocol::change_address(), and IFF_UP changes to 7429d771afbSAxel Dörfler net_datalink_protocol::interface_up(), and interface_down(). 7439d771afbSAxel Dörfler 7449d771afbSAxel Dörfler Everything else is passed unchanged to net_datalink_protocol::control(). 7459d771afbSAxel Dörfler */ 74661729d93SAxel Dörfler status_t 74761729d93SAxel Dörfler Interface::Control(net_domain* domain, int32 option, ifreq& request, 74861729d93SAxel Dörfler ifreq* userRequest, size_t length) 74961729d93SAxel Dörfler { 75061729d93SAxel Dörfler switch (option) { 75161729d93SAxel Dörfler case SIOCSIFFLAGS: 75261729d93SAxel Dörfler { 753e8802e49SAxel Dörfler if (length != sizeof(ifreq)) 754e8802e49SAxel Dörfler return B_BAD_VALUE; 755e8802e49SAxel Dörfler 75661729d93SAxel Dörfler uint32 requestFlags = request.ifr_flags; 75761729d93SAxel Dörfler uint32 oldFlags = flags; 75861729d93SAxel Dörfler status_t status = B_OK; 75961729d93SAxel Dörfler 76061729d93SAxel Dörfler request.ifr_flags &= ~(IFF_UP | IFF_LINK | IFF_BROADCAST); 76161729d93SAxel Dörfler 76261729d93SAxel Dörfler if ((requestFlags & IFF_UP) != (flags & IFF_UP)) { 76361729d93SAxel Dörfler if ((requestFlags & IFF_UP) != 0) 76461729d93SAxel Dörfler status = _SetUp(); 76561729d93SAxel Dörfler else 7662b1c0755SAxel Dörfler SetDown(); 76761729d93SAxel Dörfler } 76861729d93SAxel Dörfler 76961729d93SAxel Dörfler if (status == B_OK) { 77061729d93SAxel Dörfler // TODO: maybe allow deleting IFF_BROADCAST on the interface 77161729d93SAxel Dörfler // level? 77261729d93SAxel Dörfler flags &= IFF_UP | IFF_LINK | IFF_BROADCAST; 77361729d93SAxel Dörfler flags |= request.ifr_flags; 77461729d93SAxel Dörfler } 77561729d93SAxel Dörfler 776e78fea43SAxel Dörfler if (oldFlags != flags) { 777e78fea43SAxel Dörfler TRACE("Interface %p: flags changed from %" B_PRIx32 " to %" 778e78fea43SAxel Dörfler B_PRIx32 "\n", this, oldFlags, flags); 77961729d93SAxel Dörfler notify_interface_changed(this, oldFlags, flags); 780e78fea43SAxel Dörfler } 78161729d93SAxel Dörfler 78261729d93SAxel Dörfler return status; 78361729d93SAxel Dörfler } 78461729d93SAxel Dörfler 7852b1c0755SAxel Dörfler case B_SOCKET_SET_ALIAS: 7869d771afbSAxel Dörfler { 787e8802e49SAxel Dörfler if (length != sizeof(ifaliasreq)) 788e8802e49SAxel Dörfler return B_BAD_VALUE; 789e8802e49SAxel Dörfler 7909d771afbSAxel Dörfler RecursiveLocker locker(fLock); 7919d771afbSAxel Dörfler 7929d771afbSAxel Dörfler ifaliasreq aliasRequest; 7939d771afbSAxel Dörfler if (user_memcpy(&aliasRequest, userRequest, sizeof(ifaliasreq)) 7949d771afbSAxel Dörfler != B_OK) 7959d771afbSAxel Dörfler return B_BAD_ADDRESS; 7969d771afbSAxel Dörfler 797910ffb32SAxel Dörfler InterfaceAddress* address = NULL; 798910ffb32SAxel Dörfler if (aliasRequest.ifra_index < 0) { 799910ffb32SAxel Dörfler if (!domain->address_module->is_empty_address( 800910ffb32SAxel Dörfler (const sockaddr*)&aliasRequest.ifra_addr, false)) { 801910ffb32SAxel Dörfler // Find first address that matches the local address 802910ffb32SAxel Dörfler address = AddressForLocal(domain, 803910ffb32SAxel Dörfler (const sockaddr*)&aliasRequest.ifra_addr); 804910ffb32SAxel Dörfler } 805910ffb32SAxel Dörfler if (address == NULL) { 806910ffb32SAxel Dörfler // Find first address for family 807910ffb32SAxel Dörfler address = FirstForFamily(domain->family); 808910ffb32SAxel Dörfler } 809a271028eSAxel Dörfler if (address == NULL) { 810a271028eSAxel Dörfler // Create new on the fly 811a271028eSAxel Dörfler address = new(std::nothrow) InterfaceAddress(this, domain); 812a271028eSAxel Dörfler if (address == NULL) 813a271028eSAxel Dörfler return B_NO_MEMORY; 814a271028eSAxel Dörfler 815a271028eSAxel Dörfler status_t status = AddAddress(address); 816095f97a6SAxel Dörfler if (status != B_OK) { 817095f97a6SAxel Dörfler delete address; 818a271028eSAxel Dörfler return status; 819095f97a6SAxel Dörfler } 820a271028eSAxel Dörfler 821a271028eSAxel Dörfler // Note, even if setting the address failed, the empty 822a271028eSAxel Dörfler // address added here will still be added to the interface. 8238dcde8b1SAxel Dörfler address->AcquireReference(); 824a271028eSAxel Dörfler } 825910ffb32SAxel Dörfler } else 826910ffb32SAxel Dörfler address = AddressAt(aliasRequest.ifra_index); 827910ffb32SAxel Dörfler 8289d771afbSAxel Dörfler if (address == NULL) 8299d771afbSAxel Dörfler return B_BAD_VALUE; 8309d771afbSAxel Dörfler 8319d771afbSAxel Dörfler status_t status = B_OK; 8329d771afbSAxel Dörfler 8339d771afbSAxel Dörfler if (!domain->address_module->equal_addresses( 8349d771afbSAxel Dörfler (sockaddr*)&aliasRequest.ifra_addr, address->local)) { 8359d771afbSAxel Dörfler status = _ChangeAddress(locker, address, SIOCSIFADDR, 8369d771afbSAxel Dörfler address->local, (sockaddr*)&aliasRequest.ifra_addr); 8379d771afbSAxel Dörfler } 8389d771afbSAxel Dörfler 8399d771afbSAxel Dörfler if (status == B_OK && !domain->address_module->equal_addresses( 840910ffb32SAxel Dörfler (sockaddr*)&aliasRequest.ifra_mask, address->mask) 841910ffb32SAxel Dörfler && !domain->address_module->is_empty_address( 842910ffb32SAxel Dörfler (sockaddr*)&aliasRequest.ifra_mask, false)) { 8439d771afbSAxel Dörfler status = _ChangeAddress(locker, address, SIOCSIFNETMASK, 8449d771afbSAxel Dörfler address->mask, (sockaddr*)&aliasRequest.ifra_mask); 8459d771afbSAxel Dörfler } 8469d771afbSAxel Dörfler 8479d771afbSAxel Dörfler if (status == B_OK && !domain->address_module->equal_addresses( 8489d771afbSAxel Dörfler (sockaddr*)&aliasRequest.ifra_destination, 849910ffb32SAxel Dörfler address->destination) 850910ffb32SAxel Dörfler && !domain->address_module->is_empty_address( 851910ffb32SAxel Dörfler (sockaddr*)&aliasRequest.ifra_destination, false)) { 8529d771afbSAxel Dörfler status = _ChangeAddress(locker, address, 8539d771afbSAxel Dörfler (domain->address_module->flags 8549d771afbSAxel Dörfler & NET_ADDRESS_MODULE_FLAG_BROADCAST_ADDRESS) != 0 8559d771afbSAxel Dörfler ? SIOCSIFBRDADDR : SIOCSIFDSTADDR, 8569d771afbSAxel Dörfler address->destination, 8579d771afbSAxel Dörfler (sockaddr*)&aliasRequest.ifra_destination); 8589d771afbSAxel Dörfler } 8599d771afbSAxel Dörfler 8609d771afbSAxel Dörfler address->ReleaseReference(); 8619d771afbSAxel Dörfler return status; 8629d771afbSAxel Dörfler } 8639d771afbSAxel Dörfler 86461729d93SAxel Dörfler case SIOCSIFADDR: 86561729d93SAxel Dörfler case SIOCSIFNETMASK: 86661729d93SAxel Dörfler case SIOCSIFBRDADDR: 86761729d93SAxel Dörfler case SIOCSIFDSTADDR: 86861729d93SAxel Dörfler case SIOCDIFADDR: 86961729d93SAxel Dörfler { 870e8802e49SAxel Dörfler if (length != sizeof(ifreq)) 871e8802e49SAxel Dörfler return B_BAD_VALUE; 872e8802e49SAxel Dörfler 87361729d93SAxel Dörfler RecursiveLocker locker(fLock); 87461729d93SAxel Dörfler 87561729d93SAxel Dörfler InterfaceAddress* address = NULL; 87661729d93SAxel Dörfler sockaddr_storage newAddress; 87761729d93SAxel Dörfler 87861729d93SAxel Dörfler size_t size = max_c(request.ifr_addr.sa_len, sizeof(sockaddr)); 87961729d93SAxel Dörfler if (size > sizeof(sockaddr_storage)) 88061729d93SAxel Dörfler size = sizeof(sockaddr_storage); 88161729d93SAxel Dörfler 88261729d93SAxel Dörfler if (user_memcpy(&newAddress, &userRequest->ifr_addr, size) != B_OK) 88361729d93SAxel Dörfler return B_BAD_ADDRESS; 88461729d93SAxel Dörfler 88561729d93SAxel Dörfler if (option == SIOCDIFADDR) { 88661729d93SAxel Dörfler // Find referring address - we can't use the hash, as another 88761729d93SAxel Dörfler // interface might use the same address. 88861729d93SAxel Dörfler AddressList::Iterator iterator = fAddresses.GetIterator(); 88961729d93SAxel Dörfler while ((address = iterator.Next()) != NULL) { 89061729d93SAxel Dörfler if (address->domain == domain 89161729d93SAxel Dörfler && domain->address_module->equal_addresses( 89261729d93SAxel Dörfler address->local, (sockaddr*)&newAddress)) 89361729d93SAxel Dörfler break; 89461729d93SAxel Dörfler } 89561729d93SAxel Dörfler 89661729d93SAxel Dörfler if (address == NULL) 89761729d93SAxel Dörfler return B_BAD_VALUE; 89878abd727SAxel Dörfler } else { 89978abd727SAxel Dörfler // Just use the first address for this family 90078abd727SAxel Dörfler address = _FirstForFamily(domain->family); 90178abd727SAxel Dörfler if (address == NULL) { 90278abd727SAxel Dörfler // Create new on the fly 90378abd727SAxel Dörfler address = new(std::nothrow) InterfaceAddress(this, domain); 90478abd727SAxel Dörfler if (address == NULL) 90578abd727SAxel Dörfler return B_NO_MEMORY; 90678abd727SAxel Dörfler 90778abd727SAxel Dörfler status_t status = AddAddress(address); 908095f97a6SAxel Dörfler if (status != B_OK) { 909095f97a6SAxel Dörfler delete address; 91078abd727SAxel Dörfler return status; 911095f97a6SAxel Dörfler } 91278abd727SAxel Dörfler 91378abd727SAxel Dörfler // Note, even if setting the address failed, the empty 91478abd727SAxel Dörfler // address added here will still be added to the interface. 91578abd727SAxel Dörfler } 91678abd727SAxel Dörfler } 91761729d93SAxel Dörfler 9189d771afbSAxel Dörfler return _ChangeAddress(locker, address, option, 9199d771afbSAxel Dörfler *address->AddressFor(option), 92061729d93SAxel Dörfler option != SIOCDIFADDR ? (sockaddr*)&newAddress : NULL); 92161729d93SAxel Dörfler } 92261729d93SAxel Dörfler 92361729d93SAxel Dörfler default: 92461729d93SAxel Dörfler // pass the request into the datalink protocol stack 92561729d93SAxel Dörfler domain_datalink* datalink = DomainDatalink(domain->family); 92661729d93SAxel Dörfler if (datalink->first_info != NULL) { 92761729d93SAxel Dörfler return datalink->first_info->control( 92861729d93SAxel Dörfler datalink->first_protocol, option, userRequest, length); 92961729d93SAxel Dörfler } 93061729d93SAxel Dörfler break; 93161729d93SAxel Dörfler } 93261729d93SAxel Dörfler 93361729d93SAxel Dörfler return B_BAD_VALUE; 93461729d93SAxel Dörfler } 93561729d93SAxel Dörfler 93661729d93SAxel Dörfler 9372b1c0755SAxel Dörfler void 9382b1c0755SAxel Dörfler Interface::SetDown() 9392b1c0755SAxel Dörfler { 9402b1c0755SAxel Dörfler if ((flags & IFF_UP) == 0) 9412b1c0755SAxel Dörfler return; 9422b1c0755SAxel Dörfler 9437ed2a443SStefano Ceccherini RecursiveLocker interfacesLocker(sLock); 944ff8c8dfcSStefano Ceccherini 945ff8c8dfcSStefano Ceccherini if (IsBusy()) 946ff8c8dfcSStefano Ceccherini return; 947ff8c8dfcSStefano Ceccherini 948ff8c8dfcSStefano Ceccherini SetBusy(true); 9497ed2a443SStefano Ceccherini interfacesLocker.Unlock(); 9502b1c0755SAxel Dörfler 9512b1c0755SAxel Dörfler DatalinkTable::Iterator iterator = fDatalinkTable.GetIterator(); 9522b1c0755SAxel Dörfler while (domain_datalink* datalink = iterator.Next()) { 9532b1c0755SAxel Dörfler datalink->first_info->interface_down(datalink->first_protocol); 9542b1c0755SAxel Dörfler } 9552b1c0755SAxel Dörfler 9562b1c0755SAxel Dörfler flags &= ~IFF_UP; 957ff8c8dfcSStefano Ceccherini 958ff8c8dfcSStefano Ceccherini SetBusy(false); 9592b1c0755SAxel Dörfler } 9602b1c0755SAxel Dörfler 9612b1c0755SAxel Dörfler 9622b1c0755SAxel Dörfler /*! Called when a device lost its IFF_UP status. We will invalidate all 9632b1c0755SAxel Dörfler interface routes here. 9642b1c0755SAxel Dörfler */ 9652b1c0755SAxel Dörfler void 9662b1c0755SAxel Dörfler Interface::WentDown() 9672b1c0755SAxel Dörfler { 968ed18d7cfSAxel Dörfler TRACE("Interface %p: went down\n", this); 969ed18d7cfSAxel Dörfler 9702b1c0755SAxel Dörfler RecursiveLocker locker(fLock); 9712b1c0755SAxel Dörfler 9722b1c0755SAxel Dörfler AddressList::Iterator iterator = fAddresses.GetIterator(); 9732b1c0755SAxel Dörfler while (InterfaceAddress* address = iterator.Next()) { 9742b1c0755SAxel Dörfler if (address->domain != NULL) 9752b1c0755SAxel Dörfler invalidate_routes(address->domain, this); 9762b1c0755SAxel Dörfler } 9772b1c0755SAxel Dörfler } 9782b1c0755SAxel Dörfler 9792b1c0755SAxel Dörfler 98061729d93SAxel Dörfler status_t 98161729d93SAxel Dörfler Interface::CreateDomainDatalinkIfNeeded(net_domain* domain) 98261729d93SAxel Dörfler { 98361729d93SAxel Dörfler RecursiveLocker locker(fLock); 98461729d93SAxel Dörfler 98561729d93SAxel Dörfler if (fDatalinkTable.Lookup(domain->family) != NULL) 98661729d93SAxel Dörfler return B_OK; 98761729d93SAxel Dörfler 98861729d93SAxel Dörfler TRACE("Interface %p: create domain datalink for domain %p\n", this, domain); 98961729d93SAxel Dörfler 99061729d93SAxel Dörfler domain_datalink* datalink = new(std::nothrow) domain_datalink; 99161729d93SAxel Dörfler if (datalink == NULL) 99261729d93SAxel Dörfler return B_NO_MEMORY; 99361729d93SAxel Dörfler 994ed18d7cfSAxel Dörfler datalink->first_protocol = NULL; 995ed18d7cfSAxel Dörfler datalink->first_info = NULL; 99661729d93SAxel Dörfler datalink->domain = domain; 99761729d93SAxel Dörfler 99861729d93SAxel Dörfler // setup direct route for bound devices 99961729d93SAxel Dörfler datalink->direct_route.destination = NULL; 100061729d93SAxel Dörfler datalink->direct_route.mask = NULL; 100161729d93SAxel Dörfler datalink->direct_route.gateway = NULL; 100261729d93SAxel Dörfler datalink->direct_route.flags = 0; 100361729d93SAxel Dörfler datalink->direct_route.mtu = 0; 100461729d93SAxel Dörfler datalink->direct_route.interface_address = &datalink->direct_address; 100561729d93SAxel Dörfler datalink->direct_route.ref_count = 1; 100661729d93SAxel Dörfler // make sure this doesn't get deleted accidently 100761729d93SAxel Dörfler 100861729d93SAxel Dörfler // provide its link back to the interface 100961729d93SAxel Dörfler datalink->direct_address.local = NULL; 101061729d93SAxel Dörfler datalink->direct_address.destination = NULL; 101161729d93SAxel Dörfler datalink->direct_address.mask = NULL; 101261729d93SAxel Dörfler datalink->direct_address.domain = domain; 101361729d93SAxel Dörfler datalink->direct_address.interface = this; 10142b1c0755SAxel Dörfler datalink->direct_address.flags = IFAF_DIRECT_ADDRESS; 101561729d93SAxel Dörfler 101661729d93SAxel Dörfler fDatalinkTable.Insert(datalink); 101761729d93SAxel Dörfler 101861729d93SAxel Dörfler status_t status = get_domain_datalink_protocols(this, domain); 101961729d93SAxel Dörfler if (status == B_OK) 102061729d93SAxel Dörfler return B_OK; 102161729d93SAxel Dörfler 102261729d93SAxel Dörfler fDatalinkTable.Remove(datalink); 102361729d93SAxel Dörfler delete datalink; 102461729d93SAxel Dörfler 102561729d93SAxel Dörfler return status; 102661729d93SAxel Dörfler } 102761729d93SAxel Dörfler 102861729d93SAxel Dörfler 102961729d93SAxel Dörfler domain_datalink* 103061729d93SAxel Dörfler Interface::DomainDatalink(uint8 family) 103161729d93SAxel Dörfler { 103261729d93SAxel Dörfler // Note: domain datalinks cannot be removed while the interface is alive, 103361729d93SAxel Dörfler // since this would require us either to hold the lock while calling this 103461729d93SAxel Dörfler // function, or introduce reference counting for the domain_datalink 103561729d93SAxel Dörfler // structure. 103661729d93SAxel Dörfler RecursiveLocker locker(fLock); 103761729d93SAxel Dörfler return fDatalinkTable.Lookup(family); 103861729d93SAxel Dörfler } 103961729d93SAxel Dörfler 104061729d93SAxel Dörfler 104161729d93SAxel Dörfler #if ENABLE_DEBUGGER_COMMANDS 104261729d93SAxel Dörfler 104361729d93SAxel Dörfler 104461729d93SAxel Dörfler void 104561729d93SAxel Dörfler Interface::Dump() const 104661729d93SAxel Dörfler { 104761729d93SAxel Dörfler kprintf("name: %s\n", name); 104861729d93SAxel Dörfler kprintf("device: %p\n", device); 104961729d93SAxel Dörfler kprintf("device_interface: %p\n", fDeviceInterface); 105061729d93SAxel Dörfler kprintf("index: %" B_PRIu32 "\n", index); 105161729d93SAxel Dörfler kprintf("flags: %#" B_PRIx32 "\n", flags); 105261729d93SAxel Dörfler kprintf("type: %u\n", type); 105361729d93SAxel Dörfler kprintf("mtu: %" B_PRIu32 "\n", mtu); 105461729d93SAxel Dörfler kprintf("metric: %" B_PRIu32 "\n", metric); 1055ed18d7cfSAxel Dörfler kprintf("ref count: %" B_PRId32 "\n", CountReferences()); 105661729d93SAxel Dörfler 105761729d93SAxel Dörfler kprintf("datalink protocols:\n"); 105861729d93SAxel Dörfler 105961729d93SAxel Dörfler DatalinkTable::Iterator datalinkIterator = fDatalinkTable.GetIterator(); 106061729d93SAxel Dörfler size_t i = 0; 106161729d93SAxel Dörfler while (domain_datalink* datalink = datalinkIterator.Next()) { 106261729d93SAxel Dörfler kprintf("%2zu. domain: %p\n", ++i, datalink->domain); 106361729d93SAxel Dörfler kprintf(" first_protocol: %p\n", datalink->first_protocol); 106461729d93SAxel Dörfler kprintf(" first_info: %p\n", datalink->first_info); 106561729d93SAxel Dörfler kprintf(" direct_route: %p\n", &datalink->direct_route); 106661729d93SAxel Dörfler } 106761729d93SAxel Dörfler 106861729d93SAxel Dörfler kprintf("addresses:\n"); 106961729d93SAxel Dörfler 107061729d93SAxel Dörfler AddressList::ConstIterator iterator = fAddresses.GetIterator(); 107161729d93SAxel Dörfler i = 0; 107261729d93SAxel Dörfler while (InterfaceAddress* address = iterator.Next()) { 107361729d93SAxel Dörfler address->Dump(++i, true); 107461729d93SAxel Dörfler } 107561729d93SAxel Dörfler } 107661729d93SAxel Dörfler 107761729d93SAxel Dörfler 107861729d93SAxel Dörfler #endif // ENABLE_DEBUGGER_COMMANDS 107961729d93SAxel Dörfler 108061729d93SAxel Dörfler 108161729d93SAxel Dörfler status_t 108261729d93SAxel Dörfler Interface::_SetUp() 108361729d93SAxel Dörfler { 108461729d93SAxel Dörfler status_t status = up_device_interface(fDeviceInterface); 108561729d93SAxel Dörfler if (status != B_OK) 108661729d93SAxel Dörfler return status; 108761729d93SAxel Dörfler 1088ff8c8dfcSStefano Ceccherini RecursiveLocker interfacesLocker(sLock); 1089ff8c8dfcSStefano Ceccherini SetBusy(true); 1090ff8c8dfcSStefano Ceccherini interfacesLocker.Unlock(); 1091ff8c8dfcSStefano Ceccherini 10922b1c0755SAxel Dörfler // Propagate flag to all datalink protocols 109361729d93SAxel Dörfler DatalinkTable::Iterator iterator = fDatalinkTable.GetIterator(); 109461729d93SAxel Dörfler while (domain_datalink* datalink = iterator.Next()) { 109561729d93SAxel Dörfler status = datalink->first_info->interface_up(datalink->first_protocol); 109661729d93SAxel Dörfler if (status != B_OK) { 109761729d93SAxel Dörfler // Revert "up" status 109861729d93SAxel Dörfler DatalinkTable::Iterator secondIterator 109961729d93SAxel Dörfler = fDatalinkTable.GetIterator(); 110061729d93SAxel Dörfler while (secondIterator.HasNext()) { 110161729d93SAxel Dörfler domain_datalink* secondDatalink = secondIterator.Next(); 110261729d93SAxel Dörfler if (secondDatalink == NULL || secondDatalink == datalink) 1103c22d69bfSAxel Dörfler break; 1104c22d69bfSAxel Dörfler 110561729d93SAxel Dörfler secondDatalink->first_info->interface_down( 110661729d93SAxel Dörfler secondDatalink->first_protocol); 110761729d93SAxel Dörfler } 110861729d93SAxel Dörfler 110961729d93SAxel Dörfler down_device_interface(fDeviceInterface); 1110ff8c8dfcSStefano Ceccherini SetBusy(false); 111161729d93SAxel Dörfler return status; 111261729d93SAxel Dörfler } 111361729d93SAxel Dörfler } 111461729d93SAxel Dörfler 11152b1c0755SAxel Dörfler // Add default routes for the existing addresses 11162b1c0755SAxel Dörfler 11172b1c0755SAxel Dörfler AddressList::Iterator addressIterator = fAddresses.GetIterator(); 11182b1c0755SAxel Dörfler while (InterfaceAddress* address = addressIterator.Next()) { 11192b1c0755SAxel Dörfler address->AddDefaultRoutes(SIOCSIFADDR); 11202b1c0755SAxel Dörfler } 11212b1c0755SAxel Dörfler 112261729d93SAxel Dörfler flags |= IFF_UP; 1123ff8c8dfcSStefano Ceccherini SetBusy(false); 1124ff8c8dfcSStefano Ceccherini 112561729d93SAxel Dörfler return B_OK; 112661729d93SAxel Dörfler } 112761729d93SAxel Dörfler 112861729d93SAxel Dörfler 112961729d93SAxel Dörfler InterfaceAddress* 113061729d93SAxel Dörfler Interface::_FirstForFamily(int family) 113161729d93SAxel Dörfler { 113261729d93SAxel Dörfler ASSERT_LOCKED_RECURSIVE(&fLock); 113361729d93SAxel Dörfler 113461729d93SAxel Dörfler AddressList::Iterator iterator = fAddresses.GetIterator(); 113561729d93SAxel Dörfler while (InterfaceAddress* address = iterator.Next()) { 113661729d93SAxel Dörfler if (address->domain != NULL && address->domain->family == family) 113761729d93SAxel Dörfler return address; 113861729d93SAxel Dörfler } 113961729d93SAxel Dörfler 114061729d93SAxel Dörfler return NULL; 114161729d93SAxel Dörfler } 114261729d93SAxel Dörfler 114361729d93SAxel Dörfler 11449d771afbSAxel Dörfler status_t 11459d771afbSAxel Dörfler Interface::_ChangeAddress(RecursiveLocker& locker, InterfaceAddress* address, 1146b0cf274aSAxel Dörfler int32 option, const sockaddr* originalAddress, 1147b0cf274aSAxel Dörfler const sockaddr* requestedAddress) 11489d771afbSAxel Dörfler { 1149b0cf274aSAxel Dörfler // Copy old address 11509d771afbSAxel Dörfler sockaddr_storage oldAddress; 11519d771afbSAxel Dörfler if (address->domain->address_module->set_to((sockaddr*)&oldAddress, 11529d771afbSAxel Dörfler originalAddress) != B_OK) 11539d771afbSAxel Dörfler oldAddress.ss_family = AF_UNSPEC; 11549d771afbSAxel Dörfler 1155b0cf274aSAxel Dörfler // Copy new address (this also makes sure that sockaddr::sa_len is set 1156b0cf274aSAxel Dörfler // correctly) 1157b0cf274aSAxel Dörfler sockaddr_storage newAddress; 1158b0cf274aSAxel Dörfler if (address->domain->address_module->set_to((sockaddr*)&newAddress, 1159b0cf274aSAxel Dörfler requestedAddress) != B_OK) 1160b0cf274aSAxel Dörfler newAddress.ss_family = AF_UNSPEC; 1161b0cf274aSAxel Dörfler 11626b1e1488SAxel Dörfler // Test if anything changed for real 1163b0cf274aSAxel Dörfler if (address->domain->address_module->equal_addresses( 1164b0cf274aSAxel Dörfler (sockaddr*)&oldAddress, (sockaddr*)&newAddress)) { 11656b1e1488SAxel Dörfler // Nothing to do 1166b0cf274aSAxel Dörfler TRACE(" option %" B_PRId32 " addresses are equal!\n", option); 11676b1e1488SAxel Dörfler return B_OK; 11686b1e1488SAxel Dörfler } 11696b1e1488SAxel Dörfler 11709d771afbSAxel Dörfler // TODO: mark this address busy or call while holding the lock! 11719d771afbSAxel Dörfler address->AcquireReference(); 11729d771afbSAxel Dörfler locker.Unlock(); 11739d771afbSAxel Dörfler 11749d771afbSAxel Dörfler domain_datalink* datalink = DomainDatalink(address->domain); 11759d771afbSAxel Dörfler status_t status = datalink->first_protocol->module->change_address( 11769d771afbSAxel Dörfler datalink->first_protocol, address, option, 11779d771afbSAxel Dörfler oldAddress.ss_family != AF_UNSPEC ? (sockaddr*)&oldAddress : NULL, 1178b0cf274aSAxel Dörfler newAddress.ss_family != AF_UNSPEC ? (sockaddr*)&newAddress : NULL); 11799d771afbSAxel Dörfler 11809d771afbSAxel Dörfler locker.Lock(); 11819d771afbSAxel Dörfler address->ReleaseReference(); 11829d771afbSAxel Dörfler return status; 11839d771afbSAxel Dörfler } 11849d771afbSAxel Dörfler 11859d771afbSAxel Dörfler 118661729d93SAxel Dörfler // #pragma mark - 118761729d93SAxel Dörfler 118861729d93SAxel Dörfler 118961729d93SAxel Dörfler /*! Searches for a specific interface by name. 119061729d93SAxel Dörfler You need to have the interface list's lock hold when calling this function. 119161729d93SAxel Dörfler */ 119261729d93SAxel Dörfler static struct Interface* 119361729d93SAxel Dörfler find_interface(const char* name) 119461729d93SAxel Dörfler { 11958a6337d0SStefano Ceccherini ASSERT_LOCKED_RECURSIVE(&sLock); 119661729d93SAxel Dörfler 119761729d93SAxel Dörfler InterfaceList::Iterator iterator = sInterfaces.GetIterator(); 119861729d93SAxel Dörfler while (Interface* interface = iterator.Next()) { 1199c22d69bfSAxel Dörfler if (!strcmp(interface->name, name)) 1200c22d69bfSAxel Dörfler return interface; 1201c22d69bfSAxel Dörfler } 1202c22d69bfSAxel Dörfler 1203c22d69bfSAxel Dörfler return NULL; 1204c22d69bfSAxel Dörfler } 1205c22d69bfSAxel Dörfler 1206c22d69bfSAxel Dörfler 120761729d93SAxel Dörfler /*! Searches for a specific interface by index. 120861729d93SAxel Dörfler You need to have the interface list's lock hold when calling this function. 1209c22d69bfSAxel Dörfler */ 121061729d93SAxel Dörfler static struct Interface* 121161729d93SAxel Dörfler find_interface(uint32 index) 1212c22d69bfSAxel Dörfler { 121361729d93SAxel Dörfler InterfaceList::Iterator iterator = sInterfaces.GetIterator(); 121461729d93SAxel Dörfler while (Interface* interface = iterator.Next()) { 1215c22d69bfSAxel Dörfler if (interface->index == index) 1216c22d69bfSAxel Dörfler return interface; 1217c22d69bfSAxel Dörfler } 1218c22d69bfSAxel Dörfler 1219c22d69bfSAxel Dörfler return NULL; 1220c22d69bfSAxel Dörfler } 1221c22d69bfSAxel Dörfler 1222c22d69bfSAxel Dörfler 122361729d93SAxel Dörfler // #pragma mark - 122461729d93SAxel Dörfler 122561729d93SAxel Dörfler 122661729d93SAxel Dörfler status_t 122761729d93SAxel Dörfler add_interface(const char* name, net_domain_private* domain, 122861729d93SAxel Dörfler const ifaliasreq& request, net_device_interface* deviceInterface) 122961729d93SAxel Dörfler { 12308a6337d0SStefano Ceccherini RecursiveLocker locker(sLock); 123161729d93SAxel Dörfler 123261729d93SAxel Dörfler if (find_interface(name) != NULL) 123361729d93SAxel Dörfler return B_NAME_IN_USE; 123461729d93SAxel Dörfler 123561729d93SAxel Dörfler Interface* interface 123661729d93SAxel Dörfler = new(std::nothrow) Interface(name, deviceInterface); 1237c22d69bfSAxel Dörfler if (interface == NULL) 1238c22d69bfSAxel Dörfler return B_NO_MEMORY; 1239c22d69bfSAxel Dörfler 124061729d93SAxel Dörfler sInterfaces.Add(interface); 124161729d93SAxel Dörfler interface->AcquireReference(); 124261729d93SAxel Dörfler // We need another reference to be able to use the interface without 124361729d93SAxel Dörfler // holding sLock. 1244c22d69bfSAxel Dörfler 124561729d93SAxel Dörfler locker.Unlock(); 1246c22d69bfSAxel Dörfler 12474ae58630SAxel Dörfler status_t status = add_interface_address(interface, domain, request); 12484ae58630SAxel Dörfler if (status == B_OK) 124961729d93SAxel Dörfler notify_interface_added(interface); 12504ae58630SAxel Dörfler else { 12514ae58630SAxel Dörfler locker.Lock(); 12524ae58630SAxel Dörfler sInterfaces.Remove(interface); 12534ae58630SAxel Dörfler locker.Unlock(); 12544ae58630SAxel Dörfler interface->ReleaseReference(); 12554ae58630SAxel Dörfler } 1256c22d69bfSAxel Dörfler 125761729d93SAxel Dörfler interface->ReleaseReference(); 125827e0dea9SAxel Dörfler 12594ae58630SAxel Dörfler return status; 1260c22d69bfSAxel Dörfler } 1261c22d69bfSAxel Dörfler 1262c22d69bfSAxel Dörfler 126361729d93SAxel Dörfler /*! Removes the interface from the list, and puts the stack's reference to it. 126461729d93SAxel Dörfler */ 12657aa2819cSAxel Dörfler void 126661729d93SAxel Dörfler remove_interface(Interface* interface) 1267c64feccaSHugo Santos { 12682b1c0755SAxel Dörfler interface->SetDown(); 12692b1c0755SAxel Dörfler interface->RemoveAddresses(); 1270c64feccaSHugo Santos 12718a6337d0SStefano Ceccherini RecursiveLocker locker(sLock); 12722b1c0755SAxel Dörfler sInterfaces.Remove(interface); 12732b1c0755SAxel Dörfler locker.Unlock(); 1274f6d219a1SAxel Dörfler 127561729d93SAxel Dörfler notify_interface_removed(interface); 1276c22d69bfSAxel Dörfler 12772b1c0755SAxel Dörfler interface->ReleaseReference(); 12787aa2819cSAxel Dörfler } 12797aa2819cSAxel Dörfler 12807aa2819cSAxel Dörfler 12817aa2819cSAxel Dörfler /*! This is called whenever a device interface is being removed. We will get 12827aa2819cSAxel Dörfler the corresponding Interface, and remove it. 12837aa2819cSAxel Dörfler */ 12847aa2819cSAxel Dörfler void 12857aa2819cSAxel Dörfler interface_removed_device_interface(net_device_interface* deviceInterface) 12867aa2819cSAxel Dörfler { 12878a6337d0SStefano Ceccherini RecursiveLocker locker(sLock); 12887aa2819cSAxel Dörfler 12897aa2819cSAxel Dörfler Interface* interface = find_interface(deviceInterface->device->name); 12907aa2819cSAxel Dörfler if (interface != NULL) 12917aa2819cSAxel Dörfler remove_interface(interface); 1292c22d69bfSAxel Dörfler } 1293c22d69bfSAxel Dörfler 1294c22d69bfSAxel Dörfler 129561729d93SAxel Dörfler status_t 129661729d93SAxel Dörfler add_interface_address(Interface* interface, net_domain_private* domain, 129761729d93SAxel Dörfler const ifaliasreq& request) 1298c22d69bfSAxel Dörfler { 129961729d93SAxel Dörfler // Make sure the family of the provided addresses is valid 130061729d93SAxel Dörfler if ((request.ifra_addr.ss_family != domain->family 130161729d93SAxel Dörfler && request.ifra_addr.ss_family != AF_UNSPEC) 130261729d93SAxel Dörfler || (request.ifra_mask.ss_family != domain->family 130361729d93SAxel Dörfler && request.ifra_mask.ss_family != AF_UNSPEC) 130461729d93SAxel Dörfler || (request.ifra_broadaddr.ss_family != domain->family 130561729d93SAxel Dörfler && request.ifra_broadaddr.ss_family != AF_UNSPEC)) 130661729d93SAxel Dörfler return B_BAD_VALUE; 1307c22d69bfSAxel Dörfler 130861729d93SAxel Dörfler RecursiveLocker locker(interface->Lock()); 130961729d93SAxel Dörfler 131061729d93SAxel Dörfler InterfaceAddress* address 131161729d93SAxel Dörfler = new(std::nothrow) InterfaceAddress(interface, domain); 131261729d93SAxel Dörfler if (address == NULL) 131361729d93SAxel Dörfler return B_NO_MEMORY; 131461729d93SAxel Dörfler 131561729d93SAxel Dörfler status_t status = address->SetTo(request); 131661729d93SAxel Dörfler if (status == B_OK) 131761729d93SAxel Dörfler status = interface->CreateDomainDatalinkIfNeeded(domain); 131861729d93SAxel Dörfler if (status == B_OK) 131961729d93SAxel Dörfler status = interface->AddAddress(address); 132061729d93SAxel Dörfler 132161729d93SAxel Dörfler if (status == B_OK && address->local != NULL) { 132261729d93SAxel Dörfler // update the datalink protocols 132361729d93SAxel Dörfler domain_datalink* datalink = interface->DomainDatalink(domain->family); 132461729d93SAxel Dörfler 132561729d93SAxel Dörfler status = datalink->first_protocol->module->change_address( 132661729d93SAxel Dörfler datalink->first_protocol, address, SIOCAIFADDR, NULL, 132761729d93SAxel Dörfler address->local); 132861729d93SAxel Dörfler if (status != B_OK) 132961729d93SAxel Dörfler interface->RemoveAddress(address); 133061729d93SAxel Dörfler } 133161729d93SAxel Dörfler if (status == B_OK) 133261729d93SAxel Dörfler notify_interface_changed(interface); 133361729d93SAxel Dörfler else 133461729d93SAxel Dörfler delete address; 133561729d93SAxel Dörfler 133661729d93SAxel Dörfler return status; 133761729d93SAxel Dörfler } 133861729d93SAxel Dörfler 133961729d93SAxel Dörfler 134061729d93SAxel Dörfler status_t 134161729d93SAxel Dörfler update_interface_address(InterfaceAddress* interfaceAddress, int32 option, 134261729d93SAxel Dörfler const sockaddr* oldAddress, const sockaddr* newAddress) 134361729d93SAxel Dörfler { 1344b0cf274aSAxel Dörfler TRACE("%s(address %p, option %" B_PRId32 ", oldAddress %s, newAddress " 1345b0cf274aSAxel Dörfler "%s)\n", __FUNCTION__, interfaceAddress, option, 1346b0cf274aSAxel Dörfler AddressString(interfaceAddress->domain, oldAddress).Data(), 1347b0cf274aSAxel Dörfler AddressString(interfaceAddress->domain, newAddress).Data()); 1348b0cf274aSAxel Dörfler 134961729d93SAxel Dörfler MutexLocker locker(sHashLock); 135061729d93SAxel Dörfler 135161729d93SAxel Dörfler // set logical interface address 135261729d93SAxel Dörfler sockaddr** _address = interfaceAddress->AddressFor(option); 135361729d93SAxel Dörfler if (_address == NULL) 135461729d93SAxel Dörfler return B_BAD_VALUE; 135561729d93SAxel Dörfler 13562b1c0755SAxel Dörfler Interface* interface = (Interface*)interfaceAddress->interface; 13572b1c0755SAxel Dörfler 13582b1c0755SAxel Dörfler interfaceAddress->RemoveDefaultRoutes(option); 13592b1c0755SAxel Dörfler 13602b1c0755SAxel Dörfler if (option == SIOCDIFADDR) { 13612b1c0755SAxel Dörfler // Remove address, and release its reference (causing our caller to 13622b1c0755SAxel Dörfler // delete it) 13632b1c0755SAxel Dörfler locker.Unlock(); 13642b1c0755SAxel Dörfler 13652b1c0755SAxel Dörfler invalidate_routes(interfaceAddress); 13662b1c0755SAxel Dörfler 13672b1c0755SAxel Dörfler interface->RemoveAddress(interfaceAddress); 13682b1c0755SAxel Dörfler interfaceAddress->ReleaseReference(); 13692b1c0755SAxel Dörfler return B_OK; 13702b1c0755SAxel Dörfler } 13712b1c0755SAxel Dörfler 13724ae58630SAxel Dörfler if (interfaceAddress->LocalIsDefined()) 137361729d93SAxel Dörfler sAddressTable.Remove(interfaceAddress); 137461729d93SAxel Dörfler 137561729d93SAxel Dörfler // Copy new address over 137661729d93SAxel Dörfler status_t status = InterfaceAddress::Set(_address, newAddress); 137761729d93SAxel Dörfler if (status == B_OK) { 137861729d93SAxel Dörfler sockaddr* address = *_address; 137961729d93SAxel Dörfler 13805ebd9548SAxel Dörfler if (option == SIOCSIFADDR || option == SIOCSIFNETMASK) { 138161729d93SAxel Dörfler // Reset netmask and broadcast addresses to defaults 138261729d93SAxel Dörfler net_domain* domain = interfaceAddress->domain; 13835ebd9548SAxel Dörfler sockaddr* defaultNetmask = NULL; 13845ebd9548SAxel Dörfler const sockaddr* netmask = NULL; 138561729d93SAxel Dörfler if (option == SIOCSIFADDR) { 13865ebd9548SAxel Dörfler defaultNetmask = InterfaceAddress::Prepare( 138761729d93SAxel Dörfler &interfaceAddress->mask, address->sa_len); 13885ebd9548SAxel Dörfler } else 13895ebd9548SAxel Dörfler netmask = newAddress; 139061729d93SAxel Dörfler 139161729d93SAxel Dörfler // Reset the broadcast address if the address family has 139261729d93SAxel Dörfler // such 13935ebd9548SAxel Dörfler sockaddr* defaultBroadcast = NULL; 139461729d93SAxel Dörfler if ((domain->address_module->flags 139561729d93SAxel Dörfler & NET_ADDRESS_MODULE_FLAG_BROADCAST_ADDRESS) != 0) { 13965ebd9548SAxel Dörfler defaultBroadcast = InterfaceAddress::Prepare( 139761729d93SAxel Dörfler &interfaceAddress->destination, address->sa_len); 139861729d93SAxel Dörfler } else 139961729d93SAxel Dörfler InterfaceAddress::Set(&interfaceAddress->destination, NULL); 140061729d93SAxel Dörfler 14015ebd9548SAxel Dörfler domain->address_module->set_to_defaults(defaultNetmask, 14025ebd9548SAxel Dörfler defaultBroadcast, interfaceAddress->local, netmask); 140361729d93SAxel Dörfler } 140461729d93SAxel Dörfler 14052b1c0755SAxel Dörfler interfaceAddress->AddDefaultRoutes(option); 14062b1c0755SAxel Dörfler notify_interface_changed(interface); 140761729d93SAxel Dörfler } 140861729d93SAxel Dörfler 14094ae58630SAxel Dörfler if (interfaceAddress->LocalIsDefined()) 141061729d93SAxel Dörfler sAddressTable.Insert(interfaceAddress); 141161729d93SAxel Dörfler return status; 141261729d93SAxel Dörfler } 141361729d93SAxel Dörfler 141461729d93SAxel Dörfler 141561729d93SAxel Dörfler Interface* 141661729d93SAxel Dörfler get_interface(net_domain* domain, uint32 index) 141761729d93SAxel Dörfler { 14188a6337d0SStefano Ceccherini RecursiveLocker locker(sLock); 141961729d93SAxel Dörfler 1420ec4fb0f4SAxel Dörfler Interface* interface; 142161729d93SAxel Dörfler if (index == 0) 1422ec4fb0f4SAxel Dörfler interface = sInterfaces.First(); 1423ec4fb0f4SAxel Dörfler else 1424ec4fb0f4SAxel Dörfler interface = find_interface(index); 1425ff8c8dfcSStefano Ceccherini if (interface == NULL || interface->IsBusy()) 142661729d93SAxel Dörfler return NULL; 1427c22d69bfSAxel Dörfler 142861729d93SAxel Dörfler if (interface->CreateDomainDatalinkIfNeeded(domain) != B_OK) 142961729d93SAxel Dörfler return NULL; 143061729d93SAxel Dörfler 143161729d93SAxel Dörfler interface->AcquireReference(); 1432c22d69bfSAxel Dörfler return interface; 1433c22d69bfSAxel Dörfler } 1434c22d69bfSAxel Dörfler 143561729d93SAxel Dörfler 143661729d93SAxel Dörfler Interface* 143761729d93SAxel Dörfler get_interface(net_domain* domain, const char* name) 143861729d93SAxel Dörfler { 14398a6337d0SStefano Ceccherini RecursiveLocker locker(sLock); 144061729d93SAxel Dörfler 144161729d93SAxel Dörfler Interface* interface = find_interface(name); 1442ff8c8dfcSStefano Ceccherini if (interface == NULL || interface->IsBusy()) 144361729d93SAxel Dörfler return NULL; 144461729d93SAxel Dörfler 144561729d93SAxel Dörfler if (interface->CreateDomainDatalinkIfNeeded(domain) != B_OK) 144661729d93SAxel Dörfler return NULL; 144761729d93SAxel Dörfler 144861729d93SAxel Dörfler interface->AcquireReference(); 144961729d93SAxel Dörfler return interface; 145061729d93SAxel Dörfler } 145161729d93SAxel Dörfler 145261729d93SAxel Dörfler 145361729d93SAxel Dörfler Interface* 145461729d93SAxel Dörfler get_interface_for_device(net_domain* domain, uint32 index) 145561729d93SAxel Dörfler { 14568a6337d0SStefano Ceccherini RecursiveLocker locker(sLock); 145761729d93SAxel Dörfler 145861729d93SAxel Dörfler InterfaceList::Iterator iterator = sInterfaces.GetIterator(); 145961729d93SAxel Dörfler while (Interface* interface = iterator.Next()) { 146061729d93SAxel Dörfler if (interface->device->index == index) { 1461ff8c8dfcSStefano Ceccherini if (interface->IsBusy()) 1462ff8c8dfcSStefano Ceccherini return NULL; 146361729d93SAxel Dörfler if (interface->CreateDomainDatalinkIfNeeded(domain) != B_OK) 146461729d93SAxel Dörfler return NULL; 146561729d93SAxel Dörfler 146661729d93SAxel Dörfler interface->AcquireReference(); 146761729d93SAxel Dörfler return interface; 146861729d93SAxel Dörfler } 146961729d93SAxel Dörfler } 147061729d93SAxel Dörfler 1471c22d69bfSAxel Dörfler return NULL; 1472c22d69bfSAxel Dörfler } 1473c22d69bfSAxel Dörfler 1474c22d69bfSAxel Dörfler 1475cd08b9f7SAxel Dörfler /*! Returns a reference to an Interface that matches the given \a linkAddress. 1476cd08b9f7SAxel Dörfler The link address is checked against its hardware address, or its interface 1477cd08b9f7SAxel Dörfler name, or finally the interface index. 1478cd08b9f7SAxel Dörfler */ 1479cd08b9f7SAxel Dörfler Interface* 1480cd08b9f7SAxel Dörfler get_interface_for_link(net_domain* domain, const sockaddr* _linkAddress) 1481cd08b9f7SAxel Dörfler { 1482cd08b9f7SAxel Dörfler sockaddr_dl& linkAddress = *(sockaddr_dl*)_linkAddress; 1483cd08b9f7SAxel Dörfler 14848a6337d0SStefano Ceccherini RecursiveLocker locker(sLock); 1485cd08b9f7SAxel Dörfler 1486cd08b9f7SAxel Dörfler InterfaceList::Iterator iterator = sInterfaces.GetIterator(); 1487cd08b9f7SAxel Dörfler while (Interface* interface = iterator.Next()) { 1488ff8c8dfcSStefano Ceccherini if (interface->IsBusy()) 1489ff8c8dfcSStefano Ceccherini continue; 1490cd08b9f7SAxel Dörfler // Test if the hardware address matches, or if the given interface 1491cd08b9f7SAxel Dörfler // matches, or if at least the index matches. 1492cd08b9f7SAxel Dörfler if ((linkAddress.sdl_alen == interface->device->address.length 1493cd08b9f7SAxel Dörfler && memcmp(LLADDR(&linkAddress), interface->device->address.data, 1494cd08b9f7SAxel Dörfler linkAddress.sdl_alen) == 0) 1495cd08b9f7SAxel Dörfler || (linkAddress.sdl_nlen > 0 1496cd08b9f7SAxel Dörfler && !strcmp(interface->name, (const char*)linkAddress.sdl_data)) 1497cd08b9f7SAxel Dörfler || (linkAddress.sdl_nlen == 0 && linkAddress.sdl_alen == 0 1498cd08b9f7SAxel Dörfler && linkAddress.sdl_index == interface->index)) { 1499ff8c8dfcSStefano Ceccherini if (interface->IsBusy()) 1500ff8c8dfcSStefano Ceccherini return NULL; 1501cd08b9f7SAxel Dörfler if (interface->CreateDomainDatalinkIfNeeded(domain) != B_OK) 1502cd08b9f7SAxel Dörfler return NULL; 1503cd08b9f7SAxel Dörfler 1504cd08b9f7SAxel Dörfler interface->AcquireReference(); 1505cd08b9f7SAxel Dörfler return interface; 1506cd08b9f7SAxel Dörfler } 1507cd08b9f7SAxel Dörfler } 1508cd08b9f7SAxel Dörfler 1509cd08b9f7SAxel Dörfler return NULL; 1510cd08b9f7SAxel Dörfler } 1511cd08b9f7SAxel Dörfler 1512cd08b9f7SAxel Dörfler 151361729d93SAxel Dörfler InterfaceAddress* 151461729d93SAxel Dörfler get_interface_address(const sockaddr* local) 1515c22d69bfSAxel Dörfler { 151661729d93SAxel Dörfler if (local->sa_family == AF_UNSPEC) 151761729d93SAxel Dörfler return NULL; 1518c22d69bfSAxel Dörfler 151961729d93SAxel Dörfler MutexLocker locker(sHashLock); 1520c22d69bfSAxel Dörfler 152161729d93SAxel Dörfler InterfaceAddress* address = sAddressTable.Lookup(local); 152261729d93SAxel Dörfler if (address == NULL) 152361729d93SAxel Dörfler return NULL; 1524c22d69bfSAxel Dörfler 152561729d93SAxel Dörfler address->AcquireReference(); 152661729d93SAxel Dörfler return address; 152761729d93SAxel Dörfler } 152861729d93SAxel Dörfler 152961729d93SAxel Dörfler 153061729d93SAxel Dörfler InterfaceAddress* 153161729d93SAxel Dörfler get_interface_address_for_destination(net_domain* domain, 1532cd08b9f7SAxel Dörfler const sockaddr* destination) 153361729d93SAxel Dörfler { 15348a6337d0SStefano Ceccherini RecursiveLocker locker(sLock); 153561729d93SAxel Dörfler 153661729d93SAxel Dörfler InterfaceList::Iterator iterator = sInterfaces.GetIterator(); 153761729d93SAxel Dörfler while (Interface* interface = iterator.Next()) { 1538ff8c8dfcSStefano Ceccherini if (interface->IsBusy()) 1539ff8c8dfcSStefano Ceccherini continue; 1540ff8c8dfcSStefano Ceccherini 154161729d93SAxel Dörfler InterfaceAddress* address 154261729d93SAxel Dörfler = interface->AddressForDestination(domain, destination); 154361729d93SAxel Dörfler if (address != NULL) 154461729d93SAxel Dörfler return address; 154561729d93SAxel Dörfler } 154661729d93SAxel Dörfler 154761729d93SAxel Dörfler return NULL; 154861729d93SAxel Dörfler } 154961729d93SAxel Dörfler 155061729d93SAxel Dörfler 1551cd08b9f7SAxel Dörfler /*! Returns a reference to an InterfaceAddress of the specified \a domain that 1552cd08b9f7SAxel Dörfler belongs to the interface identified via \a linkAddress. Only the hardware 1553cd08b9f7SAxel Dörfler address is matched. 1554cd08b9f7SAxel Dörfler 1555cd08b9f7SAxel Dörfler If \a unconfiguredOnly is set, the interface address must not yet be 1556cd08b9f7SAxel Dörfler configured, or must currently be in the process of being configured. 1557cd08b9f7SAxel Dörfler */ 155861729d93SAxel Dörfler InterfaceAddress* 1559cd08b9f7SAxel Dörfler get_interface_address_for_link(net_domain* domain, const sockaddr* address, 1560cd08b9f7SAxel Dörfler bool unconfiguredOnly) 156161729d93SAxel Dörfler { 1562cd08b9f7SAxel Dörfler sockaddr_dl& linkAddress = *(sockaddr_dl*)address; 156361729d93SAxel Dörfler 15648a6337d0SStefano Ceccherini RecursiveLocker locker(sLock); 156561729d93SAxel Dörfler 156661729d93SAxel Dörfler InterfaceList::Iterator iterator = sInterfaces.GetIterator(); 156761729d93SAxel Dörfler while (Interface* interface = iterator.Next()) { 1568ff8c8dfcSStefano Ceccherini if (interface->IsBusy()) 1569ff8c8dfcSStefano Ceccherini continue; 1570cd08b9f7SAxel Dörfler // Test if the hardware address matches, or if the given interface 1571cd08b9f7SAxel Dörfler // matches, or if at least the index matches. 157261729d93SAxel Dörfler if (linkAddress.sdl_alen == interface->device->address.length 157361729d93SAxel Dörfler && memcmp(LLADDR(&linkAddress), interface->device->address.data, 157461729d93SAxel Dörfler linkAddress.sdl_alen) == 0) { 1575b0cf274aSAxel Dörfler TRACE(" %s matches\n", interface->name); 157661729d93SAxel Dörfler // link address matches 157761729d93SAxel Dörfler if (unconfiguredOnly) 157861729d93SAxel Dörfler return interface->FirstUnconfiguredForFamily(domain->family); 157961729d93SAxel Dörfler 158061729d93SAxel Dörfler return interface->FirstForFamily(domain->family); 158161729d93SAxel Dörfler } 158261729d93SAxel Dörfler } 158361729d93SAxel Dörfler 158461729d93SAxel Dörfler return NULL; 1585c22d69bfSAxel Dörfler } 1586c22d69bfSAxel Dörfler 1587c22d69bfSAxel Dörfler 1588c22d69bfSAxel Dörfler uint32 158961729d93SAxel Dörfler count_interfaces() 1590c22d69bfSAxel Dörfler { 15918a6337d0SStefano Ceccherini RecursiveLocker locker(sLock); 1592c22d69bfSAxel Dörfler 159361729d93SAxel Dörfler return sInterfaces.Count(); 1594c22d69bfSAxel Dörfler } 1595c22d69bfSAxel Dörfler 1596c22d69bfSAxel Dörfler 15973c13a5f5SAxel Dörfler /*! Dumps a list of all interfaces into the supplied userland buffer. 1598c22d69bfSAxel Dörfler If the interfaces don't fit into the buffer, an error (\c ENOBUFS) is 1599c22d69bfSAxel Dörfler returned. 1600c22d69bfSAxel Dörfler */ 1601c22d69bfSAxel Dörfler status_t 160261729d93SAxel Dörfler list_interfaces(int family, void* _buffer, size_t* bufferSize) 1603c22d69bfSAxel Dörfler { 16048a6337d0SStefano Ceccherini RecursiveLocker locker(sLock); 1605c22d69bfSAxel Dörfler 1606409b1fc0SHugo Santos UserBuffer buffer(_buffer, *bufferSize); 1607c22d69bfSAxel Dörfler 160861729d93SAxel Dörfler InterfaceList::Iterator iterator = sInterfaces.GetIterator(); 160961729d93SAxel Dörfler while (Interface* interface = iterator.Next()) { 1610715fed44SAxel Dörfler // Copy name 1611715fed44SAxel Dörfler buffer.Push(interface->name, IF_NAMESIZE); 1612c22d69bfSAxel Dörfler 1613715fed44SAxel Dörfler // Copy address 161461729d93SAxel Dörfler InterfaceAddress* address = interface->FirstForFamily(family); 16151f9c8c45SAxel Dörfler size_t length = 0; 1616715fed44SAxel Dörfler 161761729d93SAxel Dörfler if (address != NULL && address->local != NULL) { 1618715fed44SAxel Dörfler // Actual address 16191f9c8c45SAxel Dörfler buffer.Push(address->local, length = address->local->sa_len); 162061729d93SAxel Dörfler } else { 1621715fed44SAxel Dörfler // Empty address 16221f9c8c45SAxel Dörfler sockaddr empty; 16231f9c8c45SAxel Dörfler empty.sa_len = 2; 16241f9c8c45SAxel Dörfler empty.sa_family = AF_UNSPEC; 16251f9c8c45SAxel Dörfler buffer.Push(&empty, length = empty.sa_len); 1626715fed44SAxel Dörfler } 1627715fed44SAxel Dörfler 162861729d93SAxel Dörfler if (address != NULL) 162961729d93SAxel Dörfler address->ReleaseReference(); 163061729d93SAxel Dörfler 16311f9c8c45SAxel Dörfler if (IF_NAMESIZE + length < sizeof(ifreq)) { 16321f9c8c45SAxel Dörfler // Make sure at least sizeof(ifreq) bytes are written for each 16331f9c8c45SAxel Dörfler // interface for compatibility with other platforms 16341f9c8c45SAxel Dörfler buffer.Pad(sizeof(ifreq) - IF_NAMESIZE - length); 16351f9c8c45SAxel Dörfler } 16361f9c8c45SAxel Dörfler 16371f9c8c45SAxel Dörfler if (buffer.Status() != B_OK) 1638409b1fc0SHugo Santos return buffer.Status(); 1639c22d69bfSAxel Dörfler } 1640c22d69bfSAxel Dörfler 1641715fed44SAxel Dörfler *bufferSize = buffer.BytesConsumed(); 1642c22d69bfSAxel Dörfler return B_OK; 1643c22d69bfSAxel Dörfler } 1644c22d69bfSAxel Dörfler 1645c22d69bfSAxel Dörfler 1646c22d69bfSAxel Dörfler // #pragma mark - 1647c22d69bfSAxel Dörfler 1648c22d69bfSAxel Dörfler 1649c22d69bfSAxel Dörfler status_t 1650c22d69bfSAxel Dörfler init_interfaces() 1651c22d69bfSAxel Dörfler { 16528a6337d0SStefano Ceccherini recursive_lock_init(&sLock, "net interfaces"); 165361729d93SAxel Dörfler mutex_init(&sHashLock, "net local addresses"); 1654c22d69bfSAxel Dörfler 165561729d93SAxel Dörfler new (&sInterfaces) InterfaceList; 165661729d93SAxel Dörfler new (&sAddressTable) AddressTable; 16579206bb37SAxel Dörfler // static C++ objects are not initialized in the module startup 1658a3ec278aSAxel Dörfler 1659a3ec278aSAxel Dörfler #if ENABLE_DEBUGGER_COMMANDS 1660a3ec278aSAxel Dörfler add_debugger_command("net_interface", &dump_interface, 1661a3ec278aSAxel Dörfler "Dump the given network interface"); 166261729d93SAxel Dörfler add_debugger_command("net_interfaces", &dump_interfaces, 166361729d93SAxel Dörfler "Dump all network interfaces"); 166461729d93SAxel Dörfler add_debugger_command("net_local", &dump_local, 166561729d93SAxel Dörfler "Dump all local interface addresses"); 166661729d93SAxel Dörfler add_debugger_command("net_route", &dump_route, 166761729d93SAxel Dörfler "Dump the given network route"); 1668a3ec278aSAxel Dörfler #endif 1669c22d69bfSAxel Dörfler return B_OK; 1670c22d69bfSAxel Dörfler } 1671c22d69bfSAxel Dörfler 1672c22d69bfSAxel Dörfler 1673c22d69bfSAxel Dörfler status_t 1674c22d69bfSAxel Dörfler uninit_interfaces() 1675c22d69bfSAxel Dörfler { 1676a3ec278aSAxel Dörfler #if ENABLE_DEBUGGER_COMMANDS 1677a3ec278aSAxel Dörfler remove_debugger_command("net_interface", &dump_interface); 16783f2a0963SPhilippe Houdoin remove_debugger_command("net_interfaces", &dump_interfaces); 16793f2a0963SPhilippe Houdoin remove_debugger_command("net_local", &dump_local); 16803f2a0963SPhilippe Houdoin remove_debugger_command("net_route", &dump_route); 1681a3ec278aSAxel Dörfler #endif 1682a3ec278aSAxel Dörfler 16838a6337d0SStefano Ceccherini recursive_lock_destroy(&sLock); 168461729d93SAxel Dörfler mutex_destroy(&sHashLock); 1685c22d69bfSAxel Dörfler return B_OK; 1686c22d69bfSAxel Dörfler } 1687c22d69bfSAxel Dörfler 1688