ipmr.c 61 KB

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