200字范文,内容丰富有趣,生活中的好帮手!
200字范文 > 从零开始 把Raspberry Pi打造成双栈11n无线路由器 支持教育网原生IPv6

从零开始 把Raspberry Pi打造成双栈11n无线路由器 支持教育网原生IPv6

时间:2024-04-14 20:37:14

相关推荐

从零开始 把Raspberry Pi打造成双栈11n无线路由器 支持教育网原生IPv6

从零开始,把Raspberry Pi打造成双栈11n无线路由器,支持教育网原生IPv6

Skip to content

hahaschool

Adam's Blog

Tags

ACMBFSCFCPUDFSFZUhashHDUKMPLinuxMiscPOJRPiSCCSGUSTLTrieUVAZOJ二分二进制枚举几何分治前缀和动态规划博弈图论基础知识基础题字符串处理小总结归并排序找规律拓扑排序排序搜索数学数据结构数论暴力树模拟线段树记忆化搜索贪心

Recent Posts

SGU 103 Traffic LightsCodeforces 118D Caesar’s LegionsCodeForces 347C Alice and Bob从零开始,把Raspberry Pi打造成双栈11n无线路由器,支持教育网原生IPv6CodeForces 484B Maximum Value

Recent Comments

hahaschool on HDU 1166 敌兵布阵qwertyuiop on HDU 1166 敌兵布阵Eraser on 发个图试试hahaschool on CF 460C Presentahah on CF 460C Present

Archives

March February January

Categories

LinuxRaspberry PiUncategorized瞎划拉的算法解题报告

Meta

Log inEntries RSSComments RSS

从零开始,把Raspberry Pi打造成双栈11n无线路由器,支持教育网原生IPv6

准备工作

Raspberry Pi一块 要求已经刷写好了Raspbian系统,关于系统的刷写/无显示器配置这种事情,请参照这里,这里不想多说。 USB无线网卡一枚,一个合格的USB无线网卡最好是不用USB HUB就能稳定运作的,插上之后,在终端机中输入ifconfig输出内容中应该有出现wlan0字样。 无论你有没有现成的USB无线网卡,都请查看USB无线网卡对RPi兼容性列表来确定自己的USB无线网卡是不是支持RPi,如果是杂牌的话,要想办法看到自己的USB无线网卡的芯片型号,然后对照这个表里面有同样型号的无线网卡的兼容性说明。如果你现在无线网卡的芯片在这个表里面不是针对Raspbian out of box的,买买买,不要停~AP功能是一定要有的,如果有11n的话更好(后面会讲如何设置),目前来说直到RPi 2 Model B都没有实装USB 3.0所以没有必要买ac的无线网卡。我用的是Tenda W311U+,这款有个天线,不过貌似也没啥太大的效果,用起来蛮稳定,速度一般(即使打开了11n机能) 外网,这篇教程针对的是没有认证的,原生支持IPv6的教育网,有固定IP。 如果你的网络需要认证客户端的话,可以找个借口(用mac什么的)把认证取消,取消不了的话。。。别折腾了~(当然如果你真想折腾,openwrt社区里面会有dalao提供linux的模拟验证客户端,但是这个不在本文讨论之列)如果你是ADSL用户,本文没有拨号设定,IPv6也需要tunnel,这种情况,建议移步这里如果你不想折腾IPv6有关机能,忽略本文的IPv6部分即可,同样的,不想开11n的话,也可以忽略本文的11n设定部分。 确定你的有线无线网卡代号,在终端机中输入ifconfig就可以看到当前连接的所有网络接口,有线网络一般是eth开头,RPi的自带网卡一般是eth0,你连接的无线网卡一般是wlan开头,只插一个的话就是wlan0,这个是给内网用的。下文中的eth0和wlan0就是这么来的,如果不一样的话请自行翻译~

开工!先来配置IPv4无线路由

配置网络接口

