ipmr.c 73 KB

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