ip_queue.c
上传用户:jlfgdled
上传日期:2013-04-10
资源大小:33168k
文件大小:15k
源码类别:

Linux/Unix编程

开发平台:

Unix_Linux

  1. /*
  2.  * This is a module which is used for queueing IPv4 packets and
  3.  * communicating with userspace via netlink.
  4.  *
  5.  * (C) 2000-2002 James Morris, this code is GPL.
  6.  *
  7.  * 2000-03-27: Simplified code (thanks to Andi Kleen for clues).
  8.  * 2000-05-20: Fixed notifier problems (following Miguel Freitas' report).
  9.  * 2000-06-19: Fixed so nfmark is copied to metadata (reported by Sebastian 
  10.  *             Zander).
  11.  * 2000-08-01: Added Nick Williams' MAC support.
  12.  * 2002-06-25: Code cleanup.
  13.  *
  14.  */
  15. #include <linux/module.h>
  16. #include <linux/skbuff.h>
  17. #include <linux/init.h>
  18. #include <linux/ip.h>
  19. #include <linux/notifier.h>
  20. #include <linux/netdevice.h>
  21. #include <linux/netfilter.h>
  22. #include <linux/netfilter_ipv4/ip_queue.h>
  23. #include <linux/netfilter_ipv4/ip_tables.h>
  24. #include <linux/netlink.h>
  25. #include <linux/spinlock.h>
  26. #include <linux/brlock.h>
  27. #include <linux/sysctl.h>
  28. #include <linux/proc_fs.h>
  29. #include <net/sock.h>
  30. #include <net/route.h>
  31. #define IPQ_QMAX_DEFAULT 1024
  32. #define IPQ_PROC_FS_NAME "ip_queue"
  33. #define NET_IPQ_QMAX 2088
  34. #define NET_IPQ_QMAX_NAME "ip_queue_maxlen"
  35. struct ipq_rt_info {
  36. __u8 tos;
  37. __u32 daddr;
  38. __u32 saddr;
  39. };
  40. struct ipq_queue_entry {
  41. struct list_head list;
  42. struct nf_info *info;
  43. struct sk_buff *skb;
  44. struct ipq_rt_info rt_info;
  45. };
  46. typedef int (*ipq_cmpfn)(struct ipq_queue_entry *, unsigned long);
  47. static unsigned char copy_mode = IPQ_COPY_NONE;
  48. static unsigned int queue_maxlen = IPQ_QMAX_DEFAULT;
  49. static rwlock_t queue_lock = RW_LOCK_UNLOCKED;
  50. static int peer_pid;
  51. static unsigned int copy_range;
  52. static unsigned int queue_total;
  53. static struct sock *ipqnl;
  54. static LIST_HEAD(queue_list);
  55. static DECLARE_MUTEX(ipqnl_sem);
  56. static void
  57. ipq_issue_verdict(struct ipq_queue_entry *entry, int verdict)
  58. {
  59. nf_reinject(entry->skb, entry->info, verdict);
  60. kfree(entry);
  61. }
  62. static inline int
  63. __ipq_enqueue_entry(struct ipq_queue_entry *entry)
  64. {
  65.        if (queue_total >= queue_maxlen) {
  66.                if (net_ratelimit()) 
  67.                        printk(KERN_WARNING "ip_queue: full at %d entries, "
  68.                               "dropping packet(s).n", queue_total);
  69.                return -ENOSPC;
  70.        }
  71.        list_add(&entry->list, &queue_list);
  72.        queue_total++;
  73.        return 0;
  74. }
  75. /*
  76.  * Find and return a queued entry matched by cmpfn, or return the last
  77.  * entry if cmpfn is NULL.
  78.  */
  79. static inline struct ipq_queue_entry *
  80. __ipq_find_entry(ipq_cmpfn cmpfn, unsigned long data)
  81. {
  82. struct list_head *p;
  83. list_for_each_prev(p, &queue_list) {
  84. struct ipq_queue_entry *entry = (struct ipq_queue_entry *)p;
  85. if (!cmpfn || cmpfn(entry, data))
  86. return entry;
  87. }
  88. return NULL;
  89. }
  90. static inline void
  91. __ipq_dequeue_entry(struct ipq_queue_entry *entry)
  92. {
  93. list_del(&entry->list);
  94. queue_total--;
  95. }
  96. static inline struct ipq_queue_entry *
  97. __ipq_find_dequeue_entry(ipq_cmpfn cmpfn, unsigned long data)
  98. {
  99. struct ipq_queue_entry *entry;
  100. entry = __ipq_find_entry(cmpfn, data);
  101. if (entry == NULL)
  102. return NULL;
  103. __ipq_dequeue_entry(entry);
  104. return entry;
  105. }
  106. static inline void
  107. __ipq_flush(int verdict)
  108. {
  109. struct ipq_queue_entry *entry;
  110. while ((entry = __ipq_find_dequeue_entry(NULL, 0)))
  111. ipq_issue_verdict(entry, verdict);
  112. }
  113. static inline int
  114. __ipq_set_mode(unsigned char mode, unsigned int range)
  115. {
  116. int status = 0;
  117. switch(mode) {
  118. case IPQ_COPY_NONE:
  119. case IPQ_COPY_META:
  120. copy_mode = mode;
  121. copy_range = 0;
  122. break;
  123. case IPQ_COPY_PACKET:
  124. copy_mode = mode;
  125. copy_range = range;
  126. if (copy_range > 0xFFFF)
  127. copy_range = 0xFFFF;
  128. break;
  129. default:
  130. status = -EINVAL;
  131. }
  132. return status;
  133. }
  134. static inline void
  135. __ipq_reset(void)
  136. {
  137. peer_pid = 0;
  138. __ipq_set_mode(IPQ_COPY_NONE, 0);
  139. __ipq_flush(NF_DROP);
  140. }
  141. static struct ipq_queue_entry *
  142. ipq_find_dequeue_entry(ipq_cmpfn cmpfn, unsigned long data)
  143. {
  144. struct ipq_queue_entry *entry;
  145. write_lock_bh(&queue_lock);
  146. entry = __ipq_find_dequeue_entry(cmpfn, data);
  147. write_unlock_bh(&queue_lock);
  148. return entry;
  149. }
  150. static void
  151. ipq_flush(int verdict)
  152. {
  153. write_lock_bh(&queue_lock);
  154. __ipq_flush(verdict);
  155. write_unlock_bh(&queue_lock);
  156. }
  157. static struct sk_buff *
  158. ipq_build_packet_message(struct ipq_queue_entry *entry, int *errp)
  159. {
  160. unsigned char *old_tail;
  161. size_t size = 0;
  162. size_t data_len = 0;
  163. struct sk_buff *skb;
  164. struct ipq_packet_msg *pmsg;
  165. struct nlmsghdr *nlh;
  166. read_lock_bh(&queue_lock);
  167. switch (copy_mode) {
  168. case IPQ_COPY_META:
  169. case IPQ_COPY_NONE:
  170. size = NLMSG_SPACE(sizeof(*pmsg));
  171. data_len = 0;
  172. break;
  173. case IPQ_COPY_PACKET:
  174. if (copy_range == 0 || copy_range > entry->skb->len)
  175. data_len = entry->skb->len;
  176. else
  177. data_len = copy_range;
  178. size = NLMSG_SPACE(sizeof(*pmsg) + data_len);
  179. break;
  180. default:
  181. *errp = -EINVAL;
  182. read_unlock_bh(&queue_lock);
  183. return NULL;
  184. }
  185. read_unlock_bh(&queue_lock);
  186. skb = alloc_skb(size, GFP_ATOMIC);
  187. if (!skb)
  188. goto nlmsg_failure;
  189. old_tail= skb->tail;
  190. nlh = NLMSG_PUT(skb, 0, 0, IPQM_PACKET, size - sizeof(*nlh));
  191. pmsg = NLMSG_DATA(nlh);
  192. memset(pmsg, 0, sizeof(*pmsg));
  193. pmsg->packet_id       = (unsigned long )entry;
  194. pmsg->data_len        = data_len;
  195. pmsg->timestamp_sec   = entry->skb->stamp.tv_sec;
  196. pmsg->timestamp_usec  = entry->skb->stamp.tv_usec;
  197. pmsg->mark            = entry->skb->nfmark;
  198. pmsg->hook            = entry->info->hook;
  199. pmsg->hw_protocol     = entry->skb->protocol;
  200. if (entry->info->indev)
  201. strcpy(pmsg->indev_name, entry->info->indev->name);
  202. else
  203. pmsg->indev_name[0] = '';
  204. if (entry->info->outdev)
  205. strcpy(pmsg->outdev_name, entry->info->outdev->name);
  206. else
  207. pmsg->outdev_name[0] = '';
  208. if (entry->info->indev && entry->skb->dev) {
  209. pmsg->hw_type = entry->skb->dev->type;
  210. if (entry->skb->dev->hard_header_parse)
  211. pmsg->hw_addrlen =
  212. entry->skb->dev->hard_header_parse(entry->skb,
  213.                                    pmsg->hw_addr);
  214. }
  215. if (data_len)
  216. memcpy(pmsg->payload, entry->skb->data, data_len);
  217. nlh->nlmsg_len = skb->tail - old_tail;
  218. return skb;
  219. nlmsg_failure:
  220. if (skb)
  221. kfree_skb(skb);
  222. *errp = -EINVAL;
  223. printk(KERN_ERR "ip_queue: error creating packet messagen");
  224. return NULL;
  225. }
  226. static int
  227. ipq_enqueue_packet(struct sk_buff *skb, struct nf_info *info, void *data)
  228. {
  229. int status = -EINVAL;
  230. struct sk_buff *nskb;
  231. struct ipq_queue_entry *entry;
  232. if (copy_mode == IPQ_COPY_NONE)
  233. return -EAGAIN;
  234. entry = kmalloc(sizeof(*entry), GFP_ATOMIC);
  235. if (entry == NULL) {
  236. printk(KERN_ERR "ip_queue: OOM in ipq_enqueue_packet()n");
  237. return -ENOMEM;
  238. }
  239. entry->info = info;
  240. entry->skb = skb;
  241. if (entry->info->hook == NF_IP_LOCAL_OUT) {
  242. struct iphdr *iph = skb->nh.iph;
  243. entry->rt_info.tos = iph->tos;
  244. entry->rt_info.daddr = iph->daddr;
  245. entry->rt_info.saddr = iph->saddr;
  246. }
  247. nskb = ipq_build_packet_message(entry, &status);
  248. if (nskb == NULL)
  249. goto err_out_free;
  250. write_lock_bh(&queue_lock);
  251. if (!peer_pid)
  252. goto err_out_unlock;
  253. status = netlink_unicast(ipqnl, nskb, peer_pid, MSG_DONTWAIT);
  254. if (status < 0)
  255. goto err_out_unlock;
  256. status = __ipq_enqueue_entry(entry);
  257. if (status < 0)
  258. goto err_out_unlock;
  259. write_unlock_bh(&queue_lock);
  260. return status;
  261. err_out_unlock:
  262. write_unlock_bh(&queue_lock);
  263. err_out_free:
  264. kfree(entry);
  265. return status;
  266. }
  267. static int
  268. ipq_mangle_ipv4(ipq_verdict_msg_t *v, struct ipq_queue_entry *e)
  269. {
  270. int diff;
  271. struct iphdr *user_iph = (struct iphdr *)v->payload;
  272. if (v->data_len < sizeof(*user_iph))
  273. return 0;
  274. diff = v->data_len - e->skb->len;
  275. if (diff < 0)
  276. skb_trim(e->skb, v->data_len);
  277. else if (diff > 0) {
  278. if (v->data_len > 0xFFFF)
  279. return -EINVAL;
  280. if (diff > skb_tailroom(e->skb)) {
  281. struct sk_buff *newskb;
  282. newskb = skb_copy_expand(e->skb,
  283.                          skb_headroom(e->skb),
  284.                          diff,
  285.                          GFP_ATOMIC);
  286. if (newskb == NULL) {
  287. printk(KERN_WARNING "ip_queue: OOM "
  288.       "in mangle, dropping packetn");
  289. return -ENOMEM;
  290. }
  291. if (e->skb->sk)
  292. skb_set_owner_w(newskb, e->skb->sk);
  293. kfree_skb(e->skb);
  294. e->skb = newskb;
  295. }
  296. skb_put(e->skb, diff);
  297. }
  298. memcpy(e->skb->data, v->payload, v->data_len);
  299. e->skb->nfcache |= NFC_ALTERED;
  300. /*
  301.  * Extra routing may needed on local out, as the QUEUE target never
  302.  * returns control to the table.
  303.  */
  304. if (e->info->hook == NF_IP_LOCAL_OUT) {
  305. struct iphdr *iph = e->skb->nh.iph;
  306. if (!(iph->tos == e->rt_info.tos
  307.       && iph->daddr == e->rt_info.daddr
  308.       && iph->saddr == e->rt_info.saddr))
  309. return ip_route_me_harder(&e->skb);
  310. }
  311. return 0;
  312. }
  313. static inline int
  314. id_cmp(struct ipq_queue_entry *e, unsigned long id)
  315. {
  316. return (id == (unsigned long )e);
  317. }
  318. static int
  319. ipq_set_verdict(struct ipq_verdict_msg *vmsg, unsigned int len)
  320. {
  321. struct ipq_queue_entry *entry;
  322. if (vmsg->value > NF_MAX_VERDICT)
  323. return -EINVAL;
  324. entry = ipq_find_dequeue_entry(id_cmp, vmsg->id);
  325. if (entry == NULL)
  326. return -ENOENT;
  327. else {
  328. int verdict = vmsg->value;
  329. if (vmsg->data_len && vmsg->data_len == len)
  330. if (ipq_mangle_ipv4(vmsg, entry) < 0)
  331. verdict = NF_DROP;
  332. ipq_issue_verdict(entry, verdict);
  333. return 0;
  334. }
  335. }
  336. static int
  337. ipq_set_mode(unsigned char mode, unsigned int range)
  338. {
  339. int status;
  340. write_lock_bh(&queue_lock);
  341. status = __ipq_set_mode(mode, range);
  342. write_unlock_bh(&queue_lock);
  343. return status;
  344. }
  345. static int
  346. ipq_receive_peer(struct ipq_peer_msg *pmsg,
  347.                  unsigned char type, unsigned int len)
  348. {
  349. int status = 0;
  350. if (len < sizeof(*pmsg))
  351. return -EINVAL;
  352. switch (type) {
  353. case IPQM_MODE:
  354. status = ipq_set_mode(pmsg->msg.mode.value,
  355.                       pmsg->msg.mode.range);
  356. break;
  357. case IPQM_VERDICT:
  358. if (pmsg->msg.verdict.value > NF_MAX_VERDICT)
  359. status = -EINVAL;
  360. else
  361. status = ipq_set_verdict(&pmsg->msg.verdict,
  362.                          len - sizeof(*pmsg));
  363. break;
  364. default:
  365. status = -EINVAL;
  366. }
  367. return status;
  368. }
  369. static int
  370. dev_cmp(struct ipq_queue_entry *entry, unsigned long ifindex)
  371. {
  372. if (entry->info->indev)
  373. if (entry->info->indev->ifindex == ifindex)
  374. return 1;
  375. if (entry->info->outdev)
  376. if (entry->info->outdev->ifindex == ifindex)
  377. return 1;
  378. return 0;
  379. }
  380. static void
  381. ipq_dev_drop(int ifindex)
  382. {
  383. struct ipq_queue_entry *entry;
  384. while ((entry = ipq_find_dequeue_entry(dev_cmp, ifindex)) != NULL)
  385. ipq_issue_verdict(entry, NF_DROP);
  386. }
  387. #define RCV_SKB_FAIL(err) do { netlink_ack(skb, nlh, (err)); return; } while (0)
  388. static inline void
  389. ipq_rcv_skb(struct sk_buff *skb)
  390. {
  391. int status, type, pid, flags, nlmsglen, skblen;
  392. struct nlmsghdr *nlh;
  393. skblen = skb->len;
  394. if (skblen < sizeof(*nlh))
  395. return;
  396. nlh = (struct nlmsghdr *)skb->data;
  397. nlmsglen = nlh->nlmsg_len;
  398. if (nlmsglen < sizeof(*nlh) || skblen < nlmsglen)
  399. return;
  400. pid = nlh->nlmsg_pid;
  401. flags = nlh->nlmsg_flags;
  402. if(pid <= 0 || !(flags & NLM_F_REQUEST) || flags & NLM_F_MULTI)
  403. RCV_SKB_FAIL(-EINVAL);
  404. if (flags & MSG_TRUNC)
  405. RCV_SKB_FAIL(-ECOMM);
  406. type = nlh->nlmsg_type;
  407. if (type < NLMSG_NOOP || type >= IPQM_MAX)
  408. RCV_SKB_FAIL(-EINVAL);
  409. if (type <= IPQM_BASE)
  410. return;
  411. if(!cap_raised(NETLINK_CB(skb).eff_cap, CAP_NET_ADMIN))
  412. RCV_SKB_FAIL(-EPERM);
  413. write_lock_bh(&queue_lock);
  414. if (peer_pid) {
  415. if (peer_pid != pid) {
  416. write_unlock_bh(&queue_lock);
  417. RCV_SKB_FAIL(-EBUSY);
  418. }
  419. }
  420. else
  421. peer_pid = pid;
  422. write_unlock_bh(&queue_lock);
  423. status = ipq_receive_peer(NLMSG_DATA(nlh), type,
  424.                           skblen - NLMSG_LENGTH(0));
  425. if (status < 0)
  426. RCV_SKB_FAIL(status);
  427. if (flags & NLM_F_ACK)
  428. netlink_ack(skb, nlh, 0);
  429.         return;
  430. }
  431. static void
  432. ipq_rcv_sk(struct sock *sk, int len)
  433. {
  434. do {
  435. struct sk_buff *skb;
  436. if (down_trylock(&ipqnl_sem))
  437. return;
  438. while ((skb = skb_dequeue(&sk->receive_queue)) != NULL) {
  439. ipq_rcv_skb(skb);
  440. kfree_skb(skb);
  441. }
  442. up(&ipqnl_sem);
  443. } while (ipqnl && ipqnl->receive_queue.qlen);
  444. }
  445. static int
  446. ipq_rcv_dev_event(struct notifier_block *this,
  447.                   unsigned long event, void *ptr)
  448. {
  449. struct net_device *dev = ptr;
  450. /* Drop any packets associated with the downed device */
  451. if (event == NETDEV_DOWN)
  452. ipq_dev_drop(dev->ifindex);
  453. return NOTIFY_DONE;
  454. }
  455. static struct notifier_block ipq_dev_notifier = {
  456. ipq_rcv_dev_event,
  457. NULL,
  458. 0
  459. };
  460. static int
  461. ipq_rcv_nl_event(struct notifier_block *this,
  462.                  unsigned long event, void *ptr)
  463. {
  464. struct netlink_notify *n = ptr;
  465. if (event == NETLINK_URELEASE &&
  466.     n->protocol == NETLINK_FIREWALL && n->pid) {
  467. write_lock_bh(&queue_lock);
  468. if (n->pid == peer_pid)
  469. __ipq_reset();
  470. write_unlock_bh(&queue_lock);
  471. }
  472. return NOTIFY_DONE;
  473. }
  474. static struct notifier_block ipq_nl_notifier = {
  475. ipq_rcv_nl_event,
  476. NULL,
  477. 0
  478. };
  479. static int sysctl_maxlen = IPQ_QMAX_DEFAULT;
  480. static struct ctl_table_header *ipq_sysctl_header;
  481. static ctl_table ipq_table[] = {
  482. { NET_IPQ_QMAX, NET_IPQ_QMAX_NAME, &sysctl_maxlen,
  483.   sizeof(sysctl_maxlen), 0644,  NULL, proc_dointvec },
  484.   { 0 }
  485. };
  486. static ctl_table ipq_dir_table[] = {
  487. {NET_IPV4, "ipv4", NULL, 0, 0555, ipq_table, 0, 0, 0, 0, 0},
  488. { 0 }
  489. };
  490. static ctl_table ipq_root_table[] = {
  491. {CTL_NET, "net", NULL, 0, 0555, ipq_dir_table, 0, 0, 0, 0, 0},
  492. { 0 }
  493. };
  494. static int
  495. ipq_get_info(char *buffer, char **start, off_t offset, int length)
  496. {
  497. int len;
  498. read_lock_bh(&queue_lock);
  499. len = sprintf(buffer,
  500.               "Peer PID          : %dn"
  501.               "Copy mode         : %hun"
  502.               "Copy range        : %un"
  503.               "Queue length      : %un"
  504.               "Queue max. length : %un",
  505.               peer_pid,
  506.               copy_mode,
  507.               copy_range,
  508.               queue_total,
  509.               queue_maxlen);
  510. read_unlock_bh(&queue_lock);
  511. *start = buffer + offset;
  512. len -= offset;
  513. if (len > length)
  514. len = length;
  515. else if (len < 0)
  516. len = 0;
  517. return len;
  518. }
  519. static int
  520. init_or_cleanup(int init)
  521. {
  522. int status = -ENOMEM;
  523. struct proc_dir_entry *proc;
  524. if (!init)
  525. goto cleanup;
  526. netlink_register_notifier(&ipq_nl_notifier);
  527. ipqnl = netlink_kernel_create(NETLINK_FIREWALL, ipq_rcv_sk);
  528. if (ipqnl == NULL) {
  529. printk(KERN_ERR "ip_queue: failed to create netlink socketn");
  530. goto cleanup_netlink_notifier;
  531. }
  532. proc = proc_net_create(IPQ_PROC_FS_NAME, 0, ipq_get_info);
  533. if (proc)
  534. proc->owner = THIS_MODULE;
  535. else {
  536. printk(KERN_ERR "ip_queue: failed to create proc entryn");
  537. goto cleanup_ipqnl;
  538. }
  539. register_netdevice_notifier(&ipq_dev_notifier);
  540. ipq_sysctl_header = register_sysctl_table(ipq_root_table, 0);
  541. status = nf_register_queue_handler(PF_INET, ipq_enqueue_packet, NULL);
  542. if (status < 0) {
  543. printk(KERN_ERR "ip_queue: failed to register queue handlern");
  544. goto cleanup_sysctl;
  545. }
  546. return status;
  547. cleanup:
  548. nf_unregister_queue_handler(PF_INET);
  549. br_write_lock_bh(BR_NETPROTO_LOCK);
  550. br_write_unlock_bh(BR_NETPROTO_LOCK);
  551. ipq_flush(NF_DROP);
  552. cleanup_sysctl:
  553. unregister_sysctl_table(ipq_sysctl_header);
  554. unregister_netdevice_notifier(&ipq_dev_notifier);
  555. proc_net_remove(IPQ_PROC_FS_NAME);
  556. cleanup_ipqnl:
  557. sock_release(ipqnl->socket);
  558. down(&ipqnl_sem);
  559. up(&ipqnl_sem);
  560. cleanup_netlink_notifier:
  561. netlink_unregister_notifier(&ipq_nl_notifier);
  562. return status;
  563. }
  564. static int __init init(void)
  565. {
  566. return init_or_cleanup(1);
  567. }
  568. static void __exit fini(void)
  569. {
  570. init_or_cleanup(0);
  571. }
  572. MODULE_DESCRIPTION("IPv4 packet queue handler");
  573. MODULE_AUTHOR("James Morris <jmorris@intercode.com.au>");
  574. MODULE_LICENSE("GPL");
  575. module_init(init);
  576. module_exit(fini);