在终端机中输入sudo emacs /etc/network/interfaces打开网络接口配置文件,狂按Ctrl+k清除全部内容,然后写入一下内容,完成后按Control+x,Control+c,y保存退出(放弃更改的话Ctrl+x,Ctrl+c,n,yes<换行键>)。

auto loiface lo inet loopbackiface eth0 inet static #如果你的IP是自动分配的,把static改成dhcp,然后去掉下面三行就可以了address 125.216.249.000 #改成学校给你的固定IPnetmask 255.255.255.0 #学校给你的子网掩码gateway 125.216.249.254 #学校给你的网关iface eth0 inet6 manual #IPv6设定,不管他iface wlan0 inet staticaddress 10.0.0.1 #内网网关(可以理解成路由器)地址,也可以写成192.168.1.1什么的,那样的话本文后面的部分你也要做相应的改动了。netmask 255.255.255.0 #内网子网掩码,虽然是A段网络,但RPi显然没有NB到能拖那么多设备,所以三个255就够用。iface wlan0 inet6 manual #IPv6设定,不管他

配置好之后,随便ping个网站看看能不能通外网,可以的话继续~

配置外网DNS

终端机中输入sudo emacs /etc/resolv.conf,在里面按下面的样子加上你需要的DNS地址

nameserver 222.201.130.30nameserver 222.201.130.33nameserver 8.8.8.8nameserver 114.114.114.114

安装必要的软件包

这里假定你已经可以流畅的链接你的apt软件源了,如果官方源慢的要死就需要换源了,换源教程在这里。

按你的需要在终端机中(有选择性地)输入如下命令:

编译工具

sudo apt-get install subversion git build-essential autotools-dev autoconf libtool gettext gawk gperf cmake

即便装了一堆也有可能发生有软件包装不了的情况,不过本文提到的需要编译的软件基本都能过。

编辑器

sudo apt-get install vim emacs

这俩选一个,或者都不选用系统自带地nano,我用emacs,我不想挑起战争~

AP与IPv4机能

sudo apt-get install udhcpd hostapd iptables wireless-tools

IPv6机能

软件包
sudo apt-get install radvd wide-dhcpv6-server

本文使用NPD Proxy和DHCPV6实现IPv6机能。

编译安装npd6
cd ~sudo git clone /npd6/npd6 npd6cd npd6sudo makesudo make install

配置hostapd以使用无线网卡软AP

在终端机中输入sudo emacs /etc/default/hostapd,打开之后找DAEMON_CONF="",把这行首的#去掉,然后把这句话改成DAEMON_CONF="/etc/hostapd/hostapd.conf",保存退出。

在终端机输入sudo mkdir /etc/hostapd,然后sudo emacs /etc/hostapd/hostapd.conf,把文件内容(有的话清除,没有的话就从头写)改成(请仔细看注释有选择性地写~):

#这里是无线网卡的代号interface=wlan0#这个一般不用改,除非你的无线网卡比较奇葩要单独指定驱动,大部分无线网卡驱动都是基于下面这个的driver=nl80211#无线网络名称ssid=744 Experimental#这个不用改hw_mode=g#这个如果不开11n的话,可以改一个不太拥挤的信道提高链接速度channel=11#这个不用改auth_algs=3#这个不用改dtim_period=1#下面这个去掉会增加速度但是降低稳定性rts_threshold=2347#下面这个去掉会增加速度但是降低稳定性fragm_threshold=2346#这个不用改ignore_broadcast_ssid=0#网络加密模式,0就是没有,1是WPA,2是WPA2,3是两个全开wpa=3#WIFI密码wpa_passphrase=hahaschool#这个不用改wpa_key_mgmt=WPA-PSK#wpa那个填1的话要去掉一个wpa_pairwise=TKIPrsn_pairwise=CCMP#WMM机能,不知道是什么的话开着就行wmm_enabled=1#MAC地址黑白名单,本文不介绍这个机能macaddr_acl=0 #for filtering make this 1 and decomment below and create two lists #accept_mac_file=/etc/hostapd/hostapd.accept #deny_mac_file=/etc/hostapd/hostapd.deny#下面这两项是11n机能相关的,写不对会导致hostapd打不开,先不要写进去ieee80211n=1ht_capab=[HT40-][SHORT-GI-40][SHORT-GI-20][GF][SMPS-STATIC][RX-STBC1]

