openwrt入门学习手册

内容中必然出现一些文字错误和翻译错误,有一点英文基础的童鞋最好配合原文阅读;英文基础较好的童鞋更建议直接阅读原文。
原文地址
转载地址

正文:

          **

openwrt入门学习

**

目录

  1. 1 路由器

    1.1 启动

    1.1.1 安装

    1.1.2 初始化配置

    1.1.3 故障保护模式

    1.2 配置OpenWrt

    1.2.1 网络

    1.2.2 无线

    1.3 高级配置

    1.3.1 热插拔

    1.3.2 初始化脚本

    2 开发

    2.1 构建系统

    2.1.1 构建一个镜像

    2.1.2 创建包

    2.1.3 创建内核模块包

    2.1.4 惯例

    2.1.5 故障排除

    2.1.6 使用构建环境

    2.2 附加工具

    2.2.1 镜像生成器

    2.2.2 SDK

    2.3 添加支持平台

    2.3.1 设备支持哪个操作系统?

    2.3.2 查找和使用厂商SDK

    2.4 调试和debricking

    2.4.1 添加串行端口

    2.4.2 JTAG

    2.5 错误报告

    2.5.1 使用Trac 标签系统

    2.6 上传补丁

    2.6.1 如何贡献上传

    2.6.2 何处聆听和讨论

    2.6.3 补丁提交进程

第一章

路由器
1.1 启动
1.1.1 安装
1.1.2 初始化配置
1.1.3 故障保护模式
1.2 配置OpenWrt
1.2.1 网络

Kamikaze的网络配置存储在/etc/config/network 文件下分多个接口配置。每个接口配置都直接映射到一个网络/无线接口(eth0,wl0,···)或者桥接多个接口。格式类似如下形式:

config interface “lan”

option ifname “eth0”

option proto “static”

option ipaddr “192.168.1.1”

option netmask “255.255.255.0”

option gateway “192.168.1.254”

option dns “192.168.1.254”

Ifname 定义了Linux的接口名,如果你想要用来桥接一个或多个接口,需要添加ifname到一个接口列表并添加如下语句:

option type “bridge”

如果使用VLAN对接口进行标记,则只需要将各个接口的VLAN ID添加到列表里,e.g. th0.1 ,则它们同样能实现互相联通。

以上简单地对eth0的源指定协议进行了简单的静态配置。默认的镜像通常使用”none”,”static”,”dhcp”和”pppoe”。其他协议可以通过安装另外的包进行协议的扩充。

当使用“static”方式时就像以上例子一样,IP地址(ipaddr)和掩码(netmask)都是强制性的而网关和DNS则是可选择的,你可以定义多个网关并分配不同的可用区间。

DHCP目前仅支持ipaddr(服务器分配IP地址)和hostname(作为客户端主机名识别)——这两者都是可选择的。

PPP所基于的协议支持如下操作:

·用户名

PPP用户名(通常带有PAP认证)

·密码

PPP密码

·检测死连接

用LCP来ping PPP服务器。其返回值定义了重连接前ping失败的最大次数限制, 间隔默认为5次,但可以通过添加”,<间隔数>”来改变检测死连接的值。

·需求

使用Dial on Demand(其值指定了最大空闲时间)

·服务器(PPTP)

远程服务器IP地址

所有的协议都可以使用MTU操作来定义定义MTU。

设置静态路线

你可以通过给特殊接口设置静态线路来使其在配置完成后被启动。可以像如下例子一样进行添加配置节点:

config route foo

option interface lan

option target 1.1.1.0

option netmask 255.255.255.0

option gateway 192.168.1.1

线路节点的名字是可操作的,interface、target和gateway则是强制不可操作的;如果缺少netmask则会使线路在主机路由中循环。

设置交换机(目前仅支持博通)

交换机配置是通过添加一个“switch”配置节点来进行配置的。

举例如下:

config switch “eth0”

