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 static int fill_addr(struct sk_buff *skb, struct net_device *dev, u8 addr,
33 u32 pid, u32 seq, int event);
35 static void rtmsg_notify(int event, struct net_device *dev, u8 addr)
40 skb = nlmsg_new(NLMSG_ALIGN(sizeof(struct ifaddrmsg)) +
41 nla_total_size(1), GFP_KERNEL);
44 err = fill_addr(skb, dev, addr, 0, 0, event);
46 WARN_ON(err == -EMSGSIZE);
50 err = rtnl_notify(skb, dev_net(dev), 0,
51 RTNLGRP_PHONET_IFADDR, NULL, GFP_KERNEL);
54 rtnl_set_sk_err(dev_net(dev), RTNLGRP_PHONET_IFADDR, err);
57 static int newaddr_doit(struct sk_buff *skb, struct nlmsghdr *nlm, void *attr)
59 struct rtattr **rta = attr;
60 struct ifaddrmsg *ifm = NLMSG_DATA(nlm);
61 struct net_device *dev;
65 if (!capable(CAP_SYS_ADMIN))
70 if (rta[IFA_LOCAL - 1] == NULL)
73 dev = __dev_get_by_index(&init_net, ifm->ifa_index);
77 if (ifm->ifa_prefixlen > 0)
80 memcpy(&pnaddr, RTA_DATA(rta[IFA_LOCAL - 1]), 1);
82 err = phonet_address_add(dev, pnaddr);
84 rtmsg_notify(RTM_NEWADDR, dev, pnaddr);
88 static int deladdr_doit(struct sk_buff *skb, struct nlmsghdr *nlm, void *attr)
90 struct rtattr **rta = attr;
91 struct ifaddrmsg *ifm = NLMSG_DATA(nlm);
92 struct net_device *dev;
96 if (!capable(CAP_SYS_ADMIN))
101 if (rta[IFA_LOCAL - 1] == NULL)
104 dev = __dev_get_by_index(&init_net, ifm->ifa_index);
108 if (ifm->ifa_prefixlen > 0)
109 return -EADDRNOTAVAIL;
111 memcpy(&pnaddr, RTA_DATA(rta[IFA_LOCAL - 1]), 1);
113 err = phonet_address_del(dev, pnaddr);
115 rtmsg_notify(RTM_DELADDR, dev, pnaddr);
119 static int fill_addr(struct sk_buff *skb, struct net_device *dev, u8 addr,
120 u32 pid, u32 seq, int event)
122 struct ifaddrmsg *ifm;
123 struct nlmsghdr *nlh;
124 unsigned int orig_len = skb->len;
126 nlh = NLMSG_PUT(skb, pid, seq, event, sizeof(struct ifaddrmsg));
127 ifm = NLMSG_DATA(nlh);
128 ifm->ifa_family = AF_PHONET;
129 ifm->ifa_prefixlen = 0;
130 ifm->ifa_flags = IFA_F_PERMANENT;
131 ifm->ifa_scope = RT_SCOPE_HOST;
132 ifm->ifa_index = dev->ifindex;
133 RTA_PUT(skb, IFA_LOCAL, 1, &addr);
134 nlh->nlmsg_len = skb->len - orig_len;
140 skb_trim(skb, orig_len);
145 static int getaddr_dumpit(struct sk_buff *skb, struct netlink_callback *cb)
147 struct phonet_device *pnd;
148 int dev_idx = 0, dev_start_idx = cb->args[0];
149 int addr_idx = 0, addr_start_idx = cb->args[1];
151 spin_lock_bh(&pndevs.lock);
152 list_for_each_entry(pnd, &pndevs.list, list) {
155 if (dev_idx > dev_start_idx)
157 if (dev_idx++ < dev_start_idx)
161 for (addr = find_first_bit(pnd->addrs, 64); addr < 64;
162 addr = find_next_bit(pnd->addrs, 64, 1+addr)) {
163 if (addr_idx++ < addr_start_idx)
166 if (fill_addr(skb, pnd->netdev, addr << 2,
167 NETLINK_CB(cb->skb).pid,
168 cb->nlh->nlmsg_seq, RTM_NEWADDR))
174 spin_unlock_bh(&pndevs.lock);
175 cb->args[0] = dev_idx;
176 cb->args[1] = addr_idx;
181 void __init phonet_netlink_register(void)
183 rtnl_register(PF_PHONET, RTM_NEWADDR, newaddr_doit, NULL);
184 rtnl_register(PF_PHONET, RTM_DELADDR, deladdr_doit, NULL);
185 rtnl_register(PF_PHONET, RTM_GETADDR, NULL, getaddr_dumpit);