3 #include <linux/module.h>
4 #include <linux/kernel.h>
5 #include <linux/kthread.h>
6 #include <linux/sched.h>
7 #include <linux/delay.h>
8 #include <linux/wait.h>
9 #include <linux/netdevice.h>
10 #include <linux/file.h>
11 #include <linux/socket.h>
13 #include <linux/if_arp.h>
14 #include <linux/net.h>
15 #include <linux/netdevice.h>
16 #include <linux/can/core.h>
17 #include <linux/can.h>
18 #include <net/rtnetlink.h>
19 #include <linux/completion.h>
20 #include <linux/mutex.h>
21 #include <linux/miscdevice.h>
22 #include <net/inet_common.h>
25 MODULE_LICENSE("GPL");
27 static int cegw_udp2can(void *data);
28 static int cegw_udp_send(struct socket *udp_sock, struct can_frame *cf,
29 struct sockaddr_in* addr);
30 static int cegw_can2udp(void *data);
31 static int cegw_can_send(struct socket *can_sock, struct can_frame *cf);
32 static int cegw_thread_start(void *data);
33 static int cegw_thread_stop(struct cegw_job *job);
34 static void cegw_job_release(struct kref *ref);
44 struct in_addr eth_ip;
45 unsigned short eth_port;
46 struct hlist_node list;
50 struct in_addr eth_ip;
51 unsigned short eth_port;
54 static int cegw_udp_send(struct socket *udp_sock, struct can_frame *cf, struct sockaddr_in* addr)
61 mh.msg_namelen = sizeof(*addr);
62 mh.msg_control = NULL;
63 mh.msg_controllen = 0;
67 vec.iov_len = sizeof(*cf);
69 /* FIXME: Convert endianing of cf->can_id */
70 err = kernel_sendmsg(udp_sock, &mh, &vec, 1, sizeof(*cf));
75 static int cegw_can_send(struct socket* can_sock, struct can_frame* cf)
83 mh.msg_control = NULL;
84 mh.msg_controllen = 0;
88 vec.iov_len = sizeof(*cf);
90 err = kernel_sendmsg(can_sock, &mh, &vec, 1, sizeof(*cf));
96 * cegw_udp2can - performs udp->can routing
98 * This function is run as a thread.
100 static int cegw_udp2can(void *data)
105 struct cegw_job *job = (struct cegw_job *)data;
106 struct socket *udp_sock = NULL, *can_sock = NULL;
109 memset(&mh, 0, sizeof(mh));
110 udp_sock = job->udp_sock;
111 can_sock = job->can_sock;
115 vec.iov_len = sizeof(cf);
116 ret = kernel_recvmsg(udp_sock, &mh, &vec, 1,
121 /* FIXME: Convert endianing of cf.can_id */
122 cegw_can_send(can_sock, &cf);
125 cegw_thread_stop(job);
126 kref_put(&job->refcount, cegw_job_release);
131 * cegw_can2udp - performs can->udp routing
135 static int cegw_can2udp(void* data)
140 struct cegw_job* job = (struct cegw_job*)data;
141 struct socket* udp_sock = job->udp_sock;
142 struct socket* can_sock = job->can_sock;
146 memset(&mh, 0, sizeof(mh));
150 vec.iov_len = sizeof(cf);
152 ret = kernel_recvmsg(can_sock, &mh, &vec, 1,
157 for (i=0; i<job->udp_dstcnt; i++) {
158 cegw_udp_send(udp_sock, &cf, &job->udp_dst[i]);
162 cegw_thread_stop(job);
163 kref_put(&job->refcount, cegw_job_release);
167 static void cegw_job_release(struct kref *ref)
169 struct cegw_job *job = container_of(ref, struct cegw_job, refcount);
171 fput(job->can_sock->file);
172 fput(job->udp_sock->file);
177 * cegw_thread_start - start working threads
178 * @data: (struct cegw_job *) with sockets and udp addresses filled in
180 * Two threads are started. One is serving udp->can routing and the other
183 static int cegw_thread_start(void *data)
185 struct task_struct *task = NULL;
186 struct cegw_job *job = (struct cegw_job *)data;
188 kref_init(&job->refcount);
189 kref_get(&job->refcount);
191 task = kthread_run(cegw_udp2can, data, "canethgw_udp2can");
193 kref_sub(&job->refcount, 2, cegw_job_release);
197 task = kthread_run(cegw_can2udp, data, "canethgw_can2udp");
199 cegw_thread_stop(job);
200 kref_put(&job->refcount, cegw_job_release);
208 * cegw_thread_stop - stops threads
210 static int cegw_thread_stop(struct cegw_job *job)
213 struct sock *sk = NULL;
214 struct socket *udp_sock = job->udp_sock;
215 struct socket *can_sock = job->can_sock;
217 kernel_sock_shutdown(udp_sock, SHUT_RDWR);
219 /* PF_CAN sockets do not implement shutdown - do it manualy */
223 sk->sk_shutdown |= how;
224 sk->sk_state_change(sk);
230 static int cegw_open(struct inode *inode, struct file *file)
232 file->private_data = NULL;
234 if (try_module_get(THIS_MODULE) == false)
240 static int cegw_release(struct inode *inode, struct file *file)
242 struct cegw_job *job = (struct cegw_job *)file->private_data;
245 cegw_thread_stop(job);
248 module_put(THIS_MODULE);
253 * cegw_ioctl_start - processes ioctl CEGW_IOCTL_START call
255 * The function takes over cegw_ioctl structure from userspace and
256 * prepares cegw_job structure. The cegw_job is stored in
257 * file->private_data and used by kernel threads to serve gateway
260 static long cegw_ioctl_start(struct file *file, unsigned long arg)
266 struct cegw_ioctl gwctl;
267 struct cegw_job *job = NULL;
269 err = copy_from_user(&gwctl, (void __user *)arg, sizeof(gwctl));
273 dstcnt = gwctl.udp_dstcnt;
274 addrlen = gwctl.udp_addrlen;
276 if (addrlen != sizeof(struct sockaddr_in))
277 return -EAFNOSUPPORT;
280 job = kmalloc(GFP_KERNEL, sizeof(*job) + dstcnt*addrlen );
284 err = copy_from_user(&job->udp_dst, (void __user *)(arg + sizeof(struct cegw_ioctl)), dstcnt*addrlen);
290 for (i=0; i<dstcnt; i++) {
291 if (job->udp_dst[i].sin_family != AF_INET) {
293 return -EAFNOSUPPORT;
297 job->udp_sock = sockfd_lookup( gwctl.udp_sock, &err );
298 if (job->udp_sock == NULL) {
303 job->can_sock = sockfd_lookup( gwctl.can_sock, &err );
304 if (job->can_sock == NULL) {
305 fput(job->udp_sock->file);
310 job->udp_dstcnt = dstcnt;
312 err = cegw_thread_start(job);
316 file->private_data = job;
320 static long cegw_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
325 case CEGW_IOCTL_START:
326 err = cegw_ioctl_start(file, arg);
335 static const struct file_operations cegw_fops = {
336 .owner = THIS_MODULE,
338 .release = cegw_release,
339 .unlocked_ioctl = cegw_ioctl
342 static struct miscdevice cegw_device = {
343 .minor = MISC_DYNAMIC_MINOR,
348 static int __init cegw_init(void)
350 misc_register(&cegw_device);
355 static void __exit cegw_exit(void)
357 misc_deregister(&cegw_device);
359 /* ToDo: exit threads */
363 module_init(cegw_init);
364 module_exit(cegw_exit);