system-linux.c 97 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039
  1. /*
  2. * netifd - network interface daemon
  3. * Copyright (C) 2012 Felix Fietkau <nbd@openwrt.org>
  4. * Copyright (C) 2013 Jo-Philipp Wich <jow@openwrt.org>
  5. * Copyright (C) 2013 Steven Barth <steven@midlink.org>
  6. * Copyright (C) 2014 Gioacchino Mazzurco <gio@eigenlab.org>
  7. * Copyright (C) 2017 Matthias Schiffer <mschiffer@universe-factory.net>
  8. * Copyright (C) 2018 Hans Dedecker <dedeckeh@gmail.com>
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License version 2
  12. * as published by the Free Software Foundation
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. */
  19. #define _GNU_SOURCE
  20. #include <sys/socket.h>
  21. #include <sys/ioctl.h>
  22. #include <sys/stat.h>
  23. #include <sys/syscall.h>
  24. #include <net/if.h>
  25. #include <net/if_arp.h>
  26. #include <limits.h>
  27. #include <arpa/inet.h>
  28. #include <netinet/in.h>
  29. #include <netinet/ether.h>
  30. #include <linux/rtnetlink.h>
  31. #include <linux/neighbour.h>
  32. #include <linux/sockios.h>
  33. #include <linux/ip.h>
  34. #include <linux/if_addr.h>
  35. #include <linux/if_link.h>
  36. #include <linux/if_vlan.h>
  37. #include <linux/if_bridge.h>
  38. #include <linux/if_tunnel.h>
  39. #include <linux/ip6_tunnel.h>
  40. #include <linux/ethtool.h>
  41. #include <linux/fib_rules.h>
  42. #include <linux/veth.h>
  43. #include <linux/version.h>
  44. #include <sched.h>
  45. #ifndef RTN_FAILED_POLICY
  46. #define RTN_FAILED_POLICY 12
  47. #endif
  48. #ifndef IFA_F_NOPREFIXROUTE
  49. #define IFA_F_NOPREFIXROUTE 0x200
  50. #endif
  51. #ifndef IFA_FLAGS
  52. #define IFA_FLAGS (IFA_MULTICAST + 1)
  53. #endif
  54. #include <string.h>
  55. #include <fcntl.h>
  56. #include <glob.h>
  57. #include <time.h>
  58. #include <unistd.h>
  59. #include <netlink/msg.h>
  60. #include <netlink/attr.h>
  61. #include <netlink/socket.h>
  62. #include <libubox/uloop.h>
  63. #include "netifd.h"
  64. #include "device.h"
  65. #include "system.h"
  66. #include "utils.h"
  67. struct event_socket {
  68. struct uloop_fd uloop;
  69. struct nl_sock *sock;
  70. int bufsize;
  71. };
  72. static int sock_ioctl = -1;
  73. static struct nl_sock *sock_rtnl = NULL;
  74. static int cb_rtnl_event(struct nl_msg *msg, void *arg);
  75. static void handle_hotplug_event(struct uloop_fd *u, unsigned int events);
  76. static int system_add_proto_tunnel(const char *name, const uint8_t proto,
  77. const unsigned int link, struct blob_attr **tb);
  78. static char dev_buf[256];
  79. static const char *proc_path = "/proc";
  80. static const char *sysfs_path = "/sys";
  81. static void
  82. handler_nl_event(struct uloop_fd *u, unsigned int events)
  83. {
  84. struct event_socket *ev = container_of(u, struct event_socket, uloop);
  85. int err;
  86. socklen_t errlen = sizeof(err);
  87. if (!u->error) {
  88. nl_recvmsgs_default(ev->sock);
  89. return;
  90. }
  91. if (getsockopt(u->fd, SOL_SOCKET, SO_ERROR, (void *)&err, &errlen))
  92. goto abort;
  93. switch(err) {
  94. case ENOBUFS:
  95. /* Increase rx buffer size on netlink socket */
  96. ev->bufsize *= 2;
  97. if (nl_socket_set_buffer_size(ev->sock, ev->bufsize, 0))
  98. goto abort;
  99. /* Request full dump since some info got dropped */
  100. struct rtgenmsg msg = { .rtgen_family = AF_UNSPEC };
  101. nl_send_simple(ev->sock, RTM_GETLINK, NLM_F_DUMP, &msg, sizeof(msg));
  102. break;
  103. default:
  104. goto abort;
  105. }
  106. u->error = false;
  107. return;
  108. abort:
  109. uloop_fd_delete(&ev->uloop);
  110. return;
  111. }
  112. static struct nl_sock *
  113. create_socket(int protocol, int groups)
  114. {
  115. struct nl_sock *sock;
  116. sock = nl_socket_alloc();
  117. if (!sock)
  118. return NULL;
  119. if (groups)
  120. nl_join_groups(sock, groups);
  121. if (nl_connect(sock, protocol)) {
  122. nl_socket_free(sock);
  123. return NULL;
  124. }
  125. return sock;
  126. }
  127. static bool
  128. create_raw_event_socket(struct event_socket *ev, int protocol, int groups,
  129. uloop_fd_handler cb, int flags)
  130. {
  131. ev->sock = create_socket(protocol, groups);
  132. if (!ev->sock)
  133. return false;
  134. ev->uloop.fd = nl_socket_get_fd(ev->sock);
  135. ev->uloop.cb = cb;
  136. if (uloop_fd_add(&ev->uloop, ULOOP_READ|flags))
  137. return false;
  138. return true;
  139. }
  140. static bool
  141. create_event_socket(struct event_socket *ev, int protocol,
  142. int (*cb)(struct nl_msg *msg, void *arg))
  143. {
  144. if (!create_raw_event_socket(ev, protocol, 0, handler_nl_event, ULOOP_ERROR_CB))
  145. return false;
  146. /* Install the valid custom callback handler */
  147. nl_socket_modify_cb(ev->sock, NL_CB_VALID, NL_CB_CUSTOM, cb, NULL);
  148. /* Disable sequence number checking on event sockets */
  149. nl_socket_disable_seq_check(ev->sock);
  150. /* Increase rx buffer size to 65K on event sockets */
  151. ev->bufsize = 65535;
  152. if (nl_socket_set_buffer_size(ev->sock, ev->bufsize, 0))
  153. return false;
  154. return true;
  155. }
  156. static bool
  157. create_hotplug_event_socket(struct event_socket *ev, int protocol,
  158. void (*cb)(struct uloop_fd *u, unsigned int events))
  159. {
  160. if (!create_raw_event_socket(ev, protocol, 1, cb, ULOOP_ERROR_CB))
  161. return false;
  162. /* Increase rx buffer size to 65K on event sockets */
  163. ev->bufsize = 65535;
  164. if (nl_socket_set_buffer_size(ev->sock, ev->bufsize, 0))
  165. return false;
  166. return true;
  167. }
  168. static bool
  169. system_rtn_aton(const char *src, unsigned int *dst)
  170. {
  171. char *e;
  172. unsigned int n;
  173. if (!strcmp(src, "local"))
  174. n = RTN_LOCAL;
  175. else if (!strcmp(src, "nat"))
  176. n = RTN_NAT;
  177. else if (!strcmp(src, "broadcast"))
  178. n = RTN_BROADCAST;
  179. else if (!strcmp(src, "anycast"))
  180. n = RTN_ANYCAST;
  181. else if (!strcmp(src, "multicast"))
  182. n = RTN_MULTICAST;
  183. else if (!strcmp(src, "prohibit"))
  184. n = RTN_PROHIBIT;
  185. else if (!strcmp(src, "unreachable"))
  186. n = RTN_UNREACHABLE;
  187. else if (!strcmp(src, "blackhole"))
  188. n = RTN_BLACKHOLE;
  189. else if (!strcmp(src, "xresolve"))
  190. n = RTN_XRESOLVE;
  191. else if (!strcmp(src, "unicast"))
  192. n = RTN_UNICAST;
  193. else if (!strcmp(src, "throw"))
  194. n = RTN_THROW;
  195. else if (!strcmp(src, "failed_policy"))
  196. n = RTN_FAILED_POLICY;
  197. else {
  198. n = strtoul(src, &e, 0);
  199. if (!e || *e || e == src || n > 255)
  200. return false;
  201. }
  202. *dst = n;
  203. return true;
  204. }
  205. static bool
  206. system_tos_aton(const char *src, unsigned *dst)
  207. {
  208. char *e;
  209. *dst = strtoul(src, &e, 16);
  210. if (e == src || *e || *dst > 255)
  211. return false;
  212. return true;
  213. }
  214. int system_init(void)
  215. {
  216. static struct event_socket rtnl_event;
  217. static struct event_socket hotplug_event;
  218. sock_ioctl = socket(AF_LOCAL, SOCK_DGRAM, 0);
  219. system_fd_set_cloexec(sock_ioctl);
  220. /* Prepare socket for routing / address control */
  221. sock_rtnl = create_socket(NETLINK_ROUTE, 0);
  222. if (!sock_rtnl)
  223. return -1;
  224. if (!create_event_socket(&rtnl_event, NETLINK_ROUTE, cb_rtnl_event))
  225. return -1;
  226. if (!create_hotplug_event_socket(&hotplug_event, NETLINK_KOBJECT_UEVENT,
  227. handle_hotplug_event))
  228. return -1;
  229. /* Receive network link events form kernel */
  230. nl_socket_add_membership(rtnl_event.sock, RTNLGRP_LINK);
  231. return 0;
  232. }
  233. static void write_file(const char *path, const char *val)
  234. {
  235. int fd;
  236. fd = open(path, O_WRONLY);
  237. if (fd < 0)
  238. return;
  239. if (write(fd, val, strlen(val))) {}
  240. close(fd);
  241. }
  242. static int read_file(const char *path, char *buf, const size_t buf_sz)
  243. {
  244. int fd = -1, ret = -1;
  245. fd = open(path, O_RDONLY);
  246. if (fd < 0)
  247. goto out;
  248. ssize_t len = read(fd, buf, buf_sz - 1);
  249. if (len < 0)
  250. goto out;
  251. ret = buf[len] = 0;
  252. out:
  253. if (fd >= 0)
  254. close(fd);
  255. return ret;
  256. }
  257. static const char *
  258. dev_sysctl_path(const char *prefix, const char *ifname, const char *file)
  259. {
  260. snprintf(dev_buf, sizeof(dev_buf), "%s/sys/net/%s/%s/%s", proc_path, prefix, ifname, file);
  261. return dev_buf;
  262. }
  263. static const char *
  264. dev_sysfs_path(const char *ifname, const char *file)
  265. {
  266. snprintf(dev_buf, sizeof(dev_buf), "%s/class/net/%s/%s", sysfs_path, ifname, file);
  267. return dev_buf;
  268. }
  269. static void
  270. system_set_dev_sysctl(const char *prefix, const char *file, const char *ifname,
  271. const char *val)
  272. {
  273. write_file(dev_sysctl_path(prefix, ifname, file), val);
  274. }
  275. static int
  276. system_get_dev_sysctl(const char *prefix, const char *file, const char *ifname,
  277. char *buf, size_t buf_sz)
  278. {
  279. return read_file(dev_sysctl_path(prefix, ifname, file), buf, buf_sz);
  280. }
  281. static void
  282. system_set_dev_sysfs(const char *file, const char *ifname, const char *val)
  283. {
  284. if (!val)
  285. return;
  286. write_file(dev_sysfs_path(ifname, file), val);
  287. }
  288. static void
  289. system_set_dev_sysfs_int(const char *file, const char *ifname, int val)
  290. {
  291. char buf[16];
  292. snprintf(buf, sizeof(buf), "%d", val);
  293. system_set_dev_sysfs(file, ifname, buf);
  294. }
  295. static int
  296. system_get_dev_sysfs(const char *file, const char *ifname, char *buf, size_t buf_sz)
  297. {
  298. return read_file(dev_sysfs_path(ifname, file), buf, buf_sz);
  299. }
  300. static void system_set_disable_ipv6(struct device *dev, const char *val)
  301. {
  302. system_set_dev_sysctl("ipv6/conf", "disable_ipv6", dev->ifname, val);
  303. }
  304. static void system_set_ip6segmentrouting(struct device *dev, const char *val)
  305. {
  306. system_set_dev_sysctl("ipv6/conf", "seg6_enabled", dev->ifname, val);
  307. }
  308. static void system_set_rpfilter(struct device *dev, const char *val)
  309. {
  310. system_set_dev_sysctl("ipv4/conf", "rp_filter", dev->ifname, val);
  311. }
  312. static void system_set_acceptlocal(struct device *dev, const char *val)
  313. {
  314. system_set_dev_sysctl("ipv4/conf", "accept_local", dev->ifname, val);
  315. }
  316. static void system_set_igmpversion(struct device *dev, const char *val)
  317. {
  318. system_set_dev_sysctl("ipv4/conf", "force_igmp_version", dev->ifname, val);
  319. }
  320. static void system_set_mldversion(struct device *dev, const char *val)
  321. {
  322. system_set_dev_sysctl("ipv6/conf", "force_mld_version", dev->ifname, val);
  323. }
  324. static void system_set_neigh4reachabletime(struct device *dev, const char *val)
  325. {
  326. system_set_dev_sysctl("ipv4/neigh", "base_reachable_time_ms", dev->ifname, val);
  327. }
  328. static void system_set_neigh6reachabletime(struct device *dev, const char *val)
  329. {
  330. system_set_dev_sysctl("ipv6/neigh", "base_reachable_time_ms", dev->ifname, val);
  331. }
  332. static void system_set_neigh4gcstaletime(struct device *dev, const char *val)
  333. {
  334. system_set_dev_sysctl("ipv4/neigh", "gc_stale_time", dev->ifname, val);
  335. }
  336. static void system_set_neigh6gcstaletime(struct device *dev, const char *val)
  337. {
  338. system_set_dev_sysctl("ipv6/neigh", "gc_stale_time", dev->ifname, val);
  339. }
  340. static void system_set_neigh4locktime(struct device *dev, const char *val)
  341. {
  342. system_set_dev_sysctl("ipv4/neigh", "locktime", dev->ifname, val);
  343. }
  344. static void system_set_dadtransmits(struct device *dev, const char *val)
  345. {
  346. system_set_dev_sysctl("ipv6/conf", "dad_transmits", dev->ifname, val);
  347. }
  348. static void system_set_sendredirects(struct device *dev, const char *val)
  349. {
  350. system_set_dev_sysctl("ipv4/conf", "send_redirects", dev->ifname, val);
  351. }
  352. static void system_set_drop_v4_unicast_in_l2_multicast(struct device *dev, const char *val)
  353. {
  354. system_set_dev_sysctl("ipv4/conf", "drop_unicast_in_l2_multicast", dev->ifname, val);
  355. }
  356. static void system_set_drop_v6_unicast_in_l2_multicast(struct device *dev, const char *val)
  357. {
  358. system_set_dev_sysctl("ipv6/conf", "drop_unicast_in_l2_multicast", dev->ifname, val);
  359. }
  360. static void system_set_drop_gratuitous_arp(struct device *dev, const char *val)
  361. {
  362. system_set_dev_sysctl("ipv4/conf", "drop_gratuitous_arp", dev->ifname, val);
  363. }
  364. static void system_set_drop_unsolicited_na(struct device *dev, const char *val)
  365. {
  366. system_set_dev_sysctl("ipv6/conf", "drop_unsolicited_na", dev->ifname, val);
  367. }
  368. static void system_set_arp_accept(struct device *dev, const char *val)
  369. {
  370. system_set_dev_sysctl("ipv4/conf", "arp_accept", dev->ifname, val);
  371. }
  372. static void system_bridge_set_multicast_to_unicast(struct device *dev, const char *val)
  373. {
  374. system_set_dev_sysfs("brport/multicast_to_unicast", dev->ifname, val);
  375. }
  376. static void system_bridge_set_multicast_fast_leave(struct device *dev, const char *val)
  377. {
  378. system_set_dev_sysfs("brport/multicast_fast_leave", dev->ifname, val);
  379. }
  380. static void system_bridge_set_hairpin_mode(struct device *dev, const char *val)
  381. {
  382. system_set_dev_sysfs("brport/hairpin_mode", dev->ifname, val);
  383. }
  384. static void system_bridge_set_proxyarp_wifi(struct device *dev, const char *val)
  385. {
  386. system_set_dev_sysfs("brport/proxyarp_wifi", dev->ifname, val);
  387. }
  388. static void system_bridge_set_bpdu_filter(struct device *dev, const char *val)
  389. {
  390. system_set_dev_sysfs("brport/bpdu_filter", dev->ifname, val);
  391. }
  392. static void system_bridge_set_isolated(struct device *dev, const char *val)
  393. {
  394. system_set_dev_sysfs("brport/isolated", dev->ifname, val);
  395. }
  396. static void system_bridge_set_multicast_router(struct device *dev, const char *val)
  397. {
  398. system_set_dev_sysfs("brport/multicast_router", dev->ifname, val);
  399. }
  400. void system_bridge_set_stp_state(struct device *dev, bool val)
  401. {
  402. const char *valstr = val ? "1" : "0";
  403. system_set_dev_sysfs("bridge/stp_state", dev->ifname, valstr);
  404. }
  405. static void system_bridge_set_learning(struct device *dev, const char *val)
  406. {
  407. system_set_dev_sysfs("brport/learning", dev->ifname, val);
  408. }
  409. static void system_bridge_set_unicast_flood(struct device *dev, const char *val)
  410. {
  411. system_set_dev_sysfs("brport/unicast_flood", dev->ifname, val);
  412. }
  413. static int system_get_disable_ipv6(struct device *dev, char *buf, const size_t buf_sz)
  414. {
  415. return system_get_dev_sysctl("ipv6/conf", "disable_ipv6",
  416. dev->ifname, buf, buf_sz);
  417. }
  418. static int system_get_ip6segmentrouting(struct device *dev, char *buf, const size_t buf_sz)
  419. {
  420. return system_get_dev_sysctl("ipv6/conf", "seg6_enabled",
  421. dev->ifname, buf, buf_sz);
  422. }
  423. static int system_get_rpfilter(struct device *dev, char *buf, const size_t buf_sz)
  424. {
  425. return system_get_dev_sysctl("ipv4/conf", "rp_filter",
  426. dev->ifname, buf, buf_sz);
  427. }
  428. static int system_get_acceptlocal(struct device *dev, char *buf, const size_t buf_sz)
  429. {
  430. return system_get_dev_sysctl("ipv4/conf", "accept_local",
  431. dev->ifname, buf, buf_sz);
  432. }
  433. static int system_get_igmpversion(struct device *dev, char *buf, const size_t buf_sz)
  434. {
  435. return system_get_dev_sysctl("ipv4/conf", "force_igmp_version",
  436. dev->ifname, buf, buf_sz);
  437. }
  438. static int system_get_mldversion(struct device *dev, char *buf, const size_t buf_sz)
  439. {
  440. return system_get_dev_sysctl("ipv6/conf", "force_mld_version",
  441. dev->ifname, buf, buf_sz);
  442. }
  443. static int system_get_neigh4reachabletime(struct device *dev, char *buf, const size_t buf_sz)
  444. {
  445. return system_get_dev_sysctl("ipv4/neigh", "base_reachable_time_ms",
  446. dev->ifname, buf, buf_sz);
  447. }
  448. static int system_get_neigh6reachabletime(struct device *dev, char *buf, const size_t buf_sz)
  449. {
  450. return system_get_dev_sysctl("ipv6/neigh", "base_reachable_time_ms",
  451. dev->ifname, buf, buf_sz);
  452. }
  453. static int system_get_neigh4gcstaletime(struct device *dev, char *buf, const size_t buf_sz)
  454. {
  455. return system_get_dev_sysctl("ipv4/neigh", "gc_stale_time",
  456. dev->ifname, buf, buf_sz);
  457. }
  458. static int system_get_neigh6gcstaletime(struct device *dev, char *buf, const size_t buf_sz)
  459. {
  460. return system_get_dev_sysctl("ipv6/neigh", "gc_stale_time",
  461. dev->ifname, buf, buf_sz);
  462. }
  463. static int system_get_neigh4locktime(struct device *dev, char *buf, const size_t buf_sz)
  464. {
  465. return system_get_dev_sysctl("ipv4/neigh", "locktime",
  466. dev->ifname, buf, buf_sz);
  467. }
  468. static int system_get_dadtransmits(struct device *dev, char *buf, const size_t buf_sz)
  469. {
  470. return system_get_dev_sysctl("ipv6/conf", "dad_transmits",
  471. dev->ifname, buf, buf_sz);
  472. }
  473. static int system_get_sendredirects(struct device *dev, char *buf, const size_t buf_sz)
  474. {
  475. return system_get_dev_sysctl("ipv4/conf", "send_redirects",
  476. dev->ifname, buf, buf_sz);
  477. }
  478. static int system_get_drop_v4_unicast_in_l2_multicast(struct device *dev, char *buf, const size_t buf_sz)
  479. {
  480. return system_get_dev_sysctl("ipv4/conf", "drop_unicast_in_l2_multicast",
  481. dev->ifname, buf, buf_sz);
  482. }
  483. static int system_get_drop_v6_unicast_in_l2_multicast(struct device *dev, char *buf, const size_t buf_sz)
  484. {
  485. return system_get_dev_sysctl("ipv6/conf", "drop_unicast_in_l2_multicast",
  486. dev->ifname, buf, buf_sz);
  487. }
  488. static int system_get_drop_gratuitous_arp(struct device *dev, char *buf, const size_t buf_sz)
  489. {
  490. return system_get_dev_sysctl("ipv4/conf", "drop_gratuitous_arp",
  491. dev->ifname, buf, buf_sz);
  492. }
  493. static int system_get_drop_unsolicited_na(struct device *dev, char *buf, const size_t buf_sz)
  494. {
  495. return system_get_dev_sysctl("ipv6/conf", "drop_unsolicited_na",
  496. dev->ifname, buf, buf_sz);
  497. }
  498. static int system_get_arp_accept(struct device *dev, char *buf, const size_t buf_sz)
  499. {
  500. return system_get_dev_sysctl("ipv4/conf", "arp_accept",
  501. dev->ifname, buf, buf_sz);
  502. }
  503. /* Evaluate netlink messages */
  504. static int cb_rtnl_event(struct nl_msg *msg, void *arg)
  505. {
  506. struct nlmsghdr *nh = nlmsg_hdr(msg);
  507. struct nlattr *nla[__IFLA_MAX];
  508. int link_state = 0;
  509. char buf[10];
  510. if (nh->nlmsg_type != RTM_NEWLINK)
  511. goto out;
  512. nlmsg_parse(nh, sizeof(struct ifinfomsg), nla, __IFLA_MAX - 1, NULL);
  513. if (!nla[IFLA_IFNAME])
  514. goto out;
  515. struct device *dev = device_find(nla_data(nla[IFLA_IFNAME]));
  516. if (!dev)
  517. goto out;
  518. if (!system_get_dev_sysfs("carrier", dev->ifname, buf, sizeof(buf)))
  519. link_state = strtoul(buf, NULL, 0);
  520. if (dev->type == &simple_device_type)
  521. device_set_present(dev, true);
  522. device_set_link(dev, link_state ? true : false);
  523. out:
  524. return 0;
  525. }
  526. static void
  527. handle_hotplug_msg(char *data, int size)
  528. {
  529. const char *subsystem = NULL, *interface = NULL, *interface_old = NULL;
  530. char *cur, *end, *sep;
  531. int skip;
  532. bool add;
  533. if (!strncmp(data, "add@", 4) || !strncmp(data, "move@", 5))
  534. add = true;
  535. else if (!strncmp(data, "remove@", 7))
  536. add = false;
  537. else
  538. return;
  539. skip = strlen(data) + 1;
  540. end = data + size;
  541. for (cur = data + skip; cur < end; cur += skip) {
  542. skip = strlen(cur) + 1;
  543. sep = strchr(cur, '=');
  544. if (!sep)
  545. continue;
  546. *sep = 0;
  547. if (!strcmp(cur, "INTERFACE"))
  548. interface = sep + 1;
  549. else if (!strcmp(cur, "SUBSYSTEM")) {
  550. subsystem = sep + 1;
  551. if (strcmp(subsystem, "net") != 0)
  552. return;
  553. } else if (!strcmp(cur, "DEVPATH_OLD")) {
  554. interface_old = strrchr(sep + 1, '/');
  555. if (interface_old)
  556. interface_old++;
  557. }
  558. }
  559. if (!subsystem || !interface)
  560. return;
  561. if (interface_old)
  562. device_hotplug_event(interface_old, false);
  563. device_hotplug_event(interface, add);
  564. }
  565. static void
  566. handle_hotplug_event(struct uloop_fd *u, unsigned int events)
  567. {
  568. struct event_socket *ev = container_of(u, struct event_socket, uloop);
  569. struct sockaddr_nl nla;
  570. unsigned char *buf = NULL;
  571. int size;
  572. int err;
  573. socklen_t errlen = sizeof(err);
  574. if (!u->error) {
  575. while ((size = nl_recv(ev->sock, &nla, &buf, NULL)) > 0) {
  576. if (nla.nl_pid == 0)
  577. handle_hotplug_msg((char *) buf, size);
  578. free(buf);
  579. }
  580. return;
  581. }
  582. if (getsockopt(u->fd, SOL_SOCKET, SO_ERROR, (void *)&err, &errlen))
  583. goto abort;
  584. switch(err) {
  585. case ENOBUFS:
  586. /* Increase rx buffer size on netlink socket */
  587. ev->bufsize *= 2;
  588. if (nl_socket_set_buffer_size(ev->sock, ev->bufsize, 0))
  589. goto abort;
  590. break;
  591. default:
  592. goto abort;
  593. }
  594. u->error = false;
  595. return;
  596. abort:
  597. uloop_fd_delete(&ev->uloop);
  598. return;
  599. }
  600. static int system_rtnl_call(struct nl_msg *msg)
  601. {
  602. int ret;
  603. ret = nl_send_auto_complete(sock_rtnl, msg);
  604. nlmsg_free(msg);
  605. if (ret < 0)
  606. return ret;
  607. return nl_wait_for_ack(sock_rtnl);
  608. }
  609. static struct nl_msg *__system_ifinfo_msg(int af, int index, const char *ifname, uint16_t type, uint16_t flags)
  610. {
  611. struct nl_msg *msg;
  612. struct ifinfomsg iim = {
  613. .ifi_family = af,
  614. .ifi_index = index,
  615. };
  616. msg = nlmsg_alloc_simple(type, flags | NLM_F_REQUEST);
  617. if (!msg)
  618. return NULL;
  619. nlmsg_append(msg, &iim, sizeof(iim), 0);
  620. if (ifname)
  621. nla_put_string(msg, IFLA_IFNAME, ifname);
  622. return msg;
  623. }
  624. static struct nl_msg *system_ifinfo_msg(const char *ifname, uint16_t type, uint16_t flags)
  625. {
  626. return __system_ifinfo_msg(AF_UNSPEC, 0, ifname, type, flags);
  627. }
  628. static int system_link_del(const char *ifname)
  629. {
  630. struct nl_msg *msg;
  631. msg = system_ifinfo_msg(ifname, RTM_DELLINK, 0);
  632. if (!msg)
  633. return -1;
  634. return system_rtnl_call(msg);
  635. }
  636. int system_bridge_delbr(struct device *bridge)
  637. {
  638. return system_link_del(bridge->ifname);
  639. }
  640. static int system_bridge_if(const char *bridge, struct device *dev, int cmd, void *data)
  641. {
  642. struct ifreq ifr;
  643. memset(&ifr, 0, sizeof(ifr));
  644. if (dev)
  645. ifr.ifr_ifindex = dev->ifindex;
  646. else
  647. ifr.ifr_data = data;
  648. strncpy(ifr.ifr_name, bridge, sizeof(ifr.ifr_name) - 1);
  649. return ioctl(sock_ioctl, cmd, &ifr);
  650. }
  651. static bool system_is_bridge(const char *name)
  652. {
  653. struct stat st;
  654. return stat(dev_sysfs_path(name, "bridge"), &st) >= 0;
  655. }
  656. static char *system_get_bridge(const char *name, char *buf, int buflen)
  657. {
  658. char *path;
  659. ssize_t len = -1;
  660. glob_t gl;
  661. snprintf(buf, buflen, "%s/devices/virtual/net/*/brif/%s/bridge", sysfs_path, name);
  662. if (glob(buf, GLOB_NOSORT, NULL, &gl) < 0)
  663. return NULL;
  664. if (gl.gl_pathc > 0)
  665. len = readlink(gl.gl_pathv[0], buf, buflen);
  666. globfree(&gl);
  667. if (len < 0)
  668. return NULL;
  669. buf[len] = 0;
  670. path = strrchr(buf, '/');
  671. if (!path)
  672. return NULL;
  673. return path + 1;
  674. }
  675. static void
  676. system_bridge_set_wireless(struct device *bridge, struct device *dev)
  677. {
  678. bool mcast_to_ucast = dev->wireless_ap;
  679. bool hairpin;
  680. if (bridge->settings.flags & DEV_OPT_MULTICAST_TO_UNICAST &&
  681. !bridge->settings.multicast_to_unicast)
  682. mcast_to_ucast = false;
  683. hairpin = mcast_to_ucast || dev->wireless_proxyarp;
  684. if (dev->wireless_isolate)
  685. hairpin = false;
  686. system_bridge_set_multicast_to_unicast(dev, mcast_to_ucast ? "1" : "0");
  687. system_bridge_set_hairpin_mode(dev, hairpin ? "1" : "0");
  688. system_bridge_set_proxyarp_wifi(dev, dev->wireless_proxyarp ? "1" : "0");
  689. }
  690. int system_bridge_addif(struct device *bridge, struct device *dev)
  691. {
  692. char buf[64];
  693. char *oldbr;
  694. int tries = 0;
  695. int ret;
  696. retry:
  697. ret = 0;
  698. oldbr = system_get_bridge(dev->ifname, dev_buf, sizeof(dev_buf));
  699. if (!oldbr || strcmp(oldbr, bridge->ifname) != 0) {
  700. ret = system_bridge_if(bridge->ifname, dev, SIOCBRADDIF, NULL);
  701. tries++;
  702. D(SYSTEM, "Failed to add device '%s' to bridge '%s' (tries=%d): %s\n",
  703. dev->ifname, bridge->ifname, tries, strerror(errno));
  704. if (tries <= 3)
  705. goto retry;
  706. }
  707. if (dev->wireless)
  708. system_bridge_set_wireless(bridge, dev);
  709. if (dev->settings.flags & DEV_OPT_MULTICAST_ROUTER) {
  710. snprintf(buf, sizeof(buf), "%u", dev->settings.multicast_router);
  711. system_bridge_set_multicast_router(dev, buf);
  712. }
  713. if (dev->settings.flags & DEV_OPT_MULTICAST_FAST_LEAVE &&
  714. dev->settings.multicast_fast_leave)
  715. system_bridge_set_multicast_fast_leave(dev, "1");
  716. if (dev->settings.flags & DEV_OPT_LEARNING &&
  717. !dev->settings.learning)
  718. system_bridge_set_learning(dev, "0");
  719. if (dev->settings.flags & DEV_OPT_UNICAST_FLOOD &&
  720. !dev->settings.unicast_flood)
  721. system_bridge_set_unicast_flood(dev, "0");
  722. if (dev->settings.flags & DEV_OPT_ISOLATE &&
  723. dev->settings.isolate)
  724. system_bridge_set_isolated(dev, "1");
  725. if (dev->bpdu_filter)
  726. system_bridge_set_bpdu_filter(dev, dev->bpdu_filter ? "1" : "0");
  727. return ret;
  728. }
  729. int system_bridge_delif(struct device *bridge, struct device *dev)
  730. {
  731. return system_bridge_if(bridge->ifname, dev, SIOCBRDELIF, NULL);
  732. }
  733. int system_bridge_vlan(const char *iface, uint16_t vid, bool add, unsigned int vflags)
  734. {
  735. struct bridge_vlan_info vinfo = { .vid = vid, };
  736. unsigned short flags = 0;
  737. struct nlattr *afspec;
  738. struct nl_msg *nlm;
  739. int index;
  740. int ret = 0;
  741. index = if_nametoindex(iface);
  742. if (!index)
  743. return -1;
  744. nlm = __system_ifinfo_msg(PF_BRIDGE, index, NULL, add ? RTM_SETLINK : RTM_DELLINK, 0);
  745. if (!nlm)
  746. return -1;
  747. if (vflags & BRVLAN_F_SELF)
  748. flags |= BRIDGE_FLAGS_SELF;
  749. if (vflags & BRVLAN_F_PVID)
  750. vinfo.flags |= BRIDGE_VLAN_INFO_PVID;
  751. if (vflags & BRVLAN_F_UNTAGGED)
  752. vinfo.flags |= BRIDGE_VLAN_INFO_UNTAGGED;
  753. afspec = nla_nest_start(nlm, IFLA_AF_SPEC);
  754. if (!afspec) {
  755. ret = -ENOMEM;
  756. goto failure;
  757. }
  758. if (flags)
  759. nla_put_u16(nlm, IFLA_BRIDGE_FLAGS, flags);
  760. nla_put(nlm, IFLA_BRIDGE_VLAN_INFO, sizeof(vinfo), &vinfo);
  761. nla_nest_end(nlm, afspec);
  762. return system_rtnl_call(nlm);
  763. failure:
  764. nlmsg_free(nlm);
  765. return ret;
  766. }
  767. int system_bonding_set_device(struct device *dev, struct bonding_config *cfg)
  768. {
  769. const char *ifname = dev->ifname;
  770. struct blob_attr *cur;
  771. char op = cfg ? '+' : '-';
  772. char buf[64];
  773. int rem;
  774. snprintf(dev_buf, sizeof(dev_buf), "%s/class/net/bonding_masters", sysfs_path);
  775. snprintf(buf, sizeof(buf), "%c%s", op, ifname);
  776. write_file(dev_buf, buf);
  777. if (!cfg)
  778. return 0;
  779. system_set_dev_sysfs("bonding/mode", ifname, bonding_policy_str[cfg->policy]);
  780. system_set_dev_sysfs_int("bonding/all_ports_active", ifname, cfg->all_ports_active);
  781. if (cfg->policy == BONDING_MODE_BALANCE_XOR ||
  782. cfg->policy == BONDING_MODE_BALANCE_TLB ||
  783. cfg->policy == BONDING_MODE_8023AD)
  784. system_set_dev_sysfs("bonding/xmit_hash_policy", ifname, cfg->xmit_hash_policy);
  785. if (cfg->policy == BONDING_MODE_8023AD) {
  786. system_set_dev_sysfs("bonding/ad_actor_system", ifname, cfg->ad_actor_system);
  787. system_set_dev_sysfs_int("bonding/ad_actor_sys_prio", ifname, cfg->ad_actor_sys_prio);
  788. system_set_dev_sysfs("bonding/ad_select", ifname, cfg->ad_select);
  789. system_set_dev_sysfs("bonding/lacp_rate", ifname, cfg->lacp_rate);
  790. system_set_dev_sysfs_int("bonding/min_links", ifname, cfg->min_links);
  791. }
  792. if (cfg->policy == BONDING_MODE_BALANCE_RR)
  793. system_set_dev_sysfs_int("bonding/packets_per_slave", ifname, cfg->packets_per_port);
  794. if (cfg->policy == BONDING_MODE_BALANCE_TLB ||
  795. cfg->policy == BONDING_MODE_BALANCE_ALB)
  796. system_set_dev_sysfs_int("bonding/lp_interval", ifname, cfg->lp_interval);
  797. if (cfg->policy == BONDING_MODE_BALANCE_TLB)
  798. system_set_dev_sysfs_int("bonding/tlb_dynamic_lb", ifname, cfg->dynamic_lb);
  799. system_set_dev_sysfs_int("bonding/resend_igmp", ifname, cfg->resend_igmp);
  800. system_set_dev_sysfs_int("bonding/num_grat_arp", ifname, cfg->num_peer_notif);
  801. system_set_dev_sysfs("bonding/primary_reselect", ifname, cfg->primary_reselect);
  802. system_set_dev_sysfs("bonding/fail_over_mac", ifname, cfg->failover_mac);
  803. system_set_dev_sysfs_int((cfg->monitor_arp ?
  804. "bonding/arp_interval" :
  805. "bonding/miimon"), ifname, cfg->monitor_interval);
  806. blobmsg_for_each_attr(cur, cfg->arp_target, rem) {
  807. snprintf(buf, sizeof(buf), "+%s", blobmsg_get_string(cur));
  808. system_set_dev_sysfs("bonding/arp_ip_target", ifname, buf);
  809. }
  810. system_set_dev_sysfs_int("bonding/arp_all_targets", ifname, cfg->arp_all_targets);
  811. if (cfg->policy < BONDING_MODE_8023AD)
  812. system_set_dev_sysfs("bonding/arp_validate", ifname, cfg->arp_validate);
  813. system_set_dev_sysfs_int("bonding/use_carrier", ifname, cfg->use_carrier);
  814. if (!cfg->monitor_arp && cfg->monitor_interval) {
  815. system_set_dev_sysfs_int("bonding/updelay", ifname, cfg->updelay);
  816. system_set_dev_sysfs_int("bonding/downdelay", ifname, cfg->downdelay);
  817. }
  818. return 0;
  819. }
  820. int system_bonding_set_port(struct device *dev, struct device *port, bool add, bool primary)
  821. {
  822. const char *port_name = port->ifname;
  823. const char op_ch = add ? '+' : '-';
  824. char buf[IFNAMSIZ + 1];
  825. snprintf(buf, sizeof(buf), "%c%s", op_ch, port_name);
  826. system_if_down(port);
  827. system_set_dev_sysfs("bonding/slaves", dev->ifname, buf);
  828. system_if_up(port);
  829. if (primary)
  830. system_set_dev_sysfs("bonding/primary", dev->ifname,
  831. add ? port_name : "");
  832. return 0;
  833. }
  834. int system_if_resolve(struct device *dev)
  835. {
  836. struct ifreq ifr;
  837. memset(&ifr, 0, sizeof(ifr));
  838. strncpy(ifr.ifr_name, dev->ifname, sizeof(ifr.ifr_name) - 1);
  839. if (!ioctl(sock_ioctl, SIOCGIFINDEX, &ifr))
  840. return ifr.ifr_ifindex;
  841. else
  842. return 0;
  843. }
  844. static int system_if_flags(const char *ifname, unsigned add, unsigned rem)
  845. {
  846. struct ifreq ifr;
  847. memset(&ifr, 0, sizeof(ifr));
  848. strncpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name) - 1);
  849. if (ioctl(sock_ioctl, SIOCGIFFLAGS, &ifr) < 0)
  850. return -1;
  851. ifr.ifr_flags |= add;
  852. ifr.ifr_flags &= ~rem;
  853. return ioctl(sock_ioctl, SIOCSIFFLAGS, &ifr);
  854. }
  855. struct clear_data {
  856. struct nl_msg *msg;
  857. struct device *dev;
  858. int type;
  859. int size;
  860. int af;
  861. };
  862. static bool check_ifaddr(struct nlmsghdr *hdr, int ifindex)
  863. {
  864. struct ifaddrmsg *ifa = NLMSG_DATA(hdr);
  865. return ifa->ifa_index == ifindex;
  866. }
  867. static bool check_route(struct nlmsghdr *hdr, int ifindex)
  868. {
  869. struct rtmsg *r = NLMSG_DATA(hdr);
  870. struct nlattr *tb[__RTA_MAX];
  871. if (r->rtm_protocol == RTPROT_KERNEL &&
  872. r->rtm_family == AF_INET6)
  873. return false;
  874. nlmsg_parse(hdr, sizeof(struct rtmsg), tb, __RTA_MAX - 1, NULL);
  875. if (!tb[RTA_OIF])
  876. return false;
  877. return *(int *)RTA_DATA(tb[RTA_OIF]) == ifindex;
  878. }
  879. static bool check_rule(struct nlmsghdr *hdr, int ifindex)
  880. {
  881. return true;
  882. }
  883. static int cb_clear_event(struct nl_msg *msg, void *arg)
  884. {
  885. struct clear_data *clr = arg;
  886. struct nlmsghdr *hdr = nlmsg_hdr(msg);
  887. bool (*cb)(struct nlmsghdr *, int ifindex);
  888. int type, ret;
  889. switch(clr->type) {
  890. case RTM_GETADDR:
  891. type = RTM_DELADDR;
  892. if (hdr->nlmsg_type != RTM_NEWADDR)
  893. return NL_SKIP;
  894. cb = check_ifaddr;
  895. break;
  896. case RTM_GETROUTE:
  897. type = RTM_DELROUTE;
  898. if (hdr->nlmsg_type != RTM_NEWROUTE)
  899. return NL_SKIP;
  900. cb = check_route;
  901. break;
  902. case RTM_GETRULE:
  903. type = RTM_DELRULE;
  904. if (hdr->nlmsg_type != RTM_NEWRULE)
  905. return NL_SKIP;
  906. cb = check_rule;
  907. break;
  908. default:
  909. return NL_SKIP;
  910. }
  911. if (!cb(hdr, clr->dev ? clr->dev->ifindex : 0))
  912. return NL_SKIP;
  913. if (type == RTM_DELRULE)
  914. D(SYSTEM, "Remove a rule\n");
  915. else
  916. D(SYSTEM, "Remove %s from device %s\n",
  917. type == RTM_DELADDR ? "an address" : "a route",
  918. clr->dev->ifname);
  919. memcpy(nlmsg_hdr(clr->msg), hdr, hdr->nlmsg_len);
  920. hdr = nlmsg_hdr(clr->msg);
  921. hdr->nlmsg_type = type;
  922. hdr->nlmsg_flags = NLM_F_REQUEST;
  923. nl_socket_disable_auto_ack(sock_rtnl);
  924. ret = nl_send_auto_complete(sock_rtnl, clr->msg);
  925. if (ret < 0) {
  926. if (type == RTM_DELRULE)
  927. D(SYSTEM, "Error deleting a rule: %d\n", ret);
  928. else
  929. D(SYSTEM, "Error deleting %s from device '%s': %d\n",
  930. type == RTM_DELADDR ? "an address" : "a route",
  931. clr->dev->ifname, ret);
  932. }
  933. nl_socket_enable_auto_ack(sock_rtnl);
  934. return NL_SKIP;
  935. }
  936. static int
  937. cb_finish_event(struct nl_msg *msg, void *arg)
  938. {
  939. int *pending = arg;
  940. *pending = 0;
  941. return NL_STOP;
  942. }
  943. static int
  944. error_handler(struct sockaddr_nl *nla, struct nlmsgerr *err, void *arg)
  945. {
  946. int *pending = arg;
  947. *pending = err->error;
  948. return NL_STOP;
  949. }
  950. static void
  951. system_if_clear_entries(struct device *dev, int type, int af)
  952. {
  953. struct clear_data clr;
  954. struct nl_cb *cb;
  955. struct rtmsg rtm = {
  956. .rtm_family = af,
  957. .rtm_flags = RTM_F_CLONED,
  958. };
  959. int flags = NLM_F_DUMP;
  960. int pending = 1;
  961. clr.af = af;
  962. clr.dev = dev;
  963. clr.type = type;
  964. switch (type) {
  965. case RTM_GETADDR:
  966. case RTM_GETRULE:
  967. clr.size = sizeof(struct rtgenmsg);
  968. break;
  969. case RTM_GETROUTE:
  970. clr.size = sizeof(struct rtmsg);
  971. break;
  972. default:
  973. return;
  974. }
  975. cb = nl_cb_alloc(NL_CB_DEFAULT);
  976. if (!cb)
  977. return;
  978. clr.msg = nlmsg_alloc_simple(type, flags);
  979. if (!clr.msg)
  980. goto out;
  981. nlmsg_append(clr.msg, &rtm, clr.size, 0);
  982. nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, cb_clear_event, &clr);
  983. nl_cb_set(cb, NL_CB_FINISH, NL_CB_CUSTOM, cb_finish_event, &pending);
  984. nl_cb_err(cb, NL_CB_CUSTOM, error_handler, &pending);
  985. if (nl_send_auto_complete(sock_rtnl, clr.msg) < 0)
  986. goto free;
  987. while (pending > 0)
  988. nl_recvmsgs(sock_rtnl, cb);
  989. free:
  990. nlmsg_free(clr.msg);
  991. out:
  992. nl_cb_put(cb);
  993. }
  994. /*
  995. * Clear bridge (membership) state and bring down device
  996. */
  997. void system_if_clear_state(struct device *dev)
  998. {
  999. static char buf[256];
  1000. char *bridge;
  1001. device_set_ifindex(dev, system_if_resolve(dev));
  1002. if (dev->external || !dev->ifindex)
  1003. return;
  1004. system_if_flags(dev->ifname, 0, IFF_UP);
  1005. if (system_is_bridge(dev->ifname)) {
  1006. D(SYSTEM, "Delete existing bridge named '%s'\n", dev->ifname);
  1007. system_bridge_delbr(dev);
  1008. return;
  1009. }
  1010. bridge = system_get_bridge(dev->ifname, buf, sizeof(buf));
  1011. if (bridge) {
  1012. D(SYSTEM, "Remove device '%s' from bridge '%s'\n", dev->ifname, bridge);
  1013. system_bridge_if(bridge, dev, SIOCBRDELIF, NULL);
  1014. }
  1015. system_if_clear_entries(dev, RTM_GETROUTE, AF_INET);
  1016. system_if_clear_entries(dev, RTM_GETADDR, AF_INET);
  1017. system_if_clear_entries(dev, RTM_GETROUTE, AF_INET6);
  1018. system_if_clear_entries(dev, RTM_GETADDR, AF_INET6);
  1019. system_if_clear_entries(dev, RTM_GETNEIGH, AF_INET);
  1020. system_if_clear_entries(dev, RTM_GETNEIGH, AF_INET6);
  1021. system_set_disable_ipv6(dev, "0");
  1022. }
  1023. static inline unsigned long
  1024. sec_to_jiffies(int val)
  1025. {
  1026. return (unsigned long) val * 100;
  1027. }
  1028. int system_bridge_addbr(struct device *bridge, struct bridge_config *cfg)
  1029. {
  1030. struct nlattr *linkinfo, *data;
  1031. struct nl_msg *msg;
  1032. uint64_t val;
  1033. int rv;
  1034. msg = system_ifinfo_msg(bridge->ifname, RTM_NEWLINK, NLM_F_CREATE | NLM_F_EXCL);
  1035. if (!msg)
  1036. return -1;
  1037. if (!(linkinfo = nla_nest_start(msg, IFLA_LINKINFO)))
  1038. goto nla_put_failure;
  1039. nla_put_string(msg, IFLA_INFO_KIND, "bridge");
  1040. if (!(data = nla_nest_start(msg, IFLA_INFO_DATA)))
  1041. goto nla_put_failure;
  1042. nla_put_u32(msg, IFLA_BR_STP_STATE, cfg->stp);
  1043. nla_put_u32(msg, IFLA_BR_FORWARD_DELAY, sec_to_jiffies(cfg->forward_delay));
  1044. nla_put_u8(msg, IFLA_BR_MCAST_SNOOPING, !!cfg->igmp_snoop);
  1045. nla_put_u8(msg, IFLA_BR_MCAST_QUERIER, !!cfg->multicast_querier);
  1046. nla_put_u32(msg, IFLA_BR_MCAST_HASH_MAX, cfg->hash_max);
  1047. if (bridge->settings.flags & DEV_OPT_MULTICAST_ROUTER)
  1048. nla_put_u8(msg, IFLA_BR_MCAST_ROUTER, !!bridge->settings.multicast_router);
  1049. if (cfg->flags & BRIDGE_OPT_ROBUSTNESS) {
  1050. nla_put_u32(msg, IFLA_BR_MCAST_STARTUP_QUERY_CNT, cfg->robustness);
  1051. nla_put_u32(msg, IFLA_BR_MCAST_LAST_MEMBER_CNT, cfg->robustness);
  1052. }
  1053. if (cfg->flags & BRIDGE_OPT_QUERY_INTERVAL)
  1054. nla_put_u64(msg, IFLA_BR_MCAST_QUERY_INTVL, cfg->query_interval);
  1055. if (cfg->flags & BRIDGE_OPT_QUERY_RESPONSE_INTERVAL)
  1056. nla_put_u64(msg, IFLA_BR_MCAST_QUERY_RESPONSE_INTVL, cfg->query_response_interval);
  1057. if (cfg->flags & BRIDGE_OPT_LAST_MEMBER_INTERVAL)
  1058. nla_put_u64(msg, IFLA_BR_MCAST_LAST_MEMBER_INTVL, cfg->last_member_interval);
  1059. if (cfg->flags & BRIDGE_OPT_ROBUSTNESS ||
  1060. cfg->flags & BRIDGE_OPT_QUERY_INTERVAL ||
  1061. cfg->flags & BRIDGE_OPT_QUERY_RESPONSE_INTERVAL) {
  1062. val = cfg->robustness * cfg->query_interval +
  1063. cfg->query_response_interval;
  1064. nla_put_u64(msg, IFLA_BR_MCAST_MEMBERSHIP_INTVL, val);
  1065. val -= cfg->query_response_interval / 2;
  1066. nla_put_u64(msg, IFLA_BR_MCAST_QUERIER_INTVL, val);
  1067. }
  1068. if (cfg->flags & BRIDGE_OPT_QUERY_INTERVAL) {
  1069. val = cfg->query_interval / 4;
  1070. nla_put_u64(msg, IFLA_BR_MCAST_STARTUP_QUERY_INTVL, val);
  1071. }
  1072. nla_put_u8(msg, IFLA_BR_VLAN_FILTERING, !!cfg->vlan_filtering);
  1073. nla_put_u16(msg, IFLA_BR_PRIORITY, cfg->priority);
  1074. nla_put_u32(msg, IFLA_BR_HELLO_TIME, sec_to_jiffies(cfg->hello_time));
  1075. nla_put_u32(msg, IFLA_BR_MAX_AGE, sec_to_jiffies(cfg->max_age));
  1076. if (cfg->flags & BRIDGE_OPT_AGEING_TIME)
  1077. nla_put_u32(msg, IFLA_BR_AGEING_TIME, sec_to_jiffies(cfg->ageing_time));
  1078. nla_nest_end(msg, data);
  1079. nla_nest_end(msg, linkinfo);
  1080. rv = system_rtnl_call(msg);
  1081. if (rv)
  1082. D(SYSTEM, "Error adding bridge '%s': %d\n", bridge->ifname, rv);
  1083. return rv;
  1084. nla_put_failure:
  1085. nlmsg_free(msg);
  1086. return -ENOMEM;
  1087. }
  1088. int system_macvlan_add(struct device *macvlan, struct device *dev, struct macvlan_config *cfg)
  1089. {
  1090. struct nl_msg *msg;
  1091. struct nlattr *linkinfo, *data;
  1092. int i, rv;
  1093. static const struct {
  1094. const char *name;
  1095. enum macvlan_mode val;
  1096. } modes[] = {
  1097. { "private", MACVLAN_MODE_PRIVATE },
  1098. { "vepa", MACVLAN_MODE_VEPA },
  1099. { "bridge", MACVLAN_MODE_BRIDGE },
  1100. { "passthru", MACVLAN_MODE_PASSTHRU },
  1101. };
  1102. msg = system_ifinfo_msg(macvlan->ifname, RTM_NEWLINK, NLM_F_CREATE | NLM_F_EXCL);
  1103. if (!msg)
  1104. return -1;
  1105. if (cfg->flags & MACVLAN_OPT_MACADDR)
  1106. nla_put(msg, IFLA_ADDRESS, sizeof(cfg->macaddr), cfg->macaddr);
  1107. nla_put_u32(msg, IFLA_LINK, dev->ifindex);
  1108. if (!(linkinfo = nla_nest_start(msg, IFLA_LINKINFO)))
  1109. goto nla_put_failure;
  1110. nla_put_string(msg, IFLA_INFO_KIND, "macvlan");
  1111. if (!(data = nla_nest_start(msg, IFLA_INFO_DATA)))
  1112. goto nla_put_failure;
  1113. if (cfg->mode) {
  1114. for (i = 0; i < ARRAY_SIZE(modes); i++) {
  1115. if (strcmp(cfg->mode, modes[i].name) != 0)
  1116. continue;
  1117. nla_put_u32(msg, IFLA_MACVLAN_MODE, modes[i].val);
  1118. break;
  1119. }
  1120. }
  1121. nla_nest_end(msg, data);
  1122. nla_nest_end(msg, linkinfo);
  1123. rv = system_rtnl_call(msg);
  1124. if (rv)
  1125. D(SYSTEM, "Error adding macvlan '%s' over '%s': %d\n", macvlan->ifname, dev->ifname, rv);
  1126. return rv;
  1127. nla_put_failure:
  1128. nlmsg_free(msg);
  1129. return -ENOMEM;
  1130. }
  1131. int system_link_netns_move(struct device *dev, int netns_fd, const char *target_ifname)
  1132. {
  1133. struct nl_msg *msg;
  1134. int index;
  1135. if (!dev)
  1136. return -1;
  1137. index = system_if_resolve(dev);
  1138. msg = __system_ifinfo_msg(AF_UNSPEC, index, target_ifname, RTM_NEWLINK, 0);
  1139. if (!msg)
  1140. return -1;
  1141. nla_put_u32(msg, IFLA_NET_NS_FD, netns_fd);
  1142. return system_rtnl_call(msg);
  1143. }
  1144. int system_macvlan_del(struct device *macvlan)
  1145. {
  1146. return system_link_del(macvlan->ifname);
  1147. }
  1148. int system_netns_open(const pid_t target_ns)
  1149. {
  1150. char pid_net_path[PATH_MAX];
  1151. snprintf(pid_net_path, sizeof(pid_net_path), "/proc/%u/ns/net", target_ns);
  1152. return open(pid_net_path, O_RDONLY);
  1153. }
  1154. int system_netns_set(int netns_fd)
  1155. {
  1156. return setns(netns_fd, CLONE_NEWNET);
  1157. }
  1158. int system_veth_add(struct device *veth, struct veth_config *cfg)
  1159. {
  1160. struct nl_msg *msg;
  1161. struct ifinfomsg empty_iim = {};
  1162. struct nlattr *linkinfo, *data, *veth_info;
  1163. int rv;
  1164. msg = nlmsg_alloc_simple(RTM_NEWLINK, NLM_F_REQUEST | NLM_F_CREATE | NLM_F_EXCL);
  1165. if (!msg)
  1166. return -1;
  1167. nlmsg_append(msg, &empty_iim, sizeof(empty_iim), 0);
  1168. if (cfg->flags & VETH_OPT_MACADDR)
  1169. nla_put(msg, IFLA_ADDRESS, sizeof(cfg->macaddr), cfg->macaddr);
  1170. nla_put_string(msg, IFLA_IFNAME, veth->ifname);
  1171. if (!(linkinfo = nla_nest_start(msg, IFLA_LINKINFO)))
  1172. goto nla_put_failure;
  1173. nla_put_string(msg, IFLA_INFO_KIND, "veth");
  1174. if (!(data = nla_nest_start(msg, IFLA_INFO_DATA)))
  1175. goto nla_put_failure;
  1176. if (!(veth_info = nla_nest_start(msg, VETH_INFO_PEER)))
  1177. goto nla_put_failure;
  1178. nlmsg_append(msg, &empty_iim, sizeof(empty_iim), 0);
  1179. if (cfg->flags & VETH_OPT_PEER_NAME)
  1180. nla_put_string(msg, IFLA_IFNAME, cfg->peer_name);
  1181. if (cfg->flags & VETH_OPT_PEER_MACADDR)
  1182. nla_put(msg, IFLA_ADDRESS, sizeof(cfg->peer_macaddr), cfg->peer_macaddr);
  1183. nla_nest_end(msg, veth_info);
  1184. nla_nest_end(msg, data);
  1185. nla_nest_end(msg, linkinfo);
  1186. rv = system_rtnl_call(msg);
  1187. if (rv) {
  1188. if (cfg->flags & VETH_OPT_PEER_NAME)
  1189. D(SYSTEM, "Error adding veth '%s' with peer '%s': %d\n", veth->ifname, cfg->peer_name, rv);
  1190. else
  1191. D(SYSTEM, "Error adding veth '%s': %d\n", veth->ifname, rv);
  1192. }
  1193. return rv;
  1194. nla_put_failure:
  1195. nlmsg_free(msg);
  1196. return -ENOMEM;
  1197. }
  1198. int system_veth_del(struct device *veth)
  1199. {
  1200. return system_link_del(veth->ifname);
  1201. }
  1202. static int system_vlan(struct device *dev, int id)
  1203. {
  1204. struct vlan_ioctl_args ifr = {
  1205. .cmd = SET_VLAN_NAME_TYPE_CMD,
  1206. .u.name_type = VLAN_NAME_TYPE_RAW_PLUS_VID_NO_PAD,
  1207. };
  1208. if (ioctl(sock_ioctl, SIOCSIFVLAN, &ifr) < 0)
  1209. return -1;
  1210. if (id < 0) {
  1211. ifr.cmd = DEL_VLAN_CMD;
  1212. ifr.u.VID = 0;
  1213. } else {
  1214. ifr.cmd = ADD_VLAN_CMD;
  1215. ifr.u.VID = id;
  1216. }
  1217. strncpy(ifr.device1, dev->ifname, sizeof(ifr.device1));
  1218. return ioctl(sock_ioctl, SIOCSIFVLAN, &ifr);
  1219. }
  1220. int system_vlan_add(struct device *dev, int id)
  1221. {
  1222. return system_vlan(dev, id);
  1223. }
  1224. int system_vlan_del(struct device *dev)
  1225. {
  1226. return system_vlan(dev, -1);
  1227. }
  1228. int system_vlandev_add(struct device *vlandev, struct device *dev, struct vlandev_config *cfg)
  1229. {
  1230. struct nl_msg *msg;
  1231. struct nlattr *linkinfo, *data, *qos;
  1232. struct ifinfomsg iim = { .ifi_family = AF_UNSPEC };
  1233. struct vlan_qos_mapping *elem;
  1234. struct ifla_vlan_qos_mapping nl_qos_map;
  1235. int rv;
  1236. msg = nlmsg_alloc_simple(RTM_NEWLINK, NLM_F_REQUEST | NLM_F_CREATE | NLM_F_EXCL);
  1237. if (!msg)
  1238. return -1;
  1239. nlmsg_append(msg, &iim, sizeof(iim), 0);
  1240. nla_put_string(msg, IFLA_IFNAME, vlandev->ifname);
  1241. nla_put_u32(msg, IFLA_LINK, dev->ifindex);
  1242. if (!(linkinfo = nla_nest_start(msg, IFLA_LINKINFO)))
  1243. goto nla_put_failure;
  1244. nla_put_string(msg, IFLA_INFO_KIND, "vlan");
  1245. if (!(data = nla_nest_start(msg, IFLA_INFO_DATA)))
  1246. goto nla_put_failure;
  1247. nla_put_u16(msg, IFLA_VLAN_ID, cfg->vid);
  1248. #if LINUX_VERSION_CODE >= KERNEL_VERSION(3,10,0)
  1249. nla_put_u16(msg, IFLA_VLAN_PROTOCOL, htons(cfg->proto));
  1250. #else
  1251. if(cfg->proto == VLAN_PROTO_8021AD)
  1252. netifd_log_message(L_WARNING, "%s Your kernel is older than linux 3.10.0, 802.1ad is not supported defaulting to 802.1q", vlandev->type->name);
  1253. #endif
  1254. if (!(qos = nla_nest_start(msg, IFLA_VLAN_INGRESS_QOS)))
  1255. goto nla_put_failure;
  1256. vlist_simple_for_each_element(&cfg->ingress_qos_mapping_list, elem, node) {
  1257. nl_qos_map.from = elem->from;
  1258. nl_qos_map.to = elem->to;
  1259. nla_put(msg, IFLA_VLAN_QOS_MAPPING, sizeof(nl_qos_map), &nl_qos_map);
  1260. }
  1261. nla_nest_end(msg, qos);
  1262. if (!(qos = nla_nest_start(msg, IFLA_VLAN_EGRESS_QOS)))
  1263. goto nla_put_failure;
  1264. vlist_simple_for_each_element(&cfg->egress_qos_mapping_list, elem, node) {
  1265. nl_qos_map.from = elem->from;
  1266. nl_qos_map.to = elem->to;
  1267. nla_put(msg, IFLA_VLAN_QOS_MAPPING, sizeof(nl_qos_map), &nl_qos_map);
  1268. }
  1269. nla_nest_end(msg, qos);
  1270. nla_nest_end(msg, data);
  1271. nla_nest_end(msg, linkinfo);
  1272. rv = system_rtnl_call(msg);
  1273. if (rv)
  1274. D(SYSTEM, "Error adding vlandev '%s' over '%s': %d\n", vlandev->ifname, dev->ifname, rv);
  1275. return rv;
  1276. nla_put_failure:
  1277. nlmsg_free(msg);
  1278. return -ENOMEM;
  1279. }
  1280. int system_vlandev_del(struct device *vlandev)
  1281. {
  1282. return system_link_del(vlandev->ifname);
  1283. }
  1284. static void
  1285. system_set_ethtool_settings(struct device *dev, struct device_settings *s)
  1286. {
  1287. struct ethtool_cmd ecmd = {
  1288. .cmd = ETHTOOL_GSET,
  1289. };
  1290. struct ifreq ifr = {
  1291. .ifr_data = (caddr_t)&ecmd,
  1292. };
  1293. static const struct {
  1294. int speed;
  1295. uint8_t bit_half;
  1296. uint8_t bit_full;
  1297. } speed_mask[] = {
  1298. { 10, ETHTOOL_LINK_MODE_10baseT_Half_BIT, ETHTOOL_LINK_MODE_10baseT_Full_BIT },
  1299. { 100, ETHTOOL_LINK_MODE_100baseT_Half_BIT, ETHTOOL_LINK_MODE_100baseT_Full_BIT },
  1300. { 1000, ETHTOOL_LINK_MODE_1000baseT_Half_BIT, ETHTOOL_LINK_MODE_1000baseT_Full_BIT },
  1301. };
  1302. uint32_t adv;
  1303. int i;
  1304. strncpy(ifr.ifr_name, dev->ifname, sizeof(ifr.ifr_name) - 1);
  1305. if (ioctl(sock_ioctl, SIOCETHTOOL, &ifr) != 0)
  1306. return;
  1307. adv = ecmd.supported;
  1308. for (i = 0; i < ARRAY_SIZE(speed_mask); i++) {
  1309. if (s->flags & DEV_OPT_DUPLEX) {
  1310. int bit = s->duplex ? speed_mask[i].bit_half : speed_mask[i].bit_full;
  1311. adv &= ~(1 << bit);
  1312. }
  1313. if (!(s->flags & DEV_OPT_SPEED) ||
  1314. s->speed == speed_mask[i].speed)
  1315. continue;
  1316. adv &= ~(1 << speed_mask[i].bit_full);
  1317. adv &= ~(1 << speed_mask[i].bit_half);
  1318. }
  1319. if (ecmd.autoneg && ecmd.advertising == adv)
  1320. return;
  1321. ecmd.autoneg = 1;
  1322. ecmd.advertising = adv;
  1323. ecmd.cmd = ETHTOOL_SSET;
  1324. ioctl(sock_ioctl, SIOCETHTOOL, &ifr);
  1325. }
  1326. void
  1327. system_if_get_settings(struct device *dev, struct device_settings *s)
  1328. {
  1329. struct ifreq ifr;
  1330. char buf[10];
  1331. memset(&ifr, 0, sizeof(ifr));
  1332. strncpy(ifr.ifr_name, dev->ifname, sizeof(ifr.ifr_name) - 1);
  1333. if (ioctl(sock_ioctl, SIOCGIFMTU, &ifr) == 0) {
  1334. s->mtu = ifr.ifr_mtu;
  1335. s->flags |= DEV_OPT_MTU;
  1336. }
  1337. s->mtu6 = system_update_ipv6_mtu(dev, 0);
  1338. if (s->mtu6 > 0)
  1339. s->flags |= DEV_OPT_MTU6;
  1340. if (ioctl(sock_ioctl, SIOCGIFTXQLEN, &ifr) == 0) {
  1341. s->txqueuelen = ifr.ifr_qlen;
  1342. s->flags |= DEV_OPT_TXQUEUELEN;
  1343. }
  1344. if (ioctl(sock_ioctl, SIOCGIFHWADDR, &ifr) == 0) {
  1345. memcpy(s->macaddr, &ifr.ifr_hwaddr.sa_data, sizeof(s->macaddr));
  1346. s->flags |= DEV_OPT_MACADDR;
  1347. }
  1348. if (!system_get_disable_ipv6(dev, buf, sizeof(buf))) {
  1349. s->ipv6 = !strtoul(buf, NULL, 0);
  1350. s->flags |= DEV_OPT_IPV6;
  1351. }
  1352. if (!system_get_ip6segmentrouting(dev, buf, sizeof(buf))) {
  1353. s->ip6segmentrouting = strtoul(buf, NULL, 0);
  1354. s->flags |= DEV_OPT_IP6SEGMENTROUTING;
  1355. }
  1356. if (ioctl(sock_ioctl, SIOCGIFFLAGS, &ifr) == 0) {
  1357. s->promisc = ifr.ifr_flags & IFF_PROMISC;
  1358. s->flags |= DEV_OPT_PROMISC;
  1359. s->multicast = ifr.ifr_flags & IFF_MULTICAST;
  1360. s->flags |= DEV_OPT_MULTICAST;
  1361. }
  1362. if (!system_get_rpfilter(dev, buf, sizeof(buf))) {
  1363. s->rpfilter = strtoul(buf, NULL, 0);
  1364. s->flags |= DEV_OPT_RPFILTER;
  1365. }
  1366. if (!system_get_acceptlocal(dev, buf, sizeof(buf))) {
  1367. s->acceptlocal = strtoul(buf, NULL, 0);
  1368. s->flags |= DEV_OPT_ACCEPTLOCAL;
  1369. }
  1370. if (!system_get_igmpversion(dev, buf, sizeof(buf))) {
  1371. s->igmpversion = strtoul(buf, NULL, 0);
  1372. s->flags |= DEV_OPT_IGMPVERSION;
  1373. }
  1374. if (!system_get_mldversion(dev, buf, sizeof(buf))) {
  1375. s->mldversion = strtoul(buf, NULL, 0);
  1376. s->flags |= DEV_OPT_MLDVERSION;
  1377. }
  1378. if (!system_get_neigh4reachabletime(dev, buf, sizeof(buf))) {
  1379. s->neigh4reachabletime = strtoul(buf, NULL, 0);
  1380. s->flags |= DEV_OPT_NEIGHREACHABLETIME;
  1381. }
  1382. if (!system_get_neigh6reachabletime(dev, buf, sizeof(buf))) {
  1383. s->neigh6reachabletime = strtoul(buf, NULL, 0);
  1384. s->flags |= DEV_OPT_NEIGHREACHABLETIME;
  1385. }
  1386. if (!system_get_neigh4locktime(dev, buf, sizeof(buf))) {
  1387. s->neigh4locktime = strtol(buf, NULL, 0);
  1388. s->flags |= DEV_OPT_NEIGHLOCKTIME;
  1389. }
  1390. if (!system_get_neigh4gcstaletime(dev, buf, sizeof(buf))) {
  1391. s->neigh4gcstaletime = strtoul(buf, NULL, 0);
  1392. s->flags |= DEV_OPT_NEIGHGCSTALETIME;
  1393. }
  1394. if (!system_get_neigh6gcstaletime(dev, buf, sizeof(buf))) {
  1395. s->neigh6gcstaletime = strtoul(buf, NULL, 0);
  1396. s->flags |= DEV_OPT_NEIGHGCSTALETIME;
  1397. }
  1398. if (!system_get_dadtransmits(dev, buf, sizeof(buf))) {
  1399. s->dadtransmits = strtoul(buf, NULL, 0);
  1400. s->flags |= DEV_OPT_DADTRANSMITS;
  1401. }
  1402. if (!system_get_sendredirects(dev, buf, sizeof(buf))) {
  1403. s->sendredirects = strtoul(buf, NULL, 0);
  1404. s->flags |= DEV_OPT_SENDREDIRECTS;
  1405. }
  1406. if (!system_get_drop_v4_unicast_in_l2_multicast(dev, buf, sizeof(buf))) {
  1407. s->drop_v4_unicast_in_l2_multicast = strtoul(buf, NULL, 0);
  1408. s->flags |= DEV_OPT_DROP_V4_UNICAST_IN_L2_MULTICAST;
  1409. }
  1410. if (!system_get_drop_v6_unicast_in_l2_multicast(dev, buf, sizeof(buf))) {
  1411. s->drop_v6_unicast_in_l2_multicast = strtoul(buf, NULL, 0);
  1412. s->flags |= DEV_OPT_DROP_V6_UNICAST_IN_L2_MULTICAST;
  1413. }
  1414. if (!system_get_drop_gratuitous_arp(dev, buf, sizeof(buf))) {
  1415. s->drop_gratuitous_arp = strtoul(buf, NULL, 0);
  1416. s->flags |= DEV_OPT_DROP_GRATUITOUS_ARP;
  1417. }
  1418. if (!system_get_drop_unsolicited_na(dev, buf, sizeof(buf))) {
  1419. s->drop_unsolicited_na = strtoul(buf, NULL, 0);
  1420. s->flags |= DEV_OPT_DROP_UNSOLICITED_NA;
  1421. }
  1422. if (!system_get_arp_accept(dev, buf, sizeof(buf))) {
  1423. s->arp_accept = strtoul(buf, NULL, 0);
  1424. s->flags |= DEV_OPT_ARP_ACCEPT;
  1425. }
  1426. }
  1427. void
  1428. system_if_apply_settings(struct device *dev, struct device_settings *s, uint64_t apply_mask)
  1429. {
  1430. struct ifreq ifr;
  1431. char buf[12];
  1432. apply_mask &= s->flags;
  1433. memset(&ifr, 0, sizeof(ifr));
  1434. strncpy(ifr.ifr_name, dev->ifname, sizeof(ifr.ifr_name) - 1);
  1435. if (apply_mask & DEV_OPT_MTU) {
  1436. ifr.ifr_mtu = s->mtu;
  1437. if (ioctl(sock_ioctl, SIOCSIFMTU, &ifr) < 0)
  1438. s->flags &= ~DEV_OPT_MTU;
  1439. }
  1440. if (apply_mask & DEV_OPT_MTU6) {
  1441. system_update_ipv6_mtu(dev, s->mtu6);
  1442. }
  1443. if (apply_mask & DEV_OPT_TXQUEUELEN) {
  1444. ifr.ifr_qlen = s->txqueuelen;
  1445. if (ioctl(sock_ioctl, SIOCSIFTXQLEN, &ifr) < 0)
  1446. s->flags &= ~DEV_OPT_TXQUEUELEN;
  1447. }
  1448. if ((apply_mask & (DEV_OPT_MACADDR | DEV_OPT_DEFAULT_MACADDR)) && !dev->external) {
  1449. ifr.ifr_hwaddr.sa_family = ARPHRD_ETHER;
  1450. memcpy(&ifr.ifr_hwaddr.sa_data, s->macaddr, sizeof(s->macaddr));
  1451. if (ioctl(sock_ioctl, SIOCSIFHWADDR, &ifr) < 0)
  1452. s->flags &= ~DEV_OPT_MACADDR;
  1453. }
  1454. if (apply_mask & DEV_OPT_IPV6)
  1455. system_set_disable_ipv6(dev, s->ipv6 ? "0" : "1");
  1456. if (s->flags & DEV_OPT_IP6SEGMENTROUTING & apply_mask) {
  1457. struct device dummy = {
  1458. .ifname = "all",
  1459. };
  1460. bool ip6segmentrouting = device_check_ip6segmentrouting();
  1461. system_set_ip6segmentrouting(dev, s->ip6segmentrouting ? "1" : "0");
  1462. system_set_ip6segmentrouting(&dummy, ip6segmentrouting ? "1" : "0");
  1463. }
  1464. if (apply_mask & DEV_OPT_PROMISC) {
  1465. if (system_if_flags(dev->ifname, s->promisc ? IFF_PROMISC : 0,
  1466. !s->promisc ? IFF_PROMISC : 0) < 0)
  1467. s->flags &= ~DEV_OPT_PROMISC;
  1468. }
  1469. if (apply_mask & DEV_OPT_RPFILTER) {
  1470. snprintf(buf, sizeof(buf), "%u", s->rpfilter);
  1471. system_set_rpfilter(dev, buf);
  1472. }
  1473. if (apply_mask & DEV_OPT_ACCEPTLOCAL)
  1474. system_set_acceptlocal(dev, s->acceptlocal ? "1" : "0");
  1475. if (apply_mask & DEV_OPT_IGMPVERSION) {
  1476. snprintf(buf, sizeof(buf), "%u", s->igmpversion);
  1477. system_set_igmpversion(dev, buf);
  1478. }
  1479. if (apply_mask & DEV_OPT_MLDVERSION) {
  1480. snprintf(buf, sizeof(buf), "%u", s->mldversion);
  1481. system_set_mldversion(dev, buf);
  1482. }
  1483. if (apply_mask & DEV_OPT_NEIGHREACHABLETIME) {
  1484. snprintf(buf, sizeof(buf), "%u", s->neigh4reachabletime);
  1485. system_set_neigh4reachabletime(dev, buf);
  1486. snprintf(buf, sizeof(buf), "%u", s->neigh6reachabletime);
  1487. system_set_neigh6reachabletime(dev, buf);
  1488. }
  1489. if (apply_mask & DEV_OPT_NEIGHLOCKTIME) {
  1490. snprintf(buf, sizeof(buf), "%d", s->neigh4locktime);
  1491. system_set_neigh4locktime(dev, buf);
  1492. }
  1493. if (apply_mask & DEV_OPT_NEIGHGCSTALETIME) {
  1494. snprintf(buf, sizeof(buf), "%u", s->neigh4gcstaletime);
  1495. system_set_neigh4gcstaletime(dev, buf);
  1496. snprintf(buf, sizeof(buf), "%u", s->neigh6gcstaletime);
  1497. system_set_neigh6gcstaletime(dev, buf);
  1498. }
  1499. if (apply_mask & DEV_OPT_DADTRANSMITS) {
  1500. snprintf(buf, sizeof(buf), "%u", s->dadtransmits);
  1501. system_set_dadtransmits(dev, buf);
  1502. }
  1503. if (apply_mask & DEV_OPT_MULTICAST) {
  1504. if (system_if_flags(dev->ifname, s->multicast ? IFF_MULTICAST : 0,
  1505. !s->multicast ? IFF_MULTICAST : 0) < 0)
  1506. s->flags &= ~DEV_OPT_MULTICAST;
  1507. }
  1508. if (apply_mask & DEV_OPT_SENDREDIRECTS)
  1509. system_set_sendredirects(dev, s->sendredirects ? "1" : "0");
  1510. if (apply_mask & DEV_OPT_DROP_V4_UNICAST_IN_L2_MULTICAST)
  1511. system_set_drop_v4_unicast_in_l2_multicast(dev, s->drop_v4_unicast_in_l2_multicast ? "1" : "0");
  1512. if (apply_mask & DEV_OPT_DROP_V6_UNICAST_IN_L2_MULTICAST)
  1513. system_set_drop_v6_unicast_in_l2_multicast(dev, s->drop_v6_unicast_in_l2_multicast ? "1" : "0");
  1514. if (apply_mask & DEV_OPT_DROP_GRATUITOUS_ARP)
  1515. system_set_drop_gratuitous_arp(dev, s->drop_gratuitous_arp ? "1" : "0");
  1516. if (apply_mask & DEV_OPT_DROP_UNSOLICITED_NA)
  1517. system_set_drop_unsolicited_na(dev, s->drop_unsolicited_na ? "1" : "0");
  1518. if (apply_mask & DEV_OPT_ARP_ACCEPT)
  1519. system_set_arp_accept(dev, s->arp_accept ? "1" : "0");
  1520. system_set_ethtool_settings(dev, s);
  1521. }
  1522. int system_if_up(struct device *dev)
  1523. {
  1524. return system_if_flags(dev->ifname, IFF_UP, 0);
  1525. }
  1526. int system_if_down(struct device *dev)
  1527. {
  1528. return system_if_flags(dev->ifname, 0, IFF_UP);
  1529. }
  1530. struct if_check_data {
  1531. struct device *dev;
  1532. int pending;
  1533. int ret;
  1534. };
  1535. #ifndef IFF_LOWER_UP
  1536. #define IFF_LOWER_UP 0x10000
  1537. #endif
  1538. static int cb_if_check_valid(struct nl_msg *msg, void *arg)
  1539. {
  1540. struct nlmsghdr *nh = nlmsg_hdr(msg);
  1541. struct ifinfomsg *ifi = NLMSG_DATA(nh);
  1542. struct if_check_data *chk = (struct if_check_data *)arg;
  1543. if (nh->nlmsg_type != RTM_NEWLINK)
  1544. return NL_SKIP;
  1545. if (chk->dev->type == &simple_device_type)
  1546. device_set_present(chk->dev, ifi->ifi_index > 0 ? true : false);
  1547. device_set_link(chk->dev, ifi->ifi_flags & IFF_LOWER_UP ? true : false);
  1548. return NL_OK;
  1549. }
  1550. static int cb_if_check_ack(struct nl_msg *msg, void *arg)
  1551. {
  1552. struct if_check_data *chk = (struct if_check_data *)arg;
  1553. chk->pending = 0;
  1554. return NL_STOP;
  1555. }
  1556. static int cb_if_check_error(struct sockaddr_nl *nla, struct nlmsgerr *err, void *arg)
  1557. {
  1558. struct if_check_data *chk = (struct if_check_data *)arg;
  1559. if (chk->dev->type == &simple_device_type)
  1560. device_set_present(chk->dev, false);
  1561. device_set_link(chk->dev, false);
  1562. chk->pending = err->error;
  1563. return NL_STOP;
  1564. }
  1565. struct bridge_vlan_check_data {
  1566. struct device *check_dev;
  1567. int ifindex;
  1568. int ret;
  1569. bool pending;
  1570. };
  1571. static void bridge_vlan_check_port(struct bridge_vlan_check_data *data,
  1572. struct bridge_vlan_port *port,
  1573. struct bridge_vlan_info *vinfo)
  1574. {
  1575. uint16_t flags = 0, diff, mask;
  1576. if (port->flags & BRVLAN_F_PVID)
  1577. flags |= BRIDGE_VLAN_INFO_PVID;
  1578. if (port->flags & BRVLAN_F_UNTAGGED)
  1579. flags |= BRIDGE_VLAN_INFO_UNTAGGED;
  1580. diff = vinfo->flags ^ flags;
  1581. mask = BRVLAN_F_UNTAGGED | (flags & BRIDGE_VLAN_INFO_PVID);
  1582. if (diff & mask) {
  1583. data->ret = 1;
  1584. data->pending = false;
  1585. }
  1586. port->check = 1;
  1587. }
  1588. static void bridge_vlan_check_attr(struct bridge_vlan_check_data *data,
  1589. struct rtattr *attr)
  1590. {
  1591. struct bridge_vlan_hotplug_port *port;
  1592. struct bridge_vlan_info *vinfo;
  1593. struct bridge_vlan *vlan;
  1594. struct rtattr *cur;
  1595. int rem = RTA_PAYLOAD(attr);
  1596. int i;
  1597. for (cur = RTA_DATA(attr); RTA_OK(cur, rem); cur = RTA_NEXT(cur, rem)) {
  1598. if (cur->rta_type != IFLA_BRIDGE_VLAN_INFO)
  1599. continue;
  1600. vinfo = RTA_DATA(cur);
  1601. vlan = vlist_find(&data->check_dev->vlans, &vinfo->vid, vlan, node);
  1602. if (!vlan) {
  1603. data->ret = 1;
  1604. data->pending = false;
  1605. return;
  1606. }
  1607. for (i = 0; i < vlan->n_ports; i++)
  1608. if (!vlan->ports[i].check)
  1609. bridge_vlan_check_port(data, &vlan->ports[i], vinfo);
  1610. list_for_each_entry(port, &vlan->hotplug_ports, list)
  1611. if (!port->port.check)
  1612. bridge_vlan_check_port(data, &port->port, vinfo);
  1613. }
  1614. }
  1615. static int bridge_vlan_check_cb(struct nl_msg *msg, void *arg)
  1616. {
  1617. struct bridge_vlan_check_data *data = arg;
  1618. struct nlmsghdr *nh = nlmsg_hdr(msg);
  1619. struct ifinfomsg *ifi = NLMSG_DATA(nh);
  1620. struct rtattr *attr;
  1621. int rem;
  1622. if (nh->nlmsg_type != RTM_NEWLINK)
  1623. return NL_SKIP;
  1624. if (ifi->ifi_family != AF_BRIDGE)
  1625. return NL_SKIP;
  1626. if (ifi->ifi_index != data->ifindex)
  1627. return NL_SKIP;
  1628. attr = IFLA_RTA(ifi);
  1629. rem = nh->nlmsg_len - NLMSG_LENGTH(sizeof(*ifi));
  1630. while (RTA_OK(attr, rem)) {
  1631. if (attr->rta_type == IFLA_AF_SPEC)
  1632. bridge_vlan_check_attr(data, attr);
  1633. attr = RTA_NEXT(attr, rem);
  1634. }
  1635. return NL_SKIP;
  1636. }
  1637. static int bridge_vlan_ack_cb(struct nl_msg *msg, void *arg)
  1638. {
  1639. struct bridge_vlan_check_data *data = arg;
  1640. data->pending = false;
  1641. return NL_STOP;
  1642. }
  1643. static int bridge_vlan_error_cb(struct sockaddr_nl *nla, struct nlmsgerr *err, void *arg)
  1644. {
  1645. struct bridge_vlan_check_data *data = arg;
  1646. data->pending = false;
  1647. return NL_STOP;
  1648. }
  1649. int system_bridge_vlan_check(struct device *dev, char *ifname)
  1650. {
  1651. struct bridge_vlan_check_data data = {
  1652. .check_dev = dev,
  1653. .ifindex = if_nametoindex(ifname),
  1654. .ret = -1,
  1655. .pending = true,
  1656. };
  1657. static struct ifinfomsg ifi = {
  1658. .ifi_family = AF_BRIDGE
  1659. };
  1660. static struct rtattr ext_req = {
  1661. .rta_type = IFLA_EXT_MASK,
  1662. .rta_len = RTA_LENGTH(sizeof(uint32_t)),
  1663. };
  1664. uint32_t filter = RTEXT_FILTER_BRVLAN;
  1665. struct nl_cb *cb = nl_cb_alloc(NL_CB_DEFAULT);
  1666. struct bridge_vlan *vlan;
  1667. struct nl_msg *msg;
  1668. int i;
  1669. if (!data.ifindex)
  1670. return 0;
  1671. msg = nlmsg_alloc_simple(RTM_GETLINK, NLM_F_DUMP);
  1672. if (nlmsg_append(msg, &ifi, sizeof(ifi), 0) ||
  1673. nlmsg_append(msg, &ext_req, sizeof(ext_req), NLMSG_ALIGNTO) ||
  1674. nlmsg_append(msg, &filter, sizeof(filter), 0))
  1675. goto free;
  1676. vlist_for_each_element(&dev->vlans, vlan, node) {
  1677. struct bridge_vlan_hotplug_port *port;
  1678. for (i = 0; i < vlan->n_ports; i++) {
  1679. if (!strcmp(vlan->ports[i].ifname, ifname))
  1680. vlan->ports[i].check = 0;
  1681. else
  1682. vlan->ports[i].check = -1;
  1683. }
  1684. list_for_each_entry(port, &vlan->hotplug_ports, list) {
  1685. if (!strcmp(port->port.ifname, ifname))
  1686. port->port.check = 0;
  1687. else
  1688. port->port.check = -1;
  1689. }
  1690. }
  1691. nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, bridge_vlan_check_cb, &data);
  1692. nl_cb_set(cb, NL_CB_FINISH, NL_CB_CUSTOM, bridge_vlan_ack_cb, &data);
  1693. nl_cb_set(cb, NL_CB_ACK, NL_CB_CUSTOM, bridge_vlan_ack_cb, &data);
  1694. nl_cb_err(cb, NL_CB_CUSTOM, bridge_vlan_error_cb, &data);
  1695. if (nl_send_auto_complete(sock_rtnl, msg) < 0)
  1696. goto free;
  1697. data.ret = 0;
  1698. while (data.pending)
  1699. nl_recvmsgs(sock_rtnl, cb);
  1700. vlist_for_each_element(&dev->vlans, vlan, node) {
  1701. struct bridge_vlan_hotplug_port *port;
  1702. for (i = 0; i < vlan->n_ports; i++) {
  1703. if (!vlan->ports[i].check) {
  1704. data.ret = 1;
  1705. break;
  1706. }
  1707. }
  1708. list_for_each_entry(port, &vlan->hotplug_ports, list) {
  1709. if (!port->port.check) {
  1710. data.ret = 1;
  1711. break;
  1712. }
  1713. }
  1714. }
  1715. goto out;
  1716. free:
  1717. nlmsg_free(msg);
  1718. out:
  1719. nl_cb_put(cb);
  1720. return data.ret;
  1721. }
  1722. int system_if_check(struct device *dev)
  1723. {
  1724. struct nl_cb *cb = nl_cb_alloc(NL_CB_DEFAULT);
  1725. struct nl_msg *msg;
  1726. struct ifinfomsg ifi = {
  1727. .ifi_family = AF_UNSPEC,
  1728. .ifi_index = 0,
  1729. };
  1730. struct if_check_data chk = {
  1731. .dev = dev,
  1732. .pending = 1,
  1733. };
  1734. int ret = 1;
  1735. if (!cb)
  1736. return ret;
  1737. msg = nlmsg_alloc_simple(RTM_GETLINK, 0);
  1738. if (!msg)
  1739. goto out;
  1740. if (nlmsg_append(msg, &ifi, sizeof(ifi), 0) ||
  1741. nla_put_string(msg, IFLA_IFNAME, dev->ifname))
  1742. goto free;
  1743. nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, cb_if_check_valid, &chk);
  1744. nl_cb_set(cb, NL_CB_ACK, NL_CB_CUSTOM, cb_if_check_ack, &chk);
  1745. nl_cb_err(cb, NL_CB_CUSTOM, cb_if_check_error, &chk);
  1746. ret = nl_send_auto_complete(sock_rtnl, msg);
  1747. if (ret < 0)
  1748. goto free;
  1749. while (chk.pending > 0)
  1750. nl_recvmsgs(sock_rtnl, cb);
  1751. ret = chk.pending;
  1752. free:
  1753. nlmsg_free(msg);
  1754. out:
  1755. nl_cb_put(cb);
  1756. return ret;
  1757. }
  1758. struct device *
  1759. system_if_get_parent(struct device *dev)
  1760. {
  1761. char buf[64], *devname;
  1762. int ifindex, iflink;
  1763. if (system_get_dev_sysfs("iflink", dev->ifname, buf, sizeof(buf)) < 0)
  1764. return NULL;
  1765. iflink = strtoul(buf, NULL, 0);
  1766. ifindex = system_if_resolve(dev);
  1767. if (!iflink || iflink == ifindex)
  1768. return NULL;
  1769. devname = if_indextoname(iflink, buf);
  1770. if (!devname)
  1771. return NULL;
  1772. return device_get(devname, true);
  1773. }
  1774. static bool
  1775. read_string_file(int dir_fd, const char *file, char *buf, int len)
  1776. {
  1777. bool ret = false;
  1778. char *c;
  1779. int fd;
  1780. fd = openat(dir_fd, file, O_RDONLY);
  1781. if (fd < 0)
  1782. return false;
  1783. retry:
  1784. len = read(fd, buf, len - 1);
  1785. if (len < 0) {
  1786. if (errno == EINTR)
  1787. goto retry;
  1788. } else if (len > 0) {
  1789. buf[len] = 0;
  1790. c = strchr(buf, '\n');
  1791. if (c)
  1792. *c = 0;
  1793. ret = true;
  1794. }
  1795. close(fd);
  1796. return ret;
  1797. }
  1798. static bool
  1799. read_uint64_file(int dir_fd, const char *file, uint64_t *val)
  1800. {
  1801. char buf[64];
  1802. bool ret = false;
  1803. ret = read_string_file(dir_fd, file, buf, sizeof(buf));
  1804. if (ret)
  1805. *val = strtoull(buf, NULL, 0);
  1806. return ret;
  1807. }
  1808. /* Assume advertised flags == supported flags */
  1809. static const struct {
  1810. uint32_t mask;
  1811. const char *name;
  1812. } ethtool_link_modes[] = {
  1813. { ADVERTISED_10baseT_Half, "10baseT-H" },
  1814. { ADVERTISED_10baseT_Full, "10baseT-F" },
  1815. { ADVERTISED_100baseT_Half, "100baseT-H" },
  1816. { ADVERTISED_100baseT_Full, "100baseT-F" },
  1817. { ADVERTISED_1000baseT_Half, "1000baseT-H" },
  1818. { ADVERTISED_1000baseT_Full, "1000baseT-F" },
  1819. { ADVERTISED_1000baseKX_Full, "1000baseKX-F" },
  1820. { ADVERTISED_2500baseX_Full, "2500baseX-F" },
  1821. { ADVERTISED_10000baseT_Full, "10000baseT-F" },
  1822. { ADVERTISED_10000baseKX4_Full, "10000baseKX4-F" },
  1823. { ADVERTISED_10000baseKR_Full, "10000baseKR-F" },
  1824. { ADVERTISED_20000baseMLD2_Full, "20000baseMLD2-F" },
  1825. { ADVERTISED_20000baseKR2_Full, "20000baseKR2-F" },
  1826. { ADVERTISED_40000baseKR4_Full, "40000baseKR4-F" },
  1827. { ADVERTISED_40000baseCR4_Full, "40000baseCR4-F" },
  1828. { ADVERTISED_40000baseSR4_Full, "40000baseSR4-F" },
  1829. { ADVERTISED_40000baseLR4_Full, "40000baseLR4-F" },
  1830. #ifdef ADVERTISED_56000baseKR4_Full
  1831. { ADVERTISED_56000baseKR4_Full, "56000baseKR4-F" },
  1832. { ADVERTISED_56000baseCR4_Full, "56000baseCR4-F" },
  1833. { ADVERTISED_56000baseSR4_Full, "56000baseSR4-F" },
  1834. { ADVERTISED_56000baseLR4_Full, "56000baseLR4-F" },
  1835. #endif
  1836. };
  1837. static void system_add_link_modes(struct blob_buf *b, __u32 mask)
  1838. {
  1839. int i;
  1840. for (i = 0; i < ARRAY_SIZE(ethtool_link_modes); i++) {
  1841. if (mask & ethtool_link_modes[i].mask)
  1842. blobmsg_add_string(b, NULL, ethtool_link_modes[i].name);
  1843. }
  1844. }
  1845. bool
  1846. system_if_force_external(const char *ifname)
  1847. {
  1848. struct stat s;
  1849. return stat(dev_sysfs_path(ifname, "phy80211"), &s) == 0;
  1850. }
  1851. static const char *
  1852. system_netdevtype_name(unsigned short dev_type)
  1853. {
  1854. unsigned int i;
  1855. for (i = 0; i < ARRAY_SIZE(netdev_types); i++) {
  1856. if (netdev_types[i].id == dev_type)
  1857. return netdev_types[i].name;
  1858. }
  1859. /* the last key is used by default */
  1860. i = ARRAY_SIZE(netdev_types) - 1;
  1861. return netdev_types[i].name;
  1862. }
  1863. static void
  1864. system_add_devtype(struct blob_buf *b, const char *ifname)
  1865. {
  1866. char buf[100];
  1867. bool found = false;
  1868. if (!system_get_dev_sysfs("uevent", ifname, buf, sizeof(buf))) {
  1869. const char *info = "DEVTYPE=";
  1870. char *context = NULL;
  1871. const char *line = strtok_r(buf, "\r\n", &context);
  1872. while (line != NULL) {
  1873. char *index = strstr(line, info);
  1874. if (index != NULL) {
  1875. blobmsg_add_string(b, "devtype", index + strlen(info));
  1876. found = true;
  1877. break;
  1878. }
  1879. line = strtok_r(NULL, "\r\n", &context);
  1880. }
  1881. }
  1882. if (!found) {
  1883. unsigned short number = 0;
  1884. const char *name = NULL;
  1885. if (!system_get_dev_sysfs("type", ifname, buf, sizeof(buf))) {
  1886. number = strtoul(buf, NULL, 0);
  1887. name = system_netdevtype_name(number);
  1888. blobmsg_add_string(b, "devtype", name);
  1889. }
  1890. }
  1891. }
  1892. #define DIV_ROUND_UP(n,d) (((n) + (d) - 1) / (d))
  1893. static int32_t
  1894. ethtool_feature_count(const char *ifname)
  1895. {
  1896. struct {
  1897. struct ethtool_sset_info hdr;
  1898. uint32_t buf;
  1899. } req = {
  1900. .hdr = {
  1901. .cmd = ETHTOOL_GSSET_INFO,
  1902. .sset_mask = 1 << ETH_SS_FEATURES
  1903. }
  1904. };
  1905. struct ifreq ifr = {
  1906. .ifr_data = (void *)&req
  1907. };
  1908. strncpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name) - 1);
  1909. if (ioctl(sock_ioctl, SIOCETHTOOL, &ifr) != 0)
  1910. return -1;
  1911. if (!req.hdr.sset_mask)
  1912. return 0;
  1913. return req.buf;
  1914. }
  1915. static int32_t
  1916. ethtool_feature_index(const char *ifname, const char *keyname)
  1917. {
  1918. struct ethtool_gstrings *feature_names;
  1919. struct ifreq ifr = { 0 };
  1920. int32_t n_features, i;
  1921. n_features = ethtool_feature_count(ifname);
  1922. if (n_features <= 0)
  1923. return -1;
  1924. feature_names = calloc(1, sizeof(*feature_names) + n_features * ETH_GSTRING_LEN);
  1925. if (!feature_names)
  1926. return -1;
  1927. feature_names->cmd = ETHTOOL_GSTRINGS;
  1928. feature_names->string_set = ETH_SS_FEATURES;
  1929. feature_names->len = n_features;
  1930. strncpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name) - 1);
  1931. ifr.ifr_data = (void *)feature_names;
  1932. if (ioctl(sock_ioctl, SIOCETHTOOL, &ifr) != 0) {
  1933. free(feature_names);
  1934. return -1;
  1935. }
  1936. for (i = 0; i < feature_names->len; i++)
  1937. if (!strcmp((char *)&feature_names->data[i * ETH_GSTRING_LEN], keyname))
  1938. break;
  1939. if (i >= feature_names->len)
  1940. i = -1;
  1941. free(feature_names);
  1942. return i;
  1943. }
  1944. static bool
  1945. ethtool_feature_value(const char *ifname, const char *keyname)
  1946. {
  1947. struct ethtool_get_features_block *feature_block;
  1948. struct ethtool_gfeatures *feature_values;
  1949. struct ifreq ifr = { 0 };
  1950. int32_t feature_idx;
  1951. bool active;
  1952. feature_idx = ethtool_feature_index(ifname, keyname);
  1953. if (feature_idx < 0)
  1954. return false;
  1955. feature_values = calloc(1,
  1956. sizeof(*feature_values) +
  1957. sizeof(feature_values->features[0]) * DIV_ROUND_UP(feature_idx, 32));
  1958. if (!feature_values)
  1959. return false;
  1960. feature_values->cmd = ETHTOOL_GFEATURES;
  1961. feature_values->size = DIV_ROUND_UP(feature_idx, 32);
  1962. strncpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name) - 1);
  1963. ifr.ifr_data = (void *)feature_values;
  1964. if (ioctl(sock_ioctl, SIOCETHTOOL, &ifr) != 0) {
  1965. free(feature_values);
  1966. return false;
  1967. }
  1968. feature_block = &feature_values->features[feature_idx / 32];
  1969. active = feature_block->active & (1U << feature_idx % 32);
  1970. free(feature_values);
  1971. return active;
  1972. }
  1973. int
  1974. system_if_dump_info(struct device *dev, struct blob_buf *b)
  1975. {
  1976. struct ethtool_cmd ecmd;
  1977. struct ifreq ifr;
  1978. char *s;
  1979. void *c;
  1980. memset(&ecmd, 0, sizeof(ecmd));
  1981. memset(&ifr, 0, sizeof(ifr));
  1982. strncpy(ifr.ifr_name, dev->ifname, sizeof(ifr.ifr_name) - 1);
  1983. ifr.ifr_data = (caddr_t) &ecmd;
  1984. ecmd.cmd = ETHTOOL_GSET;
  1985. if (ioctl(sock_ioctl, SIOCETHTOOL, &ifr) == 0) {
  1986. c = blobmsg_open_array(b, "link-advertising");
  1987. system_add_link_modes(b, ecmd.advertising);
  1988. blobmsg_close_array(b, c);
  1989. c = blobmsg_open_array(b, "link-partner-advertising");
  1990. system_add_link_modes(b, ecmd.lp_advertising);
  1991. blobmsg_close_array(b, c);
  1992. c = blobmsg_open_array(b, "link-supported");
  1993. system_add_link_modes(b, ecmd.supported);
  1994. blobmsg_close_array(b, c);
  1995. s = blobmsg_alloc_string_buffer(b, "speed", 8);
  1996. snprintf(s, 8, "%d%c", ethtool_cmd_speed(&ecmd),
  1997. ecmd.duplex == DUPLEX_HALF ? 'H' : 'F');
  1998. blobmsg_add_string_buffer(b);
  1999. blobmsg_add_u8(b, "autoneg", !!ecmd.autoneg);
  2000. }
  2001. blobmsg_add_u8(b, "hw-tc-offload",
  2002. ethtool_feature_value(dev->ifname, "hw-tc-offload"));
  2003. system_add_devtype(b, dev->ifname);
  2004. return 0;
  2005. }
  2006. int
  2007. system_if_dump_stats(struct device *dev, struct blob_buf *b)
  2008. {
  2009. const char *const counters[] = {
  2010. "collisions", "rx_frame_errors", "tx_compressed",
  2011. "multicast", "rx_length_errors", "tx_dropped",
  2012. "rx_bytes", "rx_missed_errors", "tx_errors",
  2013. "rx_compressed", "rx_over_errors", "tx_fifo_errors",
  2014. "rx_crc_errors", "rx_packets", "tx_heartbeat_errors",
  2015. "rx_dropped", "tx_aborted_errors", "tx_packets",
  2016. "rx_errors", "tx_bytes", "tx_window_errors",
  2017. "rx_fifo_errors", "tx_carrier_errors",
  2018. };
  2019. int stats_dir;
  2020. int i;
  2021. uint64_t val = 0;
  2022. stats_dir = open(dev_sysfs_path(dev->ifname, "statistics"), O_DIRECTORY);
  2023. if (stats_dir < 0)
  2024. return -1;
  2025. for (i = 0; i < ARRAY_SIZE(counters); i++)
  2026. if (read_uint64_file(stats_dir, counters[i], &val))
  2027. blobmsg_add_u64(b, counters[i], val);
  2028. close(stats_dir);
  2029. return 0;
  2030. }
  2031. static int system_addr(struct device *dev, struct device_addr *addr, int cmd)
  2032. {
  2033. bool v4 = ((addr->flags & DEVADDR_FAMILY) == DEVADDR_INET4);
  2034. int alen = v4 ? 4 : 16;
  2035. unsigned int flags = 0;
  2036. struct ifaddrmsg ifa = {
  2037. .ifa_family = (alen == 4) ? AF_INET : AF_INET6,
  2038. .ifa_prefixlen = addr->mask,
  2039. .ifa_index = dev->ifindex,
  2040. };
  2041. struct nl_msg *msg;
  2042. if (cmd == RTM_NEWADDR)
  2043. flags |= NLM_F_CREATE | NLM_F_REPLACE;
  2044. msg = nlmsg_alloc_simple(cmd, flags);
  2045. if (!msg)
  2046. return -1;
  2047. nlmsg_append(msg, &ifa, sizeof(ifa), 0);
  2048. nla_put(msg, IFA_LOCAL, alen, &addr->addr);
  2049. if (v4) {
  2050. if (addr->broadcast)
  2051. nla_put_u32(msg, IFA_BROADCAST, addr->broadcast);
  2052. if (addr->point_to_point)
  2053. nla_put_u32(msg, IFA_ADDRESS, addr->point_to_point);
  2054. } else {
  2055. time_t now = system_get_rtime();
  2056. struct ifa_cacheinfo cinfo = {0xffffffffU, 0xffffffffU, 0, 0};
  2057. if (addr->preferred_until) {
  2058. int64_t preferred = addr->preferred_until - now;
  2059. if (preferred < 0)
  2060. preferred = 0;
  2061. else if (preferred > UINT32_MAX)
  2062. preferred = UINT32_MAX;
  2063. cinfo.ifa_prefered = preferred;
  2064. }
  2065. if (addr->valid_until) {
  2066. int64_t valid = addr->valid_until - now;
  2067. if (valid <= 0) {
  2068. nlmsg_free(msg);
  2069. return -1;
  2070. }
  2071. else if (valid > UINT32_MAX)
  2072. valid = UINT32_MAX;
  2073. cinfo.ifa_valid = valid;
  2074. }
  2075. nla_put(msg, IFA_CACHEINFO, sizeof(cinfo), &cinfo);
  2076. if (cmd == RTM_NEWADDR && (addr->flags & DEVADDR_OFFLINK))
  2077. nla_put_u32(msg, IFA_FLAGS, IFA_F_NOPREFIXROUTE);
  2078. }
  2079. return system_rtnl_call(msg);
  2080. }
  2081. int system_add_address(struct device *dev, struct device_addr *addr)
  2082. {
  2083. return system_addr(dev, addr, RTM_NEWADDR);
  2084. }
  2085. int system_del_address(struct device *dev, struct device_addr *addr)
  2086. {
  2087. return system_addr(dev, addr, RTM_DELADDR);
  2088. }
  2089. static int system_neigh(struct device *dev, struct device_neighbor *neighbor, int cmd)
  2090. {
  2091. int alen = ((neighbor->flags & DEVADDR_FAMILY) == DEVADDR_INET4) ? 4 : 16;
  2092. unsigned int flags = 0;
  2093. struct ndmsg ndm = {
  2094. .ndm_family = (alen == 4) ? AF_INET : AF_INET6,
  2095. .ndm_ifindex = dev->ifindex,
  2096. .ndm_state = NUD_PERMANENT,
  2097. .ndm_flags = (neighbor->proxy ? NTF_PROXY : 0) | (neighbor->router ? NTF_ROUTER : 0),
  2098. };
  2099. struct nl_msg *msg;
  2100. if (cmd == RTM_NEWNEIGH)
  2101. flags |= NLM_F_CREATE | NLM_F_REPLACE;
  2102. msg = nlmsg_alloc_simple(cmd, flags);
  2103. if (!msg)
  2104. return -1;
  2105. nlmsg_append(msg, &ndm, sizeof(ndm), 0);
  2106. nla_put(msg, NDA_DST, alen, &neighbor->addr);
  2107. if (neighbor->flags & DEVNEIGH_MAC)
  2108. nla_put(msg, NDA_LLADDR, sizeof(neighbor->macaddr), &neighbor->macaddr);
  2109. return system_rtnl_call(msg);
  2110. }
  2111. int system_add_neighbor(struct device *dev, struct device_neighbor *neighbor)
  2112. {
  2113. return system_neigh(dev, neighbor, RTM_NEWNEIGH);
  2114. }
  2115. int system_del_neighbor(struct device *dev, struct device_neighbor *neighbor)
  2116. {
  2117. return system_neigh(dev, neighbor, RTM_DELNEIGH);
  2118. }
  2119. static int system_rt(struct device *dev, struct device_route *route, int cmd)
  2120. {
  2121. int alen = ((route->flags & DEVADDR_FAMILY) == DEVADDR_INET4) ? 4 : 16;
  2122. bool have_gw;
  2123. unsigned int flags = 0;
  2124. if (alen == 4)
  2125. have_gw = !!route->nexthop.in.s_addr;
  2126. else
  2127. have_gw = route->nexthop.in6.s6_addr32[0] ||
  2128. route->nexthop.in6.s6_addr32[1] ||
  2129. route->nexthop.in6.s6_addr32[2] ||
  2130. route->nexthop.in6.s6_addr32[3];
  2131. unsigned int table = (route->flags & (DEVROUTE_TABLE | DEVROUTE_SRCTABLE))
  2132. ? route->table : RT_TABLE_MAIN;
  2133. struct rtmsg rtm = {
  2134. .rtm_family = (alen == 4) ? AF_INET : AF_INET6,
  2135. .rtm_dst_len = route->mask,
  2136. .rtm_src_len = route->sourcemask,
  2137. .rtm_table = (table < 256) ? table : RT_TABLE_UNSPEC,
  2138. .rtm_protocol = (route->flags & DEVROUTE_PROTO) ? route->proto : RTPROT_STATIC,
  2139. .rtm_scope = RT_SCOPE_NOWHERE,
  2140. .rtm_type = (cmd == RTM_DELROUTE) ? 0: RTN_UNICAST,
  2141. .rtm_flags = (route->flags & DEVROUTE_ONLINK) ? RTNH_F_ONLINK : 0,
  2142. };
  2143. struct nl_msg *msg;
  2144. if (cmd == RTM_NEWROUTE) {
  2145. flags |= NLM_F_CREATE | NLM_F_REPLACE;
  2146. if (!dev) { /* Add null-route */
  2147. rtm.rtm_scope = RT_SCOPE_UNIVERSE;
  2148. rtm.rtm_type = RTN_UNREACHABLE;
  2149. }
  2150. else
  2151. rtm.rtm_scope = (have_gw) ? RT_SCOPE_UNIVERSE : RT_SCOPE_LINK;
  2152. }
  2153. if (route->flags & DEVROUTE_TYPE) {
  2154. rtm.rtm_type = route->type;
  2155. if (!(route->flags & (DEVROUTE_TABLE | DEVROUTE_SRCTABLE))) {
  2156. if (rtm.rtm_type == RTN_LOCAL || rtm.rtm_type == RTN_BROADCAST ||
  2157. rtm.rtm_type == RTN_NAT || rtm.rtm_type == RTN_ANYCAST)
  2158. rtm.rtm_table = RT_TABLE_LOCAL;
  2159. }
  2160. if (rtm.rtm_type == RTN_LOCAL || rtm.rtm_type == RTN_NAT) {
  2161. rtm.rtm_scope = RT_SCOPE_HOST;
  2162. } else if (rtm.rtm_type == RTN_BROADCAST || rtm.rtm_type == RTN_MULTICAST ||
  2163. rtm.rtm_type == RTN_ANYCAST) {
  2164. rtm.rtm_scope = RT_SCOPE_LINK;
  2165. } else if (rtm.rtm_type == RTN_BLACKHOLE || rtm.rtm_type == RTN_UNREACHABLE ||
  2166. rtm.rtm_type == RTN_PROHIBIT || rtm.rtm_type == RTN_FAILED_POLICY ||
  2167. rtm.rtm_type == RTN_THROW) {
  2168. rtm.rtm_scope = RT_SCOPE_UNIVERSE;
  2169. dev = NULL;
  2170. }
  2171. }
  2172. msg = nlmsg_alloc_simple(cmd, flags);
  2173. if (!msg)
  2174. return -1;
  2175. nlmsg_append(msg, &rtm, sizeof(rtm), 0);
  2176. if (route->mask)
  2177. nla_put(msg, RTA_DST, alen, &route->addr);
  2178. if (route->sourcemask) {
  2179. if (rtm.rtm_family == AF_INET)
  2180. nla_put(msg, RTA_PREFSRC, alen, &route->source);
  2181. else
  2182. nla_put(msg, RTA_SRC, alen, &route->source);
  2183. }
  2184. if (route->metric > 0)
  2185. nla_put_u32(msg, RTA_PRIORITY, route->metric);
  2186. if (have_gw)
  2187. nla_put(msg, RTA_GATEWAY, alen, &route->nexthop);
  2188. if (dev)
  2189. nla_put_u32(msg, RTA_OIF, dev->ifindex);
  2190. if (table >= 256)
  2191. nla_put_u32(msg, RTA_TABLE, table);
  2192. if (route->flags & DEVROUTE_MTU) {
  2193. struct nlattr *metrics;
  2194. if (!(metrics = nla_nest_start(msg, RTA_METRICS)))
  2195. goto nla_put_failure;
  2196. nla_put_u32(msg, RTAX_MTU, route->mtu);
  2197. nla_nest_end(msg, metrics);
  2198. }
  2199. return system_rtnl_call(msg);
  2200. nla_put_failure:
  2201. nlmsg_free(msg);
  2202. return -ENOMEM;
  2203. }
  2204. int system_add_route(struct device *dev, struct device_route *route)
  2205. {
  2206. return system_rt(dev, route, RTM_NEWROUTE);
  2207. }
  2208. int system_del_route(struct device *dev, struct device_route *route)
  2209. {
  2210. return system_rt(dev, route, RTM_DELROUTE);
  2211. }
  2212. int system_flush_routes(void)
  2213. {
  2214. const char *names[] = { "ipv4", "ipv6" };
  2215. int fd, i;
  2216. for (i = 0; i < ARRAY_SIZE(names); i++) {
  2217. snprintf(dev_buf, sizeof(dev_buf), "%s/sys/net/%s/route/flush", proc_path, names[i]);
  2218. fd = open(dev_buf, O_WRONLY);
  2219. if (fd < 0)
  2220. continue;
  2221. if (write(fd, "-1", 2)) {}
  2222. close(fd);
  2223. }
  2224. return 0;
  2225. }
  2226. bool system_resolve_rt_type(const char *type, unsigned int *id)
  2227. {
  2228. return system_rtn_aton(type, id);
  2229. }
  2230. bool system_resolve_rt_proto(const char *type, unsigned int *id)
  2231. {
  2232. FILE *f;
  2233. char *e, buf[128];
  2234. unsigned int n, proto = 256;
  2235. n = strtoul(type, &e, 0);
  2236. if (!*e && e != type)
  2237. proto = n;
  2238. else if (!strcmp(type, "unspec"))
  2239. proto = RTPROT_UNSPEC;
  2240. else if (!strcmp(type, "kernel"))
  2241. proto = RTPROT_KERNEL;
  2242. else if (!strcmp(type, "boot"))
  2243. proto = RTPROT_BOOT;
  2244. else if (!strcmp(type, "static"))
  2245. proto = RTPROT_STATIC;
  2246. else if ((f = fopen("/etc/iproute2/rt_protos", "r")) != NULL) {
  2247. while (fgets(buf, sizeof(buf) - 1, f) != NULL) {
  2248. if ((e = strtok(buf, " \t\n")) == NULL || *e == '#')
  2249. continue;
  2250. n = strtoul(e, NULL, 10);
  2251. e = strtok(NULL, " \t\n");
  2252. if (e && !strcmp(e, type)) {
  2253. proto = n;
  2254. break;
  2255. }
  2256. }
  2257. fclose(f);
  2258. }
  2259. if (proto > 255)
  2260. return false;
  2261. *id = proto;
  2262. return true;
  2263. }
  2264. bool system_resolve_rt_table(const char *name, unsigned int *id)
  2265. {
  2266. FILE *f;
  2267. char *e, buf[128];
  2268. unsigned int n, table = RT_TABLE_UNSPEC;
  2269. /* first try to parse table as number */
  2270. if ((n = strtoul(name, &e, 0)) > 0 && !*e)
  2271. table = n;
  2272. /* handle well known aliases */
  2273. else if (!strcmp(name, "default"))
  2274. table = RT_TABLE_DEFAULT;
  2275. else if (!strcmp(name, "main"))
  2276. table = RT_TABLE_MAIN;
  2277. else if (!strcmp(name, "local"))
  2278. table = RT_TABLE_LOCAL;
  2279. /* try to look up name in /etc/iproute2/rt_tables */
  2280. else if ((f = fopen("/etc/iproute2/rt_tables", "r")) != NULL)
  2281. {
  2282. while (fgets(buf, sizeof(buf) - 1, f) != NULL)
  2283. {
  2284. if ((e = strtok(buf, " \t\n")) == NULL || *e == '#')
  2285. continue;
  2286. n = strtoul(e, NULL, 10);
  2287. e = strtok(NULL, " \t\n");
  2288. if (e && !strcmp(e, name))
  2289. {
  2290. table = n;
  2291. break;
  2292. }
  2293. }
  2294. fclose(f);
  2295. }
  2296. if (table == RT_TABLE_UNSPEC)
  2297. return false;
  2298. *id = table;
  2299. return true;
  2300. }
  2301. bool system_is_default_rt_table(unsigned int id)
  2302. {
  2303. return (id == RT_TABLE_MAIN);
  2304. }
  2305. bool system_resolve_rpfilter(const char *filter, unsigned int *id)
  2306. {
  2307. char *e;
  2308. unsigned int n;
  2309. if (!strcmp(filter, "strict"))
  2310. n = 1;
  2311. else if (!strcmp(filter, "loose"))
  2312. n = 2;
  2313. else {
  2314. n = strtoul(filter, &e, 0);
  2315. if (*e || e == filter || n > 2)
  2316. return false;
  2317. }
  2318. *id = n;
  2319. return true;
  2320. }
  2321. static int system_iprule(struct iprule *rule, int cmd)
  2322. {
  2323. int alen = ((rule->flags & IPRULE_FAMILY) == IPRULE_INET4) ? 4 : 16;
  2324. struct nl_msg *msg;
  2325. struct rtmsg rtm = {
  2326. .rtm_family = (alen == 4) ? AF_INET : AF_INET6,
  2327. .rtm_protocol = RTPROT_STATIC,
  2328. .rtm_scope = RT_SCOPE_UNIVERSE,
  2329. .rtm_table = RT_TABLE_UNSPEC,
  2330. .rtm_type = RTN_UNSPEC,
  2331. .rtm_flags = 0,
  2332. };
  2333. if (cmd == RTM_NEWRULE)
  2334. rtm.rtm_type = RTN_UNICAST;
  2335. if (rule->invert)
  2336. rtm.rtm_flags |= FIB_RULE_INVERT;
  2337. if (rule->flags & IPRULE_SRC)
  2338. rtm.rtm_src_len = rule->src_mask;
  2339. if (rule->flags & IPRULE_DEST)
  2340. rtm.rtm_dst_len = rule->dest_mask;
  2341. if (rule->flags & IPRULE_TOS)
  2342. rtm.rtm_tos = rule->tos;
  2343. if (rule->flags & IPRULE_LOOKUP) {
  2344. if (rule->lookup < 256)
  2345. rtm.rtm_table = rule->lookup;
  2346. }
  2347. if (rule->flags & IPRULE_ACTION)
  2348. rtm.rtm_type = rule->action;
  2349. else if (rule->flags & IPRULE_GOTO)
  2350. rtm.rtm_type = FR_ACT_GOTO;
  2351. else if (!(rule->flags & (IPRULE_LOOKUP | IPRULE_ACTION | IPRULE_GOTO)))
  2352. rtm.rtm_type = FR_ACT_NOP;
  2353. msg = nlmsg_alloc_simple(cmd, NLM_F_REQUEST);
  2354. if (!msg)
  2355. return -1;
  2356. nlmsg_append(msg, &rtm, sizeof(rtm), 0);
  2357. if (rule->flags & IPRULE_IN)
  2358. nla_put(msg, FRA_IFNAME, strlen(rule->in_dev) + 1, rule->in_dev);
  2359. if (rule->flags & IPRULE_OUT)
  2360. nla_put(msg, FRA_OIFNAME, strlen(rule->out_dev) + 1, rule->out_dev);
  2361. if (rule->flags & IPRULE_SRC)
  2362. nla_put(msg, FRA_SRC, alen, &rule->src_addr);
  2363. if (rule->flags & IPRULE_DEST)
  2364. nla_put(msg, FRA_DST, alen, &rule->dest_addr);
  2365. if (rule->flags & IPRULE_PRIORITY)
  2366. nla_put_u32(msg, FRA_PRIORITY, rule->priority);
  2367. else if (cmd == RTM_NEWRULE)
  2368. nla_put_u32(msg, FRA_PRIORITY, rule->order);
  2369. if (rule->flags & IPRULE_FWMARK)
  2370. nla_put_u32(msg, FRA_FWMARK, rule->fwmark);
  2371. if (rule->flags & IPRULE_FWMASK)
  2372. nla_put_u32(msg, FRA_FWMASK, rule->fwmask);
  2373. if (rule->flags & IPRULE_LOOKUP) {
  2374. if (rule->lookup >= 256)
  2375. nla_put_u32(msg, FRA_TABLE, rule->lookup);
  2376. }
  2377. if (rule->flags & IPRULE_SUP_PREFIXLEN)
  2378. nla_put_u32(msg, FRA_SUPPRESS_PREFIXLEN, rule->sup_prefixlen);
  2379. if (rule->flags & IPRULE_UIDRANGE) {
  2380. struct fib_rule_uid_range uidrange = {
  2381. .start = rule->uidrange_start,
  2382. .end = rule->uidrange_end
  2383. };
  2384. nla_put(msg, FRA_UID_RANGE, sizeof(uidrange), &uidrange);
  2385. }
  2386. if (rule->flags & IPRULE_GOTO)
  2387. nla_put_u32(msg, FRA_GOTO, rule->gotoid);
  2388. return system_rtnl_call(msg);
  2389. }
  2390. int system_add_iprule(struct iprule *rule)
  2391. {
  2392. return system_iprule(rule, RTM_NEWRULE);
  2393. }
  2394. int system_del_iprule(struct iprule *rule)
  2395. {
  2396. return system_iprule(rule, RTM_DELRULE);
  2397. }
  2398. int system_flush_iprules(void)
  2399. {
  2400. int rv = 0;
  2401. struct iprule rule;
  2402. system_if_clear_entries(NULL, RTM_GETRULE, AF_INET);
  2403. system_if_clear_entries(NULL, RTM_GETRULE, AF_INET6);
  2404. memset(&rule, 0, sizeof(rule));
  2405. rule.flags = IPRULE_INET4 | IPRULE_PRIORITY | IPRULE_LOOKUP;
  2406. rule.priority = 0;
  2407. rule.lookup = RT_TABLE_LOCAL;
  2408. rv |= system_iprule(&rule, RTM_NEWRULE);
  2409. rule.priority = 32766;
  2410. rule.lookup = RT_TABLE_MAIN;
  2411. rv |= system_iprule(&rule, RTM_NEWRULE);
  2412. rule.priority = 32767;
  2413. rule.lookup = RT_TABLE_DEFAULT;
  2414. rv |= system_iprule(&rule, RTM_NEWRULE);
  2415. rule.flags = IPRULE_INET6 | IPRULE_PRIORITY | IPRULE_LOOKUP;
  2416. rule.priority = 0;
  2417. rule.lookup = RT_TABLE_LOCAL;
  2418. rv |= system_iprule(&rule, RTM_NEWRULE);
  2419. rule.priority = 32766;
  2420. rule.lookup = RT_TABLE_MAIN;
  2421. rv |= system_iprule(&rule, RTM_NEWRULE);
  2422. return rv;
  2423. }
  2424. bool system_resolve_iprule_action(const char *action, unsigned int *id)
  2425. {
  2426. return system_rtn_aton(action, id);
  2427. }
  2428. time_t system_get_rtime(void)
  2429. {
  2430. struct timespec ts;
  2431. struct timeval tv;
  2432. if (clock_gettime(CLOCK_MONOTONIC, &ts) == 0)
  2433. return ts.tv_sec;
  2434. if (gettimeofday(&tv, NULL) == 0)
  2435. return tv.tv_sec;
  2436. return 0;
  2437. }
  2438. #ifndef IP_DF
  2439. #define IP_DF 0x4000
  2440. #endif
  2441. static int tunnel_ioctl(const char *name, int cmd, void *p)
  2442. {
  2443. struct ifreq ifr;
  2444. memset(&ifr, 0, sizeof(ifr));
  2445. strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name) - 1);
  2446. ifr.ifr_ifru.ifru_data = p;
  2447. return ioctl(sock_ioctl, cmd, &ifr);
  2448. }
  2449. #ifdef IFLA_IPTUN_MAX
  2450. static int system_add_ip6_tunnel(const char *name, const unsigned int link,
  2451. struct blob_attr **tb)
  2452. {
  2453. struct nl_msg *nlm = nlmsg_alloc_simple(RTM_NEWLINK,
  2454. NLM_F_REQUEST | NLM_F_REPLACE | NLM_F_CREATE);
  2455. struct ifinfomsg ifi = { .ifi_family = AF_UNSPEC };
  2456. struct blob_attr *cur;
  2457. int ret = 0, ttl = 0;
  2458. if (!nlm)
  2459. return -1;
  2460. nlmsg_append(nlm, &ifi, sizeof(ifi), 0);
  2461. nla_put_string(nlm, IFLA_IFNAME, name);
  2462. if (link)
  2463. nla_put_u32(nlm, IFLA_LINK, link);
  2464. struct nlattr *linkinfo = nla_nest_start(nlm, IFLA_LINKINFO);
  2465. if (!linkinfo) {
  2466. ret = -ENOMEM;
  2467. goto failure;
  2468. }
  2469. nla_put_string(nlm, IFLA_INFO_KIND, "ip6tnl");
  2470. struct nlattr *infodata = nla_nest_start(nlm, IFLA_INFO_DATA);
  2471. if (!infodata) {
  2472. ret = -ENOMEM;
  2473. goto failure;
  2474. }
  2475. if (link)
  2476. nla_put_u32(nlm, IFLA_IPTUN_LINK, link);
  2477. if ((cur = tb[TUNNEL_ATTR_TTL]))
  2478. ttl = blobmsg_get_u32(cur);
  2479. nla_put_u8(nlm, IFLA_IPTUN_PROTO, IPPROTO_IPIP);
  2480. nla_put_u8(nlm, IFLA_IPTUN_TTL, (ttl) ? ttl : 64);
  2481. struct in6_addr in6buf;
  2482. if ((cur = tb[TUNNEL_ATTR_LOCAL])) {
  2483. if (inet_pton(AF_INET6, blobmsg_data(cur), &in6buf) < 1) {
  2484. ret = -EINVAL;
  2485. goto failure;
  2486. }
  2487. nla_put(nlm, IFLA_IPTUN_LOCAL, sizeof(in6buf), &in6buf);
  2488. }
  2489. if ((cur = tb[TUNNEL_ATTR_REMOTE])) {
  2490. if (inet_pton(AF_INET6, blobmsg_data(cur), &in6buf) < 1) {
  2491. ret = -EINVAL;
  2492. goto failure;
  2493. }
  2494. nla_put(nlm, IFLA_IPTUN_REMOTE, sizeof(in6buf), &in6buf);
  2495. }
  2496. if ((cur = tb[TUNNEL_ATTR_DATA])) {
  2497. struct blob_attr *tb_data[__IPIP6_DATA_ATTR_MAX];
  2498. uint32_t tun_flags = IP6_TNL_F_IGN_ENCAP_LIMIT;
  2499. blobmsg_parse(ipip6_data_attr_list.params, __IPIP6_DATA_ATTR_MAX, tb_data,
  2500. blobmsg_data(cur), blobmsg_len(cur));
  2501. if ((cur = tb_data[IPIP6_DATA_ENCAPLIMIT])) {
  2502. char *str = blobmsg_get_string(cur);
  2503. if (strcmp(str, "ignore")) {
  2504. char *e;
  2505. unsigned encap_limit = strtoul(str, &e, 0);
  2506. if (e == str || *e || encap_limit > 255) {
  2507. ret = -EINVAL;
  2508. goto failure;
  2509. }
  2510. nla_put_u8(nlm, IFLA_IPTUN_ENCAP_LIMIT, encap_limit);
  2511. tun_flags &= ~IP6_TNL_F_IGN_ENCAP_LIMIT;
  2512. }
  2513. }
  2514. #ifdef IFLA_IPTUN_FMR_MAX
  2515. if ((cur = tb_data[IPIP6_DATA_FMRS])) {
  2516. struct blob_attr *rcur;
  2517. unsigned rrem, fmrcnt = 0;
  2518. struct nlattr *fmrs = nla_nest_start(nlm, IFLA_IPTUN_FMRS);
  2519. if (!fmrs) {
  2520. ret = -ENOMEM;
  2521. goto failure;
  2522. }
  2523. blobmsg_for_each_attr(rcur, cur, rrem) {
  2524. struct blob_attr *tb_fmr[__FMR_DATA_ATTR_MAX], *tb_cur;
  2525. struct in6_addr ip6prefix;
  2526. struct in_addr ip4prefix;
  2527. unsigned ip4len, ip6len, ealen, offset;
  2528. blobmsg_parse(fmr_data_attr_list.params, __FMR_DATA_ATTR_MAX, tb_fmr,
  2529. blobmsg_data(rcur), blobmsg_len(rcur));
  2530. if (!(tb_cur = tb_fmr[FMR_DATA_PREFIX6]) ||
  2531. !parse_ip_and_netmask(AF_INET6,
  2532. blobmsg_data(tb_cur), &ip6prefix,
  2533. &ip6len)) {
  2534. ret = -EINVAL;
  2535. goto failure;
  2536. }
  2537. if (!(tb_cur = tb_fmr[FMR_DATA_PREFIX4]) ||
  2538. !parse_ip_and_netmask(AF_INET,
  2539. blobmsg_data(tb_cur), &ip4prefix,
  2540. &ip4len)) {
  2541. ret = -EINVAL;
  2542. goto failure;
  2543. }
  2544. if (!(tb_cur = tb_fmr[FMR_DATA_EALEN])) {
  2545. ret = -EINVAL;
  2546. goto failure;
  2547. }
  2548. ealen = blobmsg_get_u32(tb_cur);
  2549. if (!(tb_cur = tb_fmr[FMR_DATA_OFFSET])) {
  2550. ret = -EINVAL;
  2551. goto failure;
  2552. }
  2553. offset = blobmsg_get_u32(tb_cur);
  2554. struct nlattr *rule = nla_nest_start(nlm, ++fmrcnt);
  2555. if (!rule) {
  2556. ret = -ENOMEM;
  2557. goto failure;
  2558. }
  2559. nla_put(nlm, IFLA_IPTUN_FMR_IP6_PREFIX, sizeof(ip6prefix), &ip6prefix);
  2560. nla_put(nlm, IFLA_IPTUN_FMR_IP4_PREFIX, sizeof(ip4prefix), &ip4prefix);
  2561. nla_put_u8(nlm, IFLA_IPTUN_FMR_IP6_PREFIX_LEN, ip6len);
  2562. nla_put_u8(nlm, IFLA_IPTUN_FMR_IP4_PREFIX_LEN, ip4len);
  2563. nla_put_u8(nlm, IFLA_IPTUN_FMR_EA_LEN, ealen);
  2564. nla_put_u8(nlm, IFLA_IPTUN_FMR_OFFSET, offset);
  2565. nla_nest_end(nlm, rule);
  2566. }
  2567. nla_nest_end(nlm, fmrs);
  2568. }
  2569. #endif
  2570. if (tun_flags)
  2571. nla_put_u32(nlm, IFLA_IPTUN_FLAGS, tun_flags);
  2572. }
  2573. nla_nest_end(nlm, infodata);
  2574. nla_nest_end(nlm, linkinfo);
  2575. return system_rtnl_call(nlm);
  2576. failure:
  2577. nlmsg_free(nlm);
  2578. return ret;
  2579. }
  2580. #endif
  2581. #ifdef IFLA_IPTUN_MAX
  2582. #define IP6_FLOWINFO_TCLASS htonl(0x0FF00000)
  2583. static int system_add_gre_tunnel(const char *name, const char *kind,
  2584. const unsigned int link, struct blob_attr **tb, bool v6)
  2585. {
  2586. struct nl_msg *nlm;
  2587. struct ifinfomsg ifi = { .ifi_family = AF_UNSPEC, };
  2588. struct blob_attr *cur;
  2589. uint32_t ikey = 0, okey = 0, flowinfo = 0, flags6 = IP6_TNL_F_IGN_ENCAP_LIMIT;
  2590. uint16_t iflags = 0, oflags = 0;
  2591. uint8_t tos = 0;
  2592. int ret = 0, ttl = 0;
  2593. unsigned encap_limit = 0;
  2594. nlm = nlmsg_alloc_simple(RTM_NEWLINK, NLM_F_REQUEST | NLM_F_REPLACE | NLM_F_CREATE);
  2595. if (!nlm)
  2596. return -1;
  2597. nlmsg_append(nlm, &ifi, sizeof(ifi), 0);
  2598. nla_put_string(nlm, IFLA_IFNAME, name);
  2599. struct nlattr *linkinfo = nla_nest_start(nlm, IFLA_LINKINFO);
  2600. if (!linkinfo) {
  2601. ret = -ENOMEM;
  2602. goto failure;
  2603. }
  2604. nla_put_string(nlm, IFLA_INFO_KIND, kind);
  2605. struct nlattr *infodata = nla_nest_start(nlm, IFLA_INFO_DATA);
  2606. if (!infodata) {
  2607. ret = -ENOMEM;
  2608. goto failure;
  2609. }
  2610. if (link)
  2611. nla_put_u32(nlm, IFLA_GRE_LINK, link);
  2612. if ((cur = tb[TUNNEL_ATTR_TTL]))
  2613. ttl = blobmsg_get_u32(cur);
  2614. if ((cur = tb[TUNNEL_ATTR_TOS])) {
  2615. char *str = blobmsg_get_string(cur);
  2616. if (strcmp(str, "inherit")) {
  2617. unsigned uval;
  2618. if (!system_tos_aton(str, &uval)) {
  2619. ret = -EINVAL;
  2620. goto failure;
  2621. }
  2622. if (v6)
  2623. flowinfo |= htonl(uval << 20) & IP6_FLOWINFO_TCLASS;
  2624. else
  2625. tos = uval;
  2626. } else {
  2627. if (v6)
  2628. flags6 |= IP6_TNL_F_USE_ORIG_TCLASS;
  2629. else
  2630. tos = 1;
  2631. }
  2632. }
  2633. if ((cur = tb[TUNNEL_ATTR_DATA])) {
  2634. struct blob_attr *tb_data[__GRE_DATA_ATTR_MAX];
  2635. blobmsg_parse(gre_data_attr_list.params, __GRE_DATA_ATTR_MAX, tb_data,
  2636. blobmsg_data(cur), blobmsg_len(cur));
  2637. if ((cur = tb_data[GRE_DATA_IKEY])) {
  2638. if ((ikey = blobmsg_get_u32(cur)))
  2639. iflags |= GRE_KEY;
  2640. }
  2641. if ((cur = tb_data[GRE_DATA_OKEY])) {
  2642. if ((okey = blobmsg_get_u32(cur)))
  2643. oflags |= GRE_KEY;
  2644. }
  2645. if ((cur = tb_data[GRE_DATA_ICSUM])) {
  2646. if (blobmsg_get_bool(cur))
  2647. iflags |= GRE_CSUM;
  2648. }
  2649. if ((cur = tb_data[GRE_DATA_OCSUM])) {
  2650. if (blobmsg_get_bool(cur))
  2651. oflags |= GRE_CSUM;
  2652. }
  2653. if ((cur = tb_data[GRE_DATA_ISEQNO])) {
  2654. if (blobmsg_get_bool(cur))
  2655. iflags |= GRE_SEQ;
  2656. }
  2657. if ((cur = tb_data[GRE_DATA_OSEQNO])) {
  2658. if (blobmsg_get_bool(cur))
  2659. oflags |= GRE_SEQ;
  2660. }
  2661. if ((cur = tb_data[GRE_DATA_ENCAPLIMIT])) {
  2662. char *str = blobmsg_get_string(cur);
  2663. if (strcmp(str, "ignore")) {
  2664. char *e;
  2665. encap_limit = strtoul(str, &e, 0);
  2666. if (e == str || *e || encap_limit > 255) {
  2667. ret = -EINVAL;
  2668. goto failure;
  2669. }
  2670. flags6 &= ~IP6_TNL_F_IGN_ENCAP_LIMIT;
  2671. }
  2672. }
  2673. }
  2674. if (v6) {
  2675. struct in6_addr in6buf;
  2676. if ((cur = tb[TUNNEL_ATTR_LOCAL])) {
  2677. if (inet_pton(AF_INET6, blobmsg_data(cur), &in6buf) < 1) {
  2678. ret = -EINVAL;
  2679. goto failure;
  2680. }
  2681. nla_put(nlm, IFLA_GRE_LOCAL, sizeof(in6buf), &in6buf);
  2682. }
  2683. if ((cur = tb[TUNNEL_ATTR_REMOTE])) {
  2684. if (inet_pton(AF_INET6, blobmsg_data(cur), &in6buf) < 1) {
  2685. ret = -EINVAL;
  2686. goto failure;
  2687. }
  2688. nla_put(nlm, IFLA_GRE_REMOTE, sizeof(in6buf), &in6buf);
  2689. }
  2690. if (!(flags6 & IP6_TNL_F_IGN_ENCAP_LIMIT))
  2691. nla_put_u8(nlm, IFLA_GRE_ENCAP_LIMIT, encap_limit);
  2692. if (flowinfo)
  2693. nla_put_u32(nlm, IFLA_GRE_FLOWINFO, flowinfo);
  2694. if (flags6)
  2695. nla_put_u32(nlm, IFLA_GRE_FLAGS, flags6);
  2696. if (!ttl)
  2697. ttl = 64;
  2698. } else {
  2699. struct in_addr inbuf;
  2700. bool set_df = true;
  2701. if ((cur = tb[TUNNEL_ATTR_LOCAL])) {
  2702. if (inet_pton(AF_INET, blobmsg_data(cur), &inbuf) < 1) {
  2703. ret = -EINVAL;
  2704. goto failure;
  2705. }
  2706. nla_put(nlm, IFLA_GRE_LOCAL, sizeof(inbuf), &inbuf);
  2707. }
  2708. if ((cur = tb[TUNNEL_ATTR_REMOTE])) {
  2709. if (inet_pton(AF_INET, blobmsg_data(cur), &inbuf) < 1) {
  2710. ret = -EINVAL;
  2711. goto failure;
  2712. }
  2713. nla_put(nlm, IFLA_GRE_REMOTE, sizeof(inbuf), &inbuf);
  2714. if (IN_MULTICAST(ntohl(inbuf.s_addr))) {
  2715. if (!okey) {
  2716. okey = inbuf.s_addr;
  2717. oflags |= GRE_KEY;
  2718. }
  2719. if (!ikey) {
  2720. ikey = inbuf.s_addr;
  2721. iflags |= GRE_KEY;
  2722. }
  2723. }
  2724. }
  2725. if ((cur = tb[TUNNEL_ATTR_DF]))
  2726. set_df = blobmsg_get_bool(cur);
  2727. if (!set_df) {
  2728. /* ttl != 0 and nopmtudisc are incompatible */
  2729. if (ttl) {
  2730. ret = -EINVAL;
  2731. goto failure;
  2732. }
  2733. } else if (!ttl)
  2734. ttl = 64;
  2735. nla_put_u8(nlm, IFLA_GRE_PMTUDISC, set_df ? 1 : 0);
  2736. nla_put_u8(nlm, IFLA_GRE_TOS, tos);
  2737. }
  2738. if (ttl)
  2739. nla_put_u8(nlm, IFLA_GRE_TTL, ttl);
  2740. if (oflags)
  2741. nla_put_u16(nlm, IFLA_GRE_OFLAGS, oflags);
  2742. if (iflags)
  2743. nla_put_u16(nlm, IFLA_GRE_IFLAGS, iflags);
  2744. if (okey)
  2745. nla_put_u32(nlm, IFLA_GRE_OKEY, htonl(okey));
  2746. if (ikey)
  2747. nla_put_u32(nlm, IFLA_GRE_IKEY, htonl(ikey));
  2748. nla_nest_end(nlm, infodata);
  2749. nla_nest_end(nlm, linkinfo);
  2750. return system_rtnl_call(nlm);
  2751. failure:
  2752. nlmsg_free(nlm);
  2753. return ret;
  2754. }
  2755. #endif
  2756. #ifdef IFLA_VTI_MAX
  2757. static int system_add_vti_tunnel(const char *name, const char *kind,
  2758. const unsigned int link, struct blob_attr **tb, bool v6)
  2759. {
  2760. struct nl_msg *nlm;
  2761. struct ifinfomsg ifi = { .ifi_family = AF_UNSPEC, };
  2762. struct blob_attr *cur;
  2763. int ret = 0;
  2764. nlm = nlmsg_alloc_simple(RTM_NEWLINK, NLM_F_REQUEST | NLM_F_REPLACE | NLM_F_CREATE);
  2765. if (!nlm)
  2766. return -1;
  2767. nlmsg_append(nlm, &ifi, sizeof(ifi), 0);
  2768. nla_put_string(nlm, IFLA_IFNAME, name);
  2769. struct nlattr *linkinfo = nla_nest_start(nlm, IFLA_LINKINFO);
  2770. if (!linkinfo) {
  2771. ret = -ENOMEM;
  2772. goto failure;
  2773. }
  2774. nla_put_string(nlm, IFLA_INFO_KIND, kind);
  2775. struct nlattr *infodata = nla_nest_start(nlm, IFLA_INFO_DATA);
  2776. if (!infodata) {
  2777. ret = -ENOMEM;
  2778. goto failure;
  2779. }
  2780. if (link)
  2781. nla_put_u32(nlm, IFLA_VTI_LINK, link);
  2782. if (v6) {
  2783. struct in6_addr in6buf;
  2784. if ((cur = tb[TUNNEL_ATTR_LOCAL])) {
  2785. if (inet_pton(AF_INET6, blobmsg_data(cur), &in6buf) < 1) {
  2786. ret = -EINVAL;
  2787. goto failure;
  2788. }
  2789. nla_put(nlm, IFLA_VTI_LOCAL, sizeof(in6buf), &in6buf);
  2790. }
  2791. if ((cur = tb[TUNNEL_ATTR_REMOTE])) {
  2792. if (inet_pton(AF_INET6, blobmsg_data(cur), &in6buf) < 1) {
  2793. ret = -EINVAL;
  2794. goto failure;
  2795. }
  2796. nla_put(nlm, IFLA_VTI_REMOTE, sizeof(in6buf), &in6buf);
  2797. }
  2798. } else {
  2799. struct in_addr inbuf;
  2800. if ((cur = tb[TUNNEL_ATTR_LOCAL])) {
  2801. if (inet_pton(AF_INET, blobmsg_data(cur), &inbuf) < 1) {
  2802. ret = -EINVAL;
  2803. goto failure;
  2804. }
  2805. nla_put(nlm, IFLA_VTI_LOCAL, sizeof(inbuf), &inbuf);
  2806. }
  2807. if ((cur = tb[TUNNEL_ATTR_REMOTE])) {
  2808. if (inet_pton(AF_INET, blobmsg_data(cur), &inbuf) < 1) {
  2809. ret = -EINVAL;
  2810. goto failure;
  2811. }
  2812. nla_put(nlm, IFLA_VTI_REMOTE, sizeof(inbuf), &inbuf);
  2813. }
  2814. }
  2815. if ((cur = tb[TUNNEL_ATTR_DATA])) {
  2816. struct blob_attr *tb_data[__VTI_DATA_ATTR_MAX];
  2817. uint32_t ikey = 0, okey = 0;
  2818. blobmsg_parse(vti_data_attr_list.params, __VTI_DATA_ATTR_MAX, tb_data,
  2819. blobmsg_data(cur), blobmsg_len(cur));
  2820. if ((cur = tb_data[VTI_DATA_IKEY])) {
  2821. if ((ikey = blobmsg_get_u32(cur)))
  2822. nla_put_u32(nlm, IFLA_VTI_IKEY, htonl(ikey));
  2823. }
  2824. if ((cur = tb_data[VTI_DATA_OKEY])) {
  2825. if ((okey = blobmsg_get_u32(cur)))
  2826. nla_put_u32(nlm, IFLA_VTI_OKEY, htonl(okey));
  2827. }
  2828. }
  2829. nla_nest_end(nlm, infodata);
  2830. nla_nest_end(nlm, linkinfo);
  2831. return system_rtnl_call(nlm);
  2832. failure:
  2833. nlmsg_free(nlm);
  2834. return ret;
  2835. }
  2836. #endif
  2837. #ifdef IFLA_XFRM_MAX
  2838. static int system_add_xfrm_tunnel(const char *name, const char *kind,
  2839. const unsigned int link, struct blob_attr **tb)
  2840. {
  2841. struct nl_msg *nlm;
  2842. struct ifinfomsg ifi = { .ifi_family = AF_UNSPEC, };
  2843. struct blob_attr *cur;
  2844. int ret = 0;
  2845. nlm = nlmsg_alloc_simple(RTM_NEWLINK, NLM_F_REQUEST | NLM_F_REPLACE | NLM_F_CREATE);
  2846. if (!nlm)
  2847. return -1;
  2848. nlmsg_append(nlm, &ifi, sizeof(ifi), 0);
  2849. nla_put_string(nlm, IFLA_IFNAME, name);
  2850. struct nlattr *linkinfo = nla_nest_start(nlm, IFLA_LINKINFO);
  2851. if (!linkinfo) {
  2852. ret = -ENOMEM;
  2853. goto failure;
  2854. }
  2855. nla_put_string(nlm, IFLA_INFO_KIND, kind);
  2856. struct nlattr *infodata = nla_nest_start(nlm, IFLA_INFO_DATA);
  2857. if (!infodata) {
  2858. ret = -ENOMEM;
  2859. goto failure;
  2860. }
  2861. if (link)
  2862. nla_put_u32(nlm, IFLA_XFRM_LINK, link);
  2863. if ((cur = tb[TUNNEL_ATTR_DATA])) {
  2864. struct blob_attr *tb_data[__XFRM_DATA_ATTR_MAX];
  2865. uint32_t if_id = 0;
  2866. blobmsg_parse(xfrm_data_attr_list.params, __XFRM_DATA_ATTR_MAX, tb_data,
  2867. blobmsg_data(cur), blobmsg_len(cur));
  2868. if ((cur = tb_data[XFRM_DATA_IF_ID])) {
  2869. if ((if_id = blobmsg_get_u32(cur)))
  2870. nla_put_u32(nlm, IFLA_XFRM_IF_ID, if_id);
  2871. }
  2872. }
  2873. nla_nest_end(nlm, infodata);
  2874. nla_nest_end(nlm, linkinfo);
  2875. return system_rtnl_call(nlm);
  2876. failure:
  2877. nlmsg_free(nlm);
  2878. return ret;
  2879. }
  2880. #endif
  2881. #ifdef IFLA_VXLAN_MAX
  2882. static void system_vxlan_map_bool_attr(struct nl_msg *msg, struct blob_attr **tb_data, int attrtype, int vxlandatatype, bool invert) {
  2883. struct blob_attr *cur;
  2884. if ((cur = tb_data[vxlandatatype])) {
  2885. bool val = blobmsg_get_bool(cur);
  2886. if (invert)
  2887. val = !val;
  2888. if ((attrtype == IFLA_VXLAN_GBP) && val)
  2889. nla_put_flag(msg, attrtype);
  2890. else
  2891. nla_put_u8(msg, attrtype, val);
  2892. }
  2893. }
  2894. static int system_add_vxlan(const char *name, const unsigned int link, struct blob_attr **tb, bool v6)
  2895. {
  2896. struct blob_attr *tb_data[__VXLAN_DATA_ATTR_MAX];
  2897. struct nl_msg *msg;
  2898. struct nlattr *linkinfo, *data;
  2899. struct ifinfomsg iim = { .ifi_family = AF_UNSPEC, };
  2900. struct blob_attr *cur;
  2901. int ret = 0;
  2902. if ((cur = tb[TUNNEL_ATTR_DATA]))
  2903. blobmsg_parse(vxlan_data_attr_list.params, __VXLAN_DATA_ATTR_MAX, tb_data,
  2904. blobmsg_data(cur), blobmsg_len(cur));
  2905. else
  2906. return -EINVAL;
  2907. msg = nlmsg_alloc_simple(RTM_NEWLINK, NLM_F_REQUEST | NLM_F_CREATE | NLM_F_EXCL);
  2908. if (!msg)
  2909. return -1;
  2910. nlmsg_append(msg, &iim, sizeof(iim), 0);
  2911. nla_put_string(msg, IFLA_IFNAME, name);
  2912. if ((cur = tb_data[VXLAN_DATA_ATTR_MACADDR])) {
  2913. struct ether_addr *ea = ether_aton(blobmsg_get_string(cur));
  2914. if (!ea) {
  2915. ret = -EINVAL;
  2916. goto failure;
  2917. }
  2918. nla_put(msg, IFLA_ADDRESS, ETH_ALEN, ea);
  2919. }
  2920. if ((cur = tb[TUNNEL_ATTR_MTU])) {
  2921. uint32_t mtu = blobmsg_get_u32(cur);
  2922. nla_put_u32(msg, IFLA_MTU, mtu);
  2923. }
  2924. if (!(linkinfo = nla_nest_start(msg, IFLA_LINKINFO))) {
  2925. ret = -ENOMEM;
  2926. goto failure;
  2927. }
  2928. nla_put_string(msg, IFLA_INFO_KIND, "vxlan");
  2929. if (!(data = nla_nest_start(msg, IFLA_INFO_DATA))) {
  2930. ret = -ENOMEM;
  2931. goto failure;
  2932. }
  2933. if (link)
  2934. nla_put_u32(msg, IFLA_VXLAN_LINK, link);
  2935. if ((cur = tb_data[VXLAN_DATA_ATTR_ID])) {
  2936. uint32_t id = blobmsg_get_u32(cur);
  2937. if (id >= (1u << 24) - 1) {
  2938. ret = -EINVAL;
  2939. goto failure;
  2940. }
  2941. nla_put_u32(msg, IFLA_VXLAN_ID, id);
  2942. }
  2943. if (v6) {
  2944. struct in6_addr in6buf;
  2945. if ((cur = tb[TUNNEL_ATTR_LOCAL])) {
  2946. if (inet_pton(AF_INET6, blobmsg_data(cur), &in6buf) < 1) {
  2947. ret = -EINVAL;
  2948. goto failure;
  2949. }
  2950. nla_put(msg, IFLA_VXLAN_LOCAL6, sizeof(in6buf), &in6buf);
  2951. }
  2952. if ((cur = tb[TUNNEL_ATTR_REMOTE])) {
  2953. if (inet_pton(AF_INET6, blobmsg_data(cur), &in6buf) < 1) {
  2954. ret = -EINVAL;
  2955. goto failure;
  2956. }
  2957. nla_put(msg, IFLA_VXLAN_GROUP6, sizeof(in6buf), &in6buf);
  2958. }
  2959. } else {
  2960. struct in_addr inbuf;
  2961. if ((cur = tb[TUNNEL_ATTR_LOCAL])) {
  2962. if (inet_pton(AF_INET, blobmsg_data(cur), &inbuf) < 1) {
  2963. ret = -EINVAL;
  2964. goto failure;
  2965. }
  2966. nla_put(msg, IFLA_VXLAN_LOCAL, sizeof(inbuf), &inbuf);
  2967. }
  2968. if ((cur = tb[TUNNEL_ATTR_REMOTE])) {
  2969. if (inet_pton(AF_INET, blobmsg_data(cur), &inbuf) < 1) {
  2970. ret = -EINVAL;
  2971. goto failure;
  2972. }
  2973. nla_put(msg, IFLA_VXLAN_GROUP, sizeof(inbuf), &inbuf);
  2974. }
  2975. }
  2976. uint32_t port = 4789;
  2977. if ((cur = tb_data[VXLAN_DATA_ATTR_PORT])) {
  2978. port = blobmsg_get_u32(cur);
  2979. if (port < 1 || port > 65535) {
  2980. ret = -EINVAL;
  2981. goto failure;
  2982. }
  2983. }
  2984. nla_put_u16(msg, IFLA_VXLAN_PORT, htons(port));
  2985. if ((cur = tb_data[VXLAN_DATA_ATTR_SRCPORTMIN])) {
  2986. struct ifla_vxlan_port_range srcports = {0,0};
  2987. uint32_t low = blobmsg_get_u32(cur);
  2988. if (low < 1 || low > 65535 - 1) {
  2989. ret = -EINVAL;
  2990. goto failure;
  2991. }
  2992. srcports.low = htons((uint16_t) low);
  2993. srcports.high = htons((uint16_t) (low+1));
  2994. if ((cur = tb_data[VXLAN_DATA_ATTR_SRCPORTMAX])) {
  2995. uint32_t high = blobmsg_get_u32(cur);
  2996. if (high < 1 || high > 65535) {
  2997. ret = -EINVAL;
  2998. goto failure;
  2999. }
  3000. if (high > low)
  3001. srcports.high = htons((uint16_t) high);
  3002. }
  3003. nla_put(msg, IFLA_VXLAN_PORT_RANGE, sizeof(srcports), &srcports);
  3004. }
  3005. system_vxlan_map_bool_attr(msg, tb_data, IFLA_VXLAN_UDP_CSUM, VXLAN_DATA_ATTR_TXCSUM, false);
  3006. system_vxlan_map_bool_attr(msg, tb_data, IFLA_VXLAN_UDP_ZERO_CSUM6_RX, VXLAN_DATA_ATTR_RXCSUM, true);
  3007. system_vxlan_map_bool_attr(msg, tb_data, IFLA_VXLAN_UDP_ZERO_CSUM6_TX, VXLAN_DATA_ATTR_TXCSUM, true);
  3008. system_vxlan_map_bool_attr(msg, tb_data, IFLA_VXLAN_LEARNING, VXLAN_DATA_ATTR_LEARNING, false);
  3009. system_vxlan_map_bool_attr(msg, tb_data, IFLA_VXLAN_RSC , VXLAN_DATA_ATTR_RSC, false);
  3010. system_vxlan_map_bool_attr(msg, tb_data, IFLA_VXLAN_PROXY , VXLAN_DATA_ATTR_PROXY, false);
  3011. system_vxlan_map_bool_attr(msg, tb_data, IFLA_VXLAN_L2MISS , VXLAN_DATA_ATTR_L2MISS, false);
  3012. system_vxlan_map_bool_attr(msg, tb_data, IFLA_VXLAN_L3MISS , VXLAN_DATA_ATTR_L3MISS, false);
  3013. system_vxlan_map_bool_attr(msg, tb_data, IFLA_VXLAN_GBP , VXLAN_DATA_ATTR_GBP, false);
  3014. if ((cur = tb_data[VXLAN_DATA_ATTR_AGEING])) {
  3015. uint32_t ageing = blobmsg_get_u32(cur);
  3016. nla_put_u32(msg, IFLA_VXLAN_AGEING, ageing);
  3017. }
  3018. if ((cur = tb_data[VXLAN_DATA_ATTR_LIMIT])) {
  3019. uint32_t maxaddress = blobmsg_get_u32(cur);
  3020. nla_put_u32(msg, IFLA_VXLAN_LIMIT, maxaddress);
  3021. }
  3022. if ((cur = tb[TUNNEL_ATTR_TOS])) {
  3023. char *str = blobmsg_get_string(cur);
  3024. unsigned tos = 1;
  3025. if (strcmp(str, "inherit")) {
  3026. if (!system_tos_aton(str, &tos)) {
  3027. ret = -EINVAL;
  3028. goto failure;
  3029. }
  3030. }
  3031. nla_put_u8(msg, IFLA_VXLAN_TOS, tos);
  3032. }
  3033. if ((cur = tb[TUNNEL_ATTR_TTL])) {
  3034. uint32_t ttl = blobmsg_get_u32(cur);
  3035. if (ttl < 1 || ttl > 255) {
  3036. ret = -EINVAL;
  3037. goto failure;
  3038. }
  3039. nla_put_u8(msg, IFLA_VXLAN_TTL, ttl);
  3040. }
  3041. nla_nest_end(msg, data);
  3042. nla_nest_end(msg, linkinfo);
  3043. ret = system_rtnl_call(msg);
  3044. if (ret)
  3045. D(SYSTEM, "Error adding vxlan '%s': %d\n", name, ret);
  3046. return ret;
  3047. failure:
  3048. nlmsg_free(msg);
  3049. return ret;
  3050. }
  3051. #endif
  3052. static int system_add_sit_tunnel(const char *name, const unsigned int link, struct blob_attr **tb)
  3053. {
  3054. struct blob_attr *cur;
  3055. int ret = 0;
  3056. if (system_add_proto_tunnel(name, IPPROTO_IPV6, link, tb) < 0)
  3057. return -1;
  3058. #ifdef SIOCADD6RD
  3059. if ((cur = tb[TUNNEL_ATTR_DATA])) {
  3060. struct blob_attr *tb_data[__SIXRD_DATA_ATTR_MAX];
  3061. unsigned int mask;
  3062. struct ip_tunnel_6rd p6;
  3063. blobmsg_parse(sixrd_data_attr_list.params, __SIXRD_DATA_ATTR_MAX, tb_data,
  3064. blobmsg_data(cur), blobmsg_len(cur));
  3065. memset(&p6, 0, sizeof(p6));
  3066. if ((cur = tb_data[SIXRD_DATA_PREFIX])) {
  3067. if (!parse_ip_and_netmask(AF_INET6, blobmsg_data(cur),
  3068. &p6.prefix, &mask) || mask > 128) {
  3069. ret = -EINVAL;
  3070. goto failure;
  3071. }
  3072. p6.prefixlen = mask;
  3073. }
  3074. if ((cur = tb_data[SIXRD_DATA_RELAY_PREFIX])) {
  3075. if (!parse_ip_and_netmask(AF_INET, blobmsg_data(cur),
  3076. &p6.relay_prefix, &mask) || mask > 32) {
  3077. ret = -EINVAL;
  3078. goto failure;
  3079. }
  3080. p6.relay_prefixlen = mask;
  3081. }
  3082. if (tunnel_ioctl(name, SIOCADD6RD, &p6) < 0) {
  3083. ret = -1;
  3084. goto failure;
  3085. }
  3086. }
  3087. #endif
  3088. return ret;
  3089. failure:
  3090. system_link_del(name);
  3091. return ret;
  3092. }
  3093. static int system_add_proto_tunnel(const char *name, const uint8_t proto, const unsigned int link, struct blob_attr **tb)
  3094. {
  3095. struct blob_attr *cur;
  3096. bool set_df = true;
  3097. struct ip_tunnel_parm p = {
  3098. .link = link,
  3099. .iph = {
  3100. .version = 4,
  3101. .ihl = 5,
  3102. .protocol = proto,
  3103. }
  3104. };
  3105. if ((cur = tb[TUNNEL_ATTR_LOCAL]) &&
  3106. inet_pton(AF_INET, blobmsg_data(cur), &p.iph.saddr) < 1)
  3107. return -EINVAL;
  3108. if ((cur = tb[TUNNEL_ATTR_REMOTE]) &&
  3109. inet_pton(AF_INET, blobmsg_data(cur), &p.iph.daddr) < 1)
  3110. return -EINVAL;
  3111. if ((cur = tb[TUNNEL_ATTR_DF]))
  3112. set_df = blobmsg_get_bool(cur);
  3113. if ((cur = tb[TUNNEL_ATTR_TTL]))
  3114. p.iph.ttl = blobmsg_get_u32(cur);
  3115. if ((cur = tb[TUNNEL_ATTR_TOS])) {
  3116. char *str = blobmsg_get_string(cur);
  3117. if (strcmp(str, "inherit")) {
  3118. unsigned uval;
  3119. if (!system_tos_aton(str, &uval))
  3120. return -EINVAL;
  3121. p.iph.tos = uval;
  3122. } else
  3123. p.iph.tos = 1;
  3124. }
  3125. p.iph.frag_off = set_df ? htons(IP_DF) : 0;
  3126. /* ttl !=0 and nopmtudisc are incompatible */
  3127. if (p.iph.ttl && p.iph.frag_off == 0)
  3128. return -EINVAL;
  3129. strncpy(p.name, name, sizeof(p.name) - 1);
  3130. switch (p.iph.protocol) {
  3131. case IPPROTO_IPIP:
  3132. return tunnel_ioctl("tunl0", SIOCADDTUNNEL, &p);
  3133. case IPPROTO_IPV6:
  3134. return tunnel_ioctl("sit0", SIOCADDTUNNEL, &p);
  3135. default:
  3136. break;
  3137. }
  3138. return -1;
  3139. }
  3140. int system_del_ip_tunnel(const struct device *dev)
  3141. {
  3142. return system_link_del(dev->ifname);
  3143. }
  3144. int system_update_ipv6_mtu(struct device *dev, int mtu)
  3145. {
  3146. int ret = -1;
  3147. char buf[64];
  3148. int fd;
  3149. fd = open(dev_sysctl_path("ipv6/conf", dev->ifname, "mtu"), O_RDWR);
  3150. if (fd < 0)
  3151. return ret;
  3152. if (!mtu) {
  3153. ssize_t len = read(fd, buf, sizeof(buf) - 1);
  3154. if (len < 0)
  3155. goto out;
  3156. buf[len] = 0;
  3157. ret = atoi(buf);
  3158. } else {
  3159. if (write(fd, buf, snprintf(buf, sizeof(buf), "%i", mtu)) > 0)
  3160. ret = mtu;
  3161. }
  3162. out:
  3163. close(fd);
  3164. return ret;
  3165. }
  3166. int system_add_ip_tunnel(const struct device *dev, struct blob_attr *attr)
  3167. {
  3168. struct blob_attr *tb[__TUNNEL_ATTR_MAX];
  3169. struct blob_attr *cur;
  3170. const char *str;
  3171. blobmsg_parse(tunnel_attr_list.params, __TUNNEL_ATTR_MAX, tb,
  3172. blob_data(attr), blob_len(attr));
  3173. system_link_del(dev->ifname);
  3174. if (!(cur = tb[TUNNEL_ATTR_TYPE]))
  3175. return -EINVAL;
  3176. str = blobmsg_data(cur);
  3177. unsigned int ttl = 0;
  3178. if ((cur = tb[TUNNEL_ATTR_TTL])) {
  3179. ttl = blobmsg_get_u32(cur);
  3180. if (ttl > 255)
  3181. return -EINVAL;
  3182. }
  3183. unsigned int link = 0;
  3184. if ((cur = tb[TUNNEL_ATTR_LINK])) {
  3185. struct interface *iface = vlist_find(&interfaces, blobmsg_data(cur), iface, node);
  3186. if (!iface)
  3187. return -EINVAL;
  3188. if (iface->l3_dev.dev)
  3189. link = iface->l3_dev.dev->ifindex;
  3190. }
  3191. if (!strcmp(str, "sit"))
  3192. return system_add_sit_tunnel(dev->ifname, link, tb);
  3193. #ifdef IFLA_IPTUN_MAX
  3194. else if (!strcmp(str, "ipip6")) {
  3195. return system_add_ip6_tunnel(dev->ifname, link, tb);
  3196. } else if (!strcmp(str, "greip")) {
  3197. return system_add_gre_tunnel(dev->ifname, "gre", link, tb, false);
  3198. } else if (!strcmp(str, "gretapip")) {
  3199. return system_add_gre_tunnel(dev->ifname, "gretap", link, tb, false);
  3200. } else if (!strcmp(str, "greip6")) {
  3201. return system_add_gre_tunnel(dev->ifname, "ip6gre", link, tb, true);
  3202. } else if (!strcmp(str, "gretapip6")) {
  3203. return system_add_gre_tunnel(dev->ifname, "ip6gretap", link, tb, true);
  3204. #ifdef IFLA_VTI_MAX
  3205. } else if (!strcmp(str, "vtiip")) {
  3206. return system_add_vti_tunnel(dev->ifname, "vti", link, tb, false);
  3207. } else if (!strcmp(str, "vtiip6")) {
  3208. return system_add_vti_tunnel(dev->ifname, "vti6", link, tb, true);
  3209. #endif
  3210. #ifdef IFLA_XFRM_MAX
  3211. } else if (!strcmp(str, "xfrm")) {
  3212. return system_add_xfrm_tunnel(dev->ifname, "xfrm", link, tb);
  3213. #endif
  3214. #ifdef IFLA_VXLAN_MAX
  3215. } else if(!strcmp(str, "vxlan")) {
  3216. return system_add_vxlan(dev->ifname, link, tb, false);
  3217. } else if(!strcmp(str, "vxlan6")) {
  3218. return system_add_vxlan(dev->ifname, link, tb, true);
  3219. #endif
  3220. #endif
  3221. } else if (!strcmp(str, "ipip")) {
  3222. return system_add_proto_tunnel(dev->ifname, IPPROTO_IPIP, link, tb);
  3223. }
  3224. else
  3225. return -EINVAL;
  3226. return 0;
  3227. }