^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-or-later
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /* xfrm6_protocol.c - Generic xfrm protocol multiplexer for ipv6.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) * Copyright (C) 2013 secunet Security Networks AG
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) * Author:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) * Steffen Klassert <steffen.klassert@secunet.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) * Based on:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) * net/ipv4/xfrm4_protocol.c
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/mutex.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <linux/skbuff.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <linux/icmpv6.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include <net/ip6_route.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include <net/ipv6.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #include <net/protocol.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #include <net/xfrm.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) static struct xfrm6_protocol __rcu *esp6_handlers __read_mostly;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) static struct xfrm6_protocol __rcu *ah6_handlers __read_mostly;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) static struct xfrm6_protocol __rcu *ipcomp6_handlers __read_mostly;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) static DEFINE_MUTEX(xfrm6_protocol_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) static inline struct xfrm6_protocol __rcu **proto_handlers(u8 protocol)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) switch (protocol) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) case IPPROTO_ESP:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) return &esp6_handlers;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) case IPPROTO_AH:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) return &ah6_handlers;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) case IPPROTO_COMP:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) return &ipcomp6_handlers;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) #define for_each_protocol_rcu(head, handler) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) for (handler = rcu_dereference(head); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) handler != NULL; \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) handler = rcu_dereference(handler->next)) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) static int xfrm6_rcv_cb(struct sk_buff *skb, u8 protocol, int err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) struct xfrm6_protocol *handler;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) struct xfrm6_protocol __rcu **head = proto_handlers(protocol);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) if (!head)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) for_each_protocol_rcu(*proto_handlers(protocol), handler)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) if ((ret = handler->cb_handler(skb, err)) <= 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) int xfrm6_rcv_encap(struct sk_buff *skb, int nexthdr, __be32 spi,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) int encap_type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) struct xfrm6_protocol *handler;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) struct xfrm6_protocol __rcu **head = proto_handlers(nexthdr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) XFRM_TUNNEL_SKB_CB(skb)->tunnel.ip6 = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) XFRM_SPI_SKB_CB(skb)->family = AF_INET6;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) XFRM_SPI_SKB_CB(skb)->daddroff = offsetof(struct ipv6hdr, daddr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) if (!head)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) if (!skb_dst(skb)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) const struct ipv6hdr *ip6h = ipv6_hdr(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) int flags = RT6_LOOKUP_F_HAS_SADDR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) struct dst_entry *dst;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) struct flowi6 fl6 = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) .flowi6_iif = skb->dev->ifindex,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) .daddr = ip6h->daddr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) .saddr = ip6h->saddr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) .flowlabel = ip6_flowinfo(ip6h),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) .flowi6_mark = skb->mark,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) .flowi6_proto = ip6h->nexthdr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) dst = ip6_route_input_lookup(dev_net(skb->dev), skb->dev, &fl6,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) skb, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) if (dst->error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) goto drop;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) skb_dst_set(skb, dst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) for_each_protocol_rcu(*head, handler)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) if ((ret = handler->input_handler(skb, nexthdr, spi, encap_type)) != -EINVAL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_PORT_UNREACH, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) drop:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) kfree_skb(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) EXPORT_SYMBOL(xfrm6_rcv_encap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) static int xfrm6_esp_rcv(struct sk_buff *skb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) struct xfrm6_protocol *handler;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) XFRM_TUNNEL_SKB_CB(skb)->tunnel.ip6 = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) for_each_protocol_rcu(esp6_handlers, handler)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) if ((ret = handler->handler(skb)) != -EINVAL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_PORT_UNREACH, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) kfree_skb(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) static int xfrm6_esp_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) u8 type, u8 code, int offset, __be32 info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) struct xfrm6_protocol *handler;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) for_each_protocol_rcu(esp6_handlers, handler)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) if (!handler->err_handler(skb, opt, type, code, offset, info))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) return -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) static int xfrm6_ah_rcv(struct sk_buff *skb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) struct xfrm6_protocol *handler;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) XFRM_TUNNEL_SKB_CB(skb)->tunnel.ip6 = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) for_each_protocol_rcu(ah6_handlers, handler)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) if ((ret = handler->handler(skb)) != -EINVAL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_PORT_UNREACH, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) kfree_skb(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) static int xfrm6_ah_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) u8 type, u8 code, int offset, __be32 info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) struct xfrm6_protocol *handler;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) for_each_protocol_rcu(ah6_handlers, handler)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) if (!handler->err_handler(skb, opt, type, code, offset, info))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) return -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) static int xfrm6_ipcomp_rcv(struct sk_buff *skb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) struct xfrm6_protocol *handler;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) XFRM_TUNNEL_SKB_CB(skb)->tunnel.ip6 = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) for_each_protocol_rcu(ipcomp6_handlers, handler)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) if ((ret = handler->handler(skb)) != -EINVAL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_PORT_UNREACH, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) kfree_skb(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) static int xfrm6_ipcomp_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) u8 type, u8 code, int offset, __be32 info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) struct xfrm6_protocol *handler;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) for_each_protocol_rcu(ipcomp6_handlers, handler)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) if (!handler->err_handler(skb, opt, type, code, offset, info))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) return -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) static const struct inet6_protocol esp6_protocol = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) .handler = xfrm6_esp_rcv,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) .err_handler = xfrm6_esp_err,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) .flags = INET6_PROTO_NOPOLICY,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) static const struct inet6_protocol ah6_protocol = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) .handler = xfrm6_ah_rcv,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) .err_handler = xfrm6_ah_err,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) .flags = INET6_PROTO_NOPOLICY,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) static const struct inet6_protocol ipcomp6_protocol = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) .handler = xfrm6_ipcomp_rcv,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) .err_handler = xfrm6_ipcomp_err,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) .flags = INET6_PROTO_NOPOLICY,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) static const struct xfrm_input_afinfo xfrm6_input_afinfo = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) .family = AF_INET6,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) .callback = xfrm6_rcv_cb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) static inline const struct inet6_protocol *netproto(unsigned char protocol)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) switch (protocol) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) case IPPROTO_ESP:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) return &esp6_protocol;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) case IPPROTO_AH:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) return &ah6_protocol;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) case IPPROTO_COMP:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) return &ipcomp6_protocol;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) int xfrm6_protocol_register(struct xfrm6_protocol *handler,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) unsigned char protocol)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) struct xfrm6_protocol __rcu **pprev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) struct xfrm6_protocol *t;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) bool add_netproto = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) int ret = -EEXIST;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) int priority = handler->priority;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) if (!proto_handlers(protocol) || !netproto(protocol))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) mutex_lock(&xfrm6_protocol_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) if (!rcu_dereference_protected(*proto_handlers(protocol),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) lockdep_is_held(&xfrm6_protocol_mutex)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) add_netproto = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) for (pprev = proto_handlers(protocol);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) (t = rcu_dereference_protected(*pprev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) lockdep_is_held(&xfrm6_protocol_mutex))) != NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) pprev = &t->next) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) if (t->priority < priority)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) if (t->priority == priority)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) handler->next = *pprev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) rcu_assign_pointer(*pprev, handler);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) err:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) mutex_unlock(&xfrm6_protocol_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) if (add_netproto) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) if (inet6_add_protocol(netproto(protocol), protocol)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) pr_err("%s: can't add protocol\n", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) ret = -EAGAIN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) EXPORT_SYMBOL(xfrm6_protocol_register);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) int xfrm6_protocol_deregister(struct xfrm6_protocol *handler,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) unsigned char protocol)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) struct xfrm6_protocol __rcu **pprev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) struct xfrm6_protocol *t;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) int ret = -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) if (!proto_handlers(protocol) || !netproto(protocol))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) mutex_lock(&xfrm6_protocol_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) for (pprev = proto_handlers(protocol);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) (t = rcu_dereference_protected(*pprev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) lockdep_is_held(&xfrm6_protocol_mutex))) != NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) pprev = &t->next) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) if (t == handler) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) *pprev = handler->next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) if (!rcu_dereference_protected(*proto_handlers(protocol),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) lockdep_is_held(&xfrm6_protocol_mutex))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) if (inet6_del_protocol(netproto(protocol), protocol) < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) pr_err("%s: can't remove protocol\n", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) ret = -EAGAIN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) mutex_unlock(&xfrm6_protocol_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) synchronize_net();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) EXPORT_SYMBOL(xfrm6_protocol_deregister);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) int __init xfrm6_protocol_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) return xfrm_input_register_afinfo(&xfrm6_input_afinfo);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) void xfrm6_protocol_fini(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) xfrm_input_unregister_afinfo(&xfrm6_input_afinfo);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) }