openwrt网络设置

来源:互联网 发布:ubuntu设置中文失败 编辑:程序博客网 时间:2024/05/18 13:27

OpenWrt的网络配置文件是/etc/config/network,它负责交换芯片VLAN网络接口路由的配置。

此文件在编辑和保存之后需要执行

/etc/init.d/network reload
命令.在变更生效前,停止和重启网络,目的是为了避免重启路由器. * https://dev.openwrt.org/browser/branches/attitude_adjustment/package/base-files/files/etc/config/network

  • https://dev.openwrt.org/browser/trunk/package/base-files/files/etc/config/network

可以参考 netifd

下面是网络配置需要定义的节类型。通常情况下,一台路由器的最简网络配置包括至少2个interfaces(lanwan),如果硬件支持,还包括一个switch

switch

switch节负责交换芯片VLAN的划分。在OpenWrt系统内部,每个VLAN都会有一个独立的interface与它对应,即便它们实际上属于同一个硬件。需要注意的是,不是所有被OpenWrt系统支持的设备都含有可编程的交换芯片,因此这个节在某些平台上不会出现。

现在有2个不同的配置格式在使用,一个是/proc/switch/的API,另一个是新的基于swconfig的switch架构。

/proc/switch

这个变种只出现在Broadcom设备上,比如WRT54GL。

一个典型的配置就像这样:

config 'switch' 'eth0'        option 'vlan0' '0 1 2 3 5*'        option 'vlan1' '4 5'

其中,eth0标识符指明这个节对应的交换芯片。VLAN的定义方法是显然的,一般来说,0、1、2、3是路由器LAN口,4是路由器WAN口,5表示CPU,而5*表示这个接口是trunk。更多的信息请参考 switch documentation。

swconfig

新的swconfig框架将取代老式switch配置。现在它正被一些设备如D-Link DIR-300使用。

基于Swconfig的配置是一种全新的格式,即每个VLAN都使用一个节。下面的例子说明了运行在D-Link DIR-300设备上的Kamikaze 8.09分支所使用的标准配置:

config 'switch' 'eth0'        option 'reset' '1'        option 'enable_vlan' '1'config 'switch_vlan' 'eth0_1'        option 'device' 'eth0'        option 'vlan' '1'        option 'ports' '0 1 2 3 5t'config 'switch_vlan' 'eth0_2'        option 'device' 'eth0'        option 'vlan' '2'        option 'ports' '4 5t'

通用的属性仍然在switch节定义,各VLAN相关属性定义在各自的switch_vlan节中,它们通过指定device属性使系统得悉它使用的交换芯片。完整的格式请参看switch documentation。

Interfaces

interface类型的节声明了逻辑网络接口,可以为这些接口指定IP地址、别名、物理网络接口名称、路由规则及防火墙规则。

一个最简的interface节包含以下行:

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

  • wan 是唯一的 逻辑网络接口名称
  • dhcp 指定接口协议,这里是dhcp
  • eth0.1 是相关联的物理网络接口名称(用ifconfig可看到)

接口协议可以是以下之一:

协议描述static静态ipdhcp动态ippppPPP 协议 - 拨号MODEM连接pppoe以太网上的PPP协议 - DSL宽带连接pppoaATM上的PPP协议 - 使用内建MODEM的DSL连接3g使用3G上网卡的CDMA, UMTS 或 GPRS 连接pptp通过PPtP VPN的连接none不指定任何协议

针对指定协议的类型,还需要额外的选项,参看下表。在Required字段标记"yes"的选项必须在interface节中定义,标记"no"选项的可以省略。

所有协议类型的有效选项

名称类型必需性默认描述ifnameinterface name(s)yes(*)(none)物理接口名称。如果type选项被设为bridge,需要指定一个接口列表.
(*) This option may be empty or missing if only a wireless interface references this networktypestringno(none)如果设置为"bridge",将建立一个包含ifname所述接口的网桥stpbooleanno0启用生成树协议,只对网桥有效macaddrmac addressno(none)指定接口MAC地址mtunumberno(none)指定接口最大传输单元(MTU)autobooleanno0 for proto none, else 1指定是否在引导成功后打开这个接口

"static"协议

名称类型必需性默认描述ipaddrip addressyes, if no ip6addr is set(none)IP地址netmasknetmaskyes, if no ip6addr is set(none)子网掩码gatewayip addressno(none)默认路由bcastip addressno(none)广播地址 (不设置会自动生成)ip6addripv6 addressyes, if no ipaddr is set(none)为接口指派给定的IPv6 地址 (CIDR notation)ip6gwipv6 addressno(none)为接口指派给定的IPv6默认网关dnslist of ip addressesno(none)DNS服务器(1个或多个)

"dhcp"协议

名称类型必需性默认描述gatewaystringno(none)如果设置为0.0.0.0,将设置DHCP协议获取的默认网关dnslist of ip addressesno(none)指定DNS服务器(1个或多个)

"ppp" (Modem上的PPP)协议

:!: 要使用PPP,必须安装ppp软件包。

名称类型必需性默认描述devicefile pathyes(none)MODEM设备节点usernamestringno(?)(none)用于PAP/CHAP认证的用户名passwordstringno(?)(none)用于PAP/CHAP authentication的密码connectfile pathno(none)自定义PPP连接脚本路径disconnectfile pathno(none)自定义PPP断开连接脚本路径keepalivenumberno(none)Number of connection failures before reconnectdemandnumberno(none)Number of seconds to wait before closing the connection due to inactivitydefaultroutebooleanno1Replace existing default route on PPP connectpeerdnsbooleanno1Use peer-assigned DNS server(s)dnslist of ip addressesno(none)Override peer-assigned DNS server(s)ipv6booleanno0为PPP连接启用IPv6pppd_optionsstringno(none)传递给pppd守护进程的额外命令行参数

"pppoe" (以太网的PPP)协议

:!: 要使用PPPoE,必须安装ppp-mod-pppoe软件包.

名称类型必需性默认描述usernamestringno(?)(none)用于PAP/CHAP认证的用户名passwordstringno(?)(none)用于PAP/CHAP认证的密码connectfile pathno(none)自定义PPP连接脚本路径disconnectfile pathno(none)自定义PPP断开连接脚本路径keepalivenumberno(none)Number of connection failures before reconnectdemandnumberno(none)Number of seconds to wait before closing the connection due to inactivitydefaultroutebooleanno1Replace existing default route on PPP connectpeerdnsbooleanno1Use peer-assigned DNS server(s)dnslist of ip addressesno(none)Override peer-assigned DNS server(s)ipv6booleanno0为PPP连接启用IPv6pppd_optionsstringno(none)传递给pppd守护进程的额外命令行参数

"pppoa" (ATM上的PPP)协议

:!: 要使用PPPoA,必须安装 ppp-mod-pppoa 软件包.

名称类型必需性默认描述unitnumberyes(none)br2684 interface numbervcinumberno35PPPoA VCIvpinumberno8PPPoA VPIatmdevnumberno(none)ATM PVC numberencapsstringnollcPPPoA encapsulation mode: 'llc' (LLC) or 'vc' (VC)payloadstringnobridgedPPPoA forwarding mode: 'routed' or 'bridged'usernamestringno(?)(none)用于PAP/CHAP认证的用户名passwordstringno(?)(none)用于PAP/CHAP认证的密码connectfile pathno(none)自定义PPP连接脚本路径disconnectfile pathno(none)自定义PPP断开连接脚本路径keepalivenumberno(none)Number of connection failures before reconnectdemandnumberno(none)Number of seconds to wait before closing the connection due to inactivitydefaultroutebooleanno1Replace existing default route on PPP connectpeerdnsbooleanno1Use peer-assigned DNS server(s)dnslist of ip addressesno(none)Override peer-assigned DNS server(s)ipv6booleanno0为PPP连接启用IPv6pppd_optionsstringno(none)传递给pppd守护进程的额外命令行参数

"3g" (PPP over EV-DO, CDMA, UMTS or GRPS)协议

:!: 要使用3G,必须安装 comgt 软件包.

名称类型必需性默认描述devicefile pathyes(none)Modem设备节点servicestringyesgprs3G设备类型: evdocdmaumts 或 gprsapnstringyes(none)Used APNpincodenumberno(none)解锁SIM卡的PIN码maxwaitnumberno20等待Modem就绪的时间(秒)usernamestringno(?)(none)用于PAP/CHAP认证的用户名passwordstringno(?)(none)用于PAP/CHAP认证的密码keepalivenumberno(none)Number of connection failures before reconnectdemandnumberno(none)Number of seconds to wait before closing the connection due to inactivitydefaultroutebooleanno1Replace existing default route on PPP connectpeerdnsbooleanno1Use peer-assigned DNS server(s)dnslist of ip addressesno(none)Override peer-assigned DNS server(s)ipv6booleanno0为PPP连接启用IPv6

"pptp" (Point-to-Point Tunneling Protocol)协议

:!: 要使用PPtP,必须安装 pptp 软件包.

名称类型必需性默认默认serverip addressyes(none)远程PPtP服务器ipprotostringnodhcp建立PPtP隧道前用来获取IP连通性的协议usernamestringno(?)(none)用于PAP/CHAP认证的用户名passwordstringno(?)(none)用于PAP/CHAP认证的密码Additionally all options defined for the corresponding ipproto can be specified

别名

Alias sections can be used to define further IPv4 and IPv6 addresses for interfaces. They also allow combinations like DHCP on the main interface and a static IPv6 address in the alias, for example to deploy IPv6 on wan while keeping normal internet connectivity. Each interface can have multiple aliases attached to it.

别名最小的声明包含以下行:

