Tag Archives: network driver

[Linux] fakenetdev 假的 network interface

有些時候, 我們會需要一個假的 network device.
像是
1) 某些軟體會去 check network interface
2) 需要學習寫作一些軟體

所以我花了一點點時間寫了這一個 kernel modules
主要功能就是在 linux 下建立一個假的 network interface
這個 network interface
只能送(馬上就 free 掉 packet buffer), 沒有收的 function .

在 insert 時, 可以用參數 name 和 mac 指定特定的 network device name 和 mac address.
ex: # insmod fakenetdev.ko name=fake0 mac=00:aa:bb:cc:dd:ee
mac 的格式也可以是 00aabbccddee 或 00-aa-bb-cc-dd-ee
也支援用 ifconfig 修改 mac
ex: # ifconfig eth2 down
# ifconfig eth2 hw ether 00:aa:bb:cc:dd:ee
# ifconfig eth2 up

這個程式在 ubuntu 8.10 (interpid), Linux kernel 2.6.27 下測試.
目前看起來沒有很大的問題, 原作者要用的目地可以達到

可以從這個 URL 下載程式 fakenetdev

— English —
Sometimes, we will need a fake network device for some purpose
like,
1) cheat some software which will check network mac
2) learn how to write a simple network device driver

So, I spend a little time to write this kernel module.
Major function is that create fake network interface.
It can send packet, but driver will free it immediately.

When insert modules, the module support argument “name” and “mac” to
modify default name and mac.
ex: # insmod fakenetdev.ko name=fake0 mac=00:aa:bb:cc:dd:ee
it also support other format for mac, like 00aabbccddee or 00-aa-bb-cc-dd-ee
and support modify mac by ifconfig
ex: # ifconfig eth2 down
# ifconfig eth2 hw ether 00:aa:bb:cc:dd:ee
# ifconfig eth2 up

This module was tested at ubuntu 8.10 (interpid), Linux kernel 2.6.27.
It seems work fine for me .

You can download follow this link fakenetdev

後記:

這 Driver 不難寫, 麻煩的是在 Debug .不小心少寫一行就 Core Dump.
又懶得弄 KGDB 或是 User Mode Linux, 因為弄的時可能比寫完還多
網路上掃了一遍沒有看到馬上可以用的, 所以就自己亂寫, 希望
大家都可以用得到, 因為是用在*謎*用途的呀 :p

不過這個功能可以用 dummy device 達成,
我寫的頂多就是可以在 load driver 的時候改 name 和 mac .

Linux kernel 2.6.24 Porting 雜記.

最近將某個 Device Porting 從 2.6.16 Porting 上 2.6.24, 記錄一下碰到的問題.

1. Network Driver

當有這樣的訊息時
# ifconfig eth0 up
SIOCSIFFLAGS : invalid agument

我碰到的狀況是, MAC Address 必需要在 probe 時先行 initial 完成.
若是沒有完成, ifconfig 就會出現這樣的錯誤訊息, 原因仍然不明.
我的做法就是加一行
memcpy(dev->dev_addr , addr, 6); 這樣就可以了.

2.6.24 的 NAPI Interface 改過了, 上比對一下 2.6.23 和 2.6.24 的 Code 就可以知道改了什麼地方.
netif_poll_* 的 interface 改名了以外, NAPI hook 的 _poll function 在傳入參數和結構上有小小的修正

2.6.24 修改了 NAPI 的呼叫法, 對我這邊比較複雜的 Switch Driver 也應該修正一下.

2. PCI
pci_find_device 要改用 pci_get_device. 這個 function 還很早就改名了,
只是在 porting 這一版的時候, 多了一個選項
[ ] Enable deprecated pci_find_* API.
如果不想開就不用改了.

3. CPU
2.6.24 CPU proc.info 結構和2.6.16 有一點點不一樣, 如果直接放上 2.6.24 則 CPU Info 會錯誤,
這時要修改 mm/CPU-proc.S , 附上部份的 patch, 增加一個 word 大小, 這樣就可以顯示正常了.

PMD_BIT4 | \
PMD_SECT_AP_WRITE | \
PMD_SECT_AP_READ
+ .long PMD_TYPE_SECT | \
+ PMD_SECT_AP_WRITE | \
+ PMD_SECT_AP_READ

4. Interrupt.
原來 SA_INTERRUPT/SA_SHIRQ 都改叫 IRQF_SHARED, 事實上, 這一組定義己經完全換掉了
詳情請見 include\linux\Interrupt.h

5. DMA
pci_free_consistent 被 dma_free_coherent 取代
pci_alloc_consistent(..); 被 dma_alloc_coherent(.., GFP_KERNEL); 取代, 後面要多加一個參數.
consistent_sync 被 dma_cache_maint 取代.
算是小改….