Merge master.kernel.org:/home/rmk/linux-2.6-arm
[pandora-kernel.git] / net / ipv4 / netfilter / ipt_owner.c
1 /* Kernel module to match various things tied to sockets associated with
2    locally generated outgoing packets. */
3
4 /* (C) 2000 Marc Boucher <marc@mbsi.ca>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 as
8  * published by the Free Software Foundation.
9  */
10
11 #include <linux/module.h>
12 #include <linux/skbuff.h>
13 #include <linux/file.h>
14 #include <linux/rcupdate.h>
15 #include <net/sock.h>
16
17 #include <linux/netfilter_ipv4/ipt_owner.h>
18 #include <linux/netfilter_ipv4/ip_tables.h>
19
20 MODULE_LICENSE("GPL");
21 MODULE_AUTHOR("Marc Boucher <marc@mbsi.ca>");
22 MODULE_DESCRIPTION("iptables owner match");
23
24 static int
25 match(const struct sk_buff *skb,
26       const struct net_device *in,
27       const struct net_device *out,
28       const void *matchinfo,
29       int offset,
30       int *hotdrop)
31 {
32         const struct ipt_owner_info *info = matchinfo;
33
34         if (!skb->sk || !skb->sk->sk_socket || !skb->sk->sk_socket->file)
35                 return 0;
36
37         if(info->match & IPT_OWNER_UID) {
38                 if ((skb->sk->sk_socket->file->f_uid != info->uid) ^
39                     !!(info->invert & IPT_OWNER_UID))
40                         return 0;
41         }
42
43         if(info->match & IPT_OWNER_GID) {
44                 if ((skb->sk->sk_socket->file->f_gid != info->gid) ^
45                     !!(info->invert & IPT_OWNER_GID))
46                         return 0;
47         }
48
49         return 1;
50 }
51
52 static int
53 checkentry(const char *tablename,
54            const struct ipt_ip *ip,
55            void *matchinfo,
56            unsigned int matchsize,
57            unsigned int hook_mask)
58 {
59         const struct ipt_owner_info *info = matchinfo;
60
61         if (hook_mask
62             & ~((1 << NF_IP_LOCAL_OUT) | (1 << NF_IP_POST_ROUTING))) {
63                 printk("ipt_owner: only valid for LOCAL_OUT or POST_ROUTING.\n");
64                 return 0;
65         }
66
67         if (matchsize != IPT_ALIGN(sizeof(struct ipt_owner_info))) {
68                 printk("Matchsize %u != %Zu\n", matchsize,
69                        IPT_ALIGN(sizeof(struct ipt_owner_info)));
70                 return 0;
71         }
72
73         if (info->match & (IPT_OWNER_PID|IPT_OWNER_SID|IPT_OWNER_COMM)) {
74                 printk("ipt_owner: pid, sid and command matching "
75                        "not supported anymore\n");
76                 return 0;
77         }
78
79         return 1;
80 }
81
82 static struct ipt_match owner_match = {
83         .name           = "owner",
84         .match          = &match,
85         .checkentry     = &checkentry,
86         .me             = THIS_MODULE,
87 };
88
89 static int __init init(void)
90 {
91         return ipt_register_match(&owner_match);
92 }
93
94 static void __exit fini(void)
95 {
96         ipt_unregister_match(&owner_match);
97 }
98
99 module_init(init);
100 module_exit(fini);