ipmr.c 58 KB

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