blob: 7236030e403eafdb9a293878364b0113297914a5 [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
79static int ip6_mr_forward(struct sk_buff *skb, struct mfc6_cache *cache);
Benjamin Thery8229efd2008-12-10 16:30:15 -080080static int ip6mr_cache_report(struct net *net, struct sk_buff *pkt,
81 mifi_t mifi, int assert);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +090082static int ip6mr_fill_mroute(struct sk_buff *skb, struct mfc6_cache *c, struct rtmsg *rtm);
Benjamin Thery8229efd2008-12-10 16:30:15 -080083static void mroute_clean_tables(struct net *net);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +090084
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +090085
86#ifdef CONFIG_PROC_FS
87
88struct ipmr_mfc_iter {
Benjamin Thery8b90fc72008-12-10 16:29:48 -080089 struct seq_net_private p;
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +090090 struct mfc6_cache **cache;
91 int ct;
92};
93
94
Benjamin Thery8b90fc72008-12-10 16:29:48 -080095static struct mfc6_cache *ipmr_mfc_seq_idx(struct net *net,
96 struct ipmr_mfc_iter *it, loff_t pos)
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +090097{
98 struct mfc6_cache *mfc;
99
Benjamin Thery8b90fc72008-12-10 16:29:48 -0800100 it->cache = net->ipv6.mfc6_cache_array;
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900101 read_lock(&mrt_lock);
Benjamin Thery4a6258a2008-12-10 16:24:07 -0800102 for (it->ct = 0; it->ct < MFC6_LINES; it->ct++)
Benjamin Thery8b90fc72008-12-10 16:29:48 -0800103 for (mfc = net->ipv6.mfc6_cache_array[it->ct];
Benjamin Thery4a6258a2008-12-10 16:24:07 -0800104 mfc; mfc = mfc->next)
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900105 if (pos-- == 0)
106 return mfc;
107 read_unlock(&mrt_lock);
108
Patrick McHardyc476efb2010-05-11 14:40:48 +0200109 it->cache = &net->ipv6.mfc6_unres_queue;
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900110 spin_lock_bh(&mfc_unres_lock);
Patrick McHardyc476efb2010-05-11 14:40:48 +0200111 for (mfc = net->ipv6.mfc6_unres_queue; mfc; mfc = mfc->next)
112 if (pos-- == 0)
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900113 return mfc;
114 spin_unlock_bh(&mfc_unres_lock);
115
116 it->cache = NULL;
117 return NULL;
118}
119
120
121
122
123/*
124 * The /proc interfaces to multicast routing /proc/ip6_mr_cache /proc/ip6_mr_vif
125 */
126
127struct ipmr_vif_iter {
Benjamin Thery8b90fc72008-12-10 16:29:48 -0800128 struct seq_net_private p;
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900129 int ct;
130};
131
Benjamin Thery8b90fc72008-12-10 16:29:48 -0800132static struct mif_device *ip6mr_vif_seq_idx(struct net *net,
133 struct ipmr_vif_iter *iter,
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900134 loff_t pos)
135{
Benjamin Thery8b90fc72008-12-10 16:29:48 -0800136 for (iter->ct = 0; iter->ct < net->ipv6.maxvif; ++iter->ct) {
137 if (!MIF_EXISTS(net, iter->ct))
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900138 continue;
139 if (pos-- == 0)
Benjamin Thery8b90fc72008-12-10 16:29:48 -0800140 return &net->ipv6.vif6_table[iter->ct];
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900141 }
142 return NULL;
143}
144
145static void *ip6mr_vif_seq_start(struct seq_file *seq, loff_t *pos)
146 __acquires(mrt_lock)
147{
Benjamin Thery8b90fc72008-12-10 16:29:48 -0800148 struct net *net = seq_file_net(seq);
149
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900150 read_lock(&mrt_lock);
Benjamin Thery8b90fc72008-12-10 16:29:48 -0800151 return *pos ? ip6mr_vif_seq_idx(net, seq->private, *pos - 1)
152 : SEQ_START_TOKEN;
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900153}
154
155static void *ip6mr_vif_seq_next(struct seq_file *seq, void *v, loff_t *pos)
156{
157 struct ipmr_vif_iter *iter = seq->private;
Benjamin Thery8b90fc72008-12-10 16:29:48 -0800158 struct net *net = seq_file_net(seq);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900159
160 ++*pos;
161 if (v == SEQ_START_TOKEN)
Benjamin Thery8b90fc72008-12-10 16:29:48 -0800162 return ip6mr_vif_seq_idx(net, iter, 0);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900163
Benjamin Thery8b90fc72008-12-10 16:29:48 -0800164 while (++iter->ct < net->ipv6.maxvif) {
165 if (!MIF_EXISTS(net, iter->ct))
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900166 continue;
Benjamin Thery8b90fc72008-12-10 16:29:48 -0800167 return &net->ipv6.vif6_table[iter->ct];
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900168 }
169 return NULL;
170}
171
172static void ip6mr_vif_seq_stop(struct seq_file *seq, void *v)
173 __releases(mrt_lock)
174{
175 read_unlock(&mrt_lock);
176}
177
178static int ip6mr_vif_seq_show(struct seq_file *seq, void *v)
179{
Benjamin Thery8b90fc72008-12-10 16:29:48 -0800180 struct net *net = seq_file_net(seq);
181
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900182 if (v == SEQ_START_TOKEN) {
183 seq_puts(seq,
184 "Interface BytesIn PktsIn BytesOut PktsOut Flags\n");
185 } else {
186 const struct mif_device *vif = v;
187 const char *name = vif->dev ? vif->dev->name : "none";
188
189 seq_printf(seq,
Al Virod430a222008-06-02 10:59:02 +0100190 "%2td %-10s %8ld %7ld %8ld %7ld %05X\n",
Benjamin Thery8b90fc72008-12-10 16:29:48 -0800191 vif - net->ipv6.vif6_table,
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900192 name, vif->bytes_in, vif->pkt_in,
193 vif->bytes_out, vif->pkt_out,
194 vif->flags);
195 }
196 return 0;
197}
198
Stephen Hemminger98147d52009-09-01 19:25:02 +0000199static const struct seq_operations ip6mr_vif_seq_ops = {
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900200 .start = ip6mr_vif_seq_start,
201 .next = ip6mr_vif_seq_next,
202 .stop = ip6mr_vif_seq_stop,
203 .show = ip6mr_vif_seq_show,
204};
205
206static int ip6mr_vif_open(struct inode *inode, struct file *file)
207{
Benjamin Thery8b90fc72008-12-10 16:29:48 -0800208 return seq_open_net(inode, file, &ip6mr_vif_seq_ops,
209 sizeof(struct ipmr_vif_iter));
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900210}
211
Stephen Hemminger5ca1b992009-09-01 19:25:05 +0000212static const struct file_operations ip6mr_vif_fops = {
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900213 .owner = THIS_MODULE,
214 .open = ip6mr_vif_open,
215 .read = seq_read,
216 .llseek = seq_lseek,
Benjamin Thery8b90fc72008-12-10 16:29:48 -0800217 .release = seq_release_net,
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900218};
219
220static void *ipmr_mfc_seq_start(struct seq_file *seq, loff_t *pos)
221{
Benjamin Thery8b90fc72008-12-10 16:29:48 -0800222 struct net *net = seq_file_net(seq);
223
224 return *pos ? ipmr_mfc_seq_idx(net, seq->private, *pos - 1)
225 : SEQ_START_TOKEN;
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900226}
227
228static void *ipmr_mfc_seq_next(struct seq_file *seq, void *v, loff_t *pos)
229{
230 struct mfc6_cache *mfc = v;
231 struct ipmr_mfc_iter *it = seq->private;
Benjamin Thery8b90fc72008-12-10 16:29:48 -0800232 struct net *net = seq_file_net(seq);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900233
234 ++*pos;
235
236 if (v == SEQ_START_TOKEN)
Benjamin Thery8b90fc72008-12-10 16:29:48 -0800237 return ipmr_mfc_seq_idx(net, seq->private, 0);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900238
239 if (mfc->next)
240 return mfc->next;
241
Patrick McHardyc476efb2010-05-11 14:40:48 +0200242 if (it->cache == &net->ipv6.mfc6_unres_queue)
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900243 goto end_of_list;
244
Benjamin Thery8b90fc72008-12-10 16:29:48 -0800245 BUG_ON(it->cache != net->ipv6.mfc6_cache_array);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900246
Benjamin Thery4a6258a2008-12-10 16:24:07 -0800247 while (++it->ct < MFC6_LINES) {
Benjamin Thery8b90fc72008-12-10 16:29:48 -0800248 mfc = net->ipv6.mfc6_cache_array[it->ct];
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900249 if (mfc)
250 return mfc;
251 }
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 McHardyc476efb2010-05-11 14:40:48 +0200259 mfc = net->ipv6.mfc6_unres_queue;
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900260 if (mfc)
261 return mfc;
262
263 end_of_list:
264 spin_unlock_bh(&mfc_unres_lock);
265 it->cache = NULL;
266
267 return NULL;
268}
269
270static void ipmr_mfc_seq_stop(struct seq_file *seq, void *v)
271{
272 struct ipmr_mfc_iter *it = seq->private;
Benjamin Thery8b90fc72008-12-10 16:29:48 -0800273 struct net *net = seq_file_net(seq);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900274
Patrick McHardyc476efb2010-05-11 14:40:48 +0200275 if (it->cache == &net->ipv6.mfc6_unres_queue)
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900276 spin_unlock_bh(&mfc_unres_lock);
Benjamin Thery8b90fc72008-12-10 16:29:48 -0800277 else if (it->cache == net->ipv6.mfc6_cache_array)
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900278 read_unlock(&mrt_lock);
279}
280
281static int ipmr_mfc_seq_show(struct seq_file *seq, void *v)
282{
283 int n;
Benjamin Thery8b90fc72008-12-10 16:29:48 -0800284 struct net *net = seq_file_net(seq);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900285
286 if (v == SEQ_START_TOKEN) {
287 seq_puts(seq,
288 "Group "
289 "Origin "
290 "Iif Pkts Bytes Wrong Oifs\n");
291 } else {
292 const struct mfc6_cache *mfc = v;
293 const struct ipmr_mfc_iter *it = seq->private;
294
Benjamin Thery999890b2008-12-03 22:22:16 -0800295 seq_printf(seq, "%pI6 %pI6 %-3hd",
Harvey Harrison0c6ce782008-10-28 16:09:23 -0700296 &mfc->mf6c_mcastgrp, &mfc->mf6c_origin,
Benjamin Thery1ea472e2008-12-03 22:21:47 -0800297 mfc->mf6c_parent);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900298
Patrick McHardyc476efb2010-05-11 14:40:48 +0200299 if (it->cache != &net->ipv6.mfc6_unres_queue) {
Benjamin Thery1ea472e2008-12-03 22:21:47 -0800300 seq_printf(seq, " %8lu %8lu %8lu",
301 mfc->mfc_un.res.pkt,
302 mfc->mfc_un.res.bytes,
303 mfc->mfc_un.res.wrong_if);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900304 for (n = mfc->mfc_un.res.minvif;
305 n < mfc->mfc_un.res.maxvif; n++) {
Benjamin Thery8b90fc72008-12-10 16:29:48 -0800306 if (MIF_EXISTS(net, n) &&
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900307 mfc->mfc_un.res.ttls[n] < 255)
308 seq_printf(seq,
309 " %2d:%-3d",
310 n, mfc->mfc_un.res.ttls[n]);
311 }
Benjamin Thery1ea472e2008-12-03 22:21:47 -0800312 } else {
313 /* unresolved mfc_caches don't contain
314 * pkt, bytes and wrong_if values
315 */
316 seq_printf(seq, " %8lu %8lu %8lu", 0ul, 0ul, 0ul);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900317 }
318 seq_putc(seq, '\n');
319 }
320 return 0;
321}
322
James Morris88e9d342009-09-22 16:43:43 -0700323static const struct seq_operations ipmr_mfc_seq_ops = {
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900324 .start = ipmr_mfc_seq_start,
325 .next = ipmr_mfc_seq_next,
326 .stop = ipmr_mfc_seq_stop,
327 .show = ipmr_mfc_seq_show,
328};
329
330static int ipmr_mfc_open(struct inode *inode, struct file *file)
331{
Benjamin Thery8b90fc72008-12-10 16:29:48 -0800332 return seq_open_net(inode, file, &ipmr_mfc_seq_ops,
333 sizeof(struct ipmr_mfc_iter));
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900334}
335
Stephen Hemminger5ca1b992009-09-01 19:25:05 +0000336static const struct file_operations ip6mr_mfc_fops = {
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900337 .owner = THIS_MODULE,
338 .open = ipmr_mfc_open,
339 .read = seq_read,
340 .llseek = seq_lseek,
Benjamin Thery8b90fc72008-12-10 16:29:48 -0800341 .release = seq_release_net,
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900342};
343#endif
344
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +0900345#ifdef CONFIG_IPV6_PIMSM_V2
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +0900346
347static int pim6_rcv(struct sk_buff *skb)
348{
349 struct pimreghdr *pim;
350 struct ipv6hdr *encap;
351 struct net_device *reg_dev = NULL;
Benjamin Thery8229efd2008-12-10 16:30:15 -0800352 struct net *net = dev_net(skb->dev);
353 int reg_vif_num = net->ipv6.mroute_reg_vif_num;
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +0900354
355 if (!pskb_may_pull(skb, sizeof(*pim) + sizeof(*encap)))
356 goto drop;
357
358 pim = (struct pimreghdr *)skb_transport_header(skb);
359 if (pim->type != ((PIM_VERSION << 4) | PIM_REGISTER) ||
360 (pim->flags & PIM_NULL_REGISTER) ||
Thomas Goff1d6e55f2009-01-27 22:39:59 -0800361 (csum_ipv6_magic(&ipv6_hdr(skb)->saddr, &ipv6_hdr(skb)->daddr,
362 sizeof(*pim), IPPROTO_PIM,
363 csum_partial((void *)pim, sizeof(*pim), 0)) &&
Al Viroec6b4862008-04-26 22:28:58 -0700364 csum_fold(skb_checksum(skb, 0, skb->len, 0))))
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +0900365 goto drop;
366
367 /* check if the inner packet is destined to mcast group */
368 encap = (struct ipv6hdr *)(skb_transport_header(skb) +
369 sizeof(*pim));
370
371 if (!ipv6_addr_is_multicast(&encap->daddr) ||
372 encap->payload_len == 0 ||
373 ntohs(encap->payload_len) + sizeof(*pim) > skb->len)
374 goto drop;
375
376 read_lock(&mrt_lock);
377 if (reg_vif_num >= 0)
Benjamin Thery8229efd2008-12-10 16:30:15 -0800378 reg_dev = net->ipv6.vif6_table[reg_vif_num].dev;
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +0900379 if (reg_dev)
380 dev_hold(reg_dev);
381 read_unlock(&mrt_lock);
382
383 if (reg_dev == NULL)
384 goto drop;
385
386 skb->mac_header = skb->network_header;
387 skb_pull(skb, (u8 *)encap - skb->data);
388 skb_reset_network_header(skb);
389 skb->dev = reg_dev;
Thomas Goff1d6e55f2009-01-27 22:39:59 -0800390 skb->protocol = htons(ETH_P_IPV6);
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +0900391 skb->ip_summed = 0;
392 skb->pkt_type = PACKET_HOST;
Eric Dumazetadf30902009-06-02 05:19:30 +0000393 skb_dst_drop(skb);
Pavel Emelyanovdc58c782008-05-21 14:17:54 -0700394 reg_dev->stats.rx_bytes += skb->len;
395 reg_dev->stats.rx_packets++;
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +0900396 nf_reset(skb);
397 netif_rx(skb);
398 dev_put(reg_dev);
399 return 0;
400 drop:
401 kfree_skb(skb);
402 return 0;
403}
404
Alexey Dobriyan41135cc2009-09-14 12:22:28 +0000405static const struct inet6_protocol pim6_protocol = {
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +0900406 .handler = pim6_rcv,
407};
408
409/* Service routines creating virtual interfaces: PIMREG */
410
Stephen Hemminger6fef4c02009-08-31 19:50:41 +0000411static netdev_tx_t reg_vif_xmit(struct sk_buff *skb,
412 struct net_device *dev)
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +0900413{
Benjamin Thery8229efd2008-12-10 16:30:15 -0800414 struct net *net = dev_net(dev);
415
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +0900416 read_lock(&mrt_lock);
Pavel Emelyanovdc58c782008-05-21 14:17:54 -0700417 dev->stats.tx_bytes += skb->len;
418 dev->stats.tx_packets++;
Benjamin Thery8229efd2008-12-10 16:30:15 -0800419 ip6mr_cache_report(net, skb, net->ipv6.mroute_reg_vif_num,
420 MRT6MSG_WHOLEPKT);
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +0900421 read_unlock(&mrt_lock);
422 kfree_skb(skb);
Patrick McHardy6ed10652009-06-23 06:03:08 +0000423 return NETDEV_TX_OK;
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +0900424}
425
Stephen Hemminger007c3832008-11-20 20:28:35 -0800426static const struct net_device_ops reg_vif_netdev_ops = {
427 .ndo_start_xmit = reg_vif_xmit,
428};
429
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +0900430static void reg_vif_setup(struct net_device *dev)
431{
432 dev->type = ARPHRD_PIMREG;
433 dev->mtu = 1500 - sizeof(struct ipv6hdr) - 8;
434 dev->flags = IFF_NOARP;
Stephen Hemminger007c3832008-11-20 20:28:35 -0800435 dev->netdev_ops = &reg_vif_netdev_ops;
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +0900436 dev->destructor = free_netdev;
Tom Goff403dbb92009-06-14 03:16:13 -0700437 dev->features |= NETIF_F_NETNS_LOCAL;
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +0900438}
439
Benjamin Thery8229efd2008-12-10 16:30:15 -0800440static struct net_device *ip6mr_reg_vif(struct net *net)
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +0900441{
442 struct net_device *dev;
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +0900443
Pavel Emelyanovdc58c782008-05-21 14:17:54 -0700444 dev = alloc_netdev(0, "pim6reg", reg_vif_setup);
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +0900445 if (dev == NULL)
446 return NULL;
447
Benjamin Thery8229efd2008-12-10 16:30:15 -0800448 dev_net_set(dev, net);
449
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +0900450 if (register_netdevice(dev)) {
451 free_netdev(dev);
452 return NULL;
453 }
454 dev->iflink = 0;
455
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +0900456 if (dev_open(dev))
457 goto failure;
458
Wang Chen7af3db72008-07-14 20:54:54 -0700459 dev_hold(dev);
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +0900460 return dev;
461
462failure:
463 /* allow the register to be completed before unregistering. */
464 rtnl_unlock();
465 rtnl_lock();
466
467 unregister_netdevice(dev);
468 return NULL;
469}
470#endif
471
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900472/*
473 * Delete a VIF entry
474 */
475
Eric Dumazetc871e662009-10-28 04:48:11 +0000476static int mif6_delete(struct net *net, int vifi, struct list_head *head)
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900477{
478 struct mif_device *v;
479 struct net_device *dev;
Thomas Goff1d6e55f2009-01-27 22:39:59 -0800480 struct inet6_dev *in6_dev;
Benjamin Thery8229efd2008-12-10 16:30:15 -0800481 if (vifi < 0 || vifi >= net->ipv6.maxvif)
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900482 return -EADDRNOTAVAIL;
483
Benjamin Thery8229efd2008-12-10 16:30:15 -0800484 v = &net->ipv6.vif6_table[vifi];
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900485
486 write_lock_bh(&mrt_lock);
487 dev = v->dev;
488 v->dev = NULL;
489
490 if (!dev) {
491 write_unlock_bh(&mrt_lock);
492 return -EADDRNOTAVAIL;
493 }
494
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +0900495#ifdef CONFIG_IPV6_PIMSM_V2
Benjamin Thery8229efd2008-12-10 16:30:15 -0800496 if (vifi == net->ipv6.mroute_reg_vif_num)
497 net->ipv6.mroute_reg_vif_num = -1;
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +0900498#endif
499
Benjamin Thery8229efd2008-12-10 16:30:15 -0800500 if (vifi + 1 == net->ipv6.maxvif) {
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900501 int tmp;
502 for (tmp = vifi - 1; tmp >= 0; tmp--) {
Benjamin Thery8229efd2008-12-10 16:30:15 -0800503 if (MIF_EXISTS(net, tmp))
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900504 break;
505 }
Benjamin Thery8229efd2008-12-10 16:30:15 -0800506 net->ipv6.maxvif = tmp + 1;
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900507 }
508
509 write_unlock_bh(&mrt_lock);
510
511 dev_set_allmulti(dev, -1);
512
Thomas Goff1d6e55f2009-01-27 22:39:59 -0800513 in6_dev = __in6_dev_get(dev);
514 if (in6_dev)
515 in6_dev->cnf.mc_forwarding--;
516
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900517 if (v->flags & MIFF_REGISTER)
Eric Dumazetc871e662009-10-28 04:48:11 +0000518 unregister_netdevice_queue(dev, head);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900519
520 dev_put(dev);
521 return 0;
522}
523
Benjamin Thery58701ad2008-12-10 16:22:34 -0800524static inline void ip6mr_cache_free(struct mfc6_cache *c)
525{
526 release_net(mfc6_net(c));
527 kmem_cache_free(mrt_cachep, c);
528}
529
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900530/* Destroy an unresolved cache entry, killing queued skbs
531 and reporting error to netlink readers.
532 */
533
534static void ip6mr_destroy_unres(struct mfc6_cache *c)
535{
536 struct sk_buff *skb;
Benjamin Thery8229efd2008-12-10 16:30:15 -0800537 struct net *net = mfc6_net(c);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900538
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;
578 ip6mr_destroy_unres(c);
579 }
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
602static void ip6mr_update_thresholds(struct mfc6_cache *cache, unsigned char *ttls)
603{
604 int vifi;
Benjamin Thery8229efd2008-12-10 16:30:15 -0800605 struct net *net = mfc6_net(cache);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900606
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 */
Benjamin Thery58701ad2008-12-10 16:22:34 -0800720static struct mfc6_cache *ip6mr_cache_alloc(struct net *net)
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;
Benjamin Thery58701ad2008-12-10 16:22:34 -0800726 mfc6_net_set(c, net);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900727 return c;
728}
729
Benjamin Thery58701ad2008-12-10 16:22:34 -0800730static struct mfc6_cache *ip6mr_cache_alloc_unres(struct net *net)
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900731{
Joe Perches36cbac52008-12-03 22:27:25 -0800732 struct mfc6_cache *c = kmem_cache_zalloc(mrt_cachep, GFP_ATOMIC);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900733 if (c == NULL)
734 return NULL;
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900735 skb_queue_head_init(&c->mfc_un.unres.unresolved);
736 c->mfc_un.unres.expires = jiffies + 10 * HZ;
Benjamin Thery58701ad2008-12-10 16:22:34 -0800737 mfc6_net_set(c, net);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900738 return c;
739}
740
741/*
742 * A cache entry has gone into a resolved state from queued
743 */
744
745static void ip6mr_cache_resolve(struct mfc6_cache *uc, struct mfc6_cache *c)
746{
747 struct sk_buff *skb;
748
749 /*
750 * Play the pending entries through our router
751 */
752
753 while((skb = __skb_dequeue(&uc->mfc_un.unres.unresolved))) {
754 if (ipv6_hdr(skb)->version == 0) {
755 int err;
756 struct nlmsghdr *nlh = (struct nlmsghdr *)skb_pull(skb, sizeof(struct ipv6hdr));
757
758 if (ip6mr_fill_mroute(skb, c, NLMSG_DATA(nlh)) > 0) {
YOSHIFUJI Hideaki549e0282008-04-05 22:17:39 +0900759 nlh->nlmsg_len = skb_tail_pointer(skb) - (u8 *)nlh;
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900760 } else {
761 nlh->nlmsg_type = NLMSG_ERROR;
762 nlh->nlmsg_len = NLMSG_LENGTH(sizeof(struct nlmsgerr));
763 skb_trim(skb, nlh->nlmsg_len);
764 ((struct nlmsgerr *)NLMSG_DATA(nlh))->error = -EMSGSIZE;
765 }
Benjamin Thery8229efd2008-12-10 16:30:15 -0800766 err = rtnl_unicast(skb, mfc6_net(uc), NETLINK_CB(skb).pid);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900767 } else
768 ip6_mr_forward(skb, c);
769 }
770}
771
772/*
773 * Bounce a cache query up to pim6sd. We could use netlink for this but pim6sd
774 * expects the following bizarre scheme.
775 *
776 * Called under mrt_lock.
777 */
778
Benjamin Thery8229efd2008-12-10 16:30:15 -0800779static int ip6mr_cache_report(struct net *net, struct sk_buff *pkt, mifi_t mifi,
780 int assert)
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900781{
782 struct sk_buff *skb;
783 struct mrt6msg *msg;
784 int ret;
785
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +0900786#ifdef CONFIG_IPV6_PIMSM_V2
787 if (assert == MRT6MSG_WHOLEPKT)
788 skb = skb_realloc_headroom(pkt, -skb_network_offset(pkt)
789 +sizeof(*msg));
790 else
791#endif
792 skb = alloc_skb(sizeof(struct ipv6hdr) + sizeof(*msg), GFP_ATOMIC);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900793
794 if (!skb)
795 return -ENOBUFS;
796
797 /* I suppose that internal messages
798 * do not require checksums */
799
800 skb->ip_summed = CHECKSUM_UNNECESSARY;
801
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +0900802#ifdef CONFIG_IPV6_PIMSM_V2
803 if (assert == MRT6MSG_WHOLEPKT) {
804 /* Ugly, but we have no choice with this interface.
805 Duplicate old header, fix length etc.
806 And all this only to mangle msg->im6_msgtype and
807 to set msg->im6_mbz to "mbz" :-)
808 */
809 skb_push(skb, -skb_network_offset(pkt));
810
811 skb_push(skb, sizeof(*msg));
812 skb_reset_transport_header(skb);
813 msg = (struct mrt6msg *)skb_transport_header(skb);
814 msg->im6_mbz = 0;
815 msg->im6_msgtype = MRT6MSG_WHOLEPKT;
Benjamin Thery8229efd2008-12-10 16:30:15 -0800816 msg->im6_mif = net->ipv6.mroute_reg_vif_num;
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +0900817 msg->im6_pad = 0;
818 ipv6_addr_copy(&msg->im6_src, &ipv6_hdr(pkt)->saddr);
819 ipv6_addr_copy(&msg->im6_dst, &ipv6_hdr(pkt)->daddr);
820
821 skb->ip_summed = CHECKSUM_UNNECESSARY;
822 } else
823#endif
824 {
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900825 /*
826 * Copy the IP header
827 */
828
829 skb_put(skb, sizeof(struct ipv6hdr));
830 skb_reset_network_header(skb);
831 skb_copy_to_linear_data(skb, ipv6_hdr(pkt), sizeof(struct ipv6hdr));
832
833 /*
834 * Add our header
835 */
836 skb_put(skb, sizeof(*msg));
837 skb_reset_transport_header(skb);
838 msg = (struct mrt6msg *)skb_transport_header(skb);
839
840 msg->im6_mbz = 0;
841 msg->im6_msgtype = assert;
Rami Rosen6ac7eb02008-04-10 12:40:10 +0300842 msg->im6_mif = mifi;
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900843 msg->im6_pad = 0;
844 ipv6_addr_copy(&msg->im6_src, &ipv6_hdr(pkt)->saddr);
845 ipv6_addr_copy(&msg->im6_dst, &ipv6_hdr(pkt)->daddr);
846
Eric Dumazetadf30902009-06-02 05:19:30 +0000847 skb_dst_set(skb, dst_clone(skb_dst(pkt)));
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900848 skb->ip_summed = CHECKSUM_UNNECESSARY;
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +0900849 }
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900850
Benjamin Thery8229efd2008-12-10 16:30:15 -0800851 if (net->ipv6.mroute6_sk == NULL) {
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900852 kfree_skb(skb);
853 return -EINVAL;
854 }
855
856 /*
857 * Deliver to user space multicast routing algorithms
858 */
Benjamin Thery8229efd2008-12-10 16:30:15 -0800859 ret = sock_queue_rcv_skb(net->ipv6.mroute6_sk, skb);
Benjamin Therybd91b8b2008-12-10 16:07:08 -0800860 if (ret < 0) {
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900861 if (net_ratelimit())
862 printk(KERN_WARNING "mroute6: pending queue full, dropping entries.\n");
863 kfree_skb(skb);
864 }
865
866 return ret;
867}
868
869/*
870 * Queue a packet for resolution. It gets locked cache entry!
871 */
872
873static int
Benjamin Thery8229efd2008-12-10 16:30:15 -0800874ip6mr_cache_unresolved(struct net *net, mifi_t mifi, struct sk_buff *skb)
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900875{
876 int err;
877 struct mfc6_cache *c;
878
879 spin_lock_bh(&mfc_unres_lock);
Patrick McHardyc476efb2010-05-11 14:40:48 +0200880 for (c = net->ipv6.mfc6_unres_queue; c; c = c->next) {
881 if (ipv6_addr_equal(&c->mf6c_mcastgrp, &ipv6_hdr(skb)->daddr) &&
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900882 ipv6_addr_equal(&c->mf6c_origin, &ipv6_hdr(skb)->saddr))
883 break;
884 }
885
886 if (c == NULL) {
887 /*
888 * Create a new entry if allowable
889 */
890
Benjamin Thery8229efd2008-12-10 16:30:15 -0800891 if (atomic_read(&net->ipv6.cache_resolve_queue_len) >= 10 ||
892 (c = ip6mr_cache_alloc_unres(net)) == NULL) {
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900893 spin_unlock_bh(&mfc_unres_lock);
894
895 kfree_skb(skb);
896 return -ENOBUFS;
897 }
898
899 /*
900 * Fill in the new cache entry
901 */
902 c->mf6c_parent = -1;
903 c->mf6c_origin = ipv6_hdr(skb)->saddr;
904 c->mf6c_mcastgrp = ipv6_hdr(skb)->daddr;
905
906 /*
907 * Reflect first query at pim6sd
908 */
Benjamin Thery8229efd2008-12-10 16:30:15 -0800909 err = ip6mr_cache_report(net, skb, mifi, MRT6MSG_NOCACHE);
910 if (err < 0) {
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900911 /* If the report failed throw the cache entry
912 out - Brad Parker
913 */
914 spin_unlock_bh(&mfc_unres_lock);
915
Benjamin Thery58701ad2008-12-10 16:22:34 -0800916 ip6mr_cache_free(c);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900917 kfree_skb(skb);
918 return err;
919 }
920
Benjamin Thery8229efd2008-12-10 16:30:15 -0800921 atomic_inc(&net->ipv6.cache_resolve_queue_len);
Patrick McHardyc476efb2010-05-11 14:40:48 +0200922 c->next = net->ipv6.mfc6_unres_queue;
923 net->ipv6.mfc6_unres_queue = c;
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900924
Patrick McHardyc476efb2010-05-11 14:40:48 +0200925 ipmr_do_expire_process(net);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900926 }
927
928 /*
929 * See if we can append the packet
930 */
931 if (c->mfc_un.unres.unresolved.qlen > 3) {
932 kfree_skb(skb);
933 err = -ENOBUFS;
934 } else {
935 skb_queue_tail(&c->mfc_un.unres.unresolved, skb);
936 err = 0;
937 }
938
939 spin_unlock_bh(&mfc_unres_lock);
940 return err;
941}
942
943/*
944 * MFC6 cache manipulation by user space
945 */
946
Benjamin Thery8229efd2008-12-10 16:30:15 -0800947static int ip6mr_mfc_delete(struct net *net, struct mf6cctl *mfc)
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900948{
949 int line;
950 struct mfc6_cache *c, **cp;
951
952 line = MFC6_HASH(&mfc->mf6cc_mcastgrp.sin6_addr, &mfc->mf6cc_origin.sin6_addr);
953
Benjamin Thery8229efd2008-12-10 16:30:15 -0800954 for (cp = &net->ipv6.mfc6_cache_array[line];
Benjamin Thery4a6258a2008-12-10 16:24:07 -0800955 (c = *cp) != NULL; cp = &c->next) {
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900956 if (ipv6_addr_equal(&c->mf6c_origin, &mfc->mf6cc_origin.sin6_addr) &&
957 ipv6_addr_equal(&c->mf6c_mcastgrp, &mfc->mf6cc_mcastgrp.sin6_addr)) {
958 write_lock_bh(&mrt_lock);
959 *cp = c->next;
960 write_unlock_bh(&mrt_lock);
961
Benjamin Thery58701ad2008-12-10 16:22:34 -0800962 ip6mr_cache_free(c);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900963 return 0;
964 }
965 }
966 return -ENOENT;
967}
968
969static int ip6mr_device_event(struct notifier_block *this,
970 unsigned long event, void *ptr)
971{
972 struct net_device *dev = ptr;
Benjamin Thery8229efd2008-12-10 16:30:15 -0800973 struct net *net = dev_net(dev);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900974 struct mif_device *v;
975 int ct;
Eric Dumazetc871e662009-10-28 04:48:11 +0000976 LIST_HEAD(list);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900977
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900978 if (event != NETDEV_UNREGISTER)
979 return NOTIFY_DONE;
980
Benjamin Thery8229efd2008-12-10 16:30:15 -0800981 v = &net->ipv6.vif6_table[0];
982 for (ct = 0; ct < net->ipv6.maxvif; ct++, v++) {
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900983 if (v->dev == dev)
Eric Dumazetc871e662009-10-28 04:48:11 +0000984 mif6_delete(net, ct, &list);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900985 }
Eric Dumazetc871e662009-10-28 04:48:11 +0000986 unregister_netdevice_many(&list);
987
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900988 return NOTIFY_DONE;
989}
990
991static struct notifier_block ip6_mr_notifier = {
992 .notifier_call = ip6mr_device_event
993};
994
995/*
996 * Setup for IP multicast routing
997 */
998
Benjamin Thery4e168802008-12-10 16:15:08 -0800999static int __net_init ip6mr_net_init(struct net *net)
1000{
1001 int err = 0;
Benjamin Thery4e168802008-12-10 16:15:08 -08001002 net->ipv6.vif6_table = kcalloc(MAXMIFS, sizeof(struct mif_device),
1003 GFP_KERNEL);
1004 if (!net->ipv6.vif6_table) {
1005 err = -ENOMEM;
1006 goto fail;
1007 }
Benjamin Thery4a6258a2008-12-10 16:24:07 -08001008
1009 /* Forwarding cache */
1010 net->ipv6.mfc6_cache_array = kcalloc(MFC6_LINES,
1011 sizeof(struct mfc6_cache *),
1012 GFP_KERNEL);
1013 if (!net->ipv6.mfc6_cache_array) {
1014 err = -ENOMEM;
1015 goto fail_mfc6_cache;
1016 }
Benjamin Thery950d5702008-12-10 16:29:24 -08001017
Patrick McHardyc476efb2010-05-11 14:40:48 +02001018 setup_timer(&net->ipv6.ipmr_expire_timer, ipmr_expire_process,
1019 (unsigned long)net);
1020
Benjamin Thery950d5702008-12-10 16:29:24 -08001021#ifdef CONFIG_IPV6_PIMSM_V2
1022 net->ipv6.mroute_reg_vif_num = -1;
1023#endif
Benjamin Thery8b90fc72008-12-10 16:29:48 -08001024
1025#ifdef CONFIG_PROC_FS
1026 err = -ENOMEM;
1027 if (!proc_net_fops_create(net, "ip6_mr_vif", 0, &ip6mr_vif_fops))
1028 goto proc_vif_fail;
1029 if (!proc_net_fops_create(net, "ip6_mr_cache", 0, &ip6mr_mfc_fops))
1030 goto proc_cache_fail;
1031#endif
Benjamin Thery4a6258a2008-12-10 16:24:07 -08001032 return 0;
1033
Benjamin Thery8b90fc72008-12-10 16:29:48 -08001034#ifdef CONFIG_PROC_FS
1035proc_cache_fail:
1036 proc_net_remove(net, "ip6_mr_vif");
1037proc_vif_fail:
1038 kfree(net->ipv6.mfc6_cache_array);
1039#endif
Benjamin Thery4a6258a2008-12-10 16:24:07 -08001040fail_mfc6_cache:
1041 kfree(net->ipv6.vif6_table);
Benjamin Thery4e168802008-12-10 16:15:08 -08001042fail:
1043 return err;
1044}
1045
1046static void __net_exit ip6mr_net_exit(struct net *net)
1047{
Benjamin Thery8b90fc72008-12-10 16:29:48 -08001048#ifdef CONFIG_PROC_FS
1049 proc_net_remove(net, "ip6_mr_cache");
1050 proc_net_remove(net, "ip6_mr_vif");
1051#endif
Patrick McHardyc476efb2010-05-11 14:40:48 +02001052 del_timer(&net->ipv6.ipmr_expire_timer);
Benjamin Thery8229efd2008-12-10 16:30:15 -08001053 mroute_clean_tables(net);
Benjamin Thery4a6258a2008-12-10 16:24:07 -08001054 kfree(net->ipv6.mfc6_cache_array);
Benjamin Thery4e168802008-12-10 16:15:08 -08001055 kfree(net->ipv6.vif6_table);
1056}
1057
1058static struct pernet_operations ip6mr_net_ops = {
1059 .init = ip6mr_net_init,
1060 .exit = ip6mr_net_exit,
1061};
1062
Wang Chen623d1a12008-07-03 12:13:30 +08001063int __init ip6_mr_init(void)
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001064{
Wang Chen623d1a12008-07-03 12:13:30 +08001065 int err;
1066
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001067 mrt_cachep = kmem_cache_create("ip6_mrt_cache",
1068 sizeof(struct mfc6_cache),
1069 0, SLAB_HWCACHE_ALIGN,
1070 NULL);
1071 if (!mrt_cachep)
Wang Chen623d1a12008-07-03 12:13:30 +08001072 return -ENOMEM;
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001073
Benjamin Thery4e168802008-12-10 16:15:08 -08001074 err = register_pernet_subsys(&ip6mr_net_ops);
1075 if (err)
1076 goto reg_pernet_fail;
1077
Wang Chen623d1a12008-07-03 12:13:30 +08001078 err = register_netdevice_notifier(&ip6_mr_notifier);
1079 if (err)
1080 goto reg_notif_fail;
Tom Goff403dbb92009-06-14 03:16:13 -07001081#ifdef CONFIG_IPV6_PIMSM_V2
1082 if (inet6_add_protocol(&pim6_protocol, IPPROTO_PIM) < 0) {
1083 printk(KERN_ERR "ip6_mr_init: can't add PIM protocol\n");
1084 err = -EAGAIN;
1085 goto add_proto_fail;
1086 }
1087#endif
Wang Chen623d1a12008-07-03 12:13:30 +08001088 return 0;
Tom Goff403dbb92009-06-14 03:16:13 -07001089#ifdef CONFIG_IPV6_PIMSM_V2
1090add_proto_fail:
1091 unregister_netdevice_notifier(&ip6_mr_notifier);
1092#endif
Benjamin Thery87b30a62008-11-10 16:34:11 -08001093reg_notif_fail:
Benjamin Thery4e168802008-12-10 16:15:08 -08001094 unregister_pernet_subsys(&ip6mr_net_ops);
1095reg_pernet_fail:
Benjamin Thery87b30a62008-11-10 16:34:11 -08001096 kmem_cache_destroy(mrt_cachep);
Wang Chen623d1a12008-07-03 12:13:30 +08001097 return err;
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001098}
1099
Wang Chen623d1a12008-07-03 12:13:30 +08001100void ip6_mr_cleanup(void)
1101{
Wang Chen623d1a12008-07-03 12:13:30 +08001102 unregister_netdevice_notifier(&ip6_mr_notifier);
Benjamin Thery4e168802008-12-10 16:15:08 -08001103 unregister_pernet_subsys(&ip6mr_net_ops);
Wang Chen623d1a12008-07-03 12:13:30 +08001104 kmem_cache_destroy(mrt_cachep);
1105}
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001106
Benjamin Thery8229efd2008-12-10 16:30:15 -08001107static int ip6mr_mfc_add(struct net *net, struct mf6cctl *mfc, int mrtsock)
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001108{
1109 int line;
1110 struct mfc6_cache *uc, *c, **cp;
Rami Rosen6ac7eb02008-04-10 12:40:10 +03001111 unsigned char ttls[MAXMIFS];
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001112 int i;
1113
Patrick McHardya50436f2010-03-17 06:04:14 +00001114 if (mfc->mf6cc_parent >= MAXMIFS)
1115 return -ENFILE;
1116
Rami Rosen6ac7eb02008-04-10 12:40:10 +03001117 memset(ttls, 255, MAXMIFS);
1118 for (i = 0; i < MAXMIFS; i++) {
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001119 if (IF_ISSET(i, &mfc->mf6cc_ifset))
1120 ttls[i] = 1;
1121
1122 }
1123
1124 line = MFC6_HASH(&mfc->mf6cc_mcastgrp.sin6_addr, &mfc->mf6cc_origin.sin6_addr);
1125
Benjamin Thery8229efd2008-12-10 16:30:15 -08001126 for (cp = &net->ipv6.mfc6_cache_array[line];
Benjamin Thery4a6258a2008-12-10 16:24:07 -08001127 (c = *cp) != NULL; cp = &c->next) {
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001128 if (ipv6_addr_equal(&c->mf6c_origin, &mfc->mf6cc_origin.sin6_addr) &&
1129 ipv6_addr_equal(&c->mf6c_mcastgrp, &mfc->mf6cc_mcastgrp.sin6_addr))
1130 break;
1131 }
1132
1133 if (c != NULL) {
1134 write_lock_bh(&mrt_lock);
1135 c->mf6c_parent = mfc->mf6cc_parent;
1136 ip6mr_update_thresholds(c, ttls);
1137 if (!mrtsock)
1138 c->mfc_flags |= MFC_STATIC;
1139 write_unlock_bh(&mrt_lock);
1140 return 0;
1141 }
1142
1143 if (!ipv6_addr_is_multicast(&mfc->mf6cc_mcastgrp.sin6_addr))
1144 return -EINVAL;
1145
Benjamin Thery8229efd2008-12-10 16:30:15 -08001146 c = ip6mr_cache_alloc(net);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001147 if (c == NULL)
1148 return -ENOMEM;
1149
1150 c->mf6c_origin = mfc->mf6cc_origin.sin6_addr;
1151 c->mf6c_mcastgrp = mfc->mf6cc_mcastgrp.sin6_addr;
1152 c->mf6c_parent = mfc->mf6cc_parent;
1153 ip6mr_update_thresholds(c, ttls);
1154 if (!mrtsock)
1155 c->mfc_flags |= MFC_STATIC;
1156
1157 write_lock_bh(&mrt_lock);
Benjamin Thery8229efd2008-12-10 16:30:15 -08001158 c->next = net->ipv6.mfc6_cache_array[line];
1159 net->ipv6.mfc6_cache_array[line] = c;
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001160 write_unlock_bh(&mrt_lock);
1161
1162 /*
1163 * Check to see if we resolved a queued list. If so we
1164 * need to send on the frames and tidy up.
1165 */
1166 spin_lock_bh(&mfc_unres_lock);
Patrick McHardyc476efb2010-05-11 14:40:48 +02001167 for (cp = &net->ipv6.mfc6_unres_queue; (uc = *cp) != NULL;
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001168 cp = &uc->next) {
Patrick McHardyc476efb2010-05-11 14:40:48 +02001169 if (ipv6_addr_equal(&uc->mf6c_origin, &c->mf6c_origin) &&
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001170 ipv6_addr_equal(&uc->mf6c_mcastgrp, &c->mf6c_mcastgrp)) {
1171 *cp = uc->next;
Benjamin Thery8229efd2008-12-10 16:30:15 -08001172 atomic_dec(&net->ipv6.cache_resolve_queue_len);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001173 break;
1174 }
1175 }
Patrick McHardyc476efb2010-05-11 14:40:48 +02001176 if (net->ipv6.mfc6_unres_queue == NULL)
1177 del_timer(&net->ipv6.ipmr_expire_timer);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001178 spin_unlock_bh(&mfc_unres_lock);
1179
1180 if (uc) {
1181 ip6mr_cache_resolve(uc, c);
Benjamin Thery58701ad2008-12-10 16:22:34 -08001182 ip6mr_cache_free(uc);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001183 }
1184 return 0;
1185}
1186
1187/*
1188 * Close the multicast socket, and clear the vif tables etc
1189 */
1190
Benjamin Thery8229efd2008-12-10 16:30:15 -08001191static void mroute_clean_tables(struct net *net)
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001192{
1193 int i;
Eric Dumazetc871e662009-10-28 04:48:11 +00001194 LIST_HEAD(list);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001195
1196 /*
1197 * Shut down all active vif entries
1198 */
Benjamin Thery8229efd2008-12-10 16:30:15 -08001199 for (i = 0; i < net->ipv6.maxvif; i++) {
1200 if (!(net->ipv6.vif6_table[i].flags & VIFF_STATIC))
Eric Dumazetc871e662009-10-28 04:48:11 +00001201 mif6_delete(net, i, &list);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001202 }
Eric Dumazetc871e662009-10-28 04:48:11 +00001203 unregister_netdevice_many(&list);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001204
1205 /*
1206 * Wipe the cache
1207 */
Benjamin Thery4a6258a2008-12-10 16:24:07 -08001208 for (i = 0; i < MFC6_LINES; i++) {
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001209 struct mfc6_cache *c, **cp;
1210
Benjamin Thery8229efd2008-12-10 16:30:15 -08001211 cp = &net->ipv6.mfc6_cache_array[i];
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001212 while ((c = *cp) != NULL) {
1213 if (c->mfc_flags & MFC_STATIC) {
1214 cp = &c->next;
1215 continue;
1216 }
1217 write_lock_bh(&mrt_lock);
1218 *cp = c->next;
1219 write_unlock_bh(&mrt_lock);
1220
Benjamin Thery58701ad2008-12-10 16:22:34 -08001221 ip6mr_cache_free(c);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001222 }
1223 }
1224
Benjamin Thery8229efd2008-12-10 16:30:15 -08001225 if (atomic_read(&net->ipv6.cache_resolve_queue_len) != 0) {
Benjamin Thery4045e572008-12-10 16:27:21 -08001226 struct mfc6_cache *c, **cp;
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001227
1228 spin_lock_bh(&mfc_unres_lock);
Patrick McHardyc476efb2010-05-11 14:40:48 +02001229 cp = &net->ipv6.mfc6_unres_queue;
Benjamin Thery4045e572008-12-10 16:27:21 -08001230 while ((c = *cp) != NULL) {
Benjamin Thery4045e572008-12-10 16:27:21 -08001231 *cp = c->next;
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001232 ip6mr_destroy_unres(c);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001233 }
1234 spin_unlock_bh(&mfc_unres_lock);
1235 }
1236}
1237
1238static int ip6mr_sk_init(struct sock *sk)
1239{
1240 int err = 0;
Benjamin Thery8229efd2008-12-10 16:30:15 -08001241 struct net *net = sock_net(sk);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001242
1243 rtnl_lock();
1244 write_lock_bh(&mrt_lock);
Thomas Goff1d6e55f2009-01-27 22:39:59 -08001245 if (likely(net->ipv6.mroute6_sk == NULL)) {
Benjamin Thery8229efd2008-12-10 16:30:15 -08001246 net->ipv6.mroute6_sk = sk;
Thomas Goff1d6e55f2009-01-27 22:39:59 -08001247 net->ipv6.devconf_all->mc_forwarding++;
1248 }
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001249 else
1250 err = -EADDRINUSE;
1251 write_unlock_bh(&mrt_lock);
1252
1253 rtnl_unlock();
1254
1255 return err;
1256}
1257
1258int ip6mr_sk_done(struct sock *sk)
1259{
1260 int err = 0;
Benjamin Thery8229efd2008-12-10 16:30:15 -08001261 struct net *net = sock_net(sk);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001262
1263 rtnl_lock();
Benjamin Thery8229efd2008-12-10 16:30:15 -08001264 if (sk == net->ipv6.mroute6_sk) {
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001265 write_lock_bh(&mrt_lock);
Benjamin Thery8229efd2008-12-10 16:30:15 -08001266 net->ipv6.mroute6_sk = NULL;
Thomas Goff1d6e55f2009-01-27 22:39:59 -08001267 net->ipv6.devconf_all->mc_forwarding--;
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001268 write_unlock_bh(&mrt_lock);
1269
Benjamin Thery8229efd2008-12-10 16:30:15 -08001270 mroute_clean_tables(net);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001271 } else
1272 err = -EACCES;
1273 rtnl_unlock();
1274
1275 return err;
1276}
1277
1278/*
1279 * Socket options and virtual interface manipulation. The whole
1280 * virtual interface system is a complete heap, but unfortunately
1281 * that's how BSD mrouted happens to think. Maybe one day with a proper
1282 * MOSPF/PIM router set up we can clean this up.
1283 */
1284
David S. Millerb7058842009-09-30 16:12:20 -07001285int ip6_mroute_setsockopt(struct sock *sk, int optname, char __user *optval, unsigned int optlen)
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001286{
1287 int ret;
1288 struct mif6ctl vif;
1289 struct mf6cctl mfc;
1290 mifi_t mifi;
Benjamin Thery8229efd2008-12-10 16:30:15 -08001291 struct net *net = sock_net(sk);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001292
1293 if (optname != MRT6_INIT) {
Benjamin Thery8229efd2008-12-10 16:30:15 -08001294 if (sk != net->ipv6.mroute6_sk && !capable(CAP_NET_ADMIN))
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001295 return -EACCES;
1296 }
1297
1298 switch (optname) {
1299 case MRT6_INIT:
1300 if (sk->sk_type != SOCK_RAW ||
Eric Dumazetc720c7e2009-10-15 06:30:45 +00001301 inet_sk(sk)->inet_num != IPPROTO_ICMPV6)
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001302 return -EOPNOTSUPP;
1303 if (optlen < sizeof(int))
1304 return -EINVAL;
1305
1306 return ip6mr_sk_init(sk);
1307
1308 case MRT6_DONE:
1309 return ip6mr_sk_done(sk);
1310
1311 case MRT6_ADD_MIF:
1312 if (optlen < sizeof(vif))
1313 return -EINVAL;
1314 if (copy_from_user(&vif, optval, sizeof(vif)))
1315 return -EFAULT;
Rami Rosen6ac7eb02008-04-10 12:40:10 +03001316 if (vif.mif6c_mifi >= MAXMIFS)
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001317 return -ENFILE;
1318 rtnl_lock();
Benjamin Thery8229efd2008-12-10 16:30:15 -08001319 ret = mif6_add(net, &vif, sk == net->ipv6.mroute6_sk);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001320 rtnl_unlock();
1321 return ret;
1322
1323 case MRT6_DEL_MIF:
1324 if (optlen < sizeof(mifi_t))
1325 return -EINVAL;
1326 if (copy_from_user(&mifi, optval, sizeof(mifi_t)))
1327 return -EFAULT;
1328 rtnl_lock();
Eric Dumazetc871e662009-10-28 04:48:11 +00001329 ret = mif6_delete(net, mifi, NULL);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001330 rtnl_unlock();
1331 return ret;
1332
1333 /*
1334 * Manipulate the forwarding caches. These live
1335 * in a sort of kernel/user symbiosis.
1336 */
1337 case MRT6_ADD_MFC:
1338 case MRT6_DEL_MFC:
1339 if (optlen < sizeof(mfc))
1340 return -EINVAL;
1341 if (copy_from_user(&mfc, optval, sizeof(mfc)))
1342 return -EFAULT;
1343 rtnl_lock();
1344 if (optname == MRT6_DEL_MFC)
Benjamin Thery8229efd2008-12-10 16:30:15 -08001345 ret = ip6mr_mfc_delete(net, &mfc);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001346 else
Benjamin Thery8229efd2008-12-10 16:30:15 -08001347 ret = ip6mr_mfc_add(net, &mfc,
1348 sk == net->ipv6.mroute6_sk);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001349 rtnl_unlock();
1350 return ret;
1351
1352 /*
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +09001353 * Control PIM assert (to activate pim will activate assert)
1354 */
1355 case MRT6_ASSERT:
1356 {
1357 int v;
1358 if (get_user(v, (int __user *)optval))
1359 return -EFAULT;
Benjamin Thery8229efd2008-12-10 16:30:15 -08001360 net->ipv6.mroute_do_assert = !!v;
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +09001361 return 0;
1362 }
1363
1364#ifdef CONFIG_IPV6_PIMSM_V2
1365 case MRT6_PIM:
1366 {
YOSHIFUJI Hideakia9f83bf2008-04-10 15:41:28 +09001367 int v;
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +09001368 if (get_user(v, (int __user *)optval))
1369 return -EFAULT;
1370 v = !!v;
1371 rtnl_lock();
1372 ret = 0;
Benjamin Thery8229efd2008-12-10 16:30:15 -08001373 if (v != net->ipv6.mroute_do_pim) {
1374 net->ipv6.mroute_do_pim = v;
1375 net->ipv6.mroute_do_assert = v;
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +09001376 }
1377 rtnl_unlock();
1378 return ret;
1379 }
1380
1381#endif
1382 /*
Rami Rosen7d120c52008-04-23 14:35:13 +03001383 * Spurious command, or MRT6_VERSION which you cannot
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001384 * set.
1385 */
1386 default:
1387 return -ENOPROTOOPT;
1388 }
1389}
1390
1391/*
1392 * Getsock opt support for the multicast routing system.
1393 */
1394
1395int ip6_mroute_getsockopt(struct sock *sk, int optname, char __user *optval,
1396 int __user *optlen)
1397{
1398 int olr;
1399 int val;
Benjamin Thery8229efd2008-12-10 16:30:15 -08001400 struct net *net = sock_net(sk);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001401
1402 switch (optname) {
1403 case MRT6_VERSION:
1404 val = 0x0305;
1405 break;
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +09001406#ifdef CONFIG_IPV6_PIMSM_V2
1407 case MRT6_PIM:
Benjamin Thery8229efd2008-12-10 16:30:15 -08001408 val = net->ipv6.mroute_do_pim;
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +09001409 break;
1410#endif
1411 case MRT6_ASSERT:
Benjamin Thery8229efd2008-12-10 16:30:15 -08001412 val = net->ipv6.mroute_do_assert;
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +09001413 break;
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001414 default:
1415 return -ENOPROTOOPT;
1416 }
1417
1418 if (get_user(olr, optlen))
1419 return -EFAULT;
1420
1421 olr = min_t(int, olr, sizeof(int));
1422 if (olr < 0)
1423 return -EINVAL;
1424
1425 if (put_user(olr, optlen))
1426 return -EFAULT;
1427 if (copy_to_user(optval, &val, olr))
1428 return -EFAULT;
1429 return 0;
1430}
1431
1432/*
1433 * The IP multicast ioctl support routines.
1434 */
1435
1436int ip6mr_ioctl(struct sock *sk, int cmd, void __user *arg)
1437{
1438 struct sioc_sg_req6 sr;
1439 struct sioc_mif_req6 vr;
1440 struct mif_device *vif;
1441 struct mfc6_cache *c;
Benjamin Thery8229efd2008-12-10 16:30:15 -08001442 struct net *net = sock_net(sk);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001443
1444 switch (cmd) {
1445 case SIOCGETMIFCNT_IN6:
1446 if (copy_from_user(&vr, arg, sizeof(vr)))
1447 return -EFAULT;
Benjamin Thery8229efd2008-12-10 16:30:15 -08001448 if (vr.mifi >= net->ipv6.maxvif)
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001449 return -EINVAL;
1450 read_lock(&mrt_lock);
Benjamin Thery8229efd2008-12-10 16:30:15 -08001451 vif = &net->ipv6.vif6_table[vr.mifi];
1452 if (MIF_EXISTS(net, vr.mifi)) {
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001453 vr.icount = vif->pkt_in;
1454 vr.ocount = vif->pkt_out;
1455 vr.ibytes = vif->bytes_in;
1456 vr.obytes = vif->bytes_out;
1457 read_unlock(&mrt_lock);
1458
1459 if (copy_to_user(arg, &vr, sizeof(vr)))
1460 return -EFAULT;
1461 return 0;
1462 }
1463 read_unlock(&mrt_lock);
1464 return -EADDRNOTAVAIL;
1465 case SIOCGETSGCNT_IN6:
1466 if (copy_from_user(&sr, arg, sizeof(sr)))
1467 return -EFAULT;
1468
1469 read_lock(&mrt_lock);
Benjamin Thery8229efd2008-12-10 16:30:15 -08001470 c = ip6mr_cache_find(net, &sr.src.sin6_addr, &sr.grp.sin6_addr);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001471 if (c) {
1472 sr.pktcnt = c->mfc_un.res.pkt;
1473 sr.bytecnt = c->mfc_un.res.bytes;
1474 sr.wrong_if = c->mfc_un.res.wrong_if;
1475 read_unlock(&mrt_lock);
1476
1477 if (copy_to_user(arg, &sr, sizeof(sr)))
1478 return -EFAULT;
1479 return 0;
1480 }
1481 read_unlock(&mrt_lock);
1482 return -EADDRNOTAVAIL;
1483 default:
1484 return -ENOIOCTLCMD;
1485 }
1486}
1487
1488
1489static inline int ip6mr_forward2_finish(struct sk_buff *skb)
1490{
Eric Dumazetadf30902009-06-02 05:19:30 +00001491 IP6_INC_STATS_BH(dev_net(skb_dst(skb)->dev), ip6_dst_idev(skb_dst(skb)),
Denis V. Lunev483a47d2008-10-08 11:09:27 -07001492 IPSTATS_MIB_OUTFORWDATAGRAMS);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001493 return dst_output(skb);
1494}
1495
1496/*
1497 * Processing handlers for ip6mr_forward
1498 */
1499
1500static int ip6mr_forward2(struct sk_buff *skb, struct mfc6_cache *c, int vifi)
1501{
1502 struct ipv6hdr *ipv6h;
Benjamin Thery8229efd2008-12-10 16:30:15 -08001503 struct net *net = mfc6_net(c);
1504 struct mif_device *vif = &net->ipv6.vif6_table[vifi];
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001505 struct net_device *dev;
1506 struct dst_entry *dst;
1507 struct flowi fl;
1508
1509 if (vif->dev == NULL)
1510 goto out_free;
1511
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +09001512#ifdef CONFIG_IPV6_PIMSM_V2
1513 if (vif->flags & MIFF_REGISTER) {
1514 vif->pkt_out++;
1515 vif->bytes_out += skb->len;
Pavel Emelyanovdc58c782008-05-21 14:17:54 -07001516 vif->dev->stats.tx_bytes += skb->len;
1517 vif->dev->stats.tx_packets++;
Benjamin Thery8229efd2008-12-10 16:30:15 -08001518 ip6mr_cache_report(net, skb, vifi, MRT6MSG_WHOLEPKT);
Ilpo Järvinen8da73b72008-12-14 23:15:49 -08001519 goto out_free;
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +09001520 }
1521#endif
1522
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001523 ipv6h = ipv6_hdr(skb);
1524
1525 fl = (struct flowi) {
1526 .oif = vif->link,
1527 .nl_u = { .ip6_u =
1528 { .daddr = ipv6h->daddr, }
1529 }
1530 };
1531
Benjamin Thery8229efd2008-12-10 16:30:15 -08001532 dst = ip6_route_output(net, NULL, &fl);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001533 if (!dst)
1534 goto out_free;
1535
Eric Dumazetadf30902009-06-02 05:19:30 +00001536 skb_dst_drop(skb);
1537 skb_dst_set(skb, dst);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001538
1539 /*
1540 * RFC1584 teaches, that DVMRP/PIM router must deliver packets locally
1541 * not only before forwarding, but after forwarding on all output
1542 * interfaces. It is clear, if mrouter runs a multicasting
1543 * program, it should receive packets not depending to what interface
1544 * program is joined.
1545 * If we will not make it, the program will have to join on all
1546 * interfaces. On the other hand, multihoming host (or router, but
1547 * not mrouter) cannot join to more than one interface - it will
1548 * result in receiving multiple packets.
1549 */
1550 dev = vif->dev;
1551 skb->dev = dev;
1552 vif->pkt_out++;
1553 vif->bytes_out += skb->len;
1554
1555 /* We are about to write */
1556 /* XXX: extension headers? */
1557 if (skb_cow(skb, sizeof(*ipv6h) + LL_RESERVED_SPACE(dev)))
1558 goto out_free;
1559
1560 ipv6h = ipv6_hdr(skb);
1561 ipv6h->hop_limit--;
1562
1563 IP6CB(skb)->flags |= IP6SKB_FORWARDED;
1564
Jan Engelhardtb2e0b382010-03-23 04:09:07 +01001565 return NF_HOOK(NFPROTO_IPV6, NF_INET_FORWARD, skb, skb->dev, dev,
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001566 ip6mr_forward2_finish);
1567
1568out_free:
1569 kfree_skb(skb);
1570 return 0;
1571}
1572
1573static int ip6mr_find_vif(struct net_device *dev)
1574{
Benjamin Thery8229efd2008-12-10 16:30:15 -08001575 struct net *net = dev_net(dev);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001576 int ct;
Benjamin Thery8229efd2008-12-10 16:30:15 -08001577 for (ct = net->ipv6.maxvif - 1; ct >= 0; ct--) {
1578 if (net->ipv6.vif6_table[ct].dev == dev)
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001579 break;
1580 }
1581 return ct;
1582}
1583
1584static int ip6_mr_forward(struct sk_buff *skb, struct mfc6_cache *cache)
1585{
1586 int psend = -1;
1587 int vif, ct;
Benjamin Thery8229efd2008-12-10 16:30:15 -08001588 struct net *net = mfc6_net(cache);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001589
1590 vif = cache->mf6c_parent;
1591 cache->mfc_un.res.pkt++;
1592 cache->mfc_un.res.bytes += skb->len;
1593
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +09001594 /*
1595 * Wrong interface: drop packet and (maybe) send PIM assert.
1596 */
Benjamin Thery8229efd2008-12-10 16:30:15 -08001597 if (net->ipv6.vif6_table[vif].dev != skb->dev) {
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +09001598 int true_vifi;
1599
1600 cache->mfc_un.res.wrong_if++;
1601 true_vifi = ip6mr_find_vif(skb->dev);
1602
Benjamin Thery8229efd2008-12-10 16:30:15 -08001603 if (true_vifi >= 0 && net->ipv6.mroute_do_assert &&
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +09001604 /* pimsm uses asserts, when switching from RPT to SPT,
1605 so that we cannot check that packet arrived on an oif.
1606 It is bad, but otherwise we would need to move pretty
1607 large chunk of pimd to kernel. Ough... --ANK
1608 */
Benjamin Thery8229efd2008-12-10 16:30:15 -08001609 (net->ipv6.mroute_do_pim ||
Benjamin Therya21f3f92008-12-10 16:28:44 -08001610 cache->mfc_un.res.ttls[true_vifi] < 255) &&
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +09001611 time_after(jiffies,
1612 cache->mfc_un.res.last_assert + MFC_ASSERT_THRESH)) {
1613 cache->mfc_un.res.last_assert = jiffies;
Benjamin Thery8229efd2008-12-10 16:30:15 -08001614 ip6mr_cache_report(net, skb, true_vifi, MRT6MSG_WRONGMIF);
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +09001615 }
1616 goto dont_forward;
1617 }
1618
Benjamin Thery8229efd2008-12-10 16:30:15 -08001619 net->ipv6.vif6_table[vif].pkt_in++;
1620 net->ipv6.vif6_table[vif].bytes_in += skb->len;
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001621
1622 /*
1623 * Forward the frame
1624 */
1625 for (ct = cache->mfc_un.res.maxvif - 1; ct >= cache->mfc_un.res.minvif; ct--) {
1626 if (ipv6_hdr(skb)->hop_limit > cache->mfc_un.res.ttls[ct]) {
1627 if (psend != -1) {
1628 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
1629 if (skb2)
1630 ip6mr_forward2(skb2, cache, psend);
1631 }
1632 psend = ct;
1633 }
1634 }
1635 if (psend != -1) {
1636 ip6mr_forward2(skb, cache, psend);
1637 return 0;
1638 }
1639
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +09001640dont_forward:
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001641 kfree_skb(skb);
1642 return 0;
1643}
1644
1645
1646/*
1647 * Multicast packets for forwarding arrive here
1648 */
1649
1650int ip6_mr_input(struct sk_buff *skb)
1651{
1652 struct mfc6_cache *cache;
Benjamin Thery8229efd2008-12-10 16:30:15 -08001653 struct net *net = dev_net(skb->dev);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001654
1655 read_lock(&mrt_lock);
Benjamin Thery8229efd2008-12-10 16:30:15 -08001656 cache = ip6mr_cache_find(net,
1657 &ipv6_hdr(skb)->saddr, &ipv6_hdr(skb)->daddr);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001658
1659 /*
1660 * No usable cache entry
1661 */
1662 if (cache == NULL) {
1663 int vif;
1664
1665 vif = ip6mr_find_vif(skb->dev);
1666 if (vif >= 0) {
Benjamin Thery8229efd2008-12-10 16:30:15 -08001667 int err = ip6mr_cache_unresolved(net, vif, skb);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001668 read_unlock(&mrt_lock);
1669
1670 return err;
1671 }
1672 read_unlock(&mrt_lock);
1673 kfree_skb(skb);
1674 return -ENODEV;
1675 }
1676
1677 ip6_mr_forward(skb, cache);
1678
1679 read_unlock(&mrt_lock);
1680
1681 return 0;
1682}
1683
1684
1685static int
1686ip6mr_fill_mroute(struct sk_buff *skb, struct mfc6_cache *c, struct rtmsg *rtm)
1687{
1688 int ct;
1689 struct rtnexthop *nhp;
Benjamin Thery8229efd2008-12-10 16:30:15 -08001690 struct net *net = mfc6_net(c);
YOSHIFUJI Hideaki549e0282008-04-05 22:17:39 +09001691 u8 *b = skb_tail_pointer(skb);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001692 struct rtattr *mp_head;
1693
Nicolas Dichtel74381892010-03-25 23:45:35 +00001694 /* If cache is unresolved, don't try to parse IIF and OIF */
1695 if (c->mf6c_parent > MAXMIFS)
1696 return -ENOENT;
1697
1698 if (MIF_EXISTS(net, c->mf6c_parent))
1699 RTA_PUT(skb, RTA_IIF, 4, &net->ipv6.vif6_table[c->mf6c_parent].dev->ifindex);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001700
1701 mp_head = (struct rtattr *)skb_put(skb, RTA_LENGTH(0));
1702
1703 for (ct = c->mfc_un.res.minvif; ct < c->mfc_un.res.maxvif; ct++) {
Nicolas Dichtel74381892010-03-25 23:45:35 +00001704 if (MIF_EXISTS(net, ct) && c->mfc_un.res.ttls[ct] < 255) {
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001705 if (skb_tailroom(skb) < RTA_ALIGN(RTA_ALIGN(sizeof(*nhp)) + 4))
1706 goto rtattr_failure;
1707 nhp = (struct rtnexthop *)skb_put(skb, RTA_ALIGN(sizeof(*nhp)));
1708 nhp->rtnh_flags = 0;
1709 nhp->rtnh_hops = c->mfc_un.res.ttls[ct];
Benjamin Thery8229efd2008-12-10 16:30:15 -08001710 nhp->rtnh_ifindex = net->ipv6.vif6_table[ct].dev->ifindex;
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001711 nhp->rtnh_len = sizeof(*nhp);
1712 }
1713 }
1714 mp_head->rta_type = RTA_MULTIPATH;
YOSHIFUJI Hideaki549e0282008-04-05 22:17:39 +09001715 mp_head->rta_len = skb_tail_pointer(skb) - (u8 *)mp_head;
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001716 rtm->rtm_type = RTN_MULTICAST;
1717 return 1;
1718
1719rtattr_failure:
1720 nlmsg_trim(skb, b);
1721 return -EMSGSIZE;
1722}
1723
Benjamin Thery8229efd2008-12-10 16:30:15 -08001724int ip6mr_get_route(struct net *net,
1725 struct sk_buff *skb, struct rtmsg *rtm, int nowait)
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001726{
1727 int err;
1728 struct mfc6_cache *cache;
Eric Dumazetadf30902009-06-02 05:19:30 +00001729 struct rt6_info *rt = (struct rt6_info *)skb_dst(skb);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001730
1731 read_lock(&mrt_lock);
Benjamin Thery8229efd2008-12-10 16:30:15 -08001732 cache = ip6mr_cache_find(net, &rt->rt6i_src.addr, &rt->rt6i_dst.addr);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001733
1734 if (!cache) {
1735 struct sk_buff *skb2;
1736 struct ipv6hdr *iph;
1737 struct net_device *dev;
1738 int vif;
1739
1740 if (nowait) {
1741 read_unlock(&mrt_lock);
1742 return -EAGAIN;
1743 }
1744
1745 dev = skb->dev;
1746 if (dev == NULL || (vif = ip6mr_find_vif(dev)) < 0) {
1747 read_unlock(&mrt_lock);
1748 return -ENODEV;
1749 }
1750
1751 /* really correct? */
1752 skb2 = alloc_skb(sizeof(struct ipv6hdr), GFP_ATOMIC);
1753 if (!skb2) {
1754 read_unlock(&mrt_lock);
1755 return -ENOMEM;
1756 }
1757
1758 skb_reset_transport_header(skb2);
1759
1760 skb_put(skb2, sizeof(struct ipv6hdr));
1761 skb_reset_network_header(skb2);
1762
1763 iph = ipv6_hdr(skb2);
1764 iph->version = 0;
1765 iph->priority = 0;
1766 iph->flow_lbl[0] = 0;
1767 iph->flow_lbl[1] = 0;
1768 iph->flow_lbl[2] = 0;
1769 iph->payload_len = 0;
1770 iph->nexthdr = IPPROTO_NONE;
1771 iph->hop_limit = 0;
1772 ipv6_addr_copy(&iph->saddr, &rt->rt6i_src.addr);
1773 ipv6_addr_copy(&iph->daddr, &rt->rt6i_dst.addr);
1774
Benjamin Thery8229efd2008-12-10 16:30:15 -08001775 err = ip6mr_cache_unresolved(net, vif, skb2);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001776 read_unlock(&mrt_lock);
1777
1778 return err;
1779 }
1780
1781 if (!nowait && (rtm->rtm_flags&RTM_F_NOTIFY))
1782 cache->mfc_flags |= MFC_NOTIFY;
1783
1784 err = ip6mr_fill_mroute(skb, cache, rtm);
1785 read_unlock(&mrt_lock);
1786 return err;
1787}
1788