ipmr.c 43 KB

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