4 * Phonet netlink interface
6 * Copyright (C) 2008 Nokia Corporation.
8 * Authors: Sakari Ailus <sakari.ailus@nokia.com>
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License
13 * version 2 as published by the Free Software Foundation.
15 * This program is distributed in the hope that it will be useful, but
16 * WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
26 #include <linux/kernel.h>
27 #include <linux/netlink.h>
28 #include <linux/phonet.h>
29 #include <linux/slab.h>
31 #include <net/phonet/pn_dev.h>
33 /* Device address handling */
35 static int fill_addr(struct sk_buff
*skb
, struct net_device
*dev
, u8 addr
,
36 u32 portid
, u32 seq
, int event
);
38 void phonet_address_notify(int event
, struct net_device
*dev
, u8 addr
)
43 skb
= nlmsg_new(NLMSG_ALIGN(sizeof(struct ifaddrmsg
)) +
44 nla_total_size(1), GFP_KERNEL
);
47 err
= fill_addr(skb
, dev
, addr
, 0, 0, event
);
49 WARN_ON(err
== -EMSGSIZE
);
53 rtnl_notify(skb
, dev_net(dev
), 0,
54 RTNLGRP_PHONET_IFADDR
, NULL
, GFP_KERNEL
);
57 rtnl_set_sk_err(dev_net(dev
), RTNLGRP_PHONET_IFADDR
, err
);
60 static const struct nla_policy ifa_phonet_policy
[IFA_MAX
+1] = {
61 [IFA_LOCAL
] = { .type
= NLA_U8
},
64 static int addr_doit(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
65 struct netlink_ext_ack
*extack
)
67 struct net
*net
= sock_net(skb
->sk
);
68 struct nlattr
*tb
[IFA_MAX
+1];
69 struct net_device
*dev
;
70 struct ifaddrmsg
*ifm
;
74 if (!netlink_capable(skb
, CAP_NET_ADMIN
))
77 if (!netlink_capable(skb
, CAP_SYS_ADMIN
))
82 err
= nlmsg_parse(nlh
, sizeof(*ifm
), tb
, IFA_MAX
, ifa_phonet_policy
,
87 ifm
= nlmsg_data(nlh
);
88 if (tb
[IFA_LOCAL
] == NULL
)
90 pnaddr
= nla_get_u8(tb
[IFA_LOCAL
]);
92 /* Phonet addresses only have 6 high-order bits */
95 dev
= __dev_get_by_index(net
, ifm
->ifa_index
);
99 if (nlh
->nlmsg_type
== RTM_NEWADDR
)
100 err
= phonet_address_add(dev
, pnaddr
);
102 err
= phonet_address_del(dev
, pnaddr
);
104 phonet_address_notify(nlh
->nlmsg_type
, dev
, pnaddr
);
108 static int fill_addr(struct sk_buff
*skb
, struct net_device
*dev
, u8 addr
,
109 u32 portid
, u32 seq
, int event
)
111 struct ifaddrmsg
*ifm
;
112 struct nlmsghdr
*nlh
;
114 nlh
= nlmsg_put(skb
, portid
, seq
, event
, sizeof(*ifm
), 0);
118 ifm
= nlmsg_data(nlh
);
119 ifm
->ifa_family
= AF_PHONET
;
120 ifm
->ifa_prefixlen
= 0;
121 ifm
->ifa_flags
= IFA_F_PERMANENT
;
122 ifm
->ifa_scope
= RT_SCOPE_LINK
;
123 ifm
->ifa_index
= dev
->ifindex
;
124 if (nla_put_u8(skb
, IFA_LOCAL
, addr
))
125 goto nla_put_failure
;
130 nlmsg_cancel(skb
, nlh
);
134 static int getaddr_dumpit(struct sk_buff
*skb
, struct netlink_callback
*cb
)
136 struct phonet_device_list
*pndevs
;
137 struct phonet_device
*pnd
;
138 int dev_idx
= 0, dev_start_idx
= cb
->args
[0];
139 int addr_idx
= 0, addr_start_idx
= cb
->args
[1];
141 pndevs
= phonet_device_list(sock_net(skb
->sk
));
143 list_for_each_entry_rcu(pnd
, &pndevs
->list
, list
) {
146 if (dev_idx
> dev_start_idx
)
148 if (dev_idx
++ < dev_start_idx
)
152 for_each_set_bit(addr
, pnd
->addrs
, 64) {
153 if (addr_idx
++ < addr_start_idx
)
156 if (fill_addr(skb
, pnd
->netdev
, addr
<< 2,
157 NETLINK_CB(cb
->skb
).portid
,
158 cb
->nlh
->nlmsg_seq
, RTM_NEWADDR
) < 0)
165 cb
->args
[0] = dev_idx
;
166 cb
->args
[1] = addr_idx
;
171 /* Routes handling */
173 static int fill_route(struct sk_buff
*skb
, struct net_device
*dev
, 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
) ||
194 nla_put_u32(skb
, RTA_OIF
, dev
->ifindex
))
195 goto nla_put_failure
;
200 nlmsg_cancel(skb
, nlh
);
204 void rtm_phonet_notify(int event
, struct net_device
*dev
, u8 dst
)
209 skb
= nlmsg_new(NLMSG_ALIGN(sizeof(struct ifaddrmsg
)) +
210 nla_total_size(1) + nla_total_size(4), GFP_KERNEL
);
213 err
= fill_route(skb
, dev
, dst
, 0, 0, event
);
215 WARN_ON(err
== -EMSGSIZE
);
219 rtnl_notify(skb
, dev_net(dev
), 0,
220 RTNLGRP_PHONET_ROUTE
, NULL
, GFP_KERNEL
);
223 rtnl_set_sk_err(dev_net(dev
), 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 struct net_device
*dev
;
241 if (!netlink_capable(skb
, CAP_NET_ADMIN
))
244 if (!netlink_capable(skb
, CAP_SYS_ADMIN
))
249 err
= nlmsg_parse(nlh
, sizeof(*rtm
), tb
, RTA_MAX
, rtm_phonet_policy
,
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 dev
= __dev_get_by_index(net
, nla_get_u32(tb
[RTA_OIF
]));
267 if (nlh
->nlmsg_type
== RTM_NEWROUTE
)
268 err
= phonet_route_add(dev
, dst
);
270 err
= phonet_route_del(dev
, dst
);
272 rtm_phonet_notify(nlh
->nlmsg_type
, dev
, dst
);
276 static int route_dumpit(struct sk_buff
*skb
, struct netlink_callback
*cb
)
278 struct net
*net
= sock_net(skb
->sk
);
282 for (addr
= cb
->args
[0]; addr
< 64; addr
++) {
283 struct net_device
*dev
= phonet_route_get_rcu(net
, addr
<< 2);
288 if (fill_route(skb
, dev
, addr
<< 2, NETLINK_CB(cb
->skb
).portid
,
289 cb
->nlh
->nlmsg_seq
, RTM_NEWROUTE
) < 0)
300 int __init
phonet_netlink_register(void)
302 int err
= rtnl_register_module(THIS_MODULE
, PF_PHONET
, RTM_NEWADDR
,
307 /* Further rtnl_register_module() cannot fail */
308 rtnl_register_module(THIS_MODULE
, PF_PHONET
, RTM_DELADDR
,
310 rtnl_register_module(THIS_MODULE
, PF_PHONET
, RTM_GETADDR
,
311 NULL
, getaddr_dumpit
, 0);
312 rtnl_register_module(THIS_MODULE
, PF_PHONET
, RTM_NEWROUTE
,
313 route_doit
, NULL
, 0);
314 rtnl_register_module(THIS_MODULE
, PF_PHONET
, RTM_DELROUTE
,
315 route_doit
, NULL
, 0);
316 rtnl_register_module(THIS_MODULE
, PF_PHONET
, RTM_GETROUTE
,
317 NULL
, route_dumpit
, 0);