blob: 7f4a445929e18ff8ee43953fb785167afd81e08c [file] [log] [blame]
Thieu Le3426c8f2012-01-11 17:35:11 -08001// Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
Paul Stewartb50f0b92011-05-16 16:31:42 -07002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
Chris Masoneb2e326b2011-07-12 13:28:51 -07005#include "shill/ethernet.h"
6
Paul Stewartb50f0b92011-05-16 16:31:42 -07007#include <time.h>
8#include <stdio.h>
Paul Stewartf1ce5d22011-05-19 13:10:20 -07009#include <netinet/ether.h>
mukesh agrawal5c4dd0b2011-09-14 13:53:14 -070010#include <linux/if.h> // Needs definitions from netinet/ether.h
Paul Stewartb50f0b92011-05-16 16:31:42 -070011
12#include <string>
13
Eric Shienbrood9a245532012-03-07 14:20:39 -050014#include "shill/adaptor_interfaces.h"
Paul Stewartb50f0b92011-05-16 16:31:42 -070015#include "shill/control_interface.h"
16#include "shill/device.h"
Paul Stewartf1ce5d22011-05-19 13:10:20 -070017#include "shill/device_info.h"
Paul Stewartf1ce5d22011-05-19 13:10:20 -070018#include "shill/ethernet_service.h"
Paul Stewart26b327e2011-10-19 11:38:09 -070019#include "shill/event_dispatcher.h"
Christopher Wileyb691efd2012-08-09 13:51:51 -070020#include "shill/logging.h"
Paul Stewart26b327e2011-10-19 11:38:09 -070021#include "shill/manager.h"
Chris Masone7aa5f902011-07-11 11:13:35 -070022#include "shill/profile.h"
Paul Stewartf1ce5d22011-05-19 13:10:20 -070023#include "shill/rtnl_handler.h"
Paul Stewartb50f0b92011-05-16 16:31:42 -070024
Chris Masonea82b7112011-05-25 15:16:29 -070025using std::string;
26
Paul Stewartb50f0b92011-05-16 16:31:42 -070027namespace shill {
Darin Petkovafa6fc42011-06-21 16:21:08 -070028
Paul Stewartb50f0b92011-05-16 16:31:42 -070029Ethernet::Ethernet(ControlInterface *control_interface,
30 EventDispatcher *dispatcher,
Thieu Le3426c8f2012-01-11 17:35:11 -080031 Metrics *metrics,
Paul Stewartf1ce5d22011-05-19 13:10:20 -070032 Manager *manager,
Darin Petkovafa6fc42011-06-21 16:21:08 -070033 const string &link_name,
mukesh agrawal93a29ed2012-04-17 16:13:01 -070034 const string &address,
Paul Stewartb50f0b92011-05-16 16:31:42 -070035 int interface_index)
Chris Masonea82b7112011-05-25 15:16:29 -070036 : Device(control_interface,
37 dispatcher,
Thieu Le3426c8f2012-01-11 17:35:11 -080038 metrics,
Chris Masonea82b7112011-05-25 15:16:29 -070039 manager,
40 link_name,
Chris Masone626719f2011-08-18 16:58:48 -070041 address,
Gaurav Shah435de2c2011-11-17 19:01:07 -080042 interface_index,
43 Technology::kEthernet),
mukesh agrawalf60e4062011-05-27 13:13:41 -070044 service_registered_(false),
mukesh agrawalf60e4062011-05-27 13:13:41 -070045 link_up_(false) {
Ben Chanfad4a0b2012-04-18 15:49:59 -070046 SLOG(Ethernet, 2) << "Ethernet device " << link_name << " initialized.";
Paul Stewartb50f0b92011-05-16 16:31:42 -070047}
48
49Ethernet::~Ethernet() {
Eric Shienbrood9a245532012-03-07 14:20:39 -050050 Stop(NULL, EnabledStateChangedCallback());
Paul Stewartf1ce5d22011-05-19 13:10:20 -070051}
52
Eric Shienbrood9a245532012-03-07 14:20:39 -050053void Ethernet::Start(Error *error,
54 const EnabledStateChangedCallback &callback) {
Paul Stewart2713d6c2011-08-25 15:38:15 -070055 service_ = new EthernetService(control_interface(),
56 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -080057 metrics(),
Paul Stewart2713d6c2011-08-25 15:38:15 -070058 manager(),
59 this);
Paul Stewartac4ac002011-08-26 12:04:26 -070060 RTNLHandler::GetInstance()->SetInterfaceFlags(interface_index(), IFF_UP,
Paul Stewartf1ce5d22011-05-19 13:10:20 -070061 IFF_UP);
Eric Shienbrood9a245532012-03-07 14:20:39 -050062 OnEnabledStateChanged(EnabledStateChangedCallback(), Error());
63 if (error)
64 error->Reset(); // indicate immediate completion
Paul Stewartf1ce5d22011-05-19 13:10:20 -070065}
66
Eric Shienbrood9a245532012-03-07 14:20:39 -050067void Ethernet::Stop(Error *error, const EnabledStateChangedCallback &callback) {
Paul Stewartf2792e82011-12-12 10:23:11 -080068 if (service_) {
69 manager()->DeregisterService(service_);
70 service_ = NULL;
71 }
Eric Shienbrood9a245532012-03-07 14:20:39 -050072 OnEnabledStateChanged(EnabledStateChangedCallback(), Error());
73 if (error)
74 error->Reset(); // indicate immediate completion
Paul Stewartb50f0b92011-05-16 16:31:42 -070075}
76
Darin Petkovafa6fc42011-06-21 16:21:08 -070077void Ethernet::LinkEvent(unsigned int flags, unsigned int change) {
Paul Stewartf1ce5d22011-05-19 13:10:20 -070078 Device::LinkEvent(flags, change);
79 if ((flags & IFF_LOWER_UP) != 0 && !link_up_) {
Paul Stewartac4ac002011-08-26 12:04:26 -070080 LOG(INFO) << link_name() << " is up; should start L3!";
Paul Stewartf1ce5d22011-05-19 13:10:20 -070081 link_up_ = true;
Eric Shienbrood9a245532012-03-07 14:20:39 -050082 if (service_) {
83 manager()->RegisterService(service_);
84 if (service_->auto_connect()) {
Paul Stewartd408fdf2012-05-07 17:15:57 -070085 if (AcquireIPConfigWithLeaseName(service_->GetStorageIdentifier())) {
Eric Shienbrood9a245532012-03-07 14:20:39 -050086 SelectService(service_);
87 SetServiceState(Service::kStateConfiguring);
88 } else {
89 LOG(ERROR) << "Unable to acquire DHCP config.";
90 }
Paul Stewart03dba0b2011-08-22 16:32:45 -070091 }
Darin Petkovafa6fc42011-06-21 16:21:08 -070092 }
Paul Stewartf1ce5d22011-05-19 13:10:20 -070093 } else if ((flags & IFF_LOWER_UP) == 0 && link_up_) {
94 link_up_ = false;
Paul Stewart63c93932012-01-06 11:24:12 -080095 DestroyIPConfig();
Paul Stewartac4ac002011-08-26 12:04:26 -070096 manager()->DeregisterService(service_);
Paul Stewart03dba0b2011-08-22 16:32:45 -070097 SelectService(NULL);
Paul Stewartf1ce5d22011-05-19 13:10:20 -070098 }
99}
100
Paul Stewartb50f0b92011-05-16 16:31:42 -0700101} // namespace shill