Advertisement
kator

routing.c

Apr 4th, 2019
295
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.05 KB | None | 0 0
  1. /*
  2.  * Copyright (C) 2019 Michal Kalewski <mkalewski at cs.put.poznan.pl>
  3.  *
  4.  * Compilation:  gcc -Wall ./ethrecv.c -o ./ethrecv
  5.  * Usage:        ./ethrecv INTERFACE
  6.  * NOTE:         This program requires root privileges.
  7.  *
  8.  * Bug reports:  https://gitlab.cs.put.poznan.pl/mkalewski/ps-2019/issues
  9.  *
  10.  */
  11.  
  12. #include <arpa/inet.h>
  13. #include <linux/route.h>
  14. #include <linux/if.h>
  15. #include <linux/if_arp.h>
  16. #include <linux/if_ether.h>
  17. #include <linux/if_packet.h>
  18. #include <netinet/in.h>
  19. #include <stdio.h>
  20. #include <stdlib.h>
  21. #include <string.h>
  22. #include <sys/ioctl.h>
  23. #include <unistd.h>
  24.  
  25.  
  26. #define IRI_T_ADDRESS 0
  27. #define IRI_T_ROUTE   1
  28.  
  29. #define ETH_P_CUSTOM 0x8888
  30.  
  31. struct ifrtinfo {
  32.   int iri_type;
  33.   char iri_iname[16];
  34.   struct sockaddr_in iri_iaddr;
  35.   struct sockaddr_in iri_rtdst;
  36.   struct sockaddr_in iri_rtmsk;
  37.   struct sockaddr_in iri_rtgip;
  38. };
  39.  
  40. int main(int argc, char** argv) {
  41.   int sfd, i;
  42.   ssize_t len;
  43.   char* frame;
  44.   char* fdata;
  45.   struct ethhdr* fhead;
  46.   struct ifreq ifr;
  47.   struct sockaddr_ll sall;
  48.  
  49.   sfd = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_CUSTOM));
  50.   strncpy(ifr.ifr_name, argv[1], IFNAMSIZ);
  51.   ioctl(sfd, SIOCGIFINDEX, &ifr);
  52.   memset(&sall, 0, sizeof(struct sockaddr_ll));
  53.   sall.sll_family = AF_PACKET;
  54.   sall.sll_protocol = htons(ETH_P_CUSTOM);
  55.   sall.sll_ifindex = ifr.ifr_ifindex;
  56.   sall.sll_hatype = ARPHRD_ETHER;
  57.   sall.sll_pkttype = PACKET_HOST;
  58.   sall.sll_halen = ETH_ALEN;
  59.   bind(sfd, (struct sockaddr*) &sall, sizeof(struct sockaddr_ll));
  60.   while(1) {
  61.     frame = malloc(ETH_FRAME_LEN);
  62.     memset(frame, 0, ETH_FRAME_LEN);
  63.     fhead = (struct ethhdr*) frame;
  64.     fdata = frame + ETH_HLEN;
  65.     len = recvfrom(sfd, frame, ETH_FRAME_LEN, 0, NULL, NULL);
  66.  
  67.     struct ifrtinfo *r;
  68.     r = (struct ifrtinfo *) frame + ETH_HLEN;
  69.  
  70.     if (r->iri_type = IRI_T_ADDRESS){
  71.       printf("IRI_T_ADDRESS\n");
  72.       // ifsetup
  73.       int sfd1;
  74.       struct ifreq ifr1;
  75.       struct sockaddr_in* sin;
  76.  
  77.       sfd1 = socket(PF_INET, SOCK_DGRAM, 0);
  78.       strncpy(ifr1.ifr_name, r->iri_iname, strlen(r->iri_iname) + 1);
  79.       sin = (struct sockaddr_in*) &(r -> iri_iaddr);
  80.       // sin = (struct sockaddr_in*) &ifr1.ifr_addr;
  81.       memset(sin, 0, sizeof(struct sockaddr_in));
  82.       sin->sin_family = AF_INET;
  83.       sin->sin_port = 0;
  84.  
  85.  
  86.       sin->sin_addr.s_addr = inet_addr(r->iri_iname);
  87.       ioctl(sfd1, SIOCSIFADDR, &ifr1);
  88.       ioctl(sfd1, SIOCGIFFLAGS, &ifr1);
  89.       ifr1.ifr_flags |= IFF_UP | IFF_RUNNING;
  90.       ioctl(sfd1, SIOCSIFFLAGS, &ifr1);
  91.       close(sfd1);
  92.  
  93.     }else if (r->iri_type = IRI_T_ROUTE){
  94.       // setgw
  95.       printf("IRI_T_ADDRESS\n");
  96.       int sfd1;
  97.       struct rtentry route;
  98.       struct sockaddr_in* addr;
  99.  
  100.       sfd1 = socket(PF_INET, SOCK_DGRAM, 0);
  101.       memset(&route, 0, sizeof(route));
  102.       addr = (struct sockaddr_in*) &route.rt_gateway;
  103.       addr->sin_family = AF_INET;
  104.       // addr->sin_addr.s_addr = inet_addr(argv[1]);
  105.       addr->sin_addr.s_addr = &(r->iri_rtgip);
  106.  
  107.       addr = (struct sockaddr_in*) &route.rt_dst;
  108.       addr->sin_family = AF_INET;
  109.       addr->sin_addr.s_addr = &(r->iri_rtdst);// dac tu .sin_addr.sin_addr
  110.      
  111.       addr = (struct sockaddr_in*) &route.rt_genmask;
  112.       addr->sin_family = AF_INET;
  113.       addr->sin_addr.s_addr = &(r->iri_rtmsk);
  114.  
  115.       route.rt_flags = RTF_UP | RTF_GATEWAY;
  116.       route.rt_metric = 500;
  117.       ioctl(sfd1, SIOCADDRT, &route);
  118.       close(sfd1);
  119.     }
  120.  
  121.  
  122.     printf("[%dB] %02x:%02x:%02x:%02x:%02x:%02x -> ", (int)len,
  123.            fhead->h_source[0], fhead->h_source[1], fhead->h_source[2],
  124.            fhead->h_source[3], fhead->h_source[4], fhead->h_source[5]);
  125.     printf("%02x:%02x:%02x:%02x:%02x:%02x | ",
  126.            fhead->h_dest[0], fhead->h_dest[1], fhead->h_dest[2],
  127.            fhead->h_dest[3], fhead->h_dest[4], fhead->h_dest[5]);
  128.     printf("%s\n", fdata);
  129.     for (i = 0; i < len ; i++) {
  130.       printf("%02x ", (unsigned char) frame[i]);
  131.       if ((i + 1) % 16 == 0)
  132.         printf("\n");
  133.     }
  134.     printf("\n\n");
  135.     free(frame);
  136.   }
  137.   close(sfd);
  138.   return EXIT_SUCCESS;
  139. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement