跳至主要内容

对网关的理解

网络模型被OSI分成七层,TCP/IP协议大致对应了2、3、4、7层,分别是数据链路层、网络层、传输层、应用层,IP协议处于网络层上,它的工作原理说白了并不复杂:
  1. 整个互联网上所有的机器都有唯一一个IP地址对应。这并没有什么稀奇的,MAC地址也是唯一的,稀奇的在后面
  2. 机器的IP地址按照连接顺序进行了一定程度的分组,这样一个连续的IP段大致都在相同的路线上,这样路由器寻找特定IP地址的时候就方便多了,把整个地址空间分成若干个可能重叠的大段,每个大段一般这样表示:A.B.C.D/n,表示32位IP地址的前n位与A.B.C.D的前n位相同。比如192.168.1.0/24,所有前24位与192.168.1.0相同的都是这个网段的IP,由于IP地址8位一分组,24位就是前三段,也就是192.168.1.x。符合规范的这段连续的IP段就叫做一个子网。这种子网的表示方法叫做CIDR。
  3. 这种表示方法对人来说比较直观,但是计算机有另一种更快速的计算方法,对于A.B.C.D/n,我们构造一个前n位都是1、后面其他位都是0的IP地址,这个32位IP地址和A.B.C.D按位与的结果,就是前n位与A.B.C.D相同而后面其他位都是0的IP地址;如果这个32位IP地址和另一个IP地址与的结果,刚好与A.B.C.D与的结果相等,就说明这个IP地址和A.B.C.D的前n位相等,就说明新IP地址在CIDR范围内。我们把前n位都是1、后面其他位是0的地址也用IP地址的形式表示出来,那么192.168.1.0/24对应的就是255.255.255.0,这个就叫做子网掩码,掩码也就是mask,mask在计算机用语当中表示按位与的操作数,用来从目标数中取出特定的二进制位。也就是说IP+子网掩码是CIDR的另一种表示形式。

对于任意一台计算机或者路由器来说,它首先会有一些本地的链路,这些链路的IP地址都在各自特定的CIDR当中,这个子网叫做链路子网,链路子网中的IP地址都可以通过链路层协议直接访问,具体的访问方法与IP协议无关,在以太网当中是靠ARP,但是如果是WLAN,或者令牌环网,或者PPPoE,又会有所不同。比如子网是192.168.1.0/24,那么范围内所有IP地址都会直接调用链路层协议访问。表现在路由表中,就是这样一项:
192.168.1.0/24 -> link local, ifindex = XXX
即“192.168.1.0/24范围的IP通过XXX网卡的链路层协议直接访问”
如果还有其他链路,也会有相应的表项,比如说还有一个网卡上面的链路子网是111.222.1.0/24,那么就有另一项:
111.222.1.0/24 -> link local, ifindex = YYY
特别的,每个网卡上都会有一个IP地址,是本机的IP地址。当数据包发到这个IP地址的时候,设备就会正确理解“这个数据包是发给我自己的”,否则会理解为“这个数据包需要我代为转发”。这些本机的IP地址一定在链路层子网当中,比如说192.168.1.0/24中本机的IP地址是192.168.1.1,111.222.1.0/24中的IP地址是111.222.1.15,那么就有两个表项:
192.168.1.1/32 -> local
111.222.1.15/32 -> local
到local表示交给本机的更高层的协议栈,比如TCP/UDP去处理。否则会转发到其他机器。
我们注意到这些表项和刚才的表项有重叠的部分,比如说192.168.1.1/32其实包含在192.168.1.0/24里面。路由表的不同表项有不同的优先级,子网越小的越优先,也就是后面n越大的越优先,这样192.168.1.1/32就比192.168.1.0/24优先。

除了这些IP地址以外,其他的IP地址显然是无法直接访问的,需要由其他设备进行转发。不同网段的IP地址可能需要不同的设备进行转发。我们必须在路由表当中记住某段IP具体由哪个设备转发,记录的方法是记住需要转发的IP段CIDR,和需要发往的设备的IP地址,像这样:
100.101.1.0/24 -> via 192.168.1.17
这个via后面的地址,192.168.1.17,这个设备有进一步将数据包转发到目的网段的能力,比如说上面有一个链路层子网就是100.101.1.0/24,或者有另一条路由信息表明这个网段应该进一步发给其他的某个设备。这个设备在IP协议当中就叫做Gateway(网关),因为所有发到目的网段的流量都会从这个设备上经过,这个设备像个门一样,把两个网段连接了起来。
大多数情况下,这个IP地址位于某个链路层子网中,这样本机可以直接通过链路层协议将IP报文发送到网关。一部分设备(主要是硬件路由器)支持递归的路由表配置,这个时候网关地址可以不是本地链路层子网中的地址,当需要将IP报文发送到这个网关时,设备会递归查找网关的路由信息,直到查找到可以直接发送的地址为止。

互联网上的网段太多了,而且天天都在变,我们不可能给每个设备都配上互联网上所有的网段。幸好,对于大部分设备来说,除了特定的少数几个网段以外,大部分IP地址都有相同的网关,于是我们可以通过配置一个包含整个IP空间的CIDR来指定大部分IP地址的共同的网关:
0.0.0.0/0 -> via 111.222.1.254
由于前面说的原则,子网越小越优先,这条规则的优先度最低,而且能匹配所有的IP地址,因此可以理解为无法匹配到其他子网网段时使用的默认规则,因此这个网关一般叫做默认网关。
一个设备可能有多个网关,但是一般最多只有一个默认网关(不考虑等价路由、策略路由等复杂的情况)。也有可能没有默认网关,比如说骨干网上的路由器通过BGP协议交换路由信息,一般路由表就由非常多的CIDR组成,这些CIDR合起来能覆盖互联网上所有的公网IP。

最后说下链路层地址与IP地址。许多链路层协议也有自己的地址,一般是MAC地址,比如最常用的以太网。链路层地址在链路层协议中使用,表明这个链路层报文会被发给谁;而IP地址在IP层协议中使用,表明这个报文最终要发给谁。可以分为两种情况:
1. 直接通过链路层发送:
链路层地址: (源MAC地址) (目的MAC地址)
IP地址: (源IP地址) (目的IP地址)
2. 经过网关转发
从本机出发发往网关时,链路层的目的是网关而IP层的目的不是网关:
链路层地址: (本机源MAC地址) (网关MAC地址)
IP地址: (本机IP地址) (目的IP地址)
网关转发到其他网关:
链路层地址: (网关MAC地址) (下一跳网关MAC地址)
IP地址: (本机IP地址) (目的IP地址)
最后一个网关转发到目标:
链路层地址: (最后一个网关MAC地址) (目的MAC地址)
IP地址: (本机IP地址) (目的IP地址)
在转发过程中,IP报文的源和目的保持不变,链路层地址则只和这一跳的双方有关,甚至如果中间经过了不一样的链路层,还会更换二层数据包的格式。

Popular posts from 产品随想的博客

产品随想 | 周刊 第130期:集结信徒,而非官僚

On Dyson, techno-centric design and social consumption   https://2earth.github.io/website/20250707.html 如何創造偉大的事物   https://ryolu.notion.site/1610a94b9c108079a95be4362afd4a26 集結信徒,而非官僚 Reflections on OpenAI   https://calv.info/openai-reflections 创业架构 Shui   https://github.com/rock-zhang/Shui 好好喝水 https://www.ghibli.jp/works/ 吉卜力作品的高清图 From Skeuomorphic to Liquid Glass: Apple's Strategic Bet on the Post-Touch Future   https://omc345.substack.com/p/from-skeuomorphic-to-liquid-glass 迄今为止关于苹果Liquid Glass变革的解读,最好的一篇 The Nueva School   https://en.wikipedia.org/wiki/The_Nueva_School?useskin=vector 看起来是很酷的一个学校 The Barbican   https://arslan.io/2025/05/12/barbican-estate/ 史蒂夫·乔布斯希望你阅读的 9 本书   https://www.douban.com/doulist/147158849/ 《禅者的初心》里有句话:“做任何事,其实都是在展示内心的天性,这是我们存在的唯一理由。” Chuck Feeney was one of the greatest philanthropists ever   https://www.gatesnotes.com/Remembering-Chuck-Feeney 慈善家 My new deadline: 20 years to give away virtually all my wealt...

A Sister’s Eulogy for Steve Jobs

I grew up as an only child, with a single mother. Because we were poor and because I knew my father had emigrated from Syria, I imagined he looked like Omar Sharif. I hoped he would be rich and kind and would come into our lives (and our not yet furnished apartment) and help us. Later, after I’d met my father, I tried to believe he’d changed his number and left no forwarding address because he was an idealistic revolutionary, plotting a new world for the Arab people. Even as a feminist, my whole life I’d been waiting for a man to love, who could love me. For decades, I’d thought that man would be my father. When I was 25, I met that man and he was my brother. By then, I lived in New York, where I was trying to write my first novel. I had a job at a small magazine in an office the size of a closet, with three other aspiring writers. When one day a lawyer called me — me, the middle-class girl from California who hassled the boss to buy us health insurance — and said his cl...

