^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /* Multipath TCP
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) * Copyright (c) 2020, Red Hat, Inc.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) #define pr_fmt(fmt) "MPTCP: " fmt
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <linux/inet.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <net/tcp.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <net/netns/generic.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <net/mptcp.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <net/genetlink.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <uapi/linux/mptcp.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include "protocol.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include "mib.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) /* forward declaration */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) static struct genl_family mptcp_genl_family;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) static int pm_nl_pernet_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) struct mptcp_pm_addr_entry {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) struct list_head list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) struct mptcp_addr_info addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) struct rcu_head rcu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) struct mptcp_pm_add_entry {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) struct list_head list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) struct mptcp_addr_info addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) struct timer_list add_timer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) struct mptcp_sock *sock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) u8 retrans_times;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) struct pm_nl_pernet {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) /* protects pernet updates */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) spinlock_t lock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) struct list_head local_addr_list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) unsigned int addrs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) unsigned int add_addr_signal_max;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) unsigned int add_addr_accept_max;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) unsigned int local_addr_max;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) unsigned int subflows_max;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) unsigned int next_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) #define MPTCP_PM_ADDR_MAX 8
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) #define ADD_ADDR_RETRANS_MAX 3
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) static bool addresses_equal(const struct mptcp_addr_info *a,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) struct mptcp_addr_info *b, bool use_port)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) bool addr_equals = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) if (a->family != b->family)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) if (a->family == AF_INET)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) addr_equals = a->addr.s_addr == b->addr.s_addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) #if IS_ENABLED(CONFIG_MPTCP_IPV6)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) addr_equals = !ipv6_addr_cmp(&a->addr6, &b->addr6);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) if (!addr_equals)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) if (!use_port)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) return a->port == b->port;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) static bool address_zero(const struct mptcp_addr_info *addr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) struct mptcp_addr_info zero;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) memset(&zero, 0, sizeof(zero));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) zero.family = addr->family;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) return addresses_equal(addr, &zero, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) static void local_address(const struct sock_common *skc,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) struct mptcp_addr_info *addr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) addr->port = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) addr->family = skc->skc_family;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) if (addr->family == AF_INET)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) addr->addr.s_addr = skc->skc_rcv_saddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) #if IS_ENABLED(CONFIG_MPTCP_IPV6)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) else if (addr->family == AF_INET6)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) addr->addr6 = skc->skc_v6_rcv_saddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) static void remote_address(const struct sock_common *skc,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) struct mptcp_addr_info *addr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) addr->family = skc->skc_family;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) addr->port = skc->skc_dport;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) if (addr->family == AF_INET)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) addr->addr.s_addr = skc->skc_daddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) #if IS_ENABLED(CONFIG_MPTCP_IPV6)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) else if (addr->family == AF_INET6)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) addr->addr6 = skc->skc_v6_daddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) #endif
^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 bool lookup_subflow_by_saddr(const struct list_head *list,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) struct mptcp_addr_info *saddr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) struct mptcp_subflow_context *subflow;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) struct mptcp_addr_info cur;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) struct sock_common *skc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) list_for_each_entry(subflow, list, node) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) skc = (struct sock_common *)mptcp_subflow_tcp_sock(subflow);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) local_address(skc, &cur);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) if (addresses_equal(&cur, saddr, false))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) static struct mptcp_pm_addr_entry *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) select_local_address(const struct pm_nl_pernet *pernet,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) struct mptcp_sock *msk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) struct mptcp_pm_addr_entry *entry, *ret = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) rcu_read_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) spin_lock_bh(&msk->join_list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) list_for_each_entry_rcu(entry, &pernet->local_addr_list, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) if (!(entry->addr.flags & MPTCP_PM_ADDR_FLAG_SUBFLOW))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) /* avoid any address already in use by subflows and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) * pending join
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) if (entry->addr.family == ((struct sock *)msk)->sk_family &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) !lookup_subflow_by_saddr(&msk->conn_list, &entry->addr) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) !lookup_subflow_by_saddr(&msk->join_list, &entry->addr)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) ret = entry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) spin_unlock_bh(&msk->join_list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) rcu_read_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) static struct mptcp_pm_addr_entry *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) select_signal_address(struct pm_nl_pernet *pernet, unsigned int pos)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) struct mptcp_pm_addr_entry *entry, *ret = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) int i = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) rcu_read_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) /* do not keep any additional per socket state, just signal
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) * the address list in order.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) * Note: removal from the local address list during the msk life-cycle
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) * can lead to additional addresses not being announced.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) list_for_each_entry_rcu(entry, &pernet->local_addr_list, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) if (!(entry->addr.flags & MPTCP_PM_ADDR_FLAG_SIGNAL))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) if (i++ == pos) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) ret = entry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) rcu_read_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) static void check_work_pending(struct mptcp_sock *msk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) if (msk->pm.add_addr_signaled == msk->pm.add_addr_signal_max &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) (msk->pm.local_addr_used == msk->pm.local_addr_max ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) msk->pm.subflows == msk->pm.subflows_max))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) WRITE_ONCE(msk->pm.work_pending, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) static struct mptcp_pm_add_entry *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) lookup_anno_list_by_saddr(struct mptcp_sock *msk,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) struct mptcp_addr_info *addr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) struct mptcp_pm_add_entry *entry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) list_for_each_entry(entry, &msk->pm.anno_list, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) if (addresses_equal(&entry->addr, addr, false))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) return entry;
^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) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) static void mptcp_pm_add_timer(struct timer_list *timer)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) struct mptcp_pm_add_entry *entry = from_timer(entry, timer, add_timer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) struct mptcp_sock *msk = entry->sock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) struct sock *sk = (struct sock *)msk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) pr_debug("msk=%p", msk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) if (!msk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) if (inet_sk_state_load(sk) == TCP_CLOSE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) if (!entry->addr.id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) if (mptcp_pm_should_add_signal(msk)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) sk_reset_timer(sk, timer, jiffies + TCP_RTO_MAX / 8);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) spin_lock_bh(&msk->pm.lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) if (!mptcp_pm_should_add_signal(msk)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) pr_debug("retransmit ADD_ADDR id=%d", entry->addr.id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) mptcp_pm_announce_addr(msk, &entry->addr, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) entry->retrans_times++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) if (entry->retrans_times < ADD_ADDR_RETRANS_MAX)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) sk_reset_timer(sk, timer, jiffies + TCP_RTO_MAX);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) spin_unlock_bh(&msk->pm.lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) __sock_put(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) struct mptcp_pm_add_entry *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) mptcp_pm_del_add_timer(struct mptcp_sock *msk,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) struct mptcp_addr_info *addr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) struct mptcp_pm_add_entry *entry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) struct sock *sk = (struct sock *)msk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) spin_lock_bh(&msk->pm.lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) entry = lookup_anno_list_by_saddr(msk, addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) if (entry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) entry->retrans_times = ADD_ADDR_RETRANS_MAX;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) spin_unlock_bh(&msk->pm.lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) if (entry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) sk_stop_timer_sync(sk, &entry->add_timer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) return entry;
^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) static bool mptcp_pm_alloc_anno_list(struct mptcp_sock *msk,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) struct mptcp_pm_addr_entry *entry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) struct mptcp_pm_add_entry *add_entry = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) struct sock *sk = (struct sock *)msk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) if (lookup_anno_list_by_saddr(msk, &entry->addr))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) add_entry = kmalloc(sizeof(*add_entry), GFP_ATOMIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) if (!add_entry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) list_add(&add_entry->list, &msk->pm.anno_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) add_entry->addr = entry->addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) add_entry->sock = msk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) add_entry->retrans_times = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) timer_setup(&add_entry->add_timer, mptcp_pm_add_timer, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) sk_reset_timer(sk, &add_entry->add_timer, jiffies + TCP_RTO_MAX);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) void mptcp_pm_free_anno_list(struct mptcp_sock *msk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) struct mptcp_pm_add_entry *entry, *tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) struct sock *sk = (struct sock *)msk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) LIST_HEAD(free_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) pr_debug("msk=%p", msk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) spin_lock_bh(&msk->pm.lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) list_splice_init(&msk->pm.anno_list, &free_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) spin_unlock_bh(&msk->pm.lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) list_for_each_entry_safe(entry, tmp, &free_list, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) sk_stop_timer_sync(sk, &entry->add_timer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) kfree(entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) static void mptcp_pm_create_subflow_or_signal_addr(struct mptcp_sock *msk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) struct mptcp_addr_info remote = { 0 };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) struct sock *sk = (struct sock *)msk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) struct mptcp_pm_addr_entry *local;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) struct pm_nl_pernet *pernet;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) pernet = net_generic(sock_net((struct sock *)msk), pm_nl_pernet_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) pr_debug("local %d:%d signal %d:%d subflows %d:%d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) msk->pm.local_addr_used, msk->pm.local_addr_max,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) msk->pm.add_addr_signaled, msk->pm.add_addr_signal_max,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) msk->pm.subflows, msk->pm.subflows_max);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) /* check first for announce */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) if (msk->pm.add_addr_signaled < msk->pm.add_addr_signal_max) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) local = select_signal_address(pernet,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) msk->pm.add_addr_signaled);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) if (local) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) if (mptcp_pm_alloc_anno_list(msk, local)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) msk->pm.add_addr_signaled++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) mptcp_pm_announce_addr(msk, &local->addr, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) /* pick failed, avoid fourther attempts later */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) msk->pm.local_addr_used = msk->pm.add_addr_signal_max;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) check_work_pending(msk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) /* check if should create a new subflow */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) if (msk->pm.local_addr_used < msk->pm.local_addr_max &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) msk->pm.subflows < msk->pm.subflows_max) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) remote_address((struct sock_common *)sk, &remote);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) local = select_local_address(pernet, msk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) if (local) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) msk->pm.local_addr_used++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) msk->pm.subflows++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) check_work_pending(msk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) spin_unlock_bh(&msk->pm.lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) __mptcp_subflow_connect(sk, &local->addr, &remote);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) spin_lock_bh(&msk->pm.lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) /* lookup failed, avoid fourther attempts later */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) msk->pm.local_addr_used = msk->pm.local_addr_max;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) check_work_pending(msk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) void mptcp_pm_nl_fully_established(struct mptcp_sock *msk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) mptcp_pm_create_subflow_or_signal_addr(msk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) void mptcp_pm_nl_subflow_established(struct mptcp_sock *msk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) mptcp_pm_create_subflow_or_signal_addr(msk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) void mptcp_pm_nl_add_addr_received(struct mptcp_sock *msk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) struct sock *sk = (struct sock *)msk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) struct mptcp_addr_info remote;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) struct mptcp_addr_info local;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) pr_debug("accepted %d:%d remote family %d",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) msk->pm.add_addr_accepted, msk->pm.add_addr_accept_max,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) msk->pm.remote.family);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) msk->pm.add_addr_accepted++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) msk->pm.subflows++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) if (msk->pm.add_addr_accepted >= msk->pm.add_addr_accept_max ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) msk->pm.subflows >= msk->pm.subflows_max)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) WRITE_ONCE(msk->pm.accept_addr, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) /* connect to the specified remote address, using whatever
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) * local address the routing configuration will pick.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) remote = msk->pm.remote;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) if (!remote.port)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) remote.port = sk->sk_dport;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) memset(&local, 0, sizeof(local));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) local.family = remote.family;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) spin_unlock_bh(&msk->pm.lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) __mptcp_subflow_connect((struct sock *)msk, &local, &remote);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) spin_lock_bh(&msk->pm.lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) mptcp_pm_announce_addr(msk, &remote, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) void mptcp_pm_nl_rm_addr_received(struct mptcp_sock *msk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) struct mptcp_subflow_context *subflow, *tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) struct sock *sk = (struct sock *)msk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) pr_debug("address rm_id %d", msk->pm.rm_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) if (!msk->pm.rm_id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) if (list_empty(&msk->conn_list))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) list_for_each_entry_safe(subflow, tmp, &msk->conn_list, node) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) int how = RCV_SHUTDOWN | SEND_SHUTDOWN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) long timeout = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) if (msk->pm.rm_id != subflow->remote_id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) spin_unlock_bh(&msk->pm.lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) mptcp_subflow_shutdown(sk, ssk, how);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) __mptcp_close_ssk(sk, ssk, subflow, timeout);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) spin_lock_bh(&msk->pm.lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) msk->pm.add_addr_accepted--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) msk->pm.subflows--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) WRITE_ONCE(msk->pm.accept_addr, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) __MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_RMADDR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) void mptcp_pm_nl_rm_subflow_received(struct mptcp_sock *msk, u8 rm_id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) struct mptcp_subflow_context *subflow, *tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) struct sock *sk = (struct sock *)msk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) pr_debug("subflow rm_id %d", rm_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) if (!rm_id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) if (list_empty(&msk->conn_list))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) list_for_each_entry_safe(subflow, tmp, &msk->conn_list, node) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) int how = RCV_SHUTDOWN | SEND_SHUTDOWN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) long timeout = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) if (rm_id != subflow->local_id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) spin_unlock_bh(&msk->pm.lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) mptcp_subflow_shutdown(sk, ssk, how);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) __mptcp_close_ssk(sk, ssk, subflow, timeout);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) spin_lock_bh(&msk->pm.lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) msk->pm.local_addr_used--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) msk->pm.subflows--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) __MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_RMSUBFLOW);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) static bool address_use_port(struct mptcp_pm_addr_entry *entry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) return (entry->addr.flags &
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) (MPTCP_PM_ADDR_FLAG_SIGNAL | MPTCP_PM_ADDR_FLAG_SUBFLOW)) ==
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) MPTCP_PM_ADDR_FLAG_SIGNAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) static int mptcp_pm_nl_append_new_local_addr(struct pm_nl_pernet *pernet,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) struct mptcp_pm_addr_entry *entry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) struct mptcp_pm_addr_entry *cur;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) int ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) spin_lock_bh(&pernet->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) /* to keep the code simple, don't do IDR-like allocation for address ID,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) * just bail when we exceed limits
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) if (pernet->next_id > 255)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) if (pernet->addrs >= MPTCP_PM_ADDR_MAX)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) /* do not insert duplicate address, differentiate on port only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) * singled addresses
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) list_for_each_entry(cur, &pernet->local_addr_list, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) if (addresses_equal(&cur->addr, &entry->addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) address_use_port(entry) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) address_use_port(cur)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) if (entry->addr.flags & MPTCP_PM_ADDR_FLAG_SIGNAL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) pernet->add_addr_signal_max++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) if (entry->addr.flags & MPTCP_PM_ADDR_FLAG_SUBFLOW)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) pernet->local_addr_max++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) entry->addr.id = pernet->next_id++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) pernet->addrs++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) list_add_tail_rcu(&entry->list, &pernet->local_addr_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) ret = entry->addr.id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) spin_unlock_bh(&pernet->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) int mptcp_pm_nl_get_local_id(struct mptcp_sock *msk, struct sock_common *skc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) struct mptcp_pm_addr_entry *entry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) struct mptcp_addr_info skc_local;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) struct mptcp_addr_info msk_local;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) struct pm_nl_pernet *pernet;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) int ret = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) if (WARN_ON_ONCE(!msk))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) /* The 0 ID mapping is defined by the first subflow, copied into the msk
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) * addr
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) local_address((struct sock_common *)msk, &msk_local);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) local_address((struct sock_common *)skc, &skc_local);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) if (addresses_equal(&msk_local, &skc_local, false))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) if (address_zero(&skc_local))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) pernet = net_generic(sock_net((struct sock *)msk), pm_nl_pernet_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) rcu_read_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) list_for_each_entry_rcu(entry, &pernet->local_addr_list, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) if (addresses_equal(&entry->addr, &skc_local, false)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) ret = entry->addr.id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) rcu_read_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) if (ret >= 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) /* address not found, add to local list */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) entry = kmalloc(sizeof(*entry), GFP_ATOMIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) if (!entry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) entry->addr = skc_local;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) entry->addr.ifindex = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560) entry->addr.flags = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561) ret = mptcp_pm_nl_append_new_local_addr(pernet, entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) kfree(entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568) void mptcp_pm_nl_data_init(struct mptcp_sock *msk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) struct mptcp_pm_data *pm = &msk->pm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571) struct pm_nl_pernet *pernet;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572) bool subflows;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574) pernet = net_generic(sock_net((struct sock *)msk), pm_nl_pernet_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576) pm->add_addr_signal_max = READ_ONCE(pernet->add_addr_signal_max);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577) pm->add_addr_accept_max = READ_ONCE(pernet->add_addr_accept_max);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578) pm->local_addr_max = READ_ONCE(pernet->local_addr_max);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579) pm->subflows_max = READ_ONCE(pernet->subflows_max);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580) subflows = !!pm->subflows_max;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581) WRITE_ONCE(pm->work_pending, (!!pm->local_addr_max && subflows) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582) !!pm->add_addr_signal_max);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583) WRITE_ONCE(pm->accept_addr, !!pm->add_addr_accept_max && subflows);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584) WRITE_ONCE(pm->accept_subflow, subflows);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587) #define MPTCP_PM_CMD_GRP_OFFSET 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589) static const struct genl_multicast_group mptcp_pm_mcgrps[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590) [MPTCP_PM_CMD_GRP_OFFSET] = { .name = MPTCP_PM_CMD_GRP_NAME, },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593) static const struct nla_policy
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594) mptcp_pm_addr_policy[MPTCP_PM_ADDR_ATTR_MAX + 1] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595) [MPTCP_PM_ADDR_ATTR_FAMILY] = { .type = NLA_U16, },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596) [MPTCP_PM_ADDR_ATTR_ID] = { .type = NLA_U8, },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597) [MPTCP_PM_ADDR_ATTR_ADDR4] = { .type = NLA_U32, },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 598) [MPTCP_PM_ADDR_ATTR_ADDR6] =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 599) NLA_POLICY_EXACT_LEN(sizeof(struct in6_addr)),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 600) [MPTCP_PM_ADDR_ATTR_PORT] = { .type = NLA_U16 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 601) [MPTCP_PM_ADDR_ATTR_FLAGS] = { .type = NLA_U32 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 602) [MPTCP_PM_ADDR_ATTR_IF_IDX] = { .type = NLA_S32 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 603) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 604)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 605) static const struct nla_policy mptcp_pm_policy[MPTCP_PM_ATTR_MAX + 1] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 606) [MPTCP_PM_ATTR_ADDR] =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 607) NLA_POLICY_NESTED(mptcp_pm_addr_policy),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 608) [MPTCP_PM_ATTR_RCV_ADD_ADDRS] = { .type = NLA_U32, },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 609) [MPTCP_PM_ATTR_SUBFLOWS] = { .type = NLA_U32, },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 610) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 611)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 612) static int mptcp_pm_family_to_addr(int family)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 613) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 614) #if IS_ENABLED(CONFIG_MPTCP_IPV6)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 615) if (family == AF_INET6)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 616) return MPTCP_PM_ADDR_ATTR_ADDR6;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 617) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 618) return MPTCP_PM_ADDR_ATTR_ADDR4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 619) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 620)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 621) static int mptcp_pm_parse_addr(struct nlattr *attr, struct genl_info *info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 622) bool require_family,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 623) struct mptcp_pm_addr_entry *entry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 624) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 625) struct nlattr *tb[MPTCP_PM_ADDR_ATTR_MAX + 1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 626) int err, addr_addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 627)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 628) if (!attr) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 629) GENL_SET_ERR_MSG(info, "missing address info");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 630) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 631) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 632)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 633) /* no validation needed - was already done via nested policy */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 634) err = nla_parse_nested_deprecated(tb, MPTCP_PM_ADDR_ATTR_MAX, attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 635) mptcp_pm_addr_policy, info->extack);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 636) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 637) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 638)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 639) memset(entry, 0, sizeof(*entry));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 640) if (!tb[MPTCP_PM_ADDR_ATTR_FAMILY]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 641) if (!require_family)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 642) goto skip_family;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 643)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 644) NL_SET_ERR_MSG_ATTR(info->extack, attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 645) "missing family");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 646) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 647) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 648)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 649) entry->addr.family = nla_get_u16(tb[MPTCP_PM_ADDR_ATTR_FAMILY]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 650) if (entry->addr.family != AF_INET
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 651) #if IS_ENABLED(CONFIG_MPTCP_IPV6)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 652) && entry->addr.family != AF_INET6
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 653) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 654) ) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 655) NL_SET_ERR_MSG_ATTR(info->extack, attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 656) "unknown address family");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 657) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 658) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 659) addr_addr = mptcp_pm_family_to_addr(entry->addr.family);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 660) if (!tb[addr_addr]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 661) NL_SET_ERR_MSG_ATTR(info->extack, attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 662) "missing address data");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 663) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 664) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 665)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 666) #if IS_ENABLED(CONFIG_MPTCP_IPV6)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 667) if (entry->addr.family == AF_INET6)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 668) entry->addr.addr6 = nla_get_in6_addr(tb[addr_addr]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 669) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 670) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 671) entry->addr.addr.s_addr = nla_get_in_addr(tb[addr_addr]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 672)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 673) skip_family:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 674) if (tb[MPTCP_PM_ADDR_ATTR_IF_IDX]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 675) u32 val = nla_get_s32(tb[MPTCP_PM_ADDR_ATTR_IF_IDX]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 676)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 677) entry->addr.ifindex = val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 678) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 679)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 680) if (tb[MPTCP_PM_ADDR_ATTR_ID])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 681) entry->addr.id = nla_get_u8(tb[MPTCP_PM_ADDR_ATTR_ID]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 682)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 683) if (tb[MPTCP_PM_ADDR_ATTR_FLAGS])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 684) entry->addr.flags = nla_get_u32(tb[MPTCP_PM_ADDR_ATTR_FLAGS]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 685)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 686) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 687) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 688)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 689) static struct pm_nl_pernet *genl_info_pm_nl(struct genl_info *info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 690) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 691) return net_generic(genl_info_net(info), pm_nl_pernet_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 692) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 693)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 694) static int mptcp_nl_cmd_add_addr(struct sk_buff *skb, struct genl_info *info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 695) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 696) struct nlattr *attr = info->attrs[MPTCP_PM_ATTR_ADDR];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 697) struct pm_nl_pernet *pernet = genl_info_pm_nl(info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 698) struct mptcp_pm_addr_entry addr, *entry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 699) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 700)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 701) ret = mptcp_pm_parse_addr(attr, info, true, &addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 702) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 703) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 704)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 705) entry = kmalloc(sizeof(*entry), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 706) if (!entry) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 707) GENL_SET_ERR_MSG(info, "can't allocate addr");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 708) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 709) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 710)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 711) *entry = addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 712) ret = mptcp_pm_nl_append_new_local_addr(pernet, entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 713) if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 714) GENL_SET_ERR_MSG(info, "too many addresses or duplicate one");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 715) kfree(entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 716) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 717) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 718)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 719) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 720) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 721)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 722) static struct mptcp_pm_addr_entry *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 723) __lookup_addr_by_id(struct pm_nl_pernet *pernet, unsigned int id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 724) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 725) struct mptcp_pm_addr_entry *entry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 726)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 727) list_for_each_entry(entry, &pernet->local_addr_list, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 728) if (entry->addr.id == id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 729) return entry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 730) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 731) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 732) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 733)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 734) static bool remove_anno_list_by_saddr(struct mptcp_sock *msk,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 735) struct mptcp_addr_info *addr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 736) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 737) struct mptcp_pm_add_entry *entry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 738)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 739) entry = mptcp_pm_del_add_timer(msk, addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 740) if (entry) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 741) list_del(&entry->list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 742) kfree(entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 743) return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 744) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 745)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 746) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 747) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 748)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 749) static bool mptcp_pm_remove_anno_addr(struct mptcp_sock *msk,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 750) struct mptcp_addr_info *addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 751) bool force)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 752) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 753) bool ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 754)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 755) ret = remove_anno_list_by_saddr(msk, addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 756) if (ret || force) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 757) spin_lock_bh(&msk->pm.lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 758) mptcp_pm_remove_addr(msk, addr->id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 759) spin_unlock_bh(&msk->pm.lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 760) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 761) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 762) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 763)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 764) static int mptcp_nl_remove_subflow_and_signal_addr(struct net *net,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 765) struct mptcp_addr_info *addr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 766) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 767) struct mptcp_sock *msk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 768) long s_slot = 0, s_num = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 769)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 770) pr_debug("remove_id=%d", addr->id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 771)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 772) while ((msk = mptcp_token_iter_next(net, &s_slot, &s_num)) != NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 773) struct sock *sk = (struct sock *)msk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 774) bool remove_subflow;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 775)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 776) if (list_empty(&msk->conn_list)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 777) mptcp_pm_remove_anno_addr(msk, addr, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 778) goto next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 779) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 780)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 781) lock_sock(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 782) remove_subflow = lookup_subflow_by_saddr(&msk->conn_list, addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 783) mptcp_pm_remove_anno_addr(msk, addr, remove_subflow);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 784) if (remove_subflow)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 785) mptcp_pm_remove_subflow(msk, addr->id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 786) release_sock(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 787)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 788) next:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 789) sock_put(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 790) cond_resched();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 791) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 792)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 793) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 794) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 795)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 796) static int mptcp_nl_cmd_del_addr(struct sk_buff *skb, struct genl_info *info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 797) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 798) struct nlattr *attr = info->attrs[MPTCP_PM_ATTR_ADDR];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 799) struct pm_nl_pernet *pernet = genl_info_pm_nl(info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 800) struct mptcp_pm_addr_entry addr, *entry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 801) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 802)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 803) ret = mptcp_pm_parse_addr(attr, info, false, &addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 804) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 805) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 806)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 807) spin_lock_bh(&pernet->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 808) entry = __lookup_addr_by_id(pernet, addr.addr.id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 809) if (!entry) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 810) GENL_SET_ERR_MSG(info, "address not found");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 811) spin_unlock_bh(&pernet->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 812) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 813) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 814) if (entry->addr.flags & MPTCP_PM_ADDR_FLAG_SIGNAL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 815) pernet->add_addr_signal_max--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 816) if (entry->addr.flags & MPTCP_PM_ADDR_FLAG_SUBFLOW)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 817) pernet->local_addr_max--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 818)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 819) pernet->addrs--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 820) list_del_rcu(&entry->list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 821) spin_unlock_bh(&pernet->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 822)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 823) mptcp_nl_remove_subflow_and_signal_addr(sock_net(skb->sk), &entry->addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 824) kfree_rcu(entry, rcu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 825)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 826) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 827) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 828)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 829) static void __flush_addrs(struct pm_nl_pernet *pernet)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 830) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 831) while (!list_empty(&pernet->local_addr_list)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 832) struct mptcp_pm_addr_entry *cur;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 833)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 834) cur = list_entry(pernet->local_addr_list.next,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 835) struct mptcp_pm_addr_entry, list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 836) list_del_rcu(&cur->list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 837) kfree_rcu(cur, rcu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 838) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 839) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 840)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 841) static void __reset_counters(struct pm_nl_pernet *pernet)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 842) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 843) pernet->add_addr_signal_max = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 844) pernet->add_addr_accept_max = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 845) pernet->local_addr_max = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 846) pernet->addrs = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 847) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 848)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 849) static int mptcp_nl_cmd_flush_addrs(struct sk_buff *skb, struct genl_info *info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 850) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 851) struct pm_nl_pernet *pernet = genl_info_pm_nl(info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 852)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 853) spin_lock_bh(&pernet->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 854) __flush_addrs(pernet);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 855) __reset_counters(pernet);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 856) spin_unlock_bh(&pernet->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 857) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 858) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 859)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 860) static int mptcp_nl_fill_addr(struct sk_buff *skb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 861) struct mptcp_pm_addr_entry *entry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 862) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 863) struct mptcp_addr_info *addr = &entry->addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 864) struct nlattr *attr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 865)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 866) attr = nla_nest_start(skb, MPTCP_PM_ATTR_ADDR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 867) if (!attr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 868) return -EMSGSIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 869)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 870) if (nla_put_u16(skb, MPTCP_PM_ADDR_ATTR_FAMILY, addr->family))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 871) goto nla_put_failure;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 872) if (nla_put_u8(skb, MPTCP_PM_ADDR_ATTR_ID, addr->id))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 873) goto nla_put_failure;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 874) if (nla_put_u32(skb, MPTCP_PM_ADDR_ATTR_FLAGS, entry->addr.flags))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 875) goto nla_put_failure;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 876) if (entry->addr.ifindex &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 877) nla_put_s32(skb, MPTCP_PM_ADDR_ATTR_IF_IDX, entry->addr.ifindex))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 878) goto nla_put_failure;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 879)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 880) if (addr->family == AF_INET &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 881) nla_put_in_addr(skb, MPTCP_PM_ADDR_ATTR_ADDR4,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 882) addr->addr.s_addr))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 883) goto nla_put_failure;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 884) #if IS_ENABLED(CONFIG_MPTCP_IPV6)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 885) else if (addr->family == AF_INET6 &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 886) nla_put_in6_addr(skb, MPTCP_PM_ADDR_ATTR_ADDR6, &addr->addr6))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 887) goto nla_put_failure;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 888) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 889) nla_nest_end(skb, attr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 890) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 891)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 892) nla_put_failure:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 893) nla_nest_cancel(skb, attr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 894) return -EMSGSIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 895) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 896)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 897) static int mptcp_nl_cmd_get_addr(struct sk_buff *skb, struct genl_info *info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 898) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 899) struct nlattr *attr = info->attrs[MPTCP_PM_ATTR_ADDR];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 900) struct pm_nl_pernet *pernet = genl_info_pm_nl(info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 901) struct mptcp_pm_addr_entry addr, *entry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 902) struct sk_buff *msg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 903) void *reply;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 904) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 905)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 906) ret = mptcp_pm_parse_addr(attr, info, false, &addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 907) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 908) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 909)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 910) msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 911) if (!msg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 912) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 913)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 914) reply = genlmsg_put_reply(msg, info, &mptcp_genl_family, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 915) info->genlhdr->cmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 916) if (!reply) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 917) GENL_SET_ERR_MSG(info, "not enough space in Netlink message");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 918) ret = -EMSGSIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 919) goto fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 920) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 921)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 922) spin_lock_bh(&pernet->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 923) entry = __lookup_addr_by_id(pernet, addr.addr.id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 924) if (!entry) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 925) GENL_SET_ERR_MSG(info, "address not found");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 926) ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 927) goto unlock_fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 928) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 929)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 930) ret = mptcp_nl_fill_addr(msg, entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 931) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 932) goto unlock_fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 933)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 934) genlmsg_end(msg, reply);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 935) ret = genlmsg_reply(msg, info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 936) spin_unlock_bh(&pernet->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 937) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 938)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 939) unlock_fail:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 940) spin_unlock_bh(&pernet->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 941)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 942) fail:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 943) nlmsg_free(msg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 944) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 945) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 946)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 947) static int mptcp_nl_cmd_dump_addrs(struct sk_buff *msg,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 948) struct netlink_callback *cb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 949) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 950) struct net *net = sock_net(msg->sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 951) struct mptcp_pm_addr_entry *entry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 952) struct pm_nl_pernet *pernet;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 953) int id = cb->args[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 954) void *hdr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 955)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 956) pernet = net_generic(net, pm_nl_pernet_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 957)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 958) spin_lock_bh(&pernet->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 959) list_for_each_entry(entry, &pernet->local_addr_list, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 960) if (entry->addr.id <= id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 961) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 962)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 963) hdr = genlmsg_put(msg, NETLINK_CB(cb->skb).portid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 964) cb->nlh->nlmsg_seq, &mptcp_genl_family,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 965) NLM_F_MULTI, MPTCP_PM_CMD_GET_ADDR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 966) if (!hdr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 967) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 968)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 969) if (mptcp_nl_fill_addr(msg, entry) < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 970) genlmsg_cancel(msg, hdr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 971) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 972) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 973)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 974) id = entry->addr.id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 975) genlmsg_end(msg, hdr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 976) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 977) spin_unlock_bh(&pernet->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 978)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 979) cb->args[0] = id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 980) return msg->len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 981) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 982)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 983) static int parse_limit(struct genl_info *info, int id, unsigned int *limit)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 984) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 985) struct nlattr *attr = info->attrs[id];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 986)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 987) if (!attr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 988) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 989)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 990) *limit = nla_get_u32(attr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 991) if (*limit > MPTCP_PM_ADDR_MAX) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 992) GENL_SET_ERR_MSG(info, "limit greater than maximum");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 993) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 994) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 995) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 996) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 997)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 998) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 999) mptcp_nl_cmd_set_limits(struct sk_buff *skb, struct genl_info *info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1000) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1001) struct pm_nl_pernet *pernet = genl_info_pm_nl(info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1002) unsigned int rcv_addrs, subflows;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1003) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1004)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1005) spin_lock_bh(&pernet->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1006) rcv_addrs = pernet->add_addr_accept_max;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1007) ret = parse_limit(info, MPTCP_PM_ATTR_RCV_ADD_ADDRS, &rcv_addrs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1008) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1009) goto unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1010)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1011) subflows = pernet->subflows_max;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1012) ret = parse_limit(info, MPTCP_PM_ATTR_SUBFLOWS, &subflows);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1013) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1014) goto unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1015)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1016) WRITE_ONCE(pernet->add_addr_accept_max, rcv_addrs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1017) WRITE_ONCE(pernet->subflows_max, subflows);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1018)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1019) unlock:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1020) spin_unlock_bh(&pernet->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1021) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1022) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1023)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1024) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1025) mptcp_nl_cmd_get_limits(struct sk_buff *skb, struct genl_info *info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1026) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1027) struct pm_nl_pernet *pernet = genl_info_pm_nl(info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1028) struct sk_buff *msg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1029) void *reply;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1030)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1031) msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1032) if (!msg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1033) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1034)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1035) reply = genlmsg_put_reply(msg, info, &mptcp_genl_family, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1036) MPTCP_PM_CMD_GET_LIMITS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1037) if (!reply)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1038) goto fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1039)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1040) if (nla_put_u32(msg, MPTCP_PM_ATTR_RCV_ADD_ADDRS,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1041) READ_ONCE(pernet->add_addr_accept_max)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1042) goto fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1043)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1044) if (nla_put_u32(msg, MPTCP_PM_ATTR_SUBFLOWS,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1045) READ_ONCE(pernet->subflows_max)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1046) goto fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1047)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1048) genlmsg_end(msg, reply);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1049) return genlmsg_reply(msg, info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1050)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1051) fail:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1052) GENL_SET_ERR_MSG(info, "not enough space in Netlink message");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1053) nlmsg_free(msg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1054) return -EMSGSIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1055) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1056)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1057) static const struct genl_small_ops mptcp_pm_ops[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1058) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1059) .cmd = MPTCP_PM_CMD_ADD_ADDR,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1060) .doit = mptcp_nl_cmd_add_addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1061) .flags = GENL_ADMIN_PERM,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1062) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1063) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1064) .cmd = MPTCP_PM_CMD_DEL_ADDR,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1065) .doit = mptcp_nl_cmd_del_addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1066) .flags = GENL_ADMIN_PERM,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1067) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1068) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1069) .cmd = MPTCP_PM_CMD_FLUSH_ADDRS,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1070) .doit = mptcp_nl_cmd_flush_addrs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1071) .flags = GENL_ADMIN_PERM,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1072) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1073) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1074) .cmd = MPTCP_PM_CMD_GET_ADDR,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1075) .doit = mptcp_nl_cmd_get_addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1076) .dumpit = mptcp_nl_cmd_dump_addrs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1077) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1078) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1079) .cmd = MPTCP_PM_CMD_SET_LIMITS,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1080) .doit = mptcp_nl_cmd_set_limits,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1081) .flags = GENL_ADMIN_PERM,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1082) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1083) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1084) .cmd = MPTCP_PM_CMD_GET_LIMITS,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1085) .doit = mptcp_nl_cmd_get_limits,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1086) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1087) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1088)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1089) static struct genl_family mptcp_genl_family __ro_after_init = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1090) .name = MPTCP_PM_NAME,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1091) .version = MPTCP_PM_VER,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1092) .maxattr = MPTCP_PM_ATTR_MAX,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1093) .policy = mptcp_pm_policy,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1094) .netnsok = true,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1095) .module = THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1096) .small_ops = mptcp_pm_ops,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1097) .n_small_ops = ARRAY_SIZE(mptcp_pm_ops),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1098) .mcgrps = mptcp_pm_mcgrps,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1099) .n_mcgrps = ARRAY_SIZE(mptcp_pm_mcgrps),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1100) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1101)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1102) static int __net_init pm_nl_init_net(struct net *net)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1103) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1104) struct pm_nl_pernet *pernet = net_generic(net, pm_nl_pernet_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1105)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1106) INIT_LIST_HEAD_RCU(&pernet->local_addr_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1107) __reset_counters(pernet);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1108) pernet->next_id = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1109) spin_lock_init(&pernet->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1110) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1111) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1112)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1113) static void __net_exit pm_nl_exit_net(struct list_head *net_list)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1114) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1115) struct net *net;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1116)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1117) list_for_each_entry(net, net_list, exit_list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1118) /* net is removed from namespace list, can't race with
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1119) * other modifiers
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1120) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1121) __flush_addrs(net_generic(net, pm_nl_pernet_id));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1122) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1123) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1124)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1125) static struct pernet_operations mptcp_pm_pernet_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1126) .init = pm_nl_init_net,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1127) .exit_batch = pm_nl_exit_net,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1128) .id = &pm_nl_pernet_id,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1129) .size = sizeof(struct pm_nl_pernet),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1130) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1131)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1132) void __init mptcp_pm_nl_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1133) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1134) if (register_pernet_subsys(&mptcp_pm_pernet_ops) < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1135) panic("Failed to register MPTCP PM pernet subsystem.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1136)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1137) if (genl_register_family(&mptcp_genl_family))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1138) panic("Failed to register MPTCP PM netlink family\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1139) }