amt.c 89 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /* Copyright (c) 2021 Taehee Yoo <ap420073@gmail.com> */
  3. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  4. #include <linux/module.h>
  5. #include <linux/skbuff.h>
  6. #include <linux/udp.h>
  7. #include <linux/jhash.h>
  8. #include <linux/if_tunnel.h>
  9. #include <linux/net.h>
  10. #include <linux/igmp.h>
  11. #include <linux/workqueue.h>
  12. #include <net/flow.h>
  13. #include <net/pkt_sched.h>
  14. #include <net/net_namespace.h>
  15. #include <net/ip.h>
  16. #include <net/udp.h>
  17. #include <net/udp_tunnel.h>
  18. #include <net/icmp.h>
  19. #include <net/mld.h>
  20. #include <net/amt.h>
  21. #include <uapi/linux/amt.h>
  22. #include <linux/security.h>
  23. #include <net/gro_cells.h>
  24. #include <net/ipv6.h>
  25. #include <net/if_inet6.h>
  26. #include <net/ndisc.h>
  27. #include <net/addrconf.h>
  28. #include <net/ip6_route.h>
  29. #include <net/inet_common.h>
  30. #include <net/inet_dscp.h>
  31. #include <net/ip6_checksum.h>
  32. static struct workqueue_struct *amt_wq;
  33. static HLIST_HEAD(source_gc_list);
  34. /* Lock for source_gc_list */
  35. static spinlock_t source_gc_lock;
  36. static struct delayed_work source_gc_wq;
  37. static char *status_str[] = {
  38. "AMT_STATUS_INIT",
  39. "AMT_STATUS_SENT_DISCOVERY",
  40. "AMT_STATUS_RECEIVED_DISCOVERY",
  41. "AMT_STATUS_SENT_ADVERTISEMENT",
  42. "AMT_STATUS_RECEIVED_ADVERTISEMENT",
  43. "AMT_STATUS_SENT_REQUEST",
  44. "AMT_STATUS_RECEIVED_REQUEST",
  45. "AMT_STATUS_SENT_QUERY",
  46. "AMT_STATUS_RECEIVED_QUERY",
  47. "AMT_STATUS_SENT_UPDATE",
  48. "AMT_STATUS_RECEIVED_UPDATE",
  49. };
  50. static char *type_str[] = {
  51. "", /* Type 0 is not defined */
  52. "AMT_MSG_DISCOVERY",
  53. "AMT_MSG_ADVERTISEMENT",
  54. "AMT_MSG_REQUEST",
  55. "AMT_MSG_MEMBERSHIP_QUERY",
  56. "AMT_MSG_MEMBERSHIP_UPDATE",
  57. "AMT_MSG_MULTICAST_DATA",
  58. "AMT_MSG_TEARDOWN",
  59. };
  60. static char *action_str[] = {
  61. "AMT_ACT_GMI",
  62. "AMT_ACT_GMI_ZERO",
  63. "AMT_ACT_GT",
  64. "AMT_ACT_STATUS_FWD_NEW",
  65. "AMT_ACT_STATUS_D_FWD_NEW",
  66. "AMT_ACT_STATUS_NONE_NEW",
  67. };
  68. static struct igmpv3_grec igmpv3_zero_grec;
  69. #if IS_ENABLED(CONFIG_IPV6)
  70. #define MLD2_ALL_NODE_INIT { { { 0xff, 0x02, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x01 } } }
  71. static struct in6_addr mld2_all_node = MLD2_ALL_NODE_INIT;
  72. static struct mld2_grec mldv2_zero_grec;
  73. #endif
  74. static struct amt_skb_cb *amt_skb_cb(struct sk_buff *skb)
  75. {
  76. BUILD_BUG_ON(sizeof(struct amt_skb_cb) + sizeof(struct tc_skb_cb) >
  77. sizeof_field(struct sk_buff, cb));
  78. return (struct amt_skb_cb *)((void *)skb->cb +
  79. sizeof(struct tc_skb_cb));
  80. }
  81. static void __amt_source_gc_work(void)
  82. {
  83. struct amt_source_node *snode;
  84. struct hlist_head gc_list;
  85. struct hlist_node *t;
  86. spin_lock_bh(&source_gc_lock);
  87. hlist_move_list(&source_gc_list, &gc_list);
  88. spin_unlock_bh(&source_gc_lock);
  89. hlist_for_each_entry_safe(snode, t, &gc_list, node) {
  90. hlist_del_rcu(&snode->node);
  91. kfree_rcu(snode, rcu);
  92. }
  93. }
  94. static void amt_source_gc_work(struct work_struct *work)
  95. {
  96. __amt_source_gc_work();
  97. spin_lock_bh(&source_gc_lock);
  98. mod_delayed_work(amt_wq, &source_gc_wq,
  99. msecs_to_jiffies(AMT_GC_INTERVAL));
  100. spin_unlock_bh(&source_gc_lock);
  101. }
  102. static bool amt_addr_equal(union amt_addr *a, union amt_addr *b)
  103. {
  104. return !memcmp(a, b, sizeof(union amt_addr));
  105. }
  106. static u32 amt_source_hash(struct amt_tunnel_list *tunnel, union amt_addr *src)
  107. {
  108. u32 hash = jhash(src, sizeof(*src), tunnel->amt->hash_seed);
  109. return reciprocal_scale(hash, tunnel->amt->hash_buckets);
  110. }
  111. static bool amt_status_filter(struct amt_source_node *snode,
  112. enum amt_filter filter)
  113. {
  114. bool rc = false;
  115. switch (filter) {
  116. case AMT_FILTER_FWD:
  117. if (snode->status == AMT_SOURCE_STATUS_FWD &&
  118. snode->flags == AMT_SOURCE_OLD)
  119. rc = true;
  120. break;
  121. case AMT_FILTER_D_FWD:
  122. if (snode->status == AMT_SOURCE_STATUS_D_FWD &&
  123. snode->flags == AMT_SOURCE_OLD)
  124. rc = true;
  125. break;
  126. case AMT_FILTER_FWD_NEW:
  127. if (snode->status == AMT_SOURCE_STATUS_FWD &&
  128. snode->flags == AMT_SOURCE_NEW)
  129. rc = true;
  130. break;
  131. case AMT_FILTER_D_FWD_NEW:
  132. if (snode->status == AMT_SOURCE_STATUS_D_FWD &&
  133. snode->flags == AMT_SOURCE_NEW)
  134. rc = true;
  135. break;
  136. case AMT_FILTER_ALL:
  137. rc = true;
  138. break;
  139. case AMT_FILTER_NONE_NEW:
  140. if (snode->status == AMT_SOURCE_STATUS_NONE &&
  141. snode->flags == AMT_SOURCE_NEW)
  142. rc = true;
  143. break;
  144. case AMT_FILTER_BOTH:
  145. if ((snode->status == AMT_SOURCE_STATUS_D_FWD ||
  146. snode->status == AMT_SOURCE_STATUS_FWD) &&
  147. snode->flags == AMT_SOURCE_OLD)
  148. rc = true;
  149. break;
  150. case AMT_FILTER_BOTH_NEW:
  151. if ((snode->status == AMT_SOURCE_STATUS_D_FWD ||
  152. snode->status == AMT_SOURCE_STATUS_FWD) &&
  153. snode->flags == AMT_SOURCE_NEW)
  154. rc = true;
  155. break;
  156. default:
  157. WARN_ON_ONCE(1);
  158. break;
  159. }
  160. return rc;
  161. }
  162. static struct amt_source_node *amt_lookup_src(struct amt_tunnel_list *tunnel,
  163. struct amt_group_node *gnode,
  164. enum amt_filter filter,
  165. union amt_addr *src)
  166. {
  167. u32 hash = amt_source_hash(tunnel, src);
  168. struct amt_source_node *snode;
  169. hlist_for_each_entry_rcu(snode, &gnode->sources[hash], node)
  170. if (amt_status_filter(snode, filter) &&
  171. amt_addr_equal(&snode->source_addr, src))
  172. return snode;
  173. return NULL;
  174. }
  175. static u32 amt_group_hash(struct amt_tunnel_list *tunnel, union amt_addr *group)
  176. {
  177. u32 hash = jhash(group, sizeof(*group), tunnel->amt->hash_seed);
  178. return reciprocal_scale(hash, tunnel->amt->hash_buckets);
  179. }
  180. static struct amt_group_node *amt_lookup_group(struct amt_tunnel_list *tunnel,
  181. union amt_addr *group,
  182. union amt_addr *host,
  183. bool v6)
  184. {
  185. u32 hash = amt_group_hash(tunnel, group);
  186. struct amt_group_node *gnode;
  187. hlist_for_each_entry_rcu(gnode, &tunnel->groups[hash], node) {
  188. if (amt_addr_equal(&gnode->group_addr, group) &&
  189. amt_addr_equal(&gnode->host_addr, host) &&
  190. gnode->v6 == v6)
  191. return gnode;
  192. }
  193. return NULL;
  194. }
  195. static void amt_destroy_source(struct amt_source_node *snode)
  196. {
  197. struct amt_group_node *gnode = snode->gnode;
  198. struct amt_tunnel_list *tunnel;
  199. tunnel = gnode->tunnel_list;
  200. if (!gnode->v6) {
  201. netdev_dbg(snode->gnode->amt->dev,
  202. "Delete source %pI4 from %pI4\n",
  203. &snode->source_addr.ip4,
  204. &gnode->group_addr.ip4);
  205. #if IS_ENABLED(CONFIG_IPV6)
  206. } else {
  207. netdev_dbg(snode->gnode->amt->dev,
  208. "Delete source %pI6 from %pI6\n",
  209. &snode->source_addr.ip6,
  210. &gnode->group_addr.ip6);
  211. #endif
  212. }
  213. cancel_delayed_work(&snode->source_timer);
  214. hlist_del_init_rcu(&snode->node);
  215. tunnel->nr_sources--;
  216. gnode->nr_sources--;
  217. spin_lock_bh(&source_gc_lock);
  218. hlist_add_head_rcu(&snode->node, &source_gc_list);
  219. spin_unlock_bh(&source_gc_lock);
  220. }
  221. static void amt_del_group(struct amt_dev *amt, struct amt_group_node *gnode)
  222. {
  223. struct amt_source_node *snode;
  224. struct hlist_node *t;
  225. int i;
  226. if (cancel_delayed_work(&gnode->group_timer))
  227. dev_put(amt->dev);
  228. hlist_del_rcu(&gnode->node);
  229. gnode->tunnel_list->nr_groups--;
  230. if (!gnode->v6)
  231. netdev_dbg(amt->dev, "Leave group %pI4\n",
  232. &gnode->group_addr.ip4);
  233. #if IS_ENABLED(CONFIG_IPV6)
  234. else
  235. netdev_dbg(amt->dev, "Leave group %pI6\n",
  236. &gnode->group_addr.ip6);
  237. #endif
  238. for (i = 0; i < amt->hash_buckets; i++)
  239. hlist_for_each_entry_safe(snode, t, &gnode->sources[i], node)
  240. amt_destroy_source(snode);
  241. /* tunnel->lock was acquired outside of amt_del_group()
  242. * But rcu_read_lock() was acquired too so It's safe.
  243. */
  244. kfree_rcu(gnode, rcu);
  245. }
  246. /* If a source timer expires with a router filter-mode for the group of
  247. * INCLUDE, the router concludes that traffic from this particular
  248. * source is no longer desired on the attached network, and deletes the
  249. * associated source record.
  250. */
  251. static void amt_source_work(struct work_struct *work)
  252. {
  253. struct amt_source_node *snode = container_of(to_delayed_work(work),
  254. struct amt_source_node,
  255. source_timer);
  256. struct amt_group_node *gnode = snode->gnode;
  257. struct amt_dev *amt = gnode->amt;
  258. struct amt_tunnel_list *tunnel;
  259. tunnel = gnode->tunnel_list;
  260. spin_lock_bh(&tunnel->lock);
  261. rcu_read_lock();
  262. if (gnode->filter_mode == MCAST_INCLUDE) {
  263. amt_destroy_source(snode);
  264. if (!gnode->nr_sources)
  265. amt_del_group(amt, gnode);
  266. } else {
  267. /* When a router filter-mode for a group is EXCLUDE,
  268. * source records are only deleted when the group timer expires
  269. */
  270. snode->status = AMT_SOURCE_STATUS_D_FWD;
  271. }
  272. rcu_read_unlock();
  273. spin_unlock_bh(&tunnel->lock);
  274. }
  275. static void amt_act_src(struct amt_tunnel_list *tunnel,
  276. struct amt_group_node *gnode,
  277. struct amt_source_node *snode,
  278. enum amt_act act)
  279. {
  280. struct amt_dev *amt = tunnel->amt;
  281. switch (act) {
  282. case AMT_ACT_GMI:
  283. mod_delayed_work(amt_wq, &snode->source_timer,
  284. msecs_to_jiffies(amt_gmi(amt)));
  285. break;
  286. case AMT_ACT_GMI_ZERO:
  287. cancel_delayed_work(&snode->source_timer);
  288. break;
  289. case AMT_ACT_GT:
  290. mod_delayed_work(amt_wq, &snode->source_timer,
  291. gnode->group_timer.timer.expires);
  292. break;
  293. case AMT_ACT_STATUS_FWD_NEW:
  294. snode->status = AMT_SOURCE_STATUS_FWD;
  295. snode->flags = AMT_SOURCE_NEW;
  296. break;
  297. case AMT_ACT_STATUS_D_FWD_NEW:
  298. snode->status = AMT_SOURCE_STATUS_D_FWD;
  299. snode->flags = AMT_SOURCE_NEW;
  300. break;
  301. case AMT_ACT_STATUS_NONE_NEW:
  302. cancel_delayed_work(&snode->source_timer);
  303. snode->status = AMT_SOURCE_STATUS_NONE;
  304. snode->flags = AMT_SOURCE_NEW;
  305. break;
  306. default:
  307. WARN_ON_ONCE(1);
  308. return;
  309. }
  310. if (!gnode->v6)
  311. netdev_dbg(amt->dev, "Source %pI4 from %pI4 Acted %s\n",
  312. &snode->source_addr.ip4,
  313. &gnode->group_addr.ip4,
  314. action_str[act]);
  315. #if IS_ENABLED(CONFIG_IPV6)
  316. else
  317. netdev_dbg(amt->dev, "Source %pI6 from %pI6 Acted %s\n",
  318. &snode->source_addr.ip6,
  319. &gnode->group_addr.ip6,
  320. action_str[act]);
  321. #endif
  322. }
  323. static struct amt_source_node *amt_alloc_snode(struct amt_group_node *gnode,
  324. union amt_addr *src)
  325. {
  326. struct amt_source_node *snode;
  327. snode = kzalloc_obj(*snode, GFP_ATOMIC);
  328. if (!snode)
  329. return NULL;
  330. memcpy(&snode->source_addr, src, sizeof(union amt_addr));
  331. snode->gnode = gnode;
  332. snode->status = AMT_SOURCE_STATUS_NONE;
  333. snode->flags = AMT_SOURCE_NEW;
  334. INIT_HLIST_NODE(&snode->node);
  335. INIT_DELAYED_WORK(&snode->source_timer, amt_source_work);
  336. return snode;
  337. }
  338. /* RFC 3810 - 7.2.2. Definition of Filter Timers
  339. *
  340. * Router Mode Filter Timer Actions/Comments
  341. * ----------- ----------------- ----------------
  342. *
  343. * INCLUDE Not Used All listeners in
  344. * INCLUDE mode.
  345. *
  346. * EXCLUDE Timer > 0 At least one listener
  347. * in EXCLUDE mode.
  348. *
  349. * EXCLUDE Timer == 0 No more listeners in
  350. * EXCLUDE mode for the
  351. * multicast address.
  352. * If the Requested List
  353. * is empty, delete
  354. * Multicast Address
  355. * Record. If not, switch
  356. * to INCLUDE filter mode;
  357. * the sources in the
  358. * Requested List are
  359. * moved to the Include
  360. * List, and the Exclude
  361. * List is deleted.
  362. */
  363. static void amt_group_work(struct work_struct *work)
  364. {
  365. struct amt_group_node *gnode = container_of(to_delayed_work(work),
  366. struct amt_group_node,
  367. group_timer);
  368. struct amt_tunnel_list *tunnel = gnode->tunnel_list;
  369. struct amt_dev *amt = gnode->amt;
  370. struct amt_source_node *snode;
  371. bool delete_group = true;
  372. struct hlist_node *t;
  373. int i, buckets;
  374. buckets = amt->hash_buckets;
  375. spin_lock_bh(&tunnel->lock);
  376. if (gnode->filter_mode == MCAST_INCLUDE) {
  377. /* Not Used */
  378. spin_unlock_bh(&tunnel->lock);
  379. goto out;
  380. }
  381. rcu_read_lock();
  382. for (i = 0; i < buckets; i++) {
  383. hlist_for_each_entry_safe(snode, t,
  384. &gnode->sources[i], node) {
  385. if (!delayed_work_pending(&snode->source_timer) ||
  386. snode->status == AMT_SOURCE_STATUS_D_FWD) {
  387. amt_destroy_source(snode);
  388. } else {
  389. delete_group = false;
  390. snode->status = AMT_SOURCE_STATUS_FWD;
  391. }
  392. }
  393. }
  394. if (delete_group)
  395. amt_del_group(amt, gnode);
  396. else
  397. gnode->filter_mode = MCAST_INCLUDE;
  398. rcu_read_unlock();
  399. spin_unlock_bh(&tunnel->lock);
  400. out:
  401. dev_put(amt->dev);
  402. }
  403. /* Non-existent group is created as INCLUDE {empty}:
  404. *
  405. * RFC 3376 - 5.1. Action on Change of Interface State
  406. *
  407. * If no interface state existed for that multicast address before
  408. * the change (i.e., the change consisted of creating a new
  409. * per-interface record), or if no state exists after the change
  410. * (i.e., the change consisted of deleting a per-interface record),
  411. * then the "non-existent" state is considered to have a filter mode
  412. * of INCLUDE and an empty source list.
  413. */
  414. static struct amt_group_node *amt_add_group(struct amt_dev *amt,
  415. struct amt_tunnel_list *tunnel,
  416. union amt_addr *group,
  417. union amt_addr *host,
  418. bool v6)
  419. {
  420. struct amt_group_node *gnode;
  421. u32 hash;
  422. int i;
  423. if (tunnel->nr_groups >= amt->max_groups)
  424. return ERR_PTR(-ENOSPC);
  425. gnode = kzalloc(sizeof(*gnode) +
  426. (sizeof(struct hlist_head) * amt->hash_buckets),
  427. GFP_ATOMIC);
  428. if (unlikely(!gnode))
  429. return ERR_PTR(-ENOMEM);
  430. gnode->amt = amt;
  431. gnode->group_addr = *group;
  432. gnode->host_addr = *host;
  433. gnode->v6 = v6;
  434. gnode->tunnel_list = tunnel;
  435. gnode->filter_mode = MCAST_INCLUDE;
  436. INIT_HLIST_NODE(&gnode->node);
  437. INIT_DELAYED_WORK(&gnode->group_timer, amt_group_work);
  438. for (i = 0; i < amt->hash_buckets; i++)
  439. INIT_HLIST_HEAD(&gnode->sources[i]);
  440. hash = amt_group_hash(tunnel, group);
  441. hlist_add_head_rcu(&gnode->node, &tunnel->groups[hash]);
  442. tunnel->nr_groups++;
  443. if (!gnode->v6)
  444. netdev_dbg(amt->dev, "Join group %pI4\n",
  445. &gnode->group_addr.ip4);
  446. #if IS_ENABLED(CONFIG_IPV6)
  447. else
  448. netdev_dbg(amt->dev, "Join group %pI6\n",
  449. &gnode->group_addr.ip6);
  450. #endif
  451. return gnode;
  452. }
  453. static struct sk_buff *amt_build_igmp_gq(struct amt_dev *amt)
  454. {
  455. u8 ra[AMT_IPHDR_OPTS] = { IPOPT_RA, 4, 0, 0 };
  456. int hlen = LL_RESERVED_SPACE(amt->dev);
  457. int tlen = amt->dev->needed_tailroom;
  458. struct igmpv3_query *ihv3;
  459. void *csum_start = NULL;
  460. __sum16 *csum = NULL;
  461. struct sk_buff *skb;
  462. struct ethhdr *eth;
  463. struct iphdr *iph;
  464. unsigned int len;
  465. int offset;
  466. len = hlen + tlen + sizeof(*iph) + AMT_IPHDR_OPTS + sizeof(*ihv3);
  467. skb = netdev_alloc_skb_ip_align(amt->dev, len);
  468. if (!skb)
  469. return NULL;
  470. skb_reserve(skb, hlen);
  471. skb_push(skb, sizeof(*eth));
  472. skb->protocol = htons(ETH_P_IP);
  473. skb_reset_mac_header(skb);
  474. skb->priority = TC_PRIO_CONTROL;
  475. skb_put(skb, sizeof(*iph));
  476. skb_put_data(skb, ra, sizeof(ra));
  477. skb_put(skb, sizeof(*ihv3));
  478. skb_pull(skb, sizeof(*eth));
  479. skb_reset_network_header(skb);
  480. iph = ip_hdr(skb);
  481. iph->version = 4;
  482. iph->ihl = (sizeof(struct iphdr) + AMT_IPHDR_OPTS) >> 2;
  483. iph->tos = AMT_TOS;
  484. iph->tot_len = htons(sizeof(*iph) + AMT_IPHDR_OPTS + sizeof(*ihv3));
  485. iph->frag_off = htons(IP_DF);
  486. iph->ttl = 1;
  487. iph->id = 0;
  488. iph->protocol = IPPROTO_IGMP;
  489. iph->daddr = htonl(INADDR_ALLHOSTS_GROUP);
  490. iph->saddr = htonl(INADDR_ANY);
  491. ip_send_check(iph);
  492. eth = eth_hdr(skb);
  493. ether_addr_copy(eth->h_source, amt->dev->dev_addr);
  494. ip_eth_mc_map(htonl(INADDR_ALLHOSTS_GROUP), eth->h_dest);
  495. eth->h_proto = htons(ETH_P_IP);
  496. ihv3 = skb_pull(skb, sizeof(*iph) + AMT_IPHDR_OPTS);
  497. skb_reset_transport_header(skb);
  498. ihv3->type = IGMP_HOST_MEMBERSHIP_QUERY;
  499. ihv3->code = 1;
  500. ihv3->group = 0;
  501. ihv3->qqic = amt->qi;
  502. ihv3->nsrcs = 0;
  503. ihv3->resv = 0;
  504. ihv3->suppress = false;
  505. ihv3->qrv = READ_ONCE(amt->net->ipv4.sysctl_igmp_qrv);
  506. ihv3->csum = 0;
  507. csum = &ihv3->csum;
  508. csum_start = (void *)ihv3;
  509. *csum = ip_compute_csum(csum_start, sizeof(*ihv3));
  510. offset = skb_transport_offset(skb);
  511. skb->csum = skb_checksum(skb, offset, skb->len - offset, 0);
  512. skb->ip_summed = CHECKSUM_NONE;
  513. skb_push(skb, sizeof(*eth) + sizeof(*iph) + AMT_IPHDR_OPTS);
  514. return skb;
  515. }
  516. static void amt_update_gw_status(struct amt_dev *amt, enum amt_status status,
  517. bool validate)
  518. {
  519. if (validate && amt->status >= status)
  520. return;
  521. netdev_dbg(amt->dev, "Update GW status %s -> %s",
  522. status_str[amt->status], status_str[status]);
  523. WRITE_ONCE(amt->status, status);
  524. }
  525. static void __amt_update_relay_status(struct amt_tunnel_list *tunnel,
  526. enum amt_status status,
  527. bool validate)
  528. {
  529. if (validate && tunnel->status >= status)
  530. return;
  531. netdev_dbg(tunnel->amt->dev,
  532. "Update Tunnel(IP = %pI4, PORT = %u) status %s -> %s",
  533. &tunnel->ip4, ntohs(tunnel->source_port),
  534. status_str[tunnel->status], status_str[status]);
  535. tunnel->status = status;
  536. }
  537. static void amt_update_relay_status(struct amt_tunnel_list *tunnel,
  538. enum amt_status status, bool validate)
  539. {
  540. spin_lock_bh(&tunnel->lock);
  541. __amt_update_relay_status(tunnel, status, validate);
  542. spin_unlock_bh(&tunnel->lock);
  543. }
  544. static void amt_send_discovery(struct amt_dev *amt)
  545. {
  546. struct amt_header_discovery *amtd;
  547. int hlen, tlen, offset;
  548. struct socket *sock;
  549. struct udphdr *udph;
  550. struct sk_buff *skb;
  551. struct iphdr *iph;
  552. struct rtable *rt;
  553. struct flowi4 fl4;
  554. u32 len;
  555. int err;
  556. rcu_read_lock();
  557. sock = rcu_dereference(amt->sock);
  558. if (!sock)
  559. goto out;
  560. if (!netif_running(amt->stream_dev) || !netif_running(amt->dev))
  561. goto out;
  562. rt = ip_route_output_ports(amt->net, &fl4, sock->sk,
  563. amt->discovery_ip, amt->local_ip,
  564. amt->gw_port, amt->relay_port,
  565. IPPROTO_UDP, 0,
  566. amt->stream_dev->ifindex);
  567. if (IS_ERR(rt)) {
  568. amt->dev->stats.tx_errors++;
  569. goto out;
  570. }
  571. hlen = LL_RESERVED_SPACE(amt->dev);
  572. tlen = amt->dev->needed_tailroom;
  573. len = hlen + tlen + sizeof(*iph) + sizeof(*udph) + sizeof(*amtd);
  574. skb = netdev_alloc_skb_ip_align(amt->dev, len);
  575. if (!skb) {
  576. ip_rt_put(rt);
  577. amt->dev->stats.tx_errors++;
  578. goto out;
  579. }
  580. skb->priority = TC_PRIO_CONTROL;
  581. skb_dst_set(skb, &rt->dst);
  582. len = sizeof(*iph) + sizeof(*udph) + sizeof(*amtd);
  583. skb_reset_network_header(skb);
  584. skb_put(skb, len);
  585. amtd = skb_pull(skb, sizeof(*iph) + sizeof(*udph));
  586. amtd->version = 0;
  587. amtd->type = AMT_MSG_DISCOVERY;
  588. amtd->reserved = 0;
  589. amtd->nonce = amt->nonce;
  590. skb_push(skb, sizeof(*udph));
  591. skb_reset_transport_header(skb);
  592. udph = udp_hdr(skb);
  593. udph->source = amt->gw_port;
  594. udph->dest = amt->relay_port;
  595. udph->len = htons(sizeof(*udph) + sizeof(*amtd));
  596. udph->check = 0;
  597. offset = skb_transport_offset(skb);
  598. skb->csum = skb_checksum(skb, offset, skb->len - offset, 0);
  599. udph->check = csum_tcpudp_magic(amt->local_ip, amt->discovery_ip,
  600. sizeof(*udph) + sizeof(*amtd),
  601. IPPROTO_UDP, skb->csum);
  602. skb_push(skb, sizeof(*iph));
  603. iph = ip_hdr(skb);
  604. iph->version = 4;
  605. iph->ihl = (sizeof(struct iphdr)) >> 2;
  606. iph->tos = AMT_TOS;
  607. iph->frag_off = 0;
  608. iph->ttl = ip4_dst_hoplimit(&rt->dst);
  609. iph->daddr = amt->discovery_ip;
  610. iph->saddr = amt->local_ip;
  611. iph->protocol = IPPROTO_UDP;
  612. iph->tot_len = htons(len);
  613. skb->ip_summed = CHECKSUM_NONE;
  614. ip_select_ident(amt->net, skb, NULL);
  615. ip_send_check(iph);
  616. err = ip_local_out(amt->net, sock->sk, skb);
  617. if (unlikely(net_xmit_eval(err)))
  618. amt->dev->stats.tx_errors++;
  619. amt_update_gw_status(amt, AMT_STATUS_SENT_DISCOVERY, true);
  620. out:
  621. rcu_read_unlock();
  622. }
  623. static void amt_send_request(struct amt_dev *amt, bool v6)
  624. {
  625. struct amt_header_request *amtrh;
  626. int hlen, tlen, offset;
  627. struct socket *sock;
  628. struct udphdr *udph;
  629. struct sk_buff *skb;
  630. struct iphdr *iph;
  631. struct rtable *rt;
  632. struct flowi4 fl4;
  633. u32 len;
  634. int err;
  635. rcu_read_lock();
  636. sock = rcu_dereference(amt->sock);
  637. if (!sock)
  638. goto out;
  639. if (!netif_running(amt->stream_dev) || !netif_running(amt->dev))
  640. goto out;
  641. rt = ip_route_output_ports(amt->net, &fl4, sock->sk,
  642. amt->remote_ip, amt->local_ip,
  643. amt->gw_port, amt->relay_port,
  644. IPPROTO_UDP, 0,
  645. amt->stream_dev->ifindex);
  646. if (IS_ERR(rt)) {
  647. amt->dev->stats.tx_errors++;
  648. goto out;
  649. }
  650. hlen = LL_RESERVED_SPACE(amt->dev);
  651. tlen = amt->dev->needed_tailroom;
  652. len = hlen + tlen + sizeof(*iph) + sizeof(*udph) + sizeof(*amtrh);
  653. skb = netdev_alloc_skb_ip_align(amt->dev, len);
  654. if (!skb) {
  655. ip_rt_put(rt);
  656. amt->dev->stats.tx_errors++;
  657. goto out;
  658. }
  659. skb->priority = TC_PRIO_CONTROL;
  660. skb_dst_set(skb, &rt->dst);
  661. len = sizeof(*iph) + sizeof(*udph) + sizeof(*amtrh);
  662. skb_reset_network_header(skb);
  663. skb_put(skb, len);
  664. amtrh = skb_pull(skb, sizeof(*iph) + sizeof(*udph));
  665. amtrh->version = 0;
  666. amtrh->type = AMT_MSG_REQUEST;
  667. amtrh->reserved1 = 0;
  668. amtrh->p = v6;
  669. amtrh->reserved2 = 0;
  670. amtrh->nonce = amt->nonce;
  671. skb_push(skb, sizeof(*udph));
  672. skb_reset_transport_header(skb);
  673. udph = udp_hdr(skb);
  674. udph->source = amt->gw_port;
  675. udph->dest = amt->relay_port;
  676. udph->len = htons(sizeof(*amtrh) + sizeof(*udph));
  677. udph->check = 0;
  678. offset = skb_transport_offset(skb);
  679. skb->csum = skb_checksum(skb, offset, skb->len - offset, 0);
  680. udph->check = csum_tcpudp_magic(amt->local_ip, amt->remote_ip,
  681. sizeof(*udph) + sizeof(*amtrh),
  682. IPPROTO_UDP, skb->csum);
  683. skb_push(skb, sizeof(*iph));
  684. iph = ip_hdr(skb);
  685. iph->version = 4;
  686. iph->ihl = (sizeof(struct iphdr)) >> 2;
  687. iph->tos = AMT_TOS;
  688. iph->frag_off = 0;
  689. iph->ttl = ip4_dst_hoplimit(&rt->dst);
  690. iph->daddr = amt->remote_ip;
  691. iph->saddr = amt->local_ip;
  692. iph->protocol = IPPROTO_UDP;
  693. iph->tot_len = htons(len);
  694. skb->ip_summed = CHECKSUM_NONE;
  695. ip_select_ident(amt->net, skb, NULL);
  696. ip_send_check(iph);
  697. err = ip_local_out(amt->net, sock->sk, skb);
  698. if (unlikely(net_xmit_eval(err)))
  699. amt->dev->stats.tx_errors++;
  700. out:
  701. rcu_read_unlock();
  702. }
  703. static void amt_send_igmp_gq(struct amt_dev *amt,
  704. struct amt_tunnel_list *tunnel)
  705. {
  706. struct sk_buff *skb;
  707. skb = amt_build_igmp_gq(amt);
  708. if (!skb)
  709. return;
  710. amt_skb_cb(skb)->tunnel = tunnel;
  711. dev_queue_xmit(skb);
  712. }
  713. #if IS_ENABLED(CONFIG_IPV6)
  714. static struct sk_buff *amt_build_mld_gq(struct amt_dev *amt)
  715. {
  716. u8 ra[AMT_IP6HDR_OPTS] = { IPPROTO_ICMPV6, 0, IPV6_TLV_ROUTERALERT,
  717. 2, 0, 0, IPV6_TLV_PAD1, IPV6_TLV_PAD1 };
  718. int hlen = LL_RESERVED_SPACE(amt->dev);
  719. int tlen = amt->dev->needed_tailroom;
  720. struct mld2_query *mld2q;
  721. void *csum_start = NULL;
  722. struct ipv6hdr *ip6h;
  723. struct sk_buff *skb;
  724. struct ethhdr *eth;
  725. u32 len;
  726. len = hlen + tlen + sizeof(*ip6h) + sizeof(ra) + sizeof(*mld2q);
  727. skb = netdev_alloc_skb_ip_align(amt->dev, len);
  728. if (!skb)
  729. return NULL;
  730. skb_reserve(skb, hlen);
  731. skb_push(skb, sizeof(*eth));
  732. skb_reset_mac_header(skb);
  733. eth = eth_hdr(skb);
  734. skb->priority = TC_PRIO_CONTROL;
  735. skb->protocol = htons(ETH_P_IPV6);
  736. skb_put_zero(skb, sizeof(*ip6h));
  737. skb_put_data(skb, ra, sizeof(ra));
  738. skb_put_zero(skb, sizeof(*mld2q));
  739. skb_pull(skb, sizeof(*eth));
  740. skb_reset_network_header(skb);
  741. ip6h = ipv6_hdr(skb);
  742. ip6h->payload_len = htons(sizeof(ra) + sizeof(*mld2q));
  743. ip6h->nexthdr = NEXTHDR_HOP;
  744. ip6h->hop_limit = 1;
  745. ip6h->daddr = mld2_all_node;
  746. ip6_flow_hdr(ip6h, 0, 0);
  747. if (ipv6_dev_get_saddr(amt->net, amt->dev, &ip6h->daddr, 0,
  748. &ip6h->saddr)) {
  749. amt->dev->stats.tx_errors++;
  750. kfree_skb(skb);
  751. return NULL;
  752. }
  753. eth->h_proto = htons(ETH_P_IPV6);
  754. ether_addr_copy(eth->h_source, amt->dev->dev_addr);
  755. ipv6_eth_mc_map(&mld2_all_node, eth->h_dest);
  756. skb_pull(skb, sizeof(*ip6h) + sizeof(ra));
  757. skb_reset_transport_header(skb);
  758. mld2q = (struct mld2_query *)icmp6_hdr(skb);
  759. mld2q->mld2q_mrc = htons(1);
  760. mld2q->mld2q_type = ICMPV6_MGM_QUERY;
  761. mld2q->mld2q_code = 0;
  762. mld2q->mld2q_cksum = 0;
  763. mld2q->mld2q_resv1 = 0;
  764. mld2q->mld2q_resv2 = 0;
  765. mld2q->mld2q_suppress = 0;
  766. mld2q->mld2q_qrv = amt->qrv;
  767. mld2q->mld2q_nsrcs = 0;
  768. mld2q->mld2q_qqic = amt->qi;
  769. csum_start = (void *)mld2q;
  770. mld2q->mld2q_cksum = csum_ipv6_magic(&ip6h->saddr, &ip6h->daddr,
  771. sizeof(*mld2q),
  772. IPPROTO_ICMPV6,
  773. csum_partial(csum_start,
  774. sizeof(*mld2q), 0));
  775. skb->ip_summed = CHECKSUM_NONE;
  776. skb_push(skb, sizeof(*eth) + sizeof(*ip6h) + sizeof(ra));
  777. return skb;
  778. }
  779. static void amt_send_mld_gq(struct amt_dev *amt, struct amt_tunnel_list *tunnel)
  780. {
  781. struct sk_buff *skb;
  782. skb = amt_build_mld_gq(amt);
  783. if (!skb)
  784. return;
  785. amt_skb_cb(skb)->tunnel = tunnel;
  786. dev_queue_xmit(skb);
  787. }
  788. #else
  789. static void amt_send_mld_gq(struct amt_dev *amt, struct amt_tunnel_list *tunnel)
  790. {
  791. }
  792. #endif
  793. static bool amt_queue_event(struct amt_dev *amt, enum amt_event event,
  794. struct sk_buff *skb)
  795. {
  796. int index;
  797. spin_lock_bh(&amt->lock);
  798. if (amt->nr_events >= AMT_MAX_EVENTS) {
  799. spin_unlock_bh(&amt->lock);
  800. return 1;
  801. }
  802. index = (amt->event_idx + amt->nr_events) % AMT_MAX_EVENTS;
  803. amt->events[index].event = event;
  804. amt->events[index].skb = skb;
  805. amt->nr_events++;
  806. amt->event_idx %= AMT_MAX_EVENTS;
  807. queue_work(amt_wq, &amt->event_wq);
  808. spin_unlock_bh(&amt->lock);
  809. return 0;
  810. }
  811. static void amt_secret_work(struct work_struct *work)
  812. {
  813. struct amt_dev *amt = container_of(to_delayed_work(work),
  814. struct amt_dev,
  815. secret_wq);
  816. spin_lock_bh(&amt->lock);
  817. get_random_bytes(&amt->key, sizeof(siphash_key_t));
  818. spin_unlock_bh(&amt->lock);
  819. mod_delayed_work(amt_wq, &amt->secret_wq,
  820. msecs_to_jiffies(AMT_SECRET_TIMEOUT));
  821. }
  822. static void amt_event_send_discovery(struct amt_dev *amt)
  823. {
  824. if (amt->status > AMT_STATUS_SENT_DISCOVERY)
  825. goto out;
  826. get_random_bytes(&amt->nonce, sizeof(__be32));
  827. amt_send_discovery(amt);
  828. out:
  829. mod_delayed_work(amt_wq, &amt->discovery_wq,
  830. msecs_to_jiffies(AMT_DISCOVERY_TIMEOUT));
  831. }
  832. static void amt_discovery_work(struct work_struct *work)
  833. {
  834. struct amt_dev *amt = container_of(to_delayed_work(work),
  835. struct amt_dev,
  836. discovery_wq);
  837. if (amt_queue_event(amt, AMT_EVENT_SEND_DISCOVERY, NULL))
  838. mod_delayed_work(amt_wq, &amt->discovery_wq,
  839. msecs_to_jiffies(AMT_DISCOVERY_TIMEOUT));
  840. }
  841. static void amt_event_send_request(struct amt_dev *amt)
  842. {
  843. u32 exp;
  844. if (amt->status < AMT_STATUS_RECEIVED_ADVERTISEMENT)
  845. goto out;
  846. if (amt->req_cnt > AMT_MAX_REQ_COUNT) {
  847. netdev_dbg(amt->dev, "Gateway is not ready");
  848. amt->qi = AMT_INIT_REQ_TIMEOUT;
  849. WRITE_ONCE(amt->ready4, false);
  850. WRITE_ONCE(amt->ready6, false);
  851. amt->remote_ip = 0;
  852. amt_update_gw_status(amt, AMT_STATUS_INIT, false);
  853. amt->req_cnt = 0;
  854. amt->nonce = 0;
  855. goto out;
  856. }
  857. if (!amt->req_cnt) {
  858. WRITE_ONCE(amt->ready4, false);
  859. WRITE_ONCE(amt->ready6, false);
  860. get_random_bytes(&amt->nonce, sizeof(__be32));
  861. }
  862. amt_send_request(amt, false);
  863. amt_send_request(amt, true);
  864. amt_update_gw_status(amt, AMT_STATUS_SENT_REQUEST, true);
  865. amt->req_cnt++;
  866. out:
  867. exp = min_t(u32, (1 * (1 << amt->req_cnt)), AMT_MAX_REQ_TIMEOUT);
  868. mod_delayed_work(amt_wq, &amt->req_wq, secs_to_jiffies(exp));
  869. }
  870. static void amt_req_work(struct work_struct *work)
  871. {
  872. struct amt_dev *amt = container_of(to_delayed_work(work),
  873. struct amt_dev,
  874. req_wq);
  875. if (amt_queue_event(amt, AMT_EVENT_SEND_REQUEST, NULL))
  876. mod_delayed_work(amt_wq, &amt->req_wq,
  877. msecs_to_jiffies(100));
  878. }
  879. static bool amt_send_membership_update(struct amt_dev *amt,
  880. struct sk_buff *skb,
  881. bool v6)
  882. {
  883. struct amt_header_membership_update *amtmu;
  884. struct socket *sock;
  885. struct iphdr *iph;
  886. struct flowi4 fl4;
  887. struct rtable *rt;
  888. int err;
  889. sock = rcu_dereference_bh(amt->sock);
  890. if (!sock)
  891. return true;
  892. err = skb_cow_head(skb, LL_RESERVED_SPACE(amt->dev) + sizeof(*amtmu) +
  893. sizeof(*iph) + sizeof(struct udphdr));
  894. if (err)
  895. return true;
  896. skb_reset_inner_headers(skb);
  897. memset(&fl4, 0, sizeof(struct flowi4));
  898. fl4.flowi4_oif = amt->stream_dev->ifindex;
  899. fl4.daddr = amt->remote_ip;
  900. fl4.saddr = amt->local_ip;
  901. fl4.flowi4_dscp = inet_dsfield_to_dscp(AMT_TOS);
  902. fl4.flowi4_proto = IPPROTO_UDP;
  903. rt = ip_route_output_key(amt->net, &fl4);
  904. if (IS_ERR(rt)) {
  905. netdev_dbg(amt->dev, "no route to %pI4\n", &amt->remote_ip);
  906. return true;
  907. }
  908. amtmu = skb_push(skb, sizeof(*amtmu));
  909. amtmu->version = 0;
  910. amtmu->type = AMT_MSG_MEMBERSHIP_UPDATE;
  911. amtmu->reserved = 0;
  912. amtmu->nonce = amt->nonce;
  913. amtmu->response_mac = amt->mac;
  914. if (!v6)
  915. skb_set_inner_protocol(skb, htons(ETH_P_IP));
  916. else
  917. skb_set_inner_protocol(skb, htons(ETH_P_IPV6));
  918. udp_tunnel_xmit_skb(rt, sock->sk, skb,
  919. fl4.saddr,
  920. fl4.daddr,
  921. AMT_TOS,
  922. ip4_dst_hoplimit(&rt->dst),
  923. 0,
  924. amt->gw_port,
  925. amt->relay_port,
  926. false,
  927. false,
  928. 0);
  929. amt_update_gw_status(amt, AMT_STATUS_SENT_UPDATE, true);
  930. return false;
  931. }
  932. static void amt_send_multicast_data(struct amt_dev *amt,
  933. const struct sk_buff *oskb,
  934. struct amt_tunnel_list *tunnel,
  935. bool v6)
  936. {
  937. struct amt_header_mcast_data *amtmd;
  938. struct socket *sock;
  939. struct sk_buff *skb;
  940. struct iphdr *iph;
  941. struct flowi4 fl4;
  942. struct rtable *rt;
  943. sock = rcu_dereference_bh(amt->sock);
  944. if (!sock)
  945. return;
  946. skb = skb_copy_expand(oskb, sizeof(*amtmd) + sizeof(*iph) +
  947. sizeof(struct udphdr), 0, GFP_ATOMIC);
  948. if (!skb)
  949. return;
  950. skb_reset_inner_headers(skb);
  951. memset(&fl4, 0, sizeof(struct flowi4));
  952. fl4.flowi4_oif = amt->stream_dev->ifindex;
  953. fl4.daddr = tunnel->ip4;
  954. fl4.saddr = amt->local_ip;
  955. fl4.flowi4_proto = IPPROTO_UDP;
  956. rt = ip_route_output_key(amt->net, &fl4);
  957. if (IS_ERR(rt)) {
  958. netdev_dbg(amt->dev, "no route to %pI4\n", &tunnel->ip4);
  959. kfree_skb(skb);
  960. return;
  961. }
  962. amtmd = skb_push(skb, sizeof(*amtmd));
  963. amtmd->version = 0;
  964. amtmd->reserved = 0;
  965. amtmd->type = AMT_MSG_MULTICAST_DATA;
  966. if (!v6)
  967. skb_set_inner_protocol(skb, htons(ETH_P_IP));
  968. else
  969. skb_set_inner_protocol(skb, htons(ETH_P_IPV6));
  970. udp_tunnel_xmit_skb(rt, sock->sk, skb,
  971. fl4.saddr,
  972. fl4.daddr,
  973. AMT_TOS,
  974. ip4_dst_hoplimit(&rt->dst),
  975. 0,
  976. amt->relay_port,
  977. tunnel->source_port,
  978. false,
  979. false,
  980. 0);
  981. }
  982. static bool amt_send_membership_query(struct amt_dev *amt,
  983. struct sk_buff *skb,
  984. struct amt_tunnel_list *tunnel,
  985. bool v6)
  986. {
  987. struct amt_header_membership_query *amtmq;
  988. struct socket *sock;
  989. struct rtable *rt;
  990. struct flowi4 fl4;
  991. int err;
  992. sock = rcu_dereference_bh(amt->sock);
  993. if (!sock)
  994. return true;
  995. err = skb_cow_head(skb, LL_RESERVED_SPACE(amt->dev) + sizeof(*amtmq) +
  996. sizeof(struct iphdr) + sizeof(struct udphdr));
  997. if (err)
  998. return true;
  999. skb_reset_inner_headers(skb);
  1000. memset(&fl4, 0, sizeof(struct flowi4));
  1001. fl4.flowi4_oif = amt->stream_dev->ifindex;
  1002. fl4.daddr = tunnel->ip4;
  1003. fl4.saddr = amt->local_ip;
  1004. fl4.flowi4_dscp = inet_dsfield_to_dscp(AMT_TOS);
  1005. fl4.flowi4_proto = IPPROTO_UDP;
  1006. rt = ip_route_output_key(amt->net, &fl4);
  1007. if (IS_ERR(rt)) {
  1008. netdev_dbg(amt->dev, "no route to %pI4\n", &tunnel->ip4);
  1009. return true;
  1010. }
  1011. amtmq = skb_push(skb, sizeof(*amtmq));
  1012. amtmq->version = 0;
  1013. amtmq->type = AMT_MSG_MEMBERSHIP_QUERY;
  1014. amtmq->reserved = 0;
  1015. amtmq->l = 0;
  1016. amtmq->g = 0;
  1017. amtmq->nonce = tunnel->nonce;
  1018. amtmq->response_mac = tunnel->mac;
  1019. if (!v6)
  1020. skb_set_inner_protocol(skb, htons(ETH_P_IP));
  1021. else
  1022. skb_set_inner_protocol(skb, htons(ETH_P_IPV6));
  1023. udp_tunnel_xmit_skb(rt, sock->sk, skb,
  1024. fl4.saddr,
  1025. fl4.daddr,
  1026. AMT_TOS,
  1027. ip4_dst_hoplimit(&rt->dst),
  1028. 0,
  1029. amt->relay_port,
  1030. tunnel->source_port,
  1031. false,
  1032. false,
  1033. 0);
  1034. amt_update_relay_status(tunnel, AMT_STATUS_SENT_QUERY, true);
  1035. return false;
  1036. }
  1037. static netdev_tx_t amt_dev_xmit(struct sk_buff *skb, struct net_device *dev)
  1038. {
  1039. struct amt_dev *amt = netdev_priv(dev);
  1040. struct amt_tunnel_list *tunnel;
  1041. struct amt_group_node *gnode;
  1042. union amt_addr group = {0,};
  1043. #if IS_ENABLED(CONFIG_IPV6)
  1044. struct ipv6hdr *ip6h;
  1045. struct mld_msg *mld;
  1046. #endif
  1047. bool report = false;
  1048. struct igmphdr *ih;
  1049. bool query = false;
  1050. struct iphdr *iph;
  1051. bool data = false;
  1052. bool v6 = false;
  1053. u32 hash;
  1054. iph = ip_hdr(skb);
  1055. if (iph->version == 4) {
  1056. if (!ipv4_is_multicast(iph->daddr))
  1057. goto free;
  1058. if (!ip_mc_check_igmp(skb)) {
  1059. ih = igmp_hdr(skb);
  1060. switch (ih->type) {
  1061. case IGMPV3_HOST_MEMBERSHIP_REPORT:
  1062. case IGMP_HOST_MEMBERSHIP_REPORT:
  1063. report = true;
  1064. break;
  1065. case IGMP_HOST_MEMBERSHIP_QUERY:
  1066. query = true;
  1067. break;
  1068. default:
  1069. goto free;
  1070. }
  1071. } else {
  1072. data = true;
  1073. }
  1074. v6 = false;
  1075. group.ip4 = iph->daddr;
  1076. #if IS_ENABLED(CONFIG_IPV6)
  1077. } else if (iph->version == 6) {
  1078. ip6h = ipv6_hdr(skb);
  1079. if (!ipv6_addr_is_multicast(&ip6h->daddr))
  1080. goto free;
  1081. if (!ipv6_mc_check_mld(skb)) {
  1082. mld = (struct mld_msg *)skb_transport_header(skb);
  1083. switch (mld->mld_type) {
  1084. case ICMPV6_MGM_REPORT:
  1085. case ICMPV6_MLD2_REPORT:
  1086. report = true;
  1087. break;
  1088. case ICMPV6_MGM_QUERY:
  1089. query = true;
  1090. break;
  1091. default:
  1092. goto free;
  1093. }
  1094. } else {
  1095. data = true;
  1096. }
  1097. v6 = true;
  1098. group.ip6 = ip6h->daddr;
  1099. #endif
  1100. } else {
  1101. dev->stats.tx_errors++;
  1102. goto free;
  1103. }
  1104. if (!pskb_may_pull(skb, sizeof(struct ethhdr)))
  1105. goto free;
  1106. skb_pull(skb, sizeof(struct ethhdr));
  1107. if (amt->mode == AMT_MODE_GATEWAY) {
  1108. /* Gateway only passes IGMP/MLD packets */
  1109. if (!report)
  1110. goto free;
  1111. if ((!v6 && !READ_ONCE(amt->ready4)) ||
  1112. (v6 && !READ_ONCE(amt->ready6)))
  1113. goto free;
  1114. if (amt_send_membership_update(amt, skb, v6))
  1115. goto free;
  1116. goto unlock;
  1117. } else if (amt->mode == AMT_MODE_RELAY) {
  1118. if (query) {
  1119. tunnel = amt_skb_cb(skb)->tunnel;
  1120. if (!tunnel) {
  1121. WARN_ON(1);
  1122. goto free;
  1123. }
  1124. /* Do not forward unexpected query */
  1125. if (amt_send_membership_query(amt, skb, tunnel, v6))
  1126. goto free;
  1127. goto unlock;
  1128. }
  1129. if (!data)
  1130. goto free;
  1131. list_for_each_entry_rcu(tunnel, &amt->tunnel_list, list) {
  1132. hash = amt_group_hash(tunnel, &group);
  1133. hlist_for_each_entry_rcu(gnode, &tunnel->groups[hash],
  1134. node) {
  1135. if (!v6) {
  1136. if (gnode->group_addr.ip4 == iph->daddr)
  1137. goto found;
  1138. #if IS_ENABLED(CONFIG_IPV6)
  1139. } else {
  1140. if (ipv6_addr_equal(&gnode->group_addr.ip6,
  1141. &ip6h->daddr))
  1142. goto found;
  1143. #endif
  1144. }
  1145. }
  1146. continue;
  1147. found:
  1148. amt_send_multicast_data(amt, skb, tunnel, v6);
  1149. }
  1150. }
  1151. dev_kfree_skb(skb);
  1152. return NETDEV_TX_OK;
  1153. free:
  1154. dev_kfree_skb(skb);
  1155. unlock:
  1156. dev->stats.tx_dropped++;
  1157. return NETDEV_TX_OK;
  1158. }
  1159. static int amt_parse_type(struct sk_buff *skb)
  1160. {
  1161. struct amt_header *amth;
  1162. if (!pskb_may_pull(skb, sizeof(struct udphdr) +
  1163. sizeof(struct amt_header)))
  1164. return -1;
  1165. amth = (struct amt_header *)(udp_hdr(skb) + 1);
  1166. if (amth->version != 0)
  1167. return -1;
  1168. if (amth->type >= __AMT_MSG_MAX || !amth->type)
  1169. return -1;
  1170. return amth->type;
  1171. }
  1172. static void amt_clear_groups(struct amt_tunnel_list *tunnel)
  1173. {
  1174. struct amt_dev *amt = tunnel->amt;
  1175. struct amt_group_node *gnode;
  1176. struct hlist_node *t;
  1177. int i;
  1178. spin_lock_bh(&tunnel->lock);
  1179. rcu_read_lock();
  1180. for (i = 0; i < amt->hash_buckets; i++)
  1181. hlist_for_each_entry_safe(gnode, t, &tunnel->groups[i], node)
  1182. amt_del_group(amt, gnode);
  1183. rcu_read_unlock();
  1184. spin_unlock_bh(&tunnel->lock);
  1185. }
  1186. static void amt_tunnel_expire(struct work_struct *work)
  1187. {
  1188. struct amt_tunnel_list *tunnel = container_of(to_delayed_work(work),
  1189. struct amt_tunnel_list,
  1190. gc_wq);
  1191. struct amt_dev *amt = tunnel->amt;
  1192. spin_lock_bh(&amt->lock);
  1193. rcu_read_lock();
  1194. list_del_rcu(&tunnel->list);
  1195. amt->nr_tunnels--;
  1196. amt_clear_groups(tunnel);
  1197. rcu_read_unlock();
  1198. spin_unlock_bh(&amt->lock);
  1199. kfree_rcu(tunnel, rcu);
  1200. }
  1201. static void amt_cleanup_srcs(struct amt_dev *amt,
  1202. struct amt_tunnel_list *tunnel,
  1203. struct amt_group_node *gnode)
  1204. {
  1205. struct amt_source_node *snode;
  1206. struct hlist_node *t;
  1207. int i;
  1208. /* Delete old sources */
  1209. for (i = 0; i < amt->hash_buckets; i++) {
  1210. hlist_for_each_entry_safe(snode, t, &gnode->sources[i], node) {
  1211. if (snode->flags == AMT_SOURCE_OLD)
  1212. amt_destroy_source(snode);
  1213. }
  1214. }
  1215. /* switch from new to old */
  1216. for (i = 0; i < amt->hash_buckets; i++) {
  1217. hlist_for_each_entry_rcu(snode, &gnode->sources[i], node) {
  1218. snode->flags = AMT_SOURCE_OLD;
  1219. if (!gnode->v6)
  1220. netdev_dbg(snode->gnode->amt->dev,
  1221. "Add source as OLD %pI4 from %pI4\n",
  1222. &snode->source_addr.ip4,
  1223. &gnode->group_addr.ip4);
  1224. #if IS_ENABLED(CONFIG_IPV6)
  1225. else
  1226. netdev_dbg(snode->gnode->amt->dev,
  1227. "Add source as OLD %pI6 from %pI6\n",
  1228. &snode->source_addr.ip6,
  1229. &gnode->group_addr.ip6);
  1230. #endif
  1231. }
  1232. }
  1233. }
  1234. static void amt_add_srcs(struct amt_dev *amt, struct amt_tunnel_list *tunnel,
  1235. struct amt_group_node *gnode, void *grec,
  1236. bool v6)
  1237. {
  1238. struct igmpv3_grec *igmp_grec;
  1239. struct amt_source_node *snode;
  1240. #if IS_ENABLED(CONFIG_IPV6)
  1241. struct mld2_grec *mld_grec;
  1242. #endif
  1243. union amt_addr src = {0,};
  1244. u16 nsrcs;
  1245. u32 hash;
  1246. int i;
  1247. if (!v6) {
  1248. igmp_grec = grec;
  1249. nsrcs = ntohs(igmp_grec->grec_nsrcs);
  1250. } else {
  1251. #if IS_ENABLED(CONFIG_IPV6)
  1252. mld_grec = grec;
  1253. nsrcs = ntohs(mld_grec->grec_nsrcs);
  1254. #else
  1255. return;
  1256. #endif
  1257. }
  1258. for (i = 0; i < nsrcs; i++) {
  1259. if (tunnel->nr_sources >= amt->max_sources)
  1260. return;
  1261. if (!v6)
  1262. src.ip4 = igmp_grec->grec_src[i];
  1263. #if IS_ENABLED(CONFIG_IPV6)
  1264. else
  1265. memcpy(&src.ip6, &mld_grec->grec_src[i],
  1266. sizeof(struct in6_addr));
  1267. #endif
  1268. if (amt_lookup_src(tunnel, gnode, AMT_FILTER_ALL, &src))
  1269. continue;
  1270. snode = amt_alloc_snode(gnode, &src);
  1271. if (snode) {
  1272. hash = amt_source_hash(tunnel, &snode->source_addr);
  1273. hlist_add_head_rcu(&snode->node, &gnode->sources[hash]);
  1274. tunnel->nr_sources++;
  1275. gnode->nr_sources++;
  1276. if (!gnode->v6)
  1277. netdev_dbg(snode->gnode->amt->dev,
  1278. "Add source as NEW %pI4 from %pI4\n",
  1279. &snode->source_addr.ip4,
  1280. &gnode->group_addr.ip4);
  1281. #if IS_ENABLED(CONFIG_IPV6)
  1282. else
  1283. netdev_dbg(snode->gnode->amt->dev,
  1284. "Add source as NEW %pI6 from %pI6\n",
  1285. &snode->source_addr.ip6,
  1286. &gnode->group_addr.ip6);
  1287. #endif
  1288. }
  1289. }
  1290. }
  1291. /* Router State Report Rec'd New Router State
  1292. * ------------ ------------ ----------------
  1293. * EXCLUDE (X,Y) IS_IN (A) EXCLUDE (X+A,Y-A)
  1294. *
  1295. * -----------+-----------+-----------+
  1296. * | OLD | NEW |
  1297. * -----------+-----------+-----------+
  1298. * FWD | X | X+A |
  1299. * -----------+-----------+-----------+
  1300. * D_FWD | Y | Y-A |
  1301. * -----------+-----------+-----------+
  1302. * NONE | | A |
  1303. * -----------+-----------+-----------+
  1304. *
  1305. * a) Received sources are NONE/NEW
  1306. * b) All NONE will be deleted by amt_cleanup_srcs().
  1307. * c) All OLD will be deleted by amt_cleanup_srcs().
  1308. * d) After delete, NEW source will be switched to OLD.
  1309. */
  1310. static void amt_lookup_act_srcs(struct amt_tunnel_list *tunnel,
  1311. struct amt_group_node *gnode,
  1312. void *grec,
  1313. enum amt_ops ops,
  1314. enum amt_filter filter,
  1315. enum amt_act act,
  1316. bool v6)
  1317. {
  1318. struct amt_dev *amt = tunnel->amt;
  1319. struct amt_source_node *snode;
  1320. struct igmpv3_grec *igmp_grec;
  1321. #if IS_ENABLED(CONFIG_IPV6)
  1322. struct mld2_grec *mld_grec;
  1323. #endif
  1324. union amt_addr src = {0,};
  1325. struct hlist_node *t;
  1326. u16 nsrcs;
  1327. int i, j;
  1328. if (!v6) {
  1329. igmp_grec = grec;
  1330. nsrcs = ntohs(igmp_grec->grec_nsrcs);
  1331. } else {
  1332. #if IS_ENABLED(CONFIG_IPV6)
  1333. mld_grec = grec;
  1334. nsrcs = ntohs(mld_grec->grec_nsrcs);
  1335. #else
  1336. return;
  1337. #endif
  1338. }
  1339. memset(&src, 0, sizeof(union amt_addr));
  1340. switch (ops) {
  1341. case AMT_OPS_INT:
  1342. /* A*B */
  1343. for (i = 0; i < nsrcs; i++) {
  1344. if (!v6)
  1345. src.ip4 = igmp_grec->grec_src[i];
  1346. #if IS_ENABLED(CONFIG_IPV6)
  1347. else
  1348. memcpy(&src.ip6, &mld_grec->grec_src[i],
  1349. sizeof(struct in6_addr));
  1350. #endif
  1351. snode = amt_lookup_src(tunnel, gnode, filter, &src);
  1352. if (!snode)
  1353. continue;
  1354. amt_act_src(tunnel, gnode, snode, act);
  1355. }
  1356. break;
  1357. case AMT_OPS_UNI:
  1358. /* A+B */
  1359. for (i = 0; i < amt->hash_buckets; i++) {
  1360. hlist_for_each_entry_safe(snode, t, &gnode->sources[i],
  1361. node) {
  1362. if (amt_status_filter(snode, filter))
  1363. amt_act_src(tunnel, gnode, snode, act);
  1364. }
  1365. }
  1366. for (i = 0; i < nsrcs; i++) {
  1367. if (!v6)
  1368. src.ip4 = igmp_grec->grec_src[i];
  1369. #if IS_ENABLED(CONFIG_IPV6)
  1370. else
  1371. memcpy(&src.ip6, &mld_grec->grec_src[i],
  1372. sizeof(struct in6_addr));
  1373. #endif
  1374. snode = amt_lookup_src(tunnel, gnode, filter, &src);
  1375. if (!snode)
  1376. continue;
  1377. amt_act_src(tunnel, gnode, snode, act);
  1378. }
  1379. break;
  1380. case AMT_OPS_SUB:
  1381. /* A-B */
  1382. for (i = 0; i < amt->hash_buckets; i++) {
  1383. hlist_for_each_entry_safe(snode, t, &gnode->sources[i],
  1384. node) {
  1385. if (!amt_status_filter(snode, filter))
  1386. continue;
  1387. for (j = 0; j < nsrcs; j++) {
  1388. if (!v6)
  1389. src.ip4 = igmp_grec->grec_src[j];
  1390. #if IS_ENABLED(CONFIG_IPV6)
  1391. else
  1392. memcpy(&src.ip6,
  1393. &mld_grec->grec_src[j],
  1394. sizeof(struct in6_addr));
  1395. #endif
  1396. if (amt_addr_equal(&snode->source_addr,
  1397. &src))
  1398. goto out_sub;
  1399. }
  1400. amt_act_src(tunnel, gnode, snode, act);
  1401. continue;
  1402. out_sub:;
  1403. }
  1404. }
  1405. break;
  1406. case AMT_OPS_SUB_REV:
  1407. /* B-A */
  1408. for (i = 0; i < nsrcs; i++) {
  1409. if (!v6)
  1410. src.ip4 = igmp_grec->grec_src[i];
  1411. #if IS_ENABLED(CONFIG_IPV6)
  1412. else
  1413. memcpy(&src.ip6, &mld_grec->grec_src[i],
  1414. sizeof(struct in6_addr));
  1415. #endif
  1416. snode = amt_lookup_src(tunnel, gnode, AMT_FILTER_ALL,
  1417. &src);
  1418. if (!snode) {
  1419. snode = amt_lookup_src(tunnel, gnode,
  1420. filter, &src);
  1421. if (snode)
  1422. amt_act_src(tunnel, gnode, snode, act);
  1423. }
  1424. }
  1425. break;
  1426. default:
  1427. netdev_dbg(amt->dev, "Invalid type\n");
  1428. return;
  1429. }
  1430. }
  1431. static void amt_mcast_is_in_handler(struct amt_dev *amt,
  1432. struct amt_tunnel_list *tunnel,
  1433. struct amt_group_node *gnode,
  1434. void *grec, void *zero_grec, bool v6)
  1435. {
  1436. if (gnode->filter_mode == MCAST_INCLUDE) {
  1437. /* Router State Report Rec'd New Router State Actions
  1438. * ------------ ------------ ---------------- -------
  1439. * INCLUDE (A) IS_IN (B) INCLUDE (A+B) (B)=GMI
  1440. */
  1441. /* Update IS_IN (B) as FWD/NEW */
  1442. amt_lookup_act_srcs(tunnel, gnode, grec, AMT_OPS_UNI,
  1443. AMT_FILTER_NONE_NEW,
  1444. AMT_ACT_STATUS_FWD_NEW,
  1445. v6);
  1446. /* Update INCLUDE (A) as NEW */
  1447. amt_lookup_act_srcs(tunnel, gnode, grec, AMT_OPS_UNI,
  1448. AMT_FILTER_FWD,
  1449. AMT_ACT_STATUS_FWD_NEW,
  1450. v6);
  1451. /* (B)=GMI */
  1452. amt_lookup_act_srcs(tunnel, gnode, grec, AMT_OPS_INT,
  1453. AMT_FILTER_FWD_NEW,
  1454. AMT_ACT_GMI,
  1455. v6);
  1456. } else {
  1457. /* State Actions
  1458. * ------------ ------------ ---------------- -------
  1459. * EXCLUDE (X,Y) IS_IN (A) EXCLUDE (X+A,Y-A) (A)=GMI
  1460. */
  1461. /* Update (A) in (X, Y) as NONE/NEW */
  1462. amt_lookup_act_srcs(tunnel, gnode, grec, AMT_OPS_INT,
  1463. AMT_FILTER_BOTH,
  1464. AMT_ACT_STATUS_NONE_NEW,
  1465. v6);
  1466. /* Update FWD/OLD as FWD/NEW */
  1467. amt_lookup_act_srcs(tunnel, gnode, zero_grec, AMT_OPS_UNI,
  1468. AMT_FILTER_FWD,
  1469. AMT_ACT_STATUS_FWD_NEW,
  1470. v6);
  1471. /* Update IS_IN (A) as FWD/NEW */
  1472. amt_lookup_act_srcs(tunnel, gnode, grec, AMT_OPS_INT,
  1473. AMT_FILTER_NONE_NEW,
  1474. AMT_ACT_STATUS_FWD_NEW,
  1475. v6);
  1476. /* Update EXCLUDE (, Y-A) as D_FWD_NEW */
  1477. amt_lookup_act_srcs(tunnel, gnode, grec, AMT_OPS_SUB,
  1478. AMT_FILTER_D_FWD,
  1479. AMT_ACT_STATUS_D_FWD_NEW,
  1480. v6);
  1481. }
  1482. }
  1483. static void amt_mcast_is_ex_handler(struct amt_dev *amt,
  1484. struct amt_tunnel_list *tunnel,
  1485. struct amt_group_node *gnode,
  1486. void *grec, void *zero_grec, bool v6)
  1487. {
  1488. if (gnode->filter_mode == MCAST_INCLUDE) {
  1489. /* Router State Report Rec'd New Router State Actions
  1490. * ------------ ------------ ---------------- -------
  1491. * INCLUDE (A) IS_EX (B) EXCLUDE (A*B,B-A) (B-A)=0
  1492. * Delete (A-B)
  1493. * Group Timer=GMI
  1494. */
  1495. /* EXCLUDE(A*B, ) */
  1496. amt_lookup_act_srcs(tunnel, gnode, grec, AMT_OPS_INT,
  1497. AMT_FILTER_FWD,
  1498. AMT_ACT_STATUS_FWD_NEW,
  1499. v6);
  1500. /* EXCLUDE(, B-A) */
  1501. amt_lookup_act_srcs(tunnel, gnode, grec, AMT_OPS_SUB_REV,
  1502. AMT_FILTER_FWD,
  1503. AMT_ACT_STATUS_D_FWD_NEW,
  1504. v6);
  1505. /* (B-A)=0 */
  1506. amt_lookup_act_srcs(tunnel, gnode, zero_grec, AMT_OPS_UNI,
  1507. AMT_FILTER_D_FWD_NEW,
  1508. AMT_ACT_GMI_ZERO,
  1509. v6);
  1510. /* Group Timer=GMI */
  1511. if (!mod_delayed_work(amt_wq, &gnode->group_timer,
  1512. msecs_to_jiffies(amt_gmi(amt))))
  1513. dev_hold(amt->dev);
  1514. gnode->filter_mode = MCAST_EXCLUDE;
  1515. /* Delete (A-B) will be worked by amt_cleanup_srcs(). */
  1516. } else {
  1517. /* Router State Report Rec'd New Router State Actions
  1518. * ------------ ------------ ---------------- -------
  1519. * EXCLUDE (X,Y) IS_EX (A) EXCLUDE (A-Y,Y*A) (A-X-Y)=GMI
  1520. * Delete (X-A)
  1521. * Delete (Y-A)
  1522. * Group Timer=GMI
  1523. */
  1524. /* EXCLUDE (A-Y, ) */
  1525. amt_lookup_act_srcs(tunnel, gnode, grec, AMT_OPS_SUB_REV,
  1526. AMT_FILTER_D_FWD,
  1527. AMT_ACT_STATUS_FWD_NEW,
  1528. v6);
  1529. /* EXCLUDE (, Y*A ) */
  1530. amt_lookup_act_srcs(tunnel, gnode, grec, AMT_OPS_INT,
  1531. AMT_FILTER_D_FWD,
  1532. AMT_ACT_STATUS_D_FWD_NEW,
  1533. v6);
  1534. /* (A-X-Y)=GMI */
  1535. amt_lookup_act_srcs(tunnel, gnode, grec, AMT_OPS_SUB_REV,
  1536. AMT_FILTER_BOTH_NEW,
  1537. AMT_ACT_GMI,
  1538. v6);
  1539. /* Group Timer=GMI */
  1540. if (!mod_delayed_work(amt_wq, &gnode->group_timer,
  1541. msecs_to_jiffies(amt_gmi(amt))))
  1542. dev_hold(amt->dev);
  1543. /* Delete (X-A), (Y-A) will be worked by amt_cleanup_srcs(). */
  1544. }
  1545. }
  1546. static void amt_mcast_to_in_handler(struct amt_dev *amt,
  1547. struct amt_tunnel_list *tunnel,
  1548. struct amt_group_node *gnode,
  1549. void *grec, void *zero_grec, bool v6)
  1550. {
  1551. if (gnode->filter_mode == MCAST_INCLUDE) {
  1552. /* Router State Report Rec'd New Router State Actions
  1553. * ------------ ------------ ---------------- -------
  1554. * INCLUDE (A) TO_IN (B) INCLUDE (A+B) (B)=GMI
  1555. * Send Q(G,A-B)
  1556. */
  1557. /* Update TO_IN (B) sources as FWD/NEW */
  1558. amt_lookup_act_srcs(tunnel, gnode, grec, AMT_OPS_UNI,
  1559. AMT_FILTER_NONE_NEW,
  1560. AMT_ACT_STATUS_FWD_NEW,
  1561. v6);
  1562. /* Update INCLUDE (A) sources as NEW */
  1563. amt_lookup_act_srcs(tunnel, gnode, grec, AMT_OPS_UNI,
  1564. AMT_FILTER_FWD,
  1565. AMT_ACT_STATUS_FWD_NEW,
  1566. v6);
  1567. /* (B)=GMI */
  1568. amt_lookup_act_srcs(tunnel, gnode, grec, AMT_OPS_INT,
  1569. AMT_FILTER_FWD_NEW,
  1570. AMT_ACT_GMI,
  1571. v6);
  1572. } else {
  1573. /* Router State Report Rec'd New Router State Actions
  1574. * ------------ ------------ ---------------- -------
  1575. * EXCLUDE (X,Y) TO_IN (A) EXCLUDE (X+A,Y-A) (A)=GMI
  1576. * Send Q(G,X-A)
  1577. * Send Q(G)
  1578. */
  1579. /* Update TO_IN (A) sources as FWD/NEW */
  1580. amt_lookup_act_srcs(tunnel, gnode, grec, AMT_OPS_UNI,
  1581. AMT_FILTER_NONE_NEW,
  1582. AMT_ACT_STATUS_FWD_NEW,
  1583. v6);
  1584. /* Update EXCLUDE(X,) sources as FWD/NEW */
  1585. amt_lookup_act_srcs(tunnel, gnode, grec, AMT_OPS_UNI,
  1586. AMT_FILTER_FWD,
  1587. AMT_ACT_STATUS_FWD_NEW,
  1588. v6);
  1589. /* EXCLUDE (, Y-A)
  1590. * (A) are already switched to FWD_NEW.
  1591. * So, D_FWD/OLD -> D_FWD/NEW is okay.
  1592. */
  1593. amt_lookup_act_srcs(tunnel, gnode, zero_grec, AMT_OPS_UNI,
  1594. AMT_FILTER_D_FWD,
  1595. AMT_ACT_STATUS_D_FWD_NEW,
  1596. v6);
  1597. /* (A)=GMI
  1598. * Only FWD_NEW will have (A) sources.
  1599. */
  1600. amt_lookup_act_srcs(tunnel, gnode, grec, AMT_OPS_INT,
  1601. AMT_FILTER_FWD_NEW,
  1602. AMT_ACT_GMI,
  1603. v6);
  1604. }
  1605. }
  1606. static void amt_mcast_to_ex_handler(struct amt_dev *amt,
  1607. struct amt_tunnel_list *tunnel,
  1608. struct amt_group_node *gnode,
  1609. void *grec, void *zero_grec, bool v6)
  1610. {
  1611. if (gnode->filter_mode == MCAST_INCLUDE) {
  1612. /* Router State Report Rec'd New Router State Actions
  1613. * ------------ ------------ ---------------- -------
  1614. * INCLUDE (A) TO_EX (B) EXCLUDE (A*B,B-A) (B-A)=0
  1615. * Delete (A-B)
  1616. * Send Q(G,A*B)
  1617. * Group Timer=GMI
  1618. */
  1619. /* EXCLUDE (A*B, ) */
  1620. amt_lookup_act_srcs(tunnel, gnode, grec, AMT_OPS_INT,
  1621. AMT_FILTER_FWD,
  1622. AMT_ACT_STATUS_FWD_NEW,
  1623. v6);
  1624. /* EXCLUDE (, B-A) */
  1625. amt_lookup_act_srcs(tunnel, gnode, grec, AMT_OPS_SUB_REV,
  1626. AMT_FILTER_FWD,
  1627. AMT_ACT_STATUS_D_FWD_NEW,
  1628. v6);
  1629. /* (B-A)=0 */
  1630. amt_lookup_act_srcs(tunnel, gnode, zero_grec, AMT_OPS_UNI,
  1631. AMT_FILTER_D_FWD_NEW,
  1632. AMT_ACT_GMI_ZERO,
  1633. v6);
  1634. /* Group Timer=GMI */
  1635. if (!mod_delayed_work(amt_wq, &gnode->group_timer,
  1636. msecs_to_jiffies(amt_gmi(amt))))
  1637. dev_hold(amt->dev);
  1638. gnode->filter_mode = MCAST_EXCLUDE;
  1639. /* Delete (A-B) will be worked by amt_cleanup_srcs(). */
  1640. } else {
  1641. /* Router State Report Rec'd New Router State Actions
  1642. * ------------ ------------ ---------------- -------
  1643. * EXCLUDE (X,Y) TO_EX (A) EXCLUDE (A-Y,Y*A) (A-X-Y)=Group Timer
  1644. * Delete (X-A)
  1645. * Delete (Y-A)
  1646. * Send Q(G,A-Y)
  1647. * Group Timer=GMI
  1648. */
  1649. /* Update (A-X-Y) as NONE/OLD */
  1650. amt_lookup_act_srcs(tunnel, gnode, grec, AMT_OPS_SUB_REV,
  1651. AMT_FILTER_BOTH,
  1652. AMT_ACT_GT,
  1653. v6);
  1654. /* EXCLUDE (A-Y, ) */
  1655. amt_lookup_act_srcs(tunnel, gnode, grec, AMT_OPS_SUB_REV,
  1656. AMT_FILTER_D_FWD,
  1657. AMT_ACT_STATUS_FWD_NEW,
  1658. v6);
  1659. /* EXCLUDE (, Y*A) */
  1660. amt_lookup_act_srcs(tunnel, gnode, grec, AMT_OPS_INT,
  1661. AMT_FILTER_D_FWD,
  1662. AMT_ACT_STATUS_D_FWD_NEW,
  1663. v6);
  1664. /* Group Timer=GMI */
  1665. if (!mod_delayed_work(amt_wq, &gnode->group_timer,
  1666. msecs_to_jiffies(amt_gmi(amt))))
  1667. dev_hold(amt->dev);
  1668. /* Delete (X-A), (Y-A) will be worked by amt_cleanup_srcs(). */
  1669. }
  1670. }
  1671. static void amt_mcast_allow_handler(struct amt_dev *amt,
  1672. struct amt_tunnel_list *tunnel,
  1673. struct amt_group_node *gnode,
  1674. void *grec, void *zero_grec, bool v6)
  1675. {
  1676. if (gnode->filter_mode == MCAST_INCLUDE) {
  1677. /* Router State Report Rec'd New Router State Actions
  1678. * ------------ ------------ ---------------- -------
  1679. * INCLUDE (A) ALLOW (B) INCLUDE (A+B) (B)=GMI
  1680. */
  1681. /* INCLUDE (A+B) */
  1682. amt_lookup_act_srcs(tunnel, gnode, grec, AMT_OPS_UNI,
  1683. AMT_FILTER_FWD,
  1684. AMT_ACT_STATUS_FWD_NEW,
  1685. v6);
  1686. /* (B)=GMI */
  1687. amt_lookup_act_srcs(tunnel, gnode, grec, AMT_OPS_INT,
  1688. AMT_FILTER_FWD_NEW,
  1689. AMT_ACT_GMI,
  1690. v6);
  1691. } else {
  1692. /* Router State Report Rec'd New Router State Actions
  1693. * ------------ ------------ ---------------- -------
  1694. * EXCLUDE (X,Y) ALLOW (A) EXCLUDE (X+A,Y-A) (A)=GMI
  1695. */
  1696. /* EXCLUDE (X+A, ) */
  1697. amt_lookup_act_srcs(tunnel, gnode, grec, AMT_OPS_UNI,
  1698. AMT_FILTER_FWD,
  1699. AMT_ACT_STATUS_FWD_NEW,
  1700. v6);
  1701. /* EXCLUDE (, Y-A) */
  1702. amt_lookup_act_srcs(tunnel, gnode, grec, AMT_OPS_SUB,
  1703. AMT_FILTER_D_FWD,
  1704. AMT_ACT_STATUS_D_FWD_NEW,
  1705. v6);
  1706. /* (A)=GMI
  1707. * All (A) source are now FWD/NEW status.
  1708. */
  1709. amt_lookup_act_srcs(tunnel, gnode, grec, AMT_OPS_INT,
  1710. AMT_FILTER_FWD_NEW,
  1711. AMT_ACT_GMI,
  1712. v6);
  1713. }
  1714. }
  1715. static void amt_mcast_block_handler(struct amt_dev *amt,
  1716. struct amt_tunnel_list *tunnel,
  1717. struct amt_group_node *gnode,
  1718. void *grec, void *zero_grec, bool v6)
  1719. {
  1720. if (gnode->filter_mode == MCAST_INCLUDE) {
  1721. /* Router State Report Rec'd New Router State Actions
  1722. * ------------ ------------ ---------------- -------
  1723. * INCLUDE (A) BLOCK (B) INCLUDE (A) Send Q(G,A*B)
  1724. */
  1725. /* INCLUDE (A) */
  1726. amt_lookup_act_srcs(tunnel, gnode, zero_grec, AMT_OPS_UNI,
  1727. AMT_FILTER_FWD,
  1728. AMT_ACT_STATUS_FWD_NEW,
  1729. v6);
  1730. } else {
  1731. /* Router State Report Rec'd New Router State Actions
  1732. * ------------ ------------ ---------------- -------
  1733. * EXCLUDE (X,Y) BLOCK (A) EXCLUDE (X+(A-Y),Y) (A-X-Y)=Group Timer
  1734. * Send Q(G,A-Y)
  1735. */
  1736. /* (A-X-Y)=Group Timer */
  1737. amt_lookup_act_srcs(tunnel, gnode, grec, AMT_OPS_SUB_REV,
  1738. AMT_FILTER_BOTH,
  1739. AMT_ACT_GT,
  1740. v6);
  1741. /* EXCLUDE (X, ) */
  1742. amt_lookup_act_srcs(tunnel, gnode, grec, AMT_OPS_UNI,
  1743. AMT_FILTER_FWD,
  1744. AMT_ACT_STATUS_FWD_NEW,
  1745. v6);
  1746. /* EXCLUDE (X+(A-Y) */
  1747. amt_lookup_act_srcs(tunnel, gnode, grec, AMT_OPS_SUB_REV,
  1748. AMT_FILTER_D_FWD,
  1749. AMT_ACT_STATUS_FWD_NEW,
  1750. v6);
  1751. /* EXCLUDE (, Y) */
  1752. amt_lookup_act_srcs(tunnel, gnode, grec, AMT_OPS_UNI,
  1753. AMT_FILTER_D_FWD,
  1754. AMT_ACT_STATUS_D_FWD_NEW,
  1755. v6);
  1756. }
  1757. }
  1758. /* RFC 3376
  1759. * 7.3.2. In the Presence of Older Version Group Members
  1760. *
  1761. * When Group Compatibility Mode is IGMPv2, a router internally
  1762. * translates the following IGMPv2 messages for that group to their
  1763. * IGMPv3 equivalents:
  1764. *
  1765. * IGMPv2 Message IGMPv3 Equivalent
  1766. * -------------- -----------------
  1767. * Report IS_EX( {} )
  1768. * Leave TO_IN( {} )
  1769. */
  1770. static void amt_igmpv2_report_handler(struct amt_dev *amt, struct sk_buff *skb,
  1771. struct amt_tunnel_list *tunnel)
  1772. {
  1773. struct igmphdr *ih = igmp_hdr(skb);
  1774. struct iphdr *iph = ip_hdr(skb);
  1775. struct amt_group_node *gnode;
  1776. union amt_addr group, host;
  1777. memset(&group, 0, sizeof(union amt_addr));
  1778. group.ip4 = ih->group;
  1779. memset(&host, 0, sizeof(union amt_addr));
  1780. host.ip4 = iph->saddr;
  1781. gnode = amt_lookup_group(tunnel, &group, &host, false);
  1782. if (!gnode) {
  1783. gnode = amt_add_group(amt, tunnel, &group, &host, false);
  1784. if (!IS_ERR(gnode)) {
  1785. gnode->filter_mode = MCAST_EXCLUDE;
  1786. if (!mod_delayed_work(amt_wq, &gnode->group_timer,
  1787. msecs_to_jiffies(amt_gmi(amt))))
  1788. dev_hold(amt->dev);
  1789. }
  1790. }
  1791. }
  1792. /* RFC 3376
  1793. * 7.3.2. In the Presence of Older Version Group Members
  1794. *
  1795. * When Group Compatibility Mode is IGMPv2, a router internally
  1796. * translates the following IGMPv2 messages for that group to their
  1797. * IGMPv3 equivalents:
  1798. *
  1799. * IGMPv2 Message IGMPv3 Equivalent
  1800. * -------------- -----------------
  1801. * Report IS_EX( {} )
  1802. * Leave TO_IN( {} )
  1803. */
  1804. static void amt_igmpv2_leave_handler(struct amt_dev *amt, struct sk_buff *skb,
  1805. struct amt_tunnel_list *tunnel)
  1806. {
  1807. struct igmphdr *ih = igmp_hdr(skb);
  1808. struct iphdr *iph = ip_hdr(skb);
  1809. struct amt_group_node *gnode;
  1810. union amt_addr group, host;
  1811. memset(&group, 0, sizeof(union amt_addr));
  1812. group.ip4 = ih->group;
  1813. memset(&host, 0, sizeof(union amt_addr));
  1814. host.ip4 = iph->saddr;
  1815. gnode = amt_lookup_group(tunnel, &group, &host, false);
  1816. if (gnode)
  1817. amt_del_group(amt, gnode);
  1818. }
  1819. static void amt_igmpv3_report_handler(struct amt_dev *amt, struct sk_buff *skb,
  1820. struct amt_tunnel_list *tunnel)
  1821. {
  1822. struct igmpv3_report *ihrv3 = igmpv3_report_hdr(skb);
  1823. int len = skb_transport_offset(skb) + sizeof(*ihrv3);
  1824. void *zero_grec = (void *)&igmpv3_zero_grec;
  1825. struct iphdr *iph = ip_hdr(skb);
  1826. struct amt_group_node *gnode;
  1827. union amt_addr group, host;
  1828. struct igmpv3_grec *grec;
  1829. u16 nsrcs;
  1830. int i;
  1831. for (i = 0; i < ntohs(ihrv3->ngrec); i++) {
  1832. len += sizeof(*grec);
  1833. if (!ip_mc_may_pull(skb, len))
  1834. break;
  1835. grec = (void *)(skb->data + len - sizeof(*grec));
  1836. nsrcs = ntohs(grec->grec_nsrcs);
  1837. len += nsrcs * sizeof(__be32);
  1838. if (!ip_mc_may_pull(skb, len))
  1839. break;
  1840. memset(&group, 0, sizeof(union amt_addr));
  1841. group.ip4 = grec->grec_mca;
  1842. memset(&host, 0, sizeof(union amt_addr));
  1843. host.ip4 = iph->saddr;
  1844. gnode = amt_lookup_group(tunnel, &group, &host, false);
  1845. if (!gnode) {
  1846. gnode = amt_add_group(amt, tunnel, &group, &host,
  1847. false);
  1848. if (IS_ERR(gnode))
  1849. continue;
  1850. }
  1851. amt_add_srcs(amt, tunnel, gnode, grec, false);
  1852. switch (grec->grec_type) {
  1853. case IGMPV3_MODE_IS_INCLUDE:
  1854. amt_mcast_is_in_handler(amt, tunnel, gnode, grec,
  1855. zero_grec, false);
  1856. break;
  1857. case IGMPV3_MODE_IS_EXCLUDE:
  1858. amt_mcast_is_ex_handler(amt, tunnel, gnode, grec,
  1859. zero_grec, false);
  1860. break;
  1861. case IGMPV3_CHANGE_TO_INCLUDE:
  1862. amt_mcast_to_in_handler(amt, tunnel, gnode, grec,
  1863. zero_grec, false);
  1864. break;
  1865. case IGMPV3_CHANGE_TO_EXCLUDE:
  1866. amt_mcast_to_ex_handler(amt, tunnel, gnode, grec,
  1867. zero_grec, false);
  1868. break;
  1869. case IGMPV3_ALLOW_NEW_SOURCES:
  1870. amt_mcast_allow_handler(amt, tunnel, gnode, grec,
  1871. zero_grec, false);
  1872. break;
  1873. case IGMPV3_BLOCK_OLD_SOURCES:
  1874. amt_mcast_block_handler(amt, tunnel, gnode, grec,
  1875. zero_grec, false);
  1876. break;
  1877. default:
  1878. break;
  1879. }
  1880. amt_cleanup_srcs(amt, tunnel, gnode);
  1881. }
  1882. }
  1883. /* caller held tunnel->lock */
  1884. static void amt_igmp_report_handler(struct amt_dev *amt, struct sk_buff *skb,
  1885. struct amt_tunnel_list *tunnel)
  1886. {
  1887. struct igmphdr *ih = igmp_hdr(skb);
  1888. switch (ih->type) {
  1889. case IGMPV3_HOST_MEMBERSHIP_REPORT:
  1890. amt_igmpv3_report_handler(amt, skb, tunnel);
  1891. break;
  1892. case IGMPV2_HOST_MEMBERSHIP_REPORT:
  1893. amt_igmpv2_report_handler(amt, skb, tunnel);
  1894. break;
  1895. case IGMP_HOST_LEAVE_MESSAGE:
  1896. amt_igmpv2_leave_handler(amt, skb, tunnel);
  1897. break;
  1898. default:
  1899. break;
  1900. }
  1901. }
  1902. #if IS_ENABLED(CONFIG_IPV6)
  1903. /* RFC 3810
  1904. * 8.3.2. In the Presence of MLDv1 Multicast Address Listeners
  1905. *
  1906. * When Multicast Address Compatibility Mode is MLDv2, a router acts
  1907. * using the MLDv2 protocol for that multicast address. When Multicast
  1908. * Address Compatibility Mode is MLDv1, a router internally translates
  1909. * the following MLDv1 messages for that multicast address to their
  1910. * MLDv2 equivalents:
  1911. *
  1912. * MLDv1 Message MLDv2 Equivalent
  1913. * -------------- -----------------
  1914. * Report IS_EX( {} )
  1915. * Done TO_IN( {} )
  1916. */
  1917. static void amt_mldv1_report_handler(struct amt_dev *amt, struct sk_buff *skb,
  1918. struct amt_tunnel_list *tunnel)
  1919. {
  1920. struct mld_msg *mld = (struct mld_msg *)icmp6_hdr(skb);
  1921. struct ipv6hdr *ip6h = ipv6_hdr(skb);
  1922. struct amt_group_node *gnode;
  1923. union amt_addr group, host;
  1924. memcpy(&group.ip6, &mld->mld_mca, sizeof(struct in6_addr));
  1925. memcpy(&host.ip6, &ip6h->saddr, sizeof(struct in6_addr));
  1926. gnode = amt_lookup_group(tunnel, &group, &host, true);
  1927. if (!gnode) {
  1928. gnode = amt_add_group(amt, tunnel, &group, &host, true);
  1929. if (!IS_ERR(gnode)) {
  1930. gnode->filter_mode = MCAST_EXCLUDE;
  1931. if (!mod_delayed_work(amt_wq, &gnode->group_timer,
  1932. msecs_to_jiffies(amt_gmi(amt))))
  1933. dev_hold(amt->dev);
  1934. }
  1935. }
  1936. }
  1937. /* RFC 3810
  1938. * 8.3.2. In the Presence of MLDv1 Multicast Address Listeners
  1939. *
  1940. * When Multicast Address Compatibility Mode is MLDv2, a router acts
  1941. * using the MLDv2 protocol for that multicast address. When Multicast
  1942. * Address Compatibility Mode is MLDv1, a router internally translates
  1943. * the following MLDv1 messages for that multicast address to their
  1944. * MLDv2 equivalents:
  1945. *
  1946. * MLDv1 Message MLDv2 Equivalent
  1947. * -------------- -----------------
  1948. * Report IS_EX( {} )
  1949. * Done TO_IN( {} )
  1950. */
  1951. static void amt_mldv1_leave_handler(struct amt_dev *amt, struct sk_buff *skb,
  1952. struct amt_tunnel_list *tunnel)
  1953. {
  1954. struct mld_msg *mld = (struct mld_msg *)icmp6_hdr(skb);
  1955. struct iphdr *iph = ip_hdr(skb);
  1956. struct amt_group_node *gnode;
  1957. union amt_addr group, host;
  1958. memcpy(&group.ip6, &mld->mld_mca, sizeof(struct in6_addr));
  1959. memset(&host, 0, sizeof(union amt_addr));
  1960. host.ip4 = iph->saddr;
  1961. gnode = amt_lookup_group(tunnel, &group, &host, true);
  1962. if (gnode) {
  1963. amt_del_group(amt, gnode);
  1964. return;
  1965. }
  1966. }
  1967. static void amt_mldv2_report_handler(struct amt_dev *amt, struct sk_buff *skb,
  1968. struct amt_tunnel_list *tunnel)
  1969. {
  1970. struct mld2_report *mld2r = (struct mld2_report *)icmp6_hdr(skb);
  1971. int len = skb_transport_offset(skb) + sizeof(*mld2r);
  1972. void *zero_grec = (void *)&mldv2_zero_grec;
  1973. struct ipv6hdr *ip6h = ipv6_hdr(skb);
  1974. struct amt_group_node *gnode;
  1975. union amt_addr group, host;
  1976. struct mld2_grec *grec;
  1977. u16 nsrcs;
  1978. int i;
  1979. for (i = 0; i < ntohs(mld2r->mld2r_ngrec); i++) {
  1980. len += sizeof(*grec);
  1981. if (!ipv6_mc_may_pull(skb, len))
  1982. break;
  1983. grec = (void *)(skb->data + len - sizeof(*grec));
  1984. nsrcs = ntohs(grec->grec_nsrcs);
  1985. len += nsrcs * sizeof(struct in6_addr);
  1986. if (!ipv6_mc_may_pull(skb, len))
  1987. break;
  1988. memset(&group, 0, sizeof(union amt_addr));
  1989. group.ip6 = grec->grec_mca;
  1990. memset(&host, 0, sizeof(union amt_addr));
  1991. host.ip6 = ip6h->saddr;
  1992. gnode = amt_lookup_group(tunnel, &group, &host, true);
  1993. if (!gnode) {
  1994. gnode = amt_add_group(amt, tunnel, &group, &host,
  1995. ETH_P_IPV6);
  1996. if (IS_ERR(gnode))
  1997. continue;
  1998. }
  1999. amt_add_srcs(amt, tunnel, gnode, grec, true);
  2000. switch (grec->grec_type) {
  2001. case MLD2_MODE_IS_INCLUDE:
  2002. amt_mcast_is_in_handler(amt, tunnel, gnode, grec,
  2003. zero_grec, true);
  2004. break;
  2005. case MLD2_MODE_IS_EXCLUDE:
  2006. amt_mcast_is_ex_handler(amt, tunnel, gnode, grec,
  2007. zero_grec, true);
  2008. break;
  2009. case MLD2_CHANGE_TO_INCLUDE:
  2010. amt_mcast_to_in_handler(amt, tunnel, gnode, grec,
  2011. zero_grec, true);
  2012. break;
  2013. case MLD2_CHANGE_TO_EXCLUDE:
  2014. amt_mcast_to_ex_handler(amt, tunnel, gnode, grec,
  2015. zero_grec, true);
  2016. break;
  2017. case MLD2_ALLOW_NEW_SOURCES:
  2018. amt_mcast_allow_handler(amt, tunnel, gnode, grec,
  2019. zero_grec, true);
  2020. break;
  2021. case MLD2_BLOCK_OLD_SOURCES:
  2022. amt_mcast_block_handler(amt, tunnel, gnode, grec,
  2023. zero_grec, true);
  2024. break;
  2025. default:
  2026. break;
  2027. }
  2028. amt_cleanup_srcs(amt, tunnel, gnode);
  2029. }
  2030. }
  2031. /* caller held tunnel->lock */
  2032. static void amt_mld_report_handler(struct amt_dev *amt, struct sk_buff *skb,
  2033. struct amt_tunnel_list *tunnel)
  2034. {
  2035. struct mld_msg *mld = (struct mld_msg *)icmp6_hdr(skb);
  2036. switch (mld->mld_type) {
  2037. case ICMPV6_MGM_REPORT:
  2038. amt_mldv1_report_handler(amt, skb, tunnel);
  2039. break;
  2040. case ICMPV6_MLD2_REPORT:
  2041. amt_mldv2_report_handler(amt, skb, tunnel);
  2042. break;
  2043. case ICMPV6_MGM_REDUCTION:
  2044. amt_mldv1_leave_handler(amt, skb, tunnel);
  2045. break;
  2046. default:
  2047. break;
  2048. }
  2049. }
  2050. #endif
  2051. static bool amt_advertisement_handler(struct amt_dev *amt, struct sk_buff *skb)
  2052. {
  2053. struct amt_header_advertisement *amta;
  2054. int hdr_size;
  2055. hdr_size = sizeof(*amta) + sizeof(struct udphdr);
  2056. if (!pskb_may_pull(skb, hdr_size))
  2057. return true;
  2058. amta = (struct amt_header_advertisement *)(udp_hdr(skb) + 1);
  2059. if (!amta->ip4)
  2060. return true;
  2061. if (amta->reserved || amta->version)
  2062. return true;
  2063. if (ipv4_is_loopback(amta->ip4) || ipv4_is_multicast(amta->ip4) ||
  2064. ipv4_is_zeronet(amta->ip4))
  2065. return true;
  2066. if (amt->status != AMT_STATUS_SENT_DISCOVERY ||
  2067. amt->nonce != amta->nonce)
  2068. return true;
  2069. amt->remote_ip = amta->ip4;
  2070. netdev_dbg(amt->dev, "advertised remote ip = %pI4\n", &amt->remote_ip);
  2071. mod_delayed_work(amt_wq, &amt->req_wq, 0);
  2072. amt_update_gw_status(amt, AMT_STATUS_RECEIVED_ADVERTISEMENT, true);
  2073. return false;
  2074. }
  2075. static bool amt_multicast_data_handler(struct amt_dev *amt, struct sk_buff *skb)
  2076. {
  2077. struct amt_header_mcast_data *amtmd;
  2078. int hdr_size, len, err;
  2079. struct ethhdr *eth;
  2080. struct iphdr *iph;
  2081. if (READ_ONCE(amt->status) != AMT_STATUS_SENT_UPDATE)
  2082. return true;
  2083. hdr_size = sizeof(*amtmd) + sizeof(struct udphdr);
  2084. if (!pskb_may_pull(skb, hdr_size))
  2085. return true;
  2086. amtmd = (struct amt_header_mcast_data *)(udp_hdr(skb) + 1);
  2087. if (amtmd->reserved || amtmd->version)
  2088. return true;
  2089. if (iptunnel_pull_header(skb, hdr_size, htons(ETH_P_IP), false))
  2090. return true;
  2091. skb_reset_network_header(skb);
  2092. skb_push(skb, sizeof(*eth));
  2093. skb_reset_mac_header(skb);
  2094. skb_pull(skb, sizeof(*eth));
  2095. eth = eth_hdr(skb);
  2096. if (!pskb_may_pull(skb, sizeof(*iph)))
  2097. return true;
  2098. iph = ip_hdr(skb);
  2099. if (iph->version == 4) {
  2100. if (!ipv4_is_multicast(iph->daddr))
  2101. return true;
  2102. skb->protocol = htons(ETH_P_IP);
  2103. eth->h_proto = htons(ETH_P_IP);
  2104. ip_eth_mc_map(iph->daddr, eth->h_dest);
  2105. #if IS_ENABLED(CONFIG_IPV6)
  2106. } else if (iph->version == 6) {
  2107. struct ipv6hdr *ip6h;
  2108. if (!pskb_may_pull(skb, sizeof(*ip6h)))
  2109. return true;
  2110. ip6h = ipv6_hdr(skb);
  2111. if (!ipv6_addr_is_multicast(&ip6h->daddr))
  2112. return true;
  2113. skb->protocol = htons(ETH_P_IPV6);
  2114. eth->h_proto = htons(ETH_P_IPV6);
  2115. ipv6_eth_mc_map(&ip6h->daddr, eth->h_dest);
  2116. #endif
  2117. } else {
  2118. return true;
  2119. }
  2120. skb->pkt_type = PACKET_MULTICAST;
  2121. skb->ip_summed = CHECKSUM_NONE;
  2122. len = skb->len;
  2123. err = gro_cells_receive(&amt->gro_cells, skb);
  2124. if (likely(err == NET_RX_SUCCESS))
  2125. dev_sw_netstats_rx_add(amt->dev, len);
  2126. else
  2127. amt->dev->stats.rx_dropped++;
  2128. return false;
  2129. }
  2130. static bool amt_membership_query_handler(struct amt_dev *amt,
  2131. struct sk_buff *skb)
  2132. {
  2133. struct amt_header_membership_query *amtmq;
  2134. struct igmpv3_query *ihv3;
  2135. struct ethhdr *eth, *oeth;
  2136. struct iphdr *iph;
  2137. int hdr_size, len;
  2138. hdr_size = sizeof(*amtmq) + sizeof(struct udphdr);
  2139. if (!pskb_may_pull(skb, hdr_size))
  2140. return true;
  2141. amtmq = (struct amt_header_membership_query *)(udp_hdr(skb) + 1);
  2142. if (amtmq->reserved || amtmq->version)
  2143. return true;
  2144. if (amtmq->nonce != amt->nonce)
  2145. return true;
  2146. hdr_size -= sizeof(*eth);
  2147. if (iptunnel_pull_header(skb, hdr_size, htons(ETH_P_TEB), false))
  2148. return true;
  2149. oeth = eth_hdr(skb);
  2150. skb_reset_mac_header(skb);
  2151. skb_pull(skb, sizeof(*eth));
  2152. skb_reset_network_header(skb);
  2153. eth = eth_hdr(skb);
  2154. if (!pskb_may_pull(skb, sizeof(*iph)))
  2155. return true;
  2156. iph = ip_hdr(skb);
  2157. if (iph->version == 4) {
  2158. if (READ_ONCE(amt->ready4))
  2159. return true;
  2160. if (!pskb_may_pull(skb, sizeof(*iph) + AMT_IPHDR_OPTS +
  2161. sizeof(*ihv3)))
  2162. return true;
  2163. if (!ipv4_is_multicast(iph->daddr))
  2164. return true;
  2165. ihv3 = skb_pull(skb, sizeof(*iph) + AMT_IPHDR_OPTS);
  2166. skb_reset_transport_header(skb);
  2167. skb_push(skb, sizeof(*iph) + AMT_IPHDR_OPTS);
  2168. WRITE_ONCE(amt->ready4, true);
  2169. amt->mac = amtmq->response_mac;
  2170. amt->req_cnt = 0;
  2171. amt->qi = ihv3->qqic;
  2172. skb->protocol = htons(ETH_P_IP);
  2173. eth->h_proto = htons(ETH_P_IP);
  2174. ip_eth_mc_map(iph->daddr, eth->h_dest);
  2175. #if IS_ENABLED(CONFIG_IPV6)
  2176. } else if (iph->version == 6) {
  2177. struct mld2_query *mld2q;
  2178. struct ipv6hdr *ip6h;
  2179. if (READ_ONCE(amt->ready6))
  2180. return true;
  2181. if (!pskb_may_pull(skb, sizeof(*ip6h) + AMT_IP6HDR_OPTS +
  2182. sizeof(*mld2q)))
  2183. return true;
  2184. ip6h = ipv6_hdr(skb);
  2185. if (!ipv6_addr_is_multicast(&ip6h->daddr))
  2186. return true;
  2187. mld2q = skb_pull(skb, sizeof(*ip6h) + AMT_IP6HDR_OPTS);
  2188. skb_reset_transport_header(skb);
  2189. skb_push(skb, sizeof(*ip6h) + AMT_IP6HDR_OPTS);
  2190. WRITE_ONCE(amt->ready6, true);
  2191. amt->mac = amtmq->response_mac;
  2192. amt->req_cnt = 0;
  2193. amt->qi = mld2q->mld2q_qqic;
  2194. skb->protocol = htons(ETH_P_IPV6);
  2195. eth->h_proto = htons(ETH_P_IPV6);
  2196. ipv6_eth_mc_map(&ip6h->daddr, eth->h_dest);
  2197. #endif
  2198. } else {
  2199. return true;
  2200. }
  2201. ether_addr_copy(eth->h_source, oeth->h_source);
  2202. skb->pkt_type = PACKET_MULTICAST;
  2203. skb->ip_summed = CHECKSUM_NONE;
  2204. len = skb->len;
  2205. local_bh_disable();
  2206. if (__netif_rx(skb) == NET_RX_SUCCESS) {
  2207. amt_update_gw_status(amt, AMT_STATUS_RECEIVED_QUERY, true);
  2208. dev_sw_netstats_rx_add(amt->dev, len);
  2209. } else {
  2210. amt->dev->stats.rx_dropped++;
  2211. }
  2212. local_bh_enable();
  2213. return false;
  2214. }
  2215. static bool amt_update_handler(struct amt_dev *amt, struct sk_buff *skb)
  2216. {
  2217. struct amt_header_membership_update *amtmu;
  2218. struct amt_tunnel_list *tunnel;
  2219. struct ethhdr *eth;
  2220. struct iphdr *iph;
  2221. int len, hdr_size;
  2222. iph = ip_hdr(skb);
  2223. hdr_size = sizeof(*amtmu) + sizeof(struct udphdr);
  2224. if (!pskb_may_pull(skb, hdr_size))
  2225. return true;
  2226. amtmu = (struct amt_header_membership_update *)(udp_hdr(skb) + 1);
  2227. if (amtmu->reserved || amtmu->version)
  2228. return true;
  2229. if (iptunnel_pull_header(skb, hdr_size, skb->protocol, false))
  2230. return true;
  2231. skb_reset_network_header(skb);
  2232. list_for_each_entry_rcu(tunnel, &amt->tunnel_list, list) {
  2233. if (tunnel->ip4 == iph->saddr) {
  2234. if ((amtmu->nonce == tunnel->nonce &&
  2235. amtmu->response_mac == tunnel->mac)) {
  2236. mod_delayed_work(amt_wq, &tunnel->gc_wq,
  2237. msecs_to_jiffies(amt_gmi(amt))
  2238. * 3);
  2239. goto report;
  2240. } else {
  2241. netdev_dbg(amt->dev, "Invalid MAC\n");
  2242. return true;
  2243. }
  2244. }
  2245. }
  2246. return true;
  2247. report:
  2248. if (!pskb_may_pull(skb, sizeof(*iph)))
  2249. return true;
  2250. iph = ip_hdr(skb);
  2251. if (iph->version == 4) {
  2252. if (ip_mc_check_igmp(skb)) {
  2253. netdev_dbg(amt->dev, "Invalid IGMP\n");
  2254. return true;
  2255. }
  2256. spin_lock_bh(&tunnel->lock);
  2257. amt_igmp_report_handler(amt, skb, tunnel);
  2258. spin_unlock_bh(&tunnel->lock);
  2259. skb_push(skb, sizeof(struct ethhdr));
  2260. skb_reset_mac_header(skb);
  2261. eth = eth_hdr(skb);
  2262. skb->protocol = htons(ETH_P_IP);
  2263. eth->h_proto = htons(ETH_P_IP);
  2264. ip_eth_mc_map(iph->daddr, eth->h_dest);
  2265. #if IS_ENABLED(CONFIG_IPV6)
  2266. } else if (iph->version == 6) {
  2267. struct ipv6hdr *ip6h = ipv6_hdr(skb);
  2268. if (ipv6_mc_check_mld(skb)) {
  2269. netdev_dbg(amt->dev, "Invalid MLD\n");
  2270. return true;
  2271. }
  2272. spin_lock_bh(&tunnel->lock);
  2273. amt_mld_report_handler(amt, skb, tunnel);
  2274. spin_unlock_bh(&tunnel->lock);
  2275. skb_push(skb, sizeof(struct ethhdr));
  2276. skb_reset_mac_header(skb);
  2277. eth = eth_hdr(skb);
  2278. skb->protocol = htons(ETH_P_IPV6);
  2279. eth->h_proto = htons(ETH_P_IPV6);
  2280. ipv6_eth_mc_map(&ip6h->daddr, eth->h_dest);
  2281. #endif
  2282. } else {
  2283. netdev_dbg(amt->dev, "Unsupported Protocol\n");
  2284. return true;
  2285. }
  2286. skb_pull(skb, sizeof(struct ethhdr));
  2287. skb->pkt_type = PACKET_MULTICAST;
  2288. skb->ip_summed = CHECKSUM_NONE;
  2289. len = skb->len;
  2290. if (__netif_rx(skb) == NET_RX_SUCCESS) {
  2291. amt_update_relay_status(tunnel, AMT_STATUS_RECEIVED_UPDATE,
  2292. true);
  2293. dev_sw_netstats_rx_add(amt->dev, len);
  2294. } else {
  2295. amt->dev->stats.rx_dropped++;
  2296. }
  2297. return false;
  2298. }
  2299. static void amt_send_advertisement(struct amt_dev *amt, __be32 nonce,
  2300. __be32 daddr, __be16 dport)
  2301. {
  2302. struct amt_header_advertisement *amta;
  2303. int hlen, tlen, offset;
  2304. struct socket *sock;
  2305. struct udphdr *udph;
  2306. struct sk_buff *skb;
  2307. struct iphdr *iph;
  2308. struct rtable *rt;
  2309. struct flowi4 fl4;
  2310. u32 len;
  2311. int err;
  2312. rcu_read_lock();
  2313. sock = rcu_dereference(amt->sock);
  2314. if (!sock)
  2315. goto out;
  2316. if (!netif_running(amt->stream_dev) || !netif_running(amt->dev))
  2317. goto out;
  2318. rt = ip_route_output_ports(amt->net, &fl4, sock->sk,
  2319. daddr, amt->local_ip,
  2320. dport, amt->relay_port,
  2321. IPPROTO_UDP, 0,
  2322. amt->stream_dev->ifindex);
  2323. if (IS_ERR(rt)) {
  2324. amt->dev->stats.tx_errors++;
  2325. goto out;
  2326. }
  2327. hlen = LL_RESERVED_SPACE(amt->dev);
  2328. tlen = amt->dev->needed_tailroom;
  2329. len = hlen + tlen + sizeof(*iph) + sizeof(*udph) + sizeof(*amta);
  2330. skb = netdev_alloc_skb_ip_align(amt->dev, len);
  2331. if (!skb) {
  2332. ip_rt_put(rt);
  2333. amt->dev->stats.tx_errors++;
  2334. goto out;
  2335. }
  2336. skb->priority = TC_PRIO_CONTROL;
  2337. skb_dst_set(skb, &rt->dst);
  2338. len = sizeof(*iph) + sizeof(*udph) + sizeof(*amta);
  2339. skb_reset_network_header(skb);
  2340. skb_put(skb, len);
  2341. amta = skb_pull(skb, sizeof(*iph) + sizeof(*udph));
  2342. amta->version = 0;
  2343. amta->type = AMT_MSG_ADVERTISEMENT;
  2344. amta->reserved = 0;
  2345. amta->nonce = nonce;
  2346. amta->ip4 = amt->local_ip;
  2347. skb_push(skb, sizeof(*udph));
  2348. skb_reset_transport_header(skb);
  2349. udph = udp_hdr(skb);
  2350. udph->source = amt->relay_port;
  2351. udph->dest = dport;
  2352. udph->len = htons(sizeof(*amta) + sizeof(*udph));
  2353. udph->check = 0;
  2354. offset = skb_transport_offset(skb);
  2355. skb->csum = skb_checksum(skb, offset, skb->len - offset, 0);
  2356. udph->check = csum_tcpudp_magic(amt->local_ip, daddr,
  2357. sizeof(*udph) + sizeof(*amta),
  2358. IPPROTO_UDP, skb->csum);
  2359. skb_push(skb, sizeof(*iph));
  2360. iph = ip_hdr(skb);
  2361. iph->version = 4;
  2362. iph->ihl = (sizeof(struct iphdr)) >> 2;
  2363. iph->tos = AMT_TOS;
  2364. iph->frag_off = 0;
  2365. iph->ttl = ip4_dst_hoplimit(&rt->dst);
  2366. iph->daddr = daddr;
  2367. iph->saddr = amt->local_ip;
  2368. iph->protocol = IPPROTO_UDP;
  2369. iph->tot_len = htons(len);
  2370. skb->ip_summed = CHECKSUM_NONE;
  2371. ip_select_ident(amt->net, skb, NULL);
  2372. ip_send_check(iph);
  2373. err = ip_local_out(amt->net, sock->sk, skb);
  2374. if (unlikely(net_xmit_eval(err)))
  2375. amt->dev->stats.tx_errors++;
  2376. out:
  2377. rcu_read_unlock();
  2378. }
  2379. static bool amt_discovery_handler(struct amt_dev *amt, struct sk_buff *skb)
  2380. {
  2381. struct amt_header_discovery *amtd;
  2382. struct udphdr *udph;
  2383. struct iphdr *iph;
  2384. if (!pskb_may_pull(skb, sizeof(*udph) + sizeof(*amtd)))
  2385. return true;
  2386. iph = ip_hdr(skb);
  2387. udph = udp_hdr(skb);
  2388. amtd = (struct amt_header_discovery *)(udp_hdr(skb) + 1);
  2389. if (amtd->reserved || amtd->version)
  2390. return true;
  2391. amt_send_advertisement(amt, amtd->nonce, iph->saddr, udph->source);
  2392. return false;
  2393. }
  2394. static bool amt_request_handler(struct amt_dev *amt, struct sk_buff *skb)
  2395. {
  2396. struct amt_header_request *amtrh;
  2397. struct amt_tunnel_list *tunnel;
  2398. unsigned long long key;
  2399. struct udphdr *udph;
  2400. struct iphdr *iph;
  2401. u64 mac;
  2402. int i;
  2403. if (!pskb_may_pull(skb, sizeof(*udph) + sizeof(*amtrh)))
  2404. return true;
  2405. iph = ip_hdr(skb);
  2406. udph = udp_hdr(skb);
  2407. amtrh = (struct amt_header_request *)(udp_hdr(skb) + 1);
  2408. if (amtrh->reserved1 || amtrh->reserved2 || amtrh->version)
  2409. return true;
  2410. list_for_each_entry_rcu(tunnel, &amt->tunnel_list, list)
  2411. if (tunnel->ip4 == iph->saddr)
  2412. goto send;
  2413. spin_lock_bh(&amt->lock);
  2414. if (amt->nr_tunnels >= amt->max_tunnels) {
  2415. spin_unlock_bh(&amt->lock);
  2416. icmp_ndo_send(skb, ICMP_DEST_UNREACH, ICMP_HOST_UNREACH, 0);
  2417. return true;
  2418. }
  2419. tunnel = kzalloc(sizeof(*tunnel) +
  2420. (sizeof(struct hlist_head) * amt->hash_buckets),
  2421. GFP_ATOMIC);
  2422. if (!tunnel) {
  2423. spin_unlock_bh(&amt->lock);
  2424. return true;
  2425. }
  2426. tunnel->source_port = udph->source;
  2427. tunnel->ip4 = iph->saddr;
  2428. memcpy(&key, &tunnel->key, sizeof(unsigned long long));
  2429. tunnel->amt = amt;
  2430. spin_lock_init(&tunnel->lock);
  2431. for (i = 0; i < amt->hash_buckets; i++)
  2432. INIT_HLIST_HEAD(&tunnel->groups[i]);
  2433. INIT_DELAYED_WORK(&tunnel->gc_wq, amt_tunnel_expire);
  2434. list_add_tail_rcu(&tunnel->list, &amt->tunnel_list);
  2435. tunnel->key = amt->key;
  2436. __amt_update_relay_status(tunnel, AMT_STATUS_RECEIVED_REQUEST, true);
  2437. amt->nr_tunnels++;
  2438. mod_delayed_work(amt_wq, &tunnel->gc_wq,
  2439. msecs_to_jiffies(amt_gmi(amt)));
  2440. spin_unlock_bh(&amt->lock);
  2441. send:
  2442. tunnel->nonce = amtrh->nonce;
  2443. mac = siphash_3u32((__force u32)tunnel->ip4,
  2444. (__force u32)tunnel->source_port,
  2445. (__force u32)tunnel->nonce,
  2446. &tunnel->key);
  2447. tunnel->mac = mac >> 16;
  2448. if (!netif_running(amt->dev) || !netif_running(amt->stream_dev))
  2449. return true;
  2450. if (!amtrh->p)
  2451. amt_send_igmp_gq(amt, tunnel);
  2452. else
  2453. amt_send_mld_gq(amt, tunnel);
  2454. return false;
  2455. }
  2456. static void amt_gw_rcv(struct amt_dev *amt, struct sk_buff *skb)
  2457. {
  2458. int type = amt_parse_type(skb);
  2459. int err = 1;
  2460. if (type == -1)
  2461. goto drop;
  2462. if (amt->mode == AMT_MODE_GATEWAY) {
  2463. switch (type) {
  2464. case AMT_MSG_ADVERTISEMENT:
  2465. err = amt_advertisement_handler(amt, skb);
  2466. break;
  2467. case AMT_MSG_MEMBERSHIP_QUERY:
  2468. err = amt_membership_query_handler(amt, skb);
  2469. if (!err)
  2470. return;
  2471. break;
  2472. default:
  2473. netdev_dbg(amt->dev, "Invalid type of Gateway\n");
  2474. break;
  2475. }
  2476. }
  2477. drop:
  2478. if (err) {
  2479. amt->dev->stats.rx_dropped++;
  2480. kfree_skb(skb);
  2481. } else {
  2482. consume_skb(skb);
  2483. }
  2484. }
  2485. static int amt_rcv(struct sock *sk, struct sk_buff *skb)
  2486. {
  2487. struct amt_dev *amt;
  2488. struct iphdr *iph;
  2489. int type;
  2490. bool err;
  2491. rcu_read_lock_bh();
  2492. amt = rcu_dereference_sk_user_data(sk);
  2493. if (!amt) {
  2494. err = true;
  2495. kfree_skb(skb);
  2496. goto out;
  2497. }
  2498. skb->dev = amt->dev;
  2499. iph = ip_hdr(skb);
  2500. type = amt_parse_type(skb);
  2501. if (type == -1) {
  2502. err = true;
  2503. goto drop;
  2504. }
  2505. if (amt->mode == AMT_MODE_GATEWAY) {
  2506. switch (type) {
  2507. case AMT_MSG_ADVERTISEMENT:
  2508. if (iph->saddr != amt->discovery_ip) {
  2509. netdev_dbg(amt->dev, "Invalid Relay IP\n");
  2510. err = true;
  2511. goto drop;
  2512. }
  2513. if (amt_queue_event(amt, AMT_EVENT_RECEIVE, skb)) {
  2514. netdev_dbg(amt->dev, "AMT Event queue full\n");
  2515. err = true;
  2516. goto drop;
  2517. }
  2518. goto out;
  2519. case AMT_MSG_MULTICAST_DATA:
  2520. if (iph->saddr != amt->remote_ip) {
  2521. netdev_dbg(amt->dev, "Invalid Relay IP\n");
  2522. err = true;
  2523. goto drop;
  2524. }
  2525. err = amt_multicast_data_handler(amt, skb);
  2526. if (err)
  2527. goto drop;
  2528. else
  2529. goto out;
  2530. case AMT_MSG_MEMBERSHIP_QUERY:
  2531. if (iph->saddr != amt->remote_ip) {
  2532. netdev_dbg(amt->dev, "Invalid Relay IP\n");
  2533. err = true;
  2534. goto drop;
  2535. }
  2536. if (amt_queue_event(amt, AMT_EVENT_RECEIVE, skb)) {
  2537. netdev_dbg(amt->dev, "AMT Event queue full\n");
  2538. err = true;
  2539. goto drop;
  2540. }
  2541. goto out;
  2542. default:
  2543. err = true;
  2544. netdev_dbg(amt->dev, "Invalid type of Gateway\n");
  2545. break;
  2546. }
  2547. } else {
  2548. switch (type) {
  2549. case AMT_MSG_DISCOVERY:
  2550. err = amt_discovery_handler(amt, skb);
  2551. break;
  2552. case AMT_MSG_REQUEST:
  2553. err = amt_request_handler(amt, skb);
  2554. break;
  2555. case AMT_MSG_MEMBERSHIP_UPDATE:
  2556. err = amt_update_handler(amt, skb);
  2557. if (err)
  2558. goto drop;
  2559. else
  2560. goto out;
  2561. default:
  2562. err = true;
  2563. netdev_dbg(amt->dev, "Invalid type of relay\n");
  2564. break;
  2565. }
  2566. }
  2567. drop:
  2568. if (err) {
  2569. amt->dev->stats.rx_dropped++;
  2570. kfree_skb(skb);
  2571. } else {
  2572. consume_skb(skb);
  2573. }
  2574. out:
  2575. rcu_read_unlock_bh();
  2576. return 0;
  2577. }
  2578. static void amt_event_work(struct work_struct *work)
  2579. {
  2580. struct amt_dev *amt = container_of(work, struct amt_dev, event_wq);
  2581. struct sk_buff *skb;
  2582. u8 event;
  2583. int i;
  2584. for (i = 0; i < AMT_MAX_EVENTS; i++) {
  2585. spin_lock_bh(&amt->lock);
  2586. if (amt->nr_events == 0) {
  2587. spin_unlock_bh(&amt->lock);
  2588. return;
  2589. }
  2590. event = amt->events[amt->event_idx].event;
  2591. skb = amt->events[amt->event_idx].skb;
  2592. amt->events[amt->event_idx].event = AMT_EVENT_NONE;
  2593. amt->events[amt->event_idx].skb = NULL;
  2594. amt->nr_events--;
  2595. amt->event_idx++;
  2596. amt->event_idx %= AMT_MAX_EVENTS;
  2597. spin_unlock_bh(&amt->lock);
  2598. switch (event) {
  2599. case AMT_EVENT_RECEIVE:
  2600. amt_gw_rcv(amt, skb);
  2601. break;
  2602. case AMT_EVENT_SEND_DISCOVERY:
  2603. amt_event_send_discovery(amt);
  2604. break;
  2605. case AMT_EVENT_SEND_REQUEST:
  2606. amt_event_send_request(amt);
  2607. break;
  2608. default:
  2609. kfree_skb(skb);
  2610. break;
  2611. }
  2612. }
  2613. }
  2614. static int amt_err_lookup(struct sock *sk, struct sk_buff *skb)
  2615. {
  2616. struct amt_dev *amt;
  2617. int type;
  2618. rcu_read_lock_bh();
  2619. amt = rcu_dereference_sk_user_data(sk);
  2620. if (!amt)
  2621. goto out;
  2622. if (amt->mode != AMT_MODE_GATEWAY)
  2623. goto drop;
  2624. type = amt_parse_type(skb);
  2625. if (type == -1)
  2626. goto drop;
  2627. netdev_dbg(amt->dev, "Received IGMP Unreachable of %s\n",
  2628. type_str[type]);
  2629. switch (type) {
  2630. case AMT_MSG_DISCOVERY:
  2631. break;
  2632. case AMT_MSG_REQUEST:
  2633. case AMT_MSG_MEMBERSHIP_UPDATE:
  2634. if (READ_ONCE(amt->status) >= AMT_STATUS_RECEIVED_ADVERTISEMENT)
  2635. mod_delayed_work(amt_wq, &amt->req_wq, 0);
  2636. break;
  2637. default:
  2638. goto drop;
  2639. }
  2640. out:
  2641. rcu_read_unlock_bh();
  2642. return 0;
  2643. drop:
  2644. rcu_read_unlock_bh();
  2645. amt->dev->stats.rx_dropped++;
  2646. return 0;
  2647. }
  2648. static struct socket *amt_create_sock(struct net *net, __be16 port)
  2649. {
  2650. struct udp_port_cfg udp_conf;
  2651. struct socket *sock;
  2652. int err;
  2653. memset(&udp_conf, 0, sizeof(udp_conf));
  2654. udp_conf.family = AF_INET;
  2655. udp_conf.local_ip.s_addr = htonl(INADDR_ANY);
  2656. udp_conf.local_udp_port = port;
  2657. err = udp_sock_create(net, &udp_conf, &sock);
  2658. if (err < 0)
  2659. return ERR_PTR(err);
  2660. return sock;
  2661. }
  2662. static int amt_socket_create(struct amt_dev *amt)
  2663. {
  2664. struct udp_tunnel_sock_cfg tunnel_cfg;
  2665. struct socket *sock;
  2666. sock = amt_create_sock(amt->net, amt->relay_port);
  2667. if (IS_ERR(sock))
  2668. return PTR_ERR(sock);
  2669. /* Mark socket as an encapsulation socket */
  2670. memset(&tunnel_cfg, 0, sizeof(tunnel_cfg));
  2671. tunnel_cfg.sk_user_data = amt;
  2672. tunnel_cfg.encap_type = 1;
  2673. tunnel_cfg.encap_rcv = amt_rcv;
  2674. tunnel_cfg.encap_err_lookup = amt_err_lookup;
  2675. tunnel_cfg.encap_destroy = NULL;
  2676. setup_udp_tunnel_sock(amt->net, sock, &tunnel_cfg);
  2677. rcu_assign_pointer(amt->sock, sock);
  2678. return 0;
  2679. }
  2680. static int amt_dev_open(struct net_device *dev)
  2681. {
  2682. struct amt_dev *amt = netdev_priv(dev);
  2683. int err;
  2684. amt->ready4 = false;
  2685. amt->ready6 = false;
  2686. amt->event_idx = 0;
  2687. amt->nr_events = 0;
  2688. err = amt_socket_create(amt);
  2689. if (err)
  2690. return err;
  2691. amt->req_cnt = 0;
  2692. amt->remote_ip = 0;
  2693. amt->nonce = 0;
  2694. get_random_bytes(&amt->key, sizeof(siphash_key_t));
  2695. amt->status = AMT_STATUS_INIT;
  2696. if (amt->mode == AMT_MODE_GATEWAY) {
  2697. mod_delayed_work(amt_wq, &amt->discovery_wq, 0);
  2698. mod_delayed_work(amt_wq, &amt->req_wq, 0);
  2699. } else if (amt->mode == AMT_MODE_RELAY) {
  2700. mod_delayed_work(amt_wq, &amt->secret_wq,
  2701. msecs_to_jiffies(AMT_SECRET_TIMEOUT));
  2702. }
  2703. return err;
  2704. }
  2705. static int amt_dev_stop(struct net_device *dev)
  2706. {
  2707. struct amt_dev *amt = netdev_priv(dev);
  2708. struct amt_tunnel_list *tunnel, *tmp;
  2709. struct socket *sock;
  2710. struct sk_buff *skb;
  2711. int i;
  2712. cancel_delayed_work_sync(&amt->req_wq);
  2713. cancel_delayed_work_sync(&amt->discovery_wq);
  2714. cancel_delayed_work_sync(&amt->secret_wq);
  2715. /* shutdown */
  2716. sock = rtnl_dereference(amt->sock);
  2717. RCU_INIT_POINTER(amt->sock, NULL);
  2718. synchronize_net();
  2719. if (sock)
  2720. udp_tunnel_sock_release(sock);
  2721. cancel_work_sync(&amt->event_wq);
  2722. for (i = 0; i < AMT_MAX_EVENTS; i++) {
  2723. skb = amt->events[i].skb;
  2724. kfree_skb(skb);
  2725. amt->events[i].event = AMT_EVENT_NONE;
  2726. amt->events[i].skb = NULL;
  2727. }
  2728. amt->ready4 = false;
  2729. amt->ready6 = false;
  2730. amt->req_cnt = 0;
  2731. amt->remote_ip = 0;
  2732. list_for_each_entry_safe(tunnel, tmp, &amt->tunnel_list, list) {
  2733. list_del_rcu(&tunnel->list);
  2734. amt->nr_tunnels--;
  2735. cancel_delayed_work_sync(&tunnel->gc_wq);
  2736. amt_clear_groups(tunnel);
  2737. kfree_rcu(tunnel, rcu);
  2738. }
  2739. return 0;
  2740. }
  2741. static const struct device_type amt_type = {
  2742. .name = "amt",
  2743. };
  2744. static int amt_dev_init(struct net_device *dev)
  2745. {
  2746. struct amt_dev *amt = netdev_priv(dev);
  2747. int err;
  2748. amt->dev = dev;
  2749. err = gro_cells_init(&amt->gro_cells, dev);
  2750. if (err)
  2751. return err;
  2752. return 0;
  2753. }
  2754. static void amt_dev_uninit(struct net_device *dev)
  2755. {
  2756. struct amt_dev *amt = netdev_priv(dev);
  2757. gro_cells_destroy(&amt->gro_cells);
  2758. }
  2759. static const struct net_device_ops amt_netdev_ops = {
  2760. .ndo_init = amt_dev_init,
  2761. .ndo_uninit = amt_dev_uninit,
  2762. .ndo_open = amt_dev_open,
  2763. .ndo_stop = amt_dev_stop,
  2764. .ndo_start_xmit = amt_dev_xmit,
  2765. };
  2766. static void amt_link_setup(struct net_device *dev)
  2767. {
  2768. dev->netdev_ops = &amt_netdev_ops;
  2769. dev->needs_free_netdev = true;
  2770. SET_NETDEV_DEVTYPE(dev, &amt_type);
  2771. dev->min_mtu = ETH_MIN_MTU;
  2772. dev->max_mtu = ETH_MAX_MTU;
  2773. dev->type = ARPHRD_NONE;
  2774. dev->flags = IFF_POINTOPOINT | IFF_NOARP | IFF_MULTICAST;
  2775. dev->hard_header_len = 0;
  2776. dev->addr_len = 0;
  2777. dev->priv_flags |= IFF_NO_QUEUE;
  2778. dev->lltx = true;
  2779. dev->netns_immutable = true;
  2780. dev->features |= NETIF_F_GSO_SOFTWARE;
  2781. dev->hw_features |= NETIF_F_SG | NETIF_F_HW_CSUM;
  2782. dev->hw_features |= NETIF_F_FRAGLIST | NETIF_F_RXCSUM;
  2783. dev->hw_features |= NETIF_F_GSO_SOFTWARE;
  2784. dev->pcpu_stat_type = NETDEV_PCPU_STAT_TSTATS;
  2785. eth_hw_addr_random(dev);
  2786. eth_zero_addr(dev->broadcast);
  2787. ether_setup(dev);
  2788. }
  2789. static const struct nla_policy amt_policy[IFLA_AMT_MAX + 1] = {
  2790. [IFLA_AMT_MODE] = { .type = NLA_U32 },
  2791. [IFLA_AMT_RELAY_PORT] = { .type = NLA_U16 },
  2792. [IFLA_AMT_GATEWAY_PORT] = { .type = NLA_U16 },
  2793. [IFLA_AMT_LINK] = { .type = NLA_U32 },
  2794. [IFLA_AMT_LOCAL_IP] = { .len = sizeof_field(struct iphdr, daddr) },
  2795. [IFLA_AMT_REMOTE_IP] = { .len = sizeof_field(struct iphdr, daddr) },
  2796. [IFLA_AMT_DISCOVERY_IP] = { .len = sizeof_field(struct iphdr, daddr) },
  2797. [IFLA_AMT_MAX_TUNNELS] = { .type = NLA_U32 },
  2798. };
  2799. static int amt_validate(struct nlattr *tb[], struct nlattr *data[],
  2800. struct netlink_ext_ack *extack)
  2801. {
  2802. if (!data)
  2803. return -EINVAL;
  2804. if (!data[IFLA_AMT_LINK]) {
  2805. NL_SET_ERR_MSG_ATTR(extack, data[IFLA_AMT_LINK],
  2806. "Link attribute is required");
  2807. return -EINVAL;
  2808. }
  2809. if (!data[IFLA_AMT_MODE]) {
  2810. NL_SET_ERR_MSG_ATTR(extack, data[IFLA_AMT_MODE],
  2811. "Mode attribute is required");
  2812. return -EINVAL;
  2813. }
  2814. if (nla_get_u32(data[IFLA_AMT_MODE]) > AMT_MODE_MAX) {
  2815. NL_SET_ERR_MSG_ATTR(extack, data[IFLA_AMT_MODE],
  2816. "Mode attribute is not valid");
  2817. return -EINVAL;
  2818. }
  2819. if (!data[IFLA_AMT_LOCAL_IP]) {
  2820. NL_SET_ERR_MSG_ATTR(extack, data[IFLA_AMT_DISCOVERY_IP],
  2821. "Local attribute is required");
  2822. return -EINVAL;
  2823. }
  2824. if (!data[IFLA_AMT_DISCOVERY_IP] &&
  2825. nla_get_u32(data[IFLA_AMT_MODE]) == AMT_MODE_GATEWAY) {
  2826. NL_SET_ERR_MSG_ATTR(extack, data[IFLA_AMT_LOCAL_IP],
  2827. "Discovery attribute is required");
  2828. return -EINVAL;
  2829. }
  2830. return 0;
  2831. }
  2832. static int amt_newlink(struct net_device *dev,
  2833. struct rtnl_newlink_params *params,
  2834. struct netlink_ext_ack *extack)
  2835. {
  2836. struct net *link_net = rtnl_newlink_link_net(params);
  2837. struct amt_dev *amt = netdev_priv(dev);
  2838. struct nlattr **data = params->data;
  2839. struct nlattr **tb = params->tb;
  2840. int err = -EINVAL;
  2841. amt->net = link_net;
  2842. amt->mode = nla_get_u32(data[IFLA_AMT_MODE]);
  2843. if (data[IFLA_AMT_MAX_TUNNELS] &&
  2844. nla_get_u32(data[IFLA_AMT_MAX_TUNNELS]))
  2845. amt->max_tunnels = nla_get_u32(data[IFLA_AMT_MAX_TUNNELS]);
  2846. else
  2847. amt->max_tunnels = AMT_MAX_TUNNELS;
  2848. spin_lock_init(&amt->lock);
  2849. amt->max_groups = AMT_MAX_GROUP;
  2850. amt->max_sources = AMT_MAX_SOURCE;
  2851. amt->hash_buckets = AMT_HSIZE;
  2852. amt->nr_tunnels = 0;
  2853. get_random_bytes(&amt->hash_seed, sizeof(amt->hash_seed));
  2854. amt->stream_dev = dev_get_by_index(link_net,
  2855. nla_get_u32(data[IFLA_AMT_LINK]));
  2856. if (!amt->stream_dev) {
  2857. NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_AMT_LINK],
  2858. "Can't find stream device");
  2859. return -ENODEV;
  2860. }
  2861. if (amt->stream_dev->type != ARPHRD_ETHER) {
  2862. NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_AMT_LINK],
  2863. "Invalid stream device type");
  2864. goto err;
  2865. }
  2866. amt->local_ip = nla_get_in_addr(data[IFLA_AMT_LOCAL_IP]);
  2867. if (ipv4_is_loopback(amt->local_ip) ||
  2868. ipv4_is_zeronet(amt->local_ip) ||
  2869. ipv4_is_multicast(amt->local_ip)) {
  2870. NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_AMT_LOCAL_IP],
  2871. "Invalid Local address");
  2872. goto err;
  2873. }
  2874. amt->relay_port = nla_get_be16_default(data[IFLA_AMT_RELAY_PORT],
  2875. htons(IANA_AMT_UDP_PORT));
  2876. amt->gw_port = nla_get_be16_default(data[IFLA_AMT_GATEWAY_PORT],
  2877. htons(IANA_AMT_UDP_PORT));
  2878. if (!amt->relay_port) {
  2879. NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_AMT_DISCOVERY_IP],
  2880. "relay port must not be 0");
  2881. goto err;
  2882. }
  2883. if (amt->mode == AMT_MODE_RELAY) {
  2884. amt->qrv = READ_ONCE(amt->net->ipv4.sysctl_igmp_qrv);
  2885. amt->qri = 10;
  2886. dev->needed_headroom = amt->stream_dev->needed_headroom +
  2887. AMT_RELAY_HLEN;
  2888. dev->mtu = amt->stream_dev->mtu - AMT_RELAY_HLEN;
  2889. dev->max_mtu = dev->mtu;
  2890. dev->min_mtu = ETH_MIN_MTU + AMT_RELAY_HLEN;
  2891. } else {
  2892. if (!data[IFLA_AMT_DISCOVERY_IP]) {
  2893. NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_AMT_DISCOVERY_IP],
  2894. "discovery must be set in gateway mode");
  2895. goto err;
  2896. }
  2897. if (!amt->gw_port) {
  2898. NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_AMT_DISCOVERY_IP],
  2899. "gateway port must not be 0");
  2900. goto err;
  2901. }
  2902. amt->remote_ip = 0;
  2903. amt->discovery_ip = nla_get_in_addr(data[IFLA_AMT_DISCOVERY_IP]);
  2904. if (ipv4_is_loopback(amt->discovery_ip) ||
  2905. ipv4_is_zeronet(amt->discovery_ip) ||
  2906. ipv4_is_multicast(amt->discovery_ip)) {
  2907. NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_AMT_DISCOVERY_IP],
  2908. "discovery must be unicast");
  2909. goto err;
  2910. }
  2911. dev->needed_headroom = amt->stream_dev->needed_headroom +
  2912. AMT_GW_HLEN;
  2913. dev->mtu = amt->stream_dev->mtu - AMT_GW_HLEN;
  2914. dev->max_mtu = dev->mtu;
  2915. dev->min_mtu = ETH_MIN_MTU + AMT_GW_HLEN;
  2916. }
  2917. amt->qi = AMT_INIT_QUERY_INTERVAL;
  2918. err = register_netdevice(dev);
  2919. if (err < 0) {
  2920. netdev_dbg(dev, "failed to register new netdev %d\n", err);
  2921. goto err;
  2922. }
  2923. err = netdev_upper_dev_link(amt->stream_dev, dev, extack);
  2924. if (err < 0) {
  2925. unregister_netdevice(dev);
  2926. goto err;
  2927. }
  2928. INIT_DELAYED_WORK(&amt->discovery_wq, amt_discovery_work);
  2929. INIT_DELAYED_WORK(&amt->req_wq, amt_req_work);
  2930. INIT_DELAYED_WORK(&amt->secret_wq, amt_secret_work);
  2931. INIT_WORK(&amt->event_wq, amt_event_work);
  2932. INIT_LIST_HEAD(&amt->tunnel_list);
  2933. return 0;
  2934. err:
  2935. dev_put(amt->stream_dev);
  2936. return err;
  2937. }
  2938. static void amt_dellink(struct net_device *dev, struct list_head *head)
  2939. {
  2940. struct amt_dev *amt = netdev_priv(dev);
  2941. unregister_netdevice_queue(dev, head);
  2942. netdev_upper_dev_unlink(amt->stream_dev, dev);
  2943. dev_put(amt->stream_dev);
  2944. }
  2945. static size_t amt_get_size(const struct net_device *dev)
  2946. {
  2947. return nla_total_size(sizeof(__u32)) + /* IFLA_AMT_MODE */
  2948. nla_total_size(sizeof(__u16)) + /* IFLA_AMT_RELAY_PORT */
  2949. nla_total_size(sizeof(__u16)) + /* IFLA_AMT_GATEWAY_PORT */
  2950. nla_total_size(sizeof(__u32)) + /* IFLA_AMT_LINK */
  2951. nla_total_size(sizeof(__u32)) + /* IFLA_MAX_TUNNELS */
  2952. nla_total_size(sizeof(struct iphdr)) + /* IFLA_AMT_DISCOVERY_IP */
  2953. nla_total_size(sizeof(struct iphdr)) + /* IFLA_AMT_REMOTE_IP */
  2954. nla_total_size(sizeof(struct iphdr)); /* IFLA_AMT_LOCAL_IP */
  2955. }
  2956. static int amt_fill_info(struct sk_buff *skb, const struct net_device *dev)
  2957. {
  2958. struct amt_dev *amt = netdev_priv(dev);
  2959. if (nla_put_u32(skb, IFLA_AMT_MODE, amt->mode))
  2960. goto nla_put_failure;
  2961. if (nla_put_be16(skb, IFLA_AMT_RELAY_PORT, amt->relay_port))
  2962. goto nla_put_failure;
  2963. if (nla_put_be16(skb, IFLA_AMT_GATEWAY_PORT, amt->gw_port))
  2964. goto nla_put_failure;
  2965. if (nla_put_u32(skb, IFLA_AMT_LINK, amt->stream_dev->ifindex))
  2966. goto nla_put_failure;
  2967. if (nla_put_in_addr(skb, IFLA_AMT_LOCAL_IP, amt->local_ip))
  2968. goto nla_put_failure;
  2969. if (nla_put_in_addr(skb, IFLA_AMT_DISCOVERY_IP, amt->discovery_ip))
  2970. goto nla_put_failure;
  2971. if (amt->remote_ip)
  2972. if (nla_put_in_addr(skb, IFLA_AMT_REMOTE_IP, amt->remote_ip))
  2973. goto nla_put_failure;
  2974. if (nla_put_u32(skb, IFLA_AMT_MAX_TUNNELS, amt->max_tunnels))
  2975. goto nla_put_failure;
  2976. return 0;
  2977. nla_put_failure:
  2978. return -EMSGSIZE;
  2979. }
  2980. static struct rtnl_link_ops amt_link_ops __read_mostly = {
  2981. .kind = "amt",
  2982. .maxtype = IFLA_AMT_MAX,
  2983. .policy = amt_policy,
  2984. .priv_size = sizeof(struct amt_dev),
  2985. .setup = amt_link_setup,
  2986. .validate = amt_validate,
  2987. .newlink = amt_newlink,
  2988. .dellink = amt_dellink,
  2989. .get_size = amt_get_size,
  2990. .fill_info = amt_fill_info,
  2991. };
  2992. static struct net_device *amt_lookup_upper_dev(struct net_device *dev)
  2993. {
  2994. struct net_device *upper_dev;
  2995. struct amt_dev *amt;
  2996. for_each_netdev(dev_net(dev), upper_dev) {
  2997. if (netif_is_amt(upper_dev)) {
  2998. amt = netdev_priv(upper_dev);
  2999. if (amt->stream_dev == dev)
  3000. return upper_dev;
  3001. }
  3002. }
  3003. return NULL;
  3004. }
  3005. static int amt_device_event(struct notifier_block *unused,
  3006. unsigned long event, void *ptr)
  3007. {
  3008. struct net_device *dev = netdev_notifier_info_to_dev(ptr);
  3009. struct net_device *upper_dev;
  3010. struct amt_dev *amt;
  3011. LIST_HEAD(list);
  3012. int new_mtu;
  3013. upper_dev = amt_lookup_upper_dev(dev);
  3014. if (!upper_dev)
  3015. return NOTIFY_DONE;
  3016. amt = netdev_priv(upper_dev);
  3017. switch (event) {
  3018. case NETDEV_UNREGISTER:
  3019. amt_dellink(amt->dev, &list);
  3020. unregister_netdevice_many(&list);
  3021. break;
  3022. case NETDEV_CHANGEMTU:
  3023. if (amt->mode == AMT_MODE_RELAY)
  3024. new_mtu = dev->mtu - AMT_RELAY_HLEN;
  3025. else
  3026. new_mtu = dev->mtu - AMT_GW_HLEN;
  3027. dev_set_mtu(amt->dev, new_mtu);
  3028. break;
  3029. }
  3030. return NOTIFY_DONE;
  3031. }
  3032. static struct notifier_block amt_notifier_block __read_mostly = {
  3033. .notifier_call = amt_device_event,
  3034. };
  3035. static int __init amt_init(void)
  3036. {
  3037. int err;
  3038. err = register_netdevice_notifier(&amt_notifier_block);
  3039. if (err < 0)
  3040. goto err;
  3041. err = rtnl_link_register(&amt_link_ops);
  3042. if (err < 0)
  3043. goto unregister_notifier;
  3044. amt_wq = alloc_workqueue("amt", WQ_UNBOUND, 0);
  3045. if (!amt_wq) {
  3046. err = -ENOMEM;
  3047. goto rtnl_unregister;
  3048. }
  3049. spin_lock_init(&source_gc_lock);
  3050. spin_lock_bh(&source_gc_lock);
  3051. INIT_DELAYED_WORK(&source_gc_wq, amt_source_gc_work);
  3052. mod_delayed_work(amt_wq, &source_gc_wq,
  3053. msecs_to_jiffies(AMT_GC_INTERVAL));
  3054. spin_unlock_bh(&source_gc_lock);
  3055. return 0;
  3056. rtnl_unregister:
  3057. rtnl_link_unregister(&amt_link_ops);
  3058. unregister_notifier:
  3059. unregister_netdevice_notifier(&amt_notifier_block);
  3060. err:
  3061. pr_err("error loading AMT module loaded\n");
  3062. return err;
  3063. }
  3064. late_initcall(amt_init);
  3065. static void __exit amt_fini(void)
  3066. {
  3067. rtnl_link_unregister(&amt_link_ops);
  3068. unregister_netdevice_notifier(&amt_notifier_block);
  3069. cancel_delayed_work_sync(&source_gc_wq);
  3070. __amt_source_gc_work();
  3071. destroy_workqueue(amt_wq);
  3072. }
  3073. module_exit(amt_fini);
  3074. MODULE_LICENSE("GPL");
  3075. MODULE_DESCRIPTION("Driver for Automatic Multicast Tunneling (AMT)");
  3076. MODULE_AUTHOR("Taehee Yoo <ap420073@gmail.com>");
  3077. MODULE_ALIAS_RTNL_LINK("amt");