热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

【openwrt】设备mt7628关于wan侧eth0.1mac地址固定的问题

本文讨论了在openwrt-17.01版本中,mt7628设备上初始化启动时eth0的mac地址总是随机生成的问题。每次随机生成的eth0的mac地址都会写到/sys/class/net/eth0/address目录下,而openwrt-17.01原版的SDK会根据随机生成的eth0的mac地址再生成eth0.1、eth0.2等,生成后的mac地址会保存在/etc/config/network下。

openwrt-17.01在mt7628上初始化启动时,eth0的mac地址总是随机生成,

mtk_soc_eth 10100000.ethernet: generated random MAC address e6:d7:5d:88:8c:8f
mtk_soc_eth
10100000.ethernet eth0: mediatek frame engine at 0xb0100000, irq 5

每次随机生成的eth0的mac地址都会写到/sys/class/net/eth0/address目录下面

root@LEDE:/# cat sys/class/net/eth0/address
e6:d7:5d:
88:8c:8f

openwrt-17.01原版的SDK会根据随机生成的eth0的mac地址,再生成eth0.1,eth0.2......,生成后的mac地址会保存再/etc/config/network下

root@LEDE:/etc/config# cat network config interface 'loopback'option ifname 'lo'option proto 'static'option ipaddr '127.0.0.1'option netmask '255.0.0.0'config globals 'globals'option ula_prefix 'fd59:fc20:6dd9::/48'config interface 'wan'option ifname 'eth0.1'option proto 'dhcp'config device 'wan_dev'option name 'eth0.1'option macaddr '00:1e:73:6e:5a:95'config interface 'wan6'option ifname 'eth0.1'option proto 'dhcpv6'config interface 'lan'option type 'bridge'option ifname 'eth0.2'option proto 'static'option ipaddr '192.168.1.1'option netmask '255.255.255.0'option ip6assign '60'config device 'lan_dev'option name 'eth0.2'option macaddr '00:1e:73:6e:5a:94'config switchoption name 'switch0'option reset '1'option enable_vlan '1'config switch_vlanoption device 'switch0'option vlan '1'option ports '0 6t'config switch_vlanoption device 'switch0'option vlan '2'option ports '2 3 6t'

这也就导致:一旦设备恢复出厂设置,eth0.1,eth0.2等的mac就会丢失,随eth0随机生成的新值变动。

如果设备所在局域网内的环境时根据mac分配IP地址的话,这样就会很麻烦,所以有这样一个需求:

设备每次启动,不管是重启还是恢复出厂,设备的wan 或者 lan侧的mac地址都会根据烧写好在factory区域的值读取出来应用。

 

所以我们的解决方案就是在每次启动时,将factory区写好的值读取出来,然后在生成/etc/config/network时,将之写进去。

openwrt的/etc/config下的文件,都是动态生成的,uci-defaults.sh会生成默认的/etc/config下的文件,脚本的函数定义在/package/base-files/files/lib/functions中,这里我们主要看一下生成network文件的内容:

ucidef_set_interface_lan() {json_select_object network_ucidef_set_interface lan "$@"json_select ..
}ucidef_set_interface_wan() {json_select_object network_ucidef_set_interface wan
"$@"json_select ..
}ucidef_set_interfaces_lan_wan() {local lan_if
="$1"local wan_if="$2"json_select_object network_ucidef_set_interface lan "$lan_if"_ucidef_set_interface wan "$wan_if"json_select ..
}ucidef_set_interface_raw() {json_select_object network_ucidef_set_interface
"$@"json_select ..
}_ucidef_add_switch_port() {# inherited: $num $device $need_tag $want_untag $role $index $prev_role# inherited: $n_cpu $n_ports $n_vlan $cpu0 $cpu1 $cpu2 $cpu3 $cpu4 $cpu5n_ports
=$((n_ports + 1))json_select_array portsjson_add_objectjson_add_int num "$num"[ -n "$device" ] && json_add_string device "$device"[ -n "$need_tag" ] && json_add_boolean need_tag "$need_tag"[ -n "$want_untag" ] && json_add_boolean want_untag "$want_untag"[ -n "$role" ] && json_add_string role "$role"[ -n "$index" ] && json_add_int index "$index"json_close_objectjson_select ..# record pointer to cpu entry for lookup in _ucidef_finish_switch_roles()[ -n "$device" ] && {export "cpu$n_cpu=$n_ports"n_cpu=$((n_cpu + 1))}# create/append object to role list[ -n "$role" ] && {json_select_array rolesif [ "$role" != "$prev_role" ]; thenjson_add_objectjson_add_string role "$role"json_add_string ports "$num"json_close_objectprev_role="$role"n_vlan=$((n_vlan + 1))elsejson_select_object "$n_vlan"json_get_var port portsjson_add_string ports "$port $num"json_select ..fijson_select ..}
}_ucidef_finish_switch_roles() {# inherited: $name $n_cpu $n_vlan $cpu0 $cpu1 $cpu2 $cpu3 $cpu4 $cpu5local index role roles num device need_tag want_untag port portsjson_select
switchjson_select "$name"json_get_keys roles rolesjson_select ..json_select ..for index in $roles; doeval "port=\$cpu$(((index - 1) % n_cpu))"json_select switchjson_select "$name"json_select portsjson_select "$port"json_get_vars num device need_tag want_untagjson_select ..json_select ..if [ $n_vlan -gt $n_cpu -o ${need_tag:-0} -eq 1 ]; thennum="${num}t"device="${device}.${index}"fijson_select rolesjson_select "$index"json_get_vars role portsjson_add_string ports "$ports $num"json_add_string device "$device"json_select ..json_select ..json_select ..json_select ..json_select_object networklocal devicesjson_select_object "$role"# attach previous interfaces (for multi-switch devices)json_get_var devices ifnameif ! list_contains devices "$device"; thendevices="${devices:+$devices }$device"fijson_select .._ucidef_set_interface "$role" "$devices"json_select ..done
}ucidef_add_switch() {local name
="$1"; shiftlocal port num role device index need_tag prev_rolelocal cpu0 cpu1 cpu2 cpu3 cpu4 cpu5local n_cpu=0 n_vlan=0 n_ports=0json_select_object switchjson_select_object "$name"json_add_boolean enable 1json_add_boolean reset 1for port in "$@"; docase "$port" in[0-9]*@*)num="${port%%@*}"device="${port##*@}"need_tag=0want_untag=0[ "${num%t}" != "$num" ] && {num="${num%t}"need_tag=1}[ "${num%u}" != "$num" ] && {num="${num%u}"want_untag=1};;[0-9]*:*:[0-9]*)num="${port%%:*}"index="${port##*:}"role="${port#[0-9]*:}"; role="${role%:*}";;[0-9]*:*)num="${port%%:*}"role="${port##*:}";;esacif [ -n "$num" ] && [ -n "$device$role" ]; then_ucidef_add_switch_portfiunset num device role index need_tag want_untagdonejson_select ..json_select .._ucidef_finish_switch_roles
}ucidef_add_switch_attr() {local name
="$1"local key="$2"local val="$3"json_select_object switchjson_select_object "$name"case "$val" intrue|false) [ "$val" != "true" ]; json_add_boolean "$key" $? ;;[0-9]) json_add_int "$key" "$val" ;;*) json_add_string "$key" "$val" ;;esacjson_select ..json_select ..
}ucidef_add_switch_port_attr() {local name
="$1"local port="$2"local key="$3"local val="$4"local ports i numjson_select_object switchjson_select_object "$name"json_get_keys ports portsjson_select_array portsfor i in $ports; dojson_select "$i"json_get_var num numif [ -n "$num" ] && [ $num -eq $port ]; thenjson_select_object attrcase "$val" intrue|false) [ "$val" != "true" ]; json_add_boolean "$key" $? ;;[0-9]) json_add_int "$key" "$val" ;;*) json_add_string "$key" "$val" ;;esacjson_select ..fijson_select ..donejson_select ..json_select ..json_select ..
}ucidef_set_interface_macaddr() {local network
="$1"local macaddr="$2"json_select_object networkjson_select "$network"[ $? -eq 0 ] || {json_select ..return}json_add_string macaddr "$macaddr"json_select ..json_select ..
}ucidef_add_atm_bridge() {local vpi
="$1"local vci="$2"local encaps="$3"local payload="$4"json_select_object dsljson_select_object atmbridgejson_add_int vpi "$vpi"json_add_int vci "$vci"json_add_string encaps "$encaps"json_add_string payload "$payload"json_select ..json_select ..
}

