ipmr.c 64 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790
  1. /*
  2. * IP multicast routing support for mrouted 3.6/3.8
  3. *
  4. * (c) 1995 Alan Cox, <alan@lxorguk.ukuu.org.uk>
  5. * Linux Consultancy and Custom Driver Development
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License
  9. * as published by the Free Software Foundation; either version
  10. * 2 of the License, or (at your option) any later version.
  11. *
  12. * Fixes:
  13. * Michael Chastain : Incorrect size of copying.
  14. * Alan Cox : Added the cache manager code
  15. * Alan Cox : Fixed the clone/copy bug and device race.
  16. * Mike McLagan : Routing by source
  17. * Malcolm Beattie : Buffer handling fixes.
  18. * Alexey Kuznetsov : Double buffer free and other fixes.
  19. * SVR Anand : Fixed several multicast bugs and problems.
  20. * Alexey Kuznetsov : Status, optimisations and more.
  21. * Brad Parker : Better behaviour on mrouted upcall
  22. * overflow.
  23. * Carlos Picoto : PIMv1 Support
  24. * Pavlin Ivanov Radoslavov: PIMv2 Registers must checksum only PIM header
  25. * Relax this requirement to work with older peers.
  26. *
  27. */
  28. #include <asm/uaccess.h>
  29. #include <linux/types.h>
  30. #include <linux/capability.h>
  31. #include <linux/errno.h>
  32. #include <linux/timer.h>
  33. #include <linux/mm.h>
  34. #include <linux/kernel.h>
  35. #include <linux/fcntl.h>
  36. #include <linux/stat.h>
  37. #include <linux/socket.h>
  38. #include <linux/in.h>
  39. #include <linux/inet.h>
  40. #include <linux/netdevice.h>
  41. #include <linux/inetdevice.h>
  42. #include <linux/igmp.h>
  43. #include <linux/proc_fs.h>
  44. #include <linux/seq_file.h>
  45. #include <linux/mroute.h>
  46. #include <linux/init.h>
  47. #include <linux/if_ether.h>
  48. #include <linux/slab.h>
  49. #include <net/net_namespace.h>
  50. #include <net/ip.h>
  51. #include <net/protocol.h>
  52. #include <linux/skbuff.h>
  53. #include <net/route.h>
  54. #include <net/sock.h>
  55. #include <net/icmp.h>
  56. #include <net/udp.h>
  57. #include <net/raw.h>
  58. #include <linux/notifier.h>
  59. #include <linux/if_arp.h>
  60. #include <linux/netfilter_ipv4.h>
  61. #include <linux/compat.h>
  62. #include <linux/export.h>
  63. #include <net/ip_tunnels.h>
  64. #include <net/checksum.h>
  65. #include <net/netlink.h>
  66. #include <net/fib_rules.h>
  67. #include <linux/netconf.h>
  68. #include <linux/nospec.h>
  69. #if defined(CONFIG_IP_PIMSM_V1) || defined(CONFIG_IP_PIMSM_V2)
  70. #define CONFIG_IP_PIMSM 1
  71. #endif
  72. struct mr_table {
  73. struct list_head list;
  74. possible_net_t net;
  75. u32 id;
  76. struct sock __rcu *mroute_sk;
  77. struct timer_list ipmr_expire_timer;
  78. struct list_head mfc_unres_queue;
  79. struct list_head mfc_cache_array[MFC_LINES];
  80. struct vif_device vif_table[MAXVIFS];
  81. int maxvif;
  82. atomic_t cache_resolve_queue_len;
  83. bool mroute_do_assert;
  84. bool mroute_do_pim;
  85. #if defined(CONFIG_IP_PIMSM_V1) || defined(CONFIG_IP_PIMSM_V2)
  86. int mroute_reg_vif_num;
  87. #endif
  88. };
  89. struct ipmr_rule {
  90. struct fib_rule common;
  91. };
  92. struct ipmr_result {
  93. struct mr_table *mrt;
  94. };
  95. /* Big lock, protecting vif table, mrt cache and mroute socket state.
  96. * Note that the changes are semaphored via rtnl_lock.
  97. */
  98. static DEFINE_RWLOCK(mrt_lock);
  99. /*
  100. * Multicast router control variables
  101. */
  102. #define VIF_EXISTS(_mrt, _idx) ((_mrt)->vif_table[_idx].dev != NULL)
  103. /* Special spinlock for queue of unresolved entries */
  104. static DEFINE_SPINLOCK(mfc_unres_lock);
  105. /* We return to original Alan's scheme. Hash table of resolved
  106. * entries is changed only in process context and protected
  107. * with weak lock mrt_lock. Queue of unresolved entries is protected
  108. * with strong spinlock mfc_unres_lock.
  109. *
  110. * In this case data path is free of exclusive locks at all.
  111. */
  112. static struct kmem_cache *mrt_cachep __read_mostly;
  113. static struct mr_table *ipmr_new_table(struct net *net, u32 id);
  114. static void ipmr_free_table(struct mr_table *mrt);
  115. static void ip_mr_forward(struct net *net, struct mr_table *mrt,
  116. struct sk_buff *skb, struct mfc_cache *cache,
  117. int local);
  118. static int ipmr_cache_report(struct mr_table *mrt,
  119. struct sk_buff *pkt, vifi_t vifi, int assert);
  120. static int __ipmr_fill_mroute(struct mr_table *mrt, struct sk_buff *skb,
  121. struct mfc_cache *c, struct rtmsg *rtm);
  122. static void mroute_netlink_event(struct mr_table *mrt, struct mfc_cache *mfc,
  123. int cmd);
  124. static void mroute_clean_tables(struct mr_table *mrt, bool all);
  125. static void ipmr_expire_process(unsigned long arg);
  126. #ifdef CONFIG_IP_MROUTE_MULTIPLE_TABLES
  127. #define ipmr_for_each_table(mrt, net) \
  128. list_for_each_entry_rcu(mrt, &net->ipv4.mr_tables, list)
  129. static struct mr_table *ipmr_get_table(struct net *net, u32 id)
  130. {
  131. struct mr_table *mrt;
  132. ipmr_for_each_table(mrt, net) {
  133. if (mrt->id == id)
  134. return mrt;
  135. }
  136. return NULL;
  137. }
  138. static int ipmr_fib_lookup(struct net *net, struct flowi4 *flp4,
  139. struct mr_table **mrt)
  140. {
  141. int err;
  142. struct ipmr_result res;
  143. struct fib_lookup_arg arg = {
  144. .result = &res,
  145. .flags = FIB_LOOKUP_NOREF,
  146. };
  147. err = fib_rules_lookup(net->ipv4.mr_rules_ops,
  148. flowi4_to_flowi(flp4), 0, &arg);
  149. if (err < 0)
  150. return err;
  151. *mrt = res.mrt;
  152. return 0;
  153. }
  154. static int ipmr_rule_action(struct fib_rule *rule, struct flowi *flp,
  155. int flags, struct fib_lookup_arg *arg)
  156. {
  157. struct ipmr_result *res = arg->result;
  158. struct mr_table *mrt;
  159. switch (rule->action) {
  160. case FR_ACT_TO_TBL:
  161. break;
  162. case FR_ACT_UNREACHABLE:
  163. return -ENETUNREACH;
  164. case FR_ACT_PROHIBIT:
  165. return -EACCES;
  166. case FR_ACT_BLACKHOLE:
  167. default:
  168. return -EINVAL;
  169. }
  170. mrt = ipmr_get_table(rule->fr_net, rule->table);
  171. if (!mrt)
  172. return -EAGAIN;
  173. res->mrt = mrt;
  174. return 0;
  175. }
  176. static int ipmr_rule_match(struct fib_rule *rule, struct flowi *fl, int flags)
  177. {
  178. return 1;
  179. }
  180. static const struct nla_policy ipmr_rule_policy[FRA_MAX + 1] = {
  181. FRA_GENERIC_POLICY,
  182. };
  183. static int ipmr_rule_configure(struct fib_rule *rule, struct sk_buff *skb,
  184. struct fib_rule_hdr *frh, struct nlattr **tb)
  185. {
  186. return 0;
  187. }
  188. static int ipmr_rule_compare(struct fib_rule *rule, struct fib_rule_hdr *frh,
  189. struct nlattr **tb)
  190. {
  191. return 1;
  192. }
  193. static int ipmr_rule_fill(struct fib_rule *rule, struct sk_buff *skb,
  194. struct fib_rule_hdr *frh)
  195. {
  196. frh->dst_len = 0;
  197. frh->src_len = 0;
  198. frh->tos = 0;
  199. return 0;
  200. }
  201. static const struct fib_rules_ops __net_initconst ipmr_rules_ops_template = {
  202. .family = RTNL_FAMILY_IPMR,
  203. .rule_size = sizeof(struct ipmr_rule),
  204. .addr_size = sizeof(u32),
  205. .action = ipmr_rule_action,
  206. .match = ipmr_rule_match,
  207. .configure = ipmr_rule_configure,
  208. .compare = ipmr_rule_compare,
  209. .fill = ipmr_rule_fill,
  210. .nlgroup = RTNLGRP_IPV4_RULE,
  211. .policy = ipmr_rule_policy,
  212. .owner = THIS_MODULE,
  213. };
  214. static int __net_init ipmr_rules_init(struct net *net)
  215. {
  216. struct fib_rules_ops *ops;
  217. struct mr_table *mrt;
  218. int err;
  219. ops = fib_rules_register(&ipmr_rules_ops_template, net);
  220. if (IS_ERR(ops))
  221. return PTR_ERR(ops);
  222. INIT_LIST_HEAD(&net->ipv4.mr_tables);
  223. mrt = ipmr_new_table(net, RT_TABLE_DEFAULT);
  224. if (!mrt) {
  225. err = -ENOMEM;
  226. goto err1;
  227. }
  228. err = fib_default_rule_add(ops, 0x7fff, RT_TABLE_DEFAULT, 0);
  229. if (err < 0)
  230. goto err2;
  231. net->ipv4.mr_rules_ops = ops;
  232. return 0;
  233. err2:
  234. ipmr_free_table(mrt);
  235. err1:
  236. fib_rules_unregister(ops);
  237. return err;
  238. }
  239. static void __net_exit ipmr_rules_exit(struct net *net)
  240. {
  241. struct mr_table *mrt, *next;
  242. rtnl_lock();
  243. list_for_each_entry_safe(mrt, next, &net->ipv4.mr_tables, list) {
  244. list_del(&mrt->list);
  245. ipmr_free_table(mrt);
  246. }
  247. fib_rules_unregister(net->ipv4.mr_rules_ops);
  248. rtnl_unlock();
  249. }
  250. #else
  251. #define ipmr_for_each_table(mrt, net) \
  252. for (mrt = net->ipv4.mrt; mrt; mrt = NULL)
  253. static struct mr_table *ipmr_get_table(struct net *net, u32 id)
  254. {
  255. return net->ipv4.mrt;
  256. }
  257. static int ipmr_fib_lookup(struct net *net, struct flowi4 *flp4,
  258. struct mr_table **mrt)
  259. {
  260. *mrt = net->ipv4.mrt;
  261. return 0;
  262. }
  263. static int __net_init ipmr_rules_init(struct net *net)
  264. {
  265. net->ipv4.mrt = ipmr_new_table(net, RT_TABLE_DEFAULT);
  266. return net->ipv4.mrt ? 0 : -ENOMEM;
  267. }
  268. static void __net_exit ipmr_rules_exit(struct net *net)
  269. {
  270. rtnl_lock();
  271. ipmr_free_table(net->ipv4.mrt);
  272. net->ipv4.mrt = NULL;
  273. rtnl_unlock();
  274. }
  275. #endif
  276. static struct mr_table *ipmr_new_table(struct net *net, u32 id)
  277. {
  278. struct mr_table *mrt;
  279. unsigned int i;
  280. mrt = ipmr_get_table(net, id);
  281. if (mrt)
  282. return mrt;
  283. mrt = kzalloc(sizeof(*mrt), GFP_KERNEL);
  284. if (!mrt)
  285. return NULL;
  286. write_pnet(&mrt->net, net);
  287. mrt->id = id;
  288. /* Forwarding cache */
  289. for (i = 0; i < MFC_LINES; i++)
  290. INIT_LIST_HEAD(&mrt->mfc_cache_array[i]);
  291. INIT_LIST_HEAD(&mrt->mfc_unres_queue);
  292. setup_timer(&mrt->ipmr_expire_timer, ipmr_expire_process,
  293. (unsigned long)mrt);
  294. #ifdef CONFIG_IP_PIMSM
  295. mrt->mroute_reg_vif_num = -1;
  296. #endif
  297. #ifdef CONFIG_IP_MROUTE_MULTIPLE_TABLES
  298. list_add_tail_rcu(&mrt->list, &net->ipv4.mr_tables);
  299. #endif
  300. return mrt;
  301. }
  302. static void ipmr_free_table(struct mr_table *mrt)
  303. {
  304. del_timer_sync(&mrt->ipmr_expire_timer);
  305. mroute_clean_tables(mrt, true);
  306. kfree(mrt);
  307. }
  308. /* Service routines creating virtual interfaces: DVMRP tunnels and PIMREG */
  309. static void ipmr_del_tunnel(struct net_device *dev, struct vifctl *v)
  310. {
  311. struct net *net = dev_net(dev);
  312. dev_close(dev);
  313. dev = __dev_get_by_name(net, "tunl0");
  314. if (dev) {
  315. const struct net_device_ops *ops = dev->netdev_ops;
  316. struct ifreq ifr;
  317. struct ip_tunnel_parm p;
  318. memset(&p, 0, sizeof(p));
  319. p.iph.daddr = v->vifc_rmt_addr.s_addr;
  320. p.iph.saddr = v->vifc_lcl_addr.s_addr;
  321. p.iph.version = 4;
  322. p.iph.ihl = 5;
  323. p.iph.protocol = IPPROTO_IPIP;
  324. sprintf(p.name, "dvmrp%d", v->vifc_vifi);
  325. ifr.ifr_ifru.ifru_data = (__force void __user *)&p;
  326. if (ops->ndo_do_ioctl) {
  327. mm_segment_t oldfs = get_fs();
  328. set_fs(KERNEL_DS);
  329. ops->ndo_do_ioctl(dev, &ifr, SIOCDELTUNNEL);
  330. set_fs(oldfs);
  331. }
  332. }
  333. }
  334. static
  335. struct net_device *ipmr_new_tunnel(struct net *net, struct vifctl *v)
  336. {
  337. struct net_device *dev;
  338. dev = __dev_get_by_name(net, "tunl0");
  339. if (dev) {
  340. const struct net_device_ops *ops = dev->netdev_ops;
  341. int err;
  342. struct ifreq ifr;
  343. struct ip_tunnel_parm p;
  344. struct in_device *in_dev;
  345. memset(&p, 0, sizeof(p));
  346. p.iph.daddr = v->vifc_rmt_addr.s_addr;
  347. p.iph.saddr = v->vifc_lcl_addr.s_addr;
  348. p.iph.version = 4;
  349. p.iph.ihl = 5;
  350. p.iph.protocol = IPPROTO_IPIP;
  351. sprintf(p.name, "dvmrp%d", v->vifc_vifi);
  352. ifr.ifr_ifru.ifru_data = (__force void __user *)&p;
  353. if (ops->ndo_do_ioctl) {
  354. mm_segment_t oldfs = get_fs();
  355. set_fs(KERNEL_DS);
  356. err = ops->ndo_do_ioctl(dev, &ifr, SIOCADDTUNNEL);
  357. set_fs(oldfs);
  358. } else {
  359. err = -EOPNOTSUPP;
  360. }
  361. dev = NULL;
  362. if (err == 0 &&
  363. (dev = __dev_get_by_name(net, p.name)) != NULL) {
  364. dev->flags |= IFF_MULTICAST;
  365. in_dev = __in_dev_get_rtnl(dev);
  366. if (!in_dev)
  367. goto failure;
  368. ipv4_devconf_setall(in_dev);
  369. neigh_parms_data_state_setall(in_dev->arp_parms);
  370. IPV4_DEVCONF(in_dev->cnf, RP_FILTER) = 0;
  371. if (dev_open(dev))
  372. goto failure;
  373. dev_hold(dev);
  374. }
  375. }
  376. return dev;
  377. failure:
  378. unregister_netdevice(dev);
  379. return NULL;
  380. }
  381. #ifdef CONFIG_IP_PIMSM
  382. static netdev_tx_t reg_vif_xmit(struct sk_buff *skb, struct net_device *dev)
  383. {
  384. struct net *net = dev_net(dev);
  385. struct mr_table *mrt;
  386. struct flowi4 fl4 = {
  387. .flowi4_oif = dev->ifindex,
  388. .flowi4_iif = skb->skb_iif ? : LOOPBACK_IFINDEX,
  389. .flowi4_mark = skb->mark,
  390. };
  391. int err;
  392. err = ipmr_fib_lookup(net, &fl4, &mrt);
  393. if (err < 0) {
  394. kfree_skb(skb);
  395. return err;
  396. }
  397. read_lock(&mrt_lock);
  398. dev->stats.tx_bytes += skb->len;
  399. dev->stats.tx_packets++;
  400. ipmr_cache_report(mrt, skb, mrt->mroute_reg_vif_num, IGMPMSG_WHOLEPKT);
  401. read_unlock(&mrt_lock);
  402. kfree_skb(skb);
  403. return NETDEV_TX_OK;
  404. }
  405. static int reg_vif_get_iflink(const struct net_device *dev)
  406. {
  407. return 0;
  408. }
  409. static const struct net_device_ops reg_vif_netdev_ops = {
  410. .ndo_start_xmit = reg_vif_xmit,
  411. .ndo_get_iflink = reg_vif_get_iflink,
  412. };
  413. static void reg_vif_setup(struct net_device *dev)
  414. {
  415. dev->type = ARPHRD_PIMREG;
  416. dev->mtu = ETH_DATA_LEN - sizeof(struct iphdr) - 8;
  417. dev->flags = IFF_NOARP;
  418. dev->netdev_ops = &reg_vif_netdev_ops;
  419. dev->destructor = free_netdev;
  420. dev->features |= NETIF_F_NETNS_LOCAL;
  421. }
  422. static struct net_device *ipmr_reg_vif(struct net *net, struct mr_table *mrt)
  423. {
  424. struct net_device *dev;
  425. struct in_device *in_dev;
  426. char name[IFNAMSIZ];
  427. if (mrt->id == RT_TABLE_DEFAULT)
  428. sprintf(name, "pimreg");
  429. else
  430. sprintf(name, "pimreg%u", mrt->id);
  431. dev = alloc_netdev(0, name, NET_NAME_UNKNOWN, reg_vif_setup);
  432. if (!dev)
  433. return NULL;
  434. dev_net_set(dev, net);
  435. if (register_netdevice(dev)) {
  436. free_netdev(dev);
  437. return NULL;
  438. }
  439. rcu_read_lock();
  440. in_dev = __in_dev_get_rcu(dev);
  441. if (!in_dev) {
  442. rcu_read_unlock();
  443. goto failure;
  444. }
  445. ipv4_devconf_setall(in_dev);
  446. neigh_parms_data_state_setall(in_dev->arp_parms);
  447. IPV4_DEVCONF(in_dev->cnf, RP_FILTER) = 0;
  448. rcu_read_unlock();
  449. if (dev_open(dev))
  450. goto failure;
  451. dev_hold(dev);
  452. return dev;
  453. failure:
  454. unregister_netdevice(dev);
  455. return NULL;
  456. }
  457. #endif
  458. /**
  459. * vif_delete - Delete a VIF entry
  460. * @notify: Set to 1, if the caller is a notifier_call
  461. */
  462. static int vif_delete(struct mr_table *mrt, int vifi, int notify,
  463. struct list_head *head)
  464. {
  465. struct vif_device *v;
  466. struct net_device *dev;
  467. struct in_device *in_dev;
  468. if (vifi < 0 || vifi >= mrt->maxvif)
  469. return -EADDRNOTAVAIL;
  470. v = &mrt->vif_table[vifi];
  471. write_lock_bh(&mrt_lock);
  472. dev = v->dev;
  473. v->dev = NULL;
  474. if (!dev) {
  475. write_unlock_bh(&mrt_lock);
  476. return -EADDRNOTAVAIL;
  477. }
  478. #ifdef CONFIG_IP_PIMSM
  479. if (vifi == mrt->mroute_reg_vif_num)
  480. mrt->mroute_reg_vif_num = -1;
  481. #endif
  482. if (vifi + 1 == mrt->maxvif) {
  483. int tmp;
  484. for (tmp = vifi - 1; tmp >= 0; tmp--) {
  485. if (VIF_EXISTS(mrt, tmp))
  486. break;
  487. }
  488. mrt->maxvif = tmp+1;
  489. }
  490. write_unlock_bh(&mrt_lock);
  491. dev_set_allmulti(dev, -1);
  492. in_dev = __in_dev_get_rtnl(dev);
  493. if (in_dev) {
  494. IPV4_DEVCONF(in_dev->cnf, MC_FORWARDING)--;
  495. inet_netconf_notify_devconf(dev_net(dev),
  496. NETCONFA_MC_FORWARDING,
  497. dev->ifindex, &in_dev->cnf);
  498. ip_rt_multicast_event(in_dev);
  499. }
  500. if (v->flags & (VIFF_TUNNEL | VIFF_REGISTER) && !notify)
  501. unregister_netdevice_queue(dev, head);
  502. dev_put(dev);
  503. return 0;
  504. }
  505. static void ipmr_cache_free_rcu(struct rcu_head *head)
  506. {
  507. struct mfc_cache *c = container_of(head, struct mfc_cache, rcu);
  508. kmem_cache_free(mrt_cachep, c);
  509. }
  510. static inline void ipmr_cache_free(struct mfc_cache *c)
  511. {
  512. call_rcu(&c->rcu, ipmr_cache_free_rcu);
  513. }
  514. /* Destroy an unresolved cache entry, killing queued skbs
  515. * and reporting error to netlink readers.
  516. */
  517. static void ipmr_destroy_unres(struct mr_table *mrt, struct mfc_cache *c)
  518. {
  519. struct net *net = read_pnet(&mrt->net);
  520. struct sk_buff *skb;
  521. struct nlmsgerr *e;
  522. atomic_dec(&mrt->cache_resolve_queue_len);
  523. while ((skb = skb_dequeue(&c->mfc_un.unres.unresolved))) {
  524. if (ip_hdr(skb)->version == 0) {
  525. struct nlmsghdr *nlh = (struct nlmsghdr *)skb_pull(skb, sizeof(struct iphdr));
  526. nlh->nlmsg_type = NLMSG_ERROR;
  527. nlh->nlmsg_len = nlmsg_msg_size(sizeof(struct nlmsgerr));
  528. skb_trim(skb, nlh->nlmsg_len);
  529. e = nlmsg_data(nlh);
  530. e->error = -ETIMEDOUT;
  531. memset(&e->msg, 0, sizeof(e->msg));
  532. rtnl_unicast(skb, net, NETLINK_CB(skb).portid);
  533. } else {
  534. kfree_skb(skb);
  535. }
  536. }
  537. ipmr_cache_free(c);
  538. }
  539. /* Timer process for the unresolved queue. */
  540. static void ipmr_expire_process(unsigned long arg)
  541. {
  542. struct mr_table *mrt = (struct mr_table *)arg;
  543. unsigned long now;
  544. unsigned long expires;
  545. struct mfc_cache *c, *next;
  546. if (!spin_trylock(&mfc_unres_lock)) {
  547. mod_timer(&mrt->ipmr_expire_timer, jiffies+HZ/10);
  548. return;
  549. }
  550. if (list_empty(&mrt->mfc_unres_queue))
  551. goto out;
  552. now = jiffies;
  553. expires = 10*HZ;
  554. list_for_each_entry_safe(c, next, &mrt->mfc_unres_queue, list) {
  555. if (time_after(c->mfc_un.unres.expires, now)) {
  556. unsigned long interval = c->mfc_un.unres.expires - now;
  557. if (interval < expires)
  558. expires = interval;
  559. continue;
  560. }
  561. list_del(&c->list);
  562. mroute_netlink_event(mrt, c, RTM_DELROUTE);
  563. ipmr_destroy_unres(mrt, c);
  564. }
  565. if (!list_empty(&mrt->mfc_unres_queue))
  566. mod_timer(&mrt->ipmr_expire_timer, jiffies + expires);
  567. out:
  568. spin_unlock(&mfc_unres_lock);
  569. }
  570. /* Fill oifs list. It is called under write locked mrt_lock. */
  571. static void ipmr_update_thresholds(struct mr_table *mrt, struct mfc_cache *cache,
  572. unsigned char *ttls)
  573. {
  574. int vifi;
  575. cache->mfc_un.res.minvif = MAXVIFS;
  576. cache->mfc_un.res.maxvif = 0;
  577. memset(cache->mfc_un.res.ttls, 255, MAXVIFS);
  578. for (vifi = 0; vifi < mrt->maxvif; vifi++) {
  579. if (VIF_EXISTS(mrt, vifi) &&
  580. ttls[vifi] && ttls[vifi] < 255) {
  581. cache->mfc_un.res.ttls[vifi] = ttls[vifi];
  582. if (cache->mfc_un.res.minvif > vifi)
  583. cache->mfc_un.res.minvif = vifi;
  584. if (cache->mfc_un.res.maxvif <= vifi)
  585. cache->mfc_un.res.maxvif = vifi + 1;
  586. }
  587. }
  588. }
  589. static int vif_add(struct net *net, struct mr_table *mrt,
  590. struct vifctl *vifc, int mrtsock)
  591. {
  592. int vifi = vifc->vifc_vifi;
  593. struct vif_device *v = &mrt->vif_table[vifi];
  594. struct net_device *dev;
  595. struct in_device *in_dev;
  596. int err;
  597. /* Is vif busy ? */
  598. if (VIF_EXISTS(mrt, vifi))
  599. return -EADDRINUSE;
  600. switch (vifc->vifc_flags) {
  601. #ifdef CONFIG_IP_PIMSM
  602. case VIFF_REGISTER:
  603. /*
  604. * Special Purpose VIF in PIM
  605. * All the packets will be sent to the daemon
  606. */
  607. if (mrt->mroute_reg_vif_num >= 0)
  608. return -EADDRINUSE;
  609. dev = ipmr_reg_vif(net, mrt);
  610. if (!dev)
  611. return -ENOBUFS;
  612. err = dev_set_allmulti(dev, 1);
  613. if (err) {
  614. unregister_netdevice(dev);
  615. dev_put(dev);
  616. return err;
  617. }
  618. break;
  619. #endif
  620. case VIFF_TUNNEL:
  621. dev = ipmr_new_tunnel(net, vifc);
  622. if (!dev)
  623. return -ENOBUFS;
  624. err = dev_set_allmulti(dev, 1);
  625. if (err) {
  626. ipmr_del_tunnel(dev, vifc);
  627. dev_put(dev);
  628. return err;
  629. }
  630. break;
  631. case VIFF_USE_IFINDEX:
  632. case 0:
  633. if (vifc->vifc_flags == VIFF_USE_IFINDEX) {
  634. dev = dev_get_by_index(net, vifc->vifc_lcl_ifindex);
  635. if (dev && !__in_dev_get_rtnl(dev)) {
  636. dev_put(dev);
  637. return -EADDRNOTAVAIL;
  638. }
  639. } else {
  640. dev = ip_dev_find(net, vifc->vifc_lcl_addr.s_addr);
  641. }
  642. if (!dev)
  643. return -EADDRNOTAVAIL;
  644. err = dev_set_allmulti(dev, 1);
  645. if (err) {
  646. dev_put(dev);
  647. return err;
  648. }
  649. break;
  650. default:
  651. return -EINVAL;
  652. }
  653. in_dev = __in_dev_get_rtnl(dev);
  654. if (!in_dev) {
  655. dev_put(dev);
  656. return -EADDRNOTAVAIL;
  657. }
  658. IPV4_DEVCONF(in_dev->cnf, MC_FORWARDING)++;
  659. inet_netconf_notify_devconf(net, NETCONFA_MC_FORWARDING, dev->ifindex,
  660. &in_dev->cnf);
  661. ip_rt_multicast_event(in_dev);
  662. /* Fill in the VIF structures */
  663. v->rate_limit = vifc->vifc_rate_limit;
  664. v->local = vifc->vifc_lcl_addr.s_addr;
  665. v->remote = vifc->vifc_rmt_addr.s_addr;
  666. v->flags = vifc->vifc_flags;
  667. if (!mrtsock)
  668. v->flags |= VIFF_STATIC;
  669. v->threshold = vifc->vifc_threshold;
  670. v->bytes_in = 0;
  671. v->bytes_out = 0;
  672. v->pkt_in = 0;
  673. v->pkt_out = 0;
  674. v->link = dev->ifindex;
  675. if (v->flags & (VIFF_TUNNEL | VIFF_REGISTER))
  676. v->link = dev_get_iflink(dev);
  677. /* And finish update writing critical data */
  678. write_lock_bh(&mrt_lock);
  679. v->dev = dev;
  680. #ifdef CONFIG_IP_PIMSM
  681. if (v->flags & VIFF_REGISTER)
  682. mrt->mroute_reg_vif_num = vifi;
  683. #endif
  684. if (vifi+1 > mrt->maxvif)
  685. mrt->maxvif = vifi+1;
  686. write_unlock_bh(&mrt_lock);
  687. return 0;
  688. }
  689. /* called with rcu_read_lock() */
  690. static struct mfc_cache *ipmr_cache_find(struct mr_table *mrt,
  691. __be32 origin,
  692. __be32 mcastgrp)
  693. {
  694. int line = MFC_HASH(mcastgrp, origin);
  695. struct mfc_cache *c;
  696. list_for_each_entry_rcu(c, &mrt->mfc_cache_array[line], list) {
  697. if (c->mfc_origin == origin && c->mfc_mcastgrp == mcastgrp)
  698. return c;
  699. }
  700. return NULL;
  701. }
  702. /* Look for a (*,*,oif) entry */
  703. static struct mfc_cache *ipmr_cache_find_any_parent(struct mr_table *mrt,
  704. int vifi)
  705. {
  706. int line = MFC_HASH(htonl(INADDR_ANY), htonl(INADDR_ANY));
  707. struct mfc_cache *c;
  708. list_for_each_entry_rcu(c, &mrt->mfc_cache_array[line], list)
  709. if (c->mfc_origin == htonl(INADDR_ANY) &&
  710. c->mfc_mcastgrp == htonl(INADDR_ANY) &&
  711. c->mfc_un.res.ttls[vifi] < 255)
  712. return c;
  713. return NULL;
  714. }
  715. /* Look for a (*,G) entry */
  716. static struct mfc_cache *ipmr_cache_find_any(struct mr_table *mrt,
  717. __be32 mcastgrp, int vifi)
  718. {
  719. int line = MFC_HASH(mcastgrp, htonl(INADDR_ANY));
  720. struct mfc_cache *c, *proxy;
  721. if (mcastgrp == htonl(INADDR_ANY))
  722. goto skip;
  723. list_for_each_entry_rcu(c, &mrt->mfc_cache_array[line], list)
  724. if (c->mfc_origin == htonl(INADDR_ANY) &&
  725. c->mfc_mcastgrp == mcastgrp) {
  726. if (c->mfc_un.res.ttls[vifi] < 255)
  727. return c;
  728. /* It's ok if the vifi is part of the static tree */
  729. proxy = ipmr_cache_find_any_parent(mrt,
  730. c->mfc_parent);
  731. if (proxy && proxy->mfc_un.res.ttls[vifi] < 255)
  732. return c;
  733. }
  734. skip:
  735. return ipmr_cache_find_any_parent(mrt, vifi);
  736. }
  737. /*
  738. * Allocate a multicast cache entry
  739. */
  740. static struct mfc_cache *ipmr_cache_alloc(void)
  741. {
  742. struct mfc_cache *c = kmem_cache_zalloc(mrt_cachep, GFP_KERNEL);
  743. if (c) {
  744. c->mfc_un.res.last_assert = jiffies - MFC_ASSERT_THRESH - 1;
  745. c->mfc_un.res.minvif = MAXVIFS;
  746. }
  747. return c;
  748. }
  749. static struct mfc_cache *ipmr_cache_alloc_unres(void)
  750. {
  751. struct mfc_cache *c = kmem_cache_zalloc(mrt_cachep, GFP_ATOMIC);
  752. if (c) {
  753. skb_queue_head_init(&c->mfc_un.unres.unresolved);
  754. c->mfc_un.unres.expires = jiffies + 10*HZ;
  755. }
  756. return c;
  757. }
  758. /*
  759. * A cache entry has gone into a resolved state from queued
  760. */
  761. static void ipmr_cache_resolve(struct net *net, struct mr_table *mrt,
  762. struct mfc_cache *uc, struct mfc_cache *c)
  763. {
  764. struct sk_buff *skb;
  765. struct nlmsgerr *e;
  766. /* Play the pending entries through our router */
  767. while ((skb = __skb_dequeue(&uc->mfc_un.unres.unresolved))) {
  768. if (ip_hdr(skb)->version == 0) {
  769. struct nlmsghdr *nlh = (struct nlmsghdr *)skb_pull(skb, sizeof(struct iphdr));
  770. if (__ipmr_fill_mroute(mrt, skb, c, nlmsg_data(nlh)) > 0) {
  771. nlh->nlmsg_len = skb_tail_pointer(skb) -
  772. (u8 *)nlh;
  773. } else {
  774. nlh->nlmsg_type = NLMSG_ERROR;
  775. nlh->nlmsg_len = nlmsg_msg_size(sizeof(struct nlmsgerr));
  776. skb_trim(skb, nlh->nlmsg_len);
  777. e = nlmsg_data(nlh);
  778. e->error = -EMSGSIZE;
  779. memset(&e->msg, 0, sizeof(e->msg));
  780. }
  781. rtnl_unicast(skb, net, NETLINK_CB(skb).portid);
  782. } else {
  783. ip_mr_forward(net, mrt, skb, c, 0);
  784. }
  785. }
  786. }
  787. /*
  788. * Bounce a cache query up to mrouted. We could use netlink for this but mrouted
  789. * expects the following bizarre scheme.
  790. *
  791. * Called under mrt_lock.
  792. */
  793. static int ipmr_cache_report(struct mr_table *mrt,
  794. struct sk_buff *pkt, vifi_t vifi, int assert)
  795. {
  796. struct sk_buff *skb;
  797. const int ihl = ip_hdrlen(pkt);
  798. struct igmphdr *igmp;
  799. struct igmpmsg *msg;
  800. struct sock *mroute_sk;
  801. int ret;
  802. #ifdef CONFIG_IP_PIMSM
  803. if (assert == IGMPMSG_WHOLEPKT)
  804. skb = skb_realloc_headroom(pkt, sizeof(struct iphdr));
  805. else
  806. #endif
  807. skb = alloc_skb(128, GFP_ATOMIC);
  808. if (!skb)
  809. return -ENOBUFS;
  810. #ifdef CONFIG_IP_PIMSM
  811. if (assert == IGMPMSG_WHOLEPKT) {
  812. /* Ugly, but we have no choice with this interface.
  813. * Duplicate old header, fix ihl, length etc.
  814. * And all this only to mangle msg->im_msgtype and
  815. * to set msg->im_mbz to "mbz" :-)
  816. */
  817. skb_push(skb, sizeof(struct iphdr));
  818. skb_reset_network_header(skb);
  819. skb_reset_transport_header(skb);
  820. msg = (struct igmpmsg *)skb_network_header(skb);
  821. memcpy(msg, skb_network_header(pkt), sizeof(struct iphdr));
  822. msg->im_msgtype = IGMPMSG_WHOLEPKT;
  823. msg->im_mbz = 0;
  824. msg->im_vif = mrt->mroute_reg_vif_num;
  825. ip_hdr(skb)->ihl = sizeof(struct iphdr) >> 2;
  826. ip_hdr(skb)->tot_len = htons(ntohs(ip_hdr(pkt)->tot_len) +
  827. sizeof(struct iphdr));
  828. } else
  829. #endif
  830. {
  831. /* Copy the IP header */
  832. skb_set_network_header(skb, skb->len);
  833. skb_put(skb, ihl);
  834. skb_copy_to_linear_data(skb, pkt->data, ihl);
  835. ip_hdr(skb)->protocol = 0; /* Flag to the kernel this is a route add */
  836. msg = (struct igmpmsg *)skb_network_header(skb);
  837. msg->im_vif = vifi;
  838. skb_dst_set(skb, dst_clone(skb_dst(pkt)));
  839. /* Add our header */
  840. igmp = (struct igmphdr *)skb_put(skb, sizeof(struct igmphdr));
  841. igmp->type =
  842. msg->im_msgtype = assert;
  843. igmp->code = 0;
  844. ip_hdr(skb)->tot_len = htons(skb->len); /* Fix the length */
  845. skb->transport_header = skb->network_header;
  846. }
  847. rcu_read_lock();
  848. mroute_sk = rcu_dereference(mrt->mroute_sk);
  849. if (!mroute_sk) {
  850. rcu_read_unlock();
  851. kfree_skb(skb);
  852. return -EINVAL;
  853. }
  854. /* Deliver to mrouted */
  855. ret = sock_queue_rcv_skb(mroute_sk, skb);
  856. rcu_read_unlock();
  857. if (ret < 0) {
  858. net_warn_ratelimited("mroute: pending queue full, dropping entries\n");
  859. kfree_skb(skb);
  860. }
  861. return ret;
  862. }
  863. /*
  864. * Queue a packet for resolution. It gets locked cache entry!
  865. */
  866. static int
  867. ipmr_cache_unresolved(struct mr_table *mrt, vifi_t vifi, struct sk_buff *skb)
  868. {
  869. bool found = false;
  870. int err;
  871. struct mfc_cache *c;
  872. const struct iphdr *iph = ip_hdr(skb);
  873. spin_lock_bh(&mfc_unres_lock);
  874. list_for_each_entry(c, &mrt->mfc_unres_queue, list) {
  875. if (c->mfc_mcastgrp == iph->daddr &&
  876. c->mfc_origin == iph->saddr) {
  877. found = true;
  878. break;
  879. }
  880. }
  881. if (!found) {
  882. /* Create a new entry if allowable */
  883. if (atomic_read(&mrt->cache_resolve_queue_len) >= 10 ||
  884. (c = ipmr_cache_alloc_unres()) == NULL) {
  885. spin_unlock_bh(&mfc_unres_lock);
  886. kfree_skb(skb);
  887. return -ENOBUFS;
  888. }
  889. /* Fill in the new cache entry */
  890. c->mfc_parent = -1;
  891. c->mfc_origin = iph->saddr;
  892. c->mfc_mcastgrp = iph->daddr;
  893. /* Reflect first query at mrouted. */
  894. err = ipmr_cache_report(mrt, skb, vifi, IGMPMSG_NOCACHE);
  895. if (err < 0) {
  896. /* If the report failed throw the cache entry
  897. out - Brad Parker
  898. */
  899. spin_unlock_bh(&mfc_unres_lock);
  900. ipmr_cache_free(c);
  901. kfree_skb(skb);
  902. return err;
  903. }
  904. atomic_inc(&mrt->cache_resolve_queue_len);
  905. list_add(&c->list, &mrt->mfc_unres_queue);
  906. mroute_netlink_event(mrt, c, RTM_NEWROUTE);
  907. if (atomic_read(&mrt->cache_resolve_queue_len) == 1)
  908. mod_timer(&mrt->ipmr_expire_timer, c->mfc_un.unres.expires);
  909. }
  910. /* See if we can append the packet */
  911. if (c->mfc_un.unres.unresolved.qlen > 3) {
  912. kfree_skb(skb);
  913. err = -ENOBUFS;
  914. } else {
  915. skb_queue_tail(&c->mfc_un.unres.unresolved, skb);
  916. err = 0;
  917. }
  918. spin_unlock_bh(&mfc_unres_lock);
  919. return err;
  920. }
  921. /*
  922. * MFC cache manipulation by user space mroute daemon
  923. */
  924. static int ipmr_mfc_delete(struct mr_table *mrt, struct mfcctl *mfc, int parent)
  925. {
  926. int line;
  927. struct mfc_cache *c, *next;
  928. line = MFC_HASH(mfc->mfcc_mcastgrp.s_addr, mfc->mfcc_origin.s_addr);
  929. list_for_each_entry_safe(c, next, &mrt->mfc_cache_array[line], list) {
  930. if (c->mfc_origin == mfc->mfcc_origin.s_addr &&
  931. c->mfc_mcastgrp == mfc->mfcc_mcastgrp.s_addr &&
  932. (parent == -1 || parent == c->mfc_parent)) {
  933. list_del_rcu(&c->list);
  934. mroute_netlink_event(mrt, c, RTM_DELROUTE);
  935. ipmr_cache_free(c);
  936. return 0;
  937. }
  938. }
  939. return -ENOENT;
  940. }
  941. static int ipmr_mfc_add(struct net *net, struct mr_table *mrt,
  942. struct mfcctl *mfc, int mrtsock, int parent)
  943. {
  944. bool found = false;
  945. int line;
  946. struct mfc_cache *uc, *c;
  947. if (mfc->mfcc_parent >= MAXVIFS)
  948. return -ENFILE;
  949. line = MFC_HASH(mfc->mfcc_mcastgrp.s_addr, mfc->mfcc_origin.s_addr);
  950. list_for_each_entry(c, &mrt->mfc_cache_array[line], list) {
  951. if (c->mfc_origin == mfc->mfcc_origin.s_addr &&
  952. c->mfc_mcastgrp == mfc->mfcc_mcastgrp.s_addr &&
  953. (parent == -1 || parent == c->mfc_parent)) {
  954. found = true;
  955. break;
  956. }
  957. }
  958. if (found) {
  959. write_lock_bh(&mrt_lock);
  960. c->mfc_parent = mfc->mfcc_parent;
  961. ipmr_update_thresholds(mrt, c, mfc->mfcc_ttls);
  962. if (!mrtsock)
  963. c->mfc_flags |= MFC_STATIC;
  964. write_unlock_bh(&mrt_lock);
  965. mroute_netlink_event(mrt, c, RTM_NEWROUTE);
  966. return 0;
  967. }
  968. if (mfc->mfcc_mcastgrp.s_addr != htonl(INADDR_ANY) &&
  969. !ipv4_is_multicast(mfc->mfcc_mcastgrp.s_addr))
  970. return -EINVAL;
  971. c = ipmr_cache_alloc();
  972. if (!c)
  973. return -ENOMEM;
  974. c->mfc_origin = mfc->mfcc_origin.s_addr;
  975. c->mfc_mcastgrp = mfc->mfcc_mcastgrp.s_addr;
  976. c->mfc_parent = mfc->mfcc_parent;
  977. ipmr_update_thresholds(mrt, c, mfc->mfcc_ttls);
  978. if (!mrtsock)
  979. c->mfc_flags |= MFC_STATIC;
  980. list_add_rcu(&c->list, &mrt->mfc_cache_array[line]);
  981. /*
  982. * Check to see if we resolved a queued list. If so we
  983. * need to send on the frames and tidy up.
  984. */
  985. found = false;
  986. spin_lock_bh(&mfc_unres_lock);
  987. list_for_each_entry(uc, &mrt->mfc_unres_queue, list) {
  988. if (uc->mfc_origin == c->mfc_origin &&
  989. uc->mfc_mcastgrp == c->mfc_mcastgrp) {
  990. list_del(&uc->list);
  991. atomic_dec(&mrt->cache_resolve_queue_len);
  992. found = true;
  993. break;
  994. }
  995. }
  996. if (list_empty(&mrt->mfc_unres_queue))
  997. del_timer(&mrt->ipmr_expire_timer);
  998. spin_unlock_bh(&mfc_unres_lock);
  999. if (found) {
  1000. ipmr_cache_resolve(net, mrt, uc, c);
  1001. ipmr_cache_free(uc);
  1002. }
  1003. mroute_netlink_event(mrt, c, RTM_NEWROUTE);
  1004. return 0;
  1005. }
  1006. /*
  1007. * Close the multicast socket, and clear the vif tables etc
  1008. */
  1009. static void mroute_clean_tables(struct mr_table *mrt, bool all)
  1010. {
  1011. int i;
  1012. LIST_HEAD(list);
  1013. struct mfc_cache *c, *next;
  1014. /* Shut down all active vif entries */
  1015. for (i = 0; i < mrt->maxvif; i++) {
  1016. if (!all && (mrt->vif_table[i].flags & VIFF_STATIC))
  1017. continue;
  1018. vif_delete(mrt, i, 0, &list);
  1019. }
  1020. unregister_netdevice_many(&list);
  1021. /* Wipe the cache */
  1022. for (i = 0; i < MFC_LINES; i++) {
  1023. list_for_each_entry_safe(c, next, &mrt->mfc_cache_array[i], list) {
  1024. if (!all && (c->mfc_flags & MFC_STATIC))
  1025. continue;
  1026. list_del_rcu(&c->list);
  1027. mroute_netlink_event(mrt, c, RTM_DELROUTE);
  1028. ipmr_cache_free(c);
  1029. }
  1030. }
  1031. if (atomic_read(&mrt->cache_resolve_queue_len) != 0) {
  1032. spin_lock_bh(&mfc_unres_lock);
  1033. list_for_each_entry_safe(c, next, &mrt->mfc_unres_queue, list) {
  1034. list_del(&c->list);
  1035. mroute_netlink_event(mrt, c, RTM_DELROUTE);
  1036. ipmr_destroy_unres(mrt, c);
  1037. }
  1038. spin_unlock_bh(&mfc_unres_lock);
  1039. }
  1040. }
  1041. /* called from ip_ra_control(), before an RCU grace period,
  1042. * we dont need to call synchronize_rcu() here
  1043. */
  1044. static void mrtsock_destruct(struct sock *sk)
  1045. {
  1046. struct net *net = sock_net(sk);
  1047. struct mr_table *mrt;
  1048. rtnl_lock();
  1049. ipmr_for_each_table(mrt, net) {
  1050. if (sk == rtnl_dereference(mrt->mroute_sk)) {
  1051. IPV4_DEVCONF_ALL(net, MC_FORWARDING)--;
  1052. inet_netconf_notify_devconf(net, NETCONFA_MC_FORWARDING,
  1053. NETCONFA_IFINDEX_ALL,
  1054. net->ipv4.devconf_all);
  1055. RCU_INIT_POINTER(mrt->mroute_sk, NULL);
  1056. mroute_clean_tables(mrt, false);
  1057. }
  1058. }
  1059. rtnl_unlock();
  1060. }
  1061. /*
  1062. * Socket options and virtual interface manipulation. The whole
  1063. * virtual interface system is a complete heap, but unfortunately
  1064. * that's how BSD mrouted happens to think. Maybe one day with a proper
  1065. * MOSPF/PIM router set up we can clean this up.
  1066. */
  1067. int ip_mroute_setsockopt(struct sock *sk, int optname, char __user *optval, unsigned int optlen)
  1068. {
  1069. int ret, parent = 0;
  1070. struct vifctl vif;
  1071. struct mfcctl mfc;
  1072. struct net *net = sock_net(sk);
  1073. struct mr_table *mrt;
  1074. if (sk->sk_type != SOCK_RAW ||
  1075. inet_sk(sk)->inet_num != IPPROTO_IGMP)
  1076. return -EOPNOTSUPP;
  1077. mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
  1078. if (!mrt)
  1079. return -ENOENT;
  1080. if (optname != MRT_INIT) {
  1081. if (sk != rcu_access_pointer(mrt->mroute_sk) &&
  1082. !ns_capable(net->user_ns, CAP_NET_ADMIN))
  1083. return -EACCES;
  1084. }
  1085. switch (optname) {
  1086. case MRT_INIT:
  1087. if (optlen != sizeof(int))
  1088. return -EINVAL;
  1089. rtnl_lock();
  1090. if (rtnl_dereference(mrt->mroute_sk)) {
  1091. rtnl_unlock();
  1092. return -EADDRINUSE;
  1093. }
  1094. ret = ip_ra_control(sk, 1, mrtsock_destruct);
  1095. if (ret == 0) {
  1096. rcu_assign_pointer(mrt->mroute_sk, sk);
  1097. IPV4_DEVCONF_ALL(net, MC_FORWARDING)++;
  1098. inet_netconf_notify_devconf(net, NETCONFA_MC_FORWARDING,
  1099. NETCONFA_IFINDEX_ALL,
  1100. net->ipv4.devconf_all);
  1101. }
  1102. rtnl_unlock();
  1103. return ret;
  1104. case MRT_DONE:
  1105. if (sk != rcu_access_pointer(mrt->mroute_sk))
  1106. return -EACCES;
  1107. return ip_ra_control(sk, 0, NULL);
  1108. case MRT_ADD_VIF:
  1109. case MRT_DEL_VIF:
  1110. if (optlen != sizeof(vif))
  1111. return -EINVAL;
  1112. if (copy_from_user(&vif, optval, sizeof(vif)))
  1113. return -EFAULT;
  1114. if (vif.vifc_vifi >= MAXVIFS)
  1115. return -ENFILE;
  1116. rtnl_lock();
  1117. if (optname == MRT_ADD_VIF) {
  1118. ret = vif_add(net, mrt, &vif,
  1119. sk == rtnl_dereference(mrt->mroute_sk));
  1120. } else {
  1121. ret = vif_delete(mrt, vif.vifc_vifi, 0, NULL);
  1122. }
  1123. rtnl_unlock();
  1124. return ret;
  1125. /*
  1126. * Manipulate the forwarding caches. These live
  1127. * in a sort of kernel/user symbiosis.
  1128. */
  1129. case MRT_ADD_MFC:
  1130. case MRT_DEL_MFC:
  1131. parent = -1;
  1132. case MRT_ADD_MFC_PROXY:
  1133. case MRT_DEL_MFC_PROXY:
  1134. if (optlen != sizeof(mfc))
  1135. return -EINVAL;
  1136. if (copy_from_user(&mfc, optval, sizeof(mfc)))
  1137. return -EFAULT;
  1138. if (parent == 0)
  1139. parent = mfc.mfcc_parent;
  1140. rtnl_lock();
  1141. if (optname == MRT_DEL_MFC || optname == MRT_DEL_MFC_PROXY)
  1142. ret = ipmr_mfc_delete(mrt, &mfc, parent);
  1143. else
  1144. ret = ipmr_mfc_add(net, mrt, &mfc,
  1145. sk == rtnl_dereference(mrt->mroute_sk),
  1146. parent);
  1147. rtnl_unlock();
  1148. return ret;
  1149. /*
  1150. * Control PIM assert.
  1151. */
  1152. case MRT_ASSERT:
  1153. {
  1154. int v;
  1155. if (optlen != sizeof(v))
  1156. return -EINVAL;
  1157. if (get_user(v, (int __user *)optval))
  1158. return -EFAULT;
  1159. mrt->mroute_do_assert = v;
  1160. return 0;
  1161. }
  1162. #ifdef CONFIG_IP_PIMSM
  1163. case MRT_PIM:
  1164. {
  1165. int v;
  1166. if (optlen != sizeof(v))
  1167. return -EINVAL;
  1168. if (get_user(v, (int __user *)optval))
  1169. return -EFAULT;
  1170. v = !!v;
  1171. rtnl_lock();
  1172. ret = 0;
  1173. if (v != mrt->mroute_do_pim) {
  1174. mrt->mroute_do_pim = v;
  1175. mrt->mroute_do_assert = v;
  1176. }
  1177. rtnl_unlock();
  1178. return ret;
  1179. }
  1180. #endif
  1181. #ifdef CONFIG_IP_MROUTE_MULTIPLE_TABLES
  1182. case MRT_TABLE:
  1183. {
  1184. u32 v;
  1185. if (optlen != sizeof(u32))
  1186. return -EINVAL;
  1187. if (get_user(v, (u32 __user *)optval))
  1188. return -EFAULT;
  1189. /* "pimreg%u" should not exceed 16 bytes (IFNAMSIZ) */
  1190. if (v != RT_TABLE_DEFAULT && v >= 1000000000)
  1191. return -EINVAL;
  1192. rtnl_lock();
  1193. ret = 0;
  1194. if (sk == rtnl_dereference(mrt->mroute_sk)) {
  1195. ret = -EBUSY;
  1196. } else {
  1197. if (!ipmr_new_table(net, v))
  1198. ret = -ENOMEM;
  1199. else
  1200. raw_sk(sk)->ipmr_table = v;
  1201. }
  1202. rtnl_unlock();
  1203. return ret;
  1204. }
  1205. #endif
  1206. /*
  1207. * Spurious command, or MRT_VERSION which you cannot
  1208. * set.
  1209. */
  1210. default:
  1211. return -ENOPROTOOPT;
  1212. }
  1213. }
  1214. /*
  1215. * Getsock opt support for the multicast routing system.
  1216. */
  1217. int ip_mroute_getsockopt(struct sock *sk, int optname, char __user *optval, int __user *optlen)
  1218. {
  1219. int olr;
  1220. int val;
  1221. struct net *net = sock_net(sk);
  1222. struct mr_table *mrt;
  1223. if (sk->sk_type != SOCK_RAW ||
  1224. inet_sk(sk)->inet_num != IPPROTO_IGMP)
  1225. return -EOPNOTSUPP;
  1226. mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
  1227. if (!mrt)
  1228. return -ENOENT;
  1229. if (optname != MRT_VERSION &&
  1230. #ifdef CONFIG_IP_PIMSM
  1231. optname != MRT_PIM &&
  1232. #endif
  1233. optname != MRT_ASSERT)
  1234. return -ENOPROTOOPT;
  1235. if (get_user(olr, optlen))
  1236. return -EFAULT;
  1237. olr = min_t(unsigned int, olr, sizeof(int));
  1238. if (olr < 0)
  1239. return -EINVAL;
  1240. if (put_user(olr, optlen))
  1241. return -EFAULT;
  1242. if (optname == MRT_VERSION)
  1243. val = 0x0305;
  1244. #ifdef CONFIG_IP_PIMSM
  1245. else if (optname == MRT_PIM)
  1246. val = mrt->mroute_do_pim;
  1247. #endif
  1248. else
  1249. val = mrt->mroute_do_assert;
  1250. if (copy_to_user(optval, &val, olr))
  1251. return -EFAULT;
  1252. return 0;
  1253. }
  1254. /*
  1255. * The IP multicast ioctl support routines.
  1256. */
  1257. int ipmr_ioctl(struct sock *sk, int cmd, void __user *arg)
  1258. {
  1259. struct sioc_sg_req sr;
  1260. struct sioc_vif_req vr;
  1261. struct vif_device *vif;
  1262. struct mfc_cache *c;
  1263. struct net *net = sock_net(sk);
  1264. struct mr_table *mrt;
  1265. mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
  1266. if (!mrt)
  1267. return -ENOENT;
  1268. switch (cmd) {
  1269. case SIOCGETVIFCNT:
  1270. if (copy_from_user(&vr, arg, sizeof(vr)))
  1271. return -EFAULT;
  1272. if (vr.vifi >= mrt->maxvif)
  1273. return -EINVAL;
  1274. read_lock(&mrt_lock);
  1275. vif = &mrt->vif_table[vr.vifi];
  1276. if (VIF_EXISTS(mrt, vr.vifi)) {
  1277. vr.icount = vif->pkt_in;
  1278. vr.ocount = vif->pkt_out;
  1279. vr.ibytes = vif->bytes_in;
  1280. vr.obytes = vif->bytes_out;
  1281. read_unlock(&mrt_lock);
  1282. if (copy_to_user(arg, &vr, sizeof(vr)))
  1283. return -EFAULT;
  1284. return 0;
  1285. }
  1286. read_unlock(&mrt_lock);
  1287. return -EADDRNOTAVAIL;
  1288. case SIOCGETSGCNT:
  1289. if (copy_from_user(&sr, arg, sizeof(sr)))
  1290. return -EFAULT;
  1291. rcu_read_lock();
  1292. c = ipmr_cache_find(mrt, sr.src.s_addr, sr.grp.s_addr);
  1293. if (c) {
  1294. sr.pktcnt = c->mfc_un.res.pkt;
  1295. sr.bytecnt = c->mfc_un.res.bytes;
  1296. sr.wrong_if = c->mfc_un.res.wrong_if;
  1297. rcu_read_unlock();
  1298. if (copy_to_user(arg, &sr, sizeof(sr)))
  1299. return -EFAULT;
  1300. return 0;
  1301. }
  1302. rcu_read_unlock();
  1303. return -EADDRNOTAVAIL;
  1304. default:
  1305. return -ENOIOCTLCMD;
  1306. }
  1307. }
  1308. #ifdef CONFIG_COMPAT
  1309. struct compat_sioc_sg_req {
  1310. struct in_addr src;
  1311. struct in_addr grp;
  1312. compat_ulong_t pktcnt;
  1313. compat_ulong_t bytecnt;
  1314. compat_ulong_t wrong_if;
  1315. };
  1316. struct compat_sioc_vif_req {
  1317. vifi_t vifi; /* Which iface */
  1318. compat_ulong_t icount;
  1319. compat_ulong_t ocount;
  1320. compat_ulong_t ibytes;
  1321. compat_ulong_t obytes;
  1322. };
  1323. int ipmr_compat_ioctl(struct sock *sk, unsigned int cmd, void __user *arg)
  1324. {
  1325. struct compat_sioc_sg_req sr;
  1326. struct compat_sioc_vif_req vr;
  1327. struct vif_device *vif;
  1328. struct mfc_cache *c;
  1329. struct net *net = sock_net(sk);
  1330. struct mr_table *mrt;
  1331. mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
  1332. if (!mrt)
  1333. return -ENOENT;
  1334. switch (cmd) {
  1335. case SIOCGETVIFCNT:
  1336. if (copy_from_user(&vr, arg, sizeof(vr)))
  1337. return -EFAULT;
  1338. if (vr.vifi >= mrt->maxvif)
  1339. return -EINVAL;
  1340. vr.vifi = array_index_nospec(vr.vifi, mrt->maxvif);
  1341. read_lock(&mrt_lock);
  1342. vif = &mrt->vif_table[vr.vifi];
  1343. if (VIF_EXISTS(mrt, vr.vifi)) {
  1344. vr.icount = vif->pkt_in;
  1345. vr.ocount = vif->pkt_out;
  1346. vr.ibytes = vif->bytes_in;
  1347. vr.obytes = vif->bytes_out;
  1348. read_unlock(&mrt_lock);
  1349. if (copy_to_user(arg, &vr, sizeof(vr)))
  1350. return -EFAULT;
  1351. return 0;
  1352. }
  1353. read_unlock(&mrt_lock);
  1354. return -EADDRNOTAVAIL;
  1355. case SIOCGETSGCNT:
  1356. if (copy_from_user(&sr, arg, sizeof(sr)))
  1357. return -EFAULT;
  1358. rcu_read_lock();
  1359. c = ipmr_cache_find(mrt, sr.src.s_addr, sr.grp.s_addr);
  1360. if (c) {
  1361. sr.pktcnt = c->mfc_un.res.pkt;
  1362. sr.bytecnt = c->mfc_un.res.bytes;
  1363. sr.wrong_if = c->mfc_un.res.wrong_if;
  1364. rcu_read_unlock();
  1365. if (copy_to_user(arg, &sr, sizeof(sr)))
  1366. return -EFAULT;
  1367. return 0;
  1368. }
  1369. rcu_read_unlock();
  1370. return -EADDRNOTAVAIL;
  1371. default:
  1372. return -ENOIOCTLCMD;
  1373. }
  1374. }
  1375. #endif
  1376. static int ipmr_device_event(struct notifier_block *this, unsigned long event, void *ptr)
  1377. {
  1378. struct net_device *dev = netdev_notifier_info_to_dev(ptr);
  1379. struct net *net = dev_net(dev);
  1380. struct mr_table *mrt;
  1381. struct vif_device *v;
  1382. int ct;
  1383. if (event != NETDEV_UNREGISTER)
  1384. return NOTIFY_DONE;
  1385. ipmr_for_each_table(mrt, net) {
  1386. v = &mrt->vif_table[0];
  1387. for (ct = 0; ct < mrt->maxvif; ct++, v++) {
  1388. if (v->dev == dev)
  1389. vif_delete(mrt, ct, 1, NULL);
  1390. }
  1391. }
  1392. return NOTIFY_DONE;
  1393. }
  1394. static struct notifier_block ip_mr_notifier = {
  1395. .notifier_call = ipmr_device_event,
  1396. };
  1397. /*
  1398. * Encapsulate a packet by attaching a valid IPIP header to it.
  1399. * This avoids tunnel drivers and other mess and gives us the speed so
  1400. * important for multicast video.
  1401. */
  1402. static void ip_encap(struct net *net, struct sk_buff *skb,
  1403. __be32 saddr, __be32 daddr)
  1404. {
  1405. struct iphdr *iph;
  1406. const struct iphdr *old_iph = ip_hdr(skb);
  1407. skb_push(skb, sizeof(struct iphdr));
  1408. skb->transport_header = skb->network_header;
  1409. skb_reset_network_header(skb);
  1410. iph = ip_hdr(skb);
  1411. iph->version = 4;
  1412. iph->tos = old_iph->tos;
  1413. iph->ttl = old_iph->ttl;
  1414. iph->frag_off = 0;
  1415. iph->daddr = daddr;
  1416. iph->saddr = saddr;
  1417. iph->protocol = IPPROTO_IPIP;
  1418. iph->ihl = 5;
  1419. iph->tot_len = htons(skb->len);
  1420. ip_select_ident(net, skb, NULL);
  1421. ip_send_check(iph);
  1422. memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
  1423. nf_reset(skb);
  1424. }
  1425. static inline int ipmr_forward_finish(struct net *net, struct sock *sk,
  1426. struct sk_buff *skb)
  1427. {
  1428. struct ip_options *opt = &(IPCB(skb)->opt);
  1429. IP_INC_STATS(net, IPSTATS_MIB_OUTFORWDATAGRAMS);
  1430. IP_ADD_STATS(net, IPSTATS_MIB_OUTOCTETS, skb->len);
  1431. if (unlikely(opt->optlen))
  1432. ip_forward_options(skb);
  1433. return dst_output(net, sk, skb);
  1434. }
  1435. /*
  1436. * Processing handlers for ipmr_forward
  1437. */
  1438. static void ipmr_queue_xmit(struct net *net, struct mr_table *mrt,
  1439. struct sk_buff *skb, struct mfc_cache *c, int vifi)
  1440. {
  1441. const struct iphdr *iph = ip_hdr(skb);
  1442. struct vif_device *vif = &mrt->vif_table[vifi];
  1443. struct net_device *dev;
  1444. struct rtable *rt;
  1445. struct flowi4 fl4;
  1446. int encap = 0;
  1447. if (!vif->dev)
  1448. goto out_free;
  1449. #ifdef CONFIG_IP_PIMSM
  1450. if (vif->flags & VIFF_REGISTER) {
  1451. vif->pkt_out++;
  1452. vif->bytes_out += skb->len;
  1453. vif->dev->stats.tx_bytes += skb->len;
  1454. vif->dev->stats.tx_packets++;
  1455. ipmr_cache_report(mrt, skb, vifi, IGMPMSG_WHOLEPKT);
  1456. goto out_free;
  1457. }
  1458. #endif
  1459. if (vif->flags & VIFF_TUNNEL) {
  1460. rt = ip_route_output_ports(net, &fl4, NULL,
  1461. vif->remote, vif->local,
  1462. 0, 0,
  1463. IPPROTO_IPIP,
  1464. RT_TOS(iph->tos), vif->link);
  1465. if (IS_ERR(rt))
  1466. goto out_free;
  1467. encap = sizeof(struct iphdr);
  1468. } else {
  1469. rt = ip_route_output_ports(net, &fl4, NULL, iph->daddr, 0,
  1470. 0, 0,
  1471. IPPROTO_IPIP,
  1472. RT_TOS(iph->tos), vif->link);
  1473. if (IS_ERR(rt))
  1474. goto out_free;
  1475. }
  1476. dev = rt->dst.dev;
  1477. if (skb->len+encap > dst_mtu(&rt->dst) && (ntohs(iph->frag_off) & IP_DF)) {
  1478. /* Do not fragment multicasts. Alas, IPv4 does not
  1479. * allow to send ICMP, so that packets will disappear
  1480. * to blackhole.
  1481. */
  1482. IP_INC_STATS(net, IPSTATS_MIB_FRAGFAILS);
  1483. ip_rt_put(rt);
  1484. goto out_free;
  1485. }
  1486. encap += LL_RESERVED_SPACE(dev) + rt->dst.header_len;
  1487. if (skb_cow(skb, encap)) {
  1488. ip_rt_put(rt);
  1489. goto out_free;
  1490. }
  1491. vif->pkt_out++;
  1492. vif->bytes_out += skb->len;
  1493. skb_dst_drop(skb);
  1494. skb_dst_set(skb, &rt->dst);
  1495. ip_decrease_ttl(ip_hdr(skb));
  1496. /* FIXME: forward and output firewalls used to be called here.
  1497. * What do we do with netfilter? -- RR
  1498. */
  1499. if (vif->flags & VIFF_TUNNEL) {
  1500. ip_encap(net, skb, vif->local, vif->remote);
  1501. /* FIXME: extra output firewall step used to be here. --RR */
  1502. vif->dev->stats.tx_packets++;
  1503. vif->dev->stats.tx_bytes += skb->len;
  1504. }
  1505. IPCB(skb)->flags |= IPSKB_FORWARDED;
  1506. /*
  1507. * RFC1584 teaches, that DVMRP/PIM router must deliver packets locally
  1508. * not only before forwarding, but after forwarding on all output
  1509. * interfaces. It is clear, if mrouter runs a multicasting
  1510. * program, it should receive packets not depending to what interface
  1511. * program is joined.
  1512. * If we will not make it, the program will have to join on all
  1513. * interfaces. On the other hand, multihoming host (or router, but
  1514. * not mrouter) cannot join to more than one interface - it will
  1515. * result in receiving multiple packets.
  1516. */
  1517. NF_HOOK(NFPROTO_IPV4, NF_INET_FORWARD,
  1518. net, NULL, skb, skb->dev, dev,
  1519. ipmr_forward_finish);
  1520. return;
  1521. out_free:
  1522. kfree_skb(skb);
  1523. }
  1524. static int ipmr_find_vif(struct mr_table *mrt, struct net_device *dev)
  1525. {
  1526. int ct;
  1527. for (ct = mrt->maxvif-1; ct >= 0; ct--) {
  1528. if (mrt->vif_table[ct].dev == dev)
  1529. break;
  1530. }
  1531. return ct;
  1532. }
  1533. /* "local" means that we should preserve one skb (for local delivery) */
  1534. static void ip_mr_forward(struct net *net, struct mr_table *mrt,
  1535. struct sk_buff *skb, struct mfc_cache *cache,
  1536. int local)
  1537. {
  1538. int psend = -1;
  1539. int vif, ct;
  1540. int true_vifi = ipmr_find_vif(mrt, skb->dev);
  1541. vif = cache->mfc_parent;
  1542. cache->mfc_un.res.pkt++;
  1543. cache->mfc_un.res.bytes += skb->len;
  1544. if (cache->mfc_origin == htonl(INADDR_ANY) && true_vifi >= 0) {
  1545. struct mfc_cache *cache_proxy;
  1546. /* For an (*,G) entry, we only check that the incomming
  1547. * interface is part of the static tree.
  1548. */
  1549. cache_proxy = ipmr_cache_find_any_parent(mrt, vif);
  1550. if (cache_proxy &&
  1551. cache_proxy->mfc_un.res.ttls[true_vifi] < 255)
  1552. goto forward;
  1553. }
  1554. /*
  1555. * Wrong interface: drop packet and (maybe) send PIM assert.
  1556. */
  1557. if (mrt->vif_table[vif].dev != skb->dev) {
  1558. if (rt_is_output_route(skb_rtable(skb))) {
  1559. /* It is our own packet, looped back.
  1560. * Very complicated situation...
  1561. *
  1562. * The best workaround until routing daemons will be
  1563. * fixed is not to redistribute packet, if it was
  1564. * send through wrong interface. It means, that
  1565. * multicast applications WILL NOT work for
  1566. * (S,G), which have default multicast route pointing
  1567. * to wrong oif. In any case, it is not a good
  1568. * idea to use multicasting applications on router.
  1569. */
  1570. goto dont_forward;
  1571. }
  1572. cache->mfc_un.res.wrong_if++;
  1573. if (true_vifi >= 0 && mrt->mroute_do_assert &&
  1574. /* pimsm uses asserts, when switching from RPT to SPT,
  1575. * so that we cannot check that packet arrived on an oif.
  1576. * It is bad, but otherwise we would need to move pretty
  1577. * large chunk of pimd to kernel. Ough... --ANK
  1578. */
  1579. (mrt->mroute_do_pim ||
  1580. cache->mfc_un.res.ttls[true_vifi] < 255) &&
  1581. time_after(jiffies,
  1582. cache->mfc_un.res.last_assert + MFC_ASSERT_THRESH)) {
  1583. cache->mfc_un.res.last_assert = jiffies;
  1584. ipmr_cache_report(mrt, skb, true_vifi, IGMPMSG_WRONGVIF);
  1585. }
  1586. goto dont_forward;
  1587. }
  1588. forward:
  1589. mrt->vif_table[vif].pkt_in++;
  1590. mrt->vif_table[vif].bytes_in += skb->len;
  1591. /*
  1592. * Forward the frame
  1593. */
  1594. if (cache->mfc_origin == htonl(INADDR_ANY) &&
  1595. cache->mfc_mcastgrp == htonl(INADDR_ANY)) {
  1596. if (true_vifi >= 0 &&
  1597. true_vifi != cache->mfc_parent &&
  1598. ip_hdr(skb)->ttl >
  1599. cache->mfc_un.res.ttls[cache->mfc_parent]) {
  1600. /* It's an (*,*) entry and the packet is not coming from
  1601. * the upstream: forward the packet to the upstream
  1602. * only.
  1603. */
  1604. psend = cache->mfc_parent;
  1605. goto last_forward;
  1606. }
  1607. goto dont_forward;
  1608. }
  1609. for (ct = cache->mfc_un.res.maxvif - 1;
  1610. ct >= cache->mfc_un.res.minvif; ct--) {
  1611. /* For (*,G) entry, don't forward to the incoming interface */
  1612. if ((cache->mfc_origin != htonl(INADDR_ANY) ||
  1613. ct != true_vifi) &&
  1614. ip_hdr(skb)->ttl > cache->mfc_un.res.ttls[ct]) {
  1615. if (psend != -1) {
  1616. struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
  1617. if (skb2)
  1618. ipmr_queue_xmit(net, mrt, skb2, cache,
  1619. psend);
  1620. }
  1621. psend = ct;
  1622. }
  1623. }
  1624. last_forward:
  1625. if (psend != -1) {
  1626. if (local) {
  1627. struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
  1628. if (skb2)
  1629. ipmr_queue_xmit(net, mrt, skb2, cache, psend);
  1630. } else {
  1631. ipmr_queue_xmit(net, mrt, skb, cache, psend);
  1632. return;
  1633. }
  1634. }
  1635. dont_forward:
  1636. if (!local)
  1637. kfree_skb(skb);
  1638. }
  1639. static struct mr_table *ipmr_rt_fib_lookup(struct net *net, struct sk_buff *skb)
  1640. {
  1641. struct rtable *rt = skb_rtable(skb);
  1642. struct iphdr *iph = ip_hdr(skb);
  1643. struct flowi4 fl4 = {
  1644. .daddr = iph->daddr,
  1645. .saddr = iph->saddr,
  1646. .flowi4_tos = RT_TOS(iph->tos),
  1647. .flowi4_oif = (rt_is_output_route(rt) ?
  1648. skb->dev->ifindex : 0),
  1649. .flowi4_iif = (rt_is_output_route(rt) ?
  1650. LOOPBACK_IFINDEX :
  1651. skb->dev->ifindex),
  1652. .flowi4_mark = skb->mark,
  1653. };
  1654. struct mr_table *mrt;
  1655. int err;
  1656. err = ipmr_fib_lookup(net, &fl4, &mrt);
  1657. if (err)
  1658. return ERR_PTR(err);
  1659. return mrt;
  1660. }
  1661. /*
  1662. * Multicast packets for forwarding arrive here
  1663. * Called with rcu_read_lock();
  1664. */
  1665. int ip_mr_input(struct sk_buff *skb)
  1666. {
  1667. struct mfc_cache *cache;
  1668. struct net *net = dev_net(skb->dev);
  1669. int local = skb_rtable(skb)->rt_flags & RTCF_LOCAL;
  1670. struct mr_table *mrt;
  1671. /* Packet is looped back after forward, it should not be
  1672. * forwarded second time, but still can be delivered locally.
  1673. */
  1674. if (IPCB(skb)->flags & IPSKB_FORWARDED)
  1675. goto dont_forward;
  1676. mrt = ipmr_rt_fib_lookup(net, skb);
  1677. if (IS_ERR(mrt)) {
  1678. kfree_skb(skb);
  1679. return PTR_ERR(mrt);
  1680. }
  1681. if (!local) {
  1682. if (IPCB(skb)->opt.router_alert) {
  1683. if (ip_call_ra_chain(skb))
  1684. return 0;
  1685. } else if (ip_hdr(skb)->protocol == IPPROTO_IGMP) {
  1686. /* IGMPv1 (and broken IGMPv2 implementations sort of
  1687. * Cisco IOS <= 11.2(8)) do not put router alert
  1688. * option to IGMP packets destined to routable
  1689. * groups. It is very bad, because it means
  1690. * that we can forward NO IGMP messages.
  1691. */
  1692. struct sock *mroute_sk;
  1693. mroute_sk = rcu_dereference(mrt->mroute_sk);
  1694. if (mroute_sk) {
  1695. nf_reset(skb);
  1696. raw_rcv(mroute_sk, skb);
  1697. return 0;
  1698. }
  1699. }
  1700. }
  1701. /* already under rcu_read_lock() */
  1702. cache = ipmr_cache_find(mrt, ip_hdr(skb)->saddr, ip_hdr(skb)->daddr);
  1703. if (!cache) {
  1704. int vif = ipmr_find_vif(mrt, skb->dev);
  1705. if (vif >= 0)
  1706. cache = ipmr_cache_find_any(mrt, ip_hdr(skb)->daddr,
  1707. vif);
  1708. }
  1709. /*
  1710. * No usable cache entry
  1711. */
  1712. if (!cache) {
  1713. int vif;
  1714. if (local) {
  1715. struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
  1716. ip_local_deliver(skb);
  1717. if (!skb2)
  1718. return -ENOBUFS;
  1719. skb = skb2;
  1720. }
  1721. read_lock(&mrt_lock);
  1722. vif = ipmr_find_vif(mrt, skb->dev);
  1723. if (vif >= 0) {
  1724. int err2 = ipmr_cache_unresolved(mrt, vif, skb);
  1725. read_unlock(&mrt_lock);
  1726. return err2;
  1727. }
  1728. read_unlock(&mrt_lock);
  1729. kfree_skb(skb);
  1730. return -ENODEV;
  1731. }
  1732. read_lock(&mrt_lock);
  1733. ip_mr_forward(net, mrt, skb, cache, local);
  1734. read_unlock(&mrt_lock);
  1735. if (local)
  1736. return ip_local_deliver(skb);
  1737. return 0;
  1738. dont_forward:
  1739. if (local)
  1740. return ip_local_deliver(skb);
  1741. kfree_skb(skb);
  1742. return 0;
  1743. }
  1744. #ifdef CONFIG_IP_PIMSM
  1745. /* called with rcu_read_lock() */
  1746. static int __pim_rcv(struct mr_table *mrt, struct sk_buff *skb,
  1747. unsigned int pimlen)
  1748. {
  1749. struct net_device *reg_dev = NULL;
  1750. struct iphdr *encap;
  1751. encap = (struct iphdr *)(skb_transport_header(skb) + pimlen);
  1752. /*
  1753. * Check that:
  1754. * a. packet is really sent to a multicast group
  1755. * b. packet is not a NULL-REGISTER
  1756. * c. packet is not truncated
  1757. */
  1758. if (!ipv4_is_multicast(encap->daddr) ||
  1759. encap->tot_len == 0 ||
  1760. ntohs(encap->tot_len) + pimlen > skb->len)
  1761. return 1;
  1762. read_lock(&mrt_lock);
  1763. if (mrt->mroute_reg_vif_num >= 0)
  1764. reg_dev = mrt->vif_table[mrt->mroute_reg_vif_num].dev;
  1765. read_unlock(&mrt_lock);
  1766. if (!reg_dev)
  1767. return 1;
  1768. skb->mac_header = skb->network_header;
  1769. skb_pull(skb, (u8 *)encap - skb->data);
  1770. skb_reset_network_header(skb);
  1771. skb->protocol = htons(ETH_P_IP);
  1772. skb->ip_summed = CHECKSUM_NONE;
  1773. skb_tunnel_rx(skb, reg_dev, dev_net(reg_dev));
  1774. netif_rx(skb);
  1775. return NET_RX_SUCCESS;
  1776. }
  1777. #endif
  1778. #ifdef CONFIG_IP_PIMSM_V1
  1779. /*
  1780. * Handle IGMP messages of PIMv1
  1781. */
  1782. int pim_rcv_v1(struct sk_buff *skb)
  1783. {
  1784. struct igmphdr *pim;
  1785. struct net *net = dev_net(skb->dev);
  1786. struct mr_table *mrt;
  1787. if (!pskb_may_pull(skb, sizeof(*pim) + sizeof(struct iphdr)))
  1788. goto drop;
  1789. pim = igmp_hdr(skb);
  1790. mrt = ipmr_rt_fib_lookup(net, skb);
  1791. if (IS_ERR(mrt))
  1792. goto drop;
  1793. if (!mrt->mroute_do_pim ||
  1794. pim->group != PIM_V1_VERSION || pim->code != PIM_V1_REGISTER)
  1795. goto drop;
  1796. if (__pim_rcv(mrt, skb, sizeof(*pim))) {
  1797. drop:
  1798. kfree_skb(skb);
  1799. }
  1800. return 0;
  1801. }
  1802. #endif
  1803. #ifdef CONFIG_IP_PIMSM_V2
  1804. static int pim_rcv(struct sk_buff *skb)
  1805. {
  1806. struct pimreghdr *pim;
  1807. struct net *net = dev_net(skb->dev);
  1808. struct mr_table *mrt;
  1809. if (!pskb_may_pull(skb, sizeof(*pim) + sizeof(struct iphdr)))
  1810. goto drop;
  1811. pim = (struct pimreghdr *)skb_transport_header(skb);
  1812. if (pim->type != ((PIM_VERSION << 4) | (PIM_REGISTER)) ||
  1813. (pim->flags & PIM_NULL_REGISTER) ||
  1814. (ip_compute_csum((void *)pim, sizeof(*pim)) != 0 &&
  1815. csum_fold(skb_checksum(skb, 0, skb->len, 0))))
  1816. goto drop;
  1817. mrt = ipmr_rt_fib_lookup(net, skb);
  1818. if (IS_ERR(mrt))
  1819. goto drop;
  1820. if (__pim_rcv(mrt, skb, sizeof(*pim))) {
  1821. drop:
  1822. kfree_skb(skb);
  1823. }
  1824. return 0;
  1825. }
  1826. #endif
  1827. static int __ipmr_fill_mroute(struct mr_table *mrt, struct sk_buff *skb,
  1828. struct mfc_cache *c, struct rtmsg *rtm)
  1829. {
  1830. int ct;
  1831. struct rtnexthop *nhp;
  1832. struct nlattr *mp_attr;
  1833. struct rta_mfc_stats mfcs;
  1834. /* If cache is unresolved, don't try to parse IIF and OIF */
  1835. if (c->mfc_parent >= MAXVIFS)
  1836. return -ENOENT;
  1837. if (VIF_EXISTS(mrt, c->mfc_parent) &&
  1838. nla_put_u32(skb, RTA_IIF, mrt->vif_table[c->mfc_parent].dev->ifindex) < 0)
  1839. return -EMSGSIZE;
  1840. if (!(mp_attr = nla_nest_start(skb, RTA_MULTIPATH)))
  1841. return -EMSGSIZE;
  1842. for (ct = c->mfc_un.res.minvif; ct < c->mfc_un.res.maxvif; ct++) {
  1843. if (VIF_EXISTS(mrt, ct) && c->mfc_un.res.ttls[ct] < 255) {
  1844. if (!(nhp = nla_reserve_nohdr(skb, sizeof(*nhp)))) {
  1845. nla_nest_cancel(skb, mp_attr);
  1846. return -EMSGSIZE;
  1847. }
  1848. nhp->rtnh_flags = 0;
  1849. nhp->rtnh_hops = c->mfc_un.res.ttls[ct];
  1850. nhp->rtnh_ifindex = mrt->vif_table[ct].dev->ifindex;
  1851. nhp->rtnh_len = sizeof(*nhp);
  1852. }
  1853. }
  1854. nla_nest_end(skb, mp_attr);
  1855. mfcs.mfcs_packets = c->mfc_un.res.pkt;
  1856. mfcs.mfcs_bytes = c->mfc_un.res.bytes;
  1857. mfcs.mfcs_wrong_if = c->mfc_un.res.wrong_if;
  1858. if (nla_put(skb, RTA_MFC_STATS, sizeof(mfcs), &mfcs) < 0)
  1859. return -EMSGSIZE;
  1860. rtm->rtm_type = RTN_MULTICAST;
  1861. return 1;
  1862. }
  1863. int ipmr_get_route(struct net *net, struct sk_buff *skb,
  1864. __be32 saddr, __be32 daddr,
  1865. struct rtmsg *rtm, int nowait, u32 portid)
  1866. {
  1867. struct mfc_cache *cache;
  1868. struct mr_table *mrt;
  1869. int err;
  1870. mrt = ipmr_get_table(net, RT_TABLE_DEFAULT);
  1871. if (!mrt)
  1872. return -ENOENT;
  1873. rcu_read_lock();
  1874. cache = ipmr_cache_find(mrt, saddr, daddr);
  1875. if (!cache && skb->dev) {
  1876. int vif = ipmr_find_vif(mrt, skb->dev);
  1877. if (vif >= 0)
  1878. cache = ipmr_cache_find_any(mrt, daddr, vif);
  1879. }
  1880. if (!cache) {
  1881. struct sk_buff *skb2;
  1882. struct iphdr *iph;
  1883. struct net_device *dev;
  1884. int vif = -1;
  1885. if (nowait) {
  1886. rcu_read_unlock();
  1887. return -EAGAIN;
  1888. }
  1889. dev = skb->dev;
  1890. read_lock(&mrt_lock);
  1891. if (dev)
  1892. vif = ipmr_find_vif(mrt, dev);
  1893. if (vif < 0) {
  1894. read_unlock(&mrt_lock);
  1895. rcu_read_unlock();
  1896. return -ENODEV;
  1897. }
  1898. skb2 = skb_clone(skb, GFP_ATOMIC);
  1899. if (!skb2) {
  1900. read_unlock(&mrt_lock);
  1901. rcu_read_unlock();
  1902. return -ENOMEM;
  1903. }
  1904. NETLINK_CB(skb2).portid = portid;
  1905. skb_push(skb2, sizeof(struct iphdr));
  1906. skb_reset_network_header(skb2);
  1907. iph = ip_hdr(skb2);
  1908. iph->ihl = sizeof(struct iphdr) >> 2;
  1909. iph->saddr = saddr;
  1910. iph->daddr = daddr;
  1911. iph->version = 0;
  1912. err = ipmr_cache_unresolved(mrt, vif, skb2);
  1913. read_unlock(&mrt_lock);
  1914. rcu_read_unlock();
  1915. return err;
  1916. }
  1917. read_lock(&mrt_lock);
  1918. if (!nowait && (rtm->rtm_flags & RTM_F_NOTIFY))
  1919. cache->mfc_flags |= MFC_NOTIFY;
  1920. err = __ipmr_fill_mroute(mrt, skb, cache, rtm);
  1921. read_unlock(&mrt_lock);
  1922. rcu_read_unlock();
  1923. return err;
  1924. }
  1925. static int ipmr_fill_mroute(struct mr_table *mrt, struct sk_buff *skb,
  1926. u32 portid, u32 seq, struct mfc_cache *c, int cmd,
  1927. int flags)
  1928. {
  1929. struct nlmsghdr *nlh;
  1930. struct rtmsg *rtm;
  1931. int err;
  1932. nlh = nlmsg_put(skb, portid, seq, cmd, sizeof(*rtm), flags);
  1933. if (!nlh)
  1934. return -EMSGSIZE;
  1935. rtm = nlmsg_data(nlh);
  1936. rtm->rtm_family = RTNL_FAMILY_IPMR;
  1937. rtm->rtm_dst_len = 32;
  1938. rtm->rtm_src_len = 32;
  1939. rtm->rtm_tos = 0;
  1940. rtm->rtm_table = mrt->id;
  1941. if (nla_put_u32(skb, RTA_TABLE, mrt->id))
  1942. goto nla_put_failure;
  1943. rtm->rtm_type = RTN_MULTICAST;
  1944. rtm->rtm_scope = RT_SCOPE_UNIVERSE;
  1945. if (c->mfc_flags & MFC_STATIC)
  1946. rtm->rtm_protocol = RTPROT_STATIC;
  1947. else
  1948. rtm->rtm_protocol = RTPROT_MROUTED;
  1949. rtm->rtm_flags = 0;
  1950. if (nla_put_in_addr(skb, RTA_SRC, c->mfc_origin) ||
  1951. nla_put_in_addr(skb, RTA_DST, c->mfc_mcastgrp))
  1952. goto nla_put_failure;
  1953. err = __ipmr_fill_mroute(mrt, skb, c, rtm);
  1954. /* do not break the dump if cache is unresolved */
  1955. if (err < 0 && err != -ENOENT)
  1956. goto nla_put_failure;
  1957. nlmsg_end(skb, nlh);
  1958. return 0;
  1959. nla_put_failure:
  1960. nlmsg_cancel(skb, nlh);
  1961. return -EMSGSIZE;
  1962. }
  1963. static size_t mroute_msgsize(bool unresolved, int maxvif)
  1964. {
  1965. size_t len =
  1966. NLMSG_ALIGN(sizeof(struct rtmsg))
  1967. + nla_total_size(4) /* RTA_TABLE */
  1968. + nla_total_size(4) /* RTA_SRC */
  1969. + nla_total_size(4) /* RTA_DST */
  1970. ;
  1971. if (!unresolved)
  1972. len = len
  1973. + nla_total_size(4) /* RTA_IIF */
  1974. + nla_total_size(0) /* RTA_MULTIPATH */
  1975. + maxvif * NLA_ALIGN(sizeof(struct rtnexthop))
  1976. /* RTA_MFC_STATS */
  1977. + nla_total_size(sizeof(struct rta_mfc_stats))
  1978. ;
  1979. return len;
  1980. }
  1981. static void mroute_netlink_event(struct mr_table *mrt, struct mfc_cache *mfc,
  1982. int cmd)
  1983. {
  1984. struct net *net = read_pnet(&mrt->net);
  1985. struct sk_buff *skb;
  1986. int err = -ENOBUFS;
  1987. skb = nlmsg_new(mroute_msgsize(mfc->mfc_parent >= MAXVIFS, mrt->maxvif),
  1988. GFP_ATOMIC);
  1989. if (!skb)
  1990. goto errout;
  1991. err = ipmr_fill_mroute(mrt, skb, 0, 0, mfc, cmd, 0);
  1992. if (err < 0)
  1993. goto errout;
  1994. rtnl_notify(skb, net, 0, RTNLGRP_IPV4_MROUTE, NULL, GFP_ATOMIC);
  1995. return;
  1996. errout:
  1997. kfree_skb(skb);
  1998. if (err < 0)
  1999. rtnl_set_sk_err(net, RTNLGRP_IPV4_MROUTE, err);
  2000. }
  2001. static int ipmr_rtm_dumproute(struct sk_buff *skb, struct netlink_callback *cb)
  2002. {
  2003. struct net *net = sock_net(skb->sk);
  2004. struct mr_table *mrt;
  2005. struct mfc_cache *mfc;
  2006. unsigned int t = 0, s_t;
  2007. unsigned int h = 0, s_h;
  2008. unsigned int e = 0, s_e;
  2009. s_t = cb->args[0];
  2010. s_h = cb->args[1];
  2011. s_e = cb->args[2];
  2012. rcu_read_lock();
  2013. ipmr_for_each_table(mrt, net) {
  2014. if (t < s_t)
  2015. goto next_table;
  2016. if (t > s_t)
  2017. s_h = 0;
  2018. for (h = s_h; h < MFC_LINES; h++) {
  2019. list_for_each_entry_rcu(mfc, &mrt->mfc_cache_array[h], list) {
  2020. if (e < s_e)
  2021. goto next_entry;
  2022. if (ipmr_fill_mroute(mrt, skb,
  2023. NETLINK_CB(cb->skb).portid,
  2024. cb->nlh->nlmsg_seq,
  2025. mfc, RTM_NEWROUTE,
  2026. NLM_F_MULTI) < 0)
  2027. goto done;
  2028. next_entry:
  2029. e++;
  2030. }
  2031. e = s_e = 0;
  2032. }
  2033. spin_lock_bh(&mfc_unres_lock);
  2034. list_for_each_entry(mfc, &mrt->mfc_unres_queue, list) {
  2035. if (e < s_e)
  2036. goto next_entry2;
  2037. if (ipmr_fill_mroute(mrt, skb,
  2038. NETLINK_CB(cb->skb).portid,
  2039. cb->nlh->nlmsg_seq,
  2040. mfc, RTM_NEWROUTE,
  2041. NLM_F_MULTI) < 0) {
  2042. spin_unlock_bh(&mfc_unres_lock);
  2043. goto done;
  2044. }
  2045. next_entry2:
  2046. e++;
  2047. }
  2048. spin_unlock_bh(&mfc_unres_lock);
  2049. e = s_e = 0;
  2050. s_h = 0;
  2051. next_table:
  2052. t++;
  2053. }
  2054. done:
  2055. rcu_read_unlock();
  2056. cb->args[2] = e;
  2057. cb->args[1] = h;
  2058. cb->args[0] = t;
  2059. return skb->len;
  2060. }
  2061. #ifdef CONFIG_PROC_FS
  2062. /*
  2063. * The /proc interfaces to multicast routing :
  2064. * /proc/net/ip_mr_cache & /proc/net/ip_mr_vif
  2065. */
  2066. struct ipmr_vif_iter {
  2067. struct seq_net_private p;
  2068. struct mr_table *mrt;
  2069. int ct;
  2070. };
  2071. static struct vif_device *ipmr_vif_seq_idx(struct net *net,
  2072. struct ipmr_vif_iter *iter,
  2073. loff_t pos)
  2074. {
  2075. struct mr_table *mrt = iter->mrt;
  2076. for (iter->ct = 0; iter->ct < mrt->maxvif; ++iter->ct) {
  2077. if (!VIF_EXISTS(mrt, iter->ct))
  2078. continue;
  2079. if (pos-- == 0)
  2080. return &mrt->vif_table[iter->ct];
  2081. }
  2082. return NULL;
  2083. }
  2084. static void *ipmr_vif_seq_start(struct seq_file *seq, loff_t *pos)
  2085. __acquires(mrt_lock)
  2086. {
  2087. struct ipmr_vif_iter *iter = seq->private;
  2088. struct net *net = seq_file_net(seq);
  2089. struct mr_table *mrt;
  2090. mrt = ipmr_get_table(net, RT_TABLE_DEFAULT);
  2091. if (!mrt)
  2092. return ERR_PTR(-ENOENT);
  2093. iter->mrt = mrt;
  2094. read_lock(&mrt_lock);
  2095. return *pos ? ipmr_vif_seq_idx(net, seq->private, *pos - 1)
  2096. : SEQ_START_TOKEN;
  2097. }
  2098. static void *ipmr_vif_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  2099. {
  2100. struct ipmr_vif_iter *iter = seq->private;
  2101. struct net *net = seq_file_net(seq);
  2102. struct mr_table *mrt = iter->mrt;
  2103. ++*pos;
  2104. if (v == SEQ_START_TOKEN)
  2105. return ipmr_vif_seq_idx(net, iter, 0);
  2106. while (++iter->ct < mrt->maxvif) {
  2107. if (!VIF_EXISTS(mrt, iter->ct))
  2108. continue;
  2109. return &mrt->vif_table[iter->ct];
  2110. }
  2111. return NULL;
  2112. }
  2113. static void ipmr_vif_seq_stop(struct seq_file *seq, void *v)
  2114. __releases(mrt_lock)
  2115. {
  2116. read_unlock(&mrt_lock);
  2117. }
  2118. static int ipmr_vif_seq_show(struct seq_file *seq, void *v)
  2119. {
  2120. struct ipmr_vif_iter *iter = seq->private;
  2121. struct mr_table *mrt = iter->mrt;
  2122. if (v == SEQ_START_TOKEN) {
  2123. seq_puts(seq,
  2124. "Interface BytesIn PktsIn BytesOut PktsOut Flags Local Remote\n");
  2125. } else {
  2126. const struct vif_device *vif = v;
  2127. const char *name = vif->dev ? vif->dev->name : "none";
  2128. seq_printf(seq,
  2129. "%2Zd %-10s %8ld %7ld %8ld %7ld %05X %08X %08X\n",
  2130. vif - mrt->vif_table,
  2131. name, vif->bytes_in, vif->pkt_in,
  2132. vif->bytes_out, vif->pkt_out,
  2133. vif->flags, vif->local, vif->remote);
  2134. }
  2135. return 0;
  2136. }
  2137. static const struct seq_operations ipmr_vif_seq_ops = {
  2138. .start = ipmr_vif_seq_start,
  2139. .next = ipmr_vif_seq_next,
  2140. .stop = ipmr_vif_seq_stop,
  2141. .show = ipmr_vif_seq_show,
  2142. };
  2143. static int ipmr_vif_open(struct inode *inode, struct file *file)
  2144. {
  2145. return seq_open_net(inode, file, &ipmr_vif_seq_ops,
  2146. sizeof(struct ipmr_vif_iter));
  2147. }
  2148. static const struct file_operations ipmr_vif_fops = {
  2149. .owner = THIS_MODULE,
  2150. .open = ipmr_vif_open,
  2151. .read = seq_read,
  2152. .llseek = seq_lseek,
  2153. .release = seq_release_net,
  2154. };
  2155. struct ipmr_mfc_iter {
  2156. struct seq_net_private p;
  2157. struct mr_table *mrt;
  2158. struct list_head *cache;
  2159. int ct;
  2160. };
  2161. static struct mfc_cache *ipmr_mfc_seq_idx(struct net *net,
  2162. struct ipmr_mfc_iter *it, loff_t pos)
  2163. {
  2164. struct mr_table *mrt = it->mrt;
  2165. struct mfc_cache *mfc;
  2166. rcu_read_lock();
  2167. for (it->ct = 0; it->ct < MFC_LINES; it->ct++) {
  2168. it->cache = &mrt->mfc_cache_array[it->ct];
  2169. list_for_each_entry_rcu(mfc, it->cache, list)
  2170. if (pos-- == 0)
  2171. return mfc;
  2172. }
  2173. rcu_read_unlock();
  2174. spin_lock_bh(&mfc_unres_lock);
  2175. it->cache = &mrt->mfc_unres_queue;
  2176. list_for_each_entry(mfc, it->cache, list)
  2177. if (pos-- == 0)
  2178. return mfc;
  2179. spin_unlock_bh(&mfc_unres_lock);
  2180. it->cache = NULL;
  2181. return NULL;
  2182. }
  2183. static void *ipmr_mfc_seq_start(struct seq_file *seq, loff_t *pos)
  2184. {
  2185. struct ipmr_mfc_iter *it = seq->private;
  2186. struct net *net = seq_file_net(seq);
  2187. struct mr_table *mrt;
  2188. mrt = ipmr_get_table(net, RT_TABLE_DEFAULT);
  2189. if (!mrt)
  2190. return ERR_PTR(-ENOENT);
  2191. it->mrt = mrt;
  2192. it->cache = NULL;
  2193. it->ct = 0;
  2194. return *pos ? ipmr_mfc_seq_idx(net, seq->private, *pos - 1)
  2195. : SEQ_START_TOKEN;
  2196. }
  2197. static void *ipmr_mfc_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  2198. {
  2199. struct mfc_cache *mfc = v;
  2200. struct ipmr_mfc_iter *it = seq->private;
  2201. struct net *net = seq_file_net(seq);
  2202. struct mr_table *mrt = it->mrt;
  2203. ++*pos;
  2204. if (v == SEQ_START_TOKEN)
  2205. return ipmr_mfc_seq_idx(net, seq->private, 0);
  2206. if (mfc->list.next != it->cache)
  2207. return list_entry(mfc->list.next, struct mfc_cache, list);
  2208. if (it->cache == &mrt->mfc_unres_queue)
  2209. goto end_of_list;
  2210. BUG_ON(it->cache != &mrt->mfc_cache_array[it->ct]);
  2211. while (++it->ct < MFC_LINES) {
  2212. it->cache = &mrt->mfc_cache_array[it->ct];
  2213. if (list_empty(it->cache))
  2214. continue;
  2215. return list_first_entry(it->cache, struct mfc_cache, list);
  2216. }
  2217. /* exhausted cache_array, show unresolved */
  2218. rcu_read_unlock();
  2219. it->cache = &mrt->mfc_unres_queue;
  2220. it->ct = 0;
  2221. spin_lock_bh(&mfc_unres_lock);
  2222. if (!list_empty(it->cache))
  2223. return list_first_entry(it->cache, struct mfc_cache, list);
  2224. end_of_list:
  2225. spin_unlock_bh(&mfc_unres_lock);
  2226. it->cache = NULL;
  2227. return NULL;
  2228. }
  2229. static void ipmr_mfc_seq_stop(struct seq_file *seq, void *v)
  2230. {
  2231. struct ipmr_mfc_iter *it = seq->private;
  2232. struct mr_table *mrt = it->mrt;
  2233. if (it->cache == &mrt->mfc_unres_queue)
  2234. spin_unlock_bh(&mfc_unres_lock);
  2235. else if (it->cache == &mrt->mfc_cache_array[it->ct])
  2236. rcu_read_unlock();
  2237. }
  2238. static int ipmr_mfc_seq_show(struct seq_file *seq, void *v)
  2239. {
  2240. int n;
  2241. if (v == SEQ_START_TOKEN) {
  2242. seq_puts(seq,
  2243. "Group Origin Iif Pkts Bytes Wrong Oifs\n");
  2244. } else {
  2245. const struct mfc_cache *mfc = v;
  2246. const struct ipmr_mfc_iter *it = seq->private;
  2247. const struct mr_table *mrt = it->mrt;
  2248. seq_printf(seq, "%08X %08X %-3hd",
  2249. (__force u32) mfc->mfc_mcastgrp,
  2250. (__force u32) mfc->mfc_origin,
  2251. mfc->mfc_parent);
  2252. if (it->cache != &mrt->mfc_unres_queue) {
  2253. seq_printf(seq, " %8lu %8lu %8lu",
  2254. mfc->mfc_un.res.pkt,
  2255. mfc->mfc_un.res.bytes,
  2256. mfc->mfc_un.res.wrong_if);
  2257. for (n = mfc->mfc_un.res.minvif;
  2258. n < mfc->mfc_un.res.maxvif; n++) {
  2259. if (VIF_EXISTS(mrt, n) &&
  2260. mfc->mfc_un.res.ttls[n] < 255)
  2261. seq_printf(seq,
  2262. " %2d:%-3d",
  2263. n, mfc->mfc_un.res.ttls[n]);
  2264. }
  2265. } else {
  2266. /* unresolved mfc_caches don't contain
  2267. * pkt, bytes and wrong_if values
  2268. */
  2269. seq_printf(seq, " %8lu %8lu %8lu", 0ul, 0ul, 0ul);
  2270. }
  2271. seq_putc(seq, '\n');
  2272. }
  2273. return 0;
  2274. }
  2275. static const struct seq_operations ipmr_mfc_seq_ops = {
  2276. .start = ipmr_mfc_seq_start,
  2277. .next = ipmr_mfc_seq_next,
  2278. .stop = ipmr_mfc_seq_stop,
  2279. .show = ipmr_mfc_seq_show,
  2280. };
  2281. static int ipmr_mfc_open(struct inode *inode, struct file *file)
  2282. {
  2283. return seq_open_net(inode, file, &ipmr_mfc_seq_ops,
  2284. sizeof(struct ipmr_mfc_iter));
  2285. }
  2286. static const struct file_operations ipmr_mfc_fops = {
  2287. .owner = THIS_MODULE,
  2288. .open = ipmr_mfc_open,
  2289. .read = seq_read,
  2290. .llseek = seq_lseek,
  2291. .release = seq_release_net,
  2292. };
  2293. #endif
  2294. #ifdef CONFIG_IP_PIMSM_V2
  2295. static const struct net_protocol pim_protocol = {
  2296. .handler = pim_rcv,
  2297. .netns_ok = 1,
  2298. };
  2299. #endif
  2300. /*
  2301. * Setup for IP multicast routing
  2302. */
  2303. static int __net_init ipmr_net_init(struct net *net)
  2304. {
  2305. int err;
  2306. err = ipmr_rules_init(net);
  2307. if (err < 0)
  2308. goto fail;
  2309. #ifdef CONFIG_PROC_FS
  2310. err = -ENOMEM;
  2311. if (!proc_create("ip_mr_vif", 0, net->proc_net, &ipmr_vif_fops))
  2312. goto proc_vif_fail;
  2313. if (!proc_create("ip_mr_cache", 0, net->proc_net, &ipmr_mfc_fops))
  2314. goto proc_cache_fail;
  2315. #endif
  2316. return 0;
  2317. #ifdef CONFIG_PROC_FS
  2318. proc_cache_fail:
  2319. remove_proc_entry("ip_mr_vif", net->proc_net);
  2320. proc_vif_fail:
  2321. ipmr_rules_exit(net);
  2322. #endif
  2323. fail:
  2324. return err;
  2325. }
  2326. static void __net_exit ipmr_net_exit(struct net *net)
  2327. {
  2328. #ifdef CONFIG_PROC_FS
  2329. remove_proc_entry("ip_mr_cache", net->proc_net);
  2330. remove_proc_entry("ip_mr_vif", net->proc_net);
  2331. #endif
  2332. ipmr_rules_exit(net);
  2333. }
  2334. static struct pernet_operations ipmr_net_ops = {
  2335. .init = ipmr_net_init,
  2336. .exit = ipmr_net_exit,
  2337. };
  2338. int __init ip_mr_init(void)
  2339. {
  2340. int err;
  2341. mrt_cachep = kmem_cache_create("ip_mrt_cache",
  2342. sizeof(struct mfc_cache),
  2343. 0, SLAB_HWCACHE_ALIGN | SLAB_PANIC,
  2344. NULL);
  2345. if (!mrt_cachep)
  2346. return -ENOMEM;
  2347. err = register_pernet_subsys(&ipmr_net_ops);
  2348. if (err)
  2349. goto reg_pernet_fail;
  2350. err = register_netdevice_notifier(&ip_mr_notifier);
  2351. if (err)
  2352. goto reg_notif_fail;
  2353. #ifdef CONFIG_IP_PIMSM_V2
  2354. if (inet_add_protocol(&pim_protocol, IPPROTO_PIM) < 0) {
  2355. pr_err("%s: can't add PIM protocol\n", __func__);
  2356. err = -EAGAIN;
  2357. goto add_proto_fail;
  2358. }
  2359. #endif
  2360. rtnl_register(RTNL_FAMILY_IPMR, RTM_GETROUTE,
  2361. NULL, ipmr_rtm_dumproute, NULL);
  2362. return 0;
  2363. #ifdef CONFIG_IP_PIMSM_V2
  2364. add_proto_fail:
  2365. unregister_netdevice_notifier(&ip_mr_notifier);
  2366. #endif
  2367. reg_notif_fail:
  2368. unregister_pernet_subsys(&ipmr_net_ops);
  2369. reg_pernet_fail:
  2370. kmem_cache_destroy(mrt_cachep);
  2371. return err;
  2372. }