Linux 网络系统学习: Neighboring Subsystem

本文详细探讨了Linux网络系统中的邻居子系统,包括主要数据结构、邻居系统的初始化、路由与邻居子系统的关联,特别是邻居状态转换机制及ARP包的处理流程。通过对UDP发送过程的分析,揭示了数据包如何通过neighbour->output送到链路层,并提供了解决无谓ARP查询的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Linux 网络系统学习: Neighboring Subsystem

1. 概述

在数据包的发送过程中,通过路由获得下一跳的 L3 地址,下一步是获得此 L3 地址所对应的 L2 地址,这个过程称为 neighbor discovery。IPv4对应的是 ARP 协议,IPv6 对应的是Neighbor Discovery 协议。

Linux 中,用于处理neighbor discovery的模块称为 neighboring subsystem。它分为两层,底层是通用框架 neighboring infrastructure,在此之上,又有不同的具体实现,例如ARP 模块、 ND 模块等。

Neighboring subsystem 的主要任务包括:

1、Neighbour discovery;通过 L3 地址找到 L2 地址;为发送数据提供保障
2、接收 neighbor 包并进行处理
3、提供 cache,以加速 neighboring 的过程
4、为系统中其它模块需要 neighboring discovery 而提供 APIs


2. Neighboring infrastructure  

2.1 主要数据结构:

1、  struct neighbour
最主要的结构

2、  struct neigh_table
用于管理 struct neighbour

3、  struct neigh_ops
用于映射到 L2  的输出函数

4、  struct neigh_parms

5、  struct hh_cache

           

2.2 数据结构关系:

下图是 neighboring subsystem 中数据结构关系图,其关系可描述如下:

1、系统通过 neigh_tables 来管理各种具体的 neigh_table,包括 arp_tbl 和 nd_table
2、Neigh_table 通过 hash_buckets 来维护一个 neigh_table 的 hash 表。可以迅速的增加、删除、查找  neighbour
3、neighbour  的作用??? Neighbour 的 parms 指向 neigh_parms 结构,此结构用于 neighbour 的维护,例如重传次数,状态转换时间,垃圾收集时间等。
4、neighbour 的 ops 指向 neigh_ops 结构,此结构用于???
5、neighbour 的 hh 指向 hh_cache,此结构用于 cache L2 地址,以加速 L3 到 L2 的映射过程。

 


 

2.3 工具函数

1、struct neighbour *neigh_alloc(struct neigh_table *tbl)
创建一个 neighbour,并初始化,它只被  neighbour_create() 调用

2、struct neighbour * neigh_create(struct neigh_table *tbl, const void *pkey, struct net_device *dev)
调用 neigh_alloc() 分配一个 neighboure ,然后进一步调用具体协议的构造函数,以及具体设备的特殊的设置函数;最后,将此 neighbour 加入 neighbour table 中
它主要被  __neigh_lookup() 调用,也就是说,当在 neighbour table 中找不到 neighbour 的时候,调用此函数来创建一个新的 neighbour

3、struct neighbour *neigh_lookup(struct neigh_table *tbl, const void *pkey, struct net_device *dev)
在 neighbour table 中寻找特定的 neighbour

4、static void neigh_timer_handler(unsigned long arg)
这是一个定时器处理函数。当某个 neighbour 超时后,由此函数处理。

