📄 ip6_queue.c
字号:
switch (type) { case IPQM_MODE: switch (m->msg.mode.value) { case IPQ_COPY_META: q->peer.copy_mode = IPQ_COPY_META; q->peer.copy_range = 0; break; case IPQ_COPY_PACKET: q->peer.copy_mode = IPQ_COPY_PACKET; q->peer.copy_range = m->msg.mode.range; if (q->peer.copy_range > 0xFFFF) q->peer.copy_range = 0xFFFF; break; default: status = -EINVAL; } break; case IPQM_VERDICT: if (m->msg.verdict.value > NF_MAX_VERDICT) status = -EINVAL; else status = ip6q_set_verdict(q, &m->msg.verdict, len - sizeof(*m)); break; default: status = -EINVAL; } return status;}static inline int dev_cmp(ip6q_queue_element_t *e, unsigned long ifindex){ if (e->info->indev) if (e->info->indev->ifindex == ifindex) return 1; if (e->info->outdev) if (e->info->outdev->ifindex == ifindex) return 1; return 0;}/* Drop any queued packets associated with device ifindex */static void ip6q_dev_drop(ip6q_queue_t *q, int ifindex){ ip6q_queue_element_t *e; while ((e = ip6q_dequeue(q, dev_cmp, ifindex))) { e->verdict = NF_DROP; nf_reinject(e->skb, e->info, e->verdict); kfree(e); }}/**************************************************************************** * * Netfilter interface * ****************************************************************************//* * Packets arrive here from netfilter for queuing to userspace. * All of them must be fed back via nf_reinject() or Alexey will kill Rusty. */static int netfilter6_receive(struct sk_buff *skb, struct nf_info *info, void *data){ return ip6q_enqueue((ip6q_queue_t *)data, skb, info);}/**************************************************************************** * * Netlink interface. * ****************************************************************************/static struct sock *nfnl = NULL;/* This is not a static one, so we should not repeat its name */ip6q_queue_t *nlq6 = NULL;static struct sk_buff *netlink_build_message(ip6q_queue_element_t *e, int *errp){ unsigned char *old_tail; size_t size = 0; size_t data_len = 0; struct sk_buff *skb; ipq_packet_msg_t *pm; struct nlmsghdr *nlh; switch (nlq6->peer.copy_mode) { size_t copy_range; case IPQ_COPY_META: size = NLMSG_SPACE(sizeof(*pm)); data_len = 0; break; case IPQ_COPY_PACKET: copy_range = nlq6->peer.copy_range; if (copy_range == 0 || copy_range > e->skb->len) data_len = e->skb->len; else data_len = copy_range; size = NLMSG_SPACE(sizeof(*pm) + data_len); break; case IPQ_COPY_NONE: default: *errp = -EINVAL; return NULL; } skb = alloc_skb(size, GFP_ATOMIC); if (!skb) goto nlmsg_failure; old_tail = skb->tail; nlh = NLMSG_PUT(skb, 0, 0, IPQM_PACKET, size - sizeof(*nlh)); pm = NLMSG_DATA(nlh); memset(pm, 0, sizeof(*pm)); pm->packet_id = (unsigned long )e; pm->data_len = data_len; pm->timestamp_sec = e->skb->stamp.tv_sec; pm->timestamp_usec = e->skb->stamp.tv_usec; pm->mark = e->skb->nfmark; pm->hook = e->info->hook; if (e->info->indev) strcpy(pm->indev_name, e->info->indev->name); else pm->indev_name[0] = '\0'; if (e->info->outdev) strcpy(pm->outdev_name, e->info->outdev->name); else pm->outdev_name[0] = '\0'; pm->hw_protocol = e->skb->protocol; if (e->info->indev && e->skb->dev) { pm->hw_type = e->skb->dev->type; if (e->skb->dev->hard_header_parse) pm->hw_addrlen = e->skb->dev->hard_header_parse(e->skb, pm->hw_addr); } if (data_len) memcpy(pm->payload, e->skb->data, data_len); nlh->nlmsg_len = skb->tail - old_tail; NETLINK_CB(skb).dst_groups = 0; return skb;nlmsg_failure: if (skb) kfree_skb(skb); *errp = 0; printk(KERN_ERR "ip6_queue: error creating netlink message\n"); return NULL;}static int netlink_send_peer(ip6q_queue_element_t *e){ int status = 0; struct sk_buff *skb; skb = netlink_build_message(e, &status); if (skb == NULL) return status; return netlink_unicast(nfnl, skb, nlq6->peer.pid, MSG_DONTWAIT);}#define RCV_SKB_FAIL(err) do { netlink_ack(skb, nlh, (err)); return; } while (0);static __inline__ void netlink_receive_user_skb(struct sk_buff *skb){ int status, type; struct nlmsghdr *nlh; if (skb->len < sizeof(struct nlmsghdr)) return; nlh = (struct nlmsghdr *)skb->data; if (nlh->nlmsg_len < sizeof(struct nlmsghdr) || skb->len < nlh->nlmsg_len) return; if(nlh->nlmsg_pid <= 0 || !(nlh->nlmsg_flags & NLM_F_REQUEST) || nlh->nlmsg_flags & NLM_F_MULTI) RCV_SKB_FAIL(-EINVAL); if (nlh->nlmsg_flags & MSG_TRUNC) RCV_SKB_FAIL(-ECOMM); type = nlh->nlmsg_type; if (type < NLMSG_NOOP || type >= IPQM_MAX) RCV_SKB_FAIL(-EINVAL); if (type <= IPQM_BASE) return; if(!cap_raised(NETLINK_CB(skb).eff_cap, CAP_NET_ADMIN)) RCV_SKB_FAIL(-EPERM); if (nlq6->peer.pid && !nlq6->peer.died && (nlq6->peer.pid != nlh->nlmsg_pid)) { printk(KERN_WARNING "ip6_queue: peer pid changed from %d to " "%d, flushing queue\n", nlq6->peer.pid, nlh->nlmsg_pid); ip6q_flush(nlq6); } nlq6->peer.pid = nlh->nlmsg_pid; nlq6->peer.died = 0; status = ip6q_receive_peer(nlq6, NLMSG_DATA(nlh), type, skb->len - NLMSG_LENGTH(0)); if (status < 0) RCV_SKB_FAIL(status); if (nlh->nlmsg_flags & NLM_F_ACK) netlink_ack(skb, nlh, 0); return;}/* Note: we are only dealing with single part messages at the moment. */static void netlink_receive_user_sk(struct sock *sk, int len){ do { struct sk_buff *skb; if (rtnl_shlock_nowait()) return; while ((skb = skb_dequeue(&sk->receive_queue)) != NULL) { netlink_receive_user_skb(skb); kfree_skb(skb); } up(&rtnl_sem); } while (nfnl && nfnl->receive_queue.qlen);}/**************************************************************************** * * System events * ****************************************************************************/static int receive_event(struct notifier_block *this, unsigned long event, void *ptr){ struct net_device *dev = ptr; /* Drop any packets associated with the downed device */ if (event == NETDEV_DOWN) ip6q_dev_drop(nlq6, dev->ifindex); return NOTIFY_DONE;}struct notifier_block ip6q_dev_notifier = { receive_event, NULL, 0};/**************************************************************************** * * Sysctl - queue tuning. * ****************************************************************************/static int sysctl_maxlen = IPQ_QMAX_DEFAULT;static struct ctl_table_header *ip6q_sysctl_header;static ctl_table ip6q_table[] = { { NET_IPQ_QMAX, NET_IPQ_QMAX_NAME, &sysctl_maxlen, sizeof(sysctl_maxlen), 0644, NULL, proc_dointvec }, { 0 }};static ctl_table ip6q_dir_table[] = { {NET_IPV6, "ipv6", NULL, 0, 0555, ip6q_table, 0, 0, 0, 0, 0}, { 0 }};static ctl_table ip6q_root_table[] = { {CTL_NET, "net", NULL, 0, 0555, ip6q_dir_table, 0, 0, 0, 0, 0}, { 0 }};/**************************************************************************** * * Procfs - debugging info. * ****************************************************************************/static int ip6q_get_info(char *buffer, char **start, off_t offset, int length){ int len; spin_lock_bh(&nlq6->lock); len = sprintf(buffer, "Peer pid : %d\n" "Peer died : %d\n" "Peer copy mode : %d\n" "Peer copy range : %Zu\n" "Queue length : %d\n" "Queue max. length : %d\n" "Queue flushing : %d\n" "Queue terminate : %d\n", nlq6->peer.pid, nlq6->peer.died, nlq6->peer.copy_mode, nlq6->peer.copy_range, nlq6->len, *nlq6->maxlen, nlq6->flushing, nlq6->terminate); spin_unlock_bh(&nlq6->lock); *start = buffer + offset; len -= offset; if (len > length) len = length; else if (len < 0) len = 0; return len;}/**************************************************************************** * * Module stuff. * ****************************************************************************/static int __init init(void){ int status = 0; struct proc_dir_entry *proc; /* We must create the NETLINK_IP6_FW protocol service */ nfnl = netlink_kernel_create(NETLINK_IP6_FW, netlink_receive_user_sk); if (nfnl == NULL) { printk(KERN_ERR "ip6_queue: initialisation failed: unable to " "create kernel netlink socket\n"); return -ENOMEM; } nlq6 = ip6q_create_queue(netfilter6_receive, netlink_send_peer, &status, &sysctl_maxlen); if (nlq6 == NULL) { printk(KERN_ERR "ip6_queue: initialisation failed: unable to " "create queue\n"); sock_release(nfnl->socket); return status; } /* The file will be /proc/net/ip6_queue */ proc = proc_net_create(IPQ_PROC_FS_NAME, 0, ip6q_get_info); if (proc) proc->owner = THIS_MODULE; else { ip6q_destroy_queue(nlq6); sock_release(nfnl->socket); return -ENOMEM; } register_netdevice_notifier(&ip6q_dev_notifier); ip6q_sysctl_header = register_sysctl_table(ip6q_root_table, 0); return status;}static void __exit fini(void){ unregister_sysctl_table(ip6q_sysctl_header); proc_net_remove(IPQ_PROC_FS_NAME); unregister_netdevice_notifier(&ip6q_dev_notifier); ip6q_destroy_queue(nlq6); sock_release(nfnl->socket);}MODULE_DESCRIPTION("IPv6 packet queue handler");MODULE_LICENSE("GPL");module_init(init);module_exit(fini);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -