Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* xfrm4_tunnel.c: Generic IP tunnel transformer. |
| 2 | * |
| 3 | * Copyright (C) 2003 David S. Miller (davem@redhat.com) |
| 4 | */ |
| 5 | |
| 6 | #include <linux/skbuff.h> |
| 7 | #include <linux/module.h> |
Arjan van de Ven | 4a3e2f7 | 2006-03-20 22:33:17 -0800 | [diff] [blame] | 8 | #include <linux/mutex.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9 | #include <net/xfrm.h> |
| 10 | #include <net/ip.h> |
| 11 | #include <net/protocol.h> |
| 12 | |
| 13 | static int ipip_output(struct xfrm_state *x, struct sk_buff *skb) |
| 14 | { |
Herbert Xu | 7b277b1 | 2007-10-10 15:44:06 -0700 | [diff] [blame] | 15 | skb_push(skb, -skb_network_offset(skb)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 16 | return 0; |
| 17 | } |
| 18 | |
Herbert Xu | e695633 | 2006-04-01 00:52:46 -0800 | [diff] [blame] | 19 | static int ipip_xfrm_rcv(struct xfrm_state *x, struct sk_buff *skb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | { |
Herbert Xu | 04663d0 | 2007-10-17 21:28:06 -0700 | [diff] [blame] | 21 | return ip_hdr(skb)->protocol; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | } |
| 23 | |
Herbert Xu | 72cb696 | 2005-06-20 13:18:08 -0700 | [diff] [blame] | 24 | static int ipip_init_state(struct xfrm_state *x) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | { |
Masahide NAKAMURA | 7e49e6d | 2006-09-22 15:05:15 -0700 | [diff] [blame] | 26 | if (x->props.mode != XFRM_MODE_TUNNEL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | return -EINVAL; |
| 28 | |
| 29 | if (x->encap) |
| 30 | return -EINVAL; |
| 31 | |
| 32 | x->props.header_len = sizeof(struct iphdr); |
| 33 | |
| 34 | return 0; |
| 35 | } |
| 36 | |
| 37 | static void ipip_destroy(struct xfrm_state *x) |
| 38 | { |
| 39 | } |
| 40 | |
| 41 | static struct xfrm_type ipip_type = { |
| 42 | .description = "IPIP", |
| 43 | .owner = THIS_MODULE, |
| 44 | .proto = IPPROTO_IPIP, |
| 45 | .init_state = ipip_init_state, |
| 46 | .destructor = ipip_destroy, |
| 47 | .input = ipip_xfrm_rcv, |
| 48 | .output = ipip_output |
| 49 | }; |
| 50 | |
Herbert Xu | c4541b4 | 2007-10-17 21:28:53 -0700 | [diff] [blame] | 51 | static int xfrm_tunnel_rcv(struct sk_buff *skb) |
| 52 | { |
| 53 | return xfrm4_rcv_spi(skb, IPPROTO_IP, ip_hdr(skb)->saddr); |
| 54 | } |
| 55 | |
Herbert Xu | d2acc34 | 2006-03-28 01:12:13 -0800 | [diff] [blame] | 56 | static int xfrm_tunnel_err(struct sk_buff *skb, u32 info) |
| 57 | { |
| 58 | return -ENOENT; |
| 59 | } |
| 60 | |
| 61 | static struct xfrm_tunnel xfrm_tunnel_handler = { |
Herbert Xu | c4541b4 | 2007-10-17 21:28:53 -0700 | [diff] [blame] | 62 | .handler = xfrm_tunnel_rcv, |
Herbert Xu | d2acc34 | 2006-03-28 01:12:13 -0800 | [diff] [blame] | 63 | .err_handler = xfrm_tunnel_err, |
| 64 | .priority = 2, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 65 | }; |
| 66 | |
Kazunori MIYAZAWA | c0d5640 | 2007-02-13 12:54:47 -0800 | [diff] [blame] | 67 | #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) |
| 68 | static struct xfrm_tunnel xfrm64_tunnel_handler = { |
Herbert Xu | c4541b4 | 2007-10-17 21:28:53 -0700 | [diff] [blame] | 69 | .handler = xfrm_tunnel_rcv, |
Kazunori MIYAZAWA | c0d5640 | 2007-02-13 12:54:47 -0800 | [diff] [blame] | 70 | .err_handler = xfrm_tunnel_err, |
| 71 | .priority = 2, |
| 72 | }; |
| 73 | #endif |
| 74 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 75 | static int __init ipip_init(void) |
| 76 | { |
| 77 | if (xfrm_register_type(&ipip_type, AF_INET) < 0) { |
| 78 | printk(KERN_INFO "ipip init: can't add xfrm type\n"); |
| 79 | return -EAGAIN; |
| 80 | } |
Kazunori MIYAZAWA | c0d5640 | 2007-02-13 12:54:47 -0800 | [diff] [blame] | 81 | |
| 82 | if (xfrm4_tunnel_register(&xfrm_tunnel_handler, AF_INET)) { |
| 83 | printk(KERN_INFO "ipip init: can't add xfrm handler for AF_INET\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 84 | xfrm_unregister_type(&ipip_type, AF_INET); |
| 85 | return -EAGAIN; |
| 86 | } |
Kazunori MIYAZAWA | c0d5640 | 2007-02-13 12:54:47 -0800 | [diff] [blame] | 87 | #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) |
| 88 | if (xfrm4_tunnel_register(&xfrm64_tunnel_handler, AF_INET6)) { |
| 89 | printk(KERN_INFO "ipip init: can't add xfrm handler for AF_INET6\n"); |
| 90 | xfrm4_tunnel_deregister(&xfrm_tunnel_handler, AF_INET); |
| 91 | xfrm_unregister_type(&ipip_type, AF_INET); |
| 92 | return -EAGAIN; |
| 93 | } |
| 94 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 95 | return 0; |
| 96 | } |
| 97 | |
| 98 | static void __exit ipip_fini(void) |
| 99 | { |
Kazunori MIYAZAWA | c0d5640 | 2007-02-13 12:54:47 -0800 | [diff] [blame] | 100 | #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) |
| 101 | if (xfrm4_tunnel_deregister(&xfrm64_tunnel_handler, AF_INET6)) |
| 102 | printk(KERN_INFO "ipip close: can't remove xfrm handler for AF_INET6\n"); |
| 103 | #endif |
| 104 | if (xfrm4_tunnel_deregister(&xfrm_tunnel_handler, AF_INET)) |
| 105 | printk(KERN_INFO "ipip close: can't remove xfrm handler for AF_INET\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 106 | if (xfrm_unregister_type(&ipip_type, AF_INET) < 0) |
| 107 | printk(KERN_INFO "ipip close: can't remove xfrm type\n"); |
| 108 | } |
| 109 | |
| 110 | module_init(ipip_init); |
| 111 | module_exit(ipip_fini); |
| 112 | MODULE_LICENSE("GPL"); |
Masahide NAKAMURA | d3d6dd3 | 2007-06-26 23:57:49 -0700 | [diff] [blame] | 113 | MODULE_ALIAS_XFRM_TYPE(AF_INET, XFRM_PROTO_IPIP); |