ipmr.c 59 KB

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