]>
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, <[email protected]> | |
6 | * LSIIT Laboratory, Strasbourg, France | |
7 | * (c) 2004 Jean-Philippe Andriot, <[email protected]> | |
8 | * 6WIND, Paris, France | |
9 | * Copyright (C)2007,2008 USAGI/WIDE Project | |
10 | * YOSHIFUJI Hideaki <[email protected]> | |
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> |
7bc570c8 YH |
36 | #include <net/protocol.h> |
37 | #include <linux/skbuff.h> | |
38 | #include <net/sock.h> | |
7bc570c8 | 39 | #include <net/raw.h> |
7bc570c8 YH |
40 | #include <linux/notifier.h> |
41 | #include <linux/if_arp.h> | |
7bc570c8 YH |
42 | #include <net/checksum.h> |
43 | #include <net/netlink.h> | |
44 | ||
45 | #include <net/ipv6.h> | |
46 | #include <net/ip6_route.h> | |
47 | #include <linux/mroute6.h> | |
14fb64e1 | 48 | #include <linux/pim.h> |
7bc570c8 YH |
49 | #include <net/addrconf.h> |
50 | #include <linux/netfilter_ipv6.h> | |
5d6e430d | 51 | #include <net/ip6_checksum.h> |
7bc570c8 | 52 | |
7bc570c8 YH |
53 | /* Big lock, protecting vif table, mrt cache and mroute socket state. |
54 | Note that the changes are semaphored via rtnl_lock. | |
55 | */ | |
56 | ||
57 | static DEFINE_RWLOCK(mrt_lock); | |
58 | ||
59 | /* | |
60 | * Multicast router control variables | |
61 | */ | |
62 | ||
4e16880c | 63 | #define MIF_EXISTS(_net, _idx) ((_net)->ipv6.vif6_table[_idx].dev != NULL) |
7bc570c8 | 64 | |
7bc570c8 | 65 | static struct mfc6_cache *mfc_unres_queue; /* Queue of unresolved entries */ |
7bc570c8 YH |
66 | |
67 | /* Special spinlock for queue of unresolved entries */ | |
68 | static DEFINE_SPINLOCK(mfc_unres_lock); | |
69 | ||
70 | /* We return to original Alan's scheme. Hash table of resolved | |
71 | entries is changed only in process context and protected | |
72 | with weak lock mrt_lock. Queue of unresolved entries is protected | |
73 | with strong spinlock mfc_unres_lock. | |
74 | ||
75 | In this case data path is free of exclusive locks at all. | |
76 | */ | |
77 | ||
78 | static struct kmem_cache *mrt_cachep __read_mostly; | |
79 | ||
80 | static int ip6_mr_forward(struct sk_buff *skb, 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); | |
7bc570c8 | 83 | static int ip6mr_fill_mroute(struct sk_buff *skb, struct mfc6_cache *c, struct rtmsg *rtm); |
8229efda | 84 | static void mroute_clean_tables(struct net *net); |
7bc570c8 YH |
85 | |
86 | static struct timer_list ipmr_expire_timer; | |
87 | ||
88 | ||
89 | #ifdef CONFIG_PROC_FS | |
90 | ||
91 | struct ipmr_mfc_iter { | |
8b90fc7e | 92 | struct seq_net_private p; |
7bc570c8 YH |
93 | struct mfc6_cache **cache; |
94 | int ct; | |
95 | }; | |
96 | ||
97 | ||
8b90fc7e BT |
98 | static struct mfc6_cache *ipmr_mfc_seq_idx(struct net *net, |
99 | struct ipmr_mfc_iter *it, loff_t pos) | |
7bc570c8 YH |
100 | { |
101 | struct mfc6_cache *mfc; | |
102 | ||
8b90fc7e | 103 | it->cache = net->ipv6.mfc6_cache_array; |
7bc570c8 | 104 | read_lock(&mrt_lock); |
4a6258a0 | 105 | for (it->ct = 0; it->ct < MFC6_LINES; it->ct++) |
8b90fc7e | 106 | for (mfc = net->ipv6.mfc6_cache_array[it->ct]; |
4a6258a0 | 107 | mfc; mfc = mfc->next) |
7bc570c8 YH |
108 | if (pos-- == 0) |
109 | return mfc; | |
110 | read_unlock(&mrt_lock); | |
111 | ||
112 | it->cache = &mfc_unres_queue; | |
113 | spin_lock_bh(&mfc_unres_lock); | |
114 | for (mfc = mfc_unres_queue; mfc; mfc = mfc->next) | |
8b90fc7e BT |
115 | if (net_eq(mfc6_net(mfc), net) && |
116 | pos-- == 0) | |
7bc570c8 YH |
117 | return mfc; |
118 | spin_unlock_bh(&mfc_unres_lock); | |
119 | ||
120 | it->cache = NULL; | |
121 | return NULL; | |
122 | } | |
123 | ||
124 | ||
125 | ||
126 | ||
127 | /* | |
128 | * The /proc interfaces to multicast routing /proc/ip6_mr_cache /proc/ip6_mr_vif | |
129 | */ | |
130 | ||
131 | struct ipmr_vif_iter { | |
8b90fc7e | 132 | struct seq_net_private p; |
7bc570c8 YH |
133 | int ct; |
134 | }; | |
135 | ||
8b90fc7e BT |
136 | static struct mif_device *ip6mr_vif_seq_idx(struct net *net, |
137 | struct ipmr_vif_iter *iter, | |
7bc570c8 YH |
138 | loff_t pos) |
139 | { | |
8b90fc7e BT |
140 | for (iter->ct = 0; iter->ct < net->ipv6.maxvif; ++iter->ct) { |
141 | if (!MIF_EXISTS(net, iter->ct)) | |
7bc570c8 YH |
142 | continue; |
143 | if (pos-- == 0) | |
8b90fc7e | 144 | return &net->ipv6.vif6_table[iter->ct]; |
7bc570c8 YH |
145 | } |
146 | return NULL; | |
147 | } | |
148 | ||
149 | static void *ip6mr_vif_seq_start(struct seq_file *seq, loff_t *pos) | |
150 | __acquires(mrt_lock) | |
151 | { | |
8b90fc7e BT |
152 | struct net *net = seq_file_net(seq); |
153 | ||
7bc570c8 | 154 | read_lock(&mrt_lock); |
8b90fc7e BT |
155 | return *pos ? ip6mr_vif_seq_idx(net, seq->private, *pos - 1) |
156 | : SEQ_START_TOKEN; | |
7bc570c8 YH |
157 | } |
158 | ||
159 | static void *ip6mr_vif_seq_next(struct seq_file *seq, void *v, loff_t *pos) | |
160 | { | |
161 | struct ipmr_vif_iter *iter = seq->private; | |
8b90fc7e | 162 | struct net *net = seq_file_net(seq); |
7bc570c8 YH |
163 | |
164 | ++*pos; | |
165 | if (v == SEQ_START_TOKEN) | |
8b90fc7e | 166 | return ip6mr_vif_seq_idx(net, iter, 0); |
7bc570c8 | 167 | |
8b90fc7e BT |
168 | while (++iter->ct < net->ipv6.maxvif) { |
169 | if (!MIF_EXISTS(net, iter->ct)) | |
7bc570c8 | 170 | continue; |
8b90fc7e | 171 | return &net->ipv6.vif6_table[iter->ct]; |
7bc570c8 YH |
172 | } |
173 | return NULL; | |
174 | } | |
175 | ||
176 | static void ip6mr_vif_seq_stop(struct seq_file *seq, void *v) | |
177 | __releases(mrt_lock) | |
178 | { | |
179 | read_unlock(&mrt_lock); | |
180 | } | |
181 | ||
182 | static int ip6mr_vif_seq_show(struct seq_file *seq, void *v) | |
183 | { | |
8b90fc7e BT |
184 | struct net *net = seq_file_net(seq); |
185 | ||
7bc570c8 YH |
186 | if (v == SEQ_START_TOKEN) { |
187 | seq_puts(seq, | |
188 | "Interface BytesIn PktsIn BytesOut PktsOut Flags\n"); | |
189 | } else { | |
190 | const struct mif_device *vif = v; | |
191 | const char *name = vif->dev ? vif->dev->name : "none"; | |
192 | ||
193 | seq_printf(seq, | |
d430a227 | 194 | "%2td %-10s %8ld %7ld %8ld %7ld %05X\n", |
8b90fc7e | 195 | vif - net->ipv6.vif6_table, |
7bc570c8 YH |
196 | name, vif->bytes_in, vif->pkt_in, |
197 | vif->bytes_out, vif->pkt_out, | |
198 | vif->flags); | |
199 | } | |
200 | return 0; | |
201 | } | |
202 | ||
98147d52 | 203 | static const struct seq_operations ip6mr_vif_seq_ops = { |
7bc570c8 YH |
204 | .start = ip6mr_vif_seq_start, |
205 | .next = ip6mr_vif_seq_next, | |
206 | .stop = ip6mr_vif_seq_stop, | |
207 | .show = ip6mr_vif_seq_show, | |
208 | }; | |
209 | ||
210 | static int ip6mr_vif_open(struct inode *inode, struct file *file) | |
211 | { | |
8b90fc7e BT |
212 | return seq_open_net(inode, file, &ip6mr_vif_seq_ops, |
213 | sizeof(struct ipmr_vif_iter)); | |
7bc570c8 YH |
214 | } |
215 | ||
5ca1b998 | 216 | static const struct file_operations ip6mr_vif_fops = { |
7bc570c8 YH |
217 | .owner = THIS_MODULE, |
218 | .open = ip6mr_vif_open, | |
219 | .read = seq_read, | |
220 | .llseek = seq_lseek, | |
8b90fc7e | 221 | .release = seq_release_net, |
7bc570c8 YH |
222 | }; |
223 | ||
224 | static void *ipmr_mfc_seq_start(struct seq_file *seq, loff_t *pos) | |
225 | { | |
8b90fc7e BT |
226 | struct net *net = seq_file_net(seq); |
227 | ||
228 | return *pos ? ipmr_mfc_seq_idx(net, seq->private, *pos - 1) | |
229 | : SEQ_START_TOKEN; | |
7bc570c8 YH |
230 | } |
231 | ||
232 | static void *ipmr_mfc_seq_next(struct seq_file *seq, void *v, loff_t *pos) | |
233 | { | |
234 | struct mfc6_cache *mfc = v; | |
235 | struct ipmr_mfc_iter *it = seq->private; | |
8b90fc7e | 236 | struct net *net = seq_file_net(seq); |
7bc570c8 YH |
237 | |
238 | ++*pos; | |
239 | ||
240 | if (v == SEQ_START_TOKEN) | |
8b90fc7e | 241 | return ipmr_mfc_seq_idx(net, seq->private, 0); |
7bc570c8 YH |
242 | |
243 | if (mfc->next) | |
244 | return mfc->next; | |
245 | ||
246 | if (it->cache == &mfc_unres_queue) | |
247 | goto end_of_list; | |
248 | ||
8b90fc7e | 249 | BUG_ON(it->cache != net->ipv6.mfc6_cache_array); |
7bc570c8 | 250 | |
4a6258a0 | 251 | while (++it->ct < MFC6_LINES) { |
8b90fc7e | 252 | mfc = net->ipv6.mfc6_cache_array[it->ct]; |
7bc570c8 YH |
253 | if (mfc) |
254 | return mfc; | |
255 | } | |
256 | ||
257 | /* exhausted cache_array, show unresolved */ | |
258 | read_unlock(&mrt_lock); | |
259 | it->cache = &mfc_unres_queue; | |
260 | it->ct = 0; | |
261 | ||
262 | spin_lock_bh(&mfc_unres_lock); | |
263 | mfc = mfc_unres_queue; | |
264 | if (mfc) | |
265 | return mfc; | |
266 | ||
267 | end_of_list: | |
268 | spin_unlock_bh(&mfc_unres_lock); | |
269 | it->cache = NULL; | |
270 | ||
271 | return NULL; | |
272 | } | |
273 | ||
274 | static void ipmr_mfc_seq_stop(struct seq_file *seq, void *v) | |
275 | { | |
276 | struct ipmr_mfc_iter *it = seq->private; | |
8b90fc7e | 277 | struct net *net = seq_file_net(seq); |
7bc570c8 YH |
278 | |
279 | if (it->cache == &mfc_unres_queue) | |
280 | spin_unlock_bh(&mfc_unres_lock); | |
8b90fc7e | 281 | else if (it->cache == net->ipv6.mfc6_cache_array) |
7bc570c8 YH |
282 | read_unlock(&mrt_lock); |
283 | } | |
284 | ||
285 | static int ipmr_mfc_seq_show(struct seq_file *seq, void *v) | |
286 | { | |
287 | int n; | |
8b90fc7e | 288 | struct net *net = seq_file_net(seq); |
7bc570c8 YH |
289 | |
290 | if (v == SEQ_START_TOKEN) { | |
291 | seq_puts(seq, | |
292 | "Group " | |
293 | "Origin " | |
294 | "Iif Pkts Bytes Wrong Oifs\n"); | |
295 | } else { | |
296 | const struct mfc6_cache *mfc = v; | |
297 | const struct ipmr_mfc_iter *it = seq->private; | |
298 | ||
999890b2 | 299 | seq_printf(seq, "%pI6 %pI6 %-3hd", |
0c6ce78a | 300 | &mfc->mf6c_mcastgrp, &mfc->mf6c_origin, |
1ea472e2 | 301 | mfc->mf6c_parent); |
7bc570c8 YH |
302 | |
303 | if (it->cache != &mfc_unres_queue) { | |
1ea472e2 BT |
304 | seq_printf(seq, " %8lu %8lu %8lu", |
305 | mfc->mfc_un.res.pkt, | |
306 | mfc->mfc_un.res.bytes, | |
307 | mfc->mfc_un.res.wrong_if); | |
7bc570c8 YH |
308 | for (n = mfc->mfc_un.res.minvif; |
309 | n < mfc->mfc_un.res.maxvif; n++) { | |
8b90fc7e | 310 | if (MIF_EXISTS(net, n) && |
7bc570c8 YH |
311 | mfc->mfc_un.res.ttls[n] < 255) |
312 | seq_printf(seq, | |
313 | " %2d:%-3d", | |
314 | n, mfc->mfc_un.res.ttls[n]); | |
315 | } | |
1ea472e2 BT |
316 | } else { |
317 | /* unresolved mfc_caches don't contain | |
318 | * pkt, bytes and wrong_if values | |
319 | */ | |
320 | seq_printf(seq, " %8lu %8lu %8lu", 0ul, 0ul, 0ul); | |
7bc570c8 YH |
321 | } |
322 | seq_putc(seq, '\n'); | |
323 | } | |
324 | return 0; | |
325 | } | |
326 | ||
88e9d34c | 327 | static const struct seq_operations ipmr_mfc_seq_ops = { |
7bc570c8 YH |
328 | .start = ipmr_mfc_seq_start, |
329 | .next = ipmr_mfc_seq_next, | |
330 | .stop = ipmr_mfc_seq_stop, | |
331 | .show = ipmr_mfc_seq_show, | |
332 | }; | |
333 | ||
334 | static int ipmr_mfc_open(struct inode *inode, struct file *file) | |
335 | { | |
8b90fc7e BT |
336 | return seq_open_net(inode, file, &ipmr_mfc_seq_ops, |
337 | sizeof(struct ipmr_mfc_iter)); | |
7bc570c8 YH |
338 | } |
339 | ||
5ca1b998 | 340 | static const struct file_operations ip6mr_mfc_fops = { |
7bc570c8 YH |
341 | .owner = THIS_MODULE, |
342 | .open = ipmr_mfc_open, | |
343 | .read = seq_read, | |
344 | .llseek = seq_lseek, | |
8b90fc7e | 345 | .release = seq_release_net, |
7bc570c8 YH |
346 | }; |
347 | #endif | |
348 | ||
14fb64e1 | 349 | #ifdef CONFIG_IPV6_PIMSM_V2 |
14fb64e1 YH |
350 | |
351 | static int pim6_rcv(struct sk_buff *skb) | |
352 | { | |
353 | struct pimreghdr *pim; | |
354 | struct ipv6hdr *encap; | |
355 | struct net_device *reg_dev = NULL; | |
8229efda BT |
356 | struct net *net = dev_net(skb->dev); |
357 | int reg_vif_num = net->ipv6.mroute_reg_vif_num; | |
14fb64e1 YH |
358 | |
359 | if (!pskb_may_pull(skb, sizeof(*pim) + sizeof(*encap))) | |
360 | goto drop; | |
361 | ||
362 | pim = (struct pimreghdr *)skb_transport_header(skb); | |
363 | if (pim->type != ((PIM_VERSION << 4) | PIM_REGISTER) || | |
364 | (pim->flags & PIM_NULL_REGISTER) || | |
1d6e55f1 TG |
365 | (csum_ipv6_magic(&ipv6_hdr(skb)->saddr, &ipv6_hdr(skb)->daddr, |
366 | sizeof(*pim), IPPROTO_PIM, | |
367 | csum_partial((void *)pim, sizeof(*pim), 0)) && | |
ec6b486f | 368 | csum_fold(skb_checksum(skb, 0, skb->len, 0)))) |
14fb64e1 YH |
369 | goto drop; |
370 | ||
371 | /* check if the inner packet is destined to mcast group */ | |
372 | encap = (struct ipv6hdr *)(skb_transport_header(skb) + | |
373 | sizeof(*pim)); | |
374 | ||
375 | if (!ipv6_addr_is_multicast(&encap->daddr) || | |
376 | encap->payload_len == 0 || | |
377 | ntohs(encap->payload_len) + sizeof(*pim) > skb->len) | |
378 | goto drop; | |
379 | ||
380 | read_lock(&mrt_lock); | |
381 | if (reg_vif_num >= 0) | |
8229efda | 382 | reg_dev = net->ipv6.vif6_table[reg_vif_num].dev; |
14fb64e1 YH |
383 | if (reg_dev) |
384 | dev_hold(reg_dev); | |
385 | read_unlock(&mrt_lock); | |
386 | ||
387 | if (reg_dev == NULL) | |
388 | goto drop; | |
389 | ||
390 | skb->mac_header = skb->network_header; | |
391 | skb_pull(skb, (u8 *)encap - skb->data); | |
392 | skb_reset_network_header(skb); | |
393 | skb->dev = reg_dev; | |
1d6e55f1 | 394 | skb->protocol = htons(ETH_P_IPV6); |
14fb64e1 YH |
395 | skb->ip_summed = 0; |
396 | skb->pkt_type = PACKET_HOST; | |
adf30907 | 397 | skb_dst_drop(skb); |
dc58c78c PE |
398 | reg_dev->stats.rx_bytes += skb->len; |
399 | reg_dev->stats.rx_packets++; | |
14fb64e1 YH |
400 | nf_reset(skb); |
401 | netif_rx(skb); | |
402 | dev_put(reg_dev); | |
403 | return 0; | |
404 | drop: | |
405 | kfree_skb(skb); | |
406 | return 0; | |
407 | } | |
408 | ||
41135cc8 | 409 | static const struct inet6_protocol pim6_protocol = { |
14fb64e1 YH |
410 | .handler = pim6_rcv, |
411 | }; | |
412 | ||
413 | /* Service routines creating virtual interfaces: PIMREG */ | |
414 | ||
6fef4c0c SH |
415 | static netdev_tx_t reg_vif_xmit(struct sk_buff *skb, |
416 | struct net_device *dev) | |
14fb64e1 | 417 | { |
8229efda BT |
418 | struct net *net = dev_net(dev); |
419 | ||
14fb64e1 | 420 | read_lock(&mrt_lock); |
dc58c78c PE |
421 | dev->stats.tx_bytes += skb->len; |
422 | dev->stats.tx_packets++; | |
8229efda BT |
423 | ip6mr_cache_report(net, skb, net->ipv6.mroute_reg_vif_num, |
424 | MRT6MSG_WHOLEPKT); | |
14fb64e1 YH |
425 | read_unlock(&mrt_lock); |
426 | kfree_skb(skb); | |
6ed10654 | 427 | return NETDEV_TX_OK; |
14fb64e1 YH |
428 | } |
429 | ||
007c3838 SH |
430 | static const struct net_device_ops reg_vif_netdev_ops = { |
431 | .ndo_start_xmit = reg_vif_xmit, | |
432 | }; | |
433 | ||
14fb64e1 YH |
434 | static void reg_vif_setup(struct net_device *dev) |
435 | { | |
436 | dev->type = ARPHRD_PIMREG; | |
437 | dev->mtu = 1500 - sizeof(struct ipv6hdr) - 8; | |
438 | dev->flags = IFF_NOARP; | |
007c3838 | 439 | dev->netdev_ops = ®_vif_netdev_ops; |
14fb64e1 | 440 | dev->destructor = free_netdev; |
403dbb97 | 441 | dev->features |= NETIF_F_NETNS_LOCAL; |
14fb64e1 YH |
442 | } |
443 | ||
8229efda | 444 | static struct net_device *ip6mr_reg_vif(struct net *net) |
14fb64e1 YH |
445 | { |
446 | struct net_device *dev; | |
14fb64e1 | 447 | |
dc58c78c | 448 | dev = alloc_netdev(0, "pim6reg", reg_vif_setup); |
14fb64e1 YH |
449 | if (dev == NULL) |
450 | return NULL; | |
451 | ||
8229efda BT |
452 | dev_net_set(dev, net); |
453 | ||
14fb64e1 YH |
454 | if (register_netdevice(dev)) { |
455 | free_netdev(dev); | |
456 | return NULL; | |
457 | } | |
458 | dev->iflink = 0; | |
459 | ||
14fb64e1 YH |
460 | if (dev_open(dev)) |
461 | goto failure; | |
462 | ||
7af3db78 | 463 | dev_hold(dev); |
14fb64e1 YH |
464 | return dev; |
465 | ||
466 | failure: | |
467 | /* allow the register to be completed before unregistering. */ | |
468 | rtnl_unlock(); | |
469 | rtnl_lock(); | |
470 | ||
471 | unregister_netdevice(dev); | |
472 | return NULL; | |
473 | } | |
474 | #endif | |
475 | ||
7bc570c8 YH |
476 | /* |
477 | * Delete a VIF entry | |
478 | */ | |
479 | ||
c871e664 | 480 | static int mif6_delete(struct net *net, int vifi, struct list_head *head) |
7bc570c8 YH |
481 | { |
482 | struct mif_device *v; | |
483 | struct net_device *dev; | |
1d6e55f1 | 484 | struct inet6_dev *in6_dev; |
8229efda | 485 | if (vifi < 0 || vifi >= net->ipv6.maxvif) |
7bc570c8 YH |
486 | return -EADDRNOTAVAIL; |
487 | ||
8229efda | 488 | v = &net->ipv6.vif6_table[vifi]; |
7bc570c8 YH |
489 | |
490 | write_lock_bh(&mrt_lock); | |
491 | dev = v->dev; | |
492 | v->dev = NULL; | |
493 | ||
494 | if (!dev) { | |
495 | write_unlock_bh(&mrt_lock); | |
496 | return -EADDRNOTAVAIL; | |
497 | } | |
498 | ||
14fb64e1 | 499 | #ifdef CONFIG_IPV6_PIMSM_V2 |
8229efda BT |
500 | if (vifi == net->ipv6.mroute_reg_vif_num) |
501 | net->ipv6.mroute_reg_vif_num = -1; | |
14fb64e1 YH |
502 | #endif |
503 | ||
8229efda | 504 | if (vifi + 1 == net->ipv6.maxvif) { |
7bc570c8 YH |
505 | int tmp; |
506 | for (tmp = vifi - 1; tmp >= 0; tmp--) { | |
8229efda | 507 | if (MIF_EXISTS(net, tmp)) |
7bc570c8 YH |
508 | break; |
509 | } | |
8229efda | 510 | net->ipv6.maxvif = tmp + 1; |
7bc570c8 YH |
511 | } |
512 | ||
513 | write_unlock_bh(&mrt_lock); | |
514 | ||
515 | dev_set_allmulti(dev, -1); | |
516 | ||
1d6e55f1 TG |
517 | in6_dev = __in6_dev_get(dev); |
518 | if (in6_dev) | |
519 | in6_dev->cnf.mc_forwarding--; | |
520 | ||
7bc570c8 | 521 | if (v->flags & MIFF_REGISTER) |
c871e664 | 522 | unregister_netdevice_queue(dev, head); |
7bc570c8 YH |
523 | |
524 | dev_put(dev); | |
525 | return 0; | |
526 | } | |
527 | ||
58701ad4 BT |
528 | static inline void ip6mr_cache_free(struct mfc6_cache *c) |
529 | { | |
530 | release_net(mfc6_net(c)); | |
531 | kmem_cache_free(mrt_cachep, c); | |
532 | } | |
533 | ||
7bc570c8 YH |
534 | /* Destroy an unresolved cache entry, killing queued skbs |
535 | and reporting error to netlink readers. | |
536 | */ | |
537 | ||
538 | static void ip6mr_destroy_unres(struct mfc6_cache *c) | |
539 | { | |
540 | struct sk_buff *skb; | |
8229efda | 541 | struct net *net = mfc6_net(c); |
7bc570c8 | 542 | |
8229efda | 543 | atomic_dec(&net->ipv6.cache_resolve_queue_len); |
7bc570c8 YH |
544 | |
545 | while((skb = skb_dequeue(&c->mfc_un.unres.unresolved)) != NULL) { | |
546 | if (ipv6_hdr(skb)->version == 0) { | |
547 | struct nlmsghdr *nlh = (struct nlmsghdr *)skb_pull(skb, sizeof(struct ipv6hdr)); | |
548 | nlh->nlmsg_type = NLMSG_ERROR; | |
549 | nlh->nlmsg_len = NLMSG_LENGTH(sizeof(struct nlmsgerr)); | |
550 | skb_trim(skb, nlh->nlmsg_len); | |
551 | ((struct nlmsgerr *)NLMSG_DATA(nlh))->error = -ETIMEDOUT; | |
8229efda | 552 | rtnl_unicast(skb, net, NETLINK_CB(skb).pid); |
7bc570c8 YH |
553 | } else |
554 | kfree_skb(skb); | |
555 | } | |
556 | ||
58701ad4 | 557 | ip6mr_cache_free(c); |
7bc570c8 YH |
558 | } |
559 | ||
560 | ||
561 | /* Single timer process for all the unresolved queue. */ | |
562 | ||
563 | static void ipmr_do_expire_process(unsigned long dummy) | |
564 | { | |
565 | unsigned long now = jiffies; | |
566 | unsigned long expires = 10 * HZ; | |
567 | struct mfc6_cache *c, **cp; | |
568 | ||
569 | cp = &mfc_unres_queue; | |
570 | ||
571 | while ((c = *cp) != NULL) { | |
572 | if (time_after(c->mfc_un.unres.expires, now)) { | |
573 | /* not yet... */ | |
574 | unsigned long interval = c->mfc_un.unres.expires - now; | |
575 | if (interval < expires) | |
576 | expires = interval; | |
577 | cp = &c->next; | |
578 | continue; | |
579 | } | |
580 | ||
581 | *cp = c->next; | |
582 | ip6mr_destroy_unres(c); | |
583 | } | |
584 | ||
4045e57c | 585 | if (mfc_unres_queue != NULL) |
7bc570c8 YH |
586 | mod_timer(&ipmr_expire_timer, jiffies + expires); |
587 | } | |
588 | ||
589 | static void ipmr_expire_process(unsigned long dummy) | |
590 | { | |
591 | if (!spin_trylock(&mfc_unres_lock)) { | |
592 | mod_timer(&ipmr_expire_timer, jiffies + 1); | |
593 | return; | |
594 | } | |
595 | ||
4045e57c | 596 | if (mfc_unres_queue != NULL) |
7bc570c8 YH |
597 | ipmr_do_expire_process(dummy); |
598 | ||
599 | spin_unlock(&mfc_unres_lock); | |
600 | } | |
601 | ||
602 | /* Fill oifs list. It is called under write locked mrt_lock. */ | |
603 | ||
604 | static void ip6mr_update_thresholds(struct mfc6_cache *cache, unsigned char *ttls) | |
605 | { | |
606 | int vifi; | |
8229efda | 607 | struct net *net = mfc6_net(cache); |
7bc570c8 | 608 | |
6ac7eb08 | 609 | cache->mfc_un.res.minvif = MAXMIFS; |
7bc570c8 | 610 | cache->mfc_un.res.maxvif = 0; |
6ac7eb08 | 611 | memset(cache->mfc_un.res.ttls, 255, MAXMIFS); |
7bc570c8 | 612 | |
8229efda BT |
613 | for (vifi = 0; vifi < net->ipv6.maxvif; vifi++) { |
614 | if (MIF_EXISTS(net, vifi) && | |
4e16880c | 615 | ttls[vifi] && ttls[vifi] < 255) { |
7bc570c8 YH |
616 | cache->mfc_un.res.ttls[vifi] = ttls[vifi]; |
617 | if (cache->mfc_un.res.minvif > vifi) | |
618 | cache->mfc_un.res.minvif = vifi; | |
619 | if (cache->mfc_un.res.maxvif <= vifi) | |
620 | cache->mfc_un.res.maxvif = vifi + 1; | |
621 | } | |
622 | } | |
623 | } | |
624 | ||
8229efda | 625 | static int mif6_add(struct net *net, struct mif6ctl *vifc, int mrtsock) |
7bc570c8 YH |
626 | { |
627 | int vifi = vifc->mif6c_mifi; | |
8229efda | 628 | struct mif_device *v = &net->ipv6.vif6_table[vifi]; |
7bc570c8 | 629 | struct net_device *dev; |
1d6e55f1 | 630 | struct inet6_dev *in6_dev; |
5ae7b444 | 631 | int err; |
7bc570c8 YH |
632 | |
633 | /* Is vif busy ? */ | |
8229efda | 634 | if (MIF_EXISTS(net, vifi)) |
7bc570c8 YH |
635 | return -EADDRINUSE; |
636 | ||
637 | switch (vifc->mif6c_flags) { | |
14fb64e1 YH |
638 | #ifdef CONFIG_IPV6_PIMSM_V2 |
639 | case MIFF_REGISTER: | |
640 | /* | |
641 | * Special Purpose VIF in PIM | |
642 | * All the packets will be sent to the daemon | |
643 | */ | |
8229efda | 644 | if (net->ipv6.mroute_reg_vif_num >= 0) |
14fb64e1 | 645 | return -EADDRINUSE; |
8229efda | 646 | dev = ip6mr_reg_vif(net); |
14fb64e1 YH |
647 | if (!dev) |
648 | return -ENOBUFS; | |
5ae7b444 WC |
649 | err = dev_set_allmulti(dev, 1); |
650 | if (err) { | |
651 | unregister_netdevice(dev); | |
7af3db78 | 652 | dev_put(dev); |
5ae7b444 WC |
653 | return err; |
654 | } | |
14fb64e1 YH |
655 | break; |
656 | #endif | |
7bc570c8 | 657 | case 0: |
8229efda | 658 | dev = dev_get_by_index(net, vifc->mif6c_pifi); |
7bc570c8 YH |
659 | if (!dev) |
660 | return -EADDRNOTAVAIL; | |
5ae7b444 | 661 | err = dev_set_allmulti(dev, 1); |
7af3db78 WC |
662 | if (err) { |
663 | dev_put(dev); | |
5ae7b444 | 664 | return err; |
7af3db78 | 665 | } |
7bc570c8 YH |
666 | break; |
667 | default: | |
668 | return -EINVAL; | |
669 | } | |
670 | ||
1d6e55f1 TG |
671 | in6_dev = __in6_dev_get(dev); |
672 | if (in6_dev) | |
673 | in6_dev->cnf.mc_forwarding++; | |
674 | ||
7bc570c8 YH |
675 | /* |
676 | * Fill in the VIF structures | |
677 | */ | |
678 | v->rate_limit = vifc->vifc_rate_limit; | |
679 | v->flags = vifc->mif6c_flags; | |
680 | if (!mrtsock) | |
681 | v->flags |= VIFF_STATIC; | |
682 | v->threshold = vifc->vifc_threshold; | |
683 | v->bytes_in = 0; | |
684 | v->bytes_out = 0; | |
685 | v->pkt_in = 0; | |
686 | v->pkt_out = 0; | |
687 | v->link = dev->ifindex; | |
688 | if (v->flags & MIFF_REGISTER) | |
689 | v->link = dev->iflink; | |
690 | ||
691 | /* And finish update writing critical data */ | |
692 | write_lock_bh(&mrt_lock); | |
7bc570c8 | 693 | v->dev = dev; |
14fb64e1 YH |
694 | #ifdef CONFIG_IPV6_PIMSM_V2 |
695 | if (v->flags & MIFF_REGISTER) | |
8229efda | 696 | net->ipv6.mroute_reg_vif_num = vifi; |
14fb64e1 | 697 | #endif |
8229efda BT |
698 | if (vifi + 1 > net->ipv6.maxvif) |
699 | net->ipv6.maxvif = vifi + 1; | |
7bc570c8 YH |
700 | write_unlock_bh(&mrt_lock); |
701 | return 0; | |
702 | } | |
703 | ||
8229efda BT |
704 | static struct mfc6_cache *ip6mr_cache_find(struct net *net, |
705 | struct in6_addr *origin, | |
706 | struct in6_addr *mcastgrp) | |
7bc570c8 YH |
707 | { |
708 | int line = MFC6_HASH(mcastgrp, origin); | |
709 | struct mfc6_cache *c; | |
710 | ||
8229efda | 711 | for (c = net->ipv6.mfc6_cache_array[line]; c; c = c->next) { |
7bc570c8 YH |
712 | if (ipv6_addr_equal(&c->mf6c_origin, origin) && |
713 | ipv6_addr_equal(&c->mf6c_mcastgrp, mcastgrp)) | |
714 | break; | |
715 | } | |
716 | return c; | |
717 | } | |
718 | ||
719 | /* | |
720 | * Allocate a multicast cache entry | |
721 | */ | |
58701ad4 | 722 | static struct mfc6_cache *ip6mr_cache_alloc(struct net *net) |
7bc570c8 | 723 | { |
36cbac59 | 724 | struct mfc6_cache *c = kmem_cache_zalloc(mrt_cachep, GFP_KERNEL); |
7bc570c8 YH |
725 | if (c == NULL) |
726 | return NULL; | |
6ac7eb08 | 727 | c->mfc_un.res.minvif = MAXMIFS; |
58701ad4 | 728 | mfc6_net_set(c, net); |
7bc570c8 YH |
729 | return c; |
730 | } | |
731 | ||
58701ad4 | 732 | static struct mfc6_cache *ip6mr_cache_alloc_unres(struct net *net) |
7bc570c8 | 733 | { |
36cbac59 | 734 | struct mfc6_cache *c = kmem_cache_zalloc(mrt_cachep, GFP_ATOMIC); |
7bc570c8 YH |
735 | if (c == NULL) |
736 | return NULL; | |
7bc570c8 YH |
737 | skb_queue_head_init(&c->mfc_un.unres.unresolved); |
738 | c->mfc_un.unres.expires = jiffies + 10 * HZ; | |
58701ad4 | 739 | mfc6_net_set(c, net); |
7bc570c8 YH |
740 | return c; |
741 | } | |
742 | ||
743 | /* | |
744 | * A cache entry has gone into a resolved state from queued | |
745 | */ | |
746 | ||
747 | static void ip6mr_cache_resolve(struct mfc6_cache *uc, struct mfc6_cache *c) | |
748 | { | |
749 | struct sk_buff *skb; | |
750 | ||
751 | /* | |
752 | * Play the pending entries through our router | |
753 | */ | |
754 | ||
755 | while((skb = __skb_dequeue(&uc->mfc_un.unres.unresolved))) { | |
756 | if (ipv6_hdr(skb)->version == 0) { | |
757 | int err; | |
758 | struct nlmsghdr *nlh = (struct nlmsghdr *)skb_pull(skb, sizeof(struct ipv6hdr)); | |
759 | ||
760 | if (ip6mr_fill_mroute(skb, c, NLMSG_DATA(nlh)) > 0) { | |
549e028d | 761 | nlh->nlmsg_len = skb_tail_pointer(skb) - (u8 *)nlh; |
7bc570c8 YH |
762 | } else { |
763 | nlh->nlmsg_type = NLMSG_ERROR; | |
764 | nlh->nlmsg_len = NLMSG_LENGTH(sizeof(struct nlmsgerr)); | |
765 | skb_trim(skb, nlh->nlmsg_len); | |
766 | ((struct nlmsgerr *)NLMSG_DATA(nlh))->error = -EMSGSIZE; | |
767 | } | |
8229efda | 768 | err = rtnl_unicast(skb, mfc6_net(uc), NETLINK_CB(skb).pid); |
7bc570c8 YH |
769 | } else |
770 | ip6_mr_forward(skb, c); | |
771 | } | |
772 | } | |
773 | ||
774 | /* | |
775 | * Bounce a cache query up to pim6sd. We could use netlink for this but pim6sd | |
776 | * expects the following bizarre scheme. | |
777 | * | |
778 | * Called under mrt_lock. | |
779 | */ | |
780 | ||
8229efda BT |
781 | static int ip6mr_cache_report(struct net *net, struct sk_buff *pkt, mifi_t mifi, |
782 | int assert) | |
7bc570c8 YH |
783 | { |
784 | struct sk_buff *skb; | |
785 | struct mrt6msg *msg; | |
786 | int ret; | |
787 | ||
14fb64e1 YH |
788 | #ifdef CONFIG_IPV6_PIMSM_V2 |
789 | if (assert == MRT6MSG_WHOLEPKT) | |
790 | skb = skb_realloc_headroom(pkt, -skb_network_offset(pkt) | |
791 | +sizeof(*msg)); | |
792 | else | |
793 | #endif | |
794 | skb = alloc_skb(sizeof(struct ipv6hdr) + sizeof(*msg), GFP_ATOMIC); | |
7bc570c8 YH |
795 | |
796 | if (!skb) | |
797 | return -ENOBUFS; | |
798 | ||
799 | /* I suppose that internal messages | |
800 | * do not require checksums */ | |
801 | ||
802 | skb->ip_summed = CHECKSUM_UNNECESSARY; | |
803 | ||
14fb64e1 YH |
804 | #ifdef CONFIG_IPV6_PIMSM_V2 |
805 | if (assert == MRT6MSG_WHOLEPKT) { | |
806 | /* Ugly, but we have no choice with this interface. | |
807 | Duplicate old header, fix length etc. | |
808 | And all this only to mangle msg->im6_msgtype and | |
809 | to set msg->im6_mbz to "mbz" :-) | |
810 | */ | |
811 | skb_push(skb, -skb_network_offset(pkt)); | |
812 | ||
813 | skb_push(skb, sizeof(*msg)); | |
814 | skb_reset_transport_header(skb); | |
815 | msg = (struct mrt6msg *)skb_transport_header(skb); | |
816 | msg->im6_mbz = 0; | |
817 | msg->im6_msgtype = MRT6MSG_WHOLEPKT; | |
8229efda | 818 | msg->im6_mif = net->ipv6.mroute_reg_vif_num; |
14fb64e1 YH |
819 | msg->im6_pad = 0; |
820 | ipv6_addr_copy(&msg->im6_src, &ipv6_hdr(pkt)->saddr); | |
821 | ipv6_addr_copy(&msg->im6_dst, &ipv6_hdr(pkt)->daddr); | |
822 | ||
823 | skb->ip_summed = CHECKSUM_UNNECESSARY; | |
824 | } else | |
825 | #endif | |
826 | { | |
7bc570c8 YH |
827 | /* |
828 | * Copy the IP header | |
829 | */ | |
830 | ||
831 | skb_put(skb, sizeof(struct ipv6hdr)); | |
832 | skb_reset_network_header(skb); | |
833 | skb_copy_to_linear_data(skb, ipv6_hdr(pkt), sizeof(struct ipv6hdr)); | |
834 | ||
835 | /* | |
836 | * Add our header | |
837 | */ | |
838 | skb_put(skb, sizeof(*msg)); | |
839 | skb_reset_transport_header(skb); | |
840 | msg = (struct mrt6msg *)skb_transport_header(skb); | |
841 | ||
842 | msg->im6_mbz = 0; | |
843 | msg->im6_msgtype = assert; | |
6ac7eb08 | 844 | msg->im6_mif = mifi; |
7bc570c8 YH |
845 | msg->im6_pad = 0; |
846 | ipv6_addr_copy(&msg->im6_src, &ipv6_hdr(pkt)->saddr); | |
847 | ipv6_addr_copy(&msg->im6_dst, &ipv6_hdr(pkt)->daddr); | |
848 | ||
adf30907 | 849 | skb_dst_set(skb, dst_clone(skb_dst(pkt))); |
7bc570c8 | 850 | skb->ip_summed = CHECKSUM_UNNECESSARY; |
14fb64e1 | 851 | } |
7bc570c8 | 852 | |
8229efda | 853 | if (net->ipv6.mroute6_sk == NULL) { |
7bc570c8 YH |
854 | kfree_skb(skb); |
855 | return -EINVAL; | |
856 | } | |
857 | ||
858 | /* | |
859 | * Deliver to user space multicast routing algorithms | |
860 | */ | |
8229efda | 861 | ret = sock_queue_rcv_skb(net->ipv6.mroute6_sk, skb); |
bd91b8bf | 862 | if (ret < 0) { |
7bc570c8 YH |
863 | if (net_ratelimit()) |
864 | printk(KERN_WARNING "mroute6: pending queue full, dropping entries.\n"); | |
865 | kfree_skb(skb); | |
866 | } | |
867 | ||
868 | return ret; | |
869 | } | |
870 | ||
871 | /* | |
872 | * Queue a packet for resolution. It gets locked cache entry! | |
873 | */ | |
874 | ||
875 | static int | |
8229efda | 876 | ip6mr_cache_unresolved(struct net *net, mifi_t mifi, struct sk_buff *skb) |
7bc570c8 YH |
877 | { |
878 | int err; | |
879 | struct mfc6_cache *c; | |
880 | ||
881 | spin_lock_bh(&mfc_unres_lock); | |
882 | for (c = mfc_unres_queue; c; c = c->next) { | |
8229efda | 883 | if (net_eq(mfc6_net(c), net) && |
4045e57c | 884 | ipv6_addr_equal(&c->mf6c_mcastgrp, &ipv6_hdr(skb)->daddr) && |
7bc570c8 YH |
885 | ipv6_addr_equal(&c->mf6c_origin, &ipv6_hdr(skb)->saddr)) |
886 | break; | |
887 | } | |
888 | ||
889 | if (c == NULL) { | |
890 | /* | |
891 | * Create a new entry if allowable | |
892 | */ | |
893 | ||
8229efda BT |
894 | if (atomic_read(&net->ipv6.cache_resolve_queue_len) >= 10 || |
895 | (c = ip6mr_cache_alloc_unres(net)) == NULL) { | |
7bc570c8 YH |
896 | spin_unlock_bh(&mfc_unres_lock); |
897 | ||
898 | kfree_skb(skb); | |
899 | return -ENOBUFS; | |
900 | } | |
901 | ||
902 | /* | |
903 | * Fill in the new cache entry | |
904 | */ | |
905 | c->mf6c_parent = -1; | |
906 | c->mf6c_origin = ipv6_hdr(skb)->saddr; | |
907 | c->mf6c_mcastgrp = ipv6_hdr(skb)->daddr; | |
908 | ||
909 | /* | |
910 | * Reflect first query at pim6sd | |
911 | */ | |
8229efda BT |
912 | err = ip6mr_cache_report(net, skb, mifi, MRT6MSG_NOCACHE); |
913 | if (err < 0) { | |
7bc570c8 YH |
914 | /* If the report failed throw the cache entry |
915 | out - Brad Parker | |
916 | */ | |
917 | spin_unlock_bh(&mfc_unres_lock); | |
918 | ||
58701ad4 | 919 | ip6mr_cache_free(c); |
7bc570c8 YH |
920 | kfree_skb(skb); |
921 | return err; | |
922 | } | |
923 | ||
8229efda | 924 | atomic_inc(&net->ipv6.cache_resolve_queue_len); |
7bc570c8 YH |
925 | c->next = mfc_unres_queue; |
926 | mfc_unres_queue = c; | |
927 | ||
928 | ipmr_do_expire_process(1); | |
929 | } | |
930 | ||
931 | /* | |
932 | * See if we can append the packet | |
933 | */ | |
934 | if (c->mfc_un.unres.unresolved.qlen > 3) { | |
935 | kfree_skb(skb); | |
936 | err = -ENOBUFS; | |
937 | } else { | |
938 | skb_queue_tail(&c->mfc_un.unres.unresolved, skb); | |
939 | err = 0; | |
940 | } | |
941 | ||
942 | spin_unlock_bh(&mfc_unres_lock); | |
943 | return err; | |
944 | } | |
945 | ||
946 | /* | |
947 | * MFC6 cache manipulation by user space | |
948 | */ | |
949 | ||
8229efda | 950 | static int ip6mr_mfc_delete(struct net *net, struct mf6cctl *mfc) |
7bc570c8 YH |
951 | { |
952 | int line; | |
953 | struct mfc6_cache *c, **cp; | |
954 | ||
955 | line = MFC6_HASH(&mfc->mf6cc_mcastgrp.sin6_addr, &mfc->mf6cc_origin.sin6_addr); | |
956 | ||
8229efda | 957 | for (cp = &net->ipv6.mfc6_cache_array[line]; |
4a6258a0 | 958 | (c = *cp) != NULL; cp = &c->next) { |
7bc570c8 YH |
959 | if (ipv6_addr_equal(&c->mf6c_origin, &mfc->mf6cc_origin.sin6_addr) && |
960 | ipv6_addr_equal(&c->mf6c_mcastgrp, &mfc->mf6cc_mcastgrp.sin6_addr)) { | |
961 | write_lock_bh(&mrt_lock); | |
962 | *cp = c->next; | |
963 | write_unlock_bh(&mrt_lock); | |
964 | ||
58701ad4 | 965 | ip6mr_cache_free(c); |
7bc570c8 YH |
966 | return 0; |
967 | } | |
968 | } | |
969 | return -ENOENT; | |
970 | } | |
971 | ||
972 | static int ip6mr_device_event(struct notifier_block *this, | |
973 | unsigned long event, void *ptr) | |
974 | { | |
975 | struct net_device *dev = ptr; | |
8229efda | 976 | struct net *net = dev_net(dev); |
7bc570c8 YH |
977 | struct mif_device *v; |
978 | int ct; | |
c871e664 | 979 | LIST_HEAD(list); |
7bc570c8 | 980 | |
7bc570c8 YH |
981 | if (event != NETDEV_UNREGISTER) |
982 | return NOTIFY_DONE; | |
983 | ||
8229efda BT |
984 | v = &net->ipv6.vif6_table[0]; |
985 | for (ct = 0; ct < net->ipv6.maxvif; ct++, v++) { | |
7bc570c8 | 986 | if (v->dev == dev) |
c871e664 | 987 | mif6_delete(net, ct, &list); |
7bc570c8 | 988 | } |
c871e664 ED |
989 | unregister_netdevice_many(&list); |
990 | ||
7bc570c8 YH |
991 | return NOTIFY_DONE; |
992 | } | |
993 | ||
994 | static struct notifier_block ip6_mr_notifier = { | |
995 | .notifier_call = ip6mr_device_event | |
996 | }; | |
997 | ||
998 | /* | |
999 | * Setup for IP multicast routing | |
1000 | */ | |
1001 | ||
4e16880c BT |
1002 | static int __net_init ip6mr_net_init(struct net *net) |
1003 | { | |
1004 | int err = 0; | |
4e16880c BT |
1005 | net->ipv6.vif6_table = kcalloc(MAXMIFS, sizeof(struct mif_device), |
1006 | GFP_KERNEL); | |
1007 | if (!net->ipv6.vif6_table) { | |
1008 | err = -ENOMEM; | |
1009 | goto fail; | |
1010 | } | |
4a6258a0 BT |
1011 | |
1012 | /* Forwarding cache */ | |
1013 | net->ipv6.mfc6_cache_array = kcalloc(MFC6_LINES, | |
1014 | sizeof(struct mfc6_cache *), | |
1015 | GFP_KERNEL); | |
1016 | if (!net->ipv6.mfc6_cache_array) { | |
1017 | err = -ENOMEM; | |
1018 | goto fail_mfc6_cache; | |
1019 | } | |
950d5704 BT |
1020 | |
1021 | #ifdef CONFIG_IPV6_PIMSM_V2 | |
1022 | net->ipv6.mroute_reg_vif_num = -1; | |
1023 | #endif | |
8b90fc7e BT |
1024 | |
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 | |
4a6258a0 BT |
1032 | return 0; |
1033 | ||
8b90fc7e BT |
1034 | #ifdef CONFIG_PROC_FS |
1035 | proc_cache_fail: | |
1036 | proc_net_remove(net, "ip6_mr_vif"); | |
1037 | proc_vif_fail: | |
1038 | kfree(net->ipv6.mfc6_cache_array); | |
1039 | #endif | |
4a6258a0 BT |
1040 | fail_mfc6_cache: |
1041 | kfree(net->ipv6.vif6_table); | |
4e16880c BT |
1042 | fail: |
1043 | return err; | |
1044 | } | |
1045 | ||
1046 | static void __net_exit ip6mr_net_exit(struct net *net) | |
1047 | { | |
8b90fc7e BT |
1048 | #ifdef CONFIG_PROC_FS |
1049 | proc_net_remove(net, "ip6_mr_cache"); | |
1050 | proc_net_remove(net, "ip6_mr_vif"); | |
1051 | #endif | |
8229efda | 1052 | mroute_clean_tables(net); |
4a6258a0 | 1053 | kfree(net->ipv6.mfc6_cache_array); |
4e16880c BT |
1054 | kfree(net->ipv6.vif6_table); |
1055 | } | |
1056 | ||
1057 | static struct pernet_operations ip6mr_net_ops = { | |
1058 | .init = ip6mr_net_init, | |
1059 | .exit = ip6mr_net_exit, | |
1060 | }; | |
1061 | ||
623d1a1a | 1062 | int __init ip6_mr_init(void) |
7bc570c8 | 1063 | { |
623d1a1a WC |
1064 | int err; |
1065 | ||
7bc570c8 YH |
1066 | mrt_cachep = kmem_cache_create("ip6_mrt_cache", |
1067 | sizeof(struct mfc6_cache), | |
1068 | 0, SLAB_HWCACHE_ALIGN, | |
1069 | NULL); | |
1070 | if (!mrt_cachep) | |
623d1a1a | 1071 | return -ENOMEM; |
7bc570c8 | 1072 | |
4e16880c BT |
1073 | err = register_pernet_subsys(&ip6mr_net_ops); |
1074 | if (err) | |
1075 | goto reg_pernet_fail; | |
1076 | ||
7bc570c8 | 1077 | setup_timer(&ipmr_expire_timer, ipmr_expire_process, 0); |
623d1a1a WC |
1078 | err = register_netdevice_notifier(&ip6_mr_notifier); |
1079 | if (err) | |
1080 | goto reg_notif_fail; | |
403dbb97 TG |
1081 | #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 | |
623d1a1a | 1088 | return 0; |
403dbb97 TG |
1089 | #ifdef CONFIG_IPV6_PIMSM_V2 |
1090 | add_proto_fail: | |
1091 | unregister_netdevice_notifier(&ip6_mr_notifier); | |
1092 | #endif | |
87b30a65 BT |
1093 | reg_notif_fail: |
1094 | del_timer(&ipmr_expire_timer); | |
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 WC |
1103 | unregister_netdevice_notifier(&ip6_mr_notifier); |
1104 | del_timer(&ipmr_expire_timer); | |
4e16880c | 1105 | unregister_pernet_subsys(&ip6mr_net_ops); |
623d1a1a WC |
1106 | kmem_cache_destroy(mrt_cachep); |
1107 | } | |
7bc570c8 | 1108 | |
8229efda | 1109 | static int ip6mr_mfc_add(struct net *net, struct mf6cctl *mfc, int mrtsock) |
7bc570c8 YH |
1110 | { |
1111 | int line; | |
1112 | struct mfc6_cache *uc, *c, **cp; | |
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 | ||
8229efda | 1128 | for (cp = &net->ipv6.mfc6_cache_array[line]; |
4a6258a0 | 1129 | (c = *cp) != NULL; cp = &c->next) { |
7bc570c8 YH |
1130 | if (ipv6_addr_equal(&c->mf6c_origin, &mfc->mf6cc_origin.sin6_addr) && |
1131 | ipv6_addr_equal(&c->mf6c_mcastgrp, &mfc->mf6cc_mcastgrp.sin6_addr)) | |
1132 | break; | |
1133 | } | |
1134 | ||
1135 | if (c != NULL) { | |
1136 | write_lock_bh(&mrt_lock); | |
1137 | c->mf6c_parent = mfc->mf6cc_parent; | |
1138 | ip6mr_update_thresholds(c, ttls); | |
1139 | if (!mrtsock) | |
1140 | c->mfc_flags |= MFC_STATIC; | |
1141 | write_unlock_bh(&mrt_lock); | |
1142 | return 0; | |
1143 | } | |
1144 | ||
1145 | if (!ipv6_addr_is_multicast(&mfc->mf6cc_mcastgrp.sin6_addr)) | |
1146 | return -EINVAL; | |
1147 | ||
8229efda | 1148 | c = ip6mr_cache_alloc(net); |
7bc570c8 YH |
1149 | if (c == NULL) |
1150 | return -ENOMEM; | |
1151 | ||
1152 | c->mf6c_origin = mfc->mf6cc_origin.sin6_addr; | |
1153 | c->mf6c_mcastgrp = mfc->mf6cc_mcastgrp.sin6_addr; | |
1154 | c->mf6c_parent = mfc->mf6cc_parent; | |
1155 | ip6mr_update_thresholds(c, ttls); | |
1156 | if (!mrtsock) | |
1157 | c->mfc_flags |= MFC_STATIC; | |
1158 | ||
1159 | write_lock_bh(&mrt_lock); | |
8229efda BT |
1160 | c->next = net->ipv6.mfc6_cache_array[line]; |
1161 | net->ipv6.mfc6_cache_array[line] = c; | |
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 | */ | |
1168 | spin_lock_bh(&mfc_unres_lock); | |
1169 | for (cp = &mfc_unres_queue; (uc = *cp) != NULL; | |
1170 | cp = &uc->next) { | |
8229efda | 1171 | if (net_eq(mfc6_net(uc), net) && |
4045e57c | 1172 | ipv6_addr_equal(&uc->mf6c_origin, &c->mf6c_origin) && |
7bc570c8 YH |
1173 | ipv6_addr_equal(&uc->mf6c_mcastgrp, &c->mf6c_mcastgrp)) { |
1174 | *cp = uc->next; | |
8229efda | 1175 | atomic_dec(&net->ipv6.cache_resolve_queue_len); |
7bc570c8 YH |
1176 | break; |
1177 | } | |
1178 | } | |
4045e57c BT |
1179 | if (mfc_unres_queue == NULL) |
1180 | del_timer(&ipmr_expire_timer); | |
7bc570c8 YH |
1181 | spin_unlock_bh(&mfc_unres_lock); |
1182 | ||
1183 | if (uc) { | |
1184 | ip6mr_cache_resolve(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); |
7bc570c8 YH |
1198 | |
1199 | /* | |
1200 | * Shut down all active vif entries | |
1201 | */ | |
8229efda BT |
1202 | for (i = 0; i < net->ipv6.maxvif; i++) { |
1203 | if (!(net->ipv6.vif6_table[i].flags & VIFF_STATIC)) | |
c871e664 | 1204 | mif6_delete(net, i, &list); |
7bc570c8 | 1205 | } |
c871e664 | 1206 | unregister_netdevice_many(&list); |
7bc570c8 YH |
1207 | |
1208 | /* | |
1209 | * Wipe the cache | |
1210 | */ | |
4a6258a0 | 1211 | for (i = 0; i < MFC6_LINES; i++) { |
7bc570c8 YH |
1212 | struct mfc6_cache *c, **cp; |
1213 | ||
8229efda | 1214 | cp = &net->ipv6.mfc6_cache_array[i]; |
7bc570c8 YH |
1215 | while ((c = *cp) != NULL) { |
1216 | if (c->mfc_flags & MFC_STATIC) { | |
1217 | cp = &c->next; | |
1218 | continue; | |
1219 | } | |
1220 | write_lock_bh(&mrt_lock); | |
1221 | *cp = c->next; | |
1222 | write_unlock_bh(&mrt_lock); | |
1223 | ||
58701ad4 | 1224 | ip6mr_cache_free(c); |
7bc570c8 YH |
1225 | } |
1226 | } | |
1227 | ||
8229efda | 1228 | if (atomic_read(&net->ipv6.cache_resolve_queue_len) != 0) { |
4045e57c | 1229 | struct mfc6_cache *c, **cp; |
7bc570c8 YH |
1230 | |
1231 | spin_lock_bh(&mfc_unres_lock); | |
4045e57c BT |
1232 | cp = &mfc_unres_queue; |
1233 | while ((c = *cp) != NULL) { | |
8229efda | 1234 | if (!net_eq(mfc6_net(c), net)) { |
4045e57c BT |
1235 | cp = &c->next; |
1236 | continue; | |
1237 | } | |
1238 | *cp = c->next; | |
7bc570c8 | 1239 | ip6mr_destroy_unres(c); |
7bc570c8 YH |
1240 | } |
1241 | spin_unlock_bh(&mfc_unres_lock); | |
1242 | } | |
1243 | } | |
1244 | ||
1245 | static int ip6mr_sk_init(struct sock *sk) | |
1246 | { | |
1247 | int err = 0; | |
8229efda | 1248 | struct net *net = sock_net(sk); |
7bc570c8 YH |
1249 | |
1250 | rtnl_lock(); | |
1251 | write_lock_bh(&mrt_lock); | |
1d6e55f1 | 1252 | if (likely(net->ipv6.mroute6_sk == NULL)) { |
8229efda | 1253 | net->ipv6.mroute6_sk = sk; |
1d6e55f1 TG |
1254 | net->ipv6.devconf_all->mc_forwarding++; |
1255 | } | |
7bc570c8 YH |
1256 | else |
1257 | err = -EADDRINUSE; | |
1258 | write_unlock_bh(&mrt_lock); | |
1259 | ||
1260 | rtnl_unlock(); | |
1261 | ||
1262 | return err; | |
1263 | } | |
1264 | ||
1265 | int ip6mr_sk_done(struct sock *sk) | |
1266 | { | |
1267 | int err = 0; | |
8229efda | 1268 | struct net *net = sock_net(sk); |
7bc570c8 YH |
1269 | |
1270 | rtnl_lock(); | |
8229efda | 1271 | if (sk == net->ipv6.mroute6_sk) { |
7bc570c8 | 1272 | write_lock_bh(&mrt_lock); |
8229efda | 1273 | net->ipv6.mroute6_sk = NULL; |
1d6e55f1 | 1274 | net->ipv6.devconf_all->mc_forwarding--; |
7bc570c8 YH |
1275 | write_unlock_bh(&mrt_lock); |
1276 | ||
8229efda | 1277 | mroute_clean_tables(net); |
7bc570c8 YH |
1278 | } else |
1279 | err = -EACCES; | |
1280 | rtnl_unlock(); | |
1281 | ||
1282 | return err; | |
1283 | } | |
1284 | ||
1285 | /* | |
1286 | * Socket options and virtual interface manipulation. The whole | |
1287 | * virtual interface system is a complete heap, but unfortunately | |
1288 | * that's how BSD mrouted happens to think. Maybe one day with a proper | |
1289 | * MOSPF/PIM router set up we can clean this up. | |
1290 | */ | |
1291 | ||
b7058842 | 1292 | int ip6_mroute_setsockopt(struct sock *sk, int optname, char __user *optval, unsigned int optlen) |
7bc570c8 YH |
1293 | { |
1294 | int ret; | |
1295 | struct mif6ctl vif; | |
1296 | struct mf6cctl mfc; | |
1297 | mifi_t mifi; | |
8229efda | 1298 | struct net *net = sock_net(sk); |
7bc570c8 YH |
1299 | |
1300 | if (optname != MRT6_INIT) { | |
8229efda | 1301 | if (sk != net->ipv6.mroute6_sk && !capable(CAP_NET_ADMIN)) |
7bc570c8 YH |
1302 | return -EACCES; |
1303 | } | |
1304 | ||
1305 | switch (optname) { | |
1306 | case MRT6_INIT: | |
1307 | if (sk->sk_type != SOCK_RAW || | |
c720c7e8 | 1308 | inet_sk(sk)->inet_num != IPPROTO_ICMPV6) |
7bc570c8 YH |
1309 | return -EOPNOTSUPP; |
1310 | if (optlen < sizeof(int)) | |
1311 | return -EINVAL; | |
1312 | ||
1313 | return ip6mr_sk_init(sk); | |
1314 | ||
1315 | case MRT6_DONE: | |
1316 | return ip6mr_sk_done(sk); | |
1317 | ||
1318 | case MRT6_ADD_MIF: | |
1319 | if (optlen < sizeof(vif)) | |
1320 | return -EINVAL; | |
1321 | if (copy_from_user(&vif, optval, sizeof(vif))) | |
1322 | return -EFAULT; | |
6ac7eb08 | 1323 | if (vif.mif6c_mifi >= MAXMIFS) |
7bc570c8 YH |
1324 | return -ENFILE; |
1325 | rtnl_lock(); | |
8229efda | 1326 | ret = mif6_add(net, &vif, sk == net->ipv6.mroute6_sk); |
7bc570c8 YH |
1327 | rtnl_unlock(); |
1328 | return ret; | |
1329 | ||
1330 | case MRT6_DEL_MIF: | |
1331 | if (optlen < sizeof(mifi_t)) | |
1332 | return -EINVAL; | |
1333 | if (copy_from_user(&mifi, optval, sizeof(mifi_t))) | |
1334 | return -EFAULT; | |
1335 | rtnl_lock(); | |
c871e664 | 1336 | ret = mif6_delete(net, mifi, NULL); |
7bc570c8 YH |
1337 | rtnl_unlock(); |
1338 | return ret; | |
1339 | ||
1340 | /* | |
1341 | * Manipulate the forwarding caches. These live | |
1342 | * in a sort of kernel/user symbiosis. | |
1343 | */ | |
1344 | case MRT6_ADD_MFC: | |
1345 | case MRT6_DEL_MFC: | |
1346 | if (optlen < sizeof(mfc)) | |
1347 | return -EINVAL; | |
1348 | if (copy_from_user(&mfc, optval, sizeof(mfc))) | |
1349 | return -EFAULT; | |
1350 | rtnl_lock(); | |
1351 | if (optname == MRT6_DEL_MFC) | |
8229efda | 1352 | ret = ip6mr_mfc_delete(net, &mfc); |
7bc570c8 | 1353 | else |
8229efda BT |
1354 | ret = ip6mr_mfc_add(net, &mfc, |
1355 | sk == net->ipv6.mroute6_sk); | |
7bc570c8 YH |
1356 | rtnl_unlock(); |
1357 | return ret; | |
1358 | ||
14fb64e1 YH |
1359 | /* |
1360 | * Control PIM assert (to activate pim will activate assert) | |
1361 | */ | |
1362 | case MRT6_ASSERT: | |
1363 | { | |
1364 | int v; | |
1365 | if (get_user(v, (int __user *)optval)) | |
1366 | return -EFAULT; | |
8229efda | 1367 | net->ipv6.mroute_do_assert = !!v; |
14fb64e1 YH |
1368 | return 0; |
1369 | } | |
1370 | ||
1371 | #ifdef CONFIG_IPV6_PIMSM_V2 | |
1372 | case MRT6_PIM: | |
1373 | { | |
a9f83bf3 | 1374 | int v; |
14fb64e1 YH |
1375 | if (get_user(v, (int __user *)optval)) |
1376 | return -EFAULT; | |
1377 | v = !!v; | |
1378 | rtnl_lock(); | |
1379 | ret = 0; | |
8229efda BT |
1380 | if (v != net->ipv6.mroute_do_pim) { |
1381 | net->ipv6.mroute_do_pim = v; | |
1382 | net->ipv6.mroute_do_assert = v; | |
14fb64e1 YH |
1383 | } |
1384 | rtnl_unlock(); | |
1385 | return ret; | |
1386 | } | |
1387 | ||
1388 | #endif | |
7bc570c8 | 1389 | /* |
7d120c55 | 1390 | * Spurious command, or MRT6_VERSION which you cannot |
7bc570c8 YH |
1391 | * set. |
1392 | */ | |
1393 | default: | |
1394 | return -ENOPROTOOPT; | |
1395 | } | |
1396 | } | |
1397 | ||
1398 | /* | |
1399 | * Getsock opt support for the multicast routing system. | |
1400 | */ | |
1401 | ||
1402 | int ip6_mroute_getsockopt(struct sock *sk, int optname, char __user *optval, | |
1403 | int __user *optlen) | |
1404 | { | |
1405 | int olr; | |
1406 | int val; | |
8229efda | 1407 | struct net *net = sock_net(sk); |
7bc570c8 YH |
1408 | |
1409 | switch (optname) { | |
1410 | case MRT6_VERSION: | |
1411 | val = 0x0305; | |
1412 | break; | |
14fb64e1 YH |
1413 | #ifdef CONFIG_IPV6_PIMSM_V2 |
1414 | case MRT6_PIM: | |
8229efda | 1415 | val = net->ipv6.mroute_do_pim; |
14fb64e1 YH |
1416 | break; |
1417 | #endif | |
1418 | case MRT6_ASSERT: | |
8229efda | 1419 | val = net->ipv6.mroute_do_assert; |
14fb64e1 | 1420 | break; |
7bc570c8 YH |
1421 | default: |
1422 | return -ENOPROTOOPT; | |
1423 | } | |
1424 | ||
1425 | if (get_user(olr, optlen)) | |
1426 | return -EFAULT; | |
1427 | ||
1428 | olr = min_t(int, olr, sizeof(int)); | |
1429 | if (olr < 0) | |
1430 | return -EINVAL; | |
1431 | ||
1432 | if (put_user(olr, optlen)) | |
1433 | return -EFAULT; | |
1434 | if (copy_to_user(optval, &val, olr)) | |
1435 | return -EFAULT; | |
1436 | return 0; | |
1437 | } | |
1438 | ||
1439 | /* | |
1440 | * The IP multicast ioctl support routines. | |
1441 | */ | |
1442 | ||
1443 | int ip6mr_ioctl(struct sock *sk, int cmd, void __user *arg) | |
1444 | { | |
1445 | struct sioc_sg_req6 sr; | |
1446 | struct sioc_mif_req6 vr; | |
1447 | struct mif_device *vif; | |
1448 | struct mfc6_cache *c; | |
8229efda | 1449 | struct net *net = sock_net(sk); |
7bc570c8 YH |
1450 | |
1451 | switch (cmd) { | |
1452 | case SIOCGETMIFCNT_IN6: | |
1453 | if (copy_from_user(&vr, arg, sizeof(vr))) | |
1454 | return -EFAULT; | |
8229efda | 1455 | if (vr.mifi >= net->ipv6.maxvif) |
7bc570c8 YH |
1456 | return -EINVAL; |
1457 | read_lock(&mrt_lock); | |
8229efda BT |
1458 | vif = &net->ipv6.vif6_table[vr.mifi]; |
1459 | if (MIF_EXISTS(net, vr.mifi)) { | |
7bc570c8 YH |
1460 | vr.icount = vif->pkt_in; |
1461 | vr.ocount = vif->pkt_out; | |
1462 | vr.ibytes = vif->bytes_in; | |
1463 | vr.obytes = vif->bytes_out; | |
1464 | read_unlock(&mrt_lock); | |
1465 | ||
1466 | if (copy_to_user(arg, &vr, sizeof(vr))) | |
1467 | return -EFAULT; | |
1468 | return 0; | |
1469 | } | |
1470 | read_unlock(&mrt_lock); | |
1471 | return -EADDRNOTAVAIL; | |
1472 | case SIOCGETSGCNT_IN6: | |
1473 | if (copy_from_user(&sr, arg, sizeof(sr))) | |
1474 | return -EFAULT; | |
1475 | ||
1476 | read_lock(&mrt_lock); | |
8229efda | 1477 | c = ip6mr_cache_find(net, &sr.src.sin6_addr, &sr.grp.sin6_addr); |
7bc570c8 YH |
1478 | if (c) { |
1479 | sr.pktcnt = c->mfc_un.res.pkt; | |
1480 | sr.bytecnt = c->mfc_un.res.bytes; | |
1481 | sr.wrong_if = c->mfc_un.res.wrong_if; | |
1482 | read_unlock(&mrt_lock); | |
1483 | ||
1484 | if (copy_to_user(arg, &sr, sizeof(sr))) | |
1485 | return -EFAULT; | |
1486 | return 0; | |
1487 | } | |
1488 | read_unlock(&mrt_lock); | |
1489 | return -EADDRNOTAVAIL; | |
1490 | default: | |
1491 | return -ENOIOCTLCMD; | |
1492 | } | |
1493 | } | |
1494 | ||
1495 | ||
1496 | static inline int ip6mr_forward2_finish(struct sk_buff *skb) | |
1497 | { | |
adf30907 | 1498 | IP6_INC_STATS_BH(dev_net(skb_dst(skb)->dev), ip6_dst_idev(skb_dst(skb)), |
483a47d2 | 1499 | IPSTATS_MIB_OUTFORWDATAGRAMS); |
7bc570c8 YH |
1500 | return dst_output(skb); |
1501 | } | |
1502 | ||
1503 | /* | |
1504 | * Processing handlers for ip6mr_forward | |
1505 | */ | |
1506 | ||
1507 | static int ip6mr_forward2(struct sk_buff *skb, struct mfc6_cache *c, int vifi) | |
1508 | { | |
1509 | struct ipv6hdr *ipv6h; | |
8229efda BT |
1510 | struct net *net = mfc6_net(c); |
1511 | struct mif_device *vif = &net->ipv6.vif6_table[vifi]; | |
7bc570c8 YH |
1512 | struct net_device *dev; |
1513 | struct dst_entry *dst; | |
1514 | struct flowi fl; | |
1515 | ||
1516 | if (vif->dev == NULL) | |
1517 | goto out_free; | |
1518 | ||
14fb64e1 YH |
1519 | #ifdef CONFIG_IPV6_PIMSM_V2 |
1520 | if (vif->flags & MIFF_REGISTER) { | |
1521 | vif->pkt_out++; | |
1522 | vif->bytes_out += skb->len; | |
dc58c78c PE |
1523 | vif->dev->stats.tx_bytes += skb->len; |
1524 | vif->dev->stats.tx_packets++; | |
8229efda | 1525 | ip6mr_cache_report(net, skb, vifi, MRT6MSG_WHOLEPKT); |
8da73b73 | 1526 | goto out_free; |
14fb64e1 YH |
1527 | } |
1528 | #endif | |
1529 | ||
7bc570c8 YH |
1530 | ipv6h = ipv6_hdr(skb); |
1531 | ||
1532 | fl = (struct flowi) { | |
1533 | .oif = vif->link, | |
1534 | .nl_u = { .ip6_u = | |
1535 | { .daddr = ipv6h->daddr, } | |
1536 | } | |
1537 | }; | |
1538 | ||
8229efda | 1539 | dst = ip6_route_output(net, NULL, &fl); |
7bc570c8 YH |
1540 | if (!dst) |
1541 | goto out_free; | |
1542 | ||
adf30907 ED |
1543 | skb_dst_drop(skb); |
1544 | skb_dst_set(skb, dst); | |
7bc570c8 YH |
1545 | |
1546 | /* | |
1547 | * RFC1584 teaches, that DVMRP/PIM router must deliver packets locally | |
1548 | * not only before forwarding, but after forwarding on all output | |
1549 | * interfaces. It is clear, if mrouter runs a multicasting | |
1550 | * program, it should receive packets not depending to what interface | |
1551 | * program is joined. | |
1552 | * If we will not make it, the program will have to join on all | |
1553 | * interfaces. On the other hand, multihoming host (or router, but | |
1554 | * not mrouter) cannot join to more than one interface - it will | |
1555 | * result in receiving multiple packets. | |
1556 | */ | |
1557 | dev = vif->dev; | |
1558 | skb->dev = dev; | |
1559 | vif->pkt_out++; | |
1560 | vif->bytes_out += skb->len; | |
1561 | ||
1562 | /* We are about to write */ | |
1563 | /* XXX: extension headers? */ | |
1564 | if (skb_cow(skb, sizeof(*ipv6h) + LL_RESERVED_SPACE(dev))) | |
1565 | goto out_free; | |
1566 | ||
1567 | ipv6h = ipv6_hdr(skb); | |
1568 | ipv6h->hop_limit--; | |
1569 | ||
1570 | IP6CB(skb)->flags |= IP6SKB_FORWARDED; | |
1571 | ||
1572 | return NF_HOOK(PF_INET6, NF_INET_FORWARD, skb, skb->dev, dev, | |
1573 | ip6mr_forward2_finish); | |
1574 | ||
1575 | out_free: | |
1576 | kfree_skb(skb); | |
1577 | return 0; | |
1578 | } | |
1579 | ||
1580 | static int ip6mr_find_vif(struct net_device *dev) | |
1581 | { | |
8229efda | 1582 | struct net *net = dev_net(dev); |
7bc570c8 | 1583 | int ct; |
8229efda BT |
1584 | for (ct = net->ipv6.maxvif - 1; ct >= 0; ct--) { |
1585 | if (net->ipv6.vif6_table[ct].dev == dev) | |
7bc570c8 YH |
1586 | break; |
1587 | } | |
1588 | return ct; | |
1589 | } | |
1590 | ||
1591 | static int ip6_mr_forward(struct sk_buff *skb, struct mfc6_cache *cache) | |
1592 | { | |
1593 | int psend = -1; | |
1594 | int vif, ct; | |
8229efda | 1595 | struct net *net = mfc6_net(cache); |
7bc570c8 YH |
1596 | |
1597 | vif = cache->mf6c_parent; | |
1598 | cache->mfc_un.res.pkt++; | |
1599 | cache->mfc_un.res.bytes += skb->len; | |
1600 | ||
14fb64e1 YH |
1601 | /* |
1602 | * Wrong interface: drop packet and (maybe) send PIM assert. | |
1603 | */ | |
8229efda | 1604 | if (net->ipv6.vif6_table[vif].dev != skb->dev) { |
14fb64e1 YH |
1605 | int true_vifi; |
1606 | ||
1607 | cache->mfc_un.res.wrong_if++; | |
1608 | true_vifi = ip6mr_find_vif(skb->dev); | |
1609 | ||
8229efda | 1610 | if (true_vifi >= 0 && net->ipv6.mroute_do_assert && |
14fb64e1 YH |
1611 | /* pimsm uses asserts, when switching from RPT to SPT, |
1612 | so that we cannot check that packet arrived on an oif. | |
1613 | It is bad, but otherwise we would need to move pretty | |
1614 | large chunk of pimd to kernel. Ough... --ANK | |
1615 | */ | |
8229efda | 1616 | (net->ipv6.mroute_do_pim || |
a21f3f99 | 1617 | cache->mfc_un.res.ttls[true_vifi] < 255) && |
14fb64e1 YH |
1618 | time_after(jiffies, |
1619 | cache->mfc_un.res.last_assert + MFC_ASSERT_THRESH)) { | |
1620 | cache->mfc_un.res.last_assert = jiffies; | |
8229efda | 1621 | ip6mr_cache_report(net, skb, true_vifi, MRT6MSG_WRONGMIF); |
14fb64e1 YH |
1622 | } |
1623 | goto dont_forward; | |
1624 | } | |
1625 | ||
8229efda BT |
1626 | net->ipv6.vif6_table[vif].pkt_in++; |
1627 | net->ipv6.vif6_table[vif].bytes_in += skb->len; | |
7bc570c8 YH |
1628 | |
1629 | /* | |
1630 | * Forward the frame | |
1631 | */ | |
1632 | for (ct = cache->mfc_un.res.maxvif - 1; ct >= cache->mfc_un.res.minvif; ct--) { | |
1633 | if (ipv6_hdr(skb)->hop_limit > cache->mfc_un.res.ttls[ct]) { | |
1634 | if (psend != -1) { | |
1635 | struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC); | |
1636 | if (skb2) | |
1637 | ip6mr_forward2(skb2, cache, psend); | |
1638 | } | |
1639 | psend = ct; | |
1640 | } | |
1641 | } | |
1642 | if (psend != -1) { | |
1643 | ip6mr_forward2(skb, cache, psend); | |
1644 | return 0; | |
1645 | } | |
1646 | ||
14fb64e1 | 1647 | dont_forward: |
7bc570c8 YH |
1648 | kfree_skb(skb); |
1649 | return 0; | |
1650 | } | |
1651 | ||
1652 | ||
1653 | /* | |
1654 | * Multicast packets for forwarding arrive here | |
1655 | */ | |
1656 | ||
1657 | int ip6_mr_input(struct sk_buff *skb) | |
1658 | { | |
1659 | struct mfc6_cache *cache; | |
8229efda | 1660 | struct net *net = dev_net(skb->dev); |
7bc570c8 YH |
1661 | |
1662 | read_lock(&mrt_lock); | |
8229efda BT |
1663 | cache = ip6mr_cache_find(net, |
1664 | &ipv6_hdr(skb)->saddr, &ipv6_hdr(skb)->daddr); | |
7bc570c8 YH |
1665 | |
1666 | /* | |
1667 | * No usable cache entry | |
1668 | */ | |
1669 | if (cache == NULL) { | |
1670 | int vif; | |
1671 | ||
1672 | vif = ip6mr_find_vif(skb->dev); | |
1673 | if (vif >= 0) { | |
8229efda | 1674 | int err = ip6mr_cache_unresolved(net, vif, skb); |
7bc570c8 YH |
1675 | read_unlock(&mrt_lock); |
1676 | ||
1677 | return err; | |
1678 | } | |
1679 | read_unlock(&mrt_lock); | |
1680 | kfree_skb(skb); | |
1681 | return -ENODEV; | |
1682 | } | |
1683 | ||
1684 | ip6_mr_forward(skb, cache); | |
1685 | ||
1686 | read_unlock(&mrt_lock); | |
1687 | ||
1688 | return 0; | |
1689 | } | |
1690 | ||
1691 | ||
1692 | static int | |
1693 | ip6mr_fill_mroute(struct sk_buff *skb, struct mfc6_cache *c, struct rtmsg *rtm) | |
1694 | { | |
1695 | int ct; | |
1696 | struct rtnexthop *nhp; | |
8229efda BT |
1697 | struct net *net = mfc6_net(c); |
1698 | struct net_device *dev = net->ipv6.vif6_table[c->mf6c_parent].dev; | |
549e028d | 1699 | u8 *b = skb_tail_pointer(skb); |
7bc570c8 YH |
1700 | struct rtattr *mp_head; |
1701 | ||
1702 | if (dev) | |
1703 | RTA_PUT(skb, RTA_IIF, 4, &dev->ifindex); | |
1704 | ||
1705 | mp_head = (struct rtattr *)skb_put(skb, RTA_LENGTH(0)); | |
1706 | ||
1707 | for (ct = c->mfc_un.res.minvif; ct < c->mfc_un.res.maxvif; ct++) { | |
1708 | if (c->mfc_un.res.ttls[ct] < 255) { | |
1709 | if (skb_tailroom(skb) < RTA_ALIGN(RTA_ALIGN(sizeof(*nhp)) + 4)) | |
1710 | goto rtattr_failure; | |
1711 | nhp = (struct rtnexthop *)skb_put(skb, RTA_ALIGN(sizeof(*nhp))); | |
1712 | nhp->rtnh_flags = 0; | |
1713 | nhp->rtnh_hops = c->mfc_un.res.ttls[ct]; | |
8229efda | 1714 | nhp->rtnh_ifindex = net->ipv6.vif6_table[ct].dev->ifindex; |
7bc570c8 YH |
1715 | nhp->rtnh_len = sizeof(*nhp); |
1716 | } | |
1717 | } | |
1718 | mp_head->rta_type = RTA_MULTIPATH; | |
549e028d | 1719 | mp_head->rta_len = skb_tail_pointer(skb) - (u8 *)mp_head; |
7bc570c8 YH |
1720 | rtm->rtm_type = RTN_MULTICAST; |
1721 | return 1; | |
1722 | ||
1723 | rtattr_failure: | |
1724 | nlmsg_trim(skb, b); | |
1725 | return -EMSGSIZE; | |
1726 | } | |
1727 | ||
8229efda BT |
1728 | int ip6mr_get_route(struct net *net, |
1729 | struct sk_buff *skb, struct rtmsg *rtm, int nowait) | |
7bc570c8 YH |
1730 | { |
1731 | int err; | |
1732 | struct mfc6_cache *cache; | |
adf30907 | 1733 | struct rt6_info *rt = (struct rt6_info *)skb_dst(skb); |
7bc570c8 YH |
1734 | |
1735 | read_lock(&mrt_lock); | |
8229efda | 1736 | cache = ip6mr_cache_find(net, &rt->rt6i_src.addr, &rt->rt6i_dst.addr); |
7bc570c8 YH |
1737 | |
1738 | if (!cache) { | |
1739 | struct sk_buff *skb2; | |
1740 | struct ipv6hdr *iph; | |
1741 | struct net_device *dev; | |
1742 | int vif; | |
1743 | ||
1744 | if (nowait) { | |
1745 | read_unlock(&mrt_lock); | |
1746 | return -EAGAIN; | |
1747 | } | |
1748 | ||
1749 | dev = skb->dev; | |
1750 | if (dev == NULL || (vif = ip6mr_find_vif(dev)) < 0) { | |
1751 | read_unlock(&mrt_lock); | |
1752 | return -ENODEV; | |
1753 | } | |
1754 | ||
1755 | /* really correct? */ | |
1756 | skb2 = alloc_skb(sizeof(struct ipv6hdr), GFP_ATOMIC); | |
1757 | if (!skb2) { | |
1758 | read_unlock(&mrt_lock); | |
1759 | return -ENOMEM; | |
1760 | } | |
1761 | ||
1762 | skb_reset_transport_header(skb2); | |
1763 | ||
1764 | skb_put(skb2, sizeof(struct ipv6hdr)); | |
1765 | skb_reset_network_header(skb2); | |
1766 | ||
1767 | iph = ipv6_hdr(skb2); | |
1768 | iph->version = 0; | |
1769 | iph->priority = 0; | |
1770 | iph->flow_lbl[0] = 0; | |
1771 | iph->flow_lbl[1] = 0; | |
1772 | iph->flow_lbl[2] = 0; | |
1773 | iph->payload_len = 0; | |
1774 | iph->nexthdr = IPPROTO_NONE; | |
1775 | iph->hop_limit = 0; | |
1776 | ipv6_addr_copy(&iph->saddr, &rt->rt6i_src.addr); | |
1777 | ipv6_addr_copy(&iph->daddr, &rt->rt6i_dst.addr); | |
1778 | ||
8229efda | 1779 | err = ip6mr_cache_unresolved(net, vif, skb2); |
7bc570c8 YH |
1780 | read_unlock(&mrt_lock); |
1781 | ||
1782 | return err; | |
1783 | } | |
1784 | ||
1785 | if (!nowait && (rtm->rtm_flags&RTM_F_NOTIFY)) | |
1786 | cache->mfc_flags |= MFC_NOTIFY; | |
1787 | ||
1788 | err = ip6mr_fill_mroute(skb, cache, rtm); | |
1789 | read_unlock(&mrt_lock); | |
1790 | return err; | |
1791 | } | |
1792 |