123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679 |
- /*
- * Multicast support for IPv6
- * Linux INET6 implementation
- *
- * Authors:
- * Pedro Roque <roque@di.fc.ul.pt>
- *
- * Based on linux/ipv4/igmp.c and linux/ipv4/ip_sockglue.c
- *
- * 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.
- */
- /* Changes:
- *
- * yoshfuji : fix format of router-alert option
- * YOSHIFUJI Hideaki @USAGI:
- * Fixed source address for MLD message based on
- * <draft-ietf-magma-mld-source-05.txt>.
- * YOSHIFUJI Hideaki @USAGI:
- * - Ignore Queries for invalid addresses.
- * - MLD for link-local addresses.
- * David L Stevens <dlstevens@us.ibm.com>:
- * - MLDv2 support
- */
- #include <linux/module.h>
- #include <linux/errno.h>
- #include <linux/types.h>
- #include <linux/string.h>
- #include <linux/socket.h>
- #include <linux/sockios.h>
- #include <linux/jiffies.h>
- #include <linux/times.h>
- #include <linux/net.h>
- #include <linux/in.h>
- #include <linux/in6.h>
- #include <linux/netdevice.h>
- #include <linux/if_arp.h>
- #include <linux/route.h>
- #include <linux/init.h>
- #include <linux/proc_fs.h>
- #include <linux/seq_file.h>
- #include <linux/slab.h>
- #include <net/mld.h>
- #include <linux/netfilter.h>
- #include <linux/netfilter_ipv6.h>
- #include <net/net_namespace.h>
- #include <net/sock.h>
- #include <net/snmp.h>
- #include <net/ipv6.h>
- #include <net/protocol.h>
- #include <net/if_inet6.h>
- #include <net/ndisc.h>
- #include <net/addrconf.h>
- #include <net/ip6_route.h>
- #include <net/inet_common.h>
- #include <net/ip6_checksum.h>
- /* Set to 3 to get tracing... */
- #define MCAST_DEBUG 2
- #if MCAST_DEBUG >= 3
- #define MDBG(x) printk x
- #else
- #define MDBG(x)
- #endif
- /* Ensure that we have struct in6_addr aligned on 32bit word. */
- static void *__mld2_query_bugs[] __attribute__((__unused__)) = {
- BUILD_BUG_ON_NULL(offsetof(struct mld2_query, mld2q_srcs) % 4),
- BUILD_BUG_ON_NULL(offsetof(struct mld2_report, mld2r_grec) % 4),
- BUILD_BUG_ON_NULL(offsetof(struct mld2_grec, grec_mca) % 4)
- };
- static struct in6_addr mld2_all_mcr = MLD2_ALL_MCR_INIT;
- /* Big mc list lock for all the sockets */
- static DEFINE_SPINLOCK(ipv6_sk_mc_lock);
- static void igmp6_join_group(struct ifmcaddr6 *ma);
- static void igmp6_leave_group(struct ifmcaddr6 *ma);
- static void igmp6_timer_handler(unsigned long data);
- static void mld_gq_timer_expire(unsigned long data);
- static void mld_ifc_timer_expire(unsigned long data);
- static void mld_ifc_event(struct inet6_dev *idev);
- static void mld_add_delrec(struct inet6_dev *idev, struct ifmcaddr6 *pmc);
- static void mld_del_delrec(struct inet6_dev *idev, const struct in6_addr *addr);
- static void mld_clear_delrec(struct inet6_dev *idev);
- static int sf_setstate(struct ifmcaddr6 *pmc);
- static void sf_markstate(struct ifmcaddr6 *pmc);
- static void ip6_mc_clear_src(struct ifmcaddr6 *pmc);
- static int ip6_mc_del_src(struct inet6_dev *idev, const struct in6_addr *pmca,
- int sfmode, int sfcount, const struct in6_addr *psfsrc,
- int delta);
- static int ip6_mc_add_src(struct inet6_dev *idev, const struct in6_addr *pmca,
- int sfmode, int sfcount, const struct in6_addr *psfsrc,
- int delta);
- static int ip6_mc_leave_src(struct sock *sk, struct ipv6_mc_socklist *iml,
- struct inet6_dev *idev);
- #define IGMP6_UNSOLICITED_IVAL (10*HZ)
- #define MLD_QRV_DEFAULT 2
- #define MLD_V1_SEEN(idev) (dev_net((idev)->dev)->ipv6.devconf_all->force_mld_version == 1 || \
- (idev)->cnf.force_mld_version == 1 || \
- ((idev)->mc_v1_seen && \
- time_before(jiffies, (idev)->mc_v1_seen)))
- #define IPV6_MLD_MAX_MSF 64
- int sysctl_mld_max_msf __read_mostly = IPV6_MLD_MAX_MSF;
- /*
- * socket join on multicast group
- */
- #define for_each_pmc_rcu(np, pmc) \
- for (pmc = rcu_dereference(np->ipv6_mc_list); \
- pmc != NULL; \
- pmc = rcu_dereference(pmc->next))
- int ipv6_sock_mc_join(struct sock *sk, int ifindex, const struct in6_addr *addr)
- {
- struct net_device *dev = NULL;
- struct ipv6_mc_socklist *mc_lst;
- struct ipv6_pinfo *np = inet6_sk(sk);
- struct net *net = sock_net(sk);
- int err;
- if (!ipv6_addr_is_multicast(addr))
- return -EINVAL;
- rcu_read_lock();
- for_each_pmc_rcu(np, mc_lst) {
- if ((ifindex == 0 || mc_lst->ifindex == ifindex) &&
- ipv6_addr_equal(&mc_lst->addr, addr)) {
- rcu_read_unlock();
- return -EADDRINUSE;
- }
- }
- rcu_read_unlock();
- mc_lst = sock_kmalloc(sk, sizeof(struct ipv6_mc_socklist), GFP_KERNEL);
- if (mc_lst == NULL)
- return -ENOMEM;
- mc_lst->next = NULL;
- ipv6_addr_copy(&mc_lst->addr, addr);
- rcu_read_lock();
- if (ifindex == 0) {
- struct rt6_info *rt;
- rt = rt6_lookup(net, addr, NULL, 0, 0);
- if (rt) {
- dev = rt->rt6i_dev;
- dst_release(&rt->dst);
- }
- } else
- dev = dev_get_by_index_rcu(net, ifindex);
- if (dev == NULL) {
- rcu_read_unlock();
- sock_kfree_s(sk, mc_lst, sizeof(*mc_lst));
- return -ENODEV;
- }
- mc_lst->ifindex = dev->ifindex;
- mc_lst->sfmode = MCAST_EXCLUDE;
- rwlock_init(&mc_lst->sflock);
- mc_lst->sflist = NULL;
- /*
- * now add/increase the group membership on the device
- */
- err = ipv6_dev_mc_inc(dev, addr);
- if (err) {
- rcu_read_unlock();
- sock_kfree_s(sk, mc_lst, sizeof(*mc_lst));
- return err;
- }
- spin_lock(&ipv6_sk_mc_lock);
- mc_lst->next = np->ipv6_mc_list;
- rcu_assign_pointer(np->ipv6_mc_list, mc_lst);
- spin_unlock(&ipv6_sk_mc_lock);
- rcu_read_unlock();
- return 0;
- }
- /*
- * socket leave on multicast group
- */
- int ipv6_sock_mc_drop(struct sock *sk, int ifindex, const struct in6_addr *addr)
- {
- struct ipv6_pinfo *np = inet6_sk(sk);
- struct ipv6_mc_socklist *mc_lst;
- struct ipv6_mc_socklist __rcu **lnk;
- struct net *net = sock_net(sk);
- spin_lock(&ipv6_sk_mc_lock);
- for (lnk = &np->ipv6_mc_list;
- (mc_lst = rcu_dereference_protected(*lnk,
- lockdep_is_held(&ipv6_sk_mc_lock))) !=NULL ;
- lnk = &mc_lst->next) {
- if ((ifindex == 0 || mc_lst->ifindex == ifindex) &&
- ipv6_addr_equal(&mc_lst->addr, addr)) {
- struct net_device *dev;
- *lnk = mc_lst->next;
- spin_unlock(&ipv6_sk_mc_lock);
- rcu_read_lock();
- dev = dev_get_by_index_rcu(net, mc_lst->ifindex);
- if (dev != NULL) {
- struct inet6_dev *idev = __in6_dev_get(dev);
- (void) ip6_mc_leave_src(sk, mc_lst, idev);
- if (idev)
- __ipv6_dev_mc_dec(idev, &mc_lst->addr);
- } else
- (void) ip6_mc_leave_src(sk, mc_lst, NULL);
- rcu_read_unlock();
- atomic_sub(sizeof(*mc_lst), &sk->sk_omem_alloc);
- kfree_rcu(mc_lst, rcu);
- return 0;
- }
- }
- spin_unlock(&ipv6_sk_mc_lock);
- return -EADDRNOTAVAIL;
- }
- /* called with rcu_read_lock() */
- static struct inet6_dev *ip6_mc_find_dev_rcu(struct net *net,
- const struct in6_addr *group,
- int ifindex)
- {
- struct net_device *dev = NULL;
- struct inet6_dev *idev = NULL;
- if (ifindex == 0) {
- struct rt6_info *rt = rt6_lookup(net, group, NULL, 0, 0);
- if (rt) {
- dev = rt->rt6i_dev;
- dst_release(&rt->dst);
- }
- } else
- dev = dev_get_by_index_rcu(net, ifindex);
- if (!dev)
- return NULL;
- idev = __in6_dev_get(dev);
- if (!idev)
- return NULL;
- read_lock_bh(&idev->lock);
- if (idev->dead) {
- read_unlock_bh(&idev->lock);
- return NULL;
- }
- return idev;
- }
- void ipv6_sock_mc_close(struct sock *sk)
- {
- struct ipv6_pinfo *np = inet6_sk(sk);
- struct ipv6_mc_socklist *mc_lst;
- struct net *net = sock_net(sk);
- spin_lock(&ipv6_sk_mc_lock);
- while ((mc_lst = rcu_dereference_protected(np->ipv6_mc_list,
- lockdep_is_held(&ipv6_sk_mc_lock))) != NULL) {
- struct net_device *dev;
- np->ipv6_mc_list = mc_lst->next;
- spin_unlock(&ipv6_sk_mc_lock);
- rcu_read_lock();
- dev = dev_get_by_index_rcu(net, mc_lst->ifindex);
- if (dev) {
- struct inet6_dev *idev = __in6_dev_get(dev);
- (void) ip6_mc_leave_src(sk, mc_lst, idev);
- if (idev)
- __ipv6_dev_mc_dec(idev, &mc_lst->addr);
- } else
- (void) ip6_mc_leave_src(sk, mc_lst, NULL);
- rcu_read_unlock();
- atomic_sub(sizeof(*mc_lst), &sk->sk_omem_alloc);
- kfree_rcu(mc_lst, rcu);
- spin_lock(&ipv6_sk_mc_lock);
- }
- spin_unlock(&ipv6_sk_mc_lock);
- }
- int ip6_mc_source(int add, int omode, struct sock *sk,
- struct group_source_req *pgsr)
- {
- struct in6_addr *source, *group;
- struct ipv6_mc_socklist *pmc;
- struct inet6_dev *idev;
- struct ipv6_pinfo *inet6 = inet6_sk(sk);
- struct ip6_sf_socklist *psl;
- struct net *net = sock_net(sk);
- int i, j, rv;
- int leavegroup = 0;
- int pmclocked = 0;
- int err;
- source = &((struct sockaddr_in6 *)&pgsr->gsr_source)->sin6_addr;
- group = &((struct sockaddr_in6 *)&pgsr->gsr_group)->sin6_addr;
- if (!ipv6_addr_is_multicast(group))
- return -EINVAL;
- rcu_read_lock();
- idev = ip6_mc_find_dev_rcu(net, group, pgsr->gsr_interface);
- if (!idev) {
- rcu_read_unlock();
- return -ENODEV;
- }
- err = -EADDRNOTAVAIL;
- for_each_pmc_rcu(inet6, pmc) {
- if (pgsr->gsr_interface && pmc->ifindex != pgsr->gsr_interface)
- continue;
- if (ipv6_addr_equal(&pmc->addr, group))
- break;
- }
- if (!pmc) { /* must have a prior join */
- err = -EINVAL;
- goto done;
- }
- /* if a source filter was set, must be the same mode as before */
- if (pmc->sflist) {
- if (pmc->sfmode != omode) {
- err = -EINVAL;
- goto done;
- }
- } else if (pmc->sfmode != omode) {
- /* allow mode switches for empty-set filters */
- ip6_mc_add_src(idev, group, omode, 0, NULL, 0);
- ip6_mc_del_src(idev, group, pmc->sfmode, 0, NULL, 0);
- pmc->sfmode = omode;
- }
- write_lock(&pmc->sflock);
- pmclocked = 1;
- psl = pmc->sflist;
- if (!add) {
- if (!psl)
- goto done; /* err = -EADDRNOTAVAIL */
- rv = !0;
- for (i=0; i<psl->sl_count; i++) {
- rv = memcmp(&psl->sl_addr[i], source,
- sizeof(struct in6_addr));
- if (rv == 0)
- break;
- }
- if (rv) /* source not found */
- goto done; /* err = -EADDRNOTAVAIL */
- /* special case - (INCLUDE, empty) == LEAVE_GROUP */
- if (psl->sl_count == 1 && omode == MCAST_INCLUDE) {
- leavegroup = 1;
- goto done;
- }
- /* update the interface filter */
- ip6_mc_del_src(idev, group, omode, 1, source, 1);
- for (j=i+1; j<psl->sl_count; j++)
- psl->sl_addr[j-1] = psl->sl_addr[j];
- psl->sl_count--;
- err = 0;
- goto done;
- }
- /* else, add a new source to the filter */
- if (psl && psl->sl_count >= sysctl_mld_max_msf) {
- err = -ENOBUFS;
- goto done;
- }
- if (!psl || psl->sl_count == psl->sl_max) {
- struct ip6_sf_socklist *newpsl;
- int count = IP6_SFBLOCK;
- if (psl)
- count += psl->sl_max;
- newpsl = sock_kmalloc(sk, IP6_SFLSIZE(count), GFP_ATOMIC);
- if (!newpsl) {
- err = -ENOBUFS;
- goto done;
- }
- newpsl->sl_max = count;
- newpsl->sl_count = count - IP6_SFBLOCK;
- if (psl) {
- for (i=0; i<psl->sl_count; i++)
- newpsl->sl_addr[i] = psl->sl_addr[i];
- sock_kfree_s(sk, psl, IP6_SFLSIZE(psl->sl_max));
- }
- pmc->sflist = psl = newpsl;
- }
- rv = 1; /* > 0 for insert logic below if sl_count is 0 */
- for (i=0; i<psl->sl_count; i++) {
- rv = memcmp(&psl->sl_addr[i], source, sizeof(struct in6_addr));
- if (rv == 0)
- break;
- }
- if (rv == 0) /* address already there is an error */
- goto done;
- for (j=psl->sl_count-1; j>=i; j--)
- psl->sl_addr[j+1] = psl->sl_addr[j];
- psl->sl_addr[i] = *source;
- psl->sl_count++;
- err = 0;
- /* update the interface list */
- ip6_mc_add_src(idev, group, omode, 1, source, 1);
- done:
- if (pmclocked)
- write_unlock(&pmc->sflock);
- read_unlock_bh(&idev->lock);
- rcu_read_unlock();
- if (leavegroup)
- return ipv6_sock_mc_drop(sk, pgsr->gsr_interface, group);
- return err;
- }
- int ip6_mc_msfilter(struct sock *sk, struct group_filter *gsf)
- {
- const struct in6_addr *group;
- struct ipv6_mc_socklist *pmc;
- struct inet6_dev *idev;
- struct ipv6_pinfo *inet6 = inet6_sk(sk);
- struct ip6_sf_socklist *newpsl, *psl;
- struct net *net = sock_net(sk);
- int leavegroup = 0;
- int i, err;
- group = &((struct sockaddr_in6 *)&gsf->gf_group)->sin6_addr;
- if (!ipv6_addr_is_multicast(group))
- return -EINVAL;
- if (gsf->gf_fmode != MCAST_INCLUDE &&
- gsf->gf_fmode != MCAST_EXCLUDE)
- return -EINVAL;
- rcu_read_lock();
- idev = ip6_mc_find_dev_rcu(net, group, gsf->gf_interface);
- if (!idev) {
- rcu_read_unlock();
- return -ENODEV;
- }
- err = 0;
- if (gsf->gf_fmode == MCAST_INCLUDE && gsf->gf_numsrc == 0) {
- leavegroup = 1;
- goto done;
- }
- for_each_pmc_rcu(inet6, pmc) {
- if (pmc->ifindex != gsf->gf_interface)
- continue;
- if (ipv6_addr_equal(&pmc->addr, group))
- break;
- }
- if (!pmc) { /* must have a prior join */
- err = -EINVAL;
- goto done;
- }
- if (gsf->gf_numsrc) {
- newpsl = sock_kmalloc(sk, IP6_SFLSIZE(gsf->gf_numsrc),
- GFP_ATOMIC);
- if (!newpsl) {
- err = -ENOBUFS;
- goto done;
- }
- newpsl->sl_max = newpsl->sl_count = gsf->gf_numsrc;
- for (i=0; i<newpsl->sl_count; ++i) {
- struct sockaddr_in6 *psin6;
- psin6 = (struct sockaddr_in6 *)&gsf->gf_slist[i];
- newpsl->sl_addr[i] = psin6->sin6_addr;
- }
- err = ip6_mc_add_src(idev, group, gsf->gf_fmode,
- newpsl->sl_count, newpsl->sl_addr, 0);
- if (err) {
- sock_kfree_s(sk, newpsl, IP6_SFLSIZE(newpsl->sl_max));
- goto done;
- }
- } else {
- newpsl = NULL;
- (void) ip6_mc_add_src(idev, group, gsf->gf_fmode, 0, NULL, 0);
- }
- write_lock(&pmc->sflock);
- psl = pmc->sflist;
- if (psl) {
- (void) ip6_mc_del_src(idev, group, pmc->sfmode,
- psl->sl_count, psl->sl_addr, 0);
- sock_kfree_s(sk, psl, IP6_SFLSIZE(psl->sl_max));
- } else
- (void) ip6_mc_del_src(idev, group, pmc->sfmode, 0, NULL, 0);
- pmc->sflist = newpsl;
- pmc->sfmode = gsf->gf_fmode;
- write_unlock(&pmc->sflock);
- err = 0;
- done:
- read_unlock_bh(&idev->lock);
- rcu_read_unlock();
- if (leavegroup)
- err = ipv6_sock_mc_drop(sk, gsf->gf_interface, group);
- return err;
- }
- int ip6_mc_msfget(struct sock *sk, struct group_filter *gsf,
- struct group_filter __user *optval, int __user *optlen)
- {
- int err, i, count, copycount;
- const struct in6_addr *group;
- struct ipv6_mc_socklist *pmc;
- struct inet6_dev *idev;
- struct ipv6_pinfo *inet6 = inet6_sk(sk);
- struct ip6_sf_socklist *psl;
- struct net *net = sock_net(sk);
- group = &((struct sockaddr_in6 *)&gsf->gf_group)->sin6_addr;
- if (!ipv6_addr_is_multicast(group))
- return -EINVAL;
- rcu_read_lock();
- idev = ip6_mc_find_dev_rcu(net, group, gsf->gf_interface);
- if (!idev) {
- rcu_read_unlock();
- return -ENODEV;
- }
- err = -EADDRNOTAVAIL;
- /*
- * changes to the ipv6_mc_list require the socket lock and
- * a read lock on ip6_sk_mc_lock. We have the socket lock,
- * so reading the list is safe.
- */
- for_each_pmc_rcu(inet6, pmc) {
- if (pmc->ifindex != gsf->gf_interface)
- continue;
- if (ipv6_addr_equal(group, &pmc->addr))
- break;
- }
- if (!pmc) /* must have a prior join */
- goto done;
- gsf->gf_fmode = pmc->sfmode;
- psl = pmc->sflist;
- count = psl ? psl->sl_count : 0;
- read_unlock_bh(&idev->lock);
- rcu_read_unlock();
- copycount = count < gsf->gf_numsrc ? count : gsf->gf_numsrc;
- gsf->gf_numsrc = count;
- if (put_user(GROUP_FILTER_SIZE(copycount), optlen) ||
- copy_to_user(optval, gsf, GROUP_FILTER_SIZE(0))) {
- return -EFAULT;
- }
- /* changes to psl require the socket lock, a read lock on
- * on ipv6_sk_mc_lock and a write lock on pmc->sflock. We
- * have the socket lock, so reading here is safe.
- */
- for (i=0; i<copycount; i++) {
- struct sockaddr_in6 *psin6;
- struct sockaddr_storage ss;
- psin6 = (struct sockaddr_in6 *)&ss;
- memset(&ss, 0, sizeof(ss));
- psin6->sin6_family = AF_INET6;
- psin6->sin6_addr = psl->sl_addr[i];
- if (copy_to_user(&optval->gf_slist[i], &ss, sizeof(ss)))
- return -EFAULT;
- }
- return 0;
- done:
- read_unlock_bh(&idev->lock);
- rcu_read_unlock();
- return err;
- }
- int inet6_mc_check(struct sock *sk, const struct in6_addr *mc_addr,
- const struct in6_addr *src_addr)
- {
- struct ipv6_pinfo *np = inet6_sk(sk);
- struct ipv6_mc_socklist *mc;
- struct ip6_sf_socklist *psl;
- int rv = 1;
- rcu_read_lock();
- for_each_pmc_rcu(np, mc) {
- if (ipv6_addr_equal(&mc->addr, mc_addr))
- break;
- }
- if (!mc) {
- rcu_read_unlock();
- return 1;
- }
- read_lock(&mc->sflock);
- psl = mc->sflist;
- if (!psl) {
- rv = mc->sfmode == MCAST_EXCLUDE;
- } else {
- int i;
- for (i=0; i<psl->sl_count; i++) {
- if (ipv6_addr_equal(&psl->sl_addr[i], src_addr))
- break;
- }
- if (mc->sfmode == MCAST_INCLUDE && i >= psl->sl_count)
- rv = 0;
- if (mc->sfmode == MCAST_EXCLUDE && i < psl->sl_count)
- rv = 0;
- }
- read_unlock(&mc->sflock);
- rcu_read_unlock();
- return rv;
- }
- static void ma_put(struct ifmcaddr6 *mc)
- {
- if (atomic_dec_and_test(&mc->mca_refcnt)) {
- in6_dev_put(mc->idev);
- kfree(mc);
- }
- }
- static void igmp6_group_added(struct ifmcaddr6 *mc)
- {
- struct net_device *dev = mc->idev->dev;
- char buf[MAX_ADDR_LEN];
- spin_lock_bh(&mc->mca_lock);
- if (!(mc->mca_flags&MAF_LOADED)) {
- mc->mca_flags |= MAF_LOADED;
- if (ndisc_mc_map(&mc->mca_addr, buf, dev, 0) == 0)
- dev_mc_add(dev, buf);
- }
- spin_unlock_bh(&mc->mca_lock);
- if (!(dev->flags & IFF_UP) || (mc->mca_flags & MAF_NOREPORT))
- return;
- if (MLD_V1_SEEN(mc->idev)) {
- igmp6_join_group(mc);
- return;
- }
- /* else v2 */
- mc->mca_crcount = mc->idev->mc_qrv;
- mld_ifc_event(mc->idev);
- }
- static void igmp6_group_dropped(struct ifmcaddr6 *mc)
- {
- struct net_device *dev = mc->idev->dev;
- char buf[MAX_ADDR_LEN];
- spin_lock_bh(&mc->mca_lock);
- if (mc->mca_flags&MAF_LOADED) {
- mc->mca_flags &= ~MAF_LOADED;
- if (ndisc_mc_map(&mc->mca_addr, buf, dev, 0) == 0)
- dev_mc_del(dev, buf);
- }
- if (mc->mca_flags & MAF_NOREPORT)
- goto done;
- spin_unlock_bh(&mc->mca_lock);
- if (!mc->idev->dead)
- igmp6_leave_group(mc);
- spin_lock_bh(&mc->mca_lock);
- if (del_timer(&mc->mca_timer))
- atomic_dec(&mc->mca_refcnt);
- done:
- ip6_mc_clear_src(mc);
- spin_unlock_bh(&mc->mca_lock);
- }
- /*
- * deleted ifmcaddr6 manipulation
- */
- static void mld_add_delrec(struct inet6_dev *idev, struct ifmcaddr6 *im)
- {
- struct ifmcaddr6 *pmc;
- /* this is an "ifmcaddr6" for convenience; only the fields below
- * are actually used. In particular, the refcnt and users are not
- * used for management of the delete list. Using the same structure
- * for deleted items allows change reports to use common code with
- * non-deleted or query-response MCA's.
- */
- pmc = kzalloc(sizeof(*pmc), GFP_ATOMIC);
- if (!pmc)
- return;
- spin_lock_bh(&im->mca_lock);
- spin_lock_init(&pmc->mca_lock);
- pmc->idev = im->idev;
- in6_dev_hold(idev);
- pmc->mca_addr = im->mca_addr;
- pmc->mca_crcount = idev->mc_qrv;
- pmc->mca_sfmode = im->mca_sfmode;
- if (pmc->mca_sfmode == MCAST_INCLUDE) {
- struct ip6_sf_list *psf;
- pmc->mca_tomb = im->mca_tomb;
- pmc->mca_sources = im->mca_sources;
- im->mca_tomb = im->mca_sources = NULL;
- for (psf=pmc->mca_sources; psf; psf=psf->sf_next)
- psf->sf_crcount = pmc->mca_crcount;
- }
- spin_unlock_bh(&im->mca_lock);
- spin_lock_bh(&idev->mc_lock);
- pmc->next = idev->mc_tomb;
- idev->mc_tomb = pmc;
- spin_unlock_bh(&idev->mc_lock);
- }
- static void mld_del_delrec(struct inet6_dev *idev, const struct in6_addr *pmca)
- {
- struct ifmcaddr6 *pmc, *pmc_prev;
- struct ip6_sf_list *psf, *psf_next;
- spin_lock_bh(&idev->mc_lock);
- pmc_prev = NULL;
- for (pmc=idev->mc_tomb; pmc; pmc=pmc->next) {
- if (ipv6_addr_equal(&pmc->mca_addr, pmca))
- break;
- pmc_prev = pmc;
- }
- if (pmc) {
- if (pmc_prev)
- pmc_prev->next = pmc->next;
- else
- idev->mc_tomb = pmc->next;
- }
- spin_unlock_bh(&idev->mc_lock);
- if (pmc) {
- for (psf=pmc->mca_tomb; psf; psf=psf_next) {
- psf_next = psf->sf_next;
- kfree(psf);
- }
- in6_dev_put(pmc->idev);
- kfree(pmc);
- }
- }
- static void mld_clear_delrec(struct inet6_dev *idev)
- {
- struct ifmcaddr6 *pmc, *nextpmc;
- spin_lock_bh(&idev->mc_lock);
- pmc = idev->mc_tomb;
- idev->mc_tomb = NULL;
- spin_unlock_bh(&idev->mc_lock);
- for (; pmc; pmc = nextpmc) {
- nextpmc = pmc->next;
- ip6_mc_clear_src(pmc);
- in6_dev_put(pmc->idev);
- kfree(pmc);
- }
- /* clear dead sources, too */
- read_lock_bh(&idev->lock);
- for (pmc=idev->mc_list; pmc; pmc=pmc->next) {
- struct ip6_sf_list *psf, *psf_next;
- spin_lock_bh(&pmc->mca_lock);
- psf = pmc->mca_tomb;
- pmc->mca_tomb = NULL;
- spin_unlock_bh(&pmc->mca_lock);
- for (; psf; psf=psf_next) {
- psf_next = psf->sf_next;
- kfree(psf);
- }
- }
- read_unlock_bh(&idev->lock);
- }
- /*
- * device multicast group inc (add if not found)
- */
- int ipv6_dev_mc_inc(struct net_device *dev, const struct in6_addr *addr)
- {
- struct ifmcaddr6 *mc;
- struct inet6_dev *idev;
- /* we need to take a reference on idev */
- idev = in6_dev_get(dev);
- if (idev == NULL)
- return -EINVAL;
- write_lock_bh(&idev->lock);
- if (idev->dead) {
- write_unlock_bh(&idev->lock);
- in6_dev_put(idev);
- return -ENODEV;
- }
- for (mc = idev->mc_list; mc; mc = mc->next) {
- if (ipv6_addr_equal(&mc->mca_addr, addr)) {
- mc->mca_users++;
- write_unlock_bh(&idev->lock);
- ip6_mc_add_src(idev, &mc->mca_addr, MCAST_EXCLUDE, 0,
- NULL, 0);
- in6_dev_put(idev);
- return 0;
- }
- }
- /*
- * not found: create a new one.
- */
- mc = kzalloc(sizeof(struct ifmcaddr6), GFP_ATOMIC);
- if (mc == NULL) {
- write_unlock_bh(&idev->lock);
- in6_dev_put(idev);
- return -ENOMEM;
- }
- setup_timer(&mc->mca_timer, igmp6_timer_handler, (unsigned long)mc);
- ipv6_addr_copy(&mc->mca_addr, addr);
- mc->idev = idev; /* (reference taken) */
- mc->mca_users = 1;
- /* mca_stamp should be updated upon changes */
- mc->mca_cstamp = mc->mca_tstamp = jiffies;
- atomic_set(&mc->mca_refcnt, 2);
- spin_lock_init(&mc->mca_lock);
- /* initial mode is (EX, empty) */
- mc->mca_sfmode = MCAST_EXCLUDE;
- mc->mca_sfcount[MCAST_EXCLUDE] = 1;
- if (ipv6_addr_is_ll_all_nodes(&mc->mca_addr) ||
- IPV6_ADDR_MC_SCOPE(&mc->mca_addr) < IPV6_ADDR_SCOPE_LINKLOCAL)
- mc->mca_flags |= MAF_NOREPORT;
- mc->next = idev->mc_list;
- idev->mc_list = mc;
- write_unlock_bh(&idev->lock);
- mld_del_delrec(idev, &mc->mca_addr);
- igmp6_group_added(mc);
- ma_put(mc);
- return 0;
- }
- /*
- * device multicast group del
- */
- int __ipv6_dev_mc_dec(struct inet6_dev *idev, const struct in6_addr *addr)
- {
- struct ifmcaddr6 *ma, **map;
- write_lock_bh(&idev->lock);
- for (map = &idev->mc_list; (ma=*map) != NULL; map = &ma->next) {
- if (ipv6_addr_equal(&ma->mca_addr, addr)) {
- if (--ma->mca_users == 0) {
- *map = ma->next;
- write_unlock_bh(&idev->lock);
- igmp6_group_dropped(ma);
- ma_put(ma);
- return 0;
- }
- write_unlock_bh(&idev->lock);
- return 0;
- }
- }
- write_unlock_bh(&idev->lock);
- return -ENOENT;
- }
- int ipv6_dev_mc_dec(struct net_device *dev, const struct in6_addr *addr)
- {
- struct inet6_dev *idev;
- int err;
- rcu_read_lock();
- idev = __in6_dev_get(dev);
- if (!idev)
- err = -ENODEV;
- else
- err = __ipv6_dev_mc_dec(idev, addr);
- rcu_read_unlock();
- return err;
- }
- /*
- * identify MLD packets for MLD filter exceptions
- */
- int ipv6_is_mld(struct sk_buff *skb, int nexthdr)
- {
- struct icmp6hdr *pic;
- if (nexthdr != IPPROTO_ICMPV6)
- return 0;
- if (!pskb_may_pull(skb, sizeof(struct icmp6hdr)))
- return 0;
- pic = icmp6_hdr(skb);
- switch (pic->icmp6_type) {
- case ICMPV6_MGM_QUERY:
- case ICMPV6_MGM_REPORT:
- case ICMPV6_MGM_REDUCTION:
- case ICMPV6_MLD2_REPORT:
- return 1;
- default:
- break;
- }
- return 0;
- }
- /*
- * check if the interface/address pair is valid
- */
- int ipv6_chk_mcast_addr(struct net_device *dev, const struct in6_addr *group,
- const struct in6_addr *src_addr)
- {
- struct inet6_dev *idev;
- struct ifmcaddr6 *mc;
- int rv = 0;
- rcu_read_lock();
- idev = __in6_dev_get(dev);
- if (idev) {
- read_lock_bh(&idev->lock);
- for (mc = idev->mc_list; mc; mc=mc->next) {
- if (ipv6_addr_equal(&mc->mca_addr, group))
- break;
- }
- if (mc) {
- if (src_addr && !ipv6_addr_any(src_addr)) {
- struct ip6_sf_list *psf;
- spin_lock_bh(&mc->mca_lock);
- for (psf=mc->mca_sources;psf;psf=psf->sf_next) {
- if (ipv6_addr_equal(&psf->sf_addr, src_addr))
- break;
- }
- if (psf)
- rv = psf->sf_count[MCAST_INCLUDE] ||
- psf->sf_count[MCAST_EXCLUDE] !=
- mc->mca_sfcount[MCAST_EXCLUDE];
- else
- rv = mc->mca_sfcount[MCAST_EXCLUDE] !=0;
- spin_unlock_bh(&mc->mca_lock);
- } else
- rv = 1; /* don't filter unspecified source */
- }
- read_unlock_bh(&idev->lock);
- }
- rcu_read_unlock();
- return rv;
- }
- static void mld_gq_start_timer(struct inet6_dev *idev)
- {
- int tv = net_random() % idev->mc_maxdelay;
- idev->mc_gq_running = 1;
- if (!mod_timer(&idev->mc_gq_timer, jiffies+tv+2))
- in6_dev_hold(idev);
- }
- static void mld_ifc_start_timer(struct inet6_dev *idev, int delay)
- {
- int tv = net_random() % delay;
- if (!mod_timer(&idev->mc_ifc_timer, jiffies+tv+2))
- in6_dev_hold(idev);
- }
- /*
- * IGMP handling (alias multicast ICMPv6 messages)
- */
- static void igmp6_group_queried(struct ifmcaddr6 *ma, unsigned long resptime)
- {
- unsigned long delay = resptime;
- /* Do not start timer for these addresses */
- if (ipv6_addr_is_ll_all_nodes(&ma->mca_addr) ||
- IPV6_ADDR_MC_SCOPE(&ma->mca_addr) < IPV6_ADDR_SCOPE_LINKLOCAL)
- return;
- if (del_timer(&ma->mca_timer)) {
- atomic_dec(&ma->mca_refcnt);
- delay = ma->mca_timer.expires - jiffies;
- }
- if (delay >= resptime) {
- if (resptime)
- delay = net_random() % resptime;
- else
- delay = 1;
- }
- ma->mca_timer.expires = jiffies + delay;
- if (!mod_timer(&ma->mca_timer, jiffies + delay))
- atomic_inc(&ma->mca_refcnt);
- ma->mca_flags |= MAF_TIMER_RUNNING;
- }
- /* mark EXCLUDE-mode sources */
- static int mld_xmarksources(struct ifmcaddr6 *pmc, int nsrcs,
- const struct in6_addr *srcs)
- {
- struct ip6_sf_list *psf;
- int i, scount;
- scount = 0;
- for (psf=pmc->mca_sources; psf; psf=psf->sf_next) {
- if (scount == nsrcs)
- break;
- for (i=0; i<nsrcs; i++) {
- /* skip inactive filters */
- if (psf->sf_count[MCAST_INCLUDE] ||
- pmc->mca_sfcount[MCAST_EXCLUDE] !=
- psf->sf_count[MCAST_EXCLUDE])
- continue;
- if (ipv6_addr_equal(&srcs[i], &psf->sf_addr)) {
- scount++;
- break;
- }
- }
- }
- pmc->mca_flags &= ~MAF_GSQUERY;
- if (scount == nsrcs) /* all sources excluded */
- return 0;
- return 1;
- }
- static int mld_marksources(struct ifmcaddr6 *pmc, int nsrcs,
- const struct in6_addr *srcs)
- {
- struct ip6_sf_list *psf;
- int i, scount;
- if (pmc->mca_sfmode == MCAST_EXCLUDE)
- return mld_xmarksources(pmc, nsrcs, srcs);
- /* mark INCLUDE-mode sources */
- scount = 0;
- for (psf=pmc->mca_sources; psf; psf=psf->sf_next) {
- if (scount == nsrcs)
- break;
- for (i=0; i<nsrcs; i++) {
- if (ipv6_addr_equal(&srcs[i], &psf->sf_addr)) {
- psf->sf_gsresp = 1;
- scount++;
- break;
- }
- }
- }
- if (!scount) {
- pmc->mca_flags &= ~MAF_GSQUERY;
- return 0;
- }
- pmc->mca_flags |= MAF_GSQUERY;
- return 1;
- }
- /* called with rcu_read_lock() */
- int igmp6_event_query(struct sk_buff *skb)
- {
- struct mld2_query *mlh2 = NULL;
- struct ifmcaddr6 *ma;
- const struct in6_addr *group;
- unsigned long max_delay;
- struct inet6_dev *idev;
- struct mld_msg *mld;
- int group_type;
- int mark = 0;
- int len;
- if (!pskb_may_pull(skb, sizeof(struct in6_addr)))
- return -EINVAL;
- /* compute payload length excluding extension headers */
- len = ntohs(ipv6_hdr(skb)->payload_len) + sizeof(struct ipv6hdr);
- len -= skb_network_header_len(skb);
- /* Drop queries with not link local source */
- if (!(ipv6_addr_type(&ipv6_hdr(skb)->saddr) & IPV6_ADDR_LINKLOCAL))
- return -EINVAL;
- idev = __in6_dev_get(skb->dev);
- if (idev == NULL)
- return 0;
- mld = (struct mld_msg *)icmp6_hdr(skb);
- group = &mld->mld_mca;
- group_type = ipv6_addr_type(group);
- if (group_type != IPV6_ADDR_ANY &&
- !(group_type&IPV6_ADDR_MULTICAST))
- return -EINVAL;
- if (len == 24) {
- int switchback;
- /* MLDv1 router present */
- /* Translate milliseconds to jiffies */
- max_delay = (ntohs(mld->mld_maxdelay)*HZ)/1000;
- switchback = (idev->mc_qrv + 1) * max_delay;
- idev->mc_v1_seen = jiffies + switchback;
- /* cancel the interface change timer */
- idev->mc_ifc_count = 0;
- if (del_timer(&idev->mc_ifc_timer))
- __in6_dev_put(idev);
- /* clear deleted report items */
- mld_clear_delrec(idev);
- } else if (len >= 28) {
- int srcs_offset = sizeof(struct mld2_query) -
- sizeof(struct icmp6hdr);
- if (!pskb_may_pull(skb, srcs_offset))
- return -EINVAL;
- mlh2 = (struct mld2_query *)skb_transport_header(skb);
- max_delay = (MLDV2_MRC(ntohs(mlh2->mld2q_mrc))*HZ)/1000;
- if (!max_delay)
- max_delay = 1;
- idev->mc_maxdelay = max_delay;
- if (mlh2->mld2q_qrv)
- idev->mc_qrv = mlh2->mld2q_qrv;
- if (group_type == IPV6_ADDR_ANY) { /* general query */
- if (mlh2->mld2q_nsrcs)
- return -EINVAL; /* no sources allowed */
- mld_gq_start_timer(idev);
- return 0;
- }
- /* mark sources to include, if group & source-specific */
- if (mlh2->mld2q_nsrcs != 0) {
- if (!pskb_may_pull(skb, srcs_offset +
- ntohs(mlh2->mld2q_nsrcs) * sizeof(struct in6_addr)))
- return -EINVAL;
- mlh2 = (struct mld2_query *)skb_transport_header(skb);
- mark = 1;
- }
- } else
- return -EINVAL;
- read_lock_bh(&idev->lock);
- if (group_type == IPV6_ADDR_ANY) {
- for (ma = idev->mc_list; ma; ma=ma->next) {
- spin_lock_bh(&ma->mca_lock);
- igmp6_group_queried(ma, max_delay);
- spin_unlock_bh(&ma->mca_lock);
- }
- } else {
- for (ma = idev->mc_list; ma; ma=ma->next) {
- if (!ipv6_addr_equal(group, &ma->mca_addr))
- continue;
- spin_lock_bh(&ma->mca_lock);
- if (ma->mca_flags & MAF_TIMER_RUNNING) {
- /* gsquery <- gsquery && mark */
- if (!mark)
- ma->mca_flags &= ~MAF_GSQUERY;
- } else {
- /* gsquery <- mark */
- if (mark)
- ma->mca_flags |= MAF_GSQUERY;
- else
- ma->mca_flags &= ~MAF_GSQUERY;
- }
- if (!(ma->mca_flags & MAF_GSQUERY) ||
- mld_marksources(ma, ntohs(mlh2->mld2q_nsrcs), mlh2->mld2q_srcs))
- igmp6_group_queried(ma, max_delay);
- spin_unlock_bh(&ma->mca_lock);
- break;
- }
- }
- read_unlock_bh(&idev->lock);
- return 0;
- }
- /* called with rcu_read_lock() */
- int igmp6_event_report(struct sk_buff *skb)
- {
- struct ifmcaddr6 *ma;
- struct inet6_dev *idev;
- struct mld_msg *mld;
- int addr_type;
- /* Our own report looped back. Ignore it. */
- if (skb->pkt_type == PACKET_LOOPBACK)
- return 0;
- /* send our report if the MC router may not have heard this report */
- if (skb->pkt_type != PACKET_MULTICAST &&
- skb->pkt_type != PACKET_BROADCAST)
- return 0;
- if (!pskb_may_pull(skb, sizeof(*mld) - sizeof(struct icmp6hdr)))
- return -EINVAL;
- mld = (struct mld_msg *)icmp6_hdr(skb);
- /* Drop reports with not link local source */
- addr_type = ipv6_addr_type(&ipv6_hdr(skb)->saddr);
- if (addr_type != IPV6_ADDR_ANY &&
- !(addr_type&IPV6_ADDR_LINKLOCAL))
- return -EINVAL;
- idev = __in6_dev_get(skb->dev);
- if (idev == NULL)
- return -ENODEV;
- /*
- * Cancel the timer for this group
- */
- read_lock_bh(&idev->lock);
- for (ma = idev->mc_list; ma; ma=ma->next) {
- if (ipv6_addr_equal(&ma->mca_addr, &mld->mld_mca)) {
- spin_lock(&ma->mca_lock);
- if (del_timer(&ma->mca_timer))
- atomic_dec(&ma->mca_refcnt);
- ma->mca_flags &= ~(MAF_LAST_REPORTER|MAF_TIMER_RUNNING);
- spin_unlock(&ma->mca_lock);
- break;
- }
- }
- read_unlock_bh(&idev->lock);
- return 0;
- }
- static int is_in(struct ifmcaddr6 *pmc, struct ip6_sf_list *psf, int type,
- int gdeleted, int sdeleted)
- {
- switch (type) {
- case MLD2_MODE_IS_INCLUDE:
- case MLD2_MODE_IS_EXCLUDE:
- if (gdeleted || sdeleted)
- return 0;
- if (!((pmc->mca_flags & MAF_GSQUERY) && !psf->sf_gsresp)) {
- if (pmc->mca_sfmode == MCAST_INCLUDE)
- return 1;
- /* don't include if this source is excluded
- * in all filters
- */
- if (psf->sf_count[MCAST_INCLUDE])
- return type == MLD2_MODE_IS_INCLUDE;
- return pmc->mca_sfcount[MCAST_EXCLUDE] ==
- psf->sf_count[MCAST_EXCLUDE];
- }
- return 0;
- case MLD2_CHANGE_TO_INCLUDE:
- if (gdeleted || sdeleted)
- return 0;
- return psf->sf_count[MCAST_INCLUDE] != 0;
- case MLD2_CHANGE_TO_EXCLUDE:
- if (gdeleted || sdeleted)
- return 0;
- if (pmc->mca_sfcount[MCAST_EXCLUDE] == 0 ||
- psf->sf_count[MCAST_INCLUDE])
- return 0;
- return pmc->mca_sfcount[MCAST_EXCLUDE] ==
- psf->sf_count[MCAST_EXCLUDE];
- case MLD2_ALLOW_NEW_SOURCES:
- if (gdeleted || !psf->sf_crcount)
- return 0;
- return (pmc->mca_sfmode == MCAST_INCLUDE) ^ sdeleted;
- case MLD2_BLOCK_OLD_SOURCES:
- if (pmc->mca_sfmode == MCAST_INCLUDE)
- return gdeleted || (psf->sf_crcount && sdeleted);
- return psf->sf_crcount && !gdeleted && !sdeleted;
- }
- return 0;
- }
- static int
- mld_scount(struct ifmcaddr6 *pmc, int type, int gdeleted, int sdeleted)
- {
- struct ip6_sf_list *psf;
- int scount = 0;
- for (psf=pmc->mca_sources; psf; psf=psf->sf_next) {
- if (!is_in(pmc, psf, type, gdeleted, sdeleted))
- continue;
- scount++;
- }
- return scount;
- }
- static struct sk_buff *mld_newpack(struct net_device *dev, int size)
- {
- struct net *net = dev_net(dev);
- struct sock *sk = net->ipv6.igmp_sk;
- struct sk_buff *skb;
- struct mld2_report *pmr;
- struct in6_addr addr_buf;
- const struct in6_addr *saddr;
- int err;
- u8 ra[8] = { IPPROTO_ICMPV6, 0,
- IPV6_TLV_ROUTERALERT, 2, 0, 0,
- IPV6_TLV_PADN, 0 };
- /* we assume size > sizeof(ra) here */
- size += LL_ALLOCATED_SPACE(dev);
- /* limit our allocations to order-0 page */
- size = min_t(int, size, SKB_MAX_ORDER(0, 0));
- skb = sock_alloc_send_skb(sk, size, 1, &err);
- if (!skb)
- return NULL;
- skb_reserve(skb, LL_RESERVED_SPACE(dev));
- if (ipv6_get_lladdr(dev, &addr_buf, IFA_F_TENTATIVE)) {
- /* <draft-ietf-magma-mld-source-05.txt>:
- * use unspecified address as the source address
- * when a valid link-local address is not available.
- */
- saddr = &in6addr_any;
- } else
- saddr = &addr_buf;
- ip6_nd_hdr(sk, skb, dev, saddr, &mld2_all_mcr, NEXTHDR_HOP, 0);
- memcpy(skb_put(skb, sizeof(ra)), ra, sizeof(ra));
- skb_set_transport_header(skb, skb_tail_pointer(skb) - skb->data);
- skb_put(skb, sizeof(*pmr));
- pmr = (struct mld2_report *)skb_transport_header(skb);
- pmr->mld2r_type = ICMPV6_MLD2_REPORT;
- pmr->mld2r_resv1 = 0;
- pmr->mld2r_cksum = 0;
- pmr->mld2r_resv2 = 0;
- pmr->mld2r_ngrec = 0;
- return skb;
- }
- static void mld_sendpack(struct sk_buff *skb)
- {
- struct ipv6hdr *pip6 = ipv6_hdr(skb);
- struct mld2_report *pmr =
- (struct mld2_report *)skb_transport_header(skb);
- int payload_len, mldlen;
- struct inet6_dev *idev;
- struct net *net = dev_net(skb->dev);
- int err;
- struct flowi6 fl6;
- struct dst_entry *dst;
- rcu_read_lock();
- idev = __in6_dev_get(skb->dev);
- IP6_UPD_PO_STATS(net, idev, IPSTATS_MIB_OUT, skb->len);
- payload_len = (skb->tail - skb->network_header) - sizeof(*pip6);
- mldlen = skb->tail - skb->transport_header;
- pip6->payload_len = htons(payload_len);
- pmr->mld2r_cksum = csum_ipv6_magic(&pip6->saddr, &pip6->daddr, mldlen,
- IPPROTO_ICMPV6,
- csum_partial(skb_transport_header(skb),
- mldlen, 0));
- dst = icmp6_dst_alloc(skb->dev, NULL, &ipv6_hdr(skb)->daddr);
- if (!dst) {
- err = -ENOMEM;
- goto err_out;
- }
- icmpv6_flow_init(net->ipv6.igmp_sk, &fl6, ICMPV6_MLD2_REPORT,
- &ipv6_hdr(skb)->saddr, &ipv6_hdr(skb)->daddr,
- skb->dev->ifindex);
- dst = xfrm_lookup(net, dst, flowi6_to_flowi(&fl6), NULL, 0);
- err = 0;
- if (IS_ERR(dst)) {
- err = PTR_ERR(dst);
- dst = NULL;
- }
- skb_dst_set(skb, dst);
- if (err)
- goto err_out;
- payload_len = skb->len;
- err = NF_HOOK(NFPROTO_IPV6, NF_INET_LOCAL_OUT, skb, NULL, skb->dev,
- dst_output);
- out:
- if (!err) {
- ICMP6MSGOUT_INC_STATS_BH(net, idev, ICMPV6_MLD2_REPORT);
- ICMP6_INC_STATS_BH(net, idev, ICMP6_MIB_OUTMSGS);
- IP6_UPD_PO_STATS_BH(net, idev, IPSTATS_MIB_OUTMCAST, payload_len);
- } else
- IP6_INC_STATS_BH(net, idev, IPSTATS_MIB_OUTDISCARDS);
- rcu_read_unlock();
- return;
- err_out:
- kfree_skb(skb);
- goto out;
- }
- static int grec_size(struct ifmcaddr6 *pmc, int type, int gdel, int sdel)
- {
- return sizeof(struct mld2_grec) + 16 * mld_scount(pmc,type,gdel,sdel);
- }
- static struct sk_buff *add_grhead(struct sk_buff *skb, struct ifmcaddr6 *pmc,
- int type, struct mld2_grec **ppgr)
- {
- struct net_device *dev = pmc->idev->dev;
- struct mld2_report *pmr;
- struct mld2_grec *pgr;
- if (!skb)
- skb = mld_newpack(dev, dev->mtu);
- if (!skb)
- return NULL;
- pgr = (struct mld2_grec *)skb_put(skb, sizeof(struct mld2_grec));
- pgr->grec_type = type;
- pgr->grec_auxwords = 0;
- pgr->grec_nsrcs = 0;
- pgr->grec_mca = pmc->mca_addr; /* structure copy */
- pmr = (struct mld2_report *)skb_transport_header(skb);
- pmr->mld2r_ngrec = htons(ntohs(pmr->mld2r_ngrec)+1);
- *ppgr = pgr;
- return skb;
- }
- #define AVAILABLE(skb) ((skb) ? ((skb)->dev ? (skb)->dev->mtu - (skb)->len : \
- skb_tailroom(skb)) : 0)
- static struct sk_buff *add_grec(struct sk_buff *skb, struct ifmcaddr6 *pmc,
- int type, int gdeleted, int sdeleted)
- {
- struct net_device *dev = pmc->idev->dev;
- struct mld2_report *pmr;
- struct mld2_grec *pgr = NULL;
- struct ip6_sf_list *psf, *psf_next, *psf_prev, **psf_list;
- int scount, stotal, first, isquery, truncate;
- if (pmc->mca_flags & MAF_NOREPORT)
- return skb;
- isquery = type == MLD2_MODE_IS_INCLUDE ||
- type == MLD2_MODE_IS_EXCLUDE;
- truncate = type == MLD2_MODE_IS_EXCLUDE ||
- type == MLD2_CHANGE_TO_EXCLUDE;
- stotal = scount = 0;
- psf_list = sdeleted ? &pmc->mca_tomb : &pmc->mca_sources;
- if (!*psf_list)
- goto empty_source;
- pmr = skb ? (struct mld2_report *)skb_transport_header(skb) : NULL;
- /* EX and TO_EX get a fresh packet, if needed */
- if (truncate) {
- if (pmr && pmr->mld2r_ngrec &&
- AVAILABLE(skb) < grec_size(pmc, type, gdeleted, sdeleted)) {
- if (skb)
- mld_sendpack(skb);
- skb = mld_newpack(dev, dev->mtu);
- }
- }
- first = 1;
- psf_prev = NULL;
- for (psf=*psf_list; psf; psf=psf_next) {
- struct in6_addr *psrc;
- psf_next = psf->sf_next;
- if (!is_in(pmc, psf, type, gdeleted, sdeleted)) {
- psf_prev = psf;
- continue;
- }
- /* clear marks on query responses */
- if (isquery)
- psf->sf_gsresp = 0;
- if (AVAILABLE(skb) < sizeof(*psrc) +
- first*sizeof(struct mld2_grec)) {
- if (truncate && !first)
- break; /* truncate these */
- if (pgr)
- pgr->grec_nsrcs = htons(scount);
- if (skb)
- mld_sendpack(skb);
- skb = mld_newpack(dev, dev->mtu);
- first = 1;
- scount = 0;
- }
- if (first) {
- skb = add_grhead(skb, pmc, type, &pgr);
- first = 0;
- }
- if (!skb)
- return NULL;
- psrc = (struct in6_addr *)skb_put(skb, sizeof(*psrc));
- *psrc = psf->sf_addr;
- scount++; stotal++;
- if ((type == MLD2_ALLOW_NEW_SOURCES ||
- type == MLD2_BLOCK_OLD_SOURCES) && psf->sf_crcount) {
- psf->sf_crcount--;
- if ((sdeleted || gdeleted) && psf->sf_crcount == 0) {
- if (psf_prev)
- psf_prev->sf_next = psf->sf_next;
- else
- *psf_list = psf->sf_next;
- kfree(psf);
- continue;
- }
- }
- psf_prev = psf;
- }
- empty_source:
- if (!stotal) {
- if (type == MLD2_ALLOW_NEW_SOURCES ||
- type == MLD2_BLOCK_OLD_SOURCES)
- return skb;
- if (pmc->mca_crcount || isquery) {
- /* make sure we have room for group header */
- if (skb && AVAILABLE(skb) < sizeof(struct mld2_grec)) {
- mld_sendpack(skb);
- skb = NULL; /* add_grhead will get a new one */
- }
- skb = add_grhead(skb, pmc, type, &pgr);
- }
- }
- if (pgr)
- pgr->grec_nsrcs = htons(scount);
- if (isquery)
- pmc->mca_flags &= ~MAF_GSQUERY; /* clear query state */
- return skb;
- }
- static void mld_send_report(struct inet6_dev *idev, struct ifmcaddr6 *pmc)
- {
- struct sk_buff *skb = NULL;
- int type;
- if (!pmc) {
- read_lock_bh(&idev->lock);
- for (pmc=idev->mc_list; pmc; pmc=pmc->next) {
- if (pmc->mca_flags & MAF_NOREPORT)
- continue;
- spin_lock_bh(&pmc->mca_lock);
- if (pmc->mca_sfcount[MCAST_EXCLUDE])
- type = MLD2_MODE_IS_EXCLUDE;
- else
- type = MLD2_MODE_IS_INCLUDE;
- skb = add_grec(skb, pmc, type, 0, 0);
- spin_unlock_bh(&pmc->mca_lock);
- }
- read_unlock_bh(&idev->lock);
- } else {
- spin_lock_bh(&pmc->mca_lock);
- if (pmc->mca_sfcount[MCAST_EXCLUDE])
- type = MLD2_MODE_IS_EXCLUDE;
- else
- type = MLD2_MODE_IS_INCLUDE;
- skb = add_grec(skb, pmc, type, 0, 0);
- spin_unlock_bh(&pmc->mca_lock);
- }
- if (skb)
- mld_sendpack(skb);
- }
- /*
- * remove zero-count source records from a source filter list
- */
- static void mld_clear_zeros(struct ip6_sf_list **ppsf)
- {
- struct ip6_sf_list *psf_prev, *psf_next, *psf;
- psf_prev = NULL;
- for (psf=*ppsf; psf; psf = psf_next) {
- psf_next = psf->sf_next;
- if (psf->sf_crcount == 0) {
- if (psf_prev)
- psf_prev->sf_next = psf->sf_next;
- else
- *ppsf = psf->sf_next;
- kfree(psf);
- } else
- psf_prev = psf;
- }
- }
- static void mld_send_cr(struct inet6_dev *idev)
- {
- struct ifmcaddr6 *pmc, *pmc_prev, *pmc_next;
- struct sk_buff *skb = NULL;
- int type, dtype;
- read_lock_bh(&idev->lock);
- spin_lock(&idev->mc_lock);
- /* deleted MCA's */
- pmc_prev = NULL;
- for (pmc=idev->mc_tomb; pmc; pmc=pmc_next) {
- pmc_next = pmc->next;
- if (pmc->mca_sfmode == MCAST_INCLUDE) {
- type = MLD2_BLOCK_OLD_SOURCES;
- dtype = MLD2_BLOCK_OLD_SOURCES;
- skb = add_grec(skb, pmc, type, 1, 0);
- skb = add_grec(skb, pmc, dtype, 1, 1);
- }
- if (pmc->mca_crcount) {
- if (pmc->mca_sfmode == MCAST_EXCLUDE) {
- type = MLD2_CHANGE_TO_INCLUDE;
- skb = add_grec(skb, pmc, type, 1, 0);
- }
- pmc->mca_crcount--;
- if (pmc->mca_crcount == 0) {
- mld_clear_zeros(&pmc->mca_tomb);
- mld_clear_zeros(&pmc->mca_sources);
- }
- }
- if (pmc->mca_crcount == 0 && !pmc->mca_tomb &&
- !pmc->mca_sources) {
- if (pmc_prev)
- pmc_prev->next = pmc_next;
- else
- idev->mc_tomb = pmc_next;
- in6_dev_put(pmc->idev);
- kfree(pmc);
- } else
- pmc_prev = pmc;
- }
- spin_unlock(&idev->mc_lock);
- /* change recs */
- for (pmc=idev->mc_list; pmc; pmc=pmc->next) {
- spin_lock_bh(&pmc->mca_lock);
- if (pmc->mca_sfcount[MCAST_EXCLUDE]) {
- type = MLD2_BLOCK_OLD_SOURCES;
- dtype = MLD2_ALLOW_NEW_SOURCES;
- } else {
- type = MLD2_ALLOW_NEW_SOURCES;
- dtype = MLD2_BLOCK_OLD_SOURCES;
- }
- skb = add_grec(skb, pmc, type, 0, 0);
- skb = add_grec(skb, pmc, dtype, 0, 1); /* deleted sources */
- /* filter mode changes */
- if (pmc->mca_crcount) {
- if (pmc->mca_sfmode == MCAST_EXCLUDE)
- type = MLD2_CHANGE_TO_EXCLUDE;
- else
- type = MLD2_CHANGE_TO_INCLUDE;
- skb = add_grec(skb, pmc, type, 0, 0);
- pmc->mca_crcount--;
- }
- spin_unlock_bh(&pmc->mca_lock);
- }
- read_unlock_bh(&idev->lock);
- if (!skb)
- return;
- (void) mld_sendpack(skb);
- }
- static void igmp6_send(struct in6_addr *addr, struct net_device *dev, int type)
- {
- struct net *net = dev_net(dev);
- struct sock *sk = net->ipv6.igmp_sk;
- struct inet6_dev *idev;
- struct sk_buff *skb;
- struct mld_msg *hdr;
- const struct in6_addr *snd_addr, *saddr;
- struct in6_addr addr_buf;
- int err, len, payload_len, full_len;
- u8 ra[8] = { IPPROTO_ICMPV6, 0,
- IPV6_TLV_ROUTERALERT, 2, 0, 0,
- IPV6_TLV_PADN, 0 };
- struct flowi6 fl6;
- struct dst_entry *dst;
- if (type == ICMPV6_MGM_REDUCTION)
- snd_addr = &in6addr_linklocal_allrouters;
- else
- snd_addr = addr;
- len = sizeof(struct icmp6hdr) + sizeof(struct in6_addr);
- payload_len = len + sizeof(ra);
- full_len = sizeof(struct ipv6hdr) + payload_len;
- rcu_read_lock();
- IP6_UPD_PO_STATS(net, __in6_dev_get(dev),
- IPSTATS_MIB_OUT, full_len);
- rcu_read_unlock();
- skb = sock_alloc_send_skb(sk, LL_ALLOCATED_SPACE(dev) + full_len, 1, &err);
- if (skb == NULL) {
- rcu_read_lock();
- IP6_INC_STATS(net, __in6_dev_get(dev),
- IPSTATS_MIB_OUTDISCARDS);
- rcu_read_unlock();
- return;
- }
- skb_reserve(skb, LL_RESERVED_SPACE(dev));
- if (ipv6_get_lladdr(dev, &addr_buf, IFA_F_TENTATIVE)) {
- /* <draft-ietf-magma-mld-source-05.txt>:
- * use unspecified address as the source address
- * when a valid link-local address is not available.
- */
- saddr = &in6addr_any;
- } else
- saddr = &addr_buf;
- ip6_nd_hdr(sk, skb, dev, saddr, snd_addr, NEXTHDR_HOP, payload_len);
- memcpy(skb_put(skb, sizeof(ra)), ra, sizeof(ra));
- hdr = (struct mld_msg *) skb_put(skb, sizeof(struct mld_msg));
- memset(hdr, 0, sizeof(struct mld_msg));
- hdr->mld_type = type;
- ipv6_addr_copy(&hdr->mld_mca, addr);
- hdr->mld_cksum = csum_ipv6_magic(saddr, snd_addr, len,
- IPPROTO_ICMPV6,
- csum_partial(hdr, len, 0));
- rcu_read_lock();
- idev = __in6_dev_get(skb->dev);
- dst = icmp6_dst_alloc(skb->dev, NULL, &ipv6_hdr(skb)->daddr);
- if (!dst) {
- err = -ENOMEM;
- goto err_out;
- }
- icmpv6_flow_init(sk, &fl6, type,
- &ipv6_hdr(skb)->saddr, &ipv6_hdr(skb)->daddr,
- skb->dev->ifindex);
- dst = xfrm_lookup(net, dst, flowi6_to_flowi(&fl6), NULL, 0);
- if (IS_ERR(dst)) {
- err = PTR_ERR(dst);
- goto err_out;
- }
- skb_dst_set(skb, dst);
- err = NF_HOOK(NFPROTO_IPV6, NF_INET_LOCAL_OUT, skb, NULL, skb->dev,
- dst_output);
- out:
- if (!err) {
- ICMP6MSGOUT_INC_STATS(net, idev, type);
- ICMP6_INC_STATS(net, idev, ICMP6_MIB_OUTMSGS);
- IP6_UPD_PO_STATS(net, idev, IPSTATS_MIB_OUTMCAST, full_len);
- } else
- IP6_INC_STATS(net, idev, IPSTATS_MIB_OUTDISCARDS);
- rcu_read_unlock();
- return;
- err_out:
- kfree_skb(skb);
- goto out;
- }
- static int ip6_mc_del1_src(struct ifmcaddr6 *pmc, int sfmode,
- const struct in6_addr *psfsrc)
- {
- struct ip6_sf_list *psf, *psf_prev;
- int rv = 0;
- psf_prev = NULL;
- for (psf=pmc->mca_sources; psf; psf=psf->sf_next) {
- if (ipv6_addr_equal(&psf->sf_addr, psfsrc))
- break;
- psf_prev = psf;
- }
- if (!psf || psf->sf_count[sfmode] == 0) {
- /* source filter not found, or count wrong => bug */
- return -ESRCH;
- }
- psf->sf_count[sfmode]--;
- if (!psf->sf_count[MCAST_INCLUDE] && !psf->sf_count[MCAST_EXCLUDE]) {
- struct inet6_dev *idev = pmc->idev;
- /* no more filters for this source */
- if (psf_prev)
- psf_prev->sf_next = psf->sf_next;
- else
- pmc->mca_sources = psf->sf_next;
- if (psf->sf_oldin && !(pmc->mca_flags & MAF_NOREPORT) &&
- !MLD_V1_SEEN(idev)) {
- psf->sf_crcount = idev->mc_qrv;
- psf->sf_next = pmc->mca_tomb;
- pmc->mca_tomb = psf;
- rv = 1;
- } else
- kfree(psf);
- }
- return rv;
- }
- static int ip6_mc_del_src(struct inet6_dev *idev, const struct in6_addr *pmca,
- int sfmode, int sfcount, const struct in6_addr *psfsrc,
- int delta)
- {
- struct ifmcaddr6 *pmc;
- int changerec = 0;
- int i, err;
- if (!idev)
- return -ENODEV;
- read_lock_bh(&idev->lock);
- for (pmc=idev->mc_list; pmc; pmc=pmc->next) {
- if (ipv6_addr_equal(pmca, &pmc->mca_addr))
- break;
- }
- if (!pmc) {
- /* MCA not found?? bug */
- read_unlock_bh(&idev->lock);
- return -ESRCH;
- }
- spin_lock_bh(&pmc->mca_lock);
- sf_markstate(pmc);
- if (!delta) {
- if (!pmc->mca_sfcount[sfmode]) {
- spin_unlock_bh(&pmc->mca_lock);
- read_unlock_bh(&idev->lock);
- return -EINVAL;
- }
- pmc->mca_sfcount[sfmode]--;
- }
- err = 0;
- for (i=0; i<sfcount; i++) {
- int rv = ip6_mc_del1_src(pmc, sfmode, &psfsrc[i]);
- changerec |= rv > 0;
- if (!err && rv < 0)
- err = rv;
- }
- if (pmc->mca_sfmode == MCAST_EXCLUDE &&
- pmc->mca_sfcount[MCAST_EXCLUDE] == 0 &&
- pmc->mca_sfcount[MCAST_INCLUDE]) {
- struct ip6_sf_list *psf;
- /* filter mode change */
- pmc->mca_sfmode = MCAST_INCLUDE;
- pmc->mca_crcount = idev->mc_qrv;
- idev->mc_ifc_count = pmc->mca_crcount;
- for (psf=pmc->mca_sources; psf; psf = psf->sf_next)
- psf->sf_crcount = 0;
- mld_ifc_event(pmc->idev);
- } else if (sf_setstate(pmc) || changerec)
- mld_ifc_event(pmc->idev);
- spin_unlock_bh(&pmc->mca_lock);
- read_unlock_bh(&idev->lock);
- return err;
- }
- /*
- * Add multicast single-source filter to the interface list
- */
- static int ip6_mc_add1_src(struct ifmcaddr6 *pmc, int sfmode,
- const struct in6_addr *psfsrc, int delta)
- {
- struct ip6_sf_list *psf, *psf_prev;
- psf_prev = NULL;
- for (psf=pmc->mca_sources; psf; psf=psf->sf_next) {
- if (ipv6_addr_equal(&psf->sf_addr, psfsrc))
- break;
- psf_prev = psf;
- }
- if (!psf) {
- psf = kzalloc(sizeof(*psf), GFP_ATOMIC);
- if (!psf)
- return -ENOBUFS;
- psf->sf_addr = *psfsrc;
- if (psf_prev) {
- psf_prev->sf_next = psf;
- } else
- pmc->mca_sources = psf;
- }
- psf->sf_count[sfmode]++;
- return 0;
- }
- static void sf_markstate(struct ifmcaddr6 *pmc)
- {
- struct ip6_sf_list *psf;
- int mca_xcount = pmc->mca_sfcount[MCAST_EXCLUDE];
- for (psf=pmc->mca_sources; psf; psf=psf->sf_next)
- if (pmc->mca_sfcount[MCAST_EXCLUDE]) {
- psf->sf_oldin = mca_xcount ==
- psf->sf_count[MCAST_EXCLUDE] &&
- !psf->sf_count[MCAST_INCLUDE];
- } else
- psf->sf_oldin = psf->sf_count[MCAST_INCLUDE] != 0;
- }
- static int sf_setstate(struct ifmcaddr6 *pmc)
- {
- struct ip6_sf_list *psf, *dpsf;
- int mca_xcount = pmc->mca_sfcount[MCAST_EXCLUDE];
- int qrv = pmc->idev->mc_qrv;
- int new_in, rv;
- rv = 0;
- for (psf=pmc->mca_sources; psf; psf=psf->sf_next) {
- if (pmc->mca_sfcount[MCAST_EXCLUDE]) {
- new_in = mca_xcount == psf->sf_count[MCAST_EXCLUDE] &&
- !psf->sf_count[MCAST_INCLUDE];
- } else
- new_in = psf->sf_count[MCAST_INCLUDE] != 0;
- if (new_in) {
- if (!psf->sf_oldin) {
- struct ip6_sf_list *prev = NULL;
- for (dpsf=pmc->mca_tomb; dpsf;
- dpsf=dpsf->sf_next) {
- if (ipv6_addr_equal(&dpsf->sf_addr,
- &psf->sf_addr))
- break;
- prev = dpsf;
- }
- if (dpsf) {
- if (prev)
- prev->sf_next = dpsf->sf_next;
- else
- pmc->mca_tomb = dpsf->sf_next;
- kfree(dpsf);
- }
- psf->sf_crcount = qrv;
- rv++;
- }
- } else if (psf->sf_oldin) {
- psf->sf_crcount = 0;
- /*
- * add or update "delete" records if an active filter
- * is now inactive
- */
- for (dpsf=pmc->mca_tomb; dpsf; dpsf=dpsf->sf_next)
- if (ipv6_addr_equal(&dpsf->sf_addr,
- &psf->sf_addr))
- break;
- if (!dpsf) {
- dpsf = kmalloc(sizeof(*dpsf), GFP_ATOMIC);
- if (!dpsf)
- continue;
- *dpsf = *psf;
- /* pmc->mca_lock held by callers */
- dpsf->sf_next = pmc->mca_tomb;
- pmc->mca_tomb = dpsf;
- }
- dpsf->sf_crcount = qrv;
- rv++;
- }
- }
- return rv;
- }
- /*
- * Add multicast source filter list to the interface list
- */
- static int ip6_mc_add_src(struct inet6_dev *idev, const struct in6_addr *pmca,
- int sfmode, int sfcount, const struct in6_addr *psfsrc,
- int delta)
- {
- struct ifmcaddr6 *pmc;
- int isexclude;
- int i, err;
- if (!idev)
- return -ENODEV;
- read_lock_bh(&idev->lock);
- for (pmc=idev->mc_list; pmc; pmc=pmc->next) {
- if (ipv6_addr_equal(pmca, &pmc->mca_addr))
- break;
- }
- if (!pmc) {
- /* MCA not found?? bug */
- read_unlock_bh(&idev->lock);
- return -ESRCH;
- }
- spin_lock_bh(&pmc->mca_lock);
- sf_markstate(pmc);
- isexclude = pmc->mca_sfmode == MCAST_EXCLUDE;
- if (!delta)
- pmc->mca_sfcount[sfmode]++;
- err = 0;
- for (i=0; i<sfcount; i++) {
- err = ip6_mc_add1_src(pmc, sfmode, &psfsrc[i], delta);
- if (err)
- break;
- }
- if (err) {
- int j;
- if (!delta)
- pmc->mca_sfcount[sfmode]--;
- for (j=0; j<i; j++)
- ip6_mc_del1_src(pmc, sfmode, &psfsrc[j]);
- } else if (isexclude != (pmc->mca_sfcount[MCAST_EXCLUDE] != 0)) {
- struct ip6_sf_list *psf;
- /* filter mode change */
- if (pmc->mca_sfcount[MCAST_EXCLUDE])
- pmc->mca_sfmode = MCAST_EXCLUDE;
- else if (pmc->mca_sfcount[MCAST_INCLUDE])
- pmc->mca_sfmode = MCAST_INCLUDE;
- /* else no filters; keep old mode for reports */
- pmc->mca_crcount = idev->mc_qrv;
- idev->mc_ifc_count = pmc->mca_crcount;
- for (psf=pmc->mca_sources; psf; psf = psf->sf_next)
- psf->sf_crcount = 0;
- mld_ifc_event(idev);
- } else if (sf_setstate(pmc))
- mld_ifc_event(idev);
- spin_unlock_bh(&pmc->mca_lock);
- read_unlock_bh(&idev->lock);
- return err;
- }
- static void ip6_mc_clear_src(struct ifmcaddr6 *pmc)
- {
- struct ip6_sf_list *psf, *nextpsf;
- for (psf=pmc->mca_tomb; psf; psf=nextpsf) {
- nextpsf = psf->sf_next;
- kfree(psf);
- }
- pmc->mca_tomb = NULL;
- for (psf=pmc->mca_sources; psf; psf=nextpsf) {
- nextpsf = psf->sf_next;
- kfree(psf);
- }
- pmc->mca_sources = NULL;
- pmc->mca_sfmode = MCAST_EXCLUDE;
- pmc->mca_sfcount[MCAST_INCLUDE] = 0;
- pmc->mca_sfcount[MCAST_EXCLUDE] = 1;
- }
- static void igmp6_join_group(struct ifmcaddr6 *ma)
- {
- unsigned long delay;
- if (ma->mca_flags & MAF_NOREPORT)
- return;
- igmp6_send(&ma->mca_addr, ma->idev->dev, ICMPV6_MGM_REPORT);
- delay = net_random() % IGMP6_UNSOLICITED_IVAL;
- spin_lock_bh(&ma->mca_lock);
- if (del_timer(&ma->mca_timer)) {
- atomic_dec(&ma->mca_refcnt);
- delay = ma->mca_timer.expires - jiffies;
- }
- if (!mod_timer(&ma->mca_timer, jiffies + delay))
- atomic_inc(&ma->mca_refcnt);
- ma->mca_flags |= MAF_TIMER_RUNNING | MAF_LAST_REPORTER;
- spin_unlock_bh(&ma->mca_lock);
- }
- static int ip6_mc_leave_src(struct sock *sk, struct ipv6_mc_socklist *iml,
- struct inet6_dev *idev)
- {
- int err;
- /* callers have the socket lock and a write lock on ipv6_sk_mc_lock,
- * so no other readers or writers of iml or its sflist
- */
- if (!iml->sflist) {
- /* any-source empty exclude case */
- return ip6_mc_del_src(idev, &iml->addr, iml->sfmode, 0, NULL, 0);
- }
- err = ip6_mc_del_src(idev, &iml->addr, iml->sfmode,
- iml->sflist->sl_count, iml->sflist->sl_addr, 0);
- sock_kfree_s(sk, iml->sflist, IP6_SFLSIZE(iml->sflist->sl_max));
- iml->sflist = NULL;
- return err;
- }
- static void igmp6_leave_group(struct ifmcaddr6 *ma)
- {
- if (MLD_V1_SEEN(ma->idev)) {
- if (ma->mca_flags & MAF_LAST_REPORTER)
- igmp6_send(&ma->mca_addr, ma->idev->dev,
- ICMPV6_MGM_REDUCTION);
- } else {
- mld_add_delrec(ma->idev, ma);
- mld_ifc_event(ma->idev);
- }
- }
- static void mld_gq_timer_expire(unsigned long data)
- {
- struct inet6_dev *idev = (struct inet6_dev *)data;
- idev->mc_gq_running = 0;
- mld_send_report(idev, NULL);
- __in6_dev_put(idev);
- }
- static void mld_ifc_timer_expire(unsigned long data)
- {
- struct inet6_dev *idev = (struct inet6_dev *)data;
- mld_send_cr(idev);
- if (idev->mc_ifc_count) {
- idev->mc_ifc_count--;
- if (idev->mc_ifc_count)
- mld_ifc_start_timer(idev, idev->mc_maxdelay);
- }
- __in6_dev_put(idev);
- }
- static void mld_ifc_event(struct inet6_dev *idev)
- {
- if (MLD_V1_SEEN(idev))
- return;
- idev->mc_ifc_count = idev->mc_qrv;
- mld_ifc_start_timer(idev, 1);
- }
- static void igmp6_timer_handler(unsigned long data)
- {
- struct ifmcaddr6 *ma = (struct ifmcaddr6 *) data;
- if (MLD_V1_SEEN(ma->idev))
- igmp6_send(&ma->mca_addr, ma->idev->dev, ICMPV6_MGM_REPORT);
- else
- mld_send_report(ma->idev, ma);
- spin_lock(&ma->mca_lock);
- ma->mca_flags |= MAF_LAST_REPORTER;
- ma->mca_flags &= ~MAF_TIMER_RUNNING;
- spin_unlock(&ma->mca_lock);
- ma_put(ma);
- }
- /* Device changing type */
- void ipv6_mc_unmap(struct inet6_dev *idev)
- {
- struct ifmcaddr6 *i;
- /* Install multicast list, except for all-nodes (already installed) */
- read_lock_bh(&idev->lock);
- for (i = idev->mc_list; i; i = i->next)
- igmp6_group_dropped(i);
- read_unlock_bh(&idev->lock);
- }
- void ipv6_mc_remap(struct inet6_dev *idev)
- {
- ipv6_mc_up(idev);
- }
- /* Device going down */
- void ipv6_mc_down(struct inet6_dev *idev)
- {
- struct ifmcaddr6 *i;
- /* Withdraw multicast list */
- read_lock_bh(&idev->lock);
- idev->mc_ifc_count = 0;
- if (del_timer(&idev->mc_ifc_timer))
- __in6_dev_put(idev);
- idev->mc_gq_running = 0;
- if (del_timer(&idev->mc_gq_timer))
- __in6_dev_put(idev);
- for (i = idev->mc_list; i; i=i->next)
- igmp6_group_dropped(i);
- read_unlock_bh(&idev->lock);
- mld_clear_delrec(idev);
- }
- /* Device going up */
- void ipv6_mc_up(struct inet6_dev *idev)
- {
- struct ifmcaddr6 *i;
- /* Install multicast list, except for all-nodes (already installed) */
- read_lock_bh(&idev->lock);
- for (i = idev->mc_list; i; i=i->next)
- igmp6_group_added(i);
- read_unlock_bh(&idev->lock);
- }
- /* IPv6 device initialization. */
- void ipv6_mc_init_dev(struct inet6_dev *idev)
- {
- write_lock_bh(&idev->lock);
- spin_lock_init(&idev->mc_lock);
- idev->mc_gq_running = 0;
- setup_timer(&idev->mc_gq_timer, mld_gq_timer_expire,
- (unsigned long)idev);
- idev->mc_tomb = NULL;
- idev->mc_ifc_count = 0;
- setup_timer(&idev->mc_ifc_timer, mld_ifc_timer_expire,
- (unsigned long)idev);
- idev->mc_qrv = MLD_QRV_DEFAULT;
- idev->mc_maxdelay = IGMP6_UNSOLICITED_IVAL;
- idev->mc_v1_seen = 0;
- write_unlock_bh(&idev->lock);
- }
- /*
- * Device is about to be destroyed: clean up.
- */
- void ipv6_mc_destroy_dev(struct inet6_dev *idev)
- {
- struct ifmcaddr6 *i;
- /* Deactivate timers */
- ipv6_mc_down(idev);
- /* Delete all-nodes address. */
- /* We cannot call ipv6_dev_mc_dec() directly, our caller in
- * addrconf.c has NULL'd out dev->ip6_ptr so in6_dev_get() will
- * fail.
- */
- __ipv6_dev_mc_dec(idev, &in6addr_linklocal_allnodes);
- if (idev->cnf.forwarding)
- __ipv6_dev_mc_dec(idev, &in6addr_linklocal_allrouters);
- write_lock_bh(&idev->lock);
- while ((i = idev->mc_list) != NULL) {
- idev->mc_list = i->next;
- write_unlock_bh(&idev->lock);
- igmp6_group_dropped(i);
- ma_put(i);
- write_lock_bh(&idev->lock);
- }
- write_unlock_bh(&idev->lock);
- }
- #ifdef CONFIG_PROC_FS
- struct igmp6_mc_iter_state {
- struct seq_net_private p;
- struct net_device *dev;
- struct inet6_dev *idev;
- };
- #define igmp6_mc_seq_private(seq) ((struct igmp6_mc_iter_state *)(seq)->private)
- static inline struct ifmcaddr6 *igmp6_mc_get_first(struct seq_file *seq)
- {
- struct ifmcaddr6 *im = NULL;
- struct igmp6_mc_iter_state *state = igmp6_mc_seq_private(seq);
- struct net *net = seq_file_net(seq);
- state->idev = NULL;
- for_each_netdev_rcu(net, state->dev) {
- struct inet6_dev *idev;
- idev = __in6_dev_get(state->dev);
- if (!idev)
- continue;
- read_lock_bh(&idev->lock);
- im = idev->mc_list;
- if (im) {
- state->idev = idev;
- break;
- }
- read_unlock_bh(&idev->lock);
- }
- return im;
- }
- static struct ifmcaddr6 *igmp6_mc_get_next(struct seq_file *seq, struct ifmcaddr6 *im)
- {
- struct igmp6_mc_iter_state *state = igmp6_mc_seq_private(seq);
- im = im->next;
- while (!im) {
- if (likely(state->idev != NULL))
- read_unlock_bh(&state->idev->lock);
- state->dev = next_net_device_rcu(state->dev);
- if (!state->dev) {
- state->idev = NULL;
- break;
- }
- state->idev = __in6_dev_get(state->dev);
- if (!state->idev)
- continue;
- read_lock_bh(&state->idev->lock);
- im = state->idev->mc_list;
- }
- return im;
- }
- static struct ifmcaddr6 *igmp6_mc_get_idx(struct seq_file *seq, loff_t pos)
- {
- struct ifmcaddr6 *im = igmp6_mc_get_first(seq);
- if (im)
- while (pos && (im = igmp6_mc_get_next(seq, im)) != NULL)
- --pos;
- return pos ? NULL : im;
- }
- static void *igmp6_mc_seq_start(struct seq_file *seq, loff_t *pos)
- __acquires(RCU)
- {
- rcu_read_lock();
- return igmp6_mc_get_idx(seq, *pos);
- }
- static void *igmp6_mc_seq_next(struct seq_file *seq, void *v, loff_t *pos)
- {
- struct ifmcaddr6 *im = igmp6_mc_get_next(seq, v);
- ++*pos;
- return im;
- }
- static void igmp6_mc_seq_stop(struct seq_file *seq, void *v)
- __releases(RCU)
- {
- struct igmp6_mc_iter_state *state = igmp6_mc_seq_private(seq);
- if (likely(state->idev != NULL)) {
- read_unlock_bh(&state->idev->lock);
- state->idev = NULL;
- }
- state->dev = NULL;
- rcu_read_unlock();
- }
- static int igmp6_mc_seq_show(struct seq_file *seq, void *v)
- {
- struct ifmcaddr6 *im = (struct ifmcaddr6 *)v;
- struct igmp6_mc_iter_state *state = igmp6_mc_seq_private(seq);
- seq_printf(seq,
- "%-4d %-15s %pi6 %5d %08X %ld\n",
- state->dev->ifindex, state->dev->name,
- &im->mca_addr,
- im->mca_users, im->mca_flags,
- (im->mca_flags&MAF_TIMER_RUNNING) ?
- jiffies_to_clock_t(im->mca_timer.expires-jiffies) : 0);
- return 0;
- }
- static const struct seq_operations igmp6_mc_seq_ops = {
- .start = igmp6_mc_seq_start,
- .next = igmp6_mc_seq_next,
- .stop = igmp6_mc_seq_stop,
- .show = igmp6_mc_seq_show,
- };
- static int igmp6_mc_seq_open(struct inode *inode, struct file *file)
- {
- return seq_open_net(inode, file, &igmp6_mc_seq_ops,
- sizeof(struct igmp6_mc_iter_state));
- }
- static const struct file_operations igmp6_mc_seq_fops = {
- .owner = THIS_MODULE,
- .open = igmp6_mc_seq_open,
- .read = seq_read,
- .llseek = seq_lseek,
- .release = seq_release_net,
- };
- struct igmp6_mcf_iter_state {
- struct seq_net_private p;
- struct net_device *dev;
- struct inet6_dev *idev;
- struct ifmcaddr6 *im;
- };
- #define igmp6_mcf_seq_private(seq) ((struct igmp6_mcf_iter_state *)(seq)->private)
- static inline struct ip6_sf_list *igmp6_mcf_get_first(struct seq_file *seq)
- {
- struct ip6_sf_list *psf = NULL;
- struct ifmcaddr6 *im = NULL;
- struct igmp6_mcf_iter_state *state = igmp6_mcf_seq_private(seq);
- struct net *net = seq_file_net(seq);
- state->idev = NULL;
- state->im = NULL;
- for_each_netdev_rcu(net, state->dev) {
- struct inet6_dev *idev;
- idev = __in6_dev_get(state->dev);
- if (unlikely(idev == NULL))
- continue;
- read_lock_bh(&idev->lock);
- im = idev->mc_list;
- if (likely(im != NULL)) {
- spin_lock_bh(&im->mca_lock);
- psf = im->mca_sources;
- if (likely(psf != NULL)) {
- state->im = im;
- state->idev = idev;
- break;
- }
- spin_unlock_bh(&im->mca_lock);
- }
- read_unlock_bh(&idev->lock);
- }
- return psf;
- }
- static struct ip6_sf_list *igmp6_mcf_get_next(struct seq_file *seq, struct ip6_sf_list *psf)
- {
- struct igmp6_mcf_iter_state *state = igmp6_mcf_seq_private(seq);
- psf = psf->sf_next;
- while (!psf) {
- spin_unlock_bh(&state->im->mca_lock);
- state->im = state->im->next;
- while (!state->im) {
- if (likely(state->idev != NULL))
- read_unlock_bh(&state->idev->lock);
- state->dev = next_net_device_rcu(state->dev);
- if (!state->dev) {
- state->idev = NULL;
- goto out;
- }
- state->idev = __in6_dev_get(state->dev);
- if (!state->idev)
- continue;
- read_lock_bh(&state->idev->lock);
- state->im = state->idev->mc_list;
- }
- if (!state->im)
- break;
- spin_lock_bh(&state->im->mca_lock);
- psf = state->im->mca_sources;
- }
- out:
- return psf;
- }
- static struct ip6_sf_list *igmp6_mcf_get_idx(struct seq_file *seq, loff_t pos)
- {
- struct ip6_sf_list *psf = igmp6_mcf_get_first(seq);
- if (psf)
- while (pos && (psf = igmp6_mcf_get_next(seq, psf)) != NULL)
- --pos;
- return pos ? NULL : psf;
- }
- static void *igmp6_mcf_seq_start(struct seq_file *seq, loff_t *pos)
- __acquires(RCU)
- {
- rcu_read_lock();
- return *pos ? igmp6_mcf_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
- }
- static void *igmp6_mcf_seq_next(struct seq_file *seq, void *v, loff_t *pos)
- {
- struct ip6_sf_list *psf;
- if (v == SEQ_START_TOKEN)
- psf = igmp6_mcf_get_first(seq);
- else
- psf = igmp6_mcf_get_next(seq, v);
- ++*pos;
- return psf;
- }
- static void igmp6_mcf_seq_stop(struct seq_file *seq, void *v)
- __releases(RCU)
- {
- struct igmp6_mcf_iter_state *state = igmp6_mcf_seq_private(seq);
- if (likely(state->im != NULL)) {
- spin_unlock_bh(&state->im->mca_lock);
- state->im = NULL;
- }
- if (likely(state->idev != NULL)) {
- read_unlock_bh(&state->idev->lock);
- state->idev = NULL;
- }
- state->dev = NULL;
- rcu_read_unlock();
- }
- static int igmp6_mcf_seq_show(struct seq_file *seq, void *v)
- {
- struct ip6_sf_list *psf = (struct ip6_sf_list *)v;
- struct igmp6_mcf_iter_state *state = igmp6_mcf_seq_private(seq);
- if (v == SEQ_START_TOKEN) {
- seq_printf(seq,
- "%3s %6s "
- "%32s %32s %6s %6s\n", "Idx",
- "Device", "Multicast Address",
- "Source Address", "INC", "EXC");
- } else {
- seq_printf(seq,
- "%3d %6.6s %pi6 %pi6 %6lu %6lu\n",
- state->dev->ifindex, state->dev->name,
- &state->im->mca_addr,
- &psf->sf_addr,
- psf->sf_count[MCAST_INCLUDE],
- psf->sf_count[MCAST_EXCLUDE]);
- }
- return 0;
- }
- static const struct seq_operations igmp6_mcf_seq_ops = {
- .start = igmp6_mcf_seq_start,
- .next = igmp6_mcf_seq_next,
- .stop = igmp6_mcf_seq_stop,
- .show = igmp6_mcf_seq_show,
- };
- static int igmp6_mcf_seq_open(struct inode *inode, struct file *file)
- {
- return seq_open_net(inode, file, &igmp6_mcf_seq_ops,
- sizeof(struct igmp6_mcf_iter_state));
- }
- static const struct file_operations igmp6_mcf_seq_fops = {
- .owner = THIS_MODULE,
- .open = igmp6_mcf_seq_open,
- .read = seq_read,
- .llseek = seq_lseek,
- .release = seq_release_net,
- };
- static int __net_init igmp6_proc_init(struct net *net)
- {
- int err;
- err = -ENOMEM;
- if (!proc_net_fops_create(net, "igmp6", S_IRUGO, &igmp6_mc_seq_fops))
- goto out;
- if (!proc_net_fops_create(net, "mcfilter6", S_IRUGO,
- &igmp6_mcf_seq_fops))
- goto out_proc_net_igmp6;
- err = 0;
- out:
- return err;
- out_proc_net_igmp6:
- proc_net_remove(net, "igmp6");
- goto out;
- }
- static void __net_exit igmp6_proc_exit(struct net *net)
- {
- proc_net_remove(net, "mcfilter6");
- proc_net_remove(net, "igmp6");
- }
- #else
- static inline int igmp6_proc_init(struct net *net)
- {
- return 0;
- }
- static inline void igmp6_proc_exit(struct net *net)
- {
- }
- #endif
- static int __net_init igmp6_net_init(struct net *net)
- {
- int err;
- err = inet_ctl_sock_create(&net->ipv6.igmp_sk, PF_INET6,
- SOCK_RAW, IPPROTO_ICMPV6, net);
- if (err < 0) {
- printk(KERN_ERR
- "Failed to initialize the IGMP6 control socket (err %d).\n",
- err);
- goto out;
- }
- inet6_sk(net->ipv6.igmp_sk)->hop_limit = 1;
- err = igmp6_proc_init(net);
- if (err)
- goto out_sock_create;
- out:
- return err;
- out_sock_create:
- inet_ctl_sock_destroy(net->ipv6.igmp_sk);
- goto out;
- }
- static void __net_exit igmp6_net_exit(struct net *net)
- {
- inet_ctl_sock_destroy(net->ipv6.igmp_sk);
- igmp6_proc_exit(net);
- }
- static struct pernet_operations igmp6_net_ops = {
- .init = igmp6_net_init,
- .exit = igmp6_net_exit,
- };
- int __init igmp6_init(void)
- {
- return register_pernet_subsys(&igmp6_net_ops);
- }
- void igmp6_cleanup(void)
- {
- unregister_pernet_subsys(&igmp6_net_ops);
- }
|