《Becoming Steve Jobs》Chapter 16 Blind Spots, Grudges, and Sharp Elbows

Steve could be pretty thin-skinned when someone prominent criticized the aesthetics of his products. He took great umbrage that Neil would, as Steve put it, “pop off in public like that without coming to talk to us about his technical concerns first.” From that point on he had rebuffed all of Neil’s attempts to smoke the peace pipe. 有趣 He had blind spots, grating behavioral habits, and a tendency to give in to emotional impulse that persisted his entire life. These characteristics are often used to make the case that Steve was an “asshole” or a “jerk,” or perhaps simply “binary”—that odd adjective often used to convey the sense that he was half asshole/half genius from birth to death. These aren’t useful, interesting, or enlightening descriptions. What’s more illuminating is to take a look at the specific ways in which Steve failed to do an effective job of tempering some of his weaknesses and antisocial traits, and to consider how, when, and why some of them continued to flare up even...

黑客技术论坛推荐

原文来自知乎, 世界各大黑客技术论坛TOP排行榜 ,表示感谢 这份名单基本上囊括了目前世界上各大最佳黑客技术论坛。

360T7 刷机步骤及固件

https://cmi.hanwckf.top/p/360t7-firmware/   360T7的固件支持由immortalwrt-mt798x项目提供支持,请参考: https://cmi.hanwckf.top/p/immortalwrt-mt798x https://github.com/hanwckf/immortalwrt-mt798x 刷机步骤 参考 此处 的办法开启原厂固件的UART和telnet功能 在以下链接下载360T7测试固件(纯净版,无任何插件) https://wwd.lanzout.com/b0bt9idwd 密码:ezex (此固件已过时,请选择其它更新的固件) 接下来将刷入修改版uboot。修改版uboot的优点有: 固件分区可达108MB,原厂uboot只能使用36M 自带一个简单的webui恢复页面 到以下仓库的Release页面下载uboot,目前暂时仅支持360T7,后续将支持更多mt798x路由器。 推荐使用 mt7981_360t7-fip-fixed-parts.bin , fixed-parts 代表uboot分区表在编译期间固定,不会随着uboot环境变量变化。 https://github.com/hanwckf/bl-mt798x/releases/latest 将 mt7981_360t7-fip-fixed-parts.bin 通过HFS等方式上传到路由器,使用以下命令刷入uboot mtd write mt7981_360t7-fip-fixed-parts.bin fip 确认刷入完毕后,拔掉路由器电源。然后将电脑的IP地址设置为固定的 192.168.1.2 ,接着按住路由器的RESET按钮后通电开机,等待8s后用浏览器进入 192.168.1.1 在uboot恢复页面选择要刷入的固件。immortalwrt-mt798x目前编译两个版本的360T7固件。 建议修改版uboot直接使用 immortalwrt-mediatek-mt7981-mt7981-360-t7-108M-squashfs-factory.bin ,两种固件区别如下: mt7981-360-t7-108M 为108M固件分区,原厂uboot不可启动,需要修改版u...

写给大家看的中文排版指南

作者:Hindy 原文地址: http:// zhuanlan.zhihu.com/uici rcle/20506092 前言:很遗憾,我们的周围充斥着大量排版丑陋的文章。我国的字体排印与日本、美国等设计强国差距实在太大。我希望能够做些力所能及的小事,让更多人意识到“设计”的价值和其必要性,创造更美好的视觉环境。本文旨在帮助普及、提升大家对文字排版的认识,让大家在平时的学习工作中能有更专业的文字排版素养。 必看人群: 设计师、编辑、作家、撰稿人、教师、学生 目录: 1. 中文排版 1.1 引号 1.2 省略号与破折号 1.3 行首行尾禁则 2. 西文排版基础 2.1 西文撰写基础 2.2 西文标点相关 2.3 斜体的用法 2.4 大小写的区别 3. 中西文混排 3.1 基础原则 3.2 标点相关 1. 中文排版 1.1 引号 我国国家标准要求弯引号,个人建议使用直角引号。 示例:你竟然喜欢“苹果表”? 引号中再用引号使用双直角引号。 示例:我问他,“你竟然喜欢‘苹果表’?” 当引号表示讽刺、反语暗示时,使用弯引号(用法参考“西文排版”部分)。 示例:说真的,我也很 “喜欢”“苹果表”哦。 1.2 省略号(删节号)与破折号 省略号占两个汉字空间,包含六个点。 正确示例:中国设计还有太长路要走…… 错误示例:中国设计还有太长路要走… 破折号占两个汉字空间。 示例:中国设计还有太长路要走──加油罢。 1.3 行首行尾禁则 点号(顿号、逗号、句号等)、结束引号、结束括号等,不能出现在一行的开头。 错误示例: 排版时注意某些 符号不能在行首 ,别弄错了。 正确示例: 排版时注意某些 符号不能在行首, 别弄错了。 开始引号、开始括号、开始双书名号等,不能出现在一行的结尾。 错误示例: 她对我们说:“ 这书太赞了。” 正确示例: 她对我们说: “这书太赞了。” 2. 西文排版基础 2.1 西文撰写基础 句首字母大写。 单词间留空格。 示例:Have a question? 2.2 西文标点相关 点号后加一个空格(如逗号、句号等)。 示例:Hello everyone! Welcome to my blog....

