0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012 #include <linux/module.h>
0013 #include <linux/err.h>
0014 #include <linux/rtnetlink.h>
0015 #include <net/ip.h>
0016 #include <net/xfrm.h>
0017 #include <net/icmp.h>
0018 #include <net/ipcomp.h>
0019 #include <net/protocol.h>
0020 #include <net/sock.h>
0021
0022 static int ipcomp4_err(struct sk_buff *skb, u32 info)
0023 {
0024 struct net *net = dev_net(skb->dev);
0025 __be32 spi;
0026 const struct iphdr *iph = (const struct iphdr *)skb->data;
0027 struct ip_comp_hdr *ipch = (struct ip_comp_hdr *)(skb->data+(iph->ihl<<2));
0028 struct xfrm_state *x;
0029
0030 switch (icmp_hdr(skb)->type) {
0031 case ICMP_DEST_UNREACH:
0032 if (icmp_hdr(skb)->code != ICMP_FRAG_NEEDED)
0033 return 0;
0034 break;
0035 case ICMP_REDIRECT:
0036 break;
0037 default:
0038 return 0;
0039 }
0040
0041 spi = htonl(ntohs(ipch->cpi));
0042 x = xfrm_state_lookup(net, skb->mark, (const xfrm_address_t *)&iph->daddr,
0043 spi, IPPROTO_COMP, AF_INET);
0044 if (!x)
0045 return 0;
0046
0047 if (icmp_hdr(skb)->type == ICMP_DEST_UNREACH)
0048 ipv4_update_pmtu(skb, net, info, 0, IPPROTO_COMP);
0049 else
0050 ipv4_redirect(skb, net, 0, IPPROTO_COMP);
0051 xfrm_state_put(x);
0052
0053 return 0;
0054 }
0055
0056
0057 static struct xfrm_state *ipcomp_tunnel_create(struct xfrm_state *x)
0058 {
0059 struct net *net = xs_net(x);
0060 struct xfrm_state *t;
0061
0062 t = xfrm_state_alloc(net);
0063 if (!t)
0064 goto out;
0065
0066 t->id.proto = IPPROTO_IPIP;
0067 t->id.spi = x->props.saddr.a4;
0068 t->id.daddr.a4 = x->id.daddr.a4;
0069 memcpy(&t->sel, &x->sel, sizeof(t->sel));
0070 t->props.family = AF_INET;
0071 t->props.mode = x->props.mode;
0072 t->props.saddr.a4 = x->props.saddr.a4;
0073 t->props.flags = x->props.flags;
0074 t->props.extra_flags = x->props.extra_flags;
0075 memcpy(&t->mark, &x->mark, sizeof(t->mark));
0076 t->if_id = x->if_id;
0077
0078 if (xfrm_init_state(t))
0079 goto error;
0080
0081 atomic_set(&t->tunnel_users, 1);
0082 out:
0083 return t;
0084
0085 error:
0086 t->km.state = XFRM_STATE_DEAD;
0087 xfrm_state_put(t);
0088 t = NULL;
0089 goto out;
0090 }
0091
0092
0093
0094
0095
0096 static int ipcomp_tunnel_attach(struct xfrm_state *x)
0097 {
0098 struct net *net = xs_net(x);
0099 int err = 0;
0100 struct xfrm_state *t;
0101 u32 mark = x->mark.v & x->mark.m;
0102
0103 t = xfrm_state_lookup(net, mark, (xfrm_address_t *)&x->id.daddr.a4,
0104 x->props.saddr.a4, IPPROTO_IPIP, AF_INET);
0105 if (!t) {
0106 t = ipcomp_tunnel_create(x);
0107 if (!t) {
0108 err = -EINVAL;
0109 goto out;
0110 }
0111 xfrm_state_insert(t);
0112 xfrm_state_hold(t);
0113 }
0114 x->tunnel = t;
0115 atomic_inc(&t->tunnel_users);
0116 out:
0117 return err;
0118 }
0119
0120 static int ipcomp4_init_state(struct xfrm_state *x)
0121 {
0122 int err = -EINVAL;
0123
0124 x->props.header_len = 0;
0125 switch (x->props.mode) {
0126 case XFRM_MODE_TRANSPORT:
0127 break;
0128 case XFRM_MODE_TUNNEL:
0129 x->props.header_len += sizeof(struct iphdr);
0130 break;
0131 default:
0132 goto out;
0133 }
0134
0135 err = ipcomp_init_state(x);
0136 if (err)
0137 goto out;
0138
0139 if (x->props.mode == XFRM_MODE_TUNNEL) {
0140 err = ipcomp_tunnel_attach(x);
0141 if (err)
0142 goto out;
0143 }
0144
0145 err = 0;
0146 out:
0147 return err;
0148 }
0149
0150 static int ipcomp4_rcv_cb(struct sk_buff *skb, int err)
0151 {
0152 return 0;
0153 }
0154
0155 static const struct xfrm_type ipcomp_type = {
0156 .owner = THIS_MODULE,
0157 .proto = IPPROTO_COMP,
0158 .init_state = ipcomp4_init_state,
0159 .destructor = ipcomp_destroy,
0160 .input = ipcomp_input,
0161 .output = ipcomp_output
0162 };
0163
0164 static struct xfrm4_protocol ipcomp4_protocol = {
0165 .handler = xfrm4_rcv,
0166 .input_handler = xfrm_input,
0167 .cb_handler = ipcomp4_rcv_cb,
0168 .err_handler = ipcomp4_err,
0169 .priority = 0,
0170 };
0171
0172 static int __init ipcomp4_init(void)
0173 {
0174 if (xfrm_register_type(&ipcomp_type, AF_INET) < 0) {
0175 pr_info("%s: can't add xfrm type\n", __func__);
0176 return -EAGAIN;
0177 }
0178 if (xfrm4_protocol_register(&ipcomp4_protocol, IPPROTO_COMP) < 0) {
0179 pr_info("%s: can't add protocol\n", __func__);
0180 xfrm_unregister_type(&ipcomp_type, AF_INET);
0181 return -EAGAIN;
0182 }
0183 return 0;
0184 }
0185
0186 static void __exit ipcomp4_fini(void)
0187 {
0188 if (xfrm4_protocol_deregister(&ipcomp4_protocol, IPPROTO_COMP) < 0)
0189 pr_info("%s: can't remove protocol\n", __func__);
0190 xfrm_unregister_type(&ipcomp_type, AF_INET);
0191 }
0192
0193 module_init(ipcomp4_init);
0194 module_exit(ipcomp4_fini);
0195
0196 MODULE_LICENSE("GPL");
0197 MODULE_DESCRIPTION("IP Payload Compression Protocol (IPComp/IPv4) - RFC3173");
0198 MODULE_AUTHOR("James Morris <jmorris@intercode.com.au>");
0199
0200 MODULE_ALIAS_XFRM_TYPE(AF_INET, XFRM_PROTO_COMP);