Thomas Gleixner | 1a59d1b8 | 2019-05-27 08:55:05 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Evgeniy Polyakov | 7672d0b | 2005-09-11 19:15:07 -0700 | [diff] [blame] | 2 | /* |
| 3 | * cn_test.c |
| 4 | * |
Evgeniy Polyakov | acb9c1b | 2009-07-21 12:43:51 -0700 | [diff] [blame] | 5 | * 2004+ Copyright (c) Evgeniy Polyakov <zbr@ioremap.net> |
Evgeniy Polyakov | 7672d0b | 2005-09-11 19:15:07 -0700 | [diff] [blame] | 6 | * All rights reserved. |
Evgeniy Polyakov | 7672d0b | 2005-09-11 19:15:07 -0700 | [diff] [blame] | 7 | */ |
| 8 | |
Mike Frysinger | 37cf2b8 | 2009-07-17 10:14:26 -0700 | [diff] [blame] | 9 | #define pr_fmt(fmt) "cn_test: " fmt |
| 10 | |
Evgeniy Polyakov | 7672d0b | 2005-09-11 19:15:07 -0700 | [diff] [blame] | 11 | #include <linux/kernel.h> |
| 12 | #include <linux/module.h> |
| 13 | #include <linux/moduleparam.h> |
| 14 | #include <linux/skbuff.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 15 | #include <linux/slab.h> |
Evgeniy Polyakov | 7672d0b | 2005-09-11 19:15:07 -0700 | [diff] [blame] | 16 | #include <linux/timer.h> |
| 17 | |
Carlo Comin | 18a0c23 | 2005-11-08 09:38:56 -0800 | [diff] [blame] | 18 | #include <linux/connector.h> |
Evgeniy Polyakov | 7672d0b | 2005-09-11 19:15:07 -0700 | [diff] [blame] | 19 | |
Mike Frysinger | 37cf2b8 | 2009-07-17 10:14:26 -0700 | [diff] [blame] | 20 | static struct cb_id cn_test_id = { CN_NETLINK_USERS + 3, 0x456 }; |
Evgeniy Polyakov | 7672d0b | 2005-09-11 19:15:07 -0700 | [diff] [blame] | 21 | static char cn_test_name[] = "cn_test"; |
| 22 | static struct sock *nls; |
| 23 | static struct timer_list cn_test_timer; |
| 24 | |
Philipp Reisner | 7069331 | 2009-10-02 02:40:05 +0000 | [diff] [blame] | 25 | static void cn_test_callback(struct cn_msg *msg, struct netlink_skb_parms *nsp) |
Evgeniy Polyakov | 7672d0b | 2005-09-11 19:15:07 -0700 | [diff] [blame] | 26 | { |
Mike Frysinger | 37cf2b8 | 2009-07-17 10:14:26 -0700 | [diff] [blame] | 27 | pr_info("%s: %lu: idx=%x, val=%x, seq=%u, ack=%u, len=%d: %s.\n", |
| 28 | __func__, jiffies, msg->id.idx, msg->id.val, |
| 29 | msg->seq, msg->ack, msg->len, |
| 30 | msg->len ? (char *)msg->data : ""); |
Evgeniy Polyakov | 7672d0b | 2005-09-11 19:15:07 -0700 | [diff] [blame] | 31 | } |
| 32 | |
Jaswinder Singh Rajput | 28f06c6 | 2009-06-17 16:26:30 -0700 | [diff] [blame] | 33 | /* |
| 34 | * Do not remove this function even if no one is using it as |
| 35 | * this is an example of how to get notifications about new |
| 36 | * connector user registration |
| 37 | */ |
| 38 | #if 0 |
Evgeniy Polyakov | 7672d0b | 2005-09-11 19:15:07 -0700 | [diff] [blame] | 39 | static int cn_test_want_notify(void) |
| 40 | { |
| 41 | struct cn_ctl_msg *ctl; |
| 42 | struct cn_notify_req *req; |
| 43 | struct cn_msg *msg = NULL; |
| 44 | int size, size0; |
| 45 | struct sk_buff *skb; |
| 46 | struct nlmsghdr *nlh; |
| 47 | u32 group = 1; |
| 48 | |
| 49 | size0 = sizeof(*msg) + sizeof(*ctl) + 3 * sizeof(*req); |
| 50 | |
| 51 | size = NLMSG_SPACE(size0); |
| 52 | |
| 53 | skb = alloc_skb(size, GFP_ATOMIC); |
| 54 | if (!skb) { |
Mike Frysinger | 37cf2b8 | 2009-07-17 10:14:26 -0700 | [diff] [blame] | 55 | pr_err("failed to allocate new skb with size=%u\n", size); |
Evgeniy Polyakov | 7672d0b | 2005-09-11 19:15:07 -0700 | [diff] [blame] | 56 | return -ENOMEM; |
| 57 | } |
| 58 | |
David S. Miller | 8786395 | 2012-06-26 21:19:02 -0700 | [diff] [blame] | 59 | nlh = nlmsg_put(skb, 0, 0x123, NLMSG_DONE, size - sizeof(*nlh), 0); |
| 60 | if (!nlh) { |
| 61 | kfree_skb(skb); |
| 62 | return -EMSGSIZE; |
| 63 | } |
Evgeniy Polyakov | 7672d0b | 2005-09-11 19:15:07 -0700 | [diff] [blame] | 64 | |
David S. Miller | 8786395 | 2012-06-26 21:19:02 -0700 | [diff] [blame] | 65 | msg = nlmsg_data(nlh); |
Evgeniy Polyakov | 7672d0b | 2005-09-11 19:15:07 -0700 | [diff] [blame] | 66 | |
| 67 | memset(msg, 0, size0); |
| 68 | |
| 69 | msg->id.idx = -1; |
| 70 | msg->id.val = -1; |
| 71 | msg->seq = 0x123; |
| 72 | msg->ack = 0x345; |
| 73 | msg->len = size0 - sizeof(*msg); |
| 74 | |
| 75 | ctl = (struct cn_ctl_msg *)(msg + 1); |
| 76 | |
| 77 | ctl->idx_notify_num = 1; |
| 78 | ctl->val_notify_num = 2; |
| 79 | ctl->group = group; |
| 80 | ctl->len = msg->len - sizeof(*ctl); |
| 81 | |
| 82 | req = (struct cn_notify_req *)(ctl + 1); |
| 83 | |
| 84 | /* |
| 85 | * Idx. |
| 86 | */ |
| 87 | req->first = cn_test_id.idx; |
| 88 | req->range = 10; |
| 89 | |
| 90 | /* |
| 91 | * Val 0. |
| 92 | */ |
| 93 | req++; |
| 94 | req->first = cn_test_id.val; |
| 95 | req->range = 10; |
| 96 | |
| 97 | /* |
| 98 | * Val 1. |
| 99 | */ |
| 100 | req++; |
| 101 | req->first = cn_test_id.val + 20; |
| 102 | req->range = 10; |
| 103 | |
Carlo Comin | 18a0c23 | 2005-11-08 09:38:56 -0800 | [diff] [blame] | 104 | NETLINK_CB(skb).dst_group = ctl->group; |
Evgeniy Polyakov | 7672d0b | 2005-09-11 19:15:07 -0700 | [diff] [blame] | 105 | //netlink_broadcast(nls, skb, 0, ctl->group, GFP_ATOMIC); |
| 106 | netlink_unicast(nls, skb, 0, 0); |
| 107 | |
Mike Frysinger | 37cf2b8 | 2009-07-17 10:14:26 -0700 | [diff] [blame] | 108 | pr_info("request was sent: group=0x%x\n", ctl->group); |
Evgeniy Polyakov | 7672d0b | 2005-09-11 19:15:07 -0700 | [diff] [blame] | 109 | |
| 110 | return 0; |
Evgeniy Polyakov | 7672d0b | 2005-09-11 19:15:07 -0700 | [diff] [blame] | 111 | } |
Jaswinder Singh Rajput | 28f06c6 | 2009-06-17 16:26:30 -0700 | [diff] [blame] | 112 | #endif |
Evgeniy Polyakov | 7672d0b | 2005-09-11 19:15:07 -0700 | [diff] [blame] | 113 | |
| 114 | static u32 cn_test_timer_counter; |
Kees Cook | 0d69423 | 2017-10-16 16:02:25 -0700 | [diff] [blame] | 115 | static void cn_test_timer_func(struct timer_list *unused) |
Evgeniy Polyakov | 7672d0b | 2005-09-11 19:15:07 -0700 | [diff] [blame] | 116 | { |
| 117 | struct cn_msg *m; |
| 118 | char data[32]; |
| 119 | |
Kees Cook | 0d69423 | 2017-10-16 16:02:25 -0700 | [diff] [blame] | 120 | pr_debug("%s: timer fired\n", __func__); |
Mike Frysinger | 37cf2b8 | 2009-07-17 10:14:26 -0700 | [diff] [blame] | 121 | |
Yoann Padioleau | dd00cc4 | 2007-07-19 01:49:03 -0700 | [diff] [blame] | 122 | m = kzalloc(sizeof(*m) + sizeof(data), GFP_ATOMIC); |
Evgeniy Polyakov | 7672d0b | 2005-09-11 19:15:07 -0700 | [diff] [blame] | 123 | if (m) { |
Evgeniy Polyakov | 7672d0b | 2005-09-11 19:15:07 -0700 | [diff] [blame] | 124 | |
| 125 | memcpy(&m->id, &cn_test_id, sizeof(m->id)); |
| 126 | m->seq = cn_test_timer_counter; |
| 127 | m->len = sizeof(data); |
| 128 | |
| 129 | m->len = |
| 130 | scnprintf(data, sizeof(data), "counter = %u", |
| 131 | cn_test_timer_counter) + 1; |
| 132 | |
| 133 | memcpy(m + 1, data, m->len); |
| 134 | |
David Fries | ac8f733 | 2014-01-15 22:29:19 -0600 | [diff] [blame] | 135 | cn_netlink_send(m, 0, 0, GFP_ATOMIC); |
Evgeniy Polyakov | 7672d0b | 2005-09-11 19:15:07 -0700 | [diff] [blame] | 136 | kfree(m); |
| 137 | } |
| 138 | |
| 139 | cn_test_timer_counter++; |
| 140 | |
Mike Frysinger | 37cf2b8 | 2009-07-17 10:14:26 -0700 | [diff] [blame] | 141 | mod_timer(&cn_test_timer, jiffies + msecs_to_jiffies(1000)); |
Evgeniy Polyakov | 7672d0b | 2005-09-11 19:15:07 -0700 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | static int cn_test_init(void) |
| 145 | { |
| 146 | int err; |
| 147 | |
| 148 | err = cn_add_callback(&cn_test_id, cn_test_name, cn_test_callback); |
| 149 | if (err) |
| 150 | goto err_out; |
| 151 | cn_test_id.val++; |
| 152 | err = cn_add_callback(&cn_test_id, cn_test_name, cn_test_callback); |
| 153 | if (err) { |
| 154 | cn_del_callback(&cn_test_id); |
| 155 | goto err_out; |
| 156 | } |
| 157 | |
Kees Cook | 0d69423 | 2017-10-16 16:02:25 -0700 | [diff] [blame] | 158 | timer_setup(&cn_test_timer, cn_test_timer_func, 0); |
Mike Frysinger | 37cf2b8 | 2009-07-17 10:14:26 -0700 | [diff] [blame] | 159 | mod_timer(&cn_test_timer, jiffies + msecs_to_jiffies(1000)); |
| 160 | |
| 161 | pr_info("initialized with id={%u.%u}\n", |
| 162 | cn_test_id.idx, cn_test_id.val); |
Evgeniy Polyakov | 7672d0b | 2005-09-11 19:15:07 -0700 | [diff] [blame] | 163 | |
| 164 | return 0; |
| 165 | |
| 166 | err_out: |
| 167 | if (nls && nls->sk_socket) |
| 168 | sock_release(nls->sk_socket); |
| 169 | |
| 170 | return err; |
| 171 | } |
| 172 | |
| 173 | static void cn_test_fini(void) |
| 174 | { |
| 175 | del_timer_sync(&cn_test_timer); |
| 176 | cn_del_callback(&cn_test_id); |
| 177 | cn_test_id.val--; |
| 178 | cn_del_callback(&cn_test_id); |
| 179 | if (nls && nls->sk_socket) |
| 180 | sock_release(nls->sk_socket); |
| 181 | } |
| 182 | |
| 183 | module_init(cn_test_init); |
| 184 | module_exit(cn_test_fini); |
| 185 | |
| 186 | MODULE_LICENSE("GPL"); |
Evgeniy Polyakov | acb9c1b | 2009-07-21 12:43:51 -0700 | [diff] [blame] | 187 | MODULE_AUTHOR("Evgeniy Polyakov <zbr@ioremap.net>"); |
Evgeniy Polyakov | 7672d0b | 2005-09-11 19:15:07 -0700 | [diff] [blame] | 188 | MODULE_DESCRIPTION("Connector's test module"); |