^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) /* xfrm4_protocol.c - Generic xfrm protocol multiplexer.
^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/tunnel4.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 <net/icmp.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include <net/ip.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include <net/protocol.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #include <net/xfrm.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) static struct xfrm4_protocol __rcu *esp4_handlers __read_mostly;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) static struct xfrm4_protocol __rcu *ah4_handlers __read_mostly;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) static struct xfrm4_protocol __rcu *ipcomp4_handlers __read_mostly;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) static DEFINE_MUTEX(xfrm4_protocol_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) static inline struct xfrm4_protocol __rcu **proto_handlers(u8 protocol)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) switch (protocol) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) case IPPROTO_ESP:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) return &esp4_handlers;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) case IPPROTO_AH:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) return &ah4_handlers;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) case IPPROTO_COMP:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) return &ipcomp4_handlers;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) #define for_each_protocol_rcu(head, handler) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) for (handler = rcu_dereference(head); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) handler != NULL; \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) handler = rcu_dereference(handler->next)) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) static int xfrm4_rcv_cb(struct sk_buff *skb, u8 protocol, int err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) struct xfrm4_protocol *handler;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) struct xfrm4_protocol __rcu **head = proto_handlers(protocol);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) if (!head)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) for_each_protocol_rcu(*head, handler)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) if ((ret = handler->cb_handler(skb, err)) <= 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) int xfrm4_rcv_encap(struct sk_buff *skb, int nexthdr, __be32 spi,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) int encap_type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) struct xfrm4_protocol *handler;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) struct xfrm4_protocol __rcu **head = proto_handlers(nexthdr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) XFRM_TUNNEL_SKB_CB(skb)->tunnel.ip4 = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) XFRM_SPI_SKB_CB(skb)->family = AF_INET;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) XFRM_SPI_SKB_CB(skb)->daddroff = offsetof(struct iphdr, daddr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) if (!head)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) if (!skb_dst(skb)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) const struct iphdr *iph = ip_hdr(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) if (ip_route_input_noref(skb, iph->daddr, iph->saddr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) iph->tos, skb->dev))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) goto drop;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) for_each_protocol_rcu(*head, handler)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) if ((ret = handler->input_handler(skb, nexthdr, spi, encap_type)) != -EINVAL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) icmp_send(skb, ICMP_DEST_UNREACH, ICMP_PORT_UNREACH, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) drop:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) kfree_skb(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) EXPORT_SYMBOL(xfrm4_rcv_encap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) static int xfrm4_esp_rcv(struct sk_buff *skb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) struct xfrm4_protocol *handler;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) XFRM_TUNNEL_SKB_CB(skb)->tunnel.ip4 = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) for_each_protocol_rcu(esp4_handlers, handler)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) if ((ret = handler->handler(skb)) != -EINVAL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) icmp_send(skb, ICMP_DEST_UNREACH, ICMP_PORT_UNREACH, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) kfree_skb(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) static int xfrm4_esp_err(struct sk_buff *skb, u32 info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) struct xfrm4_protocol *handler;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) for_each_protocol_rcu(esp4_handlers, handler)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) if (!handler->err_handler(skb, info))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) return -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) static int xfrm4_ah_rcv(struct sk_buff *skb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) struct xfrm4_protocol *handler;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) XFRM_TUNNEL_SKB_CB(skb)->tunnel.ip4 = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) for_each_protocol_rcu(ah4_handlers, handler)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) if ((ret = handler->handler(skb)) != -EINVAL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) icmp_send(skb, ICMP_DEST_UNREACH, ICMP_PORT_UNREACH, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) kfree_skb(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) static int xfrm4_ah_err(struct sk_buff *skb, u32 info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) struct xfrm4_protocol *handler;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) for_each_protocol_rcu(ah4_handlers, handler)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) if (!handler->err_handler(skb, info))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) return -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) static int xfrm4_ipcomp_rcv(struct sk_buff *skb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) struct xfrm4_protocol *handler;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) XFRM_TUNNEL_SKB_CB(skb)->tunnel.ip4 = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) for_each_protocol_rcu(ipcomp4_handlers, handler)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) if ((ret = handler->handler(skb)) != -EINVAL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) icmp_send(skb, ICMP_DEST_UNREACH, ICMP_PORT_UNREACH, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) kfree_skb(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) static int xfrm4_ipcomp_err(struct sk_buff *skb, u32 info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) struct xfrm4_protocol *handler;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) for_each_protocol_rcu(ipcomp4_handlers, handler)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) if (!handler->err_handler(skb, info))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) return -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) static const struct net_protocol esp4_protocol = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) .handler = xfrm4_esp_rcv,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) .err_handler = xfrm4_esp_err,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) .no_policy = 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) .netns_ok = 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) static const struct net_protocol ah4_protocol = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) .handler = xfrm4_ah_rcv,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) .err_handler = xfrm4_ah_err,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) .no_policy = 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) .netns_ok = 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) static const struct net_protocol ipcomp4_protocol = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) .handler = xfrm4_ipcomp_rcv,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) .err_handler = xfrm4_ipcomp_err,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) .no_policy = 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) .netns_ok = 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) static const struct xfrm_input_afinfo xfrm4_input_afinfo = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) .family = AF_INET,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) .callback = xfrm4_rcv_cb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) static inline const struct net_protocol *netproto(unsigned char protocol)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) switch (protocol) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) case IPPROTO_ESP:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) return &esp4_protocol;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) case IPPROTO_AH:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) return &ah4_protocol;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) case IPPROTO_COMP:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) return &ipcomp4_protocol;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) int xfrm4_protocol_register(struct xfrm4_protocol *handler,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) unsigned char protocol)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) struct xfrm4_protocol __rcu **pprev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) struct xfrm4_protocol *t;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) bool add_netproto = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) int ret = -EEXIST;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) int priority = handler->priority;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) if (!proto_handlers(protocol) || !netproto(protocol))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) mutex_lock(&xfrm4_protocol_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) if (!rcu_dereference_protected(*proto_handlers(protocol),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) lockdep_is_held(&xfrm4_protocol_mutex)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) add_netproto = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) for (pprev = proto_handlers(protocol);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) (t = rcu_dereference_protected(*pprev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) lockdep_is_held(&xfrm4_protocol_mutex))) != NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) pprev = &t->next) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) if (t->priority < priority)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) if (t->priority == priority)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) handler->next = *pprev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) rcu_assign_pointer(*pprev, handler);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) err:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) mutex_unlock(&xfrm4_protocol_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) if (add_netproto) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) if (inet_add_protocol(netproto(protocol), protocol)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) pr_err("%s: can't add protocol\n", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) ret = -EAGAIN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) EXPORT_SYMBOL(xfrm4_protocol_register);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) int xfrm4_protocol_deregister(struct xfrm4_protocol *handler,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) unsigned char protocol)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) struct xfrm4_protocol __rcu **pprev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) struct xfrm4_protocol *t;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) int ret = -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) if (!proto_handlers(protocol) || !netproto(protocol))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) mutex_lock(&xfrm4_protocol_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) for (pprev = proto_handlers(protocol);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) (t = rcu_dereference_protected(*pprev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) lockdep_is_held(&xfrm4_protocol_mutex))) != NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) pprev = &t->next) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) if (t == handler) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) *pprev = handler->next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) if (!rcu_dereference_protected(*proto_handlers(protocol),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) lockdep_is_held(&xfrm4_protocol_mutex))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) if (inet_del_protocol(netproto(protocol), protocol) < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) pr_err("%s: can't remove protocol\n", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) ret = -EAGAIN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) mutex_unlock(&xfrm4_protocol_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) synchronize_net();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) EXPORT_SYMBOL(xfrm4_protocol_deregister);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) void __init xfrm4_protocol_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) xfrm_input_register_afinfo(&xfrm4_input_afinfo);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) EXPORT_SYMBOL(xfrm4_protocol_init);