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> | |
5a0e3ad6 | 17 | #include <linux/slab.h> |
1da177e4 LT |
18 | #include <linux/init.h> |
19 | #include <linux/netdevice.h> | |
20 | #include <linux/netfilter.h> | |
21 | #include <linux/spinlock.h> | |
573ce260 | 22 | #include <net/netlink.h> |
63a1222b | 23 | #include <linux/netfilter_decnet.h> |
1da177e4 LT |
24 | |
25 | #include <net/sock.h> | |
26 | #include <net/flow.h> | |
27 | #include <net/dn.h> | |
28 | #include <net/dn_route.h> | |
29 | ||
1da177e4 LT |
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; | |
27a884dc | 37 | sk_buff_data_t old_tail; |
1da177e4 LT |
38 | struct nlmsghdr *nlh; |
39 | unsigned char *ptr; | |
40 | struct nf_dn_rtmsg *rtm; | |
41 | ||
573ce260 H |
42 | size = NLMSG_ALIGN(rt_skb->len) + |
43 | NLMSG_ALIGN(sizeof(struct nf_dn_rtmsg)); | |
44 | skb = nlmsg_new(size, GFP_ATOMIC); | |
77ca4ed5 DM |
45 | if (!skb) { |
46 | *errp = -ENOMEM; | |
47 | return NULL; | |
48 | } | |
1da177e4 | 49 | old_tail = skb->tail; |
573ce260 | 50 | nlh = nlmsg_put(skb, 0, 0, 0, size, 0); |
77ca4ed5 DM |
51 | if (!nlh) { |
52 | kfree_skb(skb); | |
53 | *errp = -ENOMEM; | |
54 | return NULL; | |
55 | } | |
573ce260 | 56 | rtm = (struct nf_dn_rtmsg *)nlmsg_data(nlh); |
1da177e4 LT |
57 | rtm->nfdn_ifindex = rt_skb->dev->ifindex; |
58 | ptr = NFDN_RTMSG(rtm); | |
1a4e2d09 | 59 | skb_copy_from_linear_data(rt_skb, ptr, rt_skb->len); |
1da177e4 LT |
60 | nlh->nlmsg_len = skb->tail - old_tail; |
61 | return skb; | |
1da177e4 LT |
62 | } |
63 | ||
64 | static void dnrmg_send_peer(struct sk_buff *skb) | |
65 | { | |
66 | struct sk_buff *skb2; | |
67 | int status = 0; | |
68 | int group = 0; | |
69 | unsigned char flags = *skb->data; | |
70 | ||
06f8fe11 JP |
71 | switch (flags & DN_RT_CNTL_MSK) { |
72 | case DN_RT_PKT_L1RT: | |
73 | group = DNRNG_NLGRP_L1; | |
74 | break; | |
75 | case DN_RT_PKT_L2RT: | |
76 | group = DNRNG_NLGRP_L2; | |
77 | break; | |
78 | default: | |
79 | return; | |
1da177e4 LT |
80 | } |
81 | ||
82 | skb2 = dnrmg_build_message(skb, &status); | |
83 | if (skb2 == NULL) | |
84 | return; | |
ac6d439d | 85 | NETLINK_CB(skb2).dst_group = group; |
1da177e4 LT |
86 | netlink_broadcast(dnrmg, skb2, 0, group, GFP_ATOMIC); |
87 | } | |
88 | ||
89 | ||
795aa6ef | 90 | static unsigned int dnrmg_hook(const struct nf_hook_ops *ops, |
3db05fea | 91 | struct sk_buff *skb, |
238e54c9 | 92 | const struct nf_hook_state *state) |
1da177e4 | 93 | { |
3db05fea | 94 | dnrmg_send_peer(skb); |
1da177e4 LT |
95 | return NF_ACCEPT; |
96 | } | |
97 | ||
98 | ||
99 | #define RCV_SKB_FAIL(err) do { netlink_ack(skb, nlh, (err)); return; } while (0) | |
100 | ||
101 | static inline void dnrmg_receive_user_skb(struct sk_buff *skb) | |
102 | { | |
b529ccf2 | 103 | struct nlmsghdr *nlh = nlmsg_hdr(skb); |
1da177e4 LT |
104 | |
105 | if (nlh->nlmsg_len < sizeof(*nlh) || skb->len < nlh->nlmsg_len) | |
106 | return; | |
107 | ||
90f62cf3 | 108 | if (!netlink_capable(skb, CAP_NET_ADMIN)) |
1da177e4 LT |
109 | RCV_SKB_FAIL(-EPERM); |
110 | ||
111 | /* Eventually we might send routing messages too */ | |
112 | ||
113 | RCV_SKB_FAIL(-EINVAL); | |
114 | } | |
115 | ||
1999414a | 116 | static struct nf_hook_ops dnrmg_ops __read_mostly = { |
1da177e4 | 117 | .hook = dnrmg_hook, |
3d33bf2b | 118 | .pf = NFPROTO_DECNET, |
1da177e4 LT |
119 | .hooknum = NF_DN_ROUTE, |
120 | .priority = NF_DN_PRI_DNRTMSG, | |
121 | }; | |
122 | ||
65b4b4e8 | 123 | static int __init dn_rtmsg_init(void) |
1da177e4 LT |
124 | { |
125 | int rv = 0; | |
a31f2d17 PNA |
126 | struct netlink_kernel_cfg cfg = { |
127 | .groups = DNRNG_NLGRP_MAX, | |
128 | .input = dnrmg_receive_user_skb, | |
129 | }; | |
1da177e4 | 130 | |
9f00d977 | 131 | dnrmg = netlink_kernel_create(&init_net, NETLINK_DNRTMSG, &cfg); |
1da177e4 LT |
132 | if (dnrmg == NULL) { |
133 | printk(KERN_ERR "dn_rtmsg: Cannot create netlink socket"); | |
134 | return -ENOMEM; | |
135 | } | |
136 | ||
137 | rv = nf_register_hook(&dnrmg_ops); | |
138 | if (rv) { | |
b7c6ba6e | 139 | netlink_kernel_release(dnrmg); |
1da177e4 LT |
140 | } |
141 | ||
142 | return rv; | |
143 | } | |
144 | ||
65b4b4e8 | 145 | static void __exit dn_rtmsg_fini(void) |
1da177e4 LT |
146 | { |
147 | nf_unregister_hook(&dnrmg_ops); | |
b7c6ba6e | 148 | netlink_kernel_release(dnrmg); |
1da177e4 LT |
149 | } |
150 | ||
151 | ||
152 | MODULE_DESCRIPTION("DECnet Routing Message Grabulator"); | |
153 | MODULE_AUTHOR("Steven Whitehouse <steve@chygwyn.com>"); | |
154 | MODULE_LICENSE("GPL"); | |
4fdb3bb7 | 155 | MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK, NETLINK_DNRTMSG); |
1da177e4 | 156 | |
65b4b4e8 AM |
157 | module_init(dn_rtmsg_init); |
158 | module_exit(dn_rtmsg_fini); | |
1da177e4 | 159 |