1 /* Kernel module to match one of a list of TCP/UDP ports: ports are in
2 the same place so we can treat them as equal. */
3 #include <linux/module.h>
4 #include <linux/types.h>
6 #include <linux/skbuff.h>
9 #include <linux/netfilter_ipv6/ip6t_multiport.h>
10 #include <linux/netfilter_ipv6/ip6_tables.h>
13 #define duprintf(format, args...) printk(format , ## args)
15 #define duprintf(format, args...)
18 /* Returns 1 if the port is matched by the test, 0 otherwise. */
20 ports_match(const u_int16_t *portlist, enum ip6t_multiport_flags flags,
21 u_int8_t count, u_int16_t src, u_int16_t dst)
24 for (i=0; i<count; i++) {
25 if (flags != IP6T_MULTIPORT_DESTINATION
26 && portlist[i] == src)
29 if (flags != IP6T_MULTIPORT_SOURCE
30 && portlist[i] == dst)
38 match(const struct sk_buff *skb,
39 const struct net_device *in,
40 const struct net_device *out,
41 const void *matchinfo,
47 const struct udphdr *udp = hdr;
48 const struct ip6t_multiport *multiinfo = matchinfo;
50 /* Must be big enough to read ports. */
51 if (offset == 0 && datalen < sizeof(struct udphdr)) {
52 /* We've been asked to examine this packet, and we
53 can't. Hence, no choice but to drop. */
54 duprintf("ip6t_multiport:"
55 " Dropping evil offset=0 tinygram.\n");
60 /* Must not be a fragment. */
62 && ports_match(multiinfo->ports,
63 multiinfo->flags, multiinfo->count,
64 ntohs(udp->source), ntohs(udp->dest));
67 /* Called when user tries to insert an entry of this type. */
69 checkentry(const char *tablename,
70 const struct ip6t_ip6 *ip,
72 unsigned int matchsize,
73 unsigned int hook_mask)
75 const struct ip6t_multiport *multiinfo = matchinfo;
77 /* Must specify proto == TCP/UDP, no unknown flags or bad count */
78 return (ip->proto == IPPROTO_TCP || ip->proto == IPPROTO_UDP)
79 && !(ip->flags & IP6T_INV_PROTO)
80 && matchsize == IP6T_ALIGN(sizeof(struct ip6t_multiport))
81 && (multiinfo->flags == IP6T_MULTIPORT_SOURCE
82 || multiinfo->flags == IP6T_MULTIPORT_DESTINATION
83 || multiinfo->flags == IP6T_MULTIPORT_EITHER)
84 && multiinfo->count <= IP6T_MULTI_PORTS;
87 static struct ip6t_match multiport_match
88 = { { NULL, NULL }, "multiport", &match, &checkentry, NULL, THIS_MODULE };
90 static int __init init(void)
92 return ip6t_register_match(&multiport_match);
95 static void __exit fini(void)
97 ip6t_unregister_match(&multiport_match);