3068fddb2da3cada9d074069164648eba44f6fbf
[linux-2.6.git] / net / decnet / netfilter / dn_rtmsg.c
1 /*
2  * DECnet       An implementation of the DECnet protocol suite for the LINUX
3  *              operating system.  DECnet is implemented using the  BSD Socket
4  *              interface as the means of communication with the user level.
5  *
6  *              DECnet Routing Message Grabulator
7  *
8  *              (C) 2000 ChyGwyn Limited  -  http://www.chygwyn.com/
9  *              This code may be copied under the GPL v.2 or at your option
10  *              any later version.
11  *
12  * Author:      Steven Whitehouse <steve@chygwyn.com>
13  *
14  */
15 #include <linux/module.h>
16 #include <linux/skbuff.h>
17 #include <linux/init.h>
18 #include <linux/netdevice.h>
19 #include <linux/netfilter.h>
20 #include <linux/spinlock.h>
21 #include <linux/netlink.h>
22
23 #include <net/sock.h>
24 #include <net/flow.h>
25 #include <net/dn.h>
26 #include <net/dn_route.h>
27
28 #include <linux/netfilter_decnet.h>
29
30 static struct sock *dnrmg = NULL;
31
32
33 static struct sk_buff *dnrmg_build_message(struct sk_buff *rt_skb, int *errp)
34 {
35         struct sk_buff *skb = NULL;
36         size_t size;
37         unsigned char *old_tail;
38         struct nlmsghdr *nlh;
39         unsigned char *ptr;
40         struct nf_dn_rtmsg *rtm;
41
42         size = NLMSG_SPACE(rt_skb->len);
43         size += NLMSG_ALIGN(sizeof(struct nf_dn_rtmsg));
44         skb = alloc_skb(size, GFP_ATOMIC);
45         if (!skb)
46                 goto nlmsg_failure;
47         old_tail = skb->tail;
48         nlh = NLMSG_PUT(skb, 0, 0, 0, size - sizeof(*nlh));
49         rtm = (struct nf_dn_rtmsg *)NLMSG_DATA(nlh);
50         rtm->nfdn_ifindex = rt_skb->dev->ifindex;
51         ptr = NFDN_RTMSG(rtm);
52         memcpy(ptr, rt_skb->data, rt_skb->len);
53         nlh->nlmsg_len = skb->tail - old_tail;
54         return skb;
55
56 nlmsg_failure:
57         if (skb)
58                 kfree_skb(skb);
59         *errp = -ENOMEM;
60         if (net_ratelimit())
61                 printk(KERN_ERR "dn_rtmsg: error creating netlink message\n");
62         return NULL;
63 }
64
65 static void dnrmg_send_peer(struct sk_buff *skb)
66 {
67         struct sk_buff *skb2;
68         int status = 0;
69         int group = 0;
70         unsigned char flags = *skb->data;
71
72         switch(flags & DN_RT_CNTL_MSK) {
73                 case DN_RT_PKT_L1RT:
74                         group = DNRMG_L1_GROUP;
75                         break;
76                 case DN_RT_PKT_L2RT:
77                         group = DNRMG_L2_GROUP;
78                         break;
79                 default:
80                         return;
81         }
82
83         skb2 = dnrmg_build_message(skb, &status);
84         if (skb2 == NULL)
85                 return;
86         NETLINK_CB(skb2).dst_groups = group;
87         netlink_broadcast(dnrmg, skb2, 0, group, GFP_ATOMIC);
88 }
89
90
91 static unsigned int dnrmg_hook(unsigned int hook,
92                         struct sk_buff **pskb,
93                         const struct net_device *in,
94                         const struct net_device *out,
95                         int (*okfn)(struct sk_buff *))
96 {
97         dnrmg_send_peer(*pskb);
98         return NF_ACCEPT;
99 }
100
101
102 #define RCV_SKB_FAIL(err) do { netlink_ack(skb, nlh, (err)); return; } while (0)
103
104 static inline void dnrmg_receive_user_skb(struct sk_buff *skb)
105 {
106         struct nlmsghdr *nlh = (struct nlmsghdr *)skb->data;
107
108         if (nlh->nlmsg_len < sizeof(*nlh) || skb->len < nlh->nlmsg_len)
109                 return;
110
111         if (!cap_raised(NETLINK_CB(skb).eff_cap, CAP_NET_ADMIN))
112                 RCV_SKB_FAIL(-EPERM);
113
114         /* Eventually we might send routing messages too */
115
116         RCV_SKB_FAIL(-EINVAL);
117 }
118
119 static void dnrmg_receive_user_sk(struct sock *sk, int len)
120 {
121         struct sk_buff *skb;
122         unsigned int qlen = skb_queue_len(&sk->sk_receive_queue);
123
124         for (; qlen && (skb = skb_dequeue(&sk->sk_receive_queue)); qlen--) {
125                 dnrmg_receive_user_skb(skb);
126                 kfree_skb(skb);
127         }
128 }
129
130 static struct nf_hook_ops dnrmg_ops = {
131         .hook           = dnrmg_hook,
132         .pf             = PF_DECnet,
133         .hooknum        = NF_DN_ROUTE,
134         .priority       = NF_DN_PRI_DNRTMSG,
135 };
136
137 static int __init init(void)
138 {
139         int rv = 0;
140
141         dnrmg = netlink_kernel_create(NETLINK_DNRTMSG, dnrmg_receive_user_sk,
142                                       THIS_MODULE);
143         if (dnrmg == NULL) {
144                 printk(KERN_ERR "dn_rtmsg: Cannot create netlink socket");
145                 return -ENOMEM;
146         }
147
148         rv = nf_register_hook(&dnrmg_ops);
149         if (rv) {
150                 sock_release(dnrmg->sk_socket);
151         }
152
153         return rv;
154 }
155
156 static void __exit fini(void)
157 {
158         nf_unregister_hook(&dnrmg_ops);
159         sock_release(dnrmg->sk_socket);
160 }
161
162
163 MODULE_DESCRIPTION("DECnet Routing Message Grabulator");
164 MODULE_AUTHOR("Steven Whitehouse <steve@chygwyn.com>");
165 MODULE_LICENSE("GPL");
166 MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK, NETLINK_DNRTMSG);
167
168 module_init(init);
169 module_exit(fini);
170