1 // SPDX-License-Identifier: GPL-2.0-only
5 * Phonet netlink interface
7 * Copyright (C) 2008 Nokia Corporation.
9 * Authors: Sakari Ailus <sakari.ailus@nokia.com>
13 #include <linux/kernel.h>
14 #include <linux/netlink.h>
15 #include <linux/phonet.h>
16 #include <linux/slab.h>
18 #include <net/phonet/pn_dev.h>
20 /* Device address handling */
22 static int fill_addr(struct sk_buff
*skb
, u32 ifindex
, u8 addr
,
23 u32 portid
, u32 seq
, int event
);
25 void phonet_address_notify(struct net
*net
, int event
, u32 ifindex
, u8 addr
)
30 skb
= nlmsg_new(NLMSG_ALIGN(sizeof(struct ifaddrmsg
)) +
31 nla_total_size(1), GFP_KERNEL
);
35 err
= fill_addr(skb
, ifindex
, addr
, 0, 0, event
);
37 WARN_ON(err
== -EMSGSIZE
);
42 rtnl_notify(skb
, net
, 0, RTNLGRP_PHONET_IFADDR
, NULL
, GFP_KERNEL
);
45 rtnl_set_sk_err(net
, RTNLGRP_PHONET_IFADDR
, err
);
48 static const struct nla_policy ifa_phonet_policy
[IFA_MAX
+1] = {
49 [IFA_LOCAL
] = { .type
= NLA_U8
},
52 static int addr_doit(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
53 struct netlink_ext_ack
*extack
)
55 struct net
*net
= sock_net(skb
->sk
);
56 struct nlattr
*tb
[IFA_MAX
+1];
57 struct net_device
*dev
;
58 struct ifaddrmsg
*ifm
;
62 if (!netlink_capable(skb
, CAP_NET_ADMIN
))
65 if (!netlink_capable(skb
, CAP_SYS_ADMIN
))
68 err
= nlmsg_parse_deprecated(nlh
, sizeof(*ifm
), tb
, IFA_MAX
,
69 ifa_phonet_policy
, extack
);
73 ifm
= nlmsg_data(nlh
);
74 if (tb
[IFA_LOCAL
] == NULL
)
76 pnaddr
= nla_get_u8(tb
[IFA_LOCAL
]);
78 /* Phonet addresses only have 6 high-order bits */
83 dev
= dev_get_by_index_rcu(net
, ifm
->ifa_index
);
89 if (nlh
->nlmsg_type
== RTM_NEWADDR
)
90 err
= phonet_address_add(dev
, pnaddr
);
92 err
= phonet_address_del(dev
, pnaddr
);
97 phonet_address_notify(net
, nlh
->nlmsg_type
, ifm
->ifa_index
, pnaddr
);
102 static int fill_addr(struct sk_buff
*skb
, u32 ifindex
, u8 addr
,
103 u32 portid
, u32 seq
, int event
)
105 struct ifaddrmsg
*ifm
;
106 struct nlmsghdr
*nlh
;
108 nlh
= nlmsg_put(skb
, portid
, seq
, event
, sizeof(*ifm
), 0);
112 ifm
= nlmsg_data(nlh
);
113 ifm
->ifa_family
= AF_PHONET
;
114 ifm
->ifa_prefixlen
= 0;
115 ifm
->ifa_flags
= IFA_F_PERMANENT
;
116 ifm
->ifa_scope
= RT_SCOPE_LINK
;
117 ifm
->ifa_index
= ifindex
;
118 if (nla_put_u8(skb
, IFA_LOCAL
, addr
))
119 goto nla_put_failure
;
124 nlmsg_cancel(skb
, nlh
);
128 static int getaddr_dumpit(struct sk_buff
*skb
, struct netlink_callback
*cb
)
130 int addr_idx
= 0, addr_start_idx
= cb
->args
[1];
131 int dev_idx
= 0, dev_start_idx
= cb
->args
[0];
132 struct phonet_device_list
*pndevs
;
133 struct phonet_device
*pnd
;
136 pndevs
= phonet_device_list(sock_net(skb
->sk
));
139 list_for_each_entry_rcu(pnd
, &pndevs
->list
, list
) {
140 DECLARE_BITMAP(addrs
, 64);
143 if (dev_idx
> dev_start_idx
)
145 if (dev_idx
++ < dev_start_idx
)
149 memcpy(addrs
, pnd
->addrs
, sizeof(pnd
->addrs
));
151 for_each_set_bit(addr
, addrs
, 64) {
152 if (addr_idx
++ < addr_start_idx
)
155 err
= fill_addr(skb
, READ_ONCE(pnd
->netdev
->ifindex
),
156 addr
<< 2, NETLINK_CB(cb
->skb
).portid
,
157 cb
->nlh
->nlmsg_seq
, RTM_NEWADDR
);
165 cb
->args
[0] = dev_idx
;
166 cb
->args
[1] = addr_idx
;
171 /* Routes handling */
173 static int fill_route(struct sk_buff
*skb
, u32 ifindex
, u8 dst
,
174 u32 portid
, u32 seq
, int event
)
177 struct nlmsghdr
*nlh
;
179 nlh
= nlmsg_put(skb
, portid
, seq
, event
, sizeof(*rtm
), 0);
183 rtm
= nlmsg_data(nlh
);
184 rtm
->rtm_family
= AF_PHONET
;
185 rtm
->rtm_dst_len
= 6;
186 rtm
->rtm_src_len
= 0;
188 rtm
->rtm_table
= RT_TABLE_MAIN
;
189 rtm
->rtm_protocol
= RTPROT_STATIC
;
190 rtm
->rtm_scope
= RT_SCOPE_UNIVERSE
;
191 rtm
->rtm_type
= RTN_UNICAST
;
193 if (nla_put_u8(skb
, RTA_DST
, dst
) || nla_put_u32(skb
, RTA_OIF
, ifindex
))
194 goto nla_put_failure
;
199 nlmsg_cancel(skb
, nlh
);
203 void rtm_phonet_notify(struct net
*net
, int event
, u32 ifindex
, u8 dst
)
208 skb
= nlmsg_new(NLMSG_ALIGN(sizeof(struct rtmsg
)) +
209 nla_total_size(1) + nla_total_size(4), GFP_KERNEL
);
213 err
= fill_route(skb
, ifindex
, dst
, 0, 0, event
);
215 WARN_ON(err
== -EMSGSIZE
);
220 rtnl_notify(skb
, net
, 0, RTNLGRP_PHONET_ROUTE
, NULL
, GFP_KERNEL
);
223 rtnl_set_sk_err(net
, RTNLGRP_PHONET_ROUTE
, err
);
226 static const struct nla_policy rtm_phonet_policy
[RTA_MAX
+1] = {
227 [RTA_DST
] = { .type
= NLA_U8
},
228 [RTA_OIF
] = { .type
= NLA_U32
},
231 static int route_doit(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
232 struct netlink_ext_ack
*extack
)
234 struct net
*net
= sock_net(skb
->sk
);
235 struct nlattr
*tb
[RTA_MAX
+1];
236 bool sync_needed
= false;
237 struct net_device
*dev
;
243 if (!netlink_capable(skb
, CAP_NET_ADMIN
))
246 if (!netlink_capable(skb
, CAP_SYS_ADMIN
))
249 err
= nlmsg_parse_deprecated(nlh
, sizeof(*rtm
), tb
, RTA_MAX
,
250 rtm_phonet_policy
, extack
);
254 rtm
= nlmsg_data(nlh
);
255 if (rtm
->rtm_table
!= RT_TABLE_MAIN
|| rtm
->rtm_type
!= RTN_UNICAST
)
257 if (tb
[RTA_DST
] == NULL
|| tb
[RTA_OIF
] == NULL
)
259 dst
= nla_get_u8(tb
[RTA_DST
]);
260 if (dst
& 3) /* Phonet addresses only have 6 high-order bits */
263 ifindex
= nla_get_u32(tb
[RTA_OIF
]);
267 dev
= dev_get_by_index_rcu(net
, ifindex
);
273 if (nlh
->nlmsg_type
== RTM_NEWROUTE
) {
274 err
= phonet_route_add(dev
, dst
);
276 err
= phonet_route_del(dev
, dst
);
288 rtm_phonet_notify(net
, nlh
->nlmsg_type
, ifindex
, dst
);
293 static int route_dumpit(struct sk_buff
*skb
, struct netlink_callback
*cb
)
295 struct net
*net
= sock_net(skb
->sk
);
300 for (addr
= cb
->args
[0]; addr
< 64; addr
++) {
301 struct net_device
*dev
= phonet_route_get_rcu(net
, addr
<< 2);
306 err
= fill_route(skb
, READ_ONCE(dev
->ifindex
), addr
<< 2,
307 NETLINK_CB(cb
->skb
).portid
,
308 cb
->nlh
->nlmsg_seq
, RTM_NEWROUTE
);
318 static const struct rtnl_msg_handler phonet_rtnl_msg_handlers
[] __initdata_or_module
= {
319 {.owner
= THIS_MODULE
, .protocol
= PF_PHONET
, .msgtype
= RTM_NEWADDR
,
320 .doit
= addr_doit
, .flags
= RTNL_FLAG_DOIT_UNLOCKED
},
321 {.owner
= THIS_MODULE
, .protocol
= PF_PHONET
, .msgtype
= RTM_DELADDR
,
322 .doit
= addr_doit
, .flags
= RTNL_FLAG_DOIT_UNLOCKED
},
323 {.owner
= THIS_MODULE
, .protocol
= PF_PHONET
, .msgtype
= RTM_GETADDR
,
324 .dumpit
= getaddr_dumpit
, .flags
= RTNL_FLAG_DUMP_UNLOCKED
},
325 {.owner
= THIS_MODULE
, .protocol
= PF_PHONET
, .msgtype
= RTM_NEWROUTE
,
326 .doit
= route_doit
, .flags
= RTNL_FLAG_DOIT_UNLOCKED
},
327 {.owner
= THIS_MODULE
, .protocol
= PF_PHONET
, .msgtype
= RTM_DELROUTE
,
328 .doit
= route_doit
, .flags
= RTNL_FLAG_DOIT_UNLOCKED
},
329 {.owner
= THIS_MODULE
, .protocol
= PF_PHONET
, .msgtype
= RTM_GETROUTE
,
330 .dumpit
= route_dumpit
, .flags
= RTNL_FLAG_DUMP_UNLOCKED
},
333 int __init
phonet_netlink_register(void)
335 return rtnl_register_many(phonet_rtnl_msg_handlers
);