├── .dockerignore ├── .gitignore ├── .gitlab-ci.yml ├── Dockerfile ├── LICENSE ├── Makefile ├── README.md ├── build.sh ├── docs ├── ipv6.md ├── monitoring.md ├── rpi.md ├── upgrade.md └── vpn.md ├── etc ├── config │ ├── dhcp.tpl │ ├── network.tpl │ ├── system.tpl │ └── wireless.tpl ├── intercept-dns.sh └── list-user-installed.sh ├── monitoring ├── .env.example ├── README.md ├── docker-compose.yaml ├── grafana │ ├── dashboard-openwrt.json │ └── provisioning │ │ ├── dashboards │ │ └── openwrt.yaml │ │ └── datasources │ │ └── influxdb.yaml └── influxdb │ ├── influxdb.conf │ └── types.db ├── openwrt.conf.example ├── openwrt.service ├── patches ├── dhcpv6.script.patch └── gre.sh.patch └── run.sh /.dockerignore: -------------------------------------------------------------------------------- 1 | openwrt.conf 2 | .env 3 | *.img.gz 4 | *.img 5 | etc/config/* 6 | !etc/config/*.tpl -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .vscode 2 | openwrt.conf 3 | .env 4 | *.tar.gz 5 | *.img 6 | *.img.gz 7 | etc/config/* 8 | !etc/config/*.tpl -------------------------------------------------------------------------------- /.gitlab-ci.yml: -------------------------------------------------------------------------------- 1 | variables: 2 | CI_IMAGE: $DOCKER_HUB_USER/openwrt 3 | RELEASE: "19.07.7" 4 | 5 | .build: 6 | image: docker:latest 7 | stage: build 8 | services: 9 | - docker:dind 10 | before_script: 11 | - docker login -u $DOCKER_HUB_USER -p $DOCKER_HUB_TOKEN 12 | - docker run --rm --privileged multiarch/qemu-user-static --reset -p yes 13 | script: 14 | - export OPENWRT_SOURCE_VER=$RELEASE 15 | - export ARCH=$ARCH 16 | - export IMAGE=$CI_IMAGE 17 | - export TAG=${RELEASE}-${ARCH} 18 | - ./build.sh 19 | - docker push $CI_IMAGE:${RELEASE}-${ARCH} 20 | only: 21 | - master 22 | 23 | .build-squashfs: 24 | extends: .build 25 | before_script: 26 | - docker login -u $DOCKER_HUB_USER -p $DOCKER_HUB_TOKEN 27 | - docker run --rm --privileged multiarch/qemu-user-static --reset -p yes 28 | - apk add util-linux fakeroot squashfs-tools 29 | 30 | build-release-x86-64: 31 | extends: .build 32 | variables: 33 | ARCH: x86-64 34 | 35 | build-release-armvirt-64: 36 | extends: .build 37 | variables: 38 | ARCH: armvirt-64 39 | 40 | build-release-armvirt-32: 41 | extends: .build 42 | variables: 43 | ARCH: armvirt-32 44 | 45 | build-release-bcm2708: 46 | extends: .build-squashfs 47 | variables: 48 | ARCH: bcm2708 49 | 50 | build-snapshot-x86-64: 51 | extends: .build 52 | variables: 53 | ARCH: x86-64 54 | RELEASE: snapshot 55 | 56 | build-snapshot-armvirt-64: 57 | extends: .build 58 | variables: 59 | ARCH: armvirt-64 60 | RELEASE: snapshot 61 | 62 | build-snapshot-armvirt-32: 63 | extends: .build 64 | variables: 65 | ARCH: armvirt-32 66 | RELEASE: snapshot 67 | 68 | build-snapshot-bcm2708: 69 | extends: .build-squashfs 70 | variables: 71 | ARCH: bcm2708 72 | RELEASE: snapshot 73 | -------------------------------------------------------------------------------- /Dockerfile: -------------------------------------------------------------------------------- 1 | FROM scratch 2 | ADD rootfs.tar.gz / 3 | RUN mkdir -p /var/lock 4 | RUN opkg remove --force-depends \ 5 | dnsmasq* \ 6 | wpad* \ 7 | iw* && \ 8 | opkg update && \ 9 | opkg install luci \ 10 | wpad-wolfssl \ 11 | iw-full \ 12 | ip-full \ 13 | kmod-mac80211 \ 14 | dnsmasq-full \ 15 | iptables-mod-checksum 16 | RUN opkg list-upgradable | awk '{print $1}' | xargs opkg upgrade || true 17 | 18 | RUN echo "iptables -A POSTROUTING -t mangle -p udp --dport 68 -j CHECKSUM --checksum-fill" >> /etc/firewall.user 19 | RUN sed -i '/^exit 0/i cat \/tmp\/resolv.conf > \/etc\/resolv.conf' /etc/rc.local 20 | 21 | ARG ts 22 | ARG version 23 | LABEL org.opencontainers.image.created=$ts 24 | LABEL org.opencontainers.image.version=$version 25 | LABEL org.opencontainers.image.source=https://github.com/oofnikj/docker-openwrt 26 | 27 | CMD [ "/sbin/init" ] -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 2, June 1991 3 | 4 | Copyright (C) 1989, 1991 Free Software Foundation, Inc., 5 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 6 | Everyone is permitted to copy and distribute verbatim copies 7 | of this license document, but changing it is not allowed. 8 | 9 | Preamble 10 | 11 | The licenses for most software are designed to take away your 12 | freedom to share and change it. By contrast, the GNU General Public 13 | License is intended to guarantee your freedom to share and change free 14 | software--to make sure the software is free for all its users. This 15 | General Public License applies to most of the Free Software 16 | Foundation's software and to any other program whose authors commit to 17 | using it. (Some other Free Software Foundation software is covered by 18 | the GNU Lesser General Public License instead.) You can apply it to 19 | your programs, too. 20 | 21 | When we speak of free software, we are referring to freedom, not 22 | price. Our General Public Licenses are designed to make sure that you 23 | have the freedom to distribute copies of free software (and charge for 24 | this service if you wish), that you receive source code or can get it 25 | if you want it, that you can change the software or use pieces of it 26 | in new free programs; and that you know you can do these things. 27 | 28 | To protect your rights, we need to make restrictions that forbid 29 | anyone to deny you these rights or to ask you to surrender the rights. 30 | These restrictions translate to certain responsibilities for you if you 31 | distribute copies of the software, or if you modify it. 32 | 33 | For example, if you distribute copies of such a program, whether 34 | gratis or for a fee, you must give the recipients all the rights that 35 | you have. You must make sure that they, too, receive or can get the 36 | source code. And you must show them these terms so they know their 37 | rights. 38 | 39 | We protect your rights with two steps: (1) copyright the software, and 40 | (2) offer you this license which gives you legal permission to copy, 41 | distribute and/or modify the software. 42 | 43 | Also, for each author's protection and ours, we want to make certain 44 | that everyone understands that there is no warranty for this free 45 | software. If the software is modified by someone else and passed on, we 46 | want its recipients to know that what they have is not the original, so 47 | that any problems introduced by others will not reflect on the original 48 | authors' reputations. 49 | 50 | Finally, any free program is threatened constantly by software 51 | patents. We wish to avoid the danger that redistributors of a free 52 | program will individually obtain patent licenses, in effect making the 53 | program proprietary. To prevent this, we have made it clear that any 54 | patent must be licensed for everyone's free use or not licensed at all. 55 | 56 | The precise terms and conditions for copying, distribution and 57 | modification follow. 58 | 59 | GNU GENERAL PUBLIC LICENSE 60 | TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 61 | 62 | 0. This License applies to any program or other work which contains 63 | a notice placed by the copyright holder saying it may be distributed 64 | under the terms of this General Public License. The "Program", below, 65 | refers to any such program or work, and a "work based on the Program" 66 | means either the Program or any derivative work under copyright law: 67 | that is to say, a work containing the Program or a portion of it, 68 | either verbatim or with modifications and/or translated into another 69 | language. (Hereinafter, translation is included without limitation in 70 | the term "modification".) Each licensee is addressed as "you". 71 | 72 | Activities other than copying, distribution and modification are not 73 | covered by this License; they are outside its scope. The act of 74 | running the Program is not restricted, and the output from the Program 75 | is covered only if its contents constitute a work based on the 76 | Program (independent of having been made by running the Program). 77 | Whether that is true depends on what the Program does. 78 | 79 | 1. You may copy and distribute verbatim copies of the Program's 80 | source code as you receive it, in any medium, provided that you 81 | conspicuously and appropriately publish on each copy an appropriate 82 | copyright notice and disclaimer of warranty; keep intact all the 83 | notices that refer to this License and to the absence of any warranty; 84 | and give any other recipients of the Program a copy of this License 85 | along with the Program. 86 | 87 | You may charge a fee for the physical act of transferring a copy, and 88 | you may at your option offer warranty protection in exchange for a fee. 89 | 90 | 2. You may modify your copy or copies of the Program or any portion 91 | of it, thus forming a work based on the Program, and copy and 92 | distribute such modifications or work under the terms of Section 1 93 | above, provided that you also meet all of these conditions: 94 | 95 | a) You must cause the modified files to carry prominent notices 96 | stating that you changed the files and the date of any change. 97 | 98 | b) You must cause any work that you distribute or publish, that in 99 | whole or in part contains or is derived from the Program or any 100 | part thereof, to be licensed as a whole at no charge to all third 101 | parties under the terms of this License. 102 | 103 | c) If the modified program normally reads commands interactively 104 | when run, you must cause it, when started running for such 105 | interactive use in the most ordinary way, to print or display an 106 | announcement including an appropriate copyright notice and a 107 | notice that there is no warranty (or else, saying that you provide 108 | a warranty) and that users may redistribute the program under 109 | these conditions, and telling the user how to view a copy of this 110 | License. (Exception: if the Program itself is interactive but 111 | does not normally print such an announcement, your work based on 112 | the Program is not required to print an announcement.) 113 | 114 | These requirements apply to the modified work as a whole. If 115 | identifiable sections of that work are not derived from the Program, 116 | and can be reasonably considered independent and separate works in 117 | themselves, then this License, and its terms, do not apply to those 118 | sections when you distribute them as separate works. But when you 119 | distribute the same sections as part of a whole which is a work based 120 | on the Program, the distribution of the whole must be on the terms of 121 | this License, whose permissions for other licensees extend to the 122 | entire whole, and thus to each and every part regardless of who wrote it. 123 | 124 | Thus, it is not the intent of this section to claim rights or contest 125 | your rights to work written entirely by you; rather, the intent is to 126 | exercise the right to control the distribution of derivative or 127 | collective works based on the Program. 128 | 129 | In addition, mere aggregation of another work not based on the Program 130 | with the Program (or with a work based on the Program) on a volume of 131 | a storage or distribution medium does not bring the other work under 132 | the scope of this License. 133 | 134 | 3. You may copy and distribute the Program (or a work based on it, 135 | under Section 2) in object code or executable form under the terms of 136 | Sections 1 and 2 above provided that you also do one of the following: 137 | 138 | a) Accompany it with the complete corresponding machine-readable 139 | source code, which must be distributed under the terms of Sections 140 | 1 and 2 above on a medium customarily used for software interchange; or, 141 | 142 | b) Accompany it with a written offer, valid for at least three 143 | years, to give any third party, for a charge no more than your 144 | cost of physically performing source distribution, a complete 145 | machine-readable copy of the corresponding source code, to be 146 | distributed under the terms of Sections 1 and 2 above on a medium 147 | customarily used for software interchange; or, 148 | 149 | c) Accompany it with the information you received as to the offer 150 | to distribute corresponding source code. (This alternative is 151 | allowed only for noncommercial distribution and only if you 152 | received the program in object code or executable form with such 153 | an offer, in accord with Subsection b above.) 154 | 155 | The source code for a work means the preferred form of the work for 156 | making modifications to it. For an executable work, complete source 157 | code means all the source code for all modules it contains, plus any 158 | associated interface definition files, plus the scripts used to 159 | control compilation and installation of the executable. However, as a 160 | special exception, the source code distributed need not include 161 | anything that is normally distributed (in either source or binary 162 | form) with the major components (compiler, kernel, and so on) of the 163 | operating system on which the executable runs, unless that component 164 | itself accompanies the executable. 165 | 166 | If distribution of executable or object code is made by offering 167 | access to copy from a designated place, then offering equivalent 168 | access to copy the source code from the same place counts as 169 | distribution of the source code, even though third parties are not 170 | compelled to copy the source along with the object code. 171 | 172 | 4. You may not copy, modify, sublicense, or distribute the Program 173 | except as expressly provided under this License. Any attempt 174 | otherwise to copy, modify, sublicense or distribute the Program is 175 | void, and will automatically terminate your rights under this License. 176 | However, parties who have received copies, or rights, from you under 177 | this License will not have their licenses terminated so long as such 178 | parties remain in full compliance. 179 | 180 | 5. You are not required to accept this License, since you have not 181 | signed it. However, nothing else grants you permission to modify or 182 | distribute the Program or its derivative works. These actions are 183 | prohibited by law if you do not accept this License. Therefore, by 184 | modifying or distributing the Program (or any work based on the 185 | Program), you indicate your acceptance of this License to do so, and 186 | all its terms and conditions for copying, distributing or modifying 187 | the Program or works based on it. 188 | 189 | 6. Each time you redistribute the Program (or any work based on the 190 | Program), the recipient automatically receives a license from the 191 | original licensor to copy, distribute or modify the Program subject to 192 | these terms and conditions. You may not impose any further 193 | restrictions on the recipients' exercise of the rights granted herein. 194 | You are not responsible for enforcing compliance by third parties to 195 | this License. 196 | 197 | 7. If, as a consequence of a court judgment or allegation of patent 198 | infringement or for any other reason (not limited to patent issues), 199 | conditions are imposed on you (whether by court order, agreement or 200 | otherwise) that contradict the conditions of this License, they do not 201 | excuse you from the conditions of this License. If you cannot 202 | distribute so as to satisfy simultaneously your obligations under this 203 | License and any other pertinent obligations, then as a consequence you 204 | may not distribute the Program at all. For example, if a patent 205 | license would not permit royalty-free redistribution of the Program by 206 | all those who receive copies directly or indirectly through you, then 207 | the only way you could satisfy both it and this License would be to 208 | refrain entirely from distribution of the Program. 209 | 210 | If any portion of this section is held invalid or unenforceable under 211 | any particular circumstance, the balance of the section is intended to 212 | apply and the section as a whole is intended to apply in other 213 | circumstances. 214 | 215 | It is not the purpose of this section to induce you to infringe any 216 | patents or other property right claims or to contest validity of any 217 | such claims; this section has the sole purpose of protecting the 218 | integrity of the free software distribution system, which is 219 | implemented by public license practices. Many people have made 220 | generous contributions to the wide range of software distributed 221 | through that system in reliance on consistent application of that 222 | system; it is up to the author/donor to decide if he or she is willing 223 | to distribute software through any other system and a licensee cannot 224 | impose that choice. 225 | 226 | This section is intended to make thoroughly clear what is believed to 227 | be a consequence of the rest of this License. 228 | 229 | 8. If the distribution and/or use of the Program is restricted in 230 | certain countries either by patents or by copyrighted interfaces, the 231 | original copyright holder who places the Program under this License 232 | may add an explicit geographical distribution limitation excluding 233 | those countries, so that distribution is permitted only in or among 234 | countries not thus excluded. In such case, this License incorporates 235 | the limitation as if written in the body of this License. 236 | 237 | 9. The Free Software Foundation may publish revised and/or new versions 238 | of the General Public License from time to time. Such new versions will 239 | be similar in spirit to the present version, but may differ in detail to 240 | address new problems or concerns. 241 | 242 | Each version is given a distinguishing version number. If the Program 243 | specifies a version number of this License which applies to it and "any 244 | later version", you have the option of following the terms and conditions 245 | either of that version or of any later version published by the Free 246 | Software Foundation. If the Program does not specify a version number of 247 | this License, you may choose any version ever published by the Free Software 248 | Foundation. 249 | 250 | 10. If you wish to incorporate parts of the Program into other free 251 | programs whose distribution conditions are different, write to the author 252 | to ask for permission. For software which is copyrighted by the Free 253 | Software Foundation, write to the Free Software Foundation; we sometimes 254 | make exceptions for this. Our decision will be guided by the two goals 255 | of preserving the free status of all derivatives of our free software and 256 | of promoting the sharing and reuse of software generally. 257 | 258 | NO WARRANTY 259 | 260 | 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY 261 | FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN 262 | OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES 263 | PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED 264 | OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 265 | MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS 266 | TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE 267 | PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, 268 | REPAIR OR CORRECTION. 269 | 270 | 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 271 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR 272 | REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, 273 | INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING 274 | OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED 275 | TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY 276 | YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER 277 | PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE 278 | POSSIBILITY OF SUCH DAMAGES. 279 | 280 | END OF TERMS AND CONDITIONS 281 | 282 | How to Apply These Terms to Your New Programs 283 | 284 | If you develop a new program, and you want it to be of the greatest 285 | possible use to the public, the best way to achieve this is to make it 286 | free software which everyone can redistribute and change under these terms. 287 | 288 | To do so, attach the following notices to the program. It is safest 289 | to attach them to the start of each source file to most effectively 290 | convey the exclusion of warranty; and each file should have at least 291 | the "copyright" line and a pointer to where the full notice is found. 292 | 293 | 294 | Copyright (C) 295 | 296 | This program is free software; you can redistribute it and/or modify 297 | it under the terms of the GNU General Public License as published by 298 | the Free Software Foundation; either version 2 of the License, or 299 | (at your option) any later version. 300 | 301 | This program is distributed in the hope that it will be useful, 302 | but WITHOUT ANY WARRANTY; without even the implied warranty of 303 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 304 | GNU General Public License for more details. 305 | 306 | You should have received a copy of the GNU General Public License along 307 | with this program; if not, write to the Free Software Foundation, Inc., 308 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 309 | 310 | Also add information on how to contact you by electronic and paper mail. 311 | 312 | If the program is interactive, make it output a short notice like this 313 | when it starts in an interactive mode: 314 | 315 | Gnomovision version 69, Copyright (C) year name of author 316 | Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 317 | This is free software, and you are welcome to redistribute it 318 | under certain conditions; type `show c' for details. 319 | 320 | The hypothetical commands `show w' and `show c' should show the appropriate 321 | parts of the General Public License. Of course, the commands you use may 322 | be called something other than `show w' and `show c'; they could even be 323 | mouse-clicks or menu items--whatever suits your program. 324 | 325 | You should also get your employer (if you work as a programmer) or your 326 | school, if any, to sign a "copyright disclaimer" for the program, if 327 | necessary. Here is a sample; alter the names: 328 | 329 | Yoyodyne, Inc., hereby disclaims all copyright interest in the program 330 | `Gnomovision' (which makes passes at compilers) written by James Hacker. 331 | 332 | , 1 April 1989 333 | Ty Coon, President of Vice 334 | 335 | This General Public License does not permit incorporating your program into 336 | proprietary programs. If your program is a subroutine library, you may 337 | consider it more useful to permit linking proprietary applications with the 338 | library. If this is what you want to do, use the GNU Lesser General 339 | Public License instead of this License. 340 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | .PHONY: build run clean install uninstall 2 | 3 | include openwrt.conf 4 | export 5 | 6 | build: 7 | ./build.sh 8 | 9 | run: 10 | ./run.sh 11 | 12 | clean: 13 | docker stop ${CONTAINER} || true 14 | docker rm ${CONTAINER} || true 15 | docker network rm ${LAN_NAME} ${WAN_NAME} || true 16 | 17 | install: 18 | install -Dm644 openwrt.service /usr/lib/systemd/system/openwrt.service 19 | sed -i -E "s#(ExecStart=).*#\1`pwd`/run.sh#g" /usr/lib/systemd/system/openwrt.service 20 | systemctl daemon-reload 21 | systemctl enable openwrt.service 22 | @echo "OpenWrt service installed and will be started on next boot automatically." 23 | @echo "To start it now, run 'systemctl start openwrt.service'." 24 | 25 | uninstall: 26 | systemctl stop openwrt.service 27 | systemctl disable openwrt.service 28 | rm /usr/lib/systemd/system/openwrt.service 29 | systemctl daemon-reload -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # OpenWrt in Docker 2 | 3 | [![pipeline status](https://gitlab.com/oofnik/docker-openwrt/badges/master/pipeline.svg)](https://gitlab.com/oofnik/docker-openwrt/-/commits/master) 4 | 5 | Inspired by other projects that run `hostapd` in a Docker container. This goes one step further and boots a full network OS intended for embedded devices called [OpenWrt](https://openwrt.org/), so you can manage all aspects of your network from a user-friendly web UI. 6 | 7 | For Raspberry Pi-specific instructions, see [Building on Raspberry Pi](docs/rpi.md). 8 | 9 | 10 | ## Dependencies 11 | 12 | * docker 13 | * iw 14 | * iproute2 15 | * envsubst (part of `gettext` or `gettext-base` package) 16 | * dhcpcd 17 | 18 | ## Build 19 | Pre-built images are available on Docker Hub at `oofnik/openwrt`. Alternatively, build the image yourself using the `make build` target: 20 | ``` 21 | $ make build 22 | ``` 23 | If you want additional OpenWrt packages to be present in the base image, add them to the Dockerfile. Otherwise you can install them with `opkg` after bringing up the container. 24 | 25 | A searchable package list is available on [openwrt.org](https://openwrt.org/packages/table/start). 26 | 27 | ## Configure 28 | 29 | Initial configuration is performed using a config file, `openwrt.conf`. Values read from this file at runtime are used to generate OpenWrt format config files from templates in `etc/config/*.tpl`. 30 | 31 | You can use the included `openwrt.conf.example` as a baseline, which explains the values. 32 | 33 | It is also possible to make persistent changes in the UI and download a backup of your full router configuration by navigating to System > Backup / Flash Firmware and clicking Backup. 34 | 35 | ## Run 36 | 37 | Prepare your `openwrt.conf` file as explained above and execute the `make run` target: 38 | ``` 39 | $ make run 40 | ``` 41 | 42 | If you arrive at `* Ready`, point your browser to http://openwrt.home (or whatever you set in `LAN_DOMAIN`) and you should be presented with the login page. The default login is `root` with the password set as `ROOT_PW`. 43 | 44 | To shut down the router, press `Ctrl+C`. Any settings you configured or additional packages you installed will persist until you run `make clean`, which will delete the container. 45 | 46 | ## Install / Uninstall 47 | ``` 48 | $ make install 49 | ``` 50 | Install and uninstall targets for `systemd` have been included in the Makefile. 51 | 52 | Installing will create and enable a service pointing to wherever you cloned this directory and execute `run.sh` on boot. 53 | 54 | ## Cleanup 55 | ``` 56 | $ make clean 57 | ``` 58 | This will delete the container and all associated Docker networks so you can start fresh if you screw something up. 59 | 60 | --- 61 | 62 | ## Notes 63 | 64 | ### Hairpinning 65 | 66 | In order for WLAN clients to see one another, OpenWrt bridges all interfaces in the LAN zone and sets hairpin mode (aka [reflective relay](https://lwn.net/Articles/347344/)) on the WLAN interface, meaning packets arriving on that interface can be 'reflected' back out through the same interface. 67 | 68 | `run.sh` tries to handle this if `WIFI_HAIRPIN` is set to true, and prints a warning if it fails. 69 | Hairpin mode may not be needed in all cases, but if you experience an issue where Wi-Fi clients are unable to see each other despite AP isolation being disabled, this may fix it. 70 | 71 | ### Network namespace 72 | 73 | For `hostapd` running inside the container to have access to the physical wireless device, we need to set the device's network namespace to the PID of the running container. This causes the interface to 'disappear' from the primary network namespace for the duration of the container's parent process. `run.sh` checks if the host is using NetworkManager to manage the wifi interface, and tries to steal it away if so. 74 | 75 | ### Addtional Docker services 76 | 77 | Additional containers that are run alongside OpenWrt on the same physical host are directly accessible on the LAN. No port forwarding to the host is necessary. It's recommended to add static hostnames to be able to resolve local services on your LAN. 78 | 79 | See [Monitoring with InfluxDB + Grafana](monitoring/README.md) for example. 80 | 81 | ### Upgrading 82 | 83 | Read the [upgrade guide](docs/upgrade.md). 84 | 85 | --- 86 | 87 | ### Troubleshooting 88 | 89 | Logs are redirected to `stdout` so the Docker daemon can process them. They are accessible with: 90 | ``` 91 | $ docker logs ${CONTAINER} [-f] 92 | ``` 93 | 94 | As an alternative to installing debug packages inside your router, it's possible to execute commands available to the host inside the network namespace. A symlink is created in `/var/run/netns/` for convenience: 95 | 96 | ``` 97 | $ sudo ip netns exec ${CONTAINER} tcpdump -vvi any 98 | ``` 99 | --- 100 | ## [OpenVPN Howto](docs/vpn.md) 101 | 102 | ## [Bandwidth Monitoring Howto](docs/monitoring.md) 103 | 104 | ## [Monitoring with InfluxDB + Grafana](monitoring/README.md) 105 | 106 | ## [IPv6 Notes](docs/ipv6.md) -------------------------------------------------------------------------------- /build.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | set -e 3 | 4 | 5 | download_rootfs() { 6 | case ${OPENWRT_SOURCE_VER} in 7 | snapshot) 8 | # special snowflake raspberry pi zero 9 | if [ "$ARCH" = "bcm2708" ] ; then 10 | img_url="https://downloads.openwrt.org/snapshots/targets/bcm27xx/bcm2708/openwrt-bcm27xx-bcm2708-rpi-squashfs-factory.img.gz" 11 | version="https://downloads.openwrt.org/snapshots/targets/bcm27xx/bcm2708/version.buildinfo" 12 | gen_rootfs_from_img 13 | return 14 | elif [ "$ARCH" = "armvirt-32" ] ; then 15 | rootfs_url="https://downloads.openwrt.org/snapshots/targets/armvirt/32/openwrt-armvirt-32-default-rootfs.tar.gz" 16 | version="https://downloads.openwrt.org/snapshots/targets/armvirt/32/version.buildinfo" 17 | elif [ "$ARCH" = "armvirt-64" ] ; then 18 | rootfs_url="https://downloads.openwrt.org/snapshots/targets/armvirt/64/openwrt-armvirt-64-default-rootfs.tar.gz" 19 | version="https://downloads.openwrt.org/snapshots/targets/armvirt/64/version.buildinfo" 20 | elif [ "$ARCH" = "x86-64" ] ; then 21 | rootfs_url="https://downloads.openwrt.org/snapshots/targets/x86/64/openwrt-x86-64-rootfs.tar.gz" 22 | version="https://downloads.openwrt.org/snapshots/targets/x86/64/version.buildinfo" 23 | else 24 | echo "Unsupported architecture!" 25 | exit 1 26 | fi 27 | ;; 28 | *) 29 | if [ "$ARCH" = "bcm2708" ] ; then 30 | img_url="https://downloads.openwrt.org/releases/${OPENWRT_SOURCE_VER}/targets/brcm2708/bcm2708/openwrt-${OPENWRT_SOURCE_VER}-brcm2708-bcm2708-rpi-squashfs-factory.img.gz" 31 | version="https://downloads.openwrt.org/releases/${OPENWRT_SOURCE_VER}/targets/brcm2708/bcm2708/version.buildinfo" 32 | gen_rootfs_from_img 33 | return 34 | elif [ "$ARCH" = "armvirt-32" ] ; then 35 | rootfs_url="https://downloads.openwrt.org/releases/${OPENWRT_SOURCE_VER}/targets/armvirt/32/openwrt-${OPENWRT_SOURCE_VER}-armvirt-32-default-rootfs.tar.gz" 36 | version="https://downloads.openwrt.org/releases/${OPENWRT_SOURCE_VER}/targets/armvirt/32/version.buildinfo" 37 | elif [ "$ARCH" = "armvirt-64" ] ; then 38 | rootfs_url="https://downloads.openwrt.org/releases/${OPENWRT_SOURCE_VER}/targets/armvirt/64/openwrt-${OPENWRT_SOURCE_VER}-armvirt-64-default-rootfs.tar.gz" 39 | version="https://downloads.openwrt.org/releases/${OPENWRT_SOURCE_VER}/targets/armvirt/64/version.buildinfo" 40 | elif [ "$ARCH" = "x86-64" ] ; then 41 | rootfs_url="https://downloads.openwrt.org/releases/${OPENWRT_SOURCE_VER}/targets/x86/64/openwrt-${OPENWRT_SOURCE_VER}-x86-64-generic-rootfs.tar.gz" 42 | version="https://downloads.openwrt.org/releases/${OPENWRT_SOURCE_VER}/targets/x86/64/version.buildinfo" 43 | else 44 | echo "Unsupported architecture!" 45 | exit 1 46 | fi 47 | ;; 48 | esac 49 | wget "${rootfs_url}" -O rootfs.tar.gz 50 | wget "${version}" -O version.buildinfo 51 | } 52 | 53 | gen_rootfs_from_img() { 54 | local offset 55 | wget "${img_url}" -O- | gzip -d > image.img 56 | wget "${version}" -O version.buildinfo 57 | offset=$(sfdisk -d image.img | grep "image.img2" | sed -E 's/.*start=\s+([0-9]+).*/\1/g') 58 | fakeroot unsquashfs -no-progress -quiet -offset $(( 512 * offset )) -dest "$tmpdir" image.img 59 | fakeroot tar czf rootfs.tar.gz -C "$tmpdir" . 60 | } 61 | 62 | docker_build() { 63 | docker build \ 64 | --build-arg ts="$(date)" \ 65 | --build-arg version="$(cat version.buildinfo)" \ 66 | -t "$IMAGE":"$TAG" . 67 | } 68 | 69 | cleanup() { 70 | rm -rf rootfs.tar.gz version.buildinfo image.img 71 | if [ -d "$tmpdir" ] ; then 72 | rm -rf "$tmpdir" 73 | fi 74 | } 75 | 76 | test -n "$ARCH" || { echo "\$ARCH is unset. Exiting"; exit 1; } 77 | trap cleanup EXIT 78 | tmpdir=$(mktemp -u -p .) 79 | download_rootfs 80 | docker_build -------------------------------------------------------------------------------- /docs/ipv6.md: -------------------------------------------------------------------------------- 1 | # IPv6 2 | 3 | IPv6 is not enabled by default in Docker. Since we are configuring our network inside the container all we need to do is add the following `sysctl`s: 4 | ``` 5 | net.ipv6.conf.all.disable_ipv6=0 6 | net.ipv6.conf.all.forwarding=1 7 | ``` 8 | And leave the rest to OpenWrt. 9 | 10 | It's recommended to generate a good random ULA prefix using something like https://simpledns.plus/private-ipv6 and setting `LAN6_PREFIX` to something other than the default. 11 | 12 | ## Bandwidth 13 | 14 | For bandwidth monitoring of ipv6 it may also be necessary to load the module `nf_conntrack_ipv6` on the host: 15 | ``` 16 | $ sudo modprobe nf_conntrack_ipv6 17 | ``` 18 | 19 | ## Other 20 | I noticed that my syslog was filled with the following line about every 3-5 seconds: 21 | ``` 22 | Tue Mar 3 23:39:26 2020 daemon.notice netifd: wan6 (5949): /lib/netifd/dhcpv6.script: line 14: can't create /proc/sys/net/ipv6/conf/eth1/mtu: Read-only file system 23 | ``` 24 | 25 | Every time ICMPv6 RA messages are received, `odhcp6c` triggers this script. Since we are running in Docker and can't modify `sysctl`s from inside the container, this error would be printed. 26 | 27 | The solution workaround is to comment out any lines in `/lib/netifd/dhcpv6.script` that try to modify kernel parameters. See [etc/dhcpv6.script](./etc/dhcpv6.script). 28 | 29 | Obviously this is an ugly hack but it works. 30 | The other solution would be to run the container with `--privileged`, but that's a terrible idea. -------------------------------------------------------------------------------- /docs/monitoring.md: -------------------------------------------------------------------------------- 1 | # Bandwidth Monitoring 2 | 3 | OpenWRT comes with a decent selection of traffic monitoring tools, both CLI and web UI. I use `nlbwmon`, which integrates well with the web interface and creates pretty graphs. Here's how to install it. 4 | 5 | ## Enable conntrack accounting 6 | 7 | `nlbwmon` uses the Linux netfilter conntrack subsystem to track connections and packet counts, so you need to make sure the `nf_conntrack` kernel module is loaded on your host system (it probably is). But just to check: 8 | ``` 9 | $ lsmod | grep nf_conntrack 10 | ``` 11 | 12 | Conntrack accounting is off by default, so we have to enable it inside the container: 13 | ``` 14 | $ sudo ip netns exec ${CONTAINER} sysctl -w net.netfilter.nf_conntrack_acct=1 15 | ``` 16 | Alternatively this can be enabled when creating the container by adding the flag 17 | ``` 18 | --sysctl net.netfilter.nf_conntrack_acct=1 19 | ``` 20 | to the `docker create` command in `run.sh`. 21 | 22 | ## Install packages 23 | Inside the container: 24 | ``` 25 | # opkg install nlbwmon luci-app-nlbwmon 26 | # service nlbwmon enable 27 | # service nlbwmon start 28 | ``` 29 | 30 | There should now be a "Bandwidth Monitor" section in LuCI. 31 | 32 | ## Configuration 33 | The default configuration is extremely conservative with storage. Since we're not running on a device with 4MB flash storage, we can increase the defaults: 34 | 35 | ``` 36 | # cat <` with the desired release version (e.g. "19.07.5") or "snapshot" for the latest development snapshot: 10 | 11 | | RPi version | image:tag | 12 | |--------------------------|---------------------------------------| 13 | | Pi A / B / B+ / Zero W | `oofnik/openwrt:-bcm2708` | 14 | | Pi 2 / 3 / 4 (32-bit OS) | `oofnik/openwrt:-armvirt-32` | 15 | | Pi 3 / 4 (64-bit OS) | `oofnik/openwrt:-armvirt-64` | 16 | 17 | 18 | --- 19 | ## Build 20 | You can build the OpenWrt Docker image yourself on the Pi, or on your x86 PC with `qemu-user` and `binfmt-support` installed. The image will be built according to the parameters `OPENWRT_SOURCE_VER`, `ARCH`, `IMAGE` and `TAG` (see [openwrt.conf](../openwrt.conf.example) for documentation). Packages `fakeroot` and `squashfs-tools` must be installed. 21 | 22 | ```shell 23 | $ make build 24 | ``` 25 | 26 | If you built the image on your PC, send it to your Raspberry Pi over SSH: 27 | ``` 28 | $ docker save $IMAGE:$TAG | ssh docker load 29 | ``` 30 | 31 | ## IPv6 32 | By default Raspberry Pi OS does not load the kernel module for IPv6 `iptables` on boot. 33 | 34 | Run `sudo modprobe ip6_tables` to load it immediately. 35 | 36 | To persist on reboot, run 37 | 38 | $ echo 'ip6_tables' | sudo tee /etc/modules-load.d/ip6-tables.conf 39 | 40 | ## DHCP 41 | Raspberry Pi OS ships with a default `dhcpcd` configuration that assigns a DHCP address to every new interface added to the system including virtual ones. 42 | 43 | This is not necessarily what we want. Instead we would like to limit the interfaces `dhcpcd` watches to only the physical ones, i.e., interfaces `eth*` and `wl*`: 44 | 45 | ``` 46 | $ echo 'allowinterfaces eth* wl*' | sudo tee -a /etc/dhcpcd.conf 47 | $ sudo dhcpcd -n 48 | ``` -------------------------------------------------------------------------------- /docs/upgrade.md: -------------------------------------------------------------------------------- 1 | # Upgrade Guide 2 | 3 | 4 | All commands are executed on the host running the OpenWrt container 5 | unless otherwise specified. 6 | 7 | Make a list of installed packages using the script provided in 8 | `etc/list-user-installed.sh`: 9 | 10 | $ source openwrt.conf 11 | $ docker cp ./etc/list-user-installed.sh ${CONTAINER}:/tmp/ 12 | $ docker exec $CONTAINER /tmp/list-user-installed.sh > packages 13 | 14 | Make changes to `openwrt.conf` as necessary (refer to example). 15 | Some parameters may have changed. 16 | 17 | Pull (or build) the latest image for your architecture: 18 | 19 | $ docker pull $IMAGE:$TAG 20 | 21 | OpenWrt contains a backup / restore utility called `sysupgrade`. 22 | Use it to make a configuration backup archive: 23 | 24 | $ docker exec $CONTAINER sysupgrade --create-backup - > backup.tar.gz 25 | 26 | Docker manages `/etc/hosts` as a bind mount. We need to ingore this file 27 | upon restore to avoid errors, so remove it from the archive: 28 | 29 | $ gzip -cd backup.tar.gz | \ 30 | tar --delete etc/hosts | \ 31 | gzip > backup1.tar.gz 32 | $ mv backup1.tar.gz backup.tar.gz 33 | 34 | **Confirm that all expected files are present in the archive before proceeding!** 35 | 36 | At this point you may stop the old container: 37 | 38 | docker stop $CONTAINER 39 | 40 | As a precaution, it's recommended to rename the old container 41 | before deleting it outright: 42 | 43 | $ docker rename $CONTAINER ${CONTAINER}-backup 44 | 45 | Run the `make clean` target to ensure the new container gets created properly. 46 | 47 | If using `systemd`, start the OpenWrt service with `systemctl start openwrt`. 48 | Otherwise, `make run`. 49 | 50 | Restore the configuration: 51 | 52 | $ cat backup.tar.gz | \ 53 | docker exec -i $CONTAINER sysupgrade -v --restore-backup - 54 | 55 | Install packages: 56 | 57 | $ docker exec -t $CONTAINER opkg install $(cat packages) 58 | 59 | Log in to your new OpenWrt instance and make sure everything is working. You may need to restart some services. 60 | 61 | Once everything is confirmed working, it's safe to delete the backup container: 62 | 63 | $ docker rm ${CONTAINER}-backup -------------------------------------------------------------------------------- /docs/vpn.md: -------------------------------------------------------------------------------- 1 | # Configure OpenVPN server 2 | 3 | Here is a short guide on how to set up a VPN server on OpenWRT. 4 | 5 | * Need to create `/dev/net/tun` inside the container on boot: 6 | ``` 7 | # sed -i '$i\ 8 | mkdir -p /dev/net\ 9 | mknod /dev/net/tun c 10 200' /etc/rc.local 10 | ``` 11 | 12 | 13 | ## Add firewall rules 14 | We will be using `10.16.0.0/24` as our VPN subnet. 15 | 16 | * Add `tun0` device to LAN zone and allow port 1194 UDP from WAN: 17 | 18 | ``` 19 | # cat < 61 | ``` 62 | 63 | * Generate pre-shared key 64 | ``` 65 | # openvpn --genkey --secret ${EASYRSA_PKI}/tls.pem 66 | ``` 67 | 68 | * (Re-)initialize the PKI directory 69 | ``` 70 | # easyrsa --batch init-pki 71 | ``` 72 | 73 | * Generate DH parameters 74 | ``` 75 | # easyrsa --batch gen-dh 76 | ``` 77 | 78 | * Create a new CA if you don't already have one 79 | ``` 80 | # easyrsa --batch build-ca nopass 81 | ``` 82 | * Generate a keypair and sign locally for a server 83 | ``` 84 | # easyrsa --batch build-server-full server nopass 85 | ``` 86 | 87 | * Generate a keypair and sign locally for a client 88 | ``` 89 | # easyrsa --batch build-client-full client nopass 90 | ``` 91 | 92 | Repeat the last step for any additional clients. 93 | 94 | ## Generate server configuration with UCI 95 | 96 | ``` 97 | # cat < client.ovpn 129 | client 130 | dev tun 131 | remote ${EASYRSA_REQ_CN} 1194 udp 132 | resolv-retry infinite 133 | user 999 134 | group 999 135 | nobind 136 | persist-key 137 | persist-tun 138 | tls-client 139 | topology subnet 140 | remote-cert-tls server 141 | cipher AES-128-CBC 142 | 143 | 144 | $(cat ${EASYRSA_PKI}/tls.pem) 145 | 146 | 147 | $(openssl x509 -in $EASYRSA_PKI/ca.crt) 148 | 149 | 150 | $(openssl x509 -in $EASYRSA_PKI/issued/client.crt) 151 | 152 | 153 | $(cat $EASYRSA_PKI/private/client.key) 154 | 155 | EOF 156 | ``` 157 | 158 | Copy `client.ovpn` to your client and try to connect. 159 | 160 | --- 161 | ## Reference 162 | 1. https://openwrt.org/docs/guide-user/services/vpn/openvpn/basic 163 | 1. https://openvpn.net/community-resources/reference-manual-for-openvpn-2-4 164 | -------------------------------------------------------------------------------- /etc/config/dhcp.tpl: -------------------------------------------------------------------------------- 1 | config dnsmasq 2 | option domainneeded '1' 3 | option localise_queries '1' 4 | option local "/${LAN_DOMAIN}/" 5 | option domain "${LAN_DOMAIN}" 6 | option expandhosts '1' 7 | option authoritative '1' 8 | option readethers '1' 9 | option leasefile '/tmp/dhcp.leases' 10 | option localservice '1' 11 | option rebind_protection '1' 12 | option rebind_domain "${LAN_DOMAIN}" 13 | list server "${UPSTREAM_DNS_SERVER}" 14 | 15 | config dhcp 'lan' 16 | option interface 'lan' 17 | option start '100' 18 | option limit '150' 19 | option leasetime '12h' 20 | option dhcpv6 'server' 21 | option ra 'server' 22 | option ndp 'hybrid' 23 | 24 | config dhcp 'wan' 25 | option ignore '1' 26 | option interface 'wan' 27 | option dhcpv6 'relay' 28 | option ra 'relay' 29 | option ndp 'relay' 30 | option master '1' 31 | 32 | config odhcpd 'odhcpd' 33 | option maindhcp '0' 34 | option leasefile '/tmp/hosts/odhcpd' 35 | option leasetrigger '/usr/sbin/odhcpd-update' 36 | option loglevel '4' 37 | -------------------------------------------------------------------------------- /etc/config/network.tpl: -------------------------------------------------------------------------------- 1 | config globals globals 2 | option 'ula_prefix' 'auto' 3 | 4 | config 'interface' 'loopback' 5 | option 'ifname' 'lo' 6 | option 'proto' 'static' 7 | option 'ipaddr' '127.0.0.1' 8 | option 'netmask' '255.0.0.0' 9 | 10 | config 'interface' 'lan' 11 | option 'type' 'bridge' 12 | option 'ifname' 'eth0' 13 | option 'proto' 'static' 14 | option 'ipaddr' "${LAN_ADDR}" 15 | option 'gateway' "${LAN_GW}" 16 | option 'netmask' "${LAN_NETMASK}" 17 | option 'ip6assign' 64 18 | 19 | config 'interface' 'wan' 20 | option 'ifname' 'eth1' 21 | option 'proto' 'dhcp' 22 | 23 | config 'interface' 'wan6' 24 | option 'ifname' 'eth1' 25 | option 'proto' 'dhcpv6' 26 | -------------------------------------------------------------------------------- /etc/config/system.tpl: -------------------------------------------------------------------------------- 1 | config system 2 | option ttylogin '0' 3 | option urandom_seed '0' 4 | option zonename 'UTC' 5 | option log_size '256' 6 | option cronloglevel '5' 7 | option log_proto 'udp' 8 | option log_file '/proc/1/fd/1' 9 | option conloglevel '5' 10 | 11 | config timeserver 'ntp' 12 | list server '0.openwrt.pool.ntp.org' 13 | list server '1.openwrt.pool.ntp.org' 14 | list server '2.openwrt.pool.ntp.org' 15 | list server '3.openwrt.pool.ntp.org' 16 | -------------------------------------------------------------------------------- /etc/config/wireless.tpl: -------------------------------------------------------------------------------- 1 | config 'wifi-device' 'radio0' 2 | option 'type' 'mac80211' 3 | option 'phy' "${WIFI_PHY}" 4 | option 'hwmode' "${WIFI_HW_MODE}" 5 | option 'htmode' "${WIFI_HT_MODE}" 6 | option 'channel' "${WIFI_CHANNEL}" 7 | 8 | config 'wifi-iface' "${WIFI_IFACE}" 9 | option 'device' 'radio0' 10 | option 'network' 'lan' 11 | option 'mode' 'ap' 12 | option 'ifname' "${WIFI_IFACE}" 13 | option 'ssid' "${WIFI_SSID}" 14 | option 'encryption' "${WIFI_ENCRYPTION}" 15 | option 'key' "${WIFI_KEY}" 16 | option 'disassoc_low_ack' '0' 17 | -------------------------------------------------------------------------------- /etc/intercept-dns.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | ### 3 | # DNS interceptor 4 | # iptables equivalent: 5 | # sudo iptables -t nat -A PREROUTING -p udp --dport 53 -j REDIRECT 6 | # 7 | # can be applied by: 8 | # $ cat intercept-dns.sh | docker exec -i openwrt_1 sh 9 | ### 10 | uci -q delete firewall.dns_int 11 | /sbin/uci batch < 31 | # uci batch < /proc/sys/net/ipv6/conf/$device/hop_limit 14 | - [ -n "$RA_MTU" ] && [ "$RA_MTU" -ge 1280 ] && echo "$RA_MTU" > /proc/sys/net/ipv6/conf/$device/mtu 2>/dev/null 15 | - [ -n "$RA_REACHABLE" ] && [ "$RA_REACHABLE" -gt 0 ] && echo "$RA_REACHABLE" > /proc/sys/net/ipv6/neigh/$device/base_reachable_time_ms 16 | - [ -n "$RA_RETRANSMIT" ] && [ "$RA_RETRANSMIT" -gt 0 ] && echo "$RA_RETRANSMIT" > /proc/sys/net/ipv6/neigh/$device/retrans_time_ms 17 | 18 | proto_init_update "*" 1 19 | 20 | -- 21 | 2.29.1 22 | 23 | -------------------------------------------------------------------------------- /patches/gre.sh.patch: -------------------------------------------------------------------------------- 1 | Subject: [PATCH] Remove kernel module check for GRE (/lib/netifd/proto/gre.sh), 2 | they will be loaded automatically by host kernel. 3 | 4 | --- 5 | diff --git a/gre.sh.bak b/gre.sh 6 | index 3414ed3..68544bb 100755 7 | --- a/lib/netifd/proto/gre.sh 8 | +++ b/lib/netifd/proto/gre.sh 9 | @@ -289,8 +289,8 @@ proto_grev6tap_init_config() { 10 | } 11 | 12 | [ -n "$INCLUDE_ONLY" ] || { 13 | - [ -f /lib/modules/$(uname -r)/gre.ko ] && add_protocol gre 14 | - [ -f /lib/modules/$(uname -r)/gre.ko ] && add_protocol gretap 15 | - [ -f /lib/modules/$(uname -r)/ip6_gre.ko ] && add_protocol grev6 16 | - [ -f /lib/modules/$(uname -r)/ip6_gre.ko ] && add_protocol grev6tap 17 | + add_protocol gre 18 | + add_protocol gretap 19 | + add_protocol grev6 20 | + add_protocol grev6tap 21 | } 22 | 23 | -- 24 | 2.29.1 25 | 26 | -------------------------------------------------------------------------------- /run.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | # set -x 3 | 4 | _usage() { 5 | echo "Could not find config file." 6 | echo "Usage: $0 [/path/to/openwrt.conf]" 7 | exit 1 8 | } 9 | 10 | SCRIPT_DIR=$(cd $(dirname $0) && pwd ) 11 | DEFAULT_CONFIG_FILE=$SCRIPT_DIR/openwrt.conf 12 | CONFIG_FILE=${1:-$DEFAULT_CONFIG_FILE} 13 | source $CONFIG_FILE 2>/dev/null || { _usage; exit 1; } 14 | 15 | _nmcli() { 16 | type nmcli >/dev/null 2>&1 17 | if [[ $? -eq 0 ]]; then 18 | echo "* setting interface '$WIFI_IFACE' to unmanaged" 19 | nmcli dev set $WIFI_IFACE managed no 20 | nmcli radio wifi on 21 | fi 22 | } 23 | 24 | _get_phy_from_dev() { 25 | test $WIFI_ENABLED = 'true' || return 26 | test -z $WIFI_PHY || return 27 | if [[ -f /sys/class/net/$WIFI_IFACE/phy80211/name ]]; then 28 | WIFI_PHY=$(cat /sys/class/net/$WIFI_IFACE/phy80211/name 2>/dev/null) 29 | echo "* got '$WIFI_PHY' for device '$WIFI_IFACE'" 30 | else 31 | echo "$WIFI_IFACE is not a valid phy80211 device" 32 | exit 1 33 | fi 34 | } 35 | 36 | _cleanup() { 37 | echo -e "\n* cleaning up..." 38 | echo "* stopping container" 39 | docker stop $CONTAINER >/dev/null 40 | echo "* cleaning up netns symlink" 41 | sudo rm -rf /var/run/netns/$CONTAINER 42 | echo "* removing host $LAN_DRIVER interface" 43 | if [[ $LAN_DRIVER != "bridge" ]] ; then 44 | sudo ip link del dev $LAN_IFACE 45 | elif [[ $LAN_PARENT =~ \. ]] ; then 46 | sudo ip link del dev $LAN_PARENT 47 | fi 48 | echo -ne "* finished" 49 | } 50 | 51 | _gen_config() { 52 | echo "* generating network config" 53 | set -a 54 | _get_phy_from_dev 55 | source $CONFIG_FILE 56 | for file in etc/config/*.tpl; do 57 | envsubst <${file} >${file%.tpl} 58 | docker cp ${file%.tpl} $CONTAINER:/${file%.tpl} 59 | done 60 | set +a 61 | } 62 | 63 | _init_network() { 64 | echo "* setting up docker network" 65 | local LAN_ARGS 66 | case $LAN_DRIVER in 67 | bridge) 68 | LAN_ARGS="" 69 | ;; 70 | macvlan) 71 | LAN_ARGS="-o parent=$LAN_PARENT" 72 | ;; 73 | ipvlan) 74 | LAN_ARGS="-o parent=$LAN_PARENT -o ipvlan_mode=l2" 75 | ;; 76 | *) 77 | echo "invalid choice for LAN network driver" 78 | exit 1 79 | ;; 80 | esac 81 | docker network create --driver $LAN_DRIVER \ 82 | $LAN_ARGS \ 83 | --subnet $LAN_SUBNET \ 84 | $LAN_NAME || exit 1 85 | 86 | docker network create --driver $WAN_DRIVER \ 87 | -o parent=$WAN_PARENT \ 88 | $WAN_NAME || exit 1 89 | } 90 | 91 | _set_hairpin() { 92 | test $WIFI_HAIRPIN = 'true' || return 93 | echo -n "* set hairpin mode on interface '$1'" 94 | for i in {1..10}; do 95 | echo -n '.' 96 | sudo ip netns exec $CONTAINER ip link set $WIFI_IFACE type bridge_slave hairpin on 2>/dev/null && { echo 'ok'; break; } 97 | sleep 3 98 | done 99 | if [[ $i -ge 10 ]]; then 100 | echo -e "\ncouldn't set hairpin mode, wifi clients will probably be unable to talk to each other" 101 | fi 102 | } 103 | 104 | _create_or_start_container() { 105 | if ! docker inspect $IMAGE:$TAG >/dev/null 2>&1; then 106 | echo "no image '$IMAGE:$TAG' found, did you forget to run 'make build'?" 107 | exit 1 108 | 109 | elif docker inspect $CONTAINER >/dev/null 2>&1; then 110 | echo "* starting container '$CONTAINER'" 111 | docker start $CONTAINER || exit 1 112 | 113 | else 114 | _init_network 115 | echo "* creating container $CONTAINER" 116 | docker create \ 117 | --network $LAN_NAME \ 118 | --cap-add NET_ADMIN \ 119 | --cap-add NET_RAW \ 120 | --hostname openwrt \ 121 | --ip $LAN_ADDR \ 122 | --sysctl net.netfilter.nf_conntrack_acct=1 \ 123 | --sysctl net.ipv6.conf.all.disable_ipv6=0 \ 124 | --sysctl net.ipv6.conf.all.forwarding=1 \ 125 | ${ADDITIONAL_DOCKER_CREATE_ARGS} --name $CONTAINER $IMAGE:$TAG >/dev/null 126 | docker network connect $WAN_NAME $CONTAINER 127 | 128 | _gen_config 129 | docker start $CONTAINER 130 | fi 131 | } 132 | 133 | _reload_fw() { 134 | echo "* reloading firewall rules" 135 | docker exec -i $CONTAINER sh -c ' 136 | for iptables in iptables ip6tables; do 137 | for table in filter nat mangle; do 138 | $iptables -t $table -F 139 | done 140 | done 141 | /sbin/fw3 -q restart' 142 | } 143 | 144 | _prepare_wifi() { 145 | test $WIFI_ENABLED = 'true' || return 146 | test -z $WIFI_IFACE && _usage 147 | _get_phy_from_dev 148 | _nmcli 149 | echo "* moving device $WIFI_PHY to docker network namespace" 150 | sudo iw phy "$WIFI_PHY" set netns $pid 151 | _set_hairpin $WIFI_IFACE 152 | } 153 | 154 | _prepare_network() { 155 | case $LAN_DRIVER in 156 | macvlan) 157 | echo "* setting up host $LAN_DRIVER interface" 158 | LAN_IFACE=macvlan0 159 | sudo ip link add $LAN_IFACE link $LAN_PARENT type $LAN_DRIVER mode bridge 160 | sudo ip link set $LAN_IFACE up 161 | sudo ip route add $LAN_SUBNET dev $LAN_IFACE 162 | ;; 163 | ipvlan) 164 | echo "* setting up host $LAN_DRIVER interface" 165 | LAN_IFACE=ipvlan0 166 | sudo ip link add $LAN_IFACE link $LAN_PARENT type $LAN_DRIVER mode l2 167 | sudo ip link set $LAN_IFACE up 168 | sudo ip route add $LAN_SUBNET dev $LAN_IFACE 169 | ;; 170 | bridge) 171 | LAN_ID=$(docker network inspect $LAN_NAME -f "{{.Id}}") 172 | LAN_IFACE=br-${LAN_ID:0:12} 173 | 174 | # test if $LAN_PARENT is a VLAN of $WAN_PARENT, create it if it doesn't exist and add it to the bridge 175 | local lan_array=(${LAN_PARENT//./ }) 176 | if [[ ${lan_array[0]} = $WAN_PARENT ]] && ! ip link show $LAN_PARENT >/dev/null 2>&1 ; then 177 | sudo ip link add link ${lan_array[0]} name $LAN_PARENT type vlan id ${lan_array[1]} 178 | fi 179 | sudo ip link set $LAN_PARENT master $LAN_IFACE 180 | ;; 181 | *) 182 | echo "invalid network driver type, must be 'bridge' or 'macvlan'" 183 | exit 1 184 | ;; 185 | esac 186 | 187 | if [[ "${WAN_DRIVER}" = "ipvlan" ]] ; then 188 | echo "* 'ipvlan' mode selected for WAN interface" 189 | # need to set DHCP broadcast flag 190 | # and set clientid to some random value so we get a new lease 191 | # https://tools.ietf.org/html/rfc1542#section-3.1.1 192 | local client_id 193 | client_id=$(tr -dc 'A-F0-9' < /dev/urandom | head -c12) 194 | docker exec -it $CONTAINER sh -c " 195 | uci -q set network.wan.broadcast=1 196 | uci -q set network.wan.clientid=${client_id} 197 | uci commit" 198 | fi 199 | 200 | echo "* getting address via DHCP" 201 | sudo dhcpcd -q $LAN_IFACE 202 | } 203 | 204 | main() { 205 | cd "${SCRIPT_DIR}" 206 | _create_or_start_container 207 | 208 | pid=$(docker inspect -f '{{.State.Pid}}' $CONTAINER) 209 | 210 | echo "* creating netns symlink '$CONTAINER'" 211 | sudo mkdir -p /var/run/netns 212 | sudo ln -sf /proc/$pid/ns/net /var/run/netns/$CONTAINER 213 | 214 | _prepare_wifi 215 | _prepare_network 216 | 217 | _reload_fw 218 | echo "* ready" 219 | } 220 | 221 | main 222 | trap "_cleanup" EXIT 223 | tail --pid=$pid -f /dev/null 224 | --------------------------------------------------------------------------------