libertas: don't re-initialise cmdnode when taking it off the free queue
[linux-2.6/kvm.git] / net / netfilter / xt_owner.c
blobd382f9cc38b0b5fbf90c4af50f4c5b5a2c7b2e65
1 /*
2 * Kernel module to match various things tied to sockets associated with
3 * locally generated outgoing packets.
5 * (C) 2000 Marc Boucher <marc@mbsi.ca>
7 * Copyright © CC Computer Consultants GmbH, 2007
8 * Contact: <jengelh@computergmbh.de>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2 as
12 * published by the Free Software Foundation.
14 #include <linux/module.h>
15 #include <linux/skbuff.h>
16 #include <linux/file.h>
17 #include <net/sock.h>
18 #include <linux/netfilter/x_tables.h>
19 #include <linux/netfilter/xt_owner.h>
20 #include <linux/netfilter_ipv4/ipt_owner.h>
21 #include <linux/netfilter_ipv6/ip6t_owner.h>
23 static bool
24 owner_mt_v0(const struct sk_buff *skb, const struct net_device *in,
25 const struct net_device *out, const struct xt_match *match,
26 const void *matchinfo, int offset, unsigned int protoff,
27 bool *hotdrop)
29 const struct ipt_owner_info *info = matchinfo;
30 const struct file *filp;
32 if (skb->sk == NULL || skb->sk->sk_socket == NULL)
33 return false;
35 filp = skb->sk->sk_socket->file;
36 if (filp == NULL)
37 return false;
39 if (info->match & IPT_OWNER_UID)
40 if ((filp->f_uid != info->uid) ^
41 !!(info->invert & IPT_OWNER_UID))
42 return false;
44 if (info->match & IPT_OWNER_GID)
45 if ((filp->f_gid != info->gid) ^
46 !!(info->invert & IPT_OWNER_GID))
47 return false;
49 return true;
52 static bool
53 owner_mt6_v0(const struct sk_buff *skb, const struct net_device *in,
54 const struct net_device *out, const struct xt_match *match,
55 const void *matchinfo, int offset, unsigned int protoff,
56 bool *hotdrop)
58 const struct ip6t_owner_info *info = matchinfo;
59 const struct file *filp;
61 if (skb->sk == NULL || skb->sk->sk_socket == NULL)
62 return false;
64 filp = skb->sk->sk_socket->file;
65 if (filp == NULL)
66 return false;
68 if (info->match & IP6T_OWNER_UID)
69 if ((filp->f_uid != info->uid) ^
70 !!(info->invert & IP6T_OWNER_UID))
71 return false;
73 if (info->match & IP6T_OWNER_GID)
74 if ((filp->f_gid != info->gid) ^
75 !!(info->invert & IP6T_OWNER_GID))
76 return false;
78 return true;
81 static bool
82 owner_mt(const struct sk_buff *skb, const struct net_device *in,
83 const struct net_device *out, const struct xt_match *match,
84 const void *matchinfo, int offset, unsigned int protoff,
85 bool *hotdrop)
87 const struct xt_owner_match_info *info = matchinfo;
88 const struct file *filp;
90 if (skb->sk == NULL || skb->sk->sk_socket == NULL)
91 return (info->match ^ info->invert) == 0;
92 else if (info->match & info->invert & XT_OWNER_SOCKET)
94 * Socket exists but user wanted ! --socket-exists.
95 * (Single ampersands intended.)
97 return false;
99 filp = skb->sk->sk_socket->file;
100 if (filp == NULL)
101 return ((info->match ^ info->invert) &
102 (XT_OWNER_UID | XT_OWNER_GID)) == 0;
104 if (info->match & XT_OWNER_UID)
105 if ((filp->f_uid != info->uid) ^
106 !!(info->invert & XT_OWNER_UID))
107 return false;
109 if (info->match & XT_OWNER_GID)
110 if ((filp->f_gid != info->gid) ^
111 !!(info->invert & XT_OWNER_GID))
112 return false;
114 return true;
117 static bool
118 owner_mt_check_v0(const char *tablename, const void *ip,
119 const struct xt_match *match, void *matchinfo,
120 unsigned int hook_mask)
122 const struct ipt_owner_info *info = matchinfo;
124 if (info->match & (IPT_OWNER_PID | IPT_OWNER_SID | IPT_OWNER_COMM)) {
125 printk(KERN_WARNING KBUILD_MODNAME
126 ": PID, SID and command matching is not "
127 "supported anymore\n");
128 return false;
131 return true;
134 static bool
135 owner_mt6_check_v0(const char *tablename, const void *ip,
136 const struct xt_match *match, void *matchinfo,
137 unsigned int hook_mask)
139 const struct ip6t_owner_info *info = matchinfo;
141 if (info->match & (IP6T_OWNER_PID | IP6T_OWNER_SID)) {
142 printk(KERN_WARNING KBUILD_MODNAME
143 ": PID and SID matching is not supported anymore\n");
144 return false;
147 return true;
150 static struct xt_match owner_mt_reg[] __read_mostly = {
152 .name = "owner",
153 .revision = 0,
154 .family = AF_INET,
155 .match = owner_mt_v0,
156 .matchsize = sizeof(struct ipt_owner_info),
157 .checkentry = owner_mt_check_v0,
158 .hooks = (1 << NF_INET_LOCAL_OUT) |
159 (1 << NF_INET_POST_ROUTING),
160 .me = THIS_MODULE,
163 .name = "owner",
164 .revision = 0,
165 .family = AF_INET6,
166 .match = owner_mt6_v0,
167 .matchsize = sizeof(struct ip6t_owner_info),
168 .checkentry = owner_mt6_check_v0,
169 .hooks = (1 << NF_INET_LOCAL_OUT) |
170 (1 << NF_INET_POST_ROUTING),
171 .me = THIS_MODULE,
174 .name = "owner",
175 .revision = 1,
176 .family = AF_INET,
177 .match = owner_mt,
178 .matchsize = sizeof(struct xt_owner_match_info),
179 .hooks = (1 << NF_INET_LOCAL_OUT) |
180 (1 << NF_INET_POST_ROUTING),
181 .me = THIS_MODULE,
184 .name = "owner",
185 .revision = 1,
186 .family = AF_INET6,
187 .match = owner_mt,
188 .matchsize = sizeof(struct xt_owner_match_info),
189 .hooks = (1 << NF_INET_LOCAL_OUT) |
190 (1 << NF_INET_POST_ROUTING),
191 .me = THIS_MODULE,
195 static int __init owner_mt_init(void)
197 return xt_register_matches(owner_mt_reg, ARRAY_SIZE(owner_mt_reg));
200 static void __exit owner_mt_exit(void)
202 xt_unregister_matches(owner_mt_reg, ARRAY_SIZE(owner_mt_reg));
205 module_init(owner_mt_init);
206 module_exit(owner_mt_exit);
207 MODULE_AUTHOR("Jan Engelhardt <jengelh@computergmbh.de>");
208 MODULE_DESCRIPTION("Xtables: socket owner matching");
209 MODULE_LICENSE("GPL");
210 MODULE_ALIAS("ipt_owner");
211 MODULE_ALIAS("ip6t_owner");