nf-queue.c revision f9071054d0f2512dea9e95f99c308e931ed78dba
1/* 2 * src/nf-log.c Monitor netfilter queue events 3 * 4 * This library is free software; you can redistribute it and/or 5 * modify it under the terms of the GNU Lesser General Public 6 * License as published by the Free Software Foundation version 2.1 7 * of the License. 8 * 9 * Copyright (c) 2007, 2008 Patrick McHardy <kaber@trash.net> 10 */ 11 12 13#include "queue-utils.h" 14 15static struct nl_sock *nf_sock; 16 17static void obj_input(struct nl_object *obj, void *arg) 18{ 19 struct nfnl_queue_msg *msg = (struct nfnl_queue_msg *) obj; 20 struct nl_dump_params dp = { 21 .dp_type = NL_DUMP_STATS, 22 .dp_fd = stdout, 23 .dp_dump_msgtype = 1, 24 }; 25 26 nfnl_queue_msg_set_verdict(msg, NF_ACCEPT); 27 nl_object_dump(obj, &dp); 28 nfnl_queue_msg_send_verdict(nf_sock, msg); 29} 30 31static int event_input(struct nl_msg *msg, void *arg) 32{ 33 if (nl_msg_parse(msg, &obj_input, NULL) < 0) 34 fprintf(stderr, "<<EVENT>> Unknown message type\n"); 35 36 /* Exit nl_recvmsgs_def() and return to the main select() */ 37 return NL_STOP; 38} 39 40int main(int argc, char *argv[]) 41{ 42 struct nl_sock *rt_sock; 43 struct nl_cache *link_cache; 44 struct nfnl_queue *queue; 45 enum nfnl_queue_copy_mode copy_mode; 46 uint32_t copy_range; 47 int err = 1; 48 int family; 49 50 nf_sock = nfnl_queue_socket_alloc(); 51 if (nf_sock == NULL) 52 fatal(ENOBUFS, "Unable to allocate netlink socket"); 53 54 nl_socket_disable_seq_check(nf_sock); 55 nl_socket_modify_cb(nf_sock, NL_CB_VALID, NL_CB_CUSTOM, event_input, NULL); 56 57 if ((argc > 1 && !strcasecmp(argv[1], "-h")) || argc < 3) { 58 printf("Usage: nf-queue family group [ copy_mode ] " 59 "[ copy_range ]\n"); 60 return 2; 61 } 62 63 nlt_connect(nf_sock, NETLINK_NETFILTER); 64 65 if ((family = nl_str2af(argv[1])) == AF_UNSPEC) 66 fatal(NLE_INVAL, "Unknown family \"%s\"", argv[1]); 67 68 nfnl_queue_pf_unbind(nf_sock, family); 69 if ((err = nfnl_queue_pf_bind(nf_sock, family)) < 0) 70 fatal(err, "Unable to bind logger: %s", nl_geterror(err)); 71 72 queue = nlt_alloc_queue(); 73 nfnl_queue_set_group(queue, atoi(argv[2])); 74 75 copy_mode = NFNL_QUEUE_COPY_PACKET; 76 if (argc > 3) { 77 copy_mode = nfnl_queue_str2copy_mode(argv[3]); 78 if (copy_mode < 0) 79 fatal(copy_mode, "Unable to parse copy mode \"%s\": %s", 80 argv[3], nl_geterror(copy_mode)); 81 } 82 nfnl_queue_set_copy_mode(queue, copy_mode); 83 84 copy_range = 0xFFFF; 85 if (argc > 4) 86 copy_range = atoi(argv[4]); 87 nfnl_queue_set_copy_range(queue, copy_range); 88 89 if ((err = nfnl_queue_create(nf_sock, queue)) < 0) 90 fatal(err, "Unable to bind queue: %s", nl_geterror(err)); 91 92 rt_sock = nlt_alloc_socket(); 93 nlt_connect(rt_sock, NETLINK_ROUTE); 94 link_cache = nlt_alloc_link_cache(rt_sock); 95 96 while (1) { 97 fd_set rfds; 98 int nffd, rtfd, maxfd, retval; 99 100 FD_ZERO(&rfds); 101 102 maxfd = nffd = nl_socket_get_fd(nf_sock); 103 FD_SET(nffd, &rfds); 104 105 rtfd = nl_socket_get_fd(rt_sock); 106 FD_SET(rtfd, &rfds); 107 if (maxfd < rtfd) 108 maxfd = rtfd; 109 110 /* wait for an incoming message on the netlink socket */ 111 retval = select(maxfd+1, &rfds, NULL, NULL, NULL); 112 113 if (retval) { 114 if (FD_ISSET(nffd, &rfds)) 115 nl_recvmsgs_default(nf_sock); 116 if (FD_ISSET(rtfd, &rfds)) 117 nl_recvmsgs_default(rt_sock); 118 } 119 } 120 121 return 0; 122} 123