]>
Commit | Line | Data |
---|---|---|
7bc570c8 YH |
1 | /* |
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> | |
7bc570c8 YH |
30 | #include <linux/inet.h> |
31 | #include <linux/netdevice.h> | |
32 | #include <linux/inetdevice.h> | |
7bc570c8 YH |
33 | #include <linux/proc_fs.h> |
34 | #include <linux/seq_file.h> | |
7bc570c8 | 35 | #include <linux/init.h> |
5a0e3ad6 | 36 | #include <linux/slab.h> |
7bc570c8 YH |
37 | #include <net/protocol.h> |
38 | #include <linux/skbuff.h> | |
39 | #include <net/sock.h> | |
7bc570c8 | 40 | #include <net/raw.h> |
7bc570c8 YH |
41 | #include <linux/notifier.h> |
42 | #include <linux/if_arp.h> | |
7bc570c8 YH |
43 | #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> | |
14fb64e1 | 49 | #include <linux/pim.h> |
7bc570c8 YH |
50 | #include <net/addrconf.h> |
51 | #include <linux/netfilter_ipv6.h> | |
5d6e430d | 52 | #include <net/ip6_checksum.h> |
7bc570c8 | 53 | |
7bc570c8 YH |
54 | /* Big lock, protecting vif table, mrt cache and mroute socket state. |
55 | Note that the changes are semaphored via rtnl_lock. | |
56 | */ | |
57 | ||
58 | static DEFINE_RWLOCK(mrt_lock); | |
59 | ||
60 | /* | |
61 | * Multicast router control variables | |
62 | */ | |
63 | ||
4e16880c | 64 | #define MIF_EXISTS(_net, _idx) ((_net)->ipv6.vif6_table[_idx].dev != NULL) |
7bc570c8 | 65 | |
7bc570c8 YH |
66 | /* Special spinlock for queue of unresolved entries */ |
67 | static 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 | ||
77 | static struct kmem_cache *mrt_cachep __read_mostly; | |
78 | ||
b5aa30b1 PM |
79 | static int ip6_mr_forward(struct net *net, struct sk_buff *skb, |
80 | struct mfc6_cache *cache); | |
8229efda BT |
81 | static int ip6mr_cache_report(struct net *net, struct sk_buff *pkt, |
82 | mifi_t mifi, int assert); | |
b5aa30b1 PM |
83 | static int ip6mr_fill_mroute(struct net *net, struct sk_buff *skb, |
84 | struct mfc6_cache *c, struct rtmsg *rtm); | |
8229efda | 85 | static void mroute_clean_tables(struct net *net); |
7bc570c8 | 86 | |
7bc570c8 YH |
87 | |
88 | #ifdef CONFIG_PROC_FS | |
89 | ||
90 | struct ipmr_mfc_iter { | |
8b90fc7e | 91 | struct seq_net_private p; |
f30a7784 | 92 | struct list_head *cache; |
7bc570c8 YH |
93 | int ct; |
94 | }; | |
95 | ||
96 | ||
8b90fc7e BT |
97 | static struct mfc6_cache *ipmr_mfc_seq_idx(struct net *net, |
98 | struct ipmr_mfc_iter *it, loff_t pos) | |
7bc570c8 YH |
99 | { |
100 | struct mfc6_cache *mfc; | |
101 | ||
7bc570c8 | 102 | read_lock(&mrt_lock); |
f30a7784 PM |
103 | 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) | |
7bc570c8 YH |
106 | if (pos-- == 0) |
107 | return mfc; | |
f30a7784 | 108 | } |
7bc570c8 YH |
109 | read_unlock(&mrt_lock); |
110 | ||
7bc570c8 | 111 | spin_lock_bh(&mfc_unres_lock); |
f30a7784 PM |
112 | it->cache = &net->ipv6.mfc6_unres_queue; |
113 | list_for_each_entry(mfc, it->cache, list) | |
c476efbc | 114 | if (pos-- == 0) |
7bc570c8 YH |
115 | return mfc; |
116 | spin_unlock_bh(&mfc_unres_lock); | |
117 | ||
118 | it->cache = NULL; | |
119 | return NULL; | |
120 | } | |
121 | ||
7bc570c8 YH |
122 | /* |
123 | * The /proc interfaces to multicast routing /proc/ip6_mr_cache /proc/ip6_mr_vif | |
124 | */ | |
125 | ||
126 | struct ipmr_vif_iter { | |
8b90fc7e | 127 | struct seq_net_private p; |
7bc570c8 YH |
128 | int ct; |
129 | }; | |
130 | ||
8b90fc7e BT |
131 | static struct mif_device *ip6mr_vif_seq_idx(struct net *net, |
132 | struct ipmr_vif_iter *iter, | |
7bc570c8 YH |
133 | loff_t pos) |
134 | { | |
8b90fc7e BT |
135 | for (iter->ct = 0; iter->ct < net->ipv6.maxvif; ++iter->ct) { |
136 | if (!MIF_EXISTS(net, iter->ct)) | |
7bc570c8 YH |
137 | continue; |
138 | if (pos-- == 0) | |
8b90fc7e | 139 | return &net->ipv6.vif6_table[iter->ct]; |
7bc570c8 YH |
140 | } |
141 | return NULL; | |
142 | } | |
143 | ||
144 | static void *ip6mr_vif_seq_start(struct seq_file *seq, loff_t *pos) | |
145 | __acquires(mrt_lock) | |
146 | { | |
8b90fc7e BT |
147 | struct net *net = seq_file_net(seq); |
148 | ||
7bc570c8 | 149 | read_lock(&mrt_lock); |
8b90fc7e BT |
150 | return *pos ? ip6mr_vif_seq_idx(net, seq->private, *pos - 1) |
151 | : SEQ_START_TOKEN; | |
7bc570c8 YH |
152 | } |
153 | ||
154 | static void *ip6mr_vif_seq_next(struct seq_file *seq, void *v, loff_t *pos) | |
155 | { | |
156 | struct ipmr_vif_iter *iter = seq->private; | |
8b90fc7e | 157 | struct net *net = seq_file_net(seq); |
7bc570c8 YH |
158 | |
159 | ++*pos; | |
160 | if (v == SEQ_START_TOKEN) | |
8b90fc7e | 161 | return ip6mr_vif_seq_idx(net, iter, 0); |
7bc570c8 | 162 | |
8b90fc7e BT |
163 | while (++iter->ct < net->ipv6.maxvif) { |
164 | if (!MIF_EXISTS(net, iter->ct)) | |
7bc570c8 | 165 | continue; |
8b90fc7e | 166 | return &net->ipv6.vif6_table[iter->ct]; |
7bc570c8 YH |
167 | } |
168 | return NULL; | |
169 | } | |
170 | ||
171 | static void ip6mr_vif_seq_stop(struct seq_file *seq, void *v) | |
172 | __releases(mrt_lock) | |
173 | { | |
174 | read_unlock(&mrt_lock); | |
175 | } | |
176 | ||
177 | static int ip6mr_vif_seq_show(struct seq_file *seq, void *v) | |
178 | { | |
8b90fc7e BT |
179 | struct net *net = seq_file_net(seq); |
180 | ||
7bc570c8 YH |
181 | 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, | |
d430a227 | 189 | "%2td %-10s %8ld %7ld %8ld %7ld %05X\n", |
8b90fc7e | 190 | vif - net->ipv6.vif6_table, |
7bc570c8 YH |
191 | name, vif->bytes_in, vif->pkt_in, |
192 | vif->bytes_out, vif->pkt_out, | |
193 | vif->flags); | |
194 | } | |
195 | return 0; | |
196 | } | |
197 | ||
98147d52 | 198 | static const struct seq_operations ip6mr_vif_seq_ops = { |
7bc570c8 YH |
199 | .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 | ||
205 | static int ip6mr_vif_open(struct inode *inode, struct file *file) | |
206 | { | |
8b90fc7e BT |
207 | return seq_open_net(inode, file, &ip6mr_vif_seq_ops, |
208 | sizeof(struct ipmr_vif_iter)); | |
7bc570c8 YH |
209 | } |
210 | ||
5ca1b998 | 211 | static const struct file_operations ip6mr_vif_fops = { |
7bc570c8 YH |
212 | .owner = THIS_MODULE, |
213 | .open = ip6mr_vif_open, | |
214 | .read = seq_read, | |
215 | .llseek = seq_lseek, | |
8b90fc7e | 216 | .release = seq_release_net, |
7bc570c8 YH |
217 | }; |
218 | ||
219 | static void *ipmr_mfc_seq_start(struct seq_file *seq, loff_t *pos) | |
220 | { | |
8b90fc7e BT |
221 | struct net *net = seq_file_net(seq); |
222 | ||
223 | return *pos ? ipmr_mfc_seq_idx(net, seq->private, *pos - 1) | |
224 | : SEQ_START_TOKEN; | |
7bc570c8 YH |
225 | } |
226 | ||
227 | static 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; | |
8b90fc7e | 231 | struct net *net = seq_file_net(seq); |
7bc570c8 YH |
232 | |
233 | ++*pos; | |
234 | ||
235 | if (v == SEQ_START_TOKEN) | |
8b90fc7e | 236 | return ipmr_mfc_seq_idx(net, seq->private, 0); |
7bc570c8 | 237 | |
f30a7784 PM |
238 | if (mfc->list.next != it->cache) |
239 | return list_entry(mfc->list.next, struct mfc6_cache, list); | |
7bc570c8 | 240 | |
c476efbc | 241 | if (it->cache == &net->ipv6.mfc6_unres_queue) |
7bc570c8 YH |
242 | goto end_of_list; |
243 | ||
f30a7784 | 244 | BUG_ON(it->cache != &net->ipv6.mfc6_cache_array[it->ct]); |
7bc570c8 | 245 | |
4a6258a0 | 246 | while (++it->ct < MFC6_LINES) { |
f30a7784 PM |
247 | 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); | |
7bc570c8 YH |
251 | } |
252 | ||
253 | /* exhausted cache_array, show unresolved */ | |
254 | read_unlock(&mrt_lock); | |
c476efbc | 255 | it->cache = &net->ipv6.mfc6_unres_queue; |
7bc570c8 YH |
256 | it->ct = 0; |
257 | ||
258 | spin_lock_bh(&mfc_unres_lock); | |
f30a7784 PM |
259 | if (!list_empty(it->cache)) |
260 | return list_first_entry(it->cache, struct mfc6_cache, list); | |
7bc570c8 YH |
261 | |
262 | end_of_list: | |
263 | spin_unlock_bh(&mfc_unres_lock); | |
264 | it->cache = NULL; | |
265 | ||
266 | return NULL; | |
267 | } | |
268 | ||
269 | static void ipmr_mfc_seq_stop(struct seq_file *seq, void *v) | |
270 | { | |
271 | struct ipmr_mfc_iter *it = seq->private; | |
8b90fc7e | 272 | struct net *net = seq_file_net(seq); |
7bc570c8 | 273 | |
c476efbc | 274 | if (it->cache == &net->ipv6.mfc6_unres_queue) |
7bc570c8 | 275 | spin_unlock_bh(&mfc_unres_lock); |
8b90fc7e | 276 | else if (it->cache == net->ipv6.mfc6_cache_array) |
7bc570c8 YH |
277 | read_unlock(&mrt_lock); |
278 | } | |
279 | ||
280 | static int ipmr_mfc_seq_show(struct seq_file *seq, void *v) | |
281 | { | |
282 | int n; | |
8b90fc7e | 283 | struct net *net = seq_file_net(seq); |
7bc570c8 YH |
284 | |
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 | ||
999890b2 | 294 | seq_printf(seq, "%pI6 %pI6 %-3hd", |
0c6ce78a | 295 | &mfc->mf6c_mcastgrp, &mfc->mf6c_origin, |
1ea472e2 | 296 | mfc->mf6c_parent); |
7bc570c8 | 297 | |
c476efbc | 298 | if (it->cache != &net->ipv6.mfc6_unres_queue) { |
1ea472e2 BT |
299 | 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); | |
7bc570c8 YH |
303 | for (n = mfc->mfc_un.res.minvif; |
304 | n < mfc->mfc_un.res.maxvif; n++) { | |
8b90fc7e | 305 | if (MIF_EXISTS(net, n) && |
7bc570c8 YH |
306 | mfc->mfc_un.res.ttls[n] < 255) |
307 | seq_printf(seq, | |
308 | " %2d:%-3d", | |
309 | n, mfc->mfc_un.res.ttls[n]); | |
310 | } | |
1ea472e2 BT |
311 | } 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); | |
7bc570c8 YH |
316 | } |
317 | seq_putc(seq, '\n'); | |
318 | } | |
319 | return 0; | |
320 | } | |
321 | ||
88e9d34c | 322 | static const struct seq_operations ipmr_mfc_seq_ops = { |
7bc570c8 YH |
323 | .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 | ||
329 | static int ipmr_mfc_open(struct inode *inode, struct file *file) | |
330 | { | |
8b90fc7e BT |
331 | return seq_open_net(inode, file, &ipmr_mfc_seq_ops, |
332 | sizeof(struct ipmr_mfc_iter)); | |
7bc570c8 YH |
333 | } |
334 | ||
5ca1b998 | 335 | static const struct file_operations ip6mr_mfc_fops = { |
7bc570c8 YH |
336 | .owner = THIS_MODULE, |
337 | .open = ipmr_mfc_open, | |
338 | .read = seq_read, | |
339 | .llseek = seq_lseek, | |
8b90fc7e | 340 | .release = seq_release_net, |
7bc570c8 YH |
341 | }; |
342 | #endif | |
343 | ||
14fb64e1 | 344 | #ifdef CONFIG_IPV6_PIMSM_V2 |
14fb64e1 YH |
345 | |
346 | static int pim6_rcv(struct sk_buff *skb) | |
347 | { | |
348 | struct pimreghdr *pim; | |
349 | struct ipv6hdr *encap; | |
350 | struct net_device *reg_dev = NULL; | |
8229efda BT |
351 | struct net *net = dev_net(skb->dev); |
352 | int reg_vif_num = net->ipv6.mroute_reg_vif_num; | |
14fb64e1 YH |
353 | |
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) || | |
1d6e55f1 TG |
360 | (csum_ipv6_magic(&ipv6_hdr(skb)->saddr, &ipv6_hdr(skb)->daddr, |
361 | sizeof(*pim), IPPROTO_PIM, | |
362 | csum_partial((void *)pim, sizeof(*pim), 0)) && | |
ec6b486f | 363 | csum_fold(skb_checksum(skb, 0, skb->len, 0)))) |
14fb64e1 YH |
364 | 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) | |
8229efda | 377 | reg_dev = net->ipv6.vif6_table[reg_vif_num].dev; |
14fb64e1 YH |
378 | 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; | |
1d6e55f1 | 389 | skb->protocol = htons(ETH_P_IPV6); |
14fb64e1 YH |
390 | skb->ip_summed = 0; |
391 | skb->pkt_type = PACKET_HOST; | |
adf30907 | 392 | skb_dst_drop(skb); |
dc58c78c PE |
393 | reg_dev->stats.rx_bytes += skb->len; |
394 | reg_dev->stats.rx_packets++; | |
14fb64e1 YH |
395 | 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 | ||
41135cc8 | 404 | static const struct inet6_protocol pim6_protocol = { |
14fb64e1 YH |
405 | .handler = pim6_rcv, |
406 | }; | |
407 | ||
408 | /* Service routines creating virtual interfaces: PIMREG */ | |
409 | ||
6fef4c0c SH |
410 | static netdev_tx_t reg_vif_xmit(struct sk_buff *skb, |
411 | struct net_device *dev) | |
14fb64e1 | 412 | { |
8229efda BT |
413 | struct net *net = dev_net(dev); |
414 | ||
14fb64e1 | 415 | read_lock(&mrt_lock); |
dc58c78c PE |
416 | dev->stats.tx_bytes += skb->len; |
417 | dev->stats.tx_packets++; | |
8229efda BT |
418 | ip6mr_cache_report(net, skb, net->ipv6.mroute_reg_vif_num, |
419 | MRT6MSG_WHOLEPKT); | |
14fb64e1 YH |
420 | read_unlock(&mrt_lock); |
421 | kfree_skb(skb); | |
6ed10654 | 422 | return NETDEV_TX_OK; |
14fb64e1 YH |
423 | } |
424 | ||
007c3838 SH |
425 | static const struct net_device_ops reg_vif_netdev_ops = { |
426 | .ndo_start_xmit = reg_vif_xmit, | |
427 | }; | |
428 | ||
14fb64e1 YH |
429 | static 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; | |
007c3838 | 434 | dev->netdev_ops = ®_vif_netdev_ops; |
14fb64e1 | 435 | dev->destructor = free_netdev; |
403dbb97 | 436 | dev->features |= NETIF_F_NETNS_LOCAL; |
14fb64e1 YH |
437 | } |
438 | ||
8229efda | 439 | static struct net_device *ip6mr_reg_vif(struct net *net) |
14fb64e1 YH |
440 | { |
441 | struct net_device *dev; | |
14fb64e1 | 442 | |
dc58c78c | 443 | dev = alloc_netdev(0, "pim6reg", reg_vif_setup); |
14fb64e1 YH |
444 | if (dev == NULL) |
445 | return NULL; | |
446 | ||
8229efda BT |
447 | dev_net_set(dev, net); |
448 | ||
14fb64e1 YH |
449 | if (register_netdevice(dev)) { |
450 | free_netdev(dev); | |
451 | return NULL; | |
452 | } | |
453 | dev->iflink = 0; | |
454 | ||
14fb64e1 YH |
455 | if (dev_open(dev)) |
456 | goto failure; | |
457 | ||
7af3db78 | 458 | dev_hold(dev); |
14fb64e1 YH |
459 | return dev; |
460 | ||
461 | failure: | |
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 | ||
7bc570c8 YH |
471 | /* |
472 | * Delete a VIF entry | |
473 | */ | |
474 | ||
c871e664 | 475 | static int mif6_delete(struct net *net, int vifi, struct list_head *head) |
7bc570c8 YH |
476 | { |
477 | struct mif_device *v; | |
478 | struct net_device *dev; | |
1d6e55f1 | 479 | struct inet6_dev *in6_dev; |
8229efda | 480 | if (vifi < 0 || vifi >= net->ipv6.maxvif) |
7bc570c8 YH |
481 | return -EADDRNOTAVAIL; |
482 | ||
8229efda | 483 | v = &net->ipv6.vif6_table[vifi]; |
7bc570c8 YH |
484 | |
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 | ||
14fb64e1 | 494 | #ifdef CONFIG_IPV6_PIMSM_V2 |
8229efda BT |
495 | if (vifi == net->ipv6.mroute_reg_vif_num) |
496 | net->ipv6.mroute_reg_vif_num = -1; | |
14fb64e1 YH |
497 | #endif |
498 | ||
8229efda | 499 | if (vifi + 1 == net->ipv6.maxvif) { |
7bc570c8 YH |
500 | int tmp; |
501 | for (tmp = vifi - 1; tmp >= 0; tmp--) { | |
8229efda | 502 | if (MIF_EXISTS(net, tmp)) |
7bc570c8 YH |
503 | break; |
504 | } | |
8229efda | 505 | net->ipv6.maxvif = tmp + 1; |
7bc570c8 YH |
506 | } |
507 | ||
508 | write_unlock_bh(&mrt_lock); | |
509 | ||
510 | dev_set_allmulti(dev, -1); | |
511 | ||
1d6e55f1 TG |
512 | in6_dev = __in6_dev_get(dev); |
513 | if (in6_dev) | |
514 | in6_dev->cnf.mc_forwarding--; | |
515 | ||
7bc570c8 | 516 | if (v->flags & MIFF_REGISTER) |
c871e664 | 517 | unregister_netdevice_queue(dev, head); |
7bc570c8 YH |
518 | |
519 | dev_put(dev); | |
520 | return 0; | |
521 | } | |
522 | ||
58701ad4 BT |
523 | static inline void ip6mr_cache_free(struct mfc6_cache *c) |
524 | { | |
58701ad4 BT |
525 | kmem_cache_free(mrt_cachep, c); |
526 | } | |
527 | ||
7bc570c8 YH |
528 | /* Destroy an unresolved cache entry, killing queued skbs |
529 | and reporting error to netlink readers. | |
530 | */ | |
531 | ||
b5aa30b1 | 532 | static void ip6mr_destroy_unres(struct net *net, struct mfc6_cache *c) |
7bc570c8 YH |
533 | { |
534 | struct sk_buff *skb; | |
535 | ||
8229efda | 536 | atomic_dec(&net->ipv6.cache_resolve_queue_len); |
7bc570c8 YH |
537 | |
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; | |
8229efda | 545 | rtnl_unicast(skb, net, NETLINK_CB(skb).pid); |
7bc570c8 YH |
546 | } else |
547 | kfree_skb(skb); | |
548 | } | |
549 | ||
58701ad4 | 550 | ip6mr_cache_free(c); |
7bc570c8 YH |
551 | } |
552 | ||
553 | ||
c476efbc | 554 | /* Timer process for all the unresolved queue. */ |
7bc570c8 | 555 | |
c476efbc | 556 | static void ipmr_do_expire_process(struct net *net) |
7bc570c8 YH |
557 | { |
558 | unsigned long now = jiffies; | |
559 | unsigned long expires = 10 * HZ; | |
f30a7784 | 560 | struct mfc6_cache *c, *next; |
7bc570c8 | 561 | |
f30a7784 | 562 | list_for_each_entry_safe(c, next, &net->ipv6.mfc6_unres_queue, list) { |
7bc570c8 YH |
563 | 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; | |
7bc570c8 YH |
568 | continue; |
569 | } | |
570 | ||
f30a7784 | 571 | list_del(&c->list); |
b5aa30b1 | 572 | ip6mr_destroy_unres(net, c); |
7bc570c8 YH |
573 | } |
574 | ||
f30a7784 | 575 | if (!list_empty(&net->ipv6.mfc6_unres_queue)) |
c476efbc | 576 | mod_timer(&net->ipv6.ipmr_expire_timer, jiffies + expires); |
7bc570c8 YH |
577 | } |
578 | ||
c476efbc | 579 | static void ipmr_expire_process(unsigned long arg) |
7bc570c8 | 580 | { |
c476efbc PM |
581 | struct net *net = (struct net *)arg; |
582 | ||
7bc570c8 | 583 | if (!spin_trylock(&mfc_unres_lock)) { |
c476efbc | 584 | mod_timer(&net->ipv6.ipmr_expire_timer, jiffies + 1); |
7bc570c8 YH |
585 | return; |
586 | } | |
587 | ||
f30a7784 | 588 | if (!list_empty(&net->ipv6.mfc6_unres_queue)) |
c476efbc | 589 | ipmr_do_expire_process(net); |
7bc570c8 YH |
590 | |
591 | spin_unlock(&mfc_unres_lock); | |
592 | } | |
593 | ||
594 | /* Fill oifs list. It is called under write locked mrt_lock. */ | |
595 | ||
b5aa30b1 PM |
596 | static void ip6mr_update_thresholds(struct net *net, struct mfc6_cache *cache, |
597 | unsigned char *ttls) | |
7bc570c8 YH |
598 | { |
599 | int vifi; | |
600 | ||
6ac7eb08 | 601 | cache->mfc_un.res.minvif = MAXMIFS; |
7bc570c8 | 602 | cache->mfc_un.res.maxvif = 0; |
6ac7eb08 | 603 | memset(cache->mfc_un.res.ttls, 255, MAXMIFS); |
7bc570c8 | 604 | |
8229efda BT |
605 | for (vifi = 0; vifi < net->ipv6.maxvif; vifi++) { |
606 | if (MIF_EXISTS(net, vifi) && | |
4e16880c | 607 | ttls[vifi] && ttls[vifi] < 255) { |
7bc570c8 YH |
608 | 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 | ||
8229efda | 617 | static int mif6_add(struct net *net, struct mif6ctl *vifc, int mrtsock) |
7bc570c8 YH |
618 | { |
619 | int vifi = vifc->mif6c_mifi; | |
8229efda | 620 | struct mif_device *v = &net->ipv6.vif6_table[vifi]; |
7bc570c8 | 621 | struct net_device *dev; |
1d6e55f1 | 622 | struct inet6_dev *in6_dev; |
5ae7b444 | 623 | int err; |
7bc570c8 YH |
624 | |
625 | /* Is vif busy ? */ | |
8229efda | 626 | if (MIF_EXISTS(net, vifi)) |
7bc570c8 YH |
627 | return -EADDRINUSE; |
628 | ||
629 | switch (vifc->mif6c_flags) { | |
14fb64e1 YH |
630 | #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 | */ | |
8229efda | 636 | if (net->ipv6.mroute_reg_vif_num >= 0) |
14fb64e1 | 637 | return -EADDRINUSE; |
8229efda | 638 | dev = ip6mr_reg_vif(net); |
14fb64e1 YH |
639 | if (!dev) |
640 | return -ENOBUFS; | |
5ae7b444 WC |
641 | err = dev_set_allmulti(dev, 1); |
642 | if (err) { | |
643 | unregister_netdevice(dev); | |
7af3db78 | 644 | dev_put(dev); |
5ae7b444 WC |
645 | return err; |
646 | } | |
14fb64e1 YH |
647 | break; |
648 | #endif | |
7bc570c8 | 649 | case 0: |
8229efda | 650 | dev = dev_get_by_index(net, vifc->mif6c_pifi); |
7bc570c8 YH |
651 | if (!dev) |
652 | return -EADDRNOTAVAIL; | |
5ae7b444 | 653 | err = dev_set_allmulti(dev, 1); |
7af3db78 WC |
654 | if (err) { |
655 | dev_put(dev); | |
5ae7b444 | 656 | return err; |
7af3db78 | 657 | } |
7bc570c8 YH |
658 | break; |
659 | default: | |
660 | return -EINVAL; | |
661 | } | |
662 | ||
1d6e55f1 TG |
663 | in6_dev = __in6_dev_get(dev); |
664 | if (in6_dev) | |
665 | in6_dev->cnf.mc_forwarding++; | |
666 | ||
7bc570c8 YH |
667 | /* |
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); | |
7bc570c8 | 685 | v->dev = dev; |
14fb64e1 YH |
686 | #ifdef CONFIG_IPV6_PIMSM_V2 |
687 | if (v->flags & MIFF_REGISTER) | |
8229efda | 688 | net->ipv6.mroute_reg_vif_num = vifi; |
14fb64e1 | 689 | #endif |
8229efda BT |
690 | if (vifi + 1 > net->ipv6.maxvif) |
691 | net->ipv6.maxvif = vifi + 1; | |
7bc570c8 YH |
692 | write_unlock_bh(&mrt_lock); |
693 | return 0; | |
694 | } | |
695 | ||
8229efda BT |
696 | static struct mfc6_cache *ip6mr_cache_find(struct net *net, |
697 | struct in6_addr *origin, | |
698 | struct in6_addr *mcastgrp) | |
7bc570c8 YH |
699 | { |
700 | int line = MFC6_HASH(mcastgrp, origin); | |
701 | struct mfc6_cache *c; | |
702 | ||
f30a7784 | 703 | list_for_each_entry(c, &net->ipv6.mfc6_cache_array[line], list) { |
7bc570c8 YH |
704 | if (ipv6_addr_equal(&c->mf6c_origin, origin) && |
705 | ipv6_addr_equal(&c->mf6c_mcastgrp, mcastgrp)) | |
f30a7784 | 706 | return c; |
7bc570c8 | 707 | } |
f30a7784 | 708 | return NULL; |
7bc570c8 YH |
709 | } |
710 | ||
711 | /* | |
712 | * Allocate a multicast cache entry | |
713 | */ | |
b5aa30b1 | 714 | static struct mfc6_cache *ip6mr_cache_alloc(void) |
7bc570c8 | 715 | { |
36cbac59 | 716 | struct mfc6_cache *c = kmem_cache_zalloc(mrt_cachep, GFP_KERNEL); |
7bc570c8 YH |
717 | if (c == NULL) |
718 | return NULL; | |
6ac7eb08 | 719 | c->mfc_un.res.minvif = MAXMIFS; |
7bc570c8 YH |
720 | return c; |
721 | } | |
722 | ||
b5aa30b1 | 723 | static struct mfc6_cache *ip6mr_cache_alloc_unres(void) |
7bc570c8 | 724 | { |
36cbac59 | 725 | struct mfc6_cache *c = kmem_cache_zalloc(mrt_cachep, GFP_ATOMIC); |
7bc570c8 YH |
726 | if (c == NULL) |
727 | return NULL; | |
7bc570c8 YH |
728 | 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 | ||
b5aa30b1 PM |
737 | static void ip6mr_cache_resolve(struct net *net, struct mfc6_cache *uc, |
738 | struct mfc6_cache *c) | |
7bc570c8 YH |
739 | { |
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 | ||
b5aa30b1 | 751 | if (ip6mr_fill_mroute(net, skb, c, NLMSG_DATA(nlh)) > 0) { |
549e028d | 752 | nlh->nlmsg_len = skb_tail_pointer(skb) - (u8 *)nlh; |
7bc570c8 YH |
753 | } 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 | } | |
b5aa30b1 | 759 | err = rtnl_unicast(skb, net, NETLINK_CB(skb).pid); |
7bc570c8 | 760 | } else |
b5aa30b1 | 761 | ip6_mr_forward(net, skb, c); |
7bc570c8 YH |
762 | } |
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 | ||
8229efda BT |
772 | static int ip6mr_cache_report(struct net *net, struct sk_buff *pkt, mifi_t mifi, |
773 | int assert) | |
7bc570c8 YH |
774 | { |
775 | struct sk_buff *skb; | |
776 | struct mrt6msg *msg; | |
777 | int ret; | |
778 | ||
14fb64e1 YH |
779 | #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); | |
7bc570c8 YH |
786 | |
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 | ||
14fb64e1 YH |
795 | #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; | |
8229efda | 809 | msg->im6_mif = net->ipv6.mroute_reg_vif_num; |
14fb64e1 YH |
810 | 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 | { | |
7bc570c8 YH |
818 | /* |
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; | |
6ac7eb08 | 835 | msg->im6_mif = mifi; |
7bc570c8 YH |
836 | 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 | ||
adf30907 | 840 | skb_dst_set(skb, dst_clone(skb_dst(pkt))); |
7bc570c8 | 841 | skb->ip_summed = CHECKSUM_UNNECESSARY; |
14fb64e1 | 842 | } |
7bc570c8 | 843 | |
8229efda | 844 | if (net->ipv6.mroute6_sk == NULL) { |
7bc570c8 YH |
845 | kfree_skb(skb); |
846 | return -EINVAL; | |
847 | } | |
848 | ||
849 | /* | |
850 | * Deliver to user space multicast routing algorithms | |
851 | */ | |
8229efda | 852 | ret = sock_queue_rcv_skb(net->ipv6.mroute6_sk, skb); |
bd91b8bf | 853 | if (ret < 0) { |
7bc570c8 YH |
854 | 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 | ||
866 | static int | |
8229efda | 867 | ip6mr_cache_unresolved(struct net *net, mifi_t mifi, struct sk_buff *skb) |
7bc570c8 | 868 | { |
f30a7784 | 869 | bool found = false; |
7bc570c8 YH |
870 | int err; |
871 | struct mfc6_cache *c; | |
872 | ||
873 | spin_lock_bh(&mfc_unres_lock); | |
f30a7784 | 874 | list_for_each_entry(c, &net->ipv6.mfc6_unres_queue, list) { |
c476efbc | 875 | if (ipv6_addr_equal(&c->mf6c_mcastgrp, &ipv6_hdr(skb)->daddr) && |
f30a7784 PM |
876 | ipv6_addr_equal(&c->mf6c_origin, &ipv6_hdr(skb)->saddr)) { |
877 | found = true; | |
7bc570c8 | 878 | break; |
f30a7784 | 879 | } |
7bc570c8 YH |
880 | } |
881 | ||
f30a7784 | 882 | if (!found) { |
7bc570c8 YH |
883 | /* |
884 | * Create a new entry if allowable | |
885 | */ | |
886 | ||
8229efda | 887 | if (atomic_read(&net->ipv6.cache_resolve_queue_len) >= 10 || |
b5aa30b1 | 888 | (c = ip6mr_cache_alloc_unres()) == NULL) { |
7bc570c8 YH |
889 | 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 | */ | |
8229efda BT |
905 | err = ip6mr_cache_report(net, skb, mifi, MRT6MSG_NOCACHE); |
906 | if (err < 0) { | |
7bc570c8 YH |
907 | /* If the report failed throw the cache entry |
908 | out - Brad Parker | |
909 | */ | |
910 | spin_unlock_bh(&mfc_unres_lock); | |
911 | ||
58701ad4 | 912 | ip6mr_cache_free(c); |
7bc570c8 YH |
913 | kfree_skb(skb); |
914 | return err; | |
915 | } | |
916 | ||
8229efda | 917 | atomic_inc(&net->ipv6.cache_resolve_queue_len); |
f30a7784 | 918 | list_add(&c->list, &net->ipv6.mfc6_unres_queue); |
7bc570c8 | 919 | |
c476efbc | 920 | ipmr_do_expire_process(net); |
7bc570c8 YH |
921 | } |
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 | ||
8229efda | 942 | static int ip6mr_mfc_delete(struct net *net, struct mf6cctl *mfc) |
7bc570c8 YH |
943 | { |
944 | int line; | |
f30a7784 | 945 | struct mfc6_cache *c, *next; |
7bc570c8 YH |
946 | |
947 | line = MFC6_HASH(&mfc->mf6cc_mcastgrp.sin6_addr, &mfc->mf6cc_origin.sin6_addr); | |
948 | ||
f30a7784 | 949 | list_for_each_entry_safe(c, next, &net->ipv6.mfc6_cache_array[line], list) { |
7bc570c8 YH |
950 | 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); | |
f30a7784 | 953 | list_del(&c->list); |
7bc570c8 YH |
954 | write_unlock_bh(&mrt_lock); |
955 | ||
58701ad4 | 956 | ip6mr_cache_free(c); |
7bc570c8 YH |
957 | return 0; |
958 | } | |
959 | } | |
960 | return -ENOENT; | |
961 | } | |
962 | ||
963 | static int ip6mr_device_event(struct notifier_block *this, | |
964 | unsigned long event, void *ptr) | |
965 | { | |
966 | struct net_device *dev = ptr; | |
8229efda | 967 | struct net *net = dev_net(dev); |
7bc570c8 YH |
968 | struct mif_device *v; |
969 | int ct; | |
c871e664 | 970 | LIST_HEAD(list); |
7bc570c8 | 971 | |
7bc570c8 YH |
972 | if (event != NETDEV_UNREGISTER) |
973 | return NOTIFY_DONE; | |
974 | ||
8229efda BT |
975 | v = &net->ipv6.vif6_table[0]; |
976 | for (ct = 0; ct < net->ipv6.maxvif; ct++, v++) { | |
7bc570c8 | 977 | if (v->dev == dev) |
c871e664 | 978 | mif6_delete(net, ct, &list); |
7bc570c8 | 979 | } |
c871e664 ED |
980 | unregister_netdevice_many(&list); |
981 | ||
7bc570c8 YH |
982 | return NOTIFY_DONE; |
983 | } | |
984 | ||
985 | static struct notifier_block ip6_mr_notifier = { | |
986 | .notifier_call = ip6mr_device_event | |
987 | }; | |
988 | ||
989 | /* | |
990 | * Setup for IP multicast routing | |
991 | */ | |
992 | ||
4e16880c BT |
993 | static int __net_init ip6mr_net_init(struct net *net) |
994 | { | |
f30a7784 | 995 | unsigned int i; |
4e16880c | 996 | int err = 0; |
f30a7784 | 997 | |
4e16880c BT |
998 | 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 | } | |
4a6258a0 BT |
1004 | |
1005 | /* Forwarding cache */ | |
1006 | net->ipv6.mfc6_cache_array = kcalloc(MFC6_LINES, | |
f30a7784 | 1007 | sizeof(struct list_head), |
4a6258a0 BT |
1008 | GFP_KERNEL); |
1009 | if (!net->ipv6.mfc6_cache_array) { | |
1010 | err = -ENOMEM; | |
1011 | goto fail_mfc6_cache; | |
1012 | } | |
950d5704 | 1013 | |
f30a7784 PM |
1014 | 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 | ||
c476efbc PM |
1019 | setup_timer(&net->ipv6.ipmr_expire_timer, ipmr_expire_process, |
1020 | (unsigned long)net); | |
1021 | ||
950d5704 BT |
1022 | #ifdef CONFIG_IPV6_PIMSM_V2 |
1023 | net->ipv6.mroute_reg_vif_num = -1; | |
1024 | #endif | |
8b90fc7e BT |
1025 | |
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 | |
4a6258a0 BT |
1033 | return 0; |
1034 | ||
8b90fc7e BT |
1035 | #ifdef CONFIG_PROC_FS |
1036 | proc_cache_fail: | |
1037 | proc_net_remove(net, "ip6_mr_vif"); | |
1038 | proc_vif_fail: | |
1039 | kfree(net->ipv6.mfc6_cache_array); | |
1040 | #endif | |
4a6258a0 BT |
1041 | fail_mfc6_cache: |
1042 | kfree(net->ipv6.vif6_table); | |
4e16880c BT |
1043 | fail: |
1044 | return err; | |
1045 | } | |
1046 | ||
1047 | static void __net_exit ip6mr_net_exit(struct net *net) | |
1048 | { | |
8b90fc7e BT |
1049 | #ifdef CONFIG_PROC_FS |
1050 | proc_net_remove(net, "ip6_mr_cache"); | |
1051 | proc_net_remove(net, "ip6_mr_vif"); | |
1052 | #endif | |
c476efbc | 1053 | del_timer(&net->ipv6.ipmr_expire_timer); |
8229efda | 1054 | mroute_clean_tables(net); |
4a6258a0 | 1055 | kfree(net->ipv6.mfc6_cache_array); |
4e16880c BT |
1056 | kfree(net->ipv6.vif6_table); |
1057 | } | |
1058 | ||
1059 | static struct pernet_operations ip6mr_net_ops = { | |
1060 | .init = ip6mr_net_init, | |
1061 | .exit = ip6mr_net_exit, | |
1062 | }; | |
1063 | ||
623d1a1a | 1064 | int __init ip6_mr_init(void) |
7bc570c8 | 1065 | { |
623d1a1a WC |
1066 | int err; |
1067 | ||
7bc570c8 YH |
1068 | mrt_cachep = kmem_cache_create("ip6_mrt_cache", |
1069 | sizeof(struct mfc6_cache), | |
1070 | 0, SLAB_HWCACHE_ALIGN, | |
1071 | NULL); | |
1072 | if (!mrt_cachep) | |
623d1a1a | 1073 | return -ENOMEM; |
7bc570c8 | 1074 | |
4e16880c BT |
1075 | err = register_pernet_subsys(&ip6mr_net_ops); |
1076 | if (err) | |
1077 | goto reg_pernet_fail; | |
1078 | ||
623d1a1a WC |
1079 | err = register_netdevice_notifier(&ip6_mr_notifier); |
1080 | if (err) | |
1081 | goto reg_notif_fail; | |
403dbb97 TG |
1082 | #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 | |
623d1a1a | 1089 | return 0; |
403dbb97 TG |
1090 | #ifdef CONFIG_IPV6_PIMSM_V2 |
1091 | add_proto_fail: | |
1092 | unregister_netdevice_notifier(&ip6_mr_notifier); | |
1093 | #endif | |
87b30a65 | 1094 | reg_notif_fail: |
4e16880c BT |
1095 | unregister_pernet_subsys(&ip6mr_net_ops); |
1096 | reg_pernet_fail: | |
87b30a65 | 1097 | kmem_cache_destroy(mrt_cachep); |
623d1a1a | 1098 | return err; |
7bc570c8 YH |
1099 | } |
1100 | ||
623d1a1a WC |
1101 | void ip6_mr_cleanup(void) |
1102 | { | |
623d1a1a | 1103 | unregister_netdevice_notifier(&ip6_mr_notifier); |
4e16880c | 1104 | unregister_pernet_subsys(&ip6mr_net_ops); |
623d1a1a WC |
1105 | kmem_cache_destroy(mrt_cachep); |
1106 | } | |
7bc570c8 | 1107 | |
8229efda | 1108 | static int ip6mr_mfc_add(struct net *net, struct mf6cctl *mfc, int mrtsock) |
7bc570c8 | 1109 | { |
f30a7784 | 1110 | bool found = false; |
7bc570c8 | 1111 | int line; |
f30a7784 | 1112 | struct mfc6_cache *uc, *c; |
6ac7eb08 | 1113 | unsigned char ttls[MAXMIFS]; |
7bc570c8 YH |
1114 | int i; |
1115 | ||
a50436f2 PM |
1116 | if (mfc->mf6cc_parent >= MAXMIFS) |
1117 | return -ENFILE; | |
1118 | ||
6ac7eb08 RR |
1119 | memset(ttls, 255, MAXMIFS); |
1120 | for (i = 0; i < MAXMIFS; i++) { | |
7bc570c8 YH |
1121 | 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 | ||
f30a7784 | 1128 | list_for_each_entry(c, &net->ipv6.mfc6_cache_array[line], list) { |
7bc570c8 | 1129 | if (ipv6_addr_equal(&c->mf6c_origin, &mfc->mf6cc_origin.sin6_addr) && |
f30a7784 PM |
1130 | ipv6_addr_equal(&c->mf6c_mcastgrp, &mfc->mf6cc_mcastgrp.sin6_addr)) { |
1131 | found = true; | |
7bc570c8 | 1132 | break; |
f30a7784 | 1133 | } |
7bc570c8 YH |
1134 | } |
1135 | ||
f30a7784 | 1136 | if (found) { |
7bc570c8 YH |
1137 | write_lock_bh(&mrt_lock); |
1138 | c->mf6c_parent = mfc->mf6cc_parent; | |
b5aa30b1 | 1139 | ip6mr_update_thresholds(net, c, ttls); |
7bc570c8 YH |
1140 | 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 | ||
b5aa30b1 | 1149 | c = ip6mr_cache_alloc(); |
7bc570c8 YH |
1150 | 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; | |
b5aa30b1 | 1156 | ip6mr_update_thresholds(net, c, ttls); |
7bc570c8 YH |
1157 | if (!mrtsock) |
1158 | c->mfc_flags |= MFC_STATIC; | |
1159 | ||
1160 | write_lock_bh(&mrt_lock); | |
f30a7784 | 1161 | list_add(&c->list, &net->ipv6.mfc6_cache_array[line]); |
7bc570c8 YH |
1162 | 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 | */ | |
f30a7784 | 1168 | found = false; |
7bc570c8 | 1169 | spin_lock_bh(&mfc_unres_lock); |
f30a7784 | 1170 | list_for_each_entry(uc, &net->ipv6.mfc6_unres_queue, list) { |
c476efbc | 1171 | if (ipv6_addr_equal(&uc->mf6c_origin, &c->mf6c_origin) && |
7bc570c8 | 1172 | ipv6_addr_equal(&uc->mf6c_mcastgrp, &c->mf6c_mcastgrp)) { |
f30a7784 | 1173 | list_del(&uc->list); |
8229efda | 1174 | atomic_dec(&net->ipv6.cache_resolve_queue_len); |
f30a7784 | 1175 | found = true; |
7bc570c8 YH |
1176 | break; |
1177 | } | |
1178 | } | |
f30a7784 | 1179 | if (list_empty(&net->ipv6.mfc6_unres_queue)) |
c476efbc | 1180 | del_timer(&net->ipv6.ipmr_expire_timer); |
7bc570c8 YH |
1181 | spin_unlock_bh(&mfc_unres_lock); |
1182 | ||
f30a7784 | 1183 | if (found) { |
b5aa30b1 | 1184 | ip6mr_cache_resolve(net, uc, c); |
58701ad4 | 1185 | ip6mr_cache_free(uc); |
7bc570c8 YH |
1186 | } |
1187 | return 0; | |
1188 | } | |
1189 | ||
1190 | /* | |
1191 | * Close the multicast socket, and clear the vif tables etc | |
1192 | */ | |
1193 | ||
8229efda | 1194 | static void mroute_clean_tables(struct net *net) |
7bc570c8 YH |
1195 | { |
1196 | int i; | |
c871e664 | 1197 | LIST_HEAD(list); |
f30a7784 | 1198 | struct mfc6_cache *c, *next; |
7bc570c8 YH |
1199 | |
1200 | /* | |
1201 | * Shut down all active vif entries | |
1202 | */ | |
8229efda BT |
1203 | for (i = 0; i < net->ipv6.maxvif; i++) { |
1204 | if (!(net->ipv6.vif6_table[i].flags & VIFF_STATIC)) | |
c871e664 | 1205 | mif6_delete(net, i, &list); |
7bc570c8 | 1206 | } |
c871e664 | 1207 | unregister_netdevice_many(&list); |
7bc570c8 YH |
1208 | |
1209 | /* | |
1210 | * Wipe the cache | |
1211 | */ | |
4a6258a0 | 1212 | for (i = 0; i < MFC6_LINES; i++) { |
f30a7784 PM |
1213 | list_for_each_entry_safe(c, next, &net->ipv6.mfc6_cache_array[i], list) { |
1214 | if (c->mfc_flags & MFC_STATIC) | |
7bc570c8 | 1215 | continue; |
7bc570c8 | 1216 | write_lock_bh(&mrt_lock); |
f30a7784 | 1217 | list_del(&c->list); |
7bc570c8 YH |
1218 | write_unlock_bh(&mrt_lock); |
1219 | ||
58701ad4 | 1220 | ip6mr_cache_free(c); |
7bc570c8 YH |
1221 | } |
1222 | } | |
1223 | ||
8229efda | 1224 | if (atomic_read(&net->ipv6.cache_resolve_queue_len) != 0) { |
7bc570c8 | 1225 | spin_lock_bh(&mfc_unres_lock); |
f30a7784 PM |
1226 | list_for_each_entry_safe(c, next, &net->ipv6.mfc6_unres_queue, list) { |
1227 | list_del(&c->list); | |
b5aa30b1 | 1228 | ip6mr_destroy_unres(net, c); |
7bc570c8 YH |
1229 | } |
1230 | spin_unlock_bh(&mfc_unres_lock); | |
1231 | } | |
1232 | } | |
1233 | ||
1234 | static int ip6mr_sk_init(struct sock *sk) | |
1235 | { | |
1236 | int err = 0; | |
8229efda | 1237 | struct net *net = sock_net(sk); |
7bc570c8 YH |
1238 | |
1239 | rtnl_lock(); | |
1240 | write_lock_bh(&mrt_lock); | |
1d6e55f1 | 1241 | if (likely(net->ipv6.mroute6_sk == NULL)) { |
8229efda | 1242 | net->ipv6.mroute6_sk = sk; |
1d6e55f1 TG |
1243 | net->ipv6.devconf_all->mc_forwarding++; |
1244 | } | |
7bc570c8 YH |
1245 | else |
1246 | err = -EADDRINUSE; | |
1247 | write_unlock_bh(&mrt_lock); | |
1248 | ||
1249 | rtnl_unlock(); | |
1250 | ||
1251 | return err; | |
1252 | } | |
1253 | ||
1254 | int ip6mr_sk_done(struct sock *sk) | |
1255 | { | |
1256 | int err = 0; | |
8229efda | 1257 | struct net *net = sock_net(sk); |
7bc570c8 YH |
1258 | |
1259 | rtnl_lock(); | |
8229efda | 1260 | if (sk == net->ipv6.mroute6_sk) { |
7bc570c8 | 1261 | write_lock_bh(&mrt_lock); |
8229efda | 1262 | net->ipv6.mroute6_sk = NULL; |
1d6e55f1 | 1263 | net->ipv6.devconf_all->mc_forwarding--; |
7bc570c8 YH |
1264 | write_unlock_bh(&mrt_lock); |
1265 | ||
8229efda | 1266 | mroute_clean_tables(net); |
7bc570c8 YH |
1267 | } 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 | ||
b7058842 | 1281 | int ip6_mroute_setsockopt(struct sock *sk, int optname, char __user *optval, unsigned int optlen) |
7bc570c8 YH |
1282 | { |
1283 | int ret; | |
1284 | struct mif6ctl vif; | |
1285 | struct mf6cctl mfc; | |
1286 | mifi_t mifi; | |
8229efda | 1287 | struct net *net = sock_net(sk); |
7bc570c8 YH |
1288 | |
1289 | if (optname != MRT6_INIT) { | |
8229efda | 1290 | if (sk != net->ipv6.mroute6_sk && !capable(CAP_NET_ADMIN)) |
7bc570c8 YH |
1291 | return -EACCES; |
1292 | } | |
1293 | ||
1294 | switch (optname) { | |
1295 | case MRT6_INIT: | |
1296 | if (sk->sk_type != SOCK_RAW || | |
c720c7e8 | 1297 | inet_sk(sk)->inet_num != IPPROTO_ICMPV6) |
7bc570c8 YH |
1298 | 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; | |
6ac7eb08 | 1312 | if (vif.mif6c_mifi >= MAXMIFS) |
7bc570c8 YH |
1313 | return -ENFILE; |
1314 | rtnl_lock(); | |
8229efda | 1315 | ret = mif6_add(net, &vif, sk == net->ipv6.mroute6_sk); |
7bc570c8 YH |
1316 | 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(); | |
c871e664 | 1325 | ret = mif6_delete(net, mifi, NULL); |
7bc570c8 YH |
1326 | 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) | |
8229efda | 1341 | ret = ip6mr_mfc_delete(net, &mfc); |
7bc570c8 | 1342 | else |
8229efda BT |
1343 | ret = ip6mr_mfc_add(net, &mfc, |
1344 | sk == net->ipv6.mroute6_sk); | |
7bc570c8 YH |
1345 | rtnl_unlock(); |
1346 | return ret; | |
1347 | ||
14fb64e1 YH |
1348 | /* |
1349 | * 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; | |
8229efda | 1356 | net->ipv6.mroute_do_assert = !!v; |
14fb64e1 YH |
1357 | return 0; |
1358 | } | |
1359 | ||
1360 | #ifdef CONFIG_IPV6_PIMSM_V2 | |
1361 | case MRT6_PIM: | |
1362 | { | |
a9f83bf3 | 1363 | int v; |
14fb64e1 YH |
1364 | if (get_user(v, (int __user *)optval)) |
1365 | return -EFAULT; | |
1366 | v = !!v; | |
1367 | rtnl_lock(); | |
1368 | ret = 0; | |
8229efda BT |
1369 | if (v != net->ipv6.mroute_do_pim) { |
1370 | net->ipv6.mroute_do_pim = v; | |
1371 | net->ipv6.mroute_do_assert = v; | |
14fb64e1 YH |
1372 | } |
1373 | rtnl_unlock(); | |
1374 | return ret; | |
1375 | } | |
1376 | ||
1377 | #endif | |
7bc570c8 | 1378 | /* |
7d120c55 | 1379 | * Spurious command, or MRT6_VERSION which you cannot |
7bc570c8 YH |
1380 | * set. |
1381 | */ | |
1382 | default: | |
1383 | return -ENOPROTOOPT; | |
1384 | } | |
1385 | } | |
1386 | ||
1387 | /* | |
1388 | * Getsock opt support for the multicast routing system. | |
1389 | */ | |
1390 | ||
1391 | int ip6_mroute_getsockopt(struct sock *sk, int optname, char __user *optval, | |
1392 | int __user *optlen) | |
1393 | { | |
1394 | int olr; | |
1395 | int val; | |
8229efda | 1396 | struct net *net = sock_net(sk); |
7bc570c8 YH |
1397 | |
1398 | switch (optname) { | |
1399 | case MRT6_VERSION: | |
1400 | val = 0x0305; | |
1401 | break; | |
14fb64e1 YH |
1402 | #ifdef CONFIG_IPV6_PIMSM_V2 |
1403 | case MRT6_PIM: | |
8229efda | 1404 | val = net->ipv6.mroute_do_pim; |
14fb64e1 YH |
1405 | break; |
1406 | #endif | |
1407 | case MRT6_ASSERT: | |
8229efda | 1408 | val = net->ipv6.mroute_do_assert; |
14fb64e1 | 1409 | break; |
7bc570c8 YH |
1410 | 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 | ||
1432 | int 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; | |
8229efda | 1438 | struct net *net = sock_net(sk); |
7bc570c8 YH |
1439 | |
1440 | switch (cmd) { | |
1441 | case SIOCGETMIFCNT_IN6: | |
1442 | if (copy_from_user(&vr, arg, sizeof(vr))) | |
1443 | return -EFAULT; | |
8229efda | 1444 | if (vr.mifi >= net->ipv6.maxvif) |
7bc570c8 YH |
1445 | return -EINVAL; |
1446 | read_lock(&mrt_lock); | |
8229efda BT |
1447 | vif = &net->ipv6.vif6_table[vr.mifi]; |
1448 | if (MIF_EXISTS(net, vr.mifi)) { | |
7bc570c8 YH |
1449 | 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); | |
8229efda | 1466 | c = ip6mr_cache_find(net, &sr.src.sin6_addr, &sr.grp.sin6_addr); |
7bc570c8 YH |
1467 | 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 | ||
1485 | static inline int ip6mr_forward2_finish(struct sk_buff *skb) | |
1486 | { | |
adf30907 | 1487 | IP6_INC_STATS_BH(dev_net(skb_dst(skb)->dev), ip6_dst_idev(skb_dst(skb)), |
483a47d2 | 1488 | IPSTATS_MIB_OUTFORWDATAGRAMS); |
7bc570c8 YH |
1489 | return dst_output(skb); |
1490 | } | |
1491 | ||
1492 | /* | |
1493 | * Processing handlers for ip6mr_forward | |
1494 | */ | |
1495 | ||
b5aa30b1 PM |
1496 | static int ip6mr_forward2(struct net *net, struct sk_buff *skb, |
1497 | struct mfc6_cache *c, int vifi) | |
7bc570c8 YH |
1498 | { |
1499 | struct ipv6hdr *ipv6h; | |
8229efda | 1500 | struct mif_device *vif = &net->ipv6.vif6_table[vifi]; |
7bc570c8 YH |
1501 | struct net_device *dev; |
1502 | struct dst_entry *dst; | |
1503 | struct flowi fl; | |
1504 | ||
1505 | if (vif->dev == NULL) | |
1506 | goto out_free; | |
1507 | ||
14fb64e1 YH |
1508 | #ifdef CONFIG_IPV6_PIMSM_V2 |
1509 | if (vif->flags & MIFF_REGISTER) { | |
1510 | vif->pkt_out++; | |
1511 | vif->bytes_out += skb->len; | |
dc58c78c PE |
1512 | vif->dev->stats.tx_bytes += skb->len; |
1513 | vif->dev->stats.tx_packets++; | |
8229efda | 1514 | ip6mr_cache_report(net, skb, vifi, MRT6MSG_WHOLEPKT); |
8da73b73 | 1515 | goto out_free; |
14fb64e1 YH |
1516 | } |
1517 | #endif | |
1518 | ||
7bc570c8 YH |
1519 | 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 | ||
8229efda | 1528 | dst = ip6_route_output(net, NULL, &fl); |
7bc570c8 YH |
1529 | if (!dst) |
1530 | goto out_free; | |
1531 | ||
adf30907 ED |
1532 | skb_dst_drop(skb); |
1533 | skb_dst_set(skb, dst); | |
7bc570c8 YH |
1534 | |
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 | ||
b2e0b385 | 1561 | return NF_HOOK(NFPROTO_IPV6, NF_INET_FORWARD, skb, skb->dev, dev, |
7bc570c8 YH |
1562 | ip6mr_forward2_finish); |
1563 | ||
1564 | out_free: | |
1565 | kfree_skb(skb); | |
1566 | return 0; | |
1567 | } | |
1568 | ||
1569 | static int ip6mr_find_vif(struct net_device *dev) | |
1570 | { | |
8229efda | 1571 | struct net *net = dev_net(dev); |
7bc570c8 | 1572 | int ct; |
8229efda BT |
1573 | for (ct = net->ipv6.maxvif - 1; ct >= 0; ct--) { |
1574 | if (net->ipv6.vif6_table[ct].dev == dev) | |
7bc570c8 YH |
1575 | break; |
1576 | } | |
1577 | return ct; | |
1578 | } | |
1579 | ||
b5aa30b1 PM |
1580 | static int ip6_mr_forward(struct net *net, struct sk_buff *skb, |
1581 | struct mfc6_cache *cache) | |
7bc570c8 YH |
1582 | { |
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 | ||
14fb64e1 YH |
1590 | /* |
1591 | * Wrong interface: drop packet and (maybe) send PIM assert. | |
1592 | */ | |
8229efda | 1593 | if (net->ipv6.vif6_table[vif].dev != skb->dev) { |
14fb64e1 YH |
1594 | int true_vifi; |
1595 | ||
1596 | cache->mfc_un.res.wrong_if++; | |
1597 | true_vifi = ip6mr_find_vif(skb->dev); | |
1598 | ||
8229efda | 1599 | if (true_vifi >= 0 && net->ipv6.mroute_do_assert && |
14fb64e1 YH |
1600 | /* 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 | */ | |
8229efda | 1605 | (net->ipv6.mroute_do_pim || |
a21f3f99 | 1606 | cache->mfc_un.res.ttls[true_vifi] < 255) && |
14fb64e1 YH |
1607 | time_after(jiffies, |
1608 | cache->mfc_un.res.last_assert + MFC_ASSERT_THRESH)) { | |
1609 | cache->mfc_un.res.last_assert = jiffies; | |
8229efda | 1610 | ip6mr_cache_report(net, skb, true_vifi, MRT6MSG_WRONGMIF); |
14fb64e1 YH |
1611 | } |
1612 | goto dont_forward; | |
1613 | } | |
1614 | ||
8229efda BT |
1615 | net->ipv6.vif6_table[vif].pkt_in++; |
1616 | net->ipv6.vif6_table[vif].bytes_in += skb->len; | |
7bc570c8 YH |
1617 | |
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) | |
b5aa30b1 | 1626 | ip6mr_forward2(net, skb2, cache, psend); |
7bc570c8 YH |
1627 | } |
1628 | psend = ct; | |
1629 | } | |
1630 | } | |
1631 | if (psend != -1) { | |
b5aa30b1 | 1632 | ip6mr_forward2(net, skb, cache, psend); |
7bc570c8 YH |
1633 | return 0; |
1634 | } | |
1635 | ||
14fb64e1 | 1636 | dont_forward: |
7bc570c8 YH |
1637 | kfree_skb(skb); |
1638 | return 0; | |
1639 | } | |
1640 | ||
1641 | ||
1642 | /* | |
1643 | * Multicast packets for forwarding arrive here | |
1644 | */ | |
1645 | ||
1646 | int ip6_mr_input(struct sk_buff *skb) | |
1647 | { | |
1648 | struct mfc6_cache *cache; | |
8229efda | 1649 | struct net *net = dev_net(skb->dev); |
7bc570c8 YH |
1650 | |
1651 | read_lock(&mrt_lock); | |
8229efda BT |
1652 | cache = ip6mr_cache_find(net, |
1653 | &ipv6_hdr(skb)->saddr, &ipv6_hdr(skb)->daddr); | |
7bc570c8 YH |
1654 | |
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) { | |
8229efda | 1663 | int err = ip6mr_cache_unresolved(net, vif, skb); |
7bc570c8 YH |
1664 | read_unlock(&mrt_lock); |
1665 | ||
1666 | return err; | |
1667 | } | |
1668 | read_unlock(&mrt_lock); | |
1669 | kfree_skb(skb); | |
1670 | return -ENODEV; | |
1671 | } | |
1672 | ||
b5aa30b1 | 1673 | ip6_mr_forward(net, skb, cache); |
7bc570c8 YH |
1674 | |
1675 | read_unlock(&mrt_lock); | |
1676 | ||
1677 | return 0; | |
1678 | } | |
1679 | ||
1680 | ||
1681 | static int | |
b5aa30b1 PM |
1682 | ip6mr_fill_mroute(struct net *net, struct sk_buff *skb, struct mfc6_cache *c, |
1683 | struct rtmsg *rtm) | |
7bc570c8 YH |
1684 | { |
1685 | int ct; | |
1686 | struct rtnexthop *nhp; | |
549e028d | 1687 | u8 *b = skb_tail_pointer(skb); |
7bc570c8 YH |
1688 | struct rtattr *mp_head; |
1689 | ||
7438189b ND |
1690 | /* 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); | |
7bc570c8 YH |
1696 | |
1697 | mp_head = (struct rtattr *)skb_put(skb, RTA_LENGTH(0)); | |
1698 | ||
1699 | for (ct = c->mfc_un.res.minvif; ct < c->mfc_un.res.maxvif; ct++) { | |
7438189b | 1700 | if (MIF_EXISTS(net, ct) && c->mfc_un.res.ttls[ct] < 255) { |
7bc570c8 YH |
1701 | if (skb_tailroom(skb) < RTA_ALIGN(RTA_ALIGN(sizeof(*nhp)) + 4)) |
1702 | goto rtattr_failure; | |
1703 | nhp = (struct rtnexthop *)skb_put(skb, RTA_ALIGN(sizeof(*nhp))); | |
1704 | nhp->rtnh_flags = 0; | |
1705 | nhp->rtnh_hops = c->mfc_un.res.ttls[ct]; | |
8229efda | 1706 | nhp->rtnh_ifindex = net->ipv6.vif6_table[ct].dev->ifindex; |
7bc570c8 YH |
1707 | nhp->rtnh_len = sizeof(*nhp); |
1708 | } | |
1709 | } | |
1710 | mp_head->rta_type = RTA_MULTIPATH; | |
549e028d | 1711 | mp_head->rta_len = skb_tail_pointer(skb) - (u8 *)mp_head; |
7bc570c8 YH |
1712 | rtm->rtm_type = RTN_MULTICAST; |
1713 | return 1; | |
1714 | ||
1715 | rtattr_failure: | |
1716 | nlmsg_trim(skb, b); | |
1717 | return -EMSGSIZE; | |
1718 | } | |
1719 | ||
8229efda BT |
1720 | int ip6mr_get_route(struct net *net, |
1721 | struct sk_buff *skb, struct rtmsg *rtm, int nowait) | |
7bc570c8 YH |
1722 | { |
1723 | int err; | |
1724 | struct mfc6_cache *cache; | |
adf30907 | 1725 | struct rt6_info *rt = (struct rt6_info *)skb_dst(skb); |
7bc570c8 YH |
1726 | |
1727 | read_lock(&mrt_lock); | |
8229efda | 1728 | cache = ip6mr_cache_find(net, &rt->rt6i_src.addr, &rt->rt6i_dst.addr); |
7bc570c8 YH |
1729 | |
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 | ||
8229efda | 1771 | err = ip6mr_cache_unresolved(net, vif, skb2); |
7bc570c8 YH |
1772 | 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 | ||
b5aa30b1 | 1780 | err = ip6mr_fill_mroute(net, skb, cache, rtm); |
7bc570c8 YH |
1781 | read_unlock(&mrt_lock); |
1782 | return err; | |
1783 | } | |
1784 |