blob: b3783a436bbd3e1fa90d5c184f0e20120e8edecc [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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090036#include <linux/slab.h>
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +090037#include <net/protocol.h>
38#include <linux/skbuff.h>
39#include <net/sock.h>
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +090040#include <net/raw.h>
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +090041#include <linux/notifier.h>
42#include <linux/if_arp.h>
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +090043#include <net/checksum.h>
44#include <net/netlink.h>
45
46#include <net/ipv6.h>
47#include <net/ip6_route.h>
48#include <linux/mroute6.h>
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +090049#include <linux/pim.h>
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +090050#include <net/addrconf.h>
51#include <linux/netfilter_ipv6.h>
Dave Jones5d6e4302009-01-31 00:51:49 -080052#include <net/ip6_checksum.h>
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +090053
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +090054/* Big lock, protecting vif table, mrt cache and mroute socket state.
55 Note that the changes are semaphored via rtnl_lock.
56 */
57
58static DEFINE_RWLOCK(mrt_lock);
59
60/*
61 * Multicast router control variables
62 */
63
Benjamin Thery4e168802008-12-10 16:15:08 -080064#define MIF_EXISTS(_net, _idx) ((_net)->ipv6.vif6_table[_idx].dev != NULL)
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +090065
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +090066/* Special spinlock for queue of unresolved entries */
67static DEFINE_SPINLOCK(mfc_unres_lock);
68
69/* We return to original Alan's scheme. Hash table of resolved
70 entries is changed only in process context and protected
71 with weak lock mrt_lock. Queue of unresolved entries is protected
72 with strong spinlock mfc_unres_lock.
73
74 In this case data path is free of exclusive locks at all.
75 */
76
77static struct kmem_cache *mrt_cachep __read_mostly;
78
Patrick McHardyb5aa30b2010-05-11 14:40:50 +020079static int ip6_mr_forward(struct net *net, struct sk_buff *skb,
80 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);
Patrick McHardyb5aa30b2010-05-11 14:40:50 +020083static int ip6mr_fill_mroute(struct net *net, struct sk_buff *skb,
84 struct mfc6_cache *c, struct rtmsg *rtm);
Benjamin Thery8229efd2008-12-10 16:30:15 -080085static void mroute_clean_tables(struct net *net);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +090086
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +090087
88#ifdef CONFIG_PROC_FS
89
90struct ipmr_mfc_iter {
Benjamin Thery8b90fc72008-12-10 16:29:48 -080091 struct seq_net_private p;
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +090092 struct mfc6_cache **cache;
93 int ct;
94};
95
96
Benjamin Thery8b90fc72008-12-10 16:29:48 -080097static struct mfc6_cache *ipmr_mfc_seq_idx(struct net *net,
98 struct ipmr_mfc_iter *it, loff_t pos)
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +090099{
100 struct mfc6_cache *mfc;
101
Benjamin Thery8b90fc72008-12-10 16:29:48 -0800102 it->cache = net->ipv6.mfc6_cache_array;
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900103 read_lock(&mrt_lock);
Benjamin Thery4a6258a2008-12-10 16:24:07 -0800104 for (it->ct = 0; it->ct < MFC6_LINES; it->ct++)
Benjamin Thery8b90fc72008-12-10 16:29:48 -0800105 for (mfc = net->ipv6.mfc6_cache_array[it->ct];
Benjamin Thery4a6258a2008-12-10 16:24:07 -0800106 mfc; mfc = mfc->next)
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900107 if (pos-- == 0)
108 return mfc;
109 read_unlock(&mrt_lock);
110
Patrick McHardyc476efb2010-05-11 14:40:48 +0200111 it->cache = &net->ipv6.mfc6_unres_queue;
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900112 spin_lock_bh(&mfc_unres_lock);
Patrick McHardyc476efb2010-05-11 14:40:48 +0200113 for (mfc = net->ipv6.mfc6_unres_queue; mfc; mfc = mfc->next)
114 if (pos-- == 0)
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900115 return mfc;
116 spin_unlock_bh(&mfc_unres_lock);
117
118 it->cache = NULL;
119 return NULL;
120}
121
122
123
124
125/*
126 * The /proc interfaces to multicast routing /proc/ip6_mr_cache /proc/ip6_mr_vif
127 */
128
129struct ipmr_vif_iter {
Benjamin Thery8b90fc72008-12-10 16:29:48 -0800130 struct seq_net_private p;
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900131 int ct;
132};
133
Benjamin Thery8b90fc72008-12-10 16:29:48 -0800134static struct mif_device *ip6mr_vif_seq_idx(struct net *net,
135 struct ipmr_vif_iter *iter,
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900136 loff_t pos)
137{
Benjamin Thery8b90fc72008-12-10 16:29:48 -0800138 for (iter->ct = 0; iter->ct < net->ipv6.maxvif; ++iter->ct) {
139 if (!MIF_EXISTS(net, iter->ct))
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900140 continue;
141 if (pos-- == 0)
Benjamin Thery8b90fc72008-12-10 16:29:48 -0800142 return &net->ipv6.vif6_table[iter->ct];
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900143 }
144 return NULL;
145}
146
147static void *ip6mr_vif_seq_start(struct seq_file *seq, loff_t *pos)
148 __acquires(mrt_lock)
149{
Benjamin Thery8b90fc72008-12-10 16:29:48 -0800150 struct net *net = seq_file_net(seq);
151
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900152 read_lock(&mrt_lock);
Benjamin Thery8b90fc72008-12-10 16:29:48 -0800153 return *pos ? ip6mr_vif_seq_idx(net, seq->private, *pos - 1)
154 : SEQ_START_TOKEN;
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900155}
156
157static void *ip6mr_vif_seq_next(struct seq_file *seq, void *v, loff_t *pos)
158{
159 struct ipmr_vif_iter *iter = seq->private;
Benjamin Thery8b90fc72008-12-10 16:29:48 -0800160 struct net *net = seq_file_net(seq);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900161
162 ++*pos;
163 if (v == SEQ_START_TOKEN)
Benjamin Thery8b90fc72008-12-10 16:29:48 -0800164 return ip6mr_vif_seq_idx(net, iter, 0);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900165
Benjamin Thery8b90fc72008-12-10 16:29:48 -0800166 while (++iter->ct < net->ipv6.maxvif) {
167 if (!MIF_EXISTS(net, iter->ct))
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900168 continue;
Benjamin Thery8b90fc72008-12-10 16:29:48 -0800169 return &net->ipv6.vif6_table[iter->ct];
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900170 }
171 return NULL;
172}
173
174static void ip6mr_vif_seq_stop(struct seq_file *seq, void *v)
175 __releases(mrt_lock)
176{
177 read_unlock(&mrt_lock);
178}
179
180static int ip6mr_vif_seq_show(struct seq_file *seq, void *v)
181{
Benjamin Thery8b90fc72008-12-10 16:29:48 -0800182 struct net *net = seq_file_net(seq);
183
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900184 if (v == SEQ_START_TOKEN) {
185 seq_puts(seq,
186 "Interface BytesIn PktsIn BytesOut PktsOut Flags\n");
187 } else {
188 const struct mif_device *vif = v;
189 const char *name = vif->dev ? vif->dev->name : "none";
190
191 seq_printf(seq,
Al Virod430a222008-06-02 10:59:02 +0100192 "%2td %-10s %8ld %7ld %8ld %7ld %05X\n",
Benjamin Thery8b90fc72008-12-10 16:29:48 -0800193 vif - net->ipv6.vif6_table,
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900194 name, vif->bytes_in, vif->pkt_in,
195 vif->bytes_out, vif->pkt_out,
196 vif->flags);
197 }
198 return 0;
199}
200
Stephen Hemminger98147d52009-09-01 19:25:02 +0000201static const struct seq_operations ip6mr_vif_seq_ops = {
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900202 .start = ip6mr_vif_seq_start,
203 .next = ip6mr_vif_seq_next,
204 .stop = ip6mr_vif_seq_stop,
205 .show = ip6mr_vif_seq_show,
206};
207
208static int ip6mr_vif_open(struct inode *inode, struct file *file)
209{
Benjamin Thery8b90fc72008-12-10 16:29:48 -0800210 return seq_open_net(inode, file, &ip6mr_vif_seq_ops,
211 sizeof(struct ipmr_vif_iter));
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900212}
213
Stephen Hemminger5ca1b992009-09-01 19:25:05 +0000214static const struct file_operations ip6mr_vif_fops = {
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900215 .owner = THIS_MODULE,
216 .open = ip6mr_vif_open,
217 .read = seq_read,
218 .llseek = seq_lseek,
Benjamin Thery8b90fc72008-12-10 16:29:48 -0800219 .release = seq_release_net,
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900220};
221
222static void *ipmr_mfc_seq_start(struct seq_file *seq, loff_t *pos)
223{
Benjamin Thery8b90fc72008-12-10 16:29:48 -0800224 struct net *net = seq_file_net(seq);
225
226 return *pos ? ipmr_mfc_seq_idx(net, seq->private, *pos - 1)
227 : SEQ_START_TOKEN;
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900228}
229
230static void *ipmr_mfc_seq_next(struct seq_file *seq, void *v, loff_t *pos)
231{
232 struct mfc6_cache *mfc = v;
233 struct ipmr_mfc_iter *it = seq->private;
Benjamin Thery8b90fc72008-12-10 16:29:48 -0800234 struct net *net = seq_file_net(seq);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900235
236 ++*pos;
237
238 if (v == SEQ_START_TOKEN)
Benjamin Thery8b90fc72008-12-10 16:29:48 -0800239 return ipmr_mfc_seq_idx(net, seq->private, 0);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900240
241 if (mfc->next)
242 return mfc->next;
243
Patrick McHardyc476efb2010-05-11 14:40:48 +0200244 if (it->cache == &net->ipv6.mfc6_unres_queue)
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900245 goto end_of_list;
246
Benjamin Thery8b90fc72008-12-10 16:29:48 -0800247 BUG_ON(it->cache != net->ipv6.mfc6_cache_array);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900248
Benjamin Thery4a6258a2008-12-10 16:24:07 -0800249 while (++it->ct < MFC6_LINES) {
Benjamin Thery8b90fc72008-12-10 16:29:48 -0800250 mfc = net->ipv6.mfc6_cache_array[it->ct];
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900251 if (mfc)
252 return mfc;
253 }
254
255 /* exhausted cache_array, show unresolved */
256 read_unlock(&mrt_lock);
Patrick McHardyc476efb2010-05-11 14:40:48 +0200257 it->cache = &net->ipv6.mfc6_unres_queue;
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900258 it->ct = 0;
259
260 spin_lock_bh(&mfc_unres_lock);
Patrick McHardyc476efb2010-05-11 14:40:48 +0200261 mfc = net->ipv6.mfc6_unres_queue;
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900262 if (mfc)
263 return mfc;
264
265 end_of_list:
266 spin_unlock_bh(&mfc_unres_lock);
267 it->cache = NULL;
268
269 return NULL;
270}
271
272static void ipmr_mfc_seq_stop(struct seq_file *seq, void *v)
273{
274 struct ipmr_mfc_iter *it = seq->private;
Benjamin Thery8b90fc72008-12-10 16:29:48 -0800275 struct net *net = seq_file_net(seq);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900276
Patrick McHardyc476efb2010-05-11 14:40:48 +0200277 if (it->cache == &net->ipv6.mfc6_unres_queue)
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900278 spin_unlock_bh(&mfc_unres_lock);
Benjamin Thery8b90fc72008-12-10 16:29:48 -0800279 else if (it->cache == net->ipv6.mfc6_cache_array)
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900280 read_unlock(&mrt_lock);
281}
282
283static int ipmr_mfc_seq_show(struct seq_file *seq, void *v)
284{
285 int n;
Benjamin Thery8b90fc72008-12-10 16:29:48 -0800286 struct net *net = seq_file_net(seq);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900287
288 if (v == SEQ_START_TOKEN) {
289 seq_puts(seq,
290 "Group "
291 "Origin "
292 "Iif Pkts Bytes Wrong Oifs\n");
293 } else {
294 const struct mfc6_cache *mfc = v;
295 const struct ipmr_mfc_iter *it = seq->private;
296
Benjamin Thery999890b2008-12-03 22:22:16 -0800297 seq_printf(seq, "%pI6 %pI6 %-3hd",
Harvey Harrison0c6ce782008-10-28 16:09:23 -0700298 &mfc->mf6c_mcastgrp, &mfc->mf6c_origin,
Benjamin Thery1ea472e2008-12-03 22:21:47 -0800299 mfc->mf6c_parent);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900300
Patrick McHardyc476efb2010-05-11 14:40:48 +0200301 if (it->cache != &net->ipv6.mfc6_unres_queue) {
Benjamin Thery1ea472e2008-12-03 22:21:47 -0800302 seq_printf(seq, " %8lu %8lu %8lu",
303 mfc->mfc_un.res.pkt,
304 mfc->mfc_un.res.bytes,
305 mfc->mfc_un.res.wrong_if);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900306 for (n = mfc->mfc_un.res.minvif;
307 n < mfc->mfc_un.res.maxvif; n++) {
Benjamin Thery8b90fc72008-12-10 16:29:48 -0800308 if (MIF_EXISTS(net, n) &&
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900309 mfc->mfc_un.res.ttls[n] < 255)
310 seq_printf(seq,
311 " %2d:%-3d",
312 n, mfc->mfc_un.res.ttls[n]);
313 }
Benjamin Thery1ea472e2008-12-03 22:21:47 -0800314 } else {
315 /* unresolved mfc_caches don't contain
316 * pkt, bytes and wrong_if values
317 */
318 seq_printf(seq, " %8lu %8lu %8lu", 0ul, 0ul, 0ul);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900319 }
320 seq_putc(seq, '\n');
321 }
322 return 0;
323}
324
James Morris88e9d342009-09-22 16:43:43 -0700325static const struct seq_operations ipmr_mfc_seq_ops = {
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900326 .start = ipmr_mfc_seq_start,
327 .next = ipmr_mfc_seq_next,
328 .stop = ipmr_mfc_seq_stop,
329 .show = ipmr_mfc_seq_show,
330};
331
332static int ipmr_mfc_open(struct inode *inode, struct file *file)
333{
Benjamin Thery8b90fc72008-12-10 16:29:48 -0800334 return seq_open_net(inode, file, &ipmr_mfc_seq_ops,
335 sizeof(struct ipmr_mfc_iter));
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900336}
337
Stephen Hemminger5ca1b992009-09-01 19:25:05 +0000338static const struct file_operations ip6mr_mfc_fops = {
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900339 .owner = THIS_MODULE,
340 .open = ipmr_mfc_open,
341 .read = seq_read,
342 .llseek = seq_lseek,
Benjamin Thery8b90fc72008-12-10 16:29:48 -0800343 .release = seq_release_net,
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900344};
345#endif
346
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +0900347#ifdef CONFIG_IPV6_PIMSM_V2
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +0900348
349static int pim6_rcv(struct sk_buff *skb)
350{
351 struct pimreghdr *pim;
352 struct ipv6hdr *encap;
353 struct net_device *reg_dev = NULL;
Benjamin Thery8229efd2008-12-10 16:30:15 -0800354 struct net *net = dev_net(skb->dev);
355 int reg_vif_num = net->ipv6.mroute_reg_vif_num;
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +0900356
357 if (!pskb_may_pull(skb, sizeof(*pim) + sizeof(*encap)))
358 goto drop;
359
360 pim = (struct pimreghdr *)skb_transport_header(skb);
361 if (pim->type != ((PIM_VERSION << 4) | PIM_REGISTER) ||
362 (pim->flags & PIM_NULL_REGISTER) ||
Thomas Goff1d6e55f2009-01-27 22:39:59 -0800363 (csum_ipv6_magic(&ipv6_hdr(skb)->saddr, &ipv6_hdr(skb)->daddr,
364 sizeof(*pim), IPPROTO_PIM,
365 csum_partial((void *)pim, sizeof(*pim), 0)) &&
Al Viroec6b4862008-04-26 22:28:58 -0700366 csum_fold(skb_checksum(skb, 0, skb->len, 0))))
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +0900367 goto drop;
368
369 /* check if the inner packet is destined to mcast group */
370 encap = (struct ipv6hdr *)(skb_transport_header(skb) +
371 sizeof(*pim));
372
373 if (!ipv6_addr_is_multicast(&encap->daddr) ||
374 encap->payload_len == 0 ||
375 ntohs(encap->payload_len) + sizeof(*pim) > skb->len)
376 goto drop;
377
378 read_lock(&mrt_lock);
379 if (reg_vif_num >= 0)
Benjamin Thery8229efd2008-12-10 16:30:15 -0800380 reg_dev = net->ipv6.vif6_table[reg_vif_num].dev;
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +0900381 if (reg_dev)
382 dev_hold(reg_dev);
383 read_unlock(&mrt_lock);
384
385 if (reg_dev == NULL)
386 goto drop;
387
388 skb->mac_header = skb->network_header;
389 skb_pull(skb, (u8 *)encap - skb->data);
390 skb_reset_network_header(skb);
391 skb->dev = reg_dev;
Thomas Goff1d6e55f2009-01-27 22:39:59 -0800392 skb->protocol = htons(ETH_P_IPV6);
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +0900393 skb->ip_summed = 0;
394 skb->pkt_type = PACKET_HOST;
Eric Dumazetadf30902009-06-02 05:19:30 +0000395 skb_dst_drop(skb);
Pavel Emelyanovdc58c782008-05-21 14:17:54 -0700396 reg_dev->stats.rx_bytes += skb->len;
397 reg_dev->stats.rx_packets++;
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +0900398 nf_reset(skb);
399 netif_rx(skb);
400 dev_put(reg_dev);
401 return 0;
402 drop:
403 kfree_skb(skb);
404 return 0;
405}
406
Alexey Dobriyan41135cc2009-09-14 12:22:28 +0000407static const struct inet6_protocol pim6_protocol = {
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +0900408 .handler = pim6_rcv,
409};
410
411/* Service routines creating virtual interfaces: PIMREG */
412
Stephen Hemminger6fef4c02009-08-31 19:50:41 +0000413static netdev_tx_t reg_vif_xmit(struct sk_buff *skb,
414 struct net_device *dev)
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +0900415{
Benjamin Thery8229efd2008-12-10 16:30:15 -0800416 struct net *net = dev_net(dev);
417
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +0900418 read_lock(&mrt_lock);
Pavel Emelyanovdc58c782008-05-21 14:17:54 -0700419 dev->stats.tx_bytes += skb->len;
420 dev->stats.tx_packets++;
Benjamin Thery8229efd2008-12-10 16:30:15 -0800421 ip6mr_cache_report(net, skb, net->ipv6.mroute_reg_vif_num,
422 MRT6MSG_WHOLEPKT);
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +0900423 read_unlock(&mrt_lock);
424 kfree_skb(skb);
Patrick McHardy6ed10652009-06-23 06:03:08 +0000425 return NETDEV_TX_OK;
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +0900426}
427
Stephen Hemminger007c3832008-11-20 20:28:35 -0800428static const struct net_device_ops reg_vif_netdev_ops = {
429 .ndo_start_xmit = reg_vif_xmit,
430};
431
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +0900432static void reg_vif_setup(struct net_device *dev)
433{
434 dev->type = ARPHRD_PIMREG;
435 dev->mtu = 1500 - sizeof(struct ipv6hdr) - 8;
436 dev->flags = IFF_NOARP;
Stephen Hemminger007c3832008-11-20 20:28:35 -0800437 dev->netdev_ops = &reg_vif_netdev_ops;
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +0900438 dev->destructor = free_netdev;
Tom Goff403dbb92009-06-14 03:16:13 -0700439 dev->features |= NETIF_F_NETNS_LOCAL;
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +0900440}
441
Benjamin Thery8229efd2008-12-10 16:30:15 -0800442static struct net_device *ip6mr_reg_vif(struct net *net)
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +0900443{
444 struct net_device *dev;
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +0900445
Pavel Emelyanovdc58c782008-05-21 14:17:54 -0700446 dev = alloc_netdev(0, "pim6reg", reg_vif_setup);
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +0900447 if (dev == NULL)
448 return NULL;
449
Benjamin Thery8229efd2008-12-10 16:30:15 -0800450 dev_net_set(dev, net);
451
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +0900452 if (register_netdevice(dev)) {
453 free_netdev(dev);
454 return NULL;
455 }
456 dev->iflink = 0;
457
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +0900458 if (dev_open(dev))
459 goto failure;
460
Wang Chen7af3db72008-07-14 20:54:54 -0700461 dev_hold(dev);
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +0900462 return dev;
463
464failure:
465 /* allow the register to be completed before unregistering. */
466 rtnl_unlock();
467 rtnl_lock();
468
469 unregister_netdevice(dev);
470 return NULL;
471}
472#endif
473
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900474/*
475 * Delete a VIF entry
476 */
477
Eric Dumazetc871e662009-10-28 04:48:11 +0000478static int mif6_delete(struct net *net, int vifi, struct list_head *head)
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900479{
480 struct mif_device *v;
481 struct net_device *dev;
Thomas Goff1d6e55f2009-01-27 22:39:59 -0800482 struct inet6_dev *in6_dev;
Benjamin Thery8229efd2008-12-10 16:30:15 -0800483 if (vifi < 0 || vifi >= net->ipv6.maxvif)
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900484 return -EADDRNOTAVAIL;
485
Benjamin Thery8229efd2008-12-10 16:30:15 -0800486 v = &net->ipv6.vif6_table[vifi];
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900487
488 write_lock_bh(&mrt_lock);
489 dev = v->dev;
490 v->dev = NULL;
491
492 if (!dev) {
493 write_unlock_bh(&mrt_lock);
494 return -EADDRNOTAVAIL;
495 }
496
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +0900497#ifdef CONFIG_IPV6_PIMSM_V2
Benjamin Thery8229efd2008-12-10 16:30:15 -0800498 if (vifi == net->ipv6.mroute_reg_vif_num)
499 net->ipv6.mroute_reg_vif_num = -1;
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +0900500#endif
501
Benjamin Thery8229efd2008-12-10 16:30:15 -0800502 if (vifi + 1 == net->ipv6.maxvif) {
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900503 int tmp;
504 for (tmp = vifi - 1; tmp >= 0; tmp--) {
Benjamin Thery8229efd2008-12-10 16:30:15 -0800505 if (MIF_EXISTS(net, tmp))
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900506 break;
507 }
Benjamin Thery8229efd2008-12-10 16:30:15 -0800508 net->ipv6.maxvif = tmp + 1;
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900509 }
510
511 write_unlock_bh(&mrt_lock);
512
513 dev_set_allmulti(dev, -1);
514
Thomas Goff1d6e55f2009-01-27 22:39:59 -0800515 in6_dev = __in6_dev_get(dev);
516 if (in6_dev)
517 in6_dev->cnf.mc_forwarding--;
518
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900519 if (v->flags & MIFF_REGISTER)
Eric Dumazetc871e662009-10-28 04:48:11 +0000520 unregister_netdevice_queue(dev, head);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900521
522 dev_put(dev);
523 return 0;
524}
525
Benjamin Thery58701ad2008-12-10 16:22:34 -0800526static inline void ip6mr_cache_free(struct mfc6_cache *c)
527{
Benjamin Thery58701ad2008-12-10 16:22:34 -0800528 kmem_cache_free(mrt_cachep, c);
529}
530
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900531/* Destroy an unresolved cache entry, killing queued skbs
532 and reporting error to netlink readers.
533 */
534
Patrick McHardyb5aa30b2010-05-11 14:40:50 +0200535static void ip6mr_destroy_unres(struct net *net, struct mfc6_cache *c)
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900536{
537 struct sk_buff *skb;
538
Benjamin Thery8229efd2008-12-10 16:30:15 -0800539 atomic_dec(&net->ipv6.cache_resolve_queue_len);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900540
541 while((skb = skb_dequeue(&c->mfc_un.unres.unresolved)) != NULL) {
542 if (ipv6_hdr(skb)->version == 0) {
543 struct nlmsghdr *nlh = (struct nlmsghdr *)skb_pull(skb, sizeof(struct ipv6hdr));
544 nlh->nlmsg_type = NLMSG_ERROR;
545 nlh->nlmsg_len = NLMSG_LENGTH(sizeof(struct nlmsgerr));
546 skb_trim(skb, nlh->nlmsg_len);
547 ((struct nlmsgerr *)NLMSG_DATA(nlh))->error = -ETIMEDOUT;
Benjamin Thery8229efd2008-12-10 16:30:15 -0800548 rtnl_unicast(skb, net, NETLINK_CB(skb).pid);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900549 } else
550 kfree_skb(skb);
551 }
552
Benjamin Thery58701ad2008-12-10 16:22:34 -0800553 ip6mr_cache_free(c);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900554}
555
556
Patrick McHardyc476efb2010-05-11 14:40:48 +0200557/* Timer process for all the unresolved queue. */
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900558
Patrick McHardyc476efb2010-05-11 14:40:48 +0200559static void ipmr_do_expire_process(struct net *net)
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900560{
561 unsigned long now = jiffies;
562 unsigned long expires = 10 * HZ;
563 struct mfc6_cache *c, **cp;
564
Patrick McHardyc476efb2010-05-11 14:40:48 +0200565 cp = &net->ipv6.mfc6_unres_queue;
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900566
567 while ((c = *cp) != NULL) {
568 if (time_after(c->mfc_un.unres.expires, now)) {
569 /* not yet... */
570 unsigned long interval = c->mfc_un.unres.expires - now;
571 if (interval < expires)
572 expires = interval;
573 cp = &c->next;
574 continue;
575 }
576
577 *cp = c->next;
Patrick McHardyb5aa30b2010-05-11 14:40:50 +0200578 ip6mr_destroy_unres(net, c);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900579 }
580
Patrick McHardyc476efb2010-05-11 14:40:48 +0200581 if (net->ipv6.mfc6_unres_queue != NULL)
582 mod_timer(&net->ipv6.ipmr_expire_timer, jiffies + expires);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900583}
584
Patrick McHardyc476efb2010-05-11 14:40:48 +0200585static void ipmr_expire_process(unsigned long arg)
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900586{
Patrick McHardyc476efb2010-05-11 14:40:48 +0200587 struct net *net = (struct net *)arg;
588
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900589 if (!spin_trylock(&mfc_unres_lock)) {
Patrick McHardyc476efb2010-05-11 14:40:48 +0200590 mod_timer(&net->ipv6.ipmr_expire_timer, jiffies + 1);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900591 return;
592 }
593
Patrick McHardyc476efb2010-05-11 14:40:48 +0200594 if (net->ipv6.mfc6_unres_queue != NULL)
595 ipmr_do_expire_process(net);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900596
597 spin_unlock(&mfc_unres_lock);
598}
599
600/* Fill oifs list. It is called under write locked mrt_lock. */
601
Patrick McHardyb5aa30b2010-05-11 14:40:50 +0200602static void ip6mr_update_thresholds(struct net *net, struct mfc6_cache *cache,
603 unsigned char *ttls)
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900604{
605 int vifi;
606
Rami Rosen6ac7eb02008-04-10 12:40:10 +0300607 cache->mfc_un.res.minvif = MAXMIFS;
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900608 cache->mfc_un.res.maxvif = 0;
Rami Rosen6ac7eb02008-04-10 12:40:10 +0300609 memset(cache->mfc_un.res.ttls, 255, MAXMIFS);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900610
Benjamin Thery8229efd2008-12-10 16:30:15 -0800611 for (vifi = 0; vifi < net->ipv6.maxvif; vifi++) {
612 if (MIF_EXISTS(net, vifi) &&
Benjamin Thery4e168802008-12-10 16:15:08 -0800613 ttls[vifi] && ttls[vifi] < 255) {
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900614 cache->mfc_un.res.ttls[vifi] = ttls[vifi];
615 if (cache->mfc_un.res.minvif > vifi)
616 cache->mfc_un.res.minvif = vifi;
617 if (cache->mfc_un.res.maxvif <= vifi)
618 cache->mfc_un.res.maxvif = vifi + 1;
619 }
620 }
621}
622
Benjamin Thery8229efd2008-12-10 16:30:15 -0800623static int mif6_add(struct net *net, struct mif6ctl *vifc, int mrtsock)
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900624{
625 int vifi = vifc->mif6c_mifi;
Benjamin Thery8229efd2008-12-10 16:30:15 -0800626 struct mif_device *v = &net->ipv6.vif6_table[vifi];
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900627 struct net_device *dev;
Thomas Goff1d6e55f2009-01-27 22:39:59 -0800628 struct inet6_dev *in6_dev;
Wang Chen5ae7b442008-07-14 20:54:23 -0700629 int err;
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900630
631 /* Is vif busy ? */
Benjamin Thery8229efd2008-12-10 16:30:15 -0800632 if (MIF_EXISTS(net, vifi))
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900633 return -EADDRINUSE;
634
635 switch (vifc->mif6c_flags) {
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +0900636#ifdef CONFIG_IPV6_PIMSM_V2
637 case MIFF_REGISTER:
638 /*
639 * Special Purpose VIF in PIM
640 * All the packets will be sent to the daemon
641 */
Benjamin Thery8229efd2008-12-10 16:30:15 -0800642 if (net->ipv6.mroute_reg_vif_num >= 0)
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +0900643 return -EADDRINUSE;
Benjamin Thery8229efd2008-12-10 16:30:15 -0800644 dev = ip6mr_reg_vif(net);
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +0900645 if (!dev)
646 return -ENOBUFS;
Wang Chen5ae7b442008-07-14 20:54:23 -0700647 err = dev_set_allmulti(dev, 1);
648 if (err) {
649 unregister_netdevice(dev);
Wang Chen7af3db72008-07-14 20:54:54 -0700650 dev_put(dev);
Wang Chen5ae7b442008-07-14 20:54:23 -0700651 return err;
652 }
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +0900653 break;
654#endif
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900655 case 0:
Benjamin Thery8229efd2008-12-10 16:30:15 -0800656 dev = dev_get_by_index(net, vifc->mif6c_pifi);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900657 if (!dev)
658 return -EADDRNOTAVAIL;
Wang Chen5ae7b442008-07-14 20:54:23 -0700659 err = dev_set_allmulti(dev, 1);
Wang Chen7af3db72008-07-14 20:54:54 -0700660 if (err) {
661 dev_put(dev);
Wang Chen5ae7b442008-07-14 20:54:23 -0700662 return err;
Wang Chen7af3db72008-07-14 20:54:54 -0700663 }
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900664 break;
665 default:
666 return -EINVAL;
667 }
668
Thomas Goff1d6e55f2009-01-27 22:39:59 -0800669 in6_dev = __in6_dev_get(dev);
670 if (in6_dev)
671 in6_dev->cnf.mc_forwarding++;
672
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900673 /*
674 * Fill in the VIF structures
675 */
676 v->rate_limit = vifc->vifc_rate_limit;
677 v->flags = vifc->mif6c_flags;
678 if (!mrtsock)
679 v->flags |= VIFF_STATIC;
680 v->threshold = vifc->vifc_threshold;
681 v->bytes_in = 0;
682 v->bytes_out = 0;
683 v->pkt_in = 0;
684 v->pkt_out = 0;
685 v->link = dev->ifindex;
686 if (v->flags & MIFF_REGISTER)
687 v->link = dev->iflink;
688
689 /* And finish update writing critical data */
690 write_lock_bh(&mrt_lock);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900691 v->dev = dev;
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +0900692#ifdef CONFIG_IPV6_PIMSM_V2
693 if (v->flags & MIFF_REGISTER)
Benjamin Thery8229efd2008-12-10 16:30:15 -0800694 net->ipv6.mroute_reg_vif_num = vifi;
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +0900695#endif
Benjamin Thery8229efd2008-12-10 16:30:15 -0800696 if (vifi + 1 > net->ipv6.maxvif)
697 net->ipv6.maxvif = vifi + 1;
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900698 write_unlock_bh(&mrt_lock);
699 return 0;
700}
701
Benjamin Thery8229efd2008-12-10 16:30:15 -0800702static struct mfc6_cache *ip6mr_cache_find(struct net *net,
703 struct in6_addr *origin,
704 struct in6_addr *mcastgrp)
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900705{
706 int line = MFC6_HASH(mcastgrp, origin);
707 struct mfc6_cache *c;
708
Benjamin Thery8229efd2008-12-10 16:30:15 -0800709 for (c = net->ipv6.mfc6_cache_array[line]; c; c = c->next) {
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900710 if (ipv6_addr_equal(&c->mf6c_origin, origin) &&
711 ipv6_addr_equal(&c->mf6c_mcastgrp, mcastgrp))
712 break;
713 }
714 return c;
715}
716
717/*
718 * Allocate a multicast cache entry
719 */
Patrick McHardyb5aa30b2010-05-11 14:40:50 +0200720static struct mfc6_cache *ip6mr_cache_alloc(void)
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900721{
Joe Perches36cbac52008-12-03 22:27:25 -0800722 struct mfc6_cache *c = kmem_cache_zalloc(mrt_cachep, GFP_KERNEL);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900723 if (c == NULL)
724 return NULL;
Rami Rosen6ac7eb02008-04-10 12:40:10 +0300725 c->mfc_un.res.minvif = MAXMIFS;
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900726 return c;
727}
728
Patrick McHardyb5aa30b2010-05-11 14:40:50 +0200729static struct mfc6_cache *ip6mr_cache_alloc_unres(void)
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900730{
Joe Perches36cbac52008-12-03 22:27:25 -0800731 struct mfc6_cache *c = kmem_cache_zalloc(mrt_cachep, GFP_ATOMIC);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900732 if (c == NULL)
733 return NULL;
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900734 skb_queue_head_init(&c->mfc_un.unres.unresolved);
735 c->mfc_un.unres.expires = jiffies + 10 * HZ;
736 return c;
737}
738
739/*
740 * A cache entry has gone into a resolved state from queued
741 */
742
Patrick McHardyb5aa30b2010-05-11 14:40:50 +0200743static void ip6mr_cache_resolve(struct net *net, struct mfc6_cache *uc,
744 struct mfc6_cache *c)
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900745{
746 struct sk_buff *skb;
747
748 /*
749 * Play the pending entries through our router
750 */
751
752 while((skb = __skb_dequeue(&uc->mfc_un.unres.unresolved))) {
753 if (ipv6_hdr(skb)->version == 0) {
754 int err;
755 struct nlmsghdr *nlh = (struct nlmsghdr *)skb_pull(skb, sizeof(struct ipv6hdr));
756
Patrick McHardyb5aa30b2010-05-11 14:40:50 +0200757 if (ip6mr_fill_mroute(net, skb, c, NLMSG_DATA(nlh)) > 0) {
YOSHIFUJI Hideaki549e0282008-04-05 22:17:39 +0900758 nlh->nlmsg_len = skb_tail_pointer(skb) - (u8 *)nlh;
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900759 } else {
760 nlh->nlmsg_type = NLMSG_ERROR;
761 nlh->nlmsg_len = NLMSG_LENGTH(sizeof(struct nlmsgerr));
762 skb_trim(skb, nlh->nlmsg_len);
763 ((struct nlmsgerr *)NLMSG_DATA(nlh))->error = -EMSGSIZE;
764 }
Patrick McHardyb5aa30b2010-05-11 14:40:50 +0200765 err = rtnl_unicast(skb, net, NETLINK_CB(skb).pid);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900766 } else
Patrick McHardyb5aa30b2010-05-11 14:40:50 +0200767 ip6_mr_forward(net, skb, c);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900768 }
769}
770
771/*
772 * Bounce a cache query up to pim6sd. We could use netlink for this but pim6sd
773 * expects the following bizarre scheme.
774 *
775 * Called under mrt_lock.
776 */
777
Benjamin Thery8229efd2008-12-10 16:30:15 -0800778static int ip6mr_cache_report(struct net *net, struct sk_buff *pkt, mifi_t mifi,
779 int assert)
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900780{
781 struct sk_buff *skb;
782 struct mrt6msg *msg;
783 int ret;
784
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +0900785#ifdef CONFIG_IPV6_PIMSM_V2
786 if (assert == MRT6MSG_WHOLEPKT)
787 skb = skb_realloc_headroom(pkt, -skb_network_offset(pkt)
788 +sizeof(*msg));
789 else
790#endif
791 skb = alloc_skb(sizeof(struct ipv6hdr) + sizeof(*msg), GFP_ATOMIC);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900792
793 if (!skb)
794 return -ENOBUFS;
795
796 /* I suppose that internal messages
797 * do not require checksums */
798
799 skb->ip_summed = CHECKSUM_UNNECESSARY;
800
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +0900801#ifdef CONFIG_IPV6_PIMSM_V2
802 if (assert == MRT6MSG_WHOLEPKT) {
803 /* Ugly, but we have no choice with this interface.
804 Duplicate old header, fix length etc.
805 And all this only to mangle msg->im6_msgtype and
806 to set msg->im6_mbz to "mbz" :-)
807 */
808 skb_push(skb, -skb_network_offset(pkt));
809
810 skb_push(skb, sizeof(*msg));
811 skb_reset_transport_header(skb);
812 msg = (struct mrt6msg *)skb_transport_header(skb);
813 msg->im6_mbz = 0;
814 msg->im6_msgtype = MRT6MSG_WHOLEPKT;
Benjamin Thery8229efd2008-12-10 16:30:15 -0800815 msg->im6_mif = net->ipv6.mroute_reg_vif_num;
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +0900816 msg->im6_pad = 0;
817 ipv6_addr_copy(&msg->im6_src, &ipv6_hdr(pkt)->saddr);
818 ipv6_addr_copy(&msg->im6_dst, &ipv6_hdr(pkt)->daddr);
819
820 skb->ip_summed = CHECKSUM_UNNECESSARY;
821 } else
822#endif
823 {
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900824 /*
825 * Copy the IP header
826 */
827
828 skb_put(skb, sizeof(struct ipv6hdr));
829 skb_reset_network_header(skb);
830 skb_copy_to_linear_data(skb, ipv6_hdr(pkt), sizeof(struct ipv6hdr));
831
832 /*
833 * Add our header
834 */
835 skb_put(skb, sizeof(*msg));
836 skb_reset_transport_header(skb);
837 msg = (struct mrt6msg *)skb_transport_header(skb);
838
839 msg->im6_mbz = 0;
840 msg->im6_msgtype = assert;
Rami Rosen6ac7eb02008-04-10 12:40:10 +0300841 msg->im6_mif = mifi;
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900842 msg->im6_pad = 0;
843 ipv6_addr_copy(&msg->im6_src, &ipv6_hdr(pkt)->saddr);
844 ipv6_addr_copy(&msg->im6_dst, &ipv6_hdr(pkt)->daddr);
845
Eric Dumazetadf30902009-06-02 05:19:30 +0000846 skb_dst_set(skb, dst_clone(skb_dst(pkt)));
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900847 skb->ip_summed = CHECKSUM_UNNECESSARY;
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +0900848 }
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900849
Benjamin Thery8229efd2008-12-10 16:30:15 -0800850 if (net->ipv6.mroute6_sk == NULL) {
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900851 kfree_skb(skb);
852 return -EINVAL;
853 }
854
855 /*
856 * Deliver to user space multicast routing algorithms
857 */
Benjamin Thery8229efd2008-12-10 16:30:15 -0800858 ret = sock_queue_rcv_skb(net->ipv6.mroute6_sk, skb);
Benjamin Therybd91b8b2008-12-10 16:07:08 -0800859 if (ret < 0) {
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900860 if (net_ratelimit())
861 printk(KERN_WARNING "mroute6: pending queue full, dropping entries.\n");
862 kfree_skb(skb);
863 }
864
865 return ret;
866}
867
868/*
869 * Queue a packet for resolution. It gets locked cache entry!
870 */
871
872static int
Benjamin Thery8229efd2008-12-10 16:30:15 -0800873ip6mr_cache_unresolved(struct net *net, mifi_t mifi, struct sk_buff *skb)
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900874{
875 int err;
876 struct mfc6_cache *c;
877
878 spin_lock_bh(&mfc_unres_lock);
Patrick McHardyc476efb2010-05-11 14:40:48 +0200879 for (c = net->ipv6.mfc6_unres_queue; c; c = c->next) {
880 if (ipv6_addr_equal(&c->mf6c_mcastgrp, &ipv6_hdr(skb)->daddr) &&
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900881 ipv6_addr_equal(&c->mf6c_origin, &ipv6_hdr(skb)->saddr))
882 break;
883 }
884
885 if (c == NULL) {
886 /*
887 * Create a new entry if allowable
888 */
889
Benjamin Thery8229efd2008-12-10 16:30:15 -0800890 if (atomic_read(&net->ipv6.cache_resolve_queue_len) >= 10 ||
Patrick McHardyb5aa30b2010-05-11 14:40:50 +0200891 (c = ip6mr_cache_alloc_unres()) == NULL) {
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900892 spin_unlock_bh(&mfc_unres_lock);
893
894 kfree_skb(skb);
895 return -ENOBUFS;
896 }
897
898 /*
899 * Fill in the new cache entry
900 */
901 c->mf6c_parent = -1;
902 c->mf6c_origin = ipv6_hdr(skb)->saddr;
903 c->mf6c_mcastgrp = ipv6_hdr(skb)->daddr;
904
905 /*
906 * Reflect first query at pim6sd
907 */
Benjamin Thery8229efd2008-12-10 16:30:15 -0800908 err = ip6mr_cache_report(net, skb, mifi, MRT6MSG_NOCACHE);
909 if (err < 0) {
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900910 /* If the report failed throw the cache entry
911 out - Brad Parker
912 */
913 spin_unlock_bh(&mfc_unres_lock);
914
Benjamin Thery58701ad2008-12-10 16:22:34 -0800915 ip6mr_cache_free(c);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900916 kfree_skb(skb);
917 return err;
918 }
919
Benjamin Thery8229efd2008-12-10 16:30:15 -0800920 atomic_inc(&net->ipv6.cache_resolve_queue_len);
Patrick McHardyc476efb2010-05-11 14:40:48 +0200921 c->next = net->ipv6.mfc6_unres_queue;
922 net->ipv6.mfc6_unres_queue = c;
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900923
Patrick McHardyc476efb2010-05-11 14:40:48 +0200924 ipmr_do_expire_process(net);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900925 }
926
927 /*
928 * See if we can append the packet
929 */
930 if (c->mfc_un.unres.unresolved.qlen > 3) {
931 kfree_skb(skb);
932 err = -ENOBUFS;
933 } else {
934 skb_queue_tail(&c->mfc_un.unres.unresolved, skb);
935 err = 0;
936 }
937
938 spin_unlock_bh(&mfc_unres_lock);
939 return err;
940}
941
942/*
943 * MFC6 cache manipulation by user space
944 */
945
Benjamin Thery8229efd2008-12-10 16:30:15 -0800946static int ip6mr_mfc_delete(struct net *net, struct mf6cctl *mfc)
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900947{
948 int line;
949 struct mfc6_cache *c, **cp;
950
951 line = MFC6_HASH(&mfc->mf6cc_mcastgrp.sin6_addr, &mfc->mf6cc_origin.sin6_addr);
952
Benjamin Thery8229efd2008-12-10 16:30:15 -0800953 for (cp = &net->ipv6.mfc6_cache_array[line];
Benjamin Thery4a6258a2008-12-10 16:24:07 -0800954 (c = *cp) != NULL; cp = &c->next) {
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900955 if (ipv6_addr_equal(&c->mf6c_origin, &mfc->mf6cc_origin.sin6_addr) &&
956 ipv6_addr_equal(&c->mf6c_mcastgrp, &mfc->mf6cc_mcastgrp.sin6_addr)) {
957 write_lock_bh(&mrt_lock);
958 *cp = c->next;
959 write_unlock_bh(&mrt_lock);
960
Benjamin Thery58701ad2008-12-10 16:22:34 -0800961 ip6mr_cache_free(c);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900962 return 0;
963 }
964 }
965 return -ENOENT;
966}
967
968static int ip6mr_device_event(struct notifier_block *this,
969 unsigned long event, void *ptr)
970{
971 struct net_device *dev = ptr;
Benjamin Thery8229efd2008-12-10 16:30:15 -0800972 struct net *net = dev_net(dev);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900973 struct mif_device *v;
974 int ct;
Eric Dumazetc871e662009-10-28 04:48:11 +0000975 LIST_HEAD(list);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900976
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900977 if (event != NETDEV_UNREGISTER)
978 return NOTIFY_DONE;
979
Benjamin Thery8229efd2008-12-10 16:30:15 -0800980 v = &net->ipv6.vif6_table[0];
981 for (ct = 0; ct < net->ipv6.maxvif; ct++, v++) {
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900982 if (v->dev == dev)
Eric Dumazetc871e662009-10-28 04:48:11 +0000983 mif6_delete(net, ct, &list);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900984 }
Eric Dumazetc871e662009-10-28 04:48:11 +0000985 unregister_netdevice_many(&list);
986
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900987 return NOTIFY_DONE;
988}
989
990static struct notifier_block ip6_mr_notifier = {
991 .notifier_call = ip6mr_device_event
992};
993
994/*
995 * Setup for IP multicast routing
996 */
997
Benjamin Thery4e168802008-12-10 16:15:08 -0800998static int __net_init ip6mr_net_init(struct net *net)
999{
1000 int err = 0;
Benjamin Thery4e168802008-12-10 16:15:08 -08001001 net->ipv6.vif6_table = kcalloc(MAXMIFS, sizeof(struct mif_device),
1002 GFP_KERNEL);
1003 if (!net->ipv6.vif6_table) {
1004 err = -ENOMEM;
1005 goto fail;
1006 }
Benjamin Thery4a6258a2008-12-10 16:24:07 -08001007
1008 /* Forwarding cache */
1009 net->ipv6.mfc6_cache_array = kcalloc(MFC6_LINES,
1010 sizeof(struct mfc6_cache *),
1011 GFP_KERNEL);
1012 if (!net->ipv6.mfc6_cache_array) {
1013 err = -ENOMEM;
1014 goto fail_mfc6_cache;
1015 }
Benjamin Thery950d5702008-12-10 16:29:24 -08001016
Patrick McHardyc476efb2010-05-11 14:40:48 +02001017 setup_timer(&net->ipv6.ipmr_expire_timer, ipmr_expire_process,
1018 (unsigned long)net);
1019
Benjamin Thery950d5702008-12-10 16:29:24 -08001020#ifdef CONFIG_IPV6_PIMSM_V2
1021 net->ipv6.mroute_reg_vif_num = -1;
1022#endif
Benjamin Thery8b90fc72008-12-10 16:29:48 -08001023
1024#ifdef CONFIG_PROC_FS
1025 err = -ENOMEM;
1026 if (!proc_net_fops_create(net, "ip6_mr_vif", 0, &ip6mr_vif_fops))
1027 goto proc_vif_fail;
1028 if (!proc_net_fops_create(net, "ip6_mr_cache", 0, &ip6mr_mfc_fops))
1029 goto proc_cache_fail;
1030#endif
Benjamin Thery4a6258a2008-12-10 16:24:07 -08001031 return 0;
1032
Benjamin Thery8b90fc72008-12-10 16:29:48 -08001033#ifdef CONFIG_PROC_FS
1034proc_cache_fail:
1035 proc_net_remove(net, "ip6_mr_vif");
1036proc_vif_fail:
1037 kfree(net->ipv6.mfc6_cache_array);
1038#endif
Benjamin Thery4a6258a2008-12-10 16:24:07 -08001039fail_mfc6_cache:
1040 kfree(net->ipv6.vif6_table);
Benjamin Thery4e168802008-12-10 16:15:08 -08001041fail:
1042 return err;
1043}
1044
1045static void __net_exit ip6mr_net_exit(struct net *net)
1046{
Benjamin Thery8b90fc72008-12-10 16:29:48 -08001047#ifdef CONFIG_PROC_FS
1048 proc_net_remove(net, "ip6_mr_cache");
1049 proc_net_remove(net, "ip6_mr_vif");
1050#endif
Patrick McHardyc476efb2010-05-11 14:40:48 +02001051 del_timer(&net->ipv6.ipmr_expire_timer);
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
Wang Chen623d1a12008-07-03 12:13:30 +08001077 err = register_netdevice_notifier(&ip6_mr_notifier);
1078 if (err)
1079 goto reg_notif_fail;
Tom Goff403dbb92009-06-14 03:16:13 -07001080#ifdef CONFIG_IPV6_PIMSM_V2
1081 if (inet6_add_protocol(&pim6_protocol, IPPROTO_PIM) < 0) {
1082 printk(KERN_ERR "ip6_mr_init: can't add PIM protocol\n");
1083 err = -EAGAIN;
1084 goto add_proto_fail;
1085 }
1086#endif
Wang Chen623d1a12008-07-03 12:13:30 +08001087 return 0;
Tom Goff403dbb92009-06-14 03:16:13 -07001088#ifdef CONFIG_IPV6_PIMSM_V2
1089add_proto_fail:
1090 unregister_netdevice_notifier(&ip6_mr_notifier);
1091#endif
Benjamin Thery87b30a62008-11-10 16:34:11 -08001092reg_notif_fail:
Benjamin Thery4e168802008-12-10 16:15:08 -08001093 unregister_pernet_subsys(&ip6mr_net_ops);
1094reg_pernet_fail:
Benjamin Thery87b30a62008-11-10 16:34:11 -08001095 kmem_cache_destroy(mrt_cachep);
Wang Chen623d1a12008-07-03 12:13:30 +08001096 return err;
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001097}
1098
Wang Chen623d1a12008-07-03 12:13:30 +08001099void ip6_mr_cleanup(void)
1100{
Wang Chen623d1a12008-07-03 12:13:30 +08001101 unregister_netdevice_notifier(&ip6_mr_notifier);
Benjamin Thery4e168802008-12-10 16:15:08 -08001102 unregister_pernet_subsys(&ip6mr_net_ops);
Wang Chen623d1a12008-07-03 12:13:30 +08001103 kmem_cache_destroy(mrt_cachep);
1104}
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001105
Benjamin Thery8229efd2008-12-10 16:30:15 -08001106static int ip6mr_mfc_add(struct net *net, struct mf6cctl *mfc, int mrtsock)
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001107{
1108 int line;
1109 struct mfc6_cache *uc, *c, **cp;
Rami Rosen6ac7eb02008-04-10 12:40:10 +03001110 unsigned char ttls[MAXMIFS];
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001111 int i;
1112
Patrick McHardya50436f22010-03-17 06:04:14 +00001113 if (mfc->mf6cc_parent >= MAXMIFS)
1114 return -ENFILE;
1115
Rami Rosen6ac7eb02008-04-10 12:40:10 +03001116 memset(ttls, 255, MAXMIFS);
1117 for (i = 0; i < MAXMIFS; i++) {
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001118 if (IF_ISSET(i, &mfc->mf6cc_ifset))
1119 ttls[i] = 1;
1120
1121 }
1122
1123 line = MFC6_HASH(&mfc->mf6cc_mcastgrp.sin6_addr, &mfc->mf6cc_origin.sin6_addr);
1124
Benjamin Thery8229efd2008-12-10 16:30:15 -08001125 for (cp = &net->ipv6.mfc6_cache_array[line];
Benjamin Thery4a6258a2008-12-10 16:24:07 -08001126 (c = *cp) != NULL; cp = &c->next) {
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001127 if (ipv6_addr_equal(&c->mf6c_origin, &mfc->mf6cc_origin.sin6_addr) &&
1128 ipv6_addr_equal(&c->mf6c_mcastgrp, &mfc->mf6cc_mcastgrp.sin6_addr))
1129 break;
1130 }
1131
1132 if (c != NULL) {
1133 write_lock_bh(&mrt_lock);
1134 c->mf6c_parent = mfc->mf6cc_parent;
Patrick McHardyb5aa30b2010-05-11 14:40:50 +02001135 ip6mr_update_thresholds(net, c, ttls);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001136 if (!mrtsock)
1137 c->mfc_flags |= MFC_STATIC;
1138 write_unlock_bh(&mrt_lock);
1139 return 0;
1140 }
1141
1142 if (!ipv6_addr_is_multicast(&mfc->mf6cc_mcastgrp.sin6_addr))
1143 return -EINVAL;
1144
Patrick McHardyb5aa30b2010-05-11 14:40:50 +02001145 c = ip6mr_cache_alloc();
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001146 if (c == NULL)
1147 return -ENOMEM;
1148
1149 c->mf6c_origin = mfc->mf6cc_origin.sin6_addr;
1150 c->mf6c_mcastgrp = mfc->mf6cc_mcastgrp.sin6_addr;
1151 c->mf6c_parent = mfc->mf6cc_parent;
Patrick McHardyb5aa30b2010-05-11 14:40:50 +02001152 ip6mr_update_thresholds(net, c, ttls);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001153 if (!mrtsock)
1154 c->mfc_flags |= MFC_STATIC;
1155
1156 write_lock_bh(&mrt_lock);
Benjamin Thery8229efd2008-12-10 16:30:15 -08001157 c->next = net->ipv6.mfc6_cache_array[line];
1158 net->ipv6.mfc6_cache_array[line] = c;
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001159 write_unlock_bh(&mrt_lock);
1160
1161 /*
1162 * Check to see if we resolved a queued list. If so we
1163 * need to send on the frames and tidy up.
1164 */
1165 spin_lock_bh(&mfc_unres_lock);
Patrick McHardyc476efb2010-05-11 14:40:48 +02001166 for (cp = &net->ipv6.mfc6_unres_queue; (uc = *cp) != NULL;
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001167 cp = &uc->next) {
Patrick McHardyc476efb2010-05-11 14:40:48 +02001168 if (ipv6_addr_equal(&uc->mf6c_origin, &c->mf6c_origin) &&
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001169 ipv6_addr_equal(&uc->mf6c_mcastgrp, &c->mf6c_mcastgrp)) {
1170 *cp = uc->next;
Benjamin Thery8229efd2008-12-10 16:30:15 -08001171 atomic_dec(&net->ipv6.cache_resolve_queue_len);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001172 break;
1173 }
1174 }
Patrick McHardyc476efb2010-05-11 14:40:48 +02001175 if (net->ipv6.mfc6_unres_queue == NULL)
1176 del_timer(&net->ipv6.ipmr_expire_timer);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001177 spin_unlock_bh(&mfc_unres_lock);
1178
1179 if (uc) {
Patrick McHardyb5aa30b2010-05-11 14:40:50 +02001180 ip6mr_cache_resolve(net, uc, c);
Benjamin Thery58701ad2008-12-10 16:22:34 -08001181 ip6mr_cache_free(uc);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001182 }
1183 return 0;
1184}
1185
1186/*
1187 * Close the multicast socket, and clear the vif tables etc
1188 */
1189
Benjamin Thery8229efd2008-12-10 16:30:15 -08001190static void mroute_clean_tables(struct net *net)
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001191{
1192 int i;
Eric Dumazetc871e662009-10-28 04:48:11 +00001193 LIST_HEAD(list);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001194
1195 /*
1196 * Shut down all active vif entries
1197 */
Benjamin Thery8229efd2008-12-10 16:30:15 -08001198 for (i = 0; i < net->ipv6.maxvif; i++) {
1199 if (!(net->ipv6.vif6_table[i].flags & VIFF_STATIC))
Eric Dumazetc871e662009-10-28 04:48:11 +00001200 mif6_delete(net, i, &list);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001201 }
Eric Dumazetc871e662009-10-28 04:48:11 +00001202 unregister_netdevice_many(&list);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001203
1204 /*
1205 * Wipe the cache
1206 */
Benjamin Thery4a6258a2008-12-10 16:24:07 -08001207 for (i = 0; i < MFC6_LINES; i++) {
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001208 struct mfc6_cache *c, **cp;
1209
Benjamin Thery8229efd2008-12-10 16:30:15 -08001210 cp = &net->ipv6.mfc6_cache_array[i];
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001211 while ((c = *cp) != NULL) {
1212 if (c->mfc_flags & MFC_STATIC) {
1213 cp = &c->next;
1214 continue;
1215 }
1216 write_lock_bh(&mrt_lock);
1217 *cp = c->next;
1218 write_unlock_bh(&mrt_lock);
1219
Benjamin Thery58701ad2008-12-10 16:22:34 -08001220 ip6mr_cache_free(c);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001221 }
1222 }
1223
Benjamin Thery8229efd2008-12-10 16:30:15 -08001224 if (atomic_read(&net->ipv6.cache_resolve_queue_len) != 0) {
Benjamin Thery4045e572008-12-10 16:27:21 -08001225 struct mfc6_cache *c, **cp;
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001226
1227 spin_lock_bh(&mfc_unres_lock);
Patrick McHardyc476efb2010-05-11 14:40:48 +02001228 cp = &net->ipv6.mfc6_unres_queue;
Benjamin Thery4045e572008-12-10 16:27:21 -08001229 while ((c = *cp) != NULL) {
Benjamin Thery4045e572008-12-10 16:27:21 -08001230 *cp = c->next;
Patrick McHardyb5aa30b2010-05-11 14:40:50 +02001231 ip6mr_destroy_unres(net, c);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001232 }
1233 spin_unlock_bh(&mfc_unres_lock);
1234 }
1235}
1236
1237static int ip6mr_sk_init(struct sock *sk)
1238{
1239 int err = 0;
Benjamin Thery8229efd2008-12-10 16:30:15 -08001240 struct net *net = sock_net(sk);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001241
1242 rtnl_lock();
1243 write_lock_bh(&mrt_lock);
Thomas Goff1d6e55f2009-01-27 22:39:59 -08001244 if (likely(net->ipv6.mroute6_sk == NULL)) {
Benjamin Thery8229efd2008-12-10 16:30:15 -08001245 net->ipv6.mroute6_sk = sk;
Thomas Goff1d6e55f2009-01-27 22:39:59 -08001246 net->ipv6.devconf_all->mc_forwarding++;
1247 }
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001248 else
1249 err = -EADDRINUSE;
1250 write_unlock_bh(&mrt_lock);
1251
1252 rtnl_unlock();
1253
1254 return err;
1255}
1256
1257int ip6mr_sk_done(struct sock *sk)
1258{
1259 int err = 0;
Benjamin Thery8229efd2008-12-10 16:30:15 -08001260 struct net *net = sock_net(sk);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001261
1262 rtnl_lock();
Benjamin Thery8229efd2008-12-10 16:30:15 -08001263 if (sk == net->ipv6.mroute6_sk) {
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001264 write_lock_bh(&mrt_lock);
Benjamin Thery8229efd2008-12-10 16:30:15 -08001265 net->ipv6.mroute6_sk = NULL;
Thomas Goff1d6e55f2009-01-27 22:39:59 -08001266 net->ipv6.devconf_all->mc_forwarding--;
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001267 write_unlock_bh(&mrt_lock);
1268
Benjamin Thery8229efd2008-12-10 16:30:15 -08001269 mroute_clean_tables(net);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001270 } else
1271 err = -EACCES;
1272 rtnl_unlock();
1273
1274 return err;
1275}
1276
1277/*
1278 * Socket options and virtual interface manipulation. The whole
1279 * virtual interface system is a complete heap, but unfortunately
1280 * that's how BSD mrouted happens to think. Maybe one day with a proper
1281 * MOSPF/PIM router set up we can clean this up.
1282 */
1283
David S. Millerb7058842009-09-30 16:12:20 -07001284int ip6_mroute_setsockopt(struct sock *sk, int optname, char __user *optval, unsigned int optlen)
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001285{
1286 int ret;
1287 struct mif6ctl vif;
1288 struct mf6cctl mfc;
1289 mifi_t mifi;
Benjamin Thery8229efd2008-12-10 16:30:15 -08001290 struct net *net = sock_net(sk);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001291
1292 if (optname != MRT6_INIT) {
Benjamin Thery8229efd2008-12-10 16:30:15 -08001293 if (sk != net->ipv6.mroute6_sk && !capable(CAP_NET_ADMIN))
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001294 return -EACCES;
1295 }
1296
1297 switch (optname) {
1298 case MRT6_INIT:
1299 if (sk->sk_type != SOCK_RAW ||
Eric Dumazetc720c7e2009-10-15 06:30:45 +00001300 inet_sk(sk)->inet_num != IPPROTO_ICMPV6)
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001301 return -EOPNOTSUPP;
1302 if (optlen < sizeof(int))
1303 return -EINVAL;
1304
1305 return ip6mr_sk_init(sk);
1306
1307 case MRT6_DONE:
1308 return ip6mr_sk_done(sk);
1309
1310 case MRT6_ADD_MIF:
1311 if (optlen < sizeof(vif))
1312 return -EINVAL;
1313 if (copy_from_user(&vif, optval, sizeof(vif)))
1314 return -EFAULT;
Rami Rosen6ac7eb02008-04-10 12:40:10 +03001315 if (vif.mif6c_mifi >= MAXMIFS)
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001316 return -ENFILE;
1317 rtnl_lock();
Benjamin Thery8229efd2008-12-10 16:30:15 -08001318 ret = mif6_add(net, &vif, sk == net->ipv6.mroute6_sk);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001319 rtnl_unlock();
1320 return ret;
1321
1322 case MRT6_DEL_MIF:
1323 if (optlen < sizeof(mifi_t))
1324 return -EINVAL;
1325 if (copy_from_user(&mifi, optval, sizeof(mifi_t)))
1326 return -EFAULT;
1327 rtnl_lock();
Eric Dumazetc871e662009-10-28 04:48:11 +00001328 ret = mif6_delete(net, mifi, NULL);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001329 rtnl_unlock();
1330 return ret;
1331
1332 /*
1333 * Manipulate the forwarding caches. These live
1334 * in a sort of kernel/user symbiosis.
1335 */
1336 case MRT6_ADD_MFC:
1337 case MRT6_DEL_MFC:
1338 if (optlen < sizeof(mfc))
1339 return -EINVAL;
1340 if (copy_from_user(&mfc, optval, sizeof(mfc)))
1341 return -EFAULT;
1342 rtnl_lock();
1343 if (optname == MRT6_DEL_MFC)
Benjamin Thery8229efd2008-12-10 16:30:15 -08001344 ret = ip6mr_mfc_delete(net, &mfc);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001345 else
Benjamin Thery8229efd2008-12-10 16:30:15 -08001346 ret = ip6mr_mfc_add(net, &mfc,
1347 sk == net->ipv6.mroute6_sk);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001348 rtnl_unlock();
1349 return ret;
1350
1351 /*
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +09001352 * Control PIM assert (to activate pim will activate assert)
1353 */
1354 case MRT6_ASSERT:
1355 {
1356 int v;
1357 if (get_user(v, (int __user *)optval))
1358 return -EFAULT;
Benjamin Thery8229efd2008-12-10 16:30:15 -08001359 net->ipv6.mroute_do_assert = !!v;
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +09001360 return 0;
1361 }
1362
1363#ifdef CONFIG_IPV6_PIMSM_V2
1364 case MRT6_PIM:
1365 {
YOSHIFUJI Hideakia9f83bf2008-04-10 15:41:28 +09001366 int v;
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +09001367 if (get_user(v, (int __user *)optval))
1368 return -EFAULT;
1369 v = !!v;
1370 rtnl_lock();
1371 ret = 0;
Benjamin Thery8229efd2008-12-10 16:30:15 -08001372 if (v != net->ipv6.mroute_do_pim) {
1373 net->ipv6.mroute_do_pim = v;
1374 net->ipv6.mroute_do_assert = v;
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +09001375 }
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{
Eric Dumazetadf30902009-06-02 05:19:30 +00001490 IP6_INC_STATS_BH(dev_net(skb_dst(skb)->dev), ip6_dst_idev(skb_dst(skb)),
Denis V. Lunev483a47d2008-10-08 11:09:27 -07001491 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
Patrick McHardyb5aa30b2010-05-11 14:40:50 +02001499static int ip6mr_forward2(struct net *net, struct sk_buff *skb,
1500 struct mfc6_cache *c, int vifi)
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001501{
1502 struct ipv6hdr *ipv6h;
Benjamin Thery8229efd2008-12-10 16:30:15 -08001503 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
Eric Dumazetadf30902009-06-02 05:19:30 +00001535 skb_dst_drop(skb);
1536 skb_dst_set(skb, dst);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001537
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
Jan Engelhardtb2e0b382010-03-23 04:09:07 +01001564 return NF_HOOK(NFPROTO_IPV6, NF_INET_FORWARD, skb, skb->dev, dev,
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001565 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
Patrick McHardyb5aa30b2010-05-11 14:40:50 +02001583static int ip6_mr_forward(struct net *net, struct sk_buff *skb,
1584 struct mfc6_cache *cache)
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001585{
1586 int psend = -1;
1587 int vif, ct;
1588
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)
Patrick McHardyb5aa30b2010-05-11 14:40:50 +02001629 ip6mr_forward2(net, skb2, cache, psend);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001630 }
1631 psend = ct;
1632 }
1633 }
1634 if (psend != -1) {
Patrick McHardyb5aa30b2010-05-11 14:40:50 +02001635 ip6mr_forward2(net, skb, cache, psend);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001636 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
Patrick McHardyb5aa30b2010-05-11 14:40:50 +02001676 ip6_mr_forward(net, skb, cache);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001677
1678 read_unlock(&mrt_lock);
1679
1680 return 0;
1681}
1682
1683
1684static int
Patrick McHardyb5aa30b2010-05-11 14:40:50 +02001685ip6mr_fill_mroute(struct net *net, struct sk_buff *skb, struct mfc6_cache *c,
1686 struct rtmsg *rtm)
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001687{
1688 int ct;
1689 struct rtnexthop *nhp;
YOSHIFUJI Hideaki549e0282008-04-05 22:17:39 +09001690 u8 *b = skb_tail_pointer(skb);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001691 struct rtattr *mp_head;
1692
Nicolas Dichtel74381892010-03-25 23:45:35 +00001693 /* If cache is unresolved, don't try to parse IIF and OIF */
1694 if (c->mf6c_parent > MAXMIFS)
1695 return -ENOENT;
1696
1697 if (MIF_EXISTS(net, c->mf6c_parent))
1698 RTA_PUT(skb, RTA_IIF, 4, &net->ipv6.vif6_table[c->mf6c_parent].dev->ifindex);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001699
1700 mp_head = (struct rtattr *)skb_put(skb, RTA_LENGTH(0));
1701
1702 for (ct = c->mfc_un.res.minvif; ct < c->mfc_un.res.maxvif; ct++) {
Nicolas Dichtel74381892010-03-25 23:45:35 +00001703 if (MIF_EXISTS(net, ct) && c->mfc_un.res.ttls[ct] < 255) {
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001704 if (skb_tailroom(skb) < RTA_ALIGN(RTA_ALIGN(sizeof(*nhp)) + 4))
1705 goto rtattr_failure;
1706 nhp = (struct rtnexthop *)skb_put(skb, RTA_ALIGN(sizeof(*nhp)));
1707 nhp->rtnh_flags = 0;
1708 nhp->rtnh_hops = c->mfc_un.res.ttls[ct];
Benjamin Thery8229efd2008-12-10 16:30:15 -08001709 nhp->rtnh_ifindex = net->ipv6.vif6_table[ct].dev->ifindex;
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001710 nhp->rtnh_len = sizeof(*nhp);
1711 }
1712 }
1713 mp_head->rta_type = RTA_MULTIPATH;
YOSHIFUJI Hideaki549e0282008-04-05 22:17:39 +09001714 mp_head->rta_len = skb_tail_pointer(skb) - (u8 *)mp_head;
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001715 rtm->rtm_type = RTN_MULTICAST;
1716 return 1;
1717
1718rtattr_failure:
1719 nlmsg_trim(skb, b);
1720 return -EMSGSIZE;
1721}
1722
Benjamin Thery8229efd2008-12-10 16:30:15 -08001723int ip6mr_get_route(struct net *net,
1724 struct sk_buff *skb, struct rtmsg *rtm, int nowait)
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001725{
1726 int err;
1727 struct mfc6_cache *cache;
Eric Dumazetadf30902009-06-02 05:19:30 +00001728 struct rt6_info *rt = (struct rt6_info *)skb_dst(skb);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001729
1730 read_lock(&mrt_lock);
Benjamin Thery8229efd2008-12-10 16:30:15 -08001731 cache = ip6mr_cache_find(net, &rt->rt6i_src.addr, &rt->rt6i_dst.addr);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001732
1733 if (!cache) {
1734 struct sk_buff *skb2;
1735 struct ipv6hdr *iph;
1736 struct net_device *dev;
1737 int vif;
1738
1739 if (nowait) {
1740 read_unlock(&mrt_lock);
1741 return -EAGAIN;
1742 }
1743
1744 dev = skb->dev;
1745 if (dev == NULL || (vif = ip6mr_find_vif(dev)) < 0) {
1746 read_unlock(&mrt_lock);
1747 return -ENODEV;
1748 }
1749
1750 /* really correct? */
1751 skb2 = alloc_skb(sizeof(struct ipv6hdr), GFP_ATOMIC);
1752 if (!skb2) {
1753 read_unlock(&mrt_lock);
1754 return -ENOMEM;
1755 }
1756
1757 skb_reset_transport_header(skb2);
1758
1759 skb_put(skb2, sizeof(struct ipv6hdr));
1760 skb_reset_network_header(skb2);
1761
1762 iph = ipv6_hdr(skb2);
1763 iph->version = 0;
1764 iph->priority = 0;
1765 iph->flow_lbl[0] = 0;
1766 iph->flow_lbl[1] = 0;
1767 iph->flow_lbl[2] = 0;
1768 iph->payload_len = 0;
1769 iph->nexthdr = IPPROTO_NONE;
1770 iph->hop_limit = 0;
1771 ipv6_addr_copy(&iph->saddr, &rt->rt6i_src.addr);
1772 ipv6_addr_copy(&iph->daddr, &rt->rt6i_dst.addr);
1773
Benjamin Thery8229efd2008-12-10 16:30:15 -08001774 err = ip6mr_cache_unresolved(net, vif, skb2);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001775 read_unlock(&mrt_lock);
1776
1777 return err;
1778 }
1779
1780 if (!nowait && (rtm->rtm_flags&RTM_F_NOTIFY))
1781 cache->mfc_flags |= MFC_NOTIFY;
1782
Patrick McHardyb5aa30b2010-05-11 14:40:50 +02001783 err = ip6mr_fill_mroute(net, skb, cache, rtm);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001784 read_unlock(&mrt_lock);
1785 return err;
1786}
1787