免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 3352 | 回复: 6
打印 上一主题 下一主题

[NetBSD] 如何在netbsd下修改网卡MAC地址 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2007-08-11 12:11 |只看该作者 |倒序浏览
如题。。

IP与MAC被绑定了,不改上不了网咯。。

论坛徽章:
0
2 [报告]
发表于 2007-08-11 14:33 |只看该作者
自己顶

论坛徽章:
0
3 [报告]
发表于 2007-08-11 16:43 |只看该作者
google了一下午,发现freebsd和openbsd都可以用ifconfig去改变mac地址,唯独netbsd不可以。。


上不了网,烦死啦。:em11: :em11:

论坛徽章:
0
4 [报告]
发表于 2007-08-11 20:08 |只看该作者
在网上找到了这样一段代码
mch.c
  1. #include <sys/param.h>
  2. #include <sys/systm.h>
  3. #include <sys/time.h>

  4. #include <net/if.h>
  5. #include <net/if_ether.h>
  6. #include <net/if_dl.h>

  7. #include <sys/lkm.h>
  8. #include <sys/kernel.h>

  9. void generate_mac();               //losowo generowany MAC.
  10. void print_mac(unsigned char *);         //wyswietlenie adresu MAC.

  11. int mch_lkmentry(struct lkm_table *, int, int);      //Potrzebne
  12. int mch_lkmload(struct lkm_table *, int);      //dla pracy
  13. int mch_lkmunload(struct lkm_table *, int);      //modulu.

  14. unsigned char old_mac[6];            //zapisujemy (najlepiej jak modul bedzie uruchamiany za I razem, wtedy zapisywany jest org. adr).
  15. unsigned char new_mac[6] = { 0x00, 0x05, 0x2d, 0x02, 0x25, 0xfe };   <- tutaj nowy mac.         //przechowanie nowego.
  16. struct ifnet *this = NULL;            //dla 'ifunit' - szukamy struktury 'ifnet' po nazwie urzadzenia.

  17. MOD_MISC("CHANGE_MAC");               //INFO

  18. void
  19. generate_mac()
  20. {
  21.   struct timeval tv;               //Zapisujemy aktualny czas w sek. i milisek.

  22.     microtime(&tv);               //j.w.

  23.   long stime = tv.tv_sec;            
  24.   long utime = tv.tv_usec;            




  25.     new_mac[0] = 0;   
  26.    stime = stime >> 8;
  27.     new_mac[1] = stime & 0x000000ff;
  28.    stime = stime >> 8;
  29.     new_mac[2] = stime & 0x000000ff;
  30.    stime = stime >> 8;
  31.     new_mac[3] = stime & 0x000000ff;

  32.    utime = utime >> 8;
  33.     new_mac[4] = utime & 0x000000ff;
  34.    utime = utime >> 8;
  35.     new_mac[5] = utime & 0x000000ff;         //zapisanie wygenerowanego MAC w 'new_mac'.
  36. }

  37. void
  38. print_mac(unsigned char *mac)
  39. {
  40.   if(mac)
  41.   {
  42.     int i;

  43.     for(i = 0; i <=4; i++)
  44.       printf("%02x:", mac[i]);
  45.     printf("%02x\n", mac[5]);
  46.   }
  47. }

  48. int
  49. mch_lkmentry(struct lkm_table* lkmtp, int cmd, int ver)
  50. {
  51.   LKM_DISPATCH(lkmtp, cmd, ver, mch_lkmload, mch_lkmunload, lkm_nofunc);
  52. }

  53. int
  54. mch_lkmload(struct lkm_table* lkmtp, int cmd)
  55. {
  56.   if(lkmexists(lkmtp))
  57.     return(EEXIST);

  58.   if(!(this = ifunit("rtk0"))) return(EOPNOTSUPP);   //szukamy rtk0
  59.     //else generate_mac();            //i generujemy.

  60.   memcpy(old_mac, LLADDR(this->if_sadl), (size_t)6);   //zapis starego(orginalnego) MAC.


  61.   memcpy(LLADDR(this->if_sadl), new_mac, this->if_addrlen);

  62. /* XXX: ew. to
  63.   if_detach(this);               
  64.   if_attach(this);               
  65.   ether_ifattach(this, new_mac);         //ustawiamy adres i typ na ethernet.
  66. */
  67.   printf("\nNew MAC: "); print_mac(new_mac);      

  68.   return 0;
  69. }

  70. int
  71. mch_lkmunload(struct lkm_table *lkmtp, int cmd)
  72. {
  73.   if(this)
  74.   {

  75.     /* przywrocenie starego MAC */
  76.     memcpy(LLADDR(this->if_sadl), old_mac, this->if_addrlen);
  77. /*
  78.     if_detach(this);               //Tak samo
  79.     if_attach(this);               //jak wyzej
  80.     ether_ifattach(this, old_mac);         //tylko ze starym MAC.
  81. */
  82.     printf("\nOld MAC: "); print_mac(old_mac);
  83.   }

  84.   return 0;
  85. }
