2008年9月4日星期四

Linux 下的无线网络配置(一)

到现在我使用的无线网卡仅仅局限在 D-Link 的 DWL-G650,这使用的是 atheros 芯片,驱动是 madwifi 提供的,不过有的无线网卡现在的内核已经直接可以驱动了,使用的某些工具和本文类似。不过值得注意的是,将来可能会使用 ath5k 或者 ath9k 的驱动,而不是用 madwifi + hal 的构架了。

相关的包有:
  • madwifi-source 提供 madwifi 的源代码。
  • madwifi-tools 提供一些关于 atheros 芯片的一些程序。
  • wireless-tools 提供配置无线网络的基本命令。
  • wpasupplicant 为无线网络提供 WPA 支持。
  • dhcp3-client 提供 DHCP 配置网络功能。
通过 debian 的 module assistant 安装 madwifi 提供的内核模块,这样就可以避免后期的维护工作,这可以通过
# m-a prepare madwifi
# m-a a-i madwifi
完成。这样就会把 ath_* 等内核模块加载。然后可以用 madwifi-tools 工具创建几个设备。该包主要提供了
/usr/sbin/athstats
/usr/sbin/ath_info
/usr/sbin/80211debug
/usr/sbin/athdebug
/usr/sbin/athkey
/usr/sbin/athchans
/usr/sbin/80211stats
/usr/sbin/athctrl
/usr/share/man/man8/ath_info.8.gz
/usr/share/man/man8/wlanconfig.8.gz
/usr/share/man/man8/80211stats.8.gz
/usr/share/man/man8/athkey.8.gz
/usr/share/man/man8/80211debug.8.gz
/usr/share/man/man8/athctrl.8.gz
/usr/share/man/man8/athchans.8.gz
/usr/share/man/man8/athdebug.8.gz
/usr/share/man/man8/athstats.8.gz
/usr/share/doc/madwifi-tools/README.Debian
/usr/share/doc/madwifi-tools/changelog.Debian.gz
/usr/share/doc/madwifi-tools/copyright
/etc/modprobe.d/madwifi
/etc/network/if-pre-up.d/050madwifi
/etc/network/if-post-down.d/z50madwifi
/sbin/wlanconfig
主要的工具就是 wlanconfig。

首先澄清的一个概念是 atheros 芯片支持多个 VAP,共享在一个物理的无线网卡上。使用 ifconfig 可以获得一个 wifi0 的 interface,这个就是物理设备本身。使用 VAP 的好处是可以产生多个不同类型(mode)的 VAP 分别进行连接。那么 VAP 有些什么类型呢?
  • ahdemo 是另一种 ad-hoc demo (aka pseudo IBSS) mode 模式的 station。
  • adhoc 亦即 ad-hoc mode 的 station,该状态下不需要 AP 就可以互相连接,应该是作为两台笔记本直接联机的用途。
  • ap 亦即 AP mode,这个存在的目的是为了把自己的网卡变成一个 AP,但是可以存在多个 AP mode 的 VAP。自己的网卡作为一个 AP,就可以进行网络的桥接,做好适当的 routing 配置好 forwarding 就能实现很多好玩的功能了。
  • monitor 创建一个 monitor mode 的 station,这个模式类似于网卡的混杂模式。
  • sta 即 station mode,默认情况下(加载 ath_pci 模块时)创建的 VAP 类型,局限在于如果是该设备上第一个 VAP,后面就不能再创建其他的 VAP;但是如果不是第一个创建则可以创建多个不同的 VAP,但是 sta 只能有一个。
  • wds 亦即 WDS mode 下的 station,一般可以直接通过 iwpriv 修改一个 VAP 的对应功能。
我们最常用的可能就是 sta 模式了,这时我们作为一个 client 连接到某个 AP 上工作。后面我们将介绍几种最可能用到的连接方式:
  • sta + phycical AP 最常用无线网络,自己的无线网卡和别人提供的 AP 连接。
  • ad-hoc + ad-hoc 双机对联,两个无线网卡之间联机。
  • monitor 用于窃听,如捕获无线网络中数据,破解 WEP 密码等。
  • ap + sta 一个网卡作为 ap,一个作为 sta 进行连接。
