@@ -310,44 +310,29 @@ if_init(struct interface *ifp)
310310 return if_writepathuint (ifp -> ctx , path , 1 ) == -1 ? -1 : 0 ;
311311}
312312
313- /* Returns number of bytes written to driver, 0 on error.
314- * @driverlen note that sizeof(ethtool_drvinfo.driver) = 32 */
313+ /* Returns number of bytes written to driver, else 0 (error or indeterminate). */
315314static size_t
316- if_get_driver (const char * ifname , char * driver , const size_t driverlen )
315+ if_get_driver (struct interface * ifp , char * driver , const size_t driverlen )
317316{
318- struct ifreq ifr = { .ifr_flags = 0 };
319- struct ethtool_drvinfo drvinfo ;
320- int fd ;
321- size_t n = 0 ;
317+ struct ethtool_drvinfo drvinfo = { .cmd = ETHTOOL_GDRVINFO };
318+ struct ifreq ifr = { .ifr_data = (void * )& drvinfo };
322319
323- fd = socket (AF_INET , SOCK_STREAM , 0 );
324- if (fd == -1 ) {
325- logerr ("%s: socket ifname=%s" , __func__ , ifname );
326- goto eexit ;
327- }
328- memset (& ifr , 0 , sizeof (ifr ));
329- strlcpy (ifr .ifr_name , ifname , sizeof (ifr .ifr_name ));
330- drvinfo .cmd = ETHTOOL_GDRVINFO ;
331- ifr .ifr_data = (void * )& drvinfo ;
332- if (ioctl (fd , SIOCETHTOOL , & ifr ) == -1 ) {
333- logerr ("%s: SIOCETHTOOL ifname=%s" , __func__ , ifname );
334- goto eexit ;
320+ strlcpy (ifr .ifr_name , ifp -> name , sizeof (ifr .ifr_name ));
321+ if (ioctl (ifp -> ctx -> pf_inet_fd , SIOCETHTOOL , & ifr ) != 0 ) {
322+ logerr ("%s: SIOCETHTOOL ifname=%s" , __func__ , ifp -> name );
323+ return 0 ; /* 0 means error or indeterminate driver name */
335324 }
336- n = strlcpy (driver , drvinfo .driver , driverlen );
337- eexit :
338- if (fd != -1 )
339- close (fd );
340- return n ;
325+ return strlcpy (driver , drvinfo .driver , driverlen );
341326}
342327
343328static bool
344- if_cmp_driver (const char * ifname , const char * driver )
329+ if_cmp_driver (struct interface * ifp , const char * driver )
345330{
346- char ifdriver [32 ]; /* see ethtool_drvinfo.driver declaration */
347- size_t n = if_get_driver (ifname , ifdriver , sizeof (ifdriver ));
331+ char ifdriver [FIELD_SIZEOF ( struct ethtool_drvinfo , driver )];
332+ size_t n = if_get_driver (ifp , ifdriver , sizeof (ifdriver ));
348333
349334 if (n == 0 ) {
350- logerr ("%s: if_get_driver ifname=%s" , __func__ , ifname );
335+ logerr ("%s: if_get_driver ifname=%s" , __func__ , ifp -> name );
351336 return false;
352337 }
353338 if (strncmp (ifdriver , driver , n ) == 0 )
@@ -358,7 +343,7 @@ if_cmp_driver(const char *ifname, const char *driver)
358343static bool
359344if_ipvlan (struct interface * ifp )
360345{
361- if (if_cmp_driver (ifp -> name , "ipvlan" ))
346+ if (if_cmp_driver (ifp , "ipvlan" ))
362347 return true;
363348 return false;
364349}
0 commit comments