diff options
author | Rémi Denis-Courmont <remi.denis-courmont@nokia.com> | 2009-10-13 20:48:29 -0400 |
---|---|---|
committer | David S. Miller <davem@davemloft.net> | 2009-10-14 18:04:17 -0400 |
commit | f062f41d06575744b9eaf725eef8a5d3b5f5b7ca (patch) | |
tree | 1ee1c966a2810c1f2a7274a51dba0387861efc68 /net/phonet/pn_netlink.c | |
parent | 55748ac0468134a89bc55aed6a9691e320caa8a9 (diff) |
Phonet: routing table Netlink interface
Signed-off-by: Rémi Denis-Courmont <remi.denis-courmont@nokia.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
Diffstat (limited to 'net/phonet/pn_netlink.c')
-rw-r--r-- | net/phonet/pn_netlink.c | 130 |
1 files changed, 130 insertions, 0 deletions
diff --git a/net/phonet/pn_netlink.c b/net/phonet/pn_netlink.c index d21fd3576610..d8f5d3fb9ee2 100644 --- a/net/phonet/pn_netlink.c +++ b/net/phonet/pn_netlink.c | |||
@@ -29,6 +29,8 @@ | |||
29 | #include <net/sock.h> | 29 | #include <net/sock.h> |
30 | #include <net/phonet/pn_dev.h> | 30 | #include <net/phonet/pn_dev.h> |
31 | 31 | ||
32 | /* Device address handling */ | ||
33 | |||
32 | static int fill_addr(struct sk_buff *skb, struct net_device *dev, u8 addr, | 34 | static int fill_addr(struct sk_buff *skb, struct net_device *dev, u8 addr, |
33 | u32 pid, u32 seq, int event); | 35 | u32 pid, u32 seq, int event); |
34 | 36 | ||
@@ -160,6 +162,131 @@ out: | |||
160 | return skb->len; | 162 | return skb->len; |
161 | } | 163 | } |
162 | 164 | ||
165 | /* Routes handling */ | ||
166 | |||
167 | static int fill_route(struct sk_buff *skb, struct net_device *dev, u8 dst, | ||
168 | u32 pid, u32 seq, int event) | ||
169 | { | ||
170 | struct rtmsg *rtm; | ||
171 | struct nlmsghdr *nlh; | ||
172 | |||
173 | nlh = nlmsg_put(skb, pid, seq, event, sizeof(*rtm), 0); | ||
174 | if (nlh == NULL) | ||
175 | return -EMSGSIZE; | ||
176 | |||
177 | rtm = nlmsg_data(nlh); | ||
178 | rtm->rtm_family = AF_PHONET; | ||
179 | rtm->rtm_dst_len = 6; | ||
180 | rtm->rtm_src_len = 0; | ||
181 | rtm->rtm_tos = 0; | ||
182 | rtm->rtm_table = RT_TABLE_MAIN; | ||
183 | rtm->rtm_protocol = RTPROT_STATIC; | ||
184 | rtm->rtm_scope = RT_SCOPE_UNIVERSE; | ||
185 | rtm->rtm_type = RTN_UNICAST; | ||
186 | rtm->rtm_flags = 0; | ||
187 | NLA_PUT_U8(skb, RTA_DST, dst); | ||
188 | NLA_PUT_U32(skb, RTA_OIF, dev->ifindex); | ||
189 | return nlmsg_end(skb, nlh); | ||
190 | |||
191 | nla_put_failure: | ||
192 | nlmsg_cancel(skb, nlh); | ||
193 | return -EMSGSIZE; | ||
194 | } | ||
195 | |||
196 | void rtm_phonet_notify(int event, struct net_device *dev, u8 dst) | ||
197 | { | ||
198 | struct sk_buff *skb; | ||
199 | int err = -ENOBUFS; | ||
200 | |||
201 | skb = nlmsg_new(NLMSG_ALIGN(sizeof(struct ifaddrmsg)) + | ||
202 | nla_total_size(1) + nla_total_size(4), GFP_KERNEL); | ||
203 | if (skb == NULL) | ||
204 | goto errout; | ||
205 | err = fill_route(skb, dev, dst, 0, 0, event); | ||
206 | if (err < 0) { | ||
207 | WARN_ON(err == -EMSGSIZE); | ||
208 | kfree_skb(skb); | ||
209 | goto errout; | ||
210 | } | ||
211 | rtnl_notify(skb, dev_net(dev), 0, | ||
212 | RTNLGRP_PHONET_ROUTE, NULL, GFP_KERNEL); | ||
213 | return; | ||
214 | errout: | ||
215 | if (err < 0) | ||
216 | rtnl_set_sk_err(dev_net(dev), RTNLGRP_PHONET_ROUTE, err); | ||
217 | } | ||
218 | |||
219 | static const struct nla_policy rtm_phonet_policy[RTA_MAX+1] = { | ||
220 | [RTA_DST] = { .type = NLA_U8 }, | ||
221 | [RTA_OIF] = { .type = NLA_U32 }, | ||
222 | }; | ||
223 | |||
224 | static int route_doit(struct sk_buff *skb, struct nlmsghdr *nlh, void *attr) | ||
225 | { | ||
226 | struct net *net = sock_net(skb->sk); | ||
227 | struct nlattr *tb[RTA_MAX+1]; | ||
228 | struct net_device *dev; | ||
229 | struct rtmsg *rtm; | ||
230 | int err; | ||
231 | u8 dst; | ||
232 | |||
233 | if (!capable(CAP_SYS_ADMIN)) | ||
234 | return -EPERM; | ||
235 | |||
236 | ASSERT_RTNL(); | ||
237 | |||
238 | err = nlmsg_parse(nlh, sizeof(*rtm), tb, RTA_MAX, rtm_phonet_policy); | ||
239 | if (err < 0) | ||
240 | return err; | ||
241 | |||
242 | rtm = nlmsg_data(nlh); | ||
243 | if (rtm->rtm_table != RT_TABLE_MAIN || rtm->rtm_type != RTN_UNICAST) | ||
244 | return -EINVAL; | ||
245 | if (tb[RTA_DST] == NULL || tb[RTA_OIF] == NULL) | ||
246 | return -EINVAL; | ||
247 | dst = nla_get_u8(tb[RTA_DST]); | ||
248 | if (dst & 3) /* Phonet addresses only have 6 high-order bits */ | ||
249 | return -EINVAL; | ||
250 | |||
251 | dev = __dev_get_by_index(net, nla_get_u32(tb[RTA_OIF])); | ||
252 | if (dev == NULL) | ||
253 | return -ENODEV; | ||
254 | |||
255 | if (nlh->nlmsg_type == RTM_NEWROUTE) | ||
256 | err = phonet_route_add(dev, dst); | ||
257 | else | ||
258 | err = phonet_route_del(dev, dst); | ||
259 | if (!err) | ||
260 | rtm_phonet_notify(nlh->nlmsg_type, dev, dst); | ||
261 | return err; | ||
262 | } | ||
263 | |||
264 | static int route_dumpit(struct sk_buff *skb, struct netlink_callback *cb) | ||
265 | { | ||
266 | struct net *net = sock_net(skb->sk); | ||
267 | u8 addr, addr_idx = 0, addr_start_idx = cb->args[0]; | ||
268 | |||
269 | for (addr = 0; addr < 64; addr++) { | ||
270 | struct net_device *dev; | ||
271 | |||
272 | dev = phonet_route_get(net, addr << 2); | ||
273 | if (!dev) | ||
274 | continue; | ||
275 | |||
276 | if (addr_idx++ < addr_start_idx) | ||
277 | continue; | ||
278 | if (fill_route(skb, dev, addr << 2, NETLINK_CB(cb->skb).pid, | ||
279 | cb->nlh->nlmsg_seq, RTM_NEWROUTE)) | ||
280 | goto out; | ||
281 | } | ||
282 | |||
283 | out: | ||
284 | cb->args[0] = addr_idx; | ||
285 | cb->args[1] = 0; | ||
286 | |||
287 | return skb->len; | ||
288 | } | ||
289 | |||
163 | int __init phonet_netlink_register(void) | 290 | int __init phonet_netlink_register(void) |
164 | { | 291 | { |
165 | int err = __rtnl_register(PF_PHONET, RTM_NEWADDR, addr_doit, NULL); | 292 | int err = __rtnl_register(PF_PHONET, RTM_NEWADDR, addr_doit, NULL); |
@@ -169,5 +296,8 @@ int __init phonet_netlink_register(void) | |||
169 | /* Further __rtnl_register() cannot fail */ | 296 | /* Further __rtnl_register() cannot fail */ |
170 | __rtnl_register(PF_PHONET, RTM_DELADDR, addr_doit, NULL); | 297 | __rtnl_register(PF_PHONET, RTM_DELADDR, addr_doit, NULL); |
171 | __rtnl_register(PF_PHONET, RTM_GETADDR, NULL, getaddr_dumpit); | 298 | __rtnl_register(PF_PHONET, RTM_GETADDR, NULL, getaddr_dumpit); |
299 | __rtnl_register(PF_PHONET, RTM_NEWROUTE, route_doit, NULL); | ||
300 | __rtnl_register(PF_PHONET, RTM_DELROUTE, route_doit, NULL); | ||
301 | __rtnl_register(PF_PHONET, RTM_GETROUTE, NULL, route_dumpit); | ||
172 | return 0; | 302 | return 0; |
173 | } | 303 | } |