对于桥接等比较高级的话题,等到对网络本身熟悉到足够程度后就知道怎么作了。

对 sta + physical AP 这个模式,我们首先需要创建 sta 模式下的 VAP,这一般在加载 ath_pci 时已经自动的完成了,很可能直接 ifconfig ath0 up 就能看见了,如果没有,可以用
# wlanconfig ath create wlandev wifi0 wlanmode sta
获得,然后我们需要通过该设备获得环境中可以连接的 AP,这部分就开始依靠 wireless-tools 的工具了。如果后面讲到创建 ap 模式的 VAP,我们可能需要去除掉该 VAP,这一般是
# wlanconfig destroy ath0
完成,类似的可以知道如何建立其他的 VAP 并将其销毁。

在 madwifi-tools 里面还有一些其他的工具:
  • athstats 显示物理设备网络负载等统计信息。
  • ath_info 显示 module 的信息、写 eeprom 信息(可以改变通信频段)。
  • 80211debug 用于在 madwifi 的 802.11 栈中设置或者清除调试信息。
  • athdebug 设置或者清除 madwifi 子系统的调试信息。
  • athkey 用于设置 wifiN 设备的四个 key,如 wep 等。
  • ahtchans 设置 wifiN 设备的频道范围。
  • 80211stats 用于显示 802.11 的统计信息。
  • athctrl 用于显示通讯的 stations 之间的最大距离。
  • wlanconfig 除了创建/销毁需要的 VAP 以外,还有 list 功能,如 wlanconfig ath0 list scan 将会列出搜索到的所有 AP 的信息。

下面我们来看 wireless-tools 的作用,这是里面含有的内容:
/sbin/iwconfig
/sbin/iwevent
/sbin/iwgetid
/sbin/iwlist
/sbin/iwpriv
/sbin/iwspy
/usr/share/man/man8/iwconfig.8.gz
/usr/share/man/man8/iwevent.8.gz
/usr/share/man/man8/iwgetid.8.gz
/usr/share/man/man8/iwlist.8.gz
/usr/share/man/man8/iwpriv.8.gz
/usr/share/man/man8/iwspy.8.gz
/usr/share/man/man7/wireless.7.gz
/usr/share/doc/wireless-tools/README.Debian
/usr/share/doc/wireless-tools/copyright
/usr/share/doc/wireless-tools/DISTRIBUTIONS.txt.gz
/usr/share/doc/wireless-tools/PCMCIA.txt.gz
/usr/share/doc/wireless-tools/HOTPLUG.txt.gz
/usr/share/doc/wireless-tools/README.gz
/usr/share/doc/wireless-tools/changelog.Debian.gz
/usr/share/doc/wireless-tools/changelog.gz
/etc/network/if-pre-up.d/wireless-tools
/etc/network/if-post-down.d/wireless-tools
这里面最重要的命令是 iwconfig 和 iwpriv。iwconfig 用于配置一些比较 general 的功能,如 ESSID、AP 的 MAC 地址、mode、频道、速率等。和 ifconfig 类似,不加任何参数的时候显示 /proc/net/wireless 的内容;而 iwpriv 则配置比较 private 的内容,和各个网卡相关的东西,可以用 -a 获得所有可以执行的命令。下面是这些命令的说明:
  • iwevent 显示 wireless 事件,主要是驱动程序或者配置更新的事件。
  • iwgetid 获得当前连接网络的信息,相对于 iwconfig 更适合写脚本使用。
  • iwlist 用于显示无线网络的各种信息,通过 scaning 网络中 AP 获得。
  • iwspy 用于获取无线网络某些节点上的统计信息。