生成wan,lan侧mac的代码如下:

ucidef_set_interface_macaddr() {local network="$1"local macaddr="$2"json_select_object networkjson_select "$network"[ $? -eq 0 ] || {json_select ..return}json_add_string macaddr "$macaddr"json_select ..json_select ..
}

ucidef_set_interface_macaddr()函数其实是由\target\linux\ramips\base-files\etc\board.d\02_network调用,内容如下(其他型号的board已去掉)

#!/bin/sh. /lib/functions.sh
.
/lib/ramips.sh
.
/lib/functions/uci-defaults.sh
.
/lib/functions/system.shramips_setup_rt3x5x_vlans()
{
if [ ! -x /sbin/swconfig ]; then# legacy defaultucidef_set_interfaces_lan_wan "eth0.1" "eth0.2"returnfilocal wanports=""local lanports=""for port in 5 4 3 2 1 0; doif [ `swconfig dev rt305x port $port get disable` = "1" ]; thencontinuefiif [ `swconfig dev rt305x port $port get lan` = "0" ]; thenwanports="$port:wan $wanports"elselanports="$port:lan $lanports"fidoneucidef_add_switch "rt305x" $lanports $wanports "6t@eth0"
}ramips_setup_interfaces()
{local board
="$1"case $board inmt7628)ucidef_add_switch "switch0" \"0:wan" "2:lan" "3:lan" "6@eth0";;*)RT3X5X=`cat /proc/cpuinfo | egrep "(RT3.5|RT5350)"`if [ -n "${RT3X5X}" ]; thenramips_setup_rt3x5x_vlanselseucidef_set_interfaces_lan_wan "eth0.1" "eth0.2"fi;;esac
}ramips_setup_macs()
{local board
="$1"local lan_mac=""local wan_mac=""case $board inmt7628)lan_mac=$(hexdump -v -s 0x28 -n 6 -e '2/1 "%02x:"' /dev/mtd2)wan_mac=$(hexdump -v -s 0x2e -n 6 -e '2/1 "%02x:"' /dev/mtd2)[ -n "$lan_mac" ] && ucidef_set_interface_macaddr "lan" ${lan_mac%:}[ -n "$wan_mac" ] && ucidef_set_interface_macaddr "wan" ${wan_mac%:};;*)lan_mac=$(cat /sys/class/net/eth0/address)wan_mac=$(macaddr_add "$lan_mac" 1)[ -n "$lan_mac" ] && ucidef_set_interface_macaddr "lan" $lan_mac[ -n "$wan_mac" ] && ucidef_set_interface_macaddr "wan" $wan_mac;;esac}board_config_update
board
=$(ramips_board_name)
ramips_setup_interfaces $board
ramips_setup_macs $board
board_config_flushexit
0

这里面就很清楚了,原先openwrt的SDK是直接调用

lan_mac=$(mtd_get_mac_binary factory 40)
wan_mac
=$(mtd_get_mac_binary factory 46)

40和46对应的十六进制数就是lan和wan的偏移量0x28和0x2E,但是这样的修改没有用,lan_mac,wam_mac还是根据eth0生成,所以我们直接读取factory里面的值:

lan_mac=$(hexdump -v -s 0x28 -n 6 -e '2/1 "%02x:"' /dev/mtd2)
wan_mac
=$(hexdump -v -s 0x2e -n 6 -e '2/1 "%02x:"' /dev/mtd2)
[
-n "$lan_mac" ] && ucidef_set_interface_macaddr "lan" ${lan_mac%:}
[
-n "$wan_mac" ] && ucidef_set_interface_macaddr "wan" ${wan_mac%:}

Done。

 

转:https://www.cnblogs.com/yeanz/p/11177959.html