option vlan0 “1 2 3 4 5*”

option vlan1 “0 5”

在博通的硬件设备要求节点名称必须为“eth0”,否则交换机驱动将无法侦测到任何物理设备。每个vlan操作必须有其名称为vlan,其中为交换机驱动的vlan数字。其值带有如下所列出的后缀:

·‘ * ’:将当前VLAN设置为默认VLAN(PVID)

·‘ u ’:强制端口不标记

·‘ t ’:强制端口标记

CPU端口默认为标记,其它所有端口则默认为不标记。在博通的硬件设备上CPU端口总是5,其它端口则根据不同的硬件设备而有所变化。

例如,你想要有3个vlan,分配成一个3端口交换机,1端口给隔离区(DMZ),一端口给WAN接口,则使用以下配置命令:

config switch “eth0”

option vlan0 “1 2 3 5*”

option vlan1 “0 5”

option vlan2 “4 5”

有三个接口将会因为这个开关布局而被自动创建:eth0.0(vlan0)、eth0.1(vlan1)、eth0.2(vlan2)。之后你可以对这些接口自行命名给一个通用配置名称,诸如“lan”“wan”“dmz”等等。

IPv6连接设置

OpenWrt上的IPv6连接支持使用PPP、隧道代理和静态分配。

如果使用的是PPP,则IPv6将会被设置成IP6CP且不需要进行任何配置操作。

要设置IPv6隧道给一个隧道代理的话,你可以安装一个叫做“6scripts”的包并编辑地址为“/etc/config/6tunnel”的文件做如下设置更改:

config 6tunnel

option tnlifname ‘sixbone’

option remoteip4 ‘1.0.0.1’

option localip4 ‘1.0.0.2’

option localip6 ‘2001::DEAD::BEEF::1’

option prefix ‘/64’

·tnlifname :在IPv4隧道设置好IPv6的接口名称

·remoteip4 :通过6in4 隧道建立的远程终端IP地址,该地址由隧道代理提供

·localip4 :建立6in4隧道的路由器IP地址,它通常对应你的WAN口IP地址

·localip6 :由隧道代理提供的隧道另一边的IPv6地址

·prefix :LAN口设置的IPv6前缀

用同一个包你也可以设置一个IPv6桥接:

config 6bridge

option bridge ‘br6’

默认的脚本桥接了WAN口和LAN口并用以太网防火墙(ebtables)滤掉了一切非IPv6部分的东西。

IPv6静态分配的前缀也支持和IPv4相似的设置形式(当IPv6静态分配可用时):

config interface “lan”

option ifname “eth0”

option proto “static”

option ip6addr “fe80::200:ff:fe00:0/64”

option ip6gw “2001::DEAF:BEF:1”

1.2.2 无线

WiFi配置文件所在地址为:/etc/config/wireless(目前支持Broadcom、Atheros和mac80211)。当首次启动路由器的时候,路由器需要侦测到你的网卡并创建一个简单的配置文件,即默认“optionnetworklan”已经写入,如此可以阻止通过无线接口的不安全网络共享连接。

每个无线驱动拥有其唯一的配置脚本指定了驱动的特殊操作和配置,位置在“/lib/wifi/driver_name.sh”。这个脚本也叫做驱动的特殊二进制文件,就如Broadcom的wlc或者Atheros的hostapd和wpa_supplicant。使用这样的结构是因为其摘自驱动配置内容。

Broadcom的通用无线配置:

configwifi-device“wl0”

optiontype“broadcom”

optionchannel“5”

configwifi-iface

option device“wl0”

optionnetworklan

optionmode“ap”

optionssid“OpenWrt”

optionhidden“0”

optionencryption“none”

Atheros的通用无线配置:

configwifi-device“wifi0”

optiontype“atheros”

optionchannel“5”

optionhwmode“11g”

configwifi-iface

option device“wifi0”

optionnetworklan

optionmode“ap”

optionssid“OpenWrt”