config 'alias'        option 'interface' 'lan'        option 'proto' 'static'        option 'ipaddr' '10.0.0.1'        option 'netmask' '255.255.255.0'

  • lan is the logical interface name of the parent interface
  • static is the alias interface protocol
  • 10.0.0.1 specifies the alias ip address
  • 255.255.255.0 specifies the alias netmask

At the time of writing, only the static protocol type is allowed for aliases. Defined options for alias sections are listed below.

名称类型必需性默认描述interfacestringyes(none)Specifies the logical interface name of the parent (or master) interface this alias is belonging to, must refer to one of the defined interface sectionsprotostringyes(none)Specifies the alias interface protocolipaddrip addressyes, if no ip6addris set(none)IP addressnetmasknetmaskyes, if no ip6addris set(none)Netmaskgatewayip addressno(none)Default gatewaybcastip addressno(none)Broadcast address (autogenerated if not set)ip6addripv6 addressyes, if no ipaddris set(none)IPv6 address (CIDR notation)ip6gwipv6 addressno(none)IPv6 default gatewaydnslist of ip addressesno(none)DNS server(s)

IPv4 Routes

It is possible to define arbitary IPv4 routes on specific interfaces using route sections. As for aliases, multiple sections can be attached to an interface.

一个minimial的例子类似如下:

config 'route'        option 'interface' 'lan'        option 'target' '172.16.123.0'        option 'netmask' '255.255.255.0'

  • lan is the logical interface name of the parent interface
  • 172.16.123.0 is the network address of the route
  • 255.255.255.0 specifies the route netmask

Legal options for IPv4 routes are:

名称类型必需默认描述interfacestringyes(none)Specifies the logical interface name of the parent (or master) interface this route is belonging to, must refer to one of the defined interface sectionstargetip addressyes(none)Network addressnetmasknetmaskno(none)Route netmask. If ommitted, 255.255.255.255 is assumed which makes target a host addressgatewayip addressno(none)Network gateway. If ommitted, the gateway from the parent interface is taken, if set to 0.0.0.0 no gateway will be specified for the routemetricnumberno0Specifies the route metric to use

IPv6路由

IPv6 routes可指定定义一个或多个route6的sections.

一个minimial的例子类似如下:

config 'route6'        option 'interface' 'lan'        option 'target' '2001:0DB8:100:F00:BA3::1/64'        option 'gateway' '2001:0DB8:99::1'

  • lan is the logical interface name of the parent interface
  • 2001:0DB8:100:F00:BA3::1/64 is the routed IPv6 subnet in CIDR notation
  • 2001:0DB8:99::1 specifies the IPv6 gateway for this route

Legal options for IPv6 routes are:

名称类型必要默认描述interfacestringyes(none)Specifies the logical interface name of the parent (or master) interface this route is belonging to, must refer to one of the defined interface sectionstargetipv6 addressyes(none)IPv6 network addressgatewayipv6 addressno(none)IPv6 gateway. If ommitted, the gateway from the parent interface is takenmetricnumberno0Specifies the route metric to use

例子

下面是特殊的,非标准接口配置的几个例子。

Bridge without IP

config 'interface' 'example'        option 'type'    'bridge'        option 'proto'   'none'        option 'ifname'  'eth0 eth1'        option 'auto'    '1'

DHCP without default gateway

config 'interface' 'example'        option 'proto'   'dhcp'        option 'ifname'  'eth0'        option 'gateway' '0.0.0.0'

DHCP及IPv6

config 'interface' 'example'        option 'proto'     'dhcp'        option 'ifname'    'eth0'config 'alias'        option 'interface' 'example'        option 'proto'     'static'        option 'ip6addr'   '2001:0DB8:100:F00:BA3::1'

静态IP配置和默认网关与非零十进制

config 'interface' 'example'        option 'proto'     'static'        option 'ifname'    'eth0'        option 'ipaddr'    '192.168.1.200'        option 'netmask'   '255.255.255.0'        option 'dns'       '192.168.1.1'config 'route'        option 'interface' 'example'        option 'target'    '0.0.0.0'        option 'netmask'   '0.0.0.0'        option 'gateway'   '192.168.1.1'        option 'metric'    '100'

PPtP-over-PPPoE internet connection

config 'interface' 'wan'
        option 'proto'     'pppoe'
        option 'ifname'    'eth1'
        option 'username'  'user'
        option 'password'  'pass'
        option 'timeout'   '10'


config 'interface' 'vpn'
        option 'proto'     'pptp'
        option 'ifname'    'vpn'
        option 'username'  'vpnuser'
        option 'password'  'vpnpass'
        option 'server'    'vpn.example.org' 


:!: Additionally the "wan" firewall zone must include both interfaces in /etc/config/firewall:


config 'zone'
        option 'name'      'wan'
        option 'network'   'wan vpn'
        option 'input'     'REJECT'
        option 'forward'   'REJECT'
        option 'output'    'ACCEPT'
        option 'masq'      '1' 

http://wiki.openwrt.org/zh-cn/doc/uci/network


0 0