ipmr.c 57 KB

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