blob: 08e09042ad1c08fc5c4b8c0a361ebc51dd128cb7 [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;
Patrick McHardyf30a77842010-05-11 14:40:51 +020092 struct list_head *cache;
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +090093 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
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900102 read_lock(&mrt_lock);
Patrick McHardyf30a77842010-05-11 14:40:51 +0200103 for (it->ct = 0; it->ct < MFC6_LINES; it->ct++) {
104 it->cache = &net->ipv6.mfc6_cache_array[it->ct];
105 list_for_each_entry(mfc, it->cache, list)
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900106 if (pos-- == 0)
107 return mfc;
Patrick McHardyf30a77842010-05-11 14:40:51 +0200108 }
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900109 read_unlock(&mrt_lock);
110
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900111 spin_lock_bh(&mfc_unres_lock);
Patrick McHardyf30a77842010-05-11 14:40:51 +0200112 it->cache = &net->ipv6.mfc6_unres_queue;
113 list_for_each_entry(mfc, it->cache, list)
Patrick McHardyc476efb2010-05-11 14:40:48 +0200114 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
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900122/*
123 * The /proc interfaces to multicast routing /proc/ip6_mr_cache /proc/ip6_mr_vif
124 */
125
126struct ipmr_vif_iter {
Benjamin Thery8b90fc72008-12-10 16:29:48 -0800127 struct seq_net_private p;
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900128 int ct;
129};
130
Benjamin Thery8b90fc72008-12-10 16:29:48 -0800131static struct mif_device *ip6mr_vif_seq_idx(struct net *net,
132 struct ipmr_vif_iter *iter,
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900133 loff_t pos)
134{
Benjamin Thery8b90fc72008-12-10 16:29:48 -0800135 for (iter->ct = 0; iter->ct < net->ipv6.maxvif; ++iter->ct) {
136 if (!MIF_EXISTS(net, iter->ct))
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900137 continue;
138 if (pos-- == 0)
Benjamin Thery8b90fc72008-12-10 16:29:48 -0800139 return &net->ipv6.vif6_table[iter->ct];
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900140 }
141 return NULL;
142}
143
144static void *ip6mr_vif_seq_start(struct seq_file *seq, loff_t *pos)
145 __acquires(mrt_lock)
146{
Benjamin Thery8b90fc72008-12-10 16:29:48 -0800147 struct net *net = seq_file_net(seq);
148
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900149 read_lock(&mrt_lock);
Benjamin Thery8b90fc72008-12-10 16:29:48 -0800150 return *pos ? ip6mr_vif_seq_idx(net, seq->private, *pos - 1)
151 : SEQ_START_TOKEN;
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900152}
153
154static void *ip6mr_vif_seq_next(struct seq_file *seq, void *v, loff_t *pos)
155{
156 struct ipmr_vif_iter *iter = seq->private;
Benjamin Thery8b90fc72008-12-10 16:29:48 -0800157 struct net *net = seq_file_net(seq);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900158
159 ++*pos;
160 if (v == SEQ_START_TOKEN)
Benjamin Thery8b90fc72008-12-10 16:29:48 -0800161 return ip6mr_vif_seq_idx(net, iter, 0);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900162
Benjamin Thery8b90fc72008-12-10 16:29:48 -0800163 while (++iter->ct < net->ipv6.maxvif) {
164 if (!MIF_EXISTS(net, iter->ct))
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900165 continue;
Benjamin Thery8b90fc72008-12-10 16:29:48 -0800166 return &net->ipv6.vif6_table[iter->ct];
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900167 }
168 return NULL;
169}
170
171static void ip6mr_vif_seq_stop(struct seq_file *seq, void *v)
172 __releases(mrt_lock)
173{
174 read_unlock(&mrt_lock);
175}
176
177static int ip6mr_vif_seq_show(struct seq_file *seq, void *v)
178{
Benjamin Thery8b90fc72008-12-10 16:29:48 -0800179 struct net *net = seq_file_net(seq);
180
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900181 if (v == SEQ_START_TOKEN) {
182 seq_puts(seq,
183 "Interface BytesIn PktsIn BytesOut PktsOut Flags\n");
184 } else {
185 const struct mif_device *vif = v;
186 const char *name = vif->dev ? vif->dev->name : "none";
187
188 seq_printf(seq,
Al Virod430a222008-06-02 10:59:02 +0100189 "%2td %-10s %8ld %7ld %8ld %7ld %05X\n",
Benjamin Thery8b90fc72008-12-10 16:29:48 -0800190 vif - net->ipv6.vif6_table,
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900191 name, vif->bytes_in, vif->pkt_in,
192 vif->bytes_out, vif->pkt_out,
193 vif->flags);
194 }
195 return 0;
196}
197
Stephen Hemminger98147d52009-09-01 19:25:02 +0000198static const struct seq_operations ip6mr_vif_seq_ops = {
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900199 .start = ip6mr_vif_seq_start,
200 .next = ip6mr_vif_seq_next,
201 .stop = ip6mr_vif_seq_stop,
202 .show = ip6mr_vif_seq_show,
203};
204
205static int ip6mr_vif_open(struct inode *inode, struct file *file)
206{
Benjamin Thery8b90fc72008-12-10 16:29:48 -0800207 return seq_open_net(inode, file, &ip6mr_vif_seq_ops,
208 sizeof(struct ipmr_vif_iter));
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900209}
210
Stephen Hemminger5ca1b992009-09-01 19:25:05 +0000211static const struct file_operations ip6mr_vif_fops = {
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900212 .owner = THIS_MODULE,
213 .open = ip6mr_vif_open,
214 .read = seq_read,
215 .llseek = seq_lseek,
Benjamin Thery8b90fc72008-12-10 16:29:48 -0800216 .release = seq_release_net,
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900217};
218
219static void *ipmr_mfc_seq_start(struct seq_file *seq, loff_t *pos)
220{
Benjamin Thery8b90fc72008-12-10 16:29:48 -0800221 struct net *net = seq_file_net(seq);
222
223 return *pos ? ipmr_mfc_seq_idx(net, seq->private, *pos - 1)
224 : SEQ_START_TOKEN;
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900225}
226
227static void *ipmr_mfc_seq_next(struct seq_file *seq, void *v, loff_t *pos)
228{
229 struct mfc6_cache *mfc = v;
230 struct ipmr_mfc_iter *it = seq->private;
Benjamin Thery8b90fc72008-12-10 16:29:48 -0800231 struct net *net = seq_file_net(seq);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900232
233 ++*pos;
234
235 if (v == SEQ_START_TOKEN)
Benjamin Thery8b90fc72008-12-10 16:29:48 -0800236 return ipmr_mfc_seq_idx(net, seq->private, 0);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900237
Patrick McHardyf30a77842010-05-11 14:40:51 +0200238 if (mfc->list.next != it->cache)
239 return list_entry(mfc->list.next, struct mfc6_cache, list);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900240
Patrick McHardyc476efb2010-05-11 14:40:48 +0200241 if (it->cache == &net->ipv6.mfc6_unres_queue)
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900242 goto end_of_list;
243
Patrick McHardyf30a77842010-05-11 14:40:51 +0200244 BUG_ON(it->cache != &net->ipv6.mfc6_cache_array[it->ct]);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900245
Benjamin Thery4a6258a2008-12-10 16:24:07 -0800246 while (++it->ct < MFC6_LINES) {
Patrick McHardyf30a77842010-05-11 14:40:51 +0200247 it->cache = &net->ipv6.mfc6_cache_array[it->ct];
248 if (list_empty(it->cache))
249 continue;
250 return list_first_entry(it->cache, struct mfc6_cache, list);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900251 }
252
253 /* exhausted cache_array, show unresolved */
254 read_unlock(&mrt_lock);
Patrick McHardyc476efb2010-05-11 14:40:48 +0200255 it->cache = &net->ipv6.mfc6_unres_queue;
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900256 it->ct = 0;
257
258 spin_lock_bh(&mfc_unres_lock);
Patrick McHardyf30a77842010-05-11 14:40:51 +0200259 if (!list_empty(it->cache))
260 return list_first_entry(it->cache, struct mfc6_cache, list);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900261
262 end_of_list:
263 spin_unlock_bh(&mfc_unres_lock);
264 it->cache = NULL;
265
266 return NULL;
267}
268
269static void ipmr_mfc_seq_stop(struct seq_file *seq, void *v)
270{
271 struct ipmr_mfc_iter *it = seq->private;
Benjamin Thery8b90fc72008-12-10 16:29:48 -0800272 struct net *net = seq_file_net(seq);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900273
Patrick McHardyc476efb2010-05-11 14:40:48 +0200274 if (it->cache == &net->ipv6.mfc6_unres_queue)
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900275 spin_unlock_bh(&mfc_unres_lock);
Benjamin Thery8b90fc72008-12-10 16:29:48 -0800276 else if (it->cache == net->ipv6.mfc6_cache_array)
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900277 read_unlock(&mrt_lock);
278}
279
280static int ipmr_mfc_seq_show(struct seq_file *seq, void *v)
281{
282 int n;
Benjamin Thery8b90fc72008-12-10 16:29:48 -0800283 struct net *net = seq_file_net(seq);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900284
285 if (v == SEQ_START_TOKEN) {
286 seq_puts(seq,
287 "Group "
288 "Origin "
289 "Iif Pkts Bytes Wrong Oifs\n");
290 } else {
291 const struct mfc6_cache *mfc = v;
292 const struct ipmr_mfc_iter *it = seq->private;
293
Benjamin Thery999890b2008-12-03 22:22:16 -0800294 seq_printf(seq, "%pI6 %pI6 %-3hd",
Harvey Harrison0c6ce782008-10-28 16:09:23 -0700295 &mfc->mf6c_mcastgrp, &mfc->mf6c_origin,
Benjamin Thery1ea472e2008-12-03 22:21:47 -0800296 mfc->mf6c_parent);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900297
Patrick McHardyc476efb2010-05-11 14:40:48 +0200298 if (it->cache != &net->ipv6.mfc6_unres_queue) {
Benjamin Thery1ea472e2008-12-03 22:21:47 -0800299 seq_printf(seq, " %8lu %8lu %8lu",
300 mfc->mfc_un.res.pkt,
301 mfc->mfc_un.res.bytes,
302 mfc->mfc_un.res.wrong_if);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900303 for (n = mfc->mfc_un.res.minvif;
304 n < mfc->mfc_un.res.maxvif; n++) {
Benjamin Thery8b90fc72008-12-10 16:29:48 -0800305 if (MIF_EXISTS(net, n) &&
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900306 mfc->mfc_un.res.ttls[n] < 255)
307 seq_printf(seq,
308 " %2d:%-3d",
309 n, mfc->mfc_un.res.ttls[n]);
310 }
Benjamin Thery1ea472e2008-12-03 22:21:47 -0800311 } else {
312 /* unresolved mfc_caches don't contain
313 * pkt, bytes and wrong_if values
314 */
315 seq_printf(seq, " %8lu %8lu %8lu", 0ul, 0ul, 0ul);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900316 }
317 seq_putc(seq, '\n');
318 }
319 return 0;
320}
321
James Morris88e9d342009-09-22 16:43:43 -0700322static const struct seq_operations ipmr_mfc_seq_ops = {
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900323 .start = ipmr_mfc_seq_start,
324 .next = ipmr_mfc_seq_next,
325 .stop = ipmr_mfc_seq_stop,
326 .show = ipmr_mfc_seq_show,
327};
328
329static int ipmr_mfc_open(struct inode *inode, struct file *file)
330{
Benjamin Thery8b90fc72008-12-10 16:29:48 -0800331 return seq_open_net(inode, file, &ipmr_mfc_seq_ops,
332 sizeof(struct ipmr_mfc_iter));
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900333}
334
Stephen Hemminger5ca1b992009-09-01 19:25:05 +0000335static const struct file_operations ip6mr_mfc_fops = {
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900336 .owner = THIS_MODULE,
337 .open = ipmr_mfc_open,
338 .read = seq_read,
339 .llseek = seq_lseek,
Benjamin Thery8b90fc72008-12-10 16:29:48 -0800340 .release = seq_release_net,
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900341};
342#endif
343
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +0900344#ifdef CONFIG_IPV6_PIMSM_V2
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +0900345
346static int pim6_rcv(struct sk_buff *skb)
347{
348 struct pimreghdr *pim;
349 struct ipv6hdr *encap;
350 struct net_device *reg_dev = NULL;
Benjamin Thery8229efd2008-12-10 16:30:15 -0800351 struct net *net = dev_net(skb->dev);
352 int reg_vif_num = net->ipv6.mroute_reg_vif_num;
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +0900353
354 if (!pskb_may_pull(skb, sizeof(*pim) + sizeof(*encap)))
355 goto drop;
356
357 pim = (struct pimreghdr *)skb_transport_header(skb);
358 if (pim->type != ((PIM_VERSION << 4) | PIM_REGISTER) ||
359 (pim->flags & PIM_NULL_REGISTER) ||
Thomas Goff1d6e55f2009-01-27 22:39:59 -0800360 (csum_ipv6_magic(&ipv6_hdr(skb)->saddr, &ipv6_hdr(skb)->daddr,
361 sizeof(*pim), IPPROTO_PIM,
362 csum_partial((void *)pim, sizeof(*pim), 0)) &&
Al Viroec6b4862008-04-26 22:28:58 -0700363 csum_fold(skb_checksum(skb, 0, skb->len, 0))))
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +0900364 goto drop;
365
366 /* check if the inner packet is destined to mcast group */
367 encap = (struct ipv6hdr *)(skb_transport_header(skb) +
368 sizeof(*pim));
369
370 if (!ipv6_addr_is_multicast(&encap->daddr) ||
371 encap->payload_len == 0 ||
372 ntohs(encap->payload_len) + sizeof(*pim) > skb->len)
373 goto drop;
374
375 read_lock(&mrt_lock);
376 if (reg_vif_num >= 0)
Benjamin Thery8229efd2008-12-10 16:30:15 -0800377 reg_dev = net->ipv6.vif6_table[reg_vif_num].dev;
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +0900378 if (reg_dev)
379 dev_hold(reg_dev);
380 read_unlock(&mrt_lock);
381
382 if (reg_dev == NULL)
383 goto drop;
384
385 skb->mac_header = skb->network_header;
386 skb_pull(skb, (u8 *)encap - skb->data);
387 skb_reset_network_header(skb);
388 skb->dev = reg_dev;
Thomas Goff1d6e55f2009-01-27 22:39:59 -0800389 skb->protocol = htons(ETH_P_IPV6);
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +0900390 skb->ip_summed = 0;
391 skb->pkt_type = PACKET_HOST;
Eric Dumazetadf30902009-06-02 05:19:30 +0000392 skb_dst_drop(skb);
Pavel Emelyanovdc58c782008-05-21 14:17:54 -0700393 reg_dev->stats.rx_bytes += skb->len;
394 reg_dev->stats.rx_packets++;
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +0900395 nf_reset(skb);
396 netif_rx(skb);
397 dev_put(reg_dev);
398 return 0;
399 drop:
400 kfree_skb(skb);
401 return 0;
402}
403
Alexey Dobriyan41135cc2009-09-14 12:22:28 +0000404static const struct inet6_protocol pim6_protocol = {
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +0900405 .handler = pim6_rcv,
406};
407
408/* Service routines creating virtual interfaces: PIMREG */
409
Stephen Hemminger6fef4c02009-08-31 19:50:41 +0000410static netdev_tx_t reg_vif_xmit(struct sk_buff *skb,
411 struct net_device *dev)
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +0900412{
Benjamin Thery8229efd2008-12-10 16:30:15 -0800413 struct net *net = dev_net(dev);
414
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +0900415 read_lock(&mrt_lock);
Pavel Emelyanovdc58c782008-05-21 14:17:54 -0700416 dev->stats.tx_bytes += skb->len;
417 dev->stats.tx_packets++;
Benjamin Thery8229efd2008-12-10 16:30:15 -0800418 ip6mr_cache_report(net, skb, net->ipv6.mroute_reg_vif_num,
419 MRT6MSG_WHOLEPKT);
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +0900420 read_unlock(&mrt_lock);
421 kfree_skb(skb);
Patrick McHardy6ed10652009-06-23 06:03:08 +0000422 return NETDEV_TX_OK;
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +0900423}
424
Stephen Hemminger007c3832008-11-20 20:28:35 -0800425static const struct net_device_ops reg_vif_netdev_ops = {
426 .ndo_start_xmit = reg_vif_xmit,
427};
428
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +0900429static void reg_vif_setup(struct net_device *dev)
430{
431 dev->type = ARPHRD_PIMREG;
432 dev->mtu = 1500 - sizeof(struct ipv6hdr) - 8;
433 dev->flags = IFF_NOARP;
Stephen Hemminger007c3832008-11-20 20:28:35 -0800434 dev->netdev_ops = &reg_vif_netdev_ops;
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +0900435 dev->destructor = free_netdev;
Tom Goff403dbb92009-06-14 03:16:13 -0700436 dev->features |= NETIF_F_NETNS_LOCAL;
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +0900437}
438
Benjamin Thery8229efd2008-12-10 16:30:15 -0800439static struct net_device *ip6mr_reg_vif(struct net *net)
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +0900440{
441 struct net_device *dev;
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +0900442
Pavel Emelyanovdc58c782008-05-21 14:17:54 -0700443 dev = alloc_netdev(0, "pim6reg", reg_vif_setup);
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +0900444 if (dev == NULL)
445 return NULL;
446
Benjamin Thery8229efd2008-12-10 16:30:15 -0800447 dev_net_set(dev, net);
448
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +0900449 if (register_netdevice(dev)) {
450 free_netdev(dev);
451 return NULL;
452 }
453 dev->iflink = 0;
454
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +0900455 if (dev_open(dev))
456 goto failure;
457
Wang Chen7af3db72008-07-14 20:54:54 -0700458 dev_hold(dev);
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +0900459 return dev;
460
461failure:
462 /* allow the register to be completed before unregistering. */
463 rtnl_unlock();
464 rtnl_lock();
465
466 unregister_netdevice(dev);
467 return NULL;
468}
469#endif
470
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900471/*
472 * Delete a VIF entry
473 */
474
Eric Dumazetc871e662009-10-28 04:48:11 +0000475static int mif6_delete(struct net *net, int vifi, struct list_head *head)
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900476{
477 struct mif_device *v;
478 struct net_device *dev;
Thomas Goff1d6e55f2009-01-27 22:39:59 -0800479 struct inet6_dev *in6_dev;
Benjamin Thery8229efd2008-12-10 16:30:15 -0800480 if (vifi < 0 || vifi >= net->ipv6.maxvif)
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900481 return -EADDRNOTAVAIL;
482
Benjamin Thery8229efd2008-12-10 16:30:15 -0800483 v = &net->ipv6.vif6_table[vifi];
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900484
485 write_lock_bh(&mrt_lock);
486 dev = v->dev;
487 v->dev = NULL;
488
489 if (!dev) {
490 write_unlock_bh(&mrt_lock);
491 return -EADDRNOTAVAIL;
492 }
493
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +0900494#ifdef CONFIG_IPV6_PIMSM_V2
Benjamin Thery8229efd2008-12-10 16:30:15 -0800495 if (vifi == net->ipv6.mroute_reg_vif_num)
496 net->ipv6.mroute_reg_vif_num = -1;
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +0900497#endif
498
Benjamin Thery8229efd2008-12-10 16:30:15 -0800499 if (vifi + 1 == net->ipv6.maxvif) {
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900500 int tmp;
501 for (tmp = vifi - 1; tmp >= 0; tmp--) {
Benjamin Thery8229efd2008-12-10 16:30:15 -0800502 if (MIF_EXISTS(net, tmp))
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900503 break;
504 }
Benjamin Thery8229efd2008-12-10 16:30:15 -0800505 net->ipv6.maxvif = tmp + 1;
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900506 }
507
508 write_unlock_bh(&mrt_lock);
509
510 dev_set_allmulti(dev, -1);
511
Thomas Goff1d6e55f2009-01-27 22:39:59 -0800512 in6_dev = __in6_dev_get(dev);
513 if (in6_dev)
514 in6_dev->cnf.mc_forwarding--;
515
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900516 if (v->flags & MIFF_REGISTER)
Eric Dumazetc871e662009-10-28 04:48:11 +0000517 unregister_netdevice_queue(dev, head);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900518
519 dev_put(dev);
520 return 0;
521}
522
Benjamin Thery58701ad2008-12-10 16:22:34 -0800523static inline void ip6mr_cache_free(struct mfc6_cache *c)
524{
Benjamin Thery58701ad2008-12-10 16:22:34 -0800525 kmem_cache_free(mrt_cachep, c);
526}
527
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900528/* Destroy an unresolved cache entry, killing queued skbs
529 and reporting error to netlink readers.
530 */
531
Patrick McHardyb5aa30b2010-05-11 14:40:50 +0200532static void ip6mr_destroy_unres(struct net *net, struct mfc6_cache *c)
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900533{
534 struct sk_buff *skb;
535
Benjamin Thery8229efd2008-12-10 16:30:15 -0800536 atomic_dec(&net->ipv6.cache_resolve_queue_len);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900537
538 while((skb = skb_dequeue(&c->mfc_un.unres.unresolved)) != NULL) {
539 if (ipv6_hdr(skb)->version == 0) {
540 struct nlmsghdr *nlh = (struct nlmsghdr *)skb_pull(skb, sizeof(struct ipv6hdr));
541 nlh->nlmsg_type = NLMSG_ERROR;
542 nlh->nlmsg_len = NLMSG_LENGTH(sizeof(struct nlmsgerr));
543 skb_trim(skb, nlh->nlmsg_len);
544 ((struct nlmsgerr *)NLMSG_DATA(nlh))->error = -ETIMEDOUT;
Benjamin Thery8229efd2008-12-10 16:30:15 -0800545 rtnl_unicast(skb, net, NETLINK_CB(skb).pid);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900546 } else
547 kfree_skb(skb);
548 }
549
Benjamin Thery58701ad2008-12-10 16:22:34 -0800550 ip6mr_cache_free(c);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900551}
552
553
Patrick McHardyc476efb2010-05-11 14:40:48 +0200554/* Timer process for all the unresolved queue. */
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900555
Patrick McHardyc476efb2010-05-11 14:40:48 +0200556static void ipmr_do_expire_process(struct net *net)
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900557{
558 unsigned long now = jiffies;
559 unsigned long expires = 10 * HZ;
Patrick McHardyf30a77842010-05-11 14:40:51 +0200560 struct mfc6_cache *c, *next;
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900561
Patrick McHardyf30a77842010-05-11 14:40:51 +0200562 list_for_each_entry_safe(c, next, &net->ipv6.mfc6_unres_queue, list) {
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900563 if (time_after(c->mfc_un.unres.expires, now)) {
564 /* not yet... */
565 unsigned long interval = c->mfc_un.unres.expires - now;
566 if (interval < expires)
567 expires = interval;
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900568 continue;
569 }
570
Patrick McHardyf30a77842010-05-11 14:40:51 +0200571 list_del(&c->list);
Patrick McHardyb5aa30b2010-05-11 14:40:50 +0200572 ip6mr_destroy_unres(net, c);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900573 }
574
Patrick McHardyf30a77842010-05-11 14:40:51 +0200575 if (!list_empty(&net->ipv6.mfc6_unres_queue))
Patrick McHardyc476efb2010-05-11 14:40:48 +0200576 mod_timer(&net->ipv6.ipmr_expire_timer, jiffies + expires);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900577}
578
Patrick McHardyc476efb2010-05-11 14:40:48 +0200579static void ipmr_expire_process(unsigned long arg)
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900580{
Patrick McHardyc476efb2010-05-11 14:40:48 +0200581 struct net *net = (struct net *)arg;
582
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900583 if (!spin_trylock(&mfc_unres_lock)) {
Patrick McHardyc476efb2010-05-11 14:40:48 +0200584 mod_timer(&net->ipv6.ipmr_expire_timer, jiffies + 1);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900585 return;
586 }
587
Patrick McHardyf30a77842010-05-11 14:40:51 +0200588 if (!list_empty(&net->ipv6.mfc6_unres_queue))
Patrick McHardyc476efb2010-05-11 14:40:48 +0200589 ipmr_do_expire_process(net);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900590
591 spin_unlock(&mfc_unres_lock);
592}
593
594/* Fill oifs list. It is called under write locked mrt_lock. */
595
Patrick McHardyb5aa30b2010-05-11 14:40:50 +0200596static void ip6mr_update_thresholds(struct net *net, struct mfc6_cache *cache,
597 unsigned char *ttls)
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900598{
599 int vifi;
600
Rami Rosen6ac7eb02008-04-10 12:40:10 +0300601 cache->mfc_un.res.minvif = MAXMIFS;
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900602 cache->mfc_un.res.maxvif = 0;
Rami Rosen6ac7eb02008-04-10 12:40:10 +0300603 memset(cache->mfc_un.res.ttls, 255, MAXMIFS);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900604
Benjamin Thery8229efd2008-12-10 16:30:15 -0800605 for (vifi = 0; vifi < net->ipv6.maxvif; vifi++) {
606 if (MIF_EXISTS(net, vifi) &&
Benjamin Thery4e168802008-12-10 16:15:08 -0800607 ttls[vifi] && ttls[vifi] < 255) {
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900608 cache->mfc_un.res.ttls[vifi] = ttls[vifi];
609 if (cache->mfc_un.res.minvif > vifi)
610 cache->mfc_un.res.minvif = vifi;
611 if (cache->mfc_un.res.maxvif <= vifi)
612 cache->mfc_un.res.maxvif = vifi + 1;
613 }
614 }
615}
616
Benjamin Thery8229efd2008-12-10 16:30:15 -0800617static int mif6_add(struct net *net, struct mif6ctl *vifc, int mrtsock)
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900618{
619 int vifi = vifc->mif6c_mifi;
Benjamin Thery8229efd2008-12-10 16:30:15 -0800620 struct mif_device *v = &net->ipv6.vif6_table[vifi];
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900621 struct net_device *dev;
Thomas Goff1d6e55f2009-01-27 22:39:59 -0800622 struct inet6_dev *in6_dev;
Wang Chen5ae7b442008-07-14 20:54:23 -0700623 int err;
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900624
625 /* Is vif busy ? */
Benjamin Thery8229efd2008-12-10 16:30:15 -0800626 if (MIF_EXISTS(net, vifi))
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900627 return -EADDRINUSE;
628
629 switch (vifc->mif6c_flags) {
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +0900630#ifdef CONFIG_IPV6_PIMSM_V2
631 case MIFF_REGISTER:
632 /*
633 * Special Purpose VIF in PIM
634 * All the packets will be sent to the daemon
635 */
Benjamin Thery8229efd2008-12-10 16:30:15 -0800636 if (net->ipv6.mroute_reg_vif_num >= 0)
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +0900637 return -EADDRINUSE;
Benjamin Thery8229efd2008-12-10 16:30:15 -0800638 dev = ip6mr_reg_vif(net);
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +0900639 if (!dev)
640 return -ENOBUFS;
Wang Chen5ae7b442008-07-14 20:54:23 -0700641 err = dev_set_allmulti(dev, 1);
642 if (err) {
643 unregister_netdevice(dev);
Wang Chen7af3db72008-07-14 20:54:54 -0700644 dev_put(dev);
Wang Chen5ae7b442008-07-14 20:54:23 -0700645 return err;
646 }
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +0900647 break;
648#endif
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900649 case 0:
Benjamin Thery8229efd2008-12-10 16:30:15 -0800650 dev = dev_get_by_index(net, vifc->mif6c_pifi);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900651 if (!dev)
652 return -EADDRNOTAVAIL;
Wang Chen5ae7b442008-07-14 20:54:23 -0700653 err = dev_set_allmulti(dev, 1);
Wang Chen7af3db72008-07-14 20:54:54 -0700654 if (err) {
655 dev_put(dev);
Wang Chen5ae7b442008-07-14 20:54:23 -0700656 return err;
Wang Chen7af3db72008-07-14 20:54:54 -0700657 }
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900658 break;
659 default:
660 return -EINVAL;
661 }
662
Thomas Goff1d6e55f2009-01-27 22:39:59 -0800663 in6_dev = __in6_dev_get(dev);
664 if (in6_dev)
665 in6_dev->cnf.mc_forwarding++;
666
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900667 /*
668 * Fill in the VIF structures
669 */
670 v->rate_limit = vifc->vifc_rate_limit;
671 v->flags = vifc->mif6c_flags;
672 if (!mrtsock)
673 v->flags |= VIFF_STATIC;
674 v->threshold = vifc->vifc_threshold;
675 v->bytes_in = 0;
676 v->bytes_out = 0;
677 v->pkt_in = 0;
678 v->pkt_out = 0;
679 v->link = dev->ifindex;
680 if (v->flags & MIFF_REGISTER)
681 v->link = dev->iflink;
682
683 /* And finish update writing critical data */
684 write_lock_bh(&mrt_lock);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900685 v->dev = dev;
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +0900686#ifdef CONFIG_IPV6_PIMSM_V2
687 if (v->flags & MIFF_REGISTER)
Benjamin Thery8229efd2008-12-10 16:30:15 -0800688 net->ipv6.mroute_reg_vif_num = vifi;
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +0900689#endif
Benjamin Thery8229efd2008-12-10 16:30:15 -0800690 if (vifi + 1 > net->ipv6.maxvif)
691 net->ipv6.maxvif = vifi + 1;
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900692 write_unlock_bh(&mrt_lock);
693 return 0;
694}
695
Benjamin Thery8229efd2008-12-10 16:30:15 -0800696static struct mfc6_cache *ip6mr_cache_find(struct net *net,
697 struct in6_addr *origin,
698 struct in6_addr *mcastgrp)
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900699{
700 int line = MFC6_HASH(mcastgrp, origin);
701 struct mfc6_cache *c;
702
Patrick McHardyf30a77842010-05-11 14:40:51 +0200703 list_for_each_entry(c, &net->ipv6.mfc6_cache_array[line], list) {
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900704 if (ipv6_addr_equal(&c->mf6c_origin, origin) &&
705 ipv6_addr_equal(&c->mf6c_mcastgrp, mcastgrp))
Patrick McHardyf30a77842010-05-11 14:40:51 +0200706 return c;
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900707 }
Patrick McHardyf30a77842010-05-11 14:40:51 +0200708 return NULL;
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900709}
710
711/*
712 * Allocate a multicast cache entry
713 */
Patrick McHardyb5aa30b2010-05-11 14:40:50 +0200714static struct mfc6_cache *ip6mr_cache_alloc(void)
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900715{
Joe Perches36cbac52008-12-03 22:27:25 -0800716 struct mfc6_cache *c = kmem_cache_zalloc(mrt_cachep, GFP_KERNEL);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900717 if (c == NULL)
718 return NULL;
Rami Rosen6ac7eb02008-04-10 12:40:10 +0300719 c->mfc_un.res.minvif = MAXMIFS;
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900720 return c;
721}
722
Patrick McHardyb5aa30b2010-05-11 14:40:50 +0200723static struct mfc6_cache *ip6mr_cache_alloc_unres(void)
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900724{
Joe Perches36cbac52008-12-03 22:27:25 -0800725 struct mfc6_cache *c = kmem_cache_zalloc(mrt_cachep, GFP_ATOMIC);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900726 if (c == NULL)
727 return NULL;
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900728 skb_queue_head_init(&c->mfc_un.unres.unresolved);
729 c->mfc_un.unres.expires = jiffies + 10 * HZ;
730 return c;
731}
732
733/*
734 * A cache entry has gone into a resolved state from queued
735 */
736
Patrick McHardyb5aa30b2010-05-11 14:40:50 +0200737static void ip6mr_cache_resolve(struct net *net, struct mfc6_cache *uc,
738 struct mfc6_cache *c)
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900739{
740 struct sk_buff *skb;
741
742 /*
743 * Play the pending entries through our router
744 */
745
746 while((skb = __skb_dequeue(&uc->mfc_un.unres.unresolved))) {
747 if (ipv6_hdr(skb)->version == 0) {
748 int err;
749 struct nlmsghdr *nlh = (struct nlmsghdr *)skb_pull(skb, sizeof(struct ipv6hdr));
750
Patrick McHardyb5aa30b2010-05-11 14:40:50 +0200751 if (ip6mr_fill_mroute(net, skb, c, NLMSG_DATA(nlh)) > 0) {
YOSHIFUJI Hideaki549e0282008-04-05 22:17:39 +0900752 nlh->nlmsg_len = skb_tail_pointer(skb) - (u8 *)nlh;
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900753 } else {
754 nlh->nlmsg_type = NLMSG_ERROR;
755 nlh->nlmsg_len = NLMSG_LENGTH(sizeof(struct nlmsgerr));
756 skb_trim(skb, nlh->nlmsg_len);
757 ((struct nlmsgerr *)NLMSG_DATA(nlh))->error = -EMSGSIZE;
758 }
Patrick McHardyb5aa30b2010-05-11 14:40:50 +0200759 err = rtnl_unicast(skb, net, NETLINK_CB(skb).pid);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900760 } else
Patrick McHardyb5aa30b2010-05-11 14:40:50 +0200761 ip6_mr_forward(net, skb, c);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900762 }
763}
764
765/*
766 * Bounce a cache query up to pim6sd. We could use netlink for this but pim6sd
767 * expects the following bizarre scheme.
768 *
769 * Called under mrt_lock.
770 */
771
Benjamin Thery8229efd2008-12-10 16:30:15 -0800772static int ip6mr_cache_report(struct net *net, struct sk_buff *pkt, mifi_t mifi,
773 int assert)
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900774{
775 struct sk_buff *skb;
776 struct mrt6msg *msg;
777 int ret;
778
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +0900779#ifdef CONFIG_IPV6_PIMSM_V2
780 if (assert == MRT6MSG_WHOLEPKT)
781 skb = skb_realloc_headroom(pkt, -skb_network_offset(pkt)
782 +sizeof(*msg));
783 else
784#endif
785 skb = alloc_skb(sizeof(struct ipv6hdr) + sizeof(*msg), GFP_ATOMIC);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900786
787 if (!skb)
788 return -ENOBUFS;
789
790 /* I suppose that internal messages
791 * do not require checksums */
792
793 skb->ip_summed = CHECKSUM_UNNECESSARY;
794
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +0900795#ifdef CONFIG_IPV6_PIMSM_V2
796 if (assert == MRT6MSG_WHOLEPKT) {
797 /* Ugly, but we have no choice with this interface.
798 Duplicate old header, fix length etc.
799 And all this only to mangle msg->im6_msgtype and
800 to set msg->im6_mbz to "mbz" :-)
801 */
802 skb_push(skb, -skb_network_offset(pkt));
803
804 skb_push(skb, sizeof(*msg));
805 skb_reset_transport_header(skb);
806 msg = (struct mrt6msg *)skb_transport_header(skb);
807 msg->im6_mbz = 0;
808 msg->im6_msgtype = MRT6MSG_WHOLEPKT;
Benjamin Thery8229efd2008-12-10 16:30:15 -0800809 msg->im6_mif = net->ipv6.mroute_reg_vif_num;
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +0900810 msg->im6_pad = 0;
811 ipv6_addr_copy(&msg->im6_src, &ipv6_hdr(pkt)->saddr);
812 ipv6_addr_copy(&msg->im6_dst, &ipv6_hdr(pkt)->daddr);
813
814 skb->ip_summed = CHECKSUM_UNNECESSARY;
815 } else
816#endif
817 {
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900818 /*
819 * Copy the IP header
820 */
821
822 skb_put(skb, sizeof(struct ipv6hdr));
823 skb_reset_network_header(skb);
824 skb_copy_to_linear_data(skb, ipv6_hdr(pkt), sizeof(struct ipv6hdr));
825
826 /*
827 * Add our header
828 */
829 skb_put(skb, sizeof(*msg));
830 skb_reset_transport_header(skb);
831 msg = (struct mrt6msg *)skb_transport_header(skb);
832
833 msg->im6_mbz = 0;
834 msg->im6_msgtype = assert;
Rami Rosen6ac7eb02008-04-10 12:40:10 +0300835 msg->im6_mif = mifi;
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900836 msg->im6_pad = 0;
837 ipv6_addr_copy(&msg->im6_src, &ipv6_hdr(pkt)->saddr);
838 ipv6_addr_copy(&msg->im6_dst, &ipv6_hdr(pkt)->daddr);
839
Eric Dumazetadf30902009-06-02 05:19:30 +0000840 skb_dst_set(skb, dst_clone(skb_dst(pkt)));
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900841 skb->ip_summed = CHECKSUM_UNNECESSARY;
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +0900842 }
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900843
Benjamin Thery8229efd2008-12-10 16:30:15 -0800844 if (net->ipv6.mroute6_sk == NULL) {
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900845 kfree_skb(skb);
846 return -EINVAL;
847 }
848
849 /*
850 * Deliver to user space multicast routing algorithms
851 */
Benjamin Thery8229efd2008-12-10 16:30:15 -0800852 ret = sock_queue_rcv_skb(net->ipv6.mroute6_sk, skb);
Benjamin Therybd91b8b2008-12-10 16:07:08 -0800853 if (ret < 0) {
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900854 if (net_ratelimit())
855 printk(KERN_WARNING "mroute6: pending queue full, dropping entries.\n");
856 kfree_skb(skb);
857 }
858
859 return ret;
860}
861
862/*
863 * Queue a packet for resolution. It gets locked cache entry!
864 */
865
866static int
Benjamin Thery8229efd2008-12-10 16:30:15 -0800867ip6mr_cache_unresolved(struct net *net, mifi_t mifi, struct sk_buff *skb)
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900868{
Patrick McHardyf30a77842010-05-11 14:40:51 +0200869 bool found = false;
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900870 int err;
871 struct mfc6_cache *c;
872
873 spin_lock_bh(&mfc_unres_lock);
Patrick McHardyf30a77842010-05-11 14:40:51 +0200874 list_for_each_entry(c, &net->ipv6.mfc6_unres_queue, list) {
Patrick McHardyc476efb2010-05-11 14:40:48 +0200875 if (ipv6_addr_equal(&c->mf6c_mcastgrp, &ipv6_hdr(skb)->daddr) &&
Patrick McHardyf30a77842010-05-11 14:40:51 +0200876 ipv6_addr_equal(&c->mf6c_origin, &ipv6_hdr(skb)->saddr)) {
877 found = true;
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900878 break;
Patrick McHardyf30a77842010-05-11 14:40:51 +0200879 }
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900880 }
881
Patrick McHardyf30a77842010-05-11 14:40:51 +0200882 if (!found) {
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900883 /*
884 * Create a new entry if allowable
885 */
886
Benjamin Thery8229efd2008-12-10 16:30:15 -0800887 if (atomic_read(&net->ipv6.cache_resolve_queue_len) >= 10 ||
Patrick McHardyb5aa30b2010-05-11 14:40:50 +0200888 (c = ip6mr_cache_alloc_unres()) == NULL) {
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900889 spin_unlock_bh(&mfc_unres_lock);
890
891 kfree_skb(skb);
892 return -ENOBUFS;
893 }
894
895 /*
896 * Fill in the new cache entry
897 */
898 c->mf6c_parent = -1;
899 c->mf6c_origin = ipv6_hdr(skb)->saddr;
900 c->mf6c_mcastgrp = ipv6_hdr(skb)->daddr;
901
902 /*
903 * Reflect first query at pim6sd
904 */
Benjamin Thery8229efd2008-12-10 16:30:15 -0800905 err = ip6mr_cache_report(net, skb, mifi, MRT6MSG_NOCACHE);
906 if (err < 0) {
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900907 /* If the report failed throw the cache entry
908 out - Brad Parker
909 */
910 spin_unlock_bh(&mfc_unres_lock);
911
Benjamin Thery58701ad2008-12-10 16:22:34 -0800912 ip6mr_cache_free(c);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900913 kfree_skb(skb);
914 return err;
915 }
916
Benjamin Thery8229efd2008-12-10 16:30:15 -0800917 atomic_inc(&net->ipv6.cache_resolve_queue_len);
Patrick McHardyf30a77842010-05-11 14:40:51 +0200918 list_add(&c->list, &net->ipv6.mfc6_unres_queue);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900919
Patrick McHardyc476efb2010-05-11 14:40:48 +0200920 ipmr_do_expire_process(net);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900921 }
922
923 /*
924 * See if we can append the packet
925 */
926 if (c->mfc_un.unres.unresolved.qlen > 3) {
927 kfree_skb(skb);
928 err = -ENOBUFS;
929 } else {
930 skb_queue_tail(&c->mfc_un.unres.unresolved, skb);
931 err = 0;
932 }
933
934 spin_unlock_bh(&mfc_unres_lock);
935 return err;
936}
937
938/*
939 * MFC6 cache manipulation by user space
940 */
941
Benjamin Thery8229efd2008-12-10 16:30:15 -0800942static int ip6mr_mfc_delete(struct net *net, struct mf6cctl *mfc)
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900943{
944 int line;
Patrick McHardyf30a77842010-05-11 14:40:51 +0200945 struct mfc6_cache *c, *next;
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900946
947 line = MFC6_HASH(&mfc->mf6cc_mcastgrp.sin6_addr, &mfc->mf6cc_origin.sin6_addr);
948
Patrick McHardyf30a77842010-05-11 14:40:51 +0200949 list_for_each_entry_safe(c, next, &net->ipv6.mfc6_cache_array[line], list) {
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900950 if (ipv6_addr_equal(&c->mf6c_origin, &mfc->mf6cc_origin.sin6_addr) &&
951 ipv6_addr_equal(&c->mf6c_mcastgrp, &mfc->mf6cc_mcastgrp.sin6_addr)) {
952 write_lock_bh(&mrt_lock);
Patrick McHardyf30a77842010-05-11 14:40:51 +0200953 list_del(&c->list);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900954 write_unlock_bh(&mrt_lock);
955
Benjamin Thery58701ad2008-12-10 16:22:34 -0800956 ip6mr_cache_free(c);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900957 return 0;
958 }
959 }
960 return -ENOENT;
961}
962
963static int ip6mr_device_event(struct notifier_block *this,
964 unsigned long event, void *ptr)
965{
966 struct net_device *dev = ptr;
Benjamin Thery8229efd2008-12-10 16:30:15 -0800967 struct net *net = dev_net(dev);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900968 struct mif_device *v;
969 int ct;
Eric Dumazetc871e662009-10-28 04:48:11 +0000970 LIST_HEAD(list);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900971
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900972 if (event != NETDEV_UNREGISTER)
973 return NOTIFY_DONE;
974
Benjamin Thery8229efd2008-12-10 16:30:15 -0800975 v = &net->ipv6.vif6_table[0];
976 for (ct = 0; ct < net->ipv6.maxvif; ct++, v++) {
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900977 if (v->dev == dev)
Eric Dumazetc871e662009-10-28 04:48:11 +0000978 mif6_delete(net, ct, &list);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900979 }
Eric Dumazetc871e662009-10-28 04:48:11 +0000980 unregister_netdevice_many(&list);
981
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900982 return NOTIFY_DONE;
983}
984
985static struct notifier_block ip6_mr_notifier = {
986 .notifier_call = ip6mr_device_event
987};
988
989/*
990 * Setup for IP multicast routing
991 */
992
Benjamin Thery4e168802008-12-10 16:15:08 -0800993static int __net_init ip6mr_net_init(struct net *net)
994{
Patrick McHardyf30a77842010-05-11 14:40:51 +0200995 unsigned int i;
Benjamin Thery4e168802008-12-10 16:15:08 -0800996 int err = 0;
Patrick McHardyf30a77842010-05-11 14:40:51 +0200997
Benjamin Thery4e168802008-12-10 16:15:08 -0800998 net->ipv6.vif6_table = kcalloc(MAXMIFS, sizeof(struct mif_device),
999 GFP_KERNEL);
1000 if (!net->ipv6.vif6_table) {
1001 err = -ENOMEM;
1002 goto fail;
1003 }
Benjamin Thery4a6258a2008-12-10 16:24:07 -08001004
1005 /* Forwarding cache */
1006 net->ipv6.mfc6_cache_array = kcalloc(MFC6_LINES,
Patrick McHardyf30a77842010-05-11 14:40:51 +02001007 sizeof(struct list_head),
Benjamin Thery4a6258a2008-12-10 16:24:07 -08001008 GFP_KERNEL);
1009 if (!net->ipv6.mfc6_cache_array) {
1010 err = -ENOMEM;
1011 goto fail_mfc6_cache;
1012 }
Benjamin Thery950d5702008-12-10 16:29:24 -08001013
Patrick McHardyf30a77842010-05-11 14:40:51 +02001014 for (i = 0; i < MFC6_LINES; i++)
1015 INIT_LIST_HEAD(&net->ipv6.mfc6_cache_array[i]);
1016
1017 INIT_LIST_HEAD(&net->ipv6.mfc6_unres_queue);
1018
Patrick McHardyc476efb2010-05-11 14:40:48 +02001019 setup_timer(&net->ipv6.ipmr_expire_timer, ipmr_expire_process,
1020 (unsigned long)net);
1021
Benjamin Thery950d5702008-12-10 16:29:24 -08001022#ifdef CONFIG_IPV6_PIMSM_V2
1023 net->ipv6.mroute_reg_vif_num = -1;
1024#endif
Benjamin Thery8b90fc72008-12-10 16:29:48 -08001025
1026#ifdef CONFIG_PROC_FS
1027 err = -ENOMEM;
1028 if (!proc_net_fops_create(net, "ip6_mr_vif", 0, &ip6mr_vif_fops))
1029 goto proc_vif_fail;
1030 if (!proc_net_fops_create(net, "ip6_mr_cache", 0, &ip6mr_mfc_fops))
1031 goto proc_cache_fail;
1032#endif
Benjamin Thery4a6258a2008-12-10 16:24:07 -08001033 return 0;
1034
Benjamin Thery8b90fc72008-12-10 16:29:48 -08001035#ifdef CONFIG_PROC_FS
1036proc_cache_fail:
1037 proc_net_remove(net, "ip6_mr_vif");
1038proc_vif_fail:
1039 kfree(net->ipv6.mfc6_cache_array);
1040#endif
Benjamin Thery4a6258a2008-12-10 16:24:07 -08001041fail_mfc6_cache:
1042 kfree(net->ipv6.vif6_table);
Benjamin Thery4e168802008-12-10 16:15:08 -08001043fail:
1044 return err;
1045}
1046
1047static void __net_exit ip6mr_net_exit(struct net *net)
1048{
Benjamin Thery8b90fc72008-12-10 16:29:48 -08001049#ifdef CONFIG_PROC_FS
1050 proc_net_remove(net, "ip6_mr_cache");
1051 proc_net_remove(net, "ip6_mr_vif");
1052#endif
Patrick McHardyc476efb2010-05-11 14:40:48 +02001053 del_timer(&net->ipv6.ipmr_expire_timer);
Benjamin Thery8229efd2008-12-10 16:30:15 -08001054 mroute_clean_tables(net);
Benjamin Thery4a6258a2008-12-10 16:24:07 -08001055 kfree(net->ipv6.mfc6_cache_array);
Benjamin Thery4e168802008-12-10 16:15:08 -08001056 kfree(net->ipv6.vif6_table);
1057}
1058
1059static struct pernet_operations ip6mr_net_ops = {
1060 .init = ip6mr_net_init,
1061 .exit = ip6mr_net_exit,
1062};
1063
Wang Chen623d1a12008-07-03 12:13:30 +08001064int __init ip6_mr_init(void)
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001065{
Wang Chen623d1a12008-07-03 12:13:30 +08001066 int err;
1067
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001068 mrt_cachep = kmem_cache_create("ip6_mrt_cache",
1069 sizeof(struct mfc6_cache),
1070 0, SLAB_HWCACHE_ALIGN,
1071 NULL);
1072 if (!mrt_cachep)
Wang Chen623d1a12008-07-03 12:13:30 +08001073 return -ENOMEM;
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001074
Benjamin Thery4e168802008-12-10 16:15:08 -08001075 err = register_pernet_subsys(&ip6mr_net_ops);
1076 if (err)
1077 goto reg_pernet_fail;
1078
Wang Chen623d1a12008-07-03 12:13:30 +08001079 err = register_netdevice_notifier(&ip6_mr_notifier);
1080 if (err)
1081 goto reg_notif_fail;
Tom Goff403dbb92009-06-14 03:16:13 -07001082#ifdef CONFIG_IPV6_PIMSM_V2
1083 if (inet6_add_protocol(&pim6_protocol, IPPROTO_PIM) < 0) {
1084 printk(KERN_ERR "ip6_mr_init: can't add PIM protocol\n");
1085 err = -EAGAIN;
1086 goto add_proto_fail;
1087 }
1088#endif
Wang Chen623d1a12008-07-03 12:13:30 +08001089 return 0;
Tom Goff403dbb92009-06-14 03:16:13 -07001090#ifdef CONFIG_IPV6_PIMSM_V2
1091add_proto_fail:
1092 unregister_netdevice_notifier(&ip6_mr_notifier);
1093#endif
Benjamin Thery87b30a62008-11-10 16:34:11 -08001094reg_notif_fail:
Benjamin Thery4e168802008-12-10 16:15:08 -08001095 unregister_pernet_subsys(&ip6mr_net_ops);
1096reg_pernet_fail:
Benjamin Thery87b30a62008-11-10 16:34:11 -08001097 kmem_cache_destroy(mrt_cachep);
Wang Chen623d1a12008-07-03 12:13:30 +08001098 return err;
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001099}
1100
Wang Chen623d1a12008-07-03 12:13:30 +08001101void ip6_mr_cleanup(void)
1102{
Wang Chen623d1a12008-07-03 12:13:30 +08001103 unregister_netdevice_notifier(&ip6_mr_notifier);
Benjamin Thery4e168802008-12-10 16:15:08 -08001104 unregister_pernet_subsys(&ip6mr_net_ops);
Wang Chen623d1a12008-07-03 12:13:30 +08001105 kmem_cache_destroy(mrt_cachep);
1106}
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001107
Benjamin Thery8229efd2008-12-10 16:30:15 -08001108static int ip6mr_mfc_add(struct net *net, struct mf6cctl *mfc, int mrtsock)
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001109{
Patrick McHardyf30a77842010-05-11 14:40:51 +02001110 bool found = false;
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001111 int line;
Patrick McHardyf30a77842010-05-11 14:40:51 +02001112 struct mfc6_cache *uc, *c;
Rami Rosen6ac7eb02008-04-10 12:40:10 +03001113 unsigned char ttls[MAXMIFS];
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001114 int i;
1115
Patrick McHardya50436f22010-03-17 06:04:14 +00001116 if (mfc->mf6cc_parent >= MAXMIFS)
1117 return -ENFILE;
1118
Rami Rosen6ac7eb02008-04-10 12:40:10 +03001119 memset(ttls, 255, MAXMIFS);
1120 for (i = 0; i < MAXMIFS; i++) {
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001121 if (IF_ISSET(i, &mfc->mf6cc_ifset))
1122 ttls[i] = 1;
1123
1124 }
1125
1126 line = MFC6_HASH(&mfc->mf6cc_mcastgrp.sin6_addr, &mfc->mf6cc_origin.sin6_addr);
1127
Patrick McHardyf30a77842010-05-11 14:40:51 +02001128 list_for_each_entry(c, &net->ipv6.mfc6_cache_array[line], list) {
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001129 if (ipv6_addr_equal(&c->mf6c_origin, &mfc->mf6cc_origin.sin6_addr) &&
Patrick McHardyf30a77842010-05-11 14:40:51 +02001130 ipv6_addr_equal(&c->mf6c_mcastgrp, &mfc->mf6cc_mcastgrp.sin6_addr)) {
1131 found = true;
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001132 break;
Patrick McHardyf30a77842010-05-11 14:40:51 +02001133 }
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001134 }
1135
Patrick McHardyf30a77842010-05-11 14:40:51 +02001136 if (found) {
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001137 write_lock_bh(&mrt_lock);
1138 c->mf6c_parent = mfc->mf6cc_parent;
Patrick McHardyb5aa30b2010-05-11 14:40:50 +02001139 ip6mr_update_thresholds(net, c, ttls);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001140 if (!mrtsock)
1141 c->mfc_flags |= MFC_STATIC;
1142 write_unlock_bh(&mrt_lock);
1143 return 0;
1144 }
1145
1146 if (!ipv6_addr_is_multicast(&mfc->mf6cc_mcastgrp.sin6_addr))
1147 return -EINVAL;
1148
Patrick McHardyb5aa30b2010-05-11 14:40:50 +02001149 c = ip6mr_cache_alloc();
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001150 if (c == NULL)
1151 return -ENOMEM;
1152
1153 c->mf6c_origin = mfc->mf6cc_origin.sin6_addr;
1154 c->mf6c_mcastgrp = mfc->mf6cc_mcastgrp.sin6_addr;
1155 c->mf6c_parent = mfc->mf6cc_parent;
Patrick McHardyb5aa30b2010-05-11 14:40:50 +02001156 ip6mr_update_thresholds(net, c, ttls);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001157 if (!mrtsock)
1158 c->mfc_flags |= MFC_STATIC;
1159
1160 write_lock_bh(&mrt_lock);
Patrick McHardyf30a77842010-05-11 14:40:51 +02001161 list_add(&c->list, &net->ipv6.mfc6_cache_array[line]);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001162 write_unlock_bh(&mrt_lock);
1163
1164 /*
1165 * Check to see if we resolved a queued list. If so we
1166 * need to send on the frames and tidy up.
1167 */
Patrick McHardyf30a77842010-05-11 14:40:51 +02001168 found = false;
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001169 spin_lock_bh(&mfc_unres_lock);
Patrick McHardyf30a77842010-05-11 14:40:51 +02001170 list_for_each_entry(uc, &net->ipv6.mfc6_unres_queue, list) {
Patrick McHardyc476efb2010-05-11 14:40:48 +02001171 if (ipv6_addr_equal(&uc->mf6c_origin, &c->mf6c_origin) &&
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001172 ipv6_addr_equal(&uc->mf6c_mcastgrp, &c->mf6c_mcastgrp)) {
Patrick McHardyf30a77842010-05-11 14:40:51 +02001173 list_del(&uc->list);
Benjamin Thery8229efd2008-12-10 16:30:15 -08001174 atomic_dec(&net->ipv6.cache_resolve_queue_len);
Patrick McHardyf30a77842010-05-11 14:40:51 +02001175 found = true;
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001176 break;
1177 }
1178 }
Patrick McHardyf30a77842010-05-11 14:40:51 +02001179 if (list_empty(&net->ipv6.mfc6_unres_queue))
Patrick McHardyc476efb2010-05-11 14:40:48 +02001180 del_timer(&net->ipv6.ipmr_expire_timer);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001181 spin_unlock_bh(&mfc_unres_lock);
1182
Patrick McHardyf30a77842010-05-11 14:40:51 +02001183 if (found) {
Patrick McHardyb5aa30b2010-05-11 14:40:50 +02001184 ip6mr_cache_resolve(net, uc, c);
Benjamin Thery58701ad2008-12-10 16:22:34 -08001185 ip6mr_cache_free(uc);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001186 }
1187 return 0;
1188}
1189
1190/*
1191 * Close the multicast socket, and clear the vif tables etc
1192 */
1193
Benjamin Thery8229efd2008-12-10 16:30:15 -08001194static void mroute_clean_tables(struct net *net)
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001195{
1196 int i;
Eric Dumazetc871e662009-10-28 04:48:11 +00001197 LIST_HEAD(list);
Patrick McHardyf30a77842010-05-11 14:40:51 +02001198 struct mfc6_cache *c, *next;
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001199
1200 /*
1201 * Shut down all active vif entries
1202 */
Benjamin Thery8229efd2008-12-10 16:30:15 -08001203 for (i = 0; i < net->ipv6.maxvif; i++) {
1204 if (!(net->ipv6.vif6_table[i].flags & VIFF_STATIC))
Eric Dumazetc871e662009-10-28 04:48:11 +00001205 mif6_delete(net, i, &list);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001206 }
Eric Dumazetc871e662009-10-28 04:48:11 +00001207 unregister_netdevice_many(&list);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001208
1209 /*
1210 * Wipe the cache
1211 */
Benjamin Thery4a6258a2008-12-10 16:24:07 -08001212 for (i = 0; i < MFC6_LINES; i++) {
Patrick McHardyf30a77842010-05-11 14:40:51 +02001213 list_for_each_entry_safe(c, next, &net->ipv6.mfc6_cache_array[i], list) {
1214 if (c->mfc_flags & MFC_STATIC)
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001215 continue;
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001216 write_lock_bh(&mrt_lock);
Patrick McHardyf30a77842010-05-11 14:40:51 +02001217 list_del(&c->list);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001218 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) {
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001225 spin_lock_bh(&mfc_unres_lock);
Patrick McHardyf30a77842010-05-11 14:40:51 +02001226 list_for_each_entry_safe(c, next, &net->ipv6.mfc6_unres_queue, list) {
1227 list_del(&c->list);
Patrick McHardyb5aa30b2010-05-11 14:40:50 +02001228 ip6mr_destroy_unres(net, c);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001229 }
1230 spin_unlock_bh(&mfc_unres_lock);
1231 }
1232}
1233
1234static int ip6mr_sk_init(struct sock *sk)
1235{
1236 int err = 0;
Benjamin Thery8229efd2008-12-10 16:30:15 -08001237 struct net *net = sock_net(sk);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001238
1239 rtnl_lock();
1240 write_lock_bh(&mrt_lock);
Thomas Goff1d6e55f2009-01-27 22:39:59 -08001241 if (likely(net->ipv6.mroute6_sk == NULL)) {
Benjamin Thery8229efd2008-12-10 16:30:15 -08001242 net->ipv6.mroute6_sk = sk;
Thomas Goff1d6e55f2009-01-27 22:39:59 -08001243 net->ipv6.devconf_all->mc_forwarding++;
1244 }
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001245 else
1246 err = -EADDRINUSE;
1247 write_unlock_bh(&mrt_lock);
1248
1249 rtnl_unlock();
1250
1251 return err;
1252}
1253
1254int ip6mr_sk_done(struct sock *sk)
1255{
1256 int err = 0;
Benjamin Thery8229efd2008-12-10 16:30:15 -08001257 struct net *net = sock_net(sk);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001258
1259 rtnl_lock();
Benjamin Thery8229efd2008-12-10 16:30:15 -08001260 if (sk == net->ipv6.mroute6_sk) {
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001261 write_lock_bh(&mrt_lock);
Benjamin Thery8229efd2008-12-10 16:30:15 -08001262 net->ipv6.mroute6_sk = NULL;
Thomas Goff1d6e55f2009-01-27 22:39:59 -08001263 net->ipv6.devconf_all->mc_forwarding--;
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001264 write_unlock_bh(&mrt_lock);
1265
Benjamin Thery8229efd2008-12-10 16:30:15 -08001266 mroute_clean_tables(net);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001267 } else
1268 err = -EACCES;
1269 rtnl_unlock();
1270
1271 return err;
1272}
1273
1274/*
1275 * Socket options and virtual interface manipulation. The whole
1276 * virtual interface system is a complete heap, but unfortunately
1277 * that's how BSD mrouted happens to think. Maybe one day with a proper
1278 * MOSPF/PIM router set up we can clean this up.
1279 */
1280
David S. Millerb7058842009-09-30 16:12:20 -07001281int ip6_mroute_setsockopt(struct sock *sk, int optname, char __user *optval, unsigned int optlen)
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001282{
1283 int ret;
1284 struct mif6ctl vif;
1285 struct mf6cctl mfc;
1286 mifi_t mifi;
Benjamin Thery8229efd2008-12-10 16:30:15 -08001287 struct net *net = sock_net(sk);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001288
1289 if (optname != MRT6_INIT) {
Benjamin Thery8229efd2008-12-10 16:30:15 -08001290 if (sk != net->ipv6.mroute6_sk && !capable(CAP_NET_ADMIN))
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001291 return -EACCES;
1292 }
1293
1294 switch (optname) {
1295 case MRT6_INIT:
1296 if (sk->sk_type != SOCK_RAW ||
Eric Dumazetc720c7e2009-10-15 06:30:45 +00001297 inet_sk(sk)->inet_num != IPPROTO_ICMPV6)
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001298 return -EOPNOTSUPP;
1299 if (optlen < sizeof(int))
1300 return -EINVAL;
1301
1302 return ip6mr_sk_init(sk);
1303
1304 case MRT6_DONE:
1305 return ip6mr_sk_done(sk);
1306
1307 case MRT6_ADD_MIF:
1308 if (optlen < sizeof(vif))
1309 return -EINVAL;
1310 if (copy_from_user(&vif, optval, sizeof(vif)))
1311 return -EFAULT;
Rami Rosen6ac7eb02008-04-10 12:40:10 +03001312 if (vif.mif6c_mifi >= MAXMIFS)
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001313 return -ENFILE;
1314 rtnl_lock();
Benjamin Thery8229efd2008-12-10 16:30:15 -08001315 ret = mif6_add(net, &vif, sk == net->ipv6.mroute6_sk);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001316 rtnl_unlock();
1317 return ret;
1318
1319 case MRT6_DEL_MIF:
1320 if (optlen < sizeof(mifi_t))
1321 return -EINVAL;
1322 if (copy_from_user(&mifi, optval, sizeof(mifi_t)))
1323 return -EFAULT;
1324 rtnl_lock();
Eric Dumazetc871e662009-10-28 04:48:11 +00001325 ret = mif6_delete(net, mifi, NULL);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001326 rtnl_unlock();
1327 return ret;
1328
1329 /*
1330 * Manipulate the forwarding caches. These live
1331 * in a sort of kernel/user symbiosis.
1332 */
1333 case MRT6_ADD_MFC:
1334 case MRT6_DEL_MFC:
1335 if (optlen < sizeof(mfc))
1336 return -EINVAL;
1337 if (copy_from_user(&mfc, optval, sizeof(mfc)))
1338 return -EFAULT;
1339 rtnl_lock();
1340 if (optname == MRT6_DEL_MFC)
Benjamin Thery8229efd2008-12-10 16:30:15 -08001341 ret = ip6mr_mfc_delete(net, &mfc);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001342 else
Benjamin Thery8229efd2008-12-10 16:30:15 -08001343 ret = ip6mr_mfc_add(net, &mfc,
1344 sk == net->ipv6.mroute6_sk);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001345 rtnl_unlock();
1346 return ret;
1347
1348 /*
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +09001349 * Control PIM assert (to activate pim will activate assert)
1350 */
1351 case MRT6_ASSERT:
1352 {
1353 int v;
1354 if (get_user(v, (int __user *)optval))
1355 return -EFAULT;
Benjamin Thery8229efd2008-12-10 16:30:15 -08001356 net->ipv6.mroute_do_assert = !!v;
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +09001357 return 0;
1358 }
1359
1360#ifdef CONFIG_IPV6_PIMSM_V2
1361 case MRT6_PIM:
1362 {
YOSHIFUJI Hideakia9f83bf2008-04-10 15:41:28 +09001363 int v;
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +09001364 if (get_user(v, (int __user *)optval))
1365 return -EFAULT;
1366 v = !!v;
1367 rtnl_lock();
1368 ret = 0;
Benjamin Thery8229efd2008-12-10 16:30:15 -08001369 if (v != net->ipv6.mroute_do_pim) {
1370 net->ipv6.mroute_do_pim = v;
1371 net->ipv6.mroute_do_assert = v;
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +09001372 }
1373 rtnl_unlock();
1374 return ret;
1375 }
1376
1377#endif
1378 /*
Rami Rosen7d120c52008-04-23 14:35:13 +03001379 * Spurious command, or MRT6_VERSION which you cannot
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001380 * set.
1381 */
1382 default:
1383 return -ENOPROTOOPT;
1384 }
1385}
1386
1387/*
1388 * Getsock opt support for the multicast routing system.
1389 */
1390
1391int ip6_mroute_getsockopt(struct sock *sk, int optname, char __user *optval,
1392 int __user *optlen)
1393{
1394 int olr;
1395 int val;
Benjamin Thery8229efd2008-12-10 16:30:15 -08001396 struct net *net = sock_net(sk);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001397
1398 switch (optname) {
1399 case MRT6_VERSION:
1400 val = 0x0305;
1401 break;
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +09001402#ifdef CONFIG_IPV6_PIMSM_V2
1403 case MRT6_PIM:
Benjamin Thery8229efd2008-12-10 16:30:15 -08001404 val = net->ipv6.mroute_do_pim;
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +09001405 break;
1406#endif
1407 case MRT6_ASSERT:
Benjamin Thery8229efd2008-12-10 16:30:15 -08001408 val = net->ipv6.mroute_do_assert;
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +09001409 break;
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001410 default:
1411 return -ENOPROTOOPT;
1412 }
1413
1414 if (get_user(olr, optlen))
1415 return -EFAULT;
1416
1417 olr = min_t(int, olr, sizeof(int));
1418 if (olr < 0)
1419 return -EINVAL;
1420
1421 if (put_user(olr, optlen))
1422 return -EFAULT;
1423 if (copy_to_user(optval, &val, olr))
1424 return -EFAULT;
1425 return 0;
1426}
1427
1428/*
1429 * The IP multicast ioctl support routines.
1430 */
1431
1432int ip6mr_ioctl(struct sock *sk, int cmd, void __user *arg)
1433{
1434 struct sioc_sg_req6 sr;
1435 struct sioc_mif_req6 vr;
1436 struct mif_device *vif;
1437 struct mfc6_cache *c;
Benjamin Thery8229efd2008-12-10 16:30:15 -08001438 struct net *net = sock_net(sk);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001439
1440 switch (cmd) {
1441 case SIOCGETMIFCNT_IN6:
1442 if (copy_from_user(&vr, arg, sizeof(vr)))
1443 return -EFAULT;
Benjamin Thery8229efd2008-12-10 16:30:15 -08001444 if (vr.mifi >= net->ipv6.maxvif)
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001445 return -EINVAL;
1446 read_lock(&mrt_lock);
Benjamin Thery8229efd2008-12-10 16:30:15 -08001447 vif = &net->ipv6.vif6_table[vr.mifi];
1448 if (MIF_EXISTS(net, vr.mifi)) {
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001449 vr.icount = vif->pkt_in;
1450 vr.ocount = vif->pkt_out;
1451 vr.ibytes = vif->bytes_in;
1452 vr.obytes = vif->bytes_out;
1453 read_unlock(&mrt_lock);
1454
1455 if (copy_to_user(arg, &vr, sizeof(vr)))
1456 return -EFAULT;
1457 return 0;
1458 }
1459 read_unlock(&mrt_lock);
1460 return -EADDRNOTAVAIL;
1461 case SIOCGETSGCNT_IN6:
1462 if (copy_from_user(&sr, arg, sizeof(sr)))
1463 return -EFAULT;
1464
1465 read_lock(&mrt_lock);
Benjamin Thery8229efd2008-12-10 16:30:15 -08001466 c = ip6mr_cache_find(net, &sr.src.sin6_addr, &sr.grp.sin6_addr);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001467 if (c) {
1468 sr.pktcnt = c->mfc_un.res.pkt;
1469 sr.bytecnt = c->mfc_un.res.bytes;
1470 sr.wrong_if = c->mfc_un.res.wrong_if;
1471 read_unlock(&mrt_lock);
1472
1473 if (copy_to_user(arg, &sr, sizeof(sr)))
1474 return -EFAULT;
1475 return 0;
1476 }
1477 read_unlock(&mrt_lock);
1478 return -EADDRNOTAVAIL;
1479 default:
1480 return -ENOIOCTLCMD;
1481 }
1482}
1483
1484
1485static inline int ip6mr_forward2_finish(struct sk_buff *skb)
1486{
Eric Dumazetadf30902009-06-02 05:19:30 +00001487 IP6_INC_STATS_BH(dev_net(skb_dst(skb)->dev), ip6_dst_idev(skb_dst(skb)),
Denis V. Lunev483a47d2008-10-08 11:09:27 -07001488 IPSTATS_MIB_OUTFORWDATAGRAMS);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001489 return dst_output(skb);
1490}
1491
1492/*
1493 * Processing handlers for ip6mr_forward
1494 */
1495
Patrick McHardyb5aa30b2010-05-11 14:40:50 +02001496static int ip6mr_forward2(struct net *net, struct sk_buff *skb,
1497 struct mfc6_cache *c, int vifi)
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001498{
1499 struct ipv6hdr *ipv6h;
Benjamin Thery8229efd2008-12-10 16:30:15 -08001500 struct mif_device *vif = &net->ipv6.vif6_table[vifi];
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001501 struct net_device *dev;
1502 struct dst_entry *dst;
1503 struct flowi fl;
1504
1505 if (vif->dev == NULL)
1506 goto out_free;
1507
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +09001508#ifdef CONFIG_IPV6_PIMSM_V2
1509 if (vif->flags & MIFF_REGISTER) {
1510 vif->pkt_out++;
1511 vif->bytes_out += skb->len;
Pavel Emelyanovdc58c782008-05-21 14:17:54 -07001512 vif->dev->stats.tx_bytes += skb->len;
1513 vif->dev->stats.tx_packets++;
Benjamin Thery8229efd2008-12-10 16:30:15 -08001514 ip6mr_cache_report(net, skb, vifi, MRT6MSG_WHOLEPKT);
Ilpo Järvinen8da73b72008-12-14 23:15:49 -08001515 goto out_free;
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +09001516 }
1517#endif
1518
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001519 ipv6h = ipv6_hdr(skb);
1520
1521 fl = (struct flowi) {
1522 .oif = vif->link,
1523 .nl_u = { .ip6_u =
1524 { .daddr = ipv6h->daddr, }
1525 }
1526 };
1527
Benjamin Thery8229efd2008-12-10 16:30:15 -08001528 dst = ip6_route_output(net, NULL, &fl);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001529 if (!dst)
1530 goto out_free;
1531
Eric Dumazetadf30902009-06-02 05:19:30 +00001532 skb_dst_drop(skb);
1533 skb_dst_set(skb, dst);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001534
1535 /*
1536 * RFC1584 teaches, that DVMRP/PIM router must deliver packets locally
1537 * not only before forwarding, but after forwarding on all output
1538 * interfaces. It is clear, if mrouter runs a multicasting
1539 * program, it should receive packets not depending to what interface
1540 * program is joined.
1541 * If we will not make it, the program will have to join on all
1542 * interfaces. On the other hand, multihoming host (or router, but
1543 * not mrouter) cannot join to more than one interface - it will
1544 * result in receiving multiple packets.
1545 */
1546 dev = vif->dev;
1547 skb->dev = dev;
1548 vif->pkt_out++;
1549 vif->bytes_out += skb->len;
1550
1551 /* We are about to write */
1552 /* XXX: extension headers? */
1553 if (skb_cow(skb, sizeof(*ipv6h) + LL_RESERVED_SPACE(dev)))
1554 goto out_free;
1555
1556 ipv6h = ipv6_hdr(skb);
1557 ipv6h->hop_limit--;
1558
1559 IP6CB(skb)->flags |= IP6SKB_FORWARDED;
1560
Jan Engelhardtb2e0b382010-03-23 04:09:07 +01001561 return NF_HOOK(NFPROTO_IPV6, NF_INET_FORWARD, skb, skb->dev, dev,
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001562 ip6mr_forward2_finish);
1563
1564out_free:
1565 kfree_skb(skb);
1566 return 0;
1567}
1568
1569static int ip6mr_find_vif(struct net_device *dev)
1570{
Benjamin Thery8229efd2008-12-10 16:30:15 -08001571 struct net *net = dev_net(dev);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001572 int ct;
Benjamin Thery8229efd2008-12-10 16:30:15 -08001573 for (ct = net->ipv6.maxvif - 1; ct >= 0; ct--) {
1574 if (net->ipv6.vif6_table[ct].dev == dev)
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001575 break;
1576 }
1577 return ct;
1578}
1579
Patrick McHardyb5aa30b2010-05-11 14:40:50 +02001580static int ip6_mr_forward(struct net *net, struct sk_buff *skb,
1581 struct mfc6_cache *cache)
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001582{
1583 int psend = -1;
1584 int vif, ct;
1585
1586 vif = cache->mf6c_parent;
1587 cache->mfc_un.res.pkt++;
1588 cache->mfc_un.res.bytes += skb->len;
1589
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +09001590 /*
1591 * Wrong interface: drop packet and (maybe) send PIM assert.
1592 */
Benjamin Thery8229efd2008-12-10 16:30:15 -08001593 if (net->ipv6.vif6_table[vif].dev != skb->dev) {
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +09001594 int true_vifi;
1595
1596 cache->mfc_un.res.wrong_if++;
1597 true_vifi = ip6mr_find_vif(skb->dev);
1598
Benjamin Thery8229efd2008-12-10 16:30:15 -08001599 if (true_vifi >= 0 && net->ipv6.mroute_do_assert &&
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +09001600 /* pimsm uses asserts, when switching from RPT to SPT,
1601 so that we cannot check that packet arrived on an oif.
1602 It is bad, but otherwise we would need to move pretty
1603 large chunk of pimd to kernel. Ough... --ANK
1604 */
Benjamin Thery8229efd2008-12-10 16:30:15 -08001605 (net->ipv6.mroute_do_pim ||
Benjamin Therya21f3f92008-12-10 16:28:44 -08001606 cache->mfc_un.res.ttls[true_vifi] < 255) &&
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +09001607 time_after(jiffies,
1608 cache->mfc_un.res.last_assert + MFC_ASSERT_THRESH)) {
1609 cache->mfc_un.res.last_assert = jiffies;
Benjamin Thery8229efd2008-12-10 16:30:15 -08001610 ip6mr_cache_report(net, skb, true_vifi, MRT6MSG_WRONGMIF);
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +09001611 }
1612 goto dont_forward;
1613 }
1614
Benjamin Thery8229efd2008-12-10 16:30:15 -08001615 net->ipv6.vif6_table[vif].pkt_in++;
1616 net->ipv6.vif6_table[vif].bytes_in += skb->len;
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001617
1618 /*
1619 * Forward the frame
1620 */
1621 for (ct = cache->mfc_un.res.maxvif - 1; ct >= cache->mfc_un.res.minvif; ct--) {
1622 if (ipv6_hdr(skb)->hop_limit > cache->mfc_un.res.ttls[ct]) {
1623 if (psend != -1) {
1624 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
1625 if (skb2)
Patrick McHardyb5aa30b2010-05-11 14:40:50 +02001626 ip6mr_forward2(net, skb2, cache, psend);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001627 }
1628 psend = ct;
1629 }
1630 }
1631 if (psend != -1) {
Patrick McHardyb5aa30b2010-05-11 14:40:50 +02001632 ip6mr_forward2(net, skb, cache, psend);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001633 return 0;
1634 }
1635
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +09001636dont_forward:
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001637 kfree_skb(skb);
1638 return 0;
1639}
1640
1641
1642/*
1643 * Multicast packets for forwarding arrive here
1644 */
1645
1646int ip6_mr_input(struct sk_buff *skb)
1647{
1648 struct mfc6_cache *cache;
Benjamin Thery8229efd2008-12-10 16:30:15 -08001649 struct net *net = dev_net(skb->dev);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001650
1651 read_lock(&mrt_lock);
Benjamin Thery8229efd2008-12-10 16:30:15 -08001652 cache = ip6mr_cache_find(net,
1653 &ipv6_hdr(skb)->saddr, &ipv6_hdr(skb)->daddr);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001654
1655 /*
1656 * No usable cache entry
1657 */
1658 if (cache == NULL) {
1659 int vif;
1660
1661 vif = ip6mr_find_vif(skb->dev);
1662 if (vif >= 0) {
Benjamin Thery8229efd2008-12-10 16:30:15 -08001663 int err = ip6mr_cache_unresolved(net, vif, skb);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001664 read_unlock(&mrt_lock);
1665
1666 return err;
1667 }
1668 read_unlock(&mrt_lock);
1669 kfree_skb(skb);
1670 return -ENODEV;
1671 }
1672
Patrick McHardyb5aa30b2010-05-11 14:40:50 +02001673 ip6_mr_forward(net, skb, cache);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001674
1675 read_unlock(&mrt_lock);
1676
1677 return 0;
1678}
1679
1680
1681static int
Patrick McHardyb5aa30b2010-05-11 14:40:50 +02001682ip6mr_fill_mroute(struct net *net, struct sk_buff *skb, struct mfc6_cache *c,
1683 struct rtmsg *rtm)
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001684{
1685 int ct;
1686 struct rtnexthop *nhp;
YOSHIFUJI Hideaki549e0282008-04-05 22:17:39 +09001687 u8 *b = skb_tail_pointer(skb);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001688 struct rtattr *mp_head;
1689
Nicolas Dichtel74381892010-03-25 23:45:35 +00001690 /* If cache is unresolved, don't try to parse IIF and OIF */
1691 if (c->mf6c_parent > MAXMIFS)
1692 return -ENOENT;
1693
1694 if (MIF_EXISTS(net, c->mf6c_parent))
1695 RTA_PUT(skb, RTA_IIF, 4, &net->ipv6.vif6_table[c->mf6c_parent].dev->ifindex);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001696
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++) {
Nicolas Dichtel74381892010-03-25 23:45:35 +00001700 if (MIF_EXISTS(net, ct) && c->mfc_un.res.ttls[ct] < 255) {
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001701 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;
Eric Dumazetadf30902009-06-02 05:19:30 +00001725 struct rt6_info *rt = (struct rt6_info *)skb_dst(skb);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001726
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
Patrick McHardyb5aa30b2010-05-11 14:40:50 +02001780 err = ip6mr_fill_mroute(net, skb, cache, rtm);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001781 read_unlock(&mrt_lock);
1782 return err;
1783}
1784