vxlan.c 67 KB

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