ipmr.c 57 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545
  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 flowi *flp,
  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, flp, 0, &arg);
  141. if (err < 0)
  142. return err;
  143. *mrt = res.mrt;
  144. return 0;
  145. }
  146. static int ipmr_rule_action(struct fib_rule *rule, struct flowi *flp,
  147. int flags, struct fib_lookup_arg *arg)
  148. {
  149. struct ipmr_result *res = arg->result;
  150. struct mr_table *mrt;
  151. switch (rule->action) {
  152. case FR_ACT_TO_TBL:
  153. break;
  154. case FR_ACT_UNREACHABLE:
  155. return -ENETUNREACH;
  156. case FR_ACT_PROHIBIT:
  157. return -EACCES;
  158. case FR_ACT_BLACKHOLE:
  159. default:
  160. return -EINVAL;
  161. }
  162. mrt = ipmr_get_table(rule->fr_net, rule->table);
  163. if (mrt == NULL)
  164. return -EAGAIN;
  165. res->mrt = mrt;
  166. return 0;
  167. }
  168. static int ipmr_rule_match(struct fib_rule *rule, struct flowi *fl, int flags)
  169. {
  170. return 1;
  171. }
  172. static const struct nla_policy ipmr_rule_policy[FRA_MAX + 1] = {
  173. FRA_GENERIC_POLICY,
  174. };
  175. static int ipmr_rule_configure(struct fib_rule *rule, struct sk_buff *skb,
  176. struct fib_rule_hdr *frh, struct nlattr **tb)
  177. {
  178. return 0;
  179. }
  180. static int ipmr_rule_compare(struct fib_rule *rule, struct fib_rule_hdr *frh,
  181. struct nlattr **tb)
  182. {
  183. return 1;
  184. }
  185. static int ipmr_rule_fill(struct fib_rule *rule, struct sk_buff *skb,
  186. struct fib_rule_hdr *frh)
  187. {
  188. frh->dst_len = 0;
  189. frh->src_len = 0;
  190. frh->tos = 0;
  191. return 0;
  192. }
  193. static const struct fib_rules_ops __net_initdata ipmr_rules_ops_template = {
  194. .family = RTNL_FAMILY_IPMR,
  195. .rule_size = sizeof(struct ipmr_rule),
  196. .addr_size = sizeof(u32),
  197. .action = ipmr_rule_action,
  198. .match = ipmr_rule_match,
  199. .configure = ipmr_rule_configure,
  200. .compare = ipmr_rule_compare,
  201. .default_pref = fib_default_rule_pref,
  202. .fill = ipmr_rule_fill,
  203. .nlgroup = RTNLGRP_IPV4_RULE,
  204. .policy = ipmr_rule_policy,
  205. .owner = THIS_MODULE,
  206. };
  207. static int __net_init ipmr_rules_init(struct net *net)
  208. {
  209. struct fib_rules_ops *ops;
  210. struct mr_table *mrt;
  211. int err;
  212. ops = fib_rules_register(&ipmr_rules_ops_template, net);
  213. if (IS_ERR(ops))
  214. return PTR_ERR(ops);
  215. INIT_LIST_HEAD(&net->ipv4.mr_tables);
  216. mrt = ipmr_new_table(net, RT_TABLE_DEFAULT);
  217. if (mrt == NULL) {
  218. err = -ENOMEM;
  219. goto err1;
  220. }
  221. err = fib_default_rule_add(ops, 0x7fff, RT_TABLE_DEFAULT, 0);
  222. if (err < 0)
  223. goto err2;
  224. net->ipv4.mr_rules_ops = ops;
  225. return 0;
  226. err2:
  227. kfree(mrt);
  228. err1:
  229. fib_rules_unregister(ops);
  230. return err;
  231. }
  232. static void __net_exit ipmr_rules_exit(struct net *net)
  233. {
  234. struct mr_table *mrt, *next;
  235. list_for_each_entry_safe(mrt, next, &net->ipv4.mr_tables, list) {
  236. list_del(&mrt->list);
  237. kfree(mrt);
  238. }
  239. fib_rules_unregister(net->ipv4.mr_rules_ops);
  240. }
  241. #else
  242. #define ipmr_for_each_table(mrt, net) \
  243. for (mrt = net->ipv4.mrt; mrt; mrt = NULL)
  244. static struct mr_table *ipmr_get_table(struct net *net, u32 id)
  245. {
  246. return net->ipv4.mrt;
  247. }
  248. static int ipmr_fib_lookup(struct net *net, struct flowi *flp,
  249. struct mr_table **mrt)
  250. {
  251. *mrt = net->ipv4.mrt;
  252. return 0;
  253. }
  254. static int __net_init ipmr_rules_init(struct net *net)
  255. {
  256. net->ipv4.mrt = ipmr_new_table(net, RT_TABLE_DEFAULT);
  257. return net->ipv4.mrt ? 0 : -ENOMEM;
  258. }
  259. static void __net_exit ipmr_rules_exit(struct net *net)
  260. {
  261. kfree(net->ipv4.mrt);
  262. }
  263. #endif
  264. static struct mr_table *ipmr_new_table(struct net *net, u32 id)
  265. {
  266. struct mr_table *mrt;
  267. unsigned int i;
  268. mrt = ipmr_get_table(net, id);
  269. if (mrt != NULL)
  270. return mrt;
  271. mrt = kzalloc(sizeof(*mrt), GFP_KERNEL);
  272. if (mrt == NULL)
  273. return NULL;
  274. write_pnet(&mrt->net, net);
  275. mrt->id = id;
  276. /* Forwarding cache */
  277. for (i = 0; i < MFC_LINES; i++)
  278. INIT_LIST_HEAD(&mrt->mfc_cache_array[i]);
  279. INIT_LIST_HEAD(&mrt->mfc_unres_queue);
  280. setup_timer(&mrt->ipmr_expire_timer, ipmr_expire_process,
  281. (unsigned long)mrt);
  282. #ifdef CONFIG_IP_PIMSM
  283. mrt->mroute_reg_vif_num = -1;
  284. #endif
  285. #ifdef CONFIG_IP_MROUTE_MULTIPLE_TABLES
  286. list_add_tail_rcu(&mrt->list, &net->ipv4.mr_tables);
  287. #endif
  288. return mrt;
  289. }
  290. /* Service routines creating virtual interfaces: DVMRP tunnels and PIMREG */
  291. static void ipmr_del_tunnel(struct net_device *dev, struct vifctl *v)
  292. {
  293. struct net *net = dev_net(dev);
  294. dev_close(dev);
  295. dev = __dev_get_by_name(net, "tunl0");
  296. if (dev) {
  297. const struct net_device_ops *ops = dev->netdev_ops;
  298. struct ifreq ifr;
  299. struct ip_tunnel_parm p;
  300. memset(&p, 0, sizeof(p));
  301. p.iph.daddr = v->vifc_rmt_addr.s_addr;
  302. p.iph.saddr = v->vifc_lcl_addr.s_addr;
  303. p.iph.version = 4;
  304. p.iph.ihl = 5;
  305. p.iph.protocol = IPPROTO_IPIP;
  306. sprintf(p.name, "dvmrp%d", v->vifc_vifi);
  307. ifr.ifr_ifru.ifru_data = (__force void __user *)&p;
  308. if (ops->ndo_do_ioctl) {
  309. mm_segment_t oldfs = get_fs();
  310. set_fs(KERNEL_DS);
  311. ops->ndo_do_ioctl(dev, &ifr, SIOCDELTUNNEL);
  312. set_fs(oldfs);
  313. }
  314. }
  315. }
  316. static
  317. struct net_device *ipmr_new_tunnel(struct net *net, struct vifctl *v)
  318. {
  319. struct net_device *dev;
  320. dev = __dev_get_by_name(net, "tunl0");
  321. if (dev) {
  322. const struct net_device_ops *ops = dev->netdev_ops;
  323. int err;
  324. struct ifreq ifr;
  325. struct ip_tunnel_parm p;
  326. struct in_device *in_dev;
  327. memset(&p, 0, sizeof(p));
  328. p.iph.daddr = v->vifc_rmt_addr.s_addr;
  329. p.iph.saddr = v->vifc_lcl_addr.s_addr;
  330. p.iph.version = 4;
  331. p.iph.ihl = 5;
  332. p.iph.protocol = IPPROTO_IPIP;
  333. sprintf(p.name, "dvmrp%d", v->vifc_vifi);
  334. ifr.ifr_ifru.ifru_data = (__force void __user *)&p;
  335. if (ops->ndo_do_ioctl) {
  336. mm_segment_t oldfs = get_fs();
  337. set_fs(KERNEL_DS);
  338. err = ops->ndo_do_ioctl(dev, &ifr, SIOCADDTUNNEL);
  339. set_fs(oldfs);
  340. } else {
  341. err = -EOPNOTSUPP;
  342. }
  343. dev = NULL;
  344. if (err == 0 &&
  345. (dev = __dev_get_by_name(net, p.name)) != NULL) {
  346. dev->flags |= IFF_MULTICAST;
  347. in_dev = __in_dev_get_rtnl(dev);
  348. if (in_dev == NULL)
  349. goto failure;
  350. ipv4_devconf_setall(in_dev);
  351. IPV4_DEVCONF(in_dev->cnf, RP_FILTER) = 0;
  352. if (dev_open(dev))
  353. goto failure;
  354. dev_hold(dev);
  355. }
  356. }
  357. return dev;
  358. failure:
  359. /* allow the register to be completed before unregistering. */
  360. rtnl_unlock();
  361. rtnl_lock();
  362. unregister_netdevice(dev);
  363. return NULL;
  364. }
  365. #ifdef CONFIG_IP_PIMSM
  366. static netdev_tx_t reg_vif_xmit(struct sk_buff *skb, struct net_device *dev)
  367. {
  368. struct net *net = dev_net(dev);
  369. struct mr_table *mrt;
  370. struct flowi fl = {
  371. .oif = dev->ifindex,
  372. .iif = skb->skb_iif,
  373. .mark = skb->mark,
  374. };
  375. int err;
  376. err = ipmr_fib_lookup(net, &fl, &mrt);
  377. if (err < 0) {
  378. kfree_skb(skb);
  379. return err;
  380. }
  381. read_lock(&mrt_lock);
  382. dev->stats.tx_bytes += skb->len;
  383. dev->stats.tx_packets++;
  384. ipmr_cache_report(mrt, skb, mrt->mroute_reg_vif_num, IGMPMSG_WHOLEPKT);
  385. read_unlock(&mrt_lock);
  386. kfree_skb(skb);
  387. return NETDEV_TX_OK;
  388. }
  389. static const struct net_device_ops reg_vif_netdev_ops = {
  390. .ndo_start_xmit = reg_vif_xmit,
  391. };
  392. static void reg_vif_setup(struct net_device *dev)
  393. {
  394. dev->type = ARPHRD_PIMREG;
  395. dev->mtu = ETH_DATA_LEN - sizeof(struct iphdr) - 8;
  396. dev->flags = IFF_NOARP;
  397. dev->netdev_ops = &reg_vif_netdev_ops,
  398. dev->destructor = free_netdev;
  399. dev->features |= NETIF_F_NETNS_LOCAL;
  400. }
  401. static struct net_device *ipmr_reg_vif(struct net *net, struct mr_table *mrt)
  402. {
  403. struct net_device *dev;
  404. struct in_device *in_dev;
  405. char name[IFNAMSIZ];
  406. if (mrt->id == RT_TABLE_DEFAULT)
  407. sprintf(name, "pimreg");
  408. else
  409. sprintf(name, "pimreg%u", mrt->id);
  410. dev = alloc_netdev(0, name, reg_vif_setup);
  411. if (dev == NULL)
  412. return NULL;
  413. dev_net_set(dev, net);
  414. if (register_netdevice(dev)) {
  415. free_netdev(dev);
  416. return NULL;
  417. }
  418. dev->iflink = 0;
  419. rcu_read_lock();
  420. in_dev = __in_dev_get_rcu(dev);
  421. if (!in_dev) {
  422. rcu_read_unlock();
  423. goto failure;
  424. }
  425. ipv4_devconf_setall(in_dev);
  426. IPV4_DEVCONF(in_dev->cnf, RP_FILTER) = 0;
  427. rcu_read_unlock();
  428. if (dev_open(dev))
  429. goto failure;
  430. dev_hold(dev);
  431. return dev;
  432. failure:
  433. /* allow the register to be completed before unregistering. */
  434. rtnl_unlock();
  435. rtnl_lock();
  436. unregister_netdevice(dev);
  437. return NULL;
  438. }
  439. #endif
  440. /*
  441. * Delete a VIF entry
  442. * @notify: Set to 1, if the caller is a notifier_call
  443. */
  444. static int vif_delete(struct mr_table *mrt, int vifi, int notify,
  445. struct list_head *head)
  446. {
  447. struct vif_device *v;
  448. struct net_device *dev;
  449. struct in_device *in_dev;
  450. if (vifi < 0 || vifi >= mrt->maxvif)
  451. return -EADDRNOTAVAIL;
  452. v = &mrt->vif_table[vifi];
  453. write_lock_bh(&mrt_lock);
  454. dev = v->dev;
  455. v->dev = NULL;
  456. if (!dev) {
  457. write_unlock_bh(&mrt_lock);
  458. return -EADDRNOTAVAIL;
  459. }
  460. #ifdef CONFIG_IP_PIMSM
  461. if (vifi == mrt->mroute_reg_vif_num)
  462. mrt->mroute_reg_vif_num = -1;
  463. #endif
  464. if (vifi + 1 == mrt->maxvif) {
  465. int tmp;
  466. for (tmp = vifi - 1; tmp >= 0; tmp--) {
  467. if (VIF_EXISTS(mrt, tmp))
  468. break;
  469. }
  470. mrt->maxvif = tmp+1;
  471. }
  472. write_unlock_bh(&mrt_lock);
  473. dev_set_allmulti(dev, -1);
  474. in_dev = __in_dev_get_rtnl(dev);
  475. if (in_dev) {
  476. IPV4_DEVCONF(in_dev->cnf, MC_FORWARDING)--;
  477. ip_rt_multicast_event(in_dev);
  478. }
  479. if (v->flags & (VIFF_TUNNEL | VIFF_REGISTER) && !notify)
  480. unregister_netdevice_queue(dev, head);
  481. dev_put(dev);
  482. return 0;
  483. }
  484. static void ipmr_cache_free_rcu(struct rcu_head *head)
  485. {
  486. struct mfc_cache *c = container_of(head, struct mfc_cache, rcu);
  487. kmem_cache_free(mrt_cachep, c);
  488. }
  489. static inline void ipmr_cache_free(struct mfc_cache *c)
  490. {
  491. call_rcu(&c->rcu, ipmr_cache_free_rcu);
  492. }
  493. /* Destroy an unresolved cache entry, killing queued skbs
  494. * and reporting error to netlink readers.
  495. */
  496. static void ipmr_destroy_unres(struct mr_table *mrt, struct mfc_cache *c)
  497. {
  498. struct net *net = read_pnet(&mrt->net);
  499. struct sk_buff *skb;
  500. struct nlmsgerr *e;
  501. atomic_dec(&mrt->cache_resolve_queue_len);
  502. while ((skb = skb_dequeue(&c->mfc_un.unres.unresolved))) {
  503. if (ip_hdr(skb)->version == 0) {
  504. struct nlmsghdr *nlh = (struct nlmsghdr *)skb_pull(skb, sizeof(struct iphdr));
  505. nlh->nlmsg_type = NLMSG_ERROR;
  506. nlh->nlmsg_len = NLMSG_LENGTH(sizeof(struct nlmsgerr));
  507. skb_trim(skb, nlh->nlmsg_len);
  508. e = NLMSG_DATA(nlh);
  509. e->error = -ETIMEDOUT;
  510. memset(&e->msg, 0, sizeof(e->msg));
  511. rtnl_unicast(skb, net, NETLINK_CB(skb).pid);
  512. } else {
  513. kfree_skb(skb);
  514. }
  515. }
  516. ipmr_cache_free(c);
  517. }
  518. /* Timer process for the unresolved queue. */
  519. static void ipmr_expire_process(unsigned long arg)
  520. {
  521. struct mr_table *mrt = (struct mr_table *)arg;
  522. unsigned long now;
  523. unsigned long expires;
  524. struct mfc_cache *c, *next;
  525. if (!spin_trylock(&mfc_unres_lock)) {
  526. mod_timer(&mrt->ipmr_expire_timer, jiffies+HZ/10);
  527. return;
  528. }
  529. if (list_empty(&mrt->mfc_unres_queue))
  530. goto out;
  531. now = jiffies;
  532. expires = 10*HZ;
  533. list_for_each_entry_safe(c, next, &mrt->mfc_unres_queue, list) {
  534. if (time_after(c->mfc_un.unres.expires, now)) {
  535. unsigned long interval = c->mfc_un.unres.expires - now;
  536. if (interval < expires)
  537. expires = interval;
  538. continue;
  539. }
  540. list_del(&c->list);
  541. ipmr_destroy_unres(mrt, c);
  542. }
  543. if (!list_empty(&mrt->mfc_unres_queue))
  544. mod_timer(&mrt->ipmr_expire_timer, jiffies + expires);
  545. out:
  546. spin_unlock(&mfc_unres_lock);
  547. }
  548. /* Fill oifs list. It is called under write locked mrt_lock. */
  549. static void ipmr_update_thresholds(struct mr_table *mrt, struct mfc_cache *cache,
  550. unsigned char *ttls)
  551. {
  552. int vifi;
  553. cache->mfc_un.res.minvif = MAXVIFS;
  554. cache->mfc_un.res.maxvif = 0;
  555. memset(cache->mfc_un.res.ttls, 255, MAXVIFS);
  556. for (vifi = 0; vifi < mrt->maxvif; vifi++) {
  557. if (VIF_EXISTS(mrt, vifi) &&
  558. ttls[vifi] && ttls[vifi] < 255) {
  559. cache->mfc_un.res.ttls[vifi] = ttls[vifi];
  560. if (cache->mfc_un.res.minvif > vifi)
  561. cache->mfc_un.res.minvif = vifi;
  562. if (cache->mfc_un.res.maxvif <= vifi)
  563. cache->mfc_un.res.maxvif = vifi + 1;
  564. }
  565. }
  566. }
  567. static int vif_add(struct net *net, struct mr_table *mrt,
  568. struct vifctl *vifc, int mrtsock)
  569. {
  570. int vifi = vifc->vifc_vifi;
  571. struct vif_device *v = &mrt->vif_table[vifi];
  572. struct net_device *dev;
  573. struct in_device *in_dev;
  574. int err;
  575. /* Is vif busy ? */
  576. if (VIF_EXISTS(mrt, vifi))
  577. return -EADDRINUSE;
  578. switch (vifc->vifc_flags) {
  579. #ifdef CONFIG_IP_PIMSM
  580. case VIFF_REGISTER:
  581. /*
  582. * Special Purpose VIF in PIM
  583. * All the packets will be sent to the daemon
  584. */
  585. if (mrt->mroute_reg_vif_num >= 0)
  586. return -EADDRINUSE;
  587. dev = ipmr_reg_vif(net, mrt);
  588. if (!dev)
  589. return -ENOBUFS;
  590. err = dev_set_allmulti(dev, 1);
  591. if (err) {
  592. unregister_netdevice(dev);
  593. dev_put(dev);
  594. return err;
  595. }
  596. break;
  597. #endif
  598. case VIFF_TUNNEL:
  599. dev = ipmr_new_tunnel(net, vifc);
  600. if (!dev)
  601. return -ENOBUFS;
  602. err = dev_set_allmulti(dev, 1);
  603. if (err) {
  604. ipmr_del_tunnel(dev, vifc);
  605. dev_put(dev);
  606. return err;
  607. }
  608. break;
  609. case VIFF_USE_IFINDEX:
  610. case 0:
  611. if (vifc->vifc_flags == VIFF_USE_IFINDEX) {
  612. dev = dev_get_by_index(net, vifc->vifc_lcl_ifindex);
  613. if (dev && __in_dev_get_rtnl(dev) == NULL) {
  614. dev_put(dev);
  615. return -EADDRNOTAVAIL;
  616. }
  617. } else {
  618. dev = ip_dev_find(net, vifc->vifc_lcl_addr.s_addr);
  619. }
  620. if (!dev)
  621. return -EADDRNOTAVAIL;
  622. err = dev_set_allmulti(dev, 1);
  623. if (err) {
  624. dev_put(dev);
  625. return err;
  626. }
  627. break;
  628. default:
  629. return -EINVAL;
  630. }
  631. in_dev = __in_dev_get_rtnl(dev);
  632. if (!in_dev) {
  633. dev_put(dev);
  634. return -EADDRNOTAVAIL;
  635. }
  636. IPV4_DEVCONF(in_dev->cnf, MC_FORWARDING)++;
  637. ip_rt_multicast_event(in_dev);
  638. /* Fill in the VIF structures */
  639. v->rate_limit = vifc->vifc_rate_limit;
  640. v->local = vifc->vifc_lcl_addr.s_addr;
  641. v->remote = vifc->vifc_rmt_addr.s_addr;
  642. v->flags = vifc->vifc_flags;
  643. if (!mrtsock)
  644. v->flags |= VIFF_STATIC;
  645. v->threshold = vifc->vifc_threshold;
  646. v->bytes_in = 0;
  647. v->bytes_out = 0;
  648. v->pkt_in = 0;
  649. v->pkt_out = 0;
  650. v->link = dev->ifindex;
  651. if (v->flags & (VIFF_TUNNEL | VIFF_REGISTER))
  652. v->link = dev->iflink;
  653. /* And finish update writing critical data */
  654. write_lock_bh(&mrt_lock);
  655. v->dev = dev;
  656. #ifdef CONFIG_IP_PIMSM
  657. if (v->flags & VIFF_REGISTER)
  658. mrt->mroute_reg_vif_num = vifi;
  659. #endif
  660. if (vifi+1 > mrt->maxvif)
  661. mrt->maxvif = vifi+1;
  662. write_unlock_bh(&mrt_lock);
  663. return 0;
  664. }
  665. /* called with rcu_read_lock() */
  666. static struct mfc_cache *ipmr_cache_find(struct mr_table *mrt,
  667. __be32 origin,
  668. __be32 mcastgrp)
  669. {
  670. int line = MFC_HASH(mcastgrp, origin);
  671. struct mfc_cache *c;
  672. list_for_each_entry_rcu(c, &mrt->mfc_cache_array[line], list) {
  673. if (c->mfc_origin == origin && c->mfc_mcastgrp == mcastgrp)
  674. return c;
  675. }
  676. return NULL;
  677. }
  678. /*
  679. * Allocate a multicast cache entry
  680. */
  681. static struct mfc_cache *ipmr_cache_alloc(void)
  682. {
  683. struct mfc_cache *c = kmem_cache_zalloc(mrt_cachep, GFP_KERNEL);
  684. if (c)
  685. c->mfc_un.res.minvif = MAXVIFS;
  686. return c;
  687. }
  688. static struct mfc_cache *ipmr_cache_alloc_unres(void)
  689. {
  690. struct mfc_cache *c = kmem_cache_zalloc(mrt_cachep, GFP_ATOMIC);
  691. if (c) {
  692. skb_queue_head_init(&c->mfc_un.unres.unresolved);
  693. c->mfc_un.unres.expires = jiffies + 10*HZ;
  694. }
  695. return c;
  696. }
  697. /*
  698. * A cache entry has gone into a resolved state from queued
  699. */
  700. static void ipmr_cache_resolve(struct net *net, struct mr_table *mrt,
  701. struct mfc_cache *uc, struct mfc_cache *c)
  702. {
  703. struct sk_buff *skb;
  704. struct nlmsgerr *e;
  705. /* Play the pending entries through our router */
  706. while ((skb = __skb_dequeue(&uc->mfc_un.unres.unresolved))) {
  707. if (ip_hdr(skb)->version == 0) {
  708. struct nlmsghdr *nlh = (struct nlmsghdr *)skb_pull(skb, sizeof(struct iphdr));
  709. if (__ipmr_fill_mroute(mrt, skb, c, NLMSG_DATA(nlh)) > 0) {
  710. nlh->nlmsg_len = skb_tail_pointer(skb) -
  711. (u8 *)nlh;
  712. } else {
  713. nlh->nlmsg_type = NLMSG_ERROR;
  714. nlh->nlmsg_len = NLMSG_LENGTH(sizeof(struct nlmsgerr));
  715. skb_trim(skb, nlh->nlmsg_len);
  716. e = NLMSG_DATA(nlh);
  717. e->error = -EMSGSIZE;
  718. memset(&e->msg, 0, sizeof(e->msg));
  719. }
  720. rtnl_unicast(skb, net, NETLINK_CB(skb).pid);
  721. } else {
  722. ip_mr_forward(net, mrt, skb, c, 0);
  723. }
  724. }
  725. }
  726. /*
  727. * Bounce a cache query up to mrouted. We could use netlink for this but mrouted
  728. * expects the following bizarre scheme.
  729. *
  730. * Called under mrt_lock.
  731. */
  732. static int ipmr_cache_report(struct mr_table *mrt,
  733. struct sk_buff *pkt, vifi_t vifi, int assert)
  734. {
  735. struct sk_buff *skb;
  736. const int ihl = ip_hdrlen(pkt);
  737. struct igmphdr *igmp;
  738. struct igmpmsg *msg;
  739. struct sock *mroute_sk;
  740. int ret;
  741. #ifdef CONFIG_IP_PIMSM
  742. if (assert == IGMPMSG_WHOLEPKT)
  743. skb = skb_realloc_headroom(pkt, sizeof(struct iphdr));
  744. else
  745. #endif
  746. skb = alloc_skb(128, GFP_ATOMIC);
  747. if (!skb)
  748. return -ENOBUFS;
  749. #ifdef CONFIG_IP_PIMSM
  750. if (assert == IGMPMSG_WHOLEPKT) {
  751. /* Ugly, but we have no choice with this interface.
  752. * Duplicate old header, fix ihl, length etc.
  753. * And all this only to mangle msg->im_msgtype and
  754. * to set msg->im_mbz to "mbz" :-)
  755. */
  756. skb_push(skb, sizeof(struct iphdr));
  757. skb_reset_network_header(skb);
  758. skb_reset_transport_header(skb);
  759. msg = (struct igmpmsg *)skb_network_header(skb);
  760. memcpy(msg, skb_network_header(pkt), sizeof(struct iphdr));
  761. msg->im_msgtype = IGMPMSG_WHOLEPKT;
  762. msg->im_mbz = 0;
  763. msg->im_vif = mrt->mroute_reg_vif_num;
  764. ip_hdr(skb)->ihl = sizeof(struct iphdr) >> 2;
  765. ip_hdr(skb)->tot_len = htons(ntohs(ip_hdr(pkt)->tot_len) +
  766. sizeof(struct iphdr));
  767. } else
  768. #endif
  769. {
  770. /* Copy the IP header */
  771. skb->network_header = skb->tail;
  772. skb_put(skb, ihl);
  773. skb_copy_to_linear_data(skb, pkt->data, ihl);
  774. ip_hdr(skb)->protocol = 0; /* Flag to the kernel this is a route add */
  775. msg = (struct igmpmsg *)skb_network_header(skb);
  776. msg->im_vif = vifi;
  777. skb_dst_set(skb, dst_clone(skb_dst(pkt)));
  778. /* Add our header */
  779. igmp = (struct igmphdr *)skb_put(skb, sizeof(struct igmphdr));
  780. igmp->type =
  781. msg->im_msgtype = assert;
  782. igmp->code = 0;
  783. ip_hdr(skb)->tot_len = htons(skb->len); /* Fix the length */
  784. skb->transport_header = skb->network_header;
  785. }
  786. rcu_read_lock();
  787. mroute_sk = rcu_dereference(mrt->mroute_sk);
  788. if (mroute_sk == NULL) {
  789. rcu_read_unlock();
  790. kfree_skb(skb);
  791. return -EINVAL;
  792. }
  793. /* Deliver to mrouted */
  794. ret = sock_queue_rcv_skb(mroute_sk, skb);
  795. rcu_read_unlock();
  796. if (ret < 0) {
  797. if (net_ratelimit())
  798. printk(KERN_WARNING "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_assign_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_dereference_raw(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_dereference_raw(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. LIST_HEAD(list);
  1290. if (event != NETDEV_UNREGISTER)
  1291. return NOTIFY_DONE;
  1292. ipmr_for_each_table(mrt, net) {
  1293. v = &mrt->vif_table[0];
  1294. for (ct = 0; ct < mrt->maxvif; ct++, v++) {
  1295. if (v->dev == dev)
  1296. vif_delete(mrt, ct, 1, &list);
  1297. }
  1298. }
  1299. unregister_netdevice_many(&list);
  1300. return NOTIFY_DONE;
  1301. }
  1302. static struct notifier_block ip_mr_notifier = {
  1303. .notifier_call = ipmr_device_event,
  1304. };
  1305. /*
  1306. * Encapsulate a packet by attaching a valid IPIP header to it.
  1307. * This avoids tunnel drivers and other mess and gives us the speed so
  1308. * important for multicast video.
  1309. */
  1310. static void ip_encap(struct sk_buff *skb, __be32 saddr, __be32 daddr)
  1311. {
  1312. struct iphdr *iph;
  1313. struct iphdr *old_iph = ip_hdr(skb);
  1314. skb_push(skb, sizeof(struct iphdr));
  1315. skb->transport_header = skb->network_header;
  1316. skb_reset_network_header(skb);
  1317. iph = ip_hdr(skb);
  1318. iph->version = 4;
  1319. iph->tos = old_iph->tos;
  1320. iph->ttl = old_iph->ttl;
  1321. iph->frag_off = 0;
  1322. iph->daddr = daddr;
  1323. iph->saddr = saddr;
  1324. iph->protocol = IPPROTO_IPIP;
  1325. iph->ihl = 5;
  1326. iph->tot_len = htons(skb->len);
  1327. ip_select_ident(iph, skb_dst(skb), NULL);
  1328. ip_send_check(iph);
  1329. memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
  1330. nf_reset(skb);
  1331. }
  1332. static inline int ipmr_forward_finish(struct sk_buff *skb)
  1333. {
  1334. struct ip_options *opt = &(IPCB(skb)->opt);
  1335. IP_INC_STATS_BH(dev_net(skb_dst(skb)->dev), IPSTATS_MIB_OUTFORWDATAGRAMS);
  1336. if (unlikely(opt->optlen))
  1337. ip_forward_options(skb);
  1338. return dst_output(skb);
  1339. }
  1340. /*
  1341. * Processing handlers for ipmr_forward
  1342. */
  1343. static void ipmr_queue_xmit(struct net *net, struct mr_table *mrt,
  1344. struct sk_buff *skb, struct mfc_cache *c, int vifi)
  1345. {
  1346. const struct iphdr *iph = ip_hdr(skb);
  1347. struct vif_device *vif = &mrt->vif_table[vifi];
  1348. struct net_device *dev;
  1349. struct rtable *rt;
  1350. 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. struct flowi fl = {
  1365. .oif = vif->link,
  1366. .fl4_dst = vif->remote,
  1367. .fl4_src = vif->local,
  1368. .fl4_tos = RT_TOS(iph->tos),
  1369. .proto = IPPROTO_IPIP
  1370. };
  1371. if (ip_route_output_key(net, &rt, &fl))
  1372. goto out_free;
  1373. encap = sizeof(struct iphdr);
  1374. } else {
  1375. struct flowi fl = {
  1376. .oif = vif->link,
  1377. .fl4_dst = iph->daddr,
  1378. .fl4_tos = RT_TOS(iph->tos),
  1379. .proto = IPPROTO_IPIP
  1380. };
  1381. if (ip_route_output_key(net, &rt, &fl))
  1382. goto out_free;
  1383. }
  1384. dev = rt->dst.dev;
  1385. if (skb->len+encap > dst_mtu(&rt->dst) && (ntohs(iph->frag_off) & IP_DF)) {
  1386. /* Do not fragment multicasts. Alas, IPv4 does not
  1387. * allow to send ICMP, so that packets will disappear
  1388. * to blackhole.
  1389. */
  1390. IP_INC_STATS_BH(dev_net(dev), IPSTATS_MIB_FRAGFAILS);
  1391. ip_rt_put(rt);
  1392. goto out_free;
  1393. }
  1394. encap += LL_RESERVED_SPACE(dev) + rt->dst.header_len;
  1395. if (skb_cow(skb, encap)) {
  1396. ip_rt_put(rt);
  1397. goto out_free;
  1398. }
  1399. vif->pkt_out++;
  1400. vif->bytes_out += skb->len;
  1401. skb_dst_drop(skb);
  1402. skb_dst_set(skb, &rt->dst);
  1403. ip_decrease_ttl(ip_hdr(skb));
  1404. /* FIXME: forward and output firewalls used to be called here.
  1405. * What do we do with netfilter? -- RR
  1406. */
  1407. if (vif->flags & VIFF_TUNNEL) {
  1408. ip_encap(skb, vif->local, vif->remote);
  1409. /* FIXME: extra output firewall step used to be here. --RR */
  1410. vif->dev->stats.tx_packets++;
  1411. vif->dev->stats.tx_bytes += skb->len;
  1412. }
  1413. IPCB(skb)->flags |= IPSKB_FORWARDED;
  1414. /*
  1415. * RFC1584 teaches, that DVMRP/PIM router must deliver packets locally
  1416. * not only before forwarding, but after forwarding on all output
  1417. * interfaces. It is clear, if mrouter runs a multicasting
  1418. * program, it should receive packets not depending to what interface
  1419. * program is joined.
  1420. * If we will not make it, the program will have to join on all
  1421. * interfaces. On the other hand, multihoming host (or router, but
  1422. * not mrouter) cannot join to more than one interface - it will
  1423. * result in receiving multiple packets.
  1424. */
  1425. NF_HOOK(NFPROTO_IPV4, NF_INET_FORWARD, skb, skb->dev, dev,
  1426. ipmr_forward_finish);
  1427. return;
  1428. out_free:
  1429. kfree_skb(skb);
  1430. }
  1431. static int ipmr_find_vif(struct mr_table *mrt, struct net_device *dev)
  1432. {
  1433. int ct;
  1434. for (ct = mrt->maxvif-1; ct >= 0; ct--) {
  1435. if (mrt->vif_table[ct].dev == dev)
  1436. break;
  1437. }
  1438. return ct;
  1439. }
  1440. /* "local" means that we should preserve one skb (for local delivery) */
  1441. static int ip_mr_forward(struct net *net, struct mr_table *mrt,
  1442. struct sk_buff *skb, struct mfc_cache *cache,
  1443. int local)
  1444. {
  1445. int psend = -1;
  1446. int vif, ct;
  1447. vif = cache->mfc_parent;
  1448. cache->mfc_un.res.pkt++;
  1449. cache->mfc_un.res.bytes += skb->len;
  1450. /*
  1451. * Wrong interface: drop packet and (maybe) send PIM assert.
  1452. */
  1453. if (mrt->vif_table[vif].dev != skb->dev) {
  1454. int true_vifi;
  1455. if (rt_is_output_route(skb_rtable(skb))) {
  1456. /* It is our own packet, looped back.
  1457. * Very complicated situation...
  1458. *
  1459. * The best workaround until routing daemons will be
  1460. * fixed is not to redistribute packet, if it was
  1461. * send through wrong interface. It means, that
  1462. * multicast applications WILL NOT work for
  1463. * (S,G), which have default multicast route pointing
  1464. * to wrong oif. In any case, it is not a good
  1465. * idea to use multicasting applications on router.
  1466. */
  1467. goto dont_forward;
  1468. }
  1469. cache->mfc_un.res.wrong_if++;
  1470. true_vifi = ipmr_find_vif(mrt, skb->dev);
  1471. if (true_vifi >= 0 && mrt->mroute_do_assert &&
  1472. /* pimsm uses asserts, when switching from RPT to SPT,
  1473. * so that we cannot check that packet arrived on an oif.
  1474. * It is bad, but otherwise we would need to move pretty
  1475. * large chunk of pimd to kernel. Ough... --ANK
  1476. */
  1477. (mrt->mroute_do_pim ||
  1478. cache->mfc_un.res.ttls[true_vifi] < 255) &&
  1479. time_after(jiffies,
  1480. cache->mfc_un.res.last_assert + MFC_ASSERT_THRESH)) {
  1481. cache->mfc_un.res.last_assert = jiffies;
  1482. ipmr_cache_report(mrt, skb, true_vifi, IGMPMSG_WRONGVIF);
  1483. }
  1484. goto dont_forward;
  1485. }
  1486. mrt->vif_table[vif].pkt_in++;
  1487. mrt->vif_table[vif].bytes_in += skb->len;
  1488. /*
  1489. * Forward the frame
  1490. */
  1491. for (ct = cache->mfc_un.res.maxvif - 1;
  1492. ct >= cache->mfc_un.res.minvif; ct--) {
  1493. if (ip_hdr(skb)->ttl > cache->mfc_un.res.ttls[ct]) {
  1494. if (psend != -1) {
  1495. struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
  1496. if (skb2)
  1497. ipmr_queue_xmit(net, mrt, skb2, cache,
  1498. psend);
  1499. }
  1500. psend = ct;
  1501. }
  1502. }
  1503. if (psend != -1) {
  1504. if (local) {
  1505. struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
  1506. if (skb2)
  1507. ipmr_queue_xmit(net, mrt, skb2, cache, psend);
  1508. } else {
  1509. ipmr_queue_xmit(net, mrt, skb, cache, psend);
  1510. return 0;
  1511. }
  1512. }
  1513. dont_forward:
  1514. if (!local)
  1515. kfree_skb(skb);
  1516. return 0;
  1517. }
  1518. /*
  1519. * Multicast packets for forwarding arrive here
  1520. * Called with rcu_read_lock();
  1521. */
  1522. int ip_mr_input(struct sk_buff *skb)
  1523. {
  1524. struct mfc_cache *cache;
  1525. struct net *net = dev_net(skb->dev);
  1526. int local = skb_rtable(skb)->rt_flags & RTCF_LOCAL;
  1527. struct mr_table *mrt;
  1528. int err;
  1529. /* Packet is looped back after forward, it should not be
  1530. * forwarded second time, but still can be delivered locally.
  1531. */
  1532. if (IPCB(skb)->flags & IPSKB_FORWARDED)
  1533. goto dont_forward;
  1534. err = ipmr_fib_lookup(net, &skb_rtable(skb)->fl, &mrt);
  1535. if (err < 0) {
  1536. kfree_skb(skb);
  1537. return err;
  1538. }
  1539. if (!local) {
  1540. if (IPCB(skb)->opt.router_alert) {
  1541. if (ip_call_ra_chain(skb))
  1542. return 0;
  1543. } else if (ip_hdr(skb)->protocol == IPPROTO_IGMP) {
  1544. /* IGMPv1 (and broken IGMPv2 implementations sort of
  1545. * Cisco IOS <= 11.2(8)) do not put router alert
  1546. * option to IGMP packets destined to routable
  1547. * groups. It is very bad, because it means
  1548. * that we can forward NO IGMP messages.
  1549. */
  1550. struct sock *mroute_sk;
  1551. mroute_sk = rcu_dereference(mrt->mroute_sk);
  1552. if (mroute_sk) {
  1553. nf_reset(skb);
  1554. raw_rcv(mroute_sk, skb);
  1555. return 0;
  1556. }
  1557. }
  1558. }
  1559. /* already under rcu_read_lock() */
  1560. cache = ipmr_cache_find(mrt, ip_hdr(skb)->saddr, ip_hdr(skb)->daddr);
  1561. /*
  1562. * No usable cache entry
  1563. */
  1564. if (cache == NULL) {
  1565. int vif;
  1566. if (local) {
  1567. struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
  1568. ip_local_deliver(skb);
  1569. if (skb2 == NULL)
  1570. return -ENOBUFS;
  1571. skb = skb2;
  1572. }
  1573. read_lock(&mrt_lock);
  1574. vif = ipmr_find_vif(mrt, skb->dev);
  1575. if (vif >= 0) {
  1576. int err2 = ipmr_cache_unresolved(mrt, vif, skb);
  1577. read_unlock(&mrt_lock);
  1578. return err2;
  1579. }
  1580. read_unlock(&mrt_lock);
  1581. kfree_skb(skb);
  1582. return -ENODEV;
  1583. }
  1584. read_lock(&mrt_lock);
  1585. ip_mr_forward(net, mrt, skb, cache, local);
  1586. read_unlock(&mrt_lock);
  1587. if (local)
  1588. return ip_local_deliver(skb);
  1589. return 0;
  1590. dont_forward:
  1591. if (local)
  1592. return ip_local_deliver(skb);
  1593. kfree_skb(skb);
  1594. return 0;
  1595. }
  1596. #ifdef CONFIG_IP_PIMSM
  1597. /* called with rcu_read_lock() */
  1598. static int __pim_rcv(struct mr_table *mrt, struct sk_buff *skb,
  1599. unsigned int pimlen)
  1600. {
  1601. struct net_device *reg_dev = NULL;
  1602. struct iphdr *encap;
  1603. encap = (struct iphdr *)(skb_transport_header(skb) + pimlen);
  1604. /*
  1605. * Check that:
  1606. * a. packet is really sent to a multicast group
  1607. * b. packet is not a NULL-REGISTER
  1608. * c. packet is not truncated
  1609. */
  1610. if (!ipv4_is_multicast(encap->daddr) ||
  1611. encap->tot_len == 0 ||
  1612. ntohs(encap->tot_len) + pimlen > skb->len)
  1613. return 1;
  1614. read_lock(&mrt_lock);
  1615. if (mrt->mroute_reg_vif_num >= 0)
  1616. reg_dev = mrt->vif_table[mrt->mroute_reg_vif_num].dev;
  1617. read_unlock(&mrt_lock);
  1618. if (reg_dev == NULL)
  1619. return 1;
  1620. skb->mac_header = skb->network_header;
  1621. skb_pull(skb, (u8 *)encap - skb->data);
  1622. skb_reset_network_header(skb);
  1623. skb->protocol = htons(ETH_P_IP);
  1624. skb->ip_summed = CHECKSUM_NONE;
  1625. skb->pkt_type = PACKET_HOST;
  1626. skb_tunnel_rx(skb, reg_dev);
  1627. netif_rx(skb);
  1628. return NET_RX_SUCCESS;
  1629. }
  1630. #endif
  1631. #ifdef CONFIG_IP_PIMSM_V1
  1632. /*
  1633. * Handle IGMP messages of PIMv1
  1634. */
  1635. int pim_rcv_v1(struct sk_buff *skb)
  1636. {
  1637. struct igmphdr *pim;
  1638. struct net *net = dev_net(skb->dev);
  1639. struct mr_table *mrt;
  1640. if (!pskb_may_pull(skb, sizeof(*pim) + sizeof(struct iphdr)))
  1641. goto drop;
  1642. pim = igmp_hdr(skb);
  1643. if (ipmr_fib_lookup(net, &skb_rtable(skb)->fl, &mrt) < 0)
  1644. goto drop;
  1645. if (!mrt->mroute_do_pim ||
  1646. pim->group != PIM_V1_VERSION || pim->code != PIM_V1_REGISTER)
  1647. goto drop;
  1648. if (__pim_rcv(mrt, skb, sizeof(*pim))) {
  1649. drop:
  1650. kfree_skb(skb);
  1651. }
  1652. return 0;
  1653. }
  1654. #endif
  1655. #ifdef CONFIG_IP_PIMSM_V2
  1656. static int pim_rcv(struct sk_buff *skb)
  1657. {
  1658. struct pimreghdr *pim;
  1659. struct net *net = dev_net(skb->dev);
  1660. struct mr_table *mrt;
  1661. if (!pskb_may_pull(skb, sizeof(*pim) + sizeof(struct iphdr)))
  1662. goto drop;
  1663. pim = (struct pimreghdr *)skb_transport_header(skb);
  1664. if (pim->type != ((PIM_VERSION << 4) | (PIM_REGISTER)) ||
  1665. (pim->flags & PIM_NULL_REGISTER) ||
  1666. (ip_compute_csum((void *)pim, sizeof(*pim)) != 0 &&
  1667. csum_fold(skb_checksum(skb, 0, skb->len, 0))))
  1668. goto drop;
  1669. if (ipmr_fib_lookup(net, &skb_rtable(skb)->fl, &mrt) < 0)
  1670. goto drop;
  1671. if (__pim_rcv(mrt, skb, sizeof(*pim))) {
  1672. drop:
  1673. kfree_skb(skb);
  1674. }
  1675. return 0;
  1676. }
  1677. #endif
  1678. static int __ipmr_fill_mroute(struct mr_table *mrt, struct sk_buff *skb,
  1679. struct mfc_cache *c, struct rtmsg *rtm)
  1680. {
  1681. int ct;
  1682. struct rtnexthop *nhp;
  1683. u8 *b = skb_tail_pointer(skb);
  1684. struct rtattr *mp_head;
  1685. /* If cache is unresolved, don't try to parse IIF and OIF */
  1686. if (c->mfc_parent >= MAXVIFS)
  1687. return -ENOENT;
  1688. if (VIF_EXISTS(mrt, c->mfc_parent))
  1689. RTA_PUT(skb, RTA_IIF, 4, &mrt->vif_table[c->mfc_parent].dev->ifindex);
  1690. mp_head = (struct rtattr *)skb_put(skb, RTA_LENGTH(0));
  1691. for (ct = c->mfc_un.res.minvif; ct < c->mfc_un.res.maxvif; ct++) {
  1692. if (VIF_EXISTS(mrt, ct) && c->mfc_un.res.ttls[ct] < 255) {
  1693. if (skb_tailroom(skb) < RTA_ALIGN(RTA_ALIGN(sizeof(*nhp)) + 4))
  1694. goto rtattr_failure;
  1695. nhp = (struct rtnexthop *)skb_put(skb, RTA_ALIGN(sizeof(*nhp)));
  1696. nhp->rtnh_flags = 0;
  1697. nhp->rtnh_hops = c->mfc_un.res.ttls[ct];
  1698. nhp->rtnh_ifindex = mrt->vif_table[ct].dev->ifindex;
  1699. nhp->rtnh_len = sizeof(*nhp);
  1700. }
  1701. }
  1702. mp_head->rta_type = RTA_MULTIPATH;
  1703. mp_head->rta_len = skb_tail_pointer(skb) - (u8 *)mp_head;
  1704. rtm->rtm_type = RTN_MULTICAST;
  1705. return 1;
  1706. rtattr_failure:
  1707. nlmsg_trim(skb, b);
  1708. return -EMSGSIZE;
  1709. }
  1710. int ipmr_get_route(struct net *net,
  1711. struct sk_buff *skb, struct rtmsg *rtm, int nowait)
  1712. {
  1713. int err;
  1714. struct mr_table *mrt;
  1715. struct mfc_cache *cache;
  1716. struct rtable *rt = skb_rtable(skb);
  1717. mrt = ipmr_get_table(net, RT_TABLE_DEFAULT);
  1718. if (mrt == NULL)
  1719. return -ENOENT;
  1720. rcu_read_lock();
  1721. cache = ipmr_cache_find(mrt, rt->rt_src, rt->rt_dst);
  1722. if (cache == NULL) {
  1723. struct sk_buff *skb2;
  1724. struct iphdr *iph;
  1725. struct net_device *dev;
  1726. int vif = -1;
  1727. if (nowait) {
  1728. rcu_read_unlock();
  1729. return -EAGAIN;
  1730. }
  1731. dev = skb->dev;
  1732. read_lock(&mrt_lock);
  1733. if (dev)
  1734. vif = ipmr_find_vif(mrt, dev);
  1735. if (vif < 0) {
  1736. read_unlock(&mrt_lock);
  1737. rcu_read_unlock();
  1738. return -ENODEV;
  1739. }
  1740. skb2 = skb_clone(skb, GFP_ATOMIC);
  1741. if (!skb2) {
  1742. read_unlock(&mrt_lock);
  1743. rcu_read_unlock();
  1744. return -ENOMEM;
  1745. }
  1746. skb_push(skb2, sizeof(struct iphdr));
  1747. skb_reset_network_header(skb2);
  1748. iph = ip_hdr(skb2);
  1749. iph->ihl = sizeof(struct iphdr) >> 2;
  1750. iph->saddr = rt->rt_src;
  1751. iph->daddr = rt->rt_dst;
  1752. iph->version = 0;
  1753. err = ipmr_cache_unresolved(mrt, vif, skb2);
  1754. read_unlock(&mrt_lock);
  1755. rcu_read_unlock();
  1756. return err;
  1757. }
  1758. read_lock(&mrt_lock);
  1759. if (!nowait && (rtm->rtm_flags & RTM_F_NOTIFY))
  1760. cache->mfc_flags |= MFC_NOTIFY;
  1761. err = __ipmr_fill_mroute(mrt, skb, cache, rtm);
  1762. read_unlock(&mrt_lock);
  1763. rcu_read_unlock();
  1764. return err;
  1765. }
  1766. static int ipmr_fill_mroute(struct mr_table *mrt, struct sk_buff *skb,
  1767. u32 pid, u32 seq, struct mfc_cache *c)
  1768. {
  1769. struct nlmsghdr *nlh;
  1770. struct rtmsg *rtm;
  1771. nlh = nlmsg_put(skb, pid, seq, RTM_NEWROUTE, sizeof(*rtm), NLM_F_MULTI);
  1772. if (nlh == NULL)
  1773. return -EMSGSIZE;
  1774. rtm = nlmsg_data(nlh);
  1775. rtm->rtm_family = RTNL_FAMILY_IPMR;
  1776. rtm->rtm_dst_len = 32;
  1777. rtm->rtm_src_len = 32;
  1778. rtm->rtm_tos = 0;
  1779. rtm->rtm_table = mrt->id;
  1780. NLA_PUT_U32(skb, RTA_TABLE, mrt->id);
  1781. rtm->rtm_type = RTN_MULTICAST;
  1782. rtm->rtm_scope = RT_SCOPE_UNIVERSE;
  1783. rtm->rtm_protocol = RTPROT_UNSPEC;
  1784. rtm->rtm_flags = 0;
  1785. NLA_PUT_BE32(skb, RTA_SRC, c->mfc_origin);
  1786. NLA_PUT_BE32(skb, RTA_DST, c->mfc_mcastgrp);
  1787. if (__ipmr_fill_mroute(mrt, skb, c, rtm) < 0)
  1788. goto nla_put_failure;
  1789. return nlmsg_end(skb, nlh);
  1790. nla_put_failure:
  1791. nlmsg_cancel(skb, nlh);
  1792. return -EMSGSIZE;
  1793. }
  1794. static int ipmr_rtm_dumproute(struct sk_buff *skb, struct netlink_callback *cb)
  1795. {
  1796. struct net *net = sock_net(skb->sk);
  1797. struct mr_table *mrt;
  1798. struct mfc_cache *mfc;
  1799. unsigned int t = 0, s_t;
  1800. unsigned int h = 0, s_h;
  1801. unsigned int e = 0, s_e;
  1802. s_t = cb->args[0];
  1803. s_h = cb->args[1];
  1804. s_e = cb->args[2];
  1805. rcu_read_lock();
  1806. ipmr_for_each_table(mrt, net) {
  1807. if (t < s_t)
  1808. goto next_table;
  1809. if (t > s_t)
  1810. s_h = 0;
  1811. for (h = s_h; h < MFC_LINES; h++) {
  1812. list_for_each_entry_rcu(mfc, &mrt->mfc_cache_array[h], list) {
  1813. if (e < s_e)
  1814. goto next_entry;
  1815. if (ipmr_fill_mroute(mrt, skb,
  1816. NETLINK_CB(cb->skb).pid,
  1817. cb->nlh->nlmsg_seq,
  1818. mfc) < 0)
  1819. goto done;
  1820. next_entry:
  1821. e++;
  1822. }
  1823. e = s_e = 0;
  1824. }
  1825. s_h = 0;
  1826. next_table:
  1827. t++;
  1828. }
  1829. done:
  1830. rcu_read_unlock();
  1831. cb->args[2] = e;
  1832. cb->args[1] = h;
  1833. cb->args[0] = t;
  1834. return skb->len;
  1835. }
  1836. #ifdef CONFIG_PROC_FS
  1837. /*
  1838. * The /proc interfaces to multicast routing :
  1839. * /proc/net/ip_mr_cache & /proc/net/ip_mr_vif
  1840. */
  1841. struct ipmr_vif_iter {
  1842. struct seq_net_private p;
  1843. struct mr_table *mrt;
  1844. int ct;
  1845. };
  1846. static struct vif_device *ipmr_vif_seq_idx(struct net *net,
  1847. struct ipmr_vif_iter *iter,
  1848. loff_t pos)
  1849. {
  1850. struct mr_table *mrt = iter->mrt;
  1851. for (iter->ct = 0; iter->ct < mrt->maxvif; ++iter->ct) {
  1852. if (!VIF_EXISTS(mrt, iter->ct))
  1853. continue;
  1854. if (pos-- == 0)
  1855. return &mrt->vif_table[iter->ct];
  1856. }
  1857. return NULL;
  1858. }
  1859. static void *ipmr_vif_seq_start(struct seq_file *seq, loff_t *pos)
  1860. __acquires(mrt_lock)
  1861. {
  1862. struct ipmr_vif_iter *iter = seq->private;
  1863. struct net *net = seq_file_net(seq);
  1864. struct mr_table *mrt;
  1865. mrt = ipmr_get_table(net, RT_TABLE_DEFAULT);
  1866. if (mrt == NULL)
  1867. return ERR_PTR(-ENOENT);
  1868. iter->mrt = mrt;
  1869. read_lock(&mrt_lock);
  1870. return *pos ? ipmr_vif_seq_idx(net, seq->private, *pos - 1)
  1871. : SEQ_START_TOKEN;
  1872. }
  1873. static void *ipmr_vif_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  1874. {
  1875. struct ipmr_vif_iter *iter = seq->private;
  1876. struct net *net = seq_file_net(seq);
  1877. struct mr_table *mrt = iter->mrt;
  1878. ++*pos;
  1879. if (v == SEQ_START_TOKEN)
  1880. return ipmr_vif_seq_idx(net, iter, 0);
  1881. while (++iter->ct < mrt->maxvif) {
  1882. if (!VIF_EXISTS(mrt, iter->ct))
  1883. continue;
  1884. return &mrt->vif_table[iter->ct];
  1885. }
  1886. return NULL;
  1887. }
  1888. static void ipmr_vif_seq_stop(struct seq_file *seq, void *v)
  1889. __releases(mrt_lock)
  1890. {
  1891. read_unlock(&mrt_lock);
  1892. }
  1893. static int ipmr_vif_seq_show(struct seq_file *seq, void *v)
  1894. {
  1895. struct ipmr_vif_iter *iter = seq->private;
  1896. struct mr_table *mrt = iter->mrt;
  1897. if (v == SEQ_START_TOKEN) {
  1898. seq_puts(seq,
  1899. "Interface BytesIn PktsIn BytesOut PktsOut Flags Local Remote\n");
  1900. } else {
  1901. const struct vif_device *vif = v;
  1902. const char *name = vif->dev ? vif->dev->name : "none";
  1903. seq_printf(seq,
  1904. "%2Zd %-10s %8ld %7ld %8ld %7ld %05X %08X %08X\n",
  1905. vif - mrt->vif_table,
  1906. name, vif->bytes_in, vif->pkt_in,
  1907. vif->bytes_out, vif->pkt_out,
  1908. vif->flags, vif->local, vif->remote);
  1909. }
  1910. return 0;
  1911. }
  1912. static const struct seq_operations ipmr_vif_seq_ops = {
  1913. .start = ipmr_vif_seq_start,
  1914. .next = ipmr_vif_seq_next,
  1915. .stop = ipmr_vif_seq_stop,
  1916. .show = ipmr_vif_seq_show,
  1917. };
  1918. static int ipmr_vif_open(struct inode *inode, struct file *file)
  1919. {
  1920. return seq_open_net(inode, file, &ipmr_vif_seq_ops,
  1921. sizeof(struct ipmr_vif_iter));
  1922. }
  1923. static const struct file_operations ipmr_vif_fops = {
  1924. .owner = THIS_MODULE,
  1925. .open = ipmr_vif_open,
  1926. .read = seq_read,
  1927. .llseek = seq_lseek,
  1928. .release = seq_release_net,
  1929. };
  1930. struct ipmr_mfc_iter {
  1931. struct seq_net_private p;
  1932. struct mr_table *mrt;
  1933. struct list_head *cache;
  1934. int ct;
  1935. };
  1936. static struct mfc_cache *ipmr_mfc_seq_idx(struct net *net,
  1937. struct ipmr_mfc_iter *it, loff_t pos)
  1938. {
  1939. struct mr_table *mrt = it->mrt;
  1940. struct mfc_cache *mfc;
  1941. rcu_read_lock();
  1942. for (it->ct = 0; it->ct < MFC_LINES; it->ct++) {
  1943. it->cache = &mrt->mfc_cache_array[it->ct];
  1944. list_for_each_entry_rcu(mfc, it->cache, list)
  1945. if (pos-- == 0)
  1946. return mfc;
  1947. }
  1948. rcu_read_unlock();
  1949. spin_lock_bh(&mfc_unres_lock);
  1950. it->cache = &mrt->mfc_unres_queue;
  1951. list_for_each_entry(mfc, it->cache, list)
  1952. if (pos-- == 0)
  1953. return mfc;
  1954. spin_unlock_bh(&mfc_unres_lock);
  1955. it->cache = NULL;
  1956. return NULL;
  1957. }
  1958. static void *ipmr_mfc_seq_start(struct seq_file *seq, loff_t *pos)
  1959. {
  1960. struct ipmr_mfc_iter *it = seq->private;
  1961. struct net *net = seq_file_net(seq);
  1962. struct mr_table *mrt;
  1963. mrt = ipmr_get_table(net, RT_TABLE_DEFAULT);
  1964. if (mrt == NULL)
  1965. return ERR_PTR(-ENOENT);
  1966. it->mrt = mrt;
  1967. it->cache = NULL;
  1968. it->ct = 0;
  1969. return *pos ? ipmr_mfc_seq_idx(net, seq->private, *pos - 1)
  1970. : SEQ_START_TOKEN;
  1971. }
  1972. static void *ipmr_mfc_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  1973. {
  1974. struct mfc_cache *mfc = v;
  1975. struct ipmr_mfc_iter *it = seq->private;
  1976. struct net *net = seq_file_net(seq);
  1977. struct mr_table *mrt = it->mrt;
  1978. ++*pos;
  1979. if (v == SEQ_START_TOKEN)
  1980. return ipmr_mfc_seq_idx(net, seq->private, 0);
  1981. if (mfc->list.next != it->cache)
  1982. return list_entry(mfc->list.next, struct mfc_cache, list);
  1983. if (it->cache == &mrt->mfc_unres_queue)
  1984. goto end_of_list;
  1985. BUG_ON(it->cache != &mrt->mfc_cache_array[it->ct]);
  1986. while (++it->ct < MFC_LINES) {
  1987. it->cache = &mrt->mfc_cache_array[it->ct];
  1988. if (list_empty(it->cache))
  1989. continue;
  1990. return list_first_entry(it->cache, struct mfc_cache, list);
  1991. }
  1992. /* exhausted cache_array, show unresolved */
  1993. rcu_read_unlock();
  1994. it->cache = &mrt->mfc_unres_queue;
  1995. it->ct = 0;
  1996. spin_lock_bh(&mfc_unres_lock);
  1997. if (!list_empty(it->cache))
  1998. return list_first_entry(it->cache, struct mfc_cache, list);
  1999. end_of_list:
  2000. spin_unlock_bh(&mfc_unres_lock);
  2001. it->cache = NULL;
  2002. return NULL;
  2003. }
  2004. static void ipmr_mfc_seq_stop(struct seq_file *seq, void *v)
  2005. {
  2006. struct ipmr_mfc_iter *it = seq->private;
  2007. struct mr_table *mrt = it->mrt;
  2008. if (it->cache == &mrt->mfc_unres_queue)
  2009. spin_unlock_bh(&mfc_unres_lock);
  2010. else if (it->cache == &mrt->mfc_cache_array[it->ct])
  2011. rcu_read_unlock();
  2012. }
  2013. static int ipmr_mfc_seq_show(struct seq_file *seq, void *v)
  2014. {
  2015. int n;
  2016. if (v == SEQ_START_TOKEN) {
  2017. seq_puts(seq,
  2018. "Group Origin Iif Pkts Bytes Wrong Oifs\n");
  2019. } else {
  2020. const struct mfc_cache *mfc = v;
  2021. const struct ipmr_mfc_iter *it = seq->private;
  2022. const struct mr_table *mrt = it->mrt;
  2023. seq_printf(seq, "%08X %08X %-3hd",
  2024. (__force u32) mfc->mfc_mcastgrp,
  2025. (__force u32) mfc->mfc_origin,
  2026. mfc->mfc_parent);
  2027. if (it->cache != &mrt->mfc_unres_queue) {
  2028. seq_printf(seq, " %8lu %8lu %8lu",
  2029. mfc->mfc_un.res.pkt,
  2030. mfc->mfc_un.res.bytes,
  2031. mfc->mfc_un.res.wrong_if);
  2032. for (n = mfc->mfc_un.res.minvif;
  2033. n < mfc->mfc_un.res.maxvif; n++) {
  2034. if (VIF_EXISTS(mrt, n) &&
  2035. mfc->mfc_un.res.ttls[n] < 255)
  2036. seq_printf(seq,
  2037. " %2d:%-3d",
  2038. n, mfc->mfc_un.res.ttls[n]);
  2039. }
  2040. } else {
  2041. /* unresolved mfc_caches don't contain
  2042. * pkt, bytes and wrong_if values
  2043. */
  2044. seq_printf(seq, " %8lu %8lu %8lu", 0ul, 0ul, 0ul);
  2045. }
  2046. seq_putc(seq, '\n');
  2047. }
  2048. return 0;
  2049. }
  2050. static const struct seq_operations ipmr_mfc_seq_ops = {
  2051. .start = ipmr_mfc_seq_start,
  2052. .next = ipmr_mfc_seq_next,
  2053. .stop = ipmr_mfc_seq_stop,
  2054. .show = ipmr_mfc_seq_show,
  2055. };
  2056. static int ipmr_mfc_open(struct inode *inode, struct file *file)
  2057. {
  2058. return seq_open_net(inode, file, &ipmr_mfc_seq_ops,
  2059. sizeof(struct ipmr_mfc_iter));
  2060. }
  2061. static const struct file_operations ipmr_mfc_fops = {
  2062. .owner = THIS_MODULE,
  2063. .open = ipmr_mfc_open,
  2064. .read = seq_read,
  2065. .llseek = seq_lseek,
  2066. .release = seq_release_net,
  2067. };
  2068. #endif
  2069. #ifdef CONFIG_IP_PIMSM_V2
  2070. static const struct net_protocol pim_protocol = {
  2071. .handler = pim_rcv,
  2072. .netns_ok = 1,
  2073. };
  2074. #endif
  2075. /*
  2076. * Setup for IP multicast routing
  2077. */
  2078. static int __net_init ipmr_net_init(struct net *net)
  2079. {
  2080. int err;
  2081. err = ipmr_rules_init(net);
  2082. if (err < 0)
  2083. goto fail;
  2084. #ifdef CONFIG_PROC_FS
  2085. err = -ENOMEM;
  2086. if (!proc_net_fops_create(net, "ip_mr_vif", 0, &ipmr_vif_fops))
  2087. goto proc_vif_fail;
  2088. if (!proc_net_fops_create(net, "ip_mr_cache", 0, &ipmr_mfc_fops))
  2089. goto proc_cache_fail;
  2090. #endif
  2091. return 0;
  2092. #ifdef CONFIG_PROC_FS
  2093. proc_cache_fail:
  2094. proc_net_remove(net, "ip_mr_vif");
  2095. proc_vif_fail:
  2096. ipmr_rules_exit(net);
  2097. #endif
  2098. fail:
  2099. return err;
  2100. }
  2101. static void __net_exit ipmr_net_exit(struct net *net)
  2102. {
  2103. #ifdef CONFIG_PROC_FS
  2104. proc_net_remove(net, "ip_mr_cache");
  2105. proc_net_remove(net, "ip_mr_vif");
  2106. #endif
  2107. ipmr_rules_exit(net);
  2108. }
  2109. static struct pernet_operations ipmr_net_ops = {
  2110. .init = ipmr_net_init,
  2111. .exit = ipmr_net_exit,
  2112. };
  2113. int __init ip_mr_init(void)
  2114. {
  2115. int err;
  2116. mrt_cachep = kmem_cache_create("ip_mrt_cache",
  2117. sizeof(struct mfc_cache),
  2118. 0, SLAB_HWCACHE_ALIGN | SLAB_PANIC,
  2119. NULL);
  2120. if (!mrt_cachep)
  2121. return -ENOMEM;
  2122. err = register_pernet_subsys(&ipmr_net_ops);
  2123. if (err)
  2124. goto reg_pernet_fail;
  2125. err = register_netdevice_notifier(&ip_mr_notifier);
  2126. if (err)
  2127. goto reg_notif_fail;
  2128. #ifdef CONFIG_IP_PIMSM_V2
  2129. if (inet_add_protocol(&pim_protocol, IPPROTO_PIM) < 0) {
  2130. printk(KERN_ERR "ip_mr_init: can't add PIM protocol\n");
  2131. err = -EAGAIN;
  2132. goto add_proto_fail;
  2133. }
  2134. #endif
  2135. rtnl_register(RTNL_FAMILY_IPMR, RTM_GETROUTE, NULL, ipmr_rtm_dumproute);
  2136. return 0;
  2137. #ifdef CONFIG_IP_PIMSM_V2
  2138. add_proto_fail:
  2139. unregister_netdevice_notifier(&ip_mr_notifier);
  2140. #endif
  2141. reg_notif_fail:
  2142. unregister_pernet_subsys(&ipmr_net_ops);
  2143. reg_pernet_fail:
  2144. kmem_cache_destroy(mrt_cachep);
  2145. return err;
  2146. }