Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /* IP tables module for matching IPsec policy
0003  *
0004  * Copyright (c) 2004,2005 Patrick McHardy, <kaber@trash.net>
0005  */
0006 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
0007 #include <linux/kernel.h>
0008 #include <linux/module.h>
0009 #include <linux/skbuff.h>
0010 #include <linux/init.h>
0011 #include <net/xfrm.h>
0012 
0013 #include <linux/netfilter.h>
0014 #include <linux/netfilter/xt_policy.h>
0015 #include <linux/netfilter/x_tables.h>
0016 
0017 MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
0018 MODULE_DESCRIPTION("Xtables: IPsec policy match");
0019 MODULE_LICENSE("GPL");
0020 
0021 static inline bool
0022 xt_addr_cmp(const union nf_inet_addr *a1, const union nf_inet_addr *m,
0023         const union nf_inet_addr *a2, unsigned short family)
0024 {
0025     switch (family) {
0026     case NFPROTO_IPV4:
0027         return ((a1->ip ^ a2->ip) & m->ip) == 0;
0028     case NFPROTO_IPV6:
0029         return ipv6_masked_addr_cmp(&a1->in6, &m->in6, &a2->in6) == 0;
0030     }
0031     return false;
0032 }
0033 
0034 static bool
0035 match_xfrm_state(const struct xfrm_state *x, const struct xt_policy_elem *e,
0036          unsigned short family)
0037 {
0038 #define MATCH_ADDR(x,y,z)   (!e->match.x ||                \
0039                  (xt_addr_cmp(&e->x, &e->y, (const union nf_inet_addr *)(z), family) \
0040                   ^ e->invert.x))
0041 #define MATCH(x,y)      (!e->match.x || ((e->x == (y)) ^ e->invert.x))
0042 
0043     return MATCH_ADDR(saddr, smask, &x->props.saddr) &&
0044            MATCH_ADDR(daddr, dmask, &x->id.daddr) &&
0045            MATCH(proto, x->id.proto) &&
0046            MATCH(mode, x->props.mode) &&
0047            MATCH(spi, x->id.spi) &&
0048            MATCH(reqid, x->props.reqid);
0049 }
0050 
0051 static int
0052 match_policy_in(const struct sk_buff *skb, const struct xt_policy_info *info,
0053         unsigned short family)
0054 {
0055     const struct xt_policy_elem *e;
0056     const struct sec_path *sp = skb_sec_path(skb);
0057     int strict = info->flags & XT_POLICY_MATCH_STRICT;
0058     int i, pos;
0059 
0060     if (sp == NULL)
0061         return -1;
0062     if (strict && info->len != sp->len)
0063         return 0;
0064 
0065     for (i = sp->len - 1; i >= 0; i--) {
0066         pos = strict ? i - sp->len + 1 : 0;
0067         if (pos >= info->len)
0068             return 0;
0069         e = &info->pol[pos];
0070 
0071         if (match_xfrm_state(sp->xvec[i], e, family)) {
0072             if (!strict)
0073                 return 1;
0074         } else if (strict)
0075             return 0;
0076     }
0077 
0078     return strict ? 1 : 0;
0079 }
0080 
0081 static int
0082 match_policy_out(const struct sk_buff *skb, const struct xt_policy_info *info,
0083          unsigned short family)
0084 {
0085     const struct xt_policy_elem *e;
0086     const struct dst_entry *dst = skb_dst(skb);
0087     int strict = info->flags & XT_POLICY_MATCH_STRICT;
0088     int i, pos;
0089 
0090     if (dst->xfrm == NULL)
0091         return -1;
0092 
0093     for (i = 0; dst && dst->xfrm;
0094          dst = ((struct xfrm_dst *)dst)->child, i++) {
0095         pos = strict ? i : 0;
0096         if (pos >= info->len)
0097             return 0;
0098         e = &info->pol[pos];
0099 
0100         if (match_xfrm_state(dst->xfrm, e, family)) {
0101             if (!strict)
0102                 return 1;
0103         } else if (strict)
0104             return 0;
0105     }
0106 
0107     return strict ? i == info->len : 0;
0108 }
0109 
0110 static bool
0111 policy_mt(const struct sk_buff *skb, struct xt_action_param *par)
0112 {
0113     const struct xt_policy_info *info = par->matchinfo;
0114     int ret;
0115 
0116     if (info->flags & XT_POLICY_MATCH_IN)
0117         ret = match_policy_in(skb, info, xt_family(par));
0118     else
0119         ret = match_policy_out(skb, info, xt_family(par));
0120 
0121     if (ret < 0)
0122         ret = info->flags & XT_POLICY_MATCH_NONE ? true : false;
0123     else if (info->flags & XT_POLICY_MATCH_NONE)
0124         ret = false;
0125 
0126     return ret;
0127 }
0128 
0129 static int policy_mt_check(const struct xt_mtchk_param *par)
0130 {
0131     const struct xt_policy_info *info = par->matchinfo;
0132     const char *errmsg = "neither incoming nor outgoing policy selected";
0133 
0134     if (!(info->flags & (XT_POLICY_MATCH_IN|XT_POLICY_MATCH_OUT)))
0135         goto err;
0136 
0137     if (par->hook_mask & ((1 << NF_INET_PRE_ROUTING) |
0138         (1 << NF_INET_LOCAL_IN)) && info->flags & XT_POLICY_MATCH_OUT) {
0139         errmsg = "output policy not valid in PREROUTING and INPUT";
0140         goto err;
0141     }
0142     if (par->hook_mask & ((1 << NF_INET_POST_ROUTING) |
0143         (1 << NF_INET_LOCAL_OUT)) && info->flags & XT_POLICY_MATCH_IN) {
0144         errmsg = "input policy not valid in POSTROUTING and OUTPUT";
0145         goto err;
0146     }
0147     if (info->len > XT_POLICY_MAX_ELEM) {
0148         errmsg = "too many policy elements";
0149         goto err;
0150     }
0151     return 0;
0152 err:
0153     pr_info_ratelimited("%s\n", errmsg);
0154     return -EINVAL;
0155 }
0156 
0157 static struct xt_match policy_mt_reg[] __read_mostly = {
0158     {
0159         .name       = "policy",
0160         .family     = NFPROTO_IPV4,
0161         .checkentry     = policy_mt_check,
0162         .match      = policy_mt,
0163         .matchsize  = sizeof(struct xt_policy_info),
0164         .me     = THIS_MODULE,
0165     },
0166     {
0167         .name       = "policy",
0168         .family     = NFPROTO_IPV6,
0169         .checkentry = policy_mt_check,
0170         .match      = policy_mt,
0171         .matchsize  = sizeof(struct xt_policy_info),
0172         .me     = THIS_MODULE,
0173     },
0174 };
0175 
0176 static int __init policy_mt_init(void)
0177 {
0178     return xt_register_matches(policy_mt_reg, ARRAY_SIZE(policy_mt_reg));
0179 }
0180 
0181 static void __exit policy_mt_exit(void)
0182 {
0183     xt_unregister_matches(policy_mt_reg, ARRAY_SIZE(policy_mt_reg));
0184 }
0185 
0186 module_init(policy_mt_init);
0187 module_exit(policy_mt_exit);
0188 MODULE_ALIAS("ipt_policy");
0189 MODULE_ALIAS("ip6t_policy");