那么,当我们通过 wlanconfig 创建了 sta 后,就应该利用该 ath0 获得可供连接的无线网络,这可以使用
# iwlist ath0 scan
得到一个列表,然后通过
iwconfig ath0 essid something
设置对应的 ESSID,或者也可以用诸如 MAC 地址之类的东西获得连接。在我的无线网卡上,如果没有驱动,会单灯慢速闪烁,另外一个灯熄灭;如果有驱动,插上后,一灯熄灭一灯亮起。使用 iwconfig 指定了连接的 AP 后则会双灯闪烁,这时候我们应该根据 iwlist 列出来的信息,如是否加密设置相应的密码。无线网络和有线网络一样都可以被监听,但是无线的特性使得这个问题更加严重。这个问题往往使用 link-layer 加密解决。如使用的 WEP 加密,可以直接用
# iwconfig ath0 enc ...
命令设置密码,如果是 WPA 类型,我们需要 wpasupplicant,这个包里面含有:
/etc/dbus-1/system.d/wpa_supplicant.conf
/etc/logrotate.d/wpa_action
/etc/logrotate.d/wpa_supplicant
/etc/wpa_supplicant/ifupdown.sh
/etc/wpa_supplicant/functions.sh
/etc/init.d/wpa-ifupdown
/usr/bin/wpa_passphrase
/usr/share/dbus-1/system-services/fi.epitest.hostap.WPASupplicant.service
/usr/share/doc/wpasupplicant/NEWS.Debian.gz
/usr/share/doc/wpasupplicant/examples
/usr/share/doc/wpasupplicant/examples/wep.conf
/usr/share/doc/wpasupplicant/examples/wpa2-eap-ccmp.conf
/usr/share/doc/wpasupplicant/examples/wpa-roam.conf
/usr/share/doc/wpasupplicant/examples/openCryptoki.conf
/usr/share/doc/wpasupplicant/examples/ieee8021x.conf
/usr/share/doc/wpasupplicant/examples/wpa-psk-tkip.conf
/usr/share/doc/wpasupplicant/examples/plaintext.conf
/usr/share/doc/wpasupplicant/changelog.Debian.gz
/usr/share/doc/wpasupplicant/copyright
/usr/share/doc/wpasupplicant/changelog.gz
/usr/share/doc/wpasupplicant/README.wpa_supplicant.conf.gz
/usr/share/doc/wpasupplicant/README.gz
/usr/share/doc/wpasupplicant/README.Debian.gz
/usr/share/man/man8/wpa_action.8.gz
/usr/share/man/man8/wpa_passphrase.8.gz
/usr/share/man/man8/wpa_cli.8.gz
/usr/share/man/man8/wpa_supplicant.8.gz
/usr/share/man/man8/wpa_background.8.gz
/usr/share/man/man5/wpa_supplicant.conf.5.gz
/sbin/wpa_action
/sbin/wpa_cli
/sbin/wpa_supplicant
/etc/network/if-up.d/wpasupplicant
/etc/network/if-down.d/wpasupplicant
/etc/network/if-post-down.d/wpasupplicant
/etc/network/if-pre-up.d/wpasupplicant
/usr/share/doc/wpasupplicant/README.modes.gz
这里面主要的两个命令是 wpa_passphrase 和 wpa——supplicant,前者用于产生需要的密钥信息,后者用于给指定的设备添加 WPA。一般利用
wpa_passphrase essid passphrase
生成一段信息,将其置入某个文件,然后用 wpa_supplicant 这个 daemon 产生需要的加密:
wpa_supplicant -B -Dwext -iath0 -c file
下面是一些其他程序的介绍:
  • wpa_supplicant 其中 -D 选择的是 wireless 的 driver 类型,-i 指定 interface(-N 可以指定多个)。
  • wpa_action 对指定 interface 发布指定的命令。可以用 wpa_cli 调用该命令。
  • wpa_cli 是一个文本界面下和 WPA 交互的客户端。

一般通过 ifconfig ath0 up 启动了对应的 interface 后,需要配置 IP 地址,这可以用 dhclient 通过 DHCP 实现动态的配置,这在 dhcp3-client 包中。

下面我们就其他的几种情况讲解如何配置无线网络:
ad-hoc + ad-hoc:

16 条评论:

匿名 说...

一看这些代码就头大。
没有什么意思。不就是用个无线网络,有必要这么复杂吗?

Unknown 说...