推荐阅读
  • Android系统源码分析Zygote和SystemServer启动过程详解
    本文详细解析了Android系统源码中Zygote和SystemServer的启动过程。首先介绍了系统framework层启动的内容,帮助理解四大组件的启动和管理过程。接着介绍了AMS、PMS等系统服务的作用和调用方式。然后详细分析了Zygote的启动过程,解释了Zygote在Android启动过程中的决定作用。最后通过时序图展示了整个过程。 ... [详细]
  • Java太阳系小游戏分析和源码详解
    本文介绍了一个基于Java的太阳系小游戏的分析和源码详解。通过对面向对象的知识的学习和实践,作者实现了太阳系各行星绕太阳转的效果。文章详细介绍了游戏的设计思路和源码结构,包括工具类、常量、图片加载、面板等。通过这个小游戏的制作,读者可以巩固和应用所学的知识,如类的继承、方法的重载与重写、多态和封装等。 ... [详细]
  • Iamtryingtomakeaclassthatwillreadatextfileofnamesintoanarray,thenreturnthatarra ... [详细]
  • Java容器中的compareto方法排序原理解析
    本文从源码解析Java容器中的compareto方法的排序原理,讲解了在使用数组存储数据时的限制以及存储效率的问题。同时提到了Redis的五大数据结构和list、set等知识点,回忆了作者大学时代的Java学习经历。文章以作者做的思维导图作为目录,展示了整个讲解过程。 ... [详细]
  • 阿,里,云,物,联网,net,core,客户端,czgl,aliiotclient, ... [详细]
  • 本文讨论了一个关于cuowu类的问题,作者在使用cuowu类时遇到了错误提示和使用AdjustmentListener的问题。文章提供了16个解决方案,并给出了两个可能导致错误的原因。 ... [详细]
  • [大整数乘法] java代码实现
    本文介绍了使用java代码实现大整数乘法的过程,同时也涉及到大整数加法和大整数减法的计算方法。通过分治算法来提高计算效率,并对算法的时间复杂度进行了研究。详细代码实现请参考文章链接。 ... [详细]
  • 本文介绍了Android 7的学习笔记总结,包括最新的移动架构视频、大厂安卓面试真题和项目实战源码讲义。同时还分享了开源的完整内容,并提醒读者在使用FileProvider适配时要注意不同模块的AndroidManfiest.xml中配置的xml文件名必须不同,否则会出现问题。 ... [详细]
  • Android源码深入理解JNI技术的概述和应用
    本文介绍了Android源码中的JNI技术,包括概述和应用。JNI是Java Native Interface的缩写,是一种技术,可以实现Java程序调用Native语言写的函数,以及Native程序调用Java层的函数。在Android平台上,JNI充当了连接Java世界和Native世界的桥梁。本文通过分析Android源码中的相关文件和位置,深入探讨了JNI技术在Android开发中的重要性和应用场景。 ... [详细]
  • Spring学习(4):Spring管理对象之间的关联关系
    本文是关于Spring学习的第四篇文章,讲述了Spring框架中管理对象之间的关联关系。文章介绍了MessageService类和MessagePrinter类的实现,并解释了它们之间的关联关系。通过学习本文,读者可以了解Spring框架中对象之间的关联关系的概念和实现方式。 ... [详细]
  • 海马s5近光灯能否直接更换为H7?
    本文主要介绍了海马s5车型的近光灯是否可以直接更换为H7灯泡,并提供了完整的教程下载地址。此外,还详细讲解了DSP功能函数中的数据拷贝、数据填充和浮点数转换为定点数的相关内容。 ... [详细]
  • (三)多表代码生成的实现方法
    本文介绍了一种实现多表代码生成的方法,使用了java代码和org.jeecg框架中的相关类和接口。通过设置主表配置,可以生成父子表的数据模型。 ... [详细]
  • 上图是InnoDB存储引擎的结构。1、缓冲池InnoDB存储引擎是基于磁盘存储的,并将其中的记录按照页的方式进行管理。因此可以看作是基于磁盘的数据库系统。在数据库系统中,由于CPU速度 ... [详细]
  • OpenMap教程4 – 图层概述
    本文介绍了OpenMap教程4中关于地图图层的内容,包括将ShapeLayer添加到MapBean中的方法,OpenMap支持的图层类型以及使用BufferedLayer创建图像的MapBean。此外,还介绍了Layer背景标志的作用和OMGraphicHandlerLayer的基础层类。 ... [详细]
  • 本文介绍了Hyperledger Fabric外部链码构建与运行的相关知识,包括在Hyperledger Fabric 2.0版本之前链码构建和运行的困难性,外部构建模式的实现原理以及外部构建和运行API的使用方法。通过本文的介绍,读者可以了解到如何利用外部构建和运行的方式来实现链码的构建和运行,并且不再受限于特定的语言和部署环境。 ... [详细]
author-avatar
jp85201
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有