~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/net/netfilter/xt_policy.c

Version: ~ [ linux-6.0-rc6 ] ~ [ linux-5.19.10 ] ~ [ linux-5.18.19 ] ~ [ linux-5.17.15 ] ~ [ linux-5.16.20 ] ~ [ linux-5.15.69 ] ~ [ linux-5.14.21 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.144 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.214 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.259 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.294 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.329 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.302 ] ~ [ linux-4.3.6 ] ~ [ linux-4.2.8 ] ~ [ linux-4.1.52 ] ~ [ linux-4.0.9 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.9 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  1 /* IP tables module for matching IPsec policy
  2  *
  3  * Copyright (c) 2004,2005 Patrick McHardy, <kaber@trash.net>
  4  *
  5  * This program is free software; you can redistribute it and/or modify
  6  * it under the terms of the GNU General Public License version 2 as
  7  * published by the Free Software Foundation.
  8  */
  9 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
 10 #include <linux/kernel.h>
 11 #include <linux/module.h>
 12 #include <linux/skbuff.h>
 13 #include <linux/init.h>
 14 #include <net/xfrm.h>
 15 
 16 #include <linux/netfilter.h>
 17 #include <linux/netfilter/xt_policy.h>
 18 #include <linux/netfilter/x_tables.h>
 19 
 20 MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
 21 MODULE_DESCRIPTION("Xtables: IPsec policy match");
 22 MODULE_LICENSE("GPL");
 23 
 24 static inline bool
 25 xt_addr_cmp(const union nf_inet_addr *a1, const union nf_inet_addr *m,
 26             const union nf_inet_addr *a2, unsigned short family)
 27 {
 28         switch (family) {
 29         case NFPROTO_IPV4:
 30                 return ((a1->ip ^ a2->ip) & m->ip) == 0;
 31         case NFPROTO_IPV6:
 32                 return ipv6_masked_addr_cmp(&a1->in6, &m->in6, &a2->in6) == 0;
 33         }
 34         return false;
 35 }
 36 
 37 static bool
 38 match_xfrm_state(const struct xfrm_state *x, const struct xt_policy_elem *e,
 39                  unsigned short family)
 40 {
 41 #define MATCH_ADDR(x,y,z)       (!e->match.x ||                        \
 42                                  (xt_addr_cmp(&e->x, &e->y, (const union nf_inet_addr *)(z), family) \
 43                                   ^ e->invert.x))
 44 #define MATCH(x,y)              (!e->match.x || ((e->x == (y)) ^ e->invert.x))
 45 
 46         return MATCH_ADDR(saddr, smask, &x->props.saddr) &&
 47                MATCH_ADDR(daddr, dmask, &x->id.daddr) &&
 48                MATCH(proto, x->id.proto) &&
 49                MATCH(mode, x->props.mode) &&
 50                MATCH(spi, x->id.spi) &&
 51                MATCH(reqid, x->props.reqid);
 52 }
 53 
 54 static int
 55 match_policy_in(const struct sk_buff *skb, const struct xt_policy_info *info,
 56                 unsigned short family)
 57 {
 58         const struct xt_policy_elem *e;
 59         const struct sec_path *sp = skb_sec_path(skb);
 60         int strict = info->flags & XT_POLICY_MATCH_STRICT;
 61         int i, pos;
 62 
 63         if (sp == NULL)
 64                 return -1;
 65         if (strict && info->len != sp->len)
 66                 return 0;
 67 
 68         for (i = sp->len - 1; i >= 0; i--) {
 69                 pos = strict ? i - sp->len + 1 : 0;
 70                 if (pos >= info->len)
 71                         return 0;
 72                 e = &info->pol[pos];
 73 
 74                 if (match_xfrm_state(sp->xvec[i], e, family)) {
 75                         if (!strict)
 76                                 return 1;
 77                 } else if (strict)
 78                         return 0;
 79         }
 80 
 81         return strict ? 1 : 0;
 82 }
 83 
 84 static int
 85 match_policy_out(const struct sk_buff *skb, const struct xt_policy_info *info,
 86                  unsigned short family)
 87 {
 88         const struct xt_policy_elem *e;
 89         const struct dst_entry *dst = skb_dst(skb);
 90         int strict = info->flags & XT_POLICY_MATCH_STRICT;
 91         int i, pos;
 92 
 93         if (dst->xfrm == NULL)
 94                 return -1;
 95 
 96         for (i = 0; dst && dst->xfrm;
 97              dst = ((struct xfrm_dst *)dst)->child, i++) {
 98                 pos = strict ? i : 0;
 99                 if (pos >= info->len)
100                         return 0;
101                 e = &info->pol[pos];
102 
103                 if (match_xfrm_state(dst->xfrm, e, family)) {
104                         if (!strict)
105                                 return 1;
106                 } else if (strict)
107                         return 0;
108         }
109 
110         return strict ? i == info->len : 0;
111 }
112 
113 static bool
114 policy_mt(const struct sk_buff *skb, struct xt_action_param *par)
115 {
116         const struct xt_policy_info *info = par->matchinfo;
117         int ret;
118 
119         if (info->flags & XT_POLICY_MATCH_IN)
120                 ret = match_policy_in(skb, info, xt_family(par));
121         else
122                 ret = match_policy_out(skb, info, xt_family(par));
123 
124         if (ret < 0)
125                 ret = info->flags & XT_POLICY_MATCH_NONE ? true : false;
126         else if (info->flags & XT_POLICY_MATCH_NONE)
127                 ret = false;
128 
129         return ret;
130 }
131 
132 static int policy_mt_check(const struct xt_mtchk_param *par)
133 {
134         const struct xt_policy_info *info = par->matchinfo;
135         const char *errmsg = "neither incoming nor outgoing policy selected";
136 
137         if (!(info->flags & (XT_POLICY_MATCH_IN|XT_POLICY_MATCH_OUT)))
138                 goto err;
139 
140         if (par->hook_mask & ((1 << NF_INET_PRE_ROUTING) |
141             (1 << NF_INET_LOCAL_IN)) && info->flags & XT_POLICY_MATCH_OUT) {
142                 errmsg = "output policy not valid in PREROUTING and INPUT";
143                 goto err;
144         }
145         if (par->hook_mask & ((1 << NF_INET_POST_ROUTING) |
146             (1 << NF_INET_LOCAL_OUT)) && info->flags & XT_POLICY_MATCH_IN) {
147                 errmsg = "input policy not valid in POSTROUTING and OUTPUT";
148                 goto err;
149         }
150         if (info->len > XT_POLICY_MAX_ELEM) {
151                 errmsg = "too many policy elements";
152                 goto err;
153         }
154         return 0;
155 err:
156         pr_info_ratelimited("%s\n", errmsg);
157         return -EINVAL;
158 }
159 
160 static struct xt_match policy_mt_reg[] __read_mostly = {
161         {
162                 .name           = "policy",
163                 .family         = NFPROTO_IPV4,
164                 .checkentry     = policy_mt_check,
165                 .match          = policy_mt,
166                 .matchsize      = sizeof(struct xt_policy_info),
167                 .me             = THIS_MODULE,
168         },
169         {
170                 .name           = "policy",
171                 .family         = NFPROTO_IPV6,
172                 .checkentry     = policy_mt_check,
173                 .match          = policy_mt,
174                 .matchsize      = sizeof(struct xt_policy_info),
175                 .me             = THIS_MODULE,
176         },
177 };
178 
179 static int __init policy_mt_init(void)
180 {
181         return xt_register_matches(policy_mt_reg, ARRAY_SIZE(policy_mt_reg));
182 }
183 
184 static void __exit policy_mt_exit(void)
185 {
186         xt_unregister_matches(policy_mt_reg, ARRAY_SIZE(policy_mt_reg));
187 }
188 
189 module_init(policy_mt_init);
190 module_exit(policy_mt_exit);
191 MODULE_ALIAS("ipt_policy");
192 MODULE_ALIAS("ip6t_policy");
193 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | Wiki (Japanese) | Wiki (English) | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

osdn.jp