blob: 1ceea1f92413d2d4b03d545152f339e628ab9b33 [file] [log] [blame]
Remi Denis-Courmont8fb39742008-09-22 20:04:30 -07001/*
2 * File: pn_netlink.c
3 *
4 * Phonet netlink interface
5 *
6 * Copyright (C) 2008 Nokia Corporation.
7 *
8 * Contact: Remi Denis-Courmont <remi.denis-courmont@nokia.com>
9 * Original author: Sakari Ailus <sakari.ailus@nokia.com>
10 *
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.
14 *
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.
19 *
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
23 * 02110-1301 USA
24 */
25
26#include <linux/kernel.h>
27#include <linux/netlink.h>
28#include <linux/phonet.h>
29#include <net/sock.h>
30#include <net/phonet/pn_dev.h>
31
32static int fill_addr(struct sk_buff *skb, struct net_device *dev, u8 addr,
33 u32 pid, u32 seq, int event);
34
35static void rtmsg_notify(int event, struct net_device *dev, u8 addr)
36{
37 struct sk_buff *skb;
38 int err = -ENOBUFS;
39
40 skb = nlmsg_new(NLMSG_ALIGN(sizeof(struct ifaddrmsg)) +
41 nla_total_size(1), GFP_KERNEL);
42 if (skb == NULL)
43 goto errout;
44 err = fill_addr(skb, dev, addr, 0, 0, event);
45 if (err < 0) {
46 WARN_ON(err == -EMSGSIZE);
47 kfree_skb(skb);
48 goto errout;
49 }
50 err = rtnl_notify(skb, dev_net(dev), 0,
51 RTNLGRP_PHONET_IFADDR, NULL, GFP_KERNEL);
52errout:
53 if (err < 0)
54 rtnl_set_sk_err(dev_net(dev), RTNLGRP_PHONET_IFADDR, err);
55}
56
Rémi Denis-Courmont89807132008-09-30 02:51:18 -070057static const struct nla_policy ifa_phonet_policy[IFA_MAX+1] = {
58 [IFA_LOCAL] = { .type = NLA_U8 },
59};
60
61static int addr_doit(struct sk_buff *skb, struct nlmsghdr *nlh, void *attr)
Remi Denis-Courmont8fb39742008-09-22 20:04:30 -070062{
Rémi Denis-Courmont89807132008-09-30 02:51:18 -070063 struct net *net = sock_net(skb->sk);
64 struct nlattr *tb[IFA_MAX+1];
Remi Denis-Courmont8fb39742008-09-22 20:04:30 -070065 struct net_device *dev;
Rémi Denis-Courmont89807132008-09-30 02:51:18 -070066 struct ifaddrmsg *ifm;
Remi Denis-Courmont8fb39742008-09-22 20:04:30 -070067 int err;
68 u8 pnaddr;
69
70 if (!capable(CAP_SYS_ADMIN))
71 return -EPERM;
72
73 ASSERT_RTNL();
74
Rémi Denis-Courmont89807132008-09-30 02:51:18 -070075 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_phonet_policy);
76 if (err < 0)
77 return err;
78
79 ifm = nlmsg_data(nlh);
80 if (tb[IFA_LOCAL] == NULL)
81 return -EINVAL;
82 pnaddr = nla_get_u8(tb[IFA_LOCAL]);
83 if (pnaddr & 3)
84 /* Phonet addresses only have 6 high-order bits */
Remi Denis-Courmont8fb39742008-09-22 20:04:30 -070085 return -EINVAL;
86
Rémi Denis-Courmont89807132008-09-30 02:51:18 -070087 dev = __dev_get_by_index(net, ifm->ifa_index);
Remi Denis-Courmont8fb39742008-09-22 20:04:30 -070088 if (dev == NULL)
89 return -ENODEV;
90
Rémi Denis-Courmont89807132008-09-30 02:51:18 -070091 if (nlh->nlmsg_type == RTM_NEWADDR)
92 err = phonet_address_add(dev, pnaddr);
93 else
94 err = phonet_address_del(dev, pnaddr);
Remi Denis-Courmont8fb39742008-09-22 20:04:30 -070095 if (!err)
Rémi Denis-Courmont89807132008-09-30 02:51:18 -070096 rtmsg_notify(nlh->nlmsg_type, dev, pnaddr);
Remi Denis-Courmont8fb39742008-09-22 20:04:30 -070097 return err;
98}
99
100static int fill_addr(struct sk_buff *skb, struct net_device *dev, u8 addr,
101 u32 pid, u32 seq, int event)
102{
103 struct ifaddrmsg *ifm;
104 struct nlmsghdr *nlh;
Remi Denis-Courmont8fb39742008-09-22 20:04:30 -0700105
Rémi Denis-Courmont89807132008-09-30 02:51:18 -0700106 nlh = nlmsg_put(skb, pid, seq, event, sizeof(*ifm), 0);
107 if (nlh == NULL)
108 return -EMSGSIZE;
109
110 ifm = nlmsg_data(nlh);
Remi Denis-Courmont8fb39742008-09-22 20:04:30 -0700111 ifm->ifa_family = AF_PHONET;
112 ifm->ifa_prefixlen = 0;
113 ifm->ifa_flags = IFA_F_PERMANENT;
Rémi Denis-Courmont89807132008-09-30 02:51:18 -0700114 ifm->ifa_scope = RT_SCOPE_LINK;
Remi Denis-Courmont8fb39742008-09-22 20:04:30 -0700115 ifm->ifa_index = dev->ifindex;
Rémi Denis-Courmont89807132008-09-30 02:51:18 -0700116 NLA_PUT_U8(skb, IFA_LOCAL, addr);
117 return nlmsg_end(skb, nlh);
Remi Denis-Courmont8fb39742008-09-22 20:04:30 -0700118
Rémi Denis-Courmont89807132008-09-30 02:51:18 -0700119nla_put_failure:
120 nlmsg_cancel(skb, nlh);
121 return -EMSGSIZE;
Remi Denis-Courmont8fb39742008-09-22 20:04:30 -0700122}
123
124static int getaddr_dumpit(struct sk_buff *skb, struct netlink_callback *cb)
125{
remi.denis-courmont@nokia9a3b7a42009-01-23 03:00:30 +0000126 struct phonet_device_list *pndevs;
Remi Denis-Courmont8fb39742008-09-22 20:04:30 -0700127 struct phonet_device *pnd;
128 int dev_idx = 0, dev_start_idx = cb->args[0];
129 int addr_idx = 0, addr_start_idx = cb->args[1];
130
remi.denis-courmont@nokia9a3b7a42009-01-23 03:00:30 +0000131 pndevs = phonet_device_list(sock_net(skb->sk));
132 spin_lock_bh(&pndevs->lock);
133 list_for_each_entry(pnd, &pndevs->list, list) {
Remi Denis-Courmont8fb39742008-09-22 20:04:30 -0700134 u8 addr;
135
136 if (dev_idx > dev_start_idx)
137 addr_start_idx = 0;
138 if (dev_idx++ < dev_start_idx)
139 continue;
140
141 addr_idx = 0;
142 for (addr = find_first_bit(pnd->addrs, 64); addr < 64;
143 addr = find_next_bit(pnd->addrs, 64, 1+addr)) {
144 if (addr_idx++ < addr_start_idx)
145 continue;
146
147 if (fill_addr(skb, pnd->netdev, addr << 2,
148 NETLINK_CB(cb->skb).pid,
149 cb->nlh->nlmsg_seq, RTM_NEWADDR))
150 goto out;
151 }
152 }
153
154out:
remi.denis-courmont@nokia9a3b7a42009-01-23 03:00:30 +0000155 spin_unlock_bh(&pndevs->lock);
Remi Denis-Courmont8fb39742008-09-22 20:04:30 -0700156 cb->args[0] = dev_idx;
157 cb->args[1] = addr_idx;
158
159 return skb->len;
160}
161
remi.denis-courmont@nokia660f7062009-01-23 03:00:28 +0000162int __init phonet_netlink_register(void)
Remi Denis-Courmont8fb39742008-09-22 20:04:30 -0700163{
remi.denis-courmont@nokia660f7062009-01-23 03:00:28 +0000164 int err = __rtnl_register(PF_PHONET, RTM_NEWADDR, addr_doit, NULL);
165 if (err)
166 return err;
167
168 /* Further __rtnl_register() cannot fail */
169 __rtnl_register(PF_PHONET, RTM_DELADDR, addr_doit, NULL);
170 __rtnl_register(PF_PHONET, RTM_GETADDR, NULL, getaddr_dumpit);
171 return 0;
Remi Denis-Courmont8fb39742008-09-22 20:04:30 -0700172}