打开系统的IPv4转发功能

在终端机中输入sudo emacs /etc/sysctl.conf,直接在文件头写入以下内容:

net.ipv4.conf.all.forwarding=1net.ipv4.conf.default.rp_filter=1net.ipv4.ip_forward=1

然后退出回到终端机输入sudo sysctl -q让修改立即生效。

配置DHCP服务器实现内网IPv4地址自动分配

终端机中输入sudo emacs /etc/udhcpd.conf,你需要去掉下面这些行首的#来解除注释,然后按照自己的配置方法修改,注意要和interfaces文件里面wlan0的地址设定一致:

# Sample udhcpd configuration file (/etc/udhcpd.conf)# The start and end of the IP lease block #在这里设定你内网IP的起点与终点start 10.0.0.11 #default: 192.168.0.20 end 10.0.0.40 #default: 192.168.0.254 # The interface that udhcpd will use #这里改成内网链接使用的网卡,我们这里要改成wlan0interface wlan0 #default: eth0 # The maximim number of leases (includes addressesd reserved # by OFFER's, DECLINE's, and ARP conficts #建议改小点,太大当心爆炸~max_leases30 #default: 254 # If remaining is true (default), udhcpd will store the time # remaining for each lease in the udhcpd leases file. This is # for embedded systems that cannot keep time between reboots. # If you set remaining to no, the absolute time that the lease # expires at will be stored in the dhcpd.leases file.#这个随意,我没觉得有太大作用,可能是我从来不掉电的缘故,如果你的RPi老掉电就改成yes吧remainings no #default: yes############################中间有一堆被注释掉的内容############################Examles#DNS这里最多填两个optdns114.114.114.114 8.8.8.8#子网掩码option subnet 255.255.255.0#网关地址,就是你inerfaces里面给wlan0指定的IP地址optrouter 10.0.0.1#这个找一个内网地址段以外的地址填就好,也可以不配置optwins 10.0.0.10#神奇的功能,可以默认第三个DNS,如果你有这个需要可以再来一个(我是114+google+校园网内DNS,这样什么网站都可以正常解析)option dns222.201.130.30 # appened to above DNS servers for a total of 3 #接下来的是作用域和租期时间,不懂就不要改了option domain localoption lease 864000# 10 days of seconds

写一个bash脚本来一键打开无线路由机能,开机自动启动

在终端机输入emacs ~/router.sh,接下来写入:

sysctl -piptables -t nat -A POSTROUTING -o eth0 -j MASQUERADEiptables -A FORWARD -i eth0 -o wlan0 -m state --state RELATED,ESTABLISHED -j ACCEPTiptables -A FORWARD -i wlan0 -o eth0 -j ACCEPTservice udhcpd restart

保存,然后修改一下权限让他可以执行,输入sudo chmod 755 ~/router.sh就可以了。

接下来输入sudo emacs /etc/rc.local,在文件的末尾的exit 0这行上面,添加sudo /home/pi/router.sh保存退出。

重启测试

输入sudo reboot,不出意外的话重启之后你的设备可以正常连接你创建的无线网络上网了,802.11g,仅支持IPv4网络。

接下来会将IPv6和802.11n机能的开启,如果你有需要的话可以继续阅读。

配置IPv6实现无线双栈网络

查询外网IPv6地址和网关

Mac下可以插有线网络的话直接在设置里查看就可以了,Windows的话,命令行中输入ipconfig -all,如果你直接在RPi上看,终端机输入ifconfig就够了。纪录2001开头的IPv6地址和网关地址(我这里是fe80开头,有的情况下是2001开头,这个没关系,网关显示什么就记下来什么),Windows下IPv6地址后面可能会显示/64这个是prefix长度,这个也要记下来。

