ipmr.c 59 KB

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