blob: d636960afd33b7d94cf9e5b4d61daa21099fe974 [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
16#include "shill/control_interface.h"
17#include "shill/device.h"
Paul Stewartf1ce5d22011-05-19 13:10:20 -070018#include "shill/device_info.h"
Paul Stewartf1ce5d22011-05-19 13:10:20 -070019#include "shill/ethernet_service.h"
Paul Stewart26b327e2011-10-19 11:38:09 -070020#include "shill/event_dispatcher.h"
21#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,
Chris Masone626719f2011-08-18 16:58:48 -070034 const std::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) {
Darin Petkovafa6fc42011-06-21 16:21:08 -070046 VLOG(2) << "Ethernet device " << link_name << " initialized.";
Paul Stewartb50f0b92011-05-16 16:31:42 -070047}
48
49Ethernet::~Ethernet() {
Paul Stewartf1ce5d22011-05-19 13:10:20 -070050 Stop();
51}
52
53void Ethernet::Start() {
Paul Stewart2713d6c2011-08-25 15:38:15 -070054 service_ = new EthernetService(control_interface(),
55 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -080056 metrics(),
Paul Stewart2713d6c2011-08-25 15:38:15 -070057 manager(),
58 this);
Paul Stewartf1ce5d22011-05-19 13:10:20 -070059 Device::Start();
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);
62}
63
64void Ethernet::Stop() {
Paul Stewartf2792e82011-12-12 10:23:11 -080065 if (service_) {
66 manager()->DeregisterService(service_);
67 service_ = NULL;
68 }
Paul Stewartf1ce5d22011-05-19 13:10:20 -070069 Device::Stop();
Paul Stewartb50f0b92011-05-16 16:31:42 -070070}
71
Paul Stewartfdd16072011-09-16 12:41:35 -070072bool Ethernet::TechnologyIs(const Technology::Identifier type) const {
73 return type == Technology::kEthernet;
Paul Stewartb50f0b92011-05-16 16:31:42 -070074}
75
Darin Petkovafa6fc42011-06-21 16:21:08 -070076void Ethernet::LinkEvent(unsigned int flags, unsigned int change) {
Paul Stewartf1ce5d22011-05-19 13:10:20 -070077 Device::LinkEvent(flags, change);
78 if ((flags & IFF_LOWER_UP) != 0 && !link_up_) {
Paul Stewartac4ac002011-08-26 12:04:26 -070079 LOG(INFO) << link_name() << " is up; should start L3!";
Paul Stewartf1ce5d22011-05-19 13:10:20 -070080 link_up_ = true;
Paul Stewartac4ac002011-08-26 12:04:26 -070081 manager()->RegisterService(service_);
Darin Petkovafa6fc42011-06-21 16:21:08 -070082 if (service_->auto_connect()) {
Paul Stewart2bf1d352011-12-06 15:02:55 -080083 if (AcquireIPConfig()) {
Paul Stewart03dba0b2011-08-22 16:32:45 -070084 SelectService(service_);
85 SetServiceState(Service::kStateConfiguring);
86 } else {
87 LOG(ERROR) << "Unable to acquire DHCP config.";
88 }
Darin Petkovafa6fc42011-06-21 16:21:08 -070089 }
Paul Stewartf1ce5d22011-05-19 13:10:20 -070090 } else if ((flags & IFF_LOWER_UP) == 0 && link_up_) {
91 link_up_ = false;
Paul Stewart63c93932012-01-06 11:24:12 -080092 DestroyIPConfig();
Paul Stewartac4ac002011-08-26 12:04:26 -070093 manager()->DeregisterService(service_);
Paul Stewart03dba0b2011-08-22 16:32:45 -070094 SelectService(NULL);
Paul Stewartf1ce5d22011-05-19 13:10:20 -070095 }
96}
97
Paul Stewartb50f0b92011-05-16 16:31:42 -070098} // namespace shill