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 - 2008
8 * <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>
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>
24 owner_mt_v0(const struct sk_buff
*skb
, const struct xt_match_param
*par
)
26 const struct ipt_owner_info
*info
= par
->matchinfo
;
27 const struct file
*filp
;
29 if (skb
->sk
== NULL
|| skb
->sk
->sk_socket
== NULL
)
32 filp
= skb
->sk
->sk_socket
->file
;
36 if (info
->match
& IPT_OWNER_UID
)
37 if ((filp
->f_cred
->fsuid
!= info
->uid
) ^
38 !!(info
->invert
& IPT_OWNER_UID
))
41 if (info
->match
& IPT_OWNER_GID
)
42 if ((filp
->f_cred
->fsgid
!= info
->gid
) ^
43 !!(info
->invert
& IPT_OWNER_GID
))
50 owner_mt6_v0(const struct sk_buff
*skb
, const struct xt_match_param
*par
)
52 const struct ip6t_owner_info
*info
= par
->matchinfo
;
53 const struct file
*filp
;
55 if (skb
->sk
== NULL
|| skb
->sk
->sk_socket
== NULL
)
58 filp
= skb
->sk
->sk_socket
->file
;
62 if (info
->match
& IP6T_OWNER_UID
)
63 if ((filp
->f_cred
->fsuid
!= info
->uid
) ^
64 !!(info
->invert
& IP6T_OWNER_UID
))
67 if (info
->match
& IP6T_OWNER_GID
)
68 if ((filp
->f_cred
->fsgid
!= info
->gid
) ^
69 !!(info
->invert
& IP6T_OWNER_GID
))
76 owner_mt(const struct sk_buff
*skb
, const struct xt_match_param
*par
)
78 const struct xt_owner_match_info
*info
= par
->matchinfo
;
79 const struct file
*filp
;
81 if (skb
->sk
== NULL
|| skb
->sk
->sk_socket
== NULL
)
82 return (info
->match
^ info
->invert
) == 0;
83 else if (info
->match
& info
->invert
& XT_OWNER_SOCKET
)
85 * Socket exists but user wanted ! --socket-exists.
86 * (Single ampersands intended.)
90 filp
= skb
->sk
->sk_socket
->file
;
92 return ((info
->match
^ info
->invert
) &
93 (XT_OWNER_UID
| XT_OWNER_GID
)) == 0;
95 if (info
->match
& XT_OWNER_UID
)
96 if ((filp
->f_cred
->fsuid
>= info
->uid_min
&&
97 filp
->f_cred
->fsuid
<= info
->uid_max
) ^
98 !(info
->invert
& XT_OWNER_UID
))
101 if (info
->match
& XT_OWNER_GID
)
102 if ((filp
->f_cred
->fsgid
>= info
->gid_min
&&
103 filp
->f_cred
->fsgid
<= info
->gid_max
) ^
104 !(info
->invert
& XT_OWNER_GID
))
110 static bool owner_mt_check_v0(const struct xt_mtchk_param
*par
)
112 const struct ipt_owner_info
*info
= par
->matchinfo
;
114 if (info
->match
& (IPT_OWNER_PID
| IPT_OWNER_SID
| IPT_OWNER_COMM
)) {
115 printk(KERN_WARNING KBUILD_MODNAME
116 ": PID, SID and command matching is not "
117 "supported anymore\n");
124 static bool owner_mt6_check_v0(const struct xt_mtchk_param
*par
)
126 const struct ip6t_owner_info
*info
= par
->matchinfo
;
128 if (info
->match
& (IP6T_OWNER_PID
| IP6T_OWNER_SID
)) {
129 printk(KERN_WARNING KBUILD_MODNAME
130 ": PID and SID matching is not supported anymore\n");
137 static struct xt_match owner_mt_reg
[] __read_mostly
= {
141 .family
= NFPROTO_IPV4
,
142 .match
= owner_mt_v0
,
143 .matchsize
= sizeof(struct ipt_owner_info
),
144 .checkentry
= owner_mt_check_v0
,
145 .hooks
= (1 << NF_INET_LOCAL_OUT
) |
146 (1 << NF_INET_POST_ROUTING
),
152 .family
= NFPROTO_IPV6
,
153 .match
= owner_mt6_v0
,
154 .matchsize
= sizeof(struct ip6t_owner_info
),
155 .checkentry
= owner_mt6_check_v0
,
156 .hooks
= (1 << NF_INET_LOCAL_OUT
) |
157 (1 << NF_INET_POST_ROUTING
),
163 .family
= NFPROTO_UNSPEC
,
165 .matchsize
= sizeof(struct xt_owner_match_info
),
166 .hooks
= (1 << NF_INET_LOCAL_OUT
) |
167 (1 << NF_INET_POST_ROUTING
),
172 static int __init
owner_mt_init(void)
174 return xt_register_matches(owner_mt_reg
, ARRAY_SIZE(owner_mt_reg
));
177 static void __exit
owner_mt_exit(void)
179 xt_unregister_matches(owner_mt_reg
, ARRAY_SIZE(owner_mt_reg
));
182 module_init(owner_mt_init
);
183 module_exit(owner_mt_exit
);
184 MODULE_AUTHOR("Jan Engelhardt <jengelh@computergmbh.de>");
185 MODULE_DESCRIPTION("Xtables: socket owner matching");
186 MODULE_LICENSE("GPL");
187 MODULE_ALIAS("ipt_owner");
188 MODULE_ALIAS("ip6t_owner");