^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * File: pn_netlink.c
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Phonet netlink interface
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) * Copyright (C) 2008 Nokia Corporation.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) * Authors: Sakari Ailus <sakari.ailus@nokia.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) * Remi Denis-Courmont
^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/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/netlink.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <linux/phonet.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include <net/sock.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include <net/phonet/pn_dev.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) /* Device address handling */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) static int fill_addr(struct sk_buff *skb, struct net_device *dev, u8 addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) u32 portid, u32 seq, int event);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) void phonet_address_notify(int event, struct net_device *dev, u8 addr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) struct sk_buff *skb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) int err = -ENOBUFS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) skb = nlmsg_new(NLMSG_ALIGN(sizeof(struct ifaddrmsg)) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) nla_total_size(1), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) if (skb == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) goto errout;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) err = fill_addr(skb, dev, addr, 0, 0, event);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) if (err < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) WARN_ON(err == -EMSGSIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) kfree_skb(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) goto errout;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) rtnl_notify(skb, dev_net(dev), 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) RTNLGRP_PHONET_IFADDR, NULL, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) errout:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) rtnl_set_sk_err(dev_net(dev), RTNLGRP_PHONET_IFADDR, err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) static const struct nla_policy ifa_phonet_policy[IFA_MAX+1] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) [IFA_LOCAL] = { .type = NLA_U8 },
^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) static int addr_doit(struct sk_buff *skb, struct nlmsghdr *nlh,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) struct netlink_ext_ack *extack)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) struct net *net = sock_net(skb->sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) struct nlattr *tb[IFA_MAX+1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) struct net_device *dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) struct ifaddrmsg *ifm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) u8 pnaddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) if (!netlink_capable(skb, CAP_NET_ADMIN))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) return -EPERM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) if (!netlink_capable(skb, CAP_SYS_ADMIN))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) return -EPERM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) ASSERT_RTNL();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) err = nlmsg_parse_deprecated(nlh, sizeof(*ifm), tb, IFA_MAX,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) ifa_phonet_policy, extack);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) ifm = nlmsg_data(nlh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) if (tb[IFA_LOCAL] == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) pnaddr = nla_get_u8(tb[IFA_LOCAL]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) if (pnaddr & 3)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) /* Phonet addresses only have 6 high-order bits */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) dev = __dev_get_by_index(net, ifm->ifa_index);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) if (dev == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) if (nlh->nlmsg_type == RTM_NEWADDR)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) err = phonet_address_add(dev, pnaddr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) err = phonet_address_del(dev, pnaddr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) if (!err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) phonet_address_notify(nlh->nlmsg_type, dev, pnaddr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) static int fill_addr(struct sk_buff *skb, struct net_device *dev, u8 addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) u32 portid, u32 seq, int event)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) struct ifaddrmsg *ifm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) struct nlmsghdr *nlh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) nlh = nlmsg_put(skb, portid, seq, event, sizeof(*ifm), 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) if (nlh == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) return -EMSGSIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) ifm = nlmsg_data(nlh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) ifm->ifa_family = AF_PHONET;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) ifm->ifa_prefixlen = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) ifm->ifa_flags = IFA_F_PERMANENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) ifm->ifa_scope = RT_SCOPE_LINK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) ifm->ifa_index = dev->ifindex;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) if (nla_put_u8(skb, IFA_LOCAL, addr))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) goto nla_put_failure;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) nlmsg_end(skb, nlh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) nla_put_failure:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) nlmsg_cancel(skb, nlh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) return -EMSGSIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) static int getaddr_dumpit(struct sk_buff *skb, struct netlink_callback *cb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) struct phonet_device_list *pndevs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) struct phonet_device *pnd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) int dev_idx = 0, dev_start_idx = cb->args[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) int addr_idx = 0, addr_start_idx = cb->args[1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) pndevs = phonet_device_list(sock_net(skb->sk));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) rcu_read_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) list_for_each_entry_rcu(pnd, &pndevs->list, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) u8 addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) if (dev_idx > dev_start_idx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) addr_start_idx = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) if (dev_idx++ < dev_start_idx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) addr_idx = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) for_each_set_bit(addr, pnd->addrs, 64) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) if (addr_idx++ < addr_start_idx)
^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) if (fill_addr(skb, pnd->netdev, addr << 2,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) NETLINK_CB(cb->skb).portid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) cb->nlh->nlmsg_seq, RTM_NEWADDR) < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) rcu_read_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) cb->args[0] = dev_idx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) cb->args[1] = addr_idx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) return skb->len;
^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) /* Routes handling */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) static int fill_route(struct sk_buff *skb, struct net_device *dev, u8 dst,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) u32 portid, u32 seq, int event)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) struct rtmsg *rtm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) struct nlmsghdr *nlh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) nlh = nlmsg_put(skb, portid, seq, event, sizeof(*rtm), 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) if (nlh == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) return -EMSGSIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) rtm = nlmsg_data(nlh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) rtm->rtm_family = AF_PHONET;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) rtm->rtm_dst_len = 6;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) rtm->rtm_src_len = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) rtm->rtm_tos = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) rtm->rtm_table = RT_TABLE_MAIN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) rtm->rtm_protocol = RTPROT_STATIC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) rtm->rtm_scope = RT_SCOPE_UNIVERSE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) rtm->rtm_type = RTN_UNICAST;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) rtm->rtm_flags = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) if (nla_put_u8(skb, RTA_DST, dst) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) nla_put_u32(skb, RTA_OIF, dev->ifindex))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) goto nla_put_failure;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) nlmsg_end(skb, nlh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) nla_put_failure:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) nlmsg_cancel(skb, nlh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) return -EMSGSIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) void rtm_phonet_notify(int event, struct net_device *dev, u8 dst)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) struct sk_buff *skb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) int err = -ENOBUFS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) skb = nlmsg_new(NLMSG_ALIGN(sizeof(struct ifaddrmsg)) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) nla_total_size(1) + nla_total_size(4), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) if (skb == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) goto errout;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) err = fill_route(skb, dev, dst, 0, 0, event);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) if (err < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) WARN_ON(err == -EMSGSIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) kfree_skb(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) goto errout;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) rtnl_notify(skb, dev_net(dev), 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) RTNLGRP_PHONET_ROUTE, NULL, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) errout:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) rtnl_set_sk_err(dev_net(dev), RTNLGRP_PHONET_ROUTE, err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) static const struct nla_policy rtm_phonet_policy[RTA_MAX+1] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) [RTA_DST] = { .type = NLA_U8 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) [RTA_OIF] = { .type = NLA_U32 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) static int route_doit(struct sk_buff *skb, struct nlmsghdr *nlh,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) struct netlink_ext_ack *extack)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) struct net *net = sock_net(skb->sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) struct nlattr *tb[RTA_MAX+1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) struct net_device *dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) struct rtmsg *rtm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) u8 dst;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) if (!netlink_capable(skb, CAP_NET_ADMIN))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) return -EPERM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) if (!netlink_capable(skb, CAP_SYS_ADMIN))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) return -EPERM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) ASSERT_RTNL();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) err = nlmsg_parse_deprecated(nlh, sizeof(*rtm), tb, RTA_MAX,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) rtm_phonet_policy, extack);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) rtm = nlmsg_data(nlh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) if (rtm->rtm_table != RT_TABLE_MAIN || rtm->rtm_type != RTN_UNICAST)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) if (tb[RTA_DST] == NULL || tb[RTA_OIF] == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) dst = nla_get_u8(tb[RTA_DST]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) if (dst & 3) /* Phonet addresses only have 6 high-order bits */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) dev = __dev_get_by_index(net, nla_get_u32(tb[RTA_OIF]));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) if (dev == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) if (nlh->nlmsg_type == RTM_NEWROUTE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) err = phonet_route_add(dev, dst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) err = phonet_route_del(dev, dst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) if (!err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) rtm_phonet_notify(nlh->nlmsg_type, dev, dst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) return err;
^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) static int route_dumpit(struct sk_buff *skb, struct netlink_callback *cb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) struct net *net = sock_net(skb->sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) u8 addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) rcu_read_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) for (addr = cb->args[0]; addr < 64; addr++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) struct net_device *dev = phonet_route_get_rcu(net, addr << 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) if (!dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) if (fill_route(skb, dev, addr << 2, NETLINK_CB(cb->skb).portid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) cb->nlh->nlmsg_seq, RTM_NEWROUTE) < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) rcu_read_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) cb->args[0] = addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) return skb->len;
^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) int __init phonet_netlink_register(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) int err = rtnl_register_module(THIS_MODULE, PF_PHONET, RTM_NEWADDR,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) addr_doit, NULL, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) /* Further rtnl_register_module() cannot fail */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) rtnl_register_module(THIS_MODULE, PF_PHONET, RTM_DELADDR,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) addr_doit, NULL, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) rtnl_register_module(THIS_MODULE, PF_PHONET, RTM_GETADDR,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) NULL, getaddr_dumpit, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) rtnl_register_module(THIS_MODULE, PF_PHONET, RTM_NEWROUTE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) route_doit, NULL, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) rtnl_register_module(THIS_MODULE, PF_PHONET, RTM_DELROUTE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) route_doit, NULL, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) rtnl_register_module(THIS_MODULE, PF_PHONET, RTM_GETROUTE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) NULL, route_dumpit, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) }