ipmr.c 57 KB

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