ipmr.c 57 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562
  1. /*
  2. * IP multicast routing support for mrouted 3.6/3.8
  3. *
  4. * (c) 1995 Alan Cox, <alan@lxorguk.ukuu.org.uk>
  5. * Linux Consultancy and Custom Driver Development
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License
  9. * as published by the Free Software Foundation; either version
  10. * 2 of the License, or (at your option) any later version.
  11. *
  12. * Fixes:
  13. * Michael Chastain : Incorrect size of copying.
  14. * Alan Cox : Added the cache manager code
  15. * Alan Cox : Fixed the clone/copy bug and device race.
  16. * Mike McLagan : Routing by source
  17. * Malcolm Beattie : Buffer handling fixes.
  18. * Alexey Kuznetsov : Double buffer free and other fixes.
  19. * SVR Anand : Fixed several multicast bugs and problems.
  20. * Alexey Kuznetsov : Status, optimisations and more.
  21. * Brad Parker : Better behaviour on mrouted upcall
  22. * overflow.
  23. * Carlos Picoto : PIMv1 Support
  24. * Pavlin Ivanov Radoslavov: PIMv2 Registers must checksum only PIM header
  25. * Relax this requirement to work with older peers.
  26. *
  27. */
  28. #include <asm/uaccess.h>
  29. #include <linux/types.h>
  30. #include <linux/capability.h>
  31. #include <linux/errno.h>
  32. #include <linux/timer.h>
  33. #include <linux/mm.h>
  34. #include <linux/kernel.h>
  35. #include <linux/fcntl.h>
  36. #include <linux/stat.h>
  37. #include <linux/socket.h>
  38. #include <linux/in.h>
  39. #include <linux/inet.h>
  40. #include <linux/netdevice.h>
  41. #include <linux/inetdevice.h>
  42. #include <linux/igmp.h>
  43. #include <linux/proc_fs.h>
  44. #include <linux/seq_file.h>
  45. #include <linux/mroute.h>
  46. #include <linux/init.h>
  47. #include <linux/if_ether.h>
  48. #include <linux/slab.h>
  49. #include <net/net_namespace.h>
  50. #include <net/ip.h>
  51. #include <net/protocol.h>
  52. #include <linux/skbuff.h>
  53. #include <net/route.h>
  54. #include <net/sock.h>
  55. #include <net/icmp.h>
  56. #include <net/udp.h>
  57. #include <net/raw.h>
  58. #include <linux/notifier.h>
  59. #include <linux/if_arp.h>
  60. #include <linux/netfilter_ipv4.h>
  61. #include <linux/compat.h>
  62. #include <linux/export.h>
  63. #include <net/ipip.h>
  64. #include <net/checksum.h>
  65. #include <net/netlink.h>
  66. #include <net/fib_rules.h>
  67. #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. * vif_delete - 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. net_warn_ratelimited("mroute: pending queue full, dropping entries\n");
  799. kfree_skb(skb);
  800. }
  801. return ret;
  802. }
  803. /*
  804. * Queue a packet for resolution. It gets locked cache entry!
  805. */
  806. static int
  807. ipmr_cache_unresolved(struct mr_table *mrt, vifi_t vifi, struct sk_buff *skb)
  808. {
  809. bool found = false;
  810. int err;
  811. struct mfc_cache *c;
  812. const struct iphdr *iph = ip_hdr(skb);
  813. spin_lock_bh(&mfc_unres_lock);
  814. list_for_each_entry(c, &mrt->mfc_unres_queue, list) {
  815. if (c->mfc_mcastgrp == iph->daddr &&
  816. c->mfc_origin == iph->saddr) {
  817. found = true;
  818. break;
  819. }
  820. }
  821. if (!found) {
  822. /* Create a new entry if allowable */
  823. if (atomic_read(&mrt->cache_resolve_queue_len) >= 10 ||
  824. (c = ipmr_cache_alloc_unres()) == NULL) {
  825. spin_unlock_bh(&mfc_unres_lock);
  826. kfree_skb(skb);
  827. return -ENOBUFS;
  828. }
  829. /* Fill in the new cache entry */
  830. c->mfc_parent = -1;
  831. c->mfc_origin = iph->saddr;
  832. c->mfc_mcastgrp = iph->daddr;
  833. /* Reflect first query at mrouted. */
  834. err = ipmr_cache_report(mrt, skb, vifi, IGMPMSG_NOCACHE);
  835. if (err < 0) {
  836. /* If the report failed throw the cache entry
  837. out - Brad Parker
  838. */
  839. spin_unlock_bh(&mfc_unres_lock);
  840. ipmr_cache_free(c);
  841. kfree_skb(skb);
  842. return err;
  843. }
  844. atomic_inc(&mrt->cache_resolve_queue_len);
  845. list_add(&c->list, &mrt->mfc_unres_queue);
  846. if (atomic_read(&mrt->cache_resolve_queue_len) == 1)
  847. mod_timer(&mrt->ipmr_expire_timer, c->mfc_un.unres.expires);
  848. }
  849. /* See if we can append the packet */
  850. if (c->mfc_un.unres.unresolved.qlen > 3) {
  851. kfree_skb(skb);
  852. err = -ENOBUFS;
  853. } else {
  854. skb_queue_tail(&c->mfc_un.unres.unresolved, skb);
  855. err = 0;
  856. }
  857. spin_unlock_bh(&mfc_unres_lock);
  858. return err;
  859. }
  860. /*
  861. * MFC cache manipulation by user space mroute daemon
  862. */
  863. static int ipmr_mfc_delete(struct mr_table *mrt, struct mfcctl *mfc)
  864. {
  865. int line;
  866. struct mfc_cache *c, *next;
  867. line = MFC_HASH(mfc->mfcc_mcastgrp.s_addr, mfc->mfcc_origin.s_addr);
  868. list_for_each_entry_safe(c, next, &mrt->mfc_cache_array[line], list) {
  869. if (c->mfc_origin == mfc->mfcc_origin.s_addr &&
  870. c->mfc_mcastgrp == mfc->mfcc_mcastgrp.s_addr) {
  871. list_del_rcu(&c->list);
  872. ipmr_cache_free(c);
  873. return 0;
  874. }
  875. }
  876. return -ENOENT;
  877. }
  878. static int ipmr_mfc_add(struct net *net, struct mr_table *mrt,
  879. struct mfcctl *mfc, int mrtsock)
  880. {
  881. bool found = false;
  882. int line;
  883. struct mfc_cache *uc, *c;
  884. if (mfc->mfcc_parent >= MAXVIFS)
  885. return -ENFILE;
  886. line = MFC_HASH(mfc->mfcc_mcastgrp.s_addr, mfc->mfcc_origin.s_addr);
  887. list_for_each_entry(c, &mrt->mfc_cache_array[line], list) {
  888. if (c->mfc_origin == mfc->mfcc_origin.s_addr &&
  889. c->mfc_mcastgrp == mfc->mfcc_mcastgrp.s_addr) {
  890. found = true;
  891. break;
  892. }
  893. }
  894. if (found) {
  895. write_lock_bh(&mrt_lock);
  896. c->mfc_parent = mfc->mfcc_parent;
  897. ipmr_update_thresholds(mrt, c, mfc->mfcc_ttls);
  898. if (!mrtsock)
  899. c->mfc_flags |= MFC_STATIC;
  900. write_unlock_bh(&mrt_lock);
  901. return 0;
  902. }
  903. if (!ipv4_is_multicast(mfc->mfcc_mcastgrp.s_addr))
  904. return -EINVAL;
  905. c = ipmr_cache_alloc();
  906. if (c == NULL)
  907. return -ENOMEM;
  908. c->mfc_origin = mfc->mfcc_origin.s_addr;
  909. c->mfc_mcastgrp = mfc->mfcc_mcastgrp.s_addr;
  910. c->mfc_parent = mfc->mfcc_parent;
  911. ipmr_update_thresholds(mrt, c, mfc->mfcc_ttls);
  912. if (!mrtsock)
  913. c->mfc_flags |= MFC_STATIC;
  914. list_add_rcu(&c->list, &mrt->mfc_cache_array[line]);
  915. /*
  916. * Check to see if we resolved a queued list. If so we
  917. * need to send on the frames and tidy up.
  918. */
  919. found = false;
  920. spin_lock_bh(&mfc_unres_lock);
  921. list_for_each_entry(uc, &mrt->mfc_unres_queue, list) {
  922. if (uc->mfc_origin == c->mfc_origin &&
  923. uc->mfc_mcastgrp == c->mfc_mcastgrp) {
  924. list_del(&uc->list);
  925. atomic_dec(&mrt->cache_resolve_queue_len);
  926. found = true;
  927. break;
  928. }
  929. }
  930. if (list_empty(&mrt->mfc_unres_queue))
  931. del_timer(&mrt->ipmr_expire_timer);
  932. spin_unlock_bh(&mfc_unres_lock);
  933. if (found) {
  934. ipmr_cache_resolve(net, mrt, uc, c);
  935. ipmr_cache_free(uc);
  936. }
  937. return 0;
  938. }
  939. /*
  940. * Close the multicast socket, and clear the vif tables etc
  941. */
  942. static void mroute_clean_tables(struct mr_table *mrt)
  943. {
  944. int i;
  945. LIST_HEAD(list);
  946. struct mfc_cache *c, *next;
  947. /* Shut down all active vif entries */
  948. for (i = 0; i < mrt->maxvif; i++) {
  949. if (!(mrt->vif_table[i].flags & VIFF_STATIC))
  950. vif_delete(mrt, i, 0, &list);
  951. }
  952. unregister_netdevice_many(&list);
  953. /* Wipe the cache */
  954. for (i = 0; i < MFC_LINES; i++) {
  955. list_for_each_entry_safe(c, next, &mrt->mfc_cache_array[i], list) {
  956. if (c->mfc_flags & MFC_STATIC)
  957. continue;
  958. list_del_rcu(&c->list);
  959. ipmr_cache_free(c);
  960. }
  961. }
  962. if (atomic_read(&mrt->cache_resolve_queue_len) != 0) {
  963. spin_lock_bh(&mfc_unres_lock);
  964. list_for_each_entry_safe(c, next, &mrt->mfc_unres_queue, list) {
  965. list_del(&c->list);
  966. ipmr_destroy_unres(mrt, c);
  967. }
  968. spin_unlock_bh(&mfc_unres_lock);
  969. }
  970. }
  971. /* called from ip_ra_control(), before an RCU grace period,
  972. * we dont need to call synchronize_rcu() here
  973. */
  974. static void mrtsock_destruct(struct sock *sk)
  975. {
  976. struct net *net = sock_net(sk);
  977. struct mr_table *mrt;
  978. rtnl_lock();
  979. ipmr_for_each_table(mrt, net) {
  980. if (sk == rtnl_dereference(mrt->mroute_sk)) {
  981. IPV4_DEVCONF_ALL(net, MC_FORWARDING)--;
  982. RCU_INIT_POINTER(mrt->mroute_sk, NULL);
  983. mroute_clean_tables(mrt);
  984. }
  985. }
  986. rtnl_unlock();
  987. }
  988. /*
  989. * Socket options and virtual interface manipulation. The whole
  990. * virtual interface system is a complete heap, but unfortunately
  991. * that's how BSD mrouted happens to think. Maybe one day with a proper
  992. * MOSPF/PIM router set up we can clean this up.
  993. */
  994. int ip_mroute_setsockopt(struct sock *sk, int optname, char __user *optval, unsigned int optlen)
  995. {
  996. int ret;
  997. struct vifctl vif;
  998. struct mfcctl mfc;
  999. struct net *net = sock_net(sk);
  1000. struct mr_table *mrt;
  1001. mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
  1002. if (mrt == NULL)
  1003. return -ENOENT;
  1004. if (optname != MRT_INIT) {
  1005. if (sk != rcu_access_pointer(mrt->mroute_sk) &&
  1006. !capable(CAP_NET_ADMIN))
  1007. return -EACCES;
  1008. }
  1009. switch (optname) {
  1010. case MRT_INIT:
  1011. if (sk->sk_type != SOCK_RAW ||
  1012. inet_sk(sk)->inet_num != IPPROTO_IGMP)
  1013. return -EOPNOTSUPP;
  1014. if (optlen != sizeof(int))
  1015. return -ENOPROTOOPT;
  1016. rtnl_lock();
  1017. if (rtnl_dereference(mrt->mroute_sk)) {
  1018. rtnl_unlock();
  1019. return -EADDRINUSE;
  1020. }
  1021. ret = ip_ra_control(sk, 1, mrtsock_destruct);
  1022. if (ret == 0) {
  1023. rcu_assign_pointer(mrt->mroute_sk, sk);
  1024. IPV4_DEVCONF_ALL(net, MC_FORWARDING)++;
  1025. }
  1026. rtnl_unlock();
  1027. return ret;
  1028. case MRT_DONE:
  1029. if (sk != rcu_access_pointer(mrt->mroute_sk))
  1030. return -EACCES;
  1031. return ip_ra_control(sk, 0, NULL);
  1032. case MRT_ADD_VIF:
  1033. case MRT_DEL_VIF:
  1034. if (optlen != sizeof(vif))
  1035. return -EINVAL;
  1036. if (copy_from_user(&vif, optval, sizeof(vif)))
  1037. return -EFAULT;
  1038. if (vif.vifc_vifi >= MAXVIFS)
  1039. return -ENFILE;
  1040. rtnl_lock();
  1041. if (optname == MRT_ADD_VIF) {
  1042. ret = vif_add(net, mrt, &vif,
  1043. sk == rtnl_dereference(mrt->mroute_sk));
  1044. } else {
  1045. ret = vif_delete(mrt, vif.vifc_vifi, 0, NULL);
  1046. }
  1047. rtnl_unlock();
  1048. return ret;
  1049. /*
  1050. * Manipulate the forwarding caches. These live
  1051. * in a sort of kernel/user symbiosis.
  1052. */
  1053. case MRT_ADD_MFC:
  1054. case MRT_DEL_MFC:
  1055. if (optlen != sizeof(mfc))
  1056. return -EINVAL;
  1057. if (copy_from_user(&mfc, optval, sizeof(mfc)))
  1058. return -EFAULT;
  1059. rtnl_lock();
  1060. if (optname == MRT_DEL_MFC)
  1061. ret = ipmr_mfc_delete(mrt, &mfc);
  1062. else
  1063. ret = ipmr_mfc_add(net, mrt, &mfc,
  1064. sk == rtnl_dereference(mrt->mroute_sk));
  1065. rtnl_unlock();
  1066. return ret;
  1067. /*
  1068. * Control PIM assert.
  1069. */
  1070. case MRT_ASSERT:
  1071. {
  1072. int v;
  1073. if (get_user(v, (int __user *)optval))
  1074. return -EFAULT;
  1075. mrt->mroute_do_assert = (v) ? 1 : 0;
  1076. return 0;
  1077. }
  1078. #ifdef CONFIG_IP_PIMSM
  1079. case MRT_PIM:
  1080. {
  1081. int v;
  1082. if (get_user(v, (int __user *)optval))
  1083. return -EFAULT;
  1084. v = (v) ? 1 : 0;
  1085. rtnl_lock();
  1086. ret = 0;
  1087. if (v != mrt->mroute_do_pim) {
  1088. mrt->mroute_do_pim = v;
  1089. mrt->mroute_do_assert = v;
  1090. }
  1091. rtnl_unlock();
  1092. return ret;
  1093. }
  1094. #endif
  1095. #ifdef CONFIG_IP_MROUTE_MULTIPLE_TABLES
  1096. case MRT_TABLE:
  1097. {
  1098. u32 v;
  1099. if (optlen != sizeof(u32))
  1100. return -EINVAL;
  1101. if (get_user(v, (u32 __user *)optval))
  1102. return -EFAULT;
  1103. rtnl_lock();
  1104. ret = 0;
  1105. if (sk == rtnl_dereference(mrt->mroute_sk)) {
  1106. ret = -EBUSY;
  1107. } else {
  1108. if (!ipmr_new_table(net, v))
  1109. ret = -ENOMEM;
  1110. raw_sk(sk)->ipmr_table = v;
  1111. }
  1112. rtnl_unlock();
  1113. return ret;
  1114. }
  1115. #endif
  1116. /*
  1117. * Spurious command, or MRT_VERSION which you cannot
  1118. * set.
  1119. */
  1120. default:
  1121. return -ENOPROTOOPT;
  1122. }
  1123. }
  1124. /*
  1125. * Getsock opt support for the multicast routing system.
  1126. */
  1127. int ip_mroute_getsockopt(struct sock *sk, int optname, char __user *optval, int __user *optlen)
  1128. {
  1129. int olr;
  1130. int val;
  1131. struct net *net = sock_net(sk);
  1132. struct mr_table *mrt;
  1133. mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
  1134. if (mrt == NULL)
  1135. return -ENOENT;
  1136. if (optname != MRT_VERSION &&
  1137. #ifdef CONFIG_IP_PIMSM
  1138. optname != MRT_PIM &&
  1139. #endif
  1140. optname != MRT_ASSERT)
  1141. return -ENOPROTOOPT;
  1142. if (get_user(olr, optlen))
  1143. return -EFAULT;
  1144. olr = min_t(unsigned int, olr, sizeof(int));
  1145. if (olr < 0)
  1146. return -EINVAL;
  1147. if (put_user(olr, optlen))
  1148. return -EFAULT;
  1149. if (optname == MRT_VERSION)
  1150. val = 0x0305;
  1151. #ifdef CONFIG_IP_PIMSM
  1152. else if (optname == MRT_PIM)
  1153. val = mrt->mroute_do_pim;
  1154. #endif
  1155. else
  1156. val = mrt->mroute_do_assert;
  1157. if (copy_to_user(optval, &val, olr))
  1158. return -EFAULT;
  1159. return 0;
  1160. }
  1161. /*
  1162. * The IP multicast ioctl support routines.
  1163. */
  1164. int ipmr_ioctl(struct sock *sk, int cmd, void __user *arg)
  1165. {
  1166. struct sioc_sg_req sr;
  1167. struct sioc_vif_req vr;
  1168. struct vif_device *vif;
  1169. struct mfc_cache *c;
  1170. struct net *net = sock_net(sk);
  1171. struct mr_table *mrt;
  1172. mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
  1173. if (mrt == NULL)
  1174. return -ENOENT;
  1175. switch (cmd) {
  1176. case SIOCGETVIFCNT:
  1177. if (copy_from_user(&vr, arg, sizeof(vr)))
  1178. return -EFAULT;
  1179. if (vr.vifi >= mrt->maxvif)
  1180. return -EINVAL;
  1181. read_lock(&mrt_lock);
  1182. vif = &mrt->vif_table[vr.vifi];
  1183. if (VIF_EXISTS(mrt, vr.vifi)) {
  1184. vr.icount = vif->pkt_in;
  1185. vr.ocount = vif->pkt_out;
  1186. vr.ibytes = vif->bytes_in;
  1187. vr.obytes = vif->bytes_out;
  1188. read_unlock(&mrt_lock);
  1189. if (copy_to_user(arg, &vr, sizeof(vr)))
  1190. return -EFAULT;
  1191. return 0;
  1192. }
  1193. read_unlock(&mrt_lock);
  1194. return -EADDRNOTAVAIL;
  1195. case SIOCGETSGCNT:
  1196. if (copy_from_user(&sr, arg, sizeof(sr)))
  1197. return -EFAULT;
  1198. rcu_read_lock();
  1199. c = ipmr_cache_find(mrt, sr.src.s_addr, sr.grp.s_addr);
  1200. if (c) {
  1201. sr.pktcnt = c->mfc_un.res.pkt;
  1202. sr.bytecnt = c->mfc_un.res.bytes;
  1203. sr.wrong_if = c->mfc_un.res.wrong_if;
  1204. rcu_read_unlock();
  1205. if (copy_to_user(arg, &sr, sizeof(sr)))
  1206. return -EFAULT;
  1207. return 0;
  1208. }
  1209. rcu_read_unlock();
  1210. return -EADDRNOTAVAIL;
  1211. default:
  1212. return -ENOIOCTLCMD;
  1213. }
  1214. }
  1215. #ifdef CONFIG_COMPAT
  1216. struct compat_sioc_sg_req {
  1217. struct in_addr src;
  1218. struct in_addr grp;
  1219. compat_ulong_t pktcnt;
  1220. compat_ulong_t bytecnt;
  1221. compat_ulong_t wrong_if;
  1222. };
  1223. struct compat_sioc_vif_req {
  1224. vifi_t vifi; /* Which iface */
  1225. compat_ulong_t icount;
  1226. compat_ulong_t ocount;
  1227. compat_ulong_t ibytes;
  1228. compat_ulong_t obytes;
  1229. };
  1230. int ipmr_compat_ioctl(struct sock *sk, unsigned int cmd, void __user *arg)
  1231. {
  1232. struct compat_sioc_sg_req sr;
  1233. struct compat_sioc_vif_req vr;
  1234. struct vif_device *vif;
  1235. struct mfc_cache *c;
  1236. struct net *net = sock_net(sk);
  1237. struct mr_table *mrt;
  1238. mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
  1239. if (mrt == NULL)
  1240. return -ENOENT;
  1241. switch (cmd) {
  1242. case SIOCGETVIFCNT:
  1243. if (copy_from_user(&vr, arg, sizeof(vr)))
  1244. return -EFAULT;
  1245. if (vr.vifi >= mrt->maxvif)
  1246. return -EINVAL;
  1247. read_lock(&mrt_lock);
  1248. vif = &mrt->vif_table[vr.vifi];
  1249. if (VIF_EXISTS(mrt, vr.vifi)) {
  1250. vr.icount = vif->pkt_in;
  1251. vr.ocount = vif->pkt_out;
  1252. vr.ibytes = vif->bytes_in;
  1253. vr.obytes = vif->bytes_out;
  1254. read_unlock(&mrt_lock);
  1255. if (copy_to_user(arg, &vr, sizeof(vr)))
  1256. return -EFAULT;
  1257. return 0;
  1258. }
  1259. read_unlock(&mrt_lock);
  1260. return -EADDRNOTAVAIL;
  1261. case SIOCGETSGCNT:
  1262. if (copy_from_user(&sr, arg, sizeof(sr)))
  1263. return -EFAULT;
  1264. rcu_read_lock();
  1265. c = ipmr_cache_find(mrt, sr.src.s_addr, sr.grp.s_addr);
  1266. if (c) {
  1267. sr.pktcnt = c->mfc_un.res.pkt;
  1268. sr.bytecnt = c->mfc_un.res.bytes;
  1269. sr.wrong_if = c->mfc_un.res.wrong_if;
  1270. rcu_read_unlock();
  1271. if (copy_to_user(arg, &sr, sizeof(sr)))
  1272. return -EFAULT;
  1273. return 0;
  1274. }
  1275. rcu_read_unlock();
  1276. return -EADDRNOTAVAIL;
  1277. default:
  1278. return -ENOIOCTLCMD;
  1279. }
  1280. }
  1281. #endif
  1282. static int ipmr_device_event(struct notifier_block *this, unsigned long event, void *ptr)
  1283. {
  1284. struct net_device *dev = ptr;
  1285. struct net *net = dev_net(dev);
  1286. struct mr_table *mrt;
  1287. struct vif_device *v;
  1288. int ct;
  1289. if (event != NETDEV_UNREGISTER)
  1290. return NOTIFY_DONE;
  1291. ipmr_for_each_table(mrt, net) {
  1292. v = &mrt->vif_table[0];
  1293. for (ct = 0; ct < mrt->maxvif; ct++, v++) {
  1294. if (v->dev == dev)
  1295. vif_delete(mrt, ct, 1, NULL);
  1296. }
  1297. }
  1298. return NOTIFY_DONE;
  1299. }
  1300. static struct notifier_block ip_mr_notifier = {
  1301. .notifier_call = ipmr_device_event,
  1302. };
  1303. /*
  1304. * Encapsulate a packet by attaching a valid IPIP header to it.
  1305. * This avoids tunnel drivers and other mess and gives us the speed so
  1306. * important for multicast video.
  1307. */
  1308. static void ip_encap(struct sk_buff *skb, __be32 saddr, __be32 daddr)
  1309. {
  1310. struct iphdr *iph;
  1311. const struct iphdr *old_iph = ip_hdr(skb);
  1312. skb_push(skb, sizeof(struct iphdr));
  1313. skb->transport_header = skb->network_header;
  1314. skb_reset_network_header(skb);
  1315. iph = ip_hdr(skb);
  1316. iph->version = 4;
  1317. iph->tos = old_iph->tos;
  1318. iph->ttl = old_iph->ttl;
  1319. iph->frag_off = 0;
  1320. iph->daddr = daddr;
  1321. iph->saddr = saddr;
  1322. iph->protocol = IPPROTO_IPIP;
  1323. iph->ihl = 5;
  1324. iph->tot_len = htons(skb->len);
  1325. ip_select_ident(iph, skb_dst(skb), NULL);
  1326. ip_send_check(iph);
  1327. memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
  1328. nf_reset(skb);
  1329. }
  1330. static inline int ipmr_forward_finish(struct sk_buff *skb)
  1331. {
  1332. struct ip_options *opt = &(IPCB(skb)->opt);
  1333. IP_INC_STATS_BH(dev_net(skb_dst(skb)->dev), IPSTATS_MIB_OUTFORWDATAGRAMS);
  1334. IP_ADD_STATS_BH(dev_net(skb_dst(skb)->dev), IPSTATS_MIB_OUTOCTETS, skb->len);
  1335. if (unlikely(opt->optlen))
  1336. ip_forward_options(skb);
  1337. return dst_output(skb);
  1338. }
  1339. /*
  1340. * Processing handlers for ipmr_forward
  1341. */
  1342. static void ipmr_queue_xmit(struct net *net, struct mr_table *mrt,
  1343. struct sk_buff *skb, struct mfc_cache *c, int vifi)
  1344. {
  1345. const struct iphdr *iph = ip_hdr(skb);
  1346. struct vif_device *vif = &mrt->vif_table[vifi];
  1347. struct net_device *dev;
  1348. struct rtable *rt;
  1349. struct flowi4 fl4;
  1350. int encap = 0;
  1351. if (vif->dev == NULL)
  1352. goto out_free;
  1353. #ifdef CONFIG_IP_PIMSM
  1354. if (vif->flags & VIFF_REGISTER) {
  1355. vif->pkt_out++;
  1356. vif->bytes_out += skb->len;
  1357. vif->dev->stats.tx_bytes += skb->len;
  1358. vif->dev->stats.tx_packets++;
  1359. ipmr_cache_report(mrt, skb, vifi, IGMPMSG_WHOLEPKT);
  1360. goto out_free;
  1361. }
  1362. #endif
  1363. if (vif->flags & VIFF_TUNNEL) {
  1364. rt = ip_route_output_ports(net, &fl4, NULL,
  1365. vif->remote, vif->local,
  1366. 0, 0,
  1367. IPPROTO_IPIP,
  1368. RT_TOS(iph->tos), vif->link);
  1369. if (IS_ERR(rt))
  1370. goto out_free;
  1371. encap = sizeof(struct iphdr);
  1372. } else {
  1373. rt = ip_route_output_ports(net, &fl4, NULL, iph->daddr, 0,
  1374. 0, 0,
  1375. IPPROTO_IPIP,
  1376. RT_TOS(iph->tos), vif->link);
  1377. if (IS_ERR(rt))
  1378. goto out_free;
  1379. }
  1380. dev = rt->dst.dev;
  1381. if (skb->len+encap > dst_mtu(&rt->dst) && (ntohs(iph->frag_off) & IP_DF)) {
  1382. /* Do not fragment multicasts. Alas, IPv4 does not
  1383. * allow to send ICMP, so that packets will disappear
  1384. * to blackhole.
  1385. */
  1386. IP_INC_STATS_BH(dev_net(dev), IPSTATS_MIB_FRAGFAILS);
  1387. ip_rt_put(rt);
  1388. goto out_free;
  1389. }
  1390. encap += LL_RESERVED_SPACE(dev) + rt->dst.header_len;
  1391. if (skb_cow(skb, encap)) {
  1392. ip_rt_put(rt);
  1393. goto out_free;
  1394. }
  1395. vif->pkt_out++;
  1396. vif->bytes_out += skb->len;
  1397. skb_dst_drop(skb);
  1398. skb_dst_set(skb, &rt->dst);
  1399. ip_decrease_ttl(ip_hdr(skb));
  1400. /* FIXME: forward and output firewalls used to be called here.
  1401. * What do we do with netfilter? -- RR
  1402. */
  1403. if (vif->flags & VIFF_TUNNEL) {
  1404. ip_encap(skb, vif->local, vif->remote);
  1405. /* FIXME: extra output firewall step used to be here. --RR */
  1406. vif->dev->stats.tx_packets++;
  1407. vif->dev->stats.tx_bytes += skb->len;
  1408. }
  1409. IPCB(skb)->flags |= IPSKB_FORWARDED;
  1410. /*
  1411. * RFC1584 teaches, that DVMRP/PIM router must deliver packets locally
  1412. * not only before forwarding, but after forwarding on all output
  1413. * interfaces. It is clear, if mrouter runs a multicasting
  1414. * program, it should receive packets not depending to what interface
  1415. * program is joined.
  1416. * If we will not make it, the program will have to join on all
  1417. * interfaces. On the other hand, multihoming host (or router, but
  1418. * not mrouter) cannot join to more than one interface - it will
  1419. * result in receiving multiple packets.
  1420. */
  1421. NF_HOOK(NFPROTO_IPV4, NF_INET_FORWARD, skb, skb->dev, dev,
  1422. ipmr_forward_finish);
  1423. return;
  1424. out_free:
  1425. kfree_skb(skb);
  1426. }
  1427. static int ipmr_find_vif(struct mr_table *mrt, struct net_device *dev)
  1428. {
  1429. int ct;
  1430. for (ct = mrt->maxvif-1; ct >= 0; ct--) {
  1431. if (mrt->vif_table[ct].dev == dev)
  1432. break;
  1433. }
  1434. return ct;
  1435. }
  1436. /* "local" means that we should preserve one skb (for local delivery) */
  1437. static int ip_mr_forward(struct net *net, struct mr_table *mrt,
  1438. struct sk_buff *skb, struct mfc_cache *cache,
  1439. int local)
  1440. {
  1441. int psend = -1;
  1442. int vif, ct;
  1443. vif = cache->mfc_parent;
  1444. cache->mfc_un.res.pkt++;
  1445. cache->mfc_un.res.bytes += skb->len;
  1446. /*
  1447. * Wrong interface: drop packet and (maybe) send PIM assert.
  1448. */
  1449. if (mrt->vif_table[vif].dev != skb->dev) {
  1450. int true_vifi;
  1451. if (rt_is_output_route(skb_rtable(skb))) {
  1452. /* It is our own packet, looped back.
  1453. * Very complicated situation...
  1454. *
  1455. * The best workaround until routing daemons will be
  1456. * fixed is not to redistribute packet, if it was
  1457. * send through wrong interface. It means, that
  1458. * multicast applications WILL NOT work for
  1459. * (S,G), which have default multicast route pointing
  1460. * to wrong oif. In any case, it is not a good
  1461. * idea to use multicasting applications on router.
  1462. */
  1463. goto dont_forward;
  1464. }
  1465. cache->mfc_un.res.wrong_if++;
  1466. true_vifi = ipmr_find_vif(mrt, skb->dev);
  1467. if (true_vifi >= 0 && mrt->mroute_do_assert &&
  1468. /* pimsm uses asserts, when switching from RPT to SPT,
  1469. * so that we cannot check that packet arrived on an oif.
  1470. * It is bad, but otherwise we would need to move pretty
  1471. * large chunk of pimd to kernel. Ough... --ANK
  1472. */
  1473. (mrt->mroute_do_pim ||
  1474. cache->mfc_un.res.ttls[true_vifi] < 255) &&
  1475. time_after(jiffies,
  1476. cache->mfc_un.res.last_assert + MFC_ASSERT_THRESH)) {
  1477. cache->mfc_un.res.last_assert = jiffies;
  1478. ipmr_cache_report(mrt, skb, true_vifi, IGMPMSG_WRONGVIF);
  1479. }
  1480. goto dont_forward;
  1481. }
  1482. mrt->vif_table[vif].pkt_in++;
  1483. mrt->vif_table[vif].bytes_in += skb->len;
  1484. /*
  1485. * Forward the frame
  1486. */
  1487. for (ct = cache->mfc_un.res.maxvif - 1;
  1488. ct >= cache->mfc_un.res.minvif; ct--) {
  1489. if (ip_hdr(skb)->ttl > cache->mfc_un.res.ttls[ct]) {
  1490. if (psend != -1) {
  1491. struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
  1492. if (skb2)
  1493. ipmr_queue_xmit(net, mrt, skb2, cache,
  1494. psend);
  1495. }
  1496. psend = ct;
  1497. }
  1498. }
  1499. if (psend != -1) {
  1500. if (local) {
  1501. struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
  1502. if (skb2)
  1503. ipmr_queue_xmit(net, mrt, skb2, cache, psend);
  1504. } else {
  1505. ipmr_queue_xmit(net, mrt, skb, cache, psend);
  1506. return 0;
  1507. }
  1508. }
  1509. dont_forward:
  1510. if (!local)
  1511. kfree_skb(skb);
  1512. return 0;
  1513. }
  1514. static struct mr_table *ipmr_rt_fib_lookup(struct net *net, struct sk_buff *skb)
  1515. {
  1516. struct rtable *rt = skb_rtable(skb);
  1517. struct iphdr *iph = ip_hdr(skb);
  1518. struct flowi4 fl4 = {
  1519. .daddr = iph->daddr,
  1520. .saddr = iph->saddr,
  1521. .flowi4_tos = RT_TOS(iph->tos),
  1522. .flowi4_oif = (rt_is_output_route(rt) ?
  1523. skb->dev->ifindex : 0),
  1524. .flowi4_iif = (rt_is_output_route(rt) ?
  1525. LOOPBACK_IFINDEX :
  1526. skb->dev->ifindex),
  1527. .flowi4_mark = skb->mark,
  1528. };
  1529. struct mr_table *mrt;
  1530. int err;
  1531. err = ipmr_fib_lookup(net, &fl4, &mrt);
  1532. if (err)
  1533. return ERR_PTR(err);
  1534. return mrt;
  1535. }
  1536. /*
  1537. * Multicast packets for forwarding arrive here
  1538. * Called with rcu_read_lock();
  1539. */
  1540. int ip_mr_input(struct sk_buff *skb)
  1541. {
  1542. struct mfc_cache *cache;
  1543. struct net *net = dev_net(skb->dev);
  1544. int local = skb_rtable(skb)->rt_flags & RTCF_LOCAL;
  1545. struct mr_table *mrt;
  1546. /* Packet is looped back after forward, it should not be
  1547. * forwarded second time, but still can be delivered locally.
  1548. */
  1549. if (IPCB(skb)->flags & IPSKB_FORWARDED)
  1550. goto dont_forward;
  1551. mrt = ipmr_rt_fib_lookup(net, skb);
  1552. if (IS_ERR(mrt)) {
  1553. kfree_skb(skb);
  1554. return PTR_ERR(mrt);
  1555. }
  1556. if (!local) {
  1557. if (IPCB(skb)->opt.router_alert) {
  1558. if (ip_call_ra_chain(skb))
  1559. return 0;
  1560. } else if (ip_hdr(skb)->protocol == IPPROTO_IGMP) {
  1561. /* IGMPv1 (and broken IGMPv2 implementations sort of
  1562. * Cisco IOS <= 11.2(8)) do not put router alert
  1563. * option to IGMP packets destined to routable
  1564. * groups. It is very bad, because it means
  1565. * that we can forward NO IGMP messages.
  1566. */
  1567. struct sock *mroute_sk;
  1568. mroute_sk = rcu_dereference(mrt->mroute_sk);
  1569. if (mroute_sk) {
  1570. nf_reset(skb);
  1571. raw_rcv(mroute_sk, skb);
  1572. return 0;
  1573. }
  1574. }
  1575. }
  1576. /* already under rcu_read_lock() */
  1577. cache = ipmr_cache_find(mrt, ip_hdr(skb)->saddr, ip_hdr(skb)->daddr);
  1578. /*
  1579. * No usable cache entry
  1580. */
  1581. if (cache == NULL) {
  1582. int vif;
  1583. if (local) {
  1584. struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
  1585. ip_local_deliver(skb);
  1586. if (skb2 == NULL)
  1587. return -ENOBUFS;
  1588. skb = skb2;
  1589. }
  1590. read_lock(&mrt_lock);
  1591. vif = ipmr_find_vif(mrt, skb->dev);
  1592. if (vif >= 0) {
  1593. int err2 = ipmr_cache_unresolved(mrt, vif, skb);
  1594. read_unlock(&mrt_lock);
  1595. return err2;
  1596. }
  1597. read_unlock(&mrt_lock);
  1598. kfree_skb(skb);
  1599. return -ENODEV;
  1600. }
  1601. read_lock(&mrt_lock);
  1602. ip_mr_forward(net, mrt, skb, cache, local);
  1603. read_unlock(&mrt_lock);
  1604. if (local)
  1605. return ip_local_deliver(skb);
  1606. return 0;
  1607. dont_forward:
  1608. if (local)
  1609. return ip_local_deliver(skb);
  1610. kfree_skb(skb);
  1611. return 0;
  1612. }
  1613. #ifdef CONFIG_IP_PIMSM
  1614. /* called with rcu_read_lock() */
  1615. static int __pim_rcv(struct mr_table *mrt, struct sk_buff *skb,
  1616. unsigned int pimlen)
  1617. {
  1618. struct net_device *reg_dev = NULL;
  1619. struct iphdr *encap;
  1620. encap = (struct iphdr *)(skb_transport_header(skb) + pimlen);
  1621. /*
  1622. * Check that:
  1623. * a. packet is really sent to a multicast group
  1624. * b. packet is not a NULL-REGISTER
  1625. * c. packet is not truncated
  1626. */
  1627. if (!ipv4_is_multicast(encap->daddr) ||
  1628. encap->tot_len == 0 ||
  1629. ntohs(encap->tot_len) + pimlen > skb->len)
  1630. return 1;
  1631. read_lock(&mrt_lock);
  1632. if (mrt->mroute_reg_vif_num >= 0)
  1633. reg_dev = mrt->vif_table[mrt->mroute_reg_vif_num].dev;
  1634. read_unlock(&mrt_lock);
  1635. if (reg_dev == NULL)
  1636. return 1;
  1637. skb->mac_header = skb->network_header;
  1638. skb_pull(skb, (u8 *)encap - skb->data);
  1639. skb_reset_network_header(skb);
  1640. skb->protocol = htons(ETH_P_IP);
  1641. skb->ip_summed = CHECKSUM_NONE;
  1642. skb->pkt_type = PACKET_HOST;
  1643. skb_tunnel_rx(skb, reg_dev);
  1644. netif_rx(skb);
  1645. return NET_RX_SUCCESS;
  1646. }
  1647. #endif
  1648. #ifdef CONFIG_IP_PIMSM_V1
  1649. /*
  1650. * Handle IGMP messages of PIMv1
  1651. */
  1652. int pim_rcv_v1(struct sk_buff *skb)
  1653. {
  1654. struct igmphdr *pim;
  1655. struct net *net = dev_net(skb->dev);
  1656. struct mr_table *mrt;
  1657. if (!pskb_may_pull(skb, sizeof(*pim) + sizeof(struct iphdr)))
  1658. goto drop;
  1659. pim = igmp_hdr(skb);
  1660. mrt = ipmr_rt_fib_lookup(net, skb);
  1661. if (IS_ERR(mrt))
  1662. goto drop;
  1663. if (!mrt->mroute_do_pim ||
  1664. pim->group != PIM_V1_VERSION || pim->code != PIM_V1_REGISTER)
  1665. goto drop;
  1666. if (__pim_rcv(mrt, skb, sizeof(*pim))) {
  1667. drop:
  1668. kfree_skb(skb);
  1669. }
  1670. return 0;
  1671. }
  1672. #endif
  1673. #ifdef CONFIG_IP_PIMSM_V2
  1674. static int pim_rcv(struct sk_buff *skb)
  1675. {
  1676. struct pimreghdr *pim;
  1677. struct net *net = dev_net(skb->dev);
  1678. struct mr_table *mrt;
  1679. if (!pskb_may_pull(skb, sizeof(*pim) + sizeof(struct iphdr)))
  1680. goto drop;
  1681. pim = (struct pimreghdr *)skb_transport_header(skb);
  1682. if (pim->type != ((PIM_VERSION << 4) | (PIM_REGISTER)) ||
  1683. (pim->flags & PIM_NULL_REGISTER) ||
  1684. (ip_compute_csum((void *)pim, sizeof(*pim)) != 0 &&
  1685. csum_fold(skb_checksum(skb, 0, skb->len, 0))))
  1686. goto drop;
  1687. mrt = ipmr_rt_fib_lookup(net, skb);
  1688. if (IS_ERR(mrt))
  1689. goto drop;
  1690. if (__pim_rcv(mrt, skb, sizeof(*pim))) {
  1691. drop:
  1692. kfree_skb(skb);
  1693. }
  1694. return 0;
  1695. }
  1696. #endif
  1697. static int __ipmr_fill_mroute(struct mr_table *mrt, struct sk_buff *skb,
  1698. struct mfc_cache *c, struct rtmsg *rtm)
  1699. {
  1700. int ct;
  1701. struct rtnexthop *nhp;
  1702. struct nlattr *mp_attr;
  1703. /* If cache is unresolved, don't try to parse IIF and OIF */
  1704. if (c->mfc_parent >= MAXVIFS)
  1705. return -ENOENT;
  1706. if (VIF_EXISTS(mrt, c->mfc_parent) &&
  1707. nla_put_u32(skb, RTA_IIF, mrt->vif_table[c->mfc_parent].dev->ifindex) < 0)
  1708. return -EMSGSIZE;
  1709. if (!(mp_attr = nla_nest_start(skb, RTA_MULTIPATH)))
  1710. return -EMSGSIZE;
  1711. for (ct = c->mfc_un.res.minvif; ct < c->mfc_un.res.maxvif; ct++) {
  1712. if (VIF_EXISTS(mrt, ct) && c->mfc_un.res.ttls[ct] < 255) {
  1713. if (!(nhp = nla_reserve_nohdr(skb, sizeof(*nhp)))) {
  1714. nla_nest_cancel(skb, mp_attr);
  1715. return -EMSGSIZE;
  1716. }
  1717. nhp->rtnh_flags = 0;
  1718. nhp->rtnh_hops = c->mfc_un.res.ttls[ct];
  1719. nhp->rtnh_ifindex = mrt->vif_table[ct].dev->ifindex;
  1720. nhp->rtnh_len = sizeof(*nhp);
  1721. }
  1722. }
  1723. nla_nest_end(skb, mp_attr);
  1724. rtm->rtm_type = RTN_MULTICAST;
  1725. return 1;
  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. if (nla_put_u32(skb, RTA_TABLE, mrt->id))
  1798. goto nla_put_failure;
  1799. rtm->rtm_type = RTN_MULTICAST;
  1800. rtm->rtm_scope = RT_SCOPE_UNIVERSE;
  1801. rtm->rtm_protocol = RTPROT_UNSPEC;
  1802. rtm->rtm_flags = 0;
  1803. if (nla_put_be32(skb, RTA_SRC, c->mfc_origin) ||
  1804. nla_put_be32(skb, RTA_DST, c->mfc_mcastgrp))
  1805. goto nla_put_failure;
  1806. if (__ipmr_fill_mroute(mrt, skb, c, rtm) < 0)
  1807. goto nla_put_failure;
  1808. return nlmsg_end(skb, nlh);
  1809. nla_put_failure:
  1810. nlmsg_cancel(skb, nlh);
  1811. return -EMSGSIZE;
  1812. }
  1813. static int ipmr_rtm_dumproute(struct sk_buff *skb, struct netlink_callback *cb)
  1814. {
  1815. struct net *net = sock_net(skb->sk);
  1816. struct mr_table *mrt;
  1817. struct mfc_cache *mfc;
  1818. unsigned int t = 0, s_t;
  1819. unsigned int h = 0, s_h;
  1820. unsigned int e = 0, s_e;
  1821. s_t = cb->args[0];
  1822. s_h = cb->args[1];
  1823. s_e = cb->args[2];
  1824. rcu_read_lock();
  1825. ipmr_for_each_table(mrt, net) {
  1826. if (t < s_t)
  1827. goto next_table;
  1828. if (t > s_t)
  1829. s_h = 0;
  1830. for (h = s_h; h < MFC_LINES; h++) {
  1831. list_for_each_entry_rcu(mfc, &mrt->mfc_cache_array[h], list) {
  1832. if (e < s_e)
  1833. goto next_entry;
  1834. if (ipmr_fill_mroute(mrt, skb,
  1835. NETLINK_CB(cb->skb).pid,
  1836. cb->nlh->nlmsg_seq,
  1837. mfc) < 0)
  1838. goto done;
  1839. next_entry:
  1840. e++;
  1841. }
  1842. e = s_e = 0;
  1843. }
  1844. s_h = 0;
  1845. next_table:
  1846. t++;
  1847. }
  1848. done:
  1849. rcu_read_unlock();
  1850. cb->args[2] = e;
  1851. cb->args[1] = h;
  1852. cb->args[0] = t;
  1853. return skb->len;
  1854. }
  1855. #ifdef CONFIG_PROC_FS
  1856. /*
  1857. * The /proc interfaces to multicast routing :
  1858. * /proc/net/ip_mr_cache & /proc/net/ip_mr_vif
  1859. */
  1860. struct ipmr_vif_iter {
  1861. struct seq_net_private p;
  1862. struct mr_table *mrt;
  1863. int ct;
  1864. };
  1865. static struct vif_device *ipmr_vif_seq_idx(struct net *net,
  1866. struct ipmr_vif_iter *iter,
  1867. loff_t pos)
  1868. {
  1869. struct mr_table *mrt = iter->mrt;
  1870. for (iter->ct = 0; iter->ct < mrt->maxvif; ++iter->ct) {
  1871. if (!VIF_EXISTS(mrt, iter->ct))
  1872. continue;
  1873. if (pos-- == 0)
  1874. return &mrt->vif_table[iter->ct];
  1875. }
  1876. return NULL;
  1877. }
  1878. static void *ipmr_vif_seq_start(struct seq_file *seq, loff_t *pos)
  1879. __acquires(mrt_lock)
  1880. {
  1881. struct ipmr_vif_iter *iter = seq->private;
  1882. struct net *net = seq_file_net(seq);
  1883. struct mr_table *mrt;
  1884. mrt = ipmr_get_table(net, RT_TABLE_DEFAULT);
  1885. if (mrt == NULL)
  1886. return ERR_PTR(-ENOENT);
  1887. iter->mrt = mrt;
  1888. read_lock(&mrt_lock);
  1889. return *pos ? ipmr_vif_seq_idx(net, seq->private, *pos - 1)
  1890. : SEQ_START_TOKEN;
  1891. }
  1892. static void *ipmr_vif_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  1893. {
  1894. struct ipmr_vif_iter *iter = seq->private;
  1895. struct net *net = seq_file_net(seq);
  1896. struct mr_table *mrt = iter->mrt;
  1897. ++*pos;
  1898. if (v == SEQ_START_TOKEN)
  1899. return ipmr_vif_seq_idx(net, iter, 0);
  1900. while (++iter->ct < mrt->maxvif) {
  1901. if (!VIF_EXISTS(mrt, iter->ct))
  1902. continue;
  1903. return &mrt->vif_table[iter->ct];
  1904. }
  1905. return NULL;
  1906. }
  1907. static void ipmr_vif_seq_stop(struct seq_file *seq, void *v)
  1908. __releases(mrt_lock)
  1909. {
  1910. read_unlock(&mrt_lock);
  1911. }
  1912. static int ipmr_vif_seq_show(struct seq_file *seq, void *v)
  1913. {
  1914. struct ipmr_vif_iter *iter = seq->private;
  1915. struct mr_table *mrt = iter->mrt;
  1916. if (v == SEQ_START_TOKEN) {
  1917. seq_puts(seq,
  1918. "Interface BytesIn PktsIn BytesOut PktsOut Flags Local Remote\n");
  1919. } else {
  1920. const struct vif_device *vif = v;
  1921. const char *name = vif->dev ? vif->dev->name : "none";
  1922. seq_printf(seq,
  1923. "%2Zd %-10s %8ld %7ld %8ld %7ld %05X %08X %08X\n",
  1924. vif - mrt->vif_table,
  1925. name, vif->bytes_in, vif->pkt_in,
  1926. vif->bytes_out, vif->pkt_out,
  1927. vif->flags, vif->local, vif->remote);
  1928. }
  1929. return 0;
  1930. }
  1931. static const struct seq_operations ipmr_vif_seq_ops = {
  1932. .start = ipmr_vif_seq_start,
  1933. .next = ipmr_vif_seq_next,
  1934. .stop = ipmr_vif_seq_stop,
  1935. .show = ipmr_vif_seq_show,
  1936. };
  1937. static int ipmr_vif_open(struct inode *inode, struct file *file)
  1938. {
  1939. return seq_open_net(inode, file, &ipmr_vif_seq_ops,
  1940. sizeof(struct ipmr_vif_iter));
  1941. }
  1942. static const struct file_operations ipmr_vif_fops = {
  1943. .owner = THIS_MODULE,
  1944. .open = ipmr_vif_open,
  1945. .read = seq_read,
  1946. .llseek = seq_lseek,
  1947. .release = seq_release_net,
  1948. };
  1949. struct ipmr_mfc_iter {
  1950. struct seq_net_private p;
  1951. struct mr_table *mrt;
  1952. struct list_head *cache;
  1953. int ct;
  1954. };
  1955. static struct mfc_cache *ipmr_mfc_seq_idx(struct net *net,
  1956. struct ipmr_mfc_iter *it, loff_t pos)
  1957. {
  1958. struct mr_table *mrt = it->mrt;
  1959. struct mfc_cache *mfc;
  1960. rcu_read_lock();
  1961. for (it->ct = 0; it->ct < MFC_LINES; it->ct++) {
  1962. it->cache = &mrt->mfc_cache_array[it->ct];
  1963. list_for_each_entry_rcu(mfc, it->cache, list)
  1964. if (pos-- == 0)
  1965. return mfc;
  1966. }
  1967. rcu_read_unlock();
  1968. spin_lock_bh(&mfc_unres_lock);
  1969. it->cache = &mrt->mfc_unres_queue;
  1970. list_for_each_entry(mfc, it->cache, list)
  1971. if (pos-- == 0)
  1972. return mfc;
  1973. spin_unlock_bh(&mfc_unres_lock);
  1974. it->cache = NULL;
  1975. return NULL;
  1976. }
  1977. static void *ipmr_mfc_seq_start(struct seq_file *seq, loff_t *pos)
  1978. {
  1979. struct ipmr_mfc_iter *it = seq->private;
  1980. struct net *net = seq_file_net(seq);
  1981. struct mr_table *mrt;
  1982. mrt = ipmr_get_table(net, RT_TABLE_DEFAULT);
  1983. if (mrt == NULL)
  1984. return ERR_PTR(-ENOENT);
  1985. it->mrt = mrt;
  1986. it->cache = NULL;
  1987. it->ct = 0;
  1988. return *pos ? ipmr_mfc_seq_idx(net, seq->private, *pos - 1)
  1989. : SEQ_START_TOKEN;
  1990. }
  1991. static void *ipmr_mfc_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  1992. {
  1993. struct mfc_cache *mfc = v;
  1994. struct ipmr_mfc_iter *it = seq->private;
  1995. struct net *net = seq_file_net(seq);
  1996. struct mr_table *mrt = it->mrt;
  1997. ++*pos;
  1998. if (v == SEQ_START_TOKEN)
  1999. return ipmr_mfc_seq_idx(net, seq->private, 0);
  2000. if (mfc->list.next != it->cache)
  2001. return list_entry(mfc->list.next, struct mfc_cache, list);
  2002. if (it->cache == &mrt->mfc_unres_queue)
  2003. goto end_of_list;
  2004. BUG_ON(it->cache != &mrt->mfc_cache_array[it->ct]);
  2005. while (++it->ct < MFC_LINES) {
  2006. it->cache = &mrt->mfc_cache_array[it->ct];
  2007. if (list_empty(it->cache))
  2008. continue;
  2009. return list_first_entry(it->cache, struct mfc_cache, list);
  2010. }
  2011. /* exhausted cache_array, show unresolved */
  2012. rcu_read_unlock();
  2013. it->cache = &mrt->mfc_unres_queue;
  2014. it->ct = 0;
  2015. spin_lock_bh(&mfc_unres_lock);
  2016. if (!list_empty(it->cache))
  2017. return list_first_entry(it->cache, struct mfc_cache, list);
  2018. end_of_list:
  2019. spin_unlock_bh(&mfc_unres_lock);
  2020. it->cache = NULL;
  2021. return NULL;
  2022. }
  2023. static void ipmr_mfc_seq_stop(struct seq_file *seq, void *v)
  2024. {
  2025. struct ipmr_mfc_iter *it = seq->private;
  2026. struct mr_table *mrt = it->mrt;
  2027. if (it->cache == &mrt->mfc_unres_queue)
  2028. spin_unlock_bh(&mfc_unres_lock);
  2029. else if (it->cache == &mrt->mfc_cache_array[it->ct])
  2030. rcu_read_unlock();
  2031. }
  2032. static int ipmr_mfc_seq_show(struct seq_file *seq, void *v)
  2033. {
  2034. int n;
  2035. if (v == SEQ_START_TOKEN) {
  2036. seq_puts(seq,
  2037. "Group Origin Iif Pkts Bytes Wrong Oifs\n");
  2038. } else {
  2039. const struct mfc_cache *mfc = v;
  2040. const struct ipmr_mfc_iter *it = seq->private;
  2041. const struct mr_table *mrt = it->mrt;
  2042. seq_printf(seq, "%08X %08X %-3hd",
  2043. (__force u32) mfc->mfc_mcastgrp,
  2044. (__force u32) mfc->mfc_origin,
  2045. mfc->mfc_parent);
  2046. if (it->cache != &mrt->mfc_unres_queue) {
  2047. seq_printf(seq, " %8lu %8lu %8lu",
  2048. mfc->mfc_un.res.pkt,
  2049. mfc->mfc_un.res.bytes,
  2050. mfc->mfc_un.res.wrong_if);
  2051. for (n = mfc->mfc_un.res.minvif;
  2052. n < mfc->mfc_un.res.maxvif; n++) {
  2053. if (VIF_EXISTS(mrt, n) &&
  2054. mfc->mfc_un.res.ttls[n] < 255)
  2055. seq_printf(seq,
  2056. " %2d:%-3d",
  2057. n, mfc->mfc_un.res.ttls[n]);
  2058. }
  2059. } else {
  2060. /* unresolved mfc_caches don't contain
  2061. * pkt, bytes and wrong_if values
  2062. */
  2063. seq_printf(seq, " %8lu %8lu %8lu", 0ul, 0ul, 0ul);
  2064. }
  2065. seq_putc(seq, '\n');
  2066. }
  2067. return 0;
  2068. }
  2069. static const struct seq_operations ipmr_mfc_seq_ops = {
  2070. .start = ipmr_mfc_seq_start,
  2071. .next = ipmr_mfc_seq_next,
  2072. .stop = ipmr_mfc_seq_stop,
  2073. .show = ipmr_mfc_seq_show,
  2074. };
  2075. static int ipmr_mfc_open(struct inode *inode, struct file *file)
  2076. {
  2077. return seq_open_net(inode, file, &ipmr_mfc_seq_ops,
  2078. sizeof(struct ipmr_mfc_iter));
  2079. }
  2080. static const struct file_operations ipmr_mfc_fops = {
  2081. .owner = THIS_MODULE,
  2082. .open = ipmr_mfc_open,
  2083. .read = seq_read,
  2084. .llseek = seq_lseek,
  2085. .release = seq_release_net,
  2086. };
  2087. #endif
  2088. #ifdef CONFIG_IP_PIMSM_V2
  2089. static const struct net_protocol pim_protocol = {
  2090. .handler = pim_rcv,
  2091. .netns_ok = 1,
  2092. };
  2093. #endif
  2094. /*
  2095. * Setup for IP multicast routing
  2096. */
  2097. static int __net_init ipmr_net_init(struct net *net)
  2098. {
  2099. int err;
  2100. err = ipmr_rules_init(net);
  2101. if (err < 0)
  2102. goto fail;
  2103. #ifdef CONFIG_PROC_FS
  2104. err = -ENOMEM;
  2105. if (!proc_net_fops_create(net, "ip_mr_vif", 0, &ipmr_vif_fops))
  2106. goto proc_vif_fail;
  2107. if (!proc_net_fops_create(net, "ip_mr_cache", 0, &ipmr_mfc_fops))
  2108. goto proc_cache_fail;
  2109. #endif
  2110. return 0;
  2111. #ifdef CONFIG_PROC_FS
  2112. proc_cache_fail:
  2113. proc_net_remove(net, "ip_mr_vif");
  2114. proc_vif_fail:
  2115. ipmr_rules_exit(net);
  2116. #endif
  2117. fail:
  2118. return err;
  2119. }
  2120. static void __net_exit ipmr_net_exit(struct net *net)
  2121. {
  2122. #ifdef CONFIG_PROC_FS
  2123. proc_net_remove(net, "ip_mr_cache");
  2124. proc_net_remove(net, "ip_mr_vif");
  2125. #endif
  2126. ipmr_rules_exit(net);
  2127. }
  2128. static struct pernet_operations ipmr_net_ops = {
  2129. .init = ipmr_net_init,
  2130. .exit = ipmr_net_exit,
  2131. };
  2132. int __init ip_mr_init(void)
  2133. {
  2134. int err;
  2135. mrt_cachep = kmem_cache_create("ip_mrt_cache",
  2136. sizeof(struct mfc_cache),
  2137. 0, SLAB_HWCACHE_ALIGN | SLAB_PANIC,
  2138. NULL);
  2139. if (!mrt_cachep)
  2140. return -ENOMEM;
  2141. err = register_pernet_subsys(&ipmr_net_ops);
  2142. if (err)
  2143. goto reg_pernet_fail;
  2144. err = register_netdevice_notifier(&ip_mr_notifier);
  2145. if (err)
  2146. goto reg_notif_fail;
  2147. #ifdef CONFIG_IP_PIMSM_V2
  2148. if (inet_add_protocol(&pim_protocol, IPPROTO_PIM) < 0) {
  2149. pr_err("%s: can't add PIM protocol\n", __func__);
  2150. err = -EAGAIN;
  2151. goto add_proto_fail;
  2152. }
  2153. #endif
  2154. rtnl_register(RTNL_FAMILY_IPMR, RTM_GETROUTE,
  2155. NULL, ipmr_rtm_dumproute, NULL);
  2156. return 0;
  2157. #ifdef CONFIG_IP_PIMSM_V2
  2158. add_proto_fail:
  2159. unregister_netdevice_notifier(&ip_mr_notifier);
  2160. #endif
  2161. reg_notif_fail:
  2162. unregister_pernet_subsys(&ipmr_net_ops);
  2163. reg_pernet_fail:
  2164. kmem_cache_destroy(mrt_cachep);
  2165. return err;
  2166. }