blob: 2592b0f93907b8b0e41dc2bfcf4f66cb54df7cfd [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"
19#include "shill/manager.h"
Paul Stewartb50f0b92011-05-16 16:31:42 -070020#include "shill/shill_event.h"
Paul Stewartf1ce5d22011-05-19 13:10:20 -070021#include "shill/ethernet_service.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,
Paul Stewartf1ce5d22011-05-19 13:10:20 -070040 interface_index),
mukesh agrawalf60e4062011-05-27 13:13:41 -070041 service_registered_(false),
mukesh agrawalf60e4062011-05-27 13:13:41 -070042 link_up_(false) {
Darin Petkovafa6fc42011-06-21 16:21:08 -070043 VLOG(2) << "Ethernet device " << link_name << " initialized.";
Paul Stewartb50f0b92011-05-16 16:31:42 -070044}
45
46Ethernet::~Ethernet() {
Paul Stewartf1ce5d22011-05-19 13:10:20 -070047 Stop();
48}
49
50void Ethernet::Start() {
Paul Stewart2713d6c2011-08-25 15:38:15 -070051 service_ = new EthernetService(control_interface(),
52 dispatcher(),
53 manager(),
54 this);
Paul Stewartf1ce5d22011-05-19 13:10:20 -070055 Device::Start();
Paul Stewartac4ac002011-08-26 12:04:26 -070056 RTNLHandler::GetInstance()->SetInterfaceFlags(interface_index(), IFF_UP,
Paul Stewartf1ce5d22011-05-19 13:10:20 -070057 IFF_UP);
58}
59
60void Ethernet::Stop() {
Paul Stewartac4ac002011-08-26 12:04:26 -070061 manager()->DeregisterService(service_);
Paul Stewart2713d6c2011-08-25 15:38:15 -070062 service_ = NULL;
Paul Stewartf1ce5d22011-05-19 13:10:20 -070063 Device::Stop();
Paul Stewartb50f0b92011-05-16 16:31:42 -070064}
65
Paul Stewartfdd16072011-09-16 12:41:35 -070066bool Ethernet::TechnologyIs(const Technology::Identifier type) const {
67 return type == Technology::kEthernet;
Paul Stewartb50f0b92011-05-16 16:31:42 -070068}
69
Darin Petkovafa6fc42011-06-21 16:21:08 -070070void Ethernet::LinkEvent(unsigned int flags, unsigned int change) {
Paul Stewartf1ce5d22011-05-19 13:10:20 -070071 Device::LinkEvent(flags, change);
72 if ((flags & IFF_LOWER_UP) != 0 && !link_up_) {
Paul Stewartac4ac002011-08-26 12:04:26 -070073 LOG(INFO) << link_name() << " is up; should start L3!";
Paul Stewartf1ce5d22011-05-19 13:10:20 -070074 link_up_ = true;
Paul Stewartac4ac002011-08-26 12:04:26 -070075 manager()->RegisterService(service_);
Darin Petkovafa6fc42011-06-21 16:21:08 -070076 if (service_->auto_connect()) {
Paul Stewart03dba0b2011-08-22 16:32:45 -070077 if (AcquireDHCPConfig()) {
78 SelectService(service_);
79 SetServiceState(Service::kStateConfiguring);
80 } else {
81 LOG(ERROR) << "Unable to acquire DHCP config.";
82 }
Darin Petkovafa6fc42011-06-21 16:21:08 -070083 }
Paul Stewartf1ce5d22011-05-19 13:10:20 -070084 } else if ((flags & IFF_LOWER_UP) == 0 && link_up_) {
85 link_up_ = false;
Paul Stewartac4ac002011-08-26 12:04:26 -070086 manager()->DeregisterService(service_);
Paul Stewart03dba0b2011-08-22 16:32:45 -070087 SelectService(NULL);
Darin Petkovafa6fc42011-06-21 16:21:08 -070088 DestroyIPConfig();
Paul Stewartf1ce5d22011-05-19 13:10:20 -070089 }
90}
91
Paul Stewartb50f0b92011-05-16 16:31:42 -070092} // namespace shill