pcap-netfilter-linux.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759
  1. /*
  2. * Copyright (c) 2011 Jakub Zawadzki
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. *
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * 3. The name of the author may not be used to endorse or promote
  15. * products derived from this software without specific prior written
  16. * permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  19. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  20. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  21. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  22. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  23. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  24. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  25. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  26. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  28. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. */
  30. #ifdef HAVE_CONFIG_H
  31. #include <config.h>
  32. #endif
  33. #include "pcap-int.h"
  34. #ifdef NEED_STRERROR_H
  35. #include "strerror.h"
  36. #endif
  37. #include <errno.h>
  38. #include <stdlib.h>
  39. #include <unistd.h>
  40. #include <string.h>
  41. #include <sys/socket.h>
  42. #include <arpa/inet.h>
  43. #include <time.h>
  44. #include <sys/time.h>
  45. #include <netinet/in.h>
  46. #include <linux/types.h>
  47. #include <linux/netlink.h>
  48. #include <linux/netfilter.h>
  49. #include <linux/netfilter/nfnetlink.h>
  50. #include <linux/netfilter/nfnetlink_log.h>
  51. #include <linux/netfilter/nfnetlink_queue.h>
  52. /* NOTE: if your program drops privilages after pcap_activate() it WON'T work with nfqueue.
  53. * It took me quite some time to debug ;/
  54. *
  55. * Sending any data to nfnetlink socket requires CAP_NET_ADMIN privilages,
  56. * and in nfqueue we need to send verdict reply after recving packet.
  57. *
  58. * In tcpdump you can disable dropping privilages with -Z root
  59. */
  60. #include "pcap-netfilter-linux.h"
  61. #define HDR_LENGTH (NLMSG_LENGTH(NLMSG_ALIGN(sizeof(struct nfgenmsg))))
  62. #define NFLOG_IFACE "nflog"
  63. #define NFQUEUE_IFACE "nfqueue"
  64. typedef enum { OTHER = -1, NFLOG, NFQUEUE } nftype_t;
  65. /*
  66. * Private data for capturing on Linux netfilter sockets.
  67. */
  68. struct pcap_netfilter {
  69. u_int packets_read; /* count of packets read with recvfrom() */
  70. u_int packets_nobufs; /* ENOBUFS counter */
  71. };
  72. static int nfqueue_send_verdict(const pcap_t *handle, uint16_t group_id, u_int32_t id, u_int32_t verdict);
  73. static int
  74. netfilter_read_linux(pcap_t *handle, int max_packets, pcap_handler callback, u_char *user)
  75. {
  76. struct pcap_netfilter *handlep = handle->priv;
  77. register u_char *bp, *ep;
  78. int count = 0;
  79. int len;
  80. /*
  81. * Has "pcap_breakloop()" been called?
  82. */
  83. if (handle->break_loop) {
  84. /*
  85. * Yes - clear the flag that indicates that it
  86. * has, and return PCAP_ERROR_BREAK to indicate
  87. * that we were told to break out of the loop.
  88. */
  89. handle->break_loop = 0;
  90. return PCAP_ERROR_BREAK;
  91. }
  92. len = handle->cc;
  93. if (len == 0) {
  94. /*
  95. * The buffer is empty; refill it.
  96. *
  97. * We ignore EINTR, as that might just be due to a signal
  98. * being delivered - if the signal should interrupt the
  99. * loop, the signal handler should call pcap_breakloop()
  100. * to set handle->break_loop (we ignore it on other
  101. * platforms as well).
  102. */
  103. do {
  104. len = recv(handle->fd, handle->buffer, handle->bufsize, 0);
  105. if (handle->break_loop) {
  106. handle->break_loop = 0;
  107. return PCAP_ERROR_BREAK;
  108. }
  109. if (errno == ENOBUFS)
  110. handlep->packets_nobufs++;
  111. } while ((len == -1) && (errno == EINTR || errno == ENOBUFS));
  112. if (len < 0) {
  113. pcap_fmt_errmsg_for_errno(handle->errbuf,
  114. PCAP_ERRBUF_SIZE, errno, "Can't receive packet");
  115. return PCAP_ERROR;
  116. }
  117. bp = (unsigned char *)handle->buffer;
  118. } else
  119. bp = handle->bp;
  120. ep = bp + len;
  121. while (bp < ep) {
  122. const struct nlmsghdr *nlh = (const struct nlmsghdr *) bp;
  123. uint32_t msg_len;
  124. nftype_t type = OTHER;
  125. /*
  126. * Has "pcap_breakloop()" been called?
  127. * If so, return immediately - if we haven't read any
  128. * packets, clear the flag and return PCAP_ERROR_BREAK
  129. * to indicate that we were told to break out of the loop,
  130. * otherwise leave the flag set, so that the *next* call
  131. * will break out of the loop without having read any
  132. * packets, and return the number of packets we've
  133. * processed so far.
  134. */
  135. if (handle->break_loop) {
  136. handle->bp = bp;
  137. handle->cc = ep - bp;
  138. if (count == 0) {
  139. handle->break_loop = 0;
  140. return PCAP_ERROR_BREAK;
  141. } else
  142. return count;
  143. }
  144. if (ep - bp < NLMSG_SPACE(0)) {
  145. /*
  146. * There's less than one netlink message left
  147. * in the buffer. Give up.
  148. */
  149. break;
  150. }
  151. if (nlh->nlmsg_len < sizeof(struct nlmsghdr) || (u_int)len < nlh->nlmsg_len) {
  152. pcap_snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, "Message truncated: (got: %d) (nlmsg_len: %u)", len, nlh->nlmsg_len);
  153. return -1;
  154. }
  155. if (NFNL_SUBSYS_ID(nlh->nlmsg_type) == NFNL_SUBSYS_ULOG &&
  156. NFNL_MSG_TYPE(nlh->nlmsg_type) == NFULNL_MSG_PACKET)
  157. type = NFLOG;
  158. else if (NFNL_SUBSYS_ID(nlh->nlmsg_type) == NFNL_SUBSYS_QUEUE &&
  159. NFNL_MSG_TYPE(nlh->nlmsg_type) == NFQNL_MSG_PACKET)
  160. type = NFQUEUE;
  161. if (type != OTHER) {
  162. const unsigned char *payload = NULL;
  163. struct pcap_pkthdr pkth;
  164. const struct nfgenmsg *nfg = NULL;
  165. int id = 0;
  166. if (handle->linktype != DLT_NFLOG) {
  167. const struct nfattr *payload_attr = NULL;
  168. if (nlh->nlmsg_len < HDR_LENGTH) {
  169. pcap_snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, "Malformed message: (nlmsg_len: %u)", nlh->nlmsg_len);
  170. return -1;
  171. }
  172. nfg = NLMSG_DATA(nlh);
  173. if (nlh->nlmsg_len > HDR_LENGTH) {
  174. struct nfattr *attr = NFM_NFA(nfg);
  175. int attr_len = nlh->nlmsg_len - NLMSG_ALIGN(HDR_LENGTH);
  176. while (NFA_OK(attr, attr_len)) {
  177. if (type == NFQUEUE) {
  178. switch (NFA_TYPE(attr)) {
  179. case NFQA_PACKET_HDR:
  180. {
  181. const struct nfqnl_msg_packet_hdr *pkt_hdr = (const struct nfqnl_msg_packet_hdr *) NFA_DATA(attr);
  182. id = ntohl(pkt_hdr->packet_id);
  183. break;
  184. }
  185. case NFQA_PAYLOAD:
  186. payload_attr = attr;
  187. break;
  188. }
  189. } else if (type == NFLOG) {
  190. switch (NFA_TYPE(attr)) {
  191. case NFULA_PAYLOAD:
  192. payload_attr = attr;
  193. break;
  194. }
  195. }
  196. attr = NFA_NEXT(attr, attr_len);
  197. }
  198. }
  199. if (payload_attr) {
  200. payload = NFA_DATA(payload_attr);
  201. pkth.len = pkth.caplen = NFA_PAYLOAD(payload_attr);
  202. }
  203. } else {
  204. payload = NLMSG_DATA(nlh);
  205. pkth.caplen = pkth.len = nlh->nlmsg_len-NLMSG_ALIGN(sizeof(struct nlmsghdr));
  206. }
  207. if (payload) {
  208. /* pkth.caplen = min (payload_len, handle->snapshot); */
  209. gettimeofday(&pkth.ts, NULL);
  210. if (handle->fcode.bf_insns == NULL ||
  211. bpf_filter(handle->fcode.bf_insns, payload, pkth.len, pkth.caplen))
  212. {
  213. handlep->packets_read++;
  214. callback(user, &pkth, payload);
  215. count++;
  216. }
  217. }
  218. if (type == NFQUEUE) {
  219. /* XXX, possible responses: NF_DROP, NF_ACCEPT, NF_STOLEN, NF_QUEUE, NF_REPEAT, NF_STOP */
  220. /* if type == NFQUEUE, handle->linktype is always != DLT_NFLOG,
  221. so nfg is always initialized to NLMSG_DATA(nlh). */
  222. if (nfg != NULL)
  223. nfqueue_send_verdict(handle, ntohs(nfg->res_id), id, NF_ACCEPT);
  224. }
  225. }
  226. msg_len = NLMSG_ALIGN(nlh->nlmsg_len);
  227. /*
  228. * If the message length would run past the end of the
  229. * buffer, truncate it to the remaining space in the
  230. * buffer.
  231. */
  232. if (msg_len > ep - bp)
  233. msg_len = ep - bp;
  234. bp += msg_len;
  235. if (count >= max_packets && !PACKET_COUNT_IS_UNLIMITED(max_packets)) {
  236. handle->bp = bp;
  237. handle->cc = ep - bp;
  238. if (handle->cc < 0)
  239. handle->cc = 0;
  240. return count;
  241. }
  242. }
  243. handle->cc = 0;
  244. return count;
  245. }
  246. static int
  247. netfilter_set_datalink(pcap_t *handle, int dlt)
  248. {
  249. handle->linktype = dlt;
  250. return 0;
  251. }
  252. static int
  253. netfilter_stats_linux(pcap_t *handle, struct pcap_stat *stats)
  254. {
  255. struct pcap_netfilter *handlep = handle->priv;
  256. stats->ps_recv = handlep->packets_read;
  257. stats->ps_drop = handlep->packets_nobufs;
  258. stats->ps_ifdrop = 0;
  259. return 0;
  260. }
  261. static int
  262. netfilter_inject_linux(pcap_t *handle, const void *buf _U_, size_t size _U_)
  263. {
  264. pcap_snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, "inject not supported on netfilter devices");
  265. return (-1);
  266. }
  267. struct my_nfattr {
  268. uint16_t nfa_len;
  269. uint16_t nfa_type;
  270. void *data;
  271. };
  272. static int
  273. netfilter_send_config_msg(const pcap_t *handle, uint16_t msg_type, int ack, u_int8_t family, u_int16_t res_id, const struct my_nfattr *mynfa)
  274. {
  275. char buf[1024] __attribute__ ((aligned));
  276. struct nlmsghdr *nlh = (struct nlmsghdr *) buf;
  277. struct nfgenmsg *nfg = (struct nfgenmsg *) (buf + sizeof(struct nlmsghdr));
  278. struct sockaddr_nl snl;
  279. static unsigned int seq_id;
  280. if (!seq_id)
  281. seq_id = time(NULL);
  282. ++seq_id;
  283. nlh->nlmsg_len = NLMSG_LENGTH(sizeof(struct nfgenmsg));
  284. nlh->nlmsg_type = msg_type;
  285. nlh->nlmsg_flags = NLM_F_REQUEST | (ack ? NLM_F_ACK : 0);
  286. nlh->nlmsg_pid = 0; /* to kernel */
  287. nlh->nlmsg_seq = seq_id;
  288. nfg->nfgen_family = family;
  289. nfg->version = NFNETLINK_V0;
  290. nfg->res_id = htons(res_id);
  291. if (mynfa) {
  292. struct nfattr *nfa = (struct nfattr *) (buf + NLMSG_ALIGN(nlh->nlmsg_len));
  293. nfa->nfa_type = mynfa->nfa_type;
  294. nfa->nfa_len = NFA_LENGTH(mynfa->nfa_len);
  295. memcpy(NFA_DATA(nfa), mynfa->data, mynfa->nfa_len);
  296. nlh->nlmsg_len = NLMSG_ALIGN(nlh->nlmsg_len) + NFA_ALIGN(nfa->nfa_len);
  297. }
  298. memset(&snl, 0, sizeof(snl));
  299. snl.nl_family = AF_NETLINK;
  300. if (sendto(handle->fd, nlh, nlh->nlmsg_len, 0, (struct sockaddr *) &snl, sizeof(snl)) == -1)
  301. return -1;
  302. if (!ack)
  303. return 0;
  304. /* waiting for reply loop */
  305. do {
  306. socklen_t addrlen = sizeof(snl);
  307. int len;
  308. /* ignore interrupt system call error */
  309. do {
  310. len = recvfrom(handle->fd, buf, sizeof(buf), 0, (struct sockaddr *) &snl, &addrlen);
  311. } while ((len == -1) && (errno == EINTR));
  312. if (len <= 0)
  313. return len;
  314. if (addrlen != sizeof(snl) || snl.nl_family != AF_NETLINK) {
  315. errno = EINVAL;
  316. return -1;
  317. }
  318. nlh = (struct nlmsghdr *) buf;
  319. if (snl.nl_pid != 0 || seq_id != nlh->nlmsg_seq) /* if not from kernel or wrong sequence skip */
  320. continue;
  321. while ((u_int)len >= NLMSG_SPACE(0) && NLMSG_OK(nlh, (u_int)len)) {
  322. if (nlh->nlmsg_type == NLMSG_ERROR || (nlh->nlmsg_type == NLMSG_DONE && nlh->nlmsg_flags & NLM_F_MULTI)) {
  323. if (nlh->nlmsg_len < NLMSG_ALIGN(sizeof(struct nlmsgerr))) {
  324. errno = EBADMSG;
  325. return -1;
  326. }
  327. errno = -(*((int *)NLMSG_DATA(nlh)));
  328. return (errno == 0) ? 0 : -1;
  329. }
  330. nlh = NLMSG_NEXT(nlh, len);
  331. }
  332. } while (1);
  333. return -1; /* never here */
  334. }
  335. static int
  336. nflog_send_config_msg(const pcap_t *handle, uint8_t family, u_int16_t group_id, const struct my_nfattr *mynfa)
  337. {
  338. return netfilter_send_config_msg(handle, (NFNL_SUBSYS_ULOG << 8) | NFULNL_MSG_CONFIG, 1, family, group_id, mynfa);
  339. }
  340. static int
  341. nflog_send_config_cmd(const pcap_t *handle, uint16_t group_id, u_int8_t cmd, u_int8_t family)
  342. {
  343. struct nfulnl_msg_config_cmd msg;
  344. struct my_nfattr nfa;
  345. msg.command = cmd;
  346. nfa.data = &msg;
  347. nfa.nfa_type = NFULA_CFG_CMD;
  348. nfa.nfa_len = sizeof(msg);
  349. return nflog_send_config_msg(handle, family, group_id, &nfa);
  350. }
  351. static int
  352. nflog_send_config_mode(const pcap_t *handle, uint16_t group_id, u_int8_t copy_mode, u_int32_t copy_range)
  353. {
  354. struct nfulnl_msg_config_mode msg;
  355. struct my_nfattr nfa;
  356. msg.copy_range = htonl(copy_range);
  357. msg.copy_mode = copy_mode;
  358. nfa.data = &msg;
  359. nfa.nfa_type = NFULA_CFG_MODE;
  360. nfa.nfa_len = sizeof(msg);
  361. return nflog_send_config_msg(handle, AF_UNSPEC, group_id, &nfa);
  362. }
  363. static int
  364. nfqueue_send_verdict(const pcap_t *handle, uint16_t group_id, u_int32_t id, u_int32_t verdict)
  365. {
  366. struct nfqnl_msg_verdict_hdr msg;
  367. struct my_nfattr nfa;
  368. msg.id = htonl(id);
  369. msg.verdict = htonl(verdict);
  370. nfa.data = &msg;
  371. nfa.nfa_type = NFQA_VERDICT_HDR;
  372. nfa.nfa_len = sizeof(msg);
  373. return netfilter_send_config_msg(handle, (NFNL_SUBSYS_QUEUE << 8) | NFQNL_MSG_VERDICT, 0, AF_UNSPEC, group_id, &nfa);
  374. }
  375. static int
  376. nfqueue_send_config_msg(const pcap_t *handle, uint8_t family, u_int16_t group_id, const struct my_nfattr *mynfa)
  377. {
  378. return netfilter_send_config_msg(handle, (NFNL_SUBSYS_QUEUE << 8) | NFQNL_MSG_CONFIG, 1, family, group_id, mynfa);
  379. }
  380. static int
  381. nfqueue_send_config_cmd(const pcap_t *handle, uint16_t group_id, u_int8_t cmd, u_int16_t pf)
  382. {
  383. struct nfqnl_msg_config_cmd msg;
  384. struct my_nfattr nfa;
  385. msg.command = cmd;
  386. msg.pf = htons(pf);
  387. nfa.data = &msg;
  388. nfa.nfa_type = NFQA_CFG_CMD;
  389. nfa.nfa_len = sizeof(msg);
  390. return nfqueue_send_config_msg(handle, AF_UNSPEC, group_id, &nfa);
  391. }
  392. static int
  393. nfqueue_send_config_mode(const pcap_t *handle, uint16_t group_id, u_int8_t copy_mode, u_int32_t copy_range)
  394. {
  395. struct nfqnl_msg_config_params msg;
  396. struct my_nfattr nfa;
  397. msg.copy_range = htonl(copy_range);
  398. msg.copy_mode = copy_mode;
  399. nfa.data = &msg;
  400. nfa.nfa_type = NFQA_CFG_PARAMS;
  401. nfa.nfa_len = sizeof(msg);
  402. return nfqueue_send_config_msg(handle, AF_UNSPEC, group_id, &nfa);
  403. }
  404. static int
  405. netfilter_activate(pcap_t* handle)
  406. {
  407. const char *dev = handle->opt.device;
  408. unsigned short groups[32];
  409. int group_count = 0;
  410. nftype_t type = OTHER;
  411. int i;
  412. if (strncmp(dev, NFLOG_IFACE, strlen(NFLOG_IFACE)) == 0) {
  413. dev += strlen(NFLOG_IFACE);
  414. type = NFLOG;
  415. } else if (strncmp(dev, NFQUEUE_IFACE, strlen(NFQUEUE_IFACE)) == 0) {
  416. dev += strlen(NFQUEUE_IFACE);
  417. type = NFQUEUE;
  418. }
  419. if (type != OTHER && *dev == ':') {
  420. dev++;
  421. while (*dev) {
  422. long int group_id;
  423. char *end_dev;
  424. if (group_count == 32) {
  425. pcap_snprintf(handle->errbuf, PCAP_ERRBUF_SIZE,
  426. "Maximum 32 netfilter groups! dev: %s",
  427. handle->opt.device);
  428. return PCAP_ERROR;
  429. }
  430. group_id = strtol(dev, &end_dev, 0);
  431. if (end_dev != dev) {
  432. if (group_id < 0 || group_id > 65535) {
  433. pcap_snprintf(handle->errbuf, PCAP_ERRBUF_SIZE,
  434. "Netfilter group range from 0 to 65535 (got %ld)",
  435. group_id);
  436. return PCAP_ERROR;
  437. }
  438. groups[group_count++] = (unsigned short) group_id;
  439. dev = end_dev;
  440. }
  441. if (*dev != ',')
  442. break;
  443. dev++;
  444. }
  445. }
  446. if (type == OTHER || *dev) {
  447. pcap_snprintf(handle->errbuf, PCAP_ERRBUF_SIZE,
  448. "Can't get netfilter group(s) index from %s",
  449. handle->opt.device);
  450. return PCAP_ERROR;
  451. }
  452. /* if no groups, add default: 0 */
  453. if (!group_count) {
  454. groups[0] = 0;
  455. group_count = 1;
  456. }
  457. /*
  458. * Turn a negative snapshot value (invalid), a snapshot value of
  459. * 0 (unspecified), or a value bigger than the normal maximum
  460. * value, into the maximum allowed value.
  461. *
  462. * If some application really *needs* a bigger snapshot
  463. * length, we should just increase MAXIMUM_SNAPLEN.
  464. */
  465. if (handle->snapshot <= 0 || handle->snapshot > MAXIMUM_SNAPLEN)
  466. handle->snapshot = MAXIMUM_SNAPLEN;
  467. /* Initialize some components of the pcap structure. */
  468. handle->bufsize = 128 + handle->snapshot;
  469. handle->offset = 0;
  470. handle->read_op = netfilter_read_linux;
  471. handle->inject_op = netfilter_inject_linux;
  472. handle->setfilter_op = install_bpf_program; /* no kernel filtering */
  473. handle->setdirection_op = NULL;
  474. handle->set_datalink_op = netfilter_set_datalink;
  475. handle->getnonblock_op = pcap_getnonblock_fd;
  476. handle->setnonblock_op = pcap_setnonblock_fd;
  477. handle->stats_op = netfilter_stats_linux;
  478. /* Create netlink socket */
  479. handle->fd = socket(AF_NETLINK, SOCK_RAW, NETLINK_NETFILTER);
  480. if (handle->fd < 0) {
  481. pcap_fmt_errmsg_for_errno(handle->errbuf, PCAP_ERRBUF_SIZE,
  482. errno, "Can't create raw socket");
  483. return PCAP_ERROR;
  484. }
  485. if (type == NFLOG) {
  486. handle->linktype = DLT_NFLOG;
  487. handle->dlt_list = (u_int *) malloc(sizeof(u_int) * 2);
  488. if (handle->dlt_list != NULL) {
  489. handle->dlt_list[0] = DLT_NFLOG;
  490. handle->dlt_list[1] = DLT_IPV4;
  491. handle->dlt_count = 2;
  492. }
  493. } else
  494. handle->linktype = DLT_IPV4;
  495. handle->buffer = malloc(handle->bufsize);
  496. if (!handle->buffer) {
  497. pcap_fmt_errmsg_for_errno(handle->errbuf, PCAP_ERRBUF_SIZE,
  498. errno, "Can't allocate dump buffer");
  499. goto close_fail;
  500. }
  501. if (type == NFLOG) {
  502. if (nflog_send_config_cmd(handle, 0, NFULNL_CFG_CMD_PF_UNBIND, AF_INET) < 0) {
  503. pcap_fmt_errmsg_for_errno(handle->errbuf,
  504. PCAP_ERRBUF_SIZE, errno,
  505. "NFULNL_CFG_CMD_PF_UNBIND");
  506. goto close_fail;
  507. }
  508. if (nflog_send_config_cmd(handle, 0, NFULNL_CFG_CMD_PF_BIND, AF_INET) < 0) {
  509. pcap_fmt_errmsg_for_errno(handle->errbuf,
  510. PCAP_ERRBUF_SIZE, errno, "NFULNL_CFG_CMD_PF_BIND");
  511. goto close_fail;
  512. }
  513. /* Bind socket to the nflog groups */
  514. for (i = 0; i < group_count; i++) {
  515. if (nflog_send_config_cmd(handle, groups[i], NFULNL_CFG_CMD_BIND, AF_UNSPEC) < 0) {
  516. pcap_fmt_errmsg_for_errno(handle->errbuf,
  517. PCAP_ERRBUF_SIZE, errno,
  518. "Can't listen on group group index");
  519. goto close_fail;
  520. }
  521. if (nflog_send_config_mode(handle, groups[i], NFULNL_COPY_PACKET, handle->snapshot) < 0) {
  522. pcap_fmt_errmsg_for_errno(handle->errbuf,
  523. PCAP_ERRBUF_SIZE, errno,
  524. "NFULNL_COPY_PACKET");
  525. goto close_fail;
  526. }
  527. }
  528. } else {
  529. if (nfqueue_send_config_cmd(handle, 0, NFQNL_CFG_CMD_PF_UNBIND, AF_INET) < 0) {
  530. pcap_fmt_errmsg_for_errno(handle->errbuf,
  531. PCAP_ERRBUF_SIZE, errno, "NFQNL_CFG_CMD_PF_UNBIND");
  532. goto close_fail;
  533. }
  534. if (nfqueue_send_config_cmd(handle, 0, NFQNL_CFG_CMD_PF_BIND, AF_INET) < 0) {
  535. pcap_fmt_errmsg_for_errno(handle->errbuf,
  536. PCAP_ERRBUF_SIZE, errno, "NFQNL_CFG_CMD_PF_BIND");
  537. goto close_fail;
  538. }
  539. /* Bind socket to the nfqueue groups */
  540. for (i = 0; i < group_count; i++) {
  541. if (nfqueue_send_config_cmd(handle, groups[i], NFQNL_CFG_CMD_BIND, AF_UNSPEC) < 0) {
  542. pcap_fmt_errmsg_for_errno(handle->errbuf,
  543. PCAP_ERRBUF_SIZE, errno,
  544. "Can't listen on group group index");
  545. goto close_fail;
  546. }
  547. if (nfqueue_send_config_mode(handle, groups[i], NFQNL_COPY_PACKET, handle->snapshot) < 0) {
  548. pcap_fmt_errmsg_for_errno(handle->errbuf,
  549. PCAP_ERRBUF_SIZE, errno,
  550. "NFQNL_COPY_PACKET");
  551. goto close_fail;
  552. }
  553. }
  554. }
  555. if (handle->opt.rfmon) {
  556. /*
  557. * Monitor mode doesn't apply to netfilter devices.
  558. */
  559. pcap_cleanup_live_common(handle);
  560. return PCAP_ERROR_RFMON_NOTSUP;
  561. }
  562. if (handle->opt.buffer_size != 0) {
  563. /*
  564. * Set the socket buffer size to the specified value.
  565. */
  566. if (setsockopt(handle->fd, SOL_SOCKET, SO_RCVBUF, &handle->opt.buffer_size, sizeof(handle->opt.buffer_size)) == -1) {
  567. pcap_fmt_errmsg_for_errno(handle->errbuf,
  568. PCAP_ERRBUF_SIZE, errno, "SO_RCVBUF");
  569. goto close_fail;
  570. }
  571. }
  572. handle->selectable_fd = handle->fd;
  573. return 0;
  574. close_fail:
  575. pcap_cleanup_live_common(handle);
  576. return PCAP_ERROR;
  577. }
  578. pcap_t *
  579. netfilter_create(const char *device, char *ebuf, int *is_ours)
  580. {
  581. const char *cp;
  582. pcap_t *p;
  583. /* Does this look like an netfilter device? */
  584. cp = strrchr(device, '/');
  585. if (cp == NULL)
  586. cp = device;
  587. /* Does it begin with NFLOG_IFACE or NFQUEUE_IFACE? */
  588. if (strncmp(cp, NFLOG_IFACE, sizeof NFLOG_IFACE - 1) == 0)
  589. cp += sizeof NFLOG_IFACE - 1;
  590. else if (strncmp(cp, NFQUEUE_IFACE, sizeof NFQUEUE_IFACE - 1) == 0)
  591. cp += sizeof NFQUEUE_IFACE - 1;
  592. else {
  593. /* Nope, doesn't begin with NFLOG_IFACE nor NFQUEUE_IFACE */
  594. *is_ours = 0;
  595. return NULL;
  596. }
  597. /*
  598. * Yes - is that either the end of the name, or is it followed
  599. * by a colon?
  600. */
  601. if (*cp != ':' && *cp != '\0') {
  602. /* Nope */
  603. *is_ours = 0;
  604. return NULL;
  605. }
  606. /* OK, it's probably ours. */
  607. *is_ours = 1;
  608. p = pcap_create_common(ebuf, sizeof (struct pcap_netfilter));
  609. if (p == NULL)
  610. return (NULL);
  611. p->activate_op = netfilter_activate;
  612. return (p);
  613. }
  614. int
  615. netfilter_findalldevs(pcap_if_list_t *devlistp, char *err_str)
  616. {
  617. int sock;
  618. sock = socket(AF_NETLINK, SOCK_RAW, NETLINK_NETFILTER);
  619. if (sock < 0) {
  620. /* if netlink is not supported this is not fatal */
  621. if (errno == EAFNOSUPPORT || errno == EPROTONOSUPPORT)
  622. return 0;
  623. pcap_fmt_errmsg_for_errno(err_str, PCAP_ERRBUF_SIZE,
  624. errno, "Can't open netlink socket");
  625. return -1;
  626. }
  627. close(sock);
  628. /*
  629. * The notion of "connected" vs. "disconnected" doesn't apply.
  630. * XXX - what about "up" and "running"?
  631. */
  632. if (add_dev(devlistp, NFLOG_IFACE,
  633. PCAP_IF_CONNECTION_STATUS_NOT_APPLICABLE,
  634. "Linux netfilter log (NFLOG) interface", err_str) == NULL)
  635. return -1;
  636. if (add_dev(devlistp, NFQUEUE_IFACE,
  637. PCAP_IF_CONNECTION_STATUS_NOT_APPLICABLE,
  638. "Linux netfilter queue (NFQUEUE) interface", err_str) == NULL)
  639. return -1;
  640. return 0;
  641. }