net/ipv4/ip_tunnel.c
Source file repositories/reference/linux-study-clean/net/ipv4/ip_tunnel.c
File Facts
- System
- Linux kernel
- Corpus path
net/ipv4/ip_tunnel.c- Extension
.c- Size
- 32816 bytes
- Lines
- 1343
- Domain
- Networking Core
- Bucket
- Sockets, Protocols, Packet Path, And Network Policy
- Inferred role
- Networking Core: exported/initcall integration point
- Status
- integration implementation candidate
Why This File Exists
Networking stack implementation surface: socket APIs, protocol dispatch, packet flow, routing, filtering, and network namespaces.
- Networking stack implementation surface: socket APIs, protocol dispatch, packet flow, routing, filtering, and network namespaces.
- Exports symbols or registers init work; inspect boot/module ordering and who consumes the exported contract.
- Touches user memory; correctness depends on fault-safe copying and privilege boundary handling.
- Touches IRQ or DMA behavior; this matters for the representative real-device path.
- Defines or uses C structs; map object ownership, embedded links, reference counts, and lock ownership.
Dependency Surface
linux/capability.hlinux/module.hlinux/types.hlinux/kernel.hlinux/slab.hlinux/uaccess.hlinux/skbuff.hlinux/netdevice.hlinux/in.hlinux/tcp.hlinux/udp.hlinux/if_arp.hlinux/init.hlinux/in6.hlinux/inetdevice.hlinux/igmp.hlinux/netfilter_ipv4.hlinux/etherdevice.hlinux/if_ether.hlinux/if_vlan.hlinux/rculist.hlinux/err.hnet/sock.hnet/ip.hnet/icmp.hnet/protocol.hnet/ip_tunnels.hnet/arp.hnet/checksum.hnet/dsfield.hnet/inet_ecn.hnet/xfrm.h
Detected Declarations
function Copyrightfunction ip_tunnel_key_matchfunction hlist_for_each_entry_rcufunction hlist_for_each_entry_rcufunction hlist_for_each_entry_rcufunction hlist_for_each_entry_rcufunction ip_tunnel_addfunction ip_tunnel_delfunction hlist_for_each_entry_rcufunction ip_tunnel_bind_devfunction ip_tunnel_md_udp_encapfunction ip_tunnel_rcvfunction ip_tunnel_encap_add_opsfunction ip_tunnel_encap_del_opsfunction ip_tunnel_encap_setupfunction tnl_update_pmtufunction ip_md_tunnel_xmitfunction ip_tunnel_xmitfunction ip_tunnel_updatefunction ip_tunnel_ctlfunction ip_tunnel_parm_from_userfunction ip_tunnel_parm_to_userfunction ip_tunnel_siocdevprivatefunction __ip_tunnel_change_mtufunction ip_tunnel_change_mtufunction ip_tunnel_dev_freefunction ip_tunnel_dellinkfunction ip_tunnel_get_iflinkfunction ip_tunnel_init_netfunction ip_tunnel_delete_netfunction ip_tunnel_newlinkfunction ip_tunnel_changelinkfunction __ip_tunnel_initfunction ip_tunnel_uninitfunction ip_tunnel_setupexport ip_tunnel_lookupexport ip_tunnel_md_udp_encapexport ip_tunnel_rcvexport ip_tunnel_encap_add_opsexport ip_tunnel_encap_del_opsexport ip_tunnel_encap_setupexport ip_md_tunnel_xmitexport ip_tunnel_xmitexport ip_tunnel_ctlexport ip_tunnel_parm_from_userexport ip_tunnel_parm_to_userexport ip_tunnel_siocdevprivateexport __ip_tunnel_change_mtu
Annotated Snippet
if (!IS_ERR(rt)) {
tdev = rt->dst.dev;
ip_rt_put(rt);
}
if (dev->type != ARPHRD_ETHER)
dev->flags |= IFF_POINTOPOINT;
dst_cache_reset(&tunnel->dst_cache);
}
if (!tdev && tunnel->parms.link)
tdev = __dev_get_by_index(tunnel->net, tunnel->parms.link);
if (tdev) {
hlen = tdev->hard_header_len + tdev->needed_headroom;
mtu = min(tdev->mtu, IP_MAX_MTU);
}
dev->needed_headroom = t_hlen + hlen;
mtu -= t_hlen + (dev->type == ARPHRD_ETHER ? dev->hard_header_len : 0);
if (mtu < IPV4_MIN_MTU)
mtu = IPV4_MIN_MTU;
return mtu;
}
static struct ip_tunnel *ip_tunnel_create(struct net *net,
struct ip_tunnel_net *itn,
struct ip_tunnel_parm_kern *parms)
{
struct ip_tunnel *nt;
struct net_device *dev;
int t_hlen;
int mtu;
int err;
dev = __ip_tunnel_create(net, itn->rtnl_link_ops, parms);
if (IS_ERR(dev))
return ERR_CAST(dev);
mtu = ip_tunnel_bind_dev(dev);
err = dev_set_mtu(dev, mtu);
if (err)
goto err_dev_set_mtu;
nt = netdev_priv(dev);
t_hlen = nt->hlen + sizeof(struct iphdr);
dev->min_mtu = ETH_MIN_MTU;
dev->max_mtu = IP_MAX_MTU - t_hlen;
if (dev->type == ARPHRD_ETHER)
dev->max_mtu -= dev->hard_header_len;
ip_tunnel_add(itn, nt);
return nt;
err_dev_set_mtu:
unregister_netdevice(dev);
return ERR_PTR(err);
}
void ip_tunnel_md_udp_encap(struct sk_buff *skb, struct ip_tunnel_info *info)
{
const struct iphdr *iph = ip_hdr(skb);
const struct udphdr *udph;
if (iph->protocol != IPPROTO_UDP)
return;
udph = (struct udphdr *)((__u8 *)iph + (iph->ihl << 2));
info->encap.sport = udph->source;
info->encap.dport = udph->dest;
}
EXPORT_SYMBOL(ip_tunnel_md_udp_encap);
int ip_tunnel_rcv(struct ip_tunnel *tunnel, struct sk_buff *skb,
const struct tnl_ptk_info *tpi, struct metadata_dst *tun_dst,
bool log_ecn_error)
{
const struct iphdr *iph = ip_hdr(skb);
int nh, err;
#ifdef CONFIG_NET_IPGRE_BROADCAST
if (ipv4_is_multicast(iph->daddr)) {
DEV_STATS_INC(tunnel->dev, multicast);
skb->pkt_type = PACKET_BROADCAST;
}
#endif
if (test_bit(IP_TUNNEL_CSUM_BIT, tunnel->parms.i_flags) !=
Annotation
- Immediate include surface: `linux/capability.h`, `linux/module.h`, `linux/types.h`, `linux/kernel.h`, `linux/slab.h`, `linux/uaccess.h`, `linux/skbuff.h`, `linux/netdevice.h`.
- Detected declarations: `function Copyright`, `function ip_tunnel_key_match`, `function hlist_for_each_entry_rcu`, `function hlist_for_each_entry_rcu`, `function hlist_for_each_entry_rcu`, `function hlist_for_each_entry_rcu`, `function ip_tunnel_add`, `function ip_tunnel_del`, `function hlist_for_each_entry_rcu`, `function ip_tunnel_bind_dev`.
- Atlas domain: Networking Core / Sockets, Protocols, Packet Path, And Network Policy.
- Implementation status: integration implementation candidate.
- This snippet crosses the user/kernel memory boundary; validate fault handling and access checks before translating the pattern.
- IRQ or DMA behavior appears here, which is relevant to the selected PCIe/NVMe device path.
Implementation Notes
- This generated page is the file-by-file coverage layer; curated subsystem chapters should link here when they synthesize a multi-file control flow.
- Core OS pages should be promoted from atlas-only to deep-reviewed when they explain data structures, invariants, locking, lifecycle, and C implementation snippets.
- Driver-family pages are intentionally pattern-oriented unless they are part of the selected PCIe/NVMe representative device path.