ipmr.c 57 KB

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