📄 ip6_queue.c
字号:
return -ENOMEM; } if (e->skb->sk) skb_set_owner_w(newskb, e->skb->sk); kfree_skb(e->skb); e->skb = newskb; } skb_put(e->skb, diff); } memcpy(e->skb->data, v->payload, v->data_len); e->skb->nfcache |= NFC_ALTERED; /* * Extra routing may needed on local out, as the QUEUE target never * returns control to the table. * Not a nice way to cmp, but works */ if (e->info->hook == NF_IP_LOCAL_OUT) { struct ipv6hdr *iph = e->skb->nh.ipv6h; if (ipv6_addr_cmp(&iph->daddr, &e->rt_info.daddr) || ipv6_addr_cmp(&iph->saddr, &e->rt_info.saddr)) return ip6_route_me_harder(e->skb); } return 0;}static inline intid_cmp(struct ipq_queue_entry *e, unsigned long id){ return (id == (unsigned long )e);}static intipq_set_verdict(struct ipq_verdict_msg *vmsg, unsigned int len){ struct ipq_queue_entry *entry; if (vmsg->value > NF_MAX_VERDICT) return -EINVAL; entry = ipq_find_dequeue_entry(id_cmp, vmsg->id); if (entry == NULL) return -ENOENT; else { int verdict = vmsg->value; if (vmsg->data_len && vmsg->data_len == len) if (ipq_mangle_ipv6(vmsg, entry) < 0) verdict = NF_DROP; ipq_issue_verdict(entry, verdict); return 0; }}static intipq_set_mode(unsigned char mode, unsigned int range){ int status; write_lock_bh(&queue_lock); status = __ipq_set_mode(mode, range); write_unlock_bh(&queue_lock); return status;}static intipq_receive_peer(struct ipq_peer_msg *pmsg, unsigned char type, unsigned int len){ int status = 0; if (len < sizeof(*pmsg)) return -EINVAL; switch (type) { case IPQM_MODE: status = ipq_set_mode(pmsg->msg.mode.value, pmsg->msg.mode.range); break; case IPQM_VERDICT: if (pmsg->msg.verdict.value > NF_MAX_VERDICT) status = -EINVAL; else status = ipq_set_verdict(&pmsg->msg.verdict, len - sizeof(*pmsg)); break; default: status = -EINVAL; } return status;}static intdev_cmp(struct ipq_queue_entry *entry, unsigned long ifindex){ if (entry->info->indev) if (entry->info->indev->ifindex == ifindex) return 1; if (entry->info->outdev) if (entry->info->outdev->ifindex == ifindex) return 1; return 0;}static voidipq_dev_drop(int ifindex){ struct ipq_queue_entry *entry; while ((entry = ipq_find_dequeue_entry(dev_cmp, ifindex)) != NULL) ipq_issue_verdict(entry, NF_DROP);}#define RCV_SKB_FAIL(err) do { netlink_ack(skb, nlh, (err)); return; } while (0)static inline voidipq_rcv_skb(struct sk_buff *skb){ int status, type, pid, flags, nlmsglen, skblen; struct nlmsghdr *nlh; skblen = skb->len; if (skblen < sizeof(*nlh)) return; nlh = (struct nlmsghdr *)skb->data; nlmsglen = nlh->nlmsg_len; if (nlmsglen < sizeof(*nlh) || skblen < nlmsglen) return; pid = nlh->nlmsg_pid; flags = nlh->nlmsg_flags; if(pid <= 0 || !(flags & NLM_F_REQUEST) || flags & NLM_F_MULTI) RCV_SKB_FAIL(-EINVAL); if (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); write_lock_bh(&queue_lock); if (peer_pid) { if (peer_pid != pid) { write_unlock_bh(&queue_lock); RCV_SKB_FAIL(-EBUSY); } } else peer_pid = pid; write_unlock_bh(&queue_lock); status = ipq_receive_peer(NLMSG_DATA(nlh), type, skblen - NLMSG_LENGTH(0)); if (status < 0) RCV_SKB_FAIL(status); if (flags & NLM_F_ACK) netlink_ack(skb, nlh, 0); return;}static voidipq_rcv_sk(struct sock *sk, int len){ do { struct sk_buff *skb; if (down_trylock(&ipqnl_sem)) return; while ((skb = skb_dequeue(&sk->receive_queue)) != NULL) { ipq_rcv_skb(skb); kfree_skb(skb); } up(&ipqnl_sem); } while (ipqnl && ipqnl->receive_queue.qlen);}static intipq_rcv_dev_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) ipq_dev_drop(dev->ifindex); return NOTIFY_DONE;}static struct notifier_block ipq_dev_notifier = { ipq_rcv_dev_event, NULL, 0};static intipq_rcv_nl_event(struct notifier_block *this, unsigned long event, void *ptr){ struct netlink_notify *n = ptr; if (event == NETLINK_URELEASE && n->protocol == NETLINK_IP6_FW && n->pid) { write_lock_bh(&queue_lock); if (n->pid == peer_pid) __ipq_reset(); write_unlock_bh(&queue_lock); } return NOTIFY_DONE;}static struct notifier_block ipq_nl_notifier = { ipq_rcv_nl_event, NULL, 0};static struct ctl_table_header *ipq_sysctl_header;static ctl_table ipq_table[] = { { NET_IPQ_QMAX, NET_IPQ_QMAX_NAME, &queue_maxlen, sizeof(queue_maxlen), 0644, NULL, proc_dointvec }, { 0 }};static ctl_table ipq_dir_table[] = { {NET_IPV6, "ipv6", NULL, 0, 0555, ipq_table, 0, 0, 0, 0, 0}, { 0 }};static ctl_table ipq_root_table[] = { {CTL_NET, "net", NULL, 0, 0555, ipq_dir_table, 0, 0, 0, 0, 0}, { 0 }};static intipq_get_info(char *buffer, char **start, off_t offset, int length){ int len; read_lock_bh(&queue_lock); len = sprintf(buffer, "Peer PID : %d\n" "Copy mode : %hu\n" "Copy range : %u\n" "Queue length : %u\n" "Queue max. length : %u\n", peer_pid, copy_mode, copy_range, queue_total, queue_maxlen); read_unlock_bh(&queue_lock); *start = buffer + offset; len -= offset; if (len > length) len = length; else if (len < 0) len = 0; return len;}static intinit_or_cleanup(int init){ int status = -ENOMEM; struct proc_dir_entry *proc; if (!init) goto cleanup; netlink_register_notifier(&ipq_nl_notifier); ipqnl = netlink_kernel_create(NETLINK_IP6_FW, ipq_rcv_sk); if (ipqnl == NULL) { printk(KERN_ERR "ip6_queue: failed to create netlink socket\n"); goto cleanup_netlink_notifier; } proc = proc_net_create(IPQ_PROC_FS_NAME, 0, ipq_get_info); if (proc) proc->owner = THIS_MODULE; else { printk(KERN_ERR "ip6_queue: failed to create proc entry\n"); goto cleanup_ipqnl; } register_netdevice_notifier(&ipq_dev_notifier); ipq_sysctl_header = register_sysctl_table(ipq_root_table, 0); status = nf_register_queue_handler(PF_INET6, ipq_enqueue_packet, NULL); if (status < 0) { printk(KERN_ERR "ip6_queue: failed to register queue handler\n"); goto cleanup_sysctl; } return status;cleanup: nf_unregister_queue_handler(PF_INET6); br_write_lock_bh(BR_NETPROTO_LOCK); br_write_unlock_bh(BR_NETPROTO_LOCK); ipq_flush(NF_DROP); cleanup_sysctl: unregister_sysctl_table(ipq_sysctl_header); unregister_netdevice_notifier(&ipq_dev_notifier); proc_net_remove(IPQ_PROC_FS_NAME); cleanup_ipqnl: sock_release(ipqnl->socket); down(&ipqnl_sem); up(&ipqnl_sem); cleanup_netlink_notifier: netlink_unregister_notifier(&ipq_nl_notifier); return status;}static int __init init(void){ return init_or_cleanup(1);}static void __exit fini(void){ init_or_cleanup(0);}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 + -