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 | #ifndef SHILL_NETLINK_SOCKET_H_ |
| 26 | #define SHILL_NETLINK_SOCKET_H_ |
| 27 | |
| 28 | #include <iomanip> |
| 29 | #include <string> |
| 30 | |
| 31 | #include <asm/types.h> |
| 32 | #include <linux/nl80211.h> |
| 33 | #include <netlink/handlers.h> |
| 34 | #include <netlink/netlink.h> |
| 35 | |
| 36 | #include <base/basictypes.h> |
| 37 | #include <base/bind.h> |
| 38 | #include <base/logging.h> |
| 39 | |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 40 | enum nl_cb_kind; |
| 41 | enum nl_cb_type; |
| 42 | struct nl_msg; |
| 43 | |
| 44 | namespace shill { |
| 45 | |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 46 | // Provides an abstraction to a netlink socket. See |
| 47 | // http://www.infradead.org/~tgr/libnl/ for documentation on how netlink |
| 48 | // sockets work. |
| 49 | class NetlinkSocket { |
| 50 | public: |
| 51 | // Provides a wrapper around the netlink callback. |
| 52 | class Callback { |
| 53 | public: |
| 54 | Callback() : cb_(NULL) {} |
| 55 | virtual ~Callback(); |
| 56 | |
| 57 | // Non-trivial initialization. |
| 58 | bool Init(); |
| 59 | |
| 60 | // Very thin abstraction of nl_cb_err. Takes the same parameters used by |
| 61 | // 'nl_cb_err' except for the first parameter of 'nl_cb_err' (which is |
| 62 | // filled in using the member variable |cb_|). |
| 63 | bool ErrHandler(nl_cb_kind kind, nl_recvmsg_err_cb_t func, void *arg); |
| 64 | |
| 65 | // Very thin abstraction of nl_cb_set. Takes the same parameters used by |
| 66 | // 'nl_cb_set' except for the first parameter of 'nl_cb_set' (which is |
| 67 | // filled in using the member variable |cb_|). |
| 68 | bool SetHandler(nl_cb_type type, nl_cb_kind kind, nl_recvmsg_msg_cb_t func, |
| 69 | void *arg); |
| 70 | |
| 71 | private: |
| 72 | friend class NetlinkSocket; // Because GetMessagesUsingCallback needs cb(). |
| 73 | |
| 74 | const struct nl_cb *cb() const { return cb_; } |
| 75 | |
| 76 | struct nl_cb *cb_; |
| 77 | |
| 78 | DISALLOW_COPY_AND_ASSIGN(Callback); |
| 79 | }; |
| 80 | |
| 81 | NetlinkSocket() : nl_sock_(NULL) {} |
| 82 | virtual ~NetlinkSocket(); |
| 83 | |
| 84 | // Non-trivial initialization. |
| 85 | bool Init(); |
| 86 | |
| 87 | // Disables sequence checking on the message stream. |
| 88 | virtual bool DisableSequenceChecking(); |
| 89 | |
| 90 | // Returns the file descriptor used by the socket. |
| 91 | virtual int GetFd() const; |
| 92 | |
| 93 | // Receives one or more messages (perhaps a response to a previously sent |
| 94 | // message) over the netlink socket. The message(s) are handled with the |
| 95 | // default callback (configured with 'SetNetlinkCallback'). |
| 96 | virtual bool GetMessages(); |
| 97 | |
| 98 | // Receives one or more messages over the netlink socket. The message(s) |
| 99 | // are handled with the supplied callback (uses socket's default callback |
| 100 | // function if NULL). |
| 101 | virtual bool GetMessagesUsingCallback(NetlinkSocket::Callback *callback); |
| 102 | |
Wade Guthrie | 5d3d6de | 2012-11-02 11:08:34 -0700 | [diff] [blame] | 103 | // Get the next message sequence number for this socket. Disallow zero so |
| 104 | // that we can use that as the 'broadcast' sequence number. |
| 105 | virtual unsigned int GetSequenceNumber(); |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 106 | |
| 107 | // This method is called |callback_function| to differentiate it from the |
| 108 | // 'callback' method in KernelBoundNlMessage since they return different |
| 109 | // types. |
| 110 | virtual bool SetNetlinkCallback(nl_recvmsg_msg_cb_t on_netlink_data, |
| 111 | void *callback_parameter); |
| 112 | |
| 113 | // Returns the family name of the socket created by this type of object. |
| 114 | virtual std::string GetSocketFamilyName() const = 0; |
| 115 | |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 116 | protected: |
Christopher Wiley | 393b93f | 2012-11-08 17:30:58 -0800 | [diff] [blame] | 117 | // Send a message, returns 0 on error, or the sequence number (> 0). |
| 118 | uint32 Send(struct nl_msg *message, uint8 command, int32 family_id); |
| 119 | |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 120 | struct nl_sock *GetNlSock() { return nl_sock_; } |
| 121 | |
| 122 | private: |
| 123 | // Netlink Callback function for nl80211. Used to disable the sequence |
| 124 | // checking on messages received from the netlink module. |
| 125 | static int IgnoreSequenceCheck(nl_msg *ignored_msg, void *ignored_arg); |
| 126 | |
| 127 | struct nl_sock *nl_sock_; |
| 128 | |
| 129 | DISALLOW_COPY_AND_ASSIGN(NetlinkSocket); |
| 130 | }; |
| 131 | |
| 132 | } // namespace shill |
| 133 | |
| 134 | #endif // SHILL_NETLINK_SOCKET_H_ |