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