4 * Phonet netlink interface
6 * Copyright (C) 2008 Nokia Corporation.
8 * Contact: Remi Denis-Courmont <remi.denis-courmont@nokia.com>
9 * Original author: 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>
30 #include <net/phonet/pn_dev.h>
32 /* Device address handling */
34 static int fill_addr(struct sk_buff
*skb
, struct net_device
*dev
, u8 addr
,
35 u32 pid
, u32 seq
, int event
);
37 void phonet_address_notify(int event
, struct net_device
*dev
, u8 addr
)
42 skb
= nlmsg_new(NLMSG_ALIGN(sizeof(struct ifaddrmsg
)) +
43 nla_total_size(1), GFP_KERNEL
);
46 err
= fill_addr(skb
, dev
, addr
, 0, 0, event
);
48 WARN_ON(err
== -EMSGSIZE
);
52 rtnl_notify(skb
, dev_net(dev
), 0,
53 RTNLGRP_PHONET_IFADDR
, NULL
, GFP_KERNEL
);
56 rtnl_set_sk_err(dev_net(dev
), RTNLGRP_PHONET_IFADDR
, err
);
59 static const struct nla_policy ifa_phonet_policy
[IFA_MAX
+1] = {
60 [IFA_LOCAL
] = { .type
= NLA_U8
},
63 static int addr_doit(struct sk_buff
*skb
, struct nlmsghdr
*nlh
, void *attr
)
65 struct net
*net
= sock_net(skb
->sk
);
66 struct nlattr
*tb
[IFA_MAX
+1];
67 struct net_device
*dev
;
68 struct ifaddrmsg
*ifm
;
72 if (!capable(CAP_SYS_ADMIN
))
77 err
= nlmsg_parse(nlh
, sizeof(*ifm
), tb
, IFA_MAX
, ifa_phonet_policy
);
81 ifm
= nlmsg_data(nlh
);
82 if (tb
[IFA_LOCAL
] == NULL
)
84 pnaddr
= nla_get_u8(tb
[IFA_LOCAL
]);
86 /* Phonet addresses only have 6 high-order bits */
89 dev
= __dev_get_by_index(net
, ifm
->ifa_index
);
93 if (nlh
->nlmsg_type
== RTM_NEWADDR
)
94 err
= phonet_address_add(dev
, pnaddr
);
96 err
= phonet_address_del(dev
, pnaddr
);
98 phonet_address_notify(nlh
->nlmsg_type
, dev
, pnaddr
);
102 static int fill_addr(struct sk_buff
*skb
, struct net_device
*dev
, u8 addr
,
103 u32 pid
, u32 seq
, int event
)
105 struct ifaddrmsg
*ifm
;
106 struct nlmsghdr
*nlh
;
108 nlh
= nlmsg_put(skb
, pid
, 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
= dev
->ifindex
;
118 NLA_PUT_U8(skb
, IFA_LOCAL
, addr
);
119 return nlmsg_end(skb
, nlh
);
122 nlmsg_cancel(skb
, nlh
);
126 static int getaddr_dumpit(struct sk_buff
*skb
, struct netlink_callback
*cb
)
128 struct phonet_device_list
*pndevs
;
129 struct phonet_device
*pnd
;
130 int dev_idx
= 0, dev_start_idx
= cb
->args
[0];
131 int addr_idx
= 0, addr_start_idx
= cb
->args
[1];
133 pndevs
= phonet_device_list(sock_net(skb
->sk
));
135 list_for_each_entry_rcu(pnd
, &pndevs
->list
, list
) {
138 if (dev_idx
> dev_start_idx
)
140 if (dev_idx
++ < dev_start_idx
)
144 for (addr
= find_first_bit(pnd
->addrs
, 64); addr
< 64;
145 addr
= find_next_bit(pnd
->addrs
, 64, 1+addr
)) {
146 if (addr_idx
++ < addr_start_idx
)
149 if (fill_addr(skb
, pnd
->netdev
, addr
<< 2,
150 NETLINK_CB(cb
->skb
).pid
,
151 cb
->nlh
->nlmsg_seq
, RTM_NEWADDR
) < 0)
158 cb
->args
[0] = dev_idx
;
159 cb
->args
[1] = addr_idx
;
164 /* Routes handling */
166 static int fill_route(struct sk_buff
*skb
, struct net_device
*dev
, u8 dst
,
167 u32 pid
, u32 seq
, int event
)
170 struct nlmsghdr
*nlh
;
172 nlh
= nlmsg_put(skb
, pid
, seq
, event
, sizeof(*rtm
), 0);
176 rtm
= nlmsg_data(nlh
);
177 rtm
->rtm_family
= AF_PHONET
;
178 rtm
->rtm_dst_len
= 6;
179 rtm
->rtm_src_len
= 0;
181 rtm
->rtm_table
= RT_TABLE_MAIN
;
182 rtm
->rtm_protocol
= RTPROT_STATIC
;
183 rtm
->rtm_scope
= RT_SCOPE_UNIVERSE
;
184 rtm
->rtm_type
= RTN_UNICAST
;
186 NLA_PUT_U8(skb
, RTA_DST
, dst
);
187 NLA_PUT_U32(skb
, RTA_OIF
, dev
->ifindex
);
188 return nlmsg_end(skb
, nlh
);
191 nlmsg_cancel(skb
, nlh
);
195 void rtm_phonet_notify(int event
, struct net_device
*dev
, u8 dst
)
200 skb
= nlmsg_new(NLMSG_ALIGN(sizeof(struct ifaddrmsg
)) +
201 nla_total_size(1) + nla_total_size(4), GFP_KERNEL
);
204 err
= fill_route(skb
, dev
, dst
, 0, 0, event
);
206 WARN_ON(err
== -EMSGSIZE
);
210 rtnl_notify(skb
, dev_net(dev
), 0,
211 RTNLGRP_PHONET_ROUTE
, NULL
, GFP_KERNEL
);
214 rtnl_set_sk_err(dev_net(dev
), RTNLGRP_PHONET_ROUTE
, err
);
217 static const struct nla_policy rtm_phonet_policy
[RTA_MAX
+1] = {
218 [RTA_DST
] = { .type
= NLA_U8
},
219 [RTA_OIF
] = { .type
= NLA_U32
},
222 static int route_doit(struct sk_buff
*skb
, struct nlmsghdr
*nlh
, void *attr
)
224 struct net
*net
= sock_net(skb
->sk
);
225 struct nlattr
*tb
[RTA_MAX
+1];
226 struct net_device
*dev
;
231 if (!capable(CAP_SYS_ADMIN
))
236 err
= nlmsg_parse(nlh
, sizeof(*rtm
), tb
, RTA_MAX
, rtm_phonet_policy
);
240 rtm
= nlmsg_data(nlh
);
241 if (rtm
->rtm_table
!= RT_TABLE_MAIN
|| rtm
->rtm_type
!= RTN_UNICAST
)
243 if (tb
[RTA_DST
] == NULL
|| tb
[RTA_OIF
] == NULL
)
245 dst
= nla_get_u8(tb
[RTA_DST
]);
246 if (dst
& 3) /* Phonet addresses only have 6 high-order bits */
249 dev
= __dev_get_by_index(net
, nla_get_u32(tb
[RTA_OIF
]));
253 if (nlh
->nlmsg_type
== RTM_NEWROUTE
)
254 err
= phonet_route_add(dev
, dst
);
256 err
= phonet_route_del(dev
, dst
);
258 rtm_phonet_notify(nlh
->nlmsg_type
, dev
, dst
);
262 static int route_dumpit(struct sk_buff
*skb
, struct netlink_callback
*cb
)
264 struct net
*net
= sock_net(skb
->sk
);
265 u8 addr
, addr_idx
= 0, addr_start_idx
= cb
->args
[0];
267 for (addr
= 0; addr
< 64; addr
++) {
268 struct net_device
*dev
;
270 dev
= phonet_route_get(net
, addr
<< 2);
274 if (addr_idx
++ < addr_start_idx
)
276 if (fill_route(skb
, dev
, addr
<< 2, NETLINK_CB(cb
->skb
).pid
,
277 cb
->nlh
->nlmsg_seq
, RTM_NEWROUTE
))
282 cb
->args
[0] = addr_idx
;
288 int __init
phonet_netlink_register(void)
290 int err
= __rtnl_register(PF_PHONET
, RTM_NEWADDR
, addr_doit
, NULL
);
294 /* Further __rtnl_register() cannot fail */
295 __rtnl_register(PF_PHONET
, RTM_DELADDR
, addr_doit
, NULL
);
296 __rtnl_register(PF_PHONET
, RTM_GETADDR
, NULL
, getaddr_dumpit
);
297 __rtnl_register(PF_PHONET
, RTM_NEWROUTE
, route_doit
, NULL
);
298 __rtnl_register(PF_PHONET
, RTM_DELROUTE
, route_doit
, NULL
);
299 __rtnl_register(PF_PHONET
, RTM_GETROUTE
, NULL
, route_dumpit
);