blob: 0db5c3ae92823aa9e6fd96612fff8ebcd73af8f0 [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#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
40#include "shill/kernel_bound_nlmessage.h"
41
42enum nl_cb_kind;
43enum nl_cb_type;
44struct nl_msg;
45
46namespace shill {
47
48// libnl 1.x compatibility code -- these functions provide libnl v2.x/v3.x
49// interfaces for systems that only have v1.x libraries.
50#if !defined(CONFIG_LIBNL20) && !defined(CONFIG_LIBNL30)
51#define nl_sock nl_handle
52static inline struct nl_handle *nl_socket_alloc(void) {
53 return nl_handle_alloc();
54}
55
56static inline void nl_socket_free(struct nl_sock *h) {
57 nl_handle_destroy(h);
58}
59#endif /* CONFIG_LIBNL20 && CONFIG_LIBNL30 */
60
61
62// Provides an abstraction to a netlink socket. See
63// http://www.infradead.org/~tgr/libnl/ for documentation on how netlink
64// sockets work.
65class NetlinkSocket {
66 public:
67 // Provides a wrapper around the netlink callback.
68 class Callback {
69 public:
70 Callback() : cb_(NULL) {}
71 virtual ~Callback();
72
73 // Non-trivial initialization.
74 bool Init();
75
76 // Very thin abstraction of nl_cb_err. Takes the same parameters used by
77 // 'nl_cb_err' except for the first parameter of 'nl_cb_err' (which is
78 // filled in using the member variable |cb_|).
79 bool ErrHandler(nl_cb_kind kind, nl_recvmsg_err_cb_t func, void *arg);
80
81 // Very thin abstraction of nl_cb_set. Takes the same parameters used by
82 // 'nl_cb_set' except for the first parameter of 'nl_cb_set' (which is
83 // filled in using the member variable |cb_|).
84 bool SetHandler(nl_cb_type type, nl_cb_kind kind, nl_recvmsg_msg_cb_t func,
85 void *arg);
86
87 private:
88 friend class NetlinkSocket; // Because GetMessagesUsingCallback needs cb().
89
90 const struct nl_cb *cb() const { return cb_; }
91
92 struct nl_cb *cb_;
93
94 DISALLOW_COPY_AND_ASSIGN(Callback);
95 };
96
97 NetlinkSocket() : nl_sock_(NULL) {}
98 virtual ~NetlinkSocket();
99
100 // Non-trivial initialization.
101 bool Init();
102
103 // Disables sequence checking on the message stream.
104 virtual bool DisableSequenceChecking();
105
106 // Returns the file descriptor used by the socket.
107 virtual int GetFd() const;
108
109 // Receives one or more messages (perhaps a response to a previously sent
110 // message) over the netlink socket. The message(s) are handled with the
111 // default callback (configured with 'SetNetlinkCallback').
112 virtual bool GetMessages();
113
114 // Receives one or more messages over the netlink socket. The message(s)
115 // are handled with the supplied callback (uses socket's default callback
116 // function if NULL).
117 virtual bool GetMessagesUsingCallback(NetlinkSocket::Callback *callback);
118
119 virtual unsigned int GetSequenceNumber() {
120 return nl_socket_use_seq(nl_sock_);
121 }
122
123 // This method is called |callback_function| to differentiate it from the
124 // 'callback' method in KernelBoundNlMessage since they return different
125 // types.
126 virtual bool SetNetlinkCallback(nl_recvmsg_msg_cb_t on_netlink_data,
127 void *callback_parameter);
128
129 // Returns the family name of the socket created by this type of object.
130 virtual std::string GetSocketFamilyName() const = 0;
131
132 const struct nl_sock *GetConstNlSock() const { return nl_sock_; }
133
134 protected:
135 struct nl_sock *GetNlSock() { return nl_sock_; }
136
137 private:
138 // Netlink Callback function for nl80211. Used to disable the sequence
139 // checking on messages received from the netlink module.
140 static int IgnoreSequenceCheck(nl_msg *ignored_msg, void *ignored_arg);
141
142 struct nl_sock *nl_sock_;
143
144 DISALLOW_COPY_AND_ASSIGN(NetlinkSocket);
145};
146
147} // namespace shill
148
149#endif // SHILL_NETLINK_SOCKET_H_