vxlan.c 65 KB

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