5、int neigh_update(struct neighbour *neigh, co
1 Introduction 1 1.1 Background . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 1.2 Document Conventions . . . . . . . . . . . . . . . . . . . . . . 1 1.3 Sample Network Example . . . . . . . . . . . . . . . . . . . . 1 2 Initialization 3 2.1 Function do basic setup() . . . . . . . . . . . . . . . . . . . . 3 2.2 Function sock init() . . . . . . . . . . . . . . . . . . . . . . . . 5 2.2.1 Function sk init() . . . . . . . . . . . . . . . . . . . . . 8 2.2.2 Function skb init() . . . . . . . . . . . . . . . . . . . . 10 2.2.2.1 union skb head pool . . . . . . . . . . . . . . 11 2.2.2.2 Function skb queue head init() . . . . . . . . 11 2.2.2.3 struct sk bu? head . . . . . . . . . . . . . . . 11 2.2.3 Function wanrouter init() . . . . . . . . . . . . . . . . 12 2.2.3.1 Function wanrouter proc init() . . . . . . . . 12 2.2.3.2 Function sdladrv init() . . . . . . . . . . . . . 12 2.2.3.3 wanpipe init() . . . . . . . . . . . . . . . . . 12 2.2.4 Netlink sockets . . . . . . . . . . . . . . . . . . . . . . 12 2.2.5 Function rtnetlink init() . . . . . . . . . . . . . . . . . 12 2.2.5.1 Function netlink kernel create() . . . . . . . . 13 2.2.5.2 Function register netdevice notifier() . . . . . 13 2.2.6 Function init netlink() . . . . . . . . . . . . . . . . . . 13 2.2.7 Function netfilter init() . . . . . . . . . . . . . . . . . . 15 2.2.8 Function bluez init() . . . . . . . . . . . . . . . . . . . 15 2.3 Function do initcalls() . . . . . . . . . . . . . . . . . . . . . . 16 2.4 Function netlink proto init() . . . . . . . . . . . . . . . . . . . 16 2.5 Function inet init() . . . . . . . . . . . . . . . . . . . . . . . . 17 2.5.1 Function sock register() . . . . . . . . . . . . . . . . . 22 2.5.1.1 struct net proto family . . . . . . . . . . . . . 23 iii iv CONTENTS 2.5.2 Adding INET Protocols . . . . . . . . . . . . . . . . . 24 2.5.2.1 struct inet protocol . . . . . . . . . . . . . . . 24 2.5.2.2 Function inet add protocol() . . . . . . . . . . 26 2.5.3 Register Socket Interfaces . . . . . . . . . . . . . . . . 28 2.5.3.1 struct inet protosw . . . . . . . . . . . . . . . 28 2.5.3.2 struct proto . . . . . . . . . . . . . . . . . . . 29 2.5.3.3 struct proto ops . . . . . . . . . . . . . . . . 31 2.5.3.4 Array inetsw array . . . . . . . . . . . . . . . 33 2.5.3.5 Function inet register protosw(..) . . . . . . . 36 2.5.4 Initializing Protocols . . . . . . . . . . . . . . . . . . . 36 2.5.4.1 struct packet type . . . . . . . . . . . . . . . 36 2.5.4.2 Function dev add pack() . . . . . . . . . . . . 37 2.5.5 Function arp init() . . . . . . . . . . . . . . . . . . . . 39 2.5.5.1 arp packet type . . . . . . . . . . . . . . . . . 39 2.5.5.2 Function neigh table init(..) . . . . . . . . . . 40 2.5.5.3 Function neigh sysctl register(..) . . . . . . . 40 2.5.6 Function ip init() . . . . . . . . . . . . . . . . . . . . . 40 2.5.6.1 ip packet type . . . . . . . . . . . . . . . . . 41 2.5.6.2 Function ip rt init() . . . . . . . . . . . . . . 41 2.5.6.3 Function inet initpeers() . . . . . . . . . . . . 43 2.5.7 Function tcp v4 init(..) . . . . . . . . . . . . . . . . . . 44 2.5.8 Function tcp init() . . . . . . . . . . . . . . . . . . . . 46 2.5.8.1 Function tcpdiag init() . . . . . . . . . . . . . 50 2.5.9 Function icmp init(..) . . . . . . . . . . . . . . . . . . . 51 2.5.10 Function ipip init() . . . . . . . . . . . . . . . . . . . . 53 2.5.11 Function ipgre init() . . . . . . . . . . . . . . . . . . . 53 2.5.12 Function ip mr init() . . . . . . . . . . . . . . . . . . . 54 2.6 Function af unix init() . . . . . . . . . . . . . . . . . . . . . . 54 2.7 Function packet init() . . . . . . . . . . . . . . . . . . . . . . 56 2.7.0.1 Function register netdevice notifier(..) . . . . 57 2.7.0.2 struct notifier block . . . . . . . . . . . . . . 57 3 BSD Sockets 59 3.1 Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59 4 INET Sockets 61 4.1 Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61 5 Transport Layer 63 5.1 Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63 CONTENTS v 6 Network Layer 65 6.1 Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65 7 Syscall Trace 67 7.1 Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67 7.2 Socket structres . . . . . . . . . . . . . . . . . . . . . . . . . . 67 7.3 Syscalls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67 7.3.1 Establishing Connection . . . . . . . . . . . . . . . . . 67 7.3.2 socket creation . . . . . . . . . . . . . . . . . . . . . . 67 7.3.3 bind walkthrough . . . . . . . . . . . . . . . . . . . . . 67 7.3.4 listen walkthrough . . . . . . . . . . . . . . . . . . . . 67 7.3.5 accept walkthrough . . . . . . . . . . . . . . . . . . . . 67 7.3.6 connect walkthrough . . . . . . . . . . . . . . . . . . . 67 7.3.7 close walkthrough . . . . . . . . . . . . . . . . . . . . . 67 7.4 Linux Functions . . . . . . . . . . . . . . . . . . . . . . . . . . 67 8 Receiving Messages 69 8.1 Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69 8.2 Receiving Walkthrough . . . . . . . . . . . . . . . . . . . . . . 69 8.2.1 Reading from a socket - I . . . . . . . . . . . . . . . . . 69 8.2.2 Receiving a Packet . . . . . . . . . . . . . . . . . . . . 69 8.2.3 SoftIRQ - net rx action . . . . . . . . . . . . . . . . . . 69 8.2.4 Unwrapping Packet in IP . . . . . . . . . . . . . . . . . 69 8.2.5 Accepting a Packet in UDP . . . . . . . . . . . . . . . 69 8.2.6 Accepting a Packet in TCP . . . . . . . . . . . . . . . 69 8.2.7 Reading from a Socket - II . . . . . . . . . . . . . . . . 69 8.3 Linux Functions . . . . . . . . . . . . . . . . . . . . . . . . . . 69 9 Sending Messages 71 9.1 Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71 9.2 Sending Walkthrough . . . . . . . . . . . . . . . . . . . . . . . 71 9.2.1 Writing to a socket . . . . . . . . . . . . . . . . . . . . 71 9.2.2 Creating a Packet with UDP . . . . . . . . . . . . . . . 71 9.2.3 Creating a Packet with TCP . . . . . . . . . . . . . . . 71 9.2.4 Wrapping a Packet in IP . . . . . . . . . . . . . . . . . 71 9.2.5 Transmitting a Packet . . . . . . . . . . . . . . . . . . 71 9.3 Linux Functions . . . . . . . . . . . . . . . . . . . . . . . . . . 71 10 IP Routing 73 10.1 Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73 vi CONTENTS 11 IP Forwarding 75 11.1 Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75 12 Netfilter 77 12.1 Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77 GNU Free Document License 79 Bibliography 81
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值