+++ /dev/null
-obj-m += canethgw.o
-
--include Makefile.local
-# /usr/src/linux/_build_qemu
-
-KERNEL_DIR ?= /lib/modules/`uname -r`/build
-
-all:
- make -C $(KERNEL_DIR) M=`pwd`
-install:
- make -C $(KERNEL_DIR) M=`pwd` modules_install
-clean:
- make -C $(KERNEL_DIR) M=`pwd` clean
-
-bench:
- $(MAKE) KERNEL_DIR=../distro/kernel-bench
-debug:
- $(MAKE) KERNEL_DIR=../distro/kernel-debug DEBUG_FLAGS+=-ggdb
-
+++ /dev/null
-/*
- * Copyright: (c) 2012 Czech Technical University in Prague
- *
- * Authors:
- * Radek Matějka <radek.matejka@gmail.com>
- * Michal Sojka <sojkam1@fel.cvut.cz>
- *
- * Funded by: Volkswagen Group Research
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU General Public License
- * as published by the Free Software Foundation; either version
- * 2 of the License, or (at your option) any later version.
- */
-
-#include <linux/module.h>
-#include <linux/kernel.h>
-#include <linux/kthread.h>
-#include <linux/file.h>
-#include <net/sock.h>
-#include <linux/can.h>
-#include <linux/miscdevice.h>
-#include "canethgw.h"
-
-MODULE_LICENSE("GPL");
-
-static int cegw_udp2can(void *data);
-static int cegw_udp_send(struct socket *udp_sock, struct can_frame *cf,
- struct sockaddr* addr, int addrlen);
-static int cegw_can2udp(void *data);
-static int cegw_can_send(struct socket *can_sock, struct can_frame *cf);
-static int cegw_thread_start(void *data);
-static int cegw_thread_stop(struct cegw_job *job);
-static void cegw_job_release(struct kref *ref);
-
-static int cegw_udp_send(struct socket *udp_sock, struct can_frame *cf, struct sockaddr *addr,
- int addrlen)
-{
- struct msghdr mh;
- struct kvec vec;
- int err;
-
- mh.msg_name = addr;
- mh.msg_namelen = addrlen;
- mh.msg_control = NULL;
- mh.msg_controllen = 0;
- mh.msg_flags = 0;
-
- vec.iov_base = cf;
- vec.iov_len = sizeof(*cf);
-
- err = kernel_sendmsg(udp_sock, &mh, &vec, 1, sizeof(*cf));
-
- return err;
-}
-
-static int cegw_can_send(struct socket* can_sock, struct can_frame* cf)
-{
- struct msghdr mh;
- struct kvec vec;
- int err;
-
- mh.msg_name = NULL;
- mh.msg_namelen = 0;
- mh.msg_control = NULL;
- mh.msg_controllen = 0;
- mh.msg_flags = 0;
-
- vec.iov_base = cf;
- vec.iov_len = sizeof(*cf);
-
- err = kernel_sendmsg(can_sock, &mh, &vec, 1, sizeof(*cf));
-
- return err;
-}
-
-/**
- * cegw_udp2can - performs udp->can routing
- *
- * This function is run as a thread.
- */
-static int cegw_udp2can(void *data)
-{
- struct can_frame cf;
- struct kvec vec;
- struct msghdr mh;
- struct cegw_job *job = (struct cegw_job *)data;
- struct socket *udp_sock = NULL, *can_sock = NULL;
- int ret = 0;
-
- memset(&mh, 0, sizeof(mh));
- udp_sock = job->udp_sock;
- can_sock = job->can_sock;
-
- while (1) {
- vec.iov_base = &cf;
- vec.iov_len = sizeof(cf);
- ret = kernel_recvmsg(udp_sock, &mh, &vec, 1,
- sizeof(cf), 0);
- if (ret < 1)
- break;
-
- cf.can_id = be32_to_cpu(cf.can_id);
- cegw_can_send(can_sock, &cf);
- }
-
- cegw_thread_stop(job);
- kref_put(&job->refcount, cegw_job_release);
- do_exit(ret);
-}
-
-/**
- * cegw_can2udp - performs can->udp routing
- *
- * This function is run as a thread.
- */
-static int cegw_can2udp(void* data)
-{
- struct msghdr mh;
- struct kvec vec;
- struct sockaddr *udst;
- struct can_frame cf;
- struct cegw_job* job = (struct cegw_job*)data;
- struct socket* udp_sock = job->udp_sock;
- struct socket* can_sock = job->can_sock;
- int i;
- int ret;
-
- memset(&mh, 0, sizeof(mh));
-
- while (1) {
- vec.iov_base = &cf;
- vec.iov_len = sizeof(cf);
-
- ret = kernel_recvmsg(can_sock, &mh, &vec, 1,
- sizeof(cf), 0);
- if (ret < 1)
- break;
-
- cf.can_id = cpu_to_be32(cf.can_id);
- for (i=0; i<job->udp_dstcnt; i++) {
- udst = (struct sockaddr *)(job->udp_dst + i*job->udp_addrlen);
- cegw_udp_send(udp_sock, &cf, udst, job->udp_addrlen);
- }
- }
-
- cegw_thread_stop(job);
- kref_put(&job->refcount, cegw_job_release);
- do_exit(ret);
-}
-
-static void cegw_job_release(struct kref *ref)
-{
- struct cegw_job *job = container_of(ref, struct cegw_job, refcount);
-
- fput(job->can_sock->file);
- fput(job->udp_sock->file);
- kfree(job);
-}
-
-/**
- * cegw_thread_start - start working threads
- * @data: (struct cegw_job *) with sockets and udp addresses filled in
- *
- * Two threads are started. One is serving udp->can routing and the other
- * can->udp.
- */
-static int cegw_thread_start(void *data)
-{
- struct task_struct *task = NULL;
- struct cegw_job *job = (struct cegw_job *)data;
-
- kref_init(&job->refcount);
- kref_get(&job->refcount);
-
- task = kthread_run(cegw_udp2can, data, "canethgw_udp2can");
- if (IS_ERR(task)) {
- kref_sub(&job->refcount, 2, cegw_job_release);
- return -ENOMEM;
- }
-
- task = kthread_run(cegw_can2udp, data, "canethgw_can2udp");
- if (IS_ERR(task)) {
- cegw_thread_stop(job);
- kref_put(&job->refcount, cegw_job_release);
- return -ENOMEM;
- }
-
- return 0;
-}
-
-/**
- * cegw_thread_stop - stops threads
- */
-static int cegw_thread_stop(struct cegw_job *job)
-{
- int how = SHUT_RDWR;
- struct sock *sk = NULL;
- struct socket *udp_sock = job->udp_sock;
- struct socket *can_sock = job->can_sock;
-
- kernel_sock_shutdown(udp_sock, SHUT_RDWR);
-
- /* PF_CAN sockets do not implement shutdown - do it manualy */
- sk = can_sock->sk;
- how++;
- lock_sock(sk);
- sk->sk_shutdown |= how;
- sk->sk_state_change(sk);
- release_sock(sk);
-
- return 0;
-}
-
-static int cegw_open(struct inode *inode, struct file *file)
-{
- file->private_data = NULL;
-
- if (try_module_get(THIS_MODULE) == false)
- return -EAGAIN;
-
- return 0;
-}
-
-static int cegw_release(struct inode *inode, struct file *file)
-{
- struct cegw_job *job = (struct cegw_job *)file->private_data;
-
- if (job) {
- cegw_thread_stop(job);
- }
-
- module_put(THIS_MODULE);
- return 0;
-}
-
-/**
- * cegw_ioctl_start - processes ioctl CEGW_IOCTL_START call
- *
- * The function takes over cegw_ioctl structure from userspace and
- * prepares cegw_job structure. The cegw_job is stored in
- * file->private_data and used by kernel threads to serve gateway
- * functionality.
- */
-static long cegw_ioctl_start(struct file *file, unsigned long arg)
-{
- int i;
- int chckfam;
- int err = 0;
- __u32 dstcnt = 0;
- __u32 addrlen = 0;
- struct sockaddr *sa;
- struct cegw_ioctl gwctl;
- struct cegw_job *job = NULL;
-
- err = copy_from_user(&gwctl, (void __user *)arg, sizeof(gwctl));
- if (err != 0)
- return -EFAULT;
-
- dstcnt = gwctl.udp_dstcnt;
- addrlen = gwctl.udp_addrlen;
-
- if (addrlen != sizeof(struct sockaddr_in) && addrlen != sizeof(struct sockaddr_in6))
- return -EAFNOSUPPORT;
-
- /* ToDo: consider dstcnt maximum */
- job = kmalloc(GFP_KERNEL, sizeof(*job) + dstcnt*addrlen);
- if (job == NULL)
- return -ENOMEM;
-
- err = copy_from_user(&job->udp_dst, (void __user *)(arg + sizeof(struct cegw_ioctl)), dstcnt*addrlen);
- if (err != 0) {
- kfree(job);
- return -EFAULT;
- }
-
- /* */
- if (dstcnt > 0)
- sa = (struct sockaddr *)job->udp_dst;
- chckfam = sa->sa_family;
-
- for (i=1; i<dstcnt; i++) {
- sa = (struct sockaddr *)(job->udp_dst + i*addrlen);
- if (sa->sa_family != chckfam) {
- kfree(job);
- return -EAFNOSUPPORT;
- }
- }
-
- job->udp_sock = sockfd_lookup(gwctl.udp_sock, &err);
- if (job->udp_sock == NULL) {
- kfree(job);
- return err;
- }
-
- job->can_sock = sockfd_lookup(gwctl.can_sock, &err);
- if (job->can_sock == NULL) {
- fput(job->udp_sock->file);
- kfree(job);
- return err;
- }
-
- job->udp_dstcnt = dstcnt;
- job->udp_addrlen = addrlen;
-
- err = cegw_thread_start(job);
- if (err != 0)
- return err;
-
- file->private_data = job;
- return 0;
-}
-
-static long cegw_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
-{
- int err;
-
- switch (cmd) {
- case CEGW_IOCTL_START:
- err = cegw_ioctl_start(file, arg);
- break;
- default:
- err = -EOPNOTSUPP;
- break;
- }
-
- return err;
-}
-
-static const struct file_operations cegw_fops = {
- .owner = THIS_MODULE,
- .open = cegw_open,
- .release = cegw_release,
- .unlocked_ioctl = cegw_ioctl
-};
-
-static struct miscdevice cegw_device = {
- .minor = MISC_DYNAMIC_MINOR,
- .name = "canethgw",
- .fops = &cegw_fops
-};
-
-static int __init cegw_init(void)
-{
- misc_register(&cegw_device);
-
- return 0;
-}
-
-static void __exit cegw_exit(void)
-{
- misc_deregister(&cegw_device);
-
- return;
-}
-
-module_init(cegw_init);
-module_exit(cegw_exit);
-
+++ /dev/null
-#ifndef CANETHGW_H
-#define CANETHGW_H
-
-#include <linux/types.h>
-
-struct cegw_ioctl
-{
- __u32 can_sock;
- __u32 udp_sock;
- __u32 udp_dstcnt;
- __u32 udp_addrlen;
- __u8 udp_dst[0];
-};
-
-#ifdef __KERNEL__
-struct cegw_job
-{
- struct kref refcount;
- struct socket* can_sock;
- struct socket* udp_sock;
- u32 udp_dstcnt;
- u32 udp_addrlen;
- u8 udp_dst[0];
-};
-#endif
-
-#define CEGW_IOCTL_BASE 'c'
-#define CEGW_IOCTL_START _IOW(CEGW_IOCTL_BASE, 0, struct cegw_ioctl)
-
-#endif /* CANETHGW_H */
-
+++ /dev/null
-PROGRAM=canethgw
-
-all:
- gcc -Wall -o${PROGRAM} -I../utils/common/include/ canethgw.c ../utils/common/cegwerr.c ../utils/common/readif.c
-
+++ /dev/null
-#include <stdio.h>
-#include <stdbool.h>
-#include <stdlib.h>
-#include <string.h>
-#include <unistd.h>
-#include <netinet/in.h>
-#include <signal.h>
-#include <getopt.h>
-#include <sys/ioctl.h>
-#include <sys/types.h>
-#include <sys/socket.h>
-#include <arpa/inet.h>
-#include <net/if.h>
-#include <linux/can.h>
-#include <linux/can/raw.h>
-#include <poll.h>
-#include "readif.h"
-#include "cegwerr.h"
-
-/**
- * ToDo:
- * [ ] print usage
- */
-//#define GW_DEBUG
-#ifdef GW_DEBUG
-#define printdbg(x) printf(x)
-#else
-#define printdbg(x)
-#endif
-
-#define GW_POLL_ETH 0
-#define GW_POLL_CAN 1
-
-#define GW_FLAG_SRC 1
-#define GW_FLAG_DST 2
-#define GW_FLAG_LISTEN 4
-#define GW_FLAG_FILTER 8
-
-unsigned int opt_flag = 0;
-int sock_udp;
-int sock_can;
-struct can_filter flt;
-struct sockaddr_in udp_dest;
-
-/**
- * filter_check
- * @return true if frame should be dropped
- */
-bool filter_check( struct can_frame* cf )
-{
- if( !(opt_flag & GW_FLAG_FILTER) )
- return false;
-
- return ( (cf->can_id & flt.can_mask) == (flt.can_id & flt.can_mask) );
-}
-
-void send_to_can()
-{
- struct can_frame cf;
-
- read( sock_udp, &cf, sizeof(cf) );
-
- if( filter_check( &cf ) )
- {
- printdbg( "filter: drop\n" );
- return;
- }
-
- write( sock_can, &cf, sizeof(cf) );
-
- printdbg( "eth -> can\n" );
-}
-
-void send_to_eth()
-{
- struct can_frame cf;
-
- read( sock_can, &cf, sizeof(cf) );
-
- if( filter_check( &cf ) )
- {
- printdbg( "filter: drop\n" );
- return;
- }
-
- sendto( sock_udp, &cf, sizeof(cf), 0, (struct sockaddr*) &udp_dest, sizeof(udp_dest) );
-
- printdbg( "can -> eth\n" );
-}
-
-void signal_exit( int sig )
-{
- close( sock_udp );
- close( sock_can );
- printf( "exiting\n" );
-
- exit( 0 );
-}
-
-int main( int argc, char* argv[] )
-{
- struct sockaddr_in udp_addr;
- struct sockaddr_can can_addr;
- struct pollfd pfd[2];
- int polret;
- int opt;
- struct cegw_if ceif[2], iflisten;
- struct cegw_if* ifcan,* ifeth;
-
- flt.can_id = 0;
- flt.can_mask = 0;
-
- struct option longopt[] =
- {
- {"listen", 1, NULL, 'l'},
- { 0, 0, 0, 0 }
- };
-
- while( (opt = getopt_long( argc, argv, "s:d:l:f:i:", longopt, NULL )) != -1 )
- {
- switch( opt )
- {
- case 's':
- if( read_if(optarg, &ceif[0]) != 0 )
- {
- perr( "'-s'" );
- return -1;
- }
- opt_flag |= GW_FLAG_SRC;
- break;
- case 'd':
- if( read_if(optarg, &ceif[1]) != 0 )
- {
- perr( "'-d'" );
- return -1;
- }
- opt_flag |= GW_FLAG_DST;
- break;
- case 'l':
- if( read_if(optarg, &iflisten) != 0 )
- {
- perr( "'-l'" );
- return -1;
- }
- if( iflisten.type != IF_ETH_UDP )
- {
- perr( "'-l' udp interface expected" );
- return -1;
- }
- opt_flag |= GW_FLAG_LISTEN;
- break;
- case 'f':
- if( sscanf( optarg, "%x:%x", &flt.can_id,
- &flt.can_mask ) != 2 )
- {
- perr( "bad filter format");
- return -1;
- }
- opt_flag |= GW_FLAG_FILTER;
- break;
- case '?':
- return -1;
- break;
- }
- }
-
- if( !((opt_flag & GW_FLAG_SRC) && (opt_flag & GW_FLAG_DST)
- && (opt_flag & GW_FLAG_LISTEN)) )
- {
- perr( "'s', 'd' and 'l' are mandatory" );
- /* ToDo: usage? */
- return -1;
- }
-
- if( !(ceif[0].type == IF_CAN && ceif[1].type == IF_ETH_UDP) &&
- !(ceif[0].type == IF_ETH_UDP && ceif[1].type == IF_CAN ) )
- {
- perr( "'-s' and '-d' should be different interface type" );
- return -1;
- }
-
- if( ceif[0].type == IF_CAN )
- {
- ifcan = &ceif[0];
- ifeth = &ceif[1];
- } else
- {
- ifcan = &ceif[1];
- ifeth = &ceif[0];
- }
-
- signal( SIGINT, signal_exit );
-
- /* prepare udp destination */
- udp_dest.sin_family = AF_INET;
- udp_dest.sin_port = htons(ifeth->eth.port);
- udp_dest.sin_addr = ifeth->eth.ip;
-
- /* udp socket */
- sock_udp = socket( PF_INET, SOCK_DGRAM, IPPROTO_UDP );
- if( sock_udp < 0 )
- {
- perr( "udp socket creation failed" );
- return -1;
- }
-
- udp_addr.sin_family = AF_INET;
- udp_addr.sin_port = htons(iflisten.eth.port);
- udp_addr.sin_addr = iflisten.eth.ip;
-
- if( bind( sock_udp, (struct sockaddr*) &udp_addr, sizeof(udp_addr) ) < 0 )
- {
- perr( "udp socket binding failed" );
- close( sock_udp );
- return -1;
- }
-
- /* can socket */
- sock_can = socket( PF_CAN, SOCK_RAW, CAN_RAW );
- if( sock_can < 0 )
- {
- perr( "can socket creation failed");
- close( sock_can );
- return -1;
- }
-
- can_addr.can_family = AF_CAN;
- can_addr.can_ifindex = ifcan->can.ifindex;
-
- if( bind( sock_can, (struct sockaddr*) &can_addr, sizeof(can_addr) ) < 0 )
- {
- perr( "can socket binding failed" );
- return -1;
- }
-
- /* poll */
- pfd[GW_POLL_ETH].fd = sock_udp;
- pfd[GW_POLL_ETH].events = POLLIN | POLLPRI;
- pfd[GW_POLL_ETH].revents = 0;
-
- pfd[GW_POLL_CAN].fd = sock_can;
- pfd[GW_POLL_CAN].events = POLLIN | POLLPRI;
- pfd[GW_POLL_CAN].revents = 0;
-
- printf( "canethgw is running\n" );
-
- while( 1 )
- {
- printdbg( "polling\n" );
- polret = poll( pfd, 2, -1 );
- if( polret < 0 )
- {
- perr( "poll(..) failed" );
- close( sock_udp );
- close( sock_can );
- return -1;
- }
-
- if( pfd[GW_POLL_ETH].revents != 0 )
- {
- send_to_can();
- }
- if( pfd[GW_POLL_CAN].revents != 0 )
- {
- send_to_eth();
- }
-
- pfd[GW_POLL_ETH].revents = 0;
- pfd[GW_POLL_CAN].revents = 0;
- }
-
- return 0;
-}
-
+++ /dev/null
-all: include
- gcc -Wall -ocegw -Iinclude cegw.c
-debug: include
- gcc -Wall -ggdb -D_DEBUG -ocegw -Iinclude cegw.c
-
-include: ../../linux/include/uapi/linux/can/canethgw.h
- make -C ../../linux headers_install INSTALL_HDR_PATH=$(CURDIR)
+++ /dev/null
-/*
- * Copyright: (c) 2012 Czech Technical University in Prague
- *
- * Authors:
- * Radek Matějka <radek.matejka@gmail.com>
- * Michal Sojka <sojkam1@fel.cvut.cz>
- *
- * Funded by: Volkswagen Group Research
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU General Public License
- * as published by the Free Software Foundation; either version
- * 2 of the License, or (at your option) any later version.
- */
-
-
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <sys/socket.h>
-#include <netdb.h>
-#include <net/if.h>
-#include <arpa/inet.h>
-#include <linux/can.h>
-#include <limits.h>
-#include <unistd.h>
-#include <fcntl.h>
-#include <sys/ioctl.h>
-#include <sys/types.h>
-#include <linux/can/raw.h>
-#include <linux/can/canethgw.h>
-
-unsigned int cegw_errno = 0;
-
-static const char help_msg[] = "usage:\n"
- " %s <can_if>[,filter]* <udp_listen_addr>:<port> <udp_dest_addr>:<port>\n"
- " [list of additional udp recipients <addr>:<port>]\n"
- "example:\n"
- " %s can0 192.168.0.1:10501 192.168.0.4:980 192.168.0.7:1160\n\n"
- " Executing this command will set the gateway so that it will\n"
- " listen for udp messages on 192.168.0.1:10501 and send them\n"
- " to can0. Simultaneously, it will send all messages from can0\n"
- " to 192.168.0.4:980 and 192.168.0.7:1160 via udp. The message is\n"
- " therefore cloned. Notice that there can be more udp recipients.\n"
- " The can filter is specified in the same way as in candump utility.\n";
-
-struct addrinfo hints = {
- .ai_socktype = SOCK_DGRAM
-};
-
-/**
- * readsockaddr - parses @in for eth address.
- * Valid input is e.g. 127.0.0.1:10502. If parsing fails
- * the cause is stored in cegw_errno. Please note that
- * the function modifies content of arg.
- *
- * @param[in] arg hostname:port string
- * @param[out] addr filled sockaddr_in structure
- * @return 0 on success, -1 otherwise
- */
-int readsockaddr(char *arg, struct sockaddr *addr, int ai_family)
-{
- int ret;
- char *delim;
- struct addrinfo *res;
-
- delim = strchr(arg, ':');
-
- if (delim == NULL) {
- fprintf(stderr, "expected ':' (<hostname>:<port>)");
- exit(1);
- }
-
- *delim = '\0';
- delim++;
-
- hints.ai_family = ai_family;
-
- ret = getaddrinfo(arg, delim, &hints, &res);
- if (ret != 0) {
- fprintf(stderr, "getaddrinfo failed: %s\n", gai_strerror(ret));
- exit(1);
- }
-
- memcpy(addr, res->ai_addr, res->ai_addrlen);
-
- freeaddrinfo(res);
- return 0;
-}
-
-/**
- * readfilter - reads can filter definition from nptr
- */
-int readfilter(char *nptr, struct can_filter **filter, int *out_numfilter, can_err_mask_t *err_mask)
-{
- char *ptr;
- int numfilter;
- struct can_filter *rfilter;
-
- numfilter = 0;
- ptr = nptr;
- while (ptr) {
- numfilter++;
- ptr++; /* hop behind the ',' */
- ptr = strchr(ptr, ','); /* exit condition */
- }
-
- rfilter = malloc(numfilter * sizeof(*rfilter));
- if (!rfilter) {
- fprintf(stderr, "filter malloc failed");
- exit(1);
- }
-
- numfilter = 0;
- *err_mask = 0;
-
- while (nptr) {
-
- ptr = nptr+1; /* hop behind the ',' */
- nptr = strchr(ptr, ','); /* update exit condition */
-
- if (sscanf(ptr, "%x:%x",
- &rfilter[numfilter].can_id,
- &rfilter[numfilter].can_mask) == 2) {
- rfilter[numfilter].can_mask &= ~CAN_ERR_FLAG;
- numfilter++;
- } else if (sscanf(ptr, "%x~%x",
- &rfilter[numfilter].can_id,
- &rfilter[numfilter].can_mask) == 2) {
- rfilter[numfilter].can_id |= CAN_INV_FILTER;
- rfilter[numfilter].can_mask &= ~CAN_ERR_FLAG;
- numfilter++;
- } else if (sscanf(ptr, "#%x", err_mask) != 1) {
- fprintf(stderr, "filter parsing failed");
- exit(1);
- }
- }
-
- *filter = rfilter;
- *out_numfilter = numfilter;
- return 0;
-}
-
-int main(int argc, char *argv[])
-{
- int i;
- int fd;
- int tmpi;
- int dstcnt;
- char *nptr;
- int numfilter = 0;
- int udp_sock, can_sock;
- int addrlen;
- can_err_mask_t err_mask = 0;
- struct sockaddr_can can_addr;
- struct sockaddr *dst = NULL;
- struct cegw_ioctl *gwctl = NULL;
- struct can_filter *filter = NULL;
-
- /* udp_addr can store both - in and in6 addresses */
- struct sockaddr_in6 udp6_addr;
- struct sockaddr *udp_addr = (struct sockaddr *) &udp6_addr;
-
-
- if (argc == 1 || (argc == 2 && strcmp(argv[1], "-h") == 0)) {
- printf(help_msg, argv[0], argv[0]);
- return 0;
- }
-
- if (argc < 4) {
- fprintf(stderr, "not enough arguments\n");
- printf(help_msg, argv[0], argv[0]);
- return 1;
- }
-
- dstcnt = argc-3;
-
- for (i=1; i<argc; i++) {
- switch (i) {
- case 1: /* can ifindex */
- nptr = strchr(argv[i], ',');
- if (nptr) {
- *nptr = '\0';
- }
-
- can_addr.can_family = AF_CAN;
- tmpi = if_nametoindex(argv[i]);
- if (tmpi == 0) {
- fprintf(stderr, "given can interface not found\n");
- return 1;
- }
-
- can_addr.can_ifindex = tmpi;
- break;
- case 2: /* listen addr */
- readsockaddr(argv[i], udp_addr, AF_UNSPEC);
- switch (udp_addr->sa_family) {
- case AF_INET:
- addrlen = sizeof(struct sockaddr_in);
- break;
- case AF_INET6:
- addrlen = sizeof(struct sockaddr_in6);
- break;
- default:
- fprintf(stderr, "unexpected sockaddr family");
- break;
- }
- gwctl = (struct cegw_ioctl*)malloc(sizeof(*gwctl) + dstcnt*addrlen);
- break;
- default: /* udp destination */
- dst = (struct sockaddr *)(gwctl->udp_dst + (i-3)*addrlen);
- readsockaddr(argv[i], dst, udp_addr->sa_family);
-
- break;
- }
- }
-
- /* prepare udp socket */
- udp_sock = socket(udp_addr->sa_family, SOCK_DGRAM, IPPROTO_UDP);
- if (udp_sock == -1) {
- perror("udp socket(..)");
- return 1;
- }
-
- if (bind(udp_sock, udp_addr, addrlen) != 0) {
- perror("bind(udp)");
- return 1;
- }
-
- /* prepare can socket */
- can_sock = socket(PF_CAN, SOCK_RAW, CAN_RAW);
- if (can_sock == -1) {
- perror("can socket(..)");
- return 1;
- }
-
- if (bind(can_sock, (struct sockaddr *)&can_addr, sizeof(struct sockaddr_can)) != 0) {
- perror("bind(can)");
- return 1;
- }
-
- /* can filter */
- if (nptr)
- readfilter(nptr, &filter, &numfilter, &err_mask);
-
- if (err_mask)
- setsockopt(can_sock, SOL_CAN_RAW, CAN_RAW_ERR_FILTER,
- &err_mask, sizeof(err_mask));
-
- if (numfilter)
- setsockopt(can_sock, SOL_CAN_RAW, CAN_RAW_FILTER,
- filter, numfilter * sizeof(struct can_filter));
- free(filter);
-
- /* send it to kernel gateway */
- fd = open("/dev/canethgw", O_RDONLY);
- if (fd == -1) {
- perror("/dev/canethgw");
- return 1;
- }
-
- gwctl->can_sock = can_sock;
- gwctl->udp_sock = udp_sock;
- gwctl->udp_dstcnt = dstcnt;
- gwctl->udp_addrlen = addrlen;
-
- if (ioctl(fd, CEGW_IOCTL_START, gwctl) != 0) {
- perror("ioctl");
- return 1;
- }
- printf("gateway successfully set and running\n");
- free(gwctl);
-
- /* sleep until someone kills me */
- pause();
-
- return 0;
-}