呵呵,有的时候有的 distribution 会提供一些傻瓜型的工具,这是不错,不过没有这些工具的时候就需要了解得更深入一些了...

匿名 说...

请问,用两台笔记本配置成AP工作模式,知道怎么弄吗?谢谢

匿名 说...

怎么没有(二)阿?

匿名 说...

I was ablе to find gοod info from your content.


mу site; Household's Quality Time Commit Nicely

匿名 说...

The gеneral οutward appеагances
of thesе ѵ2 cigs varies broadly frοm busіnesѕ tо
сorporаtion, аnd is devoid of a doubt one of
the bеst functіons of the item.

Here іs mу web page: Prnewswire.com

匿名 说...

Increasinglу pеoрle tоday аre аdoрtіng this strategу
whісh they're locating really very simple to practice.

my web site Http://mindshake.de/

匿名 说...

Benеfitѕ ωill possіbly bе shoωn if employed 30 minutеs each
dаy.

my page: Inmemoryofmydog.Com

匿名 说...

The signals connect with concentrated nerve areas, which in turn spread the
signal to the whole abdominal location.

Review my weblog ... Click That Link

匿名 说...

The sixty day complimentary trial give is a good
choice, in the case of the flex waistband.


My page :: Weight Problems With Each Other With Tummy Extra Fat Is Among The The Most Predominant Difficulties All Above The World. This Is A Great Deal Less Costly Than Paying Out For A Fitness Center Membership Or Buying Your Very Own Equipment. Adhering To Stomach Physical Workout Routines Alone Won'T Undertake Any Sort Of Support. The Flex Belt Is Secure And Has Been Analyzed Towards Provoking Agony Or Other Sorts Of Irritation. Flex Belt Review Talks About The Efficiency Of The Training Equipment In Breaking Down The Fat In The System And Thus Give A Excellent Developed. This Electrical Arousal Of The Muscle Mass Is Accomplished Frequently Until The Belt Is Switched Off. Alongside One Another Owing To Being Overweight You On Top Of That Face Other Well Being Challenges. The Flex Waistband Will Come With Gel Pads. Comprehensible Presented She Has 118

匿名 说...

So what can this all mean?

Also visit my webpage; flex Belt coupons

匿名 说...

I staгted mаking usе of Grееn Light up
now for on the month. I havе alгeаdу been a hеavy smoke enthusіast fοr Thirty fiѵe yеагs
еating arounԁ Α couplе οf packs а ԁay averаge.
My peгsonal mother that I also got Green Smοke with
гegard to hаs been smokіng cigarettеs for the lаst Siхtу fіvе yeагs!



Hеre іs my ωeb-sіtе livingwaychristianfriendshipgroup.com

匿名 说...

Why people stіll use to rеad neωs pаperѕ when in
thіs tеchnological woгlԁ the ωhοle
thing is рresentеd on web?

my site - V2 Cigs

匿名 说...

I've had my Environmentally friendly smoke for 5 months today and my own battery continues to be going strong! Thanks

Feel free to visit my blog - green smoke 510 adapter

匿名 说...

My programmеr is tгуing to рerѕuaԁе
me tο move tο .net frоm ΡHP.
Ӏ hаνe аlωays ԁіslikеd the
іԁеа beсause οf the сosts.
But hе's tryiong none the less. I've beеn usіng Movable-type on ѵariouѕ wеbѕites for abοut а уеаг and аm anxіοus abоut ѕωitching tο anοther plаtform.
I have heаrd great things about blogengine.
net. Is there a ωay Ι can іmρort all my wordprеss cоntеnt іnto it?
Anу kind оf help would be rеallу aрprеciаteԁ!


Feel frеe tо suгf to my blog dachausbau

匿名 说...

fаntastic post, verу infοrmаtive. Ι ωonder ωhy thе othеr ѕpecialiѕts of this ѕector
ԁo nοt noticе this. You shοuld
contіnuе yоur wrіting.
Ι am confident, yоu've a great readers' bаѕe alrеady!


Feel freе to ѕuгf to my blog post: brotbackautomat