Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium OS Authors. All rights reserved. |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
Chris Masone | b2e326b | 2011-07-12 13:28:51 -0700 | [diff] [blame] | 5 | #include "shill/ethernet.h" |
| 6 | |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 7 | #include <time.h> |
| 8 | #include <stdio.h> |
Paul Stewart | f1ce5d2 | 2011-05-19 13:10:20 -0700 | [diff] [blame] | 9 | #include <netinet/ether.h> |
mukesh agrawal | 5c4dd0b | 2011-09-14 13:53:14 -0700 | [diff] [blame] | 10 | #include <linux/if.h> // Needs definitions from netinet/ether.h |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 11 | |
| 12 | #include <string> |
| 13 | |
| 14 | #include <base/logging.h> |
| 15 | |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 16 | #include "shill/adaptor_interfaces.h" |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 17 | #include "shill/control_interface.h" |
| 18 | #include "shill/device.h" |
Paul Stewart | f1ce5d2 | 2011-05-19 13:10:20 -0700 | [diff] [blame] | 19 | #include "shill/device_info.h" |
Paul Stewart | f1ce5d2 | 2011-05-19 13:10:20 -0700 | [diff] [blame] | 20 | #include "shill/ethernet_service.h" |
Paul Stewart | 26b327e | 2011-10-19 11:38:09 -0700 | [diff] [blame] | 21 | #include "shill/event_dispatcher.h" |
| 22 | #include "shill/manager.h" |
Chris Masone | 7aa5f90 | 2011-07-11 11:13:35 -0700 | [diff] [blame] | 23 | #include "shill/profile.h" |
Paul Stewart | f1ce5d2 | 2011-05-19 13:10:20 -0700 | [diff] [blame] | 24 | #include "shill/rtnl_handler.h" |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 25 | #include "shill/scope_logger.h" |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 26 | |
Chris Masone | a82b711 | 2011-05-25 15:16:29 -0700 | [diff] [blame] | 27 | using std::string; |
| 28 | |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 29 | namespace shill { |
Darin Petkov | afa6fc4 | 2011-06-21 16:21:08 -0700 | [diff] [blame] | 30 | |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 31 | Ethernet::Ethernet(ControlInterface *control_interface, |
| 32 | EventDispatcher *dispatcher, |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 33 | Metrics *metrics, |
Paul Stewart | f1ce5d2 | 2011-05-19 13:10:20 -0700 | [diff] [blame] | 34 | Manager *manager, |
Darin Petkov | afa6fc4 | 2011-06-21 16:21:08 -0700 | [diff] [blame] | 35 | const string &link_name, |
mukesh agrawal | 93a29ed | 2012-04-17 16:13:01 -0700 | [diff] [blame] | 36 | const string &address, |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 37 | int interface_index) |
Chris Masone | a82b711 | 2011-05-25 15:16:29 -0700 | [diff] [blame] | 38 | : Device(control_interface, |
| 39 | dispatcher, |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 40 | metrics, |
Chris Masone | a82b711 | 2011-05-25 15:16:29 -0700 | [diff] [blame] | 41 | manager, |
| 42 | link_name, |
Chris Masone | 626719f | 2011-08-18 16:58:48 -0700 | [diff] [blame] | 43 | address, |
Gaurav Shah | 435de2c | 2011-11-17 19:01:07 -0800 | [diff] [blame] | 44 | interface_index, |
| 45 | Technology::kEthernet), |
mukesh agrawal | f60e406 | 2011-05-27 13:13:41 -0700 | [diff] [blame] | 46 | service_registered_(false), |
mukesh agrawal | f60e406 | 2011-05-27 13:13:41 -0700 | [diff] [blame] | 47 | link_up_(false) { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 48 | SLOG(Ethernet, 2) << "Ethernet device " << link_name << " initialized."; |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | Ethernet::~Ethernet() { |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 52 | Stop(NULL, EnabledStateChangedCallback()); |
Paul Stewart | f1ce5d2 | 2011-05-19 13:10:20 -0700 | [diff] [blame] | 53 | } |
| 54 | |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 55 | void Ethernet::Start(Error *error, |
| 56 | const EnabledStateChangedCallback &callback) { |
Paul Stewart | 2713d6c | 2011-08-25 15:38:15 -0700 | [diff] [blame] | 57 | service_ = new EthernetService(control_interface(), |
| 58 | dispatcher(), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 59 | metrics(), |
Paul Stewart | 2713d6c | 2011-08-25 15:38:15 -0700 | [diff] [blame] | 60 | manager(), |
| 61 | this); |
Paul Stewart | ac4ac00 | 2011-08-26 12:04:26 -0700 | [diff] [blame] | 62 | RTNLHandler::GetInstance()->SetInterfaceFlags(interface_index(), IFF_UP, |
Paul Stewart | f1ce5d2 | 2011-05-19 13:10:20 -0700 | [diff] [blame] | 63 | IFF_UP); |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 64 | OnEnabledStateChanged(EnabledStateChangedCallback(), Error()); |
| 65 | if (error) |
| 66 | error->Reset(); // indicate immediate completion |
Paul Stewart | f1ce5d2 | 2011-05-19 13:10:20 -0700 | [diff] [blame] | 67 | } |
| 68 | |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 69 | void Ethernet::Stop(Error *error, const EnabledStateChangedCallback &callback) { |
Paul Stewart | f2792e8 | 2011-12-12 10:23:11 -0800 | [diff] [blame] | 70 | if (service_) { |
| 71 | manager()->DeregisterService(service_); |
| 72 | service_ = NULL; |
| 73 | } |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 74 | OnEnabledStateChanged(EnabledStateChangedCallback(), Error()); |
| 75 | if (error) |
| 76 | error->Reset(); // indicate immediate completion |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 77 | } |
| 78 | |
Paul Stewart | fdd1607 | 2011-09-16 12:41:35 -0700 | [diff] [blame] | 79 | bool Ethernet::TechnologyIs(const Technology::Identifier type) const { |
| 80 | return type == Technology::kEthernet; |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 81 | } |
| 82 | |
Darin Petkov | afa6fc4 | 2011-06-21 16:21:08 -0700 | [diff] [blame] | 83 | void Ethernet::LinkEvent(unsigned int flags, unsigned int change) { |
Paul Stewart | f1ce5d2 | 2011-05-19 13:10:20 -0700 | [diff] [blame] | 84 | Device::LinkEvent(flags, change); |
| 85 | if ((flags & IFF_LOWER_UP) != 0 && !link_up_) { |
Paul Stewart | ac4ac00 | 2011-08-26 12:04:26 -0700 | [diff] [blame] | 86 | LOG(INFO) << link_name() << " is up; should start L3!"; |
Paul Stewart | f1ce5d2 | 2011-05-19 13:10:20 -0700 | [diff] [blame] | 87 | link_up_ = true; |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 88 | if (service_) { |
| 89 | manager()->RegisterService(service_); |
| 90 | if (service_->auto_connect()) { |
Paul Stewart | d408fdf | 2012-05-07 17:15:57 -0700 | [diff] [blame] | 91 | if (AcquireIPConfigWithLeaseName(service_->GetStorageIdentifier())) { |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 92 | SelectService(service_); |
| 93 | SetServiceState(Service::kStateConfiguring); |
| 94 | } else { |
| 95 | LOG(ERROR) << "Unable to acquire DHCP config."; |
| 96 | } |
Paul Stewart | 03dba0b | 2011-08-22 16:32:45 -0700 | [diff] [blame] | 97 | } |
Darin Petkov | afa6fc4 | 2011-06-21 16:21:08 -0700 | [diff] [blame] | 98 | } |
Paul Stewart | f1ce5d2 | 2011-05-19 13:10:20 -0700 | [diff] [blame] | 99 | } else if ((flags & IFF_LOWER_UP) == 0 && link_up_) { |
| 100 | link_up_ = false; |
Paul Stewart | 63c9393 | 2012-01-06 11:24:12 -0800 | [diff] [blame] | 101 | DestroyIPConfig(); |
Paul Stewart | ac4ac00 | 2011-08-26 12:04:26 -0700 | [diff] [blame] | 102 | manager()->DeregisterService(service_); |
Paul Stewart | 03dba0b | 2011-08-22 16:32:45 -0700 | [diff] [blame] | 103 | SelectService(NULL); |
Paul Stewart | f1ce5d2 | 2011-05-19 13:10:20 -0700 | [diff] [blame] | 104 | } |
| 105 | } |
| 106 | |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 107 | } // namespace shill |