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> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 17 | #include <linux/slab.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | #include <linux/init.h> |
| 19 | #include <linux/netdevice.h> |
| 20 | #include <linux/netfilter.h> |
| 21 | #include <linux/spinlock.h> |
| 22 | #include <linux/netlink.h> |
Andrew Morton | 63a1222 | 2005-08-15 20:35:44 -0700 | [diff] [blame] | 23 | #include <linux/netfilter_decnet.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | |
| 25 | #include <net/sock.h> |
| 26 | #include <net/flow.h> |
| 27 | #include <net/dn.h> |
| 28 | #include <net/dn_route.h> |
| 29 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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; |
Arnaldo Carvalho de Melo | 27a884d | 2007-04-19 20:29:13 -0700 | [diff] [blame] | 37 | sk_buff_data_t old_tail; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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); |
Arnaldo Carvalho de Melo | 1a4e2d0 | 2007-03-31 11:55:45 -0300 | [diff] [blame] | 52 | skb_copy_from_linear_data(rt_skb, ptr, rt_skb->len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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; |
Joe Perches | e87cc47 | 2012-05-13 21:56:26 +0000 | [diff] [blame] | 60 | net_err_ratelimited("dn_rtmsg: error creating netlink message\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Joe Perches | 06f8fe1 | 2011-07-01 09:43:03 +0000 | [diff] [blame] | 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; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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, |
Herbert Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame] | 91 | struct sk_buff *skb, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 92 | const struct net_device *in, |
| 93 | const struct net_device *out, |
| 94 | int (*okfn)(struct sk_buff *)) |
| 95 | { |
Herbert Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame] | 96 | dnrmg_send_peer(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | { |
Arnaldo Carvalho de Melo | b529ccf | 2007-04-25 19:08:35 -0700 | [diff] [blame] | 105 | struct nlmsghdr *nlh = nlmsg_hdr(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 106 | |
| 107 | if (nlh->nlmsg_len < sizeof(*nlh) || skb->len < nlh->nlmsg_len) |
| 108 | return; |
| 109 | |
Eric Paris | fd77846 | 2012-01-03 12:25:16 -0500 | [diff] [blame] | 110 | if (!capable(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 | |
Patrick McHardy | 1999414 | 2007-12-05 01:23:00 -0800 | [diff] [blame] | 118 | static struct nf_hook_ops dnrmg_ops __read_mostly = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 119 | .hook = dnrmg_hook, |
| 120 | .pf = PF_DECnet, |
| 121 | .hooknum = NF_DN_ROUTE, |
| 122 | .priority = NF_DN_PRI_DNRTMSG, |
| 123 | }; |
| 124 | |
Andrew Morton | 65b4b4e | 2006-03-28 16:37:06 -0800 | [diff] [blame] | 125 | static int __init dn_rtmsg_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 126 | { |
| 127 | int rv = 0; |
| 128 | |
Eric W. Biederman | b4b5102 | 2007-09-12 13:05:38 +0200 | [diff] [blame] | 129 | dnrmg = netlink_kernel_create(&init_net, |
| 130 | NETLINK_DNRTMSG, DNRNG_NLGRP_MAX, |
Denis V. Lunev | cd40b7d | 2007-10-10 21:15:29 -0700 | [diff] [blame] | 131 | dnrmg_receive_user_skb, |
| 132 | NULL, THIS_MODULE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 133 | if (dnrmg == NULL) { |
| 134 | printk(KERN_ERR "dn_rtmsg: Cannot create netlink socket"); |
| 135 | return -ENOMEM; |
| 136 | } |
| 137 | |
| 138 | rv = nf_register_hook(&dnrmg_ops); |
| 139 | if (rv) { |
Denis V. Lunev | b7c6ba6 | 2008-01-28 14:41:19 -0800 | [diff] [blame] | 140 | netlink_kernel_release(dnrmg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | return rv; |
| 144 | } |
| 145 | |
Andrew Morton | 65b4b4e | 2006-03-28 16:37:06 -0800 | [diff] [blame] | 146 | static void __exit dn_rtmsg_fini(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 147 | { |
| 148 | nf_unregister_hook(&dnrmg_ops); |
Denis V. Lunev | b7c6ba6 | 2008-01-28 14:41:19 -0800 | [diff] [blame] | 149 | netlink_kernel_release(dnrmg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | |
| 153 | MODULE_DESCRIPTION("DECnet Routing Message Grabulator"); |
| 154 | MODULE_AUTHOR("Steven Whitehouse <steve@chygwyn.com>"); |
| 155 | MODULE_LICENSE("GPL"); |
Harald Welte | 4fdb3bb | 2005-08-09 19:40:55 -0700 | [diff] [blame] | 156 | MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK, NETLINK_DNRTMSG); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 157 | |
Andrew Morton | 65b4b4e | 2006-03-28 16:37:06 -0800 | [diff] [blame] | 158 | module_init(dn_rtmsg_init); |
| 159 | module_exit(dn_rtmsg_fini); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 160 | |