blob: 80c40b4981bb51ede84aeb235444538612cfa30b [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * xfrm4_state.c
4 *
5 * Changes:
6 * YOSHIFUJI Hideaki @USAGI
7 * Split up af-specific portion
8 *
9 */
10
Herbert Xudd871472005-06-20 13:21:43 -070011#include <net/ip.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include <net/xfrm.h>
13#include <linux/pfkeyv2.h>
14#include <linux/ipsec.h>
Herbert Xu862b82c2007-11-13 21:43:11 -080015#include <linux/netfilter_ipv4.h>
Paul Gortmakerbc3b2d72011-07-15 11:47:34 -040016#include <linux/export.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017
Herbert Xudd871472005-06-20 13:21:43 -070018static int xfrm4_init_flags(struct xfrm_state *x)
19{
Hannes Frederic Sowa974eda12013-12-14 05:13:38 +010020 if (xs_net(x)->ipv4.sysctl_ip_no_pmtu_disc)
Herbert Xudd871472005-06-20 13:21:43 -070021 x->props.flags |= XFRM_STATE_NOPMTUDISC;
22 return 0;
23}
24
Linus Torvalds1da177e2005-04-16 15:20:36 -070025static void
David S. Miller73e5ebb2011-02-22 17:51:44 -080026__xfrm4_init_tempsel(struct xfrm_selector *sel, const struct flowi *fl)
Linus Torvalds1da177e2005-04-16 15:20:36 -070027{
David S. Miller7e1dc7b2011-03-12 02:42:11 -050028 const struct flowi4 *fl4 = &fl->u.ip4;
29
30 sel->daddr.a4 = fl4->daddr;
31 sel->saddr.a4 = fl4->saddr;
32 sel->dport = xfrm_flowi_dport(fl, &fl4->uli);
Thomas Egerer8444cf72010-09-20 11:11:38 -070033 sel->dport_mask = htons(0xffff);
David S. Miller7e1dc7b2011-03-12 02:42:11 -050034 sel->sport = xfrm_flowi_sport(fl, &fl4->uli);
Thomas Egerer8444cf72010-09-20 11:11:38 -070035 sel->sport_mask = htons(0xffff);
36 sel->family = AF_INET;
37 sel->prefixlen_d = 32;
38 sel->prefixlen_s = 32;
David S. Miller7e1dc7b2011-03-12 02:42:11 -050039 sel->proto = fl4->flowi4_proto;
40 sel->ifindex = fl4->flowi4_oif;
Thomas Egerer8444cf72010-09-20 11:11:38 -070041}
42
43static void
David S. Miller19bd6242011-02-24 00:07:20 -050044xfrm4_init_temprop(struct xfrm_state *x, const struct xfrm_tmpl *tmpl,
45 const xfrm_address_t *daddr, const xfrm_address_t *saddr)
Thomas Egerer8444cf72010-09-20 11:11:38 -070046{
Linus Torvalds1da177e2005-04-16 15:20:36 -070047 x->id = tmpl->id;
48 if (x->id.daddr.a4 == 0)
49 x->id.daddr.a4 = daddr->a4;
50 x->props.saddr = tmpl->saddr;
51 if (x->props.saddr.a4 == 0)
52 x->props.saddr.a4 = saddr->a4;
53 x->props.mode = tmpl->mode;
54 x->props.reqid = tmpl->reqid;
55 x->props.family = AF_INET;
56}
57
Herbert Xu36cf9ac2007-11-13 21:40:52 -080058int xfrm4_extract_header(struct sk_buff *skb)
59{
Eric Dumazetb71d1d42011-04-22 04:53:02 +000060 const struct iphdr *iph = ip_hdr(skb);
Herbert Xu36cf9ac2007-11-13 21:40:52 -080061
Herbert Xu732c8bd2008-03-26 16:51:09 -070062 XFRM_MODE_SKB_CB(skb)->ihl = sizeof(*iph);
Herbert Xu36cf9ac2007-11-13 21:40:52 -080063 XFRM_MODE_SKB_CB(skb)->id = iph->id;
64 XFRM_MODE_SKB_CB(skb)->frag_off = iph->frag_off;
65 XFRM_MODE_SKB_CB(skb)->tos = iph->tos;
66 XFRM_MODE_SKB_CB(skb)->ttl = iph->ttl;
Herbert Xu732c8bd2008-03-26 16:51:09 -070067 XFRM_MODE_SKB_CB(skb)->optlen = iph->ihl * 4 - sizeof(*iph);
Herbert Xu36cf9ac2007-11-13 21:40:52 -080068 memset(XFRM_MODE_SKB_CB(skb)->flow_lbl, 0,
69 sizeof(XFRM_MODE_SKB_CB(skb)->flow_lbl));
70
71 return 0;
72}
73
Linus Torvalds1da177e2005-04-16 15:20:36 -070074static struct xfrm_state_afinfo xfrm4_state_afinfo = {
75 .family = AF_INET,
Herbert Xu36cf9ac2007-11-13 21:40:52 -080076 .proto = IPPROTO_IPIP,
Herbert Xu227620e2007-11-13 21:41:28 -080077 .eth_proto = htons(ETH_P_IP),
Herbert Xu17c2a422007-10-17 21:33:12 -070078 .owner = THIS_MODULE,
Herbert Xudd871472005-06-20 13:21:43 -070079 .init_flags = xfrm4_init_flags,
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 .init_tempsel = __xfrm4_init_tempsel,
Thomas Egerer8444cf72010-09-20 11:11:38 -070081 .init_temprop = xfrm4_init_temprop,
Miika Komucdca7262007-02-06 14:24:56 -080082 .output = xfrm4_output,
Steffen Klassert43a4dea2011-05-09 19:36:38 +000083 .output_finish = xfrm4_output_finish,
Herbert Xu227620e2007-11-13 21:41:28 -080084 .extract_input = xfrm4_extract_input,
Herbert Xu36cf9ac2007-11-13 21:40:52 -080085 .extract_output = xfrm4_extract_output,
Herbert Xu716062f2007-11-13 21:44:23 -080086 .transport_finish = xfrm4_transport_finish,
Hannes Frederic Sowa628e3412013-08-14 13:05:23 +020087 .local_error = xfrm4_local_error,
Linus Torvalds1da177e2005-04-16 15:20:36 -070088};
89
90void __init xfrm4_state_init(void)
91{
92 xfrm_state_register_afinfo(&xfrm4_state_afinfo);
93}