ipmr.c 57 KB

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