Toggle between client and router mode on network interface (Linux)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

219 lines
8.6 KiB

6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
  1. # snic - Switch network interface mode
  2. Toggle between client and router mode on network interface (Linux)
  3. Switch network interface (NIC), such as `eth0` or `wlan0`, from default *client mode* to *router mode*. Basically, you can use the selected network interface to act as a router for your other network client devices such as mobile phones/tablets/etc... and switch it back to client mode quickly if needed.
  4. **In the client mode**, the network interface acts normally as it does on your normal client Linux computer. It retrieves a IPv4 from a DHCP server on your local network and acts as client, etc.
  5. **In the router mode**, a DHCP server on your computer kicks in and allocates IPv4 addresses for any connected client devices (which use a DHCP client, of course). This is similar behavior to normal stock/home router.
  6. - You can perform occasional [MITM attack](https://en.wikipedia.org/wiki/Man-in-the-middle_attack) scenarios without needing a fixed network setup for that
  7. - You can occasionally analyze all network traffic going through your network interface, and find out suspicious network traffic originating from a client device
  8. - You can quickly extend network area if needed
  9. - You can periodically use the same network interface for playing with Raspberry Pi/Arduino/... and switch quickly back to regular internet connection if needed
  10. - etc. ...
  11. ----------------------
  12. ## Requirements
  13. - Linux OS
  14. - Software:
  15. - `sudo`
  16. - root permissions required to configure NIC settings
  17. - `bash`
  18. - the main script uses bash environment
  19. - `systemd`
  20. - the core functionality is implemented partly into systemd service files
  21. - `iproute2`
  22. - `ip` command which is required for NIC configuration
  23. - `dhcp` (server)
  24. - DHCP server is required to allocate IPv4 addresses for connected clients
  25. - `hostapd` (for wireless router)
  26. - hostapd is required to establish a Wi-Fi hotspot (wireless interfaces)
  27. **NOTE:** Although this repository includes PKGBUILD + tar.xz files, this is not dependent on Arch Linux. You can use this repository on other Linux distributions as well. However, you should adapt the settings properly as configuration and file locations may differ.
  28. ----------------------
  29. ## Files
  30. - `snic.sh` = main script. See section "Snic main script" below
  31. - `snic/` = Snic configuration files. See section "Snic configuration files" below
  32. - `systemd/` = Snic systemd service files. See section "systemd service files" below
  33. - `conf-templates/` = Snic templates for iptables & hostapd. See section "Templates folder" below
  34. ## Arch Linux PKGBUILD
  35. - [Link here](https://github.com/Fincer/linux-patches-and-scripts/blob/master/snic/PKGBUILD)
  36. ----------------------
  37. ## Pre-configuration
  38. ### Find your network interfaces
  39. First you need to know which network interfaces you have in your system. You can check the output of `ip address` or `ifconfig` commands or simply take a look into `/sys/class/net` folder.
  40. ### Snic main script
  41. Snic main script is a bourne shell script (bash) `snic.sh`. It can be run individually, but for global purposes location such as `/usr/bin/` or `/usr/local/bin` is recommended. If you put the script in these locations (in your $PATH), it is recommended to rename the script from `snic.sh` to `snic` for convenience.
  42. Make sure the main script file is executable, i.e. run `chmod +x snic.sh`
  43. ### Snic configuration files
  44. This repository includes some sample configurations for wlan0 and eth0. Please be aware that these network interface names may not be the same you have in your system.
  45. Configuration files of snic should be placed at `/etc/snic/` folder on a Linux system. The *required* files are:
  46. - `/etc/snic/dhcpd4-<nic>.conf`
  47. - e.g. `/etc/snic/dhcpd4-eth0.conf`
  48. - `/etc/snic/snic-<nic>.conf`
  49. - e.g. `/etc/snic/snic-eth0.conf`
  50. Please take a look into these files to find correct configuration for your Linux system.
  51. ### systemd service files
  52. Additionally, `systemd` service files are usually placed at `/usr/lib/systemd/system/` on a Linux system.
  53. **NOTE:** It is not recommended to `enable` (read: autostart) systemd service files provided by snic! These service files are used by the snic main script for its internal operations.
  54. ### Templates folder
  55. This repository includes templates for `iptables.rules` and `hostapd` configuration.
  56. #### iptables.rules
  57. This is a sample `iptables` ruleset file. Change contents as you want. This file includes basic configuration to route traffic from network interface to another one.
  58. **NOTE:** In order to apply traffic between a local client and internet, you must have `iptables` running. In order to check that, run `systemctl is-active iptables`. If the answer is `inactive`, your iptables firewall service is not running.
  59. Before issuing any of the following commands (~before starting iptables), make sure you have your iptables configuration file `/etc/iptables/iptables.rules` set up correctly on your system.
  60. Once the configuration file is OK, you should check whether iptables is started during computer boot process or not: run `systemctl is-enabled iptables`
  61. And finally, to ensure iptables is automatically started during computer boot and, well, started right now: run `sudo systemctl restart iptables && sudo systemctl enable iptables`.
  62. #### hostapd.conf
  63. This is a sample `hostapd` configuration file. Change contents as you want. Usually this file is located at `etc/hostapd/hostapd.conf` on Linux system. On some Linux systems, different locations are used.
  64. ----------------------
  65. ## Usage
  66. The script syntax is as follows:
  67. - `snic <nic> <mode> <wireless (optional)>`
  68. `wireless` parameter is required only for wireless interfaces.
  69. For instance:
  70. - `snic eth0 router`
  71. - Set eth0 interface to router mode
  72. - Set a static IPv4 address for eth0, defined in `/etc/snic/snic-eth0.conf`
  73. - Start DHCP server on subnet where eth0 belongs to.
  74. - Disable NetworkManager for this interface because it interferes the connection
  75. - `snic eth0 client`
  76. - Set eth0 interface to client mode
  77. - Do not set any IPv4 address, let a DHCP server in our network to give one (requires DHCP client daemon on the computer for which eth0 belongs to. Common clients on Linux are `dhcpcd` and `dhclient`)
  78. - Enable NetworkManager for this interface, let it handle the connection
  79. - `snic wlan0 router wireless`
  80. - Set wlan0 interface to router mode.
  81. - Tell snic script that this is a wireless interface (starts hostapd service)
  82. - Set a static IPv4 address for wlan0, defined in `/etc/snic/snic-wlan0.conf`
  83. - Start DHCP server on the subnet where wlan0 belongs to.
  84. - Disable NetworkManager for this interface because it interferes the connection
  85. - `snic wlan0 client wireless`
  86. - Set wlan0 interface to client mode
  87. - Tell snic script that this is a wireless interface (stops hostapd service)
  88. - Do not set any IPv4 address, let a DHCP server in our network to give one (requires DHCP client daemon on the computer for which eth0 belongs to. Common clients on Linux are `dhcpcd` and `dhclient`)
  89. - Enable NetworkManager for this interface, let it handle the connection
  90. ----------------------
  91. ## Issues
  92. For any connectivity issues, please check the output of following programs/commands:
  93. - program: Wireshark (check traffic of relevant network interfaces)
  94. - Any weird network traffic such as ARP broadcast requests flooding the whole subnet
  95. - command: `journalctl -xe`
  96. - Any weird log entries
  97. - command: `route`
  98. - Network route tables
  99. - command (router mode): `systemctl status snic-dhcpd4@<nic>`
  100. - Any weird DHCP server log entries
  101. - command (wireless router mode): `systemctl status hostapd`
  102. - Any weird hostapd log entries
  103. - `ip addr` and/or `ifconfig`
  104. - Network interface misconfigurations (missing IPv4 addresses or interfaces, etc.)
  105. And double check contents of all relevant snic configuration files described above in "Pre-configuration" section.
  106. ## Do not use if...
  107. - **Snic? WTF is it? Useless crap!** Basically, you don't know what the hell snic does, you don't care to find out or you don't find it useful in your setup.
  108. - **Predefined network configuration** Do not use snic for absolutely static Linux network interfaces, in environments where client/router mode switching for a single computer is not required/allowed/recommended
  109. - **DHCP settings are in danger** Be aware that this script may alter your current DHCP server setup. However, your current *DHCP server setup configuration files are **NOT** overwritten (or even touched)* but snic configuration may cause conflicts in your setup, anyway.
  110. Just be careful when adapting snic settings, thank you. Debug your setup if needed.