ipmr.c 57 KB

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