- 论坛徽章:
- 0
|
在网上找到了这样一段代码
mch.c
- #include <sys/param.h>
- #include <sys/systm.h>
- #include <sys/time.h>
- #include <net/if.h>
- #include <net/if_ether.h>
- #include <net/if_dl.h>
- #include <sys/lkm.h>
- #include <sys/kernel.h>
- void generate_mac(); //losowo generowany MAC.
- void print_mac(unsigned char *); //wyswietlenie adresu MAC.
- int mch_lkmentry(struct lkm_table *, int, int); //Potrzebne
- int mch_lkmload(struct lkm_table *, int); //dla pracy
- int mch_lkmunload(struct lkm_table *, int); //modulu.
- unsigned char old_mac[6]; //zapisujemy (najlepiej jak modul bedzie uruchamiany za I razem, wtedy zapisywany jest org. adr).
- unsigned char new_mac[6] = { 0x00, 0x05, 0x2d, 0x02, 0x25, 0xfe }; <- tutaj nowy mac. //przechowanie nowego.
- struct ifnet *this = NULL; //dla 'ifunit' - szukamy struktury 'ifnet' po nazwie urzadzenia.
- MOD_MISC("CHANGE_MAC"); //INFO
- void
- generate_mac()
- {
- struct timeval tv; //Zapisujemy aktualny czas w sek. i milisek.
- microtime(&tv); //j.w.
- long stime = tv.tv_sec;
- long utime = tv.tv_usec;
-
- new_mac[0] = 0;
- stime = stime >> 8;
- new_mac[1] = stime & 0x000000ff;
- stime = stime >> 8;
- new_mac[2] = stime & 0x000000ff;
- stime = stime >> 8;
- new_mac[3] = stime & 0x000000ff;
- utime = utime >> 8;
- new_mac[4] = utime & 0x000000ff;
- utime = utime >> 8;
- new_mac[5] = utime & 0x000000ff; //zapisanie wygenerowanego MAC w 'new_mac'.
- }
- void
- print_mac(unsigned char *mac)
- {
- if(mac)
- {
- int i;
- for(i = 0; i <=4; i++)
- printf("%02x:", mac[i]);
- printf("%02x\n", mac[5]);
- }
- }
- int
- mch_lkmentry(struct lkm_table* lkmtp, int cmd, int ver)
- {
- LKM_DISPATCH(lkmtp, cmd, ver, mch_lkmload, mch_lkmunload, lkm_nofunc);
- }
- int
- mch_lkmload(struct lkm_table* lkmtp, int cmd)
- {
- if(lkmexists(lkmtp))
- return(EEXIST);
- if(!(this = ifunit("rtk0"))) return(EOPNOTSUPP); //szukamy rtk0
- //else generate_mac(); //i generujemy.
- memcpy(old_mac, LLADDR(this->if_sadl), (size_t)6); //zapis starego(orginalnego) MAC.
-
-
- memcpy(LLADDR(this->if_sadl), new_mac, this->if_addrlen);
- /* XXX: ew. to
- if_detach(this);
- if_attach(this);
- ether_ifattach(this, new_mac); //ustawiamy adres i typ na ethernet.
- */
- printf("\nNew MAC: "); print_mac(new_mac);
- return 0;
- }
- int
- mch_lkmunload(struct lkm_table *lkmtp, int cmd)
- {
- if(this)
- {
- /* przywrocenie starego MAC */
- memcpy(LLADDR(this->if_sadl), old_mac, this->if_addrlen);
- /*
- if_detach(this); //Tak samo
- if_attach(this); //jak wyzej
- ether_ifattach(this, old_mac); //tylko ze starym MAC.
- */
- printf("\nOld MAC: "); print_mac(old_mac);
- }
- return 0;
- }
复制代码
Makefile:
- KMOD= mac_ch
- SRCS= mch.c
- MAN= #noman
- .include <bsd.kmod.mk>
复制代码
修改第20行,为我要的mac地址。并注释掉 <- tutaj nowy mac.
修改第77行,rtk0为我的网卡gem0
make 通过。
生成几个.o文件。
运行modload mch.o
报错如下:
- mch.o(.text+0x10): In function `generate_mac':
- : relocation truncated to fit: R_PPC_REL24 microtime
- mch.o(.text+0x9c): In function `print_mac':
- : relocation truncated to fit: R_PPC_REL24 printf
- mch.o(.text+0xb4): In function `print_mac':
- : relocation truncated to fit: R_PPC_REL24 printf
- mch.o(.text+0x128): In function `mch_lkmentry':
- : relocation truncated to fit: R_PPC_REL24 lkmdispatch
- mch.o(.text+0x15c): In function `mch_lkmentry':
- : relocation truncated to fit: R_PPC_REL24 lkmdispatch
- mch.o(.text+0x16c): In function `mch_lkmentry':
- : relocation truncated to fit: R_PPC_REL24 lkm_nofunc
- mch.o(.text+0x180): In function `mch_lkmentry':
- : relocation truncated to fit: R_PPC_REL24 lkmdispatch
- mch.o(.text+0x1b0): In function `mch_lkmload':
- : relocation truncated to fit: R_PPC_REL24 lkmexists
- mch.o(.text+0x1c8): In function `mch_lkmload':
- : relocation truncated to fit: R_PPC_REL24 ifunit
- mch.o(.text+0x248): In function `mch_lkmload':
- : relocation truncated to fit: R_PPC_REL24 memcpy
- mch.o(.text+0x254): In function `mch_lkmload':
- : additional relocation overflows omitted from the output
- modload: can't link `mch.o' creating `mch' bound to 0xd4b20000
- YGC# modload mch.o |tee out.txt
- mch.o(.text+0x10): In function `generate_mac':
- : relocation truncated to fit: R_PPC_REL24 microtime
- mch.o(.text+0x9c): In function `print_mac':
- : relocation truncated to fit: R_PPC_REL24 printf
- mch.o(.text+0xb4): In function `print_mac':
- : relocation truncated to fit: R_PPC_REL24 printf
- mch.o(.text+0x128): In function `mch_lkmentry':
- : relocation truncated to fit: R_PPC_REL24 lkmdispatch
- mch.o(.text+0x15c): In function `mch_lkmentry':
- : relocation truncated to fit: R_PPC_REL24 lkmdispatch
- mch.o(.text+0x16c): In function `mch_lkmentry':
- : relocation truncated to fit: R_PPC_REL24 lkm_nofunc
- mch.o(.text+0x180): In function `mch_lkmentry':
- : relocation truncated to fit: R_PPC_REL24 lkmdispatch
- mch.o(.text+0x1b0): In function `mch_lkmload':
- : relocation truncated to fit: R_PPC_REL24 lkmexists
- mch.o(.text+0x1c8): In function `mch_lkmload':
- : relocation truncated to fit: R_PPC_REL24 ifunit
- mch.o(.text+0x248): In function `mch_lkmload':
- : relocation truncated to fit: R_PPC_REL24 memcpy
- mch.o(.text+0x254): In function `mch_lkmload':
- : additional relocation overflows omitted from the output
- modload: can't link `mch.o' creating `mch' bound to 0xd4b24000
复制代码 |
|