ip6_queue.c
上传用户:lgb322
上传日期:2013-02-24
资源大小:30529k
文件大小:19k
源码类别:

嵌入式Linux

开发平台:

Unix_Linux

  1. /*
  2.  * This is a module which is used for queueing IPv6 packets and
  3.  * communicating with userspace via netlink.
  4.  *
  5.  * (C) 2001 Fernando Anton, this code is GPL.
  6.  *     IPv64 Project - Work based in IPv64 draft by Arturo Azcorra.
  7.  *     Universidad Carlos III de Madrid - Leganes (Madrid) - Spain
  8.  *     Universidad Politecnica de Alcala de Henares - Alcala de H. (Madrid) - Spain
  9.  *     email: fanton@it.uc3m.es
  10.  *
  11.  * 2001-11-06: First try. Working with ip_queue.c for IPv4 and trying
  12.  *             to adapt it to IPv6
  13.  *             HEAVILY based in ipqueue.c by James Morris. It's just
  14.  *             a little modified version of it, so he's nearly the
  15.  *             real coder of this.
  16.  *             Few changes needed, mainly the hard_routing code and
  17.  *             the netlink socket protocol (we're NETLINK_IP6_FW).
  18.  *
  19.  */
  20. #include <linux/module.h>
  21. #include <linux/skbuff.h>
  22. #include <linux/init.h>
  23. #include <linux/ipv6.h>
  24. #include <linux/notifier.h>
  25. #include <linux/netdevice.h>
  26. #include <linux/netfilter.h>
  27. #include <linux/netlink.h>
  28. #include <linux/spinlock.h>
  29. #include <linux/rtnetlink.h>
  30. #include <linux/sysctl.h>
  31. #include <linux/proc_fs.h>
  32. #include <net/sock.h>
  33. #include <net/ipv6.h>
  34. #include <net/ip6_route.h>
  35. /* We're still usign the following structs. No need to change them: */
  36. /*   ipq_packet_msg                                                 */
  37. /*   ipq_mode_msg                                                   */
  38. /*   ipq_verdict_msg                                                */
  39. /*   ipq_peer_msg                                                   */
  40. #include <linux/netfilter_ipv4/ip_queue.h>
  41. #include <linux/netfilter_ipv4/ip_tables.h>
  42. #include <linux/netfilter_ipv6/ip6_tables.h>
  43. #define IPQ_QMAX_DEFAULT 1024
  44. #define IPQ_PROC_FS_NAME "ip6_queue"
  45. #define NET_IPQ_QMAX 2088
  46. #define NET_IPQ_QMAX_NAME "ip6_queue_maxlen"
  47. typedef struct ip6q_rt_info {
  48. struct in6_addr daddr;
  49. struct in6_addr saddr;
  50. } ip6q_rt_info_t;
  51. typedef struct ip6q_queue_element {
  52. struct list_head list; /* Links element into queue */
  53. int verdict; /* Current verdict */
  54. struct nf_info *info; /* Extra info from netfilter */
  55. struct sk_buff *skb; /* Packet inside */
  56. ip6q_rt_info_t rt_info; /* May need post-mangle routing */
  57. } ip6q_queue_element_t;
  58. typedef int (*ip6q_send_cb_t)(ip6q_queue_element_t *e);
  59. typedef struct ip6q_peer {
  60. pid_t pid; /* PID of userland peer */
  61. unsigned char died; /* We think the peer died */
  62. unsigned char copy_mode; /* Copy packet as well as metadata? */
  63. size_t copy_range; /* Range past metadata to copy */
  64. ip6q_send_cb_t send; /* Callback for sending data to peer */
  65. } ip6q_peer_t;
  66. typedef struct ip6q_queue {
  67.   int len; /* Current queue len */
  68.   int *maxlen; /* Maximum queue len, via sysctl */
  69.   unsigned char flushing; /* If queue is being flushed */
  70.   unsigned char terminate; /* If the queue is being terminated */
  71.   struct list_head list; /* Head of packet queue */
  72.   spinlock_t lock; /* Queue spinlock */
  73.   ip6q_peer_t peer; /* Userland peer */
  74. } ip6q_queue_t;
  75. /****************************************************************************
  76.  *
  77.  * Packet queue
  78.  *
  79.  ****************************************************************************/
  80. /* Dequeue a packet if matched by cmp, or the next available if cmp is NULL */
  81. static ip6q_queue_element_t *
  82. ip6q_dequeue(ip6q_queue_t *q,
  83.             int (*cmp)(ip6q_queue_element_t *, unsigned long),
  84.             unsigned long data)
  85. {
  86. struct list_head *i;
  87. spin_lock_bh(&q->lock);
  88. for (i = q->list.prev; i != &q->list; i = i->prev) {
  89. ip6q_queue_element_t *e = (ip6q_queue_element_t *)i;
  90. if (!cmp || cmp(e, data)) {
  91. list_del(&e->list);
  92. q->len--;
  93. spin_unlock_bh(&q->lock);
  94. return e;
  95. }
  96. }
  97. spin_unlock_bh(&q->lock);
  98. return NULL;
  99. }
  100. /* Flush all packets */
  101. static void ip6q_flush(ip6q_queue_t *q)
  102. {
  103. ip6q_queue_element_t *e;
  104. spin_lock_bh(&q->lock);
  105. q->flushing = 1;
  106. spin_unlock_bh(&q->lock);
  107. while ((e = ip6q_dequeue(q, NULL, 0))) {
  108. e->verdict = NF_DROP;
  109. nf_reinject(e->skb, e->info, e->verdict);
  110. kfree(e);
  111. }
  112. spin_lock_bh(&q->lock);
  113. q->flushing = 0;
  114. spin_unlock_bh(&q->lock);
  115. }
  116. static ip6q_queue_t *ip6q_create_queue(nf_queue_outfn_t outfn,
  117.                                      ip6q_send_cb_t send_cb,
  118.                                      int *errp, int *sysctl_qmax)
  119. {
  120. int status;
  121. ip6q_queue_t *q;
  122. *errp = 0;
  123. q = kmalloc(sizeof(ip6q_queue_t), GFP_KERNEL);
  124. if (q == NULL) {
  125. *errp = -ENOMEM;
  126. return NULL;
  127. }
  128. q->peer.pid = 0;
  129. q->peer.died = 0;
  130. q->peer.copy_mode = IPQ_COPY_NONE;
  131. q->peer.copy_range = 0;
  132. q->peer.send = send_cb;
  133. q->len = 0;
  134. q->maxlen = sysctl_qmax;
  135. q->flushing = 0;
  136. q->terminate = 0;
  137. INIT_LIST_HEAD(&q->list);
  138. spin_lock_init(&q->lock);
  139. status = nf_register_queue_handler(PF_INET6, outfn, q);
  140. if (status < 0) {
  141. *errp = -EBUSY;
  142. kfree(q);
  143. return NULL;
  144. }
  145. return q;
  146. }
  147. static int ip6q_enqueue(ip6q_queue_t *q,
  148.                        struct sk_buff *skb, struct nf_info *info)
  149. {
  150. ip6q_queue_element_t *e;
  151. int status;
  152. e = kmalloc(sizeof(*e), GFP_ATOMIC);
  153. if (e == NULL) {
  154. printk(KERN_ERR "ip6_queue: OOM in enqueuen");
  155. return -ENOMEM;
  156. }
  157. e->verdict = NF_DROP;
  158. e->info = info;
  159. e->skb = skb;
  160. if (e->info->hook == NF_IP_LOCAL_OUT) {
  161. struct ipv6hdr *iph = skb->nh.ipv6h;
  162. e->rt_info.daddr = iph->daddr;
  163. e->rt_info.saddr = iph->saddr;
  164. }
  165. spin_lock_bh(&q->lock);
  166. if (q->len >= *q->maxlen) {
  167. spin_unlock_bh(&q->lock);
  168. if (net_ratelimit()) 
  169. printk(KERN_WARNING "ip6_queue: full at %d entries, "
  170.        "dropping packet(s).n", q->len);
  171. goto free_drop;
  172. }
  173. if (q->flushing || q->peer.copy_mode == IPQ_COPY_NONE
  174.     || q->peer.pid == 0 || q->peer.died || q->terminate) {
  175. spin_unlock_bh(&q->lock);
  176. goto free_drop;
  177. }
  178. status = q->peer.send(e);
  179. if (status > 0) {
  180. list_add(&e->list, &q->list);
  181. q->len++;
  182. spin_unlock_bh(&q->lock);
  183. return status;
  184. }
  185. spin_unlock_bh(&q->lock);
  186. if (status == -ECONNREFUSED) {
  187. printk(KERN_INFO "ip6_queue: peer %d died, "
  188.        "resetting state and flushing queuen", q->peer.pid);
  189. q->peer.died = 1;
  190. q->peer.pid = 0;
  191. q->peer.copy_mode = IPQ_COPY_NONE;
  192. q->peer.copy_range = 0;
  193. ip6q_flush(q);
  194. }
  195. free_drop:
  196. kfree(e);
  197. return -EBUSY;
  198. }
  199. static void ip6q_destroy_queue(ip6q_queue_t *q)
  200. {
  201. nf_unregister_queue_handler(PF_INET6);
  202. spin_lock_bh(&q->lock);
  203. q->terminate = 1;
  204. spin_unlock_bh(&q->lock);
  205. ip6q_flush(q);
  206. kfree(q);
  207. }
  208. /*
  209.  * Taken from net/ipv6/ip6_output.c
  210.  *
  211.  * We should use the one there, but is defined static
  212.  * so we put this just here and let the things as
  213.  * they are now.
  214.  *
  215.  * If that one is modified, this one should be modified too.
  216.  */
  217. static int route6_me_harder(struct sk_buff *skb)
  218. {
  219. struct ipv6hdr *iph = skb->nh.ipv6h;
  220. struct dst_entry *dst;
  221. struct flowi fl;
  222. fl.proto = iph->nexthdr;
  223. fl.fl6_dst = &iph->daddr;
  224. fl.fl6_src = &iph->saddr;
  225. fl.oif = skb->sk ? skb->sk->bound_dev_if : 0;
  226. fl.fl6_flowlabel = 0;
  227. fl.uli_u.ports.dport = 0;
  228. fl.uli_u.ports.sport = 0;
  229. dst = ip6_route_output(skb->sk, &fl);
  230. if (dst->error) {
  231. if (net_ratelimit())
  232. printk(KERN_DEBUG "route6_me_harder: No more route.n");
  233. return -EINVAL;
  234. }
  235. /* Drop old route. */
  236. dst_release(skb->dst);
  237. skb->dst = dst;
  238. return 0;
  239. }
  240. static int ip6q_mangle_ipv6(ipq_verdict_msg_t *v, ip6q_queue_element_t *e)
  241. {
  242. int diff;
  243. struct ipv6hdr *user_iph = (struct ipv6hdr *)v->payload;
  244. if (v->data_len < sizeof(*user_iph))
  245. return 0;
  246. diff = v->data_len - e->skb->len;
  247. if (diff < 0)
  248. skb_trim(e->skb, v->data_len);
  249. else if (diff > 0) {
  250. if (v->data_len > 0xFFFF)
  251. return -EINVAL;
  252. if (diff > skb_tailroom(e->skb)) {
  253. struct sk_buff *newskb;
  254. newskb = skb_copy_expand(e->skb,
  255.                          skb_headroom(e->skb),
  256.                          diff,
  257.                          GFP_ATOMIC);
  258. if (newskb == NULL) {
  259. printk(KERN_WARNING "ip6_queue: OOM "
  260.       "in mangle, dropping packetn");
  261. return -ENOMEM;
  262. }
  263. if (e->skb->sk)
  264. skb_set_owner_w(newskb, e->skb->sk);
  265. kfree_skb(e->skb);
  266. e->skb = newskb;
  267. }
  268. skb_put(e->skb, diff);
  269. }
  270. memcpy(e->skb->data, v->payload, v->data_len);
  271. e->skb->nfcache |= NFC_ALTERED;
  272. /*
  273.  * Extra routing may needed on local out, as the QUEUE target never
  274.  * returns control to the table.
  275.          * Not a nice way to cmp, but works
  276.  */
  277. if (e->info->hook == NF_IP_LOCAL_OUT) {
  278. struct ipv6hdr *iph = e->skb->nh.ipv6h;
  279. if (!(   iph->daddr.in6_u.u6_addr32[0] == e->rt_info.daddr.in6_u.u6_addr32[0]
  280.                       && iph->daddr.in6_u.u6_addr32[1] == e->rt_info.daddr.in6_u.u6_addr32[1]
  281.                       && iph->daddr.in6_u.u6_addr32[2] == e->rt_info.daddr.in6_u.u6_addr32[2]
  282.                       && iph->daddr.in6_u.u6_addr32[3] == e->rt_info.daddr.in6_u.u6_addr32[3]
  283.       && iph->saddr.in6_u.u6_addr32[0] == e->rt_info.saddr.in6_u.u6_addr32[0]
  284.       && iph->saddr.in6_u.u6_addr32[1] == e->rt_info.saddr.in6_u.u6_addr32[1]
  285.       && iph->saddr.in6_u.u6_addr32[2] == e->rt_info.saddr.in6_u.u6_addr32[2]
  286.       && iph->saddr.in6_u.u6_addr32[3] == e->rt_info.saddr.in6_u.u6_addr32[3]))
  287. return route6_me_harder(e->skb);
  288. }
  289. return 0;
  290. }
  291. static inline int id_cmp(ip6q_queue_element_t *e, unsigned long id)
  292. {
  293. return (id == (unsigned long )e);
  294. }
  295. static int ip6q_set_verdict(ip6q_queue_t *q,
  296.                            ipq_verdict_msg_t *v, unsigned int len)
  297. {
  298. ip6q_queue_element_t *e;
  299. if (v->value > NF_MAX_VERDICT)
  300. return -EINVAL;
  301. e = ip6q_dequeue(q, id_cmp, v->id);
  302. if (e == NULL)
  303. return -ENOENT;
  304. else {
  305. e->verdict = v->value;
  306. if (v->data_len && v->data_len == len)
  307. if (ip6q_mangle_ipv6(v, e) < 0)
  308. e->verdict = NF_DROP;
  309. nf_reinject(e->skb, e->info, e->verdict);
  310. kfree(e);
  311. return 0;
  312. }
  313. }
  314. static int ip6q_receive_peer(ip6q_queue_t* q, ipq_peer_msg_t *m,
  315.                             unsigned char type, unsigned int len)
  316. {
  317. int status = 0;
  318. int busy;
  319. spin_lock_bh(&q->lock);
  320. busy = (q->terminate || q->flushing);
  321. spin_unlock_bh(&q->lock);
  322. if (busy)
  323. return -EBUSY;
  324. if (len < sizeof(ipq_peer_msg_t))
  325. return -EINVAL;
  326. switch (type) {
  327. case IPQM_MODE:
  328. switch (m->msg.mode.value) {
  329. case IPQ_COPY_META:
  330. q->peer.copy_mode = IPQ_COPY_META;
  331. q->peer.copy_range = 0;
  332. break;
  333. case IPQ_COPY_PACKET:
  334. q->peer.copy_mode = IPQ_COPY_PACKET;
  335. q->peer.copy_range = m->msg.mode.range;
  336. if (q->peer.copy_range > 0xFFFF)
  337. q->peer.copy_range = 0xFFFF;
  338. break;
  339. default:
  340. status = -EINVAL;
  341. }
  342. break;
  343. case IPQM_VERDICT:
  344. if (m->msg.verdict.value > NF_MAX_VERDICT)
  345. status = -EINVAL;
  346. else
  347. status = ip6q_set_verdict(q,
  348.                          &m->msg.verdict,
  349.                          len - sizeof(*m));
  350. break;
  351. default:
  352.  status = -EINVAL;
  353. }
  354. return status;
  355. }
  356. static inline int dev_cmp(ip6q_queue_element_t *e, unsigned long ifindex)
  357. {
  358. if (e->info->indev)
  359. if (e->info->indev->ifindex == ifindex)
  360. return 1;
  361. if (e->info->outdev)
  362. if (e->info->outdev->ifindex == ifindex)
  363. return 1;
  364. return 0;
  365. }
  366. /* Drop any queued packets associated with device ifindex */
  367. static void ip6q_dev_drop(ip6q_queue_t *q, int ifindex)
  368. {
  369. ip6q_queue_element_t *e;
  370. while ((e = ip6q_dequeue(q, dev_cmp, ifindex))) {
  371. e->verdict = NF_DROP;
  372. nf_reinject(e->skb, e->info, e->verdict);
  373. kfree(e);
  374. }
  375. }
  376. /****************************************************************************
  377.  *
  378.  * Netfilter interface
  379.  *
  380.  ****************************************************************************/
  381. /*
  382.  * Packets arrive here from netfilter for queuing to userspace.
  383.  * All of them must be fed back via nf_reinject() or Alexey will kill Rusty.
  384.  */
  385. static int netfilter6_receive(struct sk_buff *skb,
  386.                              struct nf_info *info, void *data)
  387. {
  388. return ip6q_enqueue((ip6q_queue_t *)data, skb, info);
  389. }
  390. /****************************************************************************
  391.  *
  392.  * Netlink interface.
  393.  *
  394.  ****************************************************************************/
  395. static struct sock *nfnl = NULL;
  396. /* This is not a static one, so we should not repeat its name */
  397. ip6q_queue_t *nlq6 = NULL;
  398. static struct sk_buff *netlink_build_message(ip6q_queue_element_t *e, int *errp)
  399. {
  400. unsigned char *old_tail;
  401. size_t size = 0;
  402. size_t data_len = 0;
  403. struct sk_buff *skb;
  404. ipq_packet_msg_t *pm;
  405. struct nlmsghdr *nlh;
  406. switch (nlq6->peer.copy_mode) {
  407. size_t copy_range;
  408. case IPQ_COPY_META:
  409. size = NLMSG_SPACE(sizeof(*pm));
  410. data_len = 0;
  411. break;
  412. case IPQ_COPY_PACKET:
  413. copy_range = nlq6->peer.copy_range;
  414. if (copy_range == 0 || copy_range > e->skb->len)
  415. data_len = e->skb->len;
  416. else
  417. data_len = copy_range;
  418. size = NLMSG_SPACE(sizeof(*pm) + data_len);
  419. break;
  420. case IPQ_COPY_NONE:
  421. default:
  422. *errp = -EINVAL;
  423. return NULL;
  424. }
  425. skb = alloc_skb(size, GFP_ATOMIC);
  426. if (!skb)
  427. goto nlmsg_failure;
  428. old_tail = skb->tail;
  429. nlh = NLMSG_PUT(skb, 0, 0, IPQM_PACKET, size - sizeof(*nlh));
  430. pm = NLMSG_DATA(nlh);
  431. memset(pm, 0, sizeof(*pm));
  432. pm->packet_id = (unsigned long )e;
  433. pm->data_len = data_len;
  434. pm->timestamp_sec = e->skb->stamp.tv_sec;
  435. pm->timestamp_usec = e->skb->stamp.tv_usec;
  436. pm->mark = e->skb->nfmark;
  437. pm->hook = e->info->hook;
  438. if (e->info->indev) strcpy(pm->indev_name, e->info->indev->name);
  439. else pm->indev_name[0] = '';
  440. if (e->info->outdev) strcpy(pm->outdev_name, e->info->outdev->name);
  441. else pm->outdev_name[0] = '';
  442. pm->hw_protocol = e->skb->protocol;
  443. if (e->info->indev && e->skb->dev) {
  444. pm->hw_type = e->skb->dev->type;
  445. if (e->skb->dev->hard_header_parse)
  446. pm->hw_addrlen =
  447. e->skb->dev->hard_header_parse(e->skb,
  448.                                pm->hw_addr);
  449. }
  450. if (data_len)
  451. memcpy(pm->payload, e->skb->data, data_len);
  452. nlh->nlmsg_len = skb->tail - old_tail;
  453. NETLINK_CB(skb).dst_groups = 0;
  454. return skb;
  455. nlmsg_failure:
  456. if (skb)
  457. kfree_skb(skb);
  458. *errp = 0;
  459. printk(KERN_ERR "ip6_queue: error creating netlink messagen");
  460. return NULL;
  461. }
  462. static int netlink_send_peer(ip6q_queue_element_t *e)
  463. {
  464. int status = 0;
  465. struct sk_buff *skb;
  466. skb = netlink_build_message(e, &status);
  467. if (skb == NULL)
  468. return status;
  469. return netlink_unicast(nfnl, skb, nlq6->peer.pid, MSG_DONTWAIT);
  470. }
  471. #define RCV_SKB_FAIL(err) do { netlink_ack(skb, nlh, (err)); return; } while (0);
  472. static __inline__ void netlink_receive_user_skb(struct sk_buff *skb)
  473. {
  474. int status, type;
  475. struct nlmsghdr *nlh;
  476. if (skb->len < sizeof(struct nlmsghdr))
  477. return;
  478. nlh = (struct nlmsghdr *)skb->data;
  479. if (nlh->nlmsg_len < sizeof(struct nlmsghdr)
  480.     || skb->len < nlh->nlmsg_len)
  481.      return;
  482. if(nlh->nlmsg_pid <= 0
  483.     || !(nlh->nlmsg_flags & NLM_F_REQUEST)
  484.     || nlh->nlmsg_flags & NLM_F_MULTI)
  485. RCV_SKB_FAIL(-EINVAL);
  486. if (nlh->nlmsg_flags & MSG_TRUNC)
  487. RCV_SKB_FAIL(-ECOMM);
  488. type = nlh->nlmsg_type;
  489. if (type < NLMSG_NOOP || type >= IPQM_MAX)
  490. RCV_SKB_FAIL(-EINVAL);
  491. if (type <= IPQM_BASE)
  492. return;
  493. if(!cap_raised(NETLINK_CB(skb).eff_cap, CAP_NET_ADMIN))
  494. RCV_SKB_FAIL(-EPERM);
  495. if (nlq6->peer.pid && !nlq6->peer.died
  496.     && (nlq6->peer.pid != nlh->nlmsg_pid)) {
  497.      printk(KERN_WARNING "ip6_queue: peer pid changed from %d to "
  498.            "%d, flushing queuen", nlq6->peer.pid, nlh->nlmsg_pid);
  499. ip6q_flush(nlq6);
  500. }
  501. nlq6->peer.pid = nlh->nlmsg_pid;
  502. nlq6->peer.died = 0;
  503. status = ip6q_receive_peer(nlq6, NLMSG_DATA(nlh),
  504.                           type, skb->len - NLMSG_LENGTH(0));
  505. if (status < 0)
  506. RCV_SKB_FAIL(status);
  507. if (nlh->nlmsg_flags & NLM_F_ACK)
  508. netlink_ack(skb, nlh, 0);
  509.         return;
  510. }
  511. /* Note: we are only dealing with single part messages at the moment. */
  512. static void netlink_receive_user_sk(struct sock *sk, int len)
  513. {
  514. do {
  515. struct sk_buff *skb;
  516. if (rtnl_shlock_nowait())
  517. return;
  518. while ((skb = skb_dequeue(&sk->receive_queue)) != NULL) {
  519. netlink_receive_user_skb(skb);
  520. kfree_skb(skb);
  521. }
  522. up(&rtnl_sem);
  523. } while (nfnl && nfnl->receive_queue.qlen);
  524. }
  525. /****************************************************************************
  526.  *
  527.  * System events
  528.  *
  529.  ****************************************************************************/
  530. static int receive_event(struct notifier_block *this,
  531.                          unsigned long event, void *ptr)
  532. {
  533. struct net_device *dev = ptr;
  534. /* Drop any packets associated with the downed device */
  535. if (event == NETDEV_DOWN)
  536. ip6q_dev_drop(nlq6, dev->ifindex);
  537. return NOTIFY_DONE;
  538. }
  539. struct notifier_block ip6q_dev_notifier = {
  540. receive_event,
  541. NULL,
  542. 0
  543. };
  544. /****************************************************************************
  545.  *
  546.  * Sysctl - queue tuning.
  547.  *
  548.  ****************************************************************************/
  549. static int sysctl_maxlen = IPQ_QMAX_DEFAULT;
  550. static struct ctl_table_header *ip6q_sysctl_header;
  551. static ctl_table ip6q_table[] = {
  552. { NET_IPQ_QMAX, NET_IPQ_QMAX_NAME, &sysctl_maxlen,
  553.   sizeof(sysctl_maxlen), 0644,  NULL, proc_dointvec },
  554.   { 0 }
  555. };
  556. static ctl_table ip6q_dir_table[] = {
  557. {NET_IPV6, "ipv6", NULL, 0, 0555, ip6q_table, 0, 0, 0, 0, 0},
  558. { 0 }
  559. };
  560. static ctl_table ip6q_root_table[] = {
  561. {CTL_NET, "net", NULL, 0, 0555, ip6q_dir_table, 0, 0, 0, 0, 0},
  562. { 0 }
  563. };
  564. /****************************************************************************
  565.  *
  566.  * Procfs - debugging info.
  567.  *
  568.  ****************************************************************************/
  569. static int ip6q_get_info(char *buffer, char **start, off_t offset, int length)
  570. {
  571. int len;
  572. spin_lock_bh(&nlq6->lock);
  573. len = sprintf(buffer,
  574.               "Peer pid            : %dn"
  575.               "Peer died           : %dn"
  576.               "Peer copy mode      : %dn"
  577.               "Peer copy range     : %Zun"
  578.               "Queue length        : %dn"
  579.               "Queue max. length   : %dn"
  580.               "Queue flushing      : %dn"
  581.               "Queue terminate     : %dn",
  582.               nlq6->peer.pid,
  583.               nlq6->peer.died,
  584.               nlq6->peer.copy_mode,
  585.               nlq6->peer.copy_range,
  586.               nlq6->len,
  587.               *nlq6->maxlen,
  588.               nlq6->flushing,
  589.               nlq6->terminate);
  590. spin_unlock_bh(&nlq6->lock);
  591. *start = buffer + offset;
  592. len -= offset;
  593. if (len > length)
  594. len = length;
  595. else if (len < 0)
  596. len = 0;
  597. return len;
  598. }
  599. /****************************************************************************
  600.  *
  601.  * Module stuff.
  602.  *
  603.  ****************************************************************************/
  604. static int __init init(void)
  605. {
  606. int status = 0;
  607. struct proc_dir_entry *proc;
  608.         /* We must create the NETLINK_IP6_FW protocol service */
  609. nfnl = netlink_kernel_create(NETLINK_IP6_FW, netlink_receive_user_sk);
  610. if (nfnl == NULL) {
  611. printk(KERN_ERR "ip6_queue: initialisation failed: unable to "
  612.        "create kernel netlink socketn");
  613. return -ENOMEM;
  614. }
  615. nlq6 = ip6q_create_queue(netfilter6_receive,
  616.                        netlink_send_peer, &status, &sysctl_maxlen);
  617. if (nlq6 == NULL) {
  618. printk(KERN_ERR "ip6_queue: initialisation failed: unable to "
  619.        "create queuen");
  620. sock_release(nfnl->socket);
  621. return status;
  622. }
  623.         /* The file will be /proc/net/ip6_queue */
  624. proc = proc_net_create(IPQ_PROC_FS_NAME, 0, ip6q_get_info);
  625. if (proc) proc->owner = THIS_MODULE;
  626. else {
  627. ip6q_destroy_queue(nlq6);
  628. sock_release(nfnl->socket);
  629. return -ENOMEM;
  630. }
  631. register_netdevice_notifier(&ip6q_dev_notifier);
  632. ip6q_sysctl_header = register_sysctl_table(ip6q_root_table, 0);
  633. return status;
  634. }
  635. static void __exit fini(void)
  636. {
  637. unregister_sysctl_table(ip6q_sysctl_header);
  638. proc_net_remove(IPQ_PROC_FS_NAME);
  639. unregister_netdevice_notifier(&ip6q_dev_notifier);
  640. ip6q_destroy_queue(nlq6);
  641. sock_release(nfnl->socket);
  642. }
  643. MODULE_DESCRIPTION("IPv6 packet queue handler");
  644. MODULE_LICENSE("GPL");
  645. module_init(init);
  646. module_exit(fini);