blob: 810bb7f9c3f689144539f5b8c0f6b7f578fa792a [file] [log] [blame]
Wade Guthrie0d438532012-05-18 14:18:50 -07001// Copyright (c) 2012 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//
5// This code is derived from the 'iw' source code. The copyright and license
6// of that code is as follows:
7//
8// Copyright (c) 2007, 2008 Johannes Berg
9// Copyright (c) 2007 Andy Lutomirski
10// Copyright (c) 2007 Mike Kershaw
11// Copyright (c) 2008-2009 Luis R. Rodriguez
12//
13// Permission to use, copy, modify, and/or distribute this software for any
14// purpose with or without fee is hereby granted, provided that the above
15// copyright notice and this permission notice appear in all copies.
16//
17// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
18// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
19// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
20// ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
21// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
22// ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
23// OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
24
25#include "shill/nl80211_socket.h"
26
27#include <ctype.h>
28#include <errno.h>
29
30#include <net/if.h>
31#include <netlink/attr.h>
32#include <netlink/genl/ctrl.h>
33#include <netlink/genl/family.h>
34#include <netlink/genl/genl.h>
35#include <netlink/msg.h>
36#include <netlink/netlink.h>
37
38#include <iomanip>
39#include <sstream>
40#include <string>
41
Wade Guthrie0d438532012-05-18 14:18:50 -070042#include "shill/logging.h"
43#include "shill/netlink_socket.h"
repo syncdc085c82012-12-28 08:54:41 -080044#include "shill/nl80211_message.h"
Wade Guthrie0d438532012-05-18 14:18:50 -070045
46using std::string;
47
48namespace shill {
49
50const char Nl80211Socket::kSocketFamilyName[] = "nl80211";
51
52bool Nl80211Socket::Init() {
53 if (!NetlinkSocket::Init()) {
54 LOG(ERROR) << "NetlinkSocket didn't initialize.";
55 return false;
56 }
57
repo syncdc085c82012-12-28 08:54:41 -080058 family_id_ = genl_ctrl_resolve(GetNlSock(), kSocketFamilyName);
59 if (family_id_ < 0) {
60 LOG(ERROR) << kSocketFamilyName << " not found.";
Wade Guthrie0d438532012-05-18 14:18:50 -070061 return false;
62 }
63
Wade Guthried6153612012-08-23 11:36:14 -070064 LOG(INFO) << "Nl80211Socket initialized successfully";
Wade Guthrie0d438532012-05-18 14:18:50 -070065 return true;
66}
67
68bool Nl80211Socket::AddGroupMembership(const string &group_name) {
Christopher Wileya6032b22012-11-07 21:34:46 -080069 int id = genl_ctrl_resolve_grp(GetNlSock(),
70 kSocketFamilyName,
71 group_name.c_str());
Wade Guthrie0d438532012-05-18 14:18:50 -070072 if (id < 0) {
73 LOG(ERROR) << "No Id for group " << group_name;
74 return false;
Christopher Wileya6032b22012-11-07 21:34:46 -080075 }
76 int result = nl_socket_add_membership(GetNlSock(), id);
77 if (result != 0) {
78 LOG(ERROR) << "Failed to join netlink multicast group: " << result;
79 return false;
Wade Guthrie0d438532012-05-18 14:18:50 -070080 }
Wade Guthried6153612012-08-23 11:36:14 -070081 LOG(INFO) << " Group " << group_name << " added successfully";
Wade Guthrie0d438532012-05-18 14:18:50 -070082 return true;
83}
84
Wade Guthrie0d438532012-05-18 14:18:50 -070085} // namespace shill