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.

199 lines
6.9 KiB

5 years ago
  1. # snic - Switch network interface mode
  2. Toggle between client and router mode on network interface (Linux)
  3. Switch client network interface (NIC), such as eth0 or wlan0, from the default
  4. client mode to router mode. Basically, you can use the selected interface to act as a
  5. router for your other network devices.
  6. In client mode, the interface acts normally, as it does in your daily usage.
  7. In router mode, a DHCP server kicks in and allocated IPv4 addresses for any connected devices which use a DHCP client. This is equivalent to normal home router behavior.
  8. ----------------------
  9. ## Requirements
  10. - Linux OS
  11. - Software:
  12. - sudo
  13. - root permissions required to configure NIC settings
  14. - bash
  15. - the main script uses bash environment
  16. - systemd
  17. - the core functionality is implemented partly into systemd service files
  18. - iproute2
  19. - 'ip' command which is required for NIC configuration
  20. - dhcp (server)
  21. - DHCP server is required to allocate IPv4 addresses for connected clients
  22. - hostapd (for wireless router)
  23. - hostapd is required to establish a Wi-Fi hotspot (wireless interfaces)
  24. **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.
  25. ----------------------
  26. ## Files
  27. - snic.sh = main script. See section "Snic main script" below
  28. - snic/ = Snic configuration files. See section "Snic configuration files" below
  29. - systemd/ = Snic systemd service files. See section "systemd service files" below
  30. - conf-templates/ = Snic templates for iptables & hostapd. See section "Templates folder" below
  31. ----------------------
  32. ## Pre-configuration
  33. ### Find your network interfaces
  34. 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.
  35. ### Snic main script
  36. 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.
  37. Make sure the main script file is executable, i.e. run `chmod +x snic.sh`
  38. ### Snic configuration files
  39. 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.
  40. Configuration files of snic should be placed at `/etc/snic/` folder on a Linux system. The *required* files are:
  41. - `/etc/snic/dhcpd4-<nic>.conf`
  42. - e.g. `/etc/snic/dhcpd4-eth0.conf`
  43. - `/etc/snic/snic-<nic>.conf`
  44. - e.g. `/etc/snic/snic-eth0.conf`
  45. Please take a look into these files to find correct configuration for your Linux system.
  46. ### systemd service files
  47. Additionally, `systemd` service files are usually placed at `/usr/lib/systemd/system/` on a Linux system.
  48. **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.
  49. ### Templates folder
  50. This repository includes templates for `iptables.rules` and `hostapd` configuration.
  51. #### iptables.rules
  52. 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.
  53. #### hostapd.conf
  54. 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.
  55. ----------------------
  56. ## Usage
  57. The script syntax is as follows:
  58. - `snic <nic> <mode> <wireless (optional)>`
  59. <wireless> parameter is required only for wireless interfaces.
  60. For instance:
  61. - `snic eth0 router`
  62. - Set eth0 interface to router mode
  63. - Set a static IPv4 address for eth0, defined in `/etc/snic/snic-eth0.conf`
  64. - Start DHCP server on subnet where eth0 belongs to.
  65. - Disable NetworkManager for this interface because it interferes the connection
  66. - `snic eth0 client`
  67. - Set eth0 interface to client mode
  68. - 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`)
  69. - Enable NetworkManager for this interface, let it handle the connection
  70. - `snic wlan0 router wireless`
  71. - Set wlan0 interface to router mode.
  72. - Tell snic script that this is a wireless interface (starts hostapd service)
  73. - Set a static IPv4 address for eth0, defined in `/etc/snic/snic-wlan0.conf`
  74. - Start DHCP server on the subnet where wlan0 belongs to.
  75. - Disable NetworkManager for this interface because it interferes the connection
  76. - `snic wlan0 client wireless`
  77. - Set wlan0 interface to client mode
  78. - Tell snic script that this is a wireless interface (stops hostapd service)
  79. - 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`)
  80. - Enable NetworkManager for this interface, let it handle the connection
  81. ----------------------
  82. ## Issues
  83. For any connectivity issues, please check the output of following programs/commands:
  84. - program: Wireshark (check traffic of relevant network interfaces)
  85. - Any weird network traffic such as ARP broadcast requests flooding the whole subnet
  86. - command: `journalctl -xe`
  87. - Any weird log entries
  88. - command: `route`
  89. - Network route tables
  90. - command (router mode): `systemctl status snic-dhcpd4@<nic>`
  91. - Any weird DHCP server log entries
  92. - command (wireless router mode): `systemctl status hostapd`
  93. - Any weird hostapd log entries
  94. - `ip addr` and/or `ifconfig`
  95. - Network interface misconfigurations (missing IPv4 addresses or interfaces, etc.)
  96. And double check contents of all relevant snic configuration files described above in "Pre-configuration" section.
  97. ## Do not use if...
  98. - **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.
  99. - **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
  100. - **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.
  101. Just be careful when adapting snic settings, thank you. Debug your setup if needed.