Sage Ahn | 247e9cf | 2012-05-15 13:20:36 +0900 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2012 GCT Semiconductor, Inc. All rights reserved. |
| 3 | * |
| 4 | * This software is licensed under the terms of the GNU General Public |
| 5 | * License version 2, as published by the Free Software Foundation, and |
| 6 | * may be copied, distributed, and modified under those terms. |
| 7 | * |
| 8 | * This program is distributed in the hope that it will be useful, |
| 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | * GNU General Public License for more details. |
| 12 | */ |
| 13 | |
YAMANE Toshiaki | cade0fe | 2012-10-29 20:05:02 +0900 | [diff] [blame] | 14 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 15 | |
Sage Ahn | 247e9cf | 2012-05-15 13:20:36 +0900 | [diff] [blame] | 16 | #include <linux/module.h> |
| 17 | #include <linux/etherdevice.h> |
Hong zhi guo | b96dc46 | 2013-03-27 06:52:17 +0000 | [diff] [blame] | 18 | #include <net/netlink.h> |
Sage Ahn | 247e9cf | 2012-05-15 13:20:36 +0900 | [diff] [blame] | 19 | #include <asm/byteorder.h> |
| 20 | #include <net/sock.h> |
Peter Huewe | a600053 | 2013-02-19 18:50:18 +0100 | [diff] [blame] | 21 | #include "netlink_k.h" |
Sage Ahn | 247e9cf | 2012-05-15 13:20:36 +0900 | [diff] [blame] | 22 | |
| 23 | #if !defined(NLMSG_HDRLEN) |
| 24 | #define NLMSG_HDRLEN ((int) NLMSG_ALIGN(sizeof(struct nlmsghdr))) |
| 25 | #endif |
| 26 | |
| 27 | #define ND_MAX_GROUP 30 |
| 28 | #define ND_IFINDEX_LEN sizeof(int) |
Hong zhi guo | b96dc46 | 2013-03-27 06:52:17 +0000 | [diff] [blame] | 29 | #define ND_NLMSG_SPACE(len) (nlmsg_total_size(len) + ND_IFINDEX_LEN) |
Sage Ahn | 247e9cf | 2012-05-15 13:20:36 +0900 | [diff] [blame] | 30 | #define ND_NLMSG_DATA(nlh) \ |
Hong zhi guo | b96dc46 | 2013-03-27 06:52:17 +0000 | [diff] [blame] | 31 | ((void *)((char *)nlmsg_data(nlh) + ND_IFINDEX_LEN)) |
Sage Ahn | 247e9cf | 2012-05-15 13:20:36 +0900 | [diff] [blame] | 32 | #define ND_NLMSG_S_LEN(len) (len+ND_IFINDEX_LEN) |
| 33 | #define ND_NLMSG_R_LEN(nlh) (nlh->nlmsg_len-ND_IFINDEX_LEN) |
Hong zhi guo | b96dc46 | 2013-03-27 06:52:17 +0000 | [diff] [blame] | 34 | #define ND_NLMSG_IFIDX(nlh) nlmsg_data(nlh) |
Sage Ahn | 247e9cf | 2012-05-15 13:20:36 +0900 | [diff] [blame] | 35 | #define ND_MAX_MSG_LEN 8096 |
| 36 | |
| 37 | #if defined(DEFINE_MUTEX) |
| 38 | static DEFINE_MUTEX(netlink_mutex); |
| 39 | #else |
| 40 | static struct semaphore netlink_mutex; |
| 41 | #define mutex_lock(x) down(x) |
| 42 | #define mutex_unlock(x) up(x) |
| 43 | #endif |
| 44 | |
| 45 | static void (*rcv_cb)(struct net_device *dev, u16 type, void *msg, int len); |
| 46 | |
| 47 | static void netlink_rcv_cb(struct sk_buff *skb) |
| 48 | { |
| 49 | struct nlmsghdr *nlh; |
| 50 | struct net_device *dev; |
| 51 | u32 mlen; |
| 52 | void *msg; |
| 53 | int ifindex; |
| 54 | |
Hong zhi guo | b96dc46 | 2013-03-27 06:52:17 +0000 | [diff] [blame] | 55 | if (skb->len >= NLMSG_HDRLEN) { |
Sage Ahn | 247e9cf | 2012-05-15 13:20:36 +0900 | [diff] [blame] | 56 | nlh = (struct nlmsghdr *)skb->data; |
| 57 | |
Davide Gianforte | a3709f7 | 2014-05-23 22:06:44 +0200 | [diff] [blame] | 58 | if (skb->len < nlh->nlmsg_len || |
| 59 | nlh->nlmsg_len > ND_MAX_MSG_LEN) { |
YAMANE Toshiaki | cade0fe | 2012-10-29 20:05:02 +0900 | [diff] [blame] | 60 | netdev_err(skb->dev, "Invalid length (%d,%d)\n", |
| 61 | skb->len, nlh->nlmsg_len); |
Sage Ahn | 247e9cf | 2012-05-15 13:20:36 +0900 | [diff] [blame] | 62 | return; |
| 63 | } |
| 64 | |
| 65 | memcpy(&ifindex, ND_NLMSG_IFIDX(nlh), ND_IFINDEX_LEN); |
| 66 | msg = ND_NLMSG_DATA(nlh); |
| 67 | mlen = ND_NLMSG_R_LEN(nlh); |
| 68 | |
| 69 | if (rcv_cb) { |
| 70 | dev = dev_get_by_index(&init_net, ifindex); |
| 71 | if (dev) { |
| 72 | rcv_cb(dev, nlh->nlmsg_type, msg, mlen); |
| 73 | dev_put(dev); |
| 74 | } else |
YAMANE Toshiaki | cade0fe | 2012-10-29 20:05:02 +0900 | [diff] [blame] | 75 | netdev_err(skb->dev, |
| 76 | "dev_get_by_index(%d) is not found.\n", |
| 77 | ifindex); |
Michalis Pappas | 71fd11e | 2014-05-09 18:08:25 +0800 | [diff] [blame] | 78 | } else { |
YAMANE Toshiaki | cade0fe | 2012-10-29 20:05:02 +0900 | [diff] [blame] | 79 | netdev_err(skb->dev, "Unregistered Callback\n"); |
Michalis Pappas | 71fd11e | 2014-05-09 18:08:25 +0800 | [diff] [blame] | 80 | } |
Sage Ahn | 247e9cf | 2012-05-15 13:20:36 +0900 | [diff] [blame] | 81 | } |
| 82 | } |
| 83 | |
| 84 | static void netlink_rcv(struct sk_buff *skb) |
| 85 | { |
| 86 | mutex_lock(&netlink_mutex); |
| 87 | netlink_rcv_cb(skb); |
| 88 | mutex_unlock(&netlink_mutex); |
| 89 | } |
| 90 | |
| 91 | struct sock *netlink_init(int unit, void (*cb)(struct net_device *dev, u16 type, |
Michalis Pappas | 39c511f | 2014-05-09 18:08:27 +0800 | [diff] [blame] | 92 | void *msg, int len)) |
Sage Ahn | 247e9cf | 2012-05-15 13:20:36 +0900 | [diff] [blame] | 93 | { |
| 94 | struct sock *sock; |
Pablo Neira Ayuso | a31f2d1 | 2012-06-29 06:15:21 +0000 | [diff] [blame] | 95 | struct netlink_kernel_cfg cfg = { |
| 96 | .input = netlink_rcv, |
| 97 | }; |
Sage Ahn | 247e9cf | 2012-05-15 13:20:36 +0900 | [diff] [blame] | 98 | |
| 99 | #if !defined(DEFINE_MUTEX) |
| 100 | init_MUTEX(&netlink_mutex); |
| 101 | #endif |
| 102 | |
Pablo Neira Ayuso | 9f00d97 | 2012-09-08 02:53:54 +0000 | [diff] [blame] | 103 | sock = netlink_kernel_create(&init_net, unit, &cfg); |
Sage Ahn | 247e9cf | 2012-05-15 13:20:36 +0900 | [diff] [blame] | 104 | |
| 105 | if (sock) |
| 106 | rcv_cb = cb; |
| 107 | |
| 108 | return sock; |
| 109 | } |
| 110 | |
| 111 | void netlink_exit(struct sock *sock) |
| 112 | { |
Ben Chan | 9f77186 | 2012-06-14 13:18:56 -0700 | [diff] [blame] | 113 | netlink_kernel_release(sock); |
Sage Ahn | 247e9cf | 2012-05-15 13:20:36 +0900 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | int netlink_send(struct sock *sock, int group, u16 type, void *msg, int len) |
| 117 | { |
| 118 | static u32 seq; |
| 119 | struct sk_buff *skb = NULL; |
| 120 | struct nlmsghdr *nlh; |
| 121 | int ret = 0; |
| 122 | |
| 123 | if (group > ND_MAX_GROUP) { |
Masanari Iida | 2476816 | 2015-05-27 23:51:51 +0900 | [diff] [blame] | 124 | pr_err("Group %d is invalid.\n", group); |
YAMANE Toshiaki | cade0fe | 2012-10-29 20:05:02 +0900 | [diff] [blame] | 125 | pr_err("Valid group is 0 ~ %d.\n", ND_MAX_GROUP); |
Sage Ahn | 247e9cf | 2012-05-15 13:20:36 +0900 | [diff] [blame] | 126 | return -EINVAL; |
| 127 | } |
| 128 | |
Hong zhi guo | b96dc46 | 2013-03-27 06:52:17 +0000 | [diff] [blame] | 129 | skb = nlmsg_new(len, GFP_ATOMIC); |
Sage Ahn | 247e9cf | 2012-05-15 13:20:36 +0900 | [diff] [blame] | 130 | if (!skb) { |
YAMANE Toshiaki | cade0fe | 2012-10-29 20:05:02 +0900 | [diff] [blame] | 131 | pr_err("netlink_broadcast ret=%d\n", ret); |
Sage Ahn | 247e9cf | 2012-05-15 13:20:36 +0900 | [diff] [blame] | 132 | return -ENOMEM; |
| 133 | } |
| 134 | |
| 135 | seq++; |
David S. Miller | a8edf8a | 2012-06-26 21:20:52 -0700 | [diff] [blame] | 136 | nlh = nlmsg_put(skb, 0, seq, type, len, 0); |
| 137 | if (!nlh) { |
| 138 | kfree_skb(skb); |
| 139 | return -EMSGSIZE; |
| 140 | } |
| 141 | memcpy(nlmsg_data(nlh), msg, len); |
Sage Ahn | 247e9cf | 2012-05-15 13:20:36 +0900 | [diff] [blame] | 142 | |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 143 | NETLINK_CB(skb).portid = 0; |
Sage Ahn | 247e9cf | 2012-05-15 13:20:36 +0900 | [diff] [blame] | 144 | NETLINK_CB(skb).dst_group = 0; |
| 145 | |
| 146 | ret = netlink_broadcast(sock, skb, 0, group+1, GFP_ATOMIC); |
| 147 | |
Gulsah Kose | 2e98f76 | 2014-09-27 00:00:14 +0300 | [diff] [blame] | 148 | if (!ret) |
Sage Ahn | 247e9cf | 2012-05-15 13:20:36 +0900 | [diff] [blame] | 149 | return len; |
Gulsah Kose | 93f509a | 2014-09-27 00:00:13 +0300 | [diff] [blame] | 150 | if (ret != -ESRCH) { |
| 151 | pr_err("netlink_broadcast g=%d, t=%d, l=%d, r=%d\n", |
| 152 | group, type, len, ret); |
| 153 | } |
| 154 | ret = 0; |
Sage Ahn | 247e9cf | 2012-05-15 13:20:36 +0900 | [diff] [blame] | 155 | return ret; |
| 156 | } |