ipmr.c 46 KB

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