blob: 81e86d319a8faad995b5a9471dac55c9c5826d64 [file] [log] [blame]
Harald Welte2e4e6a12006-01-12 13:30:04 -08001/* (C) 1999-2001 Michal Ludvig <michal@logix.cz>
2 *
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License version 2 as
5 * published by the Free Software Foundation.
6 */
7
8#include <linux/module.h>
9#include <linux/skbuff.h>
10#include <linux/if_ether.h>
11#include <linux/if_packet.h>
Phil Oester28658c82006-07-24 22:54:14 -070012#include <linux/in.h>
13#include <linux/ip.h>
Jan Engelhardt57de0ab2008-01-14 23:41:50 -080014#include <linux/ipv6.h>
Harald Welte2e4e6a12006-01-12 13:30:04 -080015
16#include <linux/netfilter/xt_pkttype.h>
17#include <linux/netfilter/x_tables.h>
18
19MODULE_LICENSE("GPL");
20MODULE_AUTHOR("Michal Ludvig <michal@logix.cz>");
Jan Engelhardt2ae15b62008-01-14 23:42:28 -080021MODULE_DESCRIPTION("Xtables: link layer packet type match");
Harald Welte2e4e6a12006-01-12 13:30:04 -080022MODULE_ALIAS("ipt_pkttype");
23MODULE_ALIAS("ip6t_pkttype");
24
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -080025static bool
26pkttype_mt(const struct sk_buff *skb, const struct net_device *in,
27 const struct net_device *out, const struct xt_match *match,
28 const void *matchinfo, int offset, unsigned int protoff,
29 bool *hotdrop)
Harald Welte2e4e6a12006-01-12 13:30:04 -080030{
31 const struct xt_pkttype_info *info = matchinfo;
Jan Engelhardt57de0ab2008-01-14 23:41:50 -080032 u_int8_t type;
Harald Welte2e4e6a12006-01-12 13:30:04 -080033
Jan Engelhardt57de0ab2008-01-14 23:41:50 -080034 if (skb->pkt_type != PACKET_LOOPBACK)
Phil Oester28658c82006-07-24 22:54:14 -070035 type = skb->pkt_type;
Jan Engelhardtee999d82008-10-08 11:35:01 +020036 else if (match->family == NFPROTO_IPV4 &&
Jan Engelhardt57de0ab2008-01-14 23:41:50 -080037 ipv4_is_multicast(ip_hdr(skb)->daddr))
38 type = PACKET_MULTICAST;
Jan Engelhardtee999d82008-10-08 11:35:01 +020039 else if (match->family == NFPROTO_IPV6 &&
Jan Engelhardt57de0ab2008-01-14 23:41:50 -080040 ipv6_hdr(skb)->daddr.s6_addr[0] == 0xFF)
41 type = PACKET_MULTICAST;
42 else
43 type = PACKET_BROADCAST;
Phil Oester28658c82006-07-24 22:54:14 -070044
45 return (type == info->pkttype) ^ info->invert;
Harald Welte2e4e6a12006-01-12 13:30:04 -080046}
47
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -080048static struct xt_match pkttype_mt_reg[] __read_mostly = {
Patrick McHardy4470bbc2006-08-22 00:34:04 -070049 {
50 .name = "pkttype",
Jan Engelhardtee999d82008-10-08 11:35:01 +020051 .family = NFPROTO_IPV4,
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -080052 .match = pkttype_mt,
Patrick McHardy4470bbc2006-08-22 00:34:04 -070053 .matchsize = sizeof(struct xt_pkttype_info),
54 .me = THIS_MODULE,
55 },
56 {
57 .name = "pkttype",
Jan Engelhardtee999d82008-10-08 11:35:01 +020058 .family = NFPROTO_IPV6,
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -080059 .match = pkttype_mt,
Patrick McHardy4470bbc2006-08-22 00:34:04 -070060 .matchsize = sizeof(struct xt_pkttype_info),
61 .me = THIS_MODULE,
62 },
Patrick McHardy5d04bff2006-03-20 18:01:58 -080063};
Harald Welte2e4e6a12006-01-12 13:30:04 -080064
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -080065static int __init pkttype_mt_init(void)
Harald Welte2e4e6a12006-01-12 13:30:04 -080066{
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -080067 return xt_register_matches(pkttype_mt_reg, ARRAY_SIZE(pkttype_mt_reg));
Harald Welte2e4e6a12006-01-12 13:30:04 -080068}
69
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -080070static void __exit pkttype_mt_exit(void)
Harald Welte2e4e6a12006-01-12 13:30:04 -080071{
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -080072 xt_unregister_matches(pkttype_mt_reg, ARRAY_SIZE(pkttype_mt_reg));
Harald Welte2e4e6a12006-01-12 13:30:04 -080073}
74
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -080075module_init(pkttype_mt_init);
76module_exit(pkttype_mt_exit);