vxlan.c 72 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896
  1. /*
  2. * VXLAN: Virtual eXtensible Local Area Network
  3. *
  4. * Copyright (c) 2012-2013 Vyatta Inc.
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. */
  10. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  11. #include <linux/kernel.h>
  12. #include <linux/types.h>
  13. #include <linux/module.h>
  14. #include <linux/errno.h>
  15. #include <linux/slab.h>
  16. #include <linux/skbuff.h>
  17. #include <linux/rculist.h>
  18. #include <linux/netdevice.h>
  19. #include <linux/in.h>
  20. #include <linux/ip.h>
  21. #include <linux/udp.h>
  22. #include <linux/igmp.h>
  23. #include <linux/etherdevice.h>
  24. #include <linux/if_ether.h>
  25. #include <linux/if_vlan.h>
  26. #include <linux/hash.h>
  27. #include <linux/ethtool.h>
  28. #include <net/arp.h>
  29. #include <net/ndisc.h>
  30. #include <net/ip.h>
  31. #include <net/ip_tunnels.h>
  32. #include <net/icmp.h>
  33. #include <net/udp.h>
  34. #include <net/udp_tunnel.h>
  35. #include <net/rtnetlink.h>
  36. #include <net/route.h>
  37. #include <net/dsfield.h>
  38. #include <net/inet_ecn.h>
  39. #include <net/net_namespace.h>
  40. #include <net/netns/generic.h>
  41. #include <net/vxlan.h>
  42. #include <net/protocol.h>
  43. #if IS_ENABLED(CONFIG_IPV6)
  44. #include <net/ipv6.h>
  45. #include <net/addrconf.h>
  46. #include <net/ip6_tunnel.h>
  47. #include <net/ip6_checksum.h>
  48. #endif
  49. #define VXLAN_VERSION "0.1"
  50. #define PORT_HASH_BITS 8
  51. #define PORT_HASH_SIZE (1<<PORT_HASH_BITS)
  52. #define VNI_HASH_BITS 10
  53. #define VNI_HASH_SIZE (1<<VNI_HASH_BITS)
  54. #define FDB_HASH_BITS 8
  55. #define FDB_HASH_SIZE (1<<FDB_HASH_BITS)
  56. #define FDB_AGE_DEFAULT 300 /* 5 min */
  57. #define FDB_AGE_INTERVAL (10 * HZ) /* rescan interval */
  58. #define VXLAN_N_VID (1u << 24)
  59. #define VXLAN_VID_MASK (VXLAN_N_VID - 1)
  60. #define VXLAN_HLEN (sizeof(struct udphdr) + sizeof(struct vxlanhdr))
  61. #define VXLAN_FLAGS 0x08000000 /* struct vxlanhdr.vx_flags required value. */
  62. /* VXLAN protocol header */
  63. struct vxlanhdr {
  64. __be32 vx_flags;
  65. __be32 vx_vni;
  66. };
  67. /* UDP port for VXLAN traffic.
  68. * The IANA assigned port is 4789, but the Linux default is 8472
  69. * for compatibility with early adopters.
  70. */
  71. static unsigned short vxlan_port __read_mostly = 8472;
  72. module_param_named(udp_port, vxlan_port, ushort, 0444);
  73. MODULE_PARM_DESC(udp_port, "Destination UDP port");
  74. static bool log_ecn_error = true;
  75. module_param(log_ecn_error, bool, 0644);
  76. MODULE_PARM_DESC(log_ecn_error, "Log packets received with corrupted ECN");
  77. static int vxlan_net_id;
  78. static const u8 all_zeros_mac[ETH_ALEN];
  79. /* per-network namespace private data for this module */
  80. struct vxlan_net {
  81. struct list_head vxlan_list;
  82. struct hlist_head sock_list[PORT_HASH_SIZE];
  83. spinlock_t sock_lock;
  84. };
  85. union vxlan_addr {
  86. struct sockaddr_in sin;
  87. struct sockaddr_in6 sin6;
  88. struct sockaddr sa;
  89. };
  90. struct vxlan_rdst {
  91. union vxlan_addr remote_ip;
  92. __be16 remote_port;
  93. u32 remote_vni;
  94. u32 remote_ifindex;
  95. struct list_head list;
  96. struct rcu_head rcu;
  97. };
  98. /* Forwarding table entry */
  99. struct vxlan_fdb {
  100. struct hlist_node hlist; /* linked list of entries */
  101. struct rcu_head rcu;
  102. unsigned long updated; /* jiffies */
  103. unsigned long used;
  104. struct list_head remotes;
  105. u16 state; /* see ndm_state */
  106. u8 flags; /* see ndm_flags */
  107. u8 eth_addr[ETH_ALEN];
  108. };
  109. /* Pseudo network device */
  110. struct vxlan_dev {
  111. struct hlist_node hlist; /* vni hash table */
  112. struct list_head next; /* vxlan's per namespace list */
  113. struct vxlan_sock *vn_sock; /* listening socket */
  114. struct net_device *dev;
  115. struct net *net; /* netns for packet i/o */
  116. struct vxlan_rdst default_dst; /* default destination */
  117. union vxlan_addr saddr; /* source address */
  118. __be16 dst_port;
  119. __u16 port_min; /* source port range */
  120. __u16 port_max;
  121. __u8 tos; /* TOS override */
  122. __u8 ttl;
  123. u32 flags; /* VXLAN_F_* in vxlan.h */
  124. struct work_struct sock_work;
  125. struct work_struct igmp_join;
  126. struct work_struct igmp_leave;
  127. unsigned long age_interval;
  128. struct timer_list age_timer;
  129. spinlock_t hash_lock;
  130. unsigned int addrcnt;
  131. unsigned int addrmax;
  132. struct hlist_head fdb_head[FDB_HASH_SIZE];
  133. };
  134. /* salt for hash table */
  135. static u32 vxlan_salt __read_mostly;
  136. static struct workqueue_struct *vxlan_wq;
  137. static void vxlan_sock_work(struct work_struct *work);
  138. #if IS_ENABLED(CONFIG_IPV6)
  139. static inline
  140. bool vxlan_addr_equal(const union vxlan_addr *a, const union vxlan_addr *b)
  141. {
  142. if (a->sa.sa_family != b->sa.sa_family)
  143. return false;
  144. if (a->sa.sa_family == AF_INET6)
  145. return ipv6_addr_equal(&a->sin6.sin6_addr, &b->sin6.sin6_addr);
  146. else
  147. return a->sin.sin_addr.s_addr == b->sin.sin_addr.s_addr;
  148. }
  149. static inline bool vxlan_addr_any(const union vxlan_addr *ipa)
  150. {
  151. if (ipa->sa.sa_family == AF_INET6)
  152. return ipv6_addr_any(&ipa->sin6.sin6_addr);
  153. else
  154. return ipa->sin.sin_addr.s_addr == htonl(INADDR_ANY);
  155. }
  156. static inline bool vxlan_addr_multicast(const union vxlan_addr *ipa)
  157. {
  158. if (ipa->sa.sa_family == AF_INET6)
  159. return ipv6_addr_is_multicast(&ipa->sin6.sin6_addr);
  160. else
  161. return IN_MULTICAST(ntohl(ipa->sin.sin_addr.s_addr));
  162. }
  163. static int vxlan_nla_get_addr(union vxlan_addr *ip, struct nlattr *nla)
  164. {
  165. if (nla_len(nla) >= sizeof(struct in6_addr)) {
  166. nla_memcpy(&ip->sin6.sin6_addr, nla, sizeof(struct in6_addr));
  167. ip->sa.sa_family = AF_INET6;
  168. return 0;
  169. } else if (nla_len(nla) >= sizeof(__be32)) {
  170. ip->sin.sin_addr.s_addr = nla_get_be32(nla);
  171. ip->sa.sa_family = AF_INET;
  172. return 0;
  173. } else {
  174. return -EAFNOSUPPORT;
  175. }
  176. }
  177. static int vxlan_nla_put_addr(struct sk_buff *skb, int attr,
  178. const union vxlan_addr *ip)
  179. {
  180. if (ip->sa.sa_family == AF_INET6)
  181. return nla_put(skb, attr, sizeof(struct in6_addr), &ip->sin6.sin6_addr);
  182. else
  183. return nla_put_be32(skb, attr, ip->sin.sin_addr.s_addr);
  184. }
  185. #else /* !CONFIG_IPV6 */
  186. static inline
  187. bool vxlan_addr_equal(const union vxlan_addr *a, const union vxlan_addr *b)
  188. {
  189. return a->sin.sin_addr.s_addr == b->sin.sin_addr.s_addr;
  190. }
  191. static inline bool vxlan_addr_any(const union vxlan_addr *ipa)
  192. {
  193. return ipa->sin.sin_addr.s_addr == htonl(INADDR_ANY);
  194. }
  195. static inline bool vxlan_addr_multicast(const union vxlan_addr *ipa)
  196. {
  197. return IN_MULTICAST(ntohl(ipa->sin.sin_addr.s_addr));
  198. }
  199. static int vxlan_nla_get_addr(union vxlan_addr *ip, struct nlattr *nla)
  200. {
  201. if (nla_len(nla) >= sizeof(struct in6_addr)) {
  202. return -EAFNOSUPPORT;
  203. } else if (nla_len(nla) >= sizeof(__be32)) {
  204. ip->sin.sin_addr.s_addr = nla_get_be32(nla);
  205. ip->sa.sa_family = AF_INET;
  206. return 0;
  207. } else {
  208. return -EAFNOSUPPORT;
  209. }
  210. }
  211. static int vxlan_nla_put_addr(struct sk_buff *skb, int attr,
  212. const union vxlan_addr *ip)
  213. {
  214. return nla_put_be32(skb, attr, ip->sin.sin_addr.s_addr);
  215. }
  216. #endif
  217. /* Virtual Network hash table head */
  218. static inline struct hlist_head *vni_head(struct vxlan_sock *vs, u32 id)
  219. {
  220. return &vs->vni_list[hash_32(id, VNI_HASH_BITS)];
  221. }
  222. /* Socket hash table head */
  223. static inline struct hlist_head *vs_head(struct net *net, __be16 port)
  224. {
  225. struct vxlan_net *vn = net_generic(net, vxlan_net_id);
  226. return &vn->sock_list[hash_32(ntohs(port), PORT_HASH_BITS)];
  227. }
  228. /* First remote destination for a forwarding entry.
  229. * Guaranteed to be non-NULL because remotes are never deleted.
  230. */
  231. static inline struct vxlan_rdst *first_remote_rcu(struct vxlan_fdb *fdb)
  232. {
  233. return list_entry_rcu(fdb->remotes.next, struct vxlan_rdst, list);
  234. }
  235. static inline struct vxlan_rdst *first_remote_rtnl(struct vxlan_fdb *fdb)
  236. {
  237. return list_first_entry(&fdb->remotes, struct vxlan_rdst, list);
  238. }
  239. /* Find VXLAN socket based on network namespace and UDP port */
  240. static struct vxlan_sock *vxlan_find_sock(struct net *net, __be16 port)
  241. {
  242. struct vxlan_sock *vs;
  243. hlist_for_each_entry_rcu(vs, vs_head(net, port), hlist) {
  244. if (inet_sk(vs->sock->sk)->inet_sport == port)
  245. return vs;
  246. }
  247. return NULL;
  248. }
  249. static struct vxlan_dev *vxlan_vs_find_vni(struct vxlan_sock *vs, u32 id)
  250. {
  251. struct vxlan_dev *vxlan;
  252. hlist_for_each_entry_rcu(vxlan, vni_head(vs, id), hlist) {
  253. if (vxlan->default_dst.remote_vni == id)
  254. return vxlan;
  255. }
  256. return NULL;
  257. }
  258. /* Look up VNI in a per net namespace table */
  259. static struct vxlan_dev *vxlan_find_vni(struct net *net, u32 id, __be16 port)
  260. {
  261. struct vxlan_sock *vs;
  262. vs = vxlan_find_sock(net, port);
  263. if (!vs)
  264. return NULL;
  265. return vxlan_vs_find_vni(vs, id);
  266. }
  267. /* Fill in neighbour message in skbuff. */
  268. static int vxlan_fdb_info(struct sk_buff *skb, struct vxlan_dev *vxlan,
  269. const struct vxlan_fdb *fdb,
  270. u32 portid, u32 seq, int type, unsigned int flags,
  271. const struct vxlan_rdst *rdst)
  272. {
  273. unsigned long now = jiffies;
  274. struct nda_cacheinfo ci;
  275. struct nlmsghdr *nlh;
  276. struct ndmsg *ndm;
  277. bool send_ip, send_eth;
  278. nlh = nlmsg_put(skb, portid, seq, type, sizeof(*ndm), flags);
  279. if (nlh == NULL)
  280. return -EMSGSIZE;
  281. ndm = nlmsg_data(nlh);
  282. memset(ndm, 0, sizeof(*ndm));
  283. send_eth = send_ip = true;
  284. if (type == RTM_GETNEIGH) {
  285. ndm->ndm_family = AF_INET;
  286. send_ip = !vxlan_addr_any(&rdst->remote_ip);
  287. send_eth = !is_zero_ether_addr(fdb->eth_addr);
  288. } else
  289. ndm->ndm_family = AF_BRIDGE;
  290. ndm->ndm_state = fdb->state;
  291. ndm->ndm_ifindex = vxlan->dev->ifindex;
  292. ndm->ndm_flags = fdb->flags;
  293. ndm->ndm_type = RTN_UNICAST;
  294. if (send_eth && nla_put(skb, NDA_LLADDR, ETH_ALEN, &fdb->eth_addr))
  295. goto nla_put_failure;
  296. if (send_ip && vxlan_nla_put_addr(skb, NDA_DST, &rdst->remote_ip))
  297. goto nla_put_failure;
  298. if (rdst->remote_port && rdst->remote_port != vxlan->dst_port &&
  299. nla_put_be16(skb, NDA_PORT, rdst->remote_port))
  300. goto nla_put_failure;
  301. if (rdst->remote_vni != vxlan->default_dst.remote_vni &&
  302. nla_put_u32(skb, NDA_VNI, rdst->remote_vni))
  303. goto nla_put_failure;
  304. if (rdst->remote_ifindex &&
  305. nla_put_u32(skb, NDA_IFINDEX, rdst->remote_ifindex))
  306. goto nla_put_failure;
  307. ci.ndm_used = jiffies_to_clock_t(now - fdb->used);
  308. ci.ndm_confirmed = 0;
  309. ci.ndm_updated = jiffies_to_clock_t(now - fdb->updated);
  310. ci.ndm_refcnt = 0;
  311. if (nla_put(skb, NDA_CACHEINFO, sizeof(ci), &ci))
  312. goto nla_put_failure;
  313. return nlmsg_end(skb, nlh);
  314. nla_put_failure:
  315. nlmsg_cancel(skb, nlh);
  316. return -EMSGSIZE;
  317. }
  318. static inline size_t vxlan_nlmsg_size(void)
  319. {
  320. return NLMSG_ALIGN(sizeof(struct ndmsg))
  321. + nla_total_size(ETH_ALEN) /* NDA_LLADDR */
  322. + nla_total_size(sizeof(struct in6_addr)) /* NDA_DST */
  323. + nla_total_size(sizeof(__be16)) /* NDA_PORT */
  324. + nla_total_size(sizeof(__be32)) /* NDA_VNI */
  325. + nla_total_size(sizeof(__u32)) /* NDA_IFINDEX */
  326. + nla_total_size(sizeof(struct nda_cacheinfo));
  327. }
  328. static void vxlan_fdb_notify(struct vxlan_dev *vxlan, struct vxlan_fdb *fdb,
  329. struct vxlan_rdst *rd, int type)
  330. {
  331. struct net *net = dev_net(vxlan->dev);
  332. struct sk_buff *skb;
  333. int err = -ENOBUFS;
  334. skb = nlmsg_new(vxlan_nlmsg_size(), GFP_ATOMIC);
  335. if (skb == NULL)
  336. goto errout;
  337. err = vxlan_fdb_info(skb, vxlan, fdb, 0, 0, type, 0, rd);
  338. if (err < 0) {
  339. /* -EMSGSIZE implies BUG in vxlan_nlmsg_size() */
  340. WARN_ON(err == -EMSGSIZE);
  341. kfree_skb(skb);
  342. goto errout;
  343. }
  344. rtnl_notify(skb, net, 0, RTNLGRP_NEIGH, NULL, GFP_ATOMIC);
  345. return;
  346. errout:
  347. if (err < 0)
  348. rtnl_set_sk_err(net, RTNLGRP_NEIGH, err);
  349. }
  350. static void vxlan_ip_miss(struct net_device *dev, union vxlan_addr *ipa)
  351. {
  352. struct vxlan_dev *vxlan = netdev_priv(dev);
  353. struct vxlan_fdb f = {
  354. .state = NUD_STALE,
  355. };
  356. struct vxlan_rdst remote = {
  357. .remote_ip = *ipa, /* goes to NDA_DST */
  358. .remote_vni = VXLAN_N_VID,
  359. };
  360. vxlan_fdb_notify(vxlan, &f, &remote, RTM_GETNEIGH);
  361. }
  362. static void vxlan_fdb_miss(struct vxlan_dev *vxlan, const u8 eth_addr[ETH_ALEN])
  363. {
  364. struct vxlan_fdb f = {
  365. .state = NUD_STALE,
  366. };
  367. struct vxlan_rdst remote = { };
  368. memcpy(f.eth_addr, eth_addr, ETH_ALEN);
  369. vxlan_fdb_notify(vxlan, &f, &remote, RTM_GETNEIGH);
  370. }
  371. /* Hash Ethernet address */
  372. static u32 eth_hash(const unsigned char *addr)
  373. {
  374. u64 value = get_unaligned((u64 *)addr);
  375. /* only want 6 bytes */
  376. #ifdef __BIG_ENDIAN
  377. value >>= 16;
  378. #else
  379. value <<= 16;
  380. #endif
  381. return hash_64(value, FDB_HASH_BITS);
  382. }
  383. /* Hash chain to use given mac address */
  384. static inline struct hlist_head *vxlan_fdb_head(struct vxlan_dev *vxlan,
  385. const u8 *mac)
  386. {
  387. return &vxlan->fdb_head[eth_hash(mac)];
  388. }
  389. /* Look up Ethernet address in forwarding table */
  390. static struct vxlan_fdb *__vxlan_find_mac(struct vxlan_dev *vxlan,
  391. const u8 *mac)
  392. {
  393. struct hlist_head *head = vxlan_fdb_head(vxlan, mac);
  394. struct vxlan_fdb *f;
  395. hlist_for_each_entry_rcu(f, head, hlist) {
  396. if (ether_addr_equal(mac, f->eth_addr))
  397. return f;
  398. }
  399. return NULL;
  400. }
  401. static struct vxlan_fdb *vxlan_find_mac(struct vxlan_dev *vxlan,
  402. const u8 *mac)
  403. {
  404. struct vxlan_fdb *f;
  405. f = __vxlan_find_mac(vxlan, mac);
  406. if (f)
  407. f->used = jiffies;
  408. return f;
  409. }
  410. /* caller should hold vxlan->hash_lock */
  411. static struct vxlan_rdst *vxlan_fdb_find_rdst(struct vxlan_fdb *f,
  412. union vxlan_addr *ip, __be16 port,
  413. __u32 vni, __u32 ifindex)
  414. {
  415. struct vxlan_rdst *rd;
  416. list_for_each_entry(rd, &f->remotes, list) {
  417. if (vxlan_addr_equal(&rd->remote_ip, ip) &&
  418. rd->remote_port == port &&
  419. rd->remote_vni == vni &&
  420. rd->remote_ifindex == ifindex)
  421. return rd;
  422. }
  423. return NULL;
  424. }
  425. /* Replace destination of unicast mac */
  426. static int vxlan_fdb_replace(struct vxlan_fdb *f,
  427. union vxlan_addr *ip, __be16 port, __u32 vni, __u32 ifindex)
  428. {
  429. struct vxlan_rdst *rd;
  430. rd = vxlan_fdb_find_rdst(f, ip, port, vni, ifindex);
  431. if (rd)
  432. return 0;
  433. rd = list_first_entry_or_null(&f->remotes, struct vxlan_rdst, list);
  434. if (!rd)
  435. return 0;
  436. rd->remote_ip = *ip;
  437. rd->remote_port = port;
  438. rd->remote_vni = vni;
  439. rd->remote_ifindex = ifindex;
  440. return 1;
  441. }
  442. /* Add/update destinations for multicast */
  443. static int vxlan_fdb_append(struct vxlan_fdb *f,
  444. union vxlan_addr *ip, __be16 port, __u32 vni,
  445. __u32 ifindex, struct vxlan_rdst **rdp)
  446. {
  447. struct vxlan_rdst *rd;
  448. rd = vxlan_fdb_find_rdst(f, ip, port, vni, ifindex);
  449. if (rd)
  450. return 0;
  451. rd = kmalloc(sizeof(*rd), GFP_ATOMIC);
  452. if (rd == NULL)
  453. return -ENOBUFS;
  454. rd->remote_ip = *ip;
  455. rd->remote_port = port;
  456. rd->remote_vni = vni;
  457. rd->remote_ifindex = ifindex;
  458. list_add_tail_rcu(&rd->list, &f->remotes);
  459. *rdp = rd;
  460. return 1;
  461. }
  462. static struct sk_buff **vxlan_gro_receive(struct sk_buff **head, struct sk_buff *skb)
  463. {
  464. struct sk_buff *p, **pp = NULL;
  465. struct vxlanhdr *vh, *vh2;
  466. struct ethhdr *eh, *eh2;
  467. unsigned int hlen, off_vx, off_eth;
  468. const struct packet_offload *ptype;
  469. __be16 type;
  470. int flush = 1;
  471. off_vx = skb_gro_offset(skb);
  472. hlen = off_vx + sizeof(*vh);
  473. vh = skb_gro_header_fast(skb, off_vx);
  474. if (skb_gro_header_hard(skb, hlen)) {
  475. vh = skb_gro_header_slow(skb, hlen, off_vx);
  476. if (unlikely(!vh))
  477. goto out;
  478. }
  479. skb_gro_pull(skb, sizeof(struct vxlanhdr)); /* pull vxlan header */
  480. skb_gro_postpull_rcsum(skb, vh, sizeof(struct vxlanhdr));
  481. off_eth = skb_gro_offset(skb);
  482. hlen = off_eth + sizeof(*eh);
  483. eh = skb_gro_header_fast(skb, off_eth);
  484. if (skb_gro_header_hard(skb, hlen)) {
  485. eh = skb_gro_header_slow(skb, hlen, off_eth);
  486. if (unlikely(!eh))
  487. goto out;
  488. }
  489. flush = 0;
  490. for (p = *head; p; p = p->next) {
  491. if (!NAPI_GRO_CB(p)->same_flow)
  492. continue;
  493. vh2 = (struct vxlanhdr *)(p->data + off_vx);
  494. eh2 = (struct ethhdr *)(p->data + off_eth);
  495. if (vh->vx_vni != vh2->vx_vni || compare_ether_header(eh, eh2)) {
  496. NAPI_GRO_CB(p)->same_flow = 0;
  497. continue;
  498. }
  499. }
  500. type = eh->h_proto;
  501. rcu_read_lock();
  502. ptype = gro_find_receive_by_type(type);
  503. if (ptype == NULL) {
  504. flush = 1;
  505. goto out_unlock;
  506. }
  507. skb_gro_pull(skb, sizeof(*eh)); /* pull inner eth header */
  508. skb_gro_postpull_rcsum(skb, eh, sizeof(*eh));
  509. pp = ptype->callbacks.gro_receive(head, skb);
  510. out_unlock:
  511. rcu_read_unlock();
  512. out:
  513. NAPI_GRO_CB(skb)->flush |= flush;
  514. return pp;
  515. }
  516. static int vxlan_gro_complete(struct sk_buff *skb, int nhoff)
  517. {
  518. struct ethhdr *eh;
  519. struct packet_offload *ptype;
  520. __be16 type;
  521. int vxlan_len = sizeof(struct vxlanhdr) + sizeof(struct ethhdr);
  522. int err = -ENOSYS;
  523. eh = (struct ethhdr *)(skb->data + nhoff + sizeof(struct vxlanhdr));
  524. type = eh->h_proto;
  525. rcu_read_lock();
  526. ptype = gro_find_complete_by_type(type);
  527. if (ptype != NULL)
  528. err = ptype->callbacks.gro_complete(skb, nhoff + vxlan_len);
  529. rcu_read_unlock();
  530. return err;
  531. }
  532. /* Notify netdevs that UDP port started listening */
  533. static void vxlan_notify_add_rx_port(struct vxlan_sock *vs)
  534. {
  535. struct net_device *dev;
  536. struct sock *sk = vs->sock->sk;
  537. struct net *net = sock_net(sk);
  538. sa_family_t sa_family = sk->sk_family;
  539. __be16 port = inet_sk(sk)->inet_sport;
  540. int err;
  541. if (sa_family == AF_INET) {
  542. err = udp_add_offload(&vs->udp_offloads);
  543. if (err)
  544. pr_warn("vxlan: udp_add_offload failed with status %d\n", err);
  545. }
  546. rcu_read_lock();
  547. for_each_netdev_rcu(net, dev) {
  548. if (dev->netdev_ops->ndo_add_vxlan_port)
  549. dev->netdev_ops->ndo_add_vxlan_port(dev, sa_family,
  550. port);
  551. }
  552. rcu_read_unlock();
  553. }
  554. /* Notify netdevs that UDP port is no more listening */
  555. static void vxlan_notify_del_rx_port(struct vxlan_sock *vs)
  556. {
  557. struct net_device *dev;
  558. struct sock *sk = vs->sock->sk;
  559. struct net *net = sock_net(sk);
  560. sa_family_t sa_family = sk->sk_family;
  561. __be16 port = inet_sk(sk)->inet_sport;
  562. rcu_read_lock();
  563. for_each_netdev_rcu(net, dev) {
  564. if (dev->netdev_ops->ndo_del_vxlan_port)
  565. dev->netdev_ops->ndo_del_vxlan_port(dev, sa_family,
  566. port);
  567. }
  568. rcu_read_unlock();
  569. if (sa_family == AF_INET)
  570. udp_del_offload(&vs->udp_offloads);
  571. }
  572. /* Add new entry to forwarding table -- assumes lock held */
  573. static int vxlan_fdb_create(struct vxlan_dev *vxlan,
  574. const u8 *mac, union vxlan_addr *ip,
  575. __u16 state, __u16 flags,
  576. __be16 port, __u32 vni, __u32 ifindex,
  577. __u8 ndm_flags)
  578. {
  579. struct vxlan_rdst *rd = NULL;
  580. struct vxlan_fdb *f;
  581. int notify = 0;
  582. f = __vxlan_find_mac(vxlan, mac);
  583. if (f) {
  584. if (flags & NLM_F_EXCL) {
  585. netdev_dbg(vxlan->dev,
  586. "lost race to create %pM\n", mac);
  587. return -EEXIST;
  588. }
  589. if (f->state != state) {
  590. f->state = state;
  591. f->updated = jiffies;
  592. notify = 1;
  593. }
  594. if (f->flags != ndm_flags) {
  595. f->flags = ndm_flags;
  596. f->updated = jiffies;
  597. notify = 1;
  598. }
  599. if ((flags & NLM_F_REPLACE)) {
  600. /* Only change unicasts */
  601. if (!(is_multicast_ether_addr(f->eth_addr) ||
  602. is_zero_ether_addr(f->eth_addr))) {
  603. int rc = vxlan_fdb_replace(f, ip, port, vni,
  604. ifindex);
  605. if (rc < 0)
  606. return rc;
  607. notify |= rc;
  608. } else
  609. return -EOPNOTSUPP;
  610. }
  611. if ((flags & NLM_F_APPEND) &&
  612. (is_multicast_ether_addr(f->eth_addr) ||
  613. is_zero_ether_addr(f->eth_addr))) {
  614. int rc = vxlan_fdb_append(f, ip, port, vni, ifindex,
  615. &rd);
  616. if (rc < 0)
  617. return rc;
  618. notify |= rc;
  619. }
  620. } else {
  621. if (!(flags & NLM_F_CREATE))
  622. return -ENOENT;
  623. if (vxlan->addrmax && vxlan->addrcnt >= vxlan->addrmax)
  624. return -ENOSPC;
  625. /* Disallow replace to add a multicast entry */
  626. if ((flags & NLM_F_REPLACE) &&
  627. (is_multicast_ether_addr(mac) || is_zero_ether_addr(mac)))
  628. return -EOPNOTSUPP;
  629. netdev_dbg(vxlan->dev, "add %pM -> %pIS\n", mac, ip);
  630. f = kmalloc(sizeof(*f), GFP_ATOMIC);
  631. if (!f)
  632. return -ENOMEM;
  633. notify = 1;
  634. f->state = state;
  635. f->flags = ndm_flags;
  636. f->updated = f->used = jiffies;
  637. INIT_LIST_HEAD(&f->remotes);
  638. memcpy(f->eth_addr, mac, ETH_ALEN);
  639. vxlan_fdb_append(f, ip, port, vni, ifindex, &rd);
  640. ++vxlan->addrcnt;
  641. hlist_add_head_rcu(&f->hlist,
  642. vxlan_fdb_head(vxlan, mac));
  643. }
  644. if (notify) {
  645. if (rd == NULL)
  646. rd = first_remote_rtnl(f);
  647. vxlan_fdb_notify(vxlan, f, rd, RTM_NEWNEIGH);
  648. }
  649. return 0;
  650. }
  651. static void vxlan_fdb_free(struct rcu_head *head)
  652. {
  653. struct vxlan_fdb *f = container_of(head, struct vxlan_fdb, rcu);
  654. struct vxlan_rdst *rd, *nd;
  655. list_for_each_entry_safe(rd, nd, &f->remotes, list)
  656. kfree(rd);
  657. kfree(f);
  658. }
  659. static void vxlan_fdb_destroy(struct vxlan_dev *vxlan, struct vxlan_fdb *f)
  660. {
  661. netdev_dbg(vxlan->dev,
  662. "delete %pM\n", f->eth_addr);
  663. --vxlan->addrcnt;
  664. vxlan_fdb_notify(vxlan, f, first_remote_rtnl(f), RTM_DELNEIGH);
  665. hlist_del_rcu(&f->hlist);
  666. call_rcu(&f->rcu, vxlan_fdb_free);
  667. }
  668. static int vxlan_fdb_parse(struct nlattr *tb[], struct vxlan_dev *vxlan,
  669. union vxlan_addr *ip, __be16 *port, u32 *vni, u32 *ifindex)
  670. {
  671. struct net *net = dev_net(vxlan->dev);
  672. int err;
  673. if (tb[NDA_DST]) {
  674. err = vxlan_nla_get_addr(ip, tb[NDA_DST]);
  675. if (err)
  676. return err;
  677. } else {
  678. union vxlan_addr *remote = &vxlan->default_dst.remote_ip;
  679. if (remote->sa.sa_family == AF_INET) {
  680. ip->sin.sin_addr.s_addr = htonl(INADDR_ANY);
  681. ip->sa.sa_family = AF_INET;
  682. #if IS_ENABLED(CONFIG_IPV6)
  683. } else {
  684. ip->sin6.sin6_addr = in6addr_any;
  685. ip->sa.sa_family = AF_INET6;
  686. #endif
  687. }
  688. }
  689. if (tb[NDA_PORT]) {
  690. if (nla_len(tb[NDA_PORT]) != sizeof(__be16))
  691. return -EINVAL;
  692. *port = nla_get_be16(tb[NDA_PORT]);
  693. } else {
  694. *port = vxlan->dst_port;
  695. }
  696. if (tb[NDA_VNI]) {
  697. if (nla_len(tb[NDA_VNI]) != sizeof(u32))
  698. return -EINVAL;
  699. *vni = nla_get_u32(tb[NDA_VNI]);
  700. } else {
  701. *vni = vxlan->default_dst.remote_vni;
  702. }
  703. if (tb[NDA_IFINDEX]) {
  704. struct net_device *tdev;
  705. if (nla_len(tb[NDA_IFINDEX]) != sizeof(u32))
  706. return -EINVAL;
  707. *ifindex = nla_get_u32(tb[NDA_IFINDEX]);
  708. tdev = __dev_get_by_index(net, *ifindex);
  709. if (!tdev)
  710. return -EADDRNOTAVAIL;
  711. } else {
  712. *ifindex = 0;
  713. }
  714. return 0;
  715. }
  716. /* Add static entry (via netlink) */
  717. static int vxlan_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
  718. struct net_device *dev,
  719. const unsigned char *addr, u16 flags)
  720. {
  721. struct vxlan_dev *vxlan = netdev_priv(dev);
  722. /* struct net *net = dev_net(vxlan->dev); */
  723. union vxlan_addr ip;
  724. __be16 port;
  725. u32 vni, ifindex;
  726. int err;
  727. if (!(ndm->ndm_state & (NUD_PERMANENT|NUD_REACHABLE))) {
  728. pr_info("RTM_NEWNEIGH with invalid state %#x\n",
  729. ndm->ndm_state);
  730. return -EINVAL;
  731. }
  732. if (tb[NDA_DST] == NULL)
  733. return -EINVAL;
  734. err = vxlan_fdb_parse(tb, vxlan, &ip, &port, &vni, &ifindex);
  735. if (err)
  736. return err;
  737. if (vxlan->default_dst.remote_ip.sa.sa_family != ip.sa.sa_family)
  738. return -EAFNOSUPPORT;
  739. spin_lock_bh(&vxlan->hash_lock);
  740. err = vxlan_fdb_create(vxlan, addr, &ip, ndm->ndm_state, flags,
  741. port, vni, ifindex, ndm->ndm_flags);
  742. spin_unlock_bh(&vxlan->hash_lock);
  743. return err;
  744. }
  745. /* Delete entry (via netlink) */
  746. static int vxlan_fdb_delete(struct ndmsg *ndm, struct nlattr *tb[],
  747. struct net_device *dev,
  748. const unsigned char *addr)
  749. {
  750. struct vxlan_dev *vxlan = netdev_priv(dev);
  751. struct vxlan_fdb *f;
  752. struct vxlan_rdst *rd = NULL;
  753. union vxlan_addr ip;
  754. __be16 port;
  755. u32 vni, ifindex;
  756. int err;
  757. err = vxlan_fdb_parse(tb, vxlan, &ip, &port, &vni, &ifindex);
  758. if (err)
  759. return err;
  760. err = -ENOENT;
  761. spin_lock_bh(&vxlan->hash_lock);
  762. f = vxlan_find_mac(vxlan, addr);
  763. if (!f)
  764. goto out;
  765. if (!vxlan_addr_any(&ip)) {
  766. rd = vxlan_fdb_find_rdst(f, &ip, port, vni, ifindex);
  767. if (!rd)
  768. goto out;
  769. }
  770. err = 0;
  771. /* remove a destination if it's not the only one on the list,
  772. * otherwise destroy the fdb entry
  773. */
  774. if (rd && !list_is_singular(&f->remotes)) {
  775. list_del_rcu(&rd->list);
  776. vxlan_fdb_notify(vxlan, f, rd, RTM_DELNEIGH);
  777. kfree_rcu(rd, rcu);
  778. goto out;
  779. }
  780. vxlan_fdb_destroy(vxlan, f);
  781. out:
  782. spin_unlock_bh(&vxlan->hash_lock);
  783. return err;
  784. }
  785. /* Dump forwarding table */
  786. static int vxlan_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb,
  787. struct net_device *dev,
  788. struct net_device *filter_dev, int idx)
  789. {
  790. struct vxlan_dev *vxlan = netdev_priv(dev);
  791. unsigned int h;
  792. for (h = 0; h < FDB_HASH_SIZE; ++h) {
  793. struct vxlan_fdb *f;
  794. int err;
  795. hlist_for_each_entry_rcu(f, &vxlan->fdb_head[h], hlist) {
  796. struct vxlan_rdst *rd;
  797. if (idx < cb->args[0])
  798. goto skip;
  799. list_for_each_entry_rcu(rd, &f->remotes, list) {
  800. err = vxlan_fdb_info(skb, vxlan, f,
  801. NETLINK_CB(cb->skb).portid,
  802. cb->nlh->nlmsg_seq,
  803. RTM_NEWNEIGH,
  804. NLM_F_MULTI, rd);
  805. if (err < 0)
  806. goto out;
  807. }
  808. skip:
  809. ++idx;
  810. }
  811. }
  812. out:
  813. return idx;
  814. }
  815. /* Watch incoming packets to learn mapping between Ethernet address
  816. * and Tunnel endpoint.
  817. * Return true if packet is bogus and should be droppped.
  818. */
  819. static bool vxlan_snoop(struct net_device *dev,
  820. union vxlan_addr *src_ip, const u8 *src_mac)
  821. {
  822. struct vxlan_dev *vxlan = netdev_priv(dev);
  823. struct vxlan_fdb *f;
  824. f = vxlan_find_mac(vxlan, src_mac);
  825. if (likely(f)) {
  826. struct vxlan_rdst *rdst = first_remote_rcu(f);
  827. if (likely(vxlan_addr_equal(&rdst->remote_ip, src_ip)))
  828. return false;
  829. /* Don't migrate static entries, drop packets */
  830. if (f->state & NUD_NOARP)
  831. return true;
  832. if (net_ratelimit())
  833. netdev_info(dev,
  834. "%pM migrated from %pIS to %pIS\n",
  835. src_mac, &rdst->remote_ip, &src_ip);
  836. rdst->remote_ip = *src_ip;
  837. f->updated = jiffies;
  838. vxlan_fdb_notify(vxlan, f, rdst, RTM_NEWNEIGH);
  839. } else {
  840. /* learned new entry */
  841. spin_lock(&vxlan->hash_lock);
  842. /* close off race between vxlan_flush and incoming packets */
  843. if (netif_running(dev))
  844. vxlan_fdb_create(vxlan, src_mac, src_ip,
  845. NUD_REACHABLE,
  846. NLM_F_EXCL|NLM_F_CREATE,
  847. vxlan->dst_port,
  848. vxlan->default_dst.remote_vni,
  849. 0, NTF_SELF);
  850. spin_unlock(&vxlan->hash_lock);
  851. }
  852. return false;
  853. }
  854. /* See if multicast group is already in use by other ID */
  855. static bool vxlan_group_used(struct vxlan_net *vn, struct vxlan_dev *dev)
  856. {
  857. struct vxlan_dev *vxlan;
  858. /* The vxlan_sock is only used by dev, leaving group has
  859. * no effect on other vxlan devices.
  860. */
  861. if (atomic_read(&dev->vn_sock->refcnt) == 1)
  862. return false;
  863. list_for_each_entry(vxlan, &vn->vxlan_list, next) {
  864. if (!netif_running(vxlan->dev) || vxlan == dev)
  865. continue;
  866. if (vxlan->vn_sock != dev->vn_sock)
  867. continue;
  868. if (!vxlan_addr_equal(&vxlan->default_dst.remote_ip,
  869. &dev->default_dst.remote_ip))
  870. continue;
  871. if (vxlan->default_dst.remote_ifindex !=
  872. dev->default_dst.remote_ifindex)
  873. continue;
  874. return true;
  875. }
  876. return false;
  877. }
  878. static void vxlan_sock_hold(struct vxlan_sock *vs)
  879. {
  880. atomic_inc(&vs->refcnt);
  881. }
  882. void vxlan_sock_release(struct vxlan_sock *vs)
  883. {
  884. struct sock *sk = vs->sock->sk;
  885. struct net *net = sock_net(sk);
  886. struct vxlan_net *vn = net_generic(net, vxlan_net_id);
  887. if (!atomic_dec_and_test(&vs->refcnt))
  888. return;
  889. spin_lock(&vn->sock_lock);
  890. hlist_del_rcu(&vs->hlist);
  891. rcu_assign_sk_user_data(vs->sock->sk, NULL);
  892. vxlan_notify_del_rx_port(vs);
  893. spin_unlock(&vn->sock_lock);
  894. queue_work(vxlan_wq, &vs->del_work);
  895. }
  896. EXPORT_SYMBOL_GPL(vxlan_sock_release);
  897. /* Callback to update multicast group membership when first VNI on
  898. * multicast asddress is brought up
  899. * Done as workqueue because ip_mc_join_group acquires RTNL.
  900. */
  901. static void vxlan_igmp_join(struct work_struct *work)
  902. {
  903. struct vxlan_dev *vxlan = container_of(work, struct vxlan_dev, igmp_join);
  904. struct vxlan_sock *vs = vxlan->vn_sock;
  905. struct sock *sk = vs->sock->sk;
  906. union vxlan_addr *ip = &vxlan->default_dst.remote_ip;
  907. int ifindex = vxlan->default_dst.remote_ifindex;
  908. lock_sock(sk);
  909. if (ip->sa.sa_family == AF_INET) {
  910. struct ip_mreqn mreq = {
  911. .imr_multiaddr.s_addr = ip->sin.sin_addr.s_addr,
  912. .imr_ifindex = ifindex,
  913. };
  914. ip_mc_join_group(sk, &mreq);
  915. #if IS_ENABLED(CONFIG_IPV6)
  916. } else {
  917. ipv6_stub->ipv6_sock_mc_join(sk, ifindex,
  918. &ip->sin6.sin6_addr);
  919. #endif
  920. }
  921. release_sock(sk);
  922. vxlan_sock_release(vs);
  923. dev_put(vxlan->dev);
  924. }
  925. /* Inverse of vxlan_igmp_join when last VNI is brought down */
  926. static void vxlan_igmp_leave(struct work_struct *work)
  927. {
  928. struct vxlan_dev *vxlan = container_of(work, struct vxlan_dev, igmp_leave);
  929. struct vxlan_sock *vs = vxlan->vn_sock;
  930. struct sock *sk = vs->sock->sk;
  931. union vxlan_addr *ip = &vxlan->default_dst.remote_ip;
  932. int ifindex = vxlan->default_dst.remote_ifindex;
  933. lock_sock(sk);
  934. if (ip->sa.sa_family == AF_INET) {
  935. struct ip_mreqn mreq = {
  936. .imr_multiaddr.s_addr = ip->sin.sin_addr.s_addr,
  937. .imr_ifindex = ifindex,
  938. };
  939. ip_mc_leave_group(sk, &mreq);
  940. #if IS_ENABLED(CONFIG_IPV6)
  941. } else {
  942. ipv6_stub->ipv6_sock_mc_drop(sk, ifindex,
  943. &ip->sin6.sin6_addr);
  944. #endif
  945. }
  946. release_sock(sk);
  947. vxlan_sock_release(vs);
  948. dev_put(vxlan->dev);
  949. }
  950. /* Callback from net/ipv4/udp.c to receive packets */
  951. static int vxlan_udp_encap_recv(struct sock *sk, struct sk_buff *skb)
  952. {
  953. struct vxlan_sock *vs;
  954. struct vxlanhdr *vxh;
  955. /* Need Vxlan and inner Ethernet header to be present */
  956. if (!pskb_may_pull(skb, VXLAN_HLEN))
  957. goto error;
  958. /* Return packets with reserved bits set */
  959. vxh = (struct vxlanhdr *)(udp_hdr(skb) + 1);
  960. if (vxh->vx_flags != htonl(VXLAN_FLAGS) ||
  961. (vxh->vx_vni & htonl(0xff))) {
  962. netdev_dbg(skb->dev, "invalid vxlan flags=%#x vni=%#x\n",
  963. ntohl(vxh->vx_flags), ntohl(vxh->vx_vni));
  964. goto error;
  965. }
  966. if (iptunnel_pull_header(skb, VXLAN_HLEN, htons(ETH_P_TEB)))
  967. goto drop;
  968. vs = rcu_dereference_sk_user_data(sk);
  969. if (!vs)
  970. goto drop;
  971. skb_pop_rcv_encapsulation(skb);
  972. vs->rcv(vs, skb, vxh->vx_vni);
  973. return 0;
  974. drop:
  975. /* Consume bad packet */
  976. kfree_skb(skb);
  977. return 0;
  978. error:
  979. /* Return non vxlan pkt */
  980. return 1;
  981. }
  982. static void vxlan_rcv(struct vxlan_sock *vs,
  983. struct sk_buff *skb, __be32 vx_vni)
  984. {
  985. struct iphdr *oip = NULL;
  986. struct ipv6hdr *oip6 = NULL;
  987. struct vxlan_dev *vxlan;
  988. struct pcpu_sw_netstats *stats;
  989. union vxlan_addr saddr;
  990. __u32 vni;
  991. int err = 0;
  992. union vxlan_addr *remote_ip;
  993. vni = ntohl(vx_vni) >> 8;
  994. /* Is this VNI defined? */
  995. vxlan = vxlan_vs_find_vni(vs, vni);
  996. if (!vxlan)
  997. goto drop;
  998. remote_ip = &vxlan->default_dst.remote_ip;
  999. skb_reset_mac_header(skb);
  1000. skb_scrub_packet(skb, !net_eq(vxlan->net, dev_net(vxlan->dev)));
  1001. skb->protocol = eth_type_trans(skb, vxlan->dev);
  1002. skb_postpull_rcsum(skb, eth_hdr(skb), ETH_HLEN);
  1003. /* Ignore packet loops (and multicast echo) */
  1004. if (ether_addr_equal(eth_hdr(skb)->h_source, vxlan->dev->dev_addr))
  1005. goto drop;
  1006. /* Re-examine inner Ethernet packet */
  1007. if (remote_ip->sa.sa_family == AF_INET) {
  1008. oip = ip_hdr(skb);
  1009. saddr.sin.sin_addr.s_addr = oip->saddr;
  1010. saddr.sa.sa_family = AF_INET;
  1011. #if IS_ENABLED(CONFIG_IPV6)
  1012. } else {
  1013. oip6 = ipv6_hdr(skb);
  1014. saddr.sin6.sin6_addr = oip6->saddr;
  1015. saddr.sa.sa_family = AF_INET6;
  1016. #endif
  1017. }
  1018. if ((vxlan->flags & VXLAN_F_LEARN) &&
  1019. vxlan_snoop(skb->dev, &saddr, eth_hdr(skb)->h_source))
  1020. goto drop;
  1021. skb_reset_network_header(skb);
  1022. if (oip6)
  1023. err = IP6_ECN_decapsulate(oip6, skb);
  1024. if (oip)
  1025. err = IP_ECN_decapsulate(oip, skb);
  1026. if (unlikely(err)) {
  1027. if (log_ecn_error) {
  1028. if (oip6)
  1029. net_info_ratelimited("non-ECT from %pI6\n",
  1030. &oip6->saddr);
  1031. if (oip)
  1032. net_info_ratelimited("non-ECT from %pI4 with TOS=%#x\n",
  1033. &oip->saddr, oip->tos);
  1034. }
  1035. if (err > 1) {
  1036. ++vxlan->dev->stats.rx_frame_errors;
  1037. ++vxlan->dev->stats.rx_errors;
  1038. goto drop;
  1039. }
  1040. }
  1041. stats = this_cpu_ptr(vxlan->dev->tstats);
  1042. u64_stats_update_begin(&stats->syncp);
  1043. stats->rx_packets++;
  1044. stats->rx_bytes += skb->len;
  1045. u64_stats_update_end(&stats->syncp);
  1046. netif_rx(skb);
  1047. return;
  1048. drop:
  1049. /* Consume bad packet */
  1050. kfree_skb(skb);
  1051. }
  1052. static int arp_reduce(struct net_device *dev, struct sk_buff *skb)
  1053. {
  1054. struct vxlan_dev *vxlan = netdev_priv(dev);
  1055. struct arphdr *parp;
  1056. u8 *arpptr, *sha;
  1057. __be32 sip, tip;
  1058. struct neighbour *n;
  1059. if (dev->flags & IFF_NOARP)
  1060. goto out;
  1061. if (!pskb_may_pull(skb, arp_hdr_len(dev))) {
  1062. dev->stats.tx_dropped++;
  1063. goto out;
  1064. }
  1065. parp = arp_hdr(skb);
  1066. if ((parp->ar_hrd != htons(ARPHRD_ETHER) &&
  1067. parp->ar_hrd != htons(ARPHRD_IEEE802)) ||
  1068. parp->ar_pro != htons(ETH_P_IP) ||
  1069. parp->ar_op != htons(ARPOP_REQUEST) ||
  1070. parp->ar_hln != dev->addr_len ||
  1071. parp->ar_pln != 4)
  1072. goto out;
  1073. arpptr = (u8 *)parp + sizeof(struct arphdr);
  1074. sha = arpptr;
  1075. arpptr += dev->addr_len; /* sha */
  1076. memcpy(&sip, arpptr, sizeof(sip));
  1077. arpptr += sizeof(sip);
  1078. arpptr += dev->addr_len; /* tha */
  1079. memcpy(&tip, arpptr, sizeof(tip));
  1080. if (ipv4_is_loopback(tip) ||
  1081. ipv4_is_multicast(tip))
  1082. goto out;
  1083. n = neigh_lookup(&arp_tbl, &tip, dev);
  1084. if (n) {
  1085. struct vxlan_fdb *f;
  1086. struct sk_buff *reply;
  1087. if (!(n->nud_state & NUD_CONNECTED)) {
  1088. neigh_release(n);
  1089. goto out;
  1090. }
  1091. f = vxlan_find_mac(vxlan, n->ha);
  1092. if (f && vxlan_addr_any(&(first_remote_rcu(f)->remote_ip))) {
  1093. /* bridge-local neighbor */
  1094. neigh_release(n);
  1095. goto out;
  1096. }
  1097. reply = arp_create(ARPOP_REPLY, ETH_P_ARP, sip, dev, tip, sha,
  1098. n->ha, sha);
  1099. neigh_release(n);
  1100. if (reply == NULL)
  1101. goto out;
  1102. skb_reset_mac_header(reply);
  1103. __skb_pull(reply, skb_network_offset(reply));
  1104. reply->ip_summed = CHECKSUM_UNNECESSARY;
  1105. reply->pkt_type = PACKET_HOST;
  1106. if (netif_rx_ni(reply) == NET_RX_DROP)
  1107. dev->stats.rx_dropped++;
  1108. } else if (vxlan->flags & VXLAN_F_L3MISS) {
  1109. union vxlan_addr ipa = {
  1110. .sin.sin_addr.s_addr = tip,
  1111. .sin.sin_family = AF_INET,
  1112. };
  1113. vxlan_ip_miss(dev, &ipa);
  1114. }
  1115. out:
  1116. consume_skb(skb);
  1117. return NETDEV_TX_OK;
  1118. }
  1119. #if IS_ENABLED(CONFIG_IPV6)
  1120. static struct sk_buff *vxlan_na_create(struct sk_buff *request,
  1121. struct neighbour *n, bool isrouter)
  1122. {
  1123. struct net_device *dev = request->dev;
  1124. struct sk_buff *reply;
  1125. struct nd_msg *ns, *na;
  1126. struct ipv6hdr *pip6;
  1127. u8 *daddr;
  1128. int na_olen = 8; /* opt hdr + ETH_ALEN for target */
  1129. int ns_olen;
  1130. int i, len;
  1131. if (dev == NULL)
  1132. return NULL;
  1133. len = LL_RESERVED_SPACE(dev) + sizeof(struct ipv6hdr) +
  1134. sizeof(*na) + na_olen + dev->needed_tailroom;
  1135. reply = alloc_skb(len, GFP_ATOMIC);
  1136. if (reply == NULL)
  1137. return NULL;
  1138. reply->protocol = htons(ETH_P_IPV6);
  1139. reply->dev = dev;
  1140. skb_reserve(reply, LL_RESERVED_SPACE(request->dev));
  1141. skb_push(reply, sizeof(struct ethhdr));
  1142. skb_set_mac_header(reply, 0);
  1143. ns = (struct nd_msg *)skb_transport_header(request);
  1144. daddr = eth_hdr(request)->h_source;
  1145. ns_olen = request->len - skb_transport_offset(request) - sizeof(*ns);
  1146. for (i = 0; i < ns_olen-1; i += (ns->opt[i+1]<<3)) {
  1147. if (ns->opt[i] == ND_OPT_SOURCE_LL_ADDR) {
  1148. daddr = ns->opt + i + sizeof(struct nd_opt_hdr);
  1149. break;
  1150. }
  1151. }
  1152. /* Ethernet header */
  1153. ether_addr_copy(eth_hdr(reply)->h_dest, daddr);
  1154. ether_addr_copy(eth_hdr(reply)->h_source, n->ha);
  1155. eth_hdr(reply)->h_proto = htons(ETH_P_IPV6);
  1156. reply->protocol = htons(ETH_P_IPV6);
  1157. skb_pull(reply, sizeof(struct ethhdr));
  1158. skb_set_network_header(reply, 0);
  1159. skb_put(reply, sizeof(struct ipv6hdr));
  1160. /* IPv6 header */
  1161. pip6 = ipv6_hdr(reply);
  1162. memset(pip6, 0, sizeof(struct ipv6hdr));
  1163. pip6->version = 6;
  1164. pip6->priority = ipv6_hdr(request)->priority;
  1165. pip6->nexthdr = IPPROTO_ICMPV6;
  1166. pip6->hop_limit = 255;
  1167. pip6->daddr = ipv6_hdr(request)->saddr;
  1168. pip6->saddr = *(struct in6_addr *)n->primary_key;
  1169. skb_pull(reply, sizeof(struct ipv6hdr));
  1170. skb_set_transport_header(reply, 0);
  1171. na = (struct nd_msg *)skb_put(reply, sizeof(*na) + na_olen);
  1172. /* Neighbor Advertisement */
  1173. memset(na, 0, sizeof(*na)+na_olen);
  1174. na->icmph.icmp6_type = NDISC_NEIGHBOUR_ADVERTISEMENT;
  1175. na->icmph.icmp6_router = isrouter;
  1176. na->icmph.icmp6_override = 1;
  1177. na->icmph.icmp6_solicited = 1;
  1178. na->target = ns->target;
  1179. ether_addr_copy(&na->opt[2], n->ha);
  1180. na->opt[0] = ND_OPT_TARGET_LL_ADDR;
  1181. na->opt[1] = na_olen >> 3;
  1182. na->icmph.icmp6_cksum = csum_ipv6_magic(&pip6->saddr,
  1183. &pip6->daddr, sizeof(*na)+na_olen, IPPROTO_ICMPV6,
  1184. csum_partial(na, sizeof(*na)+na_olen, 0));
  1185. pip6->payload_len = htons(sizeof(*na)+na_olen);
  1186. skb_push(reply, sizeof(struct ipv6hdr));
  1187. reply->ip_summed = CHECKSUM_UNNECESSARY;
  1188. return reply;
  1189. }
  1190. static int neigh_reduce(struct net_device *dev, struct sk_buff *skb)
  1191. {
  1192. struct vxlan_dev *vxlan = netdev_priv(dev);
  1193. struct nd_msg *msg;
  1194. const struct ipv6hdr *iphdr;
  1195. const struct in6_addr *saddr, *daddr;
  1196. struct neighbour *n;
  1197. struct inet6_dev *in6_dev;
  1198. in6_dev = __in6_dev_get(dev);
  1199. if (!in6_dev)
  1200. goto out;
  1201. if (!pskb_may_pull(skb, skb->len))
  1202. goto out;
  1203. iphdr = ipv6_hdr(skb);
  1204. saddr = &iphdr->saddr;
  1205. daddr = &iphdr->daddr;
  1206. msg = (struct nd_msg *)skb_transport_header(skb);
  1207. if (msg->icmph.icmp6_code != 0 ||
  1208. msg->icmph.icmp6_type != NDISC_NEIGHBOUR_SOLICITATION)
  1209. goto out;
  1210. if (ipv6_addr_loopback(daddr) ||
  1211. ipv6_addr_is_multicast(&msg->target))
  1212. goto out;
  1213. n = neigh_lookup(ipv6_stub->nd_tbl, &msg->target, dev);
  1214. if (n) {
  1215. struct vxlan_fdb *f;
  1216. struct sk_buff *reply;
  1217. if (!(n->nud_state & NUD_CONNECTED)) {
  1218. neigh_release(n);
  1219. goto out;
  1220. }
  1221. f = vxlan_find_mac(vxlan, n->ha);
  1222. if (f && vxlan_addr_any(&(first_remote_rcu(f)->remote_ip))) {
  1223. /* bridge-local neighbor */
  1224. neigh_release(n);
  1225. goto out;
  1226. }
  1227. reply = vxlan_na_create(skb, n,
  1228. !!(f ? f->flags & NTF_ROUTER : 0));
  1229. neigh_release(n);
  1230. if (reply == NULL)
  1231. goto out;
  1232. if (netif_rx_ni(reply) == NET_RX_DROP)
  1233. dev->stats.rx_dropped++;
  1234. } else if (vxlan->flags & VXLAN_F_L3MISS) {
  1235. union vxlan_addr ipa = {
  1236. .sin6.sin6_addr = msg->target,
  1237. .sin6.sin6_family = AF_INET6,
  1238. };
  1239. vxlan_ip_miss(dev, &ipa);
  1240. }
  1241. out:
  1242. consume_skb(skb);
  1243. return NETDEV_TX_OK;
  1244. }
  1245. #endif
  1246. static bool route_shortcircuit(struct net_device *dev, struct sk_buff *skb)
  1247. {
  1248. struct vxlan_dev *vxlan = netdev_priv(dev);
  1249. struct neighbour *n;
  1250. if (is_multicast_ether_addr(eth_hdr(skb)->h_dest))
  1251. return false;
  1252. n = NULL;
  1253. switch (ntohs(eth_hdr(skb)->h_proto)) {
  1254. case ETH_P_IP:
  1255. {
  1256. struct iphdr *pip;
  1257. if (!pskb_may_pull(skb, sizeof(struct iphdr)))
  1258. return false;
  1259. pip = ip_hdr(skb);
  1260. n = neigh_lookup(&arp_tbl, &pip->daddr, dev);
  1261. if (!n && (vxlan->flags & VXLAN_F_L3MISS)) {
  1262. union vxlan_addr ipa = {
  1263. .sin.sin_addr.s_addr = pip->daddr,
  1264. .sin.sin_family = AF_INET,
  1265. };
  1266. vxlan_ip_miss(dev, &ipa);
  1267. return false;
  1268. }
  1269. break;
  1270. }
  1271. #if IS_ENABLED(CONFIG_IPV6)
  1272. case ETH_P_IPV6:
  1273. {
  1274. struct ipv6hdr *pip6;
  1275. if (!pskb_may_pull(skb, sizeof(struct ipv6hdr)))
  1276. return false;
  1277. pip6 = ipv6_hdr(skb);
  1278. n = neigh_lookup(ipv6_stub->nd_tbl, &pip6->daddr, dev);
  1279. if (!n && (vxlan->flags & VXLAN_F_L3MISS)) {
  1280. union vxlan_addr ipa = {
  1281. .sin6.sin6_addr = pip6->daddr,
  1282. .sin6.sin6_family = AF_INET6,
  1283. };
  1284. vxlan_ip_miss(dev, &ipa);
  1285. return false;
  1286. }
  1287. break;
  1288. }
  1289. #endif
  1290. default:
  1291. return false;
  1292. }
  1293. if (n) {
  1294. bool diff;
  1295. diff = !ether_addr_equal(eth_hdr(skb)->h_dest, n->ha);
  1296. if (diff) {
  1297. memcpy(eth_hdr(skb)->h_source, eth_hdr(skb)->h_dest,
  1298. dev->addr_len);
  1299. memcpy(eth_hdr(skb)->h_dest, n->ha, dev->addr_len);
  1300. }
  1301. neigh_release(n);
  1302. return diff;
  1303. }
  1304. return false;
  1305. }
  1306. static inline struct sk_buff *vxlan_handle_offloads(struct sk_buff *skb,
  1307. bool udp_csum)
  1308. {
  1309. int type = udp_csum ? SKB_GSO_UDP_TUNNEL_CSUM : SKB_GSO_UDP_TUNNEL;
  1310. return iptunnel_handle_offloads(skb, udp_csum, type);
  1311. }
  1312. #if IS_ENABLED(CONFIG_IPV6)
  1313. static int vxlan6_xmit_skb(struct vxlan_sock *vs,
  1314. struct dst_entry *dst, struct sk_buff *skb,
  1315. struct net_device *dev, struct in6_addr *saddr,
  1316. struct in6_addr *daddr, __u8 prio, __u8 ttl,
  1317. __be16 src_port, __be16 dst_port, __be32 vni,
  1318. bool xnet)
  1319. {
  1320. struct ipv6hdr *ip6h;
  1321. struct vxlanhdr *vxh;
  1322. struct udphdr *uh;
  1323. int min_headroom;
  1324. int err;
  1325. skb = vxlan_handle_offloads(skb, !udp_get_no_check6_tx(vs->sock->sk));
  1326. if (IS_ERR(skb))
  1327. return -EINVAL;
  1328. skb_scrub_packet(skb, xnet);
  1329. min_headroom = LL_RESERVED_SPACE(dst->dev) + dst->header_len
  1330. + VXLAN_HLEN + sizeof(struct ipv6hdr)
  1331. + (vlan_tx_tag_present(skb) ? VLAN_HLEN : 0);
  1332. /* Need space for new headers (invalidates iph ptr) */
  1333. err = skb_cow_head(skb, min_headroom);
  1334. if (unlikely(err))
  1335. return err;
  1336. if (vlan_tx_tag_present(skb)) {
  1337. if (WARN_ON(!__vlan_put_tag(skb,
  1338. skb->vlan_proto,
  1339. vlan_tx_tag_get(skb))))
  1340. return -ENOMEM;
  1341. skb->vlan_tci = 0;
  1342. }
  1343. vxh = (struct vxlanhdr *) __skb_push(skb, sizeof(*vxh));
  1344. vxh->vx_flags = htonl(VXLAN_FLAGS);
  1345. vxh->vx_vni = vni;
  1346. __skb_push(skb, sizeof(*uh));
  1347. skb_reset_transport_header(skb);
  1348. uh = udp_hdr(skb);
  1349. uh->dest = dst_port;
  1350. uh->source = src_port;
  1351. uh->len = htons(skb->len);
  1352. memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
  1353. IPCB(skb)->flags &= ~(IPSKB_XFRM_TUNNEL_SIZE | IPSKB_XFRM_TRANSFORMED |
  1354. IPSKB_REROUTED);
  1355. skb_dst_set(skb, dst);
  1356. udp6_set_csum(udp_get_no_check6_tx(vs->sock->sk), skb,
  1357. saddr, daddr, skb->len);
  1358. __skb_push(skb, sizeof(*ip6h));
  1359. skb_reset_network_header(skb);
  1360. ip6h = ipv6_hdr(skb);
  1361. ip6h->version = 6;
  1362. ip6h->priority = prio;
  1363. ip6h->flow_lbl[0] = 0;
  1364. ip6h->flow_lbl[1] = 0;
  1365. ip6h->flow_lbl[2] = 0;
  1366. ip6h->payload_len = htons(skb->len);
  1367. ip6h->nexthdr = IPPROTO_UDP;
  1368. ip6h->hop_limit = ttl;
  1369. ip6h->daddr = *daddr;
  1370. ip6h->saddr = *saddr;
  1371. ip6tunnel_xmit(skb, dev);
  1372. return 0;
  1373. }
  1374. #endif
  1375. int vxlan_xmit_skb(struct vxlan_sock *vs,
  1376. struct rtable *rt, struct sk_buff *skb,
  1377. __be32 src, __be32 dst, __u8 tos, __u8 ttl, __be16 df,
  1378. __be16 src_port, __be16 dst_port, __be32 vni, bool xnet)
  1379. {
  1380. struct vxlanhdr *vxh;
  1381. struct udphdr *uh;
  1382. int min_headroom;
  1383. int err;
  1384. skb = vxlan_handle_offloads(skb, !vs->sock->sk->sk_no_check_tx);
  1385. if (IS_ERR(skb))
  1386. return -EINVAL;
  1387. min_headroom = LL_RESERVED_SPACE(rt->dst.dev) + rt->dst.header_len
  1388. + VXLAN_HLEN + sizeof(struct iphdr)
  1389. + (vlan_tx_tag_present(skb) ? VLAN_HLEN : 0);
  1390. /* Need space for new headers (invalidates iph ptr) */
  1391. err = skb_cow_head(skb, min_headroom);
  1392. if (unlikely(err))
  1393. return err;
  1394. if (vlan_tx_tag_present(skb)) {
  1395. if (WARN_ON(!__vlan_put_tag(skb,
  1396. skb->vlan_proto,
  1397. vlan_tx_tag_get(skb))))
  1398. return -ENOMEM;
  1399. skb->vlan_tci = 0;
  1400. }
  1401. vxh = (struct vxlanhdr *) __skb_push(skb, sizeof(*vxh));
  1402. vxh->vx_flags = htonl(VXLAN_FLAGS);
  1403. vxh->vx_vni = vni;
  1404. __skb_push(skb, sizeof(*uh));
  1405. skb_reset_transport_header(skb);
  1406. uh = udp_hdr(skb);
  1407. uh->dest = dst_port;
  1408. uh->source = src_port;
  1409. uh->len = htons(skb->len);
  1410. udp_set_csum(vs->sock->sk->sk_no_check_tx, skb,
  1411. src, dst, skb->len);
  1412. return iptunnel_xmit(vs->sock->sk, rt, skb, src, dst, IPPROTO_UDP,
  1413. tos, ttl, df, xnet);
  1414. }
  1415. EXPORT_SYMBOL_GPL(vxlan_xmit_skb);
  1416. /* Bypass encapsulation if the destination is local */
  1417. static void vxlan_encap_bypass(struct sk_buff *skb, struct vxlan_dev *src_vxlan,
  1418. struct vxlan_dev *dst_vxlan)
  1419. {
  1420. struct pcpu_sw_netstats *tx_stats, *rx_stats;
  1421. union vxlan_addr loopback;
  1422. union vxlan_addr *remote_ip = &dst_vxlan->default_dst.remote_ip;
  1423. tx_stats = this_cpu_ptr(src_vxlan->dev->tstats);
  1424. rx_stats = this_cpu_ptr(dst_vxlan->dev->tstats);
  1425. skb->pkt_type = PACKET_HOST;
  1426. skb->encapsulation = 0;
  1427. skb->dev = dst_vxlan->dev;
  1428. __skb_pull(skb, skb_network_offset(skb));
  1429. if (remote_ip->sa.sa_family == AF_INET) {
  1430. loopback.sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
  1431. loopback.sa.sa_family = AF_INET;
  1432. #if IS_ENABLED(CONFIG_IPV6)
  1433. } else {
  1434. loopback.sin6.sin6_addr = in6addr_loopback;
  1435. loopback.sa.sa_family = AF_INET6;
  1436. #endif
  1437. }
  1438. if (dst_vxlan->flags & VXLAN_F_LEARN)
  1439. vxlan_snoop(skb->dev, &loopback, eth_hdr(skb)->h_source);
  1440. u64_stats_update_begin(&tx_stats->syncp);
  1441. tx_stats->tx_packets++;
  1442. tx_stats->tx_bytes += skb->len;
  1443. u64_stats_update_end(&tx_stats->syncp);
  1444. if (netif_rx(skb) == NET_RX_SUCCESS) {
  1445. u64_stats_update_begin(&rx_stats->syncp);
  1446. rx_stats->rx_packets++;
  1447. rx_stats->rx_bytes += skb->len;
  1448. u64_stats_update_end(&rx_stats->syncp);
  1449. } else {
  1450. skb->dev->stats.rx_dropped++;
  1451. }
  1452. }
  1453. static void vxlan_xmit_one(struct sk_buff *skb, struct net_device *dev,
  1454. struct vxlan_rdst *rdst, bool did_rsc)
  1455. {
  1456. struct vxlan_dev *vxlan = netdev_priv(dev);
  1457. struct rtable *rt = NULL;
  1458. const struct iphdr *old_iph;
  1459. struct flowi4 fl4;
  1460. union vxlan_addr *dst;
  1461. __be16 src_port = 0, dst_port;
  1462. u32 vni;
  1463. __be16 df = 0;
  1464. __u8 tos, ttl;
  1465. int err;
  1466. dst_port = rdst->remote_port ? rdst->remote_port : vxlan->dst_port;
  1467. vni = rdst->remote_vni;
  1468. dst = &rdst->remote_ip;
  1469. if (vxlan_addr_any(dst)) {
  1470. if (did_rsc) {
  1471. /* short-circuited back to local bridge */
  1472. vxlan_encap_bypass(skb, vxlan, vxlan);
  1473. return;
  1474. }
  1475. goto drop;
  1476. }
  1477. old_iph = ip_hdr(skb);
  1478. ttl = vxlan->ttl;
  1479. if (!ttl && vxlan_addr_multicast(dst))
  1480. ttl = 1;
  1481. tos = vxlan->tos;
  1482. if (tos == 1)
  1483. tos = ip_tunnel_get_dsfield(old_iph, skb);
  1484. src_port = udp_flow_src_port(dev_net(dev), skb, vxlan->port_min,
  1485. vxlan->port_max, true);
  1486. if (dst->sa.sa_family == AF_INET) {
  1487. memset(&fl4, 0, sizeof(fl4));
  1488. fl4.flowi4_oif = rdst->remote_ifindex;
  1489. fl4.flowi4_tos = RT_TOS(tos);
  1490. fl4.daddr = dst->sin.sin_addr.s_addr;
  1491. fl4.saddr = vxlan->saddr.sin.sin_addr.s_addr;
  1492. rt = ip_route_output_key(vxlan->net, &fl4);
  1493. if (IS_ERR(rt)) {
  1494. netdev_dbg(dev, "no route to %pI4\n",
  1495. &dst->sin.sin_addr.s_addr);
  1496. dev->stats.tx_carrier_errors++;
  1497. goto tx_error;
  1498. }
  1499. if (rt->dst.dev == dev) {
  1500. netdev_dbg(dev, "circular route to %pI4\n",
  1501. &dst->sin.sin_addr.s_addr);
  1502. dev->stats.collisions++;
  1503. goto rt_tx_error;
  1504. }
  1505. /* Bypass encapsulation if the destination is local */
  1506. if (rt->rt_flags & RTCF_LOCAL &&
  1507. !(rt->rt_flags & (RTCF_BROADCAST | RTCF_MULTICAST))) {
  1508. struct vxlan_dev *dst_vxlan;
  1509. ip_rt_put(rt);
  1510. dst_vxlan = vxlan_find_vni(vxlan->net, vni, dst_port);
  1511. if (!dst_vxlan)
  1512. goto tx_error;
  1513. vxlan_encap_bypass(skb, vxlan, dst_vxlan);
  1514. return;
  1515. }
  1516. tos = ip_tunnel_ecn_encap(tos, old_iph, skb);
  1517. ttl = ttl ? : ip4_dst_hoplimit(&rt->dst);
  1518. err = vxlan_xmit_skb(vxlan->vn_sock, rt, skb,
  1519. fl4.saddr, dst->sin.sin_addr.s_addr,
  1520. tos, ttl, df, src_port, dst_port,
  1521. htonl(vni << 8),
  1522. !net_eq(vxlan->net, dev_net(vxlan->dev)));
  1523. if (err < 0)
  1524. goto rt_tx_error;
  1525. iptunnel_xmit_stats(err, &dev->stats, dev->tstats);
  1526. #if IS_ENABLED(CONFIG_IPV6)
  1527. } else {
  1528. struct sock *sk = vxlan->vn_sock->sock->sk;
  1529. struct dst_entry *ndst;
  1530. struct flowi6 fl6;
  1531. u32 flags;
  1532. memset(&fl6, 0, sizeof(fl6));
  1533. fl6.flowi6_oif = rdst->remote_ifindex;
  1534. fl6.daddr = dst->sin6.sin6_addr;
  1535. fl6.saddr = vxlan->saddr.sin6.sin6_addr;
  1536. fl6.flowi6_proto = IPPROTO_UDP;
  1537. if (ipv6_stub->ipv6_dst_lookup(sk, &ndst, &fl6)) {
  1538. netdev_dbg(dev, "no route to %pI6\n",
  1539. &dst->sin6.sin6_addr);
  1540. dev->stats.tx_carrier_errors++;
  1541. goto tx_error;
  1542. }
  1543. if (ndst->dev == dev) {
  1544. netdev_dbg(dev, "circular route to %pI6\n",
  1545. &dst->sin6.sin6_addr);
  1546. dst_release(ndst);
  1547. dev->stats.collisions++;
  1548. goto tx_error;
  1549. }
  1550. /* Bypass encapsulation if the destination is local */
  1551. flags = ((struct rt6_info *)ndst)->rt6i_flags;
  1552. if (flags & RTF_LOCAL &&
  1553. !(flags & (RTCF_BROADCAST | RTCF_MULTICAST))) {
  1554. struct vxlan_dev *dst_vxlan;
  1555. dst_release(ndst);
  1556. dst_vxlan = vxlan_find_vni(vxlan->net, vni, dst_port);
  1557. if (!dst_vxlan)
  1558. goto tx_error;
  1559. vxlan_encap_bypass(skb, vxlan, dst_vxlan);
  1560. return;
  1561. }
  1562. ttl = ttl ? : ip6_dst_hoplimit(ndst);
  1563. err = vxlan6_xmit_skb(vxlan->vn_sock, ndst, skb,
  1564. dev, &fl6.saddr, &fl6.daddr, 0, ttl,
  1565. src_port, dst_port, htonl(vni << 8),
  1566. !net_eq(vxlan->net, dev_net(vxlan->dev)));
  1567. #endif
  1568. }
  1569. return;
  1570. drop:
  1571. dev->stats.tx_dropped++;
  1572. goto tx_free;
  1573. rt_tx_error:
  1574. ip_rt_put(rt);
  1575. tx_error:
  1576. dev->stats.tx_errors++;
  1577. tx_free:
  1578. dev_kfree_skb(skb);
  1579. }
  1580. /* Transmit local packets over Vxlan
  1581. *
  1582. * Outer IP header inherits ECN and DF from inner header.
  1583. * Outer UDP destination is the VXLAN assigned port.
  1584. * source port is based on hash of flow
  1585. */
  1586. static netdev_tx_t vxlan_xmit(struct sk_buff *skb, struct net_device *dev)
  1587. {
  1588. struct vxlan_dev *vxlan = netdev_priv(dev);
  1589. struct ethhdr *eth;
  1590. bool did_rsc = false;
  1591. struct vxlan_rdst *rdst, *fdst = NULL;
  1592. struct vxlan_fdb *f;
  1593. skb_reset_mac_header(skb);
  1594. eth = eth_hdr(skb);
  1595. if ((vxlan->flags & VXLAN_F_PROXY)) {
  1596. if (ntohs(eth->h_proto) == ETH_P_ARP)
  1597. return arp_reduce(dev, skb);
  1598. #if IS_ENABLED(CONFIG_IPV6)
  1599. else if (ntohs(eth->h_proto) == ETH_P_IPV6 &&
  1600. skb->len >= sizeof(struct ipv6hdr) + sizeof(struct nd_msg) &&
  1601. ipv6_hdr(skb)->nexthdr == IPPROTO_ICMPV6) {
  1602. struct nd_msg *msg;
  1603. msg = (struct nd_msg *)skb_transport_header(skb);
  1604. if (msg->icmph.icmp6_code == 0 &&
  1605. msg->icmph.icmp6_type == NDISC_NEIGHBOUR_SOLICITATION)
  1606. return neigh_reduce(dev, skb);
  1607. }
  1608. #endif
  1609. }
  1610. f = vxlan_find_mac(vxlan, eth->h_dest);
  1611. did_rsc = false;
  1612. if (f && (f->flags & NTF_ROUTER) && (vxlan->flags & VXLAN_F_RSC) &&
  1613. (ntohs(eth->h_proto) == ETH_P_IP ||
  1614. ntohs(eth->h_proto) == ETH_P_IPV6)) {
  1615. did_rsc = route_shortcircuit(dev, skb);
  1616. if (did_rsc)
  1617. f = vxlan_find_mac(vxlan, eth->h_dest);
  1618. }
  1619. if (f == NULL) {
  1620. f = vxlan_find_mac(vxlan, all_zeros_mac);
  1621. if (f == NULL) {
  1622. if ((vxlan->flags & VXLAN_F_L2MISS) &&
  1623. !is_multicast_ether_addr(eth->h_dest))
  1624. vxlan_fdb_miss(vxlan, eth->h_dest);
  1625. dev->stats.tx_dropped++;
  1626. kfree_skb(skb);
  1627. return NETDEV_TX_OK;
  1628. }
  1629. }
  1630. list_for_each_entry_rcu(rdst, &f->remotes, list) {
  1631. struct sk_buff *skb1;
  1632. if (!fdst) {
  1633. fdst = rdst;
  1634. continue;
  1635. }
  1636. skb1 = skb_clone(skb, GFP_ATOMIC);
  1637. if (skb1)
  1638. vxlan_xmit_one(skb1, dev, rdst, did_rsc);
  1639. }
  1640. if (fdst)
  1641. vxlan_xmit_one(skb, dev, fdst, did_rsc);
  1642. else
  1643. kfree_skb(skb);
  1644. return NETDEV_TX_OK;
  1645. }
  1646. /* Walk the forwarding table and purge stale entries */
  1647. static void vxlan_cleanup(unsigned long arg)
  1648. {
  1649. struct vxlan_dev *vxlan = (struct vxlan_dev *) arg;
  1650. unsigned long next_timer = jiffies + FDB_AGE_INTERVAL;
  1651. unsigned int h;
  1652. if (!netif_running(vxlan->dev))
  1653. return;
  1654. spin_lock_bh(&vxlan->hash_lock);
  1655. for (h = 0; h < FDB_HASH_SIZE; ++h) {
  1656. struct hlist_node *p, *n;
  1657. hlist_for_each_safe(p, n, &vxlan->fdb_head[h]) {
  1658. struct vxlan_fdb *f
  1659. = container_of(p, struct vxlan_fdb, hlist);
  1660. unsigned long timeout;
  1661. if (f->state & NUD_PERMANENT)
  1662. continue;
  1663. timeout = f->used + vxlan->age_interval * HZ;
  1664. if (time_before_eq(timeout, jiffies)) {
  1665. netdev_dbg(vxlan->dev,
  1666. "garbage collect %pM\n",
  1667. f->eth_addr);
  1668. f->state = NUD_STALE;
  1669. vxlan_fdb_destroy(vxlan, f);
  1670. } else if (time_before(timeout, next_timer))
  1671. next_timer = timeout;
  1672. }
  1673. }
  1674. spin_unlock_bh(&vxlan->hash_lock);
  1675. mod_timer(&vxlan->age_timer, next_timer);
  1676. }
  1677. static void vxlan_vs_add_dev(struct vxlan_sock *vs, struct vxlan_dev *vxlan)
  1678. {
  1679. __u32 vni = vxlan->default_dst.remote_vni;
  1680. vxlan->vn_sock = vs;
  1681. hlist_add_head_rcu(&vxlan->hlist, vni_head(vs, vni));
  1682. }
  1683. /* Setup stats when device is created */
  1684. static int vxlan_init(struct net_device *dev)
  1685. {
  1686. struct vxlan_dev *vxlan = netdev_priv(dev);
  1687. struct vxlan_net *vn = net_generic(vxlan->net, vxlan_net_id);
  1688. struct vxlan_sock *vs;
  1689. dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
  1690. if (!dev->tstats)
  1691. return -ENOMEM;
  1692. spin_lock(&vn->sock_lock);
  1693. vs = vxlan_find_sock(vxlan->net, vxlan->dst_port);
  1694. if (vs) {
  1695. /* If we have a socket with same port already, reuse it */
  1696. atomic_inc(&vs->refcnt);
  1697. vxlan_vs_add_dev(vs, vxlan);
  1698. } else {
  1699. /* otherwise make new socket outside of RTNL */
  1700. dev_hold(dev);
  1701. queue_work(vxlan_wq, &vxlan->sock_work);
  1702. }
  1703. spin_unlock(&vn->sock_lock);
  1704. return 0;
  1705. }
  1706. static void vxlan_fdb_delete_default(struct vxlan_dev *vxlan)
  1707. {
  1708. struct vxlan_fdb *f;
  1709. spin_lock_bh(&vxlan->hash_lock);
  1710. f = __vxlan_find_mac(vxlan, all_zeros_mac);
  1711. if (f)
  1712. vxlan_fdb_destroy(vxlan, f);
  1713. spin_unlock_bh(&vxlan->hash_lock);
  1714. }
  1715. static void vxlan_uninit(struct net_device *dev)
  1716. {
  1717. struct vxlan_dev *vxlan = netdev_priv(dev);
  1718. struct vxlan_sock *vs = vxlan->vn_sock;
  1719. vxlan_fdb_delete_default(vxlan);
  1720. if (vs)
  1721. vxlan_sock_release(vs);
  1722. free_percpu(dev->tstats);
  1723. }
  1724. /* Start ageing timer and join group when device is brought up */
  1725. static int vxlan_open(struct net_device *dev)
  1726. {
  1727. struct vxlan_dev *vxlan = netdev_priv(dev);
  1728. struct vxlan_sock *vs = vxlan->vn_sock;
  1729. /* socket hasn't been created */
  1730. if (!vs)
  1731. return -ENOTCONN;
  1732. if (vxlan_addr_multicast(&vxlan->default_dst.remote_ip)) {
  1733. vxlan_sock_hold(vs);
  1734. dev_hold(dev);
  1735. queue_work(vxlan_wq, &vxlan->igmp_join);
  1736. }
  1737. if (vxlan->age_interval)
  1738. mod_timer(&vxlan->age_timer, jiffies + FDB_AGE_INTERVAL);
  1739. return 0;
  1740. }
  1741. /* Purge the forwarding table */
  1742. static void vxlan_flush(struct vxlan_dev *vxlan)
  1743. {
  1744. unsigned int h;
  1745. spin_lock_bh(&vxlan->hash_lock);
  1746. for (h = 0; h < FDB_HASH_SIZE; ++h) {
  1747. struct hlist_node *p, *n;
  1748. hlist_for_each_safe(p, n, &vxlan->fdb_head[h]) {
  1749. struct vxlan_fdb *f
  1750. = container_of(p, struct vxlan_fdb, hlist);
  1751. /* the all_zeros_mac entry is deleted at vxlan_uninit */
  1752. if (!is_zero_ether_addr(f->eth_addr))
  1753. vxlan_fdb_destroy(vxlan, f);
  1754. }
  1755. }
  1756. spin_unlock_bh(&vxlan->hash_lock);
  1757. }
  1758. /* Cleanup timer and forwarding table on shutdown */
  1759. static int vxlan_stop(struct net_device *dev)
  1760. {
  1761. struct vxlan_dev *vxlan = netdev_priv(dev);
  1762. struct vxlan_net *vn = net_generic(vxlan->net, vxlan_net_id);
  1763. struct vxlan_sock *vs = vxlan->vn_sock;
  1764. if (vs && vxlan_addr_multicast(&vxlan->default_dst.remote_ip) &&
  1765. !vxlan_group_used(vn, vxlan)) {
  1766. vxlan_sock_hold(vs);
  1767. dev_hold(dev);
  1768. queue_work(vxlan_wq, &vxlan->igmp_leave);
  1769. }
  1770. del_timer_sync(&vxlan->age_timer);
  1771. vxlan_flush(vxlan);
  1772. return 0;
  1773. }
  1774. /* Stub, nothing needs to be done. */
  1775. static void vxlan_set_multicast_list(struct net_device *dev)
  1776. {
  1777. }
  1778. static int vxlan_change_mtu(struct net_device *dev, int new_mtu)
  1779. {
  1780. struct vxlan_dev *vxlan = netdev_priv(dev);
  1781. struct vxlan_rdst *dst = &vxlan->default_dst;
  1782. struct net_device *lowerdev;
  1783. int max_mtu;
  1784. lowerdev = __dev_get_by_index(vxlan->net, dst->remote_ifindex);
  1785. if (lowerdev == NULL)
  1786. return eth_change_mtu(dev, new_mtu);
  1787. if (dst->remote_ip.sa.sa_family == AF_INET6)
  1788. max_mtu = lowerdev->mtu - VXLAN6_HEADROOM;
  1789. else
  1790. max_mtu = lowerdev->mtu - VXLAN_HEADROOM;
  1791. if (new_mtu < 68 || new_mtu > max_mtu)
  1792. return -EINVAL;
  1793. dev->mtu = new_mtu;
  1794. return 0;
  1795. }
  1796. static const struct net_device_ops vxlan_netdev_ops = {
  1797. .ndo_init = vxlan_init,
  1798. .ndo_uninit = vxlan_uninit,
  1799. .ndo_open = vxlan_open,
  1800. .ndo_stop = vxlan_stop,
  1801. .ndo_start_xmit = vxlan_xmit,
  1802. .ndo_get_stats64 = ip_tunnel_get_stats64,
  1803. .ndo_set_rx_mode = vxlan_set_multicast_list,
  1804. .ndo_change_mtu = vxlan_change_mtu,
  1805. .ndo_validate_addr = eth_validate_addr,
  1806. .ndo_set_mac_address = eth_mac_addr,
  1807. .ndo_fdb_add = vxlan_fdb_add,
  1808. .ndo_fdb_del = vxlan_fdb_delete,
  1809. .ndo_fdb_dump = vxlan_fdb_dump,
  1810. };
  1811. /* Info for udev, that this is a virtual tunnel endpoint */
  1812. static struct device_type vxlan_type = {
  1813. .name = "vxlan",
  1814. };
  1815. /* Calls the ndo_add_vxlan_port of the caller in order to
  1816. * supply the listening VXLAN udp ports. Callers are expected
  1817. * to implement the ndo_add_vxlan_port.
  1818. */
  1819. void vxlan_get_rx_port(struct net_device *dev)
  1820. {
  1821. struct vxlan_sock *vs;
  1822. struct net *net = dev_net(dev);
  1823. struct vxlan_net *vn = net_generic(net, vxlan_net_id);
  1824. sa_family_t sa_family;
  1825. __be16 port;
  1826. unsigned int i;
  1827. spin_lock(&vn->sock_lock);
  1828. for (i = 0; i < PORT_HASH_SIZE; ++i) {
  1829. hlist_for_each_entry_rcu(vs, &vn->sock_list[i], hlist) {
  1830. port = inet_sk(vs->sock->sk)->inet_sport;
  1831. sa_family = vs->sock->sk->sk_family;
  1832. dev->netdev_ops->ndo_add_vxlan_port(dev, sa_family,
  1833. port);
  1834. }
  1835. }
  1836. spin_unlock(&vn->sock_lock);
  1837. }
  1838. EXPORT_SYMBOL_GPL(vxlan_get_rx_port);
  1839. /* Initialize the device structure. */
  1840. static void vxlan_setup(struct net_device *dev)
  1841. {
  1842. struct vxlan_dev *vxlan = netdev_priv(dev);
  1843. unsigned int h;
  1844. eth_hw_addr_random(dev);
  1845. ether_setup(dev);
  1846. if (vxlan->default_dst.remote_ip.sa.sa_family == AF_INET6)
  1847. dev->needed_headroom = ETH_HLEN + VXLAN6_HEADROOM;
  1848. else
  1849. dev->needed_headroom = ETH_HLEN + VXLAN_HEADROOM;
  1850. dev->netdev_ops = &vxlan_netdev_ops;
  1851. dev->destructor = free_netdev;
  1852. SET_NETDEV_DEVTYPE(dev, &vxlan_type);
  1853. dev->tx_queue_len = 0;
  1854. dev->features |= NETIF_F_LLTX;
  1855. dev->features |= NETIF_F_SG | NETIF_F_HW_CSUM;
  1856. dev->features |= NETIF_F_RXCSUM;
  1857. dev->features |= NETIF_F_GSO_SOFTWARE;
  1858. dev->vlan_features = dev->features;
  1859. dev->features |= NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_STAG_TX;
  1860. dev->hw_features |= NETIF_F_SG | NETIF_F_HW_CSUM | NETIF_F_RXCSUM;
  1861. dev->hw_features |= NETIF_F_GSO_SOFTWARE;
  1862. dev->hw_features |= NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_STAG_TX;
  1863. dev->priv_flags &= ~IFF_XMIT_DST_RELEASE;
  1864. dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
  1865. INIT_LIST_HEAD(&vxlan->next);
  1866. spin_lock_init(&vxlan->hash_lock);
  1867. INIT_WORK(&vxlan->igmp_join, vxlan_igmp_join);
  1868. INIT_WORK(&vxlan->igmp_leave, vxlan_igmp_leave);
  1869. INIT_WORK(&vxlan->sock_work, vxlan_sock_work);
  1870. init_timer_deferrable(&vxlan->age_timer);
  1871. vxlan->age_timer.function = vxlan_cleanup;
  1872. vxlan->age_timer.data = (unsigned long) vxlan;
  1873. vxlan->dst_port = htons(vxlan_port);
  1874. vxlan->dev = dev;
  1875. for (h = 0; h < FDB_HASH_SIZE; ++h)
  1876. INIT_HLIST_HEAD(&vxlan->fdb_head[h]);
  1877. }
  1878. static const struct nla_policy vxlan_policy[IFLA_VXLAN_MAX + 1] = {
  1879. [IFLA_VXLAN_ID] = { .type = NLA_U32 },
  1880. [IFLA_VXLAN_GROUP] = { .len = FIELD_SIZEOF(struct iphdr, daddr) },
  1881. [IFLA_VXLAN_GROUP6] = { .len = sizeof(struct in6_addr) },
  1882. [IFLA_VXLAN_LINK] = { .type = NLA_U32 },
  1883. [IFLA_VXLAN_LOCAL] = { .len = FIELD_SIZEOF(struct iphdr, saddr) },
  1884. [IFLA_VXLAN_LOCAL6] = { .len = sizeof(struct in6_addr) },
  1885. [IFLA_VXLAN_TOS] = { .type = NLA_U8 },
  1886. [IFLA_VXLAN_TTL] = { .type = NLA_U8 },
  1887. [IFLA_VXLAN_LEARNING] = { .type = NLA_U8 },
  1888. [IFLA_VXLAN_AGEING] = { .type = NLA_U32 },
  1889. [IFLA_VXLAN_LIMIT] = { .type = NLA_U32 },
  1890. [IFLA_VXLAN_PORT_RANGE] = { .len = sizeof(struct ifla_vxlan_port_range) },
  1891. [IFLA_VXLAN_PROXY] = { .type = NLA_U8 },
  1892. [IFLA_VXLAN_RSC] = { .type = NLA_U8 },
  1893. [IFLA_VXLAN_L2MISS] = { .type = NLA_U8 },
  1894. [IFLA_VXLAN_L3MISS] = { .type = NLA_U8 },
  1895. [IFLA_VXLAN_PORT] = { .type = NLA_U16 },
  1896. };
  1897. static int vxlan_validate(struct nlattr *tb[], struct nlattr *data[])
  1898. {
  1899. if (tb[IFLA_ADDRESS]) {
  1900. if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN) {
  1901. pr_debug("invalid link address (not ethernet)\n");
  1902. return -EINVAL;
  1903. }
  1904. if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS]))) {
  1905. pr_debug("invalid all zero ethernet address\n");
  1906. return -EADDRNOTAVAIL;
  1907. }
  1908. }
  1909. if (!data)
  1910. return -EINVAL;
  1911. if (data[IFLA_VXLAN_ID]) {
  1912. __u32 id = nla_get_u32(data[IFLA_VXLAN_ID]);
  1913. if (id >= VXLAN_VID_MASK)
  1914. return -ERANGE;
  1915. }
  1916. if (data[IFLA_VXLAN_PORT_RANGE]) {
  1917. const struct ifla_vxlan_port_range *p
  1918. = nla_data(data[IFLA_VXLAN_PORT_RANGE]);
  1919. if (ntohs(p->high) < ntohs(p->low)) {
  1920. pr_debug("port range %u .. %u not valid\n",
  1921. ntohs(p->low), ntohs(p->high));
  1922. return -EINVAL;
  1923. }
  1924. }
  1925. return 0;
  1926. }
  1927. static void vxlan_get_drvinfo(struct net_device *netdev,
  1928. struct ethtool_drvinfo *drvinfo)
  1929. {
  1930. strlcpy(drvinfo->version, VXLAN_VERSION, sizeof(drvinfo->version));
  1931. strlcpy(drvinfo->driver, "vxlan", sizeof(drvinfo->driver));
  1932. }
  1933. static const struct ethtool_ops vxlan_ethtool_ops = {
  1934. .get_drvinfo = vxlan_get_drvinfo,
  1935. .get_link = ethtool_op_get_link,
  1936. };
  1937. static void vxlan_del_work(struct work_struct *work)
  1938. {
  1939. struct vxlan_sock *vs = container_of(work, struct vxlan_sock, del_work);
  1940. sk_release_kernel(vs->sock->sk);
  1941. kfree_rcu(vs, rcu);
  1942. }
  1943. static struct socket *vxlan_create_sock(struct net *net, bool ipv6,
  1944. __be16 port, u32 flags)
  1945. {
  1946. struct socket *sock;
  1947. struct udp_port_cfg udp_conf;
  1948. int err;
  1949. memset(&udp_conf, 0, sizeof(udp_conf));
  1950. if (ipv6) {
  1951. udp_conf.family = AF_INET6;
  1952. udp_conf.use_udp6_tx_checksums =
  1953. !!(flags & VXLAN_F_UDP_ZERO_CSUM6_TX);
  1954. udp_conf.use_udp6_rx_checksums =
  1955. !!(flags & VXLAN_F_UDP_ZERO_CSUM6_RX);
  1956. } else {
  1957. udp_conf.family = AF_INET;
  1958. udp_conf.local_ip.s_addr = INADDR_ANY;
  1959. udp_conf.use_udp_checksums =
  1960. !!(flags & VXLAN_F_UDP_CSUM);
  1961. }
  1962. udp_conf.local_udp_port = port;
  1963. /* Open UDP socket */
  1964. err = udp_sock_create(net, &udp_conf, &sock);
  1965. if (err < 0)
  1966. return ERR_PTR(err);
  1967. /* Disable multicast loopback */
  1968. inet_sk(sock->sk)->mc_loop = 0;
  1969. return sock;
  1970. }
  1971. /* Create new listen socket if needed */
  1972. static struct vxlan_sock *vxlan_socket_create(struct net *net, __be16 port,
  1973. vxlan_rcv_t *rcv, void *data,
  1974. u32 flags)
  1975. {
  1976. struct vxlan_net *vn = net_generic(net, vxlan_net_id);
  1977. struct vxlan_sock *vs;
  1978. struct socket *sock;
  1979. struct sock *sk;
  1980. unsigned int h;
  1981. bool ipv6 = !!(flags & VXLAN_F_IPV6);
  1982. vs = kzalloc(sizeof(*vs), GFP_KERNEL);
  1983. if (!vs)
  1984. return ERR_PTR(-ENOMEM);
  1985. for (h = 0; h < VNI_HASH_SIZE; ++h)
  1986. INIT_HLIST_HEAD(&vs->vni_list[h]);
  1987. INIT_WORK(&vs->del_work, vxlan_del_work);
  1988. sock = vxlan_create_sock(net, ipv6, port, flags);
  1989. if (IS_ERR(sock)) {
  1990. kfree(vs);
  1991. return ERR_CAST(sock);
  1992. }
  1993. vs->sock = sock;
  1994. sk = sock->sk;
  1995. atomic_set(&vs->refcnt, 1);
  1996. vs->rcv = rcv;
  1997. vs->data = data;
  1998. rcu_assign_sk_user_data(vs->sock->sk, vs);
  1999. /* Initialize the vxlan udp offloads structure */
  2000. vs->udp_offloads.port = port;
  2001. vs->udp_offloads.callbacks.gro_receive = vxlan_gro_receive;
  2002. vs->udp_offloads.callbacks.gro_complete = vxlan_gro_complete;
  2003. spin_lock(&vn->sock_lock);
  2004. hlist_add_head_rcu(&vs->hlist, vs_head(net, port));
  2005. vxlan_notify_add_rx_port(vs);
  2006. spin_unlock(&vn->sock_lock);
  2007. /* Mark socket as an encapsulation socket. */
  2008. udp_sk(sk)->encap_type = 1;
  2009. udp_sk(sk)->encap_rcv = vxlan_udp_encap_recv;
  2010. #if IS_ENABLED(CONFIG_IPV6)
  2011. if (ipv6)
  2012. ipv6_stub->udpv6_encap_enable();
  2013. else
  2014. #endif
  2015. udp_encap_enable();
  2016. return vs;
  2017. }
  2018. struct vxlan_sock *vxlan_sock_add(struct net *net, __be16 port,
  2019. vxlan_rcv_t *rcv, void *data,
  2020. bool no_share, u32 flags)
  2021. {
  2022. struct vxlan_net *vn = net_generic(net, vxlan_net_id);
  2023. struct vxlan_sock *vs;
  2024. vs = vxlan_socket_create(net, port, rcv, data, flags);
  2025. if (!IS_ERR(vs))
  2026. return vs;
  2027. if (no_share) /* Return error if sharing is not allowed. */
  2028. return vs;
  2029. spin_lock(&vn->sock_lock);
  2030. vs = vxlan_find_sock(net, port);
  2031. if (vs) {
  2032. if (vs->rcv == rcv)
  2033. atomic_inc(&vs->refcnt);
  2034. else
  2035. vs = ERR_PTR(-EBUSY);
  2036. }
  2037. spin_unlock(&vn->sock_lock);
  2038. if (!vs)
  2039. vs = ERR_PTR(-EINVAL);
  2040. return vs;
  2041. }
  2042. EXPORT_SYMBOL_GPL(vxlan_sock_add);
  2043. /* Scheduled at device creation to bind to a socket */
  2044. static void vxlan_sock_work(struct work_struct *work)
  2045. {
  2046. struct vxlan_dev *vxlan = container_of(work, struct vxlan_dev, sock_work);
  2047. struct net *net = vxlan->net;
  2048. struct vxlan_net *vn = net_generic(net, vxlan_net_id);
  2049. __be16 port = vxlan->dst_port;
  2050. struct vxlan_sock *nvs;
  2051. nvs = vxlan_sock_add(net, port, vxlan_rcv, NULL, false, vxlan->flags);
  2052. spin_lock(&vn->sock_lock);
  2053. if (!IS_ERR(nvs))
  2054. vxlan_vs_add_dev(nvs, vxlan);
  2055. spin_unlock(&vn->sock_lock);
  2056. dev_put(vxlan->dev);
  2057. }
  2058. static int vxlan_newlink(struct net *net, struct net_device *dev,
  2059. struct nlattr *tb[], struct nlattr *data[])
  2060. {
  2061. struct vxlan_net *vn = net_generic(net, vxlan_net_id);
  2062. struct vxlan_dev *vxlan = netdev_priv(dev);
  2063. struct vxlan_rdst *dst = &vxlan->default_dst;
  2064. __u32 vni;
  2065. int err;
  2066. bool use_ipv6 = false;
  2067. if (!data[IFLA_VXLAN_ID])
  2068. return -EINVAL;
  2069. vxlan->net = dev_net(dev);
  2070. vni = nla_get_u32(data[IFLA_VXLAN_ID]);
  2071. dst->remote_vni = vni;
  2072. /* Unless IPv6 is explicitly requested, assume IPv4 */
  2073. dst->remote_ip.sa.sa_family = AF_INET;
  2074. if (data[IFLA_VXLAN_GROUP]) {
  2075. dst->remote_ip.sin.sin_addr.s_addr = nla_get_be32(data[IFLA_VXLAN_GROUP]);
  2076. } else if (data[IFLA_VXLAN_GROUP6]) {
  2077. if (!IS_ENABLED(CONFIG_IPV6))
  2078. return -EPFNOSUPPORT;
  2079. nla_memcpy(&dst->remote_ip.sin6.sin6_addr, data[IFLA_VXLAN_GROUP6],
  2080. sizeof(struct in6_addr));
  2081. dst->remote_ip.sa.sa_family = AF_INET6;
  2082. use_ipv6 = true;
  2083. }
  2084. if (data[IFLA_VXLAN_LOCAL]) {
  2085. vxlan->saddr.sin.sin_addr.s_addr = nla_get_be32(data[IFLA_VXLAN_LOCAL]);
  2086. vxlan->saddr.sa.sa_family = AF_INET;
  2087. } else if (data[IFLA_VXLAN_LOCAL6]) {
  2088. if (!IS_ENABLED(CONFIG_IPV6))
  2089. return -EPFNOSUPPORT;
  2090. /* TODO: respect scope id */
  2091. nla_memcpy(&vxlan->saddr.sin6.sin6_addr, data[IFLA_VXLAN_LOCAL6],
  2092. sizeof(struct in6_addr));
  2093. vxlan->saddr.sa.sa_family = AF_INET6;
  2094. use_ipv6 = true;
  2095. }
  2096. if (data[IFLA_VXLAN_LINK] &&
  2097. (dst->remote_ifindex = nla_get_u32(data[IFLA_VXLAN_LINK]))) {
  2098. struct net_device *lowerdev
  2099. = __dev_get_by_index(net, dst->remote_ifindex);
  2100. if (!lowerdev) {
  2101. pr_info("ifindex %d does not exist\n", dst->remote_ifindex);
  2102. return -ENODEV;
  2103. }
  2104. #if IS_ENABLED(CONFIG_IPV6)
  2105. if (use_ipv6) {
  2106. struct inet6_dev *idev = __in6_dev_get(lowerdev);
  2107. if (idev && idev->cnf.disable_ipv6) {
  2108. pr_info("IPv6 is disabled via sysctl\n");
  2109. return -EPERM;
  2110. }
  2111. vxlan->flags |= VXLAN_F_IPV6;
  2112. }
  2113. #endif
  2114. if (!tb[IFLA_MTU])
  2115. dev->mtu = lowerdev->mtu - (use_ipv6 ? VXLAN6_HEADROOM : VXLAN_HEADROOM);
  2116. dev->needed_headroom = lowerdev->hard_header_len +
  2117. (use_ipv6 ? VXLAN6_HEADROOM : VXLAN_HEADROOM);
  2118. } else if (use_ipv6)
  2119. vxlan->flags |= VXLAN_F_IPV6;
  2120. if (data[IFLA_VXLAN_TOS])
  2121. vxlan->tos = nla_get_u8(data[IFLA_VXLAN_TOS]);
  2122. if (data[IFLA_VXLAN_TTL])
  2123. vxlan->ttl = nla_get_u8(data[IFLA_VXLAN_TTL]);
  2124. if (!data[IFLA_VXLAN_LEARNING] || nla_get_u8(data[IFLA_VXLAN_LEARNING]))
  2125. vxlan->flags |= VXLAN_F_LEARN;
  2126. if (data[IFLA_VXLAN_AGEING])
  2127. vxlan->age_interval = nla_get_u32(data[IFLA_VXLAN_AGEING]);
  2128. else
  2129. vxlan->age_interval = FDB_AGE_DEFAULT;
  2130. if (data[IFLA_VXLAN_PROXY] && nla_get_u8(data[IFLA_VXLAN_PROXY]))
  2131. vxlan->flags |= VXLAN_F_PROXY;
  2132. if (data[IFLA_VXLAN_RSC] && nla_get_u8(data[IFLA_VXLAN_RSC]))
  2133. vxlan->flags |= VXLAN_F_RSC;
  2134. if (data[IFLA_VXLAN_L2MISS] && nla_get_u8(data[IFLA_VXLAN_L2MISS]))
  2135. vxlan->flags |= VXLAN_F_L2MISS;
  2136. if (data[IFLA_VXLAN_L3MISS] && nla_get_u8(data[IFLA_VXLAN_L3MISS]))
  2137. vxlan->flags |= VXLAN_F_L3MISS;
  2138. if (data[IFLA_VXLAN_LIMIT])
  2139. vxlan->addrmax = nla_get_u32(data[IFLA_VXLAN_LIMIT]);
  2140. if (data[IFLA_VXLAN_PORT_RANGE]) {
  2141. const struct ifla_vxlan_port_range *p
  2142. = nla_data(data[IFLA_VXLAN_PORT_RANGE]);
  2143. vxlan->port_min = ntohs(p->low);
  2144. vxlan->port_max = ntohs(p->high);
  2145. }
  2146. if (data[IFLA_VXLAN_PORT])
  2147. vxlan->dst_port = nla_get_be16(data[IFLA_VXLAN_PORT]);
  2148. if (data[IFLA_VXLAN_UDP_CSUM] && nla_get_u8(data[IFLA_VXLAN_UDP_CSUM]))
  2149. vxlan->flags |= VXLAN_F_UDP_CSUM;
  2150. if (data[IFLA_VXLAN_UDP_ZERO_CSUM6_TX] &&
  2151. nla_get_u8(data[IFLA_VXLAN_UDP_ZERO_CSUM6_TX]))
  2152. vxlan->flags |= VXLAN_F_UDP_ZERO_CSUM6_TX;
  2153. if (data[IFLA_VXLAN_UDP_ZERO_CSUM6_RX] &&
  2154. nla_get_u8(data[IFLA_VXLAN_UDP_ZERO_CSUM6_RX]))
  2155. vxlan->flags |= VXLAN_F_UDP_ZERO_CSUM6_RX;
  2156. if (vxlan_find_vni(net, vni, vxlan->dst_port)) {
  2157. pr_info("duplicate VNI %u\n", vni);
  2158. return -EEXIST;
  2159. }
  2160. dev->ethtool_ops = &vxlan_ethtool_ops;
  2161. /* create an fdb entry for a valid default destination */
  2162. if (!vxlan_addr_any(&vxlan->default_dst.remote_ip)) {
  2163. err = vxlan_fdb_create(vxlan, all_zeros_mac,
  2164. &vxlan->default_dst.remote_ip,
  2165. NUD_REACHABLE|NUD_PERMANENT,
  2166. NLM_F_EXCL|NLM_F_CREATE,
  2167. vxlan->dst_port,
  2168. vxlan->default_dst.remote_vni,
  2169. vxlan->default_dst.remote_ifindex,
  2170. NTF_SELF);
  2171. if (err)
  2172. return err;
  2173. }
  2174. err = register_netdevice(dev);
  2175. if (err) {
  2176. vxlan_fdb_delete_default(vxlan);
  2177. return err;
  2178. }
  2179. list_add(&vxlan->next, &vn->vxlan_list);
  2180. return 0;
  2181. }
  2182. static void vxlan_dellink(struct net_device *dev, struct list_head *head)
  2183. {
  2184. struct vxlan_dev *vxlan = netdev_priv(dev);
  2185. struct vxlan_net *vn = net_generic(vxlan->net, vxlan_net_id);
  2186. spin_lock(&vn->sock_lock);
  2187. if (!hlist_unhashed(&vxlan->hlist))
  2188. hlist_del_rcu(&vxlan->hlist);
  2189. spin_unlock(&vn->sock_lock);
  2190. list_del(&vxlan->next);
  2191. unregister_netdevice_queue(dev, head);
  2192. }
  2193. static size_t vxlan_get_size(const struct net_device *dev)
  2194. {
  2195. return nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_ID */
  2196. nla_total_size(sizeof(struct in6_addr)) + /* IFLA_VXLAN_GROUP{6} */
  2197. nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_LINK */
  2198. nla_total_size(sizeof(struct in6_addr)) + /* IFLA_VXLAN_LOCAL{6} */
  2199. nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_TTL */
  2200. nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_TOS */
  2201. nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_LEARNING */
  2202. nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_PROXY */
  2203. nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_RSC */
  2204. nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_L2MISS */
  2205. nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_L3MISS */
  2206. nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_AGEING */
  2207. nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_LIMIT */
  2208. nla_total_size(sizeof(struct ifla_vxlan_port_range)) +
  2209. nla_total_size(sizeof(__be16)) + /* IFLA_VXLAN_PORT */
  2210. nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_UDP_CSUM */
  2211. nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_UDP_ZERO_CSUM6_TX */
  2212. nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_UDP_ZERO_CSUM6_RX */
  2213. 0;
  2214. }
  2215. static int vxlan_fill_info(struct sk_buff *skb, const struct net_device *dev)
  2216. {
  2217. const struct vxlan_dev *vxlan = netdev_priv(dev);
  2218. const struct vxlan_rdst *dst = &vxlan->default_dst;
  2219. struct ifla_vxlan_port_range ports = {
  2220. .low = htons(vxlan->port_min),
  2221. .high = htons(vxlan->port_max),
  2222. };
  2223. if (nla_put_u32(skb, IFLA_VXLAN_ID, dst->remote_vni))
  2224. goto nla_put_failure;
  2225. if (!vxlan_addr_any(&dst->remote_ip)) {
  2226. if (dst->remote_ip.sa.sa_family == AF_INET) {
  2227. if (nla_put_be32(skb, IFLA_VXLAN_GROUP,
  2228. dst->remote_ip.sin.sin_addr.s_addr))
  2229. goto nla_put_failure;
  2230. #if IS_ENABLED(CONFIG_IPV6)
  2231. } else {
  2232. if (nla_put(skb, IFLA_VXLAN_GROUP6, sizeof(struct in6_addr),
  2233. &dst->remote_ip.sin6.sin6_addr))
  2234. goto nla_put_failure;
  2235. #endif
  2236. }
  2237. }
  2238. if (dst->remote_ifindex && nla_put_u32(skb, IFLA_VXLAN_LINK, dst->remote_ifindex))
  2239. goto nla_put_failure;
  2240. if (!vxlan_addr_any(&vxlan->saddr)) {
  2241. if (vxlan->saddr.sa.sa_family == AF_INET) {
  2242. if (nla_put_be32(skb, IFLA_VXLAN_LOCAL,
  2243. vxlan->saddr.sin.sin_addr.s_addr))
  2244. goto nla_put_failure;
  2245. #if IS_ENABLED(CONFIG_IPV6)
  2246. } else {
  2247. if (nla_put(skb, IFLA_VXLAN_LOCAL6, sizeof(struct in6_addr),
  2248. &vxlan->saddr.sin6.sin6_addr))
  2249. goto nla_put_failure;
  2250. #endif
  2251. }
  2252. }
  2253. if (nla_put_u8(skb, IFLA_VXLAN_TTL, vxlan->ttl) ||
  2254. nla_put_u8(skb, IFLA_VXLAN_TOS, vxlan->tos) ||
  2255. nla_put_u8(skb, IFLA_VXLAN_LEARNING,
  2256. !!(vxlan->flags & VXLAN_F_LEARN)) ||
  2257. nla_put_u8(skb, IFLA_VXLAN_PROXY,
  2258. !!(vxlan->flags & VXLAN_F_PROXY)) ||
  2259. nla_put_u8(skb, IFLA_VXLAN_RSC, !!(vxlan->flags & VXLAN_F_RSC)) ||
  2260. nla_put_u8(skb, IFLA_VXLAN_L2MISS,
  2261. !!(vxlan->flags & VXLAN_F_L2MISS)) ||
  2262. nla_put_u8(skb, IFLA_VXLAN_L3MISS,
  2263. !!(vxlan->flags & VXLAN_F_L3MISS)) ||
  2264. nla_put_u32(skb, IFLA_VXLAN_AGEING, vxlan->age_interval) ||
  2265. nla_put_u32(skb, IFLA_VXLAN_LIMIT, vxlan->addrmax) ||
  2266. nla_put_be16(skb, IFLA_VXLAN_PORT, vxlan->dst_port) ||
  2267. nla_put_u8(skb, IFLA_VXLAN_UDP_CSUM,
  2268. !!(vxlan->flags & VXLAN_F_UDP_CSUM)) ||
  2269. nla_put_u8(skb, IFLA_VXLAN_UDP_ZERO_CSUM6_TX,
  2270. !!(vxlan->flags & VXLAN_F_UDP_ZERO_CSUM6_TX)) ||
  2271. nla_put_u8(skb, IFLA_VXLAN_UDP_ZERO_CSUM6_RX,
  2272. !!(vxlan->flags & VXLAN_F_UDP_ZERO_CSUM6_RX)))
  2273. goto nla_put_failure;
  2274. if (nla_put(skb, IFLA_VXLAN_PORT_RANGE, sizeof(ports), &ports))
  2275. goto nla_put_failure;
  2276. return 0;
  2277. nla_put_failure:
  2278. return -EMSGSIZE;
  2279. }
  2280. static struct rtnl_link_ops vxlan_link_ops __read_mostly = {
  2281. .kind = "vxlan",
  2282. .maxtype = IFLA_VXLAN_MAX,
  2283. .policy = vxlan_policy,
  2284. .priv_size = sizeof(struct vxlan_dev),
  2285. .setup = vxlan_setup,
  2286. .validate = vxlan_validate,
  2287. .newlink = vxlan_newlink,
  2288. .dellink = vxlan_dellink,
  2289. .get_size = vxlan_get_size,
  2290. .fill_info = vxlan_fill_info,
  2291. };
  2292. static void vxlan_handle_lowerdev_unregister(struct vxlan_net *vn,
  2293. struct net_device *dev)
  2294. {
  2295. struct vxlan_dev *vxlan, *next;
  2296. LIST_HEAD(list_kill);
  2297. list_for_each_entry_safe(vxlan, next, &vn->vxlan_list, next) {
  2298. struct vxlan_rdst *dst = &vxlan->default_dst;
  2299. /* In case we created vxlan device with carrier
  2300. * and we loose the carrier due to module unload
  2301. * we also need to remove vxlan device. In other
  2302. * cases, it's not necessary and remote_ifindex
  2303. * is 0 here, so no matches.
  2304. */
  2305. if (dst->remote_ifindex == dev->ifindex)
  2306. vxlan_dellink(vxlan->dev, &list_kill);
  2307. }
  2308. unregister_netdevice_many(&list_kill);
  2309. }
  2310. static int vxlan_lowerdev_event(struct notifier_block *unused,
  2311. unsigned long event, void *ptr)
  2312. {
  2313. struct net_device *dev = netdev_notifier_info_to_dev(ptr);
  2314. struct vxlan_net *vn = net_generic(dev_net(dev), vxlan_net_id);
  2315. if (event == NETDEV_UNREGISTER)
  2316. vxlan_handle_lowerdev_unregister(vn, dev);
  2317. return NOTIFY_DONE;
  2318. }
  2319. static struct notifier_block vxlan_notifier_block __read_mostly = {
  2320. .notifier_call = vxlan_lowerdev_event,
  2321. };
  2322. static __net_init int vxlan_init_net(struct net *net)
  2323. {
  2324. struct vxlan_net *vn = net_generic(net, vxlan_net_id);
  2325. unsigned int h;
  2326. INIT_LIST_HEAD(&vn->vxlan_list);
  2327. spin_lock_init(&vn->sock_lock);
  2328. for (h = 0; h < PORT_HASH_SIZE; ++h)
  2329. INIT_HLIST_HEAD(&vn->sock_list[h]);
  2330. return 0;
  2331. }
  2332. static void __net_exit vxlan_exit_net(struct net *net)
  2333. {
  2334. struct vxlan_net *vn = net_generic(net, vxlan_net_id);
  2335. struct vxlan_dev *vxlan, *next;
  2336. struct net_device *dev, *aux;
  2337. LIST_HEAD(list);
  2338. rtnl_lock();
  2339. for_each_netdev_safe(net, dev, aux)
  2340. if (dev->rtnl_link_ops == &vxlan_link_ops)
  2341. unregister_netdevice_queue(dev, &list);
  2342. list_for_each_entry_safe(vxlan, next, &vn->vxlan_list, next) {
  2343. /* If vxlan->dev is in the same netns, it has already been added
  2344. * to the list by the previous loop.
  2345. */
  2346. if (!net_eq(dev_net(vxlan->dev), net))
  2347. unregister_netdevice_queue(dev, &list);
  2348. }
  2349. unregister_netdevice_many(&list);
  2350. rtnl_unlock();
  2351. }
  2352. static struct pernet_operations vxlan_net_ops = {
  2353. .init = vxlan_init_net,
  2354. .exit = vxlan_exit_net,
  2355. .id = &vxlan_net_id,
  2356. .size = sizeof(struct vxlan_net),
  2357. };
  2358. static int __init vxlan_init_module(void)
  2359. {
  2360. int rc;
  2361. vxlan_wq = alloc_workqueue("vxlan", 0, 0);
  2362. if (!vxlan_wq)
  2363. return -ENOMEM;
  2364. get_random_bytes(&vxlan_salt, sizeof(vxlan_salt));
  2365. rc = register_pernet_subsys(&vxlan_net_ops);
  2366. if (rc)
  2367. goto out1;
  2368. rc = register_netdevice_notifier(&vxlan_notifier_block);
  2369. if (rc)
  2370. goto out2;
  2371. rc = rtnl_link_register(&vxlan_link_ops);
  2372. if (rc)
  2373. goto out3;
  2374. return 0;
  2375. out3:
  2376. unregister_netdevice_notifier(&vxlan_notifier_block);
  2377. out2:
  2378. unregister_pernet_subsys(&vxlan_net_ops);
  2379. out1:
  2380. destroy_workqueue(vxlan_wq);
  2381. return rc;
  2382. }
  2383. late_initcall(vxlan_init_module);
  2384. static void __exit vxlan_cleanup_module(void)
  2385. {
  2386. rtnl_link_unregister(&vxlan_link_ops);
  2387. unregister_netdevice_notifier(&vxlan_notifier_block);
  2388. destroy_workqueue(vxlan_wq);
  2389. unregister_pernet_subsys(&vxlan_net_ops);
  2390. /* rcu_barrier() is called by netns */
  2391. }
  2392. module_exit(vxlan_cleanup_module);
  2393. MODULE_LICENSE("GPL");
  2394. MODULE_VERSION(VXLAN_VERSION);
  2395. MODULE_AUTHOR("Stephen Hemminger <stephen@networkplumber.org>");
  2396. MODULE_DESCRIPTION("Driver for VXLAN encapsulated traffic");
  2397. MODULE_ALIAS_RTNL_LINK("vxlan");