netfilter: ebtables: do centralized size checking
[linux-2.6] / net / bridge / netfilter / ebt_802_3.c
1 /*
2  * 802_3
3  *
4  * Author:
5  * Chris Vitale csv@bluetail.com
6  *
7  * May 2003
8  *
9  */
10 #include <linux/module.h>
11 #include <linux/netfilter/x_tables.h>
12 #include <linux/netfilter_bridge/ebtables.h>
13 #include <linux/netfilter_bridge/ebt_802_3.h>
14
15 static int ebt_filter_802_3(const struct sk_buff *skb, const struct net_device *in,
16    const struct net_device *out, const void *data, unsigned int datalen)
17 {
18         const struct ebt_802_3_info *info = data;
19         const struct ebt_802_3_hdr *hdr = ebt_802_3_hdr(skb);
20         __be16 type = hdr->llc.ui.ctrl & IS_UI ? hdr->llc.ui.type : hdr->llc.ni.type;
21
22         if (info->bitmask & EBT_802_3_SAP) {
23                 if (FWINV(info->sap != hdr->llc.ui.ssap, EBT_802_3_SAP))
24                                 return EBT_NOMATCH;
25                 if (FWINV(info->sap != hdr->llc.ui.dsap, EBT_802_3_SAP))
26                                 return EBT_NOMATCH;
27         }
28
29         if (info->bitmask & EBT_802_3_TYPE) {
30                 if (!(hdr->llc.ui.dsap == CHECK_TYPE && hdr->llc.ui.ssap == CHECK_TYPE))
31                         return EBT_NOMATCH;
32                 if (FWINV(info->type != type, EBT_802_3_TYPE))
33                         return EBT_NOMATCH;
34         }
35
36         return EBT_MATCH;
37 }
38
39 static struct ebt_match filter_802_3;
40 static int ebt_802_3_check(const char *tablename, unsigned int hookmask,
41    const struct ebt_entry *e, void *data, unsigned int datalen)
42 {
43         const struct ebt_802_3_info *info = data;
44
45         if (info->bitmask & ~EBT_802_3_MASK || info->invflags & ~EBT_802_3_MASK)
46                 return -EINVAL;
47
48         return 0;
49 }
50
51 static struct ebt_match filter_802_3 __read_mostly = {
52         .name           = EBT_802_3_MATCH,
53         .match          = ebt_filter_802_3,
54         .check          = ebt_802_3_check,
55         .matchsize      = XT_ALIGN(sizeof(struct ebt_802_3_info)),
56         .me             = THIS_MODULE,
57 };
58
59 static int __init ebt_802_3_init(void)
60 {
61         return ebt_register_match(&filter_802_3);
62 }
63
64 static void __exit ebt_802_3_fini(void)
65 {
66         ebt_unregister_match(&filter_802_3);
67 }
68
69 module_init(ebt_802_3_init);
70 module_exit(ebt_802_3_fini);
71 MODULE_DESCRIPTION("Ebtables: DSAP/SSAP field and SNAP type matching");
72 MODULE_LICENSE("GPL");