2 * netsniff-ng - the packet sniffing beast
3 * Copyright 2012 Daniel Borkmann.
4 * Subject to the GPL, version 2.
5 * Parts derived from iw, subject to ISC license.
6 * Copyright 2007, 2008 Johannes Berg
7 * Copyright 2007 Andy Lutomirski
8 * Copyright 2007 Mike Kershaw
9 * Copyright 2008-2009 Luis R. Rodriguez
18 #include <sys/socket.h>
20 #include <linux/nl80211.h>
21 #include <netlink/genl/genl.h>
22 #include <netlink/genl/family.h>
23 #include <netlink/genl/ctrl.h>
24 #include <netlink/msg.h>
25 #include <netlink/attr.h>
34 struct nl80211_state
{
35 struct nl_sock
*nl_sock
;
36 struct nl_cache
*nl_cache
;
37 struct genl_family
*nl80211
;
40 static void get_mac80211_phydev(const char *device
, char *phydev_path
,
47 ret
= asprintf(&pathstr
, "/sys/class/net/%s/phy80211", device
);
49 panic("Can't generate path name string for /sys/class/net device");
51 num
= readlink(pathstr
, phydev_path
, phydev_len
);
53 if (errno
== ENOENT
|| errno
== EINVAL
)
54 panic("It's probably not a mac80211 device!\n");
55 panic("Can't readlink %s: %s!\n", pathstr
, strerror(errno
));
59 phydev_path
[min_t(size_t, num
, phydev_len
- 1)] = 0;
62 static inline struct nl_msg
*nl80211_nlmsg_xalloc(void)
64 struct nl_msg
*ret
= nlmsg_alloc();
66 panic("Cannot allocate nlmsg memory!\n");
70 static inline struct nl_sock
*nl80211_nl_socket_xalloc(void)
72 struct nl_sock
*ret
= nl_socket_alloc();
74 panic("Cannot allocate nl socket memory!\n");
78 static void nl80211_init(struct nl80211_state
*state
)
82 state
->nl_sock
= nl80211_nl_socket_xalloc();
84 ret
= genl_connect(state
->nl_sock
);
86 panic("Cannot connect generic netlink!\n");
88 ret
= genl_ctrl_alloc_cache(state
->nl_sock
, &state
->nl_cache
);
90 panic("Failed to allocate generic netlink cache: %s!",
93 state
->nl80211
= genl_ctrl_search_by_name(state
->nl_cache
, "nl80211");
95 panic("nl80211 not found in netlink cache!\n");
98 static void nl80211_cleanup(struct nl80211_state
*state
)
100 genl_family_put(state
->nl80211
);
102 nl_cache_free(state
->nl_cache
);
103 nl_socket_free(state
->nl_sock
);
106 static int nl80211_wait_handler(struct nl_msg
*msg __maybe_unused
, void *arg
)
115 static int nl80211_error_handler(struct sockaddr_nl
*nla __maybe_unused
,
116 struct nlmsgerr
*err
,
117 void *arg __maybe_unused
)
119 panic("nl80211 returned with error (%d): %s\n", err
->error
,
120 nl_geterror(err
->error
));
123 static int nl80211_add_mon_if(struct nl80211_state
*state
, const char *device
,
124 const char *mondevice
)
128 struct nl_cb
*cb
= NULL
;
131 ifindex
= device_ifindex(device
);
133 msg
= nl80211_nlmsg_xalloc();
135 genlmsg_put(msg
, 0, 0, genl_family_get_id(state
->nl80211
), 0,
136 0, NL80211_CMD_NEW_INTERFACE
, 0);
138 NLA_PUT_U32(msg
, NL80211_ATTR_IFINDEX
, ifindex
);
139 NLA_PUT_STRING(msg
, NL80211_ATTR_IFNAME
, mondevice
);
140 NLA_PUT_U32(msg
, NL80211_ATTR_IFTYPE
, NL80211_IFTYPE_MONITOR
);
142 ret
= nl_send_auto_complete(state
->nl_sock
, msg
);
144 if (ret
== -ENFILE
) {
149 panic("Cannot send_auto_complete!\n");
152 cb
= nl_cb_alloc(NL_CB_CUSTOM
);
154 panic("Cannot alloc nl_cb!\n");
156 nl_cb_set(cb
, NL_CB_ACK
, NL_CB_CUSTOM
, nl80211_wait_handler
, &finished
);
157 nl_cb_err(cb
, NL_CB_CUSTOM
, nl80211_error_handler
, NULL
);
159 nl_recvmsgs(state
->nl_sock
, cb
);
162 ret
= nl_wait_for_ack(state
->nl_sock
);
164 if (ret
== -ENFILE
) {
169 panic("Waiting for netlink ack failed!\n");
178 panic("nla put failure!\n");
179 return -EIO
; /* dummy */
182 static int nl80211_del_mon_if(struct nl80211_state
*state
,
183 const char *mondevice
)
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
);
199 panic("Cannot send_auto_complete!\n");
201 ret
= nl_wait_for_ack(state
->nl_sock
);
203 panic("Waiting for netlink ack failed!\n");
209 panic("nla put failure!\n");
210 return -EIO
; /* dummy */
213 void enter_rfmon_mac80211(const char *device
, char **mondev
)
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
);
225 for (n
= 0; n
< UINT_MAX
; n
++) {
228 slprintf(mondevice
, sizeof(mondevice
), "mon%u", n
);
230 if (device_ifindex_get(mondevice
) > 0)
233 ret
= nl80211_add_mon_if(&nlstate
, device
, mondevice
);
235 *mondev
= xstrdup(mondevice
);
237 flags
= device_get_flags(*mondev
);
238 flags
|= IFF_UP
| IFF_RUNNING
;
239 device_set_flags(*mondev
, flags
);
241 nl80211_cleanup(&nlstate
);
246 panic("No free monN interfaces!\n");
249 void leave_rfmon_mac80211(const char *mondev
)
252 struct nl80211_state nlstate
;
254 flags
= device_get_flags(mondev
);
255 flags
&= ~(IFF_UP
| IFF_RUNNING
);
256 device_set_flags(mondev
, flags
);
258 nl80211_init(&nlstate
);
259 nl80211_del_mon_if(&nlstate
, mondev
);
260 nl80211_cleanup(&nlstate
);