PageRenderTime 1041ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/net/netfilter/nfnetlink_log.c

https://github.com/Mengqi/linux-2.6
C | 1015 lines | 788 code | 167 blank | 60 comment | 115 complexity | 6e80d8f445b0aebd12e956b39baa1639 MD5 | raw file
  1. /*
  2. * This is a module which is used for logging packets to userspace via
  3. * nfetlink.
  4. *
  5. * (C) 2005 by Harald Welte <laforge@netfilter.org>
  6. *
  7. * Based on the old ipv4-only ipt_ULOG.c:
  8. * (C) 2000-2004 by Harald Welte <laforge@netfilter.org>
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License version 2 as
  12. * published by the Free Software Foundation.
  13. */
  14. #include <linux/module.h>
  15. #include <linux/skbuff.h>
  16. #include <linux/init.h>
  17. #include <linux/ip.h>
  18. #include <linux/ipv6.h>
  19. #include <linux/netdevice.h>
  20. #include <linux/netfilter.h>
  21. #include <linux/netlink.h>
  22. #include <linux/netfilter/nfnetlink.h>
  23. #include <linux/netfilter/nfnetlink_log.h>
  24. #include <linux/spinlock.h>
  25. #include <linux/sysctl.h>
  26. #include <linux/proc_fs.h>
  27. #include <linux/security.h>
  28. #include <linux/list.h>
  29. #include <linux/jhash.h>
  30. #include <linux/random.h>
  31. #include <linux/slab.h>
  32. #include <net/sock.h>
  33. #include <net/netfilter/nf_log.h>
  34. #include <net/netfilter/nfnetlink_log.h>
  35. #include <linux/atomic.h>
  36. #ifdef CONFIG_BRIDGE_NETFILTER
  37. #include "../bridge/br_private.h"
  38. #endif
  39. #define NFULNL_NLBUFSIZ_DEFAULT NLMSG_GOODSIZE
  40. #define NFULNL_TIMEOUT_DEFAULT 100 /* every second */
  41. #define NFULNL_QTHRESH_DEFAULT 100 /* 100 packets */
  42. #define NFULNL_COPY_RANGE_MAX 0xFFFF /* max packet size is limited by 16-bit struct nfattr nfa_len field */
  43. #define PRINTR(x, args...) do { if (net_ratelimit()) \
  44. printk(x, ## args); } while (0);
  45. struct nfulnl_instance {
  46. struct hlist_node hlist; /* global list of instances */
  47. spinlock_t lock;
  48. atomic_t use; /* use count */
  49. unsigned int qlen; /* number of nlmsgs in skb */
  50. struct sk_buff *skb; /* pre-allocatd skb */
  51. struct timer_list timer;
  52. int peer_pid; /* PID of the peer process */
  53. /* configurable parameters */
  54. unsigned int flushtimeout; /* timeout until queue flush */
  55. unsigned int nlbufsiz; /* netlink buffer allocation size */
  56. unsigned int qthreshold; /* threshold of the queue */
  57. u_int32_t copy_range;
  58. u_int32_t seq; /* instance-local sequential counter */
  59. u_int16_t group_num; /* number of this queue */
  60. u_int16_t flags;
  61. u_int8_t copy_mode;
  62. struct rcu_head rcu;
  63. };
  64. static DEFINE_SPINLOCK(instances_lock);
  65. static atomic_t global_seq;
  66. #define INSTANCE_BUCKETS 16
  67. static struct hlist_head instance_table[INSTANCE_BUCKETS];
  68. static unsigned int hash_init;
  69. static inline u_int8_t instance_hashfn(u_int16_t group_num)
  70. {
  71. return ((group_num & 0xff) % INSTANCE_BUCKETS);
  72. }
  73. static struct nfulnl_instance *
  74. __instance_lookup(u_int16_t group_num)
  75. {
  76. struct hlist_head *head;
  77. struct hlist_node *pos;
  78. struct nfulnl_instance *inst;
  79. head = &instance_table[instance_hashfn(group_num)];
  80. hlist_for_each_entry_rcu(inst, pos, head, hlist) {
  81. if (inst->group_num == group_num)
  82. return inst;
  83. }
  84. return NULL;
  85. }
  86. static inline void
  87. instance_get(struct nfulnl_instance *inst)
  88. {
  89. atomic_inc(&inst->use);
  90. }
  91. static struct nfulnl_instance *
  92. instance_lookup_get(u_int16_t group_num)
  93. {
  94. struct nfulnl_instance *inst;
  95. rcu_read_lock_bh();
  96. inst = __instance_lookup(group_num);
  97. if (inst && !atomic_inc_not_zero(&inst->use))
  98. inst = NULL;
  99. rcu_read_unlock_bh();
  100. return inst;
  101. }
  102. static void nfulnl_instance_free_rcu(struct rcu_head *head)
  103. {
  104. kfree(container_of(head, struct nfulnl_instance, rcu));
  105. module_put(THIS_MODULE);
  106. }
  107. static void
  108. instance_put(struct nfulnl_instance *inst)
  109. {
  110. if (inst && atomic_dec_and_test(&inst->use))
  111. call_rcu_bh(&inst->rcu, nfulnl_instance_free_rcu);
  112. }
  113. static void nfulnl_timer(unsigned long data);
  114. static struct nfulnl_instance *
  115. instance_create(u_int16_t group_num, int pid)
  116. {
  117. struct nfulnl_instance *inst;
  118. int err;
  119. spin_lock_bh(&instances_lock);
  120. if (__instance_lookup(group_num)) {
  121. err = -EEXIST;
  122. goto out_unlock;
  123. }
  124. inst = kzalloc(sizeof(*inst), GFP_ATOMIC);
  125. if (!inst) {
  126. err = -ENOMEM;
  127. goto out_unlock;
  128. }
  129. if (!try_module_get(THIS_MODULE)) {
  130. kfree(inst);
  131. err = -EAGAIN;
  132. goto out_unlock;
  133. }
  134. INIT_HLIST_NODE(&inst->hlist);
  135. spin_lock_init(&inst->lock);
  136. /* needs to be two, since we _put() after creation */
  137. atomic_set(&inst->use, 2);
  138. setup_timer(&inst->timer, nfulnl_timer, (unsigned long)inst);
  139. inst->peer_pid = pid;
  140. inst->group_num = group_num;
  141. inst->qthreshold = NFULNL_QTHRESH_DEFAULT;
  142. inst->flushtimeout = NFULNL_TIMEOUT_DEFAULT;
  143. inst->nlbufsiz = NFULNL_NLBUFSIZ_DEFAULT;
  144. inst->copy_mode = NFULNL_COPY_PACKET;
  145. inst->copy_range = NFULNL_COPY_RANGE_MAX;
  146. hlist_add_head_rcu(&inst->hlist,
  147. &instance_table[instance_hashfn(group_num)]);
  148. spin_unlock_bh(&instances_lock);
  149. return inst;
  150. out_unlock:
  151. spin_unlock_bh(&instances_lock);
  152. return ERR_PTR(err);
  153. }
  154. static void __nfulnl_flush(struct nfulnl_instance *inst);
  155. /* called with BH disabled */
  156. static void
  157. __instance_destroy(struct nfulnl_instance *inst)
  158. {
  159. /* first pull it out of the global list */
  160. hlist_del_rcu(&inst->hlist);
  161. /* then flush all pending packets from skb */
  162. spin_lock(&inst->lock);
  163. /* lockless readers wont be able to use us */
  164. inst->copy_mode = NFULNL_COPY_DISABLED;
  165. if (inst->skb)
  166. __nfulnl_flush(inst);
  167. spin_unlock(&inst->lock);
  168. /* and finally put the refcount */
  169. instance_put(inst);
  170. }
  171. static inline void
  172. instance_destroy(struct nfulnl_instance *inst)
  173. {
  174. spin_lock_bh(&instances_lock);
  175. __instance_destroy(inst);
  176. spin_unlock_bh(&instances_lock);
  177. }
  178. static int
  179. nfulnl_set_mode(struct nfulnl_instance *inst, u_int8_t mode,
  180. unsigned int range)
  181. {
  182. int status = 0;
  183. spin_lock_bh(&inst->lock);
  184. switch (mode) {
  185. case NFULNL_COPY_NONE:
  186. case NFULNL_COPY_META:
  187. inst->copy_mode = mode;
  188. inst->copy_range = 0;
  189. break;
  190. case NFULNL_COPY_PACKET:
  191. inst->copy_mode = mode;
  192. inst->copy_range = min_t(unsigned int,
  193. range, NFULNL_COPY_RANGE_MAX);
  194. break;
  195. default:
  196. status = -EINVAL;
  197. break;
  198. }
  199. spin_unlock_bh(&inst->lock);
  200. return status;
  201. }
  202. static int
  203. nfulnl_set_nlbufsiz(struct nfulnl_instance *inst, u_int32_t nlbufsiz)
  204. {
  205. int status;
  206. spin_lock_bh(&inst->lock);
  207. if (nlbufsiz < NFULNL_NLBUFSIZ_DEFAULT)
  208. status = -ERANGE;
  209. else if (nlbufsiz > 131072)
  210. status = -ERANGE;
  211. else {
  212. inst->nlbufsiz = nlbufsiz;
  213. status = 0;
  214. }
  215. spin_unlock_bh(&inst->lock);
  216. return status;
  217. }
  218. static int
  219. nfulnl_set_timeout(struct nfulnl_instance *inst, u_int32_t timeout)
  220. {
  221. spin_lock_bh(&inst->lock);
  222. inst->flushtimeout = timeout;
  223. spin_unlock_bh(&inst->lock);
  224. return 0;
  225. }
  226. static int
  227. nfulnl_set_qthresh(struct nfulnl_instance *inst, u_int32_t qthresh)
  228. {
  229. spin_lock_bh(&inst->lock);
  230. inst->qthreshold = qthresh;
  231. spin_unlock_bh(&inst->lock);
  232. return 0;
  233. }
  234. static int
  235. nfulnl_set_flags(struct nfulnl_instance *inst, u_int16_t flags)
  236. {
  237. spin_lock_bh(&inst->lock);
  238. inst->flags = flags;
  239. spin_unlock_bh(&inst->lock);
  240. return 0;
  241. }
  242. static struct sk_buff *
  243. nfulnl_alloc_skb(unsigned int inst_size, unsigned int pkt_size)
  244. {
  245. struct sk_buff *skb;
  246. unsigned int n;
  247. /* alloc skb which should be big enough for a whole multipart
  248. * message. WARNING: has to be <= 128k due to slab restrictions */
  249. n = max(inst_size, pkt_size);
  250. skb = alloc_skb(n, GFP_ATOMIC);
  251. if (!skb) {
  252. pr_notice("nfnetlink_log: can't alloc whole buffer (%u bytes)\n",
  253. inst_size);
  254. if (n > pkt_size) {
  255. /* try to allocate only as much as we need for current
  256. * packet */
  257. skb = alloc_skb(pkt_size, GFP_ATOMIC);
  258. if (!skb)
  259. pr_err("nfnetlink_log: can't even alloc %u "
  260. "bytes\n", pkt_size);
  261. }
  262. }
  263. return skb;
  264. }
  265. static int
  266. __nfulnl_send(struct nfulnl_instance *inst)
  267. {
  268. int status = -1;
  269. if (inst->qlen > 1)
  270. NLMSG_PUT(inst->skb, 0, 0,
  271. NLMSG_DONE,
  272. sizeof(struct nfgenmsg));
  273. status = nfnetlink_unicast(inst->skb, &init_net, inst->peer_pid,
  274. MSG_DONTWAIT);
  275. inst->qlen = 0;
  276. inst->skb = NULL;
  277. nlmsg_failure:
  278. return status;
  279. }
  280. static void
  281. __nfulnl_flush(struct nfulnl_instance *inst)
  282. {
  283. /* timer holds a reference */
  284. if (del_timer(&inst->timer))
  285. instance_put(inst);
  286. if (inst->skb)
  287. __nfulnl_send(inst);
  288. }
  289. static void
  290. nfulnl_timer(unsigned long data)
  291. {
  292. struct nfulnl_instance *inst = (struct nfulnl_instance *)data;
  293. spin_lock_bh(&inst->lock);
  294. if (inst->skb)
  295. __nfulnl_send(inst);
  296. spin_unlock_bh(&inst->lock);
  297. instance_put(inst);
  298. }
  299. /* This is an inline function, we don't really care about a long
  300. * list of arguments */
  301. static inline int
  302. __build_packet_message(struct nfulnl_instance *inst,
  303. const struct sk_buff *skb,
  304. unsigned int data_len,
  305. u_int8_t pf,
  306. unsigned int hooknum,
  307. const struct net_device *indev,
  308. const struct net_device *outdev,
  309. const char *prefix, unsigned int plen)
  310. {
  311. struct nfulnl_msg_packet_hdr pmsg;
  312. struct nlmsghdr *nlh;
  313. struct nfgenmsg *nfmsg;
  314. sk_buff_data_t old_tail = inst->skb->tail;
  315. nlh = NLMSG_PUT(inst->skb, 0, 0,
  316. NFNL_SUBSYS_ULOG << 8 | NFULNL_MSG_PACKET,
  317. sizeof(struct nfgenmsg));
  318. nfmsg = NLMSG_DATA(nlh);
  319. nfmsg->nfgen_family = pf;
  320. nfmsg->version = NFNETLINK_V0;
  321. nfmsg->res_id = htons(inst->group_num);
  322. pmsg.hw_protocol = skb->protocol;
  323. pmsg.hook = hooknum;
  324. NLA_PUT(inst->skb, NFULA_PACKET_HDR, sizeof(pmsg), &pmsg);
  325. if (prefix)
  326. NLA_PUT(inst->skb, NFULA_PREFIX, plen, prefix);
  327. if (indev) {
  328. #ifndef CONFIG_BRIDGE_NETFILTER
  329. NLA_PUT_BE32(inst->skb, NFULA_IFINDEX_INDEV,
  330. htonl(indev->ifindex));
  331. #else
  332. if (pf == PF_BRIDGE) {
  333. /* Case 1: outdev is physical input device, we need to
  334. * look for bridge group (when called from
  335. * netfilter_bridge) */
  336. NLA_PUT_BE32(inst->skb, NFULA_IFINDEX_PHYSINDEV,
  337. htonl(indev->ifindex));
  338. /* this is the bridge group "brX" */
  339. /* rcu_read_lock()ed by nf_hook_slow or nf_log_packet */
  340. NLA_PUT_BE32(inst->skb, NFULA_IFINDEX_INDEV,
  341. htonl(br_port_get_rcu(indev)->br->dev->ifindex));
  342. } else {
  343. /* Case 2: indev is bridge group, we need to look for
  344. * physical device (when called from ipv4) */
  345. NLA_PUT_BE32(inst->skb, NFULA_IFINDEX_INDEV,
  346. htonl(indev->ifindex));
  347. if (skb->nf_bridge && skb->nf_bridge->physindev)
  348. NLA_PUT_BE32(inst->skb, NFULA_IFINDEX_PHYSINDEV,
  349. htonl(skb->nf_bridge->physindev->ifindex));
  350. }
  351. #endif
  352. }
  353. if (outdev) {
  354. #ifndef CONFIG_BRIDGE_NETFILTER
  355. NLA_PUT_BE32(inst->skb, NFULA_IFINDEX_OUTDEV,
  356. htonl(outdev->ifindex));
  357. #else
  358. if (pf == PF_BRIDGE) {
  359. /* Case 1: outdev is physical output device, we need to
  360. * look for bridge group (when called from
  361. * netfilter_bridge) */
  362. NLA_PUT_BE32(inst->skb, NFULA_IFINDEX_PHYSOUTDEV,
  363. htonl(outdev->ifindex));
  364. /* this is the bridge group "brX" */
  365. /* rcu_read_lock()ed by nf_hook_slow or nf_log_packet */
  366. NLA_PUT_BE32(inst->skb, NFULA_IFINDEX_OUTDEV,
  367. htonl(br_port_get_rcu(outdev)->br->dev->ifindex));
  368. } else {
  369. /* Case 2: indev is a bridge group, we need to look
  370. * for physical device (when called from ipv4) */
  371. NLA_PUT_BE32(inst->skb, NFULA_IFINDEX_OUTDEV,
  372. htonl(outdev->ifindex));
  373. if (skb->nf_bridge && skb->nf_bridge->physoutdev)
  374. NLA_PUT_BE32(inst->skb, NFULA_IFINDEX_PHYSOUTDEV,
  375. htonl(skb->nf_bridge->physoutdev->ifindex));
  376. }
  377. #endif
  378. }
  379. if (skb->mark)
  380. NLA_PUT_BE32(inst->skb, NFULA_MARK, htonl(skb->mark));
  381. if (indev && skb->dev &&
  382. skb->mac_header != skb->network_header) {
  383. struct nfulnl_msg_packet_hw phw;
  384. int len = dev_parse_header(skb, phw.hw_addr);
  385. if (len > 0) {
  386. phw.hw_addrlen = htons(len);
  387. NLA_PUT(inst->skb, NFULA_HWADDR, sizeof(phw), &phw);
  388. }
  389. }
  390. if (indev && skb_mac_header_was_set(skb)) {
  391. NLA_PUT_BE16(inst->skb, NFULA_HWTYPE, htons(skb->dev->type));
  392. NLA_PUT_BE16(inst->skb, NFULA_HWLEN,
  393. htons(skb->dev->hard_header_len));
  394. NLA_PUT(inst->skb, NFULA_HWHEADER, skb->dev->hard_header_len,
  395. skb_mac_header(skb));
  396. }
  397. if (skb->tstamp.tv64) {
  398. struct nfulnl_msg_packet_timestamp ts;
  399. struct timeval tv = ktime_to_timeval(skb->tstamp);
  400. ts.sec = cpu_to_be64(tv.tv_sec);
  401. ts.usec = cpu_to_be64(tv.tv_usec);
  402. NLA_PUT(inst->skb, NFULA_TIMESTAMP, sizeof(ts), &ts);
  403. }
  404. /* UID */
  405. if (skb->sk) {
  406. read_lock_bh(&skb->sk->sk_callback_lock);
  407. if (skb->sk->sk_socket && skb->sk->sk_socket->file) {
  408. struct file *file = skb->sk->sk_socket->file;
  409. __be32 uid = htonl(file->f_cred->fsuid);
  410. __be32 gid = htonl(file->f_cred->fsgid);
  411. /* need to unlock here since NLA_PUT may goto */
  412. read_unlock_bh(&skb->sk->sk_callback_lock);
  413. NLA_PUT_BE32(inst->skb, NFULA_UID, uid);
  414. NLA_PUT_BE32(inst->skb, NFULA_GID, gid);
  415. } else
  416. read_unlock_bh(&skb->sk->sk_callback_lock);
  417. }
  418. /* local sequence number */
  419. if (inst->flags & NFULNL_CFG_F_SEQ)
  420. NLA_PUT_BE32(inst->skb, NFULA_SEQ, htonl(inst->seq++));
  421. /* global sequence number */
  422. if (inst->flags & NFULNL_CFG_F_SEQ_GLOBAL)
  423. NLA_PUT_BE32(inst->skb, NFULA_SEQ_GLOBAL,
  424. htonl(atomic_inc_return(&global_seq)));
  425. if (data_len) {
  426. struct nlattr *nla;
  427. int size = nla_attr_size(data_len);
  428. if (skb_tailroom(inst->skb) < nla_total_size(data_len)) {
  429. printk(KERN_WARNING "nfnetlink_log: no tailroom!\n");
  430. goto nlmsg_failure;
  431. }
  432. nla = (struct nlattr *)skb_put(inst->skb, nla_total_size(data_len));
  433. nla->nla_type = NFULA_PAYLOAD;
  434. nla->nla_len = size;
  435. if (skb_copy_bits(skb, 0, nla_data(nla), data_len))
  436. BUG();
  437. }
  438. nlh->nlmsg_len = inst->skb->tail - old_tail;
  439. return 0;
  440. nlmsg_failure:
  441. nla_put_failure:
  442. PRINTR(KERN_ERR "nfnetlink_log: error creating log nlmsg\n");
  443. return -1;
  444. }
  445. #define RCV_SKB_FAIL(err) do { netlink_ack(skb, nlh, (err)); return; } while (0)
  446. static struct nf_loginfo default_loginfo = {
  447. .type = NF_LOG_TYPE_ULOG,
  448. .u = {
  449. .ulog = {
  450. .copy_len = 0xffff,
  451. .group = 0,
  452. .qthreshold = 1,
  453. },
  454. },
  455. };
  456. /* log handler for internal netfilter logging api */
  457. void
  458. nfulnl_log_packet(u_int8_t pf,
  459. unsigned int hooknum,
  460. const struct sk_buff *skb,
  461. const struct net_device *in,
  462. const struct net_device *out,
  463. const struct nf_loginfo *li_user,
  464. const char *prefix)
  465. {
  466. unsigned int size, data_len;
  467. struct nfulnl_instance *inst;
  468. const struct nf_loginfo *li;
  469. unsigned int qthreshold;
  470. unsigned int plen;
  471. if (li_user && li_user->type == NF_LOG_TYPE_ULOG)
  472. li = li_user;
  473. else
  474. li = &default_loginfo;
  475. inst = instance_lookup_get(li->u.ulog.group);
  476. if (!inst)
  477. return;
  478. plen = 0;
  479. if (prefix)
  480. plen = strlen(prefix) + 1;
  481. /* FIXME: do we want to make the size calculation conditional based on
  482. * what is actually present? way more branches and checks, but more
  483. * memory efficient... */
  484. size = NLMSG_SPACE(sizeof(struct nfgenmsg))
  485. + nla_total_size(sizeof(struct nfulnl_msg_packet_hdr))
  486. + nla_total_size(sizeof(u_int32_t)) /* ifindex */
  487. + nla_total_size(sizeof(u_int32_t)) /* ifindex */
  488. #ifdef CONFIG_BRIDGE_NETFILTER
  489. + nla_total_size(sizeof(u_int32_t)) /* ifindex */
  490. + nla_total_size(sizeof(u_int32_t)) /* ifindex */
  491. #endif
  492. + nla_total_size(sizeof(u_int32_t)) /* mark */
  493. + nla_total_size(sizeof(u_int32_t)) /* uid */
  494. + nla_total_size(sizeof(u_int32_t)) /* gid */
  495. + nla_total_size(plen) /* prefix */
  496. + nla_total_size(sizeof(struct nfulnl_msg_packet_hw))
  497. + nla_total_size(sizeof(struct nfulnl_msg_packet_timestamp));
  498. if (in && skb_mac_header_was_set(skb)) {
  499. size += nla_total_size(skb->dev->hard_header_len)
  500. + nla_total_size(sizeof(u_int16_t)) /* hwtype */
  501. + nla_total_size(sizeof(u_int16_t)); /* hwlen */
  502. }
  503. spin_lock_bh(&inst->lock);
  504. if (inst->flags & NFULNL_CFG_F_SEQ)
  505. size += nla_total_size(sizeof(u_int32_t));
  506. if (inst->flags & NFULNL_CFG_F_SEQ_GLOBAL)
  507. size += nla_total_size(sizeof(u_int32_t));
  508. qthreshold = inst->qthreshold;
  509. /* per-rule qthreshold overrides per-instance */
  510. if (li->u.ulog.qthreshold)
  511. if (qthreshold > li->u.ulog.qthreshold)
  512. qthreshold = li->u.ulog.qthreshold;
  513. switch (inst->copy_mode) {
  514. case NFULNL_COPY_META:
  515. case NFULNL_COPY_NONE:
  516. data_len = 0;
  517. break;
  518. case NFULNL_COPY_PACKET:
  519. if (inst->copy_range == 0
  520. || inst->copy_range > skb->len)
  521. data_len = skb->len;
  522. else
  523. data_len = inst->copy_range;
  524. size += nla_total_size(data_len);
  525. break;
  526. case NFULNL_COPY_DISABLED:
  527. default:
  528. goto unlock_and_release;
  529. }
  530. if (inst->skb &&
  531. size > skb_tailroom(inst->skb) - sizeof(struct nfgenmsg)) {
  532. /* either the queue len is too high or we don't have
  533. * enough room in the skb left. flush to userspace. */
  534. __nfulnl_flush(inst);
  535. }
  536. if (!inst->skb) {
  537. inst->skb = nfulnl_alloc_skb(inst->nlbufsiz, size);
  538. if (!inst->skb)
  539. goto alloc_failure;
  540. }
  541. inst->qlen++;
  542. __build_packet_message(inst, skb, data_len, pf,
  543. hooknum, in, out, prefix, plen);
  544. if (inst->qlen >= qthreshold)
  545. __nfulnl_flush(inst);
  546. /* timer_pending always called within inst->lock, so there
  547. * is no chance of a race here */
  548. else if (!timer_pending(&inst->timer)) {
  549. instance_get(inst);
  550. inst->timer.expires = jiffies + (inst->flushtimeout*HZ/100);
  551. add_timer(&inst->timer);
  552. }
  553. unlock_and_release:
  554. spin_unlock_bh(&inst->lock);
  555. instance_put(inst);
  556. return;
  557. alloc_failure:
  558. /* FIXME: statistics */
  559. goto unlock_and_release;
  560. }
  561. EXPORT_SYMBOL_GPL(nfulnl_log_packet);
  562. static int
  563. nfulnl_rcv_nl_event(struct notifier_block *this,
  564. unsigned long event, void *ptr)
  565. {
  566. struct netlink_notify *n = ptr;
  567. if (event == NETLINK_URELEASE && n->protocol == NETLINK_NETFILTER) {
  568. int i;
  569. /* destroy all instances for this pid */
  570. spin_lock_bh(&instances_lock);
  571. for (i = 0; i < INSTANCE_BUCKETS; i++) {
  572. struct hlist_node *tmp, *t2;
  573. struct nfulnl_instance *inst;
  574. struct hlist_head *head = &instance_table[i];
  575. hlist_for_each_entry_safe(inst, tmp, t2, head, hlist) {
  576. if ((net_eq(n->net, &init_net)) &&
  577. (n->pid == inst->peer_pid))
  578. __instance_destroy(inst);
  579. }
  580. }
  581. spin_unlock_bh(&instances_lock);
  582. }
  583. return NOTIFY_DONE;
  584. }
  585. static struct notifier_block nfulnl_rtnl_notifier = {
  586. .notifier_call = nfulnl_rcv_nl_event,
  587. };
  588. static int
  589. nfulnl_recv_unsupp(struct sock *ctnl, struct sk_buff *skb,
  590. const struct nlmsghdr *nlh,
  591. const struct nlattr * const nfqa[])
  592. {
  593. return -ENOTSUPP;
  594. }
  595. static struct nf_logger nfulnl_logger __read_mostly = {
  596. .name = "nfnetlink_log",
  597. .logfn = &nfulnl_log_packet,
  598. .me = THIS_MODULE,
  599. };
  600. static const struct nla_policy nfula_cfg_policy[NFULA_CFG_MAX+1] = {
  601. [NFULA_CFG_CMD] = { .len = sizeof(struct nfulnl_msg_config_cmd) },
  602. [NFULA_CFG_MODE] = { .len = sizeof(struct nfulnl_msg_config_mode) },
  603. [NFULA_CFG_TIMEOUT] = { .type = NLA_U32 },
  604. [NFULA_CFG_QTHRESH] = { .type = NLA_U32 },
  605. [NFULA_CFG_NLBUFSIZ] = { .type = NLA_U32 },
  606. [NFULA_CFG_FLAGS] = { .type = NLA_U16 },
  607. };
  608. static int
  609. nfulnl_recv_config(struct sock *ctnl, struct sk_buff *skb,
  610. const struct nlmsghdr *nlh,
  611. const struct nlattr * const nfula[])
  612. {
  613. struct nfgenmsg *nfmsg = NLMSG_DATA(nlh);
  614. u_int16_t group_num = ntohs(nfmsg->res_id);
  615. struct nfulnl_instance *inst;
  616. struct nfulnl_msg_config_cmd *cmd = NULL;
  617. int ret = 0;
  618. if (nfula[NFULA_CFG_CMD]) {
  619. u_int8_t pf = nfmsg->nfgen_family;
  620. cmd = nla_data(nfula[NFULA_CFG_CMD]);
  621. /* Commands without queue context */
  622. switch (cmd->command) {
  623. case NFULNL_CFG_CMD_PF_BIND:
  624. return nf_log_bind_pf(pf, &nfulnl_logger);
  625. case NFULNL_CFG_CMD_PF_UNBIND:
  626. nf_log_unbind_pf(pf);
  627. return 0;
  628. }
  629. }
  630. inst = instance_lookup_get(group_num);
  631. if (inst && inst->peer_pid != NETLINK_CB(skb).pid) {
  632. ret = -EPERM;
  633. goto out_put;
  634. }
  635. if (cmd != NULL) {
  636. switch (cmd->command) {
  637. case NFULNL_CFG_CMD_BIND:
  638. if (inst) {
  639. ret = -EBUSY;
  640. goto out_put;
  641. }
  642. inst = instance_create(group_num,
  643. NETLINK_CB(skb).pid);
  644. if (IS_ERR(inst)) {
  645. ret = PTR_ERR(inst);
  646. goto out;
  647. }
  648. break;
  649. case NFULNL_CFG_CMD_UNBIND:
  650. if (!inst) {
  651. ret = -ENODEV;
  652. goto out;
  653. }
  654. instance_destroy(inst);
  655. goto out_put;
  656. default:
  657. ret = -ENOTSUPP;
  658. break;
  659. }
  660. }
  661. if (nfula[NFULA_CFG_MODE]) {
  662. struct nfulnl_msg_config_mode *params;
  663. params = nla_data(nfula[NFULA_CFG_MODE]);
  664. if (!inst) {
  665. ret = -ENODEV;
  666. goto out;
  667. }
  668. nfulnl_set_mode(inst, params->copy_mode,
  669. ntohl(params->copy_range));
  670. }
  671. if (nfula[NFULA_CFG_TIMEOUT]) {
  672. __be32 timeout = nla_get_be32(nfula[NFULA_CFG_TIMEOUT]);
  673. if (!inst) {
  674. ret = -ENODEV;
  675. goto out;
  676. }
  677. nfulnl_set_timeout(inst, ntohl(timeout));
  678. }
  679. if (nfula[NFULA_CFG_NLBUFSIZ]) {
  680. __be32 nlbufsiz = nla_get_be32(nfula[NFULA_CFG_NLBUFSIZ]);
  681. if (!inst) {
  682. ret = -ENODEV;
  683. goto out;
  684. }
  685. nfulnl_set_nlbufsiz(inst, ntohl(nlbufsiz));
  686. }
  687. if (nfula[NFULA_CFG_QTHRESH]) {
  688. __be32 qthresh = nla_get_be32(nfula[NFULA_CFG_QTHRESH]);
  689. if (!inst) {
  690. ret = -ENODEV;
  691. goto out;
  692. }
  693. nfulnl_set_qthresh(inst, ntohl(qthresh));
  694. }
  695. if (nfula[NFULA_CFG_FLAGS]) {
  696. __be16 flags = nla_get_be16(nfula[NFULA_CFG_FLAGS]);
  697. if (!inst) {
  698. ret = -ENODEV;
  699. goto out;
  700. }
  701. nfulnl_set_flags(inst, ntohs(flags));
  702. }
  703. out_put:
  704. instance_put(inst);
  705. out:
  706. return ret;
  707. }
  708. static const struct nfnl_callback nfulnl_cb[NFULNL_MSG_MAX] = {
  709. [NFULNL_MSG_PACKET] = { .call = nfulnl_recv_unsupp,
  710. .attr_count = NFULA_MAX, },
  711. [NFULNL_MSG_CONFIG] = { .call = nfulnl_recv_config,
  712. .attr_count = NFULA_CFG_MAX,
  713. .policy = nfula_cfg_policy },
  714. };
  715. static const struct nfnetlink_subsystem nfulnl_subsys = {
  716. .name = "log",
  717. .subsys_id = NFNL_SUBSYS_ULOG,
  718. .cb_count = NFULNL_MSG_MAX,
  719. .cb = nfulnl_cb,
  720. };
  721. #ifdef CONFIG_PROC_FS
  722. struct iter_state {
  723. unsigned int bucket;
  724. };
  725. static struct hlist_node *get_first(struct iter_state *st)
  726. {
  727. if (!st)
  728. return NULL;
  729. for (st->bucket = 0; st->bucket < INSTANCE_BUCKETS; st->bucket++) {
  730. if (!hlist_empty(&instance_table[st->bucket]))
  731. return rcu_dereference_bh(hlist_first_rcu(&instance_table[st->bucket]));
  732. }
  733. return NULL;
  734. }
  735. static struct hlist_node *get_next(struct iter_state *st, struct hlist_node *h)
  736. {
  737. h = rcu_dereference_bh(hlist_next_rcu(h));
  738. while (!h) {
  739. if (++st->bucket >= INSTANCE_BUCKETS)
  740. return NULL;
  741. h = rcu_dereference_bh(hlist_first_rcu(&instance_table[st->bucket]));
  742. }
  743. return h;
  744. }
  745. static struct hlist_node *get_idx(struct iter_state *st, loff_t pos)
  746. {
  747. struct hlist_node *head;
  748. head = get_first(st);
  749. if (head)
  750. while (pos && (head = get_next(st, head)))
  751. pos--;
  752. return pos ? NULL : head;
  753. }
  754. static void *seq_start(struct seq_file *seq, loff_t *pos)
  755. __acquires(rcu_bh)
  756. {
  757. rcu_read_lock_bh();
  758. return get_idx(seq->private, *pos);
  759. }
  760. static void *seq_next(struct seq_file *s, void *v, loff_t *pos)
  761. {
  762. (*pos)++;
  763. return get_next(s->private, v);
  764. }
  765. static void seq_stop(struct seq_file *s, void *v)
  766. __releases(rcu_bh)
  767. {
  768. rcu_read_unlock_bh();
  769. }
  770. static int seq_show(struct seq_file *s, void *v)
  771. {
  772. const struct nfulnl_instance *inst = v;
  773. return seq_printf(s, "%5d %6d %5d %1d %5d %6d %2d\n",
  774. inst->group_num,
  775. inst->peer_pid, inst->qlen,
  776. inst->copy_mode, inst->copy_range,
  777. inst->flushtimeout, atomic_read(&inst->use));
  778. }
  779. static const struct seq_operations nful_seq_ops = {
  780. .start = seq_start,
  781. .next = seq_next,
  782. .stop = seq_stop,
  783. .show = seq_show,
  784. };
  785. static int nful_open(struct inode *inode, struct file *file)
  786. {
  787. return seq_open_private(file, &nful_seq_ops,
  788. sizeof(struct iter_state));
  789. }
  790. static const struct file_operations nful_file_ops = {
  791. .owner = THIS_MODULE,
  792. .open = nful_open,
  793. .read = seq_read,
  794. .llseek = seq_lseek,
  795. .release = seq_release_private,
  796. };
  797. #endif /* PROC_FS */
  798. static int __init nfnetlink_log_init(void)
  799. {
  800. int i, status = -ENOMEM;
  801. for (i = 0; i < INSTANCE_BUCKETS; i++)
  802. INIT_HLIST_HEAD(&instance_table[i]);
  803. /* it's not really all that important to have a random value, so
  804. * we can do this from the init function, even if there hasn't
  805. * been that much entropy yet */
  806. get_random_bytes(&hash_init, sizeof(hash_init));
  807. netlink_register_notifier(&nfulnl_rtnl_notifier);
  808. status = nfnetlink_subsys_register(&nfulnl_subsys);
  809. if (status < 0) {
  810. printk(KERN_ERR "log: failed to create netlink socket\n");
  811. goto cleanup_netlink_notifier;
  812. }
  813. status = nf_log_register(NFPROTO_UNSPEC, &nfulnl_logger);
  814. if (status < 0) {
  815. printk(KERN_ERR "log: failed to register logger\n");
  816. goto cleanup_subsys;
  817. }
  818. #ifdef CONFIG_PROC_FS
  819. if (!proc_create("nfnetlink_log", 0440,
  820. proc_net_netfilter, &nful_file_ops))
  821. goto cleanup_logger;
  822. #endif
  823. return status;
  824. #ifdef CONFIG_PROC_FS
  825. cleanup_logger:
  826. nf_log_unregister(&nfulnl_logger);
  827. #endif
  828. cleanup_subsys:
  829. nfnetlink_subsys_unregister(&nfulnl_subsys);
  830. cleanup_netlink_notifier:
  831. netlink_unregister_notifier(&nfulnl_rtnl_notifier);
  832. return status;
  833. }
  834. static void __exit nfnetlink_log_fini(void)
  835. {
  836. nf_log_unregister(&nfulnl_logger);
  837. #ifdef CONFIG_PROC_FS
  838. remove_proc_entry("nfnetlink_log", proc_net_netfilter);
  839. #endif
  840. nfnetlink_subsys_unregister(&nfulnl_subsys);
  841. netlink_unregister_notifier(&nfulnl_rtnl_notifier);
  842. }
  843. MODULE_DESCRIPTION("netfilter userspace logging");
  844. MODULE_AUTHOR("Harald Welte <laforge@netfilter.org>");
  845. MODULE_LICENSE("GPL");
  846. MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_ULOG);
  847. module_init(nfnetlink_log_init);
  848. module_exit(nfnetlink_log_fini);