ipmr.c 67 KB

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