nfnetlink_queue.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951
  1. /*
  2. * This is a module which is used for queueing packets and communicating with
  3. * userspace via nfnetlink.
  4. *
  5. * (C) 2005 by Harald Welte <laforge@netfilter.org>
  6. * (C) 2007 by Patrick McHardy <kaber@trash.net>
  7. *
  8. * Based on the old ipv4-only ip_queue.c:
  9. * (C) 2000-2002 James Morris <jmorris@intercode.com.au>
  10. * (C) 2003-2005 Netfilter Core Team <coreteam@netfilter.org>
  11. *
  12. * This program is free software; you can redistribute it and/or modify
  13. * it under the terms of the GNU General Public License version 2 as
  14. * published by the Free Software Foundation.
  15. *
  16. */
  17. #include <linux/module.h>
  18. #include <linux/skbuff.h>
  19. #include <linux/init.h>
  20. #include <linux/spinlock.h>
  21. #include <linux/slab.h>
  22. #include <linux/notifier.h>
  23. #include <linux/netdevice.h>
  24. #include <linux/netfilter.h>
  25. #include <linux/proc_fs.h>
  26. #include <linux/netfilter_ipv4.h>
  27. #include <linux/netfilter_ipv6.h>
  28. #include <linux/netfilter/nfnetlink.h>
  29. #include <linux/netfilter/nfnetlink_queue.h>
  30. #include <linux/list.h>
  31. #include <net/sock.h>
  32. #include <net/netfilter/nf_queue.h>
  33. #include <asm/atomic.h>
  34. #ifdef CONFIG_BRIDGE_NETFILTER
  35. #include "../bridge/br_private.h"
  36. #endif
  37. #define NFQNL_QMAX_DEFAULT 1024
  38. struct nfqnl_instance {
  39. struct hlist_node hlist; /* global list of queues */
  40. struct rcu_head rcu;
  41. int peer_pid;
  42. unsigned int queue_maxlen;
  43. unsigned int copy_range;
  44. unsigned int queue_dropped;
  45. unsigned int queue_user_dropped;
  46. u_int16_t queue_num; /* number of this queue */
  47. u_int8_t copy_mode;
  48. /*
  49. * Following fields are dirtied for each queued packet,
  50. * keep them in same cache line if possible.
  51. */
  52. spinlock_t lock;
  53. unsigned int queue_total;
  54. atomic_t id_sequence; /* 'sequence' of pkt ids */
  55. struct list_head queue_list; /* packets in queue */
  56. };
  57. typedef int (*nfqnl_cmpfn)(struct nf_queue_entry *, unsigned long);
  58. static DEFINE_SPINLOCK(instances_lock);
  59. #define INSTANCE_BUCKETS 16
  60. static struct hlist_head instance_table[INSTANCE_BUCKETS] __read_mostly;
  61. static inline u_int8_t instance_hashfn(u_int16_t queue_num)
  62. {
  63. return ((queue_num >> 8) | queue_num) % INSTANCE_BUCKETS;
  64. }
  65. static struct nfqnl_instance *
  66. instance_lookup(u_int16_t queue_num)
  67. {
  68. struct hlist_head *head;
  69. struct hlist_node *pos;
  70. struct nfqnl_instance *inst;
  71. head = &instance_table[instance_hashfn(queue_num)];
  72. hlist_for_each_entry_rcu(inst, pos, head, hlist) {
  73. if (inst->queue_num == queue_num)
  74. return inst;
  75. }
  76. return NULL;
  77. }
  78. static struct nfqnl_instance *
  79. instance_create(u_int16_t queue_num, int pid)
  80. {
  81. struct nfqnl_instance *inst;
  82. unsigned int h;
  83. int err;
  84. spin_lock(&instances_lock);
  85. if (instance_lookup(queue_num)) {
  86. err = -EEXIST;
  87. goto out_unlock;
  88. }
  89. inst = kzalloc(sizeof(*inst), GFP_ATOMIC);
  90. if (!inst) {
  91. err = -ENOMEM;
  92. goto out_unlock;
  93. }
  94. inst->queue_num = queue_num;
  95. inst->peer_pid = pid;
  96. inst->queue_maxlen = NFQNL_QMAX_DEFAULT;
  97. inst->copy_range = 0xfffff;
  98. inst->copy_mode = NFQNL_COPY_NONE;
  99. spin_lock_init(&inst->lock);
  100. INIT_LIST_HEAD(&inst->queue_list);
  101. if (!try_module_get(THIS_MODULE)) {
  102. err = -EAGAIN;
  103. goto out_free;
  104. }
  105. h = instance_hashfn(queue_num);
  106. hlist_add_head_rcu(&inst->hlist, &instance_table[h]);
  107. spin_unlock(&instances_lock);
  108. return inst;
  109. out_free:
  110. kfree(inst);
  111. out_unlock:
  112. spin_unlock(&instances_lock);
  113. return ERR_PTR(err);
  114. }
  115. static void nfqnl_flush(struct nfqnl_instance *queue, nfqnl_cmpfn cmpfn,
  116. unsigned long data);
  117. static void
  118. instance_destroy_rcu(struct rcu_head *head)
  119. {
  120. struct nfqnl_instance *inst = container_of(head, struct nfqnl_instance,
  121. rcu);
  122. nfqnl_flush(inst, NULL, 0);
  123. kfree(inst);
  124. module_put(THIS_MODULE);
  125. }
  126. static void
  127. __instance_destroy(struct nfqnl_instance *inst)
  128. {
  129. hlist_del_rcu(&inst->hlist);
  130. call_rcu(&inst->rcu, instance_destroy_rcu);
  131. }
  132. static void
  133. instance_destroy(struct nfqnl_instance *inst)
  134. {
  135. spin_lock(&instances_lock);
  136. __instance_destroy(inst);
  137. spin_unlock(&instances_lock);
  138. }
  139. static inline void
  140. __enqueue_entry(struct nfqnl_instance *queue, struct nf_queue_entry *entry)
  141. {
  142. list_add_tail(&entry->list, &queue->queue_list);
  143. queue->queue_total++;
  144. }
  145. static struct nf_queue_entry *
  146. find_dequeue_entry(struct nfqnl_instance *queue, unsigned int id)
  147. {
  148. struct nf_queue_entry *entry = NULL, *i;
  149. spin_lock_bh(&queue->lock);
  150. list_for_each_entry(i, &queue->queue_list, list) {
  151. if (i->id == id) {
  152. entry = i;
  153. break;
  154. }
  155. }
  156. if (entry) {
  157. list_del(&entry->list);
  158. queue->queue_total--;
  159. }
  160. spin_unlock_bh(&queue->lock);
  161. return entry;
  162. }
  163. static void
  164. nfqnl_flush(struct nfqnl_instance *queue, nfqnl_cmpfn cmpfn, unsigned long data)
  165. {
  166. struct nf_queue_entry *entry, *next;
  167. spin_lock_bh(&queue->lock);
  168. list_for_each_entry_safe(entry, next, &queue->queue_list, list) {
  169. if (!cmpfn || cmpfn(entry, data)) {
  170. list_del(&entry->list);
  171. queue->queue_total--;
  172. nf_reinject(entry, NF_DROP);
  173. }
  174. }
  175. spin_unlock_bh(&queue->lock);
  176. }
  177. static struct sk_buff *
  178. nfqnl_build_packet_message(struct nfqnl_instance *queue,
  179. struct nf_queue_entry *entry)
  180. {
  181. sk_buff_data_t old_tail;
  182. size_t size;
  183. size_t data_len = 0;
  184. struct sk_buff *skb;
  185. struct nfqnl_msg_packet_hdr pmsg;
  186. struct nlmsghdr *nlh;
  187. struct nfgenmsg *nfmsg;
  188. struct sk_buff *entskb = entry->skb;
  189. struct net_device *indev;
  190. struct net_device *outdev;
  191. size = NLMSG_SPACE(sizeof(struct nfgenmsg))
  192. + nla_total_size(sizeof(struct nfqnl_msg_packet_hdr))
  193. + nla_total_size(sizeof(u_int32_t)) /* ifindex */
  194. + nla_total_size(sizeof(u_int32_t)) /* ifindex */
  195. #ifdef CONFIG_BRIDGE_NETFILTER
  196. + nla_total_size(sizeof(u_int32_t)) /* ifindex */
  197. + nla_total_size(sizeof(u_int32_t)) /* ifindex */
  198. #endif
  199. + nla_total_size(sizeof(u_int32_t)) /* mark */
  200. + nla_total_size(sizeof(struct nfqnl_msg_packet_hw))
  201. + nla_total_size(sizeof(struct nfqnl_msg_packet_timestamp));
  202. outdev = entry->outdev;
  203. switch ((enum nfqnl_config_mode)ACCESS_ONCE(queue->copy_mode)) {
  204. case NFQNL_COPY_META:
  205. case NFQNL_COPY_NONE:
  206. break;
  207. case NFQNL_COPY_PACKET:
  208. if (entskb->ip_summed == CHECKSUM_PARTIAL &&
  209. skb_checksum_help(entskb))
  210. return NULL;
  211. data_len = ACCESS_ONCE(queue->copy_range);
  212. if (data_len == 0 || data_len > entskb->len)
  213. data_len = entskb->len;
  214. size += nla_total_size(data_len);
  215. break;
  216. }
  217. skb = alloc_skb(size, GFP_ATOMIC);
  218. if (!skb)
  219. goto nlmsg_failure;
  220. old_tail = skb->tail;
  221. nlh = NLMSG_PUT(skb, 0, 0,
  222. NFNL_SUBSYS_QUEUE << 8 | NFQNL_MSG_PACKET,
  223. sizeof(struct nfgenmsg));
  224. nfmsg = NLMSG_DATA(nlh);
  225. nfmsg->nfgen_family = entry->pf;
  226. nfmsg->version = NFNETLINK_V0;
  227. nfmsg->res_id = htons(queue->queue_num);
  228. entry->id = atomic_inc_return(&queue->id_sequence);
  229. pmsg.packet_id = htonl(entry->id);
  230. pmsg.hw_protocol = entskb->protocol;
  231. pmsg.hook = entry->hook;
  232. NLA_PUT(skb, NFQA_PACKET_HDR, sizeof(pmsg), &pmsg);
  233. indev = entry->indev;
  234. if (indev) {
  235. #ifndef CONFIG_BRIDGE_NETFILTER
  236. NLA_PUT_BE32(skb, NFQA_IFINDEX_INDEV, htonl(indev->ifindex));
  237. #else
  238. if (entry->pf == PF_BRIDGE) {
  239. /* Case 1: indev is physical input device, we need to
  240. * look for bridge group (when called from
  241. * netfilter_bridge) */
  242. NLA_PUT_BE32(skb, NFQA_IFINDEX_PHYSINDEV,
  243. htonl(indev->ifindex));
  244. /* this is the bridge group "brX" */
  245. /* rcu_read_lock()ed by __nf_queue */
  246. NLA_PUT_BE32(skb, NFQA_IFINDEX_INDEV,
  247. htonl(br_port_get_rcu(indev)->br->dev->ifindex));
  248. } else {
  249. /* Case 2: indev is bridge group, we need to look for
  250. * physical device (when called from ipv4) */
  251. NLA_PUT_BE32(skb, NFQA_IFINDEX_INDEV,
  252. htonl(indev->ifindex));
  253. if (entskb->nf_bridge && entskb->nf_bridge->physindev)
  254. NLA_PUT_BE32(skb, NFQA_IFINDEX_PHYSINDEV,
  255. htonl(entskb->nf_bridge->physindev->ifindex));
  256. }
  257. #endif
  258. }
  259. if (outdev) {
  260. #ifndef CONFIG_BRIDGE_NETFILTER
  261. NLA_PUT_BE32(skb, NFQA_IFINDEX_OUTDEV, htonl(outdev->ifindex));
  262. #else
  263. if (entry->pf == PF_BRIDGE) {
  264. /* Case 1: outdev is physical output device, we need to
  265. * look for bridge group (when called from
  266. * netfilter_bridge) */
  267. NLA_PUT_BE32(skb, NFQA_IFINDEX_PHYSOUTDEV,
  268. htonl(outdev->ifindex));
  269. /* this is the bridge group "brX" */
  270. /* rcu_read_lock()ed by __nf_queue */
  271. NLA_PUT_BE32(skb, NFQA_IFINDEX_OUTDEV,
  272. htonl(br_port_get_rcu(outdev)->br->dev->ifindex));
  273. } else {
  274. /* Case 2: outdev is bridge group, we need to look for
  275. * physical output device (when called from ipv4) */
  276. NLA_PUT_BE32(skb, NFQA_IFINDEX_OUTDEV,
  277. htonl(outdev->ifindex));
  278. if (entskb->nf_bridge && entskb->nf_bridge->physoutdev)
  279. NLA_PUT_BE32(skb, NFQA_IFINDEX_PHYSOUTDEV,
  280. htonl(entskb->nf_bridge->physoutdev->ifindex));
  281. }
  282. #endif
  283. }
  284. if (entskb->mark)
  285. NLA_PUT_BE32(skb, NFQA_MARK, htonl(entskb->mark));
  286. if (indev && entskb->dev &&
  287. entskb->mac_header != entskb->network_header) {
  288. struct nfqnl_msg_packet_hw phw;
  289. int len = dev_parse_header(entskb, phw.hw_addr);
  290. if (len) {
  291. phw.hw_addrlen = htons(len);
  292. NLA_PUT(skb, NFQA_HWADDR, sizeof(phw), &phw);
  293. }
  294. }
  295. if (entskb->tstamp.tv64) {
  296. struct nfqnl_msg_packet_timestamp ts;
  297. struct timeval tv = ktime_to_timeval(entskb->tstamp);
  298. ts.sec = cpu_to_be64(tv.tv_sec);
  299. ts.usec = cpu_to_be64(tv.tv_usec);
  300. NLA_PUT(skb, NFQA_TIMESTAMP, sizeof(ts), &ts);
  301. }
  302. if (data_len) {
  303. struct nlattr *nla;
  304. int sz = nla_attr_size(data_len);
  305. if (skb_tailroom(skb) < nla_total_size(data_len)) {
  306. printk(KERN_WARNING "nf_queue: no tailroom!\n");
  307. goto nlmsg_failure;
  308. }
  309. nla = (struct nlattr *)skb_put(skb, nla_total_size(data_len));
  310. nla->nla_type = NFQA_PAYLOAD;
  311. nla->nla_len = sz;
  312. if (skb_copy_bits(entskb, 0, nla_data(nla), data_len))
  313. BUG();
  314. }
  315. nlh->nlmsg_len = skb->tail - old_tail;
  316. return skb;
  317. nlmsg_failure:
  318. nla_put_failure:
  319. if (skb)
  320. kfree_skb(skb);
  321. if (net_ratelimit())
  322. printk(KERN_ERR "nf_queue: error creating packet message\n");
  323. return NULL;
  324. }
  325. static int
  326. nfqnl_enqueue_packet(struct nf_queue_entry *entry, unsigned int queuenum)
  327. {
  328. struct sk_buff *nskb;
  329. struct nfqnl_instance *queue;
  330. int err = -ENOBUFS;
  331. /* rcu_read_lock()ed by nf_hook_slow() */
  332. queue = instance_lookup(queuenum);
  333. if (!queue) {
  334. err = -ESRCH;
  335. goto err_out;
  336. }
  337. if (queue->copy_mode == NFQNL_COPY_NONE) {
  338. err = -EINVAL;
  339. goto err_out;
  340. }
  341. nskb = nfqnl_build_packet_message(queue, entry);
  342. if (nskb == NULL) {
  343. err = -ENOMEM;
  344. goto err_out;
  345. }
  346. spin_lock_bh(&queue->lock);
  347. if (!queue->peer_pid) {
  348. err = -EINVAL;
  349. goto err_out_free_nskb;
  350. }
  351. if (queue->queue_total >= queue->queue_maxlen) {
  352. queue->queue_dropped++;
  353. if (net_ratelimit())
  354. printk(KERN_WARNING "nf_queue: full at %d entries, "
  355. "dropping packets(s).\n",
  356. queue->queue_total);
  357. goto err_out_free_nskb;
  358. }
  359. /* nfnetlink_unicast will either free the nskb or add it to a socket */
  360. err = nfnetlink_unicast(nskb, &init_net, queue->peer_pid, MSG_DONTWAIT);
  361. if (err < 0) {
  362. queue->queue_user_dropped++;
  363. goto err_out_unlock;
  364. }
  365. __enqueue_entry(queue, entry);
  366. spin_unlock_bh(&queue->lock);
  367. return 0;
  368. err_out_free_nskb:
  369. kfree_skb(nskb);
  370. err_out_unlock:
  371. spin_unlock_bh(&queue->lock);
  372. err_out:
  373. return err;
  374. }
  375. static int
  376. nfqnl_mangle(void *data, int data_len, struct nf_queue_entry *e)
  377. {
  378. struct sk_buff *nskb;
  379. int diff;
  380. diff = data_len - e->skb->len;
  381. if (diff < 0) {
  382. if (pskb_trim(e->skb, data_len))
  383. return -ENOMEM;
  384. } else if (diff > 0) {
  385. if (data_len > 0xFFFF)
  386. return -EINVAL;
  387. if (diff > skb_tailroom(e->skb)) {
  388. nskb = skb_copy_expand(e->skb, skb_headroom(e->skb),
  389. diff, GFP_ATOMIC);
  390. if (!nskb) {
  391. printk(KERN_WARNING "nf_queue: OOM "
  392. "in mangle, dropping packet\n");
  393. return -ENOMEM;
  394. }
  395. kfree_skb(e->skb);
  396. e->skb = nskb;
  397. }
  398. skb_put(e->skb, diff);
  399. }
  400. if (!skb_make_writable(e->skb, data_len))
  401. return -ENOMEM;
  402. skb_copy_to_linear_data(e->skb, data, data_len);
  403. e->skb->ip_summed = CHECKSUM_NONE;
  404. return 0;
  405. }
  406. static int
  407. nfqnl_set_mode(struct nfqnl_instance *queue,
  408. unsigned char mode, unsigned int range)
  409. {
  410. int status = 0;
  411. spin_lock_bh(&queue->lock);
  412. switch (mode) {
  413. case NFQNL_COPY_NONE:
  414. case NFQNL_COPY_META:
  415. queue->copy_mode = mode;
  416. queue->copy_range = 0;
  417. break;
  418. case NFQNL_COPY_PACKET:
  419. queue->copy_mode = mode;
  420. /* we're using struct nlattr which has 16bit nla_len */
  421. if (range > 0xffff)
  422. queue->copy_range = 0xffff;
  423. else
  424. queue->copy_range = range;
  425. break;
  426. default:
  427. status = -EINVAL;
  428. }
  429. spin_unlock_bh(&queue->lock);
  430. return status;
  431. }
  432. static int
  433. dev_cmp(struct nf_queue_entry *entry, unsigned long ifindex)
  434. {
  435. if (entry->indev)
  436. if (entry->indev->ifindex == ifindex)
  437. return 1;
  438. if (entry->outdev)
  439. if (entry->outdev->ifindex == ifindex)
  440. return 1;
  441. #ifdef CONFIG_BRIDGE_NETFILTER
  442. if (entry->skb->nf_bridge) {
  443. if (entry->skb->nf_bridge->physindev &&
  444. entry->skb->nf_bridge->physindev->ifindex == ifindex)
  445. return 1;
  446. if (entry->skb->nf_bridge->physoutdev &&
  447. entry->skb->nf_bridge->physoutdev->ifindex == ifindex)
  448. return 1;
  449. }
  450. #endif
  451. return 0;
  452. }
  453. /* drop all packets with either indev or outdev == ifindex from all queue
  454. * instances */
  455. static void
  456. nfqnl_dev_drop(int ifindex)
  457. {
  458. int i;
  459. rcu_read_lock();
  460. for (i = 0; i < INSTANCE_BUCKETS; i++) {
  461. struct hlist_node *tmp;
  462. struct nfqnl_instance *inst;
  463. struct hlist_head *head = &instance_table[i];
  464. hlist_for_each_entry_rcu(inst, tmp, head, hlist)
  465. nfqnl_flush(inst, dev_cmp, ifindex);
  466. }
  467. rcu_read_unlock();
  468. }
  469. #define RCV_SKB_FAIL(err) do { netlink_ack(skb, nlh, (err)); return; } while (0)
  470. static int
  471. nfqnl_rcv_dev_event(struct notifier_block *this,
  472. unsigned long event, void *ptr)
  473. {
  474. struct net_device *dev = ptr;
  475. if (!net_eq(dev_net(dev), &init_net))
  476. return NOTIFY_DONE;
  477. /* Drop any packets associated with the downed device */
  478. if (event == NETDEV_DOWN)
  479. nfqnl_dev_drop(dev->ifindex);
  480. return NOTIFY_DONE;
  481. }
  482. static struct notifier_block nfqnl_dev_notifier = {
  483. .notifier_call = nfqnl_rcv_dev_event,
  484. };
  485. static int
  486. nfqnl_rcv_nl_event(struct notifier_block *this,
  487. unsigned long event, void *ptr)
  488. {
  489. struct netlink_notify *n = ptr;
  490. if (event == NETLINK_URELEASE && n->protocol == NETLINK_NETFILTER) {
  491. int i;
  492. /* destroy all instances for this pid */
  493. spin_lock(&instances_lock);
  494. for (i = 0; i < INSTANCE_BUCKETS; i++) {
  495. struct hlist_node *tmp, *t2;
  496. struct nfqnl_instance *inst;
  497. struct hlist_head *head = &instance_table[i];
  498. hlist_for_each_entry_safe(inst, tmp, t2, head, hlist) {
  499. if ((n->net == &init_net) &&
  500. (n->pid == inst->peer_pid))
  501. __instance_destroy(inst);
  502. }
  503. }
  504. spin_unlock(&instances_lock);
  505. }
  506. return NOTIFY_DONE;
  507. }
  508. static struct notifier_block nfqnl_rtnl_notifier = {
  509. .notifier_call = nfqnl_rcv_nl_event,
  510. };
  511. static const struct nla_policy nfqa_verdict_policy[NFQA_MAX+1] = {
  512. [NFQA_VERDICT_HDR] = { .len = sizeof(struct nfqnl_msg_verdict_hdr) },
  513. [NFQA_MARK] = { .type = NLA_U32 },
  514. [NFQA_PAYLOAD] = { .type = NLA_UNSPEC },
  515. };
  516. static int
  517. nfqnl_recv_verdict(struct sock *ctnl, struct sk_buff *skb,
  518. const struct nlmsghdr *nlh,
  519. const struct nlattr * const nfqa[])
  520. {
  521. struct nfgenmsg *nfmsg = NLMSG_DATA(nlh);
  522. u_int16_t queue_num = ntohs(nfmsg->res_id);
  523. struct nfqnl_msg_verdict_hdr *vhdr;
  524. struct nfqnl_instance *queue;
  525. unsigned int verdict;
  526. struct nf_queue_entry *entry;
  527. int err;
  528. rcu_read_lock();
  529. queue = instance_lookup(queue_num);
  530. if (!queue) {
  531. err = -ENODEV;
  532. goto err_out_unlock;
  533. }
  534. if (queue->peer_pid != NETLINK_CB(skb).pid) {
  535. err = -EPERM;
  536. goto err_out_unlock;
  537. }
  538. if (!nfqa[NFQA_VERDICT_HDR]) {
  539. err = -EINVAL;
  540. goto err_out_unlock;
  541. }
  542. vhdr = nla_data(nfqa[NFQA_VERDICT_HDR]);
  543. verdict = ntohl(vhdr->verdict);
  544. if ((verdict & NF_VERDICT_MASK) > NF_MAX_VERDICT) {
  545. err = -EINVAL;
  546. goto err_out_unlock;
  547. }
  548. entry = find_dequeue_entry(queue, ntohl(vhdr->id));
  549. if (entry == NULL) {
  550. err = -ENOENT;
  551. goto err_out_unlock;
  552. }
  553. rcu_read_unlock();
  554. if (nfqa[NFQA_PAYLOAD]) {
  555. if (nfqnl_mangle(nla_data(nfqa[NFQA_PAYLOAD]),
  556. nla_len(nfqa[NFQA_PAYLOAD]), entry) < 0)
  557. verdict = NF_DROP;
  558. }
  559. if (nfqa[NFQA_MARK])
  560. entry->skb->mark = ntohl(nla_get_be32(nfqa[NFQA_MARK]));
  561. nf_reinject(entry, verdict);
  562. return 0;
  563. err_out_unlock:
  564. rcu_read_unlock();
  565. return err;
  566. }
  567. static int
  568. nfqnl_recv_unsupp(struct sock *ctnl, struct sk_buff *skb,
  569. const struct nlmsghdr *nlh,
  570. const struct nlattr * const nfqa[])
  571. {
  572. return -ENOTSUPP;
  573. }
  574. static const struct nla_policy nfqa_cfg_policy[NFQA_CFG_MAX+1] = {
  575. [NFQA_CFG_CMD] = { .len = sizeof(struct nfqnl_msg_config_cmd) },
  576. [NFQA_CFG_PARAMS] = { .len = sizeof(struct nfqnl_msg_config_params) },
  577. };
  578. static const struct nf_queue_handler nfqh = {
  579. .name = "nf_queue",
  580. .outfn = &nfqnl_enqueue_packet,
  581. };
  582. static int
  583. nfqnl_recv_config(struct sock *ctnl, struct sk_buff *skb,
  584. const struct nlmsghdr *nlh,
  585. const struct nlattr * const nfqa[])
  586. {
  587. struct nfgenmsg *nfmsg = NLMSG_DATA(nlh);
  588. u_int16_t queue_num = ntohs(nfmsg->res_id);
  589. struct nfqnl_instance *queue;
  590. struct nfqnl_msg_config_cmd *cmd = NULL;
  591. int ret = 0;
  592. if (nfqa[NFQA_CFG_CMD]) {
  593. cmd = nla_data(nfqa[NFQA_CFG_CMD]);
  594. /* Commands without queue context - might sleep */
  595. switch (cmd->command) {
  596. case NFQNL_CFG_CMD_PF_BIND:
  597. return nf_register_queue_handler(ntohs(cmd->pf),
  598. &nfqh);
  599. case NFQNL_CFG_CMD_PF_UNBIND:
  600. return nf_unregister_queue_handler(ntohs(cmd->pf),
  601. &nfqh);
  602. }
  603. }
  604. rcu_read_lock();
  605. queue = instance_lookup(queue_num);
  606. if (queue && queue->peer_pid != NETLINK_CB(skb).pid) {
  607. ret = -EPERM;
  608. goto err_out_unlock;
  609. }
  610. if (cmd != NULL) {
  611. switch (cmd->command) {
  612. case NFQNL_CFG_CMD_BIND:
  613. if (queue) {
  614. ret = -EBUSY;
  615. goto err_out_unlock;
  616. }
  617. queue = instance_create(queue_num, NETLINK_CB(skb).pid);
  618. if (IS_ERR(queue)) {
  619. ret = PTR_ERR(queue);
  620. goto err_out_unlock;
  621. }
  622. break;
  623. case NFQNL_CFG_CMD_UNBIND:
  624. if (!queue) {
  625. ret = -ENODEV;
  626. goto err_out_unlock;
  627. }
  628. instance_destroy(queue);
  629. break;
  630. case NFQNL_CFG_CMD_PF_BIND:
  631. case NFQNL_CFG_CMD_PF_UNBIND:
  632. break;
  633. default:
  634. ret = -ENOTSUPP;
  635. break;
  636. }
  637. }
  638. if (nfqa[NFQA_CFG_PARAMS]) {
  639. struct nfqnl_msg_config_params *params;
  640. if (!queue) {
  641. ret = -ENODEV;
  642. goto err_out_unlock;
  643. }
  644. params = nla_data(nfqa[NFQA_CFG_PARAMS]);
  645. nfqnl_set_mode(queue, params->copy_mode,
  646. ntohl(params->copy_range));
  647. }
  648. if (nfqa[NFQA_CFG_QUEUE_MAXLEN]) {
  649. __be32 *queue_maxlen;
  650. if (!queue) {
  651. ret = -ENODEV;
  652. goto err_out_unlock;
  653. }
  654. queue_maxlen = nla_data(nfqa[NFQA_CFG_QUEUE_MAXLEN]);
  655. spin_lock_bh(&queue->lock);
  656. queue->queue_maxlen = ntohl(*queue_maxlen);
  657. spin_unlock_bh(&queue->lock);
  658. }
  659. err_out_unlock:
  660. rcu_read_unlock();
  661. return ret;
  662. }
  663. static const struct nfnl_callback nfqnl_cb[NFQNL_MSG_MAX] = {
  664. [NFQNL_MSG_PACKET] = { .call = nfqnl_recv_unsupp,
  665. .attr_count = NFQA_MAX, },
  666. [NFQNL_MSG_VERDICT] = { .call = nfqnl_recv_verdict,
  667. .attr_count = NFQA_MAX,
  668. .policy = nfqa_verdict_policy },
  669. [NFQNL_MSG_CONFIG] = { .call = nfqnl_recv_config,
  670. .attr_count = NFQA_CFG_MAX,
  671. .policy = nfqa_cfg_policy },
  672. };
  673. static const struct nfnetlink_subsystem nfqnl_subsys = {
  674. .name = "nf_queue",
  675. .subsys_id = NFNL_SUBSYS_QUEUE,
  676. .cb_count = NFQNL_MSG_MAX,
  677. .cb = nfqnl_cb,
  678. };
  679. #ifdef CONFIG_PROC_FS
  680. struct iter_state {
  681. unsigned int bucket;
  682. };
  683. static struct hlist_node *get_first(struct seq_file *seq)
  684. {
  685. struct iter_state *st = seq->private;
  686. if (!st)
  687. return NULL;
  688. for (st->bucket = 0; st->bucket < INSTANCE_BUCKETS; st->bucket++) {
  689. if (!hlist_empty(&instance_table[st->bucket]))
  690. return instance_table[st->bucket].first;
  691. }
  692. return NULL;
  693. }
  694. static struct hlist_node *get_next(struct seq_file *seq, struct hlist_node *h)
  695. {
  696. struct iter_state *st = seq->private;
  697. h = h->next;
  698. while (!h) {
  699. if (++st->bucket >= INSTANCE_BUCKETS)
  700. return NULL;
  701. h = instance_table[st->bucket].first;
  702. }
  703. return h;
  704. }
  705. static struct hlist_node *get_idx(struct seq_file *seq, loff_t pos)
  706. {
  707. struct hlist_node *head;
  708. head = get_first(seq);
  709. if (head)
  710. while (pos && (head = get_next(seq, head)))
  711. pos--;
  712. return pos ? NULL : head;
  713. }
  714. static void *seq_start(struct seq_file *seq, loff_t *pos)
  715. __acquires(instances_lock)
  716. {
  717. spin_lock(&instances_lock);
  718. return get_idx(seq, *pos);
  719. }
  720. static void *seq_next(struct seq_file *s, void *v, loff_t *pos)
  721. {
  722. (*pos)++;
  723. return get_next(s, v);
  724. }
  725. static void seq_stop(struct seq_file *s, void *v)
  726. __releases(instances_lock)
  727. {
  728. spin_unlock(&instances_lock);
  729. }
  730. static int seq_show(struct seq_file *s, void *v)
  731. {
  732. const struct nfqnl_instance *inst = v;
  733. return seq_printf(s, "%5d %6d %5d %1d %5d %5d %5d %8d %2d\n",
  734. inst->queue_num,
  735. inst->peer_pid, inst->queue_total,
  736. inst->copy_mode, inst->copy_range,
  737. inst->queue_dropped, inst->queue_user_dropped,
  738. atomic_read(&inst->id_sequence), 1);
  739. }
  740. static const struct seq_operations nfqnl_seq_ops = {
  741. .start = seq_start,
  742. .next = seq_next,
  743. .stop = seq_stop,
  744. .show = seq_show,
  745. };
  746. static int nfqnl_open(struct inode *inode, struct file *file)
  747. {
  748. return seq_open_private(file, &nfqnl_seq_ops,
  749. sizeof(struct iter_state));
  750. }
  751. static const struct file_operations nfqnl_file_ops = {
  752. .owner = THIS_MODULE,
  753. .open = nfqnl_open,
  754. .read = seq_read,
  755. .llseek = seq_lseek,
  756. .release = seq_release_private,
  757. };
  758. #endif /* PROC_FS */
  759. static int __init nfnetlink_queue_init(void)
  760. {
  761. int i, status = -ENOMEM;
  762. for (i = 0; i < INSTANCE_BUCKETS; i++)
  763. INIT_HLIST_HEAD(&instance_table[i]);
  764. netlink_register_notifier(&nfqnl_rtnl_notifier);
  765. status = nfnetlink_subsys_register(&nfqnl_subsys);
  766. if (status < 0) {
  767. printk(KERN_ERR "nf_queue: failed to create netlink socket\n");
  768. goto cleanup_netlink_notifier;
  769. }
  770. #ifdef CONFIG_PROC_FS
  771. if (!proc_create("nfnetlink_queue", 0440,
  772. proc_net_netfilter, &nfqnl_file_ops))
  773. goto cleanup_subsys;
  774. #endif
  775. register_netdevice_notifier(&nfqnl_dev_notifier);
  776. return status;
  777. #ifdef CONFIG_PROC_FS
  778. cleanup_subsys:
  779. nfnetlink_subsys_unregister(&nfqnl_subsys);
  780. #endif
  781. cleanup_netlink_notifier:
  782. netlink_unregister_notifier(&nfqnl_rtnl_notifier);
  783. return status;
  784. }
  785. static void __exit nfnetlink_queue_fini(void)
  786. {
  787. nf_unregister_queue_handlers(&nfqh);
  788. unregister_netdevice_notifier(&nfqnl_dev_notifier);
  789. #ifdef CONFIG_PROC_FS
  790. remove_proc_entry("nfnetlink_queue", proc_net_netfilter);
  791. #endif
  792. nfnetlink_subsys_unregister(&nfqnl_subsys);
  793. netlink_unregister_notifier(&nfqnl_rtnl_notifier);
  794. rcu_barrier(); /* Wait for completion of call_rcu()'s */
  795. }
  796. MODULE_DESCRIPTION("netfilter packet queue handler");
  797. MODULE_AUTHOR("Harald Welte <laforge@netfilter.org>");
  798. MODULE_LICENSE("GPL");
  799. MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_QUEUE);
  800. module_init(nfnetlink_queue_init);
  801. module_exit(nfnetlink_queue_fini);