blob: 228be551e9c1d896eb97ba00ec78fc80cc47d2c0 [file] [log] [blame]
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001/*
2 * Linux IPv6 multicast routing support for BSD pim6sd
3 * Based on net/ipv4/ipmr.c.
4 *
5 * (c) 2004 Mickael Hoerdt, <hoerdt@clarinet.u-strasbg.fr>
6 * LSIIT Laboratory, Strasbourg, France
7 * (c) 2004 Jean-Philippe Andriot, <jean-philippe.andriot@6WIND.com>
8 * 6WIND, Paris, France
9 * Copyright (C)2007,2008 USAGI/WIDE Project
10 * YOSHIFUJI Hideaki <yoshfuji@linux-ipv6.org>
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version
15 * 2 of the License, or (at your option) any later version.
16 *
17 */
18
19#include <asm/system.h>
20#include <asm/uaccess.h>
21#include <linux/types.h>
22#include <linux/sched.h>
23#include <linux/errno.h>
24#include <linux/timer.h>
25#include <linux/mm.h>
26#include <linux/kernel.h>
27#include <linux/fcntl.h>
28#include <linux/stat.h>
29#include <linux/socket.h>
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +090030#include <linux/inet.h>
31#include <linux/netdevice.h>
32#include <linux/inetdevice.h>
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +090033#include <linux/proc_fs.h>
34#include <linux/seq_file.h>
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +090035#include <linux/init.h>
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +090036#include <net/protocol.h>
37#include <linux/skbuff.h>
38#include <net/sock.h>
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +090039#include <net/raw.h>
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +090040#include <linux/notifier.h>
41#include <linux/if_arp.h>
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +090042#include <net/checksum.h>
43#include <net/netlink.h>
44
45#include <net/ipv6.h>
46#include <net/ip6_route.h>
47#include <linux/mroute6.h>
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +090048#include <linux/pim.h>
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +090049#include <net/addrconf.h>
50#include <linux/netfilter_ipv6.h>
Dave Jones5d6e4302009-01-31 00:51:49 -080051#include <net/ip6_checksum.h>
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +090052
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +090053/* Big lock, protecting vif table, mrt cache and mroute socket state.
54 Note that the changes are semaphored via rtnl_lock.
55 */
56
57static DEFINE_RWLOCK(mrt_lock);
58
59/*
60 * Multicast router control variables
61 */
62
Benjamin Thery4e168802008-12-10 16:15:08 -080063#define MIF_EXISTS(_net, _idx) ((_net)->ipv6.vif6_table[_idx].dev != NULL)
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +090064
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +090065static struct mfc6_cache *mfc_unres_queue; /* Queue of unresolved entries */
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +090066
67/* Special spinlock for queue of unresolved entries */
68static DEFINE_SPINLOCK(mfc_unres_lock);
69
70/* We return to original Alan's scheme. Hash table of resolved
71 entries is changed only in process context and protected
72 with weak lock mrt_lock. Queue of unresolved entries is protected
73 with strong spinlock mfc_unres_lock.
74
75 In this case data path is free of exclusive locks at all.
76 */
77
78static struct kmem_cache *mrt_cachep __read_mostly;
79
80static int ip6_mr_forward(struct sk_buff *skb, struct mfc6_cache *cache);
Benjamin Thery8229efd2008-12-10 16:30:15 -080081static int ip6mr_cache_report(struct net *net, struct sk_buff *pkt,
82 mifi_t mifi, int assert);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +090083static int ip6mr_fill_mroute(struct sk_buff *skb, struct mfc6_cache *c, struct rtmsg *rtm);
Benjamin Thery8229efd2008-12-10 16:30:15 -080084static void mroute_clean_tables(struct net *net);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +090085
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +090086#ifdef CONFIG_IPV6_PIMSM_V2
87static struct inet6_protocol pim6_protocol;
88#endif
89
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +090090static struct timer_list ipmr_expire_timer;
91
92
93#ifdef CONFIG_PROC_FS
94
95struct ipmr_mfc_iter {
Benjamin Thery8b90fc72008-12-10 16:29:48 -080096 struct seq_net_private p;
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +090097 struct mfc6_cache **cache;
98 int ct;
99};
100
101
Benjamin Thery8b90fc72008-12-10 16:29:48 -0800102static struct mfc6_cache *ipmr_mfc_seq_idx(struct net *net,
103 struct ipmr_mfc_iter *it, loff_t pos)
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900104{
105 struct mfc6_cache *mfc;
106
Benjamin Thery8b90fc72008-12-10 16:29:48 -0800107 it->cache = net->ipv6.mfc6_cache_array;
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900108 read_lock(&mrt_lock);
Benjamin Thery4a6258a2008-12-10 16:24:07 -0800109 for (it->ct = 0; it->ct < MFC6_LINES; it->ct++)
Benjamin Thery8b90fc72008-12-10 16:29:48 -0800110 for (mfc = net->ipv6.mfc6_cache_array[it->ct];
Benjamin Thery4a6258a2008-12-10 16:24:07 -0800111 mfc; mfc = mfc->next)
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900112 if (pos-- == 0)
113 return mfc;
114 read_unlock(&mrt_lock);
115
116 it->cache = &mfc_unres_queue;
117 spin_lock_bh(&mfc_unres_lock);
118 for (mfc = mfc_unres_queue; mfc; mfc = mfc->next)
Benjamin Thery8b90fc72008-12-10 16:29:48 -0800119 if (net_eq(mfc6_net(mfc), net) &&
120 pos-- == 0)
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900121 return mfc;
122 spin_unlock_bh(&mfc_unres_lock);
123
124 it->cache = NULL;
125 return NULL;
126}
127
128
129
130
131/*
132 * The /proc interfaces to multicast routing /proc/ip6_mr_cache /proc/ip6_mr_vif
133 */
134
135struct ipmr_vif_iter {
Benjamin Thery8b90fc72008-12-10 16:29:48 -0800136 struct seq_net_private p;
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900137 int ct;
138};
139
Benjamin Thery8b90fc72008-12-10 16:29:48 -0800140static struct mif_device *ip6mr_vif_seq_idx(struct net *net,
141 struct ipmr_vif_iter *iter,
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900142 loff_t pos)
143{
Benjamin Thery8b90fc72008-12-10 16:29:48 -0800144 for (iter->ct = 0; iter->ct < net->ipv6.maxvif; ++iter->ct) {
145 if (!MIF_EXISTS(net, iter->ct))
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900146 continue;
147 if (pos-- == 0)
Benjamin Thery8b90fc72008-12-10 16:29:48 -0800148 return &net->ipv6.vif6_table[iter->ct];
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900149 }
150 return NULL;
151}
152
153static void *ip6mr_vif_seq_start(struct seq_file *seq, loff_t *pos)
154 __acquires(mrt_lock)
155{
Benjamin Thery8b90fc72008-12-10 16:29:48 -0800156 struct net *net = seq_file_net(seq);
157
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900158 read_lock(&mrt_lock);
Benjamin Thery8b90fc72008-12-10 16:29:48 -0800159 return *pos ? ip6mr_vif_seq_idx(net, seq->private, *pos - 1)
160 : SEQ_START_TOKEN;
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900161}
162
163static void *ip6mr_vif_seq_next(struct seq_file *seq, void *v, loff_t *pos)
164{
165 struct ipmr_vif_iter *iter = seq->private;
Benjamin Thery8b90fc72008-12-10 16:29:48 -0800166 struct net *net = seq_file_net(seq);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900167
168 ++*pos;
169 if (v == SEQ_START_TOKEN)
Benjamin Thery8b90fc72008-12-10 16:29:48 -0800170 return ip6mr_vif_seq_idx(net, iter, 0);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900171
Benjamin Thery8b90fc72008-12-10 16:29:48 -0800172 while (++iter->ct < net->ipv6.maxvif) {
173 if (!MIF_EXISTS(net, iter->ct))
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900174 continue;
Benjamin Thery8b90fc72008-12-10 16:29:48 -0800175 return &net->ipv6.vif6_table[iter->ct];
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900176 }
177 return NULL;
178}
179
180static void ip6mr_vif_seq_stop(struct seq_file *seq, void *v)
181 __releases(mrt_lock)
182{
183 read_unlock(&mrt_lock);
184}
185
186static int ip6mr_vif_seq_show(struct seq_file *seq, void *v)
187{
Benjamin Thery8b90fc72008-12-10 16:29:48 -0800188 struct net *net = seq_file_net(seq);
189
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900190 if (v == SEQ_START_TOKEN) {
191 seq_puts(seq,
192 "Interface BytesIn PktsIn BytesOut PktsOut Flags\n");
193 } else {
194 const struct mif_device *vif = v;
195 const char *name = vif->dev ? vif->dev->name : "none";
196
197 seq_printf(seq,
Al Virod430a222008-06-02 10:59:02 +0100198 "%2td %-10s %8ld %7ld %8ld %7ld %05X\n",
Benjamin Thery8b90fc72008-12-10 16:29:48 -0800199 vif - net->ipv6.vif6_table,
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900200 name, vif->bytes_in, vif->pkt_in,
201 vif->bytes_out, vif->pkt_out,
202 vif->flags);
203 }
204 return 0;
205}
206
207static struct seq_operations ip6mr_vif_seq_ops = {
208 .start = ip6mr_vif_seq_start,
209 .next = ip6mr_vif_seq_next,
210 .stop = ip6mr_vif_seq_stop,
211 .show = ip6mr_vif_seq_show,
212};
213
214static int ip6mr_vif_open(struct inode *inode, struct file *file)
215{
Benjamin Thery8b90fc72008-12-10 16:29:48 -0800216 return seq_open_net(inode, file, &ip6mr_vif_seq_ops,
217 sizeof(struct ipmr_vif_iter));
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900218}
219
220static struct file_operations ip6mr_vif_fops = {
221 .owner = THIS_MODULE,
222 .open = ip6mr_vif_open,
223 .read = seq_read,
224 .llseek = seq_lseek,
Benjamin Thery8b90fc72008-12-10 16:29:48 -0800225 .release = seq_release_net,
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900226};
227
228static void *ipmr_mfc_seq_start(struct seq_file *seq, loff_t *pos)
229{
Benjamin Thery8b90fc72008-12-10 16:29:48 -0800230 struct net *net = seq_file_net(seq);
231
232 return *pos ? ipmr_mfc_seq_idx(net, seq->private, *pos - 1)
233 : SEQ_START_TOKEN;
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900234}
235
236static void *ipmr_mfc_seq_next(struct seq_file *seq, void *v, loff_t *pos)
237{
238 struct mfc6_cache *mfc = v;
239 struct ipmr_mfc_iter *it = seq->private;
Benjamin Thery8b90fc72008-12-10 16:29:48 -0800240 struct net *net = seq_file_net(seq);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900241
242 ++*pos;
243
244 if (v == SEQ_START_TOKEN)
Benjamin Thery8b90fc72008-12-10 16:29:48 -0800245 return ipmr_mfc_seq_idx(net, seq->private, 0);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900246
247 if (mfc->next)
248 return mfc->next;
249
250 if (it->cache == &mfc_unres_queue)
251 goto end_of_list;
252
Benjamin Thery8b90fc72008-12-10 16:29:48 -0800253 BUG_ON(it->cache != net->ipv6.mfc6_cache_array);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900254
Benjamin Thery4a6258a2008-12-10 16:24:07 -0800255 while (++it->ct < MFC6_LINES) {
Benjamin Thery8b90fc72008-12-10 16:29:48 -0800256 mfc = net->ipv6.mfc6_cache_array[it->ct];
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900257 if (mfc)
258 return mfc;
259 }
260
261 /* exhausted cache_array, show unresolved */
262 read_unlock(&mrt_lock);
263 it->cache = &mfc_unres_queue;
264 it->ct = 0;
265
266 spin_lock_bh(&mfc_unres_lock);
267 mfc = mfc_unres_queue;
268 if (mfc)
269 return mfc;
270
271 end_of_list:
272 spin_unlock_bh(&mfc_unres_lock);
273 it->cache = NULL;
274
275 return NULL;
276}
277
278static void ipmr_mfc_seq_stop(struct seq_file *seq, void *v)
279{
280 struct ipmr_mfc_iter *it = seq->private;
Benjamin Thery8b90fc72008-12-10 16:29:48 -0800281 struct net *net = seq_file_net(seq);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900282
283 if (it->cache == &mfc_unres_queue)
284 spin_unlock_bh(&mfc_unres_lock);
Benjamin Thery8b90fc72008-12-10 16:29:48 -0800285 else if (it->cache == net->ipv6.mfc6_cache_array)
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900286 read_unlock(&mrt_lock);
287}
288
289static int ipmr_mfc_seq_show(struct seq_file *seq, void *v)
290{
291 int n;
Benjamin Thery8b90fc72008-12-10 16:29:48 -0800292 struct net *net = seq_file_net(seq);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900293
294 if (v == SEQ_START_TOKEN) {
295 seq_puts(seq,
296 "Group "
297 "Origin "
298 "Iif Pkts Bytes Wrong Oifs\n");
299 } else {
300 const struct mfc6_cache *mfc = v;
301 const struct ipmr_mfc_iter *it = seq->private;
302
Benjamin Thery999890b2008-12-03 22:22:16 -0800303 seq_printf(seq, "%pI6 %pI6 %-3hd",
Harvey Harrison0c6ce782008-10-28 16:09:23 -0700304 &mfc->mf6c_mcastgrp, &mfc->mf6c_origin,
Benjamin Thery1ea472e2008-12-03 22:21:47 -0800305 mfc->mf6c_parent);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900306
307 if (it->cache != &mfc_unres_queue) {
Benjamin Thery1ea472e2008-12-03 22:21:47 -0800308 seq_printf(seq, " %8lu %8lu %8lu",
309 mfc->mfc_un.res.pkt,
310 mfc->mfc_un.res.bytes,
311 mfc->mfc_un.res.wrong_if);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900312 for (n = mfc->mfc_un.res.minvif;
313 n < mfc->mfc_un.res.maxvif; n++) {
Benjamin Thery8b90fc72008-12-10 16:29:48 -0800314 if (MIF_EXISTS(net, n) &&
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900315 mfc->mfc_un.res.ttls[n] < 255)
316 seq_printf(seq,
317 " %2d:%-3d",
318 n, mfc->mfc_un.res.ttls[n]);
319 }
Benjamin Thery1ea472e2008-12-03 22:21:47 -0800320 } else {
321 /* unresolved mfc_caches don't contain
322 * pkt, bytes and wrong_if values
323 */
324 seq_printf(seq, " %8lu %8lu %8lu", 0ul, 0ul, 0ul);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900325 }
326 seq_putc(seq, '\n');
327 }
328 return 0;
329}
330
331static struct seq_operations ipmr_mfc_seq_ops = {
332 .start = ipmr_mfc_seq_start,
333 .next = ipmr_mfc_seq_next,
334 .stop = ipmr_mfc_seq_stop,
335 .show = ipmr_mfc_seq_show,
336};
337
338static int ipmr_mfc_open(struct inode *inode, struct file *file)
339{
Benjamin Thery8b90fc72008-12-10 16:29:48 -0800340 return seq_open_net(inode, file, &ipmr_mfc_seq_ops,
341 sizeof(struct ipmr_mfc_iter));
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900342}
343
344static struct file_operations ip6mr_mfc_fops = {
345 .owner = THIS_MODULE,
346 .open = ipmr_mfc_open,
347 .read = seq_read,
348 .llseek = seq_lseek,
Benjamin Thery8b90fc72008-12-10 16:29:48 -0800349 .release = seq_release_net,
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900350};
351#endif
352
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +0900353#ifdef CONFIG_IPV6_PIMSM_V2
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +0900354
355static int pim6_rcv(struct sk_buff *skb)
356{
357 struct pimreghdr *pim;
358 struct ipv6hdr *encap;
359 struct net_device *reg_dev = NULL;
Benjamin Thery8229efd2008-12-10 16:30:15 -0800360 struct net *net = dev_net(skb->dev);
361 int reg_vif_num = net->ipv6.mroute_reg_vif_num;
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +0900362
363 if (!pskb_may_pull(skb, sizeof(*pim) + sizeof(*encap)))
364 goto drop;
365
366 pim = (struct pimreghdr *)skb_transport_header(skb);
367 if (pim->type != ((PIM_VERSION << 4) | PIM_REGISTER) ||
368 (pim->flags & PIM_NULL_REGISTER) ||
Thomas Goff1d6e55f2009-01-27 22:39:59 -0800369 (csum_ipv6_magic(&ipv6_hdr(skb)->saddr, &ipv6_hdr(skb)->daddr,
370 sizeof(*pim), IPPROTO_PIM,
371 csum_partial((void *)pim, sizeof(*pim), 0)) &&
Al Viroec6b4862008-04-26 22:28:58 -0700372 csum_fold(skb_checksum(skb, 0, skb->len, 0))))
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +0900373 goto drop;
374
375 /* check if the inner packet is destined to mcast group */
376 encap = (struct ipv6hdr *)(skb_transport_header(skb) +
377 sizeof(*pim));
378
379 if (!ipv6_addr_is_multicast(&encap->daddr) ||
380 encap->payload_len == 0 ||
381 ntohs(encap->payload_len) + sizeof(*pim) > skb->len)
382 goto drop;
383
384 read_lock(&mrt_lock);
385 if (reg_vif_num >= 0)
Benjamin Thery8229efd2008-12-10 16:30:15 -0800386 reg_dev = net->ipv6.vif6_table[reg_vif_num].dev;
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +0900387 if (reg_dev)
388 dev_hold(reg_dev);
389 read_unlock(&mrt_lock);
390
391 if (reg_dev == NULL)
392 goto drop;
393
394 skb->mac_header = skb->network_header;
395 skb_pull(skb, (u8 *)encap - skb->data);
396 skb_reset_network_header(skb);
397 skb->dev = reg_dev;
Thomas Goff1d6e55f2009-01-27 22:39:59 -0800398 skb->protocol = htons(ETH_P_IPV6);
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +0900399 skb->ip_summed = 0;
400 skb->pkt_type = PACKET_HOST;
401 dst_release(skb->dst);
Pavel Emelyanovdc58c782008-05-21 14:17:54 -0700402 reg_dev->stats.rx_bytes += skb->len;
403 reg_dev->stats.rx_packets++;
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +0900404 skb->dst = NULL;
405 nf_reset(skb);
406 netif_rx(skb);
407 dev_put(reg_dev);
408 return 0;
409 drop:
410 kfree_skb(skb);
411 return 0;
412}
413
414static struct inet6_protocol pim6_protocol = {
415 .handler = pim6_rcv,
416};
417
418/* Service routines creating virtual interfaces: PIMREG */
419
420static int reg_vif_xmit(struct sk_buff *skb, struct net_device *dev)
421{
Benjamin Thery8229efd2008-12-10 16:30:15 -0800422 struct net *net = dev_net(dev);
423
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +0900424 read_lock(&mrt_lock);
Pavel Emelyanovdc58c782008-05-21 14:17:54 -0700425 dev->stats.tx_bytes += skb->len;
426 dev->stats.tx_packets++;
Benjamin Thery8229efd2008-12-10 16:30:15 -0800427 ip6mr_cache_report(net, skb, net->ipv6.mroute_reg_vif_num,
428 MRT6MSG_WHOLEPKT);
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +0900429 read_unlock(&mrt_lock);
430 kfree_skb(skb);
431 return 0;
432}
433
Stephen Hemminger007c3832008-11-20 20:28:35 -0800434static const struct net_device_ops reg_vif_netdev_ops = {
435 .ndo_start_xmit = reg_vif_xmit,
436};
437
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +0900438static void reg_vif_setup(struct net_device *dev)
439{
440 dev->type = ARPHRD_PIMREG;
441 dev->mtu = 1500 - sizeof(struct ipv6hdr) - 8;
442 dev->flags = IFF_NOARP;
Stephen Hemminger007c3832008-11-20 20:28:35 -0800443 dev->netdev_ops = &reg_vif_netdev_ops;
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +0900444 dev->destructor = free_netdev;
445}
446
Benjamin Thery8229efd2008-12-10 16:30:15 -0800447static struct net_device *ip6mr_reg_vif(struct net *net)
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +0900448{
449 struct net_device *dev;
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +0900450
Pavel Emelyanovdc58c782008-05-21 14:17:54 -0700451 dev = alloc_netdev(0, "pim6reg", reg_vif_setup);
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +0900452 if (dev == NULL)
453 return NULL;
454
Benjamin Thery8229efd2008-12-10 16:30:15 -0800455 dev_net_set(dev, net);
456
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +0900457 if (register_netdevice(dev)) {
458 free_netdev(dev);
459 return NULL;
460 }
461 dev->iflink = 0;
462
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +0900463 if (dev_open(dev))
464 goto failure;
465
Wang Chen7af3db72008-07-14 20:54:54 -0700466 dev_hold(dev);
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +0900467 return dev;
468
469failure:
470 /* allow the register to be completed before unregistering. */
471 rtnl_unlock();
472 rtnl_lock();
473
474 unregister_netdevice(dev);
475 return NULL;
476}
477#endif
478
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900479/*
480 * Delete a VIF entry
481 */
482
Benjamin Thery8229efd2008-12-10 16:30:15 -0800483static int mif6_delete(struct net *net, int vifi)
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900484{
485 struct mif_device *v;
486 struct net_device *dev;
Thomas Goff1d6e55f2009-01-27 22:39:59 -0800487 struct inet6_dev *in6_dev;
Benjamin Thery8229efd2008-12-10 16:30:15 -0800488 if (vifi < 0 || vifi >= net->ipv6.maxvif)
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900489 return -EADDRNOTAVAIL;
490
Benjamin Thery8229efd2008-12-10 16:30:15 -0800491 v = &net->ipv6.vif6_table[vifi];
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900492
493 write_lock_bh(&mrt_lock);
494 dev = v->dev;
495 v->dev = NULL;
496
497 if (!dev) {
498 write_unlock_bh(&mrt_lock);
499 return -EADDRNOTAVAIL;
500 }
501
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +0900502#ifdef CONFIG_IPV6_PIMSM_V2
Benjamin Thery8229efd2008-12-10 16:30:15 -0800503 if (vifi == net->ipv6.mroute_reg_vif_num)
504 net->ipv6.mroute_reg_vif_num = -1;
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +0900505#endif
506
Benjamin Thery8229efd2008-12-10 16:30:15 -0800507 if (vifi + 1 == net->ipv6.maxvif) {
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900508 int tmp;
509 for (tmp = vifi - 1; tmp >= 0; tmp--) {
Benjamin Thery8229efd2008-12-10 16:30:15 -0800510 if (MIF_EXISTS(net, tmp))
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900511 break;
512 }
Benjamin Thery8229efd2008-12-10 16:30:15 -0800513 net->ipv6.maxvif = tmp + 1;
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900514 }
515
516 write_unlock_bh(&mrt_lock);
517
518 dev_set_allmulti(dev, -1);
519
Thomas Goff1d6e55f2009-01-27 22:39:59 -0800520 in6_dev = __in6_dev_get(dev);
521 if (in6_dev)
522 in6_dev->cnf.mc_forwarding--;
523
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900524 if (v->flags & MIFF_REGISTER)
525 unregister_netdevice(dev);
526
527 dev_put(dev);
528 return 0;
529}
530
Benjamin Thery58701ad2008-12-10 16:22:34 -0800531static inline void ip6mr_cache_free(struct mfc6_cache *c)
532{
533 release_net(mfc6_net(c));
534 kmem_cache_free(mrt_cachep, c);
535}
536
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900537/* Destroy an unresolved cache entry, killing queued skbs
538 and reporting error to netlink readers.
539 */
540
541static void ip6mr_destroy_unres(struct mfc6_cache *c)
542{
543 struct sk_buff *skb;
Benjamin Thery8229efd2008-12-10 16:30:15 -0800544 struct net *net = mfc6_net(c);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900545
Benjamin Thery8229efd2008-12-10 16:30:15 -0800546 atomic_dec(&net->ipv6.cache_resolve_queue_len);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900547
548 while((skb = skb_dequeue(&c->mfc_un.unres.unresolved)) != NULL) {
549 if (ipv6_hdr(skb)->version == 0) {
550 struct nlmsghdr *nlh = (struct nlmsghdr *)skb_pull(skb, sizeof(struct ipv6hdr));
551 nlh->nlmsg_type = NLMSG_ERROR;
552 nlh->nlmsg_len = NLMSG_LENGTH(sizeof(struct nlmsgerr));
553 skb_trim(skb, nlh->nlmsg_len);
554 ((struct nlmsgerr *)NLMSG_DATA(nlh))->error = -ETIMEDOUT;
Benjamin Thery8229efd2008-12-10 16:30:15 -0800555 rtnl_unicast(skb, net, NETLINK_CB(skb).pid);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900556 } else
557 kfree_skb(skb);
558 }
559
Benjamin Thery58701ad2008-12-10 16:22:34 -0800560 ip6mr_cache_free(c);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900561}
562
563
564/* Single timer process for all the unresolved queue. */
565
566static void ipmr_do_expire_process(unsigned long dummy)
567{
568 unsigned long now = jiffies;
569 unsigned long expires = 10 * HZ;
570 struct mfc6_cache *c, **cp;
571
572 cp = &mfc_unres_queue;
573
574 while ((c = *cp) != NULL) {
575 if (time_after(c->mfc_un.unres.expires, now)) {
576 /* not yet... */
577 unsigned long interval = c->mfc_un.unres.expires - now;
578 if (interval < expires)
579 expires = interval;
580 cp = &c->next;
581 continue;
582 }
583
584 *cp = c->next;
585 ip6mr_destroy_unres(c);
586 }
587
Benjamin Thery4045e572008-12-10 16:27:21 -0800588 if (mfc_unres_queue != NULL)
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900589 mod_timer(&ipmr_expire_timer, jiffies + expires);
590}
591
592static void ipmr_expire_process(unsigned long dummy)
593{
594 if (!spin_trylock(&mfc_unres_lock)) {
595 mod_timer(&ipmr_expire_timer, jiffies + 1);
596 return;
597 }
598
Benjamin Thery4045e572008-12-10 16:27:21 -0800599 if (mfc_unres_queue != NULL)
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900600 ipmr_do_expire_process(dummy);
601
602 spin_unlock(&mfc_unres_lock);
603}
604
605/* Fill oifs list. It is called under write locked mrt_lock. */
606
607static void ip6mr_update_thresholds(struct mfc6_cache *cache, unsigned char *ttls)
608{
609 int vifi;
Benjamin Thery8229efd2008-12-10 16:30:15 -0800610 struct net *net = mfc6_net(cache);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900611
Rami Rosen6ac7eb02008-04-10 12:40:10 +0300612 cache->mfc_un.res.minvif = MAXMIFS;
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900613 cache->mfc_un.res.maxvif = 0;
Rami Rosen6ac7eb02008-04-10 12:40:10 +0300614 memset(cache->mfc_un.res.ttls, 255, MAXMIFS);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900615
Benjamin Thery8229efd2008-12-10 16:30:15 -0800616 for (vifi = 0; vifi < net->ipv6.maxvif; vifi++) {
617 if (MIF_EXISTS(net, vifi) &&
Benjamin Thery4e168802008-12-10 16:15:08 -0800618 ttls[vifi] && ttls[vifi] < 255) {
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900619 cache->mfc_un.res.ttls[vifi] = ttls[vifi];
620 if (cache->mfc_un.res.minvif > vifi)
621 cache->mfc_un.res.minvif = vifi;
622 if (cache->mfc_un.res.maxvif <= vifi)
623 cache->mfc_un.res.maxvif = vifi + 1;
624 }
625 }
626}
627
Benjamin Thery8229efd2008-12-10 16:30:15 -0800628static int mif6_add(struct net *net, struct mif6ctl *vifc, int mrtsock)
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900629{
630 int vifi = vifc->mif6c_mifi;
Benjamin Thery8229efd2008-12-10 16:30:15 -0800631 struct mif_device *v = &net->ipv6.vif6_table[vifi];
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900632 struct net_device *dev;
Thomas Goff1d6e55f2009-01-27 22:39:59 -0800633 struct inet6_dev *in6_dev;
Wang Chen5ae7b442008-07-14 20:54:23 -0700634 int err;
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900635
636 /* Is vif busy ? */
Benjamin Thery8229efd2008-12-10 16:30:15 -0800637 if (MIF_EXISTS(net, vifi))
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900638 return -EADDRINUSE;
639
640 switch (vifc->mif6c_flags) {
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +0900641#ifdef CONFIG_IPV6_PIMSM_V2
642 case MIFF_REGISTER:
643 /*
644 * Special Purpose VIF in PIM
645 * All the packets will be sent to the daemon
646 */
Benjamin Thery8229efd2008-12-10 16:30:15 -0800647 if (net->ipv6.mroute_reg_vif_num >= 0)
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +0900648 return -EADDRINUSE;
Benjamin Thery8229efd2008-12-10 16:30:15 -0800649 dev = ip6mr_reg_vif(net);
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +0900650 if (!dev)
651 return -ENOBUFS;
Wang Chen5ae7b442008-07-14 20:54:23 -0700652 err = dev_set_allmulti(dev, 1);
653 if (err) {
654 unregister_netdevice(dev);
Wang Chen7af3db72008-07-14 20:54:54 -0700655 dev_put(dev);
Wang Chen5ae7b442008-07-14 20:54:23 -0700656 return err;
657 }
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +0900658 break;
659#endif
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900660 case 0:
Benjamin Thery8229efd2008-12-10 16:30:15 -0800661 dev = dev_get_by_index(net, vifc->mif6c_pifi);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900662 if (!dev)
663 return -EADDRNOTAVAIL;
Wang Chen5ae7b442008-07-14 20:54:23 -0700664 err = dev_set_allmulti(dev, 1);
Wang Chen7af3db72008-07-14 20:54:54 -0700665 if (err) {
666 dev_put(dev);
Wang Chen5ae7b442008-07-14 20:54:23 -0700667 return err;
Wang Chen7af3db72008-07-14 20:54:54 -0700668 }
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900669 break;
670 default:
671 return -EINVAL;
672 }
673
Thomas Goff1d6e55f2009-01-27 22:39:59 -0800674 in6_dev = __in6_dev_get(dev);
675 if (in6_dev)
676 in6_dev->cnf.mc_forwarding++;
677
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900678 /*
679 * Fill in the VIF structures
680 */
681 v->rate_limit = vifc->vifc_rate_limit;
682 v->flags = vifc->mif6c_flags;
683 if (!mrtsock)
684 v->flags |= VIFF_STATIC;
685 v->threshold = vifc->vifc_threshold;
686 v->bytes_in = 0;
687 v->bytes_out = 0;
688 v->pkt_in = 0;
689 v->pkt_out = 0;
690 v->link = dev->ifindex;
691 if (v->flags & MIFF_REGISTER)
692 v->link = dev->iflink;
693
694 /* And finish update writing critical data */
695 write_lock_bh(&mrt_lock);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900696 v->dev = dev;
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +0900697#ifdef CONFIG_IPV6_PIMSM_V2
698 if (v->flags & MIFF_REGISTER)
Benjamin Thery8229efd2008-12-10 16:30:15 -0800699 net->ipv6.mroute_reg_vif_num = vifi;
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +0900700#endif
Benjamin Thery8229efd2008-12-10 16:30:15 -0800701 if (vifi + 1 > net->ipv6.maxvif)
702 net->ipv6.maxvif = vifi + 1;
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900703 write_unlock_bh(&mrt_lock);
704 return 0;
705}
706
Benjamin Thery8229efd2008-12-10 16:30:15 -0800707static struct mfc6_cache *ip6mr_cache_find(struct net *net,
708 struct in6_addr *origin,
709 struct in6_addr *mcastgrp)
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900710{
711 int line = MFC6_HASH(mcastgrp, origin);
712 struct mfc6_cache *c;
713
Benjamin Thery8229efd2008-12-10 16:30:15 -0800714 for (c = net->ipv6.mfc6_cache_array[line]; c; c = c->next) {
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900715 if (ipv6_addr_equal(&c->mf6c_origin, origin) &&
716 ipv6_addr_equal(&c->mf6c_mcastgrp, mcastgrp))
717 break;
718 }
719 return c;
720}
721
722/*
723 * Allocate a multicast cache entry
724 */
Benjamin Thery58701ad2008-12-10 16:22:34 -0800725static struct mfc6_cache *ip6mr_cache_alloc(struct net *net)
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900726{
Joe Perches36cbac52008-12-03 22:27:25 -0800727 struct mfc6_cache *c = kmem_cache_zalloc(mrt_cachep, GFP_KERNEL);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900728 if (c == NULL)
729 return NULL;
Rami Rosen6ac7eb02008-04-10 12:40:10 +0300730 c->mfc_un.res.minvif = MAXMIFS;
Benjamin Thery58701ad2008-12-10 16:22:34 -0800731 mfc6_net_set(c, net);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900732 return c;
733}
734
Benjamin Thery58701ad2008-12-10 16:22:34 -0800735static struct mfc6_cache *ip6mr_cache_alloc_unres(struct net *net)
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900736{
Joe Perches36cbac52008-12-03 22:27:25 -0800737 struct mfc6_cache *c = kmem_cache_zalloc(mrt_cachep, GFP_ATOMIC);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900738 if (c == NULL)
739 return NULL;
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900740 skb_queue_head_init(&c->mfc_un.unres.unresolved);
741 c->mfc_un.unres.expires = jiffies + 10 * HZ;
Benjamin Thery58701ad2008-12-10 16:22:34 -0800742 mfc6_net_set(c, net);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900743 return c;
744}
745
746/*
747 * A cache entry has gone into a resolved state from queued
748 */
749
750static void ip6mr_cache_resolve(struct mfc6_cache *uc, struct mfc6_cache *c)
751{
752 struct sk_buff *skb;
753
754 /*
755 * Play the pending entries through our router
756 */
757
758 while((skb = __skb_dequeue(&uc->mfc_un.unres.unresolved))) {
759 if (ipv6_hdr(skb)->version == 0) {
760 int err;
761 struct nlmsghdr *nlh = (struct nlmsghdr *)skb_pull(skb, sizeof(struct ipv6hdr));
762
763 if (ip6mr_fill_mroute(skb, c, NLMSG_DATA(nlh)) > 0) {
YOSHIFUJI Hideaki549e0282008-04-05 22:17:39 +0900764 nlh->nlmsg_len = skb_tail_pointer(skb) - (u8 *)nlh;
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900765 } else {
766 nlh->nlmsg_type = NLMSG_ERROR;
767 nlh->nlmsg_len = NLMSG_LENGTH(sizeof(struct nlmsgerr));
768 skb_trim(skb, nlh->nlmsg_len);
769 ((struct nlmsgerr *)NLMSG_DATA(nlh))->error = -EMSGSIZE;
770 }
Benjamin Thery8229efd2008-12-10 16:30:15 -0800771 err = rtnl_unicast(skb, mfc6_net(uc), NETLINK_CB(skb).pid);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900772 } else
773 ip6_mr_forward(skb, c);
774 }
775}
776
777/*
778 * Bounce a cache query up to pim6sd. We could use netlink for this but pim6sd
779 * expects the following bizarre scheme.
780 *
781 * Called under mrt_lock.
782 */
783
Benjamin Thery8229efd2008-12-10 16:30:15 -0800784static int ip6mr_cache_report(struct net *net, struct sk_buff *pkt, mifi_t mifi,
785 int assert)
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900786{
787 struct sk_buff *skb;
788 struct mrt6msg *msg;
789 int ret;
790
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +0900791#ifdef CONFIG_IPV6_PIMSM_V2
792 if (assert == MRT6MSG_WHOLEPKT)
793 skb = skb_realloc_headroom(pkt, -skb_network_offset(pkt)
794 +sizeof(*msg));
795 else
796#endif
797 skb = alloc_skb(sizeof(struct ipv6hdr) + sizeof(*msg), GFP_ATOMIC);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900798
799 if (!skb)
800 return -ENOBUFS;
801
802 /* I suppose that internal messages
803 * do not require checksums */
804
805 skb->ip_summed = CHECKSUM_UNNECESSARY;
806
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +0900807#ifdef CONFIG_IPV6_PIMSM_V2
808 if (assert == MRT6MSG_WHOLEPKT) {
809 /* Ugly, but we have no choice with this interface.
810 Duplicate old header, fix length etc.
811 And all this only to mangle msg->im6_msgtype and
812 to set msg->im6_mbz to "mbz" :-)
813 */
814 skb_push(skb, -skb_network_offset(pkt));
815
816 skb_push(skb, sizeof(*msg));
817 skb_reset_transport_header(skb);
818 msg = (struct mrt6msg *)skb_transport_header(skb);
819 msg->im6_mbz = 0;
820 msg->im6_msgtype = MRT6MSG_WHOLEPKT;
Benjamin Thery8229efd2008-12-10 16:30:15 -0800821 msg->im6_mif = net->ipv6.mroute_reg_vif_num;
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +0900822 msg->im6_pad = 0;
823 ipv6_addr_copy(&msg->im6_src, &ipv6_hdr(pkt)->saddr);
824 ipv6_addr_copy(&msg->im6_dst, &ipv6_hdr(pkt)->daddr);
825
826 skb->ip_summed = CHECKSUM_UNNECESSARY;
827 } else
828#endif
829 {
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900830 /*
831 * Copy the IP header
832 */
833
834 skb_put(skb, sizeof(struct ipv6hdr));
835 skb_reset_network_header(skb);
836 skb_copy_to_linear_data(skb, ipv6_hdr(pkt), sizeof(struct ipv6hdr));
837
838 /*
839 * Add our header
840 */
841 skb_put(skb, sizeof(*msg));
842 skb_reset_transport_header(skb);
843 msg = (struct mrt6msg *)skb_transport_header(skb);
844
845 msg->im6_mbz = 0;
846 msg->im6_msgtype = assert;
Rami Rosen6ac7eb02008-04-10 12:40:10 +0300847 msg->im6_mif = mifi;
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900848 msg->im6_pad = 0;
849 ipv6_addr_copy(&msg->im6_src, &ipv6_hdr(pkt)->saddr);
850 ipv6_addr_copy(&msg->im6_dst, &ipv6_hdr(pkt)->daddr);
851
852 skb->dst = dst_clone(pkt->dst);
853 skb->ip_summed = CHECKSUM_UNNECESSARY;
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +0900854 }
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900855
Benjamin Thery8229efd2008-12-10 16:30:15 -0800856 if (net->ipv6.mroute6_sk == NULL) {
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900857 kfree_skb(skb);
858 return -EINVAL;
859 }
860
861 /*
862 * Deliver to user space multicast routing algorithms
863 */
Benjamin Thery8229efd2008-12-10 16:30:15 -0800864 ret = sock_queue_rcv_skb(net->ipv6.mroute6_sk, skb);
Benjamin Therybd91b8b2008-12-10 16:07:08 -0800865 if (ret < 0) {
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900866 if (net_ratelimit())
867 printk(KERN_WARNING "mroute6: pending queue full, dropping entries.\n");
868 kfree_skb(skb);
869 }
870
871 return ret;
872}
873
874/*
875 * Queue a packet for resolution. It gets locked cache entry!
876 */
877
878static int
Benjamin Thery8229efd2008-12-10 16:30:15 -0800879ip6mr_cache_unresolved(struct net *net, mifi_t mifi, struct sk_buff *skb)
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900880{
881 int err;
882 struct mfc6_cache *c;
883
884 spin_lock_bh(&mfc_unres_lock);
885 for (c = mfc_unres_queue; c; c = c->next) {
Benjamin Thery8229efd2008-12-10 16:30:15 -0800886 if (net_eq(mfc6_net(c), net) &&
Benjamin Thery4045e572008-12-10 16:27:21 -0800887 ipv6_addr_equal(&c->mf6c_mcastgrp, &ipv6_hdr(skb)->daddr) &&
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900888 ipv6_addr_equal(&c->mf6c_origin, &ipv6_hdr(skb)->saddr))
889 break;
890 }
891
892 if (c == NULL) {
893 /*
894 * Create a new entry if allowable
895 */
896
Benjamin Thery8229efd2008-12-10 16:30:15 -0800897 if (atomic_read(&net->ipv6.cache_resolve_queue_len) >= 10 ||
898 (c = ip6mr_cache_alloc_unres(net)) == NULL) {
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900899 spin_unlock_bh(&mfc_unres_lock);
900
901 kfree_skb(skb);
902 return -ENOBUFS;
903 }
904
905 /*
906 * Fill in the new cache entry
907 */
908 c->mf6c_parent = -1;
909 c->mf6c_origin = ipv6_hdr(skb)->saddr;
910 c->mf6c_mcastgrp = ipv6_hdr(skb)->daddr;
911
912 /*
913 * Reflect first query at pim6sd
914 */
Benjamin Thery8229efd2008-12-10 16:30:15 -0800915 err = ip6mr_cache_report(net, skb, mifi, MRT6MSG_NOCACHE);
916 if (err < 0) {
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900917 /* If the report failed throw the cache entry
918 out - Brad Parker
919 */
920 spin_unlock_bh(&mfc_unres_lock);
921
Benjamin Thery58701ad2008-12-10 16:22:34 -0800922 ip6mr_cache_free(c);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900923 kfree_skb(skb);
924 return err;
925 }
926
Benjamin Thery8229efd2008-12-10 16:30:15 -0800927 atomic_inc(&net->ipv6.cache_resolve_queue_len);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900928 c->next = mfc_unres_queue;
929 mfc_unres_queue = c;
930
931 ipmr_do_expire_process(1);
932 }
933
934 /*
935 * See if we can append the packet
936 */
937 if (c->mfc_un.unres.unresolved.qlen > 3) {
938 kfree_skb(skb);
939 err = -ENOBUFS;
940 } else {
941 skb_queue_tail(&c->mfc_un.unres.unresolved, skb);
942 err = 0;
943 }
944
945 spin_unlock_bh(&mfc_unres_lock);
946 return err;
947}
948
949/*
950 * MFC6 cache manipulation by user space
951 */
952
Benjamin Thery8229efd2008-12-10 16:30:15 -0800953static int ip6mr_mfc_delete(struct net *net, struct mf6cctl *mfc)
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900954{
955 int line;
956 struct mfc6_cache *c, **cp;
957
958 line = MFC6_HASH(&mfc->mf6cc_mcastgrp.sin6_addr, &mfc->mf6cc_origin.sin6_addr);
959
Benjamin Thery8229efd2008-12-10 16:30:15 -0800960 for (cp = &net->ipv6.mfc6_cache_array[line];
Benjamin Thery4a6258a2008-12-10 16:24:07 -0800961 (c = *cp) != NULL; cp = &c->next) {
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900962 if (ipv6_addr_equal(&c->mf6c_origin, &mfc->mf6cc_origin.sin6_addr) &&
963 ipv6_addr_equal(&c->mf6c_mcastgrp, &mfc->mf6cc_mcastgrp.sin6_addr)) {
964 write_lock_bh(&mrt_lock);
965 *cp = c->next;
966 write_unlock_bh(&mrt_lock);
967
Benjamin Thery58701ad2008-12-10 16:22:34 -0800968 ip6mr_cache_free(c);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900969 return 0;
970 }
971 }
972 return -ENOENT;
973}
974
975static int ip6mr_device_event(struct notifier_block *this,
976 unsigned long event, void *ptr)
977{
978 struct net_device *dev = ptr;
Benjamin Thery8229efd2008-12-10 16:30:15 -0800979 struct net *net = dev_net(dev);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900980 struct mif_device *v;
981 int ct;
982
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900983 if (event != NETDEV_UNREGISTER)
984 return NOTIFY_DONE;
985
Benjamin Thery8229efd2008-12-10 16:30:15 -0800986 v = &net->ipv6.vif6_table[0];
987 for (ct = 0; ct < net->ipv6.maxvif; ct++, v++) {
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900988 if (v->dev == dev)
Benjamin Thery8229efd2008-12-10 16:30:15 -0800989 mif6_delete(net, ct);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900990 }
991 return NOTIFY_DONE;
992}
993
994static struct notifier_block ip6_mr_notifier = {
995 .notifier_call = ip6mr_device_event
996};
997
998/*
999 * Setup for IP multicast routing
1000 */
1001
Benjamin Thery4e168802008-12-10 16:15:08 -08001002static int __net_init ip6mr_net_init(struct net *net)
1003{
1004 int err = 0;
Benjamin Thery4e168802008-12-10 16:15:08 -08001005 net->ipv6.vif6_table = kcalloc(MAXMIFS, sizeof(struct mif_device),
1006 GFP_KERNEL);
1007 if (!net->ipv6.vif6_table) {
1008 err = -ENOMEM;
1009 goto fail;
1010 }
Benjamin Thery4a6258a2008-12-10 16:24:07 -08001011
1012 /* Forwarding cache */
1013 net->ipv6.mfc6_cache_array = kcalloc(MFC6_LINES,
1014 sizeof(struct mfc6_cache *),
1015 GFP_KERNEL);
1016 if (!net->ipv6.mfc6_cache_array) {
1017 err = -ENOMEM;
1018 goto fail_mfc6_cache;
1019 }
Benjamin Thery950d5702008-12-10 16:29:24 -08001020
1021#ifdef CONFIG_IPV6_PIMSM_V2
1022 net->ipv6.mroute_reg_vif_num = -1;
1023#endif
Benjamin Thery8b90fc72008-12-10 16:29:48 -08001024
1025#ifdef CONFIG_PROC_FS
1026 err = -ENOMEM;
1027 if (!proc_net_fops_create(net, "ip6_mr_vif", 0, &ip6mr_vif_fops))
1028 goto proc_vif_fail;
1029 if (!proc_net_fops_create(net, "ip6_mr_cache", 0, &ip6mr_mfc_fops))
1030 goto proc_cache_fail;
1031#endif
Benjamin Thery4a6258a2008-12-10 16:24:07 -08001032 return 0;
1033
Benjamin Thery8b90fc72008-12-10 16:29:48 -08001034#ifdef CONFIG_PROC_FS
1035proc_cache_fail:
1036 proc_net_remove(net, "ip6_mr_vif");
1037proc_vif_fail:
1038 kfree(net->ipv6.mfc6_cache_array);
1039#endif
Benjamin Thery4a6258a2008-12-10 16:24:07 -08001040fail_mfc6_cache:
1041 kfree(net->ipv6.vif6_table);
Benjamin Thery4e168802008-12-10 16:15:08 -08001042fail:
1043 return err;
1044}
1045
1046static void __net_exit ip6mr_net_exit(struct net *net)
1047{
Benjamin Thery8b90fc72008-12-10 16:29:48 -08001048#ifdef CONFIG_PROC_FS
1049 proc_net_remove(net, "ip6_mr_cache");
1050 proc_net_remove(net, "ip6_mr_vif");
1051#endif
Benjamin Thery8229efd2008-12-10 16:30:15 -08001052 mroute_clean_tables(net);
Benjamin Thery4a6258a2008-12-10 16:24:07 -08001053 kfree(net->ipv6.mfc6_cache_array);
Benjamin Thery4e168802008-12-10 16:15:08 -08001054 kfree(net->ipv6.vif6_table);
1055}
1056
1057static struct pernet_operations ip6mr_net_ops = {
1058 .init = ip6mr_net_init,
1059 .exit = ip6mr_net_exit,
1060};
1061
Wang Chen623d1a12008-07-03 12:13:30 +08001062int __init ip6_mr_init(void)
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001063{
Wang Chen623d1a12008-07-03 12:13:30 +08001064 int err;
1065
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001066 mrt_cachep = kmem_cache_create("ip6_mrt_cache",
1067 sizeof(struct mfc6_cache),
1068 0, SLAB_HWCACHE_ALIGN,
1069 NULL);
1070 if (!mrt_cachep)
Wang Chen623d1a12008-07-03 12:13:30 +08001071 return -ENOMEM;
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001072
Benjamin Thery4e168802008-12-10 16:15:08 -08001073 err = register_pernet_subsys(&ip6mr_net_ops);
1074 if (err)
1075 goto reg_pernet_fail;
1076
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001077 setup_timer(&ipmr_expire_timer, ipmr_expire_process, 0);
Wang Chen623d1a12008-07-03 12:13:30 +08001078 err = register_netdevice_notifier(&ip6_mr_notifier);
1079 if (err)
1080 goto reg_notif_fail;
Wang Chen623d1a12008-07-03 12:13:30 +08001081 return 0;
Benjamin Thery87b30a62008-11-10 16:34:11 -08001082reg_notif_fail:
1083 del_timer(&ipmr_expire_timer);
Benjamin Thery4e168802008-12-10 16:15:08 -08001084 unregister_pernet_subsys(&ip6mr_net_ops);
1085reg_pernet_fail:
Benjamin Thery87b30a62008-11-10 16:34:11 -08001086 kmem_cache_destroy(mrt_cachep);
Wang Chen623d1a12008-07-03 12:13:30 +08001087 return err;
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001088}
1089
Wang Chen623d1a12008-07-03 12:13:30 +08001090void ip6_mr_cleanup(void)
1091{
Wang Chen623d1a12008-07-03 12:13:30 +08001092 unregister_netdevice_notifier(&ip6_mr_notifier);
1093 del_timer(&ipmr_expire_timer);
Benjamin Thery4e168802008-12-10 16:15:08 -08001094 unregister_pernet_subsys(&ip6mr_net_ops);
Wang Chen623d1a12008-07-03 12:13:30 +08001095 kmem_cache_destroy(mrt_cachep);
1096}
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001097
Benjamin Thery8229efd2008-12-10 16:30:15 -08001098static int ip6mr_mfc_add(struct net *net, struct mf6cctl *mfc, int mrtsock)
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001099{
1100 int line;
1101 struct mfc6_cache *uc, *c, **cp;
Rami Rosen6ac7eb02008-04-10 12:40:10 +03001102 unsigned char ttls[MAXMIFS];
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001103 int i;
1104
Rami Rosen6ac7eb02008-04-10 12:40:10 +03001105 memset(ttls, 255, MAXMIFS);
1106 for (i = 0; i < MAXMIFS; i++) {
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001107 if (IF_ISSET(i, &mfc->mf6cc_ifset))
1108 ttls[i] = 1;
1109
1110 }
1111
1112 line = MFC6_HASH(&mfc->mf6cc_mcastgrp.sin6_addr, &mfc->mf6cc_origin.sin6_addr);
1113
Benjamin Thery8229efd2008-12-10 16:30:15 -08001114 for (cp = &net->ipv6.mfc6_cache_array[line];
Benjamin Thery4a6258a2008-12-10 16:24:07 -08001115 (c = *cp) != NULL; cp = &c->next) {
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001116 if (ipv6_addr_equal(&c->mf6c_origin, &mfc->mf6cc_origin.sin6_addr) &&
1117 ipv6_addr_equal(&c->mf6c_mcastgrp, &mfc->mf6cc_mcastgrp.sin6_addr))
1118 break;
1119 }
1120
1121 if (c != NULL) {
1122 write_lock_bh(&mrt_lock);
1123 c->mf6c_parent = mfc->mf6cc_parent;
1124 ip6mr_update_thresholds(c, ttls);
1125 if (!mrtsock)
1126 c->mfc_flags |= MFC_STATIC;
1127 write_unlock_bh(&mrt_lock);
1128 return 0;
1129 }
1130
1131 if (!ipv6_addr_is_multicast(&mfc->mf6cc_mcastgrp.sin6_addr))
1132 return -EINVAL;
1133
Benjamin Thery8229efd2008-12-10 16:30:15 -08001134 c = ip6mr_cache_alloc(net);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001135 if (c == NULL)
1136 return -ENOMEM;
1137
1138 c->mf6c_origin = mfc->mf6cc_origin.sin6_addr;
1139 c->mf6c_mcastgrp = mfc->mf6cc_mcastgrp.sin6_addr;
1140 c->mf6c_parent = mfc->mf6cc_parent;
1141 ip6mr_update_thresholds(c, ttls);
1142 if (!mrtsock)
1143 c->mfc_flags |= MFC_STATIC;
1144
1145 write_lock_bh(&mrt_lock);
Benjamin Thery8229efd2008-12-10 16:30:15 -08001146 c->next = net->ipv6.mfc6_cache_array[line];
1147 net->ipv6.mfc6_cache_array[line] = c;
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001148 write_unlock_bh(&mrt_lock);
1149
1150 /*
1151 * Check to see if we resolved a queued list. If so we
1152 * need to send on the frames and tidy up.
1153 */
1154 spin_lock_bh(&mfc_unres_lock);
1155 for (cp = &mfc_unres_queue; (uc = *cp) != NULL;
1156 cp = &uc->next) {
Benjamin Thery8229efd2008-12-10 16:30:15 -08001157 if (net_eq(mfc6_net(uc), net) &&
Benjamin Thery4045e572008-12-10 16:27:21 -08001158 ipv6_addr_equal(&uc->mf6c_origin, &c->mf6c_origin) &&
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001159 ipv6_addr_equal(&uc->mf6c_mcastgrp, &c->mf6c_mcastgrp)) {
1160 *cp = uc->next;
Benjamin Thery8229efd2008-12-10 16:30:15 -08001161 atomic_dec(&net->ipv6.cache_resolve_queue_len);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001162 break;
1163 }
1164 }
Benjamin Thery4045e572008-12-10 16:27:21 -08001165 if (mfc_unres_queue == NULL)
1166 del_timer(&ipmr_expire_timer);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001167 spin_unlock_bh(&mfc_unres_lock);
1168
1169 if (uc) {
1170 ip6mr_cache_resolve(uc, c);
Benjamin Thery58701ad2008-12-10 16:22:34 -08001171 ip6mr_cache_free(uc);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001172 }
1173 return 0;
1174}
1175
1176/*
1177 * Close the multicast socket, and clear the vif tables etc
1178 */
1179
Benjamin Thery8229efd2008-12-10 16:30:15 -08001180static void mroute_clean_tables(struct net *net)
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001181{
1182 int i;
1183
1184 /*
1185 * Shut down all active vif entries
1186 */
Benjamin Thery8229efd2008-12-10 16:30:15 -08001187 for (i = 0; i < net->ipv6.maxvif; i++) {
1188 if (!(net->ipv6.vif6_table[i].flags & VIFF_STATIC))
1189 mif6_delete(net, i);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001190 }
1191
1192 /*
1193 * Wipe the cache
1194 */
Benjamin Thery4a6258a2008-12-10 16:24:07 -08001195 for (i = 0; i < MFC6_LINES; i++) {
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001196 struct mfc6_cache *c, **cp;
1197
Benjamin Thery8229efd2008-12-10 16:30:15 -08001198 cp = &net->ipv6.mfc6_cache_array[i];
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001199 while ((c = *cp) != NULL) {
1200 if (c->mfc_flags & MFC_STATIC) {
1201 cp = &c->next;
1202 continue;
1203 }
1204 write_lock_bh(&mrt_lock);
1205 *cp = c->next;
1206 write_unlock_bh(&mrt_lock);
1207
Benjamin Thery58701ad2008-12-10 16:22:34 -08001208 ip6mr_cache_free(c);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001209 }
1210 }
1211
Benjamin Thery8229efd2008-12-10 16:30:15 -08001212 if (atomic_read(&net->ipv6.cache_resolve_queue_len) != 0) {
Benjamin Thery4045e572008-12-10 16:27:21 -08001213 struct mfc6_cache *c, **cp;
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001214
1215 spin_lock_bh(&mfc_unres_lock);
Benjamin Thery4045e572008-12-10 16:27:21 -08001216 cp = &mfc_unres_queue;
1217 while ((c = *cp) != NULL) {
Benjamin Thery8229efd2008-12-10 16:30:15 -08001218 if (!net_eq(mfc6_net(c), net)) {
Benjamin Thery4045e572008-12-10 16:27:21 -08001219 cp = &c->next;
1220 continue;
1221 }
1222 *cp = c->next;
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001223 ip6mr_destroy_unres(c);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001224 }
1225 spin_unlock_bh(&mfc_unres_lock);
1226 }
1227}
1228
1229static int ip6mr_sk_init(struct sock *sk)
1230{
1231 int err = 0;
Benjamin Thery8229efd2008-12-10 16:30:15 -08001232 struct net *net = sock_net(sk);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001233
1234 rtnl_lock();
1235 write_lock_bh(&mrt_lock);
Thomas Goff1d6e55f2009-01-27 22:39:59 -08001236 if (likely(net->ipv6.mroute6_sk == NULL)) {
Benjamin Thery8229efd2008-12-10 16:30:15 -08001237 net->ipv6.mroute6_sk = sk;
Thomas Goff1d6e55f2009-01-27 22:39:59 -08001238 net->ipv6.devconf_all->mc_forwarding++;
1239 }
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001240 else
1241 err = -EADDRINUSE;
1242 write_unlock_bh(&mrt_lock);
1243
1244 rtnl_unlock();
1245
1246 return err;
1247}
1248
1249int ip6mr_sk_done(struct sock *sk)
1250{
1251 int err = 0;
Benjamin Thery8229efd2008-12-10 16:30:15 -08001252 struct net *net = sock_net(sk);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001253
1254 rtnl_lock();
Benjamin Thery8229efd2008-12-10 16:30:15 -08001255 if (sk == net->ipv6.mroute6_sk) {
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001256 write_lock_bh(&mrt_lock);
Benjamin Thery8229efd2008-12-10 16:30:15 -08001257 net->ipv6.mroute6_sk = NULL;
Thomas Goff1d6e55f2009-01-27 22:39:59 -08001258 net->ipv6.devconf_all->mc_forwarding--;
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001259 write_unlock_bh(&mrt_lock);
1260
Benjamin Thery8229efd2008-12-10 16:30:15 -08001261 mroute_clean_tables(net);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001262 } else
1263 err = -EACCES;
1264 rtnl_unlock();
1265
1266 return err;
1267}
1268
1269/*
1270 * Socket options and virtual interface manipulation. The whole
1271 * virtual interface system is a complete heap, but unfortunately
1272 * that's how BSD mrouted happens to think. Maybe one day with a proper
1273 * MOSPF/PIM router set up we can clean this up.
1274 */
1275
1276int ip6_mroute_setsockopt(struct sock *sk, int optname, char __user *optval, int optlen)
1277{
1278 int ret;
1279 struct mif6ctl vif;
1280 struct mf6cctl mfc;
1281 mifi_t mifi;
Benjamin Thery8229efd2008-12-10 16:30:15 -08001282 struct net *net = sock_net(sk);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001283
1284 if (optname != MRT6_INIT) {
Benjamin Thery8229efd2008-12-10 16:30:15 -08001285 if (sk != net->ipv6.mroute6_sk && !capable(CAP_NET_ADMIN))
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001286 return -EACCES;
1287 }
1288
1289 switch (optname) {
1290 case MRT6_INIT:
1291 if (sk->sk_type != SOCK_RAW ||
1292 inet_sk(sk)->num != IPPROTO_ICMPV6)
1293 return -EOPNOTSUPP;
1294 if (optlen < sizeof(int))
1295 return -EINVAL;
1296
1297 return ip6mr_sk_init(sk);
1298
1299 case MRT6_DONE:
1300 return ip6mr_sk_done(sk);
1301
1302 case MRT6_ADD_MIF:
1303 if (optlen < sizeof(vif))
1304 return -EINVAL;
1305 if (copy_from_user(&vif, optval, sizeof(vif)))
1306 return -EFAULT;
Rami Rosen6ac7eb02008-04-10 12:40:10 +03001307 if (vif.mif6c_mifi >= MAXMIFS)
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001308 return -ENFILE;
1309 rtnl_lock();
Benjamin Thery8229efd2008-12-10 16:30:15 -08001310 ret = mif6_add(net, &vif, sk == net->ipv6.mroute6_sk);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001311 rtnl_unlock();
1312 return ret;
1313
1314 case MRT6_DEL_MIF:
1315 if (optlen < sizeof(mifi_t))
1316 return -EINVAL;
1317 if (copy_from_user(&mifi, optval, sizeof(mifi_t)))
1318 return -EFAULT;
1319 rtnl_lock();
Benjamin Thery8229efd2008-12-10 16:30:15 -08001320 ret = mif6_delete(net, mifi);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001321 rtnl_unlock();
1322 return ret;
1323
1324 /*
1325 * Manipulate the forwarding caches. These live
1326 * in a sort of kernel/user symbiosis.
1327 */
1328 case MRT6_ADD_MFC:
1329 case MRT6_DEL_MFC:
1330 if (optlen < sizeof(mfc))
1331 return -EINVAL;
1332 if (copy_from_user(&mfc, optval, sizeof(mfc)))
1333 return -EFAULT;
1334 rtnl_lock();
1335 if (optname == MRT6_DEL_MFC)
Benjamin Thery8229efd2008-12-10 16:30:15 -08001336 ret = ip6mr_mfc_delete(net, &mfc);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001337 else
Benjamin Thery8229efd2008-12-10 16:30:15 -08001338 ret = ip6mr_mfc_add(net, &mfc,
1339 sk == net->ipv6.mroute6_sk);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001340 rtnl_unlock();
1341 return ret;
1342
1343 /*
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +09001344 * Control PIM assert (to activate pim will activate assert)
1345 */
1346 case MRT6_ASSERT:
1347 {
1348 int v;
1349 if (get_user(v, (int __user *)optval))
1350 return -EFAULT;
Benjamin Thery8229efd2008-12-10 16:30:15 -08001351 net->ipv6.mroute_do_assert = !!v;
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +09001352 return 0;
1353 }
1354
1355#ifdef CONFIG_IPV6_PIMSM_V2
1356 case MRT6_PIM:
1357 {
YOSHIFUJI Hideakia9f83bf2008-04-10 15:41:28 +09001358 int v;
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +09001359 if (get_user(v, (int __user *)optval))
1360 return -EFAULT;
1361 v = !!v;
1362 rtnl_lock();
1363 ret = 0;
Benjamin Thery8229efd2008-12-10 16:30:15 -08001364 if (v != net->ipv6.mroute_do_pim) {
1365 net->ipv6.mroute_do_pim = v;
1366 net->ipv6.mroute_do_assert = v;
1367 if (net->ipv6.mroute_do_pim)
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +09001368 ret = inet6_add_protocol(&pim6_protocol,
1369 IPPROTO_PIM);
1370 else
1371 ret = inet6_del_protocol(&pim6_protocol,
1372 IPPROTO_PIM);
1373 if (ret < 0)
1374 ret = -EAGAIN;
1375 }
1376 rtnl_unlock();
1377 return ret;
1378 }
1379
1380#endif
1381 /*
Rami Rosen7d120c52008-04-23 14:35:13 +03001382 * Spurious command, or MRT6_VERSION which you cannot
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001383 * set.
1384 */
1385 default:
1386 return -ENOPROTOOPT;
1387 }
1388}
1389
1390/*
1391 * Getsock opt support for the multicast routing system.
1392 */
1393
1394int ip6_mroute_getsockopt(struct sock *sk, int optname, char __user *optval,
1395 int __user *optlen)
1396{
1397 int olr;
1398 int val;
Benjamin Thery8229efd2008-12-10 16:30:15 -08001399 struct net *net = sock_net(sk);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001400
1401 switch (optname) {
1402 case MRT6_VERSION:
1403 val = 0x0305;
1404 break;
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +09001405#ifdef CONFIG_IPV6_PIMSM_V2
1406 case MRT6_PIM:
Benjamin Thery8229efd2008-12-10 16:30:15 -08001407 val = net->ipv6.mroute_do_pim;
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +09001408 break;
1409#endif
1410 case MRT6_ASSERT:
Benjamin Thery8229efd2008-12-10 16:30:15 -08001411 val = net->ipv6.mroute_do_assert;
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +09001412 break;
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001413 default:
1414 return -ENOPROTOOPT;
1415 }
1416
1417 if (get_user(olr, optlen))
1418 return -EFAULT;
1419
1420 olr = min_t(int, olr, sizeof(int));
1421 if (olr < 0)
1422 return -EINVAL;
1423
1424 if (put_user(olr, optlen))
1425 return -EFAULT;
1426 if (copy_to_user(optval, &val, olr))
1427 return -EFAULT;
1428 return 0;
1429}
1430
1431/*
1432 * The IP multicast ioctl support routines.
1433 */
1434
1435int ip6mr_ioctl(struct sock *sk, int cmd, void __user *arg)
1436{
1437 struct sioc_sg_req6 sr;
1438 struct sioc_mif_req6 vr;
1439 struct mif_device *vif;
1440 struct mfc6_cache *c;
Benjamin Thery8229efd2008-12-10 16:30:15 -08001441 struct net *net = sock_net(sk);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001442
1443 switch (cmd) {
1444 case SIOCGETMIFCNT_IN6:
1445 if (copy_from_user(&vr, arg, sizeof(vr)))
1446 return -EFAULT;
Benjamin Thery8229efd2008-12-10 16:30:15 -08001447 if (vr.mifi >= net->ipv6.maxvif)
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001448 return -EINVAL;
1449 read_lock(&mrt_lock);
Benjamin Thery8229efd2008-12-10 16:30:15 -08001450 vif = &net->ipv6.vif6_table[vr.mifi];
1451 if (MIF_EXISTS(net, vr.mifi)) {
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001452 vr.icount = vif->pkt_in;
1453 vr.ocount = vif->pkt_out;
1454 vr.ibytes = vif->bytes_in;
1455 vr.obytes = vif->bytes_out;
1456 read_unlock(&mrt_lock);
1457
1458 if (copy_to_user(arg, &vr, sizeof(vr)))
1459 return -EFAULT;
1460 return 0;
1461 }
1462 read_unlock(&mrt_lock);
1463 return -EADDRNOTAVAIL;
1464 case SIOCGETSGCNT_IN6:
1465 if (copy_from_user(&sr, arg, sizeof(sr)))
1466 return -EFAULT;
1467
1468 read_lock(&mrt_lock);
Benjamin Thery8229efd2008-12-10 16:30:15 -08001469 c = ip6mr_cache_find(net, &sr.src.sin6_addr, &sr.grp.sin6_addr);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001470 if (c) {
1471 sr.pktcnt = c->mfc_un.res.pkt;
1472 sr.bytecnt = c->mfc_un.res.bytes;
1473 sr.wrong_if = c->mfc_un.res.wrong_if;
1474 read_unlock(&mrt_lock);
1475
1476 if (copy_to_user(arg, &sr, sizeof(sr)))
1477 return -EFAULT;
1478 return 0;
1479 }
1480 read_unlock(&mrt_lock);
1481 return -EADDRNOTAVAIL;
1482 default:
1483 return -ENOIOCTLCMD;
1484 }
1485}
1486
1487
1488static inline int ip6mr_forward2_finish(struct sk_buff *skb)
1489{
Denis V. Lunev483a47d2008-10-08 11:09:27 -07001490 IP6_INC_STATS_BH(dev_net(skb->dst->dev), ip6_dst_idev(skb->dst),
1491 IPSTATS_MIB_OUTFORWDATAGRAMS);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001492 return dst_output(skb);
1493}
1494
1495/*
1496 * Processing handlers for ip6mr_forward
1497 */
1498
1499static int ip6mr_forward2(struct sk_buff *skb, struct mfc6_cache *c, int vifi)
1500{
1501 struct ipv6hdr *ipv6h;
Benjamin Thery8229efd2008-12-10 16:30:15 -08001502 struct net *net = mfc6_net(c);
1503 struct mif_device *vif = &net->ipv6.vif6_table[vifi];
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001504 struct net_device *dev;
1505 struct dst_entry *dst;
1506 struct flowi fl;
1507
1508 if (vif->dev == NULL)
1509 goto out_free;
1510
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +09001511#ifdef CONFIG_IPV6_PIMSM_V2
1512 if (vif->flags & MIFF_REGISTER) {
1513 vif->pkt_out++;
1514 vif->bytes_out += skb->len;
Pavel Emelyanovdc58c782008-05-21 14:17:54 -07001515 vif->dev->stats.tx_bytes += skb->len;
1516 vif->dev->stats.tx_packets++;
Benjamin Thery8229efd2008-12-10 16:30:15 -08001517 ip6mr_cache_report(net, skb, vifi, MRT6MSG_WHOLEPKT);
Ilpo Järvinen8da73b72008-12-14 23:15:49 -08001518 goto out_free;
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +09001519 }
1520#endif
1521
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001522 ipv6h = ipv6_hdr(skb);
1523
1524 fl = (struct flowi) {
1525 .oif = vif->link,
1526 .nl_u = { .ip6_u =
1527 { .daddr = ipv6h->daddr, }
1528 }
1529 };
1530
Benjamin Thery8229efd2008-12-10 16:30:15 -08001531 dst = ip6_route_output(net, NULL, &fl);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001532 if (!dst)
1533 goto out_free;
1534
1535 dst_release(skb->dst);
1536 skb->dst = dst;
1537
1538 /*
1539 * RFC1584 teaches, that DVMRP/PIM router must deliver packets locally
1540 * not only before forwarding, but after forwarding on all output
1541 * interfaces. It is clear, if mrouter runs a multicasting
1542 * program, it should receive packets not depending to what interface
1543 * program is joined.
1544 * If we will not make it, the program will have to join on all
1545 * interfaces. On the other hand, multihoming host (or router, but
1546 * not mrouter) cannot join to more than one interface - it will
1547 * result in receiving multiple packets.
1548 */
1549 dev = vif->dev;
1550 skb->dev = dev;
1551 vif->pkt_out++;
1552 vif->bytes_out += skb->len;
1553
1554 /* We are about to write */
1555 /* XXX: extension headers? */
1556 if (skb_cow(skb, sizeof(*ipv6h) + LL_RESERVED_SPACE(dev)))
1557 goto out_free;
1558
1559 ipv6h = ipv6_hdr(skb);
1560 ipv6h->hop_limit--;
1561
1562 IP6CB(skb)->flags |= IP6SKB_FORWARDED;
1563
1564 return NF_HOOK(PF_INET6, NF_INET_FORWARD, skb, skb->dev, dev,
1565 ip6mr_forward2_finish);
1566
1567out_free:
1568 kfree_skb(skb);
1569 return 0;
1570}
1571
1572static int ip6mr_find_vif(struct net_device *dev)
1573{
Benjamin Thery8229efd2008-12-10 16:30:15 -08001574 struct net *net = dev_net(dev);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001575 int ct;
Benjamin Thery8229efd2008-12-10 16:30:15 -08001576 for (ct = net->ipv6.maxvif - 1; ct >= 0; ct--) {
1577 if (net->ipv6.vif6_table[ct].dev == dev)
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001578 break;
1579 }
1580 return ct;
1581}
1582
1583static int ip6_mr_forward(struct sk_buff *skb, struct mfc6_cache *cache)
1584{
1585 int psend = -1;
1586 int vif, ct;
Benjamin Thery8229efd2008-12-10 16:30:15 -08001587 struct net *net = mfc6_net(cache);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001588
1589 vif = cache->mf6c_parent;
1590 cache->mfc_un.res.pkt++;
1591 cache->mfc_un.res.bytes += skb->len;
1592
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +09001593 /*
1594 * Wrong interface: drop packet and (maybe) send PIM assert.
1595 */
Benjamin Thery8229efd2008-12-10 16:30:15 -08001596 if (net->ipv6.vif6_table[vif].dev != skb->dev) {
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +09001597 int true_vifi;
1598
1599 cache->mfc_un.res.wrong_if++;
1600 true_vifi = ip6mr_find_vif(skb->dev);
1601
Benjamin Thery8229efd2008-12-10 16:30:15 -08001602 if (true_vifi >= 0 && net->ipv6.mroute_do_assert &&
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +09001603 /* pimsm uses asserts, when switching from RPT to SPT,
1604 so that we cannot check that packet arrived on an oif.
1605 It is bad, but otherwise we would need to move pretty
1606 large chunk of pimd to kernel. Ough... --ANK
1607 */
Benjamin Thery8229efd2008-12-10 16:30:15 -08001608 (net->ipv6.mroute_do_pim ||
Benjamin Therya21f3f92008-12-10 16:28:44 -08001609 cache->mfc_un.res.ttls[true_vifi] < 255) &&
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +09001610 time_after(jiffies,
1611 cache->mfc_un.res.last_assert + MFC_ASSERT_THRESH)) {
1612 cache->mfc_un.res.last_assert = jiffies;
Benjamin Thery8229efd2008-12-10 16:30:15 -08001613 ip6mr_cache_report(net, skb, true_vifi, MRT6MSG_WRONGMIF);
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +09001614 }
1615 goto dont_forward;
1616 }
1617
Benjamin Thery8229efd2008-12-10 16:30:15 -08001618 net->ipv6.vif6_table[vif].pkt_in++;
1619 net->ipv6.vif6_table[vif].bytes_in += skb->len;
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001620
1621 /*
1622 * Forward the frame
1623 */
1624 for (ct = cache->mfc_un.res.maxvif - 1; ct >= cache->mfc_un.res.minvif; ct--) {
1625 if (ipv6_hdr(skb)->hop_limit > cache->mfc_un.res.ttls[ct]) {
1626 if (psend != -1) {
1627 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
1628 if (skb2)
1629 ip6mr_forward2(skb2, cache, psend);
1630 }
1631 psend = ct;
1632 }
1633 }
1634 if (psend != -1) {
1635 ip6mr_forward2(skb, cache, psend);
1636 return 0;
1637 }
1638
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +09001639dont_forward:
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001640 kfree_skb(skb);
1641 return 0;
1642}
1643
1644
1645/*
1646 * Multicast packets for forwarding arrive here
1647 */
1648
1649int ip6_mr_input(struct sk_buff *skb)
1650{
1651 struct mfc6_cache *cache;
Benjamin Thery8229efd2008-12-10 16:30:15 -08001652 struct net *net = dev_net(skb->dev);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001653
1654 read_lock(&mrt_lock);
Benjamin Thery8229efd2008-12-10 16:30:15 -08001655 cache = ip6mr_cache_find(net,
1656 &ipv6_hdr(skb)->saddr, &ipv6_hdr(skb)->daddr);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001657
1658 /*
1659 * No usable cache entry
1660 */
1661 if (cache == NULL) {
1662 int vif;
1663
1664 vif = ip6mr_find_vif(skb->dev);
1665 if (vif >= 0) {
Benjamin Thery8229efd2008-12-10 16:30:15 -08001666 int err = ip6mr_cache_unresolved(net, vif, skb);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001667 read_unlock(&mrt_lock);
1668
1669 return err;
1670 }
1671 read_unlock(&mrt_lock);
1672 kfree_skb(skb);
1673 return -ENODEV;
1674 }
1675
1676 ip6_mr_forward(skb, cache);
1677
1678 read_unlock(&mrt_lock);
1679
1680 return 0;
1681}
1682
1683
1684static int
1685ip6mr_fill_mroute(struct sk_buff *skb, struct mfc6_cache *c, struct rtmsg *rtm)
1686{
1687 int ct;
1688 struct rtnexthop *nhp;
Benjamin Thery8229efd2008-12-10 16:30:15 -08001689 struct net *net = mfc6_net(c);
1690 struct net_device *dev = net->ipv6.vif6_table[c->mf6c_parent].dev;
YOSHIFUJI Hideaki549e0282008-04-05 22:17:39 +09001691 u8 *b = skb_tail_pointer(skb);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001692 struct rtattr *mp_head;
1693
1694 if (dev)
1695 RTA_PUT(skb, RTA_IIF, 4, &dev->ifindex);
1696
1697 mp_head = (struct rtattr *)skb_put(skb, RTA_LENGTH(0));
1698
1699 for (ct = c->mfc_un.res.minvif; ct < c->mfc_un.res.maxvif; ct++) {
1700 if (c->mfc_un.res.ttls[ct] < 255) {
1701 if (skb_tailroom(skb) < RTA_ALIGN(RTA_ALIGN(sizeof(*nhp)) + 4))
1702 goto rtattr_failure;
1703 nhp = (struct rtnexthop *)skb_put(skb, RTA_ALIGN(sizeof(*nhp)));
1704 nhp->rtnh_flags = 0;
1705 nhp->rtnh_hops = c->mfc_un.res.ttls[ct];
Benjamin Thery8229efd2008-12-10 16:30:15 -08001706 nhp->rtnh_ifindex = net->ipv6.vif6_table[ct].dev->ifindex;
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001707 nhp->rtnh_len = sizeof(*nhp);
1708 }
1709 }
1710 mp_head->rta_type = RTA_MULTIPATH;
YOSHIFUJI Hideaki549e0282008-04-05 22:17:39 +09001711 mp_head->rta_len = skb_tail_pointer(skb) - (u8 *)mp_head;
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001712 rtm->rtm_type = RTN_MULTICAST;
1713 return 1;
1714
1715rtattr_failure:
1716 nlmsg_trim(skb, b);
1717 return -EMSGSIZE;
1718}
1719
Benjamin Thery8229efd2008-12-10 16:30:15 -08001720int ip6mr_get_route(struct net *net,
1721 struct sk_buff *skb, struct rtmsg *rtm, int nowait)
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001722{
1723 int err;
1724 struct mfc6_cache *cache;
1725 struct rt6_info *rt = (struct rt6_info *)skb->dst;
1726
1727 read_lock(&mrt_lock);
Benjamin Thery8229efd2008-12-10 16:30:15 -08001728 cache = ip6mr_cache_find(net, &rt->rt6i_src.addr, &rt->rt6i_dst.addr);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001729
1730 if (!cache) {
1731 struct sk_buff *skb2;
1732 struct ipv6hdr *iph;
1733 struct net_device *dev;
1734 int vif;
1735
1736 if (nowait) {
1737 read_unlock(&mrt_lock);
1738 return -EAGAIN;
1739 }
1740
1741 dev = skb->dev;
1742 if (dev == NULL || (vif = ip6mr_find_vif(dev)) < 0) {
1743 read_unlock(&mrt_lock);
1744 return -ENODEV;
1745 }
1746
1747 /* really correct? */
1748 skb2 = alloc_skb(sizeof(struct ipv6hdr), GFP_ATOMIC);
1749 if (!skb2) {
1750 read_unlock(&mrt_lock);
1751 return -ENOMEM;
1752 }
1753
1754 skb_reset_transport_header(skb2);
1755
1756 skb_put(skb2, sizeof(struct ipv6hdr));
1757 skb_reset_network_header(skb2);
1758
1759 iph = ipv6_hdr(skb2);
1760 iph->version = 0;
1761 iph->priority = 0;
1762 iph->flow_lbl[0] = 0;
1763 iph->flow_lbl[1] = 0;
1764 iph->flow_lbl[2] = 0;
1765 iph->payload_len = 0;
1766 iph->nexthdr = IPPROTO_NONE;
1767 iph->hop_limit = 0;
1768 ipv6_addr_copy(&iph->saddr, &rt->rt6i_src.addr);
1769 ipv6_addr_copy(&iph->daddr, &rt->rt6i_dst.addr);
1770
Benjamin Thery8229efd2008-12-10 16:30:15 -08001771 err = ip6mr_cache_unresolved(net, vif, skb2);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001772 read_unlock(&mrt_lock);
1773
1774 return err;
1775 }
1776
1777 if (!nowait && (rtm->rtm_flags&RTM_F_NOTIFY))
1778 cache->mfc_flags |= MFC_NOTIFY;
1779
1780 err = ip6mr_fill_mroute(skb, cache, rtm);
1781 read_unlock(&mrt_lock);
1782 return err;
1783}
1784