1 #include <linux/module.h>
2 #include <linux/kernel.h>
3 #include <linux/kthread.h>
4 #include <linux/file.h>
7 #include <linux/miscdevice.h>
10 MODULE_LICENSE("GPL");
12 static int cegw_udp2can(void *data);
13 static int cegw_udp_send(struct socket *udp_sock, struct can_frame *cf,
14 struct sockaddr_in* addr);
15 static int cegw_can2udp(void *data);
16 static int cegw_can_send(struct socket *can_sock, struct can_frame *cf);
17 static int cegw_thread_start(void *data);
18 static int cegw_thread_stop(struct cegw_job *job);
19 static void cegw_job_release(struct kref *ref);
21 static int cegw_udp_send(struct socket *udp_sock, struct can_frame *cf, struct sockaddr_in* addr)
28 mh.msg_namelen = sizeof(*addr);
29 mh.msg_control = NULL;
30 mh.msg_controllen = 0;
34 vec.iov_len = sizeof(*cf);
36 /* FIXME: Convert endianing of cf->can_id */
37 err = kernel_sendmsg(udp_sock, &mh, &vec, 1, sizeof(*cf));
42 static int cegw_can_send(struct socket* can_sock, struct can_frame* cf)
50 mh.msg_control = NULL;
51 mh.msg_controllen = 0;
55 vec.iov_len = sizeof(*cf);
57 err = kernel_sendmsg(can_sock, &mh, &vec, 1, sizeof(*cf));
63 * cegw_udp2can - performs udp->can routing
65 * This function is run as a thread.
67 static int cegw_udp2can(void *data)
72 struct cegw_job *job = (struct cegw_job *)data;
73 struct socket *udp_sock = NULL, *can_sock = NULL;
76 memset(&mh, 0, sizeof(mh));
77 udp_sock = job->udp_sock;
78 can_sock = job->can_sock;
82 vec.iov_len = sizeof(cf);
83 ret = kernel_recvmsg(udp_sock, &mh, &vec, 1,
88 /* FIXME: Convert endianing of cf.can_id */
89 cegw_can_send(can_sock, &cf);
92 cegw_thread_stop(job);
93 kref_put(&job->refcount, cegw_job_release);
98 * cegw_can2udp - performs can->udp routing
100 * This function is run as a thread.
102 static int cegw_can2udp(void* data)
107 struct cegw_job* job = (struct cegw_job*)data;
108 struct socket* udp_sock = job->udp_sock;
109 struct socket* can_sock = job->can_sock;
113 memset(&mh, 0, sizeof(mh));
117 vec.iov_len = sizeof(cf);
119 ret = kernel_recvmsg(can_sock, &mh, &vec, 1,
124 for (i=0; i<job->udp_dstcnt; i++) {
125 cegw_udp_send(udp_sock, &cf, &job->udp_dst[i]);
129 cegw_thread_stop(job);
130 kref_put(&job->refcount, cegw_job_release);
134 static void cegw_job_release(struct kref *ref)
136 struct cegw_job *job = container_of(ref, struct cegw_job, refcount);
138 fput(job->can_sock->file);
139 fput(job->udp_sock->file);
144 * cegw_thread_start - start working threads
145 * @data: (struct cegw_job *) with sockets and udp addresses filled in
147 * Two threads are started. One is serving udp->can routing and the other
150 static int cegw_thread_start(void *data)
152 struct task_struct *task = NULL;
153 struct cegw_job *job = (struct cegw_job *)data;
155 kref_init(&job->refcount);
156 kref_get(&job->refcount);
158 task = kthread_run(cegw_udp2can, data, "canethgw_udp2can");
160 kref_sub(&job->refcount, 2, cegw_job_release);
164 task = kthread_run(cegw_can2udp, data, "canethgw_can2udp");
166 cegw_thread_stop(job);
167 kref_put(&job->refcount, cegw_job_release);
175 * cegw_thread_stop - stops threads
177 static int cegw_thread_stop(struct cegw_job *job)
180 struct sock *sk = NULL;
181 struct socket *udp_sock = job->udp_sock;
182 struct socket *can_sock = job->can_sock;
184 kernel_sock_shutdown(udp_sock, SHUT_RDWR);
186 /* PF_CAN sockets do not implement shutdown - do it manualy */
190 sk->sk_shutdown |= how;
191 sk->sk_state_change(sk);
197 static int cegw_open(struct inode *inode, struct file *file)
199 file->private_data = NULL;
201 if (try_module_get(THIS_MODULE) == false)
207 static int cegw_release(struct inode *inode, struct file *file)
209 struct cegw_job *job = (struct cegw_job *)file->private_data;
212 cegw_thread_stop(job);
215 module_put(THIS_MODULE);
220 * cegw_ioctl_start - processes ioctl CEGW_IOCTL_START call
222 * The function takes over cegw_ioctl structure from userspace and
223 * prepares cegw_job structure. The cegw_job is stored in
224 * file->private_data and used by kernel threads to serve gateway
227 static long cegw_ioctl_start(struct file *file, unsigned long arg)
233 struct cegw_ioctl gwctl;
234 struct cegw_job *job = NULL;
236 err = copy_from_user(&gwctl, (void __user *)arg, sizeof(gwctl));
240 dstcnt = gwctl.udp_dstcnt;
241 addrlen = gwctl.udp_addrlen;
243 if (addrlen != sizeof(struct sockaddr_in))
244 return -EAFNOSUPPORT;
247 job = kmalloc(GFP_KERNEL, sizeof(*job) + dstcnt*addrlen );
251 err = copy_from_user(&job->udp_dst, (void __user *)(arg + sizeof(struct cegw_ioctl)), dstcnt*addrlen);
257 for (i=0; i<dstcnt; i++) {
258 if (job->udp_dst[i].sin_family != AF_INET) {
260 return -EAFNOSUPPORT;
264 job->udp_sock = sockfd_lookup(gwctl.udp_sock, &err);
265 if (job->udp_sock == NULL) {
270 job->can_sock = sockfd_lookup(gwctl.can_sock, &err);
271 if (job->can_sock == NULL) {
272 fput(job->udp_sock->file);
277 job->udp_dstcnt = dstcnt;
279 err = cegw_thread_start(job);
283 file->private_data = job;
287 static long cegw_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
292 case CEGW_IOCTL_START:
293 err = cegw_ioctl_start(file, arg);
303 static const struct file_operations cegw_fops = {
304 .owner = THIS_MODULE,
306 .release = cegw_release,
307 .unlocked_ioctl = cegw_ioctl
310 static struct miscdevice cegw_device = {
311 .minor = MISC_DYNAMIC_MINOR,
316 static int __init cegw_init(void)
318 misc_register(&cegw_device);
323 static void __exit cegw_exit(void)
325 misc_deregister(&cegw_device);
330 module_init(cegw_init);
331 module_exit(cegw_exit);