12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582 |
- /*
- * IP multicast routing support for mrouted 3.6/3.8
- *
- * (c) 1995 Alan Cox, <alan@lxorguk.ukuu.org.uk>
- * Linux Consultancy and Custom Driver Development
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU General Public License
- * as published by the Free Software Foundation; either version
- * 2 of the License, or (at your option) any later version.
- *
- * Fixes:
- * Michael Chastain : Incorrect size of copying.
- * Alan Cox : Added the cache manager code
- * Alan Cox : Fixed the clone/copy bug and device race.
- * Mike McLagan : Routing by source
- * Malcolm Beattie : Buffer handling fixes.
- * Alexey Kuznetsov : Double buffer free and other fixes.
- * SVR Anand : Fixed several multicast bugs and problems.
- * Alexey Kuznetsov : Status, optimisations and more.
- * Brad Parker : Better behaviour on mrouted upcall
- * overflow.
- * Carlos Picoto : PIMv1 Support
- * Pavlin Ivanov Radoslavov: PIMv2 Registers must checksum only PIM header
- * Relax this requirement to work with older peers.
- *
- */
- #include <asm/uaccess.h>
- #include <linux/types.h>
- #include <linux/capability.h>
- #include <linux/errno.h>
- #include <linux/timer.h>
- #include <linux/mm.h>
- #include <linux/kernel.h>
- #include <linux/fcntl.h>
- #include <linux/stat.h>
- #include <linux/socket.h>
- #include <linux/in.h>
- #include <linux/inet.h>
- #include <linux/netdevice.h>
- #include <linux/inetdevice.h>
- #include <linux/igmp.h>
- #include <linux/proc_fs.h>
- #include <linux/seq_file.h>
- #include <linux/mroute.h>
- #include <linux/init.h>
- #include <linux/if_ether.h>
- #include <linux/slab.h>
- #include <net/net_namespace.h>
- #include <net/ip.h>
- #include <net/protocol.h>
- #include <linux/skbuff.h>
- #include <net/route.h>
- #include <net/sock.h>
- #include <net/icmp.h>
- #include <net/udp.h>
- #include <net/raw.h>
- #include <linux/notifier.h>
- #include <linux/if_arp.h>
- #include <linux/netfilter_ipv4.h>
- #include <linux/compat.h>
- #include <linux/export.h>
- #include <net/ipip.h>
- #include <net/checksum.h>
- #include <net/netlink.h>
- #include <net/fib_rules.h>
- #if defined(CONFIG_IP_PIMSM_V1) || defined(CONFIG_IP_PIMSM_V2)
- #define CONFIG_IP_PIMSM 1
- #endif
- struct mr_table {
- struct list_head list;
- #ifdef CONFIG_NET_NS
- struct net *net;
- #endif
- u32 id;
- struct sock __rcu *mroute_sk;
- struct timer_list ipmr_expire_timer;
- struct list_head mfc_unres_queue;
- struct list_head mfc_cache_array[MFC_LINES];
- struct vif_device vif_table[MAXVIFS];
- int maxvif;
- atomic_t cache_resolve_queue_len;
- int mroute_do_assert;
- int mroute_do_pim;
- #if defined(CONFIG_IP_PIMSM_V1) || defined(CONFIG_IP_PIMSM_V2)
- int mroute_reg_vif_num;
- #endif
- };
- struct ipmr_rule {
- struct fib_rule common;
- };
- struct ipmr_result {
- struct mr_table *mrt;
- };
- /* Big lock, protecting vif table, mrt cache and mroute socket state.
- * Note that the changes are semaphored via rtnl_lock.
- */
- static DEFINE_RWLOCK(mrt_lock);
- /*
- * Multicast router control variables
- */
- #define VIF_EXISTS(_mrt, _idx) ((_mrt)->vif_table[_idx].dev != NULL)
- /* Special spinlock for queue of unresolved entries */
- static DEFINE_SPINLOCK(mfc_unres_lock);
- /* We return to original Alan's scheme. Hash table of resolved
- * entries is changed only in process context and protected
- * with weak lock mrt_lock. Queue of unresolved entries is protected
- * with strong spinlock mfc_unres_lock.
- *
- * In this case data path is free of exclusive locks at all.
- */
- static struct kmem_cache *mrt_cachep __read_mostly;
- static struct mr_table *ipmr_new_table(struct net *net, u32 id);
- static void ipmr_free_table(struct mr_table *mrt);
- static int ip_mr_forward(struct net *net, struct mr_table *mrt,
- struct sk_buff *skb, struct mfc_cache *cache,
- int local);
- static int ipmr_cache_report(struct mr_table *mrt,
- struct sk_buff *pkt, vifi_t vifi, int assert);
- static int __ipmr_fill_mroute(struct mr_table *mrt, struct sk_buff *skb,
- struct mfc_cache *c, struct rtmsg *rtm);
- static void mroute_clean_tables(struct mr_table *mrt);
- static void ipmr_expire_process(unsigned long arg);
- #ifdef CONFIG_IP_MROUTE_MULTIPLE_TABLES
- #define ipmr_for_each_table(mrt, net) \
- list_for_each_entry_rcu(mrt, &net->ipv4.mr_tables, list)
- static struct mr_table *ipmr_get_table(struct net *net, u32 id)
- {
- struct mr_table *mrt;
- ipmr_for_each_table(mrt, net) {
- if (mrt->id == id)
- return mrt;
- }
- return NULL;
- }
- static int ipmr_fib_lookup(struct net *net, struct flowi4 *flp4,
- struct mr_table **mrt)
- {
- int err;
- struct ipmr_result res;
- struct fib_lookup_arg arg = {
- .result = &res,
- .flags = FIB_LOOKUP_NOREF,
- };
- err = fib_rules_lookup(net->ipv4.mr_rules_ops,
- flowi4_to_flowi(flp4), 0, &arg);
- if (err < 0)
- return err;
- *mrt = res.mrt;
- return 0;
- }
- static int ipmr_rule_action(struct fib_rule *rule, struct flowi *flp,
- int flags, struct fib_lookup_arg *arg)
- {
- struct ipmr_result *res = arg->result;
- struct mr_table *mrt;
- switch (rule->action) {
- case FR_ACT_TO_TBL:
- break;
- case FR_ACT_UNREACHABLE:
- return -ENETUNREACH;
- case FR_ACT_PROHIBIT:
- return -EACCES;
- case FR_ACT_BLACKHOLE:
- default:
- return -EINVAL;
- }
- mrt = ipmr_get_table(rule->fr_net, rule->table);
- if (mrt == NULL)
- return -EAGAIN;
- res->mrt = mrt;
- return 0;
- }
- static int ipmr_rule_match(struct fib_rule *rule, struct flowi *fl, int flags)
- {
- return 1;
- }
- static const struct nla_policy ipmr_rule_policy[FRA_MAX + 1] = {
- FRA_GENERIC_POLICY,
- };
- static int ipmr_rule_configure(struct fib_rule *rule, struct sk_buff *skb,
- struct fib_rule_hdr *frh, struct nlattr **tb)
- {
- return 0;
- }
- static int ipmr_rule_compare(struct fib_rule *rule, struct fib_rule_hdr *frh,
- struct nlattr **tb)
- {
- return 1;
- }
- static int ipmr_rule_fill(struct fib_rule *rule, struct sk_buff *skb,
- struct fib_rule_hdr *frh)
- {
- frh->dst_len = 0;
- frh->src_len = 0;
- frh->tos = 0;
- return 0;
- }
- static const struct fib_rules_ops __net_initdata ipmr_rules_ops_template = {
- .family = RTNL_FAMILY_IPMR,
- .rule_size = sizeof(struct ipmr_rule),
- .addr_size = sizeof(u32),
- .action = ipmr_rule_action,
- .match = ipmr_rule_match,
- .configure = ipmr_rule_configure,
- .compare = ipmr_rule_compare,
- .default_pref = fib_default_rule_pref,
- .fill = ipmr_rule_fill,
- .nlgroup = RTNLGRP_IPV4_RULE,
- .policy = ipmr_rule_policy,
- .owner = THIS_MODULE,
- };
- static int __net_init ipmr_rules_init(struct net *net)
- {
- struct fib_rules_ops *ops;
- struct mr_table *mrt;
- int err;
- ops = fib_rules_register(&ipmr_rules_ops_template, net);
- if (IS_ERR(ops))
- return PTR_ERR(ops);
- INIT_LIST_HEAD(&net->ipv4.mr_tables);
- mrt = ipmr_new_table(net, RT_TABLE_DEFAULT);
- if (mrt == NULL) {
- err = -ENOMEM;
- goto err1;
- }
- err = fib_default_rule_add(ops, 0x7fff, RT_TABLE_DEFAULT, 0);
- if (err < 0)
- goto err2;
- net->ipv4.mr_rules_ops = ops;
- return 0;
- err2:
- kfree(mrt);
- err1:
- fib_rules_unregister(ops);
- return err;
- }
- static void __net_exit ipmr_rules_exit(struct net *net)
- {
- struct mr_table *mrt, *next;
- list_for_each_entry_safe(mrt, next, &net->ipv4.mr_tables, list) {
- list_del(&mrt->list);
- ipmr_free_table(mrt);
- }
- fib_rules_unregister(net->ipv4.mr_rules_ops);
- }
- #else
- #define ipmr_for_each_table(mrt, net) \
- for (mrt = net->ipv4.mrt; mrt; mrt = NULL)
- static struct mr_table *ipmr_get_table(struct net *net, u32 id)
- {
- return net->ipv4.mrt;
- }
- static int ipmr_fib_lookup(struct net *net, struct flowi4 *flp4,
- struct mr_table **mrt)
- {
- *mrt = net->ipv4.mrt;
- return 0;
- }
- static int __net_init ipmr_rules_init(struct net *net)
- {
- net->ipv4.mrt = ipmr_new_table(net, RT_TABLE_DEFAULT);
- return net->ipv4.mrt ? 0 : -ENOMEM;
- }
- static void __net_exit ipmr_rules_exit(struct net *net)
- {
- ipmr_free_table(net->ipv4.mrt);
- }
- #endif
- static struct mr_table *ipmr_new_table(struct net *net, u32 id)
- {
- struct mr_table *mrt;
- unsigned int i;
- mrt = ipmr_get_table(net, id);
- if (mrt != NULL)
- return mrt;
- mrt = kzalloc(sizeof(*mrt), GFP_KERNEL);
- if (mrt == NULL)
- return NULL;
- write_pnet(&mrt->net, net);
- mrt->id = id;
- /* Forwarding cache */
- for (i = 0; i < MFC_LINES; i++)
- INIT_LIST_HEAD(&mrt->mfc_cache_array[i]);
- INIT_LIST_HEAD(&mrt->mfc_unres_queue);
- setup_timer(&mrt->ipmr_expire_timer, ipmr_expire_process,
- (unsigned long)mrt);
- #ifdef CONFIG_IP_PIMSM
- mrt->mroute_reg_vif_num = -1;
- #endif
- #ifdef CONFIG_IP_MROUTE_MULTIPLE_TABLES
- list_add_tail_rcu(&mrt->list, &net->ipv4.mr_tables);
- #endif
- return mrt;
- }
- static void ipmr_free_table(struct mr_table *mrt)
- {
- del_timer_sync(&mrt->ipmr_expire_timer);
- mroute_clean_tables(mrt);
- kfree(mrt);
- }
- /* Service routines creating virtual interfaces: DVMRP tunnels and PIMREG */
- static void ipmr_del_tunnel(struct net_device *dev, struct vifctl *v)
- {
- struct net *net = dev_net(dev);
- dev_close(dev);
- dev = __dev_get_by_name(net, "tunl0");
- if (dev) {
- const struct net_device_ops *ops = dev->netdev_ops;
- struct ifreq ifr;
- struct ip_tunnel_parm p;
- memset(&p, 0, sizeof(p));
- p.iph.daddr = v->vifc_rmt_addr.s_addr;
- p.iph.saddr = v->vifc_lcl_addr.s_addr;
- p.iph.version = 4;
- p.iph.ihl = 5;
- p.iph.protocol = IPPROTO_IPIP;
- sprintf(p.name, "dvmrp%d", v->vifc_vifi);
- ifr.ifr_ifru.ifru_data = (__force void __user *)&p;
- if (ops->ndo_do_ioctl) {
- mm_segment_t oldfs = get_fs();
- set_fs(KERNEL_DS);
- ops->ndo_do_ioctl(dev, &ifr, SIOCDELTUNNEL);
- set_fs(oldfs);
- }
- }
- }
- static
- struct net_device *ipmr_new_tunnel(struct net *net, struct vifctl *v)
- {
- struct net_device *dev;
- dev = __dev_get_by_name(net, "tunl0");
- if (dev) {
- const struct net_device_ops *ops = dev->netdev_ops;
- int err;
- struct ifreq ifr;
- struct ip_tunnel_parm p;
- struct in_device *in_dev;
- memset(&p, 0, sizeof(p));
- p.iph.daddr = v->vifc_rmt_addr.s_addr;
- p.iph.saddr = v->vifc_lcl_addr.s_addr;
- p.iph.version = 4;
- p.iph.ihl = 5;
- p.iph.protocol = IPPROTO_IPIP;
- sprintf(p.name, "dvmrp%d", v->vifc_vifi);
- ifr.ifr_ifru.ifru_data = (__force void __user *)&p;
- if (ops->ndo_do_ioctl) {
- mm_segment_t oldfs = get_fs();
- set_fs(KERNEL_DS);
- err = ops->ndo_do_ioctl(dev, &ifr, SIOCADDTUNNEL);
- set_fs(oldfs);
- } else {
- err = -EOPNOTSUPP;
- }
- dev = NULL;
- if (err == 0 &&
- (dev = __dev_get_by_name(net, p.name)) != NULL) {
- dev->flags |= IFF_MULTICAST;
- in_dev = __in_dev_get_rtnl(dev);
- if (in_dev == NULL)
- goto failure;
- ipv4_devconf_setall(in_dev);
- IPV4_DEVCONF(in_dev->cnf, RP_FILTER) = 0;
- if (dev_open(dev))
- goto failure;
- dev_hold(dev);
- }
- }
- return dev;
- failure:
- /* allow the register to be completed before unregistering. */
- rtnl_unlock();
- rtnl_lock();
- unregister_netdevice(dev);
- return NULL;
- }
- #ifdef CONFIG_IP_PIMSM
- static netdev_tx_t reg_vif_xmit(struct sk_buff *skb, struct net_device *dev)
- {
- struct net *net = dev_net(dev);
- struct mr_table *mrt;
- struct flowi4 fl4 = {
- .flowi4_oif = dev->ifindex,
- .flowi4_iif = skb->skb_iif ? : LOOPBACK_IFINDEX,
- .flowi4_mark = skb->mark,
- };
- int err;
- err = ipmr_fib_lookup(net, &fl4, &mrt);
- if (err < 0) {
- kfree_skb(skb);
- return err;
- }
- read_lock(&mrt_lock);
- dev->stats.tx_bytes += skb->len;
- dev->stats.tx_packets++;
- ipmr_cache_report(mrt, skb, mrt->mroute_reg_vif_num, IGMPMSG_WHOLEPKT);
- read_unlock(&mrt_lock);
- kfree_skb(skb);
- return NETDEV_TX_OK;
- }
- static const struct net_device_ops reg_vif_netdev_ops = {
- .ndo_start_xmit = reg_vif_xmit,
- };
- static void reg_vif_setup(struct net_device *dev)
- {
- dev->type = ARPHRD_PIMREG;
- dev->mtu = ETH_DATA_LEN - sizeof(struct iphdr) - 8;
- dev->flags = IFF_NOARP;
- dev->netdev_ops = ®_vif_netdev_ops,
- dev->destructor = free_netdev;
- dev->features |= NETIF_F_NETNS_LOCAL;
- }
- static struct net_device *ipmr_reg_vif(struct net *net, struct mr_table *mrt)
- {
- struct net_device *dev;
- struct in_device *in_dev;
- char name[IFNAMSIZ];
- if (mrt->id == RT_TABLE_DEFAULT)
- sprintf(name, "pimreg");
- else
- sprintf(name, "pimreg%u", mrt->id);
- dev = alloc_netdev(0, name, reg_vif_setup);
- if (dev == NULL)
- return NULL;
- dev_net_set(dev, net);
- if (register_netdevice(dev)) {
- free_netdev(dev);
- return NULL;
- }
- dev->iflink = 0;
- rcu_read_lock();
- in_dev = __in_dev_get_rcu(dev);
- if (!in_dev) {
- rcu_read_unlock();
- goto failure;
- }
- ipv4_devconf_setall(in_dev);
- IPV4_DEVCONF(in_dev->cnf, RP_FILTER) = 0;
- rcu_read_unlock();
- if (dev_open(dev))
- goto failure;
- dev_hold(dev);
- return dev;
- failure:
- /* allow the register to be completed before unregistering. */
- rtnl_unlock();
- rtnl_lock();
- unregister_netdevice(dev);
- return NULL;
- }
- #endif
- /*
- * Delete a VIF entry
- * @notify: Set to 1, if the caller is a notifier_call
- */
- static int vif_delete(struct mr_table *mrt, int vifi, int notify,
- struct list_head *head)
- {
- struct vif_device *v;
- struct net_device *dev;
- struct in_device *in_dev;
- if (vifi < 0 || vifi >= mrt->maxvif)
- return -EADDRNOTAVAIL;
- v = &mrt->vif_table[vifi];
- write_lock_bh(&mrt_lock);
- dev = v->dev;
- v->dev = NULL;
- if (!dev) {
- write_unlock_bh(&mrt_lock);
- return -EADDRNOTAVAIL;
- }
- #ifdef CONFIG_IP_PIMSM
- if (vifi == mrt->mroute_reg_vif_num)
- mrt->mroute_reg_vif_num = -1;
- #endif
- if (vifi + 1 == mrt->maxvif) {
- int tmp;
- for (tmp = vifi - 1; tmp >= 0; tmp--) {
- if (VIF_EXISTS(mrt, tmp))
- break;
- }
- mrt->maxvif = tmp+1;
- }
- write_unlock_bh(&mrt_lock);
- dev_set_allmulti(dev, -1);
- in_dev = __in_dev_get_rtnl(dev);
- if (in_dev) {
- IPV4_DEVCONF(in_dev->cnf, MC_FORWARDING)--;
- ip_rt_multicast_event(in_dev);
- }
- if (v->flags & (VIFF_TUNNEL | VIFF_REGISTER) && !notify)
- unregister_netdevice_queue(dev, head);
- dev_put(dev);
- return 0;
- }
- static void ipmr_cache_free_rcu(struct rcu_head *head)
- {
- struct mfc_cache *c = container_of(head, struct mfc_cache, rcu);
- kmem_cache_free(mrt_cachep, c);
- }
- static inline void ipmr_cache_free(struct mfc_cache *c)
- {
- call_rcu(&c->rcu, ipmr_cache_free_rcu);
- }
- /* Destroy an unresolved cache entry, killing queued skbs
- * and reporting error to netlink readers.
- */
- static void ipmr_destroy_unres(struct mr_table *mrt, struct mfc_cache *c)
- {
- struct net *net = read_pnet(&mrt->net);
- struct sk_buff *skb;
- struct nlmsgerr *e;
- atomic_dec(&mrt->cache_resolve_queue_len);
- while ((skb = skb_dequeue(&c->mfc_un.unres.unresolved))) {
- if (ip_hdr(skb)->version == 0) {
- struct nlmsghdr *nlh = (struct nlmsghdr *)skb_pull(skb, sizeof(struct iphdr));
- nlh->nlmsg_type = NLMSG_ERROR;
- nlh->nlmsg_len = NLMSG_LENGTH(sizeof(struct nlmsgerr));
- skb_trim(skb, nlh->nlmsg_len);
- e = NLMSG_DATA(nlh);
- e->error = -ETIMEDOUT;
- memset(&e->msg, 0, sizeof(e->msg));
- rtnl_unicast(skb, net, NETLINK_CB(skb).pid);
- } else {
- kfree_skb(skb);
- }
- }
- ipmr_cache_free(c);
- }
- /* Timer process for the unresolved queue. */
- static void ipmr_expire_process(unsigned long arg)
- {
- struct mr_table *mrt = (struct mr_table *)arg;
- unsigned long now;
- unsigned long expires;
- struct mfc_cache *c, *next;
- if (!spin_trylock(&mfc_unres_lock)) {
- mod_timer(&mrt->ipmr_expire_timer, jiffies+HZ/10);
- return;
- }
- if (list_empty(&mrt->mfc_unres_queue))
- goto out;
- now = jiffies;
- expires = 10*HZ;
- list_for_each_entry_safe(c, next, &mrt->mfc_unres_queue, list) {
- if (time_after(c->mfc_un.unres.expires, now)) {
- unsigned long interval = c->mfc_un.unres.expires - now;
- if (interval < expires)
- expires = interval;
- continue;
- }
- list_del(&c->list);
- ipmr_destroy_unres(mrt, c);
- }
- if (!list_empty(&mrt->mfc_unres_queue))
- mod_timer(&mrt->ipmr_expire_timer, jiffies + expires);
- out:
- spin_unlock(&mfc_unres_lock);
- }
- /* Fill oifs list. It is called under write locked mrt_lock. */
- static void ipmr_update_thresholds(struct mr_table *mrt, struct mfc_cache *cache,
- unsigned char *ttls)
- {
- int vifi;
- cache->mfc_un.res.minvif = MAXVIFS;
- cache->mfc_un.res.maxvif = 0;
- memset(cache->mfc_un.res.ttls, 255, MAXVIFS);
- for (vifi = 0; vifi < mrt->maxvif; vifi++) {
- if (VIF_EXISTS(mrt, vifi) &&
- ttls[vifi] && ttls[vifi] < 255) {
- cache->mfc_un.res.ttls[vifi] = ttls[vifi];
- if (cache->mfc_un.res.minvif > vifi)
- cache->mfc_un.res.minvif = vifi;
- if (cache->mfc_un.res.maxvif <= vifi)
- cache->mfc_un.res.maxvif = vifi + 1;
- }
- }
- }
- static int vif_add(struct net *net, struct mr_table *mrt,
- struct vifctl *vifc, int mrtsock)
- {
- int vifi = vifc->vifc_vifi;
- struct vif_device *v = &mrt->vif_table[vifi];
- struct net_device *dev;
- struct in_device *in_dev;
- int err;
- /* Is vif busy ? */
- if (VIF_EXISTS(mrt, vifi))
- return -EADDRINUSE;
- switch (vifc->vifc_flags) {
- #ifdef CONFIG_IP_PIMSM
- case VIFF_REGISTER:
- /*
- * Special Purpose VIF in PIM
- * All the packets will be sent to the daemon
- */
- if (mrt->mroute_reg_vif_num >= 0)
- return -EADDRINUSE;
- dev = ipmr_reg_vif(net, mrt);
- if (!dev)
- return -ENOBUFS;
- err = dev_set_allmulti(dev, 1);
- if (err) {
- unregister_netdevice(dev);
- dev_put(dev);
- return err;
- }
- break;
- #endif
- case VIFF_TUNNEL:
- dev = ipmr_new_tunnel(net, vifc);
- if (!dev)
- return -ENOBUFS;
- err = dev_set_allmulti(dev, 1);
- if (err) {
- ipmr_del_tunnel(dev, vifc);
- dev_put(dev);
- return err;
- }
- break;
- case VIFF_USE_IFINDEX:
- case 0:
- if (vifc->vifc_flags == VIFF_USE_IFINDEX) {
- dev = dev_get_by_index(net, vifc->vifc_lcl_ifindex);
- if (dev && __in_dev_get_rtnl(dev) == NULL) {
- dev_put(dev);
- return -EADDRNOTAVAIL;
- }
- } else {
- dev = ip_dev_find(net, vifc->vifc_lcl_addr.s_addr);
- }
- if (!dev)
- return -EADDRNOTAVAIL;
- err = dev_set_allmulti(dev, 1);
- if (err) {
- dev_put(dev);
- return err;
- }
- break;
- default:
- return -EINVAL;
- }
- in_dev = __in_dev_get_rtnl(dev);
- if (!in_dev) {
- dev_put(dev);
- return -EADDRNOTAVAIL;
- }
- IPV4_DEVCONF(in_dev->cnf, MC_FORWARDING)++;
- ip_rt_multicast_event(in_dev);
- /* Fill in the VIF structures */
- v->rate_limit = vifc->vifc_rate_limit;
- v->local = vifc->vifc_lcl_addr.s_addr;
- v->remote = vifc->vifc_rmt_addr.s_addr;
- v->flags = vifc->vifc_flags;
- if (!mrtsock)
- v->flags |= VIFF_STATIC;
- v->threshold = vifc->vifc_threshold;
- v->bytes_in = 0;
- v->bytes_out = 0;
- v->pkt_in = 0;
- v->pkt_out = 0;
- v->link = dev->ifindex;
- if (v->flags & (VIFF_TUNNEL | VIFF_REGISTER))
- v->link = dev->iflink;
- /* And finish update writing critical data */
- write_lock_bh(&mrt_lock);
- v->dev = dev;
- #ifdef CONFIG_IP_PIMSM
- if (v->flags & VIFF_REGISTER)
- mrt->mroute_reg_vif_num = vifi;
- #endif
- if (vifi+1 > mrt->maxvif)
- mrt->maxvif = vifi+1;
- write_unlock_bh(&mrt_lock);
- return 0;
- }
- /* called with rcu_read_lock() */
- static struct mfc_cache *ipmr_cache_find(struct mr_table *mrt,
- __be32 origin,
- __be32 mcastgrp)
- {
- int line = MFC_HASH(mcastgrp, origin);
- struct mfc_cache *c;
- list_for_each_entry_rcu(c, &mrt->mfc_cache_array[line], list) {
- if (c->mfc_origin == origin && c->mfc_mcastgrp == mcastgrp)
- return c;
- }
- return NULL;
- }
- /*
- * Allocate a multicast cache entry
- */
- static struct mfc_cache *ipmr_cache_alloc(void)
- {
- struct mfc_cache *c = kmem_cache_zalloc(mrt_cachep, GFP_KERNEL);
- if (c)
- c->mfc_un.res.minvif = MAXVIFS;
- return c;
- }
- static struct mfc_cache *ipmr_cache_alloc_unres(void)
- {
- struct mfc_cache *c = kmem_cache_zalloc(mrt_cachep, GFP_ATOMIC);
- if (c) {
- skb_queue_head_init(&c->mfc_un.unres.unresolved);
- c->mfc_un.unres.expires = jiffies + 10*HZ;
- }
- return c;
- }
- /*
- * A cache entry has gone into a resolved state from queued
- */
- static void ipmr_cache_resolve(struct net *net, struct mr_table *mrt,
- struct mfc_cache *uc, struct mfc_cache *c)
- {
- struct sk_buff *skb;
- struct nlmsgerr *e;
- /* Play the pending entries through our router */
- while ((skb = __skb_dequeue(&uc->mfc_un.unres.unresolved))) {
- if (ip_hdr(skb)->version == 0) {
- struct nlmsghdr *nlh = (struct nlmsghdr *)skb_pull(skb, sizeof(struct iphdr));
- if (__ipmr_fill_mroute(mrt, skb, c, NLMSG_DATA(nlh)) > 0) {
- nlh->nlmsg_len = skb_tail_pointer(skb) -
- (u8 *)nlh;
- } else {
- nlh->nlmsg_type = NLMSG_ERROR;
- nlh->nlmsg_len = NLMSG_LENGTH(sizeof(struct nlmsgerr));
- skb_trim(skb, nlh->nlmsg_len);
- e = NLMSG_DATA(nlh);
- e->error = -EMSGSIZE;
- memset(&e->msg, 0, sizeof(e->msg));
- }
- rtnl_unicast(skb, net, NETLINK_CB(skb).pid);
- } else {
- ip_mr_forward(net, mrt, skb, c, 0);
- }
- }
- }
- /*
- * Bounce a cache query up to mrouted. We could use netlink for this but mrouted
- * expects the following bizarre scheme.
- *
- * Called under mrt_lock.
- */
- static int ipmr_cache_report(struct mr_table *mrt,
- struct sk_buff *pkt, vifi_t vifi, int assert)
- {
- struct sk_buff *skb;
- const int ihl = ip_hdrlen(pkt);
- struct igmphdr *igmp;
- struct igmpmsg *msg;
- struct sock *mroute_sk;
- int ret;
- #ifdef CONFIG_IP_PIMSM
- if (assert == IGMPMSG_WHOLEPKT)
- skb = skb_realloc_headroom(pkt, sizeof(struct iphdr));
- else
- #endif
- skb = alloc_skb(128, GFP_ATOMIC);
- if (!skb)
- return -ENOBUFS;
- #ifdef CONFIG_IP_PIMSM
- if (assert == IGMPMSG_WHOLEPKT) {
- /* Ugly, but we have no choice with this interface.
- * Duplicate old header, fix ihl, length etc.
- * And all this only to mangle msg->im_msgtype and
- * to set msg->im_mbz to "mbz" :-)
- */
- skb_push(skb, sizeof(struct iphdr));
- skb_reset_network_header(skb);
- skb_reset_transport_header(skb);
- msg = (struct igmpmsg *)skb_network_header(skb);
- memcpy(msg, skb_network_header(pkt), sizeof(struct iphdr));
- msg->im_msgtype = IGMPMSG_WHOLEPKT;
- msg->im_mbz = 0;
- msg->im_vif = mrt->mroute_reg_vif_num;
- ip_hdr(skb)->ihl = sizeof(struct iphdr) >> 2;
- ip_hdr(skb)->tot_len = htons(ntohs(ip_hdr(pkt)->tot_len) +
- sizeof(struct iphdr));
- } else
- #endif
- {
- /* Copy the IP header */
- skb->network_header = skb->tail;
- skb_put(skb, ihl);
- skb_copy_to_linear_data(skb, pkt->data, ihl);
- ip_hdr(skb)->protocol = 0; /* Flag to the kernel this is a route add */
- msg = (struct igmpmsg *)skb_network_header(skb);
- msg->im_vif = vifi;
- skb_dst_set(skb, dst_clone(skb_dst(pkt)));
- /* Add our header */
- igmp = (struct igmphdr *)skb_put(skb, sizeof(struct igmphdr));
- igmp->type =
- msg->im_msgtype = assert;
- igmp->code = 0;
- ip_hdr(skb)->tot_len = htons(skb->len); /* Fix the length */
- skb->transport_header = skb->network_header;
- }
- rcu_read_lock();
- mroute_sk = rcu_dereference(mrt->mroute_sk);
- if (mroute_sk == NULL) {
- rcu_read_unlock();
- kfree_skb(skb);
- return -EINVAL;
- }
- /* Deliver to mrouted */
- ret = sock_queue_rcv_skb(mroute_sk, skb);
- rcu_read_unlock();
- if (ret < 0) {
- net_warn_ratelimited("mroute: pending queue full, dropping entries\n");
- kfree_skb(skb);
- }
- return ret;
- }
- /*
- * Queue a packet for resolution. It gets locked cache entry!
- */
- static int
- ipmr_cache_unresolved(struct mr_table *mrt, vifi_t vifi, struct sk_buff *skb)
- {
- bool found = false;
- int err;
- struct mfc_cache *c;
- const struct iphdr *iph = ip_hdr(skb);
- spin_lock_bh(&mfc_unres_lock);
- list_for_each_entry(c, &mrt->mfc_unres_queue, list) {
- if (c->mfc_mcastgrp == iph->daddr &&
- c->mfc_origin == iph->saddr) {
- found = true;
- break;
- }
- }
- if (!found) {
- /* Create a new entry if allowable */
- if (atomic_read(&mrt->cache_resolve_queue_len) >= 10 ||
- (c = ipmr_cache_alloc_unres()) == NULL) {
- spin_unlock_bh(&mfc_unres_lock);
- kfree_skb(skb);
- return -ENOBUFS;
- }
- /* Fill in the new cache entry */
- c->mfc_parent = -1;
- c->mfc_origin = iph->saddr;
- c->mfc_mcastgrp = iph->daddr;
- /* Reflect first query at mrouted. */
- err = ipmr_cache_report(mrt, skb, vifi, IGMPMSG_NOCACHE);
- if (err < 0) {
- /* If the report failed throw the cache entry
- out - Brad Parker
- */
- spin_unlock_bh(&mfc_unres_lock);
- ipmr_cache_free(c);
- kfree_skb(skb);
- return err;
- }
- atomic_inc(&mrt->cache_resolve_queue_len);
- list_add(&c->list, &mrt->mfc_unres_queue);
- if (atomic_read(&mrt->cache_resolve_queue_len) == 1)
- mod_timer(&mrt->ipmr_expire_timer, c->mfc_un.unres.expires);
- }
- /* See if we can append the packet */
- if (c->mfc_un.unres.unresolved.qlen > 3) {
- kfree_skb(skb);
- err = -ENOBUFS;
- } else {
- skb_queue_tail(&c->mfc_un.unres.unresolved, skb);
- err = 0;
- }
- spin_unlock_bh(&mfc_unres_lock);
- return err;
- }
- /*
- * MFC cache manipulation by user space mroute daemon
- */
- static int ipmr_mfc_delete(struct mr_table *mrt, struct mfcctl *mfc)
- {
- int line;
- struct mfc_cache *c, *next;
- line = MFC_HASH(mfc->mfcc_mcastgrp.s_addr, mfc->mfcc_origin.s_addr);
- list_for_each_entry_safe(c, next, &mrt->mfc_cache_array[line], list) {
- if (c->mfc_origin == mfc->mfcc_origin.s_addr &&
- c->mfc_mcastgrp == mfc->mfcc_mcastgrp.s_addr) {
- list_del_rcu(&c->list);
- ipmr_cache_free(c);
- return 0;
- }
- }
- return -ENOENT;
- }
- static int ipmr_mfc_add(struct net *net, struct mr_table *mrt,
- struct mfcctl *mfc, int mrtsock)
- {
- bool found = false;
- int line;
- struct mfc_cache *uc, *c;
- if (mfc->mfcc_parent >= MAXVIFS)
- return -ENFILE;
- line = MFC_HASH(mfc->mfcc_mcastgrp.s_addr, mfc->mfcc_origin.s_addr);
- list_for_each_entry(c, &mrt->mfc_cache_array[line], list) {
- if (c->mfc_origin == mfc->mfcc_origin.s_addr &&
- c->mfc_mcastgrp == mfc->mfcc_mcastgrp.s_addr) {
- found = true;
- break;
- }
- }
- if (found) {
- write_lock_bh(&mrt_lock);
- c->mfc_parent = mfc->mfcc_parent;
- ipmr_update_thresholds(mrt, c, mfc->mfcc_ttls);
- if (!mrtsock)
- c->mfc_flags |= MFC_STATIC;
- write_unlock_bh(&mrt_lock);
- return 0;
- }
- if (!ipv4_is_multicast(mfc->mfcc_mcastgrp.s_addr))
- return -EINVAL;
- c = ipmr_cache_alloc();
- if (c == NULL)
- return -ENOMEM;
- c->mfc_origin = mfc->mfcc_origin.s_addr;
- c->mfc_mcastgrp = mfc->mfcc_mcastgrp.s_addr;
- c->mfc_parent = mfc->mfcc_parent;
- ipmr_update_thresholds(mrt, c, mfc->mfcc_ttls);
- if (!mrtsock)
- c->mfc_flags |= MFC_STATIC;
- list_add_rcu(&c->list, &mrt->mfc_cache_array[line]);
- /*
- * Check to see if we resolved a queued list. If so we
- * need to send on the frames and tidy up.
- */
- found = false;
- spin_lock_bh(&mfc_unres_lock);
- list_for_each_entry(uc, &mrt->mfc_unres_queue, list) {
- if (uc->mfc_origin == c->mfc_origin &&
- uc->mfc_mcastgrp == c->mfc_mcastgrp) {
- list_del(&uc->list);
- atomic_dec(&mrt->cache_resolve_queue_len);
- found = true;
- break;
- }
- }
- if (list_empty(&mrt->mfc_unres_queue))
- del_timer(&mrt->ipmr_expire_timer);
- spin_unlock_bh(&mfc_unres_lock);
- if (found) {
- ipmr_cache_resolve(net, mrt, uc, c);
- ipmr_cache_free(uc);
- }
- return 0;
- }
- /*
- * Close the multicast socket, and clear the vif tables etc
- */
- static void mroute_clean_tables(struct mr_table *mrt)
- {
- int i;
- LIST_HEAD(list);
- struct mfc_cache *c, *next;
- /* Shut down all active vif entries */
- for (i = 0; i < mrt->maxvif; i++) {
- if (!(mrt->vif_table[i].flags & VIFF_STATIC))
- vif_delete(mrt, i, 0, &list);
- }
- unregister_netdevice_many(&list);
- /* Wipe the cache */
- for (i = 0; i < MFC_LINES; i++) {
- list_for_each_entry_safe(c, next, &mrt->mfc_cache_array[i], list) {
- if (c->mfc_flags & MFC_STATIC)
- continue;
- list_del_rcu(&c->list);
- ipmr_cache_free(c);
- }
- }
- if (atomic_read(&mrt->cache_resolve_queue_len) != 0) {
- spin_lock_bh(&mfc_unres_lock);
- list_for_each_entry_safe(c, next, &mrt->mfc_unres_queue, list) {
- list_del(&c->list);
- ipmr_destroy_unres(mrt, c);
- }
- spin_unlock_bh(&mfc_unres_lock);
- }
- }
- /* called from ip_ra_control(), before an RCU grace period,
- * we dont need to call synchronize_rcu() here
- */
- static void mrtsock_destruct(struct sock *sk)
- {
- struct net *net = sock_net(sk);
- struct mr_table *mrt;
- rtnl_lock();
- ipmr_for_each_table(mrt, net) {
- if (sk == rtnl_dereference(mrt->mroute_sk)) {
- IPV4_DEVCONF_ALL(net, MC_FORWARDING)--;
- RCU_INIT_POINTER(mrt->mroute_sk, NULL);
- mroute_clean_tables(mrt);
- }
- }
- rtnl_unlock();
- }
- /*
- * Socket options and virtual interface manipulation. The whole
- * virtual interface system is a complete heap, but unfortunately
- * that's how BSD mrouted happens to think. Maybe one day with a proper
- * MOSPF/PIM router set up we can clean this up.
- */
- int ip_mroute_setsockopt(struct sock *sk, int optname, char __user *optval, unsigned int optlen)
- {
- int ret;
- struct vifctl vif;
- struct mfcctl mfc;
- struct net *net = sock_net(sk);
- struct mr_table *mrt;
- if (sk->sk_type != SOCK_RAW ||
- inet_sk(sk)->inet_num != IPPROTO_IGMP)
- return -EOPNOTSUPP;
- mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
- if (mrt == NULL)
- return -ENOENT;
- if (optname != MRT_INIT) {
- if (sk != rcu_access_pointer(mrt->mroute_sk) &&
- !ns_capable(net->user_ns, CAP_NET_ADMIN))
- return -EACCES;
- }
- switch (optname) {
- case MRT_INIT:
- if (optlen != sizeof(int))
- return -EINVAL;
- rtnl_lock();
- if (rtnl_dereference(mrt->mroute_sk)) {
- rtnl_unlock();
- return -EADDRINUSE;
- }
- ret = ip_ra_control(sk, 1, mrtsock_destruct);
- if (ret == 0) {
- rcu_assign_pointer(mrt->mroute_sk, sk);
- IPV4_DEVCONF_ALL(net, MC_FORWARDING)++;
- }
- rtnl_unlock();
- return ret;
- case MRT_DONE:
- if (sk != rcu_access_pointer(mrt->mroute_sk))
- return -EACCES;
- return ip_ra_control(sk, 0, NULL);
- case MRT_ADD_VIF:
- case MRT_DEL_VIF:
- if (optlen != sizeof(vif))
- return -EINVAL;
- if (copy_from_user(&vif, optval, sizeof(vif)))
- return -EFAULT;
- if (vif.vifc_vifi >= MAXVIFS)
- return -ENFILE;
- rtnl_lock();
- if (optname == MRT_ADD_VIF) {
- ret = vif_add(net, mrt, &vif,
- sk == rtnl_dereference(mrt->mroute_sk));
- } else {
- ret = vif_delete(mrt, vif.vifc_vifi, 0, NULL);
- }
- rtnl_unlock();
- return ret;
- /*
- * Manipulate the forwarding caches. These live
- * in a sort of kernel/user symbiosis.
- */
- case MRT_ADD_MFC:
- case MRT_DEL_MFC:
- if (optlen != sizeof(mfc))
- return -EINVAL;
- if (copy_from_user(&mfc, optval, sizeof(mfc)))
- return -EFAULT;
- rtnl_lock();
- if (optname == MRT_DEL_MFC)
- ret = ipmr_mfc_delete(mrt, &mfc);
- else
- ret = ipmr_mfc_add(net, mrt, &mfc,
- sk == rtnl_dereference(mrt->mroute_sk));
- rtnl_unlock();
- return ret;
- /*
- * Control PIM assert.
- */
- case MRT_ASSERT:
- {
- int v;
- if (optlen != sizeof(v))
- return -EINVAL;
- if (get_user(v, (int __user *)optval))
- return -EFAULT;
- mrt->mroute_do_assert = !!v;
- return 0;
- }
- #ifdef CONFIG_IP_PIMSM
- case MRT_PIM:
- {
- int v;
- if (optlen != sizeof(v))
- return -EINVAL;
- if (get_user(v, (int __user *)optval))
- return -EFAULT;
- v = !!v;
- rtnl_lock();
- ret = 0;
- if (v != mrt->mroute_do_pim) {
- mrt->mroute_do_pim = v;
- mrt->mroute_do_assert = v;
- }
- rtnl_unlock();
- return ret;
- }
- #endif
- #ifdef CONFIG_IP_MROUTE_MULTIPLE_TABLES
- case MRT_TABLE:
- {
- u32 v;
- if (optlen != sizeof(u32))
- return -EINVAL;
- if (get_user(v, (u32 __user *)optval))
- return -EFAULT;
- rtnl_lock();
- ret = 0;
- if (sk == rtnl_dereference(mrt->mroute_sk)) {
- ret = -EBUSY;
- } else {
- if (!ipmr_new_table(net, v))
- ret = -ENOMEM;
- else
- raw_sk(sk)->ipmr_table = v;
- }
- rtnl_unlock();
- return ret;
- }
- #endif
- /*
- * Spurious command, or MRT_VERSION which you cannot
- * set.
- */
- default:
- return -ENOPROTOOPT;
- }
- }
- /*
- * Getsock opt support for the multicast routing system.
- */
- int ip_mroute_getsockopt(struct sock *sk, int optname, char __user *optval, int __user *optlen)
- {
- int olr;
- int val;
- struct net *net = sock_net(sk);
- struct mr_table *mrt;
- if (sk->sk_type != SOCK_RAW ||
- inet_sk(sk)->inet_num != IPPROTO_IGMP)
- return -EOPNOTSUPP;
- mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
- if (mrt == NULL)
- return -ENOENT;
- if (optname != MRT_VERSION &&
- #ifdef CONFIG_IP_PIMSM
- optname != MRT_PIM &&
- #endif
- optname != MRT_ASSERT)
- return -ENOPROTOOPT;
- if (get_user(olr, optlen))
- return -EFAULT;
- olr = min_t(unsigned int, olr, sizeof(int));
- if (olr < 0)
- return -EINVAL;
- if (put_user(olr, optlen))
- return -EFAULT;
- if (optname == MRT_VERSION)
- val = 0x0305;
- #ifdef CONFIG_IP_PIMSM
- else if (optname == MRT_PIM)
- val = mrt->mroute_do_pim;
- #endif
- else
- val = mrt->mroute_do_assert;
- if (copy_to_user(optval, &val, olr))
- return -EFAULT;
- return 0;
- }
- /*
- * The IP multicast ioctl support routines.
- */
- int ipmr_ioctl(struct sock *sk, int cmd, void __user *arg)
- {
- struct sioc_sg_req sr;
- struct sioc_vif_req vr;
- struct vif_device *vif;
- struct mfc_cache *c;
- struct net *net = sock_net(sk);
- struct mr_table *mrt;
- mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
- if (mrt == NULL)
- return -ENOENT;
- switch (cmd) {
- case SIOCGETVIFCNT:
- if (copy_from_user(&vr, arg, sizeof(vr)))
- return -EFAULT;
- if (vr.vifi >= mrt->maxvif)
- return -EINVAL;
- read_lock(&mrt_lock);
- vif = &mrt->vif_table[vr.vifi];
- if (VIF_EXISTS(mrt, vr.vifi)) {
- vr.icount = vif->pkt_in;
- vr.ocount = vif->pkt_out;
- vr.ibytes = vif->bytes_in;
- vr.obytes = vif->bytes_out;
- read_unlock(&mrt_lock);
- if (copy_to_user(arg, &vr, sizeof(vr)))
- return -EFAULT;
- return 0;
- }
- read_unlock(&mrt_lock);
- return -EADDRNOTAVAIL;
- case SIOCGETSGCNT:
- if (copy_from_user(&sr, arg, sizeof(sr)))
- return -EFAULT;
- rcu_read_lock();
- c = ipmr_cache_find(mrt, sr.src.s_addr, sr.grp.s_addr);
- if (c) {
- sr.pktcnt = c->mfc_un.res.pkt;
- sr.bytecnt = c->mfc_un.res.bytes;
- sr.wrong_if = c->mfc_un.res.wrong_if;
- rcu_read_unlock();
- if (copy_to_user(arg, &sr, sizeof(sr)))
- return -EFAULT;
- return 0;
- }
- rcu_read_unlock();
- return -EADDRNOTAVAIL;
- default:
- return -ENOIOCTLCMD;
- }
- }
- #ifdef CONFIG_COMPAT
- struct compat_sioc_sg_req {
- struct in_addr src;
- struct in_addr grp;
- compat_ulong_t pktcnt;
- compat_ulong_t bytecnt;
- compat_ulong_t wrong_if;
- };
- struct compat_sioc_vif_req {
- vifi_t vifi; /* Which iface */
- compat_ulong_t icount;
- compat_ulong_t ocount;
- compat_ulong_t ibytes;
- compat_ulong_t obytes;
- };
- int ipmr_compat_ioctl(struct sock *sk, unsigned int cmd, void __user *arg)
- {
- struct compat_sioc_sg_req sr;
- struct compat_sioc_vif_req vr;
- struct vif_device *vif;
- struct mfc_cache *c;
- struct net *net = sock_net(sk);
- struct mr_table *mrt;
- mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
- if (mrt == NULL)
- return -ENOENT;
- switch (cmd) {
- case SIOCGETVIFCNT:
- if (copy_from_user(&vr, arg, sizeof(vr)))
- return -EFAULT;
- if (vr.vifi >= mrt->maxvif)
- return -EINVAL;
- read_lock(&mrt_lock);
- vif = &mrt->vif_table[vr.vifi];
- if (VIF_EXISTS(mrt, vr.vifi)) {
- vr.icount = vif->pkt_in;
- vr.ocount = vif->pkt_out;
- vr.ibytes = vif->bytes_in;
- vr.obytes = vif->bytes_out;
- read_unlock(&mrt_lock);
- if (copy_to_user(arg, &vr, sizeof(vr)))
- return -EFAULT;
- return 0;
- }
- read_unlock(&mrt_lock);
- return -EADDRNOTAVAIL;
- case SIOCGETSGCNT:
- if (copy_from_user(&sr, arg, sizeof(sr)))
- return -EFAULT;
- rcu_read_lock();
- c = ipmr_cache_find(mrt, sr.src.s_addr, sr.grp.s_addr);
- if (c) {
- sr.pktcnt = c->mfc_un.res.pkt;
- sr.bytecnt = c->mfc_un.res.bytes;
- sr.wrong_if = c->mfc_un.res.wrong_if;
- rcu_read_unlock();
- if (copy_to_user(arg, &sr, sizeof(sr)))
- return -EFAULT;
- return 0;
- }
- rcu_read_unlock();
- return -EADDRNOTAVAIL;
- default:
- return -ENOIOCTLCMD;
- }
- }
- #endif
- static int ipmr_device_event(struct notifier_block *this, unsigned long event, void *ptr)
- {
- struct net_device *dev = ptr;
- struct net *net = dev_net(dev);
- struct mr_table *mrt;
- struct vif_device *v;
- int ct;
- if (event != NETDEV_UNREGISTER)
- return NOTIFY_DONE;
- ipmr_for_each_table(mrt, net) {
- v = &mrt->vif_table[0];
- for (ct = 0; ct < mrt->maxvif; ct++, v++) {
- if (v->dev == dev)
- vif_delete(mrt, ct, 1, NULL);
- }
- }
- return NOTIFY_DONE;
- }
- static struct notifier_block ip_mr_notifier = {
- .notifier_call = ipmr_device_event,
- };
- /*
- * Encapsulate a packet by attaching a valid IPIP header to it.
- * This avoids tunnel drivers and other mess and gives us the speed so
- * important for multicast video.
- */
- static void ip_encap(struct net *net, struct sk_buff *skb,
- __be32 saddr, __be32 daddr)
- {
- struct iphdr *iph;
- const struct iphdr *old_iph = ip_hdr(skb);
- skb_push(skb, sizeof(struct iphdr));
- skb->transport_header = skb->network_header;
- skb_reset_network_header(skb);
- iph = ip_hdr(skb);
- iph->version = 4;
- iph->tos = old_iph->tos;
- iph->ttl = old_iph->ttl;
- iph->frag_off = 0;
- iph->daddr = daddr;
- iph->saddr = saddr;
- iph->protocol = IPPROTO_IPIP;
- iph->ihl = 5;
- iph->tot_len = htons(skb->len);
- ip_select_ident(net, skb, NULL);
- ip_send_check(iph);
- memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
- nf_reset(skb);
- }
- static inline int ipmr_forward_finish(struct sk_buff *skb)
- {
- struct ip_options *opt = &(IPCB(skb)->opt);
- IP_INC_STATS_BH(dev_net(skb_dst(skb)->dev), IPSTATS_MIB_OUTFORWDATAGRAMS);
- if (unlikely(opt->optlen))
- ip_forward_options(skb);
- return dst_output(skb);
- }
- /*
- * Processing handlers for ipmr_forward
- */
- static void ipmr_queue_xmit(struct net *net, struct mr_table *mrt,
- struct sk_buff *skb, struct mfc_cache *c, int vifi)
- {
- const struct iphdr *iph = ip_hdr(skb);
- struct vif_device *vif = &mrt->vif_table[vifi];
- struct net_device *dev;
- struct rtable *rt;
- struct flowi4 fl4;
- int encap = 0;
- if (vif->dev == NULL)
- goto out_free;
- #ifdef CONFIG_IP_PIMSM
- if (vif->flags & VIFF_REGISTER) {
- vif->pkt_out++;
- vif->bytes_out += skb->len;
- vif->dev->stats.tx_bytes += skb->len;
- vif->dev->stats.tx_packets++;
- ipmr_cache_report(mrt, skb, vifi, IGMPMSG_WHOLEPKT);
- goto out_free;
- }
- #endif
- if (vif->flags & VIFF_TUNNEL) {
- rt = ip_route_output_ports(net, &fl4, NULL,
- vif->remote, vif->local,
- 0, 0,
- IPPROTO_IPIP,
- RT_TOS(iph->tos), vif->link);
- if (IS_ERR(rt))
- goto out_free;
- encap = sizeof(struct iphdr);
- } else {
- rt = ip_route_output_ports(net, &fl4, NULL, iph->daddr, 0,
- 0, 0,
- IPPROTO_IPIP,
- RT_TOS(iph->tos), vif->link);
- if (IS_ERR(rt))
- goto out_free;
- }
- dev = rt->dst.dev;
- if (skb->len+encap > dst_mtu(&rt->dst) && (ntohs(iph->frag_off) & IP_DF)) {
- /* Do not fragment multicasts. Alas, IPv4 does not
- * allow to send ICMP, so that packets will disappear
- * to blackhole.
- */
- IP_INC_STATS_BH(dev_net(dev), IPSTATS_MIB_FRAGFAILS);
- ip_rt_put(rt);
- goto out_free;
- }
- encap += LL_RESERVED_SPACE(dev) + rt->dst.header_len;
- if (skb_cow(skb, encap)) {
- ip_rt_put(rt);
- goto out_free;
- }
- vif->pkt_out++;
- vif->bytes_out += skb->len;
- skb_dst_drop(skb);
- skb_dst_set(skb, &rt->dst);
- ip_decrease_ttl(ip_hdr(skb));
- /* FIXME: forward and output firewalls used to be called here.
- * What do we do with netfilter? -- RR
- */
- if (vif->flags & VIFF_TUNNEL) {
- ip_encap(net, skb, vif->local, vif->remote);
- /* FIXME: extra output firewall step used to be here. --RR */
- vif->dev->stats.tx_packets++;
- vif->dev->stats.tx_bytes += skb->len;
- }
- IPCB(skb)->flags |= IPSKB_FORWARDED;
- /*
- * RFC1584 teaches, that DVMRP/PIM router must deliver packets locally
- * not only before forwarding, but after forwarding on all output
- * interfaces. It is clear, if mrouter runs a multicasting
- * program, it should receive packets not depending to what interface
- * program is joined.
- * If we will not make it, the program will have to join on all
- * interfaces. On the other hand, multihoming host (or router, but
- * not mrouter) cannot join to more than one interface - it will
- * result in receiving multiple packets.
- */
- NF_HOOK(NFPROTO_IPV4, NF_INET_FORWARD, skb, skb->dev, dev,
- ipmr_forward_finish);
- return;
- out_free:
- kfree_skb(skb);
- }
- static int ipmr_find_vif(struct mr_table *mrt, struct net_device *dev)
- {
- int ct;
- for (ct = mrt->maxvif-1; ct >= 0; ct--) {
- if (mrt->vif_table[ct].dev == dev)
- break;
- }
- return ct;
- }
- /* "local" means that we should preserve one skb (for local delivery) */
- static int ip_mr_forward(struct net *net, struct mr_table *mrt,
- struct sk_buff *skb, struct mfc_cache *cache,
- int local)
- {
- int psend = -1;
- int vif, ct;
- vif = cache->mfc_parent;
- cache->mfc_un.res.pkt++;
- cache->mfc_un.res.bytes += skb->len;
- /*
- * Wrong interface: drop packet and (maybe) send PIM assert.
- */
- if (mrt->vif_table[vif].dev != skb->dev) {
- int true_vifi;
- if (rt_is_output_route(skb_rtable(skb))) {
- /* It is our own packet, looped back.
- * Very complicated situation...
- *
- * The best workaround until routing daemons will be
- * fixed is not to redistribute packet, if it was
- * send through wrong interface. It means, that
- * multicast applications WILL NOT work for
- * (S,G), which have default multicast route pointing
- * to wrong oif. In any case, it is not a good
- * idea to use multicasting applications on router.
- */
- goto dont_forward;
- }
- cache->mfc_un.res.wrong_if++;
- true_vifi = ipmr_find_vif(mrt, skb->dev);
- if (true_vifi >= 0 && mrt->mroute_do_assert &&
- /* pimsm uses asserts, when switching from RPT to SPT,
- * so that we cannot check that packet arrived on an oif.
- * It is bad, but otherwise we would need to move pretty
- * large chunk of pimd to kernel. Ough... --ANK
- */
- (mrt->mroute_do_pim ||
- cache->mfc_un.res.ttls[true_vifi] < 255) &&
- time_after(jiffies,
- cache->mfc_un.res.last_assert + MFC_ASSERT_THRESH)) {
- cache->mfc_un.res.last_assert = jiffies;
- ipmr_cache_report(mrt, skb, true_vifi, IGMPMSG_WRONGVIF);
- }
- goto dont_forward;
- }
- mrt->vif_table[vif].pkt_in++;
- mrt->vif_table[vif].bytes_in += skb->len;
- /*
- * Forward the frame
- */
- for (ct = cache->mfc_un.res.maxvif - 1;
- ct >= cache->mfc_un.res.minvif; ct--) {
- if (ip_hdr(skb)->ttl > cache->mfc_un.res.ttls[ct]) {
- if (psend != -1) {
- struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
- if (skb2)
- ipmr_queue_xmit(net, mrt, skb2, cache,
- psend);
- }
- psend = ct;
- }
- }
- if (psend != -1) {
- if (local) {
- struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
- if (skb2)
- ipmr_queue_xmit(net, mrt, skb2, cache, psend);
- } else {
- ipmr_queue_xmit(net, mrt, skb, cache, psend);
- return 0;
- }
- }
- dont_forward:
- if (!local)
- kfree_skb(skb);
- return 0;
- }
- static struct mr_table *ipmr_rt_fib_lookup(struct net *net, struct sk_buff *skb)
- {
- struct rtable *rt = skb_rtable(skb);
- struct iphdr *iph = ip_hdr(skb);
- struct flowi4 fl4 = {
- .daddr = iph->daddr,
- .saddr = iph->saddr,
- .flowi4_tos = RT_TOS(iph->tos),
- .flowi4_oif = rt->rt_oif,
- .flowi4_iif = rt->rt_iif,
- .flowi4_mark = rt->rt_mark,
- };
- struct mr_table *mrt;
- int err;
- err = ipmr_fib_lookup(net, &fl4, &mrt);
- if (err)
- return ERR_PTR(err);
- return mrt;
- }
- /*
- * Multicast packets for forwarding arrive here
- * Called with rcu_read_lock();
- */
- int ip_mr_input(struct sk_buff *skb)
- {
- struct mfc_cache *cache;
- struct net *net = dev_net(skb->dev);
- int local = skb_rtable(skb)->rt_flags & RTCF_LOCAL;
- struct mr_table *mrt;
- /* Packet is looped back after forward, it should not be
- * forwarded second time, but still can be delivered locally.
- */
- if (IPCB(skb)->flags & IPSKB_FORWARDED)
- goto dont_forward;
- mrt = ipmr_rt_fib_lookup(net, skb);
- if (IS_ERR(mrt)) {
- kfree_skb(skb);
- return PTR_ERR(mrt);
- }
- if (!local) {
- if (IPCB(skb)->opt.router_alert) {
- if (ip_call_ra_chain(skb))
- return 0;
- } else if (ip_hdr(skb)->protocol == IPPROTO_IGMP) {
- /* IGMPv1 (and broken IGMPv2 implementations sort of
- * Cisco IOS <= 11.2(8)) do not put router alert
- * option to IGMP packets destined to routable
- * groups. It is very bad, because it means
- * that we can forward NO IGMP messages.
- */
- struct sock *mroute_sk;
- mroute_sk = rcu_dereference(mrt->mroute_sk);
- if (mroute_sk) {
- nf_reset(skb);
- raw_rcv(mroute_sk, skb);
- return 0;
- }
- }
- }
- /* already under rcu_read_lock() */
- cache = ipmr_cache_find(mrt, ip_hdr(skb)->saddr, ip_hdr(skb)->daddr);
- /*
- * No usable cache entry
- */
- if (cache == NULL) {
- int vif;
- if (local) {
- struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
- ip_local_deliver(skb);
- if (skb2 == NULL)
- return -ENOBUFS;
- skb = skb2;
- }
- read_lock(&mrt_lock);
- vif = ipmr_find_vif(mrt, skb->dev);
- if (vif >= 0) {
- int err2 = ipmr_cache_unresolved(mrt, vif, skb);
- read_unlock(&mrt_lock);
- return err2;
- }
- read_unlock(&mrt_lock);
- kfree_skb(skb);
- return -ENODEV;
- }
- read_lock(&mrt_lock);
- ip_mr_forward(net, mrt, skb, cache, local);
- read_unlock(&mrt_lock);
- if (local)
- return ip_local_deliver(skb);
- return 0;
- dont_forward:
- if (local)
- return ip_local_deliver(skb);
- kfree_skb(skb);
- return 0;
- }
- #ifdef CONFIG_IP_PIMSM
- /* called with rcu_read_lock() */
- static int __pim_rcv(struct mr_table *mrt, struct sk_buff *skb,
- unsigned int pimlen)
- {
- struct net_device *reg_dev = NULL;
- struct iphdr *encap;
- encap = (struct iphdr *)(skb_transport_header(skb) + pimlen);
- /*
- * Check that:
- * a. packet is really sent to a multicast group
- * b. packet is not a NULL-REGISTER
- * c. packet is not truncated
- */
- if (!ipv4_is_multicast(encap->daddr) ||
- encap->tot_len == 0 ||
- ntohs(encap->tot_len) + pimlen > skb->len)
- return 1;
- read_lock(&mrt_lock);
- if (mrt->mroute_reg_vif_num >= 0)
- reg_dev = mrt->vif_table[mrt->mroute_reg_vif_num].dev;
- read_unlock(&mrt_lock);
- if (reg_dev == NULL)
- return 1;
- skb->mac_header = skb->network_header;
- skb_pull(skb, (u8 *)encap - skb->data);
- skb_reset_network_header(skb);
- skb->protocol = htons(ETH_P_IP);
- skb->ip_summed = CHECKSUM_NONE;
- skb->pkt_type = PACKET_HOST;
- skb_tunnel_rx(skb, reg_dev);
- netif_rx(skb);
- return NET_RX_SUCCESS;
- }
- #endif
- #ifdef CONFIG_IP_PIMSM_V1
- /*
- * Handle IGMP messages of PIMv1
- */
- int pim_rcv_v1(struct sk_buff *skb)
- {
- struct igmphdr *pim;
- struct net *net = dev_net(skb->dev);
- struct mr_table *mrt;
- if (!pskb_may_pull(skb, sizeof(*pim) + sizeof(struct iphdr)))
- goto drop;
- pim = igmp_hdr(skb);
- mrt = ipmr_rt_fib_lookup(net, skb);
- if (IS_ERR(mrt))
- goto drop;
- if (!mrt->mroute_do_pim ||
- pim->group != PIM_V1_VERSION || pim->code != PIM_V1_REGISTER)
- goto drop;
- if (__pim_rcv(mrt, skb, sizeof(*pim))) {
- drop:
- kfree_skb(skb);
- }
- return 0;
- }
- #endif
- #ifdef CONFIG_IP_PIMSM_V2
- static int pim_rcv(struct sk_buff *skb)
- {
- struct pimreghdr *pim;
- struct net *net = dev_net(skb->dev);
- struct mr_table *mrt;
- if (!pskb_may_pull(skb, sizeof(*pim) + sizeof(struct iphdr)))
- goto drop;
- pim = (struct pimreghdr *)skb_transport_header(skb);
- if (pim->type != ((PIM_VERSION << 4) | (PIM_REGISTER)) ||
- (pim->flags & PIM_NULL_REGISTER) ||
- (ip_compute_csum((void *)pim, sizeof(*pim)) != 0 &&
- csum_fold(skb_checksum(skb, 0, skb->len, 0))))
- goto drop;
- mrt = ipmr_rt_fib_lookup(net, skb);
- if (IS_ERR(mrt))
- goto drop;
- if (__pim_rcv(mrt, skb, sizeof(*pim))) {
- drop:
- kfree_skb(skb);
- }
- return 0;
- }
- #endif
- static int __ipmr_fill_mroute(struct mr_table *mrt, struct sk_buff *skb,
- struct mfc_cache *c, struct rtmsg *rtm)
- {
- int ct;
- struct rtnexthop *nhp;
- u8 *b = skb_tail_pointer(skb);
- struct rtattr *mp_head;
- /* If cache is unresolved, don't try to parse IIF and OIF */
- if (c->mfc_parent >= MAXVIFS)
- return -ENOENT;
- if (VIF_EXISTS(mrt, c->mfc_parent))
- RTA_PUT(skb, RTA_IIF, 4, &mrt->vif_table[c->mfc_parent].dev->ifindex);
- mp_head = (struct rtattr *)skb_put(skb, RTA_LENGTH(0));
- for (ct = c->mfc_un.res.minvif; ct < c->mfc_un.res.maxvif; ct++) {
- if (VIF_EXISTS(mrt, ct) && c->mfc_un.res.ttls[ct] < 255) {
- if (skb_tailroom(skb) < RTA_ALIGN(RTA_ALIGN(sizeof(*nhp)) + 4))
- goto rtattr_failure;
- nhp = (struct rtnexthop *)skb_put(skb, RTA_ALIGN(sizeof(*nhp)));
- nhp->rtnh_flags = 0;
- nhp->rtnh_hops = c->mfc_un.res.ttls[ct];
- nhp->rtnh_ifindex = mrt->vif_table[ct].dev->ifindex;
- nhp->rtnh_len = sizeof(*nhp);
- }
- }
- mp_head->rta_type = RTA_MULTIPATH;
- mp_head->rta_len = skb_tail_pointer(skb) - (u8 *)mp_head;
- rtm->rtm_type = RTN_MULTICAST;
- return 1;
- rtattr_failure:
- nlmsg_trim(skb, b);
- return -EMSGSIZE;
- }
- int ipmr_get_route(struct net *net, struct sk_buff *skb,
- __be32 saddr, __be32 daddr,
- struct rtmsg *rtm, int nowait)
- {
- struct mfc_cache *cache;
- struct mr_table *mrt;
- int err;
- mrt = ipmr_get_table(net, RT_TABLE_DEFAULT);
- if (mrt == NULL)
- return -ENOENT;
- rcu_read_lock();
- cache = ipmr_cache_find(mrt, saddr, daddr);
- if (cache == NULL) {
- struct sk_buff *skb2;
- struct iphdr *iph;
- struct net_device *dev;
- int vif = -1;
- if (nowait) {
- rcu_read_unlock();
- return -EAGAIN;
- }
- dev = skb->dev;
- read_lock(&mrt_lock);
- if (dev)
- vif = ipmr_find_vif(mrt, dev);
- if (vif < 0) {
- read_unlock(&mrt_lock);
- rcu_read_unlock();
- return -ENODEV;
- }
- skb2 = skb_clone(skb, GFP_ATOMIC);
- if (!skb2) {
- read_unlock(&mrt_lock);
- rcu_read_unlock();
- return -ENOMEM;
- }
- skb_push(skb2, sizeof(struct iphdr));
- skb_reset_network_header(skb2);
- iph = ip_hdr(skb2);
- iph->ihl = sizeof(struct iphdr) >> 2;
- iph->saddr = saddr;
- iph->daddr = daddr;
- iph->version = 0;
- err = ipmr_cache_unresolved(mrt, vif, skb2);
- read_unlock(&mrt_lock);
- rcu_read_unlock();
- return err;
- }
- read_lock(&mrt_lock);
- if (!nowait && (rtm->rtm_flags & RTM_F_NOTIFY))
- cache->mfc_flags |= MFC_NOTIFY;
- err = __ipmr_fill_mroute(mrt, skb, cache, rtm);
- read_unlock(&mrt_lock);
- rcu_read_unlock();
- return err;
- }
- static int ipmr_fill_mroute(struct mr_table *mrt, struct sk_buff *skb,
- u32 pid, u32 seq, struct mfc_cache *c)
- {
- struct nlmsghdr *nlh;
- struct rtmsg *rtm;
- nlh = nlmsg_put(skb, pid, seq, RTM_NEWROUTE, sizeof(*rtm), NLM_F_MULTI);
- if (nlh == NULL)
- return -EMSGSIZE;
- rtm = nlmsg_data(nlh);
- rtm->rtm_family = RTNL_FAMILY_IPMR;
- rtm->rtm_dst_len = 32;
- rtm->rtm_src_len = 32;
- rtm->rtm_tos = 0;
- rtm->rtm_table = mrt->id;
- NLA_PUT_U32(skb, RTA_TABLE, mrt->id);
- rtm->rtm_type = RTN_MULTICAST;
- rtm->rtm_scope = RT_SCOPE_UNIVERSE;
- rtm->rtm_protocol = RTPROT_UNSPEC;
- rtm->rtm_flags = 0;
- NLA_PUT_BE32(skb, RTA_SRC, c->mfc_origin);
- NLA_PUT_BE32(skb, RTA_DST, c->mfc_mcastgrp);
- if (__ipmr_fill_mroute(mrt, skb, c, rtm) < 0)
- goto nla_put_failure;
- return nlmsg_end(skb, nlh);
- nla_put_failure:
- nlmsg_cancel(skb, nlh);
- return -EMSGSIZE;
- }
- static int ipmr_rtm_dumproute(struct sk_buff *skb, struct netlink_callback *cb)
- {
- struct net *net = sock_net(skb->sk);
- struct mr_table *mrt;
- struct mfc_cache *mfc;
- unsigned int t = 0, s_t;
- unsigned int h = 0, s_h;
- unsigned int e = 0, s_e;
- s_t = cb->args[0];
- s_h = cb->args[1];
- s_e = cb->args[2];
- rcu_read_lock();
- ipmr_for_each_table(mrt, net) {
- if (t < s_t)
- goto next_table;
- if (t > s_t)
- s_h = 0;
- for (h = s_h; h < MFC_LINES; h++) {
- list_for_each_entry_rcu(mfc, &mrt->mfc_cache_array[h], list) {
- if (e < s_e)
- goto next_entry;
- if (ipmr_fill_mroute(mrt, skb,
- NETLINK_CB(cb->skb).pid,
- cb->nlh->nlmsg_seq,
- mfc) < 0)
- goto done;
- next_entry:
- e++;
- }
- e = s_e = 0;
- }
- s_h = 0;
- next_table:
- t++;
- }
- done:
- rcu_read_unlock();
- cb->args[2] = e;
- cb->args[1] = h;
- cb->args[0] = t;
- return skb->len;
- }
- #ifdef CONFIG_PROC_FS
- /*
- * The /proc interfaces to multicast routing :
- * /proc/net/ip_mr_cache & /proc/net/ip_mr_vif
- */
- struct ipmr_vif_iter {
- struct seq_net_private p;
- struct mr_table *mrt;
- int ct;
- };
- static struct vif_device *ipmr_vif_seq_idx(struct net *net,
- struct ipmr_vif_iter *iter,
- loff_t pos)
- {
- struct mr_table *mrt = iter->mrt;
- for (iter->ct = 0; iter->ct < mrt->maxvif; ++iter->ct) {
- if (!VIF_EXISTS(mrt, iter->ct))
- continue;
- if (pos-- == 0)
- return &mrt->vif_table[iter->ct];
- }
- return NULL;
- }
- static void *ipmr_vif_seq_start(struct seq_file *seq, loff_t *pos)
- __acquires(mrt_lock)
- {
- struct ipmr_vif_iter *iter = seq->private;
- struct net *net = seq_file_net(seq);
- struct mr_table *mrt;
- mrt = ipmr_get_table(net, RT_TABLE_DEFAULT);
- if (mrt == NULL)
- return ERR_PTR(-ENOENT);
- iter->mrt = mrt;
- read_lock(&mrt_lock);
- return *pos ? ipmr_vif_seq_idx(net, seq->private, *pos - 1)
- : SEQ_START_TOKEN;
- }
- static void *ipmr_vif_seq_next(struct seq_file *seq, void *v, loff_t *pos)
- {
- struct ipmr_vif_iter *iter = seq->private;
- struct net *net = seq_file_net(seq);
- struct mr_table *mrt = iter->mrt;
- ++*pos;
- if (v == SEQ_START_TOKEN)
- return ipmr_vif_seq_idx(net, iter, 0);
- while (++iter->ct < mrt->maxvif) {
- if (!VIF_EXISTS(mrt, iter->ct))
- continue;
- return &mrt->vif_table[iter->ct];
- }
- return NULL;
- }
- static void ipmr_vif_seq_stop(struct seq_file *seq, void *v)
- __releases(mrt_lock)
- {
- read_unlock(&mrt_lock);
- }
- static int ipmr_vif_seq_show(struct seq_file *seq, void *v)
- {
- struct ipmr_vif_iter *iter = seq->private;
- struct mr_table *mrt = iter->mrt;
- if (v == SEQ_START_TOKEN) {
- seq_puts(seq,
- "Interface BytesIn PktsIn BytesOut PktsOut Flags Local Remote\n");
- } else {
- const struct vif_device *vif = v;
- const char *name = vif->dev ? vif->dev->name : "none";
- seq_printf(seq,
- "%2Zd %-10s %8ld %7ld %8ld %7ld %05X %08X %08X\n",
- vif - mrt->vif_table,
- name, vif->bytes_in, vif->pkt_in,
- vif->bytes_out, vif->pkt_out,
- vif->flags, vif->local, vif->remote);
- }
- return 0;
- }
- static const struct seq_operations ipmr_vif_seq_ops = {
- .start = ipmr_vif_seq_start,
- .next = ipmr_vif_seq_next,
- .stop = ipmr_vif_seq_stop,
- .show = ipmr_vif_seq_show,
- };
- static int ipmr_vif_open(struct inode *inode, struct file *file)
- {
- return seq_open_net(inode, file, &ipmr_vif_seq_ops,
- sizeof(struct ipmr_vif_iter));
- }
- static const struct file_operations ipmr_vif_fops = {
- .owner = THIS_MODULE,
- .open = ipmr_vif_open,
- .read = seq_read,
- .llseek = seq_lseek,
- .release = seq_release_net,
- };
- struct ipmr_mfc_iter {
- struct seq_net_private p;
- struct mr_table *mrt;
- struct list_head *cache;
- int ct;
- };
- static struct mfc_cache *ipmr_mfc_seq_idx(struct net *net,
- struct ipmr_mfc_iter *it, loff_t pos)
- {
- struct mr_table *mrt = it->mrt;
- struct mfc_cache *mfc;
- rcu_read_lock();
- for (it->ct = 0; it->ct < MFC_LINES; it->ct++) {
- it->cache = &mrt->mfc_cache_array[it->ct];
- list_for_each_entry_rcu(mfc, it->cache, list)
- if (pos-- == 0)
- return mfc;
- }
- rcu_read_unlock();
- spin_lock_bh(&mfc_unres_lock);
- it->cache = &mrt->mfc_unres_queue;
- list_for_each_entry(mfc, it->cache, list)
- if (pos-- == 0)
- return mfc;
- spin_unlock_bh(&mfc_unres_lock);
- it->cache = NULL;
- return NULL;
- }
- static void *ipmr_mfc_seq_start(struct seq_file *seq, loff_t *pos)
- {
- struct ipmr_mfc_iter *it = seq->private;
- struct net *net = seq_file_net(seq);
- struct mr_table *mrt;
- mrt = ipmr_get_table(net, RT_TABLE_DEFAULT);
- if (mrt == NULL)
- return ERR_PTR(-ENOENT);
- it->mrt = mrt;
- it->cache = NULL;
- it->ct = 0;
- return *pos ? ipmr_mfc_seq_idx(net, seq->private, *pos - 1)
- : SEQ_START_TOKEN;
- }
- static void *ipmr_mfc_seq_next(struct seq_file *seq, void *v, loff_t *pos)
- {
- struct mfc_cache *mfc = v;
- struct ipmr_mfc_iter *it = seq->private;
- struct net *net = seq_file_net(seq);
- struct mr_table *mrt = it->mrt;
- ++*pos;
- if (v == SEQ_START_TOKEN)
- return ipmr_mfc_seq_idx(net, seq->private, 0);
- if (mfc->list.next != it->cache)
- return list_entry(mfc->list.next, struct mfc_cache, list);
- if (it->cache == &mrt->mfc_unres_queue)
- goto end_of_list;
- BUG_ON(it->cache != &mrt->mfc_cache_array[it->ct]);
- while (++it->ct < MFC_LINES) {
- it->cache = &mrt->mfc_cache_array[it->ct];
- if (list_empty(it->cache))
- continue;
- return list_first_entry(it->cache, struct mfc_cache, list);
- }
- /* exhausted cache_array, show unresolved */
- rcu_read_unlock();
- it->cache = &mrt->mfc_unres_queue;
- it->ct = 0;
- spin_lock_bh(&mfc_unres_lock);
- if (!list_empty(it->cache))
- return list_first_entry(it->cache, struct mfc_cache, list);
- end_of_list:
- spin_unlock_bh(&mfc_unres_lock);
- it->cache = NULL;
- return NULL;
- }
- static void ipmr_mfc_seq_stop(struct seq_file *seq, void *v)
- {
- struct ipmr_mfc_iter *it = seq->private;
- struct mr_table *mrt = it->mrt;
- if (it->cache == &mrt->mfc_unres_queue)
- spin_unlock_bh(&mfc_unres_lock);
- else if (it->cache == &mrt->mfc_cache_array[it->ct])
- rcu_read_unlock();
- }
- static int ipmr_mfc_seq_show(struct seq_file *seq, void *v)
- {
- int n;
- if (v == SEQ_START_TOKEN) {
- seq_puts(seq,
- "Group Origin Iif Pkts Bytes Wrong Oifs\n");
- } else {
- const struct mfc_cache *mfc = v;
- const struct ipmr_mfc_iter *it = seq->private;
- const struct mr_table *mrt = it->mrt;
- seq_printf(seq, "%08X %08X %-3hd",
- (__force u32) mfc->mfc_mcastgrp,
- (__force u32) mfc->mfc_origin,
- mfc->mfc_parent);
- if (it->cache != &mrt->mfc_unres_queue) {
- seq_printf(seq, " %8lu %8lu %8lu",
- mfc->mfc_un.res.pkt,
- mfc->mfc_un.res.bytes,
- mfc->mfc_un.res.wrong_if);
- for (n = mfc->mfc_un.res.minvif;
- n < mfc->mfc_un.res.maxvif; n++) {
- if (VIF_EXISTS(mrt, n) &&
- mfc->mfc_un.res.ttls[n] < 255)
- seq_printf(seq,
- " %2d:%-3d",
- n, mfc->mfc_un.res.ttls[n]);
- }
- } else {
- /* unresolved mfc_caches don't contain
- * pkt, bytes and wrong_if values
- */
- seq_printf(seq, " %8lu %8lu %8lu", 0ul, 0ul, 0ul);
- }
- seq_putc(seq, '\n');
- }
- return 0;
- }
- static const struct seq_operations ipmr_mfc_seq_ops = {
- .start = ipmr_mfc_seq_start,
- .next = ipmr_mfc_seq_next,
- .stop = ipmr_mfc_seq_stop,
- .show = ipmr_mfc_seq_show,
- };
- static int ipmr_mfc_open(struct inode *inode, struct file *file)
- {
- return seq_open_net(inode, file, &ipmr_mfc_seq_ops,
- sizeof(struct ipmr_mfc_iter));
- }
- static const struct file_operations ipmr_mfc_fops = {
- .owner = THIS_MODULE,
- .open = ipmr_mfc_open,
- .read = seq_read,
- .llseek = seq_lseek,
- .release = seq_release_net,
- };
- #endif
- #ifdef CONFIG_IP_PIMSM_V2
- static const struct net_protocol pim_protocol = {
- .handler = pim_rcv,
- .netns_ok = 1,
- };
- #endif
- /*
- * Setup for IP multicast routing
- */
- static int __net_init ipmr_net_init(struct net *net)
- {
- int err;
- err = ipmr_rules_init(net);
- if (err < 0)
- goto fail;
- #ifdef CONFIG_PROC_FS
- err = -ENOMEM;
- if (!proc_net_fops_create(net, "ip_mr_vif", 0, &ipmr_vif_fops))
- goto proc_vif_fail;
- if (!proc_net_fops_create(net, "ip_mr_cache", 0, &ipmr_mfc_fops))
- goto proc_cache_fail;
- #endif
- return 0;
- #ifdef CONFIG_PROC_FS
- proc_cache_fail:
- proc_net_remove(net, "ip_mr_vif");
- proc_vif_fail:
- ipmr_rules_exit(net);
- #endif
- fail:
- return err;
- }
- static void __net_exit ipmr_net_exit(struct net *net)
- {
- #ifdef CONFIG_PROC_FS
- proc_net_remove(net, "ip_mr_cache");
- proc_net_remove(net, "ip_mr_vif");
- #endif
- ipmr_rules_exit(net);
- }
- static struct pernet_operations ipmr_net_ops = {
- .init = ipmr_net_init,
- .exit = ipmr_net_exit,
- };
- int __init ip_mr_init(void)
- {
- int err;
- mrt_cachep = kmem_cache_create("ip_mrt_cache",
- sizeof(struct mfc_cache),
- 0, SLAB_HWCACHE_ALIGN | SLAB_PANIC,
- NULL);
- if (!mrt_cachep)
- return -ENOMEM;
- err = register_pernet_subsys(&ipmr_net_ops);
- if (err)
- goto reg_pernet_fail;
- err = register_netdevice_notifier(&ip_mr_notifier);
- if (err)
- goto reg_notif_fail;
- #ifdef CONFIG_IP_PIMSM_V2
- if (inet_add_protocol(&pim_protocol, IPPROTO_PIM) < 0) {
- pr_err("%s: can't add PIM protocol\n", __func__);
- err = -EAGAIN;
- goto add_proto_fail;
- }
- #endif
- rtnl_register(RTNL_FAMILY_IPMR, RTM_GETROUTE,
- NULL, ipmr_rtm_dumproute, NULL);
- return 0;
- #ifdef CONFIG_IP_PIMSM_V2
- add_proto_fail:
- unregister_netdevice_notifier(&ip_mr_notifier);
- #endif
- reg_notif_fail:
- unregister_pernet_subsys(&ipmr_net_ops);
- reg_pernet_fail:
- kmem_cache_destroy(mrt_cachep);
- return err;
- }
|