复制代码


Makefile:

  1. KMOD= mac_ch

  2. SRCS= mch.c

  3. MAN= #noman
  4. .include <bsd.kmod.mk>
复制代码


修改第20行,为我要的mac地址。并注释掉 <- tutaj nowy mac.     
修改第77行,rtk0为我的网卡gem0
make 通过。
生成几个.o文件。

运行modload mch.o
报错如下:
  1. mch.o(.text+0x10): In function `generate_mac':
  2. : relocation truncated to fit: R_PPC_REL24 microtime
  3. mch.o(.text+0x9c): In function `print_mac':
  4. : relocation truncated to fit: R_PPC_REL24 printf
  5. mch.o(.text+0xb4): In function `print_mac':
  6. : relocation truncated to fit: R_PPC_REL24 printf
  7. mch.o(.text+0x128): In function `mch_lkmentry':
  8. : relocation truncated to fit: R_PPC_REL24 lkmdispatch
  9. mch.o(.text+0x15c): In function `mch_lkmentry':
  10. : relocation truncated to fit: R_PPC_REL24 lkmdispatch
  11. mch.o(.text+0x16c): In function `mch_lkmentry':
  12. : relocation truncated to fit: R_PPC_REL24 lkm_nofunc
  13. mch.o(.text+0x180): In function `mch_lkmentry':
  14. : relocation truncated to fit: R_PPC_REL24 lkmdispatch
  15. mch.o(.text+0x1b0): In function `mch_lkmload':
  16. : relocation truncated to fit: R_PPC_REL24 lkmexists
  17. mch.o(.text+0x1c8): In function `mch_lkmload':
  18. : relocation truncated to fit: R_PPC_REL24 ifunit
  19. mch.o(.text+0x248): In function `mch_lkmload':
  20. : relocation truncated to fit: R_PPC_REL24 memcpy
  21. mch.o(.text+0x254): In function `mch_lkmload':
  22. : additional relocation overflows omitted from the output
  23. modload: can't link `mch.o' creating `mch' bound to 0xd4b20000
  24. YGC# modload mch.o |tee out.txt
  25. mch.o(.text+0x10): In function `generate_mac':
  26. : relocation truncated to fit: R_PPC_REL24 microtime
  27. mch.o(.text+0x9c): In function `print_mac':
  28. : relocation truncated to fit: R_PPC_REL24 printf
  29. mch.o(.text+0xb4): In function `print_mac':
  30. : relocation truncated to fit: R_PPC_REL24 printf
  31. mch.o(.text+0x128): In function `mch_lkmentry':
  32. : relocation truncated to fit: R_PPC_REL24 lkmdispatch
  33. mch.o(.text+0x15c): In function `mch_lkmentry':
  34. : relocation truncated to fit: R_PPC_REL24 lkmdispatch
  35. mch.o(.text+0x16c): In function `mch_lkmentry':
  36. : relocation truncated to fit: R_PPC_REL24 lkm_nofunc
  37. mch.o(.text+0x180): In function `mch_lkmentry':
  38. : relocation truncated to fit: R_PPC_REL24 lkmdispatch
  39. mch.o(.text+0x1b0): In function `mch_lkmload':
  40. : relocation truncated to fit: R_PPC_REL24 lkmexists
  41. mch.o(.text+0x1c8): In function `mch_lkmload':
  42. : relocation truncated to fit: R_PPC_REL24 ifunit
  43. mch.o(.text+0x248): In function `mch_lkmload':
  44. : relocation truncated to fit: R_PPC_REL24 memcpy
  45. mch.o(.text+0x254): In function `mch_lkmload':
  46. : additional relocation overflows omitted from the output
  47. modload: can't link `mch.o' creating `mch' bound to 0xd4b24000
复制代码

论坛徽章:
0
5 [报告]
发表于 2007-08-11 20:33 |只看该作者
附上上面代码的原始地址。
http://www.bsdguru.org/dyskusja/viewtopic.php?t=12313

论坛徽章:
2
亥猪
日期:2014-03-19 16:36:35午马
日期:2014-11-23 23:48:46
6 [报告]
发表于 2007-08-12 03:42 |只看该作者
http://mail-index.netbsd.org/tech-net/2001/01/
01/16:>>> Fw: Changing MAC addresses on ethernet interfaces?
似乎Jason R Thorpe已经给了补丁。
我对网络不是很熟悉,请熟悉的朋友验证下ioctl的方法。

论坛徽章:
0
7 [报告]
发表于 2007-12-13 10:47 |只看该作者
楼主我用的是debian linux 我没有if_dl.h文件,要怎么办?安装什么包?
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP