Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 1 | // 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 Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 42 | #include "shill/logging.h" |
| 43 | #include "shill/netlink_socket.h" |
repo sync | dc085c8 | 2012-12-28 08:54:41 -0800 | [diff] [blame] | 44 | #include "shill/nl80211_message.h" |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 45 | |
| 46 | using std::string; |
| 47 | |
| 48 | namespace shill { |
| 49 | |
| 50 | const char Nl80211Socket::kSocketFamilyName[] = "nl80211"; |
| 51 | |
| 52 | bool Nl80211Socket::Init() { |
| 53 | if (!NetlinkSocket::Init()) { |
| 54 | LOG(ERROR) << "NetlinkSocket didn't initialize."; |
| 55 | return false; |
| 56 | } |
| 57 | |
repo sync | dc085c8 | 2012-12-28 08:54:41 -0800 | [diff] [blame] | 58 | family_id_ = genl_ctrl_resolve(GetNlSock(), kSocketFamilyName); |
| 59 | if (family_id_ < 0) { |
| 60 | LOG(ERROR) << kSocketFamilyName << " not found."; |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 61 | return false; |
| 62 | } |
| 63 | |
Wade Guthrie | d615361 | 2012-08-23 11:36:14 -0700 | [diff] [blame] | 64 | LOG(INFO) << "Nl80211Socket initialized successfully"; |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 65 | return true; |
| 66 | } |
| 67 | |
| 68 | bool Nl80211Socket::AddGroupMembership(const string &group_name) { |
Christopher Wiley | a6032b2 | 2012-11-07 21:34:46 -0800 | [diff] [blame] | 69 | int id = genl_ctrl_resolve_grp(GetNlSock(), |
| 70 | kSocketFamilyName, |
| 71 | group_name.c_str()); |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 72 | if (id < 0) { |
| 73 | LOG(ERROR) << "No Id for group " << group_name; |
| 74 | return false; |
Christopher Wiley | a6032b2 | 2012-11-07 21:34:46 -0800 | [diff] [blame] | 75 | } |
| 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 Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 80 | } |
Wade Guthrie | d615361 | 2012-08-23 11:36:14 -0700 | [diff] [blame] | 81 | LOG(INFO) << " Group " << group_name << " added successfully"; |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 82 | return true; |
| 83 | } |
| 84 | |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 85 | } // namespace shill |