blob: 91ec1709fb0af8dcea075f67dbfef4e42f881dda [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
14#include <base/logging.h>
15
Eric Shienbrood9a245532012-03-07 14:20:39 -050016#include "shill/adaptor_interfaces.h"
Paul Stewartb50f0b92011-05-16 16:31:42 -070017#include "shill/control_interface.h"
18#include "shill/device.h"
Paul Stewartf1ce5d22011-05-19 13:10:20 -070019#include "shill/device_info.h"
Paul Stewartf1ce5d22011-05-19 13:10:20 -070020#include "shill/ethernet_service.h"
Paul Stewart26b327e2011-10-19 11:38:09 -070021#include "shill/event_dispatcher.h"
22#include "shill/manager.h"
Chris Masone7aa5f902011-07-11 11:13:35 -070023#include "shill/profile.h"
Paul Stewartf1ce5d22011-05-19 13:10:20 -070024#include "shill/rtnl_handler.h"
Paul Stewartb50f0b92011-05-16 16:31:42 -070025
Chris Masonea82b7112011-05-25 15:16:29 -070026using std::string;
27
Paul Stewartb50f0b92011-05-16 16:31:42 -070028namespace shill {
Darin Petkovafa6fc42011-06-21 16:21:08 -070029
Paul Stewartb50f0b92011-05-16 16:31:42 -070030Ethernet::Ethernet(ControlInterface *control_interface,
31 EventDispatcher *dispatcher,
Thieu Le3426c8f2012-01-11 17:35:11 -080032 Metrics *metrics,
Paul Stewartf1ce5d22011-05-19 13:10:20 -070033 Manager *manager,
Darin Petkovafa6fc42011-06-21 16:21:08 -070034 const string &link_name,
mukesh agrawal93a29ed2012-04-17 16:13:01 -070035 const string &address,
Paul Stewartb50f0b92011-05-16 16:31:42 -070036 int interface_index)
Chris Masonea82b7112011-05-25 15:16:29 -070037 : Device(control_interface,
38 dispatcher,
Thieu Le3426c8f2012-01-11 17:35:11 -080039 metrics,
Chris Masonea82b7112011-05-25 15:16:29 -070040 manager,
41 link_name,
Chris Masone626719f2011-08-18 16:58:48 -070042 address,
Gaurav Shah435de2c2011-11-17 19:01:07 -080043 interface_index,
44 Technology::kEthernet),
mukesh agrawalf60e4062011-05-27 13:13:41 -070045 service_registered_(false),
mukesh agrawalf60e4062011-05-27 13:13:41 -070046 link_up_(false) {
Darin Petkovafa6fc42011-06-21 16:21:08 -070047 VLOG(2) << "Ethernet device " << link_name << " initialized.";
Paul Stewartb50f0b92011-05-16 16:31:42 -070048}
49
50Ethernet::~Ethernet() {
Eric Shienbrood9a245532012-03-07 14:20:39 -050051 Stop(NULL, EnabledStateChangedCallback());
Paul Stewartf1ce5d22011-05-19 13:10:20 -070052}
53
Eric Shienbrood9a245532012-03-07 14:20:39 -050054void Ethernet::Start(Error *error,
55 const EnabledStateChangedCallback &callback) {
Paul Stewart2713d6c2011-08-25 15:38:15 -070056 service_ = new EthernetService(control_interface(),
57 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -080058 metrics(),
Paul Stewart2713d6c2011-08-25 15:38:15 -070059 manager(),
60 this);
Paul Stewartac4ac002011-08-26 12:04:26 -070061 RTNLHandler::GetInstance()->SetInterfaceFlags(interface_index(), IFF_UP,
Paul Stewartf1ce5d22011-05-19 13:10:20 -070062 IFF_UP);
Eric Shienbrood9a245532012-03-07 14:20:39 -050063 OnEnabledStateChanged(EnabledStateChangedCallback(), Error());
64 if (error)
65 error->Reset(); // indicate immediate completion
Paul Stewartf1ce5d22011-05-19 13:10:20 -070066}
67
Eric Shienbrood9a245532012-03-07 14:20:39 -050068void Ethernet::Stop(Error *error, const EnabledStateChangedCallback &callback) {
Paul Stewartf2792e82011-12-12 10:23:11 -080069 if (service_) {
70 manager()->DeregisterService(service_);
71 service_ = NULL;
72 }
Eric Shienbrood9a245532012-03-07 14:20:39 -050073 OnEnabledStateChanged(EnabledStateChangedCallback(), Error());
74 if (error)
75 error->Reset(); // indicate immediate completion
Paul Stewartb50f0b92011-05-16 16:31:42 -070076}
77
Paul Stewartfdd16072011-09-16 12:41:35 -070078bool Ethernet::TechnologyIs(const Technology::Identifier type) const {
79 return type == Technology::kEthernet;
Paul Stewartb50f0b92011-05-16 16:31:42 -070080}
81
Darin Petkovafa6fc42011-06-21 16:21:08 -070082void Ethernet::LinkEvent(unsigned int flags, unsigned int change) {
Paul Stewartf1ce5d22011-05-19 13:10:20 -070083 Device::LinkEvent(flags, change);
84 if ((flags & IFF_LOWER_UP) != 0 && !link_up_) {
Paul Stewartac4ac002011-08-26 12:04:26 -070085 LOG(INFO) << link_name() << " is up; should start L3!";
Paul Stewartf1ce5d22011-05-19 13:10:20 -070086 link_up_ = true;
Eric Shienbrood9a245532012-03-07 14:20:39 -050087 if (service_) {
88 manager()->RegisterService(service_);
89 if (service_->auto_connect()) {
90 if (AcquireIPConfig()) {
91 SelectService(service_);
92 SetServiceState(Service::kStateConfiguring);
93 } else {
94 LOG(ERROR) << "Unable to acquire DHCP config.";
95 }
Paul Stewart03dba0b2011-08-22 16:32:45 -070096 }
Darin Petkovafa6fc42011-06-21 16:21:08 -070097 }
Paul Stewartf1ce5d22011-05-19 13:10:20 -070098 } else if ((flags & IFF_LOWER_UP) == 0 && link_up_) {
99 link_up_ = false;
Paul Stewart63c93932012-01-06 11:24:12 -0800100 DestroyIPConfig();
Paul Stewartac4ac002011-08-26 12:04:26 -0700101 manager()->DeregisterService(service_);
Paul Stewart03dba0b2011-08-22 16:32:45 -0700102 SelectService(NULL);
Paul Stewartf1ce5d22011-05-19 13:10:20 -0700103 }
104}
105
Paul Stewartb50f0b92011-05-16 16:31:42 -0700106} // namespace shill