1 // SPDX-License-Identifier: GPL-2.0
3 * XFRM virtual interface
5 * Copyright (C) 2018 secunet Security Networks AG
8 * Steffen Klassert <steffen.klassert@secunet.com>
11 #include <linux/module.h>
12 #include <linux/capability.h>
13 #include <linux/errno.h>
14 #include <linux/types.h>
15 #include <linux/sockios.h>
16 #include <linux/icmp.h>
20 #include <linux/net.h>
21 #include <linux/in6.h>
22 #include <linux/netdevice.h>
23 #include <linux/if_link.h>
24 #include <linux/if_arp.h>
25 #include <linux/icmpv6.h>
26 #include <linux/init.h>
27 #include <linux/route.h>
28 #include <linux/rtnetlink.h>
29 #include <linux/netfilter_ipv6.h>
30 #include <linux/slab.h>
31 #include <linux/hash.h>
33 #include <linux/uaccess.h>
34 #include <linux/atomic.h>
39 #include <net/ip6_route.h>
40 #include <net/addrconf.h>
42 #include <net/net_namespace.h>
43 #include <net/netns/generic.h>
44 #include <linux/etherdevice.h>
46 static int xfrmi_dev_init(struct net_device
*dev
);
47 static void xfrmi_dev_setup(struct net_device
*dev
);
48 static struct rtnl_link_ops xfrmi_link_ops __read_mostly
;
49 static unsigned int xfrmi_net_id __read_mostly
;
52 /* lists for storing interfaces in use */
53 struct xfrm_if __rcu
*xfrmi
[1];
56 #define for_each_xfrmi_rcu(start, xi) \
57 for (xi = rcu_dereference(start); xi; xi = rcu_dereference(xi->next))
59 static struct xfrm_if
*xfrmi_lookup(struct net
*net
, struct xfrm_state
*x
)
61 struct xfrmi_net
*xfrmn
= net_generic(net
, xfrmi_net_id
);
64 for_each_xfrmi_rcu(xfrmn
->xfrmi
[0], xi
) {
65 if (x
->if_id
== xi
->p
.if_id
&&
66 (xi
->dev
->flags
& IFF_UP
))
73 static struct xfrm_if
*xfrmi_decode_session(struct sk_buff
*skb
,
74 unsigned short family
)
76 struct xfrmi_net
*xfrmn
;
80 if (!secpath_exists(skb
) || !skb
->dev
)
85 ifindex
= inet6_sdif(skb
);
88 ifindex
= inet_sdif(skb
);
92 ifindex
= skb
->dev
->ifindex
;
94 xfrmn
= net_generic(xs_net(xfrm_input_state(skb
)), xfrmi_net_id
);
96 for_each_xfrmi_rcu(xfrmn
->xfrmi
[0], xi
) {
97 if (ifindex
== xi
->dev
->ifindex
&&
98 (xi
->dev
->flags
& IFF_UP
))
105 static void xfrmi_link(struct xfrmi_net
*xfrmn
, struct xfrm_if
*xi
)
107 struct xfrm_if __rcu
**xip
= &xfrmn
->xfrmi
[0];
109 rcu_assign_pointer(xi
->next
, rtnl_dereference(*xip
));
110 rcu_assign_pointer(*xip
, xi
);
113 static void xfrmi_unlink(struct xfrmi_net
*xfrmn
, struct xfrm_if
*xi
)
115 struct xfrm_if __rcu
**xip
;
116 struct xfrm_if
*iter
;
118 for (xip
= &xfrmn
->xfrmi
[0];
119 (iter
= rtnl_dereference(*xip
)) != NULL
;
122 rcu_assign_pointer(*xip
, xi
->next
);
128 static void xfrmi_dev_free(struct net_device
*dev
)
130 struct xfrm_if
*xi
= netdev_priv(dev
);
132 gro_cells_destroy(&xi
->gro_cells
);
133 free_percpu(dev
->tstats
);
136 static int xfrmi_create2(struct net_device
*dev
)
138 struct xfrm_if
*xi
= netdev_priv(dev
);
139 struct net
*net
= dev_net(dev
);
140 struct xfrmi_net
*xfrmn
= net_generic(net
, xfrmi_net_id
);
143 dev
->rtnl_link_ops
= &xfrmi_link_ops
;
144 err
= register_netdevice(dev
);
148 strcpy(xi
->p
.name
, dev
->name
);
151 xfrmi_link(xfrmn
, xi
);
159 static struct xfrm_if
*xfrmi_create(struct net
*net
, struct xfrm_if_parms
*p
)
161 struct net_device
*dev
;
167 strlcpy(name
, p
->name
, IFNAMSIZ
);
173 dev
= alloc_netdev(sizeof(*xi
), name
, NET_NAME_UNKNOWN
, xfrmi_dev_setup
);
179 dev_net_set(dev
, net
);
181 xi
= netdev_priv(dev
);
185 xi
->phydev
= dev_get_by_index(net
, p
->link
);
191 err
= xfrmi_create2(dev
);
205 static struct xfrm_if
*xfrmi_locate(struct net
*net
, struct xfrm_if_parms
*p
,
208 struct xfrm_if __rcu
**xip
;
210 struct xfrmi_net
*xfrmn
= net_generic(net
, xfrmi_net_id
);
212 for (xip
= &xfrmn
->xfrmi
[0];
213 (xi
= rtnl_dereference(*xip
)) != NULL
;
215 if (xi
->p
.if_id
== p
->if_id
) {
217 return ERR_PTR(-EEXIST
);
223 return ERR_PTR(-ENODEV
);
224 return xfrmi_create(net
, p
);
227 static void xfrmi_dev_uninit(struct net_device
*dev
)
229 struct xfrm_if
*xi
= netdev_priv(dev
);
230 struct xfrmi_net
*xfrmn
= net_generic(xi
->net
, xfrmi_net_id
);
232 xfrmi_unlink(xfrmn
, xi
);
237 static void xfrmi_scrub_packet(struct sk_buff
*skb
, bool xnet
)
240 skb
->pkt_type
= PACKET_HOST
;
256 static int xfrmi_rcv_cb(struct sk_buff
*skb
, int err
)
258 const struct xfrm_mode
*inner_mode
;
259 struct pcpu_sw_netstats
*tstats
;
260 struct net_device
*dev
;
261 struct xfrm_state
*x
;
265 if (err
&& !secpath_exists(skb
))
268 x
= xfrm_input_state(skb
);
270 xi
= xfrmi_lookup(xs_net(x
), x
);
278 dev
->stats
.rx_errors
++;
279 dev
->stats
.rx_dropped
++;
284 xnet
= !net_eq(xi
->net
, dev_net(skb
->dev
));
287 inner_mode
= &x
->inner_mode
;
289 if (x
->sel
.family
== AF_UNSPEC
) {
290 inner_mode
= xfrm_ip2inner_mode(x
, XFRM_MODE_SKB_CB(skb
)->protocol
);
291 if (inner_mode
== NULL
) {
292 XFRM_INC_STATS(dev_net(skb
->dev
),
293 LINUX_MIB_XFRMINSTATEMODEERROR
);
298 if (!xfrm_policy_check(NULL
, XFRM_POLICY_IN
, skb
,
303 xfrmi_scrub_packet(skb
, xnet
);
305 tstats
= this_cpu_ptr(dev
->tstats
);
307 u64_stats_update_begin(&tstats
->syncp
);
308 tstats
->rx_packets
++;
309 tstats
->rx_bytes
+= skb
->len
;
310 u64_stats_update_end(&tstats
->syncp
);
316 xfrmi_xmit2(struct sk_buff
*skb
, struct net_device
*dev
, struct flowi
*fl
)
318 struct xfrm_if
*xi
= netdev_priv(dev
);
319 struct net_device_stats
*stats
= &xi
->dev
->stats
;
320 struct dst_entry
*dst
= skb_dst(skb
);
321 unsigned int length
= skb
->len
;
322 struct net_device
*tdev
;
323 struct xfrm_state
*x
;
328 goto tx_err_link_failure
;
331 dst
= xfrm_lookup_with_ifid(xi
->net
, dst
, fl
, NULL
, 0, xi
->p
.if_id
);
335 goto tx_err_link_failure
;
340 goto tx_err_link_failure
;
342 if (x
->if_id
!= xi
->p
.if_id
)
343 goto tx_err_link_failure
;
349 net_warn_ratelimited("%s: Local routing loop detected!\n",
351 goto tx_err_dst_release
;
355 if (!skb
->ignore_df
&& skb
->len
> mtu
) {
356 skb_dst_update_pmtu(skb
, mtu
);
358 if (skb
->protocol
== htons(ETH_P_IPV6
)) {
359 if (mtu
< IPV6_MIN_MTU
)
362 icmpv6_send(skb
, ICMPV6_PKT_TOOBIG
, 0, mtu
);
364 icmp_send(skb
, ICMP_DEST_UNREACH
, ICMP_FRAG_NEEDED
,
372 xfrmi_scrub_packet(skb
, !net_eq(xi
->net
, dev_net(dev
)));
373 skb_dst_set(skb
, dst
);
376 err
= dst_output(xi
->net
, skb
->sk
, skb
);
377 if (net_xmit_eval(err
) == 0) {
378 struct pcpu_sw_netstats
*tstats
= this_cpu_ptr(dev
->tstats
);
380 u64_stats_update_begin(&tstats
->syncp
);
381 tstats
->tx_bytes
+= length
;
382 tstats
->tx_packets
++;
383 u64_stats_update_end(&tstats
->syncp
);
386 stats
->tx_aborted_errors
++;
391 stats
->tx_carrier_errors
++;
392 dst_link_failure(skb
);
398 static netdev_tx_t
xfrmi_xmit(struct sk_buff
*skb
, struct net_device
*dev
)
400 struct xfrm_if
*xi
= netdev_priv(dev
);
401 struct net_device_stats
*stats
= &xi
->dev
->stats
;
405 memset(&fl
, 0, sizeof(fl
));
407 switch (skb
->protocol
) {
408 case htons(ETH_P_IPV6
):
409 xfrm_decode_session(skb
, &fl
, AF_INET6
);
410 memset(IP6CB(skb
), 0, sizeof(*IP6CB(skb
)));
412 case htons(ETH_P_IP
):
413 xfrm_decode_session(skb
, &fl
, AF_INET
);
414 memset(IPCB(skb
), 0, sizeof(*IPCB(skb
)));
420 fl
.flowi_oif
= xi
->phydev
->ifindex
;
422 ret
= xfrmi_xmit2(skb
, dev
, &fl
);
435 static int xfrmi4_err(struct sk_buff
*skb
, u32 info
)
437 const struct iphdr
*iph
= (const struct iphdr
*)skb
->data
;
438 struct net
*net
= dev_net(skb
->dev
);
439 int protocol
= iph
->protocol
;
440 struct ip_comp_hdr
*ipch
;
441 struct ip_esp_hdr
*esph
;
442 struct ip_auth_hdr
*ah
;
443 struct xfrm_state
*x
;
449 esph
= (struct ip_esp_hdr
*)(skb
->data
+(iph
->ihl
<<2));
453 ah
= (struct ip_auth_hdr
*)(skb
->data
+(iph
->ihl
<<2));
457 ipch
= (struct ip_comp_hdr
*)(skb
->data
+(iph
->ihl
<<2));
458 spi
= htonl(ntohs(ipch
->cpi
));
464 switch (icmp_hdr(skb
)->type
) {
465 case ICMP_DEST_UNREACH
:
466 if (icmp_hdr(skb
)->code
!= ICMP_FRAG_NEEDED
)
474 x
= xfrm_state_lookup(net
, skb
->mark
, (const xfrm_address_t
*)&iph
->daddr
,
475 spi
, protocol
, AF_INET
);
479 xi
= xfrmi_lookup(net
, x
);
485 if (icmp_hdr(skb
)->type
== ICMP_DEST_UNREACH
)
486 ipv4_update_pmtu(skb
, net
, info
, 0, protocol
);
488 ipv4_redirect(skb
, net
, 0, protocol
);
494 static int xfrmi6_err(struct sk_buff
*skb
, struct inet6_skb_parm
*opt
,
495 u8 type
, u8 code
, int offset
, __be32 info
)
497 const struct ipv6hdr
*iph
= (const struct ipv6hdr
*)skb
->data
;
498 struct net
*net
= dev_net(skb
->dev
);
499 int protocol
= iph
->nexthdr
;
500 struct ip_comp_hdr
*ipch
;
501 struct ip_esp_hdr
*esph
;
502 struct ip_auth_hdr
*ah
;
503 struct xfrm_state
*x
;
509 esph
= (struct ip_esp_hdr
*)(skb
->data
+ offset
);
513 ah
= (struct ip_auth_hdr
*)(skb
->data
+ offset
);
517 ipch
= (struct ip_comp_hdr
*)(skb
->data
+ offset
);
518 spi
= htonl(ntohs(ipch
->cpi
));
524 if (type
!= ICMPV6_PKT_TOOBIG
&&
525 type
!= NDISC_REDIRECT
)
528 x
= xfrm_state_lookup(net
, skb
->mark
, (const xfrm_address_t
*)&iph
->daddr
,
529 spi
, protocol
, AF_INET6
);
533 xi
= xfrmi_lookup(net
, x
);
539 if (type
== NDISC_REDIRECT
)
540 ip6_redirect(skb
, net
, skb
->dev
->ifindex
, 0,
541 sock_net_uid(net
, NULL
));
543 ip6_update_pmtu(skb
, net
, info
, 0, 0, sock_net_uid(net
, NULL
));
549 static int xfrmi_change(struct xfrm_if
*xi
, const struct xfrm_if_parms
*p
)
551 if (xi
->p
.link
!= p
->link
)
554 xi
->p
.if_id
= p
->if_id
;
559 static int xfrmi_update(struct xfrm_if
*xi
, struct xfrm_if_parms
*p
)
561 struct net
*net
= dev_net(xi
->dev
);
562 struct xfrmi_net
*xfrmn
= net_generic(net
, xfrmi_net_id
);
565 xfrmi_unlink(xfrmn
, xi
);
567 err
= xfrmi_change(xi
, p
);
568 xfrmi_link(xfrmn
, xi
);
569 netdev_state_change(xi
->dev
);
573 static void xfrmi_get_stats64(struct net_device
*dev
,
574 struct rtnl_link_stats64
*s
)
578 for_each_possible_cpu(cpu
) {
579 struct pcpu_sw_netstats
*stats
;
580 struct pcpu_sw_netstats tmp
;
583 stats
= per_cpu_ptr(dev
->tstats
, cpu
);
585 start
= u64_stats_fetch_begin_irq(&stats
->syncp
);
586 tmp
.rx_packets
= stats
->rx_packets
;
587 tmp
.rx_bytes
= stats
->rx_bytes
;
588 tmp
.tx_packets
= stats
->tx_packets
;
589 tmp
.tx_bytes
= stats
->tx_bytes
;
590 } while (u64_stats_fetch_retry_irq(&stats
->syncp
, start
));
592 s
->rx_packets
+= tmp
.rx_packets
;
593 s
->rx_bytes
+= tmp
.rx_bytes
;
594 s
->tx_packets
+= tmp
.tx_packets
;
595 s
->tx_bytes
+= tmp
.tx_bytes
;
598 s
->rx_dropped
= dev
->stats
.rx_dropped
;
599 s
->tx_dropped
= dev
->stats
.tx_dropped
;
602 static int xfrmi_get_iflink(const struct net_device
*dev
)
604 struct xfrm_if
*xi
= netdev_priv(dev
);
606 return xi
->phydev
->ifindex
;
610 static const struct net_device_ops xfrmi_netdev_ops
= {
611 .ndo_init
= xfrmi_dev_init
,
612 .ndo_uninit
= xfrmi_dev_uninit
,
613 .ndo_start_xmit
= xfrmi_xmit
,
614 .ndo_get_stats64
= xfrmi_get_stats64
,
615 .ndo_get_iflink
= xfrmi_get_iflink
,
618 static void xfrmi_dev_setup(struct net_device
*dev
)
620 dev
->netdev_ops
= &xfrmi_netdev_ops
;
621 dev
->type
= ARPHRD_NONE
;
622 dev
->hard_header_len
= ETH_HLEN
;
623 dev
->min_header_len
= ETH_HLEN
;
624 dev
->mtu
= ETH_DATA_LEN
;
625 dev
->min_mtu
= ETH_MIN_MTU
;
626 dev
->max_mtu
= ETH_DATA_LEN
;
627 dev
->addr_len
= ETH_ALEN
;
628 dev
->flags
= IFF_NOARP
;
629 dev
->needs_free_netdev
= true;
630 dev
->priv_destructor
= xfrmi_dev_free
;
634 static int xfrmi_dev_init(struct net_device
*dev
)
636 struct xfrm_if
*xi
= netdev_priv(dev
);
637 struct net_device
*phydev
= xi
->phydev
;
640 dev
->tstats
= netdev_alloc_pcpu_stats(struct pcpu_sw_netstats
);
644 err
= gro_cells_init(&xi
->gro_cells
, dev
);
646 free_percpu(dev
->tstats
);
650 dev
->features
|= NETIF_F_LLTX
;
652 dev
->needed_headroom
= phydev
->needed_headroom
;
653 dev
->needed_tailroom
= phydev
->needed_tailroom
;
655 if (is_zero_ether_addr(dev
->dev_addr
))
656 eth_hw_addr_inherit(dev
, phydev
);
657 if (is_zero_ether_addr(dev
->broadcast
))
658 memcpy(dev
->broadcast
, phydev
->broadcast
, dev
->addr_len
);
663 static int xfrmi_validate(struct nlattr
*tb
[], struct nlattr
*data
[],
664 struct netlink_ext_ack
*extack
)
669 static void xfrmi_netlink_parms(struct nlattr
*data
[],
670 struct xfrm_if_parms
*parms
)
672 memset(parms
, 0, sizeof(*parms
));
677 if (data
[IFLA_XFRM_LINK
])
678 parms
->link
= nla_get_u32(data
[IFLA_XFRM_LINK
]);
680 if (data
[IFLA_XFRM_IF_ID
])
681 parms
->if_id
= nla_get_u32(data
[IFLA_XFRM_IF_ID
]);
684 static int xfrmi_newlink(struct net
*src_net
, struct net_device
*dev
,
685 struct nlattr
*tb
[], struct nlattr
*data
[],
686 struct netlink_ext_ack
*extack
)
688 struct net
*net
= dev_net(dev
);
689 struct xfrm_if_parms
*p
;
692 xi
= netdev_priv(dev
);
695 xfrmi_netlink_parms(data
, p
);
697 if (!tb
[IFLA_IFNAME
])
700 nla_strlcpy(p
->name
, tb
[IFLA_IFNAME
], IFNAMSIZ
);
702 xi
= xfrmi_locate(net
, p
, 1);
703 return PTR_ERR_OR_ZERO(xi
);
706 static void xfrmi_dellink(struct net_device
*dev
, struct list_head
*head
)
708 unregister_netdevice_queue(dev
, head
);
711 static int xfrmi_changelink(struct net_device
*dev
, struct nlattr
*tb
[],
712 struct nlattr
*data
[],
713 struct netlink_ext_ack
*extack
)
715 struct xfrm_if
*xi
= netdev_priv(dev
);
716 struct net
*net
= dev_net(dev
);
718 xfrmi_netlink_parms(data
, &xi
->p
);
720 xi
= xfrmi_locate(net
, &xi
->p
, 0);
722 if (IS_ERR_OR_NULL(xi
)) {
723 xi
= netdev_priv(dev
);
729 return xfrmi_update(xi
, &xi
->p
);
732 static size_t xfrmi_get_size(const struct net_device
*dev
)
737 /* IFLA_XFRM_IF_ID */
742 static int xfrmi_fill_info(struct sk_buff
*skb
, const struct net_device
*dev
)
744 struct xfrm_if
*xi
= netdev_priv(dev
);
745 struct xfrm_if_parms
*parm
= &xi
->p
;
747 if (nla_put_u32(skb
, IFLA_XFRM_LINK
, parm
->link
) ||
748 nla_put_u32(skb
, IFLA_XFRM_IF_ID
, parm
->if_id
))
749 goto nla_put_failure
;
756 static struct net
*xfrmi_get_link_net(const struct net_device
*dev
)
758 struct xfrm_if
*xi
= netdev_priv(dev
);
760 return dev_net(xi
->phydev
);
763 static const struct nla_policy xfrmi_policy
[IFLA_XFRM_MAX
+ 1] = {
764 [IFLA_XFRM_LINK
] = { .type
= NLA_U32
},
765 [IFLA_XFRM_IF_ID
] = { .type
= NLA_U32
},
768 static struct rtnl_link_ops xfrmi_link_ops __read_mostly
= {
770 .maxtype
= IFLA_XFRM_MAX
,
771 .policy
= xfrmi_policy
,
772 .priv_size
= sizeof(struct xfrm_if
),
773 .setup
= xfrmi_dev_setup
,
774 .validate
= xfrmi_validate
,
775 .newlink
= xfrmi_newlink
,
776 .dellink
= xfrmi_dellink
,
777 .changelink
= xfrmi_changelink
,
778 .get_size
= xfrmi_get_size
,
779 .fill_info
= xfrmi_fill_info
,
780 .get_link_net
= xfrmi_get_link_net
,
783 static void __net_exit
xfrmi_destroy_interfaces(struct xfrmi_net
*xfrmn
)
788 xi
= rtnl_dereference(xfrmn
->xfrmi
[0]);
792 unregister_netdevice_queue(xi
->dev
, &list
);
793 unregister_netdevice_many(&list
);
796 static int __net_init
xfrmi_init_net(struct net
*net
)
801 static void __net_exit
xfrmi_exit_net(struct net
*net
)
803 struct xfrmi_net
*xfrmn
= net_generic(net
, xfrmi_net_id
);
806 xfrmi_destroy_interfaces(xfrmn
);
810 static struct pernet_operations xfrmi_net_ops
= {
811 .init
= xfrmi_init_net
,
812 .exit
= xfrmi_exit_net
,
814 .size
= sizeof(struct xfrmi_net
),
817 static struct xfrm6_protocol xfrmi_esp6_protocol __read_mostly
= {
818 .handler
= xfrm6_rcv
,
819 .cb_handler
= xfrmi_rcv_cb
,
820 .err_handler
= xfrmi6_err
,
824 static struct xfrm6_protocol xfrmi_ah6_protocol __read_mostly
= {
825 .handler
= xfrm6_rcv
,
826 .cb_handler
= xfrmi_rcv_cb
,
827 .err_handler
= xfrmi6_err
,
831 static struct xfrm6_protocol xfrmi_ipcomp6_protocol __read_mostly
= {
832 .handler
= xfrm6_rcv
,
833 .cb_handler
= xfrmi_rcv_cb
,
834 .err_handler
= xfrmi6_err
,
838 static struct xfrm4_protocol xfrmi_esp4_protocol __read_mostly
= {
839 .handler
= xfrm4_rcv
,
840 .input_handler
= xfrm_input
,
841 .cb_handler
= xfrmi_rcv_cb
,
842 .err_handler
= xfrmi4_err
,
846 static struct xfrm4_protocol xfrmi_ah4_protocol __read_mostly
= {
847 .handler
= xfrm4_rcv
,
848 .input_handler
= xfrm_input
,
849 .cb_handler
= xfrmi_rcv_cb
,
850 .err_handler
= xfrmi4_err
,
854 static struct xfrm4_protocol xfrmi_ipcomp4_protocol __read_mostly
= {
855 .handler
= xfrm4_rcv
,
856 .input_handler
= xfrm_input
,
857 .cb_handler
= xfrmi_rcv_cb
,
858 .err_handler
= xfrmi4_err
,
862 static int __init
xfrmi4_init(void)
866 err
= xfrm4_protocol_register(&xfrmi_esp4_protocol
, IPPROTO_ESP
);
868 goto xfrm_proto_esp_failed
;
869 err
= xfrm4_protocol_register(&xfrmi_ah4_protocol
, IPPROTO_AH
);
871 goto xfrm_proto_ah_failed
;
872 err
= xfrm4_protocol_register(&xfrmi_ipcomp4_protocol
, IPPROTO_COMP
);
874 goto xfrm_proto_comp_failed
;
878 xfrm_proto_comp_failed
:
879 xfrm4_protocol_deregister(&xfrmi_ah4_protocol
, IPPROTO_AH
);
880 xfrm_proto_ah_failed
:
881 xfrm4_protocol_deregister(&xfrmi_esp4_protocol
, IPPROTO_ESP
);
882 xfrm_proto_esp_failed
:
886 static void xfrmi4_fini(void)
888 xfrm4_protocol_deregister(&xfrmi_ipcomp4_protocol
, IPPROTO_COMP
);
889 xfrm4_protocol_deregister(&xfrmi_ah4_protocol
, IPPROTO_AH
);
890 xfrm4_protocol_deregister(&xfrmi_esp4_protocol
, IPPROTO_ESP
);
893 static int __init
xfrmi6_init(void)
897 err
= xfrm6_protocol_register(&xfrmi_esp6_protocol
, IPPROTO_ESP
);
899 goto xfrm_proto_esp_failed
;
900 err
= xfrm6_protocol_register(&xfrmi_ah6_protocol
, IPPROTO_AH
);
902 goto xfrm_proto_ah_failed
;
903 err
= xfrm6_protocol_register(&xfrmi_ipcomp6_protocol
, IPPROTO_COMP
);
905 goto xfrm_proto_comp_failed
;
909 xfrm_proto_comp_failed
:
910 xfrm6_protocol_deregister(&xfrmi_ah6_protocol
, IPPROTO_AH
);
911 xfrm_proto_ah_failed
:
912 xfrm6_protocol_deregister(&xfrmi_esp6_protocol
, IPPROTO_ESP
);
913 xfrm_proto_esp_failed
:
917 static void xfrmi6_fini(void)
919 xfrm6_protocol_deregister(&xfrmi_ipcomp6_protocol
, IPPROTO_COMP
);
920 xfrm6_protocol_deregister(&xfrmi_ah6_protocol
, IPPROTO_AH
);
921 xfrm6_protocol_deregister(&xfrmi_esp6_protocol
, IPPROTO_ESP
);
924 static const struct xfrm_if_cb xfrm_if_cb
= {
925 .decode_session
= xfrmi_decode_session
,
928 static int __init
xfrmi_init(void)
933 pr_info("IPsec XFRM device driver\n");
935 msg
= "tunnel device";
936 err
= register_pernet_device(&xfrmi_net_ops
);
938 goto pernet_dev_failed
;
940 msg
= "xfrm4 protocols";
945 msg
= "xfrm6 protocols";
951 msg
= "netlink interface";
952 err
= rtnl_link_register(&xfrmi_link_ops
);
954 goto rtnl_link_failed
;
956 xfrm_if_register_cb(&xfrm_if_cb
);
965 unregister_pernet_device(&xfrmi_net_ops
);
967 pr_err("xfrmi init: failed to register %s\n", msg
);
971 static void __exit
xfrmi_fini(void)
973 xfrm_if_unregister_cb();
974 rtnl_link_unregister(&xfrmi_link_ops
);
977 unregister_pernet_device(&xfrmi_net_ops
);
980 module_init(xfrmi_init
);
981 module_exit(xfrmi_fini
);
982 MODULE_LICENSE("GPL");
983 MODULE_ALIAS_RTNL_LINK("xfrm");
984 MODULE_ALIAS_NETDEV("xfrm0");
985 MODULE_AUTHOR("Steffen Klassert");
986 MODULE_DESCRIPTION("XFRM virtual interface");