29 #include <asm/uaccess.h>
30 #include <linux/types.h>
31 #include <linux/capability.h>
32 #include <linux/errno.h>
35 #include <linux/kernel.h>
36 #include <linux/fcntl.h>
37 #include <linux/stat.h>
38 #include <linux/socket.h>
41 #include <linux/netdevice.h>
43 #include <linux/igmp.h>
46 #include <linux/mroute.h>
48 #include <linux/if_ether.h>
49 #include <linux/slab.h>
60 #include <linux/if_arp.h>
61 #include <linux/netfilter_ipv4.h>
63 #include <linux/export.h>
69 #if defined(CONFIG_IP_PIMSM_V1) || defined(CONFIG_IP_PIMSM_V2)
70 #define CONFIG_IP_PIMSM 1
88 #if defined(CONFIG_IP_PIMSM_V1) || defined(CONFIG_IP_PIMSM_V2)
89 int mroute_reg_vif_num;
111 #define VIF_EXISTS(_mrt, _idx) ((_mrt)->vif_table[_idx].dev != NULL)
127 static void ipmr_free_table(
struct mr_table *mrt);
132 static int ipmr_cache_report(
struct mr_table *mrt,
136 static void mroute_clean_tables(
struct mr_table *mrt);
137 static void ipmr_expire_process(
unsigned long arg);
139 #ifdef CONFIG_IP_MROUTE_MULTIPLE_TABLES
140 #define ipmr_for_each_table(mrt, net) \
141 list_for_each_entry_rcu(mrt, &net->ipv4.mr_tables, list)
154 static int ipmr_fib_lookup(
struct net *net,
struct flowi4 *flp4,
162 flowi4_to_flowi(flp4), 0, &arg);
194 static int ipmr_rule_match(
struct fib_rule *rule,
struct flowi *
fl,
int flags)
228 .
action = ipmr_rule_action,
229 .
match = ipmr_rule_match,
230 .configure = ipmr_rule_configure,
231 .compare = ipmr_rule_compare,
233 .
fill = ipmr_rule_fill,
235 .policy = ipmr_rule_policy,
239 static int __net_init ipmr_rules_init(
struct net *net)
249 INIT_LIST_HEAD(&net->
ipv4.mr_tables);
261 net->
ipv4.mr_rules_ops = ops;
271 static void __net_exit ipmr_rules_exit(
struct net *net)
277 ipmr_free_table(mrt);
282 #define ipmr_for_each_table(mrt, net) \
283 for (mrt = net->ipv4.mrt; mrt; mrt = NULL)
285 static struct mr_table *ipmr_get_table(
struct net *net,
u32 id)
287 return net->
ipv4.mrt;
290 static int ipmr_fib_lookup(
struct net *net,
struct flowi4 *flp4,
293 *mrt = net->
ipv4.mrt;
297 static int __net_init ipmr_rules_init(
struct net *net)
303 static void __net_exit ipmr_rules_exit(
struct net *net)
305 ipmr_free_table(net->
ipv4.mrt);
309 static struct mr_table *ipmr_new_table(
struct net *net,
u32 id)
314 mrt = ipmr_get_table(net,
id);
333 #ifdef CONFIG_IP_PIMSM
334 mrt->mroute_reg_vif_num = -1;
336 #ifdef CONFIG_IP_MROUTE_MULTIPLE_TABLES
337 list_add_tail_rcu(&mrt->
list, &net->
ipv4.mr_tables);
342 static void ipmr_free_table(
struct mr_table *mrt)
345 mroute_clean_tables(mrt);
353 struct net *net = dev_net(dev);
394 struct in_device *in_dev;
420 in_dev = __in_dev_get_rtnl(dev);
424 ipv4_devconf_setall(in_dev);
425 IPV4_DEVCONF(in_dev->cnf, RP_FILTER) = 0;
439 unregister_netdevice(dev);
443 #ifdef CONFIG_IP_PIMSM
447 struct net *net = dev_net(dev);
452 .flowi4_mark = skb->
mark,
456 err = ipmr_fib_lookup(net, &fl4, &mrt);
464 dev->
stats.tx_packets++;
475 static void reg_vif_setup(
struct net_device *dev)
488 struct in_device *in_dev;
501 dev_net_set(dev, net);
510 in_dev = __in_dev_get_rcu(dev);
516 ipv4_devconf_setall(in_dev);
517 IPV4_DEVCONF(in_dev->cnf, RP_FILTER) = 0;
532 unregister_netdevice(dev);
547 struct in_device *in_dev;
549 if (vifi < 0 || vifi >= mrt->
maxvif)
563 #ifdef CONFIG_IP_PIMSM
564 if (vifi == mrt->mroute_reg_vif_num)
565 mrt->mroute_reg_vif_num = -1;
568 if (vifi + 1 == mrt->
maxvif) {
571 for (tmp = vifi - 1; tmp >= 0; tmp--) {
582 in_dev = __in_dev_get_rtnl(dev);
584 IPV4_DEVCONF(in_dev->cnf, MC_FORWARDING)--;
595 static void ipmr_cache_free_rcu(
struct rcu_head *head)
602 static inline void ipmr_cache_free(
struct mfc_cache *
c)
620 if (ip_hdr(skb)->
version == 0) {
641 static void ipmr_expire_process(
unsigned long arg)
645 unsigned long expires;
648 if (!spin_trylock(&mfc_unres_lock)) {
662 if (interval < expires)
668 ipmr_destroy_unres(mrt, c);
675 spin_unlock(&mfc_unres_lock);
689 for (vifi = 0; vifi < mrt->
maxvif; vifi++) {
691 ttls[vifi] && ttls[vifi] < 255) {
692 cache->
mfc_un.
res.ttls[vifi] = ttls[vifi];
701 static int vif_add(
struct net *net,
struct mr_table *mrt,
702 struct vifctl *vifc,
int mrtsock)
707 struct in_device *in_dev;
715 #ifdef CONFIG_IP_PIMSM
721 if (mrt->mroute_reg_vif_num >= 0)
723 dev = ipmr_reg_vif(net, mrt);
728 unregister_netdevice(dev);
735 dev = ipmr_new_tunnel(net, vifc);
740 ipmr_del_tunnel(dev, vifc);
750 if (dev && __in_dev_get_rtnl(dev) ==
NULL) {
769 in_dev = __in_dev_get_rtnl(dev);
774 IPV4_DEVCONF(in_dev->cnf, MC_FORWARDING)++;
797 #ifdef CONFIG_IP_PIMSM
799 mrt->mroute_reg_vif_num = vifi;
825 static struct mfc_cache *ipmr_cache_alloc(
void)
834 static struct mfc_cache *ipmr_cache_alloc_unres(
void)
849 static void ipmr_cache_resolve(
struct net *net,
struct mr_table *mrt,
857 while ((skb = __skb_dequeue(&uc->
mfc_un.
unres.unresolved))) {
858 if (ip_hdr(skb)->
version == 0) {
861 if (__ipmr_fill_mroute(mrt, skb, c,
NLMSG_DATA(nlh)) > 0) {
875 ip_mr_forward(net, mrt, skb, c, 0);
887 static int ipmr_cache_report(
struct mr_table *mrt,
891 const int ihl = ip_hdrlen(pkt);
894 struct sock *mroute_sk;
897 #ifdef CONFIG_IP_PIMSM
907 #ifdef CONFIG_IP_PIMSM
915 skb_reset_network_header(skb);
916 skb_reset_transport_header(skb);
917 msg = (
struct igmpmsg *)skb_network_header(skb);
918 memcpy(msg, skb_network_header(pkt),
sizeof(
struct iphdr));
921 msg->
im_vif = mrt->mroute_reg_vif_num;
922 ip_hdr(skb)->ihl =
sizeof(
struct iphdr) >> 2;
924 sizeof(
struct iphdr));
933 skb_copy_to_linear_data(skb, pkt->
data, ihl);
934 ip_hdr(skb)->protocol = 0;
935 msg = (
struct igmpmsg *)skb_network_header(skb);
937 skb_dst_set(skb, dst_clone(skb_dst(pkt)));
945 ip_hdr(skb)->tot_len =
htons(skb->
len);
951 if (mroute_sk ==
NULL) {
979 const struct iphdr *iph = ip_hdr(skb);
981 spin_lock_bh(&mfc_unres_lock);
994 (c = ipmr_cache_alloc_unres()) ==
NULL) {
995 spin_unlock_bh(&mfc_unres_lock);
1014 spin_unlock_bh(&mfc_unres_lock);
1038 spin_unlock_bh(&mfc_unres_lock);
1046 static int ipmr_mfc_delete(
struct mr_table *mrt,
struct mfcctl *mfc)
1056 list_del_rcu(&c->
list);
1065 static int ipmr_mfc_add(
struct net *net,
struct mr_table *mrt,
1066 struct mfcctl *mfc,
int mrtsock)
1088 ipmr_update_thresholds(mrt, c, mfc->
mfcc_ttls);
1098 c = ipmr_cache_alloc();
1105 ipmr_update_thresholds(mrt, c, mfc->
mfcc_ttls);
1116 spin_lock_bh(&mfc_unres_lock);
1128 spin_unlock_bh(&mfc_unres_lock);
1131 ipmr_cache_resolve(net, mrt, uc, c);
1132 ipmr_cache_free(uc);
1141 static void mroute_clean_tables(
struct mr_table *mrt)
1149 for (i = 0; i < mrt->
maxvif; i++) {
1151 vif_delete(mrt, i, 0, &
list);
1161 list_del_rcu(&c->
list);
1167 spin_lock_bh(&mfc_unres_lock);
1170 ipmr_destroy_unres(mrt, c);
1172 spin_unlock_bh(&mfc_unres_lock);
1179 static void mrtsock_destruct(
struct sock *
sk)
1181 struct net *net = sock_net(sk);
1187 IPV4_DEVCONF_ALL(net, MC_FORWARDING)--;
1189 mroute_clean_tables(mrt);
1207 struct net *net = sock_net(sk);
1225 if (optlen !=
sizeof(
int))
1237 IPV4_DEVCONF_ALL(net, MC_FORWARDING)++;
1247 if (optlen !=
sizeof(vif))
1255 ret = vif_add(net, mrt, &vif,
1269 if (optlen !=
sizeof(mfc))
1275 ret = ipmr_mfc_delete(mrt, &mfc);
1277 ret = ipmr_mfc_add(net, mrt, &mfc,
1292 #ifdef CONFIG_IP_PIMSM
1311 #ifdef CONFIG_IP_MROUTE_MULTIPLE_TABLES
1316 if (optlen !=
sizeof(
u32))
1322 if (v != RT_TABLE_DEFAULT && v >= 1000000000)
1330 if (!ipmr_new_table(net, v))
1332 raw_sk(sk)->ipmr_table =
v;
1355 struct net *net = sock_net(sk);
1363 #ifdef CONFIG_IP_PIMSM
1372 olr =
min_t(
unsigned int, olr,
sizeof(
int));
1380 #ifdef CONFIG_IP_PIMSM
1401 struct net *net = sock_net(sk);
1434 c = ipmr_cache_find(mrt, sr.
src.s_addr, sr.
grp.s_addr);
1452 #ifdef CONFIG_COMPAT
1453 struct compat_sioc_sg_req {
1461 struct compat_sioc_vif_req {
1469 int ipmr_compat_ioctl(
struct sock *sk,
unsigned int cmd,
void __user *arg)
1471 struct compat_sioc_sg_req
sr;
1472 struct compat_sioc_vif_req vr;
1475 struct net *net = sock_net(sk);
1486 if (vr.vifi >= mrt->
maxvif)
1508 c = ipmr_cache_find(mrt,
sr.src.s_addr,
sr.grp.s_addr);
1531 struct net *net = dev_net(dev);
1541 for (ct = 0; ct < mrt->
maxvif; ct++, v++) {
1543 vif_delete(mrt, ct, 1,
NULL);
1551 .notifier_call = ipmr_device_event,
1563 const struct iphdr *old_iph = ip_hdr(skb);
1567 skb_reset_network_header(skb);
1579 ip_select_ident(iph, skb_dst(skb),
NULL);
1586 static inline int ipmr_forward_finish(
struct sk_buff *skb)
1596 return dst_output(skb);
1603 static void ipmr_queue_xmit(
struct net *net,
struct mr_table *mrt,
1606 const struct iphdr *iph = ip_hdr(skb);
1616 #ifdef CONFIG_IP_PIMSM
1620 vif->
dev->stats.tx_bytes += skb->
len;
1621 vif->
dev->stats.tx_packets++;
1628 rt = ip_route_output_ports(net, &fl4,
NULL,
1635 encap =
sizeof(
struct iphdr);
1637 rt = ip_route_output_ports(net, &fl4,
NULL, iph->
daddr, 0,
1660 if (skb_cow(skb, encap)) {
1669 skb_dst_set(skb, &rt->
dst);
1670 ip_decrease_ttl(ip_hdr(skb));
1678 vif->
dev->stats.tx_packets++;
1679 vif->
dev->stats.tx_bytes += skb->
len;
1696 ipmr_forward_finish);
1707 for (ct = mrt->
maxvif-1; ct >= 0; ct--) {
1716 static int ip_mr_forward(
struct net *net,
struct mr_table *mrt,
1733 if (rt_is_output_route(skb_rtable(skb))) {
1749 true_vifi = ipmr_find_vif(mrt, skb->
dev);
1758 cache->
mfc_un.
res.ttls[true_vifi] < 255) &&
1780 ipmr_queue_xmit(net, mrt, skb2, cache,
1791 ipmr_queue_xmit(net, mrt, skb2, cache, psend);
1793 ipmr_queue_xmit(net, mrt, skb, cache, psend);
1804 static struct mr_table *ipmr_rt_fib_lookup(
struct net *net,
struct sk_buff *skb)
1806 struct rtable *rt = skb_rtable(skb);
1807 struct iphdr *iph = ip_hdr(skb);
1810 .saddr = iph->
saddr,
1812 .flowi4_oif = (rt_is_output_route(rt) ?
1813 skb->
dev->ifindex : 0),
1822 err = ipmr_fib_lookup(net, &fl4, &mrt);
1824 return ERR_PTR(err);
1836 struct net *net = dev_net(skb->
dev);
1837 int local = skb_rtable(skb)->rt_flags &
RTCF_LOCAL;
1846 mrt = ipmr_rt_fib_lookup(net, skb);
1849 return PTR_ERR(mrt);
1862 struct sock *mroute_sk;
1874 cache = ipmr_cache_find(mrt, ip_hdr(skb)->saddr, ip_hdr(skb)->daddr);
1879 if (cache ==
NULL) {
1891 vif = ipmr_find_vif(mrt, skb->
dev);
1893 int err2 = ipmr_cache_unresolved(mrt, vif, skb);
1904 ip_mr_forward(net, mrt, skb, cache, local);
1919 #ifdef CONFIG_IP_PIMSM
1922 unsigned int pimlen)
1925 struct iphdr *encap;
1927 encap = (
struct iphdr *)(skb_transport_header(skb) + pimlen);
1934 if (!ipv4_is_multicast(encap->
daddr) ||
1940 if (mrt->mroute_reg_vif_num >= 0)
1941 reg_dev = mrt->
vif_table[mrt->mroute_reg_vif_num].dev;
1944 if (reg_dev ==
NULL)
1949 skb_reset_network_header(skb);
1954 skb_tunnel_rx(skb, reg_dev);
1962 #ifdef CONFIG_IP_PIMSM_V1
1970 struct net *net = dev_net(skb->
dev);
1973 if (!pskb_may_pull(skb,
sizeof(*pim) +
sizeof(
struct iphdr)))
1976 pim = igmp_hdr(skb);
1978 mrt = ipmr_rt_fib_lookup(net, skb);
1985 if (__pim_rcv(mrt, skb,
sizeof(*pim))) {
1993 #ifdef CONFIG_IP_PIMSM_V2
1994 static int pim_rcv(
struct sk_buff *skb)
1997 struct net *net = dev_net(skb->
dev);
2000 if (!pskb_may_pull(skb,
sizeof(*pim) +
sizeof(
struct iphdr)))
2003 pim = (
struct pimreghdr *)skb_transport_header(skb);
2010 mrt = ipmr_rt_fib_lookup(net, skb);
2013 if (__pim_rcv(mrt, skb,
sizeof(*pim))) {
2021 static int __ipmr_fill_mroute(
struct mr_table *mrt,
struct sk_buff *skb,
2039 for (ct = c->
mfc_un.
res.minvif; ct < c->mfc_un.res.maxvif; ct++) {
2042 nla_nest_cancel(skb, mp_attr);
2053 nla_nest_end(skb, mp_attr);
2061 struct rtmsg *rtm,
int nowait)
2072 cache = ipmr_cache_find(mrt, saddr, daddr);
2074 if (cache ==
NULL) {
2088 vif = ipmr_find_vif(mrt, dev);
2102 skb_reset_network_header(skb2);
2104 iph->ihl =
sizeof(
struct iphdr) >> 2;
2108 err = ipmr_cache_unresolved(mrt, vif, skb2);
2117 err = __ipmr_fill_mroute(mrt, skb, cache, rtm);
2123 static int ipmr_fill_mroute(
struct mr_table *mrt,
struct sk_buff *skb,
2133 rtm = nlmsg_data(nlh);
2140 goto nla_put_failure;
2148 goto nla_put_failure;
2149 if (__ipmr_fill_mroute(mrt, skb, c, rtm) < 0)
2150 goto nla_put_failure;
2152 return nlmsg_end(skb, nlh);
2155 nlmsg_cancel(skb, nlh);
2161 struct net *net = sock_net(skb->
sk);
2164 unsigned int t = 0, s_t;
2165 unsigned int h = 0, s_h;
2166 unsigned int e = 0, s_e;
2182 if (ipmr_fill_mroute(mrt, skb,
2206 #ifdef CONFIG_PROC_FS
2211 struct ipmr_vif_iter {
2217 static struct vif_device *ipmr_vif_seq_idx(
struct net *net,
2218 struct ipmr_vif_iter *iter,
2223 for (iter->ct = 0; iter->ct < mrt->
maxvif; ++iter->ct) {
2232 static void *ipmr_vif_seq_start(
struct seq_file *seq, loff_t *pos)
2235 struct ipmr_vif_iter *iter = seq->private;
2236 struct net *net = seq_file_net(seq);
2246 return *pos ? ipmr_vif_seq_idx(net, seq->private, *pos - 1)
2250 static void *ipmr_vif_seq_next(
struct seq_file *seq,
void *v, loff_t *pos)
2252 struct ipmr_vif_iter *iter = seq->
private;
2253 struct net *net = seq_file_net(seq);
2258 return ipmr_vif_seq_idx(net, iter, 0);
2260 while (++iter->ct < mrt->
maxvif) {
2268 static void ipmr_vif_seq_stop(
struct seq_file *seq,
void *v)
2274 static int ipmr_vif_seq_show(
struct seq_file *seq,
void *v)
2276 struct ipmr_vif_iter *iter = seq->
private;
2281 "Interface BytesIn PktsIn BytesOut PktsOut Flags Local Remote\n");
2284 const char *name = vif->
dev ? vif->
dev->name :
"none";
2287 "%2Zd %-10s %8ld %7ld %8ld %7ld %05X %08X %08X\n",
2297 .
start = ipmr_vif_seq_start,
2298 .next = ipmr_vif_seq_next,
2299 .stop = ipmr_vif_seq_stop,
2300 .show = ipmr_vif_seq_show,
2306 sizeof(
struct ipmr_vif_iter));
2311 .open = ipmr_vif_open,
2317 struct ipmr_mfc_iter {
2325 static struct mfc_cache *ipmr_mfc_seq_idx(
struct net *net,
2326 struct ipmr_mfc_iter *it, loff_t pos)
2332 for (it->ct = 0; it->ct < MFC_LINES; it->ct++) {
2334 list_for_each_entry_rcu(mfc, it->cache,
list)
2340 spin_lock_bh(&mfc_unres_lock);
2341 it->cache = &mrt->mfc_unres_queue;
2345 spin_unlock_bh(&mfc_unres_lock);
2352 static
void *ipmr_mfc_seq_start(
struct seq_file *seq, loff_t *pos)
2354 struct ipmr_mfc_iter *it = seq->private;
2355 struct net *net = seq_file_net(seq);
2365 return *pos ? ipmr_mfc_seq_idx(net, seq->private, *pos - 1)
2369 static void *ipmr_mfc_seq_next(
struct seq_file *seq,
void *v, loff_t *pos)
2372 struct ipmr_mfc_iter *it = seq->
private;
2373 struct net *net = seq_file_net(seq);
2379 return ipmr_mfc_seq_idx(net, seq->
private, 0);
2381 if (mfc->
list.next != it->cache)
2389 while (++it->ct < MFC_LINES) {
2391 if (list_empty(it->cache))
2401 spin_lock_bh(&mfc_unres_lock);
2402 if (!list_empty(it->cache))
2406 spin_unlock_bh(&mfc_unres_lock);
2412 static void ipmr_mfc_seq_stop(
struct seq_file *seq,
void *v)
2414 struct ipmr_mfc_iter *it = seq->
private;
2418 spin_unlock_bh(&mfc_unres_lock);
2423 static int ipmr_mfc_seq_show(
struct seq_file *seq,
void *v)
2429 "Group Origin Iif Pkts Bytes Wrong Oifs\n");
2432 const struct ipmr_mfc_iter *it = seq->
private;
2433 const struct mr_table *mrt = it->mrt;
2446 n < mfc->mfc_un.res.maxvif; n++) {
2457 seq_printf(seq,
" %8lu %8lu %8lu", 0ul, 0ul, 0ul);
2465 .
start = ipmr_mfc_seq_start,
2466 .next = ipmr_mfc_seq_next,
2467 .stop = ipmr_mfc_seq_stop,
2468 .show = ipmr_mfc_seq_show,
2471 static int ipmr_mfc_open(
struct inode *inode,
struct file *file)
2474 sizeof(
struct ipmr_mfc_iter));
2479 .open = ipmr_mfc_open,
2486 #ifdef CONFIG_IP_PIMSM_V2
2497 static int __net_init ipmr_net_init(
struct net *net)
2501 err = ipmr_rules_init(net);
2505 #ifdef CONFIG_PROC_FS
2510 goto proc_cache_fail;
2514 #ifdef CONFIG_PROC_FS
2518 ipmr_rules_exit(net);
2524 static void __net_exit ipmr_net_exit(
struct net *net)
2526 #ifdef CONFIG_PROC_FS
2530 ipmr_rules_exit(net);
2534 .init = ipmr_net_init,
2535 .exit = ipmr_net_exit,
2551 goto reg_pernet_fail;
2555 goto reg_notif_fail;
2556 #ifdef CONFIG_IP_PIMSM_V2
2558 pr_err(
"%s: can't add PIM protocol\n", __func__);
2560 goto add_proto_fail;
2564 NULL, ipmr_rtm_dumproute, NULL);
2567 #ifdef CONFIG_IP_PIMSM_V2