openwrt学习
Network-scripts.tex
Using the network scripts
为使用network,必须通过如下方式包含必须的shell脚本: . /etc/functions.sh # common functions
include /lib/network # include /lib/network/*.sh
scan_interfaces # read and parse the network config
有些协议,如PPP会在运行期间改变配置的接口名,如PPPOE会将eth0改为ppp0,因此要运行scan_interface,而不是从配置中直接读取。运行scan_interface后,ifname选项将包含有效的接口名(用于ip traffic),如果物理设备名与此不同,则会保存在device选项中。
这意味着在scan_interface后运行config_get_lan_ifname,获取的结果可能与运行之前不同。
Writing protocol handers
添加协议处理
可通过在/lib/network下添加shell脚本来定制协议处理,脚本包含: scan_
# change the interface names if necessary }
setup_interface_
Network.tex
网络接口配置
网络配置文件为/etc/config/network,被分隔为多个接口配置。每一个接口配置或直接就是ethernet/wifi接口(如eth0、wl0),或包含多个接口的桥接口。
如:
第 11 页 共 28 页
openwrt学习
config interface \ option ifname \ option proto \
option ipaddr \ option netmask \ option gateway \ option dns \
其中ifname指定linux接口名,如果使用bridge接口则将ifname设置为接口列表,同时添加option type “bridge”。
可通过添加vlanid来支持vlan功能如eth0.1,具体参照switch一节。
此配置是一个简单的eth0静态配置,proto指定了接口使用的协议,支持none、static、dhcp,可通过install额外的包来支持其他的协议。
Static
当为static时,ipaddr与netmask必填,而gateway与dns可选,支持多个DNS服务器,中间以空格隔开即可,如:option dns \
如:
config interface \ option ifname \ option proto \ ...
option dns \
DHCP
DHCP当前仅接受ipaddr与hostname选项。其中ipaddr指定了dhcp server的地址,而hostname指定了客户的的hostname标识,两者均可选。
config interface \ option ifname \ option proto \
option ipaddr \ option hostname \
PPPOE
PPP基础的协议,如PPPOE、PPTP支持下列选项: Username:PPP用户名,用于PAP认证 Password:PPP密码
Keepalived:使用LCP ping PPP服务器,保活值,缺省间隔为5. Demand:按需拨友,值指定了最大空闲时间。 Server:对端PPTP服务器IP地址
第 12 页 共 28 页
openwrt学习
MTU设置
对所有协议,均可指定MTU,通过设置mtu选项即可,如: config interface \ option ifname \ option proto \ option username \ option password \
option mtu 1492 (optional)
设置静态路由
Setting up static routes
可对指定的接口添加静态路由,在接口配置后会自动添加,举例配置: config route foo option interface lan option target 1.1.1.0 option netmask 255.255.255.0 option gateway 192.168.1.1
其中route节的名称可选,interface、target、gateway选项是必须的。如果netmsk不填,则默认为主机路由。
设置交换机
Setting up the switch (currently broadcom only) 设置交换机,当前仅broadcom支持。 略
IPV6设置
Setting up IPv6 connectivity 略
第 13 页 共 28 页
openwrt学习
Init_script.tex
功能脚本
由于openwrt使用自己的初始script系统,所有的init script必须使用/etc/rc.common作为wrapper安装在/etc/init.d/
如/etc/init.d/httpd: #!/bin/sh /etc/rc.common
# Copyright (C) 2006 OpenWrt.org
START=50 start() {
[ -d /www ] && httpd -p 80 -h /www -r OpenWrt }
stop() {
killall httpd }
从上可以看出,script本身并不解析命令行参数,而是由/etc/rc.common来完成。 Start和stop是最基本的功能,几乎所有init script都要提供。Start是在运行/etc/init.d/httpd start时来调用,可以是系统启动时,或用户手动运行此脚本时。
通过/etc/init.d/
脚本运行的顺序是在初始脚本中通过变量START来定义,改变后需要再次运行/etc/init.d/
/etc/rc.common说明
#!/bin/sh # Copyright (C) 2006-2011 OpenWrt.org
. $IPKG_INSTROOT/lib/functions.sh . $IPKG_INSTROOT/lib/functions/service.sh initscript=$1 action=${2:-help} shift 2
start() {
return 0
第 14 页 共 28 页
openwrt学习
} stop() { return 0 } reload() {
return 1 }
restart() { trap '' TERM stop \ start \ } boot() { start \
} shutdown() {
stop } disable() { 禁止服务开启 name=\ rm -f \ rm -f \ }
enable() { 开启服务 name=\ disable [ -n \ echo \ return 1 } [ \] && ln -s \\
[ \ ] && ln -s \\
}
enabled() { name=\
第 15 页 共 28 页