《Becoming Steve Jobs》Chapter 17 “Just Tell Them I’m Being an Asshole”

For several months, I had been working to set up a joint interview of Steve, Andy Grove, Bill Gates, and Michael Dell. The confab was supposed to kick off the reporting for a book I had in mind. I had what I thought was a snappy title—Founders Keepers—and a plan to describe how a handful of geeky entrepreneurs had evolved into captains of industry; how self-absorbed inventors morphed into self-taught empire builders; how shaggy-haired idealists managed to stay in the saddle even as the companies they created grew rapidly by orders of magnitude, and as their own wealth and influence over the world itself became far more than the stuff of dreams. 好可惜啊,这次采访没有成形 During this time, Steve came to visit me in the hospital a couple of times. I was so addled with sedatives and painkillers and my own delirious hallucinations that during one visit I expressed my sincerest regrets at not being able to play saxophone in a Beatles retrospective show he was planning to put on in Las Vegas with Ringo S...

巴菲特致股东信-1980年

 笔记: 会计中对于下属股权公司的记账方式有3种: 持股50%以上,全部并入 持续20%--50%,则按持股比例并入 持股20%以下,则以实际收到的利润返还,计入报表 这种会计方式,会导致伯克希尔旗下,不少的企业,未能暴露实际的收益情况 对伯克希尔而言,对盈余的认定并非取决于持股比例是100%,50%,20%,5%或是1%,盈余的真正价值在于其将来再投资所能产生的效益 我们宁愿将所赚的盈余继续交由不受我们控制的人好好发挥,也不希望转由我们自己来浪费 高通货膨胀等于是对投入的资本额外课了一次税 翻译: https://xueqiu.com/6217262310/131837878 https://archive.ph/XMX5n  原文: Buffett’s Letters To Berkshire Shareholders 1980 巴菲特致股东的信 1980 年 Operating earnings improved to $41.9 million in 1980 from $36.0 million in 1979, but return on beginning equity capital (with securities valued at cost) fell to 17.8% from 18.6%. We believe the latter yardstick to be the most appropriate measure of single-year managerial economic performance. Informed use of that yardstick, however, requires an understanding of many factors, including accounting policies, historical ca...

Steve Jobs at 44, By Michael Krantz, 1999

Differences and Similarities Between Apple and Pixar Apple turns out many products--a dozen a year; if you count all the minor ones, probably a hundred. Pixar is striving to turn out one a year. But the converse of that is that Pixar's products will still be used fifty years from now, whereas I don't think you'll be using any product Apple brings to market this year fifty years from now. Pixar is making art for the ages. Kids will be watching Toy Story in the future. And Apple is much more of a constant race to continually improve things and stay ahead of the competition.  His Role At Pixar At Pixar my job is to help build the studio and recruit people and help create a situation where they can do the best work of their lives. And to some degree it's the same at Apple. But at Pixar, I don't direct the movies, whereas at Apple probably, if I had to pick a role out of a film production, I'd be the director. So it...

Apple's One-Dollar-a-Year Man, By Steve Jobs, 2000

(FORTUNE Magazine) – Now that Steve Jobs has showed his hand on Apple's Internet and system software strategies and dropped the "interim" from his title, other questions loom. He's always denied it, but isn't it true that his old company, Next, did wind up taking over Apple? Will there ever be an encore to the 15-year-old Macintosh? Short of that, does Apple have any plans to jump into the "Internet appliance" fray? Will Apple ever build computers for business people again? And what, pray tell, does Steve think of all these young Internet zillionaires? Let's ask. Practically every technology that your old company, Next, possessed when Apple acquired it in 1997 is now being used by Apple in some strategic way. This must seem like sweet vindication.  The thing about Next was that we produced something that was truly brilliant for an audience that our heart really wasn't into selling to--namely, the enterprise. I suppose if you were wr...