ipv6: fix typo in fib6_net_exit()
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / net / ipv4 / ipcomp.c
CommitLineData
1da177e4
LT
1/*
2 * IP Payload Compression Protocol (IPComp) - RFC3173.
3 *
4 * Copyright (c) 2003 James Morris <jmorris@intercode.com.au>
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the Free
e905a9ed 8 * Software Foundation; either version 2 of the License, or (at your option)
1da177e4
LT
9 * any later version.
10 *
11 * Todo:
12 * - Tunable compression parameters.
13 * - Compression stats.
14 * - Adaptive compression.
15 */
1da177e4 16#include <linux/module.h>
4999f362 17#include <linux/err.h>
1da177e4
LT
18#include <linux/rtnetlink.h>
19#include <net/ip.h>
20#include <net/xfrm.h>
21#include <net/icmp.h>
22#include <net/ipcomp.h>
14c85021 23#include <net/protocol.h>
6fccab67 24#include <net/sock.h>
1da177e4
LT
25
26static void ipcomp4_err(struct sk_buff *skb, u32 info)
27{
a92df254 28 struct net *net = dev_net(skb->dev);
a94cfd19 29 __be32 spi;
b71d1d42 30 const struct iphdr *iph = (const struct iphdr *)skb->data;
1da177e4
LT
31 struct ip_comp_hdr *ipch = (struct ip_comp_hdr *)(skb->data+(iph->ihl<<2));
32 struct xfrm_state *x;
33
55be7a9c
DM
34 switch (icmp_hdr(skb)->type) {
35 case ICMP_DEST_UNREACH:
36 if (icmp_hdr(skb)->code != ICMP_FRAG_NEEDED)
37 return;
38 case ICMP_REDIRECT:
39 break;
40 default:
1da177e4 41 return;
55be7a9c 42 }
1da177e4 43
4195f814 44 spi = htonl(ntohs(ipch->cpi));
b71d1d42 45 x = xfrm_state_lookup(net, skb->mark, (const xfrm_address_t *)&iph->daddr,
e905a9ed 46 spi, IPPROTO_COMP, AF_INET);
1da177e4
LT
47 if (!x)
48 return;
55be7a9c 49
05ab86c5
SK
50 if (icmp_hdr(skb)->type == ICMP_DEST_UNREACH) {
51 atomic_inc(&flow_cache_genid);
52 rt_genid_bump(net);
53
55be7a9c 54 ipv4_update_pmtu(skb, net, info, 0, 0, IPPROTO_COMP, 0);
05ab86c5 55 } else
55be7a9c 56 ipv4_redirect(skb, net, 0, 0, IPPROTO_COMP, 0);
1da177e4
LT
57 xfrm_state_put(x);
58}
59
e905a9ed 60/* We always hold one tunnel user reference to indicate a tunnel */
1da177e4
LT
61static struct xfrm_state *ipcomp_tunnel_create(struct xfrm_state *x)
62{
a92df254 63 struct net *net = xs_net(x);
1da177e4 64 struct xfrm_state *t;
e905a9ed 65
a92df254 66 t = xfrm_state_alloc(net);
1da177e4
LT
67 if (t == NULL)
68 goto out;
69
70 t->id.proto = IPPROTO_IPIP;
71 t->id.spi = x->props.saddr.a4;
72 t->id.daddr.a4 = x->id.daddr.a4;
73 memcpy(&t->sel, &x->sel, sizeof(t->sel));
74 t->props.family = AF_INET;
e40b3286 75 t->props.mode = x->props.mode;
1da177e4
LT
76 t->props.saddr.a4 = x->props.saddr.a4;
77 t->props.flags = x->props.flags;
a947b0a9 78 t->props.extra_flags = x->props.extra_flags;
bd55775c 79 memcpy(&t->mark, &x->mark, sizeof(t->mark));
72cb6962
HX
80
81 if (xfrm_init_state(t))
1da177e4
LT
82 goto error;
83
1da177e4
LT
84 atomic_set(&t->tunnel_users, 1);
85out:
86 return t;
87
88error:
89 t->km.state = XFRM_STATE_DEAD;
90 xfrm_state_put(t);
91 t = NULL;
92 goto out;
93}
94
95/*
4a3e2f71 96 * Must be protected by xfrm_cfg_mutex. State and tunnel user references are
1da177e4
LT
97 * always incremented on success.
98 */
99static int ipcomp_tunnel_attach(struct xfrm_state *x)
100{
a92df254 101 struct net *net = xs_net(x);
1da177e4
LT
102 int err = 0;
103 struct xfrm_state *t;
bd55775c 104 u32 mark = x->mark.v & x->mark.m;
1da177e4 105
bd55775c 106 t = xfrm_state_lookup(net, mark, (xfrm_address_t *)&x->id.daddr.a4,
e905a9ed 107 x->props.saddr.a4, IPPROTO_IPIP, AF_INET);
1da177e4
LT
108 if (!t) {
109 t = ipcomp_tunnel_create(x);
110 if (!t) {
111 err = -EINVAL;
112 goto out;
113 }
114 xfrm_state_insert(t);
115 xfrm_state_hold(t);
116 }
117 x->tunnel = t;
118 atomic_inc(&t->tunnel_users);
119out:
120 return err;
121}
122
6fccab67 123static int ipcomp4_init_state(struct xfrm_state *x)
1da177e4 124{
2c3abab7 125 int err = -EINVAL;
1da177e4 126
e40b3286
HX
127 x->props.header_len = 0;
128 switch (x->props.mode) {
129 case XFRM_MODE_TRANSPORT:
130 break;
131 case XFRM_MODE_TUNNEL:
132 x->props.header_len += sizeof(struct iphdr);
133 break;
134 default:
135 goto out;
136 }
137
6fccab67
HX
138 err = ipcomp_init_state(x);
139 if (err)
1da177e4
LT
140 goto out;
141
7e49e6de 142 if (x->props.mode == XFRM_MODE_TUNNEL) {
1da177e4
LT
143 err = ipcomp_tunnel_attach(x);
144 if (err)
10e7454e 145 goto out;
1da177e4
LT
146 }
147
1da177e4
LT
148 err = 0;
149out:
150 return err;
1da177e4
LT
151}
152
533cb5b0 153static const struct xfrm_type ipcomp_type = {
1da177e4
LT
154 .description = "IPCOMP4",
155 .owner = THIS_MODULE,
156 .proto = IPPROTO_COMP,
6fccab67 157 .init_state = ipcomp4_init_state,
1da177e4
LT
158 .destructor = ipcomp_destroy,
159 .input = ipcomp_input,
160 .output = ipcomp_output
161};
162
32613090 163static const struct net_protocol ipcomp4_protocol = {
1da177e4
LT
164 .handler = xfrm4_rcv,
165 .err_handler = ipcomp4_err,
166 .no_policy = 1,
27000929 167 .netns_ok = 1,
1da177e4
LT
168};
169
170static int __init ipcomp4_init(void)
171{
172 if (xfrm_register_type(&ipcomp_type, AF_INET) < 0) {
058bd4d2 173 pr_info("%s: can't add xfrm type\n", __func__);
1da177e4
LT
174 return -EAGAIN;
175 }
176 if (inet_add_protocol(&ipcomp4_protocol, IPPROTO_COMP) < 0) {
058bd4d2 177 pr_info("%s: can't add protocol\n", __func__);
1da177e4
LT
178 xfrm_unregister_type(&ipcomp_type, AF_INET);
179 return -EAGAIN;
180 }
181 return 0;
182}
183
184static void __exit ipcomp4_fini(void)
185{
186 if (inet_del_protocol(&ipcomp4_protocol, IPPROTO_COMP) < 0)
058bd4d2 187 pr_info("%s: can't remove protocol\n", __func__);
1da177e4 188 if (xfrm_unregister_type(&ipcomp_type, AF_INET) < 0)
058bd4d2 189 pr_info("%s: can't remove xfrm type\n", __func__);
1da177e4
LT
190}
191
192module_init(ipcomp4_init);
193module_exit(ipcomp4_fini);
194
195MODULE_LICENSE("GPL");
6fccab67 196MODULE_DESCRIPTION("IP Payload Compression Protocol (IPComp/IPv4) - RFC3173");
1da177e4
LT
197MODULE_AUTHOR("James Morris <jmorris@intercode.com.au>");
198
d3d6dd3a 199MODULE_ALIAS_XFRM_TYPE(AF_INET, XFRM_PROTO_COMP);