doc: some improvements in astraceroute manpage
[netsniff-ng.git] / src / mac80211.c
blobf6b52c2367b6dd64a2d9e9ce349151dc310c0f58
1 /*
2 * netsniff-ng - the packet sniffing beast
3 * By Daniel Borkmann <daniel@netsniff-ng.org>
4 * Copyright 2012 Daniel Borkmann.
5 * Subject to the GPL, version 2.
6 * Parts derived from iw, subject to ISC license.
7 * Copyright 2007, 2008 Johannes Berg
8 * Copyright 2007 Andy Lutomirski
9 * Copyright 2007 Mike Kershaw
10 * Copyright 2008-2009 Luis R. Rodriguez
13 #define _GNU_SOURCE
14 #include <stdio.h>
15 #include <stdlib.h>
16 #include <errno.h>
17 #include <string.h>
18 #include <limits.h>
19 #include <linux/nl80211.h>
20 #include <netlink/genl/genl.h>
21 #include <netlink/genl/family.h>
22 #include <netlink/genl/ctrl.h>
23 #include <netlink/msg.h>
24 #include <netlink/attr.h>
26 #include "die.h"
27 #include "xutils.h"
28 #include "mac80211.h"
29 #include "xmalloc.h"
30 #include "built_in.h"
32 #ifdef HAVE_LIBNL_2_x
33 # define LIBNL_FAILURE NLE_FAILURE
34 # define get_nl_errmsg nl_geterror
35 #else
36 # define LIBNL_FAILURE ENFILE
37 /* libnl 2.x compatibility code */
38 # define nl_sock nl_handle
40 static inline struct nl_handle *nl_socket_alloc(void)
42 return nl_handle_alloc();
45 static inline void nl_socket_free(struct nl_handle *h)
47 nl_handle_destroy(h);
50 # define get_nl_errmsg strerror
52 static inline int __genl_ctrl_alloc_cache(struct nl_handle *h,
53 struct nl_cache **cache)
55 struct nl_cache *tmp = genl_ctrl_alloc_cache(h);
56 if (!tmp)
57 return -ENOMEM;
58 *cache = tmp;
59 return 0;
62 # define genl_ctrl_alloc_cache __genl_ctrl_alloc_cache
63 #endif /* !HAVE_LIBNL_2_x */
65 struct nl80211_state {
66 struct nl_sock *nl_sock;
67 struct nl_cache *nl_cache;
68 struct genl_family *nl80211;
71 static void get_mac80211_phydev(const char *device, char *phydev_path,
72 size_t phydev_len)
74 int ret;
75 char *pathstr;
76 ssize_t num;
78 ret = asprintf(&pathstr, "/sys/class/net/%s/phy80211", device);
79 if (ret < 0)
80 panic("Can't generate path name string for /sys/class/net device");
82 num = readlink(pathstr, phydev_path, phydev_len);
83 if (num < 0) {
84 if (errno == ENOENT || errno == EINVAL)
85 panic("It's probably not a mac80211 device!\n");
86 panic("Can't readlink %s: %s!\n", pathstr, strerror(errno));
89 xfree(pathstr);
90 phydev_path[min(num, phydev_len - 1)] = 0;
93 static inline struct nl_msg *nl80211_nlmsg_xalloc(void)
95 struct nl_msg *ret = nlmsg_alloc();
96 if (!ret)
97 panic("Cannot allocate nlmsg memory!\n");
98 return ret;
101 static inline struct nl_handle *nl80211_nl_socket_xalloc(void)
103 struct nl_handle *ret = nl_socket_alloc();
104 if (!ret)
105 panic("Cannot allocate nl socket memory!\n");
106 return ret;
109 static void nl80211_init(struct nl80211_state *state, const char *device)
111 int ret;
113 state->nl_sock = nl80211_nl_socket_xalloc();
115 ret = genl_connect(state->nl_sock);
116 if (ret)
117 panic("Cannot connect generic netlink!\n");
119 ret = genl_ctrl_alloc_cache(state->nl_sock, &state->nl_cache);
120 if (ret < 0)
121 panic("Failed to allocate generic netlink cache: %s!",
122 get_nl_errmsg(-ret));
124 state->nl80211 = genl_ctrl_search_by_name(state->nl_cache, "nl80211");
125 if (!state->nl80211)
126 panic("nl80211 not found in netlink cache!\n");
129 static void nl80211_cleanup(struct nl80211_state *state)
131 genl_family_put(state->nl80211);
133 nl_cache_free(state->nl_cache);
134 nl_socket_free(state->nl_sock);
137 static int nl80211_add_mon_if(struct nl80211_state *state, const char *device,
138 const char *mondevice)
140 int ifindex, ret;
141 struct nl_msg *msg;
143 ifindex = device_ifindex(device);
145 msg = nl80211_nlmsg_xalloc();
147 genlmsg_put(msg, 0, 0, genl_family_get_id(state->nl80211), 0,
148 0, NL80211_CMD_NEW_INTERFACE, 0);
150 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifindex);
151 NLA_PUT_STRING(msg, NL80211_ATTR_IFNAME, mondevice);
152 NLA_PUT_U32(msg, NL80211_ATTR_IFTYPE, NL80211_IFTYPE_MONITOR);
154 ret = nl_send_auto_complete(state->nl_sock, msg);
155 if (ret < 0) {
156 if (ret == -LIBNL_FAILURE) {
157 nlmsg_free(msg);
158 return -EBUSY;
161 panic("Cannot send_auto_complete!\n");
164 ret = nl_wait_for_ack(state->nl_sock);
165 if (ret < 0) {
166 if (ret == -LIBNL_FAILURE) {
167 nlmsg_free(msg);
168 return -EBUSY;
171 panic("Waiting for netlink ack failed!\n");
174 nlmsg_free(msg);
175 return 0;
177 nla_put_failure:
178 panic("nla put failure!\n");
179 return -EIO; /* dummy */
182 static int nl80211_del_mon_if(struct nl80211_state *state, const char *device,
183 const char *mondevice)
185 int ifindex, ret;
186 struct nl_msg *msg;
188 ifindex = device_ifindex(mondevice);
190 msg = nl80211_nlmsg_xalloc();
192 genlmsg_put(msg, 0, 0, genl_family_get_id(state->nl80211), 0,
193 0, NL80211_CMD_DEL_INTERFACE, 0);
195 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifindex);
197 ret = nl_send_auto_complete(state->nl_sock, msg);
198 if (ret < 0)
199 panic("Cannot send_auto_complete!\n");
201 ret = nl_wait_for_ack(state->nl_sock);
202 if (ret < 0)
203 panic("Waiting for netlink ack failed!\n");
205 nlmsg_free(msg);
206 return 0;
208 nla_put_failure:
209 panic("nla put failure!\n");
210 return -EIO; /* dummy */
213 void enter_rfmon_mac80211(const char *device, char **mondev)
215 int ret;
216 short flags;
217 uint32_t n;
218 char phydev_path[256];
219 struct nl80211_state nlstate;
221 /* XXX: is this already a monN device? */
222 get_mac80211_phydev(device, phydev_path, sizeof(phydev_path));
223 nl80211_init(&nlstate, device);
225 for (n = 0; n < UINT_MAX; n++) {
226 char mondevice[32];
228 slprintf(mondevice, sizeof(mondevice), "mon%u", n);
229 ret = nl80211_add_mon_if(&nlstate, device, mondevice);
230 if (ret == 0) {
231 *mondev = xstrdup(mondevice);
233 flags = device_get_flags(*mondev);
234 flags |= IFF_UP | IFF_RUNNING;
235 device_set_flags(*mondev, flags);
237 nl80211_cleanup(&nlstate);
238 return;
242 panic("No free monN interfaces!\n");
245 void leave_rfmon_mac80211(const char *device, const char *mondev)
247 short flags;
248 struct nl80211_state nlstate;
250 flags = device_get_flags(mondev);
251 flags &= ~(IFF_UP | IFF_RUNNING);
252 device_set_flags(mondev, flags);
254 nl80211_init(&nlstate, device);
255 nl80211_del_mon_if(&nlstate, device, mondev);
256 nl80211_cleanup(&nlstate);