ipmr.c 47 KB

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