optionhidden“0”

optionencryption“none”

mac80211的通用无线配置:

configwifi-device“wifi0”

optiontype“mac80211”

optionchannel“5”

configwifi-iface

option device“wlan0”

optionnetworklan

optionmode“ap”

optionssid“OpenWrt”

optionhidden“0”

optionencryption“none”

Atheros的多无线配置:

configwifi-devicewifi0

optiontypeatheros

optionchannel1

configwifi-iface

option devicewifi0

optionnetworklan

optionmodeap

optionssidOpenWrt_private

optionhidden0

optionencryptionnone

configwifi-devicewifi1

optiontypeatheros

optionchannel11

configwifi-iface

option devicewifi1

optionnetworklan

optionmodeap

optionssidOpenWrt_public

optionhidden1

optionencryptionnone

这个文件里有两个类型的配置节点,“wifi-device”映射物理wifi接口;“wifi-iface”配置则于其上虚拟接口(驱动支持的话)。

一个完整的无线配置文件包含:

configwifi-devicewifidevivename

optiontypebroadcom,atheros,mac80211

optioncountryus,uk,fr,de,etc.

optionchannel1-14

optionmaxassoc1-128(broadcom only)

optiondistance1-n

optionhwmode11b,11g,11a,11bg(atheros,mac80211)

optionrxantenna0,1,2(ahteros,mac80211)

optiontxantenna0,1,2(atheros,mac80211)

optiontxpowertransmission power in dBm

configwifi-iface

optionnetworkthe interface you want wifi to birdge with

optiondevicewifi0,wifi1,wifi2,wifiN

optionmodeap,ata,adhoc,monitor,or wds

optiontxpower(deprecated)transmissionpowerin dBm

optionssidssid name

optionbssidbssid address

optionencryptionnone, wep, psk, psk2, wpa, wpa2

optionkeyencryption key

optionkey1key1

optionkey2key2

optionkey3key3

optionkey4key4

optionserverip address

optionportport

optionhidden0,1

optionisolate0,1

optiondoth0,1(atheros, broadcom)

optionwmm0,1(atheros, broadcom)

Wifi-device上的操作:

·type:选择该接口的驱动。

·country:国家代码用来确定管理机构的设置。

·channel:无线信道(1-14)取决于国家的设定。

·maxassoc:可操作,为最大相关客户端数量,这个功能只支持Broadcom芯片组。

·distance:可操作,为ap到最远客户端之间的距离,这个功能仅支持Atheros芯片组。

·mode:即频带(b, g, bg, a);这个功能仅支持Atheros芯片组。

·diversity:可操作,开启多WiFi设备;这个功能仅支持Atheros芯片组。

·rxantenna:可操作,接收天线标识符;该功能仅Atheros和部分Broadcom芯片支持。

·txantenna:可操作,发送天线标识符;该功能进Atheros和部分Broadcom芯片支持。

·txpower设置了无线的传输功率大小,单位为dBm。

Wifi-iface上的操作:

·network:从“etc/config/network”中选择要用的接口节点。

·device:设置wifi设备名称

·mode:可配置为如下模式:

。ap:接入点模式

。sta:客户端模式

。adhoc:点对点模式

。monitor:监听模式

。wds:WDS 点对点连接

·ssid:设置wifi设备的SSID

·bssid:设置其他wds单元的mac地址

·txpower:(具体上限由wifi设备硬件决定)设置无线传输功率,单位为dBm

·encryption:加密设置,指出如下设定:

。none

。wep

。psk,psk2

。wpa,wpa2

· key, key1, key2, key3, key4 :(wep, wpa, psk)WEP密钥,WPA密钥或RADIUS密钥(WPA RADIUS模式)

·server(wpa):RADIUS服务器IP地址

·port(wpa):RADIUS服务器端口(默认为1812)

·hidden:0,不隐藏SSID; 1,影藏SSID

