ipmr.c 58 KB

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