if.c 52 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431
  1. /* $OpenBSD: if.c,v 1.357 2015/08/13 07:19:58 mpi Exp $ */
  2. /* $NetBSD: if.c,v 1.35 1996/05/07 05:26:04 thorpej Exp $ */
  3. /*
  4. * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. * 1. Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * 2. Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. * 3. Neither the name of the project nor the names of its contributors
  16. * may be used to endorse or promote products derived from this software
  17. * without specific prior written permission.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
  20. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  21. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  22. * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
  23. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  24. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  25. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  26. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  27. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  28. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  29. * SUCH DAMAGE.
  30. */
  31. /*
  32. * Copyright (c) 1980, 1986, 1993
  33. * The Regents of the University of California. All rights reserved.
  34. *
  35. * Redistribution and use in source and binary forms, with or without
  36. * modification, are permitted provided that the following conditions
  37. * are met:
  38. * 1. Redistributions of source code must retain the above copyright
  39. * notice, this list of conditions and the following disclaimer.
  40. * 2. Redistributions in binary form must reproduce the above copyright
  41. * notice, this list of conditions and the following disclaimer in the
  42. * documentation and/or other materials provided with the distribution.
  43. * 3. Neither the name of the University nor the names of its contributors
  44. * may be used to endorse or promote products derived from this software
  45. * without specific prior written permission.
  46. *
  47. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  48. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  49. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  50. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  51. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  52. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  53. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  54. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  55. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  56. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  57. * SUCH DAMAGE.
  58. *
  59. * @(#)if.c 8.3 (Berkeley) 1/4/94
  60. */
  61. #include "bpfilter.h"
  62. #include "bridge.h"
  63. #include "carp.h"
  64. #include "pf.h"
  65. #include "trunk.h"
  66. #include "ether.h"
  67. #include <sys/param.h>
  68. #include <sys/systm.h>
  69. #include <sys/mbuf.h>
  70. #include <sys/socket.h>
  71. #include <sys/socketvar.h>
  72. #include <sys/timeout.h>
  73. #include <sys/protosw.h>
  74. #include <sys/kernel.h>
  75. #include <sys/ioctl.h>
  76. #include <sys/domain.h>
  77. #include <sys/sysctl.h>
  78. #include <sys/task.h>
  79. #include <dev/rndvar.h>
  80. #include <net/if.h>
  81. #include <net/if_dl.h>
  82. #include <net/if_types.h>
  83. #include <net/route.h>
  84. #include <net/netisr.h>
  85. #include <netinet/in.h>
  86. #include <netinet/if_ether.h>
  87. #include <netinet/igmp.h>
  88. #ifdef MROUTING
  89. #include <netinet/ip_mroute.h>
  90. #endif
  91. #ifdef INET6
  92. #include <netinet6/in6_var.h>
  93. #include <netinet6/in6_ifattach.h>
  94. #include <netinet6/nd6.h>
  95. #include <netinet/ip6.h>
  96. #include <netinet6/ip6_var.h>
  97. #endif
  98. #ifdef MPLS
  99. #include <netmpls/mpls.h>
  100. #endif
  101. #if NBPFILTER > 0
  102. #include <net/bpf.h>
  103. #endif
  104. #if NBRIDGE > 0
  105. #include <net/if_bridge.h>
  106. #endif
  107. #if NCARP > 0
  108. #include <netinet/ip_carp.h>
  109. #endif
  110. #if NPF > 0
  111. #include <net/pfvar.h>
  112. #endif
  113. void if_attachsetup(struct ifnet *);
  114. void if_attachdomain1(struct ifnet *);
  115. void if_attach_common(struct ifnet *);
  116. void if_detached_start(struct ifnet *);
  117. int if_detached_ioctl(struct ifnet *, u_long, caddr_t);
  118. int if_getgroup(caddr_t, struct ifnet *);
  119. int if_getgroupmembers(caddr_t);
  120. int if_getgroupattribs(caddr_t);
  121. int if_setgroupattribs(caddr_t);
  122. int if_clone_list(struct if_clonereq *);
  123. struct if_clone *if_clone_lookup(const char *, int *);
  124. int if_group_egress_build(void);
  125. void if_link_state_change_task(void *);
  126. void if_input_process(void *);
  127. #ifdef DDB
  128. void ifa_print_all(void);
  129. #endif
  130. TAILQ_HEAD(, ifg_group) ifg_head = TAILQ_HEAD_INITIALIZER(ifg_head);
  131. LIST_HEAD(, if_clone) if_cloners = LIST_HEAD_INITIALIZER(if_cloners);
  132. int if_cloners_count;
  133. struct timeout net_tick_to;
  134. void net_tick(void *);
  135. int net_livelocked(void);
  136. int ifq_congestion;
  137. struct taskq *softnettq;
  138. /*
  139. * Network interface utility routines.
  140. *
  141. * Routines with ifa_ifwith* names take sockaddr *'s as
  142. * parameters.
  143. */
  144. void
  145. ifinit()
  146. {
  147. timeout_set(&net_tick_to, net_tick, &net_tick_to);
  148. softnettq = taskq_create("softnet", 1, IPL_NET,
  149. TASKQ_MPSAFE | TASKQ_CANTSLEEP);
  150. if (softnettq == NULL)
  151. panic("unable to create softnet taskq");
  152. net_tick(&net_tick_to);
  153. }
  154. static unsigned int if_index = 0;
  155. static unsigned int if_indexlim = 0;
  156. struct ifnet **ifindex2ifnet = NULL;
  157. struct ifnet_head ifnet = TAILQ_HEAD_INITIALIZER(ifnet);
  158. struct ifnet_head iftxlist = TAILQ_HEAD_INITIALIZER(iftxlist);
  159. struct ifnet *lo0ifp;
  160. /*
  161. * Attach an interface to the
  162. * list of "active" interfaces.
  163. */
  164. void
  165. if_attachsetup(struct ifnet *ifp)
  166. {
  167. int wrapped = 0;
  168. /*
  169. * Always increment the index to avoid races.
  170. */
  171. if_index++;
  172. /*
  173. * If we hit USHRT_MAX, we skip back to 1 since there are a
  174. * number of places where the value of ifp->if_index or
  175. * if_index itself is compared to or stored in an unsigned
  176. * short. By jumping back, we won't botch those assignments
  177. * or comparisons.
  178. */
  179. if (if_index == USHRT_MAX) {
  180. if_index = 1;
  181. wrapped++;
  182. }
  183. while (if_index < if_indexlim && ifindex2ifnet[if_index] != NULL) {
  184. if_index++;
  185. if (if_index == USHRT_MAX) {
  186. /*
  187. * If we have to jump back to 1 twice without
  188. * finding an empty slot then there are too many
  189. * interfaces.
  190. */
  191. if (wrapped)
  192. panic("too many interfaces");
  193. if_index = 1;
  194. wrapped++;
  195. }
  196. }
  197. ifp->if_index = if_index;
  198. /*
  199. * We have some arrays that should be indexed by if_index.
  200. * since if_index will grow dynamically, they should grow too.
  201. * struct ifnet **ifindex2ifnet
  202. */
  203. if (ifindex2ifnet == NULL || if_index >= if_indexlim) {
  204. size_t m, n, oldlim;
  205. caddr_t q;
  206. oldlim = if_indexlim;
  207. if (if_indexlim == 0)
  208. if_indexlim = 8;
  209. while (if_index >= if_indexlim)
  210. if_indexlim <<= 1;
  211. /* grow ifindex2ifnet */
  212. m = oldlim * sizeof(struct ifnet *);
  213. n = if_indexlim * sizeof(struct ifnet *);
  214. q = (caddr_t)malloc(n, M_IFADDR, M_WAITOK|M_ZERO);
  215. if (ifindex2ifnet) {
  216. bcopy((caddr_t)ifindex2ifnet, q, m);
  217. free((caddr_t)ifindex2ifnet, M_IFADDR, 0);
  218. }
  219. ifindex2ifnet = (struct ifnet **)q;
  220. }
  221. TAILQ_INIT(&ifp->if_groups);
  222. if_addgroup(ifp, IFG_ALL);
  223. ifindex2ifnet[if_index] = ifp;
  224. if (ifp->if_snd.ifq_maxlen == 0)
  225. IFQ_SET_MAXLEN(&ifp->if_snd, IFQ_MAXLEN);
  226. if (domains)
  227. if_attachdomain1(ifp);
  228. #if NPF > 0
  229. pfi_attach_ifnet(ifp);
  230. #endif
  231. timeout_set(ifp->if_slowtimo, if_slowtimo, ifp);
  232. if_slowtimo(ifp);
  233. task_set(ifp->if_linkstatetask, if_link_state_change_task, ifp);
  234. /* Announce the interface. */
  235. rt_ifannouncemsg(ifp, IFAN_ARRIVAL);
  236. }
  237. /*
  238. * Allocate the link level name for the specified interface. This
  239. * is an attachment helper. It must be called after ifp->if_addrlen
  240. * is initialized, which may not be the case when if_attach() is
  241. * called.
  242. */
  243. void
  244. if_alloc_sadl(struct ifnet *ifp)
  245. {
  246. unsigned int socksize;
  247. int namelen, masklen;
  248. struct sockaddr_dl *sdl;
  249. /*
  250. * If the interface already has a link name, release it
  251. * now. This is useful for interfaces that can change
  252. * link types, and thus switch link names often.
  253. */
  254. if (ifp->if_sadl != NULL)
  255. if_free_sadl(ifp);
  256. namelen = strlen(ifp->if_xname);
  257. masklen = offsetof(struct sockaddr_dl, sdl_data[0]) + namelen;
  258. socksize = masklen + ifp->if_addrlen;
  259. #define ROUNDUP(a) (1 + (((a) - 1) | (sizeof(long) - 1)))
  260. if (socksize < sizeof(*sdl))
  261. socksize = sizeof(*sdl);
  262. socksize = ROUNDUP(socksize);
  263. sdl = malloc(socksize, M_IFADDR, M_WAITOK|M_ZERO);
  264. sdl->sdl_len = socksize;
  265. sdl->sdl_family = AF_LINK;
  266. bcopy(ifp->if_xname, sdl->sdl_data, namelen);
  267. sdl->sdl_nlen = namelen;
  268. sdl->sdl_alen = ifp->if_addrlen;
  269. sdl->sdl_index = ifp->if_index;
  270. sdl->sdl_type = ifp->if_type;
  271. ifp->if_sadl = sdl;
  272. }
  273. /*
  274. * Free the link level name for the specified interface. This is
  275. * a detach helper. This is called from if_detach() or from
  276. * link layer type specific detach functions.
  277. */
  278. void
  279. if_free_sadl(struct ifnet *ifp)
  280. {
  281. free(ifp->if_sadl, M_IFADDR, 0);
  282. ifp->if_sadl = NULL;
  283. }
  284. void
  285. if_attachdomain()
  286. {
  287. struct ifnet *ifp;
  288. int s;
  289. s = splnet();
  290. TAILQ_FOREACH(ifp, &ifnet, if_list)
  291. if_attachdomain1(ifp);
  292. splx(s);
  293. }
  294. void
  295. if_attachdomain1(struct ifnet *ifp)
  296. {
  297. struct domain *dp;
  298. int s;
  299. s = splnet();
  300. /* address family dependent data region */
  301. bzero(ifp->if_afdata, sizeof(ifp->if_afdata));
  302. for (dp = domains; dp; dp = dp->dom_next) {
  303. if (dp->dom_ifattach)
  304. ifp->if_afdata[dp->dom_family] =
  305. (*dp->dom_ifattach)(ifp);
  306. }
  307. splx(s);
  308. }
  309. void
  310. if_attachhead(struct ifnet *ifp)
  311. {
  312. if_attach_common(ifp);
  313. TAILQ_INSERT_HEAD(&ifnet, ifp, if_list);
  314. if_attachsetup(ifp);
  315. }
  316. void
  317. if_attach(struct ifnet *ifp)
  318. {
  319. if_attach_common(ifp);
  320. TAILQ_INSERT_TAIL(&ifnet, ifp, if_list);
  321. if_attachsetup(ifp);
  322. }
  323. void
  324. if_attach_common(struct ifnet *ifp)
  325. {
  326. TAILQ_INIT(&ifp->if_addrlist);
  327. TAILQ_INIT(&ifp->if_maddrlist);
  328. ifp->if_addrhooks = malloc(sizeof(*ifp->if_addrhooks),
  329. M_TEMP, M_WAITOK);
  330. TAILQ_INIT(ifp->if_addrhooks);
  331. ifp->if_linkstatehooks = malloc(sizeof(*ifp->if_linkstatehooks),
  332. M_TEMP, M_WAITOK);
  333. TAILQ_INIT(ifp->if_linkstatehooks);
  334. ifp->if_detachhooks = malloc(sizeof(*ifp->if_detachhooks),
  335. M_TEMP, M_WAITOK);
  336. TAILQ_INIT(ifp->if_detachhooks);
  337. ifp->if_slowtimo = malloc(sizeof(*ifp->if_slowtimo), M_TEMP,
  338. M_WAITOK|M_ZERO);
  339. ifp->if_linkstatetask = malloc(sizeof(*ifp->if_linkstatetask),
  340. M_TEMP, M_WAITOK|M_ZERO);
  341. SLIST_INIT(&ifp->if_inputs);
  342. }
  343. void
  344. if_start(struct ifnet *ifp)
  345. {
  346. splassert(IPL_NET);
  347. if (ifp->if_snd.ifq_len >= min(8, ifp->if_snd.ifq_maxlen) &&
  348. !ISSET(ifp->if_flags, IFF_OACTIVE)) {
  349. if (ISSET(ifp->if_xflags, IFXF_TXREADY)) {
  350. TAILQ_REMOVE(&iftxlist, ifp, if_txlist);
  351. CLR(ifp->if_xflags, IFXF_TXREADY);
  352. }
  353. ifp->if_start(ifp);
  354. return;
  355. }
  356. if (!ISSET(ifp->if_xflags, IFXF_TXREADY)) {
  357. SET(ifp->if_xflags, IFXF_TXREADY);
  358. TAILQ_INSERT_TAIL(&iftxlist, ifp, if_txlist);
  359. schednetisr(NETISR_TX);
  360. }
  361. }
  362. int
  363. if_enqueue(struct ifnet *ifp, struct mbuf *m)
  364. {
  365. int s, length, error = 0;
  366. unsigned short mflags;
  367. #if NBRIDGE > 0
  368. if (ifp->if_bridgeport && (m->m_flags & M_PROTO1) == 0)
  369. return (bridge_output(ifp, m, NULL, NULL));
  370. m->m_flags &= ~M_PROTO1; /* Loop prevention */
  371. #endif
  372. length = m->m_pkthdr.len;
  373. mflags = m->m_flags;
  374. s = splnet();
  375. /*
  376. * Queue message on interface, and start output if interface
  377. * not yet active.
  378. */
  379. IFQ_ENQUEUE(&ifp->if_snd, m, NULL, error);
  380. if (error) {
  381. splx(s);
  382. return (error);
  383. }
  384. ifp->if_obytes += length;
  385. if (mflags & M_MCAST)
  386. ifp->if_omcasts++;
  387. if_start(ifp);
  388. splx(s);
  389. return (0);
  390. }
  391. struct mbuf_queue if_input_queue = MBUF_QUEUE_INITIALIZER(8192, IPL_NET);
  392. struct task if_input_task = TASK_INITIALIZER(if_input_process, &if_input_queue);
  393. void
  394. if_input(struct ifnet *ifp, struct mbuf_list *ml)
  395. {
  396. struct mbuf *m;
  397. size_t ibytes = 0;
  398. MBUF_LIST_FOREACH(ml, m) {
  399. m->m_pkthdr.ph_ifidx = ifp->if_index;
  400. m->m_pkthdr.ph_rtableid = ifp->if_rdomain;
  401. ibytes += m->m_pkthdr.len;
  402. }
  403. ifp->if_ipackets += ml_len(ml);
  404. ifp->if_ibytes += ibytes;
  405. #if NBPFILTER > 0
  406. if (ifp->if_bpf) {
  407. KERNEL_LOCK();
  408. MBUF_LIST_FOREACH(ml, m)
  409. bpf_mtap_ether(ifp->if_bpf, m, BPF_DIRECTION_IN);
  410. KERNEL_UNLOCK();
  411. }
  412. #endif
  413. mq_enlist(&if_input_queue, ml);
  414. task_add(softnettq, &if_input_task);
  415. }
  416. void
  417. if_input_process(void *xmq)
  418. {
  419. struct mbuf_queue *mq = xmq;
  420. struct mbuf_list ml;
  421. struct mbuf *m;
  422. struct ifnet *ifp;
  423. struct ifih *ifih;
  424. int s;
  425. mq_delist(mq, &ml);
  426. if (ml_empty(&ml))
  427. return;
  428. add_net_randomness(ml_len(&ml));
  429. KERNEL_LOCK();
  430. s = splnet();
  431. while ((m = ml_dequeue(&ml)) != NULL) {
  432. sched_pause();
  433. ifp = if_get(m->m_pkthdr.ph_ifidx);
  434. if (ifp == NULL) {
  435. m_freem(m);
  436. continue;
  437. }
  438. #if NBRIDGE > 0
  439. if (ifp->if_bridgeport && (m->m_flags & M_PROTO1) == 0) {
  440. m = bridge_input(ifp, m);
  441. if (m == NULL)
  442. continue;
  443. }
  444. m->m_flags &= ~M_PROTO1; /* Loop prevention */
  445. #endif
  446. /*
  447. * Pass this mbuf to all input handlers of its
  448. * interface until it is consumed.
  449. */
  450. SLIST_FOREACH(ifih, &ifp->if_inputs, ifih_next) {
  451. if ((*ifih->ifih_input)(ifp, m))
  452. break;
  453. }
  454. if (ifih == NULL)
  455. m_freem(m);
  456. }
  457. splx(s);
  458. KERNEL_UNLOCK();
  459. }
  460. void
  461. nettxintr(void)
  462. {
  463. struct ifnet *ifp;
  464. int s;
  465. s = splnet();
  466. while ((ifp = TAILQ_FIRST(&iftxlist)) != NULL) {
  467. TAILQ_REMOVE(&iftxlist, ifp, if_txlist);
  468. CLR(ifp->if_xflags, IFXF_TXREADY);
  469. ifp->if_start(ifp);
  470. }
  471. splx(s);
  472. }
  473. void
  474. if_deactivate(struct ifnet *ifp)
  475. {
  476. int s;
  477. s = splnet();
  478. /*
  479. * Call detach hooks from head to tail. To make sure detach
  480. * hooks are executed in the reverse order they were added, all
  481. * the hooks have to be added to the head!
  482. */
  483. dohooks(ifp->if_detachhooks, HOOK_REMOVE | HOOK_FREE);
  484. #if NBRIDGE > 0
  485. /* Remove the interface from any bridge it is part of. */
  486. if (ifp->if_bridgeport)
  487. bridge_ifdetach(ifp);
  488. #endif
  489. #if NCARP > 0
  490. /* Remove the interface from any carp group it is a part of. */
  491. if (ifp->if_carp && ifp->if_type != IFT_CARP)
  492. carp_ifdetach(ifp);
  493. #endif
  494. splx(s);
  495. }
  496. /*
  497. * Detach an interface from everything in the kernel. Also deallocate
  498. * private resources.
  499. */
  500. void
  501. if_detach(struct ifnet *ifp)
  502. {
  503. struct ifaddr *ifa;
  504. struct ifg_list *ifg;
  505. struct domain *dp;
  506. int s;
  507. /* Undo pseudo-driver changes. */
  508. if_deactivate(ifp);
  509. s = splnet();
  510. ifp->if_flags &= ~IFF_OACTIVE;
  511. ifp->if_start = if_detached_start;
  512. ifp->if_ioctl = if_detached_ioctl;
  513. ifp->if_watchdog = NULL;
  514. /* Remove the watchdog timeout */
  515. timeout_del(ifp->if_slowtimo);
  516. /* Remove the link state task */
  517. task_del(systq, ifp->if_linkstatetask);
  518. #if NBPFILTER > 0
  519. bpfdetach(ifp);
  520. #endif
  521. rt_if_remove(ifp);
  522. rti_delete(ifp);
  523. #if NETHER > 0 && defined(NFSCLIENT)
  524. if (ifp == revarp_ifp)
  525. revarp_ifp = NULL;
  526. #endif
  527. #ifdef MROUTING
  528. vif_delete(ifp);
  529. #endif
  530. in_ifdetach(ifp);
  531. #ifdef INET6
  532. in6_ifdetach(ifp);
  533. #endif
  534. #if NPF > 0
  535. pfi_detach_ifnet(ifp);
  536. #endif
  537. /* Remove the interface from the list of all interfaces. */
  538. TAILQ_REMOVE(&ifnet, ifp, if_list);
  539. if (ISSET(ifp->if_xflags, IFXF_TXREADY))
  540. TAILQ_REMOVE(&iftxlist, ifp, if_txlist);
  541. while ((ifg = TAILQ_FIRST(&ifp->if_groups)) != NULL)
  542. if_delgroup(ifp, ifg->ifgl_group->ifg_group);
  543. if_free_sadl(ifp);
  544. /* We should not have any address left at this point. */
  545. if (!TAILQ_EMPTY(&ifp->if_addrlist)) {
  546. #ifdef DIAGNOSTIC
  547. printf("%s: address list non empty\n", ifp->if_xname);
  548. #endif
  549. while ((ifa = TAILQ_FIRST(&ifp->if_addrlist)) != NULL) {
  550. ifa_del(ifp, ifa);
  551. ifa->ifa_ifp = NULL;
  552. ifafree(ifa);
  553. }
  554. }
  555. free(ifp->if_addrhooks, M_TEMP, 0);
  556. free(ifp->if_linkstatehooks, M_TEMP, 0);
  557. free(ifp->if_detachhooks, M_TEMP, 0);
  558. free(ifp->if_slowtimo, M_TEMP, sizeof(*ifp->if_slowtimo));
  559. free(ifp->if_linkstatetask, M_TEMP, sizeof(*ifp->if_linkstatetask));
  560. for (dp = domains; dp; dp = dp->dom_next) {
  561. if (dp->dom_ifdetach && ifp->if_afdata[dp->dom_family])
  562. (*dp->dom_ifdetach)(ifp,
  563. ifp->if_afdata[dp->dom_family]);
  564. }
  565. /* Announce that the interface is gone. */
  566. rt_ifannouncemsg(ifp, IFAN_DEPARTURE);
  567. ifindex2ifnet[ifp->if_index] = NULL;
  568. splx(s);
  569. }
  570. /*
  571. * Create a clone network interface.
  572. */
  573. int
  574. if_clone_create(const char *name)
  575. {
  576. struct if_clone *ifc;
  577. struct ifnet *ifp;
  578. int unit, ret;
  579. ifc = if_clone_lookup(name, &unit);
  580. if (ifc == NULL)
  581. return (EINVAL);
  582. if (ifunit(name) != NULL)
  583. return (EEXIST);
  584. if ((ret = (*ifc->ifc_create)(ifc, unit)) == 0 &&
  585. (ifp = ifunit(name)) != NULL)
  586. if_addgroup(ifp, ifc->ifc_name);
  587. return (ret);
  588. }
  589. /*
  590. * Destroy a clone network interface.
  591. */
  592. int
  593. if_clone_destroy(const char *name)
  594. {
  595. struct if_clone *ifc;
  596. struct ifnet *ifp;
  597. int s;
  598. ifc = if_clone_lookup(name, NULL);
  599. if (ifc == NULL)
  600. return (EINVAL);
  601. ifp = ifunit(name);
  602. if (ifp == NULL)
  603. return (ENXIO);
  604. if (ifc->ifc_destroy == NULL)
  605. return (EOPNOTSUPP);
  606. if (ifp->if_flags & IFF_UP) {
  607. s = splnet();
  608. if_down(ifp);
  609. splx(s);
  610. }
  611. return ((*ifc->ifc_destroy)(ifp));
  612. }
  613. /*
  614. * Look up a network interface cloner.
  615. */
  616. struct if_clone *
  617. if_clone_lookup(const char *name, int *unitp)
  618. {
  619. struct if_clone *ifc;
  620. const char *cp;
  621. int unit;
  622. /* separate interface name from unit */
  623. for (cp = name;
  624. cp - name < IFNAMSIZ && *cp && (*cp < '0' || *cp > '9');
  625. cp++)
  626. continue;
  627. if (cp == name || cp - name == IFNAMSIZ || !*cp)
  628. return (NULL); /* No name or unit number */
  629. if (cp - name < IFNAMSIZ-1 && *cp == '0' && cp[1] != '\0')
  630. return (NULL); /* unit number 0 padded */
  631. LIST_FOREACH(ifc, &if_cloners, ifc_list) {
  632. if (strlen(ifc->ifc_name) == cp - name &&
  633. !strncmp(name, ifc->ifc_name, cp - name))
  634. break;
  635. }
  636. if (ifc == NULL)
  637. return (NULL);
  638. unit = 0;
  639. while (cp - name < IFNAMSIZ && *cp) {
  640. if (*cp < '0' || *cp > '9' ||
  641. unit > (INT_MAX - (*cp - '0')) / 10) {
  642. /* Bogus unit number. */
  643. return (NULL);
  644. }
  645. unit = (unit * 10) + (*cp++ - '0');
  646. }
  647. if (unitp != NULL)
  648. *unitp = unit;
  649. return (ifc);
  650. }
  651. /*
  652. * Register a network interface cloner.
  653. */
  654. void
  655. if_clone_attach(struct if_clone *ifc)
  656. {
  657. LIST_INSERT_HEAD(&if_cloners, ifc, ifc_list);
  658. if_cloners_count++;
  659. }
  660. /*
  661. * Unregister a network interface cloner.
  662. */
  663. void
  664. if_clone_detach(struct if_clone *ifc)
  665. {
  666. LIST_REMOVE(ifc, ifc_list);
  667. if_cloners_count--;
  668. }
  669. /*
  670. * Provide list of interface cloners to userspace.
  671. */
  672. int
  673. if_clone_list(struct if_clonereq *ifcr)
  674. {
  675. char outbuf[IFNAMSIZ], *dst;
  676. struct if_clone *ifc;
  677. int count, error = 0;
  678. ifcr->ifcr_total = if_cloners_count;
  679. if ((dst = ifcr->ifcr_buffer) == NULL) {
  680. /* Just asking how many there are. */
  681. return (0);
  682. }
  683. if (ifcr->ifcr_count < 0)
  684. return (EINVAL);
  685. count = (if_cloners_count < ifcr->ifcr_count) ?
  686. if_cloners_count : ifcr->ifcr_count;
  687. LIST_FOREACH(ifc, &if_cloners, ifc_list) {
  688. if (count == 0)
  689. break;
  690. bzero(outbuf, sizeof outbuf);
  691. strlcpy(outbuf, ifc->ifc_name, IFNAMSIZ);
  692. error = copyout(outbuf, dst, IFNAMSIZ);
  693. if (error)
  694. break;
  695. count--;
  696. dst += IFNAMSIZ;
  697. }
  698. return (error);
  699. }
  700. /*
  701. * set queue congestion marker
  702. */
  703. void
  704. if_congestion(void)
  705. {
  706. extern int ticks;
  707. ifq_congestion = ticks;
  708. }
  709. int
  710. if_congested(void)
  711. {
  712. extern int ticks;
  713. return (ticks - ifq_congestion <= (hz / 100));
  714. }
  715. #define equal(a1, a2) \
  716. (bcmp((caddr_t)(a1), (caddr_t)(a2), \
  717. ((struct sockaddr *)(a1))->sa_len) == 0)
  718. /*
  719. * Locate an interface based on a complete address.
  720. */
  721. struct ifaddr *
  722. ifa_ifwithaddr(struct sockaddr *addr, u_int rtableid)
  723. {
  724. struct ifnet *ifp;
  725. struct ifaddr *ifa;
  726. u_int rdomain;
  727. rdomain = rtable_l2(rtableid);
  728. TAILQ_FOREACH(ifp, &ifnet, if_list) {
  729. if (ifp->if_rdomain != rdomain)
  730. continue;
  731. TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) {
  732. if (ifa->ifa_addr->sa_family != addr->sa_family)
  733. continue;
  734. if (equal(addr, ifa->ifa_addr))
  735. return (ifa);
  736. /* IPv6 doesn't have broadcast */
  737. if ((ifp->if_flags & IFF_BROADCAST) &&
  738. ifa->ifa_broadaddr &&
  739. ifa->ifa_broadaddr->sa_len != 0 &&
  740. equal(ifa->ifa_broadaddr, addr))
  741. return (ifa);
  742. }
  743. }
  744. return (NULL);
  745. }
  746. /*
  747. * Locate the point to point interface with a given destination address.
  748. */
  749. /*ARGSUSED*/
  750. struct ifaddr *
  751. ifa_ifwithdstaddr(struct sockaddr *addr, u_int rdomain)
  752. {
  753. struct ifnet *ifp;
  754. struct ifaddr *ifa;
  755. rdomain = rtable_l2(rdomain);
  756. TAILQ_FOREACH(ifp, &ifnet, if_list) {
  757. if (ifp->if_rdomain != rdomain)
  758. continue;
  759. if (ifp->if_flags & IFF_POINTOPOINT)
  760. TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) {
  761. if (ifa->ifa_addr->sa_family !=
  762. addr->sa_family || ifa->ifa_dstaddr == NULL)
  763. continue;
  764. if (equal(addr, ifa->ifa_dstaddr))
  765. return (ifa);
  766. }
  767. }
  768. return (NULL);
  769. }
  770. /*
  771. * Find an interface on a specific network. If many, choice
  772. * is most specific found.
  773. */
  774. struct ifaddr *
  775. ifa_ifwithnet(struct sockaddr *sa, u_int rtableid)
  776. {
  777. struct ifnet *ifp;
  778. struct ifaddr *ifa, *ifa_maybe = NULL;
  779. char *cplim, *addr_data = sa->sa_data;
  780. u_int rdomain;
  781. rdomain = rtable_l2(rtableid);
  782. TAILQ_FOREACH(ifp, &ifnet, if_list) {
  783. if (ifp->if_rdomain != rdomain)
  784. continue;
  785. TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) {
  786. char *cp, *cp2, *cp3;
  787. if (ifa->ifa_addr->sa_family != sa->sa_family ||
  788. ifa->ifa_netmask == 0)
  789. next: continue;
  790. cp = addr_data;
  791. cp2 = ifa->ifa_addr->sa_data;
  792. cp3 = ifa->ifa_netmask->sa_data;
  793. cplim = (char *)ifa->ifa_netmask +
  794. ifa->ifa_netmask->sa_len;
  795. while (cp3 < cplim)
  796. if ((*cp++ ^ *cp2++) & *cp3++)
  797. /* want to continue for() loop */
  798. goto next;
  799. if (ifa_maybe == 0 ||
  800. rn_refines((caddr_t)ifa->ifa_netmask,
  801. (caddr_t)ifa_maybe->ifa_netmask))
  802. ifa_maybe = ifa;
  803. }
  804. }
  805. return (ifa_maybe);
  806. }
  807. /*
  808. * Find an interface address specific to an interface best matching
  809. * a given address.
  810. */
  811. struct ifaddr *
  812. ifaof_ifpforaddr(struct sockaddr *addr, struct ifnet *ifp)
  813. {
  814. struct ifaddr *ifa;
  815. char *cp, *cp2, *cp3;
  816. char *cplim;
  817. struct ifaddr *ifa_maybe = NULL;
  818. u_int af = addr->sa_family;
  819. if (af >= AF_MAX)
  820. return (NULL);
  821. TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) {
  822. if (ifa->ifa_addr->sa_family != af)
  823. continue;
  824. if (ifa_maybe == NULL)
  825. ifa_maybe = ifa;
  826. if (ifa->ifa_netmask == 0 || ifp->if_flags & IFF_POINTOPOINT) {
  827. if (equal(addr, ifa->ifa_addr) ||
  828. (ifa->ifa_dstaddr && equal(addr, ifa->ifa_dstaddr)))
  829. return (ifa);
  830. continue;
  831. }
  832. cp = addr->sa_data;
  833. cp2 = ifa->ifa_addr->sa_data;
  834. cp3 = ifa->ifa_netmask->sa_data;
  835. cplim = ifa->ifa_netmask->sa_len + (char *)ifa->ifa_netmask;
  836. for (; cp3 < cplim; cp3++)
  837. if ((*cp++ ^ *cp2++) & *cp3)
  838. break;
  839. if (cp3 == cplim)
  840. return (ifa);
  841. }
  842. return (ifa_maybe);
  843. }
  844. /*
  845. * Default action when installing a route with a Link Level gateway.
  846. * Lookup an appropriate real ifa to point to.
  847. * This should be moved to /sys/net/link.c eventually.
  848. */
  849. void
  850. link_rtrequest(int cmd, struct rtentry *rt)
  851. {
  852. struct ifaddr *ifa;
  853. struct sockaddr *dst;
  854. struct ifnet *ifp;
  855. if (cmd != RTM_ADD || ((ifa = rt->rt_ifa) == 0) ||
  856. ((ifp = ifa->ifa_ifp) == 0) || ((dst = rt_key(rt)) == 0))
  857. return;
  858. if ((ifa = ifaof_ifpforaddr(dst, ifp)) != NULL) {
  859. ifa->ifa_refcnt++;
  860. ifafree(rt->rt_ifa);
  861. rt->rt_ifa = ifa;
  862. if (ifa->ifa_rtrequest && ifa->ifa_rtrequest != link_rtrequest)
  863. ifa->ifa_rtrequest(cmd, rt);
  864. }
  865. }
  866. /*
  867. * Default action when installing a local route on a point-to-point
  868. * interface.
  869. */
  870. void
  871. p2p_rtrequest(int req, struct rtentry *rt)
  872. {
  873. struct ifnet *ifp = rt->rt_ifp;
  874. struct ifaddr *ifa, *lo0ifa;
  875. switch (req) {
  876. case RTM_ADD:
  877. if ((rt->rt_flags & RTF_LOCAL) == 0)
  878. break;
  879. TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) {
  880. if (memcmp(rt_key(rt), ifa->ifa_addr,
  881. rt_key(rt)->sa_len) == 0)
  882. break;
  883. }
  884. if (ifa == NULL)
  885. break;
  886. /*
  887. * XXX Since lo0 is in the default rdomain we should not
  888. * (ab)use it for any route related to an interface of a
  889. * different rdomain.
  890. */
  891. TAILQ_FOREACH(lo0ifa, &lo0ifp->if_addrlist, ifa_list)
  892. if (lo0ifa->ifa_addr->sa_family ==
  893. ifa->ifa_addr->sa_family)
  894. break;
  895. if (lo0ifa == NULL)
  896. break;
  897. rt->rt_ifp = lo0ifp;
  898. rt->rt_flags &= ~RTF_LLINFO;
  899. /*
  900. * make sure to set rt->rt_ifa to the interface
  901. * address we are using, otherwise we will have trouble
  902. * with source address selection.
  903. */
  904. if (ifa != rt->rt_ifa) {
  905. ifafree(rt->rt_ifa);
  906. ifa->ifa_refcnt++;
  907. rt->rt_ifa = ifa;
  908. }
  909. break;
  910. case RTM_DELETE:
  911. case RTM_RESOLVE:
  912. default:
  913. break;
  914. }
  915. }
  916. /*
  917. * Bring down all interfaces
  918. */
  919. void
  920. if_downall(void)
  921. {
  922. struct ifreq ifrq; /* XXX only partly built */
  923. struct ifnet *ifp;
  924. int s;
  925. s = splnet();
  926. TAILQ_FOREACH(ifp, &ifnet, if_list) {
  927. if ((ifp->if_flags & IFF_UP) == 0)
  928. continue;
  929. if_down(ifp);
  930. ifp->if_flags &= ~IFF_UP;
  931. if (ifp->if_ioctl) {
  932. ifrq.ifr_flags = ifp->if_flags;
  933. (void) (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS,
  934. (caddr_t)&ifrq);
  935. }
  936. }
  937. splx(s);
  938. }
  939. /*
  940. * Mark an interface down and notify protocols of
  941. * the transition.
  942. * NOTE: must be called at splsoftnet or equivalent.
  943. */
  944. void
  945. if_down(struct ifnet *ifp)
  946. {
  947. struct ifaddr *ifa;
  948. splsoftassert(IPL_SOFTNET);
  949. ifp->if_flags &= ~IFF_UP;
  950. microtime(&ifp->if_lastchange);
  951. TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) {
  952. pfctlinput(PRC_IFDOWN, ifa->ifa_addr);
  953. }
  954. IFQ_PURGE(&ifp->if_snd);
  955. #if NCARP > 0
  956. if (ifp->if_carp)
  957. carp_carpdev_state(ifp);
  958. #endif
  959. #if NBRIDGE > 0
  960. if (ifp->if_bridgeport)
  961. bstp_ifstate(ifp);
  962. #endif
  963. rt_ifmsg(ifp);
  964. #ifndef SMALL_KERNEL
  965. rt_if_track(ifp);
  966. #endif
  967. }
  968. /*
  969. * Mark an interface up and notify protocols of
  970. * the transition.
  971. * NOTE: must be called at splsoftnet or equivalent.
  972. */
  973. void
  974. if_up(struct ifnet *ifp)
  975. {
  976. splsoftassert(IPL_SOFTNET);
  977. ifp->if_flags |= IFF_UP;
  978. microtime(&ifp->if_lastchange);
  979. #if NCARP > 0
  980. if (ifp->if_carp)
  981. carp_carpdev_state(ifp);
  982. #endif
  983. #if NBRIDGE > 0
  984. if (ifp->if_bridgeport)
  985. bstp_ifstate(ifp);
  986. #endif
  987. rt_ifmsg(ifp);
  988. #ifdef INET6
  989. /* Userland expects the kernel to set ::1 on lo0. */
  990. if (ifp == lo0ifp)
  991. in6_ifattach(ifp);
  992. #endif
  993. #ifndef SMALL_KERNEL
  994. rt_if_track(ifp);
  995. #endif
  996. }
  997. /*
  998. * Schedule a link state change task.
  999. */
  1000. void
  1001. if_link_state_change(struct ifnet *ifp)
  1002. {
  1003. /* put the routing table update task on systq */
  1004. task_add(systq, ifp->if_linkstatetask);
  1005. }
  1006. /*
  1007. * Process a link state change.
  1008. */
  1009. void
  1010. if_link_state_change_task(void *arg)
  1011. {
  1012. struct ifnet *ifp = arg;
  1013. int s;
  1014. s = splsoftnet();
  1015. rt_ifmsg(ifp);
  1016. #ifndef SMALL_KERNEL
  1017. rt_if_track(ifp);
  1018. #endif
  1019. dohooks(ifp->if_linkstatehooks, 0);
  1020. splx(s);
  1021. }
  1022. /*
  1023. * Handle interface watchdog timer routine. Called
  1024. * from softclock, we decrement timer (if set) and
  1025. * call the appropriate interface routine on expiration.
  1026. */
  1027. void
  1028. if_slowtimo(void *arg)
  1029. {
  1030. struct ifnet *ifp = arg;
  1031. int s = splnet();
  1032. if (ifp->if_watchdog) {
  1033. if (ifp->if_timer > 0 && --ifp->if_timer == 0)
  1034. (*ifp->if_watchdog)(ifp);
  1035. timeout_add(ifp->if_slowtimo, hz / IFNET_SLOWHZ);
  1036. }
  1037. splx(s);
  1038. }
  1039. /*
  1040. * Map interface name to interface structure pointer.
  1041. */
  1042. struct ifnet *
  1043. ifunit(const char *name)
  1044. {
  1045. struct ifnet *ifp;
  1046. TAILQ_FOREACH(ifp, &ifnet, if_list) {
  1047. if (strcmp(ifp->if_xname, name) == 0)
  1048. return (ifp);
  1049. }
  1050. return (NULL);
  1051. }
  1052. /*
  1053. * Map interface index to interface structure pointer.
  1054. */
  1055. struct ifnet *
  1056. if_get(unsigned int index)
  1057. {
  1058. struct ifnet *ifp = NULL;
  1059. if (index < if_indexlim)
  1060. ifp = ifindex2ifnet[index];
  1061. return (ifp);
  1062. }
  1063. /*
  1064. * Interface ioctls.
  1065. */
  1066. int
  1067. ifioctl(struct socket *so, u_long cmd, caddr_t data, struct proc *p)
  1068. {
  1069. struct ifnet *ifp;
  1070. struct ifreq *ifr;
  1071. struct sockaddr_dl *sdl;
  1072. struct ifgroupreq *ifgr;
  1073. struct if_afreq *ifar;
  1074. char ifdescrbuf[IFDESCRSIZE];
  1075. char ifrtlabelbuf[RTLABEL_LEN];
  1076. int s, error = 0;
  1077. size_t bytesdone;
  1078. short oif_flags;
  1079. const char *label;
  1080. short up = 0;
  1081. switch (cmd) {
  1082. case SIOCGIFCONF:
  1083. #ifdef COMPAT_LINUX
  1084. case OSIOCGIFCONF:
  1085. #endif
  1086. return (ifconf(cmd, data));
  1087. }
  1088. ifr = (struct ifreq *)data;
  1089. switch (cmd) {
  1090. case SIOCIFCREATE:
  1091. case SIOCIFDESTROY:
  1092. if ((error = suser(p, 0)) != 0)
  1093. return (error);
  1094. return ((cmd == SIOCIFCREATE) ?
  1095. if_clone_create(ifr->ifr_name) :
  1096. if_clone_destroy(ifr->ifr_name));
  1097. case SIOCIFGCLONERS:
  1098. return (if_clone_list((struct if_clonereq *)data));
  1099. case SIOCGIFGMEMB:
  1100. return (if_getgroupmembers(data));
  1101. case SIOCGIFGATTR:
  1102. return (if_getgroupattribs(data));
  1103. case SIOCSIFGATTR:
  1104. if ((error = suser(p, 0)) != 0)
  1105. return (error);
  1106. return (if_setgroupattribs(data));
  1107. case SIOCIFAFATTACH:
  1108. case SIOCIFAFDETACH:
  1109. if ((error = suser(p, 0)) != 0)
  1110. return (error);
  1111. ifar = (struct if_afreq *)data;
  1112. if ((ifp = ifunit(ifar->ifar_name)) == NULL)
  1113. return (ENXIO);
  1114. switch (ifar->ifar_af) {
  1115. case AF_INET:
  1116. /* attach is a noop for AF_INET */
  1117. if (cmd == SIOCIFAFDETACH) {
  1118. s = splsoftnet();
  1119. in_ifdetach(ifp);
  1120. splx(s);
  1121. }
  1122. return (0);
  1123. #ifdef INET6
  1124. case AF_INET6:
  1125. s = splsoftnet();
  1126. if (cmd == SIOCIFAFATTACH)
  1127. in6_ifattach(ifp);
  1128. else
  1129. in6_ifdetach(ifp);
  1130. splx(s);
  1131. return (0);
  1132. #endif /* INET6 */
  1133. default:
  1134. return (EAFNOSUPPORT);
  1135. }
  1136. }
  1137. ifp = ifunit(ifr->ifr_name);
  1138. if (ifp == 0)
  1139. return (ENXIO);
  1140. oif_flags = ifp->if_flags;
  1141. switch (cmd) {
  1142. case SIOCGIFFLAGS:
  1143. ifr->ifr_flags = ifp->if_flags;
  1144. break;
  1145. case SIOCGIFXFLAGS:
  1146. ifr->ifr_flags = ifp->if_xflags;
  1147. break;
  1148. case SIOCGIFMETRIC:
  1149. ifr->ifr_metric = ifp->if_metric;
  1150. break;
  1151. case SIOCGIFMTU:
  1152. ifr->ifr_mtu = ifp->if_mtu;
  1153. break;
  1154. case SIOCGIFHARDMTU:
  1155. ifr->ifr_hardmtu = ifp->if_hardmtu;
  1156. break;
  1157. case SIOCGIFDATA:
  1158. error = copyout((caddr_t)&ifp->if_data, ifr->ifr_data,
  1159. sizeof(ifp->if_data));
  1160. break;
  1161. case SIOCSIFFLAGS:
  1162. if ((error = suser(p, 0)) != 0)
  1163. return (error);
  1164. if (ifp->if_flags & IFF_UP && (ifr->ifr_flags & IFF_UP) == 0) {
  1165. s = splnet();
  1166. if_down(ifp);
  1167. splx(s);
  1168. }
  1169. if (ifr->ifr_flags & IFF_UP && (ifp->if_flags & IFF_UP) == 0) {
  1170. s = splnet();
  1171. if_up(ifp);
  1172. splx(s);
  1173. }
  1174. ifp->if_flags = (ifp->if_flags & IFF_CANTCHANGE) |
  1175. (ifr->ifr_flags & ~IFF_CANTCHANGE);
  1176. if (ifp->if_ioctl)
  1177. (void) (*ifp->if_ioctl)(ifp, cmd, data);
  1178. break;
  1179. case SIOCSIFXFLAGS:
  1180. if ((error = suser(p, 0)) != 0)
  1181. return (error);
  1182. #ifdef INET6
  1183. if (ISSET(ifr->ifr_flags, IFXF_AUTOCONF6)) {
  1184. s = splsoftnet();
  1185. in6_ifattach(ifp);
  1186. splx(s);
  1187. }
  1188. if ((ifr->ifr_flags & IFXF_AUTOCONF6) &&
  1189. !(ifp->if_xflags & IFXF_AUTOCONF6)) {
  1190. nd6_rs_attach(ifp);
  1191. }
  1192. if ((ifp->if_xflags & IFXF_AUTOCONF6) &&
  1193. !(ifr->ifr_flags & IFXF_AUTOCONF6)) {
  1194. nd6_rs_detach(ifp);
  1195. }
  1196. #endif /* INET6 */
  1197. #ifdef MPLS
  1198. if (ISSET(ifr->ifr_flags, IFXF_MPLS) &&
  1199. !ISSET(ifp->if_xflags, IFXF_MPLS)) {
  1200. s = splnet();
  1201. ifp->if_xflags |= IFXF_MPLS;
  1202. ifp->if_ll_output = ifp->if_output;
  1203. ifp->if_output = mpls_output;
  1204. splx(s);
  1205. }
  1206. if (ISSET(ifp->if_xflags, IFXF_MPLS) &&
  1207. !ISSET(ifr->ifr_flags, IFXF_MPLS)) {
  1208. s = splnet();
  1209. ifp->if_xflags &= ~IFXF_MPLS;
  1210. ifp->if_output = ifp->if_ll_output;
  1211. ifp->if_ll_output = NULL;
  1212. splx(s);
  1213. }
  1214. #endif /* MPLS */
  1215. #ifndef SMALL_KERNEL
  1216. if (ifp->if_capabilities & IFCAP_WOL) {
  1217. if (ISSET(ifr->ifr_flags, IFXF_WOL) &&
  1218. !ISSET(ifp->if_xflags, IFXF_WOL)) {
  1219. s = splnet();
  1220. ifp->if_xflags |= IFXF_WOL;
  1221. error = ifp->if_wol(ifp, 1);
  1222. splx(s);
  1223. if (error)
  1224. return (error);
  1225. }
  1226. if (ISSET(ifp->if_xflags, IFXF_WOL) &&
  1227. !ISSET(ifr->ifr_flags, IFXF_WOL)) {
  1228. s = splnet();
  1229. ifp->if_xflags &= ~IFXF_WOL;
  1230. error = ifp->if_wol(ifp, 0);
  1231. splx(s);
  1232. if (error)
  1233. return (error);
  1234. }
  1235. } else if (ISSET(ifr->ifr_flags, IFXF_WOL)) {
  1236. ifr->ifr_flags &= ~IFXF_WOL;
  1237. error = ENOTSUP;
  1238. }
  1239. #endif
  1240. ifp->if_xflags = (ifp->if_xflags & IFXF_CANTCHANGE) |
  1241. (ifr->ifr_flags & ~IFXF_CANTCHANGE);
  1242. rt_ifmsg(ifp);
  1243. break;
  1244. case SIOCSIFMETRIC:
  1245. if ((error = suser(p, 0)) != 0)
  1246. return (error);
  1247. ifp->if_metric = ifr->ifr_metric;
  1248. break;
  1249. case SIOCSIFMTU:
  1250. {
  1251. #ifdef INET6
  1252. int oldmtu = ifp->if_mtu;
  1253. #endif
  1254. if ((error = suser(p, 0)) != 0)
  1255. return (error);
  1256. if (ifp->if_ioctl == NULL)
  1257. return (EOPNOTSUPP);
  1258. error = (*ifp->if_ioctl)(ifp, cmd, data);
  1259. /*
  1260. * If the link MTU changed, do network layer specific procedure.
  1261. */
  1262. #ifdef INET6
  1263. if (ifp->if_mtu != oldmtu)
  1264. nd6_setmtu(ifp);
  1265. #endif
  1266. break;
  1267. }
  1268. case SIOCSIFPHYADDR:
  1269. case SIOCDIFPHYADDR:
  1270. #ifdef INET6
  1271. case SIOCSIFPHYADDR_IN6:
  1272. #endif
  1273. case SIOCSLIFPHYADDR:
  1274. case SIOCSLIFPHYRTABLE:
  1275. case SIOCADDMULTI:
  1276. case SIOCDELMULTI:
  1277. case SIOCSIFMEDIA:
  1278. if ((error = suser(p, 0)) != 0)
  1279. return (error);
  1280. /* FALLTHROUGH */
  1281. case SIOCGIFPSRCADDR:
  1282. case SIOCGIFPDSTADDR:
  1283. case SIOCGLIFPHYADDR:
  1284. case SIOCGLIFPHYRTABLE:
  1285. case SIOCGIFMEDIA:
  1286. if (ifp->if_ioctl == 0)
  1287. return (EOPNOTSUPP);
  1288. error = (*ifp->if_ioctl)(ifp, cmd, data);
  1289. break;
  1290. case SIOCGIFDESCR:
  1291. strlcpy(ifdescrbuf, ifp->if_description, IFDESCRSIZE);
  1292. error = copyoutstr(ifdescrbuf, ifr->ifr_data, IFDESCRSIZE,
  1293. &bytesdone);
  1294. break;
  1295. case SIOCSIFDESCR:
  1296. if ((error = suser(p, 0)) != 0)
  1297. return (error);
  1298. error = copyinstr(ifr->ifr_data, ifdescrbuf,
  1299. IFDESCRSIZE, &bytesdone);
  1300. if (error == 0) {
  1301. (void)memset(ifp->if_description, 0, IFDESCRSIZE);
  1302. strlcpy(ifp->if_description, ifdescrbuf, IFDESCRSIZE);
  1303. }
  1304. break;
  1305. case SIOCGIFRTLABEL:
  1306. if (ifp->if_rtlabelid &&
  1307. (label = rtlabel_id2name(ifp->if_rtlabelid)) != NULL) {
  1308. strlcpy(ifrtlabelbuf, label, RTLABEL_LEN);
  1309. error = copyoutstr(ifrtlabelbuf, ifr->ifr_data,
  1310. RTLABEL_LEN, &bytesdone);
  1311. } else
  1312. error = ENOENT;
  1313. break;
  1314. case SIOCSIFRTLABEL:
  1315. if ((error = suser(p, 0)) != 0)
  1316. return (error);
  1317. error = copyinstr(ifr->ifr_data, ifrtlabelbuf,
  1318. RTLABEL_LEN, &bytesdone);
  1319. if (error == 0) {
  1320. rtlabel_unref(ifp->if_rtlabelid);
  1321. ifp->if_rtlabelid = rtlabel_name2id(ifrtlabelbuf);
  1322. }
  1323. break;
  1324. case SIOCGIFPRIORITY:
  1325. ifr->ifr_metric = ifp->if_priority;
  1326. break;
  1327. case SIOCSIFPRIORITY:
  1328. if ((error = suser(p, 0)) != 0)
  1329. return (error);
  1330. if (ifr->ifr_metric < 0 || ifr->ifr_metric > 15)
  1331. return (EINVAL);
  1332. ifp->if_priority = ifr->ifr_metric;
  1333. break;
  1334. case SIOCGIFRDOMAIN:
  1335. ifr->ifr_rdomainid = ifp->if_rdomain;
  1336. break;
  1337. case SIOCSIFRDOMAIN:
  1338. if ((error = suser(p, 0)) != 0)
  1339. return (error);
  1340. if (ifr->ifr_rdomainid < 0 ||
  1341. ifr->ifr_rdomainid > RT_TABLEID_MAX)
  1342. return (EINVAL);
  1343. /* make sure that the routing table exists */
  1344. if (!rtable_exists(ifr->ifr_rdomainid)) {
  1345. s = splsoftnet();
  1346. if ((error = rtable_add(ifr->ifr_rdomainid)) == 0)
  1347. rtable_l2set(ifr->ifr_rdomainid, ifr->ifr_rdomainid);
  1348. splx(s);
  1349. if (error)
  1350. return (error);
  1351. }
  1352. /* make sure that the routing table is a real rdomain */
  1353. if (ifr->ifr_rdomainid != rtable_l2(ifr->ifr_rdomainid))
  1354. return (EINVAL);
  1355. /* remove all routing entries when switching domains */
  1356. /* XXX hell this is ugly */
  1357. if (ifr->ifr_rdomainid != ifp->if_rdomain) {
  1358. s = splnet();
  1359. if (ifp->if_flags & IFF_UP)
  1360. up = 1;
  1361. /*
  1362. * We are tearing down the world.
  1363. * Take down the IF so:
  1364. * 1. everything that cares gets a message
  1365. * 2. the automagic IPv6 bits are recreated
  1366. */
  1367. if (up)
  1368. if_down(ifp);
  1369. rt_if_remove(ifp);
  1370. rti_delete(ifp);
  1371. #ifdef MROUTING
  1372. vif_delete(ifp);
  1373. #endif
  1374. #ifdef INET6
  1375. in6_ifdetach(ifp);
  1376. #endif
  1377. in_ifdetach(ifp);
  1378. splx(s);
  1379. }
  1380. /* Let devices like enc(4) or mpe(4) know about the change */
  1381. if ((error = (*ifp->if_ioctl)(ifp, cmd, data)) != ENOTTY)
  1382. return (error);
  1383. error = 0;
  1384. /* Add interface to the specified rdomain */
  1385. ifp->if_rdomain = ifr->ifr_rdomainid;
  1386. break;
  1387. case SIOCAIFGROUP:
  1388. if ((error = suser(p, 0)))
  1389. return (error);
  1390. ifgr = (struct ifgroupreq *)data;
  1391. if ((error = if_addgroup(ifp, ifgr->ifgr_group)))
  1392. return (error);
  1393. (*ifp->if_ioctl)(ifp, cmd, data); /* XXX error check */
  1394. break;
  1395. case SIOCGIFGROUP:
  1396. if ((error = if_getgroup(data, ifp)))
  1397. return (error);
  1398. break;
  1399. case SIOCDIFGROUP:
  1400. if ((error = suser(p, 0)))
  1401. return (error);
  1402. (*ifp->if_ioctl)(ifp, cmd, data); /* XXX error check */
  1403. ifgr = (struct ifgroupreq *)data;
  1404. if ((error = if_delgroup(ifp, ifgr->ifgr_group)))
  1405. return (error);
  1406. break;
  1407. case SIOCSIFLLADDR:
  1408. if ((error = suser(p, 0)))
  1409. return (error);
  1410. sdl = (struct sockaddr_dl *)ifp->if_sadl;
  1411. if (sdl == NULL)
  1412. return (EINVAL);
  1413. if (ifr->ifr_addr.sa_len != ETHER_ADDR_LEN)
  1414. return (EINVAL);
  1415. if (ETHER_IS_MULTICAST(ifr->ifr_addr.sa_data))
  1416. return (EINVAL);
  1417. switch (ifp->if_type) {
  1418. case IFT_ETHER:
  1419. case IFT_CARP:
  1420. case IFT_XETHER:
  1421. case IFT_ISO88025:
  1422. bcopy((caddr_t)ifr->ifr_addr.sa_data,
  1423. (caddr_t)((struct arpcom *)ifp)->ac_enaddr,
  1424. ETHER_ADDR_LEN);
  1425. bcopy((caddr_t)ifr->ifr_addr.sa_data,
  1426. LLADDR(sdl), ETHER_ADDR_LEN);
  1427. error = (*ifp->if_ioctl)(ifp, cmd, data);
  1428. if (error == ENOTTY)
  1429. error = 0;
  1430. break;
  1431. default:
  1432. return (ENODEV);
  1433. }
  1434. ifnewlladdr(ifp);
  1435. break;
  1436. default:
  1437. if (so->so_proto == 0)
  1438. return (EOPNOTSUPP);
  1439. error = ((*so->so_proto->pr_usrreq)(so, PRU_CONTROL,
  1440. (struct mbuf *) cmd, (struct mbuf *) data,
  1441. (struct mbuf *) ifp, p));
  1442. break;
  1443. }
  1444. if (((oif_flags ^ ifp->if_flags) & IFF_UP) != 0)
  1445. microtime(&ifp->if_lastchange);
  1446. /* If we took down the IF, bring it back */
  1447. if (up) {
  1448. s = splnet();
  1449. if_up(ifp);
  1450. splx(s);
  1451. }
  1452. return (error);
  1453. }
  1454. /*
  1455. * Return interface configuration
  1456. * of system. List may be used
  1457. * in later ioctl's (above) to get
  1458. * other information.
  1459. */
  1460. /*ARGSUSED*/
  1461. int
  1462. ifconf(u_long cmd, caddr_t data)
  1463. {
  1464. struct ifconf *ifc = (struct ifconf *)data;
  1465. struct ifnet *ifp;
  1466. struct ifaddr *ifa;
  1467. struct ifreq ifr, *ifrp;
  1468. int space = ifc->ifc_len, error = 0;
  1469. /* If ifc->ifc_len is 0, fill it in with the needed size and return. */
  1470. if (space == 0) {
  1471. TAILQ_FOREACH(ifp, &ifnet, if_list) {
  1472. struct sockaddr *sa;
  1473. if (TAILQ_EMPTY(&ifp->if_addrlist))
  1474. space += sizeof (ifr);
  1475. else
  1476. TAILQ_FOREACH(ifa,
  1477. &ifp->if_addrlist, ifa_list) {
  1478. sa = ifa->ifa_addr;
  1479. #ifdef COMPAT_LINUX
  1480. if (cmd != OSIOCGIFCONF)
  1481. #endif
  1482. if (sa->sa_len > sizeof(*sa))
  1483. space += sa->sa_len -
  1484. sizeof(*sa);
  1485. space += sizeof(ifr);
  1486. }
  1487. }
  1488. ifc->ifc_len = space;
  1489. return (0);
  1490. }
  1491. ifrp = ifc->ifc_req;
  1492. TAILQ_FOREACH(ifp, &ifnet, if_list) {
  1493. if (space < sizeof(ifr))
  1494. break;
  1495. bcopy(ifp->if_xname, ifr.ifr_name, IFNAMSIZ);
  1496. if (TAILQ_EMPTY(&ifp->if_addrlist)) {
  1497. bzero((caddr_t)&ifr.ifr_addr, sizeof(ifr.ifr_addr));
  1498. error = copyout((caddr_t)&ifr, (caddr_t)ifrp,
  1499. sizeof(ifr));
  1500. if (error)
  1501. break;
  1502. space -= sizeof (ifr), ifrp++;
  1503. } else
  1504. TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) {
  1505. struct sockaddr *sa = ifa->ifa_addr;
  1506. if (space < sizeof(ifr))
  1507. break;
  1508. #ifdef COMPAT_LINUX
  1509. if (cmd == OSIOCGIFCONF) {
  1510. ifr.ifr_addr = *sa;
  1511. *(u_int16_t *)&ifr.ifr_addr =
  1512. sa->sa_family;
  1513. error = copyout((caddr_t)&ifr,
  1514. (caddr_t)ifrp, sizeof (ifr));
  1515. ifrp++;
  1516. } else
  1517. #endif
  1518. if (sa->sa_len <= sizeof(*sa)) {
  1519. ifr.ifr_addr = *sa;
  1520. error = copyout((caddr_t)&ifr,
  1521. (caddr_t)ifrp, sizeof (ifr));
  1522. ifrp++;
  1523. } else {
  1524. space -= sa->sa_len - sizeof(*sa);
  1525. if (space < sizeof (ifr))
  1526. break;
  1527. error = copyout((caddr_t)&ifr,
  1528. (caddr_t)ifrp,
  1529. sizeof(ifr.ifr_name));
  1530. if (error == 0)
  1531. error = copyout((caddr_t)sa,
  1532. (caddr_t)&ifrp->ifr_addr,
  1533. sa->sa_len);
  1534. ifrp = (struct ifreq *)(sa->sa_len +
  1535. (caddr_t)&ifrp->ifr_addr);
  1536. }
  1537. if (error)
  1538. break;
  1539. space -= sizeof (ifr);
  1540. }
  1541. }
  1542. ifc->ifc_len -= space;
  1543. return (error);
  1544. }
  1545. /*
  1546. * Dummy functions replaced in ifnet during detach (if protocols decide to
  1547. * fiddle with the if during detach.
  1548. */
  1549. void
  1550. if_detached_start(struct ifnet *ifp)
  1551. {
  1552. IFQ_PURGE(&ifp->if_snd);
  1553. }
  1554. int
  1555. if_detached_ioctl(struct ifnet *ifp, u_long a, caddr_t b)
  1556. {
  1557. return ENODEV;
  1558. }
  1559. /*
  1560. * Create interface group without members
  1561. */
  1562. struct ifg_group *
  1563. if_creategroup(const char *groupname)
  1564. {
  1565. struct ifg_group *ifg;
  1566. if ((ifg = malloc(sizeof(*ifg), M_TEMP, M_NOWAIT)) == NULL)
  1567. return (NULL);
  1568. strlcpy(ifg->ifg_group, groupname, sizeof(ifg->ifg_group));
  1569. ifg->ifg_refcnt = 0;
  1570. ifg->ifg_carp_demoted = 0;
  1571. TAILQ_INIT(&ifg->ifg_members);
  1572. #if NPF > 0
  1573. pfi_attach_ifgroup(ifg);
  1574. #endif
  1575. TAILQ_INSERT_TAIL(&ifg_head, ifg, ifg_next);
  1576. return (ifg);
  1577. }
  1578. /*
  1579. * Add a group to an interface
  1580. */
  1581. int
  1582. if_addgroup(struct ifnet *ifp, const char *groupname)
  1583. {
  1584. struct ifg_list *ifgl;
  1585. struct ifg_group *ifg = NULL;
  1586. struct ifg_member *ifgm;
  1587. if (groupname[0] && groupname[strlen(groupname) - 1] >= '0' &&
  1588. groupname[strlen(groupname) - 1] <= '9')
  1589. return (EINVAL);
  1590. TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next)
  1591. if (!strcmp(ifgl->ifgl_group->ifg_group, groupname))
  1592. return (EEXIST);
  1593. if ((ifgl = malloc(sizeof(*ifgl), M_TEMP, M_NOWAIT)) == NULL)
  1594. return (ENOMEM);
  1595. if ((ifgm = malloc(sizeof(*ifgm), M_TEMP, M_NOWAIT)) == NULL) {
  1596. free(ifgl, M_TEMP, sizeof(*ifgl));
  1597. return (ENOMEM);
  1598. }
  1599. TAILQ_FOREACH(ifg, &ifg_head, ifg_next)
  1600. if (!strcmp(ifg->ifg_group, groupname))
  1601. break;
  1602. if (ifg == NULL && (ifg = if_creategroup(groupname)) == NULL) {
  1603. free(ifgl, M_TEMP, sizeof(*ifgl));
  1604. free(ifgm, M_TEMP, sizeof(*ifgm));
  1605. return (ENOMEM);
  1606. }
  1607. ifg->ifg_refcnt++;
  1608. ifgl->ifgl_group = ifg;
  1609. ifgm->ifgm_ifp = ifp;
  1610. TAILQ_INSERT_TAIL(&ifg->ifg_members, ifgm, ifgm_next);
  1611. TAILQ_INSERT_TAIL(&ifp->if_groups, ifgl, ifgl_next);
  1612. #if NPF > 0
  1613. pfi_group_change(groupname);
  1614. #endif
  1615. return (0);
  1616. }
  1617. /*
  1618. * Remove a group from an interface
  1619. */
  1620. int
  1621. if_delgroup(struct ifnet *ifp, const char *groupname)
  1622. {
  1623. struct ifg_list *ifgl;
  1624. struct ifg_member *ifgm;
  1625. TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next)
  1626. if (!strcmp(ifgl->ifgl_group->ifg_group, groupname))
  1627. break;
  1628. if (ifgl == NULL)
  1629. return (ENOENT);
  1630. TAILQ_REMOVE(&ifp->if_groups, ifgl, ifgl_next);
  1631. TAILQ_FOREACH(ifgm, &ifgl->ifgl_group->ifg_members, ifgm_next)
  1632. if (ifgm->ifgm_ifp == ifp)
  1633. break;
  1634. if (ifgm != NULL) {
  1635. TAILQ_REMOVE(&ifgl->ifgl_group->ifg_members, ifgm, ifgm_next);
  1636. free(ifgm, M_TEMP, sizeof(*ifgm));
  1637. }
  1638. if (--ifgl->ifgl_group->ifg_refcnt == 0) {
  1639. TAILQ_REMOVE(&ifg_head, ifgl->ifgl_group, ifg_next);
  1640. #if NPF > 0
  1641. pfi_detach_ifgroup(ifgl->ifgl_group);
  1642. #endif
  1643. free(ifgl->ifgl_group, M_TEMP, 0);
  1644. }
  1645. free(ifgl, M_TEMP, sizeof(*ifgl));
  1646. #if NPF > 0
  1647. pfi_group_change(groupname);
  1648. #endif
  1649. return (0);
  1650. }
  1651. /*
  1652. * Stores all groups from an interface in memory pointed
  1653. * to by data
  1654. */
  1655. int
  1656. if_getgroup(caddr_t data, struct ifnet *ifp)
  1657. {
  1658. int len, error;
  1659. struct ifg_list *ifgl;
  1660. struct ifg_req ifgrq, *ifgp;
  1661. struct ifgroupreq *ifgr = (struct ifgroupreq *)data;
  1662. if (ifgr->ifgr_len == 0) {
  1663. TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next)
  1664. ifgr->ifgr_len += sizeof(struct ifg_req);
  1665. return (0);
  1666. }
  1667. len = ifgr->ifgr_len;
  1668. ifgp = ifgr->ifgr_groups;
  1669. TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next) {
  1670. if (len < sizeof(ifgrq))
  1671. return (EINVAL);
  1672. bzero(&ifgrq, sizeof ifgrq);
  1673. strlcpy(ifgrq.ifgrq_group, ifgl->ifgl_group->ifg_group,
  1674. sizeof(ifgrq.ifgrq_group));
  1675. if ((error = copyout((caddr_t)&ifgrq, (caddr_t)ifgp,
  1676. sizeof(struct ifg_req))))
  1677. return (error);
  1678. len -= sizeof(ifgrq);
  1679. ifgp++;
  1680. }
  1681. return (0);
  1682. }
  1683. /*
  1684. * Stores all members of a group in memory pointed to by data
  1685. */
  1686. int
  1687. if_getgroupmembers(caddr_t data)
  1688. {
  1689. struct ifgroupreq *ifgr = (struct ifgroupreq *)data;
  1690. struct ifg_group *ifg;
  1691. struct ifg_member *ifgm;
  1692. struct ifg_req ifgrq, *ifgp;
  1693. int len, error;
  1694. TAILQ_FOREACH(ifg, &ifg_head, ifg_next)
  1695. if (!strcmp(ifg->ifg_group, ifgr->ifgr_name))
  1696. break;
  1697. if (ifg == NULL)
  1698. return (ENOENT);
  1699. if (ifgr->ifgr_len == 0) {
  1700. TAILQ_FOREACH(ifgm, &ifg->ifg_members, ifgm_next)
  1701. ifgr->ifgr_len += sizeof(ifgrq);
  1702. return (0);
  1703. }
  1704. len = ifgr->ifgr_len;
  1705. ifgp = ifgr->ifgr_groups;
  1706. TAILQ_FOREACH(ifgm, &ifg->ifg_members, ifgm_next) {
  1707. if (len < sizeof(ifgrq))
  1708. return (EINVAL);
  1709. bzero(&ifgrq, sizeof ifgrq);
  1710. strlcpy(ifgrq.ifgrq_member, ifgm->ifgm_ifp->if_xname,
  1711. sizeof(ifgrq.ifgrq_member));
  1712. if ((error = copyout((caddr_t)&ifgrq, (caddr_t)ifgp,
  1713. sizeof(struct ifg_req))))
  1714. return (error);
  1715. len -= sizeof(ifgrq);
  1716. ifgp++;
  1717. }
  1718. return (0);
  1719. }
  1720. int
  1721. if_getgroupattribs(caddr_t data)
  1722. {
  1723. struct ifgroupreq *ifgr = (struct ifgroupreq *)data;
  1724. struct ifg_group *ifg;
  1725. TAILQ_FOREACH(ifg, &ifg_head, ifg_next)
  1726. if (!strcmp(ifg->ifg_group, ifgr->ifgr_name))
  1727. break;
  1728. if (ifg == NULL)
  1729. return (ENOENT);
  1730. ifgr->ifgr_attrib.ifg_carp_demoted = ifg->ifg_carp_demoted;
  1731. return (0);
  1732. }
  1733. int
  1734. if_setgroupattribs(caddr_t data)
  1735. {
  1736. struct ifgroupreq *ifgr = (struct ifgroupreq *)data;
  1737. struct ifg_group *ifg;
  1738. struct ifg_member *ifgm;
  1739. int demote;
  1740. TAILQ_FOREACH(ifg, &ifg_head, ifg_next)
  1741. if (!strcmp(ifg->ifg_group, ifgr->ifgr_name))
  1742. break;
  1743. if (ifg == NULL)
  1744. return (ENOENT);
  1745. demote = ifgr->ifgr_attrib.ifg_carp_demoted;
  1746. if (demote + ifg->ifg_carp_demoted > 0xff ||
  1747. demote + ifg->ifg_carp_demoted < 0)
  1748. return (EINVAL);
  1749. ifg->ifg_carp_demoted += demote;
  1750. TAILQ_FOREACH(ifgm, &ifg->ifg_members, ifgm_next)
  1751. if (ifgm->ifgm_ifp->if_ioctl)
  1752. ifgm->ifgm_ifp->if_ioctl(ifgm->ifgm_ifp,
  1753. SIOCSIFGATTR, data);
  1754. return (0);
  1755. }
  1756. void
  1757. if_group_routechange(struct sockaddr *dst, struct sockaddr *mask)
  1758. {
  1759. switch (dst->sa_family) {
  1760. case AF_INET:
  1761. if (satosin(dst)->sin_addr.s_addr == INADDR_ANY &&
  1762. mask && (mask->sa_len == 0 ||
  1763. satosin(mask)->sin_addr.s_addr == INADDR_ANY))
  1764. if_group_egress_build();
  1765. break;
  1766. #ifdef INET6
  1767. case AF_INET6:
  1768. if (IN6_ARE_ADDR_EQUAL(&(satosin6(dst))->sin6_addr,
  1769. &in6addr_any) && mask && (mask->sa_len == 0 ||
  1770. IN6_ARE_ADDR_EQUAL(&(satosin6(mask))->sin6_addr,
  1771. &in6addr_any)))
  1772. if_group_egress_build();
  1773. break;
  1774. #endif
  1775. }
  1776. }
  1777. int
  1778. if_group_egress_build(void)
  1779. {
  1780. struct ifg_group *ifg;
  1781. struct ifg_member *ifgm, *next;
  1782. struct sockaddr_in sa_in;
  1783. #ifdef INET6
  1784. struct sockaddr_in6 sa_in6;
  1785. #endif
  1786. struct rtentry *rt;
  1787. TAILQ_FOREACH(ifg, &ifg_head, ifg_next)
  1788. if (!strcmp(ifg->ifg_group, IFG_EGRESS))
  1789. break;
  1790. if (ifg != NULL)
  1791. TAILQ_FOREACH_SAFE(ifgm, &ifg->ifg_members, ifgm_next, next)
  1792. if_delgroup(ifgm->ifgm_ifp, IFG_EGRESS);
  1793. bzero(&sa_in, sizeof(sa_in));
  1794. sa_in.sin_len = sizeof(sa_in);
  1795. sa_in.sin_family = AF_INET;
  1796. rt = rtable_lookup(0, sintosa(&sa_in), sintosa(&sa_in));
  1797. if (rt != NULL) {
  1798. do {
  1799. if (rt->rt_ifp)
  1800. if_addgroup(rt->rt_ifp, IFG_EGRESS);
  1801. #ifndef SMALL_KERNEL
  1802. rt = rt_mpath_next(rt);
  1803. #else
  1804. rt = NULL;
  1805. #endif
  1806. } while (rt != NULL);
  1807. }
  1808. #ifdef INET6
  1809. bcopy(&sa6_any, &sa_in6, sizeof(sa_in6));
  1810. rt = rtable_lookup(0, sin6tosa(&sa_in6), sin6tosa(&sa_in6));
  1811. if (rt != NULL) {
  1812. do {
  1813. if (rt->rt_ifp)
  1814. if_addgroup(rt->rt_ifp, IFG_EGRESS);
  1815. #ifndef SMALL_KERNEL
  1816. rt = rt_mpath_next(rt);
  1817. #else
  1818. rt = NULL;
  1819. #endif
  1820. } while (rt != NULL);
  1821. }
  1822. #endif
  1823. return (0);
  1824. }
  1825. /*
  1826. * Set/clear promiscuous mode on interface ifp based on the truth value
  1827. * of pswitch. The calls are reference counted so that only the first
  1828. * "on" request actually has an effect, as does the final "off" request.
  1829. * Results are undefined if the "off" and "on" requests are not matched.
  1830. */
  1831. int
  1832. ifpromisc(struct ifnet *ifp, int pswitch)
  1833. {
  1834. struct ifreq ifr;
  1835. if (pswitch) {
  1836. /*
  1837. * If the device is not configured up, we cannot put it in
  1838. * promiscuous mode.
  1839. */
  1840. if ((ifp->if_flags & IFF_UP) == 0)
  1841. return (ENETDOWN);
  1842. if (ifp->if_pcount++ != 0)
  1843. return (0);
  1844. ifp->if_flags |= IFF_PROMISC;
  1845. } else {
  1846. if (--ifp->if_pcount > 0)
  1847. return (0);
  1848. ifp->if_flags &= ~IFF_PROMISC;
  1849. /*
  1850. * If the device is not configured up, we should not need to
  1851. * turn off promiscuous mode (device should have turned it
  1852. * off when interface went down; and will look at IFF_PROMISC
  1853. * again next time interface comes up).
  1854. */
  1855. if ((ifp->if_flags & IFF_UP) == 0)
  1856. return (0);
  1857. }
  1858. ifr.ifr_flags = ifp->if_flags;
  1859. return ((*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr));
  1860. }
  1861. int
  1862. sysctl_mq(int *name, u_int namelen, void *oldp, size_t *oldlenp,
  1863. void *newp, size_t newlen, struct mbuf_queue *mq)
  1864. {
  1865. /* All sysctl names at this level are terminal. */
  1866. if (namelen != 1)
  1867. return (ENOTDIR);
  1868. switch (name[0]) {
  1869. case IFQCTL_LEN:
  1870. return (sysctl_rdint(oldp, oldlenp, newp, mq_len(mq)));
  1871. case IFQCTL_MAXLEN:
  1872. return (sysctl_int(oldp, oldlenp, newp, newlen,
  1873. &mq->mq_maxlen)); /* XXX directly accessing maxlen */
  1874. case IFQCTL_DROPS:
  1875. return (sysctl_rdint(oldp, oldlenp, newp, mq_drops(mq)));
  1876. default:
  1877. return (EOPNOTSUPP);
  1878. }
  1879. /* NOTREACHED */
  1880. }
  1881. void
  1882. ifa_add(struct ifnet *ifp, struct ifaddr *ifa)
  1883. {
  1884. TAILQ_INSERT_TAIL(&ifp->if_addrlist, ifa, ifa_list);
  1885. }
  1886. void
  1887. ifa_del(struct ifnet *ifp, struct ifaddr *ifa)
  1888. {
  1889. TAILQ_REMOVE(&ifp->if_addrlist, ifa, ifa_list);
  1890. }
  1891. void
  1892. ifa_update_broadaddr(struct ifnet *ifp, struct ifaddr *ifa, struct sockaddr *sa)
  1893. {
  1894. if (ifa->ifa_broadaddr->sa_len != sa->sa_len)
  1895. panic("ifa_update_broadaddr does not support dynamic length");
  1896. bcopy(sa, ifa->ifa_broadaddr, sa->sa_len);
  1897. }
  1898. #ifdef DDB
  1899. /* debug function, can be called from ddb> */
  1900. void
  1901. ifa_print_all(void)
  1902. {
  1903. struct ifnet *ifp;
  1904. struct ifaddr *ifa;
  1905. TAILQ_FOREACH(ifp, &ifnet, if_list) {
  1906. TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) {
  1907. char addr[INET6_ADDRSTRLEN];
  1908. switch (ifa->ifa_addr->sa_family) {
  1909. case AF_INET:
  1910. printf("%s", inet_ntop(AF_INET,
  1911. &satosin(ifa->ifa_addr)->sin_addr,
  1912. addr, sizeof(addr)));
  1913. break;
  1914. #ifdef INET6
  1915. case AF_INET6:
  1916. printf("%s", inet_ntop(AF_INET6,
  1917. &(satosin6(ifa->ifa_addr))->sin6_addr,
  1918. addr, sizeof(addr)));
  1919. break;
  1920. #endif
  1921. }
  1922. printf(" on %s\n", ifa->ifa_ifp->if_xname);
  1923. }
  1924. }
  1925. }
  1926. #endif /* DDB */
  1927. void
  1928. ifnewlladdr(struct ifnet *ifp)
  1929. {
  1930. struct ifaddr *ifa;
  1931. struct ifreq ifrq;
  1932. short up;
  1933. int s;
  1934. s = splnet();
  1935. up = ifp->if_flags & IFF_UP;
  1936. if (up) {
  1937. /* go down for a moment... */
  1938. ifp->if_flags &= ~IFF_UP;
  1939. ifrq.ifr_flags = ifp->if_flags;
  1940. (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifrq);
  1941. }
  1942. ifp->if_flags |= IFF_UP;
  1943. ifrq.ifr_flags = ifp->if_flags;
  1944. (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifrq);
  1945. TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) {
  1946. if (ifa->ifa_addr != NULL &&
  1947. ifa->ifa_addr->sa_family == AF_INET)
  1948. arp_ifinit((struct arpcom *)ifp, ifa);
  1949. }
  1950. #ifdef INET6
  1951. /*
  1952. * Update the link-local address. Don't do it if we're
  1953. * a router to avoid confusing hosts on the network.
  1954. */
  1955. if (!ip6_forwarding) {
  1956. ifa = &in6ifa_ifpforlinklocal(ifp, 0)->ia_ifa;
  1957. if (ifa) {
  1958. in6_purgeaddr(ifa);
  1959. dohooks(ifp->if_addrhooks, 0);
  1960. in6_ifattach(ifp);
  1961. }
  1962. }
  1963. #endif
  1964. if (!up) {
  1965. /* go back down */
  1966. ifp->if_flags &= ~IFF_UP;
  1967. ifrq.ifr_flags = ifp->if_flags;
  1968. (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifrq);
  1969. }
  1970. splx(s);
  1971. }
  1972. int net_ticks;
  1973. u_int net_livelocks;
  1974. void
  1975. net_tick(void *null)
  1976. {
  1977. extern int ticks;
  1978. if (ticks - net_ticks > 1)
  1979. net_livelocks++;
  1980. net_ticks = ticks;
  1981. timeout_add(&net_tick_to, 1);
  1982. }
  1983. int
  1984. net_livelocked()
  1985. {
  1986. extern int ticks;
  1987. return (ticks - net_ticks > 1);
  1988. }
  1989. void
  1990. if_rxr_init(struct if_rxring *rxr, u_int lwm, u_int hwm)
  1991. {
  1992. extern int ticks;
  1993. memset(rxr, 0, sizeof(*rxr));
  1994. rxr->rxr_adjusted = ticks;
  1995. rxr->rxr_cwm = rxr->rxr_lwm = lwm;
  1996. rxr->rxr_hwm = hwm;
  1997. }
  1998. static inline void
  1999. if_rxr_adjust_cwm(struct if_rxring *rxr)
  2000. {
  2001. extern int ticks;
  2002. if (net_livelocked()) {
  2003. if (rxr->rxr_cwm > rxr->rxr_lwm)
  2004. rxr->rxr_cwm--;
  2005. else
  2006. return;
  2007. } else if (rxr->rxr_alive >= rxr->rxr_lwm)
  2008. return;
  2009. else if (rxr->rxr_cwm < rxr->rxr_hwm)
  2010. rxr->rxr_cwm++;
  2011. rxr->rxr_adjusted = ticks;
  2012. }
  2013. u_int
  2014. if_rxr_get(struct if_rxring *rxr, u_int max)
  2015. {
  2016. extern int ticks;
  2017. u_int diff;
  2018. if (ticks - rxr->rxr_adjusted >= 1) {
  2019. /* we're free to try for an adjustment */
  2020. if_rxr_adjust_cwm(rxr);
  2021. }
  2022. if (rxr->rxr_alive >= rxr->rxr_cwm)
  2023. return (0);
  2024. diff = min(rxr->rxr_cwm - rxr->rxr_alive, max);
  2025. rxr->rxr_alive += diff;
  2026. return (diff);
  2027. }
  2028. int
  2029. if_rxr_info_ioctl(struct if_rxrinfo *uifri, u_int t, struct if_rxring_info *e)
  2030. {
  2031. struct if_rxrinfo kifri;
  2032. int error;
  2033. u_int n;
  2034. error = copyin(uifri, &kifri, sizeof(kifri));
  2035. if (error)
  2036. return (error);
  2037. n = min(t, kifri.ifri_total);
  2038. kifri.ifri_total = t;
  2039. if (n > 0) {
  2040. error = copyout(e, kifri.ifri_entries, sizeof(*e) * n);
  2041. if (error)
  2042. return (error);
  2043. }
  2044. return (copyout(&kifri, uifri, sizeof(kifri)));
  2045. }
  2046. int
  2047. if_rxr_ioctl(struct if_rxrinfo *ifri, const char *name, u_int size,
  2048. struct if_rxring *rxr)
  2049. {
  2050. struct if_rxring_info ifr;
  2051. memset(&ifr, 0, sizeof(ifr));
  2052. if (name != NULL)
  2053. strlcpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
  2054. ifr.ifr_size = size;
  2055. ifr.ifr_info = *rxr;
  2056. return (if_rxr_info_ioctl(ifri, 1, &ifr));
  2057. }
  2058. /*
  2059. * Network stack input queues.
  2060. */
  2061. void
  2062. niq_init(struct niqueue *niq, u_int maxlen, u_int isr)
  2063. {
  2064. mq_init(&niq->ni_q, maxlen, IPL_NET);
  2065. niq->ni_isr = isr;
  2066. }
  2067. int
  2068. niq_enqueue(struct niqueue *niq, struct mbuf *m)
  2069. {
  2070. int rv;
  2071. rv = mq_enqueue(&niq->ni_q, m);
  2072. if (rv == 0)
  2073. schednetisr(niq->ni_isr);
  2074. else
  2075. if_congestion();
  2076. return (rv);
  2077. }
  2078. int
  2079. niq_enlist(struct niqueue *niq, struct mbuf_list *ml)
  2080. {
  2081. int rv;
  2082. rv = mq_enlist(&niq->ni_q, ml);
  2083. if (rv == 0)
  2084. schednetisr(niq->ni_isr);
  2085. else
  2086. if_congestion();
  2087. return (rv);
  2088. }
  2089. __dead void
  2090. unhandled_af(int af)
  2091. {
  2092. panic("unhandled af %d", af);
  2093. }