·isolate:可操作,隔离操作最为一种模式通常设置在热点上以限制客户端仅能通 过AP进行连接而无法连接其他无线设备。

0,关闭AP隔离(默认);

1,开启AP隔离.

·doth:可操作,切换802.11h模式。

0,关闭802.11h(默认)

1,开启802.11h

·wmm:可操作,切换802.11e模式。

0,关闭802.11e(默认)

1,开启802.11e

无线分布系统WDS:WDS是一种非标模式工作在两台Broadcom设备而无法在一个Broadcom设备和一台Atheros设备之间作用工作。

非加密WDS连接:以下例子用于展示如何设置好非加密WDS连接。可以确定BSSID应为“ca:fe:ba:be:00:01”,远程WDS终端为“ca:fe:ba:be:00:02”。

例:

configwifi-devicewl0

optiontyoe“broadcom”

optionchannel“5”

configwifi-iface

optiondevice“wl0”

optionnetworklan

optionmode“ap”

optionssid“OpenWrt”

optionhidden“0”

optionencryption“none”

configwifi-iface

optiondevice“wl0”

optionnetworklan

optionmodewds

optionssid“OpenWrt WDS”

optionbssid“ca:fe:ba:be:00:02”

加密WDS连接:对WDS连接加密也是可能实现的,psk,psk2,psk+psk2模式都支持这种连接,使用SES算法的预共享密钥配置如下举例所示:

例:

configwifi-devicewl0

optiontypebroadcom

optionchannel5

configwifi-iface

optiondevicewl0

optionnetworklan

optionmodeap

optionssidOpenWrt

optionencryptionpsk2

optionkey“”

configwifi-iface

optiondevicewl0

optionnetworklan

optionmode wds

optionbssidca:fe:ba:be:00:02

optionssidOpenWrt WDS

optionencryptionpsk2

optionkey“”

802.1x 配置:OpenWrt同时支持802.1x客户端和接入点的配置。802.1x客户端仅在Atheros和mac80211驱动上工作,其配置仅支持EAP的TLS、TTLS、PEAP类型。

EAP-TLS:

config wifi-iface

  option device         "ath0"

  option network        lan

  option ssid           OpenWrt

  option eap_type       tls

  option ca_cert        "/etc/config/certs/ca.crt"

  option priv_key       "/etc/config/certs/priv.crt"

  option priv_key_pwd   "PKCS#12 passphrase"

EAP-PEAP:

config wifi-iface

  option device         "ath0"

  option network        lan

  option sid           OpenWrt

  option eap_type       peap

  option ca_cert        "/etc/config/certs/ca.crt"

  option auth           MSCHAPV2

  option identity       username

  option password       password

局限性:模式的组合是有局限性的,仅以下模式支持组合使用:

·Broadcom:

·1x sta, 0-3x ap

·1-4x ap

·1x adhoc

·1x monitor

WDS连接仅被用于纯AP模式切无法使用WEP(当被主动设置为主接口时除外)。

·Atheros:

·1x sta, 0-Nx ap

·1-Nx ap

·1x adhoc

N是指模块允许的最大VAP数,默认为4,但可通过载入 maxvaps=N名来来改变值的大小。

添加一个新的驱动配置:目前仅支持对多线程的无线驱动进行配置:Broadcom、Atheros和mac80211,如果有兴趣可以自己尝试添加其它驱动的支持,例如Rlink RT2x00、Texas Instruments ACx100/111的驱动。

驱动特定脚本必须为“/lib/wifi/<驱动>.sh”,且必须包含以下函数:

·侦测当前驱动

·打开/关闭 wifi接口

·配置为可读可写可执行

·第三方程序调用(网络附属存储NAS,请求)

每个驱动脚本必须将驱动追加一个全局驱动变量:

appendDRIVER“driver name”

