blob: 0b2a0641526a74118cf46336a185e62b9621fc95 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * xfrm4_state.c
3 *
4 * Changes:
5 * YOSHIFUJI Hideaki @USAGI
6 * Split up af-specific portion
7 *
8 */
9
Herbert Xudd871472005-06-20 13:21:43 -070010#include <net/ip.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#include <net/xfrm.h>
12#include <linux/pfkeyv2.h>
13#include <linux/ipsec.h>
Herbert Xu862b82c2007-11-13 21:43:11 -080014#include <linux/netfilter_ipv4.h>
Paul Gortmakerbc3b2d72011-07-15 11:47:34 -040015#include <linux/export.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016
Herbert Xudd871472005-06-20 13:21:43 -070017static int xfrm4_init_flags(struct xfrm_state *x)
18{
19 if (ipv4_config.no_pmtu_disc)
20 x->props.flags |= XFRM_STATE_NOPMTUDISC;
21 return 0;
22}
23
Linus Torvalds1da177e2005-04-16 15:20:36 -070024static void
David S. Miller73e5ebb2011-02-22 17:51:44 -080025__xfrm4_init_tempsel(struct xfrm_selector *sel, const struct flowi *fl)
Linus Torvalds1da177e2005-04-16 15:20:36 -070026{
David S. Miller7e1dc7b2011-03-12 02:42:11 -050027 const struct flowi4 *fl4 = &fl->u.ip4;
28
29 sel->daddr.a4 = fl4->daddr;
30 sel->saddr.a4 = fl4->saddr;
31 sel->dport = xfrm_flowi_dport(fl, &fl4->uli);
Thomas Egerer8444cf72010-09-20 11:11:38 -070032 sel->dport_mask = htons(0xffff);
David S. Miller7e1dc7b2011-03-12 02:42:11 -050033 sel->sport = xfrm_flowi_sport(fl, &fl4->uli);
Thomas Egerer8444cf72010-09-20 11:11:38 -070034 sel->sport_mask = htons(0xffff);
35 sel->family = AF_INET;
36 sel->prefixlen_d = 32;
37 sel->prefixlen_s = 32;
David S. Miller7e1dc7b2011-03-12 02:42:11 -050038 sel->proto = fl4->flowi4_proto;
39 sel->ifindex = fl4->flowi4_oif;
Thomas Egerer8444cf72010-09-20 11:11:38 -070040}
41
42static void
David S. Miller19bd6242011-02-24 00:07:20 -050043xfrm4_init_temprop(struct xfrm_state *x, const struct xfrm_tmpl *tmpl,
44 const xfrm_address_t *daddr, const xfrm_address_t *saddr)
Thomas Egerer8444cf72010-09-20 11:11:38 -070045{
Linus Torvalds1da177e2005-04-16 15:20:36 -070046 x->id = tmpl->id;
47 if (x->id.daddr.a4 == 0)
48 x->id.daddr.a4 = daddr->a4;
49 x->props.saddr = tmpl->saddr;
50 if (x->props.saddr.a4 == 0)
51 x->props.saddr.a4 = saddr->a4;
52 x->props.mode = tmpl->mode;
53 x->props.reqid = tmpl->reqid;
54 x->props.family = AF_INET;
55}
56
Herbert Xu36cf9ac2007-11-13 21:40:52 -080057int xfrm4_extract_header(struct sk_buff *skb)
58{
Eric Dumazetb71d1d42011-04-22 04:53:02 +000059 const struct iphdr *iph = ip_hdr(skb);
Herbert Xu36cf9ac2007-11-13 21:40:52 -080060
Herbert Xu732c8bd2008-03-26 16:51:09 -070061 XFRM_MODE_SKB_CB(skb)->ihl = sizeof(*iph);
Herbert Xu36cf9ac2007-11-13 21:40:52 -080062 XFRM_MODE_SKB_CB(skb)->id = iph->id;
63 XFRM_MODE_SKB_CB(skb)->frag_off = iph->frag_off;
64 XFRM_MODE_SKB_CB(skb)->tos = iph->tos;
65 XFRM_MODE_SKB_CB(skb)->ttl = iph->ttl;
Herbert Xu732c8bd2008-03-26 16:51:09 -070066 XFRM_MODE_SKB_CB(skb)->optlen = iph->ihl * 4 - sizeof(*iph);
Herbert Xu36cf9ac2007-11-13 21:40:52 -080067 memset(XFRM_MODE_SKB_CB(skb)->flow_lbl, 0,
68 sizeof(XFRM_MODE_SKB_CB(skb)->flow_lbl));
69
70 return 0;
71}
72
Linus Torvalds1da177e2005-04-16 15:20:36 -070073static struct xfrm_state_afinfo xfrm4_state_afinfo = {
74 .family = AF_INET,
Herbert Xu36cf9ac2007-11-13 21:40:52 -080075 .proto = IPPROTO_IPIP,
Herbert Xu227620e2007-11-13 21:41:28 -080076 .eth_proto = htons(ETH_P_IP),
Herbert Xu17c2a422007-10-17 21:33:12 -070077 .owner = THIS_MODULE,
Herbert Xudd871472005-06-20 13:21:43 -070078 .init_flags = xfrm4_init_flags,
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 .init_tempsel = __xfrm4_init_tempsel,
Thomas Egerer8444cf72010-09-20 11:11:38 -070080 .init_temprop = xfrm4_init_temprop,
Miika Komucdca7262007-02-06 14:24:56 -080081 .output = xfrm4_output,
Steffen Klassert43a4dea2011-05-09 19:36:38 +000082 .output_finish = xfrm4_output_finish,
Herbert Xu227620e2007-11-13 21:41:28 -080083 .extract_input = xfrm4_extract_input,
Herbert Xu36cf9ac2007-11-13 21:40:52 -080084 .extract_output = xfrm4_extract_output,
Herbert Xu716062f2007-11-13 21:44:23 -080085 .transport_finish = xfrm4_transport_finish,
Hannes Frederic Sowa628e3412013-08-14 13:05:23 +020086 .local_error = xfrm4_local_error,
Linus Torvalds1da177e2005-04-16 15:20:36 -070087};
88
89void __init xfrm4_state_init(void)
90{
91 xfrm_state_register_afinfo(&xfrm4_state_afinfo);
92}
93
Adrian Bunk0742fd52005-08-09 19:35:47 -070094#if 0
Linus Torvalds1da177e2005-04-16 15:20:36 -070095void __exit xfrm4_state_fini(void)
96{
97 xfrm_state_unregister_afinfo(&xfrm4_state_afinfo);
98}
Adrian Bunk0742fd52005-08-09 19:35:47 -070099#endif /* 0 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100