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
, struct net_device
*dev
, u8 addr
,
23 u32 portid
, u32 seq
, int event
);
25 void phonet_address_notify(int event
, struct net_device
*dev
, u8 addr
)
30 skb
= nlmsg_new(NLMSG_ALIGN(sizeof(struct ifaddrmsg
)) +
31 nla_total_size(1), GFP_KERNEL
);
34 err
= fill_addr(skb
, dev
, addr
, 0, 0, event
);
36 WARN_ON(err
== -EMSGSIZE
);
40 rtnl_notify(skb
, dev_net(dev
), 0,
41 RTNLGRP_PHONET_IFADDR
, NULL
, GFP_KERNEL
);
44 rtnl_set_sk_err(dev_net(dev
), RTNLGRP_PHONET_IFADDR
, err
);
47 static const struct nla_policy ifa_phonet_policy
[IFA_MAX
+1] = {
48 [IFA_LOCAL
] = { .type
= NLA_U8
},
51 static int addr_doit(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
52 struct netlink_ext_ack
*extack
)
54 struct net
*net
= sock_net(skb
->sk
);
55 struct nlattr
*tb
[IFA_MAX
+1];
56 struct net_device
*dev
;
57 struct ifaddrmsg
*ifm
;
61 if (!netlink_capable(skb
, CAP_NET_ADMIN
))
64 if (!netlink_capable(skb
, CAP_SYS_ADMIN
))
69 err
= nlmsg_parse_deprecated(nlh
, sizeof(*ifm
), tb
, IFA_MAX
,
70 ifa_phonet_policy
, extack
);
74 ifm
= nlmsg_data(nlh
);
75 if (tb
[IFA_LOCAL
] == NULL
)
77 pnaddr
= nla_get_u8(tb
[IFA_LOCAL
]);
79 /* Phonet addresses only have 6 high-order bits */
82 dev
= __dev_get_by_index(net
, ifm
->ifa_index
);
86 if (nlh
->nlmsg_type
== RTM_NEWADDR
)
87 err
= phonet_address_add(dev
, pnaddr
);
89 err
= phonet_address_del(dev
, pnaddr
);
91 phonet_address_notify(nlh
->nlmsg_type
, dev
, pnaddr
);
95 static int fill_addr(struct sk_buff
*skb
, struct net_device
*dev
, u8 addr
,
96 u32 portid
, u32 seq
, int event
)
98 struct ifaddrmsg
*ifm
;
101 nlh
= nlmsg_put(skb
, portid
, seq
, event
, sizeof(*ifm
), 0);
105 ifm
= nlmsg_data(nlh
);
106 ifm
->ifa_family
= AF_PHONET
;
107 ifm
->ifa_prefixlen
= 0;
108 ifm
->ifa_flags
= IFA_F_PERMANENT
;
109 ifm
->ifa_scope
= RT_SCOPE_LINK
;
110 ifm
->ifa_index
= dev
->ifindex
;
111 if (nla_put_u8(skb
, IFA_LOCAL
, addr
))
112 goto nla_put_failure
;
117 nlmsg_cancel(skb
, nlh
);
121 static int getaddr_dumpit(struct sk_buff
*skb
, struct netlink_callback
*cb
)
123 struct phonet_device_list
*pndevs
;
124 struct phonet_device
*pnd
;
125 int dev_idx
= 0, dev_start_idx
= cb
->args
[0];
126 int addr_idx
= 0, addr_start_idx
= cb
->args
[1];
128 pndevs
= phonet_device_list(sock_net(skb
->sk
));
130 list_for_each_entry_rcu(pnd
, &pndevs
->list
, list
) {
133 if (dev_idx
> dev_start_idx
)
135 if (dev_idx
++ < dev_start_idx
)
139 for_each_set_bit(addr
, pnd
->addrs
, 64) {
140 if (addr_idx
++ < addr_start_idx
)
143 if (fill_addr(skb
, pnd
->netdev
, addr
<< 2,
144 NETLINK_CB(cb
->skb
).portid
,
145 cb
->nlh
->nlmsg_seq
, RTM_NEWADDR
) < 0)
152 cb
->args
[0] = dev_idx
;
153 cb
->args
[1] = addr_idx
;
158 /* Routes handling */
160 static int fill_route(struct sk_buff
*skb
, struct net_device
*dev
, u8 dst
,
161 u32 portid
, u32 seq
, int event
)
164 struct nlmsghdr
*nlh
;
166 nlh
= nlmsg_put(skb
, portid
, seq
, event
, sizeof(*rtm
), 0);
170 rtm
= nlmsg_data(nlh
);
171 rtm
->rtm_family
= AF_PHONET
;
172 rtm
->rtm_dst_len
= 6;
173 rtm
->rtm_src_len
= 0;
175 rtm
->rtm_table
= RT_TABLE_MAIN
;
176 rtm
->rtm_protocol
= RTPROT_STATIC
;
177 rtm
->rtm_scope
= RT_SCOPE_UNIVERSE
;
178 rtm
->rtm_type
= RTN_UNICAST
;
180 if (nla_put_u8(skb
, RTA_DST
, dst
) ||
181 nla_put_u32(skb
, RTA_OIF
, dev
->ifindex
))
182 goto nla_put_failure
;
187 nlmsg_cancel(skb
, nlh
);
191 void rtm_phonet_notify(int event
, struct net_device
*dev
, u8 dst
)
196 skb
= nlmsg_new(NLMSG_ALIGN(sizeof(struct ifaddrmsg
)) +
197 nla_total_size(1) + nla_total_size(4), GFP_KERNEL
);
200 err
= fill_route(skb
, dev
, dst
, 0, 0, event
);
202 WARN_ON(err
== -EMSGSIZE
);
206 rtnl_notify(skb
, dev_net(dev
), 0,
207 RTNLGRP_PHONET_ROUTE
, NULL
, GFP_KERNEL
);
210 rtnl_set_sk_err(dev_net(dev
), RTNLGRP_PHONET_ROUTE
, err
);
213 static const struct nla_policy rtm_phonet_policy
[RTA_MAX
+1] = {
214 [RTA_DST
] = { .type
= NLA_U8
},
215 [RTA_OIF
] = { .type
= NLA_U32
},
218 static int route_doit(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
219 struct netlink_ext_ack
*extack
)
221 struct net
*net
= sock_net(skb
->sk
);
222 struct nlattr
*tb
[RTA_MAX
+1];
223 struct net_device
*dev
;
228 if (!netlink_capable(skb
, CAP_NET_ADMIN
))
231 if (!netlink_capable(skb
, CAP_SYS_ADMIN
))
236 err
= nlmsg_parse_deprecated(nlh
, sizeof(*rtm
), tb
, RTA_MAX
,
237 rtm_phonet_policy
, extack
);
241 rtm
= nlmsg_data(nlh
);
242 if (rtm
->rtm_table
!= RT_TABLE_MAIN
|| rtm
->rtm_type
!= RTN_UNICAST
)
244 if (tb
[RTA_DST
] == NULL
|| tb
[RTA_OIF
] == NULL
)
246 dst
= nla_get_u8(tb
[RTA_DST
]);
247 if (dst
& 3) /* Phonet addresses only have 6 high-order bits */
250 dev
= __dev_get_by_index(net
, nla_get_u32(tb
[RTA_OIF
]));
254 if (nlh
->nlmsg_type
== RTM_NEWROUTE
)
255 err
= phonet_route_add(dev
, dst
);
257 err
= phonet_route_del(dev
, dst
);
259 rtm_phonet_notify(nlh
->nlmsg_type
, dev
, dst
);
263 static int route_dumpit(struct sk_buff
*skb
, struct netlink_callback
*cb
)
265 struct net
*net
= sock_net(skb
->sk
);
269 for (addr
= cb
->args
[0]; addr
< 64; addr
++) {
270 struct net_device
*dev
= phonet_route_get_rcu(net
, addr
<< 2);
275 if (fill_route(skb
, dev
, addr
<< 2, NETLINK_CB(cb
->skb
).portid
,
276 cb
->nlh
->nlmsg_seq
, RTM_NEWROUTE
) < 0)
287 int __init
phonet_netlink_register(void)
289 int err
= rtnl_register_module(THIS_MODULE
, PF_PHONET
, RTM_NEWADDR
,
294 /* Further rtnl_register_module() cannot fail */
295 rtnl_register_module(THIS_MODULE
, PF_PHONET
, RTM_DELADDR
,
297 rtnl_register_module(THIS_MODULE
, PF_PHONET
, RTM_GETADDR
,
298 NULL
, getaddr_dumpit
, 0);
299 rtnl_register_module(THIS_MODULE
, PF_PHONET
, RTM_NEWROUTE
,
300 route_doit
, NULL
, 0);
301 rtnl_register_module(THIS_MODULE
, PF_PHONET
, RTM_DELROUTE
,
302 route_doit
, NULL
, 0);
303 rtnl_register_module(THIS_MODULE
, PF_PHONET
, RTM_GETROUTE
,
304 NULL
, route_dumpit
, 0);