nfnetlink_queue.c 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144
  1. /*
  2. * This is a module which is used for queueing packets and communicating with
  3. * userspace via nfetlink.
  4. *
  5. * (C) 2005 by Harald Welte <laforge@netfilter.org>
  6. *
  7. * Based on the old ipv4-only ip_queue.c:
  8. * (C) 2000-2002 James Morris <jmorris@intercode.com.au>
  9. * (C) 2003-2005 Netfilter Core Team <coreteam@netfilter.org>
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License version 2 as
  13. * published by the Free Software Foundation.
  14. *
  15. */
  16. #include <linux/module.h>
  17. #include <linux/skbuff.h>
  18. #include <linux/init.h>
  19. #include <linux/spinlock.h>
  20. #include <linux/notifier.h>
  21. #include <linux/netdevice.h>
  22. #include <linux/netfilter.h>
  23. #include <linux/proc_fs.h>
  24. #include <linux/netfilter_ipv4.h>
  25. #include <linux/netfilter_ipv6.h>
  26. #include <linux/netfilter/nfnetlink.h>
  27. #include <linux/netfilter/nfnetlink_queue.h>
  28. #include <linux/list.h>
  29. #include <net/sock.h>
  30. #include <asm/atomic.h>
  31. #ifdef CONFIG_BRIDGE_NETFILTER
  32. #include "../bridge/br_private.h"
  33. #endif
  34. #define NFQNL_QMAX_DEFAULT 1024
  35. #if 0
  36. #define QDEBUG(x, args ...) printk(KERN_DEBUG "%s(%d):%s(): " x, \
  37. __FILE__, __LINE__, __FUNCTION__, \
  38. ## args)
  39. #else
  40. #define QDEBUG(x, ...)
  41. #endif
  42. struct nfqnl_queue_entry {
  43. struct list_head list;
  44. struct nf_info *info;
  45. struct sk_buff *skb;
  46. unsigned int id;
  47. };
  48. struct nfqnl_instance {
  49. struct hlist_node hlist; /* global list of queues */
  50. atomic_t use;
  51. int peer_pid;
  52. unsigned int queue_maxlen;
  53. unsigned int copy_range;
  54. unsigned int queue_total;
  55. unsigned int queue_dropped;
  56. unsigned int queue_user_dropped;
  57. atomic_t id_sequence; /* 'sequence' of pkt ids */
  58. u_int16_t queue_num; /* number of this queue */
  59. u_int8_t copy_mode;
  60. spinlock_t lock;
  61. struct list_head queue_list; /* packets in queue */
  62. };
  63. typedef int (*nfqnl_cmpfn)(struct nfqnl_queue_entry *, unsigned long);
  64. static DEFINE_RWLOCK(instances_lock);
  65. #define INSTANCE_BUCKETS 16
  66. static struct hlist_head instance_table[INSTANCE_BUCKETS];
  67. static inline u_int8_t instance_hashfn(u_int16_t queue_num)
  68. {
  69. return ((queue_num >> 8) | queue_num) % INSTANCE_BUCKETS;
  70. }
  71. static struct nfqnl_instance *
  72. __instance_lookup(u_int16_t queue_num)
  73. {
  74. struct hlist_head *head;
  75. struct hlist_node *pos;
  76. struct nfqnl_instance *inst;
  77. head = &instance_table[instance_hashfn(queue_num)];
  78. hlist_for_each_entry(inst, pos, head, hlist) {
  79. if (inst->queue_num == queue_num)
  80. return inst;
  81. }
  82. return NULL;
  83. }
  84. static struct nfqnl_instance *
  85. instance_lookup_get(u_int16_t queue_num)
  86. {
  87. struct nfqnl_instance *inst;
  88. read_lock_bh(&instances_lock);
  89. inst = __instance_lookup(queue_num);
  90. if (inst)
  91. atomic_inc(&inst->use);
  92. read_unlock_bh(&instances_lock);
  93. return inst;
  94. }
  95. static void
  96. instance_put(struct nfqnl_instance *inst)
  97. {
  98. if (inst && atomic_dec_and_test(&inst->use)) {
  99. QDEBUG("kfree(inst=%p)\n", inst);
  100. kfree(inst);
  101. }
  102. }
  103. static struct nfqnl_instance *
  104. instance_create(u_int16_t queue_num, int pid)
  105. {
  106. struct nfqnl_instance *inst;
  107. QDEBUG("entering for queue_num=%u, pid=%d\n", queue_num, pid);
  108. write_lock_bh(&instances_lock);
  109. if (__instance_lookup(queue_num)) {
  110. inst = NULL;
  111. QDEBUG("aborting, instance already exists\n");
  112. goto out_unlock;
  113. }
  114. inst = kzalloc(sizeof(*inst), GFP_ATOMIC);
  115. if (!inst)
  116. goto out_unlock;
  117. inst->queue_num = queue_num;
  118. inst->peer_pid = pid;
  119. inst->queue_maxlen = NFQNL_QMAX_DEFAULT;
  120. inst->copy_range = 0xfffff;
  121. inst->copy_mode = NFQNL_COPY_NONE;
  122. atomic_set(&inst->id_sequence, 0);
  123. /* needs to be two, since we _put() after creation */
  124. atomic_set(&inst->use, 2);
  125. spin_lock_init(&inst->lock);
  126. INIT_LIST_HEAD(&inst->queue_list);
  127. if (!try_module_get(THIS_MODULE))
  128. goto out_free;
  129. hlist_add_head(&inst->hlist,
  130. &instance_table[instance_hashfn(queue_num)]);
  131. write_unlock_bh(&instances_lock);
  132. QDEBUG("successfully created new instance\n");
  133. return inst;
  134. out_free:
  135. kfree(inst);
  136. out_unlock:
  137. write_unlock_bh(&instances_lock);
  138. return NULL;
  139. }
  140. static void nfqnl_flush(struct nfqnl_instance *queue, int verdict);
  141. static void
  142. _instance_destroy2(struct nfqnl_instance *inst, int lock)
  143. {
  144. /* first pull it out of the global list */
  145. if (lock)
  146. write_lock_bh(&instances_lock);
  147. QDEBUG("removing instance %p (queuenum=%u) from hash\n",
  148. inst, inst->queue_num);
  149. hlist_del(&inst->hlist);
  150. if (lock)
  151. write_unlock_bh(&instances_lock);
  152. /* then flush all pending skbs from the queue */
  153. nfqnl_flush(inst, NF_DROP);
  154. /* and finally put the refcount */
  155. instance_put(inst);
  156. module_put(THIS_MODULE);
  157. }
  158. static inline void
  159. __instance_destroy(struct nfqnl_instance *inst)
  160. {
  161. _instance_destroy2(inst, 0);
  162. }
  163. static inline void
  164. instance_destroy(struct nfqnl_instance *inst)
  165. {
  166. _instance_destroy2(inst, 1);
  167. }
  168. static void
  169. issue_verdict(struct nfqnl_queue_entry *entry, int verdict)
  170. {
  171. QDEBUG("entering for entry %p, verdict %u\n", entry, verdict);
  172. /* TCP input path (and probably other bits) assume to be called
  173. * from softirq context, not from syscall, like issue_verdict is
  174. * called. TCP input path deadlocks with locks taken from timer
  175. * softirq, e.g. We therefore emulate this by local_bh_disable() */
  176. local_bh_disable();
  177. nf_reinject(entry->skb, entry->info, verdict);
  178. local_bh_enable();
  179. kfree(entry);
  180. }
  181. static inline void
  182. __enqueue_entry(struct nfqnl_instance *queue,
  183. struct nfqnl_queue_entry *entry)
  184. {
  185. list_add(&entry->list, &queue->queue_list);
  186. queue->queue_total++;
  187. }
  188. /*
  189. * Find and return a queued entry matched by cmpfn, or return the last
  190. * entry if cmpfn is NULL.
  191. */
  192. static inline struct nfqnl_queue_entry *
  193. __find_entry(struct nfqnl_instance *queue, nfqnl_cmpfn cmpfn,
  194. unsigned long data)
  195. {
  196. struct list_head *p;
  197. list_for_each_prev(p, &queue->queue_list) {
  198. struct nfqnl_queue_entry *entry = (struct nfqnl_queue_entry *)p;
  199. if (!cmpfn || cmpfn(entry, data))
  200. return entry;
  201. }
  202. return NULL;
  203. }
  204. static inline void
  205. __dequeue_entry(struct nfqnl_instance *q, struct nfqnl_queue_entry *entry)
  206. {
  207. list_del(&entry->list);
  208. q->queue_total--;
  209. }
  210. static inline struct nfqnl_queue_entry *
  211. __find_dequeue_entry(struct nfqnl_instance *queue,
  212. nfqnl_cmpfn cmpfn, unsigned long data)
  213. {
  214. struct nfqnl_queue_entry *entry;
  215. entry = __find_entry(queue, cmpfn, data);
  216. if (entry == NULL)
  217. return NULL;
  218. __dequeue_entry(queue, entry);
  219. return entry;
  220. }
  221. static inline void
  222. __nfqnl_flush(struct nfqnl_instance *queue, int verdict)
  223. {
  224. struct nfqnl_queue_entry *entry;
  225. while ((entry = __find_dequeue_entry(queue, NULL, 0)))
  226. issue_verdict(entry, verdict);
  227. }
  228. static inline int
  229. __nfqnl_set_mode(struct nfqnl_instance *queue,
  230. unsigned char mode, unsigned int range)
  231. {
  232. int status = 0;
  233. switch (mode) {
  234. case NFQNL_COPY_NONE:
  235. case NFQNL_COPY_META:
  236. queue->copy_mode = mode;
  237. queue->copy_range = 0;
  238. break;
  239. case NFQNL_COPY_PACKET:
  240. queue->copy_mode = mode;
  241. /* we're using struct nfattr which has 16bit nfa_len */
  242. if (range > 0xffff)
  243. queue->copy_range = 0xffff;
  244. else
  245. queue->copy_range = range;
  246. break;
  247. default:
  248. status = -EINVAL;
  249. }
  250. return status;
  251. }
  252. static struct nfqnl_queue_entry *
  253. find_dequeue_entry(struct nfqnl_instance *queue,
  254. nfqnl_cmpfn cmpfn, unsigned long data)
  255. {
  256. struct nfqnl_queue_entry *entry;
  257. spin_lock_bh(&queue->lock);
  258. entry = __find_dequeue_entry(queue, cmpfn, data);
  259. spin_unlock_bh(&queue->lock);
  260. return entry;
  261. }
  262. static void
  263. nfqnl_flush(struct nfqnl_instance *queue, int verdict)
  264. {
  265. spin_lock_bh(&queue->lock);
  266. __nfqnl_flush(queue, verdict);
  267. spin_unlock_bh(&queue->lock);
  268. }
  269. static struct sk_buff *
  270. nfqnl_build_packet_message(struct nfqnl_instance *queue,
  271. struct nfqnl_queue_entry *entry, int *errp)
  272. {
  273. unsigned char *old_tail;
  274. size_t size;
  275. size_t data_len = 0;
  276. struct sk_buff *skb;
  277. struct nfqnl_msg_packet_hdr pmsg;
  278. struct nlmsghdr *nlh;
  279. struct nfgenmsg *nfmsg;
  280. struct nf_info *entinf = entry->info;
  281. struct sk_buff *entskb = entry->skb;
  282. struct net_device *indev;
  283. struct net_device *outdev;
  284. __be32 tmp_uint;
  285. QDEBUG("entered\n");
  286. /* all macros expand to constant values at compile time */
  287. size = NLMSG_SPACE(sizeof(struct nfgenmsg)) +
  288. + NFA_SPACE(sizeof(struct nfqnl_msg_packet_hdr))
  289. + NFA_SPACE(sizeof(u_int32_t)) /* ifindex */
  290. + NFA_SPACE(sizeof(u_int32_t)) /* ifindex */
  291. #ifdef CONFIG_BRIDGE_NETFILTER
  292. + NFA_SPACE(sizeof(u_int32_t)) /* ifindex */
  293. + NFA_SPACE(sizeof(u_int32_t)) /* ifindex */
  294. #endif
  295. + NFA_SPACE(sizeof(u_int32_t)) /* mark */
  296. + NFA_SPACE(sizeof(struct nfqnl_msg_packet_hw))
  297. + NFA_SPACE(sizeof(struct nfqnl_msg_packet_timestamp));
  298. outdev = entinf->outdev;
  299. spin_lock_bh(&queue->lock);
  300. switch (queue->copy_mode) {
  301. case NFQNL_COPY_META:
  302. case NFQNL_COPY_NONE:
  303. data_len = 0;
  304. break;
  305. case NFQNL_COPY_PACKET:
  306. if ((entskb->ip_summed == CHECKSUM_PARTIAL ||
  307. entskb->ip_summed == CHECKSUM_COMPLETE) &&
  308. (*errp = skb_checksum_help(entskb))) {
  309. spin_unlock_bh(&queue->lock);
  310. return NULL;
  311. }
  312. if (queue->copy_range == 0
  313. || queue->copy_range > entskb->len)
  314. data_len = entskb->len;
  315. else
  316. data_len = queue->copy_range;
  317. size += NFA_SPACE(data_len);
  318. break;
  319. default:
  320. *errp = -EINVAL;
  321. spin_unlock_bh(&queue->lock);
  322. return NULL;
  323. }
  324. spin_unlock_bh(&queue->lock);
  325. skb = alloc_skb(size, GFP_ATOMIC);
  326. if (!skb)
  327. goto nlmsg_failure;
  328. old_tail= skb->tail;
  329. nlh = NLMSG_PUT(skb, 0, 0,
  330. NFNL_SUBSYS_QUEUE << 8 | NFQNL_MSG_PACKET,
  331. sizeof(struct nfgenmsg));
  332. nfmsg = NLMSG_DATA(nlh);
  333. nfmsg->nfgen_family = entinf->pf;
  334. nfmsg->version = NFNETLINK_V0;
  335. nfmsg->res_id = htons(queue->queue_num);
  336. pmsg.packet_id = htonl(entry->id);
  337. pmsg.hw_protocol = entskb->protocol;
  338. pmsg.hook = entinf->hook;
  339. NFA_PUT(skb, NFQA_PACKET_HDR, sizeof(pmsg), &pmsg);
  340. indev = entinf->indev;
  341. if (indev) {
  342. tmp_uint = htonl(indev->ifindex);
  343. #ifndef CONFIG_BRIDGE_NETFILTER
  344. NFA_PUT(skb, NFQA_IFINDEX_INDEV, sizeof(tmp_uint), &tmp_uint);
  345. #else
  346. if (entinf->pf == PF_BRIDGE) {
  347. /* Case 1: indev is physical input device, we need to
  348. * look for bridge group (when called from
  349. * netfilter_bridge) */
  350. NFA_PUT(skb, NFQA_IFINDEX_PHYSINDEV, sizeof(tmp_uint),
  351. &tmp_uint);
  352. /* this is the bridge group "brX" */
  353. tmp_uint = htonl(indev->br_port->br->dev->ifindex);
  354. NFA_PUT(skb, NFQA_IFINDEX_INDEV, sizeof(tmp_uint),
  355. &tmp_uint);
  356. } else {
  357. /* Case 2: indev is bridge group, we need to look for
  358. * physical device (when called from ipv4) */
  359. NFA_PUT(skb, NFQA_IFINDEX_INDEV, sizeof(tmp_uint),
  360. &tmp_uint);
  361. if (entskb->nf_bridge
  362. && entskb->nf_bridge->physindev) {
  363. tmp_uint = htonl(entskb->nf_bridge->physindev->ifindex);
  364. NFA_PUT(skb, NFQA_IFINDEX_PHYSINDEV,
  365. sizeof(tmp_uint), &tmp_uint);
  366. }
  367. }
  368. #endif
  369. }
  370. if (outdev) {
  371. tmp_uint = htonl(outdev->ifindex);
  372. #ifndef CONFIG_BRIDGE_NETFILTER
  373. NFA_PUT(skb, NFQA_IFINDEX_OUTDEV, sizeof(tmp_uint), &tmp_uint);
  374. #else
  375. if (entinf->pf == PF_BRIDGE) {
  376. /* Case 1: outdev is physical output device, we need to
  377. * look for bridge group (when called from
  378. * netfilter_bridge) */
  379. NFA_PUT(skb, NFQA_IFINDEX_PHYSOUTDEV, sizeof(tmp_uint),
  380. &tmp_uint);
  381. /* this is the bridge group "brX" */
  382. tmp_uint = htonl(outdev->br_port->br->dev->ifindex);
  383. NFA_PUT(skb, NFQA_IFINDEX_OUTDEV, sizeof(tmp_uint),
  384. &tmp_uint);
  385. } else {
  386. /* Case 2: outdev is bridge group, we need to look for
  387. * physical output device (when called from ipv4) */
  388. NFA_PUT(skb, NFQA_IFINDEX_OUTDEV, sizeof(tmp_uint),
  389. &tmp_uint);
  390. if (entskb->nf_bridge
  391. && entskb->nf_bridge->physoutdev) {
  392. tmp_uint = htonl(entskb->nf_bridge->physoutdev->ifindex);
  393. NFA_PUT(skb, NFQA_IFINDEX_PHYSOUTDEV,
  394. sizeof(tmp_uint), &tmp_uint);
  395. }
  396. }
  397. #endif
  398. }
  399. if (entskb->mark) {
  400. tmp_uint = htonl(entskb->mark);
  401. NFA_PUT(skb, NFQA_MARK, sizeof(u_int32_t), &tmp_uint);
  402. }
  403. if (indev && entskb->dev
  404. && entskb->dev->hard_header_parse) {
  405. struct nfqnl_msg_packet_hw phw;
  406. int len = entskb->dev->hard_header_parse(entskb,
  407. phw.hw_addr);
  408. phw.hw_addrlen = htons(len);
  409. NFA_PUT(skb, NFQA_HWADDR, sizeof(phw), &phw);
  410. }
  411. if (entskb->tstamp.off_sec) {
  412. struct nfqnl_msg_packet_timestamp ts;
  413. ts.sec = cpu_to_be64(entskb->tstamp.off_sec);
  414. ts.usec = cpu_to_be64(entskb->tstamp.off_usec);
  415. NFA_PUT(skb, NFQA_TIMESTAMP, sizeof(ts), &ts);
  416. }
  417. if (data_len) {
  418. struct nfattr *nfa;
  419. int size = NFA_LENGTH(data_len);
  420. if (skb_tailroom(skb) < (int)NFA_SPACE(data_len)) {
  421. printk(KERN_WARNING "nf_queue: no tailroom!\n");
  422. goto nlmsg_failure;
  423. }
  424. nfa = (struct nfattr *)skb_put(skb, NFA_ALIGN(size));
  425. nfa->nfa_type = NFQA_PAYLOAD;
  426. nfa->nfa_len = size;
  427. if (skb_copy_bits(entskb, 0, NFA_DATA(nfa), data_len))
  428. BUG();
  429. }
  430. nlh->nlmsg_len = skb->tail - old_tail;
  431. return skb;
  432. nlmsg_failure:
  433. nfattr_failure:
  434. if (skb)
  435. kfree_skb(skb);
  436. *errp = -EINVAL;
  437. if (net_ratelimit())
  438. printk(KERN_ERR "nf_queue: error creating packet message\n");
  439. return NULL;
  440. }
  441. static int
  442. nfqnl_enqueue_packet(struct sk_buff *skb, struct nf_info *info,
  443. unsigned int queuenum, void *data)
  444. {
  445. int status = -EINVAL;
  446. struct sk_buff *nskb;
  447. struct nfqnl_instance *queue;
  448. struct nfqnl_queue_entry *entry;
  449. QDEBUG("entered\n");
  450. queue = instance_lookup_get(queuenum);
  451. if (!queue) {
  452. QDEBUG("no queue instance matching\n");
  453. return -EINVAL;
  454. }
  455. if (queue->copy_mode == NFQNL_COPY_NONE) {
  456. QDEBUG("mode COPY_NONE, aborting\n");
  457. status = -EAGAIN;
  458. goto err_out_put;
  459. }
  460. entry = kmalloc(sizeof(*entry), GFP_ATOMIC);
  461. if (entry == NULL) {
  462. if (net_ratelimit())
  463. printk(KERN_ERR
  464. "nf_queue: OOM in nfqnl_enqueue_packet()\n");
  465. status = -ENOMEM;
  466. goto err_out_put;
  467. }
  468. entry->info = info;
  469. entry->skb = skb;
  470. entry->id = atomic_inc_return(&queue->id_sequence);
  471. nskb = nfqnl_build_packet_message(queue, entry, &status);
  472. if (nskb == NULL)
  473. goto err_out_free;
  474. spin_lock_bh(&queue->lock);
  475. if (!queue->peer_pid)
  476. goto err_out_free_nskb;
  477. if (queue->queue_total >= queue->queue_maxlen) {
  478. queue->queue_dropped++;
  479. status = -ENOSPC;
  480. if (net_ratelimit())
  481. printk(KERN_WARNING "nf_queue: full at %d entries, "
  482. "dropping packets(s). Dropped: %d\n",
  483. queue->queue_total, queue->queue_dropped);
  484. goto err_out_free_nskb;
  485. }
  486. /* nfnetlink_unicast will either free the nskb or add it to a socket */
  487. status = nfnetlink_unicast(nskb, queue->peer_pid, MSG_DONTWAIT);
  488. if (status < 0) {
  489. queue->queue_user_dropped++;
  490. goto err_out_unlock;
  491. }
  492. __enqueue_entry(queue, entry);
  493. spin_unlock_bh(&queue->lock);
  494. instance_put(queue);
  495. return status;
  496. err_out_free_nskb:
  497. kfree_skb(nskb);
  498. err_out_unlock:
  499. spin_unlock_bh(&queue->lock);
  500. err_out_free:
  501. kfree(entry);
  502. err_out_put:
  503. instance_put(queue);
  504. return status;
  505. }
  506. static int
  507. nfqnl_mangle(void *data, int data_len, struct nfqnl_queue_entry *e)
  508. {
  509. int diff;
  510. diff = data_len - e->skb->len;
  511. if (diff < 0) {
  512. if (pskb_trim(e->skb, data_len))
  513. return -ENOMEM;
  514. } else if (diff > 0) {
  515. if (data_len > 0xFFFF)
  516. return -EINVAL;
  517. if (diff > skb_tailroom(e->skb)) {
  518. struct sk_buff *newskb;
  519. newskb = skb_copy_expand(e->skb,
  520. skb_headroom(e->skb),
  521. diff,
  522. GFP_ATOMIC);
  523. if (newskb == NULL) {
  524. printk(KERN_WARNING "nf_queue: OOM "
  525. "in mangle, dropping packet\n");
  526. return -ENOMEM;
  527. }
  528. if (e->skb->sk)
  529. skb_set_owner_w(newskb, e->skb->sk);
  530. kfree_skb(e->skb);
  531. e->skb = newskb;
  532. }
  533. skb_put(e->skb, diff);
  534. }
  535. if (!skb_make_writable(&e->skb, data_len))
  536. return -ENOMEM;
  537. memcpy(e->skb->data, data, data_len);
  538. e->skb->ip_summed = CHECKSUM_NONE;
  539. return 0;
  540. }
  541. static inline int
  542. id_cmp(struct nfqnl_queue_entry *e, unsigned long id)
  543. {
  544. return (id == e->id);
  545. }
  546. static int
  547. nfqnl_set_mode(struct nfqnl_instance *queue,
  548. unsigned char mode, unsigned int range)
  549. {
  550. int status;
  551. spin_lock_bh(&queue->lock);
  552. status = __nfqnl_set_mode(queue, mode, range);
  553. spin_unlock_bh(&queue->lock);
  554. return status;
  555. }
  556. static int
  557. dev_cmp(struct nfqnl_queue_entry *entry, unsigned long ifindex)
  558. {
  559. struct nf_info *entinf = entry->info;
  560. if (entinf->indev)
  561. if (entinf->indev->ifindex == ifindex)
  562. return 1;
  563. if (entinf->outdev)
  564. if (entinf->outdev->ifindex == ifindex)
  565. return 1;
  566. #ifdef CONFIG_BRIDGE_NETFILTER
  567. if (entry->skb->nf_bridge) {
  568. if (entry->skb->nf_bridge->physindev &&
  569. entry->skb->nf_bridge->physindev->ifindex == ifindex)
  570. return 1;
  571. if (entry->skb->nf_bridge->physoutdev &&
  572. entry->skb->nf_bridge->physoutdev->ifindex == ifindex)
  573. return 1;
  574. }
  575. #endif
  576. return 0;
  577. }
  578. /* drop all packets with either indev or outdev == ifindex from all queue
  579. * instances */
  580. static void
  581. nfqnl_dev_drop(int ifindex)
  582. {
  583. int i;
  584. QDEBUG("entering for ifindex %u\n", ifindex);
  585. /* this only looks like we have to hold the readlock for a way too long
  586. * time, issue_verdict(), nf_reinject(), ... - but we always only
  587. * issue NF_DROP, which is processed directly in nf_reinject() */
  588. read_lock_bh(&instances_lock);
  589. for (i = 0; i < INSTANCE_BUCKETS; i++) {
  590. struct hlist_node *tmp;
  591. struct nfqnl_instance *inst;
  592. struct hlist_head *head = &instance_table[i];
  593. hlist_for_each_entry(inst, tmp, head, hlist) {
  594. struct nfqnl_queue_entry *entry;
  595. while ((entry = find_dequeue_entry(inst, dev_cmp,
  596. ifindex)) != NULL)
  597. issue_verdict(entry, NF_DROP);
  598. }
  599. }
  600. read_unlock_bh(&instances_lock);
  601. }
  602. #define RCV_SKB_FAIL(err) do { netlink_ack(skb, nlh, (err)); return; } while (0)
  603. static int
  604. nfqnl_rcv_dev_event(struct notifier_block *this,
  605. unsigned long event, void *ptr)
  606. {
  607. struct net_device *dev = ptr;
  608. /* Drop any packets associated with the downed device */
  609. if (event == NETDEV_DOWN)
  610. nfqnl_dev_drop(dev->ifindex);
  611. return NOTIFY_DONE;
  612. }
  613. static struct notifier_block nfqnl_dev_notifier = {
  614. .notifier_call = nfqnl_rcv_dev_event,
  615. };
  616. static int
  617. nfqnl_rcv_nl_event(struct notifier_block *this,
  618. unsigned long event, void *ptr)
  619. {
  620. struct netlink_notify *n = ptr;
  621. if (event == NETLINK_URELEASE &&
  622. n->protocol == NETLINK_NETFILTER && n->pid) {
  623. int i;
  624. /* destroy all instances for this pid */
  625. write_lock_bh(&instances_lock);
  626. for (i = 0; i < INSTANCE_BUCKETS; i++) {
  627. struct hlist_node *tmp, *t2;
  628. struct nfqnl_instance *inst;
  629. struct hlist_head *head = &instance_table[i];
  630. hlist_for_each_entry_safe(inst, tmp, t2, head, hlist) {
  631. if (n->pid == inst->peer_pid)
  632. __instance_destroy(inst);
  633. }
  634. }
  635. write_unlock_bh(&instances_lock);
  636. }
  637. return NOTIFY_DONE;
  638. }
  639. static struct notifier_block nfqnl_rtnl_notifier = {
  640. .notifier_call = nfqnl_rcv_nl_event,
  641. };
  642. static const int nfqa_verdict_min[NFQA_MAX] = {
  643. [NFQA_VERDICT_HDR-1] = sizeof(struct nfqnl_msg_verdict_hdr),
  644. [NFQA_MARK-1] = sizeof(u_int32_t),
  645. [NFQA_PAYLOAD-1] = 0,
  646. };
  647. static int
  648. nfqnl_recv_verdict(struct sock *ctnl, struct sk_buff *skb,
  649. struct nlmsghdr *nlh, struct nfattr *nfqa[], int *errp)
  650. {
  651. struct nfgenmsg *nfmsg = NLMSG_DATA(nlh);
  652. u_int16_t queue_num = ntohs(nfmsg->res_id);
  653. struct nfqnl_msg_verdict_hdr *vhdr;
  654. struct nfqnl_instance *queue;
  655. unsigned int verdict;
  656. struct nfqnl_queue_entry *entry;
  657. int err;
  658. if (nfattr_bad_size(nfqa, NFQA_MAX, nfqa_verdict_min)) {
  659. QDEBUG("bad attribute size\n");
  660. return -EINVAL;
  661. }
  662. queue = instance_lookup_get(queue_num);
  663. if (!queue)
  664. return -ENODEV;
  665. if (queue->peer_pid != NETLINK_CB(skb).pid) {
  666. err = -EPERM;
  667. goto err_out_put;
  668. }
  669. if (!nfqa[NFQA_VERDICT_HDR-1]) {
  670. err = -EINVAL;
  671. goto err_out_put;
  672. }
  673. vhdr = NFA_DATA(nfqa[NFQA_VERDICT_HDR-1]);
  674. verdict = ntohl(vhdr->verdict);
  675. if ((verdict & NF_VERDICT_MASK) > NF_MAX_VERDICT) {
  676. err = -EINVAL;
  677. goto err_out_put;
  678. }
  679. entry = find_dequeue_entry(queue, id_cmp, ntohl(vhdr->id));
  680. if (entry == NULL) {
  681. err = -ENOENT;
  682. goto err_out_put;
  683. }
  684. if (nfqa[NFQA_PAYLOAD-1]) {
  685. if (nfqnl_mangle(NFA_DATA(nfqa[NFQA_PAYLOAD-1]),
  686. NFA_PAYLOAD(nfqa[NFQA_PAYLOAD-1]), entry) < 0)
  687. verdict = NF_DROP;
  688. }
  689. if (nfqa[NFQA_MARK-1])
  690. entry->skb->mark = ntohl(*(__be32 *)
  691. NFA_DATA(nfqa[NFQA_MARK-1]));
  692. issue_verdict(entry, verdict);
  693. instance_put(queue);
  694. return 0;
  695. err_out_put:
  696. instance_put(queue);
  697. return err;
  698. }
  699. static int
  700. nfqnl_recv_unsupp(struct sock *ctnl, struct sk_buff *skb,
  701. struct nlmsghdr *nlh, struct nfattr *nfqa[], int *errp)
  702. {
  703. return -ENOTSUPP;
  704. }
  705. static const int nfqa_cfg_min[NFQA_CFG_MAX] = {
  706. [NFQA_CFG_CMD-1] = sizeof(struct nfqnl_msg_config_cmd),
  707. [NFQA_CFG_PARAMS-1] = sizeof(struct nfqnl_msg_config_params),
  708. };
  709. static struct nf_queue_handler nfqh = {
  710. .name = "nf_queue",
  711. .outfn = &nfqnl_enqueue_packet,
  712. };
  713. static int
  714. nfqnl_recv_config(struct sock *ctnl, struct sk_buff *skb,
  715. struct nlmsghdr *nlh, struct nfattr *nfqa[], int *errp)
  716. {
  717. struct nfgenmsg *nfmsg = NLMSG_DATA(nlh);
  718. u_int16_t queue_num = ntohs(nfmsg->res_id);
  719. struct nfqnl_instance *queue;
  720. int ret = 0;
  721. QDEBUG("entering for msg %u\n", NFNL_MSG_TYPE(nlh->nlmsg_type));
  722. if (nfattr_bad_size(nfqa, NFQA_CFG_MAX, nfqa_cfg_min)) {
  723. QDEBUG("bad attribute size\n");
  724. return -EINVAL;
  725. }
  726. queue = instance_lookup_get(queue_num);
  727. if (nfqa[NFQA_CFG_CMD-1]) {
  728. struct nfqnl_msg_config_cmd *cmd;
  729. cmd = NFA_DATA(nfqa[NFQA_CFG_CMD-1]);
  730. QDEBUG("found CFG_CMD\n");
  731. switch (cmd->command) {
  732. case NFQNL_CFG_CMD_BIND:
  733. if (queue)
  734. return -EBUSY;
  735. queue = instance_create(queue_num, NETLINK_CB(skb).pid);
  736. if (!queue)
  737. return -EINVAL;
  738. break;
  739. case NFQNL_CFG_CMD_UNBIND:
  740. if (!queue)
  741. return -ENODEV;
  742. if (queue->peer_pid != NETLINK_CB(skb).pid) {
  743. ret = -EPERM;
  744. goto out_put;
  745. }
  746. instance_destroy(queue);
  747. break;
  748. case NFQNL_CFG_CMD_PF_BIND:
  749. QDEBUG("registering queue handler for pf=%u\n",
  750. ntohs(cmd->pf));
  751. ret = nf_register_queue_handler(ntohs(cmd->pf), &nfqh);
  752. break;
  753. case NFQNL_CFG_CMD_PF_UNBIND:
  754. QDEBUG("unregistering queue handler for pf=%u\n",
  755. ntohs(cmd->pf));
  756. /* This is a bug and a feature. We can unregister
  757. * other handlers(!) */
  758. ret = nf_unregister_queue_handler(ntohs(cmd->pf));
  759. break;
  760. default:
  761. ret = -EINVAL;
  762. break;
  763. }
  764. } else {
  765. if (!queue) {
  766. QDEBUG("no config command, and no instance ENOENT\n");
  767. ret = -ENOENT;
  768. goto out_put;
  769. }
  770. if (queue->peer_pid != NETLINK_CB(skb).pid) {
  771. QDEBUG("no config command, and wrong pid\n");
  772. ret = -EPERM;
  773. goto out_put;
  774. }
  775. }
  776. if (nfqa[NFQA_CFG_PARAMS-1]) {
  777. struct nfqnl_msg_config_params *params;
  778. if (!queue) {
  779. ret = -ENOENT;
  780. goto out_put;
  781. }
  782. params = NFA_DATA(nfqa[NFQA_CFG_PARAMS-1]);
  783. nfqnl_set_mode(queue, params->copy_mode,
  784. ntohl(params->copy_range));
  785. }
  786. if (nfqa[NFQA_CFG_QUEUE_MAXLEN-1]) {
  787. __be32 *queue_maxlen;
  788. queue_maxlen = NFA_DATA(nfqa[NFQA_CFG_QUEUE_MAXLEN-1]);
  789. spin_lock_bh(&queue->lock);
  790. queue->queue_maxlen = ntohl(*queue_maxlen);
  791. spin_unlock_bh(&queue->lock);
  792. }
  793. out_put:
  794. instance_put(queue);
  795. return ret;
  796. }
  797. static struct nfnl_callback nfqnl_cb[NFQNL_MSG_MAX] = {
  798. [NFQNL_MSG_PACKET] = { .call = nfqnl_recv_unsupp,
  799. .attr_count = NFQA_MAX, },
  800. [NFQNL_MSG_VERDICT] = { .call = nfqnl_recv_verdict,
  801. .attr_count = NFQA_MAX, },
  802. [NFQNL_MSG_CONFIG] = { .call = nfqnl_recv_config,
  803. .attr_count = NFQA_CFG_MAX, },
  804. };
  805. static struct nfnetlink_subsystem nfqnl_subsys = {
  806. .name = "nf_queue",
  807. .subsys_id = NFNL_SUBSYS_QUEUE,
  808. .cb_count = NFQNL_MSG_MAX,
  809. .cb = nfqnl_cb,
  810. };
  811. #ifdef CONFIG_PROC_FS
  812. struct iter_state {
  813. unsigned int bucket;
  814. };
  815. static struct hlist_node *get_first(struct seq_file *seq)
  816. {
  817. struct iter_state *st = seq->private;
  818. if (!st)
  819. return NULL;
  820. for (st->bucket = 0; st->bucket < INSTANCE_BUCKETS; st->bucket++) {
  821. if (!hlist_empty(&instance_table[st->bucket]))
  822. return instance_table[st->bucket].first;
  823. }
  824. return NULL;
  825. }
  826. static struct hlist_node *get_next(struct seq_file *seq, struct hlist_node *h)
  827. {
  828. struct iter_state *st = seq->private;
  829. h = h->next;
  830. while (!h) {
  831. if (++st->bucket >= INSTANCE_BUCKETS)
  832. return NULL;
  833. h = instance_table[st->bucket].first;
  834. }
  835. return h;
  836. }
  837. static struct hlist_node *get_idx(struct seq_file *seq, loff_t pos)
  838. {
  839. struct hlist_node *head;
  840. head = get_first(seq);
  841. if (head)
  842. while (pos && (head = get_next(seq, head)))
  843. pos--;
  844. return pos ? NULL : head;
  845. }
  846. static void *seq_start(struct seq_file *seq, loff_t *pos)
  847. {
  848. read_lock_bh(&instances_lock);
  849. return get_idx(seq, *pos);
  850. }
  851. static void *seq_next(struct seq_file *s, void *v, loff_t *pos)
  852. {
  853. (*pos)++;
  854. return get_next(s, v);
  855. }
  856. static void seq_stop(struct seq_file *s, void *v)
  857. {
  858. read_unlock_bh(&instances_lock);
  859. }
  860. static int seq_show(struct seq_file *s, void *v)
  861. {
  862. const struct nfqnl_instance *inst = v;
  863. return seq_printf(s, "%5d %6d %5d %1d %5d %5d %5d %8d %2d\n",
  864. inst->queue_num,
  865. inst->peer_pid, inst->queue_total,
  866. inst->copy_mode, inst->copy_range,
  867. inst->queue_dropped, inst->queue_user_dropped,
  868. atomic_read(&inst->id_sequence),
  869. atomic_read(&inst->use));
  870. }
  871. static struct seq_operations nfqnl_seq_ops = {
  872. .start = seq_start,
  873. .next = seq_next,
  874. .stop = seq_stop,
  875. .show = seq_show,
  876. };
  877. static int nfqnl_open(struct inode *inode, struct file *file)
  878. {
  879. struct seq_file *seq;
  880. struct iter_state *is;
  881. int ret;
  882. is = kzalloc(sizeof(*is), GFP_KERNEL);
  883. if (!is)
  884. return -ENOMEM;
  885. ret = seq_open(file, &nfqnl_seq_ops);
  886. if (ret < 0)
  887. goto out_free;
  888. seq = file->private_data;
  889. seq->private = is;
  890. return ret;
  891. out_free:
  892. kfree(is);
  893. return ret;
  894. }
  895. static const struct file_operations nfqnl_file_ops = {
  896. .owner = THIS_MODULE,
  897. .open = nfqnl_open,
  898. .read = seq_read,
  899. .llseek = seq_lseek,
  900. .release = seq_release_private,
  901. };
  902. #endif /* PROC_FS */
  903. static int __init nfnetlink_queue_init(void)
  904. {
  905. int i, status = -ENOMEM;
  906. #ifdef CONFIG_PROC_FS
  907. struct proc_dir_entry *proc_nfqueue;
  908. #endif
  909. for (i = 0; i < INSTANCE_BUCKETS; i++)
  910. INIT_HLIST_HEAD(&instance_table[i]);
  911. netlink_register_notifier(&nfqnl_rtnl_notifier);
  912. status = nfnetlink_subsys_register(&nfqnl_subsys);
  913. if (status < 0) {
  914. printk(KERN_ERR "nf_queue: failed to create netlink socket\n");
  915. goto cleanup_netlink_notifier;
  916. }
  917. #ifdef CONFIG_PROC_FS
  918. proc_nfqueue = create_proc_entry("nfnetlink_queue", 0440,
  919. proc_net_netfilter);
  920. if (!proc_nfqueue)
  921. goto cleanup_subsys;
  922. proc_nfqueue->proc_fops = &nfqnl_file_ops;
  923. #endif
  924. register_netdevice_notifier(&nfqnl_dev_notifier);
  925. return status;
  926. #ifdef CONFIG_PROC_FS
  927. cleanup_subsys:
  928. nfnetlink_subsys_unregister(&nfqnl_subsys);
  929. #endif
  930. cleanup_netlink_notifier:
  931. netlink_unregister_notifier(&nfqnl_rtnl_notifier);
  932. return status;
  933. }
  934. static void __exit nfnetlink_queue_fini(void)
  935. {
  936. nf_unregister_queue_handlers(&nfqh);
  937. unregister_netdevice_notifier(&nfqnl_dev_notifier);
  938. #ifdef CONFIG_PROC_FS
  939. remove_proc_entry("nfnetlink_queue", proc_net_netfilter);
  940. #endif
  941. nfnetlink_subsys_unregister(&nfqnl_subsys);
  942. netlink_unregister_notifier(&nfqnl_rtnl_notifier);
  943. }
  944. MODULE_DESCRIPTION("netfilter packet queue handler");
  945. MODULE_AUTHOR("Harald Welte <laforge@netfilter.org>");
  946. MODULE_LICENSE("GPL");
  947. MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_QUEUE);
  948. module_init(nfnetlink_queue_init);
  949. module_exit(nfnetlink_queue_fini);