blob: 124f24bc4dbce4a9e47c859b9895100975ab0d96 [file] [log] [blame]
Herbert Xub59f45d2006-05-27 23:05:54 -07001/*
2 * xfrm4_mode_transport.c - Transport mode encapsulation for IPv4.
3 *
4 * Copyright (c) 2004-2006 Herbert Xu <herbert@gondor.apana.org.au>
5 */
6
7#include <linux/init.h>
8#include <linux/kernel.h>
9#include <linux/module.h>
10#include <linux/skbuff.h>
11#include <linux/stringify.h>
12#include <net/dst.h>
13#include <net/ip.h>
14#include <net/xfrm.h>
15
16/* Add encapsulation header.
17 *
18 * The IP header will be moved forward to make space for the encapsulation
19 * header.
20 *
21 * On exit, skb->h will be set to the start of the payload to be processed
22 * by x->type->output and skb->nh will be set to the top IP header.
23 */
Jamal Hadi Salimeb878e82006-08-31 17:42:59 -070024static int xfrm4_transport_output(struct xfrm_state *x, struct sk_buff *skb)
Herbert Xub59f45d2006-05-27 23:05:54 -070025{
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -070026 struct iphdr *iph = ip_hdr(skb);
27 int ihl = iph->ihl * 4;
Herbert Xub59f45d2006-05-27 23:05:54 -070028
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -070029 skb->h.raw = skb->nh.raw;
Herbert Xub59f45d2006-05-27 23:05:54 -070030 skb->h.raw += ihl;
31
Arnaldo Carvalho de Meloe2d1bca2007-04-10 20:46:21 -070032 skb_push(skb, x->props.header_len);
33 skb_reset_network_header(skb);
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -070034 memmove(skb_network_header(skb), iph, ihl);
Herbert Xub59f45d2006-05-27 23:05:54 -070035 return 0;
36}
37
Herbert Xu31a4ab92006-05-27 23:06:13 -070038/* Remove encapsulation header.
39 *
40 * The IP header will be moved over the top of the encapsulation header.
41 *
42 * On entry, skb->h shall point to where the IP header should be and skb->nh
43 * shall be set to where the IP header currently is. skb->data shall point
44 * to the start of the payload.
45 */
Herbert Xub59f45d2006-05-27 23:05:54 -070046static int xfrm4_transport_input(struct xfrm_state *x, struct sk_buff *skb)
47{
Herbert Xu31a4ab92006-05-27 23:06:13 -070048 int ihl = skb->data - skb->h.raw;
49
Arnaldo Carvalho de Melo7f5c0cb2007-03-10 19:59:16 -030050 if (skb->h.raw != skb->nh.raw) {
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -070051 memmove(skb->h.raw, skb_network_header(skb), ihl);
Arnaldo Carvalho de Melo7f5c0cb2007-03-10 19:59:16 -030052 skb->nh.raw = skb->h.raw;
53 }
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -070054 ip_hdr(skb)->tot_len = htons(skb->len + ihl);
Herbert Xu31a4ab92006-05-27 23:06:13 -070055 skb->h.raw = skb->data;
Herbert Xub59f45d2006-05-27 23:05:54 -070056 return 0;
57}
58
59static struct xfrm_mode xfrm4_transport_mode = {
60 .input = xfrm4_transport_input,
61 .output = xfrm4_transport_output,
62 .owner = THIS_MODULE,
63 .encap = XFRM_MODE_TRANSPORT,
64};
65
66static int __init xfrm4_transport_init(void)
67{
68 return xfrm_register_mode(&xfrm4_transport_mode, AF_INET);
69}
70
71static void __exit xfrm4_transport_exit(void)
72{
73 int err;
74
75 err = xfrm_unregister_mode(&xfrm4_transport_mode, AF_INET);
76 BUG_ON(err);
77}
78
79module_init(xfrm4_transport_init);
80module_exit(xfrm4_transport_exit);
81MODULE_LICENSE("GPL");
82MODULE_ALIAS_XFRM_MODE(AF_INET, XFRM_MODE_TRANSPORT);