Commit | Line | Data |
---|---|---|
1da177e4 LT |
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> | |
63a1222b | 22 | #include <linux/netfilter_decnet.h> |
1da177e4 LT |
23 | |
24 | #include <net/sock.h> | |
25 | #include <net/flow.h> | |
26 | #include <net/dn.h> | |
27 | #include <net/dn_route.h> | |
28 | ||
29 | #include <linux/netfilter_decnet.h> | |
30 | ||
31 | static struct sock *dnrmg = NULL; | |
32 | ||
33 | ||
34 | static struct sk_buff *dnrmg_build_message(struct sk_buff *rt_skb, int *errp) | |
35 | { | |
36 | struct sk_buff *skb = NULL; | |
37 | size_t size; | |
38 | unsigned char *old_tail; | |
39 | struct nlmsghdr *nlh; | |
40 | unsigned char *ptr; | |
41 | struct nf_dn_rtmsg *rtm; | |
42 | ||
43 | size = NLMSG_SPACE(rt_skb->len); | |
44 | size += NLMSG_ALIGN(sizeof(struct nf_dn_rtmsg)); | |
45 | skb = alloc_skb(size, GFP_ATOMIC); | |
46 | if (!skb) | |
47 | goto nlmsg_failure; | |
48 | old_tail = skb->tail; | |
49 | nlh = NLMSG_PUT(skb, 0, 0, 0, size - sizeof(*nlh)); | |
50 | rtm = (struct nf_dn_rtmsg *)NLMSG_DATA(nlh); | |
51 | rtm->nfdn_ifindex = rt_skb->dev->ifindex; | |
52 | ptr = NFDN_RTMSG(rtm); | |
53 | memcpy(ptr, rt_skb->data, rt_skb->len); | |
54 | nlh->nlmsg_len = skb->tail - old_tail; | |
55 | return skb; | |
56 | ||
57 | nlmsg_failure: | |
58 | if (skb) | |
59 | kfree_skb(skb); | |
60 | *errp = -ENOMEM; | |
61 | if (net_ratelimit()) | |
62 | printk(KERN_ERR "dn_rtmsg: error creating netlink message\n"); | |
63 | return NULL; | |
64 | } | |
65 | ||
66 | static void dnrmg_send_peer(struct sk_buff *skb) | |
67 | { | |
68 | struct sk_buff *skb2; | |
69 | int status = 0; | |
70 | int group = 0; | |
71 | unsigned char flags = *skb->data; | |
72 | ||
73 | switch(flags & DN_RT_CNTL_MSK) { | |
74 | case DN_RT_PKT_L1RT: | |
63a1222b | 75 | group = DNRNG_NLGRP_L1; |
1da177e4 LT |
76 | break; |
77 | case DN_RT_PKT_L2RT: | |
63a1222b | 78 | group = DNRNG_NLGRP_L2; |
1da177e4 LT |
79 | break; |
80 | default: | |
81 | return; | |
82 | } | |
83 | ||
84 | skb2 = dnrmg_build_message(skb, &status); | |
85 | if (skb2 == NULL) | |
86 | return; | |
ac6d439d | 87 | NETLINK_CB(skb2).dst_group = group; |
1da177e4 LT |
88 | netlink_broadcast(dnrmg, skb2, 0, group, GFP_ATOMIC); |
89 | } | |
90 | ||
91 | ||
92 | static unsigned int dnrmg_hook(unsigned int hook, | |
93 | struct sk_buff **pskb, | |
94 | const struct net_device *in, | |
95 | const struct net_device *out, | |
96 | int (*okfn)(struct sk_buff *)) | |
97 | { | |
98 | dnrmg_send_peer(*pskb); | |
99 | return NF_ACCEPT; | |
100 | } | |
101 | ||
102 | ||
103 | #define RCV_SKB_FAIL(err) do { netlink_ack(skb, nlh, (err)); return; } while (0) | |
104 | ||
105 | static inline void dnrmg_receive_user_skb(struct sk_buff *skb) | |
106 | { | |
107 | struct nlmsghdr *nlh = (struct nlmsghdr *)skb->data; | |
108 | ||
109 | if (nlh->nlmsg_len < sizeof(*nlh) || skb->len < nlh->nlmsg_len) | |
110 | return; | |
111 | ||
112 | if (!cap_raised(NETLINK_CB(skb).eff_cap, CAP_NET_ADMIN)) | |
113 | RCV_SKB_FAIL(-EPERM); | |
114 | ||
115 | /* Eventually we might send routing messages too */ | |
116 | ||
117 | RCV_SKB_FAIL(-EINVAL); | |
118 | } | |
119 | ||
120 | static void dnrmg_receive_user_sk(struct sock *sk, int len) | |
121 | { | |
122 | struct sk_buff *skb; | |
2a0a6ebe | 123 | unsigned int qlen = skb_queue_len(&sk->sk_receive_queue); |
1da177e4 | 124 | |
09e14305 | 125 | for (; qlen && (skb = skb_dequeue(&sk->sk_receive_queue)); qlen--) { |
1da177e4 LT |
126 | dnrmg_receive_user_skb(skb); |
127 | kfree_skb(skb); | |
128 | } | |
129 | } | |
130 | ||
131 | static struct nf_hook_ops dnrmg_ops = { | |
132 | .hook = dnrmg_hook, | |
133 | .pf = PF_DECnet, | |
134 | .hooknum = NF_DN_ROUTE, | |
135 | .priority = NF_DN_PRI_DNRTMSG, | |
136 | }; | |
137 | ||
138 | static int __init init(void) | |
139 | { | |
140 | int rv = 0; | |
141 | ||
06628607 PM |
142 | dnrmg = netlink_kernel_create(NETLINK_DNRTMSG, DNRNG_NLGRP_MAX, |
143 | dnrmg_receive_user_sk, THIS_MODULE); | |
1da177e4 LT |
144 | if (dnrmg == NULL) { |
145 | printk(KERN_ERR "dn_rtmsg: Cannot create netlink socket"); | |
146 | return -ENOMEM; | |
147 | } | |
148 | ||
149 | rv = nf_register_hook(&dnrmg_ops); | |
150 | if (rv) { | |
151 | sock_release(dnrmg->sk_socket); | |
152 | } | |
153 | ||
154 | return rv; | |
155 | } | |
156 | ||
157 | static void __exit fini(void) | |
158 | { | |
159 | nf_unregister_hook(&dnrmg_ops); | |
160 | sock_release(dnrmg->sk_socket); | |
161 | } | |
162 | ||
163 | ||
164 | MODULE_DESCRIPTION("DECnet Routing Message Grabulator"); | |
165 | MODULE_AUTHOR("Steven Whitehouse <steve@chygwyn.com>"); | |
166 | MODULE_LICENSE("GPL"); | |
4fdb3bb7 | 167 | MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK, NETLINK_DNRTMSG); |
1da177e4 LT |
168 | |
169 | module_init(init); | |
170 | module_exit(fini); | |
171 |