scan_:这个函数将会解析“/etc/config/wireless”并确认配置是否兼容,例如打开ad-hoc模式同时影藏SSID。如果你的驱动支持很多配置操作则会变得更加复杂。该函数不改变接口的实际状态。

例:

scan_dummy()

{

local device=”$1”

config_get vifs “$device” vifs

for vif in $vifs; do

# check config consistency for wifi-iface sections

done

# check mode combination

}

enable_:这个函数将会唤起wifi设备并任意创建应用特别配置文件,例如发起请求或WPA认证。

例:

enable_dummy()

{

local device=”$1”

config_get vifs “$device” vifs

for vif in $vifs; do

# bring up virtual interface belonging to

# the wifi-device “$device”

done

}

disable_:这个函数将会关闭wifi设备和它所有的虚拟接口(如果该设备支持的话)。

例:

disable_dummy()

{

local device=”$1”

# bring down virtual interfaces belonging to

# “$device” regardless of whether they are

# configured or not. Don’t rely on the vifs

# variable at this point

}

Detect_:这个函数会寻找驱动可用的接口,新设备配置节点模板需写入为标准输出,且在创在模板之前必须对所属接口的已存在配置节点进行检测。

例:

detect_dummy()

{

[ wifi-device = “$(config_get dummydev type)” ] && return 0

cat <

include

include

include

include

include

include

include

define WINDOW_ADDR 0x1FC00000 /* Real address of the flash */

define WINDOW_SIZE 0x400000 /* Size of flash */

define BUSWIDTH 2 /* Buswidth */

static void __exit device_mtd_cleanup(void);

static struct mtd_info *device_mtd_info;

static struct map_info devicd_map = {
.name = “device”,
.size = WINDOW_SIZE,
.bankwidth = BUSWIDTH,
.phys = WINDOW_ADDR,
};

static int __init device_mtd_init(void)
{
// Display that we found a flash map device
printk(“device: 0x\%08x at 0x\%08x\n”, WINDOW_SIZE, WINDOW_ADDR);
// Remap the device address to a kernel address
device_map.virt = ioremap(WINDOW_ADDR, WINDOW_SIZE);

   // If impossible to remap, exit with the EIO error  
   if (!device_map.virt) {  
           printk("device: Failed to ioremap\n");  
           return -EIO;  
   }  

// Initialize the device map
simple_map_init(&device_map);

/* MTD informations are closely linked to the flash map device
you might also use “jedec_probe” “amd_probe” or “intel_probe” */
device_mtd_info = do_map_probe(“cfi_probe”, &device_map);

if (device_mtd_info) {
device_mtd_info->owner = THIS_MODULE;

int parsed_nr_parts = 0;

// We try here to use the partition schema provided by the bootloader specific code
if (parsed_nr_parts == 0) {
int ret = parse_bootloader_partitions(device_mtd_info, &parsed_parts, 0);
if (ret > 0) {
part_type = “BootLoader”;
parsed_nr_parts = ret;
}
}

                   add_mtd_partitions(devicd_mtd_info, parsed_parts, parsed_nr_parts);  

                   return 0;  
           }  
   iounmap(device_map.virt);  

   return -ENXIO;  

}

// This function will make the driver clean up the MTD device mapping
static void __exit device_mtd_cleanup(void)
{
// If we found a MTD device before
if (device_mtd_info) {
// Delete every partitions
del_mtd_partitions(device_mtd_info);
// Delete the associated map
map_destroy(device_mtd_info);
}

// If the virtual address is already in use
if (device_map.virt) {
// Unmap the physical address to a kernel space address
iounmap(device_map.virt);
// Reset the structure field
device_map.virt = 0;
}
}

// Macros that indicate which function is called on loading/unloading the module
module_init(device_mtd_init);
module_exit(device_mtd_cleanup);

// Macros defining license and author, parameters can be defined here too.
MODULE_LICENSE(“GPL”);
MODULE_AUTHOR(“Me, myself and I [email protected]");

2.4 调试和debricjing
2.4.1 添加串行端口
2.4.2 JTAG
2.5 错误报告
2.5.1 使用Trac标签系统

OpenWrt作为一个开源软件,它开放了它的开发社区允许公众访问subversion库。Trac软件意味着一个subversion前端、一个wiki、一个面向开发者的报表系统,可以使用户和贡献者的开发进程变得更加简易和高效。

Trac系统将用户分为两类:

·开发者:可以报告、修改和关闭标签

·报告者:可以添加评说明、补丁或者请求标签状态

打开一个标签:

报告者可能会因为一下原因而需要打开标签:

·出现了一个bug影响了特定硬件的与或软件并需要对之进行修改

·出现一个特殊软件包需要在官方OpenWrt库中显示出来

·OpenWrt上需要添加或移除某个功能

视打开的标签种类的不同,补丁在以下情况下可以被接受:

·需要包含进OpenWrt的新包

·修复报告者用户模式下bug且位置是否具有影响时

·修改现有OpenWrt内的文件添加新功能

如果标签已经打开,则会受到开发者的关注,并打上开发者认可的名字,这时允许随时在标签中添加说明或者将标签关闭。

关闭一个标签:

标签会因为一下原因而关闭被开发者关闭:

·问题已修复

·问题无法判定为有效的

·开发者已经知道错误被上一级修复了

·错误重复

·问题不会转移和影响其他部分运作

同时标签也可能在bug不再被报告者触发或者发现bug本身没有意义时会被关闭。

如果一个标签由开发者打上了“已修复”的标记,则subversion子集中将会随着更正为已修复完bug的文件内容。
2.6 上传补丁
2.6.1 如何贡献

OpenWrt系统是在持续进步着的,这归功于无数人员的资源贡献,使这些资源能让广大爱好者能够获取得到。当你发现有一些修改有用,并利用各种方法将之整合到整个项目中,如此就能提升OpenWrt的品质,并可能会被收录保存在将来的OpenWrt官方版本中去。

本节主要介绍一种让人们能够上传补丁的行之有效的方法。

你需要反复重复以下步骤,这是至关重要的:

·聆听他人的想法

·解释你所遇到的问题和解决的方法

·编写有用的补丁和文档

·测试、测试再测试
2.6.2 何处聆听和讨论

·在谷歌上查找和你问题相关的资料

·邮件列表:http://lists.openwrt.org/

·wiki:查找wiki:http://wiki.openwrt.org/OpenWrtDocs

·论坛:http://forum.openwrt.org/

·IRC互联中继聊天:irc.freenode.net。Openwrt和openwrt-devel频道

·TRAC:https://dev.openwrt.org/ ,issue/bug/change 跟踪系统

最好要描述清楚之前你做的内容,过程文档往往能暴露出其中可能的问题,请保持书写到目前为止的开发文档。
2.6.3 补丁提交进程

1.使用git或svn创建补丁,手动使用“diff -urN”命令也可,但通常不需要这么做。

2.将以下内容整理成邮件发送给[email protected]

A.[PATCH]<简单描述一下>放置于主题处

B.(可选)在信息内容中详细描述补丁功能

C.署名(自己的邮箱地址)

D.附件附上补丁

3.阅读http://kerneltrap.org/Linux/Email_Clients_and_Patches 并找出确认邮箱客户端没有破坏了你的补丁文件的方法。

4.遵循Linux内核补丁上传指南上的要求在署名处使用真实的名字和邮箱地址。

Linux内核补丁上传指南:http://git.kernel.org/?p=linux/kernel/git/torvalds/linux-2.6.git;a=blob;f=Documentation/SubmittingPatches;h=681e2b36195c98ea5271b76383b3a574b190b04f;hb=HEAD

5.补丁上传范例:http://lists.openwrt.org/pipermail/openwrt-devel/2007-November/001334.html

猜你喜欢

转载自blog.csdn.net/sherry_unique/article/details/81476598