Paul Stewart | ac802ac | 2013-04-02 15:45:24 -0700 | [diff] [blame] | 1 | // Copyright (c) 2013 The Chromium OS Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "shill/eap_listener.h" |
| 6 | |
| 7 | #include <linux/if_ether.h> |
| 8 | #include <linux/if_packet.h> |
| 9 | #include <netinet/in.h> |
| 10 | |
| 11 | #include <base/compiler_specific.h> |
| 12 | #include <base/bind.h> |
| 13 | |
| 14 | #include "shill/eap_protocol.h" |
| 15 | #include "shill/event_dispatcher.h" |
| 16 | #include "shill/logging.h" |
| 17 | #include "shill/sockets.h" |
| 18 | |
| 19 | namespace shill { |
| 20 | |
| 21 | const size_t EapListener::kMaxEapPacketLength = |
| 22 | sizeof(eap_protocol::Ieee8021xHdr) + sizeof(eap_protocol::EapHeader); |
| 23 | |
| 24 | EapListener::EapListener(EventDispatcher *event_dispatcher, |
| 25 | int interface_index) |
| 26 | : dispatcher_(event_dispatcher), |
| 27 | interface_index_(interface_index), |
| 28 | sockets_(new Sockets()), |
| 29 | socket_(-1) {} |
| 30 | |
| 31 | EapListener::~EapListener() {} |
| 32 | |
| 33 | bool EapListener::Start() { |
| 34 | if (!CreateSocket()) { |
| 35 | LOG(ERROR) << "Could not open EAP listener socket."; |
| 36 | Stop(); |
| 37 | return false; |
| 38 | } |
| 39 | |
| 40 | receive_request_handler_.reset( |
| 41 | dispatcher_->CreateReadyHandler( |
| 42 | socket_, |
| 43 | IOHandler::kModeInput, |
| 44 | base::Bind(&EapListener::ReceiveRequest, base::Unretained(this)))); |
| 45 | |
| 46 | return true; |
| 47 | } |
| 48 | |
| 49 | void EapListener::Stop() { |
| 50 | receive_request_handler_.reset(); |
| 51 | socket_closer_.reset(); |
| 52 | socket_ = -1; |
| 53 | } |
| 54 | |
| 55 | |
| 56 | bool EapListener::CreateSocket() { |
| 57 | int socket = sockets_->Socket(PF_PACKET, SOCK_DGRAM, htons(ETH_P_PAE)); |
| 58 | if (socket == -1) { |
| 59 | PLOG(ERROR) << "Could not create EAP listener socket"; |
| 60 | return false; |
| 61 | } |
| 62 | socket_ = socket; |
| 63 | socket_closer_.reset(new ScopedSocketCloser(sockets_.get(), socket_)); |
| 64 | |
| 65 | if (sockets_->SetNonBlocking(socket_) != 0) { |
| 66 | PLOG(ERROR) << "Could not set socket to be non-blocking"; |
| 67 | return false; |
| 68 | } |
| 69 | |
| 70 | sockaddr_ll socket_address; |
| 71 | memset(&socket_address, 0, sizeof(socket_address)); |
| 72 | socket_address.sll_family = AF_PACKET; |
| 73 | socket_address.sll_protocol = htons(ETH_P_PAE); |
| 74 | socket_address.sll_ifindex = interface_index_; |
| 75 | |
| 76 | if (sockets_->Bind(socket_, |
| 77 | reinterpret_cast<struct sockaddr *>(&socket_address), |
| 78 | sizeof(socket_address)) != 0) { |
| 79 | PLOG(ERROR) << "Could not bind socket to interface"; |
| 80 | return false; |
| 81 | } |
| 82 | |
| 83 | return true; |
| 84 | } |
| 85 | |
| 86 | void EapListener::ReceiveRequest(int fd) { |
| 87 | struct ALIGNAS(1) { |
| 88 | eap_protocol::Ieee8021xHdr onex_header; |
| 89 | eap_protocol::EapHeader eap_header; |
| 90 | } payload; |
| 91 | sockaddr_ll remote_address; |
| 92 | memset(&remote_address, 0, sizeof(remote_address)); |
| 93 | socklen_t socklen = sizeof(remote_address); |
| 94 | int result = sockets_->RecvFrom( |
| 95 | socket_, &payload, sizeof(payload), 0, |
| 96 | reinterpret_cast<struct sockaddr *>(&remote_address), |
| 97 | &socklen); |
| 98 | if (result < 0) { |
| 99 | PLOG(ERROR) << "Socket recvfrom failed"; |
| 100 | Stop(); |
| 101 | return; |
| 102 | } |
| 103 | |
| 104 | if (result != sizeof(payload)) { |
| 105 | LOG(INFO) << "Short EAP packet received"; |
| 106 | return; |
| 107 | } |
| 108 | |
| 109 | if (payload.onex_header.version != eap_protocol::kIeee8021xEapolVersion2 || |
| 110 | payload.onex_header.type != eap_protocol::kIIeee8021xTypeEapPacket || |
| 111 | payload.eap_header.code != eap_protocol::kEapCodeRequest) { |
| 112 | LOG(INFO) << "Packet is not a valid EAP request"; |
| 113 | return; |
| 114 | } |
| 115 | |
| 116 | request_received_callback_.Run(); |
| 117 | } |
| 118 | |
| 119 | } // namespace shill |