blob: 1348301bc04fcc3fd32b128d1c6b3cc9fb1cd3b4 [file] [log] [blame]
Paul Stewartb50f0b92011-05-16 16:31:42 -07001// Copyright (c) 2011 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
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,
Paul Stewartf1ce5d22011-05-19 13:10:20 -070031 Manager *manager,
Darin Petkovafa6fc42011-06-21 16:21:08 -070032 const string &link_name,
Chris Masone626719f2011-08-18 16:58:48 -070033 const std::string &address,
Paul Stewartb50f0b92011-05-16 16:31:42 -070034 int interface_index)
Chris Masonea82b7112011-05-25 15:16:29 -070035 : Device(control_interface,
36 dispatcher,
37 manager,
38 link_name,
Chris Masone626719f2011-08-18 16:58:48 -070039 address,
Gaurav Shah435de2c2011-11-17 19:01:07 -080040 interface_index,
41 Technology::kEthernet),
mukesh agrawalf60e4062011-05-27 13:13:41 -070042 service_registered_(false),
mukesh agrawalf60e4062011-05-27 13:13:41 -070043 link_up_(false) {
Darin Petkovafa6fc42011-06-21 16:21:08 -070044 VLOG(2) << "Ethernet device " << link_name << " initialized.";
Paul Stewartb50f0b92011-05-16 16:31:42 -070045}
46
47Ethernet::~Ethernet() {
Paul Stewartf1ce5d22011-05-19 13:10:20 -070048 Stop();
49}
50
51void Ethernet::Start() {
Paul Stewart2713d6c2011-08-25 15:38:15 -070052 service_ = new EthernetService(control_interface(),
53 dispatcher(),
54 manager(),
55 this);
Paul Stewartf1ce5d22011-05-19 13:10:20 -070056 Device::Start();
Paul Stewartac4ac002011-08-26 12:04:26 -070057 RTNLHandler::GetInstance()->SetInterfaceFlags(interface_index(), IFF_UP,
Paul Stewartf1ce5d22011-05-19 13:10:20 -070058 IFF_UP);
59}
60
61void Ethernet::Stop() {
Paul Stewartf2792e82011-12-12 10:23:11 -080062 if (service_) {
63 manager()->DeregisterService(service_);
64 service_ = NULL;
65 }
Paul Stewartf1ce5d22011-05-19 13:10:20 -070066 Device::Stop();
Paul Stewartb50f0b92011-05-16 16:31:42 -070067}
68
Paul Stewartfdd16072011-09-16 12:41:35 -070069bool Ethernet::TechnologyIs(const Technology::Identifier type) const {
70 return type == Technology::kEthernet;
Paul Stewartb50f0b92011-05-16 16:31:42 -070071}
72
Darin Petkovafa6fc42011-06-21 16:21:08 -070073void Ethernet::LinkEvent(unsigned int flags, unsigned int change) {
Paul Stewartf1ce5d22011-05-19 13:10:20 -070074 Device::LinkEvent(flags, change);
75 if ((flags & IFF_LOWER_UP) != 0 && !link_up_) {
Paul Stewartac4ac002011-08-26 12:04:26 -070076 LOG(INFO) << link_name() << " is up; should start L3!";
Paul Stewartf1ce5d22011-05-19 13:10:20 -070077 link_up_ = true;
Paul Stewartac4ac002011-08-26 12:04:26 -070078 manager()->RegisterService(service_);
Darin Petkovafa6fc42011-06-21 16:21:08 -070079 if (service_->auto_connect()) {
Paul Stewart2bf1d352011-12-06 15:02:55 -080080 if (AcquireIPConfig()) {
Paul Stewart03dba0b2011-08-22 16:32:45 -070081 SelectService(service_);
82 SetServiceState(Service::kStateConfiguring);
83 } else {
84 LOG(ERROR) << "Unable to acquire DHCP config.";
85 }
Darin Petkovafa6fc42011-06-21 16:21:08 -070086 }
Paul Stewartf1ce5d22011-05-19 13:10:20 -070087 } else if ((flags & IFF_LOWER_UP) == 0 && link_up_) {
88 link_up_ = false;
Paul Stewart63c93932012-01-06 11:24:12 -080089 DestroyIPConfig();
Paul Stewartac4ac002011-08-26 12:04:26 -070090 manager()->DeregisterService(service_);
Paul Stewart03dba0b2011-08-22 16:32:45 -070091 SelectService(NULL);
Paul Stewartf1ce5d22011-05-19 13:10:20 -070092 }
93}
94
Paul Stewartb50f0b92011-05-16 16:31:42 -070095} // namespace shill