修改sysctl.conf打开IPv6转发

终端机中输入sudo emacs /etc/sysctl.conf,然后添加:

net.ipv6.conf.all.proxy_ndp=1net.ipv6.conf.all.forwarding=1

改写router.sh,加入IPv6配置信息

在终端机输入emacs ~/router.sh,接下来写入:

ip -6 addr del 2001:250:3000:3cc6:ba27:ebff:fee6:ce6c/64 dev eth0ip -6 addr add 2001:250:3000:3cc6:ba27:ebff:fee6:ce6c/126 dev eth0ip -6 addr add 2001:250:3000:3cc6:1::/80 dev wlan0ip -6 route add default via fe80::204:96ff:fe20:add6 dev eth0 metric 256ip -6 route add 2001:250:3000:3cc6:1::/80 dev wlan0service radvd restartservice npd6 restart

注意,上面这些东西加在service这行前面,上面这5行中,2001:250:3000:3cc6:ba27:ebff:fee6:ce6c是我查到的公网IPv6地址,这个要原样打进去,/64和/126也不能变(除非你的网络非常特殊,64可能要改成你查到的perfix长度,126是不能动的,防止错乱),接下来2001:250:3000:3cc6:1::/80这个是你内网用的IPv6地址,前面四段要和你查到的公网地址保持一致,后面的那一段是你的内网网段,填1就可以了(当然你换什么填什么),接下来那个fe80开头的地址换成你查到的网关地址,这样就搞定了。

配置radvd

radvd的作用是使内网客户端能自动获取IPv6地址。终端机中输入sudo emacs /etc/radvd.conf然后写入:

interface wlan0{AdvSendAdvert on;AdvManagedFlag on;AdvOtherConfigFlag on;prefix 2001:250:3000:3cc6:1::/80 {AdvRouterAddr off;AdvOnLink on;AdvAutonomous on;};};

注意上面的2001开头地址要换成你刚才设定好的内网地址。

配置npd6

npd6是邻居发现代理,是让内网客户端可以自动获取IPv6地址的。终端机中输入sudo emacs /etc/npd6.conf然后写入:

prefix=2001:250:3000:3cc6:1:interface = eth0ralogging = offlisttype = nonelistlogging = offcollectTargets = 100linkOption = falseignoreLocal = truerouterNA = truemaxHops = 255pollErrorLimit = 20

原文件中可能会用好多注释,不过没关系你只要对照上面把有效部分修改好就行了,注意2001开头那个地址要填你自己的

配置DHCPv6

DHCP是啥不用我多说了吧。终端机中输入sudo emacs /etc/wide-dhcpv6/dhcp6s.conf然后写入:

interface wlan0{address-pool pool1 86400;};pool pool1 {range 2001:250:3000:3cc6:1::200 to 2001:250:3000:3cc6:1::300;};

重启测试

sudo reboot,IPv6配置至此结束,没有异常的话在这里应该可以看到下图了吧。

配置802.11n机能

检查无线网卡支持哪些802.11n机能

在终端机中输入iw list | less之后你应该会看到一堆,我们只关注开头Capabilities的这部分,一般长得像这样:

Wiphy phy0Band 1:Capabilities: 0x172HT20/HT40Static SM Power SaveRX GreenfieldRX HT20 SGIRX HT40 SGIRX STBC 1-streamMax AMSDU length: 3839 bytesNo DSSS/CCK HT40Maximum RX AMPDU length 65535 bytes (exponent: 0x003)Minimum RX AMPDU time spacing: 2 usec (0x04)HT RX MCS rate indexes supported: 0-7, 32TX unequal modulation not supportedHT TX Max spatial streams: 1HT TX MCS rate indexes supported may differ

你要把这一段记下来,之后按q键结束。

修改hostapd配置文件增加802.11n机能支持

sudo emacs /etc/hostapd/hostapd.conf然后修改两行:

