118 #include <linux/module.h>
119 #include <linux/kernel.h>
120 #include <linux/list.h>
121 #include <linux/types.h>
122 #include <linux/skbuff.h>
125 #include <uapi/linux/can.h>
126 #include <uapi/linux/if_arp.h>
127 #include <linux/can/core.h>
128 #include <linux/can/error.h>
129 #include <linux/slab.h>
130 #include <linux/if_ether.h>
131 #include <linux/netdevice.h>
132 #include <linux/crc32.h>
135 #include <asm-generic/errno-base.h>
136 #include <asm-generic/errno.h>
138 #include <linux/can/dev.h>
143 "Stefan Smarzly <stefan.smarzly@in.tum.de>");
158 return &ce_gw_job_list;
162 struct can_frame *memory;
163 memory = kmalloc(
sizeof(
struct can_frame),GFP_KERNEL);
174 struct canfd_frame *memory;
175 memory = kmalloc(
sizeof(
struct canfd_frame), GFP_KERNEL);
189 if (new_can_frame == NULL) {
190 printk (KERN_ERR
"ce_gw_main.c: kmalloc failed in function"
191 "ce_gw_get_header_can \n");
194 new_can_frame->can_id = can_id;
195 new_can_frame->can_dlc = can_dlc;
196 *(u64 *)new_can_frame->data = *(u64 *)payload;
197 return new_can_frame;
202 __u8 res0, __u8 res1, __u8 *data) {
205 printk (KERN_ERR
"ce_gw_main.c: kmalloc failed in function"
206 "ce_gw_get_header_canfd\n");
212 canfd->flags = flags;
213 canfd->__res0 = res0;
214 canfd->__res1 = res1;
215 *(u64 *)canfd->data = *(u64 *)data;
241 struct net_device *eth_dev,
struct net_device *can_dev,
242 unsigned char *mac_dst,
unsigned char *mac_src)
244 # ifdef NET_SKBUFF_DATA_USES_OFFSET
250 skb_set_transport_header(eth_skb, 0);
253 struct can_frame *eth_canf;
254 eth_canf = (
struct can_frame *)skb_push(
255 eth_skb,
sizeof(
struct can_frame));
256 skb_set_network_header(eth_skb, 0);
258 struct can_frame *can_canf;
259 can_canf = (
struct can_frame *) can_skb->data;
261 memcpy(eth_canf, can_canf,
sizeof(
struct can_frame));
264 struct ethhdr *eth_ethh;
265 eth_ethh = (
struct ethhdr *)skb_push(eth_skb,
sizeof(
struct ethhdr));
266 skb_set_mac_header(eth_skb, 0);
268 memcpy(eth_ethh->h_dest, mac_dst , ETH_ALEN);
269 memcpy(eth_ethh->h_source, mac_src, ETH_ALEN);
270 eth_ethh->h_proto = htons(ETH_P_CAN);
296 struct net_device *eth_dev,
297 struct net_device *can_dev,
298 unsigned char *mac_dst,
299 unsigned char *mac_src) {
301 struct sk_buff *eth_skb;
302 eth_skb = netdev_alloc_skb(eth_dev,
sizeof(
struct ethhdr) +
303 sizeof(
struct can_frame));
304 if (eth_skb == NULL) {
306 pr_err(
"ce_gw: Error during ce_gw_can2net_alloc: %d\n", err);
310 skb_reserve(eth_skb,
sizeof(
struct ethhdr) +
sizeof(
struct can_frame));
312 eth_skb->pkt_type = PACKET_BROADCAST;
313 ce_gw_can2net(eth_skb, can_skb, eth_dev, can_dev, mac_dst, mac_src);
317 ce_gw_can2net_alloc_error:
339 struct net_device *can_dev) {
346 struct sk_buff *can_skb;
348 struct can_frame *canf;
349 can_skb = alloc_can_skb(can_dev, &canf);
352 pr_err(
"ce_gw: Allocation failed: %d\n", err);
353 goto ce_gw_net2can_alloc_error;
357 memcpy(canf, skb_network_header(eth_skb),
sizeof(
struct can_frame));
361 ce_gw_net2can_alloc_error:
388 struct net_device *eth_dev,
struct net_device *can_dev,
389 unsigned char *mac_dst,
unsigned char *mac_src)
391 # ifdef NET_SKBUFF_DATA_USES_OFFSET
397 skb_set_transport_header(eth_skb, 0);
400 struct canfd_frame *eth_canf;
401 eth_canf = (
struct canfd_frame *)skb_push(
402 eth_skb,
sizeof(
struct canfd_frame));
403 skb_set_network_header(eth_skb, 0);
405 struct canfd_frame *can_canf;
406 can_canf = (
struct canfd_frame *) can_skb->data;
408 memcpy(eth_canf, can_canf,
sizeof(
struct canfd_frame));
411 struct ethhdr *eth_ethh;
412 eth_ethh = (
struct ethhdr *)skb_push(eth_skb,
sizeof(
struct ethhdr));
413 skb_set_mac_header(eth_skb, 0);
415 memcpy(eth_ethh->h_dest, mac_dst, ETH_ALEN);
416 memcpy(eth_ethh->h_source, mac_src, ETH_ALEN);
417 eth_ethh->h_proto = htons(ETH_P_CANFD);
445 struct net_device *eth_dev,
446 struct net_device *can_dev,
447 unsigned char *mac_dst,
448 unsigned char *mac_src) {
450 struct sk_buff *eth_skb;
451 eth_skb = netdev_alloc_skb(eth_dev,
sizeof(
struct ethhdr) +
452 sizeof(
struct canfd_frame));
453 if (eth_skb == NULL) {
455 pr_err(
"ce_gw: Allocation failed: %d\n", err);
459 skb_reserve(eth_skb,
sizeof(
struct ethhdr) +
460 sizeof(
struct canfd_frame));
461 eth_skb->pkt_type = PACKET_BROADCAST;
463 ce_gw_can2net(eth_skb, can_skb, eth_dev, can_dev, mac_dst, mac_src);
466 ce_gw_can2net_alloc_error:
491 struct net_device *can_dev,
492 struct net_device *eth_dev) {
499 struct sk_buff *can_skb;
501 struct can_frame *canf;
502 can_skb = alloc_can_skb(can_dev, &canf);
503 if (can_skb == NULL) {
505 pr_err(
"ce_gw: Allocation failed: %d\n", err);
506 goto ce_gw_net2can_alloc_error;
514 skb_copy_expand(can_skb, 0,
sizeof(
struct canfd_frame) -
515 sizeof(
struct can_frame), GFP_ATOMIC);
516 skb_put(can_skb,
sizeof(
struct canfd_frame) -
sizeof(
struct can_frame));
517 can_skb->protocol = htons(ETH_P_CANFD);
519 struct canfd_frame *canfdf;
520 canfdf = (
struct canfd_frame *) canf;
523 memcpy(canfdf, skb_network_header(eth_skb),
sizeof(
struct canfd_frame));
527 ce_gw_net2can_alloc_error:
534 __be16 type,
struct sk_buff *can_buffer,
struct
536 struct can_frame *can_frame_skb = (
struct can_frame *)
538 struct ethhdr *ethhdr;
539 u8 canlen = can_dlc2len(can_frame_skb->can_dlc);
540 struct sk_buff *eth_skb = dev_alloc_skb(
sizeof(
struct ethhdr) +
sizeof
541 (
struct can_frame) + 64);
542 if (eth_skb == NULL) {
543 printk (KERN_ERR
"ce_gw_main.c: kmalloc failed in function"
544 "ce_gw_can_to_eth \n");
550 skb_reserve(eth_skb,
sizeof(
struct ethhdr) +
sizeof(
struct can_frame) +
553 eth_skb->data = skb_push(eth_skb, (
const int) canlen);
556 memcpy(eth_skb->data, can_buffer->data +
sizeof(
struct can_frame) -
559 skb_set_network_header(eth_skb, 0);
561 skb_set_transport_header(eth_skb, (
const int) canlen);
564 eth_skb->data = skb_push(eth_skb, (
const int)
sizeof(
struct ethhdr));
566 skb_set_mac_header(eth_skb, 0);
568 ethhdr = eth_hdr(eth_skb);
569 memcpy(ethhdr->h_dest, dest, ETH_ALEN);
570 memcpy(ethhdr->h_source, source, ETH_ALEN);
571 ethhdr->h_proto = type;
578 __be16 type,
struct sk_buff *canfd_skb,
579 struct net_device *dev) {
580 struct sk_buff *eth_skb = dev_alloc_skb(
sizeof(
struct ethhdr) +
581 sizeof(
struct canfd_frame) + 64);
582 if (eth_skb == NULL) {
583 printk (KERN_ERR
"ce_gw_main.c: kmalloc failed in function"
584 "ce_gw_canfd_to_eth \n");
587 struct ethhdr *ethhdr;
588 struct canfd_frame *canfd_frame_skb = (
struct canfd_frame *)
590 u8 canlen = can_dlc2len(canfd_frame_skb->len);
591 unsigned int iplen = ip_hdrlen(canfd_skb);
595 skb_reserve(eth_skb,
sizeof(
struct ethhdr) +
sizeof(
struct canfd_frame)
598 eth_skb->data = skb_push(eth_skb, (
const int) canlen);
601 memcpy(eth_skb->data, canfd_skb->data +
sizeof(
struct canfd_frame) -
602 sizeof(__u8)*64, canlen);
604 skb_set_network_header(eth_skb, 0);
606 if (canlen >= iplen) {
607 skb_set_transport_header(eth_skb, (
const int) iplen);
609 skb_set_transport_header(eth_skb, (
const int)
sizeof(__u8)*64);
612 eth_skb->data = skb_push(eth_skb, (
const int)
sizeof(
struct ethhdr));
614 skb_set_mac_header(eth_skb, 0);
616 ethhdr = eth_hdr(eth_skb);
617 memcpy(ethhdr->h_dest, &dest, ETH_ALEN);
618 memcpy(ethhdr->h_source, &source, ETH_ALEN);
619 ethhdr->h_proto = type;
627 struct sk_buff *can_buff;
630 printk (KERN_ERR
"ce_gw_main.c: kmalloc failed in function"
631 "ce_gw_eth_to_can \n");
634 unsigned int ethdatalen = (skb_tail_pointer(eth_buff) - (eth_buff->data
635 +
sizeof(
struct ethhdr)));
640 can->can_dlc = (__u8) eth_buff->data_len -
sizeof(
struct ethhdr);
642 can_buff = dev_alloc_skb(
sizeof(
struct can_frame) + ethdatalen + 64);
643 if (can_buff == NULL) {
644 printk (KERN_ERR
"ce_gw_main.c: kmalloc failed in function"
645 "ce_gw_eth_to_can \n");
651 skb_reserve(can_buff,
sizeof(
struct can_frame) + ethdatalen + 64);
653 can_buff->data = skb_push(can_buff, (
const int) ethdatalen);
656 memcpy(can_buff->data, eth_buff->data +
sizeof(
struct ethhdr),
659 skb_set_network_header(can_buff, 0);
661 skb_set_transport_header(can_buff, (
const int)
sizeof(u64));
664 can_buff->data = skb_push(can_buff, (
const int)
sizeof(__u32) +
667 skb_set_mac_header(can_buff, 0);
669 memcpy(skb_mac_header(can_buff), &can->can_id,
sizeof(__u32) +
679 res1,
struct sk_buff *eth_skb,
struct
681 struct sk_buff *canfd_skb;
684 printk (KERN_ERR
"ce_gw_main.c: kmalloc failed in function"
685 "ce_gw_eth_to_canfd \n");
690 unsigned int iplen = ip_hdrlen(eth_skb);
694 canfd->flags = flags;
695 canfd->__res0 = res0;
696 canfd->__res1 = res1;
697 canfd->len = (__u8) eth_skb->data_len -
sizeof(
struct ethhdr);
699 canfd_skb = dev_alloc_skb(
sizeof(
struct canfd_frame) + 64);
700 if (canfd_skb == NULL) {
701 printk (KERN_ERR
"ce_gw_main.c: kmalloc failed in function"
702 "ce_gw_eth_to_canfd \n");
705 canfd_skb->dev = dev;
708 skb_reserve(canfd_skb,
sizeof(
struct canfd_frame) + 64);
710 canfd_skb->data = skb_push(canfd_skb, (
const int)
sizeof(__u8)*64);
713 memcpy(canfd_skb->data, eth_skb->data +
sizeof(
struct ethhdr),
716 skb_set_network_header(canfd_skb, 0);
719 if (
sizeof(__u8)*64 >= iplen) {
720 skb_set_transport_header(canfd_skb, (
const int) iplen);
722 skb_set_transport_header(canfd_skb,
sizeof(__u8)*64);
725 canfd_skb->data = skb_push(canfd_skb, (
const int)
sizeof(
struct
726 canfd_frame) -
sizeof(__u8)*64);
728 skb_set_mac_header(canfd_skb, 0);
730 memcpy(skb_mac_header(canfd_skb), &canfd->can_id,
sizeof(
struct
731 canfd_frame) -
sizeof(__u8)*64);
748 __u8 version = *(__u8 *)payload & 0xF0;
749 if (version == 0x40) {
751 }
else if (version == 0x60) {
762 struct can_frame *cf;
764 cf = (
struct can_frame *)can_skb->data;
765 pr_debug(
"Incoming msg from can dev: can_id %x, len %i, can_msg %x\n",
766 cf->can_id, cf->can_dlc, cf->data[0]);
769 struct sk_buff *eth_skb = NULL;
771 long long dmac = 0xffffffffffff;
772 long long smac = 0x000000000000;
777 pr_info(
"Translation ETH not implemented yet.");
784 (
unsigned char *) &dmac,
785 (
unsigned char *) &smac);
789 pr_info(
"Translation IP TCP not implemented yet.");
793 pr_info(
"Translation IP UDP not implemented yet.");
797 pr_err(
"ce_gw: Translation type of ce_gw_job not "
798 "implemented. BUG: Some module inserted an invalid "
799 "type. Use enum ce_gw_type instead.");
804 err = netif_rx_ni(eth_skb);
806 pr_err(
"ce_gw: send to kernel failed");
818 pr_info(
"ce_gw: WARNING can skb will not be freed");
824 dev_kfree_skb(eth_skb);
836 struct sk_buff *can_skb = NULL;
841 pr_info(
"Translation ETH not implemented yet.");
849 pr_info(
"Translation IP TCP not implemented yet.");
853 pr_info(
"Translation IP UDP not implemented yet.");
857 printk(KERN_ERR
"ce_gw: Translation type of ce_gw_job not "
858 "implemented. BUG: Some module inserted an invalid "
859 "type. Use enum ce_gw_type instead.");
868 struct can_frame *cf;
869 cf = (
struct can_frame *)can_skb->data;
870 pr_debug(
"Incoming msg from eth dev (gwj %i): "
871 "can_id %x, len %i, can_msg(1) %x\n",
872 gwj->
id, cf->can_id, cf->can_dlc, cf->data[0]);
875 if (can_send(can_skb, 0x01))
884 dev_kfree_skb(can_skb);
922 gwj = kmem_cache_alloc(ce_gw_job_cache, GFP_KERNEL);
929 gwj->
src.
dev = dev_get_by_index(&init_net, src_ifindex);
930 gwj->
dst.
dev = dev_get_by_index(&init_net, dst_ifindex);
950 if (gwj->
src.
dev->type == ARPHRD_CAN &&
956 gwj->
dst.
dev->type == ARPHRD_CAN) {
965 hlist_add_head_rcu(&gwj->
list, &ce_gw_job_list);
969 printk(KERN_ERR
"ce_gw: Src or dst device not found or "
970 "not compatible (CAN<->CEGW ETH), exit.\n");
975 kmem_cache_free(ce_gw_job_cache, gwj);
984 pr_info(
"ce_gw: unregister CAN ETH GW routes\n");
986 struct hlist_node *n, *nx;
988 hlist_for_each_entry_safe(gwj, n, nx, &ce_gw_job_list, list) {
989 if (gwj->
id !=
id &&
id)
992 pr_debug(
"Removing routing src device: %s, id %x, mask %x\n",
995 hlist_del(&gwj->
list);
997 if (gwj->
src.
dev->type == ARPHRD_CAN)
1003 kmem_cache_free(ce_gw_job_cache, gwj);
1019 printk(KERN_INFO
"ce_gw: Module started.\n");
1021 ce_gw_job_cache = kmem_cache_create(
"can_eth_gw",
1023 if (!ce_gw_job_cache)
1050 printk(KERN_INFO
"ce_gw: Cleaning up the module\n");
1052 pr_debug(
"ce_gw: Unregister netlink server.\n");
1056 pr_info(
"ce_gw: unregister all CAN ETH GW routes\n");
1059 pr_debug(
"ce_gw: Unregister virtual net devices.\n");
1063 kmem_cache_destroy(ce_gw_job_cache);
1072 struct hlist_node *n, *nx;
1074 pr_info(
"Routing jobs\n"
1076 hlist_for_each_entry_safe(gwj, n, nx, &ce_gw_job_list, list) {
1077 pr_info(
"ID: %i, input dev: %s, output dev: %s\n",
1081 pr_info(
"No ce_gw routing jobs found!\n");
1089 struct sk_buff *skb;
1091 skb = netdev_alloc_skb(ethdev,
sizeof(
struct ethhdr) +
1092 sizeof(
struct can_frame));
1093 skb_reserve(skb,
sizeof(
struct ethhdr));
1102 struct net_device *dev;
1103 char vcandev[] =
"vcan1";
1105 pr_debug(
"cegw testing: Try finding vcan0 net_device.\n");
1107 if ((dev = __dev_get_by_name(&init_net, vcandev)) == NULL) {
1108 pr_debug(
"cegw testing: %s not found.\n", vcandev);
1112 pr_debug(
"cegw testing: %s found!\n", vcandev);
1113 pr_debug(
"type: %i, ifindex: %i\n", dev->type, dev->ifindex);
1122 struct ce_gw_job *gwj1 = kmem_cache_alloc(ce_gw_job_cache, GFP_KERNEL);
1124 struct ce_gw_job *gwj2 = kmem_cache_alloc(ce_gw_job_cache, GFP_KERNEL);
1131 hlist_add_head(&gwj1->
list, &ce_gw_job_list);
1132 hlist_add_head(&gwj2->
list, &ce_gw_job_list);
1133 hlist_add_head(&gwj3.
list, &ce_gw_job_list);
1136 struct hlist_node *n, *nx;
1138 hlist_for_each_entry(gwj, n, &ce_gw_job_list, list) {