blob: 3cff51ba72bb019d920554fa653937d729469b32 [file] [log] [blame]
Thomas Gleixner2874c5f2019-05-27 08:55:01 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * xfrm4_output.c - Common IPsec encapsulation code for IPv4.
4 * Copyright (c) 2004 Herbert Xu <herbert@gondor.apana.org.au>
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 */
6
Herbert Xu09b8f7a2006-06-22 03:08:03 -07007#include <linux/if_ether.h>
8#include <linux/kernel.h>
Herbert Xu36cf9ac2007-11-13 21:40:52 -08009#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070010#include <linux/skbuff.h>
Patrick McHardy16a66772006-01-06 23:01:48 -080011#include <linux/netfilter_ipv4.h>
Herbert Xu36cf9ac2007-11-13 21:40:52 -080012#include <net/dst.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <net/ip.h>
14#include <net/xfrm.h>
15#include <net/icmp.h>
16
Eric W. Biederman0c4b51f2015-09-15 20:04:18 -050017static int __xfrm4_output(struct net *net, struct sock *sk, struct sk_buff *skb)
Steffen Klassert55967322014-04-07 08:08:52 +020018{
Florian Westphal7d4343d2020-05-11 10:33:42 +020019#ifdef CONFIG_NETFILTER
Steffen Klassert55967322014-04-07 08:08:52 +020020 struct xfrm_state *x = skb_dst(skb)->xfrm;
21
Steffen Klassert55967322014-04-07 08:08:52 +020022 if (!x) {
23 IPCB(skb)->flags |= IPSKB_REROUTED;
Eric W. Biederman13206b62015-10-07 16:48:35 -050024 return dst_output(net, sk, skb);
Steffen Klassert55967322014-04-07 08:08:52 +020025 }
26#endif
27
Florian Westphal2ab60962020-05-04 10:06:09 +020028 return xfrm_output(sk, skb);
Steffen Klassert55967322014-04-07 08:08:52 +020029}
30
Eric W. Biedermanede20592015-10-07 16:48:47 -050031int xfrm4_output(struct net *net, struct sock *sk, struct sk_buff *skb)
Patrick McHardy16a66772006-01-06 23:01:48 -080032{
Eric W. Biederman29a26a52015-09-15 20:04:16 -050033 return NF_HOOK_COND(NFPROTO_IPV4, NF_INET_POST_ROUTING,
Phil Sutter28f8bfd2019-11-12 17:14:37 +010034 net, sk, skb, skb->dev, skb_dst(skb)->dev,
Eric W. Biederman29a26a52015-09-15 20:04:16 -050035 __xfrm4_output,
Patrick McHardy48d5cad2006-02-15 15:10:22 -080036 !(IPCB(skb)->flags & IPSKB_REROUTED));
Patrick McHardy16a66772006-01-06 23:01:48 -080037}
Hannes Frederic Sowa628e3412013-08-14 13:05:23 +020038
39void xfrm4_local_error(struct sk_buff *skb, u32 mtu)
40{
41 struct iphdr *hdr;
42
43 hdr = skb->encapsulation ? inner_ip_hdr(skb) : ip_hdr(skb);
44 ip_local_error(skb->sk, EMSGSIZE, hdr->daddr,
45 inet_sk(skb->sk)->inet_dport, mtu);
46}