Guest User

Untitled

a guest
Feb 17th, 2019
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.31 KB | None | 0 0
  1. sudo mknod /dev/net/tap c 10 200
  2.  
  3. /*
  4. * Taken from Kernel Documentation/networking/tuntap.txt
  5. */
  6.  
  7. int tun_alloc(char *dev)
  8. {
  9. struct ifreq ifr;
  10. int fd, err;
  11.  
  12. /* Arguments taken by the function:
  13. *
  14. * char *dev: the name of an interface (or ''). MUST have enough
  15. * space to hold the interface name if '' is passed
  16. * int flags: interface flags (eg, IFF_TUN etc.)
  17. */
  18.  
  19. /* open the clone device */
  20. if ((fd = open("/dev/net/tap", O_RDWR)) < 0)
  21. {
  22. printf("Cannot open TUN/TAP devn");
  23. exit(1);
  24. }
  25. /* preparation of the struct ifr, of type "struct ifreq" */
  26. memset(&ifr, 0, sizeof(struct ifreq));
  27.  
  28. /* Flags: IFF_TUN - TUN device (no Ethernet headers)
  29. * IFF_TAP - TAP device
  30. *
  31. * IFF_NO_PI - Do not provide packet information
  32. */
  33. ifr.ifr_flags = IFF_TAP | IFF_NO_PI;
  34. if (*dev)
  35. {
  36. /* if a device name was specified, put it in the structure; otherwise,
  37. * the kernel will try to allocate the "next" device of the
  38. * specified type */
  39. strncpy(ifr.ifr_name, dev, IFNAMSIZ);
  40. }
  41.  
  42. /* try to create the device */
  43. if ((err = ioctl(fd, TUNSETIFF, (void *)&ifr)) < 0)
  44. {
  45. printf("ERR: Could not ioctl tun: %sn", strerror(errno));
  46. close(fd);
  47. return err;
  48. }
  49.  
  50. /* if the operation was successful, write back the name of the
  51. * interface to the variable "dev", so the caller can know
  52. * it. Note that the caller MUST reserve space in *dev (see calling
  53. * code below) */
  54. strcpy(dev, ifr.ifr_name);
  55.  
  56. /* this is the special file descriptor that the caller will use to talk
  57. * with the virtual interface */
  58. return fd;
  59. }
  60.  
  61. ip link set dev tap0 up
  62. ip route add dev tap0 192.168.1.0/24
  63. ip address add dev tap0 local 192.168.1.21
  64.  
  65. enp0s3 Link encap:Ethernet HWaddr 08:00:27:2f:ba:f8
  66. inet addr:192.168.1.36 Bcast:192.168.1.255 Mask:255.255.255.0
  67. inet6 addr: fe80::445b:f137:4390:8b40/64 Scope:Link
  68. UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
  69. RX packets:8 errors:0 dropped:0 overruns:0 frame:0
  70. TX packets:149 errors:0 dropped:0 overruns:0 carrier:0
  71. collisions:0 txqueuelen:1000
  72. RX bytes:1239 (1.2 KB) TX bytes:24836 (24.8 KB)
  73.  
  74. lo Link encap:Local Loopback
  75. inet addr:127.0.0.1 Mask:255.0.0.0
  76. inet6 addr: ::1/128 Scope:Host
  77. UP LOOPBACK RUNNING MTU:65536 Metric:1
  78. RX packets:68 errors:0 dropped:0 overruns:0 frame:0
  79. TX packets:68 errors:0 dropped:0 overruns:0 carrier:0
  80. collisions:0 txqueuelen:1000
  81. RX bytes:5351 (5.3 KB) TX bytes:5351 (5.3 KB)
  82.  
  83. tap0 Link encap:Ethernet HWaddr e2:c8:48:94:af:9c
  84. inet addr:192.168.1.21 Bcast:0.0.0.0 Mask:255.255.255.255
  85. inet6 addr: fe80::e0c8:48ff:fe94:af9c/64 Scope:Link
  86. UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
  87. RX packets:1 errors:0 dropped:0 overruns:0 frame:0
  88. TX packets:51 errors:0 dropped:0 overruns:0 carrier:0
  89. collisions:0 txqueuelen:1000
  90. RX bytes:42 (42.0 B) TX bytes:6078 (6.0 KB)
  91.  
  92. arping -I tap0 192.168.1.21
  93. ARPING 192.168.1.21 from 192.168.1.21 tap0
  94. Unicast reply from 192.168.1.21 [00:0C:29:6D:50:25] 0.545ms
  95. Unicast reply from 192.168.1.21 [00:0C:29:6D:50:25] 0.733ms
Add Comment
Please, Sign In to add comment