ieee80211n=1ht_capab=[HT40-][SHORT-GI-40][SHORT-GI-20][GF][SMPS-STATIC][RX-STBC1]

你去要重点关照的就是ht_capab这一行,这一行输入的越全面,你得到的速度就越快。

我在下面引用了官方文档的配置802.11n的部分,你可以对照这个和记好的iw list数据决定填什么,填好之后保存。sudo service hostapd reboot如果提示成功,说明配置没有问题,这个时候应该可以享受到高速度了,如果FAIL了,请仔细检查ht_capab是不是填写错了,如果实在不行,就删掉不确定的项,牺牲一些速度吧。

hostapd详细的配置方法可以参照官方文档,其实hostapd可以支持802.11ac的,但是问题在于USB2.0的传输速率使得在RPi上用ac没有意义。

##### IEEE 802.11n related configuration ####################################### ieee80211n: Whether IEEE 802.11n (HT) is enabled# 0 = disabled (default)# 1 = enabled# Note: You will also need to enable WMM for full HT functionality.#ieee80211n=1# ht_capab: HT capabilities (list of flags)# LDPC coding capability: [LDPC] = supported# Supported channel width set: [HT40-] = both 20 MHz and 40 MHz with secondary#channel below the primary channel; [HT40+] = both 20 MHz and 40 MHz#with secondary channel above the primary channel#(20 MHz only if neither is set)#Note: There are limits on which channels can be used with HT40- and#HT40+. Following table shows the channels that may be available for#HT40- and HT40+ use per IEEE 802.11n Annex J:#freqHT40-HT40+#2.4 GHz5-131-7 (1-9 in Europe/Japan)#5 GHz40,48,56,6436,44,52,60#(depending on the location, not all of these channels may be available#for use)#Please note that 40 MHz channels may switch their primary and secondary#channels if needed or creation of 40 MHz channel maybe rejected based#on overlapping BSSes. These changes are done automatically when hostapd#is setting up the 40 MHz channel.# Spatial Multiplexing (SM) Power Save: [SMPS-STATIC] or [SMPS-DYNAMIC]#(SMPS disabled if neither is set)# HT-greenfield: [GF] (disabled if not set)# Short GI for 20 MHz: [SHORT-GI-20] (disabled if not set)# Short GI for 40 MHz: [SHORT-GI-40] (disabled if not set)# Tx STBC: [TX-STBC] (disabled if not set)# Rx STBC: [RX-STBC1] (one spatial stream), [RX-STBC12] (one or two spatial#streams), or [RX-STBC123] (one, two, or three spatial streams); Rx STBC#disabled if none of these set# HT-delayed Block Ack: [DELAYED-BA] (disabled if not set)# Maximum A-MSDU length: [MAX-AMSDU-7935] for 7935 octets (3839 octets if not#set)# DSSS/CCK Mode in 40 MHz: [DSSS_CCK-40] = allowed (not allowed if not set)# 40 MHz intolerant [40-INTOLERANT] (not advertised if not set)# L-SIG TXOP protection support: [LSIG-TXOP-PROT] (disabled if not set)#ht_capab=[HT40-][SHORT-GI-20][SHORT-GI-40]# Require stations to support HT PHY (reject association if they do not)#require_ht=1# If set non-zero, require stations to perform scans of overlapping# channels to test for stations which would be affected by 40 MHz traffic.# This parameter sets the interval in seconds between these scans. Setting this# to non-zero allows 2.4 GHz band AP to move dynamically to a 40 MHz channel if# no co-existence issues with neighboring devices are found.#obss_interval=0

收工

至此本文的目的已经达到,have fun!

本文系hahaschool原创,转载必须注明出处。

Posted on March 22, Author hahaschoolCategories Linux, Raspberry PiTags Linux, RPi

Leave a Reply

Post navigation

Previous Previous post: CodeForces 484B Maximum Value Next Next post: CodeForces 347C Alice and Bob Proudly powered by WordPress

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。