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

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

Version: ~ [ linux-5.15-rc1 ] ~ [ linux-5.14.5 ] ~ [ linux-5.13.18 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.66 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.147 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.206 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.246 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.282 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.283 ] ~ [ linux-4.3.6 ] ~ [ linux-4.2.8 ] ~ [ linux-4.1.52 ] ~ [ linux-4.0.9 ] ~ [ linux-3.18.140 ] ~ [ linux-3.16.85 ] ~ [ linux-3.14.79 ] ~ [ linux-3.12.74 ] ~ [ 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.5 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  1 /* Kernel module to match one of a list of TCP/UDP(-Lite)/SCTP/DCCP ports:
  2    ports are in the same place so we can treat them as equal. */
  3 
  4 /* (C) 1999-2001 Paul `Rusty' Russell
  5  * (C) 2002-2004 Netfilter Core Team <coreteam@netfilter.org>
  6  *
  7  * This program is free software; you can redistribute it and/or modify
  8  * it under the terms of the GNU General Public License version 2 as
  9  * published by the Free Software Foundation.
 10  */
 11 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
 12 #include <linux/module.h>
 13 #include <linux/types.h>
 14 #include <linux/udp.h>
 15 #include <linux/skbuff.h>
 16 #include <linux/in.h>
 17 
 18 #include <linux/netfilter/xt_multiport.h>
 19 #include <linux/netfilter/x_tables.h>
 20 #include <linux/netfilter_ipv4/ip_tables.h>
 21 #include <linux/netfilter_ipv6/ip6_tables.h>
 22 
 23 MODULE_LICENSE("GPL");
 24 MODULE_AUTHOR("Netfilter Core Team <coreteam@netfilter.org>");
 25 MODULE_DESCRIPTION("Xtables: multiple port matching for TCP, UDP, UDP-Lite, SCTP and DCCP");
 26 MODULE_ALIAS("ipt_multiport");
 27 MODULE_ALIAS("ip6t_multiport");
 28 
 29 /* Returns 1 if the port is matched by the test, 0 otherwise. */
 30 static inline bool
 31 ports_match_v1(const struct xt_multiport_v1 *minfo,
 32                u_int16_t src, u_int16_t dst)
 33 {
 34         unsigned int i;
 35         u_int16_t s, e;
 36 
 37         for (i = 0; i < minfo->count; i++) {
 38                 s = minfo->ports[i];
 39 
 40                 if (minfo->pflags[i]) {
 41                         /* range port matching */
 42                         e = minfo->ports[++i];
 43                         pr_debug("src or dst matches with %d-%d?\n", s, e);
 44 
 45                         if (minfo->flags == XT_MULTIPORT_SOURCE
 46                             && src >= s && src <= e)
 47                                 return true ^ minfo->invert;
 48                         if (minfo->flags == XT_MULTIPORT_DESTINATION
 49                             && dst >= s && dst <= e)
 50                                 return true ^ minfo->invert;
 51                         if (minfo->flags == XT_MULTIPORT_EITHER
 52                             && ((dst >= s && dst <= e)
 53                                 || (src >= s && src <= e)))
 54                                 return true ^ minfo->invert;
 55                 } else {
 56                         /* exact port matching */
 57                         pr_debug("src or dst matches with %d?\n", s);
 58 
 59                         if (minfo->flags == XT_MULTIPORT_SOURCE
 60                             && src == s)
 61                                 return true ^ minfo->invert;
 62                         if (minfo->flags == XT_MULTIPORT_DESTINATION
 63                             && dst == s)
 64                                 return true ^ minfo->invert;
 65                         if (minfo->flags == XT_MULTIPORT_EITHER
 66                             && (src == s || dst == s))
 67                                 return true ^ minfo->invert;
 68                 }
 69         }
 70 
 71         return minfo->invert;
 72 }
 73 
 74 static bool
 75 multiport_mt(const struct sk_buff *skb, struct xt_action_param *par)
 76 {
 77         const __be16 *pptr;
 78         __be16 _ports[2];
 79         const struct xt_multiport_v1 *multiinfo = par->matchinfo;
 80 
 81         if (par->fragoff != 0)
 82                 return false;
 83 
 84         pptr = skb_header_pointer(skb, par->thoff, sizeof(_ports), _ports);
 85         if (pptr == NULL) {
 86                 /* We've been asked to examine this packet, and we
 87                  * can't.  Hence, no choice but to drop.
 88                  */
 89                 pr_debug("Dropping evil offset=0 tinygram.\n");
 90                 par->hotdrop = true;
 91                 return false;
 92         }
 93 
 94         return ports_match_v1(multiinfo, ntohs(pptr[0]), ntohs(pptr[1]));
 95 }
 96 
 97 static inline bool
 98 check(u_int16_t proto,
 99       u_int8_t ip_invflags,
100       u_int8_t match_flags,
101       u_int8_t count)
102 {
103         /* Must specify supported protocol, no unknown flags or bad count */
104         return (proto == IPPROTO_TCP || proto == IPPROTO_UDP
105                 || proto == IPPROTO_UDPLITE
106                 || proto == IPPROTO_SCTP || proto == IPPROTO_DCCP)
107                 && !(ip_invflags & XT_INV_PROTO)
108                 && (match_flags == XT_MULTIPORT_SOURCE
109                     || match_flags == XT_MULTIPORT_DESTINATION
110                     || match_flags == XT_MULTIPORT_EITHER)
111                 && count <= XT_MULTI_PORTS;
112 }
113 
114 static int multiport_mt_check(const struct xt_mtchk_param *par)
115 {
116         const struct ipt_ip *ip = par->entryinfo;
117         const struct xt_multiport_v1 *multiinfo = par->matchinfo;
118 
119         return check(ip->proto, ip->invflags, multiinfo->flags,
120                      multiinfo->count) ? 0 : -EINVAL;
121 }
122 
123 static int multiport_mt6_check(const struct xt_mtchk_param *par)
124 {
125         const struct ip6t_ip6 *ip = par->entryinfo;
126         const struct xt_multiport_v1 *multiinfo = par->matchinfo;
127 
128         return check(ip->proto, ip->invflags, multiinfo->flags,
129                      multiinfo->count) ? 0 : -EINVAL;
130 }
131 
132 static struct xt_match multiport_mt_reg[] __read_mostly = {
133         {
134                 .name           = "multiport",
135                 .family         = NFPROTO_IPV4,
136                 .revision       = 1,
137                 .checkentry     = multiport_mt_check,
138                 .match          = multiport_mt,
139                 .matchsize      = sizeof(struct xt_multiport_v1),
140                 .me             = THIS_MODULE,
141         },
142         {
143                 .name           = "multiport",
144                 .family         = NFPROTO_IPV6,
145                 .revision       = 1,
146                 .checkentry     = multiport_mt6_check,
147                 .match          = multiport_mt,
148                 .matchsize      = sizeof(struct xt_multiport_v1),
149                 .me             = THIS_MODULE,
150         },
151 };
152 
153 static int __init multiport_mt_init(void)
154 {
155         return xt_register_matches(multiport_mt_reg,
156                ARRAY_SIZE(multiport_mt_reg));
157 }
158 
159 static void __exit multiport_mt_exit(void)
160 {
161         xt_unregister_matches(multiport_mt_reg, ARRAY_SIZE(multiport_mt_reg));
162 }
163 
164 module_init(multiport_mt_init);
165 module_exit(multiport_mt_exit);
166 

~ [ 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