ipmr.c 58 KB

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