haohaolee's blog

nerd以上,geek未满

Create a VLAN Trunk in TL-WR703N

TL-WR703N is a very tiny router, made by TP-LINK who markets it as “3G travel router”, although without a builtin 3G modem you cannot really say it is a 3G router, which is still an awesome device for its size. Very neat.

TL-WR703N 是一个非常小的路由器,生产商 TP-LINK 把它宣传为 “3G 旅行路由器”,然而不自带 3G 猫芯的路由器不能算作真正意义上的 3g 设备,不过它仍然是非常赞的东东,因为实在太小巧了。

There are many stories about how to make it a low power 24x7 downloading box, or a wireless audio streaming box with OpenWrt, I still wanna make it a better router. Since it has only one ethernet interface and has no switch built-in, therefore we often change its role as a normal router with wireless clients to a wireless bridge or repeater with wired clients. It would be cool if the only interface can automatically vary its role according to different situation. To be brief, our goal is:

  1. When a node’s ethernet wire is plugged in, and makes DHCP requests, the router should assign it a LAN IP.
  2. Otherwise, the router should consider the wire as an WAN path.

网上有许多关于如何用 OpenWrt 把它做成一个低功耗 24x7 的下载机,或者无线音乐播放设备的故事。不过我还是只想尝试把它折腾成一个更好的路由器。因为只有一个以太网接口并且内部也没有交换机芯片,所以我们常常要在带无线客户端的路由器和带有线客户端的中继或者网桥之间转换。但是如果不需要配置就能自动切换这两种功能就更棒了。简单地说,我们的目标是:

  1. 当有线客户端接入并且请求 DHCP 时,路由器应该分配给它一个 IP,并纳入 LAN。
  2. 其他情况,一律视作 WAN 接口。

Currently, there is a solution to this on the net is to leverage the linux kernel functionality macvlan, and make a virtual interface based on the physical interface with different MAC. This approach just works, but I don’t like the way it shares all the packets of LAN and WAN on the wire without discrimination, and OpenWrt has no uci config for macvlan so far. So I think VLAN trunk is a better way, at least for me. This approach needs configurations on the wired LAN clients, such as Computers and other linux routers. Since the typical applications of mine involve only my own devices, it’s not a big deal.

目前有一种方法可以解决,就是利用 linux kernel 的 macvlan 功能,创建一个基于物理接口的虚拟接口,只是 MAC 不同。这种方法可以工作,不过我不喜欢它对数据包完全不加以区分的方式,并且 OpenWrt 没有专门针对的 macvlan 的配置文件。这种情况下我更倾向于 VLAN trunk。但是这种办法需要在客户端,比如电脑或者其他路由器上进行配置,不过我的典型应用只涉及到我自己的设备,所以这不是什么大问题。

The config on the OpenWrt side:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# /etc/config/network

config interface 'loopback'
    option ifname 'lo'
    option proto 'static'
    option ipaddr '127.0.0.1'
    option netmask '255.0.0.0'

config interface 'wan'
    option proto 'dhcp'
    option ifname 'eth0'

config interface 'lan'
    option type 'bridge'
    option proto 'static'
    option netmask '255.255.255.0'
    option ipaddr '172.16.0.1'
    option ifname 'eth0.2'
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# /etc/config/wireless

config wifi-device 'radio0'
    option type 'mac80211'
    option macaddr 'xx:xx:xx:xx:xx:xx'
    option hwmode '11ng'
    option htmode 'HT20'
    list ht_capab 'SHORT-GI-20'
    list ht_capab 'SHORT-GI-40'
    list ht_capab 'RX-STBC1'
    list ht_capabb 'DSSS_CCK-40'
    option txpower '27'
    option country 'US'
    option channel '11'

config wifi-iface
    option device 'radio0'
    option mode 'ap'
    option ssid 'OpenWrt'
    option encryption 'none'
    option network 'lan'

eth0 is WAN, which should work without change; eth0.2 is a VLAN interface created by linux vconfig tagged with ID 2, which is not a hardware VLAN, and eth0.2 is bridged with wireless.

eth0 是 WAN 口,像往常一样不需变动;eth0.2 是 linux vconfig 创建的 VLAN 口, ID 为 2,并非硬件 VLAN,并且 eth0.2 和无线桥接到了一起。

On the client side, the configurations depend on the NIC driver. Most recent intel NICs have VLAN support with official drivers, so do the Realtek ones, but they need additional utility called Windows Diagnostic Program to do this. Here are screen shots:

在客户端这边,配置取决于网卡驱动,大部分现代的 Intel 网卡都支持 VLAN,Realtek 也支持,只不过需要专门的工具来进